Skip to contentSkip to conversation

: Support the new specification of sctp_connectx(

PATCH Ivan Skytte Jørgensen 1 participant 1 message Apr 8, 2009

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

(Attachment hopefully not stripped now)

Quoted 36 lines · On Tuesday 07 April 2009 23:56:18 Ivan Skytte Jørgensen wrote:
Quoted 1 line · On Monday 30 March 2009 19:46:11 Vlad Yasevich wrote:

Ivan Skytte Jørgensen wrote:

...

Quoted 10 lines
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

So there is a need for the system call to return both a value and an error code at the same time.

Having taken a closer look at the kernel side I think I can whip up a complete patch for this.

OK. Looks like it will need yet a third socket option that takes the sctp_getaddrs structure.

Looking forward to this code.

Attached: patch for lksctp-1.0.10 and kernel 2.6.29.1

Tested on kernel 2.6.27.19 on both 32bit and 64bit.

* Support non-blocking sctp_connectx() calls returning the future association-id.

Some notes on the user library part:

1: I decided to keep the previous "new" sctp_connectx() implementation, because a: it costs nothing, and b: some very recent programs may rely on the broken behaviour.

2: The new-new sctp_connectx() tries the new method first. If it fails due to ENOPROTOOPT (old kernel) then it falls back to the old method first, but only if it is safe to do so (blocking socket, or assoc_id not returned)

Quoted 6 lines

3: (note of users of this feature): If a non-blocking connect fails quickly then a new call to sctp_connectx() may return an identical association id becaue the ID has been freed up already.

Regards,
Ivan

src/include/netinet/sctp.h +2 -0
diff -U3 -p -r lksctp-tools-1.0.10/src/include/netinet/sctp.h lksctp-tools-1.0.11/src/include/netinet/sctp.h
@@ -146,6 +146,8 @@ enum sctp_optname {
 #define SCTP_GET_LOCAL_ADDRS	SCTP_GET_LOCAL_ADDRS
 	SCTP_SOCKOPT_CONNECTX, /* CONNECTX requests. NEW implementation */
 #define SCTP_SOCKOPT_CONNECTX	SCTP_SOCKOPT_CONNECTX
+	SCTP_SOCKOPT_CONNECTX3, /* CONNECTX requests. NEW, NEW implementation */
+#define SCTP_SOCKOPT_CONNECTX3	SCTP_SOCKOPT_CONNECTX3
 };
 
 /*
src/lib/Versions.map +4 -0
diff -U3 -p -r lksctp-tools-1.0.10/src/lib/Versions.map lksctp-tools-1.0.11/src/lib/Versions.map
@@ -20,3 +20,7 @@ VERS_1 {
 VERS_2 {
 	global:	sctp_connectx;
 } VERS_1;
+
+VERS_3 {
+	global:	sctp_connectx;
+} VERS_2;
src/lib/connectx.c +83 -5

110 lines not on this page · raw message

include/net/sctp/user.h renamed from usr/src/linux/include/net/sctp/user.h +2 -0
diff -U3 -r -p /usr/src/linux/include/net/sctp/user.h linux-2.6.27.19-3.2/include/net/sctp/user.h
@@ -145,6 +145,8 @@ enum sctp_optname {
 #define SCTP_GET_LOCAL_ADDRS	SCTP_GET_LOCAL_ADDRS
 	SCTP_SOCKOPT_CONNECTX, /* CONNECTX requests. */
 #define SCTP_SOCKOPT_CONNECTX	SCTP_SOCKOPT_CONNECTX
+	SCTP_SOCKOPT_CONNECTX3, /* CONNECTX requests. (new implementation) */
+#define SCTP_SOCKOPT_CONNECTX3	SCTP_SOCKOPT_CONNECTX3
 };
 
 /*
net/sctp/associola.c renamed from usr/src/linux/net/sctp/associola.c +4 -0
diff -U3 -r -p /usr/src/linux/net/sctp/associola.c linux-2.6.27.19-3.2/net/sctp/associola.c
@@ -1481,6 +1481,10 @@ int sctp_assoc_set_id(struct sctp_associ
 {
 	int assoc_id;
 	int error = 0;
+
+	/* If the id is already assigned, keep it. */
+	if (asoc->assoc_id)
+		return error;
 retry:
 	if (unlikely(!idr_pre_get(&sctp_assocs_id, gfp)))
 		return -ENOMEM;
net/sctp/socket.c renamed from usr/src/linux/net/sctp/socket.c +39 -1
diff -U3 -r -p /usr/src/linux/net/sctp/socket.c linux-2.6.27.19-3.2/net/sctp/socket.c
@@ -1100,6 +1100,15 @@ static int __sctp_connect(struct sock* s
 		goto out_free;
 	}
 
+	/* In case the user of sctp_connectx() wants an association
+	 * id back, assign one now.
+	 */
+	if (assoc_id) {
+		err = sctp_assoc_set_id(asoc, GFP_KERNEL);
+		if (err < 0)
+			goto out_free;
+	}
+
 	err = sctp_primitive_ASSOCIATE(asoc, NULL);
 	if (err < 0) {
 		goto out_free;
@@ -1120,7 +1129,7 @@ static int __sctp_connect(struct sock* s
 	timeo = sock_sndtimeo(sk, f_flags & O_NONBLOCK);
 
 	err = sctp_wait_for_connect(asoc, &timeo);
-	if (!err && assoc_id)
+	if ((err == 0 || err == -EINPROGRESS) && assoc_id)
 		*assoc_id = asoc->assoc_id;
 
 	/* Don't free association on exit. */
@@ -1264,6 +1273,32 @@ SCTP_STATIC int sctp_setsockopt_connectx
 		return assoc_id;
 }
 
+/*
+ * New (hopefully final) interface for the API.  The option buffer is used
+ * both for the returned association id and the addresses.
+ */
+SCTP_STATIC int sctp_getsockopt_connectx3(struct sock* sk, int len,
+					 char __user *optval,
+					 int __user *optlen)
+{
+	sctp_assoc_t assoc_id = 0;
+	int err = 0;
+
+	if(len < 4)
+		return -EINVAL;
+
+	err = __sctp_setsockopt_connectx(sk, (struct sockaddr __user *)(optval+4), len-4, &assoc_id);
+
+	if (err >= 0 || err==-EINPROGRESS) {
+		if (put_user(assoc_id, optval))
+			return -EFAULT;
+		if (put_user(4, optlen))
+			return -EFAULT;
+	}
+
+	return err;
+}
+
 /* API 3.1.4 close() - UDP Style Syntax
  * Applications use close() to perform graceful shutdown (as described in
  * Section 10.1 of [SCTP]) on ALL the associations currently represented
@@ -5447,6 +5482,9 @@ SCTP_STATIC int sctp_getsockopt(struct s
 		retval = sctp_getsockopt_local_addrs(sk, len, optval,
 						     optlen);
 		break;
+	case SCTP_SOCKOPT_CONNECTX3:
+		retval = sctp_getsockopt_connectx3(sk, len, optval, optlen);
+		break;
 	case SCTP_DEFAULT_SEND_PARAM:
 		retval = sctp_getsockopt_default_send_param(sk, len,
 							    optval, optlen);