circuit.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  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. if(circuit_consider_stop_edge_reading(circ, edge_type, layer_hint))
  324. return;
  325. }
  326. }
  327. }
  328. /* returns 1 if the window is empty, else 0. If it's empty, tell edge conns to stop reading. */
  329. int circuit_consider_stop_edge_reading(circuit_t *circ, int edge_type, crypt_path_t *layer_hint) {
  330. connection_t *conn = NULL;
  331. assert(edge_type == EDGE_EXIT || edge_type == EDGE_AP);
  332. assert(edge_type == EDGE_EXIT || layer_hint);
  333. log_fn(LOG_DEBUG,"considering");
  334. if(edge_type == EDGE_EXIT && circ->package_window <= 0)
  335. conn = circ->n_streams;
  336. else if(edge_type == EDGE_AP && layer_hint->package_window <= 0)
  337. conn = circ->p_streams;
  338. else
  339. return 0;
  340. for( ; conn; conn=conn->next_stream)
  341. if(!layer_hint || conn->cpath_layer == layer_hint)
  342. connection_stop_reading(conn);
  343. log_fn(LOG_DEBUG,"yes. stopped.");
  344. return 1;
  345. }
  346. int circuit_consider_sending_sendme(circuit_t *circ, int edge_type, crypt_path_t *layer_hint) {
  347. cell_t cell;
  348. assert(circ);
  349. memset(&cell, 0, sizeof(cell_t));
  350. cell.command = CELL_RELAY;
  351. SET_CELL_RELAY_COMMAND(cell, RELAY_COMMAND_SENDME);
  352. SET_CELL_STREAM_ID(cell, ZERO_STREAM);
  353. cell.length = RELAY_HEADER_SIZE;
  354. if(edge_type == EDGE_AP) { /* i'm the AP */
  355. cell.aci = circ->n_aci;
  356. while(layer_hint->deliver_window < CIRCWINDOW_START-CIRCWINDOW_INCREMENT) {
  357. log_fn(LOG_DEBUG,"deliver_window %d, Queueing sendme forward.", layer_hint->deliver_window);
  358. layer_hint->deliver_window += CIRCWINDOW_INCREMENT;
  359. if(circuit_deliver_relay_cell(&cell, circ, CELL_DIRECTION_OUT, layer_hint) < 0) {
  360. return -1;
  361. }
  362. }
  363. } else if(edge_type == EDGE_EXIT) { /* i'm the exit */
  364. cell.aci = circ->p_aci;
  365. while(circ->deliver_window < CIRCWINDOW_START-CIRCWINDOW_INCREMENT) {
  366. log_fn(LOG_DEBUG,"deliver_window %d, Queueing sendme back.", circ->deliver_window);
  367. circ->deliver_window += CIRCWINDOW_INCREMENT;
  368. if(circuit_deliver_relay_cell(&cell, circ, CELL_DIRECTION_IN, layer_hint) < 0) {
  369. return -1;
  370. }
  371. }
  372. }
  373. return 0;
  374. }
  375. void circuit_close(circuit_t *circ) {
  376. connection_t *conn;
  377. circuit_t *youngest=NULL;
  378. assert(circ);
  379. if(options.APPort) {
  380. youngest = circuit_get_newest_ap();
  381. log_fn(LOG_DEBUG,"youngest %d, circ %d.",(int)youngest, (int)circ);
  382. }
  383. circuit_remove(circ);
  384. if(circ->n_conn)
  385. connection_send_destroy(circ->n_aci, circ->n_conn);
  386. for(conn=circ->n_streams; conn; conn=conn->next_stream) {
  387. connection_send_destroy(circ->n_aci, conn);
  388. }
  389. if(circ->p_conn)
  390. connection_send_destroy(circ->n_aci, circ->p_conn);
  391. for(conn=circ->p_streams; conn; conn=conn->next_stream) {
  392. connection_send_destroy(circ->p_aci, conn);
  393. }
  394. if(options.APPort && youngest == circ) { /* check this after we've sent the destroys, to reduce races */
  395. /* our current circuit just died. Launch another one pronto. */
  396. log_fn(LOG_INFO,"Youngest circuit dying. Launching a replacement.");
  397. circuit_launch_new(1);
  398. }
  399. circuit_free(circ);
  400. }
  401. void circuit_about_to_close_connection(connection_t *conn) {
  402. /* send destroys for all circuits using conn */
  403. /* currently, we assume it's too late to flush conn's buf here.
  404. * down the road, maybe we'll consider that eof doesn't mean can't-write
  405. */
  406. circuit_t *circ;
  407. connection_t *prevconn;
  408. if(!connection_speaks_cells(conn)) {
  409. /* it's an edge conn. need to remove it from the linked list of
  410. * conn's for this circuit. Send an 'end' relay command.
  411. * But don't kill the circuit.
  412. */
  413. circ = circuit_get_by_conn(conn);
  414. if(!circ)
  415. return;
  416. if(conn == circ->p_streams) {
  417. circ->p_streams = conn->next_stream;
  418. goto send_end;
  419. }
  420. if(conn == circ->n_streams) {
  421. circ->n_streams = conn->next_stream;
  422. goto send_end;
  423. }
  424. for(prevconn = circ->p_streams; prevconn && prevconn->next_stream && prevconn->next_stream != conn; prevconn = prevconn->next_stream) ;
  425. if(prevconn && prevconn->next_stream) {
  426. prevconn->next_stream = conn->next_stream;
  427. goto send_end;
  428. }
  429. for(prevconn = circ->n_streams; prevconn && prevconn->next_stream && prevconn->next_stream != conn; prevconn = prevconn->next_stream) ;
  430. if(prevconn && prevconn->next_stream) {
  431. prevconn->next_stream = conn->next_stream;
  432. goto send_end;
  433. }
  434. log_fn(LOG_ERR,"edge conn not in circuit's list?");
  435. assert(0); /* should never get here */
  436. send_end:
  437. if(connection_edge_send_command(conn, circ, RELAY_COMMAND_END) < 0) {
  438. log_fn(LOG_DEBUG,"sending end failed. Closing.");
  439. circuit_close(circ);
  440. }
  441. return;
  442. }
  443. /* this connection speaks cells. We must close all the circuits on it. */
  444. while((circ = circuit_get_by_conn(conn))) {
  445. if(circ->n_conn == conn) /* it's closing in front of us */
  446. circ->n_conn = NULL;
  447. if(circ->p_conn == conn) /* it's closing behind us */
  448. circ->p_conn = NULL;
  449. circuit_close(circ);
  450. }
  451. }
  452. /* FIXME this now leaves some out */
  453. void circuit_dump_by_conn(connection_t *conn) {
  454. circuit_t *circ;
  455. connection_t *tmpconn;
  456. for(circ=global_circuitlist;circ;circ = circ->next) {
  457. if(circ->p_conn == conn)
  458. printf("Conn %d has App-ward circuit: aci %d (other side %d), state %d (%s)\n",
  459. conn->poll_index, circ->p_aci, circ->n_aci, circ->state, circuit_state_to_string[circ->state]);
  460. for(tmpconn=circ->p_streams; tmpconn; tmpconn=tmpconn->next_stream) {
  461. if(tmpconn == conn) {
  462. printf("Conn %d has App-ward circuit: aci %d (other side %d), state %d (%s)\n",
  463. conn->poll_index, circ->p_aci, circ->n_aci, circ->state, circuit_state_to_string[circ->state]);
  464. }
  465. }
  466. if(circ->n_conn == conn)
  467. printf("Conn %d has Exit-ward circuit: aci %d (other side %d), state %d (%s)\n",
  468. conn->poll_index, circ->n_aci, circ->p_aci, circ->state, circuit_state_to_string[circ->state]);
  469. for(tmpconn=circ->n_streams; tmpconn; tmpconn=tmpconn->next_stream) {
  470. if(tmpconn == conn) {
  471. printf("Conn %d has Exit-ward circuit: aci %d (other side %d), state %d (%s)\n",
  472. conn->poll_index, circ->n_aci, circ->p_aci, circ->state, circuit_state_to_string[circ->state]);
  473. }
  474. }
  475. }
  476. }
  477. void circuit_expire_unused_circuits(void) {
  478. circuit_t *circ, *tmpcirc;
  479. circuit_t *youngest;
  480. youngest = circuit_get_newest_ap();
  481. circ = global_circuitlist;
  482. while(circ) {
  483. tmpcirc = circ;
  484. circ = circ->next;
  485. if(tmpcirc != youngest && !tmpcirc->p_conn && !tmpcirc->p_streams) {
  486. log_fn(LOG_DEBUG,"Closing n_aci %d",tmpcirc->n_aci);
  487. circuit_close(tmpcirc);
  488. }
  489. }
  490. }
  491. /* failure_status code: negative means reset failures to 0. Other values mean
  492. * add that value to the current number of failures, then if we don't have too
  493. * many failures on record, try to make a new circuit.
  494. */
  495. void circuit_launch_new(int failure_status) {
  496. static int failures=0;
  497. if(!options.APPort) /* we're not an application proxy. no need for circuits. */
  498. return;
  499. if(failure_status == -1) { /* I was called because a circuit succeeded */
  500. failures = 0;
  501. return;
  502. }
  503. failures += failure_status;
  504. retry_circuit:
  505. if(failures > 5) {
  506. log_fn(LOG_INFO,"Giving up, %d failures.", failures);
  507. return;
  508. }
  509. if(circuit_establish_circuit() < 0) {
  510. failures++;
  511. goto retry_circuit;
  512. }
  513. failures = 0;
  514. return;
  515. }
  516. int circuit_establish_circuit(void) {
  517. routerinfo_t *firsthop;
  518. connection_t *n_conn;
  519. circuit_t *circ;
  520. circ = circuit_new(0, NULL); /* sets circ->p_aci and circ->p_conn */
  521. circ->state = CIRCUIT_STATE_OR_WAIT;
  522. circ->cpath = onion_generate_cpath(&firsthop);
  523. if(!circ->cpath) {
  524. log_fn(LOG_DEBUG,"Generating cpath failed.");
  525. circuit_close(circ);
  526. return -1;
  527. }
  528. /* now see if we're already connected to the first OR in 'route' */
  529. log_fn(LOG_DEBUG,"Looking for firsthop '%s:%u'",
  530. firsthop->address,firsthop->or_port);
  531. n_conn = connection_twin_get_by_addr_port(firsthop->addr,firsthop->or_port);
  532. if(!n_conn || n_conn->state != OR_CONN_STATE_OPEN) { /* not currently connected */
  533. circ->n_addr = firsthop->addr;
  534. circ->n_port = firsthop->or_port;
  535. if(options.OnionRouter) { /* we would be connected if he were up. but he's not. */
  536. log_fn(LOG_DEBUG,"Route's firsthop isn't connected.");
  537. circuit_close(circ);
  538. return -1;
  539. }
  540. if(!n_conn) { /* launch the connection */
  541. n_conn = connection_or_connect(firsthop);
  542. if(!n_conn) { /* connect failed, forget the whole thing */
  543. log_fn(LOG_DEBUG,"connect to firsthop failed. Closing.");
  544. circuit_close(circ);
  545. return -1;
  546. }
  547. }
  548. log_fn(LOG_DEBUG,"connecting in progress (or finished). Good.");
  549. return 0; /* return success. The onion/circuit/etc will be taken care of automatically
  550. * (may already have been) whenever n_conn reaches OR_CONN_STATE_OPEN.
  551. */
  552. } else { /* it (or a twin) is already open. use it. */
  553. circ->n_addr = n_conn->addr;
  554. circ->n_port = n_conn->port;
  555. circ->n_conn = n_conn;
  556. log_fn(LOG_DEBUG,"Conn open. Delivering first onion skin.");
  557. if(circuit_send_next_onion_skin(circ) < 0) {
  558. log_fn(LOG_DEBUG,"circuit_send_next_onion_skin failed.");
  559. circuit_close(circ);
  560. return -1;
  561. }
  562. }
  563. return 0;
  564. }
  565. /* find circuits that are waiting on me, if any, and get them to send the onion */
  566. void circuit_n_conn_open(connection_t *or_conn) {
  567. circuit_t *circ;
  568. log_fn(LOG_DEBUG,"Starting.");
  569. circ = circuit_enumerate_by_naddr_nport(NULL, or_conn->addr, or_conn->port);
  570. for(;;) {
  571. if(!circ)
  572. return;
  573. log_fn(LOG_DEBUG,"Found circ, sending onion skin.");
  574. circ->n_conn = or_conn;
  575. if(circuit_send_next_onion_skin(circ) < 0) {
  576. log_fn(LOG_DEBUG,"circuit marked for closing.");
  577. circuit_close(circ);
  578. return; /* FIXME will want to try the other circuits too? */
  579. }
  580. circ = circuit_enumerate_by_naddr_nport(circ, or_conn->addr, or_conn->port);
  581. }
  582. }
  583. int circuit_send_next_onion_skin(circuit_t *circ) {
  584. cell_t cell;
  585. crypt_path_t *hop;
  586. routerinfo_t *router;
  587. assert(circ && circ->cpath);
  588. if(circ->cpath->state == CPATH_STATE_CLOSED) {
  589. log_fn(LOG_DEBUG,"First skin; sending create cell.");
  590. circ->n_aci = get_unique_aci_by_addr_port(circ->n_addr, circ->n_port, ACI_TYPE_BOTH);
  591. memset(&cell, 0, sizeof(cell_t));
  592. cell.command = CELL_CREATE;
  593. cell.aci = circ->n_aci;
  594. cell.length = DH_ONIONSKIN_LEN;
  595. if(onion_skin_create(circ->n_conn->pkey, &(circ->cpath->handshake_state), cell.payload) < 0) {
  596. log_fn(LOG_INFO,"onion_skin_create (first hop) failed.");
  597. return -1;
  598. }
  599. if(connection_write_cell_to_buf(&cell, circ->n_conn) < 0) {
  600. return -1;
  601. }
  602. circ->cpath->state = CPATH_STATE_AWAITING_KEYS;
  603. circ->state = CIRCUIT_STATE_BUILDING;
  604. log_fn(LOG_DEBUG,"first skin; finished sending create cell.");
  605. } else {
  606. assert(circ->cpath->state == CPATH_STATE_OPEN);
  607. assert(circ->state == CIRCUIT_STATE_BUILDING);
  608. log_fn(LOG_DEBUG,"starting to send subsequent skin.");
  609. for(hop=circ->cpath->next;
  610. hop != circ->cpath && hop->state == CPATH_STATE_OPEN;
  611. hop=hop->next) ;
  612. if(hop == circ->cpath) { /* done building the circuit. whew. */
  613. circ->state = CIRCUIT_STATE_OPEN;
  614. log_fn(LOG_DEBUG,"circuit built!");
  615. return 0;
  616. }
  617. router = router_get_by_addr_port(hop->addr,hop->port);
  618. if(!router) {
  619. log_fn(LOG_INFO,"couldn't lookup router %d:%d",hop->addr,hop->port);
  620. return -1;
  621. }
  622. memset(&cell, 0, sizeof(cell_t));
  623. cell.command = CELL_RELAY;
  624. cell.aci = circ->n_aci;
  625. SET_CELL_RELAY_COMMAND(cell, RELAY_COMMAND_EXTEND);
  626. SET_CELL_STREAM_ID(cell, ZERO_STREAM);
  627. cell.length = RELAY_HEADER_SIZE + 6 + DH_ONIONSKIN_LEN;
  628. *(uint32_t*)(cell.payload+RELAY_HEADER_SIZE) = htonl(hop->addr);
  629. *(uint16_t*)(cell.payload+RELAY_HEADER_SIZE+4) = htons(hop->port);
  630. if(onion_skin_create(router->pkey, &(hop->handshake_state), cell.payload+RELAY_HEADER_SIZE+6) < 0) {
  631. log_fn(LOG_INFO,"onion_skin_create failed.");
  632. return -1;
  633. }
  634. log_fn(LOG_DEBUG,"Sending extend relay cell.");
  635. /* send it to hop->prev, because it will transfer it to a create cell and then send to hop */
  636. if(circuit_deliver_relay_cell(&cell, circ, CELL_DIRECTION_OUT, hop->prev) < 0) {
  637. log_fn(LOG_DEBUG,"failed to deliver extend cell. Closing.");
  638. return -1;
  639. }
  640. hop->state = CPATH_STATE_AWAITING_KEYS;
  641. }
  642. return 0;
  643. }
  644. /* take the 'extend' cell, pull out addr/port plus the onion skin. Connect
  645. * to the next hop, and pass it the onion skin in a create cell.
  646. */
  647. int circuit_extend(cell_t *cell, circuit_t *circ) {
  648. connection_t *n_conn;
  649. aci_t aci_type;
  650. struct sockaddr_in me; /* my router identity */
  651. cell_t newcell;
  652. if(circ->n_conn) {
  653. log_fn(LOG_WARNING,"n_conn already set. Bug/attack. Closing.");
  654. return -1;
  655. }
  656. circ->n_addr = ntohl(*(uint32_t*)(cell->payload+RELAY_HEADER_SIZE));
  657. circ->n_port = ntohs(*(uint16_t*)(cell->payload+RELAY_HEADER_SIZE+4));
  658. if(learn_my_address(&me) < 0)
  659. return -1;
  660. n_conn = connection_twin_get_by_addr_port(circ->n_addr,circ->n_port);
  661. if(!n_conn || n_conn->type != CONN_TYPE_OR) {
  662. /* i've disabled making connections through OPs, but it's definitely
  663. * possible here. I'm not sure if it would be a bug or a feature. -RD
  664. */
  665. /* note also that this will close circuits where the onion has the same
  666. * router twice in a row in the path. i think that's ok. -RD
  667. */
  668. struct in_addr in;
  669. in.s_addr = htonl(circ->n_addr);
  670. log_fn(LOG_DEBUG,"Next router (%s:%d) not connected. Closing.", inet_ntoa(in), circ->n_port);
  671. /* XXX later we should fail more gracefully here, like with a 'truncated' */
  672. return -1;
  673. }
  674. circ->n_addr = n_conn->addr; /* these are different if we found a twin instead */
  675. circ->n_port = n_conn->port;
  676. circ->n_conn = n_conn;
  677. log_fn(LOG_DEBUG,"n_conn is %s:%u",n_conn->address,n_conn->port);
  678. aci_type = decide_aci_type(ntohl(me.sin_addr.s_addr), ntohs(me.sin_port),
  679. circ->n_addr, circ->n_port);
  680. log_fn(LOG_DEBUG,"aci_type = %u.",aci_type);
  681. circ->n_aci = get_unique_aci_by_addr_port(circ->n_addr, circ->n_port, aci_type);
  682. if(!circ->n_aci) {
  683. log_fn(LOG_ERR,"failed to get unique aci.");
  684. return -1;
  685. }
  686. log_fn(LOG_DEBUG,"Chosen ACI %u.",circ->n_aci);
  687. memset(&newcell, 0, sizeof(cell_t));
  688. newcell.command = CELL_CREATE;
  689. newcell.aci = circ->n_aci;
  690. newcell.length = DH_ONIONSKIN_LEN;
  691. memcpy(newcell.payload, cell->payload+RELAY_HEADER_SIZE+6, DH_ONIONSKIN_LEN);
  692. if(connection_write_cell_to_buf(&newcell, circ->n_conn) < 0) {
  693. return -1;
  694. }
  695. return 0;
  696. }
  697. int circuit_finish_handshake(circuit_t *circ, char *reply) {
  698. unsigned char iv[16];
  699. unsigned char keys[32];
  700. crypt_path_t *hop;
  701. memset(iv, 0, 16);
  702. assert(circ->cpath);
  703. if(circ->cpath->state == CPATH_STATE_AWAITING_KEYS)
  704. hop = circ->cpath;
  705. else {
  706. for(hop=circ->cpath->next;
  707. hop != circ->cpath && hop->state == CPATH_STATE_OPEN;
  708. hop=hop->next) ;
  709. if(hop == circ->cpath) { /* got an extended when we're all done? */
  710. log_fn(LOG_INFO,"got extended when circ already built? Weird.");
  711. return 0;
  712. }
  713. }
  714. assert(hop->state == CPATH_STATE_AWAITING_KEYS);
  715. if(onion_skin_client_handshake(hop->handshake_state, reply, keys, 32) < 0) {
  716. log_fn(LOG_ERR,"onion_skin_client_handshake failed.");
  717. return -1;
  718. }
  719. crypto_dh_free(hop->handshake_state); /* don't need it anymore */
  720. hop->handshake_state = NULL;
  721. log_fn(LOG_DEBUG,"hop %d init cipher forward %d, backward %d.", (uint32_t)hop, *(uint32_t*)keys, *(uint32_t*)(keys+16));
  722. if (!(hop->f_crypto =
  723. crypto_create_init_cipher(CIRCUIT_CIPHER,keys,iv,1))) {
  724. log(LOG_ERR,"Cipher initialization failed.");
  725. return -1;
  726. }
  727. if (!(hop->b_crypto =
  728. crypto_create_init_cipher(CIRCUIT_CIPHER,keys+16,iv,0))) {
  729. log(LOG_ERR,"Cipher initialization failed.");
  730. return -1;
  731. }
  732. hop->state = CPATH_STATE_OPEN;
  733. log_fn(LOG_DEBUG,"Completed.");
  734. return 0;
  735. }
  736. int circuit_truncated(circuit_t *circ, crypt_path_t *layer) {
  737. crypt_path_t *victim;
  738. connection_t *stream;
  739. assert(circ);
  740. assert(layer);
  741. while(layer->next != circ->cpath) {
  742. /* we need to clear out layer->next */
  743. victim = layer->next;
  744. log_fn(LOG_DEBUG, "Killing a layer of the cpath.");
  745. for(stream = circ->p_streams; stream; stream=stream->next_stream) {
  746. if(stream->cpath_layer == victim) {
  747. log_fn(LOG_DEBUG, "Marking stream %d for close.", *(int*)stream->stream_id);
  748. stream->marked_for_close = 1;
  749. }
  750. }
  751. layer->next = victim->next;
  752. circuit_free_cpath_node(victim);
  753. }
  754. log_fn(LOG_DEBUG, "Complete.");
  755. return 0;
  756. }
  757. /*
  758. Local Variables:
  759. mode:c
  760. indent-tabs-mode:nil
  761. c-basic-offset:2
  762. End:
  763. */