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));
69 addr->flags = IFA_F_NOPREFIXROUTE;
72 addr->cinfo.ifa_prefered = lifetime_preferred;
73 addr->cinfo.ifa_valid = lifetime_valid;
75 log_link_struct(link, LOG_INFO, "MESSAGE=%-*s: DHCPv6 address "SD_ICMP6_ADDRESS_FORMAT_STR"/%d timeout preferred %d valid %d",
77 link->ifname, SD_ICMP6_ADDRESS_FORMAT_VAL(addr->in_addr.in6),
78 addr->prefixlen, lifetime_preferred, lifetime_valid,
81 r = address_update(addr, link, dhcp6_address_handler);
83 log_link_warning(link, "Could not assign DHCPv6 address: %s",
89 static int dhcp6_prefix_expired(Link *link) {
91 sd_dhcp6_lease *lease;
92 struct in6_addr *expired_prefix, ip6_addr;
93 uint8_t expired_prefixlen;
94 uint32_t lifetime_preferred, lifetime_valid;
96 r = sd_icmp6_ra_get_expired_prefix(link->icmp6_router_discovery,
97 &expired_prefix, &expired_prefixlen);
101 r = sd_dhcp6_client_get_lease(link->dhcp6_client, &lease);
105 sd_dhcp6_lease_reset_address_iter(lease);
107 while (sd_dhcp6_lease_get_address(lease, &ip6_addr,
109 &lifetime_valid) >= 0) {
111 r = sd_icmp6_prefix_match(expired_prefix, expired_prefixlen,
114 r = dhcp6_address_update(link, &ip6_addr, 128,
125 static int dhcp6_lease_address_acquired(sd_dhcp6_client *client, Link *link) {
127 sd_dhcp6_lease *lease;
128 struct in6_addr ip6_addr;
129 uint32_t lifetime_preferred, lifetime_valid;
132 r = sd_dhcp6_client_get_lease(client, &lease);
136 sd_dhcp6_lease_reset_address_iter(lease);
138 while (sd_dhcp6_lease_get_address(lease, &ip6_addr,
140 &lifetime_valid) >= 0) {
142 r = sd_icmp6_ra_get_prefixlen(link->icmp6_router_discovery,
143 &ip6_addr, &prefixlen);
144 if (r < 0 && r != -EADDRNOTAVAIL) {
145 log_link_warning(link, "Could not get prefix information: %s",
150 if (r == -EADDRNOTAVAIL)
153 r = dhcp6_address_update(link, &ip6_addr, prefixlen,
154 lifetime_preferred, lifetime_valid);
162 static void dhcp6_handler(sd_dhcp6_client *client, int event, void *userdata) {
164 Link *link = userdata;
167 assert(link->network);
168 assert(link->manager);
170 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
174 case DHCP6_EVENT_STOP:
175 case DHCP6_EVENT_RESEND_EXPIRE:
176 case DHCP6_EVENT_RETRANS_MAX:
177 log_link_debug(link, "DHCPv6 event %d", event);
180 case DHCP6_EVENT_IP_ACQUIRE:
181 r = dhcp6_lease_address_acquired(client, link);
183 link_enter_failed(link);
188 case DHCP6_EVENT_INFORMATION_REQUEST:
189 r = dhcp6_lease_information_acquired(client, link);
191 link_enter_failed(link);
199 log_link_warning(link, "DHCPv6 error: %s",
202 log_link_warning(link, "DHCPv6 unknown event: %d",
208 static int dhcp6_configure(Link *link, int event) {
210 bool information_request;
212 assert_return(link, -EINVAL);
214 if (link->dhcp6_client) {
215 if (event != ICMP6_EVENT_ROUTER_ADVERTISMENT_MANAGED)
218 r = sd_dhcp6_client_get_information_request(link->dhcp6_client,
219 &information_request);
221 log_link_warning(link, "Could not get DHCPv6 Information request setting: %s",
224 sd_dhcp6_client_unref(link->dhcp6_client);
228 if (!information_request)
231 r = sd_dhcp6_client_set_information_request(link->dhcp6_client,
234 log_link_warning(link, "Could not unset DHCPv6 Information request: %s",
237 sd_dhcp6_client_unref(link->dhcp6_client);
241 r = sd_dhcp6_client_start(link->dhcp6_client);
243 log_link_warning(link, "Could not restart DHCPv6 after enabling Information request: %s",
246 sd_dhcp6_client_unref(link->dhcp6_client);
253 r = sd_dhcp6_client_new(&link->dhcp6_client);
257 r = sd_dhcp6_client_attach_event(link->dhcp6_client, NULL, 0);
259 link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client);
263 r = sd_dhcp6_client_set_mac(link->dhcp6_client,
264 (const uint8_t *) &link->mac,
265 sizeof (link->mac), ARPHRD_ETHER);
267 link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client);
271 r = sd_dhcp6_client_set_index(link->dhcp6_client, link->ifindex);
273 link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client);
277 r = sd_dhcp6_client_set_callback(link->dhcp6_client, dhcp6_handler,
280 link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client);
284 if (event == ICMP6_EVENT_ROUTER_ADVERTISMENT_OTHER) {
285 r = sd_dhcp6_client_set_information_request(link->dhcp6_client,
289 sd_dhcp6_client_unref(link->dhcp6_client);
294 r = sd_dhcp6_client_start(link->dhcp6_client);
296 link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client);
301 static void icmp6_router_handler(sd_icmp6_nd *nd, int event, void *userdata) {
302 Link *link = userdata;
305 assert(link->network);
306 assert(link->manager);
308 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
312 case ICMP6_EVENT_ROUTER_ADVERTISMENT_NONE:
315 case ICMP6_EVENT_ROUTER_ADVERTISMENT_TIMEOUT:
316 case ICMP6_EVENT_ROUTER_ADVERTISMENT_OTHER:
317 case ICMP6_EVENT_ROUTER_ADVERTISMENT_MANAGED:
318 dhcp6_configure(link, event);
322 case ICMP6_EVENT_ROUTER_ADVERTISMENT_PREFIX_EXPIRED:
323 dhcp6_prefix_expired(link);
329 log_link_warning(link, "ICMPv6 error: %s",
332 log_link_warning(link, "ICMPv6 unknown event: %d",
340 int icmp6_configure(Link *link) {
343 assert_return(link, -EINVAL);
345 r = sd_icmp6_nd_new(&link->icmp6_router_discovery);
349 r = sd_icmp6_nd_attach_event(link->icmp6_router_discovery, NULL, 0);
353 r = sd_icmp6_nd_set_mac(link->icmp6_router_discovery, &link->mac);
357 r = sd_icmp6_nd_set_index(link->icmp6_router_discovery, link->ifindex);
361 r = sd_icmp6_nd_set_callback(link->icmp6_router_discovery,
362 icmp6_router_handler, link);