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