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