circuit.c 40 KB

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