chiark / gitweb /
udev: support ENV{}=="" global property matches
[elogind.git] / src / network / networkd-link.c
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>
24 #include <unistd.h>
25
26 #include "networkd-link.h"
27 #include "networkd-netdev.h"
28 #include "libudev-private.h"
29 #include "udev-util.h"
30 #include "util.h"
31 #include "virt.h"
32 #include "bus-util.h"
33 #include "network-internal.h"
34 #include "conf-parser.h"
35
36 #include "dhcp-lease-internal.h"
37
38 static bool link_dhcp6_enabled(Link *link) {
39         if (link->flags & IFF_LOOPBACK)
40                 return false;
41
42         if (!link->network)
43                 return false;
44
45         return IN_SET(link->network->dhcp, DHCP_SUPPORT_V6, DHCP_SUPPORT_BOTH);
46 }
47
48 static bool link_dhcp4_enabled(Link *link) {
49         if (link->flags & IFF_LOOPBACK)
50                 return false;
51
52         if (!link->network)
53                 return false;
54
55         return IN_SET(link->network->dhcp, DHCP_SUPPORT_V4, DHCP_SUPPORT_BOTH);
56 }
57
58 static bool link_dhcp4_server_enabled(Link *link) {
59         if (link->flags & IFF_LOOPBACK)
60                 return false;
61
62         if (!link->network)
63                 return false;
64
65         return link->network->dhcp_server;
66 }
67
68 static bool link_ipv4ll_enabled(Link *link) {
69         if (link->flags & IFF_LOOPBACK)
70                 return false;
71
72         if (!link->network)
73                 return false;
74
75         return link->network->ipv4ll;
76 }
77
78 #define FLAG_STRING(string, flag, old, new) \
79         (((old ^ new) & flag) \
80                 ? ((old & flag) ? (" -" string) : (" +" string)) \
81                 : "")
82
83 static int link_update_flags(Link *link, sd_rtnl_message *m) {
84         unsigned flags, unknown_flags_added, unknown_flags_removed, unknown_flags;
85         uint8_t operstate;
86         int r;
87
88         assert(link);
89
90         r = sd_rtnl_message_link_get_flags(m, &flags);
91         if (r < 0) {
92                 log_warning_link(link, "Could not get link flags");
93                 return r;
94         }
95
96         r = sd_rtnl_message_read_u8(m, IFLA_OPERSTATE, &operstate);
97         if (r < 0)
98                 /* if we got a message without operstate, take it to mean
99                    the state was unchanged */
100                 operstate = link->kernel_operstate;
101
102         if ((link->flags == flags) && (link->kernel_operstate == operstate))
103                 return 0;
104
105         if (link->flags != flags) {
106                 log_debug_link(link, "flags change:%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
107                                FLAG_STRING("LOOPBACK", IFF_LOOPBACK, link->flags, flags),
108                                FLAG_STRING("MASTER", IFF_MASTER, link->flags, flags),
109                                FLAG_STRING("SLAVE", IFF_SLAVE, link->flags, flags),
110                                FLAG_STRING("UP", IFF_UP, link->flags, flags),
111                                FLAG_STRING("DORMANT", IFF_DORMANT, link->flags, flags),
112                                FLAG_STRING("LOWER_UP", IFF_LOWER_UP, link->flags, flags),
113                                FLAG_STRING("RUNNING", IFF_RUNNING, link->flags, flags),
114                                FLAG_STRING("MULTICAST", IFF_MULTICAST, link->flags, flags),
115                                FLAG_STRING("BROADCAST", IFF_BROADCAST, link->flags, flags),
116                                FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT, link->flags, flags),
117                                FLAG_STRING("PROMISC", IFF_PROMISC, link->flags, flags),
118                                FLAG_STRING("ALLMULTI", IFF_ALLMULTI, link->flags, flags),
119                                FLAG_STRING("PORTSEL", IFF_PORTSEL, link->flags, flags),
120                                FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA, link->flags, flags),
121                                FLAG_STRING("DYNAMIC", IFF_DYNAMIC, link->flags, flags),
122                                FLAG_STRING("NOARP", IFF_NOARP, link->flags, flags),
123                                FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS, link->flags, flags),
124                                FLAG_STRING("DEBUG", IFF_DEBUG, link->flags, flags),
125                                FLAG_STRING("ECHO", IFF_ECHO, link->flags, flags));
126
127                 unknown_flags = ~(IFF_LOOPBACK | IFF_MASTER | IFF_SLAVE | IFF_UP |
128                                   IFF_DORMANT | IFF_LOWER_UP | IFF_RUNNING |
129                                   IFF_MULTICAST | IFF_BROADCAST | IFF_POINTOPOINT |
130                                   IFF_PROMISC | IFF_ALLMULTI | IFF_PORTSEL |
131                                   IFF_AUTOMEDIA | IFF_DYNAMIC | IFF_NOARP |
132                                   IFF_NOTRAILERS | IFF_DEBUG | IFF_ECHO);
133                 unknown_flags_added = ((link->flags ^ flags) & flags & unknown_flags);
134                 unknown_flags_removed = ((link->flags ^ flags) & link->flags & unknown_flags);
135
136                 /* link flags are currently at most 18 bits, let's align to
137                  * printing 20 */
138                 if (unknown_flags_added)
139                         log_debug_link(link,
140                                        "unknown link flags gained: %#.5x (ignoring)",
141                                        unknown_flags_added);
142
143                 if (unknown_flags_removed)
144                         log_debug_link(link,
145                                        "unknown link flags lost: %#.5x (ignoring)",
146                                        unknown_flags_removed);
147         }
148
149         link->flags = flags;
150         link->kernel_operstate = operstate;
151
152         link_save(link);
153
154         return 0;
155 }
156
157 static int link_new(Manager *manager, sd_rtnl_message *message, Link **ret) {
158         _cleanup_link_unref_ Link *link = NULL;
159         uint16_t type;
160         const char *ifname;
161         int r, ifindex;
162
163         assert(manager);
164         assert(message);
165         assert(ret);
166
167         r = sd_rtnl_message_get_type(message, &type);
168         if (r < 0)
169                 return r;
170         else if (type != RTM_NEWLINK)
171                 return -EINVAL;
172
173         r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
174         if (r < 0)
175                 return r;
176         else if (ifindex <= 0)
177                 return -EINVAL;
178
179         r = sd_rtnl_message_read_string(message, IFLA_IFNAME, &ifname);
180         if (r < 0)
181                 return r;
182
183         link = new0(Link, 1);
184         if (!link)
185                 return -ENOMEM;
186
187         link->n_ref = 1;
188         link->manager = manager;
189         link->state = LINK_STATE_PENDING;
190         link->ifindex = ifindex;
191         link->ifname = strdup(ifname);
192         if (!link->ifname)
193                 return -ENOMEM;
194
195         r = sd_rtnl_message_read_ether_addr(message, IFLA_ADDRESS, &link->mac);
196         if (r < 0)
197                 log_debug_link(link, "MAC address not found for new device, continuing without");
198
199         r = asprintf(&link->state_file, "/run/systemd/netif/links/%d",
200                      link->ifindex);
201         if (r < 0)
202                 return -ENOMEM;
203
204         r = asprintf(&link->lease_file, "/run/systemd/netif/leases/%d",
205                      link->ifindex);
206         if (r < 0)
207                 return -ENOMEM;
208
209         r = hashmap_ensure_allocated(&manager->links, NULL);
210         if (r < 0)
211                 return r;
212
213         r = hashmap_put(manager->links, INT_TO_PTR(link->ifindex), link);
214         if (r < 0)
215                 return r;
216
217         r = link_update_flags(link, message);
218         if (r < 0)
219                 return r;
220
221         *ret = link;
222         link = NULL;
223
224         return 0;
225 }
226
227 static void link_free(Link *link) {
228         Address *address;
229
230         if (!link)
231                 return;
232
233         while ((address = link->addresses)) {
234                 LIST_REMOVE(addresses, link->addresses, address);
235                 address_free(address);
236         }
237
238         while ((address = link->pool_addresses)) {
239                 LIST_REMOVE(addresses, link->pool_addresses, address);
240                 address_free(address);
241         }
242
243         sd_dhcp_client_unref(link->dhcp_client);
244         sd_dhcp_lease_unref(link->dhcp_lease);
245
246         unlink(link->lease_file);
247         free(link->lease_file);
248
249         sd_ipv4ll_unref(link->ipv4ll);
250         sd_dhcp6_client_unref(link->dhcp6_client);
251         sd_icmp6_nd_unref(link->icmp6_router_discovery);
252
253         if (link->manager)
254                 hashmap_remove(link->manager->links, INT_TO_PTR(link->ifindex));
255
256         free(link->ifname);
257
258         unlink(link->state_file);
259         free(link->state_file);
260
261         udev_device_unref(link->udev_device);
262
263         free(link);
264 }
265
266 Link *link_unref(Link *link) {
267         if (link && (-- link->n_ref <= 0))
268                 link_free(link);
269
270         return NULL;
271 }
272
273 Link *link_ref(Link *link) {
274         if (link)
275                 assert_se(++ link->n_ref >= 2);
276
277         return link;
278 }
279
280 int link_get(Manager *m, int ifindex, Link **ret) {
281         Link *link;
282
283         assert(m);
284         assert(ifindex);
285         assert(ret);
286
287         link = hashmap_get(m->links, INT_TO_PTR(ifindex));
288         if (!link)
289                 return -ENODEV;
290
291         *ret = link;
292
293         return 0;
294 }
295
296 void link_drop(Link *link) {
297         if (!link || link->state == LINK_STATE_LINGER)
298                 return;
299
300         link->state = LINK_STATE_LINGER;
301
302         log_debug_link(link, "link removed");
303
304         link_unref(link);
305
306         return;
307 }
308
309 static void link_enter_unmanaged(Link *link) {
310         assert(link);
311
312         log_debug_link(link, "unmanaged");
313
314         link->state = LINK_STATE_UNMANAGED;
315
316         link_save(link);
317 }
318
319 static int link_stop_clients(Link *link) {
320         int r = 0, k;
321
322         assert(link);
323         assert(link->manager);
324         assert(link->manager->event);
325
326         if (!link->network)
327                 return 0;
328
329         if (link->dhcp_client) {
330                 k = sd_dhcp_client_stop(link->dhcp_client);
331                 if (k < 0) {
332                         log_warning_link(link, "Could not stop DHCPv4 client: %s",
333                                          strerror(-r));
334                         r = k;
335                 }
336         }
337
338         if (link->ipv4ll) {
339                 k = sd_ipv4ll_stop(link->ipv4ll);
340                 if (k < 0) {
341                         log_warning_link(link, "Could not stop IPv4 link-local: %s",
342                                          strerror(-r));
343                         r = k;
344                 }
345         }
346
347         if(link->icmp6_router_discovery) {
348
349                 if (link->dhcp6_client) {
350                         k = sd_dhcp6_client_stop(link->dhcp6_client);
351                         if (k < 0) {
352                                 log_warning_link(link, "Could not stop DHCPv6 client: %s",
353                                                  strerror(-r));
354                                 r = k;
355                         }
356                 }
357
358                 k = sd_icmp6_nd_stop(link->icmp6_router_discovery);
359                 if (k < 0) {
360                         log_warning_link(link,
361                                          "Could not stop ICMPv6 router discovery: %s",
362                                          strerror(-r));
363                         r = k;
364                 }
365         }
366
367         return r;
368 }
369
370 void link_enter_failed(Link *link) {
371         assert(link);
372
373         if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
374                 return;
375
376         log_warning_link(link, "failed");
377
378         link->state = LINK_STATE_FAILED;
379
380         link_stop_clients(link);
381
382         link_save(link);
383 }
384
385 static Address* link_find_dhcp_server_address(Link *link) {
386         Address *address;
387
388         assert(link);
389         assert(link->network);
390
391         /* The the first statically configured address if there is any */
392         LIST_FOREACH(addresses, address, link->network->static_addresses) {
393
394                 if (address->family != AF_INET)
395                         continue;
396
397                 if (in_addr_is_null(address->family, &address->in_addr))
398                         continue;
399
400                 return address;
401         }
402
403         /* If that didn't work, find a suitable address we got from the pool */
404         LIST_FOREACH(addresses, address, link->pool_addresses) {
405                 if (address->family != AF_INET)
406                         continue;
407
408                 return address;
409         }
410
411         return NULL;
412 }
413
414 static int link_enter_configured(Link *link) {
415         int r;
416
417         assert(link);
418         assert(link->network);
419         assert(link->state == LINK_STATE_SETTING_ROUTES);
420
421         if (link_dhcp4_server_enabled(link) &&
422             !sd_dhcp_server_is_running(link->dhcp_server)) {
423                 struct in_addr pool_start;
424                 Address *address;
425
426                 address = link_find_dhcp_server_address(link);
427                 if (!address) {
428                         log_warning_link(link,
429                                          "Failed to find suitable address for DHCPv4 server instance.");
430                         link_enter_failed(link);
431                         return 0;
432                 }
433
434                 log_debug_link(link, "offering DHCPv4 leases");
435
436                 r = sd_dhcp_server_set_address(link->dhcp_server,
437                                                &address->in_addr.in,
438                                                address->prefixlen);
439                 if (r < 0)
440                         return r;
441
442                 /* offer 32 addresses starting from the address following the server address */
443                 pool_start.s_addr = htobe32(be32toh(address->in_addr.in.s_addr) + 1);
444                 r = sd_dhcp_server_set_lease_pool(link->dhcp_server,
445                                                   &pool_start, 32);
446                 if (r < 0)
447                         return r;
448
449                 /* TODO:
450                 r = sd_dhcp_server_set_router(link->dhcp_server,
451                                               &main_address->in_addr.in);
452                 if (r < 0)
453                         return r;
454
455                 r = sd_dhcp_server_set_prefixlen(link->dhcp_server,
456                                                  main_address->prefixlen);
457                 if (r < 0)
458                         return r;
459                 */
460
461                 r = sd_dhcp_server_start(link->dhcp_server);
462                 if (r < 0) {
463                         log_warning_link(link, "could not start DHCPv4 server "
464                                          "instance: %s", strerror(-r));
465
466                         link_enter_failed(link);
467
468                         return 0;
469                 }
470         }
471
472         log_info_link(link, "link configured");
473
474         link->state = LINK_STATE_CONFIGURED;
475
476         link_save(link);
477
478         return 0;
479 }
480
481 void link_client_handler(Link *link) {
482         assert(link);
483         assert(link->network);
484
485         if (!link->static_configured)
486                 return;
487
488         if (link_ipv4ll_enabled(link))
489                 if (!link->ipv4ll_address ||
490                     !link->ipv4ll_route)
491                         return;
492
493         if (link_dhcp4_enabled(link) && !link->dhcp4_configured)
494                         return;
495
496         if (link->state != LINK_STATE_CONFIGURED)
497                 link_enter_configured(link);
498
499         return;
500 }
501
502 static int route_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
503         _cleanup_link_unref_ Link *link = userdata;
504         int r;
505
506         assert(link->link_messages > 0);
507         assert(IN_SET(link->state, LINK_STATE_SETTING_ADDRESSES,
508                       LINK_STATE_SETTING_ROUTES, LINK_STATE_FAILED,
509                       LINK_STATE_LINGER));
510
511         link->link_messages --;
512
513         if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
514                 return 1;
515
516         r = sd_rtnl_message_get_errno(m);
517         if (r < 0 && r != -EEXIST)
518                 log_struct_link(LOG_WARNING, link,
519                                 "MESSAGE=%-*s: could not set route: %s",
520                                 IFNAMSIZ,
521                                 link->ifname, strerror(-r),
522                                 "ERRNO=%d", -r,
523                                 NULL);
524
525         if (link->link_messages == 0) {
526                 log_debug_link(link, "routes set");
527                 link->static_configured = true;
528                 link_client_handler(link);
529         }
530
531         return 1;
532 }
533
534 static int link_enter_set_routes(Link *link) {
535         Route *rt;
536         int r;
537
538         assert(link);
539         assert(link->network);
540         assert(link->state == LINK_STATE_SETTING_ADDRESSES);
541
542         link->state = LINK_STATE_SETTING_ROUTES;
543
544         LIST_FOREACH(routes, rt, link->network->static_routes) {
545                 r = route_configure(rt, link, &route_handler);
546                 if (r < 0) {
547                         log_warning_link(link,
548                                          "could not set routes: %s",
549                                          strerror(-r));
550                         link_enter_failed(link);
551                         return r;
552                 }
553
554                 link->link_messages ++;
555         }
556
557         if (link->link_messages == 0) {
558                 link->static_configured = true;
559                 link_client_handler(link);
560         } else
561                 log_debug_link(link, "setting routes");
562
563         return 0;
564 }
565
566 int link_route_drop_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
567         _cleanup_link_unref_ Link *link = userdata;
568         int r;
569
570         assert(m);
571         assert(link);
572         assert(link->ifname);
573
574         if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
575                 return 1;
576
577         r = sd_rtnl_message_get_errno(m);
578         if (r < 0 && r != -ESRCH)
579                 log_struct_link(LOG_WARNING, link,
580                                 "MESSAGE=%-*s: could not drop route: %s",
581                                 IFNAMSIZ,
582                                 link->ifname, strerror(-r),
583                                 "ERRNO=%d", -r,
584                                 NULL);
585
586         return 1;
587 }
588
589 int link_get_address_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
590         _cleanup_link_unref_ Link *link = userdata;
591         int r;
592
593         assert(rtnl);
594         assert(m);
595         assert(link);
596         assert(link->manager);
597
598         for (; m; m = sd_rtnl_message_next(m)) {
599                 r = sd_rtnl_message_get_errno(m);
600                 if (r < 0) {
601                         log_debug_link(link, "getting address failed: %s",
602                                        strerror(-r));
603                         continue;
604                 }
605
606                 r = link_rtnl_process_address(rtnl, m, link->manager);
607                 if (r < 0)
608                         log_warning_link(link, "could not process address: %s",
609                                          strerror(-r));
610         }
611
612         return 1;
613 }
614
615 static int address_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
616         _cleanup_link_unref_ Link *link = userdata;
617         int r;
618
619         assert(rtnl);
620         assert(m);
621         assert(link);
622         assert(link->ifname);
623         assert(link->link_messages > 0);
624         assert(IN_SET(link->state, LINK_STATE_SETTING_ADDRESSES,
625                LINK_STATE_FAILED, LINK_STATE_LINGER));
626
627         link->link_messages --;
628
629         if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
630                 return 1;
631
632         r = sd_rtnl_message_get_errno(m);
633         if (r < 0 && r != -EEXIST)
634                 log_struct_link(LOG_WARNING, link,
635                                 "MESSAGE=%-*s: could not set address: %s",
636                                 IFNAMSIZ,
637                                 link->ifname, strerror(-r),
638                                 "ERRNO=%d", -r,
639                                 NULL);
640         else if (r >= 0) {
641                 /* calling handler directly so take a ref */
642                 link_ref(link);
643                 link_get_address_handler(rtnl, m, link);
644         }
645
646         if (link->link_messages == 0) {
647                 log_debug_link(link, "addresses set");
648                 link_enter_set_routes(link);
649         }
650
651         return 1;
652 }
653
654 static int link_enter_set_addresses(Link *link) {
655         Address *ad;
656         int r;
657
658         assert(link);
659         assert(link->network);
660         assert(link->state != _LINK_STATE_INVALID);
661
662         link->state = LINK_STATE_SETTING_ADDRESSES;
663
664         LIST_FOREACH(addresses, ad, link->network->static_addresses) {
665                 r = address_configure(ad, link, &address_handler);
666                 if (r < 0) {
667                         log_warning_link(link,
668                                          "could not set addresses: %s",
669                                          strerror(-r));
670                         link_enter_failed(link);
671                         return r;
672                 }
673
674                 link->link_messages ++;
675         }
676
677         if (link->link_messages == 0) {
678                 link_enter_set_routes(link);
679         } else
680                 log_debug_link(link, "setting addresses");
681
682         return 0;
683 }
684
685 int link_address_drop_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
686         _cleanup_link_unref_ Link *link = userdata;
687         int r;
688
689         assert(m);
690         assert(link);
691         assert(link->ifname);
692
693         if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
694                 return 1;
695
696         r = sd_rtnl_message_get_errno(m);
697         if (r < 0 && r != -EADDRNOTAVAIL)
698                 log_struct_link(LOG_WARNING, link,
699                                 "MESSAGE=%-*s: could not drop address: %s",
700                                 IFNAMSIZ,
701                                 link->ifname, strerror(-r),
702                                 "ERRNO=%d", -r,
703                                 NULL);
704
705         return 1;
706 }
707
708 static int set_hostname_handler(sd_bus *bus, sd_bus_message *m, void *userdata,
709                                 sd_bus_error *ret_error) {
710         _cleanup_link_unref_ Link *link = userdata;
711         int r;
712
713         assert(link);
714
715         if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
716                 return 1;
717
718         r = sd_bus_message_get_errno(m);
719         if (r > 0)
720                 log_warning_link(link, "Could not set hostname: %s",
721                                  strerror(r));
722
723         return 1;
724 }
725
726 int link_set_hostname(Link *link, const char *hostname) {
727         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
728         int r = 0;
729
730         assert(link);
731         assert(link->manager);
732         assert(hostname);
733
734         log_debug_link(link, "Setting transient hostname: '%s'", hostname);
735
736         if (!link->manager->bus) {
737                 /* TODO: replace by assert when we can rely on kdbus */
738                 log_info_link(link,
739                               "Not connected to system bus, ignoring transient hostname.");
740                 return 0;
741         }
742
743         r = sd_bus_message_new_method_call(
744                         link->manager->bus,
745                         &m,
746                         "org.freedesktop.hostname1",
747                         "/org/freedesktop/hostname1",
748                         "org.freedesktop.hostname1",
749                         "SetHostname");
750         if (r < 0)
751                 return r;
752
753         r = sd_bus_message_append(m, "sb", hostname, false);
754         if (r < 0)
755                 return r;
756
757         r = sd_bus_call_async(link->manager->bus, NULL, m, set_hostname_handler,
758                               link, 0);
759         if (r < 0) {
760                 log_error_link(link, "Could not set transient hostname: %s",
761                                strerror(-r));
762                 return r;
763         }
764
765         link_ref(link);
766
767         return 0;
768 }
769
770 static int set_mtu_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
771         _cleanup_link_unref_ Link *link = userdata;
772         int r;
773
774         assert(m);
775         assert(link);
776         assert(link->ifname);
777
778         if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
779                 return 1;
780
781         r = sd_rtnl_message_get_errno(m);
782         if (r < 0)
783                 log_struct_link(LOG_WARNING, link,
784                                 "MESSAGE=%-*s: could not set MTU: %s",
785                                 IFNAMSIZ, link->ifname, strerror(-r),
786                                 "ERRNO=%d", -r,
787                                 NULL);
788
789         return 1;
790 }
791
792 int link_set_mtu(Link *link, uint32_t mtu) {
793         _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
794         int r;
795
796         assert(link);
797         assert(link->manager);
798         assert(link->manager->rtnl);
799
800         log_debug_link(link, "setting MTU: %" PRIu32, mtu);
801
802         r = sd_rtnl_message_new_link(link->manager->rtnl, &req,
803                                      RTM_SETLINK, link->ifindex);
804         if (r < 0) {
805                 log_error_link(link, "Could not allocate RTM_SETLINK message");
806                 return r;
807         }
808
809         r = sd_rtnl_message_append_u32(req, IFLA_MTU, mtu);
810         if (r < 0) {
811                 log_error_link(link, "Could not append MTU: %s", strerror(-r));
812                 return r;
813         }
814
815         r = sd_rtnl_call_async(link->manager->rtnl, req, set_mtu_handler, link,
816                                0, NULL);
817         if (r < 0) {
818                 log_error_link(link,
819                                "Could not send rtnetlink message: %s",
820                                strerror(-r));
821                 return r;
822         }
823
824         link_ref(link);
825
826         return 0;
827 }
828
829 static void dhcp6_handler(sd_dhcp6_client *client, int event, void *userdata) {
830         Link *link = userdata;
831
832         assert(link);
833         assert(link->network);
834         assert(link->manager);
835
836         if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
837                 return;
838
839         switch(event) {
840         case DHCP6_EVENT_STOP:
841         case DHCP6_EVENT_RESEND_EXPIRE:
842         case DHCP6_EVENT_RETRANS_MAX:
843         case DHCP6_EVENT_IP_ACQUIRE:
844                 log_debug_link(link, "DHCPv6 event %d", event);
845
846                 break;
847
848         default:
849                 if (event < 0)
850                         log_warning_link(link, "DHCPv6 error: %s",
851                                          strerror(-event));
852                 else
853                         log_warning_link(link, "DHCPv6 unknown event: %d",
854                                          event);
855                 return;
856         }
857 }
858
859 static void icmp6_router_handler(sd_icmp6_nd *nd, int event, void *userdata) {
860         Link *link = userdata;
861         int r;
862
863         assert(link);
864         assert(link->network);
865         assert(link->manager);
866
867         if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
868                 return;
869
870         switch(event) {
871         case ICMP6_EVENT_ROUTER_ADVERTISMENT_NONE:
872         case ICMP6_EVENT_ROUTER_ADVERTISMENT_OTHER:
873                 return;
874
875         case ICMP6_EVENT_ROUTER_ADVERTISMENT_TIMEOUT:
876         case ICMP6_EVENT_ROUTER_ADVERTISMENT_MANAGED:
877                 break;
878
879         default:
880                 if (event < 0)
881                         log_warning_link(link, "ICMPv6 error: %s",
882                                          strerror(-event));
883                 else
884                         log_warning_link(link, "ICMPv6 unknown event: %d",
885                                          event);
886
887                 return;
888         }
889
890         if (link->dhcp6_client)
891                 return;
892
893         r = sd_dhcp6_client_new(&link->dhcp6_client);
894         if (r < 0)
895                 return;
896
897         r = sd_dhcp6_client_attach_event(link->dhcp6_client, NULL, 0);
898         if (r < 0) {
899                 link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client);
900                 return;
901         }
902
903         r = sd_dhcp6_client_set_mac(link->dhcp6_client,
904                                     (const uint8_t *) &link->mac,
905                                     sizeof (link->mac), ARPHRD_ETHER);
906         if (r < 0) {
907                 link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client);
908                 return;
909         }
910
911         r = sd_dhcp6_client_set_index(link->dhcp6_client, link->ifindex);
912         if (r < 0) {
913                 link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client);
914                 return;
915         }
916
917         r = sd_dhcp6_client_set_callback(link->dhcp6_client, dhcp6_handler,
918                                          link);
919         if (r < 0) {
920                 link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client);
921                 return;
922         }
923
924         r = sd_dhcp6_client_start(link->dhcp6_client);
925         if (r < 0)
926                 link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client);
927 }
928
929 static int link_acquire_conf(Link *link) {
930         int r;
931
932         assert(link);
933         assert(link->network);
934         assert(link->manager);
935         assert(link->manager->event);
936
937         if (link_ipv4ll_enabled(link)) {
938                 assert(link->ipv4ll);
939
940                 log_debug_link(link, "acquiring IPv4 link-local address");
941
942                 r = sd_ipv4ll_start(link->ipv4ll);
943                 if (r < 0) {
944                         log_warning_link(link, "could not acquire IPv4 "
945                                          "link-local address");
946                         return r;
947                 }
948         }
949
950         if (link_dhcp4_enabled(link)) {
951                 assert(link->dhcp_client);
952
953                 log_debug_link(link, "acquiring DHCPv4 lease");
954
955                 r = sd_dhcp_client_start(link->dhcp_client);
956                 if (r < 0) {
957                         log_warning_link(link, "could not acquire DHCPv4 "
958                                          "lease");
959                         return r;
960                 }
961         }
962
963         if (link_dhcp6_enabled(link)) {
964                 assert(link->icmp6_router_discovery);
965
966                 log_debug_link(link, "discovering IPv6 routers");
967
968                 r = sd_icmp6_router_solicitation_start(link->icmp6_router_discovery);
969                 if (r < 0) {
970                         log_warning_link(link,
971                                          "could not start IPv6 router discovery");
972                         return r;
973                 }
974         }
975
976         return 0;
977 }
978
979 bool link_has_carrier(Link *link) {
980         /* see Documentation/networking/operstates.txt in the kernel sources */
981
982         if (link->kernel_operstate == IF_OPER_UP)
983                 return true;
984
985         if (link->kernel_operstate == IF_OPER_UNKNOWN)
986                 /* operstate may not be implemented, so fall back to flags */
987                 if ((link->flags & IFF_LOWER_UP) && !(link->flags & IFF_DORMANT))
988                         return true;
989
990         return false;
991 }
992
993 static int link_up_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
994         _cleanup_link_unref_ Link *link = userdata;
995         int r;
996
997         assert(link);
998
999         if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
1000                 return 1;
1001
1002         r = sd_rtnl_message_get_errno(m);
1003         if (r < 0) {
1004                 /* we warn but don't fail the link, as it may
1005                    be brought up later */
1006                 log_struct_link(LOG_WARNING, link,
1007                                 "MESSAGE=%-*s: could not bring up interface: %s",
1008                                 IFNAMSIZ,
1009                                 link->ifname, strerror(-r),
1010                                 "ERRNO=%d", -r,
1011                                 NULL);
1012         }
1013
1014         return 1;
1015 }
1016
1017 static int link_up(Link *link) {
1018         _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
1019         int r;
1020
1021         assert(link);
1022         assert(link->manager);
1023         assert(link->manager->rtnl);
1024
1025         log_debug_link(link, "bringing link up");
1026
1027         r = sd_rtnl_message_new_link(link->manager->rtnl, &req,
1028                                      RTM_SETLINK, link->ifindex);
1029         if (r < 0) {
1030                 log_error_link(link, "Could not allocate RTM_SETLINK message");
1031                 return r;
1032         }
1033
1034         r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP);
1035         if (r < 0) {
1036                 log_error_link(link, "Could not set link flags: %s",
1037                                strerror(-r));
1038                 return r;
1039         }
1040
1041         r = sd_rtnl_call_async(link->manager->rtnl, req, link_up_handler, link,
1042                                0, NULL);
1043         if (r < 0) {
1044                 log_error_link(link,
1045                                "Could not send rtnetlink message: %s",
1046                                strerror(-r));
1047                 return r;
1048         }
1049
1050         link_ref(link);
1051
1052         return 0;
1053 }
1054
1055 static int link_joined(Link *link) {
1056         int r;
1057
1058         assert(link);
1059         assert(link->network);
1060
1061         if (!(link->flags & IFF_UP)) {
1062                 r = link_up(link);
1063                 if (r < 0) {
1064                         link_enter_failed(link);
1065                         return r;
1066                 }
1067         }
1068
1069         return link_enter_set_addresses(link);
1070 }
1071
1072 static int netdev_join_handler(sd_rtnl *rtnl, sd_rtnl_message *m,
1073                                void *userdata) {
1074         _cleanup_link_unref_ Link *link = userdata;
1075         int r;
1076
1077         assert(link);
1078         assert(link->network);
1079
1080         link->enslaving --;
1081
1082         if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
1083                 return 1;
1084
1085         r = sd_rtnl_message_get_errno(m);
1086         if (r < 0 && r != -EEXIST) {
1087                 log_struct_link(LOG_ERR, link,
1088                                 "MESSAGE=%-*s: could not join netdev: %s",
1089                                 IFNAMSIZ,
1090                                 link->ifname, strerror(-r),
1091                                 "ERRNO=%d", -r,
1092                                 NULL);
1093                 link_enter_failed(link);
1094                 return 1;
1095         } else
1096                 log_debug_link(link, "joined netdev");
1097
1098         if (link->enslaving <= 0)
1099                 link_joined(link);
1100
1101         return 1;
1102 }
1103
1104 static int link_enter_join_netdev(Link *link) {
1105         NetDev *netdev;
1106         Iterator i;
1107         int r;
1108
1109         assert(link);
1110         assert(link->network);
1111         assert(link->state == LINK_STATE_PENDING);
1112
1113         link->state = LINK_STATE_ENSLAVING;
1114
1115         link_save(link);
1116
1117         if (!link->network->bridge &&
1118             !link->network->bond &&
1119             hashmap_isempty(link->network->stacked_netdevs))
1120                 return link_joined(link);
1121
1122         if (link->network->bond) {
1123                 log_struct_link(LOG_DEBUG, link,
1124                                 "MESSAGE=%-*s: enslaving by '%s'",
1125                                 IFNAMSIZ,
1126                                 link->ifname, link->network->bond->ifname,
1127                                 NETDEVIF(link->network->bond),
1128                                 NULL);
1129
1130                 r = netdev_join(link->network->bond, link, &netdev_join_handler);
1131                 if (r < 0) {
1132                         log_struct_link(LOG_WARNING, link,
1133                                         "MESSAGE=%-*s: could not join netdev '%s': %s",
1134                                         IFNAMSIZ,
1135                                         link->ifname, link->network->bond->ifname,
1136                                         strerror(-r),
1137                                         NETDEVIF(link->network->bond),
1138                                         NULL);
1139                         link_enter_failed(link);
1140                         return r;
1141                 }
1142
1143                 link->enslaving ++;
1144         }
1145
1146         if (link->network->bridge) {
1147                 log_struct_link(LOG_DEBUG, link,
1148                                 "MESSAGE=%-*s: enslaving by '%s'",
1149                                 IFNAMSIZ,
1150                                 link->ifname, link->network->bridge->ifname,
1151                                 NETDEVIF(link->network->bridge),
1152                                 NULL);
1153
1154                 r = netdev_join(link->network->bridge, link,
1155                                 &netdev_join_handler);
1156                 if (r < 0) {
1157                         log_struct_link(LOG_WARNING, link,
1158                                         "MESSAGE=%-*s: could not join netdev '%s': %s",
1159                                         IFNAMSIZ,
1160                                         link->ifname, link->network->bridge->ifname,
1161                                         strerror(-r),
1162                                         NETDEVIF(link->network->bridge),
1163                                         NULL);
1164                         link_enter_failed(link);
1165                         return r;
1166                 }
1167
1168                 link->enslaving ++;
1169         }
1170
1171         HASHMAP_FOREACH(netdev, link->network->stacked_netdevs, i) {
1172                 log_struct_link(LOG_DEBUG, link,
1173                                 "MESSAGE=%-*s: enslaving by '%s'",
1174                                 IFNAMSIZ,
1175                                 link->ifname, netdev->ifname, NETDEVIF(netdev),
1176                                 NULL);
1177
1178                 r = netdev_join(netdev, link, &netdev_join_handler);
1179                 if (r < 0) {
1180                         log_struct_link(LOG_WARNING, link,
1181                                         "MESSAGE=%-*s: could not join netdev '%s': %s",
1182                                         IFNAMSIZ,
1183                                         link->ifname, netdev->ifname,
1184                                         strerror(-r),
1185                                         NETDEVIF(netdev), NULL);
1186                         link_enter_failed(link);
1187                         return r;
1188                 }
1189
1190                 link->enslaving ++;
1191         }
1192
1193         return 0;
1194 }
1195
1196 static int link_configure(Link *link) {
1197         int r;
1198
1199         assert(link);
1200         assert(link->network);
1201         assert(link->state == LINK_STATE_PENDING);
1202
1203         if (link_ipv4ll_enabled(link)) {
1204                 r = ipv4ll_configure(link);
1205                 if (r < 0)
1206                         return r;
1207         }
1208
1209         if (link_dhcp4_enabled(link)) {
1210                 r = dhcp4_configure(link);
1211                 if (r < 0)
1212                         return r;
1213         }
1214
1215         if (link_dhcp4_server_enabled(link)) {
1216                 r = sd_dhcp_server_new(&link->dhcp_server, link->ifindex);
1217                 if (r < 0)
1218                         return r;
1219
1220                 r = sd_dhcp_server_attach_event(link->dhcp_server, NULL, 0);
1221                 if (r < 0)
1222                         return r;
1223         }
1224
1225         if (link_dhcp6_enabled(link)) {
1226                 r = sd_icmp6_nd_new(&link->icmp6_router_discovery);
1227                 if (r < 0)
1228                         return r;
1229
1230                 r = sd_icmp6_nd_attach_event(link->icmp6_router_discovery,
1231                                              NULL, 0);
1232                 if (r < 0)
1233                         return r;
1234
1235                 r = sd_icmp6_nd_set_mac(link->icmp6_router_discovery,
1236                                         &link->mac);
1237                 if (r < 0)
1238                         return r;
1239
1240                 r = sd_icmp6_nd_set_index(link->icmp6_router_discovery,
1241                                           link->ifindex);
1242                 if (r < 0)
1243                         return r;
1244
1245                 r = sd_icmp6_nd_set_callback(link->icmp6_router_discovery,
1246                                              icmp6_router_handler, link);
1247                 if (r < 0)
1248                         return r;
1249         }
1250
1251         if (link_has_carrier(link)) {
1252                 r = link_acquire_conf(link);
1253                 if (r < 0)
1254                         return r;
1255         }
1256
1257         return link_enter_join_netdev(link);
1258 }
1259
1260 static int link_initialized_and_synced(sd_rtnl *rtnl, sd_rtnl_message *m,
1261                                        void *userdata) {
1262         _cleanup_link_unref_ Link *link = userdata;
1263         Network *network;
1264         int r;
1265
1266         assert(link);
1267         assert(link->ifname);
1268         assert(link->manager);
1269
1270         if (link->state != LINK_STATE_PENDING)
1271                 return 1;
1272
1273         log_debug_link(link, "link state is up-to-date");
1274
1275         r = network_get(link->manager, link->udev_device, link->ifname,
1276                         &link->mac, &network);
1277         if (r == -ENOENT) {
1278                 link_enter_unmanaged(link);
1279                 return 1;
1280         } else if (r < 0)
1281                 return r;
1282
1283         if (link->flags & IFF_LOOPBACK) {
1284                 if (network->ipv4ll)
1285                         log_debug_link(link, "ignoring IPv4LL for loopback link");
1286
1287                 if (network->dhcp != DHCP_SUPPORT_NONE)
1288                         log_debug_link(link, "ignoring DHCP clients for loopback link");
1289
1290                 if (network->dhcp_server)
1291                         log_debug_link(link, "ignoring DHCP server for loopback link");
1292         }
1293
1294         r = network_apply(link->manager, network, link);
1295         if (r < 0)
1296                 return r;
1297
1298         r = link_configure(link);
1299         if (r < 0)
1300                 return r;
1301
1302         return 1;
1303 }
1304
1305 int link_initialized(Link *link, struct udev_device *device) {
1306         _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
1307         int r;
1308
1309         assert(link);
1310         assert(link->manager);
1311         assert(link->manager->rtnl);
1312         assert(device);
1313
1314         if (link->state != LINK_STATE_PENDING)
1315                 return 0;
1316
1317         if (link->udev_device)
1318                 return 0;
1319
1320         log_debug_link(link, "udev initialized link");
1321
1322         link->udev_device = udev_device_ref(device);
1323
1324         /* udev has initialized the link, but we don't know if we have yet
1325          * processed the NEWLINK messages with the latest state. Do a GETLINK,
1326          * when it returns we know that the pending NEWLINKs have already been
1327          * processed and that we are up-to-date */
1328
1329         r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK,
1330                                      link->ifindex);
1331         if (r < 0)
1332                 return r;
1333
1334         r = sd_rtnl_call_async(link->manager->rtnl, req,
1335                                link_initialized_and_synced, link, 0, NULL);
1336         if (r < 0)
1337                 return r;
1338
1339         link_ref(link);
1340
1341         return 0;
1342 }
1343
1344 int link_rtnl_process_address(sd_rtnl *rtnl, sd_rtnl_message *message,
1345                               void *userdata) {
1346         Manager *m = userdata;
1347         Link *link = NULL;
1348         uint16_t type;
1349         _cleanup_address_free_ Address *address = NULL;
1350         Address *ad;
1351         char buf[INET6_ADDRSTRLEN];
1352         char valid_buf[FORMAT_TIMESPAN_MAX];
1353         const char *valid_str = NULL;
1354         bool address_dropped = false;
1355         int r, ifindex;
1356
1357         assert(rtnl);
1358         assert(message);
1359         assert(m);
1360
1361         r = sd_rtnl_message_get_type(message, &type);
1362         if (r < 0) {
1363                 log_warning("rtnl: could not get message type");
1364                 return 0;
1365         }
1366
1367         r = sd_rtnl_message_addr_get_ifindex(message, &ifindex);
1368         if (r < 0 || ifindex <= 0) {
1369                 log_warning("rtnl: received address message without valid ifindex, ignoring");
1370                 return 0;
1371         } else {
1372                 r = link_get(m, ifindex, &link);
1373                 if (r < 0 || !link) {
1374                         log_warning("rtnl: received address for a nonexistent link (%d), ignoring", ifindex);
1375                         return 0;
1376                 }
1377         }
1378
1379         r = address_new_dynamic(&address);
1380         if (r < 0)
1381                 return r;
1382
1383         r = sd_rtnl_message_addr_get_family(message, &address->family);
1384         if (r < 0 || !IN_SET(address->family, AF_INET, AF_INET6)) {
1385                 log_warning_link(link,
1386                                  "rtnl: received address with invalid family, ignoring");
1387                 return 0;
1388         }
1389
1390         r = sd_rtnl_message_addr_get_prefixlen(message, &address->prefixlen);
1391         if (r < 0) {
1392                 log_warning_link(link,
1393                                  "rtnl: received address with invalid prefixlen, ignoring");
1394                 return 0;
1395         }
1396
1397         r = sd_rtnl_message_addr_get_scope(message, &address->scope);
1398         if (r < 0) {
1399                 log_warning_link(link,
1400                                  "rtnl: received address with invalid scope, ignoring");
1401                 return 0;
1402         }
1403
1404         r = sd_rtnl_message_addr_get_flags(message, &address->flags);
1405         if (r < 0) {
1406                 log_warning_link(link,
1407                                  "rtnl: received address with invalid flags, ignoring");
1408                 return 0;
1409         }
1410
1411         switch (address->family) {
1412         case AF_INET:
1413                 r = sd_rtnl_message_read_in_addr(message, IFA_LOCAL,
1414                                                  &address->in_addr.in);
1415                 if (r < 0) {
1416                         log_warning_link(link,
1417                                          "rtnl: received address without valid address, ignoring");
1418                         return 0;
1419                 }
1420
1421                 break;
1422
1423         case AF_INET6:
1424                 r = sd_rtnl_message_read_in6_addr(message, IFA_ADDRESS,
1425                                                   &address->in_addr.in6);
1426                 if (r < 0) {
1427                         log_warning_link(link,
1428                                          "rtnl: received address without valid address, ignoring");
1429                         return 0;
1430                 }
1431
1432                 break;
1433
1434         default:
1435                 assert_not_reached("invalid address family");
1436         }
1437
1438         if (!inet_ntop(address->family, &address->in_addr, buf,
1439                        INET6_ADDRSTRLEN)) {
1440                 log_warning_link(link, "could not print address");
1441                 return 0;
1442         }
1443
1444         r = sd_rtnl_message_read_cache_info(message, IFA_CACHEINFO,
1445                                             &address->cinfo);
1446         if (r >= 0) {
1447                 if (address->cinfo.ifa_valid == CACHE_INFO_INFINITY_LIFE_TIME)
1448                         valid_str = "ever";
1449                 else
1450                         valid_str = format_timespan(valid_buf, FORMAT_TIMESPAN_MAX,
1451                                                     address->cinfo.ifa_valid * USEC_PER_SEC,
1452                                                     USEC_PER_SEC);
1453         }
1454
1455         LIST_FOREACH(addresses, ad, link->addresses) {
1456                 if (address_equal(ad, address)) {
1457                         LIST_REMOVE(addresses, link->addresses, ad);
1458
1459                         address_free(ad);
1460
1461                         address_dropped = true;
1462
1463                         break;
1464                 }
1465         }
1466
1467         switch (type) {
1468         case RTM_NEWADDR:
1469                 if (!address_dropped)
1470                         log_debug_link(link, "added address: %s/%u (valid for %s)",
1471                                        buf, address->prefixlen, valid_str);
1472                 else
1473                         log_debug_link(link, "updated address: %s/%u (valid for %s)",
1474                                        buf, address->prefixlen, valid_str);
1475
1476                 LIST_PREPEND(addresses, link->addresses, address);
1477                 address = NULL;
1478
1479                 link_save(link);
1480
1481                 break;
1482         case RTM_DELADDR:
1483                 if (address_dropped) {
1484                         log_debug_link(link, "removed address: %s/%u (valid for %s)",
1485                                        buf, address->prefixlen, valid_str);
1486
1487                         link_save(link);
1488                 } else
1489                         log_warning_link(link,
1490                                          "removing non-existent address: %s/%u (valid for %s)",
1491                                          buf, address->prefixlen, valid_str);
1492
1493                 break;
1494         default:
1495                 assert_not_reached("Received invalid RTNL message type");
1496         }
1497
1498         return 1;
1499 }
1500
1501 int link_add(Manager *m, sd_rtnl_message *message, Link **ret) {
1502         Link *link;
1503         _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
1504         _cleanup_udev_device_unref_ struct udev_device *device = NULL;
1505         char ifindex_str[2 + DECIMAL_STR_MAX(int)];
1506         int r;
1507
1508         assert(m);
1509         assert(m->rtnl);
1510         assert(message);
1511         assert(ret);
1512
1513         r = link_new(m, message, ret);
1514         if (r < 0)
1515                 return r;
1516
1517         link = *ret;
1518
1519         log_debug_link(link, "link %d added", link->ifindex);
1520
1521         r = sd_rtnl_message_new_addr(m->rtnl, &req, RTM_GETADDR, link->ifindex,
1522                                      0);
1523         if (r < 0)
1524                 return r;
1525
1526         r = sd_rtnl_call_async(m->rtnl, req, link_get_address_handler, link, 0,
1527                                NULL);
1528         if (r < 0)
1529                 return r;
1530
1531         link_ref(link);
1532
1533         if (detect_container(NULL) <= 0) {
1534                 /* not in a container, udev will be around */
1535                 sprintf(ifindex_str, "n%d", link->ifindex);
1536                 device = udev_device_new_from_device_id(m->udev, ifindex_str);
1537                 if (!device) {
1538                         log_warning_link(link,
1539                                          "could not find udev device: %m");
1540                         return -errno;
1541                 }
1542
1543                 if (udev_device_get_is_initialized(device) <= 0) {
1544                         /* not yet ready */
1545                         log_debug_link(link, "link pending udev initialization...");
1546                         return 0;
1547                 }
1548
1549                 r = link_initialized(link, device);
1550                 if (r < 0)
1551                         return r;
1552         } else {
1553                 /* we are calling a callback directly, so must take a ref */
1554                 link_ref(link);
1555
1556                 r = link_initialized_and_synced(m->rtnl, NULL, link);
1557                 if (r < 0)
1558                         return r;
1559         }
1560
1561         return 0;
1562 }
1563
1564 int link_update(Link *link, sd_rtnl_message *m) {
1565         struct ether_addr mac;
1566         const char *ifname;
1567         uint32_t mtu;
1568         bool had_carrier, carrier_gained, carrier_lost;
1569         int r;
1570
1571         assert(link);
1572         assert(link->ifname);
1573         assert(m);
1574
1575         if (link->state == LINK_STATE_LINGER) {
1576                 link_ref(link);
1577                 log_info_link(link, "link readded");
1578                 link->state = LINK_STATE_ENSLAVING;
1579         }
1580
1581         r = sd_rtnl_message_read_string(m, IFLA_IFNAME, &ifname);
1582         if (r >= 0 && !streq(ifname, link->ifname)) {
1583                 log_info_link(link, "renamed to %s", ifname);
1584
1585                 free(link->ifname);
1586                 link->ifname = strdup(ifname);
1587                 if (!link->ifname)
1588                         return -ENOMEM;
1589         }
1590
1591         r = sd_rtnl_message_read_u32(m, IFLA_MTU, &mtu);
1592         if (r >= 0 && mtu > 0) {
1593                 link->mtu = mtu;
1594                 if (!link->original_mtu) {
1595                         link->original_mtu = mtu;
1596                         log_debug_link(link, "saved original MTU: %"
1597                                        PRIu32, link->original_mtu);
1598                 }
1599
1600                 if (link->dhcp_client) {
1601                         r = sd_dhcp_client_set_mtu(link->dhcp_client,
1602                                                    link->mtu);
1603                         if (r < 0) {
1604                                 log_warning_link(link,
1605                                                  "Could not update MTU in DHCP client: %s",
1606                                                  strerror(-r));
1607                                 return r;
1608                         }
1609                 }
1610         }
1611
1612         /* The kernel may broadcast NEWLINK messages without the MAC address
1613            set, simply ignore them. */
1614         r = sd_rtnl_message_read_ether_addr(m, IFLA_ADDRESS, &mac);
1615         if (r >= 0) {
1616                 if (memcmp(link->mac.ether_addr_octet, mac.ether_addr_octet,
1617                            ETH_ALEN)) {
1618
1619                         memcpy(link->mac.ether_addr_octet, mac.ether_addr_octet,
1620                                ETH_ALEN);
1621
1622                         log_debug_link(link, "MAC address: "
1623                                        "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
1624                                        mac.ether_addr_octet[0],
1625                                        mac.ether_addr_octet[1],
1626                                        mac.ether_addr_octet[2],
1627                                        mac.ether_addr_octet[3],
1628                                        mac.ether_addr_octet[4],
1629                                        mac.ether_addr_octet[5]);
1630
1631                         if (link->ipv4ll) {
1632                                 r = sd_ipv4ll_set_mac(link->ipv4ll, &link->mac);
1633                                 if (r < 0) {
1634                                         log_warning_link(link,
1635                                                          "Could not update MAC address in IPv4LL client: %s",
1636                                                          strerror(-r));
1637                                         return r;
1638                                 }
1639                         }
1640
1641                         if (link->dhcp_client) {
1642                                 r = sd_dhcp_client_set_mac(link->dhcp_client,
1643                                                            (const uint8_t *) &link->mac,
1644                                                            sizeof (link->mac),
1645                                                            ARPHRD_ETHER);
1646                                 if (r < 0) {
1647                                         log_warning_link(link,
1648                                                          "Could not update MAC address in DHCP client: %s",
1649                                                          strerror(-r));
1650                                         return r;
1651                                 }
1652                         }
1653
1654                         if (link->dhcp6_client) {
1655                                 r = sd_dhcp6_client_set_mac(link->dhcp6_client,
1656                                                             (const uint8_t *) &link->mac,
1657                                                             sizeof (link->mac),
1658                                                             ARPHRD_ETHER);
1659                                 if (r < 0) {
1660                                         log_warning_link(link,
1661                                                          "Could not update MAC address in DHCPv6 client: %s",
1662                                                          strerror(-r));
1663                                         return r;
1664                                 }
1665                         }
1666                 }
1667         }
1668
1669         had_carrier = link_has_carrier(link);
1670
1671         r = link_update_flags(link, m);
1672         if (r < 0)
1673                 return r;
1674
1675         carrier_gained = !had_carrier && link_has_carrier(link);
1676         carrier_lost = had_carrier && !link_has_carrier(link);
1677
1678         if (carrier_gained) {
1679                 log_info_link(link, "gained carrier");
1680
1681                 if (link->network) {
1682                         r = link_acquire_conf(link);
1683                         if (r < 0) {
1684                                 link_enter_failed(link);
1685                                 return r;
1686                         }
1687                 }
1688         } else if (carrier_lost) {
1689                 log_info_link(link, "lost carrier");
1690
1691                 r = link_stop_clients(link);
1692                 if (r < 0) {
1693                         link_enter_failed(link);
1694                         return r;
1695                 }
1696         }
1697
1698         return 0;
1699 }
1700
1701 static void link_update_operstate(Link *link) {
1702
1703         assert(link);
1704
1705         if (link->kernel_operstate == IF_OPER_DORMANT)
1706                 link->operstate = LINK_OPERSTATE_DORMANT;
1707         else if (link_has_carrier(link)) {
1708                 Address *address;
1709                 uint8_t scope = RT_SCOPE_NOWHERE;
1710
1711                 /* if we have carrier, check what addresses we have */
1712                 LIST_FOREACH(addresses, address, link->addresses) {
1713                         if (address->flags & (IFA_F_TENTATIVE | IFA_F_DEPRECATED))
1714                                 continue;
1715
1716                         if (address->scope < scope)
1717                                 scope = address->scope;
1718                 }
1719
1720                 if (scope < RT_SCOPE_SITE)
1721                         /* universally accessible addresses found */
1722                         link->operstate = LINK_OPERSTATE_ROUTABLE;
1723                 else if (scope < RT_SCOPE_HOST)
1724                         /* only link or site local addresses found */
1725                         link->operstate = LINK_OPERSTATE_DEGRADED;
1726                 else
1727                         /* no useful addresses found */
1728                         link->operstate = LINK_OPERSTATE_CARRIER;
1729         } else if (link->flags & IFF_UP)
1730                 link->operstate = LINK_OPERSTATE_NO_CARRIER;
1731         else
1732                 link->operstate = LINK_OPERSTATE_OFF;
1733 }
1734
1735 int link_save(Link *link) {
1736         _cleanup_free_ char *temp_path = NULL;
1737         _cleanup_fclose_ FILE *f = NULL;
1738         const char *admin_state, *oper_state;
1739         int r;
1740
1741         assert(link);
1742         assert(link->state_file);
1743         assert(link->lease_file);
1744         assert(link->manager);
1745
1746         link_update_operstate(link);
1747
1748         r = manager_save(link->manager);
1749         if (r < 0)
1750                 return r;
1751
1752         if (link->state == LINK_STATE_LINGER) {
1753                 unlink(link->state_file);
1754                 return 0;
1755         }
1756
1757         admin_state = link_state_to_string(link->state);
1758         assert(admin_state);
1759
1760         oper_state = link_operstate_to_string(link->operstate);
1761         assert(oper_state);
1762
1763         r = fopen_temporary(link->state_file, &f, &temp_path);
1764         if (r < 0)
1765                 return r;
1766
1767         fchmod(fileno(f), 0644);
1768
1769         fprintf(f,
1770                 "# This is private data. Do not parse.\n"
1771                 "ADMIN_STATE=%s\n"
1772                 "OPER_STATE=%s\n",
1773                 admin_state, oper_state);
1774
1775         if (link->network) {
1776                 char **address, **domain;
1777                 bool space;
1778
1779                 fprintf(f, "NETWORK_FILE=%s\n", link->network->filename);
1780
1781                 fputs("DNS=", f);
1782                 space = false;
1783                 STRV_FOREACH(address, link->network->dns) {
1784                         if (space)
1785                                 fputc(' ', f);
1786                         fputs(*address, f);
1787                         space = true;
1788                 }
1789
1790                 if (link->network->dhcp_dns &&
1791                     link->dhcp_lease) {
1792                         const struct in_addr *addresses;
1793
1794                         r = sd_dhcp_lease_get_dns(link->dhcp_lease, &addresses);
1795                         if (r > 0) {
1796                                 if (space)
1797                                         fputc(' ', f);
1798                                 serialize_in_addrs(f, addresses, r);
1799                         }
1800                 }
1801
1802                 fputs("\n", f);
1803
1804                 fprintf(f, "NTP=");
1805                 space = false;
1806                 STRV_FOREACH(address, link->network->ntp) {
1807                         if (space)
1808                                 fputc(' ', f);
1809                         fputs(*address, f);
1810                         space = true;
1811                 }
1812
1813                 if (link->network->dhcp_ntp &&
1814                     link->dhcp_lease) {
1815                         const struct in_addr *addresses;
1816
1817                         r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &addresses);
1818                         if (r > 0) {
1819                                 if (space)
1820                                         fputc(' ', f);
1821                                 serialize_in_addrs(f, addresses, r);
1822                         }
1823                 }
1824
1825                 fputs("\n", f);
1826
1827                 fprintf(f, "DOMAINS=");
1828                 space = false;
1829                 STRV_FOREACH(domain, link->network->domains) {
1830                         if (space)
1831                                 fputc(' ', f);
1832                         fputs(*domain, f);
1833                         space = true;
1834                 }
1835
1836                 if (link->network->dhcp_domains &&
1837                     link->dhcp_lease) {
1838                         const char *domainname;
1839
1840                         r = sd_dhcp_lease_get_domainname(link->dhcp_lease, &domainname);
1841                         if (r >= 0) {
1842                                 if (space)
1843                                         fputc(' ', f);
1844                                 fputs(domainname, f);
1845                         }
1846                 }
1847
1848                 fputs("\n", f);
1849
1850                 fprintf(f, "WILDCARD_DOMAIN=%s\n",
1851                         yes_no(link->network->wildcard_domain));
1852
1853                 fprintf(f, "LLMNR=%s\n",
1854                         llmnr_support_to_string(link->network->llmnr));
1855         }
1856
1857         if (link->dhcp_lease) {
1858                 assert(link->network);
1859
1860                 r = dhcp_lease_save(link->dhcp_lease, link->lease_file);
1861                 if (r < 0)
1862                         goto fail;
1863
1864                 fprintf(f,
1865                         "DHCP_LEASE=%s\n",
1866                         link->lease_file);
1867         } else
1868                 unlink(link->lease_file);
1869
1870         r = fflush_and_check(f);
1871         if (r < 0)
1872                 goto fail;
1873
1874         if (rename(temp_path, link->state_file) < 0) {
1875                 r = -errno;
1876                 goto fail;
1877         }
1878
1879         return 0;
1880 fail:
1881         log_error_link(link, "Failed to save link data to %s: %s", link->state_file, strerror(-r));
1882         unlink(link->state_file);
1883         unlink(temp_path);
1884         return r;
1885 }
1886
1887 static const char* const link_state_table[_LINK_STATE_MAX] = {
1888         [LINK_STATE_PENDING] = "pending",
1889         [LINK_STATE_ENSLAVING] = "configuring",
1890         [LINK_STATE_SETTING_ADDRESSES] = "configuring",
1891         [LINK_STATE_SETTING_ROUTES] = "configuring",
1892         [LINK_STATE_CONFIGURED] = "configured",
1893         [LINK_STATE_UNMANAGED] = "unmanaged",
1894         [LINK_STATE_FAILED] = "failed",
1895         [LINK_STATE_LINGER] = "linger",
1896 };
1897
1898 DEFINE_STRING_TABLE_LOOKUP(link_state, LinkState);
1899
1900 static const char* const link_operstate_table[_LINK_OPERSTATE_MAX] = {
1901         [LINK_OPERSTATE_OFF] = "off",
1902         [LINK_OPERSTATE_NO_CARRIER] = "no-carrier",
1903         [LINK_OPERSTATE_DORMANT] = "dormant",
1904         [LINK_OPERSTATE_CARRIER] = "carrier",
1905         [LINK_OPERSTATE_DEGRADED] = "degraded",
1906         [LINK_OPERSTATE_ROUTABLE] = "routable",
1907 };
1908
1909 DEFINE_STRING_TABLE_LOOKUP(link_operstate, LinkOperationalState);