Skip to contentSkip to conversation

syscalls/setsockopt05: associate receiver with destination address

PATCH New Jan Stancek 2 participants 14 messages Aug 24, 2020 – Aug 27, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 syscalls/setsockopt05: associate receiver with destination address

to avoid sporadic ECONNREFUSED errors:
safe_net.c:202: BROK: setsockopt05.c:70: send(6, 0x3ffcaf7d828, 4000, 32768) failed: ECONNREFUSED (111)

---
 testcases/kernel/syscalls/setsockopt/setsockopt05.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
testcases/kernel/syscalls/setsockopt/setsockopt05.c +3 -2
index e78ef236e337..469e5a64bf71 100644
@@ -37,6 +37,7 @@ static void setup(void)
 	int real_uid = getuid();
 	int real_gid = getgid();
 	int sock;
+	int port = TST_GET_UNUSED_PORT(AF_INET, SOCK_DGRAM);
 	struct ifreq ifr;
 
 	SAFE_UNSHARE(CLONE_NEWUSER);
@@ -45,14 +46,14 @@ static void setup(void)
 	SAFE_FILE_PRINTF("/proc/self/uid_map", "0 %d 1", real_uid);
 	SAFE_FILE_PRINTF("/proc/self/gid_map", "0 %d 1", real_gid);
 
-	tst_init_sockaddr_inet_bin(&addr, INADDR_LOOPBACK, 12345);
+	tst_init_sockaddr_inet_bin(&addr, INADDR_LOOPBACK, port);
 	sock = SAFE_SOCKET(AF_INET, SOCK_DGRAM, 0);
 	strcpy(ifr.ifr_name, "lo");
 	ifr.ifr_mtu = 1500;
 	SAFE_IOCTL(sock, SIOCSIFMTU, &ifr);
 	ifr.ifr_flags = IFF_UP;
 	SAFE_IOCTL(sock, SIOCSIFFLAGS, &ifr);
-	SAFE_CLOSE(sock);
+	SAFE_BIND(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr));
 }
 
 static void run(void)
Signature · 1 line
2.18.1
#1 lore raw

PATCH 1/1 syscalls/setsockopt05: associate receiver with destination address

Hi,

Quoted 21 lines · Jan Stancek

to avoid sporadic ECONNREFUSED errors:
safe_net.c:202: BROK: setsockopt05.c:70: send(6, 0x3ffcaf7d828, 4000, 32768) failed: ECONNREFUSED (111)

---
 testcases/kernel/syscalls/setsockopt/setsockopt05.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
testcases/kernel/syscalls/setsockopt/setsockopt05.c +1 -0
index e78ef236e337..469e5a64bf71 100644
@@ -37,6 +37,7 @@ static void setup(void)
 	int real_uid = getuid();
 	int real_gid = getgid();
 	int sock;
+	int port = TST_GET_UNUSED_PORT(AF_INET, SOCK_DGRAM);
 	struct ifreq ifr;
 
 	SAFE_UNSHARE(CLONE_NEWUSER);

testcases/kernel/syscalls/setsockopt/setsockopt05.c static void setup(void) · line 49

Quoted 5 lines
testcases/kernel/syscalls/setsockopt/setsockopt05.c +1 -1
@@ -45,14 +46,14 @@ static void setup(void)
 	SAFE_FILE_PRINTF("/proc/self/uid_map", "0 %d 1", real_uid);
 	SAFE_FILE_PRINTF("/proc/self/gid_map", "0 %d 1", real_gid);
 
-	tst_init_sockaddr_inet_bin(&addr, INADDR_LOOPBACK, 12345);
+	tst_init_sockaddr_inet_bin(&addr, INADDR_LOOPBACK, port);

Please don't use TST_GET_UNUSED_PORT() this way. The correct way to do this is to set port to 0 and then read the address back using SAFE_GETSOCKNAME() after bind(). It's the same amount of code but without any race conditions.

testcases/kernel/syscalls/setsockopt/setsockopt05.c static void setup(void) · line 55

Quoted 7 lines
testcases/kernel/syscalls/setsockopt/setsockopt05.c +0 -1
 	sock = SAFE_SOCKET(AF_INET, SOCK_DGRAM, 0);
 	strcpy(ifr.ifr_name, "lo");
 	ifr.ifr_mtu = 1500;
 	SAFE_IOCTL(sock, SIOCSIFMTU, &ifr);
 	ifr.ifr_flags = IFF_UP;
 	SAFE_IOCTL(sock, SIOCSIFFLAGS, &ifr);
-	SAFE_CLOSE(sock);

Don't forget to close the socket in cleanup().

testcases/kernel/syscalls/setsockopt/setsockopt05.c static void setup(void) · line 59

Quoted 5 lines
testcases/kernel/syscalls/setsockopt/setsockopt05.c +1 -0
+	SAFE_BIND(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr));
 }
 
 static void run(void)

Though I wonder whether setsockopt(SO_NO_CHECK, 1) is really supposed to flush the partial packet. Are you sure it's not a bug in the kernel?

Signature · 7 lines
Martin Doucha   [email protected]
QA Engineer for Software Maintenance
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic
#2 lore raw

PATCH 1/1 syscalls/setsockopt05: associate receiver with destination address

----- Original Message -----

Quoted 29 lines

Hi,

Quoted 26 lines · Jan Stancek

to avoid sporadic ECONNREFUSED errors:
safe_net.c:202: BROK: setsockopt05.c:70: send(6, 0x3ffcaf7d828, 4000, 32768) failed: ECONNREFUSED (111)

---
 testcases/kernel/syscalls/setsockopt/setsockopt05.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
testcases/kernel/syscalls/setsockopt/setsockopt05.c +1 -0
index e78ef236e337..469e5a64bf71 100644
@@ -37,6 +37,7 @@ static void setup(void)
 	int real_uid = getuid();
 	int real_gid = getgid();
 	int sock;
+	int port = TST_GET_UNUSED_PORT(AF_INET, SOCK_DGRAM);
 	struct ifreq ifr;
 
 	SAFE_UNSHARE(CLONE_NEWUSER);
@@ -45,14 +46,14 @@ static void setup(void)
 	SAFE_FILE_PRINTF("/proc/self/uid_map", "0 %d 1", real_uid);
 	SAFE_FILE_PRINTF("/proc/self/gid_map", "0 %d 1", real_gid);
 
Quoted 7 lines

testcases/kernel/syscalls/setsockopt/setsockopt05.c static void setup(void) · line 49

Quoted 2 lines
testcases/kernel/syscalls/setsockopt/setsockopt05.c +1 -1
-	tst_init_sockaddr_inet_bin(&addr, INADDR_LOOPBACK, 12345);
+	tst_init_sockaddr_inet_bin(&addr, INADDR_LOOPBACK, port);

Please don't use TST_GET_UNUSED_PORT() this way. The correct way to do this is to set port to 0 and then read the address back using SAFE_GETSOCKNAME() after bind(). It's the same amount of code but without any race conditions.

Fair point.

Quoted 11 lines

testcases/kernel/syscalls/setsockopt/setsockopt05.c static void setup(void) · line 55

Quoted 8 lines
testcases/kernel/syscalls/setsockopt/setsockopt05.c +0 -1
 	sock = SAFE_SOCKET(AF_INET, SOCK_DGRAM, 0);
 	strcpy(ifr.ifr_name, "lo");
 	ifr.ifr_mtu = 1500;
 	SAFE_IOCTL(sock, SIOCSIFMTU, &ifr);
 	ifr.ifr_flags = IFF_UP;
 	SAFE_IOCTL(sock, SIOCSIFFLAGS, &ifr);
-	SAFE_CLOSE(sock);

Don't forget to close the socket in cleanup().

Quoted 8 lines

testcases/kernel/syscalls/setsockopt/setsockopt05.c static void setup(void) · line 59

Quoted 5 lines
testcases/kernel/syscalls/setsockopt/setsockopt05.c +1 -0
+	SAFE_BIND(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr));
 }
 
 static void run(void)

Though I wonder whether setsockopt(SO_NO_CHECK, 1) is really supposed to flush the partial packet. Are you sure it's not a bug in the kernel?

I assumed it's from previous one, not partial one. From man(7) udp:

ECONNREFUSED
No receiver was associated with the destination address. This might be caused by a previous packet sent over the socket.

#3 lore raw

PATCH 1/1 syscalls/setsockopt05: associate receiver with destination address

Quoted 6 lines · Jan Stancek

I assumed it's from previous one, not partial one. From man(7) udp:

ECONNREFUSED
No receiver was associated with the destination address. This might be caused by a previous packet sent over the socket.

Wait, looking closer at the error message, it looks like send(MSG_MORE) is broken on your test kernel. Let me quote the man page here:

Quoted 11 lines

MSG_MORE (since Linux 2.4.4)
The caller has more data to send. This flag is used with TCP sockets to obtain the same effect as the TCP_CORK socket option (see tcp(7)), with the difference that this flag can be set on a per-call basis.

Since Linux 2.6, this flag is also supported for UDP sockets, and informs the kernel to package all of the data sent in calls with this flag set into a single datagram which is transmitted only when a call is performed that does not specify this flag. (See also the UDP_CORK socket option described in udp(7).)

The data from the SAFE_SEND() call are supposed to stay in kernel buffer until the send() call where we (intentionally) ignore the return value.

Signature · 7 lines
Martin Doucha   [email protected]
QA Engineer for Software Maintenance
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic
#4 lore raw

PATCH 1/1 syscalls/setsockopt05: associate receiver with destination address

----- Original Message -----

Quoted 11 lines · Jan Stancek
Quoted 7 lines

I assumed it's from previous one, not partial one. From man(7) udp:

ECONNREFUSED
No receiver was associated with the destination address. This might be caused by a previous packet sent over the socket.

Wait, looking closer at the error message, it looks like send(MSG_MORE) is broken on your test kernel. Let me quote the man page here:

Aren't we getting propagated ICMP error to send() from previous iteration of the loop?

Per https://tools.ietf.org/html/rfc1122#page-78
The application is also responsible to avoid confusion from a delayed ICMP error message resulting from an earlier use of the same port(s).

#5 lore raw

PATCH 1/1 syscalls/setsockopt05: associate receiver with destination address

Quoted 6 lines · Jan Stancek

Aren't we getting propagated ICMP error to send() from previous iteration of the loop?

Per https://tools.ietf.org/html/rfc1122#page-78
The application is also responsible to avoid confusion from a delayed ICMP error message resulting from an earlier use of the same port(s).

The test is using the loopback address. There should be no delay. We're running this test daily on 7 different kernel versions and 4 different archs and I've never seen it fail with ECONNREFUSED, ever.

But you can prove me wrong by adding a few debug prints around both send() calls.

Signature · 7 lines
Martin Doucha   [email protected]
QA Engineer for Software Maintenance
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic
#6 lore raw

PATCH 1/1 syscalls/setsockopt05: associate receiver with destination address

----- Original Message -----

Quoted 12 lines · Jan Stancek
Quoted 7 lines

Aren't we getting propagated ICMP error to send() from previous iteration of the loop?

Per https://tools.ietf.org/html/rfc1122#page-78
The application is also responsible to avoid confusion from a delayed ICMP
error message resulting from an earlier use of the same port(s).

The test is using the loopback address. There should be no delay. We're running this test daily on 7 different kernel versions and 4 different archs and I've never seen it fail with ECONNREFUSED, ever.

Similar here, it runs daily dozens of times, but appears to fail sporadically only on s390x.

Quoted 3 lines

But you can prove me wrong by adding a few debug prints around both send() calls.

1. extra prints
65 for (i = 0; i < 1000; i++) {

 66                 sock = SAFE_SOCKET(AF_INET, SOCK_DGRAM, 0);
 67                 SAFE_CONNECT(sock, (struct sockaddr *)&addr, sizeof(addr));
 68                 printf("1 %d\n", i);
 69                 SAFE_SEND(1, sock, buf, BUFSIZE, MSG_MORE);
 70                 printf("2 %d\n", i);
 71                 SAFE_SETSOCKOPT_INT(sock, SOL_SOCKET, SO_NO_CHECK, 1);
 72                 printf("3 %d\n", i);
 73                 send(sock, buf, 1, 0);
 74                 printf("4 %d\n", i);
 75                 SAFE_CLOSE(sock);

1 246
2 246
3 246
4 246
1 247
2 247
3 247
4 247
1 248

safe_net.c:202: BROK: setsockopt05.c:69: send(3, 0x3ffc397db88, 4000, 32768) failed: ECONNREFUSED (111)

2. SO_NO_CHECK doesn't matter, following fails:

                sock = SAFE_SOCKET(AF_INET, SOCK_DGRAM, 0);
                SAFE_CONNECT(sock, (struct sockaddr *)&addr, sizeof(addr));
                SAFE_SEND(1, sock, buf, BUFSIZE, MSG_MORE);
                send(sock, buf, 1, 0);
                SAFE_CLOSE(sock);

3. MSG_MORE doesn't matter, following fails:

                sock = SAFE_SOCKET(AF_INET, SOCK_DGRAM, 0);
                SAFE_CONNECT(sock, (struct sockaddr *)&addr, sizeof(addr));
                SAFE_SEND(1, sock, buf, BUFSIZE, 0);
                SAFE_CLOSE(sock);

Regards,
Jan

#7 lore raw

PATCH 1/1 syscalls/setsockopt05: associate receiver with destination address

Let's see if it's really a delayed ICMP message. Can you reproduce the error after applying this patch?

In the meantime, I'll write a test for send(MSG_MORE).

---
 testcases/kernel/syscalls/setsockopt/setsockopt05.c | 1 +
 1 file changed, 1 insertion(+)
testcases/kernel/syscalls/setsockopt/setsockopt05.c +1 -0
index e78ef236e..594178744 100644
@@ -63,6 +63,7 @@ static void run(void)
 	memset(buf, 0x42, BUFSIZE);
 
 	for (i = 0; i < 1000; i++) {
+		addr.sin_port = 12345 + i;
 		sock = SAFE_SOCKET(AF_INET, SOCK_DGRAM, 0);
 		SAFE_CONNECT(sock, (struct sockaddr *)&addr, sizeof(addr));
 		SAFE_SEND(1, sock, buf, BUFSIZE, MSG_MORE);
Signature · 1 line
2.28.0
#8 lore raw

PATCH 1/1 syscalls/setsockopt05: associate receiver with destination address

Sorry, ignore this patch, I meant to add a bind() there to change the source port...

Quoted 22 lines · Martin Doucha

Let's see if it's really a delayed ICMP message. Can you reproduce the error after applying this patch?

In the meantime, I'll write a test for send(MSG_MORE).

---
 testcases/kernel/syscalls/setsockopt/setsockopt05.c | 1 +
 1 file changed, 1 insertion(+)
testcases/kernel/syscalls/setsockopt/setsockopt05.c +1 -0
index e78ef236e..594178744 100644
@@ -63,6 +63,7 @@ static void run(void)
 	memset(buf, 0x42, BUFSIZE);
 
 	for (i = 0; i < 1000; i++) {
+		addr.sin_port = 12345 + i;
 		sock = SAFE_SOCKET(AF_INET, SOCK_DGRAM, 0);
 		SAFE_CONNECT(sock, (struct sockaddr *)&addr, sizeof(addr));
 		SAFE_SEND(1, sock, buf, BUFSIZE, MSG_MORE);
Signature · 7 lines
Martin Doucha   [email protected]
QA Engineer for Software Maintenance
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic
#9 lore raw
In reply to Jan Stancek

OK, here's the correct test patch. The source port changes on every iteration so different send() calls shouldn't interfere with one another. If you get ECONNREFUSED with this patch applied, it's caused by broken implementation of send(MSG_MORE), not delayed ICMP packets. If this patch prevents the error, resubmit your patch with fixes.

I'll write the test for send(MSG_MORE) either way because if the flag isn't honored by the kernel, setsockopt05 will be useless.

---
 testcases/kernel/syscalls/setsockopt/setsockopt05.c | 3 +++
 1 file changed, 3 insertions(+)
testcases/kernel/syscalls/setsockopt/setsockopt05.c +3 -0
index e78ef236e..698396390 100644
@@ -64,6 +64,9 @@ static void run(void)
 
 	for (i = 0; i < 1000; i++) {
 		sock = SAFE_SOCKET(AF_INET, SOCK_DGRAM, 0);
+		addr.sin_port = 12346 + i;
+		SAFE_BIND(sock, (struct sockaddr *)&addr, sizeof(addr));
+		addr.sin_port = 12345;
 		SAFE_CONNECT(sock, (struct sockaddr *)&addr, sizeof(addr));
 		SAFE_SEND(1, sock, buf, BUFSIZE, MSG_MORE);
 		SAFE_SETSOCKOPT_INT(sock, SOL_SOCKET, SO_NO_CHECK, 1);
Signature · 1 line
2.28.0
#10 lore raw

PATCH v2 1/1 syscalls/setsockopt05: associate receiver with destination address

to avoid sporadic ECONNREFUSED errors (caused by delayed ICMP):
safe_net.c:202: BROK: setsockopt05.c:70: send(6, 0x3ffcaf7d828, 4000, 32768) failed: ECONNREFUSED (111)

per man(7) udp:
ECONNREFUSED
No receiver was associated with the destination address. This might be caused by a previous packet sent over the socket.

per https://tools.ietf.org/html/rfc1122#page-78
The application is also responsible to avoid confusion from a delayed ICMP error message resulting from an earlier use of the same port(s).

---
 .../kernel/syscalls/setsockopt/setsockopt05.c | 23 ++++++++++++++-----
 1 file changed, 17 insertions(+), 6 deletions(-)
testcases/kernel/syscalls/setsockopt/setsockopt05.c +17 -6
index e78ef236e337..0b7ff39d2663 100644
@@ -31,13 +31,14 @@
 #define BUFSIZE 4000
 
 static struct sockaddr_in addr;
+static int dst_sock = -1;
 
 static void setup(void)
 {
 	int real_uid = getuid();
 	int real_gid = getgid();
-	int sock;
 	struct ifreq ifr;
+	socklen_t addrlen = sizeof(addr);
 
 	SAFE_UNSHARE(CLONE_NEWUSER);
 	SAFE_UNSHARE(CLONE_NEWNET);
@@ -45,14 +46,23 @@ static void setup(void)
 	SAFE_FILE_PRINTF("/proc/self/uid_map", "0 %d 1", real_uid);
 	SAFE_FILE_PRINTF("/proc/self/gid_map", "0 %d 1", real_gid);
 
-	tst_init_sockaddr_inet_bin(&addr, INADDR_LOOPBACK, 12345);
-	sock = SAFE_SOCKET(AF_INET, SOCK_DGRAM, 0);
+	tst_init_sockaddr_inet_bin(&addr, INADDR_LOOPBACK, 0);
+	dst_sock = SAFE_SOCKET(AF_INET, SOCK_DGRAM, 0);
+
 	strcpy(ifr.ifr_name, "lo");
 	ifr.ifr_mtu = 1500;
-	SAFE_IOCTL(sock, SIOCSIFMTU, &ifr);
+	SAFE_IOCTL(dst_sock, SIOCSIFMTU, &ifr);
 	ifr.ifr_flags = IFF_UP;
-	SAFE_IOCTL(sock, SIOCSIFFLAGS, &ifr);
-	SAFE_CLOSE(sock);
+	SAFE_IOCTL(dst_sock, SIOCSIFFLAGS, &ifr);
+
+	SAFE_BIND(dst_sock, (struct sockaddr *)&addr, addrlen);
+	SAFE_GETSOCKNAME(dst_sock, (struct sockaddr*)&addr, &addrlen);
+}
+
+static void cleanup(void)
+{
+	if (dst_sock != -1)
+		SAFE_CLOSE(dst_sock);
 }
 
 static void run(void)
@@ -82,6 +92,7 @@ static void run(void)
 static struct tst_test test = {
 	.test_all = run,
 	.setup = setup,
+	.cleanup = cleanup,
 	.taint_check = TST_TAINT_W | TST_TAINT_D,
 	.needs_kconfigs = (const char *[]) {
 		"CONFIG_USER_NS=y",
Signature · 1 line
2.18.1
#11 lore raw

PATCH v2 1/1 syscalls/setsockopt05: associate receiver with destination address

Hi,
I assume that cycling through different source ports prevented the issue?

Quoted 75 lines · Jan Stancek

to avoid sporadic ECONNREFUSED errors (caused by delayed ICMP):
safe_net.c:202: BROK: setsockopt05.c:70: send(6, 0x3ffcaf7d828, 4000, 32768) failed: ECONNREFUSED (111)

per man(7) udp:
ECONNREFUSED
No receiver was associated with the destination address. This might be caused by a previous packet sent over the socket.

per https://tools.ietf.org/html/rfc1122#page-78
The application is also responsible to avoid confusion from a delayed ICMP error message resulting from an earlier use of the same port(s).

---
 .../kernel/syscalls/setsockopt/setsockopt05.c | 23 ++++++++++++++-----
 1 file changed, 17 insertions(+), 6 deletions(-)
testcases/kernel/syscalls/setsockopt/setsockopt05.c +17 -6
index e78ef236e337..0b7ff39d2663 100644
@@ -31,13 +31,14 @@
 #define BUFSIZE 4000
 
 static struct sockaddr_in addr;
+static int dst_sock = -1;
 
 static void setup(void)
 {
 	int real_uid = getuid();
 	int real_gid = getgid();
-	int sock;
 	struct ifreq ifr;
+	socklen_t addrlen = sizeof(addr);
 
 	SAFE_UNSHARE(CLONE_NEWUSER);
 	SAFE_UNSHARE(CLONE_NEWNET);
@@ -45,14 +46,23 @@ static void setup(void)
 	SAFE_FILE_PRINTF("/proc/self/uid_map", "0 %d 1", real_uid);
 	SAFE_FILE_PRINTF("/proc/self/gid_map", "0 %d 1", real_gid);
 
-	tst_init_sockaddr_inet_bin(&addr, INADDR_LOOPBACK, 12345);
-	sock = SAFE_SOCKET(AF_INET, SOCK_DGRAM, 0);
+	tst_init_sockaddr_inet_bin(&addr, INADDR_LOOPBACK, 0);
+	dst_sock = SAFE_SOCKET(AF_INET, SOCK_DGRAM, 0);
+
 	strcpy(ifr.ifr_name, "lo");
 	ifr.ifr_mtu = 1500;
-	SAFE_IOCTL(sock, SIOCSIFMTU, &ifr);
+	SAFE_IOCTL(dst_sock, SIOCSIFMTU, &ifr);
 	ifr.ifr_flags = IFF_UP;
-	SAFE_IOCTL(sock, SIOCSIFFLAGS, &ifr);
-	SAFE_CLOSE(sock);
+	SAFE_IOCTL(dst_sock, SIOCSIFFLAGS, &ifr);
+
+	SAFE_BIND(dst_sock, (struct sockaddr *)&addr, addrlen);
+	SAFE_GETSOCKNAME(dst_sock, (struct sockaddr*)&addr, &addrlen);
+}
+
+static void cleanup(void)
+{
+	if (dst_sock != -1)
+		SAFE_CLOSE(dst_sock);
 }
 
 static void run(void)
@@ -82,6 +92,7 @@ static void run(void)
 static struct tst_test test = {
 	.test_all = run,
 	.setup = setup,
+	.cleanup = cleanup,
 	.taint_check = TST_TAINT_W | TST_TAINT_D,
 	.needs_kconfigs = (const char *[]) {
 		"CONFIG_USER_NS=y",
Signature · 7 lines
Martin Doucha   [email protected]
QA Engineer for Software Maintenance
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic
#12 lore raw

PATCH v2 1/1 syscalls/setsockopt05: associate receiver with destination address

----- Original Message -----

Quoted 2 lines

Hi,
I assume that cycling through different source ports prevented the issue?

Yes, it did, it ran fine over night.

Quoted 94 lines
Quoted 71 lines · Jan Stancek

to avoid sporadic ECONNREFUSED errors (caused by delayed ICMP):
safe_net.c:202: BROK: setsockopt05.c:70: send(6, 0x3ffcaf7d828, 4000, 32768) failed: ECONNREFUSED (111)

per man(7) udp:
ECONNREFUSED
No receiver was associated with the destination address. This might be caused by a previous packet sent over the socket.

per https://tools.ietf.org/html/rfc1122#page-78
The application is also responsible to avoid confusion from a delayed ICMP
error message resulting from an earlier use of the same port(s).

---
 .../kernel/syscalls/setsockopt/setsockopt05.c | 23 ++++++++++++++-----
 1 file changed, 17 insertions(+), 6 deletions(-)
testcases/kernel/syscalls/setsockopt/setsockopt05.c +16 -6
index e78ef236e337..0b7ff39d2663 100644
@@ -31,13 +31,14 @@
 #define BUFSIZE 4000
 
 static struct sockaddr_in addr;
+static int dst_sock = -1;
 
 static void setup(void)
 {
 	int real_uid = getuid();
 	int real_gid = getgid();
-	int sock;
 	struct ifreq ifr;
+	socklen_t addrlen = sizeof(addr);
 
 	SAFE_UNSHARE(CLONE_NEWUSER);
 	SAFE_UNSHARE(CLONE_NEWNET);
@@ -45,14 +46,23 @@ static void setup(void)
 	SAFE_FILE_PRINTF("/proc/self/uid_map", "0 %d 1", real_uid);
 	SAFE_FILE_PRINTF("/proc/self/gid_map", "0 %d 1", real_gid);
 
-	tst_init_sockaddr_inet_bin(&addr, INADDR_LOOPBACK, 12345);
-	sock = SAFE_SOCKET(AF_INET, SOCK_DGRAM, 0);
+	tst_init_sockaddr_inet_bin(&addr, INADDR_LOOPBACK, 0);
+	dst_sock = SAFE_SOCKET(AF_INET, SOCK_DGRAM, 0);
+
 	strcpy(ifr.ifr_name, "lo");
 	ifr.ifr_mtu = 1500;
-	SAFE_IOCTL(sock, SIOCSIFMTU, &ifr);
+	SAFE_IOCTL(dst_sock, SIOCSIFMTU, &ifr);
 	ifr.ifr_flags = IFF_UP;
-	SAFE_IOCTL(sock, SIOCSIFFLAGS, &ifr);
-	SAFE_CLOSE(sock);
+	SAFE_IOCTL(dst_sock, SIOCSIFFLAGS, &ifr);
+
+	SAFE_BIND(dst_sock, (struct sockaddr *)&addr, addrlen);
+	SAFE_GETSOCKNAME(dst_sock, (struct sockaddr*)&addr, &addrlen);
+}
+
+static void cleanup(void)
+{
+	if (dst_sock != -1)
+		SAFE_CLOSE(dst_sock);
 }
 
 static void run(void)

testcases/kernel/syscalls/setsockopt/setsockopt05.c static void run(void) · line 98

Quoted 7 lines
testcases/kernel/syscalls/setsockopt/setsockopt05.c +1 -0
@@ -82,6 +92,7 @@ static void run(void)
 static struct tst_test test = {
 	.test_all = run,
 	.setup = setup,
+	.cleanup = cleanup,
 	.taint_check = TST_TAINT_W | TST_TAINT_D,
 	.needs_kconfigs = (const char *[]) {
 		"CONFIG_USER_NS=y",

--
Martin Doucha [email protected]
QA Engineer for Software Maintenance
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic

#13 lore raw

PATCH v2 1/1 syscalls/setsockopt05: associate receiver with destination address

----- Original Message -----

Quoted 2 lines

Hi,
I assume that cycling through different source ports prevented the issue?

Yes, it did, it ran fine over night.

Quoted 2 lines

Pushed.

Thanks,
Jan