circuit.c 38 KB

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