Update copyrights to 2021, using "make update-copyright"
[tor.git] / src / core / or / relay.c
bloba6c2a3fa633f3bb52a299466ec9a8b28eadffd14
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-2021, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
7 /**
8 * \file relay.c
9 * \brief Handle relay cell encryption/decryption, plus packaging and
10 * receiving from circuits, plus queuing on circuits.
12 * This is a core modules that makes Tor work. It's responsible for
13 * dealing with RELAY cells (the ones that travel more than one hop along a
14 * circuit), by:
15 * <ul>
16 * <li>constructing relays cells,
17 * <li>encrypting relay cells,
18 * <li>decrypting relay cells,
19 * <li>demultiplexing relay cells as they arrive on a connection,
20 * <li>queueing relay cells for retransmission,
21 * <li>or handling relay cells that are for us to receive (as an exit or a
22 * client).
23 * </ul>
25 * RELAY cells are generated throughout the code at the client or relay side,
26 * using relay_send_command_from_edge() or one of the functions like
27 * connection_edge_send_command() that calls it. Of particular interest is
28 * connection_edge_package_raw_inbuf(), which takes information that has
29 * arrived on an edge connection socket, and packages it as a RELAY_DATA cell
30 * -- this is how information is actually sent across the Tor network. The
31 * cryptography for these functions is handled deep in
32 * circuit_package_relay_cell(), which either adds a single layer of
33 * encryption (if we're an exit), or multiple layers (if we're the origin of
34 * the circuit). After construction and encryption, the RELAY cells are
35 * passed to append_cell_to_circuit_queue(), which queues them for
36 * transmission and tells the circuitmux (see circuitmux.c) that the circuit
37 * is waiting to send something.
39 * Incoming RELAY cells arrive at circuit_receive_relay_cell(), called from
40 * command.c. There they are decrypted and, if they are for us, are passed to
41 * connection_edge_process_relay_cell(). If they're not for us, they're
42 * re-queued for retransmission again with append_cell_to_circuit_queue().
44 * The connection_edge_process_relay_cell() function handles all the different
45 * types of relay cells, launching requests or transmitting data as needed.
46 **/
48 #define RELAY_PRIVATE
49 #include "core/or/or.h"
50 #include "feature/client/addressmap.h"
51 #include "lib/err/backtrace.h"
52 #include "lib/buf/buffers.h"
53 #include "core/or/channel.h"
54 #include "feature/client/circpathbias.h"
55 #include "core/or/circuitbuild.h"
56 #include "core/or/circuitlist.h"
57 #include "core/or/circuituse.h"
58 #include "core/or/circuitpadding.h"
59 #include "core/or/extendinfo.h"
60 #include "lib/compress/compress.h"
61 #include "app/config/config.h"
62 #include "core/mainloop/connection.h"
63 #include "core/or/connection_edge.h"
64 #include "core/or/connection_or.h"
65 #include "feature/control/control_events.h"
66 #include "lib/crypt_ops/crypto_rand.h"
67 #include "lib/crypt_ops/crypto_util.h"
68 #include "feature/dircommon/directory.h"
69 #include "feature/relay/dns.h"
70 #include "feature/relay/circuitbuild_relay.h"
71 #include "feature/stats/geoip_stats.h"
72 #include "feature/hs/hs_cache.h"
73 #include "core/mainloop/mainloop.h"
74 #include "feature/nodelist/networkstatus.h"
75 #include "feature/nodelist/nodelist.h"
76 #include "core/or/onion.h"
77 #include "core/or/policies.h"
78 #include "core/or/reasons.h"
79 #include "core/or/relay.h"
80 #include "core/crypto/relay_crypto.h"
81 #include "feature/rend/rendcommon.h"
82 #include "feature/nodelist/describe.h"
83 #include "feature/nodelist/routerlist.h"
84 #include "core/or/scheduler.h"
85 #include "feature/hs/hs_metrics.h"
87 #include "core/or/cell_st.h"
88 #include "core/or/cell_queue_st.h"
89 #include "core/or/cpath_build_state_st.h"
90 #include "feature/dircommon/dir_connection_st.h"
91 #include "core/or/destroy_cell_queue_st.h"
92 #include "core/or/entry_connection_st.h"
93 #include "core/or/extend_info_st.h"
94 #include "core/or/or_circuit_st.h"
95 #include "core/or/origin_circuit_st.h"
96 #include "feature/nodelist/routerinfo_st.h"
97 #include "core/or/socks_request_st.h"
98 #include "core/or/sendme.h"
100 static edge_connection_t *relay_lookup_conn(circuit_t *circ, cell_t *cell,
101 cell_direction_t cell_direction,
102 crypt_path_t *layer_hint);
104 static void circuit_resume_edge_reading(circuit_t *circ,
105 crypt_path_t *layer_hint);
106 static int circuit_resume_edge_reading_helper(edge_connection_t *conn,
107 circuit_t *circ,
108 crypt_path_t *layer_hint);
109 static int circuit_consider_stop_edge_reading(circuit_t *circ,
110 crypt_path_t *layer_hint);
111 static int circuit_queue_streams_are_blocked(circuit_t *circ);
112 static void adjust_exit_policy_from_exitpolicy_failure(origin_circuit_t *circ,
113 entry_connection_t *conn,
114 node_t *node,
115 const tor_addr_t *addr);
117 /** Stop reading on edge connections when we have this many cells
118 * waiting on the appropriate queue. */
119 #define CELL_QUEUE_HIGHWATER_SIZE 256
120 /** Start reading from edge connections again when we get down to this many
121 * cells. */
122 #define CELL_QUEUE_LOWWATER_SIZE 64
124 /** Stats: how many relay cells have originated at this hop, or have
125 * been relayed onward (not recognized at this hop)?
127 uint64_t stats_n_relay_cells_relayed = 0;
128 /** Stats: how many relay cells have been delivered to streams at this
129 * hop?
131 uint64_t stats_n_relay_cells_delivered = 0;
132 /** Stats: how many circuits have we closed due to the cell queue limit being
133 * reached (see append_cell_to_circuit_queue()) */
134 uint64_t stats_n_circ_max_cell_reached = 0;
137 * Update channel usage state based on the type of relay cell and
138 * circuit properties.
140 * This is needed to determine if a client channel is being
141 * used for application traffic, and if a relay channel is being
142 * used for multihop circuits and application traffic. The decision
143 * to pad in channelpadding.c depends upon this info (as well as
144 * consensus parameters) to decide what channels to pad.
146 static void
147 circuit_update_channel_usage(circuit_t *circ, cell_t *cell)
149 if (CIRCUIT_IS_ORIGIN(circ)) {
151 * The client state was first set much earlier in
152 * circuit_send_next_onion_skin(), so we can start padding as early as
153 * possible.
155 * However, if padding turns out to be expensive, we may want to not do
156 * it until actual application traffic starts flowing (which is controlled
157 * via consensus param nf_pad_before_usage).
159 * So: If we're an origin circuit and we've created a full length circuit,
160 * then any CELL_RELAY cell means application data. Increase the usage
161 * state of the channel to indicate this.
163 * We want to wait for CELL_RELAY specifically here, so we know that
164 * the channel was definitely being used for data and not for extends.
165 * By default, we pad as soon as a channel has been used for *any*
166 * circuits, so this state is irrelevant to the padding decision in
167 * the default case. However, if padding turns out to be expensive,
168 * we would like the ability to avoid padding until we're absolutely
169 * sure that a channel is used for enough application data to be worth
170 * padding.
172 * (So it does not matter that CELL_RELAY_EARLY can actually contain
173 * application data. This is only a load reducing option and that edge
174 * case does not matter if we're desperately trying to reduce overhead
175 * anyway. See also consensus parameter nf_pad_before_usage).
177 if (BUG(!circ->n_chan))
178 return;
180 if (circ->n_chan->channel_usage == CHANNEL_USED_FOR_FULL_CIRCS &&
181 cell->command == CELL_RELAY) {
182 circ->n_chan->channel_usage = CHANNEL_USED_FOR_USER_TRAFFIC;
184 } else {
185 /* If we're a relay circuit, the question is more complicated. Basically:
186 * we only want to pad connections that carry multihop (anonymous)
187 * circuits.
189 * We assume we're more than one hop if either the previous hop
190 * is not a client, or if the previous hop is a client and there's
191 * a next hop. Then, circuit traffic starts at RELAY_EARLY, and
192 * user application traffic starts when we see RELAY cells.
194 or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
196 if (BUG(!or_circ->p_chan))
197 return;
199 if (!channel_is_client(or_circ->p_chan) ||
200 (channel_is_client(or_circ->p_chan) && circ->n_chan)) {
201 if (cell->command == CELL_RELAY_EARLY) {
202 if (or_circ->p_chan->channel_usage < CHANNEL_USED_FOR_FULL_CIRCS) {
203 or_circ->p_chan->channel_usage = CHANNEL_USED_FOR_FULL_CIRCS;
205 } else if (cell->command == CELL_RELAY) {
206 or_circ->p_chan->channel_usage = CHANNEL_USED_FOR_USER_TRAFFIC;
212 /** Receive a relay cell:
213 * - Crypt it (encrypt if headed toward the origin or if we <b>are</b> the
214 * origin; decrypt if we're headed toward the exit).
215 * - Check if recognized (if exitward).
216 * - If recognized and the digest checks out, then find if there's a stream
217 * that the cell is intended for, and deliver it to the right
218 * connection_edge.
219 * - If not recognized, then we need to relay it: append it to the appropriate
220 * cell_queue on <b>circ</b>.
222 * Return -<b>reason</b> on failure.
225 circuit_receive_relay_cell(cell_t *cell, circuit_t *circ,
226 cell_direction_t cell_direction)
228 channel_t *chan = NULL;
229 crypt_path_t *layer_hint=NULL;
230 char recognized=0;
231 int reason;
233 tor_assert(cell);
234 tor_assert(circ);
235 tor_assert(cell_direction == CELL_DIRECTION_OUT ||
236 cell_direction == CELL_DIRECTION_IN);
237 if (circ->marked_for_close)
238 return 0;
240 if (relay_decrypt_cell(circ, cell, cell_direction, &layer_hint, &recognized)
241 < 0) {
242 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
243 "relay crypt failed. Dropping connection.");
244 return -END_CIRC_REASON_INTERNAL;
247 circuit_update_channel_usage(circ, cell);
249 if (recognized) {
250 edge_connection_t *conn = NULL;
252 /* Recognized cell, the cell digest has been updated, we'll record it for
253 * the SENDME if need be. */
254 sendme_record_received_cell_digest(circ, layer_hint);
256 if (circ->purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING) {
257 if (pathbias_check_probe_response(circ, cell) == -1) {
258 pathbias_count_valid_cells(circ, cell);
261 /* We need to drop this cell no matter what to avoid code that expects
262 * a certain purpose (such as the hidserv code). */
263 return 0;
266 conn = relay_lookup_conn(circ, cell, cell_direction, layer_hint);
267 if (cell_direction == CELL_DIRECTION_OUT) {
268 ++stats_n_relay_cells_delivered;
269 log_debug(LD_OR,"Sending away from origin.");
270 reason = connection_edge_process_relay_cell(cell, circ, conn, NULL);
271 if (reason < 0) {
272 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
273 "connection_edge_process_relay_cell (away from origin) "
274 "failed.");
275 return reason;
278 if (cell_direction == CELL_DIRECTION_IN) {
279 ++stats_n_relay_cells_delivered;
280 log_debug(LD_OR,"Sending to origin.");
281 reason = connection_edge_process_relay_cell(cell, circ, conn,
282 layer_hint);
283 if (reason < 0) {
284 /* If a client is trying to connect to unknown hidden service port,
285 * END_CIRC_AT_ORIGIN is sent back so we can then close the circuit.
286 * Do not log warn as this is an expected behavior for a service. */
287 if (reason != END_CIRC_AT_ORIGIN) {
288 log_warn(LD_OR,
289 "connection_edge_process_relay_cell (at origin) failed.");
291 return reason;
294 return 0;
297 /* not recognized. inform circpad and pass it on. */
298 circpad_deliver_unrecognized_cell_events(circ, cell_direction);
300 if (cell_direction == CELL_DIRECTION_OUT) {
301 cell->circ_id = circ->n_circ_id; /* switch it */
302 chan = circ->n_chan;
303 } else if (! CIRCUIT_IS_ORIGIN(circ)) {
304 cell->circ_id = TO_OR_CIRCUIT(circ)->p_circ_id; /* switch it */
305 chan = TO_OR_CIRCUIT(circ)->p_chan;
306 } else {
307 log_fn(LOG_PROTOCOL_WARN, LD_OR,
308 "Dropping unrecognized inbound cell on origin circuit.");
309 /* If we see unrecognized cells on path bias testing circs,
310 * it's bad mojo. Those circuits need to die.
311 * XXX: Shouldn't they always die? */
312 if (circ->purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING) {
313 TO_ORIGIN_CIRCUIT(circ)->path_state = PATH_STATE_USE_FAILED;
314 return -END_CIRC_REASON_TORPROTOCOL;
315 } else {
316 return 0;
320 if (!chan) {
321 // XXXX Can this splice stuff be done more cleanly?
322 if (! CIRCUIT_IS_ORIGIN(circ) &&
323 TO_OR_CIRCUIT(circ)->rend_splice &&
324 cell_direction == CELL_DIRECTION_OUT) {
325 or_circuit_t *splice_ = TO_OR_CIRCUIT(circ)->rend_splice;
326 tor_assert(circ->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED);
327 tor_assert(splice_->base_.purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED);
328 cell->circ_id = splice_->p_circ_id;
329 cell->command = CELL_RELAY; /* can't be relay_early anyway */
330 if ((reason = circuit_receive_relay_cell(cell, TO_CIRCUIT(splice_),
331 CELL_DIRECTION_IN)) < 0) {
332 log_warn(LD_REND, "Error relaying cell across rendezvous; closing "
333 "circuits");
334 /* XXXX Do this here, or just return -1? */
335 circuit_mark_for_close(circ, -reason);
336 return reason;
338 return 0;
340 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
341 "Didn't recognize cell, but circ stops here! Closing circ.");
342 return -END_CIRC_REASON_TORPROTOCOL;
345 log_debug(LD_OR,"Passing on unrecognized cell.");
347 ++stats_n_relay_cells_relayed; /* XXXX no longer quite accurate {cells}
348 * we might kill the circ before we relay
349 * the cells. */
351 append_cell_to_circuit_queue(circ, chan, cell, cell_direction, 0);
352 return 0;
355 /** Package a relay cell from an edge:
356 * - Encrypt it to the right layer
357 * - Append it to the appropriate cell_queue on <b>circ</b>.
359 MOCK_IMPL(int,
360 circuit_package_relay_cell, (cell_t *cell, circuit_t *circ,
361 cell_direction_t cell_direction,
362 crypt_path_t *layer_hint, streamid_t on_stream,
363 const char *filename, int lineno))
365 channel_t *chan; /* where to send the cell */
367 if (circ->marked_for_close) {
368 /* Circuit is marked; send nothing. */
369 return 0;
372 if (cell_direction == CELL_DIRECTION_OUT) {
373 chan = circ->n_chan;
374 if (!chan) {
375 log_warn(LD_BUG,"outgoing relay cell sent from %s:%d has n_chan==NULL."
376 " Dropping. Circuit is in state %s (%d), and is "
377 "%smarked for close. (%s:%d, %d)", filename, lineno,
378 circuit_state_to_string(circ->state), circ->state,
379 circ->marked_for_close ? "" : "not ",
380 circ->marked_for_close_file?circ->marked_for_close_file:"",
381 circ->marked_for_close, circ->marked_for_close_reason);
382 if (CIRCUIT_IS_ORIGIN(circ)) {
383 circuit_log_path(LOG_WARN, LD_BUG, TO_ORIGIN_CIRCUIT(circ));
385 log_backtrace(LOG_WARN,LD_BUG,"");
386 return 0; /* just drop it */
388 if (!CIRCUIT_IS_ORIGIN(circ)) {
389 log_warn(LD_BUG,"outgoing relay cell sent from %s:%d on non-origin "
390 "circ. Dropping.", filename, lineno);
391 log_backtrace(LOG_WARN,LD_BUG,"");
392 return 0; /* just drop it */
395 relay_encrypt_cell_outbound(cell, TO_ORIGIN_CIRCUIT(circ), layer_hint);
397 /* Update circ written totals for control port */
398 origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
399 ocirc->n_written_circ_bw = tor_add_u32_nowrap(ocirc->n_written_circ_bw,
400 CELL_PAYLOAD_SIZE);
402 } else { /* incoming cell */
403 if (CIRCUIT_IS_ORIGIN(circ)) {
404 /* We should never package an _incoming_ cell from the circuit
405 * origin; that means we messed up somewhere. */
406 log_warn(LD_BUG,"incoming relay cell at origin circuit. Dropping.");
407 assert_circuit_ok(circ);
408 return 0; /* just drop it */
410 or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
411 relay_encrypt_cell_inbound(cell, or_circ);
412 chan = or_circ->p_chan;
414 ++stats_n_relay_cells_relayed;
416 append_cell_to_circuit_queue(circ, chan, cell, cell_direction, on_stream);
417 return 0;
420 /** If cell's stream_id matches the stream_id of any conn that's
421 * attached to circ, return that conn, else return NULL.
423 static edge_connection_t *
424 relay_lookup_conn(circuit_t *circ, cell_t *cell,
425 cell_direction_t cell_direction, crypt_path_t *layer_hint)
427 edge_connection_t *tmpconn;
428 relay_header_t rh;
430 relay_header_unpack(&rh, cell->payload);
432 if (!rh.stream_id)
433 return NULL;
435 /* IN or OUT cells could have come from either direction, now
436 * that we allow rendezvous *to* an OP.
439 if (CIRCUIT_IS_ORIGIN(circ)) {
440 for (tmpconn = TO_ORIGIN_CIRCUIT(circ)->p_streams; tmpconn;
441 tmpconn=tmpconn->next_stream) {
442 if (rh.stream_id == tmpconn->stream_id &&
443 !tmpconn->base_.marked_for_close &&
444 tmpconn->cpath_layer == layer_hint) {
445 log_debug(LD_APP,"found conn for stream %d.", rh.stream_id);
446 return tmpconn;
449 } else {
450 for (tmpconn = TO_OR_CIRCUIT(circ)->n_streams; tmpconn;
451 tmpconn=tmpconn->next_stream) {
452 if (rh.stream_id == tmpconn->stream_id &&
453 !tmpconn->base_.marked_for_close) {
454 log_debug(LD_EXIT,"found conn for stream %d.", rh.stream_id);
455 if (cell_direction == CELL_DIRECTION_OUT ||
456 connection_edge_is_rendezvous_stream(tmpconn))
457 return tmpconn;
460 for (tmpconn = TO_OR_CIRCUIT(circ)->resolving_streams; tmpconn;
461 tmpconn=tmpconn->next_stream) {
462 if (rh.stream_id == tmpconn->stream_id &&
463 !tmpconn->base_.marked_for_close) {
464 log_debug(LD_EXIT,"found conn for stream %d.", rh.stream_id);
465 return tmpconn;
469 return NULL; /* probably a begin relay cell */
472 /** Pack the relay_header_t host-order structure <b>src</b> into
473 * network-order in the buffer <b>dest</b>. See tor-spec.txt for details
474 * about the wire format.
476 void
477 relay_header_pack(uint8_t *dest, const relay_header_t *src)
479 set_uint8(dest, src->command);
480 set_uint16(dest+1, htons(src->recognized));
481 set_uint16(dest+3, htons(src->stream_id));
482 memcpy(dest+5, src->integrity, 4);
483 set_uint16(dest+9, htons(src->length));
486 /** Unpack the network-order buffer <b>src</b> into a host-order
487 * relay_header_t structure <b>dest</b>.
489 void
490 relay_header_unpack(relay_header_t *dest, const uint8_t *src)
492 dest->command = get_uint8(src);
493 dest->recognized = ntohs(get_uint16(src+1));
494 dest->stream_id = ntohs(get_uint16(src+3));
495 memcpy(dest->integrity, src+5, 4);
496 dest->length = ntohs(get_uint16(src+9));
499 /** Convert the relay <b>command</b> into a human-readable string. */
500 static const char *
501 relay_command_to_string(uint8_t command)
503 static char buf[64];
504 switch (command) {
505 case RELAY_COMMAND_BEGIN: return "BEGIN";
506 case RELAY_COMMAND_DATA: return "DATA";
507 case RELAY_COMMAND_END: return "END";
508 case RELAY_COMMAND_CONNECTED: return "CONNECTED";
509 case RELAY_COMMAND_SENDME: return "SENDME";
510 case RELAY_COMMAND_EXTEND: return "EXTEND";
511 case RELAY_COMMAND_EXTENDED: return "EXTENDED";
512 case RELAY_COMMAND_TRUNCATE: return "TRUNCATE";
513 case RELAY_COMMAND_TRUNCATED: return "TRUNCATED";
514 case RELAY_COMMAND_DROP: return "DROP";
515 case RELAY_COMMAND_RESOLVE: return "RESOLVE";
516 case RELAY_COMMAND_RESOLVED: return "RESOLVED";
517 case RELAY_COMMAND_BEGIN_DIR: return "BEGIN_DIR";
518 case RELAY_COMMAND_ESTABLISH_INTRO: return "ESTABLISH_INTRO";
519 case RELAY_COMMAND_ESTABLISH_RENDEZVOUS: return "ESTABLISH_RENDEZVOUS";
520 case RELAY_COMMAND_INTRODUCE1: return "INTRODUCE1";
521 case RELAY_COMMAND_INTRODUCE2: return "INTRODUCE2";
522 case RELAY_COMMAND_RENDEZVOUS1: return "RENDEZVOUS1";
523 case RELAY_COMMAND_RENDEZVOUS2: return "RENDEZVOUS2";
524 case RELAY_COMMAND_INTRO_ESTABLISHED: return "INTRO_ESTABLISHED";
525 case RELAY_COMMAND_RENDEZVOUS_ESTABLISHED:
526 return "RENDEZVOUS_ESTABLISHED";
527 case RELAY_COMMAND_INTRODUCE_ACK: return "INTRODUCE_ACK";
528 case RELAY_COMMAND_EXTEND2: return "EXTEND2";
529 case RELAY_COMMAND_EXTENDED2: return "EXTENDED2";
530 case RELAY_COMMAND_PADDING_NEGOTIATE: return "PADDING_NEGOTIATE";
531 case RELAY_COMMAND_PADDING_NEGOTIATED: return "PADDING_NEGOTIATED";
532 default:
533 tor_snprintf(buf, sizeof(buf), "Unrecognized relay command %u",
534 (unsigned)command);
535 return buf;
539 /** When padding a cell with randomness, leave this many zeros after the
540 * payload. */
541 #define CELL_PADDING_GAP 4
543 /** Return the offset where the padding should start. The <b>data_len</b> is
544 * the relay payload length expected to be put in the cell. It can not be
545 * bigger than RELAY_PAYLOAD_SIZE else this function assert().
547 * Value will always be smaller than CELL_PAYLOAD_SIZE because this offset is
548 * for the entire cell length not just the data payload length. Zero is
549 * returned if there is no room for padding.
551 * This function always skips the first 4 bytes after the payload because
552 * having some unused zero bytes has saved us a lot of times in the past. */
554 STATIC size_t
555 get_pad_cell_offset(size_t data_len)
557 /* This is never supposed to happen but in case it does, stop right away
558 * because if tor is tricked somehow into not adding random bytes to the
559 * payload with this function returning 0 for a bad data_len, the entire
560 * authenticated SENDME design can be bypassed leading to bad denial of
561 * service attacks. */
562 tor_assert(data_len <= RELAY_PAYLOAD_SIZE);
564 /* If the offset is larger than the cell payload size, we return an offset
565 * of zero indicating that no padding needs to be added. */
566 size_t offset = RELAY_HEADER_SIZE + data_len + CELL_PADDING_GAP;
567 if (offset >= CELL_PAYLOAD_SIZE) {
568 return 0;
570 return offset;
573 /* Add random bytes to the unused portion of the payload, to foil attacks
574 * where the other side can predict all of the bytes in the payload and thus
575 * compute the authenticated SENDME cells without seeing the traffic. See
576 * proposal 289. */
577 static void
578 pad_cell_payload(uint8_t *cell_payload, size_t data_len)
580 size_t pad_offset, pad_len;
582 tor_assert(cell_payload);
584 pad_offset = get_pad_cell_offset(data_len);
585 if (pad_offset == 0) {
586 /* We can't add padding so we are done. */
587 return;
590 /* Remember here that the cell_payload is the length of the header and
591 * payload size so we offset it using the full length of the cell. */
592 pad_len = CELL_PAYLOAD_SIZE - pad_offset;
593 crypto_fast_rng_getbytes(get_thread_fast_rng(),
594 cell_payload + pad_offset, pad_len);
597 /** Make a relay cell out of <b>relay_command</b> and <b>payload</b>, and send
598 * it onto the open circuit <b>circ</b>. <b>stream_id</b> is the ID on
599 * <b>circ</b> for the stream that's sending the relay cell, or 0 if it's a
600 * control cell. <b>cpath_layer</b> is NULL for OR->OP cells, or the
601 * destination hop for OP->OR cells.
603 * If you can't send the cell, mark the circuit for close and return -1. Else
604 * return 0.
606 MOCK_IMPL(int,
607 relay_send_command_from_edge_,(streamid_t stream_id, circuit_t *circ,
608 uint8_t relay_command, const char *payload,
609 size_t payload_len, crypt_path_t *cpath_layer,
610 const char *filename, int lineno))
612 cell_t cell;
613 relay_header_t rh;
614 cell_direction_t cell_direction;
615 /* XXXX NM Split this function into a separate versions per circuit type? */
617 tor_assert(circ);
618 tor_assert(payload_len <= RELAY_PAYLOAD_SIZE);
620 memset(&cell, 0, sizeof(cell_t));
621 cell.command = CELL_RELAY;
622 if (CIRCUIT_IS_ORIGIN(circ)) {
623 tor_assert(cpath_layer);
624 cell.circ_id = circ->n_circ_id;
625 cell_direction = CELL_DIRECTION_OUT;
626 } else {
627 tor_assert(! cpath_layer);
628 cell.circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
629 cell_direction = CELL_DIRECTION_IN;
632 memset(&rh, 0, sizeof(rh));
633 rh.command = relay_command;
634 rh.stream_id = stream_id;
635 rh.length = payload_len;
636 relay_header_pack(cell.payload, &rh);
637 if (payload_len)
638 memcpy(cell.payload+RELAY_HEADER_SIZE, payload, payload_len);
640 /* Add random padding to the cell if we can. */
641 pad_cell_payload(cell.payload, payload_len);
643 log_debug(LD_OR,"delivering %d cell %s.", relay_command,
644 cell_direction == CELL_DIRECTION_OUT ? "forward" : "backward");
646 /* Tell circpad we're sending a relay cell */
647 circpad_deliver_sent_relay_cell_events(circ, relay_command);
649 /* If we are sending an END cell and this circuit is used for a tunneled
650 * directory request, advance its state. */
651 if (relay_command == RELAY_COMMAND_END && circ->dirreq_id)
652 geoip_change_dirreq_state(circ->dirreq_id, DIRREQ_TUNNELED,
653 DIRREQ_END_CELL_SENT);
655 if (cell_direction == CELL_DIRECTION_OUT && circ->n_chan) {
656 /* if we're using relaybandwidthrate, this conn wants priority */
657 channel_timestamp_client(circ->n_chan);
660 if (cell_direction == CELL_DIRECTION_OUT) {
661 origin_circuit_t *origin_circ = TO_ORIGIN_CIRCUIT(circ);
662 if (origin_circ->remaining_relay_early_cells > 0 &&
663 (relay_command == RELAY_COMMAND_EXTEND ||
664 relay_command == RELAY_COMMAND_EXTEND2 ||
665 cpath_layer != origin_circ->cpath)) {
666 /* If we've got any relay_early cells left and (we're sending
667 * an extend cell or we're not talking to the first hop), use
668 * one of them. Don't worry about the conn protocol version:
669 * append_cell_to_circuit_queue will fix it up. */
670 cell.command = CELL_RELAY_EARLY;
671 /* If we're out of relay early cells, tell circpad */
672 if (--origin_circ->remaining_relay_early_cells == 0)
673 circpad_machine_event_circ_has_no_relay_early(origin_circ);
674 log_debug(LD_OR, "Sending a RELAY_EARLY cell; %d remaining.",
675 (int)origin_circ->remaining_relay_early_cells);
676 /* Memorize the command that is sent as RELAY_EARLY cell; helps debug
677 * task 878. */
678 origin_circ->relay_early_commands[
679 origin_circ->relay_early_cells_sent++] = relay_command;
680 } else if (relay_command == RELAY_COMMAND_EXTEND ||
681 relay_command == RELAY_COMMAND_EXTEND2) {
682 /* If no RELAY_EARLY cells can be sent over this circuit, log which
683 * commands have been sent as RELAY_EARLY cells before; helps debug
684 * task 878. */
685 smartlist_t *commands_list = smartlist_new();
686 int i = 0;
687 char *commands = NULL;
688 for (; i < origin_circ->relay_early_cells_sent; i++)
689 smartlist_add(commands_list, (char *)
690 relay_command_to_string(origin_circ->relay_early_commands[i]));
691 commands = smartlist_join_strings(commands_list, ",", 0, NULL);
692 log_warn(LD_BUG, "Uh-oh. We're sending a RELAY_COMMAND_EXTEND cell, "
693 "but we have run out of RELAY_EARLY cells on that circuit. "
694 "Commands sent before: %s", commands);
695 tor_free(commands);
696 smartlist_free(commands_list);
699 /* Let's assume we're well-behaved: Anything that we decide to send is
700 * valid, delivered data. */
701 circuit_sent_valid_data(origin_circ, rh.length);
704 if (circuit_package_relay_cell(&cell, circ, cell_direction, cpath_layer,
705 stream_id, filename, lineno) < 0) {
706 log_warn(LD_BUG,"circuit_package_relay_cell failed. Closing.");
707 circuit_mark_for_close(circ, END_CIRC_REASON_INTERNAL);
708 return -1;
711 /* If applicable, note the cell digest for the SENDME version 1 purpose if
712 * we need to. This call needs to be after the circuit_package_relay_cell()
713 * because the cell digest is set within that function. */
714 if (relay_command == RELAY_COMMAND_DATA) {
715 sendme_record_cell_digest_on_circ(circ, cpath_layer);
718 return 0;
721 /** Make a relay cell out of <b>relay_command</b> and <b>payload</b>, and
722 * send it onto the open circuit <b>circ</b>. <b>fromconn</b> is the stream
723 * that's sending the relay cell, or NULL if it's a control cell.
724 * <b>cpath_layer</b> is NULL for OR->OP cells, or the destination hop
725 * for OP->OR cells.
727 * If you can't send the cell, mark the circuit for close and
728 * return -1. Else return 0.
731 connection_edge_send_command(edge_connection_t *fromconn,
732 uint8_t relay_command, const char *payload,
733 size_t payload_len)
735 /* XXXX NM Split this function into a separate versions per circuit type? */
736 circuit_t *circ;
737 crypt_path_t *cpath_layer = fromconn->cpath_layer;
738 tor_assert(fromconn);
739 circ = fromconn->on_circuit;
741 if (fromconn->base_.marked_for_close) {
742 log_warn(LD_BUG,
743 "called on conn that's already marked for close at %s:%d.",
744 fromconn->base_.marked_for_close_file,
745 fromconn->base_.marked_for_close);
746 return 0;
749 if (!circ) {
750 if (fromconn->base_.type == CONN_TYPE_AP) {
751 log_info(LD_APP,"no circ. Closing conn.");
752 connection_mark_unattached_ap(EDGE_TO_ENTRY_CONN(fromconn),
753 END_STREAM_REASON_INTERNAL);
754 } else {
755 log_info(LD_EXIT,"no circ. Closing conn.");
756 fromconn->edge_has_sent_end = 1; /* no circ to send to */
757 fromconn->end_reason = END_STREAM_REASON_INTERNAL;
758 connection_mark_for_close(TO_CONN(fromconn));
760 return -1;
763 if (circ->marked_for_close) {
764 /* The circuit has been marked, but not freed yet. When it's freed, it
765 * will mark this connection for close. */
766 return -1;
769 #ifdef MEASUREMENTS_21206
770 /* Keep track of the number of RELAY_DATA cells sent for directory
771 * connections. */
772 connection_t *linked_conn = TO_CONN(fromconn)->linked_conn;
774 if (linked_conn && linked_conn->type == CONN_TYPE_DIR) {
775 ++(TO_DIR_CONN(linked_conn)->data_cells_sent);
777 #endif /* defined(MEASUREMENTS_21206) */
779 return relay_send_command_from_edge(fromconn->stream_id, circ,
780 relay_command, payload,
781 payload_len, cpath_layer);
784 /** How many times will I retry a stream that fails due to DNS
785 * resolve failure or misc error?
787 #define MAX_RESOLVE_FAILURES 3
789 /** Return 1 if reason is something that you should retry if you
790 * get the end cell before you've connected; else return 0. */
791 static int
792 edge_reason_is_retriable(int reason)
794 return reason == END_STREAM_REASON_HIBERNATING ||
795 reason == END_STREAM_REASON_RESOURCELIMIT ||
796 reason == END_STREAM_REASON_EXITPOLICY ||
797 reason == END_STREAM_REASON_RESOLVEFAILED ||
798 reason == END_STREAM_REASON_MISC ||
799 reason == END_STREAM_REASON_NOROUTE;
802 /** Called when we receive an END cell on a stream that isn't open yet,
803 * from the client side.
804 * Arguments are as for connection_edge_process_relay_cell().
806 static int
807 connection_ap_process_end_not_open(
808 relay_header_t *rh, cell_t *cell, origin_circuit_t *circ,
809 entry_connection_t *conn, crypt_path_t *layer_hint)
811 node_t *exitrouter;
812 int reason = *(cell->payload+RELAY_HEADER_SIZE);
813 int control_reason;
814 edge_connection_t *edge_conn = ENTRY_TO_EDGE_CONN(conn);
815 (void) layer_hint; /* unused */
817 if (rh->length > 0) {
818 if (reason == END_STREAM_REASON_TORPROTOCOL ||
819 reason == END_STREAM_REASON_DESTROY) {
820 /* Both of these reasons could mean a failed tag
821 * hit the exit and it complained. Do not probe.
822 * Fail the circuit. */
823 circ->path_state = PATH_STATE_USE_FAILED;
824 return -END_CIRC_REASON_TORPROTOCOL;
825 } else if (reason == END_STREAM_REASON_INTERNAL) {
826 /* We can't infer success or failure, since older Tors report
827 * ENETUNREACH as END_STREAM_REASON_INTERNAL. */
828 } else {
829 /* Path bias: If we get a valid reason code from the exit,
830 * it wasn't due to tagging.
832 * We rely on recognized+digest being strong enough to make
833 * tags unlikely to allow us to get tagged, yet 'recognized'
834 * reason codes here. */
835 pathbias_mark_use_success(circ);
839 /* This end cell is now valid. */
840 circuit_read_valid_data(circ, rh->length);
842 if (rh->length == 0) {
843 reason = END_STREAM_REASON_MISC;
846 control_reason = reason | END_STREAM_REASON_FLAG_REMOTE;
848 if (edge_reason_is_retriable(reason) &&
849 /* avoid retry if rend */
850 !connection_edge_is_rendezvous_stream(edge_conn)) {
851 const char *chosen_exit_digest =
852 circ->build_state->chosen_exit->identity_digest;
853 log_info(LD_APP,"Address '%s' refused due to '%s'. Considering retrying.",
854 safe_str(conn->socks_request->address),
855 stream_end_reason_to_string(reason));
856 exitrouter = node_get_mutable_by_id(chosen_exit_digest);
857 switch (reason) {
858 case END_STREAM_REASON_EXITPOLICY: {
859 tor_addr_t addr;
860 tor_addr_make_unspec(&addr);
861 if (rh->length >= 5) {
862 int ttl = -1;
863 tor_addr_make_unspec(&addr);
864 if (rh->length == 5 || rh->length == 9) {
865 tor_addr_from_ipv4n(&addr,
866 get_uint32(cell->payload+RELAY_HEADER_SIZE+1));
867 if (rh->length == 9)
868 ttl = (int)ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+5));
869 } else if (rh->length == 17 || rh->length == 21) {
870 tor_addr_from_ipv6_bytes(&addr,
871 (cell->payload+RELAY_HEADER_SIZE+1));
872 if (rh->length == 21)
873 ttl = (int)ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+17));
875 if (tor_addr_is_null(&addr)) {
876 log_info(LD_APP,"Address '%s' resolved to 0.0.0.0. Closing,",
877 safe_str(conn->socks_request->address));
878 connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
879 return 0;
882 if ((tor_addr_family(&addr) == AF_INET &&
883 !conn->entry_cfg.ipv4_traffic) ||
884 (tor_addr_family(&addr) == AF_INET6 &&
885 !conn->entry_cfg.ipv6_traffic)) {
886 log_fn(LOG_PROTOCOL_WARN, LD_APP,
887 "Got an EXITPOLICY failure on a connection with a "
888 "mismatched family. Closing.");
889 connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
890 return 0;
892 if (get_options()->ClientDNSRejectInternalAddresses &&
893 tor_addr_is_internal(&addr, 0)) {
894 log_info(LD_APP,"Address '%s' resolved to internal. Closing,",
895 safe_str(conn->socks_request->address));
896 connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
897 return 0;
900 client_dns_set_addressmap(conn,
901 conn->socks_request->address, &addr,
902 conn->chosen_exit_name, ttl);
905 char new_addr[TOR_ADDR_BUF_LEN];
906 tor_addr_to_str(new_addr, &addr, sizeof(new_addr), 1);
907 if (strcmp(conn->socks_request->address, new_addr)) {
908 strlcpy(conn->socks_request->address, new_addr,
909 sizeof(conn->socks_request->address));
910 control_event_stream_status(conn, STREAM_EVENT_REMAP, 0);
914 /* check if the exit *ought* to have allowed it */
916 adjust_exit_policy_from_exitpolicy_failure(circ,
917 conn,
918 exitrouter,
919 &addr);
921 if (conn->chosen_exit_optional ||
922 conn->chosen_exit_retries) {
923 /* stop wanting a specific exit */
924 conn->chosen_exit_optional = 0;
925 /* A non-zero chosen_exit_retries can happen if we set a
926 * TrackHostExits for this address under a port that the exit
927 * relay allows, but then try the same address with a different
928 * port that it doesn't allow to exit. We shouldn't unregister
929 * the mapping, since it is probably still wanted on the
930 * original port. But now we give away to the exit relay that
931 * we probably have a TrackHostExits on it. So be it. */
932 conn->chosen_exit_retries = 0;
933 tor_free(conn->chosen_exit_name); /* clears it */
935 if (connection_ap_detach_retriable(conn, circ, control_reason) >= 0)
936 return 0;
937 /* else, conn will get closed below */
938 break;
940 case END_STREAM_REASON_CONNECTREFUSED:
941 if (!conn->chosen_exit_optional)
942 break; /* break means it'll close, below */
943 /* Else fall through: expire this circuit, clear the
944 * chosen_exit_name field, and try again. */
945 FALLTHROUGH;
946 case END_STREAM_REASON_RESOLVEFAILED:
947 case END_STREAM_REASON_TIMEOUT:
948 case END_STREAM_REASON_MISC:
949 case END_STREAM_REASON_NOROUTE:
950 if (client_dns_incr_failures(conn->socks_request->address)
951 < MAX_RESOLVE_FAILURES) {
952 /* We haven't retried too many times; reattach the connection. */
953 circuit_log_path(LOG_INFO,LD_APP,circ);
954 /* Mark this circuit "unusable for new streams". */
955 mark_circuit_unusable_for_new_conns(circ);
957 if (conn->chosen_exit_optional) {
958 /* stop wanting a specific exit */
959 conn->chosen_exit_optional = 0;
960 tor_free(conn->chosen_exit_name); /* clears it */
962 if (connection_ap_detach_retriable(conn, circ, control_reason) >= 0)
963 return 0;
964 /* else, conn will get closed below */
965 } else {
966 log_notice(LD_APP,
967 "Have tried resolving or connecting to address '%s' "
968 "at %d different places. Giving up.",
969 safe_str(conn->socks_request->address),
970 MAX_RESOLVE_FAILURES);
971 /* clear the failures, so it will have a full try next time */
972 client_dns_clear_failures(conn->socks_request->address);
974 break;
975 case END_STREAM_REASON_HIBERNATING:
976 case END_STREAM_REASON_RESOURCELIMIT:
977 if (exitrouter) {
978 policies_set_node_exitpolicy_to_reject_all(exitrouter);
980 if (conn->chosen_exit_optional) {
981 /* stop wanting a specific exit */
982 conn->chosen_exit_optional = 0;
983 tor_free(conn->chosen_exit_name); /* clears it */
985 if (connection_ap_detach_retriable(conn, circ, control_reason) >= 0)
986 return 0;
987 /* else, will close below */
988 break;
989 } /* end switch */
990 log_info(LD_APP,"Giving up on retrying; conn can't be handled.");
993 log_info(LD_APP,
994 "Edge got end (%s) before we're connected. Marking for close.",
995 stream_end_reason_to_string(rh->length > 0 ? reason : -1));
996 circuit_log_path(LOG_INFO,LD_APP,circ);
997 /* need to test because of detach_retriable */
998 if (!ENTRY_TO_CONN(conn)->marked_for_close)
999 connection_mark_unattached_ap(conn, control_reason);
1000 return 0;
1003 /** Called when we have gotten an END_REASON_EXITPOLICY failure on <b>circ</b>
1004 * for <b>conn</b>, while attempting to connect via <b>node</b>. If the node
1005 * told us which address it rejected, then <b>addr</b> is that address;
1006 * otherwise it is AF_UNSPEC.
1008 * If we are sure the node should have allowed this address, mark the node as
1009 * having a reject *:* exit policy. Otherwise, mark the circuit as unusable
1010 * for this particular address.
1012 static void
1013 adjust_exit_policy_from_exitpolicy_failure(origin_circuit_t *circ,
1014 entry_connection_t *conn,
1015 node_t *node,
1016 const tor_addr_t *addr)
1018 int make_reject_all = 0;
1019 const sa_family_t family = tor_addr_family(addr);
1021 if (node) {
1022 tor_addr_t tmp;
1023 int asked_for_family = tor_addr_parse(&tmp, conn->socks_request->address);
1024 if (family == AF_UNSPEC) {
1025 make_reject_all = 1;
1026 } else if (node_exit_policy_is_exact(node, family) &&
1027 asked_for_family != -1 && !conn->chosen_exit_name) {
1028 make_reject_all = 1;
1031 if (make_reject_all) {
1032 log_info(LD_APP,
1033 "Exitrouter %s seems to be more restrictive than its exit "
1034 "policy. Not using this router as exit for now.",
1035 node_describe(node));
1036 policies_set_node_exitpolicy_to_reject_all(node);
1040 if (family != AF_UNSPEC)
1041 addr_policy_append_reject_addr(&circ->prepend_policy, addr);
1044 /** Helper: change the socks_request-&gt;address field on conn to the
1045 * dotted-quad representation of <b>new_addr</b>,
1046 * and send an appropriate REMAP event. */
1047 static void
1048 remap_event_helper(entry_connection_t *conn, const tor_addr_t *new_addr)
1050 tor_addr_to_str(conn->socks_request->address, new_addr,
1051 sizeof(conn->socks_request->address),
1053 control_event_stream_status(conn, STREAM_EVENT_REMAP,
1054 REMAP_STREAM_SOURCE_EXIT);
1057 /** Extract the contents of a connected cell in <b>cell</b>, whose relay
1058 * header has already been parsed into <b>rh</b>. On success, set
1059 * <b>addr_out</b> to the address we're connected to, and <b>ttl_out</b> to
1060 * the ttl of that address, in seconds, and return 0. On failure, return
1061 * -1.
1063 * Note that the resulting address can be UNSPEC if the connected cell had no
1064 * address (as for a stream to an union service or a tunneled directory
1065 * connection), and that the ttl can be absent (in which case <b>ttl_out</b>
1066 * is set to -1). */
1067 STATIC int
1068 connected_cell_parse(const relay_header_t *rh, const cell_t *cell,
1069 tor_addr_t *addr_out, int *ttl_out)
1071 uint32_t bytes;
1072 const uint8_t *payload = cell->payload + RELAY_HEADER_SIZE;
1074 tor_addr_make_unspec(addr_out);
1075 *ttl_out = -1;
1076 if (rh->length == 0)
1077 return 0;
1078 if (rh->length < 4)
1079 return -1;
1080 bytes = ntohl(get_uint32(payload));
1082 /* If bytes is 0, this is maybe a v6 address. Otherwise it's a v4 address */
1083 if (bytes != 0) {
1084 /* v4 address */
1085 tor_addr_from_ipv4h(addr_out, bytes);
1086 if (rh->length >= 8) {
1087 bytes = ntohl(get_uint32(payload + 4));
1088 if (bytes <= INT32_MAX)
1089 *ttl_out = bytes;
1091 } else {
1092 if (rh->length < 25) /* 4 bytes of 0s, 1 addr, 16 ipv4, 4 ttl. */
1093 return -1;
1094 if (get_uint8(payload + 4) != 6)
1095 return -1;
1096 tor_addr_from_ipv6_bytes(addr_out, (payload + 5));
1097 bytes = ntohl(get_uint32(payload + 21));
1098 if (bytes <= INT32_MAX)
1099 *ttl_out = (int) bytes;
1101 return 0;
1104 /** Drop all storage held by <b>addr</b>. */
1105 STATIC void
1106 address_ttl_free_(address_ttl_t *addr)
1108 if (!addr)
1109 return;
1110 tor_free(addr->hostname);
1111 tor_free(addr);
1114 /** Parse a resolved cell in <b>cell</b>, with parsed header in <b>rh</b>.
1115 * Return -1 on parse error. On success, add one or more newly allocated
1116 * address_ttl_t to <b>addresses_out</b>; set *<b>errcode_out</b> to
1117 * one of 0, RESOLVED_TYPE_ERROR, or RESOLVED_TYPE_ERROR_TRANSIENT, and
1118 * return 0. */
1119 STATIC int
1120 resolved_cell_parse(const cell_t *cell, const relay_header_t *rh,
1121 smartlist_t *addresses_out, int *errcode_out)
1123 const uint8_t *cp;
1124 uint8_t answer_type;
1125 size_t answer_len;
1126 address_ttl_t *addr;
1127 size_t remaining;
1128 int errcode = 0;
1129 smartlist_t *addrs;
1131 tor_assert(cell);
1132 tor_assert(rh);
1133 tor_assert(addresses_out);
1134 tor_assert(errcode_out);
1136 *errcode_out = 0;
1138 if (rh->length > RELAY_PAYLOAD_SIZE)
1139 return -1;
1141 addrs = smartlist_new();
1143 cp = cell->payload + RELAY_HEADER_SIZE;
1145 remaining = rh->length;
1146 while (remaining) {
1147 const uint8_t *cp_orig = cp;
1148 if (remaining < 2)
1149 goto err;
1150 answer_type = *cp++;
1151 answer_len = *cp++;
1152 if (remaining < 2 + answer_len + 4) {
1153 goto err;
1155 if (answer_type == RESOLVED_TYPE_IPV4) {
1156 if (answer_len != 4) {
1157 goto err;
1159 addr = tor_malloc_zero(sizeof(*addr));
1160 tor_addr_from_ipv4n(&addr->addr, get_uint32(cp));
1161 cp += 4;
1162 addr->ttl = ntohl(get_uint32(cp));
1163 cp += 4;
1164 smartlist_add(addrs, addr);
1165 } else if (answer_type == RESOLVED_TYPE_IPV6) {
1166 if (answer_len != 16)
1167 goto err;
1168 addr = tor_malloc_zero(sizeof(*addr));
1169 tor_addr_from_ipv6_bytes(&addr->addr, cp);
1170 cp += 16;
1171 addr->ttl = ntohl(get_uint32(cp));
1172 cp += 4;
1173 smartlist_add(addrs, addr);
1174 } else if (answer_type == RESOLVED_TYPE_HOSTNAME) {
1175 if (answer_len == 0) {
1176 goto err;
1178 addr = tor_malloc_zero(sizeof(*addr));
1179 addr->hostname = tor_memdup_nulterm(cp, answer_len);
1180 cp += answer_len;
1181 addr->ttl = ntohl(get_uint32(cp));
1182 cp += 4;
1183 smartlist_add(addrs, addr);
1184 } else if (answer_type == RESOLVED_TYPE_ERROR_TRANSIENT ||
1185 answer_type == RESOLVED_TYPE_ERROR) {
1186 errcode = answer_type;
1187 /* Ignore the error contents */
1188 cp += answer_len + 4;
1189 } else {
1190 cp += answer_len + 4;
1192 tor_assert(((ssize_t)remaining) >= (cp - cp_orig));
1193 remaining -= (cp - cp_orig);
1196 if (errcode && smartlist_len(addrs) == 0) {
1197 /* Report an error only if there were no results. */
1198 *errcode_out = errcode;
1201 smartlist_add_all(addresses_out, addrs);
1202 smartlist_free(addrs);
1204 return 0;
1206 err:
1207 /* On parse error, don't report any results */
1208 SMARTLIST_FOREACH(addrs, address_ttl_t *, a, address_ttl_free(a));
1209 smartlist_free(addrs);
1210 return -1;
1213 /** Helper for connection_edge_process_resolved_cell: given an error code,
1214 * an entry_connection, and a list of address_ttl_t *, report the best answer
1215 * to the entry_connection. */
1216 static void
1217 connection_ap_handshake_socks_got_resolved_cell(entry_connection_t *conn,
1218 int error_code,
1219 smartlist_t *results)
1221 address_ttl_t *addr_ipv4 = NULL;
1222 address_ttl_t *addr_ipv6 = NULL;
1223 address_ttl_t *addr_hostname = NULL;
1224 address_ttl_t *addr_best = NULL;
1226 /* If it's an error code, that's easy. */
1227 if (error_code) {
1228 tor_assert(error_code == RESOLVED_TYPE_ERROR ||
1229 error_code == RESOLVED_TYPE_ERROR_TRANSIENT);
1230 connection_ap_handshake_socks_resolved(conn,
1231 error_code,0,NULL,-1,-1);
1232 return;
1235 /* Get the first answer of each type. */
1236 SMARTLIST_FOREACH_BEGIN(results, address_ttl_t *, addr) {
1237 if (addr->hostname) {
1238 if (!addr_hostname) {
1239 addr_hostname = addr;
1241 } else if (tor_addr_family(&addr->addr) == AF_INET) {
1242 if (!addr_ipv4 && conn->entry_cfg.ipv4_traffic) {
1243 addr_ipv4 = addr;
1245 } else if (tor_addr_family(&addr->addr) == AF_INET6) {
1246 if (!addr_ipv6 && conn->entry_cfg.ipv6_traffic) {
1247 addr_ipv6 = addr;
1250 } SMARTLIST_FOREACH_END(addr);
1252 /* Now figure out which type we wanted to deliver. */
1253 if (conn->socks_request->command == SOCKS_COMMAND_RESOLVE_PTR) {
1254 if (addr_hostname) {
1255 connection_ap_handshake_socks_resolved(conn,
1256 RESOLVED_TYPE_HOSTNAME,
1257 strlen(addr_hostname->hostname),
1258 (uint8_t*)addr_hostname->hostname,
1259 addr_hostname->ttl,-1);
1260 } else {
1261 connection_ap_handshake_socks_resolved(conn,
1262 RESOLVED_TYPE_ERROR,0,NULL,-1,-1);
1264 return;
1267 if (conn->entry_cfg.prefer_ipv6) {
1268 addr_best = addr_ipv6 ? addr_ipv6 : addr_ipv4;
1269 } else {
1270 addr_best = addr_ipv4 ? addr_ipv4 : addr_ipv6;
1273 /* Now convert it to the ugly old interface */
1274 if (! addr_best) {
1275 connection_ap_handshake_socks_resolved(conn,
1276 RESOLVED_TYPE_ERROR,0,NULL,-1,-1);
1277 return;
1280 connection_ap_handshake_socks_resolved_addr(conn,
1281 &addr_best->addr,
1282 addr_best->ttl,
1283 -1);
1285 remap_event_helper(conn, &addr_best->addr);
1288 /** Handle a RELAY_COMMAND_RESOLVED cell that we received on a non-open AP
1289 * stream. */
1290 STATIC int
1291 connection_edge_process_resolved_cell(edge_connection_t *conn,
1292 const cell_t *cell,
1293 const relay_header_t *rh)
1295 entry_connection_t *entry_conn = EDGE_TO_ENTRY_CONN(conn);
1296 smartlist_t *resolved_addresses = NULL;
1297 int errcode = 0;
1299 if (conn->base_.state != AP_CONN_STATE_RESOLVE_WAIT) {
1300 log_fn(LOG_PROTOCOL_WARN, LD_APP, "Got a 'resolved' cell while "
1301 "not in state resolve_wait. Dropping.");
1302 return 0;
1304 tor_assert(SOCKS_COMMAND_IS_RESOLVE(entry_conn->socks_request->command));
1306 resolved_addresses = smartlist_new();
1307 if (resolved_cell_parse(cell, rh, resolved_addresses, &errcode)) {
1308 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1309 "Dropping malformed 'resolved' cell");
1310 connection_mark_unattached_ap(entry_conn, END_STREAM_REASON_TORPROTOCOL);
1311 goto done;
1314 if (get_options()->ClientDNSRejectInternalAddresses) {
1315 int orig_len = smartlist_len(resolved_addresses);
1316 SMARTLIST_FOREACH_BEGIN(resolved_addresses, address_ttl_t *, addr) {
1317 if (addr->hostname == NULL && tor_addr_is_internal(&addr->addr, 0)) {
1318 log_info(LD_APP, "Got a resolved cell with answer %s; dropping that "
1319 "answer.",
1320 safe_str_client(fmt_addr(&addr->addr)));
1321 address_ttl_free(addr);
1322 SMARTLIST_DEL_CURRENT(resolved_addresses, addr);
1324 } SMARTLIST_FOREACH_END(addr);
1325 if (orig_len && smartlist_len(resolved_addresses) == 0) {
1326 log_info(LD_APP, "Got a resolved cell with only private addresses; "
1327 "dropping it.");
1328 connection_ap_handshake_socks_resolved(entry_conn,
1329 RESOLVED_TYPE_ERROR_TRANSIENT,
1330 0, NULL, 0, TIME_MAX);
1331 connection_mark_unattached_ap(entry_conn,
1332 END_STREAM_REASON_TORPROTOCOL);
1333 goto done;
1337 /* This is valid data at this point. Count it */
1338 if (conn->on_circuit && CIRCUIT_IS_ORIGIN(conn->on_circuit)) {
1339 circuit_read_valid_data(TO_ORIGIN_CIRCUIT(conn->on_circuit),
1340 rh->length);
1343 connection_ap_handshake_socks_got_resolved_cell(entry_conn,
1344 errcode,
1345 resolved_addresses);
1347 connection_mark_unattached_ap(entry_conn,
1348 END_STREAM_REASON_DONE |
1349 END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED);
1351 done:
1352 SMARTLIST_FOREACH(resolved_addresses, address_ttl_t *, addr,
1353 address_ttl_free(addr));
1354 smartlist_free(resolved_addresses);
1355 return 0;
1358 /** An incoming relay cell has arrived from circuit <b>circ</b> to
1359 * stream <b>conn</b>.
1361 * The arguments here are the same as in
1362 * connection_edge_process_relay_cell() below; this function is called
1363 * from there when <b>conn</b> is defined and not in an open state.
1365 static int
1366 connection_edge_process_relay_cell_not_open(
1367 relay_header_t *rh, cell_t *cell, circuit_t *circ,
1368 edge_connection_t *conn, crypt_path_t *layer_hint)
1370 if (rh->command == RELAY_COMMAND_END) {
1371 if (CIRCUIT_IS_ORIGIN(circ) && conn->base_.type == CONN_TYPE_AP) {
1372 return connection_ap_process_end_not_open(rh, cell,
1373 TO_ORIGIN_CIRCUIT(circ),
1374 EDGE_TO_ENTRY_CONN(conn),
1375 layer_hint);
1376 } else {
1377 /* we just got an 'end', don't need to send one */
1378 conn->edge_has_sent_end = 1;
1379 conn->end_reason = *(cell->payload+RELAY_HEADER_SIZE) |
1380 END_STREAM_REASON_FLAG_REMOTE;
1381 connection_mark_for_close(TO_CONN(conn));
1382 return 0;
1386 if (conn->base_.type == CONN_TYPE_AP &&
1387 rh->command == RELAY_COMMAND_CONNECTED) {
1388 tor_addr_t addr;
1389 int ttl;
1390 entry_connection_t *entry_conn = EDGE_TO_ENTRY_CONN(conn);
1391 tor_assert(CIRCUIT_IS_ORIGIN(circ));
1392 if (conn->base_.state != AP_CONN_STATE_CONNECT_WAIT) {
1393 log_fn(LOG_PROTOCOL_WARN, LD_APP,
1394 "Got 'connected' while not in state connect_wait. Dropping.");
1395 return 0;
1397 CONNECTION_AP_EXPECT_NONPENDING(entry_conn);
1398 conn->base_.state = AP_CONN_STATE_OPEN;
1399 log_info(LD_APP,"'connected' received for circid %u streamid %d "
1400 "after %d seconds.",
1401 (unsigned)circ->n_circ_id,
1402 rh->stream_id,
1403 (int)(time(NULL) - conn->base_.timestamp_last_read_allowed));
1404 if (connected_cell_parse(rh, cell, &addr, &ttl) < 0) {
1405 log_fn(LOG_PROTOCOL_WARN, LD_APP,
1406 "Got a badly formatted connected cell. Closing.");
1407 connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL);
1408 connection_mark_unattached_ap(entry_conn, END_STREAM_REASON_TORPROTOCOL);
1409 return 0;
1411 if (tor_addr_family(&addr) != AF_UNSPEC) {
1412 /* The family is not UNSPEC: so we were given an address in the
1413 * connected cell. (This is normal, except for BEGINDIR and onion
1414 * service streams.) */
1415 const sa_family_t family = tor_addr_family(&addr);
1416 if (tor_addr_is_null(&addr) ||
1417 (get_options()->ClientDNSRejectInternalAddresses &&
1418 tor_addr_is_internal(&addr, 0))) {
1419 log_info(LD_APP, "...but it claims the IP address was %s. Closing.",
1420 fmt_addr(&addr));
1421 connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL);
1422 connection_mark_unattached_ap(entry_conn,
1423 END_STREAM_REASON_TORPROTOCOL);
1424 return 0;
1427 if ((family == AF_INET && ! entry_conn->entry_cfg.ipv4_traffic) ||
1428 (family == AF_INET6 && ! entry_conn->entry_cfg.ipv6_traffic)) {
1429 log_fn(LOG_PROTOCOL_WARN, LD_APP,
1430 "Got a connected cell to %s with unsupported address family."
1431 " Closing.", fmt_addr(&addr));
1432 connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL);
1433 connection_mark_unattached_ap(entry_conn,
1434 END_STREAM_REASON_TORPROTOCOL);
1435 return 0;
1438 client_dns_set_addressmap(entry_conn,
1439 entry_conn->socks_request->address, &addr,
1440 entry_conn->chosen_exit_name, ttl);
1442 remap_event_helper(entry_conn, &addr);
1444 circuit_log_path(LOG_INFO,LD_APP,TO_ORIGIN_CIRCUIT(circ));
1445 /* don't send a socks reply to transparent conns */
1446 tor_assert(entry_conn->socks_request != NULL);
1447 if (!entry_conn->socks_request->has_finished) {
1448 connection_ap_handshake_socks_reply(entry_conn, NULL, 0, 0);
1451 /* Was it a linked dir conn? If so, a dir request just started to
1452 * fetch something; this could be a bootstrap status milestone. */
1453 log_debug(LD_APP, "considering");
1454 if (TO_CONN(conn)->linked_conn &&
1455 TO_CONN(conn)->linked_conn->type == CONN_TYPE_DIR) {
1456 connection_t *dirconn = TO_CONN(conn)->linked_conn;
1457 log_debug(LD_APP, "it is! %d", dirconn->purpose);
1458 switch (dirconn->purpose) {
1459 case DIR_PURPOSE_FETCH_CERTIFICATE:
1460 if (consensus_is_waiting_for_certs())
1461 control_event_bootstrap(BOOTSTRAP_STATUS_LOADING_KEYS, 0);
1462 break;
1463 case DIR_PURPOSE_FETCH_CONSENSUS:
1464 control_event_bootstrap(BOOTSTRAP_STATUS_LOADING_STATUS, 0);
1465 break;
1466 case DIR_PURPOSE_FETCH_SERVERDESC:
1467 case DIR_PURPOSE_FETCH_MICRODESC:
1468 if (TO_DIR_CONN(dirconn)->router_purpose == ROUTER_PURPOSE_GENERAL)
1469 control_event_boot_dir(BOOTSTRAP_STATUS_LOADING_DESCRIPTORS,
1470 count_loading_descriptors_progress());
1471 break;
1474 /* This is definitely a success, so forget about any pending data we
1475 * had sent. */
1476 if (entry_conn->pending_optimistic_data) {
1477 buf_free(entry_conn->pending_optimistic_data);
1478 entry_conn->pending_optimistic_data = NULL;
1481 /* This is valid data at this point. Count it */
1482 circuit_read_valid_data(TO_ORIGIN_CIRCUIT(circ), rh->length);
1484 /* handle anything that might have queued */
1485 if (connection_edge_package_raw_inbuf(conn, 1, NULL) < 0) {
1486 /* (We already sent an end cell if possible) */
1487 connection_mark_for_close(TO_CONN(conn));
1488 return 0;
1490 return 0;
1492 if (conn->base_.type == CONN_TYPE_AP &&
1493 rh->command == RELAY_COMMAND_RESOLVED) {
1494 return connection_edge_process_resolved_cell(conn, cell, rh);
1497 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1498 "Got an unexpected relay command %d, in state %d (%s). Dropping.",
1499 rh->command, conn->base_.state,
1500 conn_state_to_string(conn->base_.type, conn->base_.state));
1501 return 0; /* for forward compatibility, don't kill the circuit */
1502 // connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL);
1503 // connection_mark_for_close(conn);
1504 // return -1;
1507 /** Process a SENDME cell that arrived on <b>circ</b>. If it is a stream level
1508 * cell, it is destined for the given <b>conn</b>. If it is a circuit level
1509 * cell, it is destined for the <b>layer_hint</b>. The <b>domain</b> is the
1510 * logging domain that should be used.
1512 * Return 0 if everything went well or a negative value representing a circuit
1513 * end reason on error for which the caller is responsible for closing it. */
1514 static int
1515 process_sendme_cell(const relay_header_t *rh, const cell_t *cell,
1516 circuit_t *circ, edge_connection_t *conn,
1517 crypt_path_t *layer_hint, int domain)
1519 int ret;
1521 tor_assert(rh);
1523 if (!rh->stream_id) {
1524 /* Circuit level SENDME cell. */
1525 ret = sendme_process_circuit_level(layer_hint, circ,
1526 cell->payload + RELAY_HEADER_SIZE,
1527 rh->length);
1528 if (ret < 0) {
1529 return ret;
1531 /* Resume reading on any streams now that we've processed a valid
1532 * SENDME cell that updated our package window. */
1533 circuit_resume_edge_reading(circ, layer_hint);
1534 /* We are done, the rest of the code is for the stream level. */
1535 return 0;
1538 /* No connection, might be half edge state. We are done if so. */
1539 if (!conn) {
1540 if (CIRCUIT_IS_ORIGIN(circ)) {
1541 origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
1542 if (connection_half_edge_is_valid_sendme(ocirc->half_streams,
1543 rh->stream_id)) {
1544 circuit_read_valid_data(ocirc, rh->length);
1545 log_info(domain, "Sendme cell on circ %u valid on half-closed "
1546 "stream id %d",
1547 ocirc->global_identifier, rh->stream_id);
1551 log_info(domain, "SENDME cell dropped, unknown stream (streamid %d).",
1552 rh->stream_id);
1553 return 0;
1556 /* Stream level SENDME cell. */
1557 ret = sendme_process_stream_level(conn, circ, rh->length);
1558 if (ret < 0) {
1559 /* Means we need to close the circuit with reason ret. */
1560 return ret;
1563 /* We've now processed properly a SENDME cell, all windows have been
1564 * properly updated, we'll read on the edge connection to see if we can
1565 * get data out towards the end point (Exit or client) since we are now
1566 * allowed to deliver more cells. */
1568 if (circuit_queue_streams_are_blocked(circ)) {
1569 /* Still waiting for queue to flush; don't touch conn */
1570 return 0;
1572 connection_start_reading(TO_CONN(conn));
1573 /* handle whatever might still be on the inbuf */
1574 if (connection_edge_package_raw_inbuf(conn, 1, NULL) < 0) {
1575 /* (We already sent an end cell if possible) */
1576 connection_mark_for_close(TO_CONN(conn));
1577 return 0;
1579 return 0;
1582 /** A helper for connection_edge_process_relay_cell(): Actually handles the
1583 * cell that we received on the connection.
1585 * The arguments are the same as in the parent function
1586 * connection_edge_process_relay_cell(), plus the relay header <b>rh</b> as
1587 * unpacked by the parent function, and <b>optimistic_data</b> as set by the
1588 * parent function.
1590 STATIC int
1591 handle_relay_cell_command(cell_t *cell, circuit_t *circ,
1592 edge_connection_t *conn, crypt_path_t *layer_hint,
1593 relay_header_t *rh, int optimistic_data)
1595 unsigned domain = layer_hint?LD_APP:LD_EXIT;
1596 int reason;
1598 tor_assert(rh);
1600 /* First pass the cell to the circuit padding subsystem, in case it's a
1601 * padding cell or circuit that should be handled there. */
1602 if (circpad_check_received_cell(cell, circ, layer_hint, rh) == 0) {
1603 log_debug(domain, "Cell handled as circuit padding");
1604 return 0;
1607 /* Now handle all the other commands */
1608 switch (rh->command) {
1609 case RELAY_COMMAND_BEGIN:
1610 case RELAY_COMMAND_BEGIN_DIR:
1611 if (layer_hint &&
1612 circ->purpose != CIRCUIT_PURPOSE_S_REND_JOINED) {
1613 log_fn(LOG_PROTOCOL_WARN, LD_APP,
1614 "Relay begin request unsupported at AP. Dropping.");
1615 return 0;
1617 if (circ->purpose == CIRCUIT_PURPOSE_S_REND_JOINED &&
1618 layer_hint != TO_ORIGIN_CIRCUIT(circ)->cpath->prev) {
1619 log_fn(LOG_PROTOCOL_WARN, LD_APP,
1620 "Relay begin request to Hidden Service "
1621 "from intermediary node. Dropping.");
1622 return 0;
1624 if (conn) {
1625 log_fn(LOG_PROTOCOL_WARN, domain,
1626 "Begin cell for known stream. Dropping.");
1627 return 0;
1629 if (rh->command == RELAY_COMMAND_BEGIN_DIR &&
1630 circ->purpose != CIRCUIT_PURPOSE_S_REND_JOINED) {
1631 /* Assign this circuit and its app-ward OR connection a unique ID,
1632 * so that we can measure download times. The local edge and dir
1633 * connection will be assigned the same ID when they are created
1634 * and linked. */
1635 static uint64_t next_id = 0;
1636 circ->dirreq_id = ++next_id;
1637 TO_OR_CIRCUIT(circ)->p_chan->dirreq_id = circ->dirreq_id;
1639 return connection_exit_begin_conn(cell, circ);
1640 case RELAY_COMMAND_DATA:
1641 ++stats_n_data_cells_received;
1643 /* Update our circuit-level deliver window that we received a DATA cell.
1644 * If the deliver window goes below 0, we end the circuit and stream due
1645 * to a protocol failure. */
1646 if (sendme_circuit_data_received(circ, layer_hint) < 0) {
1647 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1648 "(relay data) circ deliver_window below 0. Killing.");
1649 connection_edge_end_close(conn, END_STREAM_REASON_TORPROTOCOL);
1650 return -END_CIRC_REASON_TORPROTOCOL;
1653 /* Consider sending a circuit-level SENDME cell. */
1654 sendme_circuit_consider_sending(circ, layer_hint);
1656 if (rh->stream_id == 0) {
1657 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, "Relay data cell with zero "
1658 "stream_id. Dropping.");
1659 return 0;
1660 } else if (!conn) {
1661 if (CIRCUIT_IS_ORIGIN(circ)) {
1662 origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
1663 if (connection_half_edge_is_valid_data(ocirc->half_streams,
1664 rh->stream_id)) {
1665 circuit_read_valid_data(ocirc, rh->length);
1666 log_info(domain,
1667 "data cell on circ %u valid on half-closed "
1668 "stream id %d", ocirc->global_identifier, rh->stream_id);
1672 log_info(domain,"data cell dropped, unknown stream (streamid %d).",
1673 rh->stream_id);
1674 return 0;
1677 /* Update our stream-level deliver window that we just received a DATA
1678 * cell. Going below 0 means we have a protocol level error so the
1679 * stream and circuit are closed. */
1681 if (sendme_stream_data_received(conn) < 0) {
1682 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1683 "(relay data) conn deliver_window below 0. Killing.");
1684 connection_edge_end_close(conn, END_STREAM_REASON_TORPROTOCOL);
1685 return -END_CIRC_REASON_TORPROTOCOL;
1687 /* Total all valid application bytes delivered */
1688 if (CIRCUIT_IS_ORIGIN(circ) && rh->length > 0) {
1689 circuit_read_valid_data(TO_ORIGIN_CIRCUIT(circ), rh->length);
1692 /* For onion service connection, update the metrics. */
1693 if (conn->hs_ident) {
1694 hs_metrics_app_write_bytes(&conn->hs_ident->identity_pk,
1695 conn->hs_ident->orig_virtual_port,
1696 rh->length);
1699 stats_n_data_bytes_received += rh->length;
1700 connection_buf_add((char*)(cell->payload + RELAY_HEADER_SIZE),
1701 rh->length, TO_CONN(conn));
1703 #ifdef MEASUREMENTS_21206
1704 /* Count number of RELAY_DATA cells received on a linked directory
1705 * connection. */
1706 connection_t *linked_conn = TO_CONN(conn)->linked_conn;
1708 if (linked_conn && linked_conn->type == CONN_TYPE_DIR) {
1709 ++(TO_DIR_CONN(linked_conn)->data_cells_received);
1711 #endif /* defined(MEASUREMENTS_21206) */
1713 if (!optimistic_data) {
1714 /* Only send a SENDME if we're not getting optimistic data; otherwise
1715 * a SENDME could arrive before the CONNECTED.
1717 sendme_connection_edge_consider_sending(conn);
1720 return 0;
1721 case RELAY_COMMAND_END:
1722 reason = rh->length > 0 ?
1723 get_uint8(cell->payload+RELAY_HEADER_SIZE) : END_STREAM_REASON_MISC;
1724 if (!conn) {
1725 if (CIRCUIT_IS_ORIGIN(circ)) {
1726 origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
1727 if (connection_half_edge_is_valid_end(ocirc->half_streams,
1728 rh->stream_id)) {
1730 circuit_read_valid_data(ocirc, rh->length);
1731 log_info(domain,
1732 "end cell (%s) on circ %u valid on half-closed "
1733 "stream id %d",
1734 stream_end_reason_to_string(reason),
1735 ocirc->global_identifier, rh->stream_id);
1736 return 0;
1739 log_info(domain,"end cell (%s) dropped, unknown stream.",
1740 stream_end_reason_to_string(reason));
1741 return 0;
1743 /* XXX add to this log_fn the exit node's nickname? */
1744 log_info(domain,TOR_SOCKET_T_FORMAT": end cell (%s) for stream %d. "
1745 "Removing stream.",
1746 conn->base_.s,
1747 stream_end_reason_to_string(reason),
1748 conn->stream_id);
1749 if (conn->base_.type == CONN_TYPE_AP) {
1750 entry_connection_t *entry_conn = EDGE_TO_ENTRY_CONN(conn);
1751 if (entry_conn->socks_request &&
1752 !entry_conn->socks_request->has_finished)
1753 log_warn(LD_BUG,
1754 "open stream hasn't sent socks answer yet? Closing.");
1756 /* We just *got* an end; no reason to send one. */
1757 conn->edge_has_sent_end = 1;
1758 if (!conn->end_reason)
1759 conn->end_reason = reason | END_STREAM_REASON_FLAG_REMOTE;
1760 if (!conn->base_.marked_for_close) {
1761 /* only mark it if not already marked. it's possible to
1762 * get the 'end' right around when the client hangs up on us. */
1763 connection_mark_and_flush(TO_CONN(conn));
1765 /* Total all valid application bytes delivered */
1766 if (CIRCUIT_IS_ORIGIN(circ)) {
1767 circuit_read_valid_data(TO_ORIGIN_CIRCUIT(circ), rh->length);
1770 return 0;
1771 case RELAY_COMMAND_EXTEND:
1772 case RELAY_COMMAND_EXTEND2: {
1773 static uint64_t total_n_extend=0, total_nonearly=0;
1774 total_n_extend++;
1775 if (rh->stream_id) {
1776 log_fn(LOG_PROTOCOL_WARN, domain,
1777 "'extend' cell received for non-zero stream. Dropping.");
1778 return 0;
1780 if (cell->command != CELL_RELAY_EARLY &&
1781 !networkstatus_get_param(NULL,"AllowNonearlyExtend",0,0,1)) {
1782 #define EARLY_WARNING_INTERVAL 3600
1783 static ratelim_t early_warning_limit =
1784 RATELIM_INIT(EARLY_WARNING_INTERVAL);
1785 char *m;
1786 if (cell->command == CELL_RELAY) {
1787 ++total_nonearly;
1788 if ((m = rate_limit_log(&early_warning_limit, approx_time()))) {
1789 double percentage = ((double)total_nonearly)/total_n_extend;
1790 percentage *= 100;
1791 log_fn(LOG_PROTOCOL_WARN, domain, "EXTEND cell received, "
1792 "but not via RELAY_EARLY. Dropping.%s", m);
1793 log_fn(LOG_PROTOCOL_WARN, domain, " (We have dropped %.02f%% of "
1794 "all EXTEND cells for this reason)", percentage);
1795 tor_free(m);
1797 } else {
1798 log_fn(LOG_WARN, domain,
1799 "EXTEND cell received, in a cell with type %d! Dropping.",
1800 cell->command);
1802 return 0;
1804 return circuit_extend(cell, circ);
1806 case RELAY_COMMAND_EXTENDED:
1807 case RELAY_COMMAND_EXTENDED2:
1808 if (!layer_hint) {
1809 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1810 "'extended' unsupported at non-origin. Dropping.");
1811 return 0;
1813 log_debug(domain,"Got an extended cell! Yay.");
1815 extended_cell_t extended_cell;
1816 if (extended_cell_parse(&extended_cell, rh->command,
1817 (const uint8_t*)cell->payload+RELAY_HEADER_SIZE,
1818 rh->length)<0) {
1819 log_warn(LD_PROTOCOL,
1820 "Can't parse EXTENDED cell; killing circuit.");
1821 return -END_CIRC_REASON_TORPROTOCOL;
1823 if ((reason = circuit_finish_handshake(TO_ORIGIN_CIRCUIT(circ),
1824 &extended_cell.created_cell)) < 0) {
1825 circuit_mark_for_close(circ, -reason);
1826 return 0; /* We don't want to cause a warning, so we mark the circuit
1827 * here. */
1830 if ((reason=circuit_send_next_onion_skin(TO_ORIGIN_CIRCUIT(circ)))<0) {
1831 log_info(domain,"circuit_send_next_onion_skin() failed.");
1832 return reason;
1834 /* Total all valid bytes delivered. */
1835 if (CIRCUIT_IS_ORIGIN(circ)) {
1836 circuit_read_valid_data(TO_ORIGIN_CIRCUIT(circ), rh->length);
1838 return 0;
1839 case RELAY_COMMAND_TRUNCATE:
1840 if (layer_hint) {
1841 log_fn(LOG_PROTOCOL_WARN, LD_APP,
1842 "'truncate' unsupported at origin. Dropping.");
1843 return 0;
1845 if (circ->n_hop) {
1846 if (circ->n_chan)
1847 log_warn(LD_BUG, "n_chan and n_hop set on the same circuit!");
1848 extend_info_free(circ->n_hop);
1849 circ->n_hop = NULL;
1850 tor_free(circ->n_chan_create_cell);
1851 circuit_set_state(circ, CIRCUIT_STATE_OPEN);
1853 if (circ->n_chan) {
1854 uint8_t trunc_reason = get_uint8(cell->payload + RELAY_HEADER_SIZE);
1855 circuit_synchronize_written_or_bandwidth(circ, CIRCUIT_N_CHAN);
1856 circuit_clear_cell_queue(circ, circ->n_chan);
1857 channel_send_destroy(circ->n_circ_id, circ->n_chan,
1858 trunc_reason);
1859 circuit_set_n_circid_chan(circ, 0, NULL);
1861 log_debug(LD_EXIT, "Processed 'truncate', replying.");
1863 char payload[1];
1864 payload[0] = (char)END_CIRC_REASON_REQUESTED;
1865 relay_send_command_from_edge(0, circ, RELAY_COMMAND_TRUNCATED,
1866 payload, sizeof(payload), NULL);
1868 return 0;
1869 case RELAY_COMMAND_TRUNCATED:
1870 if (!layer_hint) {
1871 log_fn(LOG_PROTOCOL_WARN, LD_EXIT,
1872 "'truncated' unsupported at non-origin. Dropping.");
1873 return 0;
1876 /* Count the truncated as valid, for completeness. The
1877 * circuit is being torn down anyway, though. */
1878 if (CIRCUIT_IS_ORIGIN(circ)) {
1879 circuit_read_valid_data(TO_ORIGIN_CIRCUIT(circ),
1880 rh->length);
1882 circuit_truncated(TO_ORIGIN_CIRCUIT(circ),
1883 get_uint8(cell->payload + RELAY_HEADER_SIZE));
1884 return 0;
1885 case RELAY_COMMAND_CONNECTED:
1886 if (conn) {
1887 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1888 "'connected' unsupported while open. Closing circ.");
1889 return -END_CIRC_REASON_TORPROTOCOL;
1892 if (CIRCUIT_IS_ORIGIN(circ)) {
1893 origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
1894 if (connection_half_edge_is_valid_connected(ocirc->half_streams,
1895 rh->stream_id)) {
1896 circuit_read_valid_data(ocirc, rh->length);
1897 log_info(domain,
1898 "connected cell on circ %u valid on half-closed "
1899 "stream id %d", ocirc->global_identifier, rh->stream_id);
1900 return 0;
1904 log_info(domain,
1905 "'connected' received on circid %u for streamid %d, "
1906 "no conn attached anymore. Ignoring.",
1907 (unsigned)circ->n_circ_id, rh->stream_id);
1908 return 0;
1909 case RELAY_COMMAND_SENDME:
1910 return process_sendme_cell(rh, cell, circ, conn, layer_hint, domain);
1911 case RELAY_COMMAND_RESOLVE:
1912 if (layer_hint) {
1913 log_fn(LOG_PROTOCOL_WARN, LD_APP,
1914 "resolve request unsupported at AP; dropping.");
1915 return 0;
1916 } else if (conn) {
1917 log_fn(LOG_PROTOCOL_WARN, domain,
1918 "resolve request for known stream; dropping.");
1919 return 0;
1920 } else if (circ->purpose != CIRCUIT_PURPOSE_OR) {
1921 log_fn(LOG_PROTOCOL_WARN, domain,
1922 "resolve request on circ with purpose %d; dropping",
1923 circ->purpose);
1924 return 0;
1926 connection_exit_begin_resolve(cell, TO_OR_CIRCUIT(circ));
1927 return 0;
1928 case RELAY_COMMAND_RESOLVED:
1929 if (conn) {
1930 log_fn(LOG_PROTOCOL_WARN, domain,
1931 "'resolved' unsupported while open. Closing circ.");
1932 return -END_CIRC_REASON_TORPROTOCOL;
1935 if (CIRCUIT_IS_ORIGIN(circ)) {
1936 origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
1937 if (connection_half_edge_is_valid_resolved(ocirc->half_streams,
1938 rh->stream_id)) {
1939 circuit_read_valid_data(ocirc, rh->length);
1940 log_info(domain,
1941 "resolved cell on circ %u valid on half-closed "
1942 "stream id %d", ocirc->global_identifier, rh->stream_id);
1943 return 0;
1947 log_info(domain,
1948 "'resolved' received, no conn attached anymore. Ignoring.");
1949 return 0;
1950 case RELAY_COMMAND_ESTABLISH_INTRO:
1951 case RELAY_COMMAND_ESTABLISH_RENDEZVOUS:
1952 case RELAY_COMMAND_INTRODUCE1:
1953 case RELAY_COMMAND_INTRODUCE2:
1954 case RELAY_COMMAND_INTRODUCE_ACK:
1955 case RELAY_COMMAND_RENDEZVOUS1:
1956 case RELAY_COMMAND_RENDEZVOUS2:
1957 case RELAY_COMMAND_INTRO_ESTABLISHED:
1958 case RELAY_COMMAND_RENDEZVOUS_ESTABLISHED:
1959 rend_process_relay_cell(circ, layer_hint,
1960 rh->command, rh->length,
1961 cell->payload+RELAY_HEADER_SIZE);
1962 return 0;
1964 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1965 "Received unknown relay command %d. Perhaps the other side is using "
1966 "a newer version of Tor? Dropping.",
1967 rh->command);
1968 return 0; /* for forward compatibility, don't kill the circuit */
1971 /** An incoming relay cell has arrived on circuit <b>circ</b>. If
1972 * <b>conn</b> is NULL this is a control cell, else <b>cell</b> is
1973 * destined for <b>conn</b>.
1975 * If <b>layer_hint</b> is defined, then we're the origin of the
1976 * circuit, and it specifies the hop that packaged <b>cell</b>.
1978 * Return -reason if you want to warn and tear down the circuit, else 0.
1980 STATIC int
1981 connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ,
1982 edge_connection_t *conn,
1983 crypt_path_t *layer_hint)
1985 static int num_seen=0;
1986 relay_header_t rh;
1987 unsigned domain = layer_hint?LD_APP:LD_EXIT;
1988 int optimistic_data = 0; /* Set to 1 if we receive data on a stream
1989 * that's in the EXIT_CONN_STATE_RESOLVING
1990 * or EXIT_CONN_STATE_CONNECTING states. */
1992 tor_assert(cell);
1993 tor_assert(circ);
1995 relay_header_unpack(&rh, cell->payload);
1996 // log_fn(LOG_DEBUG,"command %d stream %d", rh.command, rh.stream_id);
1997 num_seen++;
1998 log_debug(domain, "Now seen %d relay cells here (command %d, stream %d).",
1999 num_seen, rh.command, rh.stream_id);
2001 if (rh.length > RELAY_PAYLOAD_SIZE) {
2002 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
2003 "Relay cell length field too long. Closing circuit.");
2004 return - END_CIRC_REASON_TORPROTOCOL;
2007 if (rh.stream_id == 0) {
2008 switch (rh.command) {
2009 case RELAY_COMMAND_BEGIN:
2010 case RELAY_COMMAND_CONNECTED:
2011 case RELAY_COMMAND_END:
2012 case RELAY_COMMAND_RESOLVE:
2013 case RELAY_COMMAND_RESOLVED:
2014 case RELAY_COMMAND_BEGIN_DIR:
2015 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, "Relay command %d with zero "
2016 "stream_id. Dropping.", (int)rh.command);
2017 return 0;
2018 default:
2023 /* Tell circpad that we've received a recognized cell */
2024 circpad_deliver_recognized_relay_cell_events(circ, rh.command, layer_hint);
2026 /* either conn is NULL, in which case we've got a control cell, or else
2027 * conn points to the recognized stream. */
2028 if (conn && !connection_state_is_open(TO_CONN(conn))) {
2029 if (conn->base_.type == CONN_TYPE_EXIT &&
2030 (conn->base_.state == EXIT_CONN_STATE_CONNECTING ||
2031 conn->base_.state == EXIT_CONN_STATE_RESOLVING) &&
2032 rh.command == RELAY_COMMAND_DATA) {
2033 /* Allow DATA cells to be delivered to an exit node in state
2034 * EXIT_CONN_STATE_CONNECTING or EXIT_CONN_STATE_RESOLVING.
2035 * This speeds up HTTP, for example. */
2036 optimistic_data = 1;
2037 } else if (rh.stream_id == 0 && rh.command == RELAY_COMMAND_DATA) {
2038 log_warn(LD_BUG, "Somehow I had a connection that matched a "
2039 "data cell with stream ID 0.");
2040 } else {
2041 return connection_edge_process_relay_cell_not_open(
2042 &rh, cell, circ, conn, layer_hint);
2046 return handle_relay_cell_command(cell, circ, conn, layer_hint,
2047 &rh, optimistic_data);
2050 /** How many relay_data cells have we built, ever? */
2051 uint64_t stats_n_data_cells_packaged = 0;
2052 /** How many bytes of data have we put in relay_data cells have we built,
2053 * ever? This would be RELAY_PAYLOAD_SIZE*stats_n_data_cells_packaged if
2054 * every relay cell we ever sent were completely full of data. */
2055 uint64_t stats_n_data_bytes_packaged = 0;
2056 /** How many relay_data cells have we received, ever? */
2057 uint64_t stats_n_data_cells_received = 0;
2058 /** How many bytes of data have we received relay_data cells, ever? This would
2059 * be RELAY_PAYLOAD_SIZE*stats_n_data_cells_packaged if every relay cell we
2060 * ever received were completely full of data. */
2061 uint64_t stats_n_data_bytes_received = 0;
2064 * Called when initializing a circuit, or when we have reached the end of the
2065 * window in which we need to send some randomness so that incoming sendme
2066 * cells will be unpredictable. Resets the flags and picks a new window.
2068 void
2069 circuit_reset_sendme_randomness(circuit_t *circ)
2071 circ->have_sent_sufficiently_random_cell = 0;
2072 circ->send_randomness_after_n_cells = CIRCWINDOW_INCREMENT / 2 +
2073 crypto_fast_rng_get_uint(get_thread_fast_rng(), CIRCWINDOW_INCREMENT / 2);
2077 * Any relay data payload containing fewer than this many real bytes is
2078 * considered to have enough randomness to.
2080 #define RELAY_PAYLOAD_LENGTH_FOR_RANDOM_SENDMES \
2081 (RELAY_PAYLOAD_SIZE - CELL_PADDING_GAP - 16)
2084 * Helper. Return the number of bytes that should be put into a cell from a
2085 * given edge connection on which <b>n_available</b> bytes are available.
2087 STATIC size_t
2088 connection_edge_get_inbuf_bytes_to_package(size_t n_available,
2089 int package_partial,
2090 circuit_t *on_circuit)
2092 if (!n_available)
2093 return 0;
2095 /* Do we need to force this payload to have space for randomness? */
2096 const bool force_random_bytes =
2097 (on_circuit->send_randomness_after_n_cells == 0) &&
2098 (! on_circuit->have_sent_sufficiently_random_cell);
2100 /* At most how much would we like to send in this cell? */
2101 size_t target_length;
2102 if (force_random_bytes) {
2103 target_length = RELAY_PAYLOAD_LENGTH_FOR_RANDOM_SENDMES;
2104 } else {
2105 target_length = RELAY_PAYLOAD_SIZE;
2108 /* Decide how many bytes we will actually put into this cell. */
2109 size_t package_length;
2110 if (n_available >= target_length) { /* A full payload is available. */
2111 package_length = target_length;
2112 } else { /* not a full payload available */
2113 if (package_partial)
2114 package_length = n_available; /* just take whatever's available now */
2115 else
2116 return 0; /* nothing to do until we have a full payload */
2119 /* If we reach this point, we will be definitely sending the cell. */
2120 tor_assert_nonfatal(package_length > 0);
2122 if (package_length <= RELAY_PAYLOAD_LENGTH_FOR_RANDOM_SENDMES) {
2123 /* This cell will have enough randomness in the padding to make a future
2124 * sendme cell unpredictable. */
2125 on_circuit->have_sent_sufficiently_random_cell = 1;
2128 if (on_circuit->send_randomness_after_n_cells == 0) {
2129 /* Either this cell, or some previous cell, had enough padding to
2130 * ensure sendme unpredictability. */
2131 tor_assert_nonfatal(on_circuit->have_sent_sufficiently_random_cell);
2132 /* Pick a new interval in which we need to send randomness. */
2133 circuit_reset_sendme_randomness(on_circuit);
2136 --on_circuit->send_randomness_after_n_cells;
2138 return package_length;
2141 /** If <b>conn</b> has an entire relay payload of bytes on its inbuf (or
2142 * <b>package_partial</b> is true), and the appropriate package windows aren't
2143 * empty, grab a cell and send it down the circuit.
2145 * If *<b>max_cells</b> is given, package no more than max_cells. Decrement
2146 * *<b>max_cells</b> by the number of cells packaged.
2148 * Return -1 (and send a RELAY_COMMAND_END cell if necessary) if conn should
2149 * be marked for close, else return 0.
2152 connection_edge_package_raw_inbuf(edge_connection_t *conn, int package_partial,
2153 int *max_cells)
2155 size_t bytes_to_process, length;
2156 char payload[CELL_PAYLOAD_SIZE];
2157 circuit_t *circ;
2158 const unsigned domain = conn->base_.type == CONN_TYPE_AP ? LD_APP : LD_EXIT;
2159 int sending_from_optimistic = 0;
2160 entry_connection_t *entry_conn =
2161 conn->base_.type == CONN_TYPE_AP ? EDGE_TO_ENTRY_CONN(conn) : NULL;
2162 const int sending_optimistically =
2163 entry_conn &&
2164 conn->base_.type == CONN_TYPE_AP &&
2165 conn->base_.state != AP_CONN_STATE_OPEN;
2166 crypt_path_t *cpath_layer = conn->cpath_layer;
2168 tor_assert(conn);
2170 if (conn->base_.marked_for_close) {
2171 log_warn(LD_BUG,
2172 "called on conn that's already marked for close at %s:%d.",
2173 conn->base_.marked_for_close_file, conn->base_.marked_for_close);
2174 return 0;
2177 if (max_cells && *max_cells <= 0)
2178 return 0;
2180 repeat_connection_edge_package_raw_inbuf:
2182 circ = circuit_get_by_edge_conn(conn);
2183 if (!circ) {
2184 log_info(domain,"conn has no circuit! Closing.");
2185 conn->end_reason = END_STREAM_REASON_CANT_ATTACH;
2186 return -1;
2189 if (circuit_consider_stop_edge_reading(circ, cpath_layer))
2190 return 0;
2192 if (conn->package_window <= 0) {
2193 log_info(domain,"called with package_window %d. Skipping.",
2194 conn->package_window);
2195 connection_stop_reading(TO_CONN(conn));
2196 return 0;
2199 sending_from_optimistic = entry_conn &&
2200 entry_conn->sending_optimistic_data != NULL;
2202 if (PREDICT_UNLIKELY(sending_from_optimistic)) {
2203 bytes_to_process = buf_datalen(entry_conn->sending_optimistic_data);
2204 if (PREDICT_UNLIKELY(!bytes_to_process)) {
2205 log_warn(LD_BUG, "sending_optimistic_data was non-NULL but empty");
2206 bytes_to_process = connection_get_inbuf_len(TO_CONN(conn));
2207 sending_from_optimistic = 0;
2209 } else {
2210 bytes_to_process = connection_get_inbuf_len(TO_CONN(conn));
2213 length = connection_edge_get_inbuf_bytes_to_package(bytes_to_process,
2214 package_partial, circ);
2215 if (!length)
2216 return 0;
2218 /* If we reach this point, we will definitely be packaging bytes into
2219 * a cell. */
2221 stats_n_data_bytes_packaged += length;
2222 stats_n_data_cells_packaged += 1;
2224 if (PREDICT_UNLIKELY(sending_from_optimistic)) {
2225 /* XXXX We could be more efficient here by sometimes packing
2226 * previously-sent optimistic data in the same cell with data
2227 * from the inbuf. */
2228 buf_get_bytes(entry_conn->sending_optimistic_data, payload, length);
2229 if (!buf_datalen(entry_conn->sending_optimistic_data)) {
2230 buf_free(entry_conn->sending_optimistic_data);
2231 entry_conn->sending_optimistic_data = NULL;
2233 } else {
2234 connection_buf_get_bytes(payload, length, TO_CONN(conn));
2237 log_debug(domain,TOR_SOCKET_T_FORMAT": Packaging %d bytes (%d waiting).",
2238 conn->base_.s,
2239 (int)length, (int)connection_get_inbuf_len(TO_CONN(conn)));
2241 if (sending_optimistically && !sending_from_optimistic) {
2242 /* This is new optimistic data; remember it in case we need to detach and
2243 retry */
2244 if (!entry_conn->pending_optimistic_data)
2245 entry_conn->pending_optimistic_data = buf_new();
2246 buf_add(entry_conn->pending_optimistic_data, payload, length);
2249 if (connection_edge_send_command(conn, RELAY_COMMAND_DATA,
2250 payload, length) < 0 ) {
2251 /* circuit got marked for close, don't continue, don't need to mark conn */
2252 return 0;
2255 /* Handle the circuit-level SENDME package window. */
2256 if (sendme_note_circuit_data_packaged(circ, cpath_layer) < 0) {
2257 /* Package window has gone under 0. Protocol issue. */
2258 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
2259 "Circuit package window is below 0. Closing circuit.");
2260 conn->end_reason = END_STREAM_REASON_TORPROTOCOL;
2261 return -1;
2264 /* Handle the stream-level SENDME package window. */
2265 if (sendme_note_stream_data_packaged(conn) < 0) {
2266 connection_stop_reading(TO_CONN(conn));
2267 log_debug(domain,"conn->package_window reached 0.");
2268 circuit_consider_stop_edge_reading(circ, cpath_layer);
2269 return 0; /* don't process the inbuf any more */
2271 log_debug(domain,"conn->package_window is now %d",conn->package_window);
2273 if (max_cells) {
2274 *max_cells -= 1;
2275 if (*max_cells <= 0)
2276 return 0;
2279 /* handle more if there's more, or return 0 if there isn't */
2280 goto repeat_connection_edge_package_raw_inbuf;
2283 /** The circuit <b>circ</b> has received a circuit-level sendme
2284 * (on hop <b>layer_hint</b>, if we're the OP). Go through all the
2285 * attached streams and let them resume reading and packaging, if
2286 * their stream windows allow it.
2288 static void
2289 circuit_resume_edge_reading(circuit_t *circ, crypt_path_t *layer_hint)
2291 if (circuit_queue_streams_are_blocked(circ)) {
2292 log_debug(layer_hint?LD_APP:LD_EXIT,"Too big queue, no resuming");
2293 return;
2295 log_debug(layer_hint?LD_APP:LD_EXIT,"resuming");
2297 if (CIRCUIT_IS_ORIGIN(circ))
2298 circuit_resume_edge_reading_helper(TO_ORIGIN_CIRCUIT(circ)->p_streams,
2299 circ, layer_hint);
2300 else
2301 circuit_resume_edge_reading_helper(TO_OR_CIRCUIT(circ)->n_streams,
2302 circ, layer_hint);
2305 /** A helper function for circuit_resume_edge_reading() above.
2306 * The arguments are the same, except that <b>conn</b> is the head
2307 * of a linked list of edge streams that should each be considered.
2309 static int
2310 circuit_resume_edge_reading_helper(edge_connection_t *first_conn,
2311 circuit_t *circ,
2312 crypt_path_t *layer_hint)
2314 edge_connection_t *conn;
2315 int n_packaging_streams, n_streams_left;
2316 int packaged_this_round;
2317 int cells_on_queue;
2318 int cells_per_conn;
2319 edge_connection_t *chosen_stream = NULL;
2320 int max_to_package;
2322 if (first_conn == NULL) {
2323 /* Don't bother to try to do the rest of this if there are no connections
2324 * to resume. */
2325 return 0;
2328 /* How many cells do we have space for? It will be the minimum of
2329 * the number needed to exhaust the package window, and the minimum
2330 * needed to fill the cell queue. */
2331 max_to_package = circ->package_window;
2332 if (CIRCUIT_IS_ORIGIN(circ)) {
2333 cells_on_queue = circ->n_chan_cells.n;
2334 } else {
2335 or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
2336 cells_on_queue = or_circ->p_chan_cells.n;
2338 if (CELL_QUEUE_HIGHWATER_SIZE - cells_on_queue < max_to_package)
2339 max_to_package = CELL_QUEUE_HIGHWATER_SIZE - cells_on_queue;
2341 /* Once we used to start listening on the streams in the order they
2342 * appeared in the linked list. That leads to starvation on the
2343 * streams that appeared later on the list, since the first streams
2344 * would always get to read first. Instead, we just pick a random
2345 * stream on the list, and enable reading for streams starting at that
2346 * point (and wrapping around as if the list were circular). It would
2347 * probably be better to actually remember which streams we've
2348 * serviced in the past, but this is simple and effective. */
2350 /* Select a stream uniformly at random from the linked list. We
2351 * don't need cryptographic randomness here. */
2353 int num_streams = 0;
2354 for (conn = first_conn; conn; conn = conn->next_stream) {
2355 num_streams++;
2357 if (crypto_fast_rng_one_in_n(get_thread_fast_rng(), num_streams)) {
2358 chosen_stream = conn;
2360 /* Invariant: chosen_stream has been chosen uniformly at random from
2361 * among the first num_streams streams on first_conn.
2363 * (Note that we iterate over every stream on the circuit, so that after
2364 * we've considered the first stream, we've chosen it with P=1; and
2365 * after we consider the second stream, we've switched to it with P=1/2
2366 * and stayed with the first stream with P=1/2; and after we've
2367 * considered the third stream, we've switched to it with P=1/3 and
2368 * remained with one of the first two streams with P=(2/3), giving each
2369 * one P=(1/2)(2/3) )=(1/3).) */
2373 /* Count how many non-marked streams there are that have anything on
2374 * their inbuf, and enable reading on all of the connections. */
2375 n_packaging_streams = 0;
2376 /* Activate reading starting from the chosen stream */
2377 for (conn=chosen_stream; conn; conn = conn->next_stream) {
2378 /* Start reading for the streams starting from here */
2379 if (conn->base_.marked_for_close || conn->package_window <= 0)
2380 continue;
2381 if (!layer_hint || conn->cpath_layer == layer_hint) {
2382 connection_start_reading(TO_CONN(conn));
2384 if (connection_get_inbuf_len(TO_CONN(conn)) > 0)
2385 ++n_packaging_streams;
2388 /* Go back and do the ones we skipped, circular-style */
2389 for (conn = first_conn; conn != chosen_stream; conn = conn->next_stream) {
2390 if (conn->base_.marked_for_close || conn->package_window <= 0)
2391 continue;
2392 if (!layer_hint || conn->cpath_layer == layer_hint) {
2393 connection_start_reading(TO_CONN(conn));
2395 if (connection_get_inbuf_len(TO_CONN(conn)) > 0)
2396 ++n_packaging_streams;
2400 if (n_packaging_streams == 0) /* avoid divide-by-zero */
2401 return 0;
2403 again:
2405 cells_per_conn = CEIL_DIV(max_to_package, n_packaging_streams);
2407 packaged_this_round = 0;
2408 n_streams_left = 0;
2410 /* Iterate over all connections. Package up to cells_per_conn cells on
2411 * each. Update packaged_this_round with the total number of cells
2412 * packaged, and n_streams_left with the number that still have data to
2413 * package.
2415 for (conn=first_conn; conn; conn=conn->next_stream) {
2416 if (conn->base_.marked_for_close || conn->package_window <= 0)
2417 continue;
2418 if (!layer_hint || conn->cpath_layer == layer_hint) {
2419 int n = cells_per_conn, r;
2420 /* handle whatever might still be on the inbuf */
2421 r = connection_edge_package_raw_inbuf(conn, 1, &n);
2423 /* Note how many we packaged */
2424 packaged_this_round += (cells_per_conn-n);
2426 if (r<0) {
2427 /* Problem while packaging. (We already sent an end cell if
2428 * possible) */
2429 connection_mark_for_close(TO_CONN(conn));
2430 continue;
2433 /* If there's still data to read, we'll be coming back to this stream. */
2434 if (connection_get_inbuf_len(TO_CONN(conn)))
2435 ++n_streams_left;
2437 /* If the circuit won't accept any more data, return without looking
2438 * at any more of the streams. Any connections that should be stopped
2439 * have already been stopped by connection_edge_package_raw_inbuf. */
2440 if (circuit_consider_stop_edge_reading(circ, layer_hint))
2441 return -1;
2442 /* XXXX should we also stop immediately if we fill up the cell queue?
2443 * Probably. */
2447 /* If we made progress, and we are willing to package more, and there are
2448 * any streams left that want to package stuff... try again!
2450 if (packaged_this_round && packaged_this_round < max_to_package &&
2451 n_streams_left) {
2452 max_to_package -= packaged_this_round;
2453 n_packaging_streams = n_streams_left;
2454 goto again;
2457 return 0;
2460 /** Check if the package window for <b>circ</b> is empty (at
2461 * hop <b>layer_hint</b> if it's defined).
2463 * If yes, tell edge streams to stop reading and return 1.
2464 * Else return 0.
2466 static int
2467 circuit_consider_stop_edge_reading(circuit_t *circ, crypt_path_t *layer_hint)
2469 edge_connection_t *conn = NULL;
2470 unsigned domain = layer_hint ? LD_APP : LD_EXIT;
2472 if (!layer_hint) {
2473 or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
2474 log_debug(domain,"considering circ->package_window %d",
2475 circ->package_window);
2476 if (circ->package_window <= 0) {
2477 log_debug(domain,"yes, not-at-origin. stopped.");
2478 for (conn = or_circ->n_streams; conn; conn=conn->next_stream)
2479 connection_stop_reading(TO_CONN(conn));
2480 return 1;
2482 return 0;
2484 /* else, layer hint is defined, use it */
2485 log_debug(domain,"considering layer_hint->package_window %d",
2486 layer_hint->package_window);
2487 if (layer_hint->package_window <= 0) {
2488 log_debug(domain,"yes, at-origin. stopped.");
2489 for (conn = TO_ORIGIN_CIRCUIT(circ)->p_streams; conn;
2490 conn=conn->next_stream) {
2491 if (conn->cpath_layer == layer_hint)
2492 connection_stop_reading(TO_CONN(conn));
2494 return 1;
2496 return 0;
2499 /** The total number of cells we have allocated. */
2500 static size_t total_cells_allocated = 0;
2502 /** Release storage held by <b>cell</b>. */
2503 static inline void
2504 packed_cell_free_unchecked(packed_cell_t *cell)
2506 --total_cells_allocated;
2507 tor_free(cell);
2510 /** Allocate and return a new packed_cell_t. */
2511 STATIC packed_cell_t *
2512 packed_cell_new(void)
2514 ++total_cells_allocated;
2515 return tor_malloc_zero(sizeof(packed_cell_t));
2518 /** Return a packed cell used outside by channel_t lower layer */
2519 void
2520 packed_cell_free_(packed_cell_t *cell)
2522 if (!cell)
2523 return;
2524 packed_cell_free_unchecked(cell);
2527 /** Log current statistics for cell pool allocation at log level
2528 * <b>severity</b>. */
2529 void
2530 dump_cell_pool_usage(int severity)
2532 int n_circs = 0;
2533 int n_cells = 0;
2534 SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, c) {
2535 n_cells += c->n_chan_cells.n;
2536 if (!CIRCUIT_IS_ORIGIN(c))
2537 n_cells += TO_OR_CIRCUIT(c)->p_chan_cells.n;
2538 ++n_circs;
2540 SMARTLIST_FOREACH_END(c);
2541 tor_log(severity, LD_MM,
2542 "%d cells allocated on %d circuits. %d cells leaked.",
2543 n_cells, n_circs, (int)total_cells_allocated - n_cells);
2546 /** Allocate a new copy of packed <b>cell</b>. */
2547 static inline packed_cell_t *
2548 packed_cell_copy(const cell_t *cell, int wide_circ_ids)
2550 packed_cell_t *c = packed_cell_new();
2551 cell_pack(c, cell, wide_circ_ids);
2552 return c;
2555 /** Append <b>cell</b> to the end of <b>queue</b>. */
2556 void
2557 cell_queue_append(cell_queue_t *queue, packed_cell_t *cell)
2559 TOR_SIMPLEQ_INSERT_TAIL(&queue->head, cell, next);
2560 ++queue->n;
2563 /** Append a newly allocated copy of <b>cell</b> to the end of the
2564 * <b>exitward</b> (or app-ward) <b>queue</b> of <b>circ</b>. If
2565 * <b>use_stats</b> is true, record statistics about the cell.
2567 void
2568 cell_queue_append_packed_copy(circuit_t *circ, cell_queue_t *queue,
2569 int exitward, const cell_t *cell,
2570 int wide_circ_ids, int use_stats)
2572 packed_cell_t *copy = packed_cell_copy(cell, wide_circ_ids);
2573 (void)circ;
2574 (void)exitward;
2575 (void)use_stats;
2577 copy->inserted_timestamp = monotime_coarse_get_stamp();
2579 cell_queue_append(queue, copy);
2582 /** Initialize <b>queue</b> as an empty cell queue. */
2583 void
2584 cell_queue_init(cell_queue_t *queue)
2586 memset(queue, 0, sizeof(cell_queue_t));
2587 TOR_SIMPLEQ_INIT(&queue->head);
2590 /** Remove and free every cell in <b>queue</b>. */
2591 void
2592 cell_queue_clear(cell_queue_t *queue)
2594 packed_cell_t *cell;
2595 while ((cell = TOR_SIMPLEQ_FIRST(&queue->head))) {
2596 TOR_SIMPLEQ_REMOVE_HEAD(&queue->head, next);
2597 packed_cell_free_unchecked(cell);
2599 TOR_SIMPLEQ_INIT(&queue->head);
2600 queue->n = 0;
2603 /** Extract and return the cell at the head of <b>queue</b>; return NULL if
2604 * <b>queue</b> is empty. */
2605 STATIC packed_cell_t *
2606 cell_queue_pop(cell_queue_t *queue)
2608 packed_cell_t *cell = TOR_SIMPLEQ_FIRST(&queue->head);
2609 if (!cell)
2610 return NULL;
2611 TOR_SIMPLEQ_REMOVE_HEAD(&queue->head, next);
2612 --queue->n;
2613 return cell;
2616 /** Initialize <b>queue</b> as an empty cell queue. */
2617 void
2618 destroy_cell_queue_init(destroy_cell_queue_t *queue)
2620 memset(queue, 0, sizeof(destroy_cell_queue_t));
2621 TOR_SIMPLEQ_INIT(&queue->head);
2624 /** Remove and free every cell in <b>queue</b>. */
2625 void
2626 destroy_cell_queue_clear(destroy_cell_queue_t *queue)
2628 destroy_cell_t *cell;
2629 while ((cell = TOR_SIMPLEQ_FIRST(&queue->head))) {
2630 TOR_SIMPLEQ_REMOVE_HEAD(&queue->head, next);
2631 tor_free(cell);
2633 TOR_SIMPLEQ_INIT(&queue->head);
2634 queue->n = 0;
2637 /** Extract and return the cell at the head of <b>queue</b>; return NULL if
2638 * <b>queue</b> is empty. */
2639 STATIC destroy_cell_t *
2640 destroy_cell_queue_pop(destroy_cell_queue_t *queue)
2642 destroy_cell_t *cell = TOR_SIMPLEQ_FIRST(&queue->head);
2643 if (!cell)
2644 return NULL;
2645 TOR_SIMPLEQ_REMOVE_HEAD(&queue->head, next);
2646 --queue->n;
2647 return cell;
2650 /** Append a destroy cell for <b>circid</b> to <b>queue</b>. */
2651 void
2652 destroy_cell_queue_append(destroy_cell_queue_t *queue,
2653 circid_t circid,
2654 uint8_t reason)
2656 destroy_cell_t *cell = tor_malloc_zero(sizeof(destroy_cell_t));
2657 cell->circid = circid;
2658 cell->reason = reason;
2659 /* Not yet used, but will be required for OOM handling. */
2660 cell->inserted_timestamp = monotime_coarse_get_stamp();
2662 TOR_SIMPLEQ_INSERT_TAIL(&queue->head, cell, next);
2663 ++queue->n;
2666 /** Convert a destroy_cell_t to a newly allocated cell_t. Frees its input. */
2667 static packed_cell_t *
2668 destroy_cell_to_packed_cell(destroy_cell_t *inp, int wide_circ_ids)
2670 packed_cell_t *packed = packed_cell_new();
2671 cell_t cell;
2672 memset(&cell, 0, sizeof(cell));
2673 cell.circ_id = inp->circid;
2674 cell.command = CELL_DESTROY;
2675 cell.payload[0] = inp->reason;
2676 cell_pack(packed, &cell, wide_circ_ids);
2678 tor_free(inp);
2679 return packed;
2682 /** Return the total number of bytes used for each packed_cell in a queue.
2683 * Approximate. */
2684 size_t
2685 packed_cell_mem_cost(void)
2687 return sizeof(packed_cell_t);
2690 /* DOCDOC */
2691 size_t
2692 cell_queues_get_total_allocation(void)
2694 return total_cells_allocated * packed_cell_mem_cost();
2697 /** How long after we've been low on memory should we try to conserve it? */
2698 #define MEMORY_PRESSURE_INTERVAL (30*60)
2700 /** The time at which we were last low on memory. */
2701 static time_t last_time_under_memory_pressure = 0;
2703 /** Check whether we've got too much space used for cells. If so,
2704 * call the OOM handler and return 1. Otherwise, return 0. */
2705 STATIC int
2706 cell_queues_check_size(void)
2708 time_t now = time(NULL);
2709 size_t alloc = cell_queues_get_total_allocation();
2710 alloc += half_streams_get_total_allocation();
2711 alloc += buf_get_total_allocation();
2712 alloc += tor_compress_get_total_allocation();
2713 const size_t hs_cache_total = hs_cache_get_total_allocation();
2714 alloc += hs_cache_total;
2715 const size_t geoip_client_cache_total =
2716 geoip_client_cache_total_allocation();
2717 alloc += geoip_client_cache_total;
2718 const size_t dns_cache_total = dns_cache_total_allocation();
2719 alloc += dns_cache_total;
2720 if (alloc >= get_options()->MaxMemInQueues_low_threshold) {
2721 last_time_under_memory_pressure = approx_time();
2722 if (alloc >= get_options()->MaxMemInQueues) {
2723 /* If we're spending over 20% of the memory limit on hidden service
2724 * descriptors, free them until we're down to 10%. Do the same for geoip
2725 * client cache. */
2726 if (hs_cache_total > get_options()->MaxMemInQueues / 5) {
2727 const size_t bytes_to_remove =
2728 hs_cache_total - (size_t)(get_options()->MaxMemInQueues / 10);
2729 alloc -= hs_cache_handle_oom(now, bytes_to_remove);
2731 if (geoip_client_cache_total > get_options()->MaxMemInQueues / 5) {
2732 const size_t bytes_to_remove =
2733 geoip_client_cache_total -
2734 (size_t)(get_options()->MaxMemInQueues / 10);
2735 alloc -= geoip_client_cache_handle_oom(now, bytes_to_remove);
2737 if (dns_cache_total > get_options()->MaxMemInQueues / 5) {
2738 const size_t bytes_to_remove =
2739 dns_cache_total - (size_t)(get_options()->MaxMemInQueues / 10);
2740 alloc -= dns_cache_handle_oom(now, bytes_to_remove);
2742 circuits_handle_oom(alloc);
2743 return 1;
2746 return 0;
2749 /** Return true if we've been under memory pressure in the last
2750 * MEMORY_PRESSURE_INTERVAL seconds. */
2752 have_been_under_memory_pressure(void)
2754 return last_time_under_memory_pressure + MEMORY_PRESSURE_INTERVAL
2755 < approx_time();
2759 * Update the number of cells available on the circuit's n_chan or p_chan's
2760 * circuit mux.
2762 void
2763 update_circuit_on_cmux_(circuit_t *circ, cell_direction_t direction,
2764 const char *file, int lineno)
2766 channel_t *chan = NULL;
2767 or_circuit_t *or_circ = NULL;
2768 circuitmux_t *cmux = NULL;
2770 tor_assert(circ);
2772 /* Okay, get the channel */
2773 if (direction == CELL_DIRECTION_OUT) {
2774 chan = circ->n_chan;
2775 } else {
2776 or_circ = TO_OR_CIRCUIT(circ);
2777 chan = or_circ->p_chan;
2780 tor_assert(chan);
2781 tor_assert(chan->cmux);
2783 /* Now get the cmux */
2784 cmux = chan->cmux;
2786 /* Cmux sanity check */
2787 if (! circuitmux_is_circuit_attached(cmux, circ)) {
2788 log_warn(LD_BUG, "called on non-attached circuit from %s:%d",
2789 file, lineno);
2790 return;
2792 tor_assert(circuitmux_attached_circuit_direction(cmux, circ) == direction);
2794 /* Update the number of cells we have for the circuit mux */
2795 if (direction == CELL_DIRECTION_OUT) {
2796 circuitmux_set_num_cells(cmux, circ, circ->n_chan_cells.n);
2797 } else {
2798 circuitmux_set_num_cells(cmux, circ, or_circ->p_chan_cells.n);
2802 /** Remove all circuits from the cmux on <b>chan</b>.
2804 * If <b>circuits_out</b> is non-NULL, add all detached circuits to
2805 * <b>circuits_out</b>.
2807 void
2808 channel_unlink_all_circuits(channel_t *chan, smartlist_t *circuits_out)
2810 tor_assert(chan);
2811 tor_assert(chan->cmux);
2813 circuitmux_detach_all_circuits(chan->cmux, circuits_out);
2814 chan->num_n_circuits = 0;
2815 chan->num_p_circuits = 0;
2818 /** Block (if <b>block</b> is true) or unblock (if <b>block</b> is false)
2819 * every edge connection that is using <b>circ</b> to write to <b>chan</b>,
2820 * and start or stop reading as appropriate.
2822 * If <b>stream_id</b> is nonzero, block only the edge connection whose
2823 * stream_id matches it.
2825 * Returns the number of streams whose status we changed.
2827 static int
2828 set_streams_blocked_on_circ(circuit_t *circ, channel_t *chan,
2829 int block, streamid_t stream_id)
2831 edge_connection_t *edge = NULL;
2832 int n = 0;
2833 if (circ->n_chan == chan) {
2834 circ->streams_blocked_on_n_chan = block;
2835 if (CIRCUIT_IS_ORIGIN(circ))
2836 edge = TO_ORIGIN_CIRCUIT(circ)->p_streams;
2837 } else {
2838 circ->streams_blocked_on_p_chan = block;
2839 tor_assert(!CIRCUIT_IS_ORIGIN(circ));
2840 edge = TO_OR_CIRCUIT(circ)->n_streams;
2843 for (; edge; edge = edge->next_stream) {
2844 connection_t *conn = TO_CONN(edge);
2845 if (stream_id && edge->stream_id != stream_id)
2846 continue;
2848 if (edge->edge_blocked_on_circ != block) {
2849 ++n;
2850 edge->edge_blocked_on_circ = block;
2853 if (!conn->read_event) {
2854 /* This connection is a placeholder for something; probably a DNS
2855 * request. It can't actually stop or start reading.*/
2856 continue;
2859 if (block) {
2860 if (connection_is_reading(conn))
2861 connection_stop_reading(conn);
2862 } else {
2863 /* Is this right? */
2864 if (!connection_is_reading(conn))
2865 connection_start_reading(conn);
2869 return n;
2872 /** Extract the command from a packed cell. */
2873 uint8_t
2874 packed_cell_get_command(const packed_cell_t *cell, int wide_circ_ids)
2876 if (wide_circ_ids) {
2877 return get_uint8(cell->body+4);
2878 } else {
2879 return get_uint8(cell->body+2);
2883 /** Extract the circuit ID from a packed cell. */
2884 circid_t
2885 packed_cell_get_circid(const packed_cell_t *cell, int wide_circ_ids)
2887 if (wide_circ_ids) {
2888 return ntohl(get_uint32(cell->body));
2889 } else {
2890 return ntohs(get_uint16(cell->body));
2894 /** Pull as many cells as possible (but no more than <b>max</b>) from the
2895 * queue of the first active circuit on <b>chan</b>, and write them to
2896 * <b>chan</b>-&gt;outbuf. Return the number of cells written. Advance
2897 * the active circuit pointer to the next active circuit in the ring. */
2898 MOCK_IMPL(int,
2899 channel_flush_from_first_active_circuit, (channel_t *chan, int max))
2901 circuitmux_t *cmux = NULL;
2902 int n_flushed = 0;
2903 cell_queue_t *queue;
2904 destroy_cell_queue_t *destroy_queue=NULL;
2905 circuit_t *circ;
2906 or_circuit_t *or_circ;
2907 int streams_blocked;
2908 packed_cell_t *cell;
2910 /* Get the cmux */
2911 tor_assert(chan);
2912 tor_assert(chan->cmux);
2913 cmux = chan->cmux;
2915 /* Main loop: pick a circuit, send a cell, update the cmux */
2916 while (n_flushed < max) {
2917 circ = circuitmux_get_first_active_circuit(cmux, &destroy_queue);
2918 if (destroy_queue) {
2919 destroy_cell_t *dcell;
2920 /* this code is duplicated from some of the logic below. Ugly! XXXX */
2921 /* If we are given a destroy_queue here, then it is required to be
2922 * nonempty... */
2923 tor_assert(destroy_queue->n > 0);
2924 dcell = destroy_cell_queue_pop(destroy_queue);
2925 /* ...and pop() will always yield a cell from a nonempty queue. */
2926 tor_assert(dcell);
2927 /* frees dcell */
2928 cell = destroy_cell_to_packed_cell(dcell, chan->wide_circ_ids);
2929 /* Send the DESTROY cell. It is very unlikely that this fails but just
2930 * in case, get rid of the channel. */
2931 if (channel_write_packed_cell(chan, cell) < 0) {
2932 /* The cell has been freed. */
2933 channel_mark_for_close(chan);
2934 continue;
2936 /* Update the cmux destroy counter */
2937 circuitmux_notify_xmit_destroy(cmux);
2938 cell = NULL;
2939 ++n_flushed;
2940 continue;
2942 /* If it returns NULL, no cells left to send */
2943 if (!circ) break;
2945 if (circ->n_chan == chan) {
2946 queue = &circ->n_chan_cells;
2947 streams_blocked = circ->streams_blocked_on_n_chan;
2948 } else {
2949 or_circ = TO_OR_CIRCUIT(circ);
2950 tor_assert(or_circ->p_chan == chan);
2951 queue = &TO_OR_CIRCUIT(circ)->p_chan_cells;
2952 streams_blocked = circ->streams_blocked_on_p_chan;
2955 /* Circuitmux told us this was active, so it should have cells */
2956 if (/*BUG(*/ queue->n == 0 /*)*/) {
2957 log_warn(LD_BUG, "Found a supposedly active circuit with no cells "
2958 "to send. Trying to recover.");
2959 circuitmux_set_num_cells(cmux, circ, 0);
2960 if (! circ->marked_for_close)
2961 circuit_mark_for_close(circ, END_CIRC_REASON_INTERNAL);
2962 continue;
2965 tor_assert(queue->n > 0);
2968 * Get just one cell here; once we've sent it, that can change the circuit
2969 * selection, so we have to loop around for another even if this circuit
2970 * has more than one.
2972 cell = cell_queue_pop(queue);
2974 /* Calculate the exact time that this cell has spent in the queue. */
2975 if (get_options()->CellStatistics ||
2976 get_options()->TestingEnableCellStatsEvent) {
2977 uint32_t timestamp_now = monotime_coarse_get_stamp();
2978 uint32_t msec_waiting =
2979 (uint32_t) monotime_coarse_stamp_units_to_approx_msec(
2980 timestamp_now - cell->inserted_timestamp);
2982 if (get_options()->CellStatistics && !CIRCUIT_IS_ORIGIN(circ)) {
2983 or_circ = TO_OR_CIRCUIT(circ);
2984 or_circ->total_cell_waiting_time += msec_waiting;
2985 or_circ->processed_cells++;
2988 if (get_options()->TestingEnableCellStatsEvent) {
2989 uint8_t command = packed_cell_get_command(cell, chan->wide_circ_ids);
2991 testing_cell_stats_entry_t *ent =
2992 tor_malloc_zero(sizeof(testing_cell_stats_entry_t));
2993 ent->command = command;
2994 ent->waiting_time = msec_waiting / 10;
2995 ent->removed = 1;
2996 if (circ->n_chan == chan)
2997 ent->exitward = 1;
2998 if (!circ->testing_cell_stats)
2999 circ->testing_cell_stats = smartlist_new();
3000 smartlist_add(circ->testing_cell_stats, ent);
3004 /* If we just flushed our queue and this circuit is used for a
3005 * tunneled directory request, possibly advance its state. */
3006 if (queue->n == 0 && chan->dirreq_id)
3007 geoip_change_dirreq_state(chan->dirreq_id,
3008 DIRREQ_TUNNELED,
3009 DIRREQ_CIRC_QUEUE_FLUSHED);
3011 /* Now send the cell. It is very unlikely that this fails but just in
3012 * case, get rid of the channel. */
3013 if (channel_write_packed_cell(chan, cell) < 0) {
3014 /* The cell has been freed at this point. */
3015 channel_mark_for_close(chan);
3016 continue;
3018 cell = NULL;
3021 * Don't packed_cell_free_unchecked(cell) here because the channel will
3022 * do so when it gets out of the channel queue (probably already did, in
3023 * which case that was an immediate double-free bug).
3026 /* Update the counter */
3027 ++n_flushed;
3030 * Now update the cmux; tell it we've just sent a cell, and how many
3031 * we have left.
3033 circuitmux_notify_xmit_cells(cmux, circ, 1);
3034 circuitmux_set_num_cells(cmux, circ, queue->n);
3035 if (queue->n == 0)
3036 log_debug(LD_GENERAL, "Made a circuit inactive.");
3038 /* Is the cell queue low enough to unblock all the streams that are waiting
3039 * to write to this circuit? */
3040 if (streams_blocked && queue->n <= CELL_QUEUE_LOWWATER_SIZE)
3041 set_streams_blocked_on_circ(circ, chan, 0, 0); /* unblock streams */
3043 /* If n_flushed < max still, loop around and pick another circuit */
3046 /* Okay, we're done sending now */
3047 return n_flushed;
3050 /* Minimum value is the maximum circuit window size.
3052 * SENDME cells makes it that we can control how many cells can be inflight on
3053 * a circuit from end to end. This logic makes it that on any circuit cell
3054 * queue, we have a maximum of cells possible.
3056 * Because the Tor protocol allows for a client to exit at any hop in a
3057 * circuit and a circuit can be of a maximum of 8 hops, so in theory the
3058 * normal worst case will be the circuit window start value times the maximum
3059 * number of hops (8). Having more cells then that means something is wrong.
3061 * However, because padding cells aren't counted in the package window, we set
3062 * the maximum size to a reasonably large size for which we expect that we'll
3063 * never reach in theory. And if we ever do because of future changes, we'll
3064 * be able to control it with a consensus parameter.
3066 * XXX: Unfortunately, END cells aren't accounted for in the circuit window
3067 * which means that for instance if a client opens 8001 streams, the 8001
3068 * following END cells will queue up in the circuit which will get closed if
3069 * the max limit is 8000. Which is sad because it is allowed by the Tor
3070 * protocol. But, we need an upper bound on circuit queue in order to avoid
3071 * DoS memory pressure so the default size is a middle ground between not
3072 * having any limit and having a very restricted one. This is why we can also
3073 * control it through a consensus parameter. */
3074 #define RELAY_CIRC_CELL_QUEUE_SIZE_MIN CIRCWINDOW_START_MAX
3075 /* We can't have a consensus parameter above this value. */
3076 #define RELAY_CIRC_CELL_QUEUE_SIZE_MAX INT32_MAX
3077 /* Default value is set to a large value so we can handle padding cells
3078 * properly which aren't accounted for in the SENDME window. Default is 50000
3079 * allowed cells in the queue resulting in ~25MB. */
3080 #define RELAY_CIRC_CELL_QUEUE_SIZE_DEFAULT \
3081 (50 * RELAY_CIRC_CELL_QUEUE_SIZE_MIN)
3083 /* The maximum number of cell a circuit queue can contain. This is updated at
3084 * every new consensus and controlled by a parameter. */
3085 static int32_t max_circuit_cell_queue_size =
3086 RELAY_CIRC_CELL_QUEUE_SIZE_DEFAULT;
3088 /* Called when the consensus has changed. At this stage, the global consensus
3089 * object has NOT been updated. It is called from
3090 * notify_before_networkstatus_changes(). */
3091 void
3092 relay_consensus_has_changed(const networkstatus_t *ns)
3094 tor_assert(ns);
3096 /* Update the circuit max cell queue size from the consensus. */
3097 max_circuit_cell_queue_size =
3098 networkstatus_get_param(ns, "circ_max_cell_queue_size",
3099 RELAY_CIRC_CELL_QUEUE_SIZE_DEFAULT,
3100 RELAY_CIRC_CELL_QUEUE_SIZE_MIN,
3101 RELAY_CIRC_CELL_QUEUE_SIZE_MAX);
3104 /** Add <b>cell</b> to the queue of <b>circ</b> writing to <b>chan</b>
3105 * transmitting in <b>direction</b>.
3107 * The given <b>cell</b> is copied onto the circuit queue so the caller must
3108 * cleanup the memory.
3110 * This function is part of the fast path. */
3111 void
3112 append_cell_to_circuit_queue(circuit_t *circ, channel_t *chan,
3113 cell_t *cell, cell_direction_t direction,
3114 streamid_t fromstream)
3116 or_circuit_t *orcirc = NULL;
3117 cell_queue_t *queue;
3118 int streams_blocked;
3119 int exitward;
3120 if (circ->marked_for_close)
3121 return;
3123 exitward = (direction == CELL_DIRECTION_OUT);
3124 if (exitward) {
3125 queue = &circ->n_chan_cells;
3126 streams_blocked = circ->streams_blocked_on_n_chan;
3127 } else {
3128 orcirc = TO_OR_CIRCUIT(circ);
3129 queue = &orcirc->p_chan_cells;
3130 streams_blocked = circ->streams_blocked_on_p_chan;
3133 if (PREDICT_UNLIKELY(queue->n >= max_circuit_cell_queue_size)) {
3134 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
3135 "%s circuit has %d cells in its queue, maximum allowed is %d. "
3136 "Closing circuit for safety reasons.",
3137 (exitward) ? "Outbound" : "Inbound", queue->n,
3138 max_circuit_cell_queue_size);
3139 circuit_mark_for_close(circ, END_CIRC_REASON_RESOURCELIMIT);
3140 stats_n_circ_max_cell_reached++;
3141 return;
3144 /* Very important that we copy to the circuit queue because all calls to
3145 * this function use the stack for the cell memory. */
3146 cell_queue_append_packed_copy(circ, queue, exitward, cell,
3147 chan->wide_circ_ids, 1);
3149 /* Check and run the OOM if needed. */
3150 if (PREDICT_UNLIKELY(cell_queues_check_size())) {
3151 /* We ran the OOM handler which might have closed this circuit. */
3152 if (circ->marked_for_close)
3153 return;
3156 /* If we have too many cells on the circuit, we should stop reading from
3157 * the edge streams for a while. */
3158 if (!streams_blocked && queue->n >= CELL_QUEUE_HIGHWATER_SIZE)
3159 set_streams_blocked_on_circ(circ, chan, 1, 0); /* block streams */
3161 if (streams_blocked && fromstream) {
3162 /* This edge connection is apparently not blocked; block it. */
3163 set_streams_blocked_on_circ(circ, chan, 1, fromstream);
3166 update_circuit_on_cmux(circ, direction);
3167 if (queue->n == 1) {
3168 /* This was the first cell added to the queue. We just made this
3169 * circuit active. */
3170 log_debug(LD_GENERAL, "Made a circuit active.");
3173 /* New way: mark this as having waiting cells for the scheduler */
3174 scheduler_channel_has_waiting_cells(chan);
3177 /** Append an encoded value of <b>addr</b> to <b>payload_out</b>, which must
3178 * have at least 18 bytes of free space. The encoding is, as specified in
3179 * tor-spec.txt:
3180 * RESOLVED_TYPE_IPV4 or RESOLVED_TYPE_IPV6 [1 byte]
3181 * LENGTH [1 byte]
3182 * ADDRESS [length bytes]
3183 * Return the number of bytes added, or -1 on error */
3185 append_address_to_payload(uint8_t *payload_out, const tor_addr_t *addr)
3187 uint32_t a;
3188 switch (tor_addr_family(addr)) {
3189 case AF_INET:
3190 payload_out[0] = RESOLVED_TYPE_IPV4;
3191 payload_out[1] = 4;
3192 a = tor_addr_to_ipv4n(addr);
3193 memcpy(payload_out+2, &a, 4);
3194 return 6;
3195 case AF_INET6:
3196 payload_out[0] = RESOLVED_TYPE_IPV6;
3197 payload_out[1] = 16;
3198 memcpy(payload_out+2, tor_addr_to_in6_addr8(addr), 16);
3199 return 18;
3200 case AF_UNSPEC:
3201 default:
3202 return -1;
3206 /** Given <b>payload_len</b> bytes at <b>payload</b>, starting with an address
3207 * encoded as by append_address_to_payload(), try to decode the address into
3208 * *<b>addr_out</b>. Return the next byte in the payload after the address on
3209 * success, or NULL on failure. */
3210 const uint8_t *
3211 decode_address_from_payload(tor_addr_t *addr_out, const uint8_t *payload,
3212 int payload_len)
3214 if (payload_len < 2)
3215 return NULL;
3216 if (payload_len < 2+payload[1])
3217 return NULL;
3219 switch (payload[0]) {
3220 case RESOLVED_TYPE_IPV4:
3221 if (payload[1] != 4)
3222 return NULL;
3223 tor_addr_from_ipv4n(addr_out, get_uint32(payload+2));
3224 break;
3225 case RESOLVED_TYPE_IPV6:
3226 if (payload[1] != 16)
3227 return NULL;
3228 tor_addr_from_ipv6_bytes(addr_out, (payload+2));
3229 break;
3230 default:
3231 tor_addr_make_unspec(addr_out);
3232 break;
3234 return payload + 2 + payload[1];
3237 /** Remove all the cells queued on <b>circ</b> for <b>chan</b>. */
3238 void
3239 circuit_clear_cell_queue(circuit_t *circ, channel_t *chan)
3241 cell_queue_t *queue;
3242 cell_direction_t direction;
3244 if (circ->n_chan == chan) {
3245 queue = &circ->n_chan_cells;
3246 direction = CELL_DIRECTION_OUT;
3247 } else {
3248 or_circuit_t *orcirc = TO_OR_CIRCUIT(circ);
3249 tor_assert(orcirc->p_chan == chan);
3250 queue = &orcirc->p_chan_cells;
3251 direction = CELL_DIRECTION_IN;
3254 /* Clear the queue */
3255 cell_queue_clear(queue);
3257 /* Update the cell counter in the cmux */
3258 if (chan->cmux && circuitmux_is_circuit_attached(chan->cmux, circ))
3259 update_circuit_on_cmux(circ, direction);
3262 /** Return 1 if we shouldn't restart reading on this circuit, even if
3263 * we get a SENDME. Else return 0.
3265 static int
3266 circuit_queue_streams_are_blocked(circuit_t *circ)
3268 if (CIRCUIT_IS_ORIGIN(circ)) {
3269 return circ->streams_blocked_on_n_chan;
3270 } else {
3271 return circ->streams_blocked_on_p_chan;