]> git.proxmox.com Git - systemd.git/blame - src/core/loopback-setup.c
Imported Upstream version 229
[systemd.git] / src / core / loopback-setup.c
CommitLineData
663996b3
MS
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
663996b3 20#include <net/if.h>
663996b3 21#include <stdlib.h>
663996b3 22
86f210e9 23#include "sd-netlink.h"
db2df898 24
e735f4d4 25#include "loopback-setup.h"
db2df898
MP
26#include "missing.h"
27#include "netlink-util.h"
663996b3 28
86f210e9 29static int start_loopback(sd_netlink *rtnl) {
4c89c718 30 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
60f067b4 31 int r;
663996b3 32
5eef597e 33 r = sd_rtnl_message_new_link(rtnl, &req, RTM_SETLINK, LOOPBACK_IFINDEX);
60f067b4
JS
34 if (r < 0)
35 return r;
663996b3 36
60f067b4
JS
37 r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP);
38 if (r < 0)
39 return r;
663996b3 40
86f210e9 41 r = sd_netlink_call(rtnl, req, 0, NULL);
60f067b4
JS
42 if (r < 0)
43 return r;
663996b3 44
60f067b4 45 return 0;
663996b3
MS
46}
47
86f210e9 48static bool check_loopback(sd_netlink *rtnl) {
4c89c718 49 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
e735f4d4 50 unsigned flags;
663996b3 51 int r;
e735f4d4
MP
52
53 r = sd_rtnl_message_new_link(rtnl, &req, RTM_GETLINK, LOOPBACK_IFINDEX);
54 if (r < 0)
55 return false;
56
86f210e9 57 r = sd_netlink_call(rtnl, req, 0, &reply);
e735f4d4
MP
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;
663996b3
MS
66}
67
68int loopback_setup(void) {
4c89c718 69 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
5eef597e 70 int r;
663996b3 71
86f210e9 72 r = sd_netlink_open(&rtnl);
663996b3 73 if (r < 0)
60f067b4 74 return r;
663996b3 75
5eef597e 76 r = start_loopback(rtnl);
e735f4d4
MP
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;
663996b3 85
e735f4d4
MP
86 return log_warning_errno(r, "Failed to configure loopback device: %m");
87 }
663996b3
MS
88
89 return 0;
663996b3 90}