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