circuit.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. /* Copyright 2001,2002 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "or.h"
  5. extern or_options_t options; /* command-line and config-file options */
  6. /********* START VARIABLES **********/
  7. static circuit_t *global_circuitlist=NULL;
  8. char *circuit_state_to_string[] = {
  9. "receiving the onion", /* 0 */
  10. "waiting to process create", /* 1 */
  11. "connecting to firsthop", /* 2 */
  12. "open" /* 3 */
  13. };
  14. /********* END VARIABLES ************/
  15. void circuit_add(circuit_t *circ) {
  16. if(!global_circuitlist) { /* first one */
  17. global_circuitlist = circ;
  18. circ->next = NULL;
  19. } else {
  20. circ->next = global_circuitlist;
  21. global_circuitlist = circ;
  22. }
  23. }
  24. void circuit_remove(circuit_t *circ) {
  25. circuit_t *tmpcirc;
  26. assert(circ && global_circuitlist);
  27. if(global_circuitlist == circ) {
  28. global_circuitlist = global_circuitlist->next;
  29. return;
  30. }
  31. for(tmpcirc = global_circuitlist;tmpcirc->next;tmpcirc = tmpcirc->next) {
  32. if(tmpcirc->next == circ) {
  33. tmpcirc->next = circ->next;
  34. return;
  35. }
  36. }
  37. }
  38. circuit_t *circuit_new(aci_t p_aci, connection_t *p_conn) {
  39. circuit_t *circ;
  40. struct timeval now;
  41. my_gettimeofday(&now);
  42. circ = (circuit_t *)malloc(sizeof(circuit_t));
  43. if(!circ)
  44. return NULL;
  45. memset(circ,0,sizeof(circuit_t)); /* zero it out */
  46. circ->timestamp_created = now.tv_sec;
  47. circ->p_aci = p_aci;
  48. circ->p_conn = p_conn;
  49. circ->state = CIRCUIT_STATE_ONION_WAIT;
  50. /* ACIs */
  51. circ->p_aci = p_aci;
  52. /* circ->n_aci remains 0 because we haven't identified the next hop yet */
  53. circ->n_receive_circwindow = CIRCWINDOW_START;
  54. circ->p_receive_circwindow = CIRCWINDOW_START;
  55. circuit_add(circ);
  56. return circ;
  57. }
  58. void circuit_free(circuit_t *circ) {
  59. struct relay_queue_t *tmpd;
  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. if(circ->onion)
  65. free(circ->onion);
  66. circuit_free_cpath(circ->cpath);
  67. while(circ->relay_queue) {
  68. tmpd = circ->relay_queue;
  69. circ->relay_queue = tmpd->next;
  70. free(tmpd->cell);
  71. free(tmpd);
  72. }
  73. free(circ);
  74. }
  75. void circuit_free_cpath(crypt_path_t *cpath) {
  76. crypt_path_t *victim, *head=cpath;
  77. if(!cpath)
  78. return;
  79. /* it's a doubly linked list, so we have to notice when we've
  80. * gone through it once. */
  81. while(cpath->next && cpath->next != head) {
  82. victim = cpath;
  83. cpath = victim->next;
  84. circuit_free_cpath_node(victim);
  85. }
  86. circuit_free_cpath_node(cpath);
  87. }
  88. void circuit_free_cpath_node(crypt_path_t *victim) {
  89. if(victim->f_crypto)
  90. crypto_free_cipher_env(victim->f_crypto);
  91. if(victim->b_crypto)
  92. crypto_free_cipher_env(victim->b_crypto);
  93. free(victim);
  94. }
  95. /* return 0 if can't get a unique aci. */
  96. aci_t get_unique_aci_by_addr_port(uint32_t addr, uint16_t port, int aci_type) {
  97. aci_t test_aci;
  98. connection_t *conn;
  99. try_again:
  100. log(LOG_DEBUG,"get_unique_aci_by_addr_port() trying to get a unique aci");
  101. if (CRYPTO_PSEUDO_RAND_INT(test_aci))
  102. return -1;
  103. if(aci_type == ACI_TYPE_LOWER && test_aci >= (1<<15))
  104. test_aci -= (1<<15);
  105. if(aci_type == ACI_TYPE_HIGHER && test_aci < (1<<15))
  106. test_aci += (1<<15);
  107. /* if aci_type == ACI_BOTH, don't filter any of it */
  108. if(test_aci == 0)
  109. goto try_again;
  110. conn = connection_exact_get_by_addr_port(addr,port);
  111. if(!conn) /* there can't be a conflict -- no connection of that sort yet */
  112. return test_aci;
  113. if(circuit_get_by_aci_conn(test_aci, conn))
  114. goto try_again;
  115. return test_aci;
  116. }
  117. int circuit_init(circuit_t *circ, int aci_type, onion_layer_t *layer) {
  118. unsigned char iv[16];
  119. unsigned char digest1[20];
  120. unsigned char digest2[20];
  121. struct timeval start, end;
  122. long time_passed;
  123. assert(circ && circ->onion);
  124. log(LOG_DEBUG,"circuit_init(): starting");
  125. circ->n_port = layer->port;
  126. log(LOG_DEBUG,"circuit_init(): Set port to %u.",circ->n_port);
  127. circ->n_addr = layer->addr;
  128. circ->state = CIRCUIT_STATE_OPEN;
  129. log(LOG_DEBUG,"circuit_init(): aci_type = %u.",aci_type);
  130. my_gettimeofday(&start);
  131. circ->n_aci = get_unique_aci_by_addr_port(circ->n_addr, circ->n_port, aci_type);
  132. if(!circ->n_aci) {
  133. log(LOG_ERR,"circuit_init(): failed to get unique aci.");
  134. return -1;
  135. }
  136. my_gettimeofday(&end);
  137. time_passed = tv_udiff(&start, &end);
  138. if (time_passed > 1000) {/* more than 1ms */
  139. log(LOG_NOTICE,"circuit_init(): get_unique_aci just took %d us!",time_passed);
  140. }
  141. log(LOG_DEBUG,"circuit_init(): Chosen ACI %u.",circ->n_aci);
  142. /* keys */
  143. memset(iv, 0, 16);
  144. crypto_SHA_digest(layer->keyseed,16,digest1);
  145. crypto_SHA_digest(digest1,20,digest2);
  146. crypto_SHA_digest(digest2,20,digest1);
  147. log(LOG_DEBUG,"circuit_init(): Computed keys.");
  148. if (!(circ->p_crypto =
  149. crypto_create_init_cipher(DEFAULT_CIPHER,digest2,iv,1))) {
  150. log(LOG_ERR,"Cipher initialization failed (ACI %u).",circ->n_aci);
  151. return -1;
  152. }
  153. if (!(circ->n_crypto =
  154. crypto_create_init_cipher(DEFAULT_CIPHER,digest1,iv,0))) {
  155. log(LOG_ERR,"Cipher initialization failed (ACI %u).",circ->n_aci);
  156. return -1;
  157. }
  158. log(LOG_DEBUG,"circuit_init(): Cipher initialization complete.");
  159. circ->expire = layer->expire;
  160. return 0;
  161. }
  162. circuit_t *circuit_enumerate_by_naddr_nport(circuit_t *circ, uint32_t naddr, uint16_t nport) {
  163. if(!circ) /* use circ if it's defined, else start from the beginning */
  164. circ = global_circuitlist;
  165. else
  166. circ = circ->next;
  167. for( ; circ; circ = circ->next) {
  168. if(circ->n_addr == naddr && circ->n_port == nport)
  169. return circ;
  170. }
  171. return NULL;
  172. }
  173. circuit_t *circuit_get_by_aci_conn(aci_t aci, connection_t *conn) {
  174. circuit_t *circ;
  175. connection_t *tmpconn;
  176. for(circ=global_circuitlist;circ;circ = circ->next) {
  177. if(circ->p_aci == aci) {
  178. for(tmpconn = circ->p_conn; tmpconn; tmpconn = tmpconn->next_stream) {
  179. if(tmpconn == conn)
  180. return circ;
  181. }
  182. }
  183. if(circ->n_aci == aci) {
  184. for(tmpconn = circ->n_conn; tmpconn; tmpconn = tmpconn->next_stream) {
  185. if(tmpconn == conn)
  186. return circ;
  187. }
  188. }
  189. }
  190. return NULL;
  191. }
  192. circuit_t *circuit_get_by_conn(connection_t *conn) {
  193. circuit_t *circ;
  194. connection_t *tmpconn;
  195. for(circ=global_circuitlist;circ;circ = circ->next) {
  196. for(tmpconn = circ->p_conn; tmpconn; tmpconn=tmpconn->next_stream)
  197. if(tmpconn == conn)
  198. return circ;
  199. for(tmpconn = circ->n_conn; tmpconn; tmpconn=tmpconn->next_stream)
  200. if(tmpconn == conn)
  201. return circ;
  202. }
  203. return NULL;
  204. }
  205. circuit_t *circuit_get_newest_ap(void) {
  206. circuit_t *circ, *bestcirc=NULL;
  207. for(circ=global_circuitlist;circ;circ = circ->next) {
  208. if(!circ->p_conn || circ->p_conn->type == CONN_TYPE_AP) {
  209. if(circ->state == CIRCUIT_STATE_OPEN && (!bestcirc ||
  210. bestcirc->timestamp_created < circ->timestamp_created)) {
  211. log(LOG_DEBUG,"circuit_get_newest_ap(): Choosing n_aci %d.", circ->n_aci);
  212. assert(circ->n_aci);
  213. bestcirc = circ;
  214. }
  215. }
  216. }
  217. return bestcirc;
  218. }
  219. int circuit_deliver_relay_cell_from_edge(cell_t *cell, circuit_t *circ,
  220. char edge_type, crypt_path_t *layer_hint) {
  221. int cell_direction;
  222. static int numsent_ap=0, numsent_exit=0;
  223. log(LOG_DEBUG,"circuit_deliver_relay_cell_from_edge(): called, edge_type %d.", edge_type);
  224. if(edge_type == EDGE_AP) { /* i'm the AP */
  225. cell_direction = CELL_DIRECTION_OUT;
  226. numsent_ap++;
  227. log(LOG_DEBUG,"circuit_deliver_relay_cell_from_edge(): now sent %d relay cells from ap", numsent_ap);
  228. if(circ->p_receive_circwindow <= 0) {
  229. log(LOG_DEBUG,"circuit_deliver_relay_cell_from_edge(): pwindow 0, queueing for later.");
  230. circ->relay_queue = relay_queue_add(circ->relay_queue, cell, layer_hint);
  231. return 0;
  232. }
  233. circ->p_receive_circwindow--;
  234. // log(LOG_INFO,"circuit_deliver_relay_cell_from_edge(): p_receive_circwindow now %d.",circ->p_receive_circwindow);
  235. } else { /* i'm the exit */
  236. cell_direction = CELL_DIRECTION_IN;
  237. numsent_exit++;
  238. log(LOG_DEBUG,"circuit_deliver_relay_cell_from_edge(): now sent %d relay cells from exit", numsent_exit);
  239. if(circ->n_receive_circwindow <= 0) {
  240. log(LOG_DEBUG,"circuit_deliver_relay_cell_from_edge(): nwindow 0, queueing for later.");
  241. circ->relay_queue = relay_queue_add(circ->relay_queue, cell, layer_hint);
  242. return 0;
  243. }
  244. circ->n_receive_circwindow--;
  245. }
  246. if(circuit_deliver_relay_cell(cell, circ, cell_direction, layer_hint) < 0) {
  247. return -1;
  248. }
  249. circuit_consider_stop_edge_reading(circ, edge_type); /* has window reached 0? */
  250. return 0;
  251. }
  252. int circuit_deliver_relay_cell(cell_t *cell, circuit_t *circ,
  253. int cell_direction, crypt_path_t *layer_hint) {
  254. connection_t *conn=NULL;
  255. char recognized=0;
  256. char buf[256];
  257. assert(cell && circ);
  258. assert(cell_direction == CELL_DIRECTION_OUT || cell_direction == CELL_DIRECTION_IN);
  259. buf[0] = cell->length;
  260. memcpy(buf+1, cell->payload, CELL_PAYLOAD_SIZE);
  261. log(LOG_DEBUG,"circuit_deliver_relay_cell(): streamid %d before crypt.", *(int*)(cell->payload+1));
  262. if(relay_crypt(circ, buf, 1+CELL_PAYLOAD_SIZE, cell_direction, layer_hint, &recognized, &conn) < 0) {
  263. log(LOG_DEBUG,"circuit_deliver_relay_cell(): relay crypt failed. Dropping connection.");
  264. return -1;
  265. }
  266. cell->length = buf[0];
  267. memcpy(cell->payload, buf+1, CELL_PAYLOAD_SIZE);
  268. if(recognized) {
  269. if(cell_direction == CELL_DIRECTION_OUT) {
  270. log(LOG_DEBUG,"circuit_deliver_relay_cell(): Sending to exit.");
  271. return connection_edge_process_relay_cell(cell, circ, conn, EDGE_EXIT);
  272. }
  273. if(cell_direction == CELL_DIRECTION_IN) {
  274. log(LOG_DEBUG,"circuit_deliver_relay_cell(): Sending to AP.");
  275. return connection_edge_process_relay_cell(cell, circ, conn, EDGE_AP);
  276. }
  277. }
  278. /* not recognized. pass it on. */
  279. if(cell_direction == CELL_DIRECTION_OUT)
  280. conn = circ->n_conn;
  281. else
  282. conn = circ->p_conn;
  283. if(!conn || !connection_speaks_cells(conn)) {
  284. log(LOG_INFO,"circuit_deliver_relay_cell(): Didn't recognize cell (%d), but circ stops here! Dropping.", *(int *)(cell->payload+1));
  285. return 0;
  286. }
  287. return connection_write_cell_to_buf(cell, conn);
  288. }
  289. int relay_crypt(circuit_t *circ, char *in, int inlen, char cell_direction,
  290. crypt_path_t *layer_hint, char *recognized, connection_t **conn) {
  291. crypt_path_t *thishop;
  292. char out[256];
  293. assert(circ && in && recognized && conn);
  294. assert(inlen < 256);
  295. if(cell_direction == CELL_DIRECTION_IN) {
  296. if(circ->cpath) { /* we're at the beginning of the circuit. We'll want to do layered crypts. */
  297. thishop = circ->cpath;
  298. do { /* Remember: cpath is in forward order, that is, first hop first. */
  299. assert(thishop);
  300. /* decrypt */
  301. if(crypto_cipher_decrypt(thishop->b_crypto, in, inlen, out)) {
  302. log(LOG_ERR,"Error performing decryption:%s",crypto_perror());
  303. return -1;
  304. }
  305. memcpy(in,out,inlen);
  306. if( (*recognized = relay_check_recognized(circ, cell_direction, in+2, conn)))
  307. return 0;
  308. thishop = thishop->next;
  309. } while(thishop != circ->cpath);
  310. log(LOG_INFO,"relay_crypt(): in-cell at OP not recognized. Killing circuit.");
  311. return -1;
  312. } else { /* we're in the middle. Just one crypt. */
  313. if(crypto_cipher_encrypt(circ->p_crypto, in, inlen, out)) {
  314. log(LOG_ERR,"circuit_encrypt(): Encryption failed for ACI : %u (%s).",
  315. circ->p_aci, crypto_perror());
  316. return -1;
  317. }
  318. memcpy(in,out,inlen);
  319. /* don't check for recognized. only the OP can recognize a stream on the way back. */
  320. }
  321. } else if(cell_direction == CELL_DIRECTION_OUT) {
  322. if(circ->cpath) { /* we're at the beginning of the circuit. We'll want to do layered crypts. */
  323. thishop = layer_hint; /* we already know which layer, from when we package_raw_inbuf'ed */
  324. /* moving from last to first hop */
  325. do {
  326. assert(thishop);
  327. /* encrypt */
  328. if(crypto_cipher_encrypt(thishop->f_crypto, in, inlen, out)) {
  329. log(LOG_ERR,"Error performing encryption:%s",crypto_perror());
  330. return -1;
  331. }
  332. memcpy(in,out,inlen);
  333. thishop = thishop->prev;
  334. } while(thishop != circ->cpath->prev);
  335. } else { /* we're in the middle. Just one crypt. */
  336. if(crypto_cipher_decrypt(circ->n_crypto,in, inlen, out)) {
  337. log(LOG_ERR,"circuit_crypt(): Decryption failed for ACI : %u (%s).",
  338. circ->n_aci, crypto_perror());
  339. return -1;
  340. }
  341. memcpy(in,out,inlen);
  342. if( (*recognized = relay_check_recognized(circ, cell_direction, in+2, conn)))
  343. return 0;
  344. }
  345. } else {
  346. log(LOG_ERR,"circuit_crypt(): unknown cell direction %d.", cell_direction);
  347. assert(0);
  348. }
  349. return 0;
  350. }
  351. int relay_check_recognized(circuit_t *circ, int cell_direction, char *stream, connection_t **conn) {
  352. /* FIXME can optimize by passing thishop in */
  353. connection_t *tmpconn;
  354. log(LOG_DEBUG,"relay_check_recognized(): entering");
  355. if(!memcmp(stream,ZERO_STREAM,STREAM_ID_SIZE))
  356. return 1; /* the zero stream is always recognized */
  357. if(cell_direction == CELL_DIRECTION_OUT)
  358. tmpconn = circ->n_conn;
  359. else
  360. tmpconn = circ->p_conn;
  361. log(LOG_DEBUG,"relay_check_recognized(): not the zero stream.");
  362. if(!tmpconn)
  363. return 0; /* no conns? don't recognize it */
  364. while(tmpconn && tmpconn->type == CONN_TYPE_OR) {
  365. log(LOG_DEBUG,"relay_check_recognized(): skipping over an OR conn");
  366. tmpconn = tmpconn->next_stream;
  367. }
  368. for( ; tmpconn; tmpconn=tmpconn->next_stream) {
  369. if(!memcmp(stream,tmpconn->stream_id, STREAM_ID_SIZE)) {
  370. log(LOG_DEBUG,"relay_check_recognized(): recognized stream %d.", *(int*)stream);
  371. *conn = tmpconn;
  372. return 1;
  373. }
  374. log(LOG_DEBUG,"relay_check_recognized(): considered stream %d, not it.",*(int*)tmpconn->stream_id);
  375. }
  376. log(LOG_DEBUG,"relay_check_recognized(): Didn't recognize. Giving up.");
  377. return 0;
  378. }
  379. void circuit_resume_edge_reading(circuit_t *circ, int edge_type) {
  380. connection_t *conn;
  381. struct relay_queue_t *tmpd;
  382. assert(edge_type == EDGE_EXIT || edge_type == EDGE_AP);
  383. /* first, send the queue waiting at circ onto the circuit */
  384. while(circ->relay_queue) {
  385. assert(circ->relay_queue->cell);
  386. if(edge_type == EDGE_EXIT) {
  387. circ->n_receive_circwindow--;
  388. assert(circ->n_receive_circwindow >= 0);
  389. if(circuit_deliver_relay_cell(circ->relay_queue->cell, circ, CELL_DIRECTION_IN, circ->relay_queue->layer_hint) < 0) {
  390. circuit_close(circ);
  391. return;
  392. }
  393. } else { /* ap */
  394. circ->p_receive_circwindow--;
  395. assert(circ->p_receive_circwindow >= 0);
  396. if(circuit_deliver_relay_cell(circ->relay_queue->cell, circ, CELL_DIRECTION_OUT, circ->relay_queue->layer_hint) < 0) {
  397. circuit_close(circ);
  398. return;
  399. }
  400. }
  401. tmpd = circ->relay_queue;
  402. circ->relay_queue = tmpd->next;
  403. free(tmpd->cell);
  404. free(tmpd);
  405. if(circuit_consider_stop_edge_reading(circ, edge_type))
  406. return;
  407. }
  408. if(edge_type == EDGE_EXIT)
  409. conn = circ->n_conn;
  410. else
  411. conn = circ->p_conn;
  412. for( ; conn; conn=conn->next_stream) {
  413. if((edge_type == EDGE_EXIT && conn->n_receive_streamwindow > 0) ||
  414. (edge_type == EDGE_AP && conn->p_receive_streamwindow > 0)) {
  415. connection_start_reading(conn);
  416. connection_package_raw_inbuf(conn); /* handle whatever might still be on the inbuf */
  417. }
  418. }
  419. circuit_consider_stop_edge_reading(circ, edge_type);
  420. }
  421. /* returns 1 if the window is empty, else 0. If it's empty, tell edge conns to stop reading. */
  422. int circuit_consider_stop_edge_reading(circuit_t *circ, int edge_type) {
  423. connection_t *conn = NULL;
  424. assert(edge_type == EDGE_EXIT || edge_type == EDGE_AP);
  425. if(edge_type == EDGE_EXIT && circ->n_receive_circwindow <= 0)
  426. conn = circ->n_conn;
  427. else if(edge_type == EDGE_AP && circ->p_receive_circwindow <= 0)
  428. conn = circ->p_conn;
  429. else
  430. return 0;
  431. for( ; conn; conn=conn->next_stream)
  432. connection_stop_reading(conn);
  433. return 1;
  434. }
  435. int circuit_consider_sending_sendme(circuit_t *circ, int edge_type) {
  436. cell_t sendme;
  437. assert(circ);
  438. memset(&sendme, 0, sizeof(cell_t));
  439. sendme.command = CELL_SENDME;
  440. sendme.length = CIRCWINDOW_INCREMENT;
  441. if(edge_type == EDGE_AP) { /* i'm the AP */
  442. while(circ->n_receive_circwindow < CIRCWINDOW_START-CIRCWINDOW_INCREMENT) {
  443. log(LOG_DEBUG,"circuit_consider_sending_sendme(): n_receive_circwindow %d, Queueing sendme forward.", circ->n_receive_circwindow);
  444. circ->n_receive_circwindow += CIRCWINDOW_INCREMENT;
  445. sendme.aci = circ->n_aci;
  446. if(connection_write_cell_to_buf(&sendme, circ->n_conn) < 0) {
  447. return -1;
  448. }
  449. }
  450. } else if(edge_type == EDGE_EXIT) { /* i'm the exit */
  451. while(circ->p_receive_circwindow < CIRCWINDOW_START-CIRCWINDOW_INCREMENT) {
  452. log(LOG_DEBUG,"circuit_consider_sending_sendme(): p_receive_circwindow %d, Queueing sendme back.", circ->p_receive_circwindow);
  453. circ->p_receive_circwindow += CIRCWINDOW_INCREMENT;
  454. sendme.aci = circ->p_aci;
  455. if(connection_write_cell_to_buf(&sendme, circ->p_conn) < 0) {
  456. return -1;
  457. }
  458. }
  459. }
  460. return 0;
  461. }
  462. void circuit_close(circuit_t *circ) {
  463. connection_t *conn;
  464. circuit_t *youngest=NULL;
  465. assert(circ);
  466. if(options.APPort) {
  467. youngest = circuit_get_newest_ap();
  468. log(LOG_DEBUG,"circuit_close(): youngest %d, circ %d.",youngest,circ);
  469. }
  470. circuit_remove(circ);
  471. for(conn=circ->n_conn; conn; conn=conn->next_stream) {
  472. connection_send_destroy(circ->n_aci, circ->n_conn);
  473. }
  474. for(conn=circ->p_conn; conn; conn=conn->next_stream) {
  475. connection_send_destroy(circ->p_aci, circ->p_conn);
  476. }
  477. if(options.APPort && youngest == circ) { /* check this after we've sent the destroys, to reduce races */
  478. /* our current circuit just died. Launch another one pronto. */
  479. log(LOG_INFO,"circuit_close(): Youngest circuit dying. Launching a replacement.");
  480. circuit_launch_new(1);
  481. }
  482. circuit_free(circ);
  483. }
  484. void circuit_about_to_close_connection(connection_t *conn) {
  485. /* send destroys for all circuits using conn */
  486. /* currently, we assume it's too late to flush conn's buf here.
  487. * down the road, maybe we'll consider that eof doesn't mean can't-write
  488. */
  489. circuit_t *circ;
  490. connection_t *prevconn;
  491. if(!connection_speaks_cells(conn)) {
  492. /* it's an edge conn. need to remove it from the linked list of
  493. * conn's for this circuit. Send an 'end' relay command.
  494. * But don't kill the circuit.
  495. */
  496. circ = circuit_get_by_conn(conn);
  497. if(!circ)
  498. return;
  499. if(conn == circ->p_conn) {
  500. circ->p_conn = conn->next_stream;
  501. goto send_end;
  502. }
  503. if(conn == circ->n_conn) {
  504. circ->n_conn = conn->next_stream;
  505. goto send_end;
  506. }
  507. for(prevconn = circ->p_conn; prevconn->next_stream && prevconn->next_stream != conn; prevconn = prevconn->next_stream) ;
  508. if(prevconn->next_stream) {
  509. prevconn->next_stream = conn->next_stream;
  510. goto send_end;
  511. }
  512. for(prevconn = circ->n_conn; prevconn->next_stream && prevconn->next_stream != conn; prevconn = prevconn->next_stream) ;
  513. if(prevconn->next_stream) {
  514. prevconn->next_stream = conn->next_stream;
  515. goto send_end;
  516. }
  517. log(LOG_ERR,"circuit_about_to_close_connection(): edge conn not in circuit's list?");
  518. assert(0); /* should never get here */
  519. send_end:
  520. if(connection_edge_send_command(conn, circ, RELAY_COMMAND_END) < 0) {
  521. log(LOG_DEBUG,"circuit_about_to_close_connection(): sending end failed. Closing.");
  522. circuit_close(circ);
  523. }
  524. return;
  525. }
  526. /* this connection speaks cells. We must close all the circuits on it. */
  527. while((circ = circuit_get_by_conn(conn))) {
  528. if(circ->n_conn == conn) /* it's closing in front of us */
  529. circ->n_conn = NULL;
  530. if(circ->p_conn == conn) /* it's closing behind us */
  531. circ->p_conn = NULL;
  532. circuit_close(circ);
  533. }
  534. }
  535. /* FIXME this now leaves some out */
  536. void circuit_dump_by_conn(connection_t *conn) {
  537. circuit_t *circ;
  538. connection_t *tmpconn;
  539. for(circ=global_circuitlist;circ;circ = circ->next) {
  540. for(tmpconn=circ->p_conn; tmpconn; tmpconn=tmpconn->next_stream) {
  541. if(tmpconn == conn) {
  542. printf("Conn %d has App-ward circuit: aci %d (other side %d), state %d (%s)\n",
  543. conn->poll_index, circ->p_aci, circ->n_aci, circ->state, circuit_state_to_string[circ->state]);
  544. }
  545. }
  546. for(tmpconn=circ->n_conn; tmpconn; tmpconn=tmpconn->next_stream) {
  547. if(tmpconn == conn) {
  548. printf("Conn %d has Exit-ward circuit: aci %d (other side %d), state %d (%s)\n",
  549. conn->poll_index, circ->n_aci, circ->p_aci, circ->state, circuit_state_to_string[circ->state]);
  550. }
  551. }
  552. }
  553. }
  554. void circuit_expire_unused_circuits(void) {
  555. circuit_t *circ, *tmpcirc;
  556. circuit_t *youngest;
  557. youngest = circuit_get_newest_ap();
  558. circ = global_circuitlist;
  559. while(circ) {
  560. tmpcirc = circ;
  561. circ = circ->next;
  562. if(tmpcirc != youngest && !tmpcirc->p_conn) {
  563. log(LOG_DEBUG,"circuit_expire_unused_circuits(): Closing n_aci %d",tmpcirc->n_aci);
  564. circuit_close(tmpcirc);
  565. }
  566. }
  567. }
  568. /* failure_status code: negative means reset failures to 0. Other values mean
  569. * add that value to the current number of failures, then if we don't have too
  570. * many failures on record, try to make a new circuit.
  571. */
  572. void circuit_launch_new(int failure_status) {
  573. static int failures=0;
  574. if(!options.APPort) /* we're not an application proxy. no need for circuits. */
  575. return;
  576. if(failure_status == -1) { /* I was called because a circuit succeeded */
  577. failures = 0;
  578. return;
  579. }
  580. failures += failure_status;
  581. retry_circuit:
  582. if(failures > 5) {
  583. log(LOG_INFO,"circuit_launch_new(): Giving up, %d failures.", failures);
  584. return;
  585. }
  586. if(circuit_create_onion() < 0) {
  587. failures++;
  588. goto retry_circuit;
  589. }
  590. failures = 0;
  591. return;
  592. }
  593. int circuit_create_onion(void) {
  594. int routelen; /* length of the route */
  595. unsigned int *route; /* hops in the route as an array of indexes into rarray */
  596. unsigned char *onion; /* holds the onion */
  597. int onionlen; /* onion length in host order */
  598. crypt_path_t *cpath; /* defines the crypt operations that need to be performed on incoming/outgoing data */
  599. /* choose a route */
  600. route = (unsigned int *)router_new_route(&routelen);
  601. if (!route) {
  602. log(LOG_ERR,"circuit_create_onion(): Error choosing a route through the OR network.");
  603. return -1;
  604. }
  605. log(LOG_DEBUG,"circuit_create_onion(): Chosen a route of length %u : ",routelen);
  606. /* create an onion and calculate crypto keys */
  607. onion = router_create_onion(route,routelen,&onionlen, &cpath);
  608. if (!onion) {
  609. log(LOG_ERR,"circuit_create_onion(): Error creating an onion.");
  610. free(route);
  611. return -1;
  612. }
  613. log(LOG_DEBUG,"circuit_create_onion(): Created an onion of size %u bytes.",onionlen);
  614. // log(LOG_DEBUG,"circuit_create_onion(): Crypt path :");
  615. return circuit_establish_circuit(route, routelen, onion, onionlen, cpath);
  616. }
  617. int circuit_establish_circuit(unsigned int *route, int routelen, char *onion,
  618. int onionlen, crypt_path_t *cpath) {
  619. routerinfo_t *firsthop;
  620. connection_t *n_conn;
  621. circuit_t *circ;
  622. /* now see if we're already connected to the first OR in 'route' */
  623. firsthop = router_get_first_in_route(route, routelen);
  624. assert(firsthop); /* should always be defined */
  625. free(route); /* we don't need it anymore */
  626. circ = circuit_new(0, NULL); /* sets circ->p_aci and circ->p_conn */
  627. circ->state = CIRCUIT_STATE_OR_WAIT;
  628. circ->onion = onion;
  629. circ->onionlen = onionlen;
  630. circ->cpath = cpath;
  631. log(LOG_DEBUG,"circuit_establish_circuit(): Looking for firsthop '%s:%u'",
  632. firsthop->address,firsthop->or_port);
  633. n_conn = connection_twin_get_by_addr_port(firsthop->addr,firsthop->or_port);
  634. if(!n_conn || n_conn->state != OR_CONN_STATE_OPEN) { /* not currently connected */
  635. circ->n_addr = firsthop->addr;
  636. circ->n_port = firsthop->or_port;
  637. if(options.ORPort) { /* we would be connected if he were up. but he's not. */
  638. log(LOG_DEBUG,"circuit_establish_circuit(): Route's firsthop isn't connected.");
  639. circuit_close(circ);
  640. return -1;
  641. }
  642. if(!n_conn) { /* launch the connection */
  643. n_conn = connection_or_connect_as_op(firsthop);
  644. if(!n_conn) { /* connect failed, forget the whole thing */
  645. log(LOG_DEBUG,"circuit_establish_circuit(): connect to firsthop failed. Closing.");
  646. circuit_close(circ);
  647. return -1;
  648. }
  649. }
  650. return 0; /* return success. The onion/circuit/etc will be taken care of automatically
  651. * (may already have been) whenever n_conn reaches OR_CONN_STATE_OPEN.
  652. */
  653. } else { /* it (or a twin) is already open. use it. */
  654. circ->n_addr = n_conn->addr;
  655. circ->n_port = n_conn->port;
  656. return circuit_send_onion(n_conn, circ);
  657. }
  658. }
  659. /* find circuits that are waiting on me, if any, and get them to send the onion */
  660. void circuit_n_conn_open(connection_t *or_conn) {
  661. circuit_t *circ;
  662. log(LOG_DEBUG,"circuit_n_conn_open(): Starting.");
  663. circ = circuit_enumerate_by_naddr_nport(NULL, or_conn->addr, or_conn->port);
  664. for(;;) {
  665. if(!circ)
  666. return;
  667. log(LOG_DEBUG,"circuit_n_conn_open(): Found circ, sending onion.");
  668. if(circuit_send_onion(or_conn, circ) < 0) {
  669. log(LOG_DEBUG,"circuit_n_conn_open(): circuit marked for closing.");
  670. circuit_close(circ);
  671. return; /* FIXME will want to try the other circuits too? */
  672. }
  673. circ = circuit_enumerate_by_naddr_nport(circ, or_conn->addr, or_conn->port);
  674. }
  675. }
  676. int circuit_send_onion(connection_t *n_conn, circuit_t *circ) {
  677. cell_t cell;
  678. int tmpbuflen, dataleft;
  679. char *tmpbuf;
  680. circ->n_aci = get_unique_aci_by_addr_port(circ->n_addr, circ->n_port, ACI_TYPE_BOTH);
  681. circ->n_conn = n_conn;
  682. log(LOG_DEBUG,"circuit_send_onion(): n_conn is %s:%u",n_conn->address,n_conn->port);
  683. /* deliver the onion as one or more create cells */
  684. cell.command = CELL_CREATE;
  685. cell.aci = circ->n_aci;
  686. tmpbuflen = circ->onionlen+4;
  687. tmpbuf = malloc(tmpbuflen);
  688. if(!tmpbuf)
  689. return -1;
  690. *(uint32_t*)tmpbuf = htonl(circ->onionlen);
  691. memcpy(tmpbuf+4, circ->onion, circ->onionlen);
  692. dataleft = tmpbuflen;
  693. while(dataleft) {
  694. cell.command = CELL_CREATE;
  695. cell.aci = circ->n_aci;
  696. log(LOG_DEBUG,"circuit_send_onion(): Sending a create cell for the onion...");
  697. if(dataleft >= CELL_PAYLOAD_SIZE) {
  698. cell.length = CELL_PAYLOAD_SIZE;
  699. memcpy(cell.payload, tmpbuf + tmpbuflen - dataleft, CELL_PAYLOAD_SIZE);
  700. connection_write_cell_to_buf(&cell, n_conn);
  701. dataleft -= CELL_PAYLOAD_SIZE;
  702. } else { /* last cell */
  703. cell.length = dataleft;
  704. memcpy(cell.payload, tmpbuf + tmpbuflen - dataleft, dataleft);
  705. /* fill extra space with 0 bytes */
  706. memset(cell.payload + dataleft, 0, CELL_PAYLOAD_SIZE - dataleft);
  707. connection_write_cell_to_buf(&cell, n_conn);
  708. dataleft = 0;
  709. }
  710. }
  711. free(tmpbuf);
  712. circ->state = CIRCUIT_STATE_OPEN;
  713. /* FIXME should set circ->expire to something here */
  714. return 0;
  715. }
  716. /*
  717. Local Variables:
  718. mode:c
  719. indent-tabs-mode:nil
  720. c-basic-offset:2
  721. End:
  722. */