circuit.c 31 KB

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