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