]>
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 LP |
6 | #include "alloc-util.h" |
7 | #include "dhcp-lease-internal.h" | |
958b66ea | 8 | #include "hostname-util.h" |
64b21ece | 9 | #include "parse-util.h" |
fc1ba79d | 10 | #include "netdev/vrf.h" |
3c9b8860 | 11 | #include "network-internal.h" |
23f53b99 TG |
12 | #include "networkd-link.h" |
13 | #include "networkd-manager.h" | |
14 | #include "networkd-network.h" | |
64b21ece MV |
15 | #include "string-util.h" |
16 | #include "sysctl-util.h" | |
3c9b8860 | 17 | |
1c4baffc | 18 | static int dhcp4_route_handler(sd_netlink *rtnl, sd_netlink_message *m, |
3c9b8860 | 19 | void *userdata) { |
8e766630 | 20 | _cleanup_(link_unrefp) Link *link = userdata; |
3c9b8860 TG |
21 | int r; |
22 | ||
23 | assert(link); | |
6cf4a01c | 24 | assert(link->dhcp4_messages > 0); |
3c9b8860 | 25 | |
313cefa1 | 26 | link->dhcp4_messages--; |
3c9b8860 | 27 | |
1c4baffc | 28 | r = sd_netlink_message_get_errno(m); |
3c9b8860 | 29 | if (r < 0 && r != -EEXIST) { |
f6b8196f | 30 | log_link_error_errno(link, r, "Could not set DHCPv4 route: %m"); |
3c9b8860 TG |
31 | link_enter_failed(link); |
32 | } | |
33 | ||
6cf4a01c | 34 | if (link->dhcp4_messages == 0) { |
3c9b8860 | 35 | link->dhcp4_configured = true; |
8012cd39 | 36 | link_check_ready(link); |
3c9b8860 TG |
37 | } |
38 | ||
39 | return 1; | |
40 | } | |
41 | ||
d6eac9bd DW |
42 | static int route_scope_from_address(const Route *route, const struct in_addr *self_addr) { |
43 | assert(route); | |
44 | assert(self_addr); | |
45 | ||
46 | if (in_addr_is_localhost(AF_INET, &route->dst) || | |
47 | (self_addr->s_addr && route->dst.in.s_addr == self_addr->s_addr)) | |
48 | return RT_SCOPE_HOST; | |
49 | else if (in4_addr_is_null(&route->gw.in)) | |
50 | return RT_SCOPE_LINK; | |
51 | else | |
52 | return RT_SCOPE_UNIVERSE; | |
53 | } | |
54 | ||
3c9b8860 | 55 | static int link_set_dhcp_routes(Link *link) { |
f8693fc7 | 56 | _cleanup_free_ sd_dhcp_route **static_routes = NULL; |
8cdc46e7 SS |
57 | bool classless_route = false, static_route = false; |
58 | struct in_addr gateway, address; | |
3c9b8860 | 59 | int r, n, i; |
fc1ba79d | 60 | uint32_t table; |
3c9b8860 TG |
61 | |
62 | assert(link); | |
0c9b15a3 AJ |
63 | |
64 | if (!link->dhcp_lease) /* link went down while we configured the IP addresses? */ | |
65 | return 0; | |
66 | ||
67 | if (!link->network) /* link went down while we configured the IP addresses? */ | |
68 | return 0; | |
964b26fe SS |
69 | |
70 | if (!link->network->dhcp_use_routes) | |
71 | return 0; | |
3c9b8860 | 72 | |
fc1ba79d AR |
73 | /* When the interface is part of an VRF use the VRFs routing table, unless |
74 | * there is a another table specified. */ | |
75 | table = link->network->dhcp_route_table; | |
76 | if (!link->network->dhcp_route_table_set && link->network->vrf != NULL) | |
77 | table = VRF(link->network->vrf)->table; | |
78 | ||
b23aec0d DW |
79 | r = sd_dhcp_lease_get_address(link->dhcp_lease, &address); |
80 | if (r < 0) | |
81 | return log_link_warning_errno(link, r, "DHCP error: could not get address: %m"); | |
82 | ||
5f04a209 | 83 | n = sd_dhcp_lease_get_routes(link->dhcp_lease, &static_routes); |
599c44e6 YW |
84 | if (n == -ENODATA) |
85 | log_link_debug_errno(link, n, "DHCP: No routes received from DHCP server: %m"); | |
86 | else if (n < 0) | |
8dc787d1 | 87 | log_link_debug_errno(link, n, "DHCP error: could not get routes: %m"); |
5f04a209 | 88 | |
8cdc46e7 SS |
89 | for (i = 0; i < n; i++) { |
90 | if (static_routes[i]->option == SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE) | |
91 | classless_route = true; | |
92 | ||
93 | if (static_routes[i]->option == SD_DHCP_OPTION_STATIC_ROUTE) | |
94 | static_route = true; | |
95 | } | |
96 | ||
5f04a209 | 97 | for (i = 0; i < n; i++) { |
8e766630 | 98 | _cleanup_(route_freep) Route *route = NULL; |
5f04a209 | 99 | |
8cdc46e7 SS |
100 | /* if the DHCP server returns both a Classless Static Routes option and a Static Routes option, |
101 | the DHCP client MUST ignore the Static Routes option. */ | |
102 | if (classless_route && static_routes[i]->option == SD_DHCP_OPTION_STATIC_ROUTE) | |
103 | continue; | |
104 | ||
5f04a209 SS |
105 | r = route_new(&route); |
106 | if (r < 0) | |
107 | return log_link_error_errno(link, r, "Could not allocate route: %m"); | |
108 | ||
109 | route->family = AF_INET; | |
110 | route->protocol = RTPROT_DHCP; | |
111 | assert_se(sd_dhcp_route_get_gateway(static_routes[i], &route->gw.in) >= 0); | |
112 | assert_se(sd_dhcp_route_get_destination(static_routes[i], &route->dst.in) >= 0); | |
113 | assert_se(sd_dhcp_route_get_destination_prefix_length(static_routes[i], &route->dst_prefixlen) >= 0); | |
114 | route->priority = link->network->dhcp_route_metric; | |
115 | route->table = table; | |
116 | route->scope = route_scope_from_address(route, &address); | |
117 | ||
118 | r = route_configure(route, link, dhcp4_route_handler); | |
119 | if (r < 0) | |
120 | return log_link_warning_errno(link, r, "Could not set host route: %m"); | |
121 | ||
122 | link->dhcp4_messages++; | |
123 | } | |
124 | ||
3c9b8860 | 125 | r = sd_dhcp_lease_get_router(link->dhcp_lease, &gateway); |
444b0170 | 126 | if (r == -ENODATA) |
fb14aa7e | 127 | log_link_info_errno(link, r, "DHCP: No gateway received from DHCP server: %m"); |
444b0170 SS |
128 | else if (r < 0) |
129 | log_link_warning_errno(link, r, "DHCP error: could not get gateway: %m"); | |
f6b8196f | 130 | |
5f04a209 SS |
131 | /* According to RFC 3442: If the DHCP server returns both a Classless Static Routes option and |
132 | a Router option, the DHCP client MUST ignore the Router option. */ | |
8cdc46e7 SS |
133 | if (classless_route && static_route) |
134 | log_link_warning(link, "Classless static routes received from DHCP server: ignoring static-route option and router option"); | |
135 | ||
136 | if (r >= 0 && !classless_route) { | |
8e766630 LP |
137 | _cleanup_(route_freep) Route *route = NULL; |
138 | _cleanup_(route_freep) Route *route_gw = NULL; | |
3c9b8860 | 139 | |
ed9e361a | 140 | r = route_new(&route); |
f6b8196f LP |
141 | if (r < 0) |
142 | return log_link_error_errno(link, r, "Could not allocate route: %m"); | |
3c9b8860 | 143 | |
ed9e361a TG |
144 | route->protocol = RTPROT_DHCP; |
145 | ||
146 | r = route_new(&route_gw); | |
f6b8196f LP |
147 | if (r < 0) |
148 | return log_link_error_errno(link, r, "Could not allocate route: %m"); | |
3c9b8860 TG |
149 | |
150 | /* The dhcp netmask may mask out the gateway. Add an explicit | |
151 | * route for the gw host so that we can route no matter the | |
152 | * netmask or existing kernel route tables. */ | |
153 | route_gw->family = AF_INET; | |
2ce40956 | 154 | route_gw->dst.in = gateway; |
3c9b8860 | 155 | route_gw->dst_prefixlen = 32; |
2ce40956 | 156 | route_gw->prefsrc.in = address; |
3c9b8860 | 157 | route_gw->scope = RT_SCOPE_LINK; |
ed9e361a | 158 | route_gw->protocol = RTPROT_DHCP; |
86655331 | 159 | route_gw->priority = link->network->dhcp_route_metric; |
fc1ba79d | 160 | route_gw->table = table; |
3c9b8860 | 161 | |
483d099e | 162 | r = route_configure(route_gw, link, dhcp4_route_handler); |
f6b8196f LP |
163 | if (r < 0) |
164 | return log_link_warning_errno(link, r, "Could not set host route: %m"); | |
3c9b8860 | 165 | |
313cefa1 | 166 | link->dhcp4_messages++; |
3c9b8860 TG |
167 | |
168 | route->family = AF_INET; | |
2ce40956 TG |
169 | route->gw.in = gateway; |
170 | route->prefsrc.in = address; | |
86655331 | 171 | route->priority = link->network->dhcp_route_metric; |
fc1ba79d | 172 | route->table = table; |
3c9b8860 | 173 | |
483d099e | 174 | r = route_configure(route, link, dhcp4_route_handler); |
3c9b8860 | 175 | if (r < 0) { |
f6b8196f | 176 | log_link_warning_errno(link, r, "Could not set routes: %m"); |
3c9b8860 TG |
177 | link_enter_failed(link); |
178 | return r; | |
179 | } | |
180 | ||
313cefa1 | 181 | link->dhcp4_messages++; |
3c9b8860 TG |
182 | } |
183 | ||
3c9b8860 TG |
184 | return 0; |
185 | } | |
186 | ||
187 | static int dhcp_lease_lost(Link *link) { | |
8e766630 | 188 | _cleanup_(address_freep) Address *address = NULL; |
3c9b8860 TG |
189 | struct in_addr addr; |
190 | struct in_addr netmask; | |
191 | struct in_addr gateway; | |
f414a269 | 192 | unsigned prefixlen = 0; |
3c9b8860 TG |
193 | int r; |
194 | ||
195 | assert(link); | |
196 | assert(link->dhcp_lease); | |
197 | ||
79008bdd | 198 | log_link_warning(link, "DHCP lease lost"); |
3c9b8860 | 199 | |
27cb34f5 | 200 | if (link->network->dhcp_use_routes) { |
f8693fc7 | 201 | _cleanup_free_ sd_dhcp_route **routes = NULL; |
3c9b8860 TG |
202 | int n, i; |
203 | ||
204 | n = sd_dhcp_lease_get_routes(link->dhcp_lease, &routes); | |
205 | if (n >= 0) { | |
206 | for (i = 0; i < n; i++) { | |
8e766630 | 207 | _cleanup_(route_freep) Route *route = NULL; |
3c9b8860 | 208 | |
ed9e361a | 209 | r = route_new(&route); |
3c9b8860 TG |
210 | if (r >= 0) { |
211 | route->family = AF_INET; | |
f8693fc7 BG |
212 | assert_se(sd_dhcp_route_get_gateway(routes[i], &route->gw.in) >= 0); |
213 | assert_se(sd_dhcp_route_get_destination(routes[i], &route->dst.in) >= 0); | |
214 | assert_se(sd_dhcp_route_get_destination_prefix_length(routes[i], &route->dst_prefixlen) >= 0); | |
3c9b8860 | 215 | |
91b5f997 | 216 | route_remove(route, link, |
483d099e | 217 | link_route_remove_handler); |
3c9b8860 TG |
218 | } |
219 | } | |
220 | } | |
221 | } | |
222 | ||
f0213e37 | 223 | r = address_new(&address); |
3c9b8860 TG |
224 | if (r >= 0) { |
225 | r = sd_dhcp_lease_get_router(link->dhcp_lease, &gateway); | |
226 | if (r >= 0) { | |
8e766630 LP |
227 | _cleanup_(route_freep) Route *route_gw = NULL; |
228 | _cleanup_(route_freep) Route *route = NULL; | |
3c9b8860 | 229 | |
ed9e361a | 230 | r = route_new(&route_gw); |
3c9b8860 TG |
231 | if (r >= 0) { |
232 | route_gw->family = AF_INET; | |
2ce40956 | 233 | route_gw->dst.in = gateway; |
3c9b8860 TG |
234 | route_gw->dst_prefixlen = 32; |
235 | route_gw->scope = RT_SCOPE_LINK; | |
236 | ||
91b5f997 | 237 | route_remove(route_gw, link, |
483d099e | 238 | link_route_remove_handler); |
3c9b8860 TG |
239 | } |
240 | ||
ed9e361a | 241 | r = route_new(&route); |
3c9b8860 TG |
242 | if (r >= 0) { |
243 | route->family = AF_INET; | |
2ce40956 | 244 | route->gw.in = gateway; |
3c9b8860 | 245 | |
91b5f997 | 246 | route_remove(route, link, |
483d099e | 247 | link_route_remove_handler); |
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 | |
483d099e | 261 | address_remove(address, link, link_address_remove_handler); |
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) { |
8e766630 | 305 | _cleanup_(link_unrefp) 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); |
3c9b8860 | 506 | if (r < 0) { |
f2341e0a | 507 | log_link_warning_errno(link, r, "DHCP error: no lifetime: %m"); |
3c9b8860 TG |
508 | return r; |
509 | } | |
510 | } | |
511 | ||
512 | r = dhcp4_update_address(link, &address, &netmask, lifetime); | |
513 | if (r < 0) { | |
f2341e0a | 514 | log_link_warning_errno(link, r, "Could not update IP address: %m"); |
3c9b8860 TG |
515 | link_enter_failed(link); |
516 | return r; | |
517 | } | |
518 | ||
519 | return 0; | |
520 | } | |
521 | static void dhcp4_handler(sd_dhcp_client *client, int event, void *userdata) { | |
522 | Link *link = userdata; | |
523 | int r = 0; | |
524 | ||
525 | assert(link); | |
526 | assert(link->network); | |
527 | assert(link->manager); | |
528 | ||
529 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
530 | return; | |
531 | ||
532 | switch (event) { | |
03748142 DH |
533 | case SD_DHCP_CLIENT_EVENT_EXPIRED: |
534 | case SD_DHCP_CLIENT_EVENT_STOP: | |
535 | case SD_DHCP_CLIENT_EVENT_IP_CHANGE: | |
3c9b8860 | 536 | if (link->network->dhcp_critical) { |
f6b8196f | 537 | log_link_error(link, "DHCPv4 connection considered system critical, ignoring request to reconfigure it."); |
3c9b8860 TG |
538 | return; |
539 | } | |
540 | ||
541 | if (link->dhcp_lease) { | |
542 | r = dhcp_lease_lost(link); | |
543 | if (r < 0) { | |
544 | link_enter_failed(link); | |
545 | return; | |
546 | } | |
547 | } | |
548 | ||
03748142 | 549 | if (event == SD_DHCP_CLIENT_EVENT_IP_CHANGE) { |
3c9b8860 TG |
550 | r = dhcp_lease_acquired(client, link); |
551 | if (r < 0) { | |
552 | link_enter_failed(link); | |
553 | return; | |
554 | } | |
555 | } | |
556 | ||
557 | break; | |
03748142 | 558 | case SD_DHCP_CLIENT_EVENT_RENEW: |
3c9b8860 TG |
559 | r = dhcp_lease_renew(client, link); |
560 | if (r < 0) { | |
561 | link_enter_failed(link); | |
562 | return; | |
563 | } | |
564 | break; | |
03748142 | 565 | case SD_DHCP_CLIENT_EVENT_IP_ACQUIRE: |
3c9b8860 TG |
566 | r = dhcp_lease_acquired(client, link); |
567 | if (r < 0) { | |
568 | link_enter_failed(link); | |
569 | return; | |
570 | } | |
571 | break; | |
572 | default: | |
573 | if (event < 0) | |
f6b8196f | 574 | log_link_warning_errno(link, event, "DHCP error: Client failed: %m"); |
3c9b8860 | 575 | else |
f6b8196f | 576 | log_link_warning(link, "DHCP unknown event: %i", event); |
3c9b8860 TG |
577 | break; |
578 | } | |
579 | ||
580 | return; | |
581 | } | |
582 | ||
7192bb81 LP |
583 | static int dhcp4_set_hostname(Link *link) { |
584 | _cleanup_free_ char *hostname = NULL; | |
585 | const char *hn; | |
586 | int r; | |
587 | ||
588 | assert(link); | |
589 | ||
590 | if (!link->network->dhcp_send_hostname) | |
591 | hn = NULL; | |
592 | else if (link->network->dhcp_hostname) | |
593 | hn = link->network->dhcp_hostname; | |
594 | else { | |
595 | r = gethostname_strict(&hostname); | |
596 | if (r < 0 && r != -ENXIO) /* ENXIO: no hostname set or hostname is "localhost" */ | |
597 | return r; | |
598 | ||
599 | hn = hostname; | |
600 | } | |
601 | ||
602 | return sd_dhcp_client_set_hostname(link->dhcp_client, hn); | |
603 | } | |
604 | ||
64b21ece | 605 | static bool promote_secondaries_enabled(const char *ifname) { |
3f550c31 HV |
606 | _cleanup_free_ char *promote_secondaries_sysctl = NULL; |
607 | char *promote_secondaries_path; | |
64b21ece MV |
608 | int r; |
609 | ||
610 | promote_secondaries_path = strjoina("net/ipv4/conf/", ifname, "/promote_secondaries"); | |
611 | r = sysctl_read(promote_secondaries_path, &promote_secondaries_sysctl); | |
612 | if (r < 0) { | |
613 | log_debug_errno(r, "Cannot read sysctl %s", promote_secondaries_path); | |
614 | return false; | |
615 | } | |
616 | ||
617 | truncate_nl(promote_secondaries_sysctl); | |
618 | r = parse_boolean(promote_secondaries_sysctl); | |
619 | if (r < 0) | |
620 | log_warning_errno(r, "Cannot parse sysctl %s with content %s as boolean", promote_secondaries_path, promote_secondaries_sysctl); | |
621 | return r > 0; | |
622 | } | |
623 | ||
624 | /* dhcp4_set_promote_secondaries will ensure this interface has | |
625 | * the "promote_secondaries" option in the kernel set. If this sysctl | |
626 | * is not set DHCP will work only as long as the IP address does not | |
627 | * changes between leases. The kernel will remove all secondary IP | |
628 | * addresses of an interface otherwise. The way systemd-network works | |
629 | * is that the new IP of a lease is added as a secondary IP and when | |
630 | * the primary one expires it relies on the kernel to promote the | |
631 | * secondary IP. See also https://github.com/systemd/systemd/issues/7163 | |
632 | */ | |
633 | int dhcp4_set_promote_secondaries(Link *link) { | |
634 | int r; | |
635 | ||
636 | assert(link); | |
637 | assert(link->network); | |
638 | assert(link->network->dhcp & ADDRESS_FAMILY_IPV4); | |
639 | ||
640 | /* check if the kernel has promote_secondaries enabled for our | |
641 | * interface. If it is not globally enabled or enabled for the | |
642 | * specific interface we must either enable it. | |
643 | */ | |
8e1a7253 | 644 | if (!(promote_secondaries_enabled("all") || promote_secondaries_enabled(link->ifname))) { |
64b21ece MV |
645 | char *promote_secondaries_path = NULL; |
646 | ||
647 | log_link_debug(link, "promote_secondaries is unset, setting it"); | |
648 | promote_secondaries_path = strjoina("net/ipv4/conf/", link->ifname, "/promote_secondaries"); | |
649 | r = sysctl_write(promote_secondaries_path, "1"); | |
650 | if (r < 0) | |
651 | log_link_warning_errno(link, r, "cannot set sysctl %s to 1", promote_secondaries_path); | |
652 | return r > 0; | |
653 | } | |
654 | ||
655 | return 0; | |
656 | } | |
657 | ||
3c9b8860 TG |
658 | int dhcp4_configure(Link *link) { |
659 | int r; | |
660 | ||
661 | assert(link); | |
662 | assert(link->network); | |
e0ee46f2 | 663 | assert(link->network->dhcp & ADDRESS_FAMILY_IPV4); |
3c9b8860 | 664 | |
0bc70f1d | 665 | if (!link->dhcp_client) { |
db3d2358 | 666 | r = sd_dhcp_client_new(&link->dhcp_client, link->network->dhcp_anonymize); |
1f6860d9 YW |
667 | if (r == -ENOMEM) |
668 | return log_oom(); | |
0bc70f1d | 669 | if (r < 0) |
1f6860d9 | 670 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to create DHCP4 client: %m"); |
0bc70f1d | 671 | } |
3c9b8860 TG |
672 | |
673 | r = sd_dhcp_client_attach_event(link->dhcp_client, NULL, 0); | |
674 | if (r < 0) | |
1f6860d9 | 675 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to attach event: %m"); |
3c9b8860 | 676 | |
76253e73 DW |
677 | r = sd_dhcp_client_set_mac(link->dhcp_client, |
678 | (const uint8_t *) &link->mac, | |
679 | sizeof (link->mac), ARPHRD_ETHER); | |
3c9b8860 | 680 | if (r < 0) |
1f6860d9 | 681 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set MAC address: %m"); |
3c9b8860 | 682 | |
2f8e7633 | 683 | r = sd_dhcp_client_set_ifindex(link->dhcp_client, link->ifindex); |
3c9b8860 | 684 | if (r < 0) |
1f6860d9 | 685 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set ifindex: %m"); |
3c9b8860 TG |
686 | |
687 | r = sd_dhcp_client_set_callback(link->dhcp_client, dhcp4_handler, link); | |
688 | if (r < 0) | |
1f6860d9 | 689 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set callback: %m"); |
3c9b8860 TG |
690 | |
691 | r = sd_dhcp_client_set_request_broadcast(link->dhcp_client, | |
692 | link->network->dhcp_broadcast); | |
693 | if (r < 0) | |
1f6860d9 | 694 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for broadcast: %m"); |
3c9b8860 TG |
695 | |
696 | if (link->mtu) { | |
697 | r = sd_dhcp_client_set_mtu(link->dhcp_client, link->mtu); | |
698 | if (r < 0) | |
1f6860d9 | 699 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set MTU: %m"); |
3c9b8860 TG |
700 | } |
701 | ||
27cb34f5 | 702 | if (link->network->dhcp_use_mtu) { |
39745a5a | 703 | r = sd_dhcp_client_set_request_option(link->dhcp_client, |
22805d92 | 704 | SD_DHCP_OPTION_INTERFACE_MTU); |
39745a5a | 705 | if (r < 0) |
1f6860d9 | 706 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for MTU: %m"); |
3c9b8860 TG |
707 | } |
708 | ||
5e77a146 | 709 | /* NOTE: even if this variable is called "use", it also "sends" PRL |
710 | * options, maybe there should be a different configuration variable | |
711 | * to send or not route options?. */ | |
28522b0d | 712 | /* NOTE: when using Anonymize=yes, routes PRL options are sent |
713 | * by default, so they don't need to be added here. */ | |
5e77a146 | 714 | if (link->network->dhcp_use_routes && !link->network->dhcp_anonymize) { |
3c9b8860 | 715 | r = sd_dhcp_client_set_request_option(link->dhcp_client, |
22805d92 | 716 | SD_DHCP_OPTION_STATIC_ROUTE); |
3c9b8860 | 717 | if (r < 0) |
1f6860d9 YW |
718 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for static route: %m"); |
719 | ||
3c9b8860 | 720 | r = sd_dhcp_client_set_request_option(link->dhcp_client, |
22805d92 | 721 | SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE); |
7d6884b6 | 722 | if (r < 0) |
1f6860d9 | 723 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for classless static route: %m"); |
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) | |
1f6860d9 | 729 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for NTP server: %m"); |
ead36ce6 | 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) | |
1f6860d9 | 735 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for timezone: %m"); |
89573b37 | 736 | } |
8eb9058d | 737 | |
7192bb81 LP |
738 | r = dhcp4_set_hostname(link); |
739 | if (r < 0) | |
1f6860d9 | 740 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set hostname: %m"); |
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) | |
1f6860d9 | 746 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set vendor class identifier: %m"); |
3c9b8860 TG |
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) | |
1f6860d9 | 752 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set user class: %m"); |
af1c0de0 SS |
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) | |
1f6860d9 | 758 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set listen port: %m"); |
9faed222 SS |
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 | 771 | if (r < 0) |
1f6860d9 | 772 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set DUID: %m"); |
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) | |
1f6860d9 | 784 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set DUID: %m"); |
dace710c YW |
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 | 792 | if (r < 0) |
1f6860d9 | 793 | return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set client ID: %m"); |
3e43b2cd JJ |
794 | break; |
795 | default: | |
796 | assert_not_reached("Unknown client identifier type."); | |
797 | } | |
798 | ||
3c9b8860 TG |
799 | return 0; |
800 | } |