]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
3c9b8860 | 2 | |
8f815e8b | 3 | #include <linux/if_arp.h> |
baa3fadf | 4 | #include <linux/rtnetlink.h> |
1cf40697 DDM |
5 | #include <netinet/in.h> |
6 | #include <netinet/ip.h> | |
baa3fadf DDM |
7 | #include <stdio.h> |
8 | ||
9 | #include "sd-dhcp-protocol.h" | |
10 | #include "sd-ipv4ll.h" | |
3c9b8860 | 11 | |
b5efdb8a | 12 | #include "alloc-util.h" |
baa3fadf | 13 | #include "conf-parser.h" |
fc956a39 | 14 | #include "device-private.h" |
cb29c156 | 15 | #include "dhcp-client-internal.h" |
baa3fadf | 16 | #include "errno-util.h" |
e2054217 | 17 | #include "hostname-setup.h" |
093e3533 | 18 | #include "networkd-address.h" |
e49bad01 | 19 | #include "networkd-dhcp-prefix-delegation.h" |
ca5ad760 | 20 | #include "networkd-dhcp4.h" |
1cf40697 | 21 | #include "networkd-dhcp4-bus.h" |
b5c83cd5 | 22 | #include "networkd-ipv4ll.h" |
23f53b99 TG |
23 | #include "networkd-link.h" |
24 | #include "networkd-manager.h" | |
25 | #include "networkd-network.h" | |
76c5a0f2 | 26 | #include "networkd-nexthop.h" |
d12fb2bc | 27 | #include "networkd-ntp.h" |
76c5a0f2 YW |
28 | #include "networkd-queue.h" |
29 | #include "networkd-route.h" | |
0fa8ee6c | 30 | #include "networkd-setlink.h" |
3b5a4fc6 | 31 | #include "networkd-state-file.h" |
1cf40697 | 32 | #include "parse-util.h" |
baa3fadf DDM |
33 | #include "set.h" |
34 | #include "socket-util.h" | |
ca5ad760 | 35 | #include "string-table.h" |
baa3fadf | 36 | #include "string-util.h" |
7e19cc54 | 37 | #include "strv.h" |
3c9b8860 | 38 | |
ae7ea5a7 YW |
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 | ||
f90635f1 YW |
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); | |
ae7ea5a7 | 57 | network->dhcp_client_identifier = DHCP_CLIENT_ID_MAC; |
ae7ea5a7 | 58 | } |
f90635f1 YW |
59 | |
60 | if (network->dhcp_client_identifier < 0) | |
7bbe9ad7 | 61 | network->dhcp_client_identifier = network->dhcp_anonymize ? DHCP_CLIENT_ID_MAC : network->manager->dhcp_client_identifier; |
cf289b14 YW |
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); | |
ae7ea5a7 YW |
69 | } |
70 | ||
0bc8b6e2 YW |
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 | ||
15aa7a5c YW |
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 | ||
d52c5a72 YW |
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 | ||
0ce86f5e YW |
153 | static int dhcp4_find_gateway_for_destination( |
154 | Link *link, | |
155 | const struct in_addr *destination, | |
156 | uint8_t prefixlength, | |
0ce86f5e YW |
157 | struct in_addr *ret) { |
158 | ||
159 | _cleanup_free_ sd_dhcp_route **routes = NULL; | |
160 | size_t n_routes = 0; | |
0f3116f0 | 161 | bool is_classless; |
0ce86f5e YW |
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 | |
0f3116f0 YW |
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. */ | |
0ce86f5e | 176 | |
0f3116f0 YW |
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. */ | |
0ce86f5e YW |
179 | r = dhcp4_prefix_covers(link, destination, prefixlength); |
180 | if (r < 0) | |
181 | return r; | |
0f3116f0 YW |
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 | } | |
0ce86f5e YW |
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 | ||
0f3116f0 | 224 | /* The destination is reachable. Note, the gateway address returned here may be NULL. */ |
0ce86f5e | 225 | if (max_prefixlen != UINT8_MAX) { |
0f3116f0 | 226 | *ret = gw; |
0ce86f5e YW |
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) { | |
0f3116f0 YW |
233 | /* No matching static route is found, and the destination is not in the acquired network, |
234 | * falling back to the Router option. */ | |
0ce86f5e YW |
235 | r = dhcp4_get_router(link, ret); |
236 | if (r >= 0) | |
237 | return 0; | |
238 | if (r != -ENODATA) | |
239 | return r; | |
240 | } | |
241 | ||
0f3116f0 | 242 | return -EHOSTUNREACH; /* Cannot reach the destination. */ |
0ce86f5e YW |
243 | } |
244 | ||
3b6a3bde YW |
245 | static int dhcp4_remove_address_and_routes(Link *link, bool only_marked) { |
246 | Address *address; | |
6e537f62 | 247 | Route *route; |
372acaad | 248 | int ret = 0; |
d03073dd | 249 | |
6e537f62 | 250 | assert(link); |
8d01e44c | 251 | assert(link->manager); |
d03073dd | 252 | |
8d01e44c | 253 | SET_FOREACH(route, link->manager->routes) { |
3b6a3bde YW |
254 | if (route->source != NETWORK_CONFIG_SOURCE_DHCP4) |
255 | continue; | |
4e76c57c | 256 | if (route->nexthop.ifindex != link->ifindex) |
8d01e44c | 257 | continue; |
3b6a3bde YW |
258 | if (only_marked && !route_is_marked(route)) |
259 | continue; | |
d03073dd | 260 | |
3caed9ea | 261 | RET_GATHER(ret, route_remove_and_cancel(route, link->manager)); |
6e537f62 YW |
262 | } |
263 | ||
3b6a3bde YW |
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 | ||
f22b586a | 270 | RET_GATHER(ret, address_remove_and_cancel(address, link)); |
6e537f62 YW |
271 | } |
272 | ||
372acaad | 273 | return ret; |
d03073dd YW |
274 | } |
275 | ||
3b6a3bde YW |
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 | ||
e49bad01 | 305 | int dhcp4_check_ready(Link *link) { |
3b6a3bde | 306 | Address *address; |
6e537f62 YW |
307 | int r; |
308 | ||
3b6a3bde YW |
309 | assert(link); |
310 | ||
76c5a0f2 YW |
311 | if (link->dhcp4_messages > 0) { |
312 | log_link_debug(link, "%s(): DHCPv4 address and routes are not set.", __func__); | |
3b6a3bde | 313 | return 0; |
76c5a0f2 YW |
314 | } |
315 | ||
3b6a3bde | 316 | if (dhcp4_address_get(link, &address) < 0) { |
76c5a0f2 | 317 | log_link_debug(link, "%s(): DHCPv4 address is not set.", __func__); |
3b6a3bde | 318 | return 0; |
76c5a0f2 YW |
319 | } |
320 | ||
3b6a3bde | 321 | if (!address_is_ready(address)) { |
76c5a0f2 | 322 | log_link_debug(link, "%s(): DHCPv4 address is not ready.", __func__); |
3b6a3bde YW |
323 | address->callback = dhcp4_address_ready_callback; |
324 | return 0; | |
76c5a0f2 | 325 | } |
153cf041 YW |
326 | |
327 | link->dhcp4_configured = true; | |
3b6a3bde | 328 | log_link_debug(link, "DHCPv4 address and routes set."); |
6e537f62 | 329 | |
153cf041 | 330 | /* New address and routes are configured now. Let's release old lease. */ |
3b6a3bde YW |
331 | r = dhcp4_remove_address_and_routes(link, /* only_marked = */ true); |
332 | if (r < 0) | |
333 | return r; | |
6e537f62 | 334 | |
b8b0c1a0 YW |
335 | r = link_request_stacked_netdevs(link, NETDEV_LOCAL_ADDRESS_DHCP4); |
336 | if (r < 0) | |
337 | return r; | |
338 | ||
8ccae2dd YW |
339 | r = sd_ipv4ll_stop(link->ipv4ll); |
340 | if (r < 0) | |
3b6a3bde | 341 | return log_link_warning_errno(link, r, "Failed to drop IPv4 link-local address: %m"); |
8ccae2dd | 342 | |
153cf041 | 343 | link_check_ready(link); |
76c5a0f2 YW |
344 | return 0; |
345 | } | |
346 | ||
80d62d4f | 347 | static int dhcp4_route_handler(sd_netlink *rtnl, sd_netlink_message *m, Request *req, Link *link, Route *route) { |
3c9b8860 TG |
348 | int r; |
349 | ||
db8dc7c1 | 350 | assert(req); |
3c9b8860 | 351 | assert(link); |
d28746ef | 352 | assert(route); |
3ab7ed3f | 353 | |
d28746ef | 354 | r = route_configure_handler_internal(m, req, route); |
dd0385c1 YW |
355 | if (r <= 0) |
356 | return r; | |
3c9b8860 | 357 | |
3b6a3bde YW |
358 | r = dhcp4_check_ready(link); |
359 | if (r < 0) | |
360 | link_enter_failed(link); | |
153cf041 | 361 | |
3c9b8860 TG |
362 | return 1; |
363 | } | |
364 | ||
e217da13 | 365 | static int dhcp4_request_route(Route *route, Link *link) { |
3b6a3bde YW |
366 | struct in_addr server; |
367 | Route *existing; | |
d4c52ee5 YW |
368 | int r; |
369 | ||
370 | assert(route); | |
d4c52ee5 | 371 | assert(link); |
8d01e44c | 372 | assert(link->manager); |
f95fb199 | 373 | assert(link->network); |
3b6a3bde YW |
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"); | |
d4c52ee5 | 379 | |
3b6a3bde YW |
380 | route->source = NETWORK_CONFIG_SOURCE_DHCP4; |
381 | route->provider.in = server; | |
429dc05a YW |
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) | |
e47bcb7d | 388 | route->table = link_get_dhcp4_route_table(link); |
ebf4fa1e YW |
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; | |
429dc05a | 401 | |
2f542fc3 YW |
402 | r = route_adjust_nexthops(route, link); |
403 | if (r < 0) | |
404 | return r; | |
405 | ||
8d01e44c | 406 | if (route_get(link->manager, route, &existing) < 0) /* This is a new route. */ |
76c5a0f2 | 407 | link->dhcp4_configured = false; |
3b6a3bde YW |
408 | else |
409 | route_unmark(existing); | |
d4c52ee5 | 410 | |
5a18697d | 411 | return link_request_route(link, route, &link->dhcp4_messages, dhcp4_route_handler); |
d4c52ee5 YW |
412 | } |
413 | ||
c7adb64e | 414 | static bool prefixroute_by_kernel(Link *link) { |
c0fef8f3 | 415 | return !link->network->dhcp_route_table_set || |
93a72170 | 416 | link->network->dhcp_route_table == RT_TABLE_MAIN; |
c0fef8f3 | 417 | } |
e723fbd7 | 418 | |
863293db | 419 | static int dhcp4_request_prefix_route(Link *link, Route *rt) { |
74c301b9 | 420 | _cleanup_(route_unrefp) Route *route = NULL; |
e723fbd7 ZJS |
421 | int r; |
422 | ||
c0fef8f3 YW |
423 | assert(link); |
424 | assert(link->dhcp_lease); | |
425 | ||
863293db | 426 | if (prefixroute_by_kernel(link) && (!rt || !rt->table_set || rt->table == RT_TABLE_MAIN)) |
c7adb64e | 427 | /* The prefix route in the main table will be created by the kernel. See dhcp4_update_address(). */ |
c0fef8f3 YW |
428 | return 0; |
429 | ||
2faf2e2a | 430 | r = route_new(&route); |
c0fef8f3 YW |
431 | if (r < 0) |
432 | return r; | |
433 | ||
2faf2e2a | 434 | route->scope = RT_SCOPE_LINK; |
863293db YW |
435 | if (rt) { |
436 | route->table_set = rt->table_set; | |
437 | route->table = rt->table; | |
438 | } | |
2faf2e2a YW |
439 | |
440 | r = sd_dhcp_lease_get_prefix(link->dhcp_lease, &route->dst.in, &route->dst_prefixlen); | |
e723fbd7 ZJS |
441 | if (r < 0) |
442 | return r; | |
443 | ||
2faf2e2a | 444 | r = sd_dhcp_lease_get_address(link->dhcp_lease, &route->prefsrc.in); |
e723fbd7 ZJS |
445 | if (r < 0) |
446 | return r; | |
447 | ||
e217da13 | 448 | return dhcp4_request_route(route, link); |
e723fbd7 ZJS |
449 | } |
450 | ||
c9ee0d2e | 451 | static int dhcp4_request_route_to_gateway(Link *link, const Route *rt) { |
74c301b9 | 452 | _cleanup_(route_unrefp) Route *route = NULL; |
ff2cf677 YW |
453 | struct in_addr address; |
454 | int r; | |
455 | ||
456 | assert(link); | |
457 | assert(link->dhcp_lease); | |
c9ee0d2e YW |
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); | |
ff2cf677 YW |
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 | ||
c9ee0d2e | 473 | route->dst.in = rt->nexthop.gw.in; |
ff2cf677 YW |
474 | route->dst_prefixlen = 32; |
475 | route->prefsrc.in = address; | |
476 | route->scope = RT_SCOPE_LINK; | |
863293db YW |
477 | route->table = rt->table; |
478 | route->table_set = rt->table_set; | |
ff2cf677 | 479 | |
e217da13 | 480 | return dhcp4_request_route(route, link); |
ff2cf677 YW |
481 | } |
482 | ||
76c5a0f2 | 483 | static int dhcp4_request_route_auto( |
e217da13 | 484 | Route *route, |
7f206276 | 485 | Link *link, |
4001653d | 486 | const struct in_addr *gw) { |
7f206276 | 487 | |
4001653d | 488 | struct in_addr address; |
7f206276 YW |
489 | int r; |
490 | ||
491 | assert(route); | |
492 | assert(link); | |
493 | assert(link->dhcp_lease); | |
494 | assert(gw); | |
495 | ||
7f206276 YW |
496 | r = sd_dhcp_lease_get_address(link->dhcp_lease, &address); |
497 | if (r < 0) | |
498 | return r; | |
499 | ||
7f206276 YW |
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; | |
054b8c28 YW |
507 | route->nexthop.family = AF_UNSPEC; |
508 | route->nexthop.gw = IN_ADDR_NULL; | |
7f206276 YW |
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; | |
054b8c28 YW |
518 | route->nexthop.family = AF_UNSPEC; |
519 | route->nexthop.gw = IN_ADDR_NULL; | |
7f206276 YW |
520 | route->prefsrc.in = address; |
521 | ||
4001653d YW |
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; | |
7f206276 | 529 | |
4001653d YW |
530 | r = sd_dhcp_lease_get_prefix(link->dhcp_lease, &prefix, &prefixlen); |
531 | if (r < 0) | |
532 | return r; | |
7f206276 | 533 | |
4001653d YW |
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 | } | |
1d84a3c7 T |
539 | |
540 | route->scope = RT_SCOPE_LINK; | |
054b8c28 YW |
541 | route->nexthop.family = AF_UNSPEC; |
542 | route->nexthop.gw = IN_ADDR_NULL; | |
1d84a3c7 | 543 | route->prefsrc.in = address; |
7f206276 | 544 | |
1d84a3c7 | 545 | } else { |
7f206276 | 546 | route->scope = RT_SCOPE_UNIVERSE; |
054b8c28 YW |
547 | route->nexthop.family = AF_INET; |
548 | route->nexthop.gw.in = *gw; | |
7f206276 | 549 | route->prefsrc.in = address; |
c9ee0d2e YW |
550 | |
551 | r = dhcp4_request_route_to_gateway(link, route); | |
552 | if (r < 0) | |
553 | return r; | |
7f206276 YW |
554 | } |
555 | ||
e217da13 | 556 | return dhcp4_request_route(route, link); |
7f206276 YW |
557 | } |
558 | ||
a56347db | 559 | static int dhcp4_request_classless_static_or_static_routes(Link *link) { |
d52c5a72 YW |
560 | _cleanup_free_ sd_dhcp_route **routes = NULL; |
561 | size_t n_routes; | |
32f8a613 | 562 | int r; |
854a1ccf YW |
563 | |
564 | assert(link); | |
565 | assert(link->dhcp_lease); | |
32f8a613 | 566 | |
a56347db | 567 | if (!link->network->dhcp_use_routes) |
854a1ccf | 568 | return 0; |
7872d0f7 | 569 | |
d52c5a72 | 570 | r = dhcp4_get_classless_static_or_static_routes(link, &routes, &n_routes); |
32f8a613 | 571 | if (r == -ENODATA) |
0ebab55f | 572 | return 0; |
d52c5a72 YW |
573 | if (r < 0) |
574 | return r; | |
32f8a613 | 575 | |
1953bcd1 | 576 | FOREACH_ARRAY(e, routes, n_routes) { |
74c301b9 | 577 | _cleanup_(route_unrefp) Route *route = NULL; |
7f206276 YW |
578 | struct in_addr gw; |
579 | ||
76c5a0f2 YW |
580 | r = route_new(&route); |
581 | if (r < 0) | |
582 | return r; | |
583 | ||
1953bcd1 | 584 | r = sd_dhcp_route_get_gateway(*e, &gw); |
854a1ccf | 585 | if (r < 0) |
7872d0f7 | 586 | return r; |
854a1ccf | 587 | |
1953bcd1 | 588 | r = sd_dhcp_route_get_destination(*e, &route->dst.in); |
7872d0f7 YW |
589 | if (r < 0) |
590 | return r; | |
854a1ccf | 591 | |
1953bcd1 | 592 | r = sd_dhcp_route_get_destination_prefix_length(*e, &route->dst_prefixlen); |
7872d0f7 YW |
593 | if (r < 0) |
594 | return r; | |
595 | ||
e217da13 | 596 | r = dhcp4_request_route_auto(route, link, &gw); |
854a1ccf | 597 | if (r < 0) |
7872d0f7 YW |
598 | return r; |
599 | } | |
600 | ||
a56347db | 601 | return 0; |
7872d0f7 YW |
602 | } |
603 | ||
a56347db | 604 | static int dhcp4_request_default_gateway(Link *link) { |
74c301b9 | 605 | _cleanup_(route_unrefp) Route *route = NULL; |
15aa7a5c | 606 | struct in_addr address, router; |
ff2cf677 YW |
607 | int r; |
608 | ||
609 | assert(link); | |
610 | assert(link->dhcp_lease); | |
a56347db YW |
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; | |
ff2cf677 YW |
620 | |
621 | r = sd_dhcp_lease_get_address(link->dhcp_lease, &address); | |
622 | if (r < 0) | |
623 | return r; | |
624 | ||
15aa7a5c YW |
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."); | |
ff2cf677 YW |
628 | return 0; |
629 | } | |
630 | if (r < 0) | |
631 | return r; | |
0ebab55f | 632 | |
ff2cf677 YW |
633 | r = route_new(&route); |
634 | if (r < 0) | |
635 | return r; | |
636 | ||
637 | /* Next, add a default gateway. */ | |
054b8c28 YW |
638 | route->nexthop.family = AF_INET; |
639 | route->nexthop.gw.in = router; | |
ff2cf677 | 640 | route->prefsrc.in = address; |
ff2cf677 | 641 | |
c9ee0d2e YW |
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 | ||
e217da13 | 648 | return dhcp4_request_route(route, link); |
0ebab55f YW |
649 | } |
650 | ||
0ce86f5e | 651 | static int dhcp4_request_semi_static_routes(Link *link) { |
0ebab55f YW |
652 | Route *rt; |
653 | int r; | |
654 | ||
655 | assert(link); | |
656 | assert(link->dhcp_lease); | |
657 | assert(link->network); | |
0ebab55f | 658 | |
ff2cf677 | 659 | HASHMAP_FOREACH(rt, link->network->routes_by_section) { |
74c301b9 | 660 | _cleanup_(route_unrefp) Route *route = NULL; |
0ce86f5e | 661 | struct in_addr gw; |
0ebab55f | 662 | |
e92f40c6 | 663 | if (rt->source != NETWORK_CONFIG_SOURCE_DHCP4) |
ff2cf677 YW |
664 | continue; |
665 | ||
0ce86f5e | 666 | assert(rt->family == AF_INET); |
e92f40c6 | 667 | assert(rt->nexthop.family == AF_INET); |
0ce86f5e | 668 | |
0f3116f0 YW |
669 | r = dhcp4_find_gateway_for_destination(link, &rt->dst.in, rt->dst_prefixlen, &gw); |
670 | if (r == -EHOSTUNREACH) { | |
0ce86f5e YW |
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 | ||
0f3116f0 YW |
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 | ||
413ea20a | 684 | r = route_dup(rt, NULL, &route); |
76c5a0f2 YW |
685 | if (r < 0) |
686 | return r; | |
687 | ||
054b8c28 | 688 | route->nexthop.gw.in = gw; |
76c5a0f2 | 689 | |
7befabaf YW |
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 | ||
863293db YW |
696 | r = dhcp4_request_prefix_route(link, route); |
697 | if (r < 0) | |
698 | return r; | |
699 | ||
c9ee0d2e YW |
700 | r = dhcp4_request_route_to_gateway(link, route); |
701 | if (r < 0) | |
702 | return r; | |
703 | ||
e217da13 | 704 | r = dhcp4_request_route(route, link); |
ff2cf677 YW |
705 | if (r < 0) |
706 | return r; | |
854a1ccf YW |
707 | } |
708 | ||
709 | return 0; | |
710 | } | |
711 | ||
76c5a0f2 | 712 | static int dhcp4_request_routes_to_servers( |
949b2c95 YW |
713 | Link *link, |
714 | const struct in_addr *servers, | |
0ce86f5e | 715 | size_t n_servers) { |
949b2c95 | 716 | |
949b2c95 | 717 | int r; |
e1c08a3d YW |
718 | |
719 | assert(link); | |
720 | assert(link->dhcp_lease); | |
721 | assert(link->network); | |
949b2c95 | 722 | assert(servers || n_servers == 0); |
e723fbd7 | 723 | |
1953bcd1 | 724 | FOREACH_ARRAY(dst, servers, n_servers) { |
74c301b9 | 725 | _cleanup_(route_unrefp) Route *route = NULL; |
0ce86f5e | 726 | struct in_addr gw; |
76c5a0f2 | 727 | |
1953bcd1 | 728 | if (in4_addr_is_null(dst)) |
3e429683 | 729 | continue; |
76c5a0f2 | 730 | |
0f3116f0 | 731 | r = dhcp4_find_gateway_for_destination(link, dst, 32, &gw); |
0ce86f5e YW |
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)); | |
3e429683 | 735 | continue; |
0ce86f5e YW |
736 | } |
737 | if (r < 0) | |
738 | return r; | |
3e429683 | 739 | |
76c5a0f2 YW |
740 | r = route_new(&route); |
741 | if (r < 0) | |
742 | return r; | |
743 | ||
1953bcd1 | 744 | route->dst.in = *dst; |
76c5a0f2 | 745 | route->dst_prefixlen = 32; |
e1c08a3d | 746 | |
e217da13 | 747 | r = dhcp4_request_route_auto(route, link, &gw); |
e1c08a3d YW |
748 | if (r < 0) |
749 | return r; | |
750 | } | |
751 | ||
e723fbd7 ZJS |
752 | return 0; |
753 | } | |
754 | ||
0ce86f5e | 755 | static int dhcp4_request_routes_to_dns(Link *link) { |
949b2c95 YW |
756 | const struct in_addr *dns; |
757 | int r; | |
758 | ||
759 | assert(link); | |
760 | assert(link->dhcp_lease); | |
761 | assert(link->network); | |
949b2c95 | 762 | |
9646ffe2 | 763 | if (!link_get_use_dns(link, NETWORK_CONFIG_SOURCE_DHCP4) || |
949b2c95 YW |
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 | ||
0ce86f5e | 773 | return dhcp4_request_routes_to_servers(link, dns, r); |
949b2c95 YW |
774 | } |
775 | ||
0ce86f5e | 776 | static int dhcp4_request_routes_to_ntp(Link *link) { |
d7b04506 YW |
777 | const struct in_addr *ntp; |
778 | int r; | |
779 | ||
780 | assert(link); | |
781 | assert(link->dhcp_lease); | |
782 | assert(link->network); | |
d7b04506 | 783 | |
d12fb2bc | 784 | if (!link_get_use_ntp(link, NETWORK_CONFIG_SOURCE_DHCP4) || |
d7b04506 YW |
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 | ||
0ce86f5e | 794 | return dhcp4_request_routes_to_servers(link, ntp, r); |
d7b04506 YW |
795 | } |
796 | ||
76c5a0f2 | 797 | static int dhcp4_request_routes(Link *link) { |
7872d0f7 | 798 | int r; |
3c9b8860 TG |
799 | |
800 | assert(link); | |
3b6a3bde | 801 | assert(link->dhcp_lease); |
d4c52ee5 | 802 | |
863293db | 803 | r = dhcp4_request_prefix_route(link, /* rt = */ NULL); |
b23aec0d | 804 | if (r < 0) |
76c5a0f2 | 805 | return log_link_error_errno(link, r, "DHCP error: Could not request prefix route: %m"); |
156ddf8d | 806 | |
a56347db YW |
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); | |
7872d0f7 | 812 | if (r < 0) |
76c5a0f2 | 813 | return log_link_error_errno(link, r, "DHCP error: Could not request static routes: %m"); |
5f04a209 | 814 | |
0ce86f5e | 815 | r = dhcp4_request_semi_static_routes(link); |
0ebab55f YW |
816 | if (r < 0) |
817 | return log_link_error_errno(link, r, "DHCP error: Could not request routes with Gateway=_dhcp4 setting: %m"); | |
818 | ||
0ce86f5e | 819 | r = dhcp4_request_routes_to_dns(link); |
e1c08a3d | 820 | if (r < 0) |
76c5a0f2 | 821 | return log_link_error_errno(link, r, "DHCP error: Could not request routes to DNS servers: %m"); |
1985c54f | 822 | |
0ce86f5e | 823 | r = dhcp4_request_routes_to_ntp(link); |
d7b04506 | 824 | if (r < 0) |
76c5a0f2 | 825 | return log_link_error_errno(link, r, "DHCP error: Could not request routes to NTP servers: %m"); |
d7b04506 | 826 | |
e1c08a3d | 827 | return 0; |
3c9b8860 TG |
828 | } |
829 | ||
7fa472f9 | 830 | static int dhcp_reset_mtu(Link *link) { |
7fa472f9 YW |
831 | int r; |
832 | ||
833 | assert(link); | |
834 | ||
835 | if (!link->network->dhcp_use_mtu) | |
836 | return 0; | |
837 | ||
0fa8ee6c | 838 | r = link_request_to_set_mtu(link, link->original_mtu); |
46b875fb | 839 | if (r < 0) |
da94a69b | 840 | return log_link_error_errno(link, r, "DHCP error: Could not queue request to reset MTU: %m"); |
3c9b8860 | 841 | |
7fa472f9 YW |
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 | ||
9155da08 | 869 | int dhcp4_lease_lost(Link *link) { |
372acaad | 870 | int r = 0; |
7fa472f9 YW |
871 | |
872 | assert(link); | |
873 | assert(link->dhcp_lease); | |
e49bad01 | 874 | assert(link->network); |
7fa472f9 | 875 | |
f8c2e4b9 | 876 | log_link_info(link, "DHCP lease lost"); |
7fa472f9 YW |
877 | |
878 | link->dhcp4_configured = false; | |
879 | ||
e49bad01 | 880 | if (link->network->dhcp_use_6rd && |
0ad8d953 | 881 | sd_dhcp_lease_has_6rd(link->dhcp_lease)) |
e49bad01 YW |
882 | dhcp4_pd_prefix_lost(link); |
883 | ||
372acaad YW |
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)); | |
7fa472f9 | 887 | |
3c9b8860 | 888 | link->dhcp_lease = sd_dhcp_lease_unref(link->dhcp_lease); |
6a3e5f6a | 889 | link_dirty(link); |
3c9b8860 | 890 | |
3b6a3bde | 891 | /* If one of the above failed. Do not request nexthops and routes. */ |
153cf041 YW |
892 | if (r < 0) |
893 | return r; | |
894 | ||
76a86ffd | 895 | r = link_request_static_nexthops(link, true); |
153cf041 YW |
896 | if (r < 0) |
897 | return r; | |
898 | ||
76a86ffd | 899 | return link_request_static_routes(link, true); |
153cf041 YW |
900 | } |
901 | ||
80d62d4f | 902 | static int dhcp4_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Request *req, Link *link, Address *address) { |
3c9b8860 TG |
903 | int r; |
904 | ||
905 | assert(link); | |
d28746ef | 906 | assert(address); |
3c9b8860 | 907 | |
d28746ef | 908 | r = address_configure_handler_internal(m, link, address); |
5a07fa9d YW |
909 | if (r <= 0) |
910 | return r; | |
3c9b8860 | 911 | |
3b6a3bde YW |
912 | r = dhcp4_check_ready(link); |
913 | if (r < 0) | |
914 | link_enter_failed(link); | |
0f3ff4ea | 915 | |
3c9b8860 TG |
916 | return 1; |
917 | } | |
918 | ||
76c5a0f2 | 919 | static int dhcp4_request_address(Link *link, bool announce) { |
ebd96906 | 920 | _cleanup_(address_unrefp) Address *addr = NULL; |
2faf2e2a YW |
921 | struct in_addr address, server; |
922 | uint8_t prefixlen; | |
3b6a3bde | 923 | Address *existing; |
16bc8635 | 924 | usec_t lifetime_usec; |
3c9b8860 TG |
925 | int r; |
926 | ||
6906794d | 927 | assert(link); |
def4741b | 928 | assert(link->manager); |
6906794d | 929 | assert(link->network); |
3b6a3bde | 930 | assert(link->dhcp_lease); |
6906794d | 931 | |
6906794d YW |
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 | ||
2faf2e2a | 936 | r = sd_dhcp_lease_get_prefix(link->dhcp_lease, NULL, &prefixlen); |
6906794d YW |
937 | if (r < 0) |
938 | return log_link_warning_errno(link, r, "DHCP error: no netmask: %m"); | |
939 | ||
ec84bb2a YW |
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 | ||
80a89d1a | 944 | if (!FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DYNAMIC)) { |
a93538e8 | 945 | r = sd_dhcp_lease_get_lifetime_timestamp(link->dhcp_lease, CLOCK_BOOTTIME, &lifetime_usec); |
6906794d | 946 | if (r < 0) |
a93538e8 | 947 | return log_link_warning_errno(link, r, "DHCP error: failed to get lifetime: %m"); |
16bc8635 YW |
948 | } else |
949 | lifetime_usec = USEC_INFINITY; | |
6906794d | 950 | |
6906794d YW |
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 | ||
94876904 | 958 | if (r > 0 && in4_addr_is_set(&router[0])) |
6906794d YW |
959 | log_struct(LOG_INFO, |
960 | LOG_LINK_INTERFACE(link), | |
ec84bb2a | 961 | LOG_LINK_MESSAGE(link, "DHCPv4 address "IPV4_ADDRESS_FMT_STR"/%u, gateway "IPV4_ADDRESS_FMT_STR" acquired from "IPV4_ADDRESS_FMT_STR, |
ceea6c1a | 962 | IPV4_ADDRESS_FMT_VAL(address), |
6906794d | 963 | prefixlen, |
ec84bb2a YW |
964 | IPV4_ADDRESS_FMT_VAL(router[0]), |
965 | IPV4_ADDRESS_FMT_VAL(server)), | |
3cf6a3a3 YW |
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]))); | |
6906794d YW |
969 | else |
970 | log_struct(LOG_INFO, | |
971 | LOG_LINK_INTERFACE(link), | |
ec84bb2a | 972 | LOG_LINK_MESSAGE(link, "DHCPv4 address "IPV4_ADDRESS_FMT_STR"/%u acquired from "IPV4_ADDRESS_FMT_STR, |
ceea6c1a | 973 | IPV4_ADDRESS_FMT_VAL(address), |
ec84bb2a YW |
974 | prefixlen, |
975 | IPV4_ADDRESS_FMT_VAL(server)), | |
3cf6a3a3 YW |
976 | LOG_ITEM("ADDRESS="IPV4_ADDRESS_FMT_STR, IPV4_ADDRESS_FMT_VAL(address)), |
977 | LOG_ITEM("PREFIXLEN=%u", prefixlen)); | |
6906794d | 978 | } |
3c9b8860 | 979 | |
f0213e37 | 980 | r = address_new(&addr); |
3c9b8860 | 981 | if (r < 0) |
6906794d | 982 | return log_oom(); |
3c9b8860 | 983 | |
3b6a3bde YW |
984 | addr->source = NETWORK_CONFIG_SOURCE_DHCP4; |
985 | addr->provider.in = server; | |
3c9b8860 | 986 | addr->family = AF_INET; |
6906794d | 987 | addr->in_addr.in.s_addr = address.s_addr; |
16bc8635 YW |
988 | addr->lifetime_preferred_usec = lifetime_usec; |
989 | addr->lifetime_valid_usec = lifetime_usec; | |
3c9b8860 | 990 | addr->prefixlen = prefixlen; |
a49cf370 YW |
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"); | |
c7adb64e | 994 | SET_FLAG(addr->flags, IFA_F_NOPREFIXROUTE, !prefixroute_by_kernel(link)); |
415deef9 | 995 | addr->route_metric = link->network->dhcp_route_metric; |
76a86ffd | 996 | addr->duplicate_address_detection = link->network->dhcp_send_decline ? ADDRESS_FAMILY_IPV4 : ADDRESS_FAMILY_NO; |
3c9b8860 | 997 | |
471e332d YW |
998 | r = free_and_strdup_warn(&addr->label, link->network->dhcp_label); |
999 | if (r < 0) | |
1000 | return r; | |
d419ef02 | 1001 | |
4b3590c3 TM |
1002 | r = free_and_strdup_warn(&addr->netlabel, link->network->dhcp_netlabel); |
1003 | if (r < 0) | |
1004 | return r; | |
1005 | ||
3b6a3bde | 1006 | if (address_get(link, addr, &existing) < 0) /* The address is new. */ |
76c5a0f2 | 1007 | link->dhcp4_configured = false; |
3b6a3bde YW |
1008 | else |
1009 | address_unmark(existing); | |
76c5a0f2 | 1010 | |
f60e6558 | 1011 | r = link_request_address(link, addr, &link->dhcp4_messages, |
3b6a3bde | 1012 | dhcp4_address_handler, NULL); |
3c9b8860 | 1013 | if (r < 0) |
76c5a0f2 | 1014 | return log_link_error_errno(link, r, "Failed to request DHCPv4 address: %m"); |
76c5a0f2 YW |
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 | ||
2ccada8d | 1024 | link_mark_addresses(link, NETWORK_CONFIG_SOURCE_DHCP4); |
8d01e44c | 1025 | manager_mark_routes(link->manager, link, NETWORK_CONFIG_SOURCE_DHCP4); |
3b6a3bde | 1026 | |
76c5a0f2 YW |
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 | ||
3b6a3bde YW |
1035 | if (!link->dhcp4_configured) { |
1036 | link_set_state(link, LINK_STATE_CONFIGURING); | |
1037 | link_check_ready(link); | |
1038 | } | |
3c9b8860 TG |
1039 | |
1040 | return 0; | |
1041 | } | |
1042 | ||
1043 | static int dhcp_lease_renew(sd_dhcp_client *client, Link *link) { | |
e49bad01 | 1044 | _cleanup_(sd_dhcp_lease_unrefp) sd_dhcp_lease *old_lease = NULL; |
3c9b8860 | 1045 | sd_dhcp_lease *lease; |
3c9b8860 TG |
1046 | int r; |
1047 | ||
1048 | assert(link); | |
e49bad01 | 1049 | assert(link->network); |
3c9b8860 | 1050 | assert(client); |
3c9b8860 TG |
1051 | |
1052 | r = sd_dhcp_client_get_lease(client, &lease); | |
f6b8196f LP |
1053 | if (r < 0) |
1054 | return log_link_warning_errno(link, r, "DHCP error: no lease: %m"); | |
3c9b8860 | 1055 | |
e49bad01 | 1056 | old_lease = TAKE_PTR(link->dhcp_lease); |
e6b18ffa | 1057 | link->dhcp_lease = sd_dhcp_lease_ref(lease); |
6a3e5f6a | 1058 | link_dirty(link); |
3c9b8860 | 1059 | |
e49bad01 | 1060 | if (link->network->dhcp_use_6rd) { |
0ad8d953 | 1061 | if (sd_dhcp_lease_has_6rd(link->dhcp_lease)) { |
e49bad01 YW |
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"); | |
0ad8d953 | 1065 | } else if (sd_dhcp_lease_has_6rd(old_lease)) |
e49bad01 YW |
1066 | dhcp4_pd_prefix_lost(link); |
1067 | } | |
1068 | ||
76c5a0f2 | 1069 | return dhcp4_request_address_and_routes(link, false); |
3c9b8860 TG |
1070 | } |
1071 | ||
1072 | static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) { | |
1073 | sd_dhcp_lease *lease; | |
3c9b8860 TG |
1074 | int r; |
1075 | ||
1076 | assert(client); | |
1077 | assert(link); | |
1078 | ||
1079 | r = sd_dhcp_client_get_lease(client, &lease); | |
f2341e0a | 1080 | if (r < 0) |
f6b8196f | 1081 | return log_link_error_errno(link, r, "DHCP error: No lease: %m"); |
3c9b8860 | 1082 | |
6906794d | 1083 | sd_dhcp_lease_unref(link->dhcp_lease); |
e6b18ffa | 1084 | link->dhcp_lease = sd_dhcp_lease_ref(lease); |
6a3e5f6a | 1085 | link_dirty(link); |
3c9b8860 | 1086 | |
27cb34f5 | 1087 | if (link->network->dhcp_use_mtu) { |
3c9b8860 TG |
1088 | uint16_t mtu; |
1089 | ||
1090 | r = sd_dhcp_lease_get_mtu(lease, &mtu); | |
1091 | if (r >= 0) { | |
0fa8ee6c | 1092 | r = link_request_to_set_mtu(link, mtu); |
3c9b8860 | 1093 | if (r < 0) |
f2341e0a | 1094 | log_link_error_errno(link, r, "Failed to set MTU to %" PRIu16 ": %m", mtu); |
3c9b8860 TG |
1095 | } |
1096 | } | |
1097 | ||
27cb34f5 | 1098 | if (link->network->dhcp_use_hostname) { |
2de2abad LB |
1099 | const char *dhcpname = NULL; |
1100 | _cleanup_free_ char *hostname = NULL; | |
3c9b8860 | 1101 | |
27cb34f5 | 1102 | if (link->network->dhcp_hostname) |
2de2abad | 1103 | dhcpname = link->network->dhcp_hostname; |
dce391e7 | 1104 | else |
2de2abad LB |
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) | |
5238e957 | 1112 | log_link_notice(link, "Overlong DHCP hostname received, shortened from '%s' to '%s'", dhcpname, hostname); |
2de2abad | 1113 | } |
a7d0ef44 | 1114 | |
dce391e7 | 1115 | if (hostname) { |
59eb33e0 | 1116 | r = manager_set_hostname(link->manager, hostname); |
3c9b8860 | 1117 | if (r < 0) |
f2341e0a | 1118 | log_link_error_errno(link, r, "Failed to set transient hostname to '%s': %m", hostname); |
3c9b8860 TG |
1119 | } |
1120 | } | |
1121 | ||
27cb34f5 | 1122 | if (link->network->dhcp_use_timezone) { |
21b80ad1 LP |
1123 | const char *tz = NULL; |
1124 | ||
1125 | (void) sd_dhcp_lease_get_timezone(link->dhcp_lease, &tz); | |
1126 | ||
1127 | if (tz) { | |
59eb33e0 | 1128 | r = manager_set_timezone(link->manager, tz); |
21b80ad1 LP |
1129 | if (r < 0) |
1130 | log_link_error_errno(link, r, "Failed to set timezone to '%s': %m", tz); | |
1131 | } | |
1132 | } | |
1133 | ||
e49bad01 | 1134 | if (link->network->dhcp_use_6rd && |
0ad8d953 | 1135 | sd_dhcp_lease_has_6rd(link->dhcp_lease)) { |
e49bad01 YW |
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 | ||
76c5a0f2 | 1141 | return dhcp4_request_address_and_routes(link, true); |
3c9b8860 | 1142 | } |
8bc17bb3 | 1143 | |
d03073dd YW |
1144 | static int dhcp_lease_ip_change(sd_dhcp_client *client, Link *link) { |
1145 | int r; | |
1146 | ||
d03073dd | 1147 | r = dhcp_lease_acquired(client, link); |
6e537f62 | 1148 | if (r < 0) |
9155da08 | 1149 | (void) dhcp4_lease_lost(link); |
d03073dd | 1150 | |
6e537f62 | 1151 | return r; |
d03073dd YW |
1152 | } |
1153 | ||
c995fa02 | 1154 | static int dhcp_server_is_filtered(Link *link, sd_dhcp_client *client) { |
727b5734 SS |
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) | |
0da425df | 1169 | return log_link_debug_errno(link, r, "Failed to get DHCP server IP address: %m"); |
727b5734 | 1170 | |
c995fa02 | 1171 | if (in4_address_is_filtered(&addr, link->network->dhcp_allow_listed_ip, link->network->dhcp_deny_listed_ip)) { |
a0b7cae5 YW |
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)); | |
98ebef62 | 1178 | |
98ebef62 SS |
1179 | return true; |
1180 | } | |
1181 | ||
1182 | return false; | |
1183 | } | |
1184 | ||
727b5734 | 1185 | static int dhcp4_handler(sd_dhcp_client *client, int event, void *userdata) { |
99534007 | 1186 | Link *link = ASSERT_PTR(userdata); |
86e2be7b | 1187 | int r; |
3c9b8860 | 1188 | |
3c9b8860 TG |
1189 | assert(link->network); |
1190 | assert(link->manager); | |
1191 | ||
1192 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
727b5734 | 1193 | return 0; |
3c9b8860 TG |
1194 | |
1195 | switch (event) { | |
03748142 | 1196 | case SD_DHCP_CLIENT_EVENT_STOP: |
80a89d1a | 1197 | if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DYNAMIC)) { |
fecd205d YW |
1198 | log_link_notice(link, "DHCPv4 connection considered critical, ignoring request to reconfigure it."); |
1199 | return 0; | |
1200 | } | |
1201 | ||
b5c83cd5 YW |
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."); | |
8bc17bb3 | 1207 | |
efdb62df | 1208 | if (link->dhcp_lease) { |
f766d9af YW |
1209 | if (link->network->dhcp_send_release) { |
1210 | r = sd_dhcp_client_send_release(client); | |
1211 | if (r < 0) | |
e558d4f4 YW |
1212 | log_link_full_errno(link, |
1213 | ERRNO_IS_DISCONNECT(r) ? LOG_DEBUG : LOG_WARNING, | |
1214 | r, "Failed to send DHCP RELEASE, ignoring: %m"); | |
f766d9af | 1215 | } |
8bea7e70 | 1216 | |
9155da08 | 1217 | r = dhcp4_lease_lost(link); |
efdb62df YW |
1218 | if (r < 0) { |
1219 | link_enter_failed(link); | |
1220 | return r; | |
1221 | } | |
1222 | } | |
1223 | ||
1224 | break; | |
8bc17bb3 | 1225 | case SD_DHCP_CLIENT_EVENT_EXPIRED: |
80a89d1a | 1226 | if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DYNAMIC)) { |
efdb62df | 1227 | log_link_notice(link, "DHCPv4 connection considered critical, ignoring request to reconfigure it."); |
727b5734 | 1228 | return 0; |
3c9b8860 TG |
1229 | } |
1230 | ||
1231 | if (link->dhcp_lease) { | |
9155da08 | 1232 | r = dhcp4_lease_lost(link); |
3c9b8860 TG |
1233 | if (r < 0) { |
1234 | link_enter_failed(link); | |
727b5734 | 1235 | return r; |
3c9b8860 TG |
1236 | } |
1237 | } | |
1238 | ||
d03073dd YW |
1239 | break; |
1240 | case SD_DHCP_CLIENT_EVENT_IP_CHANGE: | |
80a89d1a | 1241 | if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DYNAMIC)) { |
d03073dd YW |
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; | |
3c9b8860 TG |
1250 | } |
1251 | ||
1252 | break; | |
03748142 | 1253 | case SD_DHCP_CLIENT_EVENT_RENEW: |
3c9b8860 TG |
1254 | r = dhcp_lease_renew(client, link); |
1255 | if (r < 0) { | |
1256 | link_enter_failed(link); | |
727b5734 | 1257 | return r; |
3c9b8860 TG |
1258 | } |
1259 | break; | |
03748142 | 1260 | case SD_DHCP_CLIENT_EVENT_IP_ACQUIRE: |
3c9b8860 TG |
1261 | r = dhcp_lease_acquired(client, link); |
1262 | if (r < 0) { | |
1263 | link_enter_failed(link); | |
727b5734 | 1264 | return r; |
3c9b8860 TG |
1265 | } |
1266 | break; | |
727b5734 | 1267 | case SD_DHCP_CLIENT_EVENT_SELECTING: |
c995fa02 YW |
1268 | r = dhcp_server_is_filtered(link, client); |
1269 | if (r < 0) { | |
1270 | link_enter_failed(link); | |
1271 | return r; | |
98ebef62 | 1272 | } |
c995fa02 YW |
1273 | if (r > 0) |
1274 | return -ENOMSG; | |
727b5734 | 1275 | break; |
0107b769 ZJS |
1276 | |
1277 | case SD_DHCP_CLIENT_EVENT_TRANSIENT_FAILURE: | |
b5c83cd5 YW |
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) | |
0b4b66cc | 1282 | log_link_debug(link, "Problems acquiring DHCP lease, acquiring IPv4 link-local address"); |
0107b769 ZJS |
1283 | break; |
1284 | ||
3c9b8860 TG |
1285 | default: |
1286 | if (event < 0) | |
f6b8196f | 1287 | log_link_warning_errno(link, event, "DHCP error: Client failed: %m"); |
3c9b8860 | 1288 | else |
f6b8196f | 1289 | log_link_warning(link, "DHCP unknown event: %i", event); |
3c9b8860 TG |
1290 | } |
1291 | ||
727b5734 | 1292 | return 0; |
3c9b8860 TG |
1293 | } |
1294 | ||
7192bb81 LP |
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" */ | |
87160186 | 1309 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to get hostname: %m"); |
7192bb81 LP |
1310 | |
1311 | hn = hostname; | |
1312 | } | |
1313 | ||
a8494759 YW |
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. */ | |
87160186 | 1317 | log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set hostname from kernel hostname, ignoring: %m"); |
a8494759 | 1318 | else if (r < 0) |
87160186 | 1319 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set hostname: %m"); |
64b21ece MV |
1320 | |
1321 | return 0; | |
1322 | } | |
1323 | ||
d947f7f9 | 1324 | static int dhcp4_set_client_identifier(Link *link) { |
fff1f40c YW |
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: { | |
0cf7c3fd | 1333 | /* If configured, apply user specified DUID and IAID */ |
4e26a5ba | 1334 | const DUID *duid = link_get_dhcp4_duid(link); |
fff1f40c | 1335 | |
53488ea3 YW |
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, | |
4e26a5ba YW |
1367 | link->network->dhcp_iaid_set, |
1368 | link->network->dhcp_iaid, | |
53488ea3 | 1369 | duid->type, duid->raw_data, duid->raw_data_len); |
fff1f40c | 1370 | if (r < 0) |
53488ea3 | 1371 | return r; |
fff1f40c YW |
1372 | break; |
1373 | } | |
14b66dbc | 1374 | case DHCP_CLIENT_ID_MAC: { |
ca2b7cd8 | 1375 | const uint8_t *hw_addr = link->hw_addr.bytes; |
14b66dbc TR |
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 | ||
fff1f40c | 1384 | r = sd_dhcp_client_set_client_id(link->dhcp_client, |
14b66dbc TR |
1385 | link->iftype, |
1386 | hw_addr, | |
1387 | hw_addr_len); | |
fff1f40c | 1388 | if (r < 0) |
87160186 | 1389 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set client ID: %m"); |
fff1f40c | 1390 | break; |
14b66dbc | 1391 | } |
fff1f40c | 1392 | default: |
04499a70 | 1393 | assert_not_reached(); |
fff1f40c YW |
1394 | } |
1395 | ||
1396 | return 0; | |
1397 | } | |
1398 | ||
451c2baf | 1399 | static int dhcp4_set_request_address(Link *link) { |
571eeba9 | 1400 | assert(link); |
3def8850 | 1401 | assert(link->network); |
451c2baf | 1402 | assert(link->dhcp_client); |
571eeba9 | 1403 | |
451c2baf YW |
1404 | /* 1. Use already assigned address. */ |
1405 | Address *a; | |
3b6a3bde | 1406 | SET_FOREACH(a, link->addresses) { |
451c2baf | 1407 | if (a->source != NETWORK_CONFIG_SOURCE_DHCP4) |
3def8850 | 1408 | continue; |
b93bf1bf | 1409 | |
451c2baf | 1410 | assert(a->family == AF_INET); |
b93bf1bf | 1411 | |
451c2baf YW |
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 | } | |
b93bf1bf | 1416 | |
451c2baf YW |
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 | } | |
b93bf1bf | 1423 | |
80a89d1a YW |
1424 | /* 3. If KeepConfiguration=dynamic, use a foreign dynamic address. */ |
1425 | if (!FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DYNAMIC)) | |
451c2baf | 1426 | return 0; |
571eeba9 | 1427 | |
451c2baf YW |
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; | |
b93bf1bf | 1435 | |
451c2baf YW |
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 | } | |
5f016e32 | 1440 | |
451c2baf | 1441 | return 0; |
571eeba9 YW |
1442 | } |
1443 | ||
e70eca9b | 1444 | static bool link_needs_dhcp_broadcast(Link *link) { |
e70eca9b VM |
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 | |
fc956a39 YW |
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 | ||
e70eca9b | 1455 | r = link->network->dhcp_broadcast; |
fc956a39 YW |
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"); | |
e70eca9b | 1466 | |
fc956a39 | 1467 | return false; |
e70eca9b | 1468 | } |
fc956a39 YW |
1469 | |
1470 | log_link_debug(link, "DHCPv4 CLIENT: Detected ID_NET_DHCP_BROADCAST='%d'.", r); | |
1471 | return r; | |
e70eca9b VM |
1472 | } |
1473 | ||
fc35a9f8 YW |
1474 | static bool link_dhcp4_ipv6_only_mode(Link *link) { |
1475 | assert(link); | |
1476 | assert(link->network); | |
1477 | ||
7dc43183 | 1478 | /* If it is explicitly specified, then honor the setting. */ |
fc35a9f8 YW |
1479 | if (link->network->dhcp_ipv6_only_mode >= 0) |
1480 | return link->network->dhcp_ipv6_only_mode; | |
1481 | ||
7dc43183 YW |
1482 | /* Defaults to false, until we support 464XLAT. See issue #30891. */ |
1483 | return false; | |
fc35a9f8 YW |
1484 | } |
1485 | ||
ccffa166 | 1486 | static int dhcp4_configure(Link *link) { |
cb29c156 | 1487 | sd_dhcp_option *send_option; |
5bc945be | 1488 | void *request_options; |
3c9b8860 TG |
1489 | int r; |
1490 | ||
1491 | assert(link); | |
1492 | assert(link->network); | |
2ffd6d73 | 1493 | |
bc9e40c9 | 1494 | if (link->dhcp_client) |
87160186 | 1495 | return log_link_debug_errno(link, SYNTHETIC_ERRNO(EBUSY), "DHCPv4 client is already configured."); |
3c9b8860 | 1496 | |
bc9e40c9 YW |
1497 | r = sd_dhcp_client_new(&link->dhcp_client, link->network->dhcp_anonymize); |
1498 | if (r < 0) | |
87160186 | 1499 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to allocate DHCPv4 client: %m"); |
bc9e40c9 | 1500 | |
0dbb5139 CF |
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 | ||
bc9e40c9 YW |
1506 | r = sd_dhcp_client_attach_event(link->dhcp_client, link->manager->event, 0); |
1507 | if (r < 0) | |
87160186 | 1508 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to attach event to DHCPv4 client: %m"); |
3c9b8860 | 1509 | |
3b1dbdf0 YW |
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 | ||
808b65a0 RP |
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 | ||
76253e73 | 1518 | r = sd_dhcp_client_set_mac(link->dhcp_client, |
ca2b7cd8 YW |
1519 | link->hw_addr.bytes, |
1520 | link->bcast_addr.length > 0 ? link->bcast_addr.bytes : NULL, | |
14b66dbc | 1521 | link->hw_addr.length, link->iftype); |
3c9b8860 | 1522 | if (r < 0) |
87160186 | 1523 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set MAC address: %m"); |
3c9b8860 | 1524 | |
2f8e7633 | 1525 | r = sd_dhcp_client_set_ifindex(link->dhcp_client, link->ifindex); |
3c9b8860 | 1526 | if (r < 0) |
87160186 | 1527 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set ifindex: %m"); |
3c9b8860 TG |
1528 | |
1529 | r = sd_dhcp_client_set_callback(link->dhcp_client, dhcp4_handler, link); | |
1530 | if (r < 0) | |
87160186 | 1531 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set callback: %m"); |
3c9b8860 | 1532 | |
e70eca9b | 1533 | r = sd_dhcp_client_set_request_broadcast(link->dhcp_client, link_needs_dhcp_broadcast(link)); |
3c9b8860 | 1534 | if (r < 0) |
87160186 | 1535 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for broadcast: %m"); |
3c9b8860 | 1536 | |
f8da534e | 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 | ||
62f12d75 | 1541 | if (link->mtu > 0) { |
3c9b8860 TG |
1542 | r = sd_dhcp_client_set_mtu(link->dhcp_client, link->mtu); |
1543 | if (r < 0) | |
87160186 | 1544 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set MTU: %m"); |
3c9b8860 TG |
1545 | } |
1546 | ||
a83bda05 | 1547 | if (!link->network->dhcp_anonymize) { |
5d896def YW |
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 | ||
a83bda05 | 1552 | if (link->network->dhcp_use_mtu) { |
3cb62efe | 1553 | r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_MTU_INTERFACE); |
a83bda05 | 1554 | if (r < 0) |
87160186 | 1555 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for MTU: %m"); |
a83bda05 | 1556 | } |
3c9b8860 | 1557 | |
a83bda05 YW |
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) | |
87160186 | 1561 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for static route: %m"); |
1f6860d9 | 1562 | |
a83bda05 YW |
1563 | r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE); |
1564 | if (r < 0) | |
87160186 | 1565 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for classless static route: %m"); |
a83bda05 | 1566 | } |
3c9b8860 | 1567 | |
7a169cb4 | 1568 | if (link_get_use_domains(link, NETWORK_CONFIG_SOURCE_DHCP4) > 0) { |
3cb62efe | 1569 | r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_DOMAIN_SEARCH); |
a83bda05 | 1570 | if (r < 0) |
87160186 | 1571 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for domain search list: %m"); |
a83bda05 | 1572 | } |
150d3b8e | 1573 | |
d12fb2bc | 1574 | if (link_get_use_ntp(link, NETWORK_CONFIG_SOURCE_DHCP4)) { |
a83bda05 YW |
1575 | r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_NTP_SERVER); |
1576 | if (r < 0) | |
87160186 | 1577 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for NTP server: %m"); |
a83bda05 | 1578 | } |
4b7b5abb | 1579 | |
a83bda05 YW |
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) | |
87160186 | 1583 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for SIP server: %m"); |
a83bda05 | 1584 | } |
86938158 | 1585 | |
3fd6708c | 1586 | if (link_get_use_dnr(link, NETWORK_CONFIG_SOURCE_DHCP4)) { |
86938158 RP |
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 | ||
edb88a72 RP |
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 | } | |
299d578f | 1597 | |
a83bda05 | 1598 | if (link->network->dhcp_use_timezone) { |
3cb62efe | 1599 | r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_TZDB_TIMEZONE); |
a83bda05 | 1600 | if (r < 0) |
87160186 | 1601 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for timezone: %m"); |
a83bda05 | 1602 | } |
8eb9058d | 1603 | |
e49bad01 YW |
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 | ||
fc35a9f8 YW |
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 | ||
a83bda05 YW |
1616 | SET_FOREACH(request_options, link->network->dhcp_request_options) { |
1617 | uint32_t option = PTR_TO_UINT32(request_options); | |
5bc945be | 1618 | |
a83bda05 YW |
1619 | r = sd_dhcp_client_set_request_option(link->dhcp_client, option); |
1620 | if (r < 0) | |
87160186 | 1621 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for '%u': %m", option); |
a83bda05 | 1622 | } |
7354900d | 1623 | |
a83bda05 YW |
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) | |
87160186 | 1629 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set send option: %m"); |
a83bda05 | 1630 | } |
cb29c156 | 1631 | |
a83bda05 YW |
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) | |
87160186 | 1637 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set send option: %m"); |
a83bda05 | 1638 | } |
3c9b8860 | 1639 | |
a83bda05 | 1640 | r = dhcp4_set_hostname(link); |
3c9b8860 | 1641 | if (r < 0) |
a83bda05 | 1642 | return r; |
3c9b8860 | 1643 | |
a83bda05 YW |
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) | |
87160186 | 1648 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set vendor class identifier: %m"); |
a83bda05 | 1649 | } |
7b8d23a9 | 1650 | |
a83bda05 YW |
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) | |
87160186 | 1654 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set MUD URL: %m"); |
a83bda05 YW |
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) | |
87160186 | 1660 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set user class: %m"); |
a83bda05 | 1661 | } |
af1c0de0 SS |
1662 | } |
1663 | ||
62f12d75 | 1664 | if (link->network->dhcp_client_port > 0) { |
9faed222 SS |
1665 | r = sd_dhcp_client_set_client_port(link->dhcp_client, link->network->dhcp_client_port); |
1666 | if (r < 0) | |
87160186 | 1667 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set listen port: %m"); |
9faed222 | 1668 | } |
ea932bd3 MI |
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 | } | |
9faed222 | 1674 | |
715cedfb SS |
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) | |
87160186 | 1678 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set max attempts: %m"); |
715cedfb SS |
1679 | } |
1680 | ||
14dd873b | 1681 | if (link->network->dhcp_ip_service_type >= 0) { |
db5756f3 | 1682 | r = sd_dhcp_client_set_service_type(link->dhcp_client, link->network->dhcp_ip_service_type); |
afe42aef | 1683 | if (r < 0) |
87160186 | 1684 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set IP service type: %m"); |
afe42aef | 1685 | } |
0f3ff4ea | 1686 | |
ea577968 | 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 | ||
5fde4d37 YW |
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); | |
d6463307 | 1695 | if (r < 0) |
87160186 | 1696 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed set to lease lifetime: %m"); |
d6463307 SS |
1697 | } |
1698 | ||
0f3ff4ea | 1699 | return dhcp4_set_client_identifier(link); |
3c9b8860 | 1700 | } |
ca5ad760 | 1701 | |
d947f7f9 | 1702 | int dhcp4_update_mac(Link *link) { |
7fddb970 | 1703 | bool restart; |
d947f7f9 YW |
1704 | int r; |
1705 | ||
1706 | assert(link); | |
1707 | ||
1708 | if (!link->dhcp_client) | |
1709 | return 0; | |
1710 | ||
7fddb970 YW |
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, | |
ca2b7cd8 | 1719 | link->bcast_addr.length > 0 ? link->bcast_addr.bytes : NULL, |
14b66dbc | 1720 | link->hw_addr.length, link->iftype); |
d947f7f9 YW |
1721 | if (r < 0) |
1722 | return r; | |
1723 | ||
7fddb970 YW |
1724 | r = dhcp4_set_client_identifier(link); |
1725 | if (r < 0) | |
1726 | return r; | |
1727 | ||
1728 | if (restart) { | |
fc35a9f8 | 1729 | r = dhcp4_start(link); |
7fddb970 YW |
1730 | if (r < 0) |
1731 | return r; | |
1732 | } | |
1733 | ||
1734 | return 0; | |
d947f7f9 YW |
1735 | } |
1736 | ||
fc35a9f8 YW |
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) { | |
ccffa166 YW |
1762 | int r; |
1763 | ||
294f129b | 1764 | assert(link); |
fc35a9f8 | 1765 | assert(link->network); |
294f129b YW |
1766 | |
1767 | if (!link->dhcp_client) | |
1768 | return 0; | |
1769 | ||
ccffa166 YW |
1770 | if (!link_has_carrier(link)) |
1771 | return 0; | |
1772 | ||
294f129b YW |
1773 | if (sd_dhcp_client_is_running(link->dhcp_client) > 0) |
1774 | return 0; | |
1775 | ||
ccffa166 YW |
1776 | r = sd_dhcp_client_start(link->dhcp_client); |
1777 | if (r < 0) | |
1778 | return r; | |
294f129b | 1779 | |
fc35a9f8 YW |
1780 | if (set_ipv6_connectivity) { |
1781 | r = dhcp4_update_ipv6_connectivity(link); | |
1782 | if (r < 0) | |
1783 | return r; | |
1784 | } | |
1785 | ||
ccffa166 YW |
1786 | return 1; |
1787 | } | |
1788 | ||
0bc30a20 YW |
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 | ||
ccffa166 YW |
1808 | static int dhcp4_configure_duid(Link *link) { |
1809 | assert(link); | |
d8f19e03 | 1810 | assert(link->network); |
ccffa166 | 1811 | |
d8f19e03 | 1812 | if (link->network->dhcp_client_identifier != DHCP_CLIENT_ID_DUID) |
ccffa166 YW |
1813 | return 1; |
1814 | ||
1815 | return dhcp_configure_duid(link, link_get_dhcp4_duid(link)); | |
1816 | } | |
1817 | ||
09d09207 | 1818 | static int dhcp4_process_request(Request *req, Link *link, void *userdata) { |
ccffa166 YW |
1819 | int r; |
1820 | ||
ff51134c | 1821 | assert(link); |
ccffa166 | 1822 | |
4b482e8b | 1823 | if (!link_is_ready_to_configure(link, /* allow_unmanaged = */ false)) |
ccffa166 YW |
1824 | return 0; |
1825 | ||
1826 | r = dhcp4_configure_duid(link); | |
1827 | if (r <= 0) | |
1828 | return r; | |
1829 | ||
ff51134c | 1830 | r = dhcp4_configure(link); |
ccffa166 YW |
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" : ""); | |
ccffa166 YW |
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 | ||
09d09207 | 1854 | r = link_queue_request(link, REQUEST_TYPE_DHCP4_CLIENT, dhcp4_process_request, NULL); |
ccffa166 YW |
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; | |
294f129b YW |
1860 | } |
1861 | ||
451c2baf | 1862 | int link_drop_dhcp4_config(Link *link, Network *network) { |
1f872757 | 1863 | int r, ret = 0; |
451c2baf YW |
1864 | |
1865 | assert(link); | |
82df2e0f | 1866 | assert(link->network); |
451c2baf | 1867 | |
82df2e0f YW |
1868 | if (link->network == network) |
1869 | return 0; /* .network file is unchanged. It is not necessary to reconfigure the client. */ | |
451c2baf | 1870 | |
82df2e0f YW |
1871 | if (!link_dhcp4_enabled(link)) { |
1872 | /* DHCP client is disabled. Stop the client if it is running and drop the lease. */ | |
451c2baf YW |
1873 | ret = sd_dhcp_client_stop(link->dhcp_client); |
1874 | ||
82df2e0f YW |
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 | ||
1f872757 YW |
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 | ||
451c2baf YW |
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 | ||
ca5ad760 YW |
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 | ||
99534007 | 1912 | Network *network = ASSERT_PTR(data); |
ca5ad760 YW |
1913 | uint64_t a; |
1914 | int r; | |
1915 | ||
ca5ad760 YW |
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")) { | |
f5fbe71d | 1925 | network->dhcp_max_attempts = UINT64_MAX; |
ca5ad760 YW |
1926 | return 0; |
1927 | } | |
1928 | ||
1929 | r = safe_atou64(rvalue, &a); | |
1930 | if (r < 0) { | |
d96edb2c | 1931 | log_syntax(unit, LOG_WARNING, filename, line, r, |
ca5ad760 YW |
1932 | "Failed to parse DHCP maximum attempts, ignoring: %s", rvalue); |
1933 | return 0; | |
1934 | } | |
1935 | ||
1936 | if (a == 0) { | |
d96edb2c | 1937 | log_syntax(unit, LOG_WARNING, filename, line, 0, |
ca5ad760 YW |
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 | ||
a75b2117 SS |
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 | ||
99534007 | 1959 | int *tos = ASSERT_PTR(data); |
b55093ce | 1960 | |
a75b2117 SS |
1961 | assert(filename); |
1962 | assert(lvalue); | |
1963 | assert(rvalue); | |
b55093ce YW |
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; | |
a75b2117 | 1971 | else if (streq(rvalue, "CS6")) |
b55093ce | 1972 | *tos = IPTOS_CLASS_CS6; |
a75b2117 SS |
1973 | else |
1974 | log_syntax(unit, LOG_WARNING, filename, line, 0, | |
b55093ce | 1975 | "Failed to parse %s=, ignoring assignment: %s", lvalue, rvalue); |
a75b2117 SS |
1976 | |
1977 | return 0; | |
1978 | } | |
1979 | ||
ea577968 | 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 | ||
63481576 YW |
2016 | int config_parse_dhcp_fallback_lease_lifetime( |
2017 | const char *unit, | |
d6463307 SS |
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) { | |
63481576 | 2027 | |
d6463307 | 2028 | Network *network = userdata; |
d6463307 SS |
2029 | |
2030 | assert(filename); | |
2031 | assert(section); | |
2032 | assert(lvalue); | |
2033 | assert(rvalue); | |
2034 | assert(data); | |
2035 | ||
2036 | if (isempty(rvalue)) { | |
5fde4d37 | 2037 | network->dhcp_fallback_lease_lifetime_usec = 0; |
d6463307 SS |
2038 | return 0; |
2039 | } | |
2040 | ||
2041 | /* We accept only "forever" or "infinity". */ | |
16bc8635 | 2042 | if (!STR_IN_SET(rvalue, "forever", "infinity")) { |
d96edb2c | 2043 | log_syntax(unit, LOG_WARNING, filename, line, 0, |
d6463307 SS |
2044 | "Invalid LeaseLifetime= value, ignoring: %s", rvalue); |
2045 | return 0; | |
2046 | } | |
2047 | ||
5fde4d37 | 2048 | network->dhcp_fallback_lease_lifetime_usec = USEC_INFINITY; |
d6463307 SS |
2049 | |
2050 | return 0; | |
2051 | } | |
2052 | ||
d419ef02 MR |
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 | ||
99534007 | 2065 | char **label = ASSERT_PTR(data); |
d419ef02 MR |
2066 | |
2067 | assert(filename); | |
2068 | assert(lvalue); | |
2069 | assert(rvalue); | |
d419ef02 MR |
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 | ||
ca5ad760 | 2085 | static const char* const dhcp_client_identifier_table[_DHCP_CLIENT_ID_MAX] = { |
d8f19e03 | 2086 | [DHCP_CLIENT_ID_MAC] = "mac", |
ca5ad760 | 2087 | [DHCP_CLIENT_ID_DUID] = "duid", |
ca5ad760 YW |
2088 | }; |
2089 | ||
2090 | DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(dhcp_client_identifier, DHCPClientIdentifier); | |
42efe5be | 2091 | DEFINE_CONFIG_PARSE_ENUM(config_parse_dhcp_client_identifier, dhcp_client_identifier, DHCPClientIdentifier); |