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

7
8
/**
 * \file connection.c
9
10
 * \brief General high-level functions to handle reading and writing
 * on connections.
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 *
 * Each connection (ideally) represents a TLS connection, a TCP socket, a unix
 * socket, or a UDP socket on which reads and writes can occur.  (But see
 * connection_edge.c for cases where connections can also represent streams
 * that do not have a corresponding socket.)
 *
 * The module implements the abstract type, connection_t.  The subtypes are:
 *  <ul>
 *   <li>listener_connection_t, implemented here in connection.c
 *   <li>dir_connection_t, implemented in directory.c
 *   <li>or_connection_t, implemented in connection_or.c
 *   <li>edge_connection_t, implemented in connection_edge.c, along with
 *      its subtype(s):
 *      <ul><li>entry_connection_t, also implemented in connection_edge.c
 *      </ul>
 *   <li>control_connection_t, implemented in control.c
 *  </ul>
 *
 * The base type implemented in this module is responsible for basic
 * rate limiting, flow control, and marshalling bytes onto and off of the
 * network (either directly or via TLS).
 *
 * Connections are registered with the main loop with connection_add(). As
 * they become able to read or write register the fact with the event main
 * loop by calling connection_watch_events(), connection_start_reading(), or
 * connection_start_writing().  When they no longer want to read or write,
Chelsea H. Komlo's avatar
Chelsea H. Komlo committed
37
 * they call connection_stop_reading() or connection_stop_writing().
38
39
 *
 * To queue data to be written on a connection, call
40
 * connection_buf_add().  When data arrives, the
41
42
43
44
45
46
47
48
49
50
51
52
53
54
 * connection_process_inbuf() callback is invoked, which dispatches to a
 * type-specific function (such as connection_edge_process_inbuf() for
 * example). Connection types that need notice of when data has been written
 * receive notification via connection_flushed_some() and
 * connection_finished_flushing().  These functions all delegate to
 * type-specific implementations.
 *
 * Additionally, beyond the core of connection_t, this module also implements:
 * <ul>
 * <li>Listeners, which wait for incoming sockets and launch connections
 * <li>Outgoing SOCKS proxy support
 * <li>Outgoing HTTP proxy support
 * <li>An out-of-sockets handler for dealing with socket exhaustion
 * </ul>
55
56
 **/

Nick Mathewson's avatar
Nick Mathewson committed
57
#define CONNECTION_PRIVATE
Roger Dingledine's avatar
Roger Dingledine committed
58
#include "or.h"
59
#include "bridges.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
60
#include "buffers.h"
61
#include "buffers_tls.h"
62
63
64
65
/*
 * Define this so we get channel internal functions, since we're implementing
 * part of a subclass (channel_tls_t).
 */
66
#define TOR_CHANNEL_INTERNAL_
67
#define CONNECTION_PRIVATE
68
#include "backtrace.h"
69
70
#include "channel.h"
#include "channeltls.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
71
#include "circuitbuild.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
72
#include "circuitlist.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
73
#include "circuituse.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
74
#include "config.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
75
#include "connection.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
76
#include "connection_edge.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
77
#include "connection_or.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
78
#include "control.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
79
#include "directory.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
80
#include "dirserv.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
81
#include "dns.h"
82
#include "dnsserv.h"
83
#include "entrynodes.h"
84
#include "ext_orport.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
85
#include "geoip.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
86
#include "main.h"
87
#include "hs_common.h"
88
#include "hs_ident.h"
89
#include "nodelist.h"
90
91
#include "proto_http.h"
#include "proto_socks.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
92
#include "policies.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
93
#include "reasons.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
94
#include "relay.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
95
#include "rendclient.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
96
#include "rendcommon.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
97
#include "rephist.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
98
#include "router.h"
99
#include "routerlist.h"
George Kadianakis's avatar
George Kadianakis committed
100
#include "transports.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
101
#include "routerparse.h"
102
#include "sandbox.h"
103
#include "transports.h"
Roger Dingledine's avatar
Roger Dingledine committed
104

105
106
107
108
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif

109
110
111
112
113
#ifdef HAVE_SYS_UN_H
#include <sys/socket.h>
#include <sys/un.h>
#endif

114
static connection_t *connection_listener_new(
115
                               const struct sockaddr *listensockaddr,
116
                               socklen_t listensocklen, int type,
117
118
                               const char *address,
                               const port_cfg_t *portcfg);
119
120
static void connection_init(time_t now, connection_t *conn, int type,
                            int socket_family);
Nick Mathewson's avatar
Nick Mathewson committed
121
static int connection_handle_listener_read(connection_t *conn, int new_type);
122
123
static int connection_bucket_should_increase(int bucket,
                                             or_connection_t *conn);
124
static int connection_finished_flushing(connection_t *conn);
125
static int connection_flushed_some(connection_t *conn);
126
static int connection_finished_connecting(connection_t *conn);
127
static int connection_reached_eof(connection_t *conn);
128
129
130
static int connection_buf_read_from_socket(connection_t *conn,
                                           ssize_t *max_to_read,
                                           int *socket_error);
131
static int connection_process_inbuf(connection_t *conn, int package_partial);
132
133
static void client_check_address_changed(tor_socket_t sock);
static void set_constrained_socket_buffers(tor_socket_t sock, int size);
134

135
136
137
static const char *connection_proxy_state_to_string(int state);
static int connection_read_https_proxy_response(connection_t *conn);
static void connection_send_socks5_connect(connection_t *conn);
138
static const char *proxy_type_to_string(int proxy_type);
139
static int get_proxy_type(void);
140
141
const tor_addr_t *conn_get_outbound_address(sa_family_t family,
                  const or_options_t *options, unsigned int conn_type);
142

143
144
145
/** The last addresses that our network interface seemed to have been
 * binding to.  We use this as one way to detect when our IP changes.
 *
146
 * XXXX+ We should really use the entire list of interfaces here.
147
148
 **/
static tor_addr_t *last_interface_ipv4 = NULL;
149
/* DOCDOC last_interface_ipv6 */
150
151
static tor_addr_t *last_interface_ipv6 = NULL;
/** A list of tor_addr_t for addresses we've used in outgoing connections.
152
 * Used to detect IP address changes. */
153
static smartlist_t *outgoing_addrs = NULL;
154

155
156
#define CASE_ANY_LISTENER_TYPE \
    case CONN_TYPE_OR_LISTENER: \
157
    case CONN_TYPE_EXT_OR_LISTENER: \
158
159
160
161
162
    case CONN_TYPE_AP_LISTENER: \
    case CONN_TYPE_DIR_LISTENER: \
    case CONN_TYPE_CONTROL_LISTENER: \
    case CONN_TYPE_AP_TRANS_LISTENER: \
    case CONN_TYPE_AP_NATD_LISTENER: \
163
164
    case CONN_TYPE_AP_DNS_LISTENER: \
    case CONN_TYPE_AP_HTTP_CONNECT_LISTENER
165

166
167
/**************************************************************/

168
169
170
/**
 * Return the human-readable name for the connection type <b>type</b>
 */
171
172
173
174
175
176
177
178
const char *
conn_type_to_string(int type)
{
  static char buf[64];
  switch (type) {
    case CONN_TYPE_OR_LISTENER: return "OR listener";
    case CONN_TYPE_OR: return "OR";
    case CONN_TYPE_EXIT: return "Exit";
179
    case CONN_TYPE_AP_LISTENER: return "Socks listener";
180
181
182
    case CONN_TYPE_AP_TRANS_LISTENER:
      return "Transparent pf/netfilter listener";
    case CONN_TYPE_AP_NATD_LISTENER: return "Transparent natd listener";
183
    case CONN_TYPE_AP_DNS_LISTENER: return "DNS listener";
184
185
186
    case CONN_TYPE_AP: return "Socks";
    case CONN_TYPE_DIR_LISTENER: return "Directory listener";
    case CONN_TYPE_DIR: return "Directory";
187
188
    case CONN_TYPE_CONTROL_LISTENER: return "Control listener";
    case CONN_TYPE_CONTROL: return "Control";
189
190
    case CONN_TYPE_EXT_OR: return "Extended OR";
    case CONN_TYPE_EXT_OR_LISTENER: return "Extended OR listener";
191
    case CONN_TYPE_AP_HTTP_CONNECT_LISTENER: return "HTTP tunnel listener";
192
    default:
193
      log_warn(LD_BUG, "unknown connection type %d", type);
194
195
196
197
198
      tor_snprintf(buf, sizeof(buf), "unknown [%d]", type);
      return buf;
  }
}

199
200
201
202
/**
 * Return the human-readable name for the connection state <b>state</b>
 * for the connection type <b>type</b>
 */
203
const char *
204
205
conn_state_to_string(int type, int state)
{
206
207
  static char buf[96];
  switch (type) {
208
    CASE_ANY_LISTENER_TYPE:
209
210
211
212
213
214
      if (state == LISTENER_STATE_READY)
        return "ready";
      break;
    case CONN_TYPE_OR:
      switch (state) {
        case OR_CONN_STATE_CONNECTING: return "connect()ing";
215
        case OR_CONN_STATE_PROXY_HANDSHAKING: return "handshaking (proxy)";
216
        case OR_CONN_STATE_TLS_HANDSHAKING: return "handshaking (TLS)";
217
        case OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING:
218
          return "renegotiating (TLS, v2 handshake)";
219
        case OR_CONN_STATE_TLS_SERVER_RENEGOTIATING:
220
221
222
223
224
          return "waiting for renegotiation or V3 handshake";
        case OR_CONN_STATE_OR_HANDSHAKING_V2:
          return "handshaking (Tor, v2 handshake)";
        case OR_CONN_STATE_OR_HANDSHAKING_V3:
          return "handshaking (Tor, v3 handshake)";
225
226
227
        case OR_CONN_STATE_OPEN: return "open";
      }
      break;
228
229
    case CONN_TYPE_EXT_OR:
      switch (state) {
230
231
232
233
234
235
        case EXT_OR_CONN_STATE_AUTH_WAIT_AUTH_TYPE:
          return "waiting for authentication type";
        case EXT_OR_CONN_STATE_AUTH_WAIT_CLIENT_NONCE:
          return "waiting for client nonce";
        case EXT_OR_CONN_STATE_AUTH_WAIT_CLIENT_HASH:
          return "waiting for client hash";
236
237
238
239
        case EXT_OR_CONN_STATE_OPEN: return "open";
        case EXT_OR_CONN_STATE_FLUSHING: return "flushing final OKAY";
      }
      break;
240
241
242
243
244
245
246
247
248
249
    case CONN_TYPE_EXIT:
      switch (state) {
        case EXIT_CONN_STATE_RESOLVING: return "waiting for dest info";
        case EXIT_CONN_STATE_CONNECTING: return "connecting";
        case EXIT_CONN_STATE_OPEN: return "open";
        case EXIT_CONN_STATE_RESOLVEFAILED: return "resolve failed";
      }
      break;
    case CONN_TYPE_AP:
      switch (state) {
250
        case AP_CONN_STATE_SOCKS_WAIT: return "waiting for socks info";
251
        case AP_CONN_STATE_NATD_WAIT: return "waiting for natd dest info";
252
253
        case AP_CONN_STATE_RENDDESC_WAIT: return "waiting for rendezvous desc";
        case AP_CONN_STATE_CONTROLLER_WAIT: return "waiting for controller";
254
255
256
        case AP_CONN_STATE_CIRCUIT_WAIT: return "waiting for circuit";
        case AP_CONN_STATE_CONNECT_WAIT: return "waiting for connect response";
        case AP_CONN_STATE_RESOLVE_WAIT: return "waiting for resolve response";
257
258
259
260
261
262
263
        case AP_CONN_STATE_OPEN: return "open";
      }
      break;
    case CONN_TYPE_DIR:
      switch (state) {
        case DIR_CONN_STATE_CONNECTING: return "connecting";
        case DIR_CONN_STATE_CLIENT_SENDING: return "client sending";
Roger Dingledine's avatar
Roger Dingledine committed
264
        case DIR_CONN_STATE_CLIENT_READING: return "client reading";
265
        case DIR_CONN_STATE_CLIENT_FINISHED: return "client finished";
266
267
268
269
270
271
        case DIR_CONN_STATE_SERVER_COMMAND_WAIT: return "waiting for command";
        case DIR_CONN_STATE_SERVER_WRITING: return "writing";
      }
      break;
    case CONN_TYPE_CONTROL:
      switch (state) {
272
273
        case CONTROL_CONN_STATE_OPEN: return "open (protocol v1)";
        case CONTROL_CONN_STATE_NEEDAUTH:
274
          return "waiting for authentication (protocol v1)";
275
276
277
278
      }
      break;
  }

279
  log_warn(LD_BUG, "unknown connection state %d (type %d)", state, type);
280
281
282
283
284
285
  tor_snprintf(buf, sizeof(buf),
               "unknown state [%d] on unknown [%s] connection",
               state, conn_type_to_string(type));
  return buf;
}

286
287
/** Allocate and return a new dir_connection_t, initialized as by
 * connection_init(). */
288
289
290
291
292
293
294
dir_connection_t *
dir_connection_new(int socket_family)
{
  dir_connection_t *dir_conn = tor_malloc_zero(sizeof(dir_connection_t));
  connection_init(time(NULL), TO_CONN(dir_conn), CONN_TYPE_DIR, socket_family);
  return dir_conn;
}
295
296

/** Allocate and return a new or_connection_t, initialized as by
Nick Mathewson's avatar
Nick Mathewson committed
297
 * connection_init().
298
299
300
301
302
 *
 * Initialize active_circuit_pqueue.
 *
 * Set active_circuit_pqueue_last_recalibrated to current cell_ewma tick.
 */
303
or_connection_t *
304
or_connection_new(int type, int socket_family)
305
306
307
{
  or_connection_t *or_conn = tor_malloc_zero(sizeof(or_connection_t));
  time_t now = time(NULL);
308
309
  tor_assert(type == CONN_TYPE_OR || type == CONN_TYPE_EXT_OR);
  connection_init(now, TO_CONN(or_conn), type, socket_family);
310

311
  connection_or_set_canonical(or_conn, 0);
312

313
314
315
  if (type == CONN_TYPE_EXT_OR)
    connection_or_set_ext_or_identifier(or_conn);

316
317
  return or_conn;
}
318

319
/** Allocate and return a new entry_connection_t, initialized as by
320
321
322
323
 * connection_init().
 *
 * Allocate space to store the socks_request.
 */
324
325
326
327
328
329
entry_connection_t *
entry_connection_new(int type, int socket_family)
{
  entry_connection_t *entry_conn = tor_malloc_zero(sizeof(entry_connection_t));
  tor_assert(type == CONN_TYPE_AP);
  connection_init(time(NULL), ENTRY_TO_CONN(entry_conn), type, socket_family);
330
  entry_conn->socks_request = socks_request_new();
331
332
333
334
  /* If this is coming from a listener, we'll set it up based on the listener
   * in a little while.  Otherwise, we're doing this as a linked connection
   * of some kind, and we should set it up here based on the socket family */
  if (socket_family == AF_INET)
335
    entry_conn->entry_cfg.ipv4_traffic = 1;
336
  else if (socket_family == AF_INET6)
337
    entry_conn->entry_cfg.ipv6_traffic = 1;
338
339
  else if (socket_family == AF_UNIX)
    entry_conn->is_socks_socket = 1;
340
341
342
  return entry_conn;
}

343
344
/** Allocate and return a new edge_connection_t, initialized as by
 * connection_init(). */
345
346
347
348
edge_connection_t *
edge_connection_new(int type, int socket_family)
{
  edge_connection_t *edge_conn = tor_malloc_zero(sizeof(edge_connection_t));
349
  tor_assert(type == CONN_TYPE_EXIT);
350
351
352
  connection_init(time(NULL), TO_CONN(edge_conn), type, socket_family);
  return edge_conn;
}
353
354
355

/** Allocate and return a new control_connection_t, initialized as by
 * connection_init(). */
356
357
358
359
360
361
362
363
364
365
control_connection_t *
control_connection_new(int socket_family)
{
  control_connection_t *control_conn =
    tor_malloc_zero(sizeof(control_connection_t));
  connection_init(time(NULL),
                  TO_CONN(control_conn), CONN_TYPE_CONTROL, socket_family);
  return control_conn;
}

366
367
368
369
370
371
372
373
374
375
376
/** Allocate and return a new listener_connection_t, initialized as by
 * connection_init(). */
listener_connection_t *
listener_connection_new(int type, int socket_family)
{
  listener_connection_t *listener_conn =
    tor_malloc_zero(sizeof(listener_connection_t));
  connection_init(time(NULL), TO_CONN(listener_conn), type, socket_family);
  return listener_conn;
}

377
378
379
/** Allocate, initialize, and return a new connection_t subtype of <b>type</b>
 * to make or receive connections of address family <b>socket_family</b>.  The
 * type should be one of the CONN_TYPE_* constants. */
380
381
382
383
384
connection_t *
connection_new(int type, int socket_family)
{
  switch (type) {
    case CONN_TYPE_OR:
385
386
    case CONN_TYPE_EXT_OR:
      return TO_CONN(or_connection_new(type, socket_family));
387
388
389
390

    case CONN_TYPE_EXIT:
      return TO_CONN(edge_connection_new(type, socket_family));

391
392
393
    case CONN_TYPE_AP:
      return ENTRY_TO_CONN(entry_connection_new(type, socket_family));

394
395
396
397
398
399
    case CONN_TYPE_DIR:
      return TO_CONN(dir_connection_new(socket_family));

    case CONN_TYPE_CONTROL:
      return TO_CONN(control_connection_new(socket_family));

400
401
402
    CASE_ANY_LISTENER_TYPE:
      return TO_CONN(listener_connection_new(type, socket_family));

403
404
405
406
407
408
409
410
411
412
    default: {
      connection_t *conn = tor_malloc_zero(sizeof(connection_t));
      connection_init(time(NULL), conn, type, socket_family);
      return conn;
    }
  }
}

/** Initializes conn. (you must call connection_add() to link it into the main
 * array).
413
 *
414
415
 * Set conn-\>magic to the correct value.
 *
416
 * Set conn-\>type to <b>type</b>. Set conn-\>s and conn-\>conn_array_index to
417
418
419
420
 * -1 to signify they are not yet assigned.
 *
 * Initialize conn's timestamps to now.
 */
421
422
static void
connection_init(time_t now, connection_t *conn, int type, int socket_family)
423
{
424
425
  static uint64_t n_connections_allocated = 1;

426
427
  switch (type) {
    case CONN_TYPE_OR:
428
    case CONN_TYPE_EXT_OR:
429
      conn->magic = OR_CONNECTION_MAGIC;
430
431
      break;
    case CONN_TYPE_EXIT:
432
      conn->magic = EDGE_CONNECTION_MAGIC;
433
434
      break;
    case CONN_TYPE_AP:
435
      conn->magic = ENTRY_CONNECTION_MAGIC;
436
437
      break;
    case CONN_TYPE_DIR:
438
      conn->magic = DIR_CONNECTION_MAGIC;
439
440
      break;
    case CONN_TYPE_CONTROL:
441
      conn->magic = CONTROL_CONNECTION_MAGIC;
442
      break;
443
444
    CASE_ANY_LISTENER_TYPE:
      conn->magic = LISTENER_CONNECTION_MAGIC;
445
      break;
446
    default:
447
      conn->magic = BASE_CONNECTION_MAGIC;
448
449
450
      break;
  }

451
  conn->s = TOR_INVALID_SOCKET; /* give it a default of 'not used' */
452
  conn->conn_array_index = -1; /* also default to 'not used' */
453
  conn->global_identifier = n_connections_allocated++;
Roger Dingledine's avatar
Roger Dingledine committed
454
455

  conn->type = type;
456
  conn->socket_family = socket_family;
457
458
  if (!connection_is_listener(conn)) {
    /* listeners never use their buf */
459
460
461
    conn->inbuf = buf_new();
    conn->outbuf = buf_new();
  }
462

463
464
465
  conn->timestamp_created = now;
  conn->timestamp_lastread = now;
  conn->timestamp_lastwritten = now;
Roger Dingledine's avatar
Roger Dingledine committed
466
467
}

Roger Dingledine's avatar
Roger Dingledine committed
468
/** Create a link between <b>conn_a</b> and <b>conn_b</b>. */
469
470
471
void
connection_link_connections(connection_t *conn_a, connection_t *conn_b)
{
472
473
  tor_assert(! SOCKET_OK(conn_a->s));
  tor_assert(! SOCKET_OK(conn_b->s));
474
475
476
477
478
479
480

  conn_a->linked = 1;
  conn_b->linked = 1;
  conn_a->linked_conn = conn_b;
  conn_b->linked_conn = conn_a;
}

481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/** Return true iff the provided connection listener type supports AF_UNIX
 * sockets. */
int
conn_listener_type_supports_af_unix(int type)
{
  /* For now only control ports or SOCKS ports can be Unix domain sockets
   * and listeners at the same time */
  switch (type) {
    case CONN_TYPE_CONTROL_LISTENER:
    case CONN_TYPE_AP_LISTENER:
      return 1;
    default:
      return 0;
  }
}

497
498
499
/** Deallocate memory used by <b>conn</b>. Deallocate its buffers if
 * necessary, close its socket if necessary, and mark the directory as dirty
 * if <b>conn</b> is an OR or OP connection.
500
 */
Nick Mathewson's avatar
Nick Mathewson committed
501
STATIC void
502
connection_free_minimal(connection_t *conn)
503
{
504
  void *mem;
505
  size_t memlen;
506
507
508
  if (!conn)
    return;

509
510
  switch (conn->type) {
    case CONN_TYPE_OR:
511
    case CONN_TYPE_EXT_OR:
512
513
      tor_assert(conn->magic == OR_CONNECTION_MAGIC);
      mem = TO_OR_CONN(conn);
514
      memlen = sizeof(or_connection_t);
515
516
      break;
    case CONN_TYPE_AP:
517
518
519
520
      tor_assert(conn->magic == ENTRY_CONNECTION_MAGIC);
      mem = TO_ENTRY_CONN(conn);
      memlen = sizeof(entry_connection_t);
      break;
521
522
523
    case CONN_TYPE_EXIT:
      tor_assert(conn->magic == EDGE_CONNECTION_MAGIC);
      mem = TO_EDGE_CONN(conn);
524
      memlen = sizeof(edge_connection_t);
525
526
527
528
      break;
    case CONN_TYPE_DIR:
      tor_assert(conn->magic == DIR_CONNECTION_MAGIC);
      mem = TO_DIR_CONN(conn);
529
      memlen = sizeof(dir_connection_t);
530
531
532
533
      break;
    case CONN_TYPE_CONTROL:
      tor_assert(conn->magic == CONTROL_CONNECTION_MAGIC);
      mem = TO_CONTROL_CONN(conn);
534
      memlen = sizeof(control_connection_t);
535
      break;
536
537
538
539
540
    CASE_ANY_LISTENER_TYPE:
      tor_assert(conn->magic == LISTENER_CONNECTION_MAGIC);
      mem = TO_LISTENER_CONN(conn);
      memlen = sizeof(listener_connection_t);
      break;
541
542
543
    default:
      tor_assert(conn->magic == BASE_CONNECTION_MAGIC);
      mem = conn;
544
      memlen = sizeof(connection_t);
545
546
      break;
  }
Roger Dingledine's avatar
Roger Dingledine committed
547

548
  if (conn->linked) {
549
550
551
552
    log_info(LD_GENERAL, "Freeing linked %s connection [%s] with %d "
             "bytes on inbuf, %d on outbuf.",
             conn_type_to_string(conn->type),
             conn_state_to_string(conn->type, conn->state),
553
554
             (int)connection_get_inbuf_len(conn),
             (int)connection_get_outbuf_len(conn));
555
556
  }

557
  if (!connection_is_listener(conn)) {
558
559
    buf_free(conn->inbuf);
    buf_free(conn->outbuf);
560
  } else {
561
    if (conn->socket_family == AF_UNIX) {
562
      /* For now only control and SOCKS ports can be Unix domain sockets
563
       * and listeners at the same time */
564
      tor_assert(conn_listener_type_supports_af_unix(conn->type));
565
566
567
568
569
570

      if (unlink(conn->address) < 0 && errno != ENOENT) {
        log_warn(LD_NET, "Could not unlink %s: %s", conn->address,
                         strerror(errno));
      }
    }
571
  }
572

573
  tor_free(conn->address);
Roger Dingledine's avatar
Roger Dingledine committed
574

575
  if (connection_speaks_cells(conn)) {
576
    or_connection_t *or_conn = TO_OR_CONN(conn);
577
578
579
580
    tor_tls_free(or_conn->tls);
    or_conn->tls = NULL;
    or_handshake_state_free(or_conn->handshake_state);
    or_conn->handshake_state = NULL;
581
    tor_free(or_conn->nickname);
582
583
584
585
586
587
588
589
    if (or_conn->chan) {
      /* Owww, this shouldn't happen, but... */
      log_info(LD_CHANNEL,
               "Freeing orconn at %p, saw channel %p with ID "
               U64_FORMAT " left un-NULLed",
               or_conn, TLS_CHAN_TO_BASE(or_conn->chan),
               U64_PRINTF_ARG(
                 TLS_CHAN_TO_BASE(or_conn->chan)->global_identifier));
590
      if (!CHANNEL_FINISHED(TLS_CHAN_TO_BASE(or_conn->chan))) {
591
592
593
594
595
596
        channel_close_for_error(TLS_CHAN_TO_BASE(or_conn->chan));
      }

      or_conn->chan->conn = NULL;
      or_conn->chan = NULL;
    }
597
  }
598
599
600
601
602
603
604
  if (conn->type == CONN_TYPE_AP) {
    entry_connection_t *entry_conn = TO_ENTRY_CONN(conn);
    tor_free(entry_conn->chosen_exit_name);
    tor_free(entry_conn->original_dest_address);
    if (entry_conn->socks_request)
      socks_request_free(entry_conn->socks_request);
    if (entry_conn->pending_optimistic_data) {
605
      buf_free(entry_conn->pending_optimistic_data);
606
    }
607
    if (entry_conn->sending_optimistic_data) {
608
      buf_free(entry_conn->sending_optimistic_data);
609
    }
610
611
612
  }
  if (CONN_IS_EDGE(conn)) {
    rend_data_free(TO_EDGE_CONN(conn)->rend_data);
613
    hs_ident_edge_conn_free(TO_EDGE_CONN(conn)->hs_ident);
614
615
616
  }
  if (conn->type == CONN_TYPE_CONTROL) {
    control_connection_t *control_conn = TO_CONTROL_CONN(conn);
617
    tor_free(control_conn->safecookie_client_hash);
618
    tor_free(control_conn->incoming_cmd);
619
620
621
622
623
624
625
    if (control_conn->ephemeral_onion_services) {
      SMARTLIST_FOREACH(control_conn->ephemeral_onion_services, char *, cp, {
        memwipe(cp, 0, strlen(cp));
        tor_free(cp);
      });
      smartlist_free(control_conn->ephemeral_onion_services);
    }
Roger Dingledine's avatar
Roger Dingledine committed
626
627
  }

628
629
630
631
  /* Probably already freed by connection_free. */
  tor_event_free(conn->read_event);
  tor_event_free(conn->write_event);
  conn->read_event = conn->write_event = NULL;
632
633
634
635

  if (conn->type == CONN_TYPE_DIR) {
    dir_connection_t *dir_conn = TO_DIR_CONN(conn);
    tor_free(dir_conn->requested_resource);
636

637
    tor_compress_free(dir_conn->compress_state);
638
639
640
641
    if (dir_conn->spool) {
      SMARTLIST_FOREACH(dir_conn->spool, spooled_resource_t *, spooled,
                        spooled_resource_free(spooled));
      smartlist_free(dir_conn->spool);
642
    }
643
644

    rend_data_free(dir_conn->rend_data);
645
    hs_ident_dir_conn_free(dir_conn->hs_ident);
646
647
    if (dir_conn->guard_state) {
      /* Cancel before freeing, if it's still there. */
648
      entry_guard_cancel(&dir_conn->guard_state);
649
    }
650
    circuit_guard_state_free(dir_conn->guard_state);
651
  }
652

653
654
  if (SOCKET_OK(conn->s)) {
    log_debug(LD_NET,"closing fd %d.",(int)conn->s);
655
    tor_close_socket(conn->s);
656
    conn->s = TOR_INVALID_SOCKET;
657
658
  }

659
  if (conn->type == CONN_TYPE_OR &&
660
      !tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest)) {
Roger Dingledine's avatar
Roger Dingledine committed
661
    log_warn(LD_BUG, "called on OR conn with non-zeroed identity_digest");
662
    connection_or_clear_identity(TO_OR_CONN(conn));
663
  }
664
  if (conn->type == CONN_TYPE_OR || conn->type == CONN_TYPE_EXT_OR) {
665
    connection_or_remove_from_ext_or_id_map(TO_OR_CONN(conn));
666
    tor_free(TO_OR_CONN(conn)->ext_or_conn_id);
667
    tor_free(TO_OR_CONN(conn)->ext_or_auth_correct_client_hash);
668
    tor_free(TO_OR_CONN(conn)->ext_or_transport);
669
  }
670

671
  memwipe(mem, 0xCC, memlen); /* poison memory */
672
  tor_free(mem);
Roger Dingledine's avatar
Roger Dingledine committed
673
674
}

675
676
/** Make sure <b>conn</b> isn't in any of the global conn lists; then free it.
 */
677
MOCK_IMPL(void,
678
connection_free_,(connection_t *conn))
679
{
680
681
  if (!conn)
    return;
682
683
  tor_assert(!connection_is_on_closeable_list(conn));
  tor_assert(!connection_in_array(conn));
684
  if (BUG(conn->linked_conn)) {
685
686
687
688
689
690
    conn->linked_conn->linked_conn = NULL;
    if (! conn->linked_conn->marked_for_close &&
        conn->linked_conn->reading_from_linked_conn)
      connection_start_reading(conn->linked_conn);
    conn->linked_conn = NULL;
  }
691
  if (connection_speaks_cells(conn)) {
692
    if (!tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest)) {
693
      connection_or_clear_identity(TO_OR_CONN(conn));
694
    }
695
  }
696
  if (conn->type == CONN_TYPE_CONTROL) {
697
    connection_control_closed(TO_CONTROL_CONN(conn));
698
  }
699
700
701
702
703
704
#if 1
  /* DEBUGGING */
  if (conn->type == CONN_TYPE_AP) {
    connection_ap_warn_and_unmark_if_pending_circ(TO_ENTRY_CONN(conn),
                                                  "connection_free");
  }
705
#endif /* 1 */
706
  connection_unregister_events(conn);
707
  connection_free_minimal(conn);
708
709
}

710
711
712
/**
 * Called when we're about to finally unlink and free a connection:
 * perform necessary accounting and cleanup
713
714
715
716
717
718
719
 *   - Directory conns that failed to fetch a rendezvous descriptor
 *     need to inform pending rendezvous streams.
 *   - OR conns need to call rep_hist_note_*() to record status.
 *   - AP conns need to send a socks reject if necessary.
 *   - Exit conns need to call connection_dns_remove() if necessary.
 *   - AP and Exit conns need to send an end cell if they can.
 *   - DNS conns need to fail any resolves that are pending on them.
720
 *   - OR and edge connections need to be unlinked from circuits.
721
 */
722
723
void
connection_about_to_close_connection(connection_t *conn)
724
{
725
  tor_assert(conn->marked_for_close);
726

727
  switch (conn->type) {
728
    case CONN_TYPE_DIR:
729
      connection_dir_about_to_close(TO_DIR_CONN(conn));
730
      break;
731
    case CONN_TYPE_OR:
732
    case CONN_TYPE_EXT_OR:
733
      connection_or_about_to_close(TO_OR_CONN(conn));
734
      break;
735
    case CONN_TYPE_AP:
736
      connection_ap_about_to_close(TO_ENTRY_CONN(conn));
737
      break;
738
    case CONN_TYPE_EXIT:
739
      connection_exit_about_to_close(TO_EDGE_CONN(conn));
740
      break;
741
  }
742
743
}

Roger Dingledine's avatar
Roger Dingledine committed
744
745
/** Return true iff connection_close_immediate() has been called on this
 * connection. */
746
#define CONN_IS_CLOSED(c) \
747
  ((c)->linked ? ((c)->linked_conn_is_closed) : (! SOCKET_OK(c->s)))
748

749
750
751
/** Close the underlying socket for <b>conn</b>, so we don't try to
 * flush it. Must be used in conjunction with (right before)
 * connection_mark_for_close().
752
 */
753
754
void
connection_close_immediate(connection_t *conn)
755
756
{
  assert_connection_ok(conn,0);
757
  if (CONN_IS_CLOSED(conn)) {
758
    log_err(LD_BUG,"Attempt to close already-closed connection.");
759
    tor_fragile_assert();
760
761
    return;
  }
762
  if (conn->outbuf_flushlen) {
Roger Dingledine's avatar
Roger Dingledine committed
763
    log_info(LD_NET,"fd %d, type %s, state %s, %d bytes on outbuf.",
764
             (int)conn->s, conn_type_to_string(conn->type),
Roger Dingledine's avatar
Roger Dingledine committed
765
766
             conn_state_to_string(conn->type, conn->state),
             (int)conn->outbuf_flushlen);
767
  }
768

769
  connection_unregister_events(conn);
770

771
772
773
774
  /* Prevent the event from getting unblocked. */
  conn->read_blocked_on_bw =
    conn->write_blocked_on_bw = 0;

775
  if (SOCKET_OK(conn->s))
776
    tor_close_socket(conn->s);
777
  conn->s = TOR_INVALID_SOCKET;
778
779
  if (conn->linked)
    conn->linked_conn_is_closed = 1;
780
  if (conn->outbuf)
781
    buf_clear(conn->outbuf);
782
  conn->outbuf_flushlen = 0;
783
784
}

785
/** Mark <b>conn</b> to be closed next time we loop through
786
 * conn_close_if_marked() in main.c. */
787
void
788
connection_mark_for_close_(connection_t *conn, int line, const char *file)
789
790
{
  assert_connection_ok(conn,0);
791
  tor_assert(line);
792
  tor_assert(line < 1<<16); /* marked_for_close can only fit a uint16_t. */
793
  tor_assert(file);
794

795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
  if (conn->type == CONN_TYPE_OR) {
    /*
     * An or_connection should have been closed through one of the channel-
     * aware functions in connection_or.c.  We'll assume this is an error
     * close and do that, and log a bug warning.
     */
    log_warn(LD_CHANNEL | LD_BUG,
             "Something tried to close an or_connection_t without going "
             "through channels at %s:%d",
             file, line);
    connection_or_close_for_error(TO_OR_CONN(conn), 0);
  } else {
    /* Pass it down to the real function */
    connection_mark_for_close_internal_(conn, line, file);
  }
}

/** Mark <b>conn</b> to be closed next time we loop through
 * conn_close_if_marked() in main.c; the _internal version bypasses the
 * CONN_TYPE_OR checks; this should be called when you either are sure that
 * if this is an or_connection_t the controlling channel has been notified
 * (e.g. with connection_or_notify_error()), or you actually are the
817
 * connection_or_close_for_error() or connection_or_close_normally() function.
818
819
820
 * For all other cases, use connection_mark_and_flush() instead, which
 * checks for or_connection_t properly, instead.  See below.
 */
821
822
823
MOCK_IMPL(void,
connection_mark_for_close_internal_, (connection_t *conn,
                                      int line, const char *file))
824
825
826
827
828
829
{
  assert_connection_ok(conn,0);
  tor_assert(line);
  tor_assert(line < 1<<16); /* marked_for_close can only fit a uint16_t. */
  tor_assert(file);

830
  if (conn->marked_for_close) {
831
    log_warn(LD_BUG,"Duplicate call to connection_mark_for_close at %s:%d"
832
833
        " (first at %s:%d)", file, line, conn->marked_for_close_file,
        conn->marked_for_close);
834
    tor_fragile_assert();
835
    return;
836
837
  }

838
839
840
841
842
843
  if (conn->type == CONN_TYPE_OR) {
    /*
     * Bad news if this happens without telling the controlling channel; do
     * this so we can find things that call this wrongly when the asserts hit.
     */
    log_debug(LD_CHANNEL,
844
845
              "Calling connection_mark_for_close_internal_() on an OR conn "
              "at %s:%d",
846
847
848
              file, line);
  }

849
850
  conn->marked_for_close = line;
  conn->marked_for_close_file = file;
851
  add_connection_to_closeable_list(conn);
852
853
854
855
856

  /* in case we're going to be held-open-til-flushed, reset
   * the number of seconds since last successful write, so
   * we get our whole 15 seconds */
  conn->timestamp_lastwritten = time(NULL);
857
858
}

859
860
861
862
863
/** Find each connection that has hold_open_until_flushed set to
 * 1 but hasn't written in the past 15 seconds, and set
 * hold_open_until_flushed to 0. This means it will get cleaned
 * up in the next loop through close_if_marked() in main.c.
 */
864
865
void
connection_expire_held_open(void)
866
867
{
  time_t now;
868
  smartlist_t *conns = get_connection_array();
869
870
871

  now = time(NULL);

872
  SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
873
874
875
    /* If we've been holding the connection open, but we haven't written
     * for 15 seconds...
     */
876
    if (conn->hold_open_until_flushed) {
Roger Dingledine's avatar
Roger Dingledine committed
877
      tor_assert(conn->marked_for_close);
878
      if (now - conn->timestamp_lastwritten >= 15) {
Roger Dingledine's avatar
Roger Dingledine committed
879
880
        int severity;
        if (conn->type == CONN_TYPE_EXIT ||
881
882
            (conn->type == CONN_TYPE_DIR &&
             conn->purpose == DIR_PURPOSE_SERVER))
Roger Dingledine's avatar
Roger Dingledine committed
883
884
885
          severity = LOG_INFO;
        else
          severity = LOG_NOTICE;
886
        log_fn(severity, LD_NET,
887
888
               "Giving up on marked_for_close conn that's been flushing "
               "for 15s (fd %d, type %s, state %s).",
889
               (int)conn->s, conn_type_to_string(conn->type),
890
               conn_state_to_string(conn->type, conn->state));
891
892
        conn->hold_open_until_flushed = 0;
      }
893
    }
894
  } SMARTLIST_FOREACH_END(conn);
895
896
}

897
#if defined(HAVE_SYS_UN_H) || defined(RUNNING_DOXYGEN)
898
/** Create an AF_UNIX listenaddr struct.
Nick Mathewson's avatar
Nick Mathewson committed
899
 * <b>listenaddress</b> provides the path to the Unix socket.
900
901
902
903
904
905
 *
 * Eventually <b>listenaddress</b> will also optionally contain user, group,
 * and file permissions for the new socket.  But not yet. XXX
 * Also, since we do not create the socket here the information doesn't help
 * here.
 *
Nick Mathewson's avatar
Nick Mathewson committed
906
 * If not NULL <b>readable_address</b> will contain a copy of the path part of
907
908
909
910
911
 * <b>listenaddress</b>.
 *
 * The listenaddr struct has to be freed by the caller.
 */
static struct sockaddr_un *
912
913
create_unix_sockaddr(const char *listenaddress, char **readable_address,
                     socklen_t *len_out)
914
915
916
917
918
{
  struct sockaddr_un *sockaddr = NULL;

  sockaddr = tor_malloc_zero(sizeof(struct sockaddr_un));
  sockaddr->sun_family = AF_UNIX;
919
920
921
922
923
924
925
  if (strlcpy(sockaddr->sun_path, listenaddress, sizeof(sockaddr->sun_path))
      >= sizeof(sockaddr->sun_path)) {
    log_warn(LD_CONFIG, "Unix socket path '%s' is too long to fit.",
             escaped(listenaddress));
    tor_free(sockaddr);
    return NULL;
  }
926
927
928
929

  if (readable_address)
    *readable_address = tor_strdup(listenaddress);

930
  *len_out = sizeof(struct sockaddr_un);
931
932
  return sockaddr;
}
933
#else /* !(defined(HAVE_SYS_UN_H) || defined(RUNNING_DOXYGEN)) */
934
static struct sockaddr *
935
936
create_unix_sockaddr(const char *listenaddress, char **readable_address,
                     socklen_t *len_out)
937
{
938
939
  (void)listenaddress;
  (void)readable_address;
940
941
  log_fn(LOG_ERR, LD_BUG,
         "Unix domain sockets not supported, yet we tried to create one.");
942
  *len_out = 0;
943
  tor_fragile_assert();
944
945
  return NULL;
}
946
#endif /* defined(HAVE_SYS_UN_H) || defined(RUNNING_DOXYGEN) */
947

948
949
950
/** Warn that an accept or a connect has failed because we're running out of
 * TCP sockets we can use on current system.  Rate-limit these warnings so
 * that we don't spam the log. */
Nick Mathewson's avatar
Nick Mathewson committed
951
952
953
954
static void
warn_too_many_conns(void)
{
#define WARN_TOO_MANY_CONNS_INTERVAL (6*60*60)
955
956
957
958
  static ratelim_t last_warned = RATELIM_INIT(WARN_TOO_MANY_CONNS_INTERVAL);
  char *m;
  if ((m = rate_limit_log(&last_warned, approx_time()))) {
    int n_conns = get_n_open_sockets();
Nick Mathewson's avatar
Nick Mathewson committed
959