]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-manager.c
Merge pull request #1668 from ssahani/net1
[thirdparty/systemd.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 <sys/socket.h>
23 #include <linux/if.h>
24
25 #include "sd-daemon.h"
26 #include "sd-netlink.h"
27
28 #include "bus-util.h"
29 #include "conf-parser.h"
30 #include "def.h"
31 #include "fd-util.h"
32 #include "libudev-private.h"
33 #include "local-addresses.h"
34 #include "netlink-util.h"
35 #include "networkd.h"
36 #include "path-util.h"
37 #include "set.h"
38 #include "udev-util.h"
39 #include "virt.h"
40
41 /* use 8 MB for receive socket kernel queue. */
42 #define RCVBUF_SIZE (8*1024*1024)
43
44 const char* const network_dirs[] = {
45 "/etc/systemd/network",
46 "/run/systemd/network",
47 "/usr/lib/systemd/network",
48 #ifdef HAVE_SPLIT_USR
49 "/lib/systemd/network",
50 #endif
51 NULL};
52
53 static int setup_default_address_pool(Manager *m) {
54 AddressPool *p;
55 int r;
56
57 assert(m);
58
59 /* Add in the well-known private address ranges. */
60
61 r = address_pool_new_from_string(m, &p, AF_INET6, "fc00::", 7);
62 if (r < 0)
63 return r;
64
65 r = address_pool_new_from_string(m, &p, AF_INET, "192.168.0.0", 16);
66 if (r < 0)
67 return r;
68
69 r = address_pool_new_from_string(m, &p, AF_INET, "172.16.0.0", 12);
70 if (r < 0)
71 return r;
72
73 r = address_pool_new_from_string(m, &p, AF_INET, "10.0.0.0", 8);
74 if (r < 0)
75 return r;
76
77 return 0;
78 }
79
80 static int on_bus_retry(sd_event_source *s, usec_t usec, void *userdata) {
81 Manager *m = userdata;
82
83 assert(s);
84 assert(m);
85
86 m->bus_retry_event_source = sd_event_source_unref(m->bus_retry_event_source);
87
88 manager_connect_bus(m);
89
90 return 0;
91 }
92
93 static int manager_reset_all(Manager *m) {
94 Link *link;
95 Iterator i;
96 int r;
97
98 assert(m);
99
100 HASHMAP_FOREACH(link, m->links, i) {
101 r = link_carrier_reset(link);
102 if (r < 0)
103 log_link_warning_errno(link, r, "Could not reset carrier: %m");
104 }
105
106 return 0;
107 }
108
109 static int match_prepare_for_sleep(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) {
110 Manager *m = userdata;
111 int b, r;
112
113 assert(message);
114
115 r = sd_bus_message_read(message, "b", &b);
116 if (r < 0) {
117 log_debug_errno(r, "Failed to parse PrepareForSleep signal: %m");
118 return 0;
119 }
120
121 if (b)
122 return 0;
123
124 log_debug("Coming back from suspend, resetting all connections...");
125
126 manager_reset_all(m);
127
128 return 0;
129 }
130
131 int manager_connect_bus(Manager *m) {
132 int r;
133
134 assert(m);
135
136 r = sd_bus_default_system(&m->bus);
137 if (r == -ENOENT) {
138 /* We failed to connect? Yuck, we must be in early
139 * boot. Let's try in 5s again. As soon as we have
140 * kdbus we can stop doing this... */
141
142 log_debug_errno(r, "Failed to connect to bus, trying again in 5s: %m");
143
144 r = sd_event_add_time(m->event, &m->bus_retry_event_source, CLOCK_MONOTONIC, now(CLOCK_MONOTONIC) + 5*USEC_PER_SEC, 0, on_bus_retry, m);
145 if (r < 0)
146 return log_error_errno(r, "Failed to install bus reconnect time event: %m");
147
148 return 0;
149 }
150
151 if (r < 0)
152 return r;
153
154 r = sd_bus_add_match(m->bus, &m->prepare_for_sleep_slot,
155 "type='signal',"
156 "sender='org.freedesktop.login1',"
157 "interface='org.freedesktop.login1.Manager',"
158 "member='PrepareForSleep',"
159 "path='/org/freedesktop/login1'",
160 match_prepare_for_sleep,
161 m);
162 if (r < 0)
163 return log_error_errno(r, "Failed to add match for PrepareForSleep: %m");
164
165 r = sd_bus_add_object_vtable(m->bus, NULL, "/org/freedesktop/network1", "org.freedesktop.network1.Manager", manager_vtable, m);
166 if (r < 0)
167 return log_error_errno(r, "Failed to add manager object vtable: %m");
168
169 r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/network1/link", "org.freedesktop.network1.Link", link_vtable, link_object_find, m);
170 if (r < 0)
171 return log_error_errno(r, "Failed to add link object vtable: %m");
172
173 r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/network1/link", link_node_enumerator, m);
174 if (r < 0)
175 return log_error_errno(r, "Failed to add link enumerator: %m");
176
177 r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/network1/network", "org.freedesktop.network1.Network", network_vtable, network_object_find, m);
178 if (r < 0)
179 return log_error_errno(r, "Failed to add network object vtable: %m");
180
181 r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/network1/network", network_node_enumerator, m);
182 if (r < 0)
183 return log_error_errno(r, "Failed to add network enumerator: %m");
184
185 r = sd_bus_request_name(m->bus, "org.freedesktop.network1", 0);
186 if (r < 0)
187 return log_error_errno(r, "Failed to register name: %m");
188
189 r = sd_bus_attach_event(m->bus, m->event, 0);
190 if (r < 0)
191 return log_error_errno(r, "Failed to attach bus to event loop: %m");
192
193 return 0;
194 }
195
196 static int manager_udev_process_link(Manager *m, struct udev_device *device) {
197 Link *link = NULL;
198 int r, ifindex;
199
200 assert(m);
201 assert(device);
202
203 if (!streq_ptr(udev_device_get_action(device), "add"))
204 return 0;
205
206 ifindex = udev_device_get_ifindex(device);
207 if (ifindex <= 0) {
208 log_debug("Ignoring udev ADD event for device with invalid ifindex");
209 return 0;
210 }
211
212 r = link_get(m, ifindex, &link);
213 if (r == -ENODEV)
214 return 0;
215 else if (r < 0)
216 return r;
217
218 r = link_initialized(link, device);
219 if (r < 0)
220 return r;
221
222 return 0;
223 }
224
225 static int manager_dispatch_link_udev(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
226 Manager *m = userdata;
227 struct udev_monitor *monitor = m->udev_monitor;
228 _cleanup_udev_device_unref_ struct udev_device *device = NULL;
229
230 device = udev_monitor_receive_device(monitor);
231 if (!device)
232 return -ENOMEM;
233
234 manager_udev_process_link(m, device);
235 return 0;
236 }
237
238 static int manager_connect_udev(Manager *m) {
239 int r;
240
241 /* udev does not initialize devices inside containers,
242 * so we rely on them being already initialized before
243 * entering the container */
244 if (detect_container() > 0)
245 return 0;
246
247 m->udev = udev_new();
248 if (!m->udev)
249 return -ENOMEM;
250
251 m->udev_monitor = udev_monitor_new_from_netlink(m->udev, "udev");
252 if (!m->udev_monitor)
253 return -ENOMEM;
254
255 r = udev_monitor_filter_add_match_subsystem_devtype(m->udev_monitor, "net", NULL);
256 if (r < 0)
257 return log_error_errno(r, "Could not add udev monitor filter: %m");
258
259 r = udev_monitor_enable_receiving(m->udev_monitor);
260 if (r < 0) {
261 log_error("Could not enable udev monitor");
262 return r;
263 }
264
265 r = sd_event_add_io(m->event,
266 &m->udev_event_source,
267 udev_monitor_get_fd(m->udev_monitor),
268 EPOLLIN, manager_dispatch_link_udev,
269 m);
270 if (r < 0)
271 return r;
272
273 r = sd_event_source_set_description(m->udev_event_source, "networkd-udev");
274 if (r < 0)
275 return r;
276
277 return 0;
278 }
279
280 int manager_rtnl_process_address(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) {
281 Manager *m = userdata;
282 Link *link = NULL;
283 uint16_t type;
284 unsigned char flags;
285 int family;
286 unsigned char prefixlen;
287 unsigned char scope;
288 union in_addr_union in_addr;
289 struct ifa_cacheinfo cinfo;
290 Address *address = NULL;
291 char buf[INET6_ADDRSTRLEN], valid_buf[FORMAT_TIMESPAN_MAX];
292 const char *valid_str = NULL;
293 int r, ifindex;
294
295 assert(rtnl);
296 assert(message);
297 assert(m);
298
299 if (sd_netlink_message_is_error(message)) {
300 r = sd_netlink_message_get_errno(message);
301 if (r < 0)
302 log_warning_errno(r, "rtnl: failed to receive address: %m");
303
304 return 0;
305 }
306
307 r = sd_netlink_message_get_type(message, &type);
308 if (r < 0) {
309 log_warning_errno(r, "rtnl: could not get message type: %m");
310 return 0;
311 } else if (type != RTM_NEWADDR && type != RTM_DELADDR) {
312 log_warning("rtnl: received unexpected message type when processing address");
313 return 0;
314 }
315
316 r = sd_rtnl_message_addr_get_ifindex(message, &ifindex);
317 if (r < 0) {
318 log_warning_errno(r, "rtnl: could not get ifindex from address: %m");
319 return 0;
320 } else if (ifindex <= 0) {
321 log_warning("rtnl: received address message with invalid ifindex: %d", ifindex);
322 return 0;
323 } else {
324 r = link_get(m, ifindex, &link);
325 if (r < 0 || !link) {
326 /* when enumerating we might be out of sync, but we will
327 * get the address again, so just ignore it */
328 if (!m->enumerating)
329 log_warning("rtnl: received address for nonexistent link (%d), ignoring", ifindex);
330 return 0;
331 }
332 }
333
334 r = sd_rtnl_message_addr_get_family(message, &family);
335 if (r < 0 || !IN_SET(family, AF_INET, AF_INET6)) {
336 log_link_warning(link, "rtnl: received address with invalid family, ignoring.");
337 return 0;
338 }
339
340 r = sd_rtnl_message_addr_get_prefixlen(message, &prefixlen);
341 if (r < 0) {
342 log_link_warning_errno(link, r, "rtnl: received address with invalid prefixlen, ignoring: %m");
343 return 0;
344 }
345
346 r = sd_rtnl_message_addr_get_scope(message, &scope);
347 if (r < 0) {
348 log_link_warning_errno(link, r, "rtnl: received address with invalid scope, ignoring: %m");
349 return 0;
350 }
351
352 r = sd_rtnl_message_addr_get_flags(message, &flags);
353 if (r < 0) {
354 log_link_warning_errno(link, r, "rtnl: received address with invalid flags, ignoring: %m");
355 return 0;
356 }
357
358 switch (family) {
359 case AF_INET:
360 r = sd_netlink_message_read_in_addr(message, IFA_LOCAL, &in_addr.in);
361 if (r < 0) {
362 log_link_warning_errno(link, r, "rtnl: received address without valid address, ignoring: %m");
363 return 0;
364 }
365
366 break;
367
368 case AF_INET6:
369 r = sd_netlink_message_read_in6_addr(message, IFA_ADDRESS, &in_addr.in6);
370 if (r < 0) {
371 log_link_warning_errno(link, r, "rtnl: received address without valid address, ignoring: %m");
372 return 0;
373 }
374
375 break;
376
377 default:
378 assert_not_reached("invalid address family");
379 }
380
381 if (!inet_ntop(family, &in_addr, buf, INET6_ADDRSTRLEN)) {
382 log_link_warning(link, "Could not print address");
383 return 0;
384 }
385
386 r = sd_netlink_message_read_cache_info(message, IFA_CACHEINFO, &cinfo);
387 if (r >= 0) {
388 if (cinfo.ifa_valid == CACHE_INFO_INFINITY_LIFE_TIME)
389 valid_str = "ever";
390 else
391 valid_str = format_timespan(valid_buf, FORMAT_TIMESPAN_MAX,
392 cinfo.ifa_valid * USEC_PER_SEC,
393 USEC_PER_SEC);
394 }
395
396 address_get(link, family, &in_addr, prefixlen, &address);
397
398 switch (type) {
399 case RTM_NEWADDR:
400 if (address)
401 log_link_debug(link, "Updating address: %s/%u (valid for %s)", buf, prefixlen, valid_str);
402 else {
403 /* An address appeared that we did not request */
404 r = address_add_foreign(link, family, &in_addr, prefixlen, &address);
405 if (r < 0) {
406 log_link_warning_errno(link, r, "Failed to add address %s/%u: %m", buf, prefixlen);
407 return 0;
408 } else
409 log_link_debug(link, "Adding address: %s/%u (valid for %s)", buf, prefixlen, valid_str);
410 }
411
412 address_update(address, flags, scope, &cinfo);
413
414 break;
415
416 case RTM_DELADDR:
417
418 if (address) {
419 log_link_debug(link, "Removing address: %s/%u (valid for %s)", buf, prefixlen, valid_str);
420 address_drop(address);
421 } else
422 log_link_warning(link, "Removing non-existent address: %s/%u (valid for %s)", buf, prefixlen, valid_str);
423
424 break;
425 default:
426 assert_not_reached("Received invalid RTNL message type");
427 }
428
429 return 1;
430 }
431
432 static int manager_rtnl_process_link(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) {
433 Manager *m = userdata;
434 Link *link = NULL;
435 NetDev *netdev = NULL;
436 uint16_t type;
437 const char *name;
438 int r, ifindex;
439
440 assert(rtnl);
441 assert(message);
442 assert(m);
443
444 if (sd_netlink_message_is_error(message)) {
445 r = sd_netlink_message_get_errno(message);
446 if (r < 0)
447 log_warning_errno(r, "rtnl: Could not receive link: %m");
448
449 return 0;
450 }
451
452 r = sd_netlink_message_get_type(message, &type);
453 if (r < 0) {
454 log_warning_errno(r, "rtnl: Could not get message type: %m");
455 return 0;
456 } else if (type != RTM_NEWLINK && type != RTM_DELLINK) {
457 log_warning("rtnl: Received unexpected message type when processing link");
458 return 0;
459 }
460
461 r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
462 if (r < 0) {
463 log_warning_errno(r, "rtnl: Could not get ifindex from link: %m");
464 return 0;
465 } else if (ifindex <= 0) {
466 log_warning("rtnl: received link message with invalid ifindex: %d", ifindex);
467 return 0;
468 } else
469 link_get(m, ifindex, &link);
470
471 r = sd_netlink_message_read_string(message, IFLA_IFNAME, &name);
472 if (r < 0) {
473 log_warning_errno(r, "rtnl: Received link message without ifname: %m");
474 return 0;
475 } else
476 netdev_get(m, name, &netdev);
477
478 switch (type) {
479 case RTM_NEWLINK:
480 if (!link) {
481 /* link is new, so add it */
482 r = link_add(m, message, &link);
483 if (r < 0) {
484 log_warning_errno(r, "Could not add new link: %m");
485 return 0;
486 }
487 }
488
489 if (netdev) {
490 /* netdev exists, so make sure the ifindex matches */
491 r = netdev_set_ifindex(netdev, message);
492 if (r < 0) {
493 log_warning_errno(r, "Could not set ifindex on netdev: %m");
494 return 0;
495 }
496 }
497
498 r = link_update(link, message);
499 if (r < 0)
500 return 0;
501
502 break;
503
504 case RTM_DELLINK:
505 link_drop(link);
506 netdev_drop(netdev);
507
508 break;
509
510 default:
511 assert_not_reached("Received invalid RTNL message type.");
512 }
513
514 return 1;
515 }
516
517 static int systemd_netlink_fd(void) {
518 int n, fd, rtnl_fd = -EINVAL;
519
520 n = sd_listen_fds(true);
521 if (n <= 0)
522 return -EINVAL;
523
524 for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) {
525 if (sd_is_socket(fd, AF_NETLINK, SOCK_RAW, -1) > 0) {
526 if (rtnl_fd >= 0)
527 return -EINVAL;
528
529 rtnl_fd = fd;
530 }
531 }
532
533 return rtnl_fd;
534 }
535
536 static int manager_connect_rtnl(Manager *m) {
537 int fd, r;
538
539 assert(m);
540
541 fd = systemd_netlink_fd();
542 if (fd < 0)
543 r = sd_netlink_open(&m->rtnl);
544 else
545 r = sd_netlink_open_fd(&m->rtnl, fd);
546 if (r < 0)
547 return r;
548
549 r = sd_netlink_inc_rcvbuf(m->rtnl, RCVBUF_SIZE);
550 if (r < 0)
551 return r;
552
553 r = sd_netlink_attach_event(m->rtnl, m->event, 0);
554 if (r < 0)
555 return r;
556
557 r = sd_netlink_add_match(m->rtnl, RTM_NEWLINK, &manager_rtnl_process_link, m);
558 if (r < 0)
559 return r;
560
561 r = sd_netlink_add_match(m->rtnl, RTM_DELLINK, &manager_rtnl_process_link, m);
562 if (r < 0)
563 return r;
564
565 r = sd_netlink_add_match(m->rtnl, RTM_NEWADDR, &manager_rtnl_process_address, m);
566 if (r < 0)
567 return r;
568
569 r = sd_netlink_add_match(m->rtnl, RTM_DELADDR, &manager_rtnl_process_address, m);
570 if (r < 0)
571 return r;
572
573 return 0;
574 }
575
576 static int set_put_in_addr(Set *s, const struct in_addr *address) {
577 char *p;
578 int r;
579
580 assert(s);
581
582 r = in_addr_to_string(AF_INET, (const union in_addr_union*) address, &p);
583 if (r < 0)
584 return r;
585
586 r = set_consume(s, p);
587 if (r == -EEXIST)
588 return 0;
589
590 return r;
591 }
592
593 static int set_put_in_addrv(Set *s, const struct in_addr *addresses, int n) {
594 int r, i, c = 0;
595
596 assert(s);
597 assert(n <= 0 || addresses);
598
599 for (i = 0; i < n; i++) {
600 r = set_put_in_addr(s, addresses+i);
601 if (r < 0)
602 return r;
603
604 c += r;
605 }
606
607 return c;
608 }
609
610 static void print_string_set(FILE *f, const char *field, Set *s) {
611 bool space = false;
612 Iterator i;
613 char *p;
614
615 if (set_isempty(s))
616 return;
617
618 fputs(field, f);
619
620 SET_FOREACH(p, s, i) {
621 if (space)
622 fputc(' ', f);
623 fputs(p, f);
624 space = true;
625 }
626 fputc('\n', f);
627 }
628
629 static int manager_save(Manager *m) {
630 _cleanup_set_free_free_ Set *dns = NULL, *ntp = NULL, *domains = NULL;
631 Link *link;
632 Iterator i;
633 _cleanup_free_ char *temp_path = NULL;
634 _cleanup_fclose_ FILE *f = NULL;
635 LinkOperationalState operstate = LINK_OPERSTATE_OFF;
636 const char *operstate_str;
637 int r;
638
639 assert(m);
640 assert(m->state_file);
641
642 /* We add all NTP and DNS server to a set, to filter out duplicates */
643 dns = set_new(&string_hash_ops);
644 if (!dns)
645 return -ENOMEM;
646
647 ntp = set_new(&string_hash_ops);
648 if (!ntp)
649 return -ENOMEM;
650
651 domains = set_new(&string_hash_ops);
652 if (!domains)
653 return -ENOMEM;
654
655 HASHMAP_FOREACH(link, m->links, i) {
656 if (link->flags & IFF_LOOPBACK)
657 continue;
658
659 if (link->operstate > operstate)
660 operstate = link->operstate;
661
662 if (!link->network)
663 continue;
664
665 /* First add the static configured entries */
666 r = set_put_strdupv(dns, link->network->dns);
667 if (r < 0)
668 return r;
669
670 r = set_put_strdupv(ntp, link->network->ntp);
671 if (r < 0)
672 return r;
673
674 r = set_put_strdupv(domains, link->network->domains);
675 if (r < 0)
676 return r;
677
678 if (!link->dhcp_lease)
679 continue;
680
681 /* Secondly, add the entries acquired via DHCP */
682 if (link->network->dhcp_dns) {
683 const struct in_addr *addresses;
684
685 r = sd_dhcp_lease_get_dns(link->dhcp_lease, &addresses);
686 if (r > 0) {
687 r = set_put_in_addrv(dns, addresses, r);
688 if (r < 0)
689 return r;
690 } else if (r < 0 && r != -ENODATA)
691 return r;
692 }
693
694 if (link->network->dhcp_ntp) {
695 const struct in_addr *addresses;
696
697 r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &addresses);
698 if (r > 0) {
699 r = set_put_in_addrv(ntp, addresses, r);
700 if (r < 0)
701 return r;
702 } else if (r < 0 && r != -ENODATA)
703 return r;
704 }
705
706 if (link->network->dhcp_domains) {
707 const char *domainname;
708
709 r = sd_dhcp_lease_get_domainname(link->dhcp_lease, &domainname);
710 if (r >= 0) {
711 r = set_put_strdup(domains, domainname);
712 if (r < 0)
713 return r;
714 } else if (r != -ENODATA)
715 return r;
716 }
717 }
718
719 operstate_str = link_operstate_to_string(operstate);
720 assert(operstate_str);
721
722 r = fopen_temporary(m->state_file, &f, &temp_path);
723 if (r < 0)
724 return r;
725
726 fchmod(fileno(f), 0644);
727
728 fprintf(f,
729 "# This is private data. Do not parse.\n"
730 "OPER_STATE=%s\n", operstate_str);
731
732 print_string_set(f, "DNS=", dns);
733 print_string_set(f, "NTP=", ntp);
734 print_string_set(f, "DOMAINS=", domains);
735
736 r = fflush_and_check(f);
737 if (r < 0)
738 goto fail;
739
740 if (rename(temp_path, m->state_file) < 0) {
741 r = -errno;
742 goto fail;
743 }
744
745 if (m->operational_state != operstate) {
746 m->operational_state = operstate;
747 r = manager_send_changed(m, "OperationalState", NULL);
748 if (r < 0)
749 log_error_errno(r, "Could not emit changed OperationalState: %m");
750 }
751
752 m->dirty = false;
753
754 return 0;
755
756 fail:
757 (void) unlink(m->state_file);
758 (void) unlink(temp_path);
759
760 return log_error_errno(r, "Failed to save network state to %s: %m", m->state_file);
761 }
762
763 static int manager_dirty_handler(sd_event_source *s, void *userdata) {
764 Manager *m = userdata;
765 Link *link;
766 Iterator i;
767 int r;
768
769 assert(m);
770
771 if (m->dirty)
772 manager_save(m);
773
774 SET_FOREACH(link, m->dirty_links, i) {
775 r = link_save(link);
776 if (r >= 0)
777 link_clean(link);
778 }
779
780 return 1;
781 }
782
783 int manager_new(Manager **ret) {
784 _cleanup_manager_free_ Manager *m = NULL;
785 int r;
786
787 m = new0(Manager, 1);
788 if (!m)
789 return -ENOMEM;
790
791 m->state_file = strdup("/run/systemd/netif/state");
792 if (!m->state_file)
793 return -ENOMEM;
794
795 r = sd_event_default(&m->event);
796 if (r < 0)
797 return r;
798
799 sd_event_set_watchdog(m->event, true);
800
801 sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL);
802 sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL);
803
804 r = sd_event_add_post(m->event, NULL, manager_dirty_handler, m);
805 if (r < 0)
806 return r;
807
808 r = manager_connect_rtnl(m);
809 if (r < 0)
810 return r;
811
812 r = manager_connect_udev(m);
813 if (r < 0)
814 return r;
815
816 m->netdevs = hashmap_new(&string_hash_ops);
817 if (!m->netdevs)
818 return -ENOMEM;
819
820 LIST_HEAD_INIT(m->networks);
821
822 r = setup_default_address_pool(m);
823 if (r < 0)
824 return r;
825
826 *ret = m;
827 m = NULL;
828
829 return 0;
830 }
831
832 void manager_free(Manager *m) {
833 Network *network;
834 NetDev *netdev;
835 Link *link;
836 AddressPool *pool;
837
838 if (!m)
839 return;
840
841 free(m->state_file);
842
843 while ((link = hashmap_first(m->links)))
844 link_unref(link);
845 hashmap_free(m->links);
846
847 while ((network = m->networks))
848 network_free(network);
849
850 hashmap_free(m->networks_by_name);
851
852 while ((netdev = hashmap_first(m->netdevs)))
853 netdev_unref(netdev);
854 hashmap_free(m->netdevs);
855
856 while ((pool = m->address_pools))
857 address_pool_free(pool);
858
859 sd_netlink_unref(m->rtnl);
860 sd_event_unref(m->event);
861
862 sd_event_source_unref(m->udev_event_source);
863 udev_monitor_unref(m->udev_monitor);
864 udev_unref(m->udev);
865
866 sd_bus_unref(m->bus);
867 sd_bus_slot_unref(m->prepare_for_sleep_slot);
868 sd_event_source_unref(m->bus_retry_event_source);
869
870 free(m);
871 }
872
873 static bool manager_check_idle(void *userdata) {
874 Manager *m = userdata;
875 Link *link;
876 Iterator i;
877
878 assert(m);
879
880 HASHMAP_FOREACH(link, m->links, i) {
881 /* we are not woken on udev activity, so let's just wait for the
882 * pending udev event */
883 if (link->state == LINK_STATE_PENDING)
884 return false;
885
886 if (!link->network)
887 continue;
888
889 /* we are not woken on netork activity, so let's stay around */
890 if (link_lldp_enabled(link) ||
891 link_ipv4ll_enabled(link) ||
892 link_dhcp4_server_enabled(link) ||
893 link_dhcp4_enabled(link) ||
894 link_dhcp6_enabled(link))
895 return false;
896 }
897
898 return true;
899 }
900
901 int manager_run(Manager *m) {
902 Link *link;
903 Iterator i;
904
905 assert(m);
906
907 /* The dirty handler will deal with future serialization, but the first one
908 must be done explicitly. */
909
910 manager_save(m);
911
912 HASHMAP_FOREACH(link, m->links, i)
913 link_save(link);
914
915 if (m->bus)
916 return bus_event_loop_with_idle(
917 m->event,
918 m->bus,
919 "org.freedesktop.network1",
920 DEFAULT_EXIT_USEC,
921 manager_check_idle,
922 m);
923 else
924 /* failed to connect to the bus, so we lose exit-on-idle logic,
925 this should not happen except if dbus is not around at all */
926 return sd_event_loop(m->event);
927 }
928
929 int manager_load_config(Manager *m) {
930 int r;
931
932 /* update timestamp */
933 paths_check_timestamp(network_dirs, &m->network_dirs_ts_usec, true);
934
935 r = netdev_load(m);
936 if (r < 0)
937 return r;
938
939 r = network_load(m);
940 if (r < 0)
941 return r;
942
943 return 0;
944 }
945
946 bool manager_should_reload(Manager *m) {
947 return paths_check_timestamp(network_dirs, &m->network_dirs_ts_usec, false);
948 }
949
950 int manager_rtnl_enumerate_links(Manager *m) {
951 _cleanup_netlink_message_unref_ sd_netlink_message *req = NULL, *reply = NULL;
952 sd_netlink_message *link;
953 int r;
954
955 assert(m);
956 assert(m->rtnl);
957
958 r = sd_rtnl_message_new_link(m->rtnl, &req, RTM_GETLINK, 0);
959 if (r < 0)
960 return r;
961
962 r = sd_netlink_message_request_dump(req, true);
963 if (r < 0)
964 return r;
965
966 r = sd_netlink_call(m->rtnl, req, 0, &reply);
967 if (r < 0)
968 return r;
969
970 for (link = reply; link; link = sd_netlink_message_next(link)) {
971 int k;
972
973 m->enumerating = true;
974
975 k = manager_rtnl_process_link(m->rtnl, link, m);
976 if (k < 0)
977 r = k;
978
979 m->enumerating = false;
980 }
981
982 return r;
983 }
984
985 int manager_rtnl_enumerate_addresses(Manager *m) {
986 _cleanup_netlink_message_unref_ sd_netlink_message *req = NULL, *reply = NULL;
987 sd_netlink_message *addr;
988 int r;
989
990 assert(m);
991 assert(m->rtnl);
992
993 r = sd_rtnl_message_new_addr(m->rtnl, &req, RTM_GETADDR, 0, 0);
994 if (r < 0)
995 return r;
996
997 r = sd_netlink_message_request_dump(req, true);
998 if (r < 0)
999 return r;
1000
1001 r = sd_netlink_call(m->rtnl, req, 0, &reply);
1002 if (r < 0)
1003 return r;
1004
1005 for (addr = reply; addr; addr = sd_netlink_message_next(addr)) {
1006 int k;
1007
1008 m->enumerating = true;
1009
1010 k = manager_rtnl_process_address(m->rtnl, addr, m);
1011 if (k < 0)
1012 r = k;
1013
1014 m->enumerating = false;
1015 }
1016
1017 return r;
1018 }
1019
1020 int manager_address_pool_acquire(Manager *m, int family, unsigned prefixlen, union in_addr_union *found) {
1021 AddressPool *p;
1022 int r;
1023
1024 assert(m);
1025 assert(prefixlen > 0);
1026 assert(found);
1027
1028 LIST_FOREACH(address_pools, p, m->address_pools) {
1029 if (p->family != family)
1030 continue;
1031
1032 r = address_pool_acquire(p, prefixlen, found);
1033 if (r != 0)
1034 return r;
1035 }
1036
1037 return 0;
1038 }
1039
1040 Link* manager_find_uplink(Manager *m, Link *exclude) {
1041 _cleanup_free_ struct local_address *gateways = NULL;
1042 int n, i;
1043
1044 assert(m);
1045
1046 /* Looks for a suitable "uplink", via black magic: an
1047 * interface that is up and where the default route with the
1048 * highest priority points to. */
1049
1050 n = local_gateways(m->rtnl, 0, AF_UNSPEC, &gateways);
1051 if (n < 0) {
1052 log_warning_errno(n, "Failed to determine list of default gateways: %m");
1053 return NULL;
1054 }
1055
1056 for (i = 0; i < n; i++) {
1057 Link *link;
1058
1059 link = hashmap_get(m->links, INT_TO_PTR(gateways[i].ifindex));
1060 if (!link) {
1061 log_debug("Weird, found a gateway for a link we don't know. Ignoring.");
1062 continue;
1063 }
1064
1065 if (link == exclude)
1066 continue;
1067
1068 if (link->operstate < LINK_OPERSTATE_ROUTABLE)
1069 continue;
1070
1071 return link;
1072 }
1073
1074 return NULL;
1075 }
1076
1077 void manager_dirty(Manager *manager) {
1078 assert(manager);
1079
1080 /* the serialized state in /run is no longer up-to-date */
1081 manager->dirty = true;
1082 }