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