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