connection.c 189 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-2020, 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/buf/buffers.h"
61
#include "lib/tls/buffers_tls.h"
62
63
#include "lib/err/backtrace.h"

64
65
66
67
/*
 * Define this so we get channel internal functions, since we're implementing
 * part of a subclass (channel_tls_t).
 */
teor's avatar
teor committed
68
#define CHANNEL_OBJECT_PRIVATE
69
#include "app/config/config.h"
70
#include "app/config/resolve_addr.h"
71
72
73
#include "core/mainloop/connection.h"
#include "core/mainloop/mainloop.h"
#include "core/mainloop/netstatus.h"
74
75
76
77
78
79
80
#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 "core/or/connection_edge.h"
#include "core/or/connection_or.h"
81
82
83
84
#include "core/or/dos.h"
#include "core/or/policies.h"
#include "core/or/reasons.h"
#include "core/or/relay.h"
85
#include "core/or/crypt_path.h"
haxxpop's avatar
haxxpop committed
86
#include "core/proto/proto_haproxy.h"
87
88
89
90
91
#include "core/proto/proto_http.h"
#include "core/proto/proto_socks.h"
#include "feature/client/dnsserv.h"
#include "feature/client/entrynodes.h"
#include "feature/client/transports.h"
92
#include "feature/control/control.h"
93
#include "feature/control/control_events.h"
94
#include "feature/dirauth/authmode.h"
95
#include "feature/dirauth/dirauth_config.h"
96
#include "feature/dircache/dirserv.h"
97
#include "feature/dircommon/directory.h"
98
99
100
101
#include "feature/hibernate/hibernate.h"
#include "feature/hs/hs_common.h"
#include "feature/hs/hs_ident.h"
#include "feature/nodelist/nodelist.h"
102
103
104
105
#include "feature/nodelist/routerlist.h"
#include "feature/relay/dns.h"
#include "feature/relay/ext_orport.h"
#include "feature/relay/routermode.h"
106
107
108
#include "feature/rend/rendclient.h"
#include "feature/rend/rendcommon.h"
#include "feature/stats/rephist.h"
109
#include "lib/crypt_ops/crypto_util.h"
110
#include "lib/geoip/geoip.h"
111

112
#include "lib/cc/ctassert.h"
113
#include "lib/sandbox/sandbox.h"
Nick Mathewson's avatar
Nick Mathewson committed
114
#include "lib/net/buffers_net.h"
115
#include "lib/tls/tortls.h"
116
#include "lib/evloop/compat_libevent.h"
117
#include "lib/compress/compress.h"
Roger Dingledine's avatar
Roger Dingledine committed
118

119
120
121
122
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif

123
124
125
126
127
128
129
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif

130
131
132
133
134
#ifdef HAVE_SYS_UN_H
#include <sys/socket.h>
#include <sys/un.h>
#endif

135
136
137
138
139
140
141
142
#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"
143

rl1987's avatar
rl1987 committed
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
 * On Windows and Linux we cannot reliably bind() a socket to an
 * address and port if: 1) There's already a socket bound to wildcard
 * address (0.0.0.0 or ::) with the same port; 2) We try to bind()
 * to wildcard address and there's another socket bound to a
 * specific address and the same port.
 *
 * To address this problem on these two platforms we implement a
 * routine that:
 * 1) Checks if first attempt to bind() a new socket  failed with
 * EADDRINUSE.
 * 2) If so, it will close the appropriate old listener connection and
 * 3) Attempts bind()'ing the new listener socket again.
rl1987's avatar
rl1987 committed
157
158
159
 *
 * Just to be safe, we are enabling listener rebind code on all platforms,
 * to account for unexpected cases where it may be needed.
rl1987's avatar
rl1987 committed
160
 */
161
162
#define ENABLE_LISTENER_REBIND

163
static connection_t *connection_listener_new(
164
                               const struct sockaddr *listensockaddr,
165
                               socklen_t listensocklen, int type,
166
                               const char *address,
167
168
169
170
171
                               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);
172
173
static void connection_init(time_t now, connection_t *conn, int type,
                            int socket_family);
Nick Mathewson's avatar
Nick Mathewson committed
174
static int connection_handle_listener_read(connection_t *conn, int new_type);
175
static int connection_finished_flushing(connection_t *conn);
176
static int connection_flushed_some(connection_t *conn);
177
static int connection_finished_connecting(connection_t *conn);
178
static int connection_reached_eof(connection_t *conn);
179
180
181
static int connection_buf_read_from_socket(connection_t *conn,
                                           ssize_t *max_to_read,
                                           int *socket_error);
182
static int connection_process_inbuf(connection_t *conn, int package_partial);
183
184
static void client_check_address_changed(tor_socket_t sock);
static void set_constrained_socket_buffers(tor_socket_t sock, int size);
185

186
187
188
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);
189
static const char *proxy_type_to_string(int proxy_type);
190
static int conn_get_proxy_type(const connection_t *conn);
191
192
const tor_addr_t *conn_get_outbound_address(sa_family_t family,
                  const or_options_t *options, unsigned int conn_type);
193
194
static void reenable_blocked_connection_init(const or_options_t *options);
static void reenable_blocked_connection_schedule(void);
195

196
197
198
/** 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.
 *
199
 * XXXX+ We should really use the entire list of interfaces here.
200
201
 **/
static tor_addr_t *last_interface_ipv4 = NULL;
202
/* DOCDOC last_interface_ipv6 */
203
204
static tor_addr_t *last_interface_ipv6 = NULL;
/** A list of tor_addr_t for addresses we've used in outgoing connections.
205
 * Used to detect IP address changes. */
206
static smartlist_t *outgoing_addrs = NULL;
207

208
209
#define CASE_ANY_LISTENER_TYPE \
    case CONN_TYPE_OR_LISTENER: \
210
    case CONN_TYPE_EXT_OR_LISTENER: \
211
212
213
214
215
    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: \
216
217
    case CONN_TYPE_AP_DNS_LISTENER: \
    case CONN_TYPE_AP_HTTP_CONNECT_LISTENER
218

219
220
/**************************************************************/

221
222
223
224
225
226
227
228
229
/** 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);
}

230
231
232
233
234
235
236
237
238
239
240
241
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;
}

242
243
244
/**
 * Return the human-readable name for the connection type <b>type</b>
 */
245
246
247
248
249
250
251
252
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";
253
    case CONN_TYPE_AP_LISTENER: return "Socks listener";
254
255
256
    case CONN_TYPE_AP_TRANS_LISTENER:
      return "Transparent pf/netfilter listener";
    case CONN_TYPE_AP_NATD_LISTENER: return "Transparent natd listener";
257
    case CONN_TYPE_AP_DNS_LISTENER: return "DNS listener";
258
259
260
    case CONN_TYPE_AP: return "Socks";
    case CONN_TYPE_DIR_LISTENER: return "Directory listener";
    case CONN_TYPE_DIR: return "Directory";
261
262
    case CONN_TYPE_CONTROL_LISTENER: return "Control listener";
    case CONN_TYPE_CONTROL: return "Control";
263
264
    case CONN_TYPE_EXT_OR: return "Extended OR";
    case CONN_TYPE_EXT_OR_LISTENER: return "Extended OR listener";
265
    case CONN_TYPE_AP_HTTP_CONNECT_LISTENER: return "HTTP tunnel listener";
266
    default:
267
      log_warn(LD_BUG, "unknown connection type %d", type);
268
269
270
271
272
      tor_snprintf(buf, sizeof(buf), "unknown [%d]", type);
      return buf;
  }
}

273
274
275
276
/**
 * Return the human-readable name for the connection state <b>state</b>
 * for the connection type <b>type</b>
 */
277
const char *
278
279
conn_state_to_string(int type, int state)
{
280
281
  static char buf[96];
  switch (type) {
282
    CASE_ANY_LISTENER_TYPE:
283
284
285
286
287
288
      if (state == LISTENER_STATE_READY)
        return "ready";
      break;
    case CONN_TYPE_OR:
      switch (state) {
        case OR_CONN_STATE_CONNECTING: return "connect()ing";
289
        case OR_CONN_STATE_PROXY_HANDSHAKING: return "handshaking (proxy)";
290
        case OR_CONN_STATE_TLS_HANDSHAKING: return "handshaking (TLS)";
291
        case OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING:
292
          return "renegotiating (TLS, v2 handshake)";
293
        case OR_CONN_STATE_TLS_SERVER_RENEGOTIATING:
294
295
296
297
298
          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)";
299
300
301
        case OR_CONN_STATE_OPEN: return "open";
      }
      break;
302
303
    case CONN_TYPE_EXT_OR:
      switch (state) {
304
305
306
307
308
309
        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";
310
311
312
313
        case EXT_OR_CONN_STATE_OPEN: return "open";
        case EXT_OR_CONN_STATE_FLUSHING: return "flushing final OKAY";
      }
      break;
314
315
316
317
318
319
320
321
322
323
    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) {
324
        case AP_CONN_STATE_SOCKS_WAIT: return "waiting for socks info";
325
        case AP_CONN_STATE_NATD_WAIT: return "waiting for natd dest info";
326
327
        case AP_CONN_STATE_RENDDESC_WAIT: return "waiting for rendezvous desc";
        case AP_CONN_STATE_CONTROLLER_WAIT: return "waiting for controller";
328
329
330
        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";
331
332
333
334
335
336
337
        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
338
        case DIR_CONN_STATE_CLIENT_READING: return "client reading";
339
        case DIR_CONN_STATE_CLIENT_FINISHED: return "client finished";
340
341
342
343
344
345
        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) {
346
347
        case CONTROL_CONN_STATE_OPEN: return "open (protocol v1)";
        case CONTROL_CONN_STATE_NEEDAUTH:
348
          return "waiting for authentication (protocol v1)";
349
350
351
352
      }
      break;
  }

353
  log_warn(LD_BUG, "unknown connection state %d (type %d)", state, type);
354
355
356
357
358
359
  tor_snprintf(buf, sizeof(buf),
               "unknown state [%d] on unknown [%s] connection",
               state, conn_type_to_string(type));
  return buf;
}

360
361
/** Allocate and return a new dir_connection_t, initialized as by
 * connection_init(). */
362
363
364
365
366
367
368
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;
}
369
370

/** Allocate and return a new or_connection_t, initialized as by
Nick Mathewson's avatar
Nick Mathewson committed
371
 * connection_init().
372
373
374
375
376
 *
 * Initialize active_circuit_pqueue.
 *
 * Set active_circuit_pqueue_last_recalibrated to current cell_ewma tick.
 */
377
or_connection_t *
378
or_connection_new(int type, int socket_family)
379
380
381
{
  or_connection_t *or_conn = tor_malloc_zero(sizeof(or_connection_t));
  time_t now = time(NULL);
382
383
  tor_assert(type == CONN_TYPE_OR || type == CONN_TYPE_EXT_OR);
  connection_init(now, TO_CONN(or_conn), type, socket_family);
384

385
  connection_or_set_canonical(or_conn, 0);
386

387
388
389
390
  if (type == CONN_TYPE_EXT_OR) {
    /* If we aren't told an address for this connection, we should
     * presume it isn't local, and should be rate-limited. */
    TO_CONN(or_conn)->always_rate_limit_as_remote = 1;
391
    connection_or_set_ext_or_identifier(or_conn);
392
  }
393

394
395
  return or_conn;
}
396

397
/** Allocate and return a new entry_connection_t, initialized as by
398
399
400
401
 * connection_init().
 *
 * Allocate space to store the socks_request.
 */
402
403
404
405
406
407
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);
408
  entry_conn->socks_request = socks_request_new();
409
410
411
412
  /* 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)
413
    entry_conn->entry_cfg.ipv4_traffic = 1;
414
  else if (socket_family == AF_INET6)
415
    entry_conn->entry_cfg.ipv6_traffic = 1;
416
417
418
  return entry_conn;
}

419
420
/** Allocate and return a new edge_connection_t, initialized as by
 * connection_init(). */
421
422
423
424
edge_connection_t *
edge_connection_new(int type, int socket_family)
{
  edge_connection_t *edge_conn = tor_malloc_zero(sizeof(edge_connection_t));
425
  tor_assert(type == CONN_TYPE_EXIT);
426
427
428
  connection_init(time(NULL), TO_CONN(edge_conn), type, socket_family);
  return edge_conn;
}
429
430
431

/** Allocate and return a new control_connection_t, initialized as by
 * connection_init(). */
432
433
434
435
436
437
438
439
440
441
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;
}

442
443
444
445
446
447
448
449
450
451
452
/** 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;
}

453
454
455
/** 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. */
456
457
458
459
460
connection_t *
connection_new(int type, int socket_family)
{
  switch (type) {
    case CONN_TYPE_OR:
461
462
    case CONN_TYPE_EXT_OR:
      return TO_CONN(or_connection_new(type, socket_family));
463
464
465
466

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

467
468
469
    case CONN_TYPE_AP:
      return ENTRY_TO_CONN(entry_connection_new(type, socket_family));

470
471
472
473
474
475
    case CONN_TYPE_DIR:
      return TO_CONN(dir_connection_new(socket_family));

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

476
477
478
    CASE_ANY_LISTENER_TYPE:
      return TO_CONN(listener_connection_new(type, socket_family));

479
480
481
482
483
484
485
486
487
488
    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).
489
 *
490
491
 * Set conn-\>magic to the correct value.
 *
492
 * Set conn-\>type to <b>type</b>. Set conn-\>s and conn-\>conn_array_index to
493
494
495
496
 * -1 to signify they are not yet assigned.
 *
 * Initialize conn's timestamps to now.
 */
497
498
static void
connection_init(time_t now, connection_t *conn, int type, int socket_family)
499
{
500
501
  static uint64_t n_connections_allocated = 1;

502
503
  switch (type) {
    case CONN_TYPE_OR:
504
    case CONN_TYPE_EXT_OR:
505
      conn->magic = OR_CONNECTION_MAGIC;
506
507
      break;
    case CONN_TYPE_EXIT:
508
      conn->magic = EDGE_CONNECTION_MAGIC;
509
510
      break;
    case CONN_TYPE_AP:
511
      conn->magic = ENTRY_CONNECTION_MAGIC;
512
513
      break;
    case CONN_TYPE_DIR:
514
      conn->magic = DIR_CONNECTION_MAGIC;
515
516
      break;
    case CONN_TYPE_CONTROL:
517
      conn->magic = CONTROL_CONNECTION_MAGIC;
518
      break;
519
520
    CASE_ANY_LISTENER_TYPE:
      conn->magic = LISTENER_CONNECTION_MAGIC;
521
      break;
522
    default:
523
      conn->magic = BASE_CONNECTION_MAGIC;
524
525
526
      break;
  }

527
  conn->s = TOR_INVALID_SOCKET; /* give it a default of 'not used' */
528
  conn->conn_array_index = -1; /* also default to 'not used' */
529
  conn->global_identifier = n_connections_allocated++;
Roger Dingledine's avatar
Roger Dingledine committed
530
531

  conn->type = type;
532
  conn->socket_family = socket_family;
533
534
  if (!connection_is_listener(conn)) {
    /* listeners never use their buf */
535
536
537
    conn->inbuf = buf_new();
    conn->outbuf = buf_new();
  }
538

539
  conn->timestamp_created = now;
540
541
  conn->timestamp_last_read_allowed = now;
  conn->timestamp_last_write_allowed = now;
Roger Dingledine's avatar
Roger Dingledine committed
542
543
}

Roger Dingledine's avatar
Roger Dingledine committed
544
/** Create a link between <b>conn_a</b> and <b>conn_b</b>. */
545
546
547
void
connection_link_connections(connection_t *conn_a, connection_t *conn_b)
{
548
549
  tor_assert(! SOCKET_OK(conn_a->s));
  tor_assert(! SOCKET_OK(conn_b->s));
550
551
552
553
554
555
556

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

557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/** 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;
  }
}

573
574
575
/** 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.
576
 */
Nick Mathewson's avatar
Nick Mathewson committed
577
STATIC void
578
connection_free_minimal(connection_t *conn)
579
{
580
  void *mem;
581
  size_t memlen;
582
583
584
  if (!conn)
    return;

585
586
  switch (conn->type) {
    case CONN_TYPE_OR:
587
    case CONN_TYPE_EXT_OR:
588
589
      tor_assert(conn->magic == OR_CONNECTION_MAGIC);
      mem = TO_OR_CONN(conn);
590
      memlen = sizeof(or_connection_t);
591
592
      break;
    case CONN_TYPE_AP:
593
594
595
596
      tor_assert(conn->magic == ENTRY_CONNECTION_MAGIC);
      mem = TO_ENTRY_CONN(conn);
      memlen = sizeof(entry_connection_t);
      break;
597
598
599
    case CONN_TYPE_EXIT:
      tor_assert(conn->magic == EDGE_CONNECTION_MAGIC);
      mem = TO_EDGE_CONN(conn);
600
      memlen = sizeof(edge_connection_t);
601
602
603
604
      break;
    case CONN_TYPE_DIR:
      tor_assert(conn->magic == DIR_CONNECTION_MAGIC);
      mem = TO_DIR_CONN(conn);
605
      memlen = sizeof(dir_connection_t);
606
607
608
609
      break;
    case CONN_TYPE_CONTROL:
      tor_assert(conn->magic == CONTROL_CONNECTION_MAGIC);
      mem = TO_CONTROL_CONN(conn);
610
      memlen = sizeof(control_connection_t);
611
      break;
612
613
614
615
616
    CASE_ANY_LISTENER_TYPE:
      tor_assert(conn->magic == LISTENER_CONNECTION_MAGIC);
      mem = TO_LISTENER_CONN(conn);
      memlen = sizeof(listener_connection_t);
      break;
617
618
619
    default:
      tor_assert(conn->magic == BASE_CONNECTION_MAGIC);
      mem = conn;
620
      memlen = sizeof(connection_t);
621
622
      break;
  }
Roger Dingledine's avatar
Roger Dingledine committed
623

624
  if (conn->linked) {
625
626
627
628
    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),
629
630
             (int)connection_get_inbuf_len(conn),
             (int)connection_get_outbuf_len(conn));
631
632
  }

633
  if (!connection_is_listener(conn)) {
634
635
    buf_free(conn->inbuf);
    buf_free(conn->outbuf);
636
  } else {
637
    if (conn->socket_family == AF_UNIX) {
638
      /* For now only control and SOCKS ports can be Unix domain sockets
639
       * and listeners at the same time */
640
      tor_assert(conn_listener_type_supports_af_unix(conn->type));
641
642
643
644
645
646

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

649
  tor_str_wipe_and_free(conn->address);
Roger Dingledine's avatar
Roger Dingledine committed
650

651
  if (connection_speaks_cells(conn)) {
652
    or_connection_t *or_conn = TO_OR_CONN(conn);
653
654
655
656
657
658
659
660
    if (or_conn->tls) {
      if (! SOCKET_OK(conn->s)) {
        /* The socket has been closed by somebody else; we must tell the
         * TLS object not to close it. */
        tor_tls_release_socket(or_conn->tls);
      } else {
        /* The tor_tls_free() call below will close the socket; we must tell
         * the code below not to close it a second time. */
661
        tor_release_socket_ownership(conn->s);
662
663
664
665
666
        conn->s = TOR_INVALID_SOCKET;
      }
      tor_tls_free(or_conn->tls);
      or_conn->tls = NULL;
    }
667
668
    or_handshake_state_free(or_conn->handshake_state);
    or_conn->handshake_state = NULL;
669
    tor_str_wipe_and_free(or_conn->nickname);
670
671
    if (or_conn->chan) {
      /* Owww, this shouldn't happen, but... */
672
673
      channel_t *base_chan = TLS_CHAN_TO_BASE(or_conn->chan);
      tor_assert(base_chan);
674
675
      log_info(LD_CHANNEL,
               "Freeing orconn at %p, saw channel %p with ID "
676
               "%"PRIu64 " left un-NULLed",
677
678
679
680
               or_conn, base_chan,
               base_chan->global_identifier);
      if (!CHANNEL_FINISHED(base_chan)) {
        channel_close_for_error(base_chan);
681
682
683
684
685
      }

      or_conn->chan->conn = NULL;
      or_conn->chan = NULL;
    }
686
  }
687
688
  if (conn->type == CONN_TYPE_AP) {
    entry_connection_t *entry_conn = TO_ENTRY_CONN(conn);
689
690
    tor_str_wipe_and_free(entry_conn->chosen_exit_name);
    tor_str_wipe_and_free(entry_conn->original_dest_address);
691
692
693
    if (entry_conn->socks_request)
      socks_request_free(entry_conn->socks_request);
    if (entry_conn->pending_optimistic_data) {
694
      buf_free(entry_conn->pending_optimistic_data);
695
    }
696
    if (entry_conn->sending_optimistic_data) {
697
      buf_free(entry_conn->sending_optimistic_data);
698
    }
699
700
701
  }
  if (CONN_IS_EDGE(conn)) {
    rend_data_free(TO_EDGE_CONN(conn)->rend_data);
702
    hs_ident_edge_conn_free(TO_EDGE_CONN(conn)->hs_ident);
703
704
705
  }
  if (conn->type == CONN_TYPE_CONTROL) {
    control_connection_t *control_conn = TO_CONTROL_CONN(conn);
706
    tor_free(control_conn->safecookie_client_hash);
707
    tor_free(control_conn->incoming_cmd);
708
    tor_free(control_conn->current_cmd);
709
710
711
712
713
714
715
    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
716
717
  }

718
719
720
721
  /* 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;
722
723
724
725

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

727
    tor_compress_free(dir_conn->compress_state);
728
    dir_conn_clear_spool(dir_conn);
729
730

    rend_data_free(dir_conn->rend_data);
731
    hs_ident_dir_conn_free(dir_conn->hs_ident);
732
733
    if (dir_conn->guard_state) {
      /* Cancel before freeing, if it's still there. */
734
      entry_guard_cancel(&dir_conn->guard_state);
735
    }
736
    circuit_guard_state_free(dir_conn->guard_state);
737
  }
738

739
  if (SOCKET_OK(conn->s)) {
740
741
742
    log_debug(LD_NET,"closing fd %d.",(int)conn->s);
    tor_close_socket(conn->s);
    conn->s = TOR_INVALID_SOCKET;
743
744
  }

745
  if (conn->type == CONN_TYPE_OR &&
746
      !tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest)) {
Roger Dingledine's avatar
Roger Dingledine committed
747
    log_warn(LD_BUG, "called on OR conn with non-zeroed identity_digest");
748
    connection_or_clear_identity(TO_OR_CONN(conn));
749
  }
750
  if (conn->type == CONN_TYPE_OR || conn->type == CONN_TYPE_EXT_OR) {
751
    connection_or_remove_from_ext_or_id_map(TO_OR_CONN(conn));
752
    tor_free(TO_OR_CONN(conn)->ext_or_conn_id);
753
    tor_free(TO_OR_CONN(conn)->ext_or_auth_correct_client_hash);
754
    tor_free(TO_OR_CONN(conn)->ext_or_transport);
755
  }
756

757
  memwipe(mem, 0xCC, memlen); /* poison memory */
758
  tor_free(mem);
Roger Dingledine's avatar
Roger Dingledine committed
759
760
}

761
762
/** Make sure <b>conn</b> isn't in any of the global conn lists; then free it.
 */
763
MOCK_IMPL(void,
764
connection_free_,(connection_t *conn))
765
{
766
767
  if (!conn)
    return;
768
769
  tor_assert(!connection_is_on_closeable_list(conn));
  tor_assert(!connection_in_array(conn));
770
  if (BUG(conn->linked_conn)) {
771
772
773
774
775
776
    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;
  }
777
  if (connection_speaks_cells(conn)) {
778
    if (!tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest)) {
779
      connection_or_clear_identity(TO_OR_CONN(conn));
780
    }
781
  }
782
  if (conn->type == CONN_TYPE_CONTROL) {
783
    connection_control_closed(TO_CONTROL_CONN(conn));
784
  }
785
786
787
788
789
790
#if 1
  /* DEBUGGING */
  if (conn->type == CONN_TYPE_AP) {
    connection_ap_warn_and_unmark_if_pending_circ(TO_ENTRY_CONN(conn),
                                                  "connection_free");
  }
791
#endif /* 1 */
792
793
794
795
796
797
798

  /* 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));
  }

799
  connection_unregister_events(conn);
800
  connection_free_minimal(conn);
801
802
}

803
804
805
/**
 * Called when we're about to finally unlink and free a connection:
 * perform necessary accounting and cleanup
806
807
808
809
810
811
812
 *   - 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.
813
 *   - OR and edge connections need to be unlinked from circuits.
814
 */
815
816
void
connection_about_to_close_connection(connection_t *conn)
817
{
818
  tor_assert(conn->marked_for_close);
819

820
  switch (conn->type) {
821
    case CONN_TYPE_DIR:
822
      connection_dir_about_to_close(TO_DIR_CONN(conn));
823
      break;
824
    case CONN_TYPE_OR:
825
    case CONN_TYPE_EXT_OR:
826
      connection_or_about_to_close(TO_OR_CONN(conn));
827
      break;
828
    case CONN_TYPE_AP:
829
      connection_ap_about_to_close(TO_ENTRY_CONN(conn));
830
      break;
831
    case CONN_TYPE_EXIT:
832
      connection_exit_about_to_close(TO_EDGE_CONN(conn));
833
      break;
834
  }
835
836
}

Roger Dingledine's avatar
Roger Dingledine committed
837
838
/** Return true iff connection_close_immediate() has been called on this
 * connection. */
839
#define CONN_IS_CLOSED(c) \
840
  ((c)->linked ? ((c)->linked_conn_is_closed) : (! SOCKET_OK(c->s)))
841

842
843
844
/** 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().
845
 */
846
847
void
connection_close_immediate(connection_t *conn)
848
849
{
  assert_connection_ok(conn,0);
850
  if (CONN_IS_CLOSED(conn)) {
851
    log_err(LD_BUG,"Attempt to close already-closed connection.");
852
    tor_fragile_assert();
853
854
    return;
  }
855
  if (conn->outbuf_flushlen) {
Roger Dingledine's avatar
Roger Dingledine committed
856
    log_info(LD_NET,"fd %d, type %s, state %s, %d bytes on outbuf.",
857
             (int)conn->s, conn_type_to_string(conn->type),
Roger Dingledine's avatar
Roger Dingledine committed
858
859
             conn_state_to_string(conn->type, conn->state),
             (int)conn->outbuf_flushlen);
860
  }
861

862
  connection_unregister_events(conn);
863

864
  /* Prevent the event from getting unblocked. */
865
866
  conn->read_blocked_on_bw = 0;
  conn->write_blocked_on_bw = 0;
867

868
869
870
  if (SOCKET_OK(conn->s))
    tor_close_socket(conn->s);
  conn->s = TOR_INVALID_SOCKET;
871
872
  if (conn->linked)
    conn->linked_conn_is_closed = 1;
873
  if (conn->outbuf)
874
    buf_clear(conn->outbuf);
875
  conn->outbuf_flushlen = 0;
876
877
}

878
/** Mark <b>conn</b> to be closed next time we loop through
879
 * conn_close_if_marked() in main.c. */
880
void
881
connection_mark_for_close_(connection_t *conn, int line, const char *file)
882
883
{
  assert_connection_ok(conn,0);
884
  tor_assert(line);
885
  tor_assert(line < 1<<16); /* marked_for_close can only fit a uint16_t. */
886
  tor_assert(file);
887

888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
  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
906
907
908
909
910
911
 * conn_close_if_marked() in main.c.
 *
 * This _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
912
 * connection_or_close_for_error() or connection_or_close_normally() function.
913
914
915
916
917
918
 * For all other cases, use connection_mark_and_flush() which checks for
 * or_connection_t properly, instead.  See below.
 *
 * We want to keep this function simple and quick, since it can be called from
 * quite deep in the call chain, and hence it should avoid having side-effects
 * that interfere with its callers view of the connection.
919
 */
920
921
922
MOCK_IMPL(void,
connection_mark_for_close_internal_, (connection_t *conn,
                                      int line, const char *file))
923
924
925
926
927
928
{
  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);

929
  if (conn->marked_for_close) {
930
    log_warn(LD_BUG,"Duplicate call to connection_mark_for_close at %s:%d"
931
932
        " (first at %s:%d)", file, line, conn->marked_for_close_file,
        conn->marked_for_close);
933
    tor_fragile_assert();
934
    return;
935
936
  }

937
938
939
940
941
942
  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,
943
944
              "Calling connection_mark_for_close_internal_() on an OR conn "
              "at %s:%d",
945
946
947
              file, line);
  }

948
949
  conn->marked_for_close = line;
  conn->marked_for_close_file = file;
950
  add_connection_to_closeable_list(conn);
951
952
953
954

  /* 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 */
955
  conn->timestamp_last_write_allowed = time(NULL);
956
957
}

958
959
960
961
962
/** 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.
 */
963
964
void
connection_expire_held_open(void)
965
966
{
  time_t now;
967
  smartlist_t *conns = get_connection_array();
968
969
970

  now = time(NULL);

971
  SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
972
973
974
    /* If we've been holding the connection open, but we haven't written
     * for 15 seconds...
     */
975
    if (conn->hold_open_until_flushed) {
Roger Dingledine's avatar
Roger Dingledine committed
976
      tor_assert(conn->marked_for_close);
977
      if (now - conn->timestamp_last_write_allowed >= 15) {
Roger Dingledine's avatar
Roger Dingledine committed
978
979
        int severity;
        if (conn->type == CONN_TYPE_EXIT ||
980
981
            (conn->type == CONN_TYPE_DIR &&
             conn->purpose == DIR_PURPOSE_SERVER))
Roger Dingledine's avatar
Roger Dingledine committed
982
983
984
          severity = LOG_INFO;
        else
          severity = LOG_NOTICE;
985
        log_fn(severity, LD_NET,
986
987
               "Giving up on marked_for_close conn that's been flushing "
               "for 15s (fd %d, type %s, state %s).",
988
               (int)conn->s, conn_type_to_string(conn->type),
989
               conn_state_to_string(conn->type, conn->state));
990
991
        conn->hold_open_until_flushed = 0;
      }
992
    }
993
  } SMARTLIST_FOREACH_END(conn);