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