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