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