summary refs log tree commit diff
path: root/src/core/devices/nm-device-ppp.c
blob: 61e32348c48a431445ff894ca09ca36731fa0061 (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
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2017 Red Hat, Inc.
 */

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

#include "nm-device-ppp.h"

#include "nm-l3-config-data.h"
#include "nm-act-request.h"
#include "nm-device-factory.h"
#include "nm-device-private.h"
#include "nm-manager.h"
#include "nm-setting-pppoe.h"
#include "libnm-platform/nm-platform.h"
#include "ppp/nm-ppp-mgr.h"

#define _NMLOG_DEVICE_TYPE NMDevicePpp
#include "nm-device-logging.h"

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

typedef struct _NMDevicePppPrivate {
    NMPppMgr *ppp_mgr;
} NMDevicePppPrivate;

struct _NMDevicePpp {
    NMDevice           parent;
    NMDevicePppPrivate _priv;
};

struct _NMDevicePppClass {
    NMDeviceClass parent;
};

G_DEFINE_TYPE(NMDevicePpp, nm_device_ppp, NM_TYPE_DEVICE)

#define NM_DEVICE_PPP_GET_PRIVATE(self) \
    _NM_GET_PRIVATE(self, NMDevicePpp, NM_IS_DEVICE_PPP, NMDevice)

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

static NMDeviceCapabilities
get_generic_capabilities(NMDevice *device)
{
    return NM_DEVICE_CAP_IS_SOFTWARE;
}

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

static void
_ppp_mgr_cleanup(NMDevicePpp *self)
{
    NMDevicePppPrivate *priv = NM_DEVICE_PPP_GET_PRIVATE(self);

    nm_clear_pointer(&priv->ppp_mgr, nm_ppp_mgr_destroy);
}

static void
_ppp_mgr_stage3_maybe_ready(NMDevicePpp *self)
{
    NMDevice           *device = NM_DEVICE(self);
    NMDevicePppPrivate *priv   = NM_DEVICE_PPP_GET_PRIVATE(self);
    int                 IS_IPv4;

    for (IS_IPv4 = 1; IS_IPv4 >= 0; IS_IPv4--) {
        const int             addr_family = IS_IPv4 ? AF_INET : AF_INET6;
        const NMPppMgrIPData *ip_data;

        ip_data = nm_ppp_mgr_get_ip_data(priv->ppp_mgr, addr_family);
        if (ip_data->ip_received)
            nm_device_devip_set_state(device, addr_family, NM_DEVICE_IP_STATE_READY, ip_data->l3cd);
    }

    if (nm_ppp_mgr_get_state(priv->ppp_mgr) >= NM_PPP_MGR_STATE_HAVE_IP_CONFIG)
        nm_device_devip_set_state(device, AF_UNSPEC, NM_DEVICE_IP_STATE_READY, NULL);
}

static void
_ppp_mgr_callback(NMPppMgr *ppp_mgr, const NMPppMgrCallbackData *callback_data, gpointer user_data)
{
    NMDevicePpp  *self   = NM_DEVICE_PPP(user_data);
    NMDevice     *device = NM_DEVICE(self);
    NMDeviceState device_state;

    if (callback_data->callback_type != NM_PPP_MGR_CALLBACK_TYPE_STATE_CHANGED)
        return;

    device_state = nm_device_get_state(device);

    if (callback_data->data.state >= _NM_PPP_MGR_STATE_FAILED_START) {
        if (device_state <= NM_DEVICE_STATE_ACTIVATED)
            nm_device_state_changed(device, NM_DEVICE_STATE_FAILED, callback_data->data.reason);
        return;
    }

    if (device_state < NM_DEVICE_STATE_IP_CONFIG) {
        if (callback_data->data.state >= NM_PPP_MGR_STATE_HAVE_IFINDEX) {
            gs_free char         *old_name = NULL;
            gs_free_error GError *error    = NULL;

            if (!nm_device_take_over_link(device, callback_data->data.ifindex, &old_name, &error)) {
                _LOGW(LOGD_DEVICE | LOGD_PPP,
                      "could not take control of link %d: %s",
                      callback_data->data.ifindex,
                      error->message);
                _ppp_mgr_cleanup(self);
                nm_device_state_changed(device,
                                        NM_DEVICE_STATE_FAILED,
                                        NM_DEVICE_STATE_REASON_CONFIG_FAILED);
                return;
            }

            if (old_name)
                nm_manager_remove_device(NM_MANAGER_GET, old_name, NM_DEVICE_TYPE_PPP);

            nm_device_activate_schedule_stage2_device_config(device, FALSE);
        }
        return;
    }

    _ppp_mgr_stage3_maybe_ready(self);
}

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

static gboolean
check_connection_compatible(NMDevice *device, NMConnection *connection, GError **error)
{
    NMSettingPppoe *s_pppoe;

    if (!NM_DEVICE_CLASS(nm_device_ppp_parent_class)
             ->check_connection_compatible(device, connection, error))
        return FALSE;

    s_pppoe = nm_connection_get_setting_pppoe(connection);
    if (!s_pppoe || !nm_setting_pppoe_get_parent(s_pppoe)) {
        nm_utils_error_set_literal(error,
                                   NM_UTILS_ERROR_CONNECTION_AVAILABLE_INCOMPATIBLE,
                                   "the connection doesn't specify a PPPoE parent interface");
        return FALSE;
    }

    return TRUE;
}

static NMActStageReturn
act_stage2_config(NMDevice *device, NMDeviceStateReason *out_failure_reason)
{
    NMDevicePpp        *self = NM_DEVICE_PPP(device);
    NMDevicePppPrivate *priv = NM_DEVICE_PPP_GET_PRIVATE(self);
    NMSettingPppoe     *s_pppoe;
    NMActRequest       *req;

    if (!priv->ppp_mgr) {
        gs_free_error GError *error = NULL;

        req = nm_device_get_act_request(device);
        g_return_val_if_fail(req, NM_ACT_STAGE_RETURN_FAILURE);

        s_pppoe = nm_device_get_applied_setting(device, NM_TYPE_SETTING_PPPOE);
        g_return_val_if_fail(s_pppoe, NM_ACT_STAGE_RETURN_FAILURE);

        priv->ppp_mgr = nm_ppp_mgr_start(&((const NMPppMgrConfig){
                                             .netns        = nm_device_get_netns(device),
                                             .parent_iface = nm_setting_pppoe_get_parent(s_pppoe),
                                             .callback     = _ppp_mgr_callback,
                                             .user_data    = self,
                                             .act_req      = req,
                                             .ppp_username = nm_setting_pppoe_get_username(s_pppoe),
                                             .timeout_secs = 30,
                                             .baud_override = 0,
                                         }),
                                         &error);
        if (!priv->ppp_mgr) {
            _LOGW(LOGD_DEVICE | LOGD_PPP, "PPPoE failed to start: %s", error->message);
            *out_failure_reason = NM_DEVICE_STATE_REASON_PPP_START_FAILED;
            return NM_ACT_STAGE_RETURN_FAILURE;
        }

        return NM_ACT_STAGE_RETURN_POSTPONE;
    }

    if (nm_ppp_mgr_get_state(priv->ppp_mgr) < NM_PPP_MGR_STATE_HAVE_IFINDEX)
        return NM_ACT_STAGE_RETURN_POSTPONE;

    return NM_ACT_STAGE_RETURN_SUCCESS;
}

static void
act_stage3_ip_config(NMDevice *device, int addr_family)
{
    NMDevicePpp        *self = NM_DEVICE_PPP(device);
    NMDevicePppPrivate *priv = NM_DEVICE_PPP_GET_PRIVATE(self);
    NMPppMgrState       ppp_state;

    if (!priv->ppp_mgr) {
        nm_assert_not_reached();
        return;
    }

    ppp_state = nm_ppp_mgr_get_state(priv->ppp_mgr);

    nm_assert(NM_IN_SET(ppp_state, NM_PPP_MGR_STATE_HAVE_IFINDEX, NM_PPP_MGR_STATE_HAVE_IP_CONFIG));

    if (ppp_state < NM_PPP_MGR_STATE_HAVE_IP_CONFIG) {
        nm_device_devip_set_state(device, AF_UNSPEC, NM_DEVICE_IP_STATE_PENDING, NULL);
        return;
    }

    _ppp_mgr_stage3_maybe_ready(self);
}

static const char *
get_ip_method_auto(NMDevice *device, int addr_family)
{
    if (NM_IS_IPv4(addr_family)) {
        /* We cannot do DHCPv4 on a PPP link, instead we get "auto" IP addresses
         * by pppd. Return "manual" here, which has the suitable effect to a
         * (zero) manual addresses in addition. */
        return NM_SETTING_IP6_CONFIG_METHOD_MANUAL;
    }

    /* We get a interface identifier via IPV6CP, used to construct a link-local
     * address. Method auto means autoconf6 as usual.*/
    return NM_SETTING_IP6_CONFIG_METHOD_AUTO;
}

static gboolean
create_and_realize(NMDevice              *device,
                   NMConnection          *connection,
                   NMDevice              *parent,
                   const NMPlatformLink **out_plink,
                   GError               **error)
{
    int parent_ifindex;

    if (!parent) {
        g_set_error(error,
                    NM_DEVICE_ERROR,
                    NM_DEVICE_ERROR_MISSING_DEPENDENCIES,
                    "PPP devices can not be created without a parent interface");
        return FALSE;
    }

    parent_ifindex = nm_device_get_ifindex(parent);
    g_warn_if_fail(parent_ifindex > 0);

    nm_device_parent_set_ifindex(device, parent_ifindex);

    /* The interface is created later */

    return TRUE;
}

static void
deactivate(NMDevice *device)
{
    NMDevicePpp *self = NM_DEVICE_PPP(device);

    _ppp_mgr_cleanup(self);
}

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

static void
nm_device_ppp_init(NMDevicePpp *self)
{}

static void
dispose(GObject *object)
{
    NMDevicePpp *self = NM_DEVICE_PPP(object);

    _ppp_mgr_cleanup(self);

    G_OBJECT_CLASS(nm_device_ppp_parent_class)->dispose(object);
}

static const NMDBusInterfaceInfoExtended interface_info_device_ppp = {
    .parent = NM_DEFINE_GDBUS_INTERFACE_INFO_INIT(NM_DBUS_INTERFACE_DEVICE_PPP, ),
};

static void
nm_device_ppp_class_init(NMDevicePppClass *klass)
{
    GObjectClass      *object_class      = G_OBJECT_CLASS(klass);
    NMDBusObjectClass *dbus_object_class = NM_DBUS_OBJECT_CLASS(klass);
    NMDeviceClass     *device_class      = NM_DEVICE_CLASS(klass);

    object_class->dispose = dispose;

    dbus_object_class->interface_infos = NM_DBUS_INTERFACE_INFOS(&interface_info_device_ppp);

    device_class->connection_type_supported        = NM_SETTING_PPPOE_SETTING_NAME;
    device_class->connection_type_check_compatible = NM_SETTING_PPPOE_SETTING_NAME;
    device_class->link_types                       = NM_DEVICE_DEFINE_LINK_TYPES(NM_LINK_TYPE_PPP);

    device_class->act_stage2_config           = act_stage2_config;
    device_class->act_stage3_ip_config        = act_stage3_ip_config;
    device_class->get_ip_method_auto          = get_ip_method_auto;
    device_class->check_connection_compatible = check_connection_compatible;
    device_class->create_and_realize          = create_and_realize;
    device_class->deactivate                  = deactivate;
    device_class->get_generic_capabilities    = get_generic_capabilities;
}

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

#define NM_TYPE_PPP_DEVICE_FACTORY (nm_ppp_device_factory_get_type())
#define NM_PPP_DEVICE_FACTORY(obj) \
    (G_TYPE_CHECK_INSTANCE_CAST((obj), NM_TYPE_PPP_DEVICE_FACTORY, NMPppDeviceFactory))

static NMDevice *
create_device(NMDeviceFactory      *factory,
              const char           *iface,
              const NMPlatformLink *plink,
              NMConnection         *connection,
              gboolean             *out_ignore)
{
    return g_object_new(NM_TYPE_DEVICE_PPP,
                        NM_DEVICE_IFACE,
                        iface,
                        NM_DEVICE_TYPE_DESC,
                        "Ppp",
                        NM_DEVICE_DEVICE_TYPE,
                        NM_DEVICE_TYPE_PPP,
                        NM_DEVICE_LINK_TYPE,
                        NM_LINK_TYPE_PPP,
                        NULL);
}

static gboolean
match_connection(NMDeviceFactory *factory, NMConnection *connection)
{
    NMSettingPppoe *s_pppoe;

    s_pppoe = nm_connection_get_setting_pppoe(connection);
    nm_assert(s_pppoe);

    return !!nm_setting_pppoe_get_parent(s_pppoe);
}

static const char *
get_connection_parent(NMDeviceFactory *factory, NMConnection *connection)
{
    NMSettingPppoe *s_pppoe;

    nm_assert(nm_connection_is_type(connection, NM_SETTING_PPPOE_SETTING_NAME));

    s_pppoe = nm_connection_get_setting_pppoe(connection);
    nm_assert(s_pppoe);

    return nm_setting_pppoe_get_parent(s_pppoe);
}

static char *
get_connection_iface(NMDeviceFactory *factory, NMConnection *connection, const char *parent_iface)
{
    nm_assert(nm_connection_is_type(connection, NM_SETTING_PPPOE_SETTING_NAME));

    if (!parent_iface)
        return NULL;

    return g_strdup(nm_connection_get_interface_name(connection));
}

NM_DEVICE_FACTORY_DEFINE_INTERNAL(
    PPP,
    Ppp,
    ppp,
    NM_DEVICE_FACTORY_DECLARE_LINK_TYPES(NM_LINK_TYPE_PPP)
        NM_DEVICE_FACTORY_DECLARE_SETTING_TYPES(NM_SETTING_PPPOE_SETTING_NAME),
    factory_class->get_connection_parent = get_connection_parent;
    factory_class->get_connection_iface  = get_connection_iface;
    factory_class->create_device         = create_device;
    factory_class->match_connection      = match_connection;);