]>
Commit | Line | Data |
---|---|---|
f579559b TG |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2013 Tom Gundersen <teg@jklm.no> | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU Lesser General Public License as published by | |
10 | the Free Software Foundation; either version 2.1 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | Lesser General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU Lesser General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
22 | #include <netinet/ether.h> | |
23 | #include <linux/if.h> | |
4cc7a82c | 24 | #include <unistd.h> |
f579559b | 25 | |
f579559b | 26 | #include "util.h" |
505f8da7 | 27 | #include "virt.h" |
5a8bcb67 | 28 | #include "fileio.h" |
fe027299 | 29 | #include "socket-util.h" |
1346b1f0 | 30 | #include "bus-util.h" |
5a8bcb67 | 31 | #include "udev-util.h" |
c6f7c917 | 32 | #include "network-internal.h" |
5a8bcb67 LP |
33 | #include "networkd-link.h" |
34 | #include "networkd-netdev.h" | |
fe8db0c5 | 35 | |
a97dcc12 | 36 | bool link_dhcp6_enabled(Link *link) { |
78c958f8 TG |
37 | if (link->flags & IFF_LOOPBACK) |
38 | return false; | |
39 | ||
40 | if (!link->network) | |
41 | return false; | |
42 | ||
e0ee46f2 | 43 | return link->network->dhcp & ADDRESS_FAMILY_IPV6; |
78c958f8 TG |
44 | } |
45 | ||
a97dcc12 | 46 | bool link_dhcp4_enabled(Link *link) { |
78c958f8 TG |
47 | if (link->flags & IFF_LOOPBACK) |
48 | return false; | |
49 | ||
50 | if (!link->network) | |
51 | return false; | |
52 | ||
e0ee46f2 | 53 | return link->network->dhcp & ADDRESS_FAMILY_IPV4; |
78c958f8 TG |
54 | } |
55 | ||
a97dcc12 | 56 | bool link_dhcp4_server_enabled(Link *link) { |
78c958f8 TG |
57 | if (link->flags & IFF_LOOPBACK) |
58 | return false; | |
59 | ||
60 | if (!link->network) | |
61 | return false; | |
62 | ||
63 | return link->network->dhcp_server; | |
64 | } | |
65 | ||
a97dcc12 | 66 | bool link_ipv4ll_enabled(Link *link) { |
78c958f8 TG |
67 | if (link->flags & IFF_LOOPBACK) |
68 | return false; | |
69 | ||
70 | if (!link->network) | |
71 | return false; | |
72 | ||
e0ee46f2 | 73 | return link->network->link_local & ADDRESS_FAMILY_IPV4; |
d0d6a4cd TG |
74 | } |
75 | ||
76 | bool link_ipv6ll_enabled(Link *link) { | |
77 | if (link->flags & IFF_LOOPBACK) | |
78 | return false; | |
79 | ||
80 | if (!link->network) | |
81 | return false; | |
82 | ||
e0ee46f2 | 83 | return link->network->link_local & ADDRESS_FAMILY_IPV6; |
78c958f8 TG |
84 | } |
85 | ||
a97dcc12 | 86 | bool link_lldp_enabled(Link *link) { |
ce43e484 SS |
87 | if (link->flags & IFF_LOOPBACK) |
88 | return false; | |
89 | ||
90 | if (!link->network) | |
91 | return false; | |
92 | ||
5a8bcb67 | 93 | if (link->network->bridge) |
ce43e484 SS |
94 | return false; |
95 | ||
96 | return link->network->lldp; | |
97 | } | |
98 | ||
769d324c | 99 | static bool link_ipv4_forward_enabled(Link *link) { |
5a8bcb67 LP |
100 | if (link->flags & IFF_LOOPBACK) |
101 | return false; | |
102 | ||
103 | if (!link->network) | |
104 | return false; | |
105 | ||
e0ee46f2 | 106 | return link->network->ip_forward & ADDRESS_FAMILY_IPV4; |
769d324c LP |
107 | } |
108 | ||
109 | static bool link_ipv6_forward_enabled(Link *link) { | |
110 | if (link->flags & IFF_LOOPBACK) | |
111 | return false; | |
112 | ||
113 | if (!link->network) | |
114 | return false; | |
115 | ||
e0ee46f2 | 116 | return link->network->ip_forward & ADDRESS_FAMILY_IPV6; |
5a8bcb67 LP |
117 | } |
118 | ||
51d18171 TG |
119 | #define FLAG_STRING(string, flag, old, new) \ |
120 | (((old ^ new) & flag) \ | |
121 | ? ((old & flag) ? (" -" string) : (" +" string)) \ | |
122 | : "") | |
123 | ||
124 | static int link_update_flags(Link *link, sd_rtnl_message *m) { | |
125 | unsigned flags, unknown_flags_added, unknown_flags_removed, unknown_flags; | |
126 | uint8_t operstate; | |
127 | int r; | |
128 | ||
129 | assert(link); | |
130 | ||
131 | r = sd_rtnl_message_link_get_flags(m, &flags); | |
6a7a4e4d LP |
132 | if (r < 0) |
133 | return log_link_warning_errno(link, r, "Could not get link flags: %m"); | |
51d18171 TG |
134 | |
135 | r = sd_rtnl_message_read_u8(m, IFLA_OPERSTATE, &operstate); | |
136 | if (r < 0) | |
137 | /* if we got a message without operstate, take it to mean | |
138 | the state was unchanged */ | |
139 | operstate = link->kernel_operstate; | |
140 | ||
141 | if ((link->flags == flags) && (link->kernel_operstate == operstate)) | |
142 | return 0; | |
143 | ||
144 | if (link->flags != flags) { | |
6a7a4e4d | 145 | 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 |
146 | FLAG_STRING("LOOPBACK", IFF_LOOPBACK, link->flags, flags), |
147 | FLAG_STRING("MASTER", IFF_MASTER, link->flags, flags), | |
148 | FLAG_STRING("SLAVE", IFF_SLAVE, link->flags, flags), | |
149 | FLAG_STRING("UP", IFF_UP, link->flags, flags), | |
150 | FLAG_STRING("DORMANT", IFF_DORMANT, link->flags, flags), | |
151 | FLAG_STRING("LOWER_UP", IFF_LOWER_UP, link->flags, flags), | |
152 | FLAG_STRING("RUNNING", IFF_RUNNING, link->flags, flags), | |
153 | FLAG_STRING("MULTICAST", IFF_MULTICAST, link->flags, flags), | |
154 | FLAG_STRING("BROADCAST", IFF_BROADCAST, link->flags, flags), | |
155 | FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT, link->flags, flags), | |
156 | FLAG_STRING("PROMISC", IFF_PROMISC, link->flags, flags), | |
157 | FLAG_STRING("ALLMULTI", IFF_ALLMULTI, link->flags, flags), | |
158 | FLAG_STRING("PORTSEL", IFF_PORTSEL, link->flags, flags), | |
159 | FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA, link->flags, flags), | |
160 | FLAG_STRING("DYNAMIC", IFF_DYNAMIC, link->flags, flags), | |
161 | FLAG_STRING("NOARP", IFF_NOARP, link->flags, flags), | |
162 | FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS, link->flags, flags), | |
163 | FLAG_STRING("DEBUG", IFF_DEBUG, link->flags, flags), | |
164 | FLAG_STRING("ECHO", IFF_ECHO, link->flags, flags)); | |
165 | ||
166 | unknown_flags = ~(IFF_LOOPBACK | IFF_MASTER | IFF_SLAVE | IFF_UP | | |
167 | IFF_DORMANT | IFF_LOWER_UP | IFF_RUNNING | | |
168 | IFF_MULTICAST | IFF_BROADCAST | IFF_POINTOPOINT | | |
169 | IFF_PROMISC | IFF_ALLMULTI | IFF_PORTSEL | | |
170 | IFF_AUTOMEDIA | IFF_DYNAMIC | IFF_NOARP | | |
171 | IFF_NOTRAILERS | IFF_DEBUG | IFF_ECHO); | |
172 | unknown_flags_added = ((link->flags ^ flags) & flags & unknown_flags); | |
173 | unknown_flags_removed = ((link->flags ^ flags) & link->flags & unknown_flags); | |
174 | ||
175 | /* link flags are currently at most 18 bits, let's align to | |
176 | * printing 20 */ | |
177 | if (unknown_flags_added) | |
79008bdd | 178 | log_link_debug(link, |
6a7a4e4d | 179 | "Unknown link flags gained: %#.5x (ignoring)", |
51d18171 TG |
180 | unknown_flags_added); |
181 | ||
182 | if (unknown_flags_removed) | |
79008bdd | 183 | log_link_debug(link, |
6a7a4e4d | 184 | "Unknown link flags lost: %#.5x (ignoring)", |
51d18171 TG |
185 | unknown_flags_removed); |
186 | } | |
187 | ||
188 | link->flags = flags; | |
189 | link->kernel_operstate = operstate; | |
190 | ||
191 | link_save(link); | |
192 | ||
193 | return 0; | |
194 | } | |
195 | ||
505f8da7 | 196 | static int link_new(Manager *manager, sd_rtnl_message *message, Link **ret) { |
14b746f7 | 197 | _cleanup_link_unref_ Link *link = NULL; |
505f8da7 | 198 | uint16_t type; |
ca4e095a | 199 | const char *ifname; |
505f8da7 | 200 | int r, ifindex; |
f579559b | 201 | |
0c2f9b84 | 202 | assert(manager); |
505f8da7 | 203 | assert(message); |
f579559b TG |
204 | assert(ret); |
205 | ||
505f8da7 TG |
206 | r = sd_rtnl_message_get_type(message, &type); |
207 | if (r < 0) | |
208 | return r; | |
209 | else if (type != RTM_NEWLINK) | |
210 | return -EINVAL; | |
211 | ||
212 | r = sd_rtnl_message_link_get_ifindex(message, &ifindex); | |
213 | if (r < 0) | |
214 | return r; | |
215 | else if (ifindex <= 0) | |
216 | return -EINVAL; | |
217 | ||
218 | r = sd_rtnl_message_read_string(message, IFLA_IFNAME, &ifname); | |
219 | if (r < 0) | |
220 | return r; | |
221 | ||
f579559b TG |
222 | link = new0(Link, 1); |
223 | if (!link) | |
224 | return -ENOMEM; | |
225 | ||
14b746f7 | 226 | link->n_ref = 1; |
5a3eb5a7 | 227 | link->manager = manager; |
8434fd5c | 228 | link->state = LINK_STATE_PENDING; |
be3a09b7 | 229 | link->rtnl_extended_attrs = true; |
505f8da7 TG |
230 | link->ifindex = ifindex; |
231 | link->ifname = strdup(ifname); | |
232 | if (!link->ifname) | |
233 | return -ENOMEM; | |
f579559b | 234 | |
512922f8 TG |
235 | r = sd_rtnl_message_read_ether_addr(message, IFLA_ADDRESS, &link->mac); |
236 | if (r < 0) | |
79008bdd | 237 | log_link_debug(link, "MAC address not found for new device, continuing without"); |
512922f8 | 238 | |
3c9b8860 TG |
239 | r = asprintf(&link->state_file, "/run/systemd/netif/links/%d", |
240 | link->ifindex); | |
fe8db0c5 | 241 | if (r < 0) |
315db1a8 | 242 | return -ENOMEM; |
fe8db0c5 | 243 | |
3c9b8860 TG |
244 | r = asprintf(&link->lease_file, "/run/systemd/netif/leases/%d", |
245 | link->ifindex); | |
68a8723c TG |
246 | if (r < 0) |
247 | return -ENOMEM; | |
248 | ||
49699bac SS |
249 | r = asprintf(&link->lldp_file, "/run/systemd/netif/lldp/%d", |
250 | link->ifindex); | |
251 | if (r < 0) | |
252 | return -ENOMEM; | |
253 | ||
254 | ||
d5099efc | 255 | r = hashmap_ensure_allocated(&manager->links, NULL); |
ae06ab10 TG |
256 | if (r < 0) |
257 | return r; | |
258 | ||
259 | r = hashmap_put(manager->links, INT_TO_PTR(link->ifindex), link); | |
f579559b TG |
260 | if (r < 0) |
261 | return r; | |
262 | ||
51d18171 TG |
263 | r = link_update_flags(link, message); |
264 | if (r < 0) | |
265 | return r; | |
266 | ||
f579559b TG |
267 | *ret = link; |
268 | link = NULL; | |
269 | ||
270 | return 0; | |
271 | } | |
272 | ||
14b746f7 | 273 | static void link_free(Link *link) { |
428fd0a7 | 274 | Address *address; |
0d4ad91d AR |
275 | Iterator i; |
276 | Link *carrier; | |
428fd0a7 | 277 | |
f579559b TG |
278 | if (!link) |
279 | return; | |
280 | ||
428fd0a7 TG |
281 | while ((address = link->addresses)) { |
282 | LIST_REMOVE(addresses, link->addresses, address); | |
283 | address_free(address); | |
284 | } | |
285 | ||
11bf3cce LP |
286 | while ((address = link->pool_addresses)) { |
287 | LIST_REMOVE(addresses, link->pool_addresses, address); | |
288 | address_free(address); | |
289 | } | |
290 | ||
bfcdba8d | 291 | sd_dhcp_server_unref(link->dhcp_server); |
e5b04c8d | 292 | sd_dhcp_client_unref(link->dhcp_client); |
a6cc569e | 293 | sd_dhcp_lease_unref(link->dhcp_lease); |
f5be5601 | 294 | |
68a8723c TG |
295 | free(link->lease_file); |
296 | ||
07e10d1a TG |
297 | sd_lldp_free(link->lldp); |
298 | ||
49699bac SS |
299 | free(link->lldp_file); |
300 | ||
56cd007a | 301 | sd_ipv4ll_unref(link->ipv4ll); |
4138fb2c | 302 | sd_dhcp6_client_unref(link->dhcp6_client); |
6b66097b | 303 | sd_icmp6_nd_unref(link->icmp6_router_discovery); |
4138fb2c | 304 | |
28aeb07f | 305 | if (link->manager) |
ae06ab10 | 306 | hashmap_remove(link->manager->links, INT_TO_PTR(link->ifindex)); |
f579559b | 307 | |
c166a070 | 308 | free(link->ifname); |
68a8723c | 309 | |
fe8db0c5 | 310 | free(link->state_file); |
c166a070 | 311 | |
b5db00e5 UTL |
312 | udev_device_unref(link->udev_device); |
313 | ||
0d4ad91d AR |
314 | HASHMAP_FOREACH (carrier, link->bound_to_links, i) |
315 | hashmap_remove(link->bound_to_links, INT_TO_PTR(carrier->ifindex)); | |
316 | hashmap_free(link->bound_to_links); | |
317 | ||
318 | HASHMAP_FOREACH (carrier, link->bound_by_links, i) | |
319 | hashmap_remove(link->bound_by_links, INT_TO_PTR(carrier->ifindex)); | |
320 | hashmap_free(link->bound_by_links); | |
321 | ||
f579559b TG |
322 | free(link); |
323 | } | |
324 | ||
14b746f7 TG |
325 | Link *link_unref(Link *link) { |
326 | if (link && (-- link->n_ref <= 0)) | |
327 | link_free(link); | |
328 | ||
329 | return NULL; | |
330 | } | |
331 | ||
332 | Link *link_ref(Link *link) { | |
333 | if (link) | |
334 | assert_se(++ link->n_ref >= 2); | |
335 | ||
336 | return link; | |
337 | } | |
338 | ||
11a7f229 TG |
339 | int link_get(Manager *m, int ifindex, Link **ret) { |
340 | Link *link; | |
11a7f229 TG |
341 | |
342 | assert(m); | |
11a7f229 TG |
343 | assert(ifindex); |
344 | assert(ret); | |
345 | ||
ae06ab10 | 346 | link = hashmap_get(m->links, INT_TO_PTR(ifindex)); |
11a7f229 TG |
347 | if (!link) |
348 | return -ENODEV; | |
349 | ||
350 | *ret = link; | |
351 | ||
352 | return 0; | |
353 | } | |
354 | ||
e331e246 TG |
355 | static void link_set_state(Link *link, LinkState state) { |
356 | assert(link); | |
357 | ||
358 | if (link->state == state) | |
359 | return; | |
360 | ||
361 | link->state = state; | |
362 | ||
363 | link_send_changed(link, "AdministrativeState", NULL); | |
364 | ||
365 | return; | |
366 | } | |
367 | ||
57bd6899 TG |
368 | static void link_enter_unmanaged(Link *link) { |
369 | assert(link); | |
370 | ||
6a7a4e4d | 371 | log_link_debug(link, "Unmanaged"); |
57bd6899 | 372 | |
e331e246 | 373 | link_set_state(link, LINK_STATE_UNMANAGED); |
57bd6899 TG |
374 | |
375 | link_save(link); | |
376 | } | |
377 | ||
111bb8f9 TG |
378 | static int link_stop_clients(Link *link) { |
379 | int r = 0, k; | |
380 | ||
381 | assert(link); | |
382 | assert(link->manager); | |
383 | assert(link->manager->event); | |
384 | ||
385 | if (!link->network) | |
386 | return 0; | |
387 | ||
ba179154 | 388 | if (link->dhcp_client) { |
111bb8f9 | 389 | k = sd_dhcp_client_stop(link->dhcp_client); |
6a7a4e4d LP |
390 | if (k < 0) |
391 | r = log_link_warning_errno(link, r, "Could not stop DHCPv4 client: %m"); | |
111bb8f9 TG |
392 | } |
393 | ||
ba179154 | 394 | if (link->ipv4ll) { |
111bb8f9 | 395 | k = sd_ipv4ll_stop(link->ipv4ll); |
6a7a4e4d LP |
396 | if (k < 0) |
397 | r = log_link_warning_errno(link, r, "Could not stop IPv4 link-local: %m"); | |
dd43110f TG |
398 | } |
399 | ||
ba179154 | 400 | if(link->icmp6_router_discovery) { |
1873a3d3 PF |
401 | if (link->dhcp6_client) { |
402 | k = sd_dhcp6_client_stop(link->dhcp6_client); | |
6a7a4e4d LP |
403 | if (k < 0) |
404 | r = log_link_warning_errno(link, r, "Could not stop DHCPv6 client: %m"); | |
1873a3d3 | 405 | } |
4138fb2c | 406 | |
1873a3d3 | 407 | k = sd_icmp6_nd_stop(link->icmp6_router_discovery); |
6a7a4e4d LP |
408 | if (k < 0) |
409 | r = log_link_warning_errno(link, r, "Could not stop ICMPv6 router discovery: %m"); | |
4138fb2c PF |
410 | } |
411 | ||
ce43e484 | 412 | if (link->lldp) { |
ce43e484 | 413 | k = sd_lldp_stop(link->lldp); |
6a7a4e4d LP |
414 | if (k < 0) |
415 | r = log_link_warning_errno(link, r, "Could not stop LLDP: %m"); | |
ce43e484 SS |
416 | } |
417 | ||
111bb8f9 TG |
418 | return r; |
419 | } | |
420 | ||
b22d8a00 | 421 | void link_enter_failed(Link *link) { |
ef1ba606 | 422 | assert(link); |
f882c247 | 423 | |
370e9930 | 424 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
2139694e TG |
425 | return; |
426 | ||
6a7a4e4d | 427 | log_link_warning(link, "Failed"); |
449f7554 | 428 | |
e331e246 | 429 | link_set_state(link, LINK_STATE_FAILED); |
fe8db0c5 | 430 | |
111bb8f9 TG |
431 | link_stop_clients(link); |
432 | ||
fe8db0c5 | 433 | link_save(link); |
f882c247 TG |
434 | } |
435 | ||
4f434938 LP |
436 | static Address* link_find_dhcp_server_address(Link *link) { |
437 | Address *address; | |
438 | ||
439 | assert(link); | |
440 | assert(link->network); | |
441 | ||
d4cdbea5 | 442 | /* The first statically configured address if there is any */ |
4f434938 LP |
443 | LIST_FOREACH(addresses, address, link->network->static_addresses) { |
444 | ||
445 | if (address->family != AF_INET) | |
446 | continue; | |
447 | ||
af93291c | 448 | if (in_addr_is_null(address->family, &address->in_addr)) |
4f434938 LP |
449 | continue; |
450 | ||
451 | return address; | |
452 | } | |
453 | ||
454 | /* If that didn't work, find a suitable address we got from the pool */ | |
455 | LIST_FOREACH(addresses, address, link->pool_addresses) { | |
456 | if (address->family != AF_INET) | |
457 | continue; | |
458 | ||
459 | return address; | |
460 | } | |
461 | ||
462 | return NULL; | |
463 | } | |
464 | ||
dd43110f | 465 | static int link_enter_configured(Link *link) { |
dd43110f TG |
466 | assert(link); |
467 | assert(link->network); | |
468 | assert(link->state == LINK_STATE_SETTING_ROUTES); | |
469 | ||
6a7a4e4d | 470 | log_link_info(link, "Configured"); |
dd43110f | 471 | |
e331e246 | 472 | link_set_state(link, LINK_STATE_CONFIGURED); |
dd43110f TG |
473 | |
474 | link_save(link); | |
475 | ||
476 | return 0; | |
477 | } | |
478 | ||
3c9b8860 TG |
479 | void link_client_handler(Link *link) { |
480 | assert(link); | |
481 | assert(link->network); | |
482 | ||
483 | if (!link->static_configured) | |
484 | return; | |
485 | ||
78c958f8 | 486 | if (link_ipv4ll_enabled(link)) |
3c9b8860 TG |
487 | if (!link->ipv4ll_address || |
488 | !link->ipv4ll_route) | |
489 | return; | |
490 | ||
78c958f8 | 491 | if (link_dhcp4_enabled(link) && !link->dhcp4_configured) |
3c9b8860 TG |
492 | return; |
493 | ||
9fdaa992 TG |
494 | if (link->state != LINK_STATE_CONFIGURED) |
495 | link_enter_configured(link); | |
3c9b8860 TG |
496 | |
497 | return; | |
498 | } | |
499 | ||
f882c247 | 500 | static int route_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { |
5da8149f | 501 | _cleanup_link_unref_ Link *link = userdata; |
f882c247 TG |
502 | int r; |
503 | ||
3c9b8860 | 504 | assert(link->link_messages > 0); |
370e9930 TG |
505 | assert(IN_SET(link->state, LINK_STATE_SETTING_ADDRESSES, |
506 | LINK_STATE_SETTING_ROUTES, LINK_STATE_FAILED, | |
507 | LINK_STATE_LINGER)); | |
f882c247 | 508 | |
3c9b8860 | 509 | link->link_messages --; |
f882c247 | 510 | |
77a008c0 | 511 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
f882c247 TG |
512 | return 1; |
513 | ||
514 | r = sd_rtnl_message_get_errno(m); | |
c166a070 | 515 | if (r < 0 && r != -EEXIST) |
6a7a4e4d | 516 | log_link_warning_errno(link, r, "%-*s: could not set route: %m", IFNAMSIZ, link->ifname); |
f882c247 | 517 | |
3c9b8860 | 518 | if (link->link_messages == 0) { |
6a7a4e4d | 519 | log_link_debug(link, "Routes set"); |
3c9b8860 TG |
520 | link->static_configured = true; |
521 | link_client_handler(link); | |
dd3efc09 | 522 | } |
f882c247 TG |
523 | |
524 | return 1; | |
525 | } | |
526 | ||
527 | static int link_enter_set_routes(Link *link) { | |
a6cc569e | 528 | Route *rt; |
f882c247 TG |
529 | int r; |
530 | ||
531 | assert(link); | |
532 | assert(link->network); | |
ef1ba606 | 533 | assert(link->state == LINK_STATE_SETTING_ADDRESSES); |
f882c247 | 534 | |
e331e246 | 535 | link_set_state(link, LINK_STATE_SETTING_ROUTES); |
f882c247 | 536 | |
3d3d4255 | 537 | LIST_FOREACH(routes, rt, link->network->static_routes) { |
a6cc569e | 538 | r = route_configure(rt, link, &route_handler); |
dd3efc09 | 539 | if (r < 0) { |
6a7a4e4d | 540 | log_link_warning_errno(link, r, "Could not set routes: %m"); |
3c9b8860 | 541 | link_enter_failed(link); |
a6cc569e TG |
542 | return r; |
543 | } | |
544 | ||
3c9b8860 | 545 | link->link_messages ++; |
8ddbeaa2 | 546 | } |
f5be5601 | 547 | |
3c9b8860 TG |
548 | if (link->link_messages == 0) { |
549 | link->static_configured = true; | |
550 | link_client_handler(link); | |
431ca2ce | 551 | } else |
6a7a4e4d | 552 | log_link_debug(link, "Setting routes"); |
f882c247 TG |
553 | |
554 | return 0; | |
555 | } | |
556 | ||
b22d8a00 | 557 | int link_route_drop_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { |
5da8149f | 558 | _cleanup_link_unref_ Link *link = userdata; |
5c1d3fc9 UTL |
559 | int r; |
560 | ||
561 | assert(m); | |
562 | assert(link); | |
563 | assert(link->ifname); | |
564 | ||
5da8149f | 565 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
5c1d3fc9 UTL |
566 | return 1; |
567 | ||
568 | r = sd_rtnl_message_get_errno(m); | |
b90b025a | 569 | if (r < 0 && r != -ESRCH) |
6a7a4e4d | 570 | log_link_warning_errno(link, r, "%-*s: could not drop route: %m", IFNAMSIZ, link->ifname); |
5c1d3fc9 | 571 | |
5bdd314c | 572 | return 1; |
5c1d3fc9 UTL |
573 | } |
574 | ||
f882c247 | 575 | static int address_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { |
5da8149f | 576 | _cleanup_link_unref_ Link *link = userdata; |
f882c247 TG |
577 | int r; |
578 | ||
4958aee4 | 579 | assert(rtnl); |
f5be5601 TG |
580 | assert(m); |
581 | assert(link); | |
582 | assert(link->ifname); | |
3c9b8860 | 583 | assert(link->link_messages > 0); |
370e9930 TG |
584 | assert(IN_SET(link->state, LINK_STATE_SETTING_ADDRESSES, |
585 | LINK_STATE_FAILED, LINK_STATE_LINGER)); | |
f882c247 | 586 | |
3c9b8860 | 587 | link->link_messages --; |
f882c247 | 588 | |
5da8149f | 589 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
f882c247 TG |
590 | return 1; |
591 | ||
592 | r = sd_rtnl_message_get_errno(m); | |
c166a070 | 593 | if (r < 0 && r != -EEXIST) |
6a7a4e4d | 594 | log_link_warning_errno(link, r, "%-*s: could not set address: %m", IFNAMSIZ, link->ifname); |
45af44d4 TG |
595 | else if (r >= 0) |
596 | link_rtnl_process_address(rtnl, m, link->manager); | |
f882c247 | 597 | |
3c9b8860 | 598 | if (link->link_messages == 0) { |
6a7a4e4d | 599 | log_link_debug(link, "Addresses set"); |
ef1ba606 | 600 | link_enter_set_routes(link); |
dd3efc09 | 601 | } |
f882c247 TG |
602 | |
603 | return 1; | |
604 | } | |
605 | ||
606 | static int link_enter_set_addresses(Link *link) { | |
a6cc569e | 607 | Address *ad; |
f882c247 TG |
608 | int r; |
609 | ||
610 | assert(link); | |
611 | assert(link->network); | |
f5be5601 | 612 | assert(link->state != _LINK_STATE_INVALID); |
f882c247 | 613 | |
e331e246 | 614 | link_set_state(link, LINK_STATE_SETTING_ADDRESSES); |
f882c247 | 615 | |
3d3d4255 | 616 | LIST_FOREACH(addresses, ad, link->network->static_addresses) { |
a6cc569e | 617 | r = address_configure(ad, link, &address_handler); |
dd3efc09 | 618 | if (r < 0) { |
5a8bcb67 | 619 | log_link_warning_errno(link, r, "Could not set addresses: %m"); |
f5be5601 TG |
620 | link_enter_failed(link); |
621 | return r; | |
622 | } | |
623 | ||
3c9b8860 | 624 | link->link_messages ++; |
f882c247 TG |
625 | } |
626 | ||
d4cdbea5 TG |
627 | /* now that we can figure out a default address for the dhcp server, |
628 | start it */ | |
629 | if (link_dhcp4_server_enabled(link)) { | |
630 | struct in_addr pool_start; | |
631 | Address *address; | |
632 | ||
633 | address = link_find_dhcp_server_address(link); | |
634 | if (!address) { | |
6a7a4e4d | 635 | log_link_warning(link, "Failed to find suitable address for DHCPv4 server instance."); |
d4cdbea5 TG |
636 | link_enter_failed(link); |
637 | return 0; | |
638 | } | |
639 | ||
640 | r = sd_dhcp_server_set_address(link->dhcp_server, | |
641 | &address->in_addr.in, | |
642 | address->prefixlen); | |
643 | if (r < 0) | |
644 | return r; | |
645 | ||
646 | /* offer 32 addresses starting from the address following the server address */ | |
647 | pool_start.s_addr = htobe32(be32toh(address->in_addr.in.s_addr) + 1); | |
648 | r = sd_dhcp_server_set_lease_pool(link->dhcp_server, | |
649 | &pool_start, 32); | |
650 | if (r < 0) | |
651 | return r; | |
652 | ||
653 | /* TODO: | |
654 | r = sd_dhcp_server_set_router(link->dhcp_server, | |
655 | &main_address->in_addr.in); | |
656 | if (r < 0) | |
657 | return r; | |
658 | ||
659 | r = sd_dhcp_server_set_prefixlen(link->dhcp_server, | |
660 | main_address->prefixlen); | |
661 | if (r < 0) | |
662 | return r; | |
663 | */ | |
664 | ||
665 | r = sd_dhcp_server_start(link->dhcp_server); | |
666 | if (r < 0) { | |
6a7a4e4d | 667 | log_link_warning_errno(link, r, "Could not start DHCPv4 server instance: %m"); |
d4cdbea5 TG |
668 | |
669 | link_enter_failed(link); | |
670 | ||
671 | return 0; | |
672 | } | |
673 | ||
6a7a4e4d | 674 | log_link_debug(link, "Offering DHCPv4 leases"); |
d4cdbea5 TG |
675 | } |
676 | ||
6a7a4e4d | 677 | if (link->link_messages == 0) |
431ca2ce | 678 | link_enter_set_routes(link); |
6a7a4e4d LP |
679 | else |
680 | log_link_debug(link, "Setting addresses"); | |
431ca2ce | 681 | |
f882c247 TG |
682 | return 0; |
683 | } | |
684 | ||
b22d8a00 | 685 | int link_address_drop_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { |
5da8149f | 686 | _cleanup_link_unref_ Link *link = userdata; |
ff254138 TG |
687 | int r; |
688 | ||
689 | assert(m); | |
690 | assert(link); | |
691 | assert(link->ifname); | |
692 | ||
5da8149f | 693 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
ff254138 TG |
694 | return 1; |
695 | ||
696 | r = sd_rtnl_message_get_errno(m); | |
b90b025a | 697 | if (r < 0 && r != -EADDRNOTAVAIL) |
6a7a4e4d | 698 | log_link_warning_errno(link, r, "%-*s: could not drop address: %m", IFNAMSIZ, link->ifname); |
ff254138 | 699 | |
5bdd314c | 700 | return 1; |
ff254138 TG |
701 | } |
702 | ||
a245ced0 | 703 | static int link_set_bridge_fdb(Link *const link) { |
b98b483b AR |
704 | FdbEntry *fdb_entry; |
705 | int r = 0; | |
706 | ||
707 | LIST_FOREACH(static_fdb_entries, fdb_entry, link->network->static_fdb_entries) { | |
ea6ec096 | 708 | r = fdb_entry_configure(link, fdb_entry); |
b98b483b | 709 | if(r < 0) { |
6a7a4e4d | 710 | log_link_error_errno(link, r, "Failed to add MAC entry to static MAC table: %m"); |
b98b483b AR |
711 | break; |
712 | } | |
713 | } | |
714 | ||
715 | return r; | |
716 | } | |
717 | ||
e1853b00 SS |
718 | static int link_set_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { |
719 | _cleanup_link_unref_ Link *link = userdata; | |
720 | int r; | |
721 | ||
6a7a4e4d | 722 | log_link_debug(link, "Set link"); |
e1853b00 SS |
723 | |
724 | r = sd_rtnl_message_get_errno(m); | |
725 | if (r < 0 && r != -EEXIST) { | |
f2341e0a | 726 | log_link_error_errno(link, r, "Could not join netdev: %m"); |
e1853b00 SS |
727 | link_enter_failed(link); |
728 | return 1; | |
729 | } | |
730 | ||
731 | return 0; | |
732 | } | |
733 | ||
19070062 | 734 | static int set_hostname_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { |
5da8149f | 735 | _cleanup_link_unref_ Link *link = userdata; |
1346b1f0 TG |
736 | int r; |
737 | ||
19070062 | 738 | assert(m); |
b226d99b TG |
739 | assert(link); |
740 | ||
5da8149f | 741 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
370e9930 | 742 | return 1; |
370e9930 | 743 | |
1346b1f0 | 744 | r = sd_bus_message_get_errno(m); |
70b34f5d | 745 | if (r > 0) |
6a7a4e4d | 746 | log_link_warning_errno(link, r, "Could not set hostname: %m"); |
1346b1f0 TG |
747 | |
748 | return 1; | |
749 | } | |
750 | ||
3c9b8860 | 751 | int link_set_hostname(Link *link, const char *hostname) { |
1346b1f0 TG |
752 | int r = 0; |
753 | ||
b226d99b TG |
754 | assert(link); |
755 | assert(link->manager); | |
1346b1f0 TG |
756 | assert(hostname); |
757 | ||
79008bdd | 758 | log_link_debug(link, "Setting transient hostname: '%s'", hostname); |
1346b1f0 | 759 | |
3c9b8860 TG |
760 | if (!link->manager->bus) { |
761 | /* TODO: replace by assert when we can rely on kdbus */ | |
6a7a4e4d | 762 | log_link_info(link, "Not connected to system bus, ignoring transient hostname."); |
bcbca829 TG |
763 | return 0; |
764 | } | |
765 | ||
9c34154a | 766 | r = sd_bus_call_method_async( |
b226d99b | 767 | link->manager->bus, |
9c34154a | 768 | NULL, |
1346b1f0 TG |
769 | "org.freedesktop.hostname1", |
770 | "/org/freedesktop/hostname1", | |
771 | "org.freedesktop.hostname1", | |
9c34154a UTL |
772 | "SetHostname", |
773 | set_hostname_handler, | |
774 | link, | |
775 | "sb", | |
776 | hostname, | |
777 | false); | |
1346b1f0 | 778 | |
6a7a4e4d LP |
779 | if (r < 0) |
780 | return log_link_error_errno(link, r, "Could not set transient hostname: %m"); | |
b226d99b TG |
781 | |
782 | link_ref(link); | |
1346b1f0 | 783 | |
5da8149f | 784 | return 0; |
1346b1f0 TG |
785 | } |
786 | ||
4f882b2a | 787 | static int set_mtu_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { |
5da8149f | 788 | _cleanup_link_unref_ Link *link = userdata; |
4f882b2a TG |
789 | int r; |
790 | ||
791 | assert(m); | |
792 | assert(link); | |
793 | assert(link->ifname); | |
794 | ||
5da8149f | 795 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
4f882b2a TG |
796 | return 1; |
797 | ||
798 | r = sd_rtnl_message_get_errno(m); | |
c9ccc19f | 799 | if (r < 0) |
6a7a4e4d | 800 | log_link_warning_errno(link, r, "%-*s: could not set MTU: %m", IFNAMSIZ, link->ifname); |
4f882b2a TG |
801 | |
802 | return 1; | |
803 | } | |
804 | ||
3c9b8860 | 805 | int link_set_mtu(Link *link, uint32_t mtu) { |
cf6a8911 | 806 | _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL; |
4f882b2a TG |
807 | int r; |
808 | ||
809 | assert(link); | |
810 | assert(link->manager); | |
811 | assert(link->manager->rtnl); | |
812 | ||
6a7a4e4d | 813 | log_link_debug(link, "Setting MTU: %" PRIu32, mtu); |
4f882b2a | 814 | |
6a7a4e4d LP |
815 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex); |
816 | if (r < 0) | |
817 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
4f882b2a TG |
818 | |
819 | r = sd_rtnl_message_append_u32(req, IFLA_MTU, mtu); | |
6a7a4e4d LP |
820 | if (r < 0) |
821 | return log_link_error_errno(link, r, "Could not append MTU: %m"); | |
4f882b2a | 822 | |
6a7a4e4d LP |
823 | r = sd_rtnl_call_async(link->manager->rtnl, req, set_mtu_handler, link, 0, NULL); |
824 | if (r < 0) | |
825 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
4f882b2a | 826 | |
ae941762 | 827 | link_ref(link); |
b226d99b | 828 | |
4f882b2a TG |
829 | return 0; |
830 | } | |
831 | ||
e1853b00 SS |
832 | static int link_set_bridge(Link *link) { |
833 | _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL; | |
834 | int r; | |
835 | ||
836 | assert(link); | |
837 | assert(link->network); | |
838 | ||
839 | if(link->network->cost == 0) | |
840 | return 0; | |
841 | ||
6a7a4e4d LP |
842 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex); |
843 | if (r < 0) | |
844 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
e1853b00 SS |
845 | |
846 | r = sd_rtnl_message_link_set_family(req, PF_BRIDGE); | |
6a7a4e4d LP |
847 | if (r < 0) |
848 | return log_link_error_errno(link, r, "Could not set message family: %m"); | |
e1853b00 SS |
849 | |
850 | r = sd_rtnl_message_open_container(req, IFLA_PROTINFO); | |
6a7a4e4d LP |
851 | if (r < 0) |
852 | return log_link_error_errno(link, r, "Could not append IFLA_PROTINFO attribute: %m"); | |
e1853b00 SS |
853 | |
854 | if(link->network->cost != 0) { | |
855 | r = sd_rtnl_message_append_u32(req, IFLA_BRPORT_COST, link->network->cost); | |
6a7a4e4d LP |
856 | if (r < 0) |
857 | return log_link_error_errno(link, r, "Could not append IFLA_BRPORT_COST attribute: %m"); | |
e1853b00 SS |
858 | } |
859 | ||
860 | r = sd_rtnl_message_close_container(req); | |
6a7a4e4d LP |
861 | if (r < 0) |
862 | return log_link_error_errno(link, r, "Could not append IFLA_LINKINFO attribute: %m"); | |
e1853b00 SS |
863 | |
864 | r = sd_rtnl_call_async(link->manager->rtnl, req, link_set_handler, link, 0, NULL); | |
6a7a4e4d LP |
865 | if (r < 0) |
866 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
e1853b00 SS |
867 | |
868 | link_ref(link); | |
869 | ||
870 | return r; | |
871 | } | |
872 | ||
49699bac SS |
873 | static void lldp_handler(sd_lldp *lldp, int event, void *userdata) { |
874 | Link *link = userdata; | |
875 | int r; | |
876 | ||
877 | assert(link); | |
878 | assert(link->network); | |
879 | assert(link->manager); | |
880 | ||
881 | if (event != UPDATE_INFO) | |
882 | return; | |
883 | ||
884 | r = sd_lldp_save(link->lldp, link->lldp_file); | |
885 | if (r < 0) | |
6a7a4e4d | 886 | log_link_warning_errno(link, r, "Could not save LLDP: %m"); |
49699bac SS |
887 | |
888 | } | |
889 | ||
ff254138 TG |
890 | static int link_acquire_conf(Link *link) { |
891 | int r; | |
892 | ||
893 | assert(link); | |
894 | assert(link->network); | |
ff254138 TG |
895 | assert(link->manager); |
896 | assert(link->manager->event); | |
897 | ||
78c958f8 | 898 | if (link_ipv4ll_enabled(link)) { |
eb34d4af | 899 | assert(link->ipv4ll); |
ff254138 | 900 | |
6a7a4e4d | 901 | log_link_debug(link, "Acquiring IPv4 link-local address"); |
5c1d3fc9 UTL |
902 | |
903 | r = sd_ipv4ll_start(link->ipv4ll); | |
6a7a4e4d LP |
904 | if (r < 0) |
905 | return log_link_warning_errno(link, r, "Could not acquire IPv4 link-local address: %m"); | |
5c1d3fc9 UTL |
906 | } |
907 | ||
78c958f8 | 908 | if (link_dhcp4_enabled(link)) { |
eb34d4af | 909 | assert(link->dhcp_client); |
ff254138 | 910 | |
6a7a4e4d | 911 | log_link_debug(link, "Acquiring DHCPv4 lease"); |
ab47d620 | 912 | |
5c1d3fc9 | 913 | r = sd_dhcp_client_start(link->dhcp_client); |
6a7a4e4d LP |
914 | if (r < 0) |
915 | return log_link_warning_errno(link, r, "Could not acquire DHCPv4 lease: %m"); | |
5c1d3fc9 | 916 | } |
ff254138 | 917 | |
78c958f8 | 918 | if (link_dhcp6_enabled(link)) { |
4138fb2c PF |
919 | assert(link->icmp6_router_discovery); |
920 | ||
6a7a4e4d | 921 | log_link_debug(link, "Discovering IPv6 routers"); |
4138fb2c PF |
922 | |
923 | r = sd_icmp6_router_solicitation_start(link->icmp6_router_discovery); | |
6a7a4e4d LP |
924 | if (r < 0) |
925 | return log_link_warning_errno(link, r, "Could not start IPv6 router discovery: %m"); | |
4138fb2c PF |
926 | } |
927 | ||
ce43e484 SS |
928 | if (link_lldp_enabled(link)) { |
929 | assert(link->lldp); | |
930 | ||
931 | log_link_debug(link, "Starting LLDP"); | |
932 | ||
933 | r = sd_lldp_start(link->lldp); | |
6a7a4e4d LP |
934 | if (r < 0) |
935 | return log_link_warning_errno(link, r, "Could not start LLDP: %m"); | |
ce43e484 SS |
936 | } |
937 | ||
ff254138 TG |
938 | return 0; |
939 | } | |
940 | ||
a61bb41c | 941 | bool link_has_carrier(Link *link) { |
deb2e523 TG |
942 | /* see Documentation/networking/operstates.txt in the kernel sources */ |
943 | ||
a61bb41c | 944 | if (link->kernel_operstate == IF_OPER_UP) |
deb2e523 TG |
945 | return true; |
946 | ||
a61bb41c | 947 | if (link->kernel_operstate == IF_OPER_UNKNOWN) |
deb2e523 | 948 | /* operstate may not be implemented, so fall back to flags */ |
a61bb41c | 949 | if ((link->flags & IFF_LOWER_UP) && !(link->flags & IFF_DORMANT)) |
deb2e523 TG |
950 | return true; |
951 | ||
952 | return false; | |
953 | } | |
954 | ||
dd3efc09 | 955 | static int link_up_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { |
5da8149f | 956 | _cleanup_link_unref_ Link *link = userdata; |
dd3efc09 TG |
957 | int r; |
958 | ||
1746cf2a TG |
959 | assert(link); |
960 | ||
5da8149f | 961 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
1746cf2a TG |
962 | return 1; |
963 | ||
dd3efc09 | 964 | r = sd_rtnl_message_get_errno(m); |
6a7a4e4d LP |
965 | if (r < 0) |
966 | /* we warn but don't fail the link, as it may be | |
967 | brought up later */ | |
968 | log_link_warning_errno(link, r, "%-*s: could not bring up interface: %m", IFNAMSIZ, link->ifname); | |
45ad2c13 | 969 | |
f882c247 TG |
970 | return 1; |
971 | } | |
972 | ||
973 | static int link_up(Link *link) { | |
cf6a8911 | 974 | _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL; |
5c3072ea | 975 | uint8_t ipv6ll_mode; |
f579559b TG |
976 | int r; |
977 | ||
f882c247 | 978 | assert(link); |
c106cc36 | 979 | assert(link->network); |
f882c247 TG |
980 | assert(link->manager); |
981 | assert(link->manager->rtnl); | |
982 | ||
6a7a4e4d | 983 | log_link_debug(link, "Bringing link up"); |
449f7554 | 984 | |
6a7a4e4d LP |
985 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex); |
986 | if (r < 0) | |
987 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
f579559b | 988 | |
5d4795f3 | 989 | r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP); |
6a7a4e4d LP |
990 | if (r < 0) |
991 | return log_link_error_errno(link, r, "Could not set link flags: %m"); | |
fc25d7f8 | 992 | |
c106cc36 TG |
993 | if (link->network->mac) { |
994 | r = sd_rtnl_message_append_ether_addr(req, IFLA_ADDRESS, link->network->mac); | |
6a7a4e4d LP |
995 | if (r < 0) |
996 | return log_link_error_errno(link, r, "Could not set MAC address: %m"); | |
c106cc36 TG |
997 | } |
998 | ||
999 | if (link->network->mtu) { | |
1000 | r = sd_rtnl_message_append_u32(req, IFLA_MTU, link->network->mtu); | |
6a7a4e4d LP |
1001 | if (r < 0) |
1002 | return log_link_error_errno(link, r, "Could not set MTU: %m"); | |
c106cc36 TG |
1003 | } |
1004 | ||
7f77697a | 1005 | r = sd_rtnl_message_open_container(req, IFLA_AF_SPEC); |
6a7a4e4d LP |
1006 | if (r < 0) |
1007 | return log_link_error_errno(link, r, "Could not open IFLA_AF_SPEC container: %m"); | |
d0d6a4cd | 1008 | |
01d28f81 TG |
1009 | if (socket_ipv6_is_supported()) { |
1010 | /* if the kernel lacks ipv6 support setting IFF_UP fails if any ipv6 options are passed */ | |
1011 | r = sd_rtnl_message_open_container(req, AF_INET6); | |
1012 | if (r < 0) | |
1013 | return log_link_error_errno(link, r, "Could not open AF_INET6 container: %m"); | |
d0d6a4cd | 1014 | |
01d28f81 TG |
1015 | ipv6ll_mode = link_ipv6ll_enabled(link) ? IN6_ADDR_GEN_MODE_EUI64 : IN6_ADDR_GEN_MODE_NONE; |
1016 | r = sd_rtnl_message_append_u8(req, IFLA_INET6_ADDR_GEN_MODE, ipv6ll_mode); | |
1017 | if (r < 0) | |
1018 | return log_link_error_errno(link, r, "Could not append IFLA_INET6_ADDR_GEN_MODE: %m"); | |
d0d6a4cd | 1019 | |
01d28f81 TG |
1020 | if (!in_addr_is_null(AF_INET6, &link->network->ipv6_token)) { |
1021 | r = sd_rtnl_message_append_in6_addr(req, IFLA_INET6_TOKEN, &link->network->ipv6_token.in6); | |
1022 | if (r < 0) | |
1023 | return log_link_error_errno(link, r, "Could not append IFLA_INET6_TOKEN: %m"); | |
1024 | } | |
1025 | ||
1026 | r = sd_rtnl_message_close_container(req); | |
6a7a4e4d | 1027 | if (r < 0) |
01d28f81 | 1028 | return log_link_error_errno(link, r, "Could not close AF_INET6 container: %m"); |
7f77697a | 1029 | } |
d0d6a4cd | 1030 | |
7f77697a | 1031 | r = sd_rtnl_message_close_container(req); |
6a7a4e4d LP |
1032 | if (r < 0) |
1033 | return log_link_error_errno(link, r, "Could not close IFLA_AF_SPEC container: %m"); | |
d0d6a4cd | 1034 | |
6a7a4e4d LP |
1035 | r = sd_rtnl_call_async(link->manager->rtnl, req, link_up_handler, link, 0, NULL); |
1036 | if (r < 0) | |
1037 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
f579559b | 1038 | |
b226d99b TG |
1039 | link_ref(link); |
1040 | ||
f882c247 TG |
1041 | return 0; |
1042 | } | |
1043 | ||
0d4ad91d AR |
1044 | static int link_down_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { |
1045 | _cleanup_link_unref_ Link *link = userdata; | |
1046 | int r; | |
1047 | ||
1048 | assert(link); | |
1049 | ||
1050 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
1051 | return 1; | |
1052 | ||
1053 | r = sd_rtnl_message_get_errno(m); | |
1054 | if (r < 0) | |
6a7a4e4d | 1055 | log_link_warning_errno(link, r, "%-*s: could not bring down interface: %m", IFNAMSIZ, link->ifname); |
0d4ad91d AR |
1056 | |
1057 | return 1; | |
1058 | } | |
1059 | ||
1060 | static int link_down(Link *link) { | |
1061 | _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL; | |
1062 | int r; | |
1063 | ||
1064 | assert(link); | |
1065 | assert(link->manager); | |
1066 | assert(link->manager->rtnl); | |
1067 | ||
6a7a4e4d | 1068 | log_link_debug(link, "Bringing link down"); |
0d4ad91d AR |
1069 | |
1070 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, | |
1071 | RTM_SETLINK, link->ifindex); | |
6a7a4e4d LP |
1072 | if (r < 0) |
1073 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
0d4ad91d AR |
1074 | |
1075 | r = sd_rtnl_message_link_set_flags(req, 0, IFF_UP); | |
6a7a4e4d LP |
1076 | if (r < 0) |
1077 | return log_link_error_errno(link, r, "Could not set link flags: %m"); | |
0d4ad91d | 1078 | |
6a7a4e4d LP |
1079 | r = sd_rtnl_call_async(link->manager->rtnl, req, link_down_handler, link, 0, NULL); |
1080 | if (r < 0) | |
1081 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
0d4ad91d AR |
1082 | |
1083 | link_ref(link); | |
1084 | ||
1085 | return 0; | |
1086 | } | |
1087 | ||
1088 | static int link_handle_bound_to_list(Link *link) { | |
1089 | Link *l; | |
1090 | Iterator i; | |
1091 | int r; | |
1092 | bool required_up = false; | |
1093 | bool link_is_up = false; | |
1094 | ||
1095 | assert(link); | |
1096 | ||
1097 | if (hashmap_isempty(link->bound_to_links)) | |
1098 | return 0; | |
1099 | ||
1100 | if (link->flags & IFF_UP) | |
1101 | link_is_up = true; | |
1102 | ||
1103 | HASHMAP_FOREACH (l, link->bound_to_links, i) | |
1104 | if (link_has_carrier(l)) { | |
1105 | required_up = true; | |
1106 | break; | |
1107 | } | |
1108 | ||
1109 | if (!required_up && link_is_up) { | |
1110 | r = link_down(link); | |
1111 | if (r < 0) | |
1112 | return r; | |
1113 | } else if (required_up && !link_is_up) { | |
1114 | r = link_up(link); | |
1115 | if (r < 0) | |
1116 | return r; | |
1117 | } | |
1118 | ||
1119 | return 0; | |
1120 | } | |
1121 | ||
1122 | static int link_handle_bound_by_list(Link *link) { | |
1123 | Iterator i; | |
1124 | Link *l; | |
1125 | int r; | |
1126 | ||
1127 | assert(link); | |
1128 | ||
1129 | if (hashmap_isempty(link->bound_by_links)) | |
1130 | return 0; | |
1131 | ||
1132 | HASHMAP_FOREACH (l, link->bound_by_links, i) { | |
1133 | r = link_handle_bound_to_list(l); | |
1134 | if (r < 0) | |
1135 | return r; | |
1136 | } | |
1137 | ||
1138 | return 0; | |
1139 | } | |
1140 | ||
1141 | static int link_put_carrier(Link *link, Link *carrier, Hashmap **h) { | |
1142 | int r; | |
1143 | ||
1144 | assert(link); | |
1145 | assert(carrier); | |
1146 | ||
1147 | if (link == carrier) | |
1148 | return 0; | |
1149 | ||
1150 | if (hashmap_get(*h, INT_TO_PTR(carrier->ifindex))) | |
1151 | return 0; | |
1152 | ||
1153 | r = hashmap_ensure_allocated(h, NULL); | |
1154 | if (r < 0) | |
1155 | return r; | |
1156 | ||
1157 | r = hashmap_put(*h, INT_TO_PTR(carrier->ifindex), carrier); | |
1158 | if (r < 0) | |
1159 | return r; | |
1160 | ||
1161 | return 0; | |
1162 | } | |
1163 | ||
1164 | static int link_new_bound_by_list(Link *link) { | |
1165 | Manager *m; | |
1166 | Link *carrier; | |
1167 | Iterator i; | |
1168 | int r; | |
1169 | bool list_updated = false; | |
1170 | ||
1171 | assert(link); | |
1172 | assert(link->manager); | |
1173 | ||
1174 | m = link->manager; | |
1175 | ||
1176 | HASHMAP_FOREACH (carrier, m->links, i) { | |
1177 | if (!carrier->network) | |
1178 | continue; | |
1179 | ||
1180 | if (strv_isempty(carrier->network->bind_carrier)) | |
1181 | continue; | |
1182 | ||
1183 | if (strv_fnmatch(carrier->network->bind_carrier, link->ifname, 0)) { | |
1184 | r = link_put_carrier(link, carrier, &link->bound_by_links); | |
1185 | if (r < 0) | |
1186 | return r; | |
1187 | ||
1188 | list_updated = true; | |
1189 | } | |
1190 | } | |
1191 | ||
1192 | if (list_updated) | |
1193 | link_save(link); | |
1194 | ||
1195 | HASHMAP_FOREACH (carrier, link->bound_by_links, i) { | |
1196 | r = link_put_carrier(carrier, link, &carrier->bound_to_links); | |
1197 | if (r < 0) | |
1198 | return r; | |
1199 | ||
1200 | link_save(carrier); | |
1201 | } | |
1202 | ||
1203 | return 0; | |
1204 | } | |
1205 | ||
1206 | static int link_new_bound_to_list(Link *link) { | |
1207 | Manager *m; | |
1208 | Link *carrier; | |
1209 | Iterator i; | |
1210 | int r; | |
1211 | bool list_updated = false; | |
1212 | ||
1213 | assert(link); | |
1214 | assert(link->manager); | |
1215 | ||
1216 | if (!link->network) | |
1217 | return 0; | |
1218 | ||
1219 | if (strv_isempty(link->network->bind_carrier)) | |
1220 | return 0; | |
1221 | ||
1222 | m = link->manager; | |
1223 | ||
1224 | HASHMAP_FOREACH (carrier, m->links, i) { | |
1225 | if (strv_fnmatch(link->network->bind_carrier, carrier->ifname, 0)) { | |
1226 | r = link_put_carrier(link, carrier, &link->bound_to_links); | |
1227 | if (r < 0) | |
1228 | return r; | |
1229 | ||
1230 | list_updated = true; | |
1231 | } | |
1232 | } | |
1233 | ||
1234 | if (list_updated) | |
1235 | link_save(link); | |
1236 | ||
1237 | HASHMAP_FOREACH (carrier, link->bound_to_links, i) { | |
1238 | r = link_put_carrier(carrier, link, &carrier->bound_by_links); | |
1239 | if (r < 0) | |
1240 | return r; | |
1241 | ||
1242 | link_save(carrier); | |
1243 | } | |
1244 | ||
1245 | return 0; | |
1246 | } | |
1247 | ||
1248 | static int link_new_carrier_maps(Link *link) { | |
1249 | int r; | |
1250 | ||
1251 | r = link_new_bound_by_list(link); | |
1252 | if (r < 0) | |
1253 | return r; | |
1254 | ||
1255 | r = link_handle_bound_by_list(link); | |
1256 | if (r < 0) | |
1257 | return r; | |
1258 | ||
1259 | r = link_new_bound_to_list(link); | |
1260 | if (r < 0) | |
1261 | return r; | |
1262 | ||
1263 | r = link_handle_bound_to_list(link); | |
1264 | if (r < 0) | |
1265 | return r; | |
1266 | ||
1267 | return 0; | |
1268 | } | |
1269 | ||
1270 | static void link_free_bound_to_list(Link *link) { | |
1271 | Link *bound_to; | |
1272 | Iterator i; | |
1273 | ||
1274 | HASHMAP_FOREACH (bound_to, link->bound_to_links, i) { | |
1275 | hashmap_remove(link->bound_to_links, INT_TO_PTR(bound_to->ifindex)); | |
1276 | ||
1277 | if (hashmap_remove(bound_to->bound_by_links, INT_TO_PTR(link->ifindex))) | |
1278 | link_save(bound_to); | |
1279 | } | |
1280 | ||
1281 | return; | |
1282 | } | |
1283 | ||
1284 | static void link_free_bound_by_list(Link *link) { | |
1285 | Link *bound_by; | |
1286 | Iterator i; | |
1287 | ||
1288 | HASHMAP_FOREACH (bound_by, link->bound_by_links, i) { | |
1289 | hashmap_remove(link->bound_by_links, INT_TO_PTR(bound_by->ifindex)); | |
1290 | ||
1291 | if (hashmap_remove(bound_by->bound_to_links, INT_TO_PTR(link->ifindex))) { | |
1292 | link_save(bound_by); | |
1293 | link_handle_bound_to_list(bound_by); | |
1294 | } | |
1295 | } | |
1296 | ||
1297 | return; | |
1298 | } | |
1299 | ||
1300 | static void link_free_carrier_maps(Link *link) { | |
1301 | bool list_updated = false; | |
1302 | ||
1303 | assert(link); | |
1304 | ||
1305 | if (!hashmap_isempty(link->bound_to_links)) { | |
1306 | link_free_bound_to_list(link); | |
1307 | list_updated = true; | |
1308 | } | |
1309 | ||
1310 | if (!hashmap_isempty(link->bound_by_links)) { | |
1311 | link_free_bound_by_list(link); | |
1312 | list_updated = true; | |
1313 | } | |
1314 | ||
1315 | if (list_updated) | |
1316 | link_save(link); | |
1317 | ||
1318 | return; | |
1319 | } | |
1320 | ||
1321 | void link_drop(Link *link) { | |
1322 | if (!link || link->state == LINK_STATE_LINGER) | |
1323 | return; | |
1324 | ||
1325 | link_set_state(link, LINK_STATE_LINGER); | |
1326 | ||
1327 | link_free_carrier_maps(link); | |
1328 | ||
6a7a4e4d | 1329 | log_link_debug(link, "Link removed"); |
0d4ad91d AR |
1330 | |
1331 | link_unref(link); | |
1332 | ||
1333 | return; | |
1334 | } | |
1335 | ||
3f265037 | 1336 | static int link_joined(Link *link) { |
f882c247 TG |
1337 | int r; |
1338 | ||
ef1ba606 | 1339 | assert(link); |
f5be5601 | 1340 | assert(link->network); |
dd3efc09 | 1341 | |
0d4ad91d AR |
1342 | if (!hashmap_isempty(link->bound_to_links)) { |
1343 | r = link_handle_bound_to_list(link); | |
1344 | if (r < 0) | |
1345 | return r; | |
1346 | } else if (!(link->flags & IFF_UP)) { | |
505f8da7 TG |
1347 | r = link_up(link); |
1348 | if (r < 0) { | |
1349 | link_enter_failed(link); | |
1350 | return r; | |
1351 | } | |
ef1ba606 | 1352 | } |
f882c247 | 1353 | |
e1853b00 SS |
1354 | if(link->network->bridge) { |
1355 | r = link_set_bridge(link); | |
6a7a4e4d LP |
1356 | if (r < 0) |
1357 | log_link_error_errno(link, r, "Could not set bridge message: %m"); | |
e1853b00 SS |
1358 | } |
1359 | ||
fb6730c4 | 1360 | return link_enter_set_addresses(link); |
02b59d57 TG |
1361 | } |
1362 | ||
3c9b8860 TG |
1363 | static int netdev_join_handler(sd_rtnl *rtnl, sd_rtnl_message *m, |
1364 | void *userdata) { | |
5da8149f | 1365 | _cleanup_link_unref_ Link *link = userdata; |
02b59d57 TG |
1366 | int r; |
1367 | ||
1746cf2a | 1368 | assert(link); |
ef1ba606 | 1369 | assert(link->network); |
02b59d57 | 1370 | |
52433f6b TG |
1371 | link->enslaving --; |
1372 | ||
5da8149f | 1373 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) |
02b59d57 TG |
1374 | return 1; |
1375 | ||
1376 | r = sd_rtnl_message_get_errno(m); | |
856f962c | 1377 | if (r < 0 && r != -EEXIST) { |
6a7a4e4d | 1378 | log_link_error_errno(link, r, "%-*s: could not join netdev: %m", IFNAMSIZ, link->ifname); |
ef1ba606 TG |
1379 | link_enter_failed(link); |
1380 | return 1; | |
ba179154 | 1381 | } else |
6a7a4e4d | 1382 | log_link_debug(link, "Joined netdev"); |
02b59d57 | 1383 | |
856f962c | 1384 | if (link->enslaving <= 0) |
3f265037 | 1385 | link_joined(link); |
02b59d57 TG |
1386 | |
1387 | return 1; | |
1388 | } | |
1389 | ||
3f265037 | 1390 | static int link_enter_join_netdev(Link *link) { |
6a0a2f86 | 1391 | NetDev *netdev; |
672682a6 | 1392 | Iterator i; |
02b59d57 TG |
1393 | int r; |
1394 | ||
1395 | assert(link); | |
1396 | assert(link->network); | |
8434fd5c | 1397 | assert(link->state == LINK_STATE_PENDING); |
02b59d57 | 1398 | |
e331e246 | 1399 | link_set_state(link, LINK_STATE_ENSLAVING); |
02b59d57 | 1400 | |
fe8db0c5 TG |
1401 | link_save(link); |
1402 | ||
7951dea2 SS |
1403 | if (!link->network->bridge && |
1404 | !link->network->bond && | |
6a0a2f86 | 1405 | hashmap_isempty(link->network->stacked_netdevs)) |
3f265037 | 1406 | return link_joined(link); |
02b59d57 | 1407 | |
d9c67ea1 | 1408 | if (link->network->bond) { |
f2341e0a LP |
1409 | log_struct(LOG_DEBUG, |
1410 | LOG_LINK_INTERFACE(link), | |
1411 | LOG_NETDEV_INTERFACE(link->network->bond), | |
1412 | LOG_LINK_MESSAGE(link, "Enslaving by '%s'", link->network->bond->ifname), | |
1413 | NULL); | |
1414 | ||
1415 | r = netdev_join(link->network->bond, link, netdev_join_handler); | |
52433f6b | 1416 | if (r < 0) { |
f2341e0a LP |
1417 | log_struct_errno(LOG_WARNING, r, |
1418 | LOG_LINK_INTERFACE(link), | |
1419 | LOG_NETDEV_INTERFACE(link->network->bond), | |
1420 | LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", link->network->bond->ifname), | |
1421 | NULL); | |
1422 | ||
52433f6b TG |
1423 | link_enter_failed(link); |
1424 | return r; | |
1425 | } | |
1426 | ||
0ad6148e MO |
1427 | link->enslaving ++; |
1428 | } | |
1429 | ||
d9c67ea1 | 1430 | if (link->network->bridge) { |
f2341e0a LP |
1431 | log_struct(LOG_DEBUG, |
1432 | LOG_LINK_INTERFACE(link), | |
1433 | LOG_NETDEV_INTERFACE(link->network->bridge), | |
1434 | LOG_LINK_MESSAGE(link, "Enslaving by '%s'", link->network->bridge->ifname), | |
1435 | NULL); | |
1436 | ||
1437 | r = netdev_join(link->network->bridge, link, netdev_join_handler); | |
0ad6148e | 1438 | if (r < 0) { |
f2341e0a LP |
1439 | log_struct_errno(LOG_WARNING, r, |
1440 | LOG_LINK_INTERFACE(link), | |
1441 | LOG_NETDEV_INTERFACE(link->network->bridge), | |
1442 | LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", link->network->bridge->ifname), | |
1443 | NULL), | |
0ad6148e MO |
1444 | link_enter_failed(link); |
1445 | return r; | |
1446 | } | |
1447 | ||
52433f6b TG |
1448 | link->enslaving ++; |
1449 | } | |
1450 | ||
6a0a2f86 | 1451 | HASHMAP_FOREACH(netdev, link->network->stacked_netdevs, i) { |
7951dea2 | 1452 | |
f2341e0a LP |
1453 | log_struct(LOG_DEBUG, |
1454 | LOG_LINK_INTERFACE(link), | |
1455 | LOG_NETDEV_INTERFACE(netdev), | |
1456 | LOG_LINK_MESSAGE(link, "Enslaving by '%s'", netdev->ifname), | |
1457 | NULL); | |
1458 | ||
1459 | r = netdev_join(netdev, link, netdev_join_handler); | |
7951dea2 | 1460 | if (r < 0) { |
f2341e0a LP |
1461 | log_struct_errno(LOG_WARNING, r, |
1462 | LOG_LINK_INTERFACE(link), | |
1463 | LOG_NETDEV_INTERFACE(netdev), | |
1464 | LOG_LINK_MESSAGE(link, "Could not join netdev '%s': %m", netdev->ifname), | |
1465 | NULL); | |
326cb406 SS |
1466 | link_enter_failed(link); |
1467 | return r; | |
1468 | } | |
1469 | ||
326cb406 SS |
1470 | link->enslaving ++; |
1471 | } | |
1472 | ||
ef1ba606 TG |
1473 | return 0; |
1474 | } | |
1475 | ||
769d324c | 1476 | static int link_set_ipv4_forward(Link *link) { |
5a8bcb67 LP |
1477 | const char *p = NULL; |
1478 | int r; | |
1479 | ||
8add5f79 NO |
1480 | if (link->network->ip_forward == _ADDRESS_FAMILY_BOOLEAN_INVALID) |
1481 | return 0; | |
1482 | ||
63c372cb | 1483 | p = strjoina("/proc/sys/net/ipv4/conf/", link->ifname, "/forwarding"); |
505c5f6d | 1484 | r = write_string_file_no_create(p, one_zero(link_ipv4_forward_enabled(link))); |
5a8bcb67 | 1485 | if (r < 0) |
43c6d5ab LP |
1486 | log_link_warning_errno(link, r, "Cannot configure IPv4 forwarding for interface %s: %m", link->ifname); |
1487 | ||
769d324c LP |
1488 | return 0; |
1489 | } | |
1490 | ||
1491 | static int link_set_ipv6_forward(Link *link) { | |
1492 | const char *p = NULL; | |
1493 | int r; | |
1494 | ||
fe027299 LP |
1495 | /* Make this a NOP if IPv6 is not available */ |
1496 | if (!socket_ipv6_is_supported()) | |
1497 | return 0; | |
1498 | ||
8add5f79 NO |
1499 | if (link->network->ip_forward == _ADDRESS_FAMILY_BOOLEAN_INVALID) |
1500 | return 0; | |
1501 | ||
63c372cb | 1502 | p = strjoina("/proc/sys/net/ipv6/conf/", link->ifname, "/forwarding"); |
769d324c LP |
1503 | r = write_string_file_no_create(p, one_zero(link_ipv6_forward_enabled(link))); |
1504 | if (r < 0) | |
1505 | log_link_warning_errno(link, r, "Cannot configure IPv6 forwarding for interface: %m"); | |
5a8bcb67 LP |
1506 | |
1507 | return 0; | |
1508 | } | |
1509 | ||
a748b692 | 1510 | static int link_configure(Link *link) { |
02b59d57 TG |
1511 | int r; |
1512 | ||
ef1ba606 | 1513 | assert(link); |
b22d8a00 | 1514 | assert(link->network); |
8434fd5c | 1515 | assert(link->state == LINK_STATE_PENDING); |
a748b692 | 1516 | |
b98b483b AR |
1517 | r = link_set_bridge_fdb(link); |
1518 | if (r < 0) | |
1519 | return r; | |
1520 | ||
769d324c LP |
1521 | r = link_set_ipv4_forward(link); |
1522 | if (r < 0) | |
1523 | return r; | |
1524 | ||
1525 | r = link_set_ipv6_forward(link); | |
5a8bcb67 LP |
1526 | if (r < 0) |
1527 | return r; | |
1528 | ||
78c958f8 | 1529 | if (link_ipv4ll_enabled(link)) { |
b22d8a00 | 1530 | r = ipv4ll_configure(link); |
eb34d4af TG |
1531 | if (r < 0) |
1532 | return r; | |
1533 | } | |
1534 | ||
78c958f8 | 1535 | if (link_dhcp4_enabled(link)) { |
3c9b8860 | 1536 | r = dhcp4_configure(link); |
eb34d4af TG |
1537 | if (r < 0) |
1538 | return r; | |
eb34d4af TG |
1539 | } |
1540 | ||
78c958f8 | 1541 | if (link_dhcp4_server_enabled(link)) { |
dd43110f TG |
1542 | r = sd_dhcp_server_new(&link->dhcp_server, link->ifindex); |
1543 | if (r < 0) | |
1544 | return r; | |
1545 | ||
1546 | r = sd_dhcp_server_attach_event(link->dhcp_server, NULL, 0); | |
1547 | if (r < 0) | |
1548 | return r; | |
dd43110f TG |
1549 | } |
1550 | ||
78c958f8 | 1551 | if (link_dhcp6_enabled(link)) { |
5c79bd79 | 1552 | r = icmp6_configure(link); |
4138fb2c PF |
1553 | if (r < 0) |
1554 | return r; | |
1555 | } | |
1556 | ||
ce43e484 SS |
1557 | if (link_lldp_enabled(link)) { |
1558 | r = sd_lldp_new(link->ifindex, link->ifname, &link->mac, &link->lldp); | |
1559 | if (r < 0) | |
1560 | return r; | |
1561 | ||
1562 | r = sd_lldp_attach_event(link->lldp, NULL, 0); | |
1563 | if (r < 0) | |
1564 | return r; | |
49699bac SS |
1565 | |
1566 | r = sd_lldp_set_callback(link->lldp, | |
1567 | lldp_handler, link); | |
1568 | if (r < 0) | |
1569 | return r; | |
ce43e484 SS |
1570 | } |
1571 | ||
a61bb41c | 1572 | if (link_has_carrier(link)) { |
1e9be60b TG |
1573 | r = link_acquire_conf(link); |
1574 | if (r < 0) | |
1575 | return r; | |
cc544d5f | 1576 | } |
1e9be60b | 1577 | |
3f265037 | 1578 | return link_enter_join_netdev(link); |
505f8da7 TG |
1579 | } |
1580 | ||
3c9b8860 TG |
1581 | static int link_initialized_and_synced(sd_rtnl *rtnl, sd_rtnl_message *m, |
1582 | void *userdata) { | |
5da8149f | 1583 | _cleanup_link_unref_ Link *link = userdata; |
505f8da7 | 1584 | Network *network; |
505f8da7 TG |
1585 | int r; |
1586 | ||
1587 | assert(link); | |
1588 | assert(link->ifname); | |
1589 | assert(link->manager); | |
1590 | ||
8434fd5c | 1591 | if (link->state != LINK_STATE_PENDING) |
5da8149f | 1592 | return 1; |
505f8da7 | 1593 | |
6a7a4e4d | 1594 | log_link_debug(link, "Link state is up-to-date"); |
505f8da7 | 1595 | |
0d4ad91d AR |
1596 | r = link_new_bound_by_list(link); |
1597 | if (r < 0) | |
1598 | return r; | |
1599 | ||
1600 | r = link_handle_bound_by_list(link); | |
1601 | if (r < 0) | |
1602 | return r; | |
1603 | ||
3c9b8860 TG |
1604 | r = network_get(link->manager, link->udev_device, link->ifname, |
1605 | &link->mac, &network); | |
57bd6899 TG |
1606 | if (r == -ENOENT) { |
1607 | link_enter_unmanaged(link); | |
5da8149f | 1608 | return 1; |
57bd6899 TG |
1609 | } else if (r < 0) |
1610 | return r; | |
505f8da7 | 1611 | |
bd2efe92 | 1612 | if (link->flags & IFF_LOOPBACK) { |
d0d6a4cd | 1613 | if (network->link_local != ADDRESS_FAMILY_NO) |
6a7a4e4d | 1614 | log_link_debug(link, "Ignoring link-local autoconfiguration for loopback link"); |
78c958f8 | 1615 | |
cb9fc36a | 1616 | if (network->dhcp != ADDRESS_FAMILY_NO) |
6a7a4e4d | 1617 | log_link_debug(link, "Ignoring DHCP clients for loopback link"); |
78c958f8 TG |
1618 | |
1619 | if (network->dhcp_server) | |
6a7a4e4d | 1620 | log_link_debug(link, "Ignoring DHCP server for loopback link"); |
bd2efe92 TG |
1621 | } |
1622 | ||
505f8da7 TG |
1623 | r = network_apply(link->manager, network, link); |
1624 | if (r < 0) | |
1625 | return r; | |
1626 | ||
0d4ad91d AR |
1627 | r = link_new_bound_to_list(link); |
1628 | if (r < 0) | |
1629 | return r; | |
1630 | ||
a748b692 TG |
1631 | r = link_configure(link); |
1632 | if (r < 0) | |
1633 | return r; | |
1634 | ||
5da8149f | 1635 | return 1; |
505f8da7 TG |
1636 | } |
1637 | ||
4f561e8e TG |
1638 | int link_initialized(Link *link, struct udev_device *device) { |
1639 | _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL; | |
1640 | int r; | |
1641 | ||
1642 | assert(link); | |
1643 | assert(link->manager); | |
1644 | assert(link->manager->rtnl); | |
1645 | assert(device); | |
1646 | ||
8434fd5c | 1647 | if (link->state != LINK_STATE_PENDING) |
4f561e8e TG |
1648 | return 0; |
1649 | ||
679b3605 TG |
1650 | if (link->udev_device) |
1651 | return 0; | |
1652 | ||
79008bdd | 1653 | log_link_debug(link, "udev initialized link"); |
4f561e8e TG |
1654 | |
1655 | link->udev_device = udev_device_ref(device); | |
1656 | ||
3c9b8860 TG |
1657 | /* udev has initialized the link, but we don't know if we have yet |
1658 | * processed the NEWLINK messages with the latest state. Do a GETLINK, | |
1659 | * when it returns we know that the pending NEWLINKs have already been | |
1660 | * processed and that we are up-to-date */ | |
4f561e8e | 1661 | |
3c9b8860 TG |
1662 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK, |
1663 | link->ifindex); | |
4f561e8e TG |
1664 | if (r < 0) |
1665 | return r; | |
1666 | ||
3c9b8860 TG |
1667 | r = sd_rtnl_call_async(link->manager->rtnl, req, |
1668 | link_initialized_and_synced, link, 0, NULL); | |
4f561e8e TG |
1669 | if (r < 0) |
1670 | return r; | |
1671 | ||
5da8149f TG |
1672 | link_ref(link); |
1673 | ||
4f561e8e TG |
1674 | return 0; |
1675 | } | |
1676 | ||
5a8bcb67 LP |
1677 | static Address* link_get_equal_address(Link *link, Address *needle) { |
1678 | Address *i; | |
1679 | ||
1680 | assert(link); | |
1681 | assert(needle); | |
1682 | ||
1683 | LIST_FOREACH(addresses, i, link->addresses) | |
1684 | if (address_equal(i, needle)) | |
1685 | return i; | |
1686 | ||
1687 | return NULL; | |
1688 | } | |
1689 | ||
45af44d4 | 1690 | int link_rtnl_process_address(sd_rtnl *rtnl, sd_rtnl_message *message, void *userdata) { |
fbbeb65a TG |
1691 | Manager *m = userdata; |
1692 | Link *link = NULL; | |
1693 | uint16_t type; | |
1694 | _cleanup_address_free_ Address *address = NULL; | |
851c9f82 | 1695 | unsigned char flags; |
5a8bcb67 LP |
1696 | Address *existing; |
1697 | char buf[INET6_ADDRSTRLEN], valid_buf[FORMAT_TIMESPAN_MAX]; | |
c6d3b303 | 1698 | const char *valid_str = NULL; |
fbbeb65a TG |
1699 | int r, ifindex; |
1700 | ||
1701 | assert(rtnl); | |
1702 | assert(message); | |
1703 | assert(m); | |
1704 | ||
45af44d4 TG |
1705 | if (sd_rtnl_message_is_error(message)) { |
1706 | r = sd_rtnl_message_get_errno(message); | |
1707 | if (r < 0) | |
1708 | log_warning_errno(r, "rtnl: failed to receive address: %m"); | |
1709 | ||
1710 | return 0; | |
1711 | } | |
1712 | ||
fbbeb65a TG |
1713 | r = sd_rtnl_message_get_type(message, &type); |
1714 | if (r < 0) { | |
6a7a4e4d | 1715 | log_warning_errno(r, "rtnl: could not get message type: %m"); |
fbbeb65a | 1716 | return 0; |
cdfee943 TG |
1717 | } else if (type != RTM_NEWADDR && type != RTM_DELADDR) { |
1718 | log_warning("rtnl: received unexpected message type when processing address"); | |
1719 | return 0; | |
fbbeb65a TG |
1720 | } |
1721 | ||
1722 | r = sd_rtnl_message_addr_get_ifindex(message, &ifindex); | |
45af44d4 | 1723 | if (r < 0) { |
cdfee943 | 1724 | log_warning_errno(r, "rtnl: could not get ifindex from address: %m"); |
45af44d4 TG |
1725 | return 0; |
1726 | } else if (ifindex <= 0) { | |
1727 | log_warning("rtnl: received address message with invalid ifindex: %d", ifindex); | |
fbbeb65a TG |
1728 | return 0; |
1729 | } else { | |
1730 | r = link_get(m, ifindex, &link); | |
1731 | if (r < 0 || !link) { | |
6a24f148 TG |
1732 | /* when enumerating we might be out of sync, but we will |
1733 | * get the address again, so just ignore it */ | |
1734 | if (!m->enumerating) | |
1735 | log_warning("rtnl: received address for nonexistent link (%d), ignoring", ifindex); | |
fbbeb65a TG |
1736 | return 0; |
1737 | } | |
1738 | } | |
1739 | ||
1740 | r = address_new_dynamic(&address); | |
1741 | if (r < 0) | |
393c0c5e | 1742 | return r; |
fbbeb65a TG |
1743 | |
1744 | r = sd_rtnl_message_addr_get_family(message, &address->family); | |
1745 | if (r < 0 || !IN_SET(address->family, AF_INET, AF_INET6)) { | |
6a7a4e4d | 1746 | log_link_warning(link, "rtnl: received address with invalid family, ignoring."); |
fbbeb65a TG |
1747 | return 0; |
1748 | } | |
1749 | ||
1750 | r = sd_rtnl_message_addr_get_prefixlen(message, &address->prefixlen); | |
1751 | if (r < 0) { | |
6a7a4e4d | 1752 | log_link_warning_errno(link, r, "rtnl: received address with invalid prefixlen, ignoring: %m"); |
e375dcde TG |
1753 | return 0; |
1754 | } | |
1755 | ||
1756 | r = sd_rtnl_message_addr_get_scope(message, &address->scope); | |
1757 | if (r < 0) { | |
6a7a4e4d | 1758 | log_link_warning_errno(link, r, "rtnl: received address with invalid scope, ignoring: %m"); |
fbbeb65a TG |
1759 | return 0; |
1760 | } | |
1761 | ||
851c9f82 | 1762 | r = sd_rtnl_message_addr_get_flags(message, &flags); |
81163121 | 1763 | if (r < 0) { |
6a7a4e4d | 1764 | log_link_warning_errno(link, r, "rtnl: received address with invalid flags, ignoring: %m"); |
81163121 TG |
1765 | return 0; |
1766 | } | |
851c9f82 | 1767 | address->flags = flags; |
81163121 | 1768 | |
fbbeb65a TG |
1769 | switch (address->family) { |
1770 | case AF_INET: | |
5a8bcb67 | 1771 | r = sd_rtnl_message_read_in_addr(message, IFA_LOCAL, &address->in_addr.in); |
fbbeb65a | 1772 | if (r < 0) { |
6a7a4e4d | 1773 | log_link_warning_errno(link, r, "rtnl: received address without valid address, ignoring: %m"); |
fbbeb65a TG |
1774 | return 0; |
1775 | } | |
1776 | ||
1777 | break; | |
1778 | ||
1779 | case AF_INET6: | |
5a8bcb67 | 1780 | r = sd_rtnl_message_read_in6_addr(message, IFA_ADDRESS, &address->in_addr.in6); |
fbbeb65a | 1781 | if (r < 0) { |
6a7a4e4d | 1782 | log_link_warning_errno(link, r, "rtnl: received address without valid address, ignoring: %m"); |
fbbeb65a TG |
1783 | return 0; |
1784 | } | |
1785 | ||
1786 | break; | |
1787 | ||
1788 | default: | |
1789 | assert_not_reached("invalid address family"); | |
1790 | } | |
1791 | ||
5a8bcb67 | 1792 | if (!inet_ntop(address->family, &address->in_addr, buf, INET6_ADDRSTRLEN)) { |
6a7a4e4d | 1793 | log_link_warning(link, "Could not print address"); |
fbbeb65a TG |
1794 | return 0; |
1795 | } | |
1796 | ||
5a8bcb67 | 1797 | r = sd_rtnl_message_read_cache_info(message, IFA_CACHEINFO, &address->cinfo); |
c6d3b303 TG |
1798 | if (r >= 0) { |
1799 | if (address->cinfo.ifa_valid == CACHE_INFO_INFINITY_LIFE_TIME) | |
1800 | valid_str = "ever"; | |
1801 | else | |
1802 | valid_str = format_timespan(valid_buf, FORMAT_TIMESPAN_MAX, | |
1803 | address->cinfo.ifa_valid * USEC_PER_SEC, | |
1804 | USEC_PER_SEC); | |
1805 | } | |
1806 | ||
5a8bcb67 LP |
1807 | existing = link_get_equal_address(link, address); |
1808 | ||
1809 | switch (type) { | |
1810 | case RTM_NEWADDR: | |
1811 | if (existing) { | |
1812 | log_link_debug(link, "Updating address: %s/%u (valid for %s)", buf, address->prefixlen, valid_str); | |
428fd0a7 | 1813 | |
428fd0a7 | 1814 | |
5a8bcb67 LP |
1815 | existing->scope = address->scope; |
1816 | existing->flags = address->flags; | |
1817 | existing->cinfo = address->cinfo; | |
428fd0a7 | 1818 | |
5a8bcb67 LP |
1819 | } else { |
1820 | log_link_debug(link, "Adding address: %s/%u (valid for %s)", buf, address->prefixlen, valid_str); | |
428fd0a7 | 1821 | |
5a8bcb67 LP |
1822 | LIST_PREPEND(addresses, link->addresses, address); |
1823 | address_establish(address, link); | |
fbbeb65a | 1824 | |
5a8bcb67 | 1825 | address = NULL; |
428fd0a7 | 1826 | |
5a8bcb67 LP |
1827 | link_save(link); |
1828 | } | |
f5602be9 | 1829 | |
428fd0a7 | 1830 | break; |
5a8bcb67 | 1831 | |
fbbeb65a | 1832 | case RTM_DELADDR: |
428fd0a7 | 1833 | |
5a8bcb67 LP |
1834 | if (existing) { |
1835 | log_link_debug(link, "Removing address: %s/%u (valid for %s)", buf, address->prefixlen, valid_str); | |
1836 | address_release(existing, link); | |
1837 | LIST_REMOVE(addresses, link->addresses, existing); | |
1838 | address_free(existing); | |
393c0c5e | 1839 | } else |
5a8bcb67 | 1840 | log_link_warning(link, "Removing non-existent address: %s/%u (valid for %s)", buf, address->prefixlen, valid_str); |
f5602be9 | 1841 | |
fbbeb65a TG |
1842 | break; |
1843 | default: | |
1844 | assert_not_reached("Received invalid RTNL message type"); | |
1845 | } | |
1846 | ||
1847 | return 1; | |
1848 | } | |
1849 | ||
505f8da7 TG |
1850 | int link_add(Manager *m, sd_rtnl_message *message, Link **ret) { |
1851 | Link *link; | |
1852 | _cleanup_udev_device_unref_ struct udev_device *device = NULL; | |
1853 | char ifindex_str[2 + DECIMAL_STR_MAX(int)]; | |
1854 | int r; | |
1855 | ||
1856 | assert(m); | |
fbbeb65a | 1857 | assert(m->rtnl); |
505f8da7 TG |
1858 | assert(message); |
1859 | assert(ret); | |
1860 | ||
1861 | r = link_new(m, message, ret); | |
1862 | if (r < 0) | |
1863 | return r; | |
1864 | ||
1865 | link = *ret; | |
1866 | ||
6a7a4e4d | 1867 | log_link_debug(link, "Link %d added", link->ifindex); |
505f8da7 TG |
1868 | |
1869 | if (detect_container(NULL) <= 0) { | |
1870 | /* not in a container, udev will be around */ | |
ae06ab10 | 1871 | sprintf(ifindex_str, "n%d", link->ifindex); |
505f8da7 | 1872 | device = udev_device_new_from_device_id(m->udev, ifindex_str); |
6a7a4e4d LP |
1873 | if (!device) |
1874 | return log_link_warning_errno(link, errno, "Could not find udev device: %m"); | |
505f8da7 | 1875 | |
3c4cb064 | 1876 | if (udev_device_get_is_initialized(device) <= 0) { |
505f8da7 | 1877 | /* not yet ready */ |
79008bdd | 1878 | log_link_debug(link, "link pending udev initialization..."); |
505f8da7 | 1879 | return 0; |
3c4cb064 | 1880 | } |
505f8da7 | 1881 | |
4f561e8e TG |
1882 | r = link_initialized(link, device); |
1883 | if (r < 0) | |
1884 | return r; | |
1885 | } else { | |
5da8149f TG |
1886 | /* we are calling a callback directly, so must take a ref */ |
1887 | link_ref(link); | |
1888 | ||
4f561e8e TG |
1889 | r = link_initialized_and_synced(m->rtnl, NULL, link); |
1890 | if (r < 0) | |
1891 | return r; | |
1892 | } | |
505f8da7 | 1893 | |
a748b692 TG |
1894 | return 0; |
1895 | } | |
1896 | ||
9c0a72f9 TG |
1897 | static int link_carrier_gained(Link *link) { |
1898 | int r; | |
1899 | ||
1900 | assert(link); | |
1901 | ||
1902 | if (link->network) { | |
1903 | r = link_acquire_conf(link); | |
1904 | if (r < 0) { | |
1905 | link_enter_failed(link); | |
1906 | return r; | |
1907 | } | |
1908 | } | |
1909 | ||
0d4ad91d AR |
1910 | r = link_handle_bound_by_list(link); |
1911 | if (r < 0) | |
1912 | return r; | |
1913 | ||
9c0a72f9 TG |
1914 | return 0; |
1915 | } | |
1916 | ||
1917 | static int link_carrier_lost(Link *link) { | |
1918 | int r; | |
1919 | ||
1920 | assert(link); | |
1921 | ||
1922 | r = link_stop_clients(link); | |
1923 | if (r < 0) { | |
1924 | link_enter_failed(link); | |
1925 | return r; | |
1926 | } | |
1927 | ||
0d4ad91d AR |
1928 | r = link_handle_bound_by_list(link); |
1929 | if (r < 0) | |
1930 | return r; | |
1931 | ||
9c0a72f9 TG |
1932 | return 0; |
1933 | } | |
1934 | ||
1935 | int link_carrier_reset(Link *link) { | |
1936 | int r; | |
1937 | ||
1938 | assert(link); | |
1939 | ||
1940 | if (link_has_carrier(link)) { | |
1941 | r = link_carrier_lost(link); | |
1942 | if (r < 0) | |
1943 | return r; | |
1944 | ||
1945 | r = link_carrier_gained(link); | |
1946 | if (r < 0) | |
1947 | return r; | |
1948 | ||
6a7a4e4d | 1949 | log_link_info(link, "Reset carrier"); |
9c0a72f9 TG |
1950 | } |
1951 | ||
1952 | return 0; | |
1953 | } | |
1954 | ||
1955 | ||
22936833 | 1956 | int link_update(Link *link, sd_rtnl_message *m) { |
c49b33ac | 1957 | struct ether_addr mac; |
ca4e095a | 1958 | const char *ifname; |
afe7fd56 | 1959 | uint32_t mtu; |
a61bb41c | 1960 | bool had_carrier, carrier_gained, carrier_lost; |
22936833 TG |
1961 | int r; |
1962 | ||
dd3efc09 | 1963 | assert(link); |
b8941f74 | 1964 | assert(link->ifname); |
22936833 TG |
1965 | assert(m); |
1966 | ||
7619683b TG |
1967 | if (link->state == LINK_STATE_LINGER) { |
1968 | link_ref(link); | |
6a7a4e4d | 1969 | log_link_info(link, "Link readded"); |
e331e246 | 1970 | link_set_state(link, LINK_STATE_ENSLAVING); |
0d4ad91d AR |
1971 | |
1972 | r = link_new_carrier_maps(link); | |
1973 | if (r < 0) | |
1974 | return r; | |
7619683b TG |
1975 | } |
1976 | ||
b8941f74 TG |
1977 | r = sd_rtnl_message_read_string(m, IFLA_IFNAME, &ifname); |
1978 | if (r >= 0 && !streq(ifname, link->ifname)) { | |
6a7a4e4d | 1979 | log_link_info(link, "Renamed to %s", ifname); |
b8941f74 | 1980 | |
0d4ad91d AR |
1981 | link_free_carrier_maps(link); |
1982 | ||
b8941f74 TG |
1983 | free(link->ifname); |
1984 | link->ifname = strdup(ifname); | |
1985 | if (!link->ifname) | |
1986 | return -ENOMEM; | |
0d4ad91d AR |
1987 | |
1988 | r = link_new_carrier_maps(link); | |
1989 | if (r < 0) | |
1990 | return r; | |
b8941f74 TG |
1991 | } |
1992 | ||
afe7fd56 TG |
1993 | r = sd_rtnl_message_read_u32(m, IFLA_MTU, &mtu); |
1994 | if (r >= 0 && mtu > 0) { | |
1995 | link->mtu = mtu; | |
1996 | if (!link->original_mtu) { | |
1997 | link->original_mtu = mtu; | |
6a7a4e4d | 1998 | log_link_debug(link, "Saved original MTU: %" PRIu32, link->original_mtu); |
afe7fd56 TG |
1999 | } |
2000 | ||
2001 | if (link->dhcp_client) { | |
3c9b8860 TG |
2002 | r = sd_dhcp_client_set_mtu(link->dhcp_client, |
2003 | link->mtu); | |
afe7fd56 | 2004 | if (r < 0) { |
6a7a4e4d | 2005 | log_link_warning_errno(link, r, "Could not update MTU in DHCP client: %m"); |
afe7fd56 TG |
2006 | return r; |
2007 | } | |
2008 | } | |
9842de0d | 2009 | } |
69629de9 | 2010 | |
e9189a1f TG |
2011 | /* The kernel may broadcast NEWLINK messages without the MAC address |
2012 | set, simply ignore them. */ | |
c49b33ac | 2013 | r = sd_rtnl_message_read_ether_addr(m, IFLA_ADDRESS, &mac); |
e9189a1f | 2014 | if (r >= 0) { |
3c9b8860 TG |
2015 | if (memcmp(link->mac.ether_addr_octet, mac.ether_addr_octet, |
2016 | ETH_ALEN)) { | |
c49b33ac | 2017 | |
3c9b8860 TG |
2018 | memcpy(link->mac.ether_addr_octet, mac.ether_addr_octet, |
2019 | ETH_ALEN); | |
c49b33ac | 2020 | |
79008bdd | 2021 | log_link_debug(link, "MAC address: " |
20861203 TG |
2022 | "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", |
2023 | mac.ether_addr_octet[0], | |
2024 | mac.ether_addr_octet[1], | |
2025 | mac.ether_addr_octet[2], | |
2026 | mac.ether_addr_octet[3], | |
2027 | mac.ether_addr_octet[4], | |
2028 | mac.ether_addr_octet[5]); | |
c49b33ac | 2029 | |
20861203 TG |
2030 | if (link->ipv4ll) { |
2031 | r = sd_ipv4ll_set_mac(link->ipv4ll, &link->mac); | |
6a7a4e4d LP |
2032 | if (r < 0) |
2033 | return log_link_warning_errno(link, r, "Could not update MAC address in IPv4LL client: %m"); | |
c49b33ac | 2034 | } |
c49b33ac | 2035 | |
20861203 | 2036 | if (link->dhcp_client) { |
3c9b8860 | 2037 | r = sd_dhcp_client_set_mac(link->dhcp_client, |
76253e73 DW |
2038 | (const uint8_t *) &link->mac, |
2039 | sizeof (link->mac), | |
2040 | ARPHRD_ETHER); | |
6a7a4e4d LP |
2041 | if (r < 0) |
2042 | return log_link_warning_errno(link, r, "Could not update MAC address in DHCP client: %m"); | |
c49b33ac | 2043 | } |
4138fb2c PF |
2044 | |
2045 | if (link->dhcp6_client) { | |
2046 | r = sd_dhcp6_client_set_mac(link->dhcp6_client, | |
76253e73 DW |
2047 | (const uint8_t *) &link->mac, |
2048 | sizeof (link->mac), | |
2049 | ARPHRD_ETHER); | |
6a7a4e4d LP |
2050 | if (r < 0) |
2051 | return log_link_warning_errno(link, r, "Could not update MAC address in DHCPv6 client: %m"); | |
4138fb2c | 2052 | } |
c49b33ac | 2053 | } |
4f882b2a TG |
2054 | } |
2055 | ||
a61bb41c TG |
2056 | had_carrier = link_has_carrier(link); |
2057 | ||
2058 | r = link_update_flags(link, m); | |
2059 | if (r < 0) | |
2060 | return r; | |
2061 | ||
2062 | carrier_gained = !had_carrier && link_has_carrier(link); | |
2063 | carrier_lost = had_carrier && !link_has_carrier(link); | |
2064 | ||
2065 | if (carrier_gained) { | |
6a7a4e4d | 2066 | log_link_info(link, "Gained carrier"); |
a61bb41c | 2067 | |
9c0a72f9 TG |
2068 | r = link_carrier_gained(link); |
2069 | if (r < 0) | |
2070 | return r; | |
a61bb41c | 2071 | } else if (carrier_lost) { |
6a7a4e4d | 2072 | log_link_info(link, "Lost carrier"); |
a61bb41c | 2073 | |
9c0a72f9 TG |
2074 | r = link_carrier_lost(link); |
2075 | if (r < 0) | |
a61bb41c | 2076 | return r; |
9c0a72f9 | 2077 | |
a61bb41c TG |
2078 | } |
2079 | ||
2080 | return 0; | |
dd3efc09 | 2081 | } |
fe8db0c5 | 2082 | |
e375dcde | 2083 | static void link_update_operstate(Link *link) { |
e331e246 | 2084 | LinkOperationalState operstate; |
e375dcde TG |
2085 | assert(link); |
2086 | ||
2087 | if (link->kernel_operstate == IF_OPER_DORMANT) | |
e331e246 | 2088 | operstate = LINK_OPERSTATE_DORMANT; |
a61bb41c | 2089 | else if (link_has_carrier(link)) { |
e375dcde TG |
2090 | Address *address; |
2091 | uint8_t scope = RT_SCOPE_NOWHERE; | |
2092 | ||
2093 | /* if we have carrier, check what addresses we have */ | |
2094 | LIST_FOREACH(addresses, address, link->addresses) { | |
81163121 TG |
2095 | if (address->flags & (IFA_F_TENTATIVE | IFA_F_DEPRECATED)) |
2096 | continue; | |
2097 | ||
e375dcde TG |
2098 | if (address->scope < scope) |
2099 | scope = address->scope; | |
2100 | } | |
2101 | ||
2102 | if (scope < RT_SCOPE_SITE) | |
2103 | /* universally accessible addresses found */ | |
e331e246 | 2104 | operstate = LINK_OPERSTATE_ROUTABLE; |
e375dcde TG |
2105 | else if (scope < RT_SCOPE_HOST) |
2106 | /* only link or site local addresses found */ | |
e331e246 | 2107 | operstate = LINK_OPERSTATE_DEGRADED; |
e375dcde TG |
2108 | else |
2109 | /* no useful addresses found */ | |
e331e246 | 2110 | operstate = LINK_OPERSTATE_CARRIER; |
54cba0b1 | 2111 | } else if (link->flags & IFF_UP) |
e331e246 | 2112 | operstate = LINK_OPERSTATE_NO_CARRIER; |
54cba0b1 | 2113 | else |
e331e246 TG |
2114 | operstate = LINK_OPERSTATE_OFF; |
2115 | ||
2116 | if (link->operstate != operstate) { | |
2117 | link->operstate = operstate; | |
2118 | link_send_changed(link, "OperationalState", NULL); | |
2119 | } | |
e375dcde TG |
2120 | } |
2121 | ||
fe8db0c5 | 2122 | int link_save(Link *link) { |
68a8723c | 2123 | _cleanup_free_ char *temp_path = NULL; |
fe8db0c5 | 2124 | _cleanup_fclose_ FILE *f = NULL; |
e375dcde | 2125 | const char *admin_state, *oper_state; |
fe8db0c5 TG |
2126 | int r; |
2127 | ||
2128 | assert(link); | |
2129 | assert(link->state_file); | |
68a8723c | 2130 | assert(link->lease_file); |
bbf7c048 TG |
2131 | assert(link->manager); |
2132 | ||
e375dcde TG |
2133 | link_update_operstate(link); |
2134 | ||
bbf7c048 TG |
2135 | r = manager_save(link->manager); |
2136 | if (r < 0) | |
2137 | return r; | |
fe8db0c5 | 2138 | |
370e9930 TG |
2139 | if (link->state == LINK_STATE_LINGER) { |
2140 | unlink(link->state_file); | |
2141 | return 0; | |
2142 | } | |
2143 | ||
deb2e523 TG |
2144 | admin_state = link_state_to_string(link->state); |
2145 | assert(admin_state); | |
2146 | ||
e375dcde TG |
2147 | oper_state = link_operstate_to_string(link->operstate); |
2148 | assert(oper_state); | |
deb2e523 | 2149 | |
fe8db0c5 TG |
2150 | r = fopen_temporary(link->state_file, &f, &temp_path); |
2151 | if (r < 0) | |
6a7a4e4d | 2152 | goto fail; |
fe8db0c5 TG |
2153 | |
2154 | fchmod(fileno(f), 0644); | |
2155 | ||
2156 | fprintf(f, | |
2157 | "# This is private data. Do not parse.\n" | |
deb2e523 | 2158 | "ADMIN_STATE=%s\n" |
6dcaa6f5 TG |
2159 | "OPER_STATE=%s\n", |
2160 | admin_state, oper_state); | |
fe8db0c5 | 2161 | |
bcb7a07e | 2162 | if (link->network) { |
ea352b40 LP |
2163 | char **address, **domain; |
2164 | bool space; | |
b0e39c82 | 2165 | |
adc5b2e2 TG |
2166 | fprintf(f, "NETWORK_FILE=%s\n", link->network->filename); |
2167 | ||
b0e39c82 | 2168 | fputs("DNS=", f); |
ea352b40 LP |
2169 | space = false; |
2170 | STRV_FOREACH(address, link->network->dns) { | |
2171 | if (space) | |
2172 | fputc(' ', f); | |
2173 | fputs(*address, f); | |
2174 | space = true; | |
2175 | } | |
d5314fff | 2176 | |
b0e39c82 TG |
2177 | if (link->network->dhcp_dns && |
2178 | link->dhcp_lease) { | |
2179 | const struct in_addr *addresses; | |
2180 | ||
2181 | r = sd_dhcp_lease_get_dns(link->dhcp_lease, &addresses); | |
2182 | if (r > 0) { | |
ea352b40 LP |
2183 | if (space) |
2184 | fputc(' ', f); | |
b0e39c82 | 2185 | serialize_in_addrs(f, addresses, r); |
b0e39c82 TG |
2186 | } |
2187 | } | |
2188 | ||
b0e39c82 TG |
2189 | fputs("\n", f); |
2190 | ||
2191 | fprintf(f, "NTP="); | |
ea352b40 LP |
2192 | space = false; |
2193 | STRV_FOREACH(address, link->network->ntp) { | |
2194 | if (space) | |
2195 | fputc(' ', f); | |
2196 | fputs(*address, f); | |
2197 | space = true; | |
2198 | } | |
d5314fff | 2199 | |
b0e39c82 TG |
2200 | if (link->network->dhcp_ntp && |
2201 | link->dhcp_lease) { | |
2202 | const struct in_addr *addresses; | |
2203 | ||
2204 | r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &addresses); | |
2205 | if (r > 0) { | |
ea352b40 LP |
2206 | if (space) |
2207 | fputc(' ', f); | |
b0e39c82 | 2208 | serialize_in_addrs(f, addresses, r); |
b0e39c82 TG |
2209 | } |
2210 | } | |
2211 | ||
b0e39c82 | 2212 | fputs("\n", f); |
bd8f6538 | 2213 | |
6192b846 | 2214 | fprintf(f, "DOMAINS="); |
ea352b40 LP |
2215 | space = false; |
2216 | STRV_FOREACH(domain, link->network->domains) { | |
2217 | if (space) | |
2218 | fputc(' ', f); | |
2219 | fputs(*domain, f); | |
2220 | space = true; | |
2221 | } | |
d5314fff | 2222 | |
ad0734e8 | 2223 | if (link->network->dhcp_domains && |
9b4d1882 TG |
2224 | link->dhcp_lease) { |
2225 | const char *domainname; | |
2226 | ||
2227 | r = sd_dhcp_lease_get_domainname(link->dhcp_lease, &domainname); | |
6192b846 | 2228 | if (r >= 0) { |
ea352b40 LP |
2229 | if (space) |
2230 | fputc(' ', f); | |
6192b846 | 2231 | fputs(domainname, f); |
6192b846 | 2232 | } |
9b4d1882 TG |
2233 | } |
2234 | ||
6192b846 TG |
2235 | fputs("\n", f); |
2236 | ||
67272d15 TG |
2237 | fprintf(f, "WILDCARD_DOMAIN=%s\n", |
2238 | yes_no(link->network->wildcard_domain)); | |
2239 | ||
3c9b8860 TG |
2240 | fprintf(f, "LLMNR=%s\n", |
2241 | llmnr_support_to_string(link->network->llmnr)); | |
bcb7a07e | 2242 | } |
7374f9d8 | 2243 | |
0d4ad91d AR |
2244 | if (!hashmap_isempty(link->bound_to_links)) { |
2245 | Link *carrier; | |
2246 | Iterator i; | |
2247 | bool space = false; | |
2248 | ||
2249 | fputs("CARRIER_BOUND_TO=", f); | |
2250 | HASHMAP_FOREACH(carrier, link->bound_to_links, i) { | |
2251 | if (space) | |
2252 | fputc(' ', f); | |
2253 | fputs(carrier->ifname, f); | |
2254 | space = true; | |
2255 | } | |
2256 | ||
2257 | fputs("\n", f); | |
2258 | } | |
2259 | ||
2260 | if (!hashmap_isempty(link->bound_by_links)) { | |
2261 | Link *carrier; | |
2262 | Iterator i; | |
2263 | bool space = false; | |
2264 | ||
2265 | fputs("CARRIER_BOUND_BY=", f); | |
0d4ad91d AR |
2266 | HASHMAP_FOREACH(carrier, link->bound_by_links, i) { |
2267 | if (space) | |
2268 | fputc(' ', f); | |
2269 | fputs(carrier->ifname, f); | |
2270 | space = true; | |
2271 | } | |
2272 | ||
2273 | fputs("\n", f); | |
2274 | } | |
2275 | ||
fe8db0c5 | 2276 | if (link->dhcp_lease) { |
d9876a52 TG |
2277 | assert(link->network); |
2278 | ||
1dc24d5f | 2279 | r = sd_dhcp_lease_save(link->dhcp_lease, link->lease_file); |
fe8db0c5 | 2280 | if (r < 0) |
c2d6bd61 | 2281 | goto fail; |
fe8db0c5 | 2282 | |
7374f9d8 | 2283 | fprintf(f, |
b0e39c82 TG |
2284 | "DHCP_LEASE=%s\n", |
2285 | link->lease_file); | |
deb2e523 | 2286 | } else |
68a8723c | 2287 | unlink(link->lease_file); |
fe8db0c5 | 2288 | |
49699bac SS |
2289 | if (link->lldp) { |
2290 | assert(link->network); | |
2291 | ||
2292 | r = sd_lldp_save(link->lldp, link->lldp_file); | |
2293 | if (r < 0) | |
2294 | goto fail; | |
2295 | ||
2296 | fprintf(f, | |
2297 | "LLDP_FILE=%s\n", | |
2298 | link->lldp_file); | |
2299 | } else | |
2300 | unlink(link->lldp_file); | |
2301 | ||
c2d6bd61 LP |
2302 | r = fflush_and_check(f); |
2303 | if (r < 0) | |
2304 | goto fail; | |
fe8db0c5 | 2305 | |
c2d6bd61 | 2306 | if (rename(temp_path, link->state_file) < 0) { |
fe8db0c5 | 2307 | r = -errno; |
c2d6bd61 | 2308 | goto fail; |
fe8db0c5 TG |
2309 | } |
2310 | ||
c2d6bd61 | 2311 | return 0; |
c2d6bd61 | 2312 | fail: |
6a7a4e4d LP |
2313 | log_link_error_errno(link, r, "Failed to save link data to %s: %m", link->state_file); |
2314 | (void) unlink(link->state_file); | |
2315 | ||
2316 | if (temp_path) | |
2317 | (void) unlink(temp_path); | |
2318 | ||
fe8db0c5 TG |
2319 | return r; |
2320 | } | |
2321 | ||
2322 | static const char* const link_state_table[_LINK_STATE_MAX] = { | |
8434fd5c | 2323 | [LINK_STATE_PENDING] = "pending", |
fe8db0c5 TG |
2324 | [LINK_STATE_ENSLAVING] = "configuring", |
2325 | [LINK_STATE_SETTING_ADDRESSES] = "configuring", | |
2326 | [LINK_STATE_SETTING_ROUTES] = "configuring", | |
2327 | [LINK_STATE_CONFIGURED] = "configured", | |
57bd6899 | 2328 | [LINK_STATE_UNMANAGED] = "unmanaged", |
fe8db0c5 | 2329 | [LINK_STATE_FAILED] = "failed", |
370e9930 | 2330 | [LINK_STATE_LINGER] = "linger", |
fe8db0c5 TG |
2331 | }; |
2332 | ||
2333 | DEFINE_STRING_TABLE_LOOKUP(link_state, LinkState); | |
e375dcde TG |
2334 | |
2335 | static const char* const link_operstate_table[_LINK_OPERSTATE_MAX] = { | |
d3df0e39 TG |
2336 | [LINK_OPERSTATE_OFF] = "off", |
2337 | [LINK_OPERSTATE_NO_CARRIER] = "no-carrier", | |
e375dcde TG |
2338 | [LINK_OPERSTATE_DORMANT] = "dormant", |
2339 | [LINK_OPERSTATE_CARRIER] = "carrier", | |
2340 | [LINK_OPERSTATE_DEGRADED] = "degraded", | |
2341 | [LINK_OPERSTATE_ROUTABLE] = "routable", | |
2342 | }; | |
2343 | ||
2344 | DEFINE_STRING_TABLE_LOOKUP(link_operstate, LinkOperationalState); |