From 9a175cc2cea75b98fc3c20381f58dcabf24ef529 Mon Sep 17 00:00:00 2001 From: Giuseppe Scrivano Date: Wed, 29 Sep 2021 16:11:06 +0200 Subject: pasta: Allow specifying paths and names of namespaces Based on a patch from Giuseppe Scrivano, this adds the ability to: - specify paths and names of target namespaces to join, instead of a PID, also for user namespaces, with --userns - request to join or create a network namespace only, without entering or creating a user namespace, with --netns-only - specify the base directory for netns mountpoints, with --nsrun-dir Signed-off-by: Giuseppe Scrivano [sbrivio: reworked logic to actually join the given namespaces when they're not created, implemented --netns-only and --nsrun-dir, updated pasta demo script and man page] Signed-off-by: Stefano Brivio --- tap.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) (limited to 'tap.c') diff --git a/tap.c b/tap.c index 8d8f381..0abf5a0 100644 --- a/tap.c +++ b/tap.c @@ -845,13 +845,13 @@ static int tun_ns_fd = -1; /** * tap_sock_init_tun_ns() - Create tuntap fd in namespace, bring up loopback - * @target_pid: Pointer to target PID + * @c: Execution context */ -static int tap_sock_init_tun_ns(void *target_pid) +static int tap_sock_init_tun_ns(void *c) { int fd; - if (ns_enter(*(int *)target_pid)) + if (ns_enter((struct ctx *)c)) goto fail; if ((fd = open("/dev/net/tun", O_RDWR | O_NONBLOCK)) < 0) @@ -883,12 +883,10 @@ fail: /** * struct tap_sock_if_up_ns_arg - Arguments for tap_sock_if_up_ns() * @c: Execution context - * @target_pid: Target namespace PID * @ifname: Interface name of tap device */ struct tap_sock_if_up_ns_arg { struct ctx *c; - int target_pid; char ifname[IFNAMSIZ]; }; @@ -906,7 +904,7 @@ static int tap_sock_if_up_ns(void *arg) a = (struct tap_sock_if_up_ns_arg *)arg; - if (ns_enter(a->target_pid)) + if (ns_enter(a->c)) return 0; if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { @@ -955,7 +953,7 @@ static void tap_sock_init_tun(struct ctx *c) struct ifreq ifr = { .ifr_flags = IFF_TAP | IFF_NO_PI }; struct tap_sock_if_up_ns_arg ifup_arg; - NS_CALL(tap_sock_init_tun_ns, &c->pasta_pid); + NS_CALL(tap_sock_init_tun_ns, c); if (tun_ns_fd == -1) { err("Failed to open tun socket in namespace"); exit(EXIT_FAILURE); @@ -968,11 +966,10 @@ static void tap_sock_init_tun(struct ctx *c) } strncpy(ifup_arg.ifname, c->pasta_ifn, IFNAMSIZ); - ifup_arg.target_pid = c->pasta_pid; ifup_arg.c = c; NS_CALL(tap_sock_if_up_ns, (void *)&ifup_arg); - pcap_init(c, c->pasta_pid); + pcap_init(c, c->pasta_netns_fd); c->fd_tap = tun_ns_fd; } -- cgit v1.2.3