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