circuit.c 28 KB

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