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