chiark / gitweb /
effa93c81e918cd5f97d74708cb6dd24269007cc
[elogind.git] / src / network / networkd-manager.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 <resolv.h>
23 #include <linux/if.h>
24
25 #include "path-util.h"
26 #include "networkd.h"
27 #include "libudev-private.h"
28 #include "udev-util.h"
29 #include "rtnl-util.h"
30 #include "mkdir.h"
31 #include "virt.h"
32
33 #include "sd-rtnl.h"
34
35 const char* const network_dirs[] = {
36         "/etc/systemd/network",
37         "/run/systemd/network",
38         "/usr/lib/systemd/network",
39 #ifdef HAVE_SPLIT_USR
40         "/lib/systemd/network",
41 #endif
42         NULL};
43
44 static int dispatch_sigterm(sd_event_source *es, const struct signalfd_siginfo *si, void *userdata) {
45         Manager *m = userdata;
46
47         assert(m);
48
49         log_received_signal(LOG_INFO, si);
50
51         sd_event_exit(m->event, 0);
52         return 0;
53 }
54
55 static int setup_signals(Manager *m) {
56         sigset_t mask;
57         int r;
58
59         assert(m);
60
61         assert_se(sigemptyset(&mask) == 0);
62         sigset_add_many(&mask, SIGINT, SIGTERM, -1);
63         assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
64
65         r = sd_event_add_signal(m->event, &m->sigterm_event_source, SIGTERM, dispatch_sigterm, m);
66         if (r < 0)
67                 return r;
68
69         r = sd_event_add_signal(m->event, &m->sigint_event_source, SIGINT, dispatch_sigterm, m);
70         if (r < 0)
71                 return r;
72
73         return 0;
74 }
75
76 int manager_new(Manager **ret) {
77         _cleanup_manager_free_ Manager *m = NULL;
78         int r;
79
80         m = new0(Manager, 1);
81         if (!m)
82                 return -ENOMEM;
83
84         m->state_file = strdup("/run/systemd/network/state");
85         if (!m->state_file)
86                 return -ENOMEM;
87
88         r = sd_event_default(&m->event);
89         if (r < 0)
90                 return r;
91
92         sd_event_set_watchdog(m->event, true);
93
94         r = sd_rtnl_open(&m->rtnl, 3, RTNLGRP_LINK, RTNLGRP_IPV4_IFADDR,
95                          RTNLGRP_IPV6_IFADDR);
96         if (r < 0)
97                 return r;
98
99         r = sd_bus_default_system(&m->bus);
100         if (r < 0 && r != -ENOENT) /* TODO: drop when we can rely on kdbus */
101                 return r;
102
103         r = setup_signals(m);
104         if (r < 0)
105                 return r;
106
107         /* udev does not initialize devices inside containers,
108          * so we rely on them being already initialized before
109          * entering the container */
110         if (detect_container(NULL) <= 0) {
111                 m->udev = udev_new();
112                 if (!m->udev)
113                         return -ENOMEM;
114
115                 m->udev_monitor = udev_monitor_new_from_netlink(m->udev, "udev");
116                 if (!m->udev_monitor)
117                         return -ENOMEM;
118         }
119
120         m->links = hashmap_new(uint64_hash_func, uint64_compare_func);
121         if (!m->links)
122                 return -ENOMEM;
123
124         m->netdevs = hashmap_new(string_hash_func, string_compare_func);
125         if (!m->netdevs)
126                 return -ENOMEM;
127
128         LIST_HEAD_INIT(m->networks);
129
130         *ret = m;
131         m = NULL;
132
133         return 0;
134 }
135
136 void manager_free(Manager *m) {
137         Network *network;
138         NetDev *netdev;
139         Link *link;
140
141         if (!m)
142                 return;
143
144         free(m->state_file);
145
146         udev_monitor_unref(m->udev_monitor);
147         udev_unref(m->udev);
148         sd_bus_unref(m->bus);
149         sd_event_source_unref(m->udev_event_source);
150         sd_event_source_unref(m->sigterm_event_source);
151         sd_event_source_unref(m->sigint_event_source);
152         sd_event_unref(m->event);
153
154         while ((link = hashmap_first(m->links)))
155                 link_unref(link);
156         hashmap_free(m->links);
157
158         while ((network = m->networks))
159                 network_free(network);
160
161         while ((netdev = hashmap_first(m->netdevs)))
162                 netdev_unref(netdev);
163         hashmap_free(m->netdevs);
164
165         sd_rtnl_unref(m->rtnl);
166
167         free(m);
168 }
169
170 int manager_load_config(Manager *m) {
171         int r;
172
173         /* update timestamp */
174         paths_check_timestamp(network_dirs, &m->network_dirs_ts_usec, true);
175
176         r = netdev_load(m);
177         if (r < 0)
178                 return r;
179
180         r = network_load(m);
181         if (r < 0)
182                 return r;
183
184         return 0;
185 }
186
187 bool manager_should_reload(Manager *m) {
188         return paths_check_timestamp(network_dirs, &m->network_dirs_ts_usec, false);
189 }
190
191 static int manager_udev_process_link(Manager *m, struct udev_device *device) {
192         Link *link = NULL;
193         int r, ifindex;
194
195         assert(m);
196         assert(device);
197
198         if (!streq_ptr(udev_device_get_action(device), "add"))
199                 return 0;
200
201         ifindex = udev_device_get_ifindex(device);
202         if (ifindex <= 0) {
203                 log_debug("ignoring udev ADD event for device with invalid ifindex");
204                 return 0;
205         }
206
207         r = link_get(m, ifindex, &link);
208         if (r == -ENODEV)
209                 return 0;
210         else if (r < 0)
211                 return r;
212
213         r = link_initialized(link, device);
214         if (r < 0)
215                 return r;
216
217         return 0;
218 }
219
220 static int manager_rtnl_process_address(sd_rtnl *rtnl, sd_rtnl_message *message, void *userdata) {
221         Manager *m = userdata;
222         Link *link = NULL;
223         uint16_t type;
224         _cleanup_address_free_ Address *address = NULL;
225         char buf[INET6_ADDRSTRLEN];
226         int r, ifindex;
227
228         assert(rtnl);
229         assert(message);
230         assert(m);
231
232         r = sd_rtnl_message_get_type(message, &type);
233         if (r < 0) {
234                 log_warning("rtnl: could not get message type");
235                 return 0;
236         }
237
238         r = sd_rtnl_message_addr_get_ifindex(message, &ifindex);
239         if (r < 0 || ifindex <= 0) {
240                 log_warning("rtnl: received address message without valid ifindix, ignoring");
241                 return 0;
242         } else {
243                 r = link_get(m, ifindex, &link);
244                 if (r < 0 || !link) {
245                         log_warning("rtnl: received address for non-existing link, ignoring");
246                         return 0;
247                 }
248         }
249
250         r = address_new_dynamic(&address);
251         if (r < 0)
252                 return 0;
253
254         r = sd_rtnl_message_addr_get_family(message, &address->family);
255         if (r < 0 || !IN_SET(address->family, AF_INET, AF_INET6)) {
256                 log_warning("rtnl: received address with invalid family, ignoring");
257                 return 0;
258         }
259
260         r = sd_rtnl_message_addr_get_prefixlen(message, &address->prefixlen);
261         if (r < 0) {
262                 log_warning("rtnl: recevied address with invalid prefixlen, ignoring");
263                 return 0;
264         }
265
266         switch (address->family) {
267         case AF_INET:
268                 r = sd_rtnl_message_read_in_addr(message, IFA_LOCAL, &address->in_addr.in);
269                 if (r < 0) {
270                         log_warning("rtnl: received address without valid address, ignoring");
271                         return 0;
272                 }
273
274                 break;
275
276         case AF_INET6:
277                 r = sd_rtnl_message_read_in6_addr(message, IFA_ADDRESS, &address->in_addr.in6);
278                 if (r < 0) {
279                         log_warning("rtnl: received address without valid address, ignoring");
280                         return 0;
281                 }
282
283                 break;
284
285         default:
286                 assert_not_reached("invalid address family");
287         }
288
289         if (!inet_ntop(address->family, &address->in_addr, buf, INET6_ADDRSTRLEN)) {
290                 log_warning("could not print address");
291                 return 0;
292         }
293
294         switch (type) {
295         case RTM_NEWADDR:
296                 log_info("added address: %s/%u to ifindex %d", buf,
297                          address->prefixlen, ifindex);
298                 break;
299
300         case RTM_DELADDR:
301                 log_info("removed address: %s/%u from ifindex %d", buf,
302                          address->prefixlen, ifindex);
303                 break;
304         default:
305                 assert_not_reached("Received invalid RTNL message type");
306         }
307
308         return 1;
309 }
310
311 static int manager_rtnl_process_link(sd_rtnl *rtnl, sd_rtnl_message *message, void *userdata) {
312         Manager *m = userdata;
313         Link *link = NULL;
314         NetDev *netdev = NULL;
315         uint16_t type;
316         char *name;
317         int r, ifindex;
318
319         assert(rtnl);
320         assert(message);
321         assert(m);
322
323         r = sd_rtnl_message_get_type(message, &type);
324         if (r < 0) {
325                 log_warning("rtnl: could not get message type");
326                 return 0;
327         }
328
329         r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
330         if (r < 0 || ifindex <= 0) {
331                 log_warning("rtnl: received link message without valid ifindex");
332                 return 0;
333         } else
334                 link_get(m, ifindex, &link);
335
336         r = sd_rtnl_message_read_string(message, IFLA_IFNAME, &name);
337         if (r < 0 || !name) {
338                 log_warning("rtnl: received link message without valid ifname");
339                 return 0;
340         } else
341                 netdev_get(m, name, &netdev);
342
343         switch (type) {
344         case RTM_NEWLINK:
345                 if (!link) {
346                         /* link is new, so add it */
347                         r = link_add(m, message, &link);
348                         if (r < 0) {
349                                 log_debug("could not add new link");
350                                 return 0;
351                         }
352                 }
353
354                 if (netdev) {
355                         /* netdev exists, so make sure the ifindex matches */
356                         r = netdev_set_ifindex(netdev, message);
357                         if (r < 0) {
358                                 log_debug("could not set ifindex on netdev");
359                                 return 0;
360                         }
361                 }
362
363                 r = link_update(link, message);
364                 if (r < 0)
365                         return 0;
366
367                 break;
368
369         case RTM_DELLINK:
370                 link_drop(link);
371                 netdev_drop(netdev);
372
373                 break;
374
375         default:
376                 assert_not_reached("Received invalid RTNL message type.");
377         }
378
379         return 1;
380 }
381
382 int manager_rtnl_enumerate_links(Manager *m) {
383         _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL;
384         sd_rtnl_message *link;
385         int r, k;
386
387         assert(m);
388         assert(m->rtnl);
389
390         r = sd_rtnl_message_new_link(m->rtnl, &req, RTM_GETLINK, 0);
391         if (r < 0)
392                 return r;
393
394         r = sd_rtnl_message_request_dump(req, true);
395         if (r < 0)
396                 return r;
397
398         r = sd_rtnl_call(m->rtnl, req, 0, &reply);
399         if (r < 0)
400                 return r;
401
402         for (link = reply; link; link = sd_rtnl_message_next(link)) {
403                 uint16_t type;
404
405                 k = sd_rtnl_message_get_type(link, &type);
406                 if (k < 0)
407                         return k;
408
409                 if (type != RTM_NEWLINK)
410                         continue;
411
412                 k = manager_rtnl_process_link(m->rtnl, link, m);
413                 if (k < 0)
414                         r = k;
415         }
416
417         return r;
418 }
419
420 static int manager_dispatch_link_udev(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
421         Manager *m = userdata;
422         struct udev_monitor *monitor = m->udev_monitor;
423         _cleanup_udev_device_unref_ struct udev_device *device = NULL;
424
425         device = udev_monitor_receive_device(monitor);
426         if (!device)
427                 return -ENOMEM;
428
429         manager_udev_process_link(m, device);
430         return 0;
431 }
432
433 int manager_udev_listen(Manager *m) {
434         int r;
435
436         if (detect_container(NULL) > 0)
437                 return 0;
438
439         assert(m->udev_monitor);
440
441         r = udev_monitor_filter_add_match_subsystem_devtype(m->udev_monitor, "net", NULL);
442         if (r < 0) {
443                 log_error("Could not add udev monitor filter: %s", strerror(-r));
444                 return r;
445         }
446
447         r = udev_monitor_enable_receiving(m->udev_monitor);
448         if (r < 0) {
449                 log_error("Could not enable udev monitor");
450                 return r;
451         }
452
453         r = sd_event_add_io(m->event,
454                         &m->udev_event_source,
455                         udev_monitor_get_fd(m->udev_monitor),
456                         EPOLLIN, manager_dispatch_link_udev,
457                         m);
458         if (r < 0)
459                 return r;
460
461         return 0;
462 }
463
464 int manager_rtnl_listen(Manager *m) {
465         int r;
466
467         r = sd_rtnl_attach_event(m->rtnl, m->event, 0);
468         if (r < 0)
469                 return r;
470
471         r = sd_rtnl_add_match(m->rtnl, RTM_NEWLINK, &manager_rtnl_process_link, m);
472         if (r < 0)
473                 return r;
474
475         r = sd_rtnl_add_match(m->rtnl, RTM_DELLINK, &manager_rtnl_process_link, m);
476         if (r < 0)
477                 return r;
478
479         r = sd_rtnl_add_match(m->rtnl, RTM_NEWADDR, &manager_rtnl_process_address, m);
480         if (r < 0)
481                 return r;
482
483         r = sd_rtnl_add_match(m->rtnl, RTM_DELADDR, &manager_rtnl_process_address, m);
484         if (r < 0)
485                 return r;
486
487         return 0;
488 }
489
490 int manager_bus_listen(Manager *m) {
491         int r;
492
493         assert(m->event);
494
495         if (!m->bus) /* TODO: drop when we can rely on kdbus */
496                 return 0;
497
498         r = sd_bus_attach_event(m->bus, m->event, 0);
499         if (r < 0)
500                 return r;
501
502         return 0;
503 }
504
505 static void append_dns(FILE *f, struct in_addr *dns, unsigned char family, unsigned *count) {
506         char buf[INET6_ADDRSTRLEN];
507         const char *address;
508
509         address = inet_ntop(family, dns, buf, INET6_ADDRSTRLEN);
510         if (!address) {
511                 log_warning("Invalid DNS address. Ignoring.");
512                 return;
513         }
514
515         if (*count == MAXNS)
516                 fputs("# Too many DNS servers configured, the following entries "
517                       "will be ignored\n", f);
518
519         fprintf(f, "nameserver %s\n", address);
520
521         (*count) ++;
522 }
523
524 int manager_update_resolv_conf(Manager *m) {
525         _cleanup_free_ char *temp_path = NULL;
526         _cleanup_fclose_ FILE *f = NULL;
527         Link *link;
528         Iterator i;
529         unsigned count = 0;
530         const char *domainname = NULL;
531         int r;
532
533         assert(m);
534
535         r = fopen_temporary("/run/systemd/network/resolv.conf", &f, &temp_path);
536         if (r < 0)
537                 return r;
538
539         fchmod(fileno(f), 0644);
540
541         fputs("# This file is managed by systemd-networkd(8). Do not edit.\n#\n"
542               "# Third party programs must not access this file directly, but\n"
543               "# only through the symlink at /etc/resolv.conf. To manage\n"
544               "# resolv.conf(5) in a different way, replace the symlink by a\n"
545               "# static file or a different symlink.\n\n", f);
546
547         HASHMAP_FOREACH(link, m->links, i) {
548                 if (link->dhcp_lease) {
549                         struct in_addr *nameservers;
550                         size_t nameservers_size;
551
552                         if (link->network->dhcp_dns) {
553                                 r = sd_dhcp_lease_get_dns(link->dhcp_lease, &nameservers, &nameservers_size);
554                                 if (r >= 0) {
555                                         unsigned j;
556
557                                         for (j = 0; j < nameservers_size; j++)
558                                                 append_dns(f, &nameservers[j], AF_INET, &count);
559                                 }
560                         }
561
562                         if (link->network->dhcp_domainname && !domainname) {
563                                 r = sd_dhcp_lease_get_domainname(link->dhcp_lease, &domainname);
564                                 if (r >= 0)
565                                        fprintf(f, "domain %s\n", domainname);
566                         }
567                 }
568         }
569
570         HASHMAP_FOREACH(link, m->links, i) {
571                 if (link->network && link->network->dns) {
572                         Address *address;
573                         Iterator j;
574
575                         SET_FOREACH(address, link->network->dns, j) {
576                                 append_dns(f, &address->in_addr.in,
577                                            address->family, &count);
578                         }
579                 }
580         }
581
582         fflush(f);
583
584         if (ferror(f) || rename(temp_path, "/run/systemd/network/resolv.conf") < 0) {
585                 r = -errno;
586                 unlink("/run/systemd/network/resolv.conf");
587                 unlink(temp_path);
588                 return r;
589         }
590
591         return 0;
592 }
593
594 int manager_save(Manager *m) {
595         Link *link;
596         Iterator i;
597         _cleanup_free_ char *temp_path = NULL;
598         _cleanup_fclose_ FILE *f = NULL;
599         const char *oper_state = "unknown";
600         bool dormant = false, carrier = false;
601         int r;
602
603         assert(m);
604         assert(m->state_file);
605
606         HASHMAP_FOREACH(link, m->links, i) {
607                 if (link->flags & IFF_LOOPBACK)
608                         continue;
609
610                 if (link_has_carrier(link->flags, link->operstate))
611                         carrier = true;
612                 else if (link->operstate == IF_OPER_DORMANT)
613                         dormant = true;
614         }
615
616         if (carrier)
617                 oper_state = "carrier";
618         else if (dormant)
619                 oper_state = "dormant";
620
621         r = fopen_temporary(m->state_file, &f, &temp_path);
622         if (r < 0)
623                 goto finish;
624
625         fchmod(fileno(f), 0644);
626
627         fprintf(f,
628                 "# This is private data. Do not parse.\n"
629                 "OPER_STATE=%s\n", oper_state);
630
631         fflush(f);
632
633         if (ferror(f) || rename(temp_path, m->state_file) < 0) {
634                 r = -errno;
635                 unlink(m->state_file);
636                 unlink(temp_path);
637         }
638
639 finish:
640         if (r < 0)
641                 log_error("Failed to save network state to %s: %s", m->state_file, strerror(-r));
642
643         return r;
644 }