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