circuit.c 30 KB

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