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