circuit.c 29 KB

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