circuitlist.c 41 KB

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