circuit.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. /* Copyright 2001,2002 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. /********* START VARIABLES **********/
  7. static circuit_t *global_circuitlist=NULL;
  8. char *circuit_state_to_string[] = {
  9. "receiving the onion", /* 0 */
  10. "waiting to process create", /* 1 */
  11. "connecting to firsthop", /* 2 */
  12. "open" /* 3 */
  13. };
  14. /********* END VARIABLES ************/
  15. void circuit_add(circuit_t *circ) {
  16. if(!global_circuitlist) { /* first one */
  17. global_circuitlist = circ;
  18. circ->next = NULL;
  19. } else {
  20. circ->next = global_circuitlist;
  21. global_circuitlist = circ;
  22. }
  23. }
  24. void circuit_remove(circuit_t *circ) {
  25. circuit_t *tmpcirc;
  26. assert(circ && global_circuitlist);
  27. if(global_circuitlist == circ) {
  28. global_circuitlist = global_circuitlist->next;
  29. return;
  30. }
  31. for(tmpcirc = global_circuitlist;tmpcirc->next;tmpcirc = tmpcirc->next) {
  32. if(tmpcirc->next == circ) {
  33. tmpcirc->next = circ->next;
  34. return;
  35. }
  36. }
  37. }
  38. circuit_t *circuit_new(aci_t p_aci, connection_t *p_conn) {
  39. circuit_t *circ;
  40. struct timeval now;
  41. my_gettimeofday(&now);
  42. circ = (circuit_t *)tor_malloc(sizeof(circuit_t));
  43. memset(circ,0,sizeof(circuit_t)); /* zero it out */
  44. circ->timestamp_created = now.tv_sec;
  45. circ->p_aci = p_aci;
  46. circ->p_conn = p_conn;
  47. circ->state = CIRCUIT_STATE_ONIONSKIN_PENDING;
  48. /* ACIs */
  49. circ->p_aci = p_aci;
  50. /* circ->n_aci remains 0 because we haven't identified the next hop yet */
  51. circ->package_window = CIRCWINDOW_START;
  52. circ->deliver_window = CIRCWINDOW_START;
  53. circuit_add(circ);
  54. return circ;
  55. }
  56. void circuit_free(circuit_t *circ) {
  57. if (circ->n_crypto)
  58. crypto_free_cipher_env(circ->n_crypto);
  59. if (circ->p_crypto)
  60. crypto_free_cipher_env(circ->p_crypto);
  61. circuit_free_cpath(circ->cpath);
  62. free(circ);
  63. }
  64. void circuit_free_cpath(crypt_path_t *cpath) {
  65. crypt_path_t *victim, *head=cpath;
  66. if(!cpath)
  67. return;
  68. /* it's a doubly linked list, so we have to notice when we've
  69. * gone through it once. */
  70. while(cpath->next && cpath->next != head) {
  71. victim = cpath;
  72. cpath = victim->next;
  73. circuit_free_cpath_node(victim);
  74. }
  75. circuit_free_cpath_node(cpath);
  76. }
  77. void circuit_free_cpath_node(crypt_path_t *victim) {
  78. if(victim->f_crypto)
  79. crypto_free_cipher_env(victim->f_crypto);
  80. if(victim->b_crypto)
  81. crypto_free_cipher_env(victim->b_crypto);
  82. if(victim->handshake_state)
  83. crypto_dh_free(victim->handshake_state);
  84. free(victim);
  85. }
  86. /* return 0 if can't get a unique aci. */
  87. aci_t get_unique_aci_by_addr_port(uint32_t addr, uint16_t port, int aci_type) {
  88. aci_t test_aci;
  89. connection_t *conn;
  90. try_again:
  91. log_fn(LOG_DEBUG,"trying to get a unique aci");
  92. if (CRYPTO_PSEUDO_RAND_INT(test_aci))
  93. return -1;
  94. if(aci_type == ACI_TYPE_LOWER && test_aci >= (1<<15))
  95. test_aci -= (1<<15);
  96. if(aci_type == ACI_TYPE_HIGHER && test_aci < (1<<15))
  97. test_aci += (1<<15);
  98. /* if aci_type == ACI_BOTH, don't filter any of it */
  99. if(test_aci == 0)
  100. goto try_again;
  101. conn = connection_exact_get_by_addr_port(addr,port);
  102. if(!conn) /* there can't be a conflict -- no connection of that sort yet */
  103. return test_aci;
  104. if(circuit_get_by_aci_conn(test_aci, conn))
  105. goto try_again;
  106. return test_aci;
  107. }
  108. circuit_t *circuit_enumerate_by_naddr_nport(circuit_t *circ, uint32_t naddr, uint16_t nport) {
  109. if(!circ) /* use circ if it's defined, else start from the beginning */
  110. circ = global_circuitlist;
  111. else
  112. circ = circ->next;
  113. for( ; circ; circ = circ->next) {
  114. if(circ->n_addr == naddr && circ->n_port == nport)
  115. return circ;
  116. }
  117. return NULL;
  118. }
  119. circuit_t *circuit_get_by_aci_conn(aci_t aci, connection_t *conn) {
  120. circuit_t *circ;
  121. connection_t *tmpconn;
  122. for(circ=global_circuitlist;circ;circ = circ->next) {
  123. if(circ->p_aci == aci) {
  124. if(circ->p_conn == conn)
  125. return circ;
  126. for(tmpconn = circ->p_streams; tmpconn; tmpconn = tmpconn->next_stream) {
  127. if(tmpconn == conn)
  128. return circ;
  129. }
  130. }
  131. if(circ->n_aci == aci) {
  132. if(circ->n_conn == conn)
  133. return circ;
  134. for(tmpconn = circ->n_streams; tmpconn; tmpconn = tmpconn->next_stream) {
  135. if(tmpconn == conn)
  136. return circ;
  137. }
  138. }
  139. }
  140. return NULL;
  141. }
  142. circuit_t *circuit_get_by_conn(connection_t *conn) {
  143. circuit_t *circ;
  144. connection_t *tmpconn;
  145. for(circ=global_circuitlist;circ;circ = circ->next) {
  146. if(circ->p_conn == conn)
  147. return circ;
  148. if(circ->n_conn == conn)
  149. return circ;
  150. for(tmpconn = circ->p_streams; tmpconn; tmpconn=tmpconn->next_stream)
  151. if(tmpconn == conn)
  152. return circ;
  153. for(tmpconn = circ->n_streams; tmpconn; tmpconn=tmpconn->next_stream)
  154. if(tmpconn == conn)
  155. return circ;
  156. }
  157. return NULL;
  158. }
  159. circuit_t *circuit_get_newest_ap(void) {
  160. circuit_t *circ, *bestcirc=NULL;
  161. for(circ=global_circuitlist;circ;circ = circ->next) {
  162. if(circ->cpath && circ->state == CIRCUIT_STATE_OPEN && (!bestcirc ||
  163. bestcirc->timestamp_created < circ->timestamp_created)) {
  164. log_fn(LOG_DEBUG,"Choosing n_aci %d.", circ->n_aci);
  165. assert(circ->n_aci);
  166. bestcirc = circ;
  167. }
  168. }
  169. return bestcirc;
  170. }
  171. int circuit_deliver_relay_cell(cell_t *cell, circuit_t *circ,
  172. int cell_direction, crypt_path_t *layer_hint) {
  173. connection_t *conn=NULL;
  174. char recognized=0;
  175. char buf[256];
  176. assert(cell && circ);
  177. assert(cell_direction == CELL_DIRECTION_OUT || cell_direction == CELL_DIRECTION_IN);
  178. buf[0] = cell->length;
  179. memcpy(buf+1, cell->payload, CELL_PAYLOAD_SIZE);
  180. log_fn(LOG_DEBUG,"direction %d, streamid %d before crypt.", cell_direction, *(int*)(cell->payload+1));
  181. if(relay_crypt(circ, buf, 1+CELL_PAYLOAD_SIZE, cell_direction, &layer_hint, &recognized, &conn) < 0) {
  182. log_fn(LOG_DEBUG,"relay crypt failed. Dropping connection.");
  183. return -1;
  184. }
  185. cell->length = buf[0];
  186. memcpy(cell->payload, buf+1, CELL_PAYLOAD_SIZE);
  187. if(recognized) {
  188. if(cell_direction == CELL_DIRECTION_OUT) {
  189. log_fn(LOG_DEBUG,"Sending to exit.");
  190. return connection_edge_process_relay_cell(cell, circ, conn, EDGE_EXIT, NULL);
  191. }
  192. if(cell_direction == CELL_DIRECTION_IN) {
  193. log_fn(LOG_DEBUG,"Sending to AP.");
  194. return connection_edge_process_relay_cell(cell, circ, conn, EDGE_AP, layer_hint);
  195. }
  196. }
  197. /* not recognized. pass it on. */
  198. if(cell_direction == CELL_DIRECTION_OUT)
  199. conn = circ->n_conn;
  200. else
  201. conn = circ->p_conn;
  202. if(!conn) { //|| !connection_speaks_cells(conn)) {
  203. log_fn(LOG_INFO,"Didn't recognize cell (%d), but circ stops here! Dropping.", *(int *)(cell->payload+1));
  204. return 0;
  205. }
  206. log_fn(LOG_DEBUG,"Passing on unrecognized cell.");
  207. return connection_write_cell_to_buf(cell, conn);
  208. }
  209. int relay_crypt(circuit_t *circ, char *in, int inlen, char cell_direction,
  210. crypt_path_t **layer_hint, char *recognized, connection_t **conn) {
  211. crypt_path_t *thishop;
  212. char out[256];
  213. assert(circ && in && recognized && conn);
  214. assert(inlen < 256);
  215. if(cell_direction == CELL_DIRECTION_IN) {
  216. if(circ->cpath) { /* we're at the beginning of the circuit. We'll want to do layered crypts. */
  217. thishop = circ->cpath;
  218. if(thishop->state != CPATH_STATE_OPEN) {
  219. log_fn(LOG_INFO,"Relay cell before first created cell?");
  220. return -1;
  221. }
  222. do { /* Remember: cpath is in forward order, that is, first hop first. */
  223. assert(thishop);
  224. log_fn(LOG_DEBUG,"before decrypt: %d",*(int*)(in+2));
  225. /* decrypt */
  226. if(crypto_cipher_decrypt(thishop->b_crypto, in, inlen, out)) {
  227. log_fn(LOG_ERR,"Error performing decryption:%s",crypto_perror());
  228. return -1;
  229. }
  230. memcpy(in,out,inlen);
  231. log_fn(LOG_DEBUG,"after decrypt: %d",*(int*)(in+2));
  232. if( (*recognized = relay_check_recognized(circ, cell_direction, in+2, conn))) {
  233. *layer_hint = thishop;
  234. return 0;
  235. }
  236. thishop = thishop->next;
  237. } while(thishop != circ->cpath && thishop->state == CPATH_STATE_OPEN);
  238. log_fn(LOG_INFO,"in-cell at OP not recognized. Dropping.");
  239. return 0;
  240. } else { /* we're in the middle. Just one crypt. */
  241. log_fn(LOG_DEBUG,"before encrypt: %d",*(int*)(in+2));
  242. if(crypto_cipher_encrypt(circ->p_crypto, in, inlen, out)) {
  243. log_fn(LOG_ERR,"Encryption failed for ACI : %u (%s).",
  244. circ->p_aci, crypto_perror());
  245. return -1;
  246. }
  247. memcpy(in,out,inlen);
  248. log_fn(LOG_DEBUG,"after encrypt: %d",*(int*)(in+2));
  249. log_fn(LOG_DEBUG,"Skipping recognized check, because we're not the OP.");
  250. /* don't check for recognized. only the OP can recognize a stream on the way back. */
  251. }
  252. } else if(cell_direction == CELL_DIRECTION_OUT) {
  253. if(circ->cpath) { /* we're at the beginning of the circuit. We'll want to do layered crypts. */
  254. thishop = *layer_hint; /* we already know which layer, from when we package_raw_inbuf'ed */
  255. /* moving from last to first hop */
  256. do {
  257. assert(thishop);
  258. log_fn(LOG_DEBUG,"before encrypt: %d",*(int*)(in+2));
  259. if(crypto_cipher_encrypt(thishop->f_crypto, in, inlen, out)) {
  260. log_fn(LOG_ERR,"Error performing encryption:%s",crypto_perror());
  261. return -1;
  262. }
  263. memcpy(in,out,inlen);
  264. log_fn(LOG_DEBUG,"after encrypt: %d",*(int*)(in+2));
  265. thishop = thishop->prev;
  266. } while(thishop != circ->cpath->prev);
  267. } else { /* we're in the middle. Just one crypt. */
  268. if(crypto_cipher_decrypt(circ->n_crypto,in, inlen, out)) {
  269. log_fn(LOG_ERR,"Decryption failed for ACI : %u (%s).",
  270. circ->n_aci, crypto_perror());
  271. return -1;
  272. }
  273. memcpy(in,out,inlen);
  274. if( (*recognized = relay_check_recognized(circ, cell_direction, in+2, conn)))
  275. return 0;
  276. }
  277. } else {
  278. log_fn(LOG_ERR,"unknown cell direction %d.", cell_direction);
  279. assert(0);
  280. }
  281. return 0;
  282. }
  283. int relay_check_recognized(circuit_t *circ, int cell_direction, char *stream, connection_t **conn) {
  284. /* FIXME can optimize by passing thishop in */
  285. connection_t *tmpconn;
  286. if(!memcmp(stream,ZERO_STREAM,STREAM_ID_SIZE)) {
  287. log_fn(LOG_DEBUG,"It's the zero stream. Recognized.");
  288. return 1; /* the zero stream is always recognized */
  289. }
  290. log_fn(LOG_DEBUG,"not the zero stream.");
  291. if(cell_direction == CELL_DIRECTION_OUT)
  292. tmpconn = circ->n_streams;
  293. else
  294. tmpconn = circ->p_streams;
  295. if(!tmpconn) {
  296. log_fn(LOG_DEBUG,"No conns. Not recognized.");
  297. return 0;
  298. }
  299. for( ; tmpconn; tmpconn=tmpconn->next_stream) {
  300. if(!memcmp(stream,tmpconn->stream_id, STREAM_ID_SIZE)) {
  301. log_fn(LOG_DEBUG,"recognized stream %d.", *(int*)stream);
  302. *conn = tmpconn;
  303. return 1;
  304. }
  305. log_fn(LOG_DEBUG,"considered stream %d, not it.",*(int*)tmpconn->stream_id);
  306. }
  307. log_fn(LOG_DEBUG,"Didn't recognize. Giving up.");
  308. return 0;
  309. }
  310. void circuit_resume_edge_reading(circuit_t *circ, int edge_type, crypt_path_t *layer_hint) {
  311. connection_t *conn;
  312. assert(edge_type == EDGE_EXIT || edge_type == EDGE_AP);
  313. log_fn(LOG_DEBUG,"resuming");
  314. if(edge_type == EDGE_EXIT)
  315. conn = circ->n_streams;
  316. else
  317. conn = circ->p_streams;
  318. for( ; conn; conn=conn->next_stream) {
  319. if((edge_type == EDGE_EXIT && conn->package_window > 0) ||
  320. (edge_type == EDGE_AP && conn->package_window > 0 && conn->cpath_layer == layer_hint)) {
  321. connection_start_reading(conn);
  322. connection_package_raw_inbuf(conn); /* handle whatever might still be on the inbuf */
  323. }
  324. }
  325. circuit_consider_stop_edge_reading(circ, edge_type, layer_hint);
  326. }
  327. /* returns 1 if the window is empty, else 0. If it's empty, tell edge conns to stop reading. */
  328. int circuit_consider_stop_edge_reading(circuit_t *circ, int edge_type, crypt_path_t *layer_hint) {
  329. connection_t *conn = NULL;
  330. assert(edge_type == EDGE_EXIT || edge_type == EDGE_AP);
  331. assert(edge_type == EDGE_EXIT || layer_hint);
  332. log_fn(LOG_DEBUG,"considering");
  333. if(edge_type == EDGE_EXIT && circ->package_window <= 0)
  334. conn = circ->n_streams;
  335. else if(edge_type == EDGE_AP && layer_hint->package_window <= 0)
  336. conn = circ->p_streams;
  337. else
  338. return 0;
  339. for( ; conn; conn=conn->next_stream)
  340. if(!layer_hint || conn->cpath_layer == layer_hint)
  341. connection_stop_reading(conn);
  342. log_fn(LOG_DEBUG,"yes. stopped.");
  343. return 1;
  344. }
  345. int circuit_consider_sending_sendme(circuit_t *circ, int edge_type, crypt_path_t *layer_hint) {
  346. cell_t cell;
  347. assert(circ);
  348. memset(&cell, 0, sizeof(cell_t));
  349. cell.command = CELL_RELAY;
  350. SET_CELL_RELAY_COMMAND(cell, RELAY_COMMAND_SENDME);
  351. SET_CELL_STREAM_ID(cell, ZERO_STREAM);
  352. cell.length = RELAY_HEADER_SIZE;
  353. if(edge_type == EDGE_AP) { /* i'm the AP */
  354. cell.aci = circ->n_aci;
  355. while(layer_hint->deliver_window < CIRCWINDOW_START-CIRCWINDOW_INCREMENT) {
  356. log_fn(LOG_DEBUG,"deliver_window %d, Queueing sendme forward.", layer_hint->deliver_window);
  357. layer_hint->deliver_window += CIRCWINDOW_INCREMENT;
  358. if(circuit_deliver_relay_cell(&cell, circ, CELL_DIRECTION_OUT, layer_hint) < 0) {
  359. return -1;
  360. }
  361. }
  362. } else if(edge_type == EDGE_EXIT) { /* i'm the exit */
  363. cell.aci = circ->p_aci;
  364. while(circ->deliver_window < CIRCWINDOW_START-CIRCWINDOW_INCREMENT) {
  365. log_fn(LOG_DEBUG,"deliver_window %d, Queueing sendme back.", circ->deliver_window);
  366. circ->deliver_window += CIRCWINDOW_INCREMENT;
  367. if(circuit_deliver_relay_cell(&cell, circ, CELL_DIRECTION_IN, layer_hint) < 0) {
  368. return -1;
  369. }
  370. }
  371. }
  372. return 0;
  373. }
  374. void circuit_close(circuit_t *circ) {
  375. connection_t *conn;
  376. circuit_t *youngest=NULL;
  377. assert(circ);
  378. if(options.APPort) {
  379. youngest = circuit_get_newest_ap();
  380. log_fn(LOG_DEBUG,"youngest %d, circ %d.",youngest, (int)circ);
  381. }
  382. circuit_remove(circ);
  383. if(circ->n_conn)
  384. connection_send_destroy(circ->n_aci, circ->n_conn);
  385. for(conn=circ->n_streams; conn; conn=conn->next_stream) {
  386. connection_send_destroy(circ->n_aci, conn);
  387. }
  388. if(circ->p_conn)
  389. connection_send_destroy(circ->n_aci, circ->p_conn);
  390. for(conn=circ->p_streams; conn; conn=conn->next_stream) {
  391. connection_send_destroy(circ->p_aci, conn);
  392. }
  393. if(options.APPort && youngest == circ) { /* check this after we've sent the destroys, to reduce races */
  394. /* our current circuit just died. Launch another one pronto. */
  395. log_fn(LOG_INFO,"Youngest circuit dying. Launching a replacement.");
  396. circuit_launch_new(1);
  397. }
  398. circuit_free(circ);
  399. }
  400. void circuit_about_to_close_connection(connection_t *conn) {
  401. /* send destroys for all circuits using conn */
  402. /* currently, we assume it's too late to flush conn's buf here.
  403. * down the road, maybe we'll consider that eof doesn't mean can't-write
  404. */
  405. circuit_t *circ;
  406. connection_t *prevconn;
  407. if(!connection_speaks_cells(conn)) {
  408. /* it's an edge conn. need to remove it from the linked list of
  409. * conn's for this circuit. Send an 'end' relay command.
  410. * But don't kill the circuit.
  411. */
  412. circ = circuit_get_by_conn(conn);
  413. if(!circ)
  414. return;
  415. if(conn == circ->p_streams) {
  416. circ->p_streams = conn->next_stream;
  417. goto send_end;
  418. }
  419. if(conn == circ->n_streams) {
  420. circ->n_streams = conn->next_stream;
  421. goto send_end;
  422. }
  423. for(prevconn = circ->p_streams; prevconn && prevconn->next_stream && prevconn->next_stream != conn; prevconn = prevconn->next_stream) ;
  424. if(prevconn && prevconn->next_stream) {
  425. prevconn->next_stream = conn->next_stream;
  426. goto send_end;
  427. }
  428. for(prevconn = circ->n_streams; prevconn && prevconn->next_stream && prevconn->next_stream != conn; prevconn = prevconn->next_stream) ;
  429. if(prevconn && prevconn->next_stream) {
  430. prevconn->next_stream = conn->next_stream;
  431. goto send_end;
  432. }
  433. log_fn(LOG_ERR,"edge conn not in circuit's list?");
  434. assert(0); /* should never get here */
  435. send_end:
  436. if(connection_edge_send_command(conn, circ, RELAY_COMMAND_END) < 0) {
  437. log_fn(LOG_DEBUG,"sending end failed. Closing.");
  438. circuit_close(circ);
  439. }
  440. return;
  441. }
  442. /* this connection speaks cells. We must close all the circuits on it. */
  443. while((circ = circuit_get_by_conn(conn))) {
  444. if(circ->n_conn == conn) /* it's closing in front of us */
  445. circ->n_conn = NULL;
  446. if(circ->p_conn == conn) /* it's closing behind us */
  447. circ->p_conn = NULL;
  448. circuit_close(circ);
  449. }
  450. }
  451. /* FIXME this now leaves some out */
  452. void circuit_dump_by_conn(connection_t *conn) {
  453. circuit_t *circ;
  454. connection_t *tmpconn;
  455. for(circ=global_circuitlist;circ;circ = circ->next) {
  456. if(circ->p_conn == conn)
  457. printf("Conn %d has App-ward circuit: aci %d (other side %d), state %d (%s)\n",
  458. conn->poll_index, circ->p_aci, circ->n_aci, circ->state, circuit_state_to_string[circ->state]);
  459. for(tmpconn=circ->p_streams; tmpconn; tmpconn=tmpconn->next_stream) {
  460. if(tmpconn == conn) {
  461. printf("Conn %d has App-ward circuit: aci %d (other side %d), state %d (%s)\n",
  462. conn->poll_index, circ->p_aci, circ->n_aci, circ->state, circuit_state_to_string[circ->state]);
  463. }
  464. }
  465. if(circ->n_conn == conn)
  466. printf("Conn %d has Exit-ward circuit: aci %d (other side %d), state %d (%s)\n",
  467. conn->poll_index, circ->n_aci, circ->p_aci, circ->state, circuit_state_to_string[circ->state]);
  468. for(tmpconn=circ->n_streams; tmpconn; tmpconn=tmpconn->next_stream) {
  469. if(tmpconn == conn) {
  470. printf("Conn %d has Exit-ward circuit: aci %d (other side %d), state %d (%s)\n",
  471. conn->poll_index, circ->n_aci, circ->p_aci, circ->state, circuit_state_to_string[circ->state]);
  472. }
  473. }
  474. }
  475. }
  476. void circuit_expire_unused_circuits(void) {
  477. circuit_t *circ, *tmpcirc;
  478. circuit_t *youngest;
  479. youngest = circuit_get_newest_ap();
  480. circ = global_circuitlist;
  481. while(circ) {
  482. tmpcirc = circ;
  483. circ = circ->next;
  484. if(tmpcirc != youngest && !tmpcirc->p_conn && !tmpcirc->p_streams) {
  485. log_fn(LOG_DEBUG,"Closing n_aci %d",tmpcirc->n_aci);
  486. circuit_close(tmpcirc);
  487. }
  488. }
  489. }
  490. /* failure_status code: negative means reset failures to 0. Other values mean
  491. * add that value to the current number of failures, then if we don't have too
  492. * many failures on record, try to make a new circuit.
  493. */
  494. void circuit_launch_new(int failure_status) {
  495. static int failures=0;
  496. if(!options.APPort) /* we're not an application proxy. no need for circuits. */
  497. return;
  498. if(failure_status == -1) { /* I was called because a circuit succeeded */
  499. failures = 0;
  500. return;
  501. }
  502. failures += failure_status;
  503. retry_circuit:
  504. if(failures > 5) {
  505. log_fn(LOG_INFO,"Giving up, %d failures.", failures);
  506. return;
  507. }
  508. if(circuit_establish_circuit() < 0) {
  509. failures++;
  510. goto retry_circuit;
  511. }
  512. failures = 0;
  513. return;
  514. }
  515. int circuit_establish_circuit(void) {
  516. routerinfo_t *firsthop;
  517. connection_t *n_conn;
  518. circuit_t *circ;
  519. circ = circuit_new(0, NULL); /* sets circ->p_aci and circ->p_conn */
  520. circ->state = CIRCUIT_STATE_OR_WAIT;
  521. circ->cpath = onion_generate_cpath(&firsthop);
  522. if(!circ->cpath) {
  523. log_fn(LOG_DEBUG,"Generating cpath failed.");
  524. circuit_close(circ);
  525. return -1;
  526. }
  527. /* now see if we're already connected to the first OR in 'route' */
  528. log_fn(LOG_DEBUG,"Looking for firsthop '%s:%u'",
  529. firsthop->address,firsthop->or_port);
  530. n_conn = connection_twin_get_by_addr_port(firsthop->addr,firsthop->or_port);
  531. if(!n_conn || n_conn->state != OR_CONN_STATE_OPEN) { /* not currently connected */
  532. circ->n_addr = firsthop->addr;
  533. circ->n_port = firsthop->or_port;
  534. if(options.OnionRouter) { /* we would be connected if he were up. but he's not. */
  535. log_fn(LOG_DEBUG,"Route's firsthop isn't connected.");
  536. circuit_close(circ);
  537. return -1;
  538. }
  539. if(!n_conn) { /* launch the connection */
  540. n_conn = connection_or_connect(firsthop);
  541. if(!n_conn) { /* connect failed, forget the whole thing */
  542. log_fn(LOG_DEBUG,"connect to firsthop failed. Closing.");
  543. circuit_close(circ);
  544. return -1;
  545. }
  546. }
  547. log_fn(LOG_DEBUG,"connecting in progress (or finished). Good.");
  548. return 0; /* return success. The onion/circuit/etc will be taken care of automatically
  549. * (may already have been) whenever n_conn reaches OR_CONN_STATE_OPEN.
  550. */
  551. } else { /* it (or a twin) is already open. use it. */
  552. circ->n_addr = n_conn->addr;
  553. circ->n_port = n_conn->port;
  554. circ->n_conn = n_conn;
  555. log_fn(LOG_DEBUG,"Conn open. Delivering first onion skin.");
  556. if(circuit_send_next_onion_skin(circ) < 0) {
  557. log_fn(LOG_DEBUG,"circuit_send_next_onion_skin failed.");
  558. circuit_close(circ);
  559. return -1;
  560. }
  561. }
  562. return 0;
  563. }
  564. /* find circuits that are waiting on me, if any, and get them to send the onion */
  565. void circuit_n_conn_open(connection_t *or_conn) {
  566. circuit_t *circ;
  567. log_fn(LOG_DEBUG,"Starting.");
  568. circ = circuit_enumerate_by_naddr_nport(NULL, or_conn->addr, or_conn->port);
  569. for(;;) {
  570. if(!circ)
  571. return;
  572. log_fn(LOG_DEBUG,"Found circ, sending onion skin.");
  573. circ->n_conn = or_conn;
  574. if(circuit_send_next_onion_skin(circ) < 0) {
  575. log_fn(LOG_DEBUG,"circuit marked for closing.");
  576. circuit_close(circ);
  577. return; /* FIXME will want to try the other circuits too? */
  578. }
  579. circ = circuit_enumerate_by_naddr_nport(circ, or_conn->addr, or_conn->port);
  580. }
  581. }
  582. int circuit_send_next_onion_skin(circuit_t *circ) {
  583. cell_t cell;
  584. crypt_path_t *hop;
  585. routerinfo_t *router;
  586. assert(circ && circ->cpath);
  587. if(circ->cpath->state == CPATH_STATE_CLOSED) {
  588. log_fn(LOG_DEBUG,"First skin; sending create cell.");
  589. circ->n_aci = get_unique_aci_by_addr_port(circ->n_addr, circ->n_port, ACI_TYPE_BOTH);
  590. memset(&cell, 0, sizeof(cell_t));
  591. cell.command = CELL_CREATE;
  592. cell.aci = circ->n_aci;
  593. cell.length = DH_ONIONSKIN_LEN;
  594. if(onion_skin_create(circ->n_conn->pkey, &(circ->cpath->handshake_state), cell.payload) < 0) {
  595. log_fn(LOG_INFO,"onion_skin_create (first hop) failed.");
  596. return -1;
  597. }
  598. if(connection_write_cell_to_buf(&cell, circ->n_conn) < 0) {
  599. return -1;
  600. }
  601. circ->cpath->state = CPATH_STATE_AWAITING_KEYS;
  602. circ->state = CIRCUIT_STATE_BUILDING;
  603. log_fn(LOG_DEBUG,"first skin; finished sending create cell.");
  604. } else {
  605. assert(circ->cpath->state == CPATH_STATE_OPEN);
  606. assert(circ->state == CIRCUIT_STATE_BUILDING);
  607. log_fn(LOG_DEBUG,"starting to send subsequent skin.");
  608. for(hop=circ->cpath->next;
  609. hop != circ->cpath && hop->state == CPATH_STATE_OPEN;
  610. hop=hop->next) ;
  611. if(hop == circ->cpath) { /* done building the circuit. whew. */
  612. circ->state = CIRCUIT_STATE_OPEN;
  613. log_fn(LOG_DEBUG,"circuit built!");
  614. return 0;
  615. }
  616. router = router_get_by_addr_port(hop->addr,hop->port);
  617. if(!router) {
  618. log_fn(LOG_INFO,"couldn't lookup router %d:%d",hop->addr,hop->port);
  619. return -1;
  620. }
  621. memset(&cell, 0, sizeof(cell_t));
  622. cell.command = CELL_RELAY;
  623. cell.aci = circ->n_aci;
  624. SET_CELL_RELAY_COMMAND(cell, RELAY_COMMAND_EXTEND);
  625. SET_CELL_STREAM_ID(cell, ZERO_STREAM);
  626. cell.length = RELAY_HEADER_SIZE + 6 + DH_ONIONSKIN_LEN;
  627. *(uint32_t*)(cell.payload+RELAY_HEADER_SIZE) = htonl(hop->addr);
  628. *(uint32_t*)(cell.payload+RELAY_HEADER_SIZE+4) = htons(hop->port);
  629. if(onion_skin_create(router->pkey, &(hop->handshake_state), cell.payload+RELAY_HEADER_SIZE+6) < 0) {
  630. log_fn(LOG_INFO,"onion_skin_create failed.");
  631. return -1;
  632. }
  633. log_fn(LOG_DEBUG,"Sending extend relay cell.");
  634. /* send it to hop->prev, because it will transfer it to a create cell and then send to hop */
  635. if(circuit_deliver_relay_cell(&cell, circ, CELL_DIRECTION_OUT, hop->prev) < 0) {
  636. log_fn(LOG_DEBUG,"failed to deliver extend cell. Closing.");
  637. return -1;
  638. }
  639. hop->state = CPATH_STATE_AWAITING_KEYS;
  640. }
  641. return 0;
  642. }
  643. /* take the 'extend' cell, pull out addr/port plus the onion skin. Connect
  644. * to the next hop, and pass it the onion skin in a create cell.
  645. */
  646. int circuit_extend(cell_t *cell, circuit_t *circ) {
  647. connection_t *n_conn;
  648. aci_t aci_type;
  649. struct sockaddr_in me; /* my router identity */
  650. cell_t newcell;
  651. if(circ->n_conn) {
  652. log_fn(LOG_WARNING,"n_conn already set. Bug/attack. Closing.");
  653. return -1;
  654. }
  655. circ->n_addr = ntohl(*(uint32_t*)(cell->payload+RELAY_HEADER_SIZE));
  656. circ->n_port = ntohs(*(uint16_t*)(cell->payload+RELAY_HEADER_SIZE+4));
  657. if(learn_my_address(&me) < 0)
  658. return -1;
  659. n_conn = connection_twin_get_by_addr_port(circ->n_addr,circ->n_port);
  660. if(!n_conn || n_conn->type != CONN_TYPE_OR) {
  661. /* i've disabled making connections through OPs, but it's definitely
  662. * possible here. I'm not sure if it would be a bug or a feature. -RD
  663. */
  664. /* note also that this will close circuits where the onion has the same
  665. * router twice in a row in the path. i think that's ok. -RD
  666. */
  667. log_fn(LOG_DEBUG,"Next router not connected. Closing.");
  668. /* XXX later we should fail more gracefully here, like with a 'truncated' */
  669. return -1;
  670. }
  671. circ->n_addr = n_conn->addr; /* these are different if we found a twin instead */
  672. circ->n_port = n_conn->port;
  673. circ->n_conn = n_conn;
  674. log_fn(LOG_DEBUG,"n_conn is %s:%u",n_conn->address,n_conn->port);
  675. aci_type = decide_aci_type(ntohl(me.sin_addr.s_addr), ntohs(me.sin_port),
  676. circ->n_addr, circ->n_port);
  677. log_fn(LOG_DEBUG,"aci_type = %u.",aci_type);
  678. circ->n_aci = get_unique_aci_by_addr_port(circ->n_addr, circ->n_port, aci_type);
  679. if(!circ->n_aci) {
  680. log_fn(LOG_ERR,"failed to get unique aci.");
  681. return -1;
  682. }
  683. log_fn(LOG_DEBUG,"Chosen ACI %u.",circ->n_aci);
  684. memset(&newcell, 0, sizeof(cell_t));
  685. newcell.command = CELL_CREATE;
  686. newcell.aci = circ->n_aci;
  687. newcell.length = DH_ONIONSKIN_LEN;
  688. memcpy(newcell.payload, cell->payload+RELAY_HEADER_SIZE+6, DH_ONIONSKIN_LEN);
  689. if(connection_write_cell_to_buf(&newcell, circ->n_conn) < 0) {
  690. return -1;
  691. }
  692. return 0;
  693. }
  694. int circuit_finish_handshake(circuit_t *circ, char *reply) {
  695. unsigned char iv[16];
  696. unsigned char keys[32];
  697. crypt_path_t *hop;
  698. memset(iv, 0, 16);
  699. assert(circ->cpath);
  700. if(circ->cpath->state == CPATH_STATE_AWAITING_KEYS)
  701. hop = circ->cpath;
  702. else {
  703. for(hop=circ->cpath->next;
  704. hop != circ->cpath && hop->state == CPATH_STATE_OPEN;
  705. hop=hop->next) ;
  706. if(hop == circ->cpath) { /* got an extended when we're all done? */
  707. log_fn(LOG_INFO,"got extended when circ already built? Weird.");
  708. return 0;
  709. }
  710. }
  711. assert(hop->state == CPATH_STATE_AWAITING_KEYS);
  712. if(onion_skin_client_handshake(hop->handshake_state, reply, keys, 32) < 0) {
  713. log_fn(LOG_ERR,"onion_skin_client_handshake failed.");
  714. return -1;
  715. }
  716. crypto_dh_free(hop->handshake_state); /* don't need it anymore */
  717. hop->handshake_state = NULL;
  718. log_fn(LOG_DEBUG,"hop %d init cipher forward %d, backward %d.", (int)hop, *(int*)keys, *(int*)(keys+16));
  719. if (!(hop->f_crypto =
  720. crypto_create_init_cipher(DEFAULT_CIPHER,keys,iv,1))) {
  721. log(LOG_ERR,"Cipher initialization failed.");
  722. return -1;
  723. }
  724. if (!(hop->b_crypto =
  725. crypto_create_init_cipher(DEFAULT_CIPHER,keys+16,iv,0))) {
  726. log(LOG_ERR,"Cipher initialization failed.");
  727. return -1;
  728. }
  729. hop->state = CPATH_STATE_OPEN;
  730. log_fn(LOG_DEBUG,"Completed.");
  731. return 0;
  732. }
  733. int circuit_truncated(circuit_t *circ, crypt_path_t *layer) {
  734. crypt_path_t *victim;
  735. connection_t *stream;
  736. assert(circ);
  737. assert(layer);
  738. while(layer->next != circ->cpath) {
  739. /* we need to clear out layer->next */
  740. victim = layer->next;
  741. log_fn(LOG_DEBUG, "Killing a layer of the cpath.");
  742. for(stream = circ->p_streams; stream; stream=stream->next_stream) {
  743. if(stream->cpath_layer == victim) {
  744. log_fn(LOG_DEBUG, "Marking stream %d for close.", *(int*)stream->stream_id);
  745. stream->marked_for_close = 1;
  746. }
  747. }
  748. layer->next = victim->next;
  749. circuit_free_cpath_node(victim);
  750. }
  751. log_fn(LOG_DEBUG, "Complete.");
  752. return 0;
  753. }
  754. /*
  755. Local Variables:
  756. mode:c
  757. indent-tabs-mode:nil
  758. c-basic-offset:2
  759. End:
  760. */