circuitlist.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601
  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. circ->package_window = circuit_initial_package_window();
  495. circ->deliver_window = CIRCWINDOW_START;
  496. circuit_add(circ);
  497. }
  498. /** Allocate space for a new circuit, initializing with <b>p_circ_id</b>
  499. * and <b>p_conn</b>. Add it to the global circuit list.
  500. */
  501. origin_circuit_t *
  502. origin_circuit_new(void)
  503. {
  504. origin_circuit_t *circ;
  505. /* never zero, since a global ID of 0 is treated specially by the
  506. * controller */
  507. static uint32_t n_circuits_allocated = 1;
  508. circ = tor_malloc_zero(sizeof(origin_circuit_t));
  509. circ->base_.magic = ORIGIN_CIRCUIT_MAGIC;
  510. circ->next_stream_id = crypto_rand_int(1<<16);
  511. circ->global_identifier = n_circuits_allocated++;
  512. circ->remaining_relay_early_cells = MAX_RELAY_EARLY_CELLS_PER_CIRCUIT;
  513. circ->remaining_relay_early_cells -= crypto_rand_int(2);
  514. init_circuit_base(TO_CIRCUIT(circ));
  515. circ_times.last_circ_at = approx_time();
  516. return circ;
  517. }
  518. /** Allocate a new or_circuit_t, connected to <b>p_conn</b> as
  519. * <b>p_circ_id</b>. If <b>p_conn</b> is NULL, the circuit is unattached. */
  520. or_circuit_t *
  521. or_circuit_new(circid_t p_circ_id, channel_t *p_chan)
  522. {
  523. /* CircIDs */
  524. or_circuit_t *circ;
  525. circ = tor_malloc_zero(sizeof(or_circuit_t));
  526. circ->base_.magic = OR_CIRCUIT_MAGIC;
  527. if (p_chan)
  528. circuit_set_p_circid_chan(circ, p_circ_id, p_chan);
  529. circ->remaining_relay_early_cells = MAX_RELAY_EARLY_CELLS_PER_CIRCUIT;
  530. init_circuit_base(TO_CIRCUIT(circ));
  531. return circ;
  532. }
  533. /** Deallocate space associated with circ.
  534. */
  535. static void
  536. circuit_free(circuit_t *circ)
  537. {
  538. void *mem;
  539. size_t memlen;
  540. if (!circ)
  541. return;
  542. if (CIRCUIT_IS_ORIGIN(circ)) {
  543. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  544. mem = ocirc;
  545. memlen = sizeof(origin_circuit_t);
  546. tor_assert(circ->magic == ORIGIN_CIRCUIT_MAGIC);
  547. if (ocirc->build_state) {
  548. extend_info_free(ocirc->build_state->chosen_exit);
  549. circuit_free_cpath_node(ocirc->build_state->pending_final_cpath);
  550. cpath_ref_decref(ocirc->build_state->service_pending_final_cpath_ref);
  551. }
  552. tor_free(ocirc->build_state);
  553. circuit_free_cpath(ocirc->cpath);
  554. crypto_pk_free(ocirc->intro_key);
  555. rend_data_free(ocirc->rend_data);
  556. tor_free(ocirc->dest_address);
  557. if (ocirc->socks_username) {
  558. memset(ocirc->socks_username, 0x12, ocirc->socks_username_len);
  559. tor_free(ocirc->socks_username);
  560. }
  561. if (ocirc->socks_password) {
  562. memset(ocirc->socks_password, 0x06, ocirc->socks_password_len);
  563. tor_free(ocirc->socks_password);
  564. }
  565. } else {
  566. or_circuit_t *ocirc = TO_OR_CIRCUIT(circ);
  567. /* Remember cell statistics for this circuit before deallocating. */
  568. if (get_options()->CellStatistics)
  569. rep_hist_buffer_stats_add_circ(circ, time(NULL));
  570. mem = ocirc;
  571. memlen = sizeof(or_circuit_t);
  572. tor_assert(circ->magic == OR_CIRCUIT_MAGIC);
  573. crypto_cipher_free(ocirc->p_crypto);
  574. crypto_digest_free(ocirc->p_digest);
  575. crypto_cipher_free(ocirc->n_crypto);
  576. crypto_digest_free(ocirc->n_digest);
  577. if (ocirc->rend_splice) {
  578. or_circuit_t *other = ocirc->rend_splice;
  579. tor_assert(other->base_.magic == OR_CIRCUIT_MAGIC);
  580. other->rend_splice = NULL;
  581. }
  582. /* remove from map. */
  583. circuit_set_p_circid_chan(ocirc, 0, NULL);
  584. /* Clear cell queue _after_ removing it from the map. Otherwise our
  585. * "active" checks will be violated. */
  586. cell_queue_clear(&ocirc->p_chan_cells);
  587. }
  588. extend_info_free(circ->n_hop);
  589. tor_free(circ->n_chan_onionskin);
  590. /* Remove from map. */
  591. circuit_set_n_circid_chan(circ, 0, NULL);
  592. /* Clear cell queue _after_ removing it from the map. Otherwise our
  593. * "active" checks will be violated. */
  594. cell_queue_clear(&circ->n_chan_cells);
  595. memset(mem, 0xAA, memlen); /* poison memory */
  596. tor_free(mem);
  597. }
  598. /** Deallocate space associated with the linked list <b>cpath</b>. */
  599. static void
  600. circuit_free_cpath(crypt_path_t *cpath)
  601. {
  602. crypt_path_t *victim, *head=cpath;
  603. if (!cpath)
  604. return;
  605. /* it's a doubly linked list, so we have to notice when we've
  606. * gone through it once. */
  607. while (cpath->next && cpath->next != head) {
  608. victim = cpath;
  609. cpath = victim->next;
  610. circuit_free_cpath_node(victim);
  611. }
  612. circuit_free_cpath_node(cpath);
  613. }
  614. /** Release all storage held by circuits. */
  615. void
  616. circuit_free_all(void)
  617. {
  618. circuit_t *next;
  619. while (global_circuitlist) {
  620. next = global_circuitlist->next;
  621. if (! CIRCUIT_IS_ORIGIN(global_circuitlist)) {
  622. or_circuit_t *or_circ = TO_OR_CIRCUIT(global_circuitlist);
  623. while (or_circ->resolving_streams) {
  624. edge_connection_t *next_conn;
  625. next_conn = or_circ->resolving_streams->next_stream;
  626. connection_free(TO_CONN(or_circ->resolving_streams));
  627. or_circ->resolving_streams = next_conn;
  628. }
  629. }
  630. circuit_free(global_circuitlist);
  631. global_circuitlist = next;
  632. }
  633. smartlist_free(circuits_pending_chans);
  634. circuits_pending_chans = NULL;
  635. HT_CLEAR(chan_circid_map, &chan_circid_map);
  636. }
  637. /** Deallocate space associated with the cpath node <b>victim</b>. */
  638. static void
  639. circuit_free_cpath_node(crypt_path_t *victim)
  640. {
  641. if (!victim)
  642. return;
  643. crypto_cipher_free(victim->f_crypto);
  644. crypto_cipher_free(victim->b_crypto);
  645. crypto_digest_free(victim->f_digest);
  646. crypto_digest_free(victim->b_digest);
  647. crypto_dh_free(victim->dh_handshake_state);
  648. extend_info_free(victim->extend_info);
  649. memset(victim, 0xBB, sizeof(crypt_path_t)); /* poison memory */
  650. tor_free(victim);
  651. }
  652. /** Release a crypt_path_reference_t*, which may be NULL. */
  653. static void
  654. cpath_ref_decref(crypt_path_reference_t *cpath_ref)
  655. {
  656. if (cpath_ref != NULL) {
  657. if (--(cpath_ref->refcount) == 0) {
  658. circuit_free_cpath_node(cpath_ref->cpath);
  659. tor_free(cpath_ref);
  660. }
  661. }
  662. }
  663. /** A helper function for circuit_dump_by_conn() below. Log a bunch
  664. * of information about circuit <b>circ</b>.
  665. */
  666. static void
  667. circuit_dump_conn_details(int severity,
  668. circuit_t *circ,
  669. int conn_array_index,
  670. const char *type,
  671. int this_circid,
  672. int other_circid)
  673. {
  674. log(severity, LD_CIRC, "Conn %d has %s circuit: circID %d (other side %d), "
  675. "state %d (%s), born %ld:",
  676. conn_array_index, type, this_circid, other_circid, circ->state,
  677. circuit_state_to_string(circ->state),
  678. (long)circ->timestamp_created.tv_sec);
  679. if (CIRCUIT_IS_ORIGIN(circ)) { /* circ starts at this node */
  680. circuit_log_path(severity, LD_CIRC, TO_ORIGIN_CIRCUIT(circ));
  681. }
  682. }
  683. /** Log, at severity <b>severity</b>, information about each circuit
  684. * that is connected to <b>conn</b>.
  685. */
  686. void
  687. circuit_dump_by_conn(connection_t *conn, int severity)
  688. {
  689. circuit_t *circ;
  690. edge_connection_t *tmpconn;
  691. for (circ = global_circuitlist; circ; circ = circ->next) {
  692. circid_t n_circ_id = circ->n_circ_id, p_circ_id = 0;
  693. if (circ->marked_for_close) {
  694. continue;
  695. }
  696. if (!CIRCUIT_IS_ORIGIN(circ)) {
  697. p_circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  698. }
  699. if (CIRCUIT_IS_ORIGIN(circ)) {
  700. for (tmpconn=TO_ORIGIN_CIRCUIT(circ)->p_streams; tmpconn;
  701. tmpconn=tmpconn->next_stream) {
  702. if (TO_CONN(tmpconn) == conn) {
  703. circuit_dump_conn_details(severity, circ, conn->conn_array_index,
  704. "App-ward", p_circ_id, n_circ_id);
  705. }
  706. }
  707. }
  708. if (! CIRCUIT_IS_ORIGIN(circ)) {
  709. for (tmpconn=TO_OR_CIRCUIT(circ)->n_streams; tmpconn;
  710. tmpconn=tmpconn->next_stream) {
  711. if (TO_CONN(tmpconn) == conn) {
  712. circuit_dump_conn_details(severity, circ, conn->conn_array_index,
  713. "Exit-ward", n_circ_id, p_circ_id);
  714. }
  715. }
  716. }
  717. }
  718. }
  719. /** A helper function for circuit_dump_by_chan() below. Log a bunch
  720. * of information about circuit <b>circ</b>.
  721. */
  722. static void
  723. circuit_dump_chan_details(int severity,
  724. circuit_t *circ,
  725. channel_t *chan,
  726. const char *type,
  727. int this_circid,
  728. int other_circid)
  729. {
  730. log(severity, LD_CIRC, "Conn %p has %s circuit: circID %d (other side %d), "
  731. "state %d (%s), born %ld:",
  732. chan, type, this_circid, other_circid, circ->state,
  733. circuit_state_to_string(circ->state),
  734. (long)circ->timestamp_created.tv_sec);
  735. if (CIRCUIT_IS_ORIGIN(circ)) { /* circ starts at this node */
  736. circuit_log_path(severity, LD_CIRC, TO_ORIGIN_CIRCUIT(circ));
  737. }
  738. }
  739. /** Log, at severity <b>severity</b>, information about each circuit
  740. * that is connected to <b>chan</b>.
  741. */
  742. void
  743. circuit_dump_by_chan(channel_t *chan, int severity)
  744. {
  745. circuit_t *circ;
  746. tor_assert(chan);
  747. for (circ = global_circuitlist; circ; circ = circ->next) {
  748. circid_t n_circ_id = circ->n_circ_id, p_circ_id = 0;
  749. if (circ->marked_for_close) {
  750. continue;
  751. }
  752. if (!CIRCUIT_IS_ORIGIN(circ)) {
  753. p_circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  754. }
  755. if (! CIRCUIT_IS_ORIGIN(circ) && TO_OR_CIRCUIT(circ)->p_chan &&
  756. TO_OR_CIRCUIT(circ)->p_chan == chan) {
  757. circuit_dump_chan_details(severity, circ, chan, "App-ward",
  758. p_circ_id, n_circ_id);
  759. }
  760. if (circ->n_chan && circ->n_chan == chan) {
  761. circuit_dump_chan_details(severity, circ, chan, "Exit-ward",
  762. n_circ_id, p_circ_id);
  763. }
  764. if (!circ->n_chan && circ->n_hop &&
  765. channel_matches_extend_info(chan, circ->n_hop) &&
  766. tor_memeq(chan->identity_digest,
  767. circ->n_hop->identity_digest, DIGEST_LEN)) {
  768. circuit_dump_chan_details(severity, circ, chan,
  769. (circ->state == CIRCUIT_STATE_OPEN &&
  770. !CIRCUIT_IS_ORIGIN(circ)) ?
  771. "Endpoint" : "Pending",
  772. n_circ_id, p_circ_id);
  773. }
  774. }
  775. }
  776. /** Return the circuit whose global ID is <b>id</b>, or NULL if no
  777. * such circuit exists. */
  778. origin_circuit_t *
  779. circuit_get_by_global_id(uint32_t id)
  780. {
  781. circuit_t *circ;
  782. for (circ=global_circuitlist;circ;circ = circ->next) {
  783. if (CIRCUIT_IS_ORIGIN(circ) &&
  784. TO_ORIGIN_CIRCUIT(circ)->global_identifier == id) {
  785. if (circ->marked_for_close)
  786. return NULL;
  787. else
  788. return TO_ORIGIN_CIRCUIT(circ);
  789. }
  790. }
  791. return NULL;
  792. }
  793. /** Return a circ such that:
  794. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  795. * - circ is attached to <b>chan</b>, either as p_chan or n_chan.
  796. * Return NULL if no such circuit exists.
  797. */
  798. static INLINE circuit_t *
  799. circuit_get_by_circid_channel_impl(circid_t circ_id, channel_t *chan)
  800. {
  801. chan_circid_circuit_map_t search;
  802. chan_circid_circuit_map_t *found;
  803. if (_last_circid_chan_ent &&
  804. circ_id == _last_circid_chan_ent->circ_id &&
  805. chan == _last_circid_chan_ent->chan) {
  806. found = _last_circid_chan_ent;
  807. } else {
  808. search.circ_id = circ_id;
  809. search.chan = chan;
  810. found = HT_FIND(chan_circid_map, &chan_circid_map, &search);
  811. _last_circid_chan_ent = found;
  812. }
  813. if (found && found->circuit) {
  814. log_debug(LD_CIRC,
  815. "circuit_get_by_circid_channel_impl() returning circuit %p for"
  816. " circ_id %d, channel ID " U64_FORMAT " (%p)",
  817. found->circuit, circ_id,
  818. U64_PRINTF_ARG(chan->global_identifier), chan);
  819. return found->circuit;
  820. }
  821. log_debug(LD_CIRC,
  822. "circuit_get_by_circid_channel_impl() found nothing for"
  823. " circ_id %d, channel ID " U64_FORMAT " (%p)",
  824. circ_id,
  825. U64_PRINTF_ARG(chan->global_identifier), chan);
  826. return NULL;
  827. /* The rest of this checks for bugs. Disabled by default. */
  828. /* We comment it out because coverity complains otherwise.
  829. {
  830. circuit_t *circ;
  831. for (circ=global_circuitlist;circ;circ = circ->next) {
  832. if (! CIRCUIT_IS_ORIGIN(circ)) {
  833. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  834. if (or_circ->p_chan == chan && or_circ->p_circ_id == circ_id) {
  835. log_warn(LD_BUG,
  836. "circuit matches p_chan, but not in hash table (Bug!)");
  837. return circ;
  838. }
  839. }
  840. if (circ->n_chan == chan && circ->n_circ_id == circ_id) {
  841. log_warn(LD_BUG,
  842. "circuit matches n_chan, but not in hash table (Bug!)");
  843. return circ;
  844. }
  845. }
  846. return NULL;
  847. } */
  848. }
  849. /** Return a circ such that:
  850. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  851. * - circ is attached to <b>chan</b>, either as p_chan or n_chan.
  852. * - circ is not marked for close.
  853. * Return NULL if no such circuit exists.
  854. */
  855. circuit_t *
  856. circuit_get_by_circid_channel(circid_t circ_id, channel_t *chan)
  857. {
  858. circuit_t *circ = circuit_get_by_circid_channel_impl(circ_id, chan);
  859. if (!circ || circ->marked_for_close)
  860. return NULL;
  861. else
  862. return circ;
  863. }
  864. /** Return a circ such that:
  865. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  866. * - circ is attached to <b>chan</b>, either as p_chan or n_chan.
  867. * Return NULL if no such circuit exists.
  868. */
  869. circuit_t *
  870. circuit_get_by_circid_channel_even_if_marked(circid_t circ_id,
  871. channel_t *chan)
  872. {
  873. return circuit_get_by_circid_channel_impl(circ_id, chan);
  874. }
  875. /** Return true iff the circuit ID <b>circ_id</b> is currently used by a
  876. * circuit, marked or not, on <b>chan</b>. */
  877. int
  878. circuit_id_in_use_on_channel(circid_t circ_id, channel_t *chan)
  879. {
  880. return circuit_get_by_circid_channel_impl(circ_id, chan) != NULL;
  881. }
  882. /** Return the circuit that a given edge connection is using. */
  883. circuit_t *
  884. circuit_get_by_edge_conn(edge_connection_t *conn)
  885. {
  886. circuit_t *circ;
  887. circ = conn->on_circuit;
  888. tor_assert(!circ ||
  889. (CIRCUIT_IS_ORIGIN(circ) ? circ->magic == ORIGIN_CIRCUIT_MAGIC
  890. : circ->magic == OR_CIRCUIT_MAGIC));
  891. return circ;
  892. }
  893. /** For each circuit that has <b>chan</b> as n_chan or p_chan, unlink the
  894. * circuit from the chan,circid map, and mark it for close if it hasn't
  895. * been marked already.
  896. */
  897. void
  898. circuit_unlink_all_from_channel(channel_t *chan, int reason)
  899. {
  900. circuit_t *circ;
  901. channel_unlink_all_circuits(chan);
  902. for (circ = global_circuitlist; circ; circ = circ->next) {
  903. int mark = 0;
  904. if (circ->n_chan == chan) {
  905. circuit_set_n_circid_chan(circ, 0, NULL);
  906. mark = 1;
  907. }
  908. if (! CIRCUIT_IS_ORIGIN(circ)) {
  909. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  910. if (or_circ->p_chan == chan) {
  911. circuit_set_p_circid_chan(or_circ, 0, NULL);
  912. mark = 1;
  913. }
  914. }
  915. if (mark && !circ->marked_for_close)
  916. circuit_mark_for_close(circ, reason);
  917. }
  918. }
  919. /** Return a circ such that
  920. * - circ-\>rend_data-\>onion_address is equal to
  921. * <b>rend_data</b>-\>onion_address,
  922. * - circ-\>rend_data-\>rend_cookie is equal to
  923. * <b>rend_data</b>-\>rend_cookie, and
  924. * - circ-\>purpose is equal to CIRCUIT_PURPOSE_C_REND_READY.
  925. *
  926. * Return NULL if no such circuit exists.
  927. */
  928. origin_circuit_t *
  929. circuit_get_ready_rend_circ_by_rend_data(const rend_data_t *rend_data)
  930. {
  931. circuit_t *circ;
  932. for (circ = global_circuitlist; circ; circ = circ->next) {
  933. if (!circ->marked_for_close &&
  934. circ->purpose == CIRCUIT_PURPOSE_C_REND_READY) {
  935. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  936. if (ocirc->rend_data &&
  937. !rend_cmp_service_ids(rend_data->onion_address,
  938. ocirc->rend_data->onion_address) &&
  939. tor_memeq(ocirc->rend_data->rend_cookie,
  940. rend_data->rend_cookie,
  941. REND_COOKIE_LEN))
  942. return ocirc;
  943. }
  944. }
  945. return NULL;
  946. }
  947. /** Return the first circuit originating here in global_circuitlist after
  948. * <b>start</b> whose purpose is <b>purpose</b>, and where
  949. * <b>digest</b> (if set) matches the rend_pk_digest field. Return NULL if no
  950. * circuit is found. If <b>start</b> is NULL, begin at the start of the list.
  951. */
  952. origin_circuit_t *
  953. circuit_get_next_by_pk_and_purpose(origin_circuit_t *start,
  954. const char *digest, uint8_t purpose)
  955. {
  956. circuit_t *circ;
  957. tor_assert(CIRCUIT_PURPOSE_IS_ORIGIN(purpose));
  958. if (start == NULL)
  959. circ = global_circuitlist;
  960. else
  961. circ = TO_CIRCUIT(start)->next;
  962. for ( ; circ; circ = circ->next) {
  963. if (circ->marked_for_close)
  964. continue;
  965. if (circ->purpose != purpose)
  966. continue;
  967. if (!digest)
  968. return TO_ORIGIN_CIRCUIT(circ);
  969. else if (TO_ORIGIN_CIRCUIT(circ)->rend_data &&
  970. tor_memeq(TO_ORIGIN_CIRCUIT(circ)->rend_data->rend_pk_digest,
  971. digest, DIGEST_LEN))
  972. return TO_ORIGIN_CIRCUIT(circ);
  973. }
  974. return NULL;
  975. }
  976. /** Return the first OR circuit in the global list whose purpose is
  977. * <b>purpose</b>, and whose rend_token is the <b>len</b>-byte
  978. * <b>token</b>. */
  979. static or_circuit_t *
  980. circuit_get_by_rend_token_and_purpose(uint8_t purpose, const char *token,
  981. size_t len)
  982. {
  983. circuit_t *circ;
  984. for (circ = global_circuitlist; circ; circ = circ->next) {
  985. if (! circ->marked_for_close &&
  986. circ->purpose == purpose &&
  987. tor_memeq(TO_OR_CIRCUIT(circ)->rend_token, token, len))
  988. return TO_OR_CIRCUIT(circ);
  989. }
  990. return NULL;
  991. }
  992. /** Return the circuit waiting for a rendezvous with the provided cookie.
  993. * Return NULL if no such circuit is found.
  994. */
  995. or_circuit_t *
  996. circuit_get_rendezvous(const char *cookie)
  997. {
  998. return circuit_get_by_rend_token_and_purpose(
  999. CIRCUIT_PURPOSE_REND_POINT_WAITING,
  1000. cookie, REND_COOKIE_LEN);
  1001. }
  1002. /** Return the circuit waiting for intro cells of the given digest.
  1003. * Return NULL if no such circuit is found.
  1004. */
  1005. or_circuit_t *
  1006. circuit_get_intro_point(const char *digest)
  1007. {
  1008. return circuit_get_by_rend_token_and_purpose(
  1009. CIRCUIT_PURPOSE_INTRO_POINT, digest,
  1010. DIGEST_LEN);
  1011. }
  1012. /** Return a circuit that is open, is CIRCUIT_PURPOSE_C_GENERAL,
  1013. * has a timestamp_dirty value of 0, has flags matching the CIRCLAUNCH_*
  1014. * flags in <b>flags</b>, and if info is defined, does not already use info
  1015. * as any of its hops; or NULL if no circuit fits this description.
  1016. *
  1017. * The <b>purpose</b> argument (currently ignored) refers to the purpose of
  1018. * the circuit we want to create, not the purpose of the circuit we want to
  1019. * cannibalize.
  1020. *
  1021. * If !CIRCLAUNCH_NEED_UPTIME, prefer returning non-uptime circuits.
  1022. */
  1023. origin_circuit_t *
  1024. circuit_find_to_cannibalize(uint8_t purpose, extend_info_t *info,
  1025. int flags)
  1026. {
  1027. circuit_t *circ_;
  1028. origin_circuit_t *best=NULL;
  1029. int need_uptime = (flags & CIRCLAUNCH_NEED_UPTIME) != 0;
  1030. int need_capacity = (flags & CIRCLAUNCH_NEED_CAPACITY) != 0;
  1031. int internal = (flags & CIRCLAUNCH_IS_INTERNAL) != 0;
  1032. const or_options_t *options = get_options();
  1033. /* Make sure we're not trying to create a onehop circ by
  1034. * cannibalization. */
  1035. tor_assert(!(flags & CIRCLAUNCH_ONEHOP_TUNNEL));
  1036. log_debug(LD_CIRC,
  1037. "Hunting for a circ to cannibalize: purpose %d, uptime %d, "
  1038. "capacity %d, internal %d",
  1039. purpose, need_uptime, need_capacity, internal);
  1040. for (circ_=global_circuitlist; circ_; circ_ = circ_->next) {
  1041. if (CIRCUIT_IS_ORIGIN(circ_) &&
  1042. circ_->state == CIRCUIT_STATE_OPEN &&
  1043. !circ_->marked_for_close &&
  1044. circ_->purpose == CIRCUIT_PURPOSE_C_GENERAL &&
  1045. !circ_->timestamp_dirty) {
  1046. origin_circuit_t *circ = TO_ORIGIN_CIRCUIT(circ_);
  1047. if ((!need_uptime || circ->build_state->need_uptime) &&
  1048. (!need_capacity || circ->build_state->need_capacity) &&
  1049. (internal == circ->build_state->is_internal) &&
  1050. circ->remaining_relay_early_cells &&
  1051. circ->build_state->desired_path_len == DEFAULT_ROUTE_LEN &&
  1052. !circ->build_state->onehop_tunnel &&
  1053. !circ->isolation_values_set) {
  1054. if (info) {
  1055. /* need to make sure we don't duplicate hops */
  1056. crypt_path_t *hop = circ->cpath;
  1057. const node_t *ri1 = node_get_by_id(info->identity_digest);
  1058. do {
  1059. const node_t *ri2;
  1060. if (tor_memeq(hop->extend_info->identity_digest,
  1061. info->identity_digest, DIGEST_LEN))
  1062. goto next;
  1063. if (ri1 &&
  1064. (ri2 = node_get_by_id(hop->extend_info->identity_digest))
  1065. && nodes_in_same_family(ri1, ri2))
  1066. goto next;
  1067. hop=hop->next;
  1068. } while (hop!=circ->cpath);
  1069. }
  1070. if (options->ExcludeNodes) {
  1071. /* Make sure no existing nodes in the circuit are excluded for
  1072. * general use. (This may be possible if StrictNodes is 0, and we
  1073. * thought we needed to use an otherwise excluded node for, say, a
  1074. * directory operation.) */
  1075. crypt_path_t *hop = circ->cpath;
  1076. do {
  1077. if (routerset_contains_extendinfo(options->ExcludeNodes,
  1078. hop->extend_info))
  1079. goto next;
  1080. hop = hop->next;
  1081. } while (hop != circ->cpath);
  1082. }
  1083. if (!best || (best->build_state->need_uptime && !need_uptime))
  1084. best = circ;
  1085. next: ;
  1086. }
  1087. }
  1088. }
  1089. return best;
  1090. }
  1091. /** Return the number of hops in circuit's path. */
  1092. int
  1093. circuit_get_cpath_len(origin_circuit_t *circ)
  1094. {
  1095. int n = 0;
  1096. if (circ && circ->cpath) {
  1097. crypt_path_t *cpath, *cpath_next = NULL;
  1098. for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
  1099. cpath_next = cpath->next;
  1100. ++n;
  1101. }
  1102. }
  1103. return n;
  1104. }
  1105. /** Return the <b>hopnum</b>th hop in <b>circ</b>->cpath, or NULL if there
  1106. * aren't that many hops in the list. */
  1107. crypt_path_t *
  1108. circuit_get_cpath_hop(origin_circuit_t *circ, int hopnum)
  1109. {
  1110. if (circ && circ->cpath && hopnum > 0) {
  1111. crypt_path_t *cpath, *cpath_next = NULL;
  1112. for (cpath = circ->cpath; cpath_next != circ->cpath; cpath = cpath_next) {
  1113. cpath_next = cpath->next;
  1114. if (--hopnum <= 0)
  1115. return cpath;
  1116. }
  1117. }
  1118. return NULL;
  1119. }
  1120. /** Go through the circuitlist; mark-for-close each circuit that starts
  1121. * at us but has not yet been used. */
  1122. void
  1123. circuit_mark_all_unused_circs(void)
  1124. {
  1125. circuit_t *circ;
  1126. for (circ=global_circuitlist; circ; circ = circ->next) {
  1127. if (CIRCUIT_IS_ORIGIN(circ) &&
  1128. !circ->marked_for_close &&
  1129. !circ->timestamp_dirty)
  1130. circuit_mark_for_close(circ, END_CIRC_REASON_FINISHED);
  1131. }
  1132. }
  1133. /** Go through the circuitlist; for each circuit that starts at us
  1134. * and is dirty, frob its timestamp_dirty so we won't use it for any
  1135. * new streams.
  1136. *
  1137. * This is useful for letting the user change pseudonyms, so new
  1138. * streams will not be linkable to old streams.
  1139. */
  1140. /* XXX024 this is a bad name for what this function does */
  1141. void
  1142. circuit_expire_all_dirty_circs(void)
  1143. {
  1144. circuit_t *circ;
  1145. const or_options_t *options = get_options();
  1146. for (circ=global_circuitlist; circ; circ = circ->next) {
  1147. if (CIRCUIT_IS_ORIGIN(circ) &&
  1148. !circ->marked_for_close &&
  1149. circ->timestamp_dirty)
  1150. /* XXXX024 This is a screwed-up way to say "This is too dirty
  1151. * for new circuits. */
  1152. circ->timestamp_dirty -= options->MaxCircuitDirtiness;
  1153. }
  1154. }
  1155. /** Mark <b>circ</b> to be closed next time we call
  1156. * circuit_close_all_marked(). Do any cleanup needed:
  1157. * - If state is onionskin_pending, remove circ from the onion_pending
  1158. * list.
  1159. * - If circ isn't open yet: call circuit_build_failed() if we're
  1160. * the origin, and in either case call circuit_rep_hist_note_result()
  1161. * to note stats.
  1162. * - If purpose is C_INTRODUCE_ACK_WAIT, report the intro point
  1163. * failure we just had to the hidden service client module.
  1164. * - If purpose is C_INTRODUCING and <b>reason</b> isn't TIMEOUT,
  1165. * report to the hidden service client module that the intro point
  1166. * we just tried may be unreachable.
  1167. * - Send appropriate destroys and edge_destroys for conns and
  1168. * streams attached to circ.
  1169. * - If circ->rend_splice is set (we are the midpoint of a joined
  1170. * rendezvous stream), then mark the other circuit to close as well.
  1171. */
  1172. void
  1173. circuit_mark_for_close_(circuit_t *circ, int reason, int line,
  1174. const char *file)
  1175. {
  1176. int orig_reason = reason; /* Passed to the controller */
  1177. assert_circuit_ok(circ);
  1178. tor_assert(line);
  1179. tor_assert(file);
  1180. if (circ->marked_for_close) {
  1181. log(LOG_WARN,LD_BUG,
  1182. "Duplicate call to circuit_mark_for_close at %s:%d"
  1183. " (first at %s:%d)", file, line,
  1184. circ->marked_for_close_file, circ->marked_for_close);
  1185. return;
  1186. }
  1187. if (reason == END_CIRC_AT_ORIGIN) {
  1188. if (!CIRCUIT_IS_ORIGIN(circ)) {
  1189. log_warn(LD_BUG, "Specified 'at-origin' non-reason for ending circuit, "
  1190. "but circuit was not at origin. (called %s:%d, purpose=%d)",
  1191. file, line, circ->purpose);
  1192. }
  1193. reason = END_CIRC_REASON_NONE;
  1194. }
  1195. if (CIRCUIT_IS_ORIGIN(circ)) {
  1196. /* We don't send reasons when closing circuits at the origin. */
  1197. reason = END_CIRC_REASON_NONE;
  1198. }
  1199. if (reason & END_CIRC_REASON_FLAG_REMOTE)
  1200. reason &= ~END_CIRC_REASON_FLAG_REMOTE;
  1201. if (reason < END_CIRC_REASON_MIN_ || reason > END_CIRC_REASON_MAX_) {
  1202. if (!(orig_reason & END_CIRC_REASON_FLAG_REMOTE))
  1203. log_warn(LD_BUG, "Reason %d out of range at %s:%d", reason, file, line);
  1204. reason = END_CIRC_REASON_NONE;
  1205. }
  1206. if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  1207. onion_pending_remove(TO_OR_CIRCUIT(circ));
  1208. }
  1209. /* If the circuit ever became OPEN, we sent it to the reputation history
  1210. * module then. If it isn't OPEN, we send it there now to remember which
  1211. * links worked and which didn't.
  1212. */
  1213. if (circ->state != CIRCUIT_STATE_OPEN) {
  1214. if (CIRCUIT_IS_ORIGIN(circ)) {
  1215. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1216. circuit_build_failed(ocirc); /* take actions if necessary */
  1217. circuit_rep_hist_note_result(ocirc);
  1218. }
  1219. }
  1220. if (circ->state == CIRCUIT_STATE_CHAN_WAIT) {
  1221. if (circuits_pending_chans)
  1222. smartlist_remove(circuits_pending_chans, circ);
  1223. }
  1224. if (CIRCUIT_IS_ORIGIN(circ)) {
  1225. control_event_circuit_status(TO_ORIGIN_CIRCUIT(circ),
  1226. (circ->state == CIRCUIT_STATE_OPEN)?CIRC_EVENT_CLOSED:CIRC_EVENT_FAILED,
  1227. orig_reason);
  1228. }
  1229. if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
  1230. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1231. int timed_out = (reason == END_CIRC_REASON_TIMEOUT);
  1232. tor_assert(circ->state == CIRCUIT_STATE_OPEN);
  1233. tor_assert(ocirc->build_state->chosen_exit);
  1234. tor_assert(ocirc->rend_data);
  1235. /* treat this like getting a nack from it */
  1236. log_info(LD_REND, "Failed intro circ %s to %s (awaiting ack). %s",
  1237. safe_str_client(ocirc->rend_data->onion_address),
  1238. safe_str_client(build_state_get_exit_nickname(ocirc->build_state)),
  1239. timed_out ? "Recording timeout." : "Removing from descriptor.");
  1240. rend_client_report_intro_point_failure(ocirc->build_state->chosen_exit,
  1241. ocirc->rend_data,
  1242. timed_out ?
  1243. INTRO_POINT_FAILURE_TIMEOUT :
  1244. INTRO_POINT_FAILURE_GENERIC);
  1245. } else if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCING &&
  1246. reason != END_CIRC_REASON_TIMEOUT) {
  1247. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1248. if (ocirc->build_state->chosen_exit && ocirc->rend_data) {
  1249. log_info(LD_REND, "Failed intro circ %s to %s "
  1250. "(building circuit to intro point). "
  1251. "Marking intro point as possibly unreachable.",
  1252. safe_str_client(ocirc->rend_data->onion_address),
  1253. safe_str_client(build_state_get_exit_nickname(ocirc->build_state)));
  1254. rend_client_report_intro_point_failure(ocirc->build_state->chosen_exit,
  1255. ocirc->rend_data,
  1256. INTRO_POINT_FAILURE_UNREACHABLE);
  1257. }
  1258. }
  1259. if (circ->n_chan) {
  1260. circuit_clear_cell_queue(circ, circ->n_chan);
  1261. channel_send_destroy(circ->n_circ_id, circ->n_chan, reason);
  1262. circuitmux_detach_circuit(circ->n_chan->cmux, circ);
  1263. }
  1264. if (! CIRCUIT_IS_ORIGIN(circ)) {
  1265. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  1266. edge_connection_t *conn;
  1267. for (conn=or_circ->n_streams; conn; conn=conn->next_stream)
  1268. connection_edge_destroy(or_circ->p_circ_id, conn);
  1269. or_circ->n_streams = NULL;
  1270. while (or_circ->resolving_streams) {
  1271. conn = or_circ->resolving_streams;
  1272. or_circ->resolving_streams = conn->next_stream;
  1273. if (!conn->base_.marked_for_close) {
  1274. /* The client will see a DESTROY, and infer that the connections
  1275. * are closing because the circuit is getting torn down. No need
  1276. * to send an end cell. */
  1277. conn->edge_has_sent_end = 1;
  1278. conn->end_reason = END_STREAM_REASON_DESTROY;
  1279. conn->end_reason |= END_STREAM_REASON_FLAG_ALREADY_SENT_CLOSED;
  1280. connection_mark_for_close(TO_CONN(conn));
  1281. }
  1282. conn->on_circuit = NULL;
  1283. }
  1284. if (or_circ->p_chan) {
  1285. circuit_clear_cell_queue(circ, or_circ->p_chan);
  1286. channel_send_destroy(or_circ->p_circ_id, or_circ->p_chan, reason);
  1287. circuitmux_detach_circuit(or_circ->p_chan->cmux, circ);
  1288. }
  1289. } else {
  1290. origin_circuit_t *ocirc = TO_ORIGIN_CIRCUIT(circ);
  1291. edge_connection_t *conn;
  1292. for (conn=ocirc->p_streams; conn; conn=conn->next_stream)
  1293. connection_edge_destroy(circ->n_circ_id, conn);
  1294. ocirc->p_streams = NULL;
  1295. }
  1296. circ->marked_for_close = line;
  1297. circ->marked_for_close_file = file;
  1298. if (!CIRCUIT_IS_ORIGIN(circ)) {
  1299. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  1300. if (or_circ->rend_splice) {
  1301. if (!or_circ->rend_splice->base_.marked_for_close) {
  1302. /* do this after marking this circuit, to avoid infinite recursion. */
  1303. circuit_mark_for_close(TO_CIRCUIT(or_circ->rend_splice), reason);
  1304. }
  1305. or_circ->rend_splice = NULL;
  1306. }
  1307. }
  1308. }
  1309. /** Verify that cpath layer <b>cp</b> has all of its invariants
  1310. * correct. Trigger an assert if anything is invalid.
  1311. */
  1312. void
  1313. assert_cpath_layer_ok(const crypt_path_t *cp)
  1314. {
  1315. // tor_assert(cp->addr); /* these are zero for rendezvous extra-hops */
  1316. // tor_assert(cp->port);
  1317. tor_assert(cp);
  1318. tor_assert(cp->magic == CRYPT_PATH_MAGIC);
  1319. switch (cp->state)
  1320. {
  1321. case CPATH_STATE_OPEN:
  1322. tor_assert(cp->f_crypto);
  1323. tor_assert(cp->b_crypto);
  1324. /* fall through */
  1325. case CPATH_STATE_CLOSED:
  1326. tor_assert(!cp->dh_handshake_state);
  1327. break;
  1328. case CPATH_STATE_AWAITING_KEYS:
  1329. /* tor_assert(cp->dh_handshake_state); */
  1330. break;
  1331. default:
  1332. log_fn(LOG_ERR, LD_BUG, "Unexpected state %d", cp->state);
  1333. tor_assert(0);
  1334. }
  1335. tor_assert(cp->package_window >= 0);
  1336. tor_assert(cp->deliver_window >= 0);
  1337. }
  1338. /** Verify that cpath <b>cp</b> has all of its invariants
  1339. * correct. Trigger an assert if anything is invalid.
  1340. */
  1341. static void
  1342. assert_cpath_ok(const crypt_path_t *cp)
  1343. {
  1344. const crypt_path_t *start = cp;
  1345. do {
  1346. assert_cpath_layer_ok(cp);
  1347. /* layers must be in sequence of: "open* awaiting? closed*" */
  1348. if (cp != start) {
  1349. if (cp->state == CPATH_STATE_AWAITING_KEYS) {
  1350. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  1351. } else if (cp->state == CPATH_STATE_OPEN) {
  1352. tor_assert(cp->prev->state == CPATH_STATE_OPEN);
  1353. }
  1354. }
  1355. cp = cp->next;
  1356. tor_assert(cp);
  1357. } while (cp != start);
  1358. }
  1359. /** Verify that circuit <b>c</b> has all of its invariants
  1360. * correct. Trigger an assert if anything is invalid.
  1361. */
  1362. void
  1363. assert_circuit_ok(const circuit_t *c)
  1364. {
  1365. edge_connection_t *conn;
  1366. const or_circuit_t *or_circ = NULL;
  1367. const origin_circuit_t *origin_circ = NULL;
  1368. tor_assert(c);
  1369. tor_assert(c->magic == ORIGIN_CIRCUIT_MAGIC || c->magic == OR_CIRCUIT_MAGIC);
  1370. tor_assert(c->purpose >= CIRCUIT_PURPOSE_MIN_ &&
  1371. c->purpose <= CIRCUIT_PURPOSE_MAX_);
  1372. {
  1373. /* Having a separate variable for this pleases GCC 4.2 in ways I hope I
  1374. * never understand. -NM. */
  1375. circuit_t *nonconst_circ = (circuit_t*) c;
  1376. if (CIRCUIT_IS_ORIGIN(c))
  1377. origin_circ = TO_ORIGIN_CIRCUIT(nonconst_circ);
  1378. else
  1379. or_circ = TO_OR_CIRCUIT(nonconst_circ);
  1380. }
  1381. if (c->n_chan) {
  1382. tor_assert(!c->n_hop);
  1383. if (c->n_circ_id) {
  1384. /* We use the _impl variant here to make sure we don't fail on marked
  1385. * circuits, which would not be returned by the regular function. */
  1386. circuit_t *c2 = circuit_get_by_circid_channel_impl(c->n_circ_id,
  1387. c->n_chan);
  1388. tor_assert(c == c2);
  1389. }
  1390. }
  1391. if (or_circ && or_circ->p_chan) {
  1392. if (or_circ->p_circ_id) {
  1393. /* ibid */
  1394. circuit_t *c2 = circuit_get_by_circid_channel_impl(or_circ->p_circ_id,
  1395. or_circ->p_chan);
  1396. tor_assert(c == c2);
  1397. }
  1398. }
  1399. if (or_circ)
  1400. for (conn = or_circ->n_streams; conn; conn = conn->next_stream)
  1401. tor_assert(conn->base_.type == CONN_TYPE_EXIT);
  1402. tor_assert(c->deliver_window >= 0);
  1403. tor_assert(c->package_window >= 0);
  1404. if (c->state == CIRCUIT_STATE_OPEN) {
  1405. tor_assert(!c->n_chan_onionskin);
  1406. if (or_circ) {
  1407. tor_assert(or_circ->n_crypto);
  1408. tor_assert(or_circ->p_crypto);
  1409. tor_assert(or_circ->n_digest);
  1410. tor_assert(or_circ->p_digest);
  1411. }
  1412. }
  1413. if (c->state == CIRCUIT_STATE_CHAN_WAIT && !c->marked_for_close) {
  1414. tor_assert(circuits_pending_chans &&
  1415. smartlist_isin(circuits_pending_chans, c));
  1416. } else {
  1417. tor_assert(!circuits_pending_chans ||
  1418. !smartlist_isin(circuits_pending_chans, c));
  1419. }
  1420. if (origin_circ && origin_circ->cpath) {
  1421. assert_cpath_ok(origin_circ->cpath);
  1422. }
  1423. if (c->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED) {
  1424. tor_assert(or_circ);
  1425. if (!c->marked_for_close) {
  1426. tor_assert(or_circ->rend_splice);
  1427. tor_assert(or_circ->rend_splice->rend_splice == or_circ);
  1428. }
  1429. tor_assert(or_circ->rend_splice != or_circ);
  1430. } else {
  1431. tor_assert(!or_circ || !or_circ->rend_splice);
  1432. }
  1433. }