]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
3c9b8860 TG |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2013-2014 Tom Gundersen <teg@jklm.no> | |
3c9b8860 TG |
6 | ***/ |
7 | ||
8 | #include <netinet/ether.h> | |
9 | #include <linux/if.h> | |
10 | ||
b5efdb8a LP |
11 | #include "alloc-util.h" |
12 | #include "dhcp-lease-internal.h" | |
958b66ea | 13 | #include "hostname-util.h" |
64b21ece | 14 | #include "parse-util.h" |
fc1ba79d | 15 | #include "netdev/vrf.h" |
3c9b8860 | 16 | #include "network-internal.h" |
23f53b99 TG |
17 | #include "networkd-link.h" |
18 | #include "networkd-manager.h" | |
19 | #include "networkd-network.h" | |
64b21ece MV |
20 | #include "string-util.h" |
21 | #include "sysctl-util.h" | |
3c9b8860 | 22 | |
1c4baffc | 23 | static int dhcp4_route_handler(sd_netlink *rtnl, sd_netlink_message *m, |
3c9b8860 | 24 | void *userdata) { |
8e766630 | 25 | _cleanup_(link_unrefp) Link *link = userdata; |
3c9b8860 TG |
26 | int r; |
27 | ||
28 | assert(link); | |
6cf4a01c | 29 | assert(link->dhcp4_messages > 0); |
3c9b8860 | 30 | |
313cefa1 | 31 | link->dhcp4_messages--; |
3c9b8860 | 32 | |
1c4baffc | 33 | r = sd_netlink_message_get_errno(m); |
3c9b8860 | 34 | if (r < 0 && r != -EEXIST) { |
f6b8196f | 35 | log_link_error_errno(link, r, "Could not set DHCPv4 route: %m"); |
3c9b8860 TG |
36 | link_enter_failed(link); |
37 | } | |
38 | ||
6cf4a01c | 39 | if (link->dhcp4_messages == 0) { |
3c9b8860 | 40 | link->dhcp4_configured = true; |
8012cd39 | 41 | link_check_ready(link); |
3c9b8860 TG |
42 | } |
43 | ||
44 | return 1; | |
45 | } | |
46 | ||
d6eac9bd DW |
47 | static int route_scope_from_address(const Route *route, const struct in_addr *self_addr) { |
48 | assert(route); | |
49 | assert(self_addr); | |
50 | ||
51 | if (in_addr_is_localhost(AF_INET, &route->dst) || | |
52 | (self_addr->s_addr && route->dst.in.s_addr == self_addr->s_addr)) | |
53 | return RT_SCOPE_HOST; | |
54 | else if (in4_addr_is_null(&route->gw.in)) | |
55 | return RT_SCOPE_LINK; | |
56 | else | |
57 | return RT_SCOPE_UNIVERSE; | |
58 | } | |
59 | ||
3c9b8860 | 60 | static int link_set_dhcp_routes(Link *link) { |
f8693fc7 | 61 | _cleanup_free_ sd_dhcp_route **static_routes = NULL; |
8cdc46e7 SS |
62 | bool classless_route = false, static_route = false; |
63 | struct in_addr gateway, address; | |
3c9b8860 | 64 | int r, n, i; |
fc1ba79d | 65 | uint32_t table; |
3c9b8860 TG |
66 | |
67 | assert(link); | |
0c9b15a3 AJ |
68 | |
69 | if (!link->dhcp_lease) /* link went down while we configured the IP addresses? */ | |
70 | return 0; | |
71 | ||
72 | if (!link->network) /* link went down while we configured the IP addresses? */ | |
73 | return 0; | |
964b26fe SS |
74 | |
75 | if (!link->network->dhcp_use_routes) | |
76 | return 0; | |
3c9b8860 | 77 | |
fc1ba79d AR |
78 | /* When the interface is part of an VRF use the VRFs routing table, unless |
79 | * there is a another table specified. */ | |
80 | table = link->network->dhcp_route_table; | |
81 | if (!link->network->dhcp_route_table_set && link->network->vrf != NULL) | |
82 | table = VRF(link->network->vrf)->table; | |
83 | ||
b23aec0d DW |
84 | r = sd_dhcp_lease_get_address(link->dhcp_lease, &address); |
85 | if (r < 0) | |
86 | return log_link_warning_errno(link, r, "DHCP error: could not get address: %m"); | |
87 | ||
5f04a209 | 88 | n = sd_dhcp_lease_get_routes(link->dhcp_lease, &static_routes); |
5f04a209 | 89 | if (n < 0) |
8dc787d1 | 90 | log_link_debug_errno(link, n, "DHCP error: could not get routes: %m"); |
5f04a209 | 91 | |
8cdc46e7 SS |
92 | for (i = 0; i < n; i++) { |
93 | if (static_routes[i]->option == SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE) | |
94 | classless_route = true; | |
95 | ||
96 | if (static_routes[i]->option == SD_DHCP_OPTION_STATIC_ROUTE) | |
97 | static_route = true; | |
98 | } | |
99 | ||
5f04a209 | 100 | for (i = 0; i < n; i++) { |
8e766630 | 101 | _cleanup_(route_freep) Route *route = NULL; |
5f04a209 | 102 | |
8cdc46e7 SS |
103 | /* if the DHCP server returns both a Classless Static Routes option and a Static Routes option, |
104 | the DHCP client MUST ignore the Static Routes option. */ | |
105 | if (classless_route && static_routes[i]->option == SD_DHCP_OPTION_STATIC_ROUTE) | |
106 | continue; | |
107 | ||
5f04a209 SS |
108 | r = route_new(&route); |
109 | if (r < 0) | |
110 | return log_link_error_errno(link, r, "Could not allocate route: %m"); | |
111 | ||
112 | route->family = AF_INET; | |
113 | route->protocol = RTPROT_DHCP; | |
114 | assert_se(sd_dhcp_route_get_gateway(static_routes[i], &route->gw.in) >= 0); | |
115 | assert_se(sd_dhcp_route_get_destination(static_routes[i], &route->dst.in) >= 0); | |
116 | assert_se(sd_dhcp_route_get_destination_prefix_length(static_routes[i], &route->dst_prefixlen) >= 0); | |
117 | route->priority = link->network->dhcp_route_metric; | |
118 | route->table = table; | |
119 | route->scope = route_scope_from_address(route, &address); | |
120 | ||
121 | r = route_configure(route, link, dhcp4_route_handler); | |
122 | if (r < 0) | |
123 | return log_link_warning_errno(link, r, "Could not set host route: %m"); | |
124 | ||
125 | link->dhcp4_messages++; | |
126 | } | |
127 | ||
3c9b8860 | 128 | r = sd_dhcp_lease_get_router(link->dhcp_lease, &gateway); |
444b0170 SS |
129 | if (r == -ENODATA) |
130 | log_link_info_errno(link, r, "DHCP: No routes received from DHCP server: %m"); | |
131 | else if (r < 0) | |
132 | log_link_warning_errno(link, r, "DHCP error: could not get gateway: %m"); | |
f6b8196f | 133 | |
5f04a209 SS |
134 | /* According to RFC 3442: If the DHCP server returns both a Classless Static Routes option and |
135 | a Router option, the DHCP client MUST ignore the Router option. */ | |
8cdc46e7 SS |
136 | if (classless_route && static_route) |
137 | log_link_warning(link, "Classless static routes received from DHCP server: ignoring static-route option and router option"); | |
138 | ||
139 | if (r >= 0 && !classless_route) { | |
8e766630 LP |
140 | _cleanup_(route_freep) Route *route = NULL; |
141 | _cleanup_(route_freep) Route *route_gw = NULL; | |
3c9b8860 | 142 | |
ed9e361a | 143 | r = route_new(&route); |
f6b8196f LP |
144 | if (r < 0) |
145 | return log_link_error_errno(link, r, "Could not allocate route: %m"); | |
3c9b8860 | 146 | |
ed9e361a TG |
147 | route->protocol = RTPROT_DHCP; |
148 | ||
149 | r = route_new(&route_gw); | |
f6b8196f LP |
150 | if (r < 0) |
151 | return log_link_error_errno(link, r, "Could not allocate route: %m"); | |
3c9b8860 TG |
152 | |
153 | /* The dhcp netmask may mask out the gateway. Add an explicit | |
154 | * route for the gw host so that we can route no matter the | |
155 | * netmask or existing kernel route tables. */ | |
156 | route_gw->family = AF_INET; | |
2ce40956 | 157 | route_gw->dst.in = gateway; |
3c9b8860 | 158 | route_gw->dst_prefixlen = 32; |
2ce40956 | 159 | route_gw->prefsrc.in = address; |
3c9b8860 | 160 | route_gw->scope = RT_SCOPE_LINK; |
ed9e361a | 161 | route_gw->protocol = RTPROT_DHCP; |
86655331 | 162 | route_gw->priority = link->network->dhcp_route_metric; |
fc1ba79d | 163 | route_gw->table = table; |
3c9b8860 | 164 | |
483d099e | 165 | r = route_configure(route_gw, link, dhcp4_route_handler); |
f6b8196f LP |
166 | if (r < 0) |
167 | return log_link_warning_errno(link, r, "Could not set host route: %m"); | |
3c9b8860 | 168 | |
313cefa1 | 169 | link->dhcp4_messages++; |
3c9b8860 TG |
170 | |
171 | route->family = AF_INET; | |
2ce40956 TG |
172 | route->gw.in = gateway; |
173 | route->prefsrc.in = address; | |
86655331 | 174 | route->priority = link->network->dhcp_route_metric; |
fc1ba79d | 175 | route->table = table; |
3c9b8860 | 176 | |
483d099e | 177 | r = route_configure(route, link, dhcp4_route_handler); |
3c9b8860 | 178 | if (r < 0) { |
f6b8196f | 179 | log_link_warning_errno(link, r, "Could not set routes: %m"); |
3c9b8860 TG |
180 | link_enter_failed(link); |
181 | return r; | |
182 | } | |
183 | ||
313cefa1 | 184 | link->dhcp4_messages++; |
3c9b8860 TG |
185 | } |
186 | ||
3c9b8860 TG |
187 | return 0; |
188 | } | |
189 | ||
190 | static int dhcp_lease_lost(Link *link) { | |
8e766630 | 191 | _cleanup_(address_freep) Address *address = NULL; |
3c9b8860 TG |
192 | struct in_addr addr; |
193 | struct in_addr netmask; | |
194 | struct in_addr gateway; | |
f414a269 | 195 | unsigned prefixlen = 0; |
3c9b8860 TG |
196 | int r; |
197 | ||
198 | assert(link); | |
199 | assert(link->dhcp_lease); | |
200 | ||
79008bdd | 201 | log_link_warning(link, "DHCP lease lost"); |
3c9b8860 | 202 | |
27cb34f5 | 203 | if (link->network->dhcp_use_routes) { |
f8693fc7 | 204 | _cleanup_free_ sd_dhcp_route **routes = NULL; |
3c9b8860 TG |
205 | int n, i; |
206 | ||
207 | n = sd_dhcp_lease_get_routes(link->dhcp_lease, &routes); | |
208 | if (n >= 0) { | |
209 | for (i = 0; i < n; i++) { | |
8e766630 | 210 | _cleanup_(route_freep) Route *route = NULL; |
3c9b8860 | 211 | |
ed9e361a | 212 | r = route_new(&route); |
3c9b8860 TG |
213 | if (r >= 0) { |
214 | route->family = AF_INET; | |
f8693fc7 BG |
215 | assert_se(sd_dhcp_route_get_gateway(routes[i], &route->gw.in) >= 0); |
216 | assert_se(sd_dhcp_route_get_destination(routes[i], &route->dst.in) >= 0); | |
217 | assert_se(sd_dhcp_route_get_destination_prefix_length(routes[i], &route->dst_prefixlen) >= 0); | |
3c9b8860 | 218 | |
91b5f997 | 219 | route_remove(route, link, |
483d099e | 220 | link_route_remove_handler); |
3c9b8860 TG |
221 | } |
222 | } | |
223 | } | |
224 | } | |
225 | ||
f0213e37 | 226 | r = address_new(&address); |
3c9b8860 TG |
227 | if (r >= 0) { |
228 | r = sd_dhcp_lease_get_router(link->dhcp_lease, &gateway); | |
229 | if (r >= 0) { | |
8e766630 LP |
230 | _cleanup_(route_freep) Route *route_gw = NULL; |
231 | _cleanup_(route_freep) Route *route = NULL; | |
3c9b8860 | 232 | |
ed9e361a | 233 | r = route_new(&route_gw); |
3c9b8860 TG |
234 | if (r >= 0) { |
235 | route_gw->family = AF_INET; | |
2ce40956 | 236 | route_gw->dst.in = gateway; |
3c9b8860 TG |
237 | route_gw->dst_prefixlen = 32; |
238 | route_gw->scope = RT_SCOPE_LINK; | |
239 | ||
91b5f997 | 240 | route_remove(route_gw, link, |
483d099e | 241 | link_route_remove_handler); |
3c9b8860 TG |
242 | } |
243 | ||
ed9e361a | 244 | r = route_new(&route); |
3c9b8860 TG |
245 | if (r >= 0) { |
246 | route->family = AF_INET; | |
2ce40956 | 247 | route->gw.in = gateway; |
3c9b8860 | 248 | |
91b5f997 | 249 | route_remove(route, link, |
483d099e | 250 | link_route_remove_handler); |
3c9b8860 TG |
251 | } |
252 | } | |
253 | ||
f414a269 TG |
254 | r = sd_dhcp_lease_get_address(link->dhcp_lease, &addr); |
255 | if (r >= 0) { | |
256 | r = sd_dhcp_lease_get_netmask(link->dhcp_lease, &netmask); | |
257 | if (r >= 0) | |
5a941f5f | 258 | prefixlen = in4_addr_netmask_to_prefixlen(&netmask); |
3c9b8860 | 259 | |
f414a269 TG |
260 | address->family = AF_INET; |
261 | address->in_addr.in = addr; | |
262 | address->prefixlen = prefixlen; | |
3c9b8860 | 263 | |
483d099e | 264 | address_remove(address, link, link_address_remove_handler); |
f414a269 | 265 | } |
3c9b8860 TG |
266 | } |
267 | ||
27cb34f5 | 268 | if (link->network->dhcp_use_mtu) { |
3c9b8860 TG |
269 | uint16_t mtu; |
270 | ||
271 | r = sd_dhcp_lease_get_mtu(link->dhcp_lease, &mtu); | |
272 | if (r >= 0 && link->original_mtu != mtu) { | |
273 | r = link_set_mtu(link, link->original_mtu); | |
274 | if (r < 0) { | |
79008bdd | 275 | log_link_warning(link, |
3c9b8860 TG |
276 | "DHCP error: could not reset MTU"); |
277 | link_enter_failed(link); | |
278 | return r; | |
279 | } | |
280 | } | |
281 | } | |
282 | ||
27cb34f5 | 283 | if (link->network->dhcp_use_hostname) { |
3c9b8860 TG |
284 | const char *hostname = NULL; |
285 | ||
27cb34f5 LP |
286 | if (link->network->dhcp_hostname) |
287 | hostname = link->network->dhcp_hostname; | |
dce391e7 LP |
288 | else |
289 | (void) sd_dhcp_lease_get_hostname(link->dhcp_lease, &hostname); | |
a7d0ef44 | 290 | |
dce391e7 LP |
291 | if (hostname) { |
292 | /* If a hostname was set due to the lease, then unset it now. */ | |
59eb33e0 | 293 | r = manager_set_hostname(link->manager, NULL); |
3c9b8860 | 294 | if (r < 0) |
dce391e7 | 295 | log_link_warning_errno(link, r, "Failed to reset transient hostname: %m"); |
3c9b8860 TG |
296 | } |
297 | } | |
298 | ||
299 | link->dhcp_lease = sd_dhcp_lease_unref(link->dhcp_lease); | |
6a3e5f6a | 300 | link_dirty(link); |
3c9b8860 TG |
301 | link->dhcp4_configured = false; |
302 | ||
303 | return 0; | |
304 | } | |
305 | ||
1c4baffc | 306 | static int dhcp4_address_handler(sd_netlink *rtnl, sd_netlink_message *m, |
3c9b8860 | 307 | void *userdata) { |
8e766630 | 308 | _cleanup_(link_unrefp) Link *link = userdata; |
3c9b8860 TG |
309 | int r; |
310 | ||
311 | assert(link); | |
312 | ||
1c4baffc | 313 | r = sd_netlink_message_get_errno(m); |
3c9b8860 | 314 | if (r < 0 && r != -EEXIST) { |
f6b8196f | 315 | log_link_error_errno(link, r, "Could not set DHCPv4 address: %m"); |
3c9b8860 | 316 | link_enter_failed(link); |
45af44d4 | 317 | } else if (r >= 0) |
200a0868 | 318 | manager_rtnl_process_address(rtnl, m, link->manager); |
3c9b8860 TG |
319 | |
320 | link_set_dhcp_routes(link); | |
321 | ||
223932c7 AH |
322 | if (link->dhcp4_messages == 0) { |
323 | link->dhcp4_configured = true; | |
324 | link_check_ready(link); | |
325 | } | |
326 | ||
3c9b8860 TG |
327 | return 1; |
328 | } | |
329 | ||
330 | static int dhcp4_update_address(Link *link, | |
331 | struct in_addr *address, | |
332 | struct in_addr *netmask, | |
333 | uint32_t lifetime) { | |
8e766630 | 334 | _cleanup_(address_freep) Address *addr = NULL; |
3c9b8860 TG |
335 | unsigned prefixlen; |
336 | int r; | |
337 | ||
338 | assert(address); | |
339 | assert(netmask); | |
340 | assert(lifetime); | |
341 | ||
5a941f5f | 342 | prefixlen = in4_addr_netmask_to_prefixlen(netmask); |
3c9b8860 | 343 | |
f0213e37 | 344 | r = address_new(&addr); |
3c9b8860 TG |
345 | if (r < 0) |
346 | return r; | |
347 | ||
348 | addr->family = AF_INET; | |
349 | addr->in_addr.in.s_addr = address->s_addr; | |
350 | addr->cinfo.ifa_prefered = lifetime; | |
351 | addr->cinfo.ifa_valid = lifetime; | |
352 | addr->prefixlen = prefixlen; | |
353 | addr->broadcast.s_addr = address->s_addr | ~netmask->s_addr; | |
354 | ||
66669078 TG |
355 | /* allow reusing an existing address and simply update its lifetime |
356 | * in case it already exists */ | |
483d099e | 357 | r = address_configure(addr, link, dhcp4_address_handler, true); |
3c9b8860 TG |
358 | if (r < 0) |
359 | return r; | |
360 | ||
361 | return 0; | |
362 | } | |
363 | ||
364 | static int dhcp_lease_renew(sd_dhcp_client *client, Link *link) { | |
365 | sd_dhcp_lease *lease; | |
366 | struct in_addr address; | |
367 | struct in_addr netmask; | |
368 | uint32_t lifetime = CACHE_INFO_INFINITY_LIFE_TIME; | |
369 | int r; | |
370 | ||
371 | assert(link); | |
372 | assert(client); | |
373 | assert(link->network); | |
374 | ||
375 | r = sd_dhcp_client_get_lease(client, &lease); | |
f6b8196f LP |
376 | if (r < 0) |
377 | return log_link_warning_errno(link, r, "DHCP error: no lease: %m"); | |
3c9b8860 TG |
378 | |
379 | sd_dhcp_lease_unref(link->dhcp_lease); | |
380 | link->dhcp4_configured = false; | |
e6b18ffa | 381 | link->dhcp_lease = sd_dhcp_lease_ref(lease); |
6a3e5f6a | 382 | link_dirty(link); |
3c9b8860 TG |
383 | |
384 | r = sd_dhcp_lease_get_address(lease, &address); | |
f6b8196f LP |
385 | if (r < 0) |
386 | return log_link_warning_errno(link, r, "DHCP error: no address: %m"); | |
3c9b8860 TG |
387 | |
388 | r = sd_dhcp_lease_get_netmask(lease, &netmask); | |
f6b8196f LP |
389 | if (r < 0) |
390 | return log_link_warning_errno(link, r, "DHCP error: no netmask: %m"); | |
3c9b8860 TG |
391 | |
392 | if (!link->network->dhcp_critical) { | |
f6b8196f LP |
393 | r = sd_dhcp_lease_get_lifetime(link->dhcp_lease, &lifetime); |
394 | if (r < 0) | |
395 | return log_link_warning_errno(link, r, "DHCP error: no lifetime: %m"); | |
3c9b8860 TG |
396 | } |
397 | ||
398 | r = dhcp4_update_address(link, &address, &netmask, lifetime); | |
399 | if (r < 0) { | |
f6b8196f | 400 | log_link_warning_errno(link, r, "Could not update IP address: %m"); |
3c9b8860 TG |
401 | link_enter_failed(link); |
402 | return r; | |
403 | } | |
404 | ||
405 | return 0; | |
406 | } | |
407 | ||
408 | static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) { | |
409 | sd_dhcp_lease *lease; | |
410 | struct in_addr address; | |
411 | struct in_addr netmask; | |
412 | struct in_addr gateway; | |
413 | unsigned prefixlen; | |
414 | uint32_t lifetime = CACHE_INFO_INFINITY_LIFE_TIME; | |
415 | int r; | |
416 | ||
417 | assert(client); | |
418 | assert(link); | |
419 | ||
420 | r = sd_dhcp_client_get_lease(client, &lease); | |
f2341e0a | 421 | if (r < 0) |
f6b8196f | 422 | return log_link_error_errno(link, r, "DHCP error: No lease: %m"); |
3c9b8860 TG |
423 | |
424 | r = sd_dhcp_lease_get_address(lease, &address); | |
f2341e0a | 425 | if (r < 0) |
f6b8196f | 426 | return log_link_error_errno(link, r, "DHCP error: No address: %m"); |
3c9b8860 TG |
427 | |
428 | r = sd_dhcp_lease_get_netmask(lease, &netmask); | |
f2341e0a | 429 | if (r < 0) |
f6b8196f | 430 | return log_link_error_errno(link, r, "DHCP error: No netmask: %m"); |
3c9b8860 | 431 | |
5a941f5f | 432 | prefixlen = in4_addr_netmask_to_prefixlen(&netmask); |
3c9b8860 TG |
433 | |
434 | r = sd_dhcp_lease_get_router(lease, &gateway); | |
397d15fd | 435 | if (r < 0 && r != -ENODATA) |
f6b8196f | 436 | return log_link_error_errno(link, r, "DHCP error: Could not get gateway: %m"); |
3c9b8860 TG |
437 | |
438 | if (r >= 0) | |
f2341e0a LP |
439 | log_struct(LOG_INFO, |
440 | LOG_LINK_INTERFACE(link), | |
441 | LOG_LINK_MESSAGE(link, "DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u", | |
442 | ADDRESS_FMT_VAL(address), | |
443 | prefixlen, | |
444 | ADDRESS_FMT_VAL(gateway)), | |
445 | "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address), | |
446 | "PREFIXLEN=%u", prefixlen, | |
a1230ff9 | 447 | "GATEWAY=%u.%u.%u.%u", ADDRESS_FMT_VAL(gateway)); |
3c9b8860 | 448 | else |
f2341e0a LP |
449 | log_struct(LOG_INFO, |
450 | LOG_LINK_INTERFACE(link), | |
451 | LOG_LINK_MESSAGE(link, "DHCPv4 address %u.%u.%u.%u/%u", | |
452 | ADDRESS_FMT_VAL(address), | |
453 | prefixlen), | |
454 | "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address), | |
a1230ff9 | 455 | "PREFIXLEN=%u", prefixlen); |
3c9b8860 | 456 | |
e6b18ffa | 457 | link->dhcp_lease = sd_dhcp_lease_ref(lease); |
6a3e5f6a | 458 | link_dirty(link); |
3c9b8860 | 459 | |
27cb34f5 | 460 | if (link->network->dhcp_use_mtu) { |
3c9b8860 TG |
461 | uint16_t mtu; |
462 | ||
463 | r = sd_dhcp_lease_get_mtu(lease, &mtu); | |
464 | if (r >= 0) { | |
465 | r = link_set_mtu(link, mtu); | |
466 | if (r < 0) | |
f2341e0a | 467 | log_link_error_errno(link, r, "Failed to set MTU to %" PRIu16 ": %m", mtu); |
3c9b8860 TG |
468 | } |
469 | } | |
470 | ||
27cb34f5 | 471 | if (link->network->dhcp_use_hostname) { |
2de2abad LB |
472 | const char *dhcpname = NULL; |
473 | _cleanup_free_ char *hostname = NULL; | |
3c9b8860 | 474 | |
27cb34f5 | 475 | if (link->network->dhcp_hostname) |
2de2abad | 476 | dhcpname = link->network->dhcp_hostname; |
dce391e7 | 477 | else |
2de2abad LB |
478 | (void) sd_dhcp_lease_get_hostname(lease, &dhcpname); |
479 | ||
480 | if (dhcpname) { | |
481 | r = shorten_overlong(dhcpname, &hostname); | |
482 | if (r < 0) | |
483 | log_link_warning_errno(link, r, "Unable to shorten overlong DHCP hostname '%s', ignoring: %m", dhcpname); | |
484 | if (r == 1) | |
485 | log_link_notice(link, "Overlong DCHP hostname received, shortened from '%s' to '%s'", dhcpname, hostname); | |
486 | } | |
a7d0ef44 | 487 | |
dce391e7 | 488 | if (hostname) { |
59eb33e0 | 489 | r = manager_set_hostname(link->manager, hostname); |
3c9b8860 | 490 | if (r < 0) |
f2341e0a | 491 | log_link_error_errno(link, r, "Failed to set transient hostname to '%s': %m", hostname); |
3c9b8860 TG |
492 | } |
493 | } | |
494 | ||
27cb34f5 | 495 | if (link->network->dhcp_use_timezone) { |
21b80ad1 LP |
496 | const char *tz = NULL; |
497 | ||
498 | (void) sd_dhcp_lease_get_timezone(link->dhcp_lease, &tz); | |
499 | ||
500 | if (tz) { | |
59eb33e0 | 501 | r = manager_set_timezone(link->manager, tz); |
21b80ad1 LP |
502 | if (r < 0) |
503 | log_link_error_errno(link, r, "Failed to set timezone to '%s': %m", tz); | |
504 | } | |
505 | } | |
506 | ||
3c9b8860 | 507 | if (!link->network->dhcp_critical) { |
f2341e0a | 508 | r = sd_dhcp_lease_get_lifetime(link->dhcp_lease, &lifetime); |
3c9b8860 | 509 | if (r < 0) { |
f2341e0a | 510 | log_link_warning_errno(link, r, "DHCP error: no lifetime: %m"); |
3c9b8860 TG |
511 | return r; |
512 | } | |
513 | } | |
514 | ||
515 | r = dhcp4_update_address(link, &address, &netmask, lifetime); | |
516 | if (r < 0) { | |
f2341e0a | 517 | log_link_warning_errno(link, r, "Could not update IP address: %m"); |
3c9b8860 TG |
518 | link_enter_failed(link); |
519 | return r; | |
520 | } | |
521 | ||
522 | return 0; | |
523 | } | |
524 | static void dhcp4_handler(sd_dhcp_client *client, int event, void *userdata) { | |
525 | Link *link = userdata; | |
526 | int r = 0; | |
527 | ||
528 | assert(link); | |
529 | assert(link->network); | |
530 | assert(link->manager); | |
531 | ||
532 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
533 | return; | |
534 | ||
535 | switch (event) { | |
03748142 DH |
536 | case SD_DHCP_CLIENT_EVENT_EXPIRED: |
537 | case SD_DHCP_CLIENT_EVENT_STOP: | |
538 | case SD_DHCP_CLIENT_EVENT_IP_CHANGE: | |
3c9b8860 | 539 | if (link->network->dhcp_critical) { |
f6b8196f | 540 | log_link_error(link, "DHCPv4 connection considered system critical, ignoring request to reconfigure it."); |
3c9b8860 TG |
541 | return; |
542 | } | |
543 | ||
544 | if (link->dhcp_lease) { | |
545 | r = dhcp_lease_lost(link); | |
546 | if (r < 0) { | |
547 | link_enter_failed(link); | |
548 | return; | |
549 | } | |
550 | } | |
551 | ||
03748142 | 552 | if (event == SD_DHCP_CLIENT_EVENT_IP_CHANGE) { |
3c9b8860 TG |
553 | r = dhcp_lease_acquired(client, link); |
554 | if (r < 0) { | |
555 | link_enter_failed(link); | |
556 | return; | |
557 | } | |
558 | } | |
559 | ||
560 | break; | |
03748142 | 561 | case SD_DHCP_CLIENT_EVENT_RENEW: |
3c9b8860 TG |
562 | r = dhcp_lease_renew(client, link); |
563 | if (r < 0) { | |
564 | link_enter_failed(link); | |
565 | return; | |
566 | } | |
567 | break; | |
03748142 | 568 | case SD_DHCP_CLIENT_EVENT_IP_ACQUIRE: |
3c9b8860 TG |
569 | r = dhcp_lease_acquired(client, link); |
570 | if (r < 0) { | |
571 | link_enter_failed(link); | |
572 | return; | |
573 | } | |
574 | break; | |
575 | default: | |
576 | if (event < 0) | |
f6b8196f | 577 | log_link_warning_errno(link, event, "DHCP error: Client failed: %m"); |
3c9b8860 | 578 | else |
f6b8196f | 579 | log_link_warning(link, "DHCP unknown event: %i", event); |
3c9b8860 TG |
580 | break; |
581 | } | |
582 | ||
583 | return; | |
584 | } | |
585 | ||
7192bb81 LP |
586 | static int dhcp4_set_hostname(Link *link) { |
587 | _cleanup_free_ char *hostname = NULL; | |
588 | const char *hn; | |
589 | int r; | |
590 | ||
591 | assert(link); | |
592 | ||
593 | if (!link->network->dhcp_send_hostname) | |
594 | hn = NULL; | |
595 | else if (link->network->dhcp_hostname) | |
596 | hn = link->network->dhcp_hostname; | |
597 | else { | |
598 | r = gethostname_strict(&hostname); | |
599 | if (r < 0 && r != -ENXIO) /* ENXIO: no hostname set or hostname is "localhost" */ | |
600 | return r; | |
601 | ||
602 | hn = hostname; | |
603 | } | |
604 | ||
605 | return sd_dhcp_client_set_hostname(link->dhcp_client, hn); | |
606 | } | |
607 | ||
64b21ece | 608 | static bool promote_secondaries_enabled(const char *ifname) { |
3f550c31 HV |
609 | _cleanup_free_ char *promote_secondaries_sysctl = NULL; |
610 | char *promote_secondaries_path; | |
64b21ece MV |
611 | int r; |
612 | ||
613 | promote_secondaries_path = strjoina("net/ipv4/conf/", ifname, "/promote_secondaries"); | |
614 | r = sysctl_read(promote_secondaries_path, &promote_secondaries_sysctl); | |
615 | if (r < 0) { | |
616 | log_debug_errno(r, "Cannot read sysctl %s", promote_secondaries_path); | |
617 | return false; | |
618 | } | |
619 | ||
620 | truncate_nl(promote_secondaries_sysctl); | |
621 | r = parse_boolean(promote_secondaries_sysctl); | |
622 | if (r < 0) | |
623 | log_warning_errno(r, "Cannot parse sysctl %s with content %s as boolean", promote_secondaries_path, promote_secondaries_sysctl); | |
624 | return r > 0; | |
625 | } | |
626 | ||
627 | /* dhcp4_set_promote_secondaries will ensure this interface has | |
628 | * the "promote_secondaries" option in the kernel set. If this sysctl | |
629 | * is not set DHCP will work only as long as the IP address does not | |
630 | * changes between leases. The kernel will remove all secondary IP | |
631 | * addresses of an interface otherwise. The way systemd-network works | |
632 | * is that the new IP of a lease is added as a secondary IP and when | |
633 | * the primary one expires it relies on the kernel to promote the | |
634 | * secondary IP. See also https://github.com/systemd/systemd/issues/7163 | |
635 | */ | |
636 | int dhcp4_set_promote_secondaries(Link *link) { | |
637 | int r; | |
638 | ||
639 | assert(link); | |
640 | assert(link->network); | |
641 | assert(link->network->dhcp & ADDRESS_FAMILY_IPV4); | |
642 | ||
643 | /* check if the kernel has promote_secondaries enabled for our | |
644 | * interface. If it is not globally enabled or enabled for the | |
645 | * specific interface we must either enable it. | |
646 | */ | |
8e1a7253 | 647 | if (!(promote_secondaries_enabled("all") || promote_secondaries_enabled(link->ifname))) { |
64b21ece MV |
648 | char *promote_secondaries_path = NULL; |
649 | ||
650 | log_link_debug(link, "promote_secondaries is unset, setting it"); | |
651 | promote_secondaries_path = strjoina("net/ipv4/conf/", link->ifname, "/promote_secondaries"); | |
652 | r = sysctl_write(promote_secondaries_path, "1"); | |
653 | if (r < 0) | |
654 | log_link_warning_errno(link, r, "cannot set sysctl %s to 1", promote_secondaries_path); | |
655 | return r > 0; | |
656 | } | |
657 | ||
658 | return 0; | |
659 | } | |
660 | ||
3c9b8860 TG |
661 | int dhcp4_configure(Link *link) { |
662 | int r; | |
663 | ||
664 | assert(link); | |
665 | assert(link->network); | |
e0ee46f2 | 666 | assert(link->network->dhcp & ADDRESS_FAMILY_IPV4); |
3c9b8860 | 667 | |
0bc70f1d | 668 | if (!link->dhcp_client) { |
db3d2358 | 669 | r = sd_dhcp_client_new(&link->dhcp_client, link->network->dhcp_anonymize); |
0bc70f1d TG |
670 | if (r < 0) |
671 | return r; | |
672 | } | |
3c9b8860 TG |
673 | |
674 | r = sd_dhcp_client_attach_event(link->dhcp_client, NULL, 0); | |
675 | if (r < 0) | |
676 | return r; | |
677 | ||
76253e73 DW |
678 | r = sd_dhcp_client_set_mac(link->dhcp_client, |
679 | (const uint8_t *) &link->mac, | |
680 | sizeof (link->mac), ARPHRD_ETHER); | |
3c9b8860 TG |
681 | if (r < 0) |
682 | return r; | |
683 | ||
2f8e7633 | 684 | r = sd_dhcp_client_set_ifindex(link->dhcp_client, link->ifindex); |
3c9b8860 TG |
685 | if (r < 0) |
686 | return r; | |
687 | ||
688 | r = sd_dhcp_client_set_callback(link->dhcp_client, dhcp4_handler, link); | |
689 | if (r < 0) | |
690 | return r; | |
691 | ||
692 | r = sd_dhcp_client_set_request_broadcast(link->dhcp_client, | |
693 | link->network->dhcp_broadcast); | |
694 | if (r < 0) | |
695 | return r; | |
696 | ||
697 | if (link->mtu) { | |
698 | r = sd_dhcp_client_set_mtu(link->dhcp_client, link->mtu); | |
699 | if (r < 0) | |
700 | return r; | |
701 | } | |
702 | ||
27cb34f5 | 703 | if (link->network->dhcp_use_mtu) { |
39745a5a | 704 | r = sd_dhcp_client_set_request_option(link->dhcp_client, |
22805d92 | 705 | SD_DHCP_OPTION_INTERFACE_MTU); |
39745a5a LP |
706 | if (r < 0) |
707 | return r; | |
3c9b8860 TG |
708 | } |
709 | ||
5e77a146 | 710 | /* NOTE: even if this variable is called "use", it also "sends" PRL |
711 | * options, maybe there should be a different configuration variable | |
712 | * to send or not route options?. */ | |
28522b0d | 713 | /* NOTE: when using Anonymize=yes, routes PRL options are sent |
714 | * by default, so they don't need to be added here. */ | |
5e77a146 | 715 | if (link->network->dhcp_use_routes && !link->network->dhcp_anonymize) { |
3c9b8860 | 716 | r = sd_dhcp_client_set_request_option(link->dhcp_client, |
22805d92 | 717 | SD_DHCP_OPTION_STATIC_ROUTE); |
3c9b8860 TG |
718 | if (r < 0) |
719 | return r; | |
720 | r = sd_dhcp_client_set_request_option(link->dhcp_client, | |
22805d92 | 721 | SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE); |
7d6884b6 TA |
722 | if (r < 0) |
723 | return r; | |
3c9b8860 TG |
724 | } |
725 | ||
ead36ce6 | 726 | if (link->network->dhcp_use_ntp) { |
727 | r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_NTP_SERVER); | |
728 | if (r < 0) | |
729 | return r; | |
730 | } | |
4b7b5abb | 731 | |
89573b37 | 732 | if (link->network->dhcp_use_timezone) { |
733 | r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_NEW_TZDB_TIMEZONE); | |
734 | if (r < 0) | |
735 | return r; | |
736 | } | |
8eb9058d | 737 | |
7192bb81 LP |
738 | r = dhcp4_set_hostname(link); |
739 | if (r < 0) | |
740 | return r; | |
3c9b8860 TG |
741 | |
742 | if (link->network->dhcp_vendor_class_identifier) { | |
743 | r = sd_dhcp_client_set_vendor_class_identifier(link->dhcp_client, | |
744 | link->network->dhcp_vendor_class_identifier); | |
745 | if (r < 0) | |
746 | return r; | |
747 | } | |
748 | ||
af1c0de0 SS |
749 | if (link->network->dhcp_user_class) { |
750 | r = sd_dhcp_client_set_user_class(link->dhcp_client, (const char **) link->network->dhcp_user_class); | |
751 | if (r < 0) | |
752 | return r; | |
753 | } | |
754 | ||
9faed222 SS |
755 | if (link->network->dhcp_client_port) { |
756 | r = sd_dhcp_client_set_client_port(link->dhcp_client, link->network->dhcp_client_port); | |
757 | if (r < 0) | |
758 | return r; | |
759 | } | |
760 | ||
3e43b2cd | 761 | switch (link->network->dhcp_client_identifier) { |
8341a5c3 | 762 | case DHCP_CLIENT_ID_DUID: { |
413708d1 | 763 | /* If configured, apply user specified DUID and/or IAID */ |
8341a5c3 ZJS |
764 | const DUID *duid = link_duid(link); |
765 | ||
766 | r = sd_dhcp_client_set_iaid_duid(link->dhcp_client, | |
767 | link->network->iaid, | |
768 | duid->type, | |
769 | duid->raw_data_len > 0 ? duid->raw_data : NULL, | |
770 | duid->raw_data_len); | |
413708d1 VK |
771 | if (r < 0) |
772 | return r; | |
3e43b2cd | 773 | break; |
8341a5c3 | 774 | } |
dace710c YW |
775 | case DHCP_CLIENT_ID_DUID_ONLY: { |
776 | /* If configured, apply user specified DUID */ | |
777 | const DUID *duid = link_duid(link); | |
778 | ||
779 | r = sd_dhcp_client_set_duid(link->dhcp_client, | |
780 | duid->type, | |
781 | duid->raw_data_len > 0 ? duid->raw_data : NULL, | |
782 | duid->raw_data_len); | |
783 | if (r < 0) | |
784 | return r; | |
785 | break; | |
786 | } | |
3e43b2cd JJ |
787 | case DHCP_CLIENT_ID_MAC: |
788 | r = sd_dhcp_client_set_client_id(link->dhcp_client, | |
789 | ARPHRD_ETHER, | |
790 | (const uint8_t *) &link->mac, | |
8341a5c3 | 791 | sizeof(link->mac)); |
3e43b2cd JJ |
792 | if (r < 0) |
793 | return r; | |
794 | break; | |
795 | default: | |
796 | assert_not_reached("Unknown client identifier type."); | |
797 | } | |
798 | ||
3c9b8860 TG |
799 | return 0; |
800 | } |