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