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.
Nick Mathewson's avatar
Nick Mathewson committed
4
 * Copyright (c) 2007-2018, 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
58
59
#include "core/or/or.h"
#include "feature/client/bridges.h"
60
#include "lib/container/buffers.h"
61
#include "lib/tls/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 "lib/err/backtrace.h"
69
70
71
72
73
74
75
76
77
78
#include "core/or/channel.h"
#include "core/or/channeltls.h"
#include "core/or/circuitbuild.h"
#include "core/or/circuitlist.h"
#include "core/or/circuituse.h"
#include "app/config/config.h"
#include "core/mainloop/connection.h"
#include "core/or/connection_edge.h"
#include "core/or/connection_or.h"
#include "feature/control/control.h"
79
#include "lib/crypt_ops/crypto_util.h"
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "feature/dircache/directory.h"
#include "feature/dircache/dirserv.h"
#include "feature/relay/dns.h"
#include "feature/client/dnsserv.h"
#include "core/or/dos.h"
#include "feature/client/entrynodes.h"
#include "feature/relay/ext_orport.h"
#include "feature/stats/geoip.h"
#include "core/mainloop/main.h"
#include "feature/hibernate/hibernate.h"
#include "feature/hs/hs_common.h"
#include "feature/hs/hs_ident.h"
#include "feature/nodelist/nodelist.h"
#include "core/proto/proto_http.h"
#include "core/proto/proto_socks.h"
#include "core/or/policies.h"
#include "core/or/reasons.h"
#include "core/or/relay.h"
#include "feature/rend/rendclient.h"
#include "feature/rend/rendcommon.h"
#include "feature/stats/rephist.h"
#include "feature/relay/router.h"
#include "feature/nodelist/routerlist.h"
#include "feature/client/transports.h"
#include "feature/nodelist/routerparse.h"
105
#include "lib/sandbox/sandbox.h"
Nick Mathewson's avatar
Nick Mathewson committed
106
#include "lib/net/buffers_net.h"
107
#include "lib/tls/tortls.h"
108
#include "lib/evloop/compat_libevent.h"
109
#include "lib/compress/compress.h"
Roger Dingledine's avatar
Roger Dingledine committed
110

111
112
113
114
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif

115
116
117
118
119
120
121
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif

122
123
124
125
126
#ifdef HAVE_SYS_UN_H
#include <sys/socket.h>
#include <sys/un.h>
#endif

127
128
129
130
131
132
133
134
#include "feature/dircommon/dir_connection_st.h"
#include "feature/control/control_connection_st.h"
#include "core/or/entry_connection_st.h"
#include "core/or/listener_connection_st.h"
#include "core/or/or_connection_st.h"
#include "core/or/port_cfg_st.h"
#include "feature/nodelist/routerinfo_st.h"
#include "core/or/socks_request_st.h"
135

136
static connection_t *connection_listener_new(
137
                               const struct sockaddr *listensockaddr,
138
                               socklen_t listensocklen, int type,
139
                               const char *address,
140
141
142
143
144
                               const port_cfg_t *portcfg,
                               int *addr_in_use);
static connection_t *connection_listener_new_for_port(
                               const port_cfg_t *port,
                               int *defer, int *addr_in_use);
145
146
static void connection_init(time_t now, connection_t *conn, int type,
                            int socket_family);
Nick Mathewson's avatar
Nick Mathewson committed
147
static int connection_handle_listener_read(connection_t *conn, int new_type);
148
static int connection_finished_flushing(connection_t *conn);
149
static int connection_flushed_some(connection_t *conn);
150
static int connection_finished_connecting(connection_t *conn);
151
static int connection_reached_eof(connection_t *conn);
152
153
154
static int connection_buf_read_from_socket(connection_t *conn,
                                           ssize_t *max_to_read,
                                           int *socket_error);
155
static int connection_process_inbuf(connection_t *conn, int package_partial);
156
157
static void client_check_address_changed(tor_socket_t sock);
static void set_constrained_socket_buffers(tor_socket_t sock, int size);
158

159
160
161
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);
162
static const char *proxy_type_to_string(int proxy_type);
163
static int get_proxy_type(void);
164
165
const tor_addr_t *conn_get_outbound_address(sa_family_t family,
                  const or_options_t *options, unsigned int conn_type);
166
167
static void reenable_blocked_connection_init(const or_options_t *options);
static void reenable_blocked_connection_schedule(void);
168

169
170
171
/** 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.
 *
172
 * XXXX+ We should really use the entire list of interfaces here.
173
174
 **/
static tor_addr_t *last_interface_ipv4 = NULL;
175
/* DOCDOC last_interface_ipv6 */
176
177
static tor_addr_t *last_interface_ipv6 = NULL;
/** A list of tor_addr_t for addresses we've used in outgoing connections.
178
 * Used to detect IP address changes. */
179
static smartlist_t *outgoing_addrs = NULL;
180

181
182
#define CASE_ANY_LISTENER_TYPE \
    case CONN_TYPE_OR_LISTENER: \
183
    case CONN_TYPE_EXT_OR_LISTENER: \
184
185
186
187
188
    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: \
189
190
    case CONN_TYPE_AP_DNS_LISTENER: \
    case CONN_TYPE_AP_HTTP_CONNECT_LISTENER
191

192
193
/**************************************************************/

194
195
196
197
198
199
200
201
202
/** Convert a connection_t* to an listener_connection_t*; assert if the cast
 * is invalid. */
listener_connection_t *
TO_LISTENER_CONN(connection_t *c)
{
  tor_assert(c->magic == LISTENER_CONNECTION_MAGIC);
  return DOWNCAST(listener_connection_t, c);
}

203
204
205
206
207
208
209
210
211
212
213
214
size_t
connection_get_inbuf_len(connection_t *conn)
{
  return conn->inbuf ? buf_datalen(conn->inbuf) : 0;
}

size_t
connection_get_outbuf_len(connection_t *conn)
{
    return conn->outbuf ? buf_datalen(conn->outbuf) : 0;
}

215
216
217
/**
 * Return the human-readable name for the connection type <b>type</b>
 */
218
219
220
221
222
223
224
225
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";
226
    case CONN_TYPE_AP_LISTENER: return "Socks listener";
227
228
229
    case CONN_TYPE_AP_TRANS_LISTENER:
      return "Transparent pf/netfilter listener";
    case CONN_TYPE_AP_NATD_LISTENER: return "Transparent natd listener";
230
    case CONN_TYPE_AP_DNS_LISTENER: return "DNS listener";
231
232
233
    case CONN_TYPE_AP: return "Socks";
    case CONN_TYPE_DIR_LISTENER: return "Directory listener";
    case CONN_TYPE_DIR: return "Directory";
234
235
    case CONN_TYPE_CONTROL_LISTENER: return "Control listener";
    case CONN_TYPE_CONTROL: return "Control";
236
237
    case CONN_TYPE_EXT_OR: return "Extended OR";
    case CONN_TYPE_EXT_OR_LISTENER: return "Extended OR listener";
238
    case CONN_TYPE_AP_HTTP_CONNECT_LISTENER: return "HTTP tunnel listener";
239
    default:
240
      log_warn(LD_BUG, "unknown connection type %d", type);
241
242
243
244
245
      tor_snprintf(buf, sizeof(buf), "unknown [%d]", type);
      return buf;
  }
}

246
247
248
249
/**
 * Return the human-readable name for the connection state <b>state</b>
 * for the connection type <b>type</b>
 */
250
const char *
251
252
conn_state_to_string(int type, int state)
{
253
254
  static char buf[96];
  switch (type) {
255
    CASE_ANY_LISTENER_TYPE:
256
257
258
259
260
261
      if (state == LISTENER_STATE_READY)
        return "ready";
      break;
    case CONN_TYPE_OR:
      switch (state) {
        case OR_CONN_STATE_CONNECTING: return "connect()ing";
262
        case OR_CONN_STATE_PROXY_HANDSHAKING: return "handshaking (proxy)";
263
        case OR_CONN_STATE_TLS_HANDSHAKING: return "handshaking (TLS)";
264
        case OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING:
265
          return "renegotiating (TLS, v2 handshake)";
266
        case OR_CONN_STATE_TLS_SERVER_RENEGOTIATING:
267
268
269
270
271
          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)";
272
273
274
        case OR_CONN_STATE_OPEN: return "open";
      }
      break;
275
276
    case CONN_TYPE_EXT_OR:
      switch (state) {
277
278
279
280
281
282
        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";
283
284
285
286
        case EXT_OR_CONN_STATE_OPEN: return "open";
        case EXT_OR_CONN_STATE_FLUSHING: return "flushing final OKAY";
      }
      break;
287
288
289
290
291
292
293
294
295
296
    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) {
297
        case AP_CONN_STATE_SOCKS_WAIT: return "waiting for socks info";
298
        case AP_CONN_STATE_NATD_WAIT: return "waiting for natd dest info";
299
300
        case AP_CONN_STATE_RENDDESC_WAIT: return "waiting for rendezvous desc";
        case AP_CONN_STATE_CONTROLLER_WAIT: return "waiting for controller";
301
302
303
        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";
304
305
306
307
308
309
310
        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
311
        case DIR_CONN_STATE_CLIENT_READING: return "client reading";
312
        case DIR_CONN_STATE_CLIENT_FINISHED: return "client finished";
313
314
315
316
317
318
        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) {
319
320
        case CONTROL_CONN_STATE_OPEN: return "open (protocol v1)";
        case CONTROL_CONN_STATE_NEEDAUTH:
321
          return "waiting for authentication (protocol v1)";
322
323
324
325
      }
      break;
  }

326
  log_warn(LD_BUG, "unknown connection state %d (type %d)", state, type);
327
328
329
330
331
332
  tor_snprintf(buf, sizeof(buf),
               "unknown state [%d] on unknown [%s] connection",
               state, conn_type_to_string(type));
  return buf;
}

333
334
/** Allocate and return a new dir_connection_t, initialized as by
 * connection_init(). */
335
336
337
338
339
340
341
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;
}
342
343

/** Allocate and return a new or_connection_t, initialized as by
Nick Mathewson's avatar
Nick Mathewson committed
344
 * connection_init().
345
346
347
348
349
 *
 * Initialize active_circuit_pqueue.
 *
 * Set active_circuit_pqueue_last_recalibrated to current cell_ewma tick.
 */
350
or_connection_t *
351
or_connection_new(int type, int socket_family)
352
353
354
{
  or_connection_t *or_conn = tor_malloc_zero(sizeof(or_connection_t));
  time_t now = time(NULL);
355
356
  tor_assert(type == CONN_TYPE_OR || type == CONN_TYPE_EXT_OR);
  connection_init(now, TO_CONN(or_conn), type, socket_family);
357

358
  connection_or_set_canonical(or_conn, 0);
359

360
361
362
  if (type == CONN_TYPE_EXT_OR)
    connection_or_set_ext_or_identifier(or_conn);

363
364
  return or_conn;
}
365

366
/** Allocate and return a new entry_connection_t, initialized as by
367
368
369
370
 * connection_init().
 *
 * Allocate space to store the socks_request.
 */
371
372
373
374
375
376
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);
377
  entry_conn->socks_request = socks_request_new();
378
379
380
381
  /* 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)
382
    entry_conn->entry_cfg.ipv4_traffic = 1;
383
  else if (socket_family == AF_INET6)
384
    entry_conn->entry_cfg.ipv6_traffic = 1;
385
386
387
  return entry_conn;
}

388
389
/** Allocate and return a new edge_connection_t, initialized as by
 * connection_init(). */
390
391
392
393
edge_connection_t *
edge_connection_new(int type, int socket_family)
{
  edge_connection_t *edge_conn = tor_malloc_zero(sizeof(edge_connection_t));
394
  tor_assert(type == CONN_TYPE_EXIT);
395
396
397
  connection_init(time(NULL), TO_CONN(edge_conn), type, socket_family);
  return edge_conn;
}
398
399
400

/** Allocate and return a new control_connection_t, initialized as by
 * connection_init(). */
401
402
403
404
405
406
407
408
409
410
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;
}

411
412
413
414
415
416
417
418
419
420
421
/** 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;
}

422
423
424
/** 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. */
425
426
427
428
429
connection_t *
connection_new(int type, int socket_family)
{
  switch (type) {
    case CONN_TYPE_OR:
430
431
    case CONN_TYPE_EXT_OR:
      return TO_CONN(or_connection_new(type, socket_family));
432
433
434
435

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

436
437
438
    case CONN_TYPE_AP:
      return ENTRY_TO_CONN(entry_connection_new(type, socket_family));

439
440
441
442
443
444
    case CONN_TYPE_DIR:
      return TO_CONN(dir_connection_new(socket_family));

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

445
446
447
    CASE_ANY_LISTENER_TYPE:
      return TO_CONN(listener_connection_new(type, socket_family));

448
449
450
451
452
453
454
455
456
457
    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).
458
 *
459
460
 * Set conn-\>magic to the correct value.
 *
461
 * Set conn-\>type to <b>type</b>. Set conn-\>s and conn-\>conn_array_index to
462
463
464
465
 * -1 to signify they are not yet assigned.
 *
 * Initialize conn's timestamps to now.
 */
466
467
static void
connection_init(time_t now, connection_t *conn, int type, int socket_family)
468
{
469
470
  static uint64_t n_connections_allocated = 1;

471
472
  switch (type) {
    case CONN_TYPE_OR:
473
    case CONN_TYPE_EXT_OR:
474
      conn->magic = OR_CONNECTION_MAGIC;
475
476
      break;
    case CONN_TYPE_EXIT:
477
      conn->magic = EDGE_CONNECTION_MAGIC;
478
479
      break;
    case CONN_TYPE_AP:
480
      conn->magic = ENTRY_CONNECTION_MAGIC;
481
482
      break;
    case CONN_TYPE_DIR:
483
      conn->magic = DIR_CONNECTION_MAGIC;
484
485
      break;
    case CONN_TYPE_CONTROL:
486
      conn->magic = CONTROL_CONNECTION_MAGIC;
487
      break;
488
489
    CASE_ANY_LISTENER_TYPE:
      conn->magic = LISTENER_CONNECTION_MAGIC;
490
      break;
491
    default:
492
      conn->magic = BASE_CONNECTION_MAGIC;
493
494
495
      break;
  }

496
  conn->s = TOR_INVALID_SOCKET; /* give it a default of 'not used' */
497
  conn->conn_array_index = -1; /* also default to 'not used' */
498
  conn->global_identifier = n_connections_allocated++;
Roger Dingledine's avatar
Roger Dingledine committed
499
500

  conn->type = type;
501
  conn->socket_family = socket_family;
502
503
  if (!connection_is_listener(conn)) {
    /* listeners never use their buf */
504
505
506
    conn->inbuf = buf_new();
    conn->outbuf = buf_new();
  }
507

508
  conn->timestamp_created = now;
509
510
  conn->timestamp_last_read_allowed = now;
  conn->timestamp_last_write_allowed = now;
Roger Dingledine's avatar
Roger Dingledine committed
511
512
}

Roger Dingledine's avatar
Roger Dingledine committed
513
/** Create a link between <b>conn_a</b> and <b>conn_b</b>. */
514
515
516
void
connection_link_connections(connection_t *conn_a, connection_t *conn_b)
{
517
518
  tor_assert(! SOCKET_OK(conn_a->s));
  tor_assert(! SOCKET_OK(conn_b->s));
519
520
521
522
523
524
525

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

526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/** 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;
  }
}

542
543
544
/** 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.
545
 */
Nick Mathewson's avatar
Nick Mathewson committed
546
STATIC void
547
connection_free_minimal(connection_t *conn)
548
{
549
  void *mem;
550
  size_t memlen;
551
552
553
  if (!conn)
    return;

554
555
  switch (conn->type) {
    case CONN_TYPE_OR:
556
    case CONN_TYPE_EXT_OR:
557
558
      tor_assert(conn->magic == OR_CONNECTION_MAGIC);
      mem = TO_OR_CONN(conn);
559
      memlen = sizeof(or_connection_t);
560
561
      break;
    case CONN_TYPE_AP:
562
563
564
565
      tor_assert(conn->magic == ENTRY_CONNECTION_MAGIC);
      mem = TO_ENTRY_CONN(conn);
      memlen = sizeof(entry_connection_t);
      break;
566
567
568
    case CONN_TYPE_EXIT:
      tor_assert(conn->magic == EDGE_CONNECTION_MAGIC);
      mem = TO_EDGE_CONN(conn);
569
      memlen = sizeof(edge_connection_t);
570
571
572
573
      break;
    case CONN_TYPE_DIR:
      tor_assert(conn->magic == DIR_CONNECTION_MAGIC);
      mem = TO_DIR_CONN(conn);
574
      memlen = sizeof(dir_connection_t);
575
576
577
578
      break;
    case CONN_TYPE_CONTROL:
      tor_assert(conn->magic == CONTROL_CONNECTION_MAGIC);
      mem = TO_CONTROL_CONN(conn);
579
      memlen = sizeof(control_connection_t);
580
      break;
581
582
583
584
585
    CASE_ANY_LISTENER_TYPE:
      tor_assert(conn->magic == LISTENER_CONNECTION_MAGIC);
      mem = TO_LISTENER_CONN(conn);
      memlen = sizeof(listener_connection_t);
      break;
586
587
588
    default:
      tor_assert(conn->magic == BASE_CONNECTION_MAGIC);
      mem = conn;
589
      memlen = sizeof(connection_t);
590
591
      break;
  }
Roger Dingledine's avatar
Roger Dingledine committed
592

593
  if (conn->linked) {
594
595
596
597
    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),
598
599
             (int)connection_get_inbuf_len(conn),
             (int)connection_get_outbuf_len(conn));
600
601
  }

602
  if (!connection_is_listener(conn)) {
603
604
    buf_free(conn->inbuf);
    buf_free(conn->outbuf);
605
  } else {
606
    if (conn->socket_family == AF_UNIX) {
607
      /* For now only control and SOCKS ports can be Unix domain sockets
608
       * and listeners at the same time */
609
      tor_assert(conn_listener_type_supports_af_unix(conn->type));
610
611
612
613
614
615

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

618
  tor_free(conn->address);
Roger Dingledine's avatar
Roger Dingledine committed
619

620
  if (connection_speaks_cells(conn)) {
621
    or_connection_t *or_conn = TO_OR_CONN(conn);
622
623
624
625
    tor_tls_free(or_conn->tls);
    or_conn->tls = NULL;
    or_handshake_state_free(or_conn->handshake_state);
    or_conn->handshake_state = NULL;
626
    tor_free(or_conn->nickname);
627
628
629
630
    if (or_conn->chan) {
      /* Owww, this shouldn't happen, but... */
      log_info(LD_CHANNEL,
               "Freeing orconn at %p, saw channel %p with ID "
631
               "%"PRIu64 " left un-NULLed",
632
               or_conn, TLS_CHAN_TO_BASE(or_conn->chan),
633
               (
634
                 TLS_CHAN_TO_BASE(or_conn->chan)->global_identifier));
635
      if (!CHANNEL_FINISHED(TLS_CHAN_TO_BASE(or_conn->chan))) {
636
637
638
639
640
641
        channel_close_for_error(TLS_CHAN_TO_BASE(or_conn->chan));
      }

      or_conn->chan->conn = NULL;
      or_conn->chan = NULL;
    }
642
  }
643
644
645
646
647
648
649
  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) {
650
      buf_free(entry_conn->pending_optimistic_data);
651
    }
652
    if (entry_conn->sending_optimistic_data) {
653
      buf_free(entry_conn->sending_optimistic_data);
654
    }
655
656
657
  }
  if (CONN_IS_EDGE(conn)) {
    rend_data_free(TO_EDGE_CONN(conn)->rend_data);
658
    hs_ident_edge_conn_free(TO_EDGE_CONN(conn)->hs_ident);
659
660
661
  }
  if (conn->type == CONN_TYPE_CONTROL) {
    control_connection_t *control_conn = TO_CONTROL_CONN(conn);
662
    tor_free(control_conn->safecookie_client_hash);
663
    tor_free(control_conn->incoming_cmd);
664
665
666
667
668
669
670
    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
671
672
  }

673
674
675
676
  /* 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;
677
678
679
680

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

682
    tor_compress_free(dir_conn->compress_state);
683
684
685
686
    if (dir_conn->spool) {
      SMARTLIST_FOREACH(dir_conn->spool, spooled_resource_t *, spooled,
                        spooled_resource_free(spooled));
      smartlist_free(dir_conn->spool);
687
    }
688
689

    rend_data_free(dir_conn->rend_data);
690
    hs_ident_dir_conn_free(dir_conn->hs_ident);
691
692
    if (dir_conn->guard_state) {
      /* Cancel before freeing, if it's still there. */
693
      entry_guard_cancel(&dir_conn->guard_state);
694
    }
695
    circuit_guard_state_free(dir_conn->guard_state);
696
  }
697

698
699
  if (SOCKET_OK(conn->s)) {
    log_debug(LD_NET,"closing fd %d.",(int)conn->s);
700
    tor_close_socket(conn->s);
701
    conn->s = TOR_INVALID_SOCKET;
702
703
  }

704
  if (conn->type == CONN_TYPE_OR &&
705
      !tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest)) {
Roger Dingledine's avatar
Roger Dingledine committed
706
    log_warn(LD_BUG, "called on OR conn with non-zeroed identity_digest");
707
    connection_or_clear_identity(TO_OR_CONN(conn));
708
  }
709
  if (conn->type == CONN_TYPE_OR || conn->type == CONN_TYPE_EXT_OR) {
710
    connection_or_remove_from_ext_or_id_map(TO_OR_CONN(conn));
711
    tor_free(TO_OR_CONN(conn)->ext_or_conn_id);
712
    tor_free(TO_OR_CONN(conn)->ext_or_auth_correct_client_hash);
713
    tor_free(TO_OR_CONN(conn)->ext_or_transport);
714
  }
715

716
  memwipe(mem, 0xCC, memlen); /* poison memory */
717
  tor_free(mem);
Roger Dingledine's avatar
Roger Dingledine committed
718
719
}

720
721
/** Make sure <b>conn</b> isn't in any of the global conn lists; then free it.
 */
722
MOCK_IMPL(void,
723
connection_free_,(connection_t *conn))
724
{
725
726
  if (!conn)
    return;
727
728
  tor_assert(!connection_is_on_closeable_list(conn));
  tor_assert(!connection_in_array(conn));
729
  if (BUG(conn->linked_conn)) {
730
731
732
733
734
735
    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;
  }
736
  if (connection_speaks_cells(conn)) {
737
    if (!tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest)) {
738
      connection_or_clear_identity(TO_OR_CONN(conn));
739
    }
740
  }
741
  if (conn->type == CONN_TYPE_CONTROL) {
742
    connection_control_closed(TO_CONTROL_CONN(conn));
743
  }
744
745
746
747
748
749
#if 1
  /* DEBUGGING */
  if (conn->type == CONN_TYPE_AP) {
    connection_ap_warn_and_unmark_if_pending_circ(TO_ENTRY_CONN(conn),
                                                  "connection_free");
  }
750
#endif /* 1 */
751
752
753
754
755
756
757

  /* Notify the circuit creation DoS mitigation subsystem that an OR client
   * connection has been closed. And only do that if we track it. */
  if (conn->type == CONN_TYPE_OR) {
    dos_close_client_conn(TO_OR_CONN(conn));
  }

758
  connection_unregister_events(conn);
759
  connection_free_minimal(conn);
760
761
}

762
763
764
/**
 * Called when we're about to finally unlink and free a connection:
 * perform necessary accounting and cleanup
765
766
767
768
769
770
771
 *   - 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.
772
 *   - OR and edge connections need to be unlinked from circuits.
773
 */
774
775
void
connection_about_to_close_connection(connection_t *conn)
776
{
777
  tor_assert(conn->marked_for_close);
778

779
  switch (conn->type) {
780
    case CONN_TYPE_DIR:
781
      connection_dir_about_to_close(TO_DIR_CONN(conn));
782
      break;
783
    case CONN_TYPE_OR:
784
    case CONN_TYPE_EXT_OR:
785
      connection_or_about_to_close(TO_OR_CONN(conn));
786
      break;
787
    case CONN_TYPE_AP:
788
      connection_ap_about_to_close(TO_ENTRY_CONN(conn));
789
      break;
790
    case CONN_TYPE_EXIT:
791
      connection_exit_about_to_close(TO_EDGE_CONN(conn));
792
      break;
793
  }
794
795
}

Roger Dingledine's avatar
Roger Dingledine committed
796
797
/** Return true iff connection_close_immediate() has been called on this
 * connection. */
798
#define CONN_IS_CLOSED(c) \
799
  ((c)->linked ? ((c)->linked_conn_is_closed) : (! SOCKET_OK(c->s)))
800

801
802
803
/** 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().
804
 */
805
806
void
connection_close_immediate(connection_t *conn)
807
808
{
  assert_connection_ok(conn,0);
809
  if (CONN_IS_CLOSED(conn)) {
810
    log_err(LD_BUG,"Attempt to close already-closed connection.");
811
    tor_fragile_assert();
812
813
    return;
  }
814
  if (conn->outbuf_flushlen) {
Roger Dingledine's avatar
Roger Dingledine committed
815
    log_info(LD_NET,"fd %d, type %s, state %s, %d bytes on outbuf.",
816
             (int)conn->s, conn_type_to_string(conn->type),
Roger Dingledine's avatar
Roger Dingledine committed
817
818
             conn_state_to_string(conn->type, conn->state),
             (int)conn->outbuf_flushlen);
819
  }
820

821
  connection_unregister_events(conn);
822

823
  /* Prevent the event from getting unblocked. */
824
825
  conn->read_blocked_on_bw = 0;
  conn->write_blocked_on_bw = 0;
826

827
  if (SOCKET_OK(conn->s))
828
    tor_close_socket(conn->s);
829
  conn->s = TOR_INVALID_SOCKET;
830
831
  if (conn->linked)
    conn->linked_conn_is_closed = 1;
832
  if (conn->outbuf)
833
    buf_clear(conn->outbuf);
834
  conn->outbuf_flushlen = 0;
835
836
}

837
/** Mark <b>conn</b> to be closed next time we loop through
838
 * conn_close_if_marked() in main.c. */
839
void
840
connection_mark_for_close_(connection_t *conn, int line, const char *file)
841
842
{
  assert_connection_ok(conn,0);
843
  tor_assert(line);
844
  tor_assert(line < 1<<16); /* marked_for_close can only fit a uint16_t. */
845
  tor_assert(file);
846

847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
  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
869
 * connection_or_close_for_error() or connection_or_close_normally() function.
870
871
872
 * For all other cases, use connection_mark_and_flush() instead, which
 * checks for or_connection_t properly, instead.  See below.
 */
873
874
875
MOCK_IMPL(void,
connection_mark_for_close_internal_, (connection_t *conn,
                                      int line, const char *file))
876
877
878
879
880
881
{
  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);

882
  if (conn->marked_for_close) {
883
    log_warn(LD_BUG,"Duplicate call to connection_mark_for_close at %s:%d"
884
885
        " (first at %s:%d)", file, line, conn->marked_for_close_file,
        conn->marked_for_close);
886
    tor_fragile_assert();
887
    return;
888
889
  }

890
891
892
893
894
895
  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,
896
897
              "Calling connection_mark_for_close_internal_() on an OR conn "
              "at %s:%d",
898
899
900
              file, line);
  }

901
902
  conn->marked_for_close = line;
  conn->marked_for_close_file = file;
903
  add_connection_to_closeable_list(conn);
904
905
906
907

  /* 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 */
908
  conn->timestamp_last_write_allowed = time(NULL);
909
910
}

911
912
913
914
915
/** 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.
 */
916
917
void
connection_expire_held_open(void)
918
919
{
  time_t now;
920
  smartlist_t *conns = get_connection_array();
921
922
923

  now = time(NULL);

924
  SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
925
926
927
    /* If we've been holding the connection open, but we haven't written
     * for 15 seconds...
     */
928
    if (conn->hold_open_until_flushed) {
Roger Dingledine's avatar
Roger Dingledine committed
929
      tor_assert(conn->marked_for_close);
930
      if (now - conn->timestamp_last_write_allowed >= 15) {
Roger Dingledine's avatar
Roger Dingledine committed
931
932
        int severity;
        if (conn->type == CONN_TYPE_EXIT ||
933
934
            (conn->type == CONN_TYPE_DIR &&
             conn->purpose == DIR_PURPOSE_SERVER))
Roger Dingledine's avatar
Roger Dingledine committed
935
936
937
          severity = LOG_INFO;
        else
          severity = LOG_NOTICE;
938
        log_fn(severity, LD_NET,
939
940
               "Giving up on marked_for_close conn that's been flushing "
               "for 15s (fd %d, type %s, state %s).",
941
               (int)conn->s, conn_type_to_string(conn->type),
942
               conn_state_to_string(conn->type, conn->state));
943
944
        conn->hold_open_until_flushed = 0;
      }
945
    }
946
  } SMARTLIST_FOREACH_END(conn);
947
948
}

949
#if defined(HAVE_SYS_UN_H) || defined(RUNNING_DOXYGEN)
950
/** Create an AF_UNIX listenaddr struct.
Nick Mathewson's avatar
Nick Mathewson committed
951
 * <b>listenaddress</b> provides the path to the Unix socket.
952
953
954
955
956
957
 *
 * 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
958
 * If not NULL <b>readable_address</b> will contain a copy of the path part of
959
960
961
962
963
 * <b>listenaddress</b>.
 *
 * The listenaddr struct has to be freed by the caller.
 */
static struct sockaddr_un *
964
965
create_unix_sockaddr(const char *listenaddress, char **readable_address,
                     socklen_t *len_out)
966
967
968
969
970
{
  struct sockaddr_un *sockaddr = NULL;

  sockaddr = tor_malloc_zero(sizeof(struct sockaddr_un));
  sockaddr->sun_family = AF_UNIX;
971
972
973
974
975
976
977
  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;
  }
978
979
980
981

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

982
  *len_out = sizeof(struct sockaddr_un);
983
984
  return sockaddr;
}
985
#else /* !(defined(HAVE_SYS_UN_H) || defined(RUNNING_DOXYGEN)) */
986
static struct sockaddr *
987
988
create_unix_sockaddr(const char *listenaddress, char **readable_address,
                     socklen_t *len_out)
989
{
990
991
  (void)listenaddress;
  (void)readable_address;
992
993
  log_fn(LOG_ERR, LD_BUG,
         "Unix domain sockets not supported, yet we tried to create one.");
994
  *len_out = 0;
995
  tor_fragile_assert();
996
997
  return NULL;
}
998
#endif /* defined(HAVE_SYS_UN_H) || defined(RUNNING_DOXYGEN) */
999

1000
/** Warn that an accept or a connect has failed because we're running out of
For faster browsing, not all history is shown. View entire blame