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