1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright (C) 2014 Intel Corporation. All rights reserved.
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <netinet/ether.h>
25 #include "networkd-link.h"
26 #include "network-internal.h"
28 #include "sd-icmp6-nd.h"
29 #include "sd-dhcp6-client.h"
31 static int dhcp6_lease_information_acquired(sd_dhcp6_client *client,
36 static int dhcp6_address_handler(sd_rtnl *rtnl, sd_rtnl_message *m,
38 _cleanup_link_unref_ Link *link = userdata;
43 r = sd_rtnl_message_get_errno(m);
44 if (r < 0 && r != -EEXIST) {
45 log_link_error(link, "Could not set DHCPv6 address: %s",
48 link_enter_failed(link);
51 link_rtnl_process_address(rtnl, m, link->manager);
56 static int dhcp6_address_update(Link *link, struct in6_addr *ip6_addr,
57 uint8_t prefixlen, uint32_t lifetime_preferred,
58 uint32_t lifetime_valid) {
60 _cleanup_address_free_ Address *addr = NULL;
62 r = address_new_dynamic(&addr);
66 addr->family = AF_INET6;
67 memcpy(&addr->in_addr.in6, ip6_addr, sizeof(*ip6_addr));
68 addr->prefixlen = prefixlen;
70 addr->cinfo.ifa_prefered = lifetime_preferred;
71 addr->cinfo.ifa_valid = lifetime_valid;
73 log_link_struct(link, LOG_INFO, "MESSAGE=%-*s: DHCPv6 address "SD_ICMP6_ADDRESS_FORMAT_STR"/%d timeout preferred %d valid %d",
75 link->ifname, SD_ICMP6_ADDRESS_FORMAT_VAL(addr->in_addr.in6),
76 addr->prefixlen, lifetime_preferred, lifetime_valid,
79 r = address_update(addr, link, dhcp6_address_handler);
81 log_link_warning(link, "Could not assign DHCPv6 address: %s",
87 static int dhcp6_prefix_expired(Link *link) {
89 sd_dhcp6_lease *lease;
90 struct in6_addr *expired_prefix, ip6_addr;
91 uint8_t expired_prefixlen;
92 uint32_t lifetime_preferred, lifetime_valid;
94 r = sd_icmp6_ra_get_expired_prefix(link->icmp6_router_discovery,
95 &expired_prefix, &expired_prefixlen);
99 r = sd_dhcp6_client_get_lease(link->dhcp6_client, &lease);
103 sd_dhcp6_lease_reset_address_iter(lease);
105 while (sd_dhcp6_lease_get_address(lease, &ip6_addr,
107 &lifetime_valid) >= 0) {
109 r = sd_icmp6_prefix_match(expired_prefix, expired_prefixlen,
112 r = dhcp6_address_update(link, &ip6_addr, 128,
123 static int dhcp6_lease_address_acquired(sd_dhcp6_client *client, Link *link) {
125 sd_dhcp6_lease *lease;
126 struct in6_addr ip6_addr;
127 uint32_t lifetime_preferred, lifetime_valid;
130 r = sd_dhcp6_client_get_lease(client, &lease);
134 sd_dhcp6_lease_reset_address_iter(lease);
136 while (sd_dhcp6_lease_get_address(lease, &ip6_addr,
138 &lifetime_valid) >= 0) {
140 r = sd_icmp6_ra_get_prefixlen(link->icmp6_router_discovery,
141 &ip6_addr, &prefixlen);
142 if (r < 0 && r != -EADDRNOTAVAIL) {
143 log_link_warning(link, "Could not get prefix information: %s",
148 if (r == -EADDRNOTAVAIL)
151 r = dhcp6_address_update(link, &ip6_addr, prefixlen,
152 lifetime_preferred, lifetime_valid);
160 static void dhcp6_handler(sd_dhcp6_client *client, int event, void *userdata) {
162 Link *link = userdata;
165 assert(link->network);
166 assert(link->manager);
168 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
172 case DHCP6_EVENT_STOP:
173 case DHCP6_EVENT_RESEND_EXPIRE:
174 case DHCP6_EVENT_RETRANS_MAX:
175 log_link_debug(link, "DHCPv6 event %d", event);
178 case DHCP6_EVENT_IP_ACQUIRE:
179 r = dhcp6_lease_address_acquired(client, link);
181 link_enter_failed(link);
186 case DHCP6_EVENT_INFORMATION_REQUEST:
187 r = dhcp6_lease_information_acquired(client, link);
189 link_enter_failed(link);
197 log_link_warning(link, "DHCPv6 error: %s",
200 log_link_warning(link, "DHCPv6 unknown event: %d",
206 static int dhcp6_configure(Link *link, int event) {
208 bool information_request;
210 assert_return(link, -EINVAL);
212 if (link->dhcp6_client) {
213 if (event != ICMP6_EVENT_ROUTER_ADVERTISMENT_MANAGED)
216 r = sd_dhcp6_client_get_information_request(link->dhcp6_client,
217 &information_request);
219 log_link_warning(link, "Could not get DHCPv6 Information request setting: %s",
222 sd_dhcp6_client_unref(link->dhcp6_client);
226 if (!information_request)
229 r = sd_dhcp6_client_set_information_request(link->dhcp6_client,
232 log_link_warning(link, "Could not unset DHCPv6 Information request: %s",
235 sd_dhcp6_client_unref(link->dhcp6_client);
239 r = sd_dhcp6_client_start(link->dhcp6_client);
241 log_link_warning(link, "Could not restart DHCPv6 after enabling Information request: %s",
244 sd_dhcp6_client_unref(link->dhcp6_client);
251 r = sd_dhcp6_client_new(&link->dhcp6_client);
255 r = sd_dhcp6_client_attach_event(link->dhcp6_client, NULL, 0);
257 link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client);
261 r = sd_dhcp6_client_set_mac(link->dhcp6_client,
262 (const uint8_t *) &link->mac,
263 sizeof (link->mac), ARPHRD_ETHER);
265 link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client);
269 r = sd_dhcp6_client_set_index(link->dhcp6_client, link->ifindex);
271 link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client);
275 r = sd_dhcp6_client_set_callback(link->dhcp6_client, dhcp6_handler,
278 link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client);
282 if (event == ICMP6_EVENT_ROUTER_ADVERTISMENT_OTHER) {
283 r = sd_dhcp6_client_set_information_request(link->dhcp6_client,
287 sd_dhcp6_client_unref(link->dhcp6_client);
292 r = sd_dhcp6_client_start(link->dhcp6_client);
294 link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client);
299 static void icmp6_router_handler(sd_icmp6_nd *nd, int event, void *userdata) {
300 Link *link = userdata;
303 assert(link->network);
304 assert(link->manager);
306 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
310 case ICMP6_EVENT_ROUTER_ADVERTISMENT_NONE:
313 case ICMP6_EVENT_ROUTER_ADVERTISMENT_TIMEOUT:
314 case ICMP6_EVENT_ROUTER_ADVERTISMENT_OTHER:
315 case ICMP6_EVENT_ROUTER_ADVERTISMENT_MANAGED:
316 dhcp6_configure(link, event);
320 case ICMP6_EVENT_ROUTER_ADVERTISMENT_PREFIX_EXPIRED:
321 dhcp6_prefix_expired(link);
327 log_link_warning(link, "ICMPv6 error: %s",
330 log_link_warning(link, "ICMPv6 unknown event: %d",
338 int icmp6_configure(Link *link) {
341 assert_return(link, -EINVAL);
343 r = sd_icmp6_nd_new(&link->icmp6_router_discovery);
347 r = sd_icmp6_nd_attach_event(link->icmp6_router_discovery, NULL, 0);
351 r = sd_icmp6_nd_set_mac(link->icmp6_router_discovery, &link->mac);
355 r = sd_icmp6_nd_set_index(link->icmp6_router_discovery, link->ifindex);
359 r = sd_icmp6_nd_set_callback(link->icmp6_router_discovery,
360 icmp6_router_handler, link);