chiark / gitweb /
4c75fa3d38acef0e9641a63b827d69b5babc9d11
[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
25 #include "networkd.h"
26 #include "libudev-private.h"
27 #include "udev-util.h"
28 #include "util.h"
29 #include "virt.h"
30 #include "bus-util.h"
31 #include "network-internal.h"
32 #include "conf-parser.h"
33
34 #include "network-util.h"
35 #include "dhcp-lease-internal.h"
36
37 static int ipv4ll_address_update(Link *link, bool deprecate);
38 static bool ipv4ll_is_bound(sd_ipv4ll *ll);
39
40 static int link_new(Manager *manager, sd_rtnl_message *message, Link **ret) {
41         _cleanup_link_unref_ Link *link = NULL;
42         uint16_t type;
43         char *ifname;
44         int r, ifindex;
45
46         assert(manager);
47         assert(manager->links);
48         assert(message);
49         assert(ret);
50
51         r = sd_rtnl_message_get_type(message, &type);
52         if (r < 0)
53                 return r;
54         else if (type != RTM_NEWLINK)
55                 return -EINVAL;
56
57         r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
58         if (r < 0)
59                 return r;
60         else if (ifindex <= 0)
61                 return -EINVAL;
62
63         r = sd_rtnl_message_read_string(message, IFLA_IFNAME, &ifname);
64         if (r < 0)
65                 return r;
66
67         link = new0(Link, 1);
68         if (!link)
69                 return -ENOMEM;
70
71         link->n_ref = 1;
72         link->manager = manager;
73         link->state = LINK_STATE_INITIALIZING;
74         link->ifindex = ifindex;
75         link->ifname = strdup(ifname);
76         if (!link->ifname)
77                 return -ENOMEM;
78
79         r = sd_rtnl_message_read_ether_addr(message, IFLA_ADDRESS, &link->mac);
80         if (r < 0)
81                 return r;
82
83         r = asprintf(&link->state_file, "/run/systemd/netif/links/%"PRIu64,
84                      link->ifindex);
85         if (r < 0)
86                 return -ENOMEM;
87
88         r = asprintf(&link->lease_file, "/run/systemd/netif/leases/%"PRIu64,
89                      link->ifindex);
90         if (r < 0)
91                 return -ENOMEM;
92
93         r = hashmap_put(manager->links, &link->ifindex, link);
94         if (r < 0)
95                 return r;
96
97         *ret = link;
98         link = NULL;
99
100         return 0;
101 }
102
103 static void link_free(Link *link) {
104         Address *address;
105
106         if (!link)
107                 return;
108
109         assert(link->manager);
110
111         while ((address = link->addresses)) {
112                 LIST_REMOVE(addresses, link->addresses, address);
113                 address_free(address);
114         }
115
116         while ((address = link->pool_addresses)) {
117                 LIST_REMOVE(addresses, link->pool_addresses, address);
118                 address_free(address);
119         }
120
121         sd_dhcp_client_unref(link->dhcp_client);
122         sd_dhcp_lease_unref(link->dhcp_lease);
123
124         unlink(link->lease_file);
125         free(link->lease_file);
126
127         sd_ipv4ll_unref(link->ipv4ll);
128         sd_dhcp6_client_unref(link->dhcp6_client);
129         sd_icmp6_nd_unref(link->icmp6_router_discovery);
130
131         hashmap_remove(link->manager->links, &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         uint64_t ifindex_64;
160
161         assert(m);
162         assert(m->links);
163         assert(ifindex);
164         assert(ret);
165
166         ifindex_64 = ifindex;
167         link = hashmap_get(m->links, &ifindex_64);
168         if (!link)
169                 return -ENODEV;
170
171         *ret = link;
172
173         return 0;
174 }
175
176 void link_drop(Link *link) {
177         if (!link || link->state == LINK_STATE_LINGER)
178                 return;
179
180         link->state = LINK_STATE_LINGER;
181
182         log_debug_link(link, "link removed");
183
184         link_unref(link);
185
186         return;
187 }
188
189 static void link_enter_unmanaged(Link *link) {
190         assert(link);
191
192         log_debug_link(link, "unmanaged");
193
194         link->state = LINK_STATE_UNMANAGED;
195
196         link_save(link);
197 }
198
199 static int link_stop_clients(Link *link) {
200         int r = 0, k;
201
202         assert(link);
203         assert(link->manager);
204         assert(link->manager->event);
205
206         if (!link->network)
207                 return 0;
208
209         if (IN_SET(link->network->dhcp, DHCP_SUPPORT_BOTH, DHCP_SUPPORT_V6)) {
210                 assert(link->dhcp_client);
211
212                 k = sd_dhcp_client_stop(link->dhcp_client);
213                 if (k < 0) {
214                         log_warning_link(link, "Could not stop DHCPv4 client: %s", strerror(-r));
215                         r = k;
216                 }
217         }
218
219         if (link->network->ipv4ll) {
220                 assert(link->ipv4ll);
221
222                 k = sd_ipv4ll_stop(link->ipv4ll);
223                 if (k < 0) {
224                         log_warning_link(link, "Could not stop IPv4 link-local: %s", strerror(-r));
225                         r = k;
226                 }
227         }
228
229         if (link->network->dhcp_server) {
230                 assert(link->dhcp_server);
231
232                 k = sd_dhcp_server_stop(link->dhcp_server);
233                 if (k < 0) {
234                         log_warning_link(link, "Could not stop DHCPv4 server: %s", strerror(-r));
235                         r = k;
236                 }
237         }
238
239         if (IN_SET(link->network->dhcp, DHCP_SUPPORT_BOTH, DHCP_SUPPORT_V6)) {
240                 assert(link->icmp6_router_discovery);
241
242                 if (link->dhcp6_client) {
243                         k = sd_dhcp6_client_stop(link->dhcp6_client);
244                         if (k < 0) {
245                                 log_warning_link(link, "Could not stop DHCPv6 client: %s", strerror(-r));
246                                 r = k;
247                         }
248                 }
249
250                 k = sd_icmp6_nd_stop(link->icmp6_router_discovery);
251                 if (k < 0) {
252                         log_warning_link(link, "Could not stop ICMPv6 router discovery: %s", strerror(-r));
253                         r = k;
254                 }
255         }
256
257         return r;
258 }
259
260 static void link_enter_failed(Link *link) {
261         assert(link);
262
263         if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
264                 return;
265
266         log_warning_link(link, "failed");
267
268         link->state = LINK_STATE_FAILED;
269
270         link_stop_clients(link);
271
272         link_save(link);
273 }
274
275 static Address* link_find_dhcp_server_address(Link *link) {
276         Address *address;
277
278         assert(link);
279         assert(link->network);
280
281         /* The the first statically configured address if there is any */
282         LIST_FOREACH(addresses, address, link->network->static_addresses) {
283
284                 if (address->family != AF_INET)
285                         continue;
286
287                 if (in_addr_null(address->family, &address->in_addr))
288                         continue;
289
290                 return address;
291         }
292
293         /* If that didn't work, find a suitable address we got from the pool */
294         LIST_FOREACH(addresses, address, link->pool_addresses) {
295                 if (address->family != AF_INET)
296                         continue;
297
298                 return address;
299         }
300
301         return NULL;
302 }
303
304 static int link_enter_configured(Link *link) {
305         int r;
306
307         assert(link);
308         assert(link->network);
309         assert(link->state == LINK_STATE_SETTING_ROUTES);
310
311         if (link->network->dhcp_server &&
312             !sd_dhcp_server_is_running(link->dhcp_server)) {
313                 struct in_addr pool_start;
314                 Address *address;
315
316                 address = link_find_dhcp_server_address(link);
317                 if (!address) {
318                         log_warning_link(link, "Failed to find suitable address for DHCPv4 server instance.");
319                         link_enter_failed(link);
320                         return 0;
321                 }
322
323                 log_debug_link(link, "offering DHCPv4 leases");
324
325                 r = sd_dhcp_server_set_address(link->dhcp_server, &address->in_addr.in);
326                 if (r < 0)
327                         return r;
328
329                 /* offer 32 addresses starting from the address following the server address */
330                 pool_start.s_addr = htobe32(be32toh(address->in_addr.in.s_addr) + 1);
331                 r = sd_dhcp_server_set_lease_pool(link->dhcp_server,
332                                                   &pool_start, 32);
333                 if (r < 0)
334                         return r;
335
336                 /* TODO:
337                 r = sd_dhcp_server_set_router(link->dhcp_server,
338                                               &main_address->in_addr.in);
339                 if (r < 0)
340                         return r;
341
342                 r = sd_dhcp_server_set_prefixlen(link->dhcp_server,
343                                                  main_address->prefixlen);
344                 if (r < 0)
345                         return r;
346                 */
347
348                 r = sd_dhcp_server_start(link->dhcp_server);
349                 if (r < 0) {
350                         log_warning_link(link, "could not start DHCPv4 server "
351                                          "instance: %s", strerror(-r));
352
353                         link_enter_failed(link);
354
355                         return 0;
356                 }
357         }
358
359         log_info_link(link, "link configured");
360
361         link->state = LINK_STATE_CONFIGURED;
362
363         link_save(link);
364
365         return 0;
366 }
367
368 static int route_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
369         Link *link = userdata;
370         int r;
371
372         assert(link->route_messages > 0);
373         assert(IN_SET(link->state, LINK_STATE_SETTING_ADDRESSES,
374                       LINK_STATE_SETTING_ROUTES, LINK_STATE_FAILED,
375                       LINK_STATE_LINGER));
376
377         link->route_messages --;
378
379         if (IN_SET(LINK_STATE_FAILED, LINK_STATE_LINGER)) {
380                 link_unref(link);
381                 return 1;
382         }
383
384         r = sd_rtnl_message_get_errno(m);
385         if (r < 0 && r != -EEXIST)
386                 log_struct_link(LOG_WARNING, link,
387                                 "MESSAGE=%-*s: could not set route: %s",
388                                 IFNAMSIZ,
389                                 link->ifname, strerror(-r),
390                                 "ERRNO=%d", -r,
391                                 NULL);
392
393         /* we might have received an old reply after moving back to SETTING_ADDRESSES,
394          * ignore it */
395         if (link->route_messages == 0 && link->state == LINK_STATE_SETTING_ROUTES) {
396                 log_debug_link(link, "routes set");
397                 link_enter_configured(link);
398         }
399
400         link_unref(link);
401
402         return 1;
403 }
404
405 static int link_set_dhcp_routes(Link *link) {
406         struct sd_dhcp_route *static_routes;
407         size_t static_routes_size;
408         int r;
409         unsigned i;
410
411         assert(link);
412
413         r = sd_dhcp_lease_get_routes(link->dhcp_lease, &static_routes, &static_routes_size);
414         if (r < 0) {
415                 if (r != -ENOENT)
416                         log_warning_link(link, "DHCP error: %s", strerror(-r));
417                 return r;
418         }
419
420         for (i = 0; i < static_routes_size; i++) {
421                 _cleanup_route_free_ Route *route = NULL;
422
423                 r = route_new_dynamic(&route);
424                 if (r < 0) {
425                         log_error_link(link, "Could not allocate route: %s",
426                                        strerror(-r));
427                         return r;
428                 }
429
430                 route->family = AF_INET;
431                 route->in_addr.in = static_routes[i].gw_addr;
432                 route->dst_addr.in = static_routes[i].dst_addr;
433                 route->dst_prefixlen = static_routes[i].dst_prefixlen;
434                 route->metrics = DHCP_STATIC_ROUTE_METRIC;
435
436                 r = route_configure(route, link, &route_handler);
437                 if (r < 0) {
438                         log_warning_link(link,
439                                          "could not set host route: %s", strerror(-r));
440                         return r;
441                 }
442
443                 link_ref(link);
444                 link->route_messages ++;
445         }
446
447         return 0;
448 }
449
450 static int link_enter_set_routes(Link *link) {
451         Route *rt;
452         int r;
453
454         assert(link);
455         assert(link->network);
456         assert(link->state == LINK_STATE_SETTING_ADDRESSES);
457
458         link->state = LINK_STATE_SETTING_ROUTES;
459
460         if (!link->network->static_routes && !link->dhcp_lease &&
461             (!link->ipv4ll || ipv4ll_is_bound(link->ipv4ll) == false))
462                 return link_enter_configured(link);
463
464         log_debug_link(link, "setting routes");
465
466         LIST_FOREACH(routes, rt, link->network->static_routes) {
467                 r = route_configure(rt, link, &route_handler);
468                 if (r < 0) {
469                         log_warning_link(link,
470                                          "could not set routes: %s", strerror(-r));
471                         link_enter_failed(link);
472                         return r;
473                 }
474
475                 link_ref(link);
476                 link->route_messages ++;
477         }
478
479         if (link->ipv4ll && !link->dhcp_lease) {
480                 _cleanup_route_free_ Route *route = NULL;
481                 struct in_addr addr;
482
483                 r = sd_ipv4ll_get_address(link->ipv4ll, &addr);
484                 if (r < 0 && r != -ENOENT) {
485                         log_warning_link(link, "IPV4LL error: no address: %s",
486                                         strerror(-r));
487                         return r;
488                 }
489
490                 if (r != -ENOENT) {
491                         r = route_new_dynamic(&route);
492                         if (r < 0) {
493                                 log_error_link(link, "Could not allocate route: %s",
494                                                strerror(-r));
495                                 return r;
496                         }
497
498                         route->family = AF_INET;
499                         route->scope = RT_SCOPE_LINK;
500                         route->metrics = 99;
501
502                         r = route_configure(route, link, &route_handler);
503                         if (r < 0) {
504                                 log_warning_link(link,
505                                                  "could not set routes: %s", strerror(-r));
506                                 link_enter_failed(link);
507                                 return r;
508                         }
509
510                         link_ref(link);
511                         link->route_messages ++;
512                 }
513         }
514
515         if (link->dhcp_lease) {
516                 _cleanup_route_free_ Route *route = NULL;
517                 _cleanup_route_free_ Route *route_gw = NULL;
518                 struct in_addr gateway;
519
520                 r = sd_dhcp_lease_get_router(link->dhcp_lease, &gateway);
521                 if (r < 0 && r != -ENOENT) {
522                         log_warning_link(link, "DHCP error: %s", strerror(-r));
523                         return r;
524                 }
525
526                 if (r >= 0) {
527                         r = route_new_dynamic(&route);
528                         if (r < 0) {
529                                 log_error_link(link, "Could not allocate route: %s",
530                                                strerror(-r));
531                                 return r;
532                         }
533
534                         r = route_new_dynamic(&route_gw);
535                         if (r < 0) {
536                                 log_error_link(link, "Could not allocate route: %s",
537                                                strerror(-r));
538                                 return r;
539                         }
540
541                         /* The dhcp netmask may mask out the gateway. Add an explicit
542                          * route for the gw host so that we can route no matter the
543                          * netmask or existing kernel route tables. */
544                         route_gw->family = AF_INET;
545                         route_gw->dst_addr.in = gateway;
546                         route_gw->dst_prefixlen = 32;
547                         route_gw->scope = RT_SCOPE_LINK;
548
549                         r = route_configure(route_gw, link, &route_handler);
550                         if (r < 0) {
551                                 log_warning_link(link,
552                                                  "could not set host route: %s", strerror(-r));
553                                 return r;
554                         }
555
556                         link_ref(link);
557                         link->route_messages ++;
558
559                         route->family = AF_INET;
560                         route->in_addr.in = gateway;
561
562                         r = route_configure(route, link, &route_handler);
563                         if (r < 0) {
564                                 log_warning_link(link,
565                                                  "could not set routes: %s", strerror(-r));
566                                 link_enter_failed(link);
567                                 return r;
568                         }
569
570                         link_ref(link);
571                         link->route_messages ++;
572                 }
573
574                 if (link->network->dhcp_routes)
575                         link_set_dhcp_routes(link);
576         }
577
578         if (link->route_messages == 0) {
579                 link_enter_configured(link);
580         }
581
582         return 0;
583 }
584
585 static int route_drop_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
586         Link *link = userdata;
587         int r;
588
589         assert(m);
590         assert(link);
591         assert(link->ifname);
592
593         if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) {
594                 link_unref(link);
595                 return 1;
596         }
597
598         r = sd_rtnl_message_get_errno(m);
599         if (r < 0 && r != -ESRCH)
600                 log_struct_link(LOG_WARNING, link,
601                                 "MESSAGE=%-*s: could not drop route: %s",
602                                 IFNAMSIZ,
603                                 link->ifname, strerror(-r),
604                                 "ERRNO=%d", -r,
605                                 NULL);
606
607         link_unref(link);
608
609         return 0;
610 }
611
612 static int address_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
613         Link *link = userdata;
614         int r;
615
616         assert(m);
617         assert(link);
618         assert(link->ifname);
619         assert(link->addr_messages > 0);
620         assert(IN_SET(link->state, LINK_STATE_SETTING_ADDRESSES,
621                LINK_STATE_FAILED, LINK_STATE_LINGER));
622
623         link->addr_messages --;
624
625         if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) {
626                 link_unref(link);
627                 return 1;
628         }
629
630         r = sd_rtnl_message_get_errno(m);
631         if (r < 0 && r != -EEXIST)
632                 log_struct_link(LOG_WARNING, link,
633                                 "MESSAGE=%-*s: could not set address: %s",
634                                 IFNAMSIZ,
635                                 link->ifname, strerror(-r),
636                                 "ERRNO=%d", -r,
637                                 NULL);
638
639         if (link->addr_messages == 0) {
640                 log_debug_link(link, "addresses set");
641                 link_enter_set_routes(link);
642         }
643
644         link_unref(link);
645
646         return 1;
647 }
648
649 static int link_enter_set_addresses(Link *link) {
650         Address *ad;
651         int r;
652         uint32_t lifetime = CACHE_INFO_INFINITY_LIFE_TIME;
653
654         assert(link);
655         assert(link->network);
656         assert(link->state != _LINK_STATE_INVALID);
657
658         link->state = LINK_STATE_SETTING_ADDRESSES;
659
660         if (!link->network->static_addresses && !link->dhcp_lease &&
661                 (!link->ipv4ll || ipv4ll_is_bound(link->ipv4ll) == false))
662                 return link_enter_set_routes(link);
663
664         log_debug_link(link, "setting addresses");
665
666         LIST_FOREACH(addresses, ad, link->network->static_addresses) {
667                 r = address_configure(ad, link, &address_handler);
668                 if (r < 0) {
669                         log_warning_link(link,
670                                          "could not set addresses: %s", strerror(-r));
671                         link_enter_failed(link);
672                         return r;
673                 }
674
675                 link_ref(link);
676                 link->addr_messages ++;
677         }
678
679         if (link->ipv4ll && !link->dhcp_lease) {
680                 _cleanup_address_free_ Address *ll_addr = NULL;
681                 struct in_addr addr;
682
683                 r = sd_ipv4ll_get_address(link->ipv4ll, &addr);
684                 if (r < 0 && r != -ENOENT) {
685                         log_warning_link(link, "IPV4LL error: no address: %s",
686                                         strerror(-r));
687                         return r;
688                 }
689
690                 if (r != -ENOENT) {
691                         r = address_new_dynamic(&ll_addr);
692                         if (r < 0) {
693                                 log_error_link(link, "Could not allocate address: %s", strerror(-r));
694                                 return r;
695                         }
696
697                         ll_addr->family = AF_INET;
698                         ll_addr->in_addr.in = addr;
699                         ll_addr->prefixlen = 16;
700                         ll_addr->broadcast.s_addr = ll_addr->in_addr.in.s_addr | htonl(0xfffffffflu >> ll_addr->prefixlen);
701                         ll_addr->scope = RT_SCOPE_LINK;
702
703                         r = address_configure(ll_addr, link, &address_handler);
704                         if (r < 0) {
705                                 log_warning_link(link,
706                                          "could not set addresses: %s", strerror(-r));
707                                 link_enter_failed(link);
708                                 return r;
709                         }
710
711                         link_ref(link);
712                         link->addr_messages ++;
713                 }
714         }
715
716         if (link->dhcp_lease) {
717                 _cleanup_address_free_ Address *address = NULL;
718                 struct in_addr addr;
719                 struct in_addr netmask;
720                 unsigned prefixlen;
721
722                 r = sd_dhcp_lease_get_address(link->dhcp_lease, &addr);
723                 if (r < 0) {
724                         log_warning_link(link, "DHCP error: no address: %s",
725                                          strerror(-r));
726                         return r;
727                 }
728
729                 if (!link->network->dhcp_critical) {
730                         r = sd_dhcp_lease_get_lifetime(link->dhcp_lease,
731                                                        &lifetime);
732                         if (r < 0) {
733                                 log_warning_link(link, "DHCP error: no lifetime: %s",
734                                                  strerror(-r));
735                                 return r;
736                         }
737                 }
738
739                 r = sd_dhcp_lease_get_netmask(link->dhcp_lease, &netmask);
740                 if (r < 0) {
741                         log_warning_link(link, "DHCP error: no netmask: %s",
742                                          strerror(-r));
743                         return r;
744                 }
745
746                 prefixlen = net_netmask_to_prefixlen(&netmask);
747
748                 r = address_new_dynamic(&address);
749                 if (r < 0) {
750                         log_error_link(link, "Could not allocate address: %s",
751                                        strerror(-r));
752                         return r;
753                 }
754
755                 address->family = AF_INET;
756                 address->in_addr.in = addr;
757                 address->cinfo.ifa_prefered = lifetime;
758                 address->cinfo.ifa_valid = lifetime;
759                 address->prefixlen = prefixlen;
760                 address->broadcast.s_addr = addr.s_addr | ~netmask.s_addr;
761
762                 r = address_configure(address, link, &address_handler);
763                 if (r < 0) {
764                         log_warning_link(link,
765                                          "could not set addresses: %s", strerror(-r));
766                         link_enter_failed(link);
767                         return r;
768                 }
769
770                 link_ref(link);
771                 link->addr_messages ++;
772         }
773
774         return 0;
775 }
776
777 static int address_update_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
778         Link *link = userdata;
779         int r;
780
781         assert(m);
782         assert(link);
783         assert(link->ifname);
784
785         if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) {
786                 link_unref(link);
787                 return 1;
788         }
789
790         r = sd_rtnl_message_get_errno(m);
791         if (r < 0 && r != -ENOENT)
792                 log_struct_link(LOG_WARNING, link,
793                                 "MESSAGE=%-*s: could not update address: %s",
794                                 IFNAMSIZ,
795                                 link->ifname, strerror(-r),
796                                 "ERRNO=%d", -r,
797                                 NULL);
798
799         link_unref(link);
800
801         return 0;
802 }
803
804 static int address_drop_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
805         Link *link = userdata;
806         int r;
807
808         assert(m);
809         assert(link);
810         assert(link->ifname);
811
812         if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) {
813                 link_unref(link);
814                 return 1;
815         }
816
817         r = sd_rtnl_message_get_errno(m);
818         if (r < 0 && r != -EADDRNOTAVAIL)
819                 log_struct_link(LOG_WARNING, link,
820                                 "MESSAGE=%-*s: could not drop address: %s",
821                                 IFNAMSIZ,
822                                 link->ifname, strerror(-r),
823                                 "ERRNO=%d", -r,
824                                 NULL);
825
826         link_unref(link);
827
828         return 0;
829 }
830
831 static int set_hostname_handler(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
832         Link *link = userdata;
833         int r;
834
835         assert(link);
836
837         if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) {
838                 link_unref(link);
839                 return 1;
840         }
841
842         r = sd_bus_message_get_errno(m);
843         if (r < 0)
844                 log_warning_link(link, "Could not set hostname: %s", strerror(-r));
845
846         link_unref(link);
847
848         return 1;
849 }
850
851 static int link_set_hostname(Link *link, const char *hostname) {
852         _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
853         int r = 0;
854
855         assert(link);
856         assert(link->manager);
857         assert(hostname);
858
859         log_debug_link(link, "Setting transient hostname: '%s'", hostname);
860
861         if (!link->manager->bus) { /* TODO: replace by assert when we can rely on kdbus */
862                 log_info_link(link, "Not connected to system bus, ignoring transient hostname.");
863                 return 0;
864         }
865
866         r = sd_bus_message_new_method_call(
867                         link->manager->bus,
868                         &m,
869                         "org.freedesktop.hostname1",
870                         "/org/freedesktop/hostname1",
871                         "org.freedesktop.hostname1",
872                         "SetHostname");
873         if (r < 0)
874                 return r;
875
876         r = sd_bus_message_append(m, "sb", hostname, false);
877         if (r < 0)
878                 return r;
879
880         r = sd_bus_call_async(link->manager->bus, NULL, m, set_hostname_handler, link, 0);
881         if (r < 0)
882                 log_error_link(link, "Could not set transient hostname: %s", strerror(-r));
883
884         link_ref(link);
885
886         return r;
887 }
888
889 static int set_mtu_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
890         Link *link = userdata;
891         int r;
892
893         assert(m);
894         assert(link);
895         assert(link->ifname);
896
897         if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) {
898                 link_unref(link);
899                 return 1;
900         }
901
902         r = sd_rtnl_message_get_errno(m);
903         if (r < 0)
904                 log_struct_link(LOG_WARNING, link,
905                                 "MESSAGE=%-*s: could not set MTU: %s",
906                                 IFNAMSIZ, link->ifname, strerror(-r),
907                                 "ERRNO=%d", -r,
908                                 NULL);
909
910         link_unref(link);
911
912         return 1;
913 }
914
915 static int link_set_mtu(Link *link, uint32_t mtu) {
916         _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
917         int r;
918
919         assert(link);
920         assert(link->manager);
921         assert(link->manager->rtnl);
922
923         log_debug_link(link, "setting MTU: %" PRIu32, mtu);
924
925         r = sd_rtnl_message_new_link(link->manager->rtnl, &req,
926                                      RTM_SETLINK, link->ifindex);
927         if (r < 0) {
928                 log_error_link(link, "Could not allocate RTM_SETLINK message");
929                 return r;
930         }
931
932         r = sd_rtnl_message_append_u32(req, IFLA_MTU, mtu);
933         if (r < 0) {
934                 log_error_link(link, "Could not append MTU: %s", strerror(-r));
935                 return r;
936         }
937
938         r = sd_rtnl_call_async(link->manager->rtnl, req, set_mtu_handler, link, 0, NULL);
939         if (r < 0) {
940                 log_error_link(link,
941                                "Could not send rtnetlink message: %s", strerror(-r));
942                 return r;
943         }
944
945         link_ref(link);
946
947         return 0;
948 }
949
950 static int dhcp_lease_lost(Link *link) {
951         _cleanup_address_free_ Address *address = NULL;
952         struct in_addr addr;
953         struct in_addr netmask;
954         struct in_addr gateway;
955         unsigned prefixlen;
956         unsigned i;
957         int r;
958
959         assert(link);
960         assert(link->dhcp_lease);
961
962         log_warning_link(link, "DHCP lease lost");
963
964         if (link->network->dhcp_routes) {
965                 struct sd_dhcp_route *routes;
966                 size_t routes_size;
967
968                 r = sd_dhcp_lease_get_routes(link->dhcp_lease, &routes, &routes_size);
969                 if (r >= 0) {
970                         for (i = 0; i < routes_size; i++) {
971                                 _cleanup_route_free_ Route *route = NULL;
972
973                                 r = route_new_dynamic(&route);
974                                 if (r >= 0) {
975                                         route->family = AF_INET;
976                                         route->in_addr.in = routes[i].gw_addr;
977                                         route->dst_addr.in = routes[i].dst_addr;
978                                         route->dst_prefixlen = routes[i].dst_prefixlen;
979
980                                         route_drop(route, link, &route_drop_handler);
981                                         link_ref(link);
982                                 }
983                         }
984                 }
985         }
986
987         r = address_new_dynamic(&address);
988         if (r >= 0) {
989                 r = sd_dhcp_lease_get_router(link->dhcp_lease, &gateway);
990                 if (r >= 0) {
991                         _cleanup_route_free_ Route *route_gw = NULL;
992                         _cleanup_route_free_ Route *route = NULL;
993
994                         r = route_new_dynamic(&route_gw);
995                         if (r >= 0) {
996                                 route_gw->family = AF_INET;
997                                 route_gw->dst_addr.in = gateway;
998                                 route_gw->dst_prefixlen = 32;
999                                 route_gw->scope = RT_SCOPE_LINK;
1000
1001                                 route_drop(route_gw, link, &route_drop_handler);
1002                                 link_ref(link);
1003                         }
1004
1005                         r = route_new_dynamic(&route);
1006                         if (r >= 0) {
1007                                 route->family = AF_INET;
1008                                 route->in_addr.in = gateway;
1009
1010                                 route_drop(route, link, &route_drop_handler);
1011                                 link_ref(link);
1012                         }
1013                 }
1014
1015                 sd_dhcp_lease_get_address(link->dhcp_lease, &addr);
1016                 sd_dhcp_lease_get_netmask(link->dhcp_lease, &netmask);
1017                 prefixlen = net_netmask_to_prefixlen(&netmask);
1018
1019                 address->family = AF_INET;
1020                 address->in_addr.in = addr;
1021                 address->prefixlen = prefixlen;
1022
1023                 address_drop(address, link, &address_drop_handler);
1024                 link_ref(link);
1025         }
1026
1027         if (link->network->dhcp_mtu) {
1028                 uint16_t mtu;
1029
1030                 r = sd_dhcp_lease_get_mtu(link->dhcp_lease, &mtu);
1031                 if (r >= 0 && link->original_mtu != mtu) {
1032                         r = link_set_mtu(link, link->original_mtu);
1033                         if (r < 0) {
1034                                 log_warning_link(link, "DHCP error: could not reset MTU");
1035                                 link_enter_failed(link);
1036                                 return r;
1037                         }
1038                 }
1039         }
1040
1041         if (link->network->dhcp_hostname) {
1042                 const char *hostname = NULL;
1043
1044                 r = sd_dhcp_lease_get_hostname(link->dhcp_lease, &hostname);
1045                 if (r >= 0 && hostname) {
1046                         r = link_set_hostname(link, "");
1047                         if (r < 0)
1048                                 log_error_link(link, "Failed to reset transient hostname");
1049                 }
1050         }
1051
1052         link->dhcp_lease = sd_dhcp_lease_unref(link->dhcp_lease);
1053
1054         return 0;
1055 }
1056
1057 static int dhcp_lease_renew(sd_dhcp_client *client, Link *link) {
1058         sd_dhcp_lease *lease;
1059         int r;
1060
1061         r = sd_dhcp_client_get_lease(client, &lease);
1062         if (r < 0) {
1063                 log_warning_link(link, "DHCP error: no lease %s",
1064                                  strerror(-r));
1065                 return r;
1066         }
1067
1068         sd_dhcp_lease_unref(link->dhcp_lease);
1069         link->dhcp_lease = lease;
1070
1071         link_enter_set_addresses(link);
1072
1073         return 0;
1074 }
1075
1076 static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) {
1077         sd_dhcp_lease *lease;
1078         struct in_addr address;
1079         struct in_addr netmask;
1080         struct in_addr gateway;
1081         unsigned prefixlen;
1082         int r;
1083
1084         assert(client);
1085         assert(link);
1086
1087         r = sd_dhcp_client_get_lease(client, &lease);
1088         if (r < 0) {
1089                 log_warning_link(link, "DHCP error: no lease: %s",
1090                                  strerror(-r));
1091                 return r;
1092         }
1093
1094         r = sd_dhcp_lease_get_address(lease, &address);
1095         if (r < 0) {
1096                 log_warning_link(link, "DHCP error: no address: %s",
1097                                  strerror(-r));
1098                 return r;
1099         }
1100
1101         r = sd_dhcp_lease_get_netmask(lease, &netmask);
1102         if (r < 0) {
1103                 log_warning_link(link, "DHCP error: no netmask: %s",
1104                                  strerror(-r));
1105                 return r;
1106         }
1107
1108         prefixlen = net_netmask_to_prefixlen(&netmask);
1109
1110         r = sd_dhcp_lease_get_router(lease, &gateway);
1111         if (r < 0 && r != -ENOENT) {
1112                 log_warning_link(link, "DHCP error: %s", strerror(-r));
1113                 return r;
1114         }
1115
1116         if (r >= 0)
1117                 log_struct_link(LOG_INFO, link,
1118                                 "MESSAGE=%-*s: DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u",
1119                                  IFNAMSIZ,
1120                                  link->ifname,
1121                                  ADDRESS_FMT_VAL(address),
1122                                  prefixlen,
1123                                  ADDRESS_FMT_VAL(gateway),
1124                                  "ADDRESS=%u.%u.%u.%u",
1125                                  ADDRESS_FMT_VAL(address),
1126                                  "PREFIXLEN=%u",
1127                                  prefixlen,
1128                                  "GATEWAY=%u.%u.%u.%u",
1129                                  ADDRESS_FMT_VAL(gateway),
1130                                  NULL);
1131         else
1132                 log_struct_link(LOG_INFO, link,
1133                                 "MESSAGE=%-*s: DHCPv4 address %u.%u.%u.%u/%u",
1134                                  IFNAMSIZ,
1135                                  link->ifname,
1136                                  ADDRESS_FMT_VAL(address),
1137                                  prefixlen,
1138                                  "ADDRESS=%u.%u.%u.%u",
1139                                  ADDRESS_FMT_VAL(address),
1140                                  "PREFIXLEN=%u",
1141                                  prefixlen,
1142                                  NULL);
1143
1144         link->dhcp_lease = lease;
1145
1146         if (link->network->dhcp_mtu) {
1147                 uint16_t mtu;
1148
1149                 r = sd_dhcp_lease_get_mtu(lease, &mtu);
1150                 if (r >= 0) {
1151                         r = link_set_mtu(link, mtu);
1152                         if (r < 0)
1153                                 log_error_link(link, "Failed to set MTU "
1154                                                "to %" PRIu16, mtu);
1155                 }
1156         }
1157
1158         if (link->network->dhcp_hostname) {
1159                 const char *hostname;
1160
1161                 r = sd_dhcp_lease_get_hostname(lease, &hostname);
1162                 if (r >= 0) {
1163                         r = link_set_hostname(link, hostname);
1164                         if (r < 0)
1165                                 log_error_link(link, "Failed to set transient hostname "
1166                                           "to '%s'", hostname);
1167                 }
1168         }
1169
1170         link_enter_set_addresses(link);
1171
1172         return 0;
1173 }
1174
1175 static void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) {
1176         Link *link = userdata;
1177         int r = 0;
1178
1179         assert(link);
1180         assert(link->network);
1181         assert(link->manager);
1182
1183         if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
1184                 return;
1185
1186         switch (event) {
1187                 case DHCP_EVENT_NO_LEASE:
1188                         log_debug_link(link, "IP address in use.");
1189                         break;
1190                 case DHCP_EVENT_EXPIRED:
1191                 case DHCP_EVENT_STOP:
1192                 case DHCP_EVENT_IP_CHANGE:
1193                         if (link->network->dhcp_critical) {
1194                                 log_error_link(link, "DHCPv4 connection considered system critical, "
1195                                                "ignoring request to reconfigure it.");
1196                                 return;
1197                         }
1198
1199                         if (link->dhcp_lease) {
1200                                 r = dhcp_lease_lost(link);
1201                                 if (r < 0) {
1202                                         link_enter_failed(link);
1203                                         return;
1204                                 }
1205                         }
1206
1207                         if (event == DHCP_EVENT_IP_CHANGE) {
1208                                 r = dhcp_lease_acquired(client, link);
1209                                 if (r < 0) {
1210                                         link_enter_failed(link);
1211                                         return;
1212                                 }
1213                         }
1214
1215                         if (event == DHCP_EVENT_EXPIRED && link->network->ipv4ll) {
1216                                 if (!sd_ipv4ll_is_running(link->ipv4ll))
1217                                         r = sd_ipv4ll_start(link->ipv4ll);
1218                                 else if (ipv4ll_is_bound(link->ipv4ll))
1219                                         r = ipv4ll_address_update(link, false);
1220                                 if (r < 0) {
1221                                         link_enter_failed(link);
1222                                         return;
1223                                 }
1224                         }
1225
1226                         break;
1227                 case DHCP_EVENT_RENEW:
1228                         r = dhcp_lease_renew(client, link);
1229                         if (r < 0) {
1230                                 link_enter_failed(link);
1231                                 return;
1232                         }
1233                         break;
1234                 case DHCP_EVENT_IP_ACQUIRE:
1235                         r = dhcp_lease_acquired(client, link);
1236                         if (r < 0) {
1237                                 link_enter_failed(link);
1238                                 return;
1239                         }
1240                         if (link->ipv4ll) {
1241                                 if (ipv4ll_is_bound(link->ipv4ll))
1242                                         r = ipv4ll_address_update(link, true);
1243                                 else
1244                                         r = sd_ipv4ll_stop(link->ipv4ll);
1245                                 if (r < 0) {
1246                                         link_enter_failed(link);
1247                                         return;
1248                                 }
1249                         }
1250                         break;
1251                 default:
1252                         if (event < 0)
1253                                 log_warning_link(link, "DHCP error: %s", strerror(-event));
1254                         else
1255                                 log_warning_link(link, "DHCP unknown event: %d", event);
1256                         break;
1257         }
1258
1259         return;
1260 }
1261
1262 static int ipv4ll_address_update(Link *link, bool deprecate) {
1263         int r;
1264         struct in_addr addr;
1265
1266         assert(link);
1267
1268         r = sd_ipv4ll_get_address(link->ipv4ll, &addr);
1269         if (r >= 0) {
1270                 _cleanup_address_free_ Address *address = NULL;
1271
1272                 log_debug_link(link, "IPv4 link-local %s %u.%u.%u.%u",
1273                                deprecate ? "deprecate" : "approve",
1274                                ADDRESS_FMT_VAL(addr));
1275
1276                 r = address_new_dynamic(&address);
1277                 if (r < 0) {
1278                         log_error_link(link, "Could not allocate address: %s", strerror(-r));
1279                         return r;
1280                 }
1281
1282                 address->family = AF_INET;
1283                 address->in_addr.in = addr;
1284                 address->prefixlen = 16;
1285                 address->scope = RT_SCOPE_LINK;
1286                 address->cinfo.ifa_prefered = deprecate ? 0 : CACHE_INFO_INFINITY_LIFE_TIME;
1287                 address->broadcast.s_addr = address->in_addr.in.s_addr | htonl(0xfffffffflu >> address->prefixlen);
1288
1289                 address_update(address, link, &address_update_handler);
1290                 link_ref(link);
1291         }
1292
1293         return 0;
1294
1295 }
1296
1297 static int ipv4ll_address_lost(Link *link) {
1298         int r;
1299         struct in_addr addr;
1300
1301         assert(link);
1302
1303         r = sd_ipv4ll_get_address(link->ipv4ll, &addr);
1304         if (r >= 0) {
1305                 _cleanup_address_free_ Address *address = NULL;
1306                 _cleanup_route_free_ Route *route = NULL;
1307
1308                 log_debug_link(link, "IPv4 link-local release %u.%u.%u.%u",
1309                                 ADDRESS_FMT_VAL(addr));
1310
1311                 r = address_new_dynamic(&address);
1312                 if (r < 0) {
1313                         log_error_link(link, "Could not allocate address: %s", strerror(-r));
1314                         return r;
1315                 }
1316
1317                 address->family = AF_INET;
1318                 address->in_addr.in = addr;
1319                 address->prefixlen = 16;
1320                 address->scope = RT_SCOPE_LINK;
1321
1322                 address_drop(address, link, &address_drop_handler);
1323                 link_ref(link);
1324
1325                 r = route_new_dynamic(&route);
1326                 if (r < 0) {
1327                         log_error_link(link, "Could not allocate route: %s",
1328                                        strerror(-r));
1329                         return r;
1330                 }
1331
1332                 route->family = AF_INET;
1333                 route->scope = RT_SCOPE_LINK;
1334                 route->metrics = 99;
1335
1336                 route_drop(route, link, &route_drop_handler);
1337                 link_ref(link);
1338         }
1339
1340         return 0;
1341 }
1342
1343 static bool ipv4ll_is_bound(sd_ipv4ll *ll) {
1344         int r;
1345         struct in_addr addr;
1346
1347         assert(ll);
1348
1349         r = sd_ipv4ll_get_address(ll, &addr);
1350         if (r < 0)
1351                 return false;
1352         return true;
1353 }
1354
1355 static int ipv4ll_address_claimed(sd_ipv4ll *ll, Link *link) {
1356         struct in_addr address;
1357         int r;
1358
1359         assert(ll);
1360         assert(link);
1361
1362         r = sd_ipv4ll_get_address(ll, &address);
1363         if (r < 0)
1364                 return r;
1365
1366         log_struct_link(LOG_INFO, link,
1367                         "MESSAGE=%-*s: IPv4 link-local address %u.%u.%u.%u",
1368                         IFNAMSIZ,
1369                         link->ifname,
1370                         ADDRESS_FMT_VAL(address),
1371                         NULL);
1372
1373        link_enter_set_addresses(link);
1374
1375        return 0;
1376 }
1377
1378 static void ipv4ll_handler(sd_ipv4ll *ll, int event, void *userdata){
1379         Link *link = userdata;
1380         int r;
1381
1382         assert(link);
1383         assert(link->network);
1384         assert(link->manager);
1385
1386         if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
1387                 return;
1388
1389         switch(event) {
1390                 case IPV4LL_EVENT_STOP:
1391                 case IPV4LL_EVENT_CONFLICT:
1392                         r = ipv4ll_address_lost(link);
1393                         if (r < 0) {
1394                                 link_enter_failed(link);
1395                                 return;
1396                         }
1397                         break;
1398                 case IPV4LL_EVENT_BIND:
1399                         r = ipv4ll_address_claimed(ll, link);
1400                         if (r < 0) {
1401                                 link_enter_failed(link);
1402                                 return;
1403                         }
1404                         break;
1405                 default:
1406                         if (event < 0)
1407                                 log_warning_link(link, "IPv4 link-local error: %s", strerror(-event));
1408                         else
1409                                 log_warning_link(link, "IPv4 link-local unknown event: %d", event);
1410                         break;
1411         }
1412 }
1413
1414 static void dhcp6_handler(sd_dhcp6_client *client, int event, void *userdata) {
1415         Link *link = userdata;
1416
1417         assert(link);
1418         assert(link->network);
1419         assert(link->manager);
1420
1421         if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
1422                 return;
1423
1424         switch(event) {
1425         case DHCP6_EVENT_STOP:
1426         case DHCP6_EVENT_RESEND_EXPIRE:
1427         case DHCP6_EVENT_RETRANS_MAX:
1428         case DHCP6_EVENT_IP_ACQUIRE:
1429                 log_debug_link(link, "DHCPv6 event %d", event);
1430
1431                 break;
1432
1433         default:
1434                 if (event < 0)
1435                         log_warning_link(link, "DHCPv6 error: %s",
1436                                          strerror(-event));
1437                 else
1438                         log_warning_link(link, "DHCPv6 unknown event: %d",
1439                                          event);
1440                 return;
1441         }
1442 }
1443
1444 static void icmp6_router_handler(sd_icmp6_nd *nd, int event, void *userdata) {
1445         Link *link = userdata;
1446         int r;
1447
1448         assert(link);
1449         assert(link->network);
1450         assert(link->manager);
1451
1452         if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
1453                 return;
1454
1455         switch(event) {
1456         case ICMP6_EVENT_ROUTER_ADVERTISMENT_NONE:
1457         case ICMP6_EVENT_ROUTER_ADVERTISMENT_OTHER:
1458                 return;
1459
1460         case ICMP6_EVENT_ROUTER_ADVERTISMENT_TIMEOUT:
1461         case ICMP6_EVENT_ROUTER_ADVERTISMENT_MANAGED:
1462                 break;
1463
1464         default:
1465                 if (event < 0)
1466                         log_warning_link(link, "ICMPv6 error: %s",
1467                                          strerror(-event));
1468                 else
1469                         log_warning_link(link, "ICMPv6 unknown event: %d",
1470                                          event);
1471
1472                 return;
1473         }
1474
1475         if (link->dhcp6_client)
1476                 return;
1477
1478         r = sd_dhcp6_client_new(&link->dhcp6_client);
1479         if (r < 0)
1480                 return;
1481
1482         r = sd_dhcp6_client_attach_event(link->dhcp6_client, NULL, 0);
1483         if (r < 0) {
1484                 link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client);
1485                 return;
1486         }
1487
1488         r = sd_dhcp6_client_set_mac(link->dhcp6_client, &link->mac);
1489         if (r < 0) {
1490                 link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client);
1491                 return;
1492         }
1493
1494         r = sd_dhcp6_client_set_index(link->dhcp6_client, link->ifindex);
1495         if (r < 0) {
1496                 link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client);
1497                 return;
1498         }
1499
1500         r = sd_dhcp6_client_set_callback(link->dhcp6_client, dhcp6_handler,
1501                                          link);
1502         if (r < 0) {
1503                 link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client);
1504                 return;
1505         }
1506
1507         r = sd_dhcp6_client_start(link->dhcp6_client);
1508         if (r < 0)
1509                 link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client);
1510 }
1511
1512 static int link_acquire_conf(Link *link) {
1513         int r;
1514
1515         assert(link);
1516         assert(link->network);
1517         assert(link->manager);
1518         assert(link->manager->event);
1519
1520         if (link->network->ipv4ll) {
1521                 assert(link->ipv4ll);
1522
1523                 log_debug_link(link, "acquiring IPv4 link-local address");
1524
1525                 r = sd_ipv4ll_start(link->ipv4ll);
1526                 if (r < 0) {
1527                         log_warning_link(link, "could not acquire IPv4 "
1528                                          "link-local address");
1529                         return r;
1530                 }
1531         }
1532
1533         if (IN_SET(link->network->dhcp, DHCP_SUPPORT_BOTH, DHCP_SUPPORT_V4)) {
1534                 assert(link->dhcp_client);
1535
1536                 log_debug_link(link, "acquiring DHCPv4 lease");
1537
1538                 r = sd_dhcp_client_start(link->dhcp_client);
1539                 if (r < 0) {
1540                         log_warning_link(link, "could not acquire DHCPv4 "
1541                                          "lease");
1542                         return r;
1543                 }
1544         }
1545
1546         if (IN_SET(link->network->dhcp, DHCP_SUPPORT_BOTH, DHCP_SUPPORT_V6)) {
1547                 assert(link->icmp6_router_discovery);
1548
1549                 log_debug_link(link, "discovering IPv6 routers");
1550
1551                 r = sd_icmp6_router_solicitation_start(link->icmp6_router_discovery);
1552                 if (r < 0) {
1553                         log_warning_link(link, "could not start IPv6 router discovery");
1554                         return r;
1555                 }
1556         }
1557
1558         return 0;
1559 }
1560
1561 bool link_has_carrier(unsigned flags, uint8_t operstate) {
1562         /* see Documentation/networking/operstates.txt in the kernel sources */
1563
1564         if (operstate == IF_OPER_UP)
1565                 return true;
1566
1567         if (operstate == IF_OPER_UNKNOWN)
1568                 /* operstate may not be implemented, so fall back to flags */
1569                 if ((flags & IFF_LOWER_UP) && !(flags & IFF_DORMANT))
1570                         return true;
1571
1572         return false;
1573 }
1574
1575 #define FLAG_STRING(string, flag, old, new) \
1576         (((old ^ new) & flag) \
1577                 ? ((old & flag) ? (" -" string) : (" +" string)) \
1578                 : "")
1579
1580 static int link_update_flags(Link *link, sd_rtnl_message *m) {
1581         unsigned flags, unknown_flags_added, unknown_flags_removed, unknown_flags;
1582         uint8_t operstate;
1583         bool carrier_gained = false, carrier_lost = false;
1584         int r;
1585
1586         assert(link);
1587
1588         r = sd_rtnl_message_link_get_flags(m, &flags);
1589         if (r < 0) {
1590                 log_warning_link(link, "Could not get link flags");
1591                 return r;
1592         }
1593
1594         r = sd_rtnl_message_read_u8(m, IFLA_OPERSTATE, &operstate);
1595         if (r < 0)
1596                 /* if we got a message without operstate, take it to mean
1597                    the state was unchanged */
1598                 operstate = link->kernel_operstate;
1599
1600         if ((link->flags == flags) && (link->kernel_operstate == operstate))
1601                 return 0;
1602
1603         if (link->flags != flags) {
1604                 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",
1605                                FLAG_STRING("LOOPBACK", IFF_LOOPBACK, link->flags, flags),
1606                                FLAG_STRING("MASTER", IFF_MASTER, link->flags, flags),
1607                                FLAG_STRING("SLAVE", IFF_SLAVE, link->flags, flags),
1608                                FLAG_STRING("UP", IFF_UP, link->flags, flags),
1609                                FLAG_STRING("DORMANT", IFF_DORMANT, link->flags, flags),
1610                                FLAG_STRING("LOWER_UP", IFF_LOWER_UP, link->flags, flags),
1611                                FLAG_STRING("RUNNING", IFF_RUNNING, link->flags, flags),
1612                                FLAG_STRING("MULTICAST", IFF_MULTICAST, link->flags, flags),
1613                                FLAG_STRING("BROADCAST", IFF_BROADCAST, link->flags, flags),
1614                                FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT, link->flags, flags),
1615                                FLAG_STRING("PROMISC", IFF_PROMISC, link->flags, flags),
1616                                FLAG_STRING("ALLMULTI", IFF_ALLMULTI, link->flags, flags),
1617                                FLAG_STRING("PORTSEL", IFF_PORTSEL, link->flags, flags),
1618                                FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA, link->flags, flags),
1619                                FLAG_STRING("DYNAMIC", IFF_DYNAMIC, link->flags, flags),
1620                                FLAG_STRING("NOARP", IFF_NOARP, link->flags, flags),
1621                                FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS, link->flags, flags),
1622                                FLAG_STRING("DEBUG", IFF_DEBUG, link->flags, flags),
1623                                FLAG_STRING("ECHO", IFF_ECHO, link->flags, flags));
1624
1625                 unknown_flags = ~(IFF_LOOPBACK | IFF_MASTER | IFF_SLAVE | IFF_UP |
1626                                   IFF_DORMANT | IFF_LOWER_UP | IFF_RUNNING |
1627                                   IFF_MULTICAST | IFF_BROADCAST | IFF_POINTOPOINT |
1628                                   IFF_PROMISC | IFF_ALLMULTI | IFF_PORTSEL |
1629                                   IFF_AUTOMEDIA | IFF_DYNAMIC | IFF_NOARP |
1630                                   IFF_NOTRAILERS | IFF_DEBUG | IFF_ECHO);
1631                 unknown_flags_added = ((link->flags ^ flags) & flags & unknown_flags);
1632                 unknown_flags_removed = ((link->flags ^ flags) & link->flags & unknown_flags);
1633
1634                 /* link flags are currently at most 18 bits, let's align to printing 20 */
1635                 if (unknown_flags_added)
1636                         log_debug_link(link, "unknown link flags gained: %#.5x (ignoring)",
1637                                        unknown_flags_added);
1638
1639                 if (unknown_flags_removed)
1640                         log_debug_link(link, "unknown link flags lost: %#.5x (ignoring)",
1641                                        unknown_flags_removed);
1642         }
1643
1644         carrier_gained = !link_has_carrier(link->flags, link->kernel_operstate) &&
1645                        link_has_carrier(flags, operstate);
1646         carrier_lost = link_has_carrier(link->flags, link->kernel_operstate) &&
1647                          !link_has_carrier(flags, operstate);
1648
1649         link->flags = flags;
1650         link->kernel_operstate = operstate;
1651
1652         link_save(link);
1653
1654         if (link->state == LINK_STATE_FAILED ||
1655             link->state == LINK_STATE_UNMANAGED)
1656                 return 0;
1657
1658         if (carrier_gained) {
1659                 log_info_link(link, "gained carrier");
1660
1661                 if (link->network) {
1662                         r = link_acquire_conf(link);
1663                         if (r < 0) {
1664                                 link_enter_failed(link);
1665                                 return r;
1666                         }
1667                 }
1668         } else if (carrier_lost) {
1669                 log_info_link(link, "lost carrier");
1670
1671                 r = link_stop_clients(link);
1672                 if (r < 0) {
1673                         link_enter_failed(link);
1674                         return r;
1675                 }
1676         }
1677
1678         return 0;
1679 }
1680
1681 static int link_up_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
1682         Link *link = userdata;
1683         int r;
1684
1685         assert(link);
1686
1687         if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) {
1688                 link_unref(link);
1689                 return 1;
1690         }
1691
1692         r = sd_rtnl_message_get_errno(m);
1693         if (r < 0) {
1694                 /* we warn but don't fail the link, as it may
1695                    be brought up later */
1696                 log_struct_link(LOG_WARNING, link,
1697                                 "MESSAGE=%-*s: could not bring up interface: %s",
1698                                 IFNAMSIZ,
1699                                 link->ifname, strerror(-r),
1700                                 "ERRNO=%d", -r,
1701                                 NULL);
1702         }
1703
1704         link_unref(link);
1705
1706         return 1;
1707 }
1708
1709 static int link_up(Link *link) {
1710         _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
1711         int r;
1712
1713         assert(link);
1714         assert(link->manager);
1715         assert(link->manager->rtnl);
1716
1717         log_debug_link(link, "bringing link up");
1718
1719         r = sd_rtnl_message_new_link(link->manager->rtnl, &req,
1720                                      RTM_SETLINK, link->ifindex);
1721         if (r < 0) {
1722                 log_error_link(link, "Could not allocate RTM_SETLINK message");
1723                 return r;
1724         }
1725
1726         r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP);
1727         if (r < 0) {
1728                 log_error_link(link, "Could not set link flags: %s", strerror(-r));
1729                 return r;
1730         }
1731
1732         r = sd_rtnl_call_async(link->manager->rtnl, req, link_up_handler, link, 0, NULL);
1733         if (r < 0) {
1734                 log_error_link(link,
1735                                "Could not send rtnetlink message: %s", strerror(-r));
1736                 return r;
1737         }
1738
1739         link_ref(link);
1740
1741         return 0;
1742 }
1743
1744 static int link_enslaved(Link *link) {
1745         int r;
1746
1747         assert(link);
1748         assert(link->state == LINK_STATE_ENSLAVING);
1749         assert(link->network);
1750
1751         if (!(link->flags & IFF_UP)) {
1752                 r = link_up(link);
1753                 if (r < 0) {
1754                         link_enter_failed(link);
1755                         return r;
1756                 }
1757         }
1758
1759         return link_enter_set_addresses(link);
1760 }
1761
1762 static int enslave_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
1763         Link *link = userdata;
1764         int r;
1765
1766         assert(link);
1767         assert(IN_SET(link->state, LINK_STATE_ENSLAVING, LINK_STATE_FAILED,
1768                       LINK_STATE_LINGER));
1769         assert(link->network);
1770
1771         link->enslaving --;
1772
1773         if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) {
1774                 link_unref(link);
1775                 return 1;
1776         }
1777
1778         r = sd_rtnl_message_get_errno(m);
1779         if (r < 0) {
1780                 log_struct_link(LOG_ERR, link,
1781                                 "MESSAGE=%-*s: could not enslave: %s",
1782                                 IFNAMSIZ,
1783                                 link->ifname, strerror(-r),
1784                                 "ERRNO=%d", -r,
1785                                 NULL);
1786                 link_enter_failed(link);
1787                 link_unref(link);
1788                 return 1;
1789         }
1790
1791         log_debug_link(link, "enslaved");
1792
1793         if (link->enslaving == 0)
1794                 link_enslaved(link);
1795
1796         link_unref(link);
1797
1798         return 1;
1799 }
1800
1801 static int link_enter_enslave(Link *link) {
1802         NetDev *vlan, *macvlan, *vxlan;
1803         Iterator i;
1804         int r;
1805
1806         assert(link);
1807         assert(link->network);
1808         assert(link->state == LINK_STATE_INITIALIZING);
1809
1810         link->state = LINK_STATE_ENSLAVING;
1811
1812         link_save(link);
1813
1814         if (!link->network->bridge &&
1815             !link->network->bond &&
1816             !link->network->tunnel &&
1817             hashmap_isempty(link->network->vlans) &&
1818             hashmap_isempty(link->network->macvlans) &&
1819             hashmap_isempty(link->network->vxlans))
1820                 return link_enslaved(link);
1821
1822         if (link->network->bond) {
1823                 log_struct_link(LOG_DEBUG, link,
1824                                 "MESSAGE=%-*s: enslaving by '%s'",
1825                                 IFNAMSIZ,
1826                                 link->ifname, link->network->bond->ifname,
1827                                 NETDEV(link->network->bond),
1828                                 NULL);
1829
1830                 r = netdev_enslave(link->network->bond, link, &enslave_handler);
1831                 if (r < 0) {
1832                         log_struct_link(LOG_WARNING, link,
1833                                         "MESSAGE=%-*s: could not enslave by '%s': %s",
1834                                         IFNAMSIZ,
1835                                         link->ifname, link->network->bond->ifname, strerror(-r),
1836                                         NETDEV(link->network->bond),
1837                                         NULL);
1838                         link_enter_failed(link);
1839                         return r;
1840                 }
1841
1842                 link_ref(link);
1843                 link->enslaving ++;
1844         }
1845
1846         if (link->network->bridge) {
1847                 log_struct_link(LOG_DEBUG, link,
1848                                 "MESSAGE=%-*s: enslaving by '%s'",
1849                                 IFNAMSIZ,
1850                                 link->ifname, link->network->bridge->ifname,
1851                                 NETDEV(link->network->bridge),
1852                                 NULL);
1853
1854                 r = netdev_enslave(link->network->bridge, link, &enslave_handler);
1855                 if (r < 0) {
1856                         log_struct_link(LOG_WARNING, link,
1857                                         "MESSAGE=%-*s: could not enslave by '%s': %s",
1858                                         IFNAMSIZ,
1859                                         link->ifname, link->network->bridge->ifname, strerror(-r),
1860                                         NETDEV(link->network->bridge),
1861                                         NULL);
1862                         link_enter_failed(link);
1863                         return r;
1864                 }
1865
1866                 link_ref(link);
1867                 link->enslaving ++;
1868         }
1869
1870         if (link->network->tunnel) {
1871                 log_struct_link(LOG_DEBUG, link,
1872                                 "MESSAGE=%-*s: enslaving by '%s'",
1873                                 IFNAMSIZ,
1874                                 link->ifname, link->network->tunnel->ifname,
1875                                 NETDEV(link->network->tunnel),
1876                                 NULL);
1877
1878                 r = netdev_enslave(link->network->tunnel, link, &enslave_handler);
1879                 if (r < 0) {
1880                         log_struct_link(LOG_WARNING, link,
1881                                         "MESSAGE=%-*s: could not enslave by '%s': %s",
1882                                         IFNAMSIZ,
1883                                         link->ifname, link->network->tunnel->ifname, strerror(-r),
1884                                         NETDEV(link->network->tunnel),
1885                                         NULL);
1886                         link_enter_failed(link);
1887                         return r;
1888                 }
1889
1890                 link_ref(link);
1891                 link->enslaving ++;
1892         }
1893
1894         HASHMAP_FOREACH(vlan, link->network->vlans, i) {
1895                 log_struct_link(LOG_DEBUG, link,
1896                                 "MESSAGE=%-*s: enslaving by '%s'",
1897                                 IFNAMSIZ,
1898                                 link->ifname, vlan->ifname, NETDEV(vlan), NULL);
1899
1900                 r = netdev_enslave(vlan, link, &enslave_handler);
1901                 if (r < 0) {
1902                         log_struct_link(LOG_WARNING, link,
1903                                         "MESSAGE=%-*s: could not enslave by '%s': %s",
1904                                         IFNAMSIZ,
1905                                         link->ifname, vlan->ifname, strerror(-r),
1906                                         NETDEV(vlan), NULL);
1907                         link_enter_failed(link);
1908                         return r;
1909                 }
1910
1911                 link_ref(link);
1912                 link->enslaving ++;
1913         }
1914
1915         HASHMAP_FOREACH(macvlan, link->network->macvlans, i) {
1916                 log_struct_link(LOG_DEBUG, link,
1917                                 "MESSAGE=%-*s: enslaving by '%s'",
1918                                 IFNAMSIZ,
1919                                 link->ifname, macvlan->ifname, NETDEV(macvlan), NULL);
1920
1921                 r = netdev_enslave(macvlan, link, &enslave_handler);
1922                 if (r < 0) {
1923                         log_struct_link(LOG_WARNING, link,
1924                                         "MESSAGE=%-*s: could not enslave by '%s': %s",
1925                                         IFNAMSIZ,
1926                                         link->ifname, macvlan->ifname, strerror(-r),
1927                                         NETDEV(macvlan), NULL);
1928                         link_enter_failed(link);
1929                         return r;
1930                 }
1931
1932                 link_ref(link);
1933                 link->enslaving ++;
1934         }
1935
1936         HASHMAP_FOREACH(vxlan, link->network->vxlans, i) {
1937                 log_struct_link(LOG_DEBUG, link,
1938                                 "MESSAGE=%*s: enslaving by '%s'",
1939                                 IFNAMSIZ,
1940                                 link->ifname, vxlan->ifname, NETDEV(vxlan), NULL);
1941
1942                 r = netdev_enslave(vxlan, link, &enslave_handler);
1943                 if (r < 0) {
1944                         log_struct_link(LOG_WARNING, link,
1945                                         "MESSAGE=%*s: could not enslave by '%s': %s",
1946                                         IFNAMSIZ,
1947                                         link->ifname, vxlan->ifname, strerror(-r),
1948                                         NETDEV(vxlan), NULL);
1949                         link_enter_failed(link);
1950                         return r;
1951                 }
1952
1953                 link_ref(link);
1954                 link->enslaving ++;
1955         }
1956
1957         return 0;
1958 }
1959
1960 static int link_configure(Link *link) {
1961         int r;
1962
1963         assert(link);
1964         assert(link->state == LINK_STATE_INITIALIZING);
1965
1966         if (link->network->ipv4ll) {
1967                 uint8_t seed[8];
1968
1969                 r = sd_ipv4ll_new(&link->ipv4ll);
1970                 if (r < 0)
1971                         return r;
1972
1973                 if (link->udev_device) {
1974                         r = net_get_unique_predictable_data(link->udev_device, seed);
1975                         if (r >= 0) {
1976                                 r = sd_ipv4ll_set_address_seed(link->ipv4ll, seed);
1977                                 if (r < 0)
1978                                         return r;
1979                         }
1980                 }
1981
1982                 r = sd_ipv4ll_attach_event(link->ipv4ll, NULL, 0);
1983                 if (r < 0)
1984                         return r;
1985
1986                 r = sd_ipv4ll_set_mac(link->ipv4ll, &link->mac);
1987                 if (r < 0)
1988                         return r;
1989
1990                 r = sd_ipv4ll_set_index(link->ipv4ll, link->ifindex);
1991                 if (r < 0)
1992                         return r;
1993
1994                 r = sd_ipv4ll_set_callback(link->ipv4ll, ipv4ll_handler, link);
1995                 if (r < 0)
1996                         return r;
1997         }
1998
1999         if (IN_SET(link->network->dhcp, DHCP_SUPPORT_BOTH, DHCP_SUPPORT_V4)) {
2000                 r = sd_dhcp_client_new(&link->dhcp_client);
2001                 if (r < 0)
2002                         return r;
2003
2004                 r = sd_dhcp_client_attach_event(link->dhcp_client, NULL, 0);
2005                 if (r < 0)
2006                         return r;
2007
2008                 r = sd_dhcp_client_set_mac(link->dhcp_client, &link->mac);
2009                 if (r < 0)
2010                         return r;
2011
2012                 r = sd_dhcp_client_set_index(link->dhcp_client, link->ifindex);
2013                 if (r < 0)
2014                         return r;
2015
2016                 r = sd_dhcp_client_set_callback(link->dhcp_client, dhcp_handler, link);
2017                 if (r < 0)
2018                         return r;
2019
2020                 if (link->network->dhcp_mtu) {
2021                         r = sd_dhcp_client_set_request_option(link->dhcp_client, 26);
2022                         if (r < 0)
2023                                 return r;
2024                 }
2025                 if (link->network->dhcp_routes) {
2026                         r = sd_dhcp_client_set_request_option(link->dhcp_client, DHCP_OPTION_STATIC_ROUTE);
2027                         if (r < 0)
2028                                 return r;
2029                         r = sd_dhcp_client_set_request_option(link->dhcp_client, DHCP_OPTION_CLASSLESS_STATIC_ROUTE);
2030                         if (r < 0)
2031                                 return r;
2032                 }
2033         }
2034
2035         if (link->network->dhcp_server) {
2036                 r = sd_dhcp_server_new(&link->dhcp_server, link->ifindex);
2037                 if (r < 0)
2038                         return r;
2039
2040                 r = sd_dhcp_server_attach_event(link->dhcp_server, NULL, 0);
2041                 if (r < 0)
2042                         return r;
2043         }
2044
2045         if (IN_SET(link->network->dhcp, DHCP_SUPPORT_BOTH, DHCP_SUPPORT_V6)) {
2046                 r = sd_icmp6_nd_new(&link->icmp6_router_discovery);
2047                 if (r < 0)
2048                         return r;
2049
2050                 r = sd_icmp6_nd_attach_event(link->icmp6_router_discovery,
2051                                              NULL, 0);
2052                 if (r < 0)
2053                         return r;
2054
2055                 r = sd_icmp6_nd_set_mac(link->icmp6_router_discovery,
2056                                         &link->mac);
2057                 if (r < 0)
2058                         return r;
2059
2060                 r = sd_icmp6_nd_set_index(link->icmp6_router_discovery,
2061                                           link->ifindex);
2062                 if (r < 0)
2063                         return r;
2064
2065                 r = sd_icmp6_nd_set_callback(link->icmp6_router_discovery,
2066                                              icmp6_router_handler, link);
2067                 if (r < 0)
2068                         return r;
2069         }
2070
2071         if (link_has_carrier(link->flags, link->kernel_operstate)) {
2072                 r = link_acquire_conf(link);
2073                 if (r < 0)
2074                         return r;
2075         }
2076
2077         return link_enter_enslave(link);
2078 }
2079
2080 static int link_initialized_and_synced(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
2081         Link *link = userdata;
2082         Network *network;
2083         int r;
2084
2085         assert(link);
2086         assert(link->ifname);
2087         assert(link->manager);
2088
2089         if (link->state != LINK_STATE_INITIALIZING)
2090                 return 0;
2091
2092         log_debug_link(link, "link state is up-to-date");
2093
2094         r = network_get(link->manager, link->udev_device, link->ifname, &link->mac, &network);
2095         if (r == -ENOENT) {
2096                 link_enter_unmanaged(link);
2097                 return 0;
2098         } else if (r < 0)
2099                 return r;
2100
2101         r = network_apply(link->manager, network, link);
2102         if (r < 0)
2103                 return r;
2104
2105         r = link_configure(link);
2106         if (r < 0)
2107                 return r;
2108
2109         return 0;
2110 }
2111
2112 int link_initialized(Link *link, struct udev_device *device) {
2113         _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
2114         int r;
2115
2116         assert(link);
2117         assert(link->manager);
2118         assert(link->manager->rtnl);
2119         assert(device);
2120
2121         if (link->state != LINK_STATE_INITIALIZING)
2122                 return 0;
2123
2124         log_debug_link(link, "udev initialized link");
2125
2126         link->udev_device = udev_device_ref(device);
2127
2128         /* udev has initialized the link, but we don't know if we have yet processed
2129            the NEWLINK messages with the latest state. Do a GETLINK, when it returns
2130            we know that the pending NEWLINKs have already been processed and that we
2131            are up-to-date */
2132
2133         r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_GETLINK, link->ifindex);
2134         if (r < 0)
2135                 return r;
2136
2137         r = sd_rtnl_call_async(link->manager->rtnl, req, link_initialized_and_synced, link, 0, NULL);
2138         if (r < 0)
2139                 return r;
2140
2141         return 0;
2142 }
2143
2144 int link_rtnl_process_address(sd_rtnl *rtnl, sd_rtnl_message *message, void *userdata) {
2145         Manager *m = userdata;
2146         Link *link = NULL;
2147         uint16_t type;
2148         _cleanup_address_free_ Address *address = NULL;
2149         Address *ad;
2150         char buf[INET6_ADDRSTRLEN];
2151         bool address_dropped = false;
2152         int r, ifindex;
2153
2154         assert(rtnl);
2155         assert(message);
2156         assert(m);
2157
2158         r = sd_rtnl_message_get_type(message, &type);
2159         if (r < 0) {
2160                 log_warning("rtnl: could not get message type");
2161                 return 0;
2162         }
2163
2164         r = sd_rtnl_message_addr_get_ifindex(message, &ifindex);
2165         if (r < 0 || ifindex <= 0) {
2166                 log_warning("rtnl: received address message without valid ifindex, ignoring");
2167                 return 0;
2168         } else {
2169                 r = link_get(m, ifindex, &link);
2170                 if (r < 0 || !link) {
2171                         log_warning("rtnl: received address for a nonexistent link, ignoring");
2172                         return 0;
2173                 }
2174         }
2175
2176         r = address_new_dynamic(&address);
2177         if (r < 0)
2178                 return 0;
2179
2180         r = sd_rtnl_message_addr_get_family(message, &address->family);
2181         if (r < 0 || !IN_SET(address->family, AF_INET, AF_INET6)) {
2182                 log_warning_link(link, "rtnl: received address with invalid family, ignoring");
2183                 return 0;
2184         }
2185
2186         r = sd_rtnl_message_addr_get_prefixlen(message, &address->prefixlen);
2187         if (r < 0) {
2188                 log_warning_link(link, "rtnl: received address with invalid prefixlen, ignoring");
2189                 return 0;
2190         }
2191
2192         r = sd_rtnl_message_addr_get_scope(message, &address->scope);
2193         if (r < 0) {
2194                 log_warning_link(link, "rtnl: received address with invalid scope, ignoring");
2195                 return 0;
2196         }
2197
2198         switch (address->family) {
2199         case AF_INET:
2200                 r = sd_rtnl_message_read_in_addr(message, IFA_LOCAL, &address->in_addr.in);
2201                 if (r < 0) {
2202                         log_warning_link(link, "rtnl: received address without valid address, ignoring");
2203                         return 0;
2204                 }
2205
2206                 break;
2207
2208         case AF_INET6:
2209                 r = sd_rtnl_message_read_in6_addr(message, IFA_ADDRESS, &address->in_addr.in6);
2210                 if (r < 0) {
2211                         log_warning_link(link, "rtnl: received address without valid address, ignoring");
2212                         return 0;
2213                 }
2214
2215                 break;
2216
2217         default:
2218                 assert_not_reached("invalid address family");
2219         }
2220
2221         if (!inet_ntop(address->family, &address->in_addr, buf, INET6_ADDRSTRLEN)) {
2222                 log_warning_link(link, "could not print address");
2223                 return 0;
2224         }
2225
2226         LIST_FOREACH(addresses, ad, link->addresses) {
2227                 if (address_equal(ad, address)) {
2228                         LIST_REMOVE(addresses, link->addresses, ad);
2229
2230                         address_free(ad);
2231
2232                         address_dropped = true;
2233
2234                         break;
2235                 }
2236         }
2237
2238         switch (type) {
2239         case RTM_NEWADDR:
2240                 if (!address_dropped)
2241                         log_debug_link(link, "added address: %s/%u", buf,
2242                                       address->prefixlen);
2243
2244                 LIST_PREPEND(addresses, link->addresses, address);
2245                 address = NULL;
2246
2247                 link_save(link);
2248
2249                 break;
2250         case RTM_DELADDR:
2251                 if (address_dropped) {
2252                         log_debug_link(link, "removed address: %s/%u", buf,
2253                                       address->prefixlen);
2254
2255                         link_save(link);
2256                 }
2257
2258                 break;
2259         default:
2260                 assert_not_reached("Received invalid RTNL message type");
2261         }
2262
2263         return 1;
2264 }
2265
2266 static int link_get_address_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
2267         Link *link = userdata;
2268         int r;
2269
2270         assert(rtnl);
2271         assert(m);
2272         assert(link);
2273
2274         for (; m; m = sd_rtnl_message_next(m)) {
2275                 r = sd_rtnl_message_get_errno(m);
2276                 if (r < 0) {
2277                         log_debug_link(link, "getting address failed: %s", strerror(-r));
2278                         continue;
2279                 }
2280
2281                 r = link_rtnl_process_address(rtnl, m, link->manager);
2282                 if (r < 0)
2283                         log_warning_link(link, "could not process address: %s", strerror(-r));
2284         }
2285
2286         return 1;
2287 }
2288
2289 int link_add(Manager *m, sd_rtnl_message *message, Link **ret) {
2290         Link *link;
2291         _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
2292         _cleanup_udev_device_unref_ struct udev_device *device = NULL;
2293         char ifindex_str[2 + DECIMAL_STR_MAX(int)];
2294         int r;
2295
2296         assert(m);
2297         assert(m->rtnl);
2298         assert(message);
2299         assert(ret);
2300
2301         r = link_new(m, message, ret);
2302         if (r < 0)
2303                 return r;
2304
2305         link = *ret;
2306
2307         log_debug_link(link, "link %"PRIu64" added", link->ifindex);
2308
2309         r = sd_rtnl_message_new_addr(m->rtnl, &req, RTM_GETADDR, link->ifindex, 0);
2310         if (r < 0)
2311                 return r;
2312
2313         r = sd_rtnl_call_async(m->rtnl, req, link_get_address_handler, link, 0, NULL);
2314         if (r < 0)
2315                 return r;
2316
2317         if (detect_container(NULL) <= 0) {
2318                 /* not in a container, udev will be around */
2319                 sprintf(ifindex_str, "n%"PRIu64, link->ifindex);
2320                 device = udev_device_new_from_device_id(m->udev, ifindex_str);
2321                 if (!device) {
2322                         log_warning_link(link, "could not find udev device");
2323                         return -errno;
2324                 }
2325
2326                 if (udev_device_get_is_initialized(device) <= 0) {
2327                         /* not yet ready */
2328                         log_debug_link(link, "udev initializing link...");
2329                         return 0;
2330                 }
2331
2332                 r = link_initialized(link, device);
2333                 if (r < 0)
2334                         return r;
2335         } else {
2336                 r = link_initialized_and_synced(m->rtnl, NULL, link);
2337                 if (r < 0)
2338                         return r;
2339         }
2340
2341         return 0;
2342 }
2343
2344 int link_update(Link *link, sd_rtnl_message *m) {
2345         struct ether_addr mac;
2346         char *ifname;
2347         int r;
2348
2349         assert(link);
2350         assert(link->ifname);
2351         assert(m);
2352
2353         if (link->state == LINK_STATE_LINGER) {
2354                 link_ref(link);
2355                 log_info_link(link, "link readded");
2356                 link->state = LINK_STATE_ENSLAVING;
2357         }
2358
2359         r = sd_rtnl_message_read_string(m, IFLA_IFNAME, &ifname);
2360         if (r >= 0 && !streq(ifname, link->ifname)) {
2361                 log_info_link(link, "renamed to %s", ifname);
2362
2363                 free(link->ifname);
2364                 link->ifname = strdup(ifname);
2365                 if (!link->ifname)
2366                         return -ENOMEM;
2367         }
2368
2369         if (!link->original_mtu) {
2370                 r = sd_rtnl_message_read_u16(m, IFLA_MTU, &link->original_mtu);
2371                 if (r >= 0)
2372                         log_debug_link(link, "saved original MTU: %"
2373                                        PRIu16, link->original_mtu);
2374         }
2375
2376         /* The kernel may broadcast NEWLINK messages without the MAC address
2377            set, simply ignore them. */
2378         r = sd_rtnl_message_read_ether_addr(m, IFLA_ADDRESS, &mac);
2379         if (r >= 0) {
2380                 if (memcmp(link->mac.ether_addr_octet, mac.ether_addr_octet, ETH_ALEN)) {
2381
2382                         memcpy(link->mac.ether_addr_octet, mac.ether_addr_octet, ETH_ALEN);
2383
2384                         log_debug_link(link, "MAC address: "
2385                                        "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
2386                                        mac.ether_addr_octet[0],
2387                                        mac.ether_addr_octet[1],
2388                                        mac.ether_addr_octet[2],
2389                                        mac.ether_addr_octet[3],
2390                                        mac.ether_addr_octet[4],
2391                                        mac.ether_addr_octet[5]);
2392
2393                         if (link->ipv4ll) {
2394                                 r = sd_ipv4ll_set_mac(link->ipv4ll, &link->mac);
2395                                 if (r < 0) {
2396                                         log_warning_link(link, "Could not update MAC "
2397                                                          "address in IPv4LL client: %s",
2398                                                          strerror(-r));
2399                                         return r;
2400                                 }
2401                         }
2402
2403                         if (link->dhcp_client) {
2404                                 r = sd_dhcp_client_set_mac(link->dhcp_client, &link->mac);
2405                                 if (r < 0) {
2406                                         log_warning_link(link, "Could not update MAC "
2407                                                          "address in DHCP client: %s",
2408                                                          strerror(-r));
2409                                         return r;
2410                                 }
2411                         }
2412
2413                         if (link->dhcp6_client) {
2414                                 r = sd_dhcp6_client_set_mac(link->dhcp6_client,
2415                                                             &link->mac);
2416                                 if (r < 0) {
2417                                         log_warning_link(link, "Could not update MAC address in DHCPv6 client: %s",
2418                                                          strerror(-r));
2419                                         return r;
2420                                 }
2421                         }
2422                 }
2423         }
2424
2425         return link_update_flags(link, m);
2426 }
2427
2428 static void serialize_addresses(FILE *f, const char *key, Address *address) {
2429         Address *ad;
2430
2431         assert(f);
2432         assert(key);
2433
2434         if (!address)
2435                 return;
2436
2437         fprintf(f, "%s=", key);
2438
2439         LIST_FOREACH(addresses, ad, address) {
2440                 char buf[INET6_ADDRSTRLEN];
2441
2442                 if (inet_ntop(ad->family, &ad->in_addr, buf, INET6_ADDRSTRLEN))
2443                         fprintf(f, "%s%s", buf, (ad->addresses_next) ? " ": "");
2444         }
2445
2446         fputs("\n", f);
2447 }
2448
2449 static void link_update_operstate(Link *link) {
2450
2451         assert(link);
2452
2453         if (link->kernel_operstate == IF_OPER_DORMANT)
2454                 link->operstate = LINK_OPERSTATE_DORMANT;
2455         else if (link_has_carrier(link->flags, link->kernel_operstate)) {
2456                 Address *address;
2457                 uint8_t scope = RT_SCOPE_NOWHERE;
2458
2459                 /* if we have carrier, check what addresses we have */
2460                 LIST_FOREACH(addresses, address, link->addresses) {
2461                         if (address->scope < scope)
2462                                 scope = address->scope;
2463                 }
2464
2465                 if (scope < RT_SCOPE_SITE)
2466                         /* universally accessible addresses found */
2467                         link->operstate = LINK_OPERSTATE_ROUTABLE;
2468                 else if (scope < RT_SCOPE_HOST)
2469                         /* only link or site local addresses found */
2470                         link->operstate = LINK_OPERSTATE_DEGRADED;
2471                 else
2472                         /* no useful addresses found */
2473                         link->operstate = LINK_OPERSTATE_CARRIER;
2474         } else
2475                 link->operstate = LINK_OPERSTATE_UNKNOWN;
2476 }
2477
2478 int link_save(Link *link) {
2479         _cleanup_free_ char *temp_path = NULL;
2480         _cleanup_fclose_ FILE *f = NULL;
2481         const char *admin_state, *oper_state;
2482         int r;
2483
2484         assert(link);
2485         assert(link->state_file);
2486         assert(link->lease_file);
2487         assert(link->manager);
2488
2489         link_update_operstate(link);
2490
2491         r = manager_save(link->manager);
2492         if (r < 0)
2493                 return r;
2494
2495         if (link->state == LINK_STATE_LINGER) {
2496                 unlink(link->state_file);
2497                 return 0;
2498         }
2499
2500         admin_state = link_state_to_string(link->state);
2501         assert(admin_state);
2502
2503         oper_state = link_operstate_to_string(link->operstate);
2504         assert(oper_state);
2505
2506         r = fopen_temporary(link->state_file, &f, &temp_path);
2507         if (r < 0)
2508                 goto finish;
2509
2510         fchmod(fileno(f), 0644);
2511
2512         fprintf(f,
2513                 "# This is private data. Do not parse.\n"
2514                 "ADMIN_STATE=%s\n"
2515                 "OPER_STATE=%s\n"
2516                 "FLAGS=%u\n",
2517                 admin_state, oper_state, link->flags);
2518
2519         if (link->network) {
2520                 serialize_addresses(f, "DNS", link->network->dns);
2521                 serialize_addresses(f, "NTP", link->network->ntp);
2522         }
2523
2524         if (link->dhcp_lease) {
2525                 assert(link->network);
2526
2527                 r = dhcp_lease_save(link->dhcp_lease, link->lease_file);
2528                 if (r < 0)
2529                         goto finish;
2530
2531                 fprintf(f,
2532                         "DHCP_LEASE=%s\n"
2533                         "DHCP_USE_DNS=%s\n"
2534                         "DHCP_USE_NTP=%s\n",
2535                         link->lease_file,
2536                         yes_no(link->network->dhcp_dns),
2537                         yes_no(link->network->dhcp_ntp));
2538         } else
2539                 unlink(link->lease_file);
2540
2541         fflush(f);
2542
2543         if (ferror(f) || rename(temp_path, link->state_file) < 0) {
2544                 r = -errno;
2545                 unlink(link->state_file);
2546                 unlink(temp_path);
2547         }
2548
2549 finish:
2550         if (r < 0)
2551                 log_error_link(link, "Failed to save link data to %s: %s", link->state_file, strerror(-r));
2552
2553         return r;
2554 }
2555
2556 static const char* const link_state_table[_LINK_STATE_MAX] = {
2557         [LINK_STATE_INITIALIZING] = "initializing",
2558         [LINK_STATE_ENSLAVING] = "configuring",
2559         [LINK_STATE_SETTING_ADDRESSES] = "configuring",
2560         [LINK_STATE_SETTING_ROUTES] = "configuring",
2561         [LINK_STATE_CONFIGURED] = "configured",
2562         [LINK_STATE_UNMANAGED] = "unmanaged",
2563         [LINK_STATE_FAILED] = "failed",
2564         [LINK_STATE_LINGER] = "linger",
2565 };
2566
2567 DEFINE_STRING_TABLE_LOOKUP(link_state, LinkState);
2568
2569 static const char* const link_operstate_table[_LINK_OPERSTATE_MAX] = {
2570         [LINK_OPERSTATE_UNKNOWN] = "unknown",
2571         [LINK_OPERSTATE_DORMANT] = "dormant",
2572         [LINK_OPERSTATE_CARRIER] = "carrier",
2573         [LINK_OPERSTATE_DEGRADED] = "degraded",
2574         [LINK_OPERSTATE_ROUTABLE] = "routable",
2575 };
2576
2577 DEFINE_STRING_TABLE_LOOKUP(link_operstate, LinkOperationalState);
2578
2579 static const char* const dhcp_support_table[_DHCP_SUPPORT_MAX] = {
2580         [DHCP_SUPPORT_NONE] = "none",
2581         [DHCP_SUPPORT_BOTH] = "both",
2582         [DHCP_SUPPORT_V4] = "v4",
2583         [DHCP_SUPPORT_V6] = "v6",
2584 };
2585
2586 DEFINE_STRING_TABLE_LOOKUP(dhcp_support, DHCPSupport);
2587
2588 int config_parse_dhcp(
2589                 const char* unit,
2590                 const char *filename,
2591                 unsigned line,
2592                 const char *section,
2593                 unsigned section_line,
2594                 const char *lvalue,
2595                 int ltype,
2596                 const char *rvalue,
2597                 void *data,
2598                 void *userdata) {
2599
2600         DHCPSupport *dhcp = data;
2601         int k;
2602
2603         assert(filename);
2604         assert(lvalue);
2605         assert(rvalue);
2606         assert(data);
2607
2608         /* Our enum shall be a superset of booleans, hence first try
2609          * to parse as boolean, and then as enum */
2610
2611         k = parse_boolean(rvalue);
2612         if (k > 0)
2613                 *dhcp = DHCP_SUPPORT_BOTH;
2614         else if (k == 0)
2615                 *dhcp = DHCP_SUPPORT_NONE;
2616         else {
2617                 DHCPSupport s;
2618
2619                 s = dhcp_support_from_string(rvalue);
2620                 if (s < 0){
2621                         log_syntax(unit, LOG_ERR, filename, line, -s, "Failed to parse DHCP option, ignoring: %s", rvalue);
2622                         return 0;
2623                 }
2624
2625                 *dhcp = s;
2626         }
2627
2628         return 0;
2629 }