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