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