Add Coccinelle patch for replacing NULL/non-NULL tt_assert().
[tor.git] / src / or / relay.c
blob18ccc65b8076a95c572e6a4c5f3cea69850e677c
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-2017, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
7 /**
8 * \file relay.c
9 * \brief Handle relay cell encryption/decryption, plus packaging and
10 * receiving from circuits, plus queuing on circuits.
12 * This is a core modules that makes Tor work. It's responsible for
13 * dealing with RELAY cells (the ones that travel more than one hop along a
14 * circuit), by:
15 * <ul>
16 * <li>constructing relays cells,
17 * <li>encrypting relay cells,
18 * <li>decrypting relay cells,
19 * <li>demultiplexing relay cells as they arrive on a connection,
20 * <li>queueing relay cells for retransmission,
21 * <li>or handling relay cells that are for us to receive (as an exit or a
22 * client).
23 * </ul>
25 * RELAY cells are generated throughout the code at the client or relay side,
26 * using relay_send_command_from_edge() or one of the functions like
27 * connection_edge_send_command() that calls it. Of particular interest is
28 * connection_edge_package_raw_inbuf(), which takes information that has
29 * arrived on an edge connection socket, and packages it as a RELAY_DATA cell
30 * -- this is how information is actually sent across the Tor network. The
31 * cryptography for these functions is handled deep in
32 * circuit_package_relay_cell(), which either adds a single layer of
33 * encryption (if we're an exit), or multiple layers (if we're the origin of
34 * the circuit). After construction and encryption, the RELAY cells are
35 * passed to append_cell_to_circuit_queue(), which queues them for
36 * transmission and tells the circuitmux (see circuitmux.c) that the circuit
37 * is waiting to send something.
39 * Incoming RELAY cells arrive at circuit_receive_relay_cell(), called from
40 * command.c. There they are decrypted and, if they are for us, are passed to
41 * connection_edge_process_relay_cell(). If they're not for us, they're
42 * re-queued for retransmission again with append_cell_to_circuit_queue().
44 * The connection_edge_process_relay_cell() function handles all the different
45 * types of relay cells, launching requests or transmitting data as needed.
46 **/
48 #define RELAY_PRIVATE
49 #include "or.h"
50 #include "addressmap.h"
51 #include "buffers.h"
52 #include "channel.h"
53 #include "circpathbias.h"
54 #include "circuitbuild.h"
55 #include "circuitlist.h"
56 #include "circuituse.h"
57 #include "compress.h"
58 #include "config.h"
59 #include "connection.h"
60 #include "connection_edge.h"
61 #include "connection_or.h"
62 #include "control.h"
63 #include "geoip.h"
64 #include "hs_cache.h"
65 #include "main.h"
66 #include "networkstatus.h"
67 #include "nodelist.h"
68 #include "onion.h"
69 #include "policies.h"
70 #include "reasons.h"
71 #include "relay.h"
72 #include "rendcache.h"
73 #include "rendcommon.h"
74 #include "router.h"
75 #include "routerlist.h"
76 #include "routerparse.h"
77 #include "scheduler.h"
78 #include "rephist.h"
80 static edge_connection_t *relay_lookup_conn(circuit_t *circ, cell_t *cell,
81 cell_direction_t cell_direction,
82 crypt_path_t *layer_hint);
84 static int connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ,
85 edge_connection_t *conn,
86 crypt_path_t *layer_hint);
87 static void circuit_consider_sending_sendme(circuit_t *circ,
88 crypt_path_t *layer_hint);
89 static void circuit_resume_edge_reading(circuit_t *circ,
90 crypt_path_t *layer_hint);
91 static int circuit_resume_edge_reading_helper(edge_connection_t *conn,
92 circuit_t *circ,
93 crypt_path_t *layer_hint);
94 static int circuit_consider_stop_edge_reading(circuit_t *circ,
95 crypt_path_t *layer_hint);
96 static int circuit_queue_streams_are_blocked(circuit_t *circ);
97 static void adjust_exit_policy_from_exitpolicy_failure(origin_circuit_t *circ,
98 entry_connection_t *conn,
99 node_t *node,
100 const tor_addr_t *addr);
101 #if 0
102 static int get_max_middle_cells(void);
103 #endif
105 /** Stop reading on edge connections when we have this many cells
106 * waiting on the appropriate queue. */
107 #define CELL_QUEUE_HIGHWATER_SIZE 256
108 /** Start reading from edge connections again when we get down to this many
109 * cells. */
110 #define CELL_QUEUE_LOWWATER_SIZE 64
112 /** Stats: how many relay cells have originated at this hop, or have
113 * been relayed onward (not recognized at this hop)?
115 uint64_t stats_n_relay_cells_relayed = 0;
116 /** Stats: how many relay cells have been delivered to streams at this
117 * hop?
119 uint64_t stats_n_relay_cells_delivered = 0;
121 /** Used to tell which stream to read from first on a circuit. */
122 static tor_weak_rng_t stream_choice_rng = TOR_WEAK_RNG_INIT;
124 /** Update digest from the payload of cell. Assign integrity part to
125 * cell.
127 static void
128 relay_set_digest(crypto_digest_t *digest, cell_t *cell)
130 char integrity[4];
131 relay_header_t rh;
133 crypto_digest_add_bytes(digest, (char*)cell->payload, CELL_PAYLOAD_SIZE);
134 crypto_digest_get_digest(digest, integrity, 4);
135 // log_fn(LOG_DEBUG,"Putting digest of %u %u %u %u into relay cell.",
136 // integrity[0], integrity[1], integrity[2], integrity[3]);
137 relay_header_unpack(&rh, cell->payload);
138 memcpy(rh.integrity, integrity, 4);
139 relay_header_pack(cell->payload, &rh);
142 /** Does the digest for this circuit indicate that this cell is for us?
144 * Update digest from the payload of cell (with the integrity part set
145 * to 0). If the integrity part is valid, return 1, else restore digest
146 * and cell to their original state and return 0.
148 static int
149 relay_digest_matches(crypto_digest_t *digest, cell_t *cell)
151 uint32_t received_integrity, calculated_integrity;
152 relay_header_t rh;
153 crypto_digest_t *backup_digest=NULL;
155 backup_digest = crypto_digest_dup(digest);
157 relay_header_unpack(&rh, cell->payload);
158 memcpy(&received_integrity, rh.integrity, 4);
159 memset(rh.integrity, 0, 4);
160 relay_header_pack(cell->payload, &rh);
162 // log_fn(LOG_DEBUG,"Reading digest of %u %u %u %u from relay cell.",
163 // received_integrity[0], received_integrity[1],
164 // received_integrity[2], received_integrity[3]);
166 crypto_digest_add_bytes(digest, (char*) cell->payload, CELL_PAYLOAD_SIZE);
167 crypto_digest_get_digest(digest, (char*) &calculated_integrity, 4);
169 if (calculated_integrity != received_integrity) {
170 // log_fn(LOG_INFO,"Recognized=0 but bad digest. Not recognizing.");
171 // (%d vs %d).", received_integrity, calculated_integrity);
172 /* restore digest to its old form */
173 crypto_digest_assign(digest, backup_digest);
174 /* restore the relay header */
175 memcpy(rh.integrity, &received_integrity, 4);
176 relay_header_pack(cell->payload, &rh);
177 crypto_digest_free(backup_digest);
178 return 0;
180 crypto_digest_free(backup_digest);
181 return 1;
184 /** Apply <b>cipher</b> to CELL_PAYLOAD_SIZE bytes of <b>in</b>
185 * (in place).
187 * Note that we use the same operation for encrypting and for decrypting.
189 static void
190 relay_crypt_one_payload(crypto_cipher_t *cipher, uint8_t *in)
192 crypto_cipher_crypt_inplace(cipher, (char*) in, CELL_PAYLOAD_SIZE);
196 * Update channel usage state based on the type of relay cell and
197 * circuit properties.
199 * This is needed to determine if a client channel is being
200 * used for application traffic, and if a relay channel is being
201 * used for multihop circuits and application traffic. The decision
202 * to pad in channelpadding.c depends upon this info (as well as
203 * consensus parameters) to decide what channels to pad.
205 static void
206 circuit_update_channel_usage(circuit_t *circ, cell_t *cell)
208 if (CIRCUIT_IS_ORIGIN(circ)) {
210 * The client state was first set much earlier in
211 * circuit_send_next_onion_skin(), so we can start padding as early as
212 * possible.
214 * However, if padding turns out to be expensive, we may want to not do
215 * it until actual application traffic starts flowing (which is controlled
216 * via consensus param nf_pad_before_usage).
218 * So: If we're an origin circuit and we've created a full length circuit,
219 * then any CELL_RELAY cell means application data. Increase the usage
220 * state of the channel to indicate this.
222 * We want to wait for CELL_RELAY specifically here, so we know that
223 * the channel was definitely being used for data and not for extends.
224 * By default, we pad as soon as a channel has been used for *any*
225 * circuits, so this state is irrelevant to the padding decision in
226 * the default case. However, if padding turns out to be expensive,
227 * we would like the ability to avoid padding until we're absolutely
228 * sure that a channel is used for enough application data to be worth
229 * padding.
231 * (So it does not matter that CELL_RELAY_EARLY can actually contain
232 * application data. This is only a load reducing option and that edge
233 * case does not matter if we're desperately trying to reduce overhead
234 * anyway. See also consensus parameter nf_pad_before_usage).
236 if (BUG(!circ->n_chan))
237 return;
239 if (circ->n_chan->channel_usage == CHANNEL_USED_FOR_FULL_CIRCS &&
240 cell->command == CELL_RELAY) {
241 circ->n_chan->channel_usage = CHANNEL_USED_FOR_USER_TRAFFIC;
243 } else {
244 /* If we're a relay circuit, the question is more complicated. Basically:
245 * we only want to pad connections that carry multihop (anonymous)
246 * circuits.
248 * We assume we're more than one hop if either the previous hop
249 * is not a client, or if the previous hop is a client and there's
250 * a next hop. Then, circuit traffic starts at RELAY_EARLY, and
251 * user application traffic starts when we see RELAY cells.
253 or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
255 if (BUG(!or_circ->p_chan))
256 return;
258 if (!channel_is_client(or_circ->p_chan) ||
259 (channel_is_client(or_circ->p_chan) && circ->n_chan)) {
260 if (cell->command == CELL_RELAY_EARLY) {
261 if (or_circ->p_chan->channel_usage < CHANNEL_USED_FOR_FULL_CIRCS) {
262 or_circ->p_chan->channel_usage = CHANNEL_USED_FOR_FULL_CIRCS;
264 } else if (cell->command == CELL_RELAY) {
265 or_circ->p_chan->channel_usage = CHANNEL_USED_FOR_USER_TRAFFIC;
271 /** Receive a relay cell:
272 * - Crypt it (encrypt if headed toward the origin or if we <b>are</b> the
273 * origin; decrypt if we're headed toward the exit).
274 * - Check if recognized (if exitward).
275 * - If recognized and the digest checks out, then find if there's a stream
276 * that the cell is intended for, and deliver it to the right
277 * connection_edge.
278 * - If not recognized, then we need to relay it: append it to the appropriate
279 * cell_queue on <b>circ</b>.
281 * Return -<b>reason</b> on failure.
284 circuit_receive_relay_cell(cell_t *cell, circuit_t *circ,
285 cell_direction_t cell_direction)
287 channel_t *chan = NULL;
288 crypt_path_t *layer_hint=NULL;
289 char recognized=0;
290 int reason;
292 tor_assert(cell);
293 tor_assert(circ);
294 tor_assert(cell_direction == CELL_DIRECTION_OUT ||
295 cell_direction == CELL_DIRECTION_IN);
296 if (circ->marked_for_close)
297 return 0;
299 if (relay_crypt(circ, cell, cell_direction, &layer_hint, &recognized) < 0) {
300 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
301 "relay crypt failed. Dropping connection.");
302 return -END_CIRC_REASON_INTERNAL;
305 circuit_update_channel_usage(circ, cell);
307 if (recognized) {
308 edge_connection_t *conn = NULL;
310 if (circ->purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING) {
311 pathbias_check_probe_response(circ, cell);
313 /* We need to drop this cell no matter what to avoid code that expects
314 * a certain purpose (such as the hidserv code). */
315 return 0;
318 conn = relay_lookup_conn(circ, cell, cell_direction, layer_hint);
319 if (cell_direction == CELL_DIRECTION_OUT) {
320 ++stats_n_relay_cells_delivered;
321 log_debug(LD_OR,"Sending away from origin.");
322 if ((reason=connection_edge_process_relay_cell(cell, circ, conn, NULL))
323 < 0) {
324 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
325 "connection_edge_process_relay_cell (away from origin) "
326 "failed.");
327 return reason;
330 if (cell_direction == CELL_DIRECTION_IN) {
331 ++stats_n_relay_cells_delivered;
332 log_debug(LD_OR,"Sending to origin.");
333 if ((reason = connection_edge_process_relay_cell(cell, circ, conn,
334 layer_hint)) < 0) {
335 /* If a client is trying to connect to unknown hidden service port,
336 * END_CIRC_AT_ORIGIN is sent back so we can then close the circuit.
337 * Do not log warn as this is an expected behavior for a service. */
338 if (reason != END_CIRC_AT_ORIGIN) {
339 log_warn(LD_OR,
340 "connection_edge_process_relay_cell (at origin) failed.");
342 return reason;
345 return 0;
348 /* not recognized. pass it on. */
349 if (cell_direction == CELL_DIRECTION_OUT) {
350 cell->circ_id = circ->n_circ_id; /* switch it */
351 chan = circ->n_chan;
352 } else if (! CIRCUIT_IS_ORIGIN(circ)) {
353 cell->circ_id = TO_OR_CIRCUIT(circ)->p_circ_id; /* switch it */
354 chan = TO_OR_CIRCUIT(circ)->p_chan;
355 } else {
356 log_fn(LOG_PROTOCOL_WARN, LD_OR,
357 "Dropping unrecognized inbound cell on origin circuit.");
358 /* If we see unrecognized cells on path bias testing circs,
359 * it's bad mojo. Those circuits need to die.
360 * XXX: Shouldn't they always die? */
361 if (circ->purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING) {
362 TO_ORIGIN_CIRCUIT(circ)->path_state = PATH_STATE_USE_FAILED;
363 return -END_CIRC_REASON_TORPROTOCOL;
364 } else {
365 return 0;
369 if (!chan) {
370 // XXXX Can this splice stuff be done more cleanly?
371 if (! CIRCUIT_IS_ORIGIN(circ) &&
372 TO_OR_CIRCUIT(circ)->rend_splice &&
373 cell_direction == CELL_DIRECTION_OUT) {
374 or_circuit_t *splice_ = TO_OR_CIRCUIT(circ)->rend_splice;
375 tor_assert(circ->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED);
376 tor_assert(splice_->base_.purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED);
377 cell->circ_id = splice_->p_circ_id;
378 cell->command = CELL_RELAY; /* can't be relay_early anyway */
379 if ((reason = circuit_receive_relay_cell(cell, TO_CIRCUIT(splice_),
380 CELL_DIRECTION_IN)) < 0) {
381 log_warn(LD_REND, "Error relaying cell across rendezvous; closing "
382 "circuits");
383 /* XXXX Do this here, or just return -1? */
384 circuit_mark_for_close(circ, -reason);
385 return reason;
387 return 0;
389 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
390 "Didn't recognize cell, but circ stops here! Closing circ.");
391 return -END_CIRC_REASON_TORPROTOCOL;
394 log_debug(LD_OR,"Passing on unrecognized cell.");
396 ++stats_n_relay_cells_relayed; /* XXXX no longer quite accurate {cells}
397 * we might kill the circ before we relay
398 * the cells. */
400 append_cell_to_circuit_queue(circ, chan, cell, cell_direction, 0);
401 return 0;
404 /** Do the appropriate en/decryptions for <b>cell</b> arriving on
405 * <b>circ</b> in direction <b>cell_direction</b>.
407 * If cell_direction == CELL_DIRECTION_IN:
408 * - If we're at the origin (we're the OP), for hops 1..N,
409 * decrypt cell. If recognized, stop.
410 * - Else (we're not the OP), encrypt one hop. Cell is not recognized.
412 * If cell_direction == CELL_DIRECTION_OUT:
413 * - decrypt one hop. Check if recognized.
415 * If cell is recognized, set *recognized to 1, and set
416 * *layer_hint to the hop that recognized it.
418 * Return -1 to indicate that we should mark the circuit for close,
419 * else return 0.
422 relay_crypt(circuit_t *circ, cell_t *cell, cell_direction_t cell_direction,
423 crypt_path_t **layer_hint, char *recognized)
425 relay_header_t rh;
427 tor_assert(circ);
428 tor_assert(cell);
429 tor_assert(recognized);
430 tor_assert(cell_direction == CELL_DIRECTION_IN ||
431 cell_direction == CELL_DIRECTION_OUT);
433 if (cell_direction == CELL_DIRECTION_IN) {
434 if (CIRCUIT_IS_ORIGIN(circ)) { /* We're at the beginning of the circuit.
435 * We'll want to do layered decrypts. */
436 crypt_path_t *thishop, *cpath = TO_ORIGIN_CIRCUIT(circ)->cpath;
437 thishop = cpath;
438 if (thishop->state != CPATH_STATE_OPEN) {
439 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
440 "Relay cell before first created cell? Closing.");
441 return -1;
443 do { /* Remember: cpath is in forward order, that is, first hop first. */
444 tor_assert(thishop);
446 /* decrypt one layer */
447 relay_crypt_one_payload(thishop->b_crypto, cell->payload);
449 relay_header_unpack(&rh, cell->payload);
450 if (rh.recognized == 0) {
451 /* it's possibly recognized. have to check digest to be sure. */
452 if (relay_digest_matches(thishop->b_digest, cell)) {
453 *recognized = 1;
454 *layer_hint = thishop;
455 return 0;
459 thishop = thishop->next;
460 } while (thishop != cpath && thishop->state == CPATH_STATE_OPEN);
461 log_fn(LOG_PROTOCOL_WARN, LD_OR,
462 "Incoming cell at client not recognized. Closing.");
463 return -1;
464 } else {
465 /* We're in the middle. Encrypt one layer. */
466 relay_crypt_one_payload(TO_OR_CIRCUIT(circ)->p_crypto, cell->payload);
468 } else /* cell_direction == CELL_DIRECTION_OUT */ {
469 /* We're in the middle. Decrypt one layer. */
471 relay_crypt_one_payload(TO_OR_CIRCUIT(circ)->n_crypto, cell->payload);
473 relay_header_unpack(&rh, cell->payload);
474 if (rh.recognized == 0) {
475 /* it's possibly recognized. have to check digest to be sure. */
476 if (relay_digest_matches(TO_OR_CIRCUIT(circ)->n_digest, cell)) {
477 *recognized = 1;
478 return 0;
482 return 0;
485 /** Package a relay cell from an edge:
486 * - Encrypt it to the right layer
487 * - Append it to the appropriate cell_queue on <b>circ</b>.
489 static int
490 circuit_package_relay_cell(cell_t *cell, circuit_t *circ,
491 cell_direction_t cell_direction,
492 crypt_path_t *layer_hint, streamid_t on_stream,
493 const char *filename, int lineno)
495 channel_t *chan; /* where to send the cell */
497 if (cell_direction == CELL_DIRECTION_OUT) {
498 crypt_path_t *thishop; /* counter for repeated crypts */
499 chan = circ->n_chan;
500 if (!chan) {
501 log_warn(LD_BUG,"outgoing relay cell sent from %s:%d has n_chan==NULL."
502 " Dropping.", filename, lineno);
503 return 0; /* just drop it */
505 if (!CIRCUIT_IS_ORIGIN(circ)) {
506 log_warn(LD_BUG,"outgoing relay cell sent from %s:%d on non-origin "
507 "circ. Dropping.", filename, lineno);
508 return 0; /* just drop it */
511 relay_set_digest(layer_hint->f_digest, cell);
513 thishop = layer_hint;
514 /* moving from farthest to nearest hop */
515 do {
516 tor_assert(thishop);
517 log_debug(LD_OR,"encrypting a layer of the relay cell.");
518 relay_crypt_one_payload(thishop->f_crypto, cell->payload);
520 thishop = thishop->prev;
521 } while (thishop != TO_ORIGIN_CIRCUIT(circ)->cpath->prev);
523 } else { /* incoming cell */
524 or_circuit_t *or_circ;
525 if (CIRCUIT_IS_ORIGIN(circ)) {
526 /* We should never package an _incoming_ cell from the circuit
527 * origin; that means we messed up somewhere. */
528 log_warn(LD_BUG,"incoming relay cell at origin circuit. Dropping.");
529 assert_circuit_ok(circ);
530 return 0; /* just drop it */
532 or_circ = TO_OR_CIRCUIT(circ);
533 chan = or_circ->p_chan;
534 relay_set_digest(or_circ->p_digest, cell);
535 /* encrypt one layer */
536 relay_crypt_one_payload(or_circ->p_crypto, cell->payload);
538 ++stats_n_relay_cells_relayed;
540 append_cell_to_circuit_queue(circ, chan, cell, cell_direction, on_stream);
541 return 0;
544 /** If cell's stream_id matches the stream_id of any conn that's
545 * attached to circ, return that conn, else return NULL.
547 static edge_connection_t *
548 relay_lookup_conn(circuit_t *circ, cell_t *cell,
549 cell_direction_t cell_direction, crypt_path_t *layer_hint)
551 edge_connection_t *tmpconn;
552 relay_header_t rh;
554 relay_header_unpack(&rh, cell->payload);
556 if (!rh.stream_id)
557 return NULL;
559 /* IN or OUT cells could have come from either direction, now
560 * that we allow rendezvous *to* an OP.
563 if (CIRCUIT_IS_ORIGIN(circ)) {
564 for (tmpconn = TO_ORIGIN_CIRCUIT(circ)->p_streams; tmpconn;
565 tmpconn=tmpconn->next_stream) {
566 if (rh.stream_id == tmpconn->stream_id &&
567 !tmpconn->base_.marked_for_close &&
568 tmpconn->cpath_layer == layer_hint) {
569 log_debug(LD_APP,"found conn for stream %d.", rh.stream_id);
570 return tmpconn;
573 } else {
574 for (tmpconn = TO_OR_CIRCUIT(circ)->n_streams; tmpconn;
575 tmpconn=tmpconn->next_stream) {
576 if (rh.stream_id == tmpconn->stream_id &&
577 !tmpconn->base_.marked_for_close) {
578 log_debug(LD_EXIT,"found conn for stream %d.", rh.stream_id);
579 if (cell_direction == CELL_DIRECTION_OUT ||
580 connection_edge_is_rendezvous_stream(tmpconn))
581 return tmpconn;
584 for (tmpconn = TO_OR_CIRCUIT(circ)->resolving_streams; tmpconn;
585 tmpconn=tmpconn->next_stream) {
586 if (rh.stream_id == tmpconn->stream_id &&
587 !tmpconn->base_.marked_for_close) {
588 log_debug(LD_EXIT,"found conn for stream %d.", rh.stream_id);
589 return tmpconn;
593 return NULL; /* probably a begin relay cell */
596 /** Pack the relay_header_t host-order structure <b>src</b> into
597 * network-order in the buffer <b>dest</b>. See tor-spec.txt for details
598 * about the wire format.
600 void
601 relay_header_pack(uint8_t *dest, const relay_header_t *src)
603 set_uint8(dest, src->command);
604 set_uint16(dest+1, htons(src->recognized));
605 set_uint16(dest+3, htons(src->stream_id));
606 memcpy(dest+5, src->integrity, 4);
607 set_uint16(dest+9, htons(src->length));
610 /** Unpack the network-order buffer <b>src</b> into a host-order
611 * relay_header_t structure <b>dest</b>.
613 void
614 relay_header_unpack(relay_header_t *dest, const uint8_t *src)
616 dest->command = get_uint8(src);
617 dest->recognized = ntohs(get_uint16(src+1));
618 dest->stream_id = ntohs(get_uint16(src+3));
619 memcpy(dest->integrity, src+5, 4);
620 dest->length = ntohs(get_uint16(src+9));
623 /** Convert the relay <b>command</b> into a human-readable string. */
624 static const char *
625 relay_command_to_string(uint8_t command)
627 static char buf[64];
628 switch (command) {
629 case RELAY_COMMAND_BEGIN: return "BEGIN";
630 case RELAY_COMMAND_DATA: return "DATA";
631 case RELAY_COMMAND_END: return "END";
632 case RELAY_COMMAND_CONNECTED: return "CONNECTED";
633 case RELAY_COMMAND_SENDME: return "SENDME";
634 case RELAY_COMMAND_EXTEND: return "EXTEND";
635 case RELAY_COMMAND_EXTENDED: return "EXTENDED";
636 case RELAY_COMMAND_TRUNCATE: return "TRUNCATE";
637 case RELAY_COMMAND_TRUNCATED: return "TRUNCATED";
638 case RELAY_COMMAND_DROP: return "DROP";
639 case RELAY_COMMAND_RESOLVE: return "RESOLVE";
640 case RELAY_COMMAND_RESOLVED: return "RESOLVED";
641 case RELAY_COMMAND_BEGIN_DIR: return "BEGIN_DIR";
642 case RELAY_COMMAND_ESTABLISH_INTRO: return "ESTABLISH_INTRO";
643 case RELAY_COMMAND_ESTABLISH_RENDEZVOUS: return "ESTABLISH_RENDEZVOUS";
644 case RELAY_COMMAND_INTRODUCE1: return "INTRODUCE1";
645 case RELAY_COMMAND_INTRODUCE2: return "INTRODUCE2";
646 case RELAY_COMMAND_RENDEZVOUS1: return "RENDEZVOUS1";
647 case RELAY_COMMAND_RENDEZVOUS2: return "RENDEZVOUS2";
648 case RELAY_COMMAND_INTRO_ESTABLISHED: return "INTRO_ESTABLISHED";
649 case RELAY_COMMAND_RENDEZVOUS_ESTABLISHED:
650 return "RENDEZVOUS_ESTABLISHED";
651 case RELAY_COMMAND_INTRODUCE_ACK: return "INTRODUCE_ACK";
652 case RELAY_COMMAND_EXTEND2: return "EXTEND2";
653 case RELAY_COMMAND_EXTENDED2: return "EXTENDED2";
654 default:
655 tor_snprintf(buf, sizeof(buf), "Unrecognized relay command %u",
656 (unsigned)command);
657 return buf;
661 /** Make a relay cell out of <b>relay_command</b> and <b>payload</b>, and send
662 * it onto the open circuit <b>circ</b>. <b>stream_id</b> is the ID on
663 * <b>circ</b> for the stream that's sending the relay cell, or 0 if it's a
664 * control cell. <b>cpath_layer</b> is NULL for OR->OP cells, or the
665 * destination hop for OP->OR cells.
667 * If you can't send the cell, mark the circuit for close and return -1. Else
668 * return 0.
670 MOCK_IMPL(int,
671 relay_send_command_from_edge_,(streamid_t stream_id, circuit_t *circ,
672 uint8_t relay_command, const char *payload,
673 size_t payload_len, crypt_path_t *cpath_layer,
674 const char *filename, int lineno))
676 cell_t cell;
677 relay_header_t rh;
678 cell_direction_t cell_direction;
679 /* XXXX NM Split this function into a separate versions per circuit type? */
681 tor_assert(circ);
682 tor_assert(payload_len <= RELAY_PAYLOAD_SIZE);
684 memset(&cell, 0, sizeof(cell_t));
685 cell.command = CELL_RELAY;
686 if (CIRCUIT_IS_ORIGIN(circ)) {
687 tor_assert(cpath_layer);
688 cell.circ_id = circ->n_circ_id;
689 cell_direction = CELL_DIRECTION_OUT;
690 } else {
691 tor_assert(! cpath_layer);
692 cell.circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
693 cell_direction = CELL_DIRECTION_IN;
696 memset(&rh, 0, sizeof(rh));
697 rh.command = relay_command;
698 rh.stream_id = stream_id;
699 rh.length = payload_len;
700 relay_header_pack(cell.payload, &rh);
701 if (payload_len)
702 memcpy(cell.payload+RELAY_HEADER_SIZE, payload, payload_len);
704 log_debug(LD_OR,"delivering %d cell %s.", relay_command,
705 cell_direction == CELL_DIRECTION_OUT ? "forward" : "backward");
707 if (relay_command == RELAY_COMMAND_DROP)
708 rep_hist_padding_count_write(PADDING_TYPE_DROP);
710 /* If we are sending an END cell and this circuit is used for a tunneled
711 * directory request, advance its state. */
712 if (relay_command == RELAY_COMMAND_END && circ->dirreq_id)
713 geoip_change_dirreq_state(circ->dirreq_id, DIRREQ_TUNNELED,
714 DIRREQ_END_CELL_SENT);
716 if (cell_direction == CELL_DIRECTION_OUT && circ->n_chan) {
717 /* if we're using relaybandwidthrate, this conn wants priority */
718 channel_timestamp_client(circ->n_chan);
721 if (cell_direction == CELL_DIRECTION_OUT) {
722 origin_circuit_t *origin_circ = TO_ORIGIN_CIRCUIT(circ);
723 if (origin_circ->remaining_relay_early_cells > 0 &&
724 (relay_command == RELAY_COMMAND_EXTEND ||
725 relay_command == RELAY_COMMAND_EXTEND2 ||
726 cpath_layer != origin_circ->cpath)) {
727 /* If we've got any relay_early cells left and (we're sending
728 * an extend cell or we're not talking to the first hop), use
729 * one of them. Don't worry about the conn protocol version:
730 * append_cell_to_circuit_queue will fix it up. */
731 cell.command = CELL_RELAY_EARLY;
732 --origin_circ->remaining_relay_early_cells;
733 log_debug(LD_OR, "Sending a RELAY_EARLY cell; %d remaining.",
734 (int)origin_circ->remaining_relay_early_cells);
735 /* Memorize the command that is sent as RELAY_EARLY cell; helps debug
736 * task 878. */
737 origin_circ->relay_early_commands[
738 origin_circ->relay_early_cells_sent++] = relay_command;
739 } else if (relay_command == RELAY_COMMAND_EXTEND ||
740 relay_command == RELAY_COMMAND_EXTEND2) {
741 /* If no RELAY_EARLY cells can be sent over this circuit, log which
742 * commands have been sent as RELAY_EARLY cells before; helps debug
743 * task 878. */
744 smartlist_t *commands_list = smartlist_new();
745 int i = 0;
746 char *commands = NULL;
747 for (; i < origin_circ->relay_early_cells_sent; i++)
748 smartlist_add(commands_list, (char *)
749 relay_command_to_string(origin_circ->relay_early_commands[i]));
750 commands = smartlist_join_strings(commands_list, ",", 0, NULL);
751 log_warn(LD_BUG, "Uh-oh. We're sending a RELAY_COMMAND_EXTEND cell, "
752 "but we have run out of RELAY_EARLY cells on that circuit. "
753 "Commands sent before: %s", commands);
754 tor_free(commands);
755 smartlist_free(commands_list);
759 if (circuit_package_relay_cell(&cell, circ, cell_direction, cpath_layer,
760 stream_id, filename, lineno) < 0) {
761 log_warn(LD_BUG,"circuit_package_relay_cell failed. Closing.");
762 circuit_mark_for_close(circ, END_CIRC_REASON_INTERNAL);
763 return -1;
765 return 0;
768 /** Make a relay cell out of <b>relay_command</b> and <b>payload</b>, and
769 * send it onto the open circuit <b>circ</b>. <b>fromconn</b> is the stream
770 * that's sending the relay cell, or NULL if it's a control cell.
771 * <b>cpath_layer</b> is NULL for OR->OP cells, or the destination hop
772 * for OP->OR cells.
774 * If you can't send the cell, mark the circuit for close and
775 * return -1. Else return 0.
778 connection_edge_send_command(edge_connection_t *fromconn,
779 uint8_t relay_command, const char *payload,
780 size_t payload_len)
782 /* XXXX NM Split this function into a separate versions per circuit type? */
783 circuit_t *circ;
784 crypt_path_t *cpath_layer = fromconn->cpath_layer;
785 tor_assert(fromconn);
786 circ = fromconn->on_circuit;
788 if (fromconn->base_.marked_for_close) {
789 log_warn(LD_BUG,
790 "called on conn that's already marked for close at %s:%d.",
791 fromconn->base_.marked_for_close_file,
792 fromconn->base_.marked_for_close);
793 return 0;
796 if (!circ) {
797 if (fromconn->base_.type == CONN_TYPE_AP) {
798 log_info(LD_APP,"no circ. Closing conn.");
799 connection_mark_unattached_ap(EDGE_TO_ENTRY_CONN(fromconn),
800 END_STREAM_REASON_INTERNAL);
801 } else {
802 log_info(LD_EXIT,"no circ. Closing conn.");
803 fromconn->edge_has_sent_end = 1; /* no circ to send to */
804 fromconn->end_reason = END_STREAM_REASON_INTERNAL;
805 connection_mark_for_close(TO_CONN(fromconn));
807 return -1;
810 #ifdef MEASUREMENTS_21206
811 /* Keep track of the number of RELAY_DATA cells sent for directory
812 * connections. */
813 connection_t *linked_conn = TO_CONN(fromconn)->linked_conn;
815 if (linked_conn && linked_conn->type == CONN_TYPE_DIR) {
816 ++(TO_DIR_CONN(linked_conn)->data_cells_sent);
818 #endif
820 return relay_send_command_from_edge(fromconn->stream_id, circ,
821 relay_command, payload,
822 payload_len, cpath_layer);
825 /** How many times will I retry a stream that fails due to DNS
826 * resolve failure or misc error?
828 #define MAX_RESOLVE_FAILURES 3
830 /** Return 1 if reason is something that you should retry if you
831 * get the end cell before you've connected; else return 0. */
832 static int
833 edge_reason_is_retriable(int reason)
835 return reason == END_STREAM_REASON_HIBERNATING ||
836 reason == END_STREAM_REASON_RESOURCELIMIT ||
837 reason == END_STREAM_REASON_EXITPOLICY ||
838 reason == END_STREAM_REASON_RESOLVEFAILED ||
839 reason == END_STREAM_REASON_MISC ||
840 reason == END_STREAM_REASON_NOROUTE;
843 /** Called when we receive an END cell on a stream that isn't open yet,
844 * from the client side.
845 * Arguments are as for connection_edge_process_relay_cell().
847 static int
848 connection_ap_process_end_not_open(
849 relay_header_t *rh, cell_t *cell, origin_circuit_t *circ,
850 entry_connection_t *conn, crypt_path_t *layer_hint)
852 node_t *exitrouter;
853 int reason = *(cell->payload+RELAY_HEADER_SIZE);
854 int control_reason;
855 edge_connection_t *edge_conn = ENTRY_TO_EDGE_CONN(conn);
856 (void) layer_hint; /* unused */
858 if (rh->length > 0) {
859 if (reason == END_STREAM_REASON_TORPROTOCOL ||
860 reason == END_STREAM_REASON_DESTROY) {
861 /* Both of these reasons could mean a failed tag
862 * hit the exit and it complained. Do not probe.
863 * Fail the circuit. */
864 circ->path_state = PATH_STATE_USE_FAILED;
865 return -END_CIRC_REASON_TORPROTOCOL;
866 } else if (reason == END_STREAM_REASON_INTERNAL) {
867 /* We can't infer success or failure, since older Tors report
868 * ENETUNREACH as END_STREAM_REASON_INTERNAL. */
869 } else {
870 /* Path bias: If we get a valid reason code from the exit,
871 * it wasn't due to tagging.
873 * We rely on recognized+digest being strong enough to make
874 * tags unlikely to allow us to get tagged, yet 'recognized'
875 * reason codes here. */
876 pathbias_mark_use_success(circ);
880 if (rh->length == 0) {
881 reason = END_STREAM_REASON_MISC;
884 control_reason = reason | END_STREAM_REASON_FLAG_REMOTE;
886 if (edge_reason_is_retriable(reason) &&
887 /* avoid retry if rend */
888 !connection_edge_is_rendezvous_stream(edge_conn)) {
889 const char *chosen_exit_digest =
890 circ->build_state->chosen_exit->identity_digest;
891 log_info(LD_APP,"Address '%s' refused due to '%s'. Considering retrying.",
892 safe_str(conn->socks_request->address),
893 stream_end_reason_to_string(reason));
894 exitrouter = node_get_mutable_by_id(chosen_exit_digest);
895 switch (reason) {
896 case END_STREAM_REASON_EXITPOLICY: {
897 tor_addr_t addr;
898 tor_addr_make_unspec(&addr);
899 if (rh->length >= 5) {
900 int ttl = -1;
901 tor_addr_make_unspec(&addr);
902 if (rh->length == 5 || rh->length == 9) {
903 tor_addr_from_ipv4n(&addr,
904 get_uint32(cell->payload+RELAY_HEADER_SIZE+1));
905 if (rh->length == 9)
906 ttl = (int)ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+5));
907 } else if (rh->length == 17 || rh->length == 21) {
908 tor_addr_from_ipv6_bytes(&addr,
909 (char*)(cell->payload+RELAY_HEADER_SIZE+1));
910 if (rh->length == 21)
911 ttl = (int)ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+17));
913 if (tor_addr_is_null(&addr)) {
914 log_info(LD_APP,"Address '%s' resolved to 0.0.0.0. Closing,",
915 safe_str(conn->socks_request->address));
916 connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
917 return 0;
920 if ((tor_addr_family(&addr) == AF_INET &&
921 !conn->entry_cfg.ipv4_traffic) ||
922 (tor_addr_family(&addr) == AF_INET6 &&
923 !conn->entry_cfg.ipv6_traffic)) {
924 log_fn(LOG_PROTOCOL_WARN, LD_APP,
925 "Got an EXITPOLICY failure on a connection with a "
926 "mismatched family. Closing.");
927 connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
928 return 0;
930 if (get_options()->ClientDNSRejectInternalAddresses &&
931 tor_addr_is_internal(&addr, 0)) {
932 log_info(LD_APP,"Address '%s' resolved to internal. Closing,",
933 safe_str(conn->socks_request->address));
934 connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
935 return 0;
938 client_dns_set_addressmap(conn,
939 conn->socks_request->address, &addr,
940 conn->chosen_exit_name, ttl);
943 char new_addr[TOR_ADDR_BUF_LEN];
944 tor_addr_to_str(new_addr, &addr, sizeof(new_addr), 1);
945 if (strcmp(conn->socks_request->address, new_addr)) {
946 strlcpy(conn->socks_request->address, new_addr,
947 sizeof(conn->socks_request->address));
948 control_event_stream_status(conn, STREAM_EVENT_REMAP, 0);
952 /* check if the exit *ought* to have allowed it */
954 adjust_exit_policy_from_exitpolicy_failure(circ,
955 conn,
956 exitrouter,
957 &addr);
959 if (conn->chosen_exit_optional ||
960 conn->chosen_exit_retries) {
961 /* stop wanting a specific exit */
962 conn->chosen_exit_optional = 0;
963 /* A non-zero chosen_exit_retries can happen if we set a
964 * TrackHostExits for this address under a port that the exit
965 * relay allows, but then try the same address with a different
966 * port that it doesn't allow to exit. We shouldn't unregister
967 * the mapping, since it is probably still wanted on the
968 * original port. But now we give away to the exit relay that
969 * we probably have a TrackHostExits on it. So be it. */
970 conn->chosen_exit_retries = 0;
971 tor_free(conn->chosen_exit_name); /* clears it */
973 if (connection_ap_detach_retriable(conn, circ, control_reason) >= 0)
974 return 0;
975 /* else, conn will get closed below */
976 break;
978 case END_STREAM_REASON_CONNECTREFUSED:
979 if (!conn->chosen_exit_optional)
980 break; /* break means it'll close, below */
981 /* Else fall through: expire this circuit, clear the
982 * chosen_exit_name field, and try again. */
983 /* Falls through. */
984 case END_STREAM_REASON_RESOLVEFAILED:
985 case END_STREAM_REASON_TIMEOUT:
986 case END_STREAM_REASON_MISC:
987 case END_STREAM_REASON_NOROUTE:
988 if (client_dns_incr_failures(conn->socks_request->address)
989 < MAX_RESOLVE_FAILURES) {
990 /* We haven't retried too many times; reattach the connection. */
991 circuit_log_path(LOG_INFO,LD_APP,circ);
992 /* Mark this circuit "unusable for new streams". */
993 mark_circuit_unusable_for_new_conns(circ);
995 if (conn->chosen_exit_optional) {
996 /* stop wanting a specific exit */
997 conn->chosen_exit_optional = 0;
998 tor_free(conn->chosen_exit_name); /* clears it */
1000 if (connection_ap_detach_retriable(conn, circ, control_reason) >= 0)
1001 return 0;
1002 /* else, conn will get closed below */
1003 } else {
1004 log_notice(LD_APP,
1005 "Have tried resolving or connecting to address '%s' "
1006 "at %d different places. Giving up.",
1007 safe_str(conn->socks_request->address),
1008 MAX_RESOLVE_FAILURES);
1009 /* clear the failures, so it will have a full try next time */
1010 client_dns_clear_failures(conn->socks_request->address);
1012 break;
1013 case END_STREAM_REASON_HIBERNATING:
1014 case END_STREAM_REASON_RESOURCELIMIT:
1015 if (exitrouter) {
1016 policies_set_node_exitpolicy_to_reject_all(exitrouter);
1018 if (conn->chosen_exit_optional) {
1019 /* stop wanting a specific exit */
1020 conn->chosen_exit_optional = 0;
1021 tor_free(conn->chosen_exit_name); /* clears it */
1023 if (connection_ap_detach_retriable(conn, circ, control_reason) >= 0)
1024 return 0;
1025 /* else, will close below */
1026 break;
1027 } /* end switch */
1028 log_info(LD_APP,"Giving up on retrying; conn can't be handled.");
1031 log_info(LD_APP,
1032 "Edge got end (%s) before we're connected. Marking for close.",
1033 stream_end_reason_to_string(rh->length > 0 ? reason : -1));
1034 circuit_log_path(LOG_INFO,LD_APP,circ);
1035 /* need to test because of detach_retriable */
1036 if (!ENTRY_TO_CONN(conn)->marked_for_close)
1037 connection_mark_unattached_ap(conn, control_reason);
1038 return 0;
1041 /** Called when we have gotten an END_REASON_EXITPOLICY failure on <b>circ</b>
1042 * for <b>conn</b>, while attempting to connect via <b>node</b>. If the node
1043 * told us which address it rejected, then <b>addr</b> is that address;
1044 * otherwise it is AF_UNSPEC.
1046 * If we are sure the node should have allowed this address, mark the node as
1047 * having a reject *:* exit policy. Otherwise, mark the circuit as unusable
1048 * for this particular address.
1050 static void
1051 adjust_exit_policy_from_exitpolicy_failure(origin_circuit_t *circ,
1052 entry_connection_t *conn,
1053 node_t *node,
1054 const tor_addr_t *addr)
1056 int make_reject_all = 0;
1057 const sa_family_t family = tor_addr_family(addr);
1059 if (node) {
1060 tor_addr_t tmp;
1061 int asked_for_family = tor_addr_parse(&tmp, conn->socks_request->address);
1062 if (family == AF_UNSPEC) {
1063 make_reject_all = 1;
1064 } else if (node_exit_policy_is_exact(node, family) &&
1065 asked_for_family != -1 && !conn->chosen_exit_name) {
1066 make_reject_all = 1;
1069 if (make_reject_all) {
1070 log_info(LD_APP,
1071 "Exitrouter %s seems to be more restrictive than its exit "
1072 "policy. Not using this router as exit for now.",
1073 node_describe(node));
1074 policies_set_node_exitpolicy_to_reject_all(node);
1078 if (family != AF_UNSPEC)
1079 addr_policy_append_reject_addr(&circ->prepend_policy, addr);
1082 /** Helper: change the socks_request-&gt;address field on conn to the
1083 * dotted-quad representation of <b>new_addr</b>,
1084 * and send an appropriate REMAP event. */
1085 static void
1086 remap_event_helper(entry_connection_t *conn, const tor_addr_t *new_addr)
1088 tor_addr_to_str(conn->socks_request->address, new_addr,
1089 sizeof(conn->socks_request->address),
1091 control_event_stream_status(conn, STREAM_EVENT_REMAP,
1092 REMAP_STREAM_SOURCE_EXIT);
1095 /** Extract the contents of a connected cell in <b>cell</b>, whose relay
1096 * header has already been parsed into <b>rh</b>. On success, set
1097 * <b>addr_out</b> to the address we're connected to, and <b>ttl_out</b> to
1098 * the ttl of that address, in seconds, and return 0. On failure, return
1099 * -1. */
1100 STATIC int
1101 connected_cell_parse(const relay_header_t *rh, const cell_t *cell,
1102 tor_addr_t *addr_out, int *ttl_out)
1104 uint32_t bytes;
1105 const uint8_t *payload = cell->payload + RELAY_HEADER_SIZE;
1107 tor_addr_make_unspec(addr_out);
1108 *ttl_out = -1;
1109 if (rh->length == 0)
1110 return 0;
1111 if (rh->length < 4)
1112 return -1;
1113 bytes = ntohl(get_uint32(payload));
1115 /* If bytes is 0, this is maybe a v6 address. Otherwise it's a v4 address */
1116 if (bytes != 0) {
1117 /* v4 address */
1118 tor_addr_from_ipv4h(addr_out, bytes);
1119 if (rh->length >= 8) {
1120 bytes = ntohl(get_uint32(payload + 4));
1121 if (bytes <= INT32_MAX)
1122 *ttl_out = bytes;
1124 } else {
1125 if (rh->length < 25) /* 4 bytes of 0s, 1 addr, 16 ipv4, 4 ttl. */
1126 return -1;
1127 if (get_uint8(payload + 4) != 6)
1128 return -1;
1129 tor_addr_from_ipv6_bytes(addr_out, (char*)(payload + 5));
1130 bytes = ntohl(get_uint32(payload + 21));
1131 if (bytes <= INT32_MAX)
1132 *ttl_out = (int) bytes;
1134 return 0;
1137 /** Drop all storage held by <b>addr</b>. */
1138 STATIC void
1139 address_ttl_free(address_ttl_t *addr)
1141 if (!addr)
1142 return;
1143 tor_free(addr->hostname);
1144 tor_free(addr);
1147 /** Parse a resolved cell in <b>cell</b>, with parsed header in <b>rh</b>.
1148 * Return -1 on parse error. On success, add one or more newly allocated
1149 * address_ttl_t to <b>addresses_out</b>; set *<b>errcode_out</b> to
1150 * one of 0, RESOLVED_TYPE_ERROR, or RESOLVED_TYPE_ERROR_TRANSIENT, and
1151 * return 0. */
1152 STATIC int
1153 resolved_cell_parse(const cell_t *cell, const relay_header_t *rh,
1154 smartlist_t *addresses_out, int *errcode_out)
1156 const uint8_t *cp;
1157 uint8_t answer_type;
1158 size_t answer_len;
1159 address_ttl_t *addr;
1160 size_t remaining;
1161 int errcode = 0;
1162 smartlist_t *addrs;
1164 tor_assert(cell);
1165 tor_assert(rh);
1166 tor_assert(addresses_out);
1167 tor_assert(errcode_out);
1169 *errcode_out = 0;
1171 if (rh->length > RELAY_PAYLOAD_SIZE)
1172 return -1;
1174 addrs = smartlist_new();
1176 cp = cell->payload + RELAY_HEADER_SIZE;
1178 remaining = rh->length;
1179 while (remaining) {
1180 const uint8_t *cp_orig = cp;
1181 if (remaining < 2)
1182 goto err;
1183 answer_type = *cp++;
1184 answer_len = *cp++;
1185 if (remaining < 2 + answer_len + 4) {
1186 goto err;
1188 if (answer_type == RESOLVED_TYPE_IPV4) {
1189 if (answer_len != 4) {
1190 goto err;
1192 addr = tor_malloc_zero(sizeof(*addr));
1193 tor_addr_from_ipv4n(&addr->addr, get_uint32(cp));
1194 cp += 4;
1195 addr->ttl = ntohl(get_uint32(cp));
1196 cp += 4;
1197 smartlist_add(addrs, addr);
1198 } else if (answer_type == RESOLVED_TYPE_IPV6) {
1199 if (answer_len != 16)
1200 goto err;
1201 addr = tor_malloc_zero(sizeof(*addr));
1202 tor_addr_from_ipv6_bytes(&addr->addr, (const char*) cp);
1203 cp += 16;
1204 addr->ttl = ntohl(get_uint32(cp));
1205 cp += 4;
1206 smartlist_add(addrs, addr);
1207 } else if (answer_type == RESOLVED_TYPE_HOSTNAME) {
1208 if (answer_len == 0) {
1209 goto err;
1211 addr = tor_malloc_zero(sizeof(*addr));
1212 addr->hostname = tor_memdup_nulterm(cp, answer_len);
1213 cp += answer_len;
1214 addr->ttl = ntohl(get_uint32(cp));
1215 cp += 4;
1216 smartlist_add(addrs, addr);
1217 } else if (answer_type == RESOLVED_TYPE_ERROR_TRANSIENT ||
1218 answer_type == RESOLVED_TYPE_ERROR) {
1219 errcode = answer_type;
1220 /* Ignore the error contents */
1221 cp += answer_len + 4;
1222 } else {
1223 cp += answer_len + 4;
1225 tor_assert(((ssize_t)remaining) >= (cp - cp_orig));
1226 remaining -= (cp - cp_orig);
1229 if (errcode && smartlist_len(addrs) == 0) {
1230 /* Report an error only if there were no results. */
1231 *errcode_out = errcode;
1234 smartlist_add_all(addresses_out, addrs);
1235 smartlist_free(addrs);
1237 return 0;
1239 err:
1240 /* On parse error, don't report any results */
1241 SMARTLIST_FOREACH(addrs, address_ttl_t *, a, address_ttl_free(a));
1242 smartlist_free(addrs);
1243 return -1;
1246 /** Helper for connection_edge_process_resolved_cell: given an error code,
1247 * an entry_connection, and a list of address_ttl_t *, report the best answer
1248 * to the entry_connection. */
1249 static void
1250 connection_ap_handshake_socks_got_resolved_cell(entry_connection_t *conn,
1251 int error_code,
1252 smartlist_t *results)
1254 address_ttl_t *addr_ipv4 = NULL;
1255 address_ttl_t *addr_ipv6 = NULL;
1256 address_ttl_t *addr_hostname = NULL;
1257 address_ttl_t *addr_best = NULL;
1259 /* If it's an error code, that's easy. */
1260 if (error_code) {
1261 tor_assert(error_code == RESOLVED_TYPE_ERROR ||
1262 error_code == RESOLVED_TYPE_ERROR_TRANSIENT);
1263 connection_ap_handshake_socks_resolved(conn,
1264 error_code,0,NULL,-1,-1);
1265 return;
1268 /* Get the first answer of each type. */
1269 SMARTLIST_FOREACH_BEGIN(results, address_ttl_t *, addr) {
1270 if (addr->hostname) {
1271 if (!addr_hostname) {
1272 addr_hostname = addr;
1274 } else if (tor_addr_family(&addr->addr) == AF_INET) {
1275 if (!addr_ipv4 && conn->entry_cfg.ipv4_traffic) {
1276 addr_ipv4 = addr;
1278 } else if (tor_addr_family(&addr->addr) == AF_INET6) {
1279 if (!addr_ipv6 && conn->entry_cfg.ipv6_traffic) {
1280 addr_ipv6 = addr;
1283 } SMARTLIST_FOREACH_END(addr);
1285 /* Now figure out which type we wanted to deliver. */
1286 if (conn->socks_request->command == SOCKS_COMMAND_RESOLVE_PTR) {
1287 if (addr_hostname) {
1288 connection_ap_handshake_socks_resolved(conn,
1289 RESOLVED_TYPE_HOSTNAME,
1290 strlen(addr_hostname->hostname),
1291 (uint8_t*)addr_hostname->hostname,
1292 addr_hostname->ttl,-1);
1293 } else {
1294 connection_ap_handshake_socks_resolved(conn,
1295 RESOLVED_TYPE_ERROR,0,NULL,-1,-1);
1297 return;
1300 if (conn->entry_cfg.prefer_ipv6) {
1301 addr_best = addr_ipv6 ? addr_ipv6 : addr_ipv4;
1302 } else {
1303 addr_best = addr_ipv4 ? addr_ipv4 : addr_ipv6;
1306 /* Now convert it to the ugly old interface */
1307 if (! addr_best) {
1308 connection_ap_handshake_socks_resolved(conn,
1309 RESOLVED_TYPE_ERROR,0,NULL,-1,-1);
1310 return;
1313 connection_ap_handshake_socks_resolved_addr(conn,
1314 &addr_best->addr,
1315 addr_best->ttl,
1316 -1);
1318 remap_event_helper(conn, &addr_best->addr);
1321 /** Handle a RELAY_COMMAND_RESOLVED cell that we received on a non-open AP
1322 * stream. */
1323 STATIC int
1324 connection_edge_process_resolved_cell(edge_connection_t *conn,
1325 const cell_t *cell,
1326 const relay_header_t *rh)
1328 entry_connection_t *entry_conn = EDGE_TO_ENTRY_CONN(conn);
1329 smartlist_t *resolved_addresses = NULL;
1330 int errcode = 0;
1332 if (conn->base_.state != AP_CONN_STATE_RESOLVE_WAIT) {
1333 log_fn(LOG_PROTOCOL_WARN, LD_APP, "Got a 'resolved' cell while "
1334 "not in state resolve_wait. Dropping.");
1335 return 0;
1337 tor_assert(SOCKS_COMMAND_IS_RESOLVE(entry_conn->socks_request->command));
1339 resolved_addresses = smartlist_new();
1340 if (resolved_cell_parse(cell, rh, resolved_addresses, &errcode)) {
1341 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1342 "Dropping malformed 'resolved' cell");
1343 connection_mark_unattached_ap(entry_conn, END_STREAM_REASON_TORPROTOCOL);
1344 goto done;
1347 if (get_options()->ClientDNSRejectInternalAddresses) {
1348 int orig_len = smartlist_len(resolved_addresses);
1349 SMARTLIST_FOREACH_BEGIN(resolved_addresses, address_ttl_t *, addr) {
1350 if (addr->hostname == NULL && tor_addr_is_internal(&addr->addr, 0)) {
1351 log_info(LD_APP, "Got a resolved cell with answer %s; dropping that "
1352 "answer.",
1353 safe_str_client(fmt_addr(&addr->addr)));
1354 address_ttl_free(addr);
1355 SMARTLIST_DEL_CURRENT(resolved_addresses, addr);
1357 } SMARTLIST_FOREACH_END(addr);
1358 if (orig_len && smartlist_len(resolved_addresses) == 0) {
1359 log_info(LD_APP, "Got a resolved cell with only private addresses; "
1360 "dropping it.");
1361 connection_ap_handshake_socks_resolved(entry_conn,
1362 RESOLVED_TYPE_ERROR_TRANSIENT,
1363 0, NULL, 0, TIME_MAX);
1364 connection_mark_unattached_ap(entry_conn,
1365 END_STREAM_REASON_TORPROTOCOL);
1366 goto done;
1370 connection_ap_handshake_socks_got_resolved_cell(entry_conn,
1371 errcode,
1372 resolved_addresses);
1374 connection_mark_unattached_ap(entry_conn,
1375 END_STREAM_REASON_DONE |
1376 END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED);
1378 done:
1379 SMARTLIST_FOREACH(resolved_addresses, address_ttl_t *, addr,
1380 address_ttl_free(addr));
1381 smartlist_free(resolved_addresses);
1382 return 0;
1385 /** An incoming relay cell has arrived from circuit <b>circ</b> to
1386 * stream <b>conn</b>.
1388 * The arguments here are the same as in
1389 * connection_edge_process_relay_cell() below; this function is called
1390 * from there when <b>conn</b> is defined and not in an open state.
1392 static int
1393 connection_edge_process_relay_cell_not_open(
1394 relay_header_t *rh, cell_t *cell, circuit_t *circ,
1395 edge_connection_t *conn, crypt_path_t *layer_hint)
1397 if (rh->command == RELAY_COMMAND_END) {
1398 if (CIRCUIT_IS_ORIGIN(circ) && conn->base_.type == CONN_TYPE_AP) {
1399 return connection_ap_process_end_not_open(rh, cell,
1400 TO_ORIGIN_CIRCUIT(circ),
1401 EDGE_TO_ENTRY_CONN(conn),
1402 layer_hint);
1403 } else {
1404 /* we just got an 'end', don't need to send one */
1405 conn->edge_has_sent_end = 1;
1406 conn->end_reason = *(cell->payload+RELAY_HEADER_SIZE) |
1407 END_STREAM_REASON_FLAG_REMOTE;
1408 connection_mark_for_close(TO_CONN(conn));
1409 return 0;
1413 if (conn->base_.type == CONN_TYPE_AP &&
1414 rh->command == RELAY_COMMAND_CONNECTED) {
1415 tor_addr_t addr;
1416 int ttl;
1417 entry_connection_t *entry_conn = EDGE_TO_ENTRY_CONN(conn);
1418 tor_assert(CIRCUIT_IS_ORIGIN(circ));
1419 if (conn->base_.state != AP_CONN_STATE_CONNECT_WAIT) {
1420 log_fn(LOG_PROTOCOL_WARN, LD_APP,
1421 "Got 'connected' while not in state connect_wait. Dropping.");
1422 return 0;
1424 CONNECTION_AP_EXPECT_NONPENDING(entry_conn);
1425 conn->base_.state = AP_CONN_STATE_OPEN;
1426 log_info(LD_APP,"'connected' received for circid %u streamid %d "
1427 "after %d seconds.",
1428 (unsigned)circ->n_circ_id,
1429 rh->stream_id,
1430 (int)(time(NULL) - conn->base_.timestamp_lastread));
1431 if (connected_cell_parse(rh, cell, &addr, &ttl) < 0) {
1432 log_fn(LOG_PROTOCOL_WARN, LD_APP,
1433 "Got a badly formatted connected cell. Closing.");
1434 connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL);
1435 connection_mark_unattached_ap(entry_conn, END_STREAM_REASON_TORPROTOCOL);
1437 if (tor_addr_family(&addr) != AF_UNSPEC) {
1438 const sa_family_t family = tor_addr_family(&addr);
1439 if (tor_addr_is_null(&addr) ||
1440 (get_options()->ClientDNSRejectInternalAddresses &&
1441 tor_addr_is_internal(&addr, 0))) {
1442 log_info(LD_APP, "...but it claims the IP address was %s. Closing.",
1443 fmt_addr(&addr));
1444 connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL);
1445 connection_mark_unattached_ap(entry_conn,
1446 END_STREAM_REASON_TORPROTOCOL);
1447 return 0;
1450 if ((family == AF_INET && ! entry_conn->entry_cfg.ipv4_traffic) ||
1451 (family == AF_INET6 && ! entry_conn->entry_cfg.ipv6_traffic)) {
1452 log_fn(LOG_PROTOCOL_WARN, LD_APP,
1453 "Got a connected cell to %s with unsupported address family."
1454 " Closing.", fmt_addr(&addr));
1455 connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL);
1456 connection_mark_unattached_ap(entry_conn,
1457 END_STREAM_REASON_TORPROTOCOL);
1458 return 0;
1461 client_dns_set_addressmap(entry_conn,
1462 entry_conn->socks_request->address, &addr,
1463 entry_conn->chosen_exit_name, ttl);
1465 remap_event_helper(entry_conn, &addr);
1467 circuit_log_path(LOG_INFO,LD_APP,TO_ORIGIN_CIRCUIT(circ));
1468 /* don't send a socks reply to transparent conns */
1469 tor_assert(entry_conn->socks_request != NULL);
1470 if (!entry_conn->socks_request->has_finished)
1471 connection_ap_handshake_socks_reply(entry_conn, NULL, 0, 0);
1473 /* Was it a linked dir conn? If so, a dir request just started to
1474 * fetch something; this could be a bootstrap status milestone. */
1475 log_debug(LD_APP, "considering");
1476 if (TO_CONN(conn)->linked_conn &&
1477 TO_CONN(conn)->linked_conn->type == CONN_TYPE_DIR) {
1478 connection_t *dirconn = TO_CONN(conn)->linked_conn;
1479 log_debug(LD_APP, "it is! %d", dirconn->purpose);
1480 switch (dirconn->purpose) {
1481 case DIR_PURPOSE_FETCH_CERTIFICATE:
1482 if (consensus_is_waiting_for_certs())
1483 control_event_bootstrap(BOOTSTRAP_STATUS_LOADING_KEYS, 0);
1484 break;
1485 case DIR_PURPOSE_FETCH_CONSENSUS:
1486 control_event_bootstrap(BOOTSTRAP_STATUS_LOADING_STATUS, 0);
1487 break;
1488 case DIR_PURPOSE_FETCH_SERVERDESC:
1489 case DIR_PURPOSE_FETCH_MICRODESC:
1490 if (TO_DIR_CONN(dirconn)->router_purpose == ROUTER_PURPOSE_GENERAL)
1491 control_event_bootstrap(BOOTSTRAP_STATUS_LOADING_DESCRIPTORS,
1492 count_loading_descriptors_progress());
1493 break;
1496 /* This is definitely a success, so forget about any pending data we
1497 * had sent. */
1498 if (entry_conn->pending_optimistic_data) {
1499 buf_free(entry_conn->pending_optimistic_data);
1500 entry_conn->pending_optimistic_data = NULL;
1503 /* handle anything that might have queued */
1504 if (connection_edge_package_raw_inbuf(conn, 1, NULL) < 0) {
1505 /* (We already sent an end cell if possible) */
1506 connection_mark_for_close(TO_CONN(conn));
1507 return 0;
1509 return 0;
1511 if (conn->base_.type == CONN_TYPE_AP &&
1512 rh->command == RELAY_COMMAND_RESOLVED) {
1513 return connection_edge_process_resolved_cell(conn, cell, rh);
1516 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1517 "Got an unexpected relay command %d, in state %d (%s). Dropping.",
1518 rh->command, conn->base_.state,
1519 conn_state_to_string(conn->base_.type, conn->base_.state));
1520 return 0; /* for forward compatibility, don't kill the circuit */
1521 // connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL);
1522 // connection_mark_for_close(conn);
1523 // return -1;
1526 /** An incoming relay cell has arrived on circuit <b>circ</b>. If
1527 * <b>conn</b> is NULL this is a control cell, else <b>cell</b> is
1528 * destined for <b>conn</b>.
1530 * If <b>layer_hint</b> is defined, then we're the origin of the
1531 * circuit, and it specifies the hop that packaged <b>cell</b>.
1533 * Return -reason if you want to warn and tear down the circuit, else 0.
1535 static int
1536 connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ,
1537 edge_connection_t *conn,
1538 crypt_path_t *layer_hint)
1540 static int num_seen=0;
1541 relay_header_t rh;
1542 unsigned domain = layer_hint?LD_APP:LD_EXIT;
1543 int reason;
1544 int optimistic_data = 0; /* Set to 1 if we receive data on a stream
1545 * that's in the EXIT_CONN_STATE_RESOLVING
1546 * or EXIT_CONN_STATE_CONNECTING states. */
1548 tor_assert(cell);
1549 tor_assert(circ);
1551 relay_header_unpack(&rh, cell->payload);
1552 // log_fn(LOG_DEBUG,"command %d stream %d", rh.command, rh.stream_id);
1553 num_seen++;
1554 log_debug(domain, "Now seen %d relay cells here (command %d, stream %d).",
1555 num_seen, rh.command, rh.stream_id);
1557 if (rh.length > RELAY_PAYLOAD_SIZE) {
1558 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1559 "Relay cell length field too long. Closing circuit.");
1560 return - END_CIRC_REASON_TORPROTOCOL;
1563 if (rh.stream_id == 0) {
1564 switch (rh.command) {
1565 case RELAY_COMMAND_BEGIN:
1566 case RELAY_COMMAND_CONNECTED:
1567 case RELAY_COMMAND_END:
1568 case RELAY_COMMAND_RESOLVE:
1569 case RELAY_COMMAND_RESOLVED:
1570 case RELAY_COMMAND_BEGIN_DIR:
1571 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, "Relay command %d with zero "
1572 "stream_id. Dropping.", (int)rh.command);
1573 return 0;
1574 default:
1579 /* either conn is NULL, in which case we've got a control cell, or else
1580 * conn points to the recognized stream. */
1582 if (conn && !connection_state_is_open(TO_CONN(conn))) {
1583 if (conn->base_.type == CONN_TYPE_EXIT &&
1584 (conn->base_.state == EXIT_CONN_STATE_CONNECTING ||
1585 conn->base_.state == EXIT_CONN_STATE_RESOLVING) &&
1586 rh.command == RELAY_COMMAND_DATA) {
1587 /* Allow DATA cells to be delivered to an exit node in state
1588 * EXIT_CONN_STATE_CONNECTING or EXIT_CONN_STATE_RESOLVING.
1589 * This speeds up HTTP, for example. */
1590 optimistic_data = 1;
1591 } else if (rh.stream_id == 0 && rh.command == RELAY_COMMAND_DATA) {
1592 log_warn(LD_BUG, "Somehow I had a connection that matched a "
1593 "data cell with stream ID 0.");
1594 } else {
1595 return connection_edge_process_relay_cell_not_open(
1596 &rh, cell, circ, conn, layer_hint);
1600 switch (rh.command) {
1601 case RELAY_COMMAND_DROP:
1602 rep_hist_padding_count_read(PADDING_TYPE_DROP);
1603 // log_info(domain,"Got a relay-level padding cell. Dropping.");
1604 return 0;
1605 case RELAY_COMMAND_BEGIN:
1606 case RELAY_COMMAND_BEGIN_DIR:
1607 if (layer_hint &&
1608 circ->purpose != CIRCUIT_PURPOSE_S_REND_JOINED) {
1609 log_fn(LOG_PROTOCOL_WARN, LD_APP,
1610 "Relay begin request unsupported at AP. Dropping.");
1611 return 0;
1613 if (circ->purpose == CIRCUIT_PURPOSE_S_REND_JOINED &&
1614 layer_hint != TO_ORIGIN_CIRCUIT(circ)->cpath->prev) {
1615 log_fn(LOG_PROTOCOL_WARN, LD_APP,
1616 "Relay begin request to Hidden Service "
1617 "from intermediary node. Dropping.");
1618 return 0;
1620 if (conn) {
1621 log_fn(LOG_PROTOCOL_WARN, domain,
1622 "Begin cell for known stream. Dropping.");
1623 return 0;
1625 if (rh.command == RELAY_COMMAND_BEGIN_DIR &&
1626 circ->purpose != CIRCUIT_PURPOSE_S_REND_JOINED) {
1627 /* Assign this circuit and its app-ward OR connection a unique ID,
1628 * so that we can measure download times. The local edge and dir
1629 * connection will be assigned the same ID when they are created
1630 * and linked. */
1631 static uint64_t next_id = 0;
1632 circ->dirreq_id = ++next_id;
1633 TO_OR_CIRCUIT(circ)->p_chan->dirreq_id = circ->dirreq_id;
1636 return connection_exit_begin_conn(cell, circ);
1637 case RELAY_COMMAND_DATA:
1638 ++stats_n_data_cells_received;
1639 if (( layer_hint && --layer_hint->deliver_window < 0) ||
1640 (!layer_hint && --circ->deliver_window < 0)) {
1641 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1642 "(relay data) circ deliver_window below 0. Killing.");
1643 if (conn) {
1644 /* XXXX Do we actually need to do this? Will killing the circuit
1645 * not send an END and mark the stream for close as appropriate? */
1646 connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL);
1647 connection_mark_for_close(TO_CONN(conn));
1649 return -END_CIRC_REASON_TORPROTOCOL;
1651 log_debug(domain,"circ deliver_window now %d.", layer_hint ?
1652 layer_hint->deliver_window : circ->deliver_window);
1654 circuit_consider_sending_sendme(circ, layer_hint);
1656 if (rh.stream_id == 0) {
1657 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, "Relay data cell with zero "
1658 "stream_id. Dropping.");
1659 return 0;
1660 } else if (!conn) {
1661 log_info(domain,"data cell dropped, unknown stream (streamid %d).",
1662 rh.stream_id);
1663 return 0;
1666 if (--conn->deliver_window < 0) { /* is it below 0 after decrement? */
1667 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1668 "(relay data) conn deliver_window below 0. Killing.");
1669 return -END_CIRC_REASON_TORPROTOCOL;
1672 stats_n_data_bytes_received += rh.length;
1673 connection_write_to_buf((char*)(cell->payload + RELAY_HEADER_SIZE),
1674 rh.length, TO_CONN(conn));
1676 #ifdef MEASUREMENTS_21206
1677 /* Count number of RELAY_DATA cells received on a linked directory
1678 * connection. */
1679 connection_t *linked_conn = TO_CONN(conn)->linked_conn;
1681 if (linked_conn && linked_conn->type == CONN_TYPE_DIR) {
1682 ++(TO_DIR_CONN(linked_conn)->data_cells_received);
1684 #endif
1686 if (!optimistic_data) {
1687 /* Only send a SENDME if we're not getting optimistic data; otherwise
1688 * a SENDME could arrive before the CONNECTED.
1690 connection_edge_consider_sending_sendme(conn);
1693 return 0;
1694 case RELAY_COMMAND_END:
1695 reason = rh.length > 0 ?
1696 get_uint8(cell->payload+RELAY_HEADER_SIZE) : END_STREAM_REASON_MISC;
1697 if (!conn) {
1698 log_info(domain,"end cell (%s) dropped, unknown stream.",
1699 stream_end_reason_to_string(reason));
1700 return 0;
1702 /* XXX add to this log_fn the exit node's nickname? */
1703 log_info(domain,TOR_SOCKET_T_FORMAT": end cell (%s) for stream %d. "
1704 "Removing stream.",
1705 conn->base_.s,
1706 stream_end_reason_to_string(reason),
1707 conn->stream_id);
1708 if (conn->base_.type == CONN_TYPE_AP) {
1709 entry_connection_t *entry_conn = EDGE_TO_ENTRY_CONN(conn);
1710 if (entry_conn->socks_request &&
1711 !entry_conn->socks_request->has_finished)
1712 log_warn(LD_BUG,
1713 "open stream hasn't sent socks answer yet? Closing.");
1715 /* We just *got* an end; no reason to send one. */
1716 conn->edge_has_sent_end = 1;
1717 if (!conn->end_reason)
1718 conn->end_reason = reason | END_STREAM_REASON_FLAG_REMOTE;
1719 if (!conn->base_.marked_for_close) {
1720 /* only mark it if not already marked. it's possible to
1721 * get the 'end' right around when the client hangs up on us. */
1722 connection_mark_and_flush(TO_CONN(conn));
1724 return 0;
1725 case RELAY_COMMAND_EXTEND:
1726 case RELAY_COMMAND_EXTEND2: {
1727 static uint64_t total_n_extend=0, total_nonearly=0;
1728 total_n_extend++;
1729 if (rh.stream_id) {
1730 log_fn(LOG_PROTOCOL_WARN, domain,
1731 "'extend' cell received for non-zero stream. Dropping.");
1732 return 0;
1734 if (cell->command != CELL_RELAY_EARLY &&
1735 !networkstatus_get_param(NULL,"AllowNonearlyExtend",0,0,1)) {
1736 #define EARLY_WARNING_INTERVAL 3600
1737 static ratelim_t early_warning_limit =
1738 RATELIM_INIT(EARLY_WARNING_INTERVAL);
1739 char *m;
1740 if (cell->command == CELL_RELAY) {
1741 ++total_nonearly;
1742 if ((m = rate_limit_log(&early_warning_limit, approx_time()))) {
1743 double percentage = ((double)total_nonearly)/total_n_extend;
1744 percentage *= 100;
1745 log_fn(LOG_PROTOCOL_WARN, domain, "EXTEND cell received, "
1746 "but not via RELAY_EARLY. Dropping.%s", m);
1747 log_fn(LOG_PROTOCOL_WARN, domain, " (We have dropped %.02f%% of "
1748 "all EXTEND cells for this reason)", percentage);
1749 tor_free(m);
1751 } else {
1752 log_fn(LOG_WARN, domain,
1753 "EXTEND cell received, in a cell with type %d! Dropping.",
1754 cell->command);
1756 return 0;
1758 return circuit_extend(cell, circ);
1760 case RELAY_COMMAND_EXTENDED:
1761 case RELAY_COMMAND_EXTENDED2:
1762 if (!layer_hint) {
1763 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1764 "'extended' unsupported at non-origin. Dropping.");
1765 return 0;
1767 log_debug(domain,"Got an extended cell! Yay.");
1769 extended_cell_t extended_cell;
1770 if (extended_cell_parse(&extended_cell, rh.command,
1771 (const uint8_t*)cell->payload+RELAY_HEADER_SIZE,
1772 rh.length)<0) {
1773 log_warn(LD_PROTOCOL,
1774 "Can't parse EXTENDED cell; killing circuit.");
1775 return -END_CIRC_REASON_TORPROTOCOL;
1777 if ((reason = circuit_finish_handshake(TO_ORIGIN_CIRCUIT(circ),
1778 &extended_cell.created_cell)) < 0) {
1779 circuit_mark_for_close(circ, -reason);
1780 return 0; /* We don't want to cause a warning, so we mark the circuit
1781 * here. */
1784 if ((reason=circuit_send_next_onion_skin(TO_ORIGIN_CIRCUIT(circ)))<0) {
1785 log_info(domain,"circuit_send_next_onion_skin() failed.");
1786 return reason;
1788 return 0;
1789 case RELAY_COMMAND_TRUNCATE:
1790 if (layer_hint) {
1791 log_fn(LOG_PROTOCOL_WARN, LD_APP,
1792 "'truncate' unsupported at origin. Dropping.");
1793 return 0;
1795 if (circ->n_hop) {
1796 if (circ->n_chan)
1797 log_warn(LD_BUG, "n_chan and n_hop set on the same circuit!");
1798 extend_info_free(circ->n_hop);
1799 circ->n_hop = NULL;
1800 tor_free(circ->n_chan_create_cell);
1801 circuit_set_state(circ, CIRCUIT_STATE_OPEN);
1803 if (circ->n_chan) {
1804 uint8_t trunc_reason = get_uint8(cell->payload + RELAY_HEADER_SIZE);
1805 circuit_clear_cell_queue(circ, circ->n_chan);
1806 channel_send_destroy(circ->n_circ_id, circ->n_chan,
1807 trunc_reason);
1808 circuit_set_n_circid_chan(circ, 0, NULL);
1810 log_debug(LD_EXIT, "Processed 'truncate', replying.");
1812 char payload[1];
1813 payload[0] = (char)END_CIRC_REASON_REQUESTED;
1814 relay_send_command_from_edge(0, circ, RELAY_COMMAND_TRUNCATED,
1815 payload, sizeof(payload), NULL);
1817 return 0;
1818 case RELAY_COMMAND_TRUNCATED:
1819 if (!layer_hint) {
1820 log_fn(LOG_PROTOCOL_WARN, LD_EXIT,
1821 "'truncated' unsupported at non-origin. Dropping.");
1822 return 0;
1824 circuit_truncated(TO_ORIGIN_CIRCUIT(circ), layer_hint,
1825 get_uint8(cell->payload + RELAY_HEADER_SIZE));
1826 return 0;
1827 case RELAY_COMMAND_CONNECTED:
1828 if (conn) {
1829 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1830 "'connected' unsupported while open. Closing circ.");
1831 return -END_CIRC_REASON_TORPROTOCOL;
1833 log_info(domain,
1834 "'connected' received on circid %u for streamid %d, "
1835 "no conn attached anymore. Ignoring.",
1836 (unsigned)circ->n_circ_id, rh.stream_id);
1837 return 0;
1838 case RELAY_COMMAND_SENDME:
1839 if (!rh.stream_id) {
1840 if (layer_hint) {
1841 if (layer_hint->package_window + CIRCWINDOW_INCREMENT >
1842 CIRCWINDOW_START_MAX) {
1843 static struct ratelim_t exit_warn_ratelim = RATELIM_INIT(600);
1844 log_fn_ratelim(&exit_warn_ratelim, LOG_WARN, LD_PROTOCOL,
1845 "Unexpected sendme cell from exit relay. "
1846 "Closing circ.");
1847 return -END_CIRC_REASON_TORPROTOCOL;
1849 layer_hint->package_window += CIRCWINDOW_INCREMENT;
1850 log_debug(LD_APP,"circ-level sendme at origin, packagewindow %d.",
1851 layer_hint->package_window);
1852 circuit_resume_edge_reading(circ, layer_hint);
1853 } else {
1854 if (circ->package_window + CIRCWINDOW_INCREMENT >
1855 CIRCWINDOW_START_MAX) {
1856 static struct ratelim_t client_warn_ratelim = RATELIM_INIT(600);
1857 log_fn_ratelim(&client_warn_ratelim,LOG_PROTOCOL_WARN, LD_PROTOCOL,
1858 "Unexpected sendme cell from client. "
1859 "Closing circ (window %d).",
1860 circ->package_window);
1861 return -END_CIRC_REASON_TORPROTOCOL;
1863 circ->package_window += CIRCWINDOW_INCREMENT;
1864 log_debug(LD_APP,
1865 "circ-level sendme at non-origin, packagewindow %d.",
1866 circ->package_window);
1867 circuit_resume_edge_reading(circ, layer_hint);
1869 return 0;
1871 if (!conn) {
1872 log_info(domain,"sendme cell dropped, unknown stream (streamid %d).",
1873 rh.stream_id);
1874 return 0;
1876 conn->package_window += STREAMWINDOW_INCREMENT;
1877 log_debug(domain,"stream-level sendme, packagewindow now %d.",
1878 conn->package_window);
1879 if (circuit_queue_streams_are_blocked(circ)) {
1880 /* Still waiting for queue to flush; don't touch conn */
1881 return 0;
1883 connection_start_reading(TO_CONN(conn));
1884 /* handle whatever might still be on the inbuf */
1885 if (connection_edge_package_raw_inbuf(conn, 1, NULL) < 0) {
1886 /* (We already sent an end cell if possible) */
1887 connection_mark_for_close(TO_CONN(conn));
1888 return 0;
1890 return 0;
1891 case RELAY_COMMAND_RESOLVE:
1892 if (layer_hint) {
1893 log_fn(LOG_PROTOCOL_WARN, LD_APP,
1894 "resolve request unsupported at AP; dropping.");
1895 return 0;
1896 } else if (conn) {
1897 log_fn(LOG_PROTOCOL_WARN, domain,
1898 "resolve request for known stream; dropping.");
1899 return 0;
1900 } else if (circ->purpose != CIRCUIT_PURPOSE_OR) {
1901 log_fn(LOG_PROTOCOL_WARN, domain,
1902 "resolve request on circ with purpose %d; dropping",
1903 circ->purpose);
1904 return 0;
1906 connection_exit_begin_resolve(cell, TO_OR_CIRCUIT(circ));
1907 return 0;
1908 case RELAY_COMMAND_RESOLVED:
1909 if (conn) {
1910 log_fn(LOG_PROTOCOL_WARN, domain,
1911 "'resolved' unsupported while open. Closing circ.");
1912 return -END_CIRC_REASON_TORPROTOCOL;
1914 log_info(domain,
1915 "'resolved' received, no conn attached anymore. Ignoring.");
1916 return 0;
1917 case RELAY_COMMAND_ESTABLISH_INTRO:
1918 case RELAY_COMMAND_ESTABLISH_RENDEZVOUS:
1919 case RELAY_COMMAND_INTRODUCE1:
1920 case RELAY_COMMAND_INTRODUCE2:
1921 case RELAY_COMMAND_INTRODUCE_ACK:
1922 case RELAY_COMMAND_RENDEZVOUS1:
1923 case RELAY_COMMAND_RENDEZVOUS2:
1924 case RELAY_COMMAND_INTRO_ESTABLISHED:
1925 case RELAY_COMMAND_RENDEZVOUS_ESTABLISHED:
1926 rend_process_relay_cell(circ, layer_hint,
1927 rh.command, rh.length,
1928 cell->payload+RELAY_HEADER_SIZE);
1929 return 0;
1931 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1932 "Received unknown relay command %d. Perhaps the other side is using "
1933 "a newer version of Tor? Dropping.",
1934 rh.command);
1935 return 0; /* for forward compatibility, don't kill the circuit */
1938 /** How many relay_data cells have we built, ever? */
1939 uint64_t stats_n_data_cells_packaged = 0;
1940 /** How many bytes of data have we put in relay_data cells have we built,
1941 * ever? This would be RELAY_PAYLOAD_SIZE*stats_n_data_cells_packaged if
1942 * every relay cell we ever sent were completely full of data. */
1943 uint64_t stats_n_data_bytes_packaged = 0;
1944 /** How many relay_data cells have we received, ever? */
1945 uint64_t stats_n_data_cells_received = 0;
1946 /** How many bytes of data have we received relay_data cells, ever? This would
1947 * be RELAY_PAYLOAD_SIZE*stats_n_data_cells_packaged if every relay cell we
1948 * ever received were completely full of data. */
1949 uint64_t stats_n_data_bytes_received = 0;
1951 /** If <b>conn</b> has an entire relay payload of bytes on its inbuf (or
1952 * <b>package_partial</b> is true), and the appropriate package windows aren't
1953 * empty, grab a cell and send it down the circuit.
1955 * If *<b>max_cells</b> is given, package no more than max_cells. Decrement
1956 * *<b>max_cells</b> by the number of cells packaged.
1958 * Return -1 (and send a RELAY_COMMAND_END cell if necessary) if conn should
1959 * be marked for close, else return 0.
1962 connection_edge_package_raw_inbuf(edge_connection_t *conn, int package_partial,
1963 int *max_cells)
1965 size_t bytes_to_process, length;
1966 char payload[CELL_PAYLOAD_SIZE];
1967 circuit_t *circ;
1968 const unsigned domain = conn->base_.type == CONN_TYPE_AP ? LD_APP : LD_EXIT;
1969 int sending_from_optimistic = 0;
1970 entry_connection_t *entry_conn =
1971 conn->base_.type == CONN_TYPE_AP ? EDGE_TO_ENTRY_CONN(conn) : NULL;
1972 const int sending_optimistically =
1973 entry_conn &&
1974 conn->base_.type == CONN_TYPE_AP &&
1975 conn->base_.state != AP_CONN_STATE_OPEN;
1976 crypt_path_t *cpath_layer = conn->cpath_layer;
1978 tor_assert(conn);
1980 if (conn->base_.marked_for_close) {
1981 log_warn(LD_BUG,
1982 "called on conn that's already marked for close at %s:%d.",
1983 conn->base_.marked_for_close_file, conn->base_.marked_for_close);
1984 return 0;
1987 if (max_cells && *max_cells <= 0)
1988 return 0;
1990 repeat_connection_edge_package_raw_inbuf:
1992 circ = circuit_get_by_edge_conn(conn);
1993 if (!circ) {
1994 log_info(domain,"conn has no circuit! Closing.");
1995 conn->end_reason = END_STREAM_REASON_CANT_ATTACH;
1996 return -1;
1999 if (circuit_consider_stop_edge_reading(circ, cpath_layer))
2000 return 0;
2002 if (conn->package_window <= 0) {
2003 log_info(domain,"called with package_window %d. Skipping.",
2004 conn->package_window);
2005 connection_stop_reading(TO_CONN(conn));
2006 return 0;
2009 sending_from_optimistic = entry_conn &&
2010 entry_conn->sending_optimistic_data != NULL;
2012 if (PREDICT_UNLIKELY(sending_from_optimistic)) {
2013 bytes_to_process = buf_datalen(entry_conn->sending_optimistic_data);
2014 if (PREDICT_UNLIKELY(!bytes_to_process)) {
2015 log_warn(LD_BUG, "sending_optimistic_data was non-NULL but empty");
2016 bytes_to_process = connection_get_inbuf_len(TO_CONN(conn));
2017 sending_from_optimistic = 0;
2019 } else {
2020 bytes_to_process = connection_get_inbuf_len(TO_CONN(conn));
2023 if (!bytes_to_process)
2024 return 0;
2026 if (!package_partial && bytes_to_process < RELAY_PAYLOAD_SIZE)
2027 return 0;
2029 if (bytes_to_process > RELAY_PAYLOAD_SIZE) {
2030 length = RELAY_PAYLOAD_SIZE;
2031 } else {
2032 length = bytes_to_process;
2034 stats_n_data_bytes_packaged += length;
2035 stats_n_data_cells_packaged += 1;
2037 if (PREDICT_UNLIKELY(sending_from_optimistic)) {
2038 /* XXXX We could be more efficient here by sometimes packing
2039 * previously-sent optimistic data in the same cell with data
2040 * from the inbuf. */
2041 fetch_from_buf(payload, length, entry_conn->sending_optimistic_data);
2042 if (!buf_datalen(entry_conn->sending_optimistic_data)) {
2043 buf_free(entry_conn->sending_optimistic_data);
2044 entry_conn->sending_optimistic_data = NULL;
2046 } else {
2047 connection_fetch_from_buf(payload, length, TO_CONN(conn));
2050 log_debug(domain,TOR_SOCKET_T_FORMAT": Packaging %d bytes (%d waiting).",
2051 conn->base_.s,
2052 (int)length, (int)connection_get_inbuf_len(TO_CONN(conn)));
2054 if (sending_optimistically && !sending_from_optimistic) {
2055 /* This is new optimistic data; remember it in case we need to detach and
2056 retry */
2057 if (!entry_conn->pending_optimistic_data)
2058 entry_conn->pending_optimistic_data = buf_new();
2059 write_to_buf(payload, length, entry_conn->pending_optimistic_data);
2062 if (connection_edge_send_command(conn, RELAY_COMMAND_DATA,
2063 payload, length) < 0 )
2064 /* circuit got marked for close, don't continue, don't need to mark conn */
2065 return 0;
2067 if (!cpath_layer) { /* non-rendezvous exit */
2068 tor_assert(circ->package_window > 0);
2069 circ->package_window--;
2070 } else { /* we're an AP, or an exit on a rendezvous circ */
2071 tor_assert(cpath_layer->package_window > 0);
2072 cpath_layer->package_window--;
2075 if (--conn->package_window <= 0) { /* is it 0 after decrement? */
2076 connection_stop_reading(TO_CONN(conn));
2077 log_debug(domain,"conn->package_window reached 0.");
2078 circuit_consider_stop_edge_reading(circ, cpath_layer);
2079 return 0; /* don't process the inbuf any more */
2081 log_debug(domain,"conn->package_window is now %d",conn->package_window);
2083 if (max_cells) {
2084 *max_cells -= 1;
2085 if (*max_cells <= 0)
2086 return 0;
2089 /* handle more if there's more, or return 0 if there isn't */
2090 goto repeat_connection_edge_package_raw_inbuf;
2093 /** Called when we've just received a relay data cell, when
2094 * we've just finished flushing all bytes to stream <b>conn</b>,
2095 * or when we've flushed *some* bytes to the stream <b>conn</b>.
2097 * If conn->outbuf is not too full, and our deliver window is
2098 * low, send back a suitable number of stream-level sendme cells.
2100 void
2101 connection_edge_consider_sending_sendme(edge_connection_t *conn)
2103 circuit_t *circ;
2105 if (connection_outbuf_too_full(TO_CONN(conn)))
2106 return;
2108 circ = circuit_get_by_edge_conn(conn);
2109 if (!circ) {
2110 /* this can legitimately happen if the destroy has already
2111 * arrived and torn down the circuit */
2112 log_info(LD_APP,"No circuit associated with conn. Skipping.");
2113 return;
2116 while (conn->deliver_window <= STREAMWINDOW_START - STREAMWINDOW_INCREMENT) {
2117 log_debug(conn->base_.type == CONN_TYPE_AP ?LD_APP:LD_EXIT,
2118 "Outbuf %d, Queuing stream sendme.",
2119 (int)conn->base_.outbuf_flushlen);
2120 conn->deliver_window += STREAMWINDOW_INCREMENT;
2121 if (connection_edge_send_command(conn, RELAY_COMMAND_SENDME,
2122 NULL, 0) < 0) {
2123 log_warn(LD_APP,"connection_edge_send_command failed. Skipping.");
2124 return; /* the circuit's closed, don't continue */
2129 /** The circuit <b>circ</b> has received a circuit-level sendme
2130 * (on hop <b>layer_hint</b>, if we're the OP). Go through all the
2131 * attached streams and let them resume reading and packaging, if
2132 * their stream windows allow it.
2134 static void
2135 circuit_resume_edge_reading(circuit_t *circ, crypt_path_t *layer_hint)
2137 if (circuit_queue_streams_are_blocked(circ)) {
2138 log_debug(layer_hint?LD_APP:LD_EXIT,"Too big queue, no resuming");
2139 return;
2141 log_debug(layer_hint?LD_APP:LD_EXIT,"resuming");
2143 if (CIRCUIT_IS_ORIGIN(circ))
2144 circuit_resume_edge_reading_helper(TO_ORIGIN_CIRCUIT(circ)->p_streams,
2145 circ, layer_hint);
2146 else
2147 circuit_resume_edge_reading_helper(TO_OR_CIRCUIT(circ)->n_streams,
2148 circ, layer_hint);
2151 void
2152 stream_choice_seed_weak_rng(void)
2154 crypto_seed_weak_rng(&stream_choice_rng);
2157 /** A helper function for circuit_resume_edge_reading() above.
2158 * The arguments are the same, except that <b>conn</b> is the head
2159 * of a linked list of edge streams that should each be considered.
2161 static int
2162 circuit_resume_edge_reading_helper(edge_connection_t *first_conn,
2163 circuit_t *circ,
2164 crypt_path_t *layer_hint)
2166 edge_connection_t *conn;
2167 int n_packaging_streams, n_streams_left;
2168 int packaged_this_round;
2169 int cells_on_queue;
2170 int cells_per_conn;
2171 edge_connection_t *chosen_stream = NULL;
2172 int max_to_package;
2174 if (first_conn == NULL) {
2175 /* Don't bother to try to do the rest of this if there are no connections
2176 * to resume. */
2177 return 0;
2180 /* How many cells do we have space for? It will be the minimum of
2181 * the number needed to exhaust the package window, and the minimum
2182 * needed to fill the cell queue. */
2183 max_to_package = circ->package_window;
2184 if (CIRCUIT_IS_ORIGIN(circ)) {
2185 cells_on_queue = circ->n_chan_cells.n;
2186 } else {
2187 or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
2188 cells_on_queue = or_circ->p_chan_cells.n;
2190 if (CELL_QUEUE_HIGHWATER_SIZE - cells_on_queue < max_to_package)
2191 max_to_package = CELL_QUEUE_HIGHWATER_SIZE - cells_on_queue;
2193 /* Once we used to start listening on the streams in the order they
2194 * appeared in the linked list. That leads to starvation on the
2195 * streams that appeared later on the list, since the first streams
2196 * would always get to read first. Instead, we just pick a random
2197 * stream on the list, and enable reading for streams starting at that
2198 * point (and wrapping around as if the list were circular). It would
2199 * probably be better to actually remember which streams we've
2200 * serviced in the past, but this is simple and effective. */
2202 /* Select a stream uniformly at random from the linked list. We
2203 * don't need cryptographic randomness here. */
2205 int num_streams = 0;
2206 for (conn = first_conn; conn; conn = conn->next_stream) {
2207 num_streams++;
2208 if (tor_weak_random_one_in_n(&stream_choice_rng, num_streams)) {
2209 chosen_stream = conn;
2211 /* Invariant: chosen_stream has been chosen uniformly at random from
2212 * among the first num_streams streams on first_conn.
2214 * (Note that we iterate over every stream on the circuit, so that after
2215 * we've considered the first stream, we've chosen it with P=1; and
2216 * after we consider the second stream, we've switched to it with P=1/2
2217 * and stayed with the first stream with P=1/2; and after we've
2218 * considered the third stream, we've switched to it with P=1/3 and
2219 * remained with one of the first two streams with P=(2/3), giving each
2220 * one P=(1/2)(2/3) )=(1/3).) */
2224 /* Count how many non-marked streams there are that have anything on
2225 * their inbuf, and enable reading on all of the connections. */
2226 n_packaging_streams = 0;
2227 /* Activate reading starting from the chosen stream */
2228 for (conn=chosen_stream; conn; conn = conn->next_stream) {
2229 /* Start reading for the streams starting from here */
2230 if (conn->base_.marked_for_close || conn->package_window <= 0)
2231 continue;
2232 if (!layer_hint || conn->cpath_layer == layer_hint) {
2233 connection_start_reading(TO_CONN(conn));
2235 if (connection_get_inbuf_len(TO_CONN(conn)) > 0)
2236 ++n_packaging_streams;
2239 /* Go back and do the ones we skipped, circular-style */
2240 for (conn = first_conn; conn != chosen_stream; conn = conn->next_stream) {
2241 if (conn->base_.marked_for_close || conn->package_window <= 0)
2242 continue;
2243 if (!layer_hint || conn->cpath_layer == layer_hint) {
2244 connection_start_reading(TO_CONN(conn));
2246 if (connection_get_inbuf_len(TO_CONN(conn)) > 0)
2247 ++n_packaging_streams;
2251 if (n_packaging_streams == 0) /* avoid divide-by-zero */
2252 return 0;
2254 again:
2256 cells_per_conn = CEIL_DIV(max_to_package, n_packaging_streams);
2258 packaged_this_round = 0;
2259 n_streams_left = 0;
2261 /* Iterate over all connections. Package up to cells_per_conn cells on
2262 * each. Update packaged_this_round with the total number of cells
2263 * packaged, and n_streams_left with the number that still have data to
2264 * package.
2266 for (conn=first_conn; conn; conn=conn->next_stream) {
2267 if (conn->base_.marked_for_close || conn->package_window <= 0)
2268 continue;
2269 if (!layer_hint || conn->cpath_layer == layer_hint) {
2270 int n = cells_per_conn, r;
2271 /* handle whatever might still be on the inbuf */
2272 r = connection_edge_package_raw_inbuf(conn, 1, &n);
2274 /* Note how many we packaged */
2275 packaged_this_round += (cells_per_conn-n);
2277 if (r<0) {
2278 /* Problem while packaging. (We already sent an end cell if
2279 * possible) */
2280 connection_mark_for_close(TO_CONN(conn));
2281 continue;
2284 /* If there's still data to read, we'll be coming back to this stream. */
2285 if (connection_get_inbuf_len(TO_CONN(conn)))
2286 ++n_streams_left;
2288 /* If the circuit won't accept any more data, return without looking
2289 * at any more of the streams. Any connections that should be stopped
2290 * have already been stopped by connection_edge_package_raw_inbuf. */
2291 if (circuit_consider_stop_edge_reading(circ, layer_hint))
2292 return -1;
2293 /* XXXX should we also stop immediately if we fill up the cell queue?
2294 * Probably. */
2298 /* If we made progress, and we are willing to package more, and there are
2299 * any streams left that want to package stuff... try again!
2301 if (packaged_this_round && packaged_this_round < max_to_package &&
2302 n_streams_left) {
2303 max_to_package -= packaged_this_round;
2304 n_packaging_streams = n_streams_left;
2305 goto again;
2308 return 0;
2311 /** Check if the package window for <b>circ</b> is empty (at
2312 * hop <b>layer_hint</b> if it's defined).
2314 * If yes, tell edge streams to stop reading and return 1.
2315 * Else return 0.
2317 static int
2318 circuit_consider_stop_edge_reading(circuit_t *circ, crypt_path_t *layer_hint)
2320 edge_connection_t *conn = NULL;
2321 unsigned domain = layer_hint ? LD_APP : LD_EXIT;
2323 if (!layer_hint) {
2324 or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
2325 log_debug(domain,"considering circ->package_window %d",
2326 circ->package_window);
2327 if (circ->package_window <= 0) {
2328 log_debug(domain,"yes, not-at-origin. stopped.");
2329 for (conn = or_circ->n_streams; conn; conn=conn->next_stream)
2330 connection_stop_reading(TO_CONN(conn));
2331 return 1;
2333 return 0;
2335 /* else, layer hint is defined, use it */
2336 log_debug(domain,"considering layer_hint->package_window %d",
2337 layer_hint->package_window);
2338 if (layer_hint->package_window <= 0) {
2339 log_debug(domain,"yes, at-origin. stopped.");
2340 for (conn = TO_ORIGIN_CIRCUIT(circ)->p_streams; conn;
2341 conn=conn->next_stream) {
2342 if (conn->cpath_layer == layer_hint)
2343 connection_stop_reading(TO_CONN(conn));
2345 return 1;
2347 return 0;
2350 /** Check if the deliver_window for circuit <b>circ</b> (at hop
2351 * <b>layer_hint</b> if it's defined) is low enough that we should
2352 * send a circuit-level sendme back down the circuit. If so, send
2353 * enough sendmes that the window would be overfull if we sent any
2354 * more.
2356 static void
2357 circuit_consider_sending_sendme(circuit_t *circ, crypt_path_t *layer_hint)
2359 // log_fn(LOG_INFO,"Considering: layer_hint is %s",
2360 // layer_hint ? "defined" : "null");
2361 while ((layer_hint ? layer_hint->deliver_window : circ->deliver_window) <=
2362 CIRCWINDOW_START - CIRCWINDOW_INCREMENT) {
2363 log_debug(LD_CIRC,"Queuing circuit sendme.");
2364 if (layer_hint)
2365 layer_hint->deliver_window += CIRCWINDOW_INCREMENT;
2366 else
2367 circ->deliver_window += CIRCWINDOW_INCREMENT;
2368 if (relay_send_command_from_edge(0, circ, RELAY_COMMAND_SENDME,
2369 NULL, 0, layer_hint) < 0) {
2370 log_warn(LD_CIRC,
2371 "relay_send_command_from_edge failed. Circuit's closed.");
2372 return; /* the circuit's closed, don't continue */
2377 #ifdef ACTIVE_CIRCUITS_PARANOIA
2378 #define assert_cmux_ok_paranoid(chan) \
2379 assert_circuit_mux_okay(chan)
2380 #else
2381 #define assert_cmux_ok_paranoid(chan)
2382 #endif
2384 /** The total number of cells we have allocated. */
2385 static size_t total_cells_allocated = 0;
2387 /** Release storage held by <b>cell</b>. */
2388 static inline void
2389 packed_cell_free_unchecked(packed_cell_t *cell)
2391 --total_cells_allocated;
2392 tor_free(cell);
2395 /** Allocate and return a new packed_cell_t. */
2396 STATIC packed_cell_t *
2397 packed_cell_new(void)
2399 ++total_cells_allocated;
2400 return tor_malloc_zero(sizeof(packed_cell_t));
2403 /** Return a packed cell used outside by channel_t lower layer */
2404 void
2405 packed_cell_free(packed_cell_t *cell)
2407 if (!cell)
2408 return;
2409 packed_cell_free_unchecked(cell);
2412 /** Log current statistics for cell pool allocation at log level
2413 * <b>severity</b>. */
2414 void
2415 dump_cell_pool_usage(int severity)
2417 int n_circs = 0;
2418 int n_cells = 0;
2419 SMARTLIST_FOREACH_BEGIN(circuit_get_global_list(), circuit_t *, c) {
2420 n_cells += c->n_chan_cells.n;
2421 if (!CIRCUIT_IS_ORIGIN(c))
2422 n_cells += TO_OR_CIRCUIT(c)->p_chan_cells.n;
2423 ++n_circs;
2425 SMARTLIST_FOREACH_END(c);
2426 tor_log(severity, LD_MM,
2427 "%d cells allocated on %d circuits. %d cells leaked.",
2428 n_cells, n_circs, (int)total_cells_allocated - n_cells);
2431 /** Allocate a new copy of packed <b>cell</b>. */
2432 static inline packed_cell_t *
2433 packed_cell_copy(const cell_t *cell, int wide_circ_ids)
2435 packed_cell_t *c = packed_cell_new();
2436 cell_pack(c, cell, wide_circ_ids);
2437 return c;
2440 /** Append <b>cell</b> to the end of <b>queue</b>. */
2441 void
2442 cell_queue_append(cell_queue_t *queue, packed_cell_t *cell)
2444 TOR_SIMPLEQ_INSERT_TAIL(&queue->head, cell, next);
2445 ++queue->n;
2448 /** Append a newly allocated copy of <b>cell</b> to the end of the
2449 * <b>exitward</b> (or app-ward) <b>queue</b> of <b>circ</b>. If
2450 * <b>use_stats</b> is true, record statistics about the cell.
2452 void
2453 cell_queue_append_packed_copy(circuit_t *circ, cell_queue_t *queue,
2454 int exitward, const cell_t *cell,
2455 int wide_circ_ids, int use_stats)
2457 packed_cell_t *copy = packed_cell_copy(cell, wide_circ_ids);
2458 (void)circ;
2459 (void)exitward;
2460 (void)use_stats;
2462 copy->inserted_time = (uint32_t) monotime_coarse_absolute_msec();
2464 cell_queue_append(queue, copy);
2467 /** Initialize <b>queue</b> as an empty cell queue. */
2468 void
2469 cell_queue_init(cell_queue_t *queue)
2471 memset(queue, 0, sizeof(cell_queue_t));
2472 TOR_SIMPLEQ_INIT(&queue->head);
2475 /** Remove and free every cell in <b>queue</b>. */
2476 void
2477 cell_queue_clear(cell_queue_t *queue)
2479 packed_cell_t *cell;
2480 while ((cell = TOR_SIMPLEQ_FIRST(&queue->head))) {
2481 TOR_SIMPLEQ_REMOVE_HEAD(&queue->head, next);
2482 packed_cell_free_unchecked(cell);
2484 TOR_SIMPLEQ_INIT(&queue->head);
2485 queue->n = 0;
2488 /** Extract and return the cell at the head of <b>queue</b>; return NULL if
2489 * <b>queue</b> is empty. */
2490 STATIC packed_cell_t *
2491 cell_queue_pop(cell_queue_t *queue)
2493 packed_cell_t *cell = TOR_SIMPLEQ_FIRST(&queue->head);
2494 if (!cell)
2495 return NULL;
2496 TOR_SIMPLEQ_REMOVE_HEAD(&queue->head, next);
2497 --queue->n;
2498 return cell;
2501 /** Return the total number of bytes used for each packed_cell in a queue.
2502 * Approximate. */
2503 size_t
2504 packed_cell_mem_cost(void)
2506 return sizeof(packed_cell_t);
2509 /* DOCDOC */
2510 STATIC size_t
2511 cell_queues_get_total_allocation(void)
2513 return total_cells_allocated * packed_cell_mem_cost();
2516 /** How long after we've been low on memory should we try to conserve it? */
2517 #define MEMORY_PRESSURE_INTERVAL (30*60)
2519 /** The time at which we were last low on memory. */
2520 static time_t last_time_under_memory_pressure = 0;
2522 /** Check whether we've got too much space used for cells. If so,
2523 * call the OOM handler and return 1. Otherwise, return 0. */
2524 STATIC int
2525 cell_queues_check_size(void)
2527 size_t alloc = cell_queues_get_total_allocation();
2528 alloc += buf_get_total_allocation();
2529 alloc += tor_compress_get_total_allocation();
2530 const size_t rend_cache_total = rend_cache_get_total_allocation();
2531 alloc += rend_cache_total;
2532 if (alloc >= get_options()->MaxMemInQueues_low_threshold) {
2533 last_time_under_memory_pressure = approx_time();
2534 if (alloc >= get_options()->MaxMemInQueues) {
2535 /* If we're spending over 20% of the memory limit on hidden service
2536 * descriptors, free them until we're down to 10%.
2538 if (rend_cache_total > get_options()->MaxMemInQueues / 5) {
2539 const size_t bytes_to_remove =
2540 rend_cache_total - (size_t)(get_options()->MaxMemInQueues / 10);
2541 alloc -= hs_cache_handle_oom(time(NULL), bytes_to_remove);
2543 circuits_handle_oom(alloc);
2544 return 1;
2547 return 0;
2550 /** Return true if we've been under memory pressure in the last
2551 * MEMORY_PRESSURE_INTERVAL seconds. */
2553 have_been_under_memory_pressure(void)
2555 return last_time_under_memory_pressure + MEMORY_PRESSURE_INTERVAL
2556 < approx_time();
2560 * Update the number of cells available on the circuit's n_chan or p_chan's
2561 * circuit mux.
2563 void
2564 update_circuit_on_cmux_(circuit_t *circ, cell_direction_t direction,
2565 const char *file, int lineno)
2567 channel_t *chan = NULL;
2568 or_circuit_t *or_circ = NULL;
2569 circuitmux_t *cmux = NULL;
2571 tor_assert(circ);
2573 /* Okay, get the channel */
2574 if (direction == CELL_DIRECTION_OUT) {
2575 chan = circ->n_chan;
2576 } else {
2577 or_circ = TO_OR_CIRCUIT(circ);
2578 chan = or_circ->p_chan;
2581 tor_assert(chan);
2582 tor_assert(chan->cmux);
2584 /* Now get the cmux */
2585 cmux = chan->cmux;
2587 /* Cmux sanity check */
2588 if (! circuitmux_is_circuit_attached(cmux, circ)) {
2589 log_warn(LD_BUG, "called on non-attached circuit from %s:%d",
2590 file, lineno);
2591 return;
2593 tor_assert(circuitmux_attached_circuit_direction(cmux, circ) == direction);
2595 assert_cmux_ok_paranoid(chan);
2597 /* Update the number of cells we have for the circuit mux */
2598 if (direction == CELL_DIRECTION_OUT) {
2599 circuitmux_set_num_cells(cmux, circ, circ->n_chan_cells.n);
2600 } else {
2601 circuitmux_set_num_cells(cmux, circ, or_circ->p_chan_cells.n);
2604 assert_cmux_ok_paranoid(chan);
2607 /** Remove all circuits from the cmux on <b>chan</b>.
2609 * If <b>circuits_out</b> is non-NULL, add all detached circuits to
2610 * <b>circuits_out</b>.
2612 void
2613 channel_unlink_all_circuits(channel_t *chan, smartlist_t *circuits_out)
2615 tor_assert(chan);
2616 tor_assert(chan->cmux);
2618 circuitmux_detach_all_circuits(chan->cmux, circuits_out);
2619 chan->num_n_circuits = 0;
2620 chan->num_p_circuits = 0;
2623 /** Block (if <b>block</b> is true) or unblock (if <b>block</b> is false)
2624 * every edge connection that is using <b>circ</b> to write to <b>chan</b>,
2625 * and start or stop reading as appropriate.
2627 * If <b>stream_id</b> is nonzero, block only the edge connection whose
2628 * stream_id matches it.
2630 * Returns the number of streams whose status we changed.
2632 static int
2633 set_streams_blocked_on_circ(circuit_t *circ, channel_t *chan,
2634 int block, streamid_t stream_id)
2636 edge_connection_t *edge = NULL;
2637 int n = 0;
2638 if (circ->n_chan == chan) {
2639 circ->streams_blocked_on_n_chan = block;
2640 if (CIRCUIT_IS_ORIGIN(circ))
2641 edge = TO_ORIGIN_CIRCUIT(circ)->p_streams;
2642 } else {
2643 circ->streams_blocked_on_p_chan = block;
2644 tor_assert(!CIRCUIT_IS_ORIGIN(circ));
2645 edge = TO_OR_CIRCUIT(circ)->n_streams;
2648 for (; edge; edge = edge->next_stream) {
2649 connection_t *conn = TO_CONN(edge);
2650 if (stream_id && edge->stream_id != stream_id)
2651 continue;
2653 if (edge->edge_blocked_on_circ != block) {
2654 ++n;
2655 edge->edge_blocked_on_circ = block;
2658 if (!conn->read_event) {
2659 /* This connection is a placeholder for something; probably a DNS
2660 * request. It can't actually stop or start reading.*/
2661 continue;
2664 if (block) {
2665 if (connection_is_reading(conn))
2666 connection_stop_reading(conn);
2667 } else {
2668 /* Is this right? */
2669 if (!connection_is_reading(conn))
2670 connection_start_reading(conn);
2674 return n;
2677 /** Extract the command from a packed cell. */
2678 static uint8_t
2679 packed_cell_get_command(const packed_cell_t *cell, int wide_circ_ids)
2681 if (wide_circ_ids) {
2682 return get_uint8(cell->body+4);
2683 } else {
2684 return get_uint8(cell->body+2);
2688 /** Extract the circuit ID from a packed cell. */
2689 circid_t
2690 packed_cell_get_circid(const packed_cell_t *cell, int wide_circ_ids)
2692 if (wide_circ_ids) {
2693 return ntohl(get_uint32(cell->body));
2694 } else {
2695 return ntohs(get_uint16(cell->body));
2699 /** Pull as many cells as possible (but no more than <b>max</b>) from the
2700 * queue of the first active circuit on <b>chan</b>, and write them to
2701 * <b>chan</b>-&gt;outbuf. Return the number of cells written. Advance
2702 * the active circuit pointer to the next active circuit in the ring. */
2703 MOCK_IMPL(int,
2704 channel_flush_from_first_active_circuit, (channel_t *chan, int max))
2706 circuitmux_t *cmux = NULL;
2707 int n_flushed = 0;
2708 cell_queue_t *queue, *destroy_queue=NULL;
2709 circuit_t *circ;
2710 or_circuit_t *or_circ;
2711 int streams_blocked;
2712 packed_cell_t *cell;
2714 /* Get the cmux */
2715 tor_assert(chan);
2716 tor_assert(chan->cmux);
2717 cmux = chan->cmux;
2719 /* Main loop: pick a circuit, send a cell, update the cmux */
2720 while (n_flushed < max) {
2721 circ = circuitmux_get_first_active_circuit(cmux, &destroy_queue);
2722 if (destroy_queue) {
2723 /* this code is duplicated from some of the logic below. Ugly! XXXX */
2724 tor_assert(destroy_queue->n > 0);
2725 cell = cell_queue_pop(destroy_queue);
2726 channel_write_packed_cell(chan, cell);
2727 /* Update the cmux destroy counter */
2728 circuitmux_notify_xmit_destroy(cmux);
2729 cell = NULL;
2730 ++n_flushed;
2731 continue;
2733 /* If it returns NULL, no cells left to send */
2734 if (!circ) break;
2735 assert_cmux_ok_paranoid(chan);
2737 if (circ->n_chan == chan) {
2738 queue = &circ->n_chan_cells;
2739 streams_blocked = circ->streams_blocked_on_n_chan;
2740 } else {
2741 or_circ = TO_OR_CIRCUIT(circ);
2742 tor_assert(or_circ->p_chan == chan);
2743 queue = &TO_OR_CIRCUIT(circ)->p_chan_cells;
2744 streams_blocked = circ->streams_blocked_on_p_chan;
2747 /* Circuitmux told us this was active, so it should have cells */
2748 if (/*BUG(*/ queue->n == 0 /*)*/) {
2749 log_warn(LD_BUG, "Found a supposedly active circuit with no cells "
2750 "to send. Trying to recover.");
2751 circuitmux_set_num_cells(cmux, circ, 0);
2752 if (! circ->marked_for_close)
2753 circuit_mark_for_close(circ, END_CIRC_REASON_INTERNAL);
2754 continue;
2757 tor_assert(queue->n > 0);
2760 * Get just one cell here; once we've sent it, that can change the circuit
2761 * selection, so we have to loop around for another even if this circuit
2762 * has more than one.
2764 cell = cell_queue_pop(queue);
2766 /* Calculate the exact time that this cell has spent in the queue. */
2767 if (get_options()->CellStatistics ||
2768 get_options()->TestingEnableCellStatsEvent) {
2769 uint32_t msec_waiting;
2770 uint32_t msec_now = (uint32_t)monotime_coarse_absolute_msec();
2771 msec_waiting = msec_now - cell->inserted_time;
2773 if (get_options()->CellStatistics && !CIRCUIT_IS_ORIGIN(circ)) {
2774 or_circ = TO_OR_CIRCUIT(circ);
2775 or_circ->total_cell_waiting_time += msec_waiting;
2776 or_circ->processed_cells++;
2779 if (get_options()->TestingEnableCellStatsEvent) {
2780 uint8_t command = packed_cell_get_command(cell, chan->wide_circ_ids);
2782 testing_cell_stats_entry_t *ent =
2783 tor_malloc_zero(sizeof(testing_cell_stats_entry_t));
2784 ent->command = command;
2785 ent->waiting_time = msec_waiting / 10;
2786 ent->removed = 1;
2787 if (circ->n_chan == chan)
2788 ent->exitward = 1;
2789 if (!circ->testing_cell_stats)
2790 circ->testing_cell_stats = smartlist_new();
2791 smartlist_add(circ->testing_cell_stats, ent);
2795 /* If we just flushed our queue and this circuit is used for a
2796 * tunneled directory request, possibly advance its state. */
2797 if (queue->n == 0 && chan->dirreq_id)
2798 geoip_change_dirreq_state(chan->dirreq_id,
2799 DIRREQ_TUNNELED,
2800 DIRREQ_CIRC_QUEUE_FLUSHED);
2802 /* Now send the cell */
2803 channel_write_packed_cell(chan, cell);
2804 cell = NULL;
2807 * Don't packed_cell_free_unchecked(cell) here because the channel will
2808 * do so when it gets out of the channel queue (probably already did, in
2809 * which case that was an immediate double-free bug).
2812 /* Update the counter */
2813 ++n_flushed;
2816 * Now update the cmux; tell it we've just sent a cell, and how many
2817 * we have left.
2819 circuitmux_notify_xmit_cells(cmux, circ, 1);
2820 circuitmux_set_num_cells(cmux, circ, queue->n);
2821 if (queue->n == 0)
2822 log_debug(LD_GENERAL, "Made a circuit inactive.");
2824 /* Is the cell queue low enough to unblock all the streams that are waiting
2825 * to write to this circuit? */
2826 if (streams_blocked && queue->n <= CELL_QUEUE_LOWWATER_SIZE)
2827 set_streams_blocked_on_circ(circ, chan, 0, 0); /* unblock streams */
2829 /* If n_flushed < max still, loop around and pick another circuit */
2832 /* Okay, we're done sending now */
2833 assert_cmux_ok_paranoid(chan);
2835 return n_flushed;
2838 #if 0
2839 /** Indicate the current preferred cap for middle circuits; zero disables
2840 * the cap. Right now it's just a constant, ORCIRC_MAX_MIDDLE_CELLS, but
2841 * the logic in append_cell_to_circuit_queue() is written to be correct
2842 * if we want to base it on a consensus param or something that might change
2843 * in the future.
2845 static int
2846 get_max_middle_cells(void)
2848 return ORCIRC_MAX_MIDDLE_CELLS;
2850 #endif
2852 /** Add <b>cell</b> to the queue of <b>circ</b> writing to <b>chan</b>
2853 * transmitting in <b>direction</b>. */
2854 void
2855 append_cell_to_circuit_queue(circuit_t *circ, channel_t *chan,
2856 cell_t *cell, cell_direction_t direction,
2857 streamid_t fromstream)
2859 or_circuit_t *orcirc = NULL;
2860 cell_queue_t *queue;
2861 int streams_blocked;
2862 #if 0
2863 uint32_t tgt_max_middle_cells, p_len, n_len, tmp, hard_max_middle_cells;
2864 #endif
2866 int exitward;
2867 if (circ->marked_for_close)
2868 return;
2870 exitward = (direction == CELL_DIRECTION_OUT);
2871 if (exitward) {
2872 queue = &circ->n_chan_cells;
2873 streams_blocked = circ->streams_blocked_on_n_chan;
2874 } else {
2875 orcirc = TO_OR_CIRCUIT(circ);
2876 queue = &orcirc->p_chan_cells;
2877 streams_blocked = circ->streams_blocked_on_p_chan;
2881 * Disabling this for now because of a possible guard discovery attack
2883 #if 0
2884 /* Are we a middle circuit about to exceed ORCIRC_MAX_MIDDLE_CELLS? */
2885 if ((circ->n_chan != NULL) && CIRCUIT_IS_ORCIRC(circ)) {
2886 orcirc = TO_OR_CIRCUIT(circ);
2887 if (orcirc->p_chan) {
2888 /* We are a middle circuit if we have both n_chan and p_chan */
2889 /* We'll need to know the current preferred maximum */
2890 tgt_max_middle_cells = get_max_middle_cells();
2891 if (tgt_max_middle_cells > 0) {
2892 /* Do we need to initialize middle_max_cells? */
2893 if (orcirc->max_middle_cells == 0) {
2894 orcirc->max_middle_cells = tgt_max_middle_cells;
2895 } else {
2896 if (tgt_max_middle_cells > orcirc->max_middle_cells) {
2897 /* If we want to increase the cap, we can do so right away */
2898 orcirc->max_middle_cells = tgt_max_middle_cells;
2899 } else if (tgt_max_middle_cells < orcirc->max_middle_cells) {
2901 * If we're shrinking the cap, we can't shrink past either queue;
2902 * compare tgt_max_middle_cells rather than tgt_max_middle_cells *
2903 * ORCIRC_MAX_MIDDLE_KILL_THRESH so the queues don't shrink enough
2904 * to generate spurious warnings, either.
2906 n_len = circ->n_chan_cells.n;
2907 p_len = orcirc->p_chan_cells.n;
2908 tmp = tgt_max_middle_cells;
2909 if (tmp < n_len) tmp = n_len;
2910 if (tmp < p_len) tmp = p_len;
2911 orcirc->max_middle_cells = tmp;
2913 /* else no change */
2915 } else {
2916 /* tgt_max_middle_cells == 0 indicates we should disable the cap */
2917 orcirc->max_middle_cells = 0;
2920 /* Now we know orcirc->max_middle_cells is set correctly */
2921 if (orcirc->max_middle_cells > 0) {
2922 hard_max_middle_cells =
2923 (uint32_t)(((double)orcirc->max_middle_cells) *
2924 ORCIRC_MAX_MIDDLE_KILL_THRESH);
2926 if ((unsigned)queue->n + 1 >= hard_max_middle_cells) {
2927 /* Queueing this cell would put queue over the kill theshold */
2928 log_warn(LD_CIRC,
2929 "Got a cell exceeding the hard cap of %u in the "
2930 "%s direction on middle circ ID %u on chan ID "
2931 U64_FORMAT "; killing the circuit.",
2932 hard_max_middle_cells,
2933 (direction == CELL_DIRECTION_OUT) ? "n" : "p",
2934 (direction == CELL_DIRECTION_OUT) ?
2935 circ->n_circ_id : orcirc->p_circ_id,
2936 U64_PRINTF_ARG(
2937 (direction == CELL_DIRECTION_OUT) ?
2938 circ->n_chan->global_identifier :
2939 orcirc->p_chan->global_identifier));
2940 circuit_mark_for_close(circ, END_CIRC_REASON_RESOURCELIMIT);
2941 return;
2942 } else if ((unsigned)queue->n + 1 == orcirc->max_middle_cells) {
2943 /* Only use ==, not >= for this test so we don't spam the log */
2944 log_warn(LD_CIRC,
2945 "While trying to queue a cell, reached the soft cap of %u "
2946 "in the %s direction on middle circ ID %u "
2947 "on chan ID " U64_FORMAT ".",
2948 orcirc->max_middle_cells,
2949 (direction == CELL_DIRECTION_OUT) ? "n" : "p",
2950 (direction == CELL_DIRECTION_OUT) ?
2951 circ->n_circ_id : orcirc->p_circ_id,
2952 U64_PRINTF_ARG(
2953 (direction == CELL_DIRECTION_OUT) ?
2954 circ->n_chan->global_identifier :
2955 orcirc->p_chan->global_identifier));
2960 #endif
2962 cell_queue_append_packed_copy(circ, queue, exitward, cell,
2963 chan->wide_circ_ids, 1);
2965 if (PREDICT_UNLIKELY(cell_queues_check_size())) {
2966 /* We ran the OOM handler */
2967 if (circ->marked_for_close)
2968 return;
2971 /* If we have too many cells on the circuit, we should stop reading from
2972 * the edge streams for a while. */
2973 if (!streams_blocked && queue->n >= CELL_QUEUE_HIGHWATER_SIZE)
2974 set_streams_blocked_on_circ(circ, chan, 1, 0); /* block streams */
2976 if (streams_blocked && fromstream) {
2977 /* This edge connection is apparently not blocked; block it. */
2978 set_streams_blocked_on_circ(circ, chan, 1, fromstream);
2981 update_circuit_on_cmux(circ, direction);
2982 if (queue->n == 1) {
2983 /* This was the first cell added to the queue. We just made this
2984 * circuit active. */
2985 log_debug(LD_GENERAL, "Made a circuit active.");
2988 /* New way: mark this as having waiting cells for the scheduler */
2989 scheduler_channel_has_waiting_cells(chan);
2992 /** Append an encoded value of <b>addr</b> to <b>payload_out</b>, which must
2993 * have at least 18 bytes of free space. The encoding is, as specified in
2994 * tor-spec.txt:
2995 * RESOLVED_TYPE_IPV4 or RESOLVED_TYPE_IPV6 [1 byte]
2996 * LENGTH [1 byte]
2997 * ADDRESS [length bytes]
2998 * Return the number of bytes added, or -1 on error */
3000 append_address_to_payload(uint8_t *payload_out, const tor_addr_t *addr)
3002 uint32_t a;
3003 switch (tor_addr_family(addr)) {
3004 case AF_INET:
3005 payload_out[0] = RESOLVED_TYPE_IPV4;
3006 payload_out[1] = 4;
3007 a = tor_addr_to_ipv4n(addr);
3008 memcpy(payload_out+2, &a, 4);
3009 return 6;
3010 case AF_INET6:
3011 payload_out[0] = RESOLVED_TYPE_IPV6;
3012 payload_out[1] = 16;
3013 memcpy(payload_out+2, tor_addr_to_in6_addr8(addr), 16);
3014 return 18;
3015 case AF_UNSPEC:
3016 default:
3017 return -1;
3021 /** Given <b>payload_len</b> bytes at <b>payload</b>, starting with an address
3022 * encoded as by append_address_to_payload(), try to decode the address into
3023 * *<b>addr_out</b>. Return the next byte in the payload after the address on
3024 * success, or NULL on failure. */
3025 const uint8_t *
3026 decode_address_from_payload(tor_addr_t *addr_out, const uint8_t *payload,
3027 int payload_len)
3029 if (payload_len < 2)
3030 return NULL;
3031 if (payload_len < 2+payload[1])
3032 return NULL;
3034 switch (payload[0]) {
3035 case RESOLVED_TYPE_IPV4:
3036 if (payload[1] != 4)
3037 return NULL;
3038 tor_addr_from_ipv4n(addr_out, get_uint32(payload+2));
3039 break;
3040 case RESOLVED_TYPE_IPV6:
3041 if (payload[1] != 16)
3042 return NULL;
3043 tor_addr_from_ipv6_bytes(addr_out, (char*)(payload+2));
3044 break;
3045 default:
3046 tor_addr_make_unspec(addr_out);
3047 break;
3049 return payload + 2 + payload[1];
3052 /** Remove all the cells queued on <b>circ</b> for <b>chan</b>. */
3053 void
3054 circuit_clear_cell_queue(circuit_t *circ, channel_t *chan)
3056 cell_queue_t *queue;
3057 cell_direction_t direction;
3059 if (circ->n_chan == chan) {
3060 queue = &circ->n_chan_cells;
3061 direction = CELL_DIRECTION_OUT;
3062 } else {
3063 or_circuit_t *orcirc = TO_OR_CIRCUIT(circ);
3064 tor_assert(orcirc->p_chan == chan);
3065 queue = &orcirc->p_chan_cells;
3066 direction = CELL_DIRECTION_IN;
3069 /* Clear the queue */
3070 cell_queue_clear(queue);
3072 /* Update the cell counter in the cmux */
3073 if (chan->cmux && circuitmux_is_circuit_attached(chan->cmux, circ))
3074 update_circuit_on_cmux(circ, direction);
3077 /** Fail with an assert if the circuit mux on chan is corrupt
3079 void
3080 assert_circuit_mux_okay(channel_t *chan)
3082 tor_assert(chan);
3083 tor_assert(chan->cmux);
3085 circuitmux_assert_okay(chan->cmux);
3088 /** Return 1 if we shouldn't restart reading on this circuit, even if
3089 * we get a SENDME. Else return 0.
3091 static int
3092 circuit_queue_streams_are_blocked(circuit_t *circ)
3094 if (CIRCUIT_IS_ORIGIN(circ)) {
3095 return circ->streams_blocked_on_n_chan;
3096 } else {
3097 return circ->streams_blocked_on_p_chan;