Add basic SCTP listener support that binds to multiple addresses

This only supports two addresses as the intended use is 1 IPv4 and 1 IPv6
address on a single-homed host, and the only supported configuration of
outgoing connections to other servers is to bind a single IPv4 or IPv6
address.
This commit is contained in:
Simon Arlott 2017-08-19 15:11:49 +01:00
parent 589dbf4d0f
commit c6ad9b0c5f
No known key found for this signature in database
GPG key ID: C8975F2043CA5D24
21 changed files with 552 additions and 205 deletions

View file

@ -294,7 +294,7 @@ accept_client(struct auth_client *auth)
/* Begin authenticating user */
static void
start_auth(const char *cid, const char *l_ip, const char *l_port, const char *c_ip, const char *c_port)
start_auth(const char *cid, const char *l_ip, const char *l_port, const char *c_ip, const char *c_port, const char *protocol)
{
struct auth_client *auth;
unsigned long long lcid = strtoull(cid, NULL, 16);
@ -315,6 +315,8 @@ start_auth(const char *cid, const char *l_ip, const char *l_port, const char *c_
exit(EX_PROVIDER_ERROR);
}
auth->protocol = strtoull(protocol, NULL, 16);
rb_strlcpy(auth->l_ip, l_ip, sizeof(auth->l_ip));
auth->l_port = (uint16_t)atoi(l_port); /* should be safe */
(void) rb_inet_pton_sock(l_ip, (struct sockaddr *)&auth->l_addr);
@ -362,13 +364,12 @@ done:
void
handle_new_connection(int parc, char *parv[])
{
if(parc < 6)
{
if (parc < 6) {
warn_opers(L_CRIT, "provider: received too few params for new connection (6 expected, got %d)", parc);
exit(EX_PROVIDER_ERROR);
}
start_auth(parv[1], parv[2], parv[3], parv[4], parv[5]);
start_auth(parv[1], parv[2], parv[3], parv[4], parv[5], parc > 6 ? parv[6] : "0");
}
void

View file

@ -46,6 +46,8 @@ struct auth_client
{
uint32_t cid; /* Client ID */
int protocol; /* IP protocol (TCP/SCTP) */
char l_ip[HOSTIPLEN + 1]; /* Listener IP address */
uint16_t l_port; /* Listener port */
struct rb_sockaddr_storage l_addr; /* Listener address/port */

View file

@ -310,7 +310,7 @@ ident_start(struct auth_client *auth)
set_provider_data(auth, SELF_PID, query);
set_provider_timeout_relative(auth, SELF_PID, ident_timeout);
if((query->F = rb_socket(family, SOCK_STREAM, 0, "ident")) == NULL)
if((query->F = rb_socket(family, SOCK_STREAM, auth->protocol, "ident")) == NULL)
{
warn_opers(L_DEBUG, "Could not create ident socket: %s", strerror(errno));
client_fail(auth, REPORT_FAIL);

View file

@ -340,6 +340,26 @@ AC_CHECK_HEADER(zlib.h, [
fi
AC_ARG_WITH(sctp-path,
AC_HELP_STRING([--with-sctp-path=DIR],[Path to libsctp.so for SCTP support.]),
[LIBS="$LIBS -L$withval"],)
AC_ARG_ENABLE(sctp,
AC_HELP_STRING([--disable-sctp],[Disable SCTP support]),
[sctp=$enableval],[sctp=yes])
if test "$sctp" = yes; then
AC_CHECK_HEADER(netinet/sctp.h, [
AC_CHECK_LIB(sctp, sctp_bindx,
[
AC_SUBST(LIBSCTP_LD, -lsctp)
AC_DEFINE(HAVE_LIBSCTP, 1, [Define to 1 if libsctp (-lsctp) is available.])
], sctp=no)
], sctp=no)
fi
dnl Check for shared sqlite
dnl ======================
AC_ARG_WITH(shared-sqlite,
@ -679,6 +699,7 @@ Configuration of ${BRANDING_NAME}-${BRANDING_VERSION}:
Ziplinks : $zlib
OpenSSL : $openssl
SCTP : $sctp
Nickname length : $NICKLEN
Topic length : $TOPICLEN

View file

@ -436,6 +436,7 @@ struct ListClient
#define LFLAGS_SSL 0x00000001
#define LFLAGS_FLUSH 0x00000002
#define LFLAGS_CORK 0x00000004
#define LFLAGS_SCTP 0x00000008
/* umodes, settable flags */
/* lots of this moved to snomask -- jilles */
@ -506,6 +507,10 @@ struct ListClient
#define SetFlush(x) ((x)->localClient->localflags |= LFLAGS_FLUSH)
#define ClearFlush(x) ((x)->localClient->localflags &= ~LFLAGS_FLUSH)
#define IsSCTP(x) ((x)->localClient->localflags & LFLAGS_SCTP)
#define SetSCTP(x) ((x)->localClient->localflags |= LFLAGS_SCTP)
#define ClearSCTP(x) ((x)->localClient->localflags &= ~LFLAGS_SCTP)
/* oper flags */
#define MyOper(x) (MyConnect(x) && IsOper(x))

View file

@ -64,6 +64,8 @@
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif
#define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
#define HOSTLEN 63 /* Length of hostname. Updated to */
/* comply with RFC1123 */

View file

@ -38,12 +38,14 @@ struct Listener
int active; /* current state of listener */
int ssl; /* ssl listener */
int defer_accept; /* use TCP_DEFER_ACCEPT */
bool sctp; /* use SCTP */
int wsock; /* wsock listener */
struct rb_sockaddr_storage addr;
char vhost[HOSTLEN + 1]; /* virtual name of listener */
struct rb_sockaddr_storage addr[2];
char vhost[(HOSTLEN * 2) + 1]; /* virtual name of listener */
};
extern void add_listener(int port, const char *vaddr_ip, int family, int ssl, int defer_accept, int wsock);
extern void add_tcp_listener(int port, const char *vaddr_ip, int family, int ssl, int defer_accept, int wsock);
extern void add_sctp_listener(int port, const char *vaddr_ip1, const char *vaddr_ip2, int ssl, int wsock);
extern void close_listener(struct Listener *listener);
extern void close_listeners(void);
extern const char *get_listener_name(const struct Listener *listener);

View file

@ -54,7 +54,7 @@
#define NUMERIC_STR_217 "%c %d %s :%s"
#define NUMERIC_STR_218 "Y %s %d %d %d %u %d.%d %d.%d %u"
#define NUMERIC_STR_219 "%c :End of /STATS report"
#define NUMERIC_STR_220 "%c %d %s %d :%s%s"
#define NUMERIC_STR_220 "%c %d %s %d :%s%s%s"
#define NUMERIC_STR_221 "%s"
#define NUMERIC_STR_225 "%c %s :%s%s%s"
#define NUMERIC_STR_241 "L %s * %s 0 -1"

View file

@ -111,6 +111,7 @@ struct ConfItem
#define CONF_FLAGS_ENCRYPTED 0x00200000
#define CONF_FLAGS_EXEMPTDNSBL 0x04000000
#define CONF_FLAGS_EXEMPTPROXY 0x08000000
#define CONF_FLAGS_ALLOW_SCTP 0x10000000
/* Macros for struct ConfItem */
@ -134,6 +135,7 @@ struct ConfItem
#define IsConfExemptProxy(x) ((x)->flags & CONF_FLAGS_EXEMPTPROXY)
#define IsConfExtendChans(x) ((x)->flags & CONF_FLAGS_EXTEND_CHANS)
#define IsConfSSLNeeded(x) ((x)->flags & CONF_FLAGS_NEED_SSL)
#define IsConfAllowSCTP(x) ((x)->flags & CONF_FLAGS_ALLOW_SCTP)
/* flag definitions for opers now in client.h */

View file

@ -474,7 +474,12 @@ authd_initiate_client(struct Client *client_p, bool defer)
/* Add a bit of a fudge factor... */
client_p->preClient->auth.timeout = rb_current_time() + ConfigFileEntry.connect_timeout + 10;
rb_helper_write(authd_helper, "C %x %s %hu %s %hu", authd_cid, listen_ipaddr, listen_port, client_ipaddr, client_port);
rb_helper_write(authd_helper, "C %x %s %hu %s %hu %x", authd_cid, listen_ipaddr, listen_port, client_ipaddr, client_port,
#ifdef HAVE_LIBSCTP
IsSCTP(client_p) ? IPPROTO_SCTP : IPPROTO_TCP);
#else
IPPROTO_TCP);
#endif
}
static inline void

View file

@ -43,11 +43,6 @@
#include "s_assert.h"
#include "logger.h"
#if defined(NO_IN6ADDR_ANY)
static const struct in6_addr in6addr_any =
{ { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } };
#endif
static struct Listener *ListenerPollList = NULL;
static int accept_precallback(rb_fde_t *F, struct sockaddr *addr, rb_socklen_t addrlen, void *data);
static void accept_callback(rb_fde_t *F, int status, struct sockaddr *addr, rb_socklen_t addrlen, void *data);
@ -61,7 +56,7 @@ make_listener(struct rb_sockaddr_storage *addr)
listener->name = me.name;
listener->F = NULL;
memcpy(&listener->addr, addr, sizeof(struct rb_sockaddr_storage));
memcpy(&listener->addr, addr, sizeof(listener->addr));
listener->next = NULL;
return listener;
}
@ -102,21 +97,16 @@ free_listener(struct Listener *listener)
static uint16_t
get_listener_port(const struct Listener *listener)
{
return ntohs(GET_SS_PORT(&listener->addr));
return ntohs(GET_SS_PORT(&listener->addr[0]));
}
/*
* get_listener_name - return displayable listener name and port
* returns "host.foo.org:6667" for a given listener
*/
const char *
get_listener_name(const struct Listener *listener)
{
static char buf[HOSTLEN + HOSTLEN + PORTNAMELEN + 4];
s_assert(NULL != listener);
if(listener == NULL)
return NULL;
static char buf[BUFSIZE];
snprintf(buf, sizeof(buf), "%s[%s/%u]",
me.name, listener->name, get_listener_port(listener));
@ -141,6 +131,7 @@ show_ports(struct Client *source_p)
get_listener_port(listener),
IsOperAdmin(source_p) ? listener->name : me.name,
listener->ref_count, (listener->active) ? "active" : "disabled",
listener->sctp ? " sctp" : " tcp",
listener->ssl ? " ssl" : "");
}
}
@ -155,79 +146,69 @@ static int
inetport(struct Listener *listener)
{
rb_fde_t *F;
int opt = 1;
const char *errstr;
int ret;
/*
* At first, open a new socket
*/
F = rb_socket(GET_SS_FAMILY(&listener->addr), SOCK_STREAM, 0, "Listener socket");
if(GET_SS_FAMILY(&listener->addr) == AF_INET6)
{
struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)&listener->addr;
if(!IN6_ARE_ADDR_EQUAL(&in6->sin6_addr, &in6addr_any))
{
rb_inet_ntop(AF_INET6, &in6->sin6_addr, listener->vhost, sizeof(listener->vhost));
listener->name = listener->vhost;
}
if (listener->sctp) {
#ifdef HAVE_LIBSCTP
/* only AF_INET6 sockets can have both AF_INET and AF_INET6 addresses */
F = rb_socket(AF_INET6, SOCK_STREAM, IPPROTO_SCTP, "Listener socket");
#else
F = NULL;
#endif
} else {
struct sockaddr_in *in = (struct sockaddr_in *)&listener->addr;
if(in->sin_addr.s_addr != INADDR_ANY)
{
F = rb_socket(GET_SS_FAMILY(&listener->addr[0]), SOCK_STREAM, IPPROTO_TCP, "Listener socket");
}
memset(listener->vhost, 0, sizeof(listener->vhost));
if (GET_SS_FAMILY(&listener->addr[0]) == AF_INET6) {
struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)&listener->addr[0];
rb_inet_ntop(AF_INET6, &in6->sin6_addr, listener->vhost, sizeof(listener->vhost));
} else if (GET_SS_FAMILY(&listener->addr[0]) == AF_INET) {
struct sockaddr_in *in = (struct sockaddr_in *)&listener->addr[0];
rb_inet_ntop(AF_INET, &in->sin_addr, listener->vhost, sizeof(listener->vhost));
}
if (GET_SS_FAMILY(&listener->addr[1]) == AF_INET6) {
struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)&listener->addr[1];
strncat(listener->vhost, "&", sizeof(listener->vhost));
rb_inet_ntop(AF_INET6, &in6->sin6_addr, &listener->vhost[strlen(listener->vhost)], sizeof(listener->vhost) - strlen(listener->vhost));
} else if (GET_SS_FAMILY(&listener->addr[1]) == AF_INET) {
struct sockaddr_in *in = (struct sockaddr_in *)&listener->addr[1];
strncat(listener->vhost, "&", sizeof(listener->vhost));
rb_inet_ntop(AF_INET, &in->sin_addr, &listener->vhost[strlen(listener->vhost)], sizeof(listener->vhost) - strlen(listener->vhost));
}
if (listener->vhost[0] != '\0') {
listener->name = listener->vhost;
}
}
if(F == NULL)
{
if (F == NULL) {
sendto_realops_snomask(SNO_GENERAL, L_ALL,
"Cannot open socket for listener on port %d",
"Cannot open socket for listener on %s port %d",
listener->sctp ? "SCTP" : "TCP",
get_listener_port(listener));
ilog(L_MAIN, "Cannot open socket for listener %s",
ilog(L_MAIN, "Cannot open socket for %s listener %s",
listener->sctp ? "SCTP" : "TCP",
get_listener_name(listener));
return 0;
}
else if((maxconnections - 10) < rb_get_fd(F)) /* XXX this is kinda bogus*/
{
ilog_error("no more connections left for listener");
sendto_realops_snomask(SNO_GENERAL, L_ALL,
"No more connections left for listener on port %d",
get_listener_port(listener));
ilog(L_MAIN, "No more connections left for listener %s",
get_listener_name(listener));
rb_close(F);
return 0;
if (listener->sctp) {
ret = rb_sctp_bindx(F, listener->addr, ARRAY_SIZE(listener->addr));
} else {
ret = rb_bind(F, (struct sockaddr *)&listener->addr[0]);
}
/*
* XXX - we don't want to do all this crap for a listener
* set_sock_opts(listener);
*
* FIXME - doesn't this belong in librb? --Elizafox
*/
if(setsockopt(rb_get_fd(F), SOL_SOCKET, SO_REUSEADDR, (char *) &opt, sizeof(opt)))
{
if (ret) {
errstr = strerror(rb_get_sockerr(F));
sendto_realops_snomask(SNO_GENERAL, L_ALL,
"Cannot set SO_REUSEADDR for listener on port %d: %s",
"Cannot bind for listener on %s port %d: %s",
listener->sctp ? "SCTP" : "TCP",
get_listener_port(listener), errstr);
ilog(L_MAIN, "Cannot set SO_REUSEADDR for listener %s: %s",
get_listener_name(listener), errstr);
rb_close(F);
return 0;
}
/* FIXME - doesn't this belong in librb? --Elizafox */
if(bind(rb_get_fd(F), (struct sockaddr *) &listener->addr, GET_SS_LEN(&listener->addr)))
{
errstr = strerror(rb_get_sockerr(F));
sendto_realops_snomask(SNO_GENERAL, L_ALL,
"Cannot bind for listener on port %d: %s",
get_listener_port(listener), errstr);
ilog(L_MAIN, "Cannot bind for listener %s: %s",
ilog(L_MAIN, "Cannot bind for %s listener %s: %s",
listener->sctp ? "SCTP" : "TCP",
get_listener_name(listener), errstr);
rb_close(F);
return 0;
@ -237,9 +218,11 @@ inetport(struct Listener *listener)
{
errstr = strerror(rb_get_sockerr(F));
sendto_realops_snomask(SNO_GENERAL, L_ALL,
"Cannot listen() for listener on port %d: %s",
"Cannot listen() for listener on %s port %d: %s",
listener->sctp ? "SCTP" : "TCP",
get_listener_port(listener), errstr);
ilog(L_MAIN, "Cannot listen() for listener %s: %s",
ilog(L_MAIN, "Cannot listen() for %s listener %s: %s",
listener->sctp ? "SCTP" : "TCP",
get_listener_name(listener), errstr);
rb_close(F);
return 0;
@ -252,67 +235,73 @@ inetport(struct Listener *listener)
}
static struct Listener *
find_listener(struct rb_sockaddr_storage *addr)
find_listener(struct rb_sockaddr_storage *addr, int sctp)
{
struct Listener *listener = NULL;
struct Listener *last_closed = NULL;
for (listener = ListenerPollList; listener; listener = listener->next)
{
if(GET_SS_FAMILY(addr) != GET_SS_FAMILY(&listener->addr))
for (listener = ListenerPollList; listener; listener = listener->next) {
if (listener->sctp != sctp)
continue;
switch(GET_SS_FAMILY(addr))
{
for (int i = 0; i < ARRAY_SIZE(listener->addr); i++) {
if (GET_SS_FAMILY(&addr[i]) != GET_SS_FAMILY(&listener->addr[i]))
goto next;
switch (GET_SS_FAMILY(&addr[i])) {
case AF_INET:
{
struct sockaddr_in *in4 = (struct sockaddr_in *)addr;
struct sockaddr_in *lin4 = (struct sockaddr_in *)&listener->addr;
if(in4->sin_addr.s_addr == lin4->sin_addr.s_addr &&
in4->sin_port == lin4->sin_port )
struct sockaddr_in *in4 = (struct sockaddr_in *)&addr[i];
struct sockaddr_in *lin4 = (struct sockaddr_in *)&listener->addr[i];
if(in4->sin_addr.s_addr != lin4->sin_addr.s_addr ||
in4->sin_port != lin4->sin_port)
{
if(listener->F == NULL)
last_closed = listener;
else
return(listener);
goto next;
}
break;
}
case AF_INET6:
{
struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)addr;
struct sockaddr_in6 *lin6 =(struct sockaddr_in6 *)&listener->addr;
if(IN6_ARE_ADDR_EQUAL(&in6->sin6_addr, &lin6->sin6_addr) &&
in6->sin6_port == lin6->sin6_port)
struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)&addr[i];
struct sockaddr_in6 *lin6 =(struct sockaddr_in6 *)&listener->addr[i];
if (!IN6_ARE_ADDR_EQUAL(&in6->sin6_addr, &lin6->sin6_addr) ||
in6->sin6_port != lin6->sin6_port)
{
if(listener->F == NULL)
last_closed = listener;
else
return(listener);
goto next;
}
break;
}
default:
break;
}
}
if (listener->F == NULL) {
last_closed = listener;
} else {
return listener;
}
next:
continue;
}
return last_closed;
}
/*
* add_listener- create a new listener
* add_tcp_listener- create a new listener
* port - the port number to listen on
* vhost_ip - if non-null must contain a valid IP address string in
* the format "255.255.255.255"
*/
void
add_listener(int port, const char *vhost_ip, int family, int ssl, int defer_accept, int wsock)
add_tcp_listener(int port, const char *vhost_ip, int family, int ssl, int defer_accept, int wsock)
{
struct Listener *listener;
struct rb_sockaddr_storage vaddr;
struct rb_sockaddr_storage vaddr[ARRAY_SIZE(listener->addr)];
/*
* if no port in conf line, don't bother
@ -320,58 +309,50 @@ add_listener(int port, const char *vhost_ip, int family, int ssl, int defer_acce
if (port == 0)
return;
memset(&vaddr, 0, sizeof(vaddr));
SET_SS_FAMILY(&vaddr, family);
SET_SS_FAMILY(&vaddr[0], AF_UNSPEC);
SET_SS_LEN(&vaddr[0], sizeof(struct sockaddr_storage));
SET_SS_FAMILY(&vaddr[1], AF_UNSPEC);
SET_SS_LEN(&vaddr[1], sizeof(struct sockaddr_storage));
if(vhost_ip != NULL)
{
if(family == AF_INET)
{
if(rb_inet_pton(family, vhost_ip, &((struct sockaddr_in *)&vaddr)->sin_addr) <= 0)
if (vhost_ip != NULL) {
if (family == AF_INET) {
if (rb_inet_pton(family, vhost_ip, &((struct sockaddr_in *)&vaddr[0])->sin_addr) <= 0)
return;
} else {
if (rb_inet_pton(family, vhost_ip, &((struct sockaddr_in6 *)&vaddr[0])->sin6_addr) <= 0)
return;
}
else
{
if(rb_inet_pton(family, vhost_ip, &((struct sockaddr_in6 *)&vaddr)->sin6_addr) <= 0)
return;
}
} else
{
switch(family)
{
} else {
switch(family) {
case AF_INET:
((struct sockaddr_in *)&vaddr)->sin_addr.s_addr = INADDR_ANY;
((struct sockaddr_in *)&vaddr[0])->sin_addr.s_addr = INADDR_ANY;
break;
case AF_INET6:
memcpy(&((struct sockaddr_in6 *)&vaddr)->sin6_addr, &in6addr_any, sizeof(struct in6_addr));
memcpy(&((struct sockaddr_in6 *)&vaddr[0])->sin6_addr, &in6addr_any, sizeof(struct in6_addr));
break;
default:
return;
}
}
switch(family)
{
switch(family) {
case AF_INET:
SET_SS_LEN(&vaddr, sizeof(struct sockaddr_in));
SET_SS_FAMILY(&vaddr, AF_INET);
SET_SS_PORT(&vaddr, htons(port));
SET_SS_LEN(&vaddr[0], sizeof(struct sockaddr_in));
SET_SS_FAMILY(&vaddr[0], AF_INET);
SET_SS_PORT(&vaddr[0], htons(port));
break;
case AF_INET6:
SET_SS_LEN(&vaddr, sizeof(struct sockaddr_in6));
SET_SS_FAMILY(&vaddr, AF_INET6);
SET_SS_PORT(&vaddr, htons(port));
SET_SS_LEN(&vaddr[0], sizeof(struct sockaddr_in6));
SET_SS_FAMILY(&vaddr[0], AF_INET6);
SET_SS_PORT(&vaddr[0], htons(port));
break;
default:
break;
}
if((listener = find_listener(&vaddr)))
{
if ((listener = find_listener(vaddr, 0))) {
if (listener->F != NULL)
return;
}
else
{
listener = make_listener(&vaddr);
} else {
listener = make_listener(vaddr);
listener->next = ListenerPollList;
ListenerPollList = listener;
}
@ -379,13 +360,86 @@ add_listener(int port, const char *vhost_ip, int family, int ssl, int defer_acce
listener->F = NULL;
listener->ssl = ssl;
listener->defer_accept = defer_accept;
listener->sctp = 0;
listener->wsock = wsock;
if(inetport(listener))
if (inetport(listener)) {
listener->active = 1;
else
} else {
close_listener(listener);
}
}
/*
* add_sctp_listener- create a new listener
* port - the port number to listen on
* vhost_ip1/2 - if non-null must contain a valid IP address string
*/
void
add_sctp_listener(int port, const char *vhost_ip1, const char *vhost_ip2, int ssl, int wsock)
{
struct Listener *listener;
struct rb_sockaddr_storage vaddr[ARRAY_SIZE(listener->addr)];
/*
* if no port in conf line, don't bother
*/
if (port == 0)
return;
memset(&vaddr, 0, sizeof(vaddr));
if (vhost_ip1 != NULL) {
if (rb_inet_pton_sock(vhost_ip1, (struct sockaddr *)&vaddr[0]) <= 0)
return;
if (vhost_ip2 != NULL) {
if (rb_inet_pton_sock(vhost_ip2, (struct sockaddr *)&vaddr[1]) <= 0)
return;
} else {
SET_SS_FAMILY(&vaddr[1], AF_UNSPEC);
SET_SS_LEN(&vaddr[1], sizeof(struct sockaddr_storage));
}
if (GET_SS_FAMILY(&vaddr[0]) == AF_INET && GET_SS_FAMILY(&vaddr[1]) == AF_INET6) {
/* always put INET6 first */
struct rb_sockaddr_storage tmp;
tmp = vaddr[0];
vaddr[0] = vaddr[1];
vaddr[1] = tmp;
}
} else {
memcpy(&((struct sockaddr_in6 *)&vaddr[0])->sin6_addr, &in6addr_any, sizeof(struct in6_addr));
SET_SS_FAMILY(&vaddr[0], AF_INET6);
SET_SS_LEN(&vaddr[0], sizeof(struct sockaddr_in6));
SET_SS_FAMILY(&vaddr[1], AF_UNSPEC);
SET_SS_LEN(&vaddr[1], sizeof(struct sockaddr_storage));
}
SET_SS_PORT(&vaddr[0], htons(port));
SET_SS_PORT(&vaddr[1], htons(port));
if ((listener = find_listener(vaddr, 1))) {
if(listener->F != NULL)
return;
} else {
listener = make_listener(vaddr);
listener->next = ListenerPollList;
ListenerPollList = listener;
}
listener->F = NULL;
listener->ssl = ssl;
listener->defer_accept = 0;
listener->sctp = 1;
listener->wsock = wsock;
if (inetport(listener)) {
listener->active = 1;
} else {
close_listener(listener);
}
}
/*
* close_listener - close a single listener
@ -460,6 +514,10 @@ add_connection(struct Listener *listener, rb_fde_t *F, struct sockaddr *sai, str
rb_strlcpy(new_client->host, new_client->sockhost, sizeof(new_client->host));
if (listener->sctp) {
SetSCTP(new_client);
}
if (listener->ssl)
{
rb_fde_t *xF[2];

View file

@ -352,6 +352,7 @@ static struct mode_table auth_table[] = {
{"need_ssl", CONF_FLAGS_NEED_SSL },
{"need_sasl", CONF_FLAGS_NEED_SASL },
{"extend_chans", CONF_FLAGS_EXTEND_CHANS },
{"allow_sctp", CONF_FLAGS_ALLOW_SCTP },
{NULL, 0}
};
@ -830,13 +831,15 @@ conf_set_class_sendq(void *data)
yy_class->max_sendq = *(unsigned int *) data;
}
static char *listener_address;
static char *listener_address[2];
static int
conf_begin_listen(struct TopConf *tc)
{
rb_free(listener_address);
listener_address = NULL;
for (int i = 0; i < ARRAY_SIZE(listener_address); i++) {
rb_free(listener_address[i]);
listener_address[i] = NULL;
}
yy_wsock = 0;
yy_defer_accept = 0;
return 0;
@ -845,8 +848,10 @@ conf_begin_listen(struct TopConf *tc)
static int
conf_end_listen(struct TopConf *tc)
{
rb_free(listener_address);
listener_address = NULL;
for (int i = 0; i < ARRAY_SIZE(listener_address); i++) {
rb_free(listener_address[i]);
listener_address[i] = NULL;
}
yy_wsock = 0;
yy_defer_accept = 0;
return 0;
@ -865,31 +870,35 @@ conf_set_listen_wsock(void *data)
}
static void
conf_set_listen_port_both(void *data, int ssl)
conf_set_listen_port_both(void *data, int ssl, int sctp)
{
conf_parm_t *args = data;
for (; args; args = args->next)
{
if(CF_TYPE(args->type) != CF_INT)
{
conf_report_error
("listener::port argument is not an integer " "-- ignoring.");
conf_report_error("listener::port argument is not an integer -- ignoring.");
continue;
}
if(listener_address == NULL)
if(listener_address[0] == NULL)
{
if (!ssl)
{
conf_report_warning("listener 'ANY/%d': support for plaintext listeners may be removed in a future release per RFCs 7194 & 7258. "
"It is suggested that users be migrated to SSL/TLS connections.", args->v.number);
}
add_listener(args->v.number, listener_address, AF_INET, ssl, ssl || yy_defer_accept, yy_wsock);
add_listener(args->v.number, listener_address, AF_INET6, ssl, ssl || yy_defer_accept, yy_wsock);
if (sctp) {
conf_report_error("listener::sctp_port has no addresses -- ignoring.");
} else {
add_tcp_listener(args->v.number, NULL, AF_INET, ssl, ssl || yy_defer_accept, yy_wsock);
add_tcp_listener(args->v.number, NULL, AF_INET6, ssl, ssl || yy_defer_accept, yy_wsock);
}
}
else
{
int family;
if(strchr(listener_address, ':') != NULL)
if(strchr(listener_address[0], ':') != NULL)
family = AF_INET6;
else
family = AF_INET;
@ -900,7 +909,15 @@ conf_set_listen_port_both(void *data, int ssl)
"It is suggested that users be migrated to SSL/TLS connections.", listener_address, args->v.number);
}
add_listener(args->v.number, listener_address, family, ssl, ssl || yy_defer_accept, yy_wsock);
if (sctp) {
#ifdef HAVE_LIBSCTP
add_sctp_listener(args->v.number, listener_address[0], listener_address[1], ssl, yy_wsock);
#else
conf_report_error("Warning -- ignoring listener::sctp_port -- SCTP support not available.");
#endif
} else {
add_tcp_listener(args->v.number, listener_address[0], family, ssl, ssl || yy_defer_accept, yy_wsock);
}
}
}
}
@ -908,20 +925,33 @@ conf_set_listen_port_both(void *data, int ssl)
static void
conf_set_listen_port(void *data)
{
conf_set_listen_port_both(data, 0);
conf_set_listen_port_both(data, 0, 0);
}
static void
conf_set_listen_sslport(void *data)
{
conf_set_listen_port_both(data, 1);
conf_set_listen_port_both(data, 1, 0 );
}
static void
conf_set_listen_sctp_port(void *data)
{
conf_set_listen_port_both(data, 0, 1);
}
static void
conf_set_listen_sctp_sslport(void *data)
{
conf_set_listen_port_both(data, 1, 1);
}
static void
conf_set_listen_address(void *data)
{
rb_free(listener_address);
listener_address = rb_strdup(data);
rb_free(listener_address[1]);
listener_address[1] = listener_address[0];
listener_address[0] = rb_strdup(data);
}
static int
@ -2849,6 +2879,8 @@ newconf_init()
add_conf_item("listen", "wsock", CF_YESNO, conf_set_listen_wsock);
add_conf_item("listen", "port", CF_INT | CF_FLIST, conf_set_listen_port);
add_conf_item("listen", "sslport", CF_INT | CF_FLIST, conf_set_listen_sslport);
add_conf_item("listen", "sctp_port", CF_INT | CF_FLIST, conf_set_listen_sctp_port);
add_conf_item("listen", "sctp_sslport", CF_INT | CF_FLIST, conf_set_listen_sctp_sslport);
add_conf_item("listen", "ip", CF_QSTRING, conf_set_listen_address);
add_conf_item("listen", "host", CF_QSTRING, conf_set_listen_address);

View file

@ -295,9 +295,8 @@ read_packet(rb_fde_t * F, void *data)
}
}
/* bail if short read */
if(length < READBUF_SIZE)
{
/* bail if short read, but not for SCTP as it returns data in packets */
if (length < READBUF_SIZE && !(rb_get_type(client_p->localClient->F) & RB_FD_SCTP)) {
rb_setselect(client_p->localClient->F, RB_SELECT_READ, read_packet, client_p);
return;
}

View file

@ -256,7 +256,7 @@ check_client(struct Client *client_p, struct Client *source_p, const char *usern
case NOT_AUTHORISED:
{
int port = -1;
port = ntohs(GET_SS_PORT(&source_p->localClient->listener->addr));
port = ntohs(GET_SS_PORT(&source_p->localClient->listener->addr[0]));
ServerStats.is_ref++;
/* jdc - lists server name & port connections are on */

View file

@ -447,6 +447,14 @@ register_local_user(struct Client *client_p, struct Client *source_p)
return (CLIENT_EXITED);
}
if(IsSCTP(source_p) && !IsConfAllowSCTP(aconf))
{
ServerStats.is_ref++;
sendto_one_notice(source_p, ":*** Notice -- You are not allowed to use SCTP on this server");
exit_client(client_p, source_p, &me, "SCTP not allowed");
return (CLIENT_EXITED);
}
if(!IsGotId(source_p))
{
const char *p;

View file

@ -104,7 +104,7 @@ AC_TYPE_UID_T
dnl Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS([crypt.h unistd.h sys/socket.h sys/stat.h sys/time.h time.h netinet/in.h netinet/tcp.h arpa/inet.h errno.h sys/uio.h spawn.h sys/poll.h sys/epoll.h sys/select.h sys/devpoll.h sys/event.h port.h signal.h sys/signalfd.h sys/timerfd.h])
AC_CHECK_HEADERS([crypt.h unistd.h sys/socket.h sys/stat.h sys/time.h time.h netinet/in.h netinet/tcp.h netinet/sctp.h arpa/inet.h errno.h sys/uio.h spawn.h sys/poll.h sys/epoll.h sys/select.h sys/devpoll.h sys/event.h port.h signal.h sys/signalfd.h sys/timerfd.h])
AC_HEADER_TIME
dnl Networking Functions
@ -392,6 +392,27 @@ dnl End OpenSSL detection
AC_ARG_WITH(sctp-path,
AC_HELP_STRING([--with-sctp-path=DIR],[Path to libsctp.so for SCTP support.]),
[LIBS="$LIBS -L$withval"],)
AC_ARG_ENABLE(sctp,
AC_HELP_STRING([--disable-sctp],[Disable SCTP support]),
[sctp=$enableval],[sctp=yes])
if test "$sctp" = yes; then
AC_CHECK_HEADER(netinet/sctp.h, [
AC_CHECK_LIB(sctp, sctp_bindx,
[
AC_SUBST(LIBSCTP_LD, -lsctp)
AC_DEFINE(HAVE_LIBSCTP, 1, [Define to 1 if libsctp (-lsctp) is available.])
], sctp=no)
], sctp=no)
fi
dnl Debug-related options
dnl =====================
@ -525,6 +546,10 @@ if test "x$rb_netinet_tcp_h" == "xyes"; then
echo '#include <netinet/tcp.h>' >> $outfile
fi
if test "x$rb_netinet_sctp_h" == "xyes"; then
echo '#include <netinet/sctp.h>' >> $outfile
fi
if test "x$rb_arpa_inet_h" = "xyes"; then
echo '#include <arpa/inet.h>' >> $outfile
fi
@ -614,6 +639,9 @@ fi
if test x$ac_cv_header_netinet_tcp_h = xyes; then
rb_netinet_tcp_h=yes
fi
if test x$ac_cv_header_netinet_sctp_h = xyes; then
rb_netinet_sctp_h=yes
fi
if test x$ac_cv_header_crypt_h = xyes; then
rb_crypt_h=yes
@ -666,4 +694,5 @@ echo "Installing into: $prefix"
echo "Assert debugging ............... $assert"
echo "SSL Type........................ $SSL_TYPE"
echo "SCTP............................ $sctp"
echo

View file

@ -71,6 +71,9 @@ enum
#define RB_FD_LISTEN 0x10
#define RB_FD_SSL 0x20
#define RB_FD_UNKNOWN 0x40
#define RB_FD_SCTP 0x80
#define RB_FD_INHERIT_TYPES (RB_FD_SCTP)
#define RB_RW_IO_ERROR -1 /* System call error */
#define RB_RW_SSL_ERROR -2 /* SSL Error */
@ -130,6 +133,10 @@ rb_fde_t *rb_socket(int family, int sock_type, int proto, const char *note);
int rb_socketpair(int family, int sock_type, int proto, rb_fde_t **F1, rb_fde_t **F2,
const char *note);
int rb_bind(rb_fde_t *F, struct sockaddr *addr);
int rb_sctp_bindx(rb_fde_t *F, struct sockaddr_storage *addrs, size_t len);
int rb_inet_get_proto(rb_fde_t *F);
void rb_accept_tcp(rb_fde_t *, ACPRE * precb, ACCB * callback, void *data);
ssize_t rb_write(rb_fde_t *, const void *buf, int count);
ssize_t rb_writev(rb_fde_t *, struct rb_iovec *vector, int count);

View file

@ -43,5 +43,5 @@ librb_la_SOURCES = \
version.c
librb_la_LDFLAGS = @SSL_LIBS@ @GNUTLS_LIBS@ @MBEDTLS_LIBS@ -avoid-version -no-undefined -export-symbols export-syms.txt
librb_la_LIBADD = @SSL_LIBS@ @GNUTLS_LIBS@ @MBEDTLS_LIBS@
librb_la_LIBADD = @SSL_LIBS@ @GNUTLS_LIBS@ @MBEDTLS_LIBS@ @LIBSCTP_LD@
lib_LTLIBRARIES = librb.la

View file

@ -339,9 +339,165 @@ rb_checktimeouts(void *notused)
}
}
static void
rb_accept_tryaccept(rb_fde_t *F, void *data)
static int
rb_setsockopt_reuseaddr(rb_fde_t *F)
{
int opt_one = 1;
int ret;
ret = setsockopt(F->fd, SOL_SOCKET, SO_REUSEADDR, &opt_one, sizeof(opt_one));
if (ret) {
rb_lib_log("rb_setsockopt_reuseaddr: Cannot set SO_REUSEADDR for FD %d: %s",
F->fd, strerror(rb_get_sockerr(F)));
return ret;
}
return 0;
}
#ifdef HAVE_LIBSCTP
static int
rb_setsockopt_sctp(rb_fde_t *F)
{
int opt_zero = 0;
int opt_one = 1;
int ret;
struct sctp_initmsg initmsg;
struct sctp_rtoinfo rtoinfo;
struct sctp_paddrparams paddrparams;
struct sctp_assocparams assocparams;
ret = setsockopt(F->fd, SOL_SCTP, SCTP_NODELAY, &opt_one, sizeof(opt_one));
if (ret) {
rb_lib_log("rb_setsockopt_sctp: Cannot set SCTP_NODELAY for fd %d: %s",
F->fd, strerror(rb_get_sockerr(F)));
return ret;
}
ret = setsockopt(F->fd, SOL_SCTP, SCTP_I_WANT_MAPPED_V4_ADDR, &opt_zero, sizeof(opt_zero));
if (ret) {
rb_lib_log("rb_setsockopt_sctp: Cannot unset SCTP_I_WANT_MAPPED_V4_ADDR for fd %d: %s",
F->fd, strerror(rb_get_sockerr(F)));
return ret;
}
/* Configure INIT message to specify that we only want one stream */
memset(&initmsg, 0, sizeof(initmsg));
initmsg.sinit_num_ostreams = 1;
initmsg.sinit_max_instreams = 1;
ret = setsockopt(F->fd, SOL_SCTP, SCTP_INITMSG, &initmsg, sizeof(initmsg));
if (ret) {
rb_lib_log("rb_setsockopt_sctp: Cannot set SCTP_INITMSG for fd %d: %s",
F->fd, strerror(rb_get_sockerr(F)));
return ret;
}
/* Configure RTO values to reduce the maximum timeout */
memset(&rtoinfo, 0, sizeof(rtoinfo));
rtoinfo.srto_initial = 3000;
rtoinfo.srto_min = 1000;
rtoinfo.srto_max = 10000;
ret = setsockopt(F->fd, SOL_SCTP, SCTP_RTOINFO, &rtoinfo, sizeof(rtoinfo));
if (ret) {
rb_lib_log("rb_setsockopt_sctp: Cannot set SCTP_RTOINFO for fd %d: %s",
F->fd, strerror(rb_get_sockerr(F)));
return ret;
}
/*
* Configure peer address parameters to ensure that we monitor the connection
* more often than the default and don't timeout retransmit attempts before
* the ping timeout does.
*
* Each peer address will timeout reachability in about 750s.
*/
memset(&paddrparams, 0, sizeof(paddrparams));
paddrparams.spp_assoc_id = 0;
memcpy(&paddrparams.spp_address, &in6addr_any, sizeof(in6addr_any));
paddrparams.spp_pathmaxrxt = 50;
paddrparams.spp_hbinterval = 5000;
paddrparams.spp_flags |= SPP_HB_ENABLE;
ret = setsockopt(F->fd, SOL_SCTP, SCTP_PEER_ADDR_PARAMS, &paddrparams, sizeof(paddrparams));
if (ret) {
rb_lib_log("rb_setsockopt_sctp: Cannot set SCTP_PEER_ADDR_PARAMS for fd %d: %s",
F->fd, strerror(rb_get_sockerr(F)));
return ret;
}
/* Configure association parameters for retransmit attempts as above */
memset(&assocparams, 0, sizeof(assocparams));
assocparams.sasoc_assoc_id = 0;
assocparams.sasoc_asocmaxrxt = 50;
ret = setsockopt(F->fd, SOL_SCTP, SCTP_ASSOCINFO, &assocparams, sizeof(assocparams));
if (ret) {
rb_lib_log("rb_setsockopt_sctp: Cannot set SCTP_ASSOCINFO for fd %d: %s",
F->fd, strerror(rb_get_sockerr(F)));
return ret;
}
return 0;
}
#endif
int
rb_bind(rb_fde_t *F, struct sockaddr *addr)
{
int ret;
ret = rb_setsockopt_reuseaddr(F);
if (ret)
return ret;
ret = bind(F->fd, addr, GET_SS_LEN(addr));
if (ret)
return ret;
return 0;
}
int
rb_sctp_bindx(rb_fde_t *F, struct sockaddr_storage *addrs, size_t len)
{
#ifdef HAVE_LIBSCTP
int ret;
if ((F->type & RB_FD_SCTP) == 0)
return -1;
ret = rb_setsockopt_reuseaddr(F);
if (ret)
return ret;
for (size_t i = 0; i < len; i++) {
if (GET_SS_FAMILY(&addrs[i]) == AF_UNSPEC)
continue;
ret = sctp_bindx(F->fd, (struct sockaddr *)&addrs[i], 1, SCTP_BINDX_ADD_ADDR);
if (ret)
return ret;
}
return 0;
#else
return -1;
#endif
}
int
rb_inet_get_proto(rb_fde_t *F)
{
#ifdef HAVE_LIBSCTP
if (F->type & RB_FD_SCTP)
return IPPROTO_SCTP;
#endif
return IPPROTO_TCP;
}
static void rb_accept_tryaccept(rb_fde_t *F, void *data) {
struct rb_sockaddr_storage st;
rb_fde_t *new_F;
rb_socklen_t addrlen;
@ -362,12 +518,12 @@ rb_accept_tryaccept(rb_fde_t *F, void *data)
rb_fd_hack(&new_fd);
new_F = rb_open(new_fd, RB_FD_SOCKET, "Incoming Connection");
new_F = rb_open(new_fd, RB_FD_SOCKET | (F->type & RB_FD_INHERIT_TYPES), "Incoming Connection");
if(new_F == NULL)
{
rb_lib_log
("rb_accept: new_F == NULL on incoming connection. Closing new_fd == %d\n",
("rb_accept: new_F == NULL on incoming connection. Closing new_fd == %d",
new_fd);
close(new_fd);
continue;
@ -714,15 +870,19 @@ rb_socket(int family, int sock_type, int proto, const char *note)
/*
* Make sure we can take both IPv4 and IPv6 connections
* on an AF_INET6 socket
* on an AF_INET6 SCTP socket, otherwise keep them separate
*/
if(family == AF_INET6)
{
int off = 1;
if(setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (void *) &off, sizeof(off)) == -1)
#ifdef HAVE_LIBSCTP
int v6only = (proto == IPPROTO_SCTP) ? 0 : 1;
#else
int v6only = 1;
#endif
if(setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (void *) &v6only, sizeof(v6only)) == -1)
{
rb_lib_log("rb_socket: Could not set IPV6_V6ONLY option to 1 on FD %d: %s",
fd, strerror(errno));
rb_lib_log("rb_socket: Could not set IPV6_V6ONLY option to %d on FD %d: %s",
v6only, fd, strerror(errno));
close(fd);
return NULL;
}
@ -736,6 +896,20 @@ rb_socket(int family, int sock_type, int proto, const char *note)
close(fd);
return NULL;
}
#ifdef HAVE_LIBSCTP
if (proto == IPPROTO_SCTP) {
F->type |= RB_FD_SCTP;
if (rb_setsockopt_sctp(F)) {
rb_lib_log("rb_socket: Could not set SCTP socket options on FD %d: %s",
fd, strerror(errno));
close(fd);
return NULL;
}
}
#endif
/* Set the socket non-blocking, and other wonderful bits */
if(rb_unlikely(!rb_set_nb(F)))
{
@ -775,7 +949,7 @@ rb_listen(rb_fde_t *F, int backlog, int defer_accept)
{
int result;
F->type = RB_FD_SOCKET | RB_FD_LISTEN;
F->type = RB_FD_SOCKET | RB_FD_LISTEN | (F->type & RB_FD_INHERIT_TYPES);
result = listen(F->fd, backlog);
#ifdef TCP_DEFER_ACCEPT
@ -878,6 +1052,11 @@ rb_close(rb_fde_t *F)
lrb_assert(F->read_handler == NULL);
lrb_assert(F->write_handler == NULL);
}
if (type & RB_FD_LISTEN) {
listen(F->fd, 0);
}
rb_setselect(F, RB_SELECT_WRITE | RB_SELECT_READ, NULL, NULL);
rb_settimeout(F, 0, NULL, NULL);
rb_free(F->accept);

View file

@ -11,6 +11,7 @@ rb_bh_free
rb_bh_total_usage
rb_bh_usage
rb_bh_usage_all
rb_bind
rb_checktimeouts
rb_clear_patricia
rb_close
@ -77,6 +78,7 @@ rb_helper_start
rb_helper_write
rb_helper_write_queue
rb_ignore_errno
rb_inet_get_proto
rb_inet_ntop
rb_inet_ntop_sock
rb_inet_pton
@ -146,6 +148,7 @@ rb_rawbuf_length
rb_read
rb_recv_fd_buf
rb_run_one_event
rb_sctp_bindx
rb_select
rb_send_fd_buf
rb_set_buffers

View file

@ -410,22 +410,14 @@ rb_setup_fd_win32(rb_fde_t *F)
return 0;
SetHandleInformation((HANDLE) F->fd, HANDLE_FLAG_INHERIT, 0);
switch (F->type)
{
case RB_FD_SOCKET:
{
if (F->type & RB_FD_SOCKET) {
unsigned long nonb = 1;
if(ioctlsocket((SOCKET) F->fd, FIONBIO, &nonb) == -1)
{
if (ioctlsocket((SOCKET) F->fd, FIONBIO, &nonb) == -1) {
rb_get_errno();
return 0;
}
return 1;
}
default:
return 1;
}
}
void