circuitlist.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496
  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-2012, 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 "nodelist.h"
  21. #include "onion.h"
  22. #include "relay.h"
  23. #include "rendclient.h"
  24. #include "rendcommon.h"
  25. #include "rephist.h"
  26. #include "routerlist.h"
  27. #include "routerset.h"
  28. #include "ht.h"
  29. /********* START VARIABLES **********/
  30. /** A global list of all circuits at this hop. */
  31. circuit_t *global_circuitlist=NULL;
  32. /** A list of all the circuits in CIRCUIT_STATE_OR_WAIT. */
  33. static smartlist_t *circuits_pending_or_conns=NULL;
  34. static void circuit_free(circuit_t *circ);
  35. static void circuit_free_cpath(crypt_path_t *cpath);
  36. static void circuit_free_cpath_node(crypt_path_t *victim);
  37. static void cpath_ref_decref(crypt_path_reference_t *cpath_ref);
  38. /********* END VARIABLES ************/
  39. /** A map from OR connection and circuit ID to circuit. (Lookup performance is
  40. * very important here, since we need to do it every time a cell arrives.) */
  41. typedef struct orconn_circid_circuit_map_t {
  42. HT_ENTRY(orconn_circid_circuit_map_t) node;
  43. or_connection_t *or_conn;
  44. circid_t circ_id;
  45. circuit_t *circuit;
  46. } orconn_circid_circuit_map_t;
  47. /** Helper for hash tables: compare the OR connection and circuit ID for a and
  48. * b, and return less than, equal to, or greater than zero appropriately.
  49. */
  50. static INLINE int
  51. _orconn_circid_entries_eq(orconn_circid_circuit_map_t *a,
  52. orconn_circid_circuit_map_t *b)
  53. {
  54. return a->or_conn == b->or_conn && a->circ_id == b->circ_id;
  55. }
  56. /** Helper: return a hash based on circuit ID and the pointer value of
  57. * or_conn in <b>a</b>. */
  58. static INLINE unsigned int
  59. _orconn_circid_entry_hash(orconn_circid_circuit_map_t *a)
  60. {
  61. return (((unsigned)a->circ_id)<<8) ^ (unsigned)(uintptr_t)(a->or_conn);
  62. }
  63. /** Map from [orconn,circid] to circuit. */
  64. static HT_HEAD(orconn_circid_map, orconn_circid_circuit_map_t)
  65. orconn_circid_circuit_map = HT_INITIALIZER();
  66. HT_PROTOTYPE(orconn_circid_map, orconn_circid_circuit_map_t, node,
  67. _orconn_circid_entry_hash, _orconn_circid_entries_eq)
  68. HT_GENERATE(orconn_circid_map, orconn_circid_circuit_map_t, node,
  69. _orconn_circid_entry_hash, _orconn_circid_entries_eq, 0.6,
  70. malloc, realloc, free)
  71. /** The most recently returned entry from circuit_get_by_circid_orconn;
  72. * used to improve performance when many cells arrive in a row from the
  73. * same circuit.
  74. */
  75. orconn_circid_circuit_map_t *_last_circid_orconn_ent = NULL;
  76. /** Implementation helper for circuit_set_{p,n}_circid_orconn: A circuit ID
  77. * and/or or_connection for circ has just changed from <b>old_conn, old_id</b>
  78. * to <b>conn, id</b>. Adjust the conn,circid map as appropriate, removing
  79. * the old entry (if any) and adding a new one. */
  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_new();
  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_new();
  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 string specifying the state of the hidden-service circuit
  340. * purpose <b>purpose</b>, or NULL if <b>purpose</b> is not a
  341. * hidden-service-related circuit purpose. */
  342. const char *
  343. circuit_purpose_to_controller_hs_state_string(uint8_t purpose)
  344. {
  345. switch (purpose)
  346. {
  347. default:
  348. log_fn(LOG_WARN, LD_BUG,
  349. "Unrecognized circuit purpose: %d",
  350. (int)purpose);
  351. tor_fragile_assert();
  352. /* fall through */
  353. case CIRCUIT_PURPOSE_OR:
  354. case CIRCUIT_PURPOSE_C_GENERAL:
  355. case CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT:
  356. case CIRCUIT_PURPOSE_TESTING:
  357. case CIRCUIT_PURPOSE_CONTROLLER:
  358. return NULL;
  359. case CIRCUIT_PURPOSE_INTRO_POINT:
  360. return "OR_HSSI_ESTABLISHED";
  361. case CIRCUIT_PURPOSE_REND_POINT_WAITING:
  362. return "OR_HSCR_ESTABLISHED";
  363. case CIRCUIT_PURPOSE_REND_ESTABLISHED:
  364. return "OR_HS_R_JOINED";
  365. case CIRCUIT_PURPOSE_C_INTRODUCING:
  366. return "HSCI_CONNECTING";
  367. case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
  368. return "HSCI_INTRO_SENT";
  369. case CIRCUIT_PURPOSE_C_INTRODUCE_ACKED:
  370. return "HSCI_DONE";
  371. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  372. return "HSCR_CONNECTING";
  373. case CIRCUIT_PURPOSE_C_REND_READY:
  374. return "HSCR_ESTABLISHED_IDLE";
  375. case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED:
  376. return "HSCR_ESTABLISHED_WAITING";
  377. case CIRCUIT_PURPOSE_C_REND_JOINED:
  378. return "HSCR_JOINED";
  379. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  380. return "HSSI_CONNECTING";
  381. case CIRCUIT_PURPOSE_S_INTRO:
  382. return "HSSI_ESTABLISHED";
  383. case CIRCUIT_PURPOSE_S_CONNECT_REND:
  384. return "HSSR_CONNECTING";
  385. case CIRCUIT_PURPOSE_S_REND_JOINED:
  386. return "HSSR_JOINED";
  387. }
  388. }
  389. /** Return a human-readable string for the circuit purpose <b>purpose</b>. */
  390. const char *
  391. circuit_purpose_to_string(uint8_t purpose)
  392. {
  393. static char buf[32];
  394. switch (purpose)
  395. {
  396. case CIRCUIT_PURPOSE_OR:
  397. return "Circuit at relay";
  398. case CIRCUIT_PURPOSE_INTRO_POINT:
  399. return "Acting as intro point";
  400. case CIRCUIT_PURPOSE_REND_POINT_WAITING:
  401. return "Acting as rendevous (pending)";
  402. case CIRCUIT_PURPOSE_REND_ESTABLISHED:
  403. return "Acting as rendevous (established)";
  404. case CIRCUIT_PURPOSE_C_GENERAL:
  405. return "General-purpose client";
  406. case CIRCUIT_PURPOSE_C_INTRODUCING:
  407. return "Hidden service client: Connecting to intro point";
  408. case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
  409. return "Hidden service client: Waiting for ack from intro point";
  410. case CIRCUIT_PURPOSE_C_INTRODUCE_ACKED:
  411. return "Hidden service client: Received ack from intro point";
  412. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  413. return "Hidden service client: Establishing rendezvous point";
  414. case CIRCUIT_PURPOSE_C_REND_READY:
  415. return "Hidden service client: Pending rendezvous point";
  416. case CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED:
  417. return "Hidden service client: Pending rendezvous point (ack received)";
  418. case CIRCUIT_PURPOSE_C_REND_JOINED:
  419. return "Hidden service client: Active rendezvous point";
  420. case CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT:
  421. return "Measuring circuit timeout";
  422. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  423. return "Hidden service: Establishing introduction point";
  424. case CIRCUIT_PURPOSE_S_INTRO:
  425. return "Hidden service: Introduction point";
  426. case CIRCUIT_PURPOSE_S_CONNECT_REND:
  427. return "Hidden service: Connecting to rendezvous point";
  428. case CIRCUIT_PURPOSE_S_REND_JOINED:
  429. return "Hidden service: Active rendezvous point";
  430. case CIRCUIT_PURPOSE_TESTING:
  431. return "Testing circuit";
  432. case CIRCUIT_PURPOSE_CONTROLLER:
  433. return "Circuit made by controller";
  434. default:
  435. tor_snprintf(buf, sizeof(buf), "UNKNOWN_%d", (int)purpose);
  436. return buf;
  437. }
  438. }
  439. /** Pick a reasonable package_window to start out for our circuits.
  440. * Originally this was hard-coded at 1000, but now the consensus votes
  441. * on the answer. See proposal 168. */
  442. int32_t
  443. circuit_initial_package_window(void)
  444. {
  445. int32_t num = networkstatus_get_param(NULL, "circwindow", CIRCWINDOW_START,
  446. CIRCWINDOW_START_MIN,
  447. CIRCWINDOW_START_MAX);
  448. /* If the consensus tells us a negative number, we'd assert. */
  449. if (num < 0)
  450. num = CIRCWINDOW_START;
  451. return num;
  452. }
  453. /** Initialize the common elements in a circuit_t, and add it to the global
  454. * list. */
  455. static void
  456. init_circuit_base(circuit_t *circ)
  457. {
  458. tor_gettimeofday(&circ->timestamp_created);
  459. circ->package_window = circuit_initial_package_window();
  460. circ->deliver_window = CIRCWINDOW_START;
  461. /* Initialize the cell_ewma_t structure */
  462. circ->n_cell_ewma.last_adjusted_tick = cell_ewma_get_tick();
  463. circ->n_cell_ewma.cell_count = 0.0;
  464. circ->n_cell_ewma.heap_index = -1;
  465. circ->n_cell_ewma.is_for_p_conn = 0;
  466. circuit_add(circ);
  467. }
  468. /** Allocate space for a new circuit, initializing with <b>p_circ_id</b>
  469. * and <b>p_conn</b>. Add it to the global circuit list.
  470. */
  471. origin_circuit_t *
  472. origin_circuit_new(void)
  473. {
  474. origin_circuit_t *circ;
  475. /* never zero, since a global ID of 0 is treated specially by the
  476. * controller */
  477. static uint32_t n_circuits_allocated = 1;
  478. circ = tor_malloc_zero(sizeof(origin_circuit_t));
  479. circ->_base.magic = ORIGIN_CIRCUIT_MAGIC;
  480. circ->next_stream_id = crypto_rand_int(1<<16);
  481. circ->global_identifier = n_circuits_allocated++;
  482. circ->remaining_relay_early_cells = MAX_RELAY_EARLY_CELLS_PER_CIRCUIT;
  483. circ->remaining_relay_early_cells -= crypto_rand_int(2);
  484. init_circuit_base(TO_CIRCUIT(circ));
  485. circ_times.last_circ_at = approx_time();
  486. return circ;
  487. }
  488. /** Allocate a new or_circuit_t, connected to <b>p_conn</b> as
  489. * <b>p_circ_id</b>. If <b>p_conn</b> is NULL, the circuit is unattached. */
  490. or_circuit_t *
  491. or_circuit_new(circid_t p_circ_id, or_connection_t *p_conn)
  492. {
  493. /* CircIDs */
  494. or_circuit_t *circ;
  495. circ = tor_malloc_zero(sizeof(or_circuit_t));
  496. circ->_base.magic = OR_CIRCUIT_MAGIC;
  497. if (p_conn)
  498. circuit_set_p_circid_orconn(circ, p_circ_id, p_conn);
  499. circ->remaining_relay_early_cells = MAX_RELAY_EARLY_CELLS_PER_CIRCUIT;
  500. init_circuit_base(TO_CIRCUIT(circ));
  501. /* Initialize the cell_ewma_t structure */
  502. /* Initialize the cell counts to 0 */
  503. circ->p_cell_ewma.cell_count = 0.0;
  504. circ->p_cell_ewma.last_adjusted_tick = cell_ewma_get_tick();
  505. circ->p_cell_ewma.is_for_p_conn = 1;
  506. /* It's not in any heap yet. */
  507. circ->p_cell_ewma.heap_index = -1;
  508. return circ;
  509. }
  510. /** Deallocate space associated with circ.
  511. */
  512. static void
  513. circuit_free(circuit_t *circ)
  514. {
  515. void *mem;
  516. size_t memlen;
  517. if (!circ)
  518. return;
  519. if (CIRCUIT_IS_ORIGIN(circ)) {
  520. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  521. mem = ocirc;
  522. memlen = sizeof(origin_circuit_t);
  523. tor_assert(circ->magic == ORIGIN_CIRCUIT_MAGIC);
  524. if (ocirc->build_state) {
  525. extend_info_free(ocirc->build_state->chosen_exit);
  526. circuit_free_cpath_node(ocirc->build_state->pending_final_cpath);
  527. cpath_ref_decref(ocirc->build_state->service_pending_final_cpath_ref);
  528. }
  529. tor_free(ocirc->build_state);
  530. circuit_free_cpath(ocirc->cpath);
  531. crypto_pk_free(ocirc->intro_key);
  532. rend_data_free(ocirc->rend_data);
  533. tor_free(ocirc->dest_address);
  534. if (ocirc->socks_username) {
  535. memset(ocirc->socks_username, 0x12, ocirc->socks_username_len);
  536. tor_free(ocirc->socks_username);
  537. }
  538. if (ocirc->socks_password) {
  539. memset(ocirc->socks_password, 0x06, ocirc->socks_password_len);
  540. tor_free(ocirc->socks_password);
  541. }
  542. } else {
  543. or_circuit_t *ocirc = TO_OR_CIRCUIT(circ);
  544. /* Remember cell statistics for this circuit before deallocating. */
  545. if (get_options()->CellStatistics)
  546. rep_hist_buffer_stats_add_circ(circ, time(NULL));
  547. mem = ocirc;
  548. memlen = sizeof(or_circuit_t);
  549. tor_assert(circ->magic == OR_CIRCUIT_MAGIC);
  550. crypto_cipher_free(ocirc->p_crypto);
  551. crypto_digest_free(ocirc->p_digest);
  552. crypto_cipher_free(ocirc->n_crypto);
  553. crypto_digest_free(ocirc->n_digest);
  554. if (ocirc->rend_splice) {
  555. or_circuit_t *other = ocirc->rend_splice;
  556. tor_assert(other->_base.magic == OR_CIRCUIT_MAGIC);
  557. other->rend_splice = NULL;
  558. }
  559. /* remove from map. */
  560. circuit_set_p_circid_orconn(ocirc, 0, NULL);
  561. /* Clear cell queue _after_ removing it from the map. Otherwise our
  562. * "active" checks will be violated. */
  563. cell_queue_clear(&ocirc->p_conn_cells);
  564. }
  565. extend_info_free(circ->n_hop);
  566. tor_free(circ->n_conn_onionskin);
  567. /* Remove from map. */
  568. circuit_set_n_circid_orconn(circ, 0, NULL);
  569. /* Clear cell queue _after_ removing it from the map. Otherwise our
  570. * "active" checks will be violated. */
  571. cell_queue_clear(&circ->n_conn_cells);
  572. memset(mem, 0xAA, memlen); /* poison memory */
  573. tor_free(mem);
  574. }
  575. /** Deallocate space associated with the linked list <b>cpath</b>. */
  576. static void
  577. circuit_free_cpath(crypt_path_t *cpath)
  578. {
  579. crypt_path_t *victim, *head=cpath;
  580. if (!cpath)
  581. return;
  582. /* it's a doubly linked list, so we have to notice when we've
  583. * gone through it once. */
  584. while (cpath->next && cpath->next != head) {
  585. victim = cpath;
  586. cpath = victim->next;
  587. circuit_free_cpath_node(victim);
  588. }
  589. circuit_free_cpath_node(cpath);
  590. }
  591. /** Release all storage held by circuits. */
  592. void
  593. circuit_free_all(void)
  594. {
  595. circuit_t *next;
  596. while (global_circuitlist) {
  597. next = global_circuitlist->next;
  598. if (! CIRCUIT_IS_ORIGIN(global_circuitlist)) {
  599. or_circuit_t *or_circ = TO_OR_CIRCUIT(global_circuitlist);
  600. while (or_circ->resolving_streams) {
  601. edge_connection_t *next_conn;
  602. next_conn = or_circ->resolving_streams->next_stream;
  603. connection_free(TO_CONN(or_circ->resolving_streams));
  604. or_circ->resolving_streams = next_conn;
  605. }
  606. }
  607. circuit_free(global_circuitlist);
  608. global_circuitlist = next;
  609. }
  610. smartlist_free(circuits_pending_or_conns);
  611. circuits_pending_or_conns = NULL;
  612. HT_CLEAR(orconn_circid_map, &orconn_circid_circuit_map);
  613. }
  614. /** Deallocate space associated with the cpath node <b>victim</b>. */
  615. static void
  616. circuit_free_cpath_node(crypt_path_t *victim)
  617. {
  618. if (!victim)
  619. return;
  620. crypto_cipher_free(victim->f_crypto);
  621. crypto_cipher_free(victim->b_crypto);
  622. crypto_digest_free(victim->f_digest);
  623. crypto_digest_free(victim->b_digest);
  624. crypto_dh_free(victim->dh_handshake_state);
  625. extend_info_free(victim->extend_info);
  626. memset(victim, 0xBB, sizeof(crypt_path_t)); /* poison memory */
  627. tor_free(victim);
  628. }
  629. /** Release a crypt_path_reference_t*, which may be NULL. */
  630. static void
  631. cpath_ref_decref(crypt_path_reference_t *cpath_ref)
  632. {
  633. if (cpath_ref != NULL) {
  634. if (--(cpath_ref->refcount) == 0) {
  635. circuit_free_cpath_node(cpath_ref->cpath);
  636. tor_free(cpath_ref);
  637. }
  638. }
  639. }
  640. /** A helper function for circuit_dump_by_conn() below. Log a bunch
  641. * of information about circuit <b>circ</b>.
  642. */
  643. static void
  644. circuit_dump_details(int severity, circuit_t *circ, int conn_array_index,
  645. const char *type, int this_circid, int other_circid)
  646. {
  647. log(severity, LD_CIRC, "Conn %d has %s circuit: circID %d (other side %d), "
  648. "state %d (%s), born %ld:",
  649. conn_array_index, type, this_circid, other_circid, circ->state,
  650. circuit_state_to_string(circ->state),
  651. (long)circ->timestamp_created.tv_sec);
  652. if (CIRCUIT_IS_ORIGIN(circ)) { /* circ starts at this node */
  653. circuit_log_path(severity, LD_CIRC, TO_ORIGIN_CIRCUIT(circ));
  654. }
  655. }
  656. /** Log, at severity <b>severity</b>, information about each circuit
  657. * that is connected to <b>conn</b>.
  658. */
  659. void
  660. circuit_dump_by_conn(connection_t *conn, int severity)
  661. {
  662. circuit_t *circ;
  663. edge_connection_t *tmpconn;
  664. for (circ=global_circuitlist;circ;circ = circ->next) {
  665. circid_t n_circ_id = circ->n_circ_id, p_circ_id = 0;
  666. if (circ->marked_for_close)
  667. continue;
  668. if (! CIRCUIT_IS_ORIGIN(circ))
  669. p_circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  670. if (! CIRCUIT_IS_ORIGIN(circ) && TO_OR_CIRCUIT(circ)->p_conn &&
  671. TO_CONN(TO_OR_CIRCUIT(circ)->p_conn) == conn)
  672. circuit_dump_details(severity, circ, conn->conn_array_index, "App-ward",
  673. p_circ_id, n_circ_id);
  674. if (CIRCUIT_IS_ORIGIN(circ)) {
  675. for (tmpconn=TO_ORIGIN_CIRCUIT(circ)->p_streams; tmpconn;
  676. tmpconn=tmpconn->next_stream) {
  677. if (TO_CONN(tmpconn) == conn) {
  678. circuit_dump_details(severity, circ, conn->conn_array_index,
  679. "App-ward", p_circ_id, n_circ_id);
  680. }
  681. }
  682. }
  683. if (circ->n_conn && TO_CONN(circ->n_conn) == conn)
  684. circuit_dump_details(severity, circ, conn->conn_array_index, "Exit-ward",
  685. n_circ_id, p_circ_id);
  686. if (! CIRCUIT_IS_ORIGIN(circ)) {
  687. for (tmpconn=TO_OR_CIRCUIT(circ)->n_streams; tmpconn;
  688. tmpconn=tmpconn->next_stream) {
  689. if (TO_CONN(tmpconn) == conn) {
  690. circuit_dump_details(severity, circ, conn->conn_array_index,
  691. "Exit-ward", n_circ_id, p_circ_id);
  692. }
  693. }
  694. }
  695. if (!circ->n_conn && circ->n_hop &&
  696. tor_addr_eq(&circ->n_hop->addr, &conn->addr) &&
  697. circ->n_hop->port == conn->port &&
  698. conn->type == CONN_TYPE_OR &&
  699. tor_memeq(TO_OR_CONN(conn)->identity_digest,
  700. circ->n_hop->identity_digest, DIGEST_LEN)) {
  701. circuit_dump_details(severity, circ, conn->conn_array_index,
  702. (circ->state == CIRCUIT_STATE_OPEN &&
  703. !CIRCUIT_IS_ORIGIN(circ)) ?
  704. "Endpoint" : "Pending",
  705. n_circ_id, p_circ_id);
  706. }
  707. }
  708. }
  709. /** Return the circuit whose global ID is <b>id</b>, or NULL if no
  710. * such circuit exists. */
  711. origin_circuit_t *
  712. circuit_get_by_global_id(uint32_t id)
  713. {
  714. circuit_t *circ;
  715. for (circ=global_circuitlist;circ;circ = circ->next) {
  716. if (CIRCUIT_IS_ORIGIN(circ) &&
  717. TO_ORIGIN_CIRCUIT(circ)->global_identifier == id) {
  718. if (circ->marked_for_close)
  719. return NULL;
  720. else
  721. return TO_ORIGIN_CIRCUIT(circ);
  722. }
  723. }
  724. return NULL;
  725. }
  726. /** Return a circ such that:
  727. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  728. * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
  729. * Return NULL if no such circuit exists.
  730. */
  731. static INLINE circuit_t *
  732. circuit_get_by_circid_orconn_impl(circid_t circ_id, or_connection_t *conn)
  733. {
  734. orconn_circid_circuit_map_t search;
  735. orconn_circid_circuit_map_t *found;
  736. if (_last_circid_orconn_ent &&
  737. circ_id == _last_circid_orconn_ent->circ_id &&
  738. conn == _last_circid_orconn_ent->or_conn) {
  739. found = _last_circid_orconn_ent;
  740. } else {
  741. search.circ_id = circ_id;
  742. search.or_conn = conn;
  743. found = HT_FIND(orconn_circid_map, &orconn_circid_circuit_map, &search);
  744. _last_circid_orconn_ent = found;
  745. }
  746. if (found && found->circuit)
  747. return found->circuit;
  748. return NULL;
  749. /* The rest of this checks for bugs. Disabled by default. */
  750. /* We comment it out because coverity complains otherwise.
  751. {
  752. circuit_t *circ;
  753. for (circ=global_circuitlist;circ;circ = circ->next) {
  754. if (! CIRCUIT_IS_ORIGIN(circ)) {
  755. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  756. if (or_circ->p_conn == conn && or_circ->p_circ_id == circ_id) {
  757. log_warn(LD_BUG,
  758. "circuit matches p_conn, but not in hash table (Bug!)");
  759. return circ;
  760. }
  761. }
  762. if (circ->n_conn == conn && circ->n_circ_id == circ_id) {
  763. log_warn(LD_BUG,
  764. "circuit matches n_conn, but not in hash table (Bug!)");
  765. return circ;
  766. }
  767. }
  768. return NULL;
  769. } */
  770. }
  771. /** Return a circ such that:
  772. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  773. * - circ is attached to <b>conn</b>, either as p_conn or n_conn.
  774. * - circ is not marked for close.
  775. * Return NULL if no such circuit exists.
  776. */
  777. circuit_t *
  778. circuit_get_by_circid_orconn(circid_t circ_id, or_connection_t *conn)
  779. {
  780. circuit_t *circ = circuit_get_by_circid_orconn_impl(circ_id, conn);
  781. if (!circ || circ->marked_for_close)
  782. return NULL;
  783. else
  784. return circ;
  785. }
  786. /** Return true iff the circuit ID <b>circ_id</b> is currently used by a
  787. * circuit, marked or not, on <b>conn</b>. */
  788. int
  789. circuit_id_in_use_on_orconn(circid_t circ_id, or_connection_t *conn)
  790. {
  791. return circuit_get_by_circid_orconn_impl(circ_id, conn) != NULL;
  792. }
  793. /** Return the circuit that a given edge connection is using. */
  794. circuit_t *
  795. circuit_get_by_edge_conn(edge_connection_t *conn)
  796. {
  797. circuit_t *circ;
  798. circ = conn->on_circuit;
  799. tor_assert(!circ ||
  800. (CIRCUIT_IS_ORIGIN(circ) ? circ->magic == ORIGIN_CIRCUIT_MAGIC
  801. : circ->magic == OR_CIRCUIT_MAGIC));
  802. return circ;
  803. }
  804. /** For each circuit that has <b>conn</b> as n_conn or p_conn, unlink the
  805. * circuit from the orconn,circid map, and mark it for close if it hasn't
  806. * been marked already.
  807. */
  808. void
  809. circuit_unlink_all_from_or_conn(or_connection_t *conn, int reason)
  810. {
  811. circuit_t *circ;
  812. connection_or_unlink_all_active_circs(conn);
  813. for (circ = global_circuitlist; circ; circ = circ->next) {
  814. int mark = 0;
  815. if (circ->n_conn == conn) {
  816. circuit_set_n_circid_orconn(circ, 0, NULL);
  817. mark = 1;
  818. }
  819. if (! CIRCUIT_IS_ORIGIN(circ)) {
  820. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  821. if (or_circ->p_conn == conn) {
  822. circuit_set_p_circid_orconn(or_circ, 0, NULL);
  823. mark = 1;
  824. }
  825. }
  826. if (mark && !circ->marked_for_close)
  827. circuit_mark_for_close(circ, reason);
  828. }
  829. }
  830. /** Return a circ such that
  831. * - circ-\>rend_data-\>onion_address is equal to
  832. * <b>rend_data</b>-\>onion_address,
  833. * - circ-\>rend_data-\>rend_cookie is equal to
  834. * <b>rend_data</b>-\>rend_cookie, and
  835. * - circ-\>purpose is equal to CIRCUIT_PURPOSE_C_REND_READY.
  836. *
  837. * Return NULL if no such circuit exists.
  838. */
  839. origin_circuit_t *
  840. circuit_get_ready_rend_circ_by_rend_data(const rend_data_t *rend_data)
  841. {
  842. circuit_t *circ;
  843. for (circ = global_circuitlist; circ; circ = circ->next) {
  844. if (!circ->marked_for_close &&
  845. circ->purpose == CIRCUIT_PURPOSE_C_REND_READY) {
  846. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  847. if (ocirc->rend_data &&
  848. !rend_cmp_service_ids(rend_data->onion_address,
  849. ocirc->rend_data->onion_address) &&
  850. tor_memeq(ocirc->rend_data->rend_cookie,
  851. rend_data->rend_cookie,
  852. REND_COOKIE_LEN))
  853. return ocirc;
  854. }
  855. }
  856. return NULL;
  857. }
  858. /** Return the first circuit originating here in global_circuitlist after
  859. * <b>start</b> whose purpose is <b>purpose</b>, and where
  860. * <b>digest</b> (if set) matches the rend_pk_digest field. Return NULL if no
  861. * circuit is found. If <b>start</b> is NULL, begin at the start of the list.
  862. */
  863. origin_circuit_t *
  864. circuit_get_next_by_pk_and_purpose(origin_circuit_t *start,
  865. const char *digest, uint8_t purpose)
  866. {
  867. circuit_t *circ;
  868. tor_assert(CIRCUIT_PURPOSE_IS_ORIGIN(purpose));
  869. if (start == NULL)
  870. circ = global_circuitlist;
  871. else
  872. circ = TO_CIRCUIT(start)->next;
  873. for ( ; circ; circ = circ->next) {
  874. if (circ->marked_for_close)
  875. continue;
  876. if (circ->purpose != purpose)
  877. continue;
  878. if (!digest)
  879. return TO_ORIGIN_CIRCUIT(circ);
  880. else if (TO_ORIGIN_CIRCUIT(circ)->rend_data &&
  881. tor_memeq(TO_ORIGIN_CIRCUIT(circ)->rend_data->rend_pk_digest,
  882. digest, DIGEST_LEN))
  883. return TO_ORIGIN_CIRCUIT(circ);
  884. }
  885. return NULL;
  886. }
  887. /** Return the first OR circuit in the global list whose purpose is
  888. * <b>purpose</b>, and whose rend_token is the <b>len</b>-byte
  889. * <b>token</b>. */
  890. static or_circuit_t *
  891. circuit_get_by_rend_token_and_purpose(uint8_t purpose, const char *token,
  892. size_t len)
  893. {
  894. circuit_t *circ;
  895. for (circ = global_circuitlist; circ; circ = circ->next) {
  896. if (! circ->marked_for_close &&
  897. circ->purpose == purpose &&
  898. tor_memeq(TO_OR_CIRCUIT(circ)->rend_token, token, len))
  899. return TO_OR_CIRCUIT(circ);
  900. }
  901. return NULL;
  902. }
  903. /** Return the circuit waiting for a rendezvous with the provided cookie.
  904. * Return NULL if no such circuit is found.
  905. */
  906. or_circuit_t *
  907. circuit_get_rendezvous(const char *cookie)
  908. {
  909. return circuit_get_by_rend_token_and_purpose(
  910. CIRCUIT_PURPOSE_REND_POINT_WAITING,
  911. cookie, REND_COOKIE_LEN);
  912. }
  913. /** Return the circuit waiting for intro cells of the given digest.
  914. * Return NULL if no such circuit is found.
  915. */
  916. or_circuit_t *
  917. circuit_get_intro_point(const char *digest)
  918. {
  919. return circuit_get_by_rend_token_and_purpose(
  920. CIRCUIT_PURPOSE_INTRO_POINT, digest,
  921. DIGEST_LEN);
  922. }
  923. /** Return a circuit that is open, is CIRCUIT_PURPOSE_C_GENERAL,
  924. * has a timestamp_dirty value of 0, has flags matching the CIRCLAUNCH_*
  925. * flags in <b>flags</b>, and if info is defined, does not already use info
  926. * as any of its hops; or NULL if no circuit fits this description.
  927. *
  928. * The <b>purpose</b> argument (currently ignored) refers to the purpose of
  929. * the circuit we want to create, not the purpose of the circuit we want to
  930. * cannibalize.
  931. *
  932. * If !CIRCLAUNCH_NEED_UPTIME, prefer returning non-uptime circuits.
  933. */
  934. origin_circuit_t *
  935. circuit_find_to_cannibalize(uint8_t purpose, extend_info_t *info,
  936. int flags)
  937. {
  938. circuit_t *_circ;
  939. origin_circuit_t *best=NULL;
  940. int need_uptime = (flags & CIRCLAUNCH_NEED_UPTIME) != 0;
  941. int need_capacity = (flags & CIRCLAUNCH_NEED_CAPACITY) != 0;
  942. int internal = (flags & CIRCLAUNCH_IS_INTERNAL) != 0;
  943. const or_options_t *options = get_options();
  944. /* Make sure we're not trying to create a onehop circ by
  945. * cannibalization. */
  946. tor_assert(!(flags & CIRCLAUNCH_ONEHOP_TUNNEL));
  947. log_debug(LD_CIRC,
  948. "Hunting for a circ to cannibalize: purpose %d, uptime %d, "
  949. "capacity %d, internal %d",
  950. purpose, need_uptime, need_capacity, internal);
  951. for (_circ=global_circuitlist; _circ; _circ = _circ->next) {
  952. if (CIRCUIT_IS_ORIGIN(_circ) &&
  953. _circ->state == CIRCUIT_STATE_OPEN &&
  954. !_circ->marked_for_close &&
  955. _circ->purpose == CIRCUIT_PURPOSE_C_GENERAL &&
  956. !_circ->timestamp_dirty) {
  957. origin_circuit_t *circ = TO_ORIGIN_CIRCUIT(_circ);
  958. if ((!need_uptime || circ->build_state->need_uptime) &&
  959. (!need_capacity || circ->build_state->need_capacity) &&
  960. (internal == circ->build_state->is_internal) &&
  961. circ->remaining_relay_early_cells &&
  962. circ->build_state->desired_path_len == DEFAULT_ROUTE_LEN &&
  963. !circ->build_state->onehop_tunnel &&
  964. !circ->isolation_values_set) {
  965. if (info) {
  966. /* need to make sure we don't duplicate hops */
  967. crypt_path_t *hop = circ->cpath;
  968. const node_t *ri1 = node_get_by_id(info->identity_digest);
  969. do {
  970. const node_t *ri2;
  971. if (tor_memeq(hop->extend_info->identity_digest,
  972. info->identity_digest, DIGEST_LEN))
  973. goto next;
  974. if (ri1 &&
  975. (ri2 = node_get_by_id(hop->extend_info->identity_digest))
  976. && nodes_in_same_family(ri1, ri2))
  977. goto next;
  978. hop=hop->next;
  979. } while (hop!=circ->cpath);
  980. }
  981. if (options->ExcludeNodes) {
  982. /* Make sure no existing nodes in the circuit are excluded for
  983. * general use. (This may be possible if StrictNodes is 0, and we
  984. * thought we needed to use an otherwise excluded node for, say, a
  985. * directory operation.) */
  986. crypt_path_t *hop = circ->cpath;
  987. do {
  988. if (routerset_contains_extendinfo(options->ExcludeNodes,
  989. hop->extend_info))
  990. goto next;
  991. hop = hop->next;
  992. } while (hop != circ->cpath);
  993. }
  994. if (!best || (best->build_state->need_uptime && !need_uptime))
  995. best = circ;
  996. next: ;
  997. }
  998. }
  999. }
  1000. return best;
  1001. }
  1002. /** Return the number of hops in circuit's path. */
  1003. int
  1004. circuit_get_cpath_len(origin_circuit_t *circ)
  1005. {
  1006. int n = 0;
  1007. if (circ && circ->cpath) {
  1008. crypt_path_t *cpath, *cpath_next = NULL;
  1009. for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
  1010. cpath_next = cpath->next;
  1011. ++n;
  1012. }
  1013. }
  1014. return n;
  1015. }
  1016. /** Return the <b>hopnum</b>th hop in <b>circ</b>->cpath, or NULL if there
  1017. * aren't that many hops in the list. */
  1018. crypt_path_t *
  1019. circuit_get_cpath_hop(origin_circuit_t *circ, int hopnum)
  1020. {
  1021. if (circ && circ->cpath && hopnum > 0) {
  1022. crypt_path_t *cpath, *cpath_next = NULL;
  1023. for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
  1024. cpath_next = cpath->next;
  1025. if (--hopnum <= 0)
  1026. return cpath;
  1027. }
  1028. }
  1029. return NULL;
  1030. }
  1031. /** Go through the circuitlist; mark-for-close each circuit that starts
  1032. * at us but has not yet been used. */
  1033. void
  1034. circuit_mark_all_unused_circs(void)
  1035. {
  1036. circuit_t *circ;
  1037. for (circ=global_circuitlist; circ; circ = circ->next) {
  1038. if (CIRCUIT_IS_ORIGIN(circ) &&
  1039. !circ->marked_for_close &&
  1040. !circ->timestamp_dirty)
  1041. circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
  1042. }
  1043. }
  1044. /** Go through the circuitlist; for each circuit that starts at us
  1045. * and is dirty, frob its timestamp_dirty so we won't use it for any
  1046. * new streams.
  1047. *
  1048. * This is useful for letting the user change pseudonyms, so new
  1049. * streams will not be linkable to old streams.
  1050. */
  1051. /* XXX024 this is a bad name for what this function does */
  1052. void
  1053. circuit_expire_all_dirty_circs(void)
  1054. {
  1055. circuit_t *circ;
  1056. const or_options_t *options = get_options();
  1057. for (circ=global_circuitlist; circ; circ = circ->next) {
  1058. if (CIRCUIT_IS_ORIGIN(circ) &&
  1059. !circ->marked_for_close &&
  1060. circ->timestamp_dirty)
  1061. /* XXXX024 This is a screwed-up way to say "This is too dirty
  1062. * for new circuits. */
  1063. circ->timestamp_dirty -= options->MaxCircuitDirtiness;
  1064. }
  1065. }
  1066. /** Mark <b>circ</b> to be closed next time we call
  1067. * circuit_close_all_marked(). Do any cleanup needed:
  1068. * - If state is onionskin_pending, remove circ from the onion_pending
  1069. * list.
  1070. * - If circ isn't open yet: call circuit_build_failed() if we're
  1071. * the origin, and in either case call circuit_rep_hist_note_result()
  1072. * to note stats.
  1073. * - If purpose is C_INTRODUCE_ACK_WAIT, report the intro point
  1074. * failure we just had to the hidden service client module.
  1075. * - If purpose is C_INTRODUCING and <b>reason</b> isn't TIMEOUT,
  1076. * report to the hidden service client module that the intro point
  1077. * we just tried may be unreachable.
  1078. * - Send appropriate destroys and edge_destroys for conns and
  1079. * streams attached to circ.
  1080. * - If circ->rend_splice is set (we are the midpoint of a joined
  1081. * rendezvous stream), then mark the other circuit to close as well.
  1082. */
  1083. void
  1084. _circuit_mark_for_close(circuit_t *circ, int reason, int line,
  1085. const char *file)
  1086. {
  1087. int orig_reason = reason; /* Passed to the controller */
  1088. assert_circuit_ok(circ);
  1089. tor_assert(line);
  1090. tor_assert(file);
  1091. if (circ->marked_for_close) {
  1092. log(LOG_WARN,LD_BUG,
  1093. "Duplicate call to circuit_mark_for_close at %s:%d"
  1094. " (first at %s:%d)", file, line,
  1095. circ->marked_for_close_file, circ->marked_for_close);
  1096. return;
  1097. }
  1098. if (reason == END_CIRC_AT_ORIGIN) {
  1099. if (!CIRCUIT_IS_ORIGIN(circ)) {
  1100. log_warn(LD_BUG, "Specified 'at-origin' non-reason for ending circuit, "
  1101. "but circuit was not at origin. (called %s:%d, purpose=%d)",
  1102. file, line, circ->purpose);
  1103. }
  1104. reason = END_CIRC_REASON_NONE;
  1105. }
  1106. if (CIRCUIT_IS_ORIGIN(circ)) {
  1107. /* We don't send reasons when closing circuits at the origin. */
  1108. reason = END_CIRC_REASON_NONE;
  1109. }
  1110. if (reason & END_CIRC_REASON_FLAG_REMOTE)
  1111. reason &= ~END_CIRC_REASON_FLAG_REMOTE;
  1112. if (reason < _END_CIRC_REASON_MIN || reason > _END_CIRC_REASON_MAX) {
  1113. if (!(orig_reason & END_CIRC_REASON_FLAG_REMOTE))
  1114. log_warn(LD_BUG, "Reason %d out of range at %s:%d", reason, file, line);
  1115. reason = END_CIRC_REASON_NONE;
  1116. }
  1117. if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  1118. onion_pending_remove(TO_OR_CIRCUIT(circ));
  1119. }
  1120. /* If the circuit ever became OPEN, we sent it to the reputation history
  1121. * module then. If it isn't OPEN, we send it there now to remember which
  1122. * links worked and which didn't.
  1123. */
  1124. if (circ->state != CIRCUIT_STATE_OPEN) {
  1125. if (CIRCUIT_IS_ORIGIN(circ)) {
  1126. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1127. circuit_build_failed(ocirc); /* take actions if necessary */
  1128. circuit_rep_hist_note_result(ocirc);
  1129. }
  1130. }
  1131. if (circ->state == CIRCUIT_STATE_OR_WAIT) {
  1132. if (circuits_pending_or_conns)
  1133. smartlist_remove(circuits_pending_or_conns, circ);
  1134. }
  1135. if (CIRCUIT_IS_ORIGIN(circ)) {
  1136. control_event_circuit_status(TO_ORIGIN_CIRCUIT(circ),
  1137. (circ->state == CIRCUIT_STATE_OPEN)?CIRC_EVENT_CLOSED:CIRC_EVENT_FAILED,
  1138. orig_reason);
  1139. }
  1140. if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
  1141. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1142. int timed_out = (reason == END_CIRC_REASON_TIMEOUT);
  1143. tor_assert(circ->state == CIRCUIT_STATE_OPEN);
  1144. tor_assert(ocirc->build_state->chosen_exit);
  1145. tor_assert(ocirc->rend_data);
  1146. /* treat this like getting a nack from it */
  1147. log_info(LD_REND, "Failed intro circ %s to %s (awaiting ack). %s",
  1148. safe_str_client(ocirc->rend_data->onion_address),
  1149. safe_str_client(build_state_get_exit_nickname(ocirc->build_state)),
  1150. timed_out ? "Recording timeout." : "Removing from descriptor.");
  1151. rend_client_report_intro_point_failure(ocirc->build_state->chosen_exit,
  1152. ocirc->rend_data,
  1153. timed_out ?
  1154. INTRO_POINT_FAILURE_TIMEOUT :
  1155. INTRO_POINT_FAILURE_GENERIC);
  1156. } else if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCING &&
  1157. reason != END_CIRC_REASON_TIMEOUT) {
  1158. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1159. if (ocirc->build_state->chosen_exit && ocirc->rend_data) {
  1160. log_info(LD_REND, "Failed intro circ %s to %s "
  1161. "(building circuit to intro point). "
  1162. "Marking intro point as possibly unreachable.",
  1163. safe_str_client(ocirc->rend_data->onion_address),
  1164. safe_str_client(build_state_get_exit_nickname(ocirc->build_state)));
  1165. rend_client_report_intro_point_failure(ocirc->build_state->chosen_exit,
  1166. ocirc->rend_data,
  1167. INTRO_POINT_FAILURE_UNREACHABLE);
  1168. }
  1169. }
  1170. if (circ->n_conn) {
  1171. circuit_clear_cell_queue(circ, circ->n_conn);
  1172. connection_or_send_destroy(circ->n_circ_id, circ->n_conn, reason);
  1173. }
  1174. if (! CIRCUIT_IS_ORIGIN(circ)) {
  1175. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  1176. edge_connection_t *conn;
  1177. for (conn=or_circ->n_streams; conn; conn=conn->next_stream)
  1178. connection_edge_destroy(or_circ->p_circ_id, conn);
  1179. or_circ->n_streams = NULL;
  1180. while (or_circ->resolving_streams) {
  1181. conn = or_circ->resolving_streams;
  1182. or_circ->resolving_streams = conn->next_stream;
  1183. if (!conn->_base.marked_for_close) {
  1184. /* The client will see a DESTROY, and infer that the connections
  1185. * are closing because the circuit is getting torn down. No need
  1186. * to send an end cell. */
  1187. conn->edge_has_sent_end = 1;
  1188. conn->end_reason = END_STREAM_REASON_DESTROY;
  1189. conn->end_reason |= END_STREAM_REASON_FLAG_ALREADY_SENT_CLOSED;
  1190. connection_mark_for_close(TO_CONN(conn));
  1191. }
  1192. conn->on_circuit = NULL;
  1193. }
  1194. if (or_circ->p_conn) {
  1195. circuit_clear_cell_queue(circ, or_circ->p_conn);
  1196. connection_or_send_destroy(or_circ->p_circ_id, or_circ->p_conn, reason);
  1197. }
  1198. } else {
  1199. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1200. edge_connection_t *conn;
  1201. for (conn=ocirc->p_streams; conn; conn=conn->next_stream)
  1202. connection_edge_destroy(circ->n_circ_id, conn);
  1203. ocirc->p_streams = NULL;
  1204. }
  1205. circ->marked_for_close = line;
  1206. circ->marked_for_close_file = file;
  1207. if (!CIRCUIT_IS_ORIGIN(circ)) {
  1208. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  1209. if (or_circ->rend_splice) {
  1210. if (!or_circ->rend_splice->_base.marked_for_close) {
  1211. /* do this after marking this circuit, to avoid infinite recursion. */
  1212. circuit_mark_for_close(TO_CIRCUIT(or_circ->rend_splice), reason);
  1213. }
  1214. or_circ->rend_splice = NULL;
  1215. }
  1216. }
  1217. }
  1218. /** Verify that cpath layer <b>cp</b> has all of its invariants
  1219. * correct. Trigger an assert if anything is invalid.
  1220. */
  1221. void
  1222. assert_cpath_layer_ok(const crypt_path_t *cp)
  1223. {
  1224. // tor_assert(cp->addr); /* these are zero for rendezvous extra-hops */
  1225. // tor_assert(cp->port);
  1226. tor_assert(cp);
  1227. tor_assert(cp->magic == CRYPT_PATH_MAGIC);
  1228. switch (cp->state)
  1229. {
  1230. case CPATH_STATE_OPEN:
  1231. tor_assert(cp->f_crypto);
  1232. tor_assert(cp->b_crypto);
  1233. /* fall through */
  1234. case CPATH_STATE_CLOSED:
  1235. tor_assert(!cp->dh_handshake_state);
  1236. break;
  1237. case CPATH_STATE_AWAITING_KEYS:
  1238. /* tor_assert(cp->dh_handshake_state); */
  1239. break;
  1240. default:
  1241. log_fn(LOG_ERR, LD_BUG, "Unexpected state %d", cp->state);
  1242. tor_assert(0);
  1243. }
  1244. tor_assert(cp->package_window >= 0);
  1245. tor_assert(cp->deliver_window >= 0);
  1246. }
  1247. /** Verify that cpath <b>cp</b> has all of its invariants
  1248. * correct. Trigger an assert if anything is invalid.
  1249. */
  1250. static void
  1251. assert_cpath_ok(const crypt_path_t *cp)
  1252. {
  1253. const crypt_path_t *start = cp;
  1254. do {
  1255. assert_cpath_layer_ok(cp);
  1256. /* layers must be in sequence of: "open* awaiting? closed*" */
  1257. if (cp != start) {
  1258. if (cp->state == CPATH_STATE_AWAITING_KEYS) {
  1259. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  1260. } else if (cp->state == CPATH_STATE_OPEN) {
  1261. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  1262. }
  1263. }
  1264. cp = cp->next;
  1265. tor_assert(cp);
  1266. } while (cp != start);
  1267. }
  1268. /** Verify that circuit <b>c</b> has all of its invariants
  1269. * correct. Trigger an assert if anything is invalid.
  1270. */
  1271. void
  1272. assert_circuit_ok(const circuit_t *c)
  1273. {
  1274. edge_connection_t *conn;
  1275. const or_circuit_t *or_circ = NULL;
  1276. const origin_circuit_t *origin_circ = NULL;
  1277. tor_assert(c);
  1278. tor_assert(c->magic == ORIGIN_CIRCUIT_MAGIC || c->magic == OR_CIRCUIT_MAGIC);
  1279. tor_assert(c->purpose >= _CIRCUIT_PURPOSE_MIN &&
  1280. c->purpose <= _CIRCUIT_PURPOSE_MAX);
  1281. {
  1282. /* Having a separate variable for this pleases GCC 4.2 in ways I hope I
  1283. * never understand. -NM. */
  1284. circuit_t *nonconst_circ = (circuit_t*) c;
  1285. if (CIRCUIT_IS_ORIGIN(c))
  1286. origin_circ = TO_ORIGIN_CIRCUIT(nonconst_circ);
  1287. else
  1288. or_circ = TO_OR_CIRCUIT(nonconst_circ);
  1289. }
  1290. if (c->n_conn) {
  1291. tor_assert(!c->n_hop);
  1292. if (c->n_circ_id) {
  1293. /* We use the _impl variant here to make sure we don't fail on marked
  1294. * circuits, which would not be returned by the regular function. */
  1295. circuit_t *c2 = circuit_get_by_circid_orconn_impl(c->n_circ_id,
  1296. c->n_conn);
  1297. tor_assert(c == c2);
  1298. }
  1299. }
  1300. if (or_circ && or_circ->p_conn) {
  1301. if (or_circ->p_circ_id) {
  1302. /* ibid */
  1303. circuit_t *c2 = circuit_get_by_circid_orconn_impl(or_circ->p_circ_id,
  1304. or_circ->p_conn);
  1305. tor_assert(c == c2);
  1306. }
  1307. }
  1308. if (or_circ)
  1309. for (conn = or_circ->n_streams; conn; conn = conn->next_stream)
  1310. tor_assert(conn->_base.type == CONN_TYPE_EXIT);
  1311. tor_assert(c->deliver_window >= 0);
  1312. tor_assert(c->package_window >= 0);
  1313. if (c->state == CIRCUIT_STATE_OPEN) {
  1314. tor_assert(!c->n_conn_onionskin);
  1315. if (or_circ) {
  1316. tor_assert(or_circ->n_crypto);
  1317. tor_assert(or_circ->p_crypto);
  1318. tor_assert(or_circ->n_digest);
  1319. tor_assert(or_circ->p_digest);
  1320. }
  1321. }
  1322. if (c->state == CIRCUIT_STATE_OR_WAIT && !c->marked_for_close) {
  1323. tor_assert(circuits_pending_or_conns &&
  1324. smartlist_isin(circuits_pending_or_conns, c));
  1325. } else {
  1326. tor_assert(!circuits_pending_or_conns ||
  1327. !smartlist_isin(circuits_pending_or_conns, c));
  1328. }
  1329. if (origin_circ && origin_circ->cpath) {
  1330. assert_cpath_ok(origin_circ->cpath);
  1331. }
  1332. if (c->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED) {
  1333. tor_assert(or_circ);
  1334. if (!c->marked_for_close) {
  1335. tor_assert(or_circ->rend_splice);
  1336. tor_assert(or_circ->rend_splice->rend_splice == or_circ);
  1337. }
  1338. tor_assert(or_circ->rend_splice != or_circ);
  1339. } else {
  1340. tor_assert(!or_circ || !or_circ->rend_splice);
  1341. }
  1342. }