From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=none (p=none dis=none) header.from=gibson.dropbear.id.au Authentication-Results: passt.top; dkim=pass (2048-bit key; secure) header.d=gibson.dropbear.id.au header.i=@gibson.dropbear.id.au header.a=rsa-sha256 header.s=202408 header.b=CJ8NuKs/; dkim-atps=neutral Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id C48505A004C for ; Wed, 25 Sep 2024 08:54:47 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202408; t=1727247278; bh=IWpZ31hQAOz+YVqoCLSFP2PBFSlWvhjANlPAlZpTVko=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CJ8NuKs/+CfIARw7qwJkxz6NVVmNZk7B9Eh5lr55qyOocFJz6fUXwjkf6cItt53Ha obOISTzVwKojzUWwBPt7XyXeFAWuAahN85mEXpxnrHsIrmFMvRcoAP8esf3cHellul 4I1B2VYe4PIqZSOHXjkdpLWvXIuImwqDyPJpFD1q4fG2ZaOhmzR6EzMvf2OEUeYw/H 8k7FgZMAbMvoAjoDVcsFZ4NCswsq+iK8OFRTv/P3IhN7kJ/zaus3meKGhRR5Xd8/qe ieS04lCdAQ8sb/h2tMWGX9hPSZVMGABa97m2mZnbNkC/ThQqrKo0REmHJ/W6YA65r7 X0YPoNz606lAg== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4XD6sp28kKz4xPd; Wed, 25 Sep 2024 16:54:38 +1000 (AEST) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH 2/2] fwd: Direct inbound spliced forwards to the guest's external address Date: Wed, 25 Sep 2024 16:54:36 +1000 Message-ID: <20240925065436.2064995-3-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20240925065436.2064995-1-david@gibson.dropbear.id.au> References: <20240925065436.2064995-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: G6ZZ3AFNT2SOAVMEWK4BOMPH7IKEIXXI X-Message-ID-Hash: G6ZZ3AFNT2SOAVMEWK4BOMPH7IKEIXXI X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: David Gibson X-Mailman-Version: 3.3.8 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: In pasta mode, where addressing permits we "splice" connections, forwarding directly from host socket to guest/container socket without any L2 or L3 processing. This gives us a very large performance improvement when it's possible. Since the traffic is from a local socket within the guest, it will go over the guest's 'lo' interface, and accordingly we set the guest side address to be the loopback address. However this has a surprising side effect: sometimes guests will run services that are only supposed to be used within the guest and are therefore bound to only 127.0.0.1 and/or ::1. pasta's forwarding exposes those services to the host, which isn't generally what we want. Correct this by instead forwarding inbound "splice" flows to the guest's external address. Link: https://github.com/containers/podman/issues/24045 Signed-off-by: David Gibson --- fwd.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/fwd.c b/fwd.c index a505098..d5149db 100644 --- a/fwd.c +++ b/fwd.c @@ -447,20 +447,26 @@ uint8_t fwd_nat_from_host(const struct ctx *c, uint8_t proto, (proto == IPPROTO_TCP || proto == IPPROTO_UDP)) { /* spliceable */ - /* Preserve the specific loopback adddress used, but let the - * kernel pick a source port on the target side + /* The traffic will go over the guest's 'lo' interface, but use + * its external address, so we don't inadvertendly expose + * services that listen only on the guest's loopback address. + * + * Let the kernel pick our address on PIF_SPLICE */ - tgt->oaddr = ini->eaddr; + if (inany_v4(&ini->eaddr)) { + tgt->eaddr = inany_from_v4(c->ip4.addr_seen); + tgt->oaddr = inany_any4; + } else { + tgt->eaddr.a6 = c->ip6.addr_seen; + tgt->oaddr = inany_any6; + } + + /* Let the kernel pick port */ tgt->oport = 0; if (proto == IPPROTO_UDP) - /* But for UDP preserve the source port */ + /* Except for UDP, preserve the source port */ tgt->oport = ini->eport; - if (inany_v4(&ini->eaddr)) - tgt->eaddr = inany_loopback4; - else - tgt->eaddr = inany_loopback6; - return PIF_SPLICE; } -- 2.46.1