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