Skip to contentSkip to conversation

Enable bpf_setsockopt() on ktls enabled sockets.

PATCH Superseded Kui-Feng Lee 3 participants 5 messages Jan 24, 2023 – Jan 25, 2023

Subsystems (5)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover Enable bpf_setsockopt() on ktls enabled sockets.

This patchset implements a change to bpf_setsockopt() which allows ktls enabled sockets to be used with the SOL_TCP level. This is necessary as when ktls is enabled, it changes the function pointer of setsockopt of the socket, which bpf_setsockopt() checks in order to make sure that the socket is a TCP socket. Checking sk_protocol instead of the function pointer will ensure that bpf_setsockopt() with the SOL_TCP level still works on sockets with ktls enabled.

The major differences from v1 are:
- Test with a IPv6 connect as well.
- Use ASSERT_OK()

v1: https://lore.kernel.org/bpf/[email protected]/

Kui-Feng Lee (2):
bpf: Check the protocol of a sock to agree the calls to
bpf_setsockopt().
selftests/bpf: Calls bpf_setsockopt() on a ktls enabled socket.

 net/core/filter.c                             |  2 +-
 .../selftests/bpf/prog_tests/setget_sockopt.c | 70 +++++++++++++++++++
 .../selftests/bpf/progs/setget_sockopt.c      |  8 +++
 3 files changed, 79 insertions(+), 1 deletion(-)
Signature · 1 line
2.30.2
#1 lore raw

PATCH v2 1/2 bpf: Check the protocol of a sock to agree the calls to bpf_setsockopt().

Resolve an issue when calling sol_tcp_sockopt() on a socket with ktls enabled. Prior to this patch, sol_tcp_sockopt() would only allow calls if the function pointer of setsockopt of the socket was set to tcp_setsockopt(). However, any socket with ktls enabled would have its function pointer set to tls_setsockopt(). To resolve this issue, the patch adds a check of the protocol of the linux socket and allows bpf_setsockopt() to be called if ktls is initialized on the linux socket. This ensures that calls to sol_tcp_sockopt() will succeed on sockets with ktls enabled.

---
 net/core/filter.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/core/filter.c +1 -1
index b4547a2c02f4..890384cbdeb2 100644
@@ -5204,7 +5204,7 @@ static int sol_tcp_sockopt(struct sock *sk, int optname,
 			   char *optval, int *optlen,
 			   bool getopt)
 {
-	if (sk->sk_prot->setsockopt != tcp_setsockopt)
+	if (sk->sk_protocol != IPPROTO_TCP)
 		return -EINVAL;
 
 	switch (optname) {
Signature · 1 line
2.30.2
#2 lore raw

PATCH v2 2/2 selftests/bpf: Calls bpf_setsockopt() on a ktls enabled socket.

In reply to Kui-Feng Lee

Ensures that whenever bpf_setsockopt() is called with the SOL_TCP option on a ktls enabled socket, the call will be accepted by the system. The provided test makes sure of this by performing an examination when the server side socket is in the CLOSE_WAIT state. At this stage, ktls is still enabled on the server socket and can be used to test if bpf_setsockopt() works correctly with linux.

---
 .../selftests/bpf/prog_tests/setget_sockopt.c | 70 +++++++++++++++++++
 .../selftests/bpf/progs/setget_sockopt.c      |  8 +++
 2 files changed, 78 insertions(+)
tools/testing/selftests/bpf/prog_tests/setget_sockopt.c +70 -0
index 018611e6b248..7bf3736b7286 100644
@@ -4,6 +4,7 @@
 #define _GNU_SOURCE
 #include <sched.h>
 #include <linux/socket.h>
+#include <linux/tls.h>
 #include <net/if.h>
 
 #include "test_progs.h"
@@ -83,6 +84,73 @@ static void test_udp(int family)
 	ASSERT_EQ(bss->nr_binddev, 1, "nr_bind");
 }
 
+static void test_ktls(int family)
+{
+	struct tls12_crypto_info_aes_gcm_128 aes128;
+	struct setget_sockopt__bss *bss = skel->bss;
+	int cfd = -1, sfd = -1, fd = -1, ret;
+
+	memset(bss, 0, sizeof(*bss));
+
+	sfd = start_server(family, SOCK_STREAM,
+			   family == AF_INET6 ? addr6_str : addr4_str, 0, 0);
+	if (!ASSERT_GE(sfd, 0, "start_server"))
+		return;
+	fd = connect_to_fd(sfd, 0);
+	if (!ASSERT_GE(fd, 0, "connect_to_fd"))
+		goto err_out;
+
+	cfd = accept(sfd, NULL, 0);
+	if (!ASSERT_GE(cfd, 0, "accept"))
+		goto err_out;
+
+	close(sfd);
+	sfd = -1;
+
+	/* Setup KTLS */
+	ret = setsockopt(fd, IPPROTO_TCP, TCP_ULP, "tls", sizeof("tls"));
+	if (!ASSERT_OK(ret, "setsockopt"))
+		goto err_out;
+	ret = setsockopt(cfd, IPPROTO_TCP, TCP_ULP, "tls", sizeof("tls"));
+	if (!ASSERT_OK(ret, "setsockopt"))
+		goto err_out;
+
+	memset(&aes128, 0, sizeof(aes128));
+	aes128.info.version = TLS_1_2_VERSION;
+	aes128.info.cipher_type = TLS_CIPHER_AES_GCM_128;
+
+	ret = setsockopt(fd, SOL_TLS, TLS_TX, &aes128, sizeof(aes128));
+	if (!ASSERT_OK(ret, "setsockopt"))
+		goto err_out;
+
+	ret = setsockopt(cfd, SOL_TLS, TLS_RX, &aes128, sizeof(aes128));
+	if (!ASSERT_OK(ret, "setsockopt"))
+		goto err_out;
+
+	/* KTLS is enabled */
+
+	close(fd);
+	/* At this point, the cfd socket is at the CLOSE_WAIT state
+	 * and still run TLS protocol.  The test for
+	 * BPF_TCP_CLOSE_WAIT should be run at this point.
+	 */
+	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");
+	ASSERT_EQ(bss->nr_fin_wait1, 1, "nr_fin_wait1");
+	return;
+
+err_out:
+	close(fd);
+	close(cfd);
+	close(sfd);
+}
+
 void test_setget_sockopt(void)
 {
 	cg_fd = test__join_cgroup(CG_NAME);
@@ -118,6 +186,8 @@ void test_setget_sockopt(void)
 	test_tcp(AF_INET);
 	test_udp(AF_INET6);
 	test_udp(AF_INET);
+	test_ktls(AF_INET6);
+	test_ktls(AF_INET);
 
 done:
 	setget_sockopt__destroy(skel);
tools/testing/selftests/bpf/progs/setget_sockopt.c +8 -0
index 9523333b8905..027d95755f9f 100644
@@ -6,6 +6,8 @@
 #include <bpf/bpf_core_read.h>
 #include <bpf/bpf_helpers.h>
 #include <bpf/bpf_tracing.h>
+#define BPF_PROG_TEST_TCP_HDR_OPTIONS
+#include "test_tcp_hdr_options.h"
 
 #ifndef ARRAY_SIZE
 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
@@ -22,6 +24,7 @@ int nr_active;
 int nr_connect;
 int nr_binddev;
 int nr_socket_post_create;
+int nr_fin_wait1;
 
 struct sockopt_test {
 	int opt;
@@ -386,6 +389,11 @@ int skops_sockopt(struct bpf_sock_ops *skops)
 		nr_passive += !(bpf_test_sockopt(skops, sk) ||
 				test_tcp_maxseg(skops, sk) ||
 				test_tcp_saved_syn(skops, sk));
+		set_hdr_cb_flags(skops, BPF_SOCK_OPS_STATE_CB_FLAG);
+		break;
+	case BPF_SOCK_OPS_STATE_CB:
+		if (skops->args[1] == BPF_TCP_CLOSE_WAIT)
+			nr_fin_wait1 += !bpf_test_sockopt(skops, sk);
 		break;
 	}
 
Signature · 1 line
2.30.2
#3 lore raw

Re: [PATCH bpf-next v2 2/2] selftests/bpf: Calls bpf_setsockopt() on a ktls enabled socket.

Quoted 40 lines · Kui-Feng Lee
tools/testing/selftests/bpf/prog_tests/setget_sockopt.c +40 -0
+static void test_ktls(int family)
+{
+	struct tls12_crypto_info_aes_gcm_128 aes128;
+	struct setget_sockopt__bss *bss = skel->bss;
+	int cfd = -1, sfd = -1, fd = -1, ret;
+
+	memset(bss, 0, sizeof(*bss));
+
+	sfd = start_server(family, SOCK_STREAM,
+			   family == AF_INET6 ? addr6_str : addr4_str, 0, 0);
+	if (!ASSERT_GE(sfd, 0, "start_server"))
+		return;
+	fd = connect_to_fd(sfd, 0);
+	if (!ASSERT_GE(fd, 0, "connect_to_fd"))
+		goto err_out;
+
+	cfd = accept(sfd, NULL, 0);
+	if (!ASSERT_GE(cfd, 0, "accept"))
+		goto err_out;
+
+	close(sfd);
+	sfd = -1;
+
+	/* Setup KTLS */
+	ret = setsockopt(fd, IPPROTO_TCP, TCP_ULP, "tls", sizeof("tls"));
+	if (!ASSERT_OK(ret, "setsockopt"))
+		goto err_out;
+	ret = setsockopt(cfd, IPPROTO_TCP, TCP_ULP, "tls", sizeof("tls"));
+	if (!ASSERT_OK(ret, "setsockopt"))
+		goto err_out;
+
+	memset(&aes128, 0, sizeof(aes128));
+	aes128.info.version = TLS_1_2_VERSION;
+	aes128.info.cipher_type = TLS_CIPHER_AES_GCM_128;
+
+	ret = setsockopt(fd, SOL_TLS, TLS_TX, &aes128, sizeof(aes128));
+	if (!ASSERT_OK(ret, "setsockopt"))
+		goto err_out;
+
+	ret = setsockopt(cfd, SOL_TLS, TLS_RX, &aes128, sizeof(aes128));

tools/testing/selftests/bpf/prog_tests/setget_sockopt.c static void test_udp(int family) · line 136

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/setget_sockopt.c +10 -0
+	if (!ASSERT_OK(ret, "setsockopt"))
+		goto err_out;
+
+	/* KTLS is enabled */
+
+	close(fd);
+	/* At this point, the cfd socket is at the CLOSE_WAIT state
+	 * and still run TLS protocol.  The test for
+	 * BPF_TCP_CLOSE_WAIT should be run at this point.
+	 */

Just came to my mind. I think it is better to ensure the cfd got the FIN first to avoid potential (unlikely) flaky test:

	ret = read(cfd, ...);
	ASSERT_EQ(ret, 0, ...);
Quoted 34 lines
tools/testing/selftests/bpf/prog_tests/setget_sockopt.c +19 -0
+	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");
+	ASSERT_EQ(bss->nr_fin_wait1, 1, "nr_fin_wait1");
+	return;
+
+err_out:
+	close(fd);
+	close(cfd);
+	close(sfd);
+}
+
  void test_setget_sockopt(void)
  {
  	cg_fd = test__join_cgroup(CG_NAME);
@@ -118,6 +186,8 @@ void test_setget_sockopt(void)
  	test_tcp(AF_INET);
  	test_udp(AF_INET6);
  	test_udp(AF_INET);
+	test_ktls(AF_INET6);
+	test_ktls(AF_INET);
 
  done:
  	setget_sockopt__destroy(skel);
tools/testing/selftests/bpf/progs/setget_sockopt.c +0 -0
index 9523333b8905..027d95755f9f 100644

tools/testing/selftests/bpf/progs/setget_sockopt.c line 10

Quoted 5 lines
tools/testing/selftests/bpf/progs/setget_sockopt.c +2 -0
@@ -6,6 +6,8 @@
  #include <bpf/bpf_core_read.h>
  #include <bpf/bpf_helpers.h>
  #include <bpf/bpf_tracing.h>
+#define BPF_PROG_TEST_TCP_HDR_OPTIONS
+#include "test_tcp_hdr_options.h"

Instead of having dependency on another test's header,

Quoted 12 lines
tools/testing/selftests/bpf/progs/setget_sockopt.c +1 -0
  #ifndef ARRAY_SIZE
  #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
@@ -22,6 +24,7 @@ int nr_active;
  int nr_connect;
  int nr_binddev;
  int nr_socket_post_create;
+int nr_fin_wait1;
 
  struct sockopt_test {
  	int opt;

tools/testing/selftests/bpf/progs/setget_sockopt.c int skops_sockopt(struct bpf_sock_ops *skops) · line 392

Quoted 4 lines
tools/testing/selftests/bpf/progs/setget_sockopt.c +1 -0
@@ -386,6 +389,11 @@ int skops_sockopt(struct bpf_sock_ops *skops)
  		nr_passive += !(bpf_test_sockopt(skops, sk) ||
  				test_tcp_maxseg(skops, sk) ||
  				test_tcp_saved_syn(skops, sk));
+		set_hdr_cb_flags(skops, BPF_SOCK_OPS_STATE_CB_FLAG);

how about directly doing this:

                bpf_sock_ops_cb_flags_set(skops,
                                          skops->bpf_sock_ops_cb_flags |
                                          BPF_SOCK_OPS_STATE_CB_FLAG);
Quoted 7 lines
tools/testing/selftests/bpf/progs/setget_sockopt.c +4 -0
+		break;
+	case BPF_SOCK_OPS_STATE_CB:
+		if (skops->args[1] == BPF_TCP_CLOSE_WAIT)
+			nr_fin_wait1 += !bpf_test_sockopt(skops, sk);
  		break;
  	}
 
#4 lore raw

Re: [PATCH bpf-next v2 2/2] selftests/bpf: Calls bpf_setsockopt() on a ktls enabled socket.

Quoted 1 line · Martin KaFai Lau

On 1/24/23 10:12 AM, Kui-Feng Lee wrote: index 9523333b8905..027d95755f9f 100644

.... skip ....

Quoted 22 lines

tools/testing/selftests/bpf/progs/setget_sockopt.c line 10

Quoted 8 lines
tools/testing/selftests/bpf/progs/setget_sockopt.c +2 -0
@@ -6,6 +6,8 @@
   #include <bpf/bpf_core_read.h>
   #include <bpf/bpf_helpers.h>
   #include <bpf/bpf_tracing.h>
+#define BPF_PROG_TEST_TCP_HDR_OPTIONS
+#include "test_tcp_hdr_options.h"

Instead of having dependency on another test's header,

Quoted 11 lines
tools/testing/selftests/bpf/progs/setget_sockopt.c +1 -0
     #ifndef ARRAY_SIZE
   #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
@@ -22,6 +24,7 @@ int nr_active;
   int nr_connect;
   int nr_binddev;
   int nr_socket_post_create;
+int nr_fin_wait1;
     struct sockopt_test {
       int opt;
@@ -386,6 +389,11 @@ int skops_sockopt(struct bpf_sock_ops *skops)

          nr_passive += !(bpf_test_sockopt(skops, sk) ||

Quoted 8 lines
Quoted 3 lines

                  test_tcp_maxseg(skops, sk) || test_tcp_saved_syn(skops, sk));
+        set_hdr_cb_flags(skops, BPF_SOCK_OPS_STATE_CB_FLAG);

how about directly doing this:
                bpf_sock_ops_cb_flags_set(skops,
skops->bpf_sock_ops_cb_flags |
BPF_SOCK_OPS_STATE_CB_FLAG);

Sure! It makes sense.

Quoted 8 lines
Quoted 7 lines

+        break;
+    case BPF_SOCK_OPS_STATE_CB:
+        if (skops->args[1] == BPF_TCP_CLOSE_WAIT) +            nr_fin_wait1 += !bpf_test_sockopt(skops, sk); break;
      }