2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2010-05-09 18:02:38 +02:00
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
#include "sd-netlink.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
|
2015-02-03 13:48:15 +01:00
|
|
|
#include "loopback-setup.h"
|
2019-10-31 03:07:23 +01:00
|
|
|
#include "missing_network.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "netlink-util.h"
|
2019-03-27 11:32:41 +01:00
|
|
|
#include "time-util.h"
|
2014-01-21 15:55:57 +01:00
|
|
|
|
2017-06-26 09:33:37 +02:00
|
|
|
#define LOOPBACK_SETUP_TIMEOUT_USEC (5 * USEC_PER_SEC)
|
|
|
|
|
2017-06-23 12:07:03 +02:00
|
|
|
struct state {
|
|
|
|
unsigned n_messages;
|
|
|
|
int rcode;
|
2018-01-23 01:50:30 +01:00
|
|
|
const char *error_message;
|
|
|
|
const char *success_message;
|
2017-06-23 12:07:03 +02:00
|
|
|
};
|
|
|
|
|
2017-07-02 16:23:07 +02:00
|
|
|
static int generic_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) {
|
2017-06-23 12:07:03 +02:00
|
|
|
struct state *s = userdata;
|
2018-01-23 01:50:30 +01:00
|
|
|
int r;
|
2017-06-23 12:07:03 +02:00
|
|
|
|
|
|
|
assert(s);
|
|
|
|
assert(s->n_messages > 0);
|
|
|
|
s->n_messages--;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
|
2018-01-23 01:50:30 +01:00
|
|
|
r = sd_netlink_message_get_errno(m);
|
|
|
|
if (r < 0)
|
|
|
|
log_debug_errno(r, "%s: %m", s->error_message);
|
|
|
|
else
|
|
|
|
log_debug("%s", s->success_message);
|
2017-06-23 12:07:03 +02:00
|
|
|
|
2018-01-23 01:50:30 +01:00
|
|
|
s->rcode = r;
|
2017-06-23 12:07:03 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int start_loopback(sd_netlink *rtnl, struct state *s) {
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
|
2013-10-29 23:50:07 +01:00
|
|
|
int r;
|
2010-05-09 18:02:38 +02:00
|
|
|
|
2017-06-23 12:07:03 +02:00
|
|
|
assert(rtnl);
|
|
|
|
assert(s);
|
|
|
|
|
2014-08-08 12:12:31 +02:00
|
|
|
r = sd_rtnl_message_new_link(rtnl, &req, RTM_SETLINK, LOOPBACK_IFINDEX);
|
2013-12-06 18:16:16 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2014-01-21 15:20:42 +01:00
|
|
|
r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP);
|
2013-10-29 23:50:07 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2010-05-09 18:02:38 +02:00
|
|
|
|
2018-10-15 10:49:53 +02:00
|
|
|
r = sd_netlink_call_async(rtnl, NULL, req, generic_handler, NULL, s, LOOPBACK_SETUP_TIMEOUT_USEC, "systemd-start-loopback");
|
2017-06-23 12:07:03 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
s->n_messages ++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int add_ipv4_address(sd_netlink *rtnl, struct state *s) {
|
|
|
|
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(rtnl);
|
|
|
|
assert(s);
|
|
|
|
|
|
|
|
r = sd_rtnl_message_new_addr(rtnl, &req, RTM_NEWADDR, LOOPBACK_IFINDEX, AF_INET);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = sd_rtnl_message_addr_set_prefixlen(req, 8);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = sd_rtnl_message_addr_set_flags(req, IFA_F_PERMANENT);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = sd_rtnl_message_addr_set_scope(req, RT_SCOPE_HOST);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = sd_netlink_message_append_in_addr(req, IFA_LOCAL, &(struct in_addr) { .s_addr = htobe32(INADDR_LOOPBACK) } );
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2018-10-15 10:49:53 +02:00
|
|
|
r = sd_netlink_call_async(rtnl, NULL, req, generic_handler, NULL, s, USEC_INFINITY, "systemd-loopback-ipv4");
|
2017-06-23 12:07:03 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
s->n_messages ++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int add_ipv6_address(sd_netlink *rtnl, struct state *s) {
|
|
|
|
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(rtnl);
|
|
|
|
assert(s);
|
|
|
|
|
|
|
|
r = sd_rtnl_message_new_addr(rtnl, &req, RTM_NEWADDR, LOOPBACK_IFINDEX, AF_INET6);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = sd_rtnl_message_addr_set_prefixlen(req, 128);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = sd_rtnl_message_addr_set_flags(req, IFA_F_PERMANENT);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = sd_rtnl_message_addr_set_scope(req, RT_SCOPE_HOST);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = sd_netlink_message_append_in6_addr(req, IFA_LOCAL, &in6addr_loopback);
|
2013-10-29 23:50:07 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2010-05-09 18:02:38 +02:00
|
|
|
|
2018-10-15 10:49:53 +02:00
|
|
|
r = sd_netlink_call_async(rtnl, NULL, req, generic_handler, NULL, s, USEC_INFINITY, "systemd-loopback-ipv6");
|
2017-06-23 12:07:03 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
s->n_messages ++;
|
2010-05-09 18:02:38 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
static bool check_loopback(sd_netlink *rtnl) {
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
|
2014-12-28 13:38:23 +01:00
|
|
|
unsigned flags;
|
2013-03-25 00:45:16 +01:00
|
|
|
int r;
|
2014-12-28 13:38:23 +01:00
|
|
|
|
|
|
|
r = sd_rtnl_message_new_link(rtnl, &req, RTM_GETLINK, LOOPBACK_IFINDEX);
|
|
|
|
if (r < 0)
|
2014-12-29 13:05:45 +01:00
|
|
|
return false;
|
2014-12-28 13:38:23 +01:00
|
|
|
|
2017-06-23 12:07:03 +02:00
|
|
|
r = sd_netlink_call(rtnl, req, USEC_INFINITY, &reply);
|
2014-12-28 13:38:23 +01:00
|
|
|
if (r < 0)
|
2014-12-29 13:05:45 +01:00
|
|
|
return false;
|
2014-12-28 13:38:23 +01:00
|
|
|
|
|
|
|
r = sd_rtnl_message_link_get_flags(reply, &flags);
|
|
|
|
if (r < 0)
|
2014-12-29 13:05:45 +01:00
|
|
|
return false;
|
2014-12-28 13:38:23 +01:00
|
|
|
|
|
|
|
return flags & IFF_UP;
|
2012-04-13 17:10:21 +02:00
|
|
|
}
|
|
|
|
|
2010-05-09 18:02:38 +02:00
|
|
|
int loopback_setup(void) {
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
|
2018-01-23 01:50:30 +01:00
|
|
|
struct state state_4 = {
|
|
|
|
.error_message = "Failed to add address 127.0.0.1 to loopback interface",
|
|
|
|
.success_message = "Successfully added address 127.0.0.1 to loopback interface",
|
|
|
|
}, state_6 = {
|
|
|
|
.error_message = "Failed to add address ::1 to loopback interface",
|
|
|
|
.success_message = "Successfully added address ::1 to loopback interface",
|
|
|
|
}, state_up = {
|
|
|
|
.error_message = "Failed to bring loopback interface up",
|
|
|
|
.success_message = "Successfully brought loopback interface up",
|
|
|
|
};
|
2014-08-08 12:12:31 +02:00
|
|
|
int r;
|
2013-12-15 14:00:20 +01:00
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
r = sd_netlink_open(&rtnl);
|
2012-04-13 17:10:21 +02:00
|
|
|
if (r < 0)
|
2017-06-23 12:07:03 +02:00
|
|
|
return log_error_errno(r, "Failed to open netlink: %m");
|
|
|
|
|
|
|
|
/* Note that we add the IP addresses here explicitly even though the kernel does that too implicitly when
|
|
|
|
* setting up the loopback device. The reason we do this here a second time (and possibly race against the
|
|
|
|
* kernel) is that we want to synchronously wait until the IP addresses are set up correctly, see
|
|
|
|
*
|
|
|
|
* https://github.com/systemd/systemd/issues/5641 */
|
|
|
|
|
2017-07-02 16:23:07 +02:00
|
|
|
r = add_ipv4_address(rtnl, &state_4);
|
2017-06-23 12:07:03 +02:00
|
|
|
if (r < 0)
|
2017-06-24 00:53:29 +02:00
|
|
|
return log_error_errno(r, "Failed to enqueue IPv4 loopback address add request: %m");
|
2017-06-23 12:07:03 +02:00
|
|
|
|
2017-07-02 16:23:07 +02:00
|
|
|
r = add_ipv6_address(rtnl, &state_6);
|
2017-06-23 12:07:03 +02:00
|
|
|
if (r < 0)
|
2017-07-02 16:23:07 +02:00
|
|
|
return log_error_errno(r, "Failed to enqueue IPv6 loopback address add request: %m");
|
2017-06-23 12:07:03 +02:00
|
|
|
|
2017-07-02 16:23:07 +02:00
|
|
|
r = start_loopback(rtnl, &state_up);
|
2017-06-23 12:07:03 +02:00
|
|
|
if (r < 0)
|
2017-06-24 00:53:29 +02:00
|
|
|
return log_error_errno(r, "Failed to enqueue loopback interface start request: %m");
|
2017-06-23 12:07:03 +02:00
|
|
|
|
2017-07-02 16:23:07 +02:00
|
|
|
while (state_4.n_messages + state_6.n_messages + state_up.n_messages > 0) {
|
2017-06-26 09:33:37 +02:00
|
|
|
r = sd_netlink_wait(rtnl, LOOPBACK_SETUP_TIMEOUT_USEC);
|
2017-06-23 12:07:03 +02:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to wait for netlink event: %m");
|
|
|
|
|
|
|
|
r = sd_netlink_process(rtnl, NULL);
|
|
|
|
if (r < 0)
|
|
|
|
return log_warning_errno(r, "Failed to process netlink event: %m");
|
|
|
|
}
|
2010-05-09 18:02:38 +02:00
|
|
|
|
2017-07-02 16:23:07 +02:00
|
|
|
/* Note that we don't really care whether the addresses could be added or not */
|
|
|
|
if (state_up.rcode != 0) {
|
|
|
|
/* If we lack the permissions to configure the loopback device,
|
|
|
|
* but we find it to be already configured, let's exit cleanly,
|
|
|
|
* in order to supported unprivileged containers. */
|
|
|
|
if (state_up.rcode == -EPERM && check_loopback(rtnl))
|
2015-02-03 13:53:01 +01:00
|
|
|
return 0;
|
2010-05-09 18:02:38 +02:00
|
|
|
|
2017-07-02 16:23:07 +02:00
|
|
|
return log_warning_errno(state_up.rcode, "Failed to configure loopback device: %m");
|
2015-02-03 13:53:01 +01:00
|
|
|
}
|
2012-04-13 17:10:21 +02:00
|
|
|
|
2013-03-25 00:45:16 +01:00
|
|
|
return 0;
|
2010-05-09 18:02:38 +02:00
|
|
|
}
|