]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
3c9b8860 | 2 | |
9aa5d8ba | 3 | #include <netinet/in.h> |
a75b2117 | 4 | #include <netinet/ip.h> |
3c9b8860 | 5 | #include <linux/if.h> |
8f815e8b | 6 | #include <linux/if_arp.h> |
3c9b8860 | 7 | |
b5efdb8a | 8 | #include "alloc-util.h" |
cb29c156 | 9 | #include "dhcp-client-internal.h" |
e2054217 | 10 | #include "hostname-setup.h" |
958b66ea | 11 | #include "hostname-util.h" |
64b21ece | 12 | #include "parse-util.h" |
3c9b8860 | 13 | #include "network-internal.h" |
093e3533 | 14 | #include "networkd-address.h" |
ca5ad760 | 15 | #include "networkd-dhcp4.h" |
76a86ffd | 16 | #include "networkd-ipv4acd.h" |
23f53b99 TG |
17 | #include "networkd-link.h" |
18 | #include "networkd-manager.h" | |
19 | #include "networkd-network.h" | |
76c5a0f2 YW |
20 | #include "networkd-nexthop.h" |
21 | #include "networkd-queue.h" | |
22 | #include "networkd-route.h" | |
0fa8ee6c | 23 | #include "networkd-setlink.h" |
3b5a4fc6 | 24 | #include "networkd-state-file.h" |
ca5ad760 | 25 | #include "string-table.h" |
7e19cc54 | 26 | #include "strv.h" |
64b21ece | 27 | #include "sysctl-util.h" |
3c9b8860 | 28 | |
76c5a0f2 | 29 | static int dhcp4_request_address_and_routes(Link *link, bool announce); |
6906794d | 30 | static int dhcp4_remove_all(Link *link); |
d03073dd | 31 | |
ae7ea5a7 YW |
32 | void network_adjust_dhcp4(Network *network) { |
33 | assert(network); | |
34 | ||
35 | if (!FLAGS_SET(network->dhcp, ADDRESS_FAMILY_IPV4)) | |
36 | return; | |
37 | ||
38 | if (network->dhcp_use_gateway < 0) | |
39 | network->dhcp_use_gateway = network->dhcp_use_routes; | |
40 | ||
f90635f1 YW |
41 | /* RFC7844 section 3.: MAY contain the Client Identifier option |
42 | * Section 3.5: clients MUST use client identifiers based solely on the link-layer address | |
43 | * NOTE: Using MAC, as it does not reveal extra information, and some servers might not answer | |
44 | * if this option is not sent */ | |
45 | if (network->dhcp_anonymize && | |
46 | network->dhcp_client_identifier >= 0 && | |
47 | network->dhcp_client_identifier != DHCP_CLIENT_ID_MAC) { | |
48 | log_warning("%s: ClientIdentifier= is set, although Anonymize=yes. Using ClientIdentifier=mac.", | |
49 | network->filename); | |
ae7ea5a7 | 50 | network->dhcp_client_identifier = DHCP_CLIENT_ID_MAC; |
ae7ea5a7 | 51 | } |
f90635f1 YW |
52 | |
53 | if (network->dhcp_client_identifier < 0) | |
54 | network->dhcp_client_identifier = network->dhcp_anonymize ? DHCP_CLIENT_ID_MAC : DHCP_CLIENT_ID_DUID; | |
ae7ea5a7 YW |
55 | } |
56 | ||
c45fdad6 | 57 | static int dhcp4_release_old_lease(Link *link) { |
6e537f62 | 58 | Route *route; |
6e537f62 | 59 | int k, r = 0; |
d03073dd | 60 | |
6e537f62 | 61 | assert(link); |
d03073dd | 62 | |
6e537f62 YW |
63 | if (!link->dhcp_address_old && set_isempty(link->dhcp_routes_old)) |
64 | return 0; | |
d03073dd | 65 | |
6e537f62 | 66 | log_link_debug(link, "Removing old DHCPv4 address and routes."); |
d03073dd | 67 | |
90e74a66 | 68 | SET_FOREACH(route, link->dhcp_routes_old) { |
f4cc1364 | 69 | k = route_remove(route, NULL, link); |
6e537f62 YW |
70 | if (k < 0) |
71 | r = k; | |
72 | } | |
73 | ||
74 | if (link->dhcp_address_old) { | |
f4cc1364 | 75 | k = address_remove(link->dhcp_address_old, link); |
6e537f62 YW |
76 | if (k < 0) |
77 | r = k; | |
78 | } | |
79 | ||
80 | return r; | |
d03073dd YW |
81 | } |
82 | ||
c1d3fa29 | 83 | static void dhcp4_check_ready(Link *link) { |
6e537f62 YW |
84 | int r; |
85 | ||
76c5a0f2 YW |
86 | if (link->dhcp4_messages > 0) { |
87 | log_link_debug(link, "%s(): DHCPv4 address and routes are not set.", __func__); | |
153cf041 | 88 | return; |
76c5a0f2 YW |
89 | } |
90 | ||
91 | if (!link->dhcp_address) { | |
92 | log_link_debug(link, "%s(): DHCPv4 address is not set.", __func__); | |
93 | return; | |
94 | } | |
95 | ||
96 | if (!address_is_ready(link->dhcp_address)) { | |
97 | log_link_debug(link, "%s(): DHCPv4 address is not ready.", __func__); | |
98 | return; | |
99 | } | |
153cf041 YW |
100 | |
101 | link->dhcp4_configured = true; | |
6e537f62 | 102 | |
153cf041 | 103 | /* New address and routes are configured now. Let's release old lease. */ |
c45fdad6 | 104 | r = dhcp4_release_old_lease(link); |
6e537f62 YW |
105 | if (r < 0) { |
106 | link_enter_failed(link); | |
107 | return; | |
108 | } | |
109 | ||
8ccae2dd YW |
110 | r = sd_ipv4ll_stop(link->ipv4ll); |
111 | if (r < 0) | |
112 | log_link_warning_errno(link, r, "Failed to drop IPv4 link-local address, ignoring: %m"); | |
113 | ||
153cf041 | 114 | link_check_ready(link); |
c1d3fa29 YW |
115 | } |
116 | ||
76c5a0f2 YW |
117 | static int dhcp4_after_route_configure(Request *req, void *object) { |
118 | Route *route = object; | |
119 | Link *link; | |
120 | int r; | |
121 | ||
122 | assert(req); | |
123 | assert(req->link); | |
124 | assert(req->type == REQUEST_TYPE_ROUTE); | |
125 | assert(route); | |
126 | ||
127 | link = req->link; | |
128 | ||
129 | r = set_ensure_put(&link->dhcp_routes, &route_hash_ops, route); | |
130 | if (r < 0) | |
131 | return log_link_error_errno(link, r, "Failed to store DHCPv4 route: %m"); | |
132 | ||
133 | set_remove(link->dhcp_routes_old, route); | |
134 | ||
135 | return 0; | |
136 | } | |
137 | ||
302a796f | 138 | static int dhcp4_route_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
3c9b8860 TG |
139 | int r; |
140 | ||
141 | assert(link); | |
6cf4a01c | 142 | assert(link->dhcp4_messages > 0); |
3c9b8860 | 143 | |
313cefa1 | 144 | link->dhcp4_messages--; |
3c9b8860 | 145 | |
3ab7ed3f YW |
146 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
147 | return 1; | |
148 | ||
1c4baffc | 149 | r = sd_netlink_message_get_errno(m); |
dd9b10c8 YW |
150 | if (r == -ENETUNREACH && !link->dhcp4_route_retrying) { |
151 | ||
152 | /* It seems kernel does not support that the prefix route cannot be configured with | |
153 | * route table. Let's once drop the config and reconfigure them later. */ | |
154 | ||
81eb5bc5 | 155 | log_link_message_debug_errno(link, m, r, "Could not set DHCPv4 route, retrying later"); |
dd9b10c8 YW |
156 | link->dhcp4_route_failed = true; |
157 | link->manager->dhcp4_prefix_root_cannot_set_table = true; | |
158 | } else if (r < 0 && r != -EEXIST) { | |
81eb5bc5 | 159 | log_link_message_warning_errno(link, m, r, "Could not set DHCPv4 route"); |
3c9b8860 | 160 | link_enter_failed(link); |
3ab7ed3f | 161 | return 1; |
3c9b8860 TG |
162 | } |
163 | ||
153cf041 YW |
164 | if (link->dhcp4_messages == 0 && link->dhcp4_route_failed) { |
165 | link->dhcp4_route_failed = false; | |
166 | link->dhcp4_route_retrying = true; | |
dd9b10c8 | 167 | |
153cf041 YW |
168 | r = dhcp4_remove_all(link); |
169 | if (r < 0) | |
170 | link_enter_failed(link); | |
76c5a0f2 YW |
171 | |
172 | r = link_request_static_nexthops(link, true); | |
173 | if (r < 0) | |
174 | link_enter_failed(link); | |
175 | ||
176 | r = link_request_static_routes(link, true); | |
177 | if (r < 0) | |
178 | link_enter_failed(link); | |
179 | ||
50b74770 YW |
180 | r = dhcp4_request_address_and_routes(link, false); |
181 | if (r < 0) | |
182 | link_enter_failed(link); | |
183 | ||
153cf041 | 184 | return 1; |
3c9b8860 TG |
185 | } |
186 | ||
153cf041 YW |
187 | dhcp4_check_ready(link); |
188 | ||
3c9b8860 TG |
189 | return 1; |
190 | } | |
191 | ||
76c5a0f2 YW |
192 | static int dhcp4_request_route(Route *in, Link *link) { |
193 | _cleanup_(route_freep) Route *route = in; | |
194 | Request *req; | |
d4c52ee5 YW |
195 | int r; |
196 | ||
197 | assert(route); | |
d4c52ee5 YW |
198 | assert(link); |
199 | ||
76c5a0f2 | 200 | r = link_has_route(link, route); |
6e537f62 | 201 | if (r < 0) |
76c5a0f2 YW |
202 | return r; |
203 | if (r == 0) | |
204 | link->dhcp4_configured = false; | |
d4c52ee5 | 205 | |
76c5a0f2 YW |
206 | r = link_request_route(link, TAKE_PTR(route), true, &link->dhcp4_messages, |
207 | dhcp4_route_handler, &req); | |
40b12fa2 | 208 | if (r <= 0) |
76c5a0f2 | 209 | return r; |
6e537f62 | 210 | |
76c5a0f2 | 211 | req->after_configure = dhcp4_after_route_configure; |
d4c52ee5 | 212 | |
d4c52ee5 YW |
213 | return 0; |
214 | } | |
215 | ||
c0fef8f3 YW |
216 | static bool link_prefixroute(Link *link) { |
217 | return !link->network->dhcp_route_table_set || | |
218 | link->network->dhcp_route_table == RT_TABLE_MAIN || | |
219 | link->manager->dhcp4_prefix_root_cannot_set_table; | |
220 | } | |
e723fbd7 | 221 | |
76c5a0f2 | 222 | static int dhcp4_request_prefix_route(Link *link) { |
c0fef8f3 YW |
223 | _cleanup_(route_freep) Route *route = NULL; |
224 | struct in_addr address, netmask; | |
e723fbd7 ZJS |
225 | int r; |
226 | ||
c0fef8f3 YW |
227 | assert(link); |
228 | assert(link->dhcp_lease); | |
229 | ||
230 | if (link_prefixroute(link)) | |
231 | /* When true, the route will be created by kernel. See dhcp4_update_address(). */ | |
232 | return 0; | |
233 | ||
234 | r = sd_dhcp_lease_get_address(link->dhcp_lease, &address); | |
235 | if (r < 0) | |
236 | return r; | |
237 | ||
238 | r = sd_dhcp_lease_get_netmask(link->dhcp_lease, &netmask); | |
e723fbd7 ZJS |
239 | if (r < 0) |
240 | return r; | |
241 | ||
242 | r = route_new(&route); | |
243 | if (r < 0) | |
244 | return r; | |
245 | ||
246 | route->family = AF_INET; | |
c0fef8f3 | 247 | route->dst.in.s_addr = address.s_addr & netmask.s_addr; |
e723fbd7 | 248 | route->dst_prefixlen = in4_addr_netmask_to_prefixlen(&netmask); |
c0fef8f3 | 249 | route->prefsrc.in = address; |
e723fbd7 ZJS |
250 | route->scope = RT_SCOPE_LINK; |
251 | route->protocol = RTPROT_DHCP; | |
c0fef8f3 | 252 | route->table = link_get_dhcp_route_table(link); |
b714d9a6 | 253 | route->mtu = link->network->dhcp_route_mtu; |
c0fef8f3 | 254 | |
76c5a0f2 | 255 | return dhcp4_request_route(TAKE_PTR(route), link); |
e723fbd7 ZJS |
256 | } |
257 | ||
76c5a0f2 | 258 | static int dhcp4_request_route_to_gateway(Link *link, const struct in_addr *gw) { |
ff2cf677 YW |
259 | _cleanup_(route_freep) Route *route = NULL; |
260 | struct in_addr address; | |
261 | int r; | |
262 | ||
263 | assert(link); | |
264 | assert(link->dhcp_lease); | |
265 | assert(gw); | |
266 | ||
267 | r = sd_dhcp_lease_get_address(link->dhcp_lease, &address); | |
268 | if (r < 0) | |
269 | return r; | |
270 | ||
271 | r = route_new(&route); | |
272 | if (r < 0) | |
273 | return r; | |
274 | ||
275 | route->family = AF_INET; | |
276 | route->dst.in = *gw; | |
277 | route->dst_prefixlen = 32; | |
278 | route->prefsrc.in = address; | |
279 | route->scope = RT_SCOPE_LINK; | |
280 | route->protocol = RTPROT_DHCP; | |
281 | route->priority = link->network->dhcp_route_metric; | |
282 | route->table = link_get_dhcp_route_table(link); | |
283 | route->mtu = link->network->dhcp_route_mtu; | |
284 | ||
76c5a0f2 | 285 | return dhcp4_request_route(TAKE_PTR(route), link); |
ff2cf677 YW |
286 | } |
287 | ||
76c5a0f2 YW |
288 | static int dhcp4_request_route_auto( |
289 | Route *in, | |
7f206276 YW |
290 | Link *link, |
291 | const struct in_addr *gw) { | |
292 | ||
76c5a0f2 | 293 | _cleanup_(route_freep) Route *route = in; |
7f206276 YW |
294 | struct in_addr address, netmask, prefix; |
295 | uint8_t prefixlen; | |
296 | int r; | |
297 | ||
298 | assert(route); | |
299 | assert(link); | |
300 | assert(link->dhcp_lease); | |
301 | assert(gw); | |
302 | ||
7f206276 YW |
303 | r = sd_dhcp_lease_get_address(link->dhcp_lease, &address); |
304 | if (r < 0) | |
305 | return r; | |
306 | ||
307 | r = sd_dhcp_lease_get_netmask(link->dhcp_lease, &netmask); | |
308 | if (r < 0) | |
309 | return r; | |
310 | ||
311 | prefix.s_addr = address.s_addr & netmask.s_addr; | |
312 | prefixlen = in4_addr_netmask_to_prefixlen(&netmask); | |
313 | ||
314 | if (in4_addr_is_localhost(&route->dst.in)) { | |
315 | if (in4_addr_is_set(gw)) | |
316 | log_link_debug(link, "DHCP: requested route destination "IPV4_ADDRESS_FMT_STR"/%u is localhost, " | |
317 | "ignoring gateway address "IPV4_ADDRESS_FMT_STR, | |
318 | IPV4_ADDRESS_FMT_VAL(route->dst.in), route->dst_prefixlen, IPV4_ADDRESS_FMT_VAL(*gw)); | |
319 | ||
320 | route->scope = RT_SCOPE_HOST; | |
321 | route->gw_family = AF_UNSPEC; | |
322 | route->gw = IN_ADDR_NULL; | |
323 | route->prefsrc = IN_ADDR_NULL; | |
324 | ||
325 | } else if (in4_addr_equal(&route->dst.in, &address)) { | |
326 | if (in4_addr_is_set(gw)) | |
327 | log_link_debug(link, "DHCP: requested route destination "IPV4_ADDRESS_FMT_STR"/%u is equivalent to the acquired address, " | |
328 | "ignoring gateway address "IPV4_ADDRESS_FMT_STR, | |
329 | IPV4_ADDRESS_FMT_VAL(route->dst.in), route->dst_prefixlen, IPV4_ADDRESS_FMT_VAL(*gw)); | |
330 | ||
331 | route->scope = RT_SCOPE_HOST; | |
332 | route->gw_family = AF_UNSPEC; | |
333 | route->gw = IN_ADDR_NULL; | |
334 | route->prefsrc.in = address; | |
335 | ||
336 | } else if (route->dst_prefixlen >= prefixlen && | |
337 | (route->dst.in.s_addr & netmask.s_addr) == prefix.s_addr) { | |
338 | if (in4_addr_is_set(gw)) | |
339 | log_link_debug(link, "DHCP: requested route destination "IPV4_ADDRESS_FMT_STR"/%u is in the assigned network " | |
340 | IPV4_ADDRESS_FMT_STR"/%u, ignoring gateway address "IPV4_ADDRESS_FMT_STR, | |
341 | IPV4_ADDRESS_FMT_VAL(route->dst.in), route->dst_prefixlen, | |
342 | IPV4_ADDRESS_FMT_VAL(prefix), prefixlen, | |
343 | IPV4_ADDRESS_FMT_VAL(*gw)); | |
344 | ||
345 | route->scope = RT_SCOPE_LINK; | |
346 | route->gw_family = AF_UNSPEC; | |
347 | route->gw = IN_ADDR_NULL; | |
348 | route->prefsrc.in = address; | |
349 | ||
350 | } else { | |
351 | if (in4_addr_is_null(gw)) { | |
352 | log_link_debug(link, "DHCP: requested route destination "IPV4_ADDRESS_FMT_STR"/%u is not in the assigned network " | |
353 | IPV4_ADDRESS_FMT_STR"/%u, but no gateway is specified, ignoring.", | |
354 | IPV4_ADDRESS_FMT_VAL(route->dst.in), route->dst_prefixlen, | |
355 | IPV4_ADDRESS_FMT_VAL(prefix), prefixlen); | |
356 | return 0; | |
357 | } | |
358 | ||
76c5a0f2 | 359 | r = dhcp4_request_route_to_gateway(link, gw); |
7f206276 YW |
360 | if (r < 0) |
361 | return r; | |
362 | ||
363 | route->scope = RT_SCOPE_UNIVERSE; | |
364 | route->gw_family = AF_INET; | |
365 | route->gw.in = *gw; | |
366 | route->prefsrc.in = address; | |
367 | } | |
368 | ||
76c5a0f2 | 369 | return dhcp4_request_route(TAKE_PTR(route), link); |
7f206276 YW |
370 | } |
371 | ||
76c5a0f2 | 372 | static int dhcp4_request_static_routes(Link *link, struct in_addr *ret_default_gw) { |
f8693fc7 | 373 | _cleanup_free_ sd_dhcp_route **static_routes = NULL; |
8cdc46e7 | 374 | bool classless_route = false, static_route = false; |
afe23f87 | 375 | struct in_addr default_gw = {}; |
cda7fc8d | 376 | int n, r; |
854a1ccf YW |
377 | |
378 | assert(link); | |
379 | assert(link->dhcp_lease); | |
afe23f87 | 380 | assert(ret_default_gw); |
854a1ccf | 381 | |
7872d0f7 YW |
382 | n = sd_dhcp_lease_get_routes(link->dhcp_lease, &static_routes); |
383 | if (IN_SET(n, 0, -ENODATA)) { | |
384 | log_link_debug(link, "DHCP: No static routes received from DHCP server."); | |
854a1ccf | 385 | return 0; |
7872d0f7 | 386 | } |
854a1ccf | 387 | if (n < 0) |
7872d0f7 | 388 | return n; |
854a1ccf | 389 | |
7872d0f7 YW |
390 | for (int i = 0; i < n; i++) { |
391 | switch (sd_dhcp_route_get_option(static_routes[i])) { | |
392 | case SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE: | |
393 | classless_route = true; | |
394 | break; | |
395 | case SD_DHCP_OPTION_STATIC_ROUTE: | |
396 | static_route = true; | |
397 | break; | |
398 | } | |
399 | } | |
854a1ccf | 400 | |
7872d0f7 YW |
401 | /* if the DHCP server returns both a Classless Static Routes option and a Static Routes option, |
402 | * the DHCP client MUST ignore the Static Routes option. */ | |
403 | if (classless_route && static_route) | |
dec2ed3e | 404 | log_link_debug(link, "Classless static routes received from DHCP server: ignoring static-route option"); |
7872d0f7 | 405 | |
0ebab55f YW |
406 | if (!link->network->dhcp_use_routes) { |
407 | if (!classless_route) | |
408 | return 0; | |
409 | ||
410 | /* Even if UseRoutes=no, try to find default gateway to make semi-static routes and | |
411 | * routes to DNS or NTP servers can be configured in later steps. */ | |
412 | for (int i = 0; i < n; i++) { | |
413 | struct in_addr dst; | |
414 | uint8_t prefixlen; | |
415 | ||
416 | if (sd_dhcp_route_get_option(static_routes[i]) != SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE) | |
417 | continue; | |
418 | ||
419 | r = sd_dhcp_route_get_destination(static_routes[i], &dst); | |
420 | if (r < 0) | |
421 | return r; | |
422 | ||
423 | if (in4_addr_is_set(&dst)) | |
424 | continue; | |
425 | ||
426 | r = sd_dhcp_route_get_destination_prefix_length(static_routes[i], &prefixlen); | |
427 | if (r < 0) | |
428 | return r; | |
429 | ||
430 | if (prefixlen != 0) | |
431 | continue; | |
432 | ||
433 | r = sd_dhcp_route_get_gateway(static_routes[i], ret_default_gw); | |
434 | if (r < 0) | |
435 | return r; | |
436 | ||
437 | break; | |
438 | } | |
439 | ||
440 | /* Do not return 1 here, to ensure the router option can override the default gateway | |
441 | * that was found. */ | |
442 | return 0; | |
443 | } | |
444 | ||
7872d0f7 | 445 | for (int i = 0; i < n; i++) { |
76c5a0f2 | 446 | _cleanup_(route_freep) Route *route = NULL; |
7f206276 YW |
447 | struct in_addr gw; |
448 | ||
7872d0f7 YW |
449 | if (sd_dhcp_route_get_option(static_routes[i]) != |
450 | (classless_route ? SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE : SD_DHCP_OPTION_STATIC_ROUTE)) | |
451 | continue; | |
854a1ccf | 452 | |
76c5a0f2 YW |
453 | r = route_new(&route); |
454 | if (r < 0) | |
455 | return r; | |
456 | ||
457 | route->family = AF_INET; | |
458 | route->gw_family = AF_INET; | |
459 | route->protocol = RTPROT_DHCP; | |
460 | route->priority = link->network->dhcp_route_metric; | |
461 | route->table = link_get_dhcp_route_table(link); | |
462 | route->mtu = link->network->dhcp_route_mtu; | |
463 | ||
7f206276 | 464 | r = sd_dhcp_route_get_gateway(static_routes[i], &gw); |
854a1ccf | 465 | if (r < 0) |
7872d0f7 | 466 | return r; |
854a1ccf | 467 | |
7872d0f7 YW |
468 | r = sd_dhcp_route_get_destination(static_routes[i], &route->dst.in); |
469 | if (r < 0) | |
470 | return r; | |
854a1ccf | 471 | |
7872d0f7 YW |
472 | r = sd_dhcp_route_get_destination_prefix_length(static_routes[i], &route->dst_prefixlen); |
473 | if (r < 0) | |
474 | return r; | |
475 | ||
afe23f87 YW |
476 | /* When classless static routes are provided, then router option will be ignored. To |
477 | * use the default gateway later in other routes, e.g., routes to dns servers, here we | |
478 | * need to find the default gateway in the classless static routes. */ | |
479 | if (classless_route && | |
480 | in4_addr_is_null(&route->dst.in) && route->dst_prefixlen == 0 && | |
481 | in4_addr_is_null(&default_gw)) | |
482 | default_gw = gw; | |
483 | ||
76c5a0f2 | 484 | r = dhcp4_request_route_auto(TAKE_PTR(route), link, &gw); |
854a1ccf | 485 | if (r < 0) |
7872d0f7 YW |
486 | return r; |
487 | } | |
488 | ||
afe23f87 | 489 | *ret_default_gw = default_gw; |
7872d0f7 YW |
490 | return classless_route; |
491 | } | |
492 | ||
0ebab55f | 493 | static int dhcp4_request_gateway(Link *link, struct in_addr *gw) { |
ff2cf677 YW |
494 | _cleanup_(route_freep) Route *route = NULL; |
495 | const struct in_addr *router; | |
496 | struct in_addr address; | |
ff2cf677 YW |
497 | int r; |
498 | ||
499 | assert(link); | |
500 | assert(link->dhcp_lease); | |
0ebab55f | 501 | assert(gw); |
ff2cf677 YW |
502 | |
503 | r = sd_dhcp_lease_get_address(link->dhcp_lease, &address); | |
504 | if (r < 0) | |
505 | return r; | |
506 | ||
507 | r = sd_dhcp_lease_get_router(link->dhcp_lease, &router); | |
508 | if (IN_SET(r, 0, -ENODATA)) { | |
509 | log_link_debug(link, "DHCP: No gateway received from DHCP server."); | |
510 | return 0; | |
511 | } | |
512 | if (r < 0) | |
513 | return r; | |
514 | if (in4_addr_is_null(&router[0])) { | |
515 | log_link_debug(link, "DHCP: Received gateway address is null."); | |
516 | return 0; | |
517 | } | |
518 | ||
0ebab55f YW |
519 | if (!link->network->dhcp_use_gateway) { |
520 | /* When no classless static route is provided, even if UseGateway=no, use the gateway | |
521 | * address to configure semi-static routes or routes to DNS or NTP servers. Note, if | |
522 | * neither UseRoutes= nor UseGateway= is disabled, use the default gateway in classless | |
523 | * static routes if provided (in that case, in4_addr_is_null(gw) below is true). */ | |
524 | if (in4_addr_is_null(gw)) | |
525 | *gw = router[0]; | |
526 | return 0; | |
527 | } | |
528 | ||
ff2cf677 YW |
529 | /* The dhcp netmask may mask out the gateway. First, add an explicit route for the gateway host |
530 | * so that we can route no matter the netmask or existing kernel route tables. */ | |
76c5a0f2 | 531 | r = dhcp4_request_route_to_gateway(link, &router[0]); |
ff2cf677 YW |
532 | if (r < 0) |
533 | return r; | |
534 | ||
535 | r = route_new(&route); | |
536 | if (r < 0) | |
537 | return r; | |
538 | ||
539 | /* Next, add a default gateway. */ | |
540 | route->family = AF_INET; | |
541 | route->gw_family = AF_INET; | |
542 | route->gw.in = router[0]; | |
543 | route->prefsrc.in = address; | |
544 | route->protocol = RTPROT_DHCP; | |
545 | route->priority = link->network->dhcp_route_metric; | |
546 | route->table = link_get_dhcp_route_table(link); | |
547 | route->mtu = link->network->dhcp_route_mtu; | |
548 | ||
76c5a0f2 | 549 | r = dhcp4_request_route(TAKE_PTR(route), link); |
ff2cf677 YW |
550 | if (r < 0) |
551 | return r; | |
552 | ||
0ebab55f YW |
553 | /* When no classless static route is provided, or UseRoutes=no, then use the router address to |
554 | * configure semi-static routes and routes to DNS or NTP servers in later steps. */ | |
555 | *gw = router[0]; | |
556 | return 0; | |
557 | } | |
558 | ||
559 | static int dhcp4_request_semi_static_routes(Link *link, const struct in_addr *gw) { | |
560 | Route *rt; | |
561 | int r; | |
562 | ||
563 | assert(link); | |
564 | assert(link->dhcp_lease); | |
565 | assert(link->network); | |
566 | assert(gw); | |
567 | ||
568 | if (in4_addr_is_null(gw)) | |
569 | return 0; | |
570 | ||
ff2cf677 | 571 | HASHMAP_FOREACH(rt, link->network->routes_by_section) { |
0ebab55f YW |
572 | _cleanup_(route_freep) Route *route = NULL; |
573 | ||
ff2cf677 YW |
574 | if (!rt->gateway_from_dhcp_or_ra) |
575 | continue; | |
576 | ||
577 | if (rt->gw_family != AF_INET) | |
578 | continue; | |
579 | ||
0ebab55f YW |
580 | r = dhcp4_request_route_to_gateway(link, gw); |
581 | if (r < 0) | |
582 | return r; | |
583 | ||
76c5a0f2 YW |
584 | r = route_dup(rt, &route); |
585 | if (r < 0) | |
586 | return r; | |
587 | ||
0ebab55f | 588 | route->gw.in = *gw; |
76c5a0f2 YW |
589 | if (!route->protocol_set) |
590 | route->protocol = RTPROT_DHCP; | |
591 | if (!route->priority_set) | |
592 | route->priority = link->network->dhcp_route_metric; | |
593 | if (!route->table_set) | |
594 | route->table = link_get_dhcp_route_table(link); | |
595 | if (route->mtu == 0) | |
596 | route->mtu = link->network->dhcp_route_mtu; | |
597 | ||
598 | r = dhcp4_request_route(TAKE_PTR(route), link); | |
ff2cf677 YW |
599 | if (r < 0) |
600 | return r; | |
854a1ccf YW |
601 | } |
602 | ||
603 | return 0; | |
604 | } | |
605 | ||
76c5a0f2 | 606 | static int dhcp4_request_routes_to_servers( |
949b2c95 YW |
607 | Link *link, |
608 | const struct in_addr *servers, | |
609 | size_t n_servers, | |
610 | const struct in_addr *gw) { | |
611 | ||
949b2c95 | 612 | int r; |
e1c08a3d YW |
613 | |
614 | assert(link); | |
615 | assert(link->dhcp_lease); | |
616 | assert(link->network); | |
949b2c95 | 617 | assert(servers || n_servers == 0); |
afe23f87 | 618 | assert(gw); |
e723fbd7 | 619 | |
949b2c95 | 620 | for (size_t i = 0; i < n_servers; i++) { |
76c5a0f2 YW |
621 | _cleanup_(route_freep) Route *route = NULL; |
622 | ||
949b2c95 | 623 | if (in4_addr_is_null(&servers[i])) |
3e429683 YW |
624 | continue; |
625 | ||
76c5a0f2 YW |
626 | r = route_new(&route); |
627 | if (r < 0) | |
628 | return r; | |
629 | ||
630 | route->family = AF_INET; | |
949b2c95 | 631 | route->dst.in = servers[i]; |
76c5a0f2 YW |
632 | route->dst_prefixlen = 32; |
633 | route->protocol = RTPROT_DHCP; | |
634 | route->priority = link->network->dhcp_route_metric; | |
635 | route->table = link_get_dhcp_route_table(link); | |
636 | route->mtu = link->network->dhcp_route_mtu; | |
e1c08a3d | 637 | |
76c5a0f2 | 638 | r = dhcp4_request_route_auto(TAKE_PTR(route), link, gw); |
e1c08a3d YW |
639 | if (r < 0) |
640 | return r; | |
641 | } | |
642 | ||
e723fbd7 ZJS |
643 | return 0; |
644 | } | |
645 | ||
76c5a0f2 | 646 | static int dhcp4_request_routes_to_dns(Link *link, const struct in_addr *gw) { |
949b2c95 YW |
647 | const struct in_addr *dns; |
648 | int r; | |
649 | ||
650 | assert(link); | |
651 | assert(link->dhcp_lease); | |
652 | assert(link->network); | |
653 | assert(gw); | |
654 | ||
655 | if (!link->network->dhcp_use_dns || | |
656 | !link->network->dhcp_routes_to_dns) | |
657 | return 0; | |
658 | ||
659 | r = sd_dhcp_lease_get_dns(link->dhcp_lease, &dns); | |
660 | if (IN_SET(r, 0, -ENODATA)) | |
661 | return 0; | |
662 | if (r < 0) | |
663 | return r; | |
664 | ||
76c5a0f2 | 665 | return dhcp4_request_routes_to_servers(link, dns, r, gw); |
949b2c95 YW |
666 | } |
667 | ||
76c5a0f2 | 668 | static int dhcp4_request_routes_to_ntp(Link *link, const struct in_addr *gw) { |
d7b04506 YW |
669 | const struct in_addr *ntp; |
670 | int r; | |
671 | ||
672 | assert(link); | |
673 | assert(link->dhcp_lease); | |
674 | assert(link->network); | |
675 | assert(gw); | |
676 | ||
677 | if (!link->network->dhcp_use_ntp || | |
678 | !link->network->dhcp_routes_to_ntp) | |
679 | return 0; | |
680 | ||
681 | r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &ntp); | |
682 | if (IN_SET(r, 0, -ENODATA)) | |
683 | return 0; | |
684 | if (r < 0) | |
685 | return r; | |
686 | ||
76c5a0f2 | 687 | return dhcp4_request_routes_to_servers(link, ntp, r, gw); |
d7b04506 YW |
688 | } |
689 | ||
76c5a0f2 | 690 | static int dhcp4_request_routes(Link *link) { |
afe23f87 | 691 | struct in_addr gw = {}; |
6e537f62 | 692 | Route *rt; |
7872d0f7 | 693 | int r; |
3c9b8860 TG |
694 | |
695 | assert(link); | |
0c9b15a3 | 696 | |
76c5a0f2 | 697 | if (!link->dhcp_lease) |
4e2ef9d9 YW |
698 | return 0; |
699 | ||
6e537f62 YW |
700 | while ((rt = set_steal_first(link->dhcp_routes))) { |
701 | r = set_ensure_put(&link->dhcp_routes_old, &route_hash_ops, rt); | |
702 | if (r < 0) | |
703 | return log_link_error_errno(link, r, "Failed to store old DHCPv4 route: %m"); | |
704 | } | |
d4c52ee5 | 705 | |
76c5a0f2 | 706 | r = dhcp4_request_prefix_route(link); |
b23aec0d | 707 | if (r < 0) |
76c5a0f2 | 708 | return log_link_error_errno(link, r, "DHCP error: Could not request prefix route: %m"); |
156ddf8d | 709 | |
76c5a0f2 | 710 | r = dhcp4_request_static_routes(link, &gw); |
7872d0f7 | 711 | if (r < 0) |
76c5a0f2 | 712 | return log_link_error_errno(link, r, "DHCP error: Could not request static routes: %m"); |
ff2cf677 | 713 | if (r == 0) { |
7872d0f7 YW |
714 | /* According to RFC 3442: If the DHCP server returns both a Classless Static Routes option and |
715 | * a Router option, the DHCP client MUST ignore the Router option. */ | |
76c5a0f2 | 716 | r = dhcp4_request_gateway(link, &gw); |
156ddf8d | 717 | if (r < 0) |
76c5a0f2 | 718 | return log_link_error_errno(link, r, "DHCP error: Could not request gateway: %m"); |
5f04a209 SS |
719 | } |
720 | ||
0ebab55f YW |
721 | r = dhcp4_request_semi_static_routes(link, &gw); |
722 | if (r < 0) | |
723 | return log_link_error_errno(link, r, "DHCP error: Could not request routes with Gateway=_dhcp4 setting: %m"); | |
724 | ||
76c5a0f2 | 725 | r = dhcp4_request_routes_to_dns(link, &gw); |
e1c08a3d | 726 | if (r < 0) |
76c5a0f2 | 727 | return log_link_error_errno(link, r, "DHCP error: Could not request routes to DNS servers: %m"); |
1985c54f | 728 | |
76c5a0f2 | 729 | r = dhcp4_request_routes_to_ntp(link, &gw); |
d7b04506 | 730 | if (r < 0) |
76c5a0f2 | 731 | return log_link_error_errno(link, r, "DHCP error: Could not request routes to NTP servers: %m"); |
d7b04506 | 732 | |
e1c08a3d | 733 | return 0; |
3c9b8860 TG |
734 | } |
735 | ||
7fa472f9 YW |
736 | static int dhcp_reset_mtu(Link *link) { |
737 | uint16_t mtu; | |
738 | int r; | |
739 | ||
740 | assert(link); | |
741 | ||
742 | if (!link->network->dhcp_use_mtu) | |
743 | return 0; | |
744 | ||
745 | r = sd_dhcp_lease_get_mtu(link->dhcp_lease, &mtu); | |
46b875fb YW |
746 | if (r == -ENODATA) |
747 | return 0; | |
7fa472f9 | 748 | if (r < 0) |
46b875fb | 749 | return log_link_error_errno(link, r, "DHCP error: failed to get MTU from lease: %m"); |
7fa472f9 YW |
750 | |
751 | if (link->original_mtu == mtu) | |
752 | return 0; | |
753 | ||
0fa8ee6c | 754 | r = link_request_to_set_mtu(link, link->original_mtu); |
46b875fb YW |
755 | if (r < 0) |
756 | return log_link_error_errno(link, r, "DHCP error: could not reset MTU: %m"); | |
3c9b8860 | 757 | |
7fa472f9 YW |
758 | return 0; |
759 | } | |
760 | ||
761 | static int dhcp_reset_hostname(Link *link) { | |
762 | const char *hostname; | |
763 | int r; | |
764 | ||
765 | assert(link); | |
766 | ||
767 | if (!link->network->dhcp_use_hostname) | |
768 | return 0; | |
769 | ||
770 | hostname = link->network->dhcp_hostname; | |
771 | if (!hostname) | |
772 | (void) sd_dhcp_lease_get_hostname(link->dhcp_lease, &hostname); | |
773 | ||
774 | if (!hostname) | |
775 | return 0; | |
776 | ||
777 | /* If a hostname was set due to the lease, then unset it now. */ | |
778 | r = manager_set_hostname(link->manager, NULL); | |
779 | if (r < 0) | |
780 | return log_link_error_errno(link, r, "DHCP error: Failed to reset transient hostname: %m"); | |
781 | ||
782 | return 0; | |
783 | } | |
784 | ||
6906794d | 785 | static int dhcp4_remove_all(Link *link) { |
6e537f62 | 786 | Route *route; |
6e537f62 | 787 | int k, r = 0; |
6906794d YW |
788 | |
789 | assert(link); | |
6906794d | 790 | |
90e74a66 | 791 | SET_FOREACH(route, link->dhcp_routes) { |
f4cc1364 | 792 | k = route_remove(route, NULL, link); |
6e537f62 YW |
793 | if (k < 0) |
794 | r = k; | |
6e537f62 | 795 | } |
6906794d | 796 | |
6e537f62 | 797 | if (link->dhcp_address) { |
f4cc1364 | 798 | k = address_remove(link->dhcp_address, link); |
6e537f62 YW |
799 | if (k < 0) |
800 | r = k; | |
6e537f62 | 801 | } |
6906794d | 802 | |
6e537f62 | 803 | return r; |
6906794d YW |
804 | } |
805 | ||
9155da08 | 806 | int dhcp4_lease_lost(Link *link) { |
2ac7eec3 | 807 | int k, r = 0; |
7fa472f9 YW |
808 | |
809 | assert(link); | |
810 | assert(link->dhcp_lease); | |
811 | ||
f8c2e4b9 | 812 | log_link_info(link, "DHCP lease lost"); |
7fa472f9 YW |
813 | |
814 | link->dhcp4_configured = false; | |
815 | ||
9155da08 | 816 | /* dhcp4_lease_lost() may be called during renewing IP address. */ |
c45fdad6 | 817 | k = dhcp4_release_old_lease(link); |
6e537f62 YW |
818 | if (k < 0) |
819 | r = k; | |
75be72d1 | 820 | |
6e537f62 YW |
821 | k = dhcp4_remove_all(link); |
822 | if (k < 0) | |
823 | r = k; | |
6906794d | 824 | |
6e537f62 YW |
825 | k = dhcp_reset_mtu(link); |
826 | if (k < 0) | |
827 | r = k; | |
6906794d | 828 | |
6e537f62 YW |
829 | k = dhcp_reset_hostname(link); |
830 | if (k < 0) | |
831 | r = k; | |
7fa472f9 | 832 | |
3c9b8860 | 833 | link->dhcp_lease = sd_dhcp_lease_unref(link->dhcp_lease); |
6a3e5f6a | 834 | link_dirty(link); |
3c9b8860 | 835 | |
76a86ffd YW |
836 | if (link->network->dhcp_send_decline) { |
837 | Address *a; | |
54312274 | 838 | |
76a86ffd | 839 | /* The acquired address may be still ARP probing and not configured. */ |
0f3ff4ea | 840 | |
76a86ffd YW |
841 | SET_FOREACH(a, link->addresses_ipv4acd) |
842 | if (!a->is_static && address_get(link, a, NULL) < 0) { | |
843 | Request req = { | |
844 | .link = link, | |
845 | .address = a, | |
846 | }; | |
0f3ff4ea | 847 | |
76a86ffd YW |
848 | log_link_debug(link, "Canceling the request to configure DHCPv4 address "IPV4_ADDRESS_FMT_STR, |
849 | IPV4_ADDRESS_FMT_VAL(a->in_addr.in)); | |
850 | request_drop(ordered_set_get(link->manager->request_queue, &req)); | |
851 | } | |
54312274 YW |
852 | } |
853 | ||
153cf041 YW |
854 | if (r < 0) |
855 | return r; | |
856 | ||
76a86ffd | 857 | r = link_request_static_nexthops(link, true); |
153cf041 YW |
858 | if (r < 0) |
859 | return r; | |
860 | ||
76a86ffd | 861 | return link_request_static_routes(link, true); |
153cf041 YW |
862 | } |
863 | ||
c45fdad6 | 864 | static int dhcp4_address_ready_callback(Address *address) { |
76c5a0f2 YW |
865 | assert(address); |
866 | ||
867 | /* Do not call this again. */ | |
868 | address->callback = NULL; | |
869 | ||
870 | dhcp4_check_ready(address->link); | |
871 | return 0; | |
872 | } | |
873 | ||
874 | static int dhcp4_after_address_configure(Request *req, void *object) { | |
875 | Address *address = object; | |
c45fdad6 YW |
876 | Link *link; |
877 | int r; | |
878 | ||
76c5a0f2 YW |
879 | assert(req); |
880 | assert(req->link); | |
881 | assert(req->type == REQUEST_TYPE_ADDRESS); | |
c45fdad6 YW |
882 | assert(address); |
883 | ||
76c5a0f2 | 884 | link = req->link; |
c45fdad6 | 885 | |
76c5a0f2 YW |
886 | if (!address_equal(link->dhcp_address, address)) { |
887 | if (link->dhcp_address_old && | |
888 | !address_equal(link->dhcp_address_old, link->dhcp_address)) { | |
889 | /* Still too old address exists? Let's remove it immediately. */ | |
f4cc1364 | 890 | r = address_remove(link->dhcp_address_old, link); |
76c5a0f2 YW |
891 | if (r < 0) |
892 | return r; | |
893 | } | |
894 | link->dhcp_address_old = link->dhcp_address; | |
895 | } | |
c45fdad6 | 896 | |
76c5a0f2 | 897 | link->dhcp_address = address; |
c45fdad6 YW |
898 | return 0; |
899 | } | |
900 | ||
302a796f | 901 | static int dhcp4_address_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
3c9b8860 TG |
902 | int r; |
903 | ||
904 | assert(link); | |
76c5a0f2 YW |
905 | assert(link->dhcp4_messages > 0); |
906 | ||
907 | link->dhcp4_messages--; | |
3c9b8860 | 908 | |
5a07fa9d YW |
909 | r = address_configure_handler_internal(rtnl, m, link, "Could not set DHCPv4 address"); |
910 | if (r <= 0) | |
911 | return r; | |
3c9b8860 | 912 | |
c45fdad6 YW |
913 | if (address_is_ready(link->dhcp_address)) { |
914 | r = dhcp4_address_ready_callback(link->dhcp_address); | |
5a07fa9d | 915 | if (r < 0) |
c45fdad6 | 916 | link_enter_failed(link); |
c45fdad6 YW |
917 | } else |
918 | link->dhcp_address->callback = dhcp4_address_ready_callback; | |
0f3ff4ea | 919 | |
3c9b8860 TG |
920 | return 1; |
921 | } | |
922 | ||
76c5a0f2 | 923 | static int dhcp4_request_address(Link *link, bool announce) { |
8e766630 | 924 | _cleanup_(address_freep) Address *addr = NULL; |
6906794d YW |
925 | uint32_t lifetime = CACHE_INFO_INFINITY_LIFE_TIME; |
926 | struct in_addr address, netmask; | |
3c9b8860 | 927 | unsigned prefixlen; |
76c5a0f2 | 928 | Request *req; |
3c9b8860 TG |
929 | int r; |
930 | ||
6906794d YW |
931 | assert(link); |
932 | assert(link->network); | |
933 | ||
934 | if (!link->dhcp_lease) | |
935 | return 0; | |
936 | ||
6906794d YW |
937 | r = sd_dhcp_lease_get_address(link->dhcp_lease, &address); |
938 | if (r < 0) | |
939 | return log_link_warning_errno(link, r, "DHCP error: no address: %m"); | |
940 | ||
941 | r = sd_dhcp_lease_get_netmask(link->dhcp_lease, &netmask); | |
942 | if (r < 0) | |
943 | return log_link_warning_errno(link, r, "DHCP error: no netmask: %m"); | |
944 | ||
945 | if (!FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) { | |
946 | r = sd_dhcp_lease_get_lifetime(link->dhcp_lease, &lifetime); | |
947 | if (r < 0) | |
948 | return log_link_warning_errno(link, r, "DHCP error: no lifetime: %m"); | |
949 | } | |
950 | ||
951 | prefixlen = in4_addr_netmask_to_prefixlen(&netmask); | |
952 | ||
953 | if (announce) { | |
954 | const struct in_addr *router; | |
955 | ||
956 | r = sd_dhcp_lease_get_router(link->dhcp_lease, &router); | |
957 | if (r < 0 && r != -ENODATA) | |
958 | return log_link_error_errno(link, r, "DHCP error: Could not get gateway: %m"); | |
959 | ||
94876904 | 960 | if (r > 0 && in4_addr_is_set(&router[0])) |
6906794d YW |
961 | log_struct(LOG_INFO, |
962 | LOG_LINK_INTERFACE(link), | |
ceea6c1a YW |
963 | LOG_LINK_MESSAGE(link, "DHCPv4 address "IPV4_ADDRESS_FMT_STR"/%u via "IPV4_ADDRESS_FMT_STR, |
964 | IPV4_ADDRESS_FMT_VAL(address), | |
6906794d | 965 | prefixlen, |
ceea6c1a YW |
966 | IPV4_ADDRESS_FMT_VAL(router[0])), |
967 | "ADDRESS="IPV4_ADDRESS_FMT_STR, IPV4_ADDRESS_FMT_VAL(address), | |
6906794d | 968 | "PREFIXLEN=%u", prefixlen, |
ceea6c1a | 969 | "GATEWAY="IPV4_ADDRESS_FMT_STR, IPV4_ADDRESS_FMT_VAL(router[0])); |
6906794d YW |
970 | else |
971 | log_struct(LOG_INFO, | |
972 | LOG_LINK_INTERFACE(link), | |
ceea6c1a YW |
973 | LOG_LINK_MESSAGE(link, "DHCPv4 address "IPV4_ADDRESS_FMT_STR"/%u", |
974 | IPV4_ADDRESS_FMT_VAL(address), | |
6906794d | 975 | prefixlen), |
ceea6c1a | 976 | "ADDRESS="IPV4_ADDRESS_FMT_STR, IPV4_ADDRESS_FMT_VAL(address), |
6906794d YW |
977 | "PREFIXLEN=%u", prefixlen); |
978 | } | |
3c9b8860 | 979 | |
f0213e37 | 980 | r = address_new(&addr); |
3c9b8860 | 981 | if (r < 0) |
6906794d | 982 | return log_oom(); |
3c9b8860 TG |
983 | |
984 | addr->family = AF_INET; | |
6906794d | 985 | addr->in_addr.in.s_addr = address.s_addr; |
3c9b8860 TG |
986 | addr->cinfo.ifa_prefered = lifetime; |
987 | addr->cinfo.ifa_valid = lifetime; | |
988 | addr->prefixlen = prefixlen; | |
df8aa086 YW |
989 | if (prefixlen <= 30) |
990 | addr->broadcast.s_addr = address.s_addr | ~netmask.s_addr; | |
eaff204f | 991 | SET_FLAG(addr->flags, IFA_F_NOPREFIXROUTE, !link_prefixroute(link)); |
415deef9 | 992 | addr->route_metric = link->network->dhcp_route_metric; |
76a86ffd | 993 | addr->duplicate_address_detection = link->network->dhcp_send_decline ? ADDRESS_FAMILY_IPV4 : ADDRESS_FAMILY_NO; |
3c9b8860 | 994 | |
d419ef02 MR |
995 | if (link->network->dhcp_label) { |
996 | addr->label = strdup(link->network->dhcp_label); | |
997 | if (!addr->label) | |
998 | return log_oom(); | |
999 | } | |
1000 | ||
76c5a0f2 YW |
1001 | if (address_get(link, addr, NULL) < 0) |
1002 | link->dhcp4_configured = false; | |
1003 | ||
1004 | r = link_request_address(link, TAKE_PTR(addr), true, &link->dhcp4_messages, | |
1005 | dhcp4_address_handler, &req); | |
3c9b8860 | 1006 | if (r < 0) |
76c5a0f2 | 1007 | return log_link_error_errno(link, r, "Failed to request DHCPv4 address: %m"); |
40b12fa2 YW |
1008 | if (r == 0) |
1009 | return 0; | |
6e537f62 | 1010 | |
76c5a0f2 YW |
1011 | req->after_configure = dhcp4_after_address_configure; |
1012 | ||
1013 | return 0; | |
1014 | } | |
1015 | ||
1016 | static int dhcp4_request_address_and_routes(Link *link, bool announce) { | |
1017 | int r; | |
1018 | ||
1019 | assert(link); | |
1020 | ||
1021 | r = dhcp4_request_address(link, announce); | |
1022 | if (r < 0) | |
1023 | return r; | |
1024 | ||
1025 | r = dhcp4_request_routes(link); | |
1026 | if (r < 0) | |
1027 | return r; | |
1028 | ||
1029 | link_set_state(link, LINK_STATE_CONFIGURING); | |
1030 | link_check_ready(link); | |
3c9b8860 TG |
1031 | |
1032 | return 0; | |
1033 | } | |
1034 | ||
1035 | static int dhcp_lease_renew(sd_dhcp_client *client, Link *link) { | |
1036 | sd_dhcp_lease *lease; | |
3c9b8860 TG |
1037 | int r; |
1038 | ||
1039 | assert(link); | |
1040 | assert(client); | |
3c9b8860 TG |
1041 | |
1042 | r = sd_dhcp_client_get_lease(client, &lease); | |
f6b8196f LP |
1043 | if (r < 0) |
1044 | return log_link_warning_errno(link, r, "DHCP error: no lease: %m"); | |
3c9b8860 TG |
1045 | |
1046 | sd_dhcp_lease_unref(link->dhcp_lease); | |
e6b18ffa | 1047 | link->dhcp_lease = sd_dhcp_lease_ref(lease); |
6a3e5f6a | 1048 | link_dirty(link); |
3c9b8860 | 1049 | |
76c5a0f2 | 1050 | return dhcp4_request_address_and_routes(link, false); |
3c9b8860 TG |
1051 | } |
1052 | ||
1053 | static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) { | |
1054 | sd_dhcp_lease *lease; | |
3c9b8860 TG |
1055 | int r; |
1056 | ||
1057 | assert(client); | |
1058 | assert(link); | |
1059 | ||
1060 | r = sd_dhcp_client_get_lease(client, &lease); | |
f2341e0a | 1061 | if (r < 0) |
f6b8196f | 1062 | return log_link_error_errno(link, r, "DHCP error: No lease: %m"); |
3c9b8860 | 1063 | |
6906794d | 1064 | sd_dhcp_lease_unref(link->dhcp_lease); |
e6b18ffa | 1065 | link->dhcp_lease = sd_dhcp_lease_ref(lease); |
6a3e5f6a | 1066 | link_dirty(link); |
3c9b8860 | 1067 | |
27cb34f5 | 1068 | if (link->network->dhcp_use_mtu) { |
3c9b8860 TG |
1069 | uint16_t mtu; |
1070 | ||
1071 | r = sd_dhcp_lease_get_mtu(lease, &mtu); | |
1072 | if (r >= 0) { | |
0fa8ee6c | 1073 | r = link_request_to_set_mtu(link, mtu); |
3c9b8860 | 1074 | if (r < 0) |
f2341e0a | 1075 | log_link_error_errno(link, r, "Failed to set MTU to %" PRIu16 ": %m", mtu); |
3c9b8860 TG |
1076 | } |
1077 | } | |
1078 | ||
27cb34f5 | 1079 | if (link->network->dhcp_use_hostname) { |
2de2abad LB |
1080 | const char *dhcpname = NULL; |
1081 | _cleanup_free_ char *hostname = NULL; | |
3c9b8860 | 1082 | |
27cb34f5 | 1083 | if (link->network->dhcp_hostname) |
2de2abad | 1084 | dhcpname = link->network->dhcp_hostname; |
dce391e7 | 1085 | else |
2de2abad LB |
1086 | (void) sd_dhcp_lease_get_hostname(lease, &dhcpname); |
1087 | ||
1088 | if (dhcpname) { | |
1089 | r = shorten_overlong(dhcpname, &hostname); | |
1090 | if (r < 0) | |
1091 | log_link_warning_errno(link, r, "Unable to shorten overlong DHCP hostname '%s', ignoring: %m", dhcpname); | |
1092 | if (r == 1) | |
5238e957 | 1093 | log_link_notice(link, "Overlong DHCP hostname received, shortened from '%s' to '%s'", dhcpname, hostname); |
2de2abad | 1094 | } |
a7d0ef44 | 1095 | |
dce391e7 | 1096 | if (hostname) { |
59eb33e0 | 1097 | r = manager_set_hostname(link->manager, hostname); |
3c9b8860 | 1098 | if (r < 0) |
f2341e0a | 1099 | log_link_error_errno(link, r, "Failed to set transient hostname to '%s': %m", hostname); |
3c9b8860 TG |
1100 | } |
1101 | } | |
1102 | ||
27cb34f5 | 1103 | if (link->network->dhcp_use_timezone) { |
21b80ad1 LP |
1104 | const char *tz = NULL; |
1105 | ||
1106 | (void) sd_dhcp_lease_get_timezone(link->dhcp_lease, &tz); | |
1107 | ||
1108 | if (tz) { | |
59eb33e0 | 1109 | r = manager_set_timezone(link->manager, tz); |
21b80ad1 LP |
1110 | if (r < 0) |
1111 | log_link_error_errno(link, r, "Failed to set timezone to '%s': %m", tz); | |
1112 | } | |
1113 | } | |
1114 | ||
76c5a0f2 | 1115 | return dhcp4_request_address_and_routes(link, true); |
3c9b8860 | 1116 | } |
8bc17bb3 | 1117 | |
d03073dd YW |
1118 | static int dhcp_lease_ip_change(sd_dhcp_client *client, Link *link) { |
1119 | int r; | |
1120 | ||
d03073dd | 1121 | r = dhcp_lease_acquired(client, link); |
6e537f62 | 1122 | if (r < 0) |
9155da08 | 1123 | (void) dhcp4_lease_lost(link); |
d03073dd | 1124 | |
6e537f62 | 1125 | return r; |
d03073dd YW |
1126 | } |
1127 | ||
6b000af4 | 1128 | static int dhcp_server_is_deny_listed(Link *link, sd_dhcp_client *client) { |
727b5734 SS |
1129 | sd_dhcp_lease *lease; |
1130 | struct in_addr addr; | |
1131 | int r; | |
1132 | ||
1133 | assert(link); | |
1134 | assert(link->network); | |
1135 | assert(client); | |
1136 | ||
1137 | r = sd_dhcp_client_get_lease(client, &lease); | |
1138 | if (r < 0) | |
1139 | return log_link_error_errno(link, r, "Failed to get DHCP lease: %m"); | |
1140 | ||
1141 | r = sd_dhcp_lease_get_server_identifier(lease, &addr); | |
1142 | if (r < 0) | |
0da425df | 1143 | return log_link_debug_errno(link, r, "Failed to get DHCP server IP address: %m"); |
727b5734 | 1144 | |
6b000af4 | 1145 | if (set_contains(link->network->dhcp_deny_listed_ip, UINT32_TO_PTR(addr.s_addr))) { |
727b5734 SS |
1146 | log_struct(LOG_DEBUG, |
1147 | LOG_LINK_INTERFACE(link), | |
ceea6c1a YW |
1148 | LOG_LINK_MESSAGE(link, "DHCPv4 server IP address "IPV4_ADDRESS_FMT_STR" found in deny-list, ignoring offer", |
1149 | IPV4_ADDRESS_FMT_VAL(addr))); | |
727b5734 SS |
1150 | return true; |
1151 | } | |
1152 | ||
1153 | return false; | |
1154 | } | |
1155 | ||
98ebef62 SS |
1156 | static int dhcp_server_is_allow_listed(Link *link, sd_dhcp_client *client) { |
1157 | sd_dhcp_lease *lease; | |
1158 | struct in_addr addr; | |
1159 | int r; | |
1160 | ||
1161 | assert(link); | |
1162 | assert(link->network); | |
1163 | assert(client); | |
1164 | ||
1165 | r = sd_dhcp_client_get_lease(client, &lease); | |
1166 | if (r < 0) | |
1167 | return log_link_error_errno(link, r, "Failed to get DHCP lease: %m"); | |
1168 | ||
1169 | r = sd_dhcp_lease_get_server_identifier(lease, &addr); | |
1170 | if (r < 0) | |
0da425df | 1171 | return log_link_debug_errno(link, r, "Failed to get DHCP server IP address: %m"); |
98ebef62 SS |
1172 | |
1173 | if (set_contains(link->network->dhcp_allow_listed_ip, UINT32_TO_PTR(addr.s_addr))) { | |
1174 | log_struct(LOG_DEBUG, | |
1175 | LOG_LINK_INTERFACE(link), | |
ceea6c1a YW |
1176 | LOG_LINK_MESSAGE(link, "DHCPv4 server IP address "IPV4_ADDRESS_FMT_STR" found in allow-list, accepting offer", |
1177 | IPV4_ADDRESS_FMT_VAL(addr))); | |
98ebef62 SS |
1178 | return true; |
1179 | } | |
1180 | ||
1181 | return false; | |
1182 | } | |
1183 | ||
727b5734 | 1184 | static int dhcp4_handler(sd_dhcp_client *client, int event, void *userdata) { |
3c9b8860 | 1185 | Link *link = userdata; |
86e2be7b | 1186 | int r; |
3c9b8860 TG |
1187 | |
1188 | assert(link); | |
1189 | assert(link->network); | |
1190 | assert(link->manager); | |
1191 | ||
1192 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
727b5734 | 1193 | return 0; |
3c9b8860 TG |
1194 | |
1195 | switch (event) { | |
03748142 | 1196 | case SD_DHCP_CLIENT_EVENT_STOP: |
0b4b66cc | 1197 | if (link->ipv4ll) { |
8bc17bb3 SS |
1198 | log_link_debug(link, "DHCP client is stopped. Acquiring IPv4 link-local address"); |
1199 | ||
1200 | r = sd_ipv4ll_start(link->ipv4ll); | |
727b5734 SS |
1201 | if (r < 0) |
1202 | return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m"); | |
8bc17bb3 SS |
1203 | } |
1204 | ||
7da377ef | 1205 | if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) { |
efdb62df YW |
1206 | log_link_notice(link, "DHCPv4 connection considered critical, ignoring request to reconfigure it."); |
1207 | return 0; | |
1208 | } | |
1209 | ||
efdb62df | 1210 | if (link->dhcp_lease) { |
f766d9af YW |
1211 | if (link->network->dhcp_send_release) { |
1212 | r = sd_dhcp_client_send_release(client); | |
1213 | if (r < 0) | |
e558d4f4 YW |
1214 | log_link_full_errno(link, |
1215 | ERRNO_IS_DISCONNECT(r) ? LOG_DEBUG : LOG_WARNING, | |
1216 | r, "Failed to send DHCP RELEASE, ignoring: %m"); | |
f766d9af | 1217 | } |
8bea7e70 | 1218 | |
9155da08 | 1219 | r = dhcp4_lease_lost(link); |
efdb62df YW |
1220 | if (r < 0) { |
1221 | link_enter_failed(link); | |
1222 | return r; | |
1223 | } | |
1224 | } | |
1225 | ||
1226 | break; | |
8bc17bb3 | 1227 | case SD_DHCP_CLIENT_EVENT_EXPIRED: |
7da377ef | 1228 | if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) { |
efdb62df | 1229 | log_link_notice(link, "DHCPv4 connection considered critical, ignoring request to reconfigure it."); |
727b5734 | 1230 | return 0; |
3c9b8860 TG |
1231 | } |
1232 | ||
1233 | if (link->dhcp_lease) { | |
9155da08 | 1234 | r = dhcp4_lease_lost(link); |
3c9b8860 TG |
1235 | if (r < 0) { |
1236 | link_enter_failed(link); | |
727b5734 | 1237 | return r; |
3c9b8860 TG |
1238 | } |
1239 | } | |
1240 | ||
d03073dd YW |
1241 | break; |
1242 | case SD_DHCP_CLIENT_EVENT_IP_CHANGE: | |
1243 | if (FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) { | |
1244 | log_link_notice(link, "DHCPv4 connection considered critical, ignoring request to reconfigure it."); | |
1245 | return 0; | |
1246 | } | |
1247 | ||
1248 | r = dhcp_lease_ip_change(client, link); | |
1249 | if (r < 0) { | |
1250 | link_enter_failed(link); | |
1251 | return r; | |
3c9b8860 TG |
1252 | } |
1253 | ||
1254 | break; | |
03748142 | 1255 | case SD_DHCP_CLIENT_EVENT_RENEW: |
3c9b8860 TG |
1256 | r = dhcp_lease_renew(client, link); |
1257 | if (r < 0) { | |
1258 | link_enter_failed(link); | |
727b5734 | 1259 | return r; |
3c9b8860 TG |
1260 | } |
1261 | break; | |
03748142 | 1262 | case SD_DHCP_CLIENT_EVENT_IP_ACQUIRE: |
3c9b8860 TG |
1263 | r = dhcp_lease_acquired(client, link); |
1264 | if (r < 0) { | |
1265 | link_enter_failed(link); | |
727b5734 | 1266 | return r; |
3c9b8860 TG |
1267 | } |
1268 | break; | |
727b5734 | 1269 | case SD_DHCP_CLIENT_EVENT_SELECTING: |
98ebef62 SS |
1270 | if (!set_isempty(link->network->dhcp_allow_listed_ip)) { |
1271 | r = dhcp_server_is_allow_listed(link, client); | |
1272 | if (r < 0) | |
1273 | return r; | |
1274 | if (r == 0) | |
1275 | return -ENOMSG; | |
1276 | } else { | |
1277 | r = dhcp_server_is_deny_listed(link, client); | |
1278 | if (r < 0) | |
1279 | return r; | |
1280 | if (r != 0) | |
1281 | return -ENOMSG; | |
1282 | } | |
727b5734 | 1283 | break; |
0107b769 ZJS |
1284 | |
1285 | case SD_DHCP_CLIENT_EVENT_TRANSIENT_FAILURE: | |
0b4b66cc YW |
1286 | if (link->ipv4ll && !sd_ipv4ll_is_running(link->ipv4ll)) { |
1287 | log_link_debug(link, "Problems acquiring DHCP lease, acquiring IPv4 link-local address"); | |
0107b769 | 1288 | |
0b4b66cc YW |
1289 | r = sd_ipv4ll_start(link->ipv4ll); |
1290 | if (r < 0) | |
1291 | return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m"); | |
0107b769 ZJS |
1292 | } |
1293 | break; | |
1294 | ||
3c9b8860 TG |
1295 | default: |
1296 | if (event < 0) | |
f6b8196f | 1297 | log_link_warning_errno(link, event, "DHCP error: Client failed: %m"); |
3c9b8860 | 1298 | else |
f6b8196f | 1299 | log_link_warning(link, "DHCP unknown event: %i", event); |
3c9b8860 TG |
1300 | break; |
1301 | } | |
1302 | ||
727b5734 | 1303 | return 0; |
3c9b8860 TG |
1304 | } |
1305 | ||
7192bb81 LP |
1306 | static int dhcp4_set_hostname(Link *link) { |
1307 | _cleanup_free_ char *hostname = NULL; | |
1308 | const char *hn; | |
1309 | int r; | |
1310 | ||
1311 | assert(link); | |
1312 | ||
1313 | if (!link->network->dhcp_send_hostname) | |
1314 | hn = NULL; | |
1315 | else if (link->network->dhcp_hostname) | |
1316 | hn = link->network->dhcp_hostname; | |
1317 | else { | |
1318 | r = gethostname_strict(&hostname); | |
1319 | if (r < 0 && r != -ENXIO) /* ENXIO: no hostname set or hostname is "localhost" */ | |
87160186 | 1320 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to get hostname: %m"); |
7192bb81 LP |
1321 | |
1322 | hn = hostname; | |
1323 | } | |
1324 | ||
a8494759 YW |
1325 | r = sd_dhcp_client_set_hostname(link->dhcp_client, hn); |
1326 | if (r == -EINVAL && hostname) | |
1327 | /* Ignore error when the machine's hostname is not suitable to send in DHCP packet. */ | |
87160186 | 1328 | log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set hostname from kernel hostname, ignoring: %m"); |
a8494759 | 1329 | else if (r < 0) |
87160186 | 1330 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set hostname: %m"); |
64b21ece MV |
1331 | |
1332 | return 0; | |
1333 | } | |
1334 | ||
d947f7f9 | 1335 | static int dhcp4_set_client_identifier(Link *link) { |
fff1f40c YW |
1336 | int r; |
1337 | ||
1338 | assert(link); | |
1339 | assert(link->network); | |
1340 | assert(link->dhcp_client); | |
1341 | ||
1342 | switch (link->network->dhcp_client_identifier) { | |
1343 | case DHCP_CLIENT_ID_DUID: { | |
0cf7c3fd | 1344 | /* If configured, apply user specified DUID and IAID */ |
4e26a5ba | 1345 | const DUID *duid = link_get_dhcp4_duid(link); |
fff1f40c | 1346 | |
0cf7c3fd YW |
1347 | if (duid->type == DUID_TYPE_LLT && duid->raw_data_len == 0) |
1348 | r = sd_dhcp_client_set_iaid_duid_llt(link->dhcp_client, | |
4e26a5ba YW |
1349 | link->network->dhcp_iaid_set, |
1350 | link->network->dhcp_iaid, | |
0cf7c3fd YW |
1351 | duid->llt_time); |
1352 | else | |
1353 | r = sd_dhcp_client_set_iaid_duid(link->dhcp_client, | |
4e26a5ba YW |
1354 | link->network->dhcp_iaid_set, |
1355 | link->network->dhcp_iaid, | |
0cf7c3fd YW |
1356 | duid->type, |
1357 | duid->raw_data_len > 0 ? duid->raw_data : NULL, | |
1358 | duid->raw_data_len); | |
fff1f40c | 1359 | if (r < 0) |
87160186 | 1360 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set IAID+DUID: %m"); |
fff1f40c YW |
1361 | break; |
1362 | } | |
1363 | case DHCP_CLIENT_ID_DUID_ONLY: { | |
1364 | /* If configured, apply user specified DUID */ | |
4e26a5ba | 1365 | const DUID *duid = link_get_dhcp4_duid(link); |
fff1f40c | 1366 | |
0cf7c3fd YW |
1367 | if (duid->type == DUID_TYPE_LLT && duid->raw_data_len == 0) |
1368 | r = sd_dhcp_client_set_duid_llt(link->dhcp_client, | |
89b3fa66 | 1369 | duid->llt_time); |
0cf7c3fd YW |
1370 | else |
1371 | r = sd_dhcp_client_set_duid(link->dhcp_client, | |
1372 | duid->type, | |
1373 | duid->raw_data_len > 0 ? duid->raw_data : NULL, | |
1374 | duid->raw_data_len); | |
fff1f40c | 1375 | if (r < 0) |
87160186 | 1376 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set DUID: %m"); |
fff1f40c YW |
1377 | break; |
1378 | } | |
14b66dbc | 1379 | case DHCP_CLIENT_ID_MAC: { |
ca2b7cd8 | 1380 | const uint8_t *hw_addr = link->hw_addr.bytes; |
14b66dbc TR |
1381 | size_t hw_addr_len = link->hw_addr.length; |
1382 | ||
1383 | if (link->iftype == ARPHRD_INFINIBAND && hw_addr_len == INFINIBAND_ALEN) { | |
1384 | /* set_client_id expects only last 8 bytes of an IB address */ | |
1385 | hw_addr += INFINIBAND_ALEN - 8; | |
1386 | hw_addr_len -= INFINIBAND_ALEN - 8; | |
1387 | } | |
1388 | ||
fff1f40c | 1389 | r = sd_dhcp_client_set_client_id(link->dhcp_client, |
14b66dbc TR |
1390 | link->iftype, |
1391 | hw_addr, | |
1392 | hw_addr_len); | |
fff1f40c | 1393 | if (r < 0) |
87160186 | 1394 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set client ID: %m"); |
fff1f40c | 1395 | break; |
14b66dbc | 1396 | } |
fff1f40c | 1397 | default: |
04499a70 | 1398 | assert_not_reached(); |
fff1f40c YW |
1399 | } |
1400 | ||
1401 | return 0; | |
1402 | } | |
1403 | ||
3def8850 YW |
1404 | static int dhcp4_set_request_address(Link *link) { |
1405 | Address *a; | |
571eeba9 YW |
1406 | |
1407 | assert(link); | |
3def8850 YW |
1408 | assert(link->network); |
1409 | assert(link->dhcp_client); | |
571eeba9 | 1410 | |
3def8850 | 1411 | if (!FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP)) |
571eeba9 YW |
1412 | return 0; |
1413 | ||
3def8850 YW |
1414 | SET_FOREACH(a, link->addresses_foreign) { |
1415 | if (a->family != AF_INET) | |
1416 | continue; | |
1417 | if (link_address_is_dynamic(link, a)) | |
1418 | break; | |
1419 | } | |
571eeba9 | 1420 | |
3def8850 YW |
1421 | if (!a) |
1422 | return 0; | |
571eeba9 | 1423 | |
87160186 | 1424 | log_link_debug(link, "DHCPv4 CLIENT: requesting " IPV4_ADDRESS_FMT_STR, IPV4_ADDRESS_FMT_VAL(a->in_addr.in)); |
5f016e32 | 1425 | |
3def8850 | 1426 | return sd_dhcp_client_set_request_address(link->dhcp_client, &a->in_addr.in); |
571eeba9 YW |
1427 | } |
1428 | ||
e70eca9b VM |
1429 | static bool link_needs_dhcp_broadcast(Link *link) { |
1430 | const char *val; | |
1431 | int r; | |
1432 | ||
1433 | assert(link); | |
1434 | assert(link->network); | |
1435 | ||
1436 | /* Return the setting in DHCP[4].RequestBroadcast if specified. Otherwise return the device property | |
1437 | * ID_NET_DHCP_BROADCAST setting, which may be set for interfaces requiring that the DHCPOFFER message | |
1438 | * is being broadcast because they can't handle unicast messages while not fully configured. | |
1439 | * If neither is set or a failure occurs, return false, which is the default for this flag. | |
1440 | */ | |
1441 | r = link->network->dhcp_broadcast; | |
1442 | if (r < 0 && link->sd_device && sd_device_get_property_value(link->sd_device, "ID_NET_DHCP_BROADCAST", &val) >= 0) { | |
1443 | r = parse_boolean(val); | |
1444 | if (r < 0) | |
87160186 | 1445 | log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to parse ID_NET_DHCP_BROADCAST, ignoring: %m"); |
e70eca9b | 1446 | else |
87160186 | 1447 | log_link_debug(link, "DHCPv4 CLIENT: Detected ID_NET_DHCP_BROADCAST='%d'.", r); |
e70eca9b VM |
1448 | |
1449 | } | |
1450 | return r == true; | |
1451 | } | |
1452 | ||
ccffa166 | 1453 | static int dhcp4_configure(Link *link) { |
cb29c156 | 1454 | sd_dhcp_option *send_option; |
5bc945be | 1455 | void *request_options; |
3c9b8860 TG |
1456 | int r; |
1457 | ||
1458 | assert(link); | |
1459 | assert(link->network); | |
2ffd6d73 | 1460 | |
bc9e40c9 | 1461 | if (link->dhcp_client) |
87160186 | 1462 | return log_link_debug_errno(link, SYNTHETIC_ERRNO(EBUSY), "DHCPv4 client is already configured."); |
3c9b8860 | 1463 | |
bc9e40c9 YW |
1464 | r = sd_dhcp_client_new(&link->dhcp_client, link->network->dhcp_anonymize); |
1465 | if (r < 0) | |
87160186 | 1466 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to allocate DHCPv4 client: %m"); |
bc9e40c9 YW |
1467 | |
1468 | r = sd_dhcp_client_attach_event(link->dhcp_client, link->manager->event, 0); | |
1469 | if (r < 0) | |
87160186 | 1470 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to attach event to DHCPv4 client: %m"); |
3c9b8860 | 1471 | |
76253e73 | 1472 | r = sd_dhcp_client_set_mac(link->dhcp_client, |
ca2b7cd8 YW |
1473 | link->hw_addr.bytes, |
1474 | link->bcast_addr.length > 0 ? link->bcast_addr.bytes : NULL, | |
14b66dbc | 1475 | link->hw_addr.length, link->iftype); |
3c9b8860 | 1476 | if (r < 0) |
87160186 | 1477 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set MAC address: %m"); |
3c9b8860 | 1478 | |
2f8e7633 | 1479 | r = sd_dhcp_client_set_ifindex(link->dhcp_client, link->ifindex); |
3c9b8860 | 1480 | if (r < 0) |
87160186 | 1481 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set ifindex: %m"); |
3c9b8860 TG |
1482 | |
1483 | r = sd_dhcp_client_set_callback(link->dhcp_client, dhcp4_handler, link); | |
1484 | if (r < 0) | |
87160186 | 1485 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set callback: %m"); |
3c9b8860 | 1486 | |
e70eca9b | 1487 | r = sd_dhcp_client_set_request_broadcast(link->dhcp_client, link_needs_dhcp_broadcast(link)); |
3c9b8860 | 1488 | if (r < 0) |
87160186 | 1489 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for broadcast: %m"); |
3c9b8860 | 1490 | |
62f12d75 | 1491 | if (link->mtu > 0) { |
3c9b8860 TG |
1492 | r = sd_dhcp_client_set_mtu(link->dhcp_client, link->mtu); |
1493 | if (r < 0) | |
87160186 | 1494 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set MTU: %m"); |
3c9b8860 TG |
1495 | } |
1496 | ||
a83bda05 YW |
1497 | if (!link->network->dhcp_anonymize) { |
1498 | if (link->network->dhcp_use_mtu) { | |
1499 | r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_INTERFACE_MTU); | |
1500 | if (r < 0) | |
87160186 | 1501 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for MTU: %m"); |
a83bda05 | 1502 | } |
3c9b8860 | 1503 | |
a83bda05 YW |
1504 | if (link->network->dhcp_use_routes) { |
1505 | r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_STATIC_ROUTE); | |
1506 | if (r < 0) | |
87160186 | 1507 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for static route: %m"); |
1f6860d9 | 1508 | |
a83bda05 YW |
1509 | r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE); |
1510 | if (r < 0) | |
87160186 | 1511 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for classless static route: %m"); |
a83bda05 | 1512 | } |
3c9b8860 | 1513 | |
a83bda05 YW |
1514 | if (link->network->dhcp_use_domains != DHCP_USE_DOMAINS_NO) { |
1515 | r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_DOMAIN_SEARCH_LIST); | |
1516 | if (r < 0) | |
87160186 | 1517 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for domain search list: %m"); |
a83bda05 | 1518 | } |
150d3b8e | 1519 | |
a83bda05 YW |
1520 | if (link->network->dhcp_use_ntp) { |
1521 | r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_NTP_SERVER); | |
1522 | if (r < 0) | |
87160186 | 1523 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for NTP server: %m"); |
a83bda05 | 1524 | } |
4b7b5abb | 1525 | |
a83bda05 YW |
1526 | if (link->network->dhcp_use_sip) { |
1527 | r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_SIP_SERVER); | |
1528 | if (r < 0) | |
87160186 | 1529 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for SIP server: %m"); |
a83bda05 | 1530 | } |
299d578f | 1531 | |
a83bda05 YW |
1532 | if (link->network->dhcp_use_timezone) { |
1533 | r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_NEW_TZDB_TIMEZONE); | |
1534 | if (r < 0) | |
87160186 | 1535 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for timezone: %m"); |
a83bda05 | 1536 | } |
8eb9058d | 1537 | |
a83bda05 YW |
1538 | SET_FOREACH(request_options, link->network->dhcp_request_options) { |
1539 | uint32_t option = PTR_TO_UINT32(request_options); | |
5bc945be | 1540 | |
a83bda05 YW |
1541 | r = sd_dhcp_client_set_request_option(link->dhcp_client, option); |
1542 | if (r < 0) | |
87160186 | 1543 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set request flag for '%u': %m", option); |
a83bda05 | 1544 | } |
7354900d | 1545 | |
a83bda05 YW |
1546 | ORDERED_HASHMAP_FOREACH(send_option, link->network->dhcp_client_send_options) { |
1547 | r = sd_dhcp_client_add_option(link->dhcp_client, send_option); | |
1548 | if (r == -EEXIST) | |
1549 | continue; | |
1550 | if (r < 0) | |
87160186 | 1551 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set send option: %m"); |
a83bda05 | 1552 | } |
cb29c156 | 1553 | |
a83bda05 YW |
1554 | ORDERED_HASHMAP_FOREACH(send_option, link->network->dhcp_client_send_vendor_options) { |
1555 | r = sd_dhcp_client_add_vendor_option(link->dhcp_client, send_option); | |
1556 | if (r == -EEXIST) | |
1557 | continue; | |
1558 | if (r < 0) | |
87160186 | 1559 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set send option: %m"); |
a83bda05 | 1560 | } |
3c9b8860 | 1561 | |
a83bda05 | 1562 | r = dhcp4_set_hostname(link); |
3c9b8860 | 1563 | if (r < 0) |
a83bda05 | 1564 | return r; |
3c9b8860 | 1565 | |
a83bda05 YW |
1566 | if (link->network->dhcp_vendor_class_identifier) { |
1567 | r = sd_dhcp_client_set_vendor_class_identifier(link->dhcp_client, | |
1568 | link->network->dhcp_vendor_class_identifier); | |
1569 | if (r < 0) | |
87160186 | 1570 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set vendor class identifier: %m"); |
a83bda05 | 1571 | } |
7b8d23a9 | 1572 | |
a83bda05 YW |
1573 | if (link->network->dhcp_mudurl) { |
1574 | r = sd_dhcp_client_set_mud_url(link->dhcp_client, link->network->dhcp_mudurl); | |
1575 | if (r < 0) | |
87160186 | 1576 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set MUD URL: %m"); |
a83bda05 YW |
1577 | } |
1578 | ||
1579 | if (link->network->dhcp_user_class) { | |
1580 | r = sd_dhcp_client_set_user_class(link->dhcp_client, link->network->dhcp_user_class); | |
1581 | if (r < 0) | |
87160186 | 1582 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set user class: %m"); |
a83bda05 | 1583 | } |
af1c0de0 SS |
1584 | } |
1585 | ||
62f12d75 | 1586 | if (link->network->dhcp_client_port > 0) { |
9faed222 SS |
1587 | r = sd_dhcp_client_set_client_port(link->dhcp_client, link->network->dhcp_client_port); |
1588 | if (r < 0) | |
87160186 | 1589 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set listen port: %m"); |
9faed222 SS |
1590 | } |
1591 | ||
715cedfb SS |
1592 | if (link->network->dhcp_max_attempts > 0) { |
1593 | r = sd_dhcp_client_set_max_attempts(link->dhcp_client, link->network->dhcp_max_attempts); | |
1594 | if (r < 0) | |
87160186 | 1595 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set max attempts: %m"); |
715cedfb SS |
1596 | } |
1597 | ||
db5756f3 YW |
1598 | if (link->network->dhcp_ip_service_type > 0) { |
1599 | r = sd_dhcp_client_set_service_type(link->dhcp_client, link->network->dhcp_ip_service_type); | |
afe42aef | 1600 | if (r < 0) |
87160186 | 1601 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set IP service type: %m"); |
afe42aef | 1602 | } |
0f3ff4ea | 1603 | |
d6463307 SS |
1604 | if (link->network->dhcp_fallback_lease_lifetime > 0) { |
1605 | r = sd_dhcp_client_set_fallback_lease_lifetime(link->dhcp_client, link->network->dhcp_fallback_lease_lifetime); | |
1606 | if (r < 0) | |
87160186 | 1607 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed set to lease lifetime: %m"); |
d6463307 SS |
1608 | } |
1609 | ||
3def8850 YW |
1610 | r = dhcp4_set_request_address(link); |
1611 | if (r < 0) | |
87160186 | 1612 | return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to set initial DHCPv4 address: %m"); |
3def8850 | 1613 | |
0f3ff4ea | 1614 | return dhcp4_set_client_identifier(link); |
3c9b8860 | 1615 | } |
ca5ad760 | 1616 | |
d947f7f9 YW |
1617 | int dhcp4_update_mac(Link *link) { |
1618 | int r; | |
1619 | ||
1620 | assert(link); | |
1621 | ||
1622 | if (!link->dhcp_client) | |
1623 | return 0; | |
1624 | ||
ca2b7cd8 YW |
1625 | r = sd_dhcp_client_set_mac(link->dhcp_client, link->hw_addr.bytes, |
1626 | link->bcast_addr.length > 0 ? link->bcast_addr.bytes : NULL, | |
14b66dbc | 1627 | link->hw_addr.length, link->iftype); |
d947f7f9 YW |
1628 | if (r < 0) |
1629 | return r; | |
1630 | ||
76a86ffd | 1631 | return dhcp4_set_client_identifier(link); |
d947f7f9 YW |
1632 | } |
1633 | ||
294f129b | 1634 | int dhcp4_start(Link *link) { |
ccffa166 YW |
1635 | int r; |
1636 | ||
294f129b YW |
1637 | assert(link); |
1638 | ||
1639 | if (!link->dhcp_client) | |
1640 | return 0; | |
1641 | ||
ccffa166 YW |
1642 | if (!link_has_carrier(link)) |
1643 | return 0; | |
1644 | ||
294f129b YW |
1645 | if (sd_dhcp_client_is_running(link->dhcp_client) > 0) |
1646 | return 0; | |
1647 | ||
ccffa166 YW |
1648 | r = sd_dhcp_client_start(link->dhcp_client); |
1649 | if (r < 0) | |
1650 | return r; | |
294f129b | 1651 | |
ccffa166 YW |
1652 | return 1; |
1653 | } | |
1654 | ||
1655 | static int dhcp4_configure_duid(Link *link) { | |
1656 | assert(link); | |
1657 | ||
1658 | if (!IN_SET(link->network->dhcp_client_identifier, DHCP_CLIENT_ID_DUID, DHCP_CLIENT_ID_DUID_ONLY)) | |
1659 | return 1; | |
1660 | ||
1661 | return dhcp_configure_duid(link, link_get_dhcp4_duid(link)); | |
1662 | } | |
1663 | ||
1664 | int request_process_dhcp4_client(Request *req) { | |
1665 | Link *link; | |
1666 | int r; | |
1667 | ||
1668 | assert(req); | |
1669 | assert(req->link); | |
1670 | assert(req->type == REQUEST_TYPE_DHCP4_CLIENT); | |
1671 | ||
1672 | link = req->link; | |
1673 | ||
1674 | if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) | |
1675 | return 0; | |
1676 | ||
1677 | r = dhcp4_configure_duid(link); | |
1678 | if (r <= 0) | |
1679 | return r; | |
1680 | ||
1681 | r = dhcp4_configure(req->link); | |
1682 | if (r < 0) | |
1683 | return log_link_warning_errno(link, r, "Failed to configure DHCPv4 client: %m"); | |
1684 | ||
1685 | r = dhcp4_start(link); | |
1686 | if (r < 0) | |
1687 | return log_link_warning_errno(link, r, "Failed to start DHCPv4 client: %m"); | |
1688 | ||
1689 | log_link_debug(link, "DHCPv4 client is configured%s.", | |
1690 | r > 0 ? ", acquiring DHCPv4 lease" : ""); | |
1691 | ||
1692 | return 1; | |
1693 | } | |
1694 | ||
1695 | int link_request_dhcp4_client(Link *link) { | |
1696 | int r; | |
1697 | ||
1698 | assert(link); | |
1699 | ||
1700 | if (!link_dhcp4_enabled(link)) | |
1701 | return 0; | |
1702 | ||
1703 | if (link->dhcp_client) | |
1704 | return 0; | |
1705 | ||
1706 | r = link_queue_request(link, REQUEST_TYPE_DHCP4_CLIENT, NULL, false, NULL, NULL, NULL); | |
1707 | if (r < 0) | |
1708 | return log_link_warning_errno(link, r, "Failed to request configuring of the DHCPv4 client: %m"); | |
1709 | ||
1710 | log_link_debug(link, "Requested configuring of the DHCPv4 client."); | |
1711 | return 0; | |
294f129b YW |
1712 | } |
1713 | ||
ca5ad760 YW |
1714 | int config_parse_dhcp_max_attempts( |
1715 | const char *unit, | |
1716 | const char *filename, | |
1717 | unsigned line, | |
1718 | const char *section, | |
1719 | unsigned section_line, | |
1720 | const char *lvalue, | |
1721 | int ltype, | |
1722 | const char *rvalue, | |
1723 | void *data, | |
1724 | void *userdata) { | |
1725 | ||
1726 | Network *network = data; | |
1727 | uint64_t a; | |
1728 | int r; | |
1729 | ||
1730 | assert(network); | |
1731 | assert(lvalue); | |
1732 | assert(rvalue); | |
1733 | ||
1734 | if (isempty(rvalue)) { | |
1735 | network->dhcp_max_attempts = 0; | |
1736 | return 0; | |
1737 | } | |
1738 | ||
1739 | if (streq(rvalue, "infinity")) { | |
f5fbe71d | 1740 | network->dhcp_max_attempts = UINT64_MAX; |
ca5ad760 YW |
1741 | return 0; |
1742 | } | |
1743 | ||
1744 | r = safe_atou64(rvalue, &a); | |
1745 | if (r < 0) { | |
d96edb2c | 1746 | log_syntax(unit, LOG_WARNING, filename, line, r, |
ca5ad760 YW |
1747 | "Failed to parse DHCP maximum attempts, ignoring: %s", rvalue); |
1748 | return 0; | |
1749 | } | |
1750 | ||
1751 | if (a == 0) { | |
d96edb2c | 1752 | log_syntax(unit, LOG_WARNING, filename, line, 0, |
ca5ad760 YW |
1753 | "%s= must be positive integer or 'infinity', ignoring: %s", lvalue, rvalue); |
1754 | return 0; | |
1755 | } | |
1756 | ||
1757 | network->dhcp_max_attempts = a; | |
1758 | ||
1759 | return 0; | |
1760 | } | |
1761 | ||
98ebef62 | 1762 | int config_parse_dhcp_acl_ip_address( |
ca5ad760 YW |
1763 | const char *unit, |
1764 | const char *filename, | |
1765 | unsigned line, | |
1766 | const char *section, | |
1767 | unsigned section_line, | |
1768 | const char *lvalue, | |
1769 | int ltype, | |
1770 | const char *rvalue, | |
1771 | void *data, | |
1772 | void *userdata) { | |
1773 | ||
1774 | Network *network = data; | |
98ebef62 | 1775 | Set **acl; |
ca5ad760 YW |
1776 | int r; |
1777 | ||
1778 | assert(filename); | |
1779 | assert(lvalue); | |
1780 | assert(rvalue); | |
1781 | assert(data); | |
1782 | ||
98ebef62 SS |
1783 | acl = STR_IN_SET(lvalue, "DenyList", "BlackList") ? &network->dhcp_deny_listed_ip : &network->dhcp_allow_listed_ip; |
1784 | ||
ca5ad760 | 1785 | if (isempty(rvalue)) { |
98ebef62 | 1786 | *acl = set_free(*acl); |
ca5ad760 YW |
1787 | return 0; |
1788 | } | |
1789 | ||
de7fef4b | 1790 | for (const char *p = rvalue;;) { |
ca5ad760 YW |
1791 | _cleanup_free_ char *n = NULL; |
1792 | union in_addr_union ip; | |
1793 | ||
1794 | r = extract_first_word(&p, &n, NULL, 0); | |
d96edb2c YW |
1795 | if (r == -ENOMEM) |
1796 | return log_oom(); | |
ca5ad760 | 1797 | if (r < 0) { |
d96edb2c | 1798 | log_syntax(unit, LOG_WARNING, filename, line, r, |
98ebef62 SS |
1799 | "Failed to parse DHCP '%s=' IP address, ignoring assignment: %s", |
1800 | lvalue, rvalue); | |
ca5ad760 YW |
1801 | return 0; |
1802 | } | |
1803 | if (r == 0) | |
1804 | return 0; | |
1805 | ||
1806 | r = in_addr_from_string(AF_INET, n, &ip); | |
1807 | if (r < 0) { | |
d96edb2c | 1808 | log_syntax(unit, LOG_WARNING, filename, line, r, |
98ebef62 | 1809 | "DHCP '%s=' IP address is invalid, ignoring assignment: %s", lvalue, n); |
ca5ad760 YW |
1810 | continue; |
1811 | } | |
1812 | ||
98ebef62 | 1813 | r = set_ensure_put(acl, NULL, UINT32_TO_PTR(ip.in.s_addr)); |
ca5ad760 | 1814 | if (r < 0) |
d96edb2c | 1815 | log_syntax(unit, LOG_WARNING, filename, line, r, |
98ebef62 | 1816 | "Failed to store DHCP '%s=' IP address '%s', ignoring assignment: %m", lvalue, n); |
ca5ad760 | 1817 | } |
ca5ad760 YW |
1818 | } |
1819 | ||
a75b2117 SS |
1820 | int config_parse_dhcp_ip_service_type( |
1821 | const char *unit, | |
1822 | const char *filename, | |
1823 | unsigned line, | |
1824 | const char *section, | |
1825 | unsigned section_line, | |
1826 | const char *lvalue, | |
1827 | int ltype, | |
1828 | const char *rvalue, | |
1829 | void *data, | |
1830 | void *userdata) { | |
1831 | ||
1832 | assert(filename); | |
1833 | assert(lvalue); | |
1834 | assert(rvalue); | |
1835 | ||
1836 | if (streq(rvalue, "CS4")) | |
1837 | *((int *)data) = IPTOS_CLASS_CS4; | |
1838 | else if (streq(rvalue, "CS6")) | |
1839 | *((int *)data) = IPTOS_CLASS_CS6; | |
1840 | else | |
1841 | log_syntax(unit, LOG_WARNING, filename, line, 0, | |
1842 | "Failed to parse IPServiceType type '%s', ignoring.", rvalue); | |
1843 | ||
1844 | return 0; | |
1845 | } | |
1846 | ||
63481576 YW |
1847 | int config_parse_dhcp_fallback_lease_lifetime( |
1848 | const char *unit, | |
d6463307 SS |
1849 | const char *filename, |
1850 | unsigned line, | |
1851 | const char *section, | |
1852 | unsigned section_line, | |
1853 | const char *lvalue, | |
1854 | int ltype, | |
1855 | const char *rvalue, | |
1856 | void *data, | |
1857 | void *userdata) { | |
63481576 | 1858 | |
d6463307 | 1859 | Network *network = userdata; |
d2735796 | 1860 | uint32_t k; |
d6463307 SS |
1861 | |
1862 | assert(filename); | |
1863 | assert(section); | |
1864 | assert(lvalue); | |
1865 | assert(rvalue); | |
1866 | assert(data); | |
1867 | ||
1868 | if (isempty(rvalue)) { | |
1869 | network->dhcp_fallback_lease_lifetime = 0; | |
1870 | return 0; | |
1871 | } | |
1872 | ||
1873 | /* We accept only "forever" or "infinity". */ | |
1874 | if (STR_IN_SET(rvalue, "forever", "infinity")) | |
1875 | k = CACHE_INFO_INFINITY_LIFE_TIME; | |
1876 | else { | |
d96edb2c | 1877 | log_syntax(unit, LOG_WARNING, filename, line, 0, |
d6463307 SS |
1878 | "Invalid LeaseLifetime= value, ignoring: %s", rvalue); |
1879 | return 0; | |
1880 | } | |
1881 | ||
1882 | network->dhcp_fallback_lease_lifetime = k; | |
1883 | ||
1884 | return 0; | |
1885 | } | |
1886 | ||
d419ef02 MR |
1887 | int config_parse_dhcp_label( |
1888 | const char *unit, | |
1889 | const char *filename, | |
1890 | unsigned line, | |
1891 | const char *section, | |
1892 | unsigned section_line, | |
1893 | const char *lvalue, | |
1894 | int ltype, | |
1895 | const char *rvalue, | |
1896 | void *data, | |
1897 | void *userdata) { | |
1898 | ||
1899 | char **label = data; | |
1900 | ||
1901 | assert(filename); | |
1902 | assert(lvalue); | |
1903 | assert(rvalue); | |
1904 | assert(data); | |
1905 | ||
1906 | if (isempty(rvalue)) { | |
1907 | *label = mfree(*label); | |
1908 | return 0; | |
1909 | } | |
1910 | ||
1911 | if (!address_label_valid(rvalue)) { | |
1912 | log_syntax(unit, LOG_WARNING, filename, line, 0, | |
1913 | "Address label is too long or invalid, ignoring assignment: %s", rvalue); | |
1914 | return 0; | |
1915 | } | |
1916 | ||
1917 | return free_and_strdup_warn(label, rvalue); | |
1918 | } | |
1919 | ||
ca5ad760 YW |
1920 | static const char* const dhcp_client_identifier_table[_DHCP_CLIENT_ID_MAX] = { |
1921 | [DHCP_CLIENT_ID_MAC] = "mac", | |
1922 | [DHCP_CLIENT_ID_DUID] = "duid", | |
1923 | [DHCP_CLIENT_ID_DUID_ONLY] = "duid-only", | |
1924 | }; | |
1925 | ||
1926 | DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(dhcp_client_identifier, DHCPClientIdentifier); | |
1927 | DEFINE_CONFIG_PARSE_ENUM(config_parse_dhcp_client_identifier, dhcp_client_identifier, DHCPClientIdentifier, | |
1928 | "Failed to parse client identifier type"); |