From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by passt.top (Postfix) with ESMTP id 10DBD5A026A for ; Thu, 17 Nov 2022 13:26:23 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1668687982; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=WICiwNd5N1W63Q3VPAeqnSCeuMPnoCAXVNG9iLQovO4=; b=jEakuGIxZtVciWmyJrtSfcEZYyzq4XSW4JgF2r20Kio5GI6fg8Cw4k4sCx9o5kpZp2taGX xL1PjDymtxik1a5OVziL40Go6jykTBZseFIVYn9DPEHIGh66JwSdCDR17ZlwHzULF67M+g mOTKJTG+vwa4oWRyLt7VYi7LJ/lOPPk= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-515-sDaNSAIAO2mN-z5wmyoTlg-1; Thu, 17 Nov 2022 07:26:21 -0500 X-MC-Unique: sDaNSAIAO2mN-z5wmyoTlg-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E09702999B50 for ; Thu, 17 Nov 2022 12:26:20 +0000 (UTC) Received: from pick.home.annexia.org (unknown [10.39.193.198]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6D210C158CF; Thu, 17 Nov 2022 12:26:20 +0000 (UTC) From: "Richard W.M. Jones" To: sbrivio@redhat.com Subject: [PATCH passt 3/5] Add --fd option Date: Thu, 17 Nov 2022 12:26:12 +0000 Message-Id: <20221117122614.1269214-4-rjones@redhat.com> In-Reply-To: <20221117122614.1269214-1-rjones@redhat.com> References: <20221117122614.1269214-1-rjones@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-MailFrom: rjones@redhat.com X-Mailman-Rule-Hits: nonmember-moderation X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation Message-ID-Hash: 7Q2WNKO5GNMEPGX6HYQ22NSPR7Z5UVKY X-Message-ID-Hash: 7Q2WNKO5GNMEPGX6HYQ22NSPR7Z5UVKY X-Mailman-Approved-At: Thu, 17 Nov 2022 13:28:25 +0100 CC: passt-dev@passt.top X-Mailman-Version: 3.3.3 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: This passes a fully connected stream socket to passt. Signed-off-by: Richard W.M. Jones --- conf.c | 15 +++++++++++++++ passt.1 | 10 ++++++++++ passt.c | 2 +- passt.h | 2 ++ tap.c | 22 +++++++++++++++++++++- 5 files changed, 49 insertions(+), 2 deletions(-) diff --git a/conf.c b/conf.c index 1adcf83..5e08bd6 100644 --- a/conf.c +++ b/conf.c @@ -1089,6 +1089,7 @@ void conf(struct ctx *c, int argc, char **argv) {"log-file", required_argument, NULL, 'l' }, {"help", no_argument, NULL, 'h' }, {"socket", required_argument, NULL, 's' }, + {"fd", required_argument, NULL, 'F' }, {"ns-ifname", required_argument, NULL, 'I' }, {"pcap", required_argument, NULL, 'p' }, {"pid", required_argument, NULL, 'P' }, @@ -1366,6 +1367,15 @@ void conf(struct ctx *c, int argc, char **argv) usage(argv[0]); } break; + case 'F': + if (c->sock_fd >= 0) { + err("Multiple --fd options given"); + usage(argv[0]); + } + + errno = 0; + c->sock_fd = strtol(optarg, NULL, 0); + break; case 'I': if (*c->pasta_ifn) { err("Multiple --ns-ifname options given"); @@ -1600,6 +1610,11 @@ void conf(struct ctx *c, int argc, char **argv) usage(argv[0]); } + if (*c->sock_path && c->sock_fd >= 0) { + err("Options --socket and --fd are mutually exclusive"); + usage(argv[0]); + } + ret = conf_ugid(runas, &uid, &gid); if (ret) usage(argv[0]); diff --git a/passt.1 b/passt.1 index e34a3e0..66d5eb6 100644 --- a/passt.1 +++ b/passt.1 @@ -297,6 +297,16 @@ Path for UNIX domain socket used by \fBqemu\fR(1) or \fBqrap\fR(1) to connect to Default is to probe a free socket, not accepting connections, starting from \fI/tmp/passt_1.socket\fR to \fI/tmp/passt_64.socket\fR. +.TP +.BR \-F ", " \-\-fd " " \fIFD +Pass a pre-opened, connected socket to +\fBpasst\fR. +Usually the socket is opened in the parent process and +\fBpasst\fR +inherits it when run as a child. This allows the parent process to +open sockets using another address family or requiring special +privileges. + .TP .BR \-1 ", " \-\-one-off Quit after handling a single client connection, that is, once the client closes diff --git a/passt.c b/passt.c index 7d323c2..ef4643e 100644 --- a/passt.c +++ b/passt.c @@ -187,7 +187,7 @@ int main(int argc, char **argv) isolate_initial(); - c.pasta_netns_fd = c.fd_tap = c.fd_tap_listen = -1; + c.pasta_netns_fd = c.fd_tap = c.fd_tap_listen = c.sock_fd = -1; sigemptyset(&sa.sa_mask); sa.sa_flags = 0; diff --git a/passt.h b/passt.h index e93eea8..32fc4ae 100644 --- a/passt.h +++ b/passt.h @@ -149,6 +149,7 @@ struct ip6_ctx { * @stderr: Force logging to stderr * @nofile: Maximum number of open files (ulimit -n) * @sock_path: Path for UNIX domain socket + * @sock_fd: Connected UNIX domain socket * @pcap: Path for packet capture file * @pid_file: Path to PID file, empty string if not configured * @pasta_netns_fd: File descriptor for network namespace in pasta mode @@ -198,6 +199,7 @@ struct ctx { int stderr; int nofile; char sock_path[UNIX_PATH_MAX]; + int sock_fd; char pcap[PATH_MAX]; char pid_file[PATH_MAX]; int one_off; diff --git a/tap.c b/tap.c index abeff25..418a788 100644 --- a/tap.c +++ b/tap.c @@ -1063,6 +1063,24 @@ static void tap_sock_tun_init(struct ctx *c) epoll_ctl(c->epollfd, EPOLL_CTL_ADD, c->fd_tap, &ev); } +/** + * tap_sock_fd_init() - Set up a connected socket (c->sock_fd) + * @c: Execution context + */ +void tap_sock_fd_init(struct ctx *c) +{ + struct epoll_event ev = { 0 }; + + c->fd_tap = c->sock_fd; + c->sock_fd = -1; + + info("Setting up fd %d as tap socket", c->fd_tap); + + ev.data.fd = c->fd_tap; + ev.events = EPOLLIN | EPOLLRDHUP; + epoll_ctl(c->epollfd, EPOLL_CTL_ADD, c->fd_tap, &ev); +} + /** * tap_sock_init() - Create and set up AF_UNIX socket or tuntap file descriptor * @c: Execution context @@ -1087,7 +1105,9 @@ void tap_sock_init(struct ctx *c) } if (c->mode == MODE_PASST) { - if (c->fd_tap_listen == -1) + if (c->sock_fd >= 0) + tap_sock_fd_init(c); + else if (c->fd_tap_listen == -1) tap_sock_unix_init(c); } else { tap_sock_tun_init(c); -- 2.37.0.rc2