Reduce cell statistics accuracy from 1 ms to 10 ms.
[tor/rransom.git] / src / or / relay.c
blob929b2e7707d6525d3264ec4df56fa3815df0c436
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-2009, 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.
11 **/
13 #include "or.h"
14 #include "mempool.h"
16 static int relay_crypt(circuit_t *circ, cell_t *cell,
17 cell_direction_t cell_direction,
18 crypt_path_t **layer_hint, char *recognized);
19 static edge_connection_t *relay_lookup_conn(circuit_t *circ, cell_t *cell,
20 cell_direction_t cell_direction,
21 crypt_path_t *layer_hint);
23 static int
24 connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ,
25 edge_connection_t *conn,
26 crypt_path_t *layer_hint);
27 static void
28 circuit_consider_sending_sendme(circuit_t *circ, crypt_path_t *layer_hint);
29 static void
30 circuit_resume_edge_reading(circuit_t *circ, crypt_path_t *layer_hint);
31 static int
32 circuit_resume_edge_reading_helper(edge_connection_t *conn,
33 circuit_t *circ,
34 crypt_path_t *layer_hint);
35 static int
36 circuit_consider_stop_edge_reading(circuit_t *circ, crypt_path_t *layer_hint);
38 /** Stats: how many relay cells have originated at this hop, or have
39 * been relayed onward (not recognized at this hop)?
41 uint64_t stats_n_relay_cells_relayed = 0;
42 /** Stats: how many relay cells have been delivered to streams at this
43 * hop?
45 uint64_t stats_n_relay_cells_delivered = 0;
47 /** Update digest from the payload of cell. Assign integrity part to
48 * cell.
50 static void
51 relay_set_digest(crypto_digest_env_t *digest, cell_t *cell)
53 char integrity[4];
54 relay_header_t rh;
56 crypto_digest_add_bytes(digest, cell->payload, CELL_PAYLOAD_SIZE);
57 crypto_digest_get_digest(digest, integrity, 4);
58 // log_fn(LOG_DEBUG,"Putting digest of %u %u %u %u into relay cell.",
59 // integrity[0], integrity[1], integrity[2], integrity[3]);
60 relay_header_unpack(&rh, cell->payload);
61 memcpy(rh.integrity, integrity, 4);
62 relay_header_pack(cell->payload, &rh);
65 /** Does the digest for this circuit indicate that this cell is for us?
67 * Update digest from the payload of cell (with the integrity part set
68 * to 0). If the integrity part is valid, return 1, else restore digest
69 * and cell to their original state and return 0.
71 static int
72 relay_digest_matches(crypto_digest_env_t *digest, cell_t *cell)
74 char received_integrity[4], calculated_integrity[4];
75 relay_header_t rh;
76 crypto_digest_env_t *backup_digest=NULL;
78 backup_digest = crypto_digest_dup(digest);
80 relay_header_unpack(&rh, cell->payload);
81 memcpy(received_integrity, rh.integrity, 4);
82 memset(rh.integrity, 0, 4);
83 relay_header_pack(cell->payload, &rh);
85 // log_fn(LOG_DEBUG,"Reading digest of %u %u %u %u from relay cell.",
86 // received_integrity[0], received_integrity[1],
87 // received_integrity[2], received_integrity[3]);
89 crypto_digest_add_bytes(digest, cell->payload, CELL_PAYLOAD_SIZE);
90 crypto_digest_get_digest(digest, calculated_integrity, 4);
92 if (memcmp(received_integrity, calculated_integrity, 4)) {
93 // log_fn(LOG_INFO,"Recognized=0 but bad digest. Not recognizing.");
94 // (%d vs %d).", received_integrity, calculated_integrity);
95 /* restore digest to its old form */
96 crypto_digest_assign(digest, backup_digest);
97 /* restore the relay header */
98 memcpy(rh.integrity, received_integrity, 4);
99 relay_header_pack(cell->payload, &rh);
100 crypto_free_digest_env(backup_digest);
101 return 0;
103 crypto_free_digest_env(backup_digest);
104 return 1;
107 /** Apply <b>cipher</b> to CELL_PAYLOAD_SIZE bytes of <b>in</b>
108 * (in place).
110 * If <b>encrypt_mode</b> is 1 then encrypt, else decrypt.
112 * Return -1 if the crypto fails, else return 0.
114 static int
115 relay_crypt_one_payload(crypto_cipher_env_t *cipher, char *in,
116 int encrypt_mode)
118 int r;
119 (void)encrypt_mode;
120 r = crypto_cipher_crypt_inplace(cipher, in, CELL_PAYLOAD_SIZE);
122 if (r) {
123 log_warn(LD_BUG,"Error during relay encryption");
124 return -1;
126 return 0;
129 /** Receive a relay cell:
130 * - Crypt it (encrypt if headed toward the origin or if we <b>are</b> the
131 * origin; decrypt if we're headed toward the exit).
132 * - Check if recognized (if exitward).
133 * - If recognized and the digest checks out, then find if there's a stream
134 * that the cell is intended for, and deliver it to the right
135 * connection_edge.
136 * - If not recognized, then we need to relay it: append it to the appropriate
137 * cell_queue on <b>circ</b>.
139 * Return -<b>reason</b> on failure.
142 circuit_receive_relay_cell(cell_t *cell, circuit_t *circ,
143 cell_direction_t cell_direction)
145 or_connection_t *or_conn=NULL;
146 crypt_path_t *layer_hint=NULL;
147 char recognized=0;
148 int reason;
150 tor_assert(cell);
151 tor_assert(circ);
152 tor_assert(cell_direction == CELL_DIRECTION_OUT ||
153 cell_direction == CELL_DIRECTION_IN);
154 if (circ->marked_for_close)
155 return 0;
157 if (relay_crypt(circ, cell, cell_direction, &layer_hint, &recognized) < 0) {
158 log_warn(LD_BUG,"relay crypt failed. Dropping connection.");
159 return -END_CIRC_REASON_INTERNAL;
162 if (recognized) {
163 edge_connection_t *conn = relay_lookup_conn(circ, cell, cell_direction,
164 layer_hint);
165 if (cell_direction == CELL_DIRECTION_OUT) {
166 ++stats_n_relay_cells_delivered;
167 log_debug(LD_OR,"Sending away from origin.");
168 if ((reason=connection_edge_process_relay_cell(cell, circ, conn, NULL))
169 < 0) {
170 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
171 "connection_edge_process_relay_cell (away from origin) "
172 "failed.");
173 return reason;
176 if (cell_direction == CELL_DIRECTION_IN) {
177 ++stats_n_relay_cells_delivered;
178 log_debug(LD_OR,"Sending to origin.");
179 if ((reason = connection_edge_process_relay_cell(cell, circ, conn,
180 layer_hint)) < 0) {
181 log_warn(LD_OR,
182 "connection_edge_process_relay_cell (at origin) failed.");
183 return reason;
186 return 0;
189 /* not recognized. pass it on. */
190 if (cell_direction == CELL_DIRECTION_OUT) {
191 cell->circ_id = circ->n_circ_id; /* switch it */
192 or_conn = circ->n_conn;
193 } else if (! CIRCUIT_IS_ORIGIN(circ)) {
194 cell->circ_id = TO_OR_CIRCUIT(circ)->p_circ_id; /* switch it */
195 or_conn = TO_OR_CIRCUIT(circ)->p_conn;
196 } else {
197 log_fn(LOG_PROTOCOL_WARN, LD_OR,
198 "Dropping unrecognized inbound cell on origin circuit.");
199 return 0;
202 if (!or_conn) {
203 // XXXX Can this splice stuff be done more cleanly?
204 if (! CIRCUIT_IS_ORIGIN(circ) &&
205 TO_OR_CIRCUIT(circ)->rend_splice &&
206 cell_direction == CELL_DIRECTION_OUT) {
207 or_circuit_t *splice = TO_OR_CIRCUIT(circ)->rend_splice;
208 tor_assert(circ->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED);
209 tor_assert(splice->_base.purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED);
210 cell->circ_id = splice->p_circ_id;
211 cell->command = CELL_RELAY; /* can't be relay_early anyway */
212 if ((reason = circuit_receive_relay_cell(cell, TO_CIRCUIT(splice),
213 CELL_DIRECTION_IN)) < 0) {
214 log_warn(LD_REND, "Error relaying cell across rendezvous; closing "
215 "circuits");
216 /* XXXX Do this here, or just return -1? */
217 circuit_mark_for_close(circ, -reason);
218 return reason;
220 return 0;
222 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
223 "Didn't recognize cell, but circ stops here! Closing circ.");
224 return -END_CIRC_REASON_TORPROTOCOL;
227 log_debug(LD_OR,"Passing on unrecognized cell.");
229 ++stats_n_relay_cells_relayed; /* XXXX no longer quite accurate {cells}
230 * we might kill the circ before we relay
231 * the cells. */
233 append_cell_to_circuit_queue(circ, or_conn, cell, cell_direction);
234 return 0;
237 /** Do the appropriate en/decryptions for <b>cell</b> arriving on
238 * <b>circ</b> in direction <b>cell_direction</b>.
240 * If cell_direction == CELL_DIRECTION_IN:
241 * - If we're at the origin (we're the OP), for hops 1..N,
242 * decrypt cell. If recognized, stop.
243 * - Else (we're not the OP), encrypt one hop. Cell is not recognized.
245 * If cell_direction == CELL_DIRECTION_OUT:
246 * - decrypt one hop. Check if recognized.
248 * If cell is recognized, set *recognized to 1, and set
249 * *layer_hint to the hop that recognized it.
251 * Return -1 to indicate that we should mark the circuit for close,
252 * else return 0.
254 static int
255 relay_crypt(circuit_t *circ, cell_t *cell, cell_direction_t cell_direction,
256 crypt_path_t **layer_hint, char *recognized)
258 relay_header_t rh;
260 tor_assert(circ);
261 tor_assert(cell);
262 tor_assert(recognized);
263 tor_assert(cell_direction == CELL_DIRECTION_IN ||
264 cell_direction == CELL_DIRECTION_OUT);
266 if (cell_direction == CELL_DIRECTION_IN) {
267 if (CIRCUIT_IS_ORIGIN(circ)) { /* We're at the beginning of the circuit.
268 * We'll want to do layered decrypts. */
269 crypt_path_t *thishop, *cpath = TO_ORIGIN_CIRCUIT(circ)->cpath;
270 thishop = cpath;
271 if (thishop->state != CPATH_STATE_OPEN) {
272 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
273 "Relay cell before first created cell? Closing.");
274 return -1;
276 do { /* Remember: cpath is in forward order, that is, first hop first. */
277 tor_assert(thishop);
279 if (relay_crypt_one_payload(thishop->b_crypto, cell->payload, 0) < 0)
280 return -1;
282 relay_header_unpack(&rh, cell->payload);
283 if (rh.recognized == 0) {
284 /* it's possibly recognized. have to check digest to be sure. */
285 if (relay_digest_matches(thishop->b_digest, cell)) {
286 *recognized = 1;
287 *layer_hint = thishop;
288 return 0;
292 thishop = thishop->next;
293 } while (thishop != cpath && thishop->state == CPATH_STATE_OPEN);
294 log_fn(LOG_PROTOCOL_WARN, LD_OR,
295 "Incoming cell at client not recognized. Closing.");
296 return -1;
297 } else { /* we're in the middle. Just one crypt. */
298 if (relay_crypt_one_payload(TO_OR_CIRCUIT(circ)->p_crypto,
299 cell->payload, 1) < 0)
300 return -1;
301 // log_fn(LOG_DEBUG,"Skipping recognized check, because we're not "
302 // "the client.");
304 } else /* cell_direction == CELL_DIRECTION_OUT */ {
305 /* we're in the middle. Just one crypt. */
307 if (relay_crypt_one_payload(TO_OR_CIRCUIT(circ)->n_crypto,
308 cell->payload, 0) < 0)
309 return -1;
311 relay_header_unpack(&rh, cell->payload);
312 if (rh.recognized == 0) {
313 /* it's possibly recognized. have to check digest to be sure. */
314 if (relay_digest_matches(TO_OR_CIRCUIT(circ)->n_digest, cell)) {
315 *recognized = 1;
316 return 0;
320 return 0;
323 /** Package a relay cell from an edge:
324 * - Encrypt it to the right layer
325 * - Append it to the appropriate cell_queue on <b>circ</b>.
327 static int
328 circuit_package_relay_cell(cell_t *cell, circuit_t *circ,
329 cell_direction_t cell_direction,
330 crypt_path_t *layer_hint)
332 or_connection_t *conn; /* where to send the cell */
334 if (cell_direction == CELL_DIRECTION_OUT) {
335 crypt_path_t *thishop; /* counter for repeated crypts */
336 conn = circ->n_conn;
337 if (!CIRCUIT_IS_ORIGIN(circ) || !conn) {
338 log_warn(LD_BUG,"outgoing relay cell has n_conn==NULL. Dropping.");
339 return 0; /* just drop it */
342 relay_set_digest(layer_hint->f_digest, cell);
344 thishop = layer_hint;
345 /* moving from farthest to nearest hop */
346 do {
347 tor_assert(thishop);
348 /* XXXX RD This is a bug, right? */
349 log_debug(LD_OR,"crypting a layer of the relay cell.");
350 if (relay_crypt_one_payload(thishop->f_crypto, cell->payload, 1) < 0) {
351 return -1;
354 thishop = thishop->prev;
355 } while (thishop != TO_ORIGIN_CIRCUIT(circ)->cpath->prev);
357 } else { /* incoming cell */
358 or_circuit_t *or_circ;
359 if (CIRCUIT_IS_ORIGIN(circ)) {
360 /* We should never package an _incoming_ cell from the circuit
361 * origin; that means we messed up somewhere. */
362 log_warn(LD_BUG,"incoming relay cell at origin circuit. Dropping.");
363 assert_circuit_ok(circ);
364 return 0; /* just drop it */
366 or_circ = TO_OR_CIRCUIT(circ);
367 conn = or_circ->p_conn;
368 relay_set_digest(or_circ->p_digest, cell);
369 if (relay_crypt_one_payload(or_circ->p_crypto, cell->payload, 1) < 0)
370 return -1;
372 ++stats_n_relay_cells_relayed;
374 append_cell_to_circuit_queue(circ, conn, cell, cell_direction);
375 return 0;
378 /** If cell's stream_id matches the stream_id of any conn that's
379 * attached to circ, return that conn, else return NULL.
381 static edge_connection_t *
382 relay_lookup_conn(circuit_t *circ, cell_t *cell,
383 cell_direction_t cell_direction, crypt_path_t *layer_hint)
385 edge_connection_t *tmpconn;
386 relay_header_t rh;
388 relay_header_unpack(&rh, cell->payload);
390 if (!rh.stream_id)
391 return NULL;
393 /* IN or OUT cells could have come from either direction, now
394 * that we allow rendezvous *to* an OP.
397 if (CIRCUIT_IS_ORIGIN(circ)) {
398 for (tmpconn = TO_ORIGIN_CIRCUIT(circ)->p_streams; tmpconn;
399 tmpconn=tmpconn->next_stream) {
400 if (rh.stream_id == tmpconn->stream_id &&
401 !tmpconn->_base.marked_for_close &&
402 tmpconn->cpath_layer == layer_hint) {
403 log_debug(LD_APP,"found conn for stream %d.", rh.stream_id);
404 return tmpconn;
407 } else {
408 for (tmpconn = TO_OR_CIRCUIT(circ)->n_streams; tmpconn;
409 tmpconn=tmpconn->next_stream) {
410 if (rh.stream_id == tmpconn->stream_id &&
411 !tmpconn->_base.marked_for_close) {
412 log_debug(LD_EXIT,"found conn for stream %d.", rh.stream_id);
413 if (cell_direction == CELL_DIRECTION_OUT ||
414 connection_edge_is_rendezvous_stream(tmpconn))
415 return tmpconn;
418 for (tmpconn = TO_OR_CIRCUIT(circ)->resolving_streams; tmpconn;
419 tmpconn=tmpconn->next_stream) {
420 if (rh.stream_id == tmpconn->stream_id &&
421 !tmpconn->_base.marked_for_close) {
422 log_debug(LD_EXIT,"found conn for stream %d.", rh.stream_id);
423 return tmpconn;
427 return NULL; /* probably a begin relay cell */
430 /** Pack the relay_header_t host-order structure <b>src</b> into
431 * network-order in the buffer <b>dest</b>. See tor-spec.txt for details
432 * about the wire format.
434 void
435 relay_header_pack(char *dest, const relay_header_t *src)
437 *(uint8_t*)(dest) = src->command;
439 set_uint16(dest+1, htons(src->recognized));
440 set_uint16(dest+3, htons(src->stream_id));
441 memcpy(dest+5, src->integrity, 4);
442 set_uint16(dest+9, htons(src->length));
445 /** Unpack the network-order buffer <b>src</b> into a host-order
446 * relay_header_t structure <b>dest</b>.
448 void
449 relay_header_unpack(relay_header_t *dest, const char *src)
451 dest->command = *(uint8_t*)(src);
453 dest->recognized = ntohs(get_uint16(src+1));
454 dest->stream_id = ntohs(get_uint16(src+3));
455 memcpy(dest->integrity, src+5, 4);
456 dest->length = ntohs(get_uint16(src+9));
459 /** Convert the relay <b>command</b> into a human-readable string. */
460 static const char *
461 relay_command_to_string(uint8_t command)
463 switch (command) {
464 case RELAY_COMMAND_BEGIN: return "BEGIN";
465 case RELAY_COMMAND_DATA: return "DATA";
466 case RELAY_COMMAND_END: return "END";
467 case RELAY_COMMAND_CONNECTED: return "CONNECTED";
468 case RELAY_COMMAND_SENDME: return "SENDME";
469 case RELAY_COMMAND_EXTEND: return "EXTEND";
470 case RELAY_COMMAND_EXTENDED: return "EXTENDED";
471 case RELAY_COMMAND_TRUNCATE: return "TRUNCATE";
472 case RELAY_COMMAND_TRUNCATED: return "TRUNCATED";
473 case RELAY_COMMAND_DROP: return "DROP";
474 case RELAY_COMMAND_RESOLVE: return "RESOLVE";
475 case RELAY_COMMAND_RESOLVED: return "RESOLVED";
476 case RELAY_COMMAND_BEGIN_DIR: return "BEGIN_DIR";
477 case RELAY_COMMAND_ESTABLISH_INTRO: return "ESTABLISH_INTRO";
478 case RELAY_COMMAND_ESTABLISH_RENDEZVOUS: return "ESTABLISH_RENDEZVOUS";
479 case RELAY_COMMAND_INTRODUCE1: return "INTRODUCE1";
480 case RELAY_COMMAND_INTRODUCE2: return "INTRODUCE2";
481 case RELAY_COMMAND_RENDEZVOUS1: return "RENDEZVOUS1";
482 case RELAY_COMMAND_RENDEZVOUS2: return "RENDEZVOUS2";
483 case RELAY_COMMAND_INTRO_ESTABLISHED: return "INTRO_ESTABLISHED";
484 case RELAY_COMMAND_RENDEZVOUS_ESTABLISHED:
485 return "RENDEZVOUS_ESTABLISHED";
486 case RELAY_COMMAND_INTRODUCE_ACK: return "INTRODUCE_ACK";
487 default: return "(unrecognized)";
491 /** Make a relay cell out of <b>relay_command</b> and <b>payload</b>, and send
492 * it onto the open circuit <b>circ</b>. <b>stream_id</b> is the ID on
493 * <b>circ</b> for the stream that's sending the relay cell, or 0 if it's a
494 * control cell. <b>cpath_layer</b> is NULL for OR->OP cells, or the
495 * destination hop for OP->OR cells.
497 * If you can't send the cell, mark the circuit for close and return -1. Else
498 * return 0.
501 relay_send_command_from_edge(uint16_t stream_id, circuit_t *circ,
502 uint8_t relay_command, const char *payload,
503 size_t payload_len, crypt_path_t *cpath_layer)
505 cell_t cell;
506 relay_header_t rh;
507 cell_direction_t cell_direction;
508 /* XXXX NM Split this function into a separate versions per circuit type? */
510 tor_assert(circ);
511 tor_assert(payload_len <= RELAY_PAYLOAD_SIZE);
513 memset(&cell, 0, sizeof(cell_t));
514 cell.command = CELL_RELAY;
515 if (cpath_layer) {
516 cell.circ_id = circ->n_circ_id;
517 cell_direction = CELL_DIRECTION_OUT;
518 } else if (! CIRCUIT_IS_ORIGIN(circ)) {
519 cell.circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
520 cell_direction = CELL_DIRECTION_IN;
521 } else {
522 return -1;
525 memset(&rh, 0, sizeof(rh));
526 rh.command = relay_command;
527 rh.stream_id = stream_id;
528 rh.length = payload_len;
529 relay_header_pack(cell.payload, &rh);
530 if (payload_len)
531 memcpy(cell.payload+RELAY_HEADER_SIZE, payload, payload_len);
533 log_debug(LD_OR,"delivering %d cell %s.", relay_command,
534 cell_direction == CELL_DIRECTION_OUT ? "forward" : "backward");
536 /* If we are sending an END cell and this circuit is used for a tunneled
537 * directory request, advance its state. */
538 if (relay_command == RELAY_COMMAND_END && circ->dirreq_id)
539 geoip_change_dirreq_state(circ->dirreq_id, DIRREQ_TUNNELED,
540 DIRREQ_END_CELL_SENT);
542 if (cell_direction == CELL_DIRECTION_OUT && circ->n_conn) {
543 /* if we're using relaybandwidthrate, this conn wants priority */
544 circ->n_conn->client_used = approx_time();
547 if (cell_direction == CELL_DIRECTION_OUT) {
548 origin_circuit_t *origin_circ = TO_ORIGIN_CIRCUIT(circ);
549 if (origin_circ->remaining_relay_early_cells > 0 &&
550 (relay_command == RELAY_COMMAND_EXTEND ||
551 (cpath_layer != origin_circ->cpath &&
552 !CIRCUIT_PURPOSE_IS_ESTABLISHED_REND(circ->purpose)))) {
553 /* If we've got any relay_early cells left, and we're sending
554 * an extend cell or (we're not talking to the first hop and we're
555 * not talking to a rendezvous circuit), use one of them.
556 * Don't worry about the conn protocol version:
557 * append_cell_to_circuit_queue will fix it up. */
558 /* XXX For now, clients don't use RELAY_EARLY cells when sending
559 * relay cells on rendezvous circuits. See bug 1038. Eventually,
560 * we can take this behavior away in favor of having clients avoid
561 * rendezvous points running 0.2.1.3-alpha through 0.2.1.18. -RD */
562 cell.command = CELL_RELAY_EARLY;
563 --origin_circ->remaining_relay_early_cells;
564 log_debug(LD_OR, "Sending a RELAY_EARLY cell; %d remaining.",
565 (int)origin_circ->remaining_relay_early_cells);
566 /* Memorize the command that is sent as RELAY_EARLY cell; helps debug
567 * task 878. */
568 origin_circ->relay_early_commands[
569 origin_circ->relay_early_cells_sent++] = relay_command;
570 } else if (relay_command == RELAY_COMMAND_EXTEND) {
571 /* If no RELAY_EARLY cells can be sent over this circuit, log which
572 * commands have been sent as RELAY_EARLY cells before; helps debug
573 * task 878. */
574 smartlist_t *commands_list = smartlist_create();
575 int i = 0;
576 char *commands = NULL;
577 for (; i < origin_circ->relay_early_cells_sent; i++)
578 smartlist_add(commands_list, (char *)
579 relay_command_to_string(origin_circ->relay_early_commands[i]));
580 commands = smartlist_join_strings(commands_list, ",", 0, NULL);
581 log_warn(LD_BUG, "Uh-oh. We're sending a RELAY_COMMAND_EXTEND cell, "
582 "but we have run out of RELAY_EARLY cells on that circuit. "
583 "Commands sent before: %s", commands);
584 tor_free(commands);
585 smartlist_free(commands_list);
589 if (circuit_package_relay_cell(&cell, circ, cell_direction, cpath_layer)
590 < 0) {
591 log_warn(LD_BUG,"circuit_package_relay_cell failed. Closing.");
592 circuit_mark_for_close(circ, END_CIRC_REASON_INTERNAL);
593 return -1;
595 return 0;
598 /** Make a relay cell out of <b>relay_command</b> and <b>payload</b>, and
599 * send it onto the open circuit <b>circ</b>. <b>fromconn</b> is the stream
600 * that's sending the relay cell, or NULL if it's a control cell.
601 * <b>cpath_layer</b> is NULL for OR->OP cells, or the destination hop
602 * for OP->OR cells.
604 * If you can't send the cell, mark the circuit for close and
605 * return -1. Else return 0.
608 connection_edge_send_command(edge_connection_t *fromconn,
609 uint8_t relay_command, const char *payload,
610 size_t payload_len)
612 /* XXXX NM Split this function into a separate versions per circuit type? */
613 circuit_t *circ;
614 tor_assert(fromconn);
615 circ = fromconn->on_circuit;
617 if (fromconn->_base.marked_for_close) {
618 log_warn(LD_BUG,
619 "called on conn that's already marked for close at %s:%d.",
620 fromconn->_base.marked_for_close_file,
621 fromconn->_base.marked_for_close);
622 return 0;
625 if (!circ) {
626 if (fromconn->_base.type == CONN_TYPE_AP) {
627 log_info(LD_APP,"no circ. Closing conn.");
628 connection_mark_unattached_ap(fromconn, END_STREAM_REASON_INTERNAL);
629 } else {
630 log_info(LD_EXIT,"no circ. Closing conn.");
631 fromconn->edge_has_sent_end = 1; /* no circ to send to */
632 fromconn->end_reason = END_STREAM_REASON_INTERNAL;
633 connection_mark_for_close(TO_CONN(fromconn));
635 return -1;
638 return relay_send_command_from_edge(fromconn->stream_id, circ,
639 relay_command, payload,
640 payload_len, fromconn->cpath_layer);
643 /** How many times will I retry a stream that fails due to DNS
644 * resolve failure or misc error?
646 #define MAX_RESOLVE_FAILURES 3
648 /** Return 1 if reason is something that you should retry if you
649 * get the end cell before you've connected; else return 0. */
650 static int
651 edge_reason_is_retriable(int reason)
653 return reason == END_STREAM_REASON_HIBERNATING ||
654 reason == END_STREAM_REASON_RESOURCELIMIT ||
655 reason == END_STREAM_REASON_EXITPOLICY ||
656 reason == END_STREAM_REASON_RESOLVEFAILED ||
657 reason == END_STREAM_REASON_MISC;
660 /** Called when we receive an END cell on a stream that isn't open yet,
661 * from the client side.
662 * Arguments are as for connection_edge_process_relay_cell().
664 static int
665 connection_ap_process_end_not_open(
666 relay_header_t *rh, cell_t *cell, origin_circuit_t *circ,
667 edge_connection_t *conn, crypt_path_t *layer_hint)
669 struct in_addr in;
670 routerinfo_t *exitrouter;
671 int reason = *(cell->payload+RELAY_HEADER_SIZE);
672 int control_reason = reason | END_STREAM_REASON_FLAG_REMOTE;
673 (void) layer_hint; /* unused */
675 if (rh->length > 0 && edge_reason_is_retriable(reason) &&
676 !connection_edge_is_rendezvous_stream(conn) /* avoid retry if rend */
678 log_info(LD_APP,"Address '%s' refused due to '%s'. Considering retrying.",
679 safe_str(conn->socks_request->address),
680 stream_end_reason_to_string(reason));
681 exitrouter =
682 router_get_by_digest(circ->build_state->chosen_exit->identity_digest);
683 switch (reason) {
684 case END_STREAM_REASON_EXITPOLICY:
685 if (rh->length >= 5) {
686 uint32_t addr = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+1));
687 int ttl;
688 if (!addr) {
689 log_info(LD_APP,"Address '%s' resolved to 0.0.0.0. Closing,",
690 safe_str(conn->socks_request->address));
691 connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
692 return 0;
694 if (rh->length >= 9)
695 ttl = (int)ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+5));
696 else
697 ttl = -1;
699 if (get_options()->ClientDNSRejectInternalAddresses &&
700 is_internal_IP(addr, 0)) {
701 log_info(LD_APP,"Address '%s' resolved to internal. Closing,",
702 safe_str(conn->socks_request->address));
703 connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
704 return 0;
706 client_dns_set_addressmap(conn->socks_request->address, addr,
707 conn->chosen_exit_name, ttl);
709 /* check if he *ought* to have allowed it */
710 if (exitrouter &&
711 (rh->length < 5 ||
712 (tor_inet_aton(conn->socks_request->address, &in) &&
713 !conn->chosen_exit_name))) {
714 log_info(LD_APP,
715 "Exitrouter '%s' seems to be more restrictive than its exit "
716 "policy. Not using this router as exit for now.",
717 exitrouter->nickname);
718 policies_set_router_exitpolicy_to_reject_all(exitrouter);
720 /* rewrite it to an IP if we learned one. */
721 if (addressmap_rewrite(conn->socks_request->address,
722 sizeof(conn->socks_request->address),
723 NULL)) {
724 control_event_stream_status(conn, STREAM_EVENT_REMAP, 0);
726 if (conn->chosen_exit_optional ||
727 conn->chosen_exit_retries) {
728 /* stop wanting a specific exit */
729 conn->chosen_exit_optional = 0;
730 /* A non-zero chosen_exit_retries can happen if we set a
731 * TrackHostExits for this address under a port that the exit
732 * relay allows, but then try the same address with a different
733 * port that it doesn't allow to exit. We shouldn't unregister
734 * the mapping, since it is probably still wanted on the
735 * original port. But now we give away to the exit relay that
736 * we probably have a TrackHostExits on it. So be it. */
737 conn->chosen_exit_retries = 0;
738 tor_free(conn->chosen_exit_name); /* clears it */
740 if (connection_ap_detach_retriable(conn, circ, control_reason) >= 0)
741 return 0;
742 /* else, conn will get closed below */
743 break;
744 case END_STREAM_REASON_CONNECTREFUSED:
745 if (!conn->chosen_exit_optional)
746 break; /* break means it'll close, below */
747 /* Else fall through: expire this circuit, clear the
748 * chosen_exit_name field, and try again. */
749 case END_STREAM_REASON_RESOLVEFAILED:
750 case END_STREAM_REASON_TIMEOUT:
751 case END_STREAM_REASON_MISC:
752 if (client_dns_incr_failures(conn->socks_request->address)
753 < MAX_RESOLVE_FAILURES) {
754 /* We haven't retried too many times; reattach the connection. */
755 circuit_log_path(LOG_INFO,LD_APP,circ);
756 tor_assert(circ->_base.timestamp_dirty);
757 circ->_base.timestamp_dirty -= get_options()->MaxCircuitDirtiness;
759 if (conn->chosen_exit_optional) {
760 /* stop wanting a specific exit */
761 conn->chosen_exit_optional = 0;
762 tor_free(conn->chosen_exit_name); /* clears it */
764 if (connection_ap_detach_retriable(conn, circ, control_reason) >= 0)
765 return 0;
766 /* else, conn will get closed below */
767 } else {
768 log_notice(LD_APP,
769 "Have tried resolving or connecting to address '%s' "
770 "at %d different places. Giving up.",
771 safe_str(conn->socks_request->address),
772 MAX_RESOLVE_FAILURES);
773 /* clear the failures, so it will have a full try next time */
774 client_dns_clear_failures(conn->socks_request->address);
776 break;
777 case END_STREAM_REASON_HIBERNATING:
778 case END_STREAM_REASON_RESOURCELIMIT:
779 if (exitrouter) {
780 policies_set_router_exitpolicy_to_reject_all(exitrouter);
782 if (conn->chosen_exit_optional) {
783 /* stop wanting a specific exit */
784 conn->chosen_exit_optional = 0;
785 tor_free(conn->chosen_exit_name); /* clears it */
787 if (connection_ap_detach_retriable(conn, circ, control_reason) >= 0)
788 return 0;
789 /* else, will close below */
790 break;
791 } /* end switch */
792 log_info(LD_APP,"Giving up on retrying; conn can't be handled.");
795 log_info(LD_APP,
796 "Edge got end (%s) before we're connected. Marking for close.",
797 stream_end_reason_to_string(rh->length > 0 ? reason : -1));
798 circuit_log_path(LOG_INFO,LD_APP,circ);
799 /* need to test because of detach_retriable */
800 if (!conn->_base.marked_for_close)
801 connection_mark_unattached_ap(conn, control_reason);
802 return 0;
805 /** Helper: change the socks_request-&gt;address field on conn to the
806 * dotted-quad representation of <b>new_addr</b> (given in host order),
807 * and send an appropriate REMAP event. */
808 static void
809 remap_event_helper(edge_connection_t *conn, uint32_t new_addr)
811 struct in_addr in;
813 in.s_addr = htonl(new_addr);
814 tor_inet_ntoa(&in, conn->socks_request->address,
815 sizeof(conn->socks_request->address));
816 control_event_stream_status(conn, STREAM_EVENT_REMAP,
817 REMAP_STREAM_SOURCE_EXIT);
820 /** An incoming relay cell has arrived from circuit <b>circ</b> to
821 * stream <b>conn</b>.
823 * The arguments here are the same as in
824 * connection_edge_process_relay_cell() below; this function is called
825 * from there when <b>conn</b> is defined and not in an open state.
827 static int
828 connection_edge_process_relay_cell_not_open(
829 relay_header_t *rh, cell_t *cell, circuit_t *circ,
830 edge_connection_t *conn, crypt_path_t *layer_hint)
832 if (rh->command == RELAY_COMMAND_END) {
833 if (CIRCUIT_IS_ORIGIN(circ) && conn->_base.type == CONN_TYPE_AP) {
834 return connection_ap_process_end_not_open(rh, cell,
835 TO_ORIGIN_CIRCUIT(circ), conn,
836 layer_hint);
837 } else {
838 /* we just got an 'end', don't need to send one */
839 conn->edge_has_sent_end = 1;
840 conn->end_reason = *(cell->payload+RELAY_HEADER_SIZE) |
841 END_STREAM_REASON_FLAG_REMOTE;
842 connection_mark_for_close(TO_CONN(conn));
843 return 0;
847 if (conn->_base.type == CONN_TYPE_AP &&
848 rh->command == RELAY_COMMAND_CONNECTED) {
849 tor_assert(CIRCUIT_IS_ORIGIN(circ));
850 if (conn->_base.state != AP_CONN_STATE_CONNECT_WAIT) {
851 log_fn(LOG_PROTOCOL_WARN, LD_APP,
852 "Got 'connected' while not in state connect_wait. Dropping.");
853 return 0;
855 conn->_base.state = AP_CONN_STATE_OPEN;
856 log_info(LD_APP,"'connected' received after %d seconds.",
857 (int)(time(NULL) - conn->_base.timestamp_lastread));
858 if (rh->length >= 4) {
859 uint32_t addr = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE));
860 int ttl;
861 if (!addr || (get_options()->ClientDNSRejectInternalAddresses &&
862 is_internal_IP(addr, 0))) {
863 char buf[INET_NTOA_BUF_LEN];
864 struct in_addr a;
865 a.s_addr = htonl(addr);
866 tor_inet_ntoa(&a, buf, sizeof(buf));
867 log_info(LD_APP,
868 "...but it claims the IP address was %s. Closing.", buf);
869 connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL);
870 connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
871 return 0;
873 if (rh->length >= 8)
874 ttl = (int)ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+4));
875 else
876 ttl = -1;
877 client_dns_set_addressmap(conn->socks_request->address, addr,
878 conn->chosen_exit_name, ttl);
880 remap_event_helper(conn, addr);
882 circuit_log_path(LOG_INFO,LD_APP,TO_ORIGIN_CIRCUIT(circ));
883 /* don't send a socks reply to transparent conns */
884 if (!conn->socks_request->has_finished)
885 connection_ap_handshake_socks_reply(conn, NULL, 0, 0);
887 /* Was it a linked dir conn? If so, a dir request just started to
888 * fetch something; this could be a bootstrap status milestone. */
889 log_debug(LD_APP, "considering");
890 if (TO_CONN(conn)->linked_conn &&
891 TO_CONN(conn)->linked_conn->type == CONN_TYPE_DIR) {
892 connection_t *dirconn = TO_CONN(conn)->linked_conn;
893 log_debug(LD_APP, "it is! %d", dirconn->purpose);
894 switch (dirconn->purpose) {
895 case DIR_PURPOSE_FETCH_CERTIFICATE:
896 if (consensus_is_waiting_for_certs())
897 control_event_bootstrap(BOOTSTRAP_STATUS_LOADING_KEYS, 0);
898 break;
899 case DIR_PURPOSE_FETCH_CONSENSUS:
900 control_event_bootstrap(BOOTSTRAP_STATUS_LOADING_STATUS, 0);
901 break;
902 case DIR_PURPOSE_FETCH_SERVERDESC:
903 control_event_bootstrap(BOOTSTRAP_STATUS_LOADING_DESCRIPTORS,
904 count_loading_descriptors_progress());
905 break;
909 /* handle anything that might have queued */
910 if (connection_edge_package_raw_inbuf(conn, 1) < 0) {
911 /* (We already sent an end cell if possible) */
912 connection_mark_for_close(TO_CONN(conn));
913 return 0;
915 return 0;
917 if (conn->_base.type == CONN_TYPE_AP &&
918 rh->command == RELAY_COMMAND_RESOLVED) {
919 int ttl;
920 int answer_len;
921 uint8_t answer_type;
922 if (conn->_base.state != AP_CONN_STATE_RESOLVE_WAIT) {
923 log_fn(LOG_PROTOCOL_WARN, LD_APP, "Got a 'resolved' cell while "
924 "not in state resolve_wait. Dropping.");
925 return 0;
927 tor_assert(SOCKS_COMMAND_IS_RESOLVE(conn->socks_request->command));
928 answer_len = cell->payload[RELAY_HEADER_SIZE+1];
929 if (rh->length < 2 || answer_len+2>rh->length) {
930 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
931 "Dropping malformed 'resolved' cell");
932 connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
933 return 0;
935 answer_type = cell->payload[RELAY_HEADER_SIZE];
936 if (rh->length >= answer_len+6)
937 ttl = (int)ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+
938 2+answer_len));
939 else
940 ttl = -1;
941 if (answer_type == RESOLVED_TYPE_IPV4 && answer_len == 4) {
942 uint32_t addr = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+2));
943 if (get_options()->ClientDNSRejectInternalAddresses &&
944 is_internal_IP(addr, 0)) {
945 char buf[INET_NTOA_BUF_LEN];
946 struct in_addr a;
947 a.s_addr = htonl(addr);
948 tor_inet_ntoa(&a, buf, sizeof(buf));
949 log_info(LD_APP,"Got a resolve with answer %s. Rejecting.", buf);
950 connection_ap_handshake_socks_resolved(conn,
951 RESOLVED_TYPE_ERROR_TRANSIENT,
952 0, NULL, 0, TIME_MAX);
953 connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
954 return 0;
957 connection_ap_handshake_socks_resolved(conn,
958 answer_type,
959 cell->payload[RELAY_HEADER_SIZE+1], /*answer_len*/
960 cell->payload+RELAY_HEADER_SIZE+2, /*answer*/
961 ttl,
962 -1);
963 if (answer_type == RESOLVED_TYPE_IPV4 && answer_len == 4) {
964 uint32_t addr = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+2));
965 remap_event_helper(conn, addr);
967 connection_mark_unattached_ap(conn,
968 END_STREAM_REASON_DONE |
969 END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED);
970 return 0;
973 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
974 "Got an unexpected relay command %d, in state %d (%s). Dropping.",
975 rh->command, conn->_base.state,
976 conn_state_to_string(conn->_base.type, conn->_base.state));
977 return 0; /* for forward compatibility, don't kill the circuit */
978 // connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL);
979 // connection_mark_for_close(conn);
980 // return -1;
983 /** An incoming relay cell has arrived on circuit <b>circ</b>. If
984 * <b>conn</b> is NULL this is a control cell, else <b>cell</b> is
985 * destined for <b>conn</b>.
987 * If <b>layer_hint</b> is defined, then we're the origin of the
988 * circuit, and it specifies the hop that packaged <b>cell</b>.
990 * Return -reason if you want to warn and tear down the circuit, else 0.
992 static int
993 connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ,
994 edge_connection_t *conn,
995 crypt_path_t *layer_hint)
997 static int num_seen=0;
998 relay_header_t rh;
999 unsigned domain = layer_hint?LD_APP:LD_EXIT;
1000 int reason;
1002 tor_assert(cell);
1003 tor_assert(circ);
1005 relay_header_unpack(&rh, cell->payload);
1006 // log_fn(LOG_DEBUG,"command %d stream %d", rh.command, rh.stream_id);
1007 num_seen++;
1008 log_debug(domain, "Now seen %d relay cells here (command %d, stream %d).",
1009 num_seen, rh.command, rh.stream_id);
1011 if (rh.length > RELAY_PAYLOAD_SIZE) {
1012 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1013 "Relay cell length field too long. Closing circuit.");
1014 return - END_CIRC_REASON_TORPROTOCOL;
1017 /* either conn is NULL, in which case we've got a control cell, or else
1018 * conn points to the recognized stream. */
1020 if (conn && !connection_state_is_open(TO_CONN(conn)))
1021 return connection_edge_process_relay_cell_not_open(
1022 &rh, cell, circ, conn, layer_hint);
1024 switch (rh.command) {
1025 case RELAY_COMMAND_DROP:
1026 // log_info(domain,"Got a relay-level padding cell. Dropping.");
1027 return 0;
1028 case RELAY_COMMAND_BEGIN:
1029 case RELAY_COMMAND_BEGIN_DIR:
1030 if (layer_hint &&
1031 circ->purpose != CIRCUIT_PURPOSE_S_REND_JOINED) {
1032 log_fn(LOG_PROTOCOL_WARN, LD_APP,
1033 "Relay begin request unsupported at AP. Dropping.");
1034 return 0;
1036 if (circ->purpose == CIRCUIT_PURPOSE_S_REND_JOINED &&
1037 layer_hint != TO_ORIGIN_CIRCUIT(circ)->cpath->prev) {
1038 log_fn(LOG_PROTOCOL_WARN, LD_APP,
1039 "Relay begin request to Hidden Service "
1040 "from intermediary node. Dropping.");
1041 return 0;
1043 if (conn) {
1044 log_fn(LOG_PROTOCOL_WARN, domain,
1045 "Begin cell for known stream. Dropping.");
1046 return 0;
1048 if (rh.command == RELAY_COMMAND_BEGIN_DIR) {
1049 /* Assign this circuit and its app-ward OR connection a unique ID,
1050 * so that we can measure download times. The local edge and dir
1051 * connection will be assigned the same ID when they are created
1052 * and linked. */
1053 static uint64_t next_id = 0;
1054 circ->dirreq_id = ++next_id;
1055 TO_CONN(TO_OR_CIRCUIT(circ)->p_conn)->dirreq_id = circ->dirreq_id;
1058 return connection_exit_begin_conn(cell, circ);
1059 case RELAY_COMMAND_DATA:
1060 ++stats_n_data_cells_received;
1061 if (( layer_hint && --layer_hint->deliver_window < 0) ||
1062 (!layer_hint && --circ->deliver_window < 0)) {
1063 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1064 "(relay data) circ deliver_window below 0. Killing.");
1065 connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL);
1066 connection_mark_for_close(TO_CONN(conn));
1067 return -END_CIRC_REASON_TORPROTOCOL;
1069 log_debug(domain,"circ deliver_window now %d.", layer_hint ?
1070 layer_hint->deliver_window : circ->deliver_window);
1072 circuit_consider_sending_sendme(circ, layer_hint);
1074 if (!conn) {
1075 log_info(domain,"data cell dropped, unknown stream (streamid %d).",
1076 rh.stream_id);
1077 return 0;
1080 if (--conn->deliver_window < 0) { /* is it below 0 after decrement? */
1081 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1082 "(relay data) conn deliver_window below 0. Killing.");
1083 return -END_CIRC_REASON_TORPROTOCOL;
1086 stats_n_data_bytes_received += rh.length;
1087 connection_write_to_buf(cell->payload + RELAY_HEADER_SIZE,
1088 rh.length, TO_CONN(conn));
1089 connection_edge_consider_sending_sendme(conn);
1090 return 0;
1091 case RELAY_COMMAND_END:
1092 reason = rh.length > 0 ?
1093 *(uint8_t *)(cell->payload+RELAY_HEADER_SIZE) : END_STREAM_REASON_MISC;
1094 if (!conn) {
1095 log_info(domain,"end cell (%s) dropped, unknown stream.",
1096 stream_end_reason_to_string(reason));
1097 return 0;
1099 /* XXX add to this log_fn the exit node's nickname? */
1100 log_info(domain,"%d: end cell (%s) for stream %d. Removing stream.",
1101 conn->_base.s,
1102 stream_end_reason_to_string(reason),
1103 conn->stream_id);
1104 if (conn->socks_request && !conn->socks_request->has_finished)
1105 log_warn(LD_BUG,
1106 "open stream hasn't sent socks answer yet? Closing.");
1107 /* We just *got* an end; no reason to send one. */
1108 conn->edge_has_sent_end = 1;
1109 if (!conn->end_reason)
1110 conn->end_reason = reason | END_STREAM_REASON_FLAG_REMOTE;
1111 if (!conn->_base.marked_for_close) {
1112 /* only mark it if not already marked. it's possible to
1113 * get the 'end' right around when the client hangs up on us. */
1114 connection_mark_for_close(TO_CONN(conn));
1115 conn->_base.hold_open_until_flushed = 1;
1117 return 0;
1118 case RELAY_COMMAND_EXTEND:
1119 if (conn) {
1120 log_fn(LOG_PROTOCOL_WARN, domain,
1121 "'extend' cell received for non-zero stream. Dropping.");
1122 return 0;
1124 return circuit_extend(cell, circ);
1125 case RELAY_COMMAND_EXTENDED:
1126 if (!layer_hint) {
1127 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1128 "'extended' unsupported at non-origin. Dropping.");
1129 return 0;
1131 log_debug(domain,"Got an extended cell! Yay.");
1132 if ((reason = circuit_finish_handshake(TO_ORIGIN_CIRCUIT(circ),
1133 CELL_CREATED,
1134 cell->payload+RELAY_HEADER_SIZE)) < 0) {
1135 log_warn(domain,"circuit_finish_handshake failed.");
1136 return reason;
1138 if ((reason=circuit_send_next_onion_skin(TO_ORIGIN_CIRCUIT(circ)))<0) {
1139 log_info(domain,"circuit_send_next_onion_skin() failed.");
1140 return reason;
1142 return 0;
1143 case RELAY_COMMAND_TRUNCATE:
1144 if (layer_hint) {
1145 log_fn(LOG_PROTOCOL_WARN, LD_APP,
1146 "'truncate' unsupported at origin. Dropping.");
1147 return 0;
1149 if (circ->n_conn) {
1150 uint8_t trunc_reason = *(uint8_t*)(cell->payload + RELAY_HEADER_SIZE);
1151 connection_or_send_destroy(circ->n_circ_id, circ->n_conn,
1152 trunc_reason);
1153 circuit_set_n_circid_orconn(circ, 0, NULL);
1155 log_debug(LD_EXIT, "Processed 'truncate', replying.");
1157 char payload[1];
1158 payload[0] = (char)END_CIRC_REASON_REQUESTED;
1159 relay_send_command_from_edge(0, circ, RELAY_COMMAND_TRUNCATED,
1160 payload, sizeof(payload), NULL);
1162 return 0;
1163 case RELAY_COMMAND_TRUNCATED:
1164 if (!layer_hint) {
1165 log_fn(LOG_PROTOCOL_WARN, LD_EXIT,
1166 "'truncated' unsupported at non-origin. Dropping.");
1167 return 0;
1169 circuit_truncated(TO_ORIGIN_CIRCUIT(circ), layer_hint);
1170 return 0;
1171 case RELAY_COMMAND_CONNECTED:
1172 if (conn) {
1173 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1174 "'connected' unsupported while open. Closing circ.");
1175 return -END_CIRC_REASON_TORPROTOCOL;
1177 log_info(domain,
1178 "'connected' received, no conn attached anymore. Ignoring.");
1179 return 0;
1180 case RELAY_COMMAND_SENDME:
1181 if (!conn) {
1182 if (layer_hint) {
1183 layer_hint->package_window += CIRCWINDOW_INCREMENT;
1184 log_debug(LD_APP,"circ-level sendme at origin, packagewindow %d.",
1185 layer_hint->package_window);
1186 circuit_resume_edge_reading(circ, layer_hint);
1187 } else {
1188 circ->package_window += CIRCWINDOW_INCREMENT;
1189 log_debug(LD_APP,
1190 "circ-level sendme at non-origin, packagewindow %d.",
1191 circ->package_window);
1192 circuit_resume_edge_reading(circ, layer_hint);
1194 return 0;
1196 conn->package_window += STREAMWINDOW_INCREMENT;
1197 log_debug(domain,"stream-level sendme, packagewindow now %d.",
1198 conn->package_window);
1199 connection_start_reading(TO_CONN(conn));
1200 /* handle whatever might still be on the inbuf */
1201 if (connection_edge_package_raw_inbuf(conn, 1) < 0) {
1202 /* (We already sent an end cell if possible) */
1203 connection_mark_for_close(TO_CONN(conn));
1204 return 0;
1206 return 0;
1207 case RELAY_COMMAND_RESOLVE:
1208 if (layer_hint) {
1209 log_fn(LOG_PROTOCOL_WARN, LD_APP,
1210 "resolve request unsupported at AP; dropping.");
1211 return 0;
1212 } else if (conn) {
1213 log_fn(LOG_PROTOCOL_WARN, domain,
1214 "resolve request for known stream; dropping.");
1215 return 0;
1216 } else if (circ->purpose != CIRCUIT_PURPOSE_OR) {
1217 log_fn(LOG_PROTOCOL_WARN, domain,
1218 "resolve request on circ with purpose %d; dropping",
1219 circ->purpose);
1220 return 0;
1222 connection_exit_begin_resolve(cell, TO_OR_CIRCUIT(circ));
1223 return 0;
1224 case RELAY_COMMAND_RESOLVED:
1225 if (conn) {
1226 log_fn(LOG_PROTOCOL_WARN, domain,
1227 "'resolved' unsupported while open. Closing circ.");
1228 return -END_CIRC_REASON_TORPROTOCOL;
1230 log_info(domain,
1231 "'resolved' received, no conn attached anymore. Ignoring.");
1232 return 0;
1233 case RELAY_COMMAND_ESTABLISH_INTRO:
1234 case RELAY_COMMAND_ESTABLISH_RENDEZVOUS:
1235 case RELAY_COMMAND_INTRODUCE1:
1236 case RELAY_COMMAND_INTRODUCE2:
1237 case RELAY_COMMAND_INTRODUCE_ACK:
1238 case RELAY_COMMAND_RENDEZVOUS1:
1239 case RELAY_COMMAND_RENDEZVOUS2:
1240 case RELAY_COMMAND_INTRO_ESTABLISHED:
1241 case RELAY_COMMAND_RENDEZVOUS_ESTABLISHED:
1242 rend_process_relay_cell(circ, layer_hint,
1243 rh.command, rh.length,
1244 cell->payload+RELAY_HEADER_SIZE);
1245 return 0;
1247 log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
1248 "Received unknown relay command %d. Perhaps the other side is using "
1249 "a newer version of Tor? Dropping.",
1250 rh.command);
1251 return 0; /* for forward compatibility, don't kill the circuit */
1254 /** How many relay_data cells have we built, ever? */
1255 uint64_t stats_n_data_cells_packaged = 0;
1256 /** How many bytes of data have we put in relay_data cells have we built,
1257 * ever? This would be RELAY_PAYLOAD_SIZE*stats_n_data_cells_packaged if
1258 * every relay cell we ever sent were completely full of data. */
1259 uint64_t stats_n_data_bytes_packaged = 0;
1260 /** How many relay_data cells have we received, ever? */
1261 uint64_t stats_n_data_cells_received = 0;
1262 /** How many bytes of data have we received relay_data cells, ever? This would
1263 * be RELAY_PAYLOAD_SIZE*stats_n_data_cells_packaged if every relay cell we
1264 * ever received were completely full of data. */
1265 uint64_t stats_n_data_bytes_received = 0;
1267 /** While conn->inbuf has an entire relay payload of bytes on it,
1268 * and the appropriate package windows aren't empty, grab a cell
1269 * and send it down the circuit.
1271 * Return -1 (and send a RELAY_COMMAND_END cell if necessary) if conn should
1272 * be marked for close, else return 0.
1275 connection_edge_package_raw_inbuf(edge_connection_t *conn, int package_partial)
1277 size_t amount_to_process, length;
1278 char payload[CELL_PAYLOAD_SIZE];
1279 circuit_t *circ;
1280 unsigned domain = conn->cpath_layer ? LD_APP : LD_EXIT;
1282 tor_assert(conn);
1284 if (conn->_base.marked_for_close) {
1285 log_warn(LD_BUG,
1286 "called on conn that's already marked for close at %s:%d.",
1287 conn->_base.marked_for_close_file, conn->_base.marked_for_close);
1288 return 0;
1291 repeat_connection_edge_package_raw_inbuf:
1293 circ = circuit_get_by_edge_conn(conn);
1294 if (!circ) {
1295 log_info(domain,"conn has no circuit! Closing.");
1296 conn->end_reason = END_STREAM_REASON_CANT_ATTACH;
1297 return -1;
1300 if (circuit_consider_stop_edge_reading(circ, conn->cpath_layer))
1301 return 0;
1303 if (conn->package_window <= 0) {
1304 log_info(domain,"called with package_window %d. Skipping.",
1305 conn->package_window);
1306 connection_stop_reading(TO_CONN(conn));
1307 return 0;
1310 amount_to_process = buf_datalen(conn->_base.inbuf);
1312 if (!amount_to_process)
1313 return 0;
1315 if (!package_partial && amount_to_process < RELAY_PAYLOAD_SIZE)
1316 return 0;
1318 if (amount_to_process > RELAY_PAYLOAD_SIZE) {
1319 length = RELAY_PAYLOAD_SIZE;
1320 } else {
1321 length = amount_to_process;
1323 stats_n_data_bytes_packaged += length;
1324 stats_n_data_cells_packaged += 1;
1326 connection_fetch_from_buf(payload, length, TO_CONN(conn));
1328 log_debug(domain,"(%d) Packaging %d bytes (%d waiting).", conn->_base.s,
1329 (int)length, (int)buf_datalen(conn->_base.inbuf));
1331 if (connection_edge_send_command(conn, RELAY_COMMAND_DATA,
1332 payload, length) < 0 )
1333 /* circuit got marked for close, don't continue, don't need to mark conn */
1334 return 0;
1336 if (!conn->cpath_layer) { /* non-rendezvous exit */
1337 tor_assert(circ->package_window > 0);
1338 circ->package_window--;
1339 } else { /* we're an AP, or an exit on a rendezvous circ */
1340 tor_assert(conn->cpath_layer->package_window > 0);
1341 conn->cpath_layer->package_window--;
1344 if (--conn->package_window <= 0) { /* is it 0 after decrement? */
1345 connection_stop_reading(TO_CONN(conn));
1346 log_debug(domain,"conn->package_window reached 0.");
1347 circuit_consider_stop_edge_reading(circ, conn->cpath_layer);
1348 return 0; /* don't process the inbuf any more */
1350 log_debug(domain,"conn->package_window is now %d",conn->package_window);
1352 /* handle more if there's more, or return 0 if there isn't */
1353 goto repeat_connection_edge_package_raw_inbuf;
1356 /** Called when we've just received a relay data cell, or when
1357 * we've just finished flushing all bytes to stream <b>conn</b>.
1359 * If conn->outbuf is not too full, and our deliver window is
1360 * low, send back a suitable number of stream-level sendme cells.
1362 void
1363 connection_edge_consider_sending_sendme(edge_connection_t *conn)
1365 circuit_t *circ;
1367 if (connection_outbuf_too_full(TO_CONN(conn)))
1368 return;
1370 circ = circuit_get_by_edge_conn(conn);
1371 if (!circ) {
1372 /* this can legitimately happen if the destroy has already
1373 * arrived and torn down the circuit */
1374 log_info(LD_APP,"No circuit associated with conn. Skipping.");
1375 return;
1378 while (conn->deliver_window <= STREAMWINDOW_START - STREAMWINDOW_INCREMENT) {
1379 log_debug(conn->cpath_layer?LD_APP:LD_EXIT,
1380 "Outbuf %d, Queuing stream sendme.",
1381 (int)conn->_base.outbuf_flushlen);
1382 conn->deliver_window += STREAMWINDOW_INCREMENT;
1383 if (connection_edge_send_command(conn, RELAY_COMMAND_SENDME,
1384 NULL, 0) < 0) {
1385 log_warn(LD_APP,"connection_edge_send_command failed. Skipping.");
1386 return; /* the circuit's closed, don't continue */
1391 /** The circuit <b>circ</b> has received a circuit-level sendme
1392 * (on hop <b>layer_hint</b>, if we're the OP). Go through all the
1393 * attached streams and let them resume reading and packaging, if
1394 * their stream windows allow it.
1396 static void
1397 circuit_resume_edge_reading(circuit_t *circ, crypt_path_t *layer_hint)
1400 log_debug(layer_hint?LD_APP:LD_EXIT,"resuming");
1402 if (CIRCUIT_IS_ORIGIN(circ))
1403 circuit_resume_edge_reading_helper(TO_ORIGIN_CIRCUIT(circ)->p_streams,
1404 circ, layer_hint);
1405 else
1406 circuit_resume_edge_reading_helper(TO_OR_CIRCUIT(circ)->n_streams,
1407 circ, layer_hint);
1410 /** A helper function for circuit_resume_edge_reading() above.
1411 * The arguments are the same, except that <b>conn</b> is the head
1412 * of a linked list of edge streams that should each be considered.
1414 static int
1415 circuit_resume_edge_reading_helper(edge_connection_t *conn,
1416 circuit_t *circ,
1417 crypt_path_t *layer_hint)
1419 for ( ; conn; conn=conn->next_stream) {
1420 if (conn->_base.marked_for_close)
1421 continue;
1422 if ((!layer_hint && conn->package_window > 0) ||
1423 (layer_hint && conn->package_window > 0 &&
1424 conn->cpath_layer == layer_hint)) {
1425 connection_start_reading(TO_CONN(conn));
1426 /* handle whatever might still be on the inbuf */
1427 if (connection_edge_package_raw_inbuf(conn, 1)<0) {
1428 /* (We already sent an end cell if possible) */
1429 connection_mark_for_close(TO_CONN(conn));
1430 continue;
1433 /* If the circuit won't accept any more data, return without looking
1434 * at any more of the streams. Any connections that should be stopped
1435 * have already been stopped by connection_edge_package_raw_inbuf. */
1436 if (circuit_consider_stop_edge_reading(circ, layer_hint))
1437 return -1;
1440 return 0;
1443 /** Check if the package window for <b>circ</b> is empty (at
1444 * hop <b>layer_hint</b> if it's defined).
1446 * If yes, tell edge streams to stop reading and return 1.
1447 * Else return 0.
1449 static int
1450 circuit_consider_stop_edge_reading(circuit_t *circ, crypt_path_t *layer_hint)
1452 edge_connection_t *conn = NULL;
1453 unsigned domain = layer_hint ? LD_APP : LD_EXIT;
1455 if (!layer_hint) {
1456 or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
1457 log_debug(domain,"considering circ->package_window %d",
1458 circ->package_window);
1459 if (circ->package_window <= 0) {
1460 log_debug(domain,"yes, not-at-origin. stopped.");
1461 for (conn = or_circ->n_streams; conn; conn=conn->next_stream)
1462 connection_stop_reading(TO_CONN(conn));
1463 return 1;
1465 return 0;
1467 /* else, layer hint is defined, use it */
1468 log_debug(domain,"considering layer_hint->package_window %d",
1469 layer_hint->package_window);
1470 if (layer_hint->package_window <= 0) {
1471 log_debug(domain,"yes, at-origin. stopped.");
1472 for (conn = TO_ORIGIN_CIRCUIT(circ)->p_streams; conn;
1473 conn=conn->next_stream)
1474 if (conn->cpath_layer == layer_hint)
1475 connection_stop_reading(TO_CONN(conn));
1476 return 1;
1478 return 0;
1481 /** Check if the deliver_window for circuit <b>circ</b> (at hop
1482 * <b>layer_hint</b> if it's defined) is low enough that we should
1483 * send a circuit-level sendme back down the circuit. If so, send
1484 * enough sendmes that the window would be overfull if we sent any
1485 * more.
1487 static void
1488 circuit_consider_sending_sendme(circuit_t *circ, crypt_path_t *layer_hint)
1490 // log_fn(LOG_INFO,"Considering: layer_hint is %s",
1491 // layer_hint ? "defined" : "null");
1492 while ((layer_hint ? layer_hint->deliver_window : circ->deliver_window) <=
1493 CIRCWINDOW_START - CIRCWINDOW_INCREMENT) {
1494 log_debug(LD_CIRC,"Queuing circuit sendme.");
1495 if (layer_hint)
1496 layer_hint->deliver_window += CIRCWINDOW_INCREMENT;
1497 else
1498 circ->deliver_window += CIRCWINDOW_INCREMENT;
1499 if (relay_send_command_from_edge(0, circ, RELAY_COMMAND_SENDME,
1500 NULL, 0, layer_hint) < 0) {
1501 log_warn(LD_CIRC,
1502 "relay_send_command_from_edge failed. Circuit's closed.");
1503 return; /* the circuit's closed, don't continue */
1508 /** Stop reading on edge connections when we have this many cells
1509 * waiting on the appropriate queue. */
1510 #define CELL_QUEUE_HIGHWATER_SIZE 256
1511 /** Start reading from edge connections again when we get down to this many
1512 * cells. */
1513 #define CELL_QUEUE_LOWWATER_SIZE 64
1515 #ifdef ACTIVE_CIRCUITS_PARANOIA
1516 #define assert_active_circuits_ok_paranoid(conn) \
1517 assert_active_circuits_ok(conn)
1518 #else
1519 #define assert_active_circuits_ok_paranoid(conn)
1520 #endif
1522 /** The total number of cells we have allocated from the memory pool. */
1523 static int total_cells_allocated = 0;
1525 /** A memory pool to allocate packed_cell_t objects. */
1526 static mp_pool_t *cell_pool = NULL;
1528 /** Allocate structures to hold cells. */
1529 void
1530 init_cell_pool(void)
1532 tor_assert(!cell_pool);
1533 cell_pool = mp_pool_new(sizeof(packed_cell_t), 128*1024);
1536 /** Free all storage used to hold cells. */
1537 void
1538 free_cell_pool(void)
1540 /* Maybe we haven't called init_cell_pool yet; need to check for it. */
1541 if (cell_pool) {
1542 mp_pool_destroy(cell_pool);
1543 cell_pool = NULL;
1547 /** Free excess storage in cell pool. */
1548 void
1549 clean_cell_pool(void)
1551 tor_assert(cell_pool);
1552 mp_pool_clean(cell_pool, 0, 1);
1555 /** Release storage held by <b>cell</b>. */
1556 static INLINE void
1557 packed_cell_free(packed_cell_t *cell)
1559 --total_cells_allocated;
1560 mp_pool_release(cell);
1563 /** Allocate and return a new packed_cell_t. */
1564 static INLINE packed_cell_t *
1565 packed_cell_alloc(void)
1567 ++total_cells_allocated;
1568 return mp_pool_get(cell_pool);
1571 /** Log current statistics for cell pool allocation at log level
1572 * <b>severity</b>. */
1573 void
1574 dump_cell_pool_usage(int severity)
1576 circuit_t *c;
1577 int n_circs = 0;
1578 int n_cells = 0;
1579 for (c = _circuit_get_global_list(); c; c = c->next) {
1580 n_cells += c->n_conn_cells.n;
1581 if (!CIRCUIT_IS_ORIGIN(c))
1582 n_cells += TO_OR_CIRCUIT(c)->p_conn_cells.n;
1583 ++n_circs;
1585 log(severity, LD_MM, "%d cells allocated on %d circuits. %d cells leaked.",
1586 n_cells, n_circs, total_cells_allocated - n_cells);
1587 mp_pool_log_status(cell_pool, severity);
1590 /** Allocate a new copy of packed <b>cell</b>. */
1591 static INLINE packed_cell_t *
1592 packed_cell_copy(const cell_t *cell)
1594 packed_cell_t *c = packed_cell_alloc();
1595 cell_pack(c, cell);
1596 c->next = NULL;
1597 return c;
1600 /** Append <b>cell</b> to the end of <b>queue</b>. */
1601 void
1602 cell_queue_append(cell_queue_t *queue, packed_cell_t *cell)
1604 if (queue->tail) {
1605 tor_assert(!queue->tail->next);
1606 queue->tail->next = cell;
1607 } else {
1608 queue->head = cell;
1610 queue->tail = cell;
1611 cell->next = NULL;
1612 ++queue->n;
1615 /** Number of cells added to a circuit queue including their insertion
1616 * time on 10 millisecond detail; used for buffer statistics. */
1617 typedef struct insertion_time_elem_t {
1618 uint32_t insertion_time; /**< When were cells inserted (in 10 ms steps
1619 * starting at 0:00 of the current day)? */
1620 unsigned counter; /**< How many cells were inserted? */
1621 } insertion_time_elem_t;
1623 /** Append a newly allocated copy of <b>cell</b> to the end of <b>queue</b> */
1624 void
1625 cell_queue_append_packed_copy(cell_queue_t *queue, const cell_t *cell)
1627 packed_cell_t *copy = packed_cell_copy(cell);
1628 /* Remember the time in millis when this cell was put in the queue. */
1629 if (get_options()->CellStatistics) {
1630 struct timeval now;
1631 uint32_t added;
1632 insertion_time_elem_t *last_elem = NULL;
1633 int add_new_elem = 0;
1634 tor_gettimeofday(&now);
1635 #define SECONDS_IN_A_DAY 86400L
1636 added = now.tv_sec % SECONDS_IN_A_DAY * 10L + now.tv_usec / 100000L;
1637 if (!queue->insertion_times) {
1638 queue->insertion_times = smartlist_create();
1640 if (smartlist_len(queue->insertion_times) < 1) {
1641 add_new_elem = 1;
1642 } else {
1643 last_elem = smartlist_get(queue->insertion_times,
1644 smartlist_len(queue->insertion_times) - 1);
1645 if (last_elem->insertion_time == added)
1646 last_elem->counter++;
1647 else
1648 add_new_elem = 1;
1650 if (add_new_elem) {
1651 insertion_time_elem_t *elem =
1652 tor_malloc_zero(sizeof(insertion_time_elem_t));
1653 elem->insertion_time = added;
1654 elem->counter = 1;
1655 smartlist_add(queue->insertion_times, elem);
1658 cell_queue_append(queue, copy);
1661 /** Remove and free every cell in <b>queue</b>. */
1662 void
1663 cell_queue_clear(cell_queue_t *queue)
1665 packed_cell_t *cell, *next;
1666 cell = queue->head;
1667 while (cell) {
1668 next = cell->next;
1669 packed_cell_free(cell);
1670 cell = next;
1672 queue->head = queue->tail = NULL;
1673 queue->n = 0;
1674 if (queue->insertion_times) {
1675 SMARTLIST_FOREACH(queue->insertion_times, void *, e, tor_free(e));
1676 smartlist_free(queue->insertion_times);
1677 queue->insertion_times = NULL;
1681 /** Extract and return the cell at the head of <b>queue</b>; return NULL if
1682 * <b>queue</b> is empty. */
1683 static INLINE packed_cell_t *
1684 cell_queue_pop(cell_queue_t *queue)
1686 packed_cell_t *cell = queue->head;
1687 if (!cell)
1688 return NULL;
1689 queue->head = cell->next;
1690 if (cell == queue->tail) {
1691 tor_assert(!queue->head);
1692 queue->tail = NULL;
1694 --queue->n;
1695 return cell;
1698 /** Return a pointer to the "next_active_on_{n,p}_conn" pointer of <b>circ</b>,
1699 * depending on whether <b>conn</b> matches n_conn or p_conn. */
1700 static INLINE circuit_t **
1701 next_circ_on_conn_p(circuit_t *circ, or_connection_t *conn)
1703 tor_assert(circ);
1704 tor_assert(conn);
1705 if (conn == circ->n_conn) {
1706 return &circ->next_active_on_n_conn;
1707 } else {
1708 or_circuit_t *orcirc = TO_OR_CIRCUIT(circ);
1709 tor_assert(conn == orcirc->p_conn);
1710 return &orcirc->next_active_on_p_conn;
1714 /** Return a pointer to the "prev_active_on_{n,p}_conn" pointer of <b>circ</b>,
1715 * depending on whether <b>conn</b> matches n_conn or p_conn. */
1716 static INLINE circuit_t **
1717 prev_circ_on_conn_p(circuit_t *circ, or_connection_t *conn)
1719 tor_assert(circ);
1720 tor_assert(conn);
1721 if (conn == circ->n_conn) {
1722 return &circ->prev_active_on_n_conn;
1723 } else {
1724 or_circuit_t *orcirc = TO_OR_CIRCUIT(circ);
1725 tor_assert(conn == orcirc->p_conn);
1726 return &orcirc->prev_active_on_p_conn;
1730 /** Add <b>circ</b> to the list of circuits with pending cells on
1731 * <b>conn</b>. No effect if <b>circ</b> is already linked. */
1732 void
1733 make_circuit_active_on_conn(circuit_t *circ, or_connection_t *conn)
1735 circuit_t **nextp = next_circ_on_conn_p(circ, conn);
1736 circuit_t **prevp = prev_circ_on_conn_p(circ, conn);
1738 if (*nextp && *prevp) {
1739 /* Already active. */
1740 return;
1743 if (! conn->active_circuits) {
1744 conn->active_circuits = circ;
1745 *prevp = *nextp = circ;
1746 } else {
1747 circuit_t *head = conn->active_circuits;
1748 circuit_t *old_tail = *prev_circ_on_conn_p(head, conn);
1749 *next_circ_on_conn_p(old_tail, conn) = circ;
1750 *nextp = head;
1751 *prev_circ_on_conn_p(head, conn) = circ;
1752 *prevp = old_tail;
1754 assert_active_circuits_ok_paranoid(conn);
1757 /** Remove <b>circ</b> from the list of circuits with pending cells on
1758 * <b>conn</b>. No effect if <b>circ</b> is already unlinked. */
1759 void
1760 make_circuit_inactive_on_conn(circuit_t *circ, or_connection_t *conn)
1762 circuit_t **nextp = next_circ_on_conn_p(circ, conn);
1763 circuit_t **prevp = prev_circ_on_conn_p(circ, conn);
1764 circuit_t *next = *nextp, *prev = *prevp;
1766 if (!next && !prev) {
1767 /* Already inactive. */
1768 return;
1771 tor_assert(next && prev);
1772 tor_assert(*prev_circ_on_conn_p(next, conn) == circ);
1773 tor_assert(*next_circ_on_conn_p(prev, conn) == circ);
1775 if (next == circ) {
1776 conn->active_circuits = NULL;
1777 } else {
1778 *prev_circ_on_conn_p(next, conn) = prev;
1779 *next_circ_on_conn_p(prev, conn) = next;
1780 if (conn->active_circuits == circ)
1781 conn->active_circuits = next;
1783 *prevp = *nextp = NULL;
1784 assert_active_circuits_ok_paranoid(conn);
1787 /** Remove all circuits from the list of circuits with pending cells on
1788 * <b>conn</b>. */
1789 void
1790 connection_or_unlink_all_active_circs(or_connection_t *orconn)
1792 circuit_t *head = orconn->active_circuits;
1793 circuit_t *cur = head;
1794 if (! head)
1795 return;
1796 do {
1797 circuit_t *next = *next_circ_on_conn_p(cur, orconn);
1798 *prev_circ_on_conn_p(cur, orconn) = NULL;
1799 *next_circ_on_conn_p(cur, orconn) = NULL;
1800 cur = next;
1801 } while (cur != head);
1802 orconn->active_circuits = NULL;
1805 /** Block (if <b>block</b> is true) or unblock (if <b>block</b> is false)
1806 * every edge connection that is using <b>circ</b> to write to <b>orconn</b>,
1807 * and start or stop reading as appropriate. */
1808 static void
1809 set_streams_blocked_on_circ(circuit_t *circ, or_connection_t *orconn,
1810 int block)
1812 edge_connection_t *edge = NULL;
1813 if (circ->n_conn == orconn) {
1814 circ->streams_blocked_on_n_conn = block;
1815 if (CIRCUIT_IS_ORIGIN(circ))
1816 edge = TO_ORIGIN_CIRCUIT(circ)->p_streams;
1817 } else {
1818 circ->streams_blocked_on_p_conn = block;
1819 tor_assert(!CIRCUIT_IS_ORIGIN(circ));
1820 edge = TO_OR_CIRCUIT(circ)->n_streams;
1823 for (; edge; edge = edge->next_stream) {
1824 connection_t *conn = TO_CONN(edge);
1825 edge->edge_blocked_on_circ = block;
1827 if (!conn->read_event) {
1828 /* This connection is a placeholder for something; probably a DNS
1829 * request. It can't actually stop or start reading.*/
1830 continue;
1833 if (block) {
1834 if (connection_is_reading(conn))
1835 connection_stop_reading(conn);
1836 } else {
1837 /* Is this right? */
1838 if (!connection_is_reading(conn))
1839 connection_start_reading(conn);
1844 /** Pull as many cells as possible (but no more than <b>max</b>) from the
1845 * queue of the first active circuit on <b>conn</b>, and write then to
1846 * <b>conn</b>-&gt;outbuf. Return the number of cells written. Advance
1847 * the active circuit pointer to the next active circuit in the ring. */
1849 connection_or_flush_from_first_active_circuit(or_connection_t *conn, int max,
1850 time_t now)
1852 int n_flushed;
1853 cell_queue_t *queue;
1854 circuit_t *circ;
1855 int streams_blocked;
1856 circ = conn->active_circuits;
1857 if (!circ) return 0;
1858 assert_active_circuits_ok_paranoid(conn);
1859 if (circ->n_conn == conn) {
1860 queue = &circ->n_conn_cells;
1861 streams_blocked = circ->streams_blocked_on_n_conn;
1862 } else {
1863 queue = &TO_OR_CIRCUIT(circ)->p_conn_cells;
1864 streams_blocked = circ->streams_blocked_on_p_conn;
1866 tor_assert(*next_circ_on_conn_p(circ,conn));
1868 for (n_flushed = 0; n_flushed < max && queue->head; ) {
1869 packed_cell_t *cell = cell_queue_pop(queue);
1870 tor_assert(*next_circ_on_conn_p(circ,conn));
1872 /* Calculate the exact time that this cell has spent in the queue. */
1873 if (get_options()->CellStatistics && !CIRCUIT_IS_ORIGIN(circ)) {
1874 struct timeval now;
1875 uint32_t flushed;
1876 uint32_t cell_waiting_time;
1877 tor_gettimeofday(&now);
1878 flushed = now.tv_sec % SECONDS_IN_A_DAY * 10L + now.tv_usec / 100000L;
1879 if (!queue->insertion_times ||
1880 smartlist_len(queue->insertion_times) < 1) {
1881 log_warn(LD_BUG, "Cannot determine insertion time of cell.");
1882 } else {
1883 or_circuit_t *orcirc = TO_OR_CIRCUIT(circ);
1884 insertion_time_elem_t *elem = smartlist_get(
1885 queue->insertion_times, 0);
1886 cell_waiting_time = (flushed + SECONDS_IN_A_DAY * 10L -
1887 elem->insertion_time) % (SECONDS_IN_A_DAY * 10L);
1888 #undef SECONDS_IN_A_DAY
1889 elem->counter--;
1890 if (elem->counter < 1) {
1891 // TODO this operation is really expensive! write own queue impl?
1892 // smartlist_del(queue->insertion_times, 0);
1893 smartlist_remove(queue->insertion_times, elem);
1894 tor_free(elem);
1896 orcirc->total_cell_waiting_time += cell_waiting_time;
1897 orcirc->processed_cells++;
1901 /* If we just flushed our queue and this circuit is used for a
1902 * tunneled directory request, possibly advance its state. */
1903 if (queue->n == 0 && TO_CONN(conn)->dirreq_id)
1904 geoip_change_dirreq_state(TO_CONN(conn)->dirreq_id,
1905 DIRREQ_TUNNELED,
1906 DIRREQ_CIRC_QUEUE_FLUSHED);
1908 connection_write_to_buf(cell->body, CELL_NETWORK_SIZE, TO_CONN(conn));
1910 packed_cell_free(cell);
1911 ++n_flushed;
1912 if (circ != conn->active_circuits) {
1913 /* If this happens, the current circuit just got made inactive by
1914 * a call in connection_write_to_buf(). That's nothing to worry about:
1915 * circuit_make_inactive_on_conn() already advanced conn->active_circuits
1916 * for us.
1918 assert_active_circuits_ok_paranoid(conn);
1919 goto done;
1922 tor_assert(*next_circ_on_conn_p(circ,conn));
1923 assert_active_circuits_ok_paranoid(conn);
1924 conn->active_circuits = *next_circ_on_conn_p(circ, conn);
1926 /* Is the cell queue low enough to unblock all the streams that are waiting
1927 * to write to this circuit? */
1928 if (streams_blocked && queue->n <= CELL_QUEUE_LOWWATER_SIZE)
1929 set_streams_blocked_on_circ(circ, conn, 0); /* unblock streams */
1931 /* Did we just ran out of cells on this queue? */
1932 if (queue->n == 0) {
1933 log_debug(LD_GENERAL, "Made a circuit inactive.");
1934 make_circuit_inactive_on_conn(circ, conn);
1936 done:
1937 if (n_flushed)
1938 conn->timestamp_last_added_nonpadding = now;
1939 return n_flushed;
1942 /** Add <b>cell</b> to the queue of <b>circ</b> writing to <b>orconn</b>
1943 * transmitting in <b>direction</b>. */
1944 void
1945 append_cell_to_circuit_queue(circuit_t *circ, or_connection_t *orconn,
1946 cell_t *cell, cell_direction_t direction)
1948 cell_queue_t *queue;
1949 int streams_blocked;
1950 if (direction == CELL_DIRECTION_OUT) {
1951 queue = &circ->n_conn_cells;
1952 streams_blocked = circ->streams_blocked_on_n_conn;
1953 } else {
1954 or_circuit_t *orcirc = TO_OR_CIRCUIT(circ);
1955 queue = &orcirc->p_conn_cells;
1956 streams_blocked = circ->streams_blocked_on_p_conn;
1958 if (cell->command == CELL_RELAY_EARLY && orconn->link_proto < 2) {
1959 /* V1 connections don't understand RELAY_EARLY. */
1960 cell->command = CELL_RELAY;
1963 cell_queue_append_packed_copy(queue, cell);
1965 /* If we have too many cells on the circuit, we should stop reading from
1966 * the edge streams for a while. */
1967 if (!streams_blocked && queue->n >= CELL_QUEUE_HIGHWATER_SIZE)
1968 set_streams_blocked_on_circ(circ, orconn, 1); /* block streams */
1970 if (queue->n == 1) {
1971 /* This was the first cell added to the queue. We need to make this
1972 * circuit active. */
1973 log_debug(LD_GENERAL, "Made a circuit active.");
1974 make_circuit_active_on_conn(circ, orconn);
1977 if (! buf_datalen(orconn->_base.outbuf)) {
1978 /* There is no data at all waiting to be sent on the outbuf. Add a
1979 * cell, so that we can notice when it gets flushed, flushed_some can
1980 * get called, and we can start putting more data onto the buffer then.
1982 log_debug(LD_GENERAL, "Primed a buffer.");
1983 connection_or_flush_from_first_active_circuit(orconn, 1, approx_time());
1987 /** Append an encoded value of <b>addr</b> to <b>payload_out</b>, which must
1988 * have at least 18 bytes of free space. The encoding is, as specified in
1989 * tor-spec.txt:
1990 * RESOLVED_TYPE_IPV4 or RESOLVED_TYPE_IPV6 [1 byte]
1991 * LENGTH [1 byte]
1992 * ADDRESS [length bytes]
1993 * Return the number of bytes added, or -1 on error */
1995 append_address_to_payload(char *payload_out, const tor_addr_t *addr)
1997 uint32_t a;
1998 switch (tor_addr_family(addr)) {
1999 case AF_INET:
2000 payload_out[0] = RESOLVED_TYPE_IPV4;
2001 payload_out[1] = 4;
2002 a = tor_addr_to_ipv4n(addr);
2003 memcpy(payload_out+2, &a, 4);
2004 return 6;
2005 case AF_INET6:
2006 payload_out[0] = RESOLVED_TYPE_IPV6;
2007 payload_out[1] = 16;
2008 memcpy(payload_out+2, tor_addr_to_in6_addr8(addr), 16);
2009 return 18;
2010 case AF_UNSPEC:
2011 default:
2012 return -1;
2016 /** Given <b>payload_len</b> bytes at <b>payload</b>, starting with an address
2017 * encoded as by append_address_to_payload(), try to decode the address into
2018 * *<b>addr_out</b>. Return the next byte in the payload after the address on
2019 * success, or NULL on failure. */
2020 const char *
2021 decode_address_from_payload(tor_addr_t *addr_out, const char *payload,
2022 int payload_len)
2024 if (payload_len < 2)
2025 return NULL;
2026 if (payload_len < 2+(uint8_t)payload[1])
2027 return NULL;
2029 switch (payload[0]) {
2030 case RESOLVED_TYPE_IPV4:
2031 if (payload[1] != 4)
2032 return NULL;
2033 tor_addr_from_ipv4n(addr_out, get_uint32(payload+2));
2034 break;
2035 case RESOLVED_TYPE_IPV6:
2036 if (payload[1] != 16)
2037 return NULL;
2038 tor_addr_from_ipv6_bytes(addr_out, payload+2);
2039 break;
2040 default:
2041 tor_addr_make_unspec(addr_out);
2042 break;
2044 return payload + 2 + (uint8_t)payload[1];
2047 /** Fail with an assert if the active circuits ring on <b>orconn</b> is
2048 * corrupt. */
2049 void
2050 assert_active_circuits_ok(or_connection_t *orconn)
2052 circuit_t *head = orconn->active_circuits;
2053 circuit_t *cur = head;
2054 if (! head)
2055 return;
2056 do {
2057 circuit_t *next = *next_circ_on_conn_p(cur, orconn);
2058 circuit_t *prev = *prev_circ_on_conn_p(cur, orconn);
2059 tor_assert(next);
2060 tor_assert(prev);
2061 tor_assert(*next_circ_on_conn_p(prev, orconn) == cur);
2062 tor_assert(*prev_circ_on_conn_p(next, orconn) == cur);
2063 cur = next;
2064 } while (cur != head);