circuitlist.c 32 KB

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