X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fnspawn%2Fnspawn.c;h=379ea92355e6f512c15b06ad04fcf3b66fd63f7e;hp=fcc0f17aee011a9542262bd0d18177d6dcdadf62;hb=3302da4667640ac130956d8d90be7c39fb39222f;hpb=ee3a6a51e5b098aa0c9641ed71d275c459ad2f5a diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c index fcc0f17ae..379ea9235 100644 --- a/src/nspawn/nspawn.c +++ b/src/nspawn/nspawn.c @@ -40,7 +40,6 @@ #include #include #include -#include #include #include #include @@ -80,6 +79,10 @@ #include "rtnl-util.h" #include "udev-util.h" +#ifdef HAVE_SECCOMP +#include "seccomp-util.h" +#endif + typedef enum LinkJournal { LINK_NO, LINK_AUTO, @@ -134,6 +137,7 @@ static bool arg_register = true; 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 int help(void) { @@ -152,8 +156,12 @@ 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" @@ -197,6 +205,7 @@ static int parse_argv(int argc, char *argv[]) { ARG_KEEP_UNIT, ARG_NETWORK_INTERFACE, ARG_NETWORK_VETH, + ARG_NETWORK_BRIDGE, }; static const struct option options[] = { @@ -223,7 +232,8 @@ static int parse_argv(int argc, char *argv[]) { { "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 }, {} }; @@ -263,6 +273,13 @@ static int parse_argv(int argc, char *argv[]) { 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; @@ -1271,10 +1288,9 @@ static int reset_audit_loginuid(void) { 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) @@ -1283,7 +1299,7 @@ static int setup_veth(int netns_fd) { 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); if (r < 0) { @@ -1291,15 +1307,15 @@ static int setup_veth(int netns_fd) { return r; } - r = sd_rtnl_message_new_link(RTM_NEWLINK, 0, &m); + r = sd_rtnl_message_new_link(rtnl, RTM_NEWLINK, 0, &m); 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; } @@ -1323,17 +1339,17 @@ static int setup_veth(int netns_fd) { 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; @@ -1366,6 +1382,59 @@ static int setup_veth(int netns_fd) { 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(0, &rtnl); + if (r < 0) { + log_error("Failed to connect to netlink: %s", strerror(-r)); + return r; + } + + r = sd_rtnl_message_new_link(rtnl, RTM_SETLINK, 0, &m); + 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; @@ -1414,7 +1483,7 @@ static int move_network_interfaces(pid_t pid) { return -EBUSY; } - r = sd_rtnl_message_new_link(RTM_NEWLINK, ifi, &m); + r = sd_rtnl_message_new_link(rtnl, RTM_NEWLINK, ifi, &m); if (r < 0) { log_error("Failed to allocate netlink message: %s", strerror(-r)); return r; @@ -1456,7 +1525,13 @@ static int audit_still_doesnt_work_in_containers(void) { 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), @@ -1489,7 +1564,7 @@ finish: 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; @@ -1498,6 +1573,7 @@ int main(int argc, char *argv[]) { int n_fd_passed; pid_t pid = 0; sigset_t mask; + char veth_name[IFNAMSIZ] = "ve-"; log_parse_environment(); log_open(); @@ -1604,14 +1680,6 @@ int main(int argc, char *argv[]) { 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) { @@ -1766,14 +1834,6 @@ int main(int argc, char *argv[]) { 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; @@ -1988,6 +2048,14 @@ int main(int argc, char *argv[]) { 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;