]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
3c9b8860 | 2 | |
9aa5d8ba | 3 | #include <netinet/in.h> |
a75b2117 | 4 | #include <netinet/ip.h> |
3c9b8860 | 5 | #include <linux/if.h> |
8f815e8b | 6 | #include <linux/if_arp.h> |
3c9b8860 | 7 | |
7b8d23a9 | 8 | #include "escape.h" |
b5efdb8a | 9 | #include "alloc-util.h" |
cb29c156 | 10 | #include "dhcp-client-internal.h" |
958b66ea | 11 | #include "hostname-util.h" |
64b21ece | 12 | #include "parse-util.h" |
3c9b8860 | 13 | #include "network-internal.h" |
093e3533 | 14 | #include "networkd-address.h" |
ca5ad760 | 15 | #include "networkd-dhcp4.h" |
23f53b99 TG |
16 | #include "networkd-link.h" |
17 | #include "networkd-manager.h" | |
18 | #include "networkd-network.h" | |
ca5ad760 | 19 | #include "string-table.h" |
64b21ece MV |
20 | #include "string-util.h" |
21 | #include "sysctl-util.h" | |
7b8d23a9 | 22 | #include "web-util.h" |
3c9b8860 | 23 | |
6906794d YW |
24 | static int dhcp4_update_address(Link *link, bool announce); |
25 | static int dhcp4_remove_all(Link *link); | |
d03073dd | 26 | |
c45fdad6 | 27 | static int dhcp4_release_old_lease(Link *link) { |
6e537f62 | 28 | Route *route; |
6e537f62 | 29 | int k, r = 0; |
d03073dd | 30 | |
6e537f62 | 31 | assert(link); |
d03073dd | 32 | |
6e537f62 YW |
33 | if (!link->dhcp_address_old && set_isempty(link->dhcp_routes_old)) |
34 | return 0; | |
d03073dd | 35 | |
6e537f62 | 36 | log_link_debug(link, "Removing old DHCPv4 address and routes."); |
d03073dd | 37 | |
d03073dd | 38 | link_dirty(link); |
6e537f62 | 39 | |
90e74a66 | 40 | SET_FOREACH(route, link->dhcp_routes_old) { |
ad208fac | 41 | k = route_remove(route, NULL, link, NULL); |
6e537f62 YW |
42 | if (k < 0) |
43 | r = k; | |
44 | } | |
45 | ||
46 | if (link->dhcp_address_old) { | |
47 | k = address_remove(link->dhcp_address_old, link, NULL); | |
48 | if (k < 0) | |
49 | r = k; | |
50 | } | |
51 | ||
52 | return r; | |
d03073dd YW |
53 | } |
54 | ||
c1d3fa29 | 55 | static void dhcp4_check_ready(Link *link) { |
6e537f62 YW |
56 | int r; |
57 | ||
153cf041 YW |
58 | if (link->network->dhcp_send_decline && !link->dhcp4_address_bind) |
59 | return; | |
60 | ||
61 | if (link->dhcp4_messages > 0) | |
62 | return; | |
63 | ||
64 | link->dhcp4_configured = true; | |
6e537f62 | 65 | |
153cf041 | 66 | /* New address and routes are configured now. Let's release old lease. */ |
c45fdad6 | 67 | r = dhcp4_release_old_lease(link); |
6e537f62 YW |
68 | if (r < 0) { |
69 | link_enter_failed(link); | |
70 | return; | |
71 | } | |
72 | ||
153cf041 | 73 | link_check_ready(link); |
c1d3fa29 YW |
74 | } |
75 | ||
302a796f | 76 | static int dhcp4_route_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
3c9b8860 TG |
77 | int r; |
78 | ||
79 | assert(link); | |
6cf4a01c | 80 | assert(link->dhcp4_messages > 0); |
3c9b8860 | 81 | |
313cefa1 | 82 | link->dhcp4_messages--; |
3c9b8860 | 83 | |
3ab7ed3f YW |
84 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
85 | return 1; | |
86 | ||
1c4baffc | 87 | r = sd_netlink_message_get_errno(m); |
dd9b10c8 YW |
88 | if (r == -ENETUNREACH && !link->dhcp4_route_retrying) { |
89 | ||
90 | /* It seems kernel does not support that the prefix route cannot be configured with | |
91 | * route table. Let's once drop the config and reconfigure them later. */ | |
92 | ||
81eb5bc5 | 93 | log_link_message_debug_errno(link, m, r, "Could not set DHCPv4 route, retrying later"); |
dd9b10c8 YW |
94 | link->dhcp4_route_failed = true; |
95 | link->manager->dhcp4_prefix_root_cannot_set_table = true; | |
96 | } else if (r < 0 && r != -EEXIST) { | |
81eb5bc5 | 97 | log_link_message_warning_errno(link, m, r, "Could not set DHCPv4 route"); |
3c9b8860 | 98 | link_enter_failed(link); |
3ab7ed3f | 99 | return 1; |
3c9b8860 TG |
100 | } |
101 | ||
153cf041 YW |
102 | if (link->dhcp4_messages == 0 && link->dhcp4_route_failed) { |
103 | link->dhcp4_route_failed = false; | |
104 | link->dhcp4_route_retrying = true; | |
dd9b10c8 | 105 | |
153cf041 YW |
106 | r = dhcp4_remove_all(link); |
107 | if (r < 0) | |
108 | link_enter_failed(link); | |
109 | return 1; | |
3c9b8860 TG |
110 | } |
111 | ||
153cf041 YW |
112 | dhcp4_check_ready(link); |
113 | ||
3c9b8860 TG |
114 | return 1; |
115 | } | |
116 | ||
d6eac9bd DW |
117 | static int route_scope_from_address(const Route *route, const struct in_addr *self_addr) { |
118 | assert(route); | |
119 | assert(self_addr); | |
120 | ||
7ed5420a YW |
121 | if (in4_addr_is_localhost(&route->dst.in) || |
122 | (!in4_addr_is_null(self_addr) && in4_addr_equal(&route->dst.in, self_addr))) | |
d6eac9bd DW |
123 | return RT_SCOPE_HOST; |
124 | else if (in4_addr_is_null(&route->gw.in)) | |
125 | return RT_SCOPE_LINK; | |
126 | else | |
127 | return RT_SCOPE_UNIVERSE; | |
128 | } | |
129 | ||
de697db0 YW |
130 | static bool link_prefixroute(Link *link) { |
131 | return !link->network->dhcp_route_table_set || | |
132 | link->network->dhcp_route_table == RT_TABLE_MAIN || | |
133 | link->manager->dhcp4_prefix_root_cannot_set_table; | |
156ddf8d YW |
134 | } |
135 | ||
6e537f62 YW |
136 | static int dhcp_route_configure(Route *route, Link *link) { |
137 | Route *ret; | |
d4c52ee5 YW |
138 | int r; |
139 | ||
140 | assert(route); | |
d4c52ee5 YW |
141 | assert(link); |
142 | ||
6e537f62 YW |
143 | r = route_configure(route, link, dhcp4_route_handler, &ret); |
144 | if (r < 0) | |
145 | return log_link_error_errno(link, r, "Failed to set DHCPv4 route: %m"); | |
d4c52ee5 YW |
146 | |
147 | link->dhcp4_messages++; | |
148 | ||
6e537f62 | 149 | r = set_ensure_put(&link->dhcp_routes, &route_hash_ops, ret); |
d4c52ee5 | 150 | if (r < 0) |
6e537f62 YW |
151 | return log_link_error_errno(link, r, "Failed to store DHCPv4 route: %m"); |
152 | ||
153 | (void) set_remove(link->dhcp_routes_old, ret); | |
d4c52ee5 | 154 | |
d4c52ee5 YW |
155 | return 0; |
156 | } | |
157 | ||
854a1ccf YW |
158 | static int link_set_dns_routes(Link *link, const struct in_addr *address) { |
159 | const struct in_addr *dns; | |
160 | uint32_t table; | |
161 | int i, n, r; | |
162 | ||
163 | assert(link); | |
164 | assert(link->dhcp_lease); | |
165 | assert(link->network); | |
166 | ||
a24e12f0 YW |
167 | if (!link->network->dhcp_use_dns || |
168 | !link->network->dhcp_routes_to_dns) | |
854a1ccf YW |
169 | return 0; |
170 | ||
171 | n = sd_dhcp_lease_get_dns(link->dhcp_lease, &dns); | |
172 | if (IN_SET(n, 0, -ENODATA)) | |
173 | return 0; | |
174 | if (n < 0) | |
175 | return log_link_warning_errno(link, n, "DHCP error: could not get DNS servers: %m"); | |
176 | ||
177 | table = link_get_dhcp_route_table(link); | |
178 | ||
179 | for (i = 0; i < n; i ++) { | |
180 | _cleanup_(route_freep) Route *route = NULL; | |
181 | ||
182 | r = route_new(&route); | |
183 | if (r < 0) | |
6906794d | 184 | return log_link_error_errno(link, r, "Could not allocate route: %m"); |
854a1ccf YW |
185 | |
186 | /* Set routes to DNS servers. */ | |
187 | ||
188 | route->family = AF_INET; | |
189 | route->dst.in = dns[i]; | |
190 | route->dst_prefixlen = 32; | |
191 | route->prefsrc.in = *address; | |
192 | route->scope = RT_SCOPE_LINK; | |
193 | route->protocol = RTPROT_DHCP; | |
194 | route->priority = link->network->dhcp_route_metric; | |
195 | route->table = table; | |
196 | ||
6e537f62 | 197 | r = dhcp_route_configure(route, link); |
854a1ccf YW |
198 | if (r < 0) |
199 | return log_link_error_errno(link, r, "Could not set route to DNS server: %m"); | |
200 | } | |
201 | ||
202 | return 0; | |
203 | } | |
204 | ||
e723fbd7 ZJS |
205 | static int dhcp_prefix_route_from_lease( |
206 | const sd_dhcp_lease *lease, | |
207 | uint32_t table, | |
208 | const struct in_addr *address, | |
209 | Route **ret_route) { | |
210 | ||
211 | Route *route; | |
212 | struct in_addr netmask; | |
213 | int r; | |
214 | ||
215 | r = sd_dhcp_lease_get_netmask((sd_dhcp_lease*) lease, &netmask); | |
216 | if (r < 0) | |
217 | return r; | |
218 | ||
219 | r = route_new(&route); | |
220 | if (r < 0) | |
221 | return r; | |
222 | ||
223 | route->family = AF_INET; | |
224 | route->dst.in.s_addr = address->s_addr & netmask.s_addr; | |
225 | route->dst_prefixlen = in4_addr_netmask_to_prefixlen(&netmask); | |
226 | route->prefsrc.in = *address; | |
227 | route->scope = RT_SCOPE_LINK; | |
228 | route->protocol = RTPROT_DHCP; | |
229 | route->table = table; | |
230 | *ret_route = route; | |
231 | return 0; | |
232 | } | |
233 | ||
3c9b8860 | 234 | static int link_set_dhcp_routes(Link *link) { |
f8693fc7 | 235 | _cleanup_free_ sd_dhcp_route **static_routes = NULL; |
8cdc46e7 | 236 | bool classless_route = false, static_route = false; |
f8862395 | 237 | struct in_addr address; |
fc1ba79d | 238 | uint32_t table; |
6e537f62 | 239 | Route *rt; |
2200c3cf | 240 | int r, n; |
3c9b8860 TG |
241 | |
242 | assert(link); | |
0c9b15a3 AJ |
243 | |
244 | if (!link->dhcp_lease) /* link went down while we configured the IP addresses? */ | |
245 | return 0; | |
246 | ||
247 | if (!link->network) /* link went down while we configured the IP addresses? */ | |
248 | return 0; | |
964b26fe | 249 | |
4e2ef9d9 YW |
250 | if (!link_has_carrier(link) && !link->network->configure_without_carrier) |
251 | /* During configuring addresses, the link lost its carrier. As networkd is dropping | |
252 | * the addresses now, let's not configure the routes either. */ | |
253 | return 0; | |
254 | ||
6e537f62 YW |
255 | while ((rt = set_steal_first(link->dhcp_routes))) { |
256 | r = set_ensure_put(&link->dhcp_routes_old, &route_hash_ops, rt); | |
257 | if (r < 0) | |
258 | return log_link_error_errno(link, r, "Failed to store old DHCPv4 route: %m"); | |
259 | } | |
d4c52ee5 | 260 | |
bdb9f580 | 261 | table = link_get_dhcp_route_table(link); |
fc1ba79d | 262 | |
b23aec0d DW |
263 | r = sd_dhcp_lease_get_address(link->dhcp_lease, &address); |
264 | if (r < 0) | |
265 | return log_link_warning_errno(link, r, "DHCP error: could not get address: %m"); | |
266 | ||
de697db0 | 267 | if (!link_prefixroute(link)) { |
156ddf8d | 268 | _cleanup_(route_freep) Route *prefix_route = NULL; |
156ddf8d | 269 | |
e723fbd7 | 270 | r = dhcp_prefix_route_from_lease(link->dhcp_lease, table, &address, &prefix_route); |
156ddf8d | 271 | if (r < 0) |
6906794d | 272 | return log_link_error_errno(link, r, "Could not create prefix route: %m"); |
156ddf8d | 273 | |
6e537f62 | 274 | r = dhcp_route_configure(prefix_route, link); |
156ddf8d YW |
275 | if (r < 0) |
276 | return log_link_error_errno(link, r, "Could not set prefix route: %m"); | |
277 | } | |
278 | ||
5f04a209 | 279 | n = sd_dhcp_lease_get_routes(link->dhcp_lease, &static_routes); |
599c44e6 YW |
280 | if (n == -ENODATA) |
281 | log_link_debug_errno(link, n, "DHCP: No routes received from DHCP server: %m"); | |
282 | else if (n < 0) | |
2200c3cf | 283 | return log_link_error_errno(link, n, "DHCP: could not get routes: %m"); |
5f04a209 | 284 | |
2200c3cf | 285 | for (int i = 0; i < n; i++) { |
3476951c TH |
286 | switch (sd_dhcp_route_get_option(static_routes[i])) { |
287 | case SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE: | |
8cdc46e7 | 288 | classless_route = true; |
3476951c TH |
289 | break; |
290 | case SD_DHCP_OPTION_STATIC_ROUTE: | |
8cdc46e7 | 291 | static_route = true; |
3476951c TH |
292 | break; |
293 | } | |
8cdc46e7 SS |
294 | } |
295 | ||
ad098b14 | 296 | if (link->network->dhcp_use_routes) { |
2200c3cf YW |
297 | /* if the DHCP server returns both a Classless Static Routes option and a Static Routes option, |
298 | * the DHCP client MUST ignore the Static Routes option. */ | |
299 | if (classless_route && static_route) | |
300 | log_link_warning(link, "Classless static routes received from DHCP server: ignoring static-route option"); | |
301 | ||
302 | for (int i = 0; i < n; i++) { | |
ad098b14 SS |
303 | _cleanup_(route_freep) Route *route = NULL; |
304 | ||
ad098b14 SS |
305 | if (classless_route && |
306 | sd_dhcp_route_get_option(static_routes[i]) != SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE) | |
307 | continue; | |
308 | ||
309 | r = route_new(&route); | |
310 | if (r < 0) | |
311 | return log_link_error_errno(link, r, "Could not allocate route: %m"); | |
312 | ||
313 | route->family = AF_INET; | |
314 | route->protocol = RTPROT_DHCP; | |
6dd53981 | 315 | route->gw_family = AF_INET; |
ad098b14 SS |
316 | assert_se(sd_dhcp_route_get_gateway(static_routes[i], &route->gw.in) >= 0); |
317 | assert_se(sd_dhcp_route_get_destination(static_routes[i], &route->dst.in) >= 0); | |
318 | assert_se(sd_dhcp_route_get_destination_prefix_length(static_routes[i], &route->dst_prefixlen) >= 0); | |
319 | route->priority = link->network->dhcp_route_metric; | |
320 | route->table = table; | |
321 | route->mtu = link->network->dhcp_route_mtu; | |
322 | route->scope = route_scope_from_address(route, &address); | |
323 | if (IN_SET(route->scope, RT_SCOPE_LINK, RT_SCOPE_UNIVERSE)) | |
324 | route->prefsrc.in = address; | |
325 | ||
326 | if (set_contains(link->dhcp_routes, route)) | |
327 | continue; | |
328 | ||
6e537f62 | 329 | r = dhcp_route_configure(route, link); |
ad098b14 SS |
330 | if (r < 0) |
331 | return log_link_error_errno(link, r, "Could not set route: %m"); | |
332 | } | |
5f04a209 SS |
333 | } |
334 | ||
244490f5 | 335 | if (link->network->dhcp_use_gateway) { |
2200c3cf YW |
336 | const struct in_addr *router; |
337 | ||
244490f5 DS |
338 | r = sd_dhcp_lease_get_router(link->dhcp_lease, &router); |
339 | if (IN_SET(r, 0, -ENODATA)) | |
340 | log_link_info(link, "DHCP: No gateway received from DHCP server."); | |
341 | else if (r < 0) | |
2200c3cf | 342 | return log_link_error_errno(link, r, "DHCP error: could not get gateway: %m"); |
244490f5 DS |
343 | else if (in4_addr_is_null(&router[0])) |
344 | log_link_info(link, "DHCP: Received gateway is null."); | |
2200c3cf YW |
345 | else if (classless_route) |
346 | /* According to RFC 3442: If the DHCP server returns both a Classless Static Routes option and | |
347 | * a Router option, the DHCP client MUST ignore the Router option. */ | |
348 | log_link_warning(link, "Classless static routes received from DHCP server: ignoring router option"); | |
349 | else { | |
244490f5 DS |
350 | _cleanup_(route_freep) Route *route = NULL, *route_gw = NULL; |
351 | ||
352 | r = route_new(&route_gw); | |
353 | if (r < 0) | |
6906794d | 354 | return log_link_error_errno(link, r, "Could not allocate route: %m"); |
244490f5 DS |
355 | |
356 | /* The dhcp netmask may mask out the gateway. Add an explicit | |
357 | * route for the gw host so that we can route no matter the | |
358 | * netmask or existing kernel route tables. */ | |
359 | route_gw->family = AF_INET; | |
360 | route_gw->dst.in = router[0]; | |
361 | route_gw->dst_prefixlen = 32; | |
362 | route_gw->prefsrc.in = address; | |
363 | route_gw->scope = RT_SCOPE_LINK; | |
364 | route_gw->protocol = RTPROT_DHCP; | |
365 | route_gw->priority = link->network->dhcp_route_metric; | |
366 | route_gw->table = table; | |
367 | route_gw->mtu = link->network->dhcp_route_mtu; | |
368 | ||
6e537f62 | 369 | r = dhcp_route_configure(route_gw, link); |
244490f5 DS |
370 | if (r < 0) |
371 | return log_link_error_errno(link, r, "Could not set host route: %m"); | |
3c9b8860 | 372 | |
244490f5 DS |
373 | r = route_new(&route); |
374 | if (r < 0) | |
375 | return log_link_error_errno(link, r, "Could not allocate route: %m"); | |
860e636c | 376 | |
244490f5 | 377 | route->family = AF_INET; |
6dd53981 | 378 | route->gw_family = AF_INET; |
244490f5 DS |
379 | route->gw.in = router[0]; |
380 | route->prefsrc.in = address; | |
381 | route->protocol = RTPROT_DHCP; | |
382 | route->priority = link->network->dhcp_route_metric; | |
383 | route->table = table; | |
384 | route->mtu = link->network->dhcp_route_mtu; | |
3c9b8860 | 385 | |
6e537f62 | 386 | r = dhcp_route_configure(route, link); |
244490f5 DS |
387 | if (r < 0) |
388 | return log_link_error_errno(link, r, "Could not set router: %m"); | |
3c9b8860 | 389 | |
2a54a044 | 390 | HASHMAP_FOREACH(rt, link->network->routes_by_section) { |
2200c3cf YW |
391 | if (!rt->gateway_from_dhcp) |
392 | continue; | |
1985c54f | 393 | |
2200c3cf YW |
394 | if (rt->family != AF_INET) |
395 | continue; | |
1985c54f | 396 | |
6dd53981 | 397 | rt->gw_family = AF_INET; |
2200c3cf | 398 | rt->gw.in = router[0]; |
1985c54f | 399 | |
2200c3cf YW |
400 | r = dhcp_route_configure(rt, link); |
401 | if (r < 0) | |
402 | return log_link_error_errno(link, r, "Could not set gateway: %m"); | |
403 | } | |
244490f5 | 404 | } |
1985c54f YW |
405 | } |
406 | ||
854a1ccf | 407 | return link_set_dns_routes(link, &address); |
3c9b8860 TG |
408 | } |
409 | ||
7fa472f9 YW |
410 | static int dhcp_reset_mtu(Link *link) { |
411 | uint16_t mtu; | |
412 | int r; | |
413 | ||
414 | assert(link); | |
415 | ||
416 | if (!link->network->dhcp_use_mtu) | |
417 | return 0; | |
418 | ||
419 | r = sd_dhcp_lease_get_mtu(link->dhcp_lease, &mtu); | |
46b875fb YW |
420 | if (r == -ENODATA) |
421 | return 0; | |
7fa472f9 | 422 | if (r < 0) |
46b875fb | 423 | return log_link_error_errno(link, r, "DHCP error: failed to get MTU from lease: %m"); |
7fa472f9 YW |
424 | |
425 | if (link->original_mtu == mtu) | |
426 | return 0; | |
427 | ||
428 | r = link_set_mtu(link, link->original_mtu); | |
46b875fb YW |
429 | if (r < 0) |
430 | return log_link_error_errno(link, r, "DHCP error: could not reset MTU: %m"); | |
3c9b8860 | 431 | |
7fa472f9 YW |
432 | return 0; |
433 | } | |
434 | ||
435 | static int dhcp_reset_hostname(Link *link) { | |
436 | const char *hostname; | |
437 | int r; | |
438 | ||
439 | assert(link); | |
440 | ||
441 | if (!link->network->dhcp_use_hostname) | |
442 | return 0; | |
443 | ||
444 | hostname = link->network->dhcp_hostname; | |
445 | if (!hostname) | |
446 | (void) sd_dhcp_lease_get_hostname(link->dhcp_lease, &hostname); | |
447 | ||
448 | if (!hostname) | |
449 | return 0; | |
450 | ||
451 | /* If a hostname was set due to the lease, then unset it now. */ | |
452 | r = manager_set_hostname(link->manager, NULL); | |
453 | if (r < 0) | |
454 | return log_link_error_errno(link, r, "DHCP error: Failed to reset transient hostname: %m"); | |
455 | ||
456 | return 0; | |
457 | } | |
458 | ||
6906794d YW |
459 | static int dhcp4_remove_route_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
460 | int r; | |
461 | ||
462 | assert(m); | |
463 | assert(link); | |
464 | assert(link->dhcp4_remove_messages > 0); | |
465 | ||
466 | link->dhcp4_remove_messages--; | |
467 | ||
468 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
469 | return 1; | |
470 | ||
471 | r = sd_netlink_message_get_errno(m); | |
472 | if (r < 0 && r != -ESRCH) | |
473 | log_link_message_warning_errno(link, m, r, "Failed to remove DHCPv4 route, ignoring"); | |
474 | ||
475 | if (link->dhcp4_remove_messages == 0) { | |
476 | r = dhcp4_update_address(link, false); | |
477 | if (r < 0) | |
478 | link_enter_failed(link); | |
479 | } | |
480 | ||
481 | return 1; | |
482 | } | |
483 | ||
484 | static int dhcp4_remove_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { | |
485 | int r; | |
486 | ||
487 | assert(m); | |
488 | assert(link); | |
489 | assert(link->dhcp4_remove_messages > 0); | |
490 | ||
491 | link->dhcp4_remove_messages--; | |
492 | ||
493 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
494 | return 1; | |
495 | ||
496 | r = sd_netlink_message_get_errno(m); | |
497 | if (r < 0 && r != -EADDRNOTAVAIL) | |
498 | log_link_message_warning_errno(link, m, r, "Failed to remove DHCPv4 address, ignoring"); | |
499 | else | |
500 | (void) manager_rtnl_process_address(rtnl, m, link->manager); | |
501 | ||
502 | if (link->dhcp4_remove_messages == 0) { | |
503 | r = dhcp4_update_address(link, false); | |
504 | if (r < 0) | |
505 | link_enter_failed(link); | |
506 | } | |
507 | ||
508 | return 1; | |
509 | } | |
510 | ||
511 | static int dhcp4_remove_all(Link *link) { | |
6e537f62 | 512 | Route *route; |
6e537f62 | 513 | int k, r = 0; |
6906794d YW |
514 | |
515 | assert(link); | |
6906794d | 516 | |
90e74a66 | 517 | SET_FOREACH(route, link->dhcp_routes) { |
ad208fac | 518 | k = route_remove(route, NULL, link, dhcp4_remove_route_handler); |
6e537f62 YW |
519 | if (k < 0) |
520 | r = k; | |
521 | else | |
522 | link->dhcp4_remove_messages++; | |
523 | } | |
6906794d | 524 | |
6e537f62 YW |
525 | if (link->dhcp_address) { |
526 | k = address_remove(link->dhcp_address, link, dhcp4_remove_address_handler); | |
527 | if (k < 0) | |
528 | r = k; | |
529 | else | |
530 | link->dhcp4_remove_messages++; | |
531 | } | |
6906794d | 532 | |
6e537f62 | 533 | return r; |
6906794d YW |
534 | } |
535 | ||
7fa472f9 | 536 | static int dhcp_lease_lost(Link *link) { |
2ac7eec3 | 537 | int k, r = 0; |
7fa472f9 YW |
538 | |
539 | assert(link); | |
540 | assert(link->dhcp_lease); | |
541 | ||
f8c2e4b9 | 542 | log_link_info(link, "DHCP lease lost"); |
7fa472f9 YW |
543 | |
544 | link->dhcp4_configured = false; | |
545 | ||
75be72d1 | 546 | /* dhcp_lease_lost() may be called during renewing IP address. */ |
c45fdad6 | 547 | k = dhcp4_release_old_lease(link); |
6e537f62 YW |
548 | if (k < 0) |
549 | r = k; | |
75be72d1 | 550 | |
6e537f62 YW |
551 | k = dhcp4_remove_all(link); |
552 | if (k < 0) | |
553 | r = k; | |
6906794d | 554 | |
6e537f62 YW |
555 | k = dhcp_reset_mtu(link); |
556 | if (k < 0) | |
557 | r = k; | |
6906794d | 558 | |
6e537f62 YW |
559 | k = dhcp_reset_hostname(link); |
560 | if (k < 0) | |
561 | r = k; | |
7fa472f9 | 562 | |
3c9b8860 | 563 | link->dhcp_lease = sd_dhcp_lease_unref(link->dhcp_lease); |
6a3e5f6a | 564 | link_dirty(link); |
3c9b8860 | 565 | |
6e537f62 | 566 | return r; |
3c9b8860 TG |
567 | } |
568 | ||
0f3ff4ea SS |
569 | static void dhcp_address_on_acd(sd_ipv4acd *acd, int event, void *userdata) { |
570 | _cleanup_free_ char *pretty = NULL; | |
571 | union in_addr_union address = {}; | |
572 | Link *link; | |
573 | int r; | |
574 | ||
575 | assert(acd); | |
576 | assert(userdata); | |
577 | ||
578 | link = userdata; | |
579 | ||
580 | switch (event) { | |
581 | case SD_IPV4ACD_EVENT_STOP: | |
582 | log_link_debug(link, "Stopping ACD client for DHCP4..."); | |
583 | return; | |
584 | ||
585 | case SD_IPV4ACD_EVENT_BIND: | |
586 | if (DEBUG_LOGGING) { | |
587 | (void) sd_dhcp_lease_get_address(link->dhcp_lease, &address.in); | |
588 | (void) in_addr_to_string(AF_INET, &address, &pretty); | |
589 | log_link_debug(link, "Successfully claimed DHCP4 address %s", strna(pretty)); | |
590 | } | |
153cf041 | 591 | link->dhcp4_address_bind = true; |
c1d3fa29 | 592 | dhcp4_check_ready(link); |
0f3ff4ea SS |
593 | break; |
594 | ||
595 | case SD_IPV4ACD_EVENT_CONFLICT: | |
596 | (void) sd_dhcp_lease_get_address(link->dhcp_lease, &address.in); | |
597 | (void) in_addr_to_string(AF_INET, &address, &pretty); | |
598 | log_link_warning(link, "DAD conflict. Dropping DHCP4 address %s", strna(pretty)); | |
599 | ||
f766d9af YW |
600 | r = sd_dhcp_client_send_decline(link->dhcp_client); |
601 | if (r < 0) | |
602 | log_link_warning_errno(link, r, "Failed to send DHCP DECLINE, ignoring: %m"); | |
0f3ff4ea SS |
603 | |
604 | if (link->dhcp_lease) { | |
605 | r = dhcp_lease_lost(link); | |
606 | if (r < 0) | |
607 | link_enter_failed(link); | |
608 | } | |
609 | break; | |
610 | ||
611 | default: | |
612 | assert_not_reached("Invalid IPv4ACD event."); | |
613 | } | |
614 | ||
dce1cd41 | 615 | (void) sd_ipv4acd_stop(acd); |
0f3ff4ea SS |
616 | |
617 | return; | |
618 | } | |
619 | ||
620 | static int configure_dhcpv4_duplicate_address_detection(Link *link) { | |
621 | int r; | |
622 | ||
623 | assert(link); | |
624 | ||
625 | r = sd_ipv4acd_new(&link->network->dhcp_acd); | |
626 | if (r < 0) | |
627 | return r; | |
628 | ||
4cf85000 | 629 | r = sd_ipv4acd_attach_event(link->network->dhcp_acd, link->manager->event, 0); |
0f3ff4ea SS |
630 | if (r < 0) |
631 | return r; | |
632 | ||
633 | r = sd_ipv4acd_set_ifindex(link->network->dhcp_acd, link->ifindex); | |
634 | if (r < 0) | |
635 | return r; | |
636 | ||
637 | r = sd_ipv4acd_set_mac(link->network->dhcp_acd, &link->mac); | |
638 | if (r < 0) | |
639 | return r; | |
640 | ||
641 | return 0; | |
642 | } | |
643 | ||
153cf041 YW |
644 | static int dhcp4_start_acd(Link *link) { |
645 | union in_addr_union addr; | |
687b3bc6 | 646 | struct in_addr old; |
153cf041 YW |
647 | int r; |
648 | ||
649 | if (!link->network->dhcp_send_decline) | |
650 | return 0; | |
651 | ||
652 | if (!link->dhcp_lease) | |
653 | return 0; | |
654 | ||
5acf54a0 YW |
655 | (void) sd_ipv4acd_stop(link->network->dhcp_acd); |
656 | ||
153cf041 YW |
657 | link->dhcp4_address_bind = false; |
658 | ||
659 | r = sd_dhcp_lease_get_address(link->dhcp_lease, &addr.in); | |
660 | if (r < 0) | |
661 | return r; | |
662 | ||
687b3bc6 YW |
663 | r = sd_ipv4acd_get_address(link->network->dhcp_acd, &old); |
664 | if (r < 0) | |
665 | return r; | |
666 | ||
153cf041 YW |
667 | r = sd_ipv4acd_set_address(link->network->dhcp_acd, &addr.in); |
668 | if (r < 0) | |
669 | return r; | |
670 | ||
671 | r = sd_ipv4acd_set_callback(link->network->dhcp_acd, dhcp_address_on_acd, link); | |
672 | if (r < 0) | |
673 | return r; | |
674 | ||
675 | if (DEBUG_LOGGING) { | |
676 | _cleanup_free_ char *pretty = NULL; | |
677 | ||
678 | (void) in_addr_to_string(AF_INET, &addr, &pretty); | |
679 | log_link_debug(link, "Starting IPv4ACD client. Probing DHCPv4 address %s", strna(pretty)); | |
680 | } | |
681 | ||
687b3bc6 | 682 | r = sd_ipv4acd_start(link->network->dhcp_acd, !in4_addr_equal(&addr.in, &old)); |
153cf041 YW |
683 | if (r < 0) |
684 | return r; | |
685 | ||
686 | return 1; | |
687 | } | |
688 | ||
c45fdad6 YW |
689 | static int dhcp4_address_ready_callback(Address *address) { |
690 | Link *link; | |
691 | int r; | |
692 | ||
693 | assert(address); | |
694 | ||
695 | link = address->link; | |
696 | ||
697 | /* Do not call this again. */ | |
698 | address->callback = NULL; | |
699 | ||
700 | r = link_set_dhcp_routes(link); | |
701 | if (r < 0) | |
702 | return r; | |
703 | ||
704 | /* Reconfigure static routes as kernel may remove some routes when lease expires. */ | |
141318f7 | 705 | r = link_set_routes(link); |
c45fdad6 YW |
706 | if (r < 0) |
707 | return r; | |
708 | ||
709 | r = dhcp4_start_acd(link); | |
710 | if (r < 0) | |
69e3234d | 711 | return log_link_error_errno(link, r, "Failed to start IPv4ACD for DHCP4 address: %m"); |
c45fdad6 YW |
712 | |
713 | dhcp4_check_ready(link); | |
714 | return 0; | |
715 | } | |
716 | ||
302a796f | 717 | static int dhcp4_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
3c9b8860 TG |
718 | int r; |
719 | ||
720 | assert(link); | |
721 | ||
3ab7ed3f YW |
722 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
723 | return 1; | |
724 | ||
1c4baffc | 725 | r = sd_netlink_message_get_errno(m); |
3c9b8860 | 726 | if (r < 0 && r != -EEXIST) { |
5ecb131d | 727 | log_link_message_warning_errno(link, m, r, "Could not set DHCPv4 address"); |
3c9b8860 | 728 | link_enter_failed(link); |
3ab7ed3f | 729 | return 1; |
4ff296b0 YW |
730 | } else if (r >= 0) |
731 | (void) manager_rtnl_process_address(rtnl, m, link->manager); | |
3c9b8860 | 732 | |
c45fdad6 YW |
733 | if (address_is_ready(link->dhcp_address)) { |
734 | r = dhcp4_address_ready_callback(link->dhcp_address); | |
735 | if (r < 0) { | |
736 | link_enter_failed(link); | |
737 | return 1; | |
738 | } | |
739 | } else | |
740 | link->dhcp_address->callback = dhcp4_address_ready_callback; | |
0f3ff4ea | 741 | |
3c9b8860 TG |
742 | return 1; |
743 | } | |
744 | ||
6906794d | 745 | static int dhcp4_update_address(Link *link, bool announce) { |
8e766630 | 746 | _cleanup_(address_freep) Address *addr = NULL; |
6906794d YW |
747 | uint32_t lifetime = CACHE_INFO_INFINITY_LIFE_TIME; |
748 | struct in_addr address, netmask; | |
3c9b8860 | 749 | unsigned prefixlen; |
6e537f62 | 750 | Address *ret; |
3c9b8860 TG |
751 | int r; |
752 | ||
6906794d YW |
753 | assert(link); |
754 | assert(link->network); | |
755 | ||
756 | if (!link->dhcp_lease) | |
757 | return 0; | |
758 | ||
759 | link_set_state(link, LINK_STATE_CONFIGURING); | |
760 | link->dhcp4_configured = false; | |
3c9b8860 | 761 | |
141318f7 YW |
762 | /* address_handler calls link_set_routes() and link_set_nexthop(). Before they are called, the |
763 | * related flags must be cleared. Otherwise, the link becomes configured state before routes | |
764 | * are configured. */ | |
0c816fcc YW |
765 | link->static_routes_configured = false; |
766 | link->static_nexthops_configured = false; | |
767 | ||
6906794d YW |
768 | r = sd_dhcp_lease_get_address(link->dhcp_lease, &address); |
769 | if (r < 0) | |
770 | return log_link_warning_errno(link, r, "DHCP error: no address: %m"); | |
771 | ||
772 | r = sd_dhcp_lease_get_netmask(link->dhcp_lease, &netmask); | |
773 | if (r < 0) | |
774 | return log_link_warning_errno(link, r, "DHCP error: no netmask: %m"); | |
775 | ||
776 | if (!FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) { | |
777 | r = sd_dhcp_lease_get_lifetime(link->dhcp_lease, &lifetime); | |
778 | if (r < 0) | |
779 | return log_link_warning_errno(link, r, "DHCP error: no lifetime: %m"); | |
780 | } | |
781 | ||
782 | prefixlen = in4_addr_netmask_to_prefixlen(&netmask); | |
783 | ||
784 | if (announce) { | |
785 | const struct in_addr *router; | |
786 | ||
787 | r = sd_dhcp_lease_get_router(link->dhcp_lease, &router); | |
788 | if (r < 0 && r != -ENODATA) | |
789 | return log_link_error_errno(link, r, "DHCP error: Could not get gateway: %m"); | |
790 | ||
791 | if (r > 0 && !in4_addr_is_null(&router[0])) | |
792 | log_struct(LOG_INFO, | |
793 | LOG_LINK_INTERFACE(link), | |
794 | LOG_LINK_MESSAGE(link, "DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u", | |
795 | ADDRESS_FMT_VAL(address), | |
796 | prefixlen, | |
797 | ADDRESS_FMT_VAL(router[0])), | |
798 | "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address), | |
799 | "PREFIXLEN=%u", prefixlen, | |
800 | "GATEWAY=%u.%u.%u.%u", ADDRESS_FMT_VAL(router[0])); | |
801 | else | |
802 | log_struct(LOG_INFO, | |
803 | LOG_LINK_INTERFACE(link), | |
804 | LOG_LINK_MESSAGE(link, "DHCPv4 address %u.%u.%u.%u/%u", | |
805 | ADDRESS_FMT_VAL(address), | |
806 | prefixlen), | |
807 | "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address), | |
808 | "PREFIXLEN=%u", prefixlen); | |
809 | } | |
3c9b8860 | 810 | |
f0213e37 | 811 | r = address_new(&addr); |
3c9b8860 | 812 | if (r < 0) |
6906794d | 813 | return log_oom(); |
3c9b8860 TG |
814 | |
815 | addr->family = AF_INET; | |
6906794d | 816 | addr->in_addr.in.s_addr = address.s_addr; |
3c9b8860 TG |
817 | addr->cinfo.ifa_prefered = lifetime; |
818 | addr->cinfo.ifa_valid = lifetime; | |
819 | addr->prefixlen = prefixlen; | |
6906794d | 820 | addr->broadcast.s_addr = address.s_addr | ~netmask.s_addr; |
eaff204f | 821 | SET_FLAG(addr->flags, IFA_F_NOPREFIXROUTE, !link_prefixroute(link)); |
3c9b8860 | 822 | |
66669078 TG |
823 | /* allow reusing an existing address and simply update its lifetime |
824 | * in case it already exists */ | |
6e537f62 | 825 | r = address_configure(addr, link, dhcp4_address_handler, true, &ret); |
3c9b8860 | 826 | if (r < 0) |
6e537f62 YW |
827 | return log_link_error_errno(link, r, "Failed to set DHCPv4 address: %m"); |
828 | ||
829 | if (!address_equal(link->dhcp_address, ret)) | |
830 | link->dhcp_address_old = link->dhcp_address; | |
831 | link->dhcp_address = ret; | |
3c9b8860 TG |
832 | |
833 | return 0; | |
834 | } | |
835 | ||
836 | static int dhcp_lease_renew(sd_dhcp_client *client, Link *link) { | |
837 | sd_dhcp_lease *lease; | |
3c9b8860 TG |
838 | int r; |
839 | ||
840 | assert(link); | |
841 | assert(client); | |
3c9b8860 TG |
842 | |
843 | r = sd_dhcp_client_get_lease(client, &lease); | |
f6b8196f LP |
844 | if (r < 0) |
845 | return log_link_warning_errno(link, r, "DHCP error: no lease: %m"); | |
3c9b8860 TG |
846 | |
847 | sd_dhcp_lease_unref(link->dhcp_lease); | |
e6b18ffa | 848 | link->dhcp_lease = sd_dhcp_lease_ref(lease); |
6a3e5f6a | 849 | link_dirty(link); |
3c9b8860 | 850 | |
6906794d | 851 | return dhcp4_update_address(link, false); |
3c9b8860 TG |
852 | } |
853 | ||
854 | static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) { | |
855 | sd_dhcp_lease *lease; | |
3c9b8860 TG |
856 | int r; |
857 | ||
858 | assert(client); | |
859 | assert(link); | |
860 | ||
861 | r = sd_dhcp_client_get_lease(client, &lease); | |
f2341e0a | 862 | if (r < 0) |
f6b8196f | 863 | return log_link_error_errno(link, r, "DHCP error: No lease: %m"); |
3c9b8860 | 864 | |
6906794d | 865 | sd_dhcp_lease_unref(link->dhcp_lease); |
e6b18ffa | 866 | link->dhcp_lease = sd_dhcp_lease_ref(lease); |
6a3e5f6a | 867 | link_dirty(link); |
3c9b8860 | 868 | |
27cb34f5 | 869 | if (link->network->dhcp_use_mtu) { |
3c9b8860 TG |
870 | uint16_t mtu; |
871 | ||
872 | r = sd_dhcp_lease_get_mtu(lease, &mtu); | |
873 | if (r >= 0) { | |
933c70a0 | 874 | r = link_set_mtu(link, mtu); |
3c9b8860 | 875 | if (r < 0) |
f2341e0a | 876 | log_link_error_errno(link, r, "Failed to set MTU to %" PRIu16 ": %m", mtu); |
3c9b8860 TG |
877 | } |
878 | } | |
879 | ||
27cb34f5 | 880 | if (link->network->dhcp_use_hostname) { |
2de2abad LB |
881 | const char *dhcpname = NULL; |
882 | _cleanup_free_ char *hostname = NULL; | |
3c9b8860 | 883 | |
27cb34f5 | 884 | if (link->network->dhcp_hostname) |
2de2abad | 885 | dhcpname = link->network->dhcp_hostname; |
dce391e7 | 886 | else |
2de2abad LB |
887 | (void) sd_dhcp_lease_get_hostname(lease, &dhcpname); |
888 | ||
889 | if (dhcpname) { | |
890 | r = shorten_overlong(dhcpname, &hostname); | |
891 | if (r < 0) | |
892 | log_link_warning_errno(link, r, "Unable to shorten overlong DHCP hostname '%s', ignoring: %m", dhcpname); | |
893 | if (r == 1) | |
5238e957 | 894 | log_link_notice(link, "Overlong DHCP hostname received, shortened from '%s' to '%s'", dhcpname, hostname); |
2de2abad | 895 | } |
a7d0ef44 | 896 | |
dce391e7 | 897 | if (hostname) { |
59eb33e0 | 898 | r = manager_set_hostname(link->manager, hostname); |
3c9b8860 | 899 | if (r < 0) |
f2341e0a | 900 | log_link_error_errno(link, r, "Failed to set transient hostname to '%s': %m", hostname); |
3c9b8860 TG |
901 | } |
902 | } | |
903 | ||
27cb34f5 | 904 | if (link->network->dhcp_use_timezone) { |
21b80ad1 LP |
905 | const char *tz = NULL; |
906 | ||
907 | (void) sd_dhcp_lease_get_timezone(link->dhcp_lease, &tz); | |
908 | ||
909 | if (tz) { | |
59eb33e0 | 910 | r = manager_set_timezone(link->manager, tz); |
21b80ad1 LP |
911 | if (r < 0) |
912 | log_link_error_errno(link, r, "Failed to set timezone to '%s': %m", tz); | |
913 | } | |
914 | } | |
915 | ||
6906794d YW |
916 | if (link->dhcp4_remove_messages == 0) { |
917 | r = dhcp4_update_address(link, true); | |
918 | if (r < 0) | |
919 | return r; | |
920 | } else | |
921 | log_link_debug(link, | |
922 | "The link has previously assigned DHCPv4 address or routes. " | |
923 | "The newly assigned address and routes will set up after old ones are removed."); | |
3c9b8860 TG |
924 | |
925 | return 0; | |
926 | } | |
8bc17bb3 | 927 | |
d03073dd YW |
928 | static int dhcp_lease_ip_change(sd_dhcp_client *client, Link *link) { |
929 | int r; | |
930 | ||
d03073dd | 931 | r = dhcp_lease_acquired(client, link); |
6e537f62 | 932 | if (r < 0) |
d03073dd | 933 | (void) dhcp_lease_lost(link); |
d03073dd | 934 | |
6e537f62 | 935 | return r; |
d03073dd YW |
936 | } |
937 | ||
6b000af4 | 938 | static int dhcp_server_is_deny_listed(Link *link, sd_dhcp_client *client) { |
727b5734 SS |
939 | sd_dhcp_lease *lease; |
940 | struct in_addr addr; | |
941 | int r; | |
942 | ||
943 | assert(link); | |
944 | assert(link->network); | |
945 | assert(client); | |
946 | ||
947 | r = sd_dhcp_client_get_lease(client, &lease); | |
948 | if (r < 0) | |
949 | return log_link_error_errno(link, r, "Failed to get DHCP lease: %m"); | |
950 | ||
951 | r = sd_dhcp_lease_get_server_identifier(lease, &addr); | |
952 | if (r < 0) | |
0da425df | 953 | return log_link_debug_errno(link, r, "Failed to get DHCP server IP address: %m"); |
727b5734 | 954 | |
6b000af4 | 955 | if (set_contains(link->network->dhcp_deny_listed_ip, UINT32_TO_PTR(addr.s_addr))) { |
727b5734 SS |
956 | log_struct(LOG_DEBUG, |
957 | LOG_LINK_INTERFACE(link), | |
0da425df | 958 | LOG_LINK_MESSAGE(link, "DHCPv4 IP '%u.%u.%u.%u' found in deny-listed IP addresses, ignoring offer", |
727b5734 SS |
959 | ADDRESS_FMT_VAL(addr))); |
960 | return true; | |
961 | } | |
962 | ||
963 | return false; | |
964 | } | |
965 | ||
98ebef62 SS |
966 | static int dhcp_server_is_allow_listed(Link *link, sd_dhcp_client *client) { |
967 | sd_dhcp_lease *lease; | |
968 | struct in_addr addr; | |
969 | int r; | |
970 | ||
971 | assert(link); | |
972 | assert(link->network); | |
973 | assert(client); | |
974 | ||
975 | r = sd_dhcp_client_get_lease(client, &lease); | |
976 | if (r < 0) | |
977 | return log_link_error_errno(link, r, "Failed to get DHCP lease: %m"); | |
978 | ||
979 | r = sd_dhcp_lease_get_server_identifier(lease, &addr); | |
980 | if (r < 0) | |
0da425df | 981 | return log_link_debug_errno(link, r, "Failed to get DHCP server IP address: %m"); |
98ebef62 SS |
982 | |
983 | if (set_contains(link->network->dhcp_allow_listed_ip, UINT32_TO_PTR(addr.s_addr))) { | |
984 | log_struct(LOG_DEBUG, | |
985 | LOG_LINK_INTERFACE(link), | |
0da425df | 986 | LOG_LINK_MESSAGE(link, "DHCPv4 IP '%u.%u.%u.%u' found in allow-listed IP addresses, accepting offer", |
98ebef62 SS |
987 | ADDRESS_FMT_VAL(addr))); |
988 | return true; | |
989 | } | |
990 | ||
991 | return false; | |
992 | } | |
993 | ||
727b5734 | 994 | static int dhcp4_handler(sd_dhcp_client *client, int event, void *userdata) { |
3c9b8860 | 995 | Link *link = userdata; |
86e2be7b | 996 | int r; |
3c9b8860 TG |
997 | |
998 | assert(link); | |
999 | assert(link->network); | |
1000 | assert(link->manager); | |
1001 | ||
1002 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
727b5734 | 1003 | return 0; |
3c9b8860 TG |
1004 | |
1005 | switch (event) { | |
03748142 | 1006 | case SD_DHCP_CLIENT_EVENT_STOP: |
8bc17bb3 | 1007 | |
910feb78 | 1008 | if (link_ipv4ll_enabled(link, ADDRESS_FAMILY_FALLBACK_IPV4)) { |
8bc17bb3 SS |
1009 | assert(link->ipv4ll); |
1010 | ||
1011 | log_link_debug(link, "DHCP client is stopped. Acquiring IPv4 link-local address"); | |
1012 | ||
1013 | r = sd_ipv4ll_start(link->ipv4ll); | |
727b5734 SS |
1014 | if (r < 0) |
1015 | return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m"); | |
8bc17bb3 SS |
1016 | } |
1017 | ||
7da377ef | 1018 | if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) { |
efdb62df YW |
1019 | log_link_notice(link, "DHCPv4 connection considered critical, ignoring request to reconfigure it."); |
1020 | return 0; | |
1021 | } | |
1022 | ||
efdb62df | 1023 | if (link->dhcp_lease) { |
f766d9af YW |
1024 | if (link->network->dhcp_send_release) { |
1025 | r = sd_dhcp_client_send_release(client); | |
1026 | if (r < 0) | |
1027 | log_link_warning_errno(link, r, "Failed to send DHCP RELEASE, ignoring: %m"); | |
1028 | } | |
8bea7e70 | 1029 | |
efdb62df YW |
1030 | r = dhcp_lease_lost(link); |
1031 | if (r < 0) { | |
1032 | link_enter_failed(link); | |
1033 | return r; | |
1034 | } | |
1035 | } | |
1036 | ||
1037 | break; | |
8bc17bb3 | 1038 | case SD_DHCP_CLIENT_EVENT_EXPIRED: |
7da377ef | 1039 | if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) { |
efdb62df | 1040 | log_link_notice(link, "DHCPv4 connection considered critical, ignoring request to reconfigure it."); |
727b5734 | 1041 | return 0; |
3c9b8860 TG |
1042 | } |
1043 | ||
1044 | if (link->dhcp_lease) { | |
1045 | r = dhcp_lease_lost(link); | |
1046 | if (r < 0) { | |
1047 | link_enter_failed(link); | |
727b5734 | 1048 | return r; |
3c9b8860 TG |
1049 | } |
1050 | } | |
1051 | ||
d03073dd YW |
1052 | break; |
1053 | case SD_DHCP_CLIENT_EVENT_IP_CHANGE: | |
1054 | if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) { | |
1055 | log_link_notice(link, "DHCPv4 connection considered critical, ignoring request to reconfigure it."); | |
1056 | return 0; | |
1057 | } | |
1058 | ||
1059 | r = dhcp_lease_ip_change(client, link); | |
1060 | if (r < 0) { | |
1061 | link_enter_failed(link); | |
1062 | return r; | |
3c9b8860 TG |
1063 | } |
1064 | ||
1065 | break; | |
03748142 | 1066 | case SD_DHCP_CLIENT_EVENT_RENEW: |
3c9b8860 TG |
1067 | r = dhcp_lease_renew(client, link); |
1068 | if (r < 0) { | |
1069 | link_enter_failed(link); | |
727b5734 | 1070 | return r; |
3c9b8860 TG |
1071 | } |
1072 | break; | |
03748142 | 1073 | case SD_DHCP_CLIENT_EVENT_IP_ACQUIRE: |
3c9b8860 TG |
1074 | r = dhcp_lease_acquired(client, link); |
1075 | if (r < 0) { | |
1076 | link_enter_failed(link); | |
727b5734 | 1077 | return r; |
3c9b8860 TG |
1078 | } |
1079 | break; | |
727b5734 | 1080 | case SD_DHCP_CLIENT_EVENT_SELECTING: |
98ebef62 SS |
1081 | if (!set_isempty(link->network->dhcp_allow_listed_ip)) { |
1082 | r = dhcp_server_is_allow_listed(link, client); | |
1083 | if (r < 0) | |
1084 | return r; | |
1085 | if (r == 0) | |
1086 | return -ENOMSG; | |
1087 | } else { | |
1088 | r = dhcp_server_is_deny_listed(link, client); | |
1089 | if (r < 0) | |
1090 | return r; | |
1091 | if (r != 0) | |
1092 | return -ENOMSG; | |
1093 | } | |
727b5734 | 1094 | break; |
3c9b8860 TG |
1095 | default: |
1096 | if (event < 0) | |
f6b8196f | 1097 | log_link_warning_errno(link, event, "DHCP error: Client failed: %m"); |
3c9b8860 | 1098 | else |
f6b8196f | 1099 | log_link_warning(link, "DHCP unknown event: %i", event); |
3c9b8860 TG |
1100 | break; |
1101 | } | |
1102 | ||
727b5734 | 1103 | return 0; |
3c9b8860 TG |
1104 | } |
1105 | ||
7192bb81 LP |
1106 | static int dhcp4_set_hostname(Link *link) { |
1107 | _cleanup_free_ char *hostname = NULL; | |
1108 | const char *hn; | |
1109 | int r; | |
1110 | ||
1111 | assert(link); | |
1112 | ||
1113 | if (!link->network->dhcp_send_hostname) | |
1114 | hn = NULL; | |
1115 | else if (link->network->dhcp_hostname) | |
1116 | hn = link->network->dhcp_hostname; | |
1117 | else { | |
1118 | r = gethostname_strict(&hostname); | |
1119 | if (r < 0 && r != -ENXIO) /* ENXIO: no hostname set or hostname is "localhost" */ | |
1120 | return r; | |
1121 | ||
1122 | hn = hostname; | |
1123 | } | |
1124 | ||
a8494759 YW |
1125 | r = sd_dhcp_client_set_hostname(link->dhcp_client, hn); |
1126 | if (r == -EINVAL && hostname) | |
1127 | /* Ignore error when the machine's hostname is not suitable to send in DHCP packet. */ | |
1128 | log_link_warning_errno(link, r, "DHCP4 CLIENT: Failed to set hostname from kernel hostname, ignoring: %m"); | |
1129 | else if (r < 0) | |
1130 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set hostname: %m"); | |
1131 | ||
1132 | return 0; | |
7192bb81 LP |
1133 | } |
1134 | ||
64b21ece | 1135 | static bool promote_secondaries_enabled(const char *ifname) { |
3f550c31 HV |
1136 | _cleanup_free_ char *promote_secondaries_sysctl = NULL; |
1137 | char *promote_secondaries_path; | |
64b21ece MV |
1138 | int r; |
1139 | ||
1140 | promote_secondaries_path = strjoina("net/ipv4/conf/", ifname, "/promote_secondaries"); | |
1141 | r = sysctl_read(promote_secondaries_path, &promote_secondaries_sysctl); | |
1142 | if (r < 0) { | |
1143 | log_debug_errno(r, "Cannot read sysctl %s", promote_secondaries_path); | |
1144 | return false; | |
1145 | } | |
1146 | ||
1147 | truncate_nl(promote_secondaries_sysctl); | |
1148 | r = parse_boolean(promote_secondaries_sysctl); | |
1149 | if (r < 0) | |
1150 | log_warning_errno(r, "Cannot parse sysctl %s with content %s as boolean", promote_secondaries_path, promote_secondaries_sysctl); | |
1151 | return r > 0; | |
1152 | } | |
1153 | ||
1154 | /* dhcp4_set_promote_secondaries will ensure this interface has | |
1155 | * the "promote_secondaries" option in the kernel set. If this sysctl | |
1156 | * is not set DHCP will work only as long as the IP address does not | |
1157 | * changes between leases. The kernel will remove all secondary IP | |
1158 | * addresses of an interface otherwise. The way systemd-network works | |
1159 | * is that the new IP of a lease is added as a secondary IP and when | |
1160 | * the primary one expires it relies on the kernel to promote the | |
1161 | * secondary IP. See also https://github.com/systemd/systemd/issues/7163 | |
1162 | */ | |
2ffd6d73 | 1163 | static int dhcp4_set_promote_secondaries(Link *link) { |
64b21ece MV |
1164 | int r; |
1165 | ||
1166 | assert(link); | |
64b21ece MV |
1167 | |
1168 | /* check if the kernel has promote_secondaries enabled for our | |
1169 | * interface. If it is not globally enabled or enabled for the | |
1170 | * specific interface we must either enable it. | |
1171 | */ | |
8e1a7253 | 1172 | if (!(promote_secondaries_enabled("all") || promote_secondaries_enabled(link->ifname))) { |
64b21ece MV |
1173 | char *promote_secondaries_path = NULL; |
1174 | ||
1175 | log_link_debug(link, "promote_secondaries is unset, setting it"); | |
1176 | promote_secondaries_path = strjoina("net/ipv4/conf/", link->ifname, "/promote_secondaries"); | |
1177 | r = sysctl_write(promote_secondaries_path, "1"); | |
1178 | if (r < 0) | |
1179 | log_link_warning_errno(link, r, "cannot set sysctl %s to 1", promote_secondaries_path); | |
1180 | return r > 0; | |
1181 | } | |
1182 | ||
1183 | return 0; | |
1184 | } | |
1185 | ||
d947f7f9 | 1186 | static int dhcp4_set_client_identifier(Link *link) { |
fff1f40c YW |
1187 | int r; |
1188 | ||
1189 | assert(link); | |
1190 | assert(link->network); | |
1191 | assert(link->dhcp_client); | |
1192 | ||
1193 | switch (link->network->dhcp_client_identifier) { | |
1194 | case DHCP_CLIENT_ID_DUID: { | |
0cf7c3fd | 1195 | /* If configured, apply user specified DUID and IAID */ |
fff1f40c YW |
1196 | const DUID *duid = link_get_duid(link); |
1197 | ||
0cf7c3fd YW |
1198 | if (duid->type == DUID_TYPE_LLT && duid->raw_data_len == 0) |
1199 | r = sd_dhcp_client_set_iaid_duid_llt(link->dhcp_client, | |
8217ed5e | 1200 | link->network->iaid_set, |
0cf7c3fd YW |
1201 | link->network->iaid, |
1202 | duid->llt_time); | |
1203 | else | |
1204 | r = sd_dhcp_client_set_iaid_duid(link->dhcp_client, | |
8217ed5e | 1205 | link->network->iaid_set, |
0cf7c3fd YW |
1206 | link->network->iaid, |
1207 | duid->type, | |
1208 | duid->raw_data_len > 0 ? duid->raw_data : NULL, | |
1209 | duid->raw_data_len); | |
fff1f40c | 1210 | if (r < 0) |
0cf7c3fd | 1211 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set IAID+DUID: %m"); |
fff1f40c YW |
1212 | break; |
1213 | } | |
1214 | case DHCP_CLIENT_ID_DUID_ONLY: { | |
1215 | /* If configured, apply user specified DUID */ | |
1216 | const DUID *duid = link_get_duid(link); | |
1217 | ||
0cf7c3fd YW |
1218 | if (duid->type == DUID_TYPE_LLT && duid->raw_data_len == 0) |
1219 | r = sd_dhcp_client_set_duid_llt(link->dhcp_client, | |
89b3fa66 | 1220 | duid->llt_time); |
0cf7c3fd YW |
1221 | else |
1222 | r = sd_dhcp_client_set_duid(link->dhcp_client, | |
1223 | duid->type, | |
1224 | duid->raw_data_len > 0 ? duid->raw_data : NULL, | |
1225 | duid->raw_data_len); | |
fff1f40c YW |
1226 | if (r < 0) |
1227 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set DUID: %m"); | |
1228 | break; | |
1229 | } | |
1230 | case DHCP_CLIENT_ID_MAC: | |
1231 | r = sd_dhcp_client_set_client_id(link->dhcp_client, | |
1232 | ARPHRD_ETHER, | |
1233 | (const uint8_t *) &link->mac, | |
1234 | sizeof(link->mac)); | |
1235 | if (r < 0) | |
1236 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set client ID: %m"); | |
1237 | break; | |
1238 | default: | |
1239 | assert_not_reached("Unknown client identifier type."); | |
1240 | } | |
1241 | ||
1242 | return 0; | |
1243 | } | |
1244 | ||
571eeba9 YW |
1245 | static int dhcp4_init(Link *link) { |
1246 | int r; | |
1247 | ||
1248 | assert(link); | |
1249 | ||
1250 | if (link->dhcp_client) | |
1251 | return 0; | |
1252 | ||
1253 | r = sd_dhcp_client_new(&link->dhcp_client, link->network->dhcp_anonymize); | |
1254 | if (r < 0) | |
1255 | return r; | |
1256 | ||
1257 | r = sd_dhcp_client_attach_event(link->dhcp_client, link->manager->event, 0); | |
1258 | if (r < 0) | |
1259 | return r; | |
1260 | ||
1261 | return 0; | |
1262 | } | |
1263 | ||
3c9b8860 | 1264 | int dhcp4_configure(Link *link) { |
cb29c156 | 1265 | sd_dhcp_option *send_option; |
5bc945be | 1266 | void *request_options; |
3c9b8860 TG |
1267 | int r; |
1268 | ||
1269 | assert(link); | |
1270 | assert(link->network); | |
2ffd6d73 YW |
1271 | |
1272 | if (!link_dhcp4_enabled(link)) | |
1273 | return 0; | |
1274 | ||
1275 | r = dhcp4_set_promote_secondaries(link); | |
1276 | if (r < 0) | |
1277 | return r; | |
3c9b8860 | 1278 | |
571eeba9 YW |
1279 | r = dhcp4_init(link); |
1280 | if (r < 0) | |
1281 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to initialize DHCP4 client: %m"); | |
3c9b8860 | 1282 | |
76253e73 DW |
1283 | r = sd_dhcp_client_set_mac(link->dhcp_client, |
1284 | (const uint8_t *) &link->mac, | |
1285 | sizeof (link->mac), ARPHRD_ETHER); | |
3c9b8860 | 1286 | if (r < 0) |
1f6860d9 | 1287 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set MAC address: %m"); |
3c9b8860 | 1288 | |
2f8e7633 | 1289 | r = sd_dhcp_client_set_ifindex(link->dhcp_client, link->ifindex); |
3c9b8860 | 1290 | if (r < 0) |
1f6860d9 | 1291 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set ifindex: %m"); |
3c9b8860 TG |
1292 | |
1293 | r = sd_dhcp_client_set_callback(link->dhcp_client, dhcp4_handler, link); | |
1294 | if (r < 0) | |
1f6860d9 | 1295 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set callback: %m"); |
3c9b8860 TG |
1296 | |
1297 | r = sd_dhcp_client_set_request_broadcast(link->dhcp_client, | |
1298 | link->network->dhcp_broadcast); | |
1299 | if (r < 0) | |
1f6860d9 | 1300 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for broadcast: %m"); |
3c9b8860 TG |
1301 | |
1302 | if (link->mtu) { | |
1303 | r = sd_dhcp_client_set_mtu(link->dhcp_client, link->mtu); | |
1304 | if (r < 0) | |
1f6860d9 | 1305 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set MTU: %m"); |
3c9b8860 TG |
1306 | } |
1307 | ||
27cb34f5 | 1308 | if (link->network->dhcp_use_mtu) { |
39745a5a | 1309 | r = sd_dhcp_client_set_request_option(link->dhcp_client, |
22805d92 | 1310 | SD_DHCP_OPTION_INTERFACE_MTU); |
39745a5a | 1311 | if (r < 0) |
1f6860d9 | 1312 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for MTU: %m"); |
3c9b8860 TG |
1313 | } |
1314 | ||
5e77a146 | 1315 | /* NOTE: even if this variable is called "use", it also "sends" PRL |
1316 | * options, maybe there should be a different configuration variable | |
1317 | * to send or not route options?. */ | |
28522b0d | 1318 | /* NOTE: when using Anonymize=yes, routes PRL options are sent |
1319 | * by default, so they don't need to be added here. */ | |
5e77a146 | 1320 | if (link->network->dhcp_use_routes && !link->network->dhcp_anonymize) { |
3c9b8860 | 1321 | r = sd_dhcp_client_set_request_option(link->dhcp_client, |
22805d92 | 1322 | SD_DHCP_OPTION_STATIC_ROUTE); |
3c9b8860 | 1323 | if (r < 0) |
1f6860d9 YW |
1324 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for static route: %m"); |
1325 | ||
3c9b8860 | 1326 | r = sd_dhcp_client_set_request_option(link->dhcp_client, |
22805d92 | 1327 | SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE); |
7d6884b6 | 1328 | if (r < 0) |
1f6860d9 | 1329 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for classless static route: %m"); |
3c9b8860 TG |
1330 | } |
1331 | ||
150d3b8e YW |
1332 | if (link->network->dhcp_use_domains != DHCP_USE_DOMAINS_NO && !link->network->dhcp_anonymize) { |
1333 | r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_DOMAIN_SEARCH_LIST); | |
1334 | if (r < 0) | |
1335 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for domain search list: %m"); | |
1336 | } | |
1337 | ||
ead36ce6 | 1338 | if (link->network->dhcp_use_ntp) { |
1339 | r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_NTP_SERVER); | |
1340 | if (r < 0) | |
1f6860d9 | 1341 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for NTP server: %m"); |
ead36ce6 | 1342 | } |
4b7b5abb | 1343 | |
299d578f SS |
1344 | if (link->network->dhcp_use_sip) { |
1345 | r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_SIP_SERVER); | |
1346 | if (r < 0) | |
1347 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for SIP server: %m"); | |
1348 | } | |
1349 | ||
89573b37 | 1350 | if (link->network->dhcp_use_timezone) { |
1351 | r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_NEW_TZDB_TIMEZONE); | |
1352 | if (r < 0) | |
1f6860d9 | 1353 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for timezone: %m"); |
89573b37 | 1354 | } |
8eb9058d | 1355 | |
90e74a66 | 1356 | SET_FOREACH(request_options, link->network->dhcp_request_options) { |
5bc945be SS |
1357 | uint32_t option = PTR_TO_UINT32(request_options); |
1358 | ||
1359 | r = sd_dhcp_client_set_request_option(link->dhcp_client, option); | |
1360 | if (r == -EEXIST) { | |
1361 | log_link_debug(link, "DHCP4 CLIENT: Failed to set request flag for '%u' already exists, ignoring.", option); | |
1362 | continue; | |
1363 | } | |
5bc945be SS |
1364 | if (r < 0) |
1365 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for '%u': %m", option); | |
1366 | } | |
1367 | ||
90e74a66 | 1368 | ORDERED_HASHMAP_FOREACH(send_option, link->network->dhcp_client_send_options) { |
7354900d DW |
1369 | r = sd_dhcp_client_add_option(link->dhcp_client, send_option); |
1370 | if (r == -EEXIST) | |
1371 | continue; | |
1372 | if (r < 0) | |
1373 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set send option: %m"); | |
1374 | } | |
1375 | ||
90e74a66 | 1376 | ORDERED_HASHMAP_FOREACH(send_option, link->network->dhcp_client_send_vendor_options) { |
7354900d DW |
1377 | r = sd_dhcp_client_add_vendor_option(link->dhcp_client, send_option); |
1378 | if (r == -EEXIST) | |
1379 | continue; | |
cb29c156 SS |
1380 | if (r < 0) |
1381 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set send option: %m"); | |
1382 | } | |
1383 | ||
7192bb81 LP |
1384 | r = dhcp4_set_hostname(link); |
1385 | if (r < 0) | |
a8494759 | 1386 | return r; |
3c9b8860 TG |
1387 | |
1388 | if (link->network->dhcp_vendor_class_identifier) { | |
1389 | r = sd_dhcp_client_set_vendor_class_identifier(link->dhcp_client, | |
1390 | link->network->dhcp_vendor_class_identifier); | |
1391 | if (r < 0) | |
1f6860d9 | 1392 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set vendor class identifier: %m"); |
3c9b8860 TG |
1393 | } |
1394 | ||
7b8d23a9 SS |
1395 | if (link->network->dhcp_mudurl) { |
1396 | r = sd_dhcp_client_set_mud_url(link->dhcp_client, | |
1397 | link->network->dhcp_mudurl); | |
1398 | if (r < 0) | |
1399 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set MUD URL: %m"); | |
1400 | } | |
1401 | ||
af1c0de0 SS |
1402 | if (link->network->dhcp_user_class) { |
1403 | r = sd_dhcp_client_set_user_class(link->dhcp_client, (const char **) link->network->dhcp_user_class); | |
1404 | if (r < 0) | |
1f6860d9 | 1405 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set user class: %m"); |
af1c0de0 SS |
1406 | } |
1407 | ||
9faed222 SS |
1408 | if (link->network->dhcp_client_port) { |
1409 | r = sd_dhcp_client_set_client_port(link->dhcp_client, link->network->dhcp_client_port); | |
1410 | if (r < 0) | |
1f6860d9 | 1411 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set listen port: %m"); |
9faed222 SS |
1412 | } |
1413 | ||
715cedfb SS |
1414 | if (link->network->dhcp_max_attempts > 0) { |
1415 | r = sd_dhcp_client_set_max_attempts(link->dhcp_client, link->network->dhcp_max_attempts); | |
1416 | if (r < 0) | |
1417 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set max attempts: %m"); | |
1418 | } | |
1419 | ||
afe42aef SC |
1420 | if (link->network->ip_service_type > 0) { |
1421 | r = sd_dhcp_client_set_service_type(link->dhcp_client, link->network->ip_service_type); | |
1422 | if (r < 0) | |
0da425df | 1423 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set IP service type: %m"); |
afe42aef | 1424 | } |
0f3ff4ea | 1425 | |
d6463307 SS |
1426 | if (link->network->dhcp_fallback_lease_lifetime > 0) { |
1427 | r = sd_dhcp_client_set_fallback_lease_lifetime(link->dhcp_client, link->network->dhcp_fallback_lease_lifetime); | |
1428 | if (r < 0) | |
1429 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed set to lease lifetime: %m"); | |
1430 | } | |
1431 | ||
0f3ff4ea SS |
1432 | if (link->network->dhcp_send_decline) { |
1433 | r = configure_dhcpv4_duplicate_address_detection(link); | |
1434 | if (r < 0) | |
1435 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to configure service type: %m"); | |
1436 | } | |
1437 | ||
1438 | return dhcp4_set_client_identifier(link); | |
3c9b8860 | 1439 | } |
ca5ad760 | 1440 | |
d947f7f9 YW |
1441 | int dhcp4_update_mac(Link *link) { |
1442 | int r; | |
1443 | ||
1444 | assert(link); | |
1445 | ||
1446 | if (!link->dhcp_client) | |
1447 | return 0; | |
1448 | ||
1449 | r = sd_dhcp_client_set_mac(link->dhcp_client, (const uint8_t *) &link->mac, sizeof (link->mac), ARPHRD_ETHER); | |
1450 | if (r < 0) | |
1451 | return r; | |
1452 | ||
1453 | r = dhcp4_set_client_identifier(link); | |
1454 | if (r < 0) | |
1455 | return r; | |
1456 | ||
1457 | return 0; | |
1458 | } | |
1459 | ||
571eeba9 YW |
1460 | int link_deserialize_dhcp4(Link *link, const char *dhcp4_address) { |
1461 | union in_addr_union address; | |
1462 | int r; | |
1463 | ||
1464 | assert(link); | |
1465 | ||
1466 | if (isempty(dhcp4_address)) | |
1467 | return 0; | |
1468 | ||
1469 | r = in_addr_from_string(AF_INET, dhcp4_address, &address); | |
1470 | if (r < 0) | |
1471 | return log_link_debug_errno(link, r, "Failed to parse DHCPv4 address: %s", dhcp4_address); | |
1472 | ||
1473 | r = dhcp4_init(link); | |
1474 | if (r < 0) | |
1475 | return log_link_debug_errno(link, r, "Failed to initialize DHCPv4 client: %m"); | |
1476 | ||
1477 | r = sd_dhcp_client_set_request_address(link->dhcp_client, &address.in); | |
1478 | if (r < 0) | |
1479 | return log_link_debug_errno(link, r, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address); | |
1480 | ||
1481 | return 0; | |
1482 | } | |
1483 | ||
ca5ad760 YW |
1484 | int config_parse_dhcp_max_attempts( |
1485 | const char *unit, | |
1486 | const char *filename, | |
1487 | unsigned line, | |
1488 | const char *section, | |
1489 | unsigned section_line, | |
1490 | const char *lvalue, | |
1491 | int ltype, | |
1492 | const char *rvalue, | |
1493 | void *data, | |
1494 | void *userdata) { | |
1495 | ||
1496 | Network *network = data; | |
1497 | uint64_t a; | |
1498 | int r; | |
1499 | ||
1500 | assert(network); | |
1501 | assert(lvalue); | |
1502 | assert(rvalue); | |
1503 | ||
1504 | if (isempty(rvalue)) { | |
1505 | network->dhcp_max_attempts = 0; | |
1506 | return 0; | |
1507 | } | |
1508 | ||
1509 | if (streq(rvalue, "infinity")) { | |
1510 | network->dhcp_max_attempts = (uint64_t) -1; | |
1511 | return 0; | |
1512 | } | |
1513 | ||
1514 | r = safe_atou64(rvalue, &a); | |
1515 | if (r < 0) { | |
d96edb2c | 1516 | log_syntax(unit, LOG_WARNING, filename, line, r, |
ca5ad760 YW |
1517 | "Failed to parse DHCP maximum attempts, ignoring: %s", rvalue); |
1518 | return 0; | |
1519 | } | |
1520 | ||
1521 | if (a == 0) { | |
d96edb2c | 1522 | log_syntax(unit, LOG_WARNING, filename, line, 0, |
ca5ad760 YW |
1523 | "%s= must be positive integer or 'infinity', ignoring: %s", lvalue, rvalue); |
1524 | return 0; | |
1525 | } | |
1526 | ||
1527 | network->dhcp_max_attempts = a; | |
1528 | ||
1529 | return 0; | |
1530 | } | |
1531 | ||
98ebef62 | 1532 | int config_parse_dhcp_acl_ip_address( |
ca5ad760 YW |
1533 | const char *unit, |
1534 | const char *filename, | |
1535 | unsigned line, | |
1536 | const char *section, | |
1537 | unsigned section_line, | |
1538 | const char *lvalue, | |
1539 | int ltype, | |
1540 | const char *rvalue, | |
1541 | void *data, | |
1542 | void *userdata) { | |
1543 | ||
1544 | Network *network = data; | |
98ebef62 | 1545 | Set **acl; |
ca5ad760 YW |
1546 | int r; |
1547 | ||
1548 | assert(filename); | |
1549 | assert(lvalue); | |
1550 | assert(rvalue); | |
1551 | assert(data); | |
1552 | ||
98ebef62 SS |
1553 | acl = STR_IN_SET(lvalue, "DenyList", "BlackList") ? &network->dhcp_deny_listed_ip : &network->dhcp_allow_listed_ip; |
1554 | ||
ca5ad760 | 1555 | if (isempty(rvalue)) { |
98ebef62 | 1556 | *acl = set_free(*acl); |
ca5ad760 YW |
1557 | return 0; |
1558 | } | |
1559 | ||
de7fef4b | 1560 | for (const char *p = rvalue;;) { |
ca5ad760 YW |
1561 | _cleanup_free_ char *n = NULL; |
1562 | union in_addr_union ip; | |
1563 | ||
1564 | r = extract_first_word(&p, &n, NULL, 0); | |
d96edb2c YW |
1565 | if (r == -ENOMEM) |
1566 | return log_oom(); | |
ca5ad760 | 1567 | if (r < 0) { |
d96edb2c | 1568 | log_syntax(unit, LOG_WARNING, filename, line, r, |
98ebef62 SS |
1569 | "Failed to parse DHCP '%s=' IP address, ignoring assignment: %s", |
1570 | lvalue, rvalue); | |
ca5ad760 YW |
1571 | return 0; |
1572 | } | |
1573 | if (r == 0) | |
1574 | return 0; | |
1575 | ||
1576 | r = in_addr_from_string(AF_INET, n, &ip); | |
1577 | if (r < 0) { | |
d96edb2c | 1578 | log_syntax(unit, LOG_WARNING, filename, line, r, |
98ebef62 | 1579 | "DHCP '%s=' IP address is invalid, ignoring assignment: %s", lvalue, n); |
ca5ad760 YW |
1580 | continue; |
1581 | } | |
1582 | ||
98ebef62 | 1583 | r = set_ensure_put(acl, NULL, UINT32_TO_PTR(ip.in.s_addr)); |
ca5ad760 | 1584 | if (r < 0) |
d96edb2c | 1585 | log_syntax(unit, LOG_WARNING, filename, line, r, |
98ebef62 | 1586 | "Failed to store DHCP '%s=' IP address '%s', ignoring assignment: %m", lvalue, n); |
ca5ad760 | 1587 | } |
ca5ad760 YW |
1588 | } |
1589 | ||
a75b2117 SS |
1590 | int config_parse_dhcp_ip_service_type( |
1591 | const char *unit, | |
1592 | const char *filename, | |
1593 | unsigned line, | |
1594 | const char *section, | |
1595 | unsigned section_line, | |
1596 | const char *lvalue, | |
1597 | int ltype, | |
1598 | const char *rvalue, | |
1599 | void *data, | |
1600 | void *userdata) { | |
1601 | ||
1602 | assert(filename); | |
1603 | assert(lvalue); | |
1604 | assert(rvalue); | |
1605 | ||
1606 | if (streq(rvalue, "CS4")) | |
1607 | *((int *)data) = IPTOS_CLASS_CS4; | |
1608 | else if (streq(rvalue, "CS6")) | |
1609 | *((int *)data) = IPTOS_CLASS_CS6; | |
1610 | else | |
1611 | log_syntax(unit, LOG_WARNING, filename, line, 0, | |
1612 | "Failed to parse IPServiceType type '%s', ignoring.", rvalue); | |
1613 | ||
1614 | return 0; | |
1615 | } | |
1616 | ||
7b8d23a9 SS |
1617 | int config_parse_dhcp_mud_url( |
1618 | const char *unit, | |
1619 | const char *filename, | |
1620 | unsigned line, | |
1621 | const char *section, | |
1622 | unsigned section_line, | |
1623 | const char *lvalue, | |
1624 | int ltype, | |
1625 | const char *rvalue, | |
1626 | void *data, | |
1627 | void *userdata) { | |
1628 | ||
1629 | _cleanup_free_ char *unescaped = NULL; | |
1630 | Network *network = data; | |
1631 | int r; | |
1632 | ||
1633 | assert(filename); | |
1634 | assert(lvalue); | |
1635 | assert(rvalue); | |
1636 | ||
1637 | if (isempty(rvalue)) { | |
1638 | network->dhcp_mudurl = mfree(network->dhcp_mudurl); | |
1639 | return 0; | |
1640 | } | |
1641 | ||
1642 | r = cunescape(rvalue, 0, &unescaped); | |
1643 | if (r < 0) { | |
d96edb2c | 1644 | log_syntax(unit, LOG_WARNING, filename, line, r, |
7b8d23a9 SS |
1645 | "Failed to Failed to unescape MUD URL, ignoring: %s", rvalue); |
1646 | return 0; | |
1647 | } | |
1648 | ||
1649 | if (!http_url_is_valid(unescaped) || strlen(unescaped) > 255) { | |
d96edb2c | 1650 | log_syntax(unit, LOG_WARNING, filename, line, 0, |
7b8d23a9 SS |
1651 | "Failed to parse MUD URL '%s', ignoring: %m", rvalue); |
1652 | ||
1653 | return 0; | |
1654 | } | |
1655 | ||
1656 | return free_and_strdup_warn(&network->dhcp_mudurl, unescaped); | |
1657 | } | |
1658 | ||
d6463307 SS |
1659 | int config_parse_dhcp_fallback_lease_lifetime(const char *unit, |
1660 | const char *filename, | |
1661 | unsigned line, | |
1662 | const char *section, | |
1663 | unsigned section_line, | |
1664 | const char *lvalue, | |
1665 | int ltype, | |
1666 | const char *rvalue, | |
1667 | void *data, | |
1668 | void *userdata) { | |
1669 | Network *network = userdata; | |
d2735796 | 1670 | uint32_t k; |
d6463307 SS |
1671 | |
1672 | assert(filename); | |
1673 | assert(section); | |
1674 | assert(lvalue); | |
1675 | assert(rvalue); | |
1676 | assert(data); | |
1677 | ||
1678 | if (isempty(rvalue)) { | |
1679 | network->dhcp_fallback_lease_lifetime = 0; | |
1680 | return 0; | |
1681 | } | |
1682 | ||
1683 | /* We accept only "forever" or "infinity". */ | |
1684 | if (STR_IN_SET(rvalue, "forever", "infinity")) | |
1685 | k = CACHE_INFO_INFINITY_LIFE_TIME; | |
1686 | else { | |
d96edb2c | 1687 | log_syntax(unit, LOG_WARNING, filename, line, 0, |
d6463307 SS |
1688 | "Invalid LeaseLifetime= value, ignoring: %s", rvalue); |
1689 | return 0; | |
1690 | } | |
1691 | ||
1692 | network->dhcp_fallback_lease_lifetime = k; | |
1693 | ||
1694 | return 0; | |
1695 | } | |
1696 | ||
ca5ad760 YW |
1697 | static const char* const dhcp_client_identifier_table[_DHCP_CLIENT_ID_MAX] = { |
1698 | [DHCP_CLIENT_ID_MAC] = "mac", | |
1699 | [DHCP_CLIENT_ID_DUID] = "duid", | |
1700 | [DHCP_CLIENT_ID_DUID_ONLY] = "duid-only", | |
1701 | }; | |
1702 | ||
1703 | DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(dhcp_client_identifier, DHCPClientIdentifier); | |
1704 | DEFINE_CONFIG_PARSE_ENUM(config_parse_dhcp_client_identifier, dhcp_client_identifier, DHCPClientIdentifier, | |
1705 | "Failed to parse client identifier type"); |