channeltls.c 69.6 KB
Newer Older
Nick Mathewson's avatar
Nick Mathewson committed
1
/* * Copyright (c) 2012-2016, The Tor Project, Inc. */
2
3
4
5
/* See LICENSE for licensing information */

/**
 * \file channeltls.c
6
7
8
 *
 * \brief A concrete subclass of channel_t using or_connection_t to transfer
 * cells between Tor instances.
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 *
 * This module fills in the various function pointers in channel_t, to
 * implement the channel_tls_t channels as used in Tor today.  These channels
 * are created from channel_tls_connect() and
 * channel_tls_handle_incoming(). Each corresponds 1:1 to or_connection_t
 * object, as implemented in connection_or.c.  These channels transmit cells
 * to the underlying or_connection_t by calling
 * connection_or_write_*_cell_to_buf(), and receive cells from the underlying
 * or_connection_t when connection_or_process_cells_from_inbuf() calls
 * channel_tls_handle_*_cell().
 *
 * Here we also implement the server (responder) side of the v3+ Tor link
 * handshake, which uses CERTS and AUTHENTICATE cell to negotiate versions,
 * exchange expected and observed IP and time information, and bootstrap a
 * level of authentication higher than we have gotten on the raw TLS
 * handshake.
 *
 * NOTE: Since there is currently only one type of channel, there are probably
 * more than a few cases where functionality that is currently in
 * channeltls.c, connection_or.c, and channel.c ought to be divided up
 * differently.  The right time to do this is probably whenever we introduce
 * our next channel type.
31
32
33
34
35
36
37
 **/

/*
 * Define this so channel.h gives us things only channel_t subclasses
 * should touch.
 */

38
#define TOR_CHANNEL_INTERNAL_
39

40
41
#define CHANNELTLS_PRIVATE

42
43
44
#include "or.h"
#include "channel.h"
#include "channeltls.h"
45
#include "circuitmux.h"
46
#include "circuitmux_ewma.h"
47
#include "command.h"
48
49
50
51
#include "config.h"
#include "connection.h"
#include "connection_or.h"
#include "control.h"
52
#include "link_handshake.h"
53
#include "relay.h"
54
#include "rephist.h"
55
56
#include "router.h"
#include "routerlist.h"
57
#include "scheduler.h"
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

/** How many CELL_PADDING cells have we received, ever? */
uint64_t stats_n_padding_cells_processed = 0;
/** How many CELL_VERSIONS cells have we received, ever? */
uint64_t stats_n_versions_cells_processed = 0;
/** How many CELL_NETINFO cells have we received, ever? */
uint64_t stats_n_netinfo_cells_processed = 0;
/** How many CELL_VPADDING cells have we received, ever? */
uint64_t stats_n_vpadding_cells_processed = 0;
/** How many CELL_CERTS cells have we received, ever? */
uint64_t stats_n_certs_cells_processed = 0;
/** How many CELL_AUTH_CHALLENGE cells have we received, ever? */
uint64_t stats_n_auth_challenge_cells_processed = 0;
/** How many CELL_AUTHENTICATE cells have we received, ever? */
uint64_t stats_n_authenticate_cells_processed = 0;
/** How many CELL_AUTHORIZE cells have we received, ever? */
uint64_t stats_n_authorize_cells_processed = 0;

/** Active listener, if any */
77
static channel_listener_t *channel_tls_listener = NULL;
78
79
80
81

/* channel_tls_t method declarations */

static void channel_tls_close_method(channel_t *chan);
82
static const char * channel_tls_describe_transport_method(channel_t *chan);
83
static void channel_tls_free_method(channel_t *chan);
84
static double channel_tls_get_overhead_estimate_method(channel_t *chan);
85
86
static int
channel_tls_get_remote_addr_method(channel_t *chan, tor_addr_t *addr_out);
87
88
static int
channel_tls_get_transport_name_method(channel_t *chan, char **transport_out);
89
static const char *
90
channel_tls_get_remote_descr_method(channel_t *chan, int flags);
91
92
93
94
95
96
97
static int channel_tls_has_queued_writes_method(channel_t *chan);
static int channel_tls_is_canonical_method(channel_t *chan, int req);
static int
channel_tls_matches_extend_info_method(channel_t *chan,
                                       extend_info_t *extend_info);
static int channel_tls_matches_target_method(channel_t *chan,
                                             const tor_addr_t *target);
98
static int channel_tls_num_cells_writeable_method(channel_t *chan);
99
static size_t channel_tls_num_bytes_queued_method(channel_t *chan);
100
101
102
103
104
105
106
static int channel_tls_write_cell_method(channel_t *chan,
                                         cell_t *cell);
static int channel_tls_write_packed_cell_method(channel_t *chan,
                                                packed_cell_t *packed_cell);
static int channel_tls_write_var_cell_method(channel_t *chan,
                                             var_cell_t *var_cell);

107
108
109
110
111
112
/* channel_listener_tls_t method declarations */

static void channel_tls_listener_close_method(channel_listener_t *chan_l);
static const char *
channel_tls_listener_describe_transport_method(channel_listener_t *chan_l);

113
114
115
116
117
118
119
120
121
122
123
124
/** Handle incoming cells for the handshake stuff here rather than
 * passing them on up. */

static void channel_tls_process_versions_cell(var_cell_t *cell,
                                              channel_tls_t *tlschan);
static void channel_tls_process_netinfo_cell(cell_t *cell,
                                             channel_tls_t *tlschan);
static int command_allowed_before_handshake(uint8_t command);
static int enter_v3_handshake_with_cell(var_cell_t *cell,
                                        channel_tls_t *tlschan);

/**
125
126
 * Do parts of channel_tls_t initialization common to channel_tls_connect()
 * and channel_tls_handle_incoming().
127
128
 */

129
STATIC void
130
channel_tls_common_init(channel_tls_t *tlschan)
131
{
132
133
134
135
136
  channel_t *chan;

  tor_assert(tlschan);

  chan = &(tlschan->base_);
137
  channel_init(chan);
138
  chan->magic = TLS_CHAN_MAGIC;
139
140
  chan->state = CHANNEL_STATE_OPENING;
  chan->close = channel_tls_close_method;
141
  chan->describe_transport = channel_tls_describe_transport_method;
142
  chan->free_fn = channel_tls_free_method;
143
  chan->get_overhead_estimate = channel_tls_get_overhead_estimate_method;
144
145
  chan->get_remote_addr = channel_tls_get_remote_addr_method;
  chan->get_remote_descr = channel_tls_get_remote_descr_method;
146
  chan->get_transport_name = channel_tls_get_transport_name_method;
147
148
149
150
  chan->has_queued_writes = channel_tls_has_queued_writes_method;
  chan->is_canonical = channel_tls_is_canonical_method;
  chan->matches_extend_info = channel_tls_matches_extend_info_method;
  chan->matches_target = channel_tls_matches_target_method;
151
  chan->num_bytes_queued = channel_tls_num_bytes_queued_method;
152
  chan->num_cells_writeable = channel_tls_num_cells_writeable_method;
153
154
155
  chan->write_cell = channel_tls_write_cell_method;
  chan->write_packed_cell = channel_tls_write_packed_cell_method;
  chan->write_var_cell = channel_tls_write_var_cell_method;
156

157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
  chan->cmux = circuitmux_alloc();
  if (cell_ewma_enabled()) {
    circuitmux_set_policy(chan->cmux, &ewma_policy);
  }
}

/**
 * Start a new TLS channel
 *
 * Launch a new OR connection to <b>addr</b>:<b>port</b> and expect to
 * handshake with an OR with identity digest <b>id_digest</b>, and wrap
 * it in a channel_tls_t.
 */

channel_t *
channel_tls_connect(const tor_addr_t *addr, uint16_t port,
                    const char *id_digest)
{
  channel_tls_t *tlschan = tor_malloc_zero(sizeof(*tlschan));
  channel_t *chan = &(tlschan->base_);

  channel_tls_common_init(tlschan);

180
  log_debug(LD_CHANNEL,
181
182
183
184
            "In channel_tls_connect() for channel %p "
            "(global id " U64_FORMAT ")",
            tlschan,
            U64_PRINTF_ARG(chan->global_identifier));
185

186
187
188
189
190
191
192
193
194
195
196
197
  if (is_local_addr(addr)) {
    log_debug(LD_CHANNEL,
              "Marking new outgoing channel " U64_FORMAT " at %p as local",
              U64_PRINTF_ARG(chan->global_identifier), chan);
    channel_mark_local(chan);
  } else {
    log_debug(LD_CHANNEL,
              "Marking new outgoing channel " U64_FORMAT " at %p as remote",
              U64_PRINTF_ARG(chan->global_identifier), chan);
    channel_mark_remote(chan);
  }

198
199
200
  channel_mark_outgoing(chan);

  /* Set up or_connection stuff */
201
  tlschan->conn = connection_or_connect(addr, port, id_digest, tlschan);
202
203
  /* connection_or_connect() will fill in tlschan->conn */
  if (!(tlschan->conn)) {
204
    chan->reason_for_closing = CHANNEL_CLOSE_FOR_ERROR;
205
206
207
208
209
    channel_change_state(chan, CHANNEL_STATE_ERROR);
    goto err;
  }

  log_debug(LD_CHANNEL,
210
211
            "Got orconn %p for channel with global id " U64_FORMAT,
            tlschan->conn, U64_PRINTF_ARG(chan->global_identifier));
212
213
214
215

  goto done;

 err:
216
  circuitmux_free(chan->cmux);
217
218
219
220
221
222
223
224
225
226
227
228
229
  tor_free(tlschan);
  chan = NULL;

 done:
  /* If we got one, we should register it */
  if (chan) channel_register(chan);

  return chan;
}

/**
 * Return the current channel_tls_t listener
 *
230
 * Returns the current channel listener for incoming TLS connections, or
231
232
233
 * NULL if none has been established
 */

234
channel_listener_t *
235
236
channel_tls_get_listener(void)
{
237
  return channel_tls_listener;
238
239
240
241
242
243
244
245
246
}

/**
 * Start a channel_tls_t listener if necessary
 *
 * Return the current channel_tls_t listener, or start one if we haven't yet,
 * and return that.
 */

247
channel_listener_t *
248
249
channel_tls_start_listener(void)
{
250
  channel_listener_t *listener;
251
252
253

  if (!channel_tls_listener) {
    listener = tor_malloc_zero(sizeof(*listener));
254
255
256
257
258
    channel_init_listener(listener);
    listener->state = CHANNEL_LISTENER_STATE_LISTENING;
    listener->close = channel_tls_listener_close_method;
    listener->describe_transport =
      channel_tls_listener_describe_transport_method;
259
260
261
262

    channel_tls_listener = listener;

    log_debug(LD_CHANNEL,
263
264
              "Starting TLS channel listener %p with global id " U64_FORMAT,
              listener, U64_PRINTF_ARG(listener->global_identifier));
265

266
267
    channel_listener_register(listener);
  } else listener = channel_tls_listener;
268

269
  return listener;
270
271
272
273
274
275
276
277
278
279
280
281
}

/**
 * Free everything on shutdown
 *
 * Not much to do here, since channel_free_all() takes care of a lot, but let's
 * get rid of the listener.
 */

void
channel_tls_free_all(void)
{
282
283
  channel_listener_t *old_listener = NULL;

284
285
286
287
  log_debug(LD_CHANNEL,
            "Shutting down TLS channels...");

  if (channel_tls_listener) {
288
289
290
291
292
293
294
295
296
297
298
299
300
301
    /*
     * When we close it, channel_tls_listener will get nulled out, so save
     * a pointer so we can free it.
     */
    old_listener = channel_tls_listener;
    log_debug(LD_CHANNEL,
              "Closing channel_tls_listener with ID " U64_FORMAT
              " at %p.",
              U64_PRINTF_ARG(old_listener->global_identifier),
              old_listener);
    channel_listener_unregister(old_listener);
    channel_listener_mark_for_close(old_listener);
    channel_listener_free(old_listener);
    tor_assert(channel_tls_listener == NULL);
302
303
304
305
306
307
308
309
310
311
312
313
314
315
  }

  log_debug(LD_CHANNEL,
            "Done shutting down TLS channels");
}

/**
 * Create a new channel around an incoming or_connection_t
 */

channel_t *
channel_tls_handle_incoming(or_connection_t *orconn)
{
  channel_tls_t *tlschan = tor_malloc_zero(sizeof(*tlschan));
316
  channel_t *chan = &(tlschan->base_);
317
318
319
320

  tor_assert(orconn);
  tor_assert(!(orconn->chan));

321
  channel_tls_common_init(tlschan);
322
323
324
325
326

  /* Link the channel and orconn to each other */
  tlschan->conn = orconn;
  orconn->chan = tlschan;

327
328
329
330
331
332
333
334
335
336
337
338
  if (is_local_addr(&(TO_CONN(orconn)->addr))) {
    log_debug(LD_CHANNEL,
              "Marking new incoming channel " U64_FORMAT " at %p as local",
              U64_PRINTF_ARG(chan->global_identifier), chan);
    channel_mark_local(chan);
  } else {
    log_debug(LD_CHANNEL,
              "Marking new incoming channel " U64_FORMAT " at %p as remote",
              U64_PRINTF_ARG(chan->global_identifier), chan);
    channel_mark_remote(chan);
  }

339
340
  channel_mark_incoming(chan);

341
342
  /* Register it */
  channel_register(chan);
343
344
345
346

  return chan;
}

347
348
349
350
351
352
353
354
355
356
357
358
359
/*********
 * Casts *
 ********/

/**
 * Cast a channel_tls_t to a channel_t.
 */

channel_t *
channel_tls_to_base(channel_tls_t *tlschan)
{
  if (!tlschan) return NULL;

360
  return &(tlschan->base_);
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
}

/**
 * Cast a channel_t to a channel_tls_t, with appropriate type-checking
 * asserts.
 */

channel_tls_t *
channel_tls_from_base(channel_t *chan)
{
  if (!chan) return NULL;

  tor_assert(chan->magic == TLS_CHAN_MAGIC);

  return (channel_tls_t *)(chan);
}

378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/********************************************
 * Method implementations for channel_tls_t *
 *******************************************/

/**
 * Close a channel_tls_t
 *
 * This implements the close method for channel_tls_t
 */

static void
channel_tls_close_method(channel_t *chan)
{
  channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);

  tor_assert(tlschan);

395
396
397
398
399
400
401
  if (tlschan->conn) connection_or_close_normally(tlschan->conn, 1);
  else {
    /* Weird - we'll have to change the state ourselves, I guess */
    log_info(LD_CHANNEL,
             "Tried to close channel_tls_t %p with NULL conn",
             tlschan);
    channel_change_state(chan, CHANNEL_STATE_ERROR);
402
403
404
  }
}

405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/**
 * Describe the transport for a channel_tls_t
 *
 * This returns the string "TLS channel on connection <id>" to the upper
 * layer.
 */

static const char *
channel_tls_describe_transport_method(channel_t *chan)
{
  static char *buf = NULL;
  uint64_t id;
  channel_tls_t *tlschan;
  const char *rv = NULL;

  tor_assert(chan);

422
  tlschan = BASE_CHAN_TO_TLS(chan);
423

424
425
  if (tlschan->conn) {
    id = TO_CONN(tlschan->conn)->global_identifier;
426

427
428
429
430
    if (buf) tor_free(buf);
    tor_asprintf(&buf,
                 "TLS channel (connection " U64_FORMAT ")",
                 U64_PRINTF_ARG(id));
431

432
433
434
    rv = buf;
  } else {
    rv = "TLS channel (no connection)";
435
436
437
438
439
  }

  return rv;
}

440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/**
 * Free a channel_tls_t
 *
 * This is called by the generic channel layer when freeing a channel_tls_t;
 * this happens either on a channel which has already reached
 * CHANNEL_STATE_CLOSED or CHANNEL_STATE_ERROR from channel_run_cleanup() or
 * on shutdown from channel_free_all().  In the latter case we might still
 * have an orconn active (which connection_free_all() will get to later),
 * so we should null out its channel pointer now.
 */

static void
channel_tls_free_method(channel_t *chan)
{
  channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);

  tor_assert(tlschan);

  if (tlschan->conn) {
    tlschan->conn->chan = NULL;
    tlschan->conn = NULL;
  }
}

464
465
466
467
468
469
470
/**
 * Get an estimate of the average TLS overhead for the upper layer
 */

static double
channel_tls_get_overhead_estimate_method(channel_t *chan)
{
471
  double overhead = 1.0;
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
  channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);

  tor_assert(tlschan);
  tor_assert(tlschan->conn);

  /* Just return 1.0f if we don't have sensible data */
  if (tlschan->conn->bytes_xmitted > 0 &&
      tlschan->conn->bytes_xmitted_by_tls >=
      tlschan->conn->bytes_xmitted) {
    overhead = ((double)(tlschan->conn->bytes_xmitted_by_tls)) /
      ((double)(tlschan->conn->bytes_xmitted));

    /*
     * Never estimate more than 2.0; otherwise we get silly large estimates
     * at the very start of a new TLS connection.
     */
488
489
    if (overhead > 2.0)
      overhead = 2.0;
490
491
492
493
494
495
496
497
498
  }

  log_debug(LD_CHANNEL,
            "Estimated overhead ratio for TLS chan " U64_FORMAT " is %f",
            U64_PRINTF_ARG(chan->global_identifier), overhead);

  return overhead;
}

499
500
501
502
503
504
505
506
507
508
509
/**
 * Get the remote address of a channel_tls_t
 *
 * This implements the get_remote_addr method for channel_tls_t; copy the
 * remote endpoint of the channel to addr_out and return 1 (always
 * succeeds for this transport).
 */

static int
channel_tls_get_remote_addr_method(channel_t *chan, tor_addr_t *addr_out)
{
510
  int rv = 0;
511
512
513
514
515
  channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);

  tor_assert(tlschan);
  tor_assert(addr_out);

516
  if (tlschan->conn) {
517
    tor_addr_copy(addr_out, &(tlschan->conn->real_addr));
518
519
    rv = 1;
  } else tor_addr_make_unspec(addr_out);
520

521
  return rv;
522
523
}

524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/**
 * Get the name of the pluggable transport used by a channel_tls_t.
 *
 * This implements the get_transport_name for channel_tls_t. If the
 * channel uses a pluggable transport, copy its name to
 * <b>transport_out</b> and return 0. If the channel did not use a
 * pluggable transport, return -1. */

static int
channel_tls_get_transport_name_method(channel_t *chan, char **transport_out)
{
  channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);

  tor_assert(tlschan);
  tor_assert(transport_out);
  tor_assert(tlschan->conn);

  if (!tlschan->conn->ext_or_transport)
    return -1;

  *transport_out = tor_strdup(tlschan->conn->ext_or_transport);
  return 0;
}

548
549
550
551
552
553
554
555
556
557
/**
 * Get endpoint description of a channel_tls_t
 *
 * This implements the get_remote_descr method for channel_tls_t; it returns
 * a text description of the remote endpoint of the channel suitable for use
 * in log messages.  The req parameter is 0 for the canonical address or 1 for
 * the actual address seen.
 */

static const char *
558
channel_tls_get_remote_descr_method(channel_t *chan, int flags)
559
560
561
562
563
564
565
566
567
568
569
{
#define MAX_DESCR_LEN 32

  static char buf[MAX_DESCR_LEN + 1];
  channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
  connection_t *conn;
  const char *answer = NULL;
  char *addr_str;

  tor_assert(tlschan);

570
571
572
573
574
575
576
577
578
579
580
  if (tlschan->conn) {
    conn = TO_CONN(tlschan->conn);
    switch (flags) {
      case 0:
        /* Canonical address with port*/
        tor_snprintf(buf, MAX_DESCR_LEN + 1,
                     "%s:%u", conn->address, conn->port);
        answer = buf;
        break;
      case GRD_FLAG_ORIGINAL:
        /* Actual address with port */
581
        addr_str = tor_addr_to_str_dup(&(tlschan->conn->real_addr));
582
583
584
585
586
587
588
589
590
591
592
593
        tor_snprintf(buf, MAX_DESCR_LEN + 1,
                     "%s:%u", addr_str, conn->port);
        tor_free(addr_str);
        answer = buf;
        break;
      case GRD_FLAG_ADDR_ONLY:
        /* Canonical address, no port */
        strlcpy(buf, conn->address, sizeof(buf));
        answer = buf;
        break;
      case GRD_FLAG_ORIGINAL|GRD_FLAG_ADDR_ONLY:
        /* Actual address, no port */
594
        addr_str = tor_addr_to_str_dup(&(tlschan->conn->real_addr));
595
596
597
598
599
600
601
602
603
604
605
        strlcpy(buf, addr_str, sizeof(buf));
        tor_free(addr_str);
        answer = buf;
        break;
      default:
        /* Something's broken in channel.c */
        tor_assert(1);
    }
  } else {
    strlcpy(buf, "(No connection)", sizeof(buf));
    answer = buf;
606
607
608
609
610
611
612
613
  }

  return answer;
}

/**
 * Tell the upper layer if we have queued writes
 *
614
 * This implements the has_queued_writes method for channel_tls t_; it returns
615
616
617
618
619
620
621
622
623
624
 * 1 iff we have queued writes on the outbuf of the underlying or_connection_t.
 */

static int
channel_tls_has_queued_writes_method(channel_t *chan)
{
  size_t outbuf_len;
  channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);

  tor_assert(tlschan);
625
626
627
628
629
630
  if (!(tlschan->conn)) {
    log_info(LD_CHANNEL,
             "something called has_queued_writes on a tlschan "
             "(%p with ID " U64_FORMAT " but no conn",
             chan, U64_PRINTF_ARG(chan->global_identifier));
  }
631

632
633
634
  outbuf_len = (tlschan->conn != NULL) ?
    connection_get_outbuf_len(TO_CONN(tlschan->conn)) :
    0;
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654

  return (outbuf_len > 0);
}

/**
 * Tell the upper layer if we're canonical
 *
 * This implements the is_canonical method for channel_tls_t; if req is zero,
 * it returns whether this is a canonical channel, and if it is one it returns
 * whether that can be relied upon.
 */

static int
channel_tls_is_canonical_method(channel_t *chan, int req)
{
  int answer = 0;
  channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);

  tor_assert(tlschan);

655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
  if (tlschan->conn) {
    switch (req) {
      case 0:
        answer = tlschan->conn->is_canonical;
        break;
      case 1:
        /*
         * Is the is_canonical bit reliable?  In protocols version 2 and up
         * we get the canonical address from a NETINFO cell, but in older
         * versions it might be based on an obsolete descriptor.
         */
        answer = (tlschan->conn->link_proto >= 2);
        break;
      default:
        /* This shouldn't happen; channel.c is broken if it does */
        tor_assert(1);
    }
672
  }
673
  /* else return 0 for tlschan->conn == NULL */
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693

  return answer;
}

/**
 * Check if we match an extend_info_t
 *
 * This implements the matches_extend_info method for channel_tls_t; the upper
 * layer wants to know if this channel matches an extend_info_t.
 */

static int
channel_tls_matches_extend_info_method(channel_t *chan,
                                       extend_info_t *extend_info)
{
  channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);

  tor_assert(tlschan);
  tor_assert(extend_info);

694
695
696
697
698
699
700
701
702
  /* Never match if we have no conn */
  if (!(tlschan->conn)) {
    log_info(LD_CHANNEL,
             "something called matches_extend_info on a tlschan "
             "(%p with ID " U64_FORMAT " but no conn",
             chan, U64_PRINTF_ARG(chan->global_identifier));
    return 0;
  }

703
704
705
706
707
708
  return (tor_addr_eq(&(extend_info->addr),
                      &(TO_CONN(tlschan->conn)->addr)) &&
         (extend_info->port == TO_CONN(tlschan->conn)->port));
}

/**
709
 * Check if we match a target address; return true iff we do.
710
 *
711
 * This implements the matches_target method for channel_tls t_; the upper
712
713
714
715
716
717
718
719
720
721
722
723
 * layer wants to know if this channel matches a target address when extending
 * a circuit.
 */

static int
channel_tls_matches_target_method(channel_t *chan,
                                  const tor_addr_t *target)
{
  channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);

  tor_assert(tlschan);
  tor_assert(target);
724
725
726
727
728
729
730
731
732

  /* Never match if we have no conn */
  if (!(tlschan->conn)) {
    log_info(LD_CHANNEL,
             "something called matches_target on a tlschan "
             "(%p with ID " U64_FORMAT " but no conn",
             chan, U64_PRINTF_ARG(chan->global_identifier));
    return 0;
  }
733

734
  return tor_addr_eq(&(tlschan->conn->real_addr), target);
735
736
}

737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
/**
 * Tell the upper layer how many bytes we have queued and not yet
 * sent.
 */

static size_t
channel_tls_num_bytes_queued_method(channel_t *chan)
{
  channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);

  tor_assert(tlschan);
  tor_assert(tlschan->conn);

  return connection_get_outbuf_len(TO_CONN(tlschan->conn));
}

753
754
755
756
757
758
759
760
761
762
763
764
/**
 * Tell the upper layer how many cells we can accept to write
 *
 * This implements the num_cells_writeable method for channel_tls_t; it
 * returns an estimate of the number of cells we can accept with
 * channel_tls_write_*_cell().
 */

static int
channel_tls_num_cells_writeable_method(channel_t *chan)
{
  size_t outbuf_len;
Nick Mathewson's avatar
Nick Mathewson committed
765
  ssize_t n;
766
767
768
769
770
771
772
773
774
775
776
  channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
  size_t cell_network_size;

  tor_assert(tlschan);
  tor_assert(tlschan->conn);

  cell_network_size = get_cell_network_size(tlschan->conn->wide_circ_ids);
  outbuf_len = connection_get_outbuf_len(TO_CONN(tlschan->conn));
  /* Get the number of cells */
  n = CEIL_DIV(OR_CONN_HIGHWATER - outbuf_len, cell_network_size);
  if (n < 0) n = 0;
Nick Mathewson's avatar
Nick Mathewson committed
777
778
779
#if SIZEOF_SIZE_T > SIZEOF_INT
  if (n > INT_MAX) n = INT_MAX;
#endif
780

Nick Mathewson's avatar
Nick Mathewson committed
781
  return (int)n;
782
783
}

784
785
786
787
788
789
790
791
792
793
794
/**
 * Write a cell to a channel_tls_t
 *
 * This implements the write_cell method for channel_tls_t; given a
 * channel_tls_t and a cell_t, transmit the cell_t.
 */

static int
channel_tls_write_cell_method(channel_t *chan, cell_t *cell)
{
  channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
795
  int written = 0;
796
797
798
799

  tor_assert(tlschan);
  tor_assert(cell);

800
801
802
803
804
805
806
807
808
  if (tlschan->conn) {
    connection_or_write_cell_to_buf(cell, tlschan->conn);
    ++written;
  } else {
    log_info(LD_CHANNEL,
             "something called write_cell on a tlschan "
             "(%p with ID " U64_FORMAT " but no conn",
             chan, U64_PRINTF_ARG(chan->global_identifier));
  }
809

810
  return written;
811
812
813
814
815
816
817
818
819
820
821
822
823
}

/**
 * Write a packed cell to a channel_tls_t
 *
 * This implements the write_packed_cell method for channel_tls_t; given a
 * channel_tls_t and a packed_cell_t, transmit the packed_cell_t.
 */

static int
channel_tls_write_packed_cell_method(channel_t *chan,
                                     packed_cell_t *packed_cell)
{
824
  tor_assert(chan);
825
  channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
826
  size_t cell_network_size = get_cell_network_size(chan->wide_circ_ids);
827
  int written = 0;
828
829
830
831

  tor_assert(tlschan);
  tor_assert(packed_cell);

832
833
834
  if (tlschan->conn) {
    connection_write_to_buf(packed_cell->body, cell_network_size,
                            TO_CONN(tlschan->conn));
835

836
837
838
839
840
841
842
843
844
    /* This is where the cell is finished; used to be done from relay.c */
    packed_cell_free(packed_cell);
    ++written;
  } else {
    log_info(LD_CHANNEL,
             "something called write_packed_cell on a tlschan "
             "(%p with ID " U64_FORMAT " but no conn",
             chan, U64_PRINTF_ARG(chan->global_identifier));
  }
845

846
  return written;
847
848
849
850
851
852
853
854
855
856
857
858
859
}

/**
 * Write a variable-length cell to a channel_tls_t
 *
 * This implements the write_var_cell method for channel_tls_t; given a
 * channel_tls_t and a var_cell_t, transmit the var_cell_t.
 */

static int
channel_tls_write_var_cell_method(channel_t *chan, var_cell_t *var_cell)
{
  channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
860
  int written = 0;
861
862
863
864

  tor_assert(tlschan);
  tor_assert(var_cell);

865
866
867
868
869
870
871
872
873
  if (tlschan->conn) {
    connection_or_write_var_cell_to_buf(var_cell, tlschan->conn);
    ++written;
  } else {
    log_info(LD_CHANNEL,
             "something called write_var_cell on a tlschan "
             "(%p with ID " U64_FORMAT " but no conn",
             chan, U64_PRINTF_ARG(chan->global_identifier));
  }
874

875
  return written;
876
877
}

878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
/*************************************************
 * Method implementations for channel_listener_t *
 ************************************************/

/**
 * Close a channel_listener_t
 *
 * This implements the close method for channel_listener_t
 */

static void
channel_tls_listener_close_method(channel_listener_t *chan_l)
{
  tor_assert(chan_l);

  /*
   * Listeners we just go ahead and change state through to CLOSED, but
   * make sure to check if they're channel_tls_listener to NULL it out.
   */
  if (chan_l == channel_tls_listener)
    channel_tls_listener = NULL;

  if (!(chan_l->state == CHANNEL_LISTENER_STATE_CLOSING ||
        chan_l->state == CHANNEL_LISTENER_STATE_CLOSED ||
        chan_l->state == CHANNEL_LISTENER_STATE_ERROR)) {
    channel_listener_change_state(chan_l, CHANNEL_LISTENER_STATE_CLOSING);
  }

  if (chan_l->incoming_list) {
    SMARTLIST_FOREACH_BEGIN(chan_l->incoming_list,
                            channel_t *, ichan) {
      channel_mark_for_close(ichan);
    } SMARTLIST_FOREACH_END(ichan);

    smartlist_free(chan_l->incoming_list);
    chan_l->incoming_list = NULL;
  }

  if (!(chan_l->state == CHANNEL_LISTENER_STATE_CLOSED ||
        chan_l->state == CHANNEL_LISTENER_STATE_ERROR)) {
    channel_listener_change_state(chan_l, CHANNEL_LISTENER_STATE_CLOSED);
  }
}

/**
 * Describe the transport for a channel_listener_t
 *
 * This returns the string "TLS channel (listening)" to the upper
 * layer.
 */

static const char *
channel_tls_listener_describe_transport_method(channel_listener_t *chan_l)
{
  tor_assert(chan_l);

  return "TLS channel (listening)";
}

937
938
939
940
941
/*******************************************************
 * Functions for handling events on an or_connection_t *
 ******************************************************/

/**
942
 * Handle an orconn state change
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
 *
 * This function will be called by connection_or.c when the or_connection_t
 * associated with this channel_tls_t changes state.
 */

void
channel_tls_handle_state_change_on_orconn(channel_tls_t *chan,
                                          or_connection_t *conn,
                                          uint8_t old_state,
                                          uint8_t state)
{
  channel_t *base_chan;

  tor_assert(chan);
  tor_assert(conn);
  tor_assert(conn->chan == chan);
  tor_assert(chan->conn == conn);
teor's avatar
teor committed
960
961
  /* Shut the compiler up without triggering -Wtautological-compare */
  (void)old_state;
962
963
964

  base_chan = TLS_CHAN_TO_BASE(chan);

rl1987's avatar
rl1987 committed
965
966
  /* Make sure the base connection state makes sense - shouldn't be error
   * or closed. */
967

968
969
970
971
  tor_assert(CHANNEL_IS_OPENING(base_chan) ||
             CHANNEL_IS_OPEN(base_chan) ||
             CHANNEL_IS_MAINT(base_chan) ||
             CHANNEL_IS_CLOSING(base_chan));
972
973
974
975
976
977
978
979

  /* Did we just go to state open? */
  if (state == OR_CONN_STATE_OPEN) {
    /*
     * We can go to CHANNEL_STATE_OPEN from CHANNEL_STATE_OPENING or
     * CHANNEL_STATE_MAINT on this.
     */
    channel_change_state(base_chan, CHANNEL_STATE_OPEN);
980
981
982
983
    /* We might have just become writeable; check and tell the scheduler */
    if (connection_or_num_cells_writeable(conn) > 0) {
      scheduler_channel_wants_writes(base_chan);
    }
984
985
986
987
988
  } else {
    /*
     * Not open, so from CHANNEL_STATE_OPEN we go to CHANNEL_STATE_MAINT,
     * otherwise no change.
     */
989
    if (CHANNEL_IS_OPEN(base_chan)) {
990
991
992
993
994
995
996
997
998
999
1000
      channel_change_state(base_chan, CHANNEL_STATE_MAINT);
    }
  }
}

#ifdef KEEP_TIMING_STATS

/**
 * Timing states wrapper
 *
 * This is a wrapper function around the actual function that processes the
For faster browsing, not all history is shown. View entire blame