From nobody Sat Nov 30 02:50:11 2024 Received: from mail-wm1-f54.google.com (mail-wm1-f54.google.com [209.85.128.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E54E51D88B9 for ; Fri, 13 Sep 2024 09:39:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726220370; cv=none; b=Rh9Sa2nmseJnWeqy/0oWDQ169EXStv3MQYT/j85ZW6o54viznICmlGkapeyx7Y51anTbCK54I0lPuk6vyDA0yalW2FlTaem7/Qd4saxnw3j5AQOk2WwUV/jat/SfBuJf91I56FMHNxFuwkjTXDDHyaor7ymTG4y8z6SzZPLUJmg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726220370; c=relaxed/simple; bh=kwVTA1xn2dlYAQ5FaOqW3WQio+O9v5ZocxxWoHtdcDQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=FU4vU7Y3Yea9Rtfcg08XxPesxZnRdbzOccz28gi/mVjZkxiacK7kidyZdDVkMzOzrCdu4n93o++YFwCaLyQyukcMCzEoLn9l9ApK+I/2weQCka9ZpvTrMXQDXkAt9GphdXel/FlCgQeu9WZKAjwH7eUmHj+H26Gd60OqeHKttR8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=cloudflare.com; spf=pass smtp.mailfrom=cloudflare.com; dkim=pass (2048-bit key) header.d=cloudflare.com header.i=@cloudflare.com header.b=gN9mRqbh; arc=none smtp.client-ip=209.85.128.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=cloudflare.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=cloudflare.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=cloudflare.com header.i=@cloudflare.com header.b="gN9mRqbh" Received: by mail-wm1-f54.google.com with SMTP id 5b1f17b1804b1-42cae6bb895so18498545e9.1 for ; Fri, 13 Sep 2024 02:39:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google09082023; t=1726220367; x=1726825167; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=lw0zQpZeq+FMh46boYtf1wMWWNki5/w8+fazi2fO7P0=; b=gN9mRqbhjam2T9IGTs5lMaq2b0xYNElZCBYrlIxpoFhGq75l+ouF3d3+vgPjZNP9Vb SQK56eUh1pXkA3R3wG1grYG6i9TOv9aHavtwufvKmBa2xO9w3pBBVXkGhhNr1EIPVV71 iAdC1aIfySCOBS1hpuphDztkLTsgFeKhDnqTx4AY4SMiELeLYKlJJV+0lLRcigpqsPCw /QT15gPRIzWRb2lp51cvNq30ku7aoLD0Sp4u53xUYGSlUTo2L6vwN1NrM3jVYmr1zada YKg9CZntzErK8fVmE3cJNzHChmrcesJF7jfcq9ExmG3h4Ho5rSKSF7uDQztCaIzKCy31 DaCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726220367; x=1726825167; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=lw0zQpZeq+FMh46boYtf1wMWWNki5/w8+fazi2fO7P0=; b=VQ2mr/JIZIsR/v95+F1w5VWuEPCFs5aXVRGMTn0qF6q6GX3eKlOGX6Uq5h1LWMG2jQ htN+UwWZjINsLxXmf3+C4Iua9vOdHPdqaXmQyyHVV1sXkJcPOLaYe1B6+IOx0/2RVv6s GbVYagtGz3XgOrEl7RdfL0i6YXNQLz75gPJTxaSDREMt4JQg/3qJ0MfWly86ydUUeIxR I995LF7L4iUBCEiY4UA3dh7EcKzaDKXojrCeZO/5d5IZwWJSRZi/DGGwj64Ocwm7fxmg wVtUeF/xwETALCzAfV9i2AY4+lSh5nS7ponkPdn2cHw++899BtnTrDkeQLk+4FGtZQUV +e5g== X-Forwarded-Encrypted: i=1; AJvYcCURTjcpi5w2myXwI9fnciLmqK+mPLVDEWec2kATDAsosUvM86oNvfDVwywnQTLDb2IMzxv0AsPxzRBEpRU=@vger.kernel.org X-Gm-Message-State: AOJu0Yzgs06mmYjMmeXe3QPH2+Ib/sEUQj4LiCn3Q6eu6Yt5TUFJpHw8 ttgunyMhjxlyA1w4PtqJ+cKxCNfw2GpOzJImod0sd4NOQDiXALoPyOMKJpIsumc= X-Google-Smtp-Source: AGHT+IGgG2ZTysSwmI61riKiy7WlPK0eMWG0eKUUV3q1+A4ChRBKjnQEymksqdsX8ZEbb5LITzFKGA== X-Received: by 2002:adf:a407:0:b0:374:c90c:226 with SMTP id ffacd0b85a97d-378c2cfec60mr3448025f8f.9.1726220367026; Fri, 13 Sep 2024 02:39:27 -0700 (PDT) Received: from [127.0.1.1] ([2a09:bac5:3802:d2::15:37a]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-37895665548sm16474484f8f.34.2024.09.13.02.39.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Sep 2024 02:39:26 -0700 (PDT) From: Tiago Lam Date: Fri, 13 Sep 2024 10:39:21 +0100 Subject: [RFC PATCH 3/3] bpf: Add sk_lookup test to use ORIGDSTADDR cmsg. Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20240913-reverse-sk-lookup-v1-3-e721ea003d4c@cloudflare.com> References: <20240913-reverse-sk-lookup-v1-0-e721ea003d4c@cloudflare.com> In-Reply-To: <20240913-reverse-sk-lookup-v1-0-e721ea003d4c@cloudflare.com> To: "David S. Miller" , David Ahern , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, Jakub Sitnicki , Tiago Lam , kernel-team@cloudflare.com X-Mailer: b4 0.14.1 This patch reuses the framework already in place for sk_lookup, allowing it now to send a reply from the server fd directly, instead of having to create a socket bound to the original destination address and reply from there. It does this by passing the source address and port from where to reply from in a IP_ORIGDSTADDR ancilliary message passed in sendmsg. Signed-off-by: Tiago Lam --- tools/testing/selftests/bpf/prog_tests/sk_lookup.c | 70 +++++++++++++++---= ---- 1 file changed, 48 insertions(+), 22 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c b/tools/tes= ting/selftests/bpf/prog_tests/sk_lookup.c index ae87c00867ba..b99aff2e3976 100644 --- a/tools/testing/selftests/bpf/prog_tests/sk_lookup.c +++ b/tools/testing/selftests/bpf/prog_tests/sk_lookup.c @@ -75,6 +75,7 @@ struct test { struct inet_addr listen_at; enum server accept_on; bool reuseport_has_conns; /* Add a connected socket to reuseport group */ + bool dont_bind_reply; /* Don't bind, send direct from server fd. */ }; =20 struct cb_opts { @@ -363,7 +364,7 @@ static void v4_to_v6(struct sockaddr_storage *ss) memset(&v6->sin6_addr.s6_addr[0], 0, 10); } =20 -static int udp_recv_send(int server_fd) +static int udp_recv_send(int server_fd, bool dont_bind_reply) { char cmsg_buf[CMSG_SPACE(sizeof(struct sockaddr_storage))]; struct sockaddr_storage _src_addr =3D { 0 }; @@ -415,26 +416,41 @@ static int udp_recv_send(int server_fd) v4_to_v6(dst_addr); } =20 - /* Reply from original destination address. */ - fd =3D start_server_addr(SOCK_DGRAM, dst_addr, sizeof(*dst_addr), NULL); - if (!ASSERT_OK_FD(fd, "start_server_addr")) { - log_err("failed to create tx socket"); - return -1; - } + if (dont_bind_reply) { + /* Reply directly from server fd, specifying the source address and/or + * port in struct msghdr. + */ + n =3D sendmsg(server_fd, &msg, 0); + if (CHECK(n <=3D 0, "sendmsg", "failed\n")) { + log_err("failed to send echo reply"); + return -1; + } + } else { + /* Reply from original destination address. */ + fd =3D socket(dst_addr->ss_family, SOCK_DGRAM, 0); + if (CHECK(fd < 0, "socket", "failed\n")) { + log_err("failed to create tx socket"); + return -1; + } =20 - msg.msg_control =3D NULL; - msg.msg_controllen =3D 0; - n =3D sendmsg(fd, &msg, 0); - if (CHECK(n <=3D 0, "sendmsg", "failed\n")) { - log_err("failed to send echo reply"); - ret =3D -1; - goto out; - } + ret =3D bind(fd, (struct sockaddr *)dst_addr, sizeof(*dst_addr)); + if (CHECK(ret, "bind", "failed\n")) { + log_err("failed to bind tx socket"); + close(fd); + return ret; + } =20 - ret =3D 0; -out: - close(fd); - return ret; + msg.msg_control =3D NULL; + msg.msg_controllen =3D 0; + n =3D sendmsg(fd, &msg, 0); + if (CHECK(n <=3D 0, "sendmsg", "failed\n")) { + log_err("failed to send echo reply"); + close(fd); + return -1; + } + + close(fd); + } } =20 static int tcp_echo_test(int client_fd, int server_fd) @@ -454,14 +470,14 @@ static int tcp_echo_test(int client_fd, int server_fd) return 0; } =20 -static int udp_echo_test(int client_fd, int server_fd) +static int udp_echo_test(int client_fd, int server_fd, bool dont_bind_repl= y) { int err; =20 err =3D send_byte(client_fd); if (err) return -1; - err =3D udp_recv_send(server_fd); + err =3D udp_recv_send(server_fd, dont_bind_reply); if (err) return -1; err =3D recv_byte(client_fd); @@ -653,7 +669,7 @@ static void run_lookup_prog(const struct test *t) if (t->sotype =3D=3D SOCK_STREAM) tcp_echo_test(client_fd, server_fds[t->accept_on]); else - udp_echo_test(client_fd, server_fds[t->accept_on]); + udp_echo_test(client_fd, server_fds[t->accept_on], t->dont_bind_reply); =20 close(client_fd); close: @@ -775,6 +791,16 @@ static void test_redirect_lookup(struct test_sk_lookup= *skel) .listen_at =3D { INT_IP4, INT_PORT }, .accept_on =3D SERVER_B, }, + { + .desc =3D "UDP IPv4 redir different ports", + .lookup_prog =3D skel->progs.select_sock_a_no_reuseport, + .sock_map =3D skel->maps.redir_map, + .sotype =3D SOCK_DGRAM, + .connect_to =3D { EXT_IP4, EXT_PORT }, + .listen_at =3D { INT_IP4, INT_PORT }, + .accept_on =3D SERVER_A, + .dont_bind_reply =3D true, + }, { .desc =3D "UDP IPv4 redir and reuseport with conns", .lookup_prog =3D skel->progs.select_sock_a, --=20 2.34.1