circuitlist.c 40 KB

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