circuitlist.c 44 KB

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