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