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