connection_or.c 46.3 KB
Newer Older
Roger Dingledine's avatar
Roger Dingledine committed
1
2
/* Copyright (c) 2001 Matej Pfajfar.
 * Copyright (c) 2001-2004, Roger Dingledine.
3
 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
Karsten Loesing's avatar
Karsten Loesing committed
4
 * Copyright (c) 2007-2009, The Tor Project, Inc. */
5
/* See LICENSE for licensing information */
Roger Dingledine's avatar
Roger Dingledine committed
6

Roger Dingledine's avatar
Roger Dingledine committed
7
8
9
10
11
12
/**
 * \file connection_or.c
 * \brief Functions to handle OR connections, TLS handshaking, and
 * cells on the network.
 **/

Roger Dingledine's avatar
Roger Dingledine committed
13
#include "or.h"
14

15
16
static int connection_tls_finish_handshake(or_connection_t *conn);
static int connection_or_process_cells_from_inbuf(or_connection_t *conn);
17
static int connection_or_send_versions(or_connection_t *conn);
18
19
static int connection_init_or_handshake_state(or_connection_t *conn,
                                              int started_here);
20
21
22
static int connection_or_check_valid_tls_handshake(or_connection_t *conn,
                                                   int started_here,
                                                   char *digest_rcvd_out);
23

Roger Dingledine's avatar
Roger Dingledine committed
24
25
/**************************************************************/

26
27
/** Map from identity digest of connected OR or desired OR to a connection_t
 * with that identity digest.  If there is more than one such connection_t,
Roger Dingledine's avatar
Roger Dingledine committed
28
 * they form a linked list, with next_with_same_id as the next pointer. */
29
30
31
static digestmap_t *orconn_identity_map = NULL;

/** If conn is listed in orconn_identity_map, remove it, and clear
32
 * conn->identity_digest.  Otherwise do nothing. */
33
void
34
connection_or_remove_from_identity_map(or_connection_t *conn)
35
{
36
  or_connection_t *tmp;
37
38
39
40
  tor_assert(conn);
  if (!orconn_identity_map)
    return;
  tmp = digestmap_get(orconn_identity_map, conn->identity_digest);
41
42
  if (!tmp) {
    if (!tor_digest_is_zero(conn->identity_digest)) {
43
44
45
      log_warn(LD_BUG, "Didn't find connection '%s' on identity map when "
               "trying to remove it.",
               conn->nickname ? conn->nickname : "NULL");
46
    }
47
    return;
48
  }
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  if (conn == tmp) {
    if (conn->next_with_same_id)
      digestmap_set(orconn_identity_map, conn->identity_digest,
                    conn->next_with_same_id);
    else
      digestmap_remove(orconn_identity_map, conn->identity_digest);
  } else {
    while (tmp->next_with_same_id) {
      if (tmp->next_with_same_id == conn) {
        tmp->next_with_same_id = conn->next_with_same_id;
        break;
      }
      tmp = tmp->next_with_same_id;
    }
  }
  memset(conn->identity_digest, 0, DIGEST_LEN);
  conn->next_with_same_id = NULL;
}

68
69
70
71
72
/** Remove all entries from the identity-to-orconn map, and clear
 * all identities in OR conns.*/
void
connection_or_clear_identity_map(void)
{
73
74
75
  smartlist_t *conns = get_connection_array();
  SMARTLIST_FOREACH(conns, connection_t *, conn,
  {
76
    if (conn->type == CONN_TYPE_OR) {
77
78
79
      or_connection_t *or_conn = TO_OR_CONN(conn);
      memset(or_conn->identity_digest, 0, DIGEST_LEN);
      or_conn->next_with_same_id = NULL;
80
    }
81
  });
82

83
84
85
86
  if (orconn_identity_map) {
    digestmap_free(orconn_identity_map, NULL);
    orconn_identity_map = NULL;
  }
87
88
}

89
/** Change conn->identity_digest to digest, and add conn into
90
 * orconn_digest_map. */
91
static void
92
connection_or_set_identity_digest(or_connection_t *conn, const char *digest)
93
{
94
  or_connection_t *tmp;
95
96
97
98
99
100
101
  tor_assert(conn);
  tor_assert(digest);

  if (!orconn_identity_map)
    orconn_identity_map = digestmap_new();
  if (!memcmp(conn->identity_digest, digest, DIGEST_LEN))
    return;
102
103
104

  /* If the identity was set previously, remove the old mapping. */
  if (! tor_digest_is_zero(conn->identity_digest))
105
106
107
    connection_or_remove_from_identity_map(conn);

  memcpy(conn->identity_digest, digest, DIGEST_LEN);
108

109
  /* If we're setting the ID to zero, don't add a mapping. */
110
111
112
  if (tor_digest_is_zero(digest))
    return;

113
114
115
  tmp = digestmap_set(orconn_identity_map, digest, conn);
  conn->next_with_same_id = tmp;

116
#if 1
117
  /* Testing code to check for bugs in representation. */
118
119
120
121
  for (; tmp; tmp = tmp->next_with_same_id) {
    tor_assert(!memcmp(tmp->identity_digest, digest, DIGEST_LEN));
    tor_assert(tmp != conn);
  }
122
#endif
123
124
}

Roger Dingledine's avatar
Roger Dingledine committed
125
126
/** Pack the cell_t host-order structure <b>src</b> into network-order
 * in the buffer <b>dest</b>. See tor-spec.txt for details about the
127
 * wire format.
Roger Dingledine's avatar
Roger Dingledine committed
128
129
130
 *
 * Note that this function doesn't touch <b>dst</b>-\>next: the caller
 * should set it or clear it as appropriate.
131
 */
132
133
void
cell_pack(packed_cell_t *dst, const cell_t *src)
134
{
135
  char *dest = dst->body;
136
137
  *(uint16_t*)dest    = htons(src->circ_id);
  *(uint8_t*)(dest+2) = src->command;
Roger Dingledine's avatar
Roger Dingledine committed
138
  memcpy(dest+3, src->payload, CELL_PAYLOAD_SIZE);
Roger Dingledine's avatar
Roger Dingledine committed
139
140
}

Roger Dingledine's avatar
Roger Dingledine committed
141
142
/** Unpack the network-order buffer <b>src</b> into a host-order
 * cell_t structure <b>dest</b>.
143
 */
144
145
146
static void
cell_unpack(cell_t *dest, const char *src)
{
147
  dest->circ_id = ntohs(*(uint16_t*)(src));
Roger Dingledine's avatar
Roger Dingledine committed
148
  dest->command = *(uint8_t*)(src+2);
Roger Dingledine's avatar
Roger Dingledine committed
149
  memcpy(dest->payload, src+3, CELL_PAYLOAD_SIZE);
Roger Dingledine's avatar
Roger Dingledine committed
150
151
}

152
153
/** Write the header of <b>cell</b> into the first VAR_CELL_HEADER_SIZE
 * bytes of <b>hdr_out</b>. */
154
void
155
var_cell_pack_header(const var_cell_t *cell, char *hdr_out)
156
{
157
158
  set_uint16(hdr_out, htons(cell->circ_id));
  set_uint8(hdr_out+2, cell->command);
159
160
161
  set_uint16(hdr_out+3, htons(cell->payload_len));
}

162
163
/** Allocate and return a new var_cell_t with <b>payload_len</b> bytes of
 * payload space. */
164
165
166
167
168
169
170
171
172
173
var_cell_t *
var_cell_new(uint16_t payload_len)
{
  var_cell_t *cell = tor_malloc(sizeof(var_cell_t)+payload_len-1);
  cell->payload_len = payload_len;
  cell->command = 0;
  cell->circ_id = 0;
  return cell;
}

Roger Dingledine's avatar
Roger Dingledine committed
174
/** Release all space held by <b>cell</b>. */
175
176
177
178
179
180
void
var_cell_free(var_cell_t *cell)
{
  tor_free(cell);
}

Roger Dingledine's avatar
Roger Dingledine committed
181
/** We've received an EOF from <b>conn</b>. Mark it for close and return. */
182
int
183
connection_or_reached_eof(or_connection_t *conn)
184
{
Roger Dingledine's avatar
Roger Dingledine committed
185
  log_info(LD_OR,"OR connection reached EOF. Closing.");
186
  connection_mark_for_close(TO_CONN(conn));
187
188
189
  return 0;
}

190
191
192
193
194
195
/** Read conn's inbuf. If the http response from the proxy is all
 * here, make sure it's good news, and begin the tls handshake. If
 * it's bad news, close the connection and return -1. Else return 0
 * and hope for better luck next time.
 */
static int
196
connection_or_read_proxy_response(or_connection_t *or_conn)
197
{
198
  char *headers;
199
  char *reason=NULL;
200
201
  int status_code;
  time_t date_header;
202
  connection_t *conn = TO_CONN(or_conn);
203
204
205

  switch (fetch_from_buf_http(conn->inbuf,
                              &headers, MAX_HEADERS_SIZE,
206
                              NULL, NULL, 10000, 0)) {
207
    case -1: /* overflow */
Roger Dingledine's avatar
Roger Dingledine committed
208
209
      log_warn(LD_PROTOCOL,
               "Your https proxy sent back an oversized response. Closing.");
210
211
      return -1;
    case 0:
Roger Dingledine's avatar
Roger Dingledine committed
212
      log_info(LD_OR,"https proxy response not all here yet. Waiting.");
213
214
215
216
217
      return 0;
    /* case 1, fall through */
  }

  if (parse_http_response(headers, &status_code, &date_header,
218
                          NULL, &reason) < 0) {
Roger Dingledine's avatar
Roger Dingledine committed
219
220
221
    log_warn(LD_OR,
             "Unparseable headers from proxy (connecting to '%s'). Closing.",
             conn->address);
222
223
224
    tor_free(headers);
    return -1;
  }
225
  if (!reason) reason = tor_strdup("[no reason given]");
226
227

  if (status_code == 200) {
Roger Dingledine's avatar
Roger Dingledine committed
228
    log_info(LD_OR,
229
230
             "HTTPS connect to '%s' successful! (200 %s) Starting TLS.",
             conn->address, escaped(reason));
231
    tor_free(reason);
232
    if (connection_tls_start_handshake(or_conn, 0) < 0) {
233
234
      /* TLS handshaking error of some kind. */
      connection_mark_for_close(conn);
235

236
237
238
239
240
      return -1;
    }
    return 0;
  }
  /* else, bad news on the status code */
Roger Dingledine's avatar
Roger Dingledine committed
241
  log_warn(LD_OR,
242
           "The https proxy sent back an unexpected status code %d (%s). "
Roger Dingledine's avatar
Roger Dingledine committed
243
           "Closing.",
244
           status_code, escaped(reason));
245
  tor_free(reason);
246
247
248
249
  connection_mark_for_close(conn);
  return -1;
}

Roger Dingledine's avatar
Roger Dingledine committed
250
/** Handle any new bytes that have come in on connection <b>conn</b>.
251
252
253
254
 * If conn is in 'open' state, hand it to
 * connection_or_process_cells_from_inbuf()
 * (else do nothing).
 */
255
int
256
connection_or_process_inbuf(or_connection_t *conn)
257
{
258
  tor_assert(conn);
Roger Dingledine's avatar
Roger Dingledine committed
259

260
  switch (conn->_base.state) {
261
262
263
    case OR_CONN_STATE_PROXY_READING:
      return connection_or_read_proxy_response(conn);
    case OR_CONN_STATE_OPEN:
264
    case OR_CONN_STATE_OR_HANDSHAKING:
265
266
267
268
      return connection_or_process_cells_from_inbuf(conn);
    default:
      return 0; /* don't do anything */
  }
Roger Dingledine's avatar
Roger Dingledine committed
269
270
}

271
272
/** When adding cells to an OR connection's outbuf, keep adding until the
 * outbuf is at least this long, or we run out of cells. */
273
274
#define OR_CONN_HIGHWATER (32*1024)

275
276
/** Add cells to an OR connection's outbuf whenever the outbuf's data length
 * drops below this size. */
277
278
#define OR_CONN_LOWWATER (16*1024)

279
280
/** Called whenever we have flushed some data on an or_conn: add more data
 * from active circuits. */
281
282
283
int
connection_or_flushed_some(or_connection_t *conn)
{
284
  size_t datalen = buf_datalen(conn->_base.outbuf);
285
286
  /* If we're under the low water mark, add cells until we're just over the
   * high water mark. */
287
  if (datalen < OR_CONN_LOWWATER) {
288
    ssize_t n = (OR_CONN_HIGHWATER - datalen + CELL_NETWORK_SIZE-1)
289
      / CELL_NETWORK_SIZE;
290
    time_t now = approx_time();
291
    while (conn->active_circuits && n > 0) {
292
293
      int flushed;
      flushed = connection_or_flush_from_first_active_circuit(conn, 1, now);
294
295
296
      n -= flushed;
    }
  }
297
298
299
  return 0;
}

Roger Dingledine's avatar
Roger Dingledine committed
300
/** Connection <b>conn</b> has finished writing and has no bytes left on
301
302
 * its outbuf.
 *
Nick Mathewson's avatar
Nick Mathewson committed
303
 * Otherwise it's in state "open": stop writing and return.
304
305
306
 *
 * If <b>conn</b> is broken, mark it for close and return -1, else
 * return 0.
307
 */
308
int
309
connection_or_finished_flushing(or_connection_t *conn)
310
{
311
  tor_assert(conn);
312
  assert_connection_ok(TO_CONN(conn),0);
Roger Dingledine's avatar
Roger Dingledine committed
313

314
  switch (conn->_base.state) {
315
    case OR_CONN_STATE_PROXY_FLUSHING:
Roger Dingledine's avatar
Roger Dingledine committed
316
      log_debug(LD_OR,"finished sending CONNECT to proxy.");
317
318
      conn->_base.state = OR_CONN_STATE_PROXY_READING;
      connection_stop_writing(TO_CONN(conn));
319
320
      break;
    case OR_CONN_STATE_OPEN:
321
    case OR_CONN_STATE_OR_HANDSHAKING:
322
      connection_stop_writing(TO_CONN(conn));
323
324
      break;
    default:
325
      log_err(LD_BUG,"Called in unexpected state %d.", conn->_base.state);
326
      tor_fragile_assert();
327
      return -1;
328
329
330
331
332
333
  }
  return 0;
}

/** Connected handler for OR connections: begin the TLS handshake.
 */
334
int
335
connection_or_finished_connecting(or_connection_t *or_conn)
336
{
337
338
339
  connection_t *conn;
  tor_assert(or_conn);
  conn = TO_CONN(or_conn);
340
341
  tor_assert(conn->state == OR_CONN_STATE_CONNECTING);

Roger Dingledine's avatar
Roger Dingledine committed
342
343
  log_debug(LD_OR,"OR connect() to router at %s:%u finished.",
            conn->address,conn->port);
344
  control_event_bootstrap(BOOTSTRAP_STATUS_HANDSHAKE, 0);
345

346
347
  if (get_options()->HttpsProxy) {
    char buf[1024];
348
    char *base64_authenticator=NULL;
349
    const char *authenticator = get_options()->HttpsProxyAuthenticator;
350

351
    if (authenticator) {
352
353
      base64_authenticator = alloc_http_authenticator(authenticator);
      if (!base64_authenticator)
Roger Dingledine's avatar
Roger Dingledine committed
354
        log_warn(LD_OR, "Encoding https authenticator failed");
355
356
    }
    if (base64_authenticator) {
357
      tor_snprintf(buf, sizeof(buf), "CONNECT %s:%d HTTP/1.1\r\n"
358
359
                   "Proxy-Authorization: Basic %s\r\n\r\n",
                   fmt_addr(&conn->addr),
360
361
362
363
                   conn->port, base64_authenticator);
      tor_free(base64_authenticator);
    } else {
      tor_snprintf(buf, sizeof(buf), "CONNECT %s:%d HTTP/1.0\r\n\r\n",
364
                   fmt_addr(&conn->addr), conn->port);
365
    }
366
367
368
369
370
    connection_write_to_buf(buf, strlen(buf), conn);
    conn->state = OR_CONN_STATE_PROXY_FLUSHING;
    return 0;
  }

371
  if (connection_tls_start_handshake(or_conn, 0) < 0) {
372
373
    /* TLS handshaking error of some kind. */
    connection_mark_for_close(conn);
374
    return -1;
Roger Dingledine's avatar
Roger Dingledine committed
375
  }
376
  return 0;
Roger Dingledine's avatar
Roger Dingledine committed
377
378
}

379
380
381
/** If we don't necessarily know the router we're connecting to, but we
 * have an addr/port/id_digest, then fill in as much as we can. Start
 * by checking to see if this describes a router we know. */
382
static void
383
connection_or_init_conn_from_address(or_connection_t *conn,
384
                                     const tor_addr_t *addr, uint16_t port,
385
386
                                     const char *id_digest,
                                     int started_here)
387
{
388
389
  or_options_t *options = get_options();
  routerinfo_t *r = router_get_by_digest(id_digest);
390
  conn->bandwidthrate = (int)options->BandwidthRate;
391
  conn->read_bucket = conn->bandwidthburst = (int)options->BandwidthBurst;
392
  connection_or_set_identity_digest(conn, id_digest);
393

394
  conn->_base.port = port;
395
396
  tor_addr_copy(&conn->_base.addr, addr);
  tor_addr_copy(&conn->real_addr, addr);
397
  if (r) {
398
    /* XXXX proposal 118 will make this more complex. */
399
    if (tor_addr_eq_ipv4h(&conn->_base.addr, r->addr))
400
      conn->is_canonical = 1;
401
402
403
404
    if (!started_here) {
      /* Override the addr/port, so our log messages will make sense.
       * This is dangerous, since if we ever try looking up a conn by
       * its actual addr/port, we won't remember. Careful! */
405
      /* XXXX arma: this is stupid, and it's the reason we need real_addr
406
       * to track is_canonical properly.  What requires it? */
407
408
409
410
      /* XXXX <arma> i believe the reason we did this, originally, is because
       * we wanted to log what OR a connection was to, and if we logged the
       * right IP address and port 56244, that wouldn't be as helpful. now we
       * log the "right" port too, so we know if it's moria1 or moria2.
411
412
       */
      tor_addr_from_ipv4h(&conn->_base.addr, r->addr);
413
      conn->_base.port = r->or_port;
414
415
    }
    conn->nickname = tor_strdup(r->nickname);
416
417
    tor_free(conn->_base.address);
    conn->_base.address = tor_strdup(r->address);
418
  } else {
419
420
421
422
423
424
425
426
427
428
429
430
    const char *n;
    /* If we're an authoritative directory server, we may know a
     * nickname for this router. */
    n = dirserv_get_nickname_by_digest(id_digest);
    if (n) {
      conn->nickname = tor_strdup(n);
    } else {
      conn->nickname = tor_malloc(HEX_DIGEST_LEN+2);
      conn->nickname[0] = '$';
      base16_encode(conn->nickname+1, HEX_DIGEST_LEN+1,
                    conn->identity_digest, DIGEST_LEN);
    }
431
    tor_free(conn->_base.address);
432
    conn->_base.address = tor_dup_addr(addr);
433
  }
434
435
}

436
/** Return true iff <b>a</b> is "better" than <b>b</b> for new circuits.
437
 *
438
439
440
441
442
443
 * A more canonical connection is always better than a less canonical
 * connection.  That aside, a connection is better if it has circuits and the
 * other does not, or if it was created more recently.
 *
 * Requires that both input connections are open; not is_bad_for_new_circs,
 * and not impossibly non-canonical.
444
445
446
447
 *
 * If </b>forgive_new_connections</b> is true, then we do not call
 * <b>a</b>better than <b>b</b> simply because b has no circuits,
 * unless b is also relatively old.
448
 */
449
static int
450
451
452
453
connection_or_is_better(time_t now,
                        const or_connection_t *a,
                        const or_connection_t *b,
                        int forgive_new_connections)
454
455
{
  int newer;
456
457
458
/** Do not definitively deprecate a new connection with no circuits on it
 * until this much time has passed. */
#define NEW_CONN_GRACE_PERIOD (15*60)
459
460
461
462
463
464
465

  if (b->is_canonical && !a->is_canonical)
    return 0; /* A canonical connection is better than a non-canonical
               * one, no matter how new it is or which has circuits. */

  newer = b->_base.timestamp_created < a->_base.timestamp_created;

466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
  if (
      /* We prefer canonical connections regardless of newness. */
      (!b->is_canonical && a->is_canonical) ||
      /* If both have circuits we prefer the newer: */
      (b->n_circuits && a->n_circuits && newer) ||
      /* If neither has circuits we prefer the newer: */
      (!b->n_circuits && !a->n_circuits && newer))
    return 1;

  /* If one has no circuits and the other does... */
  if (!b->n_circuits && a->n_circuits) {
    /* Then it's bad, unless it's in its grace period and we're forgiving. */
    if (forgive_new_connections &&
        now < b->_base.timestamp_created + NEW_CONN_GRACE_PERIOD)
      return 0;
    else
      return 1;
  }

  return 0;
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
}

/** Return the OR connection we should use to extend a circuit to the router
 * whose identity is <b>digest</b>, and whose address we believe (or have been
 * told in an extend cell) is <b>target_addr</b>.  If there is no good
 * connection, set *<b>msg_out</b> to a message describing the connection's
 * state and our next action, and set <b>launch_out</b> to a boolean for
 * whether we should launch a new connection or not.
 */
or_connection_t *
connection_or_get_for_extend(const char *digest,
                             const tor_addr_t *target_addr,
                             const char **msg_out,
                             int *launch_out)
{
501
  or_connection_t *conn, *best=NULL;
502
  int n_inprogress_goodaddr = 0, n_old = 0, n_noncanonical = 0, n_possible = 0;
503
  time_t now = approx_time();
504

505
506
507
508
509
510
  tor_assert(msg_out);
  tor_assert(launch_out);

  if (!orconn_identity_map) {
    *msg_out = "Router not connected (nothing is).  Connecting.";
    *launch_out = 1;
511
    return NULL;
512
  }
513
514
515
516

  conn = digestmap_get(orconn_identity_map, digest);

  for (; conn; conn = conn->next_with_same_id) {
517
518
    tor_assert(conn->_base.magic == OR_CONNECTION_MAGIC);
    tor_assert(conn->_base.type == CONN_TYPE_OR);
519
    tor_assert(!memcmp(conn->identity_digest, digest, DIGEST_LEN));
520
    if (conn->_base.marked_for_close)
521
      continue;
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
    /* Never return a non-open connection. */
    if (conn->_base.state != OR_CONN_STATE_OPEN) {
      /* If the address matches, don't launch a new connection for this
       * circuit. */
      if (!tor_addr_compare(&conn->real_addr, target_addr, CMP_EXACT))
        ++n_inprogress_goodaddr;
      continue;
    }
    /* Never return a connection that shouldn't be used for circs. */
    if (conn->is_bad_for_new_circs) {
      ++n_old;
      continue;
    }
    /* Never return a non-canonical connection using a recent link protocol
     * if the address is not what we wanted.
     *
     * (For old link protocols, we can't rely on is_canonical getting
     * set properly if we're talking to the right address, since we might
     * have an out-of-date descriptor, and we will get no NETINFO cell to
     * tell us about the right address.) */
    if (!conn->is_canonical && conn->link_proto >= 2 &&
        tor_addr_compare(&conn->real_addr, target_addr, CMP_EXACT)) {
      ++n_noncanonical;
      continue;
    }

    ++n_possible;

550
    if (!best) {
551
      best = conn; /* If we have no 'best' so far, this one is good enough. */
552
553
      continue;
    }
554

555
    if (connection_or_is_better(now, conn, best, 0))
556
      best = conn;
557
  }
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642

  if (best) {
    *msg_out = "Connection is fine; using it.";
    *launch_out = 0;
    return best;
  } else if (n_inprogress_goodaddr) {
    *msg_out = "Connection in progress; waiting.";
    *launch_out = 0;
    return NULL;
  } else if (n_old || n_noncanonical) {
    *msg_out = "Connections all too old, or too non-canonical. "
      " Launching a new one.";
    *launch_out = 1;
    return NULL;
  } else {
    *msg_out = "Not connected. Connecting.";
    *launch_out = 1;
    return NULL;
  }
}

/** How old do we let a connection to an OR get before deciding it's
 * too old for new circuits? */
#define TIME_BEFORE_OR_CONN_IS_TOO_OLD (60*60*24*7)

/** Given the head of the linked list for all the or_connections with a given
 * identity, set elements of that list as is_bad_for_new_circs() as
 * appropriate.  Helper for connection_or_set_bad_connections().
 */
static void
connection_or_group_set_badness(or_connection_t *head)
{
  or_connection_t *or_conn = NULL, *best = NULL;
  int n_old = 0, n_inprogress = 0, n_canonical = 0, n_other = 0;
  time_t now = time(NULL);

  /* Pass 1: expire everything that's old, and see what the status of
   * everything else is. */
  for (or_conn = head; or_conn; or_conn = or_conn->next_with_same_id) {
    if (or_conn->_base.marked_for_close ||
        or_conn->is_bad_for_new_circs)
      continue;
    if (or_conn->_base.timestamp_created + TIME_BEFORE_OR_CONN_IS_TOO_OLD
        < now) {
      log_info(LD_OR,
               "Marking OR conn to %s:%d as too old for new circuits "
               "(fd %d, %d secs old).",
               or_conn->_base.address, or_conn->_base.port, or_conn->_base.s,
               (int)(now - or_conn->_base.timestamp_created));
      or_conn->is_bad_for_new_circs = 1;
    }

    if (or_conn->is_bad_for_new_circs) {
      ++n_old;
    } else if (or_conn->_base.state != OR_CONN_STATE_OPEN) {
      ++n_inprogress;
    } else if (or_conn->is_canonical) {
      ++n_canonical;
    } else {
      ++n_other;
    }
  }

  /* Pass 2: We know how about how good the best connection is.
   * expire everything that's worse, and find the very best if we can. */
  for (or_conn = head; or_conn; or_conn = or_conn->next_with_same_id) {
    if (or_conn->_base.marked_for_close ||
        or_conn->is_bad_for_new_circs)
      continue; /* This one doesn't need to be marked bad. */
    if (or_conn->_base.state != OR_CONN_STATE_OPEN)
      continue; /* Don't mark anything bad until we have seen what happens
                 * when the connection finishes. */
    if (n_canonical && !or_conn->is_canonical) {
      /* We have at least one open canonical connection to this router,
       * and this one is open but not canonical.  Mark it bad. */
      log_info(LD_OR,
               "Marking OR conn to %s:%d as too old for new circuits: "
               "(fd %d, %d secs old).  It is not canonical, and we have "
               "another connection to that OR that is.",
               or_conn->_base.address, or_conn->_base.port, or_conn->_base.s,
               (int)(now - or_conn->_base.timestamp_created));
      or_conn->is_bad_for_new_circs = 1;
      continue;
    }

643
    if (!best || connection_or_is_better(now, or_conn, best, 0))
644
645
646
647
648
649
650
651
652
653
      best = or_conn;
  }

  if (!best)
    return;

  /* Pass 3: One connection to OR is best.  If it's canonical, mark as bad
   * every other open connection.  If it's non-canonical, mark as bad
   * every other open connection to the same address.
   *
654
655
656
657
658
659
660
661
662
   * XXXX This isn't optimal; if we have connections to an OR at multiple
   *   addresses, we'd like to pick the best _for each address_, and mark as
   *   bad every open connection that isn't best for its address.  But this
   *   can only occur in cases where the other OR is old (so we have no
   *   canonical connection to it), or where all the connections to the OR are
   *   at noncanonical addresses and we have no good direct connection (which
   *   means we aren't at risk of attaching circuits to it anyway).  As
   *   0.1.2.x dies out, the first case will go away, and the second one is
   *   "mostly harmless", so a fix can wait until somebody is bored.
663
664
665
666
667
668
   */
  for (or_conn = head; or_conn; or_conn = or_conn->next_with_same_id) {
    if (or_conn->_base.marked_for_close ||
        or_conn->is_bad_for_new_circs ||
        or_conn->_base.state != OR_CONN_STATE_OPEN)
      continue;
669
670
671
    if (or_conn != best && connection_or_is_better(now, best, or_conn, 1)) {
      /* This isn't the best conn, _and_ the best conn is better than it,
         even when we're being forgiving. */
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
      if (best->is_canonical) {
        log_info(LD_OR,
                 "Marking OR conn to %s:%d as too old for new circuits: "
                 "(fd %d, %d secs old).  We have a better canonical one "
                 "(fd %d; %d secs old).",
                 or_conn->_base.address, or_conn->_base.port, or_conn->_base.s,
                 (int)(now - or_conn->_base.timestamp_created),
                 best->_base.s, (int)(now - best->_base.timestamp_created));
        or_conn->is_bad_for_new_circs = 1;
      } else if (!tor_addr_compare(&or_conn->real_addr,
                                   &best->real_addr, CMP_EXACT)) {
        log_info(LD_OR,
                 "Marking OR conn to %s:%d as too old for new circuits: "
                 "(fd %d, %d secs old).  We have a better one "
                 "(fd %d; %d secs old).",
                 or_conn->_base.address, or_conn->_base.port, or_conn->_base.s,
                 (int)(now - or_conn->_base.timestamp_created),
                 best->_base.s, (int)(now - best->_base.timestamp_created));
        or_conn->is_bad_for_new_circs = 1;
      }
    }
  }
}

/** Go through all the OR connections, and set the is_bad_for_new_circs
 * flag on:
 *    - all connections that are too old.
 *    - all open non-canonical connections for which a canonical connection
 *      exists to the same router.
 *    - all open canonical connections for which a 'better' canonical
 *      connection exists to the same router.
 *    - all open non-canonical connections for which a 'better' non-canonical
 *      connection exists to the same router at the same address.
 *
 * See connection_or_is_better() for our idea of what makes one OR connection
 * better than another.
 */
void
connection_or_set_bad_connections(void)
{
  if (!orconn_identity_map)
    return;

  DIGESTMAP_FOREACH(orconn_identity_map, identity, or_connection_t *, conn) {
    connection_or_group_set_badness(conn);
  } DIGESTMAP_FOREACH_END;
718
719
}

720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
/** <b>conn</b> is in the 'connecting' state, and it failed to complete
 * a TCP connection. Send notifications appropriately.
 *
 * <b>reason</b> specifies the or_conn_end_reason for the failure;
 * <b>msg</b> specifies the strerror-style error message.
 */
void
connection_or_connect_failed(or_connection_t *conn,
                             int reason, const char *msg)
{
  control_event_or_conn_status(conn, OR_CONN_EVENT_FAILED, reason);
  if (!authdir_mode_tests_reachability(get_options()))
    control_event_bootstrap_problem(msg, reason);
}

735
736
/** Launch a new OR connection to <b>addr</b>:<b>port</b> and expect to
 * handshake with an OR with identity digest <b>id_digest</b>.
737
 *
738
 * If <b>id_digest</b> is me, do nothing. If we're already connected to it,
739
740
 * return that connection. If the connect() is in progress, set the
 * new conn's state to 'connecting' and return it. If connect() succeeds,
741
 * call connection_tls_start_handshake() on it.
742
 *
743
 * This function is called from router_retry_connections(), for
744
745
746
747
748
 * ORs connecting to ORs, and circuit_establish_circuit(), for
 * OPs connecting to ORs.
 *
 * Return the launched conn, or NULL if it failed.
 */
749
or_connection_t *
750
751
connection_or_connect(const tor_addr_t *_addr, uint16_t port,
                      const char *id_digest)
752
{
753
  or_connection_t *conn;
754
  or_options_t *options = get_options();
755
  int socket_error = 0;
756
  tor_addr_t addr;
Roger Dingledine's avatar
Roger Dingledine committed
757

758
  tor_assert(_addr);
759
  tor_assert(id_digest);
760
  tor_addr_copy(&addr, _addr);
761

762
  if (server_mode(options) && router_digest_is_me(id_digest)) {
Roger Dingledine's avatar
Roger Dingledine committed
763
    log_info(LD_PROTOCOL,"Client asked me to connect to myself. Refusing.");
764
765
766
    return NULL;
  }

767
  conn = or_connection_new(AF_INET);
Roger Dingledine's avatar
Roger Dingledine committed
768
769

  /* set up conn so it's got all the data we need to remember */
770
  connection_or_init_conn_from_address(conn, &addr, port, id_digest, 1);
771
  conn->_base.state = OR_CONN_STATE_CONNECTING;
772
  control_event_or_conn_status(conn, OR_CONN_EVENT_LAUNCHED, 0);
Roger Dingledine's avatar
Roger Dingledine committed
773

774
775
  if (options->HttpsProxy) {
    /* we shouldn't connect directly. use the https proxy instead. */
776
    tor_addr_from_ipv4h(&addr, options->HttpsProxyAddr);
777
778
779
    port = options->HttpsProxyPort;
  }

780
  switch (connection_connect(TO_CONN(conn), conn->_base.address,
781
                             &addr, port, &socket_error)) {
782
    case -1:
Roger Dingledine's avatar
Roger Dingledine committed
783
784
785
      /* If the connection failed immediately, and we're using
       * an https proxy, our https proxy is down. Don't blame the
       * Tor server. */
786
787
788
      if (!options->HttpsProxy)
        entry_guard_register_connect_status(conn->identity_digest,
                                            0, 1, time(NULL));
789
790
791
      connection_or_connect_failed(conn,
                                   errno_to_orconn_end_reason(socket_error),
                                   tor_socket_strerror(socket_error));
792
      connection_free(TO_CONN(conn));
793
      return NULL;
794
    case 0:
795
      connection_watch_events(TO_CONN(conn), READ_EVENT | WRITE_EVENT);
796
797
      /* writable indicates finish, readable indicates broken link,
         error indicates broken link on windows */
798
      return conn;
799
    /* case 1: fall through */
800
801
  }

802
803
804
805
806
  if (connection_or_finished_connecting(conn) < 0) {
    /* already marked for close */
    return NULL;
  }
  return conn;
807
808
}

Roger Dingledine's avatar
Roger Dingledine committed
809
810
/** Begin the tls handshake with <b>conn</b>. <b>receiving</b> is 0 if
 * we initiated the connection, else it's 1.
811
 *
Roger Dingledine's avatar
Roger Dingledine committed
812
813
 * Assign a new tls object to conn->tls, begin reading on <b>conn</b>, and
 * pass <b>conn</b> to connection_tls_continue_handshake().
814
 *
Roger Dingledine's avatar
Roger Dingledine committed
815
 * Return -1 if <b>conn</b> is broken, else return 0.
816
 */
817
int
818
connection_tls_start_handshake(or_connection_t *conn, int receiving)
819
{
820
  conn->_base.state = OR_CONN_STATE_TLS_HANDSHAKING;
821
  conn->tls = tor_tls_new(conn->_base.s, receiving);
822
  tor_tls_set_logged_address(conn->tls, escaped_safe_str(conn->_base.address));
823
  if (!conn->tls) {
Roger Dingledine's avatar
Roger Dingledine committed
824
    log_warn(LD_BUG,"tor_tls_new failed. Closing.");
825
826
    return -1;
  }
827
828
  connection_start_reading(TO_CONN(conn));
  log_debug(LD_OR,"starting TLS handshake on fd %d", conn->_base.s);
829
830
  note_crypto_pk_op(receiving ? TLS_HANDSHAKE_S : TLS_HANDSHAKE_C);

831
  if (connection_tls_continue_handshake(conn) < 0) {
832
    return -1;
833
  }
834
835
836
  return 0;
}

837
838
/** Invoked on the server side from inside tor_tls_read() when the server
 * gets a successful TLS renegotiation from the client. */
839
840
841
842
static void
connection_or_tls_renegotiated_cb(tor_tls_t *tls, void *_conn)
{
  or_connection_t *conn = _conn;
843
  (void)tls;
844

845
846
847
  /* Don't invoke this again. */
  tor_tls_set_renegotiate_callback(tls, NULL, NULL);

848
  if (connection_tls_finish_handshake(conn) < 0) {
849
    /* XXXX_TLS double-check that it's ok to do this from inside read. */
850
    /* XXXX_TLS double-check that this verifies certificates. */
851
852
    connection_mark_for_close(TO_CONN(conn));
  }
853
854
}

Roger Dingledine's avatar
Roger Dingledine committed
855
856
/** Move forward with the tls handshake. If it finishes, hand
 * <b>conn</b> to connection_tls_finish_handshake().
857
 *
Roger Dingledine's avatar
Roger Dingledine committed
858
 * Return -1 if <b>conn</b> is broken, else return 0.
859
 */
860
int
861
connection_tls_continue_handshake(or_connection_t *conn)
862
{
863
  int result;
864
  check_no_tls_errors();
865
 again:
866
867
  if (conn->_base.state == OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING) {
    // log_notice(LD_OR, "Renegotiate with %p", conn->tls);
868
    result = tor_tls_renegotiate(conn->tls);
869
870
    // log_notice(LD_OR, "Result: %d", result);
  } else {
871
    tor_assert(conn->_base.state == OR_CONN_STATE_TLS_HANDSHAKING);
872
    // log_notice(LD_OR, "Continue handshake with %p", conn->tls);
873
    result = tor_tls_handshake(conn->tls);
874
    // log_notice(LD_OR, "Result: %d", result);
875
  }
876
  switch (result) {
877
    CASE_TOR_TLS_ERROR_ANY:
878
879
    log_info(LD_OR,"tls error [%s]. breaking connection.",
             tor_tls_err_to_string(result));
880
881
      return -1;
    case TOR_TLS_DONE:
882
      if (! tor_tls_used_v1_handshake(conn->tls)) {
883
884
        if (!tor_tls_is_server(conn->tls)) {
          if (conn->_base.state == OR_CONN_STATE_TLS_HANDSHAKING) {
885
            // log_notice(LD_OR,"Done. state was TLS_HANDSHAKING.");
886
            conn->_base.state = OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING;
887
888
            goto again;
          }
889
          // log_notice(LD_OR,"Done. state was %d.", conn->_base.state);
890
891
892
893
894
        } else {
          /* improved handshake, but not a client. */
          tor_tls_set_renegotiate_callback(conn->tls,
                                           connection_or_tls_renegotiated_cb,
                                           conn);
895
896
897
898
          conn->_base.state = OR_CONN_STATE_TLS_SERVER_RENEGOTIATING;
          connection_stop_writing(TO_CONN(conn));
          connection_start_reading(TO_CONN(conn));
          return 0;
899
        }
900
      }
901
      return connection_tls_finish_handshake(conn);
902
    case TOR_TLS_WANTWRITE:
903
      connection_start_writing(TO_CONN(conn));
Roger Dingledine's avatar
Roger Dingledine committed
904
      log_debug(LD_OR,"wanted write");
905
906
      return 0;
    case TOR_TLS_WANTREAD: /* handshaking conns are *always* reading */
Roger Dingledine's avatar
Roger Dingledine committed
907
      log_debug(LD_OR,"wanted read");
908
      return 0;
909
910
911
    case TOR_TLS_CLOSE:
      log_info(LD_OR,"tls closed. breaking connection.");
      return -1;
912
913
914
915
  }
  return 0;
}

916
917
918
/** Return 1 if we initiated this connection, or 0 if it started
 * out as an incoming connection.
 */
919
int
920
connection_or_nonopen_was_started_here(or_connection_t *conn)
921
{
922
  tor_assert(conn->_base.type == CONN_TYPE_OR);
923
924
  if (!conn->tls)
    return 1; /* it's still in proxy states or something */
925
926
  if (conn->handshake_state)
    return conn->handshake_state->started_here;
927
  return !tor_tls_is_server(conn->tls);
928
929
}

930
/** <b>Conn</b> just completed its handshake. Return 0 if all is well, and
931
 * return -1 if he is lying, broken, or otherwise something is wrong.
932
 *
933
934
935
 * If we initiated this connection (<b>started_here</b> is true), make sure
 * the other side sent sent a correctly formed certificate. If I initiated the
 * connection, make sure it's the right guy.
936
 *
937
938
939
940
941
942
943
944
 * Otherwise (if we _didn't_ initiate this connection), it's okay for
 * the certificate to be weird or absent.
 *
 * If we return 0, and the certificate is as expected, write a hash of the
 * identity key into digest_rcvd, which must have DIGEST_LEN space in it. (If
 * we return -1 this buffer is undefined.)  If the certificate is invalid
 * or missing on an incoming connection, we return 0 and set digest_rcvd to
 * DIGEST_LEN 0 bytes.
945
 *
946
 * As side effects,
Roger Dingledine's avatar
Roger Dingledine committed
947
 * 1) Set conn->circ_id_type according to tor-spec.txt.
948
949
950
 * 2) If we're an authdirserver and we initiated the connection: drop all
 *    descriptors that claim to be on that IP/port but that aren't
 *    this guy; and note that this guy is reachable.
951
 */
952
static int
953
954
955
connection_or_check_valid_tls_handshake(or_connection_t *conn,
                                        int started_here,
                                        char *digest_rcvd_out)
956
{
957
  crypto_pk_env_t *identity_rcvd=NULL;
958
  or_options_t *options = get_options();
Roger Dingledine's avatar
Roger Dingledine committed
959
  int severity = server_mode(options) ? LOG_PROTOCOL_WARN : LOG_WARN;
960
961
  const char *safe_address =
    started_here ? conn->_base.address : safe_str(conn->_base.address);
962
  const char *conn_type = started_here ? "outgoing" : "incoming";
963
  int has_cert = 0, has_identity=0;
964

965
  check_no_tls_errors();
966
967
968
969
970
  has_cert = tor_tls_peer_has_cert(conn->tls);
  if (started_here && !has_cert) {
    log_info(LD_PROTOCOL,"Tried connecting to router at %s:%d, but it didn't "
             "send a cert! Closing.",
             safe_address, conn->_base.port);
971
    return -1;
972
973
974
  } else if (!has_cert) {
    log_debug(LD_PROTOCOL,"Got incoming connection with no certificate. "
              "That's ok.");
975
  }
976
  check_no_tls_errors();
977

978
  if (has_cert) {
979
980
    int v = tor_tls_verify(started_here?severity:LOG_INFO,
                           conn->tls, &identity_rcvd);
981
982
983
984
    if (started_here && v<0) {
      log_fn(severity,LD_OR,"Tried connecting to router at %s:%d: It"
             " has a cert but it's invalid. Closing.",
             safe_address, conn->_base.port);
985
        return -1;
986
987
988
989
990
991
    } else if (v<0) {
      log_info(LD_PROTOCOL,"Incoming connection gave us an invalid cert "
               "chain; ignoring.");
    } else {
      log_debug(LD_OR,"The certificate seems to be valid on %s connection "
                "with %s:%d", conn_type, safe_address, conn->_base.port);
992
    }
993
    check_no_tls_errors();
994
  }
995

996
  if (identity_rcvd) {
997
    has_identity = 1;
998
    crypto_pk_get_digest(identity_rcvd, digest_rcvd_out);
999
1000
    if (crypto_pk_cmp_keys(get_identity_key(), identity_rcvd)<0) {
      conn->circ_id_type = CIRC_ID_TYPE_LOWER;