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