1 /* Copyright (c) 2001 Matej Pfajfar.
2 * Copyright (c) 2001-2004, Roger Dingledine.
3 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4 * Copyright (c) 2007-2021, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
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
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
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.
49 #include "core/or/or.h"
50 #include "feature/client/addressmap.h"
51 #include "lib/err/backtrace.h"
52 #include "lib/buf/buffers.h"
53 #include "core/or/channel.h"
54 #include "feature/client/circpathbias.h"
55 #include "core/or/circuitbuild.h"
56 #include "core/or/circuitlist.h"
57 #include "core/or/circuituse.h"
58 #include "core/or/circuitpadding.h"
59 #include "core/or/extendinfo.h"
60 #include "lib/compress/compress.h"
61 #include "app/config/config.h"
62 #include "core/mainloop/connection.h"
63 #include "core/or/connection_edge.h"
64 #include "core/or/connection_or.h"
65 #include "feature/control/control_events.h"
66 #include "lib/crypt_ops/crypto_rand.h"
67 #include "lib/crypt_ops/crypto_util.h"
68 #include "feature/dircommon/directory.h"
69 #include "feature/relay/dns.h"
70 #include "feature/relay/circuitbuild_relay.h"
71 #include "feature/stats/geoip_stats.h"
72 #include "feature/hs/hs_cache.h"
73 #include "core/mainloop/mainloop.h"
74 #include "feature/nodelist/networkstatus.h"
75 #include "feature/nodelist/nodelist.h"
76 #include "core/or/onion.h"
77 #include "core/or/policies.h"
78 #include "core/or/reasons.h"
79 #include "core/or/relay.h"
80 #include "core/crypto/relay_crypto.h"
81 #include "feature/rend/rendcommon.h"
82 #include "feature/nodelist/describe.h"
83 #include "feature/nodelist/routerlist.h"
84 #include "core/or/scheduler.h"
85 #include "feature/hs/hs_metrics.h"
86 #include "feature/stats/rephist.h"
88 #include "core/or/cell_st.h"
89 #include "core/or/cell_queue_st.h"
90 #include "core/or/cpath_build_state_st.h"
91 #include "feature/dircommon/dir_connection_st.h"
92 #include "core/or/destroy_cell_queue_st.h"
93 #include "core/or/entry_connection_st.h"
94 #include "core/or/extend_info_st.h"
95 #include "core/or/or_circuit_st.h"
96 #include "core/or/origin_circuit_st.h"
97 #include "feature/nodelist/routerinfo_st.h"
98 #include "core/or/socks_request_st.h"
99 #include "core/or/sendme.h"
100 #include "core/or/congestion_control_common.h"
101 #include "core/or/congestion_control_flow.h"
102 #include "core/or/conflux.h"
103 #include "core/or/conflux_util.h"
104 #include "core/or/conflux_pool.h"
106 static edge_connection_t
*relay_lookup_conn(circuit_t
*circ
, cell_t
*cell
,
107 cell_direction_t cell_direction
,
108 crypt_path_t
*layer_hint
);
110 static void circuit_resume_edge_reading(circuit_t
*circ
,
111 crypt_path_t
*layer_hint
);
112 static int circuit_resume_edge_reading_helper(edge_connection_t
*conn
,
114 crypt_path_t
*layer_hint
);
115 static int circuit_consider_stop_edge_reading(circuit_t
*circ
,
116 crypt_path_t
*layer_hint
);
117 static int circuit_queue_streams_are_blocked(circuit_t
*circ
);
118 static void adjust_exit_policy_from_exitpolicy_failure(origin_circuit_t
*circ
,
119 entry_connection_t
*conn
,
121 const tor_addr_t
*addr
);
122 static int connection_edge_process_ordered_relay_cell(cell_t
*cell
,
124 edge_connection_t
*conn
,
125 crypt_path_t
*layer_hint
,
127 static void set_block_state_for_streams(circuit_t
*circ
,
128 edge_connection_t
*stream_list
,
129 int block
, streamid_t stream_id
);
131 /** Stats: how many relay cells have originated at this hop, or have
132 * been relayed onward (not recognized at this hop)?
134 uint64_t stats_n_relay_cells_relayed
= 0;
135 /** Stats: how many relay cells have been delivered to streams at this
138 uint64_t stats_n_relay_cells_delivered
= 0;
139 /** Stats: how many circuits have we closed due to the cell queue limit being
140 * reached (see append_cell_to_circuit_queue()) */
141 uint64_t stats_n_circ_max_cell_reached
= 0;
142 uint64_t stats_n_circ_max_cell_outq_reached
= 0;
145 * Update channel usage state based on the type of relay cell and
146 * circuit properties.
148 * This is needed to determine if a client channel is being
149 * used for application traffic, and if a relay channel is being
150 * used for multihop circuits and application traffic. The decision
151 * to pad in channelpadding.c depends upon this info (as well as
152 * consensus parameters) to decide what channels to pad.
155 circuit_update_channel_usage(circuit_t
*circ
, cell_t
*cell
)
157 if (CIRCUIT_IS_ORIGIN(circ
)) {
159 * The client state was first set much earlier in
160 * circuit_send_next_onion_skin(), so we can start padding as early as
163 * However, if padding turns out to be expensive, we may want to not do
164 * it until actual application traffic starts flowing (which is controlled
165 * via consensus param nf_pad_before_usage).
167 * So: If we're an origin circuit and we've created a full length circuit,
168 * then any CELL_RELAY cell means application data. Increase the usage
169 * state of the channel to indicate this.
171 * We want to wait for CELL_RELAY specifically here, so we know that
172 * the channel was definitely being used for data and not for extends.
173 * By default, we pad as soon as a channel has been used for *any*
174 * circuits, so this state is irrelevant to the padding decision in
175 * the default case. However, if padding turns out to be expensive,
176 * we would like the ability to avoid padding until we're absolutely
177 * sure that a channel is used for enough application data to be worth
180 * (So it does not matter that CELL_RELAY_EARLY can actually contain
181 * application data. This is only a load reducing option and that edge
182 * case does not matter if we're desperately trying to reduce overhead
183 * anyway. See also consensus parameter nf_pad_before_usage).
185 if (BUG(!circ
->n_chan
))
188 if (circ
->n_chan
->channel_usage
== CHANNEL_USED_FOR_FULL_CIRCS
&&
189 cell
->command
== CELL_RELAY
) {
190 circ
->n_chan
->channel_usage
= CHANNEL_USED_FOR_USER_TRAFFIC
;
193 /* If we're a relay circuit, the question is more complicated. Basically:
194 * we only want to pad connections that carry multihop (anonymous)
197 * We assume we're more than one hop if either the previous hop
198 * is not a client, or if the previous hop is a client and there's
199 * a next hop. Then, circuit traffic starts at RELAY_EARLY, and
200 * user application traffic starts when we see RELAY cells.
202 or_circuit_t
*or_circ
= TO_OR_CIRCUIT(circ
);
204 if (BUG(!or_circ
->p_chan
))
207 if (!channel_is_client(or_circ
->p_chan
) ||
208 (channel_is_client(or_circ
->p_chan
) && circ
->n_chan
)) {
209 if (cell
->command
== CELL_RELAY_EARLY
) {
210 if (or_circ
->p_chan
->channel_usage
< CHANNEL_USED_FOR_FULL_CIRCS
) {
211 or_circ
->p_chan
->channel_usage
= CHANNEL_USED_FOR_FULL_CIRCS
;
213 } else if (cell
->command
== CELL_RELAY
) {
214 or_circ
->p_chan
->channel_usage
= CHANNEL_USED_FOR_USER_TRAFFIC
;
220 /** Receive a relay cell:
221 * - Crypt it (encrypt if headed toward the origin or if we <b>are</b> the
222 * origin; decrypt if we're headed toward the exit).
223 * - Check if recognized (if exitward).
224 * - If recognized and the digest checks out, then find if there's a stream
225 * that the cell is intended for, and deliver it to the right
227 * - If not recognized, then we need to relay it: append it to the appropriate
228 * cell_queue on <b>circ</b>.
230 * Return -<b>reason</b> on failure.
233 circuit_receive_relay_cell(cell_t
*cell
, circuit_t
*circ
,
234 cell_direction_t cell_direction
)
236 channel_t
*chan
= NULL
;
237 crypt_path_t
*layer_hint
=NULL
;
243 tor_assert(cell_direction
== CELL_DIRECTION_OUT
||
244 cell_direction
== CELL_DIRECTION_IN
);
245 if (circ
->marked_for_close
)
248 if (relay_decrypt_cell(circ
, cell
, cell_direction
, &layer_hint
, &recognized
)
250 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
251 "relay crypt failed. Dropping connection.");
252 return -END_CIRC_REASON_INTERNAL
;
255 circuit_update_channel_usage(circ
, cell
);
258 edge_connection_t
*conn
= NULL
;
260 /* Recognized cell, the cell digest has been updated, we'll record it for
261 * the SENDME if need be. */
262 sendme_record_received_cell_digest(circ
, layer_hint
);
264 if (circ
->purpose
== CIRCUIT_PURPOSE_PATH_BIAS_TESTING
) {
265 if (pathbias_check_probe_response(circ
, cell
) == -1) {
266 pathbias_count_valid_cells(circ
, cell
);
269 /* We need to drop this cell no matter what to avoid code that expects
270 * a certain purpose (such as the hidserv code). */
274 conn
= relay_lookup_conn(circ
, cell
, cell_direction
, layer_hint
);
275 if (cell_direction
== CELL_DIRECTION_OUT
) {
276 ++stats_n_relay_cells_delivered
;
277 log_debug(LD_OR
,"Sending away from origin.");
278 reason
= connection_edge_process_relay_cell(cell
, circ
, conn
, NULL
);
280 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
281 "connection_edge_process_relay_cell (away from origin) "
286 if (cell_direction
== CELL_DIRECTION_IN
) {
287 ++stats_n_relay_cells_delivered
;
288 log_debug(LD_OR
,"Sending to origin.");
289 reason
= connection_edge_process_relay_cell(cell
, circ
, conn
,
292 /* If a client is trying to connect to unknown hidden service port,
293 * END_CIRC_AT_ORIGIN is sent back so we can then close the circuit.
294 * Do not log warn as this is an expected behavior for a service. */
295 if (reason
!= END_CIRC_AT_ORIGIN
) {
297 "connection_edge_process_relay_cell (at origin) failed.");
305 /* not recognized. inform circpad and pass it on. */
306 circpad_deliver_unrecognized_cell_events(circ
, cell_direction
);
308 if (cell_direction
== CELL_DIRECTION_OUT
) {
309 cell
->circ_id
= circ
->n_circ_id
; /* switch it */
311 } else if (! CIRCUIT_IS_ORIGIN(circ
)) {
312 cell
->circ_id
= TO_OR_CIRCUIT(circ
)->p_circ_id
; /* switch it */
313 chan
= TO_OR_CIRCUIT(circ
)->p_chan
;
315 log_fn(LOG_PROTOCOL_WARN
, LD_OR
,
316 "Dropping unrecognized inbound cell on origin circuit.");
317 /* If we see unrecognized cells on path bias testing circs,
318 * it's bad mojo. Those circuits need to die.
319 * XXX: Shouldn't they always die? */
320 if (circ
->purpose
== CIRCUIT_PURPOSE_PATH_BIAS_TESTING
) {
321 TO_ORIGIN_CIRCUIT(circ
)->path_state
= PATH_STATE_USE_FAILED
;
322 return -END_CIRC_REASON_TORPROTOCOL
;
329 // XXXX Can this splice stuff be done more cleanly?
330 if (! CIRCUIT_IS_ORIGIN(circ
) &&
331 TO_OR_CIRCUIT(circ
)->rend_splice
&&
332 cell_direction
== CELL_DIRECTION_OUT
) {
333 or_circuit_t
*splice_
= TO_OR_CIRCUIT(circ
)->rend_splice
;
334 tor_assert(circ
->purpose
== CIRCUIT_PURPOSE_REND_ESTABLISHED
);
335 tor_assert(splice_
->base_
.purpose
== CIRCUIT_PURPOSE_REND_ESTABLISHED
);
336 cell
->circ_id
= splice_
->p_circ_id
;
337 cell
->command
= CELL_RELAY
; /* can't be relay_early anyway */
338 if ((reason
= circuit_receive_relay_cell(cell
, TO_CIRCUIT(splice_
),
339 CELL_DIRECTION_IN
)) < 0) {
340 log_warn(LD_REND
, "Error relaying cell across rendezvous; closing "
342 /* XXXX Do this here, or just return -1? */
343 circuit_mark_for_close(circ
, -reason
);
348 if (BUG(CIRCUIT_IS_ORIGIN(circ
))) {
349 /* Should be impossible at this point. */
350 return -END_CIRC_REASON_TORPROTOCOL
;
352 or_circuit_t
*or_circ
= TO_OR_CIRCUIT(circ
);
353 if (++or_circ
->n_cells_discarded_at_end
== 1) {
354 time_t seconds_open
= approx_time() - circ
->timestamp_created
.tv_sec
;
355 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
356 "Didn't recognize a cell, but circ stops here! Closing circuit. "
357 "It was created %ld seconds ago.", (long)seconds_open
);
359 return -END_CIRC_REASON_TORPROTOCOL
;
362 log_debug(LD_OR
,"Passing on unrecognized cell.");
364 ++stats_n_relay_cells_relayed
; /* XXXX no longer quite accurate {cells}
365 * we might kill the circ before we relay
368 if (append_cell_to_circuit_queue(circ
, chan
, cell
, cell_direction
, 0) < 0) {
369 return -END_CIRC_REASON_RESOURCELIMIT
;
374 /** Package a relay cell from an edge:
375 * - Encrypt it to the right layer
376 * - Append it to the appropriate cell_queue on <b>circ</b>.
378 * Return 1 if the cell was successfully sent as in queued on the circuit.
379 * Return 0 if the cell needs to be dropped as in ignored.
380 * Return -1 on error for which the circuit should be marked for close. */
382 circuit_package_relay_cell
, (cell_t
*cell
, circuit_t
*circ
,
383 cell_direction_t cell_direction
,
384 crypt_path_t
*layer_hint
, streamid_t on_stream
,
385 const char *filename
, int lineno
))
387 channel_t
*chan
; /* where to send the cell */
389 if (circ
->marked_for_close
) {
390 /* Circuit is marked; send nothing. */
394 if (cell_direction
== CELL_DIRECTION_OUT
) {
397 log_warn(LD_BUG
,"outgoing relay cell sent from %s:%d has n_chan==NULL."
398 " Dropping. Circuit is in state %s (%d), and is "
399 "%smarked for close. (%s:%d, %d)", filename
, lineno
,
400 circuit_state_to_string(circ
->state
), circ
->state
,
401 circ
->marked_for_close
? "" : "not ",
402 circ
->marked_for_close_file
?circ
->marked_for_close_file
:"",
403 circ
->marked_for_close
, circ
->marked_for_close_reason
);
404 if (CIRCUIT_IS_ORIGIN(circ
)) {
405 circuit_log_path(LOG_WARN
, LD_BUG
, TO_ORIGIN_CIRCUIT(circ
));
407 log_backtrace(LOG_WARN
,LD_BUG
,"");
408 return 0; /* just drop it */
410 if (!CIRCUIT_IS_ORIGIN(circ
)) {
411 log_warn(LD_BUG
,"outgoing relay cell sent from %s:%d on non-origin "
412 "circ. Dropping.", filename
, lineno
);
413 log_backtrace(LOG_WARN
,LD_BUG
,"");
414 return 0; /* just drop it */
417 relay_encrypt_cell_outbound(cell
, TO_ORIGIN_CIRCUIT(circ
), layer_hint
);
419 /* Update circ written totals for control port */
420 origin_circuit_t
*ocirc
= TO_ORIGIN_CIRCUIT(circ
);
421 ocirc
->n_written_circ_bw
= tor_add_u32_nowrap(ocirc
->n_written_circ_bw
,
424 } else { /* incoming cell */
425 if (CIRCUIT_IS_ORIGIN(circ
)) {
426 /* We should never package an _incoming_ cell from the circuit
427 * origin; that means we messed up somewhere. */
428 log_warn(LD_BUG
,"incoming relay cell at origin circuit. Dropping.");
429 assert_circuit_ok(circ
);
430 return 0; /* just drop it */
432 or_circuit_t
*or_circ
= TO_OR_CIRCUIT(circ
);
433 relay_encrypt_cell_inbound(cell
, or_circ
);
434 chan
= or_circ
->p_chan
;
436 ++stats_n_relay_cells_relayed
;
438 return append_cell_to_circuit_queue(circ
, chan
, cell
,
439 cell_direction
, on_stream
);
442 /** If cell's stream_id matches the stream_id of any conn that's
443 * attached to circ, return that conn, else return NULL.
445 static edge_connection_t
*
446 relay_lookup_conn(circuit_t
*circ
, cell_t
*cell
,
447 cell_direction_t cell_direction
, crypt_path_t
*layer_hint
)
449 edge_connection_t
*tmpconn
;
452 relay_header_unpack(&rh
, cell
->payload
);
457 /* IN or OUT cells could have come from either direction, now
458 * that we allow rendezvous *to* an OP.
460 if (CIRCUIT_IS_ORIGIN(circ
)) {
461 for (tmpconn
= TO_ORIGIN_CIRCUIT(circ
)->p_streams
; tmpconn
;
462 tmpconn
=tmpconn
->next_stream
) {
463 if (rh
.stream_id
== tmpconn
->stream_id
&&
464 !tmpconn
->base_
.marked_for_close
&&
465 edge_uses_cpath(tmpconn
, layer_hint
)) {
466 log_debug(LD_APP
,"found conn for stream %d.", rh
.stream_id
);
471 for (tmpconn
= TO_OR_CIRCUIT(circ
)->n_streams
; tmpconn
;
472 tmpconn
=tmpconn
->next_stream
) {
473 if (rh
.stream_id
== tmpconn
->stream_id
&&
474 !tmpconn
->base_
.marked_for_close
) {
475 log_debug(LD_EXIT
,"found conn for stream %d.", rh
.stream_id
);
476 if (cell_direction
== CELL_DIRECTION_OUT
||
477 connection_edge_is_rendezvous_stream(tmpconn
))
481 for (tmpconn
= TO_OR_CIRCUIT(circ
)->resolving_streams
; tmpconn
;
482 tmpconn
=tmpconn
->next_stream
) {
483 if (rh
.stream_id
== tmpconn
->stream_id
&&
484 !tmpconn
->base_
.marked_for_close
) {
485 log_debug(LD_EXIT
,"found conn for stream %d.", rh
.stream_id
);
490 return NULL
; /* probably a begin relay cell */
493 /** Pack the relay_header_t host-order structure <b>src</b> into
494 * network-order in the buffer <b>dest</b>. See tor-spec.txt for details
495 * about the wire format.
498 relay_header_pack(uint8_t *dest
, const relay_header_t
*src
)
500 set_uint8(dest
, src
->command
);
501 set_uint16(dest
+1, htons(src
->recognized
));
502 set_uint16(dest
+3, htons(src
->stream_id
));
503 memcpy(dest
+5, src
->integrity
, 4);
504 set_uint16(dest
+9, htons(src
->length
));
507 /** Unpack the network-order buffer <b>src</b> into a host-order
508 * relay_header_t structure <b>dest</b>.
511 relay_header_unpack(relay_header_t
*dest
, const uint8_t *src
)
513 dest
->command
= get_uint8(src
);
514 dest
->recognized
= ntohs(get_uint16(src
+1));
515 dest
->stream_id
= ntohs(get_uint16(src
+3));
516 memcpy(dest
->integrity
, src
+5, 4);
517 dest
->length
= ntohs(get_uint16(src
+9));
520 /** Convert the relay <b>command</b> into a human-readable string. */
522 relay_command_to_string(uint8_t command
)
526 case RELAY_COMMAND_BEGIN
: return "BEGIN";
527 case RELAY_COMMAND_DATA
: return "DATA";
528 case RELAY_COMMAND_END
: return "END";
529 case RELAY_COMMAND_CONNECTED
: return "CONNECTED";
530 case RELAY_COMMAND_SENDME
: return "SENDME";
531 case RELAY_COMMAND_EXTEND
: return "EXTEND";
532 case RELAY_COMMAND_EXTENDED
: return "EXTENDED";
533 case RELAY_COMMAND_TRUNCATE
: return "TRUNCATE";
534 case RELAY_COMMAND_TRUNCATED
: return "TRUNCATED";
535 case RELAY_COMMAND_DROP
: return "DROP";
536 case RELAY_COMMAND_RESOLVE
: return "RESOLVE";
537 case RELAY_COMMAND_RESOLVED
: return "RESOLVED";
538 case RELAY_COMMAND_BEGIN_DIR
: return "BEGIN_DIR";
539 case RELAY_COMMAND_ESTABLISH_INTRO
: return "ESTABLISH_INTRO";
540 case RELAY_COMMAND_ESTABLISH_RENDEZVOUS
: return "ESTABLISH_RENDEZVOUS";
541 case RELAY_COMMAND_INTRODUCE1
: return "INTRODUCE1";
542 case RELAY_COMMAND_INTRODUCE2
: return "INTRODUCE2";
543 case RELAY_COMMAND_RENDEZVOUS1
: return "RENDEZVOUS1";
544 case RELAY_COMMAND_RENDEZVOUS2
: return "RENDEZVOUS2";
545 case RELAY_COMMAND_INTRO_ESTABLISHED
: return "INTRO_ESTABLISHED";
546 case RELAY_COMMAND_RENDEZVOUS_ESTABLISHED
:
547 return "RENDEZVOUS_ESTABLISHED";
548 case RELAY_COMMAND_INTRODUCE_ACK
: return "INTRODUCE_ACK";
549 case RELAY_COMMAND_EXTEND2
: return "EXTEND2";
550 case RELAY_COMMAND_EXTENDED2
: return "EXTENDED2";
551 case RELAY_COMMAND_PADDING_NEGOTIATE
: return "PADDING_NEGOTIATE";
552 case RELAY_COMMAND_PADDING_NEGOTIATED
: return "PADDING_NEGOTIATED";
553 case RELAY_COMMAND_CONFLUX_LINK
: return "CONFLUX_LINK";
554 case RELAY_COMMAND_CONFLUX_LINKED
: return "CONFLUX_LINKED";
555 case RELAY_COMMAND_CONFLUX_LINKED_ACK
: return "CONFLUX_LINKED_ACK";
556 case RELAY_COMMAND_CONFLUX_SWITCH
: return "CONFLUX_SWITCH";
558 tor_snprintf(buf
, sizeof(buf
), "Unrecognized relay command %u",
564 /** When padding a cell with randomness, leave this many zeros after the
566 #define CELL_PADDING_GAP 4
568 /** Return the offset where the padding should start. The <b>data_len</b> is
569 * the relay payload length expected to be put in the cell. It can not be
570 * bigger than RELAY_PAYLOAD_SIZE else this function assert().
572 * Value will always be smaller than CELL_PAYLOAD_SIZE because this offset is
573 * for the entire cell length not just the data payload length. Zero is
574 * returned if there is no room for padding.
576 * This function always skips the first 4 bytes after the payload because
577 * having some unused zero bytes has saved us a lot of times in the past. */
580 get_pad_cell_offset(size_t data_len
)
582 /* This is never supposed to happen but in case it does, stop right away
583 * because if tor is tricked somehow into not adding random bytes to the
584 * payload with this function returning 0 for a bad data_len, the entire
585 * authenticated SENDME design can be bypassed leading to bad denial of
586 * service attacks. */
587 tor_assert(data_len
<= RELAY_PAYLOAD_SIZE
);
589 /* If the offset is larger than the cell payload size, we return an offset
590 * of zero indicating that no padding needs to be added. */
591 size_t offset
= RELAY_HEADER_SIZE
+ data_len
+ CELL_PADDING_GAP
;
592 if (offset
>= CELL_PAYLOAD_SIZE
) {
598 /* Add random bytes to the unused portion of the payload, to foil attacks
599 * where the other side can predict all of the bytes in the payload and thus
600 * compute the authenticated SENDME cells without seeing the traffic. See
603 pad_cell_payload(uint8_t *cell_payload
, size_t data_len
)
605 size_t pad_offset
, pad_len
;
607 tor_assert(cell_payload
);
609 pad_offset
= get_pad_cell_offset(data_len
);
610 if (pad_offset
== 0) {
611 /* We can't add padding so we are done. */
615 /* Remember here that the cell_payload is the length of the header and
616 * payload size so we offset it using the full length of the cell. */
617 pad_len
= CELL_PAYLOAD_SIZE
- pad_offset
;
618 crypto_fast_rng_getbytes(get_thread_fast_rng(),
619 cell_payload
+ pad_offset
, pad_len
);
622 /** Make a relay cell out of <b>relay_command</b> and <b>payload</b>, and send
623 * it onto the open circuit <b>circ</b>. <b>stream_id</b> is the ID on
624 * <b>circ</b> for the stream that's sending the relay cell, or 0 if it's a
625 * control cell. <b>cpath_layer</b> is NULL for OR->OP cells, or the
626 * destination hop for OP->OR cells.
628 * If you can't send the cell, mark the circuit for close and return -1. Else
632 relay_send_command_from_edge_
,(streamid_t stream_id
, circuit_t
*orig_circ
,
633 uint8_t relay_command
, const char *payload
,
634 size_t payload_len
, crypt_path_t
*cpath_layer
,
635 const char *filename
, int lineno
))
639 cell_direction_t cell_direction
;
640 circuit_t
*circ
= orig_circ
;
642 /* If conflux is enabled, decide which leg to send on, and use that */
643 if (orig_circ
->conflux
&& conflux_should_multiplex(relay_command
)) {
644 circ
= conflux_decide_circ_for_send(orig_circ
->conflux
, orig_circ
,
647 log_warn(LD_BUG
, "No circuit to send for conflux for relay command %d, "
648 "called from %s:%d", relay_command
, filename
, lineno
);
649 conflux_log_set(LOG_WARN
, orig_circ
->conflux
,
650 CIRCUIT_IS_ORIGIN(orig_circ
));
653 /* Conflux circuits always send multiplexed relay commands to
654 * to the last hop. (Non-multiplexed commands go on their
655 * original circuit and hop). */
656 cpath_layer
= conflux_get_destination_hop(circ
);
660 /* XXXX NM Split this function into a separate versions per circuit type? */
663 tor_assert(payload_len
<= RELAY_PAYLOAD_SIZE
);
665 memset(&cell
, 0, sizeof(cell_t
));
666 cell
.command
= CELL_RELAY
;
667 if (CIRCUIT_IS_ORIGIN(circ
)) {
668 tor_assert(cpath_layer
);
669 cell
.circ_id
= circ
->n_circ_id
;
670 cell_direction
= CELL_DIRECTION_OUT
;
672 tor_assert(! cpath_layer
);
673 cell
.circ_id
= TO_OR_CIRCUIT(circ
)->p_circ_id
;
674 cell_direction
= CELL_DIRECTION_IN
;
677 memset(&rh
, 0, sizeof(rh
));
678 rh
.command
= relay_command
;
679 rh
.stream_id
= stream_id
;
680 rh
.length
= payload_len
;
681 relay_header_pack(cell
.payload
, &rh
);
684 memcpy(cell
.payload
+RELAY_HEADER_SIZE
, payload
, payload_len
);
686 /* Add random padding to the cell if we can. */
687 pad_cell_payload(cell
.payload
, payload_len
);
689 log_debug(LD_OR
,"delivering %d cell %s.", relay_command
,
690 cell_direction
== CELL_DIRECTION_OUT
? "forward" : "backward");
692 /* Tell circpad we're sending a relay cell */
693 circpad_deliver_sent_relay_cell_events(circ
, relay_command
);
695 /* If we are sending an END cell and this circuit is used for a tunneled
696 * directory request, advance its state. */
697 if (relay_command
== RELAY_COMMAND_END
&& circ
->dirreq_id
)
698 geoip_change_dirreq_state(circ
->dirreq_id
, DIRREQ_TUNNELED
,
699 DIRREQ_END_CELL_SENT
);
701 if (cell_direction
== CELL_DIRECTION_OUT
&& circ
->n_chan
) {
702 /* if we're using relaybandwidthrate, this conn wants priority */
703 channel_timestamp_client(circ
->n_chan
);
706 if (cell_direction
== CELL_DIRECTION_OUT
) {
707 origin_circuit_t
*origin_circ
= TO_ORIGIN_CIRCUIT(circ
);
708 if (origin_circ
->remaining_relay_early_cells
> 0 &&
709 (relay_command
== RELAY_COMMAND_EXTEND
||
710 relay_command
== RELAY_COMMAND_EXTEND2
||
711 cpath_layer
!= origin_circ
->cpath
)) {
712 /* If we've got any relay_early cells left and (we're sending
713 * an extend cell or we're not talking to the first hop), use
714 * one of them. Don't worry about the conn protocol version:
715 * append_cell_to_circuit_queue will fix it up. */
716 cell
.command
= CELL_RELAY_EARLY
;
717 /* If we're out of relay early cells, tell circpad */
718 if (--origin_circ
->remaining_relay_early_cells
== 0)
719 circpad_machine_event_circ_has_no_relay_early(origin_circ
);
720 log_debug(LD_OR
, "Sending a RELAY_EARLY cell; %d remaining.",
721 (int)origin_circ
->remaining_relay_early_cells
);
722 /* Memorize the command that is sent as RELAY_EARLY cell; helps debug
724 origin_circ
->relay_early_commands
[
725 origin_circ
->relay_early_cells_sent
++] = relay_command
;
726 } else if (relay_command
== RELAY_COMMAND_EXTEND
||
727 relay_command
== RELAY_COMMAND_EXTEND2
) {
728 /* If no RELAY_EARLY cells can be sent over this circuit, log which
729 * commands have been sent as RELAY_EARLY cells before; helps debug
731 smartlist_t
*commands_list
= smartlist_new();
733 char *commands
= NULL
;
734 for (; i
< origin_circ
->relay_early_cells_sent
; i
++)
735 smartlist_add(commands_list
, (char *)
736 relay_command_to_string(origin_circ
->relay_early_commands
[i
]));
737 commands
= smartlist_join_strings(commands_list
, ",", 0, NULL
);
738 log_warn(LD_BUG
, "Uh-oh. We're sending a RELAY_COMMAND_EXTEND cell, "
739 "but we have run out of RELAY_EARLY cells on that circuit. "
740 "Commands sent before: %s", commands
);
742 smartlist_free(commands_list
);
745 /* Let's assume we're well-behaved: Anything that we decide to send is
746 * valid, delivered data. */
747 circuit_sent_valid_data(origin_circ
, rh
.length
);
750 int ret
= circuit_package_relay_cell(&cell
, circ
, cell_direction
,
751 cpath_layer
, stream_id
, filename
,
754 circuit_mark_for_close(circ
, END_CIRC_REASON_INTERNAL
);
756 } else if (ret
== 0) {
757 /* This means we should drop the cell or that the circuit was already
758 * marked for close. At this point in time, we do NOT close the circuit if
759 * the cell is dropped. It is not the case with arti where each circuit
760 * protocol violation will lead to closing the circuit. */
764 /* At this point, we are certain that the cell was queued on the circuit and
765 * thus will be sent on the wire. */
768 conflux_note_cell_sent(circ
->conflux
, circ
, relay_command
);
771 /* If applicable, note the cell digest for the SENDME version 1 purpose if
772 * we need to. This call needs to be after the circuit_package_relay_cell()
773 * because the cell digest is set within that function. */
774 if (relay_command
== RELAY_COMMAND_DATA
) {
775 sendme_record_cell_digest_on_circ(circ
, cpath_layer
);
777 /* Handle the circuit-level SENDME package window. */
778 if (sendme_note_circuit_data_packaged(circ
, cpath_layer
) < 0) {
779 /* Package window has gone under 0. Protocol issue. */
780 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
781 "Circuit package window is below 0. Closing circuit.");
782 circuit_mark_for_close(circ
, END_CIRC_REASON_TORPROTOCOL
);
790 /** Make a relay cell out of <b>relay_command</b> and <b>payload</b>, and
791 * send it onto the open circuit <b>circ</b>. <b>fromconn</b> is the stream
792 * that's sending the relay cell, or NULL if it's a control cell.
793 * <b>cpath_layer</b> is NULL for OR->OP cells, or the destination hop
796 * If you can't send the cell, mark the circuit for close and
797 * return -1. Else return 0.
800 connection_edge_send_command(edge_connection_t
*fromconn
,
801 uint8_t relay_command
, const char *payload
,
804 /* XXXX NM Split this function into a separate versions per circuit type? */
806 crypt_path_t
*cpath_layer
= fromconn
->cpath_layer
;
807 tor_assert(fromconn
);
809 circ
= fromconn
->on_circuit
;
811 if (fromconn
->base_
.marked_for_close
) {
813 "called on conn that's already marked for close at %s:%d.",
814 fromconn
->base_
.marked_for_close_file
,
815 fromconn
->base_
.marked_for_close
);
820 if (fromconn
->base_
.type
== CONN_TYPE_AP
) {
821 log_info(LD_APP
,"no circ. Closing conn.");
822 connection_mark_unattached_ap(EDGE_TO_ENTRY_CONN(fromconn
),
823 END_STREAM_REASON_INTERNAL
);
825 log_info(LD_EXIT
,"no circ. Closing conn.");
826 fromconn
->edge_has_sent_end
= 1; /* no circ to send to */
827 fromconn
->end_reason
= END_STREAM_REASON_INTERNAL
;
828 connection_mark_for_close(TO_CONN(fromconn
));
833 if (circ
->marked_for_close
) {
834 /* The circuit has been marked, but not freed yet. When it's freed, it
835 * will mark this connection for close. */
839 #ifdef MEASUREMENTS_21206
840 /* Keep track of the number of RELAY_DATA cells sent for directory
842 connection_t
*linked_conn
= TO_CONN(fromconn
)->linked_conn
;
844 if (linked_conn
&& linked_conn
->type
== CONN_TYPE_DIR
) {
845 ++(TO_DIR_CONN(linked_conn
)->data_cells_sent
);
847 #endif /* defined(MEASUREMENTS_21206) */
849 return relay_send_command_from_edge(fromconn
->stream_id
, circ
,
850 relay_command
, payload
,
851 payload_len
, cpath_layer
);
854 /** How many times will I retry a stream that fails due to DNS
855 * resolve failure or misc error?
857 #define MAX_RESOLVE_FAILURES 3
859 /** Return 1 if reason is something that you should retry if you
860 * get the end cell before you've connected; else return 0. */
862 edge_reason_is_retriable(int reason
)
864 return reason
== END_STREAM_REASON_HIBERNATING
||
865 reason
== END_STREAM_REASON_RESOURCELIMIT
||
866 reason
== END_STREAM_REASON_EXITPOLICY
||
867 reason
== END_STREAM_REASON_RESOLVEFAILED
||
868 reason
== END_STREAM_REASON_MISC
||
869 reason
== END_STREAM_REASON_NOROUTE
;
872 /** Called when we receive an END cell on a stream that isn't open yet,
873 * from the client side.
874 * Arguments are as for connection_edge_process_relay_cell().
877 connection_ap_process_end_not_open(
878 relay_header_t
*rh
, cell_t
*cell
, origin_circuit_t
*circ
,
879 entry_connection_t
*conn
, crypt_path_t
*layer_hint
)
882 int reason
= *(cell
->payload
+RELAY_HEADER_SIZE
);
884 edge_connection_t
*edge_conn
= ENTRY_TO_EDGE_CONN(conn
);
885 (void) layer_hint
; /* unused */
887 if (rh
->length
> 0) {
888 if (reason
== END_STREAM_REASON_TORPROTOCOL
||
889 reason
== END_STREAM_REASON_DESTROY
) {
890 /* Both of these reasons could mean a failed tag
891 * hit the exit and it complained. Do not probe.
892 * Fail the circuit. */
893 circ
->path_state
= PATH_STATE_USE_FAILED
;
894 return -END_CIRC_REASON_TORPROTOCOL
;
895 } else if (reason
== END_STREAM_REASON_INTERNAL
) {
896 /* We can't infer success or failure, since older Tors report
897 * ENETUNREACH as END_STREAM_REASON_INTERNAL. */
899 /* Path bias: If we get a valid reason code from the exit,
900 * it wasn't due to tagging.
902 * We rely on recognized+digest being strong enough to make
903 * tags unlikely to allow us to get tagged, yet 'recognized'
904 * reason codes here. */
905 pathbias_mark_use_success(circ
);
909 /* This end cell is now valid. */
910 circuit_read_valid_data(circ
, rh
->length
);
912 if (rh
->length
== 0) {
913 reason
= END_STREAM_REASON_MISC
;
916 control_reason
= reason
| END_STREAM_REASON_FLAG_REMOTE
;
918 if (edge_reason_is_retriable(reason
) &&
919 /* avoid retry if rend */
920 !connection_edge_is_rendezvous_stream(edge_conn
)) {
921 const char *chosen_exit_digest
=
922 circ
->build_state
->chosen_exit
->identity_digest
;
923 log_info(LD_APP
,"Address '%s' refused due to '%s'. Considering retrying.",
924 safe_str(conn
->socks_request
->address
),
925 stream_end_reason_to_string(reason
));
926 exitrouter
= node_get_mutable_by_id(chosen_exit_digest
);
928 case END_STREAM_REASON_EXITPOLICY
: {
930 tor_addr_make_unspec(&addr
);
931 if (rh
->length
>= 5) {
933 tor_addr_make_unspec(&addr
);
934 if (rh
->length
== 5 || rh
->length
== 9) {
935 tor_addr_from_ipv4n(&addr
,
936 get_uint32(cell
->payload
+RELAY_HEADER_SIZE
+1));
938 ttl
= (int)ntohl(get_uint32(cell
->payload
+RELAY_HEADER_SIZE
+5));
939 } else if (rh
->length
== 17 || rh
->length
== 21) {
940 tor_addr_from_ipv6_bytes(&addr
,
941 (cell
->payload
+RELAY_HEADER_SIZE
+1));
942 if (rh
->length
== 21)
943 ttl
= (int)ntohl(get_uint32(cell
->payload
+RELAY_HEADER_SIZE
+17));
945 if (tor_addr_is_null(&addr
)) {
946 log_info(LD_APP
,"Address '%s' resolved to 0.0.0.0. Closing,",
947 safe_str(conn
->socks_request
->address
));
948 connection_mark_unattached_ap(conn
, END_STREAM_REASON_TORPROTOCOL
);
952 if ((tor_addr_family(&addr
) == AF_INET
&&
953 !conn
->entry_cfg
.ipv4_traffic
) ||
954 (tor_addr_family(&addr
) == AF_INET6
&&
955 !conn
->entry_cfg
.ipv6_traffic
)) {
956 log_fn(LOG_PROTOCOL_WARN
, LD_APP
,
957 "Got an EXITPOLICY failure on a connection with a "
958 "mismatched family. Closing.");
959 connection_mark_unattached_ap(conn
, END_STREAM_REASON_TORPROTOCOL
);
962 if (get_options()->ClientDNSRejectInternalAddresses
&&
963 tor_addr_is_internal(&addr
, 0)) {
964 log_info(LD_APP
,"Address '%s' resolved to internal. Closing,",
965 safe_str(conn
->socks_request
->address
));
966 connection_mark_unattached_ap(conn
, END_STREAM_REASON_TORPROTOCOL
);
970 client_dns_set_addressmap(conn
,
971 conn
->socks_request
->address
, &addr
,
972 conn
->chosen_exit_name
, ttl
);
975 char new_addr
[TOR_ADDR_BUF_LEN
];
976 tor_addr_to_str(new_addr
, &addr
, sizeof(new_addr
), 1);
977 if (strcmp(conn
->socks_request
->address
, new_addr
)) {
978 strlcpy(conn
->socks_request
->address
, new_addr
,
979 sizeof(conn
->socks_request
->address
));
980 control_event_stream_status(conn
, STREAM_EVENT_REMAP
, 0);
984 /* check if the exit *ought* to have allowed it */
986 adjust_exit_policy_from_exitpolicy_failure(circ
,
991 if (conn
->chosen_exit_optional
||
992 conn
->chosen_exit_retries
) {
993 /* stop wanting a specific exit */
994 conn
->chosen_exit_optional
= 0;
995 /* A non-zero chosen_exit_retries can happen if we set a
996 * TrackHostExits for this address under a port that the exit
997 * relay allows, but then try the same address with a different
998 * port that it doesn't allow to exit. We shouldn't unregister
999 * the mapping, since it is probably still wanted on the
1000 * original port. But now we give away to the exit relay that
1001 * we probably have a TrackHostExits on it. So be it. */
1002 conn
->chosen_exit_retries
= 0;
1003 tor_free(conn
->chosen_exit_name
); /* clears it */
1005 if (connection_ap_detach_retriable(conn
, circ
, control_reason
) >= 0)
1007 /* else, conn will get closed below */
1010 case END_STREAM_REASON_CONNECTREFUSED
:
1011 if (!conn
->chosen_exit_optional
)
1012 break; /* break means it'll close, below */
1013 /* Else fall through: expire this circuit, clear the
1014 * chosen_exit_name field, and try again. */
1016 case END_STREAM_REASON_RESOLVEFAILED
:
1017 case END_STREAM_REASON_TIMEOUT
:
1018 case END_STREAM_REASON_MISC
:
1019 case END_STREAM_REASON_NOROUTE
:
1020 if (client_dns_incr_failures(conn
->socks_request
->address
)
1021 < MAX_RESOLVE_FAILURES
) {
1022 /* We haven't retried too many times; reattach the connection. */
1023 circuit_log_path(LOG_INFO
,LD_APP
,circ
);
1024 /* Mark this circuit "unusable for new streams". */
1025 mark_circuit_unusable_for_new_conns(circ
);
1027 if (conn
->chosen_exit_optional
) {
1028 /* stop wanting a specific exit */
1029 conn
->chosen_exit_optional
= 0;
1030 tor_free(conn
->chosen_exit_name
); /* clears it */
1032 if (connection_ap_detach_retriable(conn
, circ
, control_reason
) >= 0)
1034 /* else, conn will get closed below */
1037 "Have tried resolving or connecting to address '%s' "
1038 "at %d different places. Giving up.",
1039 safe_str(conn
->socks_request
->address
),
1040 MAX_RESOLVE_FAILURES
);
1041 /* clear the failures, so it will have a full try next time */
1042 client_dns_clear_failures(conn
->socks_request
->address
);
1045 case END_STREAM_REASON_HIBERNATING
:
1046 case END_STREAM_REASON_RESOURCELIMIT
:
1048 policies_set_node_exitpolicy_to_reject_all(exitrouter
);
1050 if (conn
->chosen_exit_optional
) {
1051 /* stop wanting a specific exit */
1052 conn
->chosen_exit_optional
= 0;
1053 tor_free(conn
->chosen_exit_name
); /* clears it */
1055 if (connection_ap_detach_retriable(conn
, circ
, control_reason
) >= 0)
1057 /* else, will close below */
1060 log_info(LD_APP
,"Giving up on retrying; conn can't be handled.");
1064 "Edge got end (%s) before we're connected. Marking for close.",
1065 stream_end_reason_to_string(rh
->length
> 0 ? reason
: -1));
1066 circuit_log_path(LOG_INFO
,LD_APP
,circ
);
1067 /* need to test because of detach_retriable */
1068 if (!ENTRY_TO_CONN(conn
)->marked_for_close
)
1069 connection_mark_unattached_ap(conn
, control_reason
);
1073 /** Called when we have gotten an END_REASON_EXITPOLICY failure on <b>circ</b>
1074 * for <b>conn</b>, while attempting to connect via <b>node</b>. If the node
1075 * told us which address it rejected, then <b>addr</b> is that address;
1076 * otherwise it is AF_UNSPEC.
1078 * If we are sure the node should have allowed this address, mark the node as
1079 * having a reject *:* exit policy. Otherwise, mark the circuit as unusable
1080 * for this particular address.
1083 adjust_exit_policy_from_exitpolicy_failure(origin_circuit_t
*circ
,
1084 entry_connection_t
*conn
,
1086 const tor_addr_t
*addr
)
1088 int make_reject_all
= 0;
1089 const sa_family_t family
= tor_addr_family(addr
);
1093 int asked_for_family
= tor_addr_parse(&tmp
, conn
->socks_request
->address
);
1094 if (family
== AF_UNSPEC
) {
1095 make_reject_all
= 1;
1096 } else if (node_exit_policy_is_exact(node
, family
) &&
1097 asked_for_family
!= -1 && !conn
->chosen_exit_name
) {
1098 make_reject_all
= 1;
1101 if (make_reject_all
) {
1103 "Exitrouter %s seems to be more restrictive than its exit "
1104 "policy. Not using this router as exit for now.",
1105 node_describe(node
));
1106 policies_set_node_exitpolicy_to_reject_all(node
);
1110 if (family
!= AF_UNSPEC
)
1111 addr_policy_append_reject_addr(&circ
->prepend_policy
, addr
);
1114 /** Helper: change the socks_request->address field on conn to the
1115 * dotted-quad representation of <b>new_addr</b>,
1116 * and send an appropriate REMAP event. */
1118 remap_event_helper(entry_connection_t
*conn
, const tor_addr_t
*new_addr
)
1120 tor_addr_to_str(conn
->socks_request
->address
, new_addr
,
1121 sizeof(conn
->socks_request
->address
),
1123 control_event_stream_status(conn
, STREAM_EVENT_REMAP
,
1124 REMAP_STREAM_SOURCE_EXIT
);
1127 /** Extract the contents of a connected cell in <b>cell</b>, whose relay
1128 * header has already been parsed into <b>rh</b>. On success, set
1129 * <b>addr_out</b> to the address we're connected to, and <b>ttl_out</b> to
1130 * the ttl of that address, in seconds, and return 0. On failure, return
1133 * Note that the resulting address can be UNSPEC if the connected cell had no
1134 * address (as for a stream to an union service or a tunneled directory
1135 * connection), and that the ttl can be absent (in which case <b>ttl_out</b>
1138 connected_cell_parse(const relay_header_t
*rh
, const cell_t
*cell
,
1139 tor_addr_t
*addr_out
, int *ttl_out
)
1142 const uint8_t *payload
= cell
->payload
+ RELAY_HEADER_SIZE
;
1144 tor_addr_make_unspec(addr_out
);
1146 if (rh
->length
== 0)
1150 bytes
= ntohl(get_uint32(payload
));
1152 /* If bytes is 0, this is maybe a v6 address. Otherwise it's a v4 address */
1155 tor_addr_from_ipv4h(addr_out
, bytes
);
1156 if (rh
->length
>= 8) {
1157 bytes
= ntohl(get_uint32(payload
+ 4));
1158 if (bytes
<= INT32_MAX
)
1162 if (rh
->length
< 25) /* 4 bytes of 0s, 1 addr, 16 ipv4, 4 ttl. */
1164 if (get_uint8(payload
+ 4) != 6)
1166 tor_addr_from_ipv6_bytes(addr_out
, (payload
+ 5));
1167 bytes
= ntohl(get_uint32(payload
+ 21));
1168 if (bytes
<= INT32_MAX
)
1169 *ttl_out
= (int) bytes
;
1174 /** Drop all storage held by <b>addr</b>. */
1176 address_ttl_free_(address_ttl_t
*addr
)
1180 tor_free(addr
->hostname
);
1184 /** Parse a resolved cell in <b>cell</b>, with parsed header in <b>rh</b>.
1185 * Return -1 on parse error. On success, add one or more newly allocated
1186 * address_ttl_t to <b>addresses_out</b>; set *<b>errcode_out</b> to
1187 * one of 0, RESOLVED_TYPE_ERROR, or RESOLVED_TYPE_ERROR_TRANSIENT, and
1190 resolved_cell_parse(const cell_t
*cell
, const relay_header_t
*rh
,
1191 smartlist_t
*addresses_out
, int *errcode_out
)
1194 uint8_t answer_type
;
1196 address_ttl_t
*addr
;
1203 tor_assert(addresses_out
);
1204 tor_assert(errcode_out
);
1208 if (rh
->length
> RELAY_PAYLOAD_SIZE
)
1211 addrs
= smartlist_new();
1213 cp
= cell
->payload
+ RELAY_HEADER_SIZE
;
1215 remaining
= rh
->length
;
1217 const uint8_t *cp_orig
= cp
;
1220 answer_type
= *cp
++;
1222 if (remaining
< 2 + answer_len
+ 4) {
1225 if (answer_type
== RESOLVED_TYPE_IPV4
) {
1226 if (answer_len
!= 4) {
1229 addr
= tor_malloc_zero(sizeof(*addr
));
1230 tor_addr_from_ipv4n(&addr
->addr
, get_uint32(cp
));
1232 addr
->ttl
= ntohl(get_uint32(cp
));
1234 smartlist_add(addrs
, addr
);
1235 } else if (answer_type
== RESOLVED_TYPE_IPV6
) {
1236 if (answer_len
!= 16)
1238 addr
= tor_malloc_zero(sizeof(*addr
));
1239 tor_addr_from_ipv6_bytes(&addr
->addr
, cp
);
1241 addr
->ttl
= ntohl(get_uint32(cp
));
1243 smartlist_add(addrs
, addr
);
1244 } else if (answer_type
== RESOLVED_TYPE_HOSTNAME
) {
1245 if (answer_len
== 0) {
1248 addr
= tor_malloc_zero(sizeof(*addr
));
1249 addr
->hostname
= tor_memdup_nulterm(cp
, answer_len
);
1251 addr
->ttl
= ntohl(get_uint32(cp
));
1253 smartlist_add(addrs
, addr
);
1254 } else if (answer_type
== RESOLVED_TYPE_ERROR_TRANSIENT
||
1255 answer_type
== RESOLVED_TYPE_ERROR
) {
1256 errcode
= answer_type
;
1257 /* Ignore the error contents */
1258 cp
+= answer_len
+ 4;
1260 cp
+= answer_len
+ 4;
1262 tor_assert(((ssize_t
)remaining
) >= (cp
- cp_orig
));
1263 remaining
-= (cp
- cp_orig
);
1266 if (errcode
&& smartlist_len(addrs
) == 0) {
1267 /* Report an error only if there were no results. */
1268 *errcode_out
= errcode
;
1271 smartlist_add_all(addresses_out
, addrs
);
1272 smartlist_free(addrs
);
1277 /* On parse error, don't report any results */
1278 SMARTLIST_FOREACH(addrs
, address_ttl_t
*, a
, address_ttl_free(a
));
1279 smartlist_free(addrs
);
1283 /** Helper for connection_edge_process_resolved_cell: given an error code,
1284 * an entry_connection, and a list of address_ttl_t *, report the best answer
1285 * to the entry_connection. */
1287 connection_ap_handshake_socks_got_resolved_cell(entry_connection_t
*conn
,
1289 smartlist_t
*results
)
1291 address_ttl_t
*addr_ipv4
= NULL
;
1292 address_ttl_t
*addr_ipv6
= NULL
;
1293 address_ttl_t
*addr_hostname
= NULL
;
1294 address_ttl_t
*addr_best
= NULL
;
1296 /* If it's an error code, that's easy. */
1298 tor_assert(error_code
== RESOLVED_TYPE_ERROR
||
1299 error_code
== RESOLVED_TYPE_ERROR_TRANSIENT
);
1300 connection_ap_handshake_socks_resolved(conn
,
1301 error_code
,0,NULL
,-1,-1);
1305 /* Get the first answer of each type. */
1306 SMARTLIST_FOREACH_BEGIN(results
, address_ttl_t
*, addr
) {
1307 if (addr
->hostname
) {
1308 if (!addr_hostname
) {
1309 addr_hostname
= addr
;
1311 } else if (tor_addr_family(&addr
->addr
) == AF_INET
) {
1312 if (!addr_ipv4
&& conn
->entry_cfg
.ipv4_traffic
) {
1315 } else if (tor_addr_family(&addr
->addr
) == AF_INET6
) {
1316 if (!addr_ipv6
&& conn
->entry_cfg
.ipv6_traffic
) {
1320 } SMARTLIST_FOREACH_END(addr
);
1322 /* Now figure out which type we wanted to deliver. */
1323 if (conn
->socks_request
->command
== SOCKS_COMMAND_RESOLVE_PTR
) {
1324 if (addr_hostname
) {
1325 connection_ap_handshake_socks_resolved(conn
,
1326 RESOLVED_TYPE_HOSTNAME
,
1327 strlen(addr_hostname
->hostname
),
1328 (uint8_t*)addr_hostname
->hostname
,
1329 addr_hostname
->ttl
,-1);
1331 connection_ap_handshake_socks_resolved(conn
,
1332 RESOLVED_TYPE_ERROR
,0,NULL
,-1,-1);
1337 if (conn
->entry_cfg
.prefer_ipv6
) {
1338 addr_best
= addr_ipv6
? addr_ipv6
: addr_ipv4
;
1340 addr_best
= addr_ipv4
? addr_ipv4
: addr_ipv6
;
1343 /* Now convert it to the ugly old interface */
1345 connection_ap_handshake_socks_resolved(conn
,
1346 RESOLVED_TYPE_ERROR
,0,NULL
,-1,-1);
1350 connection_ap_handshake_socks_resolved_addr(conn
,
1355 remap_event_helper(conn
, &addr_best
->addr
);
1358 /** Handle a RELAY_COMMAND_RESOLVED cell that we received on a non-open AP
1361 connection_edge_process_resolved_cell(edge_connection_t
*conn
,
1363 const relay_header_t
*rh
)
1365 entry_connection_t
*entry_conn
= EDGE_TO_ENTRY_CONN(conn
);
1366 smartlist_t
*resolved_addresses
= NULL
;
1369 if (conn
->base_
.state
!= AP_CONN_STATE_RESOLVE_WAIT
) {
1370 log_fn(LOG_PROTOCOL_WARN
, LD_APP
, "Got a 'resolved' cell while "
1371 "not in state resolve_wait. Dropping.");
1374 tor_assert(SOCKS_COMMAND_IS_RESOLVE(entry_conn
->socks_request
->command
));
1376 resolved_addresses
= smartlist_new();
1377 if (resolved_cell_parse(cell
, rh
, resolved_addresses
, &errcode
)) {
1378 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
1379 "Dropping malformed 'resolved' cell");
1380 connection_mark_unattached_ap(entry_conn
, END_STREAM_REASON_TORPROTOCOL
);
1384 if (get_options()->ClientDNSRejectInternalAddresses
) {
1385 int orig_len
= smartlist_len(resolved_addresses
);
1386 SMARTLIST_FOREACH_BEGIN(resolved_addresses
, address_ttl_t
*, addr
) {
1387 if (addr
->hostname
== NULL
&& tor_addr_is_internal(&addr
->addr
, 0)) {
1388 log_info(LD_APP
, "Got a resolved cell with answer %s; dropping that "
1390 safe_str_client(fmt_addr(&addr
->addr
)));
1391 address_ttl_free(addr
);
1392 SMARTLIST_DEL_CURRENT(resolved_addresses
, addr
);
1394 } SMARTLIST_FOREACH_END(addr
);
1395 if (orig_len
&& smartlist_len(resolved_addresses
) == 0) {
1396 log_info(LD_APP
, "Got a resolved cell with only private addresses; "
1398 connection_ap_handshake_socks_resolved(entry_conn
,
1399 RESOLVED_TYPE_ERROR_TRANSIENT
,
1400 0, NULL
, 0, TIME_MAX
);
1401 connection_mark_unattached_ap(entry_conn
,
1402 END_STREAM_REASON_TORPROTOCOL
);
1407 /* This is valid data at this point. Count it */
1408 if (conn
->on_circuit
&& CIRCUIT_IS_ORIGIN(conn
->on_circuit
)) {
1409 circuit_read_valid_data(TO_ORIGIN_CIRCUIT(conn
->on_circuit
),
1413 connection_ap_handshake_socks_got_resolved_cell(entry_conn
,
1415 resolved_addresses
);
1417 connection_mark_unattached_ap(entry_conn
,
1418 END_STREAM_REASON_DONE
|
1419 END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED
);
1422 SMARTLIST_FOREACH(resolved_addresses
, address_ttl_t
*, addr
,
1423 address_ttl_free(addr
));
1424 smartlist_free(resolved_addresses
);
1428 /** An incoming relay cell has arrived from circuit <b>circ</b> to
1429 * stream <b>conn</b>.
1431 * The arguments here are the same as in
1432 * connection_edge_process_relay_cell() below; this function is called
1433 * from there when <b>conn</b> is defined and not in an open state.
1436 connection_edge_process_relay_cell_not_open(
1437 relay_header_t
*rh
, cell_t
*cell
, circuit_t
*circ
,
1438 edge_connection_t
*conn
, crypt_path_t
*layer_hint
)
1440 if (rh
->command
== RELAY_COMMAND_END
) {
1441 if (CIRCUIT_IS_ORIGIN(circ
) && conn
->base_
.type
== CONN_TYPE_AP
) {
1442 return connection_ap_process_end_not_open(rh
, cell
,
1443 TO_ORIGIN_CIRCUIT(circ
),
1444 EDGE_TO_ENTRY_CONN(conn
),
1447 /* we just got an 'end', don't need to send one */
1448 conn
->edge_has_sent_end
= 1;
1449 conn
->end_reason
= *(cell
->payload
+RELAY_HEADER_SIZE
) |
1450 END_STREAM_REASON_FLAG_REMOTE
;
1451 connection_mark_for_close(TO_CONN(conn
));
1456 if (conn
->base_
.type
== CONN_TYPE_AP
&&
1457 rh
->command
== RELAY_COMMAND_CONNECTED
) {
1460 entry_connection_t
*entry_conn
= EDGE_TO_ENTRY_CONN(conn
);
1461 tor_assert(CIRCUIT_IS_ORIGIN(circ
));
1462 if (conn
->base_
.state
!= AP_CONN_STATE_CONNECT_WAIT
) {
1463 log_fn(LOG_PROTOCOL_WARN
, LD_APP
,
1464 "Got 'connected' while not in state connect_wait. Dropping.");
1467 CONNECTION_AP_EXPECT_NONPENDING(entry_conn
);
1468 conn
->base_
.state
= AP_CONN_STATE_OPEN
;
1469 log_info(LD_APP
,"'connected' received for circid %u streamid %d "
1470 "after %d seconds.",
1471 (unsigned)circ
->n_circ_id
,
1473 (int)(time(NULL
) - conn
->base_
.timestamp_last_read_allowed
));
1474 if (connected_cell_parse(rh
, cell
, &addr
, &ttl
) < 0) {
1475 log_fn(LOG_PROTOCOL_WARN
, LD_APP
,
1476 "Got a badly formatted connected cell. Closing.");
1477 connection_edge_end(conn
, END_STREAM_REASON_TORPROTOCOL
);
1478 connection_mark_unattached_ap(entry_conn
, END_STREAM_REASON_TORPROTOCOL
);
1481 if (tor_addr_family(&addr
) != AF_UNSPEC
) {
1482 /* The family is not UNSPEC: so we were given an address in the
1483 * connected cell. (This is normal, except for BEGINDIR and onion
1484 * service streams.) */
1485 const sa_family_t family
= tor_addr_family(&addr
);
1486 if (tor_addr_is_null(&addr
) ||
1487 (get_options()->ClientDNSRejectInternalAddresses
&&
1488 tor_addr_is_internal(&addr
, 0))) {
1489 log_info(LD_APP
, "...but it claims the IP address was %s. Closing.",
1490 safe_str(fmt_addr(&addr
)));
1491 connection_edge_end(conn
, END_STREAM_REASON_TORPROTOCOL
);
1492 connection_mark_unattached_ap(entry_conn
,
1493 END_STREAM_REASON_TORPROTOCOL
);
1497 if ((family
== AF_INET
&& ! entry_conn
->entry_cfg
.ipv4_traffic
) ||
1498 (family
== AF_INET6
&& ! entry_conn
->entry_cfg
.ipv6_traffic
)) {
1499 log_fn(LOG_PROTOCOL_WARN
, LD_APP
,
1500 "Got a connected cell to %s with unsupported address family."
1501 " Closing.", safe_str(fmt_addr(&addr
)));
1502 connection_edge_end(conn
, END_STREAM_REASON_TORPROTOCOL
);
1503 connection_mark_unattached_ap(entry_conn
,
1504 END_STREAM_REASON_TORPROTOCOL
);
1508 client_dns_set_addressmap(entry_conn
,
1509 entry_conn
->socks_request
->address
, &addr
,
1510 entry_conn
->chosen_exit_name
, ttl
);
1512 remap_event_helper(entry_conn
, &addr
);
1514 circuit_log_path(LOG_INFO
,LD_APP
,TO_ORIGIN_CIRCUIT(circ
));
1515 /* don't send a socks reply to transparent conns */
1516 tor_assert(entry_conn
->socks_request
!= NULL
);
1517 if (!entry_conn
->socks_request
->has_finished
) {
1518 connection_ap_handshake_socks_reply(entry_conn
, NULL
, 0, 0);
1521 /* Was it a linked dir conn? If so, a dir request just started to
1522 * fetch something; this could be a bootstrap status milestone. */
1523 log_debug(LD_APP
, "considering");
1524 if (TO_CONN(conn
)->linked_conn
&&
1525 TO_CONN(conn
)->linked_conn
->type
== CONN_TYPE_DIR
) {
1526 connection_t
*dirconn
= TO_CONN(conn
)->linked_conn
;
1527 log_debug(LD_APP
, "it is! %d", dirconn
->purpose
);
1528 switch (dirconn
->purpose
) {
1529 case DIR_PURPOSE_FETCH_CERTIFICATE
:
1530 if (consensus_is_waiting_for_certs())
1531 control_event_bootstrap(BOOTSTRAP_STATUS_LOADING_KEYS
, 0);
1533 case DIR_PURPOSE_FETCH_CONSENSUS
:
1534 control_event_bootstrap(BOOTSTRAP_STATUS_LOADING_STATUS
, 0);
1536 case DIR_PURPOSE_FETCH_SERVERDESC
:
1537 case DIR_PURPOSE_FETCH_MICRODESC
:
1538 if (TO_DIR_CONN(dirconn
)->router_purpose
== ROUTER_PURPOSE_GENERAL
)
1539 control_event_boot_dir(BOOTSTRAP_STATUS_LOADING_DESCRIPTORS
,
1540 count_loading_descriptors_progress());
1544 /* This is definitely a success, so forget about any pending data we
1546 if (entry_conn
->pending_optimistic_data
) {
1547 buf_free(entry_conn
->pending_optimistic_data
);
1548 entry_conn
->pending_optimistic_data
= NULL
;
1551 /* This is valid data at this point. Count it */
1552 circuit_read_valid_data(TO_ORIGIN_CIRCUIT(circ
), rh
->length
);
1554 /* handle anything that might have queued */
1555 if (connection_edge_package_raw_inbuf(conn
, 1, NULL
) < 0) {
1556 /* (We already sent an end cell if possible) */
1557 connection_mark_for_close(TO_CONN(conn
));
1562 if (conn
->base_
.type
== CONN_TYPE_AP
&&
1563 rh
->command
== RELAY_COMMAND_RESOLVED
) {
1564 return connection_edge_process_resolved_cell(conn
, cell
, rh
);
1567 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
1568 "Got an unexpected relay command %d, in state %d (%s). Dropping.",
1569 rh
->command
, conn
->base_
.state
,
1570 conn_state_to_string(conn
->base_
.type
, conn
->base_
.state
));
1571 return 0; /* for forward compatibility, don't kill the circuit */
1572 // connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL);
1573 // connection_mark_for_close(conn);
1577 /** Process a SENDME cell that arrived on <b>circ</b>. If it is a stream level
1578 * cell, it is destined for the given <b>conn</b>. If it is a circuit level
1579 * cell, it is destined for the <b>layer_hint</b>. The <b>domain</b> is the
1580 * logging domain that should be used.
1582 * Return 0 if everything went well or a negative value representing a circuit
1583 * end reason on error for which the caller is responsible for closing it. */
1585 process_sendme_cell(const relay_header_t
*rh
, const cell_t
*cell
,
1586 circuit_t
*circ
, edge_connection_t
*conn
,
1587 crypt_path_t
*layer_hint
, int domain
)
1593 if (!rh
->stream_id
) {
1594 /* Circuit level SENDME cell. */
1595 ret
= sendme_process_circuit_level(layer_hint
, circ
,
1596 cell
->payload
+ RELAY_HEADER_SIZE
,
1601 /* Resume reading on any streams now that we've processed a valid
1602 * SENDME cell that updated our package window. */
1603 circuit_resume_edge_reading(circ
, layer_hint
);
1604 /* We are done, the rest of the code is for the stream level. */
1608 /* No connection, might be half edge state. We are done if so. */
1610 if (CIRCUIT_IS_ORIGIN(circ
)) {
1611 origin_circuit_t
*ocirc
= TO_ORIGIN_CIRCUIT(circ
);
1612 if (connection_half_edge_is_valid_sendme(ocirc
->half_streams
,
1614 circuit_read_valid_data(ocirc
, rh
->length
);
1615 log_info(domain
, "Sendme cell on circ %u valid on half-closed "
1617 ocirc
->global_identifier
, rh
->stream_id
);
1621 log_info(domain
, "SENDME cell dropped, unknown stream (streamid %d).",
1626 /* Stream level SENDME cell. */
1627 // TODO: Turn this off for cc_alg=1,2,3; use XON/XOFF instead
1628 ret
= sendme_process_stream_level(conn
, circ
, rh
->length
);
1630 /* Means we need to close the circuit with reason ret. */
1634 /* We've now processed properly a SENDME cell, all windows have been
1635 * properly updated, we'll read on the edge connection to see if we can
1636 * get data out towards the end point (Exit or client) since we are now
1637 * allowed to deliver more cells. */
1639 if (circuit_queue_streams_are_blocked(circ
)) {
1640 /* Still waiting for queue to flush; don't touch conn */
1643 connection_start_reading(TO_CONN(conn
));
1644 /* handle whatever might still be on the inbuf */
1645 if (connection_edge_package_raw_inbuf(conn
, 1, NULL
) < 0) {
1646 /* (We already sent an end cell if possible) */
1647 connection_mark_for_close(TO_CONN(conn
));
1653 /** A helper for connection_edge_process_relay_cell(): Actually handles the
1654 * cell that we received on the connection.
1656 * The arguments are the same as in the parent function
1657 * connection_edge_process_relay_cell(), plus the relay header <b>rh</b> as
1658 * unpacked by the parent function, and <b>optimistic_data</b> as set by the
1662 handle_relay_cell_command(cell_t
*cell
, circuit_t
*circ
,
1663 edge_connection_t
*conn
, crypt_path_t
*layer_hint
,
1664 relay_header_t
*rh
, int optimistic_data
)
1666 unsigned domain
= layer_hint
?LD_APP
:LD_EXIT
;
1671 /* First pass the cell to the circuit padding subsystem, in case it's a
1672 * padding cell or circuit that should be handled there. */
1673 if (circpad_check_received_cell(cell
, circ
, layer_hint
, rh
) == 0) {
1674 log_debug(domain
, "Cell handled as circuit padding");
1678 /* Now handle all the other commands */
1679 switch (rh
->command
) {
1680 case RELAY_COMMAND_CONFLUX_LINK
:
1681 conflux_process_link(circ
, cell
, rh
->length
);
1683 case RELAY_COMMAND_CONFLUX_LINKED
:
1684 conflux_process_linked(circ
, layer_hint
, cell
, rh
->length
);
1686 case RELAY_COMMAND_CONFLUX_LINKED_ACK
:
1687 conflux_process_linked_ack(circ
);
1689 case RELAY_COMMAND_CONFLUX_SWITCH
:
1690 return conflux_process_switch_command(circ
, layer_hint
, cell
, rh
);
1691 case RELAY_COMMAND_BEGIN
:
1692 case RELAY_COMMAND_BEGIN_DIR
:
1694 circ
->purpose
!= CIRCUIT_PURPOSE_S_REND_JOINED
) {
1695 log_fn(LOG_PROTOCOL_WARN
, LD_APP
,
1696 "Relay begin request unsupported at AP. Dropping.");
1699 if (circ
->purpose
== CIRCUIT_PURPOSE_S_REND_JOINED
&&
1700 layer_hint
!= TO_ORIGIN_CIRCUIT(circ
)->cpath
->prev
) {
1701 log_fn(LOG_PROTOCOL_WARN
, LD_APP
,
1702 "Relay begin request to Hidden Service "
1703 "from intermediary node. Dropping.");
1707 log_fn(LOG_PROTOCOL_WARN
, domain
,
1708 "Begin cell for known stream. Dropping.");
1711 if (rh
->command
== RELAY_COMMAND_BEGIN_DIR
&&
1712 circ
->purpose
!= CIRCUIT_PURPOSE_S_REND_JOINED
) {
1713 /* Assign this circuit and its app-ward OR connection a unique ID,
1714 * so that we can measure download times. The local edge and dir
1715 * connection will be assigned the same ID when they are created
1717 static uint64_t next_id
= 0;
1718 circ
->dirreq_id
= ++next_id
;
1719 TO_OR_CIRCUIT(circ
)->p_chan
->dirreq_id
= circ
->dirreq_id
;
1721 return connection_exit_begin_conn(cell
, circ
);
1722 case RELAY_COMMAND_DATA
:
1723 ++stats_n_data_cells_received
;
1725 if (rh
->stream_id
== 0) {
1726 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
, "Relay data cell with zero "
1727 "stream_id. Dropping.");
1730 if (CIRCUIT_IS_ORIGIN(circ
)) {
1731 origin_circuit_t
*ocirc
= TO_ORIGIN_CIRCUIT(circ
);
1732 if (connection_half_edge_is_valid_data(ocirc
->half_streams
,
1734 circuit_read_valid_data(ocirc
, rh
->length
);
1736 "data cell on circ %u valid on half-closed "
1737 "stream id %d", ocirc
->global_identifier
, rh
->stream_id
);
1741 log_info(domain
,"data cell dropped, unknown stream (streamid %d).",
1746 /* Update our stream-level deliver window that we just received a DATA
1747 * cell. Going below 0 means we have a protocol level error so the
1748 * stream and circuit are closed. */
1749 if (sendme_stream_data_received(conn
) < 0) {
1750 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
1751 "(relay data) conn deliver_window below 0. Killing.");
1752 connection_edge_end_close(conn
, END_STREAM_REASON_TORPROTOCOL
);
1753 return -END_CIRC_REASON_TORPROTOCOL
;
1755 /* Total all valid application bytes delivered */
1756 if (CIRCUIT_IS_ORIGIN(circ
) && rh
->length
> 0) {
1757 circuit_read_valid_data(TO_ORIGIN_CIRCUIT(circ
), rh
->length
);
1760 /* For onion service connection, update the metrics. */
1761 if (conn
->hs_ident
) {
1762 hs_metrics_app_write_bytes(&conn
->hs_ident
->identity_pk
,
1763 conn
->hs_ident
->orig_virtual_port
,
1767 stats_n_data_bytes_received
+= rh
->length
;
1768 connection_buf_add((char*)(cell
->payload
+ RELAY_HEADER_SIZE
),
1769 rh
->length
, TO_CONN(conn
));
1771 #ifdef MEASUREMENTS_21206
1772 /* Count number of RELAY_DATA cells received on a linked directory
1774 connection_t
*linked_conn
= TO_CONN(conn
)->linked_conn
;
1776 if (linked_conn
&& linked_conn
->type
== CONN_TYPE_DIR
) {
1777 ++(TO_DIR_CONN(linked_conn
)->data_cells_received
);
1779 #endif /* defined(MEASUREMENTS_21206) */
1781 if (!optimistic_data
) {
1782 /* Only send a SENDME if we're not getting optimistic data; otherwise
1783 * a SENDME could arrive before the CONNECTED.
1785 sendme_connection_edge_consider_sending(conn
);
1789 case RELAY_COMMAND_XOFF
:
1791 if (CIRCUIT_IS_ORIGIN(circ
)) {
1792 origin_circuit_t
*ocirc
= TO_ORIGIN_CIRCUIT(circ
);
1793 if (relay_crypt_from_last_hop(ocirc
, layer_hint
) &&
1794 connection_half_edge_is_valid_data(ocirc
->half_streams
,
1796 circuit_read_valid_data(ocirc
, rh
->length
);
1802 if (circuit_process_stream_xoff(conn
, layer_hint
, cell
)) {
1803 if (CIRCUIT_IS_ORIGIN(circ
)) {
1804 circuit_read_valid_data(TO_ORIGIN_CIRCUIT(circ
), rh
->length
);
1808 case RELAY_COMMAND_XON
:
1810 if (CIRCUIT_IS_ORIGIN(circ
)) {
1811 origin_circuit_t
*ocirc
= TO_ORIGIN_CIRCUIT(circ
);
1812 if (relay_crypt_from_last_hop(ocirc
, layer_hint
) &&
1813 connection_half_edge_is_valid_data(ocirc
->half_streams
,
1815 circuit_read_valid_data(ocirc
, rh
->length
);
1821 if (circuit_process_stream_xon(conn
, layer_hint
, cell
)) {
1822 if (CIRCUIT_IS_ORIGIN(circ
)) {
1823 circuit_read_valid_data(TO_ORIGIN_CIRCUIT(circ
), rh
->length
);
1827 case RELAY_COMMAND_END
:
1828 reason
= rh
->length
> 0 ?
1829 get_uint8(cell
->payload
+RELAY_HEADER_SIZE
) : END_STREAM_REASON_MISC
;
1831 if (CIRCUIT_IS_ORIGIN(circ
)) {
1832 origin_circuit_t
*ocirc
= TO_ORIGIN_CIRCUIT(circ
);
1833 if (relay_crypt_from_last_hop(ocirc
, layer_hint
) &&
1834 connection_half_edge_is_valid_end(ocirc
->half_streams
,
1837 circuit_read_valid_data(ocirc
, rh
->length
);
1839 "end cell (%s) on circ %u valid on half-closed "
1841 stream_end_reason_to_string(reason
),
1842 ocirc
->global_identifier
, rh
->stream_id
);
1846 log_info(domain
,"end cell (%s) dropped, unknown stream.",
1847 stream_end_reason_to_string(reason
));
1850 /* XXX add to this log_fn the exit node's nickname? */
1851 log_info(domain
,TOR_SOCKET_T_FORMAT
": end cell (%s) for stream %d. "
1854 stream_end_reason_to_string(reason
),
1856 if (conn
->base_
.type
== CONN_TYPE_AP
) {
1857 entry_connection_t
*entry_conn
= EDGE_TO_ENTRY_CONN(conn
);
1858 if (entry_conn
->socks_request
&&
1859 !entry_conn
->socks_request
->has_finished
)
1861 "open stream hasn't sent socks answer yet? Closing.");
1863 /* We just *got* an end; no reason to send one. */
1864 conn
->edge_has_sent_end
= 1;
1865 if (!conn
->end_reason
)
1866 conn
->end_reason
= reason
| END_STREAM_REASON_FLAG_REMOTE
;
1867 if (!conn
->base_
.marked_for_close
) {
1868 /* only mark it if not already marked. it's possible to
1869 * get the 'end' right around when the client hangs up on us. */
1870 connection_mark_and_flush(TO_CONN(conn
));
1872 /* Total all valid application bytes delivered */
1873 if (CIRCUIT_IS_ORIGIN(circ
)) {
1874 circuit_read_valid_data(TO_ORIGIN_CIRCUIT(circ
), rh
->length
);
1878 case RELAY_COMMAND_EXTEND
:
1879 case RELAY_COMMAND_EXTEND2
: {
1880 static uint64_t total_n_extend
=0, total_nonearly
=0;
1882 if (rh
->stream_id
) {
1883 log_fn(LOG_PROTOCOL_WARN
, domain
,
1884 "'extend' cell received for non-zero stream. Dropping.");
1887 if (cell
->command
!= CELL_RELAY_EARLY
&&
1888 !networkstatus_get_param(NULL
,"AllowNonearlyExtend",0,0,1)) {
1889 #define EARLY_WARNING_INTERVAL 3600
1890 static ratelim_t early_warning_limit
=
1891 RATELIM_INIT(EARLY_WARNING_INTERVAL
);
1893 if (cell
->command
== CELL_RELAY
) {
1895 if ((m
= rate_limit_log(&early_warning_limit
, approx_time()))) {
1896 double percentage
= ((double)total_nonearly
)/total_n_extend
;
1898 log_fn(LOG_PROTOCOL_WARN
, domain
, "EXTEND cell received, "
1899 "but not via RELAY_EARLY. Dropping.%s", m
);
1900 log_fn(LOG_PROTOCOL_WARN
, domain
, " (We have dropped %.02f%% of "
1901 "all EXTEND cells for this reason)", percentage
);
1905 log_fn(LOG_WARN
, domain
,
1906 "EXTEND cell received, in a cell with type %d! Dropping.",
1911 return circuit_extend(cell
, circ
);
1913 case RELAY_COMMAND_EXTENDED
:
1914 case RELAY_COMMAND_EXTENDED2
:
1916 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
1917 "'extended' unsupported at non-origin. Dropping.");
1920 log_debug(domain
,"Got an extended cell! Yay.");
1922 extended_cell_t extended_cell
;
1923 if (extended_cell_parse(&extended_cell
, rh
->command
,
1924 (const uint8_t*)cell
->payload
+RELAY_HEADER_SIZE
,
1926 log_warn(LD_PROTOCOL
,
1927 "Can't parse EXTENDED cell; killing circuit.");
1928 return -END_CIRC_REASON_TORPROTOCOL
;
1930 if ((reason
= circuit_finish_handshake(TO_ORIGIN_CIRCUIT(circ
),
1931 &extended_cell
.created_cell
)) < 0) {
1932 circuit_mark_for_close(circ
, -reason
);
1933 return 0; /* We don't want to cause a warning, so we mark the circuit
1937 if ((reason
=circuit_send_next_onion_skin(TO_ORIGIN_CIRCUIT(circ
)))<0) {
1938 log_info(domain
,"circuit_send_next_onion_skin() failed.");
1941 /* Total all valid bytes delivered. */
1942 if (CIRCUIT_IS_ORIGIN(circ
)) {
1943 circuit_read_valid_data(TO_ORIGIN_CIRCUIT(circ
), rh
->length
);
1946 case RELAY_COMMAND_TRUNCATE
:
1948 log_fn(LOG_PROTOCOL_WARN
, LD_APP
,
1949 "'truncate' unsupported at origin. Dropping.");
1954 log_warn(LD_BUG
, "n_chan and n_hop set on the same circuit!");
1955 extend_info_free(circ
->n_hop
);
1957 tor_free(circ
->n_chan_create_cell
);
1958 circuit_set_state(circ
, CIRCUIT_STATE_OPEN
);
1961 uint8_t trunc_reason
= get_uint8(cell
->payload
+ RELAY_HEADER_SIZE
);
1962 circuit_synchronize_written_or_bandwidth(circ
, CIRCUIT_N_CHAN
);
1963 circuit_clear_cell_queue(circ
, circ
->n_chan
);
1964 channel_send_destroy(circ
->n_circ_id
, circ
->n_chan
,
1966 circuit_set_n_circid_chan(circ
, 0, NULL
);
1968 log_debug(LD_EXIT
, "Processed 'truncate', replying.");
1971 payload
[0] = (char)END_CIRC_REASON_REQUESTED
;
1972 relay_send_command_from_edge(0, circ
, RELAY_COMMAND_TRUNCATED
,
1973 payload
, sizeof(payload
), NULL
);
1976 case RELAY_COMMAND_TRUNCATED
:
1978 log_fn(LOG_PROTOCOL_WARN
, LD_EXIT
,
1979 "'truncated' unsupported at non-origin. Dropping.");
1983 /* Count the truncated as valid, for completeness. The
1984 * circuit is being torn down anyway, though. */
1985 if (CIRCUIT_IS_ORIGIN(circ
)) {
1986 circuit_read_valid_data(TO_ORIGIN_CIRCUIT(circ
),
1989 circuit_truncated(TO_ORIGIN_CIRCUIT(circ
),
1990 get_uint8(cell
->payload
+ RELAY_HEADER_SIZE
));
1992 case RELAY_COMMAND_CONNECTED
:
1994 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
1995 "'connected' unsupported while open. Closing circ.");
1996 return -END_CIRC_REASON_TORPROTOCOL
;
1999 if (CIRCUIT_IS_ORIGIN(circ
)) {
2000 origin_circuit_t
*ocirc
= TO_ORIGIN_CIRCUIT(circ
);
2001 if (connection_half_edge_is_valid_connected(ocirc
->half_streams
,
2003 circuit_read_valid_data(ocirc
, rh
->length
);
2005 "connected cell on circ %u valid on half-closed "
2006 "stream id %d", ocirc
->global_identifier
, rh
->stream_id
);
2012 "'connected' received on circid %u for streamid %d, "
2013 "no conn attached anymore. Ignoring.",
2014 (unsigned)circ
->n_circ_id
, rh
->stream_id
);
2016 case RELAY_COMMAND_SENDME
:
2017 return process_sendme_cell(rh
, cell
, circ
, conn
, layer_hint
, domain
);
2018 case RELAY_COMMAND_RESOLVE
:
2020 log_fn(LOG_PROTOCOL_WARN
, LD_APP
,
2021 "resolve request unsupported at AP; dropping.");
2024 log_fn(LOG_PROTOCOL_WARN
, domain
,
2025 "resolve request for known stream; dropping.");
2027 } else if (circ
->purpose
!= CIRCUIT_PURPOSE_OR
) {
2028 log_fn(LOG_PROTOCOL_WARN
, domain
,
2029 "resolve request on circ with purpose %d; dropping",
2033 return connection_exit_begin_resolve(cell
, TO_OR_CIRCUIT(circ
));
2034 case RELAY_COMMAND_RESOLVED
:
2036 log_fn(LOG_PROTOCOL_WARN
, domain
,
2037 "'resolved' unsupported while open. Closing circ.");
2038 return -END_CIRC_REASON_TORPROTOCOL
;
2041 if (CIRCUIT_IS_ORIGIN(circ
)) {
2042 origin_circuit_t
*ocirc
= TO_ORIGIN_CIRCUIT(circ
);
2043 if (relay_crypt_from_last_hop(ocirc
, layer_hint
) &&
2044 connection_half_edge_is_valid_resolved(ocirc
->half_streams
,
2046 circuit_read_valid_data(ocirc
, rh
->length
);
2048 "resolved cell on circ %u valid on half-closed "
2049 "stream id %d", ocirc
->global_identifier
, rh
->stream_id
);
2055 "'resolved' received, no conn attached anymore. Ignoring.");
2057 case RELAY_COMMAND_ESTABLISH_INTRO
:
2058 case RELAY_COMMAND_ESTABLISH_RENDEZVOUS
:
2059 case RELAY_COMMAND_INTRODUCE1
:
2060 case RELAY_COMMAND_INTRODUCE2
:
2061 case RELAY_COMMAND_INTRODUCE_ACK
:
2062 case RELAY_COMMAND_RENDEZVOUS1
:
2063 case RELAY_COMMAND_RENDEZVOUS2
:
2064 case RELAY_COMMAND_INTRO_ESTABLISHED
:
2065 case RELAY_COMMAND_RENDEZVOUS_ESTABLISHED
:
2066 rend_process_relay_cell(circ
, layer_hint
,
2067 rh
->command
, rh
->length
,
2068 cell
->payload
+RELAY_HEADER_SIZE
);
2071 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
2072 "Received unknown relay command %d. Perhaps the other side is using "
2073 "a newer version of Tor? Dropping.",
2075 return 0; /* for forward compatibility, don't kill the circuit */
2078 /** An incoming relay cell has arrived on circuit <b>circ</b>. If
2079 * <b>conn</b> is NULL this is a control cell, else <b>cell</b> is
2080 * destined for <b>conn</b>.
2082 * If <b>layer_hint</b> is defined, then we're the origin of the
2083 * circuit, and it specifies the hop that packaged <b>cell</b>.
2085 * Return -reason if you want to warn and tear down the circuit, else 0.
2088 connection_edge_process_relay_cell(cell_t
*cell
, circuit_t
*circ
,
2089 edge_connection_t
*conn
,
2090 crypt_path_t
*layer_hint
)
2092 static int num_seen
=0;
2094 unsigned domain
= layer_hint
?LD_APP
:LD_EXIT
;
2099 relay_header_unpack(&rh
, cell
->payload
);
2100 // log_fn(LOG_DEBUG,"command %d stream %d", rh.command, rh.stream_id);
2102 log_debug(domain
, "Now seen %d relay cells here (command %d, stream %d).",
2103 num_seen
, rh
.command
, rh
.stream_id
);
2105 if (rh
.length
> RELAY_PAYLOAD_SIZE
) {
2106 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
2107 "Relay cell length field too long. Closing circuit.");
2108 return - END_CIRC_REASON_TORPROTOCOL
;
2111 if (rh
.stream_id
== 0) {
2112 switch (rh
.command
) {
2113 case RELAY_COMMAND_BEGIN
:
2114 case RELAY_COMMAND_CONNECTED
:
2115 case RELAY_COMMAND_END
:
2116 case RELAY_COMMAND_RESOLVE
:
2117 case RELAY_COMMAND_RESOLVED
:
2118 case RELAY_COMMAND_BEGIN_DIR
:
2119 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
, "Relay command %d with zero "
2120 "stream_id. Dropping.", (int)rh
.command
);
2127 /* Regardless of conflux or not, we always decide to send a SENDME
2128 * for RELAY_DATA immediately
2130 if (rh
.command
== RELAY_COMMAND_DATA
) {
2131 /* Update our circuit-level deliver window that we received a DATA cell.
2132 * If the deliver window goes below 0, we end the circuit and stream due
2133 * to a protocol failure. */
2134 if (sendme_circuit_data_received(circ
, layer_hint
) < 0) {
2135 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
2136 "(relay data) circ deliver_window below 0. Killing.");
2137 connection_edge_end_close(conn
, END_STREAM_REASON_TORPROTOCOL
);
2138 return -END_CIRC_REASON_TORPROTOCOL
;
2141 /* Consider sending a circuit-level SENDME cell. */
2142 sendme_circuit_consider_sending(circ
, layer_hint
);
2144 /* Continue on to process the data cell via conflux or not */
2147 /* Conflux handling: If conflux is disabled, or the relay command is not
2148 * multiplexed across circuits, then process it immediately.
2150 * Otherwise, we need to process the relay cell against our conflux
2151 * queues, and if doing so results in ordered cells to deliver, we
2152 * dequeue and process those in-order until there are no more.
2154 if (!circ
->conflux
|| !conflux_should_multiplex(rh
.command
)) {
2155 return connection_edge_process_ordered_relay_cell(cell
, circ
, conn
,
2158 // If conflux says this cell is in-order, then begin processing
2159 // cells from queue until there are none. Otherwise, we do nothing
2160 // until further cells arrive.
2161 if (conflux_process_cell(circ
->conflux
, circ
, layer_hint
, cell
)) {
2162 conflux_cell_t
*c_cell
= NULL
;
2165 /* First, process this cell */
2166 if ((ret
= connection_edge_process_ordered_relay_cell(cell
, circ
, conn
,
2167 layer_hint
, &rh
)) < 0) {
2171 /* Now, check queue for more */
2172 while ((c_cell
= conflux_dequeue_cell(circ
->conflux
))) {
2173 relay_header_unpack(&rh
, c_cell
->cell
.payload
);
2174 conn
= relay_lookup_conn(circ
, &c_cell
->cell
, CELL_DIRECTION_OUT
,
2176 if ((ret
= connection_edge_process_ordered_relay_cell(&c_cell
->cell
,
2177 circ
, conn
, layer_hint
,
2179 /* Negative return value is a fatal error. Return early and tear down
2193 * Helper function to process a relay cell that is in the proper order
2194 * for processing right now. */
2196 connection_edge_process_ordered_relay_cell(cell_t
*cell
, circuit_t
*circ
,
2197 edge_connection_t
*conn
,
2198 crypt_path_t
*layer_hint
,
2201 int optimistic_data
= 0; /* Set to 1 if we receive data on a stream
2202 * that's in the EXIT_CONN_STATE_RESOLVING
2203 * or EXIT_CONN_STATE_CONNECTING states. */
2205 /* Tell circpad that we've received a recognized cell */
2206 circpad_deliver_recognized_relay_cell_events(circ
, rh
->command
, layer_hint
);
2208 /* either conn is NULL, in which case we've got a control cell, or else
2209 * conn points to the recognized stream. */
2210 if (conn
&& !connection_state_is_open(TO_CONN(conn
))) {
2211 if (conn
->base_
.type
== CONN_TYPE_EXIT
&&
2212 (conn
->base_
.state
== EXIT_CONN_STATE_CONNECTING
||
2213 conn
->base_
.state
== EXIT_CONN_STATE_RESOLVING
) &&
2214 rh
->command
== RELAY_COMMAND_DATA
) {
2215 /* Allow DATA cells to be delivered to an exit node in state
2216 * EXIT_CONN_STATE_CONNECTING or EXIT_CONN_STATE_RESOLVING.
2217 * This speeds up HTTP, for example. */
2218 optimistic_data
= 1;
2219 } else if (rh
->stream_id
== 0 && rh
->command
== RELAY_COMMAND_DATA
) {
2220 log_warn(LD_BUG
, "Somehow I had a connection that matched a "
2221 "data cell with stream ID 0.");
2223 return connection_edge_process_relay_cell_not_open(
2224 rh
, cell
, circ
, conn
, layer_hint
);
2228 return handle_relay_cell_command(cell
, circ
, conn
, layer_hint
,
2229 rh
, optimistic_data
);
2232 /** How many relay_data cells have we built, ever? */
2233 uint64_t stats_n_data_cells_packaged
= 0;
2234 /** How many bytes of data have we put in relay_data cells have we built,
2235 * ever? This would be RELAY_PAYLOAD_SIZE*stats_n_data_cells_packaged if
2236 * every relay cell we ever sent were completely full of data. */
2237 uint64_t stats_n_data_bytes_packaged
= 0;
2238 /** How many relay_data cells have we received, ever? */
2239 uint64_t stats_n_data_cells_received
= 0;
2240 /** How many bytes of data have we received relay_data cells, ever? This would
2241 * be RELAY_PAYLOAD_SIZE*stats_n_data_cells_packaged if every relay cell we
2242 * ever received were completely full of data. */
2243 uint64_t stats_n_data_bytes_received
= 0;
2246 * Called when initializing a circuit, or when we have reached the end of the
2247 * window in which we need to send some randomness so that incoming sendme
2248 * cells will be unpredictable. Resets the flags and picks a new window.
2251 circuit_reset_sendme_randomness(circuit_t
*circ
)
2253 circ
->have_sent_sufficiently_random_cell
= 0;
2254 // XXX: do we need to change this check for congestion control?
2255 circ
->send_randomness_after_n_cells
= CIRCWINDOW_INCREMENT
/ 2 +
2256 crypto_fast_rng_get_uint(get_thread_fast_rng(), CIRCWINDOW_INCREMENT
/ 2);
2260 * Any relay data payload containing fewer than this many real bytes is
2261 * considered to have enough randomness to.
2263 #define RELAY_PAYLOAD_LENGTH_FOR_RANDOM_SENDMES \
2264 (RELAY_PAYLOAD_SIZE - CELL_PADDING_GAP - 16)
2267 * Helper. Return the number of bytes that should be put into a cell from a
2268 * given edge connection on which <b>n_available</b> bytes are available.
2271 connection_edge_get_inbuf_bytes_to_package(size_t n_available
,
2272 int package_partial
,
2273 circuit_t
*on_circuit
)
2278 /* Do we need to force this payload to have space for randomness? */
2279 const bool force_random_bytes
=
2280 (on_circuit
->send_randomness_after_n_cells
== 0) &&
2281 (! on_circuit
->have_sent_sufficiently_random_cell
);
2283 /* At most how much would we like to send in this cell? */
2284 size_t target_length
;
2285 if (force_random_bytes
) {
2286 target_length
= RELAY_PAYLOAD_LENGTH_FOR_RANDOM_SENDMES
;
2288 target_length
= RELAY_PAYLOAD_SIZE
;
2291 /* Decide how many bytes we will actually put into this cell. */
2292 size_t package_length
;
2293 if (n_available
>= target_length
) { /* A full payload is available. */
2294 package_length
= target_length
;
2295 } else { /* not a full payload available */
2296 if (package_partial
)
2297 package_length
= n_available
; /* just take whatever's available now */
2299 return 0; /* nothing to do until we have a full payload */
2302 /* If we reach this point, we will be definitely sending the cell. */
2303 tor_assert_nonfatal(package_length
> 0);
2305 if (package_length
<= RELAY_PAYLOAD_LENGTH_FOR_RANDOM_SENDMES
) {
2306 /* This cell will have enough randomness in the padding to make a future
2307 * sendme cell unpredictable. */
2308 on_circuit
->have_sent_sufficiently_random_cell
= 1;
2311 if (on_circuit
->send_randomness_after_n_cells
== 0) {
2312 /* Either this cell, or some previous cell, had enough padding to
2313 * ensure sendme unpredictability. */
2314 tor_assert_nonfatal(on_circuit
->have_sent_sufficiently_random_cell
);
2315 /* Pick a new interval in which we need to send randomness. */
2316 circuit_reset_sendme_randomness(on_circuit
);
2319 --on_circuit
->send_randomness_after_n_cells
;
2321 return package_length
;
2324 /** If <b>conn</b> has an entire relay payload of bytes on its inbuf (or
2325 * <b>package_partial</b> is true), and the appropriate package windows aren't
2326 * empty, grab a cell and send it down the circuit.
2328 * If *<b>max_cells</b> is given, package no more than max_cells. Decrement
2329 * *<b>max_cells</b> by the number of cells packaged.
2331 * Return -1 (and send a RELAY_COMMAND_END cell if necessary) if conn should
2332 * be marked for close, else return 0.
2335 connection_edge_package_raw_inbuf(edge_connection_t
*conn
, int package_partial
,
2338 size_t bytes_to_process
, length
;
2339 char payload
[CELL_PAYLOAD_SIZE
];
2341 const unsigned domain
= conn
->base_
.type
== CONN_TYPE_AP
? LD_APP
: LD_EXIT
;
2342 int sending_from_optimistic
= 0;
2343 entry_connection_t
*entry_conn
=
2344 conn
->base_
.type
== CONN_TYPE_AP
? EDGE_TO_ENTRY_CONN(conn
) : NULL
;
2345 const int sending_optimistically
=
2347 conn
->base_
.type
== CONN_TYPE_AP
&&
2348 conn
->base_
.state
!= AP_CONN_STATE_OPEN
;
2349 crypt_path_t
*cpath_layer
= conn
->cpath_layer
;
2353 if (BUG(conn
->base_
.marked_for_close
)) {
2355 "called on conn that's already marked for close at %s:%d.",
2356 conn
->base_
.marked_for_close_file
, conn
->base_
.marked_for_close
);
2360 if (max_cells
&& *max_cells
<= 0)
2363 repeat_connection_edge_package_raw_inbuf
:
2365 circ
= circuit_get_by_edge_conn(conn
);
2367 log_info(domain
,"conn has no circuit! Closing.");
2368 conn
->end_reason
= END_STREAM_REASON_CANT_ATTACH
;
2372 if (circuit_consider_stop_edge_reading(circ
, cpath_layer
))
2375 if (conn
->package_window
<= 0) {
2376 log_info(domain
,"called with package_window %d. Skipping.",
2377 conn
->package_window
);
2378 connection_stop_reading(TO_CONN(conn
));
2382 sending_from_optimistic
= entry_conn
&&
2383 entry_conn
->sending_optimistic_data
!= NULL
;
2385 if (PREDICT_UNLIKELY(sending_from_optimistic
)) {
2386 bytes_to_process
= buf_datalen(entry_conn
->sending_optimistic_data
);
2387 if (PREDICT_UNLIKELY(!bytes_to_process
)) {
2388 log_warn(LD_BUG
, "sending_optimistic_data was non-NULL but empty");
2389 bytes_to_process
= connection_get_inbuf_len(TO_CONN(conn
));
2390 sending_from_optimistic
= 0;
2393 bytes_to_process
= connection_get_inbuf_len(TO_CONN(conn
));
2396 length
= connection_edge_get_inbuf_bytes_to_package(bytes_to_process
,
2397 package_partial
, circ
);
2401 /* If we reach this point, we will definitely be packaging bytes into
2404 stats_n_data_bytes_packaged
+= length
;
2405 stats_n_data_cells_packaged
+= 1;
2407 if (PREDICT_UNLIKELY(sending_from_optimistic
)) {
2408 /* XXXX We could be more efficient here by sometimes packing
2409 * previously-sent optimistic data in the same cell with data
2410 * from the inbuf. */
2411 buf_get_bytes(entry_conn
->sending_optimistic_data
, payload
, length
);
2412 if (!buf_datalen(entry_conn
->sending_optimistic_data
)) {
2413 buf_free(entry_conn
->sending_optimistic_data
);
2414 entry_conn
->sending_optimistic_data
= NULL
;
2417 connection_buf_get_bytes(payload
, length
, TO_CONN(conn
));
2420 log_debug(domain
,TOR_SOCKET_T_FORMAT
": Packaging %d bytes (%d waiting).",
2422 (int)length
, (int)connection_get_inbuf_len(TO_CONN(conn
)));
2424 if (sending_optimistically
&& !sending_from_optimistic
) {
2425 /* This is new optimistic data; remember it in case we need to detach and
2427 if (!entry_conn
->pending_optimistic_data
)
2428 entry_conn
->pending_optimistic_data
= buf_new();
2429 buf_add(entry_conn
->pending_optimistic_data
, payload
, length
);
2432 /* Send a data cell. This handles the circuit package window. */
2433 if (connection_edge_send_command(conn
, RELAY_COMMAND_DATA
,
2434 payload
, length
) < 0 ) {
2435 /* circuit got marked for close, don't continue, don't need to mark conn */
2439 /* Handle the stream-level SENDME package window. */
2440 if (sendme_note_stream_data_packaged(conn
, length
) < 0) {
2441 connection_stop_reading(TO_CONN(conn
));
2442 log_debug(domain
,"conn->package_window reached 0.");
2443 circuit_consider_stop_edge_reading(circ
, cpath_layer
);
2444 return 0; /* don't process the inbuf any more */
2446 log_debug(domain
,"conn->package_window is now %d",conn
->package_window
);
2450 if (*max_cells
<= 0)
2454 /* handle more if there's more, or return 0 if there isn't */
2455 goto repeat_connection_edge_package_raw_inbuf
;
2458 /** The circuit <b>circ</b> has received a circuit-level sendme
2459 * (on hop <b>layer_hint</b>, if we're the OP). Go through all the
2460 * attached streams and let them resume reading and packaging, if
2461 * their stream windows allow it.
2464 circuit_resume_edge_reading(circuit_t
*circ
, crypt_path_t
*layer_hint
)
2466 if (circuit_queue_streams_are_blocked(circ
)) {
2467 log_debug(layer_hint
?LD_APP
:LD_EXIT
,"Too big queue, no resuming");
2471 /* If we have a conflux negotiated, and it still can't send on
2472 * any circuit, then do not resume sending. */
2473 if (circ
->conflux
&& !conflux_can_send(circ
->conflux
)) {
2474 log_debug(layer_hint
?LD_APP
:LD_EXIT
,
2475 "Conflux can't send, not resuming edges");
2479 log_debug(layer_hint
?LD_APP
:LD_EXIT
,"resuming");
2481 if (CIRCUIT_IS_ORIGIN(circ
))
2482 circuit_resume_edge_reading_helper(TO_ORIGIN_CIRCUIT(circ
)->p_streams
,
2485 circuit_resume_edge_reading_helper(TO_OR_CIRCUIT(circ
)->n_streams
,
2489 /** A helper function for circuit_resume_edge_reading() above.
2490 * The arguments are the same, except that <b>conn</b> is the head
2491 * of a linked list of edge streams that should each be considered.
2494 circuit_resume_edge_reading_helper(edge_connection_t
*first_conn
,
2496 crypt_path_t
*layer_hint
)
2498 edge_connection_t
*conn
;
2499 int n_packaging_streams
, n_streams_left
;
2500 int packaged_this_round
;
2503 edge_connection_t
*chosen_stream
= NULL
;
2506 if (first_conn
== NULL
) {
2507 /* Don't bother to try to do the rest of this if there are no connections
2512 /* Once we used to start listening on the streams in the order they
2513 * appeared in the linked list. That leads to starvation on the
2514 * streams that appeared later on the list, since the first streams
2515 * would always get to read first. Instead, we just pick a random
2516 * stream on the list, and enable reading for streams starting at that
2517 * point (and wrapping around as if the list were circular). It would
2518 * probably be better to actually remember which streams we've
2519 * serviced in the past, but this is simple and effective. */
2521 /* Select a stream uniformly at random from the linked list. We
2522 * don't need cryptographic randomness here. */
2524 int num_streams
= 0;
2525 for (conn
= first_conn
; conn
; conn
= conn
->next_stream
) {
2528 if (crypto_fast_rng_one_in_n(get_thread_fast_rng(), num_streams
)) {
2529 chosen_stream
= conn
;
2531 /* Invariant: chosen_stream has been chosen uniformly at random from
2532 * among the first num_streams streams on first_conn.
2534 * (Note that we iterate over every stream on the circuit, so that after
2535 * we've considered the first stream, we've chosen it with P=1; and
2536 * after we consider the second stream, we've switched to it with P=1/2
2537 * and stayed with the first stream with P=1/2; and after we've
2538 * considered the third stream, we've switched to it with P=1/3 and
2539 * remained with one of the first two streams with P=(2/3), giving each
2540 * one P=(1/2)(2/3) )=(1/3).) */
2544 /* Count how many non-marked streams there are that have anything on
2545 * their inbuf, and enable reading on all of the connections. */
2546 n_packaging_streams
= 0;
2547 /* Activate reading starting from the chosen stream */
2548 for (conn
=chosen_stream
; conn
; conn
= conn
->next_stream
) {
2549 /* Start reading for the streams starting from here */
2550 if (conn
->base_
.marked_for_close
|| conn
->package_window
<= 0)
2553 if (edge_uses_cpath(conn
, layer_hint
)) {
2554 if (!conn
->xoff_received
) {
2555 connection_start_reading(TO_CONN(conn
));
2558 if (connection_get_inbuf_len(TO_CONN(conn
)) > 0)
2559 ++n_packaging_streams
;
2562 /* Go back and do the ones we skipped, circular-style */
2563 for (conn
= first_conn
; conn
!= chosen_stream
; conn
= conn
->next_stream
) {
2564 if (conn
->base_
.marked_for_close
|| conn
->package_window
<= 0)
2567 if (edge_uses_cpath(conn
, layer_hint
)) {
2568 if (!conn
->xoff_received
) {
2569 connection_start_reading(TO_CONN(conn
));
2572 if (connection_get_inbuf_len(TO_CONN(conn
)) > 0)
2573 ++n_packaging_streams
;
2577 if (n_packaging_streams
== 0) /* avoid divide-by-zero */
2582 /* If we're using conflux, the circuit we decide to send on may change
2583 * after we're sending. Get it again, and re-check package windows
2585 if (circ
->conflux
) {
2586 if (circuit_consider_stop_edge_reading(circ
, layer_hint
))
2589 circ
= conflux_decide_next_circ(circ
->conflux
);
2591 /* Get the destination layer hint for this circuit */
2592 layer_hint
= conflux_get_destination_hop(circ
);
2595 /* How many cells do we have space for? It will be the minimum of
2596 * the number needed to exhaust the package window, and the minimum
2597 * needed to fill the cell queue. */
2598 max_to_package
= congestion_control_get_package_window(circ
, layer_hint
);
2599 if (CIRCUIT_IS_ORIGIN(circ
)) {
2600 cells_on_queue
= circ
->n_chan_cells
.n
;
2602 or_circuit_t
*or_circ
= TO_OR_CIRCUIT(circ
);
2603 cells_on_queue
= or_circ
->p_chan_cells
.n
;
2605 if (cell_queue_highwatermark() - cells_on_queue
< max_to_package
)
2606 max_to_package
= cell_queue_highwatermark() - cells_on_queue
;
2608 cells_per_conn
= CEIL_DIV(max_to_package
, n_packaging_streams
);
2610 packaged_this_round
= 0;
2613 /* Iterate over all connections. Package up to cells_per_conn cells on
2614 * each. Update packaged_this_round with the total number of cells
2615 * packaged, and n_streams_left with the number that still have data to
2618 for (conn
=first_conn
; conn
; conn
=conn
->next_stream
) {
2619 if (conn
->base_
.marked_for_close
|| conn
->package_window
<= 0)
2621 if (edge_uses_cpath(conn
, layer_hint
)) {
2622 int n
= cells_per_conn
, r
;
2623 /* handle whatever might still be on the inbuf */
2624 r
= connection_edge_package_raw_inbuf(conn
, 1, &n
);
2626 /* Note how many we packaged */
2627 packaged_this_round
+= (cells_per_conn
-n
);
2630 /* Problem while packaging. (We already sent an end cell if
2632 connection_mark_for_close(TO_CONN(conn
));
2636 /* If there's still data to read, we'll be coming back to this stream. */
2637 if (connection_get_inbuf_len(TO_CONN(conn
)))
2640 /* If the circuit won't accept any more data, return without looking
2641 * at any more of the streams. Any connections that should be stopped
2642 * have already been stopped by connection_edge_package_raw_inbuf. */
2643 if (circuit_consider_stop_edge_reading(circ
, layer_hint
))
2645 /* XXXX should we also stop immediately if we fill up the cell queue?
2650 /* If we made progress, and we are willing to package more, and there are
2651 * any streams left that want to package stuff... try again!
2653 if (packaged_this_round
&& packaged_this_round
< max_to_package
&&
2655 n_packaging_streams
= n_streams_left
;
2662 /** Check if the package window for <b>circ</b> is empty (at
2663 * hop <b>layer_hint</b> if it's defined).
2665 * If yes, tell edge streams to stop reading and return 1.
2669 circuit_consider_stop_edge_reading(circuit_t
*circ
, crypt_path_t
*layer_hint
)
2671 edge_connection_t
*conn
= NULL
;
2672 unsigned domain
= layer_hint
? LD_APP
: LD_EXIT
;
2675 or_circuit_t
*or_circ
= TO_OR_CIRCUIT(circ
);
2676 log_debug(domain
,"considering circ->package_window %d",
2677 circ
->package_window
);
2678 if (circuit_get_package_window(circ
, layer_hint
) <= 0) {
2679 log_debug(domain
,"yes, not-at-origin. stopped.");
2680 for (conn
= or_circ
->n_streams
; conn
; conn
=conn
->next_stream
)
2681 connection_stop_reading(TO_CONN(conn
));
2686 /* else, layer hint is defined, use it */
2687 log_debug(domain
,"considering layer_hint->package_window %d",
2688 layer_hint
->package_window
);
2689 if (circuit_get_package_window(circ
, layer_hint
) <= 0) {
2690 log_debug(domain
,"yes, at-origin. stopped.");
2691 for (conn
= TO_ORIGIN_CIRCUIT(circ
)->p_streams
; conn
;
2692 conn
=conn
->next_stream
) {
2693 if (edge_uses_cpath(conn
, layer_hint
))
2694 connection_stop_reading(TO_CONN(conn
));
2701 /** The total number of cells we have allocated. */
2702 static size_t total_cells_allocated
= 0;
2704 /** Release storage held by <b>cell</b>. */
2706 packed_cell_free_unchecked(packed_cell_t
*cell
)
2708 --total_cells_allocated
;
2712 /** Allocate and return a new packed_cell_t. */
2713 STATIC packed_cell_t
*
2714 packed_cell_new(void)
2716 ++total_cells_allocated
;
2717 return tor_malloc_zero(sizeof(packed_cell_t
));
2720 /** Return a packed cell used outside by channel_t lower layer */
2722 packed_cell_free_(packed_cell_t
*cell
)
2726 packed_cell_free_unchecked(cell
);
2729 /** Log current statistics for cell pool allocation at log level
2730 * <b>severity</b>. */
2732 dump_cell_pool_usage(int severity
)
2736 SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t
*, c
) {
2737 n_cells
+= c
->n_chan_cells
.n
;
2738 if (!CIRCUIT_IS_ORIGIN(c
))
2739 n_cells
+= TO_OR_CIRCUIT(c
)->p_chan_cells
.n
;
2742 SMARTLIST_FOREACH_END(c
);
2743 tor_log(severity
, LD_MM
,
2744 "%d cells allocated on %d circuits. %d cells leaked.",
2745 n_cells
, n_circs
, (int)total_cells_allocated
- n_cells
);
2748 /** Allocate a new copy of packed <b>cell</b>. */
2749 static inline packed_cell_t
*
2750 packed_cell_copy(const cell_t
*cell
, int wide_circ_ids
)
2752 packed_cell_t
*c
= packed_cell_new();
2753 cell_pack(c
, cell
, wide_circ_ids
);
2757 /** Append <b>cell</b> to the end of <b>queue</b>. */
2759 cell_queue_append(cell_queue_t
*queue
, packed_cell_t
*cell
)
2761 TOR_SIMPLEQ_INSERT_TAIL(&queue
->head
, cell
, next
);
2765 /** Append a newly allocated copy of <b>cell</b> to the end of the
2766 * <b>exitward</b> (or app-ward) <b>queue</b> of <b>circ</b>. If
2767 * <b>use_stats</b> is true, record statistics about the cell.
2770 cell_queue_append_packed_copy(circuit_t
*circ
, cell_queue_t
*queue
,
2771 int exitward
, const cell_t
*cell
,
2772 int wide_circ_ids
, int use_stats
)
2774 packed_cell_t
*copy
= packed_cell_copy(cell
, wide_circ_ids
);
2779 copy
->inserted_timestamp
= monotime_coarse_get_stamp();
2781 cell_queue_append(queue
, copy
);
2784 /** Initialize <b>queue</b> as an empty cell queue. */
2786 cell_queue_init(cell_queue_t
*queue
)
2788 memset(queue
, 0, sizeof(cell_queue_t
));
2789 TOR_SIMPLEQ_INIT(&queue
->head
);
2792 /** Remove and free every cell in <b>queue</b>. */
2794 cell_queue_clear(cell_queue_t
*queue
)
2796 packed_cell_t
*cell
;
2797 while ((cell
= TOR_SIMPLEQ_FIRST(&queue
->head
))) {
2798 TOR_SIMPLEQ_REMOVE_HEAD(&queue
->head
, next
);
2799 packed_cell_free_unchecked(cell
);
2801 TOR_SIMPLEQ_INIT(&queue
->head
);
2805 /** Extract and return the cell at the head of <b>queue</b>; return NULL if
2806 * <b>queue</b> is empty. */
2807 STATIC packed_cell_t
*
2808 cell_queue_pop(cell_queue_t
*queue
)
2810 packed_cell_t
*cell
= TOR_SIMPLEQ_FIRST(&queue
->head
);
2813 TOR_SIMPLEQ_REMOVE_HEAD(&queue
->head
, next
);
2818 /** Initialize <b>queue</b> as an empty cell queue. */
2820 destroy_cell_queue_init(destroy_cell_queue_t
*queue
)
2822 memset(queue
, 0, sizeof(destroy_cell_queue_t
));
2823 TOR_SIMPLEQ_INIT(&queue
->head
);
2826 /** Remove and free every cell in <b>queue</b>. */
2828 destroy_cell_queue_clear(destroy_cell_queue_t
*queue
)
2830 destroy_cell_t
*cell
;
2831 while ((cell
= TOR_SIMPLEQ_FIRST(&queue
->head
))) {
2832 TOR_SIMPLEQ_REMOVE_HEAD(&queue
->head
, next
);
2835 TOR_SIMPLEQ_INIT(&queue
->head
);
2839 /** Extract and return the cell at the head of <b>queue</b>; return NULL if
2840 * <b>queue</b> is empty. */
2841 STATIC destroy_cell_t
*
2842 destroy_cell_queue_pop(destroy_cell_queue_t
*queue
)
2844 destroy_cell_t
*cell
= TOR_SIMPLEQ_FIRST(&queue
->head
);
2847 TOR_SIMPLEQ_REMOVE_HEAD(&queue
->head
, next
);
2852 /** Append a destroy cell for <b>circid</b> to <b>queue</b>. */
2854 destroy_cell_queue_append(destroy_cell_queue_t
*queue
,
2858 destroy_cell_t
*cell
= tor_malloc_zero(sizeof(destroy_cell_t
));
2859 cell
->circid
= circid
;
2860 cell
->reason
= reason
;
2861 /* Not yet used, but will be required for OOM handling. */
2862 cell
->inserted_timestamp
= monotime_coarse_get_stamp();
2864 TOR_SIMPLEQ_INSERT_TAIL(&queue
->head
, cell
, next
);
2868 /** Convert a destroy_cell_t to a newly allocated cell_t. Frees its input. */
2869 static packed_cell_t
*
2870 destroy_cell_to_packed_cell(destroy_cell_t
*inp
, int wide_circ_ids
)
2872 packed_cell_t
*packed
= packed_cell_new();
2874 memset(&cell
, 0, sizeof(cell
));
2875 cell
.circ_id
= inp
->circid
;
2876 cell
.command
= CELL_DESTROY
;
2877 cell
.payload
[0] = inp
->reason
;
2878 cell_pack(packed
, &cell
, wide_circ_ids
);
2884 /** Return the total number of bytes used for each packed_cell in a queue.
2887 packed_cell_mem_cost(void)
2889 return sizeof(packed_cell_t
);
2894 cell_queues_get_total_allocation(void)
2896 return total_cells_allocated
* packed_cell_mem_cost();
2899 /** How long after we've been low on memory should we try to conserve it? */
2900 #define MEMORY_PRESSURE_INTERVAL (30*60)
2902 /** The time at which we were last low on memory. */
2903 static time_t last_time_under_memory_pressure
= 0;
2905 /** Statistics on how many bytes were removed by the OOM per type. */
2906 uint64_t oom_stats_n_bytes_removed_dns
= 0;
2907 uint64_t oom_stats_n_bytes_removed_cell
= 0;
2908 uint64_t oom_stats_n_bytes_removed_geoip
= 0;
2909 uint64_t oom_stats_n_bytes_removed_hsdir
= 0;
2911 /** Check whether we've got too much space used for cells. If so,
2912 * call the OOM handler and return 1. Otherwise, return 0. */
2914 cell_queues_check_size(void)
2917 time_t now
= time(NULL
);
2918 size_t alloc
= cell_queues_get_total_allocation();
2919 alloc
+= half_streams_get_total_allocation();
2920 alloc
+= buf_get_total_allocation();
2921 alloc
+= tor_compress_get_total_allocation();
2922 const size_t hs_cache_total
= hs_cache_get_total_allocation();
2923 alloc
+= hs_cache_total
;
2924 const size_t geoip_client_cache_total
=
2925 geoip_client_cache_total_allocation();
2926 alloc
+= geoip_client_cache_total
;
2927 const size_t dns_cache_total
= dns_cache_total_allocation();
2928 alloc
+= dns_cache_total
;
2929 const size_t conflux_total
= conflux_get_total_bytes_allocation();
2930 alloc
+= conflux_total
;
2931 if (alloc
>= get_options()->MaxMemInQueues_low_threshold
) {
2932 last_time_under_memory_pressure
= approx_time();
2933 if (alloc
>= get_options()->MaxMemInQueues
) {
2934 /* Note this overload down */
2935 rep_hist_note_overload(OVERLOAD_GENERAL
);
2937 /* If we're spending over 20% of the memory limit on hidden service
2938 * descriptors, free them until we're down to 10%. Do the same for geoip
2940 if (hs_cache_total
> get_options()->MaxMemInQueues
/ 5) {
2941 const size_t bytes_to_remove
=
2942 hs_cache_total
- (size_t)(get_options()->MaxMemInQueues
/ 10);
2943 removed
= hs_cache_handle_oom(bytes_to_remove
);
2944 oom_stats_n_bytes_removed_hsdir
+= removed
;
2947 if (geoip_client_cache_total
> get_options()->MaxMemInQueues
/ 5) {
2948 const size_t bytes_to_remove
=
2949 geoip_client_cache_total
-
2950 (size_t)(get_options()->MaxMemInQueues
/ 10);
2951 removed
= geoip_client_cache_handle_oom(now
, bytes_to_remove
);
2952 oom_stats_n_bytes_removed_geoip
+= removed
;
2955 if (dns_cache_total
> get_options()->MaxMemInQueues
/ 5) {
2956 const size_t bytes_to_remove
=
2957 dns_cache_total
- (size_t)(get_options()->MaxMemInQueues
/ 10);
2958 removed
= dns_cache_handle_oom(now
, bytes_to_remove
);
2959 oom_stats_n_bytes_removed_dns
+= removed
;
2962 /* Like onion service above, try to go down to 10% if we are above 20% */
2963 if (conflux_total
> get_options()->MaxMemInQueues
/ 5) {
2964 const size_t bytes_to_remove
=
2965 conflux_total
- (size_t)(get_options()->MaxMemInQueues
/ 10);
2966 removed
= conflux_handle_oom(bytes_to_remove
);
2967 oom_stats_n_bytes_removed_cell
+= removed
;
2970 removed
= circuits_handle_oom(alloc
);
2971 oom_stats_n_bytes_removed_cell
+= removed
;
2978 /** Return true if we've been under memory pressure in the last
2979 * MEMORY_PRESSURE_INTERVAL seconds. */
2981 have_been_under_memory_pressure(void)
2983 return last_time_under_memory_pressure
+ MEMORY_PRESSURE_INTERVAL
2988 * Update the number of cells available on the circuit's n_chan or p_chan's
2992 update_circuit_on_cmux_(circuit_t
*circ
, cell_direction_t direction
,
2993 const char *file
, int lineno
)
2995 channel_t
*chan
= NULL
;
2996 or_circuit_t
*or_circ
= NULL
;
2997 circuitmux_t
*cmux
= NULL
;
3001 /* Okay, get the channel */
3002 if (direction
== CELL_DIRECTION_OUT
) {
3003 chan
= circ
->n_chan
;
3005 or_circ
= TO_OR_CIRCUIT(circ
);
3006 chan
= or_circ
->p_chan
;
3010 tor_assert(chan
->cmux
);
3012 /* Now get the cmux */
3015 /* Cmux sanity check */
3016 if (! circuitmux_is_circuit_attached(cmux
, circ
)) {
3017 log_warn(LD_BUG
, "called on non-attached circuit from %s:%d",
3021 tor_assert(circuitmux_attached_circuit_direction(cmux
, circ
) == direction
);
3023 /* Update the number of cells we have for the circuit mux */
3024 if (direction
== CELL_DIRECTION_OUT
) {
3025 circuitmux_set_num_cells(cmux
, circ
, circ
->n_chan_cells
.n
);
3027 circuitmux_set_num_cells(cmux
, circ
, or_circ
->p_chan_cells
.n
);
3031 /** Remove all circuits from the cmux on <b>chan</b>.
3033 * If <b>circuits_out</b> is non-NULL, add all detached circuits to
3034 * <b>circuits_out</b>.
3037 channel_unlink_all_circuits(channel_t
*chan
, smartlist_t
*circuits_out
)
3040 tor_assert(chan
->cmux
);
3042 circuitmux_detach_all_circuits(chan
->cmux
, circuits_out
);
3043 chan
->num_n_circuits
= 0;
3044 chan
->num_p_circuits
= 0;
3048 * Called when a circuit becomes blocked or unblocked due to the channel
3051 * Block (if <b>block</b> is true) or unblock (if <b>block</b> is false)
3052 * every edge connection that is using <b>circ</b> to write to <b>chan</b>,
3053 * and start or stop reading as appropriate.
3056 set_circuit_blocked_on_chan(circuit_t
*circ
, channel_t
*chan
, int block
)
3058 edge_connection_t
*edge
= NULL
;
3059 if (circ
->n_chan
== chan
) {
3060 circ
->circuit_blocked_on_n_chan
= block
;
3061 if (CIRCUIT_IS_ORIGIN(circ
))
3062 edge
= TO_ORIGIN_CIRCUIT(circ
)->p_streams
;
3064 circ
->circuit_blocked_on_p_chan
= block
;
3065 tor_assert(!CIRCUIT_IS_ORIGIN(circ
));
3066 edge
= TO_OR_CIRCUIT(circ
)->n_streams
;
3069 set_block_state_for_streams(circ
, edge
, block
, 0);
3073 * Helper function to block or unblock streams in a stream list.
3075 * If <b>stream_id</b> is 0, apply the <b>block</b> state to all streams
3076 * in the stream list. If it is non-zero, only apply to that specific stream.
3079 set_block_state_for_streams(circuit_t
*circ
, edge_connection_t
*stream_list
,
3080 int block
, streamid_t stream_id
)
3082 /* If we have a conflux object, we need to examine its status before
3083 * blocking and unblocking streams. */
3084 if (circ
->conflux
) {
3085 bool can_send
= conflux_can_send(circ
->conflux
);
3087 if (block
&& can_send
) {
3088 /* Don't actually block streams, since conflux can send*/
3090 } else if (!block
&& !can_send
) {
3091 /* Don't actually unblock streams, since conflux still can't send */
3096 for (edge_connection_t
*edge
= stream_list
; edge
; edge
= edge
->next_stream
) {
3097 connection_t
*conn
= TO_CONN(edge
);
3098 if (stream_id
&& edge
->stream_id
!= stream_id
)
3101 if (!conn
->read_event
|| edge
->xoff_received
||
3102 conn
->marked_for_close
) {
3103 /* This connection should not start or stop reading. */
3108 if (connection_is_reading(conn
))
3109 connection_stop_reading(conn
);
3111 /* Is this right? */
3112 if (!connection_is_reading(conn
))
3113 connection_start_reading(conn
);
3118 /** Extract the command from a packed cell. */
3120 packed_cell_get_command(const packed_cell_t
*cell
, int wide_circ_ids
)
3122 if (wide_circ_ids
) {
3123 return get_uint8(cell
->body
+4);
3125 return get_uint8(cell
->body
+2);
3129 /** Extract the circuit ID from a packed cell. */
3131 packed_cell_get_circid(const packed_cell_t
*cell
, int wide_circ_ids
)
3133 if (wide_circ_ids
) {
3134 return ntohl(get_uint32(cell
->body
));
3136 return ntohs(get_uint16(cell
->body
));
3140 /** Pull as many cells as possible (but no more than <b>max</b>) from the
3141 * queue of the first active circuit on <b>chan</b>, and write them to
3142 * <b>chan</b>->outbuf. Return the number of cells written. Advance
3143 * the active circuit pointer to the next active circuit in the ring. */
3145 channel_flush_from_first_active_circuit
, (channel_t
*chan
, int max
))
3147 circuitmux_t
*cmux
= NULL
;
3149 cell_queue_t
*queue
;
3150 destroy_cell_queue_t
*destroy_queue
=NULL
;
3152 or_circuit_t
*or_circ
;
3154 packed_cell_t
*cell
;
3158 tor_assert(chan
->cmux
);
3161 /* Main loop: pick a circuit, send a cell, update the cmux */
3162 while (n_flushed
< max
) {
3163 circ
= circuitmux_get_first_active_circuit(cmux
, &destroy_queue
);
3164 if (destroy_queue
) {
3165 destroy_cell_t
*dcell
;
3166 /* this code is duplicated from some of the logic below. Ugly! XXXX */
3167 /* If we are given a destroy_queue here, then it is required to be
3169 tor_assert(destroy_queue
->n
> 0);
3170 dcell
= destroy_cell_queue_pop(destroy_queue
);
3171 /* ...and pop() will always yield a cell from a nonempty queue. */
3174 cell
= destroy_cell_to_packed_cell(dcell
, chan
->wide_circ_ids
);
3175 /* Send the DESTROY cell. It is very unlikely that this fails but just
3176 * in case, get rid of the channel. */
3177 if (channel_write_packed_cell(chan
, cell
) < 0) {
3178 /* The cell has been freed. */
3179 channel_mark_for_close(chan
);
3182 /* Update the cmux destroy counter */
3183 circuitmux_notify_xmit_destroy(cmux
);
3188 /* If it returns NULL, no cells left to send */
3191 if (circ
->n_chan
== chan
) {
3192 queue
= &circ
->n_chan_cells
;
3193 circ_blocked
= circ
->circuit_blocked_on_n_chan
;
3195 or_circ
= TO_OR_CIRCUIT(circ
);
3196 tor_assert(or_circ
->p_chan
== chan
);
3197 queue
= &TO_OR_CIRCUIT(circ
)->p_chan_cells
;
3198 circ_blocked
= circ
->circuit_blocked_on_p_chan
;
3201 /* Circuitmux told us this was active, so it should have cells.
3203 * Note: In terms of logic and coherence, this should never happen but the
3204 * cmux dragon is powerful. Reason is that when the OOM is triggered, when
3205 * cleaning up circuits, we mark them for close and then clear their cell
3206 * queues. And so, we can have a circuit considered active by the cmux
3207 * dragon but without cells. The cmux subsystem is only notified of this
3208 * when the circuit is freed which leaves a tiny window between close and
3209 * free to end up here.
3211 * We are accepting this as an "ok" race else the changes are likely non
3212 * trivial to make the mark for close to set the num cells to 0 and change
3213 * the free functions to detach the circuit conditionally without creating
3214 * a chain effect of madness.
3216 * The lesson here is arti will prevail and leave the cmux dragon alone. */
3217 if (queue
->n
== 0) {
3218 circuitmux_set_num_cells(cmux
, circ
, 0);
3219 if (! circ
->marked_for_close
)
3220 circuit_mark_for_close(circ
, END_CIRC_REASON_INTERNAL
);
3224 tor_assert(queue
->n
> 0);
3227 * Get just one cell here; once we've sent it, that can change the circuit
3228 * selection, so we have to loop around for another even if this circuit
3229 * has more than one.
3231 cell
= cell_queue_pop(queue
);
3233 /* Calculate the exact time that this cell has spent in the queue. */
3234 if (get_options()->CellStatistics
||
3235 get_options()->TestingEnableCellStatsEvent
) {
3236 uint32_t timestamp_now
= monotime_coarse_get_stamp();
3237 uint32_t msec_waiting
=
3238 (uint32_t) monotime_coarse_stamp_units_to_approx_msec(
3239 timestamp_now
- cell
->inserted_timestamp
);
3241 if (get_options()->CellStatistics
&& !CIRCUIT_IS_ORIGIN(circ
)) {
3242 or_circ
= TO_OR_CIRCUIT(circ
);
3243 or_circ
->total_cell_waiting_time
+= msec_waiting
;
3244 or_circ
->processed_cells
++;
3247 if (get_options()->TestingEnableCellStatsEvent
) {
3248 uint8_t command
= packed_cell_get_command(cell
, chan
->wide_circ_ids
);
3250 testing_cell_stats_entry_t
*ent
=
3251 tor_malloc_zero(sizeof(testing_cell_stats_entry_t
));
3252 ent
->command
= command
;
3253 ent
->waiting_time
= msec_waiting
/ 10;
3255 if (circ
->n_chan
== chan
)
3257 if (!circ
->testing_cell_stats
)
3258 circ
->testing_cell_stats
= smartlist_new();
3259 smartlist_add(circ
->testing_cell_stats
, ent
);
3263 /* If we just flushed our queue and this circuit is used for a
3264 * tunneled directory request, possibly advance its state. */
3265 if (queue
->n
== 0 && chan
->dirreq_id
)
3266 geoip_change_dirreq_state(chan
->dirreq_id
,
3268 DIRREQ_CIRC_QUEUE_FLUSHED
);
3270 /* Now send the cell. It is very unlikely that this fails but just in
3271 * case, get rid of the channel. */
3272 if (channel_write_packed_cell(chan
, cell
) < 0) {
3273 /* The cell has been freed at this point. */
3274 channel_mark_for_close(chan
);
3280 * Don't packed_cell_free_unchecked(cell) here because the channel will
3281 * do so when it gets out of the channel queue (probably already did, in
3282 * which case that was an immediate double-free bug).
3285 /* Update the counter */
3289 * Now update the cmux; tell it we've just sent a cell, and how many
3292 circuitmux_notify_xmit_cells(cmux
, circ
, 1);
3293 circuitmux_set_num_cells(cmux
, circ
, queue
->n
);
3295 log_debug(LD_GENERAL
, "Made a circuit inactive.");
3297 /* Is the cell queue low enough to unblock all the streams that are waiting
3298 * to write to this circuit? */
3299 if (circ_blocked
&& queue
->n
<= cell_queue_lowwatermark())
3300 set_circuit_blocked_on_chan(circ
, chan
, 0); /* unblock streams */
3302 /* If n_flushed < max still, loop around and pick another circuit */
3305 /* Okay, we're done sending now */
3309 /* Minimum value is the maximum circuit window size.
3311 * This value is set to a lower bound we believe is reasonable with congestion
3312 * control and basic network running parameters.
3314 * SENDME cells makes it that we can control how many cells can be inflight on
3315 * a circuit from end to end. This logic makes it that on any circuit cell
3316 * queue, we have a maximum of cells possible.
3318 * Because the Tor protocol allows for a client to exit at any hop in a
3319 * circuit and a circuit can be of a maximum of 8 hops, so in theory the
3320 * normal worst case will be the circuit window start value times the maximum
3321 * number of hops (8). Having more cells then that means something is wrong.
3323 * However, because padding cells aren't counted in the package window, we set
3324 * the maximum size to a reasonably large size for which we expect that we'll
3325 * never reach in theory. And if we ever do because of future changes, we'll
3326 * be able to control it with a consensus parameter.
3328 * XXX: Unfortunately, END cells aren't accounted for in the circuit window
3329 * which means that for instance if a client opens 8001 streams, the 8001
3330 * following END cells will queue up in the circuit which will get closed if
3331 * the max limit is 8000. Which is sad because it is allowed by the Tor
3332 * protocol. But, we need an upper bound on circuit queue in order to avoid
3333 * DoS memory pressure so the default size is a middle ground between not
3334 * having any limit and having a very restricted one. This is why we can also
3335 * control it through a consensus parameter. */
3336 #define RELAY_CIRC_CELL_QUEUE_SIZE_MIN 50
3337 /* We can't have a consensus parameter above this value. */
3338 #define RELAY_CIRC_CELL_QUEUE_SIZE_MAX INT32_MAX
3339 /* Default value is set to a large value so we can handle padding cells
3340 * properly which aren't accounted for in the SENDME window. Default is 2500
3341 * allowed cells in the queue resulting in ~1MB. */
3342 #define RELAY_CIRC_CELL_QUEUE_SIZE_DEFAULT \
3343 (50 * RELAY_CIRC_CELL_QUEUE_SIZE_MIN)
3345 /* The maximum number of cells a circuit queue can contain. This is updated at
3346 * every new consensus and controlled by a parameter. */
3347 static int32_t max_circuit_cell_queue_size
=
3348 RELAY_CIRC_CELL_QUEUE_SIZE_DEFAULT
;
3349 /** Maximum number of cell on an outbound circuit queue. This is updated at
3350 * every new consensus and controlled by a parameter. This default is incorrect
3351 * and won't be used at all except in unit tests. */
3352 static int32_t max_circuit_cell_queue_size_out
=
3353 RELAY_CIRC_CELL_QUEUE_SIZE_DEFAULT
;
3355 /** Return consensus parameter "circ_max_cell_queue_size". The given ns can be
3358 get_param_max_circuit_cell_queue_size(const networkstatus_t
*ns
)
3360 return networkstatus_get_param(ns
, "circ_max_cell_queue_size",
3361 RELAY_CIRC_CELL_QUEUE_SIZE_DEFAULT
,
3362 RELAY_CIRC_CELL_QUEUE_SIZE_MIN
,
3363 RELAY_CIRC_CELL_QUEUE_SIZE_MAX
);
3366 /** Return consensus parameter "circ_max_cell_queue_size_out". The given ns can
3369 get_param_max_circuit_cell_queue_size_out(const networkstatus_t
*ns
)
3371 return networkstatus_get_param(ns
, "circ_max_cell_queue_size_out",
3372 get_param_max_circuit_cell_queue_size(ns
),
3373 RELAY_CIRC_CELL_QUEUE_SIZE_MIN
,
3374 RELAY_CIRC_CELL_QUEUE_SIZE_MAX
);
3377 /* Called when the consensus has changed. At this stage, the global consensus
3378 * object has NOT been updated. It is called from
3379 * notify_before_networkstatus_changes(). */
3381 relay_consensus_has_changed(const networkstatus_t
*ns
)
3385 /* Update the circuit max cell queue size from the consensus. */
3386 max_circuit_cell_queue_size
=
3387 get_param_max_circuit_cell_queue_size(ns
);
3388 max_circuit_cell_queue_size_out
=
3389 get_param_max_circuit_cell_queue_size_out(ns
);
3392 /** Add <b>cell</b> to the queue of <b>circ</b> writing to <b>chan</b>
3393 * transmitting in <b>direction</b>.
3395 * The given <b>cell</b> is copied onto the circuit queue so the caller must
3396 * cleanup the memory.
3398 * This function is part of the fast path.
3400 * Return 1 if the cell was successfully sent.
3401 * Return 0 if the cell can not be sent. The caller MUST NOT close the circuit.
3402 * Return -1 indicating an error and that the caller should mark the circuit
3405 append_cell_to_circuit_queue(circuit_t
*circ
, channel_t
*chan
,
3406 cell_t
*cell
, cell_direction_t direction
,
3407 streamid_t fromstream
)
3409 or_circuit_t
*orcirc
= NULL
;
3410 edge_connection_t
*stream_list
= NULL
;
3411 cell_queue_t
*queue
;
3412 int32_t max_queue_size
;
3415 if (circ
->marked_for_close
) {
3419 exitward
= (direction
== CELL_DIRECTION_OUT
);
3421 queue
= &circ
->n_chan_cells
;
3422 circ_blocked
= circ
->circuit_blocked_on_n_chan
;
3423 max_queue_size
= max_circuit_cell_queue_size_out
;
3424 if (CIRCUIT_IS_ORIGIN(circ
))
3425 stream_list
= TO_ORIGIN_CIRCUIT(circ
)->p_streams
;
3427 orcirc
= TO_OR_CIRCUIT(circ
);
3428 queue
= &orcirc
->p_chan_cells
;
3429 circ_blocked
= circ
->circuit_blocked_on_p_chan
;
3430 max_queue_size
= max_circuit_cell_queue_size
;
3431 stream_list
= TO_OR_CIRCUIT(circ
)->n_streams
;
3434 if (PREDICT_UNLIKELY(queue
->n
>= max_queue_size
)) {
3435 /* This DoS defense only applies at the Guard as in the p_chan is likely
3436 * a client IP attacking the network. */
3437 if (exitward
&& CIRCUIT_IS_ORCIRC(circ
)) {
3438 stats_n_circ_max_cell_outq_reached
++;
3439 dos_note_circ_max_outq(CONST_TO_OR_CIRCUIT(circ
)->p_chan
);
3442 log_fn(LOG_PROTOCOL_WARN
, LD_PROTOCOL
,
3443 "%s circuit has %d cells in its queue, maximum allowed is %d. "
3444 "Closing circuit for safety reasons.",
3445 (exitward
) ? "Outbound" : "Inbound", queue
->n
,
3447 stats_n_circ_max_cell_reached
++;
3451 /* Very important that we copy to the circuit queue because all calls to
3452 * this function use the stack for the cell memory. */
3453 cell_queue_append_packed_copy(circ
, queue
, exitward
, cell
,
3454 chan
->wide_circ_ids
, 1);
3456 /* Check and run the OOM if needed. */
3457 if (PREDICT_UNLIKELY(cell_queues_check_size())) {
3458 /* We ran the OOM handler which might have closed this circuit. */
3459 if (circ
->marked_for_close
) {
3464 /* If we have too many cells on the circuit, note that it should
3465 * be blocked from new cells. */
3466 if (!circ_blocked
&& queue
->n
>= cell_queue_highwatermark())
3467 set_circuit_blocked_on_chan(circ
, chan
, 1);
3469 if (circ_blocked
&& fromstream
) {
3470 /* This edge connection is apparently not blocked; this can happen for
3471 * new streams on a blocked circuit, for their CONNECTED response.
3472 * block it now, unless we have conflux. */
3473 set_block_state_for_streams(circ
, stream_list
, 1, fromstream
);
3476 update_circuit_on_cmux(circ
, direction
);
3477 if (queue
->n
== 1) {
3478 /* This was the first cell added to the queue. We just made this
3479 * circuit active. */
3480 log_debug(LD_GENERAL
, "Made a circuit active.");
3483 /* New way: mark this as having waiting cells for the scheduler */
3484 scheduler_channel_has_waiting_cells(chan
);
3488 /** Append an encoded value of <b>addr</b> to <b>payload_out</b>, which must
3489 * have at least 18 bytes of free space. The encoding is, as specified in
3491 * RESOLVED_TYPE_IPV4 or RESOLVED_TYPE_IPV6 [1 byte]
3493 * ADDRESS [length bytes]
3494 * Return the number of bytes added, or -1 on error */
3496 append_address_to_payload(uint8_t *payload_out
, const tor_addr_t
*addr
)
3499 switch (tor_addr_family(addr
)) {
3501 payload_out
[0] = RESOLVED_TYPE_IPV4
;
3503 a
= tor_addr_to_ipv4n(addr
);
3504 memcpy(payload_out
+2, &a
, 4);
3507 payload_out
[0] = RESOLVED_TYPE_IPV6
;
3508 payload_out
[1] = 16;
3509 memcpy(payload_out
+2, tor_addr_to_in6_addr8(addr
), 16);
3517 /** Given <b>payload_len</b> bytes at <b>payload</b>, starting with an address
3518 * encoded as by append_address_to_payload(), try to decode the address into
3519 * *<b>addr_out</b>. Return the next byte in the payload after the address on
3520 * success, or NULL on failure. */
3522 decode_address_from_payload(tor_addr_t
*addr_out
, const uint8_t *payload
,
3525 if (payload_len
< 2)
3527 if (payload_len
< 2+payload
[1])
3530 switch (payload
[0]) {
3531 case RESOLVED_TYPE_IPV4
:
3532 if (payload
[1] != 4)
3534 tor_addr_from_ipv4n(addr_out
, get_uint32(payload
+2));
3536 case RESOLVED_TYPE_IPV6
:
3537 if (payload
[1] != 16)
3539 tor_addr_from_ipv6_bytes(addr_out
, (payload
+2));
3542 tor_addr_make_unspec(addr_out
);
3545 return payload
+ 2 + payload
[1];
3548 /** Remove all the cells queued on <b>circ</b> for <b>chan</b>. */
3550 circuit_clear_cell_queue(circuit_t
*circ
, channel_t
*chan
)
3552 cell_queue_t
*queue
;
3553 cell_direction_t direction
;
3555 if (circ
->n_chan
== chan
) {
3556 queue
= &circ
->n_chan_cells
;
3557 direction
= CELL_DIRECTION_OUT
;
3559 or_circuit_t
*orcirc
= TO_OR_CIRCUIT(circ
);
3560 tor_assert(orcirc
->p_chan
== chan
);
3561 queue
= &orcirc
->p_chan_cells
;
3562 direction
= CELL_DIRECTION_IN
;
3565 /* Clear the queue */
3566 cell_queue_clear(queue
);
3568 /* Update the cell counter in the cmux */
3569 if (chan
->cmux
&& circuitmux_is_circuit_attached(chan
->cmux
, circ
))
3570 update_circuit_on_cmux(circ
, direction
);
3573 /** Return 1 if we shouldn't restart reading on this circuit, even if
3574 * we get a SENDME. Else return 0.
3577 circuit_queue_streams_are_blocked(circuit_t
*circ
)
3579 if (CIRCUIT_IS_ORIGIN(circ
)) {
3580 return circ
->circuit_blocked_on_n_chan
;
3582 return circ
->circuit_blocked_on_p_chan
;