relay.c 37 KB

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