circuit.c 30 KB

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