]>
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 | ||
29889b4d | 833 | r = routing_policy_rule_configure(rule, link, NULL, false); |
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 | ||
5e5b137a TG |
2489 | static int link_drop_foreign_config(Link *link) { |
2490 | Address *address; | |
2491 | Route *route; | |
2492 | Iterator i; | |
2493 | int r; | |
2494 | ||
2495 | SET_FOREACH(address, link->addresses_foreign, i) { | |
fe307276 | 2496 | /* we consider IPv6LL addresses to be managed by the kernel */ |
5e5b137a TG |
2497 | if (address->family == AF_INET6 && in_addr_is_link_local(AF_INET6, &address->in_addr) == 1) |
2498 | continue; | |
2499 | ||
30226d27 TJ |
2500 | if (link_is_static_address_configured(link, address)) { |
2501 | r = address_add(link, address->family, &address->in_addr, address->prefixlen, NULL); | |
2502 | if (r < 0) | |
2503 | return log_link_error_errno(link, r, "Failed to add address: %m"); | |
2504 | } else { | |
63ae0569 | 2505 | r = address_remove(address, link, NULL); |
30226d27 TJ |
2506 | if (r < 0) |
2507 | return r; | |
2508 | } | |
5e5b137a TG |
2509 | } |
2510 | ||
2511 | SET_FOREACH(route, link->routes_foreign, i) { | |
fe307276 | 2512 | /* do not touch routes managed by the kernel */ |
5e5b137a TG |
2513 | if (route->protocol == RTPROT_KERNEL) |
2514 | continue; | |
2515 | ||
7ecf0c3e TJ |
2516 | if (link_is_static_route_configured(link, route)) { |
2517 | r = route_add(link, route->family, &route->dst, route->dst_prefixlen, route->tos, route->priority, route->table, NULL); | |
2518 | if (r < 0) | |
2519 | return r; | |
2520 | } else { | |
4645ad47 | 2521 | r = route_remove(route, link, NULL); |
7ecf0c3e TJ |
2522 | if (r < 0) |
2523 | return r; | |
2524 | } | |
5e5b137a TG |
2525 | } |
2526 | ||
2527 | return 0; | |
2528 | } | |
2529 | ||
3104883d | 2530 | static int link_drop_config(Link *link) { |
410a7f15 | 2531 | Address *address, *pool_address; |
3104883d SS |
2532 | Route *route; |
2533 | Iterator i; | |
2534 | int r; | |
2535 | ||
2536 | SET_FOREACH(address, link->addresses, i) { | |
2537 | /* we consider IPv6LL addresses to be managed by the kernel */ | |
2538 | if (address->family == AF_INET6 && in_addr_is_link_local(AF_INET6, &address->in_addr) == 1) | |
2539 | continue; | |
2540 | ||
63ae0569 | 2541 | r = address_remove(address, link, NULL); |
3104883d SS |
2542 | if (r < 0) |
2543 | return r; | |
410a7f15 RM |
2544 | |
2545 | /* If this address came from an address pool, clean up the pool */ | |
2546 | LIST_FOREACH(addresses, pool_address, link->pool_addresses) { | |
2547 | if (address_equal(address, pool_address)) { | |
2548 | LIST_REMOVE(addresses, link->pool_addresses, pool_address); | |
2549 | address_free(pool_address); | |
2550 | break; | |
2551 | } | |
2552 | } | |
3104883d SS |
2553 | } |
2554 | ||
2555 | SET_FOREACH(route, link->routes, i) { | |
2556 | /* do not touch routes managed by the kernel */ | |
2557 | if (route->protocol == RTPROT_KERNEL) | |
2558 | continue; | |
2559 | ||
4645ad47 | 2560 | r = route_remove(route, link, NULL); |
3104883d SS |
2561 | if (r < 0) |
2562 | return r; | |
2563 | } | |
2564 | ||
c69305ff LP |
2565 | ndisc_flush(link); |
2566 | ||
3104883d SS |
2567 | return 0; |
2568 | } | |
2569 | ||
a748b692 | 2570 | static int link_configure(Link *link) { |
02b59d57 TG |
2571 | int r; |
2572 | ||
ef1ba606 | 2573 | assert(link); |
b22d8a00 | 2574 | assert(link->network); |
bd08ce56 | 2575 | assert(link->state == LINK_STATE_INITIALIZED); |
a748b692 | 2576 | |
cab042b8 | 2577 | if (STRPTR_IN_SET(link->kind, "can", "vcan", "vxcan")) |
93ea7750 | 2578 | return link_configure_can(link); |
92c918b0 | 2579 | |
02e28621 ДГ |
2580 | /* Drop foreign config, but ignore loopback or critical devices. |
2581 | * We do not want to remove loopback address or addresses used for root NFS. */ | |
7da377ef | 2582 | if (!(link->flags & IFF_LOOPBACK) && |
95355a28 | 2583 | !(link->network->keep_configuration & (KEEP_CONFIGURATION_DHCP_ON_START | KEEP_CONFIGURATION_STATIC))) { |
e5d44b34 CH |
2584 | r = link_drop_foreign_config(link); |
2585 | if (r < 0) | |
2586 | return r; | |
2587 | } | |
5e5b137a | 2588 | |
23d8b221 SS |
2589 | r = link_set_proxy_arp(link); |
2590 | if (r < 0) | |
2591 | return r; | |
2592 | ||
a0e5c15d FK |
2593 | r = ipv6_proxy_ndp_addresses_configure(link); |
2594 | if (r < 0) | |
2595 | return r; | |
2596 | ||
769d324c LP |
2597 | r = link_set_ipv4_forward(link); |
2598 | if (r < 0) | |
2599 | return r; | |
2600 | ||
2601 | r = link_set_ipv6_forward(link); | |
5a8bcb67 LP |
2602 | if (r < 0) |
2603 | return r; | |
2604 | ||
49092e22 SS |
2605 | r = link_set_ipv6_privacy_extensions(link); |
2606 | if (r < 0) | |
2607 | return r; | |
2608 | ||
4f2e437a SS |
2609 | r = link_set_ipv6_accept_ra(link); |
2610 | if (r < 0) | |
2611 | return r; | |
2612 | ||
8749cbcd SS |
2613 | r = link_set_ipv6_dad_transmits(link); |
2614 | if (r < 0) | |
2615 | return r; | |
b69c3180 SS |
2616 | |
2617 | r = link_set_ipv6_hop_limit(link); | |
2618 | if (r < 0) | |
2619 | return r; | |
8749cbcd | 2620 | |
99d2baa2 SS |
2621 | r = link_set_flags(link); |
2622 | if (r < 0) | |
2623 | return r; | |
2624 | ||
11102cba SS |
2625 | r = link_set_ipv6_mtu(link); |
2626 | if (r < 0) | |
2627 | return r; | |
2628 | ||
910feb78 | 2629 | if (link_ipv4ll_enabled(link, ADDRESS_FAMILY_IPV4 | ADDRESS_FAMILY_FALLBACK_IPV4)) { |
b22d8a00 | 2630 | r = ipv4ll_configure(link); |
eb34d4af TG |
2631 | if (r < 0) |
2632 | return r; | |
2633 | } | |
2634 | ||
78c958f8 | 2635 | if (link_dhcp4_enabled(link)) { |
64b21ece MV |
2636 | r = dhcp4_set_promote_secondaries(link); |
2637 | if (r < 0) | |
2638 | return r; | |
2639 | ||
3c9b8860 | 2640 | r = dhcp4_configure(link); |
eb34d4af TG |
2641 | if (r < 0) |
2642 | return r; | |
eb34d4af TG |
2643 | } |
2644 | ||
78c958f8 | 2645 | if (link_dhcp4_server_enabled(link)) { |
dd43110f TG |
2646 | r = sd_dhcp_server_new(&link->dhcp_server, link->ifindex); |
2647 | if (r < 0) | |
2648 | return r; | |
2649 | ||
2650 | r = sd_dhcp_server_attach_event(link->dhcp_server, NULL, 0); | |
2651 | if (r < 0) | |
2652 | return r; | |
dd43110f TG |
2653 | } |
2654 | ||
62379e88 TG |
2655 | if (link_dhcp6_enabled(link) || |
2656 | link_ipv6_accept_ra_enabled(link)) { | |
f5a8c43f TG |
2657 | r = dhcp6_configure(link); |
2658 | if (r < 0) | |
2659 | return r; | |
2660 | } | |
2661 | ||
2662 | if (link_ipv6_accept_ra_enabled(link)) { | |
de1e9928 | 2663 | r = ndisc_configure(link); |
4138fb2c PF |
2664 | if (r < 0) |
2665 | return r; | |
2666 | } | |
2667 | ||
7465dd22 PF |
2668 | if (link_radv_enabled(link)) { |
2669 | r = radv_configure(link); | |
2670 | if (r < 0) | |
2671 | return r; | |
2672 | } | |
2673 | ||
8e1ad1ea | 2674 | if (link_lldp_rx_enabled(link)) { |
7f853950 | 2675 | r = link_lldp_rx_configure(link); |
273eec24 LP |
2676 | if (r < 0) |
2677 | return r; | |
ce43e484 SS |
2678 | } |
2679 | ||
f6fcc1c2 | 2680 | r = link_configure_mtu(link); |
40288ece YW |
2681 | if (r < 0) |
2682 | return r; | |
44b598a1 | 2683 | |
9f6e82e6 YW |
2684 | r = link_configure_addrgen_mode(link); |
2685 | if (r < 0) | |
2686 | return r; | |
0e2fdb83 | 2687 | |
55dc8c4a YW |
2688 | return link_configure_after_setting_mtu(link); |
2689 | } | |
2690 | ||
2691 | static int link_configure_after_setting_mtu(Link *link) { | |
2692 | int r; | |
2693 | ||
2694 | assert(link); | |
2695 | assert(link->network); | |
bd08ce56 | 2696 | assert(link->state == LINK_STATE_INITIALIZED); |
55dc8c4a YW |
2697 | |
2698 | if (link->setting_mtu) | |
2699 | return 0; | |
2700 | ||
dad2d78e | 2701 | if (link_has_carrier(link) || link->network->configure_without_carrier) { |
1e9be60b TG |
2702 | r = link_acquire_conf(link); |
2703 | if (r < 0) | |
2704 | return r; | |
cc544d5f | 2705 | } |
1e9be60b | 2706 | |
3f265037 | 2707 | return link_enter_join_netdev(link); |
505f8da7 TG |
2708 | } |
2709 | ||
27dfc982 YW |
2710 | static int duid_set_uuid(DUID *duid, sd_id128_t uuid) { |
2711 | assert(duid); | |
2712 | ||
2713 | if (duid->raw_data_len > 0) | |
2714 | return 0; | |
2715 | ||
2716 | if (duid->type != DUID_TYPE_UUID) | |
2717 | return -EINVAL; | |
2718 | ||
2719 | memcpy(&duid->raw_data, &uuid, sizeof(sd_id128_t)); | |
2720 | duid->raw_data_len = sizeof(sd_id128_t); | |
2721 | ||
2722 | return 1; | |
2723 | } | |
2724 | ||
2725 | int get_product_uuid_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { | |
2726 | Manager *manager = userdata; | |
2727 | const sd_bus_error *e; | |
2728 | const void *a; | |
2729 | size_t sz; | |
2730 | DUID *duid; | |
2731 | Link *link; | |
2732 | int r; | |
2733 | ||
2734 | assert(m); | |
2735 | assert(manager); | |
2736 | ||
2737 | e = sd_bus_message_get_error(m); | |
2738 | if (e) { | |
2739 | log_error_errno(sd_bus_error_get_errno(e), | |
ab4dd984 | 2740 | "Could not get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %s", |
27dfc982 YW |
2741 | e->message); |
2742 | goto configure; | |
2743 | } | |
2744 | ||
2745 | r = sd_bus_message_read_array(m, 'y', &a, &sz); | |
2746 | if (r < 0) | |
2747 | goto configure; | |
2748 | ||
2749 | if (sz != sizeof(sd_id128_t)) { | |
ab4dd984 | 2750 | log_error("Invalid product UUID. Falling back to use machine-app-specific ID as DUID-UUID."); |
27dfc982 YW |
2751 | goto configure; |
2752 | } | |
2753 | ||
2754 | memcpy(&manager->product_uuid, a, sz); | |
2755 | while ((duid = set_steal_first(manager->duids_requesting_uuid))) | |
2756 | (void) duid_set_uuid(duid, manager->product_uuid); | |
2757 | ||
2758 | manager->duids_requesting_uuid = set_free(manager->duids_requesting_uuid); | |
2759 | ||
2760 | configure: | |
2761 | while ((link = set_steal_first(manager->links_requesting_uuid))) { | |
2762 | r = link_configure(link); | |
2763 | if (r < 0) | |
2764 | log_link_error_errno(link, r, "Failed to configure link: %m"); | |
2765 | } | |
2766 | ||
2767 | manager->links_requesting_uuid = set_free(manager->links_requesting_uuid); | |
2768 | ||
2769 | /* To avoid calling GetProductUUID() bus method so frequently, set the flag below | |
2770 | * even if the method fails. */ | |
2771 | manager->has_product_uuid = true; | |
2772 | ||
2773 | return 1; | |
2774 | } | |
2775 | ||
2776 | static bool link_requires_uuid(Link *link) { | |
2777 | const DUID *duid; | |
2778 | ||
2779 | assert(link); | |
2780 | assert(link->manager); | |
2781 | assert(link->network); | |
2782 | ||
2783 | duid = link_get_duid(link); | |
2784 | if (duid->type != DUID_TYPE_UUID || duid->raw_data_len != 0) | |
2785 | return false; | |
2786 | ||
2787 | if (link_dhcp4_enabled(link) && IN_SET(link->network->dhcp_client_identifier, DHCP_CLIENT_ID_DUID, DHCP_CLIENT_ID_DUID_ONLY)) | |
2788 | return true; | |
2789 | ||
2790 | if (link_dhcp6_enabled(link) || link_ipv6_accept_ra_enabled(link)) | |
2791 | return true; | |
2792 | ||
2793 | return false; | |
2794 | } | |
2795 | ||
2796 | static int link_configure_duid(Link *link) { | |
2797 | Manager *m; | |
2798 | DUID *duid; | |
2799 | int r; | |
2800 | ||
2801 | assert(link); | |
2802 | assert(link->manager); | |
2803 | assert(link->network); | |
2804 | ||
2805 | m = link->manager; | |
2806 | duid = link_get_duid(link); | |
2807 | ||
2808 | if (!link_requires_uuid(link)) | |
2809 | return 1; | |
2810 | ||
2811 | if (m->has_product_uuid) { | |
2812 | (void) duid_set_uuid(duid, m->product_uuid); | |
2813 | return 1; | |
2814 | } | |
2815 | ||
2816 | if (!m->links_requesting_uuid) { | |
2817 | r = manager_request_product_uuid(m, link); | |
2818 | if (r < 0) { | |
2819 | if (r == -ENOMEM) | |
2820 | return r; | |
2821 | ||
ab4dd984 ZJS |
2822 | log_link_warning_errno(link, r, |
2823 | "Failed to get product UUID. Falling back to use machine-app-specific ID as DUID-UUID: %m"); | |
27dfc982 YW |
2824 | return 1; |
2825 | } | |
2826 | } else { | |
2827 | r = set_put(m->links_requesting_uuid, link); | |
2828 | if (r < 0) | |
2829 | return log_oom(); | |
2830 | ||
2831 | r = set_put(m->duids_requesting_uuid, duid); | |
2832 | if (r < 0) | |
2833 | return log_oom(); | |
2834 | } | |
2835 | ||
2836 | return 0; | |
2837 | } | |
2838 | ||
e6bf7774 | 2839 | static int link_initialized_and_synced(Link *link) { |
505f8da7 | 2840 | Network *network; |
505f8da7 TG |
2841 | int r; |
2842 | ||
2843 | assert(link); | |
2844 | assert(link->ifname); | |
2845 | assert(link->manager); | |
2846 | ||
bd08ce56 YW |
2847 | /* We may get called either from the asynchronous netlink callback, |
2848 | * or directly for link_add() if running in a container. See link_add(). */ | |
2849 | if (!IN_SET(link->state, LINK_STATE_PENDING, LINK_STATE_INITIALIZED)) | |
4232cf04 | 2850 | return 0; |
505f8da7 | 2851 | |
6a7a4e4d | 2852 | log_link_debug(link, "Link state is up-to-date"); |
bd08ce56 | 2853 | link_set_state(link, LINK_STATE_INITIALIZED); |
505f8da7 | 2854 | |
0d4ad91d AR |
2855 | r = link_new_bound_by_list(link); |
2856 | if (r < 0) | |
2857 | return r; | |
2858 | ||
2859 | r = link_handle_bound_by_list(link); | |
2860 | if (r < 0) | |
2861 | return r; | |
2862 | ||
c4a03a56 | 2863 | if (!link->network) { |
51517f9e | 2864 | r = network_get(link->manager, link->sd_device, link->ifname, |
c4a03a56 TG |
2865 | &link->mac, &network); |
2866 | if (r == -ENOENT) { | |
2867 | link_enter_unmanaged(link); | |
4232cf04 | 2868 | return 0; |
a09dc546 DM |
2869 | } else if (r == 0 && network->unmanaged) { |
2870 | link_enter_unmanaged(link); | |
2871 | return 0; | |
c4a03a56 TG |
2872 | } else if (r < 0) |
2873 | return r; | |
505f8da7 | 2874 | |
c4a03a56 TG |
2875 | if (link->flags & IFF_LOOPBACK) { |
2876 | if (network->link_local != ADDRESS_FAMILY_NO) | |
2877 | log_link_debug(link, "Ignoring link-local autoconfiguration for loopback link"); | |
78c958f8 | 2878 | |
c4a03a56 TG |
2879 | if (network->dhcp != ADDRESS_FAMILY_NO) |
2880 | log_link_debug(link, "Ignoring DHCP clients for loopback link"); | |
78c958f8 | 2881 | |
c4a03a56 TG |
2882 | if (network->dhcp_server) |
2883 | log_link_debug(link, "Ignoring DHCP server for loopback link"); | |
2884 | } | |
bd2efe92 | 2885 | |
7d342c03 | 2886 | r = network_apply(network, link); |
c4a03a56 TG |
2887 | if (r < 0) |
2888 | return r; | |
2889 | } | |
505f8da7 | 2890 | |
0d4ad91d AR |
2891 | r = link_new_bound_to_list(link); |
2892 | if (r < 0) | |
2893 | return r; | |
2894 | ||
27dfc982 YW |
2895 | /* link_configure_duid() returns 0 if it requests product UUID. In that case, |
2896 | * link_configure() is called later asynchronously. */ | |
2897 | r = link_configure_duid(link); | |
2898 | if (r <= 0) | |
2899 | return r; | |
2900 | ||
a748b692 TG |
2901 | r = link_configure(link); |
2902 | if (r < 0) | |
2903 | return r; | |
2904 | ||
4232cf04 | 2905 | return 0; |
505f8da7 TG |
2906 | } |
2907 | ||
302a796f YW |
2908 | static int link_initialized_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
2909 | (void) link_initialized_and_synced(link); | |
e6bf7774 YW |
2910 | return 1; |
2911 | } | |
2912 | ||
51517f9e | 2913 | int link_initialized(Link *link, sd_device *device) { |
4afd3348 | 2914 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
4f561e8e TG |
2915 | int r; |
2916 | ||
2917 | assert(link); | |
2918 | assert(link->manager); | |
2919 | assert(link->manager->rtnl); | |
2920 | assert(device); | |
2921 | ||
8434fd5c | 2922 | if (link->state != LINK_STATE_PENDING) |
4f561e8e TG |
2923 | return 0; |
2924 | ||
51517f9e | 2925 | if (link->sd_device) |
679b3605 TG |
2926 | return 0; |
2927 | ||
79008bdd | 2928 | log_link_debug(link, "udev initialized link"); |
bd08ce56 | 2929 | link_set_state(link, LINK_STATE_INITIALIZED); |
4f561e8e | 2930 | |
51517f9e | 2931 | link->sd_device = sd_device_ref(device); |
4f561e8e | 2932 | |
3c9b8860 TG |
2933 | /* udev has initialized the link, but we don't know if we have yet |
2934 | * processed the NEWLINK messages with the latest state. Do a GETLINK, | |
2935 | * when it returns we know that the pending NEWLINKs have already been | |
2936 | * processed and that we are up-to-date */ | |
4f561e8e | 2937 | |
3c9b8860 TG |
2938 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK, |
2939 | link->ifindex); | |
4f561e8e TG |
2940 | if (r < 0) |
2941 | return r; | |
2942 | ||
302a796f YW |
2943 | r = netlink_call_async(link->manager->rtnl, NULL, req, link_initialized_handler, |
2944 | link_netlink_destroy_callback, link); | |
4f561e8e TG |
2945 | if (r < 0) |
2946 | return r; | |
2947 | ||
5da8149f TG |
2948 | link_ref(link); |
2949 | ||
4f561e8e TG |
2950 | return 0; |
2951 | } | |
2952 | ||
c4a03a56 | 2953 | static int link_load(Link *link) { |
0bc70f1d TG |
2954 | _cleanup_free_ char *network_file = NULL, |
2955 | *addresses = NULL, | |
f703cc2c | 2956 | *routes = NULL, |
0bc70f1d TG |
2957 | *dhcp4_address = NULL, |
2958 | *ipv4ll_address = NULL; | |
2959 | union in_addr_union address; | |
f703cc2c | 2960 | union in_addr_union route_dst; |
c598ac76 | 2961 | const char *p; |
c4a03a56 TG |
2962 | int r; |
2963 | ||
2964 | assert(link); | |
2965 | ||
aa8fbc74 | 2966 | r = parse_env_file(NULL, link->state_file, |
c4a03a56 TG |
2967 | "NETWORK_FILE", &network_file, |
2968 | "ADDRESSES", &addresses, | |
f703cc2c | 2969 | "ROUTES", &routes, |
0bc70f1d | 2970 | "DHCP4_ADDRESS", &dhcp4_address, |
13df9c39 | 2971 | "IPV4LL_ADDRESS", &ipv4ll_address); |
c4a03a56 TG |
2972 | if (r < 0 && r != -ENOENT) |
2973 | return log_link_error_errno(link, r, "Failed to read %s: %m", link->state_file); | |
2974 | ||
2975 | if (network_file) { | |
2976 | Network *network; | |
2977 | char *suffix; | |
2978 | ||
2979 | /* drop suffix */ | |
2980 | suffix = strrchr(network_file, '.'); | |
2981 | if (!suffix) { | |
2982 | log_link_debug(link, "Failed to get network name from %s", network_file); | |
2983 | goto network_file_fail; | |
2984 | } | |
2985 | *suffix = '\0'; | |
2986 | ||
2987 | r = network_get_by_name(link->manager, basename(network_file), &network); | |
2988 | if (r < 0) { | |
2989 | log_link_debug_errno(link, r, "Failed to get network %s: %m", basename(network_file)); | |
2990 | goto network_file_fail; | |
2991 | } | |
2992 | ||
7d342c03 | 2993 | r = network_apply(network, link); |
c4a03a56 TG |
2994 | if (r < 0) |
2995 | return log_link_error_errno(link, r, "Failed to apply network %s: %m", basename(network_file)); | |
2996 | } | |
2997 | ||
2998 | network_file_fail: | |
2999 | ||
3000 | if (addresses) { | |
c598ac76 | 3001 | p = addresses; |
c4a03a56 | 3002 | |
c598ac76 TG |
3003 | for (;;) { |
3004 | _cleanup_free_ char *address_str = NULL; | |
c4a03a56 TG |
3005 | char *prefixlen_str; |
3006 | int family; | |
3007 | unsigned char prefixlen; | |
c4a03a56 | 3008 | |
c598ac76 TG |
3009 | r = extract_first_word(&p, &address_str, NULL, 0); |
3010 | if (r < 0) { | |
3011 | log_link_debug_errno(link, r, "Failed to extract next address string: %m"); | |
3012 | continue; | |
928bc597 TA |
3013 | } |
3014 | if (r == 0) | |
c598ac76 TG |
3015 | break; |
3016 | ||
3017 | prefixlen_str = strchr(address_str, '/'); | |
c4a03a56 | 3018 | if (!prefixlen_str) { |
c598ac76 | 3019 | log_link_debug(link, "Failed to parse address and prefix length %s", address_str); |
c4a03a56 TG |
3020 | continue; |
3021 | } | |
3022 | ||
313cefa1 | 3023 | *prefixlen_str++ = '\0'; |
c4a03a56 TG |
3024 | |
3025 | r = sscanf(prefixlen_str, "%hhu", &prefixlen); | |
3026 | if (r != 1) { | |
3027 | log_link_error(link, "Failed to parse prefixlen %s", prefixlen_str); | |
3028 | continue; | |
3029 | } | |
3030 | ||
c598ac76 | 3031 | r = in_addr_from_string_auto(address_str, &family, &address); |
c4a03a56 | 3032 | if (r < 0) { |
c598ac76 | 3033 | log_link_debug_errno(link, r, "Failed to parse address %s: %m", address_str); |
c4a03a56 TG |
3034 | continue; |
3035 | } | |
3036 | ||
3037 | r = address_add(link, family, &address, prefixlen, NULL); | |
3038 | if (r < 0) | |
3039 | return log_link_error_errno(link, r, "Failed to add address: %m"); | |
3040 | } | |
3041 | } | |
3042 | ||
f703cc2c | 3043 | if (routes) { |
74544b4e TA |
3044 | p = routes; |
3045 | ||
c598ac76 | 3046 | for (;;) { |
f833694d | 3047 | Route *route; |
c598ac76 | 3048 | _cleanup_free_ char *route_str = NULL; |
4afd3348 | 3049 | _cleanup_(sd_event_source_unrefp) sd_event_source *expire = NULL; |
f833694d | 3050 | usec_t lifetime; |
f703cc2c TG |
3051 | char *prefixlen_str; |
3052 | int family; | |
3053 | unsigned char prefixlen, tos, table; | |
3054 | uint32_t priority; | |
3055 | ||
c598ac76 TG |
3056 | r = extract_first_word(&p, &route_str, NULL, 0); |
3057 | if (r < 0) { | |
3058 | log_link_debug_errno(link, r, "Failed to extract next route string: %m"); | |
3059 | continue; | |
928bc597 TA |
3060 | } |
3061 | if (r == 0) | |
c598ac76 TG |
3062 | break; |
3063 | ||
3064 | prefixlen_str = strchr(route_str, '/'); | |
f703cc2c | 3065 | if (!prefixlen_str) { |
c598ac76 | 3066 | log_link_debug(link, "Failed to parse route %s", route_str); |
f703cc2c TG |
3067 | continue; |
3068 | } | |
3069 | ||
313cefa1 | 3070 | *prefixlen_str++ = '\0'; |
f703cc2c | 3071 | |
f833694d TG |
3072 | r = sscanf(prefixlen_str, "%hhu/%hhu/%"SCNu32"/%hhu/"USEC_FMT, &prefixlen, &tos, &priority, &table, &lifetime); |
3073 | if (r != 5) { | |
3074 | log_link_debug(link, | |
3075 | "Failed to parse destination prefix length, tos, priority, table or expiration %s", | |
3076 | prefixlen_str); | |
f703cc2c TG |
3077 | continue; |
3078 | } | |
3079 | ||
c598ac76 | 3080 | r = in_addr_from_string_auto(route_str, &family, &route_dst); |
f703cc2c | 3081 | if (r < 0) { |
c598ac76 | 3082 | log_link_debug_errno(link, r, "Failed to parse route destination %s: %m", route_str); |
f703cc2c TG |
3083 | continue; |
3084 | } | |
3085 | ||
f833694d | 3086 | r = route_add(link, family, &route_dst, prefixlen, tos, priority, table, &route); |
f703cc2c TG |
3087 | if (r < 0) |
3088 | return log_link_error_errno(link, r, "Failed to add route: %m"); | |
f833694d | 3089 | |
f02ba163 | 3090 | if (lifetime != USEC_INFINITY && !kernel_route_expiration_supported()) { |
f833694d TG |
3091 | r = sd_event_add_time(link->manager->event, &expire, clock_boottime_or_monotonic(), lifetime, |
3092 | 0, route_expire_handler, route); | |
3093 | if (r < 0) | |
3094 | log_link_warning_errno(link, r, "Could not arm route expiration handler: %m"); | |
3095 | } | |
3096 | ||
3097 | route->lifetime = lifetime; | |
3098 | sd_event_source_unref(route->expire); | |
1cc6c93a | 3099 | route->expire = TAKE_PTR(expire); |
f703cc2c TG |
3100 | } |
3101 | } | |
3102 | ||
0bc70f1d TG |
3103 | if (dhcp4_address) { |
3104 | r = in_addr_from_string(AF_INET, dhcp4_address, &address); | |
3105 | if (r < 0) { | |
b68d26b8 | 3106 | log_link_debug_errno(link, r, "Failed to parse DHCPv4 address %s: %m", dhcp4_address); |
0bc70f1d TG |
3107 | goto dhcp4_address_fail; |
3108 | } | |
3109 | ||
b1c626f6 | 3110 | r = sd_dhcp_client_new(&link->dhcp_client, link->network ? link->network->dhcp_anonymize : 0); |
0bc70f1d | 3111 | if (r < 0) |
b68d26b8 | 3112 | return log_link_error_errno(link, r, "Failed to create DHCPv4 client: %m"); |
0bc70f1d TG |
3113 | |
3114 | r = sd_dhcp_client_set_request_address(link->dhcp_client, &address.in); | |
3115 | if (r < 0) | |
b68d26b8 | 3116 | return log_link_error_errno(link, r, "Failed to set initial DHCPv4 address %s: %m", dhcp4_address); |
0bc70f1d TG |
3117 | } |
3118 | ||
3119 | dhcp4_address_fail: | |
3120 | ||
3121 | if (ipv4ll_address) { | |
3122 | r = in_addr_from_string(AF_INET, ipv4ll_address, &address); | |
3123 | if (r < 0) { | |
b68d26b8 | 3124 | log_link_debug_errno(link, r, "Failed to parse IPv4LL address %s: %m", ipv4ll_address); |
0bc70f1d TG |
3125 | goto ipv4ll_address_fail; |
3126 | } | |
3127 | ||
3128 | r = sd_ipv4ll_new(&link->ipv4ll); | |
3129 | if (r < 0) | |
b68d26b8 | 3130 | return log_link_error_errno(link, r, "Failed to create IPv4LL client: %m"); |
0bc70f1d TG |
3131 | |
3132 | r = sd_ipv4ll_set_address(link->ipv4ll, &address.in); | |
3133 | if (r < 0) | |
b68d26b8 | 3134 | return log_link_error_errno(link, r, "Failed to set initial IPv4LL address %s: %m", ipv4ll_address); |
0bc70f1d TG |
3135 | } |
3136 | ||
3137 | ipv4ll_address_fail: | |
3138 | ||
c4a03a56 TG |
3139 | return 0; |
3140 | } | |
3141 | ||
1c4baffc | 3142 | int link_add(Manager *m, sd_netlink_message *message, Link **ret) { |
51517f9e | 3143 | _cleanup_(sd_device_unrefp) sd_device *device = NULL; |
505f8da7 | 3144 | char ifindex_str[2 + DECIMAL_STR_MAX(int)]; |
51517f9e | 3145 | Link *link; |
5a937ea2 | 3146 | int r; |
505f8da7 TG |
3147 | |
3148 | assert(m); | |
fbbeb65a | 3149 | assert(m->rtnl); |
505f8da7 TG |
3150 | assert(message); |
3151 | assert(ret); | |
3152 | ||
3153 | r = link_new(m, message, ret); | |
3154 | if (r < 0) | |
3155 | return r; | |
3156 | ||
3157 | link = *ret; | |
3158 | ||
6a7a4e4d | 3159 | log_link_debug(link, "Link %d added", link->ifindex); |
505f8da7 | 3160 | |
c4a03a56 TG |
3161 | r = link_load(link); |
3162 | if (r < 0) | |
3163 | return r; | |
3164 | ||
75f86906 | 3165 | if (detect_container() <= 0) { |
505f8da7 | 3166 | /* not in a container, udev will be around */ |
ae06ab10 | 3167 | sprintf(ifindex_str, "n%d", link->ifindex); |
51517f9e YW |
3168 | r = sd_device_new_from_device_id(&device, ifindex_str); |
3169 | if (r < 0) { | |
3170 | log_link_warning_errno(link, r, "Could not find device: %m"); | |
5c416fc4 TG |
3171 | goto failed; |
3172 | } | |
505f8da7 | 3173 | |
5a937ea2 | 3174 | r = sd_device_get_is_initialized(device); |
51517f9e YW |
3175 | if (r < 0) { |
3176 | log_link_warning_errno(link, r, "Could not determine whether the device is initialized or not: %m"); | |
3177 | goto failed; | |
3178 | } | |
5a937ea2 | 3179 | if (r == 0) { |
505f8da7 | 3180 | /* not yet ready */ |
79008bdd | 3181 | log_link_debug(link, "link pending udev initialization..."); |
505f8da7 | 3182 | return 0; |
3c4cb064 | 3183 | } |
505f8da7 | 3184 | |
299ad32d YW |
3185 | r = device_is_renaming(device); |
3186 | if (r < 0) { | |
3187 | log_link_warning_errno(link, r, "Failed to determine the device is renamed or not: %m"); | |
3188 | goto failed; | |
3189 | } | |
3190 | if (r > 0) { | |
3191 | log_link_debug(link, "Interface is under renaming, pending initialization."); | |
3192 | return 0; | |
3193 | } | |
3194 | ||
4f561e8e TG |
3195 | r = link_initialized(link, device); |
3196 | if (r < 0) | |
5c416fc4 | 3197 | goto failed; |
4f561e8e | 3198 | } else { |
e6bf7774 | 3199 | r = link_initialized_and_synced(link); |
4f561e8e | 3200 | if (r < 0) |
5c416fc4 | 3201 | goto failed; |
4f561e8e | 3202 | } |
505f8da7 | 3203 | |
a748b692 | 3204 | return 0; |
5c416fc4 TG |
3205 | failed: |
3206 | link_enter_failed(link); | |
3207 | return r; | |
a748b692 TG |
3208 | } |
3209 | ||
c601ebf7 | 3210 | int link_ipv6ll_gained(Link *link, const struct in6_addr *address) { |
e7ab854c TG |
3211 | int r; |
3212 | ||
3213 | assert(link); | |
3214 | ||
3215 | log_link_info(link, "Gained IPv6LL"); | |
3216 | ||
c601ebf7 | 3217 | link->ipv6ll_address = *address; |
e7ab854c TG |
3218 | link_check_ready(link); |
3219 | ||
b9ea3d2e | 3220 | if (IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) { |
e7ab854c TG |
3221 | r = link_acquire_ipv6_conf(link); |
3222 | if (r < 0) { | |
3223 | link_enter_failed(link); | |
3224 | return r; | |
3225 | } | |
3226 | } | |
3227 | ||
3228 | return 0; | |
3229 | } | |
3230 | ||
9c0a72f9 TG |
3231 | static int link_carrier_gained(Link *link) { |
3232 | int r; | |
3233 | ||
3234 | assert(link); | |
3235 | ||
b9ea3d2e | 3236 | if (IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED)) { |
9c0a72f9 TG |
3237 | r = link_acquire_conf(link); |
3238 | if (r < 0) { | |
3239 | link_enter_failed(link); | |
3240 | return r; | |
3241 | } | |
6fc25497 | 3242 | |
289e6774 | 3243 | r = link_request_set_addresses(link); |
6fc25497 SS |
3244 | if (r < 0) |
3245 | return r; | |
9c0a72f9 TG |
3246 | } |
3247 | ||
0d4ad91d AR |
3248 | r = link_handle_bound_by_list(link); |
3249 | if (r < 0) | |
3250 | return r; | |
3251 | ||
9c0a72f9 TG |
3252 | return 0; |
3253 | } | |
3254 | ||
3255 | static int link_carrier_lost(Link *link) { | |
3256 | int r; | |
3257 | ||
3258 | assert(link); | |
3259 | ||
25e992ba | 3260 | if (link->network && link->network->ignore_carrier_loss) |
93b4dab5 SS |
3261 | return 0; |
3262 | ||
fbdb6605 | 3263 | /* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop. |
55dc8c4a YW |
3264 | * setting_mtu keep track whether the device got reset because of setting MTU and does not drop the |
3265 | * configuration and stop the clients as well. */ | |
97e7fb39 SS |
3266 | if (link->setting_mtu) |
3267 | return 0; | |
3268 | ||
95355a28 | 3269 | r = link_stop_clients(link, false); |
9c0a72f9 TG |
3270 | if (r < 0) { |
3271 | link_enter_failed(link); | |
3272 | return r; | |
3273 | } | |
3274 | ||
28464ae0 SS |
3275 | if (link_dhcp4_server_enabled(link)) |
3276 | (void) sd_dhcp_server_stop(link->dhcp_server); | |
45a9eac9 | 3277 | |
3104883d SS |
3278 | r = link_drop_config(link); |
3279 | if (r < 0) | |
3280 | return r; | |
3281 | ||
f258e948 MP |
3282 | if (!IN_SET(link->state, LINK_STATE_UNMANAGED, LINK_STATE_PENDING)) { |
3283 | log_link_debug(link, "State is %s, dropping config", link_state_to_string(link->state)); | |
c436d553 MM |
3284 | r = link_drop_foreign_config(link); |
3285 | if (r < 0) | |
3286 | return r; | |
3287 | } | |
3104883d | 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 | int link_carrier_reset(Link *link) { | |
3297 | int r; | |
3298 | ||
3299 | assert(link); | |
3300 | ||
3301 | if (link_has_carrier(link)) { | |
3302 | r = link_carrier_lost(link); | |
3303 | if (r < 0) | |
3304 | return r; | |
3305 | ||
3306 | r = link_carrier_gained(link); | |
3307 | if (r < 0) | |
3308 | return r; | |
3309 | ||
6a7a4e4d | 3310 | log_link_info(link, "Reset carrier"); |
9c0a72f9 TG |
3311 | } |
3312 | ||
3313 | return 0; | |
3314 | } | |
3315 | ||
1c4baffc | 3316 | int link_update(Link *link, sd_netlink_message *m) { |
c49b33ac | 3317 | struct ether_addr mac; |
ca4e095a | 3318 | const char *ifname; |
afe7fd56 | 3319 | uint32_t mtu; |
a61bb41c | 3320 | bool had_carrier, carrier_gained, carrier_lost; |
bb262ef0 | 3321 | int old_master, r; |
22936833 | 3322 | |
dd3efc09 | 3323 | assert(link); |
b8941f74 | 3324 | assert(link->ifname); |
22936833 TG |
3325 | assert(m); |
3326 | ||
7619683b | 3327 | if (link->state == LINK_STATE_LINGER) { |
5238e957 | 3328 | log_link_info(link, "Link re-added"); |
289e6774 | 3329 | link_set_state(link, LINK_STATE_CONFIGURING); |
0d4ad91d AR |
3330 | |
3331 | r = link_new_carrier_maps(link); | |
3332 | if (r < 0) | |
3333 | return r; | |
7619683b TG |
3334 | } |
3335 | ||
1c4baffc | 3336 | r = sd_netlink_message_read_string(m, IFLA_IFNAME, &ifname); |
b8941f74 | 3337 | if (r >= 0 && !streq(ifname, link->ifname)) { |
30de2b89 | 3338 | Manager *manager = link->manager; |
b8941f74 | 3339 | |
30de2b89 | 3340 | log_link_info(link, "Interface name change detected, %s has been renamed to %s.", link->ifname, ifname); |
0d4ad91d | 3341 | |
30de2b89 YW |
3342 | link_drop(link); |
3343 | r = link_add(manager, m, &link); | |
3344 | if (r < 0) | |
3345 | return r; | |
b8941f74 TG |
3346 | } |
3347 | ||
1c4baffc | 3348 | r = sd_netlink_message_read_u32(m, IFLA_MTU, &mtu); |
afe7fd56 TG |
3349 | if (r >= 0 && mtu > 0) { |
3350 | link->mtu = mtu; | |
4e964aa0 | 3351 | if (link->original_mtu == 0) { |
afe7fd56 | 3352 | link->original_mtu = mtu; |
6a7a4e4d | 3353 | log_link_debug(link, "Saved original MTU: %" PRIu32, link->original_mtu); |
afe7fd56 TG |
3354 | } |
3355 | ||
3356 | if (link->dhcp_client) { | |
3c9b8860 TG |
3357 | r = sd_dhcp_client_set_mtu(link->dhcp_client, |
3358 | link->mtu); | |
fc95c359 YW |
3359 | if (r < 0) |
3360 | return log_link_warning_errno(link, r, "Could not update MTU in DHCP client: %m"); | |
afe7fd56 | 3361 | } |
7465dd22 PF |
3362 | |
3363 | if (link->radv) { | |
3364 | r = sd_radv_set_mtu(link->radv, link->mtu); | |
3365 | if (r < 0) | |
3366 | return log_link_warning_errno(link, r, "Could not set MTU for Router Advertisement: %m"); | |
3367 | } | |
9842de0d | 3368 | } |
69629de9 | 3369 | |
e9189a1f TG |
3370 | /* The kernel may broadcast NEWLINK messages without the MAC address |
3371 | set, simply ignore them. */ | |
1c4baffc | 3372 | r = sd_netlink_message_read_ether_addr(m, IFLA_ADDRESS, &mac); |
e9189a1f | 3373 | if (r >= 0) { |
3c9b8860 TG |
3374 | if (memcmp(link->mac.ether_addr_octet, mac.ether_addr_octet, |
3375 | ETH_ALEN)) { | |
c49b33ac | 3376 | |
3c9b8860 TG |
3377 | memcpy(link->mac.ether_addr_octet, mac.ether_addr_octet, |
3378 | ETH_ALEN); | |
c49b33ac | 3379 | |
79008bdd | 3380 | log_link_debug(link, "MAC address: " |
20861203 TG |
3381 | "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", |
3382 | mac.ether_addr_octet[0], | |
3383 | mac.ether_addr_octet[1], | |
3384 | mac.ether_addr_octet[2], | |
3385 | mac.ether_addr_octet[3], | |
3386 | mac.ether_addr_octet[4], | |
3387 | mac.ether_addr_octet[5]); | |
c49b33ac | 3388 | |
20861203 TG |
3389 | if (link->ipv4ll) { |
3390 | r = sd_ipv4ll_set_mac(link->ipv4ll, &link->mac); | |
6a7a4e4d LP |
3391 | if (r < 0) |
3392 | return log_link_warning_errno(link, r, "Could not update MAC address in IPv4LL client: %m"); | |
c49b33ac | 3393 | } |
c49b33ac | 3394 | |
20861203 | 3395 | if (link->dhcp_client) { |
3c9b8860 | 3396 | r = sd_dhcp_client_set_mac(link->dhcp_client, |
76253e73 DW |
3397 | (const uint8_t *) &link->mac, |
3398 | sizeof (link->mac), | |
3399 | ARPHRD_ETHER); | |
6a7a4e4d LP |
3400 | if (r < 0) |
3401 | return log_link_warning_errno(link, r, "Could not update MAC address in DHCP client: %m"); | |
413708d1 | 3402 | |
fff1f40c YW |
3403 | r = dhcp4_set_client_identifier(link); |
3404 | if (r < 0) | |
3405 | return r; | |
c49b33ac | 3406 | } |
4138fb2c PF |
3407 | |
3408 | if (link->dhcp6_client) { | |
f24648a6 | 3409 | const DUID* duid = link_get_duid(link); |
8341a5c3 | 3410 | |
4138fb2c | 3411 | r = sd_dhcp6_client_set_mac(link->dhcp6_client, |
76253e73 DW |
3412 | (const uint8_t *) &link->mac, |
3413 | sizeof (link->mac), | |
3414 | ARPHRD_ETHER); | |
6a7a4e4d LP |
3415 | if (r < 0) |
3416 | return log_link_warning_errno(link, r, "Could not update MAC address in DHCPv6 client: %m"); | |
413708d1 | 3417 | |
8217ed5e TH |
3418 | if (link->network->iaid_set) { |
3419 | r = sd_dhcp6_client_set_iaid(link->dhcp6_client, | |
3420 | link->network->iaid); | |
3421 | if (r < 0) | |
3422 | return log_link_warning_errno(link, r, "Could not update DHCPv6 IAID: %m"); | |
3423 | } | |
413708d1 | 3424 | |
8341a5c3 ZJS |
3425 | r = sd_dhcp6_client_set_duid(link->dhcp6_client, |
3426 | duid->type, | |
3427 | duid->raw_data_len > 0 ? duid->raw_data : NULL, | |
3428 | duid->raw_data_len); | |
413708d1 VK |
3429 | if (r < 0) |
3430 | return log_link_warning_errno(link, r, "Could not update DHCPv6 DUID: %m"); | |
4138fb2c | 3431 | } |
7465dd22 PF |
3432 | |
3433 | if (link->radv) { | |
3434 | r = sd_radv_set_mac(link->radv, &link->mac); | |
3435 | if (r < 0) | |
3436 | return log_link_warning_errno(link, r, "Could not update MAC for Router Advertisement: %m"); | |
3437 | } | |
420d2058 SS |
3438 | |
3439 | if (link->ndisc) { | |
3440 | r = sd_ndisc_set_mac(link->ndisc, &link->mac); | |
3441 | if (r < 0) | |
3442 | return log_link_warning_errno(link, r, "Could not update MAC for ndisc: %m"); | |
3443 | } | |
c49b33ac | 3444 | } |
4f882b2a TG |
3445 | } |
3446 | ||
bb262ef0 YW |
3447 | old_master = link->master_ifindex; |
3448 | (void) sd_netlink_message_read_u32(m, IFLA_MASTER, (uint32_t *) &link->master_ifindex); | |
3449 | ||
a61bb41c TG |
3450 | had_carrier = link_has_carrier(link); |
3451 | ||
bb262ef0 | 3452 | r = link_update_flags(link, m, old_master != link->master_ifindex); |
a61bb41c TG |
3453 | if (r < 0) |
3454 | return r; | |
3455 | ||
273eec24 LP |
3456 | r = link_update_lldp(link); |
3457 | if (r < 0) | |
3458 | return r; | |
3459 | ||
a61bb41c TG |
3460 | carrier_gained = !had_carrier && link_has_carrier(link); |
3461 | carrier_lost = had_carrier && !link_has_carrier(link); | |
3462 | ||
3463 | if (carrier_gained) { | |
6a7a4e4d | 3464 | log_link_info(link, "Gained carrier"); |
a61bb41c | 3465 | |
9c0a72f9 TG |
3466 | r = link_carrier_gained(link); |
3467 | if (r < 0) | |
3468 | return r; | |
a61bb41c | 3469 | } else if (carrier_lost) { |
6a7a4e4d | 3470 | log_link_info(link, "Lost carrier"); |
a61bb41c | 3471 | |
9c0a72f9 TG |
3472 | r = link_carrier_lost(link); |
3473 | if (r < 0) | |
a61bb41c | 3474 | return r; |
a61bb41c TG |
3475 | } |
3476 | ||
3477 | return 0; | |
dd3efc09 | 3478 | } |
fe8db0c5 | 3479 | |
b295beea LP |
3480 | static void print_link_hashmap(FILE *f, const char *prefix, Hashmap* h) { |
3481 | bool space = false; | |
3482 | Iterator i; | |
3483 | Link *link; | |
3484 | ||
3485 | assert(f); | |
3486 | assert(prefix); | |
3487 | ||
3488 | if (hashmap_isempty(h)) | |
3489 | return; | |
3490 | ||
0d536673 | 3491 | fputs(prefix, f); |
b295beea LP |
3492 | HASHMAP_FOREACH(link, h, i) { |
3493 | if (space) | |
0d536673 | 3494 | fputc(' ', f); |
b295beea LP |
3495 | |
3496 | fprintf(f, "%i", link->ifindex); | |
3497 | space = true; | |
3498 | } | |
3499 | ||
0d536673 | 3500 | fputc('\n', f); |
b295beea LP |
3501 | } |
3502 | ||
fe8db0c5 | 3503 | int link_save(Link *link) { |
68a8723c | 3504 | _cleanup_free_ char *temp_path = NULL; |
fe8db0c5 | 3505 | _cleanup_fclose_ FILE *f = NULL; |
e375dcde | 3506 | const char *admin_state, *oper_state; |
e7780c8d | 3507 | Address *a; |
c1eb9872 | 3508 | Route *route; |
e7780c8d | 3509 | Iterator i; |
fe8db0c5 TG |
3510 | int r; |
3511 | ||
3512 | assert(link); | |
3513 | assert(link->state_file); | |
68a8723c | 3514 | assert(link->lease_file); |
bbf7c048 TG |
3515 | assert(link->manager); |
3516 | ||
370e9930 | 3517 | if (link->state == LINK_STATE_LINGER) { |
6990fb6b | 3518 | (void) unlink(link->state_file); |
370e9930 TG |
3519 | return 0; |
3520 | } | |
3521 | ||
34437b4f LP |
3522 | link_lldp_save(link); |
3523 | ||
deb2e523 TG |
3524 | admin_state = link_state_to_string(link->state); |
3525 | assert(admin_state); | |
3526 | ||
e375dcde TG |
3527 | oper_state = link_operstate_to_string(link->operstate); |
3528 | assert(oper_state); | |
deb2e523 | 3529 | |
fe8db0c5 TG |
3530 | r = fopen_temporary(link->state_file, &f, &temp_path); |
3531 | if (r < 0) | |
6a7a4e4d | 3532 | goto fail; |
fe8db0c5 | 3533 | |
5512a963 | 3534 | (void) fchmod(fileno(f), 0644); |
fe8db0c5 TG |
3535 | |
3536 | fprintf(f, | |
3537 | "# This is private data. Do not parse.\n" | |
deb2e523 | 3538 | "ADMIN_STATE=%s\n" |
6dcaa6f5 TG |
3539 | "OPER_STATE=%s\n", |
3540 | admin_state, oper_state); | |
fe8db0c5 | 3541 | |
bcb7a07e | 3542 | if (link->network) { |
fe0e16db YW |
3543 | char **dhcp6_domains = NULL, **dhcp_domains = NULL; |
3544 | const char *dhcp_domainname = NULL, *p; | |
07bdc70d | 3545 | sd_dhcp6_lease *dhcp6_lease = NULL; |
5512a963 | 3546 | unsigned j; |
fe0e16db | 3547 | bool space; |
07bdc70d | 3548 | |
c1a38904 MTL |
3549 | fprintf(f, "REQUIRED_FOR_ONLINE=%s\n", |
3550 | yes_no(link->network->required_for_online)); | |
3551 | ||
4ac77d63 YW |
3552 | fprintf(f, "REQUIRED_OPER_STATE_FOR_ONLINE=%s\n", |
3553 | strempty(link_operstate_to_string(link->network->required_operstate_for_online))); | |
3554 | ||
07bdc70d | 3555 | if (link->dhcp6_client) { |
4058d339 TG |
3556 | r = sd_dhcp6_client_get_lease(link->dhcp6_client, &dhcp6_lease); |
3557 | if (r < 0 && r != -ENOMSG) | |
07bdc70d PF |
3558 | log_link_debug(link, "No DHCPv6 lease"); |
3559 | } | |
b0e39c82 | 3560 | |
adc5b2e2 TG |
3561 | fprintf(f, "NETWORK_FILE=%s\n", link->network->filename); |
3562 | ||
0d536673 | 3563 | fputs("DNS=", f); |
ea352b40 | 3564 | space = false; |
5512a963 LP |
3565 | |
3566 | for (j = 0; j < link->network->n_dns; j++) { | |
3567 | _cleanup_free_ char *b = NULL; | |
3568 | ||
3569 | r = in_addr_to_string(link->network->dns[j].family, | |
3570 | &link->network->dns[j].address, &b); | |
3571 | if (r < 0) { | |
3572 | log_debug_errno(r, "Failed to format address, ignoring: %m"); | |
3573 | continue; | |
3574 | } | |
3575 | ||
3576 | if (space) | |
0d536673 LP |
3577 | fputc(' ', f); |
3578 | fputs(b, f); | |
5512a963 LP |
3579 | space = true; |
3580 | } | |
d5314fff | 3581 | |
27cb34f5 | 3582 | if (link->network->dhcp_use_dns && |
b0e39c82 TG |
3583 | link->dhcp_lease) { |
3584 | const struct in_addr *addresses; | |
3585 | ||
3586 | r = sd_dhcp_lease_get_dns(link->dhcp_lease, &addresses); | |
072320ea TH |
3587 | if (r > 0) |
3588 | if (serialize_in_addrs(f, addresses, r, space, in4_addr_is_non_local) > 0) | |
3589 | space = true; | |
07bdc70d PF |
3590 | } |
3591 | ||
27cb34f5 | 3592 | if (link->network->dhcp_use_dns && dhcp6_lease) { |
07bdc70d PF |
3593 | struct in6_addr *in6_addrs; |
3594 | ||
3595 | r = sd_dhcp6_lease_get_dns(dhcp6_lease, &in6_addrs); | |
3596 | if (r > 0) { | |
3597 | if (space) | |
0d536673 | 3598 | fputc(' ', f); |
07bdc70d | 3599 | serialize_in6_addrs(f, in6_addrs, r); |
1e7a0e21 LP |
3600 | space = true; |
3601 | } | |
3602 | } | |
3603 | ||
3604 | /* Make sure to flush out old entries before we use the NDISC data */ | |
3605 | ndisc_vacuum(link); | |
3606 | ||
b296797f | 3607 | if (link->network->ipv6_accept_ra_use_dns && link->ndisc_rdnss) { |
1e7a0e21 LP |
3608 | NDiscRDNSS *dd; |
3609 | ||
3610 | SET_FOREACH(dd, link->ndisc_rdnss, i) { | |
3611 | if (space) | |
0d536673 | 3612 | fputc(' ', f); |
1e7a0e21 LP |
3613 | |
3614 | serialize_in6_addrs(f, &dd->address, 1); | |
3615 | space = true; | |
b0e39c82 TG |
3616 | } |
3617 | } | |
3618 | ||
0d536673 | 3619 | fputc('\n', f); |
b0e39c82 | 3620 | |
0d536673 | 3621 | fputs("NTP=", f); |
ea352b40 | 3622 | space = false; |
3df9bec5 | 3623 | fputstrv(f, link->network->ntp, NULL, &space); |
d5314fff | 3624 | |
27cb34f5 | 3625 | if (link->network->dhcp_use_ntp && |
b0e39c82 TG |
3626 | link->dhcp_lease) { |
3627 | const struct in_addr *addresses; | |
3628 | ||
3629 | r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &addresses); | |
072320ea TH |
3630 | if (r > 0) |
3631 | if (serialize_in_addrs(f, addresses, r, space, in4_addr_is_non_local) > 0) | |
3632 | space = true; | |
07bdc70d PF |
3633 | } |
3634 | ||
27cb34f5 | 3635 | if (link->network->dhcp_use_ntp && dhcp6_lease) { |
07bdc70d PF |
3636 | struct in6_addr *in6_addrs; |
3637 | char **hosts; | |
07bdc70d PF |
3638 | |
3639 | r = sd_dhcp6_lease_get_ntp_addrs(dhcp6_lease, | |
3640 | &in6_addrs); | |
3641 | if (r > 0) { | |
3642 | if (space) | |
0d536673 | 3643 | fputc(' ', f); |
07bdc70d PF |
3644 | serialize_in6_addrs(f, in6_addrs, r); |
3645 | space = true; | |
3646 | } | |
3647 | ||
3648 | r = sd_dhcp6_lease_get_ntp_fqdn(dhcp6_lease, &hosts); | |
3df9bec5 LP |
3649 | if (r > 0) |
3650 | fputstrv(f, hosts, NULL, &space); | |
b0e39c82 TG |
3651 | } |
3652 | ||
0d536673 | 3653 | fputc('\n', f); |
bd8f6538 | 3654 | |
b2a81c0b | 3655 | if (link->network->dhcp_use_domains != DHCP_USE_DOMAINS_NO) { |
b85bc551 | 3656 | if (link->dhcp_lease) { |
b2a81c0b | 3657 | (void) sd_dhcp_lease_get_domainname(link->dhcp_lease, &dhcp_domainname); |
b85bc551 DW |
3658 | (void) sd_dhcp_lease_get_search_domains(link->dhcp_lease, &dhcp_domains); |
3659 | } | |
b2a81c0b LP |
3660 | if (dhcp6_lease) |
3661 | (void) sd_dhcp6_lease_get_domains(dhcp6_lease, &dhcp6_domains); | |
07bdc70d PF |
3662 | } |
3663 | ||
fe0e16db YW |
3664 | fputs("DOMAINS=", f); |
3665 | space = false; | |
3666 | ORDERED_SET_FOREACH(p, link->network->search_domains, i) | |
3667 | fputs_with_space(f, p, NULL, &space); | |
07bdc70d | 3668 | |
1e7a0e21 LP |
3669 | if (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_YES) { |
3670 | NDiscDNSSL *dd; | |
9b4d1882 | 3671 | |
1e7a0e21 LP |
3672 | if (dhcp_domainname) |
3673 | fputs_with_space(f, dhcp_domainname, NULL, &space); | |
b85bc551 DW |
3674 | if (dhcp_domains) |
3675 | fputstrv(f, dhcp_domains, NULL, &space); | |
1e7a0e21 LP |
3676 | if (dhcp6_domains) |
3677 | fputstrv(f, dhcp6_domains, NULL, &space); | |
3678 | ||
3679 | SET_FOREACH(dd, link->ndisc_dnssl, i) | |
3680 | fputs_with_space(f, NDISC_DNSSL_DOMAIN(dd), NULL, &space); | |
3681 | } | |
b2a81c0b | 3682 | |
0d536673 | 3683 | fputc('\n', f); |
6192b846 | 3684 | |
fe0e16db YW |
3685 | fputs("ROUTE_DOMAINS=", f); |
3686 | space = false; | |
3687 | ORDERED_SET_FOREACH(p, link->network->route_domains, i) | |
3688 | fputs_with_space(f, p, NULL, &space); | |
b2a81c0b | 3689 | |
1e7a0e21 LP |
3690 | if (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_ROUTE) { |
3691 | NDiscDNSSL *dd; | |
b2a81c0b | 3692 | |
1e7a0e21 LP |
3693 | if (dhcp_domainname) |
3694 | fputs_with_space(f, dhcp_domainname, NULL, &space); | |
b85bc551 DW |
3695 | if (dhcp_domains) |
3696 | fputstrv(f, dhcp_domains, NULL, &space); | |
1e7a0e21 LP |
3697 | if (dhcp6_domains) |
3698 | fputstrv(f, dhcp6_domains, NULL, &space); | |
3699 | ||
3700 | SET_FOREACH(dd, link->ndisc_dnssl, i) | |
3701 | fputs_with_space(f, NDISC_DNSSL_DOMAIN(dd), NULL, &space); | |
3702 | } | |
b2a81c0b | 3703 | |
0d536673 | 3704 | fputc('\n', f); |
67272d15 | 3705 | |
3c9b8860 | 3706 | fprintf(f, "LLMNR=%s\n", |
a7e5da6e | 3707 | resolve_support_to_string(link->network->llmnr)); |
aaa297d4 LP |
3708 | fprintf(f, "MDNS=%s\n", |
3709 | resolve_support_to_string(link->network->mdns)); | |
7ece6f58 LP |
3710 | if (link->network->dns_default_route >= 0) |
3711 | fprintf(f, "DNS_DEFAULT_ROUTE=%s\n", yes_no(link->network->dns_default_route)); | |
e7780c8d | 3712 | |
c9299be2 IT |
3713 | if (link->network->dns_over_tls_mode != _DNS_OVER_TLS_MODE_INVALID) |
3714 | fprintf(f, "DNS_OVER_TLS=%s\n", | |
3715 | dns_over_tls_mode_to_string(link->network->dns_over_tls_mode)); | |
d050561a | 3716 | |
ad6c0475 LP |
3717 | if (link->network->dnssec_mode != _DNSSEC_MODE_INVALID) |
3718 | fprintf(f, "DNSSEC=%s\n", | |
3719 | dnssec_mode_to_string(link->network->dnssec_mode)); | |
3720 | ||
8a516214 LP |
3721 | if (!set_isempty(link->network->dnssec_negative_trust_anchors)) { |
3722 | const char *n; | |
3723 | ||
0d536673 | 3724 | fputs("DNSSEC_NTA=", f); |
8a516214 | 3725 | space = false; |
d390f8ef LP |
3726 | SET_FOREACH(n, link->network->dnssec_negative_trust_anchors, i) |
3727 | fputs_with_space(f, n, NULL, &space); | |
0d536673 | 3728 | fputc('\n', f); |
8a516214 LP |
3729 | } |
3730 | ||
0d536673 | 3731 | fputs("ADDRESSES=", f); |
e7780c8d TG |
3732 | space = false; |
3733 | SET_FOREACH(a, link->addresses, i) { | |
3734 | _cleanup_free_ char *address_str = NULL; | |
3735 | ||
3736 | r = in_addr_to_string(a->family, &a->in_addr, &address_str); | |
3737 | if (r < 0) | |
3738 | goto fail; | |
3739 | ||
e7780c8d TG |
3740 | fprintf(f, "%s%s/%u", space ? " " : "", address_str, a->prefixlen); |
3741 | space = true; | |
3742 | } | |
0d536673 | 3743 | fputc('\n', f); |
c1eb9872 | 3744 | |
0d536673 | 3745 | fputs("ROUTES=", f); |
c1eb9872 TG |
3746 | space = false; |
3747 | SET_FOREACH(route, link->routes, i) { | |
3748 | _cleanup_free_ char *route_str = NULL; | |
3749 | ||
3750 | r = in_addr_to_string(route->family, &route->dst, &route_str); | |
3751 | if (r < 0) | |
3752 | goto fail; | |
3753 | ||
ecc3f340 ZJS |
3754 | fprintf(f, "%s%s/%hhu/%hhu/%"PRIu32"/%"PRIu32"/"USEC_FMT, |
3755 | space ? " " : "", route_str, | |
f833694d | 3756 | route->dst_prefixlen, route->tos, route->priority, route->table, route->lifetime); |
c1eb9872 TG |
3757 | space = true; |
3758 | } | |
3759 | ||
0d536673 | 3760 | fputc('\n', f); |
bcb7a07e | 3761 | } |
7374f9d8 | 3762 | |
b295beea LP |
3763 | print_link_hashmap(f, "CARRIER_BOUND_TO=", link->bound_to_links); |
3764 | print_link_hashmap(f, "CARRIER_BOUND_BY=", link->bound_by_links); | |
0d4ad91d | 3765 | |
8eb9058d | 3766 | if (link->dhcp_lease) { |
0bc70f1d | 3767 | struct in_addr address; |
8eb9058d LP |
3768 | const char *tz = NULL; |
3769 | ||
0bc70f1d TG |
3770 | assert(link->network); |
3771 | ||
8eb9058d LP |
3772 | r = sd_dhcp_lease_get_timezone(link->dhcp_lease, &tz); |
3773 | if (r >= 0) | |
3774 | fprintf(f, "TIMEZONE=%s\n", tz); | |
8eb9058d | 3775 | |
0bc70f1d TG |
3776 | r = sd_dhcp_lease_get_address(link->dhcp_lease, &address); |
3777 | if (r >= 0) { | |
0d536673 | 3778 | fputs("DHCP4_ADDRESS=", f); |
072320ea | 3779 | serialize_in_addrs(f, &address, 1, false, NULL); |
0d536673 | 3780 | fputc('\n', f); |
0bc70f1d | 3781 | } |
d9876a52 | 3782 | |
bd91b83e | 3783 | r = dhcp_lease_save(link->dhcp_lease, link->lease_file); |
fe8db0c5 | 3784 | if (r < 0) |
c2d6bd61 | 3785 | goto fail; |
fe8db0c5 | 3786 | |
7374f9d8 | 3787 | fprintf(f, |
b0e39c82 TG |
3788 | "DHCP_LEASE=%s\n", |
3789 | link->lease_file); | |
deb2e523 | 3790 | } else |
6990fb6b | 3791 | (void) unlink(link->lease_file); |
fe8db0c5 | 3792 | |
0bc70f1d TG |
3793 | if (link->ipv4ll) { |
3794 | struct in_addr address; | |
3795 | ||
3796 | r = sd_ipv4ll_get_address(link->ipv4ll, &address); | |
3797 | if (r >= 0) { | |
0d536673 | 3798 | fputs("IPV4LL_ADDRESS=", f); |
072320ea | 3799 | serialize_in_addrs(f, &address, 1, false, NULL); |
0d536673 | 3800 | fputc('\n', f); |
0bc70f1d TG |
3801 | } |
3802 | } | |
3803 | ||
c2d6bd61 LP |
3804 | r = fflush_and_check(f); |
3805 | if (r < 0) | |
3806 | goto fail; | |
fe8db0c5 | 3807 | |
c2d6bd61 | 3808 | if (rename(temp_path, link->state_file) < 0) { |
fe8db0c5 | 3809 | r = -errno; |
c2d6bd61 | 3810 | goto fail; |
fe8db0c5 TG |
3811 | } |
3812 | ||
c2d6bd61 | 3813 | return 0; |
dacd6cee | 3814 | |
c2d6bd61 | 3815 | fail: |
6a7a4e4d | 3816 | (void) unlink(link->state_file); |
6a7a4e4d LP |
3817 | if (temp_path) |
3818 | (void) unlink(temp_path); | |
3819 | ||
dacd6cee | 3820 | return log_link_error_errno(link, r, "Failed to save link data to %s: %m", link->state_file); |
fe8db0c5 TG |
3821 | } |
3822 | ||
84de38c5 TG |
3823 | /* The serialized state in /run is no longer up-to-date. */ |
3824 | void link_dirty(Link *link) { | |
3825 | int r; | |
3826 | ||
3827 | assert(link); | |
3828 | ||
0c241a37 SS |
3829 | /* mark manager dirty as link is dirty */ |
3830 | manager_dirty(link->manager); | |
3831 | ||
84de38c5 TG |
3832 | r = set_ensure_allocated(&link->manager->dirty_links, NULL); |
3833 | if (r < 0) | |
3834 | /* allocation errors are ignored */ | |
3835 | return; | |
3836 | ||
3837 | r = set_put(link->manager->dirty_links, link); | |
0c241a37 SS |
3838 | if (r <= 0) |
3839 | /* don't take another ref if the link was already dirty */ | |
84de38c5 TG |
3840 | return; |
3841 | ||
3842 | link_ref(link); | |
3843 | } | |
3844 | ||
3845 | /* The serialized state in /run is up-to-date */ | |
3846 | void link_clean(Link *link) { | |
3847 | assert(link); | |
3848 | assert(link->manager); | |
3849 | ||
1046bf9b | 3850 | link_unref(set_remove(link->manager->dirty_links, link)); |
84de38c5 TG |
3851 | } |
3852 | ||
fe8db0c5 | 3853 | static const char* const link_state_table[_LINK_STATE_MAX] = { |
8434fd5c | 3854 | [LINK_STATE_PENDING] = "pending", |
bd08ce56 | 3855 | [LINK_STATE_INITIALIZED] = "initialized", |
289e6774 | 3856 | [LINK_STATE_CONFIGURING] = "configuring", |
fe8db0c5 | 3857 | [LINK_STATE_CONFIGURED] = "configured", |
57bd6899 | 3858 | [LINK_STATE_UNMANAGED] = "unmanaged", |
fe8db0c5 | 3859 | [LINK_STATE_FAILED] = "failed", |
370e9930 | 3860 | [LINK_STATE_LINGER] = "linger", |
fe8db0c5 TG |
3861 | }; |
3862 | ||
3863 | DEFINE_STRING_TABLE_LOOKUP(link_state, LinkState); |