circuitlist.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. /* Copyright 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2007, Roger Dingledine, Nick Mathewson. */
  4. /* See LICENSE for licensing information */
  5. /* $Id$ */
  6. const char circuitlist_c_id[] =
  7. "$Id$";
  8. /**
  9. * \file circuitlist.c
  10. * \brief Manage the global circuit list.
  11. **/
  12. #include "or.h"
  13. #include "../common/ht.h"
  14. /********* START VARIABLES **********/
  15. /** A global list of all circuits at this hop. */
  16. circuit_t *global_circuitlist=NULL;
  17. /** A list of all the circuits in CIRCUIT_STATE_OR_WAIT. */
  18. static smartlist_t *circuits_pending_or_conns=NULL;
  19. static void circuit_free(circuit_t *circ);
  20. static void circuit_free_cpath(crypt_path_t *cpath);
  21. static void circuit_free_cpath_node(crypt_path_t *victim);
  22. /********* END VARIABLES ************/
  23. /** A map from OR connection and circuit ID to circuit. (Lookup performance is
  24. * very important here, since we need to do it every time a cell arrives.) */
  25. typedef struct orconn_circid_circuit_map_t {
  26. HT_ENTRY(orconn_circid_circuit_map_t) node;
  27. or_connection_t *or_conn;
  28. uint16_t circ_id;
  29. circuit_t *circuit;
  30. } orconn_circid_circuit_map_t;
  31. /** Helper for hash tables: compare the OR connection and circuit ID for a and
  32. * b, and return less than, equal to, or greater than zero appropriately.
  33. */
  34. static INLINE int
  35. _orconn_circid_entries_eq(orconn_circid_circuit_map_t *a,
  36. orconn_circid_circuit_map_t *b)
  37. {
  38. return a->or_conn == b->or_conn && a->circ_id == b->circ_id;
  39. }
  40. /** DOCDOC */
  41. static INLINE unsigned int
  42. _orconn_circid_entry_hash(orconn_circid_circuit_map_t *a)
  43. {
  44. return (((unsigned)a->circ_id)<<16) ^ (unsigned)(uintptr_t)(a->or_conn);
  45. }
  46. /** DOCDOC */
  47. static HT_HEAD(orconn_circid_map, orconn_circid_circuit_map_t)
  48. orconn_circid_circuit_map = HT_INITIALIZER();
  49. HT_PROTOTYPE(orconn_circid_map, orconn_circid_circuit_map_t, node,
  50. _orconn_circid_entry_hash, _orconn_circid_entries_eq);
  51. HT_GENERATE(orconn_circid_map, orconn_circid_circuit_map_t, node,
  52. _orconn_circid_entry_hash, _orconn_circid_entries_eq, 0.6,
  53. malloc, realloc, free);
  54. /** The most recently returned entry from circuit_get_by_circid_orconn;
  55. * used to improve performance when many cells arrive in a row from the
  56. * same circuit.
  57. */
  58. orconn_circid_circuit_map_t *_last_circid_orconn_ent = NULL;
  59. static void
  60. circuit_set_circid_orconn_helper(circuit_t *circ, uint16_t id,
  61. or_connection_t *conn,
  62. uint16_t old_id, or_connection_t *old_conn)
  63. {
  64. orconn_circid_circuit_map_t search;
  65. orconn_circid_circuit_map_t *found;
  66. if (_last_circid_orconn_ent &&
  67. ((old_id == _last_circid_orconn_ent->circ_id &&
  68. old_conn == _last_circid_orconn_ent->or_conn) ||
  69. (id == _last_circid_orconn_ent->circ_id &&
  70. conn == _last_circid_orconn_ent->or_conn))) {
  71. _last_circid_orconn_ent = NULL;
  72. }
  73. if (old_conn) { /* we may need to remove it from the conn-circid map */
  74. tor_assert(old_conn->_base.magic == OR_CONNECTION_MAGIC);
  75. search.circ_id = old_id;
  76. search.or_conn = old_conn;
  77. found = HT_REMOVE(orconn_circid_map, &orconn_circid_circuit_map, &search);
  78. if (found) {
  79. tor_free(found);
  80. --old_conn->n_circuits;
  81. }
  82. }
  83. if (conn == NULL)
  84. return;
  85. /* now add the new one to the conn-circid map */
  86. search.circ_id = id;
  87. search.or_conn = conn;
  88. found = HT_FIND(orconn_circid_map, &orconn_circid_circuit_map, &search);
  89. if (found) {
  90. found->circuit = circ;
  91. } else {
  92. found = tor_malloc_zero(sizeof(orconn_circid_circuit_map_t));
  93. found->circ_id = id;
  94. found->or_conn = conn;
  95. found->circuit = circ;
  96. HT_INSERT(orconn_circid_map, &orconn_circid_circuit_map, found);
  97. }
  98. ++conn->n_circuits;
  99. }
  100. /** Set the p_conn field of a circuit <b>circ</b>, along
  101. * with the corresponding circuit ID, and add the circuit as appropriate
  102. * to the (orconn,id)-\>circuit map. */
  103. void
  104. circuit_set_p_circid_orconn(or_circuit_t *circ, uint16_t id,
  105. or_connection_t *conn)
  106. {
  107. uint16_t old_id;
  108. or_connection_t *old_conn;
  109. old_id = circ->p_circ_id;
  110. old_conn = circ->p_conn;
  111. circ->p_circ_id = id;
  112. circ->p_conn = conn;
  113. if (id == old_id && conn == old_conn)
  114. return;
  115. circuit_set_circid_orconn_helper(TO_CIRCUIT(circ), id, conn,
  116. old_id, old_conn);
  117. }
  118. /** Set the n_conn field of a circuit <b>circ</b>, along
  119. * with the corresponding circuit ID, and add the circuit as appropriate
  120. * to the (orconn,id)-\>circuit map. */
  121. void
  122. circuit_set_n_circid_orconn(circuit_t *circ, uint16_t id,
  123. or_connection_t *conn)
  124. {
  125. uint16_t old_id;
  126. or_connection_t *old_conn;
  127. old_id = circ->n_circ_id;
  128. old_conn = circ->n_conn;
  129. circ->n_circ_id = id;
  130. circ->n_conn = conn;
  131. if (id == old_id && conn == old_conn)
  132. return;
  133. circuit_set_circid_orconn_helper(circ, id, conn, old_id, old_conn);
  134. }
  135. /** Change the state of <b>circ</b> to <b>state</b>, adding it to or removing
  136. * it from lists as appropriate. */
  137. void
  138. circuit_set_state(circuit_t *circ, int state)
  139. {
  140. tor_assert(circ);
  141. if (state == circ->state)
  142. return;
  143. if (!circuits_pending_or_conns)
  144. circuits_pending_or_conns = smartlist_create();
  145. if (circ->state == CIRCUIT_STATE_OR_WAIT) {
  146. /* remove from waiting-circuit list. */
  147. smartlist_remove(circuits_pending_or_conns, circ);
  148. }
  149. if (state == CIRCUIT_STATE_OR_WAIT) {
  150. /* add to waiting-circuit list. */
  151. smartlist_add(circuits_pending_or_conns, circ);
  152. }
  153. circ->state = state;
  154. }
  155. /** Add <b>circ</b> to the global list of circuits. This is called only from
  156. * within circuit_new.
  157. */
  158. static void
  159. circuit_add(circuit_t *circ)
  160. {
  161. if (!global_circuitlist) { /* first one */
  162. global_circuitlist = circ;
  163. circ->next = NULL;
  164. } else {
  165. circ->next = global_circuitlist;
  166. global_circuitlist = circ;
  167. }
  168. }
  169. /** Append to <b>out</b> the number of circuits in state OR_WAIT, waiting for
  170. * the given connection. */
  171. void
  172. circuit_get_all_pending_on_or_conn(smartlist_t *out, or_connection_t *or_conn)
  173. {
  174. tor_assert(out);
  175. tor_assert(or_conn);
  176. if (!circuits_pending_or_conns)
  177. return;
  178. SMARTLIST_FOREACH(circuits_pending_or_conns, circuit_t *, circ,
  179. {
  180. if (circ->marked_for_close)
  181. continue;
  182. tor_assert(circ->state == CIRCUIT_STATE_OR_WAIT);
  183. if (!circ->n_conn &&
  184. !memcmp(or_conn->identity_digest, circ->n_conn_id_digest,
  185. DIGEST_LEN)) {
  186. smartlist_add(out, circ);
  187. }
  188. });
  189. }
  190. /** Return the number of circuits in state OR_WAIT, waiting for the given
  191. * connection. */
  192. int
  193. circuit_count_pending_on_or_conn(or_connection_t *or_conn)
  194. {
  195. int cnt;
  196. smartlist_t *sl = smartlist_create();
  197. circuit_get_all_pending_on_or_conn(sl, or_conn);
  198. cnt = smartlist_len(sl);
  199. smartlist_free(sl);
  200. log_debug(LD_CIRC,"or_conn to %s, %d pending circs",
  201. or_conn->nickname ? or_conn->nickname : "NULL", cnt);
  202. return cnt;
  203. }
  204. /** Detach from the global circuit list, and deallocate, all
  205. * circuits that have been marked for close.
  206. */
  207. void
  208. circuit_close_all_marked(void)
  209. {
  210. circuit_t *tmp,*m;
  211. while (global_circuitlist && global_circuitlist->marked_for_close) {
  212. tmp = global_circuitlist->next;
  213. circuit_free(global_circuitlist);
  214. global_circuitlist = tmp;
  215. }
  216. tmp = global_circuitlist;
  217. while (tmp && tmp->next) {
  218. if (tmp->next->marked_for_close) {
  219. m = tmp->next->next;
  220. circuit_free(tmp->next);
  221. tmp->next = m;
  222. /* Need to check new tmp->next; don't advance tmp. */
  223. } else {
  224. /* Advance tmp. */
  225. tmp = tmp->next;
  226. }
  227. }
  228. }
  229. /** Return the head of the global linked list of circuits. */
  230. circuit_t *
  231. _circuit_get_global_list(void)
  232. {
  233. return global_circuitlist;
  234. }
  235. /** Function to make circ-\>state human-readable */
  236. const char *
  237. circuit_state_to_string(int state)
  238. {
  239. static char buf[64];
  240. switch (state) {
  241. case CIRCUIT_STATE_BUILDING: return "doing handshakes";
  242. case CIRCUIT_STATE_ONIONSKIN_PENDING: return "processing the onion";
  243. case CIRCUIT_STATE_OR_WAIT: return "connecting to server";
  244. case CIRCUIT_STATE_OPEN: return "open";
  245. default:
  246. log_warn(LD_BUG, "Bug: unknown circuit state %d", state);
  247. tor_snprintf(buf, sizeof(buf), "unknown state [%d]", state);
  248. return buf;
  249. }
  250. }
  251. /** Initialize the common elements in a circuit_t, and add it to the global
  252. * list. */
  253. static void
  254. init_circuit_base(circuit_t *circ)
  255. {
  256. circ->timestamp_created = time(NULL);
  257. circ->package_window = CIRCWINDOW_START;
  258. circ->deliver_window = CIRCWINDOW_START;
  259. circuit_add(circ);
  260. }
  261. /** Allocate space for a new circuit, initializing with <b>p_circ_id</b>
  262. * and <b>p_conn</b>. Add it to the global circuit list.
  263. */
  264. origin_circuit_t *
  265. origin_circuit_new(void)
  266. {
  267. origin_circuit_t *circ;
  268. /* never zero, since a global ID of 0 is treated specially by the
  269. * controller */
  270. static uint32_t n_circuits_allocated = 1;
  271. circ = tor_malloc_zero(sizeof(origin_circuit_t));
  272. circ->_base.magic = ORIGIN_CIRCUIT_MAGIC;
  273. circ->next_stream_id = crypto_rand_int(1<<16);
  274. circ->global_identifier = n_circuits_allocated++;
  275. init_circuit_base(TO_CIRCUIT(circ));
  276. return circ;
  277. }
  278. /** DOCDOC */
  279. or_circuit_t *
  280. or_circuit_new(uint16_t p_circ_id, or_connection_t *p_conn)
  281. {
  282. /* CircIDs */
  283. or_circuit_t *circ;
  284. circ = tor_malloc_zero(sizeof(or_circuit_t));
  285. circ->_base.magic = OR_CIRCUIT_MAGIC;
  286. if (p_conn)
  287. circuit_set_p_circid_orconn(circ, p_circ_id, p_conn);
  288. init_circuit_base(TO_CIRCUIT(circ));
  289. return circ;
  290. }
  291. /** Deallocate space associated with circ.
  292. */
  293. static void
  294. circuit_free(circuit_t *circ)
  295. {
  296. void *mem;
  297. tor_assert(circ);
  298. if (CIRCUIT_IS_ORIGIN(circ)) {
  299. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  300. mem = ocirc;
  301. tor_assert(circ->magic == ORIGIN_CIRCUIT_MAGIC);
  302. if (ocirc->build_state) {
  303. if (ocirc->build_state->chosen_exit)
  304. extend_info_free(ocirc->build_state->chosen_exit);
  305. if (ocirc->build_state->pending_final_cpath)
  306. circuit_free_cpath_node(ocirc->build_state->pending_final_cpath);
  307. }
  308. tor_free(ocirc->build_state);
  309. circuit_free_cpath(ocirc->cpath);
  310. } else {
  311. or_circuit_t *ocirc = TO_OR_CIRCUIT(circ);
  312. mem = ocirc;
  313. tor_assert(circ->magic == OR_CIRCUIT_MAGIC);
  314. if (ocirc->p_crypto)
  315. crypto_free_cipher_env(ocirc->p_crypto);
  316. if (ocirc->p_digest)
  317. crypto_free_digest_env(ocirc->p_digest);
  318. if (ocirc->n_crypto)
  319. crypto_free_cipher_env(ocirc->n_crypto);
  320. if (ocirc->n_digest)
  321. crypto_free_digest_env(ocirc->n_digest);
  322. if (ocirc->rend_splice) {
  323. or_circuit_t *other = ocirc->rend_splice;
  324. tor_assert(other->_base.magic == OR_CIRCUIT_MAGIC);
  325. other->rend_splice = NULL;
  326. }
  327. tor_free(circ->onionskin);
  328. /* remove from map. */
  329. circuit_set_p_circid_orconn(ocirc, 0, NULL);
  330. }
  331. /* Remove from map. */
  332. circuit_set_n_circid_orconn(circ, 0, NULL);
  333. memset(circ, 0xAA, sizeof(circuit_t)); /* poison memory */
  334. tor_free(mem);
  335. }
  336. /** Deallocate space associated with the linked list <b>cpath</b>. */
  337. static void
  338. circuit_free_cpath(crypt_path_t *cpath)
  339. {
  340. crypt_path_t *victim, *head=cpath;
  341. if (!cpath)
  342. return;
  343. /* it's a doubly linked list, so we have to notice when we've
  344. * gone through it once. */
  345. while (cpath->next && cpath->next != head) {
  346. victim = cpath;
  347. cpath = victim->next;
  348. circuit_free_cpath_node(victim);
  349. }
  350. circuit_free_cpath_node(cpath);
  351. }
  352. /** Release all storage held by circuits. */
  353. void
  354. circuit_free_all(void)
  355. {
  356. circuit_t *next;
  357. while (global_circuitlist) {
  358. next = global_circuitlist->next;
  359. if (! CIRCUIT_IS_ORIGIN(global_circuitlist)) {
  360. or_circuit_t *or_circ = TO_OR_CIRCUIT(global_circuitlist);
  361. while (or_circ->resolving_streams) {
  362. edge_connection_t *next;
  363. next = or_circ->resolving_streams->next_stream;
  364. connection_free(TO_CONN(or_circ->resolving_streams));
  365. or_circ->resolving_streams = next;
  366. }
  367. }
  368. circuit_free(global_circuitlist);
  369. global_circuitlist = next;
  370. }
  371. if (circuits_pending_or_conns) {
  372. smartlist_free(circuits_pending_or_conns);
  373. circuits_pending_or_conns = NULL;
  374. }
  375. HT_CLEAR(orconn_circid_map, &orconn_circid_circuit_map);
  376. }
  377. /** Deallocate space associated with the cpath node <b>victim</b>. */
  378. static void
  379. circuit_free_cpath_node(crypt_path_t *victim)
  380. {
  381. if (victim->f_crypto)
  382. crypto_free_cipher_env(victim->f_crypto);
  383. if (victim->b_crypto)
  384. crypto_free_cipher_env(victim->b_crypto);
  385. if (victim->f_digest)
  386. crypto_free_digest_env(victim->f_digest);
  387. if (victim->b_digest)
  388. crypto_free_digest_env(victim->b_digest);
  389. if (victim->dh_handshake_state)
  390. crypto_dh_free(victim->dh_handshake_state);
  391. if (victim->extend_info)
  392. extend_info_free(victim->extend_info);
  393. victim->magic = 0xDEADBEEFu;
  394. tor_free(victim);
  395. }
  396. /** A helper function for circuit_dump_by_conn() below. Log a bunch
  397. * of information about circuit <b>circ</b>.
  398. */
  399. static void
  400. circuit_dump_details(int severity, circuit_t *circ, int conn_array_index,
  401. const char *type, int this_circid, int other_circid)
  402. {
  403. log(severity, LD_CIRC, "Conn %d has %s circuit: circID %d (other side %d), "
  404. "state %d (%s), born %d:",
  405. conn_array_index, type, this_circid, other_circid, circ->state,
  406. circuit_state_to_string(circ->state), (int)circ->timestamp_created);
  407. if (CIRCUIT_IS_ORIGIN(circ)) { /* circ starts at this node */
  408. circuit_log_path(severity, LD_CIRC, TO_ORIGIN_CIRCUIT(circ));
  409. }
  410. }
  411. /** Log, at severity <b>severity</b>, information about each circuit
  412. * that is connected to <b>conn</b>.
  413. */
  414. void
  415. circuit_dump_by_conn(connection_t *conn, int severity)
  416. {
  417. circuit_t *circ;
  418. edge_connection_t *tmpconn;
  419. for (circ=global_circuitlist;circ;circ = circ->next) {
  420. circid_t n_circ_id = circ->n_circ_id, p_circ_id = 0;
  421. if (circ->marked_for_close)
  422. continue;
  423. if (! CIRCUIT_IS_ORIGIN(circ))
  424. p_circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  425. if (! CIRCUIT_IS_ORIGIN(circ) && TO_OR_CIRCUIT(circ)->p_conn &&
  426. TO_CONN(TO_OR_CIRCUIT(circ)->p_conn) == conn)
  427. circuit_dump_details(severity, circ, conn->conn_array_index, "App-ward",
  428. p_circ_id, n_circ_id);
  429. if (CIRCUIT_IS_ORIGIN(circ)) {
  430. for (tmpconn=TO_ORIGIN_CIRCUIT(circ)->p_streams; tmpconn;
  431. tmpconn=tmpconn->next_stream) {
  432. if (TO_CONN(tmpconn) == conn) {
  433. circuit_dump_details(severity, circ, conn->conn_array_index,
  434. "App-ward", p_circ_id, n_circ_id);
  435. }
  436. }
  437. }
  438. if (circ->n_conn && TO_CONN(circ->n_conn) == conn)
  439. circuit_dump_details(severity, circ, conn->conn_array_index, "Exit-ward",
  440. n_circ_id, p_circ_id);
  441. if (! CIRCUIT_IS_ORIGIN(circ)) {
  442. for (tmpconn=TO_OR_CIRCUIT(circ)->n_streams; tmpconn;
  443. tmpconn=tmpconn->next_stream) {
  444. if (TO_CONN(tmpconn) == conn) {
  445. circuit_dump_details(severity, circ, conn->conn_array_index,
  446. "Exit-ward", n_circ_id, p_circ_id);
  447. }
  448. }
  449. }
  450. if (!circ->n_conn && circ->n_addr && circ->n_port &&
  451. circ->n_addr == conn->addr &&
  452. circ->n_port == conn->port &&
  453. conn->type == CONN_TYPE_OR &&
  454. !memcmp(TO_OR_CONN(conn)->identity_digest, circ->n_conn_id_digest,
  455. DIGEST_LEN)) {
  456. circuit_dump_details(severity, circ, conn->conn_array_index,
  457. (circ->state == CIRCUIT_STATE_OPEN &&
  458. !CIRCUIT_IS_ORIGIN(circ)) ?
  459. "Endpoint" : "Pending",
  460. n_circ_id, p_circ_id);
  461. }
  462. }
  463. }
  464. /** Return the circuit whose global ID is <b>id</b>, or NULL if no
  465. * such circuit exists. */
  466. origin_circuit_t *
  467. circuit_get_by_global_id(uint32_t id)
  468. {
  469. circuit_t *circ;
  470. for (circ=global_circuitlist;circ;circ = circ->next) {
  471. if (CIRCUIT_IS_ORIGIN(circ) &&
  472. TO_ORIGIN_CIRCUIT(circ)->global_identifier == id) {
  473. if (circ->marked_for_close)
  474. return NULL;
  475. else
  476. return TO_ORIGIN_CIRCUIT(circ);
  477. }
  478. }
  479. return NULL;
  480. }
  481. /** Return a circ such that:
  482. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  483. * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
  484. * Return NULL if no such circuit exists.
  485. */
  486. static INLINE circuit_t *
  487. circuit_get_by_circid_orconn_impl(uint16_t circ_id, or_connection_t *conn)
  488. {
  489. orconn_circid_circuit_map_t search;
  490. orconn_circid_circuit_map_t *found;
  491. if (_last_circid_orconn_ent &&
  492. circ_id == _last_circid_orconn_ent->circ_id &&
  493. conn == _last_circid_orconn_ent->or_conn) {
  494. found = _last_circid_orconn_ent;
  495. } else {
  496. search.circ_id = circ_id;
  497. search.or_conn = conn;
  498. found = HT_FIND(orconn_circid_map, &orconn_circid_circuit_map, &search);
  499. _last_circid_orconn_ent = found;
  500. }
  501. if (found && found->circuit)
  502. return found->circuit;
  503. return NULL;
  504. /* The rest of this checks for bugs. Disabled by default. */
  505. {
  506. circuit_t *circ;
  507. for (circ=global_circuitlist;circ;circ = circ->next) {
  508. if (! CIRCUIT_IS_ORIGIN(circ)) {
  509. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  510. if (or_circ->p_conn == conn && or_circ->p_circ_id == circ_id) {
  511. log_warn(LD_BUG,
  512. "circuit matches p_conn, but not in hash table (Bug!)");
  513. return circ;
  514. }
  515. }
  516. if (circ->n_conn == conn && circ->n_circ_id == circ_id) {
  517. log_warn(LD_BUG,
  518. "circuit matches n_conn, but not in hash table (Bug!)");
  519. return circ;
  520. }
  521. }
  522. return NULL;
  523. }
  524. }
  525. /** Return a circ such that:
  526. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  527. * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
  528. * - circ is not marked for close.
  529. * Return NULL if no such circuit exists.
  530. */
  531. circuit_t *
  532. circuit_get_by_circid_orconn(uint16_t circ_id, or_connection_t *conn)
  533. {
  534. circuit_t *circ = circuit_get_by_circid_orconn_impl(circ_id, conn);
  535. if (!circ || circ->marked_for_close)
  536. return NULL;
  537. else
  538. return circ;
  539. }
  540. /** Return the circuit that a given edge connection is using. */
  541. circuit_t *
  542. circuit_get_by_edge_conn(edge_connection_t *conn)
  543. {
  544. circuit_t *circ;
  545. circ = conn->on_circuit;
  546. tor_assert(!circ ||
  547. (CIRCUIT_IS_ORIGIN(circ) ? circ->magic == ORIGIN_CIRCUIT_MAGIC
  548. : circ->magic == OR_CIRCUIT_MAGIC));
  549. return circ;
  550. }
  551. /** For each circuit that has <b>conn</b> as n_conn or p_conn, unlink the
  552. * circuit from the orconn,circid map, and mark it for close if it hasn't
  553. * been marked already.
  554. */
  555. void
  556. circuit_unlink_all_from_or_conn(or_connection_t *conn, int reason)
  557. {
  558. circuit_t *circ;
  559. for (circ = global_circuitlist; circ; circ = circ->next) {
  560. int mark = 0;
  561. if (circ->n_conn == conn) {
  562. circuit_set_n_circid_orconn(circ, 0, NULL);
  563. mark = 1;
  564. }
  565. if (! CIRCUIT_IS_ORIGIN(circ)) {
  566. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  567. if (or_circ->p_conn == conn) {
  568. circuit_set_p_circid_orconn(or_circ, 0, NULL);
  569. mark = 1;
  570. }
  571. }
  572. if (mark && !circ->marked_for_close)
  573. circuit_mark_for_close(circ, reason);
  574. }
  575. }
  576. /** Return a circ such that:
  577. * - circ-\>rend_query is equal to <b>rend_query</b>, and
  578. * - circ-\>purpose is equal to <b>purpose</b>.
  579. *
  580. * Return NULL if no such circuit exists.
  581. */
  582. origin_circuit_t *
  583. circuit_get_by_rend_query_and_purpose(const char *rend_query, uint8_t purpose)
  584. {
  585. circuit_t *circ;
  586. tor_assert(CIRCUIT_PURPOSE_IS_ORIGIN(purpose));
  587. for (circ = global_circuitlist; circ; circ = circ->next) {
  588. if (!circ->marked_for_close &&
  589. circ->purpose == purpose &&
  590. !rend_cmp_service_ids(rend_query, TO_ORIGIN_CIRCUIT(circ)->rend_query))
  591. return TO_ORIGIN_CIRCUIT(circ);
  592. }
  593. return NULL;
  594. }
  595. /** Return the first circuit originating here in global_circuitlist after
  596. * <b>start</b> whose purpose is <b>purpose</b>, and where
  597. * <b>digest</b> (if set) matches the rend_pk_digest field. Return NULL if no
  598. * circuit is found. If <b>start</b> is NULL, begin at the start of the list.
  599. */
  600. origin_circuit_t *
  601. circuit_get_next_by_pk_and_purpose(origin_circuit_t *start,
  602. const char *digest, uint8_t purpose)
  603. {
  604. circuit_t *circ;
  605. tor_assert(CIRCUIT_PURPOSE_IS_ORIGIN(purpose));
  606. if (start == NULL)
  607. circ = global_circuitlist;
  608. else
  609. circ = TO_CIRCUIT(start)->next;
  610. for ( ; circ; circ = circ->next) {
  611. if (circ->marked_for_close)
  612. continue;
  613. if (circ->purpose != purpose)
  614. continue;
  615. if (!digest)
  616. return TO_ORIGIN_CIRCUIT(circ);
  617. else if (!memcmp(TO_ORIGIN_CIRCUIT(circ)->rend_pk_digest,
  618. digest, DIGEST_LEN))
  619. return TO_ORIGIN_CIRCUIT(circ);
  620. }
  621. return NULL;
  622. }
  623. /** Return the first OR circuit in the global list whose purpose is
  624. * <b>purpose</b>, and whose rend_token is the <b>len</b>-byte
  625. * <b>token</b>. */
  626. static or_circuit_t *
  627. circuit_get_by_rend_token_and_purpose(uint8_t purpose, const char *token,
  628. size_t len)
  629. {
  630. circuit_t *circ;
  631. for (circ = global_circuitlist; circ; circ = circ->next) {
  632. if (! circ->marked_for_close &&
  633. circ->purpose == purpose &&
  634. ! memcmp(TO_OR_CIRCUIT(circ)->rend_token, token, len))
  635. return TO_OR_CIRCUIT(circ);
  636. }
  637. return NULL;
  638. }
  639. /** Return the circuit waiting for a rendezvous with the provided cookie.
  640. * Return NULL if no such circuit is found.
  641. */
  642. or_circuit_t *
  643. circuit_get_rendezvous(const char *cookie)
  644. {
  645. return circuit_get_by_rend_token_and_purpose(
  646. CIRCUIT_PURPOSE_REND_POINT_WAITING,
  647. cookie, REND_COOKIE_LEN);
  648. }
  649. /** Return the circuit waiting for intro cells of the given digest.
  650. * Return NULL if no such circuit is found.
  651. */
  652. or_circuit_t *
  653. circuit_get_intro_point(const char *digest)
  654. {
  655. return circuit_get_by_rend_token_and_purpose(
  656. CIRCUIT_PURPOSE_INTRO_POINT, digest,
  657. DIGEST_LEN);
  658. }
  659. /** Return a circuit that is open, has specified <b>purpose</b>,
  660. * has a timestamp_dirty value of 0, is uptime/capacity/internal
  661. * if required, and if info is defined, does not already use info
  662. * as any of its hops; or NULL if no circuit fits this description.
  663. *
  664. * Return need_uptime circuits if that is requested; and if it's not
  665. * requested, return non-uptime circuits if possible, else either.
  666. *
  667. * Only return internal circuits if that is requested.
  668. */
  669. origin_circuit_t *
  670. circuit_find_to_cannibalize(uint8_t purpose, extend_info_t *info,
  671. int need_uptime,
  672. int need_capacity, int internal)
  673. {
  674. circuit_t *_circ;
  675. origin_circuit_t *best=NULL;
  676. log_debug(LD_CIRC,
  677. "Hunting for a circ to cannibalize: purpose %d, uptime %d, "
  678. "capacity %d, internal %d",
  679. purpose, need_uptime, need_capacity, internal);
  680. for (_circ=global_circuitlist; _circ; _circ = _circ->next) {
  681. if (CIRCUIT_IS_ORIGIN(_circ) &&
  682. _circ->state == CIRCUIT_STATE_OPEN &&
  683. !_circ->marked_for_close &&
  684. _circ->purpose == purpose &&
  685. !_circ->timestamp_dirty) {
  686. origin_circuit_t *circ = TO_ORIGIN_CIRCUIT(_circ);
  687. if ((!need_uptime || circ->build_state->need_uptime) &&
  688. (!need_capacity || circ->build_state->need_capacity) &&
  689. (internal == circ->build_state->is_internal)) {
  690. if (info) {
  691. /* need to make sure we don't duplicate hops */
  692. crypt_path_t *hop = circ->cpath;
  693. do {
  694. if (!memcmp(hop->extend_info->identity_digest,
  695. info->identity_digest, DIGEST_LEN))
  696. goto next;
  697. hop=hop->next;
  698. } while (hop!=circ->cpath);
  699. }
  700. if (!best || (best->build_state->need_uptime && !need_uptime))
  701. best = circ;
  702. next: ;
  703. }
  704. }
  705. }
  706. return best;
  707. }
  708. /** Return the number of hops in circuit's path. */
  709. int
  710. circuit_get_cpath_len(origin_circuit_t *circ)
  711. {
  712. int n = 0;
  713. if (circ && circ->cpath) {
  714. crypt_path_t *cpath, *cpath_next = NULL;
  715. for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
  716. cpath_next = cpath->next;
  717. ++n;
  718. }
  719. }
  720. return n;
  721. }
  722. /** Go through the circuitlist; mark-for-close each circuit that starts
  723. * at us but has not yet been used. */
  724. void
  725. circuit_mark_all_unused_circs(void)
  726. {
  727. circuit_t *circ;
  728. for (circ=global_circuitlist; circ; circ = circ->next) {
  729. if (CIRCUIT_IS_ORIGIN(circ) &&
  730. !circ->marked_for_close &&
  731. !circ->timestamp_dirty)
  732. circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
  733. }
  734. }
  735. /** Go through the circuitlist; for each circuit that starts at us
  736. * and is dirty, frob its timestamp_dirty so we won't use it for any
  737. * new streams.
  738. *
  739. * This is useful for letting the user change pseudonyms, so new
  740. * streams will not be linkable to old streams.
  741. */
  742. void
  743. circuit_expire_all_dirty_circs(void)
  744. {
  745. circuit_t *circ;
  746. or_options_t *options = get_options();
  747. for (circ=global_circuitlist; circ; circ = circ->next) {
  748. if (CIRCUIT_IS_ORIGIN(circ) &&
  749. !circ->marked_for_close &&
  750. circ->timestamp_dirty)
  751. circ->timestamp_dirty -= options->MaxCircuitDirtiness;
  752. }
  753. }
  754. /** Mark <b>circ</b> to be closed next time we call
  755. * circuit_close_all_marked(). Do any cleanup needed:
  756. * - If state is onionskin_pending, remove circ from the onion_pending
  757. * list.
  758. * - If circ isn't open yet: call circuit_build_failed() if we're
  759. * the origin, and in either case call circuit_rep_hist_note_result()
  760. * to note stats.
  761. * - If purpose is C_INTRODUCE_ACK_WAIT, remove the intro point we
  762. * just tried from our list of intro points for that service
  763. * descriptor.
  764. * - Send appropriate destroys and edge_destroys for conns and
  765. * streams attached to circ.
  766. * - If circ->rend_splice is set (we are the midpoint of a joined
  767. * rendezvous stream), then mark the other circuit to close as well.
  768. */
  769. void
  770. _circuit_mark_for_close(circuit_t *circ, int reason, int line,
  771. const char *file)
  772. {
  773. int orig_reason = reason; /* Passed to the controller */
  774. assert_circuit_ok(circ);
  775. tor_assert(line);
  776. tor_assert(file);
  777. if (circ->marked_for_close) {
  778. log(LOG_WARN,LD_BUG,
  779. "Duplicate call to circuit_mark_for_close at %s:%d"
  780. " (first at %s:%d)", file, line,
  781. circ->marked_for_close_file, circ->marked_for_close);
  782. return;
  783. }
  784. if (reason == END_CIRC_AT_ORIGIN) {
  785. if (!CIRCUIT_IS_ORIGIN(circ)) {
  786. log_warn(LD_BUG, "Specified 'at-origin' non-reason for ending circuit, "
  787. "but circuit was not at origin. (called %s:%d, purpose=%d)",
  788. file, line, circ->purpose);
  789. }
  790. reason = END_CIRC_REASON_NONE;
  791. } else if (CIRCUIT_IS_ORIGIN(circ) && reason < _END_CIRC_REASON_MIN) {
  792. /* We don't send reasons when closing circuits at the origin, but we want
  793. * to track them anyway so we can give them to the controller. */
  794. reason = END_CIRC_REASON_NONE;
  795. }
  796. if (reason & END_CIRC_REASON_FLAG_REMOTE)
  797. reason &= ~END_CIRC_REASON_FLAG_REMOTE;
  798. if (reason < _END_CIRC_REASON_MIN || reason > _END_CIRC_REASON_MAX) {
  799. if (!(orig_reason & END_CIRC_REASON_FLAG_REMOTE))
  800. log_warn(LD_BUG, "Reason %d out of range at %s:%d", reason, file, line);
  801. reason = END_CIRC_REASON_NONE;
  802. }
  803. if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  804. onion_pending_remove(TO_OR_CIRCUIT(circ));
  805. }
  806. /* If the circuit ever became OPEN, we sent it to the reputation history
  807. * module then. If it isn't OPEN, we send it there now to remember which
  808. * links worked and which didn't.
  809. */
  810. if (circ->state != CIRCUIT_STATE_OPEN) {
  811. if (CIRCUIT_IS_ORIGIN(circ)) {
  812. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  813. circuit_build_failed(ocirc); /* take actions if necessary */
  814. circuit_rep_hist_note_result(ocirc);
  815. }
  816. }
  817. if (circ->state == CIRCUIT_STATE_OR_WAIT) {
  818. if (circuits_pending_or_conns)
  819. smartlist_remove(circuits_pending_or_conns, circ);
  820. }
  821. if (CIRCUIT_IS_ORIGIN(circ)) {
  822. control_event_circuit_status(TO_ORIGIN_CIRCUIT(circ),
  823. (circ->state == CIRCUIT_STATE_OPEN)?CIRC_EVENT_CLOSED:CIRC_EVENT_FAILED,
  824. orig_reason);
  825. }
  826. if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
  827. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  828. tor_assert(circ->state == CIRCUIT_STATE_OPEN);
  829. tor_assert(ocirc->build_state->chosen_exit);
  830. /* treat this like getting a nack from it */
  831. log_info(LD_REND, "Failed intro circ %s to %s (awaiting ack). "
  832. "Removing from descriptor.",
  833. safe_str(ocirc->rend_query),
  834. safe_str(build_state_get_exit_nickname(ocirc->build_state)));
  835. rend_client_remove_intro_point(ocirc->build_state->chosen_exit,
  836. ocirc->rend_query);
  837. }
  838. if (circ->n_conn)
  839. connection_or_send_destroy(circ->n_circ_id, circ->n_conn, reason);
  840. if (! CIRCUIT_IS_ORIGIN(circ)) {
  841. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  842. edge_connection_t *conn;
  843. for (conn=or_circ->n_streams; conn; conn=conn->next_stream)
  844. connection_edge_destroy(or_circ->p_circ_id, conn);
  845. while (or_circ->resolving_streams) {
  846. conn = or_circ->resolving_streams;
  847. or_circ->resolving_streams = conn->next_stream;
  848. if (!conn->_base.marked_for_close) {
  849. /* The client will see a DESTROY, and infer that the connections
  850. * are closing because the circuit is getting torn down. No need
  851. * to send an end cell. */
  852. conn->_base.edge_has_sent_end = 1;
  853. conn->end_reason = END_STREAM_REASON_DESTROY;
  854. conn->end_reason |= END_STREAM_REASON_FLAG_ALREADY_SENT_CLOSED;
  855. connection_mark_for_close(TO_CONN(conn));
  856. }
  857. conn->on_circuit = NULL;
  858. }
  859. if (or_circ->p_conn)
  860. connection_or_send_destroy(or_circ->p_circ_id, or_circ->p_conn, reason);
  861. } else {
  862. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  863. edge_connection_t *conn;
  864. for (conn=ocirc->p_streams; conn; conn=conn->next_stream)
  865. connection_edge_destroy(circ->n_circ_id, conn);
  866. }
  867. circ->marked_for_close = line;
  868. circ->marked_for_close_file = file;
  869. if (! CIRCUIT_IS_ORIGIN(circ)) {
  870. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  871. if (or_circ->rend_splice) {
  872. if (!or_circ->rend_splice->_base.marked_for_close) {
  873. /* do this after marking this circuit, to avoid infinite recursion. */
  874. circuit_mark_for_close(TO_CIRCUIT(or_circ->rend_splice), reason);
  875. }
  876. or_circ->rend_splice = NULL;
  877. }
  878. }
  879. }
  880. /** Verify that cpath layer <b>cp</b> has all of its invariants
  881. * correct. Trigger an assert if anything is invalid.
  882. */
  883. void
  884. assert_cpath_layer_ok(const crypt_path_t *cp)
  885. {
  886. // tor_assert(cp->addr); /* these are zero for rendezvous extra-hops */
  887. // tor_assert(cp->port);
  888. tor_assert(cp);
  889. tor_assert(cp->magic == CRYPT_PATH_MAGIC);
  890. switch (cp->state)
  891. {
  892. case CPATH_STATE_OPEN:
  893. tor_assert(cp->f_crypto);
  894. tor_assert(cp->b_crypto);
  895. /* fall through */
  896. case CPATH_STATE_CLOSED:
  897. tor_assert(!cp->dh_handshake_state);
  898. break;
  899. case CPATH_STATE_AWAITING_KEYS:
  900. /* tor_assert(cp->dh_handshake_state); */
  901. break;
  902. default:
  903. log_fn(LOG_ERR, LD_BUG, "Unexpected state %d", cp->state);
  904. tor_assert(0);
  905. }
  906. tor_assert(cp->package_window >= 0);
  907. tor_assert(cp->deliver_window >= 0);
  908. }
  909. /** Verify that cpath <b>cp</b> has all of its invariants
  910. * correct. Trigger an assert if anything is invalid.
  911. */
  912. static void
  913. assert_cpath_ok(const crypt_path_t *cp)
  914. {
  915. const crypt_path_t *start = cp;
  916. do {
  917. assert_cpath_layer_ok(cp);
  918. /* layers must be in sequence of: "open* awaiting? closed*" */
  919. if (cp != start) {
  920. if (cp->state == CPATH_STATE_AWAITING_KEYS) {
  921. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  922. } else if (cp->state == CPATH_STATE_OPEN) {
  923. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  924. }
  925. }
  926. cp = cp->next;
  927. tor_assert(cp);
  928. } while (cp != start);
  929. }
  930. /** Verify that circuit <b>c</b> has all of its invariants
  931. * correct. Trigger an assert if anything is invalid.
  932. */
  933. void
  934. assert_circuit_ok(const circuit_t *c)
  935. {
  936. edge_connection_t *conn;
  937. const or_circuit_t *or_circ = NULL;
  938. const origin_circuit_t *origin_circ = NULL;
  939. tor_assert(c);
  940. tor_assert(c->magic == ORIGIN_CIRCUIT_MAGIC || c->magic == OR_CIRCUIT_MAGIC);
  941. tor_assert(c->purpose >= _CIRCUIT_PURPOSE_MIN &&
  942. c->purpose <= _CIRCUIT_PURPOSE_MAX);
  943. if (CIRCUIT_IS_ORIGIN(c))
  944. origin_circ = TO_ORIGIN_CIRCUIT((circuit_t*)c);
  945. else
  946. or_circ = TO_OR_CIRCUIT((circuit_t*)c);
  947. if (c->n_conn) {
  948. tor_assert(!memcmp(c->n_conn->identity_digest, c->n_conn_id_digest,
  949. DIGEST_LEN));
  950. if (c->n_circ_id)
  951. tor_assert(c == circuit_get_by_circid_orconn(c->n_circ_id, c->n_conn));
  952. }
  953. if (or_circ && or_circ->p_conn) {
  954. if (or_circ->p_circ_id)
  955. tor_assert(c == circuit_get_by_circid_orconn(or_circ->p_circ_id,
  956. or_circ->p_conn));
  957. }
  958. #if 0 /* false now that rendezvous exits are attached to p_streams */
  959. if (origin_circ)
  960. for (conn = origin_circ->p_streams; conn; conn = conn->next_stream)
  961. tor_assert(conn->_base.type == CONN_TYPE_AP);
  962. #endif
  963. if (or_circ)
  964. for (conn = or_circ->n_streams; conn; conn = conn->next_stream)
  965. tor_assert(conn->_base.type == CONN_TYPE_EXIT);
  966. tor_assert(c->deliver_window >= 0);
  967. tor_assert(c->package_window >= 0);
  968. if (c->state == CIRCUIT_STATE_OPEN) {
  969. tor_assert(!c->onionskin);
  970. if (or_circ) {
  971. tor_assert(or_circ->n_crypto);
  972. tor_assert(or_circ->p_crypto);
  973. tor_assert(or_circ->n_digest);
  974. tor_assert(or_circ->p_digest);
  975. }
  976. }
  977. if (c->state == CIRCUIT_STATE_OR_WAIT && !c->marked_for_close) {
  978. tor_assert(circuits_pending_or_conns &&
  979. smartlist_isin(circuits_pending_or_conns, c));
  980. } else {
  981. tor_assert(!circuits_pending_or_conns ||
  982. !smartlist_isin(circuits_pending_or_conns, c));
  983. }
  984. if (origin_circ && origin_circ->cpath) {
  985. assert_cpath_ok(origin_circ->cpath);
  986. }
  987. if (c->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED) {
  988. tor_assert(or_circ);
  989. if (!c->marked_for_close) {
  990. tor_assert(or_circ->rend_splice);
  991. tor_assert(or_circ->rend_splice->rend_splice == or_circ);
  992. }
  993. tor_assert(or_circ->rend_splice != or_circ);
  994. } else {
  995. tor_assert(!or_circ || !or_circ->rend_splice);
  996. }
  997. }