circuitlist.c 51 KB

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