circuit.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  1. /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "or.h"
  5. extern or_options_t options; /* command-line and config-file options */
  6. static int relay_crypt(circuit_t *circ, cell_t *cell, char cell_direction,
  7. crypt_path_t **layer_hint, char *recognized);
  8. static connection_t *relay_lookup_conn(circuit_t *circ, cell_t *cell, int cell_direction);
  9. static void circuit_free_cpath_node(crypt_path_t *victim);
  10. static uint16_t get_unique_circ_id_by_conn(connection_t *conn, int circ_id_type);
  11. unsigned long stats_n_relay_cells_relayed = 0;
  12. unsigned long stats_n_relay_cells_delivered = 0;
  13. /********* START VARIABLES **********/
  14. static circuit_t *global_circuitlist=NULL;
  15. char *circuit_state_to_string[] = {
  16. "doing handshakes", /* 0 */
  17. "processing the onion", /* 1 */
  18. "connecting to firsthop", /* 2 */
  19. "open" /* 3 */
  20. };
  21. /********* END VARIABLES ************/
  22. void circuit_add(circuit_t *circ) {
  23. if(!global_circuitlist) { /* first one */
  24. global_circuitlist = circ;
  25. circ->next = NULL;
  26. } else {
  27. circ->next = global_circuitlist;
  28. global_circuitlist = circ;
  29. }
  30. }
  31. void circuit_remove(circuit_t *circ) {
  32. circuit_t *tmpcirc;
  33. assert(circ && global_circuitlist);
  34. if(global_circuitlist == circ) {
  35. global_circuitlist = global_circuitlist->next;
  36. return;
  37. }
  38. for(tmpcirc = global_circuitlist;tmpcirc->next;tmpcirc = tmpcirc->next) {
  39. if(tmpcirc->next == circ) {
  40. tmpcirc->next = circ->next;
  41. return;
  42. }
  43. }
  44. }
  45. circuit_t *circuit_new(uint16_t p_circ_id, connection_t *p_conn) {
  46. circuit_t *circ;
  47. circ = tor_malloc_zero(sizeof(circuit_t));
  48. circ->timestamp_created = time(NULL);
  49. circ->p_circ_id = p_circ_id;
  50. circ->p_conn = p_conn;
  51. circ->state = CIRCUIT_STATE_ONIONSKIN_PENDING;
  52. /* CircIDs */
  53. circ->p_circ_id = p_circ_id;
  54. /* circ->n_circ_id remains 0 because we haven't identified the next hop yet */
  55. circ->package_window = CIRCWINDOW_START;
  56. circ->deliver_window = CIRCWINDOW_START;
  57. circ->next_stream_id = crypto_pseudo_rand_int(1<<16);
  58. circuit_add(circ);
  59. return circ;
  60. }
  61. void circuit_free(circuit_t *circ) {
  62. assert(circ);
  63. if (circ->n_crypto)
  64. crypto_free_cipher_env(circ->n_crypto);
  65. if (circ->p_crypto)
  66. crypto_free_cipher_env(circ->p_crypto);
  67. if (circ->n_digest)
  68. crypto_free_digest_env(circ->n_digest);
  69. if (circ->p_digest)
  70. crypto_free_digest_env(circ->p_digest);
  71. if(circ->build_state)
  72. tor_free(circ->build_state->chosen_exit);
  73. tor_free(circ->build_state);
  74. circuit_free_cpath(circ->cpath);
  75. free(circ);
  76. }
  77. void circuit_free_cpath(crypt_path_t *cpath) {
  78. crypt_path_t *victim, *head=cpath;
  79. if(!cpath)
  80. return;
  81. /* it's a doubly linked list, so we have to notice when we've
  82. * gone through it once. */
  83. while(cpath->next && cpath->next != head) {
  84. victim = cpath;
  85. cpath = victim->next;
  86. circuit_free_cpath_node(victim);
  87. }
  88. circuit_free_cpath_node(cpath);
  89. }
  90. static void circuit_free_cpath_node(crypt_path_t *victim) {
  91. if(victim->f_crypto)
  92. crypto_free_cipher_env(victim->f_crypto);
  93. if(victim->b_crypto)
  94. crypto_free_cipher_env(victim->b_crypto);
  95. if(victim->f_digest)
  96. crypto_free_digest_env(victim->f_digest);
  97. if(victim->b_digest)
  98. crypto_free_digest_env(victim->b_digest);
  99. if(victim->handshake_state)
  100. crypto_dh_free(victim->handshake_state);
  101. free(victim);
  102. }
  103. /* return 0 if can't get a unique circ_id. */
  104. static uint16_t get_unique_circ_id_by_conn(connection_t *conn, int circ_id_type) {
  105. uint16_t test_circ_id;
  106. int attempts=0;
  107. uint16_t high_bit;
  108. assert(conn && conn->type == CONN_TYPE_OR);
  109. high_bit = (circ_id_type == CIRC_ID_TYPE_HIGHER) ? 1<<15 : 0;
  110. do {
  111. /* Sequentially iterate over test_circ_id=1...1<<15-1 until we find a
  112. * circID such that (high_bit|test_circ_id) is not already used. */
  113. test_circ_id = conn->next_circ_id++;
  114. if (test_circ_id == 0 || test_circ_id >= 1<<15) {
  115. test_circ_id = 1;
  116. conn->next_circ_id = 2;
  117. }
  118. if(++attempts > 1<<15) {
  119. /* Make sure we don't loop forever if all circ_id's are used. This
  120. * matters because it's an external DoS vulnerability.
  121. */
  122. log_fn(LOG_WARN,"No unused circ IDs. Failing.");
  123. return 0;
  124. }
  125. test_circ_id |= high_bit;
  126. } while(circuit_get_by_circ_id_conn(test_circ_id, conn));
  127. return test_circ_id;
  128. }
  129. circuit_t *circuit_get_by_circ_id_conn(uint16_t circ_id, connection_t *conn) {
  130. circuit_t *circ;
  131. connection_t *tmpconn;
  132. for(circ=global_circuitlist;circ;circ = circ->next) {
  133. if(circ->p_circ_id == circ_id) {
  134. if(circ->p_conn == conn)
  135. return circ;
  136. for(tmpconn = circ->p_streams; tmpconn; tmpconn = tmpconn->next_stream) {
  137. if(tmpconn == conn)
  138. return circ;
  139. }
  140. }
  141. if(circ->n_circ_id == circ_id) {
  142. if(circ->n_conn == conn)
  143. return circ;
  144. for(tmpconn = circ->n_streams; tmpconn; tmpconn = tmpconn->next_stream) {
  145. if(tmpconn == conn)
  146. return circ;
  147. }
  148. }
  149. }
  150. return NULL;
  151. }
  152. circuit_t *circuit_get_by_conn(connection_t *conn) {
  153. circuit_t *circ;
  154. connection_t *tmpconn;
  155. for(circ=global_circuitlist;circ;circ = circ->next) {
  156. if(circ->p_conn == conn)
  157. return circ;
  158. if(circ->n_conn == conn)
  159. return circ;
  160. for(tmpconn = circ->p_streams; tmpconn; tmpconn=tmpconn->next_stream)
  161. if(tmpconn == conn)
  162. return circ;
  163. for(tmpconn = circ->n_streams; tmpconn; tmpconn=tmpconn->next_stream)
  164. if(tmpconn == conn)
  165. return circ;
  166. }
  167. return NULL;
  168. }
  169. /* Find the newest circ that conn can use, preferably one which is
  170. * dirty. Circ must not be too old.
  171. * If !conn, return newest.
  172. *
  173. * If must_be_open, ignore circs not in CIRCUIT_STATE_OPEN.
  174. */
  175. circuit_t *circuit_get_newest(connection_t *conn, int must_be_open) {
  176. circuit_t *circ, *newest=NULL, *leastdirty=NULL;
  177. routerinfo_t *exitrouter;
  178. for(circ=global_circuitlist;circ;circ = circ->next) {
  179. if(!circ->cpath)
  180. continue; /* this circ doesn't start at us */
  181. if(must_be_open && (circ->state != CIRCUIT_STATE_OPEN || !circ->n_conn))
  182. continue; /* ignore non-open circs */
  183. if(conn) {
  184. if(circ->state == CIRCUIT_STATE_OPEN && circ->n_conn) /* open */
  185. exitrouter = router_get_by_addr_port(circ->cpath->prev->addr, circ->cpath->prev->port);
  186. else /* not open */
  187. exitrouter = router_get_by_nickname(circ->build_state->chosen_exit);
  188. if(!exitrouter || connection_ap_can_use_exit(conn, exitrouter) == ADDR_POLICY_REJECTED) {
  189. /* can't exit from this router */
  190. continue;
  191. }
  192. }
  193. if(!newest || newest->timestamp_created < circ->timestamp_created) {
  194. newest = circ;
  195. }
  196. if(conn && circ->timestamp_dirty &&
  197. (!leastdirty || leastdirty->timestamp_dirty < circ->timestamp_dirty)) {
  198. leastdirty = circ;
  199. }
  200. }
  201. if(leastdirty &&
  202. leastdirty->timestamp_dirty+options.NewCircuitPeriod > time(NULL)) {
  203. /* log_fn(LOG_DEBUG,"Choosing in-use circuit %s:%d:%d.",
  204. leastdirty->n_conn->address, leastdirty->n_port, leastdirty->n_circ_id); */
  205. return leastdirty;
  206. }
  207. if(newest) {
  208. /* log_fn(LOG_DEBUG,"Choosing circuit %s:%d:%d.",
  209. newest->n_conn->address, newest->n_port, newest->n_circ_id); */
  210. return newest;
  211. }
  212. return NULL;
  213. }
  214. #define MIN_SECONDS_BEFORE_EXPIRING_CIRC 10
  215. /* circuits that were born at the end of their second might be expired
  216. * after 10.1 seconds; circuits born at the beginning might be expired
  217. * after closer to 11 seconds.
  218. */
  219. /* close all circuits that start at us, aren't open, and were born
  220. * at least MIN_SECONDS_BEFORE_EXPIRING_CIRC seconds ago */
  221. void circuit_expire_building(void) {
  222. int now = time(NULL);
  223. circuit_t *victim, *circ = global_circuitlist;
  224. while(circ) {
  225. victim = circ;
  226. circ = circ->next;
  227. if(victim->cpath &&
  228. victim->state != CIRCUIT_STATE_OPEN &&
  229. victim->timestamp_created + MIN_SECONDS_BEFORE_EXPIRING_CIRC+1 < now) {
  230. if(victim->n_conn)
  231. log_fn(LOG_INFO,"Abandoning circ %s:%d:%d (state %d:%s)",
  232. victim->n_conn->address, victim->n_port, victim->n_circ_id,
  233. victim->state, circuit_state_to_string[victim->state]);
  234. else
  235. log_fn(LOG_INFO,"Abandoning circ %d (state %d:%s)", victim->n_circ_id,
  236. victim->state, circuit_state_to_string[victim->state]);
  237. circuit_log_path(LOG_INFO,victim);
  238. circuit_close(victim);
  239. }
  240. }
  241. }
  242. /* count the number of circs starting at us that aren't open */
  243. int circuit_count_building(void) {
  244. circuit_t *circ;
  245. int num=0;
  246. for(circ=global_circuitlist;circ;circ = circ->next) {
  247. if(circ->cpath && circ->state != CIRCUIT_STATE_OPEN)
  248. num++;
  249. }
  250. return num;
  251. }
  252. #define MIN_CIRCUITS_HANDLING_STREAM 2
  253. /* return 1 if at least MIN_CIRCUITS_HANDLING_STREAM non-open circuits
  254. * will have an acceptable exit node for conn. Else return 0.
  255. */
  256. int circuit_stream_is_being_handled(connection_t *conn) {
  257. circuit_t *circ;
  258. routerinfo_t *exitrouter;
  259. int num=0;
  260. for(circ=global_circuitlist;circ;circ = circ->next) {
  261. if(circ->cpath && circ->state != CIRCUIT_STATE_OPEN) {
  262. exitrouter = router_get_by_nickname(circ->build_state->chosen_exit);
  263. if(exitrouter && connection_ap_can_use_exit(conn, exitrouter) != ADDR_POLICY_REJECTED)
  264. if(++num >= MIN_CIRCUITS_HANDLING_STREAM)
  265. return 1;
  266. }
  267. }
  268. return 0;
  269. }
  270. /* update digest from the payload of cell. assign integrity part to cell. */
  271. static void relay_set_digest(crypto_digest_env_t *digest, cell_t *cell) {
  272. char integrity[4];
  273. relay_header_t rh;
  274. crypto_digest_add_bytes(digest, cell->payload, CELL_PAYLOAD_SIZE);
  275. crypto_digest_get_digest(digest, integrity, 4);
  276. log_fn(LOG_DEBUG,"Putting digest of %u %u %u %u into relay cell.",
  277. integrity[0], integrity[1], integrity[2], integrity[3]);
  278. relay_header_unpack(&rh, cell->payload);
  279. memcpy(rh.integrity, integrity, 4);
  280. relay_header_pack(cell->payload, &rh);
  281. }
  282. /* update digest from the payload of cell (with the integrity part set
  283. * to 0). If the integrity part is valid return 1, else restore digest
  284. * and cell to their original state and return 0.
  285. */
  286. static int relay_digest_matches(crypto_digest_env_t *digest, cell_t *cell) {
  287. char received_integrity[4], calculated_integrity[4];
  288. relay_header_t rh;
  289. crypto_digest_env_t *backup_digest=NULL;
  290. backup_digest = crypto_digest_dup(digest);
  291. relay_header_unpack(&rh, cell->payload);
  292. memcpy(received_integrity, rh.integrity, 4);
  293. memset(rh.integrity, 0, 4);
  294. relay_header_pack(cell->payload, &rh);
  295. log_fn(LOG_DEBUG,"Reading digest of %u %u %u %u from relay cell.",
  296. received_integrity[0], received_integrity[1],
  297. received_integrity[2], received_integrity[3]);
  298. crypto_digest_add_bytes(digest, cell->payload, CELL_PAYLOAD_SIZE);
  299. crypto_digest_get_digest(digest, calculated_integrity, 4);
  300. if(memcmp(received_integrity, calculated_integrity, 4)) {
  301. log_fn(LOG_INFO,"Recognized=0 but bad digest. Not recognizing.");
  302. // (%d vs %d).", received_integrity, calculated_integrity);
  303. /* restore digest to its old form */
  304. crypto_digest_assign(digest, backup_digest);
  305. /* restore the relay header */
  306. memcpy(rh.integrity, received_integrity, 4);
  307. relay_header_pack(cell->payload, &rh);
  308. crypto_free_digest_env(backup_digest);
  309. return 0;
  310. }
  311. crypto_free_digest_env(backup_digest);
  312. return 1;
  313. }
  314. static int relay_crypt_one_payload(crypto_cipher_env_t *cipher, char *in,
  315. int encrypt_mode) {
  316. char out[CELL_PAYLOAD_SIZE]; /* 'in' must be this size too */
  317. relay_header_t rh;
  318. relay_header_unpack(&rh, in);
  319. log_fn(LOG_DEBUG,"before crypt: %d",rh.recognized);
  320. if(( encrypt_mode && crypto_cipher_encrypt(cipher, in, CELL_PAYLOAD_SIZE, out)) ||
  321. (!encrypt_mode && crypto_cipher_decrypt(cipher, in, CELL_PAYLOAD_SIZE, out))) {
  322. log_fn(LOG_WARN,"Error during crypt: %s", crypto_perror());
  323. return -1;
  324. }
  325. memcpy(in,out,CELL_PAYLOAD_SIZE);
  326. relay_header_unpack(&rh, in);
  327. log_fn(LOG_DEBUG,"after crypt: %d",rh.recognized);
  328. return 0;
  329. }
  330. /*
  331. receive a relay cell:
  332. - crypt it (encrypt APward, decrypt at AP, decrypt exitward)
  333. - check if recognized (if exitward)
  334. - if recognized, check digest, find right conn, deliver to edge.
  335. - else connection_or_write_cell_to_buf to the right conn
  336. */
  337. int circuit_receive_relay_cell(cell_t *cell, circuit_t *circ,
  338. int cell_direction) {
  339. connection_t *conn=NULL;
  340. crypt_path_t *layer_hint=NULL;
  341. char recognized=0;
  342. assert(cell && circ);
  343. assert(cell_direction == CELL_DIRECTION_OUT || cell_direction == CELL_DIRECTION_IN);
  344. if(relay_crypt(circ, cell, cell_direction, &layer_hint, &recognized) < 0) {
  345. log_fn(LOG_WARN,"relay crypt failed. Dropping connection.");
  346. return -1;
  347. }
  348. if(recognized) {
  349. conn = relay_lookup_conn(circ, cell, cell_direction);
  350. if(cell_direction == CELL_DIRECTION_OUT) {
  351. ++stats_n_relay_cells_delivered;
  352. log_fn(LOG_DEBUG,"Sending to exit.");
  353. if (connection_edge_process_relay_cell(cell, circ, conn, EDGE_EXIT, NULL) < 0) {
  354. log_fn(LOG_WARN,"connection_edge_process_relay_cell (at exit) failed.");
  355. return -1;
  356. }
  357. }
  358. if(cell_direction == CELL_DIRECTION_IN) {
  359. ++stats_n_relay_cells_delivered;
  360. log_fn(LOG_DEBUG,"Sending to AP.");
  361. if (connection_edge_process_relay_cell(cell, circ, conn, EDGE_AP, layer_hint) < 0) {
  362. log_fn(LOG_WARN,"connection_edge_process_relay_cell (at AP) failed.");
  363. return -1;
  364. }
  365. }
  366. return 0;
  367. }
  368. /* not recognized. pass it on. */
  369. if(cell_direction == CELL_DIRECTION_OUT) {
  370. cell->circ_id = circ->n_circ_id; /* switch it */
  371. conn = circ->n_conn;
  372. } else {
  373. cell->circ_id = circ->p_circ_id; /* switch it */
  374. conn = circ->p_conn;
  375. }
  376. if(!conn) {
  377. log_fn(LOG_WARN,"Didn't recognize cell, but circ stops here! Dropping.");
  378. return 0; /* XXX if this warning never triggers, then return -1 */
  379. }
  380. log_fn(LOG_DEBUG,"Passing on unrecognized cell.");
  381. ++stats_n_relay_cells_relayed;
  382. connection_or_write_cell_to_buf(cell, conn);
  383. return 0;
  384. }
  385. /* wrap this into receive_relay_cell one day */
  386. static int relay_crypt(circuit_t *circ, cell_t *cell, char cell_direction,
  387. crypt_path_t **layer_hint, char *recognized) {
  388. crypt_path_t *thishop;
  389. relay_header_t rh;
  390. assert(circ && cell && recognized);
  391. assert(cell_direction == CELL_DIRECTION_IN || cell_direction == CELL_DIRECTION_OUT);
  392. if(cell_direction == CELL_DIRECTION_IN) {
  393. if(circ->cpath) { /* we're at the beginning of the circuit.
  394. We'll want to do layered crypts. */
  395. thishop = circ->cpath;
  396. if(thishop->state != CPATH_STATE_OPEN) {
  397. log_fn(LOG_WARN,"Relay cell before first created cell?");
  398. return -1;
  399. }
  400. do { /* Remember: cpath is in forward order, that is, first hop first. */
  401. assert(thishop);
  402. if(relay_crypt_one_payload(thishop->b_crypto, cell->payload, 0) < 0)
  403. return -1;
  404. relay_header_unpack(&rh, cell->payload);
  405. if(rh.recognized == 0) {
  406. /* it's possibly recognized. have to check digest to be sure. */
  407. if(relay_digest_matches(thishop->b_digest, cell)) {
  408. *recognized = 1;
  409. *layer_hint = thishop;
  410. return 0;
  411. }
  412. }
  413. thishop = thishop->next;
  414. } while(thishop != circ->cpath && thishop->state == CPATH_STATE_OPEN);
  415. log_fn(LOG_WARN,"in-cell at OP not recognized. Dropping.");
  416. return 0;
  417. } else { /* we're in the middle. Just one crypt. */
  418. if(relay_crypt_one_payload(circ->p_crypto, cell->payload, 1) < 0)
  419. return -1;
  420. log_fn(LOG_DEBUG,"Skipping recognized check, because we're not the OP.");
  421. }
  422. } else /* cell_direction == CELL_DIRECTION_OUT */ {
  423. /* we're in the middle. Just one crypt. */
  424. if(relay_crypt_one_payload(circ->n_crypto, cell->payload, 0) < 0)
  425. return -1;
  426. relay_header_unpack(&rh, cell->payload);
  427. if (rh.recognized == 0) {
  428. /* it's possibly recognized. have to check digest to be sure. */
  429. if(relay_digest_matches(circ->n_digest, cell)) {
  430. *recognized = 1;
  431. return 0;
  432. }
  433. }
  434. }
  435. return 0;
  436. }
  437. /*
  438. package a relay cell:
  439. - if from AP: encrypt it to the right conn if 'recognized' doesn't
  440. conflict, else insert dummies first as appropriate
  441. - if from exit: encrypt it
  442. - connection_or_write_cell_to_buf to the right conn
  443. */
  444. int
  445. circuit_package_relay_cell(cell_t *cell, circuit_t *circ,
  446. int cell_direction,
  447. crypt_path_t *layer_hint)
  448. {
  449. connection_t *conn; /* where to send the cell */
  450. crypt_path_t *thishop; /* counter for repeated crypts */
  451. if(cell_direction == CELL_DIRECTION_OUT) {
  452. conn = circ->n_conn;
  453. if(!conn) {
  454. log_fn(LOG_INFO,"outgoing relay cell has n_conn==NULL. Dropping.");
  455. return 0; /* just drop it */
  456. }
  457. relay_set_digest(layer_hint->f_digest, cell);
  458. thishop = layer_hint;
  459. /* moving from farthest to nearest hop */
  460. do {
  461. assert(thishop);
  462. log_fn(LOG_DEBUG,"crypting a layer of the relay cell.");
  463. if(relay_crypt_one_payload(thishop->f_crypto, cell->payload, 1) < 0) {
  464. return -1;
  465. }
  466. thishop = thishop->prev;
  467. } while (thishop != circ->cpath->prev);
  468. } else { /* incoming cell */
  469. conn = circ->p_conn;
  470. if(!conn) {
  471. log_fn(LOG_INFO,"incoming relay cell has p_conn==NULL. Dropping.");
  472. return 0; /* just drop it */
  473. }
  474. relay_set_digest(circ->p_digest, cell);
  475. if(relay_crypt_one_payload(circ->p_crypto, cell->payload, 1) < 0)
  476. return -1;
  477. }
  478. ++stats_n_relay_cells_relayed;
  479. connection_or_write_cell_to_buf(cell, conn);
  480. return 0;
  481. }
  482. static connection_t *
  483. relay_lookup_conn(circuit_t *circ, cell_t *cell, int cell_direction)
  484. {
  485. connection_t *tmpconn;
  486. relay_header_t rh;
  487. relay_header_unpack(&rh, cell->payload);
  488. if(!rh.stream_id)
  489. return NULL;
  490. if(cell_direction == CELL_DIRECTION_OUT)
  491. tmpconn = circ->n_streams;
  492. else
  493. tmpconn = circ->p_streams;
  494. for( ; tmpconn; tmpconn=tmpconn->next_stream) {
  495. if(rh.stream_id == tmpconn->stream_id) {
  496. log_fn(LOG_DEBUG,"found conn for stream %d.", rh.stream_id);
  497. return tmpconn;
  498. }
  499. log_fn(LOG_DEBUG,"considered stream %d, not it.",tmpconn->stream_id);
  500. }
  501. return NULL; /* probably a begin relay cell */
  502. }
  503. void circuit_resume_edge_reading(circuit_t *circ, int edge_type, crypt_path_t *layer_hint) {
  504. connection_t *conn;
  505. assert(edge_type == EDGE_EXIT || edge_type == EDGE_AP);
  506. log_fn(LOG_DEBUG,"resuming");
  507. if(edge_type == EDGE_EXIT)
  508. conn = circ->n_streams;
  509. else
  510. conn = circ->p_streams;
  511. for( ; conn; conn=conn->next_stream) {
  512. if((edge_type == EDGE_EXIT && conn->package_window > 0) ||
  513. (edge_type == EDGE_AP && conn->package_window > 0 && conn->cpath_layer == layer_hint)) {
  514. connection_start_reading(conn);
  515. connection_edge_package_raw_inbuf(conn); /* handle whatever might still be on the inbuf */
  516. /* If the circuit won't accept any more data, return without looking
  517. * at any more of the streams. Any connections that should be stopped
  518. * have already been stopped by connection_edge_package_raw_inbuf. */
  519. if(circuit_consider_stop_edge_reading(circ, edge_type, layer_hint))
  520. return;
  521. }
  522. }
  523. }
  524. /* returns 1 if the window is empty, else 0. If it's empty, tell edge conns to stop reading. */
  525. int circuit_consider_stop_edge_reading(circuit_t *circ, int edge_type, crypt_path_t *layer_hint) {
  526. connection_t *conn = NULL;
  527. assert(edge_type == EDGE_EXIT || edge_type == EDGE_AP);
  528. assert(edge_type == EDGE_EXIT || layer_hint);
  529. log_fn(LOG_DEBUG,"considering");
  530. if(edge_type == EDGE_EXIT && circ->package_window <= 0)
  531. conn = circ->n_streams;
  532. else if(edge_type == EDGE_AP && layer_hint->package_window <= 0)
  533. conn = circ->p_streams;
  534. else
  535. return 0;
  536. for( ; conn; conn=conn->next_stream)
  537. if(!layer_hint || conn->cpath_layer == layer_hint)
  538. connection_stop_reading(conn);
  539. log_fn(LOG_DEBUG,"yes. stopped.");
  540. return 1;
  541. }
  542. void circuit_consider_sending_sendme(circuit_t *circ, int edge_type, crypt_path_t *layer_hint) {
  543. while((edge_type == EDGE_AP ? layer_hint->deliver_window : circ->deliver_window) <
  544. CIRCWINDOW_START - CIRCWINDOW_INCREMENT) {
  545. log_fn(LOG_DEBUG,"Queueing circuit sendme.");
  546. if(edge_type == EDGE_AP)
  547. layer_hint->deliver_window += CIRCWINDOW_INCREMENT;
  548. else
  549. circ->deliver_window += CIRCWINDOW_INCREMENT;
  550. if(connection_edge_send_command(NULL, circ, RELAY_COMMAND_SENDME,
  551. NULL, 0, layer_hint) < 0) {
  552. log_fn(LOG_WARN,"connection_edge_send_command failed. Returning.");
  553. return; /* the circuit's closed, don't continue */
  554. }
  555. }
  556. }
  557. void circuit_close(circuit_t *circ) {
  558. connection_t *conn;
  559. assert(circ);
  560. circuit_remove(circ);
  561. if(circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  562. onion_pending_remove(circ);
  563. }
  564. if(circ->n_conn)
  565. connection_send_destroy(circ->n_circ_id, circ->n_conn);
  566. for(conn=circ->n_streams; conn; conn=conn->next_stream) {
  567. connection_send_destroy(circ->n_circ_id, conn);
  568. }
  569. if(circ->p_conn)
  570. connection_send_destroy(circ->n_circ_id, circ->p_conn);
  571. for(conn=circ->p_streams; conn; conn=conn->next_stream) {
  572. connection_send_destroy(circ->p_circ_id, conn);
  573. }
  574. if (circ->state == CIRCUIT_STATE_BUILDING ||
  575. circ->state == CIRCUIT_STATE_OR_WAIT) {
  576. /* If we never built the circuit, note it as a failure. */
  577. /* Note that we can't just check circ->cpath here, because if
  578. * circuit-building failed immediately, it won't be set yet. */
  579. circuit_increment_failure_count();
  580. }
  581. circuit_free(circ);
  582. }
  583. void circuit_about_to_close_connection(connection_t *conn) {
  584. /* send destroys for all circuits using conn */
  585. /* currently, we assume it's too late to flush conn's buf here.
  586. * down the road, maybe we'll consider that eof doesn't mean can't-write
  587. */
  588. circuit_t *circ;
  589. connection_t *prevconn;
  590. switch(conn->type) {
  591. case CONN_TYPE_OR:
  592. /* We must close all the circuits on it. */
  593. while((circ = circuit_get_by_conn(conn))) {
  594. if(circ->n_conn == conn) /* it's closing in front of us */
  595. circ->n_conn = NULL;
  596. if(circ->p_conn == conn) /* it's closing behind us */
  597. circ->p_conn = NULL;
  598. circuit_close(circ);
  599. }
  600. return;
  601. case CONN_TYPE_AP:
  602. case CONN_TYPE_EXIT:
  603. /* It's an edge conn. Need to remove it from the linked list of
  604. * conn's for this circuit. Confirm that 'end' relay command has
  605. * been sent. But don't kill the circuit.
  606. */
  607. circ = circuit_get_by_conn(conn);
  608. if(!circ)
  609. return;
  610. if(!conn->has_sent_end) {
  611. log_fn(LOG_INFO,"Edge connection hasn't sent end yet? Bug.");
  612. if(connection_edge_end(conn, END_STREAM_REASON_MISC, conn->cpath_layer) < 0)
  613. log_fn(LOG_WARN,"1: I called connection_edge_end redundantly.");
  614. }
  615. if(conn == circ->p_streams) {
  616. circ->p_streams = conn->next_stream;
  617. return;
  618. }
  619. if(conn == circ->n_streams) {
  620. circ->n_streams = conn->next_stream;
  621. return;
  622. }
  623. for(prevconn = circ->p_streams; prevconn && prevconn->next_stream && prevconn->next_stream != conn; prevconn = prevconn->next_stream) ;
  624. if(prevconn && prevconn->next_stream) {
  625. prevconn->next_stream = conn->next_stream;
  626. return;
  627. }
  628. for(prevconn = circ->n_streams; prevconn && prevconn->next_stream && prevconn->next_stream != conn; prevconn = prevconn->next_stream) ;
  629. if(prevconn && prevconn->next_stream) {
  630. prevconn->next_stream = conn->next_stream;
  631. return;
  632. }
  633. log_fn(LOG_ERR,"edge conn not in circuit's list?");
  634. assert(0); /* should never get here */
  635. } /* end switch */
  636. }
  637. void circuit_log_path(int severity, circuit_t *circ) {
  638. static char b[1024];
  639. struct crypt_path_t *hop;
  640. char *states[] = {"closed", "waiting for keys", "open"};
  641. routerinfo_t *router;
  642. assert(circ->cpath);
  643. sprintf(b,"circ (length %d, exit %s): ",
  644. circ->build_state->desired_path_len, circ->build_state->chosen_exit);
  645. hop=circ->cpath;
  646. do {
  647. router = router_get_by_addr_port(hop->addr,hop->port);
  648. if(router) {
  649. /* XXX strcat allows buffer overflow */
  650. strcat(b,router->nickname);
  651. strcat(b,"(");
  652. strcat(b,states[hop->state]);
  653. strcat(b,"),");
  654. } else {
  655. strcat(b,"UNKNOWN,");
  656. }
  657. hop=hop->next;
  658. } while(hop!=circ->cpath);
  659. log_fn(severity,"%s",b);
  660. }
  661. static void
  662. circuit_dump_details(int severity, circuit_t *circ, int poll_index,
  663. char *type, int this_circid, int other_circid) {
  664. struct crypt_path_t *hop;
  665. log(severity,"Conn %d has %s circuit: circID %d (other side %d), state %d (%s), born %d",
  666. poll_index, type, this_circid, other_circid, circ->state,
  667. circuit_state_to_string[circ->state], (int)circ->timestamp_created);
  668. if(circ->cpath) { /* circ starts at this node */
  669. if(circ->state == CIRCUIT_STATE_BUILDING)
  670. log(severity,"Building: desired len %d, planned exit node %s.",
  671. circ->build_state->desired_path_len, circ->build_state->chosen_exit);
  672. for(hop=circ->cpath;hop->next != circ->cpath; hop=hop->next)
  673. log(severity,"hop: state %d, addr %x, port %d", hop->state,
  674. (unsigned int)hop->addr,
  675. (int)hop->port);
  676. }
  677. }
  678. void circuit_dump_by_conn(connection_t *conn, int severity) {
  679. circuit_t *circ;
  680. connection_t *tmpconn;
  681. for(circ=global_circuitlist;circ;circ = circ->next) {
  682. if(circ->p_conn == conn)
  683. circuit_dump_details(severity, circ, conn->poll_index, "App-ward",
  684. circ->p_circ_id, circ->n_circ_id);
  685. for(tmpconn=circ->p_streams; tmpconn; tmpconn=tmpconn->next_stream) {
  686. if(tmpconn == conn) {
  687. circuit_dump_details(severity, circ, conn->poll_index, "App-ward",
  688. circ->p_circ_id, circ->n_circ_id);
  689. }
  690. }
  691. if(circ->n_conn == conn)
  692. circuit_dump_details(severity, circ, conn->poll_index, "Exit-ward",
  693. circ->n_circ_id, circ->p_circ_id);
  694. for(tmpconn=circ->n_streams; tmpconn; tmpconn=tmpconn->next_stream) {
  695. if(tmpconn == conn) {
  696. circuit_dump_details(severity, circ, conn->poll_index, "Exit-ward",
  697. circ->n_circ_id, circ->p_circ_id);
  698. }
  699. }
  700. }
  701. }
  702. void circuit_expire_unused_circuits(void) {
  703. circuit_t *circ, *tmpcirc;
  704. time_t now = time(NULL);
  705. circ = global_circuitlist;
  706. while(circ) {
  707. tmpcirc = circ;
  708. circ = circ->next;
  709. if(tmpcirc->timestamp_dirty &&
  710. tmpcirc->timestamp_dirty + options.NewCircuitPeriod < now &&
  711. !tmpcirc->p_conn && !tmpcirc->p_streams) {
  712. log_fn(LOG_DEBUG,"Closing n_circ_id %d",tmpcirc->n_circ_id);
  713. circuit_close(tmpcirc);
  714. }
  715. }
  716. }
  717. /* Number of consecutive failures so far; should only be touched by
  718. * circuit_launch_new and circuit_*_failure_count.
  719. */
  720. static int n_circuit_failures = 0;
  721. /* Return -1 if you aren't going to try to make a circuit, 0 if you did try. */
  722. int circuit_launch_new(void) {
  723. if(!options.SocksPort) /* we're not an application proxy. no need for circuits. */
  724. return -1;
  725. if(n_circuit_failures > 5) { /* too many failed circs in a row. don't try. */
  726. // log_fn(LOG_INFO,"%d failures so far, not trying.",n_circuit_failures);
  727. return -1;
  728. }
  729. /* try a circ. if it fails, circuit_close will increment n_circuit_failures */
  730. circuit_establish_circuit();
  731. return 0;
  732. }
  733. void circuit_increment_failure_count(void) {
  734. ++n_circuit_failures;
  735. log_fn(LOG_DEBUG,"n_circuit_failures now %d.",n_circuit_failures);
  736. }
  737. void circuit_reset_failure_count(void) {
  738. n_circuit_failures = 0;
  739. }
  740. int circuit_establish_circuit(void) {
  741. routerinfo_t *firsthop;
  742. connection_t *n_conn;
  743. circuit_t *circ;
  744. circ = circuit_new(0, NULL); /* sets circ->p_circ_id and circ->p_conn */
  745. circ->state = CIRCUIT_STATE_OR_WAIT;
  746. circ->build_state = onion_new_cpath_build_state();
  747. if (! circ->build_state) {
  748. log_fn(LOG_INFO,"Generating cpath length failed.");
  749. circuit_close(circ);
  750. return -1;
  751. }
  752. onion_extend_cpath(&circ->cpath, circ->build_state, &firsthop);
  753. if(!circ->cpath) {
  754. log_fn(LOG_INFO,"Generating first cpath hop failed.");
  755. circuit_close(circ);
  756. return -1;
  757. }
  758. /* now see if we're already connected to the first OR in 'route' */
  759. log_fn(LOG_DEBUG,"Looking for firsthop '%s:%u'",
  760. firsthop->address,firsthop->or_port);
  761. n_conn = connection_twin_get_by_addr_port(firsthop->addr,firsthop->or_port);
  762. if(!n_conn || n_conn->state != OR_CONN_STATE_OPEN) { /* not currently connected */
  763. circ->n_addr = firsthop->addr;
  764. circ->n_port = firsthop->or_port;
  765. if(options.ORPort) { /* we would be connected if he were up. and he's not. */
  766. log_fn(LOG_INFO,"Route's firsthop isn't connected.");
  767. circuit_close(circ);
  768. return -1;
  769. }
  770. if(!n_conn) { /* launch the connection */
  771. n_conn = connection_or_connect(firsthop);
  772. if(!n_conn) { /* connect failed, forget the whole thing */
  773. log_fn(LOG_INFO,"connect to firsthop failed. Closing.");
  774. circuit_close(circ);
  775. return -1;
  776. }
  777. }
  778. log_fn(LOG_DEBUG,"connecting in progress (or finished). Good.");
  779. return 0; /* return success. The onion/circuit/etc will be taken care of automatically
  780. * (may already have been) whenever n_conn reaches OR_CONN_STATE_OPEN.
  781. */
  782. } else { /* it (or a twin) is already open. use it. */
  783. circ->n_addr = n_conn->addr;
  784. circ->n_port = n_conn->port;
  785. circ->n_conn = n_conn;
  786. log_fn(LOG_DEBUG,"Conn open. Delivering first onion skin.");
  787. if(circuit_send_next_onion_skin(circ) < 0) {
  788. log_fn(LOG_INFO,"circuit_send_next_onion_skin failed.");
  789. circuit_close(circ);
  790. return -1;
  791. }
  792. }
  793. return 0;
  794. }
  795. /* find circuits that are waiting on me, if any, and get them to send the onion */
  796. void circuit_n_conn_open(connection_t *or_conn) {
  797. circuit_t *circ;
  798. for(circ=global_circuitlist;circ;circ = circ->next) {
  799. if(circ->cpath && circ->n_addr == or_conn->addr && circ->n_port == or_conn->port) {
  800. assert(circ->state == CIRCUIT_STATE_OR_WAIT);
  801. log_fn(LOG_DEBUG,"Found circ %d, sending onion skin.", circ->n_circ_id);
  802. circ->n_conn = or_conn;
  803. if(circuit_send_next_onion_skin(circ) < 0) {
  804. log_fn(LOG_INFO,"send_next_onion_skin failed; circuit marked for closing.");
  805. circuit_close(circ);
  806. continue;
  807. /* XXX could this be bad, eg if next_onion_skin failed because conn died? */
  808. }
  809. }
  810. }
  811. }
  812. int circuit_send_next_onion_skin(circuit_t *circ) {
  813. cell_t cell;
  814. crypt_path_t *hop;
  815. routerinfo_t *router;
  816. int r;
  817. int circ_id_type;
  818. char payload[6+ONIONSKIN_CHALLENGE_LEN];
  819. assert(circ && circ->cpath);
  820. if(circ->cpath->state == CPATH_STATE_CLOSED) {
  821. assert(circ->n_conn && circ->n_conn->type == CONN_TYPE_OR);
  822. log_fn(LOG_DEBUG,"First skin; sending create cell.");
  823. circ_id_type = decide_circ_id_type(options.Nickname,
  824. circ->n_conn->nickname);
  825. circ->n_circ_id = get_unique_circ_id_by_conn(circ->n_conn, circ_id_type);
  826. memset(&cell, 0, sizeof(cell_t));
  827. cell.command = CELL_CREATE;
  828. cell.circ_id = circ->n_circ_id;
  829. if(onion_skin_create(circ->n_conn->onion_pkey, &(circ->cpath->handshake_state), cell.payload) < 0) {
  830. log_fn(LOG_WARN,"onion_skin_create (first hop) failed.");
  831. return -1;
  832. }
  833. connection_or_write_cell_to_buf(&cell, circ->n_conn);
  834. circ->cpath->state = CPATH_STATE_AWAITING_KEYS;
  835. circ->state = CIRCUIT_STATE_BUILDING;
  836. log_fn(LOG_DEBUG,"first skin; finished sending create cell.");
  837. } else {
  838. assert(circ->cpath->state == CPATH_STATE_OPEN);
  839. assert(circ->state == CIRCUIT_STATE_BUILDING);
  840. log_fn(LOG_DEBUG,"starting to send subsequent skin.");
  841. r = onion_extend_cpath(&circ->cpath, circ->build_state, &router);
  842. if (r==1) {
  843. /* done building the circuit. whew. */
  844. circ->state = CIRCUIT_STATE_OPEN;
  845. log_fn(LOG_INFO,"circuit built!");
  846. circuit_reset_failure_count();
  847. /* Tell any AP connections that have been waiting for a new
  848. * circuit that one is ready. */
  849. connection_ap_attach_pending();
  850. return 0;
  851. } else if (r<0) {
  852. log_fn(LOG_INFO,"Unable to extend circuit path.");
  853. return -1;
  854. }
  855. hop = circ->cpath->prev;
  856. *(uint32_t*)payload = htonl(hop->addr);
  857. *(uint16_t*)(payload+4) = htons(hop->port);
  858. if(onion_skin_create(router->onion_pkey, &(hop->handshake_state), payload+6) < 0) {
  859. log_fn(LOG_WARN,"onion_skin_create failed.");
  860. return -1;
  861. }
  862. log_fn(LOG_DEBUG,"Sending extend relay cell.");
  863. /* send it to hop->prev, because it will transfer
  864. * it to a create cell and then send to hop */
  865. if(connection_edge_send_command(NULL, circ, RELAY_COMMAND_EXTEND,
  866. payload, sizeof(payload), hop->prev) < 0)
  867. return 0; /* circuit is closed */
  868. hop->state = CPATH_STATE_AWAITING_KEYS;
  869. }
  870. return 0;
  871. }
  872. /* take the 'extend' cell, pull out addr/port plus the onion skin. Make
  873. * sure we're connected to the next hop, and pass it the onion skin in
  874. * a create cell.
  875. */
  876. int circuit_extend(cell_t *cell, circuit_t *circ) {
  877. connection_t *n_conn;
  878. int circ_id_type;
  879. cell_t newcell;
  880. if(circ->n_conn) {
  881. log_fn(LOG_WARN,"n_conn already set. Bug/attack. Closing.");
  882. return -1;
  883. }
  884. circ->n_addr = ntohl(*(uint32_t*)(cell->payload+RELAY_HEADER_SIZE));
  885. circ->n_port = ntohs(*(uint16_t*)(cell->payload+RELAY_HEADER_SIZE+4));
  886. n_conn = connection_twin_get_by_addr_port(circ->n_addr,circ->n_port);
  887. if(!n_conn || n_conn->type != CONN_TYPE_OR) {
  888. /* i've disabled making connections through OPs, but it's definitely
  889. * possible here. I'm not sure if it would be a bug or a feature. -RD
  890. */
  891. /* note also that this will close circuits where the onion has the same
  892. * router twice in a row in the path. i think that's ok. -RD
  893. */
  894. struct in_addr in;
  895. in.s_addr = htonl(circ->n_addr);
  896. log_fn(LOG_INFO,"Next router (%s:%d) not connected. Closing.", inet_ntoa(in), circ->n_port);
  897. connection_edge_send_command(NULL, circ, RELAY_COMMAND_TRUNCATED,
  898. NULL, 0, NULL);
  899. return 0;
  900. }
  901. circ->n_addr = n_conn->addr; /* these are different if we found a twin instead */
  902. circ->n_port = n_conn->port;
  903. circ->n_conn = n_conn;
  904. log_fn(LOG_DEBUG,"n_conn is %s:%u",n_conn->address,n_conn->port);
  905. circ_id_type = decide_circ_id_type(options.Nickname, n_conn->nickname);
  906. log_fn(LOG_DEBUG,"circ_id_type = %u.",circ_id_type);
  907. circ->n_circ_id = get_unique_circ_id_by_conn(circ->n_conn, circ_id_type);
  908. if(!circ->n_circ_id) {
  909. log_fn(LOG_WARN,"failed to get unique circID.");
  910. return -1;
  911. }
  912. log_fn(LOG_DEBUG,"Chosen circID %u.",circ->n_circ_id);
  913. memset(&newcell, 0, sizeof(cell_t));
  914. newcell.command = CELL_CREATE;
  915. newcell.circ_id = circ->n_circ_id;
  916. memcpy(newcell.payload, cell->payload+RELAY_HEADER_SIZE+6,
  917. ONIONSKIN_CHALLENGE_LEN);
  918. connection_or_write_cell_to_buf(&newcell, circ->n_conn);
  919. return 0;
  920. }
  921. int circuit_finish_handshake(circuit_t *circ, char *reply) {
  922. unsigned char iv[16];
  923. unsigned char keys[40+32];
  924. crypt_path_t *hop;
  925. memset(iv, 0, 16);
  926. assert(circ->cpath);
  927. if(circ->cpath->state == CPATH_STATE_AWAITING_KEYS)
  928. hop = circ->cpath;
  929. else {
  930. for(hop=circ->cpath->next;
  931. hop != circ->cpath && hop->state == CPATH_STATE_OPEN;
  932. hop=hop->next) ;
  933. if(hop == circ->cpath) { /* got an extended when we're all done? */
  934. log_fn(LOG_WARN,"got extended when circ already built? Closing.");
  935. return -1;
  936. }
  937. }
  938. assert(hop->state == CPATH_STATE_AWAITING_KEYS);
  939. if(onion_skin_client_handshake(hop->handshake_state, reply, keys, 40+32) < 0) {
  940. log_fn(LOG_WARN,"onion_skin_client_handshake failed.");
  941. return -1;
  942. }
  943. crypto_dh_free(hop->handshake_state); /* don't need it anymore */
  944. hop->handshake_state = NULL;
  945. log_fn(LOG_INFO,"hop %d init digest forward %u, backward %u.",
  946. (int)hop, (unsigned)*(uint32_t*)keys, (unsigned)*(uint32_t*)(keys+20));
  947. hop->f_digest = crypto_new_digest_env(CRYPTO_SHA1_DIGEST);
  948. crypto_digest_add_bytes(hop->f_digest, keys, 20);
  949. hop->b_digest = crypto_new_digest_env(CRYPTO_SHA1_DIGEST);
  950. crypto_digest_add_bytes(hop->b_digest, keys+20, 20);
  951. log_fn(LOG_DEBUG,"hop %d init cipher forward %u, backward %u.",
  952. (int)hop, (unsigned)*(uint32_t*)(keys+40), (unsigned) *(uint32_t*)(keys+40+16));
  953. if (!(hop->f_crypto =
  954. crypto_create_init_cipher(CIRCUIT_CIPHER,keys+40,iv,1))) {
  955. log(LOG_WARN,"forward cipher initialization failed.");
  956. return -1;
  957. }
  958. if (!(hop->b_crypto =
  959. crypto_create_init_cipher(CIRCUIT_CIPHER,keys+40+16,iv,0))) {
  960. log(LOG_WARN,"backward cipher initialization failed.");
  961. return -1;
  962. }
  963. hop->state = CPATH_STATE_OPEN;
  964. log_fn(LOG_INFO,"finished");
  965. circuit_log_path(LOG_WARN,circ);
  966. return 0;
  967. }
  968. int circuit_truncated(circuit_t *circ, crypt_path_t *layer) {
  969. crypt_path_t *victim;
  970. connection_t *stream;
  971. assert(circ);
  972. assert(layer);
  973. /* XXX Since we don't ask for truncates currently, getting a truncated
  974. * means that a connection broke or an extend failed. For now,
  975. * just give up.
  976. */
  977. circuit_close(circ);
  978. return 0;
  979. while(layer->next != circ->cpath) {
  980. /* we need to clear out layer->next */
  981. victim = layer->next;
  982. log_fn(LOG_DEBUG, "Killing a layer of the cpath.");
  983. for(stream = circ->p_streams; stream; stream=stream->next_stream) {
  984. if(stream->cpath_layer == victim) {
  985. log_fn(LOG_INFO, "Marking stream %d for close.", stream->stream_id);
  986. /* no need to send 'end' relay cells,
  987. * because the other side's already dead
  988. */
  989. stream->marked_for_close = 1;
  990. stream->has_sent_end = 1;
  991. }
  992. }
  993. layer->next = victim->next;
  994. circuit_free_cpath_node(victim);
  995. }
  996. log_fn(LOG_INFO, "finished");
  997. return 0;
  998. }
  999. void assert_cpath_layer_ok(const crypt_path_t *cp)
  1000. {
  1001. assert(cp->f_crypto);
  1002. assert(cp->b_crypto);
  1003. assert(cp->addr);
  1004. assert(cp->port);
  1005. switch(cp->state)
  1006. {
  1007. case CPATH_STATE_CLOSED:
  1008. case CPATH_STATE_OPEN:
  1009. assert(!cp->handshake_state);
  1010. break;
  1011. case CPATH_STATE_AWAITING_KEYS:
  1012. assert(cp->handshake_state);
  1013. break;
  1014. default:
  1015. assert(0);
  1016. }
  1017. assert(cp->package_window >= 0);
  1018. assert(cp->deliver_window >= 0);
  1019. }
  1020. void assert_cpath_ok(const crypt_path_t *cp)
  1021. {
  1022. while(cp->prev)
  1023. cp = cp->prev;
  1024. while(cp->next) {
  1025. assert_cpath_layer_ok(cp);
  1026. /* layers must be in sequence of: "open* awaiting? closed*" */
  1027. if (cp->prev) {
  1028. if (cp->prev->state == CPATH_STATE_OPEN) {
  1029. assert(cp->state == CPATH_STATE_CLOSED ||
  1030. cp->state == CPATH_STATE_AWAITING_KEYS);
  1031. } else {
  1032. assert(cp->state == CPATH_STATE_CLOSED);
  1033. }
  1034. }
  1035. cp = cp->next;
  1036. }
  1037. }
  1038. void assert_circuit_ok(const circuit_t *c)
  1039. {
  1040. connection_t *conn;
  1041. assert(c->n_addr);
  1042. assert(c->n_port);
  1043. assert(c->n_conn);
  1044. assert(c->n_conn->type == CONN_TYPE_OR);
  1045. if (c->p_conn)
  1046. assert(c->p_conn->type == CONN_TYPE_OR);
  1047. for (conn = c->p_streams; conn; conn = conn->next_stream)
  1048. assert(c->p_conn->type == CONN_TYPE_EXIT);
  1049. for (conn = c->n_streams; conn; conn = conn->next_stream)
  1050. assert(conn->type == CONN_TYPE_EXIT);
  1051. assert(c->deliver_window >= 0);
  1052. assert(c->package_window >= 0);
  1053. if (c->state == CIRCUIT_STATE_OPEN) {
  1054. if (c->cpath) {
  1055. assert(!c->n_crypto);
  1056. assert(!c->p_crypto);
  1057. assert(!c->n_digest);
  1058. assert(!c->p_digest);
  1059. } else {
  1060. assert(c->n_crypto);
  1061. assert(c->p_crypto);
  1062. assert(c->n_digest);
  1063. assert(c->p_digest);
  1064. }
  1065. }
  1066. if (c->cpath) {
  1067. assert_cpath_ok(c->cpath);
  1068. }
  1069. }
  1070. /*
  1071. Local Variables:
  1072. mode:c
  1073. indent-tabs-mode:nil
  1074. c-basic-offset:2
  1075. End:
  1076. */