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