connection.c 182 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
#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"
88
#include "core/mainloop/mainloop.h"
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#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

rl1987's avatar
rl1987 committed
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
 * 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
149
150
151
 *
 * 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
152
 */
153
154
#define ENABLE_LISTENER_REBIND

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

178
179
180
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);
181
static const char *proxy_type_to_string(int proxy_type);
182
static int get_proxy_type(void);
183
184
const tor_addr_t *conn_get_outbound_address(sa_family_t family,
                  const or_options_t *options, unsigned int conn_type);
185
186
static void reenable_blocked_connection_init(const or_options_t *options);
static void reenable_blocked_connection_schedule(void);
187

188
189
190
/** 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.
 *
191
 * XXXX+ We should really use the entire list of interfaces here.
192
193
 **/
static tor_addr_t *last_interface_ipv4 = NULL;
194
/* DOCDOC last_interface_ipv6 */
195
196
static tor_addr_t *last_interface_ipv6 = NULL;
/** A list of tor_addr_t for addresses we've used in outgoing connections.
197
 * Used to detect IP address changes. */
198
static smartlist_t *outgoing_addrs = NULL;
199

200
201
#define CASE_ANY_LISTENER_TYPE \
    case CONN_TYPE_OR_LISTENER: \
202
    case CONN_TYPE_EXT_OR_LISTENER: \
203
204
205
206
207
    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: \
208
209
    case CONN_TYPE_AP_DNS_LISTENER: \
    case CONN_TYPE_AP_HTTP_CONNECT_LISTENER
210

211
212
/**************************************************************/

213
214
215
216
217
218
219
220
221
/** 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);
}

222
223
224
225
226
227
228
229
230
231
232
233
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;
}

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

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

345
  log_warn(LD_BUG, "unknown connection state %d (type %d)", state, type);
346
347
348
349
350
351
  tor_snprintf(buf, sizeof(buf),
               "unknown state [%d] on unknown [%s] connection",
               state, conn_type_to_string(type));
  return buf;
}

352
353
/** Allocate and return a new dir_connection_t, initialized as by
 * connection_init(). */
354
355
356
357
358
359
360
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;
}
361
362

/** Allocate and return a new or_connection_t, initialized as by
Nick Mathewson's avatar
Nick Mathewson committed
363
 * connection_init().
364
365
366
367
368
 *
 * Initialize active_circuit_pqueue.
 *
 * Set active_circuit_pqueue_last_recalibrated to current cell_ewma tick.
 */
369
or_connection_t *
370
or_connection_new(int type, int socket_family)
371
372
373
{
  or_connection_t *or_conn = tor_malloc_zero(sizeof(or_connection_t));
  time_t now = time(NULL);
374
375
  tor_assert(type == CONN_TYPE_OR || type == CONN_TYPE_EXT_OR);
  connection_init(now, TO_CONN(or_conn), type, socket_family);
376

377
  connection_or_set_canonical(or_conn, 0);
378

379
380
381
  if (type == CONN_TYPE_EXT_OR)
    connection_or_set_ext_or_identifier(or_conn);

382
383
  return or_conn;
}
384

385
/** Allocate and return a new entry_connection_t, initialized as by
386
387
388
389
 * connection_init().
 *
 * Allocate space to store the socks_request.
 */
390
391
392
393
394
395
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);
396
  entry_conn->socks_request = socks_request_new();
397
398
399
400
  /* 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)
401
    entry_conn->entry_cfg.ipv4_traffic = 1;
402
  else if (socket_family == AF_INET6)
403
    entry_conn->entry_cfg.ipv6_traffic = 1;
404
405
406
  return entry_conn;
}

407
408
/** Allocate and return a new edge_connection_t, initialized as by
 * connection_init(). */
409
410
411
412
edge_connection_t *
edge_connection_new(int type, int socket_family)
{
  edge_connection_t *edge_conn = tor_malloc_zero(sizeof(edge_connection_t));
413
  tor_assert(type == CONN_TYPE_EXIT);
414
415
416
  connection_init(time(NULL), TO_CONN(edge_conn), type, socket_family);
  return edge_conn;
}
417
418
419

/** Allocate and return a new control_connection_t, initialized as by
 * connection_init(). */
420
421
422
423
424
425
426
427
428
429
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;
}

430
431
432
433
434
435
436
437
438
439
440
/** 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;
}

441
442
443
/** 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. */
444
445
446
447
448
connection_t *
connection_new(int type, int socket_family)
{
  switch (type) {
    case CONN_TYPE_OR:
449
450
    case CONN_TYPE_EXT_OR:
      return TO_CONN(or_connection_new(type, socket_family));
451
452
453
454

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

455
456
457
    case CONN_TYPE_AP:
      return ENTRY_TO_CONN(entry_connection_new(type, socket_family));

458
459
460
461
462
463
    case CONN_TYPE_DIR:
      return TO_CONN(dir_connection_new(socket_family));

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

464
465
466
    CASE_ANY_LISTENER_TYPE:
      return TO_CONN(listener_connection_new(type, socket_family));

467
468
469
470
471
472
473
474
475
476
    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).
477
 *
478
479
 * Set conn-\>magic to the correct value.
 *
480
 * Set conn-\>type to <b>type</b>. Set conn-\>s and conn-\>conn_array_index to
481
482
483
484
 * -1 to signify they are not yet assigned.
 *
 * Initialize conn's timestamps to now.
 */
485
486
static void
connection_init(time_t now, connection_t *conn, int type, int socket_family)
487
{
488
489
  static uint64_t n_connections_allocated = 1;

490
491
  switch (type) {
    case CONN_TYPE_OR:
492
    case CONN_TYPE_EXT_OR:
493
      conn->magic = OR_CONNECTION_MAGIC;
494
495
      break;
    case CONN_TYPE_EXIT:
496
      conn->magic = EDGE_CONNECTION_MAGIC;
497
498
      break;
    case CONN_TYPE_AP:
499
      conn->magic = ENTRY_CONNECTION_MAGIC;
500
501
      break;
    case CONN_TYPE_DIR:
502
      conn->magic = DIR_CONNECTION_MAGIC;
503
504
      break;
    case CONN_TYPE_CONTROL:
505
      conn->magic = CONTROL_CONNECTION_MAGIC;
506
      break;
507
508
    CASE_ANY_LISTENER_TYPE:
      conn->magic = LISTENER_CONNECTION_MAGIC;
509
      break;
510
    default:
511
      conn->magic = BASE_CONNECTION_MAGIC;
512
513
514
      break;
  }

515
  conn->s = TOR_INVALID_SOCKET; /* give it a default of 'not used' */
516
  conn->conn_array_index = -1; /* also default to 'not used' */
517
  conn->global_identifier = n_connections_allocated++;
Roger Dingledine's avatar
Roger Dingledine committed
518
519

  conn->type = type;
520
  conn->socket_family = socket_family;
521
522
  if (!connection_is_listener(conn)) {
    /* listeners never use their buf */
523
524
525
    conn->inbuf = buf_new();
    conn->outbuf = buf_new();
  }
526

527
  conn->timestamp_created = now;
528
529
  conn->timestamp_last_read_allowed = now;
  conn->timestamp_last_write_allowed = now;
Roger Dingledine's avatar
Roger Dingledine committed
530
531
}

Roger Dingledine's avatar
Roger Dingledine committed
532
/** Create a link between <b>conn_a</b> and <b>conn_b</b>. */
533
534
535
void
connection_link_connections(connection_t *conn_a, connection_t *conn_b)
{
536
537
  tor_assert(! SOCKET_OK(conn_a->s));
  tor_assert(! SOCKET_OK(conn_b->s));
538
539
540
541
542
543
544

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

545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
/** 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;
  }
}

561
562
563
/** 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.
564
 */
Nick Mathewson's avatar
Nick Mathewson committed
565
STATIC void
566
connection_free_minimal(connection_t *conn)
567
{
568
  void *mem;
569
  size_t memlen;
570
571
572
  if (!conn)
    return;

573
574
  switch (conn->type) {
    case CONN_TYPE_OR:
575
    case CONN_TYPE_EXT_OR:
576
577
      tor_assert(conn->magic == OR_CONNECTION_MAGIC);
      mem = TO_OR_CONN(conn);
578
      memlen = sizeof(or_connection_t);
579
580
      break;
    case CONN_TYPE_AP:
581
582
583
584
      tor_assert(conn->magic == ENTRY_CONNECTION_MAGIC);
      mem = TO_ENTRY_CONN(conn);
      memlen = sizeof(entry_connection_t);
      break;
585
586
587
    case CONN_TYPE_EXIT:
      tor_assert(conn->magic == EDGE_CONNECTION_MAGIC);
      mem = TO_EDGE_CONN(conn);
588
      memlen = sizeof(edge_connection_t);
589
590
591
592
      break;
    case CONN_TYPE_DIR:
      tor_assert(conn->magic == DIR_CONNECTION_MAGIC);
      mem = TO_DIR_CONN(conn);
593
      memlen = sizeof(dir_connection_t);
594
595
596
597
      break;
    case CONN_TYPE_CONTROL:
      tor_assert(conn->magic == CONTROL_CONNECTION_MAGIC);
      mem = TO_CONTROL_CONN(conn);
598
      memlen = sizeof(control_connection_t);
599
      break;
600
601
602
603
604
    CASE_ANY_LISTENER_TYPE:
      tor_assert(conn->magic == LISTENER_CONNECTION_MAGIC);
      mem = TO_LISTENER_CONN(conn);
      memlen = sizeof(listener_connection_t);
      break;
605
606
607
    default:
      tor_assert(conn->magic == BASE_CONNECTION_MAGIC);
      mem = conn;
608
      memlen = sizeof(connection_t);
609
610
      break;
  }
Roger Dingledine's avatar
Roger Dingledine committed
611

612
  if (conn->linked) {
613
614
615
616
    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),
617
618
             (int)connection_get_inbuf_len(conn),
             (int)connection_get_outbuf_len(conn));
619
620
  }

621
  if (!connection_is_listener(conn)) {
622
623
    buf_free(conn->inbuf);
    buf_free(conn->outbuf);
624
  } else {
625
    if (conn->socket_family == AF_UNIX) {
626
      /* For now only control and SOCKS ports can be Unix domain sockets
627
       * and listeners at the same time */
628
      tor_assert(conn_listener_type_supports_af_unix(conn->type));
629
630
631
632
633
634

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

637
  tor_free(conn->address);
Roger Dingledine's avatar
Roger Dingledine committed
638

639
  if (connection_speaks_cells(conn)) {
640
    or_connection_t *or_conn = TO_OR_CONN(conn);
641
642
643
644
645
646
647
648
    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. */
649
        tor_release_socket_ownership(conn->s);
650
651
652
653
654
        conn->s = TOR_INVALID_SOCKET;
      }
      tor_tls_free(or_conn->tls);
      or_conn->tls = NULL;
    }
655
656
    or_handshake_state_free(or_conn->handshake_state);
    or_conn->handshake_state = NULL;
657
    tor_free(or_conn->nickname);
658
659
660
661
    if (or_conn->chan) {
      /* Owww, this shouldn't happen, but... */
      log_info(LD_CHANNEL,
               "Freeing orconn at %p, saw channel %p with ID "
662
               "%"PRIu64 " left un-NULLed",
663
               or_conn, TLS_CHAN_TO_BASE(or_conn->chan),
664
               (
665
                 TLS_CHAN_TO_BASE(or_conn->chan)->global_identifier));
666
      if (!CHANNEL_FINISHED(TLS_CHAN_TO_BASE(or_conn->chan))) {
667
668
669
670
671
672
        channel_close_for_error(TLS_CHAN_TO_BASE(or_conn->chan));
      }

      or_conn->chan->conn = NULL;
      or_conn->chan = NULL;
    }
673
  }
674
675
676
677
678
679
680
  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) {
681
      buf_free(entry_conn->pending_optimistic_data);
682
    }
683
    if (entry_conn->sending_optimistic_data) {
684
      buf_free(entry_conn->sending_optimistic_data);
685
    }
686
687
688
  }
  if (CONN_IS_EDGE(conn)) {
    rend_data_free(TO_EDGE_CONN(conn)->rend_data);
689
    hs_ident_edge_conn_free(TO_EDGE_CONN(conn)->hs_ident);
690
691
692
  }
  if (conn->type == CONN_TYPE_CONTROL) {
    control_connection_t *control_conn = TO_CONTROL_CONN(conn);
693
    tor_free(control_conn->safecookie_client_hash);
694
    tor_free(control_conn->incoming_cmd);
695
696
697
698
699
700
701
    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
702
703
  }

704
705
706
707
  /* 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;
708
709
710
711

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

713
    tor_compress_free(dir_conn->compress_state);
714
715
716
717
    if (dir_conn->spool) {
      SMARTLIST_FOREACH(dir_conn->spool, spooled_resource_t *, spooled,
                        spooled_resource_free(spooled));
      smartlist_free(dir_conn->spool);
718
    }
719
720

    rend_data_free(dir_conn->rend_data);
721
    hs_ident_dir_conn_free(dir_conn->hs_ident);
722
723
    if (dir_conn->guard_state) {
      /* Cancel before freeing, if it's still there. */
724
      entry_guard_cancel(&dir_conn->guard_state);
725
    }
726
    circuit_guard_state_free(dir_conn->guard_state);
727
  }
728

729
  if (SOCKET_OK(conn->s)) {
730
731
732
    log_debug(LD_NET,"closing fd %d.",(int)conn->s);
    tor_close_socket(conn->s);
    conn->s = TOR_INVALID_SOCKET;
733
734
  }

735
  if (conn->type == CONN_TYPE_OR &&
736
      !tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest)) {
Roger Dingledine's avatar
Roger Dingledine committed
737
    log_warn(LD_BUG, "called on OR conn with non-zeroed identity_digest");
738
    connection_or_clear_identity(TO_OR_CONN(conn));
739
  }
740
  if (conn->type == CONN_TYPE_OR || conn->type == CONN_TYPE_EXT_OR) {
741
    connection_or_remove_from_ext_or_id_map(TO_OR_CONN(conn));
742
    tor_free(TO_OR_CONN(conn)->ext_or_conn_id);
743
    tor_free(TO_OR_CONN(conn)->ext_or_auth_correct_client_hash);
744
    tor_free(TO_OR_CONN(conn)->ext_or_transport);
745
  }
746

747
  memwipe(mem, 0xCC, memlen); /* poison memory */
748
  tor_free(mem);
Roger Dingledine's avatar
Roger Dingledine committed
749
750
}

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

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

789
  connection_unregister_events(conn);
790
  connection_free_minimal(conn);
791
792
}

793
794
795
/**
 * Called when we're about to finally unlink and free a connection:
 * perform necessary accounting and cleanup
796
797
798
799
800
801
802
 *   - 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.
803
 *   - OR and edge connections need to be unlinked from circuits.
804
 */
805
806
void
connection_about_to_close_connection(connection_t *conn)
807
{
808
  tor_assert(conn->marked_for_close);
809

810
  switch (conn->type) {
811
    case CONN_TYPE_DIR:
812
      connection_dir_about_to_close(TO_DIR_CONN(conn));
813
      break;
814
    case CONN_TYPE_OR:
815
    case CONN_TYPE_EXT_OR:
816
      connection_or_about_to_close(TO_OR_CONN(conn));
817
      break;
818
    case CONN_TYPE_AP:
819
      connection_ap_about_to_close(TO_ENTRY_CONN(conn));
820
      break;
821
    case CONN_TYPE_EXIT:
822
      connection_exit_about_to_close(TO_EDGE_CONN(conn));
823
      break;
824
  }
825
826
}

Roger Dingledine's avatar
Roger Dingledine committed
827
828
/** Return true iff connection_close_immediate() has been called on this
 * connection. */
829
#define CONN_IS_CLOSED(c) \
830
  ((c)->linked ? ((c)->linked_conn_is_closed) : (! SOCKET_OK(c->s)))
831

832
833
834
/** 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().
835
 */
836
837
void
connection_close_immediate(connection_t *conn)
838
839
{
  assert_connection_ok(conn,0);
840
  if (CONN_IS_CLOSED(conn)) {
841
    log_err(LD_BUG,"Attempt to close already-closed connection.");
842
    tor_fragile_assert();
843
844
    return;
  }
845
  if (conn->outbuf_flushlen) {
Roger Dingledine's avatar
Roger Dingledine committed
846
    log_info(LD_NET,"fd %d, type %s, state %s, %d bytes on outbuf.",
847
             (int)conn->s, conn_type_to_string(conn->type),
Roger Dingledine's avatar
Roger Dingledine committed
848
849
             conn_state_to_string(conn->type, conn->state),
             (int)conn->outbuf_flushlen);
850
  }
851

852
  connection_unregister_events(conn);
853

854
  /* Prevent the event from getting unblocked. */
855
856
  conn->read_blocked_on_bw = 0;
  conn->write_blocked_on_bw = 0;
857

858
859
860
  if (SOCKET_OK(conn->s))
    tor_close_socket(conn->s);
  conn->s = TOR_INVALID_SOCKET;
861
862
  if (conn->linked)
    conn->linked_conn_is_closed = 1;
863
  if (conn->outbuf)
864
    buf_clear(conn->outbuf);
865
  conn->outbuf_flushlen = 0;
866
867
}

868
/** Mark <b>conn</b> to be closed next time we loop through
869
 * conn_close_if_marked() in main.c. */
870
void
871
connection_mark_for_close_(connection_t *conn, int line, const char *file)
872
873
{
  assert_connection_ok(conn,0);
874
  tor_assert(line);
875
  tor_assert(line < 1<<16); /* marked_for_close can only fit a uint16_t. */
876
  tor_assert(file);
877

878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
  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
900
 * connection_or_close_for_error() or connection_or_close_normally() function.
901
902
903
 * For all other cases, use connection_mark_and_flush() instead, which
 * checks for or_connection_t properly, instead.  See below.
 */
904
905
906
MOCK_IMPL(void,
connection_mark_for_close_internal_, (connection_t *conn,
                                      int line, const char *file))
907
908
909
910
911
912
{
  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);

913
  if (conn->marked_for_close) {
914
    log_warn(LD_BUG,"Duplicate call to connection_mark_for_close at %s:%d"
915
916
        " (first at %s:%d)", file, line, conn->marked_for_close_file,
        conn->marked_for_close);
917
    tor_fragile_assert();
918
    return;
919
920
  }

921
922
923
924
925
926
  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,
927
928
              "Calling connection_mark_for_close_internal_() on an OR conn "
              "at %s:%d",
929
930
931
              file, line);
  }

932
933
  conn->marked_for_close = line;
  conn->marked_for_close_file = file;
934
  add_connection_to_closeable_list(conn);
935
936
937
938

  /* 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 */
939
  conn->timestamp_last_write_allowed = time(NULL);
940
941
}

942
943
944
945
946
/** 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.
 */
947
948
void
connection_expire_held_open(void)
949
950
{
  time_t now;
951
  smartlist_t *conns = get_connection_array();
952
953
954

  now = time(NULL);

955
  SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
956
957
958
    /* If we've been holding the connection open, but we haven't written
     * for 15 seconds...
     */
959
    if (conn->hold_open_until_flushed) {
Roger Dingledine's avatar
Roger Dingledine committed
960
      tor_assert(conn->marked_for_close);
961
      if (now - conn->timestamp_last_write_allowed >= 15) {
Roger Dingledine's avatar
Roger Dingledine committed
962
963
        int severity;
        if (conn->type == CONN_TYPE_EXIT ||
964
965
            (conn->type == CONN_TYPE_DIR &&
             conn->purpose == DIR_PURPOSE_SERVER))
Roger Dingledine's avatar
Roger Dingledine committed
966
967
968
          severity = LOG_INFO;
        else
          severity = LOG_NOTICE;
969
        log_fn(severity, LD_NET,
970
971
               "Giving up on marked_for_close conn that's been flushing "
               "for 15s (fd %d, type %s, state %s).",
972
               (int)conn->s, conn_type_to_string(conn->type),
973
               conn_state_to_string(conn->type, conn->state));
974
975
        conn->hold_open_until_flushed = 0;
      }
976
    }
977
  } SMARTLIST_FOREACH_END(conn);
978
979
}

980
#if defined(HAVE_SYS_UN_H) || defined(RUNNING_DOXYGEN)
981
/** Create an AF_UNIX listenaddr struct.
Nick Mathewson's avatar
Nick Mathewson committed
982
 * <b>listenaddress</b> provides the path to the Unix socket.
983
984
985
986
987
988
 *
 * 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
989
 * If not NULL <b>readable_address</b> will contain a copy of the path part of
990
991
992
993
994
 * <b>listenaddress</b>.
 *
 * The listenaddr struct has to be freed by the caller.
 */
static struct sockaddr_un *
995
996
create_unix_sockaddr(const char *listenaddress, char **readable_address,
                     socklen_t *len_out)
997
998
999
1000
{
  struct sockaddr_un *sockaddr = NULL;

  sockaddr = tor_malloc_zero(sizeof(struct sockaddr_un));
For faster browsing, not all history is shown. View entire blame