]>
Commit | Line | Data |
---|---|---|
5c79bd79 PF |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright (C) 2014 Intel Corporation. All rights reserved. | |
7 | ||
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. | |
12 | ||
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. | |
17 | ||
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/>. | |
20 | ***/ | |
21 | ||
22 | #include <netinet/ether.h> | |
23 | #include <linux/if.h> | |
24 | ||
5c79bd79 PF |
25 | #include "sd-dhcp6-client.h" |
26 | ||
07630cea LP |
27 | #include "network-internal.h" |
28 | #include "networkd-link.h" | |
29 | ||
be3a09b7 PF |
30 | static int dhcp6_lease_address_acquired(sd_dhcp6_client *client, Link *link); |
31 | ||
c62c4628 PF |
32 | static int dhcp6_lease_information_acquired(sd_dhcp6_client *client, |
33 | Link *link) { | |
34 | return 0; | |
35 | } | |
36 | ||
1c4baffc | 37 | static int dhcp6_address_handler(sd_netlink *rtnl, sd_netlink_message *m, |
c62c4628 PF |
38 | void *userdata) { |
39 | _cleanup_link_unref_ Link *link = userdata; | |
40 | int r; | |
41 | ||
42 | assert(link); | |
43 | ||
1c4baffc | 44 | r = sd_netlink_message_get_errno(m); |
c62c4628 | 45 | if (r < 0 && r != -EEXIST) { |
be3a09b7 PF |
46 | if (link->rtnl_extended_attrs) { |
47 | log_link_warning(link, "Could not set extended netlink attributes, reverting to fallback mechanism"); | |
48 | ||
49 | link->rtnl_extended_attrs = false; | |
50 | dhcp6_lease_address_acquired(link->dhcp6_client, link); | |
51 | ||
52 | return 1; | |
53 | } | |
54 | ||
e53fc357 | 55 | log_link_error_errno(link, r, "Could not set DHCPv6 address: %m"); |
c62c4628 PF |
56 | |
57 | link_enter_failed(link); | |
58 | ||
59 | } else if (r >= 0) | |
200a0868 | 60 | manager_rtnl_process_address(rtnl, m, link->manager); |
c62c4628 PF |
61 | |
62 | return 1; | |
63 | } | |
64 | ||
36c32f61 | 65 | static int dhcp6_address_change(Link *link, struct in6_addr *ip6_addr, |
6d8f6b0b | 66 | uint32_t lifetime_preferred, uint32_t lifetime_valid) { |
c62c4628 PF |
67 | int r; |
68 | _cleanup_address_free_ Address *addr = NULL; | |
69 | ||
f0213e37 | 70 | r = address_new(&addr); |
c62c4628 PF |
71 | if (r < 0) |
72 | return r; | |
73 | ||
74 | addr->family = AF_INET6; | |
75 | memcpy(&addr->in_addr.in6, ip6_addr, sizeof(*ip6_addr)); | |
851c9f82 PF |
76 | |
77 | addr->flags = IFA_F_NOPREFIXROUTE; | |
6d8f6b0b | 78 | addr->prefixlen = 128; |
c62c4628 PF |
79 | |
80 | addr->cinfo.ifa_prefered = lifetime_preferred; | |
81 | addr->cinfo.ifa_valid = lifetime_valid; | |
82 | ||
f2341e0a | 83 | log_link_info(link, |
4d7b83da TG |
84 | "DHCPv6 address "SD_NDISC_ADDRESS_FORMAT_STR"/%d timeout preferred %d valid %d", |
85 | SD_NDISC_ADDRESS_FORMAT_VAL(addr->in_addr.in6), | |
f2341e0a | 86 | addr->prefixlen, lifetime_preferred, lifetime_valid); |
c62c4628 | 87 | |
66669078 | 88 | r = address_configure(addr, link, dhcp6_address_handler, true); |
c62c4628 | 89 | if (r < 0) |
f2341e0a | 90 | log_link_warning_errno(link, r, "Could not assign DHCPv6 address: %m"); |
c62c4628 PF |
91 | |
92 | return r; | |
93 | } | |
94 | ||
c62c4628 PF |
95 | static int dhcp6_lease_address_acquired(sd_dhcp6_client *client, Link *link) { |
96 | int r; | |
97 | sd_dhcp6_lease *lease; | |
98 | struct in6_addr ip6_addr; | |
99 | uint32_t lifetime_preferred, lifetime_valid; | |
c62c4628 PF |
100 | |
101 | r = sd_dhcp6_client_get_lease(client, &lease); | |
102 | if (r < 0) | |
103 | return r; | |
104 | ||
105 | sd_dhcp6_lease_reset_address_iter(lease); | |
106 | ||
107 | while (sd_dhcp6_lease_get_address(lease, &ip6_addr, | |
108 | &lifetime_preferred, | |
109 | &lifetime_valid) >= 0) { | |
110 | ||
6d8f6b0b | 111 | r = dhcp6_address_change(link, &ip6_addr, lifetime_preferred, lifetime_valid); |
c62c4628 PF |
112 | if (r < 0) |
113 | return r; | |
114 | } | |
115 | ||
116 | return 0; | |
117 | } | |
118 | ||
5c79bd79 | 119 | static void dhcp6_handler(sd_dhcp6_client *client, int event, void *userdata) { |
c62c4628 | 120 | int r; |
5c79bd79 PF |
121 | Link *link = userdata; |
122 | ||
123 | assert(link); | |
124 | assert(link->network); | |
125 | assert(link->manager); | |
126 | ||
127 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
128 | return; | |
129 | ||
130 | switch(event) { | |
10c9ce61 DH |
131 | case SD_DHCP6_CLIENT_EVENT_STOP: |
132 | case SD_DHCP6_CLIENT_EVENT_RESEND_EXPIRE: | |
133 | case SD_DHCP6_CLIENT_EVENT_RETRANS_MAX: | |
18d29550 PF |
134 | log_link_warning(link, "DHCPv6 lease lost"); |
135 | ||
136 | link->dhcp6_configured = false; | |
c62c4628 PF |
137 | break; |
138 | ||
10c9ce61 | 139 | case SD_DHCP6_CLIENT_EVENT_IP_ACQUIRE: |
c62c4628 PF |
140 | r = dhcp6_lease_address_acquired(client, link); |
141 | if (r < 0) { | |
142 | link_enter_failed(link); | |
143 | return; | |
144 | } | |
145 | ||
146 | /* fall through */ | |
10c9ce61 | 147 | case SD_DHCP6_CLIENT_EVENT_INFORMATION_REQUEST: |
c62c4628 PF |
148 | r = dhcp6_lease_information_acquired(client, link); |
149 | if (r < 0) { | |
150 | link_enter_failed(link); | |
151 | return; | |
152 | } | |
5c79bd79 | 153 | |
18d29550 | 154 | link->dhcp6_configured = true; |
5c79bd79 PF |
155 | break; |
156 | ||
157 | default: | |
158 | if (event < 0) | |
e53fc357 | 159 | log_link_warning_errno(link, event, "DHCPv6 error: %m"); |
5c79bd79 | 160 | else |
e53fc357 | 161 | log_link_warning(link, "DHCPv6 unknown event: %d", event); |
5c79bd79 PF |
162 | return; |
163 | } | |
18d29550 | 164 | |
8012cd39 | 165 | link_check_ready(link); |
5c79bd79 PF |
166 | } |
167 | ||
a13c50e7 | 168 | int dhcp6_configure(Link *link, bool inf_req) { |
5c79bd79 | 169 | int r; |
85bd849f | 170 | bool information_request; |
5c79bd79 PF |
171 | |
172 | assert_return(link, -EINVAL); | |
173 | ||
18d29550 PF |
174 | link->dhcp6_configured = false; |
175 | ||
85bd849f | 176 | if (link->dhcp6_client) { |
a13c50e7 | 177 | r = sd_dhcp6_client_get_information_request(link->dhcp6_client, &information_request); |
85bd849f | 178 | if (r < 0) { |
e53fc357 | 179 | log_link_warning_errno(link, r, "Could not get DHCPv6 Information request setting: %m"); |
e6604041 | 180 | goto error; |
85bd849f PF |
181 | } |
182 | ||
a13c50e7 | 183 | if (information_request && !inf_req) { |
e6604041 PF |
184 | r = sd_dhcp6_client_stop(link->dhcp6_client); |
185 | if (r < 0) { | |
e53fc357 | 186 | log_link_warning_errno(link, r, "Could not stop DHCPv6 while setting Managed mode: %m"); |
e6604041 PF |
187 | goto error; |
188 | } | |
189 | ||
a13c50e7 | 190 | r = sd_dhcp6_client_set_information_request(link->dhcp6_client, false); |
e6604041 | 191 | if (r < 0) { |
e53fc357 | 192 | log_link_warning_errno(link, r, "Could not unset DHCPv6 Information request: %m"); |
e6604041 PF |
193 | goto error; |
194 | } | |
85bd849f | 195 | |
85bd849f PF |
196 | } |
197 | ||
198 | r = sd_dhcp6_client_start(link->dhcp6_client); | |
e6604041 | 199 | if (r < 0 && r != -EALREADY) { |
e53fc357 | 200 | log_link_warning_errno(link, r, "Could not restart DHCPv6: %m"); |
e6604041 | 201 | goto error; |
85bd849f PF |
202 | } |
203 | ||
18d29550 PF |
204 | if (r == -EALREADY) |
205 | link->dhcp6_configured = true; | |
206 | ||
85bd849f PF |
207 | return r; |
208 | } | |
5c79bd79 PF |
209 | |
210 | r = sd_dhcp6_client_new(&link->dhcp6_client); | |
211 | if (r < 0) | |
e6604041 | 212 | goto error; |
5c79bd79 PF |
213 | |
214 | r = sd_dhcp6_client_attach_event(link->dhcp6_client, NULL, 0); | |
e6604041 PF |
215 | if (r < 0) |
216 | goto error; | |
5c79bd79 PF |
217 | |
218 | r = sd_dhcp6_client_set_mac(link->dhcp6_client, | |
219 | (const uint8_t *) &link->mac, | |
220 | sizeof (link->mac), ARPHRD_ETHER); | |
e6604041 PF |
221 | if (r < 0) |
222 | goto error; | |
5c79bd79 PF |
223 | |
224 | r = sd_dhcp6_client_set_index(link->dhcp6_client, link->ifindex); | |
e6604041 PF |
225 | if (r < 0) |
226 | goto error; | |
5c79bd79 PF |
227 | |
228 | r = sd_dhcp6_client_set_callback(link->dhcp6_client, dhcp6_handler, | |
229 | link); | |
e6604041 PF |
230 | if (r < 0) |
231 | goto error; | |
5c79bd79 | 232 | |
a13c50e7 TG |
233 | if (inf_req) { |
234 | r = sd_dhcp6_client_set_information_request(link->dhcp6_client, true); | |
e6604041 PF |
235 | if (r < 0) |
236 | goto error; | |
85bd849f PF |
237 | } |
238 | ||
5c79bd79 PF |
239 | r = sd_dhcp6_client_start(link->dhcp6_client); |
240 | if (r < 0) | |
e6604041 PF |
241 | goto error; |
242 | ||
243 | return r; | |
5c79bd79 | 244 | |
e6604041 PF |
245 | error: |
246 | link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client); | |
5c79bd79 PF |
247 | return r; |
248 | } |