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