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