Saturday, October 27, 2012

SCTP multi-homing with dynamic address reconfiguration

NetworkIn the previous article, I have introduced SCTP, the Stream Control Transmission Protocol. In this article I would like to get more technical and explain how you can create SCTP multi-homed connections with dynamic address reconfiguration in C/C++ in a Linux environment. All in all it is fairly easy, but the hard part was to find all pieces of the puzzle, so I have put them together for you.
I found this great coding example: http://goo.gl/7A5cw, which works perfectly as the base for this article.

Configuration

Adding/Deleting IP addresses in an SCTP association

Before you can use this SCTP feature, you have to enable following sysctl options:

First of all, you need to enable the extension that allows addition or deletion of IP addresses to the SCTP associations by setting
sysctl -w net.sctp.addip_enable=1
Further more, you need the authentication features of SCTP. This is required because otherwise it would be easy to hijack an existing SCTP association with the addition and deletion of IP addresses.
sysctl -w net.sctp.auth_enable=1 (preferred)
OR
sysctl -w net.sctp.addip_noauth_enable=1
The second option disables authentication, but is not safe in a production environment.

Address Configuration Change Chunk (ASCONF)

This type of messages is used to signal the addition or deletion of an IP address of the peer or to set the primary IP address of the association. Every time you change one of these settings, such a message is sent (and acknowledged by the peer). The option addip_enable ensures these chunks are sent whenever the software calls for such a change, but it is also possible to trigger the transmission of these chunks automatically from within the network stack whenever a change in IP addresses is detected on one of the network interfaces. This can be enabled globally (i.e. for all SCTP connections) with:
sysctl -w net.sctp.default_auto_asconf=1
This option is only available since Linux kernel 3.2!

Programming

Manual changes

You can manually add/delete IP addresses of an SCTP association with the following C functions.
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/sctp.h>


int sctp_connectx(int sd, struct sockaddr * addrs,
    int addrcnt, sctp_assoc_t * id);
int sctp_bindx(int sd, struct sockaddr * addrs,
    int addrcnt, int flags);
sctp_connectx initiates a connection to a set of addresses passed in the array addrs to/from the socket sd. addrcnt is the number of addresses in the array.
sctp_bindx adds or removes a set of bind addresses passed in the array addrs to/from the socket sd. addrcnt is the number of addresses in the array and the flags paramater indicates if the addresses need to be added or removed. The flags parameter can be either SCTP_BINDX_ADD_ADDR or SCTP_BINDX_REM_ADDR. An application can use SCTP_BINDX_ADD_ADDR to associate additional addresses with an endpoint after calling bind. SCTP_BINDX_REM_ADDR directs SCTP to remove the given addresses from the association. A caller may not remove all addresses from an association. (This is according to the Linux sctp man page)

Automatic changes

This option is only available since Linux kernel 3.2!
 
If you have not set default_auto_asconf, it is possible to do this on a per SCTP socket basis with the call
#include <sys/socket.h>#include <netinet/sctp.h>
#include <net/sctp/user.h>

int asconf = 1;
setsockopt(sd, IPPROTO_SCTP, SCTP_AUTO_ASCONF, &asconf, sizeof(asconf));
sd is the socket file descriptor. The net/sctp/user.h header can be found in the Linux source and defines SCTP_AUTO_ASCONF. If you do not like to include it, SCTP_AUTO_ASCONF = 30 at the moment of writing, but developing your software using this value hard coded makes your software less portable of course.
Attention: this option can only be set after the bind or connect function call, otherwise it fails! I.e. on line 36 of the SCTPServer.c and line 39 of the SCTPClient.c you can find on http://goo.gl/7A5cw.

Conclusion

In this article you find how to enable SCTP multi-homing with dynamic address reconfiguration in C/C++ on a Linux system. This article combines information I gathered from several Internet sources and from the lksctp-devel mailing list. If you have questions, hints or remarks, please let me know in the comments below!

No comments:

Post a Comment