connection.c 141 KB
Newer Older
Roger Dingledine's avatar
Roger Dingledine committed
1
2
/* Copyright (c) 2001 Matej Pfajfar.
 * Copyright (c) 2001-2004, Roger Dingledine.
3
 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4
 * Copyright (c) 2007-2012, 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
 **/

Roger Dingledine's avatar
Roger Dingledine committed
13
#include "or.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
14
#include "buffers.h"
15
16
17
18
/*
 * Define this so we get channel internal functions, since we're implementing
 * part of a subclass (channel_tls_t).
 */
19
#define TOR_CHANNEL_INTERNAL_
20
21
#include "channel.h"
#include "channeltls.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
22
#include "circuitbuild.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
23
#include "circuitlist.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
24
#include "circuituse.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
25
#include "config.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
26
#include "connection.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
27
#include "connection_edge.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
28
#include "connection_or.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
29
#include "control.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
30
#include "cpuworker.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
31
#include "directory.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
32
#include "dirserv.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
33
#include "dns.h"
34
#include "dnsserv.h"
35
#include "entrynodes.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
36
#include "geoip.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
37
#include "main.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
38
#include "policies.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
39
#include "reasons.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
40
#include "relay.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
41
#include "rendclient.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
42
#include "rendcommon.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
43
#include "rephist.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
44
#include "router.h"
George Kadianakis's avatar
George Kadianakis committed
45
#include "transports.h"
Sebastian Hahn's avatar
Sebastian Hahn committed
46
#include "routerparse.h"
Roger Dingledine's avatar
Roger Dingledine committed
47

48
49
50
51
#ifdef USE_BUFFEREVENTS
#include <event2/event.h>
#endif

52
53
54
55
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif

56
static connection_t *connection_listener_new(
57
                               const struct sockaddr *listensockaddr,
58
                               socklen_t listensocklen, int type,
59
60
                               const char *address,
                               const port_cfg_t *portcfg);
61
62
static void connection_init(time_t now, connection_t *conn, int type,
                            int socket_family);
63
static int connection_init_accepted_conn(connection_t *conn,
64
                          const listener_connection_t *listener);
Nick Mathewson's avatar
Nick Mathewson committed
65
static int connection_handle_listener_read(connection_t *conn, int new_type);
66
#ifndef USE_BUFFEREVENTS
67
68
static int connection_bucket_should_increase(int bucket,
                                             or_connection_t *conn);
69
#endif
70
static int connection_finished_flushing(connection_t *conn);
71
static int connection_flushed_some(connection_t *conn);
72
static int connection_finished_connecting(connection_t *conn);
73
static int connection_reached_eof(connection_t *conn);
74
static int connection_read_to_buf(connection_t *conn, ssize_t *max_to_read,
75
                                  int *socket_error);
76
static int connection_process_inbuf(connection_t *conn, int package_partial);
77
78
static void client_check_address_changed(tor_socket_t sock);
static void set_constrained_socket_buffers(tor_socket_t sock, int size);
79

80
81
82
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);
83
static const char *proxy_type_to_string(int proxy_type);
84
static int get_proxy_type(void);
85

86
87
88
89
90
91
/** 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.
 *
 * XXX024 We should really use the entire list of interfaces here.
 **/
static tor_addr_t *last_interface_ipv4 = NULL;
92
/* DOCDOC last_interface_ipv6 */
93
94
static tor_addr_t *last_interface_ipv6 = NULL;
/** A list of tor_addr_t for addresses we've used in outgoing connections.
95
 * Used to detect IP address changes. */
96
static smartlist_t *outgoing_addrs = NULL;
97

98
99
100
101
102
103
104
105
106
#define CASE_ANY_LISTENER_TYPE \
    case CONN_TYPE_OR_LISTENER: \
    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: \
    case CONN_TYPE_AP_DNS_LISTENER

107
108
/**************************************************************/

109
110
111
/**
 * Return the human-readable name for the connection type <b>type</b>
 */
112
113
114
115
116
117
118
119
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";
120
    case CONN_TYPE_AP_LISTENER: return "Socks listener";
121
122
123
    case CONN_TYPE_AP_TRANS_LISTENER:
      return "Transparent pf/netfilter listener";
    case CONN_TYPE_AP_NATD_LISTENER: return "Transparent natd listener";
124
    case CONN_TYPE_AP_DNS_LISTENER: return "DNS listener";
125
126
127
    case CONN_TYPE_AP: return "Socks";
    case CONN_TYPE_DIR_LISTENER: return "Directory listener";
    case CONN_TYPE_DIR: return "Directory";
128
129
130
131
    case CONN_TYPE_CPUWORKER: return "CPU worker";
    case CONN_TYPE_CONTROL_LISTENER: return "Control listener";
    case CONN_TYPE_CONTROL: return "Control";
    default:
132
      log_warn(LD_BUG, "unknown connection type %d", type);
133
134
135
136
137
      tor_snprintf(buf, sizeof(buf), "unknown [%d]", type);
      return buf;
  }
}

138
139
140
141
/**
 * Return the human-readable name for the connection state <b>state</b>
 * for the connection type <b>type</b>
 */
142
const char *
143
144
conn_state_to_string(int type, int state)
{
145
146
  static char buf[96];
  switch (type) {
147
    CASE_ANY_LISTENER_TYPE:
148
149
150
151
152
153
      if (state == LISTENER_STATE_READY)
        return "ready";
      break;
    case CONN_TYPE_OR:
      switch (state) {
        case OR_CONN_STATE_CONNECTING: return "connect()ing";
154
        case OR_CONN_STATE_PROXY_HANDSHAKING: return "handshaking (proxy)";
155
        case OR_CONN_STATE_TLS_HANDSHAKING: return "handshaking (TLS)";
156
        case OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING:
157
          return "renegotiating (TLS, v2 handshake)";
158
        case OR_CONN_STATE_TLS_SERVER_RENEGOTIATING:
159
160
161
162
163
          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)";
164
165
166
167
168
169
170
171
172
173
174
175
176
        case OR_CONN_STATE_OPEN: return "open";
      }
      break;
    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) {
177
        case AP_CONN_STATE_SOCKS_WAIT: return "waiting for socks info";
178
        case AP_CONN_STATE_NATD_WAIT: return "waiting for natd dest info";
179
180
        case AP_CONN_STATE_RENDDESC_WAIT: return "waiting for rendezvous desc";
        case AP_CONN_STATE_CONTROLLER_WAIT: return "waiting for controller";
181
182
183
        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";
184
185
186
187
188
189
190
        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
191
        case DIR_CONN_STATE_CLIENT_READING: return "client reading";
192
        case DIR_CONN_STATE_CLIENT_FINISHED: return "client finished";
193
194
195
196
197
198
199
200
201
202
203
204
        case DIR_CONN_STATE_SERVER_COMMAND_WAIT: return "waiting for command";
        case DIR_CONN_STATE_SERVER_WRITING: return "writing";
      }
      break;
    case CONN_TYPE_CPUWORKER:
      switch (state) {
        case CPUWORKER_STATE_IDLE: return "idle";
        case CPUWORKER_STATE_BUSY_ONION: return "busy with onion";
      }
      break;
    case CONN_TYPE_CONTROL:
      switch (state) {
205
206
        case CONTROL_CONN_STATE_OPEN: return "open (protocol v1)";
        case CONTROL_CONN_STATE_NEEDAUTH:
207
          return "waiting for authentication (protocol v1)";
208
209
210
211
      }
      break;
  }

212
  log_warn(LD_BUG, "unknown connection state %d (type %d)", state, type);
213
214
215
216
217
218
  tor_snprintf(buf, sizeof(buf),
               "unknown state [%d] on unknown [%s] connection",
               state, conn_type_to_string(type));
  return buf;
}

219
220
221
222
223
224
225
226
#ifdef USE_BUFFEREVENTS
/** Return true iff the connection's type is one that can use a
    bufferevent-based implementation. */
int
connection_type_uses_bufferevent(connection_t *conn)
{
  switch (conn->type) {
    case CONN_TYPE_AP:
227
    case CONN_TYPE_EXIT:
228
    case CONN_TYPE_DIR:
229
    case CONN_TYPE_CONTROL:
230
    case CONN_TYPE_OR:
231
    case CONN_TYPE_CPUWORKER:
232
      return 1;
233
234
235
236
237
238
    default:
      return 0;
  }
}
#endif

239
240
/** Allocate and return a new dir_connection_t, initialized as by
 * connection_init(). */
241
242
243
244
245
246
247
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;
}
248
249

/** Allocate and return a new or_connection_t, initialized as by
Nick Mathewson's avatar
Nick Mathewson committed
250
 * connection_init().
251
252
253
254
255
256
257
258
259
 *
 * Set timestamp_last_added_nonpadding to now.
 *
 * Assign a pseudorandom next_circ_id between 0 and 2**15.
 *
 * Initialize active_circuit_pqueue.
 *
 * Set active_circuit_pqueue_last_recalibrated to current cell_ewma tick.
 */
260
261
262
263
264
265
266
267
268
269
270
or_connection_t *
or_connection_new(int socket_family)
{
  or_connection_t *or_conn = tor_malloc_zero(sizeof(or_connection_t));
  time_t now = time(NULL);
  connection_init(now, TO_CONN(or_conn), CONN_TYPE_OR, socket_family);

  or_conn->timestamp_last_added_nonpadding = time(NULL);

  return or_conn;
}
271

272
/** Allocate and return a new entry_connection_t, initialized as by
273
274
275
276
 * connection_init().
 *
 * Allocate space to store the socks_request.
 */
277
278
279
280
281
282
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);
283
  entry_conn->socks_request = socks_request_new();
284
285
286
  return entry_conn;
}

287
288
/** Allocate and return a new edge_connection_t, initialized as by
 * connection_init(). */
289
290
291
292
edge_connection_t *
edge_connection_new(int type, int socket_family)
{
  edge_connection_t *edge_conn = tor_malloc_zero(sizeof(edge_connection_t));
293
  tor_assert(type == CONN_TYPE_EXIT);
294
295
296
  connection_init(time(NULL), TO_CONN(edge_conn), type, socket_family);
  return edge_conn;
}
297
298
299

/** Allocate and return a new control_connection_t, initialized as by
 * connection_init(). */
300
301
302
303
304
305
306
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);
307
  log_notice(LD_CONTROL, "New control connection opened.");
308
309
310
  return control_conn;
}

311
312
313
314
315
316
317
318
319
320
321
/** 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;
}

322
323
324
/** 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. */
325
326
327
328
329
330
331
332
333
334
connection_t *
connection_new(int type, int socket_family)
{
  switch (type) {
    case CONN_TYPE_OR:
      return TO_CONN(or_connection_new(socket_family));

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

335
336
337
    case CONN_TYPE_AP:
      return ENTRY_TO_CONN(entry_connection_new(type, socket_family));

338
339
340
341
342
343
    case CONN_TYPE_DIR:
      return TO_CONN(dir_connection_new(socket_family));

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

344
345
346
    CASE_ANY_LISTENER_TYPE:
      return TO_CONN(listener_connection_new(type, socket_family));

347
348
349
350
351
352
353
354
355
356
    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).
357
 *
358
359
 * Set conn-\>magic to the correct value.
 *
360
 * Set conn-\>type to <b>type</b>. Set conn-\>s and conn-\>conn_array_index to
361
362
363
364
 * -1 to signify they are not yet assigned.
 *
 * Initialize conn's timestamps to now.
 */
365
366
static void
connection_init(time_t now, connection_t *conn, int type, int socket_family)
367
{
368
369
  static uint64_t n_connections_allocated = 1;

370
371
  switch (type) {
    case CONN_TYPE_OR:
372
      conn->magic = OR_CONNECTION_MAGIC;
373
374
      break;
    case CONN_TYPE_EXIT:
375
      conn->magic = EDGE_CONNECTION_MAGIC;
376
377
      break;
    case CONN_TYPE_AP:
378
      conn->magic = ENTRY_CONNECTION_MAGIC;
379
380
      break;
    case CONN_TYPE_DIR:
381
      conn->magic = DIR_CONNECTION_MAGIC;
382
383
      break;
    case CONN_TYPE_CONTROL:
384
      conn->magic = CONTROL_CONNECTION_MAGIC;
385
      break;
386
387
    CASE_ANY_LISTENER_TYPE:
      conn->magic = LISTENER_CONNECTION_MAGIC;
388
      break;
389
    default:
390
      conn->magic = BASE_CONNECTION_MAGIC;
391
392
393
      break;
  }

394
  conn->s = TOR_INVALID_SOCKET; /* give it a default of 'not used' */
395
  conn->conn_array_index = -1; /* also default to 'not used' */
396
  conn->global_identifier = n_connections_allocated++;
Roger Dingledine's avatar
Roger Dingledine committed
397
398

  conn->type = type;
399
  conn->socket_family = socket_family;
400
#ifndef USE_BUFFEREVENTS
401
402
  if (!connection_is_listener(conn)) {
    /* listeners never use their buf */
403
404
405
    conn->inbuf = buf_new();
    conn->outbuf = buf_new();
  }
406
#endif
407

408
409
410
  conn->timestamp_created = now;
  conn->timestamp_lastread = now;
  conn->timestamp_lastwritten = now;
Roger Dingledine's avatar
Roger Dingledine committed
411
412
}

Roger Dingledine's avatar
Roger Dingledine committed
413
/** Create a link between <b>conn_a</b> and <b>conn_b</b>. */
414
415
416
void
connection_link_connections(connection_t *conn_a, connection_t *conn_b)
{
417
418
  tor_assert(! SOCKET_OK(conn_a->s));
  tor_assert(! SOCKET_OK(conn_b->s));
419
420
421
422
423
424
425

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

426
427
428
/** 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.
429
 */
430
static void
431
connection_free_(connection_t *conn)
432
{
433
  void *mem;
434
  size_t memlen;
435
436
437
  if (!conn)
    return;

438
439
440
441
  switch (conn->type) {
    case CONN_TYPE_OR:
      tor_assert(conn->magic == OR_CONNECTION_MAGIC);
      mem = TO_OR_CONN(conn);
442
      memlen = sizeof(or_connection_t);
443
444
      break;
    case CONN_TYPE_AP:
445
446
447
448
      tor_assert(conn->magic == ENTRY_CONNECTION_MAGIC);
      mem = TO_ENTRY_CONN(conn);
      memlen = sizeof(entry_connection_t);
      break;
449
450
451
    case CONN_TYPE_EXIT:
      tor_assert(conn->magic == EDGE_CONNECTION_MAGIC);
      mem = TO_EDGE_CONN(conn);
452
      memlen = sizeof(edge_connection_t);
453
454
455
456
      break;
    case CONN_TYPE_DIR:
      tor_assert(conn->magic == DIR_CONNECTION_MAGIC);
      mem = TO_DIR_CONN(conn);
457
      memlen = sizeof(dir_connection_t);
458
459
460
461
      break;
    case CONN_TYPE_CONTROL:
      tor_assert(conn->magic == CONTROL_CONNECTION_MAGIC);
      mem = TO_CONTROL_CONN(conn);
462
      memlen = sizeof(control_connection_t);
463
      break;
464
465
466
467
468
    CASE_ANY_LISTENER_TYPE:
      tor_assert(conn->magic == LISTENER_CONNECTION_MAGIC);
      mem = TO_LISTENER_CONN(conn);
      memlen = sizeof(listener_connection_t);
      break;
469
470
471
    default:
      tor_assert(conn->magic == BASE_CONNECTION_MAGIC);
      mem = conn;
472
      memlen = sizeof(connection_t);
473
474
      break;
  }
Roger Dingledine's avatar
Roger Dingledine committed
475

476
  if (conn->linked) {
477
478
479
480
    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),
481
482
             (int)connection_get_inbuf_len(conn),
             (int)connection_get_outbuf_len(conn));
483
484
  }

485
  if (!connection_is_listener(conn)) {
486
487
    buf_free(conn->inbuf);
    buf_free(conn->outbuf);
488
  } else {
489
    if (conn->socket_family == AF_UNIX) {
Nick Mathewson's avatar
Nick Mathewson committed
490
      /* For now only control ports can be Unix domain sockets
491
492
493
494
495
496
497
498
       * and listeners at the same time */
      tor_assert(conn->type == CONN_TYPE_CONTROL_LISTENER);

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

501
  tor_free(conn->address);
Roger Dingledine's avatar
Roger Dingledine committed
502

503
  if (connection_speaks_cells(conn)) {
504
    or_connection_t *or_conn = TO_OR_CONN(conn);
505
506
507
508
    tor_tls_free(or_conn->tls);
    or_conn->tls = NULL;
    or_handshake_state_free(or_conn->handshake_state);
    or_conn->handshake_state = NULL;
509
510
    tor_free(or_conn->nickname);
  }
511
512
513
514
515
516
517
518
  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) {
      generic_buffer_free(entry_conn->pending_optimistic_data);
519
    }
520
521
    if (entry_conn->sending_optimistic_data) {
      generic_buffer_free(entry_conn->sending_optimistic_data);
522
    }
523
524
525
  }
  if (CONN_IS_EDGE(conn)) {
    rend_data_free(TO_EDGE_CONN(conn)->rend_data);
526
527
528
  }
  if (conn->type == CONN_TYPE_CONTROL) {
    control_connection_t *control_conn = TO_CONTROL_CONN(conn);
529
    tor_free(control_conn->safecookie_client_hash);
530
    tor_free(control_conn->incoming_cmd);
Roger Dingledine's avatar
Roger Dingledine committed
531
532
  }

533
534
  tor_free(conn->read_event); /* Probably already freed by connection_free. */
  tor_free(conn->write_event); /* Probably already freed by connection_free. */
535
  IF_HAS_BUFFEREVENT(conn, {
536
537
538
539
      /* This was a workaround to handle bugs in some old versions of libevent
       * where callbacks can occur after calling bufferevent_free().  Setting
       * the callbacks to NULL prevented this.  It shouldn't be necessary any
       * more, but let's not tempt fate for now.  */
540
      bufferevent_setcb(conn->bufev, NULL, NULL, NULL, NULL);
541
542
543
      bufferevent_free(conn->bufev);
      conn->bufev = NULL;
  });
544
545
546
547

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

    tor_zlib_free(dir_conn->zlib_state);
550
551
552
553
    if (dir_conn->fingerprint_stack) {
      SMARTLIST_FOREACH(dir_conn->fingerprint_stack, char *, cp, tor_free(cp));
      smartlist_free(dir_conn->fingerprint_stack);
    }
554
555
556

    cached_dir_decref(dir_conn->cached_dir);
    rend_data_free(dir_conn->rend_data);
557
  }
558

559
560
  if (SOCKET_OK(conn->s)) {
    log_debug(LD_NET,"closing fd %d.",(int)conn->s);
561
    tor_close_socket(conn->s);
562
    conn->s = TOR_INVALID_SOCKET;
563
564
  }

565
  if (conn->type == CONN_TYPE_OR &&
566
      !tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest)) {
Roger Dingledine's avatar
Roger Dingledine committed
567
    log_warn(LD_BUG, "called on OR conn with non-zeroed identity_digest");
568
    connection_or_remove_from_identity_map(TO_OR_CONN(conn));
569
  }
570
571
572
#ifdef USE_BUFFEREVENTS
  if (conn->type == CONN_TYPE_OR && TO_OR_CONN(conn)->bucket_cfg) {
    ev_token_bucket_cfg_free(TO_OR_CONN(conn)->bucket_cfg);
573
    TO_OR_CONN(conn)->bucket_cfg = NULL;
574
575
  }
#endif
576

577
  memset(mem, 0xCC, memlen); /* poison memory */
578
  tor_free(mem);
Roger Dingledine's avatar
Roger Dingledine committed
579
580
}

581
582
/** Make sure <b>conn</b> isn't in any of the global conn lists; then free it.
 */
583
584
585
void
connection_free(connection_t *conn)
{
586
587
  if (!conn)
    return;
588
589
  tor_assert(!connection_is_on_closeable_list(conn));
  tor_assert(!connection_in_array(conn));
590
591
592
593
594
595
596
597
598
  if (conn->linked_conn) {
    log_err(LD_BUG, "Called with conn->linked_conn still set.");
    tor_fragile_assert();
    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;
  }
599
  if (connection_speaks_cells(conn)) {
600
601
    if (!tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest)) {
      connection_or_remove_from_identity_map(TO_OR_CONN(conn));
602
    }
603
  }
604
  if (conn->type == CONN_TYPE_CONTROL) {
605
    connection_control_closed(TO_CONTROL_CONN(conn));
606
  }
607
  connection_unregister_events(conn);
608
  connection_free_(conn);
609
610
}

611
612
613
/**
 * Called when we're about to finally unlink and free a connection:
 * perform necessary accounting and cleanup
614
615
616
617
618
619
620
 *   - 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.
621
 *   - OR and edge connections need to be unlinked from circuits.
622
 */
623
624
void
connection_about_to_close_connection(connection_t *conn)
625
{
626
  tor_assert(conn->marked_for_close);
627

628
  switch (conn->type) {
629
    case CONN_TYPE_DIR:
630
      connection_dir_about_to_close(TO_DIR_CONN(conn));
631
      break;
632
    case CONN_TYPE_OR:
633
      connection_or_about_to_close(TO_OR_CONN(conn));
634
      break;
635
    case CONN_TYPE_AP:
636
      connection_ap_about_to_close(TO_ENTRY_CONN(conn));
637
      break;
638
    case CONN_TYPE_EXIT:
639
      connection_exit_about_to_close(TO_EDGE_CONN(conn));
640
      break;
641
  }
642
643
}

Roger Dingledine's avatar
Roger Dingledine committed
644
645
/** Return true iff connection_close_immediate() has been called on this
 * connection. */
646
#define CONN_IS_CLOSED(c) \
647
  ((c)->linked ? ((c)->linked_conn_is_closed) : (! SOCKET_OK(c->s)))
648

649
650
651
/** 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().
652
 */
653
654
void
connection_close_immediate(connection_t *conn)
655
656
{
  assert_connection_ok(conn,0);
657
  if (CONN_IS_CLOSED(conn)) {
658
    log_err(LD_BUG,"Attempt to close already-closed connection.");
659
    tor_fragile_assert();
660
661
    return;
  }
662
  if (conn->outbuf_flushlen) {
Roger Dingledine's avatar
Roger Dingledine committed
663
    log_info(LD_NET,"fd %d, type %s, state %s, %d bytes on outbuf.",
664
             (int)conn->s, conn_type_to_string(conn->type),
Roger Dingledine's avatar
Roger Dingledine committed
665
666
             conn_state_to_string(conn->type, conn->state),
             (int)conn->outbuf_flushlen);
667
  }
668

669
  connection_unregister_events(conn);
670

671
  if (SOCKET_OK(conn->s))
672
    tor_close_socket(conn->s);
673
  conn->s = TOR_INVALID_SOCKET;
674
675
  if (conn->linked)
    conn->linked_conn_is_closed = 1;
676
  if (conn->outbuf)
677
    buf_clear(conn->outbuf);
678
  conn->outbuf_flushlen = 0;
679
680
}

681
/** Mark <b>conn</b> to be closed next time we loop through
682
 * conn_close_if_marked() in main.c. */
683
void
684
connection_mark_for_close_(connection_t *conn, int line, const char *file)
685
686
{
  assert_connection_ok(conn,0);
687
  tor_assert(line);
688
  tor_assert(line < 1<<16); /* marked_for_close can only fit a uint16_t. */
689
  tor_assert(file);
690
691

  if (conn->marked_for_close) {
692
    log(LOG_WARN,LD_BUG,"Duplicate call to connection_mark_for_close at %s:%d"
693
694
        " (first at %s:%d)", file, line, conn->marked_for_close_file,
        conn->marked_for_close);
695
    tor_fragile_assert();
696
    return;
697
698
  }

699
700
701
702
703
704
705
706
707
708
  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,
              "Calling connection_mark_for_close on an OR conn at %s:%d",
              file, line);
  }

709
710
  conn->marked_for_close = line;
  conn->marked_for_close_file = file;
711
  add_connection_to_closeable_list(conn);
712
713
714
715
716

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

719
720
721
722
723
/** 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.
 */
724
725
void
connection_expire_held_open(void)
726
727
{
  time_t now;
728
  smartlist_t *conns = get_connection_array();
729
730
731

  now = time(NULL);

732
  SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
733
734
735
    /* If we've been holding the connection open, but we haven't written
     * for 15 seconds...
     */
736
    if (conn->hold_open_until_flushed) {
Roger Dingledine's avatar
Roger Dingledine committed
737
      tor_assert(conn->marked_for_close);
738
      if (now - conn->timestamp_lastwritten >= 15) {
Roger Dingledine's avatar
Roger Dingledine committed
739
740
        int severity;
        if (conn->type == CONN_TYPE_EXIT ||
741
742
            (conn->type == CONN_TYPE_DIR &&
             conn->purpose == DIR_PURPOSE_SERVER))
Roger Dingledine's avatar
Roger Dingledine committed
743
744
745
          severity = LOG_INFO;
        else
          severity = LOG_NOTICE;
746
        log_fn(severity, LD_NET,
747
748
               "Giving up on marked_for_close conn that's been flushing "
               "for 15s (fd %d, type %s, state %s).",
749
               (int)conn->s, conn_type_to_string(conn->type),
750
               conn_state_to_string(conn->type, conn->state));
751
752
        conn->hold_open_until_flushed = 0;
      }
753
    }
754
  } SMARTLIST_FOREACH_END(conn);
755
756
}

757
#if defined(HAVE_SYS_UN_H) || defined(RUNNING_DOXYGEN)
758
/** Create an AF_UNIX listenaddr struct.
Nick Mathewson's avatar
Nick Mathewson committed
759
 * <b>listenaddress</b> provides the path to the Unix socket.
760
761
762
763
764
765
 *
 * 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
766
 * If not NULL <b>readable_address</b> will contain a copy of the path part of
767
768
769
770
771
 * <b>listenaddress</b>.
 *
 * The listenaddr struct has to be freed by the caller.
 */
static struct sockaddr_un *
772
773
create_unix_sockaddr(const char *listenaddress, char **readable_address,
                     socklen_t *len_out)
774
775
776
777
778
{
  struct sockaddr_un *sockaddr = NULL;

  sockaddr = tor_malloc_zero(sizeof(struct sockaddr_un));
  sockaddr->sun_family = AF_UNIX;
779
780
781
782
783
784
785
  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;
  }
786
787
788
789

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

790
  *len_out = sizeof(struct sockaddr_un);
791
792
793
794
  return sockaddr;
}
#else
static struct sockaddr *
795
796
create_unix_sockaddr(const char *listenaddress, char **readable_address,
                     socklen_t *len_out)
797
{
798
799
  (void)listenaddress;
  (void)readable_address;
800
801
  log_fn(LOG_ERR, LD_BUG,
         "Unix domain sockets not supported, yet we tried to create one.");
802
  *len_out = 0;
803
  tor_fragile_assert();
804
805
  return NULL;
}
806
807
#endif /* HAVE_SYS_UN_H */

808
809
810
/** Warn that an accept or a connect has failed because we're running up
 * against our ulimit.  Rate-limit these warnings so that we don't spam
 * the log. */
Nick Mathewson's avatar
Nick Mathewson committed
811
812
813
814
static void
warn_too_many_conns(void)
{
#define WARN_TOO_MANY_CONNS_INTERVAL (6*60*60)
815
816
817
818
  static ratelim_t last_warned = RATELIM_INIT(WARN_TOO_MANY_CONNS_INTERVAL);
  char *m;
  if ((m = rate_limit_log(&last_warned, approx_time()))) {
    int n_conns = get_n_open_sockets();
Nick Mathewson's avatar
Nick Mathewson committed
819
    log_warn(LD_NET,"Failing because we have %d connections already. Please "
820
821
             "raise your ulimit -n.%s", n_conns, m);
    tor_free(m);
822
823
    control_event_general_status(LOG_WARN, "TOO_MANY_CONNECTIONS CURRENT=%d",
                                 n_conns);
Nick Mathewson's avatar
Nick Mathewson committed
824
825
826
  }
}

827
828
829
830
#ifdef HAVE_SYS_UN_H
/** Check whether we should be willing to open an AF_UNIX socket in
 * <b>path</b>.  Return 0 if we should go ahead and -1 if we shouldn't. */
static int
831
check_location_for_unix_socket(const or_options_t *options, const char *path)
832
833
834
835
836
837
838
839
840
841
{
  int r = -1;
  char *p = tor_strdup(path);
  cpd_check_t flags = CPD_CHECK_MODE_ONLY;
  if (get_parent_directory(p)<0)
    goto done;

  if (options->ControlSocketsGroupWritable)
    flags |= CPD_GROUP_OK;

842
  if (check_private_dir(p, flags, options->User) < 0) {
843
844
845
846
847
848
    char *escpath, *escdir;
    escpath = esc_for_log(path);
    escdir = esc_for_log(p);
    log_warn(LD_GENERAL, "Before Tor can create a control socket in %s, the "
             "directory %s needs to exist, and to be accessible only by the "
             "user%s account that is running Tor.  (On some Unix systems, "
849
             "anybody who can list a socket can connect to it, so Tor is "
850
851
852
853
854
855
856
857
858
859
860
861
862
863
             "being careful.)", escpath, escdir,
             options->ControlSocketsGroupWritable ? " and group" : "");
    tor_free(escpath);
    tor_free(escdir);
    goto done;
  }

  r = 0;
 done:
  tor_free(p);
  return r;
}
#endif

864
865
866
/** Tell the TCP stack that it shouldn't wait for a long time after
 * <b>sock</b> has closed before reusing its port. */
static void
867
make_socket_reuseable(tor_socket_t sock)
868
{
869
#ifdef _WIN32
870
871
872
873
874
875
876
877
878
879
880
881
882
  (void) sock;
#else
  int one=1;

  /* REUSEADDR on normal places means you can rebind to the port
   * right after somebody else has let it go. But REUSEADDR on win32
   * means you can bind to the port _even when somebody else
   * already has it bound_. So, don't do that on Win32. */
  setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*) &one,
             (socklen_t)sizeof(one));
#endif
}

883
884
885
886
887
888
889
/** Bind a new non-blocking socket listening to the socket described
 * by <b>listensockaddr</b>.
 *
 * <b>address</b> is only used for logging purposes and to add the information
 * to the conn.
 */
static connection_t *
890
connection_listener_new(const struct sockaddr *listensockaddr,
891
                           socklen_t socklen,
892
893
                           int type, const char *address,
                           const port_cfg_t *port_cfg)
894
{
895
  listener_connection_t *lis_conn;
896
  connection_t *conn;
897
  tor_socket_t s; /* the socket we're going to make */
898
899
900
901
  or_options_t const *options = get_options();
#if defined(HAVE_PWD_H) && defined(HAVE_SYS_UN_H)
  struct passwd *pw = NULL;
#endif
902
  uint16_t usePort = 0, gotPort = 0;
903
  int start_reading = 0;
904
  static int global_next_session_group = SESSION_GROUP_FIRST_AUTO;
905
  tor_addr_t addr;
Nick Mathewson's avatar
Nick Mathewson committed
906

907
  if (get_n_open_sockets() >= get_options()->ConnLimit_-1) {
Nick Mathewson's avatar
Nick Mathewson committed
908
    warn_too_many_conns();
909
910
911
    return NULL;
  }

912
913
  if (listensockaddr->sa_family == AF_INET ||
      listensockaddr->sa_family == AF_INET6) {
914
915
916
917
    int is_tcp = (type != CONN_TYPE_AP_DNS_LISTENER);
    if (is_tcp)
      start_reading = 1;

918
    tor_addr_from_sockaddr(&addr, listensockaddr, &usePort);
919

920
921
    log_notice(LD_NET, "Opening %s on %s",
               conn_type_to_string(type), fmt_addrport(&addr, usePort));
922

923
    s = tor_open_socket(tor_addr_family(&addr),
924
925
                        is_tcp ? SOCK_STREAM : SOCK_DGRAM,
                        is_tcp ? IPPROTO_TCP: IPPROTO_UDP);
926
    if (!SOCKET_OK(s)) {
927
928
      log_warn(LD_NET,"Socket creation failed: %s",
               tor_socket_strerror(tor_socket_errno(-1)));
929
930
      goto err;
    }
Roger Dingledine's avatar
Roger Dingledine committed
931

932
    make_socket_reuseable(s);
Roger Dingledine's avatar
Roger Dingledine committed
933

934
935
936
937
938
939
940
941
942
943
#ifdef IPV6_V6ONLY
    if (listensockaddr->sa_family == AF_INET6) {
#ifdef _WIN32
      /* In Redmond, this kind of thing passes for standards-conformance. */
      DWORD one = 1;
#else
      int one = 1;
#endif
      /* We need to set IPV6_V6ONLY so that this socket can't get used for
       * IPv4 connections. */
944
945
      if (setsockopt(s,IPPROTO_IPV6, IPV6_V6ONLY,
                     (void*)&one, sizeof(one))<0) {
946
947
948
949
950
951
952
953
        int e = tor_socket_errno(s);
        log_warn(LD_NET, "Error setting IPV6_V6ONLY flag: %s",
                 tor_socket_strerror(e));
        /* Keep going; probably not harmful. */
      }
    }
#endif

954
    if (bind(s,listensockaddr,socklen) < 0) {
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
      const char *helpfulhint = "";
      int e = tor_socket_errno(s);
      if (ERRNO_IS_EADDRINUSE(e))
        helpfulhint = ". Is Tor already running?";
      log_warn(LD_NET, "Could not bind to %s:%u: %s%s", address, usePort,
               tor_socket_strerror(e), helpfulhint);
      tor_close_socket(s);
      goto err;
    }

    if (is_tcp) {
      if (listen(s,SOMAXCONN) < 0) {
        log_warn(LD_NET, "Could not listen on %s:%u: %s", address, usePort,
                 tor_socket_strerror(tor_socket_errno(s)));
        tor_close_socket(s);
        goto err;
      }
    }
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987

    if (usePort != 0) {
      gotPort = usePort;
    } else {
      tor_addr_t addr2;
      struct sockaddr_storage ss;
      socklen_t ss_len=sizeof(ss);
      if (getsockname(s, (struct sockaddr*)&ss, &ss_len)<0) {
        log_warn(LD_NET, "getsockname() couldn't learn address for %s: %s",
                 conn_type_to_string(type),
                 tor_socket_strerror(tor_socket_errno(s)));
        gotPort = 0;
      }
      tor_addr_from_sockaddr(&addr2, (struct sockaddr*)&ss, &gotPort);
    }
988
989
990
991
#ifdef HAVE_SYS_UN_H
  } else if (listensockaddr->sa_family == AF_UNIX) {
    start_reading = 1;

Nick Mathewson's avatar
Nick Mathewson committed
992
    /* For now only control ports can be Unix domain sockets
993
994
995
     * and listeners at the same time */
    tor_assert(type == CONN_TYPE_CONTROL_LISTENER);

996
    if (check_location_for_unix_socket(options, address) < 0)
997
998
      goto err;

999
1000
    log_notice(LD_NET, "Opening %s on %s",
               conn_type_to_string(type), address);