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