circuit.c 40 KB

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