summaryrefslogtreecommitdiff
path: root/src/network/networkd-dhcp6.c
blob: 3cb7b8d9caccf4448dd51b280b2aa34dd163d4fb (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
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/

/***
  This file is part of systemd.

  Copyright (C) 2014 Intel Corporation. All rights reserved.

  systemd is free software; you can redistribute it and/or modify it
  under the terms of the GNU Lesser General Public License as published by
  the Free Software Foundation; either version 2.1 of the License, or
  (at your option) any later version.

  systemd is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/

#include <netinet/ether.h>
#include <linux/if.h>

#include "networkd-link.h"
#include "network-internal.h"

#include "sd-icmp6-nd.h"
#include "sd-dhcp6-client.h"

static int dhcp6_lease_address_acquired(sd_dhcp6_client *client, Link *link);

static int dhcp6_lease_information_acquired(sd_dhcp6_client *client,
                                        Link *link) {
        return 0;
}

static int dhcp6_address_handler(sd_netlink *rtnl, sd_netlink_message *m,
                                 void *userdata) {
        _cleanup_link_unref_ Link *link = userdata;
        int r;

        assert(link);

        r = sd_netlink_message_get_errno(m);
        if (r < 0 && r != -EEXIST) {
                if (link->rtnl_extended_attrs) {
                        log_link_warning(link, "Could not set extended netlink attributes, reverting to fallback mechanism");

                        link->rtnl_extended_attrs = false;
                        dhcp6_lease_address_acquired(link->dhcp6_client, link);

                        return 1;
                }

                log_link_error_errno(link, r, "Could not set DHCPv6 address: %m");

                link_enter_failed(link);

        } else if (r >= 0)
                link_rtnl_process_address(rtnl, m, link->manager);

        return 1;
}

static int dhcp6_address_update(Link *link, struct in6_addr *ip6_addr,
                                uint8_t prefixlen, uint32_t lifetime_preferred,
                                uint32_t lifetime_valid) {
        int r;
        _cleanup_address_free_ Address *addr = NULL;

        r = address_new_dynamic(&addr);
        if (r < 0)
                return r;

        addr->family = AF_INET6;
        memcpy(&addr->in_addr.in6, ip6_addr, sizeof(*ip6_addr));

        addr->flags = IFA_F_NOPREFIXROUTE;
        addr->prefixlen = prefixlen;

        addr->cinfo.ifa_prefered = lifetime_preferred;
        addr->cinfo.ifa_valid = lifetime_valid;

        log_link_info(link,
                      "DHCPv6 address "SD_ICMP6_ND_ADDRESS_FORMAT_STR"/%d timeout preferred %d valid %d",
                      SD_ICMP6_ND_ADDRESS_FORMAT_VAL(addr->in_addr.in6),
                      addr->prefixlen, lifetime_preferred, lifetime_valid);

        r = address_update(addr, link, dhcp6_address_handler);
        if (r < 0)
                log_link_warning_errno(link, r, "Could not assign DHCPv6 address: %m");

        return r;
}

static int dhcp6_lease_address_acquired(sd_dhcp6_client *client, Link *link) {
        int r;
        sd_dhcp6_lease *lease;
        struct in6_addr ip6_addr;
        uint32_t lifetime_preferred, lifetime_valid;
        uint8_t prefixlen;

        r = sd_dhcp6_client_get_lease(client, &lease);
        if (r < 0)
                return r;

        sd_dhcp6_lease_reset_address_iter(lease);

        while (sd_dhcp6_lease_get_address(lease, &ip6_addr,
                                                &lifetime_preferred,
                                                &lifetime_valid) >= 0) {

                r = sd_icmp6_ra_get_prefixlen(link->icmp6_router_discovery,
                                        &ip6_addr, &prefixlen);
                if (r < 0 && r != -EADDRNOTAVAIL) {
                        log_link_warning_errno(link, r, "Could not get prefix information: %m");
                        return r;
                }

                if (r == -EADDRNOTAVAIL)
                        prefixlen = 128;

                r = dhcp6_address_update(link, &ip6_addr, prefixlen,
                                        lifetime_preferred, lifetime_valid);
                if (r < 0)
                        return r;
        }

        return 0;
}

static void dhcp6_handler(sd_dhcp6_client *client, int event, void *userdata) {
        int r;
        Link *link = userdata;

        assert(link);
        assert(link->network);
        assert(link->manager);

        if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
                return;

        switch(event) {
        case SD_DHCP6_CLIENT_EVENT_STOP:
        case SD_DHCP6_CLIENT_EVENT_RESEND_EXPIRE:
        case SD_DHCP6_CLIENT_EVENT_RETRANS_MAX:
                log_link_warning(link, "DHCPv6 lease lost");

                link->dhcp6_configured = false;
                break;

        case SD_DHCP6_CLIENT_EVENT_IP_ACQUIRE:
                r = dhcp6_lease_address_acquired(client, link);
                if (r < 0) {
                        link_enter_failed(link);
                        return;
                }

                /* fall through */
        case SD_DHCP6_CLIENT_EVENT_INFORMATION_REQUEST:
                r = dhcp6_lease_information_acquired(client, link);
                if (r < 0) {
                        link_enter_failed(link);
                        return;
                }

                link->dhcp6_configured = true;
                break;

        default:
                if (event < 0)
                        log_link_warning_errno(link, event, "DHCPv6 error: %m");
                else
                        log_link_warning(link, "DHCPv6 unknown event: %d", event);
                return;
        }

        link_client_handler(link);
}

static int dhcp6_configure(Link *link, int event) {
        int r;
        bool information_request;

        assert_return(link, -EINVAL);
        assert_return(IN_SET(event, SD_ICMP6_ND_EVENT_ROUTER_ADVERTISMENT_TIMEOUT,
                             SD_ICMP6_ND_EVENT_ROUTER_ADVERTISMENT_OTHER,
                             SD_ICMP6_ND_EVENT_ROUTER_ADVERTISMENT_MANAGED), -EINVAL);

        link->dhcp6_configured = false;

        if (link->dhcp6_client) {
                r = sd_dhcp6_client_get_information_request(link->dhcp6_client,
                                                        &information_request);
                if (r < 0) {
                        log_link_warning_errno(link, r, "Could not get DHCPv6 Information request setting: %m");
                        goto error;
                }

                if (information_request && event != SD_ICMP6_ND_EVENT_ROUTER_ADVERTISMENT_OTHER) {
                        r = sd_dhcp6_client_stop(link->dhcp6_client);
                        if (r < 0) {
                                log_link_warning_errno(link, r, "Could not stop DHCPv6 while setting Managed mode: %m");
                                goto error;
                        }

                        r = sd_dhcp6_client_set_information_request(link->dhcp6_client,
                                                                    false);
                        if (r < 0) {
                                log_link_warning_errno(link, r, "Could not unset DHCPv6 Information request: %m");
                                goto error;
                        }

                }

                r = sd_dhcp6_client_start(link->dhcp6_client);
                if (r < 0 && r != -EALREADY) {
                        log_link_warning_errno(link, r, "Could not restart DHCPv6: %m");
                        goto error;
                }

                if (r == -EALREADY)
                        link->dhcp6_configured = true;

                return r;
        }

        r = sd_dhcp6_client_new(&link->dhcp6_client);
        if (r < 0)
                goto error;

        r = sd_dhcp6_client_attach_event(link->dhcp6_client, NULL, 0);
        if (r < 0)
                goto error;

        r = sd_dhcp6_client_set_mac(link->dhcp6_client,
                                    (const uint8_t *) &link->mac,
                                    sizeof (link->mac), ARPHRD_ETHER);
        if (r < 0)
                goto error;

        r = sd_dhcp6_client_set_index(link->dhcp6_client, link->ifindex);
        if (r < 0)
                goto error;

        r = sd_dhcp6_client_set_callback(link->dhcp6_client, dhcp6_handler,
                                         link);
        if (r < 0)
                goto error;

        if (event == SD_ICMP6_ND_EVENT_ROUTER_ADVERTISMENT_OTHER) {
                r = sd_dhcp6_client_set_information_request(link->dhcp6_client,
                                                        true);
                if (r < 0)
                        goto error;
        }

        r = sd_dhcp6_client_start(link->dhcp6_client);
        if (r < 0)
                goto error;

        return r;

 error:
        link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client);
        return r;
}

static int dhcp6_prefix_expired(Link *link) {
        int r;
        sd_dhcp6_lease *lease;
        struct in6_addr *expired_prefix, ip6_addr;
        uint8_t expired_prefixlen;
        uint32_t lifetime_preferred, lifetime_valid;

        r = sd_icmp6_ra_get_expired_prefix(link->icmp6_router_discovery,
                                        &expired_prefix, &expired_prefixlen);
        if (r < 0)
                return r;

        r = sd_dhcp6_client_get_lease(link->dhcp6_client, &lease);
        if (r < 0)
                return r;

        log_link_info(link, "IPv6 prefix "SD_ICMP6_ND_ADDRESS_FORMAT_STR"/%d expired",
                      SD_ICMP6_ND_ADDRESS_FORMAT_VAL(*expired_prefix),
                      expired_prefixlen);

        sd_dhcp6_lease_reset_address_iter(lease);

        while (sd_dhcp6_lease_get_address(lease, &ip6_addr,
                                                &lifetime_preferred,
                                                &lifetime_valid) >= 0) {

                r = sd_icmp6_prefix_match(expired_prefix, expired_prefixlen,
                                        &ip6_addr);
                if (r < 0)
                        continue;

                log_link_info(link, "IPv6 prefix length updated "SD_ICMP6_ND_ADDRESS_FORMAT_STR"/%d", SD_ICMP6_ND_ADDRESS_FORMAT_VAL(ip6_addr), 128);

                dhcp6_address_update(link, &ip6_addr, 128, lifetime_preferred, lifetime_valid);
        }

        return 0;
}

static void icmp6_router_handler(sd_icmp6_nd *nd, int event, void *userdata) {
        Link *link = userdata;

        assert(link);
        assert(link->network);
        assert(link->manager);

        if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
                return;

        switch(event) {
        case SD_ICMP6_ND_EVENT_ROUTER_ADVERTISMENT_NONE:
                return;

        case SD_ICMP6_ND_EVENT_ROUTER_ADVERTISMENT_TIMEOUT:
        case SD_ICMP6_ND_EVENT_ROUTER_ADVERTISMENT_OTHER:
        case SD_ICMP6_ND_EVENT_ROUTER_ADVERTISMENT_MANAGED:
                dhcp6_configure(link, event);

                break;

        case SD_ICMP6_ND_EVENT_ROUTER_ADVERTISMENT_PREFIX_EXPIRED:
                if (!link->rtnl_extended_attrs)
                        dhcp6_prefix_expired(link);

                break;

        default:
                if (event < 0)
                        log_link_warning_errno(link, event, "ICMPv6 error: %m");
                else
                        log_link_warning(link, "ICMPv6 unknown event: %d", event);

                break;
        }

}

int icmp6_configure(Link *link) {
        int r;

        assert_return(link, -EINVAL);

        r = sd_icmp6_nd_new(&link->icmp6_router_discovery);
        if (r < 0)
                return r;

        r = sd_icmp6_nd_attach_event(link->icmp6_router_discovery, NULL, 0);
        if (r < 0)
                return r;

        r = sd_icmp6_nd_set_mac(link->icmp6_router_discovery, &link->mac);
        if (r < 0)
                return r;

        r = sd_icmp6_nd_set_index(link->icmp6_router_discovery, link->ifindex);
        if (r < 0)
                return r;

        r = sd_icmp6_nd_set_callback(link->icmp6_router_discovery,
                                icmp6_router_handler, link);

        return r;
}