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