Clean up proposal 166 and its implementation.
[tor/rransom.git] / src / or / relay.c
bloba79a4c161af355036c80ba72171b59b53b1010ce
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 /** Memory pool to allocate insertion_time_elem_t objects used for cell
1529 * statistics. */
1530 static mp_pool_t *it_pool = NULL;
1532 /** Allocate structures to hold cells. */
1533 void
1534 init_cell_pool(void)
1536 tor_assert(!cell_pool);
1537 cell_pool = mp_pool_new(sizeof(packed_cell_t), 128*1024);
1540 /** Free all storage used to hold cells (and insertion times if we measure
1541 * cell statistics). */
1542 void
1543 free_cell_pool(void)
1545 /* Maybe we haven't called init_cell_pool yet; need to check for it. */
1546 if (cell_pool) {
1547 mp_pool_destroy(cell_pool);
1548 cell_pool = NULL;
1550 if (it_pool) {
1551 mp_pool_destroy(it_pool);
1552 it_pool = NULL;
1556 /** Free excess storage in cell pool. */
1557 void
1558 clean_cell_pool(void)
1560 tor_assert(cell_pool);
1561 mp_pool_clean(cell_pool, 0, 1);
1564 /** Release storage held by <b>cell</b>. */
1565 static INLINE void
1566 packed_cell_free(packed_cell_t *cell)
1568 --total_cells_allocated;
1569 mp_pool_release(cell);
1572 /** Allocate and return a new packed_cell_t. */
1573 static INLINE packed_cell_t *
1574 packed_cell_alloc(void)
1576 ++total_cells_allocated;
1577 return mp_pool_get(cell_pool);
1580 /** Log current statistics for cell pool allocation at log level
1581 * <b>severity</b>. */
1582 void
1583 dump_cell_pool_usage(int severity)
1585 circuit_t *c;
1586 int n_circs = 0;
1587 int n_cells = 0;
1588 for (c = _circuit_get_global_list(); c; c = c->next) {
1589 n_cells += c->n_conn_cells.n;
1590 if (!CIRCUIT_IS_ORIGIN(c))
1591 n_cells += TO_OR_CIRCUIT(c)->p_conn_cells.n;
1592 ++n_circs;
1594 log(severity, LD_MM, "%d cells allocated on %d circuits. %d cells leaked.",
1595 n_cells, n_circs, total_cells_allocated - n_cells);
1596 mp_pool_log_status(cell_pool, severity);
1599 /** Allocate a new copy of packed <b>cell</b>. */
1600 static INLINE packed_cell_t *
1601 packed_cell_copy(const cell_t *cell)
1603 packed_cell_t *c = packed_cell_alloc();
1604 cell_pack(c, cell);
1605 c->next = NULL;
1606 return c;
1609 /** Append <b>cell</b> to the end of <b>queue</b>. */
1610 void
1611 cell_queue_append(cell_queue_t *queue, packed_cell_t *cell)
1613 if (queue->tail) {
1614 tor_assert(!queue->tail->next);
1615 queue->tail->next = cell;
1616 } else {
1617 queue->head = cell;
1619 queue->tail = cell;
1620 cell->next = NULL;
1621 ++queue->n;
1624 /** Append a newly allocated copy of <b>cell</b> to the end of <b>queue</b> */
1625 void
1626 cell_queue_append_packed_copy(cell_queue_t *queue, const cell_t *cell)
1628 packed_cell_t *copy = packed_cell_copy(cell);
1629 /* Remember the time when this cell was put in the queue. */
1630 if (get_options()->CellStatistics) {
1631 struct timeval now;
1632 uint32_t added;
1633 insertion_time_queue_t *it_queue = queue->insertion_times;
1634 if (!it_pool)
1635 it_pool = mp_pool_new(sizeof(insertion_time_elem_t), 1024);
1636 tor_gettimeofday(&now);
1637 #define SECONDS_IN_A_DAY 86400L
1638 added = (now.tv_sec % SECONDS_IN_A_DAY) * 100L + now.tv_usec / 10000L;
1639 if (!it_queue) {
1640 it_queue = tor_malloc_zero(sizeof(insertion_time_queue_t));
1641 queue->insertion_times = it_queue;
1643 if (it_queue->last && it_queue->last->insertion_time == added) {
1644 it_queue->last->counter++;
1645 } else {
1646 insertion_time_elem_t *elem = mp_pool_get(it_pool);
1647 elem->next = NULL;
1648 elem->insertion_time = added;
1649 elem->counter = 1;
1650 if (it_queue->last) {
1651 it_queue->last->next = elem;
1652 it_queue->last = elem;
1653 } else {
1654 it_queue->first = it_queue->last = 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 while (queue->insertion_times->first) {
1676 insertion_time_elem_t *elem = queue->insertion_times->first;
1677 queue->insertion_times->first = elem->next;
1678 mp_pool_release(elem);
1680 queue->insertion_times = NULL;
1684 /** Extract and return the cell at the head of <b>queue</b>; return NULL if
1685 * <b>queue</b> is empty. */
1686 static INLINE packed_cell_t *
1687 cell_queue_pop(cell_queue_t *queue)
1689 packed_cell_t *cell = queue->head;
1690 if (!cell)
1691 return NULL;
1692 queue->head = cell->next;
1693 if (cell == queue->tail) {
1694 tor_assert(!queue->head);
1695 queue->tail = NULL;
1697 --queue->n;
1698 return cell;
1701 /** Return a pointer to the "next_active_on_{n,p}_conn" pointer of <b>circ</b>,
1702 * depending on whether <b>conn</b> matches n_conn or p_conn. */
1703 static INLINE circuit_t **
1704 next_circ_on_conn_p(circuit_t *circ, or_connection_t *conn)
1706 tor_assert(circ);
1707 tor_assert(conn);
1708 if (conn == circ->n_conn) {
1709 return &circ->next_active_on_n_conn;
1710 } else {
1711 or_circuit_t *orcirc = TO_OR_CIRCUIT(circ);
1712 tor_assert(conn == orcirc->p_conn);
1713 return &orcirc->next_active_on_p_conn;
1717 /** Return a pointer to the "prev_active_on_{n,p}_conn" pointer of <b>circ</b>,
1718 * depending on whether <b>conn</b> matches n_conn or p_conn. */
1719 static INLINE circuit_t **
1720 prev_circ_on_conn_p(circuit_t *circ, or_connection_t *conn)
1722 tor_assert(circ);
1723 tor_assert(conn);
1724 if (conn == circ->n_conn) {
1725 return &circ->prev_active_on_n_conn;
1726 } else {
1727 or_circuit_t *orcirc = TO_OR_CIRCUIT(circ);
1728 tor_assert(conn == orcirc->p_conn);
1729 return &orcirc->prev_active_on_p_conn;
1733 /** Add <b>circ</b> to the list of circuits with pending cells on
1734 * <b>conn</b>. No effect if <b>circ</b> is already linked. */
1735 void
1736 make_circuit_active_on_conn(circuit_t *circ, or_connection_t *conn)
1738 circuit_t **nextp = next_circ_on_conn_p(circ, conn);
1739 circuit_t **prevp = prev_circ_on_conn_p(circ, conn);
1741 if (*nextp && *prevp) {
1742 /* Already active. */
1743 return;
1746 if (! conn->active_circuits) {
1747 conn->active_circuits = circ;
1748 *prevp = *nextp = circ;
1749 } else {
1750 circuit_t *head = conn->active_circuits;
1751 circuit_t *old_tail = *prev_circ_on_conn_p(head, conn);
1752 *next_circ_on_conn_p(old_tail, conn) = circ;
1753 *nextp = head;
1754 *prev_circ_on_conn_p(head, conn) = circ;
1755 *prevp = old_tail;
1757 assert_active_circuits_ok_paranoid(conn);
1760 /** Remove <b>circ</b> from the list of circuits with pending cells on
1761 * <b>conn</b>. No effect if <b>circ</b> is already unlinked. */
1762 void
1763 make_circuit_inactive_on_conn(circuit_t *circ, or_connection_t *conn)
1765 circuit_t **nextp = next_circ_on_conn_p(circ, conn);
1766 circuit_t **prevp = prev_circ_on_conn_p(circ, conn);
1767 circuit_t *next = *nextp, *prev = *prevp;
1769 if (!next && !prev) {
1770 /* Already inactive. */
1771 return;
1774 tor_assert(next && prev);
1775 tor_assert(*prev_circ_on_conn_p(next, conn) == circ);
1776 tor_assert(*next_circ_on_conn_p(prev, conn) == circ);
1778 if (next == circ) {
1779 conn->active_circuits = NULL;
1780 } else {
1781 *prev_circ_on_conn_p(next, conn) = prev;
1782 *next_circ_on_conn_p(prev, conn) = next;
1783 if (conn->active_circuits == circ)
1784 conn->active_circuits = next;
1786 *prevp = *nextp = NULL;
1787 assert_active_circuits_ok_paranoid(conn);
1790 /** Remove all circuits from the list of circuits with pending cells on
1791 * <b>conn</b>. */
1792 void
1793 connection_or_unlink_all_active_circs(or_connection_t *orconn)
1795 circuit_t *head = orconn->active_circuits;
1796 circuit_t *cur = head;
1797 if (! head)
1798 return;
1799 do {
1800 circuit_t *next = *next_circ_on_conn_p(cur, orconn);
1801 *prev_circ_on_conn_p(cur, orconn) = NULL;
1802 *next_circ_on_conn_p(cur, orconn) = NULL;
1803 cur = next;
1804 } while (cur != head);
1805 orconn->active_circuits = NULL;
1808 /** Block (if <b>block</b> is true) or unblock (if <b>block</b> is false)
1809 * every edge connection that is using <b>circ</b> to write to <b>orconn</b>,
1810 * and start or stop reading as appropriate. */
1811 static void
1812 set_streams_blocked_on_circ(circuit_t *circ, or_connection_t *orconn,
1813 int block)
1815 edge_connection_t *edge = NULL;
1816 if (circ->n_conn == orconn) {
1817 circ->streams_blocked_on_n_conn = block;
1818 if (CIRCUIT_IS_ORIGIN(circ))
1819 edge = TO_ORIGIN_CIRCUIT(circ)->p_streams;
1820 } else {
1821 circ->streams_blocked_on_p_conn = block;
1822 tor_assert(!CIRCUIT_IS_ORIGIN(circ));
1823 edge = TO_OR_CIRCUIT(circ)->n_streams;
1826 for (; edge; edge = edge->next_stream) {
1827 connection_t *conn = TO_CONN(edge);
1828 edge->edge_blocked_on_circ = block;
1830 if (!conn->read_event) {
1831 /* This connection is a placeholder for something; probably a DNS
1832 * request. It can't actually stop or start reading.*/
1833 continue;
1836 if (block) {
1837 if (connection_is_reading(conn))
1838 connection_stop_reading(conn);
1839 } else {
1840 /* Is this right? */
1841 if (!connection_is_reading(conn))
1842 connection_start_reading(conn);
1847 /** Pull as many cells as possible (but no more than <b>max</b>) from the
1848 * queue of the first active circuit on <b>conn</b>, and write then to
1849 * <b>conn</b>-&gt;outbuf. Return the number of cells written. Advance
1850 * the active circuit pointer to the next active circuit in the ring. */
1852 connection_or_flush_from_first_active_circuit(or_connection_t *conn, int max,
1853 time_t now)
1855 int n_flushed;
1856 cell_queue_t *queue;
1857 circuit_t *circ;
1858 int streams_blocked;
1859 circ = conn->active_circuits;
1860 if (!circ) return 0;
1861 assert_active_circuits_ok_paranoid(conn);
1862 if (circ->n_conn == conn) {
1863 queue = &circ->n_conn_cells;
1864 streams_blocked = circ->streams_blocked_on_n_conn;
1865 } else {
1866 queue = &TO_OR_CIRCUIT(circ)->p_conn_cells;
1867 streams_blocked = circ->streams_blocked_on_p_conn;
1869 tor_assert(*next_circ_on_conn_p(circ,conn));
1871 for (n_flushed = 0; n_flushed < max && queue->head; ) {
1872 packed_cell_t *cell = cell_queue_pop(queue);
1873 tor_assert(*next_circ_on_conn_p(circ,conn));
1875 /* Calculate the exact time that this cell has spent in the queue. */
1876 if (get_options()->CellStatistics && !CIRCUIT_IS_ORIGIN(circ)) {
1877 struct timeval now;
1878 uint32_t flushed;
1879 uint32_t cell_waiting_time;
1880 insertion_time_queue_t *it_queue = queue->insertion_times;
1881 tor_gettimeofday(&now);
1882 flushed = (now.tv_sec % SECONDS_IN_A_DAY) * 100L +
1883 now.tv_usec / 10000L;
1884 if (!it_queue || !it_queue->first) {
1885 log_warn(LD_BUG, "Cannot determine insertion time of cell.");
1886 } else {
1887 or_circuit_t *orcirc = TO_OR_CIRCUIT(circ);
1888 insertion_time_elem_t *elem = it_queue->first;
1889 cell_waiting_time = (flushed * 10L + SECONDS_IN_A_DAY * 1000L -
1890 elem->insertion_time * 10L) % (SECONDS_IN_A_DAY * 1000L);
1891 #undef SECONDS_IN_A_DAY
1892 elem->counter--;
1893 if (elem->counter < 1) {
1894 it_queue->first = elem->next;
1895 if (elem == it_queue->last)
1896 it_queue->last = NULL;
1897 mp_pool_release(elem);
1899 orcirc->total_cell_waiting_time += cell_waiting_time;
1900 orcirc->processed_cells++;
1904 /* If we just flushed our queue and this circuit is used for a
1905 * tunneled directory request, possibly advance its state. */
1906 if (queue->n == 0 && TO_CONN(conn)->dirreq_id)
1907 geoip_change_dirreq_state(TO_CONN(conn)->dirreq_id,
1908 DIRREQ_TUNNELED,
1909 DIRREQ_CIRC_QUEUE_FLUSHED);
1911 connection_write_to_buf(cell->body, CELL_NETWORK_SIZE, TO_CONN(conn));
1913 packed_cell_free(cell);
1914 ++n_flushed;
1915 if (circ != conn->active_circuits) {
1916 /* If this happens, the current circuit just got made inactive by
1917 * a call in connection_write_to_buf(). That's nothing to worry about:
1918 * circuit_make_inactive_on_conn() already advanced conn->active_circuits
1919 * for us.
1921 assert_active_circuits_ok_paranoid(conn);
1922 goto done;
1925 tor_assert(*next_circ_on_conn_p(circ,conn));
1926 assert_active_circuits_ok_paranoid(conn);
1927 conn->active_circuits = *next_circ_on_conn_p(circ, conn);
1929 /* Is the cell queue low enough to unblock all the streams that are waiting
1930 * to write to this circuit? */
1931 if (streams_blocked && queue->n <= CELL_QUEUE_LOWWATER_SIZE)
1932 set_streams_blocked_on_circ(circ, conn, 0); /* unblock streams */
1934 /* Did we just ran out of cells on this queue? */
1935 if (queue->n == 0) {
1936 log_debug(LD_GENERAL, "Made a circuit inactive.");
1937 make_circuit_inactive_on_conn(circ, conn);
1939 done:
1940 if (n_flushed)
1941 conn->timestamp_last_added_nonpadding = now;
1942 return n_flushed;
1945 /** Add <b>cell</b> to the queue of <b>circ</b> writing to <b>orconn</b>
1946 * transmitting in <b>direction</b>. */
1947 void
1948 append_cell_to_circuit_queue(circuit_t *circ, or_connection_t *orconn,
1949 cell_t *cell, cell_direction_t direction)
1951 cell_queue_t *queue;
1952 int streams_blocked;
1953 if (direction == CELL_DIRECTION_OUT) {
1954 queue = &circ->n_conn_cells;
1955 streams_blocked = circ->streams_blocked_on_n_conn;
1956 } else {
1957 or_circuit_t *orcirc = TO_OR_CIRCUIT(circ);
1958 queue = &orcirc->p_conn_cells;
1959 streams_blocked = circ->streams_blocked_on_p_conn;
1961 if (cell->command == CELL_RELAY_EARLY && orconn->link_proto < 2) {
1962 /* V1 connections don't understand RELAY_EARLY. */
1963 cell->command = CELL_RELAY;
1966 cell_queue_append_packed_copy(queue, cell);
1968 /* If we have too many cells on the circuit, we should stop reading from
1969 * the edge streams for a while. */
1970 if (!streams_blocked && queue->n >= CELL_QUEUE_HIGHWATER_SIZE)
1971 set_streams_blocked_on_circ(circ, orconn, 1); /* block streams */
1973 if (queue->n == 1) {
1974 /* This was the first cell added to the queue. We need to make this
1975 * circuit active. */
1976 log_debug(LD_GENERAL, "Made a circuit active.");
1977 make_circuit_active_on_conn(circ, orconn);
1980 if (! buf_datalen(orconn->_base.outbuf)) {
1981 /* There is no data at all waiting to be sent on the outbuf. Add a
1982 * cell, so that we can notice when it gets flushed, flushed_some can
1983 * get called, and we can start putting more data onto the buffer then.
1985 log_debug(LD_GENERAL, "Primed a buffer.");
1986 connection_or_flush_from_first_active_circuit(orconn, 1, approx_time());
1990 /** Append an encoded value of <b>addr</b> to <b>payload_out</b>, which must
1991 * have at least 18 bytes of free space. The encoding is, as specified in
1992 * tor-spec.txt:
1993 * RESOLVED_TYPE_IPV4 or RESOLVED_TYPE_IPV6 [1 byte]
1994 * LENGTH [1 byte]
1995 * ADDRESS [length bytes]
1996 * Return the number of bytes added, or -1 on error */
1998 append_address_to_payload(char *payload_out, const tor_addr_t *addr)
2000 uint32_t a;
2001 switch (tor_addr_family(addr)) {
2002 case AF_INET:
2003 payload_out[0] = RESOLVED_TYPE_IPV4;
2004 payload_out[1] = 4;
2005 a = tor_addr_to_ipv4n(addr);
2006 memcpy(payload_out+2, &a, 4);
2007 return 6;
2008 case AF_INET6:
2009 payload_out[0] = RESOLVED_TYPE_IPV6;
2010 payload_out[1] = 16;
2011 memcpy(payload_out+2, tor_addr_to_in6_addr8(addr), 16);
2012 return 18;
2013 case AF_UNSPEC:
2014 default:
2015 return -1;
2019 /** Given <b>payload_len</b> bytes at <b>payload</b>, starting with an address
2020 * encoded as by append_address_to_payload(), try to decode the address into
2021 * *<b>addr_out</b>. Return the next byte in the payload after the address on
2022 * success, or NULL on failure. */
2023 const char *
2024 decode_address_from_payload(tor_addr_t *addr_out, const char *payload,
2025 int payload_len)
2027 if (payload_len < 2)
2028 return NULL;
2029 if (payload_len < 2+(uint8_t)payload[1])
2030 return NULL;
2032 switch (payload[0]) {
2033 case RESOLVED_TYPE_IPV4:
2034 if (payload[1] != 4)
2035 return NULL;
2036 tor_addr_from_ipv4n(addr_out, get_uint32(payload+2));
2037 break;
2038 case RESOLVED_TYPE_IPV6:
2039 if (payload[1] != 16)
2040 return NULL;
2041 tor_addr_from_ipv6_bytes(addr_out, payload+2);
2042 break;
2043 default:
2044 tor_addr_make_unspec(addr_out);
2045 break;
2047 return payload + 2 + (uint8_t)payload[1];
2050 /** Fail with an assert if the active circuits ring on <b>orconn</b> is
2051 * corrupt. */
2052 void
2053 assert_active_circuits_ok(or_connection_t *orconn)
2055 circuit_t *head = orconn->active_circuits;
2056 circuit_t *cur = head;
2057 if (! head)
2058 return;
2059 do {
2060 circuit_t *next = *next_circ_on_conn_p(cur, orconn);
2061 circuit_t *prev = *prev_circ_on_conn_p(cur, orconn);
2062 tor_assert(next);
2063 tor_assert(prev);
2064 tor_assert(*next_circ_on_conn_p(prev, orconn) == cur);
2065 tor_assert(*prev_circ_on_conn_p(next, orconn) == cur);
2066 cur = next;
2067 } while (cur != head);