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