chiark / gitweb /
build system: commit configure
[secnet.git] / site.c
diff --git a/site.c b/site.c
index 2b51a5d0c6b41941ab89640de4c1f0e0545d4daa..2fafd915b4beeacfa069c9efe8e83f516799bced 100644 (file)
--- a/site.c
+++ b/site.c
@@ -16,6 +16,7 @@
 #include "secnet.h"
 #include <stdio.h>
 #include <string.h>
+#include <limits.h>
 #include <sys/socket.h>
 
 #include <sys/mman.h>
@@ -28,7 +29,7 @@
 #define DEFAULT_KEY_LIFETIME 3600000 /* One hour */
 #define DEFAULT_KEY_RENEGOTIATE_GAP 300000 /* Five minutes */
 #define DEFAULT_SETUP_RETRIES 5
-#define DEFAULT_SETUP_TIMEOUT 1000
+#define DEFAULT_SETUP_TIMEOUT 2000
 #define DEFAULT_WAIT_TIME 20000
 
 /* Each site can be in one of several possible states. */
@@ -78,7 +79,7 @@
 #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";
@@ -193,7 +194,7 @@ struct site {
     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];
@@ -223,8 +224,8 @@ static void slog(struct site *st, uint32_t event, string_t msg, ...)
 }
 
 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);
@@ -256,10 +257,10 @@ struct msg {
 
 /* 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;
@@ -275,15 +276,20 @@ static bool_t generate_msg(struct site *st, uint32_t type, string_t what)
     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;
 }
 
@@ -330,7 +336,7 @@ static bool_t unpick_msg(struct site *st, uint32_t type,
 }
 
 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;
 
@@ -392,7 +398,7 @@ static bool_t process_msg2(struct site *st, struct buffer_if *msg2,
                           struct sockaddr_in *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)) {
@@ -416,9 +422,9 @@ static bool_t process_msg3(struct site *st, struct buffer_if *msg3,
                           struct sockaddr_in *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)) {
@@ -427,6 +433,7 @@ static bool_t process_msg3(struct site *st, struct buffer_if *msg3,
     }
 
     /* 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);
@@ -434,8 +441,10 @@ static bool_t process_msg3(struct site *st, struct buffer_if *msg3,
     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;
@@ -464,9 +473,9 @@ static bool_t process_msg4(struct site *st, struct buffer_if *msg4,
                           struct sockaddr_in *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)) {
@@ -475,6 +484,7 @@ static bool_t process_msg4(struct site *st, struct buffer_if *msg4,
     }
     
     /* 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);
@@ -482,8 +492,10 @@ static bool_t process_msg4(struct site *st, struct buffer_if *msg4,
     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;
@@ -518,7 +530,7 @@ static bool_t unpick_msg0(struct site *st, struct buffer_if *msg0,
 
 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 */
@@ -541,7 +553,7 @@ static bool_t process_msg5(struct site *st, struct buffer_if *msg5,
                           struct sockaddr_in *src)
 {
     struct msg0 m;
-    string_t transform_err;
+    cstring_t transform_err;
 
     if (!unpick_msg0(st,msg5,&m)) return False;
 
@@ -567,7 +579,7 @@ static bool_t process_msg5(struct site *st, struct buffer_if *msg5,
 
 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 */
@@ -590,7 +602,7 @@ static bool_t process_msg6(struct site *st, struct buffer_if *msg6,
                           struct sockaddr_in *src)
 {
     struct msg0 m;
-    string_t transform_err;
+    cstring_t transform_err;
 
     if (!unpick_msg0(st,msg6,&m)) return False;
 
@@ -618,7 +630,7 @@ static bool_t process_msg0(struct site *st, struct buffer_if *msg0,
                           struct sockaddr_in *src)
 {
     struct msg0 m;
-    string_t transform_err;
+    cstring_t transform_err;
     uint32_t type;
 
     if (!st->current_valid) {
@@ -663,9 +675,9 @@ static void dump_packet(struct site *st, struct buffer_if *buf,
     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)
@@ -710,7 +722,7 @@ static void site_resolve_callback(void *sst, struct in_addr *address)
     }
 }
 
-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);
@@ -749,7 +761,7 @@ static void activate_new_key(struct site *st)
     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);
@@ -763,7 +775,7 @@ static void delete_key(struct site *st, string_t reason, uint32_t loglevel)
 
 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)
@@ -820,6 +832,8 @@ static bool_t enter_state_resolve(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:
@@ -853,11 +867,19 @@ static bool_t enter_new_state(struct site *st, uint32_t next)
        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_timeout,
+                 send_msg, st);
+    
+    if (r) {
        st->state=next;
        if (next==SITE_RUN) {
            BUF_FREE(&st->buffer); /* Never reused */
@@ -875,9 +897,9 @@ static bool_t enter_new_state(struct site *st, uint32_t next)
 }
 
 /* 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");
@@ -910,6 +932,17 @@ static void enter_state_wait(struct site *st)
     /* XXX Erase keys etc. */
 }
 
+static inline void site_settimeout(uint64_t timeout, uint64_t *now,
+                                  int *timeout_io)
+{
+    if (timeout) {
+       uint64_t offset=timeout-*now;
+       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)
@@ -922,12 +955,8 @@ static int site_beforepoll(void *sst, struct pollfd *fds, int *nfds_io,
     /* 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, now, timeout_io);
+    site_settimeout(st->current_key_timeout, now, timeout_io);
 
     return 0; /* success */
 }
@@ -941,9 +970,10 @@ static void site_afterpoll(void *sst, struct pollfd *fds, int nfds,
     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",
@@ -961,7 +991,7 @@ static void site_afterpoll(void *sst, struct pollfd *fds, int nfds,
 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);