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