Skip to contentSkip to conversation

mptcp: getsockopt: add support for IP_TOS

PATCH Superseded Florian Westphal 2 participants 4 messages Nov 22, 2021

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 mptcp: getsockopt: add support for IP_TOS

earlier patch added IP_TOS setsockopt support, this allows to get the value set by earlier setsockopt.

---
 net/mptcp/sockopt.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
net/mptcp/sockopt.c +33 -0
index 11cda8629993..5626b49c0270 100644
@@ -1066,6 +1066,24 @@ static int mptcp_put_int_option(struct mptcp_sock *msk, char __user *optval,
 	return 0;
 }
 
+static int mptcp_put_int8_option(struct mptcp_sock *msk, char __user *optval,
+				 int __user *optlen, u8 val)
+{
+	int len;
+
+	if (get_user(len, optlen))
+		return -EFAULT;
+
+	if (len) {
+		if (put_user(1, optlen))
+			return -EFAULT;
+		if (copy_to_user(optval, &val, 1))
+			return -EFAULT;
+	}
+
+	return 0;
+}
+
 static int mptcp_getsockopt_sol_tcp(struct mptcp_sock *msk, int optname,
 				    char __user *optval, int __user *optlen)
 {
@@ -1082,6 +1100,19 @@ static int mptcp_getsockopt_sol_tcp(struct mptcp_sock *msk, int optname,
 	return -EOPNOTSUPP;
 }
 
+static int mptcp_getsockopt_v4(struct mptcp_sock *msk, int optname,
+			       char __user *optval, int __user *optlen)
+{
+	struct sock *sk = (void *)msk;
+
+	switch (optname) {
+	case IP_TOS:
+		return mptcp_put_int8_option(msk, optval, optlen, inet_sk(sk)->tos);
+	}
+
+	return -EOPNOTSUPP;
+}
+
 static int mptcp_getsockopt_sol_mptcp(struct mptcp_sock *msk, int optname,
 				      char __user *optval, int __user *optlen)
 {
@@ -1117,6 +1148,8 @@ int mptcp_getsockopt(struct sock *sk, int level, int optname,
 	if (ssk)
 		return tcp_getsockopt(ssk, level, optname, optval, option);
 
+	if (level == SOL_IP)
+		return mptcp_getsockopt_v4(msk, optname, optval, option);
 	if (level == SOL_TCP)
 		return mptcp_getsockopt_sol_tcp(msk, optname, optval, option);
 	if (level == SOL_MPTCP)
Signature · 1 line
2.32.0
#1 lore raw

PATCH 2/2 selftests: mptcp: check IP_TOS in/out are the same

Check that getsockopt(IP_TOS) returns what setsockopt(IP_TOS) did set right before.

---
 .../selftests/net/mptcp/mptcp_sockopt.c       | 45 +++++++++++++++++++
 1 file changed, 45 insertions(+)
tools/testing/selftests/net/mptcp/mptcp_sockopt.c +45 -0
index 417b11cafafe..87ed6d050e67 100644
@@ -4,6 +4,7 @@
 
 #include <assert.h>
 #include <errno.h>
+#include <fcntl.h>
 #include <limits.h>
 #include <string.h>
 #include <stdarg.h>
@@ -594,6 +595,30 @@ static int server(int pipefd)
 	return 0;
 }
 
+static void test_ip_tos_sockopt(int fd)
+{
+	uint8_t tos_in, tos_out;
+	socklen_t s;
+	int r;
+
+	tos_in = rand() & 0xfc;
+	r = setsockopt(fd, SOL_IP, IP_TOS, &tos_in, sizeof(tos_out));
+	if (r != 0)
+		die_perror("setsockopt IP_TOS");
+
+	tos_out = 0;
+	s = sizeof(tos_out);
+	r = getsockopt(fd, SOL_IP, IP_TOS, &tos_out, &s);
+	if (r != 0)
+		die_perror("getsockopt IP_TOS");
+
+	if (tos_in != tos_out)
+		xerror("tos %x != %x\n", tos_in, tos_out);
+
+	if (s != 1)
+		xerror("tos should be 1 byte");
+}
+
 static int client(int pipefd)
 {
 	int fd = -1;
@@ -611,6 +636,8 @@ static int client(int pipefd)
 		xerror("Unknown pf %d\n", pf);
 	}
 
+	test_ip_tos_sockopt(fd);
+
 	connect_one_server(fd, pipefd);
 
 	return 0;
@@ -642,6 +669,22 @@ static int rcheck(int wstatus, const char *what)
 	return 111;
 }
 
+static void init_rng(void)
+{
+	int fd = open("/dev/urandom", O_RDONLY);
+	unsigned int foo;
+
+	if (fd > 0) {
+		int ret = read(fd, &foo, sizeof(foo));
+
+		if (ret < 0)
+			srand(fd + foo);
+		close(fd);
+	}
+
+	srand(foo);
+}
+
 int main(int argc, char *argv[])
 {
 	int e1, e2, wstatus;
@@ -650,6 +693,8 @@ int main(int argc, char *argv[])
 
 	parse_opts(argc, argv);
 
+	init_rng();
+
 	e1 = pipe(pipefds);
 	if (e1 < 0)
 		die_perror("pipe");
Signature · 1 line
2.32.0
#2 lore raw

Re: [PATCH mptcp-next 2/2] selftests: mptcp: check IP_TOS in/out are the same

Quoted 2 lines · Florian Westphal

Check that getsockopt(IP_TOS) returns what setsockopt(IP_TOS) did set right before.

Thank you for adding the new selftest!

Quoted 1 line

+static void init_rng(void)

That's really a detail but this function in the other MPTCP selftests always intrigued me. Do you mind me asking a few questions about it? :)

tools/testing/selftests/net/mptcp/mptcp_sockopt.c static int rcheck(int wstatus, const char *what) · line 677

Quoted 5 lines
tools/testing/selftests/net/mptcp/mptcp_sockopt.c +5 -0
+{
+	int fd = open("/dev/urandom", O_RDONLY);
+	unsigned int foo;
+
+	if (fd > 0) {

- Here, we do more if 'open()' succeeds or fails (0 is still a valid fd). I guess it is unlikely to have 0 here because we don't close fd 0 elsewhere if I'm not mistaken.

- if open() "fails", 'foo' will be used later while undefined. Which is fine in this context but static code analysers might complain.

tools/testing/selftests/net/mptcp/mptcp_sockopt.c static int rcheck(int wstatus, const char *what) · line 681

Quoted 4 lines
tools/testing/selftests/net/mptcp/mptcp_sockopt.c +4 -0
+		int ret = read(fd, &foo, sizeof(foo));
+
+		if (ret < 0)
+			srand(fd + foo);

- Here we do this if 'read()' fails with an undefined 'foo'. Again it is fine in this context but strange :)

tools/testing/selftests/net/mptcp/mptcp_sockopt.c static int rcheck(int wstatus, const char *what) · line 682

Quoted 1 line
tools/testing/selftests/net/mptcp/mptcp_sockopt.c +1 -0
+		close(fd);

- should not happen: the 'fd' might not be closed if fd == 0.

tools/testing/selftests/net/mptcp/mptcp_sockopt.c static int rcheck(int wstatus, const char *what) · line 685

Quoted 3 lines
tools/testing/selftests/net/mptcp/mptcp_sockopt.c +3 -0
+	}
+
+	srand(foo);

The function looks strange but I don't see issue in this context. Anyway, why did we not use the typical:

srand(time(NULL))

Cheers,
Matt

Signature · 2 lines
Tessares | Belgium | Hybrid Access Solutions
www.tessares.net
#3 lore raw
In reply to Florian Westphal

Hi Florian,

Quoted 2 lines · Florian Westphal

earlier patch added IP_TOS setsockopt support, this allows to get the value set by earlier setsockopt.

Thank you for sharing the patches, good idea to add this!

Quoted 7 lines
net/mptcp/sockopt.c +0 -0
index 11cda8629993..5626b49c0270 100644
@@ -1066,6 +1066,24 @@ static int mptcp_put_int_option(struct mptcp_sock *msk, char __user *optval,
 	return 0;
 }

net/mptcp/sockopt.c static int mptcp_put_int_option(struct mptcp_sock *msk, char __user *optval, · line 1077

Quoted 10 lines
net/mptcp/sockopt.c +9 -0
 
+static int mptcp_put_int8_option(struct mptcp_sock *msk, char __user *optval,
+				 int __user *optlen, u8 val)
+{
+	int len;
+
+	if (get_user(len, optlen))
+		return -EFAULT;
+
+	if (len) {

What can be the possible values of 'len' here? Can it be negative?

I'm asking this question because when I'm looking at similar code in mptcp/sockopt.c, I see different behaviours:

- mptcp_getsockopt_info(): no additional check but we suppose 'len' is unsigned

- mptcp_put_int_option(): same but we return an error if len < 0

- mptcp_get_subflow_data(): we check if len is not < the expected size

In ipv4/ip_sockglue.c, we check if len < 0

I don't know if we should uniform that and always do:

if (len < 0 (or expected value))
return (...);

But I'm fine with the current version if we would never have a negative value.

Cheers,
Matt

Signature · 2 lines
Tessares | Belgium | Hybrid Access Solutions
www.tessares.net