]> git.proxmox.com Git - systemd.git/blob - src/core/loopback-setup.c
Merge tag 'upstream/229'
[systemd.git] / src / core / loopback-setup.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <net/if.h>
21 #include <stdlib.h>
22
23 #include "sd-netlink.h"
24
25 #include "loopback-setup.h"
26 #include "missing.h"
27 #include "netlink-util.h"
28
29 static int start_loopback(sd_netlink *rtnl) {
30 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
31 int r;
32
33 r = sd_rtnl_message_new_link(rtnl, &req, RTM_SETLINK, LOOPBACK_IFINDEX);
34 if (r < 0)
35 return r;
36
37 r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP);
38 if (r < 0)
39 return r;
40
41 r = sd_netlink_call(rtnl, req, 0, NULL);
42 if (r < 0)
43 return r;
44
45 return 0;
46 }
47
48 static bool check_loopback(sd_netlink *rtnl) {
49 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
50 unsigned flags;
51 int r;
52
53 r = sd_rtnl_message_new_link(rtnl, &req, RTM_GETLINK, LOOPBACK_IFINDEX);
54 if (r < 0)
55 return false;
56
57 r = sd_netlink_call(rtnl, req, 0, &reply);
58 if (r < 0)
59 return false;
60
61 r = sd_rtnl_message_link_get_flags(reply, &flags);
62 if (r < 0)
63 return false;
64
65 return flags & IFF_UP;
66 }
67
68 int loopback_setup(void) {
69 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
70 int r;
71
72 r = sd_netlink_open(&rtnl);
73 if (r < 0)
74 return r;
75
76 r = start_loopback(rtnl);
77 if (r < 0) {
78
79 /* If we lack the permissions to configure the
80 * loopback device, but we find it to be already
81 * configured, let's exit cleanly, in order to
82 * supported unprivileged containers. */
83 if (r == -EPERM && check_loopback(rtnl))
84 return 0;
85
86 return log_warning_errno(r, "Failed to configure loopback device: %m");
87 }
88
89 return 0;
90 }