Merge remote-tracking branch 'tor-github/pr/998'
[tor.git] / src / core / or / relay.c
blob8b3a1be18eb13a65b5758c85bcbdf18b014bcb8e
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-2019, 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 "lib/compress/compress.h"
60 #include "app/config/config.h"
61 #include "core/mainloop/connection.h"
62 #include "core/or/connection_edge.h"
63 #include "core/or/connection_or.h"
64 #include "feature/control/control_events.h"
65 #include "lib/crypt_ops/crypto_rand.h"
66 #include "lib/crypt_ops/crypto_util.h"
67 #include "feature/dircommon/directory.h"
68 #include "feature/relay/dns.h"
69 #include "feature/stats/geoip_stats.h"
70 #include "feature/hs/hs_cache.h"
71 #include "core/mainloop/mainloop.h"
72 #include "feature/nodelist/networkstatus.h"
73 #include "feature/nodelist/nodelist.h"
74 #include "core/or/onion.h"
75 #include "core/or/policies.h"
76 #include "core/or/reasons.h"
77 #include "core/or/relay.h"
78 #include "core/crypto/relay_crypto.h"
79 #include "feature/rend/rendcache.h"
80 #include "feature/rend/rendcommon.h"
81 #include "feature/nodelist/describe.h"
82 #include "feature/nodelist/routerlist.h"
83 #include "core/or/scheduler.h"
85 #include "core/or/cell_st.h"
86 #include "core/or/cell_queue_st.h"
87 #include "core/or/cpath_build_state_st.h"
88 #include "feature/dircommon/dir_connection_st.h"
89 #include "core/or/destroy_cell_queue_st.h"
90 #include "core/or/entry_connection_st.h"
91 #include "core/or/extend_info_st.h"
92 #include "core/or/or_circuit_st.h"
93 #include "core/or/origin_circuit_st.h"
94 #include "feature/nodelist/routerinfo_st.h"
95 #include "core/or/socks_request_st.h"
96 #include "core/or/sendme.h"
98 static edge_connection_t *relay_lookup_conn(circuit_t *circ, cell_t *cell,
99 cell_direction_t cell_direction,
100 crypt_path_t *layer_hint);
102 static void circuit_resume_edge_reading(circuit_t *circ,
103 crypt_path_t *layer_hint);
104 static int circuit_resume_edge_reading_helper(edge_connection_t *conn,
105 circuit_t *circ,
106 crypt_path_t *layer_hint);
107 static int circuit_consider_stop_edge_reading(circuit_t *circ,
108 crypt_path_t *layer_hint);
109 static int circuit_queue_streams_are_blocked(circuit_t *circ);
110 static void adjust_exit_policy_from_exitpolicy_failure(origin_circuit_t *circ,
111 entry_connection_t *conn,
112 node_t *node,
113 const tor_addr_t *addr);
115 /** Stop reading on edge connections when we have this many cells
116 * waiting on the appropriate queue. */
117 #define CELL_QUEUE_HIGHWATER_SIZE 256
118 /** Start reading from edge connections again when we get down to this many
119 * cells. */
120 #define CELL_QUEUE_LOWWATER_SIZE 64
122 /** Stats: how many relay cells have originated at this hop, or have
123 * been relayed onward (not recognized at this hop)?
125 uint64_t stats_n_relay_cells_relayed = 0;
126 /** Stats: how many relay cells have been delivered to streams at this
127 * hop?
129 uint64_t stats_n_relay_cells_delivered = 0;
130 /** Stats: how many circuits have we closed due to the cell queue limit being
131 * reached (see append_cell_to_circuit_queue()) */
132 uint64_t stats_n_circ_max_cell_reached = 0;
135 * Update channel usage state based on the type of relay cell and
136 * circuit properties.
138 * This is needed to determine if a client channel is being
139 * used for application traffic, and if a relay channel is being
140 * used for multihop circuits and application traffic. The decision
141 * to pad in channelpadding.c depends upon this info (as well as
142 * consensus parameters) to decide what channels to pad.
144 static void
145 circuit_update_channel_usage(circuit_t *circ, cell_t *cell)
147 if (CIRCUIT_IS_ORIGIN(circ)) {
149 * The client state was first set much earlier in
150 * circuit_send_next_onion_skin(), so we can start padding as early as
151 * possible.
153 * However, if padding turns out to be expensive, we may want to not do
154 * it until actual application traffic starts flowing (which is controlled
155 * via consensus param nf_pad_before_usage).
157 * So: If we're an origin circuit and we've created a full length circuit,
158 * then any CELL_RELAY cell means application data. Increase the usage
159 * state of the channel to indicate this.
161 * We want to wait for CELL_RELAY specifically here, so we know that
162 * the channel was definitely being used for data and not for extends.
163 * By default, we pad as soon as a channel has been used for *any*
164 * circuits, so this state is irrelevant to the padding decision in
165 * the default case. However, if padding turns out to be expensive,
166 * we would like the ability to avoid padding until we're absolutely
167 * sure that a channel is used for enough application data to be worth
168 * padding.
170 * (So it does not matter that CELL_RELAY_EARLY can actually contain
171 * application data. This is only a load reducing option and that edge
172 * case does not matter if we're desperately trying to reduce overhead
173 * anyway. See also consensus parameter nf_pad_before_usage).
175 if (BUG(!circ->n_chan))
176 return;
178 if (circ->n_chan->channel_usage == CHANNEL_USED_FOR_FULL_CIRCS &&
179 cell->command == CELL_RELAY) {
180 circ->n_chan->channel_usage = CHANNEL_USED_FOR_USER_TRAFFIC;
182 } else {
183 /* If we're a relay circuit, the question is more complicated. Basically:
184 * we only want to pad connections that carry multihop (anonymous)
185 * circuits.
187 * We assume we're more than one hop if either the previous hop
188 * is not a client, or if the previous hop is a client and there's
189 * a next hop. Then, circuit traffic starts at RELAY_EARLY, and
190 * user application traffic starts when we see RELAY cells.
192 or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
194 if (BUG(!or_circ->p_chan))
195 return;
197 if (!channel_is_client(or_circ->p_chan) ||
198 (channel_is_client(or_circ->p_chan) && circ->n_chan)) {
199 if (cell->command == CELL_RELAY_EARLY) {
200 if (or_circ->p_chan->channel_usage < CHANNEL_USED_FOR_FULL_CIRCS) {
201 or_circ->p_chan->channel_usage = CHANNEL_USED_FOR_FULL_CIRCS;
203 } else if (cell->command == CELL_RELAY) {
204 or_circ->p_chan->channel_usage = CHANNEL_USED_FOR_USER_TRAFFIC;
210 /** Receive a relay cell:
211 * - Crypt it (encrypt if headed toward the origin or if we <b>are</b> the
212 * origin; decrypt if we're headed toward the exit).
213 * - Check if recognized (if exitward).
214 * - If recognized and the digest checks out, then find if there's a stream
215 * that the cell is intended for, and deliver it to the right
216 * connection_edge.
217 * - If not recognized, then we need to relay it: append it to the appropriate
218 * cell_queue on <b>circ</b>.
220 * Return -<b>reason</b> on failure.
223 circuit_receive_relay_cell(cell_t *cell, circuit_t *circ,
224 cell_direction_t cell_direction)
226 channel_t *chan = NULL;
227 crypt_path_t *layer_hint=NULL;
228 char recognized=0;
229 int reason;
231 tor_assert(cell);
232 tor_assert(circ);
233 tor_assert(cell_direction == CELL_DIRECTION_OUT ||
234 cell_direction == CELL_DIRECTION_IN);
235 if (circ->marked_for_close)
236 return 0;
238 if (relay_decrypt_cell(circ, cell, cell_direction, &layer_hint, &recognized)
239 < 0) {
240 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
241 "relay crypt failed. Dropping connection.");
242 return -END_CIRC_REASON_INTERNAL;
245 circuit_update_channel_usage(circ, cell);
247 if (recognized) {
248 edge_connection_t *conn = NULL;
250 if (circ->purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING) {
251 if (pathbias_check_probe_response(circ, cell) == -1) {
252 pathbias_count_valid_cells(circ, cell);
255 /* We need to drop this cell no matter what to avoid code that expects
256 * a certain purpose (such as the hidserv code). */
257 return 0;
260 conn = relay_lookup_conn(circ, cell, cell_direction, layer_hint);
261 if (cell_direction == CELL_DIRECTION_OUT) {
262 ++stats_n_relay_cells_delivered;
263 log_debug(LD_OR,"Sending away from origin.");
264 if ((reason=connection_edge_process_relay_cell(cell, circ, conn, NULL))
265 < 0) {
266 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
267 "connection_edge_process_relay_cell (away from origin) "
268 "failed.");
269 return reason;
272 if (cell_direction == CELL_DIRECTION_IN) {
273 ++stats_n_relay_cells_delivered;
274 log_debug(LD_OR,"Sending to origin.");
275 if ((reason = connection_edge_process_relay_cell(cell, circ, conn,
276 layer_hint)) < 0) {
277 /* If a client is trying to connect to unknown hidden service port,
278 * END_CIRC_AT_ORIGIN is sent back so we can then close the circuit.
279 * Do not log warn as this is an expected behavior for a service. */
280 if (reason != END_CIRC_AT_ORIGIN) {
281 log_warn(LD_OR,
282 "connection_edge_process_relay_cell (at origin) failed.");
284 return reason;
287 return 0;
290 /* not recognized. inform circpad and pass it on. */
291 circpad_deliver_unrecognized_cell_events(circ, cell_direction);
293 if (cell_direction == CELL_DIRECTION_OUT) {
294 cell->circ_id = circ->n_circ_id; /* switch it */
295 chan = circ->n_chan;
296 } else if (! CIRCUIT_IS_ORIGIN(circ)) {
297 cell->circ_id = TO_OR_CIRCUIT(circ)->p_circ_id; /* switch it */
298 chan = TO_OR_CIRCUIT(circ)->p_chan;
299 } else {
300 log_fn(LOG_PROTOCOL_WARN, LD_OR,
301 "Dropping unrecognized inbound cell on origin circuit.");
302 /* If we see unrecognized cells on path bias testing circs,
303 * it's bad mojo. Those circuits need to die.
304 * XXX: Shouldn't they always die? */
305 if (circ->purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING) {
306 TO_ORIGIN_CIRCUIT(circ)->path_state = PATH_STATE_USE_FAILED;
307 return -END_CIRC_REASON_TORPROTOCOL;
308 } else {
309 return 0;
313 if (!chan) {
314 // XXXX Can this splice stuff be done more cleanly?
315 if (! CIRCUIT_IS_ORIGIN(circ) &&
316 TO_OR_CIRCUIT(circ)->rend_splice &&
317 cell_direction == CELL_DIRECTION_OUT) {
318 or_circuit_t *splice_ = TO_OR_CIRCUIT(circ)->rend_splice;
319 tor_assert(circ->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED);
320 tor_assert(splice_->base_.purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED);
321 cell->circ_id = splice_->p_circ_id;
322 cell->command = CELL_RELAY; /* can't be relay_early anyway */
323 if ((reason = circuit_receive_relay_cell(cell, TO_CIRCUIT(splice_),
324 CELL_DIRECTION_IN)) < 0) {
325 log_warn(LD_REND, "Error relaying cell across rendezvous; closing "
326 "circuits");
327 /* XXXX Do this here, or just return -1? */
328 circuit_mark_for_close(circ, -reason);
329 return reason;
331 return 0;
333 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
334 "Didn't recognize cell, but circ stops here! Closing circ.");
335 return -END_CIRC_REASON_TORPROTOCOL;
338 log_debug(LD_OR,"Passing on unrecognized cell.");
340 ++stats_n_relay_cells_relayed; /* XXXX no longer quite accurate {cells}
341 * we might kill the circ before we relay
342 * the cells. */
344 append_cell_to_circuit_queue(circ, chan, cell, cell_direction, 0);
345 return 0;
348 /** Package a relay cell from an edge:
349 * - Encrypt it to the right layer
350 * - Append it to the appropriate cell_queue on <b>circ</b>.
352 MOCK_IMPL(int,
353 circuit_package_relay_cell, (cell_t *cell, circuit_t *circ,
354 cell_direction_t cell_direction,
355 crypt_path_t *layer_hint, streamid_t on_stream,
356 const char *filename, int lineno))
358 channel_t *chan; /* where to send the cell */
360 if (circ->marked_for_close) {
361 /* Circuit is marked; send nothing. */
362 return 0;
365 if (cell_direction == CELL_DIRECTION_OUT) {
366 chan = circ->n_chan;
367 if (!chan) {
368 log_warn(LD_BUG,"outgoing relay cell sent from %s:%d has n_chan==NULL."
369 " Dropping. Circuit is in state %s (%d), and is "
370 "%smarked for close. (%s:%d, %d)", filename, lineno,
371 circuit_state_to_string(circ->state), circ->state,
372 circ->marked_for_close ? "" : "not ",
373 circ->marked_for_close_file?circ->marked_for_close_file:"",
374 circ->marked_for_close, circ->marked_for_close_reason);
375 if (CIRCUIT_IS_ORIGIN(circ)) {
376 circuit_log_path(LOG_WARN, LD_BUG, TO_ORIGIN_CIRCUIT(circ));
378 log_backtrace(LOG_WARN,LD_BUG,"");
379 return 0; /* just drop it */
381 if (!CIRCUIT_IS_ORIGIN(circ)) {
382 log_warn(LD_BUG,"outgoing relay cell sent from %s:%d on non-origin "
383 "circ. Dropping.", filename, lineno);
384 log_backtrace(LOG_WARN,LD_BUG,"");
385 return 0; /* just drop it */
388 relay_encrypt_cell_outbound(cell, TO_ORIGIN_CIRCUIT(circ), layer_hint);
390 /* Update circ written totals for control port */
391 origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
392 ocirc->n_written_circ_bw = tor_add_u32_nowrap(ocirc->n_written_circ_bw,
393 CELL_PAYLOAD_SIZE);
395 } else { /* incoming cell */
396 if (CIRCUIT_IS_ORIGIN(circ)) {
397 /* We should never package an _incoming_ cell from the circuit
398 * origin; that means we messed up somewhere. */
399 log_warn(LD_BUG,"incoming relay cell at origin circuit. Dropping.");
400 assert_circuit_ok(circ);
401 return 0; /* just drop it */
403 or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
404 relay_encrypt_cell_inbound(cell, or_circ);
405 chan = or_circ->p_chan;
407 ++stats_n_relay_cells_relayed;
409 append_cell_to_circuit_queue(circ, chan, cell, cell_direction, on_stream);
410 return 0;
413 /** If cell's stream_id matches the stream_id of any conn that's
414 * attached to circ, return that conn, else return NULL.
416 static edge_connection_t *
417 relay_lookup_conn(circuit_t *circ, cell_t *cell,
418 cell_direction_t cell_direction, crypt_path_t *layer_hint)
420 edge_connection_t *tmpconn;
421 relay_header_t rh;
423 relay_header_unpack(&rh, cell->payload);
425 if (!rh.stream_id)
426 return NULL;
428 /* IN or OUT cells could have come from either direction, now
429 * that we allow rendezvous *to* an OP.
432 if (CIRCUIT_IS_ORIGIN(circ)) {
433 for (tmpconn = TO_ORIGIN_CIRCUIT(circ)->p_streams; tmpconn;
434 tmpconn=tmpconn->next_stream) {
435 if (rh.stream_id == tmpconn->stream_id &&
436 !tmpconn->base_.marked_for_close &&
437 tmpconn->cpath_layer == layer_hint) {
438 log_debug(LD_APP,"found conn for stream %d.", rh.stream_id);
439 return tmpconn;
442 } else {
443 for (tmpconn = TO_OR_CIRCUIT(circ)->n_streams; tmpconn;
444 tmpconn=tmpconn->next_stream) {
445 if (rh.stream_id == tmpconn->stream_id &&
446 !tmpconn->base_.marked_for_close) {
447 log_debug(LD_EXIT,"found conn for stream %d.", rh.stream_id);
448 if (cell_direction == CELL_DIRECTION_OUT ||
449 connection_edge_is_rendezvous_stream(tmpconn))
450 return tmpconn;
453 for (tmpconn = TO_OR_CIRCUIT(circ)->resolving_streams; tmpconn;
454 tmpconn=tmpconn->next_stream) {
455 if (rh.stream_id == tmpconn->stream_id &&
456 !tmpconn->base_.marked_for_close) {
457 log_debug(LD_EXIT,"found conn for stream %d.", rh.stream_id);
458 return tmpconn;
462 return NULL; /* probably a begin relay cell */
465 /** Pack the relay_header_t host-order structure <b>src</b> into
466 * network-order in the buffer <b>dest</b>. See tor-spec.txt for details
467 * about the wire format.
469 void
470 relay_header_pack(uint8_t *dest, const relay_header_t *src)
472 set_uint8(dest, src->command);
473 set_uint16(dest+1, htons(src->recognized));
474 set_uint16(dest+3, htons(src->stream_id));
475 memcpy(dest+5, src->integrity, 4);
476 set_uint16(dest+9, htons(src->length));
479 /** Unpack the network-order buffer <b>src</b> into a host-order
480 * relay_header_t structure <b>dest</b>.
482 void
483 relay_header_unpack(relay_header_t *dest, const uint8_t *src)
485 dest->command = get_uint8(src);
486 dest->recognized = ntohs(get_uint16(src+1));
487 dest->stream_id = ntohs(get_uint16(src+3));
488 memcpy(dest->integrity, src+5, 4);
489 dest->length = ntohs(get_uint16(src+9));
492 /** Convert the relay <b>command</b> into a human-readable string. */
493 static const char *
494 relay_command_to_string(uint8_t command)
496 static char buf[64];
497 switch (command) {
498 case RELAY_COMMAND_BEGIN: return "BEGIN";
499 case RELAY_COMMAND_DATA: return "DATA";
500 case RELAY_COMMAND_END: return "END";
501 case RELAY_COMMAND_CONNECTED: return "CONNECTED";
502 case RELAY_COMMAND_SENDME: return "SENDME";
503 case RELAY_COMMAND_EXTEND: return "EXTEND";
504 case RELAY_COMMAND_EXTENDED: return "EXTENDED";
505 case RELAY_COMMAND_TRUNCATE: return "TRUNCATE";
506 case RELAY_COMMAND_TRUNCATED: return "TRUNCATED";
507 case RELAY_COMMAND_DROP: return "DROP";
508 case RELAY_COMMAND_RESOLVE: return "RESOLVE";
509 case RELAY_COMMAND_RESOLVED: return "RESOLVED";
510 case RELAY_COMMAND_BEGIN_DIR: return "BEGIN_DIR";
511 case RELAY_COMMAND_ESTABLISH_INTRO: return "ESTABLISH_INTRO";
512 case RELAY_COMMAND_ESTABLISH_RENDEZVOUS: return "ESTABLISH_RENDEZVOUS";
513 case RELAY_COMMAND_INTRODUCE1: return "INTRODUCE1";
514 case RELAY_COMMAND_INTRODUCE2: return "INTRODUCE2";
515 case RELAY_COMMAND_RENDEZVOUS1: return "RENDEZVOUS1";
516 case RELAY_COMMAND_RENDEZVOUS2: return "RENDEZVOUS2";
517 case RELAY_COMMAND_INTRO_ESTABLISHED: return "INTRO_ESTABLISHED";
518 case RELAY_COMMAND_RENDEZVOUS_ESTABLISHED:
519 return "RENDEZVOUS_ESTABLISHED";
520 case RELAY_COMMAND_INTRODUCE_ACK: return "INTRODUCE_ACK";
521 case RELAY_COMMAND_EXTEND2: return "EXTEND2";
522 case RELAY_COMMAND_EXTENDED2: return "EXTENDED2";
523 case RELAY_COMMAND_PADDING_NEGOTIATE: return "PADDING_NEGOTIATE";
524 case RELAY_COMMAND_PADDING_NEGOTIATED: return "PADDING_NEGOTIATED";
525 default:
526 tor_snprintf(buf, sizeof(buf), "Unrecognized relay command %u",
527 (unsigned)command);
528 return buf;
532 /** Return the offset where the padding should start. The <b>data_len</b> is
533 * the relay payload length expected to be put in the cell. It can not be
534 * bigger than RELAY_PAYLOAD_SIZE else this function assert().
536 * Value will always be smaller than CELL_PAYLOAD_SIZE because this offset is
537 * for the entire cell length not just the data payload length. Zero is
538 * returned if there is no room for padding.
540 * This function always skips the first 4 bytes after the payload because
541 * having some unused zero bytes has saved us a lot of times in the past. */
543 STATIC size_t
544 get_pad_cell_offset(size_t data_len)
546 /* This is never suppose to happen but in case it does, stop right away
547 * because if tor is tricked somehow into not adding random bytes to the
548 * payload with this function returning 0 for a bad data_len, the entire
549 * authenticated SENDME design can be bypassed leading to bad denial of
550 * service attacks. */
551 tor_assert(data_len <= RELAY_PAYLOAD_SIZE);
553 /* If the offset is larger than the cell payload size, we return an offset
554 * of zero indicating that no padding needs to be added. */
555 size_t offset = RELAY_HEADER_SIZE + data_len + 4;
556 if (offset >= CELL_PAYLOAD_SIZE) {
557 return 0;
559 return offset;
562 /* Add random bytes to the unused portion of the payload, to foil attacks
563 * where the other side can predict all of the bytes in the payload and thus
564 * compute the authenticated SENDME cells without seeing the traffic. See
565 * proposal 289. */
566 static void
567 pad_cell_payload(uint8_t *cell_payload, size_t data_len)
569 size_t pad_offset, pad_len;
571 tor_assert(cell_payload);
573 pad_offset = get_pad_cell_offset(data_len);
574 if (pad_offset == 0) {
575 /* We can't add padding so we are done. */
576 return;
579 /* Remember here that the cell_payload is the length of the header and
580 * payload size so we offset it using the full lenght of the cell. */
581 pad_len = CELL_PAYLOAD_SIZE - pad_offset;
582 crypto_fast_rng_getbytes(get_thread_fast_rng(),
583 cell_payload + pad_offset, pad_len);
586 /** Make a relay cell out of <b>relay_command</b> and <b>payload</b>, and send
587 * it onto the open circuit <b>circ</b>. <b>stream_id</b> is the ID on
588 * <b>circ</b> for the stream that's sending the relay cell, or 0 if it's a
589 * control cell. <b>cpath_layer</b> is NULL for OR->OP cells, or the
590 * destination hop for OP->OR cells.
592 * If you can't send the cell, mark the circuit for close and return -1. Else
593 * return 0.
595 MOCK_IMPL(int,
596 relay_send_command_from_edge_,(streamid_t stream_id, circuit_t *circ,
597 uint8_t relay_command, const char *payload,
598 size_t payload_len, crypt_path_t *cpath_layer,
599 const char *filename, int lineno))
601 cell_t cell;
602 relay_header_t rh;
603 cell_direction_t cell_direction;
604 /* XXXX NM Split this function into a separate versions per circuit type? */
606 tor_assert(circ);
607 tor_assert(payload_len <= RELAY_PAYLOAD_SIZE);
609 memset(&cell, 0, sizeof(cell_t));
610 cell.command = CELL_RELAY;
611 if (CIRCUIT_IS_ORIGIN(circ)) {
612 tor_assert(cpath_layer);
613 cell.circ_id = circ->n_circ_id;
614 cell_direction = CELL_DIRECTION_OUT;
615 } else {
616 tor_assert(! cpath_layer);
617 cell.circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
618 cell_direction = CELL_DIRECTION_IN;
621 memset(&rh, 0, sizeof(rh));
622 rh.command = relay_command;
623 rh.stream_id = stream_id;
624 rh.length = payload_len;
625 relay_header_pack(cell.payload, &rh);
626 if (payload_len)
627 memcpy(cell.payload+RELAY_HEADER_SIZE, payload, payload_len);
629 /* Add random padding to the cell if we can. */
630 pad_cell_payload(cell.payload, payload_len);
632 log_debug(LD_OR,"delivering %d cell %s.", relay_command,
633 cell_direction == CELL_DIRECTION_OUT ? "forward" : "backward");
635 /* Tell circpad we're sending a relay cell */
636 circpad_deliver_sent_relay_cell_events(circ, relay_command);
638 /* If we are sending an END cell and this circuit is used for a tunneled
639 * directory request, advance its state. */
640 if (relay_command == RELAY_COMMAND_END && circ->dirreq_id)
641 geoip_change_dirreq_state(circ->dirreq_id, DIRREQ_TUNNELED,
642 DIRREQ_END_CELL_SENT);
644 if (cell_direction == CELL_DIRECTION_OUT && circ->n_chan) {
645 /* if we're using relaybandwidthrate, this conn wants priority */
646 channel_timestamp_client(circ->n_chan);
649 if (cell_direction == CELL_DIRECTION_OUT) {
650 origin_circuit_t *origin_circ = TO_ORIGIN_CIRCUIT(circ);
651 if (origin_circ->remaining_relay_early_cells > 0 &&
652 (relay_command == RELAY_COMMAND_EXTEND ||
653 relay_command == RELAY_COMMAND_EXTEND2 ||
654 cpath_layer != origin_circ->cpath)) {
655 /* If we've got any relay_early cells left and (we're sending
656 * an extend cell or we're not talking to the first hop), use
657 * one of them. Don't worry about the conn protocol version:
658 * append_cell_to_circuit_queue will fix it up. */
659 cell.command = CELL_RELAY_EARLY;
660 /* If we're out of relay early cells, tell circpad */
661 if (--origin_circ->remaining_relay_early_cells == 0)
662 circpad_machine_event_circ_has_no_relay_early(origin_circ);
663 log_debug(LD_OR, "Sending a RELAY_EARLY cell; %d remaining.",
664 (int)origin_circ->remaining_relay_early_cells);
665 /* Memorize the command that is sent as RELAY_EARLY cell; helps debug
666 * task 878. */
667 origin_circ->relay_early_commands[
668 origin_circ->relay_early_cells_sent++] = relay_command;
669 } else if (relay_command == RELAY_COMMAND_EXTEND ||
670 relay_command == RELAY_COMMAND_EXTEND2) {
671 /* If no RELAY_EARLY cells can be sent over this circuit, log which
672 * commands have been sent as RELAY_EARLY cells before; helps debug
673 * task 878. */
674 smartlist_t *commands_list = smartlist_new();
675 int i = 0;
676 char *commands = NULL;
677 for (; i < origin_circ->relay_early_cells_sent; i++)
678 smartlist_add(commands_list, (char *)
679 relay_command_to_string(origin_circ->relay_early_commands[i]));
680 commands = smartlist_join_strings(commands_list, ",", 0, NULL);
681 log_warn(LD_BUG, "Uh-oh. We're sending a RELAY_COMMAND_EXTEND cell, "
682 "but we have run out of RELAY_EARLY cells on that circuit. "
683 "Commands sent before: %s", commands);
684 tor_free(commands);
685 smartlist_free(commands_list);
688 /* Let's assume we're well-behaved: Anything that we decide to send is
689 * valid, delivered data. */
690 circuit_sent_valid_data(origin_circ, rh.length);
693 if (circuit_package_relay_cell(&cell, circ, cell_direction, cpath_layer,
694 stream_id, filename, lineno) < 0) {
695 log_warn(LD_BUG,"circuit_package_relay_cell failed. Closing.");
696 circuit_mark_for_close(circ, END_CIRC_REASON_INTERNAL);
697 return -1;
700 /* If applicable, note the cell digest for the SENDME version 1 purpose if
701 * we need to. This call needs to be after the circuit_package_relay_cell()
702 * because the cell digest is set within that function. */
703 if (relay_command == RELAY_COMMAND_DATA) {
704 sendme_record_cell_digest(circ);
707 return 0;
710 /** Make a relay cell out of <b>relay_command</b> and <b>payload</b>, and
711 * send it onto the open circuit <b>circ</b>. <b>fromconn</b> is the stream
712 * that's sending the relay cell, or NULL if it's a control cell.
713 * <b>cpath_layer</b> is NULL for OR->OP cells, or the destination hop
714 * for OP->OR cells.
716 * If you can't send the cell, mark the circuit for close and
717 * return -1. Else return 0.
720 connection_edge_send_command(edge_connection_t *fromconn,
721 uint8_t relay_command, const char *payload,
722 size_t payload_len)
724 /* XXXX NM Split this function into a separate versions per circuit type? */
725 circuit_t *circ;
726 crypt_path_t *cpath_layer = fromconn->cpath_layer;
727 tor_assert(fromconn);
728 circ = fromconn->on_circuit;
730 if (fromconn->base_.marked_for_close) {
731 log_warn(LD_BUG,
732 "called on conn that's already marked for close at %s:%d.",
733 fromconn->base_.marked_for_close_file,
734 fromconn->base_.marked_for_close);
735 return 0;
738 if (!circ) {
739 if (fromconn->base_.type == CONN_TYPE_AP) {
740 log_info(LD_APP,"no circ. Closing conn.");
741 connection_mark_unattached_ap(EDGE_TO_ENTRY_CONN(fromconn),
742 END_STREAM_REASON_INTERNAL);
743 } else {
744 log_info(LD_EXIT,"no circ. Closing conn.");
745 fromconn->edge_has_sent_end = 1; /* no circ to send to */
746 fromconn->end_reason = END_STREAM_REASON_INTERNAL;
747 connection_mark_for_close(TO_CONN(fromconn));
749 return -1;
752 if (circ->marked_for_close) {
753 /* The circuit has been marked, but not freed yet. When it's freed, it
754 * will mark this connection for close. */
755 return -1;
758 #ifdef MEASUREMENTS_21206
759 /* Keep track of the number of RELAY_DATA cells sent for directory
760 * connections. */
761 connection_t *linked_conn = TO_CONN(fromconn)->linked_conn;
763 if (linked_conn && linked_conn->type == CONN_TYPE_DIR) {
764 ++(TO_DIR_CONN(linked_conn)->data_cells_sent);
766 #endif /* defined(MEASUREMENTS_21206) */
768 return relay_send_command_from_edge(fromconn->stream_id, circ,
769 relay_command, payload,
770 payload_len, cpath_layer);
773 /** How many times will I retry a stream that fails due to DNS
774 * resolve failure or misc error?
776 #define MAX_RESOLVE_FAILURES 3
778 /** Return 1 if reason is something that you should retry if you
779 * get the end cell before you've connected; else return 0. */
780 static int
781 edge_reason_is_retriable(int reason)
783 return reason == END_STREAM_REASON_HIBERNATING ||
784 reason == END_STREAM_REASON_RESOURCELIMIT ||
785 reason == END_STREAM_REASON_EXITPOLICY ||
786 reason == END_STREAM_REASON_RESOLVEFAILED ||
787 reason == END_STREAM_REASON_MISC ||
788 reason == END_STREAM_REASON_NOROUTE;
791 /** Called when we receive an END cell on a stream that isn't open yet,
792 * from the client side.
793 * Arguments are as for connection_edge_process_relay_cell().
795 static int
796 connection_ap_process_end_not_open(
797 relay_header_t *rh, cell_t *cell, origin_circuit_t *circ,
798 entry_connection_t *conn, crypt_path_t *layer_hint)
800 node_t *exitrouter;
801 int reason = *(cell->payload+RELAY_HEADER_SIZE);
802 int control_reason;
803 edge_connection_t *edge_conn = ENTRY_TO_EDGE_CONN(conn);
804 (void) layer_hint; /* unused */
806 if (rh->length > 0) {
807 if (reason == END_STREAM_REASON_TORPROTOCOL ||
808 reason == END_STREAM_REASON_DESTROY) {
809 /* Both of these reasons could mean a failed tag
810 * hit the exit and it complained. Do not probe.
811 * Fail the circuit. */
812 circ->path_state = PATH_STATE_USE_FAILED;
813 return -END_CIRC_REASON_TORPROTOCOL;
814 } else if (reason == END_STREAM_REASON_INTERNAL) {
815 /* We can't infer success or failure, since older Tors report
816 * ENETUNREACH as END_STREAM_REASON_INTERNAL. */
817 } else {
818 /* Path bias: If we get a valid reason code from the exit,
819 * it wasn't due to tagging.
821 * We rely on recognized+digest being strong enough to make
822 * tags unlikely to allow us to get tagged, yet 'recognized'
823 * reason codes here. */
824 pathbias_mark_use_success(circ);
828 /* This end cell is now valid. */
829 circuit_read_valid_data(circ, rh->length);
831 if (rh->length == 0) {
832 reason = END_STREAM_REASON_MISC;
835 control_reason = reason | END_STREAM_REASON_FLAG_REMOTE;
837 if (edge_reason_is_retriable(reason) &&
838 /* avoid retry if rend */
839 !connection_edge_is_rendezvous_stream(edge_conn)) {
840 const char *chosen_exit_digest =
841 circ->build_state->chosen_exit->identity_digest;
842 log_info(LD_APP,"Address '%s' refused due to '%s'. Considering retrying.",
843 safe_str(conn->socks_request->address),
844 stream_end_reason_to_string(reason));
845 exitrouter = node_get_mutable_by_id(chosen_exit_digest);
846 switch (reason) {
847 case END_STREAM_REASON_EXITPOLICY: {
848 tor_addr_t addr;
849 tor_addr_make_unspec(&addr);
850 if (rh->length >= 5) {
851 int ttl = -1;
852 tor_addr_make_unspec(&addr);
853 if (rh->length == 5 || rh->length == 9) {
854 tor_addr_from_ipv4n(&addr,
855 get_uint32(cell->payload+RELAY_HEADER_SIZE+1));
856 if (rh->length == 9)
857 ttl = (int)ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+5));
858 } else if (rh->length == 17 || rh->length == 21) {
859 tor_addr_from_ipv6_bytes(&addr,
860 (char*)(cell->payload+RELAY_HEADER_SIZE+1));
861 if (rh->length == 21)
862 ttl = (int)ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+17));
864 if (tor_addr_is_null(&addr)) {
865 log_info(LD_APP,"Address '%s' resolved to 0.0.0.0. Closing,",
866 safe_str(conn->socks_request->address));
867 connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
868 return 0;
871 if ((tor_addr_family(&addr) == AF_INET &&
872 !conn->entry_cfg.ipv4_traffic) ||
873 (tor_addr_family(&addr) == AF_INET6 &&
874 !conn->entry_cfg.ipv6_traffic)) {
875 log_fn(LOG_PROTOCOL_WARN, LD_APP,
876 "Got an EXITPOLICY failure on a connection with a "
877 "mismatched family. Closing.");
878 connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
879 return 0;
881 if (get_options()->ClientDNSRejectInternalAddresses &&
882 tor_addr_is_internal(&addr, 0)) {
883 log_info(LD_APP,"Address '%s' resolved to internal. Closing,",
884 safe_str(conn->socks_request->address));
885 connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
886 return 0;
889 client_dns_set_addressmap(conn,
890 conn->socks_request->address, &addr,
891 conn->chosen_exit_name, ttl);
894 char new_addr[TOR_ADDR_BUF_LEN];
895 tor_addr_to_str(new_addr, &addr, sizeof(new_addr), 1);
896 if (strcmp(conn->socks_request->address, new_addr)) {
897 strlcpy(conn->socks_request->address, new_addr,
898 sizeof(conn->socks_request->address));
899 control_event_stream_status(conn, STREAM_EVENT_REMAP, 0);
903 /* check if the exit *ought* to have allowed it */
905 adjust_exit_policy_from_exitpolicy_failure(circ,
906 conn,
907 exitrouter,
908 &addr);
910 if (conn->chosen_exit_optional ||
911 conn->chosen_exit_retries) {
912 /* stop wanting a specific exit */
913 conn->chosen_exit_optional = 0;
914 /* A non-zero chosen_exit_retries can happen if we set a
915 * TrackHostExits for this address under a port that the exit
916 * relay allows, but then try the same address with a different
917 * port that it doesn't allow to exit. We shouldn't unregister
918 * the mapping, since it is probably still wanted on the
919 * original port. But now we give away to the exit relay that
920 * we probably have a TrackHostExits on it. So be it. */
921 conn->chosen_exit_retries = 0;
922 tor_free(conn->chosen_exit_name); /* clears it */
924 if (connection_ap_detach_retriable(conn, circ, control_reason) >= 0)
925 return 0;
926 /* else, conn will get closed below */
927 break;
929 case END_STREAM_REASON_CONNECTREFUSED:
930 if (!conn->chosen_exit_optional)
931 break; /* break means it'll close, below */
932 /* Else fall through: expire this circuit, clear the
933 * chosen_exit_name field, and try again. */
934 /* Falls through. */
935 case END_STREAM_REASON_RESOLVEFAILED:
936 case END_STREAM_REASON_TIMEOUT:
937 case END_STREAM_REASON_MISC:
938 case END_STREAM_REASON_NOROUTE:
939 if (client_dns_incr_failures(conn->socks_request->address)
940 < MAX_RESOLVE_FAILURES) {
941 /* We haven't retried too many times; reattach the connection. */
942 circuit_log_path(LOG_INFO,LD_APP,circ);
943 /* Mark this circuit "unusable for new streams". */
944 mark_circuit_unusable_for_new_conns(circ);
946 if (conn->chosen_exit_optional) {
947 /* stop wanting a specific exit */
948 conn->chosen_exit_optional = 0;
949 tor_free(conn->chosen_exit_name); /* clears it */
951 if (connection_ap_detach_retriable(conn, circ, control_reason) >= 0)
952 return 0;
953 /* else, conn will get closed below */
954 } else {
955 log_notice(LD_APP,
956 "Have tried resolving or connecting to address '%s' "
957 "at %d different places. Giving up.",
958 safe_str(conn->socks_request->address),
959 MAX_RESOLVE_FAILURES);
960 /* clear the failures, so it will have a full try next time */
961 client_dns_clear_failures(conn->socks_request->address);
963 break;
964 case END_STREAM_REASON_HIBERNATING:
965 case END_STREAM_REASON_RESOURCELIMIT:
966 if (exitrouter) {
967 policies_set_node_exitpolicy_to_reject_all(exitrouter);
969 if (conn->chosen_exit_optional) {
970 /* stop wanting a specific exit */
971 conn->chosen_exit_optional = 0;
972 tor_free(conn->chosen_exit_name); /* clears it */
974 if (connection_ap_detach_retriable(conn, circ, control_reason) >= 0)
975 return 0;
976 /* else, will close below */
977 break;
978 } /* end switch */
979 log_info(LD_APP,"Giving up on retrying; conn can't be handled.");
982 log_info(LD_APP,
983 "Edge got end (%s) before we're connected. Marking for close.",
984 stream_end_reason_to_string(rh->length > 0 ? reason : -1));
985 circuit_log_path(LOG_INFO,LD_APP,circ);
986 /* need to test because of detach_retriable */
987 if (!ENTRY_TO_CONN(conn)->marked_for_close)
988 connection_mark_unattached_ap(conn, control_reason);
989 return 0;
992 /** Called when we have gotten an END_REASON_EXITPOLICY failure on <b>circ</b>
993 * for <b>conn</b>, while attempting to connect via <b>node</b>. If the node
994 * told us which address it rejected, then <b>addr</b> is that address;
995 * otherwise it is AF_UNSPEC.
997 * If we are sure the node should have allowed this address, mark the node as
998 * having a reject *:* exit policy. Otherwise, mark the circuit as unusable
999 * for this particular address.
1001 static void
1002 adjust_exit_policy_from_exitpolicy_failure(origin_circuit_t *circ,
1003 entry_connection_t *conn,
1004 node_t *node,
1005 const tor_addr_t *addr)
1007 int make_reject_all = 0;
1008 const sa_family_t family = tor_addr_family(addr);
1010 if (node) {
1011 tor_addr_t tmp;
1012 int asked_for_family = tor_addr_parse(&tmp, conn->socks_request->address);
1013 if (family == AF_UNSPEC) {
1014 make_reject_all = 1;
1015 } else if (node_exit_policy_is_exact(node, family) &&
1016 asked_for_family != -1 && !conn->chosen_exit_name) {
1017 make_reject_all = 1;
1020 if (make_reject_all) {
1021 log_info(LD_APP,
1022 "Exitrouter %s seems to be more restrictive than its exit "
1023 "policy. Not using this router as exit for now.",
1024 node_describe(node));
1025 policies_set_node_exitpolicy_to_reject_all(node);
1029 if (family != AF_UNSPEC)
1030 addr_policy_append_reject_addr(&circ->prepend_policy, addr);
1033 /** Helper: change the socks_request-&gt;address field on conn to the
1034 * dotted-quad representation of <b>new_addr</b>,
1035 * and send an appropriate REMAP event. */
1036 static void
1037 remap_event_helper(entry_connection_t *conn, const tor_addr_t *new_addr)
1039 tor_addr_to_str(conn->socks_request->address, new_addr,
1040 sizeof(conn->socks_request->address),
1042 control_event_stream_status(conn, STREAM_EVENT_REMAP,
1043 REMAP_STREAM_SOURCE_EXIT);
1046 /** Extract the contents of a connected cell in <b>cell</b>, whose relay
1047 * header has already been parsed into <b>rh</b>. On success, set
1048 * <b>addr_out</b> to the address we're connected to, and <b>ttl_out</b> to
1049 * the ttl of that address, in seconds, and return 0. On failure, return
1050 * -1.
1052 * Note that the resulting address can be UNSPEC if the connected cell had no
1053 * address (as for a stream to an union service or a tunneled directory
1054 * connection), and that the ttl can be absent (in which case <b>ttl_out</b>
1055 * is set to -1). */
1056 STATIC int
1057 connected_cell_parse(const relay_header_t *rh, const cell_t *cell,
1058 tor_addr_t *addr_out, int *ttl_out)
1060 uint32_t bytes;
1061 const uint8_t *payload = cell->payload + RELAY_HEADER_SIZE;
1063 tor_addr_make_unspec(addr_out);
1064 *ttl_out = -1;
1065 if (rh->length == 0)
1066 return 0;
1067 if (rh->length < 4)
1068 return -1;
1069 bytes = ntohl(get_uint32(payload));
1071 /* If bytes is 0, this is maybe a v6 address. Otherwise it's a v4 address */
1072 if (bytes != 0) {
1073 /* v4 address */
1074 tor_addr_from_ipv4h(addr_out, bytes);
1075 if (rh->length >= 8) {
1076 bytes = ntohl(get_uint32(payload + 4));
1077 if (bytes <= INT32_MAX)
1078 *ttl_out = bytes;
1080 } else {
1081 if (rh->length < 25) /* 4 bytes of 0s, 1 addr, 16 ipv4, 4 ttl. */
1082 return -1;
1083 if (get_uint8(payload + 4) != 6)
1084 return -1;
1085 tor_addr_from_ipv6_bytes(addr_out, (char*)(payload + 5));
1086 bytes = ntohl(get_uint32(payload + 21));
1087 if (bytes <= INT32_MAX)
1088 *ttl_out = (int) bytes;
1090 return 0;
1093 /** Drop all storage held by <b>addr</b>. */
1094 STATIC void
1095 address_ttl_free_(address_ttl_t *addr)
1097 if (!addr)
1098 return;
1099 tor_free(addr->hostname);
1100 tor_free(addr);
1103 /** Parse a resolved cell in <b>cell</b>, with parsed header in <b>rh</b>.
1104 * Return -1 on parse error. On success, add one or more newly allocated
1105 * address_ttl_t to <b>addresses_out</b>; set *<b>errcode_out</b> to
1106 * one of 0, RESOLVED_TYPE_ERROR, or RESOLVED_TYPE_ERROR_TRANSIENT, and
1107 * return 0. */
1108 STATIC int
1109 resolved_cell_parse(const cell_t *cell, const relay_header_t *rh,
1110 smartlist_t *addresses_out, int *errcode_out)
1112 const uint8_t *cp;
1113 uint8_t answer_type;
1114 size_t answer_len;
1115 address_ttl_t *addr;
1116 size_t remaining;
1117 int errcode = 0;
1118 smartlist_t *addrs;
1120 tor_assert(cell);
1121 tor_assert(rh);
1122 tor_assert(addresses_out);
1123 tor_assert(errcode_out);
1125 *errcode_out = 0;
1127 if (rh->length > RELAY_PAYLOAD_SIZE)
1128 return -1;
1130 addrs = smartlist_new();
1132 cp = cell->payload + RELAY_HEADER_SIZE;
1134 remaining = rh->length;
1135 while (remaining) {
1136 const uint8_t *cp_orig = cp;
1137 if (remaining < 2)
1138 goto err;
1139 answer_type = *cp++;
1140 answer_len = *cp++;
1141 if (remaining < 2 + answer_len + 4) {
1142 goto err;
1144 if (answer_type == RESOLVED_TYPE_IPV4) {
1145 if (answer_len != 4) {
1146 goto err;
1148 addr = tor_malloc_zero(sizeof(*addr));
1149 tor_addr_from_ipv4n(&addr->addr, get_uint32(cp));
1150 cp += 4;
1151 addr->ttl = ntohl(get_uint32(cp));
1152 cp += 4;
1153 smartlist_add(addrs, addr);
1154 } else if (answer_type == RESOLVED_TYPE_IPV6) {
1155 if (answer_len != 16)
1156 goto err;
1157 addr = tor_malloc_zero(sizeof(*addr));
1158 tor_addr_from_ipv6_bytes(&addr->addr, (const char*) cp);
1159 cp += 16;
1160 addr->ttl = ntohl(get_uint32(cp));
1161 cp += 4;
1162 smartlist_add(addrs, addr);
1163 } else if (answer_type == RESOLVED_TYPE_HOSTNAME) {
1164 if (answer_len == 0) {
1165 goto err;
1167 addr = tor_malloc_zero(sizeof(*addr));
1168 addr->hostname = tor_memdup_nulterm(cp, answer_len);
1169 cp += answer_len;
1170 addr->ttl = ntohl(get_uint32(cp));
1171 cp += 4;
1172 smartlist_add(addrs, addr);
1173 } else if (answer_type == RESOLVED_TYPE_ERROR_TRANSIENT ||
1174 answer_type == RESOLVED_TYPE_ERROR) {
1175 errcode = answer_type;
1176 /* Ignore the error contents */
1177 cp += answer_len + 4;
1178 } else {
1179 cp += answer_len + 4;
1181 tor_assert(((ssize_t)remaining) >= (cp - cp_orig));
1182 remaining -= (cp - cp_orig);
1185 if (errcode && smartlist_len(addrs) == 0) {
1186 /* Report an error only if there were no results. */
1187 *errcode_out = errcode;
1190 smartlist_add_all(addresses_out, addrs);
1191 smartlist_free(addrs);
1193 return 0;
1195 err:
1196 /* On parse error, don't report any results */
1197 SMARTLIST_FOREACH(addrs, address_ttl_t *, a, address_ttl_free(a));
1198 smartlist_free(addrs);
1199 return -1;
1202 /** Helper for connection_edge_process_resolved_cell: given an error code,
1203 * an entry_connection, and a list of address_ttl_t *, report the best answer
1204 * to the entry_connection. */
1205 static void
1206 connection_ap_handshake_socks_got_resolved_cell(entry_connection_t *conn,
1207 int error_code,
1208 smartlist_t *results)
1210 address_ttl_t *addr_ipv4 = NULL;
1211 address_ttl_t *addr_ipv6 = NULL;
1212 address_ttl_t *addr_hostname = NULL;
1213 address_ttl_t *addr_best = NULL;
1215 /* If it's an error code, that's easy. */
1216 if (error_code) {
1217 tor_assert(error_code == RESOLVED_TYPE_ERROR ||
1218 error_code == RESOLVED_TYPE_ERROR_TRANSIENT);
1219 connection_ap_handshake_socks_resolved(conn,
1220 error_code,0,NULL,-1,-1);
1221 return;
1224 /* Get the first answer of each type. */
1225 SMARTLIST_FOREACH_BEGIN(results, address_ttl_t *, addr) {
1226 if (addr->hostname) {
1227 if (!addr_hostname) {
1228 addr_hostname = addr;
1230 } else if (tor_addr_family(&addr->addr) == AF_INET) {
1231 if (!addr_ipv4 && conn->entry_cfg.ipv4_traffic) {
1232 addr_ipv4 = addr;
1234 } else if (tor_addr_family(&addr->addr) == AF_INET6) {
1235 if (!addr_ipv6 && conn->entry_cfg.ipv6_traffic) {
1236 addr_ipv6 = addr;
1239 } SMARTLIST_FOREACH_END(addr);
1241 /* Now figure out which type we wanted to deliver. */
1242 if (conn->socks_request->command == SOCKS_COMMAND_RESOLVE_PTR) {
1243 if (addr_hostname) {
1244 connection_ap_handshake_socks_resolved(conn,
1245 RESOLVED_TYPE_HOSTNAME,
1246 strlen(addr_hostname->hostname),
1247 (uint8_t*)addr_hostname->hostname,
1248 addr_hostname->ttl,-1);
1249 } else {
1250 connection_ap_handshake_socks_resolved(conn,
1251 RESOLVED_TYPE_ERROR,0,NULL,-1,-1);
1253 return;
1256 if (conn->entry_cfg.prefer_ipv6) {
1257 addr_best = addr_ipv6 ? addr_ipv6 : addr_ipv4;
1258 } else {
1259 addr_best = addr_ipv4 ? addr_ipv4 : addr_ipv6;
1262 /* Now convert it to the ugly old interface */
1263 if (! addr_best) {
1264 connection_ap_handshake_socks_resolved(conn,
1265 RESOLVED_TYPE_ERROR,0,NULL,-1,-1);
1266 return;
1269 connection_ap_handshake_socks_resolved_addr(conn,
1270 &addr_best->addr,
1271 addr_best->ttl,
1272 -1);
1274 remap_event_helper(conn, &addr_best->addr);
1277 /** Handle a RELAY_COMMAND_RESOLVED cell that we received on a non-open AP
1278 * stream. */
1279 STATIC int
1280 connection_edge_process_resolved_cell(edge_connection_t *conn,
1281 const cell_t *cell,
1282 const relay_header_t *rh)
1284 entry_connection_t *entry_conn = EDGE_TO_ENTRY_CONN(conn);
1285 smartlist_t *resolved_addresses = NULL;
1286 int errcode = 0;
1288 if (conn->base_.state != AP_CONN_STATE_RESOLVE_WAIT) {
1289 log_fn(LOG_PROTOCOL_WARN, LD_APP, "Got a 'resolved' cell while "
1290 "not in state resolve_wait. Dropping.");
1291 return 0;
1293 tor_assert(SOCKS_COMMAND_IS_RESOLVE(entry_conn->socks_request->command));
1295 resolved_addresses = smartlist_new();
1296 if (resolved_cell_parse(cell, rh, resolved_addresses, &errcode)) {
1297 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1298 "Dropping malformed 'resolved' cell");
1299 connection_mark_unattached_ap(entry_conn, END_STREAM_REASON_TORPROTOCOL);
1300 goto done;
1303 if (get_options()->ClientDNSRejectInternalAddresses) {
1304 int orig_len = smartlist_len(resolved_addresses);
1305 SMARTLIST_FOREACH_BEGIN(resolved_addresses, address_ttl_t *, addr) {
1306 if (addr->hostname == NULL && tor_addr_is_internal(&addr->addr, 0)) {
1307 log_info(LD_APP, "Got a resolved cell with answer %s; dropping that "
1308 "answer.",
1309 safe_str_client(fmt_addr(&addr->addr)));
1310 address_ttl_free(addr);
1311 SMARTLIST_DEL_CURRENT(resolved_addresses, addr);
1313 } SMARTLIST_FOREACH_END(addr);
1314 if (orig_len && smartlist_len(resolved_addresses) == 0) {
1315 log_info(LD_APP, "Got a resolved cell with only private addresses; "
1316 "dropping it.");
1317 connection_ap_handshake_socks_resolved(entry_conn,
1318 RESOLVED_TYPE_ERROR_TRANSIENT,
1319 0, NULL, 0, TIME_MAX);
1320 connection_mark_unattached_ap(entry_conn,
1321 END_STREAM_REASON_TORPROTOCOL);
1322 goto done;
1326 /* This is valid data at this point. Count it */
1327 if (conn->on_circuit && CIRCUIT_IS_ORIGIN(conn->on_circuit)) {
1328 circuit_read_valid_data(TO_ORIGIN_CIRCUIT(conn->on_circuit),
1329 rh->length);
1332 connection_ap_handshake_socks_got_resolved_cell(entry_conn,
1333 errcode,
1334 resolved_addresses);
1336 connection_mark_unattached_ap(entry_conn,
1337 END_STREAM_REASON_DONE |
1338 END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED);
1340 done:
1341 SMARTLIST_FOREACH(resolved_addresses, address_ttl_t *, addr,
1342 address_ttl_free(addr));
1343 smartlist_free(resolved_addresses);
1344 return 0;
1347 /** An incoming relay cell has arrived from circuit <b>circ</b> to
1348 * stream <b>conn</b>.
1350 * The arguments here are the same as in
1351 * connection_edge_process_relay_cell() below; this function is called
1352 * from there when <b>conn</b> is defined and not in an open state.
1354 static int
1355 connection_edge_process_relay_cell_not_open(
1356 relay_header_t *rh, cell_t *cell, circuit_t *circ,
1357 edge_connection_t *conn, crypt_path_t *layer_hint)
1359 if (rh->command == RELAY_COMMAND_END) {
1360 if (CIRCUIT_IS_ORIGIN(circ) && conn->base_.type == CONN_TYPE_AP) {
1361 return connection_ap_process_end_not_open(rh, cell,
1362 TO_ORIGIN_CIRCUIT(circ),
1363 EDGE_TO_ENTRY_CONN(conn),
1364 layer_hint);
1365 } else {
1366 /* we just got an 'end', don't need to send one */
1367 conn->edge_has_sent_end = 1;
1368 conn->end_reason = *(cell->payload+RELAY_HEADER_SIZE) |
1369 END_STREAM_REASON_FLAG_REMOTE;
1370 connection_mark_for_close(TO_CONN(conn));
1371 return 0;
1375 if (conn->base_.type == CONN_TYPE_AP &&
1376 rh->command == RELAY_COMMAND_CONNECTED) {
1377 tor_addr_t addr;
1378 int ttl;
1379 entry_connection_t *entry_conn = EDGE_TO_ENTRY_CONN(conn);
1380 tor_assert(CIRCUIT_IS_ORIGIN(circ));
1381 if (conn->base_.state != AP_CONN_STATE_CONNECT_WAIT) {
1382 log_fn(LOG_PROTOCOL_WARN, LD_APP,
1383 "Got 'connected' while not in state connect_wait. Dropping.");
1384 return 0;
1386 CONNECTION_AP_EXPECT_NONPENDING(entry_conn);
1387 conn->base_.state = AP_CONN_STATE_OPEN;
1388 log_info(LD_APP,"'connected' received for circid %u streamid %d "
1389 "after %d seconds.",
1390 (unsigned)circ->n_circ_id,
1391 rh->stream_id,
1392 (int)(time(NULL) - conn->base_.timestamp_last_read_allowed));
1393 if (connected_cell_parse(rh, cell, &addr, &ttl) < 0) {
1394 log_fn(LOG_PROTOCOL_WARN, LD_APP,
1395 "Got a badly formatted connected cell. Closing.");
1396 connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL);
1397 connection_mark_unattached_ap(entry_conn, END_STREAM_REASON_TORPROTOCOL);
1398 return 0;
1400 if (tor_addr_family(&addr) != AF_UNSPEC) {
1401 /* The family is not UNSPEC: so we were given an address in the
1402 * connected cell. (This is normal, except for BEGINDIR and onion
1403 * service streams.) */
1404 const sa_family_t family = tor_addr_family(&addr);
1405 if (tor_addr_is_null(&addr) ||
1406 (get_options()->ClientDNSRejectInternalAddresses &&
1407 tor_addr_is_internal(&addr, 0))) {
1408 log_info(LD_APP, "...but it claims the IP address was %s. Closing.",
1409 fmt_addr(&addr));
1410 connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL);
1411 connection_mark_unattached_ap(entry_conn,
1412 END_STREAM_REASON_TORPROTOCOL);
1413 return 0;
1416 if ((family == AF_INET && ! entry_conn->entry_cfg.ipv4_traffic) ||
1417 (family == AF_INET6 && ! entry_conn->entry_cfg.ipv6_traffic)) {
1418 log_fn(LOG_PROTOCOL_WARN, LD_APP,
1419 "Got a connected cell to %s with unsupported address family."
1420 " Closing.", 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 client_dns_set_addressmap(entry_conn,
1428 entry_conn->socks_request->address, &addr,
1429 entry_conn->chosen_exit_name, ttl);
1431 remap_event_helper(entry_conn, &addr);
1433 circuit_log_path(LOG_INFO,LD_APP,TO_ORIGIN_CIRCUIT(circ));
1434 /* don't send a socks reply to transparent conns */
1435 tor_assert(entry_conn->socks_request != NULL);
1436 if (!entry_conn->socks_request->has_finished) {
1437 connection_ap_handshake_socks_reply(entry_conn, NULL, 0, 0);
1440 /* Was it a linked dir conn? If so, a dir request just started to
1441 * fetch something; this could be a bootstrap status milestone. */
1442 log_debug(LD_APP, "considering");
1443 if (TO_CONN(conn)->linked_conn &&
1444 TO_CONN(conn)->linked_conn->type == CONN_TYPE_DIR) {
1445 connection_t *dirconn = TO_CONN(conn)->linked_conn;
1446 log_debug(LD_APP, "it is! %d", dirconn->purpose);
1447 switch (dirconn->purpose) {
1448 case DIR_PURPOSE_FETCH_CERTIFICATE:
1449 if (consensus_is_waiting_for_certs())
1450 control_event_bootstrap(BOOTSTRAP_STATUS_LOADING_KEYS, 0);
1451 break;
1452 case DIR_PURPOSE_FETCH_CONSENSUS:
1453 control_event_bootstrap(BOOTSTRAP_STATUS_LOADING_STATUS, 0);
1454 break;
1455 case DIR_PURPOSE_FETCH_SERVERDESC:
1456 case DIR_PURPOSE_FETCH_MICRODESC:
1457 if (TO_DIR_CONN(dirconn)->router_purpose == ROUTER_PURPOSE_GENERAL)
1458 control_event_boot_dir(BOOTSTRAP_STATUS_LOADING_DESCRIPTORS,
1459 count_loading_descriptors_progress());
1460 break;
1463 /* This is definitely a success, so forget about any pending data we
1464 * had sent. */
1465 if (entry_conn->pending_optimistic_data) {
1466 buf_free(entry_conn->pending_optimistic_data);
1467 entry_conn->pending_optimistic_data = NULL;
1470 /* This is valid data at this point. Count it */
1471 circuit_read_valid_data(TO_ORIGIN_CIRCUIT(circ), rh->length);
1473 /* handle anything that might have queued */
1474 if (connection_edge_package_raw_inbuf(conn, 1, NULL) < 0) {
1475 /* (We already sent an end cell if possible) */
1476 connection_mark_for_close(TO_CONN(conn));
1477 return 0;
1479 return 0;
1481 if (conn->base_.type == CONN_TYPE_AP &&
1482 rh->command == RELAY_COMMAND_RESOLVED) {
1483 return connection_edge_process_resolved_cell(conn, cell, rh);
1486 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1487 "Got an unexpected relay command %d, in state %d (%s). Dropping.",
1488 rh->command, conn->base_.state,
1489 conn_state_to_string(conn->base_.type, conn->base_.state));
1490 return 0; /* for forward compatibility, don't kill the circuit */
1491 // connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL);
1492 // connection_mark_for_close(conn);
1493 // return -1;
1496 /** Process a SENDME cell that arrived on <b>circ</b>. If it is a stream level
1497 * cell, it is destined for the given <b>conn</b>. If it is a circuit level
1498 * cell, it is destined for the <b>layer_hint</b>. The <b>domain</b> is the
1499 * logging domain that should be used.
1501 * Return 0 if everything went well or a negative value representing a circuit
1502 * end reason on error for which the caller is responsible for closing it. */
1503 static int
1504 process_sendme_cell(const relay_header_t *rh, const cell_t *cell,
1505 circuit_t *circ, edge_connection_t *conn,
1506 crypt_path_t *layer_hint, int domain)
1508 int ret;
1510 tor_assert(rh);
1512 if (!rh->stream_id) {
1513 /* Circuit level SENDME cell. */
1514 ret = sendme_process_circuit_level(layer_hint, circ,
1515 cell->payload + RELAY_HEADER_SIZE,
1516 rh->length);
1517 if (ret < 0) {
1518 return ret;
1520 /* Resume reading on any streams now that we've processed a valid
1521 * SENDME cell that updated our package window. */
1522 circuit_resume_edge_reading(circ, layer_hint);
1523 /* We are done, the rest of the code is for the stream level. */
1524 return 0;
1527 /* No connection, might be half edge state. We are done if so. */
1528 if (!conn) {
1529 if (CIRCUIT_IS_ORIGIN(circ)) {
1530 origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
1531 if (connection_half_edge_is_valid_sendme(ocirc->half_streams,
1532 rh->stream_id)) {
1533 circuit_read_valid_data(ocirc, rh->length);
1534 log_info(domain, "Sendme cell on circ %u valid on half-closed "
1535 "stream id %d",
1536 ocirc->global_identifier, rh->stream_id);
1540 log_info(domain, "SENDME cell dropped, unknown stream (streamid %d).",
1541 rh->stream_id);
1542 return 0;
1545 /* Stream level SENDME cell. */
1546 ret = sendme_process_stream_level(conn, circ, rh->length);
1547 if (ret < 0) {
1548 /* Means we need to close the circuit with reason ret. */
1549 return ret;
1552 /* We've now processed properly a SENDME cell, all windows have been
1553 * properly updated, we'll read on the edge connection to see if we can
1554 * get data out towards the end point (Exit or client) since we are now
1555 * allowed to deliver more cells. */
1557 if (circuit_queue_streams_are_blocked(circ)) {
1558 /* Still waiting for queue to flush; don't touch conn */
1559 return 0;
1561 connection_start_reading(TO_CONN(conn));
1562 /* handle whatever might still be on the inbuf */
1563 if (connection_edge_package_raw_inbuf(conn, 1, NULL) < 0) {
1564 /* (We already sent an end cell if possible) */
1565 connection_mark_for_close(TO_CONN(conn));
1566 return 0;
1568 return 0;
1571 /** An incoming relay cell has arrived on circuit <b>circ</b>. If
1572 * <b>conn</b> is NULL this is a control cell, else <b>cell</b> is
1573 * destined for <b>conn</b>.
1575 * If <b>layer_hint</b> is defined, then we're the origin of the
1576 * circuit, and it specifies the hop that packaged <b>cell</b>.
1578 * Return -reason if you want to warn and tear down the circuit, else 0.
1580 STATIC int
1581 connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ,
1582 edge_connection_t *conn,
1583 crypt_path_t *layer_hint)
1585 static int num_seen=0;
1586 relay_header_t rh;
1587 unsigned domain = layer_hint?LD_APP:LD_EXIT;
1588 int reason;
1589 int optimistic_data = 0; /* Set to 1 if we receive data on a stream
1590 * that's in the EXIT_CONN_STATE_RESOLVING
1591 * or EXIT_CONN_STATE_CONNECTING states. */
1593 tor_assert(cell);
1594 tor_assert(circ);
1596 relay_header_unpack(&rh, cell->payload);
1597 // log_fn(LOG_DEBUG,"command %d stream %d", rh.command, rh.stream_id);
1598 num_seen++;
1599 log_debug(domain, "Now seen %d relay cells here (command %d, stream %d).",
1600 num_seen, rh.command, rh.stream_id);
1602 if (rh.length > RELAY_PAYLOAD_SIZE) {
1603 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1604 "Relay cell length field too long. Closing circuit.");
1605 return - END_CIRC_REASON_TORPROTOCOL;
1608 if (rh.stream_id == 0) {
1609 switch (rh.command) {
1610 case RELAY_COMMAND_BEGIN:
1611 case RELAY_COMMAND_CONNECTED:
1612 case RELAY_COMMAND_END:
1613 case RELAY_COMMAND_RESOLVE:
1614 case RELAY_COMMAND_RESOLVED:
1615 case RELAY_COMMAND_BEGIN_DIR:
1616 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, "Relay command %d with zero "
1617 "stream_id. Dropping.", (int)rh.command);
1618 return 0;
1619 default:
1624 /* Tell circpad that we've recieved a recognized cell */
1625 circpad_deliver_recognized_relay_cell_events(circ, rh.command, layer_hint);
1627 /* either conn is NULL, in which case we've got a control cell, or else
1628 * conn points to the recognized stream. */
1629 if (conn && !connection_state_is_open(TO_CONN(conn))) {
1630 if (conn->base_.type == CONN_TYPE_EXIT &&
1631 (conn->base_.state == EXIT_CONN_STATE_CONNECTING ||
1632 conn->base_.state == EXIT_CONN_STATE_RESOLVING) &&
1633 rh.command == RELAY_COMMAND_DATA) {
1634 /* Allow DATA cells to be delivered to an exit node in state
1635 * EXIT_CONN_STATE_CONNECTING or EXIT_CONN_STATE_RESOLVING.
1636 * This speeds up HTTP, for example. */
1637 optimistic_data = 1;
1638 } else if (rh.stream_id == 0 && rh.command == RELAY_COMMAND_DATA) {
1639 log_warn(LD_BUG, "Somehow I had a connection that matched a "
1640 "data cell with stream ID 0.");
1641 } else {
1642 return connection_edge_process_relay_cell_not_open(
1643 &rh, cell, circ, conn, layer_hint);
1647 switch (rh.command) {
1648 case RELAY_COMMAND_DROP:
1649 /* Already examined in circpad_deliver_recognized_relay_cell_events */
1650 return 0;
1651 case RELAY_COMMAND_PADDING_NEGOTIATE:
1652 circpad_handle_padding_negotiate(circ, cell);
1653 return 0;
1654 case RELAY_COMMAND_PADDING_NEGOTIATED:
1655 if (circpad_handle_padding_negotiated(circ, cell, layer_hint) == 0)
1656 circuit_read_valid_data(TO_ORIGIN_CIRCUIT(circ), rh.length);
1657 return 0;
1658 case RELAY_COMMAND_BEGIN:
1659 case RELAY_COMMAND_BEGIN_DIR:
1660 if (layer_hint &&
1661 circ->purpose != CIRCUIT_PURPOSE_S_REND_JOINED) {
1662 log_fn(LOG_PROTOCOL_WARN, LD_APP,
1663 "Relay begin request unsupported at AP. Dropping.");
1664 return 0;
1666 if (circ->purpose == CIRCUIT_PURPOSE_S_REND_JOINED &&
1667 layer_hint != TO_ORIGIN_CIRCUIT(circ)->cpath->prev) {
1668 log_fn(LOG_PROTOCOL_WARN, LD_APP,
1669 "Relay begin request to Hidden Service "
1670 "from intermediary node. Dropping.");
1671 return 0;
1673 if (conn) {
1674 log_fn(LOG_PROTOCOL_WARN, domain,
1675 "Begin cell for known stream. Dropping.");
1676 return 0;
1678 if (rh.command == RELAY_COMMAND_BEGIN_DIR &&
1679 circ->purpose != CIRCUIT_PURPOSE_S_REND_JOINED) {
1680 /* Assign this circuit and its app-ward OR connection a unique ID,
1681 * so that we can measure download times. The local edge and dir
1682 * connection will be assigned the same ID when they are created
1683 * and linked. */
1684 static uint64_t next_id = 0;
1685 circ->dirreq_id = ++next_id;
1686 TO_OR_CIRCUIT(circ)->p_chan->dirreq_id = circ->dirreq_id;
1688 return connection_exit_begin_conn(cell, circ);
1689 case RELAY_COMMAND_DATA:
1690 ++stats_n_data_cells_received;
1692 /* Update our circuit-level deliver window that we received a DATA cell.
1693 * If the deliver window goes below 0, we end the circuit and stream due
1694 * to a protocol failure. */
1695 if (sendme_circuit_data_received(circ, layer_hint) < 0) {
1696 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1697 "(relay data) circ deliver_window below 0. Killing.");
1698 connection_edge_end_close(conn, END_STREAM_REASON_TORPROTOCOL);
1699 return -END_CIRC_REASON_TORPROTOCOL;
1702 /* Consider sending a circuit-level SENDME cell. */
1703 sendme_circuit_consider_sending(circ, layer_hint);
1705 if (rh.stream_id == 0) {
1706 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, "Relay data cell with zero "
1707 "stream_id. Dropping.");
1708 return 0;
1709 } else if (!conn) {
1710 if (CIRCUIT_IS_ORIGIN(circ)) {
1711 origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
1712 if (connection_half_edge_is_valid_data(ocirc->half_streams,
1713 rh.stream_id)) {
1714 circuit_read_valid_data(ocirc, rh.length);
1715 log_info(domain,
1716 "data cell on circ %u valid on half-closed "
1717 "stream id %d", ocirc->global_identifier, rh.stream_id);
1721 log_info(domain,"data cell dropped, unknown stream (streamid %d).",
1722 rh.stream_id);
1723 return 0;
1726 /* Update our stream-level deliver window that we just received a DATA
1727 * cell. Going below 0 means we have a protocol level error so the
1728 * stream and circuit are closed. */
1730 if (sendme_stream_data_received(conn) < 0) {
1731 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1732 "(relay data) conn deliver_window below 0. Killing.");
1733 connection_edge_end_close(conn, END_STREAM_REASON_TORPROTOCOL);
1734 return -END_CIRC_REASON_TORPROTOCOL;
1736 /* Total all valid application bytes delivered */
1737 if (CIRCUIT_IS_ORIGIN(circ) && rh.length > 0) {
1738 circuit_read_valid_data(TO_ORIGIN_CIRCUIT(circ), rh.length);
1741 stats_n_data_bytes_received += rh.length;
1742 connection_buf_add((char*)(cell->payload + RELAY_HEADER_SIZE),
1743 rh.length, TO_CONN(conn));
1745 #ifdef MEASUREMENTS_21206
1746 /* Count number of RELAY_DATA cells received on a linked directory
1747 * connection. */
1748 connection_t *linked_conn = TO_CONN(conn)->linked_conn;
1750 if (linked_conn && linked_conn->type == CONN_TYPE_DIR) {
1751 ++(TO_DIR_CONN(linked_conn)->data_cells_received);
1753 #endif /* defined(MEASUREMENTS_21206) */
1755 if (!optimistic_data) {
1756 /* Only send a SENDME if we're not getting optimistic data; otherwise
1757 * a SENDME could arrive before the CONNECTED.
1759 sendme_connection_edge_consider_sending(conn);
1762 return 0;
1763 case RELAY_COMMAND_END:
1764 reason = rh.length > 0 ?
1765 get_uint8(cell->payload+RELAY_HEADER_SIZE) : END_STREAM_REASON_MISC;
1766 if (!conn) {
1767 if (CIRCUIT_IS_ORIGIN(circ)) {
1768 origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
1769 if (connection_half_edge_is_valid_end(ocirc->half_streams,
1770 rh.stream_id)) {
1772 circuit_read_valid_data(ocirc, rh.length);
1773 log_info(domain,
1774 "end cell (%s) on circ %u valid on half-closed "
1775 "stream id %d",
1776 stream_end_reason_to_string(reason),
1777 ocirc->global_identifier, rh.stream_id);
1778 return 0;
1781 log_info(domain,"end cell (%s) dropped, unknown stream.",
1782 stream_end_reason_to_string(reason));
1783 return 0;
1785 /* XXX add to this log_fn the exit node's nickname? */
1786 log_info(domain,TOR_SOCKET_T_FORMAT": end cell (%s) for stream %d. "
1787 "Removing stream.",
1788 conn->base_.s,
1789 stream_end_reason_to_string(reason),
1790 conn->stream_id);
1791 if (conn->base_.type == CONN_TYPE_AP) {
1792 entry_connection_t *entry_conn = EDGE_TO_ENTRY_CONN(conn);
1793 if (entry_conn->socks_request &&
1794 !entry_conn->socks_request->has_finished)
1795 log_warn(LD_BUG,
1796 "open stream hasn't sent socks answer yet? Closing.");
1798 /* We just *got* an end; no reason to send one. */
1799 conn->edge_has_sent_end = 1;
1800 if (!conn->end_reason)
1801 conn->end_reason = reason | END_STREAM_REASON_FLAG_REMOTE;
1802 if (!conn->base_.marked_for_close) {
1803 /* only mark it if not already marked. it's possible to
1804 * get the 'end' right around when the client hangs up on us. */
1805 connection_mark_and_flush(TO_CONN(conn));
1807 /* Total all valid application bytes delivered */
1808 if (CIRCUIT_IS_ORIGIN(circ)) {
1809 circuit_read_valid_data(TO_ORIGIN_CIRCUIT(circ), rh.length);
1812 return 0;
1813 case RELAY_COMMAND_EXTEND:
1814 case RELAY_COMMAND_EXTEND2: {
1815 static uint64_t total_n_extend=0, total_nonearly=0;
1816 total_n_extend++;
1817 if (rh.stream_id) {
1818 log_fn(LOG_PROTOCOL_WARN, domain,
1819 "'extend' cell received for non-zero stream. Dropping.");
1820 return 0;
1822 if (cell->command != CELL_RELAY_EARLY &&
1823 !networkstatus_get_param(NULL,"AllowNonearlyExtend",0,0,1)) {
1824 #define EARLY_WARNING_INTERVAL 3600
1825 static ratelim_t early_warning_limit =
1826 RATELIM_INIT(EARLY_WARNING_INTERVAL);
1827 char *m;
1828 if (cell->command == CELL_RELAY) {
1829 ++total_nonearly;
1830 if ((m = rate_limit_log(&early_warning_limit, approx_time()))) {
1831 double percentage = ((double)total_nonearly)/total_n_extend;
1832 percentage *= 100;
1833 log_fn(LOG_PROTOCOL_WARN, domain, "EXTEND cell received, "
1834 "but not via RELAY_EARLY. Dropping.%s", m);
1835 log_fn(LOG_PROTOCOL_WARN, domain, " (We have dropped %.02f%% of "
1836 "all EXTEND cells for this reason)", percentage);
1837 tor_free(m);
1839 } else {
1840 log_fn(LOG_WARN, domain,
1841 "EXTEND cell received, in a cell with type %d! Dropping.",
1842 cell->command);
1844 return 0;
1846 return circuit_extend(cell, circ);
1848 case RELAY_COMMAND_EXTENDED:
1849 case RELAY_COMMAND_EXTENDED2:
1850 if (!layer_hint) {
1851 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1852 "'extended' unsupported at non-origin. Dropping.");
1853 return 0;
1855 log_debug(domain,"Got an extended cell! Yay.");
1857 extended_cell_t extended_cell;
1858 if (extended_cell_parse(&extended_cell, rh.command,
1859 (const uint8_t*)cell->payload+RELAY_HEADER_SIZE,
1860 rh.length)<0) {
1861 log_warn(LD_PROTOCOL,
1862 "Can't parse EXTENDED cell; killing circuit.");
1863 return -END_CIRC_REASON_TORPROTOCOL;
1865 if ((reason = circuit_finish_handshake(TO_ORIGIN_CIRCUIT(circ),
1866 &extended_cell.created_cell)) < 0) {
1867 circuit_mark_for_close(circ, -reason);
1868 return 0; /* We don't want to cause a warning, so we mark the circuit
1869 * here. */
1872 if ((reason=circuit_send_next_onion_skin(TO_ORIGIN_CIRCUIT(circ)))<0) {
1873 log_info(domain,"circuit_send_next_onion_skin() failed.");
1874 return reason;
1876 /* Total all valid bytes delivered. */
1877 if (CIRCUIT_IS_ORIGIN(circ)) {
1878 circuit_read_valid_data(TO_ORIGIN_CIRCUIT(circ), rh.length);
1880 return 0;
1881 case RELAY_COMMAND_TRUNCATE:
1882 if (layer_hint) {
1883 log_fn(LOG_PROTOCOL_WARN, LD_APP,
1884 "'truncate' unsupported at origin. Dropping.");
1885 return 0;
1887 if (circ->n_hop) {
1888 if (circ->n_chan)
1889 log_warn(LD_BUG, "n_chan and n_hop set on the same circuit!");
1890 extend_info_free(circ->n_hop);
1891 circ->n_hop = NULL;
1892 tor_free(circ->n_chan_create_cell);
1893 circuit_set_state(circ, CIRCUIT_STATE_OPEN);
1895 if (circ->n_chan) {
1896 uint8_t trunc_reason = get_uint8(cell->payload + RELAY_HEADER_SIZE);
1897 circuit_synchronize_written_or_bandwidth(circ, CIRCUIT_N_CHAN);
1898 circuit_clear_cell_queue(circ, circ->n_chan);
1899 channel_send_destroy(circ->n_circ_id, circ->n_chan,
1900 trunc_reason);
1901 circuit_set_n_circid_chan(circ, 0, NULL);
1903 log_debug(LD_EXIT, "Processed 'truncate', replying.");
1905 char payload[1];
1906 payload[0] = (char)END_CIRC_REASON_REQUESTED;
1907 relay_send_command_from_edge(0, circ, RELAY_COMMAND_TRUNCATED,
1908 payload, sizeof(payload), NULL);
1910 return 0;
1911 case RELAY_COMMAND_TRUNCATED:
1912 if (!layer_hint) {
1913 log_fn(LOG_PROTOCOL_WARN, LD_EXIT,
1914 "'truncated' unsupported at non-origin. Dropping.");
1915 return 0;
1918 /* Count the truncated as valid, for completeness. The
1919 * circuit is being torn down anyway, though. */
1920 if (CIRCUIT_IS_ORIGIN(circ)) {
1921 circuit_read_valid_data(TO_ORIGIN_CIRCUIT(circ),
1922 rh.length);
1924 circuit_truncated(TO_ORIGIN_CIRCUIT(circ),
1925 get_uint8(cell->payload + RELAY_HEADER_SIZE));
1926 return 0;
1927 case RELAY_COMMAND_CONNECTED:
1928 if (conn) {
1929 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1930 "'connected' unsupported while open. Closing circ.");
1931 return -END_CIRC_REASON_TORPROTOCOL;
1934 if (CIRCUIT_IS_ORIGIN(circ)) {
1935 origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
1936 if (connection_half_edge_is_valid_connected(ocirc->half_streams,
1937 rh.stream_id)) {
1938 circuit_read_valid_data(ocirc, rh.length);
1939 log_info(domain,
1940 "connected cell on circ %u valid on half-closed "
1941 "stream id %d", ocirc->global_identifier, rh.stream_id);
1942 return 0;
1946 log_info(domain,
1947 "'connected' received on circid %u for streamid %d, "
1948 "no conn attached anymore. Ignoring.",
1949 (unsigned)circ->n_circ_id, rh.stream_id);
1950 return 0;
1951 case RELAY_COMMAND_SENDME:
1952 return process_sendme_cell(&rh, cell, circ, conn, layer_hint, domain);
1953 case RELAY_COMMAND_RESOLVE:
1954 if (layer_hint) {
1955 log_fn(LOG_PROTOCOL_WARN, LD_APP,
1956 "resolve request unsupported at AP; dropping.");
1957 return 0;
1958 } else if (conn) {
1959 log_fn(LOG_PROTOCOL_WARN, domain,
1960 "resolve request for known stream; dropping.");
1961 return 0;
1962 } else if (circ->purpose != CIRCUIT_PURPOSE_OR) {
1963 log_fn(LOG_PROTOCOL_WARN, domain,
1964 "resolve request on circ with purpose %d; dropping",
1965 circ->purpose);
1966 return 0;
1968 connection_exit_begin_resolve(cell, TO_OR_CIRCUIT(circ));
1969 return 0;
1970 case RELAY_COMMAND_RESOLVED:
1971 if (conn) {
1972 log_fn(LOG_PROTOCOL_WARN, domain,
1973 "'resolved' unsupported while open. Closing circ.");
1974 return -END_CIRC_REASON_TORPROTOCOL;
1977 if (CIRCUIT_IS_ORIGIN(circ)) {
1978 origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
1979 if (connection_half_edge_is_valid_resolved(ocirc->half_streams,
1980 rh.stream_id)) {
1981 circuit_read_valid_data(ocirc, rh.length);
1982 log_info(domain,
1983 "resolved cell on circ %u valid on half-closed "
1984 "stream id %d", ocirc->global_identifier, rh.stream_id);
1985 return 0;
1989 log_info(domain,
1990 "'resolved' received, no conn attached anymore. Ignoring.");
1991 return 0;
1992 case RELAY_COMMAND_ESTABLISH_INTRO:
1993 case RELAY_COMMAND_ESTABLISH_RENDEZVOUS:
1994 case RELAY_COMMAND_INTRODUCE1:
1995 case RELAY_COMMAND_INTRODUCE2:
1996 case RELAY_COMMAND_INTRODUCE_ACK:
1997 case RELAY_COMMAND_RENDEZVOUS1:
1998 case RELAY_COMMAND_RENDEZVOUS2:
1999 case RELAY_COMMAND_INTRO_ESTABLISHED:
2000 case RELAY_COMMAND_RENDEZVOUS_ESTABLISHED:
2001 rend_process_relay_cell(circ, layer_hint,
2002 rh.command, rh.length,
2003 cell->payload+RELAY_HEADER_SIZE);
2004 return 0;
2006 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
2007 "Received unknown relay command %d. Perhaps the other side is using "
2008 "a newer version of Tor? Dropping.",
2009 rh.command);
2010 return 0; /* for forward compatibility, don't kill the circuit */
2013 /** How many relay_data cells have we built, ever? */
2014 uint64_t stats_n_data_cells_packaged = 0;
2015 /** How many bytes of data have we put in relay_data cells have we built,
2016 * ever? This would be RELAY_PAYLOAD_SIZE*stats_n_data_cells_packaged if
2017 * every relay cell we ever sent were completely full of data. */
2018 uint64_t stats_n_data_bytes_packaged = 0;
2019 /** How many relay_data cells have we received, ever? */
2020 uint64_t stats_n_data_cells_received = 0;
2021 /** How many bytes of data have we received relay_data cells, ever? This would
2022 * be RELAY_PAYLOAD_SIZE*stats_n_data_cells_packaged if every relay cell we
2023 * ever received were completely full of data. */
2024 uint64_t stats_n_data_bytes_received = 0;
2026 /** If <b>conn</b> has an entire relay payload of bytes on its inbuf (or
2027 * <b>package_partial</b> is true), and the appropriate package windows aren't
2028 * empty, grab a cell and send it down the circuit.
2030 * If *<b>max_cells</b> is given, package no more than max_cells. Decrement
2031 * *<b>max_cells</b> by the number of cells packaged.
2033 * Return -1 (and send a RELAY_COMMAND_END cell if necessary) if conn should
2034 * be marked for close, else return 0.
2037 connection_edge_package_raw_inbuf(edge_connection_t *conn, int package_partial,
2038 int *max_cells)
2040 size_t bytes_to_process, length;
2041 char payload[CELL_PAYLOAD_SIZE];
2042 circuit_t *circ;
2043 const unsigned domain = conn->base_.type == CONN_TYPE_AP ? LD_APP : LD_EXIT;
2044 int sending_from_optimistic = 0;
2045 entry_connection_t *entry_conn =
2046 conn->base_.type == CONN_TYPE_AP ? EDGE_TO_ENTRY_CONN(conn) : NULL;
2047 const int sending_optimistically =
2048 entry_conn &&
2049 conn->base_.type == CONN_TYPE_AP &&
2050 conn->base_.state != AP_CONN_STATE_OPEN;
2051 crypt_path_t *cpath_layer = conn->cpath_layer;
2053 tor_assert(conn);
2055 if (conn->base_.marked_for_close) {
2056 log_warn(LD_BUG,
2057 "called on conn that's already marked for close at %s:%d.",
2058 conn->base_.marked_for_close_file, conn->base_.marked_for_close);
2059 return 0;
2062 if (max_cells && *max_cells <= 0)
2063 return 0;
2065 repeat_connection_edge_package_raw_inbuf:
2067 circ = circuit_get_by_edge_conn(conn);
2068 if (!circ) {
2069 log_info(domain,"conn has no circuit! Closing.");
2070 conn->end_reason = END_STREAM_REASON_CANT_ATTACH;
2071 return -1;
2074 if (circuit_consider_stop_edge_reading(circ, cpath_layer))
2075 return 0;
2077 if (conn->package_window <= 0) {
2078 log_info(domain,"called with package_window %d. Skipping.",
2079 conn->package_window);
2080 connection_stop_reading(TO_CONN(conn));
2081 return 0;
2084 sending_from_optimistic = entry_conn &&
2085 entry_conn->sending_optimistic_data != NULL;
2087 if (PREDICT_UNLIKELY(sending_from_optimistic)) {
2088 bytes_to_process = buf_datalen(entry_conn->sending_optimistic_data);
2089 if (PREDICT_UNLIKELY(!bytes_to_process)) {
2090 log_warn(LD_BUG, "sending_optimistic_data was non-NULL but empty");
2091 bytes_to_process = connection_get_inbuf_len(TO_CONN(conn));
2092 sending_from_optimistic = 0;
2094 } else {
2095 bytes_to_process = connection_get_inbuf_len(TO_CONN(conn));
2098 if (!bytes_to_process)
2099 return 0;
2101 if (!package_partial && bytes_to_process < RELAY_PAYLOAD_SIZE)
2102 return 0;
2104 if (bytes_to_process > RELAY_PAYLOAD_SIZE) {
2105 length = RELAY_PAYLOAD_SIZE;
2106 } else {
2107 length = bytes_to_process;
2109 stats_n_data_bytes_packaged += length;
2110 stats_n_data_cells_packaged += 1;
2112 if (PREDICT_UNLIKELY(sending_from_optimistic)) {
2113 /* XXXX We could be more efficient here by sometimes packing
2114 * previously-sent optimistic data in the same cell with data
2115 * from the inbuf. */
2116 buf_get_bytes(entry_conn->sending_optimistic_data, payload, length);
2117 if (!buf_datalen(entry_conn->sending_optimistic_data)) {
2118 buf_free(entry_conn->sending_optimistic_data);
2119 entry_conn->sending_optimistic_data = NULL;
2121 } else {
2122 connection_buf_get_bytes(payload, length, TO_CONN(conn));
2125 log_debug(domain,TOR_SOCKET_T_FORMAT": Packaging %d bytes (%d waiting).",
2126 conn->base_.s,
2127 (int)length, (int)connection_get_inbuf_len(TO_CONN(conn)));
2129 if (sending_optimistically && !sending_from_optimistic) {
2130 /* This is new optimistic data; remember it in case we need to detach and
2131 retry */
2132 if (!entry_conn->pending_optimistic_data)
2133 entry_conn->pending_optimistic_data = buf_new();
2134 buf_add(entry_conn->pending_optimistic_data, payload, length);
2137 if (connection_edge_send_command(conn, RELAY_COMMAND_DATA,
2138 payload, length) < 0 ) {
2139 /* circuit got marked for close, don't continue, don't need to mark conn */
2140 return 0;
2143 /* Handle the circuit-level SENDME package window. */
2144 if (sendme_note_circuit_data_packaged(circ, cpath_layer) < 0) {
2145 /* Package window has gone under 0. Protocol issue. */
2146 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
2147 "Circuit package window is below 0. Closing circuit.");
2148 conn->end_reason = END_STREAM_REASON_TORPROTOCOL;
2149 return -1;
2152 /* Handle the stream-level SENDME package window. */
2153 if (sendme_note_stream_data_packaged(conn) < 0) {
2154 connection_stop_reading(TO_CONN(conn));
2155 log_debug(domain,"conn->package_window reached 0.");
2156 circuit_consider_stop_edge_reading(circ, cpath_layer);
2157 return 0; /* don't process the inbuf any more */
2159 log_debug(domain,"conn->package_window is now %d",conn->package_window);
2161 if (max_cells) {
2162 *max_cells -= 1;
2163 if (*max_cells <= 0)
2164 return 0;
2167 /* handle more if there's more, or return 0 if there isn't */
2168 goto repeat_connection_edge_package_raw_inbuf;
2171 /** The circuit <b>circ</b> has received a circuit-level sendme
2172 * (on hop <b>layer_hint</b>, if we're the OP). Go through all the
2173 * attached streams and let them resume reading and packaging, if
2174 * their stream windows allow it.
2176 static void
2177 circuit_resume_edge_reading(circuit_t *circ, crypt_path_t *layer_hint)
2179 if (circuit_queue_streams_are_blocked(circ)) {
2180 log_debug(layer_hint?LD_APP:LD_EXIT,"Too big queue, no resuming");
2181 return;
2183 log_debug(layer_hint?LD_APP:LD_EXIT,"resuming");
2185 if (CIRCUIT_IS_ORIGIN(circ))
2186 circuit_resume_edge_reading_helper(TO_ORIGIN_CIRCUIT(circ)->p_streams,
2187 circ, layer_hint);
2188 else
2189 circuit_resume_edge_reading_helper(TO_OR_CIRCUIT(circ)->n_streams,
2190 circ, layer_hint);
2193 /** A helper function for circuit_resume_edge_reading() above.
2194 * The arguments are the same, except that <b>conn</b> is the head
2195 * of a linked list of edge streams that should each be considered.
2197 static int
2198 circuit_resume_edge_reading_helper(edge_connection_t *first_conn,
2199 circuit_t *circ,
2200 crypt_path_t *layer_hint)
2202 edge_connection_t *conn;
2203 int n_packaging_streams, n_streams_left;
2204 int packaged_this_round;
2205 int cells_on_queue;
2206 int cells_per_conn;
2207 edge_connection_t *chosen_stream = NULL;
2208 int max_to_package;
2210 if (first_conn == NULL) {
2211 /* Don't bother to try to do the rest of this if there are no connections
2212 * to resume. */
2213 return 0;
2216 /* How many cells do we have space for? It will be the minimum of
2217 * the number needed to exhaust the package window, and the minimum
2218 * needed to fill the cell queue. */
2219 max_to_package = circ->package_window;
2220 if (CIRCUIT_IS_ORIGIN(circ)) {
2221 cells_on_queue = circ->n_chan_cells.n;
2222 } else {
2223 or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
2224 cells_on_queue = or_circ->p_chan_cells.n;
2226 if (CELL_QUEUE_HIGHWATER_SIZE - cells_on_queue < max_to_package)
2227 max_to_package = CELL_QUEUE_HIGHWATER_SIZE - cells_on_queue;
2229 /* Once we used to start listening on the streams in the order they
2230 * appeared in the linked list. That leads to starvation on the
2231 * streams that appeared later on the list, since the first streams
2232 * would always get to read first. Instead, we just pick a random
2233 * stream on the list, and enable reading for streams starting at that
2234 * point (and wrapping around as if the list were circular). It would
2235 * probably be better to actually remember which streams we've
2236 * serviced in the past, but this is simple and effective. */
2238 /* Select a stream uniformly at random from the linked list. We
2239 * don't need cryptographic randomness here. */
2241 int num_streams = 0;
2242 for (conn = first_conn; conn; conn = conn->next_stream) {
2243 num_streams++;
2245 if (crypto_fast_rng_one_in_n(get_thread_fast_rng(), num_streams)) {
2246 chosen_stream = conn;
2248 /* Invariant: chosen_stream has been chosen uniformly at random from
2249 * among the first num_streams streams on first_conn.
2251 * (Note that we iterate over every stream on the circuit, so that after
2252 * we've considered the first stream, we've chosen it with P=1; and
2253 * after we consider the second stream, we've switched to it with P=1/2
2254 * and stayed with the first stream with P=1/2; and after we've
2255 * considered the third stream, we've switched to it with P=1/3 and
2256 * remained with one of the first two streams with P=(2/3), giving each
2257 * one P=(1/2)(2/3) )=(1/3).) */
2261 /* Count how many non-marked streams there are that have anything on
2262 * their inbuf, and enable reading on all of the connections. */
2263 n_packaging_streams = 0;
2264 /* Activate reading starting from the chosen stream */
2265 for (conn=chosen_stream; conn; conn = conn->next_stream) {
2266 /* Start reading for the streams starting from here */
2267 if (conn->base_.marked_for_close || conn->package_window <= 0)
2268 continue;
2269 if (!layer_hint || conn->cpath_layer == layer_hint) {
2270 connection_start_reading(TO_CONN(conn));
2272 if (connection_get_inbuf_len(TO_CONN(conn)) > 0)
2273 ++n_packaging_streams;
2276 /* Go back and do the ones we skipped, circular-style */
2277 for (conn = first_conn; conn != chosen_stream; conn = conn->next_stream) {
2278 if (conn->base_.marked_for_close || conn->package_window <= 0)
2279 continue;
2280 if (!layer_hint || conn->cpath_layer == layer_hint) {
2281 connection_start_reading(TO_CONN(conn));
2283 if (connection_get_inbuf_len(TO_CONN(conn)) > 0)
2284 ++n_packaging_streams;
2288 if (n_packaging_streams == 0) /* avoid divide-by-zero */
2289 return 0;
2291 again:
2293 cells_per_conn = CEIL_DIV(max_to_package, n_packaging_streams);
2295 packaged_this_round = 0;
2296 n_streams_left = 0;
2298 /* Iterate over all connections. Package up to cells_per_conn cells on
2299 * each. Update packaged_this_round with the total number of cells
2300 * packaged, and n_streams_left with the number that still have data to
2301 * package.
2303 for (conn=first_conn; conn; conn=conn->next_stream) {
2304 if (conn->base_.marked_for_close || conn->package_window <= 0)
2305 continue;
2306 if (!layer_hint || conn->cpath_layer == layer_hint) {
2307 int n = cells_per_conn, r;
2308 /* handle whatever might still be on the inbuf */
2309 r = connection_edge_package_raw_inbuf(conn, 1, &n);
2311 /* Note how many we packaged */
2312 packaged_this_round += (cells_per_conn-n);
2314 if (r<0) {
2315 /* Problem while packaging. (We already sent an end cell if
2316 * possible) */
2317 connection_mark_for_close(TO_CONN(conn));
2318 continue;
2321 /* If there's still data to read, we'll be coming back to this stream. */
2322 if (connection_get_inbuf_len(TO_CONN(conn)))
2323 ++n_streams_left;
2325 /* If the circuit won't accept any more data, return without looking
2326 * at any more of the streams. Any connections that should be stopped
2327 * have already been stopped by connection_edge_package_raw_inbuf. */
2328 if (circuit_consider_stop_edge_reading(circ, layer_hint))
2329 return -1;
2330 /* XXXX should we also stop immediately if we fill up the cell queue?
2331 * Probably. */
2335 /* If we made progress, and we are willing to package more, and there are
2336 * any streams left that want to package stuff... try again!
2338 if (packaged_this_round && packaged_this_round < max_to_package &&
2339 n_streams_left) {
2340 max_to_package -= packaged_this_round;
2341 n_packaging_streams = n_streams_left;
2342 goto again;
2345 return 0;
2348 /** Check if the package window for <b>circ</b> is empty (at
2349 * hop <b>layer_hint</b> if it's defined).
2351 * If yes, tell edge streams to stop reading and return 1.
2352 * Else return 0.
2354 static int
2355 circuit_consider_stop_edge_reading(circuit_t *circ, crypt_path_t *layer_hint)
2357 edge_connection_t *conn = NULL;
2358 unsigned domain = layer_hint ? LD_APP : LD_EXIT;
2360 if (!layer_hint) {
2361 or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
2362 log_debug(domain,"considering circ->package_window %d",
2363 circ->package_window);
2364 if (circ->package_window <= 0) {
2365 log_debug(domain,"yes, not-at-origin. stopped.");
2366 for (conn = or_circ->n_streams; conn; conn=conn->next_stream)
2367 connection_stop_reading(TO_CONN(conn));
2368 return 1;
2370 return 0;
2372 /* else, layer hint is defined, use it */
2373 log_debug(domain,"considering layer_hint->package_window %d",
2374 layer_hint->package_window);
2375 if (layer_hint->package_window <= 0) {
2376 log_debug(domain,"yes, at-origin. stopped.");
2377 for (conn = TO_ORIGIN_CIRCUIT(circ)->p_streams; conn;
2378 conn=conn->next_stream) {
2379 if (conn->cpath_layer == layer_hint)
2380 connection_stop_reading(TO_CONN(conn));
2382 return 1;
2384 return 0;
2387 /** The total number of cells we have allocated. */
2388 static size_t total_cells_allocated = 0;
2390 /** Release storage held by <b>cell</b>. */
2391 static inline void
2392 packed_cell_free_unchecked(packed_cell_t *cell)
2394 --total_cells_allocated;
2395 tor_free(cell);
2398 /** Allocate and return a new packed_cell_t. */
2399 STATIC packed_cell_t *
2400 packed_cell_new(void)
2402 ++total_cells_allocated;
2403 return tor_malloc_zero(sizeof(packed_cell_t));
2406 /** Return a packed cell used outside by channel_t lower layer */
2407 void
2408 packed_cell_free_(packed_cell_t *cell)
2410 if (!cell)
2411 return;
2412 packed_cell_free_unchecked(cell);
2415 /** Log current statistics for cell pool allocation at log level
2416 * <b>severity</b>. */
2417 void
2418 dump_cell_pool_usage(int severity)
2420 int n_circs = 0;
2421 int n_cells = 0;
2422 SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, c) {
2423 n_cells += c->n_chan_cells.n;
2424 if (!CIRCUIT_IS_ORIGIN(c))
2425 n_cells += TO_OR_CIRCUIT(c)->p_chan_cells.n;
2426 ++n_circs;
2428 SMARTLIST_FOREACH_END(c);
2429 tor_log(severity, LD_MM,
2430 "%d cells allocated on %d circuits. %d cells leaked.",
2431 n_cells, n_circs, (int)total_cells_allocated - n_cells);
2434 /** Allocate a new copy of packed <b>cell</b>. */
2435 static inline packed_cell_t *
2436 packed_cell_copy(const cell_t *cell, int wide_circ_ids)
2438 packed_cell_t *c = packed_cell_new();
2439 cell_pack(c, cell, wide_circ_ids);
2440 return c;
2443 /** Append <b>cell</b> to the end of <b>queue</b>. */
2444 void
2445 cell_queue_append(cell_queue_t *queue, packed_cell_t *cell)
2447 TOR_SIMPLEQ_INSERT_TAIL(&queue->head, cell, next);
2448 ++queue->n;
2451 /** Append a newly allocated copy of <b>cell</b> to the end of the
2452 * <b>exitward</b> (or app-ward) <b>queue</b> of <b>circ</b>. If
2453 * <b>use_stats</b> is true, record statistics about the cell.
2455 void
2456 cell_queue_append_packed_copy(circuit_t *circ, cell_queue_t *queue,
2457 int exitward, const cell_t *cell,
2458 int wide_circ_ids, int use_stats)
2460 packed_cell_t *copy = packed_cell_copy(cell, wide_circ_ids);
2461 (void)circ;
2462 (void)exitward;
2463 (void)use_stats;
2465 copy->inserted_timestamp = monotime_coarse_get_stamp();
2467 cell_queue_append(queue, copy);
2470 /** Initialize <b>queue</b> as an empty cell queue. */
2471 void
2472 cell_queue_init(cell_queue_t *queue)
2474 memset(queue, 0, sizeof(cell_queue_t));
2475 TOR_SIMPLEQ_INIT(&queue->head);
2478 /** Remove and free every cell in <b>queue</b>. */
2479 void
2480 cell_queue_clear(cell_queue_t *queue)
2482 packed_cell_t *cell;
2483 while ((cell = TOR_SIMPLEQ_FIRST(&queue->head))) {
2484 TOR_SIMPLEQ_REMOVE_HEAD(&queue->head, next);
2485 packed_cell_free_unchecked(cell);
2487 TOR_SIMPLEQ_INIT(&queue->head);
2488 queue->n = 0;
2491 /** Extract and return the cell at the head of <b>queue</b>; return NULL if
2492 * <b>queue</b> is empty. */
2493 STATIC packed_cell_t *
2494 cell_queue_pop(cell_queue_t *queue)
2496 packed_cell_t *cell = TOR_SIMPLEQ_FIRST(&queue->head);
2497 if (!cell)
2498 return NULL;
2499 TOR_SIMPLEQ_REMOVE_HEAD(&queue->head, next);
2500 --queue->n;
2501 return cell;
2504 /** Initialize <b>queue</b> as an empty cell queue. */
2505 void
2506 destroy_cell_queue_init(destroy_cell_queue_t *queue)
2508 memset(queue, 0, sizeof(destroy_cell_queue_t));
2509 TOR_SIMPLEQ_INIT(&queue->head);
2512 /** Remove and free every cell in <b>queue</b>. */
2513 void
2514 destroy_cell_queue_clear(destroy_cell_queue_t *queue)
2516 destroy_cell_t *cell;
2517 while ((cell = TOR_SIMPLEQ_FIRST(&queue->head))) {
2518 TOR_SIMPLEQ_REMOVE_HEAD(&queue->head, next);
2519 tor_free(cell);
2521 TOR_SIMPLEQ_INIT(&queue->head);
2522 queue->n = 0;
2525 /** Extract and return the cell at the head of <b>queue</b>; return NULL if
2526 * <b>queue</b> is empty. */
2527 STATIC destroy_cell_t *
2528 destroy_cell_queue_pop(destroy_cell_queue_t *queue)
2530 destroy_cell_t *cell = TOR_SIMPLEQ_FIRST(&queue->head);
2531 if (!cell)
2532 return NULL;
2533 TOR_SIMPLEQ_REMOVE_HEAD(&queue->head, next);
2534 --queue->n;
2535 return cell;
2538 /** Append a destroy cell for <b>circid</b> to <b>queue</b>. */
2539 void
2540 destroy_cell_queue_append(destroy_cell_queue_t *queue,
2541 circid_t circid,
2542 uint8_t reason)
2544 destroy_cell_t *cell = tor_malloc_zero(sizeof(destroy_cell_t));
2545 cell->circid = circid;
2546 cell->reason = reason;
2547 /* Not yet used, but will be required for OOM handling. */
2548 cell->inserted_timestamp = monotime_coarse_get_stamp();
2550 TOR_SIMPLEQ_INSERT_TAIL(&queue->head, cell, next);
2551 ++queue->n;
2554 /** Convert a destroy_cell_t to a newly allocated cell_t. Frees its input. */
2555 static packed_cell_t *
2556 destroy_cell_to_packed_cell(destroy_cell_t *inp, int wide_circ_ids)
2558 packed_cell_t *packed = packed_cell_new();
2559 cell_t cell;
2560 memset(&cell, 0, sizeof(cell));
2561 cell.circ_id = inp->circid;
2562 cell.command = CELL_DESTROY;
2563 cell.payload[0] = inp->reason;
2564 cell_pack(packed, &cell, wide_circ_ids);
2566 tor_free(inp);
2567 return packed;
2570 /** Return the total number of bytes used for each packed_cell in a queue.
2571 * Approximate. */
2572 size_t
2573 packed_cell_mem_cost(void)
2575 return sizeof(packed_cell_t);
2578 /* DOCDOC */
2579 size_t
2580 cell_queues_get_total_allocation(void)
2582 return total_cells_allocated * packed_cell_mem_cost();
2585 /** How long after we've been low on memory should we try to conserve it? */
2586 #define MEMORY_PRESSURE_INTERVAL (30*60)
2588 /** The time at which we were last low on memory. */
2589 static time_t last_time_under_memory_pressure = 0;
2591 /** Check whether we've got too much space used for cells. If so,
2592 * call the OOM handler and return 1. Otherwise, return 0. */
2593 STATIC int
2594 cell_queues_check_size(void)
2596 time_t now = time(NULL);
2597 size_t alloc = cell_queues_get_total_allocation();
2598 alloc += half_streams_get_total_allocation();
2599 alloc += buf_get_total_allocation();
2600 alloc += tor_compress_get_total_allocation();
2601 const size_t rend_cache_total = rend_cache_get_total_allocation();
2602 alloc += rend_cache_total;
2603 const size_t geoip_client_cache_total =
2604 geoip_client_cache_total_allocation();
2605 alloc += geoip_client_cache_total;
2606 const size_t dns_cache_total = dns_cache_total_allocation();
2607 alloc += dns_cache_total;
2608 if (alloc >= get_options()->MaxMemInQueues_low_threshold) {
2609 last_time_under_memory_pressure = approx_time();
2610 if (alloc >= get_options()->MaxMemInQueues) {
2611 /* If we're spending over 20% of the memory limit on hidden service
2612 * descriptors, free them until we're down to 10%. Do the same for geoip
2613 * client cache. */
2614 if (rend_cache_total > get_options()->MaxMemInQueues / 5) {
2615 const size_t bytes_to_remove =
2616 rend_cache_total - (size_t)(get_options()->MaxMemInQueues / 10);
2617 alloc -= hs_cache_handle_oom(now, bytes_to_remove);
2619 if (geoip_client_cache_total > get_options()->MaxMemInQueues / 5) {
2620 const size_t bytes_to_remove =
2621 geoip_client_cache_total -
2622 (size_t)(get_options()->MaxMemInQueues / 10);
2623 alloc -= geoip_client_cache_handle_oom(now, bytes_to_remove);
2625 if (dns_cache_total > get_options()->MaxMemInQueues / 5) {
2626 const size_t bytes_to_remove =
2627 dns_cache_total - (size_t)(get_options()->MaxMemInQueues / 10);
2628 alloc -= dns_cache_handle_oom(now, bytes_to_remove);
2630 circuits_handle_oom(alloc);
2631 return 1;
2634 return 0;
2637 /** Return true if we've been under memory pressure in the last
2638 * MEMORY_PRESSURE_INTERVAL seconds. */
2640 have_been_under_memory_pressure(void)
2642 return last_time_under_memory_pressure + MEMORY_PRESSURE_INTERVAL
2643 < approx_time();
2647 * Update the number of cells available on the circuit's n_chan or p_chan's
2648 * circuit mux.
2650 void
2651 update_circuit_on_cmux_(circuit_t *circ, cell_direction_t direction,
2652 const char *file, int lineno)
2654 channel_t *chan = NULL;
2655 or_circuit_t *or_circ = NULL;
2656 circuitmux_t *cmux = NULL;
2658 tor_assert(circ);
2660 /* Okay, get the channel */
2661 if (direction == CELL_DIRECTION_OUT) {
2662 chan = circ->n_chan;
2663 } else {
2664 or_circ = TO_OR_CIRCUIT(circ);
2665 chan = or_circ->p_chan;
2668 tor_assert(chan);
2669 tor_assert(chan->cmux);
2671 /* Now get the cmux */
2672 cmux = chan->cmux;
2674 /* Cmux sanity check */
2675 if (! circuitmux_is_circuit_attached(cmux, circ)) {
2676 log_warn(LD_BUG, "called on non-attached circuit from %s:%d",
2677 file, lineno);
2678 return;
2680 tor_assert(circuitmux_attached_circuit_direction(cmux, circ) == direction);
2682 /* Update the number of cells we have for the circuit mux */
2683 if (direction == CELL_DIRECTION_OUT) {
2684 circuitmux_set_num_cells(cmux, circ, circ->n_chan_cells.n);
2685 } else {
2686 circuitmux_set_num_cells(cmux, circ, or_circ->p_chan_cells.n);
2690 /** Remove all circuits from the cmux on <b>chan</b>.
2692 * If <b>circuits_out</b> is non-NULL, add all detached circuits to
2693 * <b>circuits_out</b>.
2695 void
2696 channel_unlink_all_circuits(channel_t *chan, smartlist_t *circuits_out)
2698 tor_assert(chan);
2699 tor_assert(chan->cmux);
2701 circuitmux_detach_all_circuits(chan->cmux, circuits_out);
2702 chan->num_n_circuits = 0;
2703 chan->num_p_circuits = 0;
2706 /** Block (if <b>block</b> is true) or unblock (if <b>block</b> is false)
2707 * every edge connection that is using <b>circ</b> to write to <b>chan</b>,
2708 * and start or stop reading as appropriate.
2710 * If <b>stream_id</b> is nonzero, block only the edge connection whose
2711 * stream_id matches it.
2713 * Returns the number of streams whose status we changed.
2715 static int
2716 set_streams_blocked_on_circ(circuit_t *circ, channel_t *chan,
2717 int block, streamid_t stream_id)
2719 edge_connection_t *edge = NULL;
2720 int n = 0;
2721 if (circ->n_chan == chan) {
2722 circ->streams_blocked_on_n_chan = block;
2723 if (CIRCUIT_IS_ORIGIN(circ))
2724 edge = TO_ORIGIN_CIRCUIT(circ)->p_streams;
2725 } else {
2726 circ->streams_blocked_on_p_chan = block;
2727 tor_assert(!CIRCUIT_IS_ORIGIN(circ));
2728 edge = TO_OR_CIRCUIT(circ)->n_streams;
2731 for (; edge; edge = edge->next_stream) {
2732 connection_t *conn = TO_CONN(edge);
2733 if (stream_id && edge->stream_id != stream_id)
2734 continue;
2736 if (edge->edge_blocked_on_circ != block) {
2737 ++n;
2738 edge->edge_blocked_on_circ = block;
2741 if (!conn->read_event) {
2742 /* This connection is a placeholder for something; probably a DNS
2743 * request. It can't actually stop or start reading.*/
2744 continue;
2747 if (block) {
2748 if (connection_is_reading(conn))
2749 connection_stop_reading(conn);
2750 } else {
2751 /* Is this right? */
2752 if (!connection_is_reading(conn))
2753 connection_start_reading(conn);
2757 return n;
2760 /** Extract the command from a packed cell. */
2761 uint8_t
2762 packed_cell_get_command(const packed_cell_t *cell, int wide_circ_ids)
2764 if (wide_circ_ids) {
2765 return get_uint8(cell->body+4);
2766 } else {
2767 return get_uint8(cell->body+2);
2771 /** Extract the circuit ID from a packed cell. */
2772 circid_t
2773 packed_cell_get_circid(const packed_cell_t *cell, int wide_circ_ids)
2775 if (wide_circ_ids) {
2776 return ntohl(get_uint32(cell->body));
2777 } else {
2778 return ntohs(get_uint16(cell->body));
2782 /** Pull as many cells as possible (but no more than <b>max</b>) from the
2783 * queue of the first active circuit on <b>chan</b>, and write them to
2784 * <b>chan</b>-&gt;outbuf. Return the number of cells written. Advance
2785 * the active circuit pointer to the next active circuit in the ring. */
2786 MOCK_IMPL(int,
2787 channel_flush_from_first_active_circuit, (channel_t *chan, int max))
2789 circuitmux_t *cmux = NULL;
2790 int n_flushed = 0;
2791 cell_queue_t *queue;
2792 destroy_cell_queue_t *destroy_queue=NULL;
2793 circuit_t *circ;
2794 or_circuit_t *or_circ;
2795 int streams_blocked;
2796 packed_cell_t *cell;
2798 /* Get the cmux */
2799 tor_assert(chan);
2800 tor_assert(chan->cmux);
2801 cmux = chan->cmux;
2803 /* Main loop: pick a circuit, send a cell, update the cmux */
2804 while (n_flushed < max) {
2805 circ = circuitmux_get_first_active_circuit(cmux, &destroy_queue);
2806 if (destroy_queue) {
2807 destroy_cell_t *dcell;
2808 /* this code is duplicated from some of the logic below. Ugly! XXXX */
2809 /* If we are given a destroy_queue here, then it is required to be
2810 * nonempty... */
2811 tor_assert(destroy_queue->n > 0);
2812 dcell = destroy_cell_queue_pop(destroy_queue);
2813 /* ...and pop() will always yield a cell from a nonempty queue. */
2814 tor_assert(dcell);
2815 /* frees dcell */
2816 cell = destroy_cell_to_packed_cell(dcell, chan->wide_circ_ids);
2817 /* Send the DESTROY cell. It is very unlikely that this fails but just
2818 * in case, get rid of the channel. */
2819 if (channel_write_packed_cell(chan, cell) < 0) {
2820 /* The cell has been freed. */
2821 channel_mark_for_close(chan);
2822 continue;
2824 /* Update the cmux destroy counter */
2825 circuitmux_notify_xmit_destroy(cmux);
2826 cell = NULL;
2827 ++n_flushed;
2828 continue;
2830 /* If it returns NULL, no cells left to send */
2831 if (!circ) break;
2833 if (circ->n_chan == chan) {
2834 queue = &circ->n_chan_cells;
2835 streams_blocked = circ->streams_blocked_on_n_chan;
2836 } else {
2837 or_circ = TO_OR_CIRCUIT(circ);
2838 tor_assert(or_circ->p_chan == chan);
2839 queue = &TO_OR_CIRCUIT(circ)->p_chan_cells;
2840 streams_blocked = circ->streams_blocked_on_p_chan;
2843 /* Circuitmux told us this was active, so it should have cells */
2844 if (/*BUG(*/ queue->n == 0 /*)*/) {
2845 log_warn(LD_BUG, "Found a supposedly active circuit with no cells "
2846 "to send. Trying to recover.");
2847 circuitmux_set_num_cells(cmux, circ, 0);
2848 if (! circ->marked_for_close)
2849 circuit_mark_for_close(circ, END_CIRC_REASON_INTERNAL);
2850 continue;
2853 tor_assert(queue->n > 0);
2856 * Get just one cell here; once we've sent it, that can change the circuit
2857 * selection, so we have to loop around for another even if this circuit
2858 * has more than one.
2860 cell = cell_queue_pop(queue);
2862 /* Calculate the exact time that this cell has spent in the queue. */
2863 if (get_options()->CellStatistics ||
2864 get_options()->TestingEnableCellStatsEvent) {
2865 uint32_t timestamp_now = monotime_coarse_get_stamp();
2866 uint32_t msec_waiting =
2867 (uint32_t) monotime_coarse_stamp_units_to_approx_msec(
2868 timestamp_now - cell->inserted_timestamp);
2870 if (get_options()->CellStatistics && !CIRCUIT_IS_ORIGIN(circ)) {
2871 or_circ = TO_OR_CIRCUIT(circ);
2872 or_circ->total_cell_waiting_time += msec_waiting;
2873 or_circ->processed_cells++;
2876 if (get_options()->TestingEnableCellStatsEvent) {
2877 uint8_t command = packed_cell_get_command(cell, chan->wide_circ_ids);
2879 testing_cell_stats_entry_t *ent =
2880 tor_malloc_zero(sizeof(testing_cell_stats_entry_t));
2881 ent->command = command;
2882 ent->waiting_time = msec_waiting / 10;
2883 ent->removed = 1;
2884 if (circ->n_chan == chan)
2885 ent->exitward = 1;
2886 if (!circ->testing_cell_stats)
2887 circ->testing_cell_stats = smartlist_new();
2888 smartlist_add(circ->testing_cell_stats, ent);
2892 /* If we just flushed our queue and this circuit is used for a
2893 * tunneled directory request, possibly advance its state. */
2894 if (queue->n == 0 && chan->dirreq_id)
2895 geoip_change_dirreq_state(chan->dirreq_id,
2896 DIRREQ_TUNNELED,
2897 DIRREQ_CIRC_QUEUE_FLUSHED);
2899 /* Now send the cell. It is very unlikely that this fails but just in
2900 * case, get rid of the channel. */
2901 if (channel_write_packed_cell(chan, cell) < 0) {
2902 /* The cell has been freed at this point. */
2903 channel_mark_for_close(chan);
2904 continue;
2906 cell = NULL;
2909 * Don't packed_cell_free_unchecked(cell) here because the channel will
2910 * do so when it gets out of the channel queue (probably already did, in
2911 * which case that was an immediate double-free bug).
2914 /* Update the counter */
2915 ++n_flushed;
2918 * Now update the cmux; tell it we've just sent a cell, and how many
2919 * we have left.
2921 circuitmux_notify_xmit_cells(cmux, circ, 1);
2922 circuitmux_set_num_cells(cmux, circ, queue->n);
2923 if (queue->n == 0)
2924 log_debug(LD_GENERAL, "Made a circuit inactive.");
2926 /* Is the cell queue low enough to unblock all the streams that are waiting
2927 * to write to this circuit? */
2928 if (streams_blocked && queue->n <= CELL_QUEUE_LOWWATER_SIZE)
2929 set_streams_blocked_on_circ(circ, chan, 0, 0); /* unblock streams */
2931 /* If n_flushed < max still, loop around and pick another circuit */
2934 /* Okay, we're done sending now */
2935 return n_flushed;
2938 /* Minimum value is the maximum circuit window size.
2940 * SENDME cells makes it that we can control how many cells can be inflight on
2941 * a circuit from end to end. This logic makes it that on any circuit cell
2942 * queue, we have a maximum of cells possible.
2944 * Because the Tor protocol allows for a client to exit at any hop in a
2945 * circuit and a circuit can be of a maximum of 8 hops, so in theory the
2946 * normal worst case will be the circuit window start value times the maximum
2947 * number of hops (8). Having more cells then that means something is wrong.
2949 * However, because padding cells aren't counted in the package window, we set
2950 * the maximum size to a reasonably large size for which we expect that we'll
2951 * never reach in theory. And if we ever do because of future changes, we'll
2952 * be able to control it with a consensus parameter.
2954 * XXX: Unfortunately, END cells aren't accounted for in the circuit window
2955 * which means that for instance if a client opens 8001 streams, the 8001
2956 * following END cells will queue up in the circuit which will get closed if
2957 * the max limit is 8000. Which is sad because it is allowed by the Tor
2958 * protocol. But, we need an upper bound on circuit queue in order to avoid
2959 * DoS memory pressure so the default size is a middle ground between not
2960 * having any limit and having a very restricted one. This is why we can also
2961 * control it through a consensus parameter. */
2962 #define RELAY_CIRC_CELL_QUEUE_SIZE_MIN CIRCWINDOW_START_MAX
2963 /* We can't have a consensus parameter above this value. */
2964 #define RELAY_CIRC_CELL_QUEUE_SIZE_MAX INT32_MAX
2965 /* Default value is set to a large value so we can handle padding cells
2966 * properly which aren't accounted for in the SENDME window. Default is 50000
2967 * allowed cells in the queue resulting in ~25MB. */
2968 #define RELAY_CIRC_CELL_QUEUE_SIZE_DEFAULT \
2969 (50 * RELAY_CIRC_CELL_QUEUE_SIZE_MIN)
2971 /* The maximum number of cell a circuit queue can contain. This is updated at
2972 * every new consensus and controlled by a parameter. */
2973 static int32_t max_circuit_cell_queue_size =
2974 RELAY_CIRC_CELL_QUEUE_SIZE_DEFAULT;
2976 /* Called when the consensus has changed. At this stage, the global consensus
2977 * object has NOT been updated. It is called from
2978 * notify_before_networkstatus_changes(). */
2979 void
2980 relay_consensus_has_changed(const networkstatus_t *ns)
2982 tor_assert(ns);
2984 /* Update the circuit max cell queue size from the consensus. */
2985 max_circuit_cell_queue_size =
2986 networkstatus_get_param(ns, "circ_max_cell_queue_size",
2987 RELAY_CIRC_CELL_QUEUE_SIZE_DEFAULT,
2988 RELAY_CIRC_CELL_QUEUE_SIZE_MIN,
2989 RELAY_CIRC_CELL_QUEUE_SIZE_MAX);
2992 /** Add <b>cell</b> to the queue of <b>circ</b> writing to <b>chan</b>
2993 * transmitting in <b>direction</b>.
2995 * The given <b>cell</b> is copied onto the circuit queue so the caller must
2996 * cleanup the memory.
2998 * This function is part of the fast path. */
2999 void
3000 append_cell_to_circuit_queue(circuit_t *circ, channel_t *chan,
3001 cell_t *cell, cell_direction_t direction,
3002 streamid_t fromstream)
3004 or_circuit_t *orcirc = NULL;
3005 cell_queue_t *queue;
3006 int streams_blocked;
3007 int exitward;
3008 if (circ->marked_for_close)
3009 return;
3011 exitward = (direction == CELL_DIRECTION_OUT);
3012 if (exitward) {
3013 queue = &circ->n_chan_cells;
3014 streams_blocked = circ->streams_blocked_on_n_chan;
3015 } else {
3016 orcirc = TO_OR_CIRCUIT(circ);
3017 queue = &orcirc->p_chan_cells;
3018 streams_blocked = circ->streams_blocked_on_p_chan;
3021 if (PREDICT_UNLIKELY(queue->n >= max_circuit_cell_queue_size)) {
3022 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
3023 "%s circuit has %d cells in its queue, maximum allowed is %d. "
3024 "Closing circuit for safety reasons.",
3025 (exitward) ? "Outbound" : "Inbound", queue->n,
3026 max_circuit_cell_queue_size);
3027 circuit_mark_for_close(circ, END_CIRC_REASON_RESOURCELIMIT);
3028 stats_n_circ_max_cell_reached++;
3029 return;
3032 /* Very important that we copy to the circuit queue because all calls to
3033 * this function use the stack for the cell memory. */
3034 cell_queue_append_packed_copy(circ, queue, exitward, cell,
3035 chan->wide_circ_ids, 1);
3037 /* Check and run the OOM if needed. */
3038 if (PREDICT_UNLIKELY(cell_queues_check_size())) {
3039 /* We ran the OOM handler which might have closed this circuit. */
3040 if (circ->marked_for_close)
3041 return;
3044 /* If we have too many cells on the circuit, we should stop reading from
3045 * the edge streams for a while. */
3046 if (!streams_blocked && queue->n >= CELL_QUEUE_HIGHWATER_SIZE)
3047 set_streams_blocked_on_circ(circ, chan, 1, 0); /* block streams */
3049 if (streams_blocked && fromstream) {
3050 /* This edge connection is apparently not blocked; block it. */
3051 set_streams_blocked_on_circ(circ, chan, 1, fromstream);
3054 update_circuit_on_cmux(circ, direction);
3055 if (queue->n == 1) {
3056 /* This was the first cell added to the queue. We just made this
3057 * circuit active. */
3058 log_debug(LD_GENERAL, "Made a circuit active.");
3061 /* New way: mark this as having waiting cells for the scheduler */
3062 scheduler_channel_has_waiting_cells(chan);
3065 /** Append an encoded value of <b>addr</b> to <b>payload_out</b>, which must
3066 * have at least 18 bytes of free space. The encoding is, as specified in
3067 * tor-spec.txt:
3068 * RESOLVED_TYPE_IPV4 or RESOLVED_TYPE_IPV6 [1 byte]
3069 * LENGTH [1 byte]
3070 * ADDRESS [length bytes]
3071 * Return the number of bytes added, or -1 on error */
3073 append_address_to_payload(uint8_t *payload_out, const tor_addr_t *addr)
3075 uint32_t a;
3076 switch (tor_addr_family(addr)) {
3077 case AF_INET:
3078 payload_out[0] = RESOLVED_TYPE_IPV4;
3079 payload_out[1] = 4;
3080 a = tor_addr_to_ipv4n(addr);
3081 memcpy(payload_out+2, &a, 4);
3082 return 6;
3083 case AF_INET6:
3084 payload_out[0] = RESOLVED_TYPE_IPV6;
3085 payload_out[1] = 16;
3086 memcpy(payload_out+2, tor_addr_to_in6_addr8(addr), 16);
3087 return 18;
3088 case AF_UNSPEC:
3089 default:
3090 return -1;
3094 /** Given <b>payload_len</b> bytes at <b>payload</b>, starting with an address
3095 * encoded as by append_address_to_payload(), try to decode the address into
3096 * *<b>addr_out</b>. Return the next byte in the payload after the address on
3097 * success, or NULL on failure. */
3098 const uint8_t *
3099 decode_address_from_payload(tor_addr_t *addr_out, const uint8_t *payload,
3100 int payload_len)
3102 if (payload_len < 2)
3103 return NULL;
3104 if (payload_len < 2+payload[1])
3105 return NULL;
3107 switch (payload[0]) {
3108 case RESOLVED_TYPE_IPV4:
3109 if (payload[1] != 4)
3110 return NULL;
3111 tor_addr_from_ipv4n(addr_out, get_uint32(payload+2));
3112 break;
3113 case RESOLVED_TYPE_IPV6:
3114 if (payload[1] != 16)
3115 return NULL;
3116 tor_addr_from_ipv6_bytes(addr_out, (char*)(payload+2));
3117 break;
3118 default:
3119 tor_addr_make_unspec(addr_out);
3120 break;
3122 return payload + 2 + payload[1];
3125 /** Remove all the cells queued on <b>circ</b> for <b>chan</b>. */
3126 void
3127 circuit_clear_cell_queue(circuit_t *circ, channel_t *chan)
3129 cell_queue_t *queue;
3130 cell_direction_t direction;
3132 if (circ->n_chan == chan) {
3133 queue = &circ->n_chan_cells;
3134 direction = CELL_DIRECTION_OUT;
3135 } else {
3136 or_circuit_t *orcirc = TO_OR_CIRCUIT(circ);
3137 tor_assert(orcirc->p_chan == chan);
3138 queue = &orcirc->p_chan_cells;
3139 direction = CELL_DIRECTION_IN;
3142 /* Clear the queue */
3143 cell_queue_clear(queue);
3145 /* Update the cell counter in the cmux */
3146 if (chan->cmux && circuitmux_is_circuit_attached(chan->cmux, circ))
3147 update_circuit_on_cmux(circ, direction);
3150 /** Return 1 if we shouldn't restart reading on this circuit, even if
3151 * we get a SENDME. Else return 0.
3153 static int
3154 circuit_queue_streams_are_blocked(circuit_t *circ)
3156 if (CIRCUIT_IS_ORIGIN(circ)) {
3157 return circ->streams_blocked_on_n_chan;
3158 } else {
3159 return circ->streams_blocked_on_p_chan;