circuit.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  1. /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "or.h"
  5. extern or_options_t options; /* command-line and config-file options */
  6. static void circuit_free_cpath_node(crypt_path_t *victim);
  7. static circ_id_t get_unique_circ_id_by_conn(connection_t *conn, int circ_id_type);
  8. unsigned long stats_n_relay_cells_relayed = 0;
  9. unsigned long stats_n_relay_cells_delivered = 0;
  10. /********* START VARIABLES **********/
  11. static circuit_t *global_circuitlist=NULL;
  12. char *circuit_state_to_string[] = {
  13. "doing handshakes", /* 0 */
  14. "processing the onion", /* 1 */
  15. "connecting to firsthop", /* 2 */
  16. "open" /* 3 */
  17. };
  18. /********* END VARIABLES ************/
  19. void circuit_add(circuit_t *circ) {
  20. if(!global_circuitlist) { /* first one */
  21. global_circuitlist = circ;
  22. circ->next = NULL;
  23. } else {
  24. circ->next = global_circuitlist;
  25. global_circuitlist = circ;
  26. }
  27. }
  28. void circuit_remove(circuit_t *circ) {
  29. circuit_t *tmpcirc;
  30. assert(circ && global_circuitlist);
  31. if(global_circuitlist == circ) {
  32. global_circuitlist = global_circuitlist->next;
  33. return;
  34. }
  35. for(tmpcirc = global_circuitlist;tmpcirc->next;tmpcirc = tmpcirc->next) {
  36. if(tmpcirc->next == circ) {
  37. tmpcirc->next = circ->next;
  38. return;
  39. }
  40. }
  41. }
  42. circuit_t *circuit_new(circ_id_t p_circ_id, connection_t *p_conn) {
  43. circuit_t *circ;
  44. circ = tor_malloc_zero(sizeof(circuit_t));
  45. circ->timestamp_created = time(NULL);
  46. circ->p_circ_id = p_circ_id;
  47. circ->p_conn = p_conn;
  48. circ->state = CIRCUIT_STATE_ONIONSKIN_PENDING;
  49. /* CircIDs */
  50. circ->p_circ_id = p_circ_id;
  51. /* circ->n_circ_id remains 0 because we haven't identified the next hop yet */
  52. circ->package_window = CIRCWINDOW_START;
  53. circ->deliver_window = CIRCWINDOW_START;
  54. circuit_add(circ);
  55. return circ;
  56. }
  57. void circuit_free(circuit_t *circ) {
  58. assert(circ);
  59. if (circ->n_crypto)
  60. crypto_free_cipher_env(circ->n_crypto);
  61. if (circ->p_crypto)
  62. crypto_free_cipher_env(circ->p_crypto);
  63. if(circ->build_state)
  64. tor_free(circ->build_state->chosen_exit);
  65. tor_free(circ->build_state);
  66. circuit_free_cpath(circ->cpath);
  67. free(circ);
  68. }
  69. void circuit_free_cpath(crypt_path_t *cpath) {
  70. crypt_path_t *victim, *head=cpath;
  71. if(!cpath)
  72. return;
  73. /* it's a doubly linked list, so we have to notice when we've
  74. * gone through it once. */
  75. while(cpath->next && cpath->next != head) {
  76. victim = cpath;
  77. cpath = victim->next;
  78. circuit_free_cpath_node(victim);
  79. }
  80. circuit_free_cpath_node(cpath);
  81. }
  82. static void circuit_free_cpath_node(crypt_path_t *victim) {
  83. if(victim->f_crypto)
  84. crypto_free_cipher_env(victim->f_crypto);
  85. if(victim->b_crypto)
  86. crypto_free_cipher_env(victim->b_crypto);
  87. if(victim->handshake_state)
  88. crypto_dh_free(victim->handshake_state);
  89. free(victim);
  90. }
  91. /* return 0 if can't get a unique circ_id. */
  92. static circ_id_t get_unique_circ_id_by_conn(connection_t *conn, int circ_id_type) {
  93. circ_id_t test_circ_id;
  94. uint16_t high_bit;
  95. assert(conn && conn->type == CONN_TYPE_OR);
  96. high_bit = (circ_id_type == CIRC_ID_TYPE_HIGHER) ? 1<<15 : 0;
  97. do {
  98. /* Sequentially iterate over test_circ_id=1...1<<15-1 until we find an
  99. * circID such that (high_bit|test_circ_id) is not already used. */
  100. /* XXX Will loop forever if all circ_id's in our range are used.
  101. * This matters because it's an external DoS vulnerability. */
  102. test_circ_id = conn->next_circ_id++;
  103. if (test_circ_id == 0 || test_circ_id >= 1<<15) {
  104. test_circ_id = 1;
  105. conn->next_circ_id = 2;
  106. }
  107. test_circ_id |= high_bit;
  108. } while(circuit_get_by_circ_id_conn(test_circ_id, conn));
  109. return test_circ_id;
  110. }
  111. circuit_t *circuit_get_by_circ_id_conn(circ_id_t circ_id, connection_t *conn) {
  112. circuit_t *circ;
  113. connection_t *tmpconn;
  114. for(circ=global_circuitlist;circ;circ = circ->next) {
  115. if(circ->p_circ_id == circ_id) {
  116. if(circ->p_conn == conn)
  117. return circ;
  118. for(tmpconn = circ->p_streams; tmpconn; tmpconn = tmpconn->next_stream) {
  119. if(tmpconn == conn)
  120. return circ;
  121. }
  122. }
  123. if(circ->n_circ_id == circ_id) {
  124. if(circ->n_conn == conn)
  125. return circ;
  126. for(tmpconn = circ->n_streams; tmpconn; tmpconn = tmpconn->next_stream) {
  127. if(tmpconn == conn)
  128. return circ;
  129. }
  130. }
  131. }
  132. return NULL;
  133. }
  134. circuit_t *circuit_get_by_conn(connection_t *conn) {
  135. circuit_t *circ;
  136. connection_t *tmpconn;
  137. for(circ=global_circuitlist;circ;circ = circ->next) {
  138. if(circ->p_conn == conn)
  139. return circ;
  140. if(circ->n_conn == conn)
  141. return circ;
  142. for(tmpconn = circ->p_streams; tmpconn; tmpconn=tmpconn->next_stream)
  143. if(tmpconn == conn)
  144. return circ;
  145. for(tmpconn = circ->n_streams; tmpconn; tmpconn=tmpconn->next_stream)
  146. if(tmpconn == conn)
  147. return circ;
  148. }
  149. return NULL;
  150. }
  151. /* Find the newest circ that conn can use, preferably one which is
  152. * dirty and not too old.
  153. * If !conn, return newest.
  154. *
  155. * If must_be_open, ignore circs not in CIRCUIT_STATE_OPEN.
  156. */
  157. circuit_t *circuit_get_newest(connection_t *conn, int must_be_open) {
  158. circuit_t *circ, *newest=NULL, *leastdirty=NULL;
  159. routerinfo_t *exitrouter;
  160. for(circ=global_circuitlist;circ;circ = circ->next) {
  161. if(!circ->cpath)
  162. continue; /* this circ doesn't start at us */
  163. if(must_be_open && (circ->state != CIRCUIT_STATE_OPEN || !circ->n_conn))
  164. continue; /* ignore non-open circs */
  165. if(conn) {
  166. if(circ->state == CIRCUIT_STATE_OPEN && circ->n_conn) /* open */
  167. exitrouter = router_get_by_addr_port(circ->cpath->prev->addr, circ->cpath->prev->port);
  168. else /* not open */
  169. exitrouter = router_get_by_nickname(circ->build_state->chosen_exit);
  170. if(!exitrouter || connection_ap_can_use_exit(conn, exitrouter) < 0) {
  171. /* can't exit from this router */
  172. continue;
  173. }
  174. }
  175. if(!newest || newest->timestamp_created < circ->timestamp_created) {
  176. assert(circ->n_circ_id);
  177. newest = circ;
  178. }
  179. if(conn && circ->timestamp_dirty &&
  180. (!leastdirty || leastdirty->timestamp_dirty < circ->timestamp_dirty)) {
  181. assert(circ->n_circ_id);
  182. leastdirty = circ;
  183. }
  184. }
  185. if(leastdirty &&
  186. leastdirty->timestamp_dirty+options.NewCircuitPeriod > time(NULL)) {
  187. log_fn(LOG_DEBUG,"Choosing in-use circuit %s:%d:%d.",
  188. leastdirty->n_conn->address, leastdirty->n_port, leastdirty->n_circ_id);
  189. return leastdirty;
  190. }
  191. if(newest) {
  192. log_fn(LOG_DEBUG,"Choosing circuit %s:%d:%d.",
  193. newest->n_conn->address, newest->n_port, newest->n_circ_id);
  194. return newest;
  195. }
  196. return NULL;
  197. }
  198. #define MIN_SECONDS_BEFORE_EXPIRING_CIRC 2
  199. /* circuits that were born at the end of their second might be expired
  200. * after 2.1 seconds; circuits born at the beginning might be expired
  201. * after closer to 3 seconds.
  202. */
  203. /* close all circuits that start at us, aren't open, and were born
  204. * at least MIN_SECONDS_BEFORE_EXPIRING_CIRC seconds ago */
  205. void circuit_expire_building(void) {
  206. int now = time(NULL);
  207. circuit_t *victim, *circ = global_circuitlist;
  208. while(circ) {
  209. victim = circ;
  210. circ = circ->next;
  211. if(victim->cpath &&
  212. victim->state != CIRCUIT_STATE_OPEN &&
  213. victim->timestamp_created + MIN_SECONDS_BEFORE_EXPIRING_CIRC+1 < now) {
  214. if(victim->n_conn)
  215. log_fn(LOG_INFO,"Abandoning circ %s:%d:%d (state %d:%s)",
  216. victim->n_conn->address, victim->n_port, victim->n_circ_id,
  217. victim->state, circuit_state_to_string[victim->state]);
  218. else
  219. log_fn(LOG_INFO,"Abandoning circ %d (state %d:%s)", victim->n_circ_id,
  220. victim->state, circuit_state_to_string[victim->state]);
  221. circuit_close(victim);
  222. }
  223. }
  224. }
  225. /* count the number of circs starting at us that aren't open */
  226. int circuit_count_building(void) {
  227. circuit_t *circ;
  228. int num=0;
  229. for(circ=global_circuitlist;circ;circ = circ->next) {
  230. if(circ->cpath && circ->state != CIRCUIT_STATE_OPEN)
  231. num++;
  232. }
  233. return num;
  234. }
  235. int circuit_deliver_relay_cell(cell_t *cell, circuit_t *circ,
  236. int cell_direction, crypt_path_t *layer_hint) {
  237. connection_t *conn=NULL;
  238. char recognized=0;
  239. char buf[256];
  240. assert(cell && circ);
  241. assert(cell_direction == CELL_DIRECTION_OUT || cell_direction == CELL_DIRECTION_IN);
  242. buf[0] = cell->length;
  243. memcpy(buf+1, cell->payload, CELL_PAYLOAD_SIZE);
  244. log_fn(LOG_DEBUG,"direction %d, streamid %d before crypt.", cell_direction, *(int*)(cell->payload+1));
  245. if(relay_crypt(circ, buf, 1+CELL_PAYLOAD_SIZE, cell_direction, &layer_hint, &recognized, &conn) < 0) {
  246. log_fn(LOG_WARN,"relay crypt failed. Dropping connection.");
  247. return -1;
  248. }
  249. cell->length = buf[0];
  250. memcpy(cell->payload, buf+1, CELL_PAYLOAD_SIZE);
  251. if(recognized) {
  252. if(cell_direction == CELL_DIRECTION_OUT) {
  253. ++stats_n_relay_cells_delivered;
  254. log_fn(LOG_DEBUG,"Sending to exit.");
  255. if (connection_edge_process_relay_cell(cell, circ, conn, EDGE_EXIT, NULL) < 0) {
  256. log_fn(LOG_WARN,"connection_edge_process_relay_cell (at exit) failed.");
  257. return -1;
  258. }
  259. }
  260. if(cell_direction == CELL_DIRECTION_IN) {
  261. ++stats_n_relay_cells_delivered;
  262. log_fn(LOG_DEBUG,"Sending to AP.");
  263. if (connection_edge_process_relay_cell(cell, circ, conn, EDGE_AP, layer_hint) < 0) {
  264. log_fn(LOG_WARN,"connection_edge_process_relay_cell (at AP) failed.");
  265. return -1;
  266. }
  267. }
  268. return 0;
  269. }
  270. /* not recognized. pass it on. */
  271. if(cell_direction == CELL_DIRECTION_OUT)
  272. conn = circ->n_conn;
  273. else
  274. conn = circ->p_conn;
  275. if(!conn) {
  276. log_fn(LOG_INFO,"Didn't recognize cell (%d), but circ stops here! Dropping.",
  277. *(int *)(cell->payload+1));
  278. return 0;
  279. }
  280. log_fn(LOG_DEBUG,"Passing on unrecognized cell.");
  281. ++stats_n_relay_cells_relayed;
  282. connection_or_write_cell_to_buf(cell, conn);
  283. return 0;
  284. }
  285. int relay_crypt(circuit_t *circ, char *in, int inlen, char cell_direction,
  286. crypt_path_t **layer_hint, char *recognized, connection_t **conn) {
  287. crypt_path_t *thishop;
  288. char out[256];
  289. assert(circ && in && recognized && conn);
  290. assert(inlen < 256);
  291. if(cell_direction == CELL_DIRECTION_IN) {
  292. if(circ->cpath) { /* we're at the beginning of the circuit. We'll want to do layered crypts. */
  293. thishop = circ->cpath;
  294. if(thishop->state != CPATH_STATE_OPEN) {
  295. log_fn(LOG_WARN,"Relay cell before first created cell?");
  296. return -1;
  297. }
  298. do { /* Remember: cpath is in forward order, that is, first hop first. */
  299. assert(thishop);
  300. log_fn(LOG_DEBUG,"before decrypt: %d",*(int*)(in+2));
  301. /* decrypt */
  302. if(crypto_cipher_decrypt(thishop->b_crypto, in, inlen, out)) {
  303. log_fn(LOG_WARN,"Error performing onion decryption: %s", crypto_perror());
  304. return -1;
  305. }
  306. memcpy(in,out,inlen);
  307. log_fn(LOG_DEBUG,"after decrypt: %d",*(int*)(in+2));
  308. if( (*recognized = relay_check_recognized(circ, cell_direction, in+2, conn))) {
  309. *layer_hint = thishop;
  310. return 0;
  311. }
  312. thishop = thishop->next;
  313. } while(thishop != circ->cpath && thishop->state == CPATH_STATE_OPEN);
  314. log_fn(LOG_INFO,"in-cell at OP not recognized. Dropping.");
  315. return 0;
  316. } else { /* we're in the middle. Just one crypt. */
  317. log_fn(LOG_DEBUG,"before encrypt: %d",*(int*)(in+2));
  318. if(crypto_cipher_encrypt(circ->p_crypto, in, inlen, out)) {
  319. log_fn(LOG_WARN,"Onion encryption failed for circID %u: %s",
  320. circ->p_circ_id, crypto_perror());
  321. return -1;
  322. }
  323. memcpy(in,out,inlen);
  324. log_fn(LOG_DEBUG,"after encrypt: %d",*(int*)(in+2));
  325. log_fn(LOG_DEBUG,"Skipping recognized check, because we're not the OP.");
  326. /* don't check for recognized. only the OP can recognize a stream on the way back. */
  327. }
  328. } else if(cell_direction == CELL_DIRECTION_OUT) {
  329. if(circ->cpath) { /* we're at the beginning of the circuit. We'll want to do layered crypts. */
  330. thishop = *layer_hint; /* we already know which layer, from when we package_raw_inbuf'ed */
  331. /* moving from last to first hop */
  332. do {
  333. assert(thishop);
  334. log_fn(LOG_DEBUG,"before encrypt: %d",*(int*)(in+2));
  335. if(crypto_cipher_encrypt(thishop->f_crypto, in, inlen, out)) {
  336. log_fn(LOG_WARN,"Error performing encryption: %s", crypto_perror());
  337. return -1;
  338. }
  339. memcpy(in,out,inlen);
  340. log_fn(LOG_DEBUG,"after encrypt: %d",*(int*)(in+2));
  341. thishop = thishop->prev;
  342. } while(thishop != circ->cpath->prev);
  343. } else { /* we're in the middle. Just one crypt. */
  344. if(crypto_cipher_decrypt(circ->n_crypto,in, inlen, out)) {
  345. log_fn(LOG_WARN,"Decryption failed for circID %u: %s",
  346. circ->n_circ_id, crypto_perror());
  347. return -1;
  348. }
  349. memcpy(in,out,inlen);
  350. if( (*recognized = relay_check_recognized(circ, cell_direction, in+2, conn)))
  351. return 0;
  352. }
  353. } else {
  354. log_fn(LOG_ERR,"unknown cell direction %d.", cell_direction);
  355. assert(0);
  356. }
  357. return 0;
  358. }
  359. int relay_check_recognized(circuit_t *circ, int cell_direction, char *stream, connection_t **conn) {
  360. /* FIXME can optimize by passing thishop in */
  361. connection_t *tmpconn;
  362. if(!memcmp(stream,ZERO_STREAM,STREAM_ID_SIZE)) {
  363. log_fn(LOG_DEBUG,"It's the zero stream. Recognized.");
  364. return 1; /* the zero stream is always recognized */
  365. }
  366. log_fn(LOG_DEBUG,"not the zero stream.");
  367. if(cell_direction == CELL_DIRECTION_OUT)
  368. tmpconn = circ->n_streams;
  369. else
  370. tmpconn = circ->p_streams;
  371. if(!tmpconn) {
  372. log_fn(LOG_DEBUG,"No conns. Not recognized.");
  373. return 0;
  374. }
  375. for( ; tmpconn; tmpconn=tmpconn->next_stream) {
  376. if(!memcmp(stream,tmpconn->stream_id, STREAM_ID_SIZE)) {
  377. log_fn(LOG_DEBUG,"recognized stream %d.", *(int*)stream);
  378. *conn = tmpconn;
  379. return 1;
  380. }
  381. log_fn(LOG_DEBUG,"considered stream %d, not it.",*(int*)tmpconn->stream_id);
  382. }
  383. log_fn(LOG_DEBUG,"Didn't recognize on this iteration of decryption.");
  384. return 0;
  385. }
  386. void circuit_resume_edge_reading(circuit_t *circ, int edge_type, crypt_path_t *layer_hint) {
  387. connection_t *conn;
  388. assert(edge_type == EDGE_EXIT || edge_type == EDGE_AP);
  389. log_fn(LOG_DEBUG,"resuming");
  390. if(edge_type == EDGE_EXIT)
  391. conn = circ->n_streams;
  392. else
  393. conn = circ->p_streams;
  394. for( ; conn; conn=conn->next_stream) {
  395. if((edge_type == EDGE_EXIT && conn->package_window > 0) ||
  396. (edge_type == EDGE_AP && conn->package_window > 0 && conn->cpath_layer == layer_hint)) {
  397. connection_start_reading(conn);
  398. connection_edge_package_raw_inbuf(conn); /* handle whatever might still be on the inbuf */
  399. /* If the circuit won't accept any more data, return without looking
  400. * at any more of the streams. Any connections that should be stopped
  401. * have already been stopped by connection_edge_package_raw_inbuf. */
  402. if(circuit_consider_stop_edge_reading(circ, edge_type, layer_hint))
  403. return;
  404. }
  405. }
  406. }
  407. /* returns 1 if the window is empty, else 0. If it's empty, tell edge conns to stop reading. */
  408. int circuit_consider_stop_edge_reading(circuit_t *circ, int edge_type, crypt_path_t *layer_hint) {
  409. connection_t *conn = NULL;
  410. assert(edge_type == EDGE_EXIT || edge_type == EDGE_AP);
  411. assert(edge_type == EDGE_EXIT || layer_hint);
  412. log_fn(LOG_DEBUG,"considering");
  413. if(edge_type == EDGE_EXIT && circ->package_window <= 0)
  414. conn = circ->n_streams;
  415. else if(edge_type == EDGE_AP && layer_hint->package_window <= 0)
  416. conn = circ->p_streams;
  417. else
  418. return 0;
  419. for( ; conn; conn=conn->next_stream)
  420. if(!layer_hint || conn->cpath_layer == layer_hint)
  421. connection_stop_reading(conn);
  422. log_fn(LOG_DEBUG,"yes. stopped.");
  423. return 1;
  424. }
  425. int circuit_consider_sending_sendme(circuit_t *circ, int edge_type, crypt_path_t *layer_hint) {
  426. cell_t cell;
  427. assert(circ);
  428. memset(&cell, 0, sizeof(cell_t));
  429. cell.command = CELL_RELAY;
  430. SET_CELL_RELAY_COMMAND(cell, RELAY_COMMAND_SENDME);
  431. SET_CELL_STREAM_ID(cell, ZERO_STREAM);
  432. cell.length = RELAY_HEADER_SIZE;
  433. if(edge_type == EDGE_AP) { /* i'm the AP */
  434. cell.circ_id = circ->n_circ_id;
  435. while(layer_hint->deliver_window < CIRCWINDOW_START-CIRCWINDOW_INCREMENT) {
  436. log_fn(LOG_DEBUG,"deliver_window %d, Queueing sendme forward.", layer_hint->deliver_window);
  437. layer_hint->deliver_window += CIRCWINDOW_INCREMENT;
  438. if(circuit_deliver_relay_cell(&cell, circ, CELL_DIRECTION_OUT, layer_hint) < 0) {
  439. log_fn(LOG_WARN,"At AP: circuit_deliver_relay_cell failed.");
  440. return -1;
  441. }
  442. }
  443. } else if(edge_type == EDGE_EXIT) { /* i'm the exit */
  444. cell.circ_id = circ->p_circ_id;
  445. while(circ->deliver_window < CIRCWINDOW_START-CIRCWINDOW_INCREMENT) {
  446. log_fn(LOG_DEBUG,"deliver_window %d, Queueing sendme back.", circ->deliver_window);
  447. circ->deliver_window += CIRCWINDOW_INCREMENT;
  448. if(circuit_deliver_relay_cell(&cell, circ, CELL_DIRECTION_IN, layer_hint) < 0) {
  449. log_fn(LOG_WARN,"At exit: circuit_deliver_relay_cell failed.");
  450. return -1;
  451. }
  452. }
  453. }
  454. return 0;
  455. }
  456. void circuit_close(circuit_t *circ) {
  457. connection_t *conn;
  458. assert(circ);
  459. circuit_remove(circ);
  460. if(circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  461. onion_pending_remove(circ);
  462. }
  463. if(circ->n_conn)
  464. connection_send_destroy(circ->n_circ_id, circ->n_conn);
  465. for(conn=circ->n_streams; conn; conn=conn->next_stream) {
  466. connection_send_destroy(circ->n_circ_id, conn);
  467. }
  468. if(circ->p_conn)
  469. connection_send_destroy(circ->n_circ_id, circ->p_conn);
  470. for(conn=circ->p_streams; conn; conn=conn->next_stream) {
  471. connection_send_destroy(circ->p_circ_id, conn);
  472. }
  473. circuit_free(circ);
  474. }
  475. void circuit_about_to_close_connection(connection_t *conn) {
  476. /* send destroys for all circuits using conn */
  477. /* currently, we assume it's too late to flush conn's buf here.
  478. * down the road, maybe we'll consider that eof doesn't mean can't-write
  479. */
  480. circuit_t *circ;
  481. connection_t *prevconn;
  482. switch(conn->type) {
  483. case CONN_TYPE_OR:
  484. /* We must close all the circuits on it. */
  485. while((circ = circuit_get_by_conn(conn))) {
  486. if(circ->n_conn == conn) /* it's closing in front of us */
  487. circ->n_conn = NULL;
  488. if(circ->p_conn == conn) /* it's closing behind us */
  489. circ->p_conn = NULL;
  490. circuit_close(circ);
  491. }
  492. return;
  493. case CONN_TYPE_AP:
  494. case CONN_TYPE_EXIT:
  495. /* It's an edge conn. Need to remove it from the linked list of
  496. * conn's for this circuit. Confirm that 'end' relay command has
  497. * been sent. But don't kill the circuit.
  498. */
  499. circ = circuit_get_by_conn(conn);
  500. if(!circ)
  501. return;
  502. if(!conn->has_sent_end) {
  503. log_fn(LOG_INFO,"Edge connection hasn't sent end yet? Bug.");
  504. connection_edge_end(conn, END_STREAM_REASON_MISC, conn->cpath_layer);
  505. }
  506. if(conn == circ->p_streams) {
  507. circ->p_streams = conn->next_stream;
  508. return;
  509. }
  510. if(conn == circ->n_streams) {
  511. circ->n_streams = conn->next_stream;
  512. return;
  513. }
  514. for(prevconn = circ->p_streams; prevconn && prevconn->next_stream && prevconn->next_stream != conn; prevconn = prevconn->next_stream) ;
  515. if(prevconn && prevconn->next_stream) {
  516. prevconn->next_stream = conn->next_stream;
  517. return;
  518. }
  519. for(prevconn = circ->n_streams; prevconn && prevconn->next_stream && prevconn->next_stream != conn; prevconn = prevconn->next_stream) ;
  520. if(prevconn && prevconn->next_stream) {
  521. prevconn->next_stream = conn->next_stream;
  522. return;
  523. }
  524. log_fn(LOG_ERR,"edge conn not in circuit's list?");
  525. assert(0); /* should never get here */
  526. } /* end switch */
  527. }
  528. void circuit_dump_details(int severity, circuit_t *circ, int poll_index,
  529. char *type, int this_circid, int other_circid) {
  530. struct crypt_path_t *hop;
  531. log(severity,"Conn %d has %s circuit: circID %d (other side %d), state %d (%s), born %d",
  532. poll_index, type, this_circid, other_circid, circ->state,
  533. circuit_state_to_string[circ->state], (int)circ->timestamp_created);
  534. if(circ->cpath) { /* circ starts at this node */
  535. if(circ->state == CIRCUIT_STATE_BUILDING)
  536. log(severity,"Building: desired len %d, planned exit node %s.",
  537. circ->build_state->desired_path_len, circ->build_state->chosen_exit);
  538. for(hop=circ->cpath;hop->next != circ->cpath; hop=hop->next)
  539. log(severity,"hop: state %d, addr %d, port %d", hop->state, hop->addr, hop->port);
  540. }
  541. }
  542. void circuit_dump_by_conn(connection_t *conn, int severity) {
  543. circuit_t *circ;
  544. connection_t *tmpconn;
  545. for(circ=global_circuitlist;circ;circ = circ->next) {
  546. if(circ->p_conn == conn)
  547. circuit_dump_details(severity, circ, conn->poll_index, "App-ward",
  548. circ->p_circ_id, circ->n_circ_id);
  549. for(tmpconn=circ->p_streams; tmpconn; tmpconn=tmpconn->next_stream) {
  550. if(tmpconn == conn) {
  551. circuit_dump_details(severity, circ, conn->poll_index, "App-ward",
  552. circ->p_circ_id, circ->n_circ_id);
  553. }
  554. }
  555. if(circ->n_conn == conn)
  556. circuit_dump_details(severity, circ, conn->poll_index, "Exit-ward",
  557. circ->n_circ_id, circ->p_circ_id);
  558. for(tmpconn=circ->n_streams; tmpconn; tmpconn=tmpconn->next_stream) {
  559. if(tmpconn == conn) {
  560. circuit_dump_details(severity, circ, conn->poll_index, "Exit-ward",
  561. circ->n_circ_id, circ->p_circ_id);
  562. }
  563. }
  564. }
  565. }
  566. void circuit_expire_unused_circuits(void) {
  567. circuit_t *circ, *tmpcirc;
  568. time_t now = time(NULL);
  569. circ = global_circuitlist;
  570. while(circ) {
  571. tmpcirc = circ;
  572. circ = circ->next;
  573. if(tmpcirc->timestamp_dirty &&
  574. tmpcirc->timestamp_dirty + options.NewCircuitPeriod < now &&
  575. !tmpcirc->p_conn && !tmpcirc->p_streams) {
  576. log_fn(LOG_DEBUG,"Closing n_circ_id %d",tmpcirc->n_circ_id);
  577. circuit_close(tmpcirc);
  578. }
  579. }
  580. }
  581. /* failure_status code: negative means reset failures to 0. Other values mean
  582. * add that value to the current number of failures, then if we don't have too
  583. * many failures on record, try to make a new circuit.
  584. *
  585. * Return -1 if you aren't going to try to make a circuit, 0 if you did try.
  586. */
  587. int circuit_launch_new(int failure_status) {
  588. static int failures=0;
  589. if(!options.SocksPort) /* we're not an application proxy. no need for circuits. */
  590. return -1;
  591. if(failure_status == -1) { /* I was called because a circuit succeeded */
  592. failures = 0;
  593. return -1;
  594. }
  595. failures += failure_status;
  596. if(failures > 5) {
  597. return -1;
  598. }
  599. if(circuit_establish_circuit() < 0) {
  600. return 0;
  601. }
  602. failures = 0;
  603. return 0;
  604. }
  605. int circuit_establish_circuit(void) {
  606. routerinfo_t *firsthop;
  607. connection_t *n_conn;
  608. circuit_t *circ;
  609. circ = circuit_new(0, NULL); /* sets circ->p_circ_id and circ->p_conn */
  610. circ->state = CIRCUIT_STATE_OR_WAIT;
  611. circ->build_state = onion_new_cpath_build_state();
  612. if (! circ->build_state) {
  613. log_fn(LOG_INFO,"Generating cpath length failed.");
  614. circuit_close(circ);
  615. return -1;
  616. }
  617. onion_extend_cpath(&circ->cpath, circ->build_state, &firsthop);
  618. if(!circ->cpath) {
  619. log_fn(LOG_INFO,"Generating first cpath hop failed.");
  620. circuit_close(circ);
  621. return -1;
  622. }
  623. /* now see if we're already connected to the first OR in 'route' */
  624. log_fn(LOG_DEBUG,"Looking for firsthop '%s:%u'",
  625. firsthop->address,firsthop->or_port);
  626. n_conn = connection_twin_get_by_addr_port(firsthop->addr,firsthop->or_port);
  627. if(!n_conn || n_conn->state != OR_CONN_STATE_OPEN) { /* not currently connected */
  628. circ->n_addr = firsthop->addr;
  629. circ->n_port = firsthop->or_port;
  630. if(options.OnionRouter) { /* we would be connected if he were up. but he's not. */
  631. log_fn(LOG_INFO,"Route's firsthop isn't connected.");
  632. circuit_close(circ);
  633. return -1;
  634. }
  635. if(!n_conn) { /* launch the connection */
  636. n_conn = connection_or_connect(firsthop);
  637. if(!n_conn) { /* connect failed, forget the whole thing */
  638. log_fn(LOG_INFO,"connect to firsthop failed. Closing.");
  639. circuit_close(circ);
  640. return -1;
  641. }
  642. }
  643. log_fn(LOG_DEBUG,"connecting in progress (or finished). Good.");
  644. return 0; /* return success. The onion/circuit/etc will be taken care of automatically
  645. * (may already have been) whenever n_conn reaches OR_CONN_STATE_OPEN.
  646. */
  647. } else { /* it (or a twin) is already open. use it. */
  648. circ->n_addr = n_conn->addr;
  649. circ->n_port = n_conn->port;
  650. circ->n_conn = n_conn;
  651. log_fn(LOG_DEBUG,"Conn open. Delivering first onion skin.");
  652. if(circuit_send_next_onion_skin(circ) < 0) {
  653. log_fn(LOG_INFO,"circuit_send_next_onion_skin failed.");
  654. circuit_close(circ);
  655. return -1;
  656. }
  657. }
  658. return 0;
  659. }
  660. /* find circuits that are waiting on me, if any, and get them to send the onion */
  661. void circuit_n_conn_open(connection_t *or_conn) {
  662. circuit_t *circ;
  663. for(circ=global_circuitlist;circ;circ = circ->next) {
  664. if(circ->cpath && circ->n_addr == or_conn->addr && circ->n_port == or_conn->port) {
  665. assert(circ->state == CIRCUIT_STATE_OR_WAIT);
  666. log_fn(LOG_DEBUG,"Found circ %d, sending onion skin.", circ->n_circ_id);
  667. circ->n_conn = or_conn;
  668. if(circuit_send_next_onion_skin(circ) < 0) {
  669. log_fn(LOG_INFO,"send_next_onion_skin failed; circuit marked for closing.");
  670. circuit_close(circ);
  671. continue;
  672. /* XXX could this be bad, eg if next_onion_skin failed because conn died? */
  673. }
  674. }
  675. }
  676. }
  677. int circuit_send_next_onion_skin(circuit_t *circ) {
  678. cell_t cell;
  679. crypt_path_t *hop;
  680. routerinfo_t *router;
  681. int r;
  682. int circ_id_type;
  683. assert(circ && circ->cpath);
  684. if(circ->cpath->state == CPATH_STATE_CLOSED) {
  685. assert(circ->n_conn && circ->n_conn->type == CONN_TYPE_OR);
  686. log_fn(LOG_DEBUG,"First skin; sending create cell.");
  687. circ_id_type = decide_circ_id_type(options.Nickname,
  688. circ->n_conn->nickname);
  689. circ->n_circ_id = get_unique_circ_id_by_conn(circ->n_conn, circ_id_type);
  690. memset(&cell, 0, sizeof(cell_t));
  691. cell.command = CELL_CREATE;
  692. cell.circ_id = circ->n_circ_id;
  693. cell.length = DH_ONIONSKIN_LEN;
  694. if(onion_skin_create(circ->n_conn->onion_pkey, &(circ->cpath->handshake_state), cell.payload) < 0) {
  695. log_fn(LOG_WARN,"onion_skin_create (first hop) failed.");
  696. return -1;
  697. }
  698. connection_or_write_cell_to_buf(&cell, circ->n_conn);
  699. circ->cpath->state = CPATH_STATE_AWAITING_KEYS;
  700. circ->state = CIRCUIT_STATE_BUILDING;
  701. log_fn(LOG_DEBUG,"first skin; finished sending create cell.");
  702. } else {
  703. assert(circ->cpath->state == CPATH_STATE_OPEN);
  704. assert(circ->state == CIRCUIT_STATE_BUILDING);
  705. log_fn(LOG_DEBUG,"starting to send subsequent skin.");
  706. r = onion_extend_cpath(&circ->cpath, circ->build_state, &router);
  707. if (r==1) {
  708. /* done building the circuit. whew. */
  709. circ->state = CIRCUIT_STATE_OPEN;
  710. log_fn(LOG_INFO,"circuit built!");
  711. /* Tell any AP connections that have been waiting for a new
  712. * circuit that one is ready. */
  713. connection_ap_attach_pending();
  714. return 0;
  715. } else if (r<0) {
  716. log_fn(LOG_INFO,"Unable to extend circuit path.");
  717. return -1;
  718. }
  719. hop = circ->cpath->prev;
  720. memset(&cell, 0, sizeof(cell_t));
  721. cell.command = CELL_RELAY;
  722. cell.circ_id = circ->n_circ_id;
  723. SET_CELL_RELAY_COMMAND(cell, RELAY_COMMAND_EXTEND);
  724. SET_CELL_STREAM_ID(cell, ZERO_STREAM);
  725. cell.length = RELAY_HEADER_SIZE + 6 + DH_ONIONSKIN_LEN;
  726. *(uint32_t*)(cell.payload+RELAY_HEADER_SIZE) = htonl(hop->addr);
  727. *(uint16_t*)(cell.payload+RELAY_HEADER_SIZE+4) = htons(hop->port);
  728. if(onion_skin_create(router->onion_pkey, &(hop->handshake_state), cell.payload+RELAY_HEADER_SIZE+6) < 0) {
  729. log_fn(LOG_WARN,"onion_skin_create failed.");
  730. return -1;
  731. }
  732. log_fn(LOG_DEBUG,"Sending extend relay cell.");
  733. /* send it to hop->prev, because it will transfer it to a create cell and then send to hop */
  734. if(circuit_deliver_relay_cell(&cell, circ, CELL_DIRECTION_OUT, hop->prev) < 0) {
  735. log_fn(LOG_WARN,"failed to deliver extend cell. Closing.");
  736. return -1;
  737. }
  738. hop->state = CPATH_STATE_AWAITING_KEYS;
  739. }
  740. return 0;
  741. }
  742. /* take the 'extend' cell, pull out addr/port plus the onion skin. Make
  743. * sure we're connected to the next hop, and pass it the onion skin in
  744. * a create cell.
  745. */
  746. int circuit_extend(cell_t *cell, circuit_t *circ) {
  747. connection_t *n_conn;
  748. int circ_id_type;
  749. cell_t newcell;
  750. if(circ->n_conn) {
  751. log_fn(LOG_WARN,"n_conn already set. Bug/attack. Closing.");
  752. return -1;
  753. }
  754. circ->n_addr = ntohl(*(uint32_t*)(cell->payload+RELAY_HEADER_SIZE));
  755. circ->n_port = ntohs(*(uint16_t*)(cell->payload+RELAY_HEADER_SIZE+4));
  756. n_conn = connection_twin_get_by_addr_port(circ->n_addr,circ->n_port);
  757. if(!n_conn || n_conn->type != CONN_TYPE_OR) {
  758. /* i've disabled making connections through OPs, but it's definitely
  759. * possible here. I'm not sure if it would be a bug or a feature. -RD
  760. */
  761. /* note also that this will close circuits where the onion has the same
  762. * router twice in a row in the path. i think that's ok. -RD
  763. */
  764. struct in_addr in;
  765. in.s_addr = htonl(circ->n_addr);
  766. log_fn(LOG_INFO,"Next router (%s:%d) not connected. Closing.", inet_ntoa(in), circ->n_port);
  767. connection_edge_send_command(NULL, circ, RELAY_COMMAND_TRUNCATED,
  768. NULL, 0, NULL);
  769. return 0;
  770. }
  771. circ->n_addr = n_conn->addr; /* these are different if we found a twin instead */
  772. circ->n_port = n_conn->port;
  773. circ->n_conn = n_conn;
  774. log_fn(LOG_DEBUG,"n_conn is %s:%u",n_conn->address,n_conn->port);
  775. circ_id_type = decide_circ_id_type(options.Nickname, n_conn->nickname);
  776. log_fn(LOG_DEBUG,"circ_id_type = %u.",circ_id_type);
  777. circ->n_circ_id = get_unique_circ_id_by_conn(circ->n_conn, circ_id_type);
  778. if(!circ->n_circ_id) {
  779. log_fn(LOG_WARN,"failed to get unique circID.");
  780. return -1;
  781. }
  782. log_fn(LOG_DEBUG,"Chosen circID %u.",circ->n_circ_id);
  783. memset(&newcell, 0, sizeof(cell_t));
  784. newcell.command = CELL_CREATE;
  785. newcell.circ_id = circ->n_circ_id;
  786. newcell.length = DH_ONIONSKIN_LEN;
  787. memcpy(newcell.payload, cell->payload+RELAY_HEADER_SIZE+6, DH_ONIONSKIN_LEN);
  788. connection_or_write_cell_to_buf(&newcell, circ->n_conn);
  789. return 0;
  790. }
  791. int circuit_finish_handshake(circuit_t *circ, char *reply) {
  792. unsigned char iv[16];
  793. unsigned char keys[32];
  794. crypt_path_t *hop;
  795. memset(iv, 0, 16);
  796. assert(circ->cpath);
  797. if(circ->cpath->state == CPATH_STATE_AWAITING_KEYS)
  798. hop = circ->cpath;
  799. else {
  800. for(hop=circ->cpath->next;
  801. hop != circ->cpath && hop->state == CPATH_STATE_OPEN;
  802. hop=hop->next) ;
  803. if(hop == circ->cpath) { /* got an extended when we're all done? */
  804. log_fn(LOG_WARN,"got extended when circ already built? Closing.");
  805. return -1;
  806. }
  807. }
  808. assert(hop->state == CPATH_STATE_AWAITING_KEYS);
  809. if(onion_skin_client_handshake(hop->handshake_state, reply, keys, 32) < 0) {
  810. log_fn(LOG_WARN,"onion_skin_client_handshake failed.");
  811. return -1;
  812. }
  813. crypto_dh_free(hop->handshake_state); /* don't need it anymore */
  814. hop->handshake_state = NULL;
  815. log_fn(LOG_DEBUG,"hop %d init cipher forward %d, backward %d.", (uint32_t)hop, *(uint32_t*)keys, *(uint32_t*)(keys+16));
  816. if (!(hop->f_crypto =
  817. crypto_create_init_cipher(CIRCUIT_CIPHER,keys,iv,1))) {
  818. log(LOG_WARN,"forward cipher initialization failed.");
  819. return -1;
  820. }
  821. if (!(hop->b_crypto =
  822. crypto_create_init_cipher(CIRCUIT_CIPHER,keys+16,iv,0))) {
  823. log(LOG_WARN,"backward cipher initialization failed.");
  824. return -1;
  825. }
  826. hop->state = CPATH_STATE_OPEN;
  827. log_fn(LOG_INFO,"finished");
  828. return 0;
  829. }
  830. int circuit_truncated(circuit_t *circ, crypt_path_t *layer) {
  831. crypt_path_t *victim;
  832. connection_t *stream;
  833. assert(circ);
  834. assert(layer);
  835. while(layer->next != circ->cpath) {
  836. /* we need to clear out layer->next */
  837. victim = layer->next;
  838. log_fn(LOG_DEBUG, "Killing a layer of the cpath.");
  839. for(stream = circ->p_streams; stream; stream=stream->next_stream) {
  840. if(stream->cpath_layer == victim) {
  841. log_fn(LOG_INFO, "Marking stream %d for close.", *(int*)stream->stream_id);
  842. /* no need to send 'end' relay cells,
  843. * because the other side's already dead
  844. */
  845. stream->marked_for_close = 1;
  846. stream->has_sent_end = 1;
  847. }
  848. }
  849. layer->next = victim->next;
  850. circuit_free_cpath_node(victim);
  851. }
  852. log_fn(LOG_INFO, "finished");
  853. return 0;
  854. }
  855. void assert_cpath_layer_ok(const crypt_path_t *cp)
  856. {
  857. assert(cp->f_crypto);
  858. assert(cp->b_crypto);
  859. assert(cp->addr);
  860. assert(cp->port);
  861. switch(cp->state)
  862. {
  863. case CPATH_STATE_CLOSED:
  864. case CPATH_STATE_OPEN:
  865. assert(!cp->handshake_state);
  866. break;
  867. case CPATH_STATE_AWAITING_KEYS:
  868. assert(cp->handshake_state);
  869. break;
  870. default:
  871. assert(0);
  872. }
  873. assert(cp->package_window >= 0);
  874. assert(cp->deliver_window >= 0);
  875. }
  876. void assert_cpath_ok(const crypt_path_t *cp)
  877. {
  878. while(cp->prev)
  879. cp = cp->prev;
  880. while(cp->next) {
  881. assert_cpath_layer_ok(cp);
  882. /* layers must be in sequence of: "open* awaiting? closed*" */
  883. if (cp->prev) {
  884. if (cp->prev->state == CPATH_STATE_OPEN) {
  885. assert(cp->state == CPATH_STATE_CLOSED ||
  886. cp->state == CPATH_STATE_AWAITING_KEYS);
  887. } else {
  888. assert(cp->state == CPATH_STATE_CLOSED);
  889. }
  890. }
  891. cp = cp->next;
  892. }
  893. }
  894. void assert_circuit_ok(const circuit_t *c)
  895. {
  896. connection_t *conn;
  897. assert(c->n_addr);
  898. assert(c->n_port);
  899. assert(c->n_conn);
  900. assert(c->n_conn->type == CONN_TYPE_OR);
  901. if (c->p_conn)
  902. assert(c->p_conn->type == CONN_TYPE_OR);
  903. for (conn = c->p_streams; conn; conn = conn->next_stream)
  904. assert(c->p_conn->type == CONN_TYPE_EXIT);
  905. for (conn = c->n_streams; conn; conn = conn->next_stream)
  906. assert(conn->type == CONN_TYPE_EXIT);
  907. assert(c->deliver_window >= 0);
  908. assert(c->package_window >= 0);
  909. if (c->state == CIRCUIT_STATE_OPEN) {
  910. if (c->cpath) {
  911. assert(!c->n_crypto);
  912. assert(!c->p_crypto);
  913. } else {
  914. assert(c->n_crypto);
  915. assert(c->p_crypto);
  916. }
  917. }
  918. if (c->cpath) {
  919. assert_cpath_ok(c->cpath);
  920. }
  921. }
  922. /*
  923. Local Variables:
  924. mode:c
  925. indent-tabs-mode:nil
  926. c-basic-offset:2
  927. End:
  928. */