#include "secnet.h"
#include <stdio.h>
#include <string.h>
+#include <limits.h>
+#include <assert.h>
#include <sys/socket.h>
#include <sys/mman.h>
#define SETUP_BUFFER_LEN 2048
-#define DEFAULT_KEY_LIFETIME 3600000 /* One hour */
-#define DEFAULT_KEY_RENEGOTIATE_GAP 300000 /* Five minutes */
+#define DEFAULT_KEY_LIFETIME (3600*1000) /* [ms] */
+#define DEFAULT_KEY_RENEGOTIATE_GAP (5*60*1000) /* [ms] */
#define DEFAULT_SETUP_RETRIES 5
-#define DEFAULT_SETUP_TIMEOUT 1000
-#define DEFAULT_WAIT_TIME 20000
+#define DEFAULT_SETUP_RETRY_INTERVAL (2*1000) /* [ms] */
+#define DEFAULT_WAIT_TIME (20*1000) /* [ms] */
/* Each site can be in one of several possible states. */
#define SITE_SENTMSG5 7
#define SITE_WAIT 8
-static string_t state_name(uint32_t state)
+static cstring_t state_name(uint32_t state)
{
switch (state) {
case 0: return "STOP";
struct dh_if *dh;
struct hash_if *hash;
- uint32_t setup_retries; /* How many times to send setup packets */
- uint32_t setup_timeout; /* Initial timeout for setup packets */
- uint32_t wait_timeout; /* How long to wait if setup unsuccessful */
- uint32_t key_lifetime; /* How long a key lasts once set up */
- uint32_t key_renegotiate_time; /* If we see traffic (or a keepalive)
+ uint32_t index; /* Index of this site */
+ int32_t setup_retries; /* How many times to send setup packets */
+ int32_t setup_retry_interval; /* Initial timeout for setup packets */
+ int32_t wait_timeout; /* How long to wait if setup unsuccessful */
+ int32_t key_lifetime; /* How long a key lasts once set up */
+ int32_t key_renegotiate_time; /* If we see traffic (or a keepalive)
after this time, initiate a new
key exchange */
- bool_t keepalive; /* Send keepalives to detect peer failure (not yet
- implemented) */
uint8_t *setupsig; /* Expected signature of incoming MSG1 packets */
- uint32_t setupsiglen; /* Allows us to discard packets quickly if
- they are not for us */
+ int32_t setupsiglen; /* Allows us to discard packets quickly if
+ they are not for us */
bool_t setup_priority; /* Do we have precedence if both sites emit
message 1 simultaneously? */
uint32_t log_events;
bool_t current_valid;
uint64_t current_key_timeout; /* End of life of current key */
uint64_t renegotiate_key_time; /* When we can negotiate a new key */
- struct sockaddr_in peer; /* Current address of peer */
+ struct comm_addr peer; /* Current address of peer */
bool_t peer_valid; /* Peer address becomes invalid when key times out,
but only if we have a DNS name for our peer */
timeout before we can listen for another setup packet); perhaps
we should keep a list of 'bad' sources for setup packets. */
uint32_t setup_session_id;
- struct sockaddr_in setup_peer;
+ struct comm_addr setup_peer;
uint8_t localN[NONCELEN]; /* Nonces for key exchange */
uint8_t remoteN[NONCELEN];
struct buffer_if buffer; /* Current outgoing key exchange packet */
- uint32_t retries; /* Number of retries remaining */
+ int32_t retries; /* Number of retries remaining */
uint64_t timeout; /* Timeout for current state */
uint8_t *dhsecret;
uint8_t *sharedsecret;
struct transform_inst_if *new_transform; /* For key setup/verify */
};
-static void slog(struct site *st, uint32_t event, string_t msg, ...)
+static void slog(struct site *st, uint32_t event, cstring_t msg, ...)
{
va_list ap;
- uint8_t buf[240];
+ char buf[240];
uint32_t class;
va_start(ap,msg);
default: class=M_ERR; break;
}
- vsnprintf(buf,240,msg,ap);
+ vsnprintf(buf,sizeof(buf),msg,ap);
st->log->log(st->log->st,class,"%s: %s",st->tunname,buf);
}
va_end(ap);
}
static void set_link_quality(struct site *st);
-static void delete_key(struct site *st, string_t reason, uint32_t loglevel);
-static bool_t initiate_key_setup(struct site *st, string_t reason);
+static void delete_key(struct site *st, cstring_t reason, uint32_t loglevel);
+static bool_t initiate_key_setup(struct site *st, cstring_t reason);
static void enter_state_run(struct site *st);
static bool_t enter_state_resolve(struct site *st);
static bool_t enter_new_state(struct site *st,uint32_t next);
uint8_t *hashstart;
uint32_t dest;
uint32_t source;
- uint32_t remlen;
+ int32_t remlen;
uint8_t *remote;
- uint32_t loclen;
+ int32_t loclen;
uint8_t *local;
uint8_t *nR;
uint8_t *nL;
- uint32_t pklen;
- uint8_t *pk;
- uint32_t hashlen;
- uint32_t siglen;
- uint8_t *sig;
+ int32_t pklen;
+ char *pk;
+ int32_t hashlen;
+ int32_t siglen;
+ char *sig;
};
/* Build any of msg1 to msg4. msg5 and msg6 are built from the inside
out using a transform of config data supplied by netlink */
-static bool_t generate_msg(struct site *st, uint32_t type, string_t what)
+static bool_t generate_msg(struct site *st, uint32_t type, cstring_t what)
{
void *hst;
- uint8_t *hash=alloca(st->hash->len);
+ uint8_t *hash;
string_t dhpub, sig;
st->retries=st->setup_retries;
buffer_init(&st->buffer,0);
buf_append_uint32(&st->buffer,
(type==LABEL_MSG1?0:st->setup_session_id));
- buf_append_uint32(&st->buffer,(uint32_t)st);
+ buf_append_uint32(&st->buffer,st->index);
buf_append_uint32(&st->buffer,type);
buf_append_string(&st->buffer,st->localname);
buf_append_string(&st->buffer,st->remotename);
if (type==LABEL_MSG1) return True;
memcpy(buf_append(&st->buffer,NONCELEN),st->remoteN,NONCELEN);
if (type==LABEL_MSG2) return True;
+
+ if (hacky_par_mid_failnow()) return False;
+
dhpub=st->dh->makepublic(st->dh->st,st->dhsecret,st->dh->len);
buf_append_string(&st->buffer,dhpub);
free(dhpub);
+ hash=safe_malloc(st->hash->len, "generate_msg");
hst=st->hash->init();
st->hash->update(hst,st->buffer.start,st->buffer.size);
st->hash->final(hst,hash);
sig=st->privkey->sign(st->privkey->st,hash,st->hash->len);
buf_append_string(&st->buffer,sig);
free(sig);
+ free(hash);
return True;
}
}
static bool_t check_msg(struct site *st, uint32_t type, struct msg *m,
- string_t *error)
+ cstring_t *error)
{
if (type==LABEL_MSG1) return True;
}
static bool_t process_msg1(struct site *st, struct buffer_if *msg1,
- struct sockaddr_in *src)
+ const struct comm_addr *src)
{
struct msg m;
}
static bool_t process_msg2(struct site *st, struct buffer_if *msg2,
- struct sockaddr_in *src)
+ const struct comm_addr *src)
{
struct msg m;
- string_t err;
+ cstring_t err;
if (!unpick_msg(st,LABEL_MSG2,msg2,&m)) return False;
if (!check_msg(st,LABEL_MSG2,&m,&err)) {
}
static bool_t process_msg3(struct site *st, struct buffer_if *msg3,
- struct sockaddr_in *src)
+ const struct comm_addr *src)
{
struct msg m;
- uint8_t *hash=alloca(st->hash->len);
+ uint8_t *hash;
void *hst;
- string_t err;
+ cstring_t err;
if (!unpick_msg(st,LABEL_MSG3,msg3,&m)) return False;
if (!check_msg(st,LABEL_MSG3,&m,&err)) {
}
/* Check signature and store g^x mod m */
+ hash=safe_malloc(st->hash->len, "process_msg3");
hst=st->hash->init();
st->hash->update(hst,m.hashstart,m.hashlen);
st->hash->final(hst,hash);
m.sig[m.siglen]=0;
if (!st->pubkey->check(st->pubkey->st,hash,st->hash->len,m.sig)) {
slog(st,LOG_SEC,"msg3 signature failed check!");
+ free(hash);
return False;
}
+ free(hash);
/* Terminate their DH public key with a '0' */
m.pk[m.pklen]=0;
}
static bool_t process_msg4(struct site *st, struct buffer_if *msg4,
- struct sockaddr_in *src)
+ const struct comm_addr *src)
{
struct msg m;
- uint8_t *hash=alloca(st->hash->len);
+ uint8_t *hash;
void *hst;
- string_t err;
+ cstring_t err;
if (!unpick_msg(st,LABEL_MSG4,msg4,&m)) return False;
if (!check_msg(st,LABEL_MSG4,&m,&err)) {
}
/* Check signature and store g^x mod m */
+ hash=safe_malloc(st->hash->len, "process_msg4");
hst=st->hash->init();
st->hash->update(hst,m.hashstart,m.hashlen);
st->hash->final(hst,hash);
m.sig[m.siglen]=0;
if (!st->pubkey->check(st->pubkey->st,hash,st->hash->len,m.sig)) {
slog(st,LOG_SEC,"msg4 signature failed check!");
+ free(hash);
return False;
}
+ free(hash);
/* Terminate their DH public key with a '0' */
m.pk[m.pklen]=0;
static bool_t generate_msg5(struct site *st)
{
- string_t transform_err;
+ cstring_t transform_err;
BUF_ALLOC(&st->buffer,"site:MSG5");
/* We are going to add four words to the message */
st->new_transform->forwards(st->new_transform->st,&st->buffer,
&transform_err);
buf_prepend_uint32(&st->buffer,LABEL_MSG5);
- buf_prepend_uint32(&st->buffer,(uint32_t)st);
+ buf_prepend_uint32(&st->buffer,st->index);
buf_prepend_uint32(&st->buffer,st->setup_session_id);
st->retries=st->setup_retries;
}
static bool_t process_msg5(struct site *st, struct buffer_if *msg5,
- struct sockaddr_in *src)
+ const struct comm_addr *src)
{
struct msg0 m;
- string_t transform_err;
+ cstring_t transform_err;
if (!unpick_msg0(st,msg5,&m)) return False;
static bool_t generate_msg6(struct site *st)
{
- string_t transform_err;
+ cstring_t transform_err;
BUF_ALLOC(&st->buffer,"site:MSG6");
/* We are going to add four words to the message */
st->new_transform->forwards(st->new_transform->st,&st->buffer,
&transform_err);
buf_prepend_uint32(&st->buffer,LABEL_MSG6);
- buf_prepend_uint32(&st->buffer,(uint32_t)st);
+ buf_prepend_uint32(&st->buffer,st->index);
buf_prepend_uint32(&st->buffer,st->setup_session_id);
st->retries=1; /* Peer will retransmit MSG5 if this packet gets lost */
}
static bool_t process_msg6(struct site *st, struct buffer_if *msg6,
- struct sockaddr_in *src)
+ const struct comm_addr *src)
{
struct msg0 m;
- string_t transform_err;
+ cstring_t transform_err;
if (!unpick_msg0(st,msg6,&m)) return False;
}
static bool_t process_msg0(struct site *st, struct buffer_if *msg0,
- struct sockaddr_in *src)
+ const struct comm_addr *src)
{
struct msg0 m;
- string_t transform_err;
+ cstring_t transform_err;
uint32_t type;
if (!st->current_valid) {
/* We must forget about the current session. */
delete_key(st,"request from peer",LOG_SEC);
return True;
- break;
case LABEL_MSG9:
/* Deliver to netlink layer */
st->netlink->deliver(st->netlink->st,msg0);
+ /* See whether we should start negotiating a new key */
+ if (st->now > st->renegotiate_key_time)
+ initiate_key_setup(st,"incoming packet in renegotiation window");
return True;
- break;
default:
slog(st,LOG_SEC,"incoming encrypted message of type %08x "
"(unknown)",type);
}
static void dump_packet(struct site *st, struct buffer_if *buf,
- struct sockaddr_in *addr, bool_t incoming)
+ const struct comm_addr *addr, bool_t incoming)
{
uint32_t dest=ntohl(*(uint32_t *)buf->start);
uint32_t source=ntohl(*(uint32_t *)(buf->start+4));
uint32_t msgtype=ntohl(*(uint32_t *)(buf->start+8));
if (st->log_events & LOG_DUMP)
- log(st->log,M_DEBUG,"%s: %s: %08x<-%08x: %08x:",
- st->tunname,incoming?"incoming":"outgoing",
- dest,source,msgtype);
+ slilog(st->log,M_DEBUG,"%s: %s: %08x<-%08x: %08x:",
+ st->tunname,incoming?"incoming":"outgoing",
+ dest,source,msgtype);
}
static uint32_t site_status(void *st)
if (st->retries>0) {
dump_packet(st,&st->buffer,&st->setup_peer,False);
st->comm->sendmsg(st->comm->st,&st->buffer,&st->setup_peer);
- st->timeout=st->now+st->setup_timeout;
+ st->timeout=st->now+st->setup_retry_interval;
st->retries--;
return True;
} else {
}
if (address) {
memset(&st->setup_peer,0,sizeof(st->setup_peer));
- st->setup_peer.sin_family=AF_INET;
- st->setup_peer.sin_port=htons(st->remoteport);
- st->setup_peer.sin_addr=*address;
+ st->setup_peer.comm=st->comm;
+ st->setup_peer.sin.sin_family=AF_INET;
+ st->setup_peer.sin.sin_port=htons(st->remoteport);
+ st->setup_peer.sin.sin_addr=*address;
enter_new_state(st,SITE_SENTMSG1);
} else {
/* Resolution failed */
}
}
-static bool_t initiate_key_setup(struct site *st,string_t reason)
+static bool_t initiate_key_setup(struct site *st, cstring_t reason)
{
if (st->state!=SITE_RUN) return False;
slog(st,LOG_SETUP_INIT,"initiating key exchange (%s)",reason);
enter_state_run(st);
}
-static void delete_key(struct site *st, string_t reason, uint32_t loglevel)
+static void delete_key(struct site *st, cstring_t reason, uint32_t loglevel)
{
if (st->current_valid) {
slog(st,loglevel,"session closed (%s)",reason);
static void state_assert(struct site *st, bool_t ok)
{
- if (!ok) fatal("state_assert\n");
+ if (!ok) fatal("site:state_assert");
}
static void enter_state_stop(struct site *st)
static bool_t enter_new_state(struct site *st, uint32_t next)
{
bool_t (*gen)(struct site *st);
+ int r;
+
slog(st,LOG_STATE,"entering state %s",state_name(next));
switch(next) {
case SITE_SENTMSG1:
break;
default:
gen=NULL;
- fatal("enter_new_state(%s): invalid new state\n",state_name(next));
+ fatal("enter_new_state(%s): invalid new state",state_name(next));
break;
}
- if (gen(st) && send_msg(st)) {
+ if (hacky_par_start_failnow()) return False;
+
+ r= gen(st) && send_msg(st);
+
+ hacky_par_end(&r,
+ st->setup_retries, st->setup_retry_interval,
+ send_msg, st);
+
+ if (r) {
st->state=next;
if (next==SITE_RUN) {
BUF_FREE(&st->buffer); /* Never reused */
}
/* msg7 tells our peer that we're about to forget our key */
-static bool_t send_msg7(struct site *st,string_t reason)
+static bool_t send_msg7(struct site *st, cstring_t reason)
{
- string_t transform_err;
+ cstring_t transform_err;
if (st->current_valid && st->peer_valid && st->buffer.free) {
BUF_ALLOC(&st->buffer,"site:MSG7");
st->current_transform->forwards(st->current_transform->st,
&st->buffer, &transform_err);
buf_prepend_uint32(&st->buffer,LABEL_MSG0);
- buf_prepend_uint32(&st->buffer,(uint32_t)st);
+ buf_prepend_uint32(&st->buffer,st->index);
buf_prepend_uint32(&st->buffer,st->remote_session_id);
+ dump_packet(st,&st->buffer,&st->peer,False);
st->comm->sendmsg(st->comm->st,&st->buffer,&st->peer);
BUF_FREE(&st->buffer);
return True;
slog(st,LOG_STATE,"entering state WAIT");
st->timeout=st->now+st->wait_timeout;
st->state=SITE_WAIT;
- st->peer_valid=False;
set_link_quality(st);
BUF_FREE(&st->buffer); /* will have had an outgoing packet in it */
/* XXX Erase keys etc. */
}
+static inline void site_settimeout(uint64_t timeout, int *timeout_io)
+{
+ if (timeout) {
+ int64_t offset=timeout-*now;
+ if (offset<0) offset=0;
+ if (offset>INT_MAX) offset=INT_MAX;
+ if (*timeout_io<0 || offset<*timeout_io)
+ *timeout_io=offset;
+ }
+}
+
static int site_beforepoll(void *sst, struct pollfd *fds, int *nfds_io,
- int *timeout_io, const struct timeval *tv_now,
- uint64_t *now)
+ int *timeout_io)
{
struct site *st=sst;
/* Work out when our next timeout is. The earlier of 'timeout' or
'current_key_timeout'. A stored value of '0' indicates no timeout
active. */
- if (st->timeout && st->timeout-*now < *timeout_io) {
- *timeout_io=st->timeout-*now;
- }
-
- if (st->current_key_timeout && st->current_key_timeout-*now < *timeout_io)
- *timeout_io=st->current_key_timeout-*now;
+ site_settimeout(st->timeout, timeout_io);
+ site_settimeout(st->current_key_timeout, timeout_io);
return 0; /* success */
}
/* NB site_afterpoll will be called before site_beforepoll is ever called */
-static void site_afterpoll(void *sst, struct pollfd *fds, int nfds,
- const struct timeval *tv_now, uint64_t *now)
+static void site_afterpoll(void *sst, struct pollfd *fds, int nfds)
{
struct site *st=sst;
st->now=*now;
if (st->timeout && *now>st->timeout) {
st->timeout=0;
- if (st->state>=SITE_SENTMSG1 && st->state<=SITE_SENTMSG5)
- send_msg(st);
- else if (st->state==SITE_WAIT) {
+ if (st->state>=SITE_SENTMSG1 && st->state<=SITE_SENTMSG5) {
+ if (!hacky_par_start_failnow())
+ send_msg(st);
+ } else if (st->state==SITE_WAIT) {
enter_state_run(st);
} else {
slog(st,LOG_ERROR,"site_afterpoll: unexpected timeout, state=%d",
static void site_outgoing(void *sst, struct buffer_if *buf)
{
struct site *st=sst;
- string_t transform_err;
+ cstring_t transform_err;
if (st->state==SITE_STOP) {
BUF_FREE(buf);
st->current_transform->forwards(st->current_transform->st,
buf, &transform_err);
buf_prepend_uint32(buf,LABEL_MSG0);
- buf_prepend_uint32(buf,(uint32_t)st);
+ buf_prepend_uint32(buf,st->index);
buf_prepend_uint32(buf,st->remote_session_id);
st->comm->sendmsg(st->comm->st,buf,&st->peer);
}
BUF_FREE(buf);
- /* See whether we should start negotiating a new key */
- if (st->now > st->renegotiate_key_time)
- initiate_key_setup(st,"outgoing packet in renegotiation window");
return;
}
/* This function is called by the communication device to deliver
packets from our peers. */
static bool_t site_incoming(void *sst, struct buffer_if *buf,
- struct sockaddr_in *source)
+ const struct comm_addr *source)
{
struct site *st=sst;
uint32_t dest=ntohl(*(uint32_t *)buf->start);
}
return False; /* Not for us. */
}
- if (dest==(uint32_t)st) {
+ if (dest==st->index) {
/* Explicitly addressed to us */
uint32_t msgtype=ntohl(get_uint32(buf->start+8));
if (msgtype!=LABEL_MSG0) dump_packet(st,buf,source,True);
static list_t *site_apply(closure_t *self, struct cloc loc, dict_t *context,
list_t *args)
{
+ static uint32_t index_sequence;
struct site *st;
item_t *item;
dict_t *dict;
free(st);
return NULL;
}
+ assert(index_sequence < 0xffffffffUL);
+ st->index = ++index_sequence;
st->netlink=find_cl_if(dict,"link",CL_NETLINK,True,"site",loc);
st->comm=find_cl_if(dict,"comm",CL_COMM,True,"site",loc);
st->resolver=find_cl_if(dict,"resolver",CL_RESOLVER,True,"site",loc);
st->dh=find_cl_if(dict,"dh",CL_DH,True,"site",loc);
st->hash=find_cl_if(dict,"hash",CL_HASH,True,"site",loc);
- st->key_lifetime=dict_read_number(
- dict,"key-lifetime",False,"site",loc,DEFAULT_KEY_LIFETIME);
- if (st->key_lifetime < DEFAULT_KEY_RENEGOTIATE_GAP)
+#define DEFAULT(D) DEFAULT_##D
+#define CFG_NUMBER(k,D) dict_read_number(dict,(k),False,"site",loc,DEFAULT(D));
+
+ st->key_lifetime= CFG_NUMBER("key-lifetime", KEY_LIFETIME);
+ st->setup_retries= CFG_NUMBER("setup-retries", SETUP_RETRIES);
+ st->setup_retry_interval= CFG_NUMBER("setup-timeout", SETUP_RETRY_INTERVAL);
+ st->wait_timeout= CFG_NUMBER("wait-time", WAIT_TIME);
+
+ if (st->key_lifetime < DEFAULT(KEY_RENEGOTIATE_GAP)*2)
st->key_renegotiate_time=st->key_lifetime/2;
else
- st->key_renegotiate_time=st->key_lifetime-DEFAULT_KEY_RENEGOTIATE_GAP;
- st->setup_retries=dict_read_number(
- dict,"setup-retries",False,"site",loc,DEFAULT_SETUP_RETRIES);
- st->setup_timeout=dict_read_number(
- dict,"setup-timeout",False,"site",loc,DEFAULT_SETUP_TIMEOUT);
- st->wait_timeout=dict_read_number(
- dict,"wait-time",False,"site",loc,DEFAULT_WAIT_TIME);
+ st->key_renegotiate_time=st->key_lifetime-DEFAULT(KEY_RENEGOTIATE_GAP);
st->key_renegotiate_time=dict_read_number(
- dict,"renegotiate-time",False,"site",loc,st->key_lifetime);
+ dict,"renegotiate-time",False,"site",loc,st->key_renegotiate_time);
if (st->key_renegotiate_time > st->key_lifetime) {
cfgfatal(loc,"site",
"renegotiate-time must be less than key-lifetime\n");
}
- st->keepalive=dict_read_bool(dict,"keepalive",False,"site",loc,False);
st->log_events=string_list_to_word(dict_lookup(dict,"log-events"),
log_event_table,"site");
sprintf(st->tunname,"%s<->%s",st->localname,st->remotename);
/* The information we expect to see in incoming messages of type 1 */
+ /* fixme: lots of unchecked overflows here, but the results are only
+ corrupted packets rather than undefined behaviour */
st->setupsiglen=strlen(st->remotename)+strlen(st->localname)+8;
st->setupsig=safe_malloc(st->setupsiglen,"site_apply");
put_uint32(st->setupsig+0,LABEL_MSG1);
return new_closure(&st->cl);
}
-init_module site_module;
void site_module(dict_t *dict)
{
add_closure(dict,"site",site_apply);