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