summaryrefslogtreecommitdiff
path: root/src/ndisc/nm-fake-ndisc.c
blob: 634a48c2f33c5a4eaa73a1678b4cc047dd05bd09 (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
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2013 Red Hat, Inc.
 */

#include "nm-default.h"

#include "nm-fake-ndisc.h"

#include <arpa/inet.h>

#include "nm-ndisc-private.h"

#define _NMLOG_PREFIX_NAME "ndisc-fake"

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

typedef struct {
    guint id;
    guint when;

    NMNDiscDHCPLevel dhcp_level;
    GArray *         gateways;
    GArray *         prefixes;
    GArray *         dns_servers;
    GArray *         dns_domains;
    int              hop_limit;
    guint32          mtu;
} FakeRa;

typedef struct {
    struct in6_addr    network;
    int                plen;
    struct in6_addr    gateway;
    guint32            timestamp;
    guint32            lifetime;
    guint32            preferred;
    NMIcmpv6RouterPref preference;
} FakePrefix;

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

enum {
    RS_SENT,
    LAST_SIGNAL,
};
static guint signals[LAST_SIGNAL] = {0};

typedef struct {
    guint   receive_ra_id;
    GSList *ras;
} NMFakeNDiscPrivate;

struct _NMFakeRNDisc {
    NMNDisc            parent;
    NMFakeNDiscPrivate _priv;
};

struct _NMFakeRNDiscClass {
    NMNDiscClass parent;
};

G_DEFINE_TYPE(NMFakeNDisc, nm_fake_ndisc, NM_TYPE_NDISC)

#define NM_FAKE_NDISC_GET_PRIVATE(self) \
    _NM_GET_PRIVATE(self, NMFakeNDisc, NM_IS_FAKE_NDISC, NMNDisc)

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

static void
fake_ra_free(gpointer data)
{
    FakeRa *ra = data;

    g_array_free(ra->gateways, TRUE);
    g_array_free(ra->prefixes, TRUE);
    g_array_free(ra->dns_servers, TRUE);
    g_array_free(ra->dns_domains, TRUE);
    g_free(ra);
}

static void
ra_dns_domain_free(gpointer data)
{
    g_free(((NMNDiscDNSDomain *) (data))->domain);
}

static FakeRa *
find_ra(GSList *ras, guint id)
{
    GSList *iter;

    for (iter = ras; iter; iter = iter->next) {
        if (((FakeRa *) iter->data)->id == id)
            return iter->data;
    }
    return NULL;
}

guint
nm_fake_ndisc_add_ra(NMFakeNDisc *    self,
                     guint            seconds_after_previous,
                     NMNDiscDHCPLevel dhcp_level,
                     int              hop_limit,
                     guint32          mtu)
{
    NMFakeNDiscPrivate *priv    = NM_FAKE_NDISC_GET_PRIVATE(self);
    static guint        counter = 1;
    FakeRa *            ra;

    ra              = g_malloc0(sizeof(*ra));
    ra->id          = counter++;
    ra->when        = seconds_after_previous;
    ra->dhcp_level  = dhcp_level;
    ra->hop_limit   = hop_limit;
    ra->mtu         = mtu;
    ra->gateways    = g_array_new(FALSE, FALSE, sizeof(NMNDiscGateway));
    ra->prefixes    = g_array_new(FALSE, FALSE, sizeof(FakePrefix));
    ra->dns_servers = g_array_new(FALSE, FALSE, sizeof(NMNDiscDNSServer));
    ra->dns_domains = g_array_new(FALSE, FALSE, sizeof(NMNDiscDNSDomain));
    g_array_set_clear_func(ra->dns_domains, ra_dns_domain_free);

    priv->ras = g_slist_append(priv->ras, ra);
    return ra->id;
}

void
nm_fake_ndisc_add_gateway(NMFakeNDisc *      self,
                          guint              ra_id,
                          const char *       addr,
                          guint32            timestamp,
                          guint32            lifetime,
                          NMIcmpv6RouterPref preference)
{
    NMFakeNDiscPrivate *priv = NM_FAKE_NDISC_GET_PRIVATE(self);
    FakeRa *            ra   = find_ra(priv->ras, ra_id);
    NMNDiscGateway *    gw;

    g_assert(ra);
    g_array_set_size(ra->gateways, ra->gateways->len + 1);
    gw = &g_array_index(ra->gateways, NMNDiscGateway, ra->gateways->len - 1);
    g_assert(inet_pton(AF_INET6, addr, &gw->address) == 1);
    gw->timestamp  = timestamp;
    gw->lifetime   = lifetime;
    gw->preference = preference;
}

void
nm_fake_ndisc_add_prefix(NMFakeNDisc *      self,
                         guint              ra_id,
                         const char *       network,
                         guint              plen,
                         const char *       gateway,
                         guint32            timestamp,
                         guint32            lifetime,
                         guint32            preferred,
                         NMIcmpv6RouterPref preference)
{
    NMFakeNDiscPrivate *priv = NM_FAKE_NDISC_GET_PRIVATE(self);
    FakeRa *            ra   = find_ra(priv->ras, ra_id);
    FakePrefix *        prefix;

    g_assert(ra);
    g_array_set_size(ra->prefixes, ra->prefixes->len + 1);
    prefix = &g_array_index(ra->prefixes, FakePrefix, ra->prefixes->len - 1);
    memset(prefix, 0, sizeof(*prefix));
    g_assert(inet_pton(AF_INET6, network, &prefix->network) == 1);
    g_assert(inet_pton(AF_INET6, gateway, &prefix->gateway) == 1);
    prefix->plen       = plen;
    prefix->timestamp  = timestamp;
    prefix->lifetime   = lifetime;
    prefix->preferred  = preferred;
    prefix->preference = preference;
}

void
nm_fake_ndisc_add_dns_server(NMFakeNDisc *self,
                             guint        ra_id,
                             const char * address,
                             guint32      timestamp,
                             guint32      lifetime)
{
    NMFakeNDiscPrivate *priv = NM_FAKE_NDISC_GET_PRIVATE(self);
    FakeRa *            ra   = find_ra(priv->ras, ra_id);
    NMNDiscDNSServer *  dns;

    g_assert(ra);
    g_array_set_size(ra->dns_servers, ra->dns_servers->len + 1);
    dns = &g_array_index(ra->dns_servers, NMNDiscDNSServer, ra->dns_servers->len - 1);
    g_assert(inet_pton(AF_INET6, address, &dns->address) == 1);
    dns->timestamp = timestamp;
    dns->lifetime  = lifetime;
}

void
nm_fake_ndisc_add_dns_domain(NMFakeNDisc *self,
                             guint        ra_id,
                             const char * domain,
                             guint32      timestamp,
                             guint32      lifetime)
{
    NMFakeNDiscPrivate *priv = NM_FAKE_NDISC_GET_PRIVATE(self);
    FakeRa *            ra   = find_ra(priv->ras, ra_id);
    NMNDiscDNSDomain *  dns;

    g_assert(ra);
    g_array_set_size(ra->dns_domains, ra->dns_domains->len + 1);
    dns            = &g_array_index(ra->dns_domains, NMNDiscDNSDomain, ra->dns_domains->len - 1);
    dns->domain    = g_strdup(domain);
    dns->timestamp = timestamp;
    dns->lifetime  = lifetime;
}

gboolean
nm_fake_ndisc_done(NMFakeNDisc *self)
{
    return !NM_FAKE_NDISC_GET_PRIVATE(self)->ras;
}

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

static gboolean
send_rs(NMNDisc *ndisc, GError **error)
{
    _LOGT("send_rs()");
    g_signal_emit(ndisc, signals[RS_SENT], 0);
    return TRUE;
}

static gboolean
receive_ra(gpointer user_data)
{
    NMFakeNDisc *        self    = user_data;
    NMFakeNDiscPrivate * priv    = NM_FAKE_NDISC_GET_PRIVATE(self);
    NMNDisc *            ndisc   = NM_NDISC(self);
    NMNDiscDataInternal *rdata   = ndisc->rdata;
    FakeRa *             ra      = priv->ras->data;
    NMNDiscConfigMap     changed = 0;
    gint32               now     = nm_utils_get_monotonic_timestamp_sec();
    guint                i;
    NMNDiscDHCPLevel     dhcp_level;

    priv->receive_ra_id = 0;

    /* preserve the "most managed" level  on updates. */
    dhcp_level = MAX(rdata->public.dhcp_level, ra->dhcp_level);

    if (rdata->public.dhcp_level != dhcp_level) {
        rdata->public.dhcp_level = dhcp_level;
        changed |= NM_NDISC_CONFIG_DHCP_LEVEL;
    }

    for (i = 0; i < ra->gateways->len; i++) {
        NMNDiscGateway *item = &g_array_index(ra->gateways, NMNDiscGateway, i);

        if (nm_ndisc_add_gateway(ndisc, item))
            changed |= NM_NDISC_CONFIG_GATEWAYS;
    }

    for (i = 0; i < ra->prefixes->len; i++) {
        FakePrefix * item  = &g_array_index(ra->prefixes, FakePrefix, i);
        NMNDiscRoute route = {
            .network    = item->network,
            .plen       = item->plen,
            .gateway    = item->gateway,
            .timestamp  = item->timestamp,
            .lifetime   = item->lifetime,
            .preference = item->preference,
        };

        g_assert(route.plen > 0 && route.plen <= 128);

        if (nm_ndisc_add_route(ndisc, &route))
            changed |= NM_NDISC_CONFIG_ROUTES;

        if (item->plen == 64) {
            NMNDiscAddress address = {
                .address     = item->network,
                .timestamp   = item->timestamp,
                .lifetime    = item->lifetime,
                .preferred   = item->preferred,
                .dad_counter = 0,
            };

            if (nm_ndisc_complete_and_add_address(ndisc, &address, now))
                changed |= NM_NDISC_CONFIG_ADDRESSES;
        }
    }

    for (i = 0; i < ra->dns_servers->len; i++) {
        NMNDiscDNSServer *item = &g_array_index(ra->dns_servers, NMNDiscDNSServer, i);

        if (nm_ndisc_add_dns_server(ndisc, item))
            changed |= NM_NDISC_CONFIG_DNS_SERVERS;
    }

    for (i = 0; i < ra->dns_domains->len; i++) {
        NMNDiscDNSDomain *item = &g_array_index(ra->dns_domains, NMNDiscDNSDomain, i);

        if (nm_ndisc_add_dns_domain(ndisc, item))
            changed |= NM_NDISC_CONFIG_DNS_DOMAINS;
    }

    if (rdata->public.mtu != ra->mtu) {
        rdata->public.mtu = ra->mtu;
        changed |= NM_NDISC_CONFIG_MTU;
    }

    if (rdata->public.hop_limit != ra->hop_limit) {
        rdata->public.hop_limit = ra->hop_limit;
        changed |= NM_NDISC_CONFIG_HOP_LIMIT;
    }

    priv->ras = g_slist_remove(priv->ras, priv->ras->data);
    fake_ra_free(ra);

    nm_ndisc_ra_received(NM_NDISC(self), now, changed);

    /* Schedule next RA */
    if (priv->ras) {
        ra                  = priv->ras->data;
        priv->receive_ra_id = g_timeout_add_seconds(ra->when, receive_ra, self);
    }

    return G_SOURCE_REMOVE;
}

static void
start(NMNDisc *ndisc)
{
    NMFakeNDiscPrivate *priv = NM_FAKE_NDISC_GET_PRIVATE(ndisc);
    FakeRa *            ra;

    /* Queue up the first fake RA */
    g_assert(priv->ras);
    ra = priv->ras->data;

    g_assert(!priv->receive_ra_id);
    priv->receive_ra_id = g_timeout_add_seconds(ra->when, receive_ra, ndisc);
}

static void
stop(NMNDisc *ndisc)
{
    NMFakeNDiscPrivate *priv = NM_FAKE_NDISC_GET_PRIVATE(ndisc);

    nm_clear_g_source(&priv->receive_ra_id);
}

void
nm_fake_ndisc_emit_new_ras(NMFakeNDisc *self)
{
    if (!NM_FAKE_NDISC_GET_PRIVATE(self)->receive_ra_id)
        start(NM_NDISC(self));
}

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

static void
nm_fake_ndisc_init(NMFakeNDisc *fake_ndisc)
{}

NMNDisc *
nm_fake_ndisc_new(int ifindex, const char *ifname)
{
    return g_object_new(NM_TYPE_FAKE_NDISC,
                        NM_NDISC_IFINDEX,
                        ifindex,
                        NM_NDISC_IFNAME,
                        ifname,
                        NM_NDISC_NODE_TYPE,
                        (int) NM_NDISC_NODE_TYPE_HOST,
                        NM_NDISC_STABLE_TYPE,
                        (int) NM_UTILS_STABLE_TYPE_UUID,
                        NM_NDISC_NETWORK_ID,
                        "fake",
                        NM_NDISC_MAX_ADDRESSES,
                        NM_NDISC_MAX_ADDRESSES_DEFAULT,
                        NM_NDISC_ROUTER_SOLICITATIONS,
                        NM_NDISC_ROUTER_SOLICITATIONS_DEFAULT,
                        NM_NDISC_ROUTER_SOLICITATION_INTERVAL,
                        NM_NDISC_ROUTER_SOLICITATION_INTERVAL_DEFAULT,
                        NM_NDISC_RA_TIMEOUT,
                        30u,
                        NULL);
}

static void
dispose(GObject *object)
{
    NMFakeNDiscPrivate *priv = NM_FAKE_NDISC_GET_PRIVATE(object);

    nm_clear_g_source(&priv->receive_ra_id);

    g_slist_free_full(priv->ras, fake_ra_free);
    priv->ras = NULL;

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

static void
nm_fake_ndisc_class_init(NMFakeNDiscClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS(klass);
    NMNDiscClass *ndisc_class  = NM_NDISC_CLASS(klass);

    object_class->dispose = dispose;

    ndisc_class->start   = start;
    ndisc_class->stop    = stop;
    ndisc_class->send_rs = send_rs;

    signals[RS_SENT] = g_signal_new(NM_FAKE_NDISC_RS_SENT,
                                    G_OBJECT_CLASS_TYPE(klass),
                                    G_SIGNAL_RUN_FIRST,
                                    0,
                                    NULL,
                                    NULL,
                                    NULL,
                                    G_TYPE_NONE,
                                    0);
}