circuit.c 38 KB

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