chiark / gitweb /
site: When shutting down, if debug enabled, do dump the MSG7
[secnet.git] / site.c
diff --git a/site.c b/site.c
index b780815b831bbb5968fa4b286918e2999166cfcd..157d9f2455c0b459ee53406db1ab94d694c6bfd9 100644 (file)
--- a/site.c
+++ b/site.c
@@ -17,6 +17,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <limits.h>
+#include <assert.h>
 #include <sys/socket.h>
 
 #include <sys/mman.h>
@@ -145,6 +146,7 @@ struct site {
     struct dh_if *dh;
     struct hash_if *hash;
 
+    uint32_t index; /* Index of this site */
     int32_t setup_retries; /* How many times to send setup packets */
     int32_t setup_timeout; /* Initial timeout for setup packets */
     int32_t wait_timeout; /* How long to wait if setup unsuccessful */
@@ -152,8 +154,6 @@ struct site {
     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 */
     int32_t setupsiglen; /* Allows us to discard packets quickly if
@@ -217,7 +217,7 @@ static void slog(struct site *st, uint32_t event, cstring_t 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);
@@ -249,10 +249,10 @@ struct msg {
     uint8_t *nR;
     uint8_t *nL;
     int32_t pklen;
-    uint8_t *pk;
+    char *pk;
     int32_t hashlen;
     int32_t siglen;
-    uint8_t *sig;
+    char *sig;
 };
 
 /* Build any of msg1 to msg4. msg5 and msg6 are built from the inside
@@ -268,7 +268,7 @@ static bool_t generate_msg(struct site *st, uint32_t type, cstring_t what)
     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);
@@ -542,7 +542,7 @@ static bool_t generate_msg5(struct site *st)
     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;
@@ -591,7 +591,7 @@ static bool_t generate_msg6(struct site *st)
     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 */
@@ -653,12 +653,10 @@ static bool_t process_msg0(struct site *st, struct buffer_if *msg0,
        /* 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);
        return True;
-       break;
     default:
        slog(st,LOG_SEC,"incoming encrypted message of type %08x "
             "(unknown)",type);
@@ -909,8 +907,9 @@ static bool_t send_msg7(struct site *st, cstring_t reason)
        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;
@@ -926,7 +925,6 @@ static void enter_state_wait(struct site *st)
     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. */
@@ -1005,7 +1003,7 @@ static void site_outgoing(void *sst, struct buffer_if *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);
        }
@@ -1078,7 +1076,7 @@ static bool_t site_incoming(void *sst, struct buffer_if *buf,
        }
        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);
@@ -1186,6 +1184,7 @@ static void site_phase_hook(void *sst, uint32_t newphase)
 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;
@@ -1217,6 +1216,8 @@ static list_t *site_apply(closure_t *self, struct cloc loc, dict_t *context,
        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);
@@ -1250,12 +1251,11 @@ static list_t *site_apply(closure_t *self, struct cloc loc, dict_t *context,
     else
        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");