]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ | |
2 | ||
3 | #include <linux/if_arp.h> | |
4 | #include <linux/rtnetlink.h> | |
5 | #include <netinet/in.h> | |
6 | #include <netinet/ip.h> | |
7 | #include <stdio.h> | |
8 | ||
9 | #include "sd-dhcp-protocol.h" | |
10 | #include "sd-ipv4ll.h" | |
11 | ||
12 | #include "alloc-util.h" | |
13 | #include "conf-parser.h" | |
14 | #include "device-private.h" | |
15 | #include "dhcp-client-internal.h" | |
16 | #include "errno-util.h" | |
17 | #include "hostname-setup.h" | |
18 | #include "networkd-address.h" | |
19 | #include "networkd-dhcp-prefix-delegation.h" | |
20 | #include "networkd-dhcp4.h" | |
21 | #include "networkd-dhcp4-bus.h" | |
22 | #include "networkd-ipv4ll.h" | |
23 | #include "networkd-link.h" | |
24 | #include "networkd-manager.h" | |
25 | #include "networkd-network.h" | |
26 | #include "networkd-nexthop.h" | |
27 | #include "networkd-ntp.h" | |
28 | #include "networkd-queue.h" | |
29 | #include "networkd-route.h" | |
30 | #include "networkd-setlink.h" | |
31 | #include "networkd-state-file.h" | |
32 | #include "parse-util.h" | |
33 | #include "set.h" | |
34 | #include "socket-util.h" | |
35 | #include "string-table.h" | |
36 | #include "string-util.h" | |
37 | #include "strv.h" | |
38 | ||
39 | void network_adjust_dhcp4(Network *network) { | |
40 | assert(network); | |
41 | ||
42 | if (!FLAGS_SET(network->dhcp, ADDRESS_FAMILY_IPV4)) | |
43 | return; | |
44 | ||
45 | if (network->dhcp_use_gateway < 0) | |
46 | network->dhcp_use_gateway = network->dhcp_use_routes; | |
47 | ||
48 | /* RFC7844 section 3.: MAY contain the Client Identifier option | |
49 | * Section 3.5: clients MUST use client identifiers based solely on the link-layer address | |
50 | * NOTE: Using MAC, as it does not reveal extra information, and some servers might not answer | |
51 | * if this option is not sent */ | |
52 | if (network->dhcp_anonymize && | |
53 | network->dhcp_client_identifier >= 0 && | |
54 | network->dhcp_client_identifier != DHCP_CLIENT_ID_MAC) { | |
55 | log_warning("%s: ClientIdentifier= is set, although Anonymize=yes. Using ClientIdentifier=mac.", | |
56 | network->filename); | |
57 | network->dhcp_client_identifier = DHCP_CLIENT_ID_MAC; | |
58 | } | |
59 | ||
60 | if (network->dhcp_client_identifier < 0) | |
61 | network->dhcp_client_identifier = network->dhcp_anonymize ? DHCP_CLIENT_ID_MAC : network->manager->dhcp_client_identifier; | |
62 | ||
63 | /* By default, RapidCommit= is enabled when Anonymize=no and neither AllowList= nor DenyList= is specified. */ | |
64 | if (network->dhcp_use_rapid_commit < 0) | |
65 | network->dhcp_use_rapid_commit = | |
66 | !network->dhcp_anonymize && | |
67 | set_isempty(network->dhcp_allow_listed_ip) && | |
68 | set_isempty(network->dhcp_deny_listed_ip); | |
69 | } | |
70 | ||
71 | static int dhcp4_prefix_covers( | |
72 | Link *link, | |
73 | const struct in_addr *in_prefix, | |
74 | uint8_t in_prefixlen) { | |
75 | ||
76 | struct in_addr prefix; | |
77 | uint8_t prefixlen; | |
78 | int r; | |
79 | ||
80 | assert(link); | |
81 | assert(link->dhcp_lease); | |
82 | assert(in_prefix); | |
83 | ||
84 | /* Return true if the input address or address range is in the assigned network. | |
85 | * E.g. if the DHCP server provides 192.168.0.100/24, then this returns true for the address or | |
86 | * address range in 192.168.0.0/24, and returns false otherwise. */ | |
87 | ||
88 | r = sd_dhcp_lease_get_prefix(link->dhcp_lease, &prefix, &prefixlen); | |
89 | if (r < 0) | |
90 | return r; | |
91 | ||
92 | return in4_addr_prefix_covers_full(&prefix, prefixlen, in_prefix, in_prefixlen); | |
93 | } | |
94 | ||
95 | static int dhcp4_get_router(Link *link, struct in_addr *ret) { | |
96 | const struct in_addr *routers; | |
97 | int r; | |
98 | ||
99 | assert(link); | |
100 | assert(link->dhcp_lease); | |
101 | assert(ret); | |
102 | ||
103 | r = sd_dhcp_lease_get_router(link->dhcp_lease, &routers); | |
104 | if (r < 0) | |
105 | return r; | |
106 | ||
107 | /* The router option may provide multiple routers, We only use the first non-null address. */ | |
108 | ||
109 | FOREACH_ARRAY(router, routers, r) { | |
110 | if (in4_addr_is_null(router)) | |
111 | continue; | |
112 | ||
113 | *ret = *router; | |
114 | return 0; | |
115 | } | |
116 | ||
117 | return -ENODATA; | |
118 | } | |
119 | ||
120 | static int dhcp4_get_classless_static_or_static_routes(Link *link, sd_dhcp_route ***ret_routes, size_t *ret_num) { | |
121 | _cleanup_free_ sd_dhcp_route **routes = NULL; | |
122 | int r; | |
123 | ||
124 | assert(link); | |
125 | assert(link->dhcp_lease); | |
126 | ||
127 | /* If the DHCP server returns both a Classless Static Routes option and a Static Routes option, | |
128 | * the DHCP client MUST ignore the Static Routes option. */ | |
129 | ||
130 | r = sd_dhcp_lease_get_classless_routes(link->dhcp_lease, &routes); | |
131 | if (r >= 0) { | |
132 | assert(r > 0); | |
133 | if (ret_routes) | |
134 | *ret_routes = TAKE_PTR(routes); | |
135 | if (ret_num) | |
136 | *ret_num = r; | |
137 | return 1; /* classless */ | |
138 | } else if (r != -ENODATA) | |
139 | return r; | |
140 | ||
141 | r = sd_dhcp_lease_get_static_routes(link->dhcp_lease, &routes); | |
142 | if (r < 0) | |
143 | return r; | |
144 | ||
145 | assert(r > 0); | |
146 | if (ret_routes) | |
147 | *ret_routes = TAKE_PTR(routes); | |
148 | if (ret_num) | |
149 | *ret_num = r; | |
150 | return 0; /* static */ | |
151 | } | |
152 | ||
153 | static int dhcp4_find_gateway_for_destination( | |
154 | Link *link, | |
155 | const struct in_addr *destination, | |
156 | uint8_t prefixlength, | |
157 | struct in_addr *ret) { | |
158 | ||
159 | _cleanup_free_ sd_dhcp_route **routes = NULL; | |
160 | size_t n_routes = 0; | |
161 | bool is_classless; | |
162 | uint8_t max_prefixlen = UINT8_MAX; | |
163 | struct in_addr gw; | |
164 | int r; | |
165 | ||
166 | assert(link); | |
167 | assert(link->dhcp_lease); | |
168 | assert(destination); | |
169 | assert(ret); | |
170 | ||
171 | /* This tries to find the most suitable gateway for an address or address range. | |
172 | * E.g. if the server provides the default gateway 192.168.0.1 and a classless static route for | |
173 | * 8.0.0.0/8 with gateway 192.168.0.2, then this returns 192.168.0.2 for 8.8.8.8/32, and 192.168.0.1 | |
174 | * for 9.9.9.9/32. If the input address or address range is in the assigned network, then the null | |
175 | * address will be returned. */ | |
176 | ||
177 | /* First, check with the assigned prefix, and if the destination is in the prefix, set the null | |
178 | * address for the gateway, and return it unless more suitable static route is found. */ | |
179 | r = dhcp4_prefix_covers(link, destination, prefixlength); | |
180 | if (r < 0) | |
181 | return r; | |
182 | if (r > 0) { | |
183 | r = sd_dhcp_lease_get_prefix(link->dhcp_lease, NULL, &max_prefixlen); | |
184 | if (r < 0) | |
185 | return r; | |
186 | ||
187 | gw = (struct in_addr) {}; | |
188 | } | |
189 | ||
190 | r = dhcp4_get_classless_static_or_static_routes(link, &routes, &n_routes); | |
191 | if (r < 0 && r != -ENODATA) | |
192 | return r; | |
193 | is_classless = r > 0; | |
194 | ||
195 | /* First, find most suitable gateway. */ | |
196 | FOREACH_ARRAY(e, routes, n_routes) { | |
197 | struct in_addr dst; | |
198 | uint8_t len; | |
199 | ||
200 | r = sd_dhcp_route_get_destination(*e, &dst); | |
201 | if (r < 0) | |
202 | return r; | |
203 | ||
204 | r = sd_dhcp_route_get_destination_prefix_length(*e, &len); | |
205 | if (r < 0) | |
206 | return r; | |
207 | ||
208 | r = in4_addr_prefix_covers_full(&dst, len, destination, prefixlength); | |
209 | if (r < 0) | |
210 | return r; | |
211 | if (r == 0) | |
212 | continue; | |
213 | ||
214 | if (max_prefixlen != UINT8_MAX && max_prefixlen > len) | |
215 | continue; | |
216 | ||
217 | r = sd_dhcp_route_get_gateway(*e, &gw); | |
218 | if (r < 0) | |
219 | return r; | |
220 | ||
221 | max_prefixlen = len; | |
222 | } | |
223 | ||
224 | /* The destination is reachable. Note, the gateway address returned here may be NULL. */ | |
225 | if (max_prefixlen != UINT8_MAX) { | |
226 | *ret = gw; | |
227 | return 0; | |
228 | } | |
229 | ||
230 | /* According to RFC 3442: If the DHCP server returns both a Classless Static Routes option and | |
231 | * a Router option, the DHCP client MUST ignore the Router option. */ | |
232 | if (!is_classless) { | |
233 | /* No matching static route is found, and the destination is not in the acquired network, | |
234 | * falling back to the Router option. */ | |
235 | r = dhcp4_get_router(link, ret); | |
236 | if (r >= 0) | |
237 | return 0; | |
238 | if (r != -ENODATA) | |
239 | return r; | |
240 | } | |
241 | ||
242 | return -EHOSTUNREACH; /* Cannot reach the destination. */ | |
243 | } | |
244 | ||
245 | static int dhcp4_remove_address_and_routes(Link *link, bool only_marked) { | |
246 | Address *address; | |
247 | Route *route; | |
248 | int ret = 0; | |
249 | ||
250 | assert(link); | |
251 | assert(link->manager); | |
252 | ||
253 | SET_FOREACH(route, link->manager->routes) { | |
254 | if (route->source != NETWORK_CONFIG_SOURCE_DHCP4) | |
255 | continue; | |
256 | if (route->nexthop.ifindex != link->ifindex) | |
257 | continue; | |
258 | if (only_marked && !route_is_marked(route)) | |
259 | continue; | |
260 | ||
261 | RET_GATHER(ret, route_remove_and_cancel(route, link->manager)); | |
262 | } | |
263 | ||
264 | SET_FOREACH(address, link->addresses) { | |
265 | if (address->source != NETWORK_CONFIG_SOURCE_DHCP4) | |
266 | continue; | |
267 | if (only_marked && !address_is_marked(address)) | |
268 | continue; | |
269 | ||
270 | RET_GATHER(ret, address_remove_and_cancel(address, link)); | |
271 | } | |
272 | ||
273 | return ret; | |
274 | } | |
275 | ||
276 | static int dhcp4_address_get(Link *link, Address **ret) { | |
277 | Address *address; | |
278 | ||
279 | assert(link); | |
280 | ||
281 | SET_FOREACH(address, link->addresses) { | |
282 | if (address->source != NETWORK_CONFIG_SOURCE_DHCP4) | |
283 | continue; | |
284 | if (address_is_marked(address)) | |
285 | continue; | |
286 | ||
287 | if (ret) | |
288 | *ret = address; | |
289 | return 0; | |
290 | } | |
291 | ||
292 | return -ENOENT; | |
293 | } | |
294 | ||
295 | static int dhcp4_address_ready_callback(Address *address) { | |
296 | assert(address); | |
297 | assert(address->link); | |
298 | ||
299 | /* Do not call this again. */ | |
300 | address->callback = NULL; | |
301 | ||
302 | return dhcp4_check_ready(address->link); | |
303 | } | |
304 | ||
305 | int dhcp4_check_ready(Link *link) { | |
306 | Address *address; | |
307 | int r; | |
308 | ||
309 | assert(link); | |
310 | ||
311 | if (link->dhcp4_messages > 0) { | |
312 | log_link_debug(link, "%s(): DHCPv4 address and routes are not set.", __func__); | |
313 | return 0; | |
314 | } | |
315 | ||
316 | if (dhcp4_address_get(link, &address) < 0) { | |
317 | log_link_debug(link, "%s(): DHCPv4 address is not set.", __func__); | |
318 | return 0; | |
319 | } | |
320 | ||
321 | if (!address_is_ready(address)) { | |
322 | log_link_debug(link, "%s(): DHCPv4 address is not ready.", __func__); | |
323 | address->callback = dhcp4_address_ready_callback; | |
324 | return 0; | |
325 | } | |
326 | ||
327 | link->dhcp4_configured = true; | |
328 | log_link_debug(link, "DHCPv4 address and routes set."); | |
329 | ||
330 | /* New address and routes are configured now. Let's release old lease. */ | |
331 | r = dhcp4_remove_address_and_routes(link, /* only_marked = */ true); | |
332 | if (r < 0) | |
333 | return r; | |
334 | ||
335 | r = link_request_stacked_netdevs(link, NETDEV_LOCAL_ADDRESS_DHCP4); | |
336 | if (r < 0) | |
337 | return r; | |
338 | ||
339 | r = sd_ipv4ll_stop(link->ipv4ll); | |
340 | if (r < 0) | |
341 | return log_link_warning_errno(link, r, "Failed to drop IPv4 link-local address: %m"); | |
342 | ||
343 | link_check_ready(link); | |
344 | return 0; | |
345 | } | |
346 | ||
347 | static int dhcp4_route_handler(sd_netlink *rtnl, sd_netlink_message *m, Request *req, Link *link, Route *route) { | |
348 | int r; | |
349 | ||
350 | assert(req); | |
351 | assert(link); | |
352 | assert(route); | |
353 | ||
354 | r = route_configure_handler_internal(m, req, route); | |
355 | if (r <= 0) | |
356 | return r; | |
357 | ||
358 | r = dhcp4_check_ready(link); | |
359 | if (r < 0) | |
360 | link_enter_failed(link); | |
361 | ||
362 | return 1; | |
363 | } | |
364 | ||
365 | static int dhcp4_request_route(Route *route, Link *link) { | |
366 | struct in_addr server; | |
367 | Route *existing; | |
368 | int r; | |
369 | ||
370 | assert(route); | |
371 | assert(link); | |
372 | assert(link->manager); | |
373 | assert(link->network); | |
374 | assert(link->dhcp_lease); | |
375 | ||
376 | r = sd_dhcp_lease_get_server_identifier(link->dhcp_lease, &server); | |
377 | if (r < 0) | |
378 | return log_link_debug_errno(link, r, "Failed to get DHCP server IP address: %m"); | |
379 | ||
380 | route->source = NETWORK_CONFIG_SOURCE_DHCP4; | |
381 | route->provider.in = server; | |
382 | route->family = AF_INET; | |
383 | if (!route->protocol_set) | |
384 | route->protocol = RTPROT_DHCP; | |
385 | if (!route->priority_set) | |
386 | route->priority = link->network->dhcp_route_metric; | |
387 | if (!route->table_set) | |
388 | route->table = link_get_dhcp4_route_table(link); | |
389 | r = route_metric_set(&route->metric, RTAX_MTU, link->network->dhcp_route_mtu); | |
390 | if (r < 0) | |
391 | return r; | |
392 | r = route_metric_set(&route->metric, RTAX_INITCWND, link->network->dhcp_initial_congestion_window); | |
393 | if (r < 0) | |
394 | return r; | |
395 | r = route_metric_set(&route->metric, RTAX_INITRWND, link->network->dhcp_advertised_receive_window); | |
396 | if (r < 0) | |
397 | return r; | |
398 | r = route_metric_set(&route->metric, RTAX_QUICKACK, link->network->dhcp_quickack); | |
399 | if (r < 0) | |
400 | return r; | |
401 | ||
402 | r = route_adjust_nexthops(route, link); | |
403 | if (r < 0) | |
404 | return r; | |
405 | ||
406 | if (route_get(link->manager, route, &existing) < 0) /* This is a new route. */ | |
407 | link->dhcp4_configured = false; | |
408 | else | |
409 | route_unmark(existing); | |
410 | ||
411 | return link_request_route(link, route, &link->dhcp4_messages, dhcp4_route_handler); | |
412 | } | |
413 | ||
414 | static bool prefixroute_by_kernel(Link *link) { | |
415 | return !link->network->dhcp_route_table_set || | |
416 | link->network->dhcp_route_table == RT_TABLE_MAIN; | |
417 | } | |
418 | ||
419 | static int dhcp4_request_prefix_route(Link *link, Route *rt) { | |
420 | _cleanup_(route_unrefp) Route *route = NULL; | |
421 | int r; | |
422 | ||
423 | assert(link); | |
424 | assert(link->dhcp_lease); | |
425 | ||
426 | if (prefixroute_by_kernel(link) && (!rt || !rt->table_set || rt->table == RT_TABLE_MAIN)) | |
427 | /* The prefix route in the main table will be created by the kernel. See dhcp4_update_address(). */ | |
428 | return 0; | |
429 | ||
430 | r = route_new(&route); | |
431 | if (r < 0) | |
432 | return r; | |
433 | ||
434 | route->scope = RT_SCOPE_LINK; | |
435 | if (rt) { | |
436 | route->table_set = rt->table_set; | |
437 | route->table = rt->table; | |
438 | } | |
439 | ||
440 | r = sd_dhcp_lease_get_prefix(link->dhcp_lease, &route->dst.in, &route->dst_prefixlen); | |
441 | if (r < 0) | |
442 | return r; | |
443 | ||
444 | r = sd_dhcp_lease_get_address(link->dhcp_lease, &route->prefsrc.in); | |
445 | if (r < 0) | |
446 | return r; | |
447 | ||
448 | return dhcp4_request_route(route, link); | |
449 | } | |
450 | ||
451 | static int dhcp4_request_route_to_gateway(Link *link, const Route *rt) { | |
452 | _cleanup_(route_unrefp) Route *route = NULL; | |
453 | struct in_addr address; | |
454 | int r; | |
455 | ||
456 | assert(link); | |
457 | assert(link->dhcp_lease); | |
458 | assert(rt); | |
459 | ||
460 | if (in_addr_is_set(rt->nexthop.family, &rt->nexthop.gw) <= 0) | |
461 | return 0; | |
462 | ||
463 | assert(rt->nexthop.family == AF_INET); | |
464 | ||
465 | r = sd_dhcp_lease_get_address(link->dhcp_lease, &address); | |
466 | if (r < 0) | |
467 | return r; | |
468 | ||
469 | r = route_new(&route); | |
470 | if (r < 0) | |
471 | return r; | |
472 | ||
473 | route->dst.in = rt->nexthop.gw.in; | |
474 | route->dst_prefixlen = 32; | |
475 | route->prefsrc.in = address; | |
476 | route->scope = RT_SCOPE_LINK; | |
477 | route->table = rt->table; | |
478 | route->table_set = rt->table_set; | |
479 | ||
480 | return dhcp4_request_route(route, link); | |
481 | } | |
482 | ||
483 | static int dhcp4_request_route_auto( | |
484 | Route *route, | |
485 | Link *link, | |
486 | const struct in_addr *gw) { | |
487 | ||
488 | struct in_addr address; | |
489 | int r; | |
490 | ||
491 | assert(route); | |
492 | assert(link); | |
493 | assert(link->dhcp_lease); | |
494 | assert(gw); | |
495 | ||
496 | r = sd_dhcp_lease_get_address(link->dhcp_lease, &address); | |
497 | if (r < 0) | |
498 | return r; | |
499 | ||
500 | if (in4_addr_is_localhost(&route->dst.in)) { | |
501 | if (in4_addr_is_set(gw)) | |
502 | log_link_debug(link, "DHCP: requested route destination "IPV4_ADDRESS_FMT_STR"/%u is localhost, " | |
503 | "ignoring gateway address "IPV4_ADDRESS_FMT_STR, | |
504 | IPV4_ADDRESS_FMT_VAL(route->dst.in), route->dst_prefixlen, IPV4_ADDRESS_FMT_VAL(*gw)); | |
505 | ||
506 | route->scope = RT_SCOPE_HOST; | |
507 | route->nexthop.family = AF_UNSPEC; | |
508 | route->nexthop.gw = IN_ADDR_NULL; | |
509 | route->prefsrc = IN_ADDR_NULL; | |
510 | ||
511 | } else if (in4_addr_equal(&route->dst.in, &address)) { | |
512 | if (in4_addr_is_set(gw)) | |
513 | log_link_debug(link, "DHCP: requested route destination "IPV4_ADDRESS_FMT_STR"/%u is equivalent to the acquired address, " | |
514 | "ignoring gateway address "IPV4_ADDRESS_FMT_STR, | |
515 | IPV4_ADDRESS_FMT_VAL(route->dst.in), route->dst_prefixlen, IPV4_ADDRESS_FMT_VAL(*gw)); | |
516 | ||
517 | route->scope = RT_SCOPE_HOST; | |
518 | route->nexthop.family = AF_UNSPEC; | |
519 | route->nexthop.gw = IN_ADDR_NULL; | |
520 | route->prefsrc.in = address; | |
521 | ||
522 | } else if (in4_addr_is_null(gw)) { | |
523 | r = dhcp4_prefix_covers(link, &route->dst.in, route->dst_prefixlen); | |
524 | if (r < 0) | |
525 | return r; | |
526 | if (r == 0 && DEBUG_LOGGING) { | |
527 | struct in_addr prefix; | |
528 | uint8_t prefixlen; | |
529 | ||
530 | r = sd_dhcp_lease_get_prefix(link->dhcp_lease, &prefix, &prefixlen); | |
531 | if (r < 0) | |
532 | return r; | |
533 | ||
534 | log_link_debug(link, "DHCP: requested route destination "IPV4_ADDRESS_FMT_STR"/%u is not in the assigned network " | |
535 | IPV4_ADDRESS_FMT_STR"/%u, but no gateway is specified, using 'link' scope.", | |
536 | IPV4_ADDRESS_FMT_VAL(route->dst.in), route->dst_prefixlen, | |
537 | IPV4_ADDRESS_FMT_VAL(prefix), prefixlen); | |
538 | } | |
539 | ||
540 | route->scope = RT_SCOPE_LINK; | |
541 | route->nexthop.family = AF_UNSPEC; | |
542 | route->nexthop.gw = IN_ADDR_NULL; | |
543 | route->prefsrc.in = address; | |
544 | ||
545 | } else { | |
546 | route->scope = RT_SCOPE_UNIVERSE; | |
547 | route->nexthop.family = AF_INET; | |
548 | route->nexthop.gw.in = *gw; | |
549 | route->prefsrc.in = address; | |
550 | ||
551 | r = dhcp4_request_route_to_gateway(link, route); | |
552 | if (r < 0) | |
553 | return r; | |
554 | } | |
555 | ||
556 | return dhcp4_request_route(route, link); | |
557 | } | |
558 | ||
559 | static int dhcp4_request_classless_static_or_static_routes(Link *link) { | |
560 | _cleanup_free_ sd_dhcp_route **routes = NULL; | |
561 | size_t n_routes; | |
562 | int r; | |
563 | ||
564 | assert(link); | |
565 | assert(link->dhcp_lease); | |
566 | ||
567 | if (!link->network->dhcp_use_routes) | |
568 | return 0; | |
569 | ||
570 | r = dhcp4_get_classless_static_or_static_routes(link, &routes, &n_routes); | |
571 | if (r == -ENODATA) | |
572 | return 0; | |
573 | if (r < 0) | |
574 | return r; | |
575 | ||
576 | FOREACH_ARRAY(e, routes, n_routes) { | |
577 | _cleanup_(route_unrefp) Route *route = NULL; | |
578 | struct in_addr gw; | |
579 | ||
580 | r = route_new(&route); | |
581 | if (r < 0) | |
582 | return r; | |
583 | ||
584 | r = sd_dhcp_route_get_gateway(*e, &gw); | |
585 | if (r < 0) | |
586 | return r; | |
587 | ||
588 | r = sd_dhcp_route_get_destination(*e, &route->dst.in); | |
589 | if (r < 0) | |
590 | return r; | |
591 | ||
592 | r = sd_dhcp_route_get_destination_prefix_length(*e, &route->dst_prefixlen); | |
593 | if (r < 0) | |
594 | return r; | |
595 | ||
596 | r = dhcp4_request_route_auto(route, link, &gw); | |
597 | if (r < 0) | |
598 | return r; | |
599 | } | |
600 | ||
601 | return 0; | |
602 | } | |
603 | ||
604 | static int dhcp4_request_default_gateway(Link *link) { | |
605 | _cleanup_(route_unrefp) Route *route = NULL; | |
606 | struct in_addr address, router; | |
607 | int r; | |
608 | ||
609 | assert(link); | |
610 | assert(link->dhcp_lease); | |
611 | ||
612 | if (!link->network->dhcp_use_gateway) | |
613 | return 0; | |
614 | ||
615 | /* According to RFC 3442: If the DHCP server returns both a Classless Static Routes option and | |
616 | * a Router option, the DHCP client MUST ignore the Router option. */ | |
617 | if (link->network->dhcp_use_routes && | |
618 | dhcp4_get_classless_static_or_static_routes(link, NULL, NULL) > 0) | |
619 | return 0; | |
620 | ||
621 | r = sd_dhcp_lease_get_address(link->dhcp_lease, &address); | |
622 | if (r < 0) | |
623 | return r; | |
624 | ||
625 | r = dhcp4_get_router(link, &router); | |
626 | if (r == -ENODATA) { | |
627 | log_link_debug(link, "DHCP: No valid router address received from DHCP server."); | |
628 | return 0; | |
629 | } | |
630 | if (r < 0) | |
631 | return r; | |
632 | ||
633 | r = route_new(&route); | |
634 | if (r < 0) | |
635 | return r; | |
636 | ||
637 | /* Next, add a default gateway. */ | |
638 | route->nexthop.family = AF_INET; | |
639 | route->nexthop.gw.in = router; | |
640 | route->prefsrc.in = address; | |
641 | ||
642 | /* The dhcp netmask may mask out the gateway. First, add an explicit route for the gateway host | |
643 | * so that we can route no matter the netmask or existing kernel route tables. */ | |
644 | r = dhcp4_request_route_to_gateway(link, route); | |
645 | if (r < 0) | |
646 | return r; | |
647 | ||
648 | return dhcp4_request_route(route, link); | |
649 | } | |
650 | ||
651 | static int dhcp4_request_semi_static_routes(Link *link) { | |
652 | Route *rt; | |
653 | int r; | |
654 | ||
655 | assert(link); | |
656 | assert(link->dhcp_lease); | |
657 | assert(link->network); | |
658 | ||
659 | HASHMAP_FOREACH(rt, link->network->routes_by_section) { | |
660 | _cleanup_(route_unrefp) Route *route = NULL; | |
661 | struct in_addr gw; | |
662 | ||
663 | if (rt->source != NETWORK_CONFIG_SOURCE_DHCP4) | |
664 | continue; | |
665 | ||
666 | assert(rt->family == AF_INET); | |
667 | assert(rt->nexthop.family == AF_INET); | |
668 | ||
669 | r = dhcp4_find_gateway_for_destination(link, &rt->dst.in, rt->dst_prefixlen, &gw); | |
670 | if (r == -EHOSTUNREACH) { | |
671 | log_link_debug_errno(link, r, "DHCP: Cannot find suitable gateway for destination %s of semi-static route, ignoring: %m", | |
672 | IN4_ADDR_PREFIX_TO_STRING(&rt->dst.in, rt->dst_prefixlen)); | |
673 | continue; | |
674 | } | |
675 | if (r < 0) | |
676 | return r; | |
677 | ||
678 | if (in4_addr_is_null(&gw)) { | |
679 | log_link_debug(link, "DHCP: Destination %s of semi-static route is in the acquired network, skipping configuration.", | |
680 | IN4_ADDR_PREFIX_TO_STRING(&rt->dst.in, rt->dst_prefixlen)); | |
681 | continue; | |
682 | } | |
683 | ||
684 | r = route_dup(rt, NULL, &route); | |
685 | if (r < 0) | |
686 | return r; | |
687 | ||
688 | route->nexthop.gw.in = gw; | |
689 | ||
690 | if (!route->prefsrc_set) { | |
691 | r = sd_dhcp_lease_get_address(link->dhcp_lease, &route->prefsrc.in); | |
692 | if (r < 0) | |
693 | return r; | |
694 | } | |
695 | ||
696 | r = dhcp4_request_prefix_route(link, route); | |
697 | if (r < 0) | |
698 | return r; | |
699 | ||
700 | r = dhcp4_request_route_to_gateway(link, route); | |
701 | if (r < 0) | |
702 | return r; | |
703 | ||
704 | r = dhcp4_request_route(route, link); | |
705 | if (r < 0) | |
706 | return r; | |
707 | } | |
708 | ||
709 | return 0; | |
710 | } | |
711 | ||
712 | static int dhcp4_request_routes_to_servers( | |
713 | Link *link, | |
714 | const struct in_addr *servers, | |
715 | size_t n_servers) { | |
716 | ||
717 | int r; | |
718 | ||
719 | assert(link); | |
720 | assert(link->dhcp_lease); | |
721 | assert(link->network); | |
722 | assert(servers || n_servers == 0); | |
723 | ||
724 | FOREACH_ARRAY(dst, servers, n_servers) { | |
725 | _cleanup_(route_unrefp) Route *route = NULL; | |
726 | struct in_addr gw; | |
727 | ||
728 | if (in4_addr_is_null(dst)) | |
729 | continue; | |
730 | ||
731 | r = dhcp4_find_gateway_for_destination(link, dst, 32, &gw); | |
732 | if (r == -EHOSTUNREACH) { | |
733 | log_link_debug_errno(link, r, "DHCP: Cannot find suitable gateway for destination %s, ignoring: %m", | |
734 | IN4_ADDR_PREFIX_TO_STRING(dst, 32)); | |
735 | continue; | |
736 | } | |
737 | if (r < 0) | |
738 | return r; | |
739 | ||
740 | r = route_new(&route); | |
741 | if (r < 0) | |
742 | return r; | |
743 | ||
744 | route->dst.in = *dst; | |
745 | route->dst_prefixlen = 32; | |
746 | ||
747 | r = dhcp4_request_route_auto(route, link, &gw); | |
748 | if (r < 0) | |
749 | return r; | |
750 | } | |
751 | ||
752 | return 0; | |
753 | } | |
754 | ||
755 | static int dhcp4_request_routes_to_dns(Link *link) { | |
756 | const struct in_addr *dns; | |
757 | int r; | |
758 | ||
759 | assert(link); | |
760 | assert(link->dhcp_lease); | |
761 | assert(link->network); | |
762 | ||
763 | if (!link_get_use_dns(link, NETWORK_CONFIG_SOURCE_DHCP4) || | |
764 | !link->network->dhcp_routes_to_dns) | |
765 | return 0; | |
766 | ||
767 | r = sd_dhcp_lease_get_dns(link->dhcp_lease, &dns); | |
768 | if (IN_SET(r, 0, -ENODATA)) | |
769 | return 0; | |
770 | if (r < 0) | |
771 | return r; | |
772 | ||
773 | return dhcp4_request_routes_to_servers(link, dns, r); | |
774 | } | |
775 | ||
776 | static int dhcp4_request_routes_to_ntp(Link *link) { | |
777 | const struct in_addr *ntp; | |
778 | int r; | |
779 | ||
780 | assert(link); | |
781 | assert(link->dhcp_lease); | |
782 | assert(link->network); | |
783 | ||
784 | if (!link_get_use_ntp(link, NETWORK_CONFIG_SOURCE_DHCP4) || | |
785 | !link->network->dhcp_routes_to_ntp) | |
786 | return 0; | |
787 | ||
788 | r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &ntp); | |
789 | if (IN_SET(r, 0, -ENODATA)) | |
790 | return 0; | |
791 | if (r < 0) | |
792 | return r; | |
793 | ||
794 | return dhcp4_request_routes_to_servers(link, ntp, r); | |
795 | } | |
796 | ||
797 | static int dhcp4_request_routes(Link *link) { | |
798 | int r; | |
799 | ||
800 | assert(link); | |
801 | assert(link->dhcp_lease); | |
802 | ||
803 | r = dhcp4_request_prefix_route(link, /* rt = */ NULL); | |
804 | if (r < 0) | |
805 | return log_link_error_errno(link, r, "DHCP error: Could not request prefix route: %m"); | |
806 | ||
807 | r = dhcp4_request_default_gateway(link); | |
808 | if (r < 0) | |
809 | return log_link_error_errno(link, r, "DHCP error: Could not request default gateway: %m"); | |
810 | ||
811 | r = dhcp4_request_classless_static_or_static_routes(link); | |
812 | if (r < 0) | |
813 | return log_link_error_errno(link, r, "DHCP error: Could not request static routes: %m"); | |
814 | ||
815 | r = dhcp4_request_semi_static_routes(link); | |
816 | if (r < 0) | |
817 | return log_link_error_errno(link, r, "DHCP error: Could not request routes with Gateway=_dhcp4 setting: %m"); | |
818 | ||
819 | r = dhcp4_request_routes_to_dns(link); | |
820 | if (r < 0) | |
821 | return log_link_error_errno(link, r, "DHCP error: Could not request routes to DNS servers: %m"); | |
822 | ||
823 | r = dhcp4_request_routes_to_ntp(link); | |
824 | if (r < 0) | |
825 | return log_link_error_errno(link, r, "DHCP error: Could not request routes to NTP servers: %m"); | |
826 | ||
827 | return 0; | |
828 | } | |
829 | ||
830 | static int dhcp_reset_mtu(Link *link) { | |
831 | int r; | |
832 | ||
833 | assert(link); | |
834 | ||
835 | if (!link->network->dhcp_use_mtu) | |
836 | return 0; | |
837 | ||
838 | r = link_request_to_set_mtu(link, link->original_mtu); | |
839 | if (r < 0) | |
840 | return log_link_error_errno(link, r, "DHCP error: Could not queue request to reset MTU: %m"); | |
841 | ||
842 | return 0; | |
843 | } | |
844 | ||
845 | static int dhcp_reset_hostname(Link *link) { | |
846 | const char *hostname; | |
847 | int r; | |
848 | ||
849 | assert(link); | |
850 | ||
851 | if (!link->network->dhcp_use_hostname) | |
852 | return 0; | |
853 | ||
854 | hostname = link->network->dhcp_hostname; | |
855 | if (!hostname) | |
856 | (void) sd_dhcp_lease_get_hostname(link->dhcp_lease, &hostname); | |
857 | ||
858 | if (!hostname) | |
859 | return 0; | |
860 | ||
861 | /* If a hostname was set due to the lease, then unset it now. */ | |
862 | r = manager_set_hostname(link->manager, NULL); | |
863 | if (r < 0) | |
864 | return log_link_error_errno(link, r, "DHCP error: Failed to reset transient hostname: %m"); | |
865 | ||
866 | return 0; | |
867 | } | |
868 | ||
869 | int dhcp4_lease_lost(Link *link) { | |
870 | int r = 0; | |
871 | ||
872 | assert(link); | |
873 | assert(link->dhcp_lease); | |
874 | assert(link->network); | |
875 | ||
876 | log_link_info(link, "DHCP lease lost"); | |
877 | ||
878 | link->dhcp4_configured = false; | |
879 | ||
880 | if (link->network->dhcp_use_6rd && | |
881 | sd_dhcp_lease_has_6rd(link->dhcp_lease)) | |
882 | dhcp4_pd_prefix_lost(link); | |
883 | ||
884 | RET_GATHER(r, dhcp4_remove_address_and_routes(link, /* only_marked = */ false)); | |
885 | RET_GATHER(r, dhcp_reset_mtu(link)); | |
886 | RET_GATHER(r, dhcp_reset_hostname(link)); | |
887 | ||
888 | link->dhcp_lease = sd_dhcp_lease_unref(link->dhcp_lease); | |
889 | link_dirty(link); | |
890 | ||
891 | /* If one of the above failed. Do not request nexthops and routes. */ | |
892 | if (r < 0) | |
893 | return r; | |
894 | ||
895 | r = link_request_static_nexthops(link, true); | |
896 | if (r < 0) | |
897 | return r; | |
898 | ||
899 | return link_request_static_routes(link, true); | |
900 | } | |
901 | ||
902 | static int dhcp4_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Request *req, Link *link, Address *address) { | |
903 | int r; | |
904 | ||
905 | assert(link); | |
906 | assert(address); | |
907 | ||
908 | r = address_configure_handler_internal(m, link, address); | |
909 | if (r <= 0) | |
910 | return r; | |
911 | ||
912 | r = dhcp4_check_ready(link); | |
913 | if (r < 0) | |
914 | link_enter_failed(link); | |
915 | ||
916 | return 1; | |
917 | } | |
918 | ||
919 | static int dhcp4_request_address(Link *link, bool announce) { | |
920 | _cleanup_(address_unrefp) Address *addr = NULL; | |
921 | struct in_addr address, server; | |
922 | uint8_t prefixlen; | |
923 | Address *existing; | |
924 | usec_t lifetime_usec; | |
925 | int r; | |
926 | ||
927 | assert(link); | |
928 | assert(link->manager); | |
929 | assert(link->network); | |
930 | assert(link->dhcp_lease); | |
931 | ||
932 | r = sd_dhcp_lease_get_address(link->dhcp_lease, &address); | |
933 | if (r < 0) | |
934 | return log_link_warning_errno(link, r, "DHCP error: no address: %m"); | |
935 | ||
936 | r = sd_dhcp_lease_get_prefix(link->dhcp_lease, NULL, &prefixlen); | |
937 | if (r < 0) | |
938 | return log_link_warning_errno(link, r, "DHCP error: no netmask: %m"); | |
939 | ||
940 | r = sd_dhcp_lease_get_server_identifier(link->dhcp_lease, &server); | |
941 | if (r < 0) | |
942 | return log_link_debug_errno(link, r, "DHCP error: failed to get DHCP server IP address: %m"); | |
943 | ||
944 | if (!FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DYNAMIC)) { | |
945 | r = sd_dhcp_lease_get_lifetime_timestamp(link->dhcp_lease, CLOCK_BOOTTIME, &lifetime_usec); | |
946 | if (r < 0) | |
947 | return log_link_warning_errno(link, r, "DHCP error: failed to get lifetime: %m"); | |
948 | } else | |
949 | lifetime_usec = USEC_INFINITY; | |
950 | ||
951 | if (announce) { | |
952 | const struct in_addr *router; | |
953 | ||
954 | r = sd_dhcp_lease_get_router(link->dhcp_lease, &router); | |
955 | if (r < 0 && r != -ENODATA) | |
956 | return log_link_error_errno(link, r, "DHCP error: Could not get gateway: %m"); | |
957 | ||
958 | if (r > 0 && in4_addr_is_set(&router[0])) | |
959 | log_struct(LOG_INFO, | |
960 | LOG_LINK_INTERFACE(link), | |
961 | LOG_LINK_MESSAGE(link, "DHCPv4 address "IPV4_ADDRESS_FMT_STR"/%u, gateway "IPV4_ADDRESS_FMT_STR" acquired from "IPV4_ADDRESS_FMT_STR, | |
962 | IPV4_ADDRESS_FMT_VAL(address), | |
963 | prefixlen, | |
964 | IPV4_ADDRESS_FMT_VAL(router[0]), | |
965 | IPV4_ADDRESS_FMT_VAL(server)), | |
966 | LOG_ITEM("ADDRESS="IPV4_ADDRESS_FMT_STR, IPV4_ADDRESS_FMT_VAL(address)), | |
967 | LOG_ITEM("PREFIXLEN=%u", prefixlen), | |
968 | LOG_ITEM("GATEWAY="IPV4_ADDRESS_FMT_STR, IPV4_ADDRESS_FMT_VAL(router[0]))); | |
969 | else | |
970 | log_struct(LOG_INFO, | |
971 | LOG_LINK_INTERFACE(link), | |
972 | LOG_LINK_MESSAGE(link, "DHCPv4 address "IPV4_ADDRESS_FMT_STR"/%u acquired from "IPV4_ADDRESS_FMT_STR, | |
973 | IPV4_ADDRESS_FMT_VAL(address), | |
974 | prefixlen, | |
975 | IPV4_ADDRESS_FMT_VAL(server)), | |
976 | LOG_ITEM("ADDRESS="IPV4_ADDRESS_FMT_STR, IPV4_ADDRESS_FMT_VAL(address)), | |
977 | LOG_ITEM("PREFIXLEN=%u", prefixlen)); | |
978 | } | |
979 | ||
980 | r = address_new(&addr); | |
981 | if (r < 0) | |
982 | return log_oom(); | |
983 | ||
984 | addr->source = NETWORK_CONFIG_SOURCE_DHCP4; | |
985 | addr->provider.in = server; | |
986 | addr->family = AF_INET; | |
987 | addr->in_addr.in.s_addr = address.s_addr; | |
988 | addr->lifetime_preferred_usec = lifetime_usec; | |
989 | addr->lifetime_valid_usec = lifetime_usec; | |
990 | addr->prefixlen = prefixlen; | |
991 | r = sd_dhcp_lease_get_broadcast(link->dhcp_lease, &addr->broadcast); | |
992 | if (r < 0 && r != -ENODATA) | |
993 | return log_link_warning_errno(link, r, "DHCP: failed to get broadcast address: %m"); | |
994 | SET_FLAG(addr->flags, IFA_F_NOPREFIXROUTE, !prefixroute_by_kernel(link)); | |
995 | addr->route_metric = link->network->dhcp_route_metric; | |
996 | addr->duplicate_address_detection = link->network->dhcp_send_decline ? ADDRESS_FAMILY_IPV4 : ADDRESS_FAMILY_NO; | |
997 | ||
998 | r = free_and_strdup_warn(&addr->label, link->network->dhcp_label); | |
999 | if (r < 0) | |
1000 | return r; | |
1001 | ||
1002 | r = free_and_strdup_warn(&addr->netlabel, link->network->dhcp_netlabel); | |
1003 | if (r < 0) | |
1004 | return r; | |
1005 | ||
1006 | if (address_get(link, addr, &existing) < 0) /* The address is new. */ | |
1007 | link->dhcp4_configured = false; | |
1008 | else | |
1009 | address_unmark(existing); | |
1010 | ||
1011 | r = link_request_address(link, addr, &link->dhcp4_messages, | |
1012 | dhcp4_address_handler, NULL); | |
1013 | if (r < 0) | |
1014 | return log_link_error_errno(link, r, "Failed to request DHCPv4 address: %m"); | |
1015 | ||
1016 | return 0; | |
1017 | } | |
1018 | ||
1019 | static int dhcp4_request_address_and_routes(Link *link, bool announce) { | |
1020 | int r; | |
1021 | ||
1022 | assert(link); | |
1023 | ||
1024 | link_mark_addresses(link, NETWORK_CONFIG_SOURCE_DHCP4); | |
1025 | manager_mark_routes(link->manager, link, NETWORK_CONFIG_SOURCE_DHCP4); | |
1026 | ||
1027 | r = dhcp4_request_address(link, announce); | |
1028 | if (r < 0) | |
1029 | return r; | |
1030 | ||
1031 | r = dhcp4_request_routes(link); | |
1032 | if (r < 0) | |
1033 | return r; | |
1034 | ||
1035 | if (!link->dhcp4_configured) { | |
1036 | link_set_state(link, LINK_STATE_CONFIGURING); | |
1037 | link_check_ready(link); | |
1038 | } | |
1039 | ||
1040 | return 0; | |
1041 | } | |
1042 | ||
1043 | static int dhcp_lease_renew(sd_dhcp_client *client, Link *link) { | |
1044 | _cleanup_(sd_dhcp_lease_unrefp) sd_dhcp_lease *old_lease = NULL; | |
1045 | sd_dhcp_lease *lease; | |
1046 | int r; | |
1047 | ||
1048 | assert(link); | |
1049 | assert(link->network); | |
1050 | assert(client); | |
1051 | ||
1052 | r = sd_dhcp_client_get_lease(client, &lease); | |
1053 | if (r < 0) | |
1054 | return log_link_warning_errno(link, r, "DHCP error: no lease: %m"); | |
1055 | ||
1056 | old_lease = TAKE_PTR(link->dhcp_lease); | |
1057 | link->dhcp_lease = sd_dhcp_lease_ref(lease); | |
1058 | link_dirty(link); | |
1059 | ||
1060 | if (link->network->dhcp_use_6rd) { | |
1061 | if (sd_dhcp_lease_has_6rd(link->dhcp_lease)) { | |
1062 | r = dhcp4_pd_prefix_acquired(link); | |
1063 | if (r < 0) | |
1064 | return log_link_warning_errno(link, r, "Failed to process 6rd option: %m"); | |
1065 | } else if (sd_dhcp_lease_has_6rd(old_lease)) | |
1066 | dhcp4_pd_prefix_lost(link); | |
1067 | } | |
1068 | ||
1069 | return dhcp4_request_address_and_routes(link, false); | |
1070 | } | |
1071 | ||
1072 | static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) { | |
1073 | sd_dhcp_lease *lease; | |
1074 | int r; | |
1075 | ||
1076 | assert(client); | |
1077 | assert(link); | |
1078 | ||
1079 | r = sd_dhcp_client_get_lease(client, &lease); | |
1080 | if (r < 0) | |
1081 | return log_link_error_errno(link, r, "DHCP error: No lease: %m"); | |
1082 | ||
1083 | sd_dhcp_lease_unref(link->dhcp_lease); | |
1084 | link->dhcp_lease = sd_dhcp_lease_ref(lease); | |
1085 | link_dirty(link); | |
1086 | ||
1087 | if (link->network->dhcp_use_mtu) { | |
1088 | uint16_t mtu; | |
1089 | ||
1090 | r = sd_dhcp_lease_get_mtu(lease, &mtu); | |
1091 | if (r >= 0) { | |
1092 | r = link_request_to_set_mtu(link, mtu); | |
1093 | if (r < 0) | |
1094 | log_link_error_errno(link, r, "Failed to set MTU to %" PRIu16 ": %m", mtu); | |
1095 | } | |
1096 | } | |
1097 | ||
1098 | if (link->network->dhcp_use_hostname) { | |
1099 | const char *dhcpname = NULL; | |
1100 | _cleanup_free_ char *hostname = NULL; | |
1101 | ||
1102 | if (link->network->dhcp_hostname) | |
1103 | dhcpname = link->network->dhcp_hostname; | |
1104 | else | |
1105 | (void) sd_dhcp_lease_get_hostname(lease, &dhcpname); | |
1106 | ||
1107 | if (dhcpname) { | |
1108 | r = shorten_overlong(dhcpname, &hostname); | |
1109 | if (r < 0) | |
1110 | log_link_warning_errno(link, r, "Unable to shorten overlong DHCP hostname '%s', ignoring: %m", dhcpname); | |
1111 | if (r == 1) | |
1112 | log_link_notice(link, "Overlong DHCP hostname received, shortened from '%s' to '%s'", dhcpname, hostname); | |
1113 | } | |
1114 | ||
1115 | if (hostname) { | |
1116 | r = manager_set_hostname(link->manager, hostname); | |
1117 | if (r < 0) | |
1118 | log_link_error_errno(link, r, "Failed to set transient hostname to '%s': %m", hostname); | |
1119 | } | |
1120 | } | |
1121 | ||
1122 | if (link->network->dhcp_use_timezone) { | |
1123 | const char *tz = NULL; | |
1124 | ||
1125 | (void) sd_dhcp_lease_get_timezone(link->dhcp_lease, &tz); | |
1126 | ||
1127 | if (tz) { | |
1128 | r = manager_set_timezone(link->manager, tz); | |
1129 | if (r < 0) | |
1130 | log_link_error_errno(link, r, "Failed to set timezone to '%s': %m", tz); | |
1131 | } | |
1132 | } | |
1133 | ||
1134 | if (link->network->dhcp_use_6rd && | |
1135 | sd_dhcp_lease_has_6rd(link->dhcp_lease)) { | |
1136 | r = dhcp4_pd_prefix_acquired(link); | |
1137 | if (r < 0) | |
1138 | return log_link_warning_errno(link, r, "Failed to process 6rd option: %m"); | |
1139 | } | |
1140 | ||
1141 | return dhcp4_request_address_and_routes(link, true); | |
1142 | } | |
1143 | ||
1144 | static int dhcp_lease_ip_change(sd_dhcp_client *client, Link *link) { | |
1145 | int r; | |
1146 | ||
1147 | r = dhcp_lease_acquired(client, link); | |
1148 | if (r < 0) | |
1149 | (void) dhcp4_lease_lost(link); | |
1150 | ||
1151 | return r; | |
1152 | } | |
1153 | ||
1154 | static int dhcp_server_is_filtered(Link *link, sd_dhcp_client *client) { | |
1155 | sd_dhcp_lease *lease; | |
1156 | struct in_addr addr; | |
1157 | int r; | |
1158 | ||
1159 | assert(link); | |
1160 | assert(link->network); | |
1161 | assert(client); | |
1162 | ||
1163 | r = sd_dhcp_client_get_lease(client, &lease); | |
1164 | if (r < 0) | |
1165 | return log_link_error_errno(link, r, "Failed to get DHCP lease: %m"); | |
1166 | ||
1167 | r = sd_dhcp_lease_get_server_identifier(lease, &addr); | |
1168 | if (r < 0) | |
1169 | return log_link_debug_errno(link, r, "Failed to get DHCP server IP address: %m"); | |
1170 | ||
1171 | if (in4_address_is_filtered(&addr, link->network->dhcp_allow_listed_ip, link->network->dhcp_deny_listed_ip)) { | |
1172 | if (link->network->dhcp_allow_listed_ip) | |
1173 | log_link_debug(link, "DHCPv4 server IP address "IPV4_ADDRESS_FMT_STR" not found in allow-list, ignoring offer.", | |
1174 | IPV4_ADDRESS_FMT_VAL(addr)); | |
1175 | else | |
1176 | log_link_debug(link, "DHCPv4 server IP address "IPV4_ADDRESS_FMT_STR" found in deny-list, ignoring offer.", | |
1177 | IPV4_ADDRESS_FMT_VAL(addr)); | |
1178 | ||
1179 | return true; | |
1180 | } | |
1181 | ||
1182 | return false; | |
1183 | } | |
1184 | ||
1185 | static int dhcp4_handler(sd_dhcp_client *client, int event, void *userdata) { | |
1186 | Link *link = ASSERT_PTR(userdata); | |
1187 | int r; | |
1188 | ||
1189 | assert(link->network); | |
1190 | assert(link->manager); | |
1191 | ||
1192 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
1193 | return 0; | |
1194 | ||
1195 | switch (event) { | |
1196 | case SD_DHCP_CLIENT_EVENT_STOP: | |
1197 | if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DYNAMIC)) { | |
1198 | log_link_notice(link, "DHCPv4 connection considered critical, ignoring request to reconfigure it."); | |
1199 | return 0; | |
1200 | } | |
1201 | ||
1202 | r = ipv4ll_start(link); | |
1203 | if (r < 0) | |
1204 | return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m"); | |
1205 | if (r > 0) | |
1206 | log_link_debug(link, "DHCP client is stopped. Acquiring IPv4 link-local address."); | |
1207 | ||
1208 | if (link->dhcp_lease) { | |
1209 | if (link->network->dhcp_send_release) { | |
1210 | r = sd_dhcp_client_send_release(client); | |
1211 | if (r < 0) | |
1212 | log_link_full_errno(link, | |
1213 | ERRNO_IS_DISCONNECT(r) ? LOG_DEBUG : LOG_WARNING, | |
1214 | r, "Failed to send DHCP RELEASE, ignoring: %m"); | |
1215 | } | |
1216 | ||
1217 | r = dhcp4_lease_lost(link); | |
1218 | if (r < 0) { | |
1219 | link_enter_failed(link); | |
1220 | return r; | |
1221 | } | |
1222 | } | |
1223 | ||
1224 | break; | |
1225 | case SD_DHCP_CLIENT_EVENT_EXPIRED: | |
1226 | if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DYNAMIC)) { | |
1227 | log_link_notice(link, "DHCPv4 connection considered critical, ignoring request to reconfigure it."); | |
1228 | return 0; | |
1229 | } | |
1230 | ||
1231 | if (link->dhcp_lease) { | |
1232 | r = dhcp4_lease_lost(link); | |
1233 | if (r < 0) { | |
1234 | link_enter_failed(link); | |
1235 | return r; | |
1236 | } | |
1237 | } | |
1238 | ||
1239 | break; | |
1240 | case SD_DHCP_CLIENT_EVENT_IP_CHANGE: | |
1241 | if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DYNAMIC)) { | |
1242 | log_link_notice(link, "DHCPv4 connection considered critical, ignoring request to reconfigure it."); | |
1243 | return 0; | |
1244 | } | |
1245 | ||
1246 | r = dhcp_lease_ip_change(client, link); | |
1247 | if (r < 0) { | |
1248 | link_enter_failed(link); | |
1249 | return r; | |
1250 | } | |
1251 | ||
1252 | break; | |
1253 | case SD_DHCP_CLIENT_EVENT_RENEW: | |
1254 | r = dhcp_lease_renew(client, link); | |
1255 | if (r < 0) { | |
1256 | link_enter_failed(link); | |
1257 | return r; | |
1258 | } | |
1259 | break; | |
1260 | case SD_DHCP_CLIENT_EVENT_IP_ACQUIRE: | |
1261 | r = dhcp_lease_acquired(client, link); | |
1262 | if (r < 0) { | |
1263 | link_enter_failed(link); | |
1264 | return r; | |
1265 | } | |
1266 | break; | |
1267 | case SD_DHCP_CLIENT_EVENT_SELECTING: | |
1268 | r = dhcp_server_is_filtered(link, client); | |
1269 | if (r < 0) { | |
1270 | link_enter_failed(link); | |
1271 | return r; | |
1272 | } | |
1273 | if (r > 0) | |
1274 | return -ENOMSG; | |
1275 | break; | |
1276 | ||
1277 | case SD_DHCP_CLIENT_EVENT_TRANSIENT_FAILURE: | |
1278 | r = ipv4ll_start(link); | |
1279 | if (r < 0) | |
1280 | return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m"); | |
1281 | if (r > 0) | |
1282 | log_link_debug(link, "Problems acquiring DHCP lease, acquiring IPv4 link-local address"); | |
1283 | break; | |
1284 | ||
1285 | default: | |
1286 | if (event < 0) | |
1287 | log_link_warning_errno(link, event, "DHCP error: Client failed: %m"); | |
1288 | else | |
1289 | log_link_warning(link, "DHCP unknown event: %i", event); | |
1290 | } | |
1291 | ||
1292 | return 0; | |
1293 | } | |
1294 | ||
1295 | static int dhcp4_set_hostname(Link *link) { | |
1296 | _cleanup_free_ char *hostname = NULL; | |
1297 | const char *hn; | |
1298 | int r; | |
1299 | ||
1300 | assert(link); | |
1301 | ||
1302 | if (!link->network->dhcp_send_hostname) | |
1303 | hn = NULL; | |
1304 | else if (link->network->dhcp_hostname) | |
1305 | hn = link->network->dhcp_hostname; | |
1306 | else { | |
1307 | r = gethostname_strict(&hostname); | |
1308 | if (r < 0 && r != -ENXIO) /* ENXIO: no hostname set or hostname is "localhost" */ | |
1309 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to get hostname: %m"); | |
1310 | ||
1311 | hn = hostname; | |
1312 | } | |
1313 | ||
1314 | r = sd_dhcp_client_set_hostname(link->dhcp_client, hn); | |
1315 | if (r == -EINVAL && hostname) | |
1316 | /* Ignore error when the machine's hostname is not suitable to send in DHCP packet. */ | |
1317 | log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set hostname from kernel hostname, ignoring: %m"); | |
1318 | else if (r < 0) | |
1319 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set hostname: %m"); | |
1320 | ||
1321 | return 0; | |
1322 | } | |
1323 | ||
1324 | static int dhcp4_set_client_identifier(Link *link) { | |
1325 | int r; | |
1326 | ||
1327 | assert(link); | |
1328 | assert(link->network); | |
1329 | assert(link->dhcp_client); | |
1330 | ||
1331 | switch (link->network->dhcp_client_identifier) { | |
1332 | case DHCP_CLIENT_ID_DUID: { | |
1333 | /* If configured, apply user specified DUID and IAID */ | |
1334 | const DUID *duid = link_get_dhcp4_duid(link); | |
1335 | ||
1336 | if (duid->raw_data_len == 0) | |
1337 | switch (duid->type) { | |
1338 | case DUID_TYPE_LLT: | |
1339 | r = sd_dhcp_client_set_iaid_duid_llt(link->dhcp_client, | |
1340 | link->network->dhcp_iaid_set, | |
1341 | link->network->dhcp_iaid, | |
1342 | duid->llt_time); | |
1343 | break; | |
1344 | case DUID_TYPE_LL: | |
1345 | r = sd_dhcp_client_set_iaid_duid_ll(link->dhcp_client, | |
1346 | link->network->dhcp_iaid_set, | |
1347 | link->network->dhcp_iaid); | |
1348 | break; | |
1349 | case DUID_TYPE_EN: | |
1350 | r = sd_dhcp_client_set_iaid_duid_en(link->dhcp_client, | |
1351 | link->network->dhcp_iaid_set, | |
1352 | link->network->dhcp_iaid); | |
1353 | break; | |
1354 | case DUID_TYPE_UUID: | |
1355 | r = sd_dhcp_client_set_iaid_duid_uuid(link->dhcp_client, | |
1356 | link->network->dhcp_iaid_set, | |
1357 | link->network->dhcp_iaid); | |
1358 | break; | |
1359 | default: | |
1360 | r = sd_dhcp_client_set_iaid_duid_raw(link->dhcp_client, | |
1361 | link->network->dhcp_iaid_set, | |
1362 | link->network->dhcp_iaid, | |
1363 | duid->type, NULL, 0); | |
1364 | } | |
1365 | else | |
1366 | r = sd_dhcp_client_set_iaid_duid_raw(link->dhcp_client, | |
1367 | link->network->dhcp_iaid_set, | |
1368 | link->network->dhcp_iaid, | |
1369 | duid->type, duid->raw_data, duid->raw_data_len); | |
1370 | if (r < 0) | |
1371 | return r; | |
1372 | break; | |
1373 | } | |
1374 | case DHCP_CLIENT_ID_MAC: { | |
1375 | const uint8_t *hw_addr = link->hw_addr.bytes; | |
1376 | size_t hw_addr_len = link->hw_addr.length; | |
1377 | ||
1378 | if (link->iftype == ARPHRD_INFINIBAND && hw_addr_len == INFINIBAND_ALEN) { | |
1379 | /* set_client_id expects only last 8 bytes of an IB address */ | |
1380 | hw_addr += INFINIBAND_ALEN - 8; | |
1381 | hw_addr_len -= INFINIBAND_ALEN - 8; | |
1382 | } | |
1383 | ||
1384 | r = sd_dhcp_client_set_client_id(link->dhcp_client, | |
1385 | link->iftype, | |
1386 | hw_addr, | |
1387 | hw_addr_len); | |
1388 | if (r < 0) | |
1389 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set client ID: %m"); | |
1390 | break; | |
1391 | } | |
1392 | default: | |
1393 | assert_not_reached(); | |
1394 | } | |
1395 | ||
1396 | return 0; | |
1397 | } | |
1398 | ||
1399 | static int dhcp4_set_request_address(Link *link) { | |
1400 | assert(link); | |
1401 | assert(link->network); | |
1402 | assert(link->dhcp_client); | |
1403 | ||
1404 | /* 1. Use already assigned address. */ | |
1405 | Address *a; | |
1406 | SET_FOREACH(a, link->addresses) { | |
1407 | if (a->source != NETWORK_CONFIG_SOURCE_DHCP4) | |
1408 | continue; | |
1409 | ||
1410 | assert(a->family == AF_INET); | |
1411 | ||
1412 | log_link_debug(link, "DHCPv4 CLIENT: requesting previously acquired address %s.", | |
1413 | IN4_ADDR_TO_STRING(&a->in_addr.in)); | |
1414 | return sd_dhcp_client_set_request_address(link->dhcp_client, &a->in_addr.in); | |
1415 | } | |
1416 | ||
1417 | /* 2. If no address is assigned yet, use explicitly configured address. */ | |
1418 | if (in4_addr_is_set(&link->network->dhcp_request_address)) { | |
1419 | log_link_debug(link, "DHCPv4 CLIENT: requesting address %s specified by RequestAddress=.", | |
1420 | IN4_ADDR_TO_STRING(&link->network->dhcp_request_address)); | |
1421 | return sd_dhcp_client_set_request_address(link->dhcp_client, &link->network->dhcp_request_address); | |
1422 | } | |
1423 | ||
1424 | /* 3. If KeepConfiguration=dynamic, use a foreign dynamic address. */ | |
1425 | if (!FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DYNAMIC)) | |
1426 | return 0; | |
1427 | ||
1428 | SET_FOREACH(a, link->addresses) { | |
1429 | if (a->source != NETWORK_CONFIG_SOURCE_FOREIGN) | |
1430 | continue; | |
1431 | if (a->family != AF_INET) | |
1432 | continue; | |
1433 | if (!link_address_is_dynamic(link, a)) | |
1434 | continue; | |
1435 | ||
1436 | log_link_debug(link, "DHCPv4 CLIENT: requesting foreign dynamic address %s.", | |
1437 | IN4_ADDR_TO_STRING(&a->in_addr.in)); | |
1438 | return sd_dhcp_client_set_request_address(link->dhcp_client, &a->in_addr.in); | |
1439 | } | |
1440 | ||
1441 | return 0; | |
1442 | } | |
1443 | ||
1444 | static bool link_needs_dhcp_broadcast(Link *link) { | |
1445 | int r; | |
1446 | ||
1447 | assert(link); | |
1448 | assert(link->network); | |
1449 | ||
1450 | /* Return the setting in DHCP[4].RequestBroadcast if specified. Otherwise return the device property | |
1451 | * ID_NET_DHCP_BROADCAST setting, which may be set for interfaces requiring that the DHCPOFFER | |
1452 | * message is being broadcast because they can't handle unicast messages while not fully configured. | |
1453 | * If neither is set or a failure occurs, return false, which is the default for this flag. */ | |
1454 | ||
1455 | r = link->network->dhcp_broadcast; | |
1456 | if (r >= 0) | |
1457 | return r; | |
1458 | ||
1459 | if (!link->dev) | |
1460 | return false; | |
1461 | ||
1462 | r = device_get_property_bool(link->dev, "ID_NET_DHCP_BROADCAST"); | |
1463 | if (r < 0) { | |
1464 | if (r != -ENOENT) | |
1465 | log_link_warning_errno(link, r, "DHCPv4 CLIENT: Failed to get or parse ID_NET_DHCP_BROADCAST, ignoring: %m"); | |
1466 | ||
1467 | return false; | |
1468 | } | |
1469 | ||
1470 | log_link_debug(link, "DHCPv4 CLIENT: Detected ID_NET_DHCP_BROADCAST='%d'.", r); | |
1471 | return r; | |
1472 | } | |
1473 | ||
1474 | static bool link_dhcp4_ipv6_only_mode(Link *link) { | |
1475 | assert(link); | |
1476 | assert(link->network); | |
1477 | ||
1478 | /* If it is explicitly specified, then honor the setting. */ | |
1479 | if (link->network->dhcp_ipv6_only_mode >= 0) | |
1480 | return link->network->dhcp_ipv6_only_mode; | |
1481 | ||
1482 | /* Defaults to false, until we support 464XLAT. See issue #30891. */ | |
1483 | return false; | |
1484 | } | |
1485 | ||
1486 | static int dhcp4_configure(Link *link) { | |
1487 | sd_dhcp_option *send_option; | |
1488 | void *request_options; | |
1489 | int r; | |
1490 | ||
1491 | assert(link); | |
1492 | assert(link->network); | |
1493 | ||
1494 | if (link->dhcp_client) | |
1495 | return log_link_debug_errno(link, SYNTHETIC_ERRNO(EBUSY), "DHCPv4 client is already configured."); | |
1496 | ||
1497 | r = sd_dhcp_client_new(&link->dhcp_client, link->network->dhcp_anonymize); | |
1498 | if (r < 0) | |
1499 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to allocate DHCPv4 client: %m"); | |
1500 | ||
1501 | r = sd_dhcp_client_set_bootp(link->dhcp_client, link->network->dhcp_use_bootp); | |
1502 | if (r < 0) | |
1503 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to %s BOOTP: %m", | |
1504 | enable_disable(link->network->dhcp_use_bootp)); | |
1505 | ||
1506 | r = sd_dhcp_client_attach_event(link->dhcp_client, link->manager->event, 0); | |
1507 | if (r < 0) | |
1508 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to attach event to DHCPv4 client: %m"); | |
1509 | ||
1510 | r = sd_dhcp_client_attach_device(link->dhcp_client, link->dev); | |
1511 | if (r < 0) | |
1512 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to attach device: %m"); | |
1513 | ||
1514 | r = sd_dhcp_client_set_rapid_commit(link->dhcp_client, link->network->dhcp_use_rapid_commit); | |
1515 | if (r < 0) | |
1516 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set rapid commit: %m"); | |
1517 | ||
1518 | r = sd_dhcp_client_set_mac(link->dhcp_client, | |
1519 | link->hw_addr.bytes, | |
1520 | link->bcast_addr.length > 0 ? link->bcast_addr.bytes : NULL, | |
1521 | link->hw_addr.length, link->iftype); | |
1522 | if (r < 0) | |
1523 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set MAC address: %m"); | |
1524 | ||
1525 | r = sd_dhcp_client_set_ifindex(link->dhcp_client, link->ifindex); | |
1526 | if (r < 0) | |
1527 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set ifindex: %m"); | |
1528 | ||
1529 | r = sd_dhcp_client_set_callback(link->dhcp_client, dhcp4_handler, link); | |
1530 | if (r < 0) | |
1531 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set callback: %m"); | |
1532 | ||
1533 | r = sd_dhcp_client_set_request_broadcast(link->dhcp_client, link_needs_dhcp_broadcast(link)); | |
1534 | if (r < 0) | |
1535 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for broadcast: %m"); | |
1536 | ||
1537 | r = dhcp_client_set_state_callback(link->dhcp_client, dhcp_client_callback_bus, link); | |
1538 | if (r < 0) | |
1539 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set state change callback: %m"); | |
1540 | ||
1541 | if (link->mtu > 0) { | |
1542 | r = sd_dhcp_client_set_mtu(link->dhcp_client, link->mtu); | |
1543 | if (r < 0) | |
1544 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set MTU: %m"); | |
1545 | } | |
1546 | ||
1547 | if (!link->network->dhcp_anonymize) { | |
1548 | r = dhcp4_set_request_address(link); | |
1549 | if (r < 0) | |
1550 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set initial DHCPv4 address: %m"); | |
1551 | ||
1552 | if (link->network->dhcp_use_mtu) { | |
1553 | r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_MTU_INTERFACE); | |
1554 | if (r < 0) | |
1555 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for MTU: %m"); | |
1556 | } | |
1557 | ||
1558 | if (link->network->dhcp_use_routes) { | |
1559 | r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_STATIC_ROUTE); | |
1560 | if (r < 0) | |
1561 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for static route: %m"); | |
1562 | ||
1563 | r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE); | |
1564 | if (r < 0) | |
1565 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for classless static route: %m"); | |
1566 | } | |
1567 | ||
1568 | if (link_get_use_domains(link, NETWORK_CONFIG_SOURCE_DHCP4) > 0) { | |
1569 | r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_DOMAIN_SEARCH); | |
1570 | if (r < 0) | |
1571 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for domain search list: %m"); | |
1572 | } | |
1573 | ||
1574 | if (link_get_use_ntp(link, NETWORK_CONFIG_SOURCE_DHCP4)) { | |
1575 | r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_NTP_SERVER); | |
1576 | if (r < 0) | |
1577 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for NTP server: %m"); | |
1578 | } | |
1579 | ||
1580 | if (link->network->dhcp_use_sip) { | |
1581 | r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_SIP_SERVER); | |
1582 | if (r < 0) | |
1583 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for SIP server: %m"); | |
1584 | } | |
1585 | ||
1586 | if (link_get_use_dnr(link, NETWORK_CONFIG_SOURCE_DHCP4)) { | |
1587 | r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_V4_DNR); | |
1588 | if (r < 0) | |
1589 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for DNR: %m"); | |
1590 | } | |
1591 | ||
1592 | if (link->network->dhcp_use_captive_portal) { | |
1593 | r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_DHCP_CAPTIVE_PORTAL); | |
1594 | if (r < 0) | |
1595 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for captive portal: %m"); | |
1596 | } | |
1597 | ||
1598 | if (link->network->dhcp_use_timezone) { | |
1599 | r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_TZDB_TIMEZONE); | |
1600 | if (r < 0) | |
1601 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for timezone: %m"); | |
1602 | } | |
1603 | ||
1604 | if (link->network->dhcp_use_6rd) { | |
1605 | r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_6RD); | |
1606 | if (r < 0) | |
1607 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for 6rd: %m"); | |
1608 | } | |
1609 | ||
1610 | if (link_dhcp4_ipv6_only_mode(link)) { | |
1611 | r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_IPV6_ONLY_PREFERRED); | |
1612 | if (r < 0) | |
1613 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for IPv6-only preferred option: %m"); | |
1614 | } | |
1615 | ||
1616 | SET_FOREACH(request_options, link->network->dhcp_request_options) { | |
1617 | uint32_t option = PTR_TO_UINT32(request_options); | |
1618 | ||
1619 | r = sd_dhcp_client_set_request_option(link->dhcp_client, option); | |
1620 | if (r < 0) | |
1621 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for '%u': %m", option); | |
1622 | } | |
1623 | ||
1624 | ORDERED_HASHMAP_FOREACH(send_option, link->network->dhcp_client_send_options) { | |
1625 | r = sd_dhcp_client_add_option(link->dhcp_client, send_option); | |
1626 | if (r == -EEXIST) | |
1627 | continue; | |
1628 | if (r < 0) | |
1629 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set send option: %m"); | |
1630 | } | |
1631 | ||
1632 | ORDERED_HASHMAP_FOREACH(send_option, link->network->dhcp_client_send_vendor_options) { | |
1633 | r = sd_dhcp_client_add_vendor_option(link->dhcp_client, send_option); | |
1634 | if (r == -EEXIST) | |
1635 | continue; | |
1636 | if (r < 0) | |
1637 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set send option: %m"); | |
1638 | } | |
1639 | ||
1640 | r = dhcp4_set_hostname(link); | |
1641 | if (r < 0) | |
1642 | return r; | |
1643 | ||
1644 | if (link->network->dhcp_vendor_class_identifier) { | |
1645 | r = sd_dhcp_client_set_vendor_class_identifier(link->dhcp_client, | |
1646 | link->network->dhcp_vendor_class_identifier); | |
1647 | if (r < 0) | |
1648 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set vendor class identifier: %m"); | |
1649 | } | |
1650 | ||
1651 | if (link->network->dhcp_mudurl) { | |
1652 | r = sd_dhcp_client_set_mud_url(link->dhcp_client, link->network->dhcp_mudurl); | |
1653 | if (r < 0) | |
1654 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set MUD URL: %m"); | |
1655 | } | |
1656 | ||
1657 | if (link->network->dhcp_user_class) { | |
1658 | r = sd_dhcp_client_set_user_class(link->dhcp_client, link->network->dhcp_user_class); | |
1659 | if (r < 0) | |
1660 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set user class: %m"); | |
1661 | } | |
1662 | } | |
1663 | ||
1664 | if (link->network->dhcp_client_port > 0) { | |
1665 | r = sd_dhcp_client_set_client_port(link->dhcp_client, link->network->dhcp_client_port); | |
1666 | if (r < 0) | |
1667 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set listen port: %m"); | |
1668 | } | |
1669 | if (link->network->dhcp_port > 0) { | |
1670 | r = sd_dhcp_client_set_port(link->dhcp_client, link->network->dhcp_port); | |
1671 | if (r < 0) | |
1672 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set server port: %m"); | |
1673 | } | |
1674 | ||
1675 | if (link->network->dhcp_max_attempts > 0) { | |
1676 | r = sd_dhcp_client_set_max_attempts(link->dhcp_client, link->network->dhcp_max_attempts); | |
1677 | if (r < 0) | |
1678 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set max attempts: %m"); | |
1679 | } | |
1680 | ||
1681 | if (link->network->dhcp_ip_service_type >= 0) { | |
1682 | r = sd_dhcp_client_set_service_type(link->dhcp_client, link->network->dhcp_ip_service_type); | |
1683 | if (r < 0) | |
1684 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set IP service type: %m"); | |
1685 | } | |
1686 | ||
1687 | if (link->network->dhcp_socket_priority_set) { | |
1688 | r = sd_dhcp_client_set_socket_priority(link->dhcp_client, link->network->dhcp_socket_priority); | |
1689 | if (r < 0) | |
1690 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set socket priority: %m"); | |
1691 | } | |
1692 | ||
1693 | if (link->network->dhcp_fallback_lease_lifetime_usec > 0) { | |
1694 | r = sd_dhcp_client_set_fallback_lease_lifetime(link->dhcp_client, link->network->dhcp_fallback_lease_lifetime_usec); | |
1695 | if (r < 0) | |
1696 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed set to lease lifetime: %m"); | |
1697 | } | |
1698 | ||
1699 | return dhcp4_set_client_identifier(link); | |
1700 | } | |
1701 | ||
1702 | int dhcp4_update_mac(Link *link) { | |
1703 | bool restart; | |
1704 | int r; | |
1705 | ||
1706 | assert(link); | |
1707 | ||
1708 | if (!link->dhcp_client) | |
1709 | return 0; | |
1710 | ||
1711 | restart = sd_dhcp_client_is_running(link->dhcp_client); | |
1712 | ||
1713 | r = sd_dhcp_client_stop(link->dhcp_client); | |
1714 | if (r < 0) | |
1715 | return r; | |
1716 | ||
1717 | r = sd_dhcp_client_set_mac(link->dhcp_client, | |
1718 | link->hw_addr.bytes, | |
1719 | link->bcast_addr.length > 0 ? link->bcast_addr.bytes : NULL, | |
1720 | link->hw_addr.length, link->iftype); | |
1721 | if (r < 0) | |
1722 | return r; | |
1723 | ||
1724 | r = dhcp4_set_client_identifier(link); | |
1725 | if (r < 0) | |
1726 | return r; | |
1727 | ||
1728 | if (restart) { | |
1729 | r = dhcp4_start(link); | |
1730 | if (r < 0) | |
1731 | return r; | |
1732 | } | |
1733 | ||
1734 | return 0; | |
1735 | } | |
1736 | ||
1737 | int dhcp4_update_ipv6_connectivity(Link *link) { | |
1738 | assert(link); | |
1739 | ||
1740 | if (!link->network) | |
1741 | return 0; | |
1742 | ||
1743 | if (!link->network->dhcp_ipv6_only_mode) | |
1744 | return 0; | |
1745 | ||
1746 | if (!link->dhcp_client) | |
1747 | return 0; | |
1748 | ||
1749 | /* If the client is running, set the current connectivity. */ | |
1750 | if (sd_dhcp_client_is_running(link->dhcp_client)) | |
1751 | return sd_dhcp_client_set_ipv6_connectivity(link->dhcp_client, link_has_ipv6_connectivity(link)); | |
1752 | ||
1753 | /* If the client has been already stopped or not started yet, let's check the current connectivity | |
1754 | * and start the client if necessary. */ | |
1755 | if (link_has_ipv6_connectivity(link)) | |
1756 | return 0; | |
1757 | ||
1758 | return dhcp4_start_full(link, /* set_ipv6_connectivity = */ false); | |
1759 | } | |
1760 | ||
1761 | int dhcp4_start_full(Link *link, bool set_ipv6_connectivity) { | |
1762 | int r; | |
1763 | ||
1764 | assert(link); | |
1765 | assert(link->network); | |
1766 | ||
1767 | if (!link->dhcp_client) | |
1768 | return 0; | |
1769 | ||
1770 | if (!link_has_carrier(link)) | |
1771 | return 0; | |
1772 | ||
1773 | if (sd_dhcp_client_is_running(link->dhcp_client) > 0) | |
1774 | return 0; | |
1775 | ||
1776 | r = sd_dhcp_client_start(link->dhcp_client); | |
1777 | if (r < 0) | |
1778 | return r; | |
1779 | ||
1780 | if (set_ipv6_connectivity) { | |
1781 | r = dhcp4_update_ipv6_connectivity(link); | |
1782 | if (r < 0) | |
1783 | return r; | |
1784 | } | |
1785 | ||
1786 | return 1; | |
1787 | } | |
1788 | ||
1789 | int dhcp4_renew(Link *link) { | |
1790 | assert(link); | |
1791 | ||
1792 | if (!link->dhcp_client) | |
1793 | return 0; | |
1794 | ||
1795 | /* The DHCPv4 client may have been stopped by the IPv6 only mode. Let's unconditionally restart the | |
1796 | * client if it is not running. */ | |
1797 | if (!sd_dhcp_client_is_running(link->dhcp_client)) | |
1798 | return dhcp4_start(link); | |
1799 | ||
1800 | /* The client may be waiting for IPv6 connectivity. Let's restart the client in that case. */ | |
1801 | if (dhcp_client_get_state(link->dhcp_client) != DHCP_STATE_BOUND) | |
1802 | return sd_dhcp_client_interrupt_ipv6_only_mode(link->dhcp_client); | |
1803 | ||
1804 | /* Otherwise, send a RENEW command. */ | |
1805 | return sd_dhcp_client_send_renew(link->dhcp_client); | |
1806 | } | |
1807 | ||
1808 | static int dhcp4_configure_duid(Link *link) { | |
1809 | assert(link); | |
1810 | assert(link->network); | |
1811 | ||
1812 | if (link->network->dhcp_client_identifier != DHCP_CLIENT_ID_DUID) | |
1813 | return 1; | |
1814 | ||
1815 | return dhcp_configure_duid(link, link_get_dhcp4_duid(link)); | |
1816 | } | |
1817 | ||
1818 | static int dhcp4_process_request(Request *req, Link *link, void *userdata) { | |
1819 | int r; | |
1820 | ||
1821 | assert(link); | |
1822 | ||
1823 | if (!link_is_ready_to_configure(link, /* allow_unmanaged = */ false)) | |
1824 | return 0; | |
1825 | ||
1826 | r = dhcp4_configure_duid(link); | |
1827 | if (r <= 0) | |
1828 | return r; | |
1829 | ||
1830 | r = dhcp4_configure(link); | |
1831 | if (r < 0) | |
1832 | return log_link_warning_errno(link, r, "Failed to configure DHCPv4 client: %m"); | |
1833 | ||
1834 | r = dhcp4_start(link); | |
1835 | if (r < 0) | |
1836 | return log_link_warning_errno(link, r, "Failed to start DHCPv4 client: %m"); | |
1837 | ||
1838 | log_link_debug(link, "DHCPv4 client is configured%s.", | |
1839 | r > 0 ? ", acquiring DHCPv4 lease" : ""); | |
1840 | return 1; | |
1841 | } | |
1842 | ||
1843 | int link_request_dhcp4_client(Link *link) { | |
1844 | int r; | |
1845 | ||
1846 | assert(link); | |
1847 | ||
1848 | if (!link_dhcp4_enabled(link)) | |
1849 | return 0; | |
1850 | ||
1851 | if (link->dhcp_client) | |
1852 | return 0; | |
1853 | ||
1854 | r = link_queue_request(link, REQUEST_TYPE_DHCP4_CLIENT, dhcp4_process_request, NULL); | |
1855 | if (r < 0) | |
1856 | return log_link_warning_errno(link, r, "Failed to request configuring of the DHCPv4 client: %m"); | |
1857 | ||
1858 | log_link_debug(link, "Requested configuring of the DHCPv4 client."); | |
1859 | return 0; | |
1860 | } | |
1861 | ||
1862 | int link_drop_dhcp4_config(Link *link, Network *network) { | |
1863 | int r, ret = 0; | |
1864 | ||
1865 | assert(link); | |
1866 | assert(link->network); | |
1867 | ||
1868 | if (link->network == network) | |
1869 | return 0; /* .network file is unchanged. It is not necessary to reconfigure the client. */ | |
1870 | ||
1871 | if (!link_dhcp4_enabled(link)) { | |
1872 | /* DHCP client is disabled. Stop the client if it is running and drop the lease. */ | |
1873 | ret = sd_dhcp_client_stop(link->dhcp_client); | |
1874 | ||
1875 | /* Also explicitly drop DHCPv4 address and routes. Why? This is for the case when the DHCPv4 | |
1876 | * client was enabled on the previous invocation of networkd, but when it is restarted, a new | |
1877 | * .network file may match to the interface, and DHCPv4 client may be disabled. In that case, | |
1878 | * the DHCPv4 client is not running, hence sd_dhcp_client_stop() in the above does nothing. */ | |
1879 | RET_GATHER(ret, dhcp4_remove_address_and_routes(link, /* only_marked = */ false)); | |
1880 | } | |
1881 | ||
1882 | if (link->dhcp_client && link->network->dhcp_use_bootp && | |
1883 | network && !network->dhcp_use_bootp && network->dhcp_send_release) { | |
1884 | /* If the client was enabled as a DHCP client, and is now enabled as a BOOTP client, release | |
1885 | * the previous lease. Note, this can be easily fail, e.g. when the interface is down. Hence, | |
1886 | * ignore any failures here. */ | |
1887 | r = sd_dhcp_client_send_release(link->dhcp_client); | |
1888 | if (r < 0) | |
1889 | log_link_full_errno(link, ERRNO_IS_DISCONNECT(r) ? LOG_DEBUG : LOG_WARNING, r, | |
1890 | "Failed to send DHCP RELEASE, ignoring: %m"); | |
1891 | } | |
1892 | ||
1893 | /* Even if the client is currently enabled and also enabled in the new .network file, detailed | |
1894 | * settings for the client may be different. Let's unref() the client. But do not unref() the lease. | |
1895 | * it will be unref()ed later when a new lease is acquired. */ | |
1896 | link->dhcp_client = sd_dhcp_client_unref(link->dhcp_client); | |
1897 | return ret; | |
1898 | } | |
1899 | ||
1900 | int config_parse_dhcp_max_attempts( | |
1901 | const char *unit, | |
1902 | const char *filename, | |
1903 | unsigned line, | |
1904 | const char *section, | |
1905 | unsigned section_line, | |
1906 | const char *lvalue, | |
1907 | int ltype, | |
1908 | const char *rvalue, | |
1909 | void *data, | |
1910 | void *userdata) { | |
1911 | ||
1912 | Network *network = ASSERT_PTR(data); | |
1913 | uint64_t a; | |
1914 | int r; | |
1915 | ||
1916 | assert(lvalue); | |
1917 | assert(rvalue); | |
1918 | ||
1919 | if (isempty(rvalue)) { | |
1920 | network->dhcp_max_attempts = 0; | |
1921 | return 0; | |
1922 | } | |
1923 | ||
1924 | if (streq(rvalue, "infinity")) { | |
1925 | network->dhcp_max_attempts = UINT64_MAX; | |
1926 | return 0; | |
1927 | } | |
1928 | ||
1929 | r = safe_atou64(rvalue, &a); | |
1930 | if (r < 0) { | |
1931 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
1932 | "Failed to parse DHCP maximum attempts, ignoring: %s", rvalue); | |
1933 | return 0; | |
1934 | } | |
1935 | ||
1936 | if (a == 0) { | |
1937 | log_syntax(unit, LOG_WARNING, filename, line, 0, | |
1938 | "%s= must be positive integer or 'infinity', ignoring: %s", lvalue, rvalue); | |
1939 | return 0; | |
1940 | } | |
1941 | ||
1942 | network->dhcp_max_attempts = a; | |
1943 | ||
1944 | return 0; | |
1945 | } | |
1946 | ||
1947 | int config_parse_dhcp_ip_service_type( | |
1948 | const char *unit, | |
1949 | const char *filename, | |
1950 | unsigned line, | |
1951 | const char *section, | |
1952 | unsigned section_line, | |
1953 | const char *lvalue, | |
1954 | int ltype, | |
1955 | const char *rvalue, | |
1956 | void *data, | |
1957 | void *userdata) { | |
1958 | ||
1959 | int *tos = ASSERT_PTR(data); | |
1960 | ||
1961 | assert(filename); | |
1962 | assert(lvalue); | |
1963 | assert(rvalue); | |
1964 | ||
1965 | if (isempty(rvalue)) | |
1966 | *tos = -1; /* use sd_dhcp_client's default (currently, CS6). */ | |
1967 | else if (streq(rvalue, "none")) | |
1968 | *tos = 0; | |
1969 | else if (streq(rvalue, "CS4")) | |
1970 | *tos = IPTOS_CLASS_CS4; | |
1971 | else if (streq(rvalue, "CS6")) | |
1972 | *tos = IPTOS_CLASS_CS6; | |
1973 | else | |
1974 | log_syntax(unit, LOG_WARNING, filename, line, 0, | |
1975 | "Failed to parse %s=, ignoring assignment: %s", lvalue, rvalue); | |
1976 | ||
1977 | return 0; | |
1978 | } | |
1979 | ||
1980 | int config_parse_dhcp_socket_priority( | |
1981 | const char *unit, | |
1982 | const char *filename, | |
1983 | unsigned line, | |
1984 | const char *section, | |
1985 | unsigned section_line, | |
1986 | const char *lvalue, | |
1987 | int ltype, | |
1988 | const char *rvalue, | |
1989 | void *data, | |
1990 | void *userdata) { | |
1991 | ||
1992 | Network *network = ASSERT_PTR(data); | |
1993 | int a, r; | |
1994 | ||
1995 | assert(lvalue); | |
1996 | assert(rvalue); | |
1997 | ||
1998 | if (isempty(rvalue)) { | |
1999 | network->dhcp_socket_priority_set = false; | |
2000 | return 0; | |
2001 | } | |
2002 | ||
2003 | r = safe_atoi(rvalue, &a); | |
2004 | if (r < 0) { | |
2005 | log_syntax(unit, LOG_WARNING, filename, line, r, | |
2006 | "Failed to parse socket priority, ignoring: %s", rvalue); | |
2007 | return 0; | |
2008 | } | |
2009 | ||
2010 | network->dhcp_socket_priority_set = true; | |
2011 | network->dhcp_socket_priority = a; | |
2012 | ||
2013 | return 0; | |
2014 | } | |
2015 | ||
2016 | int config_parse_dhcp_fallback_lease_lifetime( | |
2017 | const char *unit, | |
2018 | const char *filename, | |
2019 | unsigned line, | |
2020 | const char *section, | |
2021 | unsigned section_line, | |
2022 | const char *lvalue, | |
2023 | int ltype, | |
2024 | const char *rvalue, | |
2025 | void *data, | |
2026 | void *userdata) { | |
2027 | ||
2028 | Network *network = userdata; | |
2029 | ||
2030 | assert(filename); | |
2031 | assert(section); | |
2032 | assert(lvalue); | |
2033 | assert(rvalue); | |
2034 | assert(data); | |
2035 | ||
2036 | if (isempty(rvalue)) { | |
2037 | network->dhcp_fallback_lease_lifetime_usec = 0; | |
2038 | return 0; | |
2039 | } | |
2040 | ||
2041 | /* We accept only "forever" or "infinity". */ | |
2042 | if (!STR_IN_SET(rvalue, "forever", "infinity")) { | |
2043 | log_syntax(unit, LOG_WARNING, filename, line, 0, | |
2044 | "Invalid LeaseLifetime= value, ignoring: %s", rvalue); | |
2045 | return 0; | |
2046 | } | |
2047 | ||
2048 | network->dhcp_fallback_lease_lifetime_usec = USEC_INFINITY; | |
2049 | ||
2050 | return 0; | |
2051 | } | |
2052 | ||
2053 | int config_parse_dhcp_label( | |
2054 | const char *unit, | |
2055 | const char *filename, | |
2056 | unsigned line, | |
2057 | const char *section, | |
2058 | unsigned section_line, | |
2059 | const char *lvalue, | |
2060 | int ltype, | |
2061 | const char *rvalue, | |
2062 | void *data, | |
2063 | void *userdata) { | |
2064 | ||
2065 | char **label = ASSERT_PTR(data); | |
2066 | ||
2067 | assert(filename); | |
2068 | assert(lvalue); | |
2069 | assert(rvalue); | |
2070 | ||
2071 | if (isempty(rvalue)) { | |
2072 | *label = mfree(*label); | |
2073 | return 0; | |
2074 | } | |
2075 | ||
2076 | if (!address_label_valid(rvalue)) { | |
2077 | log_syntax(unit, LOG_WARNING, filename, line, 0, | |
2078 | "Address label is too long or invalid, ignoring assignment: %s", rvalue); | |
2079 | return 0; | |
2080 | } | |
2081 | ||
2082 | return free_and_strdup_warn(label, rvalue); | |
2083 | } | |
2084 | ||
2085 | static const char* const dhcp_client_identifier_table[_DHCP_CLIENT_ID_MAX] = { | |
2086 | [DHCP_CLIENT_ID_MAC] = "mac", | |
2087 | [DHCP_CLIENT_ID_DUID] = "duid", | |
2088 | }; | |
2089 | ||
2090 | DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(dhcp_client_identifier, DHCPClientIdentifier); | |
2091 | DEFINE_CONFIG_PARSE_ENUM(config_parse_dhcp_client_identifier, dhcp_client_identifier, DHCPClientIdentifier); |