circuit.c 28 KB

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