2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
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.
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.
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/>.
23 #include "sd-netlink.h"
25 #include "loopback-setup.h"
27 #include "netlink-util.h"
29 static int start_loopback(sd_netlink
*rtnl
) {
30 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
33 r
= sd_rtnl_message_new_link(rtnl
, &req
, RTM_SETLINK
, LOOPBACK_IFINDEX
);
37 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
41 r
= sd_netlink_call(rtnl
, req
, 0, NULL
);
48 static bool check_loopback(sd_netlink
*rtnl
) {
49 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
53 r
= sd_rtnl_message_new_link(rtnl
, &req
, RTM_GETLINK
, LOOPBACK_IFINDEX
);
57 r
= sd_netlink_call(rtnl
, req
, 0, &reply
);
61 r
= sd_rtnl_message_link_get_flags(reply
, &flags
);
65 return flags
& IFF_UP
;
68 int loopback_setup(void) {
69 _cleanup_(sd_netlink_unrefp
) sd_netlink
*rtnl
= NULL
;
72 r
= sd_netlink_open(&rtnl
);
76 r
= start_loopback(rtnl
);
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
))
86 return log_warning_errno(r
, "Failed to configure loopback device: %m");