circuit.c 31 KB

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