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