#include <sys/un.h>
#include <sys/socket.h>
#include <linux/netlink.h>
-#include <linux/rtnetlink.h>
#include <sys/eventfd.h>
#include <net/if.h>
#include <linux/veth.h>
+#include <sys/personality.h>
#ifdef HAVE_SELINUX
#include <selinux/selinux.h>
#include "rtnl-util.h"
#include "udev-util.h"
+#ifdef HAVE_SECCOMP
+#include "seccomp-util.h"
+#endif
+
typedef enum LinkJournal {
LINK_NO,
LINK_AUTO,
static bool arg_keep_unit = false;
static char **arg_network_interfaces = NULL;
static bool arg_network_veth = false;
+static char *arg_network_bridge = NULL;
+static unsigned long arg_personality = 0xffffffffLU;
static int help(void) {
" --network-interface=INTERFACE\n"
" Assign an existing network interface to the\n"
" container\n"
- " --network-veth Add a a virtual ethernet connection between host\n"
+ " --network-veth Add a virtual ethernet connection between host\n"
" and container\n"
+ " --network-bridge=INTERFACE\n"
+ " Add a virtual ethernet connection between host\n"
+ " and container and add it to an existing bridge on\n"
+ " the host\n"
" -Z --selinux-context=SECLABEL\n"
" Set the SELinux security context to be used by\n"
" processes in the container\n"
ARG_KEEP_UNIT,
ARG_NETWORK_INTERFACE,
ARG_NETWORK_VETH,
+ ARG_NETWORK_BRIDGE,
+ ARG_PERSONALITY,
};
static const struct option options[] = {
{ "register", required_argument, NULL, ARG_REGISTER },
{ "keep-unit", no_argument, NULL, ARG_KEEP_UNIT },
{ "network-interface", required_argument, NULL, ARG_NETWORK_INTERFACE },
- { "network-veth", no_argument, NULL, ARG_NETWORK_VETH },
+ { "network-veth", no_argument, NULL, ARG_NETWORK_VETH },
+ { "network-bridge", required_argument, NULL, ARG_NETWORK_BRIDGE },
+ { "personality", required_argument, NULL, ARG_PERSONALITY },
{}
};
break;
+ case ARG_NETWORK_BRIDGE:
+ arg_network_bridge = strdup(optarg);
+ if (!arg_network_bridge)
+ return log_oom();
+
+ /* fall through */
+
case ARG_NETWORK_VETH:
arg_network_veth = true;
arg_private_network = true;
arg_keep_unit = true;
break;
+ case ARG_PERSONALITY:
+
+ arg_personality = personality_from_string(optarg);
+ if (arg_personality == 0xffffffffLU) {
+ log_error("Unknown or unsupported personality '%s'.", optarg);
+ return -EINVAL;
+ }
+
+ break;
+
case '?':
return -EINVAL;
static int setup_boot_id(const char *dest) {
_cleanup_free_ char *from = NULL, *to = NULL;
- sd_id128_t rnd;
+ sd_id128_t rnd = {};
char as_uuid[37];
int r;
return 0;
}
-static int setup_veth(int netns_fd) {
+static int setup_veth(pid_t pid, char iface_name[]) {
_cleanup_rtnl_message_unref_ sd_rtnl_message *m = NULL;
_cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
- char iface_name[IFNAMSIZ] = "ve-";
int r;
if (!arg_private_network)
if (!arg_network_veth)
return 0;
- strncpy(iface_name+3, arg_machine, sizeof(iface_name) - 3);
+ strncpy(iface_name+3, arg_machine, IFNAMSIZ - 3);
- r = sd_rtnl_open(0, &rtnl);
+ r = sd_rtnl_open(&rtnl, 0);
if (r < 0) {
log_error("Failed to connect to netlink: %s", strerror(-r));
return r;
}
- r = sd_rtnl_message_new_link(RTM_NEWLINK, 0, &m);
+ r = sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0);
if (r < 0) {
log_error("Failed to allocate netlink message: %s", strerror(-r));
return r;
}
- r = sd_rtnl_message_append_string(m, IFLA_IFNAME, "host0");
+ r = sd_rtnl_message_append_string(m, IFLA_IFNAME, iface_name);
if (r < 0) {
- log_error("Failed to append netlink kind: %s", strerror(-r));
+ log_error("Failed to add netlink interface name: %s", strerror(-r));
return r;
}
- r = sd_rtnl_message_open_container(m, IFLA_LINKINFO, 0);
+ r = sd_rtnl_message_open_container(m, IFLA_LINKINFO);
if (r < 0) {
log_error("Failed to open netlink container: %s", strerror(-r));
return r;
return r;
}
- r = sd_rtnl_message_open_container(m, IFLA_INFO_DATA, 0);
+ r = sd_rtnl_message_open_container(m, IFLA_INFO_DATA);
if (r < 0) {
log_error("Failed to open netlink container: %s", strerror(-r));
return r;
}
- r = sd_rtnl_message_open_container(m, VETH_INFO_PEER, sizeof(struct ifinfomsg));
+ r = sd_rtnl_message_open_container(m, VETH_INFO_PEER);
if (r < 0) {
- log_error("z Failed to open netlink container: %s", strerror(-r));
+ log_error("Failed to open netlink container: %s", strerror(-r));
return r;
}
- r = sd_rtnl_message_append_string(m, IFLA_IFNAME, iface_name);
+ r = sd_rtnl_message_append_string(m, IFLA_IFNAME, "host0");
if (r < 0) {
- log_error("Failed to append netlink kind: %s", strerror(-r));
+ log_error("Failed to add netlink interface name: %s", strerror(-r));
return r;
}
- r = sd_rtnl_message_append_u32(m, IFLA_NET_NS_FD, netns_fd);
+ r = sd_rtnl_message_append_u32(m, IFLA_NET_NS_PID, pid);
if (r < 0) {
log_error("Failed to add netlink namespace field: %s", strerror(-r));
return r;
return 0;
}
+static int setup_bridge(const char veth_name[]) {
+ _cleanup_rtnl_message_unref_ sd_rtnl_message *m = NULL;
+ _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
+ int r, bridge;
+
+ if (!arg_private_network)
+ return 0;
+
+ if (!arg_network_veth)
+ return 0;
+
+ if (!arg_network_bridge)
+ return 0;
+
+ bridge = (int) if_nametoindex(arg_network_bridge);
+ if (bridge <= 0) {
+ log_error("Failed to resolve interface %s: %m", arg_network_bridge);
+ return -errno;
+ }
+
+ r = sd_rtnl_open(&rtnl, 0);
+ if (r < 0) {
+ log_error("Failed to connect to netlink: %s", strerror(-r));
+ return r;
+ }
+
+ r = sd_rtnl_message_new_link(rtnl, &m, RTM_SETLINK, 0);
+ if (r < 0) {
+ log_error("Failed to allocate netlink message: %s", strerror(-r));
+ return r;
+ }
+
+ r = sd_rtnl_message_append_string(m, IFLA_IFNAME, veth_name);
+ if (r < 0) {
+ log_error("Failed to add netlink interface name field: %s", strerror(-r));
+ return r;
+ }
+
+ r = sd_rtnl_message_append_u32(m, IFLA_MASTER, bridge);
+ if (r < 0) {
+ log_error("Failed to add netlink master field: %s", strerror(-r));
+ return r;
+ }
+
+ r = sd_rtnl_call(rtnl, m, 0, NULL);
+ if (r < 0) {
+ log_error("Failed to add veth interface to bridge: %s", strerror(-r));
+ return r;
+ }
+
+ return 0;
+}
+
static int move_network_interfaces(pid_t pid) {
_cleanup_udev_unref_ struct udev *udev = NULL;
_cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
if (strv_isempty(arg_network_interfaces))
return 0;
- r = sd_rtnl_open(0, &rtnl);
+ r = sd_rtnl_open(&rtnl, 0);
if (r < 0) {
log_error("Failed to connect to netlink: %s", strerror(-r));
return r;
return -EBUSY;
}
- r = sd_rtnl_message_new_link(RTM_NEWLINK, ifi, &m);
+ r = sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, ifi);
if (r < 0) {
log_error("Failed to allocate netlink message: %s", strerror(-r));
return r;
if (!seccomp)
return log_oom();
- r = seccomp_rule_add_exact(
+ r = seccomp_add_secondary_archs(seccomp);
+ if (r < 0 && r != -EEXIST) {
+ log_error("Failed to add secondary archs to seccomp filter: %s", strerror(-r));
+ goto finish;
+ }
+
+ r = seccomp_rule_add(
seccomp,
SCMP_ACT_ERRNO(EAFNOSUPPORT),
SCMP_SYS(socket),
int main(int argc, char *argv[]) {
- _cleanup_close_ int master = -1, kdbus_fd = -1, sync_fd = -1, netns_fd = -1;
+ _cleanup_close_ int master = -1, kdbus_fd = -1, sync_fd = -1;
_cleanup_close_pipe_ int kmsg_socket_pair[2] = { -1, -1 };
_cleanup_free_ char *kdbus_domain = NULL;
_cleanup_fdset_free_ FDSet *fds = NULL;
int n_fd_passed;
pid_t pid = 0;
sigset_t mask;
+ char veth_name[IFNAMSIZ] = "ve-";
log_parse_environment();
log_open();
goto finish;
}
- if (arg_network_veth) {
- netns_fd = open("/proc/self/ns/net", O_RDWR|O_CLOEXEC);
- if (netns_fd < 0) {
- log_error("Failed to open network namespace fd: %m");
- goto finish;
- }
- }
-
if (access("/dev/kdbus/control", F_OK) >= 0) {
if (arg_share_system) {
dev_setup(arg_directory);
- if (setup_veth(netns_fd) < 0)
- goto child_fail;
-
- if (netns_fd >= 0) {
- close_nointr_nofail(netns_fd);
- netns_fd = -1;
- }
-
if (audit_still_doesnt_work_in_containers() < 0)
goto child_fail;
setup_hostname();
+ if (arg_personality != 0xffffffffLU) {
+ if (personality(arg_personality) < 0) {
+ log_error("personality() failed: %m");
+ goto child_fail;
+ }
+ }
+
eventfd_read(sync_fd, &x);
close_nointr_nofail(sync_fd);
sync_fd = -1;
if (r < 0)
goto finish;
+ r = setup_veth(pid, veth_name);
+ if (r < 0)
+ goto finish;
+
+ r = setup_bridge(veth_name);
+ if (r < 0)
+ goto finish;
+
eventfd_write(sync_fd, 1);
close_nointr_nofail(sync_fd);
sync_fd = -1;