relay.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  1. /* Copyright 2001 Matej Pfajfar.
  2. * Copyright 2001-2004 Roger Dingledine.
  3. * Copyright 2004 Roger Dingledine, Nick Mathewson. */
  4. /* See LICENSE for licensing information */
  5. /* $Id$ */
  6. /**
  7. * \file relay.c
  8. * \brief Handle relay cell encryption/decryption, plus packaging and
  9. * receiving from circuits.
  10. **/
  11. #include "or.h"
  12. static int relay_crypt(circuit_t *circ, cell_t *cell, int cell_direction,
  13. crypt_path_t **layer_hint, char *recognized);
  14. static connection_t *relay_lookup_conn(circuit_t *circ, cell_t *cell, int cell_direction);
  15. static int
  16. connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ,
  17. connection_t *conn,
  18. crypt_path_t *layer_hint);
  19. static void
  20. circuit_consider_sending_sendme(circuit_t *circ, crypt_path_t *layer_hint);
  21. static void
  22. circuit_resume_edge_reading(circuit_t *circ, crypt_path_t *layer_hint);
  23. static int
  24. circuit_resume_edge_reading_helper(connection_t *conn,
  25. circuit_t *circ,
  26. crypt_path_t *layer_hint);
  27. static int
  28. circuit_consider_stop_edge_reading(circuit_t *circ, crypt_path_t *layer_hint);
  29. /** Stats: how many relay cells have originated at this hop, or have
  30. * been relayed onward (not recognized at this hop)?
  31. */
  32. unsigned long stats_n_relay_cells_relayed = 0;
  33. /** Stats: how many relay cells have been delivered to streams at this
  34. * hop?
  35. */
  36. unsigned long stats_n_relay_cells_delivered = 0;
  37. /** Update digest from the payload of cell. Assign integrity part to
  38. * cell.
  39. */
  40. static void relay_set_digest(crypto_digest_env_t *digest, cell_t *cell) {
  41. char integrity[4];
  42. relay_header_t rh;
  43. crypto_digest_add_bytes(digest, cell->payload, CELL_PAYLOAD_SIZE);
  44. crypto_digest_get_digest(digest, integrity, 4);
  45. // log_fn(LOG_DEBUG,"Putting digest of %u %u %u %u into relay cell.",
  46. // integrity[0], integrity[1], integrity[2], integrity[3]);
  47. relay_header_unpack(&rh, cell->payload);
  48. memcpy(rh.integrity, integrity, 4);
  49. relay_header_pack(cell->payload, &rh);
  50. }
  51. /** Does the digest for this circuit indicate that this cell is for us?
  52. *
  53. * Update digest from the payload of cell (with the integrity part set
  54. * to 0). If the integrity part is valid, return 1, else restore digest
  55. * and cell to their original state and return 0.
  56. */
  57. static int relay_digest_matches(crypto_digest_env_t *digest, cell_t *cell) {
  58. char received_integrity[4], calculated_integrity[4];
  59. relay_header_t rh;
  60. crypto_digest_env_t *backup_digest=NULL;
  61. backup_digest = crypto_digest_dup(digest);
  62. relay_header_unpack(&rh, cell->payload);
  63. memcpy(received_integrity, rh.integrity, 4);
  64. memset(rh.integrity, 0, 4);
  65. relay_header_pack(cell->payload, &rh);
  66. // log_fn(LOG_DEBUG,"Reading digest of %u %u %u %u from relay cell.",
  67. // received_integrity[0], received_integrity[1],
  68. // received_integrity[2], received_integrity[3]);
  69. crypto_digest_add_bytes(digest, cell->payload, CELL_PAYLOAD_SIZE);
  70. crypto_digest_get_digest(digest, calculated_integrity, 4);
  71. if(memcmp(received_integrity, calculated_integrity, 4)) {
  72. // log_fn(LOG_INFO,"Recognized=0 but bad digest. Not recognizing.");
  73. // (%d vs %d).", received_integrity, calculated_integrity);
  74. /* restore digest to its old form */
  75. crypto_digest_assign(digest, backup_digest);
  76. /* restore the relay header */
  77. memcpy(rh.integrity, received_integrity, 4);
  78. relay_header_pack(cell->payload, &rh);
  79. crypto_free_digest_env(backup_digest);
  80. return 0;
  81. }
  82. crypto_free_digest_env(backup_digest);
  83. return 1;
  84. }
  85. /** Apply <b>cipher</b> to CELL_PAYLOAD_SIZE bytes of <b>in</b>
  86. * (in place).
  87. *
  88. * If <b>encrypt_mode</b> is 1 then encrypt, else decrypt.
  89. *
  90. * Return -1 if the crypto fails, else return 0.
  91. */
  92. static int relay_crypt_one_payload(crypto_cipher_env_t *cipher, char *in,
  93. int encrypt_mode) {
  94. char out[CELL_PAYLOAD_SIZE]; /* 'in' must be this size too */
  95. relay_header_t rh;
  96. relay_header_unpack(&rh, in);
  97. // log_fn(LOG_DEBUG,"before crypt: %d",rh.recognized);
  98. if(( encrypt_mode && crypto_cipher_encrypt(cipher, out, in, CELL_PAYLOAD_SIZE)) ||
  99. (!encrypt_mode && crypto_cipher_decrypt(cipher, out, in, CELL_PAYLOAD_SIZE))) {
  100. log_fn(LOG_WARN,"Error during relay encryption");
  101. return -1;
  102. }
  103. memcpy(in,out,CELL_PAYLOAD_SIZE);
  104. relay_header_unpack(&rh, in);
  105. // log_fn(LOG_DEBUG,"after crypt: %d",rh.recognized);
  106. return 0;
  107. }
  108. /** Receive a relay cell:
  109. * - Crypt it (encrypt APward, decrypt at AP, decrypt exitward).
  110. * - Check if recognized (if exitward).
  111. * - If recognized and the digest checks out, then find if there's
  112. * a conn that the cell is intended for, and deliver it to·
  113. * connection_edge.
  114. * - Else connection_or_write_cell_to_buf to the conn on the other
  115. * side of the circuit.
  116. */
  117. int circuit_receive_relay_cell(cell_t *cell, circuit_t *circ,
  118. int cell_direction) {
  119. connection_t *conn=NULL;
  120. crypt_path_t *layer_hint=NULL;
  121. char recognized=0;
  122. tor_assert(cell);
  123. tor_assert(circ);
  124. tor_assert(cell_direction == CELL_DIRECTION_OUT ||
  125. cell_direction == CELL_DIRECTION_IN);
  126. if (circ->marked_for_close)
  127. return 0;
  128. if(relay_crypt(circ, cell, cell_direction, &layer_hint, &recognized) < 0) {
  129. log_fn(LOG_WARN,"relay crypt failed. Dropping connection.");
  130. return -1;
  131. }
  132. if(recognized) {
  133. conn = relay_lookup_conn(circ, cell, cell_direction);
  134. if(cell_direction == CELL_DIRECTION_OUT) {
  135. ++stats_n_relay_cells_delivered;
  136. log_fn(LOG_DEBUG,"Sending away from origin.");
  137. if (connection_edge_process_relay_cell(cell, circ, conn, NULL) < 0) {
  138. log_fn(LOG_WARN,"connection_edge_process_relay_cell (away from origin) failed.");
  139. return -1;
  140. }
  141. }
  142. if(cell_direction == CELL_DIRECTION_IN) {
  143. ++stats_n_relay_cells_delivered;
  144. log_fn(LOG_DEBUG,"Sending to origin.");
  145. if (connection_edge_process_relay_cell(cell, circ, conn, layer_hint) < 0) {
  146. log_fn(LOG_WARN,"connection_edge_process_relay_cell (at origin) failed.");
  147. return -1;
  148. }
  149. }
  150. return 0;
  151. }
  152. /* not recognized. pass it on. */
  153. if(cell_direction == CELL_DIRECTION_OUT) {
  154. cell->circ_id = circ->n_circ_id; /* switch it */
  155. conn = circ->n_conn;
  156. } else {
  157. cell->circ_id = circ->p_circ_id; /* switch it */
  158. conn = circ->p_conn;
  159. }
  160. if(!conn) {
  161. if (circ->rend_splice && cell_direction == CELL_DIRECTION_OUT) {
  162. tor_assert(circ->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED);
  163. tor_assert(circ->rend_splice->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED);
  164. cell->circ_id = circ->rend_splice->p_circ_id;
  165. if (circuit_receive_relay_cell(cell, circ->rend_splice, CELL_DIRECTION_IN)<0) {
  166. log_fn(LOG_WARN, "Error relaying cell across rendezvous; closing circuits");
  167. circuit_mark_for_close(circ); /* XXXX Do this here, or just return -1? */
  168. return -1;
  169. }
  170. return 0;
  171. }
  172. log_fn(LOG_WARN,"Didn't recognize cell, but circ stops here! Closing circ.");
  173. return -1;
  174. }
  175. log_fn(LOG_DEBUG,"Passing on unrecognized cell.");
  176. ++stats_n_relay_cells_relayed;
  177. connection_or_write_cell_to_buf(cell, conn);
  178. return 0;
  179. }
  180. /** Do the appropriate en/decryptions for <b>cell</b> arriving on
  181. * <b>circ</b> in direction <b>cell_direction</b>.
  182. *
  183. * If cell_direction == CELL_DIRECTION_IN:
  184. * - If we're at the origin (we're the OP), for hops 1..N,
  185. * decrypt cell. If recognized, stop.
  186. * - Else (we're not the OP), encrypt one hop. Cell is not recognized.
  187. *
  188. * If cell_direction == CELL_DIRECTION_OUT:
  189. * - decrypt one hop. Check if recognized.
  190. *
  191. * If cell is recognized, set *recognized to 1, and set
  192. * *layer_hint to the hop that recognized it.
  193. *
  194. * Return -1 to indicate that we should mark the circuit for close,
  195. * else return 0.
  196. */
  197. /* wrap this into receive_relay_cell one day */
  198. static int relay_crypt(circuit_t *circ, cell_t *cell, int cell_direction,
  199. crypt_path_t **layer_hint, char *recognized) {
  200. crypt_path_t *thishop;
  201. relay_header_t rh;
  202. tor_assert(circ);
  203. tor_assert(cell);
  204. tor_assert(recognized);
  205. tor_assert(cell_direction == CELL_DIRECTION_IN ||
  206. cell_direction == CELL_DIRECTION_OUT);
  207. if(cell_direction == CELL_DIRECTION_IN) {
  208. if(CIRCUIT_IS_ORIGIN(circ)) { /* We're at the beginning of the circuit.
  209. We'll want to do layered decrypts. */
  210. tor_assert(circ->cpath);
  211. thishop = circ->cpath;
  212. if(thishop->state != CPATH_STATE_OPEN) {
  213. log_fn(LOG_WARN,"Relay cell before first created cell? Closing.");
  214. return -1;
  215. }
  216. do { /* Remember: cpath is in forward order, that is, first hop first. */
  217. tor_assert(thishop);
  218. if(relay_crypt_one_payload(thishop->b_crypto, cell->payload, 0) < 0)
  219. return -1;
  220. relay_header_unpack(&rh, cell->payload);
  221. if(rh.recognized == 0) {
  222. /* it's possibly recognized. have to check digest to be sure. */
  223. if(relay_digest_matches(thishop->b_digest, cell)) {
  224. *recognized = 1;
  225. *layer_hint = thishop;
  226. return 0;
  227. }
  228. }
  229. thishop = thishop->next;
  230. } while(thishop != circ->cpath && thishop->state == CPATH_STATE_OPEN);
  231. log_fn(LOG_WARN,"in-cell at OP not recognized. Closing.");
  232. return -1;
  233. } else { /* we're in the middle. Just one crypt. */
  234. if(relay_crypt_one_payload(circ->p_crypto, cell->payload, 1) < 0)
  235. return -1;
  236. // log_fn(LOG_DEBUG,"Skipping recognized check, because we're not the OP.");
  237. }
  238. } else /* cell_direction == CELL_DIRECTION_OUT */ {
  239. /* we're in the middle. Just one crypt. */
  240. if(relay_crypt_one_payload(circ->n_crypto, cell->payload, 0) < 0)
  241. return -1;
  242. relay_header_unpack(&rh, cell->payload);
  243. if (rh.recognized == 0) {
  244. /* it's possibly recognized. have to check digest to be sure. */
  245. if(relay_digest_matches(circ->n_digest, cell)) {
  246. *recognized = 1;
  247. return 0;
  248. }
  249. }
  250. }
  251. return 0;
  252. }
  253. /** Package a relay cell:
  254. * - Encrypt it to the right layer
  255. * - connection_or_write_cell_to_buf to the right conn
  256. */
  257. static int
  258. circuit_package_relay_cell(cell_t *cell, circuit_t *circ,
  259. int cell_direction,
  260. crypt_path_t *layer_hint)
  261. {
  262. connection_t *conn; /* where to send the cell */
  263. crypt_path_t *thishop; /* counter for repeated crypts */
  264. if(cell_direction == CELL_DIRECTION_OUT) {
  265. conn = circ->n_conn;
  266. if(!conn) {
  267. log_fn(LOG_WARN,"outgoing relay cell has n_conn==NULL. Dropping.");
  268. return 0; /* just drop it */
  269. }
  270. relay_set_digest(layer_hint->f_digest, cell);
  271. thishop = layer_hint;
  272. /* moving from farthest to nearest hop */
  273. do {
  274. tor_assert(thishop);
  275. log_fn(LOG_DEBUG,"crypting a layer of the relay cell.");
  276. if(relay_crypt_one_payload(thishop->f_crypto, cell->payload, 1) < 0) {
  277. return -1;
  278. }
  279. thishop = thishop->prev;
  280. } while (thishop != circ->cpath->prev);
  281. } else { /* incoming cell */
  282. conn = circ->p_conn;
  283. if(!conn) {
  284. log_fn(LOG_WARN,"incoming relay cell has p_conn==NULL. Dropping.");
  285. return 0; /* just drop it */
  286. }
  287. relay_set_digest(circ->p_digest, cell);
  288. if(relay_crypt_one_payload(circ->p_crypto, cell->payload, 1) < 0)
  289. return -1;
  290. }
  291. ++stats_n_relay_cells_relayed;
  292. connection_or_write_cell_to_buf(cell, conn);
  293. return 0;
  294. }
  295. /** If cell's stream_id matches the stream_id of any conn that's
  296. * attached to circ, return that conn, else return NULL.
  297. */
  298. static connection_t *
  299. relay_lookup_conn(circuit_t *circ, cell_t *cell, int cell_direction)
  300. {
  301. connection_t *tmpconn;
  302. relay_header_t rh;
  303. relay_header_unpack(&rh, cell->payload);
  304. if(!rh.stream_id)
  305. return NULL;
  306. /* IN or OUT cells could have come from either direction, now
  307. * that we allow rendezvous *to* an OP.
  308. */
  309. for(tmpconn = circ->n_streams; tmpconn; tmpconn=tmpconn->next_stream) {
  310. if(rh.stream_id == tmpconn->stream_id) {
  311. log_fn(LOG_DEBUG,"found conn for stream %d.", rh.stream_id);
  312. if(cell_direction == CELL_DIRECTION_OUT ||
  313. connection_edge_is_rendezvous_stream(tmpconn))
  314. return tmpconn;
  315. }
  316. }
  317. for(tmpconn = circ->p_streams; tmpconn; tmpconn=tmpconn->next_stream) {
  318. if(rh.stream_id == tmpconn->stream_id) {
  319. log_fn(LOG_DEBUG,"found conn for stream %d.", rh.stream_id);
  320. return tmpconn;
  321. }
  322. }
  323. for(tmpconn = circ->resolving_streams; tmpconn; tmpconn=tmpconn->next_stream) {
  324. if(rh.stream_id == tmpconn->stream_id) {
  325. log_fn(LOG_DEBUG,"found conn for stream %d.", rh.stream_id);
  326. return tmpconn;
  327. }
  328. }
  329. return NULL; /* probably a begin relay cell */
  330. }
  331. /** Pack the relay_header_t host-order structure <b>src</b> into
  332. * network-order in the buffer <b>dest</b>. See tor-spec.txt for details
  333. * about the wire format.
  334. */
  335. void relay_header_pack(char *dest, const relay_header_t *src) {
  336. *(uint8_t*)(dest) = src->command;
  337. set_uint16(dest+1, htons(src->recognized));
  338. set_uint16(dest+3, htons(src->stream_id));
  339. memcpy(dest+5, src->integrity, 4);
  340. set_uint16(dest+9, htons(src->length));
  341. }
  342. /** Unpack the network-order buffer <b>src</b> into a host-order
  343. * relay_header_t structure <b>dest</b>.
  344. */
  345. void relay_header_unpack(relay_header_t *dest, const char *src) {
  346. dest->command = *(uint8_t*)(src);
  347. dest->recognized = ntohs(get_uint16(src+1));
  348. dest->stream_id = ntohs(get_uint16(src+3));
  349. memcpy(dest->integrity, src+5, 4);
  350. dest->length = ntohs(get_uint16(src+9));
  351. }
  352. /** Make a relay cell out of <b>relay_command</b> and <b>payload</b>, and
  353. * send it onto the open circuit <b>circ</b>. <b>fromconn</b> is the stream
  354. * that's sending the relay cell, or NULL if it's a control cell.
  355. * <b>cpath_layer</b> is NULL for OR->OP cells, or the destination hop
  356. * for OP->OR cells.
  357. *
  358. * If you can't send the cell, mark the circuit for close and
  359. * return -1. Else return 0.
  360. */
  361. int connection_edge_send_command(connection_t *fromconn, circuit_t *circ,
  362. int relay_command, const char *payload,
  363. size_t payload_len, crypt_path_t *cpath_layer) {
  364. cell_t cell;
  365. relay_header_t rh;
  366. int cell_direction;
  367. if(!circ) {
  368. log_fn(LOG_WARN,"no circ. Closing conn.");
  369. tor_assert(fromconn);
  370. fromconn->has_sent_end = 1; /* no circ to send to */
  371. connection_mark_for_close(fromconn);
  372. return -1;
  373. }
  374. memset(&cell, 0, sizeof(cell_t));
  375. cell.command = CELL_RELAY;
  376. if(cpath_layer) {
  377. cell.circ_id = circ->n_circ_id;
  378. cell_direction = CELL_DIRECTION_OUT;
  379. } else {
  380. cell.circ_id = circ->p_circ_id;
  381. cell_direction = CELL_DIRECTION_IN;
  382. }
  383. memset(&rh, 0, sizeof(rh));
  384. rh.command = relay_command;
  385. if(fromconn)
  386. rh.stream_id = fromconn->stream_id; /* else it's 0 */
  387. rh.length = payload_len;
  388. relay_header_pack(cell.payload, &rh);
  389. if(payload_len)
  390. memcpy(cell.payload+RELAY_HEADER_SIZE, payload, payload_len);
  391. log_fn(LOG_DEBUG,"delivering %d cell %s.", relay_command,
  392. cell_direction == CELL_DIRECTION_OUT ? "forward" : "backward");
  393. if(circuit_package_relay_cell(&cell, circ, cell_direction, cpath_layer) < 0) {
  394. log_fn(LOG_WARN,"circuit_package_relay_cell failed. Closing.");
  395. circuit_mark_for_close(circ);
  396. return -1;
  397. }
  398. return 0;
  399. }
  400. /** Translate the <b>payload</b> of length <b>length</b>, which
  401. * came from a relay 'end' cell, into a static const string describing
  402. * why the stream is closing.
  403. */
  404. static const char *
  405. connection_edge_end_reason(char *payload, uint16_t length) {
  406. if(length < 1) {
  407. log_fn(LOG_WARN,"End cell arrived with length 0. Should be at least 1.");
  408. return "MALFORMED";
  409. }
  410. if(*payload < _MIN_END_STREAM_REASON || *payload > _MAX_END_STREAM_REASON) {
  411. log_fn(LOG_WARN,"Reason for ending (%d) not recognized.",*payload);
  412. return "MALFORMED";
  413. }
  414. switch(*payload) {
  415. case END_STREAM_REASON_MISC: return "misc error";
  416. case END_STREAM_REASON_RESOLVEFAILED: return "resolve failed";
  417. case END_STREAM_REASON_CONNECTFAILED: return "connect failed";
  418. case END_STREAM_REASON_EXITPOLICY: return "exit policy failed";
  419. case END_STREAM_REASON_DESTROY: return "destroyed";
  420. case END_STREAM_REASON_DONE: return "closed normally";
  421. case END_STREAM_REASON_TIMEOUT: return "gave up (timeout)";
  422. }
  423. tor_assert(0);
  424. return "";
  425. }
  426. /** How many times will I retry a stream that fails due to DNS
  427. * resolve failure?
  428. */
  429. #define MAX_RESOLVE_FAILURES 3
  430. /** An incoming relay cell has arrived from circuit <b>circ</b> to
  431. * stream <b>conn</b>.
  432. *
  433. * The arguments here are the same as in
  434. * connection_edge_process_relay_cell() below; this function is called
  435. * from there when <b>conn</b> is defined and not in an open state.
  436. */
  437. static int
  438. connection_edge_process_relay_cell_not_open(
  439. relay_header_t *rh, cell_t *cell, circuit_t *circ,
  440. connection_t *conn, crypt_path_t *layer_hint) {
  441. uint32_t addr;
  442. int reason;
  443. routerinfo_t *exitrouter;
  444. if(rh->command == RELAY_COMMAND_END) {
  445. reason = *(cell->payload+RELAY_HEADER_SIZE);
  446. /* We have to check this here, since we aren't connected yet. */
  447. if (rh->length >= 5 && reason == END_STREAM_REASON_EXITPOLICY) {
  448. if(conn->type != CONN_TYPE_AP) {
  449. log_fn(LOG_WARN,"Got an end because of exitpolicy, but we're not an AP. Closing.");
  450. return -1;
  451. }
  452. addr = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+1));
  453. if(addr) {
  454. log_fn(LOG_INFO,"Address %s refused due to exit policy. Retrying.",
  455. conn->socks_request->address);
  456. } else {
  457. log_fn(LOG_INFO,"Address %s resolved to 0.0.0.0. Closing,",
  458. conn->socks_request->address);
  459. conn->has_sent_end = 1; /* we just got an 'end', don't need to send one */
  460. connection_mark_for_close(conn);
  461. return 0;
  462. }
  463. client_dns_set_entry(conn->socks_request->address, addr);
  464. /* check if he *ought* to have allowed it */
  465. exitrouter = router_get_by_digest(circ->build_state->chosen_exit_digest);
  466. if(!exitrouter) {
  467. log_fn(LOG_INFO,"Skipping broken circ (exit router vanished)");
  468. return 0; /* this circuit is screwed and doesn't know it yet */
  469. }
  470. if(connection_ap_can_use_exit(conn, exitrouter)) {
  471. log_fn(LOG_WARN,"Exitrouter %s seems to be more restrictive than its exit policy. Not using this router as exit for now,", exitrouter->nickname);
  472. exit_policy_free(exitrouter->exit_policy);
  473. exitrouter->exit_policy =
  474. router_parse_exit_policy_from_string("reject *:*");
  475. }
  476. conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
  477. circuit_detach_stream(circ,conn);
  478. if(connection_ap_handshake_attach_circuit(conn) >= 0)
  479. return 0;
  480. log_fn(LOG_INFO,"Giving up on retrying (from exitpolicy); conn can't be handled.");
  481. /* else, conn will get closed below */
  482. } else if (rh->length && reason == END_STREAM_REASON_RESOLVEFAILED) {
  483. if (client_dns_incr_failures(conn->socks_request->address)
  484. < MAX_RESOLVE_FAILURES) {
  485. /* We haven't retried too many times; reattach the connection. */
  486. log_fn(LOG_INFO,"Resolve of '%s' failed, trying again.",
  487. conn->socks_request->address);
  488. circuit_log_path(LOG_INFO,circ);
  489. conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
  490. circuit_detach_stream(circ,conn);
  491. tor_assert(circ->timestamp_dirty);
  492. circ->timestamp_dirty -= get_options()->NewCircuitPeriod;
  493. /* make sure not to expire/retry the stream quite yet */
  494. conn->timestamp_lastread = time(NULL);
  495. if(connection_ap_handshake_attach_circuit(conn) >= 0)
  496. return 0;
  497. /* else, conn will get closed below */
  498. log_fn(LOG_INFO,"Giving up on retrying (from resolvefailed); conn can't be handled.");
  499. } else {
  500. log_fn(LOG_WARN,"Have tried resolving address %s at %d different places. Giving up.",
  501. conn->socks_request->address, MAX_RESOLVE_FAILURES);
  502. }
  503. }
  504. log_fn(LOG_INFO,"Edge got end (%s) before we're connected. Marking for close.",
  505. connection_edge_end_reason(cell->payload+RELAY_HEADER_SIZE, rh->length));
  506. if(CIRCUIT_IS_ORIGIN(circ))
  507. circuit_log_path(LOG_INFO,circ);
  508. conn->has_sent_end = 1; /* we just got an 'end', don't need to send one */
  509. connection_mark_for_close(conn);
  510. return 0;
  511. }
  512. if(conn->type == CONN_TYPE_AP && rh->command == RELAY_COMMAND_CONNECTED) {
  513. if(conn->state != AP_CONN_STATE_CONNECT_WAIT) {
  514. log_fn(LOG_WARN,"Got 'connected' while not in state connect_wait. Dropping.");
  515. return 0;
  516. }
  517. // log_fn(LOG_INFO,"Connected! Notifying application.");
  518. conn->state = AP_CONN_STATE_OPEN;
  519. log_fn(LOG_INFO,"'connected' received after %d seconds.",
  520. (int)(time(NULL) - conn->timestamp_lastread));
  521. if (rh->length >= 4) {
  522. addr = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE));
  523. if(!addr) {
  524. log_fn(LOG_INFO,"...but it claims the IP address was 0.0.0.0. Closing.");
  525. connection_edge_end(conn, END_STREAM_REASON_MISC, conn->cpath_layer);
  526. connection_mark_for_close(conn);
  527. return 0;
  528. }
  529. client_dns_set_entry(conn->socks_request->address, addr);
  530. }
  531. circuit_log_path(LOG_INFO,circ);
  532. connection_ap_handshake_socks_reply(conn, NULL, 0, 1);
  533. conn->socks_request->has_finished = 1;
  534. /* handle anything that might have queued */
  535. if (connection_edge_package_raw_inbuf(conn) < 0) {
  536. connection_edge_end(conn, END_STREAM_REASON_MISC, conn->cpath_layer);
  537. connection_mark_for_close(conn);
  538. return 0;
  539. }
  540. return 0;
  541. }
  542. if(conn->type == CONN_TYPE_AP && rh->command == RELAY_COMMAND_RESOLVED) {
  543. if (conn->state != AP_CONN_STATE_RESOLVE_WAIT) {
  544. log_fn(LOG_WARN,"Got a 'resolved' cell while not in state resolve_wait. Dropping.");
  545. return 0;
  546. }
  547. tor_assert(conn->socks_request->command == SOCKS_COMMAND_RESOLVE);
  548. if (rh->length < 2 || cell->payload[RELAY_HEADER_SIZE+1]+2>rh->length) {
  549. log_fn(LOG_WARN, "Dropping malformed 'resolved' cell");
  550. conn->has_sent_end = 1;
  551. connection_mark_for_close(conn);
  552. return 0;
  553. }
  554. connection_ap_handshake_socks_resolved(conn,
  555. cell->payload[RELAY_HEADER_SIZE], /*answer_type*/
  556. cell->payload[RELAY_HEADER_SIZE+1], /*answer_len*/
  557. cell->payload+RELAY_HEADER_SIZE+2); /* answer */
  558. conn->socks_request->has_finished = 1;
  559. conn->has_sent_end = 1;
  560. connection_mark_for_close(conn);
  561. conn->hold_open_until_flushed = 1;
  562. return 0;
  563. }
  564. log_fn(LOG_WARN,"Got an unexpected relay command %d, in state %d (%s). Closing.",
  565. rh->command, conn->state, conn_state_to_string[conn->type][conn->state]);
  566. connection_edge_end(conn, END_STREAM_REASON_MISC, conn->cpath_layer);
  567. connection_mark_for_close(conn);
  568. return -1;
  569. }
  570. /** An incoming relay cell has arrived on circuit <b>circ</b>. If
  571. * <b>conn</b> is NULL this is a control cell, else <b>cell</b> is
  572. * destined for <b>conn</b>.
  573. *
  574. * If <b>layer_hint</b> is defined, then we're the origin of the
  575. * circuit, and it specifies the hop that packaged <b>cell</b>.
  576. *
  577. * Return -1 if you want to tear down the circuit, else 0.
  578. */
  579. static int
  580. connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ,
  581. connection_t *conn,
  582. crypt_path_t *layer_hint)
  583. {
  584. static int num_seen=0;
  585. relay_header_t rh;
  586. tor_assert(cell);
  587. tor_assert(circ);
  588. relay_header_unpack(&rh, cell->payload);
  589. // log_fn(LOG_DEBUG,"command %d stream %d", rh.command, rh.stream_id);
  590. num_seen++;
  591. log_fn(LOG_DEBUG,"Now seen %d relay cells here.", num_seen);
  592. /* either conn is NULL, in which case we've got a control cell, or else
  593. * conn points to the recognized stream. */
  594. if(conn &&
  595. conn->state != AP_CONN_STATE_OPEN &&
  596. conn->state != EXIT_CONN_STATE_OPEN) {
  597. return connection_edge_process_relay_cell_not_open(
  598. &rh, cell, circ, conn, layer_hint);
  599. }
  600. switch(rh.command) {
  601. case RELAY_COMMAND_DROP:
  602. log_fn(LOG_INFO,"Got a relay-level padding cell. Dropping.");
  603. return 0;
  604. case RELAY_COMMAND_BEGIN:
  605. if (layer_hint &&
  606. circ->purpose != CIRCUIT_PURPOSE_S_REND_JOINED) {
  607. log_fn(LOG_WARN,"relay begin request unsupported at AP. Dropping.");
  608. return 0;
  609. }
  610. if(conn) {
  611. log_fn(LOG_WARN,"begin cell for known stream. Dropping.");
  612. return 0;
  613. }
  614. connection_exit_begin_conn(cell, circ);
  615. return 0;
  616. case RELAY_COMMAND_DATA:
  617. ++stats_n_data_cells_received;
  618. if((layer_hint && --layer_hint->deliver_window < 0) ||
  619. (!layer_hint && --circ->deliver_window < 0)) {
  620. log_fn(LOG_WARN,"(relay data) circ deliver_window below 0. Killing.");
  621. connection_edge_end(conn, END_STREAM_REASON_MISC, conn->cpath_layer);
  622. connection_mark_for_close(conn);
  623. return -1;
  624. }
  625. log_fn(LOG_DEBUG,"circ deliver_window now %d.", layer_hint ?
  626. layer_hint->deliver_window : circ->deliver_window);
  627. circuit_consider_sending_sendme(circ, layer_hint);
  628. if(!conn) {
  629. log_fn(LOG_INFO,"data cell dropped, unknown stream.");
  630. return 0;
  631. }
  632. if(--conn->deliver_window < 0) { /* is it below 0 after decrement? */
  633. log_fn(LOG_WARN,"(relay data) conn deliver_window below 0. Killing.");
  634. return -1; /* somebody's breaking protocol. kill the whole circuit. */
  635. }
  636. stats_n_data_bytes_received += rh.length;
  637. connection_write_to_buf(cell->payload + RELAY_HEADER_SIZE,
  638. rh.length, conn);
  639. connection_edge_consider_sending_sendme(conn);
  640. return 0;
  641. case RELAY_COMMAND_END:
  642. if(!conn) {
  643. log_fn(LOG_INFO,"end cell (%s) dropped, unknown stream.",
  644. connection_edge_end_reason(cell->payload+RELAY_HEADER_SIZE, rh.length));
  645. return 0;
  646. }
  647. /* XXX add to this log_fn the exit node's nickname? */
  648. log_fn(LOG_INFO,"end cell (%s) for stream %d. Removing stream.",
  649. connection_edge_end_reason(cell->payload+RELAY_HEADER_SIZE, rh.length),
  650. conn->stream_id);
  651. #ifdef HALF_OPEN
  652. conn->done_sending = 1;
  653. shutdown(conn->s, 1); /* XXX check return; refactor NM */
  654. if (conn->done_receiving) {
  655. /* We just *got* an end; no reason to send one. */
  656. conn->has_sent_end = 1;
  657. connection_mark_for_close(conn);
  658. conn->hold_open_until_flushed = 1;
  659. }
  660. #else
  661. /* We just *got* an end; no reason to send one. */
  662. conn->has_sent_end = 1;
  663. if(!conn->marked_for_close) {
  664. /* only mark it if not already marked. it's possible to
  665. * get the 'end' right around when the client hangs up on us. */
  666. connection_mark_for_close(conn);
  667. }
  668. conn->hold_open_until_flushed = 1;
  669. #endif
  670. return 0;
  671. case RELAY_COMMAND_EXTEND:
  672. if(conn) {
  673. log_fn(LOG_WARN,"'extend' for non-zero stream. Dropping.");
  674. return 0;
  675. }
  676. return circuit_extend(cell, circ);
  677. case RELAY_COMMAND_EXTENDED:
  678. if(!layer_hint) {
  679. log_fn(LOG_WARN,"'extended' unsupported at non-origin. Dropping.");
  680. return 0;
  681. }
  682. log_fn(LOG_DEBUG,"Got an extended cell! Yay.");
  683. if(circuit_finish_handshake(circ, cell->payload+RELAY_HEADER_SIZE) < 0) {
  684. log_fn(LOG_WARN,"circuit_finish_handshake failed.");
  685. return -1;
  686. }
  687. if (circuit_send_next_onion_skin(circ)<0) {
  688. log_fn(LOG_INFO,"circuit_send_next_onion_skin() failed.");
  689. return -1;
  690. }
  691. return 0;
  692. case RELAY_COMMAND_TRUNCATE:
  693. if(layer_hint) {
  694. log_fn(LOG_WARN,"'truncate' unsupported at origin. Dropping.");
  695. return 0;
  696. }
  697. if(circ->n_conn) {
  698. connection_send_destroy(circ->n_circ_id, circ->n_conn);
  699. circ->n_conn = NULL;
  700. }
  701. log_fn(LOG_DEBUG, "Processed 'truncate', replying.");
  702. connection_edge_send_command(NULL, circ, RELAY_COMMAND_TRUNCATED,
  703. NULL, 0, NULL);
  704. return 0;
  705. case RELAY_COMMAND_TRUNCATED:
  706. if(!layer_hint) {
  707. log_fn(LOG_WARN,"'truncated' unsupported at non-origin. Dropping.");
  708. return 0;
  709. }
  710. circuit_truncated(circ, layer_hint);
  711. return 0;
  712. case RELAY_COMMAND_CONNECTED:
  713. if(conn) {
  714. log_fn(LOG_WARN,"'connected' unsupported while open. Closing circ.");
  715. return -1;
  716. }
  717. log_fn(LOG_INFO,"'connected' received, no conn attached anymore. Ignoring.");
  718. return 0;
  719. case RELAY_COMMAND_SENDME:
  720. if(!conn) {
  721. if(layer_hint) {
  722. layer_hint->package_window += CIRCWINDOW_INCREMENT;
  723. log_fn(LOG_DEBUG,"circ-level sendme at origin, packagewindow %d.",
  724. layer_hint->package_window);
  725. circuit_resume_edge_reading(circ, layer_hint);
  726. } else {
  727. circ->package_window += CIRCWINDOW_INCREMENT;
  728. log_fn(LOG_DEBUG,"circ-level sendme at non-origin, packagewindow %d.",
  729. circ->package_window);
  730. circuit_resume_edge_reading(circ, layer_hint);
  731. }
  732. return 0;
  733. }
  734. conn->package_window += STREAMWINDOW_INCREMENT;
  735. log_fn(LOG_DEBUG,"stream-level sendme, packagewindow now %d.", conn->package_window);
  736. connection_start_reading(conn);
  737. connection_edge_package_raw_inbuf(conn); /* handle whatever might still be on the inbuf */
  738. return 0;
  739. case RELAY_COMMAND_RESOLVE:
  740. if (layer_hint) {
  741. log_fn(LOG_WARN,"resolve request unsupported at AP; dropping.");
  742. return 0;
  743. } else if (conn) {
  744. log_fn(LOG_WARN, "resolve request for known stream; dropping.");
  745. return 0;
  746. } else if (circ->purpose != CIRCUIT_PURPOSE_OR) {
  747. log_fn(LOG_WARN, "resolve request on circ with purpose %d; dropping",
  748. circ->purpose);
  749. return 0;
  750. }
  751. connection_exit_begin_resolve(cell, circ);
  752. return 0;
  753. case RELAY_COMMAND_RESOLVED:
  754. if(conn) {
  755. log_fn(LOG_WARN,"'resolved' unsupported while open. Closing circ.");
  756. return -1;
  757. }
  758. log_fn(LOG_INFO,"'resolved' received, no conn attached anymore. Ignoring.");
  759. return 0;
  760. case RELAY_COMMAND_ESTABLISH_INTRO:
  761. case RELAY_COMMAND_ESTABLISH_RENDEZVOUS:
  762. case RELAY_COMMAND_INTRODUCE1:
  763. case RELAY_COMMAND_INTRODUCE2:
  764. case RELAY_COMMAND_INTRODUCE_ACK:
  765. case RELAY_COMMAND_RENDEZVOUS1:
  766. case RELAY_COMMAND_RENDEZVOUS2:
  767. case RELAY_COMMAND_INTRO_ESTABLISHED:
  768. case RELAY_COMMAND_RENDEZVOUS_ESTABLISHED:
  769. rend_process_relay_cell(circ, rh.command, rh.length,
  770. cell->payload+RELAY_HEADER_SIZE);
  771. return 0;
  772. }
  773. log_fn(LOG_WARN,"unknown relay command %d.",rh.command);
  774. return -1;
  775. }
  776. uint64_t stats_n_data_cells_packaged = 0;
  777. uint64_t stats_n_data_bytes_packaged = 0;
  778. uint64_t stats_n_data_cells_received = 0;
  779. uint64_t stats_n_data_bytes_received = 0;
  780. /** While conn->inbuf has an entire relay payload of bytes on it,
  781. * and the appropriate package windows aren't empty, grab a cell
  782. * and send it down the circuit.
  783. *
  784. * Return -1 if conn should be marked for close, else return 0.
  785. */
  786. int connection_edge_package_raw_inbuf(connection_t *conn) {
  787. size_t amount_to_process, length;
  788. char payload[CELL_PAYLOAD_SIZE];
  789. circuit_t *circ;
  790. tor_assert(conn);
  791. tor_assert(!connection_speaks_cells(conn));
  792. repeat_connection_edge_package_raw_inbuf:
  793. circ = circuit_get_by_conn(conn);
  794. if(!circ) {
  795. log_fn(LOG_INFO,"conn has no circuits! Closing.");
  796. return -1;
  797. }
  798. if(circuit_consider_stop_edge_reading(circ, conn->cpath_layer))
  799. return 0;
  800. if(conn->package_window <= 0) {
  801. log_fn(LOG_WARN,"called with package_window %d. Tell Roger.", conn->package_window);
  802. connection_stop_reading(conn);
  803. return 0;
  804. }
  805. amount_to_process = buf_datalen(conn->inbuf);
  806. if(!amount_to_process)
  807. return 0;
  808. if(amount_to_process > RELAY_PAYLOAD_SIZE) {
  809. length = RELAY_PAYLOAD_SIZE;
  810. } else {
  811. length = amount_to_process;
  812. }
  813. stats_n_data_bytes_packaged += length;
  814. stats_n_data_cells_packaged += 1;
  815. connection_fetch_from_buf(payload, length, conn);
  816. log_fn(LOG_DEBUG,"(%d) Packaging %d bytes (%d waiting).", conn->s,
  817. (int)length, (int)buf_datalen(conn->inbuf));
  818. if(connection_edge_send_command(conn, circ, RELAY_COMMAND_DATA,
  819. payload, length, conn->cpath_layer) < 0)
  820. return 0; /* circuit is closed, don't continue */
  821. if(!conn->cpath_layer) { /* non-rendezvous exit */
  822. tor_assert(circ->package_window > 0);
  823. circ->package_window--;
  824. } else { /* we're an AP, or an exit on a rendezvous circ */
  825. tor_assert(conn->cpath_layer->package_window > 0);
  826. conn->cpath_layer->package_window--;
  827. }
  828. if(--conn->package_window <= 0) { /* is it 0 after decrement? */
  829. connection_stop_reading(conn);
  830. log_fn(LOG_DEBUG,"conn->package_window reached 0.");
  831. circuit_consider_stop_edge_reading(circ, conn->cpath_layer);
  832. return 0; /* don't process the inbuf any more */
  833. }
  834. log_fn(LOG_DEBUG,"conn->package_window is now %d",conn->package_window);
  835. /* handle more if there's more, or return 0 if there isn't */
  836. goto repeat_connection_edge_package_raw_inbuf;
  837. }
  838. /** Called when we've just received a relay data cell, or when
  839. * we've just finished flushing all bytes to stream <b>conn</b>.
  840. *
  841. * If conn->outbuf is not too full, and our deliver window is
  842. * low, send back a suitable number of stream-level sendme cells.
  843. */
  844. void connection_edge_consider_sending_sendme(connection_t *conn) {
  845. circuit_t *circ;
  846. if(connection_outbuf_too_full(conn))
  847. return;
  848. circ = circuit_get_by_conn(conn);
  849. if(!circ) {
  850. /* this can legitimately happen if the destroy has already
  851. * arrived and torn down the circuit */
  852. log_fn(LOG_INFO,"No circuit associated with conn. Skipping.");
  853. return;
  854. }
  855. while(conn->deliver_window < STREAMWINDOW_START - STREAMWINDOW_INCREMENT) {
  856. log_fn(LOG_DEBUG,"Outbuf %d, Queueing stream sendme.", (int)conn->outbuf_flushlen);
  857. conn->deliver_window += STREAMWINDOW_INCREMENT;
  858. if(connection_edge_send_command(conn, circ, RELAY_COMMAND_SENDME,
  859. NULL, 0, conn->cpath_layer) < 0) {
  860. log_fn(LOG_WARN,"connection_edge_send_command failed. Returning.");
  861. return; /* the circuit's closed, don't continue */
  862. }
  863. }
  864. }
  865. /** The circuit <b>circ</b> has received a circuit-level sendme
  866. * (on hop <b>layer_hint</b>, if we're the OP). Go through all the
  867. * attached streams and let them resume reading and packaging, if
  868. * their stream windows allow it.
  869. */
  870. static void
  871. circuit_resume_edge_reading(circuit_t *circ, crypt_path_t *layer_hint)
  872. {
  873. log_fn(LOG_DEBUG,"resuming");
  874. /* have to check both n_streams and p_streams, to handle rendezvous */
  875. if(circuit_resume_edge_reading_helper(circ->n_streams, circ, layer_hint) >= 0)
  876. circuit_resume_edge_reading_helper(circ->p_streams, circ, layer_hint);
  877. }
  878. /** A helper function for circuit_resume_edge_reading() above.
  879. * The arguments are the same, except that <b>conn</b> is the head
  880. * of a linked list of edge streams that should each be considered.
  881. */
  882. static int
  883. circuit_resume_edge_reading_helper(connection_t *conn,
  884. circuit_t *circ,
  885. crypt_path_t *layer_hint) {
  886. for( ; conn; conn=conn->next_stream) {
  887. if((!layer_hint && conn->package_window > 0) ||
  888. (layer_hint && conn->package_window > 0 && conn->cpath_layer == layer_hint)) {
  889. connection_start_reading(conn);
  890. /* handle whatever might still be on the inbuf */
  891. connection_edge_package_raw_inbuf(conn);
  892. /* If the circuit won't accept any more data, return without looking
  893. * at any more of the streams. Any connections that should be stopped
  894. * have already been stopped by connection_edge_package_raw_inbuf. */
  895. if(circuit_consider_stop_edge_reading(circ, layer_hint))
  896. return -1;
  897. }
  898. }
  899. return 0;
  900. }
  901. /** Check if the package window for <b>circ</b> is empty (at
  902. * hop <b>layer_hint</b> if it's defined).
  903. *
  904. * If yes, tell edge streams to stop reading and return -1.
  905. * Else return 0.
  906. */
  907. static int
  908. circuit_consider_stop_edge_reading(circuit_t *circ, crypt_path_t *layer_hint)
  909. {
  910. connection_t *conn = NULL;
  911. log_fn(LOG_DEBUG,"considering");
  912. if(!layer_hint && circ->package_window <= 0) {
  913. log_fn(LOG_DEBUG,"yes, not-at-origin. stopped.");
  914. for(conn = circ->n_streams; conn; conn=conn->next_stream)
  915. connection_stop_reading(conn);
  916. return -1;
  917. } else if(layer_hint && layer_hint->package_window <= 0) {
  918. log_fn(LOG_DEBUG,"yes, at-origin. stopped.");
  919. for(conn = circ->n_streams; conn; conn=conn->next_stream)
  920. if(conn->cpath_layer == layer_hint)
  921. connection_stop_reading(conn);
  922. for(conn = circ->p_streams; conn; conn=conn->next_stream)
  923. if(conn->cpath_layer == layer_hint)
  924. connection_stop_reading(conn);
  925. return -1;
  926. }
  927. return 0;
  928. }
  929. /** Check if the deliver_window for circuit <b>circ</b> (at hop
  930. * <b>layer_hint</b> if it's defined) is low enough that we should
  931. * send a circuit-level sendme back down the circuit. If so, send
  932. * enough sendmes that the window would be overfull if we sent any
  933. * more.
  934. */
  935. static void
  936. circuit_consider_sending_sendme(circuit_t *circ, crypt_path_t *layer_hint)
  937. {
  938. // log_fn(LOG_INFO,"Considering: layer_hint is %s",
  939. // layer_hint ? "defined" : "null");
  940. while((layer_hint ? layer_hint->deliver_window : circ->deliver_window) <
  941. CIRCWINDOW_START - CIRCWINDOW_INCREMENT) {
  942. log_fn(LOG_DEBUG,"Queueing circuit sendme.");
  943. if(layer_hint)
  944. layer_hint->deliver_window += CIRCWINDOW_INCREMENT;
  945. else
  946. circ->deliver_window += CIRCWINDOW_INCREMENT;
  947. if(connection_edge_send_command(NULL, circ, RELAY_COMMAND_SENDME,
  948. NULL, 0, layer_hint) < 0) {
  949. log_fn(LOG_WARN,"connection_edge_send_command failed. Circuit's closed.");
  950. return; /* the circuit's closed, don't continue */
  951. }
  952. }
  953. }