]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
f579559b TG |
2 | |
3 | #include <netinet/ether.h> | |
4 | #include <linux/if.h> | |
06828bb6 | 5 | #include <linux/can/netlink.h> |
4cc7a82c | 6 | #include <unistd.h> |
0d536673 | 7 | #include <stdio_ext.h> |
f579559b | 8 | |
b5efdb8a | 9 | #include "alloc-util.h" |
1346b1f0 | 10 | #include "bus-util.h" |
27dfc982 | 11 | #include "dhcp-identifier.h" |
bd91b83e | 12 | #include "dhcp-lease-internal.h" |
686d13b9 | 13 | #include "env-file.h" |
3ffd4af2 | 14 | #include "fd-util.h" |
cf1d700d TG |
15 | #include "fileio.h" |
16 | #include "netlink-util.h" | |
c6f7c917 | 17 | #include "network-internal.h" |
a0e5c15d | 18 | #include "networkd-ipv6-proxy-ndp.h" |
8e1ad1ea | 19 | #include "networkd-lldp-tx.h" |
23f53b99 | 20 | #include "networkd-manager.h" |
1e7a0e21 | 21 | #include "networkd-ndisc.h" |
7465dd22 | 22 | #include "networkd-radv.h" |
bce67bbe | 23 | #include "networkd-routing-policy-rule.h" |
cf1d700d TG |
24 | #include "set.h" |
25 | #include "socket-util.h" | |
15a5e950 | 26 | #include "stdio-util.h" |
8b43440b | 27 | #include "string-table.h" |
51517f9e | 28 | #include "strv.h" |
e4de7287 | 29 | #include "tmpfile-util.h" |
cf1d700d TG |
30 | #include "util.h" |
31 | #include "virt.h" | |
fc2f9534 | 32 | |
f24648a6 YW |
33 | DUID* link_get_duid(Link *link) { |
34 | if (link->network->duid.type != _DUID_TYPE_INVALID) | |
35 | return &link->network->duid; | |
36 | else | |
37 | return &link->manager->duid; | |
38 | } | |
39 | ||
b9d74c40 LP |
40 | static bool link_dhcp6_enabled(Link *link) { |
41 | assert(link); | |
42 | ||
fa709992 LP |
43 | if (!socket_ipv6_is_supported()) |
44 | return false; | |
45 | ||
78c958f8 TG |
46 | if (link->flags & IFF_LOOPBACK) |
47 | return false; | |
48 | ||
49 | if (!link->network) | |
50 | return false; | |
51 | ||
e0ee46f2 | 52 | return link->network->dhcp & ADDRESS_FAMILY_IPV6; |
78c958f8 TG |
53 | } |
54 | ||
b9d74c40 LP |
55 | static bool link_dhcp4_enabled(Link *link) { |
56 | assert(link); | |
57 | ||
78c958f8 TG |
58 | if (link->flags & IFF_LOOPBACK) |
59 | return false; | |
60 | ||
61 | if (!link->network) | |
62 | return false; | |
63 | ||
e0ee46f2 | 64 | return link->network->dhcp & ADDRESS_FAMILY_IPV4; |
78c958f8 TG |
65 | } |
66 | ||
b9d74c40 LP |
67 | static bool link_dhcp4_server_enabled(Link *link) { |
68 | assert(link); | |
69 | ||
78c958f8 TG |
70 | if (link->flags & IFF_LOOPBACK) |
71 | return false; | |
72 | ||
73 | if (!link->network) | |
74 | return false; | |
75 | ||
76 | return link->network->dhcp_server; | |
77 | } | |
78 | ||
b9d74c40 LP |
79 | static bool link_ipv4ll_enabled(Link *link) { |
80 | assert(link); | |
81 | ||
78c958f8 TG |
82 | if (link->flags & IFF_LOOPBACK) |
83 | return false; | |
84 | ||
85 | if (!link->network) | |
86 | return false; | |
87 | ||
a8f5bba6 JD |
88 | if (streq_ptr(link->kind, "wireguard")) |
89 | return false; | |
90 | ||
e0ee46f2 | 91 | return link->network->link_local & ADDRESS_FAMILY_IPV4; |
d0d6a4cd TG |
92 | } |
93 | ||
b9d74c40 LP |
94 | static bool link_ipv6ll_enabled(Link *link) { |
95 | assert(link); | |
96 | ||
fa709992 LP |
97 | if (!socket_ipv6_is_supported()) |
98 | return false; | |
99 | ||
d0d6a4cd TG |
100 | if (link->flags & IFF_LOOPBACK) |
101 | return false; | |
102 | ||
103 | if (!link->network) | |
104 | return false; | |
105 | ||
a8f5bba6 JD |
106 | if (streq_ptr(link->kind, "wireguard")) |
107 | return false; | |
108 | ||
e0ee46f2 | 109 | return link->network->link_local & ADDRESS_FAMILY_IPV6; |
78c958f8 TG |
110 | } |
111 | ||
439689c6 SS |
112 | static bool link_ipv6_enabled(Link *link) { |
113 | assert(link); | |
114 | ||
115 | if (!socket_ipv6_is_supported()) | |
116 | return false; | |
117 | ||
2b00a4e0 TY |
118 | if (link->network->bridge) |
119 | return false; | |
120 | ||
4cef7fe3 TY |
121 | /* DHCPv6 client will not be started if no IPv6 link-local address is configured. */ |
122 | return link_ipv6ll_enabled(link) || network_has_static_ipv6_addresses(link->network); | |
439689c6 SS |
123 | } |
124 | ||
7465dd22 PF |
125 | static bool link_radv_enabled(Link *link) { |
126 | assert(link); | |
127 | ||
128 | if (!link_ipv6ll_enabled(link)) | |
129 | return false; | |
130 | ||
55a7c78b | 131 | return link->network->router_prefix_delegation != RADV_PREFIX_DELEGATION_NONE; |
7465dd22 PF |
132 | } |
133 | ||
8e1ad1ea | 134 | static bool link_lldp_rx_enabled(Link *link) { |
b710e6b6 LP |
135 | assert(link); |
136 | ||
ce43e484 SS |
137 | if (link->flags & IFF_LOOPBACK) |
138 | return false; | |
139 | ||
b710e6b6 LP |
140 | if (link->iftype != ARPHRD_ETHER) |
141 | return false; | |
142 | ||
ce43e484 SS |
143 | if (!link->network) |
144 | return false; | |
145 | ||
764febc2 SS |
146 | /* LLDP should be handled on bridge slaves as those have a direct |
147 | * connection to their peers not on the bridge master. Linux doesn't | |
148 | * even (by default) forward lldp packets to the bridge master.*/ | |
149 | if (streq_ptr("bridge", link->kind)) | |
ce43e484 SS |
150 | return false; |
151 | ||
34437b4f | 152 | return link->network->lldp_mode != LLDP_MODE_NO; |
ce43e484 SS |
153 | } |
154 | ||
7272b25e | 155 | static bool link_lldp_emit_enabled(Link *link) { |
8e1ad1ea LP |
156 | assert(link); |
157 | ||
158 | if (link->flags & IFF_LOOPBACK) | |
159 | return false; | |
160 | ||
161 | if (link->iftype != ARPHRD_ETHER) | |
162 | return false; | |
163 | ||
164 | if (!link->network) | |
165 | return false; | |
166 | ||
7272b25e | 167 | return link->network->lldp_emit != LLDP_EMIT_NO; |
8e1ad1ea LP |
168 | } |
169 | ||
769d324c | 170 | static bool link_ipv4_forward_enabled(Link *link) { |
b9d74c40 LP |
171 | assert(link); |
172 | ||
5a8bcb67 LP |
173 | if (link->flags & IFF_LOOPBACK) |
174 | return false; | |
175 | ||
176 | if (!link->network) | |
177 | return false; | |
178 | ||
765afd5c LP |
179 | if (link->network->ip_forward == _ADDRESS_FAMILY_BOOLEAN_INVALID) |
180 | return false; | |
181 | ||
e0ee46f2 | 182 | return link->network->ip_forward & ADDRESS_FAMILY_IPV4; |
769d324c LP |
183 | } |
184 | ||
185 | static bool link_ipv6_forward_enabled(Link *link) { | |
b9d74c40 | 186 | assert(link); |
765afd5c LP |
187 | |
188 | if (!socket_ipv6_is_supported()) | |
189 | return false; | |
190 | ||
769d324c LP |
191 | if (link->flags & IFF_LOOPBACK) |
192 | return false; | |
193 | ||
194 | if (!link->network) | |
195 | return false; | |
196 | ||
765afd5c LP |
197 | if (link->network->ip_forward == _ADDRESS_FAMILY_BOOLEAN_INVALID) |
198 | return false; | |
199 | ||
e0ee46f2 | 200 | return link->network->ip_forward & ADDRESS_FAMILY_IPV6; |
5a8bcb67 LP |
201 | } |
202 | ||
23d8b221 SS |
203 | static bool link_proxy_arp_enabled(Link *link) { |
204 | assert(link); | |
205 | ||
206 | if (link->flags & IFF_LOOPBACK) | |
207 | return false; | |
208 | ||
209 | if (!link->network) | |
210 | return false; | |
211 | ||
212 | if (link->network->proxy_arp < 0) | |
213 | return false; | |
214 | ||
215 | return true; | |
216 | } | |
217 | ||
b9d74c40 LP |
218 | static bool link_ipv6_accept_ra_enabled(Link *link) { |
219 | assert(link); | |
220 | ||
fa709992 LP |
221 | if (!socket_ipv6_is_supported()) |
222 | return false; | |
223 | ||
f5a8c43f TG |
224 | if (link->flags & IFF_LOOPBACK) |
225 | return false; | |
226 | ||
227 | if (!link->network) | |
228 | return false; | |
229 | ||
702c979f SS |
230 | if (!link_ipv6ll_enabled(link)) |
231 | return false; | |
232 | ||
f5a8c43f TG |
233 | /* If unset use system default (enabled if local forwarding is disabled. |
234 | * disabled if local forwarding is enabled). | |
235 | * If set, ignore or enforce RA independent of local forwarding state. | |
236 | */ | |
237 | if (link->network->ipv6_accept_ra < 0) | |
238 | /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */ | |
239 | return !link_ipv6_forward_enabled(link); | |
240 | else if (link->network->ipv6_accept_ra > 0) | |
241 | /* accept RA even if ip_forward is enabled */ | |
242 | return true; | |
243 | else | |
244 | /* ignore RA */ | |
245 | return false; | |
246 | } | |
247 | ||
1f0d9695 | 248 | static IPv6PrivacyExtensions link_ipv6_privacy_extensions(Link *link) { |
fa709992 | 249 | assert(link); |
d68e2e59 LP |
250 | |
251 | if (!socket_ipv6_is_supported()) | |
252 | return _IPV6_PRIVACY_EXTENSIONS_INVALID; | |
253 | ||
49092e22 | 254 | if (link->flags & IFF_LOOPBACK) |
1f0d9695 | 255 | return _IPV6_PRIVACY_EXTENSIONS_INVALID; |
49092e22 SS |
256 | |
257 | if (!link->network) | |
1f0d9695 | 258 | return _IPV6_PRIVACY_EXTENSIONS_INVALID; |
49092e22 SS |
259 | |
260 | return link->network->ipv6_privacy_extensions; | |
261 | } | |
262 | ||
439689c6 SS |
263 | static int link_enable_ipv6(Link *link) { |
264 | const char *p = NULL; | |
265 | bool disabled; | |
266 | int r; | |
267 | ||
268 | if (link->flags & IFF_LOOPBACK) | |
269 | return 0; | |
270 | ||
271 | disabled = !link_ipv6_enabled(link); | |
272 | ||
273 | p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/disable_ipv6"); | |
274 | ||
57512c89 | 275 | r = write_string_file(p, one_zero(disabled), WRITE_STRING_FILE_VERIFY_ON_FAILURE | WRITE_STRING_FILE_DISABLE_BUFFER); |
439689c6 | 276 | if (r < 0) |
b106c586 ZJS |
277 | log_link_warning_errno(link, r, "Cannot %s IPv6 for interface %s: %m", |
278 | enable_disable(!disabled), link->ifname); | |
2d37cd53 | 279 | else |
b106c586 | 280 | log_link_info(link, "IPv6 successfully %sd", enable_disable(!disabled)); |
439689c6 SS |
281 | |
282 | return 0; | |
283 | } | |
284 | ||
84de38c5 TG |
285 | void link_update_operstate(Link *link) { |
286 | LinkOperationalState operstate; | |
287 | assert(link); | |
288 | ||
289 | if (link->kernel_operstate == IF_OPER_DORMANT) | |
290 | operstate = LINK_OPERSTATE_DORMANT; | |
291 | else if (link_has_carrier(link)) { | |
292 | Address *address; | |
293 | uint8_t scope = RT_SCOPE_NOWHERE; | |
294 | Iterator i; | |
295 | ||
296 | /* if we have carrier, check what addresses we have */ | |
297 | SET_FOREACH(address, link->addresses, i) { | |
298 | if (!address_is_ready(address)) | |
299 | continue; | |
300 | ||
301 | if (address->scope < scope) | |
302 | scope = address->scope; | |
303 | } | |
304 | ||
305 | /* for operstate we also take foreign addresses into account */ | |
306 | SET_FOREACH(address, link->addresses_foreign, i) { | |
307 | if (!address_is_ready(address)) | |
308 | continue; | |
309 | ||
310 | if (address->scope < scope) | |
311 | scope = address->scope; | |
312 | } | |
313 | ||
314 | if (scope < RT_SCOPE_SITE) | |
315 | /* universally accessible addresses found */ | |
316 | operstate = LINK_OPERSTATE_ROUTABLE; | |
317 | else if (scope < RT_SCOPE_HOST) | |
318 | /* only link or site local addresses found */ | |
319 | operstate = LINK_OPERSTATE_DEGRADED; | |
320 | else | |
321 | /* no useful addresses found */ | |
322 | operstate = LINK_OPERSTATE_CARRIER; | |
323 | } else if (link->flags & IFF_UP) | |
324 | operstate = LINK_OPERSTATE_NO_CARRIER; | |
325 | else | |
326 | operstate = LINK_OPERSTATE_OFF; | |
327 | ||
328 | if (link->operstate != operstate) { | |
329 | link->operstate = operstate; | |
330 | link_send_changed(link, "OperationalState", NULL); | |
331 | link_dirty(link); | |
84de38c5 TG |
332 | } |
333 | } | |
334 | ||
51d18171 TG |
335 | #define FLAG_STRING(string, flag, old, new) \ |
336 | (((old ^ new) & flag) \ | |
337 | ? ((old & flag) ? (" -" string) : (" +" string)) \ | |
338 | : "") | |
339 | ||
1c4baffc | 340 | static int link_update_flags(Link *link, sd_netlink_message *m) { |
51d18171 TG |
341 | unsigned flags, unknown_flags_added, unknown_flags_removed, unknown_flags; |
342 | uint8_t operstate; | |
343 | int r; | |
344 | ||
345 | assert(link); | |
346 | ||
347 | r = sd_rtnl_message_link_get_flags(m, &flags); | |
6a7a4e4d LP |
348 | if (r < 0) |
349 | return log_link_warning_errno(link, r, "Could not get link flags: %m"); | |
51d18171 | 350 | |
1c4baffc | 351 | r = sd_netlink_message_read_u8(m, IFLA_OPERSTATE, &operstate); |
51d18171 TG |
352 | if (r < 0) |
353 | /* if we got a message without operstate, take it to mean | |
354 | the state was unchanged */ | |
355 | operstate = link->kernel_operstate; | |
356 | ||
357 | if ((link->flags == flags) && (link->kernel_operstate == operstate)) | |
358 | return 0; | |
359 | ||
360 | if (link->flags != flags) { | |
6a7a4e4d | 361 | log_link_debug(link, "Flags change:%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s", |
51d18171 TG |
362 | FLAG_STRING("LOOPBACK", IFF_LOOPBACK, link->flags, flags), |
363 | FLAG_STRING("MASTER", IFF_MASTER, link->flags, flags), | |
364 | FLAG_STRING("SLAVE", IFF_SLAVE, link->flags, flags), | |
365 | FLAG_STRING("UP", IFF_UP, link->flags, flags), | |
366 | FLAG_STRING("DORMANT", IFF_DORMANT, link->flags, flags), | |
367 | FLAG_STRING("LOWER_UP", IFF_LOWER_UP, link->flags, flags), | |
368 | FLAG_STRING("RUNNING", IFF_RUNNING, link->flags, flags), | |
369 | FLAG_STRING("MULTICAST", IFF_MULTICAST, link->flags, flags), | |
370 | FLAG_STRING("BROADCAST", IFF_BROADCAST, link->flags, flags), | |
371 | FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT, link->flags, flags), | |
372 | FLAG_STRING("PROMISC", IFF_PROMISC, link->flags, flags), | |
373 | FLAG_STRING("ALLMULTI", IFF_ALLMULTI, link->flags, flags), | |
374 | FLAG_STRING("PORTSEL", IFF_PORTSEL, link->flags, flags), | |
375 | FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA, link->flags, flags), | |
376 | FLAG_STRING("DYNAMIC", IFF_DYNAMIC, link->flags, flags), | |
377 | FLAG_STRING("NOARP", IFF_NOARP, link->flags, flags), | |
378 | FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS, link->flags, flags), | |
379 | FLAG_STRING("DEBUG", IFF_DEBUG, link->flags, flags), | |
380 | FLAG_STRING("ECHO", IFF_ECHO, link->flags, flags)); | |
381 | ||
382 | unknown_flags = ~(IFF_LOOPBACK | IFF_MASTER | IFF_SLAVE | IFF_UP | | |
383 | IFF_DORMANT | IFF_LOWER_UP | IFF_RUNNING | | |
384 | IFF_MULTICAST | IFF_BROADCAST | IFF_POINTOPOINT | | |
385 | IFF_PROMISC | IFF_ALLMULTI | IFF_PORTSEL | | |
386 | IFF_AUTOMEDIA | IFF_DYNAMIC | IFF_NOARP | | |
387 | IFF_NOTRAILERS | IFF_DEBUG | IFF_ECHO); | |
388 | unknown_flags_added = ((link->flags ^ flags) & flags & unknown_flags); | |
389 | unknown_flags_removed = ((link->flags ^ flags) & link->flags & unknown_flags); | |
390 | ||
391 | /* link flags are currently at most 18 bits, let's align to | |
392 | * printing 20 */ | |
393 | if (unknown_flags_added) | |
79008bdd | 394 | log_link_debug(link, |
6a7a4e4d | 395 | "Unknown link flags gained: %#.5x (ignoring)", |
51d18171 TG |
396 | unknown_flags_added); |
397 | ||
398 | if (unknown_flags_removed) | |
79008bdd | 399 | log_link_debug(link, |
6a7a4e4d | 400 | "Unknown link flags lost: %#.5x (ignoring)", |
51d18171 TG |
401 | unknown_flags_removed); |
402 | } | |
403 | ||
404 | link->flags = flags; | |
405 | link->kernel_operstate = operstate; | |
406 | ||
84de38c5 | 407 | link_update_operstate(link); |
51d18171 TG |
408 | |
409 | return 0; | |
410 | } | |
411 | ||
1046bf9b YW |
412 | DEFINE_TRIVIAL_CLEANUP_FUNC(Link*, link_unref); |
413 | ||
1c4baffc | 414 | static int link_new(Manager *manager, sd_netlink_message *message, Link **ret) { |
8e766630 | 415 | _cleanup_(link_unrefp) Link *link = NULL; |
505f8da7 | 416 | uint16_t type; |
6cad256d | 417 | const char *ifname, *kind = NULL; |
505f8da7 | 418 | int r, ifindex; |
b710e6b6 | 419 | unsigned short iftype; |
f579559b | 420 | |
0c2f9b84 | 421 | assert(manager); |
505f8da7 | 422 | assert(message); |
f579559b TG |
423 | assert(ret); |
424 | ||
6cad256d TJ |
425 | /* check for link kind */ |
426 | r = sd_netlink_message_enter_container(message, IFLA_LINKINFO); | |
427 | if (r == 0) { | |
db2f8a2e | 428 | (void) sd_netlink_message_read_string(message, IFLA_INFO_KIND, &kind); |
6cad256d TJ |
429 | r = sd_netlink_message_exit_container(message); |
430 | if (r < 0) | |
431 | return r; | |
432 | } | |
433 | ||
1c4baffc | 434 | r = sd_netlink_message_get_type(message, &type); |
505f8da7 TG |
435 | if (r < 0) |
436 | return r; | |
437 | else if (type != RTM_NEWLINK) | |
438 | return -EINVAL; | |
439 | ||
440 | r = sd_rtnl_message_link_get_ifindex(message, &ifindex); | |
441 | if (r < 0) | |
442 | return r; | |
443 | else if (ifindex <= 0) | |
444 | return -EINVAL; | |
445 | ||
b710e6b6 LP |
446 | r = sd_rtnl_message_link_get_type(message, &iftype); |
447 | if (r < 0) | |
448 | return r; | |
449 | ||
1c4baffc | 450 | r = sd_netlink_message_read_string(message, IFLA_IFNAME, &ifname); |
505f8da7 TG |
451 | if (r < 0) |
452 | return r; | |
453 | ||
17f9c355 | 454 | link = new(Link, 1); |
f579559b TG |
455 | if (!link) |
456 | return -ENOMEM; | |
457 | ||
17f9c355 YW |
458 | *link = (Link) { |
459 | .n_ref = 1, | |
460 | .manager = manager, | |
461 | .state = LINK_STATE_PENDING, | |
462 | .rtnl_extended_attrs = true, | |
463 | .ifindex = ifindex, | |
464 | .iftype = iftype, | |
465 | }; | |
466 | ||
505f8da7 TG |
467 | link->ifname = strdup(ifname); |
468 | if (!link->ifname) | |
469 | return -ENOMEM; | |
f579559b | 470 | |
6cad256d TJ |
471 | if (kind) { |
472 | link->kind = strdup(kind); | |
473 | if (!link->kind) | |
474 | return -ENOMEM; | |
475 | } | |
476 | ||
cbff7170 TJ |
477 | r = sd_netlink_message_read_u32(message, IFLA_MASTER, (uint32_t *)&link->master_ifindex); |
478 | if (r < 0) | |
479 | log_link_debug_errno(link, r, "New device has no master, continuing without"); | |
480 | ||
1c4baffc | 481 | r = sd_netlink_message_read_ether_addr(message, IFLA_ADDRESS, &link->mac); |
512922f8 | 482 | if (r < 0) |
34437b4f | 483 | log_link_debug_errno(link, r, "MAC address not found for new device, continuing without"); |
512922f8 | 484 | |
34437b4f | 485 | if (asprintf(&link->state_file, "/run/systemd/netif/links/%d", link->ifindex) < 0) |
315db1a8 | 486 | return -ENOMEM; |
fe8db0c5 | 487 | |
34437b4f | 488 | if (asprintf(&link->lease_file, "/run/systemd/netif/leases/%d", link->ifindex) < 0) |
68a8723c TG |
489 | return -ENOMEM; |
490 | ||
34437b4f | 491 | if (asprintf(&link->lldp_file, "/run/systemd/netif/lldp/%d", link->ifindex) < 0) |
49699bac SS |
492 | return -ENOMEM; |
493 | ||
d5099efc | 494 | r = hashmap_ensure_allocated(&manager->links, NULL); |
ae06ab10 TG |
495 | if (r < 0) |
496 | return r; | |
497 | ||
498 | r = hashmap_put(manager->links, INT_TO_PTR(link->ifindex), link); | |
f579559b TG |
499 | if (r < 0) |
500 | return r; | |
501 | ||
51d18171 TG |
502 | r = link_update_flags(link, message); |
503 | if (r < 0) | |
504 | return r; | |
505 | ||
1cc6c93a | 506 | *ret = TAKE_PTR(link); |
f579559b TG |
507 | |
508 | return 0; | |
509 | } | |
510 | ||
c4397d94 YW |
511 | static void link_detach_from_manager(Link *link) { |
512 | if (!link || !link->manager) | |
513 | return; | |
514 | ||
515 | hashmap_remove(link->manager->links, INT_TO_PTR(link->ifindex)); | |
516 | set_remove(link->manager->links_requesting_uuid, link); | |
517 | link_clean(link); | |
518 | ||
519 | link->manager = NULL; | |
520 | } | |
521 | ||
8301aa0b | 522 | static Link *link_free(Link *link) { |
428fd0a7 | 523 | Address *address; |
0d4ad91d | 524 | Link *carrier; |
ddfc4f6e | 525 | Route *route; |
bce67bbe | 526 | Iterator i; |
428fd0a7 | 527 | |
8301aa0b | 528 | assert(link); |
f579559b | 529 | |
ddfc4f6e YW |
530 | while ((route = set_first(link->routes))) |
531 | route_free(route); | |
532 | ||
533 | while ((route = set_first(link->routes_foreign))) | |
534 | route_free(route); | |
535 | ||
536 | link->routes = set_free(link->routes); | |
537 | link->routes_foreign = set_free(link->routes_foreign); | |
538 | ||
0ade014c YW |
539 | while ((address = set_first(link->addresses))) |
540 | address_free(address); | |
cf1d700d | 541 | |
0ade014c YW |
542 | while ((address = set_first(link->addresses_foreign))) |
543 | address_free(address); | |
adda1ed9 | 544 | |
4701725c | 545 | link->addresses = set_free(link->addresses); |
4701725c | 546 | link->addresses_foreign = set_free(link->addresses_foreign); |
adda1ed9 | 547 | |
11bf3cce LP |
548 | while ((address = link->pool_addresses)) { |
549 | LIST_REMOVE(addresses, link->pool_addresses, address); | |
550 | address_free(address); | |
551 | } | |
552 | ||
bfcdba8d | 553 | sd_dhcp_server_unref(link->dhcp_server); |
e5b04c8d | 554 | sd_dhcp_client_unref(link->dhcp_client); |
a6cc569e | 555 | sd_dhcp_lease_unref(link->dhcp_lease); |
f5be5601 | 556 | |
7272b25e | 557 | link_lldp_emit_stop(link); |
8e1ad1ea | 558 | |
68a8723c TG |
559 | free(link->lease_file); |
560 | ||
4afd3348 | 561 | sd_lldp_unref(link->lldp); |
49699bac SS |
562 | free(link->lldp_file); |
563 | ||
c69305ff LP |
564 | ndisc_flush(link); |
565 | ||
56cd007a | 566 | sd_ipv4ll_unref(link->ipv4ll); |
4138fb2c | 567 | sd_dhcp6_client_unref(link->dhcp6_client); |
1e7a0e21 | 568 | sd_ndisc_unref(link->ndisc); |
7465dd22 | 569 | sd_radv_unref(link->radv); |
1e7a0e21 | 570 | |
c4397d94 | 571 | link_detach_from_manager(link); |
f579559b | 572 | |
c166a070 | 573 | free(link->ifname); |
68a8723c | 574 | |
ceac4078 | 575 | free(link->kind); |
6cad256d | 576 | |
db2f8a2e | 577 | (void) unlink(link->state_file); |
fe8db0c5 | 578 | free(link->state_file); |
c166a070 | 579 | |
51517f9e | 580 | sd_device_unref(link->sd_device); |
b5db00e5 | 581 | |
0d4ad91d AR |
582 | HASHMAP_FOREACH (carrier, link->bound_to_links, i) |
583 | hashmap_remove(link->bound_to_links, INT_TO_PTR(carrier->ifindex)); | |
584 | hashmap_free(link->bound_to_links); | |
585 | ||
586 | HASHMAP_FOREACH (carrier, link->bound_by_links, i) | |
587 | hashmap_remove(link->bound_by_links, INT_TO_PTR(carrier->ifindex)); | |
588 | hashmap_free(link->bound_by_links); | |
589 | ||
8301aa0b | 590 | return mfree(link); |
14b746f7 TG |
591 | } |
592 | ||
8301aa0b | 593 | DEFINE_TRIVIAL_REF_UNREF_FUNC(Link, link, link_free); |
14b746f7 | 594 | |
1046bf9b YW |
595 | void link_netlink_destroy_callback(void *userdata) { |
596 | Link *link = userdata; | |
597 | ||
598 | assert(userdata); | |
599 | ||
600 | link_unref(link); | |
601 | } | |
602 | ||
11a7f229 TG |
603 | int link_get(Manager *m, int ifindex, Link **ret) { |
604 | Link *link; | |
11a7f229 TG |
605 | |
606 | assert(m); | |
11a7f229 TG |
607 | assert(ifindex); |
608 | assert(ret); | |
609 | ||
ae06ab10 | 610 | link = hashmap_get(m->links, INT_TO_PTR(ifindex)); |
11a7f229 TG |
611 | if (!link) |
612 | return -ENODEV; | |
613 | ||
614 | *ret = link; | |
615 | ||
616 | return 0; | |
617 | } | |
618 | ||
e331e246 TG |
619 | static void link_set_state(Link *link, LinkState state) { |
620 | assert(link); | |
621 | ||
622 | if (link->state == state) | |
623 | return; | |
624 | ||
625 | link->state = state; | |
626 | ||
627 | link_send_changed(link, "AdministrativeState", NULL); | |
e331e246 TG |
628 | } |
629 | ||
57bd6899 TG |
630 | static void link_enter_unmanaged(Link *link) { |
631 | assert(link); | |
632 | ||
6a7a4e4d | 633 | log_link_debug(link, "Unmanaged"); |
57bd6899 | 634 | |
e331e246 | 635 | link_set_state(link, LINK_STATE_UNMANAGED); |
57bd6899 | 636 | |
84de38c5 | 637 | link_dirty(link); |
57bd6899 TG |
638 | } |
639 | ||
111bb8f9 TG |
640 | static int link_stop_clients(Link *link) { |
641 | int r = 0, k; | |
642 | ||
643 | assert(link); | |
644 | assert(link->manager); | |
645 | assert(link->manager->event); | |
646 | ||
ba179154 | 647 | if (link->dhcp_client) { |
111bb8f9 | 648 | k = sd_dhcp_client_stop(link->dhcp_client); |
6a7a4e4d | 649 | if (k < 0) |
36c7d709 | 650 | r = log_link_warning_errno(link, k, "Could not stop DHCPv4 client: %m"); |
111bb8f9 TG |
651 | } |
652 | ||
ba179154 | 653 | if (link->ipv4ll) { |
111bb8f9 | 654 | k = sd_ipv4ll_stop(link->ipv4ll); |
6a7a4e4d | 655 | if (k < 0) |
36c7d709 | 656 | r = log_link_warning_errno(link, k, "Could not stop IPv4 link-local: %m"); |
dd43110f TG |
657 | } |
658 | ||
f5a8c43f TG |
659 | if (link->dhcp6_client) { |
660 | k = sd_dhcp6_client_stop(link->dhcp6_client); | |
661 | if (k < 0) | |
36c7d709 | 662 | r = log_link_warning_errno(link, k, "Could not stop DHCPv6 client: %m"); |
f5a8c43f | 663 | } |
4138fb2c | 664 | |
1e7a0e21 LP |
665 | if (link->ndisc) { |
666 | k = sd_ndisc_stop(link->ndisc); | |
6a7a4e4d | 667 | if (k < 0) |
36c7d709 | 668 | r = log_link_warning_errno(link, k, "Could not stop IPv6 Router Discovery: %m"); |
4138fb2c PF |
669 | } |
670 | ||
7465dd22 PF |
671 | if (link->radv) { |
672 | k = sd_radv_stop(link->radv); | |
673 | if (k < 0) | |
674 | r = log_link_warning_errno(link, k, "Could not stop IPv6 Router Advertisement: %m"); | |
675 | } | |
676 | ||
7272b25e | 677 | link_lldp_emit_stop(link); |
111bb8f9 TG |
678 | return r; |
679 | } | |
680 | ||
b22d8a00 | 681 | void link_enter_failed(Link *link) { |
ef1ba606 | 682 | assert(link); |
f882c247 | 683 | |
370e9930 | 684 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
2139694e TG |
685 | return; |
686 | ||
6a7a4e4d | 687 | log_link_warning(link, "Failed"); |
449f7554 | 688 | |
e331e246 | 689 | link_set_state(link, LINK_STATE_FAILED); |
fe8db0c5 | 690 | |
111bb8f9 TG |
691 | link_stop_clients(link); |
692 | ||
84de38c5 | 693 | link_dirty(link); |
f882c247 TG |
694 | } |
695 | ||
4f434938 LP |
696 | static Address* link_find_dhcp_server_address(Link *link) { |
697 | Address *address; | |
698 | ||
699 | assert(link); | |
700 | assert(link->network); | |
701 | ||
d4cdbea5 | 702 | /* The first statically configured address if there is any */ |
4f434938 LP |
703 | LIST_FOREACH(addresses, address, link->network->static_addresses) { |
704 | ||
705 | if (address->family != AF_INET) | |
706 | continue; | |
707 | ||
af93291c | 708 | if (in_addr_is_null(address->family, &address->in_addr)) |
4f434938 LP |
709 | continue; |
710 | ||
711 | return address; | |
712 | } | |
713 | ||
714 | /* If that didn't work, find a suitable address we got from the pool */ | |
715 | LIST_FOREACH(addresses, address, link->pool_addresses) { | |
716 | if (address->family != AF_INET) | |
717 | continue; | |
718 | ||
719 | return address; | |
720 | } | |
721 | ||
722 | return NULL; | |
723 | } | |
724 | ||
e3a7b048 | 725 | static void link_enter_configured(Link *link) { |
dd43110f TG |
726 | assert(link); |
727 | assert(link->network); | |
e3a7b048 SS |
728 | |
729 | if (link->state != LINK_STATE_SETTING_ROUTES) | |
730 | return; | |
dd43110f | 731 | |
6a7a4e4d | 732 | log_link_info(link, "Configured"); |
dd43110f | 733 | |
e331e246 | 734 | link_set_state(link, LINK_STATE_CONFIGURED); |
dd43110f | 735 | |
84de38c5 | 736 | link_dirty(link); |
dd43110f TG |
737 | } |
738 | ||
8012cd39 TG |
739 | void link_check_ready(Link *link) { |
740 | Address *a; | |
741 | Iterator i; | |
742 | ||
3c9b8860 | 743 | assert(link); |
adda1ed9 | 744 | |
7209086d SS |
745 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
746 | return; | |
747 | ||
adda1ed9 TG |
748 | if (!link->network) |
749 | return; | |
3c9b8860 | 750 | |
7715629e ST |
751 | if (!link->static_routes_configured) |
752 | return; | |
753 | ||
754 | if (!link->routing_policy_rules_configured) | |
3c9b8860 TG |
755 | return; |
756 | ||
78c958f8 | 757 | if (link_ipv4ll_enabled(link)) |
3c9b8860 TG |
758 | if (!link->ipv4ll_address || |
759 | !link->ipv4ll_route) | |
760 | return; | |
761 | ||
5971cb9d SS |
762 | if (!link->network->bridge) { |
763 | ||
764 | if (link_ipv6ll_enabled(link)) | |
765 | if (in_addr_is_null(AF_INET6, (const union in_addr_union*) &link->ipv6ll_address) > 0) | |
766 | return; | |
767 | ||
768 | if ((link_dhcp4_enabled(link) && !link_dhcp6_enabled(link) && | |
769 | !link->dhcp4_configured) || | |
770 | (link_dhcp6_enabled(link) && !link_dhcp4_enabled(link) && | |
771 | !link->dhcp6_configured) || | |
772 | (link_dhcp4_enabled(link) && link_dhcp6_enabled(link) && | |
773 | !link->dhcp4_configured && !link->dhcp6_configured)) | |
e7ab854c TG |
774 | return; |
775 | ||
5971cb9d SS |
776 | if (link_ipv6_accept_ra_enabled(link) && !link->ndisc_configured) |
777 | return; | |
778 | } | |
fe307276 | 779 | |
8012cd39 TG |
780 | SET_FOREACH(a, link->addresses, i) |
781 | if (!address_is_ready(a)) | |
782 | return; | |
783 | ||
9fdaa992 TG |
784 | if (link->state != LINK_STATE_CONFIGURED) |
785 | link_enter_configured(link); | |
3c9b8860 TG |
786 | |
787 | return; | |
788 | } | |
789 | ||
8a9b3a23 SS |
790 | static int link_set_routing_policy_rule(Link *link) { |
791 | RoutingPolicyRule *rule, *rrule = NULL; | |
792 | int r; | |
793 | ||
794 | assert(link); | |
795 | assert(link->network); | |
796 | ||
797 | LIST_FOREACH(rules, rule, link->network->rules) { | |
798 | r = routing_policy_rule_get(link->manager, rule->family, &rule->from, rule->from_prefixlen, &rule->to, | |
926062f0 SS |
799 | rule->to_prefixlen, rule->tos, rule->fwmark, rule->table, rule->iif, rule->oif, |
800 | rule->protocol, &rule->sport, &rule->dport, &rrule); | |
e6b65ab7 | 801 | if (r == 0) { |
8a9b3a23 SS |
802 | (void) routing_policy_rule_make_local(link->manager, rrule); |
803 | continue; | |
804 | } | |
805 | ||
806 | r = routing_policy_rule_configure(rule, link, link_routing_policy_rule_handler, false); | |
807 | if (r < 0) { | |
808 | log_link_warning_errno(link, r, "Could not set routing policy rules: %m"); | |
809 | link_enter_failed(link); | |
810 | return r; | |
811 | } | |
812 | ||
7715629e | 813 | link->routing_policy_rule_messages++; |
8a9b3a23 SS |
814 | } |
815 | ||
816 | routing_policy_rule_purge(link->manager, link); | |
7715629e ST |
817 | if (link->routing_policy_rule_messages == 0) { |
818 | link->routing_policy_rules_configured = true; | |
819 | link_check_ready(link); | |
820 | } else | |
821 | log_link_debug(link, "Setting routing policy rules"); | |
8a9b3a23 SS |
822 | |
823 | return 0; | |
824 | } | |
825 | ||
1c4baffc | 826 | static int route_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
1046bf9b | 827 | Link *link = userdata; |
f882c247 TG |
828 | int r; |
829 | ||
1046bf9b | 830 | assert(link); |
7715629e | 831 | assert(link->route_messages > 0); |
370e9930 TG |
832 | assert(IN_SET(link->state, LINK_STATE_SETTING_ADDRESSES, |
833 | LINK_STATE_SETTING_ROUTES, LINK_STATE_FAILED, | |
834 | LINK_STATE_LINGER)); | |
f882c247 | 835 | |
7715629e | 836 | link->route_messages--; |
f882c247 | 837 | |
77a008c0 | 838 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
f882c247 TG |
839 | return 1; |
840 | ||
1c4baffc | 841 | r = sd_netlink_message_get_errno(m); |
c166a070 | 842 | if (r < 0 && r != -EEXIST) |
a2fae7bb | 843 | log_link_warning_errno(link, r, "Could not set route: %m"); |
f882c247 | 844 | |
7715629e | 845 | if (link->route_messages == 0) { |
6a7a4e4d | 846 | log_link_debug(link, "Routes set"); |
7715629e | 847 | link->static_routes_configured = true; |
8012cd39 | 848 | link_check_ready(link); |
dd3efc09 | 849 | } |
f882c247 TG |
850 | |
851 | return 1; | |
852 | } | |
853 | ||
854 | static int link_enter_set_routes(Link *link) { | |
bf61b05a LP |
855 | enum { |
856 | PHASE_NON_GATEWAY, /* First phase: Routes without a gateway */ | |
857 | PHASE_GATEWAY, /* Second phase: Routes with a gateway */ | |
858 | _PHASE_MAX | |
859 | } phase; | |
a6cc569e | 860 | Route *rt; |
f882c247 TG |
861 | int r; |
862 | ||
863 | assert(link); | |
864 | assert(link->network); | |
ef1ba606 | 865 | assert(link->state == LINK_STATE_SETTING_ADDRESSES); |
f882c247 | 866 | |
27c34f73 SS |
867 | (void) link_set_routing_policy_rule(link); |
868 | ||
e331e246 | 869 | link_set_state(link, LINK_STATE_SETTING_ROUTES); |
f882c247 | 870 | |
bf61b05a LP |
871 | /* First add the routes that enable us to talk to gateways, then add in the others that need a gateway. */ |
872 | for (phase = 0; phase < _PHASE_MAX; phase++) | |
873 | LIST_FOREACH(routes, rt, link->network->static_routes) { | |
0d34228f | 874 | |
bf61b05a LP |
875 | if (in_addr_is_null(rt->family, &rt->gw) != (phase == PHASE_NON_GATEWAY)) |
876 | continue; | |
a6cc569e | 877 | |
0d34228f SS |
878 | r = route_configure(rt, link, route_handler); |
879 | if (r < 0) { | |
880 | log_link_warning_errno(link, r, "Could not set routes: %m"); | |
881 | link_enter_failed(link); | |
882 | return r; | |
883 | } | |
884 | ||
885 | link->route_messages++; | |
886 | } | |
f5be5601 | 887 | |
7715629e ST |
888 | if (link->route_messages == 0) { |
889 | link->static_routes_configured = true; | |
8012cd39 | 890 | link_check_ready(link); |
431ca2ce | 891 | } else |
6a7a4e4d | 892 | log_link_debug(link, "Setting routes"); |
f882c247 TG |
893 | |
894 | return 0; | |
895 | } | |
896 | ||
91b5f997 | 897 | int link_route_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
1046bf9b | 898 | Link *link = userdata; |
5c1d3fc9 UTL |
899 | int r; |
900 | ||
901 | assert(m); | |
902 | assert(link); | |
903 | assert(link->ifname); | |
904 | ||
5da8149f | 905 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
5c1d3fc9 UTL |
906 | return 1; |
907 | ||
1c4baffc | 908 | r = sd_netlink_message_get_errno(m); |
b90b025a | 909 | if (r < 0 && r != -ESRCH) |
a2fae7bb | 910 | log_link_warning_errno(link, r, "Could not drop route: %m"); |
5c1d3fc9 | 911 | |
5bdd314c | 912 | return 1; |
5c1d3fc9 UTL |
913 | } |
914 | ||
1c4baffc | 915 | static int address_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
1046bf9b | 916 | Link *link = userdata; |
f882c247 TG |
917 | int r; |
918 | ||
4958aee4 | 919 | assert(rtnl); |
f5be5601 TG |
920 | assert(m); |
921 | assert(link); | |
922 | assert(link->ifname); | |
7715629e | 923 | assert(link->address_messages > 0); |
370e9930 TG |
924 | assert(IN_SET(link->state, LINK_STATE_SETTING_ADDRESSES, |
925 | LINK_STATE_FAILED, LINK_STATE_LINGER)); | |
f882c247 | 926 | |
7715629e | 927 | link->address_messages--; |
f882c247 | 928 | |
5da8149f | 929 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
f882c247 TG |
930 | return 1; |
931 | ||
1c4baffc | 932 | r = sd_netlink_message_get_errno(m); |
c166a070 | 933 | if (r < 0 && r != -EEXIST) |
a2fae7bb | 934 | log_link_warning_errno(link, r, "could not set address: %m"); |
45af44d4 | 935 | else if (r >= 0) |
200a0868 | 936 | manager_rtnl_process_address(rtnl, m, link->manager); |
f882c247 | 937 | |
7715629e | 938 | if (link->address_messages == 0) { |
6a7a4e4d | 939 | log_link_debug(link, "Addresses set"); |
ef1ba606 | 940 | link_enter_set_routes(link); |
dd3efc09 | 941 | } |
f882c247 TG |
942 | |
943 | return 1; | |
944 | } | |
945 | ||
95b74ef6 | 946 | static int address_label_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
1046bf9b | 947 | Link *link = userdata; |
95b74ef6 SS |
948 | int r; |
949 | ||
950 | assert(rtnl); | |
951 | assert(m); | |
952 | assert(link); | |
953 | assert(link->ifname); | |
7715629e | 954 | assert(link->address_label_messages > 0); |
95b74ef6 | 955 | |
7715629e | 956 | link->address_label_messages--; |
95b74ef6 SS |
957 | |
958 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
959 | return 1; | |
960 | ||
961 | r = sd_netlink_message_get_errno(m); | |
962 | if (r < 0 && r != -EEXIST) | |
963 | log_link_warning_errno(link, r, "could not set address label: %m"); | |
964 | else if (r >= 0) | |
965 | manager_rtnl_process_address(rtnl, m, link->manager); | |
966 | ||
7715629e | 967 | if (link->address_label_messages == 0) |
95b74ef6 | 968 | log_link_debug(link, "Addresses label set"); |
95b74ef6 SS |
969 | |
970 | return 1; | |
971 | } | |
972 | ||
a380b2d4 | 973 | static int link_push_uplink_dns_to_dhcp_server(Link *link, sd_dhcp_server *s) { |
4f5f911e LP |
974 | _cleanup_free_ struct in_addr *addresses = NULL; |
975 | size_t n_addresses = 0, n_allocated = 0; | |
5512a963 | 976 | unsigned i; |
4f5f911e LP |
977 | |
978 | log_debug("Copying DNS server information from %s", link->ifname); | |
979 | ||
980 | if (!link->network) | |
981 | return 0; | |
982 | ||
5512a963 | 983 | for (i = 0; i < link->network->n_dns; i++) { |
49ad6829 | 984 | struct in_addr ia; |
4f5f911e LP |
985 | |
986 | /* Only look for IPv4 addresses */ | |
5512a963 | 987 | if (link->network->dns[i].family != AF_INET) |
4f5f911e LP |
988 | continue; |
989 | ||
49ad6829 LP |
990 | ia = link->network->dns[i].address.in; |
991 | ||
992 | /* Never propagate obviously borked data */ | |
993 | if (in4_addr_is_null(&ia) || in4_addr_is_localhost(&ia)) | |
994 | continue; | |
995 | ||
4f5f911e LP |
996 | if (!GREEDY_REALLOC(addresses, n_allocated, n_addresses + 1)) |
997 | return log_oom(); | |
998 | ||
49ad6829 | 999 | addresses[n_addresses++] = ia; |
4f5f911e LP |
1000 | } |
1001 | ||
5512a963 | 1002 | if (link->network->dhcp_use_dns && link->dhcp_lease) { |
4f5f911e LP |
1003 | const struct in_addr *da = NULL; |
1004 | int n; | |
1005 | ||
1006 | n = sd_dhcp_lease_get_dns(link->dhcp_lease, &da); | |
1007 | if (n > 0) { | |
1008 | ||
1009 | if (!GREEDY_REALLOC(addresses, n_allocated, n_addresses + n)) | |
1010 | return log_oom(); | |
1011 | ||
1012 | memcpy(addresses + n_addresses, da, n * sizeof(struct in_addr)); | |
1013 | n_addresses += n; | |
1014 | } | |
1015 | } | |
1016 | ||
1017 | if (n_addresses <= 0) | |
1018 | return 0; | |
1019 | ||
1020 | return sd_dhcp_server_set_dns(s, addresses, n_addresses); | |
1021 | } | |
1022 | ||
a380b2d4 | 1023 | static int link_push_uplink_ntp_to_dhcp_server(Link *link, sd_dhcp_server *s) { |
4f5f911e LP |
1024 | _cleanup_free_ struct in_addr *addresses = NULL; |
1025 | size_t n_addresses = 0, n_allocated = 0; | |
1026 | char **a; | |
1027 | ||
1028 | if (!link->network) | |
1029 | return 0; | |
1030 | ||
1031 | log_debug("Copying NTP server information from %s", link->ifname); | |
1032 | ||
1033 | STRV_FOREACH(a, link->network->ntp) { | |
1034 | struct in_addr ia; | |
1035 | ||
1036 | /* Only look for IPv4 addresses */ | |
1037 | if (inet_pton(AF_INET, *a, &ia) <= 0) | |
1038 | continue; | |
49ad6829 LP |
1039 | |
1040 | /* Never propagate obviously borked data */ | |
1041 | if (in4_addr_is_null(&ia) || in4_addr_is_localhost(&ia)) | |
1042 | continue; | |
4f5f911e LP |
1043 | |
1044 | if (!GREEDY_REALLOC(addresses, n_allocated, n_addresses + 1)) | |
1045 | return log_oom(); | |
1046 | ||
1047 | addresses[n_addresses++] = ia; | |
1048 | } | |
1049 | ||
5512a963 | 1050 | if (link->network->dhcp_use_ntp && link->dhcp_lease) { |
4f5f911e LP |
1051 | const struct in_addr *da = NULL; |
1052 | int n; | |
1053 | ||
1054 | n = sd_dhcp_lease_get_ntp(link->dhcp_lease, &da); | |
1055 | if (n > 0) { | |
1056 | ||
1057 | if (!GREEDY_REALLOC(addresses, n_allocated, n_addresses + n)) | |
1058 | return log_oom(); | |
1059 | ||
1060 | memcpy(addresses + n_addresses, da, n * sizeof(struct in_addr)); | |
1061 | n_addresses += n; | |
1062 | } | |
1063 | } | |
1064 | ||
1065 | if (n_addresses <= 0) | |
1066 | return 0; | |
1067 | ||
1068 | return sd_dhcp_server_set_ntp(s, addresses, n_addresses); | |
1069 | } | |
1070 | ||
f6bb7ac5 TJ |
1071 | static int link_set_bridge_fdb(Link *link) { |
1072 | FdbEntry *fdb_entry; | |
197e2809 | 1073 | int r; |
f6bb7ac5 TJ |
1074 | |
1075 | LIST_FOREACH(static_fdb_entries, fdb_entry, link->network->static_fdb_entries) { | |
1076 | r = fdb_entry_configure(link, fdb_entry); | |
197e2809 | 1077 | if (r < 0) |
f6bb7ac5 | 1078 | return log_link_error_errno(link, r, "Failed to add MAC entry to static MAC table: %m"); |
f6bb7ac5 TJ |
1079 | } |
1080 | ||
197e2809 | 1081 | return 0; |
f6bb7ac5 TJ |
1082 | } |
1083 | ||
f882c247 | 1084 | static int link_enter_set_addresses(Link *link) { |
95b74ef6 | 1085 | AddressLabel *label; |
a6cc569e | 1086 | Address *ad; |
f882c247 TG |
1087 | int r; |
1088 | ||
1089 | assert(link); | |
1090 | assert(link->network); | |
f5be5601 | 1091 | assert(link->state != _LINK_STATE_INVALID); |
f882c247 | 1092 | |
f6bb7ac5 TJ |
1093 | r = link_set_bridge_fdb(link); |
1094 | if (r < 0) | |
1095 | return r; | |
1096 | ||
e331e246 | 1097 | link_set_state(link, LINK_STATE_SETTING_ADDRESSES); |
f882c247 | 1098 | |
3d3d4255 | 1099 | LIST_FOREACH(addresses, ad, link->network->static_addresses) { |
483d099e | 1100 | r = address_configure(ad, link, address_handler, false); |
dd3efc09 | 1101 | if (r < 0) { |
5a8bcb67 | 1102 | log_link_warning_errno(link, r, "Could not set addresses: %m"); |
f5be5601 | 1103 | link_enter_failed(link); |
95b74ef6 SS |
1104 | return r; |
1105 | } | |
1106 | ||
7715629e | 1107 | link->address_messages++; |
95b74ef6 SS |
1108 | } |
1109 | ||
1110 | LIST_FOREACH(labels, label, link->network->address_labels) { | |
1111 | r = address_label_configure(label, link, address_label_handler, false); | |
1112 | if (r < 0) { | |
1113 | log_link_warning_errno(link, r, "Could not set address label: %m"); | |
1114 | link_enter_failed(link); | |
f5be5601 TG |
1115 | return r; |
1116 | } | |
1117 | ||
7715629e | 1118 | link->address_label_messages++; |
f882c247 TG |
1119 | } |
1120 | ||
d4cdbea5 TG |
1121 | /* now that we can figure out a default address for the dhcp server, |
1122 | start it */ | |
708c425d | 1123 | if (link_dhcp4_server_enabled(link) && (link->flags & IFF_UP)) { |
d4cdbea5 | 1124 | Address *address; |
4f5f911e LP |
1125 | Link *uplink = NULL; |
1126 | bool acquired_uplink = false; | |
d4cdbea5 TG |
1127 | |
1128 | address = link_find_dhcp_server_address(link); | |
1129 | if (!address) { | |
6a7a4e4d | 1130 | log_link_warning(link, "Failed to find suitable address for DHCPv4 server instance."); |
d4cdbea5 TG |
1131 | link_enter_failed(link); |
1132 | return 0; | |
1133 | } | |
1134 | ||
61986155 | 1135 | /* use the server address' subnet as the pool */ |
9b3a67c5 TG |
1136 | r = sd_dhcp_server_configure_pool(link->dhcp_server, &address->in_addr.in, address->prefixlen, |
1137 | link->network->dhcp_server_pool_offset, link->network->dhcp_server_pool_size); | |
d4cdbea5 TG |
1138 | if (r < 0) |
1139 | return r; | |
1140 | ||
1141 | /* TODO: | |
1142 | r = sd_dhcp_server_set_router(link->dhcp_server, | |
1143 | &main_address->in_addr.in); | |
1144 | if (r < 0) | |
1145 | return r; | |
d4cdbea5 TG |
1146 | */ |
1147 | ||
586ac6f7 LP |
1148 | if (link->network->dhcp_server_max_lease_time_usec > 0) { |
1149 | r = sd_dhcp_server_set_max_lease_time( | |
1150 | link->dhcp_server, | |
1151 | DIV_ROUND_UP(link->network->dhcp_server_max_lease_time_usec, USEC_PER_SEC)); | |
1152 | if (r < 0) | |
1153 | return r; | |
1154 | } | |
1155 | ||
1156 | if (link->network->dhcp_server_default_lease_time_usec > 0) { | |
1157 | r = sd_dhcp_server_set_default_lease_time( | |
1158 | link->dhcp_server, | |
1159 | DIV_ROUND_UP(link->network->dhcp_server_default_lease_time_usec, USEC_PER_SEC)); | |
1160 | if (r < 0) | |
1161 | return r; | |
1162 | } | |
1163 | ||
1a04db0f LP |
1164 | if (link->network->dhcp_server_emit_dns) { |
1165 | ||
4f5f911e | 1166 | if (link->network->n_dhcp_server_dns > 0) |
1a04db0f | 1167 | r = sd_dhcp_server_set_dns(link->dhcp_server, link->network->dhcp_server_dns, link->network->n_dhcp_server_dns); |
4f5f911e LP |
1168 | else { |
1169 | uplink = manager_find_uplink(link->manager, link); | |
1170 | acquired_uplink = true; | |
1171 | ||
1172 | if (!uplink) { | |
1173 | log_link_debug(link, "Not emitting DNS server information on link, couldn't find suitable uplink."); | |
1174 | r = 0; | |
1175 | } else | |
a380b2d4 | 1176 | r = link_push_uplink_dns_to_dhcp_server(uplink, link->dhcp_server); |
4f5f911e LP |
1177 | } |
1178 | if (r < 0) | |
1179 | log_link_warning_errno(link, r, "Failed to set DNS server for DHCP server, ignoring: %m"); | |
1a04db0f LP |
1180 | } |
1181 | ||
1a04db0f LP |
1182 | if (link->network->dhcp_server_emit_ntp) { |
1183 | ||
4f5f911e | 1184 | if (link->network->n_dhcp_server_ntp > 0) |
1a04db0f | 1185 | r = sd_dhcp_server_set_ntp(link->dhcp_server, link->network->dhcp_server_ntp, link->network->n_dhcp_server_ntp); |
4f5f911e LP |
1186 | else { |
1187 | if (!acquired_uplink) | |
1188 | uplink = manager_find_uplink(link->manager, link); | |
1189 | ||
1190 | if (!uplink) { | |
1191 | log_link_debug(link, "Not emitting NTP server information on link, couldn't find suitable uplink."); | |
1192 | r = 0; | |
1193 | } else | |
a380b2d4 | 1194 | r = link_push_uplink_ntp_to_dhcp_server(uplink, link->dhcp_server); |
4f5f911e LP |
1195 | |
1196 | } | |
1197 | if (r < 0) | |
1198 | log_link_warning_errno(link, r, "Failed to set NTP server for DHCP server, ignoring: %m"); | |
1a04db0f LP |
1199 | } |
1200 | ||
77ff6022 | 1201 | r = sd_dhcp_server_set_emit_router(link->dhcp_server, link->network->dhcp_server_emit_router); |
fc95c359 YW |
1202 | if (r < 0) |
1203 | return log_link_warning_errno(link, r, "Failed to set router emission for DHCP server: %m"); | |
77ff6022 | 1204 | |
8eb9058d LP |
1205 | if (link->network->dhcp_server_emit_timezone) { |
1206 | _cleanup_free_ char *buffer = NULL; | |
0ab8a1b6 | 1207 | const char *tz = NULL; |
8eb9058d LP |
1208 | |
1209 | if (link->network->dhcp_server_timezone) | |
1210 | tz = link->network->dhcp_server_timezone; | |
1211 | else { | |
1212 | r = get_timezone(&buffer); | |
1213 | if (r < 0) | |
1214 | log_warning_errno(r, "Failed to determine timezone: %m"); | |
1215 | else | |
1216 | tz = buffer; | |
1217 | } | |
1218 | ||
1219 | if (tz) { | |
1220 | r = sd_dhcp_server_set_timezone(link->dhcp_server, tz); | |
1221 | if (r < 0) | |
1222 | return r; | |
1223 | } | |
1224 | } | |
1225 | ||
d4cdbea5 TG |
1226 | r = sd_dhcp_server_start(link->dhcp_server); |
1227 | if (r < 0) { | |
6a7a4e4d | 1228 | log_link_warning_errno(link, r, "Could not start DHCPv4 server instance: %m"); |
d4cdbea5 TG |
1229 | |
1230 | link_enter_failed(link); | |
1231 | ||
1232 | return 0; | |
1233 | } | |
1234 | ||
6a7a4e4d | 1235 | log_link_debug(link, "Offering DHCPv4 leases"); |
d4cdbea5 TG |
1236 | } |
1237 | ||
7715629e | 1238 | if (link->address_messages == 0) |
431ca2ce | 1239 | link_enter_set_routes(link); |
6a7a4e4d LP |
1240 | else |
1241 | log_link_debug(link, "Setting addresses"); | |
431ca2ce | 1242 | |
f882c247 TG |
1243 | return 0; |
1244 | } | |
1245 | ||
91b5f997 | 1246 | int link_address_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
1046bf9b | 1247 | Link *link = userdata; |
ff254138 TG |
1248 | int r; |
1249 | ||
1250 | assert(m); | |
1251 | assert(link); | |
1252 | assert(link->ifname); | |
1253 | ||
5da8149f | 1254 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
ff254138 TG |
1255 | return 1; |
1256 | ||
1c4baffc | 1257 | r = sd_netlink_message_get_errno(m); |
b90b025a | 1258 | if (r < 0 && r != -EADDRNOTAVAIL) |
a2fae7bb | 1259 | log_link_warning_errno(link, r, "Could not drop address: %m"); |
ff254138 | 1260 | |
5bdd314c | 1261 | return 1; |
ff254138 TG |
1262 | } |
1263 | ||
13b498f9 TJ |
1264 | static int link_set_bridge_vlan(Link *link) { |
1265 | int r = 0; | |
1266 | ||
1267 | r = br_vlan_configure(link, link->network->pvid, link->network->br_vid_bitmap, link->network->br_untagged_bitmap); | |
1268 | if (r < 0) | |
1269 | log_link_error_errno(link, r, "Failed to assign VLANs to bridge port: %m"); | |
1270 | ||
1271 | return r; | |
1272 | } | |
1273 | ||
a60a720c | 1274 | static int link_set_proxy_arp(Link *link) { |
23d8b221 SS |
1275 | const char *p = NULL; |
1276 | int r; | |
1277 | ||
1278 | if (!link_proxy_arp_enabled(link)) | |
1279 | return 0; | |
1280 | ||
1281 | p = strjoina("/proc/sys/net/ipv4/conf/", link->ifname, "/proxy_arp"); | |
1282 | ||
57512c89 | 1283 | r = write_string_file(p, one_zero(link->network->proxy_arp), WRITE_STRING_FILE_VERIFY_ON_FAILURE | WRITE_STRING_FILE_DISABLE_BUFFER); |
23d8b221 SS |
1284 | if (r < 0) |
1285 | log_link_warning_errno(link, r, "Cannot configure proxy ARP for interface: %m"); | |
1286 | ||
1287 | return 0; | |
1288 | } | |
1289 | ||
1c4baffc | 1290 | static int link_set_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
1046bf9b | 1291 | Link *link = userdata; |
e1853b00 SS |
1292 | int r; |
1293 | ||
1046bf9b YW |
1294 | assert(link); |
1295 | ||
6a7a4e4d | 1296 | log_link_debug(link, "Set link"); |
e1853b00 | 1297 | |
1c4baffc | 1298 | r = sd_netlink_message_get_errno(m); |
e1853b00 | 1299 | if (r < 0 && r != -EEXIST) { |
f2341e0a | 1300 | log_link_error_errno(link, r, "Could not join netdev: %m"); |
e1853b00 | 1301 | link_enter_failed(link); |
e1853b00 SS |
1302 | } |
1303 | ||
0ae286e6 | 1304 | return 1; |
e1853b00 SS |
1305 | } |
1306 | ||
55dc8c4a YW |
1307 | static int link_configure_after_setting_mtu(Link *link); |
1308 | ||
1c4baffc | 1309 | static int set_mtu_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
1046bf9b | 1310 | Link *link = userdata; |
4f882b2a TG |
1311 | int r; |
1312 | ||
1313 | assert(m); | |
1314 | assert(link); | |
1315 | assert(link->ifname); | |
1316 | ||
55dc8c4a YW |
1317 | link->setting_mtu = false; |
1318 | ||
5da8149f | 1319 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
4f882b2a TG |
1320 | return 1; |
1321 | ||
1c4baffc | 1322 | r = sd_netlink_message_get_errno(m); |
55dc8c4a | 1323 | if (r < 0) { |
a2fae7bb | 1324 | log_link_warning_errno(link, r, "Could not set MTU: %m"); |
55dc8c4a YW |
1325 | return 1; |
1326 | } | |
1327 | ||
1328 | log_link_debug(link, "Setting MTU done."); | |
1329 | ||
1330 | if (link->state == LINK_STATE_PENDING) | |
1331 | (void) link_configure_after_setting_mtu(link); | |
4f882b2a TG |
1332 | |
1333 | return 1; | |
1334 | } | |
1335 | ||
3c9b8860 | 1336 | int link_set_mtu(Link *link, uint32_t mtu) { |
4afd3348 | 1337 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
4f882b2a TG |
1338 | int r; |
1339 | ||
1340 | assert(link); | |
1341 | assert(link->manager); | |
1342 | assert(link->manager->rtnl); | |
1343 | ||
55dc8c4a | 1344 | if (link->mtu == mtu || link->setting_mtu) |
ee493106 YW |
1345 | return 0; |
1346 | ||
6a7a4e4d | 1347 | log_link_debug(link, "Setting MTU: %" PRIu32, mtu); |
4f882b2a | 1348 | |
6a7a4e4d LP |
1349 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex); |
1350 | if (r < 0) | |
1351 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
4f882b2a | 1352 | |
44b598a1 | 1353 | /* If IPv6 not configured (no static IPv6 address and IPv6LL autoconfiguration is disabled) |
72c2500b | 1354 | * for this interface, or if it is a bridge slave, then disable IPv6 else enable it. */ |
44b598a1 SS |
1355 | (void) link_enable_ipv6(link); |
1356 | ||
1357 | /* IPv6 protocol requires a minimum MTU of IPV6_MTU_MIN(1280) bytes | |
72c2500b YW |
1358 | * on the interface. Bump up MTU bytes to IPV6_MTU_MIN. */ |
1359 | if (link_ipv6_enabled(link) && mtu < IPV6_MIN_MTU) { | |
44b598a1 SS |
1360 | |
1361 | log_link_warning(link, "Bumping MTU to " STRINGIFY(IPV6_MIN_MTU) ", as " | |
1362 | "IPv6 is requested and requires a minimum MTU of " STRINGIFY(IPV6_MIN_MTU) " bytes: %m"); | |
1363 | ||
72c2500b | 1364 | mtu = IPV6_MIN_MTU; |
44b598a1 SS |
1365 | } |
1366 | ||
1c4baffc | 1367 | r = sd_netlink_message_append_u32(req, IFLA_MTU, mtu); |
6a7a4e4d LP |
1368 | if (r < 0) |
1369 | return log_link_error_errno(link, r, "Could not append MTU: %m"); | |
4f882b2a | 1370 | |
ee38400b | 1371 | r = sd_netlink_call_async(link->manager->rtnl, NULL, req, set_mtu_handler, |
8190a388 | 1372 | link_netlink_destroy_callback, link, 0, __func__); |
6a7a4e4d LP |
1373 | if (r < 0) |
1374 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
4f882b2a | 1375 | |
ae941762 | 1376 | link_ref(link); |
1046bf9b | 1377 | link->setting_mtu = true; |
b226d99b | 1378 | |
4f882b2a TG |
1379 | return 0; |
1380 | } | |
1381 | ||
99d2baa2 | 1382 | static int set_flags_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
1046bf9b | 1383 | Link *link = userdata; |
99d2baa2 SS |
1384 | int r; |
1385 | ||
1386 | assert(m); | |
1387 | assert(link); | |
1388 | assert(link->ifname); | |
1389 | ||
1390 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
1391 | return 1; | |
1392 | ||
1393 | r = sd_netlink_message_get_errno(m); | |
1394 | if (r < 0) | |
1395 | log_link_warning_errno(link, r, "Could not set link flags: %m"); | |
1396 | ||
1397 | return 1; | |
1398 | } | |
1399 | ||
1400 | static int link_set_flags(Link *link) { | |
1401 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; | |
1402 | unsigned ifi_change = 0; | |
1403 | unsigned ifi_flags = 0; | |
1404 | int r; | |
1405 | ||
1406 | assert(link); | |
1407 | assert(link->manager); | |
1408 | assert(link->manager->rtnl); | |
1409 | ||
1410 | if (link->flags & IFF_LOOPBACK) | |
1411 | return 0; | |
1412 | ||
1413 | if (!link->network) | |
1414 | return 0; | |
1415 | ||
866e6b7a | 1416 | if (link->network->arp < 0 && link->network->multicast < 0 && link->network->allmulticast < 0) |
99d2baa2 SS |
1417 | return 0; |
1418 | ||
1419 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex); | |
1420 | if (r < 0) | |
1421 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
1422 | ||
1423 | if (link->network->arp >= 0) { | |
1424 | ifi_change |= IFF_NOARP; | |
e6ebebbe SS |
1425 | SET_FLAG(ifi_flags, IFF_NOARP, link->network->arp == 0); |
1426 | } | |
1427 | ||
1428 | if (link->network->multicast >= 0) { | |
1429 | ifi_change |= IFF_MULTICAST; | |
1430 | SET_FLAG(ifi_flags, IFF_MULTICAST, link->network->multicast); | |
99d2baa2 SS |
1431 | } |
1432 | ||
866e6b7a SS |
1433 | if (link->network->allmulticast >= 0) { |
1434 | ifi_change |= IFF_ALLMULTI; | |
1435 | SET_FLAG(ifi_flags, IFF_ALLMULTI, link->network->allmulticast); | |
1436 | } | |
1437 | ||
99d2baa2 SS |
1438 | r = sd_rtnl_message_link_set_flags(req, ifi_flags, ifi_change); |
1439 | if (r < 0) | |
1440 | return log_link_error_errno(link, r, "Could not set link flags: %m"); | |
1441 | ||
ee38400b | 1442 | r = sd_netlink_call_async(link->manager->rtnl, NULL, req, set_flags_handler, |
8190a388 | 1443 | link_netlink_destroy_callback, link, 0, __func__); |
99d2baa2 SS |
1444 | if (r < 0) |
1445 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
1446 | ||
1447 | link_ref(link); | |
1448 | ||
1449 | return 0; | |
1450 | } | |
1451 | ||
e1853b00 | 1452 | static int link_set_bridge(Link *link) { |
4afd3348 | 1453 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
e1853b00 SS |
1454 | int r; |
1455 | ||
1456 | assert(link); | |
1457 | assert(link->network); | |
1458 | ||
6a7a4e4d LP |
1459 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex); |
1460 | if (r < 0) | |
1461 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
e1853b00 SS |
1462 | |
1463 | r = sd_rtnl_message_link_set_family(req, PF_BRIDGE); | |
6a7a4e4d LP |
1464 | if (r < 0) |
1465 | return log_link_error_errno(link, r, "Could not set message family: %m"); | |
e1853b00 | 1466 | |
1c4baffc | 1467 | r = sd_netlink_message_open_container(req, IFLA_PROTINFO); |
6a7a4e4d LP |
1468 | if (r < 0) |
1469 | return log_link_error_errno(link, r, "Could not append IFLA_PROTINFO attribute: %m"); | |
e1853b00 | 1470 | |
7f9915f0 SS |
1471 | if (link->network->use_bpdu >= 0) { |
1472 | r = sd_netlink_message_append_u8(req, IFLA_BRPORT_GUARD, link->network->use_bpdu); | |
1473 | if (r < 0) | |
1474 | return log_link_error_errno(link, r, "Could not append IFLA_BRPORT_GUARD attribute: %m"); | |
1475 | } | |
eb7ff4dd | 1476 | |
7f9915f0 SS |
1477 | if (link->network->hairpin >= 0) { |
1478 | r = sd_netlink_message_append_u8(req, IFLA_BRPORT_MODE, link->network->hairpin); | |
1479 | if (r < 0) | |
1480 | return log_link_error_errno(link, r, "Could not append IFLA_BRPORT_MODE attribute: %m"); | |
1481 | } | |
eb7ff4dd | 1482 | |
7f9915f0 SS |
1483 | if (link->network->fast_leave >= 0) { |
1484 | r = sd_netlink_message_append_u8(req, IFLA_BRPORT_FAST_LEAVE, link->network->fast_leave); | |
1485 | if (r < 0) | |
1486 | return log_link_error_errno(link, r, "Could not append IFLA_BRPORT_FAST_LEAVE attribute: %m"); | |
1487 | } | |
eb7ff4dd | 1488 | |
7f9915f0 SS |
1489 | if (link->network->allow_port_to_be_root >= 0) { |
1490 | r = sd_netlink_message_append_u8(req, IFLA_BRPORT_PROTECT, link->network->allow_port_to_be_root); | |
1491 | if (r < 0) | |
1492 | return log_link_error_errno(link, r, "Could not append IFLA_BRPORT_PROTECT attribute: %m"); | |
eb7ff4dd | 1493 | |
7f9915f0 SS |
1494 | } |
1495 | ||
1496 | if (link->network->unicast_flood >= 0) { | |
1497 | r = sd_netlink_message_append_u8(req, IFLA_BRPORT_UNICAST_FLOOD, link->network->unicast_flood); | |
1498 | if (r < 0) | |
1499 | return log_link_error_errno(link, r, "Could not append IFLA_BRPORT_UNICAST_FLOOD attribute: %m"); | |
1500 | ||
1501 | } | |
eb7ff4dd | 1502 | |
34437b4f | 1503 | if (link->network->cost != 0) { |
1c4baffc | 1504 | r = sd_netlink_message_append_u32(req, IFLA_BRPORT_COST, link->network->cost); |
6a7a4e4d LP |
1505 | if (r < 0) |
1506 | return log_link_error_errno(link, r, "Could not append IFLA_BRPORT_COST attribute: %m"); | |
e1853b00 | 1507 | } |
b56be296 DJL |
1508 | if (link->network->priority != LINK_BRIDGE_PORT_PRIORITY_INVALID) { |
1509 | r = sd_netlink_message_append_u16(req, IFLA_BRPORT_PRIORITY, link->network->priority); | |
1510 | if (r < 0) | |
1511 | return log_link_error_errno(link, r, "Could not append IFLA_BRPORT_PRIORITY attribute: %m"); | |
1512 | } | |
e1853b00 | 1513 | |
1c4baffc | 1514 | r = sd_netlink_message_close_container(req); |
6a7a4e4d LP |
1515 | if (r < 0) |
1516 | return log_link_error_errno(link, r, "Could not append IFLA_LINKINFO attribute: %m"); | |
e1853b00 | 1517 | |
ee38400b | 1518 | r = sd_netlink_call_async(link->manager->rtnl, NULL, req, link_set_handler, |
8190a388 | 1519 | link_netlink_destroy_callback, link, 0, __func__); |
6a7a4e4d LP |
1520 | if (r < 0) |
1521 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
e1853b00 SS |
1522 | |
1523 | link_ref(link); | |
1524 | ||
1525 | return r; | |
1526 | } | |
1527 | ||
eb64b435 SS |
1528 | static int link_bond_set(Link *link) { |
1529 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; | |
1530 | int r; | |
1531 | ||
1532 | assert(link); | |
1533 | assert(link->network); | |
1534 | ||
1535 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_NEWLINK, link->network->bond->ifindex); | |
1536 | if (r < 0) | |
1537 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
1538 | ||
1539 | r = sd_netlink_message_set_flags(req, NLM_F_REQUEST | NLM_F_ACK); | |
1540 | if (r < 0) | |
1541 | return log_link_error_errno(link, r, "Could not set netlink flags: %m"); | |
1542 | ||
1543 | r = sd_netlink_message_open_container(req, IFLA_LINKINFO); | |
1544 | if (r < 0) | |
1545 | return log_link_error_errno(link, r, "Could not append IFLA_PROTINFO attribute: %m"); | |
1546 | ||
1547 | r = sd_netlink_message_open_container_union(req, IFLA_INFO_DATA, "bond"); | |
1548 | if (r < 0) | |
1549 | return log_link_error_errno(link, r, "Could not append IFLA_INFO_DATA attribute: %m"); | |
1550 | ||
1551 | if (link->network->active_slave) { | |
1552 | r = sd_netlink_message_append_u32(req, IFLA_BOND_ACTIVE_SLAVE, link->ifindex); | |
1553 | if (r < 0) | |
1554 | return log_link_error_errno(link, r, "Could not append IFLA_BOND_ACTIVE_SLAVE attribute: %m"); | |
1555 | } | |
1556 | ||
1557 | if (link->network->primary_slave) { | |
1558 | r = sd_netlink_message_append_u32(req, IFLA_BOND_PRIMARY, link->ifindex); | |
1559 | if (r < 0) | |
1560 | return log_link_error_errno(link, r, "Could not append IFLA_BOND_PRIMARY attribute: %m"); | |
1561 | } | |
1562 | ||
1563 | r = sd_netlink_message_close_container(req); | |
1564 | if (r < 0) | |
1565 | return log_link_error_errno(link, r, "Could not append IFLA_LINKINFO attribute: %m"); | |
1566 | ||
1567 | r = sd_netlink_message_close_container(req); | |
1568 | if (r < 0) | |
1569 | return log_link_error_errno(link, r, "Could not append IFLA_INFO_DATA attribute: %m"); | |
1570 | ||
ee38400b | 1571 | r = sd_netlink_call_async(link->manager->rtnl, NULL, req, set_flags_handler, |
8190a388 | 1572 | link_netlink_destroy_callback, link, 0, __func__); |
eb64b435 SS |
1573 | if (r < 0) |
1574 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
1575 | ||
1576 | link_ref(link); | |
1577 | ||
1578 | return r; | |
1579 | } | |
1580 | ||
34437b4f LP |
1581 | static int link_lldp_save(Link *link) { |
1582 | _cleanup_free_ char *temp_path = NULL; | |
1583 | _cleanup_fclose_ FILE *f = NULL; | |
1584 | sd_lldp_neighbor **l = NULL; | |
1585 | int n = 0, r, i; | |
49699bac SS |
1586 | |
1587 | assert(link); | |
34437b4f LP |
1588 | assert(link->lldp_file); |
1589 | ||
1590 | if (!link->lldp) { | |
1591 | (void) unlink(link->lldp_file); | |
1592 | return 0; | |
1593 | } | |
1594 | ||
1595 | r = sd_lldp_get_neighbors(link->lldp, &l); | |
1596 | if (r < 0) | |
1597 | goto finish; | |
1598 | if (r == 0) { | |
1599 | (void) unlink(link->lldp_file); | |
1600 | goto finish; | |
1601 | } | |
1602 | ||
1603 | n = r; | |
1604 | ||
1605 | r = fopen_temporary(link->lldp_file, &f, &temp_path); | |
1606 | if (r < 0) | |
1607 | goto finish; | |
1608 | ||
1609 | fchmod(fileno(f), 0644); | |
49699bac | 1610 | |
34437b4f LP |
1611 | for (i = 0; i < n; i++) { |
1612 | const void *p; | |
1613 | le64_t u; | |
1614 | size_t sz; | |
1615 | ||
1616 | r = sd_lldp_neighbor_get_raw(l[i], &p, &sz); | |
9ef61f2e | 1617 | if (r < 0) |
34437b4f LP |
1618 | goto finish; |
1619 | ||
1620 | u = htole64(sz); | |
1621 | (void) fwrite(&u, 1, sizeof(u), f); | |
1622 | (void) fwrite(p, 1, sz, f); | |
1623 | } | |
49699bac | 1624 | |
34437b4f LP |
1625 | r = fflush_and_check(f); |
1626 | if (r < 0) | |
1627 | goto finish; | |
1628 | ||
1629 | if (rename(temp_path, link->lldp_file) < 0) { | |
1630 | r = -errno; | |
1631 | goto finish; | |
9ef61f2e | 1632 | } |
34437b4f LP |
1633 | |
1634 | finish: | |
1635 | if (r < 0) { | |
1636 | (void) unlink(link->lldp_file); | |
1637 | if (temp_path) | |
1638 | (void) unlink(temp_path); | |
1639 | ||
1640 | log_link_error_errno(link, r, "Failed to save LLDP data to %s: %m", link->lldp_file); | |
1641 | } | |
1642 | ||
1643 | if (l) { | |
1644 | for (i = 0; i < n; i++) | |
1645 | sd_lldp_neighbor_unref(l[i]); | |
1646 | free(l); | |
1647 | } | |
1648 | ||
1649 | return r; | |
1650 | } | |
1651 | ||
90dffb22 | 1652 | static void lldp_handler(sd_lldp *lldp, sd_lldp_event event, sd_lldp_neighbor *n, void *userdata) { |
34437b4f | 1653 | Link *link = userdata; |
90dffb22 | 1654 | int r; |
34437b4f LP |
1655 | |
1656 | assert(link); | |
1657 | ||
1658 | (void) link_lldp_save(link); | |
90dffb22 | 1659 | |
7272b25e | 1660 | if (link_lldp_emit_enabled(link) && event == SD_LLDP_EVENT_ADDED) { |
90dffb22 LP |
1661 | /* If we received information about a new neighbor, restart the LLDP "fast" logic */ |
1662 | ||
1663 | log_link_debug(link, "Received LLDP datagram from previously unknown neighbor, restarting 'fast' LLDP transmission."); | |
1664 | ||
7272b25e | 1665 | r = link_lldp_emit_start(link); |
90dffb22 LP |
1666 | if (r < 0) |
1667 | log_link_warning_errno(link, r, "Failed to restart LLDP transmission: %m"); | |
1668 | } | |
49699bac SS |
1669 | } |
1670 | ||
e7ab854c TG |
1671 | static int link_acquire_ipv6_conf(Link *link) { |
1672 | int r; | |
1673 | ||
1674 | assert(link); | |
1675 | ||
e7ab854c | 1676 | if (link_ipv6_accept_ra_enabled(link)) { |
1e7a0e21 | 1677 | assert(link->ndisc); |
e7ab854c TG |
1678 | |
1679 | log_link_debug(link, "Discovering IPv6 routers"); | |
1680 | ||
1e7a0e21 | 1681 | r = sd_ndisc_start(link->ndisc); |
63348d13 | 1682 | if (r < 0 && r != -EBUSY) |
e7ab854c TG |
1683 | return log_link_warning_errno(link, r, "Could not start IPv6 Router Discovery: %m"); |
1684 | } | |
1685 | ||
7465dd22 PF |
1686 | if (link_radv_enabled(link)) { |
1687 | assert(link->radv); | |
1688 | assert(in_addr_is_link_local(AF_INET6, (const union in_addr_union*)&link->ipv6ll_address) > 0); | |
1689 | ||
1690 | log_link_debug(link, "Starting IPv6 Router Advertisements"); | |
1691 | ||
1692 | r = sd_radv_start(link->radv); | |
1693 | if (r < 0 && r != -EBUSY) | |
1694 | return log_link_warning_errno(link, r, "Could not start IPv6 Router Advertisement: %m"); | |
1695 | } | |
1696 | ||
10752343 PF |
1697 | (void) dhcp6_request_prefix_delegation(link); |
1698 | ||
e7ab854c TG |
1699 | return 0; |
1700 | } | |
1701 | ||
6fc25497 | 1702 | static int link_acquire_ipv4_conf(Link *link) { |
ff254138 TG |
1703 | int r; |
1704 | ||
1705 | assert(link); | |
1706 | assert(link->network); | |
ff254138 TG |
1707 | assert(link->manager); |
1708 | assert(link->manager->event); | |
1709 | ||
78c958f8 | 1710 | if (link_ipv4ll_enabled(link)) { |
eb34d4af | 1711 | assert(link->ipv4ll); |
ff254138 | 1712 | |
6a7a4e4d | 1713 | log_link_debug(link, "Acquiring IPv4 link-local address"); |
5c1d3fc9 UTL |
1714 | |
1715 | r = sd_ipv4ll_start(link->ipv4ll); | |
6a7a4e4d LP |
1716 | if (r < 0) |
1717 | return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m"); | |
5c1d3fc9 UTL |
1718 | } |
1719 | ||
78c958f8 | 1720 | if (link_dhcp4_enabled(link)) { |
eb34d4af | 1721 | assert(link->dhcp_client); |
ff254138 | 1722 | |
6a7a4e4d | 1723 | log_link_debug(link, "Acquiring DHCPv4 lease"); |
ab47d620 | 1724 | |
5c1d3fc9 | 1725 | r = sd_dhcp_client_start(link->dhcp_client); |
6a7a4e4d LP |
1726 | if (r < 0) |
1727 | return log_link_warning_errno(link, r, "Could not acquire DHCPv4 lease: %m"); | |
5c1d3fc9 | 1728 | } |
ff254138 | 1729 | |
6fc25497 SS |
1730 | return 0; |
1731 | } | |
1732 | ||
1733 | static int link_acquire_conf(Link *link) { | |
1734 | int r; | |
1735 | ||
1736 | assert(link); | |
1737 | ||
1738 | r = link_acquire_ipv4_conf(link); | |
1739 | if (r < 0) | |
1740 | return r; | |
1741 | ||
1742 | if (in_addr_is_null(AF_INET6, (const union in_addr_union*) &link->ipv6ll_address) == 0) { | |
1743 | r = link_acquire_ipv6_conf(link); | |
1744 | if (r < 0) | |
1745 | return r; | |
1746 | } | |
1747 | ||
7272b25e LP |
1748 | if (link_lldp_emit_enabled(link)) { |
1749 | r = link_lldp_emit_start(link); | |
8e1ad1ea LP |
1750 | if (r < 0) |
1751 | return log_link_warning_errno(link, r, "Failed to start LLDP transmission: %m"); | |
1752 | } | |
1753 | ||
ff254138 TG |
1754 | return 0; |
1755 | } | |
1756 | ||
a61bb41c | 1757 | bool link_has_carrier(Link *link) { |
deb2e523 TG |
1758 | /* see Documentation/networking/operstates.txt in the kernel sources */ |
1759 | ||
a61bb41c | 1760 | if (link->kernel_operstate == IF_OPER_UP) |
deb2e523 TG |
1761 | return true; |
1762 | ||
a61bb41c | 1763 | if (link->kernel_operstate == IF_OPER_UNKNOWN) |
deb2e523 | 1764 | /* operstate may not be implemented, so fall back to flags */ |
a61bb41c | 1765 | if ((link->flags & IFF_LOWER_UP) && !(link->flags & IFF_DORMANT)) |
deb2e523 TG |
1766 | return true; |
1767 | ||
1768 | return false; | |
1769 | } | |
1770 | ||
1c4baffc | 1771 | static int link_up_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
1046bf9b | 1772 | Link *link = userdata; |
dd3efc09 TG |
1773 | int r; |
1774 | ||
1746cf2a TG |
1775 | assert(link); |
1776 | ||
5da8149f | 1777 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
1746cf2a TG |
1778 | return 1; |
1779 | ||
1c4baffc | 1780 | r = sd_netlink_message_get_errno(m); |
6a7a4e4d | 1781 | if (r < 0) |
26d6b214 | 1782 | /* we warn but don't fail the link, as it may be brought up later */ |
a2fae7bb | 1783 | log_link_warning_errno(link, r, "Could not bring up interface: %m"); |
45ad2c13 | 1784 | |
f882c247 TG |
1785 | return 1; |
1786 | } | |
1787 | ||
14b6bb77 | 1788 | int link_up(Link *link) { |
4afd3348 | 1789 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
5c3072ea | 1790 | uint8_t ipv6ll_mode; |
f579559b TG |
1791 | int r; |
1792 | ||
f882c247 | 1793 | assert(link); |
c106cc36 | 1794 | assert(link->network); |
f882c247 TG |
1795 | assert(link->manager); |
1796 | assert(link->manager->rtnl); | |
1797 | ||
6a7a4e4d | 1798 | log_link_debug(link, "Bringing link up"); |
449f7554 | 1799 | |
6a7a4e4d LP |
1800 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex); |
1801 | if (r < 0) | |
1802 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
f579559b | 1803 | |
2b2d8603 | 1804 | /* set it free if not enslaved with networkd */ |
6cb955c6 | 1805 | if (!link->network->bridge && !link->network->bond && !link->network->vrf) { |
2b2d8603 TY |
1806 | r = sd_netlink_message_append_u32(req, IFLA_MASTER, 0); |
1807 | if (r < 0) | |
1808 | return log_link_error_errno(link, r, "Could not append IFLA_MASTER attribute: %m"); | |
1809 | } | |
1810 | ||
5d4795f3 | 1811 | r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP); |
6a7a4e4d LP |
1812 | if (r < 0) |
1813 | return log_link_error_errno(link, r, "Could not set link flags: %m"); | |
fc25d7f8 | 1814 | |
c106cc36 | 1815 | if (link->network->mac) { |
1c4baffc | 1816 | r = sd_netlink_message_append_ether_addr(req, IFLA_ADDRESS, link->network->mac); |
6a7a4e4d LP |
1817 | if (r < 0) |
1818 | return log_link_error_errno(link, r, "Could not set MAC address: %m"); | |
c106cc36 TG |
1819 | } |
1820 | ||
1c4baffc | 1821 | r = sd_netlink_message_open_container(req, IFLA_AF_SPEC); |
6a7a4e4d LP |
1822 | if (r < 0) |
1823 | return log_link_error_errno(link, r, "Could not open IFLA_AF_SPEC container: %m"); | |
d0d6a4cd | 1824 | |
439689c6 | 1825 | if (link_ipv6_enabled(link)) { |
01d28f81 | 1826 | /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */ |
1c4baffc | 1827 | r = sd_netlink_message_open_container(req, AF_INET6); |
01d28f81 TG |
1828 | if (r < 0) |
1829 | return log_link_error_errno(link, r, "Could not open AF_INET6 container: %m"); | |
d0d6a4cd | 1830 | |
fbc38f23 TY |
1831 | if (!link_ipv6ll_enabled(link)) |
1832 | ipv6ll_mode = IN6_ADDR_GEN_MODE_NONE; | |
1833 | else { | |
1834 | const char *p = NULL; | |
1835 | _cleanup_free_ char *stable_secret = NULL; | |
1836 | ||
1837 | p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/stable_secret"); | |
1838 | r = read_one_line_file(p, &stable_secret); | |
1839 | ||
1840 | if (r < 0) | |
1841 | ipv6ll_mode = IN6_ADDR_GEN_MODE_EUI64; | |
1842 | else | |
1843 | ipv6ll_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY; | |
1844 | } | |
1c4baffc | 1845 | r = sd_netlink_message_append_u8(req, IFLA_INET6_ADDR_GEN_MODE, ipv6ll_mode); |
01d28f81 TG |
1846 | if (r < 0) |
1847 | return log_link_error_errno(link, r, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m"); | |
d0d6a4cd | 1848 | |
01d28f81 | 1849 | if (!in_addr_is_null(AF_INET6, &link->network->ipv6_token)) { |
1c4baffc | 1850 | r = sd_netlink_message_append_in6_addr(req, IFLA_INET6_TOKEN, &link->network->ipv6_token.in6); |
01d28f81 TG |
1851 | if (r < 0) |
1852 | return log_link_error_errno(link, r, "Could not append IFLA_INET6_TOKEN: %m"); | |
1853 | } | |
1854 | ||
1c4baffc | 1855 | r = sd_netlink_message_close_container(req); |
6a7a4e4d | 1856 | if (r < 0) |
01d28f81 | 1857 | return log_link_error_errno(link, r, "Could not close AF_INET6 container: %m"); |
7f77697a | 1858 | } |
d0d6a4cd | 1859 | |
1c4baffc | 1860 | r = sd_netlink_message_close_container(req); |
6a7a4e4d LP |
1861 | if (r < 0) |
1862 | return log_link_error_errno(link, r, "Could not close IFLA_AF_SPEC container: %m"); | |
d0d6a4cd | 1863 | |
ee38400b | 1864 | r = sd_netlink_call_async(link->manager->rtnl, NULL, req, link_up_handler, |
8190a388 | 1865 | link_netlink_destroy_callback, link, 0, __func__); |
6a7a4e4d LP |
1866 | if (r < 0) |
1867 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
f579559b | 1868 | |
b226d99b TG |
1869 | link_ref(link); |
1870 | ||
f882c247 TG |
1871 | return 0; |
1872 | } | |
1873 | ||
bd5038f8 MKB |
1874 | static int link_up_can(Link *link) { |
1875 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; | |
1876 | int r; | |
1877 | ||
1878 | assert(link); | |
1879 | ||
1880 | log_link_debug(link, "Bringing CAN link up"); | |
1881 | ||
1882 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex); | |
1883 | if (r < 0) | |
1884 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
1885 | ||
1886 | r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP); | |
1887 | if (r < 0) | |
1888 | return log_link_error_errno(link, r, "Could not set link flags: %m"); | |
1889 | ||
ee38400b | 1890 | r = sd_netlink_call_async(link->manager->rtnl, NULL, req, link_up_handler, |
8190a388 | 1891 | link_netlink_destroy_callback, link, 0, __func__); |
bd5038f8 MKB |
1892 | if (r < 0) |
1893 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
1894 | ||
1895 | link_ref(link); | |
1896 | ||
1897 | return 0; | |
1898 | } | |
1899 | ||
06828bb6 HP |
1900 | static int link_set_can(Link *link) { |
1901 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; | |
1902 | int r; | |
1903 | ||
1904 | assert(link); | |
1905 | assert(link->network); | |
1906 | assert(link->manager); | |
1907 | assert(link->manager->rtnl); | |
1908 | ||
1909 | log_link_debug(link, "link_set_can"); | |
1910 | ||
1911 | r = sd_rtnl_message_new_link(link->manager->rtnl, &m, RTM_NEWLINK, link->ifindex); | |
1912 | if (r < 0) | |
1913 | return log_link_error_errno(link, r, "Failed to allocate netlink message: %m"); | |
1914 | ||
1915 | r = sd_netlink_message_set_flags(m, NLM_F_REQUEST | NLM_F_ACK); | |
1916 | if (r < 0) | |
1917 | return log_link_error_errno(link, r, "Could not set netlink flags: %m"); | |
1918 | ||
1919 | r = sd_netlink_message_open_container(m, IFLA_LINKINFO); | |
1920 | if (r < 0) | |
1921 | return log_link_error_errno(link, r, "Failed to open netlink container: %m"); | |
1922 | ||
1923 | r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, link->kind); | |
1924 | if (r < 0) | |
1925 | return log_link_error_errno(link, r, "Could not append IFLA_INFO_DATA attribute: %m"); | |
1926 | ||
1927 | if (link->network->can_bitrate > 0 || link->network->can_sample_point > 0) { | |
1928 | struct can_bittiming bt = { | |
1929 | .bitrate = link->network->can_bitrate, | |
1930 | .sample_point = link->network->can_sample_point, | |
1931 | }; | |
1932 | ||
1933 | if (link->network->can_bitrate > UINT32_MAX) { | |
1934 | log_link_error(link, "bitrate (%zu) too big.", link->network->can_bitrate); | |
1935 | return -ERANGE; | |
1936 | } | |
1937 | ||
1938 | log_link_debug(link, "Setting bitrate = %d bit/s", bt.bitrate); | |
1939 | if (link->network->can_sample_point > 0) | |
1940 | log_link_debug(link, "Setting sample point = %d.%d%%", bt.sample_point / 10, bt.sample_point % 10); | |
1941 | else | |
1942 | log_link_debug(link, "Using default sample point"); | |
1943 | ||
1944 | r = sd_netlink_message_append_data(m, IFLA_CAN_BITTIMING, &bt, sizeof(bt)); | |
1945 | if (r < 0) | |
1946 | return log_link_error_errno(link, r, "Could not append IFLA_CAN_BITTIMING attribute: %m"); | |
1947 | } | |
1948 | ||
1949 | if (link->network->can_restart_us > 0) { | |
1950 | char time_string[FORMAT_TIMESPAN_MAX]; | |
1951 | uint64_t restart_ms; | |
1952 | ||
1953 | if (link->network->can_restart_us == USEC_INFINITY) | |
1954 | restart_ms = 0; | |
1955 | else | |
1956 | restart_ms = DIV_ROUND_UP(link->network->can_restart_us, USEC_PER_MSEC); | |
1957 | ||
1958 | format_timespan(time_string, FORMAT_TIMESPAN_MAX, restart_ms * 1000, MSEC_PER_SEC); | |
1959 | ||
1960 | if (restart_ms > UINT32_MAX) { | |
1961 | log_link_error(link, "restart timeout (%s) too big.", time_string); | |
1962 | return -ERANGE; | |
1963 | } | |
1964 | ||
1965 | log_link_debug(link, "Setting restart = %s", time_string); | |
1966 | ||
1967 | r = sd_netlink_message_append_u32(m, IFLA_CAN_RESTART_MS, restart_ms); | |
1968 | if (r < 0) | |
1969 | return log_link_error_errno(link, r, "Could not append IFLA_CAN_RESTART_MS attribute: %m"); | |
1970 | } | |
1971 | ||
1972 | r = sd_netlink_message_close_container(m); | |
1973 | if (r < 0) | |
1974 | return log_link_error_errno(link, r, "Failed to close netlink container: %m"); | |
1975 | ||
1976 | r = sd_netlink_message_close_container(m); | |
1977 | if (r < 0) | |
1978 | return log_link_error_errno(link, r, "Failed to close netlink container: %m"); | |
1979 | ||
ee38400b | 1980 | r = sd_netlink_call_async(link->manager->rtnl, NULL, m, link_set_handler, |
8190a388 | 1981 | link_netlink_destroy_callback, link, 0, __func__); |
06828bb6 HP |
1982 | if (r < 0) |
1983 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
1984 | ||
1985 | link_ref(link); | |
1986 | ||
1987 | if (!(link->flags & IFF_UP)) { | |
1988 | r = link_up_can(link); | |
1989 | if (r < 0) { | |
1990 | link_enter_failed(link); | |
1991 | return r; | |
1992 | } | |
1993 | } | |
1994 | ||
1995 | log_link_debug(link, "link_set_can done"); | |
1996 | ||
1997 | return r; | |
1998 | } | |
1999 | ||
1c4baffc | 2000 | static int link_down_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
1046bf9b | 2001 | Link *link = userdata; |
0d4ad91d AR |
2002 | int r; |
2003 | ||
2004 | assert(link); | |
2005 | ||
2006 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
2007 | return 1; | |
2008 | ||
1c4baffc | 2009 | r = sd_netlink_message_get_errno(m); |
0d4ad91d | 2010 | if (r < 0) |
a2fae7bb | 2011 | log_link_warning_errno(link, r, "Could not bring down interface: %m"); |
0d4ad91d | 2012 | |
1046bf9b | 2013 | if (streq_ptr(link->kind, "can")) |
06828bb6 | 2014 | link_set_can(link); |
06828bb6 | 2015 | |
0d4ad91d AR |
2016 | return 1; |
2017 | } | |
2018 | ||
14b6bb77 | 2019 | int link_down(Link *link) { |
4afd3348 | 2020 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
0d4ad91d AR |
2021 | int r; |
2022 | ||
2023 | assert(link); | |
2024 | assert(link->manager); | |
2025 | assert(link->manager->rtnl); | |
2026 | ||
6a7a4e4d | 2027 | log_link_debug(link, "Bringing link down"); |
0d4ad91d AR |
2028 | |
2029 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, | |
2030 | RTM_SETLINK, link->ifindex); | |
6a7a4e4d LP |
2031 | if (r < 0) |
2032 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
0d4ad91d AR |
2033 | |
2034 | r = sd_rtnl_message_link_set_flags(req, 0, IFF_UP); | |
6a7a4e4d LP |
2035 | if (r < 0) |
2036 | return log_link_error_errno(link, r, "Could not set link flags: %m"); | |
0d4ad91d | 2037 | |
ee38400b | 2038 | r = sd_netlink_call_async(link->manager->rtnl, NULL, req, link_down_handler, |
8190a388 | 2039 | link_netlink_destroy_callback, link, 0, __func__); |
6a7a4e4d LP |
2040 | if (r < 0) |
2041 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
0d4ad91d AR |
2042 | |
2043 | link_ref(link); | |
2044 | ||
2045 | return 0; | |
2046 | } | |
2047 | ||
2048 | static int link_handle_bound_to_list(Link *link) { | |
2049 | Link *l; | |
2050 | Iterator i; | |
2051 | int r; | |
2052 | bool required_up = false; | |
2053 | bool link_is_up = false; | |
2054 | ||
2055 | assert(link); | |
2056 | ||
2057 | if (hashmap_isempty(link->bound_to_links)) | |
2058 | return 0; | |
2059 | ||
2060 | if (link->flags & IFF_UP) | |
2061 | link_is_up = true; | |
2062 | ||
2063 | HASHMAP_FOREACH (l, link->bound_to_links, i) | |
2064 | if (link_has_carrier(l)) { | |
2065 | required_up = true; | |
2066 | break; | |
2067 | } | |
2068 | ||
2069 | if (!required_up && link_is_up) { | |
2070 | r = link_down(link); | |
2071 | if (r < 0) | |
2072 | return r; | |
2073 | } else if (required_up && !link_is_up) { | |
2074 | r = link_up(link); | |
2075 | if (r < 0) | |
2076 | return r; | |
2077 | } | |
2078 | ||
2079 | return 0; | |
2080 | } | |
2081 | ||
2082 | static int link_handle_bound_by_list(Link *link) { | |
2083 | Iterator i; | |
2084 | Link *l; | |
2085 | int r; | |
2086 | ||
2087 | assert(link); | |
2088 | ||
2089 | if (hashmap_isempty(link->bound_by_links)) | |
2090 | return 0; | |
2091 | ||
2092 | HASHMAP_FOREACH (l, link->bound_by_links, i) { | |
2093 | r = link_handle_bound_to_list(l); | |
2094 | if (r < 0) | |
2095 | return r; | |
2096 | } | |
2097 | ||
2098 | return 0; | |
2099 | } | |
2100 | ||
2101 | static int link_put_carrier(Link *link, Link *carrier, Hashmap **h) { | |
2102 | int r; | |
2103 | ||
2104 | assert(link); | |
2105 | assert(carrier); | |
2106 | ||
2107 | if (link == carrier) | |
2108 | return 0; | |
2109 | ||
2110 | if (hashmap_get(*h, INT_TO_PTR(carrier->ifindex))) | |
2111 | return 0; | |
2112 | ||
2113 | r = hashmap_ensure_allocated(h, NULL); | |
2114 | if (r < 0) | |
2115 | return r; | |
2116 | ||
2117 | r = hashmap_put(*h, INT_TO_PTR(carrier->ifindex), carrier); | |
2118 | if (r < 0) | |
2119 | return r; | |
2120 | ||
2121 | return 0; | |
2122 | } | |
2123 | ||
2124 | static int link_new_bound_by_list(Link *link) { | |
2125 | Manager *m; | |
2126 | Link *carrier; | |
2127 | Iterator i; | |
2128 | int r; | |
2129 | bool list_updated = false; | |
2130 | ||
2131 | assert(link); | |
2132 | assert(link->manager); | |
2133 | ||
2134 | m = link->manager; | |
2135 | ||
b295beea | 2136 | HASHMAP_FOREACH(carrier, m->links, i) { |
0d4ad91d AR |
2137 | if (!carrier->network) |
2138 | continue; | |
2139 | ||
2140 | if (strv_isempty(carrier->network->bind_carrier)) | |
2141 | continue; | |
2142 | ||
2143 | if (strv_fnmatch(carrier->network->bind_carrier, link->ifname, 0)) { | |
2144 | r = link_put_carrier(link, carrier, &link->bound_by_links); | |
2145 | if (r < 0) | |
2146 | return r; | |
2147 | ||
2148 | list_updated = true; | |
2149 | } | |
2150 | } | |
2151 | ||
2152 | if (list_updated) | |
84de38c5 | 2153 | link_dirty(link); |
0d4ad91d | 2154 | |
b295beea | 2155 | HASHMAP_FOREACH(carrier, link->bound_by_links, i) { |
0d4ad91d AR |
2156 | r = link_put_carrier(carrier, link, &carrier->bound_to_links); |
2157 | if (r < 0) | |
2158 | return r; | |
2159 | ||
84de38c5 | 2160 | link_dirty(carrier); |
0d4ad91d AR |
2161 | } |
2162 | ||
2163 | return 0; | |
2164 | } | |
2165 | ||
2166 | static int link_new_bound_to_list(Link *link) { | |
2167 | Manager *m; | |
2168 | Link *carrier; | |
2169 | Iterator i; | |
2170 | int r; | |
2171 | bool list_updated = false; | |
2172 | ||
2173 | assert(link); | |
2174 | assert(link->manager); | |
2175 | ||
2176 | if (!link->network) | |
2177 | return 0; | |
2178 | ||
2179 | if (strv_isempty(link->network->bind_carrier)) | |
2180 | return 0; | |
2181 | ||
2182 | m = link->manager; | |
2183 | ||
2184 | HASHMAP_FOREACH (carrier, m->links, i) { | |
2185 | if (strv_fnmatch(link->network->bind_carrier, carrier->ifname, 0)) { | |
2186 | r = link_put_carrier(link, carrier, &link->bound_to_links); | |
2187 | if (r < 0) | |
2188 | return r; | |
2189 | ||
2190 | list_updated = true; | |
2191 | } | |
2192 | } | |
2193 | ||
2194 | if (list_updated) | |
84de38c5 | 2195 | link_dirty(link); |
0d4ad91d AR |
2196 | |
2197 | HASHMAP_FOREACH (carrier, link->bound_to_links, i) { | |
2198 | r = link_put_carrier(carrier, link, &carrier->bound_by_links); | |
2199 | if (r < 0) | |
2200 | return r; | |
2201 | ||
84de38c5 | 2202 | link_dirty(carrier); |
0d4ad91d AR |
2203 | } |
2204 | ||
2205 | return 0; | |
2206 | } | |
2207 | ||
2208 | static int link_new_carrier_maps(Link *link) { | |
2209 | int r; | |
2210 | ||
2211 | r = link_new_bound_by_list(link); | |
2212 | if (r < 0) | |
2213 | return r; | |
2214 | ||
2215 | r = link_handle_bound_by_list(link); | |
2216 | if (r < 0) | |
2217 | return r; | |
2218 | ||
2219 | r = link_new_bound_to_list(link); | |
2220 | if (r < 0) | |
2221 | return r; | |
2222 | ||
2223 | r = link_handle_bound_to_list(link); | |
2224 | if (r < 0) | |
2225 | return r; | |
2226 | ||
2227 | return 0; | |
2228 | } | |
2229 | ||
2230 | static void link_free_bound_to_list(Link *link) { | |
2231 | Link *bound_to; | |
2232 | Iterator i; | |
2233 | ||
2234 | HASHMAP_FOREACH (bound_to, link->bound_to_links, i) { | |
2235 | hashmap_remove(link->bound_to_links, INT_TO_PTR(bound_to->ifindex)); | |
2236 | ||
2237 | if (hashmap_remove(bound_to->bound_by_links, INT_TO_PTR(link->ifindex))) | |
84de38c5 | 2238 | link_dirty(bound_to); |
0d4ad91d AR |
2239 | } |
2240 | ||
2241 | return; | |
2242 | } | |
2243 | ||
2244 | static void link_free_bound_by_list(Link *link) { | |
2245 | Link *bound_by; | |
2246 | Iterator i; | |
2247 | ||
2248 | HASHMAP_FOREACH (bound_by, link->bound_by_links, i) { | |
2249 | hashmap_remove(link->bound_by_links, INT_TO_PTR(bound_by->ifindex)); | |
2250 | ||
2251 | if (hashmap_remove(bound_by->bound_to_links, INT_TO_PTR(link->ifindex))) { | |
84de38c5 | 2252 | link_dirty(bound_by); |
0d4ad91d AR |
2253 | link_handle_bound_to_list(bound_by); |
2254 | } | |
2255 | } | |
2256 | ||
2257 | return; | |
2258 | } | |
2259 | ||
2260 | static void link_free_carrier_maps(Link *link) { | |
2261 | bool list_updated = false; | |
2262 | ||
2263 | assert(link); | |
2264 | ||
2265 | if (!hashmap_isempty(link->bound_to_links)) { | |
2266 | link_free_bound_to_list(link); | |
2267 | list_updated = true; | |
2268 | } | |
2269 | ||
2270 | if (!hashmap_isempty(link->bound_by_links)) { | |
2271 | link_free_bound_by_list(link); | |
2272 | list_updated = true; | |
2273 | } | |
2274 | ||
2275 | if (list_updated) | |
84de38c5 | 2276 | link_dirty(link); |
0d4ad91d AR |
2277 | |
2278 | return; | |
2279 | } | |
2280 | ||
2281 | void link_drop(Link *link) { | |
2282 | if (!link || link->state == LINK_STATE_LINGER) | |
2283 | return; | |
2284 | ||
2285 | link_set_state(link, LINK_STATE_LINGER); | |
2286 | ||
2287 | link_free_carrier_maps(link); | |
2288 | ||
6a7a4e4d | 2289 | log_link_debug(link, "Link removed"); |
0d4ad91d | 2290 | |
db2f8a2e | 2291 | (void) unlink(link->state_file); |
c4397d94 YW |
2292 | |
2293 | link_detach_from_manager(link); | |
2294 | ||
0d4ad91d AR |
2295 | link_unref(link); |
2296 | ||
2297 | return; | |
2298 | } | |
2299 | ||
3f265037 | 2300 | static int link_joined(Link *link) { |
f882c247 TG |
2301 | int r; |
2302 | ||
ef1ba606 | 2303 | assert(link); |
f5be5601 | 2304 | assert(link->network); |
dd3efc09 | 2305 | |
0d4ad91d AR |
2306 | if (!hashmap_isempty(link->bound_to_links)) { |
2307 | r = link_handle_bound_to_list(link); | |
2308 | if (r < 0) | |
2309 | return r; | |
2310 | } else if (!(link->flags & IFF_UP)) { | |
505f8da7 TG |
2311 | r = link_up(link); |
2312 | if (r < 0) { | |
2313 | link_enter_failed(link); | |
2314 | return r; | |
2315 | } | |
ef1ba606 | 2316 | } |
f882c247 | 2317 | |
9ed794a3 | 2318 | if (link->network->bridge) { |
e1853b00 | 2319 | r = link_set_bridge(link); |
6a7a4e4d LP |
2320 | if (r < 0) |
2321 | log_link_error_errno(link, r, "Could not set bridge message: %m"); | |
e1853b00 SS |
2322 | } |
2323 | ||
eb64b435 SS |
2324 | if (link->network->bond) { |
2325 | r = link_bond_set(link); | |
2326 | if (r < 0) | |
2327 | log_link_error_errno(link, r, "Could not set bond message: %m"); | |
2328 | } | |
2329 | ||
ffff9abe TJ |
2330 | if (link->network->use_br_vlan && |
2331 | (link->network->bridge || streq_ptr("bridge", link->kind))) { | |
13b498f9 TJ |
2332 | r = link_set_bridge_vlan(link); |
2333 | if (r < 0) | |
2334 | log_link_error_errno(link, r, "Could not set bridge vlan: %m"); | |
2335 | } | |
2336 | ||
c1835a42 RM |
2337 | /* Skip setting up addresses until it gets carrier, |
2338 | or it would try to set addresses twice, | |
2339 | which is bad for non-idempotent steps. */ | |
dad2d78e | 2340 | if (!link_has_carrier(link) && !link->network->configure_without_carrier) |
c1835a42 RM |
2341 | return 0; |
2342 | ||
fb6730c4 | 2343 | return link_enter_set_addresses(link); |
02b59d57 TG |
2344 | } |
2345 | ||
62e2d5bb | 2346 | static int netdev_join_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
1046bf9b | 2347 | Link *link = userdata; |
02b59d57 TG |
2348 | int r; |
2349 | ||
1746cf2a | 2350 | assert(link); |
ef1ba606 | 2351 | assert(link->network); |
02b59d57 | 2352 | |
313cefa1 | 2353 | link->enslaving--; |
52433f6b | 2354 | |
5da8149f | 2355 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
02b59d57 TG |
2356 | return 1; |
2357 | ||
1c4baffc | 2358 | r = sd_netlink_message_get_errno(m); |
856f962c | 2359 | if (r < 0 && r != -EEXIST) { |
a2fae7bb | 2360 | log_link_error_errno(link, r, "Could not join netdev: %m"); |
ef1ba606 TG |
2361 | link_enter_failed(link); |
2362 | return 1; | |
ba179154 | 2363 | } else |
6a7a4e4d | 2364 | log_link_debug(link, "Joined netdev"); |
02b59d57 | 2365 | |
856f962c | 2366 | if (link->enslaving <= 0) |
3f265037 | 2367 | link_joined(link); |
02b59d57 TG |
2368 | |
2369 | return 1; | |
2370 | } | |
2371 | ||
3f265037 | 2372 | static int link_enter_join_netdev(Link *link) { |
6a0a2f86 | 2373 | NetDev *netdev; |
672682a6 | 2374 | Iterator i; |
02b59d57 TG |
2375 | int r; |
2376 | ||
2377 | assert(link); | |
2378 | assert(link->network); | |
8434fd5c | 2379 | assert(link->state == LINK_STATE_PENDING); |
02b59d57 | 2380 | |
e331e246 | 2381 | link_set_state(link, LINK_STATE_ENSLAVING); |
02b59d57 | 2382 | |
84de38c5 | 2383 | link_dirty(link); |
fe8db0c5 | 2384 | |
7951dea2 SS |
2385 | if (!link->network->bridge && |
2386 | !link->network->bond && | |
6cb955c6 | 2387 | !link->network->vrf && |
6a0a2f86 | 2388 | hashmap_isempty(link->network->stacked_netdevs)) |
3f265037 | 2389 | return link_joined(link); |
02b59d57 | 2390 | |
d9c67ea1 | 2391 | if (link->network->bond) { |
cbff7170 TJ |
2392 | if (link->network->bond->state == NETDEV_STATE_READY && |
2393 | link->network->bond->ifindex == link->master_ifindex) | |
2394 | return link_joined(link); | |
2395 | ||
f2341e0a LP |
2396 | log_struct(LOG_DEBUG, |
2397 | LOG_LINK_INTERFACE(link), | |
2398 | LOG_NETDEV_INTERFACE(link->network->bond), | |
a1230ff9 | 2399 | LOG_LINK_MESSAGE(link, "Enslaving by '%s'", link->network->bond->ifname)); |
f2341e0a LP |
2400 | |
2401 | r = netdev_join(link->network->bond, link, netdev_join_handler); | |
52433f6b | 2402 | if (r < 0) { |
f2341e0a LP |
2403 | log_struct_errno(LOG_WARNING, r, |
2404 | LOG_LINK_INTERFACE(link), | |
2405 | LOG_NETDEV_INTERFACE(link->network->bond), | |
a1230ff9 | 2406 | LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", link->network->bond->ifname)); |
52433f6b TG |
2407 | link_enter_failed(link); |
2408 | return r; | |
2409 | } | |
2410 | ||
313cefa1 | 2411 | link->enslaving++; |
0ad6148e MO |
2412 | } |
2413 | ||
d9c67ea1 | 2414 | if (link->network->bridge) { |
f2341e0a LP |
2415 | log_struct(LOG_DEBUG, |
2416 | LOG_LINK_INTERFACE(link), | |
2417 | LOG_NETDEV_INTERFACE(link->network->bridge), | |
a1230ff9 | 2418 | LOG_LINK_MESSAGE(link, "Enslaving by '%s'", link->network->bridge->ifname)); |
f2341e0a LP |
2419 | |
2420 | r = netdev_join(link->network->bridge, link, netdev_join_handler); | |
0ad6148e | 2421 | if (r < 0) { |
f2341e0a LP |
2422 | log_struct_errno(LOG_WARNING, r, |
2423 | LOG_LINK_INTERFACE(link), | |
2424 | LOG_NETDEV_INTERFACE(link->network->bridge), | |
a1230ff9 | 2425 | LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", link->network->bridge->ifname)); |
0ad6148e MO |
2426 | link_enter_failed(link); |
2427 | return r; | |
2428 | } | |
2429 | ||
313cefa1 | 2430 | link->enslaving++; |
52433f6b TG |
2431 | } |
2432 | ||
6cb955c6 AR |
2433 | if (link->network->vrf) { |
2434 | log_struct(LOG_DEBUG, | |
2435 | LOG_LINK_INTERFACE(link), | |
2436 | LOG_NETDEV_INTERFACE(link->network->vrf), | |
a1230ff9 ZJS |
2437 | LOG_LINK_MESSAGE(link, "Enslaving by '%s'", link->network->vrf->ifname)); |
2438 | ||
6cb955c6 AR |
2439 | r = netdev_join(link->network->vrf, link, netdev_join_handler); |
2440 | if (r < 0) { | |
2441 | log_struct_errno(LOG_WARNING, r, | |
2442 | LOG_LINK_INTERFACE(link), | |
2443 | LOG_NETDEV_INTERFACE(link->network->vrf), | |
a1230ff9 | 2444 | LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", link->network->vrf->ifname)); |
6cb955c6 AR |
2445 | link_enter_failed(link); |
2446 | return r; | |
2447 | } | |
2448 | ||
2449 | link->enslaving++; | |
2450 | } | |
2451 | ||
6a0a2f86 | 2452 | HASHMAP_FOREACH(netdev, link->network->stacked_netdevs, i) { |
7951dea2 | 2453 | |
a63e5daa SS |
2454 | if (netdev->ifindex > 0) { |
2455 | link_joined(link); | |
2456 | continue; | |
2457 | } | |
2458 | ||
f2341e0a LP |
2459 | log_struct(LOG_DEBUG, |
2460 | LOG_LINK_INTERFACE(link), | |
2461 | LOG_NETDEV_INTERFACE(netdev), | |
a1230ff9 | 2462 | LOG_LINK_MESSAGE(link, "Enslaving by '%s'", netdev->ifname)); |
f2341e0a LP |
2463 | |
2464 | r = netdev_join(netdev, link, netdev_join_handler); | |
7951dea2 | 2465 | if (r < 0) { |
f2341e0a LP |
2466 | log_struct_errno(LOG_WARNING, r, |
2467 | LOG_LINK_INTERFACE(link), | |
2468 | LOG_NETDEV_INTERFACE(netdev), | |
a1230ff9 | 2469 | LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", netdev->ifname)); |
326cb406 SS |
2470 | link_enter_failed(link); |
2471 | return r; | |
2472 | } | |
2473 | ||
313cefa1 | 2474 | link->enslaving++; |
326cb406 SS |
2475 | } |
2476 | ||
ef1ba606 TG |
2477 | return 0; |
2478 | } | |
2479 | ||
769d324c | 2480 | static int link_set_ipv4_forward(Link *link) { |
5a8bcb67 LP |
2481 | int r; |
2482 | ||
765afd5c | 2483 | if (!link_ipv4_forward_enabled(link)) |
15dee3f0 LP |
2484 | return 0; |
2485 | ||
765afd5c LP |
2486 | /* We propagate the forwarding flag from one interface to the |
2487 | * global setting one way. This means: as long as at least one | |
2488 | * interface was configured at any time that had IP forwarding | |
2489 | * enabled the setting will stay on for good. We do this | |
2490 | * primarily to keep IPv4 and IPv6 packet forwarding behaviour | |
2491 | * somewhat in sync (see below). */ | |
15dee3f0 | 2492 | |
57512c89 | 2493 | r = write_string_file("/proc/sys/net/ipv4/ip_forward", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE | WRITE_STRING_FILE_DISABLE_BUFFER); |
eb3da901 | 2494 | if (r < 0) |
765afd5c | 2495 | log_link_warning_errno(link, r, "Cannot turn on IPv4 packet forwarding, ignoring: %m"); |
43c6d5ab | 2496 | |
769d324c LP |
2497 | return 0; |
2498 | } | |
2499 | ||
2500 | static int link_set_ipv6_forward(Link *link) { | |
769d324c LP |
2501 | int r; |
2502 | ||
765afd5c | 2503 | if (!link_ipv6_forward_enabled(link)) |
8add5f79 NO |
2504 | return 0; |
2505 | ||
61233823 | 2506 | /* On Linux, the IPv6 stack does not know a per-interface |
765afd5c LP |
2507 | * packet forwarding setting: either packet forwarding is on |
2508 | * for all, or off for all. We hence don't bother with a | |
2509 | * per-interface setting, but simply propagate the interface | |
2510 | * flag, if it is set, to the global flag, one-way. Note that | |
2511 | * while IPv4 would allow a per-interface flag, we expose the | |
2512 | * same behaviour there and also propagate the setting from | |
2513 | * one to all, to keep things simple (see above). */ | |
15dee3f0 | 2514 | |
57512c89 | 2515 | r = write_string_file("/proc/sys/net/ipv6/conf/all/forwarding", "1", WRITE_STRING_FILE_VERIFY_ON_FAILURE | WRITE_STRING_FILE_DISABLE_BUFFER); |
eb3da901 | 2516 | if (r < 0) |
765afd5c | 2517 | log_link_warning_errno(link, r, "Cannot configure IPv6 packet forwarding, ignoring: %m"); |
5a8bcb67 LP |
2518 | |
2519 | return 0; | |
2520 | } | |
2521 | ||
49092e22 | 2522 | static int link_set_ipv6_privacy_extensions(Link *link) { |
1f0d9695 LP |
2523 | char buf[DECIMAL_STR_MAX(unsigned) + 1]; |
2524 | IPv6PrivacyExtensions s; | |
49092e22 SS |
2525 | const char *p = NULL; |
2526 | int r; | |
2527 | ||
1f0d9695 | 2528 | s = link_ipv6_privacy_extensions(link); |
66a6bd68 | 2529 | if (s < 0) |
49092e22 SS |
2530 | return 0; |
2531 | ||
2532 | p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/use_tempaddr"); | |
66a6bd68 | 2533 | xsprintf(buf, "%u", (unsigned) link->network->ipv6_privacy_extensions); |
49092e22 | 2534 | |
57512c89 | 2535 | r = write_string_file(p, buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE | WRITE_STRING_FILE_DISABLE_BUFFER); |
eb3da901 | 2536 | if (r < 0) |
49092e22 SS |
2537 | log_link_warning_errno(link, r, "Cannot configure IPv6 privacy extension for interface: %m"); |
2538 | ||
2539 | return 0; | |
2540 | } | |
2541 | ||
4f2e437a | 2542 | static int link_set_ipv6_accept_ra(Link *link) { |
3b015d40 | 2543 | const char *p = NULL; |
4f2e437a SS |
2544 | int r; |
2545 | ||
2546 | /* Make this a NOP if IPv6 is not available */ | |
2547 | if (!socket_ipv6_is_supported()) | |
2548 | return 0; | |
2549 | ||
2550 | if (link->flags & IFF_LOOPBACK) | |
2551 | return 0; | |
2552 | ||
d68e2e59 LP |
2553 | if (!link->network) |
2554 | return 0; | |
2555 | ||
4f2e437a | 2556 | p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/accept_ra"); |
4f2e437a | 2557 | |
0053598f | 2558 | /* We handle router advertisements ourselves, tell the kernel to GTFO */ |
57512c89 | 2559 | r = write_string_file(p, "0", WRITE_STRING_FILE_VERIFY_ON_FAILURE | WRITE_STRING_FILE_DISABLE_BUFFER); |
eb3da901 | 2560 | if (r < 0) |
fe307276 | 2561 | log_link_warning_errno(link, r, "Cannot disable kernel IPv6 accept_ra for interface: %m"); |
4f2e437a SS |
2562 | |
2563 | return 0; | |
2564 | } | |
2565 | ||
8749cbcd | 2566 | static int link_set_ipv6_dad_transmits(Link *link) { |
66a6bd68 | 2567 | char buf[DECIMAL_STR_MAX(int) + 1]; |
8749cbcd SS |
2568 | const char *p = NULL; |
2569 | int r; | |
2570 | ||
2571 | /* Make this a NOP if IPv6 is not available */ | |
2572 | if (!socket_ipv6_is_supported()) | |
2573 | return 0; | |
2574 | ||
2575 | if (link->flags & IFF_LOOPBACK) | |
2576 | return 0; | |
2577 | ||
d68e2e59 LP |
2578 | if (!link->network) |
2579 | return 0; | |
2580 | ||
8749cbcd SS |
2581 | if (link->network->ipv6_dad_transmits < 0) |
2582 | return 0; | |
2583 | ||
2584 | p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/dad_transmits"); | |
66a6bd68 | 2585 | xsprintf(buf, "%i", link->network->ipv6_dad_transmits); |
8749cbcd | 2586 | |
57512c89 | 2587 | r = write_string_file(p, buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE | WRITE_STRING_FILE_DISABLE_BUFFER); |
eb3da901 | 2588 | if (r < 0) |
8749cbcd | 2589 | log_link_warning_errno(link, r, "Cannot set IPv6 dad transmits for interface: %m"); |
8749cbcd SS |
2590 | |
2591 | return 0; | |
2592 | } | |
2593 | ||
b69c3180 | 2594 | static int link_set_ipv6_hop_limit(Link *link) { |
66a6bd68 | 2595 | char buf[DECIMAL_STR_MAX(int) + 1]; |
b69c3180 SS |
2596 | const char *p = NULL; |
2597 | int r; | |
2598 | ||
2599 | /* Make this a NOP if IPv6 is not available */ | |
2600 | if (!socket_ipv6_is_supported()) | |
2601 | return 0; | |
2602 | ||
2603 | if (link->flags & IFF_LOOPBACK) | |
2604 | return 0; | |
2605 | ||
d68e2e59 LP |
2606 | if (!link->network) |
2607 | return 0; | |
2608 | ||
b69c3180 SS |
2609 | if (link->network->ipv6_hop_limit < 0) |
2610 | return 0; | |
2611 | ||
2612 | p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/hop_limit"); | |
66a6bd68 | 2613 | xsprintf(buf, "%i", link->network->ipv6_hop_limit); |
b69c3180 | 2614 | |
57512c89 | 2615 | r = write_string_file(p, buf, WRITE_STRING_FILE_VERIFY_ON_FAILURE | WRITE_STRING_FILE_DISABLE_BUFFER); |
eb3da901 | 2616 | if (r < 0) |
b69c3180 | 2617 | log_link_warning_errno(link, r, "Cannot set IPv6 hop limit for interface: %m"); |
b69c3180 SS |
2618 | |
2619 | return 0; | |
2620 | } | |
2621 | ||
11102cba SS |
2622 | static int link_set_ipv6_mtu(Link *link) { |
2623 | char buf[DECIMAL_STR_MAX(unsigned) + 1]; | |
2624 | const char *p = NULL; | |
2625 | int r; | |
2626 | ||
2627 | /* Make this a NOP if IPv6 is not available */ | |
2628 | if (!socket_ipv6_is_supported()) | |
2629 | return 0; | |
2630 | ||
2631 | if (link->flags & IFF_LOOPBACK) | |
2632 | return 0; | |
2633 | ||
2634 | if (link->network->ipv6_mtu == 0) | |
2635 | return 0; | |
2636 | ||
2637 | p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/mtu"); | |
2638 | ||
4e964aa0 | 2639 | xsprintf(buf, "%" PRIu32, link->network->ipv6_mtu); |
11102cba | 2640 | |
57512c89 | 2641 | r = write_string_file(p, buf, WRITE_STRING_FILE_DISABLE_BUFFER); |
11102cba SS |
2642 | if (r < 0) |
2643 | log_link_warning_errno(link, r, "Cannot set IPv6 MTU for interface: %m"); | |
2644 | ||
2645 | return 0; | |
2646 | } | |
2647 | ||
30226d27 TJ |
2648 | static bool link_is_static_address_configured(Link *link, Address *address) { |
2649 | Address *net_address; | |
2650 | ||
2651 | assert(link); | |
2652 | assert(address); | |
2653 | ||
2654 | if (!link->network) | |
2655 | return false; | |
2656 | ||
2657 | LIST_FOREACH(addresses, net_address, link->network->static_addresses) | |
2658 | if (address_equal(net_address, address)) | |
2659 | return true; | |
2660 | ||
2661 | return false; | |
2662 | } | |
2663 | ||
7ecf0c3e TJ |
2664 | static bool link_is_static_route_configured(Link *link, Route *route) { |
2665 | Route *net_route; | |
2666 | ||
2667 | assert(link); | |
2668 | assert(route); | |
2669 | ||
2670 | if (!link->network) | |
2671 | return false; | |
2672 | ||
2673 | LIST_FOREACH(routes, net_route, link->network->static_routes) | |
2674 | if (route_equal(net_route, route)) | |
2675 | return true; | |
2676 | ||
2677 | return false; | |
2678 | } | |
2679 | ||
5e5b137a TG |
2680 | static int link_drop_foreign_config(Link *link) { |
2681 | Address *address; | |
2682 | Route *route; | |
2683 | Iterator i; | |
2684 | int r; | |
2685 | ||
2686 | SET_FOREACH(address, link->addresses_foreign, i) { | |
fe307276 | 2687 | /* we consider IPv6LL addresses to be managed by the kernel */ |
5e5b137a TG |
2688 | if (address->family == AF_INET6 && in_addr_is_link_local(AF_INET6, &address->in_addr) == 1) |
2689 | continue; | |
2690 | ||
30226d27 TJ |
2691 | if (link_is_static_address_configured(link, address)) { |
2692 | r = address_add(link, address->family, &address->in_addr, address->prefixlen, NULL); | |
2693 | if (r < 0) | |
2694 | return log_link_error_errno(link, r, "Failed to add address: %m"); | |
2695 | } else { | |
2696 | r = address_remove(address, link, link_address_remove_handler); | |
2697 | if (r < 0) | |
2698 | return r; | |
2699 | } | |
5e5b137a TG |
2700 | } |
2701 | ||
2702 | SET_FOREACH(route, link->routes_foreign, i) { | |
fe307276 | 2703 | /* do not touch routes managed by the kernel */ |
5e5b137a TG |
2704 | if (route->protocol == RTPROT_KERNEL) |
2705 | continue; | |
2706 | ||
7ecf0c3e TJ |
2707 | if (link_is_static_route_configured(link, route)) { |
2708 | r = route_add(link, route->family, &route->dst, route->dst_prefixlen, route->tos, route->priority, route->table, NULL); | |
2709 | if (r < 0) | |
2710 | return r; | |
2711 | } else { | |
2712 | r = route_remove(route, link, link_route_remove_handler); | |
2713 | if (r < 0) | |
2714 | return r; | |
2715 | } | |
5e5b137a TG |
2716 | } |
2717 | ||
2718 | return 0; | |
2719 | } | |
2720 | ||
3104883d | 2721 | static int link_drop_config(Link *link) { |
410a7f15 | 2722 | Address *address, *pool_address; |
3104883d SS |
2723 | Route *route; |
2724 | Iterator i; | |
2725 | int r; | |
2726 | ||
2727 | SET_FOREACH(address, link->addresses, i) { | |
2728 | /* we consider IPv6LL addresses to be managed by the kernel */ | |
2729 | if (address->family == AF_INET6 && in_addr_is_link_local(AF_INET6, &address->in_addr) == 1) | |
2730 | continue; | |
2731 | ||
2732 | r = address_remove(address, link, link_address_remove_handler); | |
2733 | if (r < 0) | |
2734 | return r; | |
410a7f15 RM |
2735 | |
2736 | /* If this address came from an address pool, clean up the pool */ | |
2737 | LIST_FOREACH(addresses, pool_address, link->pool_addresses) { | |
2738 | if (address_equal(address, pool_address)) { | |
2739 | LIST_REMOVE(addresses, link->pool_addresses, pool_address); | |
2740 | address_free(pool_address); | |
2741 | break; | |
2742 | } | |
2743 | } | |
3104883d SS |
2744 | } |
2745 | ||
2746 | SET_FOREACH(route, link->routes, i) { | |
2747 | /* do not touch routes managed by the kernel */ | |
2748 | if (route->protocol == RTPROT_KERNEL) | |
2749 | continue; | |
2750 | ||
2751 | r = route_remove(route, link, link_route_remove_handler); | |
2752 | if (r < 0) | |
2753 | return r; | |
2754 | } | |
2755 | ||
c69305ff LP |
2756 | ndisc_flush(link); |
2757 | ||
3104883d SS |
2758 | return 0; |
2759 | } | |
2760 | ||
273eec24 LP |
2761 | static int link_update_lldp(Link *link) { |
2762 | int r; | |
2763 | ||
2764 | assert(link); | |
2765 | ||
2766 | if (!link->lldp) | |
2767 | return 0; | |
2768 | ||
2769 | if (link->flags & IFF_UP) { | |
2770 | r = sd_lldp_start(link->lldp); | |
2771 | if (r > 0) | |
2772 | log_link_debug(link, "Started LLDP."); | |
2773 | } else { | |
2774 | r = sd_lldp_stop(link->lldp); | |
2775 | if (r > 0) | |
2776 | log_link_debug(link, "Stopped LLDP."); | |
2777 | } | |
2778 | ||
2779 | return r; | |
2780 | } | |
2781 | ||
93ea7750 MKB |
2782 | static int link_configure_can(Link *link) { |
2783 | int r; | |
2784 | ||
06828bb6 HP |
2785 | if (streq_ptr(link->kind, "can")) { |
2786 | /* The CAN interface must be down to configure bitrate, etc... */ | |
2787 | if ((link->flags & IFF_UP)) { | |
2788 | r = link_down(link); | |
2789 | if (r < 0) { | |
2790 | link_enter_failed(link); | |
2791 | return r; | |
2792 | } | |
2793 | ||
2794 | return 0; | |
2795 | } | |
2796 | ||
2797 | return link_set_can(link); | |
2798 | } | |
2799 | ||
93ea7750 MKB |
2800 | if (!(link->flags & IFF_UP)) { |
2801 | r = link_up_can(link); | |
2802 | if (r < 0) { | |
2803 | link_enter_failed(link); | |
2804 | return r; | |
2805 | } | |
2806 | } | |
2807 | ||
2808 | return 0; | |
2809 | } | |
2810 | ||
a748b692 | 2811 | static int link_configure(Link *link) { |
02b59d57 TG |
2812 | int r; |
2813 | ||
ef1ba606 | 2814 | assert(link); |
b22d8a00 | 2815 | assert(link->network); |
8434fd5c | 2816 | assert(link->state == LINK_STATE_PENDING); |
a748b692 | 2817 | |
06828bb6 | 2818 | if (STRPTR_IN_SET(link->kind, "can", "vcan")) |
93ea7750 | 2819 | return link_configure_can(link); |
92c918b0 | 2820 | |
02e28621 ДГ |
2821 | /* Drop foreign config, but ignore loopback or critical devices. |
2822 | * We do not want to remove loopback address or addresses used for root NFS. */ | |
2823 | if (!(link->flags & IFF_LOOPBACK) && !(link->network->dhcp_critical)) { | |
e5d44b34 CH |
2824 | r = link_drop_foreign_config(link); |
2825 | if (r < 0) | |
2826 | return r; | |
2827 | } | |
5e5b137a | 2828 | |
23d8b221 SS |
2829 | r = link_set_proxy_arp(link); |
2830 | if (r < 0) | |
2831 | return r; | |
2832 | ||
a0e5c15d FK |
2833 | r = ipv6_proxy_ndp_addresses_configure(link); |
2834 | if (r < 0) | |
2835 | return r; | |
2836 | ||
769d324c LP |
2837 | r = link_set_ipv4_forward(link); |
2838 | if (r < 0) | |
2839 | return r; | |
2840 | ||
2841 | r = link_set_ipv6_forward(link); | |
5a8bcb67 LP |
2842 | if (r < 0) |
2843 | return r; | |
2844 | ||
49092e22 SS |
2845 | r = link_set_ipv6_privacy_extensions(link); |
2846 | if (r < 0) | |
2847 | return r; | |
2848 | ||
4f2e437a SS |
2849 | r = link_set_ipv6_accept_ra(link); |
2850 | if (r < 0) | |
2851 | return r; | |
2852 | ||
8749cbcd SS |
2853 | r = link_set_ipv6_dad_transmits(link); |
2854 | if (r < 0) | |
2855 | return r; | |
b69c3180 SS |
2856 | |
2857 | r = link_set_ipv6_hop_limit(link); | |
2858 | if (r < 0) | |
2859 | return r; | |
8749cbcd | 2860 | |
99d2baa2 SS |
2861 | r = link_set_flags(link); |
2862 | if (r < 0) | |
2863 | return r; | |
2864 | ||
11102cba SS |
2865 | r = link_set_ipv6_mtu(link); |
2866 | if (r < 0) | |
2867 | return r; | |
2868 | ||
78c958f8 | 2869 | if (link_ipv4ll_enabled(link)) { |
b22d8a00 | 2870 | r = ipv4ll_configure(link); |
eb34d4af TG |
2871 | if (r < 0) |
2872 | return r; | |
2873 | } | |
2874 | ||
78c958f8 | 2875 | if (link_dhcp4_enabled(link)) { |
64b21ece MV |
2876 | r = dhcp4_set_promote_secondaries(link); |
2877 | if (r < 0) | |
2878 | return r; | |
2879 | ||
3c9b8860 | 2880 | r = dhcp4_configure(link); |
eb34d4af TG |
2881 | if (r < 0) |
2882 | return r; | |
eb34d4af TG |
2883 | } |
2884 | ||
78c958f8 | 2885 | if (link_dhcp4_server_enabled(link)) { |
dd43110f TG |
2886 | r = sd_dhcp_server_new(&link->dhcp_server, link->ifindex); |
2887 | if (r < 0) | |
2888 | return r; | |
2889 | ||
2890 | r = sd_dhcp_server_attach_event(link->dhcp_server, NULL, 0); | |
2891 | if (r < 0) | |
2892 | return r; | |
dd43110f TG |
2893 | } |
2894 | ||
62379e88 TG |
2895 | if (link_dhcp6_enabled(link) || |
2896 | link_ipv6_accept_ra_enabled(link)) { | |
f5a8c43f TG |
2897 | r = dhcp6_configure(link); |
2898 | if (r < 0) | |
2899 | return r; | |
2900 | } | |
2901 | ||
2902 | if (link_ipv6_accept_ra_enabled(link)) { | |
de1e9928 | 2903 | r = ndisc_configure(link); |
4138fb2c PF |
2904 | if (r < 0) |
2905 | return r; | |
2906 | } | |
2907 | ||
7465dd22 PF |
2908 | if (link_radv_enabled(link)) { |
2909 | r = radv_configure(link); | |
2910 | if (r < 0) | |
2911 | return r; | |
2912 | } | |
2913 | ||
8e1ad1ea | 2914 | if (link_lldp_rx_enabled(link)) { |
fc6a313b LP |
2915 | r = sd_lldp_new(&link->lldp); |
2916 | if (r < 0) | |
2917 | return r; | |
2918 | ||
2919 | r = sd_lldp_set_ifindex(link->lldp, link->ifindex); | |
34437b4f LP |
2920 | if (r < 0) |
2921 | return r; | |
2922 | ||
2923 | r = sd_lldp_match_capabilities(link->lldp, | |
2924 | link->network->lldp_mode == LLDP_MODE_ROUTERS_ONLY ? | |
6afa6767 BG |
2925 | SD_LLDP_SYSTEM_CAPABILITIES_ALL_ROUTERS : |
2926 | SD_LLDP_SYSTEM_CAPABILITIES_ALL); | |
ce43e484 SS |
2927 | if (r < 0) |
2928 | return r; | |
2929 | ||
b553a6b1 LP |
2930 | r = sd_lldp_set_filter_address(link->lldp, &link->mac); |
2931 | if (r < 0) | |
2932 | return r; | |
2933 | ||
ce43e484 SS |
2934 | r = sd_lldp_attach_event(link->lldp, NULL, 0); |
2935 | if (r < 0) | |
2936 | return r; | |
49699bac | 2937 | |
032b27f5 | 2938 | r = sd_lldp_set_callback(link->lldp, lldp_handler, link); |
49699bac SS |
2939 | if (r < 0) |
2940 | return r; | |
273eec24 LP |
2941 | |
2942 | r = link_update_lldp(link); | |
2943 | if (r < 0) | |
2944 | return r; | |
ce43e484 SS |
2945 | } |
2946 | ||
44b598a1 SS |
2947 | if (link->network->mtu > 0) { |
2948 | r = link_set_mtu(link, link->network->mtu); | |
2949 | if (r < 0) | |
2950 | return r; | |
2951 | } | |
2952 | ||
55dc8c4a YW |
2953 | return link_configure_after_setting_mtu(link); |
2954 | } | |
2955 | ||
2956 | static int link_configure_after_setting_mtu(Link *link) { | |
2957 | int r; | |
2958 | ||
2959 | assert(link); | |
2960 | assert(link->network); | |
2961 | assert(link->state == LINK_STATE_PENDING); | |
2962 | ||
2963 | if (link->setting_mtu) | |
2964 | return 0; | |
2965 | ||
dad2d78e | 2966 | if (link_has_carrier(link) || link->network->configure_without_carrier) { |
1e9be60b TG |
2967 | r = link_acquire_conf(link); |
2968 | if (r < 0) | |
2969 | return r; | |
cc544d5f | 2970 | } |
1e9be60b | 2971 | |
3f265037 | 2972 | return link_enter_join_netdev(link); |
505f8da7 TG |
2973 | } |
2974 | ||
27dfc982 YW |
2975 | static int duid_set_uuid(DUID *duid, sd_id128_t uuid) { |
2976 | assert(duid); | |
2977 | ||
2978 | if (duid->raw_data_len > 0) | |
2979 | return 0; | |
2980 | ||
2981 | if (duid->type != DUID_TYPE_UUID) | |
2982 | return -EINVAL; | |
2983 | ||
2984 | memcpy(&duid->raw_data, &uuid, sizeof(sd_id128_t)); | |
2985 | duid->raw_data_len = sizeof(sd_id128_t); | |
2986 | ||
2987 | return 1; | |
2988 | } | |
2989 | ||
2990 | int get_product_uuid_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { | |
2991 | Manager *manager = userdata; | |
2992 | const sd_bus_error *e; | |
2993 | const void *a; | |
2994 | size_t sz; | |
2995 | DUID *duid; | |
2996 | Link *link; | |
2997 | int r; | |
2998 | ||
2999 | assert(m); | |
3000 | assert(manager); | |
3001 | ||
3002 | e = sd_bus_message_get_error(m); | |
3003 | if (e) { | |
3004 | log_error_errno(sd_bus_error_get_errno(e), | |
ab4dd984 | 3005 | "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s", |
27dfc982 YW |
3006 | e->message); |
3007 | goto configure; | |
3008 | } | |
3009 | ||
3010 | r = sd_bus_message_read_array(m, 'y', &a, &sz); | |
3011 | if (r < 0) | |
3012 | goto configure; | |
3013 | ||
3014 | if (sz != sizeof(sd_id128_t)) { | |
ab4dd984 | 3015 | log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID."); |
27dfc982 YW |
3016 | goto configure; |
3017 | } | |
3018 | ||
3019 | memcpy(&manager->product_uuid, a, sz); | |
3020 | while ((duid = set_steal_first(manager->duids_requesting_uuid))) | |
3021 | (void) duid_set_uuid(duid, manager->product_uuid); | |
3022 | ||
3023 | manager->duids_requesting_uuid = set_free(manager->duids_requesting_uuid); | |
3024 | ||
3025 | configure: | |
3026 | while ((link = set_steal_first(manager->links_requesting_uuid))) { | |
3027 | r = link_configure(link); | |
3028 | if (r < 0) | |
3029 | log_link_error_errno(link, r, "Failed to configure link: %m"); | |
3030 | } | |
3031 | ||
3032 | manager->links_requesting_uuid = set_free(manager->links_requesting_uuid); | |
3033 | ||
3034 | /* To avoid calling GetProductUUID() bus method so frequently, set the flag below | |
3035 | * even if the method fails. */ | |
3036 | manager->has_product_uuid = true; | |
3037 | ||
3038 | return 1; | |
3039 | } | |
3040 | ||
3041 | static bool link_requires_uuid(Link *link) { | |
3042 | const DUID *duid; | |
3043 | ||
3044 | assert(link); | |
3045 | assert(link->manager); | |
3046 | assert(link->network); | |
3047 | ||
3048 | duid = link_get_duid(link); | |
3049 | if (duid->type != DUID_TYPE_UUID || duid->raw_data_len != 0) | |
3050 | return false; | |
3051 | ||
3052 | if (link_dhcp4_enabled(link) && IN_SET(link->network->dhcp_client_identifier, DHCP_CLIENT_ID_DUID, DHCP_CLIENT_ID_DUID_ONLY)) | |
3053 | return true; | |
3054 | ||
3055 | if (link_dhcp6_enabled(link) || link_ipv6_accept_ra_enabled(link)) | |
3056 | return true; | |
3057 | ||
3058 | return false; | |
3059 | } | |
3060 | ||
3061 | static int link_configure_duid(Link *link) { | |
3062 | Manager *m; | |
3063 | DUID *duid; | |
3064 | int r; | |
3065 | ||
3066 | assert(link); | |
3067 | assert(link->manager); | |
3068 | assert(link->network); | |
3069 | ||
3070 | m = link->manager; | |
3071 | duid = link_get_duid(link); | |
3072 | ||
3073 | if (!link_requires_uuid(link)) | |
3074 | return 1; | |
3075 | ||
3076 | if (m->has_product_uuid) { | |
3077 | (void) duid_set_uuid(duid, m->product_uuid); | |
3078 | return 1; | |
3079 | } | |
3080 | ||
3081 | if (!m->links_requesting_uuid) { | |
3082 | r = manager_request_product_uuid(m, link); | |
3083 | if (r < 0) { | |
3084 | if (r == -ENOMEM) | |
3085 | return r; | |
3086 | ||
ab4dd984 ZJS |
3087 | log_link_warning_errno(link, r, |
3088 | "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m"); | |
27dfc982 YW |
3089 | return 1; |
3090 | } | |
3091 | } else { | |
3092 | r = set_put(m->links_requesting_uuid, link); | |
3093 | if (r < 0) | |
3094 | return log_oom(); | |
3095 | ||
3096 | r = set_put(m->duids_requesting_uuid, duid); | |
3097 | if (r < 0) | |
3098 | return log_oom(); | |
3099 | } | |
3100 | ||
3101 | return 0; | |
3102 | } | |
3103 | ||
e6bf7774 | 3104 | static int link_initialized_and_synced(Link *link) { |
505f8da7 | 3105 | Network *network; |
505f8da7 TG |
3106 | int r; |
3107 | ||
3108 | assert(link); | |
3109 | assert(link->ifname); | |
3110 | assert(link->manager); | |
3111 | ||
8434fd5c | 3112 | if (link->state != LINK_STATE_PENDING) |
5da8149f | 3113 | return 1; |
505f8da7 | 3114 | |
6a7a4e4d | 3115 | log_link_debug(link, "Link state is up-to-date"); |
505f8da7 | 3116 | |
0d4ad91d AR |
3117 | r = link_new_bound_by_list(link); |
3118 | if (r < 0) | |
3119 | return r; | |
3120 | ||
3121 | r = link_handle_bound_by_list(link); | |
3122 | if (r < 0) | |
3123 | return r; | |
3124 | ||
c4a03a56 | 3125 | if (!link->network) { |
51517f9e | 3126 | r = network_get(link->manager, link->sd_device, link->ifname, |
c4a03a56 TG |
3127 | &link->mac, &network); |
3128 | if (r == -ENOENT) { | |
3129 | link_enter_unmanaged(link); | |
3130 | return 1; | |
a09dc546 DM |
3131 | } else if (r == 0 && network->unmanaged) { |
3132 | link_enter_unmanaged(link); | |
3133 | return 0; | |
c4a03a56 TG |
3134 | } else if (r < 0) |
3135 | return r; | |
505f8da7 | 3136 | |
c4a03a56 TG |
3137 | if (link->flags & IFF_LOOPBACK) { |
3138 | if (network->link_local != ADDRESS_FAMILY_NO) | |
3139 | log_link_debug(link, "Ignoring link-local autoconfiguration for loopback link"); | |
78c958f8 | 3140 | |
c4a03a56 TG |
3141 | if (network->dhcp != ADDRESS_FAMILY_NO) |
3142 | log_link_debug(link, "Ignoring DHCP clients for loopback link"); | |
78c958f8 | 3143 | |
c4a03a56 TG |
3144 | if (network->dhcp_server) |
3145 | log_link_debug(link, "Ignoring DHCP server for loopback link"); | |
3146 | } | |
bd2efe92 | 3147 | |
7d342c03 | 3148 | r = network_apply(network, link); |
c4a03a56 TG |
3149 | if (r < 0) |
3150 | return r; | |
3151 | } | |
505f8da7 | 3152 | |
0d4ad91d AR |
3153 | r = link_new_bound_to_list(link); |
3154 | if (r < 0) | |
3155 | return r; | |
3156 | ||
27dfc982 YW |
3157 | /* link_configure_duid() returns 0 if it requests product UUID. In that case, |
3158 | * link_configure() is called later asynchronously. */ | |
3159 | r = link_configure_duid(link); | |
3160 | if (r <= 0) | |
3161 | return r; | |
3162 | ||
a748b692 TG |
3163 | r = link_configure(link); |
3164 | if (r < 0) | |
3165 | return r; | |
3166 | ||
5da8149f | 3167 | return 1; |
505f8da7 TG |
3168 | } |
3169 | ||
e6bf7774 YW |
3170 | static int link_initialized_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { |
3171 | (void) link_initialized_and_synced(userdata); | |
3172 | return 1; | |
3173 | } | |
3174 | ||
51517f9e | 3175 | int link_initialized(Link *link, sd_device *device) { |
4afd3348 | 3176 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
4f561e8e TG |
3177 | int r; |
3178 | ||
3179 | assert(link); | |
3180 | assert(link->manager); | |
3181 | assert(link->manager->rtnl); | |
3182 | assert(device); | |
3183 | ||
8434fd5c | 3184 | if (link->state != LINK_STATE_PENDING) |
4f561e8e TG |
3185 | return 0; |
3186 | ||
51517f9e | 3187 | if (link->sd_device) |
679b3605 TG |
3188 | return 0; |
3189 | ||
79008bdd | 3190 | log_link_debug(link, "udev initialized link"); |
4f561e8e | 3191 | |
51517f9e | 3192 | link->sd_device = sd_device_ref(device); |
4f561e8e | 3193 | |
3c9b8860 TG |
3194 | /* udev has initialized the link, but we don't know if we have yet |
3195 | * processed the NEWLINK messages with the latest state. Do a GETLINK, | |
3196 | * when it returns we know that the pending NEWLINKs have already been | |
3197 | * processed and that we are up-to-date */ | |
4f561e8e | 3198 | |
3c9b8860 TG |
3199 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK, |
3200 | link->ifindex); | |
4f561e8e TG |
3201 | if (r < 0) |
3202 | return r; | |
3203 | ||
ee38400b | 3204 | r = sd_netlink_call_async(link->manager->rtnl, NULL, req, link_initialized_handler, |
8190a388 | 3205 | link_netlink_destroy_callback, link, 0, __func__); |
4f561e8e TG |
3206 | if (r < 0) |
3207 | return r; | |
3208 | ||
5da8149f TG |
3209 | link_ref(link); |
3210 | ||
4f561e8e TG |
3211 | return 0; |
3212 | } | |
3213 | ||
c4a03a56 | 3214 | static int link_load(Link *link) { |
0bc70f1d TG |
3215 | _cleanup_free_ char *network_file = NULL, |
3216 | *addresses = NULL, | |
f703cc2c | 3217 | *routes = NULL, |
0bc70f1d TG |
3218 | *dhcp4_address = NULL, |
3219 | *ipv4ll_address = NULL; | |
3220 | union in_addr_union address; | |
f703cc2c | 3221 | union in_addr_union route_dst; |
c598ac76 | 3222 | const char *p; |
c4a03a56 TG |
3223 | int r; |
3224 | ||
3225 | assert(link); | |
3226 | ||
aa8fbc74 | 3227 | r = parse_env_file(NULL, link->state_file, |
c4a03a56 TG |
3228 | "NETWORK_FILE", &network_file, |
3229 | "ADDRESSES", &addresses, | |
f703cc2c | 3230 | "ROUTES", &routes, |
0bc70f1d | 3231 | "DHCP4_ADDRESS", &dhcp4_address, |
13df9c39 | 3232 | "IPV4LL_ADDRESS", &ipv4ll_address); |
c4a03a56 TG |
3233 | if (r < 0 && r != -ENOENT) |
3234 | return log_link_error_errno(link, r, "Failed to read %s: %m", link->state_file); | |
3235 | ||
3236 | if (network_file) { | |
3237 | Network *network; | |
3238 | char *suffix; | |
3239 | ||
3240 | /* drop suffix */ | |
3241 | suffix = strrchr(network_file, '.'); | |
3242 | if (!suffix) { | |
3243 | log_link_debug(link, "Failed to get network name from %s", network_file); | |
3244 | goto network_file_fail; | |
3245 | } | |
3246 | *suffix = '\0'; | |
3247 | ||
3248 | r = network_get_by_name(link->manager, basename(network_file), &network); | |
3249 | if (r < 0) { | |
3250 | log_link_debug_errno(link, r, "Failed to get network %s: %m", basename(network_file)); | |
3251 | goto network_file_fail; | |
3252 | } | |
3253 | ||
7d342c03 | 3254 | r = network_apply(network, link); |
c4a03a56 TG |
3255 | if (r < 0) |
3256 | return log_link_error_errno(link, r, "Failed to apply network %s: %m", basename(network_file)); | |
3257 | } | |
3258 | ||
3259 | network_file_fail: | |
3260 | ||
3261 | if (addresses) { | |
c598ac76 | 3262 | p = addresses; |
c4a03a56 | 3263 | |
c598ac76 TG |
3264 | for (;;) { |
3265 | _cleanup_free_ char *address_str = NULL; | |
c4a03a56 TG |
3266 | char *prefixlen_str; |
3267 | int family; | |
3268 | unsigned char prefixlen; | |
c4a03a56 | 3269 | |
c598ac76 TG |
3270 | r = extract_first_word(&p, &address_str, NULL, 0); |
3271 | if (r < 0) { | |
3272 | log_link_debug_errno(link, r, "Failed to extract next address string: %m"); | |
3273 | continue; | |
928bc597 TA |
3274 | } |
3275 | if (r == 0) | |
c598ac76 TG |
3276 | break; |
3277 | ||
3278 | prefixlen_str = strchr(address_str, '/'); | |
c4a03a56 | 3279 | if (!prefixlen_str) { |
c598ac76 | 3280 | log_link_debug(link, "Failed to parse address and prefix length %s", address_str); |
c4a03a56 TG |
3281 | continue; |
3282 | } | |
3283 | ||
313cefa1 | 3284 | *prefixlen_str++ = '\0'; |
c4a03a56 TG |
3285 | |
3286 | r = sscanf(prefixlen_str, "%hhu", &prefixlen); | |
3287 | if (r != 1) { | |
3288 | log_link_error(link, "Failed to parse prefixlen %s", prefixlen_str); | |
3289 | continue; | |
3290 | } | |
3291 | ||
c598ac76 | 3292 | r = in_addr_from_string_auto(address_str, &family, &address); |
c4a03a56 | 3293 | if (r < 0) { |
c598ac76 | 3294 | log_link_debug_errno(link, r, "Failed to parse address %s: %m", address_str); |
c4a03a56 TG |
3295 | continue; |
3296 | } | |
3297 | ||
3298 | r = address_add(link, family, &address, prefixlen, NULL); | |
3299 | if (r < 0) | |
3300 | return log_link_error_errno(link, r, "Failed to add address: %m"); | |
3301 | } | |
3302 | } | |
3303 | ||
f703cc2c | 3304 | if (routes) { |
74544b4e TA |
3305 | p = routes; |
3306 | ||
c598ac76 | 3307 | for (;;) { |
f833694d | 3308 | Route *route; |
c598ac76 | 3309 | _cleanup_free_ char *route_str = NULL; |
4afd3348 | 3310 | _cleanup_(sd_event_source_unrefp) sd_event_source *expire = NULL; |
f833694d | 3311 | usec_t lifetime; |
f703cc2c TG |
3312 | char *prefixlen_str; |
3313 | int family; | |
3314 | unsigned char prefixlen, tos, table; | |
3315 | uint32_t priority; | |
3316 | ||
c598ac76 TG |
3317 | r = extract_first_word(&p, &route_str, NULL, 0); |
3318 | if (r < 0) { | |
3319 | log_link_debug_errno(link, r, "Failed to extract next route string: %m"); | |
3320 | continue; | |
928bc597 TA |
3321 | } |
3322 | if (r == 0) | |
c598ac76 TG |
3323 | break; |
3324 | ||
3325 | prefixlen_str = strchr(route_str, '/'); | |
f703cc2c | 3326 | if (!prefixlen_str) { |
c598ac76 | 3327 | log_link_debug(link, "Failed to parse route %s", route_str); |
f703cc2c TG |
3328 | continue; |
3329 | } | |
3330 | ||
313cefa1 | 3331 | *prefixlen_str++ = '\0'; |
f703cc2c | 3332 | |
f833694d TG |
3333 | r = sscanf(prefixlen_str, "%hhu/%hhu/%"SCNu32"/%hhu/"USEC_FMT, &prefixlen, &tos, &priority, &table, &lifetime); |
3334 | if (r != 5) { | |
3335 | log_link_debug(link, | |
3336 | "Failed to parse destination prefix length, tos, priority, table or expiration %s", | |
3337 | prefixlen_str); | |
f703cc2c TG |
3338 | continue; |
3339 | } | |
3340 | ||
c598ac76 | 3341 | r = in_addr_from_string_auto(route_str, &family, &route_dst); |
f703cc2c | 3342 | if (r < 0) { |
c598ac76 | 3343 | log_link_debug_errno(link, r, "Failed to parse route destination %s: %m", route_str); |
f703cc2c TG |
3344 | continue; |
3345 | } | |
3346 | ||
f833694d | 3347 | r = route_add(link, family, &route_dst, prefixlen, tos, priority, table, &route); |
f703cc2c TG |
3348 | if (r < 0) |
3349 | return log_link_error_errno(link, r, "Failed to add route: %m"); | |
f833694d | 3350 | |
f02ba163 | 3351 | if (lifetime != USEC_INFINITY && !kernel_route_expiration_supported()) { |
f833694d TG |
3352 | r = sd_event_add_time(link->manager->event, &expire, clock_boottime_or_monotonic(), lifetime, |
3353 | 0, route_expire_handler, route); | |
3354 | if (r < 0) | |
3355 | log_link_warning_errno(link, r, "Could not arm route expiration handler: %m"); | |
3356 | } | |
3357 | ||
3358 | route->lifetime = lifetime; | |
3359 | sd_event_source_unref(route->expire); | |
1cc6c93a | 3360 | route->expire = TAKE_PTR(expire); |
f703cc2c TG |
3361 | } |
3362 | } | |
3363 | ||
0bc70f1d TG |
3364 | if (dhcp4_address) { |
3365 | r = in_addr_from_string(AF_INET, dhcp4_address, &address); | |
3366 | if (r < 0) { | |
b68d26b8 | 3367 | log_link_debug_errno(link, r, "Failed to parse DHCPv4 address %s: %m", dhcp4_address); |
0bc70f1d TG |
3368 | goto dhcp4_address_fail; |
3369 | } | |
3370 | ||
b1c626f6 | 3371 | r = sd_dhcp_client_new(&link->dhcp_client, link->network ? link->network->dhcp_anonymize : 0); |
0bc70f1d | 3372 | if (r < 0) |
b68d26b8 | 3373 | return log_link_error_errno(link, r, "Failed to create DHCPv4 client: %m"); |
0bc70f1d TG |
3374 | |
3375 | r = sd_dhcp_client_set_request_address(link->dhcp_client, &address.in); | |
3376 | if (r < 0) | |
b68d26b8 | 3377 | return log_link_error_errno(link, r, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address); |
0bc70f1d TG |
3378 | } |
3379 | ||
3380 | dhcp4_address_fail: | |
3381 | ||
3382 | if (ipv4ll_address) { | |
3383 | r = in_addr_from_string(AF_INET, ipv4ll_address, &address); | |
3384 | if (r < 0) { | |
b68d26b8 | 3385 | log_link_debug_errno(link, r, "Failed to parse IPv4LL address %s: %m", ipv4ll_address); |
0bc70f1d TG |
3386 | goto ipv4ll_address_fail; |
3387 | } | |
3388 | ||
3389 | r = sd_ipv4ll_new(&link->ipv4ll); | |
3390 | if (r < 0) | |
b68d26b8 | 3391 | return log_link_error_errno(link, r, "Failed to create IPv4LL client: %m"); |
0bc70f1d TG |
3392 | |
3393 | r = sd_ipv4ll_set_address(link->ipv4ll, &address.in); | |
3394 | if (r < 0) | |
b68d26b8 | 3395 | return log_link_error_errno(link, r, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address); |
0bc70f1d TG |
3396 | } |
3397 | ||
3398 | ipv4ll_address_fail: | |
3399 | ||
c4a03a56 TG |
3400 | return 0; |
3401 | } | |
3402 | ||
1c4baffc | 3403 | int link_add(Manager *m, sd_netlink_message *message, Link **ret) { |
51517f9e | 3404 | _cleanup_(sd_device_unrefp) sd_device *device = NULL; |
505f8da7 | 3405 | char ifindex_str[2 + DECIMAL_STR_MAX(int)]; |
51517f9e | 3406 | Link *link; |
5a937ea2 | 3407 | int r; |
505f8da7 TG |
3408 | |
3409 | assert(m); | |
fbbeb65a | 3410 | assert(m->rtnl); |
505f8da7 TG |
3411 | assert(message); |
3412 | assert(ret); | |
3413 | ||
3414 | r = link_new(m, message, ret); | |
3415 | if (r < 0) | |
3416 | return r; | |
3417 | ||
3418 | link = *ret; | |
3419 | ||
6a7a4e4d | 3420 | log_link_debug(link, "Link %d added", link->ifindex); |
505f8da7 | 3421 | |
c4a03a56 TG |
3422 | r = link_load(link); |
3423 | if (r < 0) | |
3424 | return r; | |
3425 | ||
75f86906 | 3426 | if (detect_container() <= 0) { |
505f8da7 | 3427 | /* not in a container, udev will be around */ |
ae06ab10 | 3428 | sprintf(ifindex_str, "n%d", link->ifindex); |
51517f9e YW |
3429 | r = sd_device_new_from_device_id(&device, ifindex_str); |
3430 | if (r < 0) { | |
3431 | log_link_warning_errno(link, r, "Could not find device: %m"); | |
5c416fc4 TG |
3432 | goto failed; |
3433 | } | |
505f8da7 | 3434 | |
5a937ea2 | 3435 | r = sd_device_get_is_initialized(device); |
51517f9e YW |
3436 | if (r < 0) { |
3437 | log_link_warning_errno(link, r, "Could not determine whether the device is initialized or not: %m"); | |
3438 | goto failed; | |
3439 | } | |
5a937ea2 | 3440 | if (r == 0) { |
505f8da7 | 3441 | /* not yet ready */ |
79008bdd | 3442 | log_link_debug(link, "link pending udev initialization..."); |
505f8da7 | 3443 | return 0; |
3c4cb064 | 3444 | } |
505f8da7 | 3445 | |
4f561e8e TG |
3446 | r = link_initialized(link, device); |
3447 | if (r < 0) | |
5c416fc4 | 3448 | goto failed; |
4f561e8e | 3449 | } else { |
e6bf7774 | 3450 | r = link_initialized_and_synced(link); |
4f561e8e | 3451 | if (r < 0) |
5c416fc4 | 3452 | goto failed; |
4f561e8e | 3453 | } |
505f8da7 | 3454 | |
a748b692 | 3455 | return 0; |
5c416fc4 TG |
3456 | failed: |
3457 | link_enter_failed(link); | |
3458 | return r; | |
a748b692 TG |
3459 | } |
3460 | ||
c601ebf7 | 3461 | int link_ipv6ll_gained(Link *link, const struct in6_addr *address) { |
e7ab854c TG |
3462 | int r; |
3463 | ||
3464 | assert(link); | |
3465 | ||
3466 | log_link_info(link, "Gained IPv6LL"); | |
3467 | ||
c601ebf7 | 3468 | link->ipv6ll_address = *address; |
e7ab854c TG |
3469 | link_check_ready(link); |
3470 | ||
201f0c91 | 3471 | if (!IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_UNMANAGED, LINK_STATE_FAILED)) { |
e7ab854c TG |
3472 | r = link_acquire_ipv6_conf(link); |
3473 | if (r < 0) { | |
3474 | link_enter_failed(link); | |
3475 | return r; | |
3476 | } | |
3477 | } | |
3478 | ||
3479 | return 0; | |
3480 | } | |
3481 | ||
9c0a72f9 TG |
3482 | static int link_carrier_gained(Link *link) { |
3483 | int r; | |
3484 | ||
3485 | assert(link); | |
3486 | ||
201f0c91 | 3487 | if (!IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_UNMANAGED, LINK_STATE_FAILED)) { |
9c0a72f9 TG |
3488 | r = link_acquire_conf(link); |
3489 | if (r < 0) { | |
3490 | link_enter_failed(link); | |
3491 | return r; | |
3492 | } | |
6fc25497 SS |
3493 | |
3494 | r = link_enter_set_addresses(link); | |
3495 | if (r < 0) | |
3496 | return r; | |
9c0a72f9 TG |
3497 | } |
3498 | ||
0d4ad91d AR |
3499 | r = link_handle_bound_by_list(link); |
3500 | if (r < 0) | |
3501 | return r; | |
3502 | ||
9c0a72f9 TG |
3503 | return 0; |
3504 | } | |
3505 | ||
3506 | static int link_carrier_lost(Link *link) { | |
3507 | int r; | |
3508 | ||
3509 | assert(link); | |
3510 | ||
fbdb6605 | 3511 | /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop. |
55dc8c4a YW |
3512 | * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the |
3513 | * configuration and stop the clients as well. */ | |
97e7fb39 SS |
3514 | if (link->setting_mtu) |
3515 | return 0; | |
3516 | ||
9c0a72f9 TG |
3517 | r = link_stop_clients(link); |
3518 | if (r < 0) { | |
3519 | link_enter_failed(link); | |
3520 | return r; | |
3521 | } | |
3522 | ||
28464ae0 SS |
3523 | if (link_dhcp4_server_enabled(link)) |
3524 | (void) sd_dhcp_server_stop(link->dhcp_server); | |
45a9eac9 | 3525 | |
3104883d SS |
3526 | r = link_drop_config(link); |
3527 | if (r < 0) | |
3528 | return r; | |
3529 | ||
f258e948 MP |
3530 | if (!IN_SET(link->state, LINK_STATE_UNMANAGED, LINK_STATE_PENDING)) { |
3531 | log_link_debug(link, "State is %s, dropping config", link_state_to_string(link->state)); | |
c436d553 MM |
3532 | r = link_drop_foreign_config(link); |
3533 | if (r < 0) | |
3534 | return r; | |
3535 | } | |
3104883d | 3536 | |
0d4ad91d AR |
3537 | r = link_handle_bound_by_list(link); |
3538 | if (r < 0) | |
3539 | return r; | |
3540 | ||
9c0a72f9 TG |
3541 | return 0; |
3542 | } | |
3543 | ||
3544 | int link_carrier_reset(Link *link) { | |
3545 | int r; | |
3546 | ||
3547 | assert(link); | |
3548 | ||
3549 | if (link_has_carrier(link)) { | |
3550 | r = link_carrier_lost(link); | |
3551 | if (r < 0) | |
3552 | return r; | |
3553 | ||
3554 | r = link_carrier_gained(link); | |
3555 | if (r < 0) | |
3556 | return r; | |
3557 | ||
6a7a4e4d | 3558 | log_link_info(link, "Reset carrier"); |
9c0a72f9 TG |
3559 | } |
3560 | ||
3561 | return 0; | |
3562 | } | |
3563 | ||
1c4baffc | 3564 | int link_update(Link *link, sd_netlink_message *m) { |
c49b33ac | 3565 | struct ether_addr mac; |
ca4e095a | 3566 | const char *ifname; |
afe7fd56 | 3567 | uint32_t mtu; |
a61bb41c | 3568 | bool had_carrier, carrier_gained, carrier_lost; |
22936833 TG |
3569 | int r; |
3570 | ||
dd3efc09 | 3571 | assert(link); |
b8941f74 | 3572 | assert(link->ifname); |
22936833 TG |
3573 | assert(m); |
3574 | ||
7619683b | 3575 | if (link->state == LINK_STATE_LINGER) { |
6a7a4e4d | 3576 | log_link_info(link, "Link readded"); |
e331e246 | 3577 | link_set_state(link, LINK_STATE_ENSLAVING); |
0d4ad91d AR |
3578 | |
3579 | r = link_new_carrier_maps(link); | |
3580 | if (r < 0) | |
3581 | return r; | |
7619683b TG |
3582 | } |
3583 | ||
1c4baffc | 3584 | r = sd_netlink_message_read_string(m, IFLA_IFNAME, &ifname); |
b8941f74 | 3585 | if (r >= 0 && !streq(ifname, link->ifname)) { |
aa2b10da | 3586 | log_link_info(link, "Interface name change detected, %s has been renamed to %s.", link->ifname, ifname); |
b8941f74 | 3587 | |
587c4586 YW |
3588 | if (link->state == LINK_STATE_PENDING) { |
3589 | r = free_and_strdup(&link->ifname, ifname); | |
3590 | if (r < 0) | |
3591 | return r; | |
3592 | } else { | |
3593 | Manager *manager = link->manager; | |
0d4ad91d | 3594 | |
587c4586 YW |
3595 | link_drop(link); |
3596 | r = link_add(manager, m, &link); | |
3597 | if (r < 0) | |
3598 | return r; | |
3599 | } | |
b8941f74 TG |
3600 | } |
3601 | ||
1c4baffc | 3602 | r = sd_netlink_message_read_u32(m, IFLA_MTU, &mtu); |
afe7fd56 TG |
3603 | if (r >= 0 && mtu > 0) { |
3604 | link->mtu = mtu; | |
4e964aa0 | 3605 | if (link->original_mtu == 0) { |
afe7fd56 | 3606 | link->original_mtu = mtu; |
6a7a4e4d | 3607 | log_link_debug(link, "Saved original MTU: %" PRIu32, link->original_mtu); |
afe7fd56 TG |
3608 | } |
3609 | ||
3610 | if (link->dhcp_client) { | |
3c9b8860 TG |
3611 | r = sd_dhcp_client_set_mtu(link->dhcp_client, |
3612 | link->mtu); | |
fc95c359 YW |
3613 | if (r < 0) |
3614 | return log_link_warning_errno(link, r, "Could not update MTU in DHCP client: %m"); | |
afe7fd56 | 3615 | } |
7465dd22 PF |
3616 | |
3617 | if (link->radv) { | |
3618 | r = sd_radv_set_mtu(link->radv, link->mtu); | |
3619 | if (r < 0) | |
3620 | return log_link_warning_errno(link, r, "Could not set MTU for Router Advertisement: %m"); | |
3621 | } | |
9842de0d | 3622 | } |
69629de9 | 3623 | |
e9189a1f TG |
3624 | /* The kernel may broadcast NEWLINK messages without the MAC address |
3625 | set, simply ignore them. */ | |
1c4baffc | 3626 | r = sd_netlink_message_read_ether_addr(m, IFLA_ADDRESS, &mac); |
e9189a1f | 3627 | if (r >= 0) { |
3c9b8860 TG |
3628 | if (memcmp(link->mac.ether_addr_octet, mac.ether_addr_octet, |
3629 | ETH_ALEN)) { | |
c49b33ac | 3630 | |
3c9b8860 TG |
3631 | memcpy(link->mac.ether_addr_octet, mac.ether_addr_octet, |
3632 | ETH_ALEN); | |
c49b33ac | 3633 | |
79008bdd | 3634 | log_link_debug(link, "MAC address: " |
20861203 TG |
3635 | "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", |
3636 | mac.ether_addr_octet[0], | |
3637 | mac.ether_addr_octet[1], | |
3638 | mac.ether_addr_octet[2], | |
3639 | mac.ether_addr_octet[3], | |
3640 | mac.ether_addr_octet[4], | |
3641 | mac.ether_addr_octet[5]); | |
c49b33ac | 3642 | |
20861203 TG |
3643 | if (link->ipv4ll) { |
3644 | r = sd_ipv4ll_set_mac(link->ipv4ll, &link->mac); | |
6a7a4e4d LP |
3645 | if (r < 0) |
3646 | return log_link_warning_errno(link, r, "Could not update MAC address in IPv4LL client: %m"); | |
c49b33ac | 3647 | } |
c49b33ac | 3648 | |
20861203 | 3649 | if (link->dhcp_client) { |
3c9b8860 | 3650 | r = sd_dhcp_client_set_mac(link->dhcp_client, |
76253e73 DW |
3651 | (const uint8_t *) &link->mac, |
3652 | sizeof (link->mac), | |
3653 | ARPHRD_ETHER); | |
6a7a4e4d LP |
3654 | if (r < 0) |
3655 | return log_link_warning_errno(link, r, "Could not update MAC address in DHCP client: %m"); | |
413708d1 | 3656 | |
fff1f40c YW |
3657 | r = dhcp4_set_client_identifier(link); |
3658 | if (r < 0) | |
3659 | return r; | |
c49b33ac | 3660 | } |
4138fb2c PF |
3661 | |
3662 | if (link->dhcp6_client) { | |
f24648a6 | 3663 | const DUID* duid = link_get_duid(link); |
8341a5c3 | 3664 | |
4138fb2c | 3665 | r = sd_dhcp6_client_set_mac(link->dhcp6_client, |
76253e73 DW |
3666 | (const uint8_t *) &link->mac, |
3667 | sizeof (link->mac), | |
3668 | ARPHRD_ETHER); | |
6a7a4e4d LP |
3669 | if (r < 0) |
3670 | return log_link_warning_errno(link, r, "Could not update MAC address in DHCPv6 client: %m"); | |
413708d1 VK |
3671 | |
3672 | r = sd_dhcp6_client_set_iaid(link->dhcp6_client, | |
3673 | link->network->iaid); | |
3674 | if (r < 0) | |
3675 | return log_link_warning_errno(link, r, "Could not update DHCPv6 IAID: %m"); | |
3676 | ||
8341a5c3 ZJS |
3677 | r = sd_dhcp6_client_set_duid(link->dhcp6_client, |
3678 | duid->type, | |
3679 | duid->raw_data_len > 0 ? duid->raw_data : NULL, | |
3680 | duid->raw_data_len); | |
413708d1 VK |
3681 | if (r < 0) |
3682 | return log_link_warning_errno(link, r, "Could not update DHCPv6 DUID: %m"); | |
4138fb2c | 3683 | } |
7465dd22 PF |
3684 | |
3685 | if (link->radv) { | |
3686 | r = sd_radv_set_mac(link->radv, &link->mac); | |
3687 | if (r < 0) | |
3688 | return log_link_warning_errno(link, r, "Could not update MAC for Router Advertisement: %m"); | |
3689 | } | |
420d2058 SS |
3690 | |
3691 | if (link->ndisc) { | |
3692 | r = sd_ndisc_set_mac(link->ndisc, &link->mac); | |
3693 | if (r < 0) | |
3694 | return log_link_warning_errno(link, r, "Could not update MAC for ndisc: %m"); | |
3695 | } | |
c49b33ac | 3696 | } |
4f882b2a TG |
3697 | } |
3698 | ||
a61bb41c TG |
3699 | had_carrier = link_has_carrier(link); |
3700 | ||
3701 | r = link_update_flags(link, m); | |
3702 | if (r < 0) | |
3703 | return r; | |
3704 | ||
273eec24 LP |
3705 | r = link_update_lldp(link); |
3706 | if (r < 0) | |
3707 | return r; | |
3708 | ||
a61bb41c TG |
3709 | carrier_gained = !had_carrier && link_has_carrier(link); |
3710 | carrier_lost = had_carrier && !link_has_carrier(link); | |
3711 | ||
3712 | if (carrier_gained) { | |
6a7a4e4d | 3713 | log_link_info(link, "Gained carrier"); |
a61bb41c | 3714 | |
9c0a72f9 TG |
3715 | r = link_carrier_gained(link); |
3716 | if (r < 0) | |
3717 | return r; | |
a61bb41c | 3718 | } else if (carrier_lost) { |
6a7a4e4d | 3719 | log_link_info(link, "Lost carrier"); |
a61bb41c | 3720 | |
9c0a72f9 TG |
3721 | r = link_carrier_lost(link); |
3722 | if (r < 0) | |
a61bb41c | 3723 | return r; |
a61bb41c TG |
3724 | } |
3725 | ||
3726 | return 0; | |
dd3efc09 | 3727 | } |
fe8db0c5 | 3728 | |
b295beea LP |
3729 | static void print_link_hashmap(FILE *f, const char *prefix, Hashmap* h) { |
3730 | bool space = false; | |
3731 | Iterator i; | |
3732 | Link *link; | |
3733 | ||
3734 | assert(f); | |
3735 | assert(prefix); | |
3736 | ||
3737 | if (hashmap_isempty(h)) | |
3738 | return; | |
3739 | ||
0d536673 | 3740 | fputs(prefix, f); |
b295beea LP |
3741 | HASHMAP_FOREACH(link, h, i) { |
3742 | if (space) | |
0d536673 | 3743 | fputc(' ', f); |
b295beea LP |
3744 | |
3745 | fprintf(f, "%i", link->ifindex); | |
3746 | space = true; | |
3747 | } | |
3748 | ||
0d536673 | 3749 | fputc('\n', f); |
b295beea LP |
3750 | } |
3751 | ||
fe8db0c5 | 3752 | int link_save(Link *link) { |
68a8723c | 3753 | _cleanup_free_ char *temp_path = NULL; |
fe8db0c5 | 3754 | _cleanup_fclose_ FILE *f = NULL; |
e375dcde | 3755 | const char *admin_state, *oper_state; |
e7780c8d | 3756 | Address *a; |
c1eb9872 | 3757 | Route *route; |
e7780c8d | 3758 | Iterator i; |
fe8db0c5 TG |
3759 | int r; |
3760 | ||
3761 | assert(link); | |
3762 | assert(link->state_file); | |
68a8723c | 3763 | assert(link->lease_file); |
bbf7c048 TG |
3764 | assert(link->manager); |
3765 | ||
370e9930 TG |
3766 | if (link->state == LINK_STATE_LINGER) { |
3767 | unlink(link->state_file); | |
3768 | return 0; | |
3769 | } | |
3770 | ||
34437b4f LP |
3771 | link_lldp_save(link); |
3772 | ||
deb2e523 TG |
3773 | admin_state = link_state_to_string(link->state); |
3774 | assert(admin_state); | |
3775 | ||
e375dcde TG |
3776 | oper_state = link_operstate_to_string(link->operstate); |
3777 | assert(oper_state); | |
deb2e523 | 3778 | |
fe8db0c5 TG |
3779 | r = fopen_temporary(link->state_file, &f, &temp_path); |
3780 | if (r < 0) | |
6a7a4e4d | 3781 | goto fail; |
fe8db0c5 | 3782 | |
0d536673 | 3783 | (void) __fsetlocking(f, FSETLOCKING_BYCALLER); |
5512a963 | 3784 | (void) fchmod(fileno(f), 0644); |
fe8db0c5 TG |
3785 | |
3786 | fprintf(f, | |
3787 | "# This is private data. Do not parse.\n" | |
deb2e523 | 3788 | "ADMIN_STATE=%s\n" |
6dcaa6f5 TG |
3789 | "OPER_STATE=%s\n", |
3790 | admin_state, oper_state); | |
fe8db0c5 | 3791 | |
bcb7a07e | 3792 | if (link->network) { |
ea352b40 | 3793 | bool space; |
07bdc70d | 3794 | sd_dhcp6_lease *dhcp6_lease = NULL; |
b2a81c0b LP |
3795 | const char *dhcp_domainname = NULL; |
3796 | char **dhcp6_domains = NULL; | |
b85bc551 | 3797 | char **dhcp_domains = NULL; |
5512a963 | 3798 | unsigned j; |
07bdc70d | 3799 | |
c1a38904 MTL |
3800 | fprintf(f, "REQUIRED_FOR_ONLINE=%s\n", |
3801 | yes_no(link->network->required_for_online)); | |
3802 | ||
07bdc70d | 3803 | if (link->dhcp6_client) { |
4058d339 TG |
3804 | r = sd_dhcp6_client_get_lease(link->dhcp6_client, &dhcp6_lease); |
3805 | if (r < 0 && r != -ENOMSG) | |
07bdc70d PF |
3806 | log_link_debug(link, "No DHCPv6 lease"); |
3807 | } | |
b0e39c82 | 3808 | |
adc5b2e2 TG |
3809 | fprintf(f, "NETWORK_FILE=%s\n", link->network->filename); |
3810 | ||
0d536673 | 3811 | fputs("DNS=", f); |
ea352b40 | 3812 | space = false; |
5512a963 LP |
3813 | |
3814 | for (j = 0; j < link->network->n_dns; j++) { | |
3815 | _cleanup_free_ char *b = NULL; | |
3816 | ||
3817 | r = in_addr_to_string(link->network->dns[j].family, | |
3818 | &link->network->dns[j].address, &b); | |
3819 | if (r < 0) { | |
3820 | log_debug_errno(r, "Failed to format address, ignoring: %m"); | |
3821 | continue; | |
3822 | } | |
3823 | ||
3824 | if (space) | |
0d536673 LP |
3825 | fputc(' ', f); |
3826 | fputs(b, f); | |
5512a963 LP |
3827 | space = true; |
3828 | } | |
d5314fff | 3829 | |
27cb34f5 | 3830 | if (link->network->dhcp_use_dns && |
b0e39c82 TG |
3831 | link->dhcp_lease) { |
3832 | const struct in_addr *addresses; | |
3833 | ||
3834 | r = sd_dhcp_lease_get_dns(link->dhcp_lease, &addresses); | |
3835 | if (r > 0) { | |
ea352b40 | 3836 | if (space) |
0d536673 | 3837 | fputc(' ', f); |
b0e39c82 | 3838 | serialize_in_addrs(f, addresses, r); |
07bdc70d PF |
3839 | space = true; |
3840 | } | |
3841 | } | |
3842 | ||
27cb34f5 | 3843 | if (link->network->dhcp_use_dns && dhcp6_lease) { |
07bdc70d PF |
3844 | struct in6_addr *in6_addrs; |
3845 | ||
3846 | r = sd_dhcp6_lease_get_dns(dhcp6_lease, &in6_addrs); | |
3847 | if (r > 0) { | |
3848 | if (space) | |
0d536673 | 3849 | fputc(' ', f); |
07bdc70d | 3850 | serialize_in6_addrs(f, in6_addrs, r); |
1e7a0e21 LP |
3851 | space = true; |
3852 | } | |
3853 | } | |
3854 | ||
3855 | /* Make sure to flush out old entries before we use the NDISC data */ | |
3856 | ndisc_vacuum(link); | |
3857 | ||
b296797f | 3858 | if (link->network->ipv6_accept_ra_use_dns && link->ndisc_rdnss) { |
1e7a0e21 LP |
3859 | NDiscRDNSS *dd; |
3860 | ||
3861 | SET_FOREACH(dd, link->ndisc_rdnss, i) { | |
3862 | if (space) | |
0d536673 | 3863 | fputc(' ', f); |
1e7a0e21 LP |
3864 | |
3865 | serialize_in6_addrs(f, &dd->address, 1); | |
3866 | space = true; | |
b0e39c82 TG |
3867 | } |
3868 | } | |
3869 | ||
0d536673 | 3870 | fputc('\n', f); |
b0e39c82 | 3871 | |
0d536673 | 3872 | fputs("NTP=", f); |
ea352b40 | 3873 | space = false; |
3df9bec5 | 3874 | fputstrv(f, link->network->ntp, NULL, &space); |
d5314fff | 3875 | |
27cb34f5 | 3876 | if (link->network->dhcp_use_ntp && |
b0e39c82 TG |
3877 | link->dhcp_lease) { |
3878 | const struct in_addr *addresses; | |
3879 | ||
3880 | r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &addresses); | |
3881 | if (r > 0) { | |
ea352b40 | 3882 | if (space) |
0d536673 | 3883 | fputc(' ', f); |
b0e39c82 | 3884 | serialize_in_addrs(f, addresses, r); |
07bdc70d PF |
3885 | space = true; |
3886 | } | |
3887 | } | |
3888 | ||
27cb34f5 | 3889 | if (link->network->dhcp_use_ntp && dhcp6_lease) { |
07bdc70d PF |
3890 | struct in6_addr *in6_addrs; |
3891 | char **hosts; | |
07bdc70d PF |
3892 | |
3893 | r = sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease, | |
3894 | &in6_addrs); | |
3895 | if (r > 0) { | |
3896 | if (space) | |
0d536673 | 3897 | fputc(' ', f); |
07bdc70d PF |
3898 | serialize_in6_addrs(f, in6_addrs, r); |
3899 | space = true; | |
3900 | } | |
3901 | ||
3902 | r = sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease, &hosts); | |
3df9bec5 LP |
3903 | if (r > 0) |
3904 | fputstrv(f, hosts, NULL, &space); | |
b0e39c82 TG |
3905 | } |
3906 | ||
0d536673 | 3907 | fputc('\n', f); |
bd8f6538 | 3908 | |
b2a81c0b | 3909 | if (link->network->dhcp_use_domains != DHCP_USE_DOMAINS_NO) { |
b85bc551 | 3910 | if (link->dhcp_lease) { |
b2a81c0b | 3911 | (void) sd_dhcp_lease_get_domainname(link->dhcp_lease, &dhcp_domainname); |
b85bc551 DW |
3912 | (void) sd_dhcp_lease_get_search_domains(link->dhcp_lease, &dhcp_domains); |
3913 | } | |
b2a81c0b LP |
3914 | if (dhcp6_lease) |
3915 | (void) sd_dhcp6_lease_get_domains(dhcp6_lease, &dhcp6_domains); | |
07bdc70d PF |
3916 | } |
3917 | ||
0d536673 | 3918 | fputs("DOMAINS=", f); |
b85bc551 | 3919 | space = false; |
b2a81c0b | 3920 | fputstrv(f, link->network->search_domains, NULL, &space); |
07bdc70d | 3921 | |
1e7a0e21 LP |
3922 | if (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_YES) { |
3923 | NDiscDNSSL *dd; | |
9b4d1882 | 3924 | |
1e7a0e21 LP |
3925 | if (dhcp_domainname) |
3926 | fputs_with_space(f, dhcp_domainname, NULL, &space); | |
b85bc551 DW |
3927 | if (dhcp_domains) |
3928 | fputstrv(f, dhcp_domains, NULL, &space); | |
1e7a0e21 LP |
3929 | if (dhcp6_domains) |
3930 | fputstrv(f, dhcp6_domains, NULL, &space); | |
3931 | ||
3932 | SET_FOREACH(dd, link->ndisc_dnssl, i) | |
3933 | fputs_with_space(f, NDISC_DNSSL_DOMAIN(dd), NULL, &space); | |
3934 | } | |
b2a81c0b | 3935 | |
0d536673 | 3936 | fputc('\n', f); |
6192b846 | 3937 | |
0d536673 | 3938 | fputs("ROUTE_DOMAINS=", f); |
b85bc551 DW |
3939 | space = false; |
3940 | fputstrv(f, link->network->route_domains, NULL, &space); | |
b2a81c0b | 3941 | |
1e7a0e21 LP |
3942 | if (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_ROUTE) { |
3943 | NDiscDNSSL *dd; | |
b2a81c0b | 3944 | |
1e7a0e21 LP |
3945 | if (dhcp_domainname) |
3946 | fputs_with_space(f, dhcp_domainname, NULL, &space); | |
b85bc551 DW |
3947 | if (dhcp_domains) |
3948 | fputstrv(f, dhcp_domains, NULL, &space); | |
1e7a0e21 LP |
3949 | if (dhcp6_domains) |
3950 | fputstrv(f, dhcp6_domains, NULL, &space); | |
3951 | ||
3952 | SET_FOREACH(dd, link->ndisc_dnssl, i) | |
3953 | fputs_with_space(f, NDISC_DNSSL_DOMAIN(dd), NULL, &space); | |
3954 | } | |
b2a81c0b | 3955 | |
0d536673 | 3956 | fputc('\n', f); |
67272d15 | 3957 | |
3c9b8860 | 3958 | fprintf(f, "LLMNR=%s\n", |
a7e5da6e | 3959 | resolve_support_to_string(link->network->llmnr)); |
aaa297d4 LP |
3960 | fprintf(f, "MDNS=%s\n", |
3961 | resolve_support_to_string(link->network->mdns)); | |
e7780c8d | 3962 | |
c9299be2 IT |
3963 | if (link->network->dns_over_tls_mode != _DNS_OVER_TLS_MODE_INVALID) |
3964 | fprintf(f, "DNS_OVER_TLS=%s\n", | |
3965 | dns_over_tls_mode_to_string(link->network->dns_over_tls_mode)); | |
d050561a | 3966 | |
ad6c0475 LP |
3967 | if (link->network->dnssec_mode != _DNSSEC_MODE_INVALID) |
3968 | fprintf(f, "DNSSEC=%s\n", | |
3969 | dnssec_mode_to_string(link->network->dnssec_mode)); | |
3970 | ||
8a516214 LP |
3971 | if (!set_isempty(link->network->dnssec_negative_trust_anchors)) { |
3972 | const char *n; | |
3973 | ||
0d536673 | 3974 | fputs("DNSSEC_NTA=", f); |
8a516214 | 3975 | space = false; |
d390f8ef LP |
3976 | SET_FOREACH(n, link->network->dnssec_negative_trust_anchors, i) |
3977 | fputs_with_space(f, n, NULL, &space); | |
0d536673 | 3978 | fputc('\n', f); |
8a516214 LP |
3979 | } |
3980 | ||
0d536673 | 3981 | fputs("ADDRESSES=", f); |
e7780c8d TG |
3982 | space = false; |
3983 | SET_FOREACH(a, link->addresses, i) { | |
3984 | _cleanup_free_ char *address_str = NULL; | |
3985 | ||
3986 | r = in_addr_to_string(a->family, &a->in_addr, &address_str); | |
3987 | if (r < 0) | |
3988 | goto fail; | |
3989 | ||
e7780c8d TG |
3990 | fprintf(f, "%s%s/%u", space ? " " : "", address_str, a->prefixlen); |
3991 | space = true; | |
3992 | } | |
0d536673 | 3993 | fputc('\n', f); |
c1eb9872 | 3994 | |
0d536673 | 3995 | fputs("ROUTES=", f); |
c1eb9872 TG |
3996 | space = false; |
3997 | SET_FOREACH(route, link->routes, i) { | |
3998 | _cleanup_free_ char *route_str = NULL; | |
3999 | ||
4000 | r = in_addr_to_string(route->family, &route->dst, &route_str); | |
4001 | if (r < 0) | |
4002 | goto fail; | |
4003 | ||
ecc3f340 ZJS |
4004 | fprintf(f, "%s%s/%hhu/%hhu/%"PRIu32"/%"PRIu32"/"USEC_FMT, |
4005 | space ? " " : "", route_str, | |
f833694d | 4006 | route->dst_prefixlen, route->tos, route->priority, route->table, route->lifetime); |
c1eb9872 TG |
4007 | space = true; |
4008 | } | |
4009 | ||
0d536673 | 4010 | fputc('\n', f); |
bcb7a07e | 4011 | } |
7374f9d8 | 4012 | |
b295beea LP |
4013 | print_link_hashmap(f, "CARRIER_BOUND_TO=", link->bound_to_links); |
4014 | print_link_hashmap(f, "CARRIER_BOUND_BY=", link->bound_by_links); | |
0d4ad91d | 4015 | |
8eb9058d | 4016 | if (link->dhcp_lease) { |
0bc70f1d | 4017 | struct in_addr address; |
8eb9058d LP |
4018 | const char *tz = NULL; |
4019 | ||
0bc70f1d TG |
4020 | assert(link->network); |
4021 | ||
8eb9058d LP |
4022 | r = sd_dhcp_lease_get_timezone(link->dhcp_lease, &tz); |
4023 | if (r >= 0) | |
4024 | fprintf(f, "TIMEZONE=%s\n", tz); | |
8eb9058d | 4025 | |
0bc70f1d TG |
4026 | r = sd_dhcp_lease_get_address(link->dhcp_lease, &address); |
4027 | if (r >= 0) { | |
0d536673 | 4028 | fputs("DHCP4_ADDRESS=", f); |
0bc70f1d | 4029 | serialize_in_addrs(f, &address, 1); |
0d536673 | 4030 | fputc('\n', f); |
0bc70f1d | 4031 | } |
d9876a52 | 4032 | |
bd91b83e | 4033 | r = dhcp_lease_save(link->dhcp_lease, link->lease_file); |
fe8db0c5 | 4034 | if (r < 0) |
c2d6bd61 | 4035 | goto fail; |
fe8db0c5 | 4036 | |
7374f9d8 | 4037 | fprintf(f, |
b0e39c82 TG |
4038 | "DHCP_LEASE=%s\n", |
4039 | link->lease_file); | |
deb2e523 | 4040 | } else |
68a8723c | 4041 | unlink(link->lease_file); |
fe8db0c5 | 4042 | |
0bc70f1d TG |
4043 | if (link->ipv4ll) { |
4044 | struct in_addr address; | |
4045 | ||
4046 | r = sd_ipv4ll_get_address(link->ipv4ll, &address); | |
4047 | if (r >= 0) { | |
0d536673 | 4048 | fputs("IPV4LL_ADDRESS=", f); |
0bc70f1d | 4049 | serialize_in_addrs(f, &address, 1); |
0d536673 | 4050 | fputc('\n', f); |
0bc70f1d TG |
4051 | } |
4052 | } | |
4053 | ||
c2d6bd61 LP |
4054 | r = fflush_and_check(f); |
4055 | if (r < 0) | |
4056 | goto fail; | |
fe8db0c5 | 4057 | |
c2d6bd61 | 4058 | if (rename(temp_path, link->state_file) < 0) { |
fe8db0c5 | 4059 | r = -errno; |
c2d6bd61 | 4060 | goto fail; |
fe8db0c5 TG |
4061 | } |
4062 | ||
c2d6bd61 | 4063 | return 0; |
dacd6cee | 4064 | |
c2d6bd61 | 4065 | fail: |
6a7a4e4d | 4066 | (void) unlink(link->state_file); |
6a7a4e4d LP |
4067 | if (temp_path) |
4068 | (void) unlink(temp_path); | |
4069 | ||
dacd6cee | 4070 | return log_link_error_errno(link, r, "Failed to save link data to %s: %m", link->state_file); |
fe8db0c5 TG |
4071 | } |
4072 | ||
84de38c5 TG |
4073 | /* The serialized state in /run is no longer up-to-date. */ |
4074 | void link_dirty(Link *link) { | |
4075 | int r; | |
4076 | ||
4077 | assert(link); | |
4078 | ||
0c241a37 SS |
4079 | /* mark manager dirty as link is dirty */ |
4080 | manager_dirty(link->manager); | |
4081 | ||
84de38c5 TG |
4082 | r = set_ensure_allocated(&link->manager->dirty_links, NULL); |
4083 | if (r < 0) | |
4084 | /* allocation errors are ignored */ | |
4085 | return; | |
4086 | ||
4087 | r = set_put(link->manager->dirty_links, link); | |
0c241a37 SS |
4088 | if (r <= 0) |
4089 | /* don't take another ref if the link was already dirty */ | |
84de38c5 TG |
4090 | return; |
4091 | ||
4092 | link_ref(link); | |
4093 | } | |
4094 | ||
4095 | /* The serialized state in /run is up-to-date */ | |
4096 | void link_clean(Link *link) { | |
4097 | assert(link); | |
4098 | assert(link->manager); | |
4099 | ||
1046bf9b | 4100 | link_unref(set_remove(link->manager->dirty_links, link)); |
84de38c5 TG |
4101 | } |
4102 | ||
fe8db0c5 | 4103 | static const char* const link_state_table[_LINK_STATE_MAX] = { |
8434fd5c | 4104 | [LINK_STATE_PENDING] = "pending", |
fe8db0c5 TG |
4105 | [LINK_STATE_ENSLAVING] = "configuring", |
4106 | [LINK_STATE_SETTING_ADDRESSES] = "configuring", | |
4107 | [LINK_STATE_SETTING_ROUTES] = "configuring", | |
4108 | [LINK_STATE_CONFIGURED] = "configured", | |
57bd6899 | 4109 | [LINK_STATE_UNMANAGED] = "unmanaged", |
fe8db0c5 | 4110 | [LINK_STATE_FAILED] = "failed", |
370e9930 | 4111 | [LINK_STATE_LINGER] = "linger", |
fe8db0c5 TG |
4112 | }; |
4113 | ||
4114 | DEFINE_STRING_TABLE_LOOKUP(link_state, LinkState); | |
e375dcde TG |
4115 | |
4116 | static const char* const link_operstate_table[_LINK_OPERSTATE_MAX] = { | |
d3df0e39 TG |
4117 | [LINK_OPERSTATE_OFF] = "off", |
4118 | [LINK_OPERSTATE_NO_CARRIER] = "no-carrier", | |
e375dcde TG |
4119 | [LINK_OPERSTATE_DORMANT] = "dormant", |
4120 | [LINK_OPERSTATE_CARRIER] = "carrier", | |
4121 | [LINK_OPERSTATE_DEGRADED] = "degraded", | |
4122 | [LINK_OPERSTATE_ROUTABLE] = "routable", | |
4123 | }; | |
4124 | ||
4125 | DEFINE_STRING_TABLE_LOOKUP(link_operstate, LinkOperationalState); |