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