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