From nobody Fri Nov 29 21:00:26 2024 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) (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 8FB181CF2A9 for ; Sat, 14 Sep 2024 10:32:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726309969; cv=none; b=nS5Hiu6Xult9h1urtrGk2zWUU/Mx2HiRzTg//zxuVbjLHtuHabNdxswf+TSZ+hXbYb2H99VAA1e9WVczWLJyJ/7J+mnWdMpufsHirXlft0WszkZK3Ytwlj/bUHz+kSBncfa4p0Q5DJHH+/6AqEmURkYxfrfAeyCmh1oE+S2egvI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726309969; c=relaxed/simple; bh=s8DsojteGWQbIibiQeeN+V7NoCWk/i240ms0OBizBQ8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=p0XEMmhlOCCAJTRv46OLVJvLGf+GKHca9qbhiveH7CwDIwCtqYtRLGAiYZIdF4vDQVwrpxaCEXvURywyWYCYxJ5948vS5gB6Ld6XANp+MSL/7x8zpJ9s29/87l0JJ1m9DTZ1CUPEGliE/WxsyeMRnm0+3keRYXMP4qOs2siI9yk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=EK7LQ/Is; arc=none smtp.client-ip=209.85.210.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="EK7LQ/Is" Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-718e1ce7e84so2183034b3a.1 for ; Sat, 14 Sep 2024 03:32:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1726309968; x=1726914768; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=NjQqPR/O4cbkW/hD684XD91wFosDYzncY6/VLXc3j6E=; b=EK7LQ/IsdjLG9d2TMkz8SfAv2K5yE9ioe5VbSQF8+DBgw8FKj28CpsAtJUh7W50Ezf mZyRcNhHMSLpbOhBiCq0F7bkIzQqp/SIEyAooSFgBuOWmTl16WLzEOf1W4v5im31g8rG Xll4+71pNd45RcIO6UTQ3lblh7OjGCaGWq3MQ0HMkd5JL0csxtKOlT92AEJi2ltsZ1+2 TSN7+Ie2zLF/BheM8brRcF5HtF/RxfPqu/5aGxKNWql8j2RBKSYtB8S1BNp19I5zZ8EV Sz6WH4Jl0ejC3YUAHvwWxogPl5Wnk0Cavy5cOhjTvHudDmgoLAmR99bQnnaJGnt1//Do bGpg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726309968; x=1726914768; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=NjQqPR/O4cbkW/hD684XD91wFosDYzncY6/VLXc3j6E=; b=XP5oA1nAhFpk33S+L0f9qIudrfdAqpHYvkUP0qbFE67LnRicoRA05JTyzrwHe431e2 Wn1X8B0+kBDWgiH7EroWvvJc5YW2aW91hGP/OFuXzNRLfexaKBR4jMwu2+0+lhR649sV ZjwqtPMFC/u24U+FreiT+bJJbs7/GJleY6T+yz/N9Ynl2BSHgQCxpPXAtrW7ignDb3kS kA9q8UicPP1NDbZmcEMMM+mZpjXbwwq19yiksBX1ZtAX+rQ3FOh6SixkkFOKdhZgj+IO gXBXkA1oGSTnPj4FSS7dOQ4dhbe0Yzp9w2gdt7ebPc7PtFJqYL3ojflFuy2sabtn28+Y eVoQ== X-Forwarded-Encrypted: i=1; AJvYcCWjL6y2UvP18ON2W/OHkSqVFJmE7USrrPbdMVchk+RodVGVI54tXGRrXJQXxoowC0wrx+QH5NOgL82Q/+Q=@vger.kernel.org X-Gm-Message-State: AOJu0YwSk5Oam1uJ0UbBL72YPlQz2gEgmFlZdx1VfdK9E4ty24Kua3Oy pGo5RYoaomzenZZvyGAxLc/121UdgmI3+I5ykre469n80ua4zIBJOFY5PLVbLMw= X-Google-Smtp-Source: AGHT+IE3UauTZ/385KdWRCwcFSIlvJxIZ90AhgKMFaRWwNdUkCqO1ooNd68vWpjxwmGHl3ODnkxKfg== X-Received: by 2002:a05:6a20:c916:b0:1d0:45c2:8140 with SMTP id adf61e73a8af0-1d045c2821fmr12659050637.18.1726309967883; Sat, 14 Sep 2024 03:32:47 -0700 (PDT) Received: from C02F52LSML85.bytedance.net ([203.208.167.149]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71944ab50cbsm788332b3a.53.2024.09.14.03.32.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 14 Sep 2024 03:32:47 -0700 (PDT) From: Feng zhou To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, mykolal@fb.com, shuah@kernel.org, alan.maguire@oracle.com Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, yangzhenze@bytedance.com, wangdongdong.6@bytedance.com, zhoufeng.zf@bytedance.com Subject: [PATCH bpf-next v3 1/2] bpf: Fix bpf_get/setsockopt to tos not take effect when TCP over IPv4 via INET6 API Date: Sat, 14 Sep 2024 18:32:25 +0800 Message-Id: <20240914103226.71109-2-zhoufeng.zf@bytedance.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20240914103226.71109-1-zhoufeng.zf@bytedance.com> References: <20240914103226.71109-1-zhoufeng.zf@bytedance.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Feng Zhou when TCP over IPv4 via INET6 API, bpf_get/setsockopt with ipv4 will fail, because sk->sk_family is AF_INET6. With ipv6 will success, not take effect, because inet_csk(sk)->icsk_af_ops is ipv6_mapped and use ip_queue_xmit, inet_sk(sk)->tos. Bpf_get/setsockopt use sk_is_inet() helper to fix this case. Signed-off-by: Feng Zhou Reviewed-by: Eric Dumazet --- net/core/filter.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/net/core/filter.c b/net/core/filter.c index e4a4454df5f9..90f4dbb8d2b5 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -5399,7 +5399,12 @@ static int sol_ip_sockopt(struct sock *sk, int optna= me, char *optval, int *optlen, bool getopt) { - if (sk->sk_family !=3D AF_INET) + + /* + * SOL_IP socket options are available on AF_INET and AF_INET6, for + * example, TCP over IPv4 via INET6 API. + */ + if (!sk_is_inet(sk)) return -EINVAL; =20 switch (optname) { --=20 2.30.2 From nobody Fri Nov 29 21:00:26 2024 Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.177]) (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 229E11CE6E1 for ; Sat, 14 Sep 2024 10:32:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726309978; cv=none; b=p+BDsMZjSMALN0FzFO3bhHXeTTT/8rvAvjX8e4vARSveob+QcBw4RsQHBbn0Czm8zVumm9EqnUu4NEhS+Me0KjrprUsSiBucmA9i3xqLxstUdF8IREXqBn1Y+PZWyliXklKcwAigBVBDSgybk7DGSUR7xWEktLvBsp/s2IDaA7Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726309978; c=relaxed/simple; bh=m6tganteey5Zg+ZqOOxN3LGGrsmpJmws608jYsU4v9Q=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Fev0wg+8eAwtSLKpH+KIRJtjrSblYYDqBsj6ZZV/j0m+n0PITR+Evuqi/w/vo0cxFQnF7WoRUvi6KfPXDJ1JeBiueM4pLmFhSALmrT3Z3yzpT+Zm2avAOH4RSzEBUg/KC5fGOfraBFKVkZOCdVHtmOP5W6g4npCerWJXRbaHrSw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=YRkBn9Ss; arc=none smtp.client-ip=209.85.210.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="YRkBn9Ss" Received: by mail-pf1-f177.google.com with SMTP id d2e1a72fcca58-718e2855479so2194904b3a.1 for ; Sat, 14 Sep 2024 03:32:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1726309976; x=1726914776; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=2wKJ6T8lzrG3Rgs6AhFQqgnkeWhWEchyLf0I6JJ+l9w=; b=YRkBn9Ssnea4wzVsgFxbSa6SpUnr31dRWq86B8ztJytn/Zben3too/SnZLkaWpjD55 KPvjtuZ1xSRSVhEliXtzzLgYfMUz0CQRsrDBwxScQ3lqCCzkUDMhLQnAaWf3UwKp8y5a IhZaQ2pKutl0fdD0cYKS/UyZ46OiWRafrp26utdmDy5CvsVBoCRm6i2i3n9WHf/jkzCp /QkoJ7ZrRRe30lN03gEEC/zXO0pIsPcJWrbAlzbpy57kJEXpKF+dHDWRlLUzlZRULGms qMzKnYfDSx0W2RvOpHMVlKrh96pvhHIwKK4Q1ux0OzFT6IOV/N2LXGVSxy9zG1zKZsDG U4Cg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726309976; x=1726914776; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2wKJ6T8lzrG3Rgs6AhFQqgnkeWhWEchyLf0I6JJ+l9w=; b=UgGmFxGSzJqrAP8O+PxuDmn2B/z7tLlV3Wm2GgY9JlL+Zz4WrwUv3pYtsVcUrIs2C0 h7wHJwSNr2l5rogCzUftRlEzpTPfsDODXCzKADfWTU3LQlzxCGILD2OyUjQ/6La5F8y0 Cv6Fv1Vsu5EBt6JIdvjgibqDDnBdJMjdoxNHDqAMDcUIvaFG0fAUv4VTUuqV23c73WD+ NXwmYPR+m5m69pCebYjCNn4iMGO7igf4e2BAnAlktDhSXFeojchR8hmFxK/uNZBDuTpo nXobKtBsbd0n/WZ570oQ4U6n3hgO2yluZMk8H41xOE5Fw0/07l+l+1SjQkGT0FkMmPTz uAHg== X-Forwarded-Encrypted: i=1; AJvYcCVgUcrPDgAclTYu7iYr0OeqsUzrFwvaU9UX5NvMfY9FH/s2JVBZ7y6V6Y5qlT29Zf6eGWE9sLUH+SmwcEs=@vger.kernel.org X-Gm-Message-State: AOJu0Yx7eT/QvRnbBpbtJORid0nr1Qv8Nb2GdXjQHjdmlrQ1Dt60apsQ zRG3c3cb3q5+5FokR5DHwno3S946eudpWc4rmE0jLSQnqkWwhm9DLqycuKuzg8s= X-Google-Smtp-Source: AGHT+IF8FZ62F3t14kxbTqWQfHm1EgOf2TO1sWKDgesgw76UlXpQC6XKw5Ig4N3rdi4YJTYevfZbNg== X-Received: by 2002:a05:6a00:812:b0:70d:1b48:e362 with SMTP id d2e1a72fcca58-719262065e5mr13240001b3a.26.1726309976169; Sat, 14 Sep 2024 03:32:56 -0700 (PDT) Received: from C02F52LSML85.bytedance.net ([203.208.167.149]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71944ab50cbsm788332b3a.53.2024.09.14.03.32.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 14 Sep 2024 03:32:55 -0700 (PDT) From: Feng zhou To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, mykolal@fb.com, shuah@kernel.org, alan.maguire@oracle.com Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, yangzhenze@bytedance.com, wangdongdong.6@bytedance.com, zhoufeng.zf@bytedance.com Subject: [PATCH bpf-next v3 2/2] selftests/bpf: Setget_sockopt add a test for tcp over ipv4 via ipv6 Date: Sat, 14 Sep 2024 18:32:26 +0800 Message-Id: <20240914103226.71109-3-zhoufeng.zf@bytedance.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20240914103226.71109-1-zhoufeng.zf@bytedance.com> References: <20240914103226.71109-1-zhoufeng.zf@bytedance.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Feng Zhou This patch adds a test for TCP over IPv4 via INET6 API. Signed-off-by: Feng Zhou --- .../selftests/bpf/prog_tests/setget_sockopt.c | 33 +++++++++++++++++++ .../selftests/bpf/progs/setget_sockopt.c | 13 ++++++-- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/setget_sockopt.c b/tool= s/testing/selftests/bpf/prog_tests/setget_sockopt.c index 7d4a9b3d3722..3cad92128e60 100644 --- a/tools/testing/selftests/bpf/prog_tests/setget_sockopt.c +++ b/tools/testing/selftests/bpf/prog_tests/setget_sockopt.c @@ -15,8 +15,11 @@ =20 #define CG_NAME "/setget-sockopt-test" =20 +#define INT_PORT 8008 + static const char addr4_str[] =3D "127.0.0.1"; static const char addr6_str[] =3D "::1"; +static const char addr6_any_str[] =3D "::"; static struct setget_sockopt *skel; static int cg_fd; =20 @@ -67,6 +70,35 @@ static void test_tcp(int family) ASSERT_EQ(bss->nr_binddev, 2, "nr_bind"); } =20 +static void test_tcp_over_ipv4_via_ipv6(void) +{ + struct setget_sockopt__bss *bss =3D skel->bss; + int sfd, cfd; + + memset(bss, 0, sizeof(*bss)); + skel->bss->test_tcp_over_ipv4_via_ipv6 =3D 1; + + sfd =3D start_server(AF_INET6, SOCK_STREAM, + addr6_any_str, INT_PORT, 0); + if (!ASSERT_GE(sfd, 0, "start_server")) + return; + + cfd =3D connect_to_addr_str(AF_INET, SOCK_STREAM, addr4_str, INT_PORT, NU= LL); + if (!ASSERT_GE(cfd, 0, "connect_to_addr_str")) { + close(sfd); + return; + } + close(sfd); + close(cfd); + + ASSERT_EQ(bss->nr_listen, 1, "nr_listen"); + ASSERT_EQ(bss->nr_connect, 1, "nr_connect"); + ASSERT_EQ(bss->nr_active, 1, "nr_active"); + ASSERT_EQ(bss->nr_passive, 1, "nr_passive"); + ASSERT_EQ(bss->nr_socket_post_create, 2, "nr_socket_post_create"); + ASSERT_EQ(bss->nr_binddev, 2, "nr_bind"); +} + static void test_udp(int family) { struct setget_sockopt__bss *bss =3D skel->bss; @@ -191,6 +223,7 @@ void test_setget_sockopt(void) test_udp(AF_INET); test_ktls(AF_INET6); test_ktls(AF_INET); + test_tcp_over_ipv4_via_ipv6(); =20 done: setget_sockopt__destroy(skel); diff --git a/tools/testing/selftests/bpf/progs/setget_sockopt.c b/tools/tes= ting/selftests/bpf/progs/setget_sockopt.c index 60518aed1ffc..ff834d94dd23 100644 --- a/tools/testing/selftests/bpf/progs/setget_sockopt.c +++ b/tools/testing/selftests/bpf/progs/setget_sockopt.c @@ -20,6 +20,7 @@ int nr_connect; int nr_binddev; int nr_socket_post_create; int nr_fin_wait1; +int test_tcp_over_ipv4_via_ipv6; =20 struct sockopt_test { int opt; @@ -262,9 +263,15 @@ static int bpf_test_sockopt(void *ctx, struct sock *sk) if (n !=3D ARRAY_SIZE(sol_ip_tests)) return -1; } else { - n =3D bpf_loop(ARRAY_SIZE(sol_ipv6_tests), bpf_test_ipv6_sockopt, &lc, 0= ); - if (n !=3D ARRAY_SIZE(sol_ipv6_tests)) - return -1; + if (test_tcp_over_ipv4_via_ipv6) { + n =3D bpf_loop(ARRAY_SIZE(sol_ip_tests), bpf_test_ip_sockopt, &lc, 0); + if (n !=3D ARRAY_SIZE(sol_ip_tests)) + return -1; + } else { + n =3D bpf_loop(ARRAY_SIZE(sol_ipv6_tests), bpf_test_ipv6_sockopt, &lc, = 0); + if (n !=3D ARRAY_SIZE(sol_ipv6_tests)) + return -1; + } } =20 return 0; --=20 2.30.2