chiark / gitweb /
b83919c3b4467bc484a5a18fbea78373d6920044
[secnet.git] / site.c
1 /* site.c - manage communication with a remote network site */
2
3 /*
4  * This file is part of secnet.
5  * See README for full list of copyright holders.
6  *
7  * secnet is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version d of the License, or
10  * (at your option) any later version.
11  * 
12  * secnet is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * version 3 along with secnet; if not, see
19  * https://www.gnu.org/licenses/gpl.html.
20  */
21
22 /* The 'site' code doesn't know anything about the structure of the
23    packets it's transmitting.  In fact, under the new netlink
24    configuration scheme it doesn't need to know anything at all about
25    IP addresses, except how to contact its peer.  This means it could
26    potentially be used to tunnel other protocols too (IPv6, IPX, plain
27    old Ethernet frames) if appropriate netlink code can be written
28    (and that ought not to be too hard, eg. using the TUN/TAP device to
29    pretend to be an Ethernet interface).  */
30
31 /* At some point in the future the netlink code will be asked for
32    configuration information to go in the PING/PONG packets at the end
33    of the key exchange. */
34
35 #include "secnet.h"
36 #include <stdio.h>
37 #include <string.h>
38 #include <limits.h>
39 #include <assert.h>
40 #include <sys/socket.h>
41
42 #include <sys/mman.h>
43 #include "util.h"
44 #include "unaligned.h"
45 #include "magic.h"
46
47 #define SETUP_BUFFER_LEN 2048
48
49 #define DEFAULT_KEY_LIFETIME                  (3600*1000) /* [ms] */
50 #define DEFAULT_KEY_RENEGOTIATE_GAP           (5*60*1000) /* [ms] */
51 #define DEFAULT_SETUP_RETRIES 5
52 #define DEFAULT_SETUP_RETRY_INTERVAL             (2*1000) /* [ms] */
53 #define DEFAULT_WAIT_TIME                       (20*1000) /* [ms] */
54
55 #define DEFAULT_MOBILE_KEY_LIFETIME      (2*24*3600*1000) /* [ms] */
56 #define DEFAULT_MOBILE_KEY_RENEGOTIATE_GAP (12*3600*1000) /* [ms] */
57 #define DEFAULT_MOBILE_SETUP_RETRIES 30
58 #define DEFAULT_MOBILE_SETUP_RETRY_INTERVAL      (1*1000) /* [ms] */
59 #define DEFAULT_MOBILE_WAIT_TIME                (10*1000) /* [ms] */
60
61 #define DEFAULT_MOBILE_PEER_EXPIRY            (2*60)      /* [s] */
62
63 /* Each site can be in one of several possible states. */
64
65 /* States:
66    SITE_STOP         - nothing is allowed to happen; tunnel is down;
67                        all session keys have been erased
68      -> SITE_RUN upon external instruction
69    SITE_RUN          - site up, maybe with valid key
70      -> SITE_RESOLVE upon outgoing packet and no valid key
71          we start name resolution for the other end of the tunnel
72      -> SITE_SENTMSG2 upon valid incoming message 1 and suitable time
73          we send an appropriate message 2
74    SITE_RESOLVE      - waiting for name resolution
75      -> SITE_SENTMSG1 upon successful resolution
76          we send an appropriate message 1
77      -> SITE_SENTMSG2 upon valid incoming message 1 (then abort resolution)
78          we abort resolution and 
79      -> SITE_WAIT on timeout or resolution failure
80    SITE_SENTMSG1
81      -> SITE_SENTMSG2 upon valid incoming message 1 from higher priority end
82      -> SITE_SENTMSG3 upon valid incoming message 2
83      -> SITE_WAIT on timeout
84    SITE_SENTMSG2
85      -> SITE_SENTMSG4 upon valid incoming message 3
86      -> SITE_WAIT on timeout
87    SITE_SENTMSG3
88      -> SITE_SENTMSG5 upon valid incoming message 4
89      -> SITE_WAIT on timeout
90    SITE_SENTMSG4
91      -> SITE_RUN upon valid incoming message 5
92      -> SITE_WAIT on timeout
93    SITE_SENTMSG5
94      -> SITE_RUN upon valid incoming message 6
95      -> SITE_WAIT on timeout
96    SITE_WAIT         - failed to establish key; do nothing for a while
97      -> SITE_RUN on timeout
98    */
99
100 #define SITE_STOP     0
101 #define SITE_RUN      1
102 #define SITE_RESOLVE  2
103 #define SITE_SENTMSG1 3
104 #define SITE_SENTMSG2 4
105 #define SITE_SENTMSG3 5
106 #define SITE_SENTMSG4 6
107 #define SITE_SENTMSG5 7
108 #define SITE_WAIT     8
109
110 int32_t site_max_start_pad = 4*4;
111
112 static cstring_t state_name(uint32_t state)
113 {
114     switch (state) {
115     case 0: return "STOP";
116     case 1: return "RUN";
117     case 2: return "RESOLVE";
118     case 3: return "SENTMSG1";
119     case 4: return "SENTMSG2";
120     case 5: return "SENTMSG3";
121     case 6: return "SENTMSG4";
122     case 7: return "SENTMSG5";
123     case 8: return "WAIT";
124     default: return "*bad state*";
125     }
126 }
127
128 #define NONCELEN 8
129
130 #define LOG_UNEXPECTED    0x00000001
131 #define LOG_SETUP_INIT    0x00000002
132 #define LOG_SETUP_TIMEOUT 0x00000004
133 #define LOG_ACTIVATE_KEY  0x00000008
134 #define LOG_TIMEOUT_KEY   0x00000010
135 #define LOG_SEC           0x00000020
136 #define LOG_STATE         0x00000040
137 #define LOG_DROP          0x00000080
138 #define LOG_DUMP          0x00000100
139 #define LOG_ERROR         0x00000400
140 #define LOG_PEER_ADDRS    0x00000800
141
142 static struct flagstr log_event_table[]={
143     { "unexpected", LOG_UNEXPECTED },
144     { "setup-init", LOG_SETUP_INIT },
145     { "setup-timeout", LOG_SETUP_TIMEOUT },
146     { "activate-key", LOG_ACTIVATE_KEY },
147     { "timeout-key", LOG_TIMEOUT_KEY },
148     { "security", LOG_SEC },
149     { "state-change", LOG_STATE },
150     { "packet-drop", LOG_DROP },
151     { "dump-packets", LOG_DUMP },
152     { "errors", LOG_ERROR },
153     { "peer-addrs", LOG_PEER_ADDRS },
154     { "default", LOG_SETUP_INIT|LOG_SETUP_TIMEOUT|
155       LOG_ACTIVATE_KEY|LOG_TIMEOUT_KEY|LOG_SEC|LOG_ERROR },
156     { "all", 0xffffffff },
157     { NULL, 0 }
158 };
159
160
161 /***** TRANSPORT PEERS declarations *****/
162
163 /* Details of "mobile peer" semantics:
164
165    - We use the same data structure for the different configurations,
166      but manage it with different algorithms.
167    
168    - We record up to mobile_peers_max peer address/port numbers
169      ("peers") for key setup, and separately up to mobile_peers_max
170      for data transfer.
171
172    - In general, we make a new set of addrs (see below) when we start
173      a new key exchange; the key setup addrs become the data transport
174      addrs when key setup complets.
175
176    If our peer is mobile:
177
178    - We send to all recent addresses of incoming packets, plus
179      initially all configured addresses (which we also expire).
180
181    - So, we record addrs of good incoming packets, as follows:
182       1. expire any peers last seen >120s ("mobile-peer-expiry") ago
183       2. add the peer of the just received packet to the applicable list
184          (possibly evicting the oldest entries to make room)
185      NB that we do not expire peers until an incoming packet arrives.
186
187    - If the peer has a configured address or name, we record them the
188      same way, but only as a result of our own initiation of key
189      setup.  (We might evict some incoming packet addrs to make room.)
190
191    - The default number of addrs to keep is 3, or 4 if we have a
192      configured name or address.  That's space for two configured
193      addresses (one IPv6 and one IPv4), plus two received addresses.
194
195    - Outgoing packets are sent to every recorded address in the
196      applicable list.  Any unsupported[1] addresses are deleted from
197      the list right away.  (This should only happen to configured
198      addresses, of course, but there is no need to check that.)
199
200    - When we successfully complete a key setup, we merge the key setup
201      peers into the data transfer peers.
202
203    [1] An unsupported address is one for whose AF we don't have a
204      socket (perhaps because we got EAFNOSUPPORT or some such) or for
205      which sendto gives ENETUNREACH.
206
207    If neither end is mobile:
208
209    - When peer initiated the key exchange, we use the incoming packet
210      address.
211
212    - When we initiate the key exchange, we try configured addresses
213      until we get one which isn't unsupported then fixate on that.
214
215    - When we complete a key setup, we replace the data transport peers
216      with those from the key setup.
217
218    If we are mobile:
219
220    - We can't tell when local network setup changes so we can't cache
221      the unsupported addrs and completely remove the spurious calls to
222      sendto, but we can optimise things a bit by deprioritising addrs
223      which seem to be unsupported.
224
225    - Use only configured addresses.  (Except, that if our peer
226      initiated a key exchange we use the incoming packet address until
227      our name resolution completes.)
228
229    - When we send a packet, try each address in turn; if addr
230      supported, put that address to the end of the list for future
231      packets, and go onto the next address.
232
233    - When we complete a key setup, we replace the data transport peers
234      with those from the key setup.
235
236    */
237
238 typedef struct {
239     struct timeval last;
240     struct comm_addr addr;
241 } transport_peer;
242
243 typedef struct {
244 /* configuration information */
245 /* runtime information */
246     int npeers;
247     transport_peer peers[MAX_PEER_ADDRS];
248 } transport_peers;
249
250 /* Basic operations on transport peer address sets */
251 static void transport_peers_clear(struct site *st, transport_peers *peers);
252 static int transport_peers_valid(transport_peers *peers);
253 static void transport_peers_copy(struct site *st, transport_peers *dst,
254                                  const transport_peers *src);
255
256 /* Record address of incoming setup packet; resp. data packet. */
257 static void transport_setup_msgok(struct site *st, const struct comm_addr *a);
258 static void transport_data_msgok(struct site *st, const struct comm_addr *a);
259
260 /* Initialise the setup addresses.  Called before we send the first
261  * packet in a key exchange.  If we are the initiator, as a result of
262  * resolve completing (or being determined not to be relevant) or an
263  * incoming PROD; if we are the responder, as a result of the MSG1. */
264 static bool_t transport_compute_setupinit_peers(struct site *st,
265         const struct comm_addr *configured_addrs /* 0 if none or not found */,
266         int n_configured_addrs /* 0 if none or not found */,
267         const struct comm_addr *incoming_packet_addr /* 0 if none */);
268
269 /* Called if we are the responder in a key setup, when the resolve
270  * completes.  transport_compute_setupinit_peers will hvae been called
271  * earlier.  If _complete is called, we are still doing the key setup
272  * (and we should use the new values for both the rest of the key
273  * setup and the ongoing data exchange); if _tardy is called, the key
274  * setup is done (either completed or not) and only the data peers are
275  * relevant */
276 static void transport_resolve_complete(struct site *st,
277         const struct comm_addr *addrs, int naddrs);
278 static void transport_resolve_complete_tardy(struct site *st,
279         const struct comm_addr *addrs, int naddrs);
280
281 static void transport_xmit(struct site *st, transport_peers *peers,
282                            struct buffer_if *buf, bool_t candebug);
283
284  /***** END of transport peers declarations *****/
285
286
287 struct data_key {
288     struct transform_inst_if *transform;
289     uint64_t key_timeout; /* End of life of current key */
290     uint32_t remote_session_id;
291 };
292
293 struct site {
294     closure_t cl;
295     struct site_if ops;
296 /* configuration information */
297     string_t localname;
298     string_t remotename;
299     bool_t keepalive;
300     bool_t local_mobile, peer_mobile; /* Mobile client support */
301     int32_t transport_peers_max;
302     string_t tunname; /* localname<->remotename by default, used in logs */
303     cstring_t *addresses; /* DNS name or address(es) for bootstrapping, optional */
304     int remoteport; /* Port for bootstrapping, optional */
305     uint32_t mtu_target;
306     struct netlink_if *netlink;
307     struct comm_if **comms;
308     int ncomms;
309     struct resolver_if *resolver;
310     struct log_if *log;
311     struct random_if *random;
312     struct rsaprivkey_if *privkey;
313     struct rsapubkey_if *pubkey;
314     struct transform_if **transforms;
315     int ntransforms;
316     struct dh_if *dh;
317     struct hash_if *hash;
318
319     uint32_t index; /* Index of this site */
320     uint32_t local_capabilities;
321     int32_t setup_retries; /* How many times to send setup packets */
322     int32_t setup_retry_interval; /* Initial timeout for setup packets */
323     int32_t wait_timeout; /* How long to wait if setup unsuccessful */
324     int32_t mobile_peer_expiry; /* How long to remember 2ary addresses */
325     int32_t key_lifetime; /* How long a key lasts once set up */
326     int32_t key_renegotiate_time; /* If we see traffic (or a keepalive)
327                                       after this time, initiate a new
328                                       key exchange */
329
330     bool_t setup_priority; /* Do we have precedence if both sites emit
331                               message 1 simultaneously? */
332     uint32_t log_events;
333
334 /* runtime information */
335     uint32_t state;
336     uint64_t now; /* Most recently seen time */
337     bool_t allow_send_prod;
338     int resolving_count;
339     int resolving_n_results_all;
340     int resolving_n_results_stored;
341     struct comm_addr resolving_results[MAX_PEER_ADDRS];
342
343     /* The currently established session */
344     struct data_key current;
345     struct data_key auxiliary_key;
346     bool_t auxiliary_is_new;
347     uint64_t renegotiate_key_time; /* When we can negotiate a new key */
348     uint64_t auxiliary_renegotiate_key_time;
349     transport_peers peers; /* Current address(es) of peer for data traffic */
350
351     /* The current key setup protocol exchange.  We can only be
352        involved in one of these at a time.  There's a potential for
353        denial of service here (the attacker keeps sending a setup
354        packet; we keep trying to continue the exchange, and have to
355        timeout before we can listen for another setup packet); perhaps
356        we should keep a list of 'bad' sources for setup packets. */
357     uint32_t remote_capabilities;
358     uint16_t remote_adv_mtu;
359     struct transform_if *chosen_transform;
360     uint32_t setup_session_id;
361     transport_peers setup_peers;
362     uint8_t localN[NONCELEN]; /* Nonces for key exchange */
363     uint8_t remoteN[NONCELEN];
364     struct buffer_if buffer; /* Current outgoing key exchange packet */
365     struct buffer_if scratch;
366     int32_t retries; /* Number of retries remaining */
367     uint64_t timeout; /* Timeout for current state */
368     uint8_t *dhsecret;
369     uint8_t *sharedsecret;
370     uint32_t sharedsecretlen, sharedsecretallocd;
371     struct transform_inst_if *new_transform; /* For key setup/verify */
372 };
373
374 static uint32_t event_log_priority(struct site *st, uint32_t event)
375 {
376     if (!(event&st->log_events))
377         return 0;
378     switch(event) {
379     case LOG_UNEXPECTED:    return M_INFO;
380     case LOG_SETUP_INIT:    return M_INFO;
381     case LOG_SETUP_TIMEOUT: return M_NOTICE;
382     case LOG_ACTIVATE_KEY:  return M_INFO;
383     case LOG_TIMEOUT_KEY:   return M_INFO;
384     case LOG_SEC:           return M_SECURITY;
385     case LOG_STATE:         return M_DEBUG;
386     case LOG_DROP:          return M_DEBUG;
387     case LOG_DUMP:          return M_DEBUG;
388     case LOG_ERROR:         return M_ERR;
389     case LOG_PEER_ADDRS:    return M_DEBUG;
390     default:                return M_ERR;
391     }
392 }
393
394 static void vslog(struct site *st, uint32_t event, cstring_t msg, va_list ap)
395 FORMAT(printf,3,0);
396 static void vslog(struct site *st, uint32_t event, cstring_t msg, va_list ap)
397 {
398     uint32_t class;
399
400     class=event_log_priority(st, event);
401     if (class) {
402         slilog_part(st->log,class,"%s: ",st->tunname);
403         vslilog_part(st->log,class,msg,ap);
404         slilog_part(st->log,class,"\n");
405     }
406 }
407
408 static void slog(struct site *st, uint32_t event, cstring_t msg, ...)
409 FORMAT(printf,3,4);
410 static void slog(struct site *st, uint32_t event, cstring_t msg, ...)
411 {
412     va_list ap;
413     va_start(ap,msg);
414     vslog(st,event,msg,ap);
415     va_end(ap);
416 }
417
418 static void logtimeout(struct site *st, const char *fmt, ...)
419 FORMAT(printf,2,3);
420 static void logtimeout(struct site *st, const char *fmt, ...)
421 {
422     uint32_t class=event_log_priority(st,LOG_SETUP_TIMEOUT);
423     if (!class)
424         return;
425
426     va_list ap;
427     va_start(ap,fmt);
428
429     slilog_part(st->log,class,"%s: ",st->tunname);
430     vslilog_part(st->log,class,fmt,ap);
431
432     const char *delim;
433     int i;
434     for (i=0, delim=" (tried ";
435          i<st->setup_peers.npeers;
436          i++, delim=", ") {
437         transport_peer *peer=&st->setup_peers.peers[i];
438         const char *s=comm_addr_to_string(&peer->addr);
439         slilog_part(st->log,class,"%s%s",delim,s);
440     }
441
442     slilog_part(st->log,class,")\n");
443     va_end(ap);
444 }
445
446 static void set_link_quality(struct site *st);
447 static void delete_keys(struct site *st, cstring_t reason, uint32_t loglevel);
448 static void delete_one_key(struct site *st, struct data_key *key,
449                            const char *reason /* may be 0 meaning don't log*/,
450                            const char *which /* ignored if !reasonn */,
451                            uint32_t loglevel /* ignored if !reasonn */);
452 static bool_t initiate_key_setup(struct site *st, cstring_t reason,
453                                  const struct comm_addr *prod_hint);
454 static void enter_state_run(struct site *st);
455 static bool_t enter_state_resolve(struct site *st);
456 static void decrement_resolving_count(struct site *st, int by);
457 static bool_t enter_new_state(struct site *st,uint32_t next);
458 static void enter_state_wait(struct site *st);
459 static void activate_new_key(struct site *st);
460
461 static bool_t is_transform_valid(struct transform_inst_if *transform)
462 {
463     return transform && transform->valid(transform->st);
464 }
465
466 static bool_t current_valid(struct site *st)
467 {
468     return is_transform_valid(st->current.transform);
469 }
470
471 #define DEFINE_CALL_TRANSFORM(fwdrev)                                   \
472 static int call_transform_##fwdrev(struct site *st,                     \
473                                    struct transform_inst_if *transform, \
474                                    struct buffer_if *buf,               \
475                                    const char **errmsg)                 \
476 {                                                                       \
477     if (!is_transform_valid(transform)) {                               \
478         *errmsg="transform not set up";                                 \
479         return 1;                                                       \
480     }                                                                   \
481     return transform->fwdrev(transform->st,buf,errmsg);                 \
482 }
483
484 DEFINE_CALL_TRANSFORM(forwards)
485 DEFINE_CALL_TRANSFORM(reverse)
486
487 static void dispose_transform(struct transform_inst_if **transform_var)
488 {
489     struct transform_inst_if *transform=*transform_var;
490     if (transform) {
491         transform->delkey(transform->st);
492         transform->destroy(transform->st);
493     }
494     *transform_var = 0;
495 }    
496
497 #define CHECK_AVAIL(b,l) do { if ((b)->size<(l)) return False; } while(0)
498 #define CHECK_EMPTY(b) do { if ((b)->size!=0) return False; } while(0)
499 #define CHECK_TYPE(b,t) do { uint32_t type; \
500     CHECK_AVAIL((b),4); \
501     type=buf_unprepend_uint32((b)); \
502     if (type!=(t)) return False; } while(0)
503
504 static _Bool type_is_msg34(uint32_t type)
505 {
506     return
507         type == LABEL_MSG3 ||
508         type == LABEL_MSG3BIS ||
509         type == LABEL_MSG4;
510 }
511
512 struct parsedname {
513     int32_t len;
514     uint8_t *name;
515     struct buffer_if extrainfo;
516 };
517
518 struct msg {
519     uint8_t *hashstart;
520     uint32_t dest;
521     uint32_t source;
522     struct parsedname remote;
523     struct parsedname local;
524     uint32_t remote_capabilities;
525     uint16_t remote_mtu;
526     int capab_transformnum;
527     uint8_t *nR;
528     uint8_t *nL;
529     int32_t pklen;
530     char *pk;
531     int32_t hashlen;
532     int32_t siglen;
533     char *sig;
534 };
535
536 static void set_new_transform(struct site *st, char *pk)
537 {
538     /* Make room for the shared key */
539     st->sharedsecretlen=st->chosen_transform->keylen?:st->dh->ceil_len;
540     assert(st->sharedsecretlen);
541     if (st->sharedsecretlen > st->sharedsecretallocd) {
542         st->sharedsecretallocd=st->sharedsecretlen;
543         st->sharedsecret=safe_realloc_ary(st->sharedsecret,1,
544                                           st->sharedsecretallocd,
545                                           "site:sharedsecret");
546     }
547
548     /* Generate the shared key */
549     st->dh->makeshared(st->dh->st,st->dhsecret,st->dh->len,pk,
550                        st->sharedsecret,st->sharedsecretlen);
551
552     /* Set up the transform */
553     struct transform_if *generator=st->chosen_transform;
554     struct transform_inst_if *generated=generator->create(generator->st);
555     generated->setkey(generated->st,st->sharedsecret,
556                       st->sharedsecretlen,st->setup_priority);
557     dispose_transform(&st->new_transform);
558     st->new_transform=generated;
559
560     slog(st,LOG_SETUP_INIT,"key exchange negotiated transform"
561          " %d (capabilities ours=%#"PRIx32" theirs=%#"PRIx32")",
562          st->chosen_transform->capab_transformnum,
563          st->local_capabilities, st->remote_capabilities);
564 }
565
566 struct xinfoadd {
567     int32_t lenpos, afternul;
568 };
569 static void append_string_xinfo_start(struct buffer_if *buf,
570                                       struct xinfoadd *xia,
571                                       const char *str)
572     /* Helps construct one of the names with additional info as found
573      * in MSG1..4.  Call this function first, then append all the
574      * desired extra info (not including the nul byte) to the buffer,
575      * then call append_string_xinfo_done. */
576 {
577     xia->lenpos = buf->size;
578     buf_append_string(buf,str);
579     buf_append_uint8(buf,0);
580     xia->afternul = buf->size;
581 }
582 static void append_string_xinfo_done(struct buffer_if *buf,
583                                      struct xinfoadd *xia)
584 {
585     /* we just need to adjust the string length */
586     if (buf->size == xia->afternul) {
587         /* no extra info, strip the nul too */
588         buf_unappend_uint8(buf);
589     } else {
590         put_uint16(buf->start+xia->lenpos, buf->size-(xia->lenpos+2));
591     }
592 }
593
594 /* Build any of msg1 to msg4. msg5 and msg6 are built from the inside
595    out using a transform of config data supplied by netlink */
596 static bool_t generate_msg(struct site *st, uint32_t type, cstring_t what)
597 {
598     void *hst;
599     uint8_t *hash;
600     string_t dhpub, sig;
601
602     st->retries=st->setup_retries;
603     BUF_ALLOC(&st->buffer,what);
604     buffer_init(&st->buffer,0);
605     buf_append_uint32(&st->buffer,
606         (type==LABEL_MSG1?0:st->setup_session_id));
607     buf_append_uint32(&st->buffer,st->index);
608     buf_append_uint32(&st->buffer,type);
609
610     struct xinfoadd xia;
611     append_string_xinfo_start(&st->buffer,&xia,st->localname);
612     if ((st->local_capabilities & CAPAB_EARLY) || (type != LABEL_MSG1)) {
613         buf_append_uint32(&st->buffer,st->local_capabilities);
614     }
615     if (type_is_msg34(type)) {
616         buf_append_uint16(&st->buffer,st->mtu_target);
617     }
618     append_string_xinfo_done(&st->buffer,&xia);
619
620     buf_append_string(&st->buffer,st->remotename);
621     BUF_ADD_OBJ(append,&st->buffer,st->localN);
622     if (type==LABEL_MSG1) return True;
623     BUF_ADD_OBJ(append,&st->buffer,st->remoteN);
624     if (type==LABEL_MSG2) return True;
625
626     if (hacky_par_mid_failnow()) return False;
627
628     if (type==LABEL_MSG3BIS)
629         buf_append_uint8(&st->buffer,st->chosen_transform->capab_transformnum);
630
631     dhpub=st->dh->makepublic(st->dh->st,st->dhsecret,st->dh->len);
632     buf_append_string(&st->buffer,dhpub);
633     free(dhpub);
634     hash=safe_malloc(st->hash->len, "generate_msg");
635     hst=st->hash->init();
636     st->hash->update(hst,st->buffer.start,st->buffer.size);
637     st->hash->final(hst,hash);
638     sig=st->privkey->sign(st->privkey->st,hash,st->hash->len);
639     buf_append_string(&st->buffer,sig);
640     free(sig);
641     free(hash);
642     return True;
643 }
644
645 static bool_t unpick_name(struct buffer_if *msg, struct parsedname *nm)
646 {
647     CHECK_AVAIL(msg,2);
648     nm->len=buf_unprepend_uint16(msg);
649     CHECK_AVAIL(msg,nm->len);
650     nm->name=buf_unprepend(msg,nm->len);
651     uint8_t *nul=memchr(nm->name,0,nm->len);
652     if (!nul) {
653         buffer_readonly_view(&nm->extrainfo,0,0);
654     } else {
655         buffer_readonly_view(&nm->extrainfo, nul+1, msg->start-(nul+1));
656         nm->len=nul-nm->name;
657     }
658     return True;
659 }
660
661 static bool_t unpick_msg(struct site *st, uint32_t type,
662                          struct buffer_if *msg, struct msg *m)
663 {
664     m->capab_transformnum=-1;
665     m->hashstart=msg->start;
666     CHECK_AVAIL(msg,4);
667     m->dest=buf_unprepend_uint32(msg);
668     CHECK_AVAIL(msg,4);
669     m->source=buf_unprepend_uint32(msg);
670     CHECK_TYPE(msg,type);
671     if (!unpick_name(msg,&m->remote)) return False;
672     m->remote_capabilities=0;
673     m->remote_mtu=0;
674     if (m->remote.extrainfo.size) {
675         CHECK_AVAIL(&m->remote.extrainfo,4);
676         m->remote_capabilities=buf_unprepend_uint32(&m->remote.extrainfo);
677     }
678     if (type_is_msg34(type) && m->remote.extrainfo.size) {
679         CHECK_AVAIL(&m->remote.extrainfo,2);
680         m->remote_mtu=buf_unprepend_uint16(&m->remote.extrainfo);
681     }
682     if (!unpick_name(msg,&m->local)) return False;
683     if (type==LABEL_PROD) {
684         CHECK_EMPTY(msg);
685         return True;
686     }
687     CHECK_AVAIL(msg,NONCELEN);
688     m->nR=buf_unprepend(msg,NONCELEN);
689     if (type==LABEL_MSG1) {
690         CHECK_EMPTY(msg);
691         return True;
692     }
693     CHECK_AVAIL(msg,NONCELEN);
694     m->nL=buf_unprepend(msg,NONCELEN);
695     if (type==LABEL_MSG2) {
696         CHECK_EMPTY(msg);
697         return True;
698     }
699     if (type==LABEL_MSG3BIS) {
700         CHECK_AVAIL(msg,1);
701         m->capab_transformnum = buf_unprepend_uint8(msg);
702     } else {
703         m->capab_transformnum = CAPAB_TRANSFORMNUM_ANCIENT;
704     }
705     CHECK_AVAIL(msg,2);
706     m->pklen=buf_unprepend_uint16(msg);
707     CHECK_AVAIL(msg,m->pklen);
708     m->pk=buf_unprepend(msg,m->pklen);
709     m->hashlen=msg->start-m->hashstart;
710     CHECK_AVAIL(msg,2);
711     m->siglen=buf_unprepend_uint16(msg);
712     CHECK_AVAIL(msg,m->siglen);
713     m->sig=buf_unprepend(msg,m->siglen);
714     CHECK_EMPTY(msg);
715     return True;
716 }
717
718 static bool_t name_matches(const struct parsedname *nm, const char *expected)
719 {
720     int expected_len=strlen(expected);
721     return
722         nm->len == expected_len &&
723         !memcmp(nm->name, expected, expected_len);
724 }    
725
726 static bool_t check_msg(struct site *st, uint32_t type, struct msg *m,
727                         cstring_t *error)
728 {
729     if (type==LABEL_MSG1) return True;
730
731     /* Check that the site names and our nonce have been sent
732        back correctly, and then store our peer's nonce. */ 
733     if (!name_matches(&m->remote,st->remotename)) {
734         *error="wrong remote site name";
735         return False;
736     }
737     if (!name_matches(&m->local,st->localname)) {
738         *error="wrong local site name";
739         return False;
740     }
741     if (memcmp(m->nL,st->localN,NONCELEN)!=0) {
742         *error="wrong locally-generated nonce";
743         return False;
744     }
745     if (type==LABEL_MSG2) return True;
746     if (!consttime_memeq(m->nR,st->remoteN,NONCELEN)!=0) {
747         *error="wrong remotely-generated nonce";
748         return False;
749     }
750     /* MSG3 has complicated rules about capabilities, which are
751      * handled in process_msg3. */
752     if (type==LABEL_MSG3 || type==LABEL_MSG3BIS) return True;
753     if (m->remote_capabilities!=st->remote_capabilities) {
754         *error="remote capabilities changed";
755         return False;
756     }
757     if (type==LABEL_MSG4) return True;
758     *error="unknown message type";
759     return False;
760 }
761
762 static bool_t generate_msg1(struct site *st)
763 {
764     st->random->generate(st->random->st,NONCELEN,st->localN);
765     return generate_msg(st,LABEL_MSG1,"site:MSG1");
766 }
767
768 static bool_t process_msg1(struct site *st, struct buffer_if *msg1,
769                            const struct comm_addr *src, struct msg *m)
770 {
771     /* We've already determined we're in an appropriate state to
772        process an incoming MSG1, and that the MSG1 has correct values
773        of A and B. */
774
775     st->setup_session_id=m->source;
776     st->remote_capabilities=m->remote_capabilities;
777     memcpy(st->remoteN,m->nR,NONCELEN);
778     return True;
779 }
780
781 static bool_t generate_msg2(struct site *st)
782 {
783     st->random->generate(st->random->st,NONCELEN,st->localN);
784     return generate_msg(st,LABEL_MSG2,"site:MSG2");
785 }
786
787 static bool_t process_msg2(struct site *st, struct buffer_if *msg2,
788                            const struct comm_addr *src)
789 {
790     struct msg m;
791     cstring_t err;
792
793     if (!unpick_msg(st,LABEL_MSG2,msg2,&m)) return False;
794     if (!check_msg(st,LABEL_MSG2,&m,&err)) {
795         slog(st,LOG_SEC,"msg2: %s",err);
796         return False;
797     }
798     st->setup_session_id=m.source;
799     st->remote_capabilities=m.remote_capabilities;
800
801     /* Select the transform to use */
802
803     uint32_t remote_transforms = st->remote_capabilities & CAPAB_TRANSFORM_MASK;
804     if (!remote_transforms)
805         /* old secnets only had this one transform */
806         remote_transforms = 1UL << CAPAB_TRANSFORMNUM_ANCIENT;
807
808     struct transform_if *ti;
809     int i;
810     for (i=0; i<st->ntransforms; i++) {
811         ti=st->transforms[i];
812         if ((1UL << ti->capab_transformnum) & remote_transforms)
813             goto transform_found;
814     }
815     slog(st,LOG_ERROR,"no transforms in common"
816          " (us %#"PRIx32"; them: %#"PRIx32")",
817          st->local_capabilities & CAPAB_TRANSFORM_MASK,
818          remote_transforms);
819     return False;
820  transform_found:
821     st->chosen_transform=ti;
822
823     memcpy(st->remoteN,m.nR,NONCELEN);
824     return True;
825 }
826
827 static bool_t generate_msg3(struct site *st)
828 {
829     /* Now we have our nonce and their nonce. Think of a secret key,
830        and create message number 3. */
831     st->random->generate(st->random->st,st->dh->len,st->dhsecret);
832     return generate_msg(st,
833                         (st->remote_capabilities & CAPAB_TRANSFORM_MASK
834                          ? LABEL_MSG3BIS : LABEL_MSG3),
835                         "site:MSG3");
836 }
837
838 static bool_t process_msg3_msg4(struct site *st, struct msg *m)
839 {
840     uint8_t *hash;
841     void *hst;
842
843     /* Check signature and store g^x mod m */
844     hash=safe_malloc(st->hash->len, "process_msg3_msg4");
845     hst=st->hash->init();
846     st->hash->update(hst,m->hashstart,m->hashlen);
847     st->hash->final(hst,hash);
848     /* Terminate signature with a '0' - cheating, but should be ok */
849     m->sig[m->siglen]=0;
850     if (!st->pubkey->check(st->pubkey->st,hash,st->hash->len,m->sig)) {
851         slog(st,LOG_SEC,"msg3/msg4 signature failed check!");
852         free(hash);
853         return False;
854     }
855     free(hash);
856
857     st->remote_adv_mtu=m->remote_mtu;
858
859     return True;
860 }
861
862 static bool_t process_msg3(struct site *st, struct buffer_if *msg3,
863                            const struct comm_addr *src, uint32_t msgtype)
864 {
865     struct msg m;
866     cstring_t err;
867
868     assert(msgtype==LABEL_MSG3 || msgtype==LABEL_MSG3BIS);
869
870     if (!unpick_msg(st,msgtype,msg3,&m)) return False;
871     if (!check_msg(st,msgtype,&m,&err)) {
872         slog(st,LOG_SEC,"msg3: %s",err);
873         return False;
874     }
875     uint32_t capab_adv_late = m.remote_capabilities
876         & ~st->remote_capabilities & CAPAB_EARLY;
877     if (capab_adv_late) {
878         slog(st,LOG_SEC,"msg3 impermissibly adds early capability flag(s)"
879              " %#"PRIx32" (was %#"PRIx32", now %#"PRIx32")",
880              capab_adv_late, st->remote_capabilities, m.remote_capabilities);
881         return False;
882     }
883     st->remote_capabilities|=m.remote_capabilities;
884
885     struct transform_if *ti;
886     int i;
887     for (i=0; i<st->ntransforms; i++) {
888         ti=st->transforms[i];
889         if (ti->capab_transformnum == m.capab_transformnum)
890             goto transform_found;
891     }
892     slog(st,LOG_SEC,"peer chose unknown-to-us transform %d!",
893          m.capab_transformnum);
894     return False;
895  transform_found:
896     st->chosen_transform=ti;
897
898     if (!process_msg3_msg4(st,&m))
899         return False;
900
901     /* Terminate their DH public key with a '0' */
902     m.pk[m.pklen]=0;
903     /* Invent our DH secret key */
904     st->random->generate(st->random->st,st->dh->len,st->dhsecret);
905
906     /* Generate the shared key and set up the transform */
907     set_new_transform(st,m.pk);
908
909     return True;
910 }
911
912 static bool_t generate_msg4(struct site *st)
913 {
914     /* We have both nonces, their public key and our private key. Generate
915        our public key, sign it and send it to them. */
916     return generate_msg(st,LABEL_MSG4,"site:MSG4");
917 }
918
919 static bool_t process_msg4(struct site *st, struct buffer_if *msg4,
920                            const struct comm_addr *src)
921 {
922     struct msg m;
923     cstring_t err;
924
925     if (!unpick_msg(st,LABEL_MSG4,msg4,&m)) return False;
926     if (!check_msg(st,LABEL_MSG4,&m,&err)) {
927         slog(st,LOG_SEC,"msg4: %s",err);
928         return False;
929     }
930     
931     if (!process_msg3_msg4(st,&m))
932         return False;
933
934     /* Terminate their DH public key with a '0' */
935     m.pk[m.pklen]=0;
936
937     /* Generate the shared key and set up the transform */
938     set_new_transform(st,m.pk);
939
940     return True;
941 }
942
943 struct msg0 {
944     uint32_t dest;
945     uint32_t source;
946     uint32_t type;
947 };
948
949 static bool_t unpick_msg0(struct site *st, struct buffer_if *msg0,
950                           struct msg0 *m)
951 {
952     CHECK_AVAIL(msg0,4);
953     m->dest=buf_unprepend_uint32(msg0);
954     CHECK_AVAIL(msg0,4);
955     m->source=buf_unprepend_uint32(msg0);
956     CHECK_AVAIL(msg0,4);
957     m->type=buf_unprepend_uint32(msg0);
958     return True;
959     /* Leaves transformed part of buffer untouched */
960 }
961
962 static bool_t generate_msg5(struct site *st)
963 {
964     cstring_t transform_err;
965
966     BUF_ALLOC(&st->buffer,"site:MSG5");
967     /* We are going to add four words to the message */
968     buffer_init(&st->buffer,calculate_max_start_pad());
969     /* Give the netlink code an opportunity to put its own stuff in the
970        message (configuration information, etc.) */
971     buf_prepend_uint32(&st->buffer,LABEL_MSG5);
972     if (call_transform_forwards(st,st->new_transform,
973                                 &st->buffer,&transform_err))
974         return False;
975     buf_prepend_uint32(&st->buffer,LABEL_MSG5);
976     buf_prepend_uint32(&st->buffer,st->index);
977     buf_prepend_uint32(&st->buffer,st->setup_session_id);
978
979     st->retries=st->setup_retries;
980     return True;
981 }
982
983 static bool_t process_msg5(struct site *st, struct buffer_if *msg5,
984                            const struct comm_addr *src,
985                            struct transform_inst_if *transform)
986 {
987     struct msg0 m;
988     cstring_t transform_err;
989
990     if (!unpick_msg0(st,msg5,&m)) return False;
991
992     if (call_transform_reverse(st,transform,msg5,&transform_err)) {
993         /* There's a problem */
994         slog(st,LOG_SEC,"process_msg5: transform: %s",transform_err);
995         return False;
996     }
997     /* Buffer should now contain untransformed PING packet data */
998     CHECK_AVAIL(msg5,4);
999     if (buf_unprepend_uint32(msg5)!=LABEL_MSG5) {
1000         slog(st,LOG_SEC,"MSG5/PING packet contained wrong label");
1001         return False;
1002     }
1003     /* Older versions of secnet used to write some config data here
1004      * which we ignore.  So we don't CHECK_EMPTY */
1005     return True;
1006 }
1007
1008 static void create_msg6(struct site *st, struct transform_inst_if *transform,
1009                         uint32_t session_id)
1010 {
1011     cstring_t transform_err;
1012
1013     BUF_ALLOC(&st->buffer,"site:MSG6");
1014     /* We are going to add four words to the message */
1015     buffer_init(&st->buffer,calculate_max_start_pad());
1016     /* Give the netlink code an opportunity to put its own stuff in the
1017        message (configuration information, etc.) */
1018     buf_prepend_uint32(&st->buffer,LABEL_MSG6);
1019     int problem = call_transform_forwards(st,transform,
1020                                           &st->buffer,&transform_err);
1021     assert(!problem);
1022     buf_prepend_uint32(&st->buffer,LABEL_MSG6);
1023     buf_prepend_uint32(&st->buffer,st->index);
1024     buf_prepend_uint32(&st->buffer,session_id);
1025 }
1026
1027 static bool_t generate_msg6(struct site *st)
1028 {
1029     if (!is_transform_valid(st->new_transform))
1030         return False;
1031     create_msg6(st,st->new_transform,st->setup_session_id);
1032     st->retries=1; /* Peer will retransmit MSG5 if this packet gets lost */
1033     return True;
1034 }
1035
1036 static bool_t process_msg6(struct site *st, struct buffer_if *msg6,
1037                            const struct comm_addr *src)
1038 {
1039     struct msg0 m;
1040     cstring_t transform_err;
1041
1042     if (!unpick_msg0(st,msg6,&m)) return False;
1043
1044     if (call_transform_reverse(st,st->new_transform,msg6,&transform_err)) {
1045         /* There's a problem */
1046         slog(st,LOG_SEC,"process_msg6: transform: %s",transform_err);
1047         return False;
1048     }
1049     /* Buffer should now contain untransformed PING packet data */
1050     CHECK_AVAIL(msg6,4);
1051     if (buf_unprepend_uint32(msg6)!=LABEL_MSG6) {
1052         slog(st,LOG_SEC,"MSG6/PONG packet contained invalid data");
1053         return False;
1054     }
1055     /* Older versions of secnet used to write some config data here
1056      * which we ignore.  So we don't CHECK_EMPTY */
1057     return True;
1058 }
1059
1060 static bool_t decrypt_msg0(struct site *st, struct buffer_if *msg0,
1061                            const struct comm_addr *src)
1062 {
1063     cstring_t transform_err, auxkey_err, newkey_err="n/a";
1064     struct msg0 m;
1065     uint32_t problem;
1066
1067     if (!unpick_msg0(st,msg0,&m)) return False;
1068
1069     /* Keep a copy so we can try decrypting it with multiple keys */
1070     buffer_copy(&st->scratch, msg0);
1071
1072     problem = call_transform_reverse(st,st->current.transform,
1073                                      msg0,&transform_err);
1074     if (!problem) {
1075         if (!st->auxiliary_is_new)
1076             delete_one_key(st,&st->auxiliary_key,
1077                            "peer has used new key","auxiliary key",LOG_SEC);
1078         return True;
1079     }
1080     if (problem==2)
1081         goto skew;
1082
1083     buffer_copy(msg0, &st->scratch);
1084     problem = call_transform_reverse(st,st->auxiliary_key.transform,
1085                                      msg0,&auxkey_err);
1086     if (problem==0) {
1087         slog(st,LOG_DROP,"processing packet which uses auxiliary key");
1088         if (st->auxiliary_is_new) {
1089             /* We previously timed out in state SENTMSG5 but it turns
1090              * out that our peer did in fact get our MSG5 and is
1091              * using the new key.  So we should switch to it too. */
1092             /* This is a bit like activate_new_key. */
1093             struct data_key t;
1094             t=st->current;
1095             st->current=st->auxiliary_key;
1096             st->auxiliary_key=t;
1097
1098             delete_one_key(st,&st->auxiliary_key,"peer has used new key",
1099                            "previous key",LOG_SEC);
1100             st->auxiliary_is_new=0;
1101             st->renegotiate_key_time=st->auxiliary_renegotiate_key_time;
1102         }
1103         return True;
1104     }
1105     if (problem==2)
1106         goto skew;
1107
1108     if (st->state==SITE_SENTMSG5) {
1109         buffer_copy(msg0, &st->scratch);
1110         problem = call_transform_reverse(st,st->new_transform,
1111                                          msg0,&newkey_err);
1112         if (!problem) {
1113             /* It looks like we didn't get the peer's MSG6 */
1114             /* This is like a cut-down enter_new_state(SITE_RUN) */
1115             slog(st,LOG_STATE,"will enter state RUN (MSG0 with new key)");
1116             BUF_FREE(&st->buffer);
1117             st->timeout=0;
1118             activate_new_key(st);
1119             return True; /* do process the data in this packet */
1120         }
1121         if (problem==2)
1122             goto skew;
1123     }
1124
1125     slog(st,LOG_SEC,"transform: %s (aux: %s, new: %s)",
1126          transform_err,auxkey_err,newkey_err);
1127     initiate_key_setup(st,"incoming message would not decrypt",0);
1128     send_nak(src,m.dest,m.source,m.type,msg0,"message would not decrypt");
1129     return False;
1130
1131  skew:
1132     slog(st,LOG_DROP,"transform: %s (merely skew)",transform_err);
1133     return False;
1134 }
1135
1136 static bool_t process_msg0(struct site *st, struct buffer_if *msg0,
1137                            const struct comm_addr *src)
1138 {
1139     uint32_t type;
1140
1141     if (!decrypt_msg0(st,msg0,src))
1142         return False;
1143
1144     CHECK_AVAIL(msg0,4);
1145     type=buf_unprepend_uint32(msg0);
1146     switch(type) {
1147     case LABEL_MSG7:
1148         /* We must forget about the current session. */
1149         delete_keys(st,"request from peer",LOG_SEC);
1150         /* probably, the peer is shutting down, and this is going to fail,
1151          * but we need to be trying to bring the link up again */
1152         if (st->keepalive)
1153             initiate_key_setup(st,"peer requested key teardown",0);
1154         return True;
1155     case LABEL_MSG9:
1156         /* Deliver to netlink layer */
1157         st->netlink->deliver(st->netlink->st,msg0);
1158         transport_data_msgok(st,src);
1159         /* See whether we should start negotiating a new key */
1160         if (st->now > st->renegotiate_key_time)
1161             initiate_key_setup(st,"incoming packet in renegotiation window",0);
1162         return True;
1163     default:
1164         slog(st,LOG_SEC,"incoming encrypted message of type %08x "
1165              "(unknown)",type);
1166         break;
1167     }
1168     return False;
1169 }
1170
1171 static void dump_packet(struct site *st, struct buffer_if *buf,
1172                         const struct comm_addr *addr, bool_t incoming,
1173                         bool_t ok)
1174 {
1175     uint32_t dest=get_uint32(buf->start);
1176     uint32_t source=get_uint32(buf->start+4);
1177     uint32_t msgtype=get_uint32(buf->start+8);
1178
1179     if (st->log_events & LOG_DUMP)
1180         slilog(st->log,M_DEBUG,"%s: %s: %08x<-%08x: %08x: %s%s",
1181                st->tunname,incoming?"incoming":"outgoing",
1182                dest,source,msgtype,comm_addr_to_string(addr),
1183                ok?"":" - fail");
1184 }
1185
1186 static uint32_t site_status(void *st)
1187 {
1188     return 0;
1189 }
1190
1191 static bool_t send_msg(struct site *st)
1192 {
1193     if (st->retries>0) {
1194         transport_xmit(st, &st->setup_peers, &st->buffer, True);
1195         st->timeout=st->now+st->setup_retry_interval;
1196         st->retries--;
1197         return True;
1198     } else if (st->state==SITE_SENTMSG5) {
1199         logtimeout(st,"timed out sending MSG5, stashing new key");
1200         /* We stash the key we have produced, in case it turns out that
1201          * our peer did see our MSG5 after all and starts using it. */
1202         /* This is a bit like some of activate_new_key */
1203         struct transform_inst_if *t;
1204         t=st->auxiliary_key.transform;
1205         st->auxiliary_key.transform=st->new_transform;
1206         st->new_transform=t;
1207         dispose_transform(&st->new_transform);
1208
1209         st->auxiliary_is_new=1;
1210         st->auxiliary_key.key_timeout=st->now+st->key_lifetime;
1211         st->auxiliary_renegotiate_key_time=st->now+st->key_renegotiate_time;
1212         st->auxiliary_key.remote_session_id=st->setup_session_id;
1213
1214         enter_state_wait(st);
1215         return False;
1216     } else {
1217         logtimeout(st,"timed out sending key setup packet "
1218             "(in state %s)",state_name(st->state));
1219         enter_state_wait(st);
1220         return False;
1221     }
1222 }
1223
1224 static void site_resolve_callback(void *sst, const struct comm_addr *addrs,
1225                                   int stored_naddrs, int all_naddrs,
1226                                   const char *address, const char *failwhy)
1227 {
1228     struct site *st=sst;
1229
1230     if (!stored_naddrs) {
1231         slog(st,LOG_ERROR,"resolution of %s failed: %s",address,failwhy);
1232     } else {
1233         slog(st,LOG_PEER_ADDRS,"resolution of %s completed, %d addrs, eg: %s",
1234              address, all_naddrs, comm_addr_to_string(&addrs[0]));;
1235
1236         int space=st->transport_peers_max-st->resolving_n_results_stored;
1237         int n_tocopy=MIN(stored_naddrs,space);
1238         COPY_ARRAY(st->resolving_results + st->resolving_n_results_stored,
1239                    addrs,
1240                    n_tocopy);
1241         st->resolving_n_results_stored += n_tocopy;
1242         st->resolving_n_results_all += all_naddrs;
1243     }
1244
1245     decrement_resolving_count(st,1);
1246 }
1247
1248 static void decrement_resolving_count(struct site *st, int by)
1249 {
1250     assert(st->resolving_count>0);
1251     st->resolving_count-=by;
1252
1253     if (st->resolving_count)
1254         return;
1255
1256     /* OK, we are done with them all.  Handle combined results. */
1257
1258     const struct comm_addr *addrs=st->resolving_results;
1259     int naddrs=st->resolving_n_results_stored;
1260     assert(naddrs<=st->transport_peers_max);
1261
1262     if (naddrs) {
1263         if (naddrs != st->resolving_n_results_all) {
1264             slog(st,LOG_SETUP_INIT,"resolution of supplied addresses/names"
1265                  " yielded too many results (%d > %d), some ignored",
1266                  st->resolving_n_results_all, naddrs);
1267         }
1268         slog(st,LOG_STATE,"resolution completed, %d addrs, eg: %s",
1269              naddrs, iaddr_to_string(&addrs[0].ia));;
1270     }
1271
1272     switch (st->state) {
1273     case SITE_RESOLVE:
1274         if (transport_compute_setupinit_peers(st,addrs,naddrs,0)) {
1275             enter_new_state(st,SITE_SENTMSG1);
1276         } else {
1277             /* Can't figure out who to try to to talk to */
1278             slog(st,LOG_SETUP_INIT,
1279                  "key exchange failed: cannot find peer address");
1280             enter_state_run(st);
1281         }
1282         break;
1283     case SITE_SENTMSG1: case SITE_SENTMSG2:
1284     case SITE_SENTMSG3: case SITE_SENTMSG4:
1285     case SITE_SENTMSG5:
1286         if (naddrs) {
1287             /* We start using the address immediately for data too.
1288              * It's best to store it in st->peers now because we might
1289              * go via SENTMSG5, WAIT, and a MSG0, straight into using
1290              * the new key (without updating the data peer addrs). */
1291             transport_resolve_complete(st,addrs,naddrs);
1292         } else if (st->local_mobile) {
1293             /* We can't let this rest because we may have a peer
1294              * address which will break in the future. */
1295             slog(st,LOG_SETUP_INIT,"resolution failed: "
1296                  "abandoning key exchange");
1297             enter_state_wait(st);
1298         } else {
1299             slog(st,LOG_SETUP_INIT,"resolution failed: "
1300                  " continuing to use source address of peer's packets"
1301                  " for key exchange and ultimately data");
1302         }
1303         break;
1304     case SITE_RUN:
1305         if (naddrs) {
1306             slog(st,LOG_SETUP_INIT,"resolution completed tardily,"
1307                  " updating peer address(es)");
1308             transport_resolve_complete_tardy(st,addrs,naddrs);
1309         } else if (st->local_mobile) {
1310             /* Not very good.  We should queue (another) renegotiation
1311              * so that we can update the peer address. */
1312             st->key_renegotiate_time=st->now+st->wait_timeout;
1313         } else {
1314             slog(st,LOG_SETUP_INIT,"resolution failed: "
1315                  " continuing to use source address of peer's packets");
1316         }
1317         break;
1318     case SITE_WAIT:
1319     case SITE_STOP:
1320         /* oh well */
1321         break;
1322     }
1323 }
1324
1325 static bool_t initiate_key_setup(struct site *st, cstring_t reason,
1326                                  const struct comm_addr *prod_hint)
1327 {
1328     /* Reentrancy hazard: can call enter_new_state/enter_state_* */
1329     if (st->state!=SITE_RUN) return False;
1330     slog(st,LOG_SETUP_INIT,"initiating key exchange (%s)",reason);
1331     if (st->addresses) {
1332         slog(st,LOG_SETUP_INIT,"resolving peer address(es)");
1333         return enter_state_resolve(st);
1334     } else if (transport_compute_setupinit_peers(st,0,0,prod_hint)) {
1335         return enter_new_state(st,SITE_SENTMSG1);
1336     }
1337     slog(st,LOG_SETUP_INIT,"key exchange failed: no address for peer");
1338     return False;
1339 }
1340
1341 static void activate_new_key(struct site *st)
1342 {
1343     struct transform_inst_if *t;
1344
1345     /* We have three transform instances, which we swap between old,
1346        active and setup */
1347     t=st->auxiliary_key.transform;
1348     st->auxiliary_key.transform=st->current.transform;
1349     st->current.transform=st->new_transform;
1350     st->new_transform=t;
1351     dispose_transform(&st->new_transform);
1352
1353     st->timeout=0;
1354     st->auxiliary_is_new=0;
1355     st->auxiliary_key.key_timeout=st->current.key_timeout;
1356     st->current.key_timeout=st->now+st->key_lifetime;
1357     st->renegotiate_key_time=st->now+st->key_renegotiate_time;
1358     transport_peers_copy(st,&st->peers,&st->setup_peers);
1359     st->current.remote_session_id=st->setup_session_id;
1360
1361     /* Compute the inter-site MTU.  This is min( our_mtu, their_mtu ).
1362      * But their mtu be unspecified, in which case we just use ours. */
1363     uint32_t intersite_mtu=
1364         MIN(st->mtu_target, st->remote_adv_mtu ?: ~(uint32_t)0);
1365     st->netlink->set_mtu(st->netlink->st,intersite_mtu);
1366
1367     slog(st,LOG_ACTIVATE_KEY,"new key activated"
1368          " (mtu ours=%"PRId32" theirs=%"PRId32" intersite=%"PRId32")",
1369          st->mtu_target, st->remote_adv_mtu, intersite_mtu);
1370     enter_state_run(st);
1371 }
1372
1373 static void delete_one_key(struct site *st, struct data_key *key,
1374                            cstring_t reason, cstring_t which, uint32_t loglevel)
1375 {
1376     if (!is_transform_valid(key->transform)) return;
1377     if (reason) slog(st,loglevel,"%s deleted (%s)",which,reason);
1378     dispose_transform(&key->transform);
1379     key->key_timeout=0;
1380 }
1381
1382 static void delete_keys(struct site *st, cstring_t reason, uint32_t loglevel)
1383 {
1384     if (current_valid(st)) {
1385         slog(st,loglevel,"session closed (%s)",reason);
1386
1387         delete_one_key(st,&st->current,0,0,0);
1388         set_link_quality(st);
1389     }
1390     delete_one_key(st,&st->auxiliary_key,0,0,0);
1391 }
1392
1393 static void state_assert(struct site *st, bool_t ok)
1394 {
1395     if (!ok) fatal("site:state_assert");
1396 }
1397
1398 static void enter_state_stop(struct site *st)
1399 {
1400     st->state=SITE_STOP;
1401     st->timeout=0;
1402     delete_keys(st,"entering state STOP",LOG_TIMEOUT_KEY);
1403     dispose_transform(&st->new_transform);
1404 }
1405
1406 static void set_link_quality(struct site *st)
1407 {
1408     uint32_t quality;
1409     if (current_valid(st))
1410         quality=LINK_QUALITY_UP;
1411     else if (st->state==SITE_WAIT || st->state==SITE_STOP)
1412         quality=LINK_QUALITY_DOWN;
1413     else if (st->addresses)
1414         quality=LINK_QUALITY_DOWN_CURRENT_ADDRESS;
1415     else if (transport_peers_valid(&st->peers))
1416         quality=LINK_QUALITY_DOWN_STALE_ADDRESS;
1417     else
1418         quality=LINK_QUALITY_DOWN;
1419
1420     st->netlink->set_quality(st->netlink->st,quality);
1421 }
1422
1423 static void enter_state_run(struct site *st)
1424 {
1425     slog(st,LOG_STATE,"entering state RUN");
1426     st->state=SITE_RUN;
1427     st->timeout=0;
1428
1429     st->setup_session_id=0;
1430     transport_peers_clear(st,&st->setup_peers);
1431     FILLZERO(st->localN);
1432     FILLZERO(st->remoteN);
1433     dispose_transform(&st->new_transform);
1434     memset(st->dhsecret,0,st->dh->len);
1435     memset(st->sharedsecret,0,st->sharedsecretlen);
1436     set_link_quality(st);
1437
1438     if (st->keepalive && !current_valid(st))
1439         initiate_key_setup(st, "keepalive", 0);
1440 }
1441
1442 static bool_t ensure_resolving(struct site *st)
1443 {
1444     /* Reentrancy hazard: may call site_resolve_callback and hence
1445      * enter_new_state, enter_state_* and generate_msg*. */
1446     if (st->resolving_count)
1447         return True;
1448
1449     assert(st->addresses);
1450
1451     /* resolver->request might reentrantly call site_resolve_callback
1452      * which will decrement st->resolving, so we need to increment it
1453      * twice beforehand to prevent decrement from thinking we're
1454      * finished, and decrement it ourselves.  Alternatively if
1455      * everything fails then there are no callbacks due and we simply
1456      * set it to 0 and return false.. */
1457     st->resolving_n_results_stored=0;
1458     st->resolving_n_results_all=0;
1459     st->resolving_count+=2;
1460     const char **addrp=st->addresses;
1461     const char *address;
1462     bool_t anyok=False;
1463     for (; (address=*addrp++); ) {
1464         bool_t ok = st->resolver->request(st->resolver->st,address,
1465                                           st->remoteport,st->comms[0],
1466                                           site_resolve_callback,st);
1467         if (ok)
1468             st->resolving_count++;
1469         anyok|=ok;
1470     }
1471     if (!anyok) {
1472         st->resolving_count=0;
1473         return False;
1474     }
1475     decrement_resolving_count(st,2);
1476     return True;
1477 }
1478
1479 static bool_t enter_state_resolve(struct site *st)
1480 {
1481     /* Reentrancy hazard!  See ensure_resolving. */
1482     state_assert(st,st->state==SITE_RUN);
1483     slog(st,LOG_STATE,"entering state RESOLVE");
1484     st->state=SITE_RESOLVE;
1485     return ensure_resolving(st);
1486 }
1487
1488 static bool_t enter_new_state(struct site *st, uint32_t next)
1489 {
1490     bool_t (*gen)(struct site *st);
1491     int r;
1492
1493     slog(st,LOG_STATE,"entering state %s",state_name(next));
1494     switch(next) {
1495     case SITE_SENTMSG1:
1496         state_assert(st,st->state==SITE_RUN || st->state==SITE_RESOLVE);
1497         gen=generate_msg1;
1498         break;
1499     case SITE_SENTMSG2:
1500         state_assert(st,st->state==SITE_RUN || st->state==SITE_RESOLVE ||
1501                      st->state==SITE_SENTMSG1 || st->state==SITE_WAIT);
1502         gen=generate_msg2;
1503         break;
1504     case SITE_SENTMSG3:
1505         state_assert(st,st->state==SITE_SENTMSG1);
1506         BUF_FREE(&st->buffer);
1507         gen=generate_msg3;
1508         break;
1509     case SITE_SENTMSG4:
1510         state_assert(st,st->state==SITE_SENTMSG2);
1511         BUF_FREE(&st->buffer);
1512         gen=generate_msg4;
1513         break;
1514     case SITE_SENTMSG5:
1515         state_assert(st,st->state==SITE_SENTMSG3);
1516         BUF_FREE(&st->buffer);
1517         gen=generate_msg5;
1518         break;
1519     case SITE_RUN:
1520         state_assert(st,st->state==SITE_SENTMSG4);
1521         BUF_FREE(&st->buffer);
1522         gen=generate_msg6;
1523         break;
1524     default:
1525         gen=NULL;
1526         fatal("enter_new_state(%s): invalid new state",state_name(next));
1527         break;
1528     }
1529
1530     if (hacky_par_start_failnow()) return False;
1531
1532     r= gen(st) && send_msg(st);
1533
1534     hacky_par_end(&r,
1535                   st->setup_retries, st->setup_retry_interval,
1536                   send_msg, st);
1537     
1538     if (r) {
1539         st->state=next;
1540         if (next==SITE_RUN) {
1541             BUF_FREE(&st->buffer); /* Never reused */
1542             st->timeout=0; /* Never retransmit */
1543             activate_new_key(st);
1544         }
1545         return True;
1546     }
1547     slog(st,LOG_ERROR,"error entering state %s",state_name(next));
1548     st->buffer.free=False; /* Unconditionally use the buffer; it may be
1549                               in either state, and enter_state_wait() will
1550                               do a BUF_FREE() */
1551     enter_state_wait(st);
1552     return False;
1553 }
1554
1555 /* msg7 tells our peer that we're about to forget our key */
1556 static bool_t send_msg7(struct site *st, cstring_t reason)
1557 {
1558     cstring_t transform_err;
1559
1560     if (current_valid(st) && st->buffer.free
1561         && transport_peers_valid(&st->peers)) {
1562         BUF_ALLOC(&st->buffer,"site:MSG7");
1563         buffer_init(&st->buffer,calculate_max_start_pad());
1564         buf_append_uint32(&st->buffer,LABEL_MSG7);
1565         buf_append_string(&st->buffer,reason);
1566         if (call_transform_forwards(st, st->current.transform,
1567                                     &st->buffer, &transform_err))
1568             goto free_out;
1569         buf_prepend_uint32(&st->buffer,LABEL_MSG0);
1570         buf_prepend_uint32(&st->buffer,st->index);
1571         buf_prepend_uint32(&st->buffer,st->current.remote_session_id);
1572         transport_xmit(st,&st->peers,&st->buffer,True);
1573         BUF_FREE(&st->buffer);
1574     free_out:
1575         return True;
1576     }
1577     return False;
1578 }
1579
1580 /* We go into this state if our peer becomes uncommunicative. Similar to
1581    the "stop" state, we forget all session keys for a while, before
1582    re-entering the "run" state. */
1583 static void enter_state_wait(struct site *st)
1584 {
1585     slog(st,LOG_STATE,"entering state WAIT");
1586     st->timeout=st->now+st->wait_timeout;
1587     st->state=SITE_WAIT;
1588     set_link_quality(st);
1589     BUF_FREE(&st->buffer); /* will have had an outgoing packet in it */
1590     /* XXX Erase keys etc. */
1591 }
1592
1593 static void generate_prod(struct site *st, struct buffer_if *buf)
1594 {
1595     buffer_init(buf,0);
1596     buf_append_uint32(buf,0);
1597     buf_append_uint32(buf,0);
1598     buf_append_uint32(buf,LABEL_PROD);
1599     buf_append_string(buf,st->localname);
1600     buf_append_string(buf,st->remotename);
1601 }
1602
1603 static void generate_send_prod(struct site *st,
1604                                const struct comm_addr *source)
1605 {
1606     if (!st->allow_send_prod) return; /* too soon */
1607     if (!(st->state==SITE_RUN || st->state==SITE_RESOLVE ||
1608           st->state==SITE_WAIT)) return; /* we'd ignore peer's MSG1 */
1609
1610     slog(st,LOG_SETUP_INIT,"prodding peer for key exchange");
1611     st->allow_send_prod=0;
1612     generate_prod(st,&st->scratch);
1613     bool_t ok = source->comm->sendmsg(source->comm->st, &st->scratch, source);
1614     dump_packet(st,&st->scratch,source,False,ok);
1615 }
1616
1617 static inline void site_settimeout(uint64_t timeout, int *timeout_io)
1618 {
1619     if (timeout) {
1620         int64_t offset=timeout-*now;
1621         if (offset<0) offset=0;
1622         if (offset>INT_MAX) offset=INT_MAX;
1623         if (*timeout_io<0 || offset<*timeout_io)
1624             *timeout_io=offset;
1625     }
1626 }
1627
1628 static int site_beforepoll(void *sst, struct pollfd *fds, int *nfds_io,
1629                            int *timeout_io)
1630 {
1631     struct site *st=sst;
1632
1633     BEFOREPOLL_WANT_FDS(0); /* We don't use any file descriptors */
1634     st->now=*now;
1635
1636     /* Work out when our next timeout is. The earlier of 'timeout' or
1637        'current.key_timeout'. A stored value of '0' indicates no timeout
1638        active. */
1639     site_settimeout(st->timeout, timeout_io);
1640     site_settimeout(st->current.key_timeout, timeout_io);
1641     site_settimeout(st->auxiliary_key.key_timeout, timeout_io);
1642
1643     return 0; /* success */
1644 }
1645
1646 static void check_expiry(struct site *st, struct data_key *key,
1647                          const char *which)
1648 {
1649     if (key->key_timeout && *now>key->key_timeout) {
1650         delete_one_key(st,key,"maximum life exceeded",which,LOG_TIMEOUT_KEY);
1651     }
1652 }
1653
1654 /* NB site_afterpoll will be called before site_beforepoll is ever called */
1655 static void site_afterpoll(void *sst, struct pollfd *fds, int nfds)
1656 {
1657     struct site *st=sst;
1658
1659     st->now=*now;
1660     if (st->timeout && *now>st->timeout) {
1661         st->timeout=0;
1662         if (st->state>=SITE_SENTMSG1 && st->state<=SITE_SENTMSG5) {
1663             if (!hacky_par_start_failnow())
1664                 send_msg(st);
1665         } else if (st->state==SITE_WAIT) {
1666             enter_state_run(st);
1667         } else {
1668             slog(st,LOG_ERROR,"site_afterpoll: unexpected timeout, state=%d",
1669                  st->state);
1670         }
1671     }
1672     check_expiry(st,&st->current,"current key");
1673     check_expiry(st,&st->auxiliary_key,"auxiliary key");
1674 }
1675
1676 /* This function is called by the netlink device to deliver packets
1677    intended for the remote network. The packet is in "raw" wire
1678    format, but is guaranteed to be word-aligned. */
1679 static void site_outgoing(void *sst, struct buffer_if *buf)
1680 {
1681     struct site *st=sst;
1682     cstring_t transform_err;
1683     
1684     if (st->state==SITE_STOP) {
1685         BUF_FREE(buf);
1686         return;
1687     }
1688
1689     st->allow_send_prod=1;
1690
1691     /* In all other states we consider delivering the packet if we have
1692        a valid key and a valid address to send it to. */
1693     if (current_valid(st) && transport_peers_valid(&st->peers)) {
1694         /* Transform it and send it */
1695         if (buf->size>0) {
1696             buf_prepend_uint32(buf,LABEL_MSG9);
1697             if (call_transform_forwards(st, st->current.transform,
1698                                         buf, &transform_err))
1699                 goto free_out;
1700             buf_prepend_uint32(buf,LABEL_MSG0);
1701             buf_prepend_uint32(buf,st->index);
1702             buf_prepend_uint32(buf,st->current.remote_session_id);
1703             transport_xmit(st,&st->peers,buf,False);
1704         }
1705     free_out:
1706         BUF_FREE(buf);
1707         return;
1708     }
1709
1710     slog(st,LOG_DROP,"discarding outgoing packet of size %d",buf->size);
1711     BUF_FREE(buf);
1712     initiate_key_setup(st,"outgoing packet",0);
1713 }
1714
1715 static bool_t named_for_us(struct site *st, const struct buffer_if *buf_in,
1716                            uint32_t type, struct msg *m)
1717     /* For packets which are identified by the local and remote names.
1718      * If it has our name and our peer's name in it it's for us. */
1719 {
1720     struct buffer_if buf[1];
1721     buffer_readonly_clone(buf,buf_in);
1722     return unpick_msg(st,type,buf,m)
1723         && name_matches(&m->remote,st->remotename)
1724         && name_matches(&m->local,st->localname);
1725 }
1726
1727 /* This function is called by the communication device to deliver
1728    packets from our peers.
1729    It should return True if the packet is recognised as being for
1730    this current site instance (and should therefore not be processed
1731    by other sites), even if the packet was otherwise ignored. */
1732 static bool_t site_incoming(void *sst, struct buffer_if *buf,
1733                             const struct comm_addr *source)
1734 {
1735     struct site *st=sst;
1736
1737     if (buf->size < 12) return False;
1738
1739     uint32_t dest=get_uint32(buf->start);
1740     uint32_t msgtype=get_uint32(buf->start+8);
1741     struct msg named_msg;
1742
1743     if (msgtype==LABEL_MSG1) {
1744         if (!named_for_us(st,buf,msgtype,&named_msg))
1745             return False;
1746         /* It's a MSG1 addressed to us. Decide what to do about it. */
1747         dump_packet(st,buf,source,True,True);
1748         if (st->state==SITE_RUN || st->state==SITE_RESOLVE ||
1749             st->state==SITE_WAIT) {
1750             /* We should definitely process it */
1751             transport_compute_setupinit_peers(st,0,0,source);
1752             if (process_msg1(st,buf,source,&named_msg)) {
1753                 slog(st,LOG_SETUP_INIT,"key setup initiated by peer");
1754                 bool_t entered=enter_new_state(st,SITE_SENTMSG2);
1755                 if (entered && st->addresses && st->local_mobile)
1756                     /* We must do this as the very last thing, because
1757                        the resolver callback might reenter us. */
1758                     ensure_resolving(st);
1759             } else {
1760                 slog(st,LOG_ERROR,"failed to process incoming msg1");
1761             }
1762             BUF_FREE(buf);
1763             return True;
1764         } else if (st->state==SITE_SENTMSG1) {
1765             /* We've just sent a message 1! They may have crossed on
1766                the wire. If we have priority then we ignore the
1767                incoming one, otherwise we process it as usual. */
1768             if (st->setup_priority) {
1769                 BUF_FREE(buf);
1770                 slog(st,LOG_DUMP,"crossed msg1s; we are higher "
1771                      "priority => ignore incoming msg1");
1772                 return True;
1773             } else {
1774                 slog(st,LOG_DUMP,"crossed msg1s; we are lower "
1775                      "priority => use incoming msg1");
1776                 if (process_msg1(st,buf,source,&named_msg)) {
1777                     BUF_FREE(&st->buffer); /* Free our old message 1 */
1778                     transport_setup_msgok(st,source);
1779                     enter_new_state(st,SITE_SENTMSG2);
1780                 } else {
1781                     slog(st,LOG_ERROR,"failed to process an incoming "
1782                          "crossed msg1 (we have low priority)");
1783                 }
1784                 BUF_FREE(buf);
1785                 return True;
1786             }
1787         }
1788         /* The message 1 was received at an unexpected stage of the
1789            key setup. XXX POLICY - what do we do? */
1790         slog(st,LOG_UNEXPECTED,"unexpected incoming message 1");
1791         BUF_FREE(buf);
1792         return True;
1793     }
1794     if (msgtype==LABEL_PROD) {
1795         if (!named_for_us(st,buf,msgtype,&named_msg))
1796             return False;
1797         dump_packet(st,buf,source,True,True);
1798         if (st->state!=SITE_RUN) {
1799             slog(st,LOG_DROP,"ignoring PROD when not in state RUN");
1800         } else if (current_valid(st)) {
1801             slog(st,LOG_DROP,"ignoring PROD when we think we have a key");
1802         } else {
1803             initiate_key_setup(st,"peer sent PROD packet",source);
1804         }
1805         BUF_FREE(buf);
1806         return True;
1807     }
1808     if (dest==st->index) {
1809         /* Explicitly addressed to us */
1810         if (msgtype!=LABEL_MSG0) dump_packet(st,buf,source,True,True);
1811         switch (msgtype) {
1812         case LABEL_NAK:
1813             /* If the source is our current peer then initiate a key setup,
1814                because our peer's forgotten the key */
1815             if (get_uint32(buf->start+4)==st->current.remote_session_id) {
1816                 bool_t initiated;
1817                 initiated = initiate_key_setup(st,"received a NAK",source);
1818                 if (!initiated) generate_send_prod(st,source);
1819             } else {
1820                 slog(st,LOG_SEC,"bad incoming NAK");
1821             }
1822             break;
1823         case LABEL_MSG0:
1824             process_msg0(st,buf,source);
1825             break;
1826         case LABEL_MSG1:
1827             /* Setup packet: should not have been explicitly addressed
1828                to us */
1829             slog(st,LOG_SEC,"incoming explicitly addressed msg1");
1830             break;
1831         case LABEL_MSG2:
1832             /* Setup packet: expected only in state SENTMSG1 */
1833             if (st->state!=SITE_SENTMSG1) {
1834                 slog(st,LOG_UNEXPECTED,"unexpected MSG2");
1835             } else if (process_msg2(st,buf,source)) {
1836                 transport_setup_msgok(st,source);
1837                 enter_new_state(st,SITE_SENTMSG3);
1838             } else {
1839                 slog(st,LOG_SEC,"invalid MSG2");
1840             }
1841             break;
1842         case LABEL_MSG3:
1843         case LABEL_MSG3BIS:
1844             /* Setup packet: expected only in state SENTMSG2 */
1845             if (st->state!=SITE_SENTMSG2) {
1846                 slog(st,LOG_UNEXPECTED,"unexpected MSG3");
1847             } else if (process_msg3(st,buf,source,msgtype)) {
1848                 transport_setup_msgok(st,source);
1849                 enter_new_state(st,SITE_SENTMSG4);
1850             } else {
1851                 slog(st,LOG_SEC,"invalid MSG3");
1852             }
1853             break;
1854         case LABEL_MSG4:
1855             /* Setup packet: expected only in state SENTMSG3 */
1856             if (st->state!=SITE_SENTMSG3) {
1857                 slog(st,LOG_UNEXPECTED,"unexpected MSG4");
1858             } else if (process_msg4(st,buf,source)) {
1859                 transport_setup_msgok(st,source);
1860                 enter_new_state(st,SITE_SENTMSG5);
1861             } else {
1862                 slog(st,LOG_SEC,"invalid MSG4");
1863             }
1864             break;
1865         case LABEL_MSG5:
1866             /* Setup packet: expected only in state SENTMSG4 */
1867             /* (may turn up in state RUN if our return MSG6 was lost
1868                and the new key has already been activated. In that
1869                case we discard it. The peer will realise that we
1870                are using the new key when they see our data packets.
1871                Until then the peer's data packets to us get discarded. */
1872             if (st->state==SITE_SENTMSG4) {
1873                 if (process_msg5(st,buf,source,st->new_transform)) {
1874                     transport_setup_msgok(st,source);
1875                     enter_new_state(st,SITE_RUN);
1876                 } else {
1877                     slog(st,LOG_SEC,"invalid MSG5");
1878                 }
1879             } else if (st->state==SITE_RUN) {
1880                 if (process_msg5(st,buf,source,st->current.transform)) {
1881                     slog(st,LOG_DROP,"got MSG5, retransmitting MSG6");
1882                     transport_setup_msgok(st,source);
1883                     create_msg6(st,st->current.transform,
1884                                 st->current.remote_session_id);
1885                     transport_xmit(st,&st->peers,&st->buffer,True);
1886                     BUF_FREE(&st->buffer);
1887                 } else {
1888                     slog(st,LOG_SEC,"invalid MSG5 (in state RUN)");
1889                 }
1890             } else {
1891                 slog(st,LOG_UNEXPECTED,"unexpected MSG5");
1892             }
1893             break;
1894         case LABEL_MSG6:
1895             /* Setup packet: expected only in state SENTMSG5 */
1896             if (st->state!=SITE_SENTMSG5) {
1897                 slog(st,LOG_UNEXPECTED,"unexpected MSG6");
1898             } else if (process_msg6(st,buf,source)) {
1899                 BUF_FREE(&st->buffer); /* Free message 5 */
1900                 transport_setup_msgok(st,source);
1901                 activate_new_key(st);
1902             } else {
1903                 slog(st,LOG_SEC,"invalid MSG6");
1904             }
1905             break;
1906         default:
1907             slog(st,LOG_SEC,"received message of unknown type 0x%08x",
1908                  msgtype);
1909             break;
1910         }
1911         BUF_FREE(buf);
1912         return True;
1913     }
1914
1915     return False;
1916 }
1917
1918 static void site_control(void *vst, bool_t run)
1919 {
1920     struct site *st=vst;
1921     if (run) enter_state_run(st);
1922     else enter_state_stop(st);
1923 }
1924
1925 static void site_phase_hook(void *sst, uint32_t newphase)
1926 {
1927     struct site *st=sst;
1928
1929     /* The program is shutting down; tell our peer */
1930     send_msg7(st,"shutting down");
1931 }
1932
1933 static void site_childpersist_clearkeys(void *sst, uint32_t newphase)
1934 {
1935     struct site *st=sst;
1936     dispose_transform(&st->current.transform);
1937     dispose_transform(&st->auxiliary_key.transform);
1938     dispose_transform(&st->new_transform);
1939     /* Not much point overwiting the signing key, since we loaded it
1940        from disk, and it is only valid prospectively if at all,
1941        anyway. */
1942     /* XXX it would be best to overwrite the DH state, because that
1943        _is_ relevant to forward secrecy.  However we have no
1944        convenient interface for doing that and in practice gmp has
1945        probably dribbled droppings all over the malloc arena.  A good
1946        way to fix this would be to have a privsep child for asymmetric
1947        crypto operations, but that's a task for another day. */
1948 }
1949
1950 static list_t *site_apply(closure_t *self, struct cloc loc, dict_t *context,
1951                           list_t *args)
1952 {
1953     static uint32_t index_sequence;
1954     struct site *st;
1955     item_t *item;
1956     dict_t *dict;
1957     int i;
1958
1959     NEW(st);
1960
1961     st->cl.description="site";
1962     st->cl.type=CL_SITE;
1963     st->cl.apply=NULL;
1964     st->cl.interface=&st->ops;
1965     st->ops.st=st;
1966     st->ops.control=site_control;
1967     st->ops.status=site_status;
1968
1969     /* First parameter must be a dict */
1970     item=list_elem(args,0);
1971     if (!item || item->type!=t_dict)
1972         cfgfatal(loc,"site","parameter must be a dictionary\n");
1973     
1974     dict=item->data.dict;
1975     st->localname=dict_read_string(dict, "local-name", True, "site", loc);
1976     st->remotename=dict_read_string(dict, "name", True, "site", loc);
1977
1978     st->keepalive=dict_read_bool(dict,"keepalive",False,"site",loc,False);
1979
1980     st->peer_mobile=dict_read_bool(dict,"mobile",False,"site",loc,False);
1981     st->local_mobile=
1982         dict_read_bool(dict,"local-mobile",False,"site",loc,False);
1983
1984     /* Sanity check (which also allows the 'sites' file to include
1985        site() closures for all sites including our own): refuse to
1986        talk to ourselves */
1987     if (strcmp(st->localname,st->remotename)==0) {
1988         Message(M_DEBUG,"site %s: local-name==name -> ignoring this site\n",
1989                 st->localname);
1990         if (st->peer_mobile != st->local_mobile)
1991             cfgfatal(loc,"site","site %s's peer-mobile=%d"
1992                     " but our local-mobile=%d\n",
1993                     st->localname, st->peer_mobile, st->local_mobile);
1994         free(st);
1995         return NULL;
1996     }
1997     if (st->peer_mobile && st->local_mobile) {
1998         Message(M_WARNING,"site %s: site is mobile but so are we"
1999                 " -> ignoring this site\n", st->remotename);
2000         free(st);
2001         return NULL;
2002     }
2003
2004     assert(index_sequence < 0xffffffffUL);
2005     st->index = ++index_sequence;
2006     st->local_capabilities = 0;
2007     st->netlink=find_cl_if(dict,"link",CL_NETLINK,True,"site",loc);
2008
2009 #define GET_CLOSURE_LIST(dictkey,things,nthings,CL_TYPE) do{            \
2010     list_t *things##_cfg=dict_lookup(dict,dictkey);                     \
2011     if (!things##_cfg)                                                  \
2012         cfgfatal(loc,"site","closure list \"%s\" not found\n",dictkey); \
2013     st->nthings=list_length(things##_cfg);                              \
2014     NEW_ARY(st->things,st->nthings);                                    \
2015     assert(st->nthings);                                                \
2016     for (i=0; i<st->nthings; i++) {                                     \
2017         item_t *item=list_elem(things##_cfg,i);                         \
2018         if (item->type!=t_closure)                                      \
2019             cfgfatal(loc,"site","%s is not a closure\n",dictkey);       \
2020         closure_t *cl=item->data.closure;                               \
2021         if (cl->type!=CL_TYPE)                                          \
2022             cfgfatal(loc,"site","%s closure wrong type\n",dictkey);     \
2023         st->things[i]=cl->interface;                                    \
2024     }                                                                   \
2025 }while(0)
2026
2027     GET_CLOSURE_LIST("comm",comms,ncomms,CL_COMM);
2028
2029     st->resolver=find_cl_if(dict,"resolver",CL_RESOLVER,True,"site",loc);
2030     st->log=find_cl_if(dict,"log",CL_LOG,True,"site",loc);
2031     st->random=find_cl_if(dict,"random",CL_RANDOMSRC,True,"site",loc);
2032
2033     st->privkey=find_cl_if(dict,"local-key",CL_RSAPRIVKEY,True,"site",loc);
2034     st->addresses=dict_read_string_array(dict,"address",False,"site",loc,0);
2035     if (st->addresses)
2036         st->remoteport=dict_read_number(dict,"port",True,"site",loc,0);
2037     else st->remoteport=0;
2038     st->pubkey=find_cl_if(dict,"key",CL_RSAPUBKEY,True,"site",loc);
2039
2040     GET_CLOSURE_LIST("transform",transforms,ntransforms,CL_TRANSFORM);
2041
2042     st->dh=find_cl_if(dict,"dh",CL_DH,True,"site",loc);
2043     st->hash=find_cl_if(dict,"hash",CL_HASH,True,"site",loc);
2044
2045 #define DEFAULT(D) (st->peer_mobile || st->local_mobile \
2046                     ? DEFAULT_MOBILE_##D : DEFAULT_##D)
2047 #define CFG_NUMBER(k,D) dict_read_number(dict,(k),False,"site",loc,DEFAULT(D));
2048
2049     st->key_lifetime=         CFG_NUMBER("key-lifetime",  KEY_LIFETIME);
2050     st->setup_retries=        CFG_NUMBER("setup-retries", SETUP_RETRIES);
2051     st->setup_retry_interval= CFG_NUMBER("setup-timeout", SETUP_RETRY_INTERVAL);
2052     st->wait_timeout=         CFG_NUMBER("wait-time",     WAIT_TIME);
2053     st->mtu_target= dict_read_number(dict,"mtu-target",False,"site",loc,0);
2054
2055     st->mobile_peer_expiry= dict_read_number(
2056        dict,"mobile-peer-expiry",False,"site",loc,DEFAULT_MOBILE_PEER_EXPIRY);
2057
2058     const char *peerskey= st->peer_mobile
2059         ? "mobile-peers-max" : "static-peers-max";
2060     st->transport_peers_max= dict_read_number(
2061         dict,peerskey,False,"site",loc, st->addresses ? 4 : 3);
2062     if (st->transport_peers_max<1 ||
2063         st->transport_peers_max>MAX_PEER_ADDRS) {
2064         cfgfatal(loc,"site", "%s must be in range 1.."
2065                  STRING(MAX_PEER_ADDRS) "\n", peerskey);
2066     }
2067
2068     if (st->key_lifetime < DEFAULT(KEY_RENEGOTIATE_GAP)*2)
2069         st->key_renegotiate_time=st->key_lifetime/2;
2070     else
2071         st->key_renegotiate_time=st->key_lifetime-DEFAULT(KEY_RENEGOTIATE_GAP);
2072     st->key_renegotiate_time=dict_read_number(
2073         dict,"renegotiate-time",False,"site",loc,st->key_renegotiate_time);
2074     if (st->key_renegotiate_time > st->key_lifetime) {
2075         cfgfatal(loc,"site",
2076                  "renegotiate-time must be less than key-lifetime\n");
2077     }
2078
2079     st->log_events=string_list_to_word(dict_lookup(dict,"log-events"),
2080                                        log_event_table,"site");
2081
2082     st->resolving_count=0;
2083     st->allow_send_prod=0;
2084
2085     st->tunname=safe_malloc(strlen(st->localname)+strlen(st->remotename)+5,
2086                             "site_apply");
2087     sprintf(st->tunname,"%s<->%s",st->localname,st->remotename);
2088
2089     /* The information we expect to see in incoming messages of type 1 */
2090     /* fixme: lots of unchecked overflows here, but the results are only
2091        corrupted packets rather than undefined behaviour */
2092     st->setup_priority=(strcmp(st->localname,st->remotename)>0);
2093
2094     buffer_new(&st->buffer,SETUP_BUFFER_LEN);
2095
2096     buffer_new(&st->scratch,SETUP_BUFFER_LEN);
2097     BUF_ALLOC(&st->scratch,"site:scratch");
2098
2099     /* We are interested in poll(), but only for timeouts. We don't have
2100        any fds of our own. */
2101     register_for_poll(st, site_beforepoll, site_afterpoll, "site");
2102     st->timeout=0;
2103
2104     st->remote_capabilities=0;
2105     st->chosen_transform=0;
2106     st->current.key_timeout=0;
2107     st->auxiliary_key.key_timeout=0;
2108     transport_peers_clear(st,&st->peers);
2109     transport_peers_clear(st,&st->setup_peers);
2110     /* XXX mlock these */
2111     st->dhsecret=safe_malloc(st->dh->len,"site:dhsecret");
2112     st->sharedsecretlen=st->sharedsecretallocd=0;
2113     st->sharedsecret=0;
2114
2115     for (i=0; i<st->ntransforms; i++) {
2116         struct transform_if *ti=st->transforms[i];
2117         uint32_t capbit = 1UL << ti->capab_transformnum;
2118         if (st->local_capabilities & capbit)
2119             slog(st,LOG_ERROR,"transformnum capability bit"
2120                  " %d (%#"PRIx32") reused", ti->capab_transformnum, capbit);
2121         st->local_capabilities |= capbit;
2122     }
2123
2124     /* We need to register the remote networks with the netlink device */
2125     uint32_t netlink_mtu; /* local virtual interface mtu */
2126     st->netlink->reg(st->netlink->st, site_outgoing, st, &netlink_mtu);
2127     if (!st->mtu_target)
2128         st->mtu_target=netlink_mtu;
2129     
2130     for (i=0; i<st->ncomms; i++)
2131         st->comms[i]->request_notify(st->comms[i]->st, st, site_incoming);
2132
2133     st->current.transform=0;
2134     st->auxiliary_key.transform=0;
2135     st->new_transform=0;
2136     st->auxiliary_is_new=0;
2137
2138     enter_state_stop(st);
2139
2140     add_hook(PHASE_SHUTDOWN,site_phase_hook,st);
2141     add_hook(PHASE_CHILDPERSIST,site_childpersist_clearkeys,st);
2142
2143     return new_closure(&st->cl);
2144 }
2145
2146 void site_module(dict_t *dict)
2147 {
2148     add_closure(dict,"site",site_apply);
2149 }
2150
2151
2152 /***** TRANSPORT PEERS definitions *****/
2153
2154 static void transport_peers_debug(struct site *st, transport_peers *dst,
2155                                   const char *didwhat,
2156                                   int nargs, const struct comm_addr *args,
2157                                   size_t stride) {
2158     int i;
2159     char *argp;
2160
2161     if (!(st->log_events & LOG_PEER_ADDRS))
2162         return; /* an optimisation */
2163
2164     slog(st, LOG_PEER_ADDRS, "peers (%s) %s nargs=%d => npeers=%d",
2165          (dst==&st->peers ? "data" :
2166           dst==&st->setup_peers ? "setup" : "UNKNOWN"),
2167          didwhat, nargs, dst->npeers);
2168
2169     for (i=0, argp=(void*)args;
2170          i<nargs;
2171          i++, (argp+=stride?stride:sizeof(*args))) {
2172         const struct comm_addr *ca=(void*)argp;
2173         slog(st, LOG_PEER_ADDRS, " args: addrs[%d]=%s",
2174              i, comm_addr_to_string(ca));
2175     }
2176     for (i=0; i<dst->npeers; i++) {
2177         struct timeval diff;
2178         timersub(tv_now,&dst->peers[i].last,&diff);
2179         const struct comm_addr *ca=&dst->peers[i].addr;
2180         slog(st, LOG_PEER_ADDRS, " peers: addrs[%d]=%s T-%ld.%06ld",
2181              i, comm_addr_to_string(ca),
2182              (unsigned long)diff.tv_sec, (unsigned long)diff.tv_usec);
2183     }
2184 }
2185
2186 static void transport_peers_expire(struct site *st, transport_peers *peers) {
2187     /* peers must be sorted first */
2188     int previous_peers=peers->npeers;
2189     struct timeval oldest;
2190     oldest.tv_sec  = tv_now->tv_sec - st->mobile_peer_expiry;
2191     oldest.tv_usec = tv_now->tv_usec;
2192     while (peers->npeers>1 &&
2193            timercmp(&peers->peers[peers->npeers-1].last, &oldest, <))
2194         peers->npeers--;
2195     if (peers->npeers != previous_peers)
2196         transport_peers_debug(st,peers,"expire", 0,0,0);
2197 }
2198
2199 static bool_t transport_peer_record_one(struct site *st, transport_peers *peers,
2200                                         const struct comm_addr *ca,
2201                                         const struct timeval *tv) {
2202     /* returns false if output is full */
2203     int search;
2204
2205     if (peers->npeers >= st->transport_peers_max)
2206         return 0;
2207
2208     for (search=0; search<peers->npeers; search++)
2209         if (comm_addr_equal(&peers->peers[search].addr, ca))
2210             return 1;
2211
2212     peers->peers[peers->npeers].addr = *ca;
2213     peers->peers[peers->npeers].last = *tv;
2214     peers->npeers++;
2215     return 1;
2216 }
2217
2218 static void transport_record_peers(struct site *st, transport_peers *peers,
2219                                    const struct comm_addr *addrs, int naddrs,
2220                                    const char *m) {
2221     /* We add addrs into peers.  The new entries end up at the front
2222      * and displace entries towards the end (perhaps even off the
2223      * end).  Any existing matching entries are moved up to the front.
2224      *
2225      * Caller must first call transport_peers_expire. */
2226
2227     if (naddrs==1 && peers->npeers>=1 &&
2228         comm_addr_equal(&addrs[0], &peers->peers[0].addr)) {
2229         /* optimisation, also avoids debug for trivial updates */
2230         peers->peers[0].last = *tv_now;
2231         return;
2232     }
2233
2234     int old_npeers=peers->npeers;
2235     transport_peer old_peers[old_npeers];
2236     COPY_ARRAY(old_peers,peers->peers,old_npeers);
2237
2238     peers->npeers=0;
2239     int i;
2240     for (i=0; i<naddrs; i++) {
2241         if (!transport_peer_record_one(st,peers, &addrs[i], tv_now))
2242             break;
2243     }
2244     for (i=0; i<old_npeers; i++) {
2245         const transport_peer *old=&old_peers[i];
2246         if (!transport_peer_record_one(st,peers, &old->addr, &old->last))
2247             break;
2248     }
2249
2250     transport_peers_debug(st,peers,m, naddrs,addrs,0);
2251 }
2252
2253 static void transport_expire_record_peers(struct site *st,
2254                                           transport_peers *peers,
2255                                           const struct comm_addr *addrs,
2256                                           int naddrs, const char *m) {
2257     /* Convenience function */
2258     transport_peers_expire(st,peers);
2259     transport_record_peers(st,peers,addrs,naddrs,m);
2260 }
2261
2262 static bool_t transport_compute_setupinit_peers(struct site *st,
2263         const struct comm_addr *configured_addrs /* 0 if none or not found */,
2264         int n_configured_addrs /* 0 if none or not found */,
2265         const struct comm_addr *incoming_packet_addr /* 0 if none */) {
2266     if (!n_configured_addrs && !incoming_packet_addr &&
2267         !transport_peers_valid(&st->peers))
2268         return False;
2269
2270     slog(st,LOG_SETUP_INIT,
2271          "using: %d configured addr(s);%s %d old peer addrs(es)",
2272          n_configured_addrs,
2273          incoming_packet_addr ? " incoming packet address;" : "",
2274          st->peers.npeers);
2275
2276     /* Non-mobile peers try addresses until one is plausible.  The
2277      * effect is that this code always tries first the configured
2278      * address if supplied, or otherwise the address of the incoming
2279      * PROD, or finally the existing data peer if one exists; this is
2280      * as desired. */
2281
2282     transport_peers_copy(st,&st->setup_peers,&st->peers);
2283     transport_peers_expire(st,&st->setup_peers);
2284
2285     if (incoming_packet_addr)
2286         transport_record_peers(st,&st->setup_peers,
2287                                incoming_packet_addr,1, "incoming");
2288
2289     if (n_configured_addrs)
2290         transport_record_peers(st,&st->setup_peers,
2291                               configured_addrs,n_configured_addrs, "setupinit");
2292
2293     assert(transport_peers_valid(&st->setup_peers));
2294     return True;
2295 }
2296
2297 static void transport_setup_msgok(struct site *st, const struct comm_addr *a) {
2298     if (st->peer_mobile)
2299         transport_expire_record_peers(st,&st->setup_peers,a,1,"setupmsg");
2300 }
2301 static void transport_data_msgok(struct site *st, const struct comm_addr *a) {
2302     if (st->peer_mobile)
2303         transport_expire_record_peers(st,&st->peers,a,1,"datamsg");
2304 }
2305
2306 static int transport_peers_valid(transport_peers *peers) {
2307     return peers->npeers;
2308 }
2309 static void transport_peers_clear(struct site *st, transport_peers *peers) {
2310     peers->npeers= 0;
2311     transport_peers_debug(st,peers,"clear",0,0,0);
2312 }
2313 static void transport_peers_copy(struct site *st, transport_peers *dst,
2314                                  const transport_peers *src) {
2315     dst->npeers=src->npeers;
2316     COPY_ARRAY(dst->peers, src->peers, dst->npeers);
2317     transport_peers_debug(st,dst,"copy",
2318                           src->npeers, &src->peers->addr, sizeof(*src->peers));
2319 }
2320
2321 static void transport_resolve_complete(struct site *st,
2322                                        const struct comm_addr *addrs,
2323                                        int naddrs) {
2324     transport_expire_record_peers(st,&st->peers,addrs,naddrs,
2325                                   "resolved data");
2326     transport_expire_record_peers(st,&st->setup_peers,addrs,naddrs,
2327                                   "resolved setup");
2328 }
2329
2330 static void transport_resolve_complete_tardy(struct site *st,
2331                                              const struct comm_addr *addrs,
2332                                              int naddrs) {
2333     transport_expire_record_peers(st,&st->peers,addrs,naddrs,
2334                                   "resolved tardily");
2335 }
2336
2337 static void transport_peers__copy_by_mask(transport_peer *out, int *nout_io,
2338                                           unsigned mask,
2339                                           const transport_peers *inp) {
2340     /* out and in->peers may be the same region, or nonoverlapping */
2341     const transport_peer *in=inp->peers;
2342     int slot;
2343     for (slot=0; slot<inp->npeers; slot++) {
2344         if (!(mask & (1U << slot)))
2345             continue;
2346         if (!(out==in && slot==*nout_io))
2347             COPY_OBJ(out[*nout_io], in[slot]);
2348         (*nout_io)++;
2349     }
2350 }
2351
2352 void transport_xmit(struct site *st, transport_peers *peers,
2353                     struct buffer_if *buf, bool_t candebug) {
2354     int slot;
2355     transport_peers_expire(st, peers);
2356     unsigned failed=0; /* bitmask */
2357     assert(MAX_PEER_ADDRS < sizeof(unsigned)*CHAR_BIT);
2358
2359     int nfailed=0;
2360     for (slot=0; slot<peers->npeers; slot++) {
2361         transport_peer *peer=&peers->peers[slot];
2362         bool_t ok =
2363             peer->addr.comm->sendmsg(peer->addr.comm->st, buf, &peer->addr);
2364         if (candebug)
2365             dump_packet(st, buf, &peer->addr, False, ok);
2366         if (!ok) {
2367             failed |= 1U << slot;
2368             nfailed++;
2369         }
2370         if (ok && !st->peer_mobile)
2371             break;
2372     }
2373     /* Now we need to demote/delete failing addrs: if we are mobile we
2374      * merely demote them; otherwise we delete them. */
2375     if (st->local_mobile) {
2376         unsigned expected = ((1U << nfailed)-1) << (peers->npeers-nfailed);
2377         /* `expected' has all the failures at the end already */
2378         if (failed != expected) {
2379             int fslot=0;
2380             transport_peer failedpeers[nfailed];
2381             transport_peers__copy_by_mask(failedpeers, &fslot, failed,peers);
2382             assert(fslot == nfailed);
2383             int wslot=0;
2384             transport_peers__copy_by_mask(peers->peers,&wslot,~failed,peers);
2385             assert(wslot+nfailed == peers->npeers);
2386             COPY_ARRAY(peers->peers+wslot, failedpeers, nfailed);
2387             transport_peers_debug(st,peers,"mobile failure reorder",0,0,0);
2388         }
2389     } else {
2390         if (failed && peers->npeers > 1) {
2391             int wslot=0;
2392             transport_peers__copy_by_mask(peers->peers,&wslot,~failed,peers);
2393             peers->npeers=wslot;
2394             transport_peers_debug(st,peers,"non-mobile failure cleanup",0,0,0);
2395         }
2396     }
2397 }
2398
2399 /***** END of transport peers declarations *****/