summaryrefslogtreecommitdiff
path: root/src/network/networkd-dhcp6.c
blob: c31bd4ec3076757412bf7522761c6b609cc2d511 (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
/*-*- 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 void dhcp6_handler(sd_dhcp6_client *client, 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 DHCP6_EVENT_STOP:
        case DHCP6_EVENT_RESEND_EXPIRE:
        case DHCP6_EVENT_RETRANS_MAX:
        case DHCP6_EVENT_IP_ACQUIRE:
        case DHCP6_EVENT_INFORMATION_REQUEST:
                log_link_debug(link, "DHCPv6 event %d", event);

                break;

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

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

        assert_return(link, -EINVAL);

        if (link->dhcp6_client) {
                if (event != ICMP6_EVENT_ROUTER_ADVERTISMENT_MANAGED)
                        return 0;

                r = sd_dhcp6_client_get_information_request(link->dhcp6_client,
                                                        &information_request);
                if (r < 0) {
                        log_link_warning(link, "Could not get DHCPv6 Information request setting");
                        link->dhcp6_client =
                                sd_dhcp6_client_unref(link->dhcp6_client);
                        return r;
                }

                if (!information_request)
                        return r;

                r = sd_dhcp6_client_set_information_request(link->dhcp6_client,
                                                        false);
                if (r < 0) {
                        log_link_warning(link, "Could not unset DHCPv6 Information request");
                        link->dhcp6_client =
                                sd_dhcp6_client_unref(link->dhcp6_client);
                        return r;
                }

                r = sd_dhcp6_client_start(link->dhcp6_client);
                if (r < 0) {
                        log_link_warning(link, "Could not restart DHCPv6 after enabling Information request");
                        link->dhcp6_client =
                                sd_dhcp6_client_unref(link->dhcp6_client);
                        return r;
                }

                return r;
        }

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

        r = sd_dhcp6_client_attach_event(link->dhcp6_client, NULL, 0);
        if (r < 0) {
                link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client);
                return r;
        }

        r = sd_dhcp6_client_set_mac(link->dhcp6_client,
                                    (const uint8_t *) &link->mac,
                                    sizeof (link->mac), ARPHRD_ETHER);
        if (r < 0) {
                link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client);
                return r;
        }

        r = sd_dhcp6_client_set_index(link->dhcp6_client, link->ifindex);
        if (r < 0) {
                link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client);
                return r;
        }

        r = sd_dhcp6_client_set_callback(link->dhcp6_client, dhcp6_handler,
                                         link);
        if (r < 0) {
                link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client);
                return r;
        }

        if (event == ICMP6_EVENT_ROUTER_ADVERTISMENT_OTHER) {
                r = sd_dhcp6_client_set_information_request(link->dhcp6_client,
                                                        true);
                if (r < 0) {
                        link->dhcp6_client =
                                sd_dhcp6_client_unref(link->dhcp6_client);
                        return r;
                }
        }

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

        return r;
}

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 ICMP6_EVENT_ROUTER_ADVERTISMENT_NONE:
                return;

        case ICMP6_EVENT_ROUTER_ADVERTISMENT_TIMEOUT:
        case ICMP6_EVENT_ROUTER_ADVERTISMENT_OTHER:
        case ICMP6_EVENT_ROUTER_ADVERTISMENT_MANAGED:
                break;

        default:
                if (event < 0)
                        log_link_warning(link, "ICMPv6 error: %s",
                                         strerror(-event));
                else
                        log_link_warning(link, "ICMPv6 unknown event: %d",
                                         event);

                return;
        }

        dhcp6_configure(link, event);
}

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;
}