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