]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
3c9b8860 TG |
2 | |
3 | #include <netinet/ether.h> | |
4 | #include <linux/if.h> | |
5 | ||
b5efdb8a | 6 | #include "alloc-util.h" |
958b66ea | 7 | #include "hostname-util.h" |
64b21ece | 8 | #include "parse-util.h" |
fc1ba79d | 9 | #include "netdev/vrf.h" |
3c9b8860 | 10 | #include "network-internal.h" |
23f53b99 TG |
11 | #include "networkd-link.h" |
12 | #include "networkd-manager.h" | |
13 | #include "networkd-network.h" | |
64b21ece MV |
14 | #include "string-util.h" |
15 | #include "sysctl-util.h" | |
3c9b8860 | 16 | |
302a796f | 17 | static int dhcp4_route_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
3c9b8860 TG |
18 | int r; |
19 | ||
20 | assert(link); | |
6cf4a01c | 21 | assert(link->dhcp4_messages > 0); |
3c9b8860 | 22 | |
313cefa1 | 23 | link->dhcp4_messages--; |
3c9b8860 | 24 | |
1c4baffc | 25 | r = sd_netlink_message_get_errno(m); |
3c9b8860 | 26 | if (r < 0 && r != -EEXIST) { |
f6b8196f | 27 | log_link_error_errno(link, r, "Could not set DHCPv4 route: %m"); |
3c9b8860 TG |
28 | link_enter_failed(link); |
29 | } | |
30 | ||
6cf4a01c | 31 | if (link->dhcp4_messages == 0) { |
3c9b8860 | 32 | link->dhcp4_configured = true; |
8012cd39 | 33 | link_check_ready(link); |
3c9b8860 TG |
34 | } |
35 | ||
36 | return 1; | |
37 | } | |
38 | ||
d6eac9bd DW |
39 | static int route_scope_from_address(const Route *route, const struct in_addr *self_addr) { |
40 | assert(route); | |
41 | assert(self_addr); | |
42 | ||
43 | if (in_addr_is_localhost(AF_INET, &route->dst) || | |
44 | (self_addr->s_addr && route->dst.in.s_addr == self_addr->s_addr)) | |
45 | return RT_SCOPE_HOST; | |
46 | else if (in4_addr_is_null(&route->gw.in)) | |
47 | return RT_SCOPE_LINK; | |
48 | else | |
49 | return RT_SCOPE_UNIVERSE; | |
50 | } | |
51 | ||
3c9b8860 | 52 | static int link_set_dhcp_routes(Link *link) { |
f8693fc7 | 53 | _cleanup_free_ sd_dhcp_route **static_routes = NULL; |
8cdc46e7 SS |
54 | bool classless_route = false, static_route = false; |
55 | struct in_addr gateway, address; | |
3c9b8860 | 56 | int r, n, i; |
fc1ba79d | 57 | uint32_t table; |
3c9b8860 TG |
58 | |
59 | assert(link); | |
0c9b15a3 AJ |
60 | |
61 | if (!link->dhcp_lease) /* link went down while we configured the IP addresses? */ | |
62 | return 0; | |
63 | ||
64 | if (!link->network) /* link went down while we configured the IP addresses? */ | |
65 | return 0; | |
964b26fe SS |
66 | |
67 | if (!link->network->dhcp_use_routes) | |
68 | return 0; | |
3c9b8860 | 69 | |
fc1ba79d AR |
70 | /* When the interface is part of an VRF use the VRFs routing table, unless |
71 | * there is a another table specified. */ | |
72 | table = link->network->dhcp_route_table; | |
860e636c | 73 | if (!link->network->dhcp_route_table_set && link->network->vrf) |
fc1ba79d AR |
74 | table = VRF(link->network->vrf)->table; |
75 | ||
b23aec0d DW |
76 | r = sd_dhcp_lease_get_address(link->dhcp_lease, &address); |
77 | if (r < 0) | |
78 | return log_link_warning_errno(link, r, "DHCP error: could not get address: %m"); | |
79 | ||
5f04a209 | 80 | n = sd_dhcp_lease_get_routes(link->dhcp_lease, &static_routes); |
599c44e6 YW |
81 | if (n == -ENODATA) |
82 | log_link_debug_errno(link, n, "DHCP: No routes received from DHCP server: %m"); | |
83 | else if (n < 0) | |
8dc787d1 | 84 | log_link_debug_errno(link, n, "DHCP error: could not get routes: %m"); |
5f04a209 | 85 | |
8cdc46e7 | 86 | for (i = 0; i < n; i++) { |
3476951c TH |
87 | switch (sd_dhcp_route_get_option(static_routes[i])) { |
88 | case SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE: | |
8cdc46e7 | 89 | classless_route = true; |
3476951c TH |
90 | break; |
91 | case SD_DHCP_OPTION_STATIC_ROUTE: | |
8cdc46e7 | 92 | static_route = true; |
3476951c TH |
93 | break; |
94 | } | |
8cdc46e7 SS |
95 | } |
96 | ||
5f04a209 | 97 | for (i = 0; i < n; i++) { |
8e766630 | 98 | _cleanup_(route_freep) Route *route = NULL; |
5f04a209 | 99 | |
8cdc46e7 SS |
100 | /* if the DHCP server returns both a Classless Static Routes option and a Static Routes option, |
101 | the DHCP client MUST ignore the Static Routes option. */ | |
3476951c TH |
102 | if (classless_route && |
103 | sd_dhcp_route_get_option(static_routes[i]) != SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE) | |
8cdc46e7 SS |
104 | continue; |
105 | ||
5f04a209 SS |
106 | r = route_new(&route); |
107 | if (r < 0) | |
108 | return log_link_error_errno(link, r, "Could not allocate route: %m"); | |
109 | ||
110 | route->family = AF_INET; | |
111 | route->protocol = RTPROT_DHCP; | |
112 | assert_se(sd_dhcp_route_get_gateway(static_routes[i], &route->gw.in) >= 0); | |
113 | assert_se(sd_dhcp_route_get_destination(static_routes[i], &route->dst.in) >= 0); | |
114 | assert_se(sd_dhcp_route_get_destination_prefix_length(static_routes[i], &route->dst_prefixlen) >= 0); | |
115 | route->priority = link->network->dhcp_route_metric; | |
116 | route->table = table; | |
117 | route->scope = route_scope_from_address(route, &address); | |
118 | ||
119 | r = route_configure(route, link, dhcp4_route_handler); | |
120 | if (r < 0) | |
121 | return log_link_warning_errno(link, r, "Could not set host route: %m"); | |
122 | ||
123 | link->dhcp4_messages++; | |
124 | } | |
125 | ||
3c9b8860 | 126 | r = sd_dhcp_lease_get_router(link->dhcp_lease, &gateway); |
444b0170 | 127 | if (r == -ENODATA) |
fb14aa7e | 128 | log_link_info_errno(link, r, "DHCP: No gateway received from DHCP server: %m"); |
444b0170 SS |
129 | else if (r < 0) |
130 | log_link_warning_errno(link, r, "DHCP error: could not get gateway: %m"); | |
f6b8196f | 131 | |
5f04a209 SS |
132 | /* According to RFC 3442: If the DHCP server returns both a Classless Static Routes option and |
133 | a Router option, the DHCP client MUST ignore the Router option. */ | |
8cdc46e7 SS |
134 | if (classless_route && static_route) |
135 | log_link_warning(link, "Classless static routes received from DHCP server: ignoring static-route option and router option"); | |
136 | ||
137 | if (r >= 0 && !classless_route) { | |
860e636c | 138 | _cleanup_(route_freep) Route *route = NULL, *route_gw = NULL; |
ed9e361a TG |
139 | |
140 | r = route_new(&route_gw); | |
f6b8196f LP |
141 | if (r < 0) |
142 | return log_link_error_errno(link, r, "Could not allocate route: %m"); | |
3c9b8860 TG |
143 | |
144 | /* The dhcp netmask may mask out the gateway. Add an explicit | |
145 | * route for the gw host so that we can route no matter the | |
146 | * netmask or existing kernel route tables. */ | |
147 | route_gw->family = AF_INET; | |
2ce40956 | 148 | route_gw->dst.in = gateway; |
3c9b8860 | 149 | route_gw->dst_prefixlen = 32; |
2ce40956 | 150 | route_gw->prefsrc.in = address; |
3c9b8860 | 151 | route_gw->scope = RT_SCOPE_LINK; |
ed9e361a | 152 | route_gw->protocol = RTPROT_DHCP; |
86655331 | 153 | route_gw->priority = link->network->dhcp_route_metric; |
fc1ba79d | 154 | route_gw->table = table; |
3c9b8860 | 155 | |
483d099e | 156 | r = route_configure(route_gw, link, dhcp4_route_handler); |
f6b8196f LP |
157 | if (r < 0) |
158 | return log_link_warning_errno(link, r, "Could not set host route: %m"); | |
3c9b8860 | 159 | |
313cefa1 | 160 | link->dhcp4_messages++; |
3c9b8860 | 161 | |
860e636c YW |
162 | r = route_new(&route); |
163 | if (r < 0) | |
164 | return log_link_error_errno(link, r, "Could not allocate route: %m"); | |
165 | ||
3c9b8860 | 166 | route->family = AF_INET; |
2ce40956 TG |
167 | route->gw.in = gateway; |
168 | route->prefsrc.in = address; | |
860e636c | 169 | route->protocol = RTPROT_DHCP; |
86655331 | 170 | route->priority = link->network->dhcp_route_metric; |
fc1ba79d | 171 | route->table = table; |
3c9b8860 | 172 | |
483d099e | 173 | r = route_configure(route, link, dhcp4_route_handler); |
3c9b8860 | 174 | if (r < 0) { |
f6b8196f | 175 | log_link_warning_errno(link, r, "Could not set routes: %m"); |
3c9b8860 TG |
176 | link_enter_failed(link); |
177 | return r; | |
178 | } | |
179 | ||
313cefa1 | 180 | link->dhcp4_messages++; |
3c9b8860 TG |
181 | } |
182 | ||
3c9b8860 TG |
183 | return 0; |
184 | } | |
185 | ||
186 | static int dhcp_lease_lost(Link *link) { | |
8e766630 | 187 | _cleanup_(address_freep) Address *address = NULL; |
3c9b8860 TG |
188 | struct in_addr addr; |
189 | struct in_addr netmask; | |
190 | struct in_addr gateway; | |
f414a269 | 191 | unsigned prefixlen = 0; |
3c9b8860 TG |
192 | int r; |
193 | ||
194 | assert(link); | |
195 | assert(link->dhcp_lease); | |
196 | ||
79008bdd | 197 | log_link_warning(link, "DHCP lease lost"); |
3c9b8860 | 198 | |
27cb34f5 | 199 | if (link->network->dhcp_use_routes) { |
f8693fc7 | 200 | _cleanup_free_ sd_dhcp_route **routes = NULL; |
3c9b8860 TG |
201 | int n, i; |
202 | ||
203 | n = sd_dhcp_lease_get_routes(link->dhcp_lease, &routes); | |
204 | if (n >= 0) { | |
205 | for (i = 0; i < n; i++) { | |
8e766630 | 206 | _cleanup_(route_freep) Route *route = NULL; |
3c9b8860 | 207 | |
ed9e361a | 208 | r = route_new(&route); |
3c9b8860 TG |
209 | if (r >= 0) { |
210 | route->family = AF_INET; | |
f8693fc7 BG |
211 | assert_se(sd_dhcp_route_get_gateway(routes[i], &route->gw.in) >= 0); |
212 | assert_se(sd_dhcp_route_get_destination(routes[i], &route->dst.in) >= 0); | |
213 | assert_se(sd_dhcp_route_get_destination_prefix_length(routes[i], &route->dst_prefixlen) >= 0); | |
3c9b8860 | 214 | |
4645ad47 | 215 | route_remove(route, link, NULL); |
3c9b8860 TG |
216 | } |
217 | } | |
218 | } | |
219 | } | |
220 | ||
f0213e37 | 221 | r = address_new(&address); |
3c9b8860 TG |
222 | if (r >= 0) { |
223 | r = sd_dhcp_lease_get_router(link->dhcp_lease, &gateway); | |
224 | if (r >= 0) { | |
8e766630 LP |
225 | _cleanup_(route_freep) Route *route_gw = NULL; |
226 | _cleanup_(route_freep) Route *route = NULL; | |
3c9b8860 | 227 | |
ed9e361a | 228 | r = route_new(&route_gw); |
3c9b8860 TG |
229 | if (r >= 0) { |
230 | route_gw->family = AF_INET; | |
2ce40956 | 231 | route_gw->dst.in = gateway; |
3c9b8860 TG |
232 | route_gw->dst_prefixlen = 32; |
233 | route_gw->scope = RT_SCOPE_LINK; | |
234 | ||
4645ad47 | 235 | route_remove(route_gw, link, NULL); |
3c9b8860 TG |
236 | } |
237 | ||
ed9e361a | 238 | r = route_new(&route); |
3c9b8860 TG |
239 | if (r >= 0) { |
240 | route->family = AF_INET; | |
2ce40956 | 241 | route->gw.in = gateway; |
3c9b8860 | 242 | |
4645ad47 | 243 | route_remove(route, link, NULL); |
3c9b8860 TG |
244 | } |
245 | } | |
246 | ||
f414a269 TG |
247 | r = sd_dhcp_lease_get_address(link->dhcp_lease, &addr); |
248 | if (r >= 0) { | |
249 | r = sd_dhcp_lease_get_netmask(link->dhcp_lease, &netmask); | |
250 | if (r >= 0) | |
5a941f5f | 251 | prefixlen = in4_addr_netmask_to_prefixlen(&netmask); |
3c9b8860 | 252 | |
f414a269 TG |
253 | address->family = AF_INET; |
254 | address->in_addr.in = addr; | |
255 | address->prefixlen = prefixlen; | |
3c9b8860 | 256 | |
63ae0569 | 257 | address_remove(address, link, NULL); |
f414a269 | 258 | } |
3c9b8860 TG |
259 | } |
260 | ||
27cb34f5 | 261 | if (link->network->dhcp_use_mtu) { |
3c9b8860 TG |
262 | uint16_t mtu; |
263 | ||
264 | r = sd_dhcp_lease_get_mtu(link->dhcp_lease, &mtu); | |
265 | if (r >= 0 && link->original_mtu != mtu) { | |
266 | r = link_set_mtu(link, link->original_mtu); | |
267 | if (r < 0) { | |
79008bdd | 268 | log_link_warning(link, |
3c9b8860 TG |
269 | "DHCP error: could not reset MTU"); |
270 | link_enter_failed(link); | |
271 | return r; | |
272 | } | |
273 | } | |
274 | } | |
275 | ||
27cb34f5 | 276 | if (link->network->dhcp_use_hostname) { |
3c9b8860 TG |
277 | const char *hostname = NULL; |
278 | ||
27cb34f5 LP |
279 | if (link->network->dhcp_hostname) |
280 | hostname = link->network->dhcp_hostname; | |
dce391e7 LP |
281 | else |
282 | (void) sd_dhcp_lease_get_hostname(link->dhcp_lease, &hostname); | |
a7d0ef44 | 283 | |
dce391e7 LP |
284 | if (hostname) { |
285 | /* If a hostname was set due to the lease, then unset it now. */ | |
59eb33e0 | 286 | r = manager_set_hostname(link->manager, NULL); |
3c9b8860 | 287 | if (r < 0) |
dce391e7 | 288 | log_link_warning_errno(link, r, "Failed to reset transient hostname: %m"); |
3c9b8860 TG |
289 | } |
290 | } | |
291 | ||
292 | link->dhcp_lease = sd_dhcp_lease_unref(link->dhcp_lease); | |
6a3e5f6a | 293 | link_dirty(link); |
3c9b8860 TG |
294 | link->dhcp4_configured = false; |
295 | ||
296 | return 0; | |
297 | } | |
298 | ||
302a796f | 299 | static int dhcp4_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
3c9b8860 TG |
300 | int r; |
301 | ||
302 | assert(link); | |
303 | ||
1c4baffc | 304 | r = sd_netlink_message_get_errno(m); |
3c9b8860 | 305 | if (r < 0 && r != -EEXIST) { |
f6b8196f | 306 | log_link_error_errno(link, r, "Could not set DHCPv4 address: %m"); |
3c9b8860 | 307 | link_enter_failed(link); |
45af44d4 | 308 | } else if (r >= 0) |
200a0868 | 309 | manager_rtnl_process_address(rtnl, m, link->manager); |
3c9b8860 TG |
310 | |
311 | link_set_dhcp_routes(link); | |
312 | ||
223932c7 AH |
313 | if (link->dhcp4_messages == 0) { |
314 | link->dhcp4_configured = true; | |
315 | link_check_ready(link); | |
316 | } | |
317 | ||
3c9b8860 TG |
318 | return 1; |
319 | } | |
320 | ||
321 | static int dhcp4_update_address(Link *link, | |
322 | struct in_addr *address, | |
323 | struct in_addr *netmask, | |
324 | uint32_t lifetime) { | |
8e766630 | 325 | _cleanup_(address_freep) Address *addr = NULL; |
3c9b8860 TG |
326 | unsigned prefixlen; |
327 | int r; | |
328 | ||
329 | assert(address); | |
330 | assert(netmask); | |
331 | assert(lifetime); | |
332 | ||
5a941f5f | 333 | prefixlen = in4_addr_netmask_to_prefixlen(netmask); |
3c9b8860 | 334 | |
f0213e37 | 335 | r = address_new(&addr); |
3c9b8860 TG |
336 | if (r < 0) |
337 | return r; | |
338 | ||
339 | addr->family = AF_INET; | |
340 | addr->in_addr.in.s_addr = address->s_addr; | |
341 | addr->cinfo.ifa_prefered = lifetime; | |
342 | addr->cinfo.ifa_valid = lifetime; | |
343 | addr->prefixlen = prefixlen; | |
344 | addr->broadcast.s_addr = address->s_addr | ~netmask->s_addr; | |
345 | ||
66669078 TG |
346 | /* allow reusing an existing address and simply update its lifetime |
347 | * in case it already exists */ | |
483d099e | 348 | r = address_configure(addr, link, dhcp4_address_handler, true); |
3c9b8860 TG |
349 | if (r < 0) |
350 | return r; | |
351 | ||
352 | return 0; | |
353 | } | |
354 | ||
355 | static int dhcp_lease_renew(sd_dhcp_client *client, Link *link) { | |
356 | sd_dhcp_lease *lease; | |
357 | struct in_addr address; | |
358 | struct in_addr netmask; | |
359 | uint32_t lifetime = CACHE_INFO_INFINITY_LIFE_TIME; | |
360 | int r; | |
361 | ||
362 | assert(link); | |
363 | assert(client); | |
364 | assert(link->network); | |
365 | ||
366 | r = sd_dhcp_client_get_lease(client, &lease); | |
f6b8196f LP |
367 | if (r < 0) |
368 | return log_link_warning_errno(link, r, "DHCP error: no lease: %m"); | |
3c9b8860 TG |
369 | |
370 | sd_dhcp_lease_unref(link->dhcp_lease); | |
371 | link->dhcp4_configured = false; | |
e6b18ffa | 372 | link->dhcp_lease = sd_dhcp_lease_ref(lease); |
6a3e5f6a | 373 | link_dirty(link); |
3c9b8860 TG |
374 | |
375 | r = sd_dhcp_lease_get_address(lease, &address); | |
f6b8196f LP |
376 | if (r < 0) |
377 | return log_link_warning_errno(link, r, "DHCP error: no address: %m"); | |
3c9b8860 TG |
378 | |
379 | r = sd_dhcp_lease_get_netmask(lease, &netmask); | |
f6b8196f LP |
380 | if (r < 0) |
381 | return log_link_warning_errno(link, r, "DHCP error: no netmask: %m"); | |
3c9b8860 TG |
382 | |
383 | if (!link->network->dhcp_critical) { | |
f6b8196f LP |
384 | r = sd_dhcp_lease_get_lifetime(link->dhcp_lease, &lifetime); |
385 | if (r < 0) | |
386 | return log_link_warning_errno(link, r, "DHCP error: no lifetime: %m"); | |
3c9b8860 TG |
387 | } |
388 | ||
389 | r = dhcp4_update_address(link, &address, &netmask, lifetime); | |
390 | if (r < 0) { | |
f6b8196f | 391 | log_link_warning_errno(link, r, "Could not update IP address: %m"); |
3c9b8860 TG |
392 | link_enter_failed(link); |
393 | return r; | |
394 | } | |
395 | ||
396 | return 0; | |
397 | } | |
398 | ||
399 | static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) { | |
400 | sd_dhcp_lease *lease; | |
401 | struct in_addr address; | |
402 | struct in_addr netmask; | |
403 | struct in_addr gateway; | |
404 | unsigned prefixlen; | |
405 | uint32_t lifetime = CACHE_INFO_INFINITY_LIFE_TIME; | |
406 | int r; | |
407 | ||
408 | assert(client); | |
409 | assert(link); | |
410 | ||
411 | r = sd_dhcp_client_get_lease(client, &lease); | |
f2341e0a | 412 | if (r < 0) |
f6b8196f | 413 | return log_link_error_errno(link, r, "DHCP error: No lease: %m"); |
3c9b8860 TG |
414 | |
415 | r = sd_dhcp_lease_get_address(lease, &address); | |
f2341e0a | 416 | if (r < 0) |
f6b8196f | 417 | return log_link_error_errno(link, r, "DHCP error: No address: %m"); |
3c9b8860 TG |
418 | |
419 | r = sd_dhcp_lease_get_netmask(lease, &netmask); | |
f2341e0a | 420 | if (r < 0) |
f6b8196f | 421 | return log_link_error_errno(link, r, "DHCP error: No netmask: %m"); |
3c9b8860 | 422 | |
5a941f5f | 423 | prefixlen = in4_addr_netmask_to_prefixlen(&netmask); |
3c9b8860 TG |
424 | |
425 | r = sd_dhcp_lease_get_router(lease, &gateway); | |
397d15fd | 426 | if (r < 0 && r != -ENODATA) |
f6b8196f | 427 | return log_link_error_errno(link, r, "DHCP error: Could not get gateway: %m"); |
3c9b8860 TG |
428 | |
429 | if (r >= 0) | |
f2341e0a LP |
430 | log_struct(LOG_INFO, |
431 | LOG_LINK_INTERFACE(link), | |
432 | LOG_LINK_MESSAGE(link, "DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u", | |
433 | ADDRESS_FMT_VAL(address), | |
434 | prefixlen, | |
435 | ADDRESS_FMT_VAL(gateway)), | |
436 | "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address), | |
437 | "PREFIXLEN=%u", prefixlen, | |
a1230ff9 | 438 | "GATEWAY=%u.%u.%u.%u", ADDRESS_FMT_VAL(gateway)); |
3c9b8860 | 439 | else |
f2341e0a LP |
440 | log_struct(LOG_INFO, |
441 | LOG_LINK_INTERFACE(link), | |
442 | LOG_LINK_MESSAGE(link, "DHCPv4 address %u.%u.%u.%u/%u", | |
443 | ADDRESS_FMT_VAL(address), | |
444 | prefixlen), | |
445 | "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address), | |
a1230ff9 | 446 | "PREFIXLEN=%u", prefixlen); |
3c9b8860 | 447 | |
e6b18ffa | 448 | link->dhcp_lease = sd_dhcp_lease_ref(lease); |
6a3e5f6a | 449 | link_dirty(link); |
3c9b8860 | 450 | |
27cb34f5 | 451 | if (link->network->dhcp_use_mtu) { |
3c9b8860 TG |
452 | uint16_t mtu; |
453 | ||
454 | r = sd_dhcp_lease_get_mtu(lease, &mtu); | |
455 | if (r >= 0) { | |
456 | r = link_set_mtu(link, mtu); | |
457 | if (r < 0) | |
f2341e0a | 458 | log_link_error_errno(link, r, "Failed to set MTU to %" PRIu16 ": %m", mtu); |
3c9b8860 TG |
459 | } |
460 | } | |
461 | ||
27cb34f5 | 462 | if (link->network->dhcp_use_hostname) { |
2de2abad LB |
463 | const char *dhcpname = NULL; |
464 | _cleanup_free_ char *hostname = NULL; | |
3c9b8860 | 465 | |
27cb34f5 | 466 | if (link->network->dhcp_hostname) |
2de2abad | 467 | dhcpname = link->network->dhcp_hostname; |
dce391e7 | 468 | else |
2de2abad LB |
469 | (void) sd_dhcp_lease_get_hostname(lease, &dhcpname); |
470 | ||
471 | if (dhcpname) { | |
472 | r = shorten_overlong(dhcpname, &hostname); | |
473 | if (r < 0) | |
474 | log_link_warning_errno(link, r, "Unable to shorten overlong DHCP hostname '%s', ignoring: %m", dhcpname); | |
475 | if (r == 1) | |
476 | log_link_notice(link, "Overlong DCHP hostname received, shortened from '%s' to '%s'", dhcpname, hostname); | |
477 | } | |
a7d0ef44 | 478 | |
dce391e7 | 479 | if (hostname) { |
59eb33e0 | 480 | r = manager_set_hostname(link->manager, hostname); |
3c9b8860 | 481 | if (r < 0) |
f2341e0a | 482 | log_link_error_errno(link, r, "Failed to set transient hostname to '%s': %m", hostname); |
3c9b8860 TG |
483 | } |
484 | } | |
485 | ||
27cb34f5 | 486 | if (link->network->dhcp_use_timezone) { |
21b80ad1 LP |
487 | const char *tz = NULL; |
488 | ||
489 | (void) sd_dhcp_lease_get_timezone(link->dhcp_lease, &tz); | |
490 | ||
491 | if (tz) { | |
59eb33e0 | 492 | r = manager_set_timezone(link->manager, tz); |
21b80ad1 LP |
493 | if (r < 0) |
494 | log_link_error_errno(link, r, "Failed to set timezone to '%s': %m", tz); | |
495 | } | |
496 | } | |
497 | ||
3c9b8860 | 498 | if (!link->network->dhcp_critical) { |
f2341e0a | 499 | r = sd_dhcp_lease_get_lifetime(link->dhcp_lease, &lifetime); |
fc95c359 YW |
500 | if (r < 0) |
501 | return log_link_warning_errno(link, r, "DHCP error: no lifetime: %m"); | |
3c9b8860 TG |
502 | } |
503 | ||
504 | r = dhcp4_update_address(link, &address, &netmask, lifetime); | |
505 | if (r < 0) { | |
f2341e0a | 506 | log_link_warning_errno(link, r, "Could not update IP address: %m"); |
3c9b8860 TG |
507 | link_enter_failed(link); |
508 | return r; | |
509 | } | |
510 | ||
511 | return 0; | |
512 | } | |
513 | static void dhcp4_handler(sd_dhcp_client *client, int event, void *userdata) { | |
514 | Link *link = userdata; | |
515 | int r = 0; | |
516 | ||
517 | assert(link); | |
518 | assert(link->network); | |
519 | assert(link->manager); | |
520 | ||
521 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
522 | return; | |
523 | ||
524 | switch (event) { | |
03748142 DH |
525 | case SD_DHCP_CLIENT_EVENT_EXPIRED: |
526 | case SD_DHCP_CLIENT_EVENT_STOP: | |
527 | case SD_DHCP_CLIENT_EVENT_IP_CHANGE: | |
3c9b8860 | 528 | if (link->network->dhcp_critical) { |
f6b8196f | 529 | log_link_error(link, "DHCPv4 connection considered system critical, ignoring request to reconfigure it."); |
3c9b8860 TG |
530 | return; |
531 | } | |
532 | ||
533 | if (link->dhcp_lease) { | |
534 | r = dhcp_lease_lost(link); | |
535 | if (r < 0) { | |
536 | link_enter_failed(link); | |
537 | return; | |
538 | } | |
539 | } | |
540 | ||
03748142 | 541 | if (event == SD_DHCP_CLIENT_EVENT_IP_CHANGE) { |
3c9b8860 TG |
542 | r = dhcp_lease_acquired(client, link); |
543 | if (r < 0) { | |
544 | link_enter_failed(link); | |
545 | return; | |
546 | } | |
547 | } | |
548 | ||
549 | break; | |
03748142 | 550 | case SD_DHCP_CLIENT_EVENT_RENEW: |
3c9b8860 TG |
551 | r = dhcp_lease_renew(client, link); |
552 | if (r < 0) { | |
553 | link_enter_failed(link); | |
554 | return; | |
555 | } | |
556 | break; | |
03748142 | 557 | case SD_DHCP_CLIENT_EVENT_IP_ACQUIRE: |
3c9b8860 TG |
558 | r = dhcp_lease_acquired(client, link); |
559 | if (r < 0) { | |
560 | link_enter_failed(link); | |
561 | return; | |
562 | } | |
563 | break; | |
564 | default: | |
565 | if (event < 0) | |
f6b8196f | 566 | log_link_warning_errno(link, event, "DHCP error: Client failed: %m"); |
3c9b8860 | 567 | else |
f6b8196f | 568 | log_link_warning(link, "DHCP unknown event: %i", event); |
3c9b8860 TG |
569 | break; |
570 | } | |
571 | ||
572 | return; | |
573 | } | |
574 | ||
7192bb81 LP |
575 | static int dhcp4_set_hostname(Link *link) { |
576 | _cleanup_free_ char *hostname = NULL; | |
577 | const char *hn; | |
578 | int r; | |
579 | ||
580 | assert(link); | |
581 | ||
582 | if (!link->network->dhcp_send_hostname) | |
583 | hn = NULL; | |
584 | else if (link->network->dhcp_hostname) | |
585 | hn = link->network->dhcp_hostname; | |
586 | else { | |
587 | r = gethostname_strict(&hostname); | |
588 | if (r < 0 && r != -ENXIO) /* ENXIO: no hostname set or hostname is "localhost" */ | |
589 | return r; | |
590 | ||
591 | hn = hostname; | |
592 | } | |
593 | ||
a8494759 YW |
594 | r = sd_dhcp_client_set_hostname(link->dhcp_client, hn); |
595 | if (r == -EINVAL && hostname) | |
596 | /* Ignore error when the machine's hostname is not suitable to send in DHCP packet. */ | |
597 | log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set hostname from kernel hostname, ignoring: %m"); | |
598 | else if (r < 0) | |
599 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set hostname: %m"); | |
600 | ||
601 | return 0; | |
7192bb81 LP |
602 | } |
603 | ||
64b21ece | 604 | static bool promote_secondaries_enabled(const char *ifname) { |
3f550c31 HV |
605 | _cleanup_free_ char *promote_secondaries_sysctl = NULL; |
606 | char *promote_secondaries_path; | |
64b21ece MV |
607 | int r; |
608 | ||
609 | promote_secondaries_path = strjoina("net/ipv4/conf/", ifname, "/promote_secondaries"); | |
610 | r = sysctl_read(promote_secondaries_path, &promote_secondaries_sysctl); | |
611 | if (r < 0) { | |
612 | log_debug_errno(r, "Cannot read sysctl %s", promote_secondaries_path); | |
613 | return false; | |
614 | } | |
615 | ||
616 | truncate_nl(promote_secondaries_sysctl); | |
617 | r = parse_boolean(promote_secondaries_sysctl); | |
618 | if (r < 0) | |
619 | log_warning_errno(r, "Cannot parse sysctl %s with content %s as boolean", promote_secondaries_path, promote_secondaries_sysctl); | |
620 | return r > 0; | |
621 | } | |
622 | ||
623 | /* dhcp4_set_promote_secondaries will ensure this interface has | |
624 | * the "promote_secondaries" option in the kernel set. If this sysctl | |
625 | * is not set DHCP will work only as long as the IP address does not | |
626 | * changes between leases. The kernel will remove all secondary IP | |
627 | * addresses of an interface otherwise. The way systemd-network works | |
628 | * is that the new IP of a lease is added as a secondary IP and when | |
629 | * the primary one expires it relies on the kernel to promote the | |
630 | * secondary IP. See also https://github.com/systemd/systemd/issues/7163 | |
631 | */ | |
632 | int dhcp4_set_promote_secondaries(Link *link) { | |
633 | int r; | |
634 | ||
635 | assert(link); | |
636 | assert(link->network); | |
637 | assert(link->network->dhcp & ADDRESS_FAMILY_IPV4); | |
638 | ||
639 | /* check if the kernel has promote_secondaries enabled for our | |
640 | * interface. If it is not globally enabled or enabled for the | |
641 | * specific interface we must either enable it. | |
642 | */ | |
8e1a7253 | 643 | if (!(promote_secondaries_enabled("all") || promote_secondaries_enabled(link->ifname))) { |
64b21ece MV |
644 | char *promote_secondaries_path = NULL; |
645 | ||
646 | log_link_debug(link, "promote_secondaries is unset, setting it"); | |
647 | promote_secondaries_path = strjoina("net/ipv4/conf/", link->ifname, "/promote_secondaries"); | |
648 | r = sysctl_write(promote_secondaries_path, "1"); | |
649 | if (r < 0) | |
650 | log_link_warning_errno(link, r, "cannot set sysctl %s to 1", promote_secondaries_path); | |
651 | return r > 0; | |
652 | } | |
653 | ||
654 | return 0; | |
655 | } | |
656 | ||
fff1f40c YW |
657 | int dhcp4_set_client_identifier(Link *link) { |
658 | int r; | |
659 | ||
660 | assert(link); | |
661 | assert(link->network); | |
662 | assert(link->dhcp_client); | |
663 | ||
664 | switch (link->network->dhcp_client_identifier) { | |
665 | case DHCP_CLIENT_ID_DUID: { | |
0cf7c3fd | 666 | /* If configured, apply user specified DUID and IAID */ |
fff1f40c YW |
667 | const DUID *duid = link_get_duid(link); |
668 | ||
0cf7c3fd YW |
669 | if (duid->type == DUID_TYPE_LLT && duid->raw_data_len == 0) |
670 | r = sd_dhcp_client_set_iaid_duid_llt(link->dhcp_client, | |
8217ed5e | 671 | link->network->iaid_set, |
0cf7c3fd YW |
672 | link->network->iaid, |
673 | duid->llt_time); | |
674 | else | |
675 | r = sd_dhcp_client_set_iaid_duid(link->dhcp_client, | |
8217ed5e | 676 | link->network->iaid_set, |
0cf7c3fd YW |
677 | link->network->iaid, |
678 | duid->type, | |
679 | duid->raw_data_len > 0 ? duid->raw_data : NULL, | |
680 | duid->raw_data_len); | |
fff1f40c | 681 | if (r < 0) |
0cf7c3fd | 682 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set IAID+DUID: %m"); |
fff1f40c YW |
683 | break; |
684 | } | |
685 | case DHCP_CLIENT_ID_DUID_ONLY: { | |
686 | /* If configured, apply user specified DUID */ | |
687 | const DUID *duid = link_get_duid(link); | |
688 | ||
0cf7c3fd YW |
689 | if (duid->type == DUID_TYPE_LLT && duid->raw_data_len == 0) |
690 | r = sd_dhcp_client_set_duid_llt(link->dhcp_client, | |
89b3fa66 | 691 | duid->llt_time); |
0cf7c3fd YW |
692 | else |
693 | r = sd_dhcp_client_set_duid(link->dhcp_client, | |
694 | duid->type, | |
695 | duid->raw_data_len > 0 ? duid->raw_data : NULL, | |
696 | duid->raw_data_len); | |
fff1f40c YW |
697 | if (r < 0) |
698 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set DUID: %m"); | |
699 | break; | |
700 | } | |
701 | case DHCP_CLIENT_ID_MAC: | |
702 | r = sd_dhcp_client_set_client_id(link->dhcp_client, | |
703 | ARPHRD_ETHER, | |
704 | (const uint8_t *) &link->mac, | |
705 | sizeof(link->mac)); | |
706 | if (r < 0) | |
707 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set client ID: %m"); | |
708 | break; | |
709 | default: | |
710 | assert_not_reached("Unknown client identifier type."); | |
711 | } | |
712 | ||
713 | return 0; | |
714 | } | |
715 | ||
3c9b8860 TG |
716 | int dhcp4_configure(Link *link) { |
717 | int r; | |
718 | ||
719 | assert(link); | |
720 | assert(link->network); | |
e0ee46f2 | 721 | assert(link->network->dhcp & ADDRESS_FAMILY_IPV4); |
3c9b8860 | 722 | |
0bc70f1d | 723 | if (!link->dhcp_client) { |
db3d2358 | 724 | r = sd_dhcp_client_new(&link->dhcp_client, link->network->dhcp_anonymize); |
1f6860d9 YW |
725 | if (r == -ENOMEM) |
726 | return log_oom(); | |
0bc70f1d | 727 | if (r < 0) |
1f6860d9 | 728 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to create DHCP4 client: %m"); |
0bc70f1d | 729 | } |
3c9b8860 TG |
730 | |
731 | r = sd_dhcp_client_attach_event(link->dhcp_client, NULL, 0); | |
732 | if (r < 0) | |
1f6860d9 | 733 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to attach event: %m"); |
3c9b8860 | 734 | |
76253e73 DW |
735 | r = sd_dhcp_client_set_mac(link->dhcp_client, |
736 | (const uint8_t *) &link->mac, | |
737 | sizeof (link->mac), ARPHRD_ETHER); | |
3c9b8860 | 738 | if (r < 0) |
1f6860d9 | 739 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set MAC address: %m"); |
3c9b8860 | 740 | |
2f8e7633 | 741 | r = sd_dhcp_client_set_ifindex(link->dhcp_client, link->ifindex); |
3c9b8860 | 742 | if (r < 0) |
1f6860d9 | 743 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set ifindex: %m"); |
3c9b8860 TG |
744 | |
745 | r = sd_dhcp_client_set_callback(link->dhcp_client, dhcp4_handler, link); | |
746 | if (r < 0) | |
1f6860d9 | 747 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set callback: %m"); |
3c9b8860 TG |
748 | |
749 | r = sd_dhcp_client_set_request_broadcast(link->dhcp_client, | |
750 | link->network->dhcp_broadcast); | |
751 | if (r < 0) | |
1f6860d9 | 752 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for broadcast: %m"); |
3c9b8860 TG |
753 | |
754 | if (link->mtu) { | |
755 | r = sd_dhcp_client_set_mtu(link->dhcp_client, link->mtu); | |
756 | if (r < 0) | |
1f6860d9 | 757 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set MTU: %m"); |
3c9b8860 TG |
758 | } |
759 | ||
27cb34f5 | 760 | if (link->network->dhcp_use_mtu) { |
39745a5a | 761 | r = sd_dhcp_client_set_request_option(link->dhcp_client, |
22805d92 | 762 | SD_DHCP_OPTION_INTERFACE_MTU); |
39745a5a | 763 | if (r < 0) |
1f6860d9 | 764 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for MTU: %m"); |
3c9b8860 TG |
765 | } |
766 | ||
5e77a146 | 767 | /* NOTE: even if this variable is called "use", it also "sends" PRL |
768 | * options, maybe there should be a different configuration variable | |
769 | * to send or not route options?. */ | |
28522b0d | 770 | /* NOTE: when using Anonymize=yes, routes PRL options are sent |
771 | * by default, so they don't need to be added here. */ | |
5e77a146 | 772 | if (link->network->dhcp_use_routes && !link->network->dhcp_anonymize) { |
3c9b8860 | 773 | r = sd_dhcp_client_set_request_option(link->dhcp_client, |
22805d92 | 774 | SD_DHCP_OPTION_STATIC_ROUTE); |
3c9b8860 | 775 | if (r < 0) |
1f6860d9 YW |
776 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for static route: %m"); |
777 | ||
3c9b8860 | 778 | r = sd_dhcp_client_set_request_option(link->dhcp_client, |
22805d92 | 779 | SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE); |
7d6884b6 | 780 | if (r < 0) |
1f6860d9 | 781 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for classless static route: %m"); |
3c9b8860 TG |
782 | } |
783 | ||
ead36ce6 | 784 | if (link->network->dhcp_use_ntp) { |
785 | r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_NTP_SERVER); | |
786 | if (r < 0) | |
1f6860d9 | 787 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for NTP server: %m"); |
ead36ce6 | 788 | } |
4b7b5abb | 789 | |
89573b37 | 790 | if (link->network->dhcp_use_timezone) { |
791 | r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_NEW_TZDB_TIMEZONE); | |
792 | if (r < 0) | |
1f6860d9 | 793 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for timezone: %m"); |
89573b37 | 794 | } |
8eb9058d | 795 | |
7192bb81 LP |
796 | r = dhcp4_set_hostname(link); |
797 | if (r < 0) | |
a8494759 | 798 | return r; |
3c9b8860 TG |
799 | |
800 | if (link->network->dhcp_vendor_class_identifier) { | |
801 | r = sd_dhcp_client_set_vendor_class_identifier(link->dhcp_client, | |
802 | link->network->dhcp_vendor_class_identifier); | |
803 | if (r < 0) | |
1f6860d9 | 804 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set vendor class identifier: %m"); |
3c9b8860 TG |
805 | } |
806 | ||
af1c0de0 SS |
807 | if (link->network->dhcp_user_class) { |
808 | r = sd_dhcp_client_set_user_class(link->dhcp_client, (const char **) link->network->dhcp_user_class); | |
809 | if (r < 0) | |
1f6860d9 | 810 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set user class: %m"); |
af1c0de0 SS |
811 | } |
812 | ||
9faed222 SS |
813 | if (link->network->dhcp_client_port) { |
814 | r = sd_dhcp_client_set_client_port(link->dhcp_client, link->network->dhcp_client_port); | |
815 | if (r < 0) | |
1f6860d9 | 816 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set listen port: %m"); |
9faed222 SS |
817 | } |
818 | ||
fff1f40c | 819 | return dhcp4_set_client_identifier(link); |
3c9b8860 | 820 | } |