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