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