Tor  0.4.5.0-alpha-dev
connection_or.c
Go to the documentation of this file.
1 /* Copyright (c) 2001 Matej Pfajfar.
2  * Copyright (c) 2001-2004, Roger Dingledine.
3  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4  * Copyright (c) 2007-2020, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
6 
7 /**
8  * \file connection_or.c
9  * \brief Functions to handle OR connections, TLS handshaking, and
10  * cells on the network.
11  *
12  * An or_connection_t is a subtype of connection_t (as implemented in
13  * connection.c) that uses a TLS connection to send and receive cells on the
14  * Tor network. (By sending and receiving cells connection_or.c, it cooperates
15  * with channeltls.c to implement a the channel interface of channel.c.)
16  *
17  * Every OR connection has an underlying tortls_t object (as implemented in
18  * tortls.c) which it uses as its TLS stream. It is responsible for
19  * sending and receiving cells over that TLS.
20  *
21  * This module also implements the client side of the v3 (and greater) Tor
22  * link handshake.
23  **/
24 #include "core/or/or.h"
25 #include "feature/client/bridges.h"
26 #include "lib/buf/buffers.h"
27 /*
28  * Define this so we get channel internal functions, since we're implementing
29  * part of a subclass (channel_tls_t).
30  */
31 #define CHANNEL_OBJECT_PRIVATE
32 #define CONNECTION_OR_PRIVATE
33 #define ORCONN_EVENT_PRIVATE
34 #include "core/or/channel.h"
35 #include "core/or/channeltls.h"
36 #include "core/or/circuitbuild.h"
37 #include "core/or/circuitlist.h"
38 #include "core/or/circuitstats.h"
39 #include "core/or/command.h"
40 #include "app/config/config.h"
42 #include "core/or/connection_or.h"
48 #include "lib/geoip/geoip.h"
49 #include "core/mainloop/mainloop.h"
50 #include "trunnel/netinfo.h"
54 #include "core/proto/proto_cell.h"
55 #include "core/or/reasons.h"
56 #include "core/or/relay.h"
58 #include "feature/stats/rephist.h"
59 #include "feature/relay/router.h"
65 #include "core/or/scheduler.h"
67 #include "core/or/channelpadding.h"
69 
70 #include "core/or/cell_st.h"
71 #include "core/or/cell_queue_st.h"
75 #include "app/config/or_state_st.h"
77 #include "core/or/var_cell_st.h"
79 
80 #include "lib/tls/tortls.h"
81 
82 #include "core/or/orconn_event.h"
83 
88  int started_here,
89  char *digest_rcvd_out);
90 
91 static void connection_or_tls_renegotiated_cb(tor_tls_t *tls, void *_conn);
92 
93 static unsigned int
95 static void connection_or_mark_bad_for_new_circs(or_connection_t *or_conn);
96 
98  int started_here);
99 
100 /**************************************************************/
101 
102 /**
103  * Cast a `connection_t *` to an `or_connection_t *`.
104  *
105  * Exit with an assertion failure if the input is not an `or_connnection_t`.
106  **/
109 {
110  tor_assert(c->magic == OR_CONNECTION_MAGIC);
111  return DOWNCAST(or_connection_t, c);
112 }
113 
114 /**
115  * Cast a `const connection_t *` to a `const or_connection_t *`.
116  *
117  * Exit with an assertion failure if the input is not an `or_connnection_t`.
118  **/
119 const or_connection_t *
121 {
122  return TO_OR_CONN((connection_t *)c);
123 }
124 
125 /** Clear clear conn->identity_digest and update other data
126  * structures as appropriate.*/
127 void
129 {
130  tor_assert(conn);
131  memset(conn->identity_digest, 0, DIGEST_LEN);
132 }
133 
134 /** Clear all identities in OR conns.*/
135 void
137 {
139  SMARTLIST_FOREACH(conns, connection_t *, conn,
140  {
141  if (conn->type == CONN_TYPE_OR) {
142  connection_or_clear_identity(TO_OR_CONN(conn));
143  }
144  });
145 }
146 
147 /** Change conn->identity_digest to digest, and add conn into
148  * the appropriate digest maps.
149  *
150  * NOTE that this function only allows two kinds of transitions: from
151  * unset identity to set identity, and from idempotent re-settings
152  * of the same identity. It's not allowed to clear an identity or to
153  * change an identity. Return 0 on success, and -1 if the transition
154  * is not allowed.
155  **/
156 static void
158  const char *rsa_digest,
159  const ed25519_public_key_t *ed_id)
160 {
161  channel_t *chan = NULL;
162  tor_assert(conn);
163  tor_assert(rsa_digest);
164 
165  if (conn->chan)
166  chan = TLS_CHAN_TO_BASE(conn->chan);
167 
168  log_info(LD_HANDSHAKE, "Set identity digest for %s at %p: %s %s.",
170  conn,
171  hex_str(rsa_digest, DIGEST_LEN),
172  ed25519_fmt(ed_id));
173  log_info(LD_HANDSHAKE, " (Previously: %s %s)",
175  chan ? ed25519_fmt(&chan->ed25519_identity) : "<null>");
176 
177  const int rsa_id_was_set = ! tor_digest_is_zero(conn->identity_digest);
178  const int ed_id_was_set =
180  const int rsa_changed =
181  tor_memneq(conn->identity_digest, rsa_digest, DIGEST_LEN);
182  const int ed_changed = ed_id_was_set &&
183  (!ed_id || !ed25519_pubkey_eq(ed_id, &chan->ed25519_identity));
184 
185  tor_assert(!rsa_changed || !rsa_id_was_set);
186  tor_assert(!ed_changed || !ed_id_was_set);
187 
188  if (!rsa_changed && !ed_changed)
189  return;
190 
191  /* If the identity was set previously, remove the old mapping. */
192  if (rsa_id_was_set) {
194  if (chan)
196  }
197 
198  memcpy(conn->identity_digest, rsa_digest, DIGEST_LEN);
199 
200  /* If we're initializing the IDs to zero, don't add a mapping yet. */
201  if (tor_digest_is_zero(rsa_digest) &&
202  (!ed_id || ed25519_public_key_is_zero(ed_id)))
203  return;
204 
205  /* Deal with channels */
206  if (chan)
207  channel_set_identity_digest(chan, rsa_digest, ed_id);
208 }
209 
210 /**************************************************************/
211 
212 /** Map from a string describing what a non-open OR connection was doing when
213  * failed, to an intptr_t describing the count of connections that failed that
214  * way. Note that the count is stored _as_ the pointer.
215  */
216 static strmap_t *broken_connection_counts;
217 
218 /** If true, do not record information in <b>broken_connection_counts</b>. */
220 
221 /** Record that an OR connection failed in <b>state</b>. */
222 static void
223 note_broken_connection(const char *state)
224 {
225  void *ptr;
226  intptr_t val;
228  return;
229 
231  broken_connection_counts = strmap_new();
232 
233  ptr = strmap_get(broken_connection_counts, state);
234  val = (intptr_t)ptr;
235  val++;
236  ptr = (void*)val;
237  strmap_set(broken_connection_counts, state, ptr);
238 }
239 
240 /** Forget all recorded states for failed connections. If
241  * <b>stop_recording</b> is true, don't record any more. */
242 void
243 clear_broken_connection_map(int stop_recording)
244 {
246  strmap_free(broken_connection_counts, NULL);
248  if (stop_recording)
250 }
251 
252 /** Write a detailed description the state of <b>orconn</b> into the
253  * <b>buflen</b>-byte buffer at <b>buf</b>. This description includes not
254  * only the OR-conn level state but also the TLS state. It's useful for
255  * diagnosing broken handshakes. */
256 static void
258  char *buf, size_t buflen)
259 {
260  connection_t *conn = TO_CONN(orconn);
261  const char *conn_state;
262  char tls_state[256];
263 
264  tor_assert(conn->type == CONN_TYPE_OR || conn->type == CONN_TYPE_EXT_OR);
265 
266  conn_state = conn_state_to_string(conn->type, conn->state);
267  tor_tls_get_state_description(orconn->tls, tls_state, sizeof(tls_state));
268 
269  tor_snprintf(buf, buflen, "%s with SSL state %s", conn_state, tls_state);
270 }
271 
272 /** Record the current state of <b>orconn</b> as the state of a broken
273  * connection. */
274 static void
276 {
277  char buf[256];
279  return;
280  connection_or_get_state_description(orconn, buf, sizeof(buf));
281  log_info(LD_HANDSHAKE,"Connection died in state '%s'", buf);
283 }
284 
285 /** Helper type used to sort connection states and find the most frequent. */
286 typedef struct broken_state_count_t {
287  intptr_t count;
288  const char *state;
290 
291 /** Helper function used to sort broken_state_count_t by frequency. */
292 static int
293 broken_state_count_compare(const void **a_ptr, const void **b_ptr)
294 {
295  const broken_state_count_t *a = *a_ptr, *b = *b_ptr;
296  if (b->count < a->count)
297  return -1;
298  else if (b->count == a->count)
299  return 0;
300  else
301  return 1;
302 }
303 
304 /** Upper limit on the number of different states to report for connection
305  * failure. */
306 #define MAX_REASONS_TO_REPORT 10
307 
308 /** Report a list of the top states for failed OR connections at log level
309  * <b>severity</b>, in log domain <b>domain</b>. */
310 void
311 connection_or_report_broken_states(int severity, int domain)
312 {
313  int total = 0;
314  smartlist_t *items;
315 
317  return;
318 
319  items = smartlist_new();
320  STRMAP_FOREACH(broken_connection_counts, state, void *, countptr) {
321  broken_state_count_t *c = tor_malloc(sizeof(broken_state_count_t));
322  c->count = (intptr_t)countptr;
323  total += (int)c->count;
324  c->state = state;
325  smartlist_add(items, c);
326  } STRMAP_FOREACH_END;
327 
329 
330  tor_log(severity, domain, "%d connections have failed%s", total,
331  smartlist_len(items) > MAX_REASONS_TO_REPORT ? ". Top reasons:" : ":");
332 
333  SMARTLIST_FOREACH_BEGIN(items, const broken_state_count_t *, c) {
334  if (c_sl_idx > MAX_REASONS_TO_REPORT)
335  break;
336  tor_log(severity, domain,
337  " %d connections died in state %s", (int)c->count, c->state);
338  } SMARTLIST_FOREACH_END(c);
339 
341  smartlist_free(items);
342 }
343 
344 /**
345  * Helper function to publish an OR connection status event
346  *
347  * Publishes a messages to subscribers of ORCONN messages, and sends
348  * the control event.
349  **/
350 void
352  int reason)
353 {
354  orconn_status_msg_t *msg = tor_malloc(sizeof(*msg));
355 
356  msg->gid = conn->base_.global_identifier;
357  msg->status = tp;
358  msg->reason = reason;
359  orconn_status_publish(msg);
360  control_event_or_conn_status(conn, tp, reason);
361 }
362 
363 /**
364  * Helper function to publish a state change message
365  *
366  * connection_or_change_state() calls this to notify subscribers about
367  * a change of an OR connection state.
368  **/
369 static void
370 connection_or_state_publish(const or_connection_t *conn, uint8_t state)
371 {
372  orconn_state_msg_t *msg = tor_malloc(sizeof(*msg));
373 
374  msg->gid = conn->base_.global_identifier;
375  if (conn->is_pt) {
376  /* Do extra decoding because conn->proxy_type indicates the proxy
377  * protocol that tor uses to talk with the transport plugin,
378  * instead of PROXY_PLUGGABLE. */
379  tor_assert_nonfatal(conn->proxy_type != PROXY_NONE);
380  msg->proxy_type = PROXY_PLUGGABLE;
381  } else {
382  msg->proxy_type = conn->proxy_type;
383  }
384  msg->state = state;
385  if (conn->chan) {
386  msg->chan = TLS_CHAN_TO_BASE(conn->chan)->global_identifier;
387  } else {
388  msg->chan = 0;
389  }
390  orconn_state_publish(msg);
391 }
392 
393 /** Call this to change or_connection_t states, so the owning channel_tls_t can
394  * be notified.
395  */
396 
397 MOCK_IMPL(STATIC void,
399 {
400  tor_assert(conn);
401 
402  conn->base_.state = state;
403 
404  connection_or_state_publish(conn, state);
405  if (conn->chan)
407 }
408 
409 /** Return the number of circuits using an or_connection_t; this used to
410  * be an or_connection_t field, but it got moved to channel_t and we
411  * shouldn't maintain two copies. */
412 
413 MOCK_IMPL(int,
415 {
416  tor_assert(conn);
417 
418  if (conn->chan) {
419  return channel_num_circuits(TLS_CHAN_TO_BASE(conn->chan));
420  } else return 0;
421 }
422 
423 /**************************************************************/
424 
425 /** Pack the cell_t host-order structure <b>src</b> into network-order
426  * in the buffer <b>dest</b>. See tor-spec.txt for details about the
427  * wire format.
428  *
429  * Note that this function doesn't touch <b>dst</b>->next: the caller
430  * should set it or clear it as appropriate.
431  */
432 void
433 cell_pack(packed_cell_t *dst, const cell_t *src, int wide_circ_ids)
434 {
435  char *dest = dst->body;
436  if (wide_circ_ids) {
437  set_uint32(dest, htonl(src->circ_id));
438  dest += 4;
439  } else {
440  /* Clear the last two bytes of dest, in case we can accidentally
441  * send them to the network somehow. */
442  memset(dest+CELL_MAX_NETWORK_SIZE-2, 0, 2);
443  set_uint16(dest, htons(src->circ_id));
444  dest += 2;
445  }
446  set_uint8(dest, src->command);
447  memcpy(dest+1, src->payload, CELL_PAYLOAD_SIZE);
448 }
449 
450 /** Unpack the network-order buffer <b>src</b> into a host-order
451  * cell_t structure <b>dest</b>.
452  */
453 static void
454 cell_unpack(cell_t *dest, const char *src, int wide_circ_ids)
455 {
456  if (wide_circ_ids) {
457  dest->circ_id = ntohl(get_uint32(src));
458  src += 4;
459  } else {
460  dest->circ_id = ntohs(get_uint16(src));
461  src += 2;
462  }
463  dest->command = get_uint8(src);
464  memcpy(dest->payload, src+1, CELL_PAYLOAD_SIZE);
465 }
466 
467 /** Write the header of <b>cell</b> into the first VAR_CELL_MAX_HEADER_SIZE
468  * bytes of <b>hdr_out</b>. Returns number of bytes used. */
469 int
470 var_cell_pack_header(const var_cell_t *cell, char *hdr_out, int wide_circ_ids)
471 {
472  int r;
473  if (wide_circ_ids) {
474  set_uint32(hdr_out, htonl(cell->circ_id));
475  hdr_out += 4;
477  } else {
478  set_uint16(hdr_out, htons(cell->circ_id));
479  hdr_out += 2;
480  r = VAR_CELL_MAX_HEADER_SIZE - 2;
481  }
482  set_uint8(hdr_out, cell->command);
483  set_uint16(hdr_out+1, htons(cell->payload_len));
484  return r;
485 }
486 
487 /** Allocate and return a new var_cell_t with <b>payload_len</b> bytes of
488  * payload space. */
489 var_cell_t *
490 var_cell_new(uint16_t payload_len)
491 {
492  size_t size = offsetof(var_cell_t, payload) + payload_len;
493  var_cell_t *cell = tor_malloc_zero(size);
494  cell->payload_len = payload_len;
495  cell->command = 0;
496  cell->circ_id = 0;
497  return cell;
498 }
499 
500 /**
501  * Copy a var_cell_t
502  */
503 
504 var_cell_t *
506 {
507  var_cell_t *copy = NULL;
508  size_t size = 0;
509 
510  if (src != NULL) {
511  size = offsetof(var_cell_t, payload) + src->payload_len;
512  copy = tor_malloc_zero(size);
513  copy->payload_len = src->payload_len;
514  copy->command = src->command;
515  copy->circ_id = src->circ_id;
516  memcpy(copy->payload, src->payload, copy->payload_len);
517  }
518 
519  return copy;
520 }
521 
522 /** Release all space held by <b>cell</b>. */
523 void
525 {
526  tor_free(cell);
527 }
528 
529 /** We've received an EOF from <b>conn</b>. Mark it for close and return. */
530 int
532 {
533  tor_assert(conn);
534 
535  log_info(LD_OR,"OR connection reached EOF. Closing.");
537 
538  return 0;
539 }
540 
541 /** Handle any new bytes that have come in on connection <b>conn</b>.
542  * If conn is in 'open' state, hand it to
543  * connection_or_process_cells_from_inbuf()
544  * (else do nothing).
545  */
546 int
548 {
549  /** Don't let the inbuf of a nonopen OR connection grow beyond this many
550  * bytes: it's either a broken client, a non-Tor client, or a DOS
551  * attempt. */
552 #define MAX_OR_INBUF_WHEN_NONOPEN 0
553 
554  int ret = 0;
555  tor_assert(conn);
556 
557  switch (conn->base_.state) {
560 
561  /* start TLS after handshake completion, or deal with error */
562  if (ret == 1) {
563  tor_assert(TO_CONN(conn)->proxy_state == PROXY_CONNECTED);
564  if (connection_tls_start_handshake(conn, 0) < 0)
565  ret = -1;
566  /* Touch the channel's active timestamp if there is one */
567  if (conn->chan)
568  channel_timestamp_active(TLS_CHAN_TO_BASE(conn->chan));
569  }
570  if (ret < 0) {
572  }
573 
574  return ret;
576  case OR_CONN_STATE_OPEN:
580  default:
581  break; /* don't do anything */
582  }
583 
584  /* This check was necessary with 0.2.2, when the TLS_SERVER_RENEGOTIATING
585  * check would otherwise just let data accumulate. It serves no purpose
586  * in 0.2.3.
587  *
588  * XXXX Remove this check once we verify that the above paragraph is
589  * 100% true. */
590  if (buf_datalen(conn->base_.inbuf) > MAX_OR_INBUF_WHEN_NONOPEN) {
591  log_fn(LOG_PROTOCOL_WARN, LD_NET, "Accumulated too much data (%d bytes) "
592  "on non-open %s; closing.",
593  (int)buf_datalen(conn->base_.inbuf),
596  ret = -1;
597  }
598 
599  return ret;
600 }
601 
602 /** Called whenever we have flushed some data on an or_conn: add more data
603  * from active circuits. */
604 int
606 {
607  size_t datalen;
608 
609  /* Update the channel's active timestamp if there is one */
610  if (conn->chan)
611  channel_timestamp_active(TLS_CHAN_TO_BASE(conn->chan));
612 
613  /* If we're under the low water mark, add cells until we're just over the
614  * high water mark. */
615  datalen = connection_get_outbuf_len(TO_CONN(conn));
616  if (datalen < OR_CONN_LOWWATER) {
617  /* Let the scheduler know */
618  scheduler_channel_wants_writes(TLS_CHAN_TO_BASE(conn->chan));
619  }
620 
621  return 0;
622 }
623 
624 /** This is for channeltls.c to ask how many cells we could accept if
625  * they were available. */
626 ssize_t
628 {
629  size_t datalen, cell_network_size;
630  ssize_t n = 0;
631 
632  tor_assert(conn);
633 
634  /*
635  * If we're under the high water mark, we're potentially
636  * writeable; note this is different from the calculation above
637  * used to trigger when to start writing after we've stopped.
638  */
639  datalen = connection_get_outbuf_len(TO_CONN(conn));
640  if (datalen < OR_CONN_HIGHWATER) {
641  cell_network_size = get_cell_network_size(conn->wide_circ_ids);
642  n = CEIL_DIV(OR_CONN_HIGHWATER - datalen, cell_network_size);
643  }
644 
645  return n;
646 }
647 
648 /** Connection <b>conn</b> has finished writing and has no bytes left on
649  * its outbuf.
650  *
651  * Otherwise it's in state "open": stop writing and return.
652  *
653  * If <b>conn</b> is broken, mark it for close and return -1, else
654  * return 0.
655  */
656 int
658 {
659  tor_assert(conn);
660  assert_connection_ok(TO_CONN(conn),0);
661 
662  switch (conn->base_.state) {
664  /* PROXY_HAPROXY gets connected by receiving an ack. */
665  if (conn->proxy_type == PROXY_HAPROXY) {
666  tor_assert(TO_CONN(conn)->proxy_state == PROXY_HAPROXY_WAIT_FOR_FLUSH);
667  TO_CONN(conn)->proxy_state = PROXY_CONNECTED;
668 
669  if (connection_tls_start_handshake(conn, 0) < 0) {
670  /* TLS handshaking error of some kind. */
672  return -1;
673  }
674  break;
675  }
676  break;
677  case OR_CONN_STATE_OPEN:
680  break;
681  default:
682  log_err(LD_BUG,"Called in unexpected state %d.", conn->base_.state);
684  return -1;
685  }
686 
687  /* Update the channel's active timestamp if there is one */
688  if (conn->chan)
689  channel_timestamp_active(TLS_CHAN_TO_BASE(conn->chan));
690 
691  return 0;
692 }
693 
694 /** Connected handler for OR connections: begin the TLS handshake.
695  */
696 int
698 {
699  const int proxy_type = or_conn->proxy_type;
700  connection_t *conn;
701 
702  tor_assert(or_conn);
703  conn = TO_CONN(or_conn);
705 
706  log_debug(LD_HANDSHAKE,"connect finished for %s",
707  connection_describe(conn));
708 
709  if (proxy_type != PROXY_NONE) {
710  /* start proxy handshake */
711  if (connection_proxy_connect(conn, proxy_type) < 0) {
712  connection_or_close_for_error(or_conn, 0);
713  return -1;
714  }
715 
718 
719  return 0;
720  }
721 
722  if (connection_tls_start_handshake(or_conn, 0) < 0) {
723  /* TLS handshaking error of some kind. */
724  connection_or_close_for_error(or_conn, 0);
725  return -1;
726  }
727  return 0;
728 }
729 
730 /** Called when we're about to finally unlink and free an OR connection:
731  * perform necessary accounting and cleanup */
732 void
734 {
735  connection_t *conn = TO_CONN(or_conn);
736 
737  /* Tell the controlling channel we're closed */
738  if (or_conn->chan) {
739  channel_closed(TLS_CHAN_TO_BASE(or_conn->chan));
740  /*
741  * NULL this out because the channel might hang around a little
742  * longer before channel_run_cleanup() gets it.
743  */
744  or_conn->chan->conn = NULL;
745  or_conn->chan = NULL;
746  }
747 
748  /* Remember why we're closing this connection. */
749  if (conn->state != OR_CONN_STATE_OPEN) {
750  /* now mark things down as needed */
752  const or_options_t *options = get_options();
754  /* Tell the new guard API about the channel failure */
755  entry_guard_chan_failed(TLS_CHAN_TO_BASE(or_conn->chan));
756  if (conn->state >= OR_CONN_STATE_TLS_HANDSHAKING) {
757  int reason = tls_error_to_orconn_end_reason(or_conn->tls_error);
758  connection_or_event_status(or_conn, OR_CONN_EVENT_FAILED,
759  reason);
760  if (!authdir_mode_tests_reachability(options)) {
761  const char *warning = NULL;
762  if (reason == END_OR_CONN_REASON_TLS_ERROR && or_conn->tls) {
763  warning = tor_tls_get_last_error_msg(or_conn->tls);
764  }
765  if (warning == NULL) {
766  warning = orconn_end_reason_to_control_string(reason);
767  }
768  control_event_bootstrap_prob_or(warning, reason, or_conn);
769  }
770  }
771  }
772  } else if (conn->hold_open_until_flushed) {
773  /* We only set hold_open_until_flushed when we're intentionally
774  * closing a connection. */
775  connection_or_event_status(or_conn, OR_CONN_EVENT_CLOSED,
777  } else if (!tor_digest_is_zero(or_conn->identity_digest)) {
778  connection_or_event_status(or_conn, OR_CONN_EVENT_CLOSED,
780  }
781 }
782 
783 /** Return 1 if identity digest <b>id_digest</b> is known to be a
784  * currently or recently running relay. Otherwise return 0. */
785 int
787 {
788  if (router_get_consensus_status_by_id(id_digest))
789  return 1; /* It's in the consensus: "yes" */
790  if (router_get_by_id_digest(id_digest))
791  return 1; /* Not in the consensus, but we have a descriptor for
792  * it. Probably it was in a recent consensus. "Yes". */
793  return 0;
794 }
795 
796 /** Set the per-conn read and write limits for <b>conn</b>. If it's a known
797  * relay, we will rely on the global read and write buckets, so give it
798  * per-conn limits that are big enough they'll never matter. But if it's
799  * not a known relay, first check if we set PerConnBwRate/Burst, then
800  * check if the consensus sets them, else default to 'big enough'.
801  *
802  * If <b>reset</b> is true, set the bucket to be full. Otherwise, just
803  * clip the bucket if it happens to be <em>too</em> full.
804  */
805 static void
807  const or_options_t *options)
808 {
809  int rate, burst; /* per-connection rate limiting params */
811  /* It's in the consensus, or we have a descriptor for it meaning it
812  * was probably in a recent consensus. It's a recognized relay:
813  * give it full bandwidth. */
814  rate = (int)options->BandwidthRate;
815  burst = (int)options->BandwidthBurst;
816  } else {
817  /* Not a recognized relay. Squeeze it down based on the suggested
818  * bandwidth parameters in the consensus, but allow local config
819  * options to override. */
820  rate = options->PerConnBWRate ? (int)options->PerConnBWRate :
821  networkstatus_get_param(NULL, "perconnbwrate",
822  (int)options->BandwidthRate, 1, INT32_MAX);
823  burst = options->PerConnBWBurst ? (int)options->PerConnBWBurst :
824  networkstatus_get_param(NULL, "perconnbwburst",
825  (int)options->BandwidthBurst, 1, INT32_MAX);
826  }
827 
828  token_bucket_rw_adjust(&conn->bucket, rate, burst);
829  if (reset) {
831  }
832 }
833 
834 /** Either our set of relays or our per-conn rate limits have changed.
835  * Go through all the OR connections and update their token buckets to make
836  * sure they don't exceed their maximum values. */
837 void
839  const or_options_t *options)
840 {
841  SMARTLIST_FOREACH(conns, connection_t *, conn,
842  {
843  if (connection_speaks_cells(conn))
845  });
846 }
847 
848 /* Mark <b>or_conn</b> as canonical if <b>is_canonical</b> is set, and
849  * non-canonical otherwise. Adjust idle_timeout accordingly.
850  */
851 void
852 connection_or_set_canonical(or_connection_t *or_conn,
853  int is_canonical)
854 {
855  if (bool_eq(is_canonical, or_conn->is_canonical) &&
856  or_conn->idle_timeout != 0) {
857  /* Don't recalculate an existing idle_timeout unless the canonical
858  * status changed. */
859  return;
860  }
861 
862  or_conn->is_canonical = !! is_canonical; /* force to a 1-bit boolean */
864  TLS_CHAN_TO_BASE(or_conn->chan), is_canonical);
865 
866  log_info(LD_CIRC,
867  "Channel %"PRIu64 " chose an idle timeout of %d.",
868  or_conn->chan ?
869  (TLS_CHAN_TO_BASE(or_conn->chan)->global_identifier):0,
870  or_conn->idle_timeout);
871 }
872 
873 /** If we don't necessarily know the router we're connecting to, but we
874  * have an addr/port/id_digest, then fill in as much as we can. Start
875  * by checking to see if this describes a router we know.
876  * <b>started_here</b> is 1 if we are the initiator of <b>conn</b> and
877  * 0 if it's an incoming connection. */
878 void
880  const tor_addr_t *addr, uint16_t port,
881  const char *id_digest,
882  const ed25519_public_key_t *ed_id,
883  int started_here)
884 {
885  log_debug(LD_HANDSHAKE, "init conn from address %s: %s, %s (%d)",
886  fmt_addr(addr),
887  hex_str((const char*)id_digest, DIGEST_LEN),
888  ed25519_fmt(ed_id),
889  started_here);
890 
891  connection_or_set_identity_digest(conn, id_digest, ed_id);
893 
894  conn->base_.port = port;
895  tor_addr_copy(&conn->base_.addr, addr);
896  if (! conn->base_.address) {
897  conn->base_.address = tor_strdup(fmt_addr(addr));
898  }
899 
900  connection_or_check_canonicity(conn, started_here);
901 }
902 
903 /** Check whether the identity of <b>conn</b> matches a known node. If it
904  * does, check whether the address of conn matches the expected address, and
905  * update the connection's is_canonical flag, nickname, and address fields as
906  * appropriate. */
907 static void
909 {
910  (void) started_here;
911 
912  const char *id_digest = conn->identity_digest;
913  const ed25519_public_key_t *ed_id = NULL;
914  if (conn->chan)
915  ed_id = & TLS_CHAN_TO_BASE(conn->chan)->ed25519_identity;
916 
917  const node_t *r = node_get_by_id(id_digest);
918  if (r &&
920  ! node_ed25519_id_matches(r, ed_id)) {
921  /* If this node is capable of proving an ed25519 ID,
922  * we can't call this a canonical connection unless both IDs match. */
923  r = NULL;
924  }
925 
926  if (r) {
927  tor_addr_port_t node_ipv4_ap;
928  tor_addr_port_t node_ipv6_ap;
929  node_get_prim_orport(r, &node_ipv4_ap);
930  node_get_pref_ipv6_orport(r, &node_ipv6_ap);
931  if (tor_addr_eq(&conn->base_.addr, &node_ipv4_ap.addr) ||
932  tor_addr_eq(&conn->base_.addr, &node_ipv6_ap.addr)) {
933  connection_or_set_canonical(conn, 1);
934  }
935  /* Choose the correct canonical address and port. */
936  tor_addr_port_t *node_ap;
937  if (tor_addr_family(&conn->base_.addr) == AF_INET) {
938  node_ap = &node_ipv4_ap;
939  } else {
940  node_ap = &node_ipv6_ap;
941  }
942  /* Remember the canonical addr/port so our log messages will make
943  sense. */
944  tor_addr_port_copy(&conn->canonical_orport, node_ap);
945  tor_free(conn->nickname);
946  conn->nickname = tor_strdup(node_get_nickname(r));
947  } else {
948  tor_free(conn->nickname);
949  conn->nickname = tor_malloc(HEX_DIGEST_LEN+2);
950  conn->nickname[0] = '$';
952  conn->identity_digest, DIGEST_LEN);
953  }
954 
955  /*
956  * We have to tell channeltls.c to update the channel marks (local, in
957  * particular), since we may have changed the address.
958  */
959 
960  if (conn->chan) {
962  }
963 }
964 
965 /** These just pass all the is_bad_for_new_circs manipulation on to
966  * channel_t */
967 
968 static unsigned int
970 {
971  tor_assert(or_conn);
972 
973  if (or_conn->chan)
974  return channel_is_bad_for_new_circs(TLS_CHAN_TO_BASE(or_conn->chan));
975  else return 0;
976 }
977 
978 static void
979 connection_or_mark_bad_for_new_circs(or_connection_t *or_conn)
980 {
981  tor_assert(or_conn);
982 
983  if (or_conn->chan)
984  channel_mark_bad_for_new_circs(TLS_CHAN_TO_BASE(or_conn->chan));
985 }
986 
987 /** How old do we let a connection to an OR get before deciding it's
988  * too old for new circuits? */
989 #define TIME_BEFORE_OR_CONN_IS_TOO_OLD (60*60*24*7)
990 
991 /** Expire an or_connection if it is too old. Helper for
992  * connection_or_group_set_badness_ and fast path for
993  * channel_rsa_id_group_set_badness.
994  *
995  * Returns 1 if the connection was already expired, else 0.
996  */
997 int
999  or_connection_t *or_conn,
1000  int force)
1001 {
1002  /* XXXX this function should also be about channels? */
1003  if (or_conn->base_.marked_for_close ||
1005  return 1;
1006 
1007  if (force ||
1009  < now) {
1010  log_info(LD_OR,
1011  "Marking %s as too old for new circuits "
1012  "(fd "TOR_SOCKET_T_FORMAT", %d secs old).",
1013  connection_describe(TO_CONN(or_conn)),
1014  or_conn->base_.s,
1015  (int)(now - or_conn->base_.timestamp_created));
1016  connection_or_mark_bad_for_new_circs(or_conn);
1017  }
1018 
1019  return 0;
1020 }
1021 
1022 /** Given a list of all the or_connections with a given
1023  * identity, set elements of that list as is_bad_for_new_circs as
1024  * appropriate. Helper for connection_or_set_bad_connections().
1025  *
1026  * Specifically, we set the is_bad_for_new_circs flag on:
1027  * - all connections if <b>force</b> is true.
1028  * - all connections that are too old.
1029  * - all open non-canonical connections for which a canonical connection
1030  * exists to the same router.
1031  * - all open canonical connections for which a 'better' canonical
1032  * connection exists to the same router.
1033  * - all open non-canonical connections for which a 'better' non-canonical
1034  * connection exists to the same router at the same address.
1035  *
1036  * See channel_is_better() in channel.c for our idea of what makes one OR
1037  * connection better than another.
1038  */
1039 void
1041 {
1042  /* XXXX this function should be entirely about channels, not OR
1043  * XXXX connections. */
1044 
1045  or_connection_t *best = NULL;
1046  int n_old = 0, n_inprogress = 0, n_canonical = 0, n_other = 0;
1047  time_t now = time(NULL);
1048 
1049  /* Pass 1: expire everything that's old, and see what the status of
1050  * everything else is. */
1051  SMARTLIST_FOREACH_BEGIN(group, or_connection_t *, or_conn) {
1052  if (connection_or_single_set_badness_(now, or_conn, force))
1053  continue;
1054 
1055  if (connection_or_is_bad_for_new_circs(or_conn)) {
1056  ++n_old;
1057  } else if (or_conn->base_.state != OR_CONN_STATE_OPEN) {
1058  ++n_inprogress;
1059  } else if (or_conn->is_canonical) {
1060  ++n_canonical;
1061  } else {
1062  ++n_other;
1063  }
1064  } SMARTLIST_FOREACH_END(or_conn);
1065 
1066  /* Pass 2: We know how about how good the best connection is.
1067  * expire everything that's worse, and find the very best if we can. */
1068  SMARTLIST_FOREACH_BEGIN(group, or_connection_t *, or_conn) {
1069  if (or_conn->base_.marked_for_close ||
1071  continue; /* This one doesn't need to be marked bad. */
1072  if (or_conn->base_.state != OR_CONN_STATE_OPEN)
1073  continue; /* Don't mark anything bad until we have seen what happens
1074  * when the connection finishes. */
1075  if (n_canonical && !or_conn->is_canonical) {
1076  /* We have at least one open canonical connection to this router,
1077  * and this one is open but not canonical. Mark it bad. */
1078  log_info(LD_OR,
1079  "Marking %s unsuitable for new circuits: "
1080  "(fd "TOR_SOCKET_T_FORMAT", %d secs old). It is not "
1081  "canonical, and we have another connection to that OR that is.",
1082  connection_describe(TO_CONN(or_conn)),
1083  or_conn->base_.s,
1084  (int)(now - or_conn->base_.timestamp_created));
1085  connection_or_mark_bad_for_new_circs(or_conn);
1086  continue;
1087  }
1088 
1089  if (!best ||
1090  channel_is_better(TLS_CHAN_TO_BASE(or_conn->chan),
1091  TLS_CHAN_TO_BASE(best->chan))) {
1092  best = or_conn;
1093  }
1094  } SMARTLIST_FOREACH_END(or_conn);
1095 
1096  if (!best)
1097  return;
1098 
1099  /* Pass 3: One connection to OR is best. If it's canonical, mark as bad
1100  * every other open connection. If it's non-canonical, mark as bad
1101  * every other open connection to the same address.
1102  *
1103  * XXXX This isn't optimal; if we have connections to an OR at multiple
1104  * addresses, we'd like to pick the best _for each address_, and mark as
1105  * bad every open connection that isn't best for its address. But this
1106  * can only occur in cases where the other OR is old (so we have no
1107  * canonical connection to it), or where all the connections to the OR are
1108  * at noncanonical addresses and we have no good direct connection (which
1109  * means we aren't at risk of attaching circuits to it anyway). As
1110  * 0.1.2.x dies out, the first case will go away, and the second one is
1111  * "mostly harmless", so a fix can wait until somebody is bored.
1112  */
1113  SMARTLIST_FOREACH_BEGIN(group, or_connection_t *, or_conn) {
1114  if (or_conn->base_.marked_for_close ||
1116  or_conn->base_.state != OR_CONN_STATE_OPEN)
1117  continue;
1118  if (or_conn != best &&
1119  channel_is_better(TLS_CHAN_TO_BASE(best->chan),
1120  TLS_CHAN_TO_BASE(or_conn->chan))) {
1121  /* This isn't the best conn, _and_ the best conn is better than it */
1122  if (best->is_canonical) {
1123  log_info(LD_OR,
1124  "Marking %s as unsuitable for new circuits: "
1125  "(fd "TOR_SOCKET_T_FORMAT", %d secs old). "
1126  "We have a better canonical one "
1127  "(fd "TOR_SOCKET_T_FORMAT"; %d secs old).",
1128  connection_describe(TO_CONN(or_conn)),
1129  or_conn->base_.s,
1130  (int)(now - or_conn->base_.timestamp_created),
1131  best->base_.s, (int)(now - best->base_.timestamp_created));
1132  connection_or_mark_bad_for_new_circs(or_conn);
1133  } else if (tor_addr_eq(&TO_CONN(or_conn)->addr,
1134  &TO_CONN(best)->addr)) {
1135  log_info(LD_OR,
1136  "Marking %s unsuitable for new circuits: "
1137  "(fd "TOR_SOCKET_T_FORMAT", %d secs old). We have a better "
1138  "one with the "
1139  "same address (fd "TOR_SOCKET_T_FORMAT"; %d secs old).",
1140  connection_describe(TO_CONN(or_conn)),
1141  or_conn->base_.s,
1142  (int)(now - or_conn->base_.timestamp_created),
1143  best->base_.s, (int)(now - best->base_.timestamp_created));
1144  connection_or_mark_bad_for_new_circs(or_conn);
1145  }
1146  }
1147  } SMARTLIST_FOREACH_END(or_conn);
1148 }
1149 
1150 /* Lifetime of a connection failure. After that, we'll retry. This is in
1151  * seconds. */
1152 #define OR_CONNECT_FAILURE_LIFETIME 60
1153 /* The interval to use with when to clean up the failure cache. */
1154 #define OR_CONNECT_FAILURE_CLEANUP_INTERVAL 60
1155 
1156 /* When is the next time we have to cleanup the failure map. We keep this
1157  * because we clean it opportunistically. */
1158 static time_t or_connect_failure_map_next_cleanup_ts = 0;
1159 
1160 /* OR connection failure entry data structure. It is kept in the connection
1161  * failure map defined below and indexed by OR identity digest, address and
1162  * port.
1163  *
1164  * We need to identify a connection failure with these three values because we
1165  * want to avoid to wrongfully block a relay if someone is trying to
1166  * extend to a known identity digest but with the wrong IP/port. For instance,
1167  * it can happen if a relay changed its port but the client still has an old
1168  * descriptor with the old port. We want to stop connecting to that
1169  * IP/port/identity all together, not only the relay identity. */
1171  HT_ENTRY(or_connect_failure_entry_t) node;
1172  /* Identity digest of the connection where it is connecting to. */
1173  uint8_t identity_digest[DIGEST_LEN];
1174  /* This is the connection address from the base connection_t. After the
1175  * connection is checked for canonicity, the base address should represent
1176  * what we know instead of where we are connecting to. This is what we need
1177  * so we can correlate known relays within the consensus. */
1178  tor_addr_t addr;
1179  uint16_t port;
1180  /* Last time we were unable to connect. */
1181  time_t last_failed_connect_ts;
1183 
1184 /* Map where we keep connection failure entries. They are indexed by addr,
1185  * port and identity digest. */
1186 static HT_HEAD(or_connect_failure_ht, or_connect_failure_entry_t)
1187  or_connect_failures_map = HT_INITIALIZER();
1188 
1189 /* Helper: Hashtable equal function. Return 1 if equal else 0. */
1190 static int
1191 or_connect_failure_ht_eq(const or_connect_failure_entry_t *a,
1192  const or_connect_failure_entry_t *b)
1193 {
1194  return fast_memeq(a->identity_digest, b->identity_digest, DIGEST_LEN) &&
1195  tor_addr_eq(&a->addr, &b->addr) &&
1196  a->port == b->port;
1197 }
1198 
1199 /* Helper: Return the hash for the hashtable of the given entry. For this
1200  * table, it is a combination of address, port and identity digest. */
1201 static unsigned int
1202 or_connect_failure_ht_hash(const or_connect_failure_entry_t *entry)
1203 {
1204  size_t offset = 0, addr_size;
1205  const void *addr_ptr;
1206  /* Largest size is IPv6 and IPv4 is smaller so it is fine. */
1207  uint8_t data[16 + sizeof(uint16_t) + DIGEST_LEN];
1208 
1209  /* Get the right address bytes depending on the family. */
1210  switch (tor_addr_family(&entry->addr)) {
1211  case AF_INET:
1212  addr_size = 4;
1213  addr_ptr = &entry->addr.addr.in_addr.s_addr;
1214  break;
1215  case AF_INET6:
1216  addr_size = 16;
1217  addr_ptr = &entry->addr.addr.in6_addr.s6_addr;
1218  break;
1219  default:
1221  return 0;
1222  }
1223 
1224  memcpy(data, addr_ptr, addr_size);
1225  offset += addr_size;
1226  memcpy(data + offset, entry->identity_digest, DIGEST_LEN);
1227  offset += DIGEST_LEN;
1228  set_uint16(data + offset, entry->port);
1229  offset += sizeof(uint16_t);
1230 
1231  return (unsigned int) siphash24g(data, offset);
1232 }
1233 
1234 HT_PROTOTYPE(or_connect_failure_ht, or_connect_failure_entry_t, node,
1235  or_connect_failure_ht_hash, or_connect_failure_ht_eq);
1236 
1237 HT_GENERATE2(or_connect_failure_ht, or_connect_failure_entry_t, node,
1238  or_connect_failure_ht_hash, or_connect_failure_ht_eq,
1240 
1241 /* Initialize a given connect failure entry with the given identity_digest,
1242  * addr and port. All field are optional except ocf. */
1243 static void
1244 or_connect_failure_init(const char *identity_digest, const tor_addr_t *addr,
1245  uint16_t port, or_connect_failure_entry_t *ocf)
1246 {
1247  tor_assert(ocf);
1248  if (identity_digest) {
1249  memcpy(ocf->identity_digest, identity_digest,
1250  sizeof(ocf->identity_digest));
1251  }
1252  if (addr) {
1253  tor_addr_copy(&ocf->addr, addr);
1254  }
1255  ocf->port = port;
1256 }
1257 
1258 /* Return a newly allocated connection failure entry. It is initialized with
1259  * the given or_conn data. This can't fail. */
1261 or_connect_failure_new(const or_connection_t *or_conn)
1262 {
1263  or_connect_failure_entry_t *ocf = tor_malloc_zero(sizeof(*ocf));
1264  or_connect_failure_init(or_conn->identity_digest, &TO_CONN(or_conn)->addr,
1265  TO_CONN(or_conn)->port, ocf);
1266  return ocf;
1267 }
1268 
1269 /* Return a connection failure entry matching the given or_conn. NULL is
1270  * returned if not found. */
1272 or_connect_failure_find(const or_connection_t *or_conn)
1273 {
1275  tor_assert(or_conn);
1276  or_connect_failure_init(or_conn->identity_digest, &TO_CONN(or_conn)->addr,
1277  TO_CONN(or_conn)->port, &lookup);
1278  return HT_FIND(or_connect_failure_ht, &or_connect_failures_map, &lookup);
1279 }
1280 
1281 /* Note down in the connection failure cache that a failure occurred on the
1282  * given or_conn. */
1283 STATIC void
1284 note_or_connect_failed(const or_connection_t *or_conn)
1285 {
1286  or_connect_failure_entry_t *ocf = NULL;
1287 
1288  tor_assert(or_conn);
1289 
1290  ocf = or_connect_failure_find(or_conn);
1291  if (ocf == NULL) {
1292  ocf = or_connect_failure_new(or_conn);
1293  HT_INSERT(or_connect_failure_ht, &or_connect_failures_map, ocf);
1294  }
1295  ocf->last_failed_connect_ts = approx_time();
1296 }
1297 
1298 /* Cleanup the connection failure cache and remove all entries below the
1299  * given cutoff. */
1300 static void
1301 or_connect_failure_map_cleanup(time_t cutoff)
1302 {
1303  or_connect_failure_entry_t **ptr, **next, *entry;
1304 
1305  for (ptr = HT_START(or_connect_failure_ht, &or_connect_failures_map);
1306  ptr != NULL; ptr = next) {
1307  entry = *ptr;
1308  if (entry->last_failed_connect_ts <= cutoff) {
1309  next = HT_NEXT_RMV(or_connect_failure_ht, &or_connect_failures_map, ptr);
1310  tor_free(entry);
1311  } else {
1312  next = HT_NEXT(or_connect_failure_ht, &or_connect_failures_map, ptr);
1313  }
1314  }
1315 }
1316 
1317 /* Return true iff the given OR connection can connect to its destination that
1318  * is the triplet identity_digest, address and port.
1319  *
1320  * The or_conn MUST have gone through connection_or_check_canonicity() so the
1321  * base address is properly set to what we know or doesn't know. */
1322 STATIC int
1323 should_connect_to_relay(const or_connection_t *or_conn)
1324 {
1325  time_t now, cutoff;
1326  time_t connect_failed_since_ts = 0;
1328 
1329  tor_assert(or_conn);
1330 
1331  now = approx_time();
1332  cutoff = now - OR_CONNECT_FAILURE_LIFETIME;
1333 
1334  /* Opportunistically try to cleanup the failure cache. We do that at regular
1335  * interval so it doesn't grow too big. */
1336  if (or_connect_failure_map_next_cleanup_ts <= now) {
1337  or_connect_failure_map_cleanup(cutoff);
1338  or_connect_failure_map_next_cleanup_ts =
1339  now + OR_CONNECT_FAILURE_CLEANUP_INTERVAL;
1340  }
1341 
1342  /* Look if we have failed previously to the same destination as this
1343  * OR connection. */
1344  ocf = or_connect_failure_find(or_conn);
1345  if (ocf) {
1346  connect_failed_since_ts = ocf->last_failed_connect_ts;
1347  }
1348  /* If we do have an unable to connect timestamp and it is below cutoff, we
1349  * can connect. Or we have never failed before so let it connect. */
1350  if (connect_failed_since_ts > cutoff) {
1351  goto no_connect;
1352  }
1353 
1354  /* Ok we can connect! */
1355  return 1;
1356  no_connect:
1357  return 0;
1358 }
1359 
1360 /** <b>conn</b> is in the 'connecting' state, and it failed to complete
1361  * a TCP connection. Send notifications appropriately.
1362  *
1363  * <b>reason</b> specifies the or_conn_end_reason for the failure;
1364  * <b>msg</b> specifies the strerror-style error message.
1365  */
1366 void
1368  int reason, const char *msg)
1369 {
1370  connection_or_event_status(conn, OR_CONN_EVENT_FAILED, reason);
1372  control_event_bootstrap_prob_or(msg, reason, conn);
1373  note_or_connect_failed(conn);
1374 }
1375 
1376 /** <b>conn</b> got an error in connection_handle_read_impl() or
1377  * connection_handle_write_impl() and is going to die soon.
1378  *
1379  * <b>reason</b> specifies the or_conn_end_reason for the failure;
1380  * <b>msg</b> specifies the strerror-style error message.
1381  */
1382 void
1384  int reason, const char *msg)
1385 {
1386  channel_t *chan;
1387 
1388  tor_assert(conn);
1389 
1390  /* If we're connecting, call connect_failed() too */
1391  if (TO_CONN(conn)->state == OR_CONN_STATE_CONNECTING)
1392  connection_or_connect_failed(conn, reason, msg);
1393 
1394  /* Tell the controlling channel if we have one */
1395  if (conn->chan) {
1396  chan = TLS_CHAN_TO_BASE(conn->chan);
1397  /* Don't transition if we're already in closing, closed or error */
1398  if (!CHANNEL_CONDEMNED(chan)) {
1400  }
1401  }
1402 
1403  /* No need to mark for error because connection.c is about to do that */
1404 }
1405 
1406 /** Launch a new OR connection to <b>addr</b>:<b>port</b> and expect to
1407  * handshake with an OR with identity digest <b>id_digest</b>. Optionally,
1408  * pass in a pointer to a channel using this connection.
1409  *
1410  * If <b>id_digest</b> is me, do nothing. If we're already connected to it,
1411  * return that connection. If the connect() is in progress, set the
1412  * new conn's state to 'connecting' and return it. If connect() succeeds,
1413  * call connection_tls_start_handshake() on it.
1414  *
1415  * This function is called from router_retry_connections(), for
1416  * ORs connecting to ORs, and circuit_establish_circuit(), for
1417  * OPs connecting to ORs.
1418  *
1419  * Return the launched conn, or NULL if it failed.
1420  */
1421 
1423 connection_or_connect, (const tor_addr_t *_addr, uint16_t port,
1424  const char *id_digest,
1425  const ed25519_public_key_t *ed_id,
1426  channel_tls_t *chan))
1427 {
1428  or_connection_t *conn;
1429  const or_options_t *options = get_options();
1430  int socket_error = 0;
1431  tor_addr_t addr;
1432 
1433  int r;
1434  tor_addr_t proxy_addr;
1435  uint16_t proxy_port;
1436  int proxy_type, is_pt = 0;
1437 
1438  tor_assert(_addr);
1439  tor_assert(id_digest);
1440  tor_addr_copy(&addr, _addr);
1441 
1442  if (server_mode(options) && router_digest_is_me(id_digest)) {
1443  log_info(LD_PROTOCOL,"Client asked me to connect to myself. Refusing.");
1444  return NULL;
1445  }
1446  if (server_mode(options) && router_ed25519_id_is_me(ed_id)) {
1447  log_info(LD_PROTOCOL,"Client asked me to connect to myself by Ed25519 "
1448  "identity. Refusing.");
1449  return NULL;
1450  }
1451 
1453 
1454  /*
1455  * Set up conn so it's got all the data we need to remember for channels
1456  *
1457  * This stuff needs to happen before connection_or_init_conn_from_address()
1458  * so connection_or_set_identity_digest() and such know where to look to
1459  * keep the channel up to date.
1460  */
1461  conn->chan = chan;
1462  chan->conn = conn;
1463  connection_or_init_conn_from_address(conn, &addr, port, id_digest, ed_id, 1);
1464 
1465  /* We have a proper OR connection setup, now check if we can connect to it
1466  * that is we haven't had a failure earlier. This is to avoid to try to
1467  * constantly connect to relays that we think are not reachable. */
1468  if (!should_connect_to_relay(conn)) {
1469  log_info(LD_GENERAL, "Can't connect to %s because we "
1470  "failed earlier. Refusing.",
1472  connection_free_(TO_CONN(conn));
1473  return NULL;
1474  }
1475 
1476  conn->is_outgoing = 1;
1477 
1478  /* If we are using a proxy server, find it and use it. */
1479  r = get_proxy_addrport(&proxy_addr, &proxy_port, &proxy_type, &is_pt,
1480  TO_CONN(conn));
1481  if (r == 0) {
1482  conn->proxy_type = proxy_type;
1483  if (proxy_type != PROXY_NONE) {
1484  tor_addr_copy(&addr, &proxy_addr);
1485  port = proxy_port;
1486  conn->base_.proxy_state = PROXY_INFANT;
1487  conn->is_pt = is_pt;
1488  }
1490  connection_or_event_status(conn, OR_CONN_EVENT_LAUNCHED, 0);
1491  } else {
1492  /* This duplication of state change calls is necessary in case we
1493  * run into an error condition below */
1495  connection_or_event_status(conn, OR_CONN_EVENT_LAUNCHED, 0);
1496 
1497  /* get_proxy_addrport() might fail if we have a Bridge line that
1498  references a transport, but no ClientTransportPlugin lines
1499  defining its transport proxy. If this is the case, let's try to
1500  output a useful log message to the user. */
1501  const char *transport_name =
1503  TO_CONN(conn)->port);
1504 
1505  if (transport_name) {
1506  log_warn(LD_GENERAL, "We were supposed to connect to bridge '%s' "
1507  "using pluggable transport '%s', but we can't find a pluggable "
1508  "transport proxy supporting '%s'. This can happen if you "
1509  "haven't provided a ClientTransportPlugin line, or if "
1510  "your pluggable transport proxy stopped running.",
1512  transport_name, transport_name);
1513 
1515  "Can't connect to bridge",
1516  END_OR_CONN_REASON_PT_MISSING,
1517  conn);
1518 
1519  } else {
1520  log_warn(LD_GENERAL, "Tried to connect to %s through a proxy, but "
1521  "the proxy address could not be found.",
1523  }
1524 
1525  connection_free_(TO_CONN(conn));
1526  return NULL;
1527  }
1528 
1529  switch (connection_connect(TO_CONN(conn), conn->base_.address,
1530  &addr, port, &socket_error)) {
1531  case -1:
1532  /* We failed to establish a connection probably because of a local
1533  * error. No need to blame the guard in this case. Notify the networking
1534  * system of this failure. */
1536  errno_to_orconn_end_reason(socket_error),
1537  tor_socket_strerror(socket_error));
1538  connection_free_(TO_CONN(conn));
1539  return NULL;
1540  case 0:
1542  /* writable indicates finish, readable indicates broken link,
1543  error indicates broken link on windows */
1544  return conn;
1545  /* case 1: fall through */
1546  }
1547 
1548  if (connection_or_finished_connecting(conn) < 0) {
1549  /* already marked for close */
1550  return NULL;
1551  }
1552  return conn;
1553 }
1554 
1555 /** Mark orconn for close and transition the associated channel, if any, to
1556  * the closing state.
1557  *
1558  * It's safe to call this and connection_or_close_for_error() any time, and
1559  * channel layer will treat it as a connection closing for reasons outside
1560  * its control, like the remote end closing it. It can also be a local
1561  * reason that's specific to connection_t/or_connection_t rather than
1562  * the channel mechanism, such as expiration of old connections in
1563  * run_connection_housekeeping(). If you want to close a channel_t
1564  * from somewhere that logically works in terms of generic channels
1565  * rather than connections, use channel_mark_for_close(); see also
1566  * the comment on that function in channel.c.
1567  */
1568 
1569 void
1571 {
1572  channel_t *chan = NULL;
1573 
1574  tor_assert(orconn);
1575  if (flush) connection_mark_and_flush_internal(TO_CONN(orconn));
1576  else connection_mark_for_close_internal(TO_CONN(orconn));
1577  if (orconn->chan) {
1578  chan = TLS_CHAN_TO_BASE(orconn->chan);
1579  /* Don't transition if we're already in closing, closed or error */
1580  if (!CHANNEL_CONDEMNED(chan)) {
1582  }
1583  }
1584 }
1585 
1586 /** Mark orconn for close and transition the associated channel, if any, to
1587  * the error state.
1588  */
1589 
1590 MOCK_IMPL(void,
1592 {
1593  channel_t *chan = NULL;
1594 
1595  tor_assert(orconn);
1596  if (flush) connection_mark_and_flush_internal(TO_CONN(orconn));
1597  else connection_mark_for_close_internal(TO_CONN(orconn));
1598  if (orconn->chan) {
1599  chan = TLS_CHAN_TO_BASE(orconn->chan);
1600  /* Don't transition if we're already in closing, closed or error */
1601  if (!CHANNEL_CONDEMNED(chan)) {
1603  }
1604  }
1605 }
1606 
1607 /** Begin the tls handshake with <b>conn</b>. <b>receiving</b> is 0 if
1608  * we initiated the connection, else it's 1.
1609  *
1610  * Assign a new tls object to conn->tls, begin reading on <b>conn</b>, and
1611  * pass <b>conn</b> to connection_tls_continue_handshake().
1612  *
1613  * Return -1 if <b>conn</b> is broken, else return 0.
1614  */
1615 MOCK_IMPL(int,
1617 {
1618  channel_listener_t *chan_listener;
1619  channel_t *chan;
1620 
1621  /* Incoming connections will need a new channel passed to the
1622  * channel_tls_listener */
1623  if (receiving) {
1624  /* It shouldn't already be set */
1625  tor_assert(!(conn->chan));
1626  chan_listener = channel_tls_get_listener();
1627  if (!chan_listener) {
1628  chan_listener = channel_tls_start_listener();
1629  command_setup_listener(chan_listener);
1630  }
1631  chan = channel_tls_handle_incoming(conn);
1632  channel_listener_queue_incoming(chan_listener, chan);
1633  }
1634 
1636  tor_assert(!conn->tls);
1637  conn->tls = tor_tls_new(conn->base_.s, receiving);
1638  if (!conn->tls) {
1639  log_warn(LD_BUG,"tor_tls_new failed. Closing.");
1640  return -1;
1641  }
1644 
1646  log_debug(LD_HANDSHAKE,"starting TLS handshake on fd "TOR_SOCKET_T_FORMAT,
1647  conn->base_.s);
1648 
1649  if (connection_tls_continue_handshake(conn) < 0)
1650  return -1;
1651 
1652  return 0;
1653 }
1654 
1655 /** Block all future attempts to renegotiate on 'conn' */
1656 void
1658 {
1659  tor_tls_t *tls = conn->tls;
1660  if (!tls)
1661  return;
1662  tor_tls_set_renegotiate_callback(tls, NULL, NULL);
1664 }
1665 
1666 /** Invoked on the server side from inside tor_tls_read() when the server
1667  * gets a successful TLS renegotiation from the client. */
1668 static void
1670 {
1671  or_connection_t *conn = _conn;
1672  (void)tls;
1673 
1674  /* Don't invoke this again. */
1676 
1677  if (connection_tls_finish_handshake(conn) < 0) {
1678  /* XXXX_TLS double-check that it's ok to do this from inside read. */
1679  /* XXXX_TLS double-check that this verifies certificates. */
1681  }
1682 }
1683 
1684 /** Move forward with the tls handshake. If it finishes, hand
1685  * <b>conn</b> to connection_tls_finish_handshake().
1686  *
1687  * Return -1 if <b>conn</b> is broken, else return 0.
1688  */
1689 int
1691 {
1692  int result;
1693  check_no_tls_errors();
1694 
1696  // log_notice(LD_OR, "Continue handshake with %p", conn->tls);
1697  result = tor_tls_handshake(conn->tls);
1698  // log_notice(LD_OR, "Result: %d", result);
1699 
1700  switch (result) {
1702  conn->tls_error = result;
1703  log_info(LD_OR,"tls error [%s]. breaking connection.",
1704  tor_tls_err_to_string(result));
1705  return -1;
1706  case TOR_TLS_DONE:
1707  if (! tor_tls_used_v1_handshake(conn->tls)) {
1708  if (!tor_tls_is_server(conn->tls)) {
1711  } else {
1712  /* v2/v3 handshake, but we are not a client. */
1713  log_debug(LD_OR, "Done with initial SSL handshake (server-side). "
1714  "Expecting renegotiation or VERSIONS cell");
1717  conn);
1722  return 0;
1723  }
1724  }
1726  return connection_tls_finish_handshake(conn);
1727  case TOR_TLS_WANTWRITE:
1729  log_debug(LD_OR,"wanted write");
1730  return 0;
1731  case TOR_TLS_WANTREAD: /* handshaking conns are *always* reading */
1732  log_debug(LD_OR,"wanted read");
1733  return 0;
1734  case TOR_TLS_CLOSE:
1735  conn->tls_error = result;
1736  log_info(LD_OR,"tls closed. breaking connection.");
1737  return -1;
1738  }
1739  return 0;
1740 }
1741 
1742 /** Return 1 if we initiated this connection, or 0 if it started
1743  * out as an incoming connection.
1744  */
1745 int
1747 {
1748  tor_assert(conn->base_.type == CONN_TYPE_OR ||
1749  conn->base_.type == CONN_TYPE_EXT_OR);
1750  if (!conn->tls)
1751  return 1; /* it's still in proxy states or something */
1752  if (conn->handshake_state)
1753  return conn->handshake_state->started_here;
1754  return !tor_tls_is_server(conn->tls);
1755 }
1756 
1757 /** <b>Conn</b> just completed its handshake. Return 0 if all is well, and
1758  * return -1 if they are lying, broken, or otherwise something is wrong.
1759  *
1760  * If we initiated this connection (<b>started_here</b> is true), make sure
1761  * the other side sent a correctly formed certificate. If I initiated the
1762  * connection, make sure it's the right relay by checking the certificate.
1763  *
1764  * Otherwise (if we _didn't_ initiate this connection), it's okay for
1765  * the certificate to be weird or absent.
1766  *
1767  * If we return 0, and the certificate is as expected, write a hash of the
1768  * identity key into <b>digest_rcvd_out</b>, which must have DIGEST_LEN
1769  * space in it.
1770  * If the certificate is invalid or missing on an incoming connection,
1771  * we return 0 and set <b>digest_rcvd_out</b> to DIGEST_LEN NUL bytes.
1772  * (If we return -1, the contents of this buffer are undefined.)
1773  *
1774  * As side effects,
1775  * 1) Set conn->circ_id_type according to tor-spec.txt.
1776  * 2) If we're an authdirserver and we initiated the connection: drop all
1777  * descriptors that claim to be on that IP/port but that aren't
1778  * this relay; and note that this relay is reachable.
1779  * 3) If this is a bridge and we didn't configure its identity
1780  * fingerprint, remember the keyid we just learned.
1781  */
1782 static int
1784  int started_here,
1785  char *digest_rcvd_out)
1786 {
1787  crypto_pk_t *identity_rcvd=NULL;
1788  const or_options_t *options = get_options();
1789  int severity = server_mode(options) ? LOG_PROTOCOL_WARN : LOG_WARN;
1790  const char *conn_type = started_here ? "outgoing" : "incoming";
1791  int has_cert = 0;
1792 
1793  check_no_tls_errors();
1794  has_cert = tor_tls_peer_has_cert(conn->tls);
1795  if (started_here && !has_cert) {
1796  log_info(LD_HANDSHAKE,"Tried connecting to router at %s, but it didn't "
1797  "send a cert! Closing.",
1799  return -1;
1800  } else if (!has_cert) {
1801  log_debug(LD_HANDSHAKE,"Got incoming connection with no certificate. "
1802  "That's ok.");
1803  }
1804  check_no_tls_errors();
1805 
1806  if (has_cert) {
1807  int v = tor_tls_verify(started_here?severity:LOG_INFO,
1808  conn->tls, &identity_rcvd);
1809  if (started_here && v<0) {
1810  log_fn(severity,LD_HANDSHAKE,"Tried connecting to router at %s: It"
1811  " has a cert but it's invalid. Closing.",
1813  return -1;
1814  } else if (v<0) {
1815  log_info(LD_HANDSHAKE,"Incoming connection gave us an invalid cert "
1816  "chain; ignoring.");
1817  } else {
1818  log_debug(LD_HANDSHAKE,
1819  "The certificate seems to be valid on %s connection "
1820  "with %s", conn_type,
1822  }
1823  check_no_tls_errors();
1824  }
1825 
1826  if (identity_rcvd) {
1827  if (crypto_pk_get_digest(identity_rcvd, digest_rcvd_out) < 0) {
1828  crypto_pk_free(identity_rcvd);
1829  return -1;
1830  }
1831  } else {
1832  memset(digest_rcvd_out, 0, DIGEST_LEN);
1833  }
1834 
1835  tor_assert(conn->chan);
1836  channel_set_circid_type(TLS_CHAN_TO_BASE(conn->chan), identity_rcvd, 1);
1837 
1838  crypto_pk_free(identity_rcvd);
1839 
1840  if (started_here) {
1841  /* A TLS handshake can't teach us an Ed25519 ID, so we set it to NULL
1842  * here. */
1843  log_debug(LD_HANDSHAKE, "Calling client_learned_peer_id from "
1844  "check_valid_tls_handshake");
1846  (const uint8_t*)digest_rcvd_out,
1847  NULL);
1848  }
1849 
1850  return 0;
1851 }
1852 
1853 /** Called when we (as a connection initiator) have definitively,
1854  * authenticatedly, learned that ID of the Tor instance on the other
1855  * side of <b>conn</b> is <b>rsa_peer_id</b> and optionally <b>ed_peer_id</b>.
1856  * For v1 and v2 handshakes,
1857  * this is right after we get a certificate chain in a TLS handshake
1858  * or renegotiation. For v3+ handshakes, this is right after we get a
1859  * certificate chain in a CERTS cell.
1860  *
1861  * If we did not know the ID before, record the one we got.
1862  *
1863  * If we wanted an ID, but we didn't get the one we expected, log a message
1864  * and return -1.
1865  * On relays:
1866  * - log a protocol warning whenever the fingerprints don't match;
1867  * On clients:
1868  * - if a relay's fingerprint doesn't match, log a warning;
1869  * - if we don't have updated relay fingerprints from a recent consensus, and
1870  * a fallback directory mirror's hard-coded fingerprint has changed, log an
1871  * info explaining that we will try another fallback.
1872  *
1873  * If we're testing reachability, remember what we learned.
1874  *
1875  * Return 0 on success, -1 on failure.
1876  */
1877 int
1879  const uint8_t *rsa_peer_id,
1880  const ed25519_public_key_t *ed_peer_id)
1881 {
1882  const or_options_t *options = get_options();
1883  channel_tls_t *chan_tls = conn->chan;
1884  channel_t *chan = channel_tls_to_base(chan_tls);
1885  int changed_identity = 0;
1886  tor_assert(chan);
1887 
1888  const int expected_rsa_key =
1890  const int expected_ed_key =
1892 
1893  log_info(LD_HANDSHAKE, "learned peer id for %s at %p: %s, %s",
1895  conn,
1896  hex_str((const char*)rsa_peer_id, DIGEST_LEN),
1897  ed25519_fmt(ed_peer_id));
1898 
1899  if (! expected_rsa_key && ! expected_ed_key) {
1900  log_info(LD_HANDSHAKE, "(we had no ID in mind when we made this "
1901  "connection.");
1903  (const char*)rsa_peer_id, ed_peer_id);
1904  tor_free(conn->nickname);
1905  conn->nickname = tor_malloc(HEX_DIGEST_LEN+2);
1906  conn->nickname[0] = '$';
1908  conn->identity_digest, DIGEST_LEN);
1909  log_info(LD_HANDSHAKE, "Connected to router at %s without knowing "
1910  "its key. Hoping for the best.",
1912  /* if it's a bridge and we didn't know its identity fingerprint, now
1913  * we do -- remember it for future attempts. */
1914  learned_router_identity(&conn->base_.addr, conn->base_.port,
1915  (const char*)rsa_peer_id, ed_peer_id);
1916  changed_identity = 1;
1917  }
1918 
1919  const int rsa_mismatch = expected_rsa_key &&
1920  tor_memneq(rsa_peer_id, conn->identity_digest, DIGEST_LEN);
1921  /* It only counts as an ed25519 mismatch if we wanted an ed25519 identity
1922  * and didn't get it. It's okay if we get one that we didn't ask for. */
1923  const int ed25519_mismatch =
1924  expected_ed_key &&
1925  (ed_peer_id == NULL ||
1926  ! ed25519_pubkey_eq(&chan->ed25519_identity, ed_peer_id));
1927 
1928  if (rsa_mismatch || ed25519_mismatch) {
1929  /* I was aiming for a particular digest. I didn't get it! */
1930  char seen_rsa[HEX_DIGEST_LEN+1];
1931  char expected_rsa[HEX_DIGEST_LEN+1];
1932  char seen_ed[ED25519_BASE64_LEN+1];
1933  char expected_ed[ED25519_BASE64_LEN+1];
1934  base16_encode(seen_rsa, sizeof(seen_rsa),
1935  (const char*)rsa_peer_id, DIGEST_LEN);
1936  base16_encode(expected_rsa, sizeof(expected_rsa), conn->identity_digest,
1937  DIGEST_LEN);
1938  if (ed_peer_id) {
1939  ed25519_public_to_base64(seen_ed, ed_peer_id);
1940  } else {
1941  strlcpy(seen_ed, "no ed25519 key", sizeof(seen_ed));
1942  }
1944  ed25519_public_to_base64(expected_ed, &chan->ed25519_identity);
1945  } else {
1946  strlcpy(expected_ed, "no ed25519 key", sizeof(expected_ed));
1947  }
1948  const int using_hardcoded_fingerprints =
1951  const int is_fallback_fingerprint = router_digest_is_fallback_dir(
1952  conn->identity_digest);
1953  const int is_authority_fingerprint = router_digest_is_trusted_dir(
1954  conn->identity_digest);
1955  const int non_anonymous_mode = rend_non_anonymous_mode_enabled(options);
1956  int severity;
1957  const char *extra_log = "";
1958 
1959  /* Relays and Single Onion Services make direct connections using
1960  * untrusted authentication keys. */
1961  if (server_mode(options) || non_anonymous_mode) {
1962  severity = LOG_PROTOCOL_WARN;
1963  } else {
1964  if (using_hardcoded_fingerprints) {
1965  /* We need to do the checks in this order, because the list of
1966  * fallbacks includes the list of authorities */
1967  if (is_authority_fingerprint) {
1968  severity = LOG_WARN;
1969  } else if (is_fallback_fingerprint) {
1970  /* we expect a small number of fallbacks to change from their
1971  * hard-coded fingerprints over the life of a release */
1972  severity = LOG_INFO;
1973  extra_log = " Tor will try a different fallback.";
1974  } else {
1975  /* it's a bridge, it's either a misconfiguration, or unexpected */
1976  severity = LOG_WARN;
1977  }
1978  } else {
1979  /* a relay has changed its fingerprint from the one in the consensus */
1980  severity = LOG_WARN;
1981  }
1982  }
1983 
1984  log_fn(severity, LD_HANDSHAKE,
1985  "Tried connecting to router at %s, but RSA + ed25519 identity "
1986  "keys were not as expected: wanted %s + %s but got %s + %s.%s",
1988  expected_rsa, expected_ed, seen_rsa, seen_ed, extra_log);
1989 
1990  /* Tell the new guard API about the channel failure */
1991  entry_guard_chan_failed(TLS_CHAN_TO_BASE(conn->chan));
1992  connection_or_event_status(conn, OR_CONN_EVENT_FAILED,
1993  END_OR_CONN_REASON_OR_IDENTITY);
1994  if (!authdir_mode_tests_reachability(options))
1996  "Unexpected identity in router certificate",
1997  END_OR_CONN_REASON_OR_IDENTITY,
1998  conn);
1999  return -1;
2000  }
2001 
2002  if (!expected_ed_key && ed_peer_id) {
2003  log_info(LD_HANDSHAKE, "(We had no Ed25519 ID in mind when we made this "
2004  "connection.)");
2006  (const char*)rsa_peer_id, ed_peer_id);
2007  changed_identity = 1;
2008  }
2009 
2010  if (changed_identity) {
2011  /* If we learned an identity for this connection, then we might have
2012  * just discovered it to be canonical. */
2014  if (conn->tls)
2017  }
2018 
2019  if (authdir_mode_tests_reachability(options)) {
2020  // We don't want to use canonical_orport here -- we want the address
2021  // that we really used.
2022  dirserv_orconn_tls_done(&conn->base_.addr, conn->base_.port,
2023  (const char*)rsa_peer_id, ed_peer_id);
2024  }
2025 
2026  return 0;
2027 }
2028 
2029 /** Return when we last used this channel for client activity (origin
2030  * circuits). This is called from connection.c, since client_used is now one
2031  * of the timestamps in channel_t */
2032 
2033 time_t
2035 {
2036  tor_assert(conn);
2037 
2038  if (conn->chan) {
2039  return channel_when_last_client(TLS_CHAN_TO_BASE(conn->chan));
2040  } else return 0;
2041 }
2042 
2043 /** The v1/v2 TLS handshake is finished.
2044  *
2045  * Make sure we are happy with the peer we just handshaked with.
2046  *
2047  * If they initiated the connection, make sure they're not already connected,
2048  * then initialize conn from the information in router.
2049  *
2050  * If all is successful, call circuit_n_conn_done() to handle events
2051  * that have been pending on the <tls handshake completion. Also set the
2052  * directory to be dirty (only matters if I'm an authdirserver).
2053  *
2054  * If this is a v2 TLS handshake, send a versions cell.
2055  */
2056 static int
2058 {
2059  char digest_rcvd[DIGEST_LEN];
2060  int started_here = connection_or_nonopen_was_started_here(conn);
2061 
2062  tor_assert(!started_here);
2063 
2064  log_debug(LD_HANDSHAKE,"%s tls handshake on %s done, using "
2065  "ciphersuite %s. verifying.",
2066  started_here?"outgoing":"incoming",
2068  tor_tls_get_ciphersuite_name(conn->tls));
2069 
2070  if (connection_or_check_valid_tls_handshake(conn, started_here,
2071  digest_rcvd) < 0)
2072  return -1;
2073 
2075 
2076  if (tor_tls_used_v1_handshake(conn->tls)) {
2077  conn->link_proto = 1;
2078  connection_or_init_conn_from_address(conn, &conn->base_.addr,
2079  conn->base_.port, digest_rcvd,
2080  NULL, 0);
2082  rep_hist_note_negotiated_link_proto(1, started_here);
2083  return connection_or_set_state_open(conn);
2084  } else {
2086  if (connection_init_or_handshake_state(conn, started_here) < 0)
2087  return -1;
2088  connection_or_init_conn_from_address(conn, &conn->base_.addr,
2089  conn->base_.port, digest_rcvd,
2090  NULL, 0);
2091  return connection_or_send_versions(conn, 0);
2092  }
2093 }
2094 
2095 /**
2096  * Called as client when initial TLS handshake is done, and we notice
2097  * that we got a v3-handshake signalling certificate from the server.
2098  * Set up structures, do bookkeeping, and send the versions cell.
2099  * Return 0 on success and -1 on failure.
2100  */
2101 static int
2103 {
2105 
2107 
2109  if (connection_init_or_handshake_state(conn, 1) < 0)
2110  return -1;
2111 
2112  return connection_or_send_versions(conn, 1);
2113 }
2114 
2115 /** Allocate a new connection handshake state for the connection
2116  * <b>conn</b>. Return 0 on success, -1 on failure. */
2117 int
2119 {
2121  if (conn->handshake_state) {
2122  log_warn(LD_BUG, "Duplicate call to connection_init_or_handshake_state!");
2123  return 0;
2124  }
2125  s = conn->handshake_state = tor_malloc_zero(sizeof(or_handshake_state_t));
2126  s->started_here = started_here ? 1 : 0;
2127  s->digest_sent_data = 1;
2128  s->digest_received_data = 1;
2129  if (! started_here && get_current_link_cert_cert()) {
2130  s->own_link_cert = tor_cert_dup(get_current_link_cert_cert());
2131  }
2133  s->certs->started_here = s->started_here;
2134  return 0;
2135 }
2136 
2137 /** Free all storage held by <b>state</b>. */
2138 void
2140 {
2141  if (!state)
2142  return;
2144  crypto_digest_free(state->digest_received);
2145  or_handshake_certs_free(state->certs);
2146  tor_cert_free(state->own_link_cert);
2147  memwipe(state, 0xBE, sizeof(or_handshake_state_t));
2148  tor_free(state);
2149 }
2150 
2151 /**
2152  * Remember that <b>cell</b> has been transmitted (if <b>incoming</b> is
2153  * false) or received (if <b>incoming</b> is true) during a V3 handshake using
2154  * <b>state</b>.
2155  *
2156  * (We don't record the cell, but we keep a digest of everything sent or
2157  * received during the v3 handshake, and the client signs it in an
2158  * authenticate cell.)
2159  */
2160 void
2162  or_handshake_state_t *state,
2163  const cell_t *cell,
2164  int incoming)
2165 {
2166  size_t cell_network_size = get_cell_network_size(conn->wide_circ_ids);
2167  crypto_digest_t *d, **dptr;
2168  packed_cell_t packed;
2169  if (incoming) {
2170  if (!state->digest_received_data)
2171  return;
2172  } else {
2173  if (!state->digest_sent_data)
2174  return;
2175  }
2176  if (!incoming) {
2177  log_warn(LD_BUG, "We shouldn't be sending any non-variable-length cells "
2178  "while making a handshake digest. But we think we are sending "
2179  "one with type %d.", (int)cell->command);
2180  }
2181  dptr = incoming ? &state->digest_received : &state->digest_sent;
2182  if (! *dptr)
2183  *dptr = crypto_digest256_new(DIGEST_SHA256);
2184 
2185  d = *dptr;
2186  /* Re-packing like this is a little inefficient, but we don't have to do
2187  this very often at all. */
2188  cell_pack(&packed, cell, conn->wide_circ_ids);
2189  crypto_digest_add_bytes(d, packed.body, cell_network_size);
2190  memwipe(&packed, 0, sizeof(packed));
2191 }
2192 
2193 /** Remember that a variable-length <b>cell</b> has been transmitted (if
2194  * <b>incoming</b> is false) or received (if <b>incoming</b> is true) during a
2195  * V3 handshake using <b>state</b>.
2196  *
2197  * (We don't record the cell, but we keep a digest of everything sent or
2198  * received during the v3 handshake, and the client signs it in an
2199  * authenticate cell.)
2200  */
2201 void
2203  or_handshake_state_t *state,
2204  const var_cell_t *cell,
2205  int incoming)
2206 {
2207  crypto_digest_t *d, **dptr;
2208  int n;
2209  char buf[VAR_CELL_MAX_HEADER_SIZE];
2210  if (incoming) {
2211  if (!state->digest_received_data)
2212  return;
2213  } else {
2214  if (!state->digest_sent_data)
2215  return;
2216  }
2217  dptr = incoming ? &state->digest_received : &state->digest_sent;
2218  if (! *dptr)
2219  *dptr = crypto_digest256_new(DIGEST_SHA256);
2220 
2221  d = *dptr;
2222 
2223  n = var_cell_pack_header(cell, buf, conn->wide_circ_ids);
2224  crypto_digest_add_bytes(d, buf, n);
2225  crypto_digest_add_bytes(d, (const char *)cell->payload, cell->payload_len);
2226 
2227  memwipe(buf, 0, sizeof(buf));
2228 }
2229 
2230 /** Set <b>conn</b>'s state to OR_CONN_STATE_OPEN, and tell other subsystems
2231  * as appropriate. Called when we are done with all TLS and OR handshaking.
2232  */
2233 int
2235 {
2237  connection_or_event_status(conn, OR_CONN_EVENT_CONNECTED, 0);
2238 
2239  /* Link protocol 3 appeared in Tor 0.2.3.6-alpha, so any connection
2240  * that uses an earlier link protocol should not be treated as a relay. */
2241  if (conn->link_proto < 3) {
2242  channel_mark_client(TLS_CHAN_TO_BASE(conn->chan));
2243  }
2244 
2245  or_handshake_state_free(conn->handshake_state);
2246  conn->handshake_state = NULL;
2248 
2249  return 0;
2250 }
2251 
2252 /** Pack <b>cell</b> into wire-format, and write it onto <b>conn</b>'s outbuf.
2253  * For cells that use or affect a circuit, this should only be called by
2254  * connection_or_flush_from_first_active_circuit().
2255  */
2256 void
2258 {
2259  packed_cell_t networkcell;
2260  size_t cell_network_size = get_cell_network_size(conn->wide_circ_ids);
2261 
2262  tor_assert(cell);
2263  tor_assert(conn);
2264 
2265  cell_pack(&networkcell, cell, conn->wide_circ_ids);
2266 
2267  /* We need to count padding cells from this non-packed code path
2268  * since they are sent via chan->write_cell() (which is not packed) */
2270  if (cell->command == CELL_PADDING)
2272 
2273  connection_buf_add(networkcell.body, cell_network_size, TO_CONN(conn));
2274 
2275  /* Touch the channel's active timestamp if there is one */
2276  if (conn->chan) {
2277  channel_timestamp_active(TLS_CHAN_TO_BASE(conn->chan));
2278 
2279  if (TLS_CHAN_TO_BASE(conn->chan)->padding_enabled) {
2281  if (cell->command == CELL_PADDING)
2283  }
2284  }
2285 
2286  if (conn->base_.state == OR_CONN_STATE_OR_HANDSHAKING_V3)
2287  or_handshake_state_record_cell(conn, conn->handshake_state, cell, 0);
2288 }
2289 
2290 /** Pack a variable-length <b>cell</b> into wire-format, and write it onto
2291  * <b>conn</b>'s outbuf. Right now, this <em>DOES NOT</em> support cells that
2292  * affect a circuit.
2293  */
2294 MOCK_IMPL(void,
2297 {
2298  int n;
2299  char hdr[VAR_CELL_MAX_HEADER_SIZE];
2300  tor_assert(cell);
2301  tor_assert(conn);
2302  n = var_cell_pack_header(cell, hdr, conn->wide_circ_ids);
2303  connection_buf_add(hdr, n, TO_CONN(conn));
2304  connection_buf_add((char*)cell->payload,
2305  cell->payload_len, TO_CONN(conn));
2306  if (conn->base_.state == OR_CONN_STATE_OR_HANDSHAKING_V3)
2308 
2310  /* Touch the channel's active timestamp if there is one */
2311  if (conn->chan)
2312  channel_timestamp_active(TLS_CHAN_TO_BASE(conn->chan));
2313 }
2314 
2315 /** See whether there's a variable-length cell waiting on <b>or_conn</b>'s
2316  * inbuf. Return values as for fetch_var_cell_from_buf(). */
2317 static int
2319 {
2320  connection_t *conn = TO_CONN(or_conn);
2321  return fetch_var_cell_from_buf(conn->inbuf, out, or_conn->link_proto);
2322 }
2323 
2324 /** Process cells from <b>conn</b>'s inbuf.
2325  *
2326  * Loop: while inbuf contains a cell, pull it off the inbuf, unpack it,
2327  * and hand it to command_process_cell().
2328  *
2329  * Always return 0.
2330  */
2331 static int
2333 {
2334  var_cell_t *var_cell;
2335 
2336  /*
2337  * Note on memory management for incoming cells: below the channel layer,
2338  * we shouldn't need to consider its internal queueing/copying logic. It
2339  * is safe to pass cells to it on the stack or on the heap, but in the
2340  * latter case we must be sure we free them later.
2341  *
2342  * The incoming cell queue code in channel.c will (in the common case)
2343  * decide it can pass them to the upper layer immediately, in which case
2344  * those functions may run directly on the cell pointers we pass here, or
2345  * it may decide to queue them, in which case it will allocate its own
2346  * buffer and copy the cell.
2347  */
2348 
2349  while (1) {
2350  log_debug(LD_OR,
2351  TOR_SOCKET_T_FORMAT": starting, inbuf_datalen %d "
2352  "(%d pending in tls object).",
2353  conn->base_.s,(int)connection_get_inbuf_len(TO_CONN(conn)),
2355  if (connection_fetch_var_cell_from_buf(conn, &var_cell)) {
2356  if (!var_cell)
2357  return 0; /* not yet. */
2358 
2359  /* Touch the channel's active timestamp if there is one */
2360  if (conn->chan)
2361  channel_timestamp_active(TLS_CHAN_TO_BASE(conn->chan));
2362 
2364  channel_tls_handle_var_cell(var_cell, conn);
2365  var_cell_free(var_cell);
2366  } else {
2367  const int wide_circ_ids = conn->wide_circ_ids;
2368  size_t cell_network_size = get_cell_network_size(conn->wide_circ_ids);
2369  char buf[CELL_MAX_NETWORK_SIZE];
2370  cell_t cell;
2371  if (connection_get_inbuf_len(TO_CONN(conn))
2372  < cell_network_size) /* whole response available? */
2373  return 0; /* not yet */
2374 
2375  /* Touch the channel's active timestamp if there is one */
2376  if (conn->chan)
2377  channel_timestamp_active(TLS_CHAN_TO_BASE(conn->chan));
2378 
2380  connection_buf_get_bytes(buf, cell_network_size, TO_CONN(conn));
2381 
2382  /* retrieve cell info from buf (create the host-order struct from the
2383  * network-order string) */
2384  cell_unpack(&cell, buf, wide_circ_ids);
2385 
2386  channel_tls_handle_cell(&cell, conn);
2387  }
2388  }
2389 }
2390 
2391 /** Array of recognized link protocol versions. */
2392 static const uint16_t or_protocol_versions[] = { 1, 2, 3, 4, 5 };
2393 /** Number of versions in <b>or_protocol_versions</b>. */
2394 static const int n_or_protocol_versions =
2395  (int)( sizeof(or_protocol_versions)/sizeof(uint16_t) );
2396 
2397 /** Return true iff <b>v</b> is a link protocol version that this Tor
2398  * implementation believes it can support. */
2399 int
2401 {
2402  int i;
2403  for (i = 0; i < n_or_protocol_versions; ++i) {
2404  if (or_protocol_versions[i] == v)
2405  return 1;
2406  }
2407  return 0;
2408 }
2409 
2410 /** Send a VERSIONS cell on <b>conn</b>, telling the other host about the
2411  * link protocol versions that this Tor can support.
2412  *
2413  * If <b>v3_plus</b>, this is part of a V3 protocol handshake, so only
2414  * allow protocol version v3 or later. If not <b>v3_plus</b>, this is
2415  * not part of a v3 protocol handshake, so don't allow protocol v3 or
2416  * later.
2417  **/
2418 int
2420 {
2421  var_cell_t *cell;
2422  int i;
2423  int n_versions = 0;
2424  const int min_version = v3_plus ? 3 : 0;
2425  const int max_version = v3_plus ? UINT16_MAX : 2;
2426  tor_assert(conn->handshake_state &&
2429  cell->command = CELL_VERSIONS;
2430  for (i = 0; i < n_or_protocol_versions; ++i) {
2431  uint16_t v = or_protocol_versions[i];
2432  if (v < min_version || v > max_version)
2433  continue;
2434  set_uint16(cell->payload+(2*n_versions), htons(v));
2435  ++n_versions;
2436  }
2437  cell->payload_len = n_versions * 2;
2438 
2440  conn->handshake_state->sent_versions_at = time(NULL);
2441 
2442  var_cell_free(cell);
2443  return 0;
2444 }
2445 
2446 static netinfo_addr_t *
2447 netinfo_addr_from_tor_addr(const tor_addr_t *tor_addr)
2448 {
2449  sa_family_t addr_family = tor_addr_family(tor_addr);
2450 
2451  if (BUG(addr_family != AF_INET && addr_family != AF_INET6))
2452  return NULL;
2453 
2454  netinfo_addr_t *netinfo_addr = netinfo_addr_new();
2455 
2456  if (addr_family == AF_INET) {
2457  netinfo_addr_set_addr_type(netinfo_addr, NETINFO_ADDR_TYPE_IPV4);
2458  netinfo_addr_set_len(netinfo_addr, 4);
2459  netinfo_addr_set_addr_ipv4(netinfo_addr, tor_addr_to_ipv4h(tor_addr));
2460  } else if (addr_family == AF_INET6) {
2461  netinfo_addr_set_addr_type(netinfo_addr, NETINFO_ADDR_TYPE_IPV6);
2462  netinfo_addr_set_len(netinfo_addr, 16);
2463  uint8_t *ipv6_buf = netinfo_addr_getarray_addr_ipv6(netinfo_addr);
2464  const uint8_t *in6_addr = tor_addr_to_in6_addr8(tor_addr);
2465  memcpy(ipv6_buf, in6_addr, 16);
2466  }
2467 
2468  return netinfo_addr;
2469 }
2470 
2471 /** Send a NETINFO cell on <b>conn</b>, telling the other server what we know
2472  * about their address, our address, and the current time. */
2473 MOCK_IMPL(int,
2475 {
2476  cell_t cell;
2477  time_t now = time(NULL);
2478  const routerinfo_t *me;
2479  int r = -1;
2480 
2481  tor_assert(conn->handshake_state);
2482 
2483  if (conn->handshake_state->sent_netinfo) {
2484  log_warn(LD_BUG, "Attempted to send an extra netinfo cell on a connection "
2485  "where we already sent one.");
2486  return 0;
2487  }
2488 
2489  memset(&cell, 0, sizeof(cell_t));
2490  cell.command = CELL_NETINFO;
2491 
2492  netinfo_cell_t *netinfo_cell = netinfo_cell_new();
2493 
2494  /* Timestamp, if we're a relay. */
2495  if (public_server_mode(get_options()) || ! conn->is_outgoing)
2496  netinfo_cell_set_timestamp(netinfo_cell, (uint32_t)now);
2497 
2498  /* Their address. */
2499  const tor_addr_t *remote_tor_addr = &TO_CONN(conn)->addr;
2500  /* We can safely use TO_CONN(conn)->addr here, since we no longer replace
2501  * it with a canonical address. */
2502  netinfo_addr_t *their_addr = netinfo_addr_from_tor_addr(remote_tor_addr);
2503 
2504  netinfo_cell_set_other_addr(netinfo_cell, their_addr);
2505 
2506  /* My address -- only include it if I'm a public relay, or if I'm a
2507  * bridge and this is an incoming connection. If I'm a bridge and this
2508  * is an outgoing connection, act like a normal client and omit it. */
2509  if ((public_server_mode(get_options()) || !conn->is_outgoing) &&
2510  (me = router_get_my_routerinfo())) {
2511  uint8_t n_my_addrs = 1 + !tor_addr_is_null(&me->ipv6_addr);
2512  netinfo_cell_set_n_my_addrs(netinfo_cell, n_my_addrs);
2513 
2514  netinfo_cell_add_my_addrs(netinfo_cell,
2515  netinfo_addr_from_tor_addr(&me->ipv4_addr));
2516 
2517  if (!tor_addr_is_null(&me->ipv6_addr)) {
2518  netinfo_cell_add_my_addrs(netinfo_cell,
2519  netinfo_addr_from_tor_addr(&me->ipv6_addr));
2520  }
2521  }
2522 
2523  const char *errmsg = NULL;
2524  if ((errmsg = netinfo_cell_check(netinfo_cell))) {
2525  log_warn(LD_OR, "Failed to validate NETINFO cell with error: %s",
2526  errmsg);
2527  goto cleanup;
2528  }
2529 
2530  if (netinfo_cell_encode(cell.payload, CELL_PAYLOAD_SIZE,
2531  netinfo_cell) < 0) {
2532  log_warn(LD_OR, "Failed generating NETINFO cell");
2533  goto cleanup;
2534  }
2535 
2536  conn->handshake_state->digest_sent_data = 0;
2537  conn->handshake_state->sent_netinfo = 1;
2538  connection_or_write_cell_to_buf(&cell, conn);
2539 
2540  r = 0;
2541  cleanup:
2542  netinfo_cell_free(netinfo_cell);
2543 
2544  return r;
2545 }
log_fn
#define log_fn(severity, domain, args,...)
Definition: log.h:287
connection_or_client_used
time_t connection_or_client_used(or_connection_t *conn)
Definition: connection_or.c:2034
CELL_MAX_NETWORK_SIZE
#define CELL_MAX_NETWORK_SIZE
Definition: or.h:580
DOWNCAST
#define DOWNCAST(to, ptr)
Definition: or.h:109
or_connection_t::bucket
token_bucket_rw_t bucket
Definition: or_connection_st.h:98
routermode.h
Header file for routermode.c.
CONST_TO_OR_CONN
const or_connection_t * CONST_TO_OR_CONN(const connection_t *c)
Definition: connection_or.c:120
tor_free
#define tor_free(p)
Definition: malloc.h:52
get_circuit_build_times_mutable
circuit_build_times_t * get_circuit_build_times_mutable(void)
Definition: circuitstats.c:86
routerinfo_t
Definition: routerinfo_st.h:20
connection_stop_writing
void connection_stop_writing(connection_t *conn)
Definition: mainloop.c:666
connection_or_send_netinfo
int connection_or_send_netinfo(or_connection_t *conn)
Definition: connection_or.c:2474
fetch_var_cell_from_buf
int fetch_var_cell_from_buf(buf_t *buf, var_cell_t **out, int linkproto)
Definition: proto_cell.c:57
channel_tls_handle_state_change_on_orconn
void channel_tls_handle_state_change_on_orconn(channel_tls_t *chan, or_connection_t *conn, uint8_t state)
Definition: channeltls.c:938
orconn_status_msg_t::status
int status
Definition: orconn_event.h:92
tor_free_
void tor_free_(void *mem)
Definition: malloc.c:227
connection_or_finished_connecting
int connection_or_finished_connecting(or_connection_t *or_conn)
Definition: connection_or.c:697
tor_addr_family
static sa_family_t tor_addr_family(const tor_addr_t *a)
Definition: address.h:187
hex_str
const char * hex_str(const char *from, size_t fromlen)
Definition: binascii.c:34
or_handshake_state_t::digest_sent
crypto_digest_t * digest_sent
Definition: or_handshake_state_st.h:72
connection_or_send_versions
int connection_or_send_versions(or_connection_t *conn, int v3_plus)
Definition: connection_or.c:2419
or_options_t::BandwidthRate
uint64_t BandwidthRate
Definition: or_options_st.h:393
OR_CONN_STATE_CONNECTING
#define OR_CONN_STATE_CONNECTING
Definition: orconn_event.h:31
or_handshake_state_free_
void or_handshake_state_free_(or_handshake_state_t *state)
Definition: connection_or.c:2139
or_connection_t::handshake_state
or_handshake_state_t * handshake_state
Definition: or_connection_st.h:93
memwipe
void memwipe(void *mem, uint8_t byte, size_t sz)
Definition: crypto_util.c:55
tor_tls_get_pending_bytes
int tor_tls_get_pending_bytes(tor_tls_t *tls)
Definition: tortls_nss.c:657
OR_CONN_STATE_TLS_SERVER_RENEGOTIATING
#define OR_CONN_STATE_TLS_SERVER_RENEGOTIATING
Definition: orconn_event.h:43
channel_timestamp_active
void channel_timestamp_active(channel_t *chan)
Definition: channel.c:3111
tor_addr_t
Definition: address.h:69
ED25519_BASE64_LEN
#define ED25519_BASE64_LEN
Definition: x25519_sizes.h:43
orconn_state_msg_t::gid
uint64_t gid
Definition: orconn_event.h:76
approx_time
time_t approx_time(void)
Definition: approx_time.c:32
connection_t::address
char * address
Definition: connection_st.h:166
MOCK_IMPL
#define MOCK_IMPL(rv, funcname, arglist)
Definition: testsupport.h:133
connection_or_check_valid_tls_handshake
static int connection_or_check_valid_tls_handshake(or_connection_t *conn, int started_here, char *digest_rcvd_out)
Definition: connection_or.c:1783
var_cell_pack_header
int var_cell_pack_header(const var_cell_t *cell, char *hdr_out, int wide_circ_ids)
Definition: connection_or.c:470
tor_tls_get_state_description
void tor_tls_get_state_description(tor_tls_t *tls, char *buf, size_t sz)
Definition: tortls_nss.c:346
channel_tls_handle_cell
void channel_tls_handle_cell(cell_t *cell, or_connection_t *conn)
Definition: channeltls.c:1041
connection_or_reached_eof
int connection_or_reached_eof(or_connection_t *conn)
Definition: connection_or.c:531
channel_mark_client
void channel_mark_client(channel_t *chan)
Definition: channel.c:2895
tor_assert
#define tor_assert(expr)
Definition: util_bug.h:102
routerkeys.h
Header for routerkeys.c.
microdesc.h
Header file for microdesc.c.
LD_BUG
#define LD_BUG
Definition: log.h:86
ed25519_pubkey_eq
int ed25519_pubkey_eq(const ed25519_public_key_t *key1, const ed25519_public_key_t *key2)
Definition: crypto_ed25519.c:642
router.h
Header file for router.c.
CONN_TYPE_EXT_OR
#define CONN_TYPE_EXT_OR
Definition: connection.h:71
connection_or_tls_renegotiated_cb
static void connection_or_tls_renegotiated_cb(tor_tls_t *tls, void *_conn)
Definition: connection_or.c:1669
command_setup_listener
void command_setup_listener(channel_listener_t *listener)
Definition: command.c:680
connection_or_state_publish
static void connection_or_state_publish(const or_connection_t *conn, uint8_t state)
Definition: connection_or.c:370
router_digest_is_me
int router_digest_is_me(const char *digest)
Definition: router.c:1698
channel.h
Header file for channel.c.
control_event_or_conn_status
int control_event_or_conn_status(or_connection_t *conn, or_conn_status_event_t tp, int reason)
Definition: control_events.c:922
or_handshake_state_t::started_here
unsigned int started_here
Definition: or_handshake_state_st.h:23
LD_GENERAL
#define LD_GENERAL
Definition: log.h:62
clear_broken_connection_map
void clear_broken_connection_map(int stop_recording)
Definition: connection_or.c:243
PADDING_TYPE_ENABLED_CELL
@ PADDING_TYPE_ENABLED_CELL
Definition: rephist.h:99
connection_or_clear_identity
void connection_or_clear_identity(or_connection_t *conn)
Definition: connection_or.c:128
CASE_TOR_TLS_ERROR_ANY
#define CASE_TOR_TLS_ERROR_ANY
Definition: tortls.h:62
base16_encode
void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:478
conn_state_to_string
const char * conn_state_to_string(int type, int state)
Definition: connection.c:297
tor_fragile_assert
#define tor_fragile_assert()
Definition: util_bug.h:259
channel_is_better
int channel_is_better(channel_t *a, channel_t *b)
Definition: channel.c:2337
connection_init_or_handshake_state
int connection_init_or_handshake_state(or_connection_t *conn, int started_here)
Definition: connection_or.c:2118
learned_router_identity
void learned_router_identity(const tor_addr_t *addr, uint16_t port, const char *digest, const ed25519_public_key_t *ed_id)
Definition: bridges.c:381
channel_when_last_client
time_t channel_when_last_client(channel_t *chan)
Definition: channel.c:3231
router_get_my_routerinfo
const routerinfo_t * router_get_my_routerinfo(void)
Definition: router.c:1760
set_uint8
static void set_uint8(void *cp, uint8_t v)
Definition: bytes.h:31
channel_tls_get_listener
channel_listener_t * channel_tls_get_listener(void)
Definition: channeltls.c:254
cell_unpack
static void cell_unpack(cell_t *dest, const char *src, int wide_circ_ids)
Definition: connection_or.c:454
OR_CONN_STATE_TLS_HANDSHAKING
#define OR_CONN_STATE_TLS_HANDSHAKING
Definition: orconn_event.h:36
reachability.h
Header file for reachability.c.
tor_reallocarray_
void * tor_reallocarray_(void *ptr, size_t sz1, size_t sz2)
Definition: malloc.c:146
or_connection_t::is_canonical
unsigned int is_canonical
Definition: or_connection_st.h:71
connection_or_client_learned_peer_id
int connection_or_client_learned_peer_id(or_connection_t *conn, const uint8_t *rsa_peer_id, const ed25519_public_key_t *ed_peer_id)
Definition: connection_or.c:1878
channel_close_from_lower_layer
void channel_close_from_lower_layer(channel_t *chan)
Definition: channel.c:1216
is_or_protocol_version_known
int is_or_protocol_version_known(uint16_t v)
Definition: connection_or.c:2400
node_get_by_id
const node_t * node_get_by_id(const char *identity_digest)
Definition: nodelist.c:223
smartlist_add
void smartlist_add(smartlist_t *sl, void *element)
Definition: smartlist_core.c:117
tor_addr_port_copy
void tor_addr_port_copy(tor_addr_port_t *dest, const tor_addr_port_t *source)
Definition: address.c:2121
router_digest_is_fallback_dir
int router_digest_is_fallback_dir(const char *digest)
Definition: dirlist.c:189
tor_tls_get_last_error_msg
const char * tor_tls_get_last_error_msg(const tor_tls_t *tls)
Definition: tortls_nss.c:397
channel_tls_update_marks
void channel_tls_update_marks(or_connection_t *conn)
Definition: channeltls.c:1332
tor_cert_dup
tor_cert_t * tor_cert_dup(const tor_cert_t *cert)
Definition: torcert.c:294
ed25519_public_key_is_zero
int ed25519_public_key_is_zero(const ed25519_public_key_t *pubkey)
Definition: crypto_ed25519.c:227
channel_mark_bad_for_new_circs
void channel_mark_bad_for_new_circs(channel_t *chan)
Definition: channel.c:2867
or_handshake_state_record_cell
void or_handshake_state_record_cell(or_connection_t *conn, or_handshake_state_t *state, const cell_t *cell, int incoming)
Definition: connection_or.c:2161
connection_or_is_bad_for_new_circs
static unsigned int connection_or_is_bad_for_new_circs(or_connection_t *or_conn)
Definition: connection_or.c:969
READ_EVENT
@ READ_EVENT
Definition: mainloop.h:37
tor_tls_peer_has_cert
int tor_tls_peer_has_cert(tor_tls_t *tls)
Definition: tortls_nss.c:532
connection_or_update_token_buckets_helper
static void connection_or_update_token_buckets_helper(or_connection_t *conn, int reset, const or_options_t *options)
Definition: connection_or.c:806
or_options_t::PerConnBWRate
uint64_t PerConnBWRate
Definition: or_options_st.h:403
var_cell_copy
var_cell_t * var_cell_copy(const var_cell_t *src)
Definition: connection_or.c:505
connection_or_finished_flushing
int connection_or_finished_flushing(or_connection_t *conn)
Definition: connection_or.c:657
tor_tls_used_v1_handshake
int tor_tls_used_v1_handshake(tor_tls_t *tls)
Definition: tortls_nss.c:720
connection_or_process_inbuf
int connection_or_process_inbuf(or_connection_t *conn)
Definition: connection_or.c:547
or_connection_t::is_pt
unsigned int is_pt
Definition: or_connection_st.h:85
crypto_digest_add_bytes
void crypto_digest_add_bytes(crypto_digest_t *digest, const char *data, size_t len)
Definition: crypto_digest_nss.c:308
connection_or_write_var_cell_to_buf
void connection_or_write_var_cell_to_buf(const var_cell_t *cell, or_connection_t *conn)
Definition: connection_or.c:2296
orconn_status_msg_t::reason
int reason
Definition: orconn_event.h:93
var_cell_free_
void var_cell_free_(var_cell_t *cell)
Definition: connection_or.c:524
smartlist_new
smartlist_t * smartlist_new(void)
Definition: smartlist_core.c:26
reasons.h
Header file for reasons.c.
or_connection_t::link_proto
uint16_t link_proto
Definition: or_connection_st.h:87
note_broken_connection
static void note_broken_connection(const char *state)
Definition: connection_or.c:223
crypto_digest_t
Definition: crypto_digest_nss.c:166
or_connection_t
Definition: or_connection_st.h:22
tor_snprintf
int tor_snprintf(char *str, size_t size, const char *format,...)
Definition: printf.c:27
routerinfo_t::ipv6_addr
tor_addr_t ipv6_addr
Definition: routerinfo_st.h:30
connection_t::port
uint16_t port
Definition: connection_st.h:146
connection_start_reading
void connection_start_reading(connection_t *conn)
Definition: mainloop.c:633
CELL_PAYLOAD_SIZE
#define CELL_PAYLOAD_SIZE
Definition: or.h:577
connection_proxy_connect
int connection_proxy_connect(connection_t *conn, int type)
Definition: connection.c:2721
ed25519_public_to_base64
void ed25519_public_to_base64(char *output, const ed25519_public_key_t *pkey)
Definition: crypto_format.c:227
SMARTLIST_FOREACH
#define SMARTLIST_FOREACH(sl, type, var, cmd)
Definition: smartlist_foreach.h:112
networkstatus.h
Header file for networkstatus.c.
rep_hist_note_negotiated_link_proto
void rep_hist_note_negotiated_link_proto(unsigned link_proto, int started_here)
Definition: rephist.c:1937
scheduler.h
Header file for scheduler*.c.
orconn_status_msg_t
Definition: orconn_event.h:90
connection_or_flushed_some
int connection_or_flushed_some(or_connection_t *conn)
Definition: connection_or.c:605
cell_t
Definition: cell_st.h:17
router_get_by_id_digest
const routerinfo_t * router_get_by_id_digest(const char *digest)
Definition: routerlist.c:775
node_ed25519_id_matches
int node_ed25519_id_matches(const node_t *node, const ed25519_public_key_t *id)
Definition: nodelist.c:1114
or_connection_t::chan
channel_tls_t * chan
Definition: or_connection_st.h:50
ed25519_fmt
const char * ed25519_fmt(const ed25519_public_key_t *pkey)
Definition: crypto_format.c:193
in6_addr
Definition: inaddr_st.h:51
dirlist.h
Header file for dirlist.c.
crypto_util.h
Common functions for cryptographic routines.
connection_or_clear_identity_map
void connection_or_clear_identity_map(void)
Definition: connection_or.c:136
LD_CIRC
#define LD_CIRC
Definition: log.h:82
buf_datalen
size_t buf_datalen(const buf_t *buf)
Definition: buffers.c:394
or_handshake_state_t::digest_sent_data
unsigned int digest_sent_data
Definition: or_handshake_state_st.h:56
connection_tls_continue_handshake
int connection_tls_continue_handshake(or_connection_t *conn)
Definition: connection_or.c:1690
circuitlist.h
Header file for circuitlist.c.
mainloop.h
Header file for mainloop.c.
tor_tls_t
Definition: tortls_st.h:41
DIGEST_LEN
#define DIGEST_LEN
Definition: digest_sizes.h:20
orconn_state_msg_t::state
uint8_t state
Definition: orconn_event.h:79
MAX_REASONS_TO_REPORT
#define MAX_REASONS_TO_REPORT
Definition: connection_or.c:306
OR_CONN_STATE_OPEN
#define OR_CONN_STATE_OPEN
Definition: orconn_event.h:53
connection_or_event_status
void connection_or_event_status(or_connection_t *conn, or_conn_status_event_t tp, int reason)
Definition: connection_or.c:351
errno_to_orconn_end_reason
int errno_to_orconn_end_reason(int e)
Definition: reasons.c:291
crypto_format.h
Header for crypto_format.c.
packed_cell_t
Definition: cell_queue_st.h:18
bridges.h
Header file for circuitbuild.c.
or_connection_new
or_connection_t * or_connection_new(int type, int socket_family)
Definition: connection.c:563
PADDING_TYPE_ENABLED_TOTAL
@ PADDING_TYPE_ENABLED_TOTAL
Definition: rephist.h:97
geoip.h
Header file for geoip.c.
command.h
Header file for command.c.
or_connection_t::idle_timeout
uint16_t idle_timeout
Definition: or_connection_st.h:89
connection_t::hold_open_until_flushed
unsigned int hold_open_until_flushed
Definition: connection_st.h:61
routerinfo_t::ipv4_addr
tor_addr_t ipv4_addr
Definition: routerinfo_st.h:25
connection_or_block_renegotiation
void connection_or_block_renegotiation(or_connection_t *conn)
Definition: connection_or.c:1657
tortls.h
Headers for tortls.c.
tor_addr_port_t
Definition: address.h:81
tor_memneq
#define tor_memneq(a, b, sz)
Definition: di_ops.h:21
tor_addr_to_ipv4h
static uint32_t tor_addr_to_ipv4h(const tor_addr_t *a)
Definition: address.h:160
entrynodes.h
Header file for circuitbuild.c.
TIME_BEFORE_OR_CONN_IS_TOO_OLD
#define TIME_BEFORE_OR_CONN_IS_TOO_OLD
Definition: connection_or.c:989
tor_assert_nonfatal_unreached
#define tor_assert_nonfatal_unreached()
Definition: util_bug.h:176
node_t
Definition: node_st.h:34
LD_OR
#define LD_OR
Definition: log.h:92
connection_describe_peer
const char * connection_describe_peer(const connection_t *conn)
Definition: connection.c:515
channelpadding_get_channel_idle_timeout
unsigned int channelpadding_get_channel_idle_timeout(const channel_t *chan, int is_canonical)
Definition: channelpadding.c:595
circuit_build_times_network_is_live
void circuit_build_times_network_is_live(circuit_build_times_t *cbt)
Definition: circuitstats.c:1418
torcert.h
Header for torcert.c.
CONN_TYPE_OR
#define CONN_TYPE_OR
Definition: connection.h:44
crypto_pk_get_digest
int crypto_pk_get_digest(const crypto_pk_t *pk, char *digest_out)
Definition: crypto_rsa.c:356
VAR_CELL_MAX_HEADER_SIZE
#define VAR_CELL_MAX_HEADER_SIZE
Definition: or.h:583
find_transport_name_by_bridge_addrport
const char * find_transport_name_by_bridge_addrport(const tor_addr_t *addr, uint16_t port)
Definition: bridges.c:575
tor_digest_is_zero
int tor_digest_is_zero(const char *digest)
Definition: util_string.c:96
ed25519_public_key_t
Definition: crypto_ed25519.h:23
broken_state_count_compare
static int broken_state_count_compare(const void **a_ptr, const void **b_ptr)
Definition: connection_or.c:293
networkstatus_get_reasonably_live_consensus
networkstatus_t * networkstatus_get_reasonably_live_consensus(time_t now, int flavor)
Definition: networkstatus.c:1477
or_handshake_state_t::sent_versions_at
time_t sent_versions_at
Definition: or_handshake_state_st.h:21
usable_consensus_flavor
int usable_consensus_flavor(void)
Definition: microdesc.c:1085
connection_t::inbuf
struct buf_t * inbuf
Definition: connection_st.h:101
orconn_state_msg_t::proxy_type
int proxy_type
Definition: orconn_event.h:78
OR_CONN_STATE_PROXY_HANDSHAKING
#define OR_CONN_STATE_PROXY_HANDSHAKING
Definition: orconn_event.h:33
authmode.h
Header file for directory authority mode.
get_connection_array
smartlist_t * get_connection_array(void)
Definition: mainloop.c:453
tor_tls_err_to_string
const char * tor_tls_err_to_string(int err)
Definition: tortls.c:155
or_connection_t::is_outgoing
unsigned int is_outgoing
Definition: or_connection_st.h:74
or_handshake_certs_t::started_here
int started_here
Definition: or_handshake_certs_st.h:21
connection_or_single_set_badness_
int connection_or_single_set_badness_(time_t now, or_connection_t *or_conn, int force)
Definition: connection_or.c:998
connection_or_nonopen_was_started_here
int connection_or_nonopen_was_started_here(or_connection_t *conn)
Definition: connection_or.c:1746
or_connection_t::tls_error
int tls_error
Definition: or_connection_st.h:45
connection_t::marked_for_close
uint16_t marked_for_close
Definition: connection_st.h:149
connection_or_num_cells_writeable
ssize_t connection_or_num_cells_writeable(or_connection_t *conn)
Definition: connection_or.c:627
connection_tls_start_handshake
int connection_tls_start_handshake(or_connection_t *conn, int receiving)
Definition: connection_or.c:1616
nodelist.h
Header file for nodelist.c.
broken_state_count_t
Definition: connection_or.c:286
connection_or_set_state_open
int connection_or_set_state_open(or_connection_t *conn)
Definition: connection_or.c:2234
routerlist.h
Header file for routerlist.c.
var_cell_t::payload_len
uint16_t payload_len
Definition: var_cell_st.h:22
relay_handshake.h
Header for feature/relay/relay_handshake.c.
or_handshake_certs_new
or_handshake_certs_t * or_handshake_certs_new(void)
Definition: torcert.c:471
router_get_consensus_status_by_id
const routerstatus_t * router_get_consensus_status_by_id(const char *digest)
Definition: networkstatus.c:843
assert_connection_ok
void assert_connection_ok(connection_t *conn, time_t now)
Definition: connection.c:5537
channel_listener_queue_incoming
void channel_listener_queue_incoming(channel_listener_t *listener, channel_t *incoming)
Definition: channel.c:1925
connection_t::addr
tor_addr_t addr
Definition: connection_st.h:145
routerinfo_st.h
Router descriptor structure.
circuitstats.h
Header file for circuitstats.c.
token_bucket_rw_reset
void token_bucket_rw_reset(token_bucket_rw_t *bucket, uint32_t now_ts)
Definition: token_bucket.c:167
connection_start_writing
void connection_start_writing(connection_t *conn)
Definition: mainloop.c:689
connection_or_change_state
STATIC void connection_or_change_state(or_connection_t *conn, uint8_t state)
Definition: connection_or.c:398
buffers.h
Header file for buffers.c.
connection_or_notify_error
void connection_or_notify_error(or_connection_t *conn, int reason, const char *msg)
Definition: connection_or.c:1383
cell_t::command
uint8_t command
Definition: cell_st.h:19
cell_t::payload
uint8_t payload[CELL_PAYLOAD_SIZE]
Definition: cell_st.h:21
channel_t
Definition: channel.h:181
PADDING_TYPE_TOTAL
@ PADDING_TYPE_TOTAL
Definition: rephist.h:95
HT_HEAD
typedef HT_HEAD(hs_service_ht, hs_service_t) hs_service_ht
tor_tls_is_server
int tor_tls_is_server(tor_tls_t *tls)
Definition: tortls.c:379
OR_CONN_STATE_OR_HANDSHAKING_V2
#define OR_CONN_STATE_OR_HANDSHAKING_V2
Definition: orconn_event.h:47
connection_tls_finish_handshake
static int connection_tls_finish_handshake(or_connection_t *conn)
Definition: connection_or.c:2057
channel_tls_start_listener
channel_listener_t * channel_tls_start_listener(void)
Definition: channeltls.c:266
channel_t::ed25519_identity
struct ed25519_public_key_t ed25519_identity
Definition: channel.h:388
crypto_digest256_new
crypto_digest_t * crypto_digest256_new(digest_algorithm_t algorithm)
Definition: crypto_digest_nss.c:272
connection_t
Definition: connection_st.h:45
connection_t::type
unsigned int type
Definition: connection_st.h:50
rep_hist_padding_count_write
void rep_hist_padding_count_write(padding_type_t type)
Definition: rephist.c:1967
connection_or_launch_v3_or_handshake
static int connection_or_launch_v3_or_handshake(or_connection_t *conn)
Definition: connection_or.c:2102
or_handshake_certs_st.h
OR handshake certs structure.
packed_cell_t::body
char body[CELL_MAX_NETWORK_SIZE]
Definition: cell_queue_st.h:21
ext_orport.h
Header for ext_orport.c.
LOG_INFO
#define LOG_INFO
Definition: log.h:45
dirserv_orconn_tls_done
void dirserv_orconn_tls_done(const tor_addr_t *addr, uint16_t or_port, const char *digest_rcvd, const ed25519_public_key_t *ed_id_rcvd)
Definition: reachability.c:40
fmt_addr
#define fmt_addr(a)
Definition: address.h:239
connection_free_
void connection_free_(connection_t *conn)
Definition: connection.c:950
node_get_pref_ipv6_orport
void node_get_pref_ipv6_orport(const node_t *node, tor_addr_port_t *ap_out)
Definition: nodelist.c:1776
connection_fetch_var_cell_from_buf
static int connection_fetch_var_cell_from_buf(or_connection_t *or_conn, var_cell_t **out)
Definition: connection_or.c:2318
control_events.h
Header file for control_events.c.
get_options
const or_options_t * get_options(void)
Definition: config.c:928
bool_eq
#define bool_eq(a, b)
Definition: logic.h:16
or_state_st.h
The or_state_t structure, which represents Tor's state file.
n_or_protocol_versions
static const int n_or_protocol_versions
Definition: connection_or.c:2394
or_options_t::PerConnBWBurst
uint64_t PerConnBWBurst
Definition: or_options_st.h:404
var_cell_st.h
Variable-length cell structure.
circuitbuild.h
Header file for circuitbuild.c.
connection_watch_events
void connection_watch_events(connection_t *conn, watchable_events_t events)
Definition: mainloop.c:495
tor_tls_set_logged_address
void tor_tls_set_logged_address(tor_tls_t *tls, const char *address)
Definition: tortls.c:369
connection_or_note_state_when_broken
static void connection_or_note_state_when_broken(or_connection_t *orconn)
Definition: connection_or.c:275
get_uint32
static uint32_t get_uint32(const void *cp)
Definition: bytes.h:54
OR_CONN_STATE_OR_HANDSHAKING_V3
#define OR_CONN_STATE_OR_HANDSHAKING_V3
Definition: orconn_event.h:51
crypto_digest_free
#define crypto_digest_free(d)
Definition: crypto_digest.h:130
connection_or_close_for_error
void connection_or_close_for_error(or_connection_t *orconn, int flush)
Definition: connection_or.c:1591
scheduler_channel_wants_writes
void scheduler_channel_wants_writes(channel_t *chan)
Definition: scheduler.c:673
connection_connect
int connection_connect(connection_t *conn, const char *address, const tor_addr_t *addr, uint16_t port, int *socket_error)
Definition: connection.c:2367
get_proxy_addrport
int get_proxy_addrport(tor_addr_t *addr, uint16_t *port, int *proxy_type, int *is_pt_out, const connection_t *conn)
Definition: connection.c:5679
connection_or_process_cells_from_inbuf
static int connection_or_process_cells_from_inbuf(or_connection_t *conn)
Definition: connection_or.c:2332
channel_clear_identity_digest
void channel_clear_identity_digest(channel_t *chan)
Definition: channel.c:1302
set_uint32
static void set_uint32(void *cp, uint32_t v)
Definition: bytes.h:87
or_connection_t::proxy_type
unsigned int proxy_type
Definition: or_connection_st.h:75
channel_tls_handle_incoming
channel_t * channel_tls_handle_incoming(or_connection_t *orconn)
Definition: channeltls.c:329
or_connect_failure_entry_t
Definition: connection_or.c:1170
OR_CONN_HIGHWATER
#define OR_CONN_HIGHWATER
Definition: or.h:721
connection_describe
const char * connection_describe(const connection_t *conn)
Definition: connection.c:530
connection_or_check_canonicity
static void connection_or_check_canonicity(or_connection_t *conn, int started_here)
Definition: connection_or.c:908
relay.h
Header file for relay.c.
channel_is_bad_for_new_circs
int channel_is_bad_for_new_circs(channel_t *chan)
Definition: channel.c:2854
connection.h
Header file for connection.c.
connection_buf_get_bytes
int connection_buf_get_bytes(char *string, size_t len, connection_t *conn)
Definition: connection.c:4177
channeltls.h
Header file for channeltls.c.
connection_read_proxy_handshake
int connection_read_proxy_handshake(connection_t *conn)
Definition: connection.c:2874
connection_or_write_cell_to_buf
void connection_or_write_cell_to_buf(const cell_t *cell, or_connection_t *conn)
Definition: connection_or.c:2257
connection_or_report_broken_states
void connection_or_report_broken_states(int severity, int domain)
Definition: connection_or.c:311
node_supports_ed25519_link_authentication
bool node_supports_ed25519_link_authentication(const node_t *node, bool compatible_with_us)
Definition: nodelist.c:1154
connection_or_about_to_close
void connection_or_about_to_close(or_connection_t *or_conn)
Definition: connection_or.c:733
monotime_coarse_get_stamp
uint32_t monotime_coarse_get_stamp(void)
Definition: compat_time.c:844
OR_CONN_LOWWATER
#define OR_CONN_LOWWATER
Definition: or.h:725
or_handshake_state_t
Definition: or_handshake_state_st.h:18
server_mode
int server_mode(const or_options_t *options)
Definition: routermode.c:34
LD_HANDSHAKE
#define LD_HANDSHAKE
Definition: log.h:101
connection_or_init_conn_from_address
void connection_or_init_conn_from_address(or_connection_t *conn, const tor_addr_t *addr, uint16_t port, const char *id_digest, const ed25519_public_key_t *ed_id, int started_here)
Definition: connection_or.c:879
tor_addr_is_null
int tor_addr_is_null(const tor_addr_t *addr)
Definition: address.c:780
or_handshake_state_t::certs
or_handshake_certs_t * certs
Definition: or_handshake_state_st.h:79
connection_or_update_token_buckets
void connection_or_update_token_buckets(smartlist_t *conns, const or_options_t *options)
Definition: connection_or.c:838
var_cell_t::circ_id
circid_t circ_id
Definition: var_cell_st.h:20
channel_listener_t
Definition: channel.h:461
or_connection_st.h
OR connection structure.
or_connection_t::canonical_orport
tor_addr_port_t canonical_orport
Definition: or_connection_st.h:64
tor_tls_new
tor_tls_t * tor_tls_new(tor_socket_t sock, int is_server)
Definition: tortls_nss.c:409
token_bucket_rw_adjust
void token_bucket_rw_adjust(token_bucket_rw_t *bucket, uint32_t rate, uint32_t burst)
Definition: token_bucket.c:152
SMARTLIST_FOREACH_BEGIN
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
Definition: smartlist_foreach.h:78
node_get_nickname
const char * node_get_nickname(const node_t *node)
Definition: nodelist.c:1369
crypto_pk_t
Definition: crypto_rsa_nss.c:37
node_get_prim_orport
void node_get_prim_orport(const node_t *node, tor_addr_port_t *ap_out)
Definition: nodelist.c:1740
public_server_mode
int public_server_mode(const or_options_t *options)
Definition: routermode.c:43
HEX_DIGEST_LEN
#define HEX_DIGEST_LEN
Definition: crypto_digest.h:35
LOG_WARN
#define LOG_WARN
Definition: log.h:53
connection_or_get_state_description
static void connection_or_get_state_description(or_connection_t *orconn, char *buf, size_t buflen)
Definition: connection_or.c:257
orconn_event.h
Header file for orconn_event.c.
or_handshake_state_t::own_link_cert
struct tor_cert_st * own_link_cert
Definition: or_handshake_state_st.h:45
or_conn_status_event_t
or_conn_status_event_t
Definition: orconn_event.h:59
tls_error_to_orconn_end_reason
int tls_error_to_orconn_end_reason(int e)
Definition: reasons.c:263
tor_tls_verify
int tor_tls_verify(int severity, tor_tls_t *tls, crypto_pk_t **identity)
Definition: tortls.c:416
get_uint8
static uint8_t get_uint8(const void *cp)
Definition: bytes.h:23
cell_st.h
Fixed-size cell structure.
channel_tls_handle_var_cell
void channel_tls_handle_var_cell(var_cell_t *var_cell, or_connection_t *conn)
Definition: channeltls.c:1157
var_cell_new
var_cell_t * var_cell_new(uint16_t payload_len)
Definition: connection_or.c:490
connection_t::magic
uint32_t magic
Definition: connection_st.h:46
connection_or_digest_is_known_relay
int connection_or_digest_is_known_relay(const char *id_digest)
Definition: connection_or.c:786
cell_pack
void cell_pack(packed_cell_t *dst, const cell_t *src, int wide_circ_ids)
Definition: connection_or.c:433
HT_PROTOTYPE
HT_PROTOTYPE(hs_circuitmap_ht, circuit_t, hs_circuitmap_node, hs_circuit_hash_token, hs_circuits_have_same_token)
router_ed25519_id_is_me
int router_ed25519_id_is_me(const ed25519_public_key_t *id)
Definition: routerkeys.c:633
tor_tls_handshake
int tor_tls_handshake(tor_tls_t *tls)
Definition: tortls_nss.c:609
config.h
Header file for config.c.
orconn_state_msg_t
Definition: orconn_event.h:75
connection_t::s
tor_socket_t s
Definition: connection_st.h:96
get_uint16
static uint16_t get_uint16(const void *cp)
Definition: bytes.h:42
authdir_mode_tests_reachability
int authdir_mode_tests_reachability(const or_options_t *options)
Definition: authmode.c:68
or_handshake_state_record_var_cell
void or_handshake_state_record_var_cell(or_connection_t *conn, or_handshake_state_t *state, const var_cell_t *cell, int incoming)
Definition: connection_or.c:2202
connection_t::timestamp_created
time_t timestamp_created
Definition: connection_st.h:109
control_event_bootstrap_prob_or
void control_event_bootstrap_prob_or(const char *warn, int reason, or_connection_t *or_conn)
Definition: control_bootstrap.c:347
LD_NET
#define LD_NET
Definition: log.h:66
tor_tls_block_renegotiation
void tor_tls_block_renegotiation(tor_tls_t *tls)
Definition: tortls_nss.c:643
channel_set_circid_type
void channel_set_circid_type(channel_t *chan, crypto_pk_t *identity_rcvd, int consider_identity)
Definition: channel.c:3322
tor_log
void tor_log(int severity, log_domain_mask_t domain, const char *format,...)
Definition: log.c:628
connection_t::state
uint8_t state
Definition: connection_st.h:49
sa_family_t
uint16_t sa_family_t
Definition: inaddr_st.h:77
or_options_t
Definition: or_options_st.h:45
orconn_state_msg_t::chan
uint64_t chan
Definition: orconn_event.h:77
connection_or_close_normally
void connection_or_close_normally(or_connection_t *orconn, int flush)
Definition: connection_or.c:1570
TO_CONN
#define TO_CONN(c)
Definition: or.h:736
set_uint16
static void set_uint16(void *cp, uint16_t v)
Definition: bytes.h:78
var_cell_t::payload
uint8_t payload[FLEXIBLE_ARRAY_MEMBER]
Definition: var_cell_st.h:24
or_handshake_state_st.h
OR handshake state structure.
STATIC
#define STATIC
Definition: testsupport.h:32
cell_queue_st.h
Cell queue structures.
tor_tls_set_renegotiate_callback
void tor_tls_set_renegotiate_callback(tor_tls_t *tls, void(*cb)(tor_tls_t *, void *arg), void *arg)
Definition: tortls_nss.c:468
orconn_end_reason_to_control_string
const char * orconn_end_reason_to_control_string(int r)
Definition: reasons.c:225
var_cell_t::command
uint8_t command
Definition: var_cell_st.h:18
channel_num_circuits
unsigned int channel_num_circuits(channel_t *chan)
Definition: channel.c:3305
entry_guard_chan_failed
void entry_guard_chan_failed(channel_t *chan)
Definition: entrynodes.c:2554
broken_connection_counts
static strmap_t * broken_connection_counts
Definition: connection_or.c:216
networkstatus_get_param
int32_t networkstatus_get_param(const networkstatus_t *ns, const char *param_name, int32_t default_val, int32_t min_val, int32_t max_val)
Definition: networkstatus.c:2495
or_options_t::BandwidthBurst
uint64_t BandwidthBurst
Definition: or_options_st.h:395
connection_or_connect
or_connection_t * connection_or_connect(const tor_addr_t *_addr, uint16_t port, const char *id_digest, const ed25519_public_key_t *ed_id, channel_tls_t *chan)
Definition: connection_or.c:1426
var_cell_t
Definition: var_cell_st.h:16
connection_or_group_set_badness_
void connection_or_group_set_badness_(smartlist_t *group, int force)
Definition: connection_or.c:1040
WRITE_EVENT
@ WRITE_EVENT
Definition: mainloop.h:38
channel_tls_to_base
channel_t * channel_tls_to_base(channel_tls_t *tlschan)
Definition: channeltls.c:371
proto_cell.h
Header for proto_cell.c.
smartlist_sort
void smartlist_sort(smartlist_t *sl, int(*compare)(const void **a, const void **b))
Definition: smartlist.c:334
cell_t::circ_id
circid_t circ_id
Definition: cell_st.h:18
or_connection_t::tls
struct tor_tls_t * tls
Definition: or_connection_st.h:44
orconn_status_msg_t::gid
uint64_t gid
Definition: orconn_event.h:91
tor_addr_to_in6_addr8
#define tor_addr_to_in6_addr8(x)
Definition: address.h:135
or_protocol_versions
static const uint16_t or_protocol_versions[]
Definition: connection_or.c:2392
PADDING_TYPE_CELL
@ PADDING_TYPE_CELL
Definition: rephist.h:93
or_connection_t::identity_digest
char identity_digest[DIGEST_LEN]
Definition: or_connection_st.h:27
smartlist_t
Definition: smartlist_core.h:26
TO_OR_CONN
or_connection_t * TO_OR_CONN(connection_t *c)
Definition: connection_or.c:108
fast_memeq
#define fast_memeq(a, b, c)
Definition: di_ops.h:35
channel_set_identity_digest
void channel_set_identity_digest(channel_t *chan, const char *identity_digest, const ed25519_public_key_t *ed_identity)
Definition: channel.c:1331
tor_addr_eq
#define tor_addr_eq(a, b)
Definition: address.h:280
connection_t::proxy_state
unsigned int proxy_state
Definition: connection_st.h:92
rendcommon.h
Header file for rendcommon.c.
rephist.h
Header file for rephist.c.
connection_or_set_identity_digest
static void connection_or_set_identity_digest(or_connection_t *conn, const char *rsa_digest, const ed25519_public_key_t *ed_id)
Definition: connection_or.c:157
connection_or.h
Header file for connection_or.c.
disable_broken_connection_counts
static int disable_broken_connection_counts
Definition: connection_or.c:219
tor_addr_copy
void tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src)
Definition: address.c:933
channel_close_for_error
void channel_close_for_error(channel_t *chan)
Definition: channel.c:1244
or_connection_t::nickname
char * nickname
Definition: or_connection_st.h:42
or.h
Master header file for Tor-specific functionality.
connection_t::global_identifier
uint64_t global_identifier
Definition: connection_st.h:171
channel_closed
void channel_closed(channel_t *chan)
Definition: channel.c:1271
connection_or_get_num_circuits
int connection_or_get_num_circuits(or_connection_t *conn)
Definition: connection_or.c:414
connection_or_connect_failed
void connection_or_connect_failed(or_connection_t *conn, int reason, const char *msg)
Definition: connection_or.c:1367
LD_PROTOCOL
#define LD_PROTOCOL
Definition: log.h:72