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