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