circuit.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736
  1. /* Copyright 2001 Matej Pfajfar, 2001-2004 Roger Dingledine. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. /**
  5. * \file circuit.c
  6. * \brief Manage circuits and the global circuit list.
  7. **/
  8. #include "or.h"
  9. extern or_options_t options; /* command-line and config-file options */
  10. static int circuit_resume_edge_reading_helper(connection_t *conn,
  11. circuit_t *circ,
  12. crypt_path_t *layer_hint);
  13. static void circuit_free_cpath_node(crypt_path_t *victim);
  14. static uint16_t get_unique_circ_id_by_conn(connection_t *conn, int circ_id_type);
  15. static void circuit_rep_hist_note_result(circuit_t *circ);
  16. void circuit_expire_old_circuits(void);
  17. static void circuit_is_open(circuit_t *circ);
  18. static void circuit_build_failed(circuit_t *circ);
  19. static circuit_t *circuit_establish_circuit(uint8_t purpose, const char *exit_nickname);
  20. static void circuit_free(circuit_t *circ);
  21. static void circuit_free_cpath(crypt_path_t *cpath);
  22. /********* START VARIABLES **********/
  23. /** A global (within this file) list of all circuits at this hop. */
  24. static circuit_t *global_circuitlist=NULL;
  25. /** How many entries are in global_circuitlist? */
  26. static int circuitlist_len=0;
  27. /** Array of strings to make circ-\>state human-readable */
  28. char *circuit_state_to_string[] = {
  29. "doing handshakes", /* 0 */
  30. "processing the onion", /* 1 */
  31. "connecting to firsthop", /* 2 */
  32. "open" /* 3 */
  33. };
  34. /********* END VARIABLES ************/
  35. /** Add <b>circ</b> to the global list of circuits. This is called only from
  36. * within circuit_new.
  37. */
  38. static void circuit_add(circuit_t *circ) {
  39. if(!global_circuitlist) { /* first one */
  40. global_circuitlist = circ;
  41. circ->next = NULL;
  42. } else {
  43. circ->next = global_circuitlist;
  44. global_circuitlist = circ;
  45. }
  46. ++circuitlist_len;
  47. }
  48. /** Detach from the global circuit list, and deallocate, all
  49. * circuits that have been marked for close.
  50. */
  51. void circuit_close_all_marked()
  52. {
  53. circuit_t *tmp,*m;
  54. while (global_circuitlist && global_circuitlist->marked_for_close) {
  55. tmp = global_circuitlist->next;
  56. circuit_free(global_circuitlist);
  57. global_circuitlist = tmp;
  58. }
  59. tmp = global_circuitlist;
  60. while (tmp && tmp->next) {
  61. if (tmp->next->marked_for_close) {
  62. m = tmp->next->next;
  63. circuit_free(tmp->next);
  64. tmp->next = m;
  65. /* Need to check new tmp->next; don't advance tmp. */
  66. } else {
  67. /* Advance tmp. */
  68. tmp = tmp->next;
  69. }
  70. }
  71. }
  72. /** Allocate space for a new circuit, initializing with <b>p_circ_id</b>
  73. * and <b>p_conn</b>. Add it to the global circuit list.
  74. */
  75. circuit_t *circuit_new(uint16_t p_circ_id, connection_t *p_conn) {
  76. circuit_t *circ;
  77. circ = tor_malloc_zero(sizeof(circuit_t));
  78. circ->magic = CIRCUIT_MAGIC;
  79. circ->timestamp_created = time(NULL);
  80. circ->p_circ_id = p_circ_id;
  81. circ->p_conn = p_conn;
  82. circ->state = CIRCUIT_STATE_ONIONSKIN_PENDING;
  83. /* CircIDs */
  84. circ->p_circ_id = p_circ_id;
  85. /* circ->n_circ_id remains 0 because we haven't identified the next hop yet */
  86. circ->package_window = CIRCWINDOW_START;
  87. circ->deliver_window = CIRCWINDOW_START;
  88. circ->next_stream_id = crypto_pseudo_rand_int(1<<16);
  89. circuit_add(circ);
  90. return circ;
  91. }
  92. /** Deallocate space associated with circ.
  93. */
  94. static void circuit_free(circuit_t *circ) {
  95. tor_assert(circ);
  96. tor_assert(circ->magic == CIRCUIT_MAGIC);
  97. if (circ->n_crypto)
  98. crypto_free_cipher_env(circ->n_crypto);
  99. if (circ->p_crypto)
  100. crypto_free_cipher_env(circ->p_crypto);
  101. if (circ->n_digest)
  102. crypto_free_digest_env(circ->n_digest);
  103. if (circ->p_digest)
  104. crypto_free_digest_env(circ->p_digest);
  105. if(circ->build_state) {
  106. tor_free(circ->build_state->chosen_exit);
  107. if (circ->build_state->pending_final_cpath)
  108. circuit_free_cpath_node(circ->build_state->pending_final_cpath);
  109. }
  110. tor_free(circ->build_state);
  111. circuit_free_cpath(circ->cpath);
  112. if (circ->rend_splice) {
  113. circ->rend_splice->rend_splice = NULL;
  114. }
  115. memset(circ, 0xAA, sizeof(circuit_t)); /* poison memory */
  116. free(circ);
  117. }
  118. /** Deallocate space associated with the linked list <b>cpath</b>. */
  119. static void circuit_free_cpath(crypt_path_t *cpath) {
  120. crypt_path_t *victim, *head=cpath;
  121. if(!cpath)
  122. return;
  123. /* it's a doubly linked list, so we have to notice when we've
  124. * gone through it once. */
  125. while(cpath->next && cpath->next != head) {
  126. victim = cpath;
  127. cpath = victim->next;
  128. circuit_free_cpath_node(victim);
  129. }
  130. circuit_free_cpath_node(cpath);
  131. }
  132. /** Deallocate space associated with the cpath node <b>victim</b>. */
  133. static void circuit_free_cpath_node(crypt_path_t *victim) {
  134. if(victim->f_crypto)
  135. crypto_free_cipher_env(victim->f_crypto);
  136. if(victim->b_crypto)
  137. crypto_free_cipher_env(victim->b_crypto);
  138. if(victim->f_digest)
  139. crypto_free_digest_env(victim->f_digest);
  140. if(victim->b_digest)
  141. crypto_free_digest_env(victim->b_digest);
  142. if(victim->handshake_state)
  143. crypto_dh_free(victim->handshake_state);
  144. free(victim);
  145. }
  146. /** Iterate over values of circ_id, starting from conn-\>next_circ_id,
  147. * and with the high bit specified by circ_id_type (see
  148. * decide_circ_id_type()), until we get a circ_id that is not in use
  149. * by any other circuit on that conn.
  150. *
  151. * Return it, or 0 if can't get a unique circ_id.
  152. */
  153. static uint16_t get_unique_circ_id_by_conn(connection_t *conn, int circ_id_type) {
  154. uint16_t test_circ_id;
  155. int attempts=0;
  156. uint16_t high_bit;
  157. tor_assert(conn && conn->type == CONN_TYPE_OR);
  158. high_bit = (circ_id_type == CIRC_ID_TYPE_HIGHER) ? 1<<15 : 0;
  159. do {
  160. /* Sequentially iterate over test_circ_id=1...1<<15-1 until we find a
  161. * circID such that (high_bit|test_circ_id) is not already used. */
  162. test_circ_id = conn->next_circ_id++;
  163. if (test_circ_id == 0 || test_circ_id >= 1<<15) {
  164. test_circ_id = 1;
  165. conn->next_circ_id = 2;
  166. }
  167. if(++attempts > 1<<15) {
  168. /* Make sure we don't loop forever if all circ_id's are used. This
  169. * matters because it's an external DoS vulnerability.
  170. */
  171. log_fn(LOG_WARN,"No unused circ IDs. Failing.");
  172. return 0;
  173. }
  174. test_circ_id |= high_bit;
  175. } while(circuit_get_by_circ_id_conn(test_circ_id, conn));
  176. return test_circ_id;
  177. }
  178. /** Return a circ such that:
  179. * - circ-\>n_circ_id or circ-\>p_circ_id is equal to <b>circ_id</b>, and
  180. * - circ is attached to <b>conn</b>, either as p_conn, n-conn, or
  181. * in p_streams or n_streams.
  182. * Return NULL if no such circuit exists.
  183. */
  184. circuit_t *circuit_get_by_circ_id_conn(uint16_t circ_id, connection_t *conn) {
  185. circuit_t *circ;
  186. connection_t *tmpconn;
  187. for(circ=global_circuitlist;circ;circ = circ->next) {
  188. if (circ->marked_for_close)
  189. continue;
  190. if(circ->p_circ_id == circ_id) {
  191. if(circ->p_conn == conn)
  192. return circ;
  193. for(tmpconn = circ->p_streams; tmpconn; tmpconn = tmpconn->next_stream) {
  194. if(tmpconn == conn)
  195. return circ;
  196. }
  197. }
  198. if(circ->n_circ_id == circ_id) {
  199. if(circ->n_conn == conn)
  200. return circ;
  201. for(tmpconn = circ->n_streams; tmpconn; tmpconn = tmpconn->next_stream) {
  202. if(tmpconn == conn)
  203. return circ;
  204. }
  205. for(tmpconn = circ->resolving_streams; tmpconn; tmpconn = tmpconn->next_stream) {
  206. if(tmpconn == conn)
  207. return circ;
  208. }
  209. }
  210. }
  211. return NULL;
  212. }
  213. /** Return a circ such that circ is attached to <b>conn</b>, either as
  214. * p_conn, n-conn, or in p_streams or n_streams.
  215. *
  216. * Return NULL if no such circuit exists.
  217. */
  218. circuit_t *circuit_get_by_conn(connection_t *conn) {
  219. circuit_t *circ;
  220. connection_t *tmpconn;
  221. for(circ=global_circuitlist;circ;circ = circ->next) {
  222. if (circ->marked_for_close)
  223. continue;
  224. if(circ->p_conn == conn)
  225. return circ;
  226. if(circ->n_conn == conn)
  227. return circ;
  228. for(tmpconn = circ->p_streams; tmpconn; tmpconn=tmpconn->next_stream)
  229. if(tmpconn == conn)
  230. return circ;
  231. for(tmpconn = circ->n_streams; tmpconn; tmpconn=tmpconn->next_stream)
  232. if(tmpconn == conn)
  233. return circ;
  234. for(tmpconn = circ->resolving_streams; tmpconn; tmpconn=tmpconn->next_stream)
  235. if(tmpconn == conn)
  236. return circ;
  237. }
  238. return NULL;
  239. }
  240. /* Return 1 if <b>circ</b> could be returned by circuit_get_best().
  241. * Else return 0.
  242. */
  243. static int circuit_is_acceptable(circuit_t *circ,
  244. connection_t *conn,
  245. int must_be_open,
  246. uint8_t purpose,
  247. time_t now)
  248. {
  249. routerinfo_t *exitrouter;
  250. if (!CIRCUIT_IS_ORIGIN(circ))
  251. return 0; /* this circ doesn't start at us */
  252. if (must_be_open && (circ->state != CIRCUIT_STATE_OPEN || !circ->n_conn))
  253. return 0; /* ignore non-open circs */
  254. if (circ->marked_for_close)
  255. return 0;
  256. /* if this circ isn't our purpose, skip. */
  257. if(purpose == CIRCUIT_PURPOSE_C_REND_JOINED && !must_be_open) {
  258. if(circ->purpose != CIRCUIT_PURPOSE_C_ESTABLISH_REND &&
  259. circ->purpose != CIRCUIT_PURPOSE_C_REND_READY &&
  260. circ->purpose != CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED &&
  261. circ->purpose != CIRCUIT_PURPOSE_C_REND_JOINED)
  262. return 0;
  263. } else if (purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT && !must_be_open) {
  264. if (circ->purpose != CIRCUIT_PURPOSE_C_INTRODUCING &&
  265. circ->purpose != CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT)
  266. return 0;
  267. } else {
  268. if(purpose != circ->purpose)
  269. return 0;
  270. }
  271. if(purpose == CIRCUIT_PURPOSE_C_GENERAL)
  272. if(circ->timestamp_dirty &&
  273. circ->timestamp_dirty+options.NewCircuitPeriod < now)
  274. return 0;
  275. if(conn) {
  276. /* decide if this circ is suitable for this conn */
  277. /* for rend circs, circ->cpath->prev is not the last router in the
  278. * circuit, it's the magical extra bob hop. so just check the nickname
  279. * of the one we meant to finish at.
  280. */
  281. exitrouter = router_get_by_nickname(circ->build_state->chosen_exit);
  282. if(!exitrouter) {
  283. log_fn(LOG_INFO,"Skipping broken circ (exit router vanished)");
  284. return 0; /* this circuit is screwed and doesn't know it yet */
  285. }
  286. if(purpose == CIRCUIT_PURPOSE_C_GENERAL) {
  287. if(connection_ap_can_use_exit(conn, exitrouter) == ADDR_POLICY_REJECTED) {
  288. /* can't exit from this router */
  289. return 0;
  290. }
  291. } else { /* not general */
  292. if(rend_cmp_service_ids(conn->rend_query, circ->rend_query) &&
  293. (circ->rend_query[0] || purpose != CIRCUIT_PURPOSE_C_REND_JOINED)) {
  294. /* this circ is not for this conn, and it's not suitable
  295. * for cannibalizing either */
  296. return 0;
  297. }
  298. }
  299. }
  300. return 1;
  301. }
  302. /* Return 1 if circuit <b>a</b> is better than circuit <b>b</b> for
  303. * <b>purpose</b>, and return 0 otherwise. Used by circuit_get_best.
  304. */
  305. static int circuit_is_better(circuit_t *a, circuit_t *b, uint8_t purpose)
  306. {
  307. switch(purpose) {
  308. case CIRCUIT_PURPOSE_C_GENERAL:
  309. /* if it's used but less dirty it's best;
  310. * else if it's more recently created it's best
  311. */
  312. if(b->timestamp_dirty) {
  313. if(a->timestamp_dirty &&
  314. a->timestamp_dirty > b->timestamp_dirty)
  315. return 1;
  316. } else {
  317. if(a->timestamp_dirty ||
  318. a->timestamp_created > b->timestamp_created)
  319. return 1;
  320. }
  321. break;
  322. case CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT:
  323. /* the closer it is to ack_wait the better it is */
  324. if(a->purpose > b->purpose)
  325. return 1;
  326. break;
  327. case CIRCUIT_PURPOSE_C_REND_JOINED:
  328. /* the closer it is to rend_joined the better it is */
  329. if(a->purpose > b->purpose)
  330. return 1;
  331. break;
  332. }
  333. return 0;
  334. }
  335. /** Find the best circ that conn can use, preferably one which is
  336. * dirty. Circ must not be too old.
  337. *
  338. * Conn must be defined.
  339. *
  340. * If must_be_open, ignore circs not in CIRCUIT_STATE_OPEN.
  341. *
  342. * circ_purpose specifies what sort of circuit we must have.
  343. * It can be C_GENERAL, C_INTRODUCE_ACK_WAIT, or C_REND_JOINED.
  344. *
  345. * If it's REND_JOINED and must_be_open==0, then return the closest
  346. * rendezvous-purposed circuit that you can find.
  347. *
  348. * If it's INTRODUCE_ACK_WAIT and must_be_open==0, then return the
  349. * closest introduce-purposed circuit that you can find.
  350. */
  351. circuit_t *circuit_get_best(connection_t *conn,
  352. int must_be_open, uint8_t purpose) {
  353. circuit_t *circ, *best=NULL;
  354. time_t now = time(NULL);
  355. tor_assert(conn);
  356. tor_assert(purpose == CIRCUIT_PURPOSE_C_GENERAL ||
  357. purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT ||
  358. purpose == CIRCUIT_PURPOSE_C_REND_JOINED);
  359. for (circ=global_circuitlist;circ;circ = circ->next) {
  360. if (!circuit_is_acceptable(circ,conn,must_be_open,purpose,now))
  361. continue;
  362. /* now this is an acceptable circ to hand back. but that doesn't
  363. * mean it's the *best* circ to hand back. try to decide.
  364. */
  365. if(!best || circuit_is_better(circ,best,purpose))
  366. best = circ;
  367. }
  368. return best;
  369. }
  370. /** Return a circ such that:
  371. * - circ-\>rend_query is equal to <b>rend_query</b>, and
  372. * - circ-\>purpose is equal to <b>purpose</b>.
  373. *
  374. * Return NULL if no such circuit exists.
  375. */
  376. circuit_t *circuit_get_by_rend_query_and_purpose(const char *rend_query, uint8_t purpose) {
  377. circuit_t *circ;
  378. for (circ = global_circuitlist; circ; circ = circ->next) {
  379. if (!circ->marked_for_close &&
  380. circ->purpose == purpose &&
  381. !rend_cmp_service_ids(rend_query, circ->rend_query))
  382. return circ;
  383. }
  384. return NULL;
  385. }
  386. /** Return the first circuit in global_circuitlist after <b>start</b> whose
  387. * rend_pk_digest field is <b>digest</b> and whose purpose is <b>purpose</b>. Returns
  388. * NULL if no circuit is found. If <b>start</b> is NULL, begin at the start of
  389. * the list.
  390. */
  391. circuit_t *circuit_get_next_by_pk_and_purpose(circuit_t *start,
  392. const char *digest, uint8_t purpose)
  393. {
  394. circuit_t *circ;
  395. if (start == NULL)
  396. circ = global_circuitlist;
  397. else
  398. circ = start->next;
  399. for( ; circ; circ = circ->next) {
  400. if (circ->marked_for_close)
  401. continue;
  402. if (circ->purpose != purpose)
  403. continue;
  404. if (!memcmp(circ->rend_pk_digest, digest, DIGEST_LEN))
  405. return circ;
  406. }
  407. return NULL;
  408. }
  409. /** Return the circuit waiting for a rendezvous with the provided cookie.
  410. * Return NULL if no such circuit is found.
  411. */
  412. circuit_t *circuit_get_rendezvous(const char *cookie)
  413. {
  414. circuit_t *circ;
  415. for (circ = global_circuitlist; circ; circ = circ->next) {
  416. if (! circ->marked_for_close &&
  417. circ->purpose == CIRCUIT_PURPOSE_REND_POINT_WAITING &&
  418. ! memcmp(circ->rend_cookie, cookie, REND_COOKIE_LEN) )
  419. return circ;
  420. }
  421. return NULL;
  422. }
  423. /** Circuits that were born at the end of their second might be expired
  424. * after 30.1 seconds; circuits born at the beginning might be expired
  425. * after closer to 31 seconds.
  426. */
  427. #define MIN_SECONDS_BEFORE_EXPIRING_CIRC 30
  428. /** Close all circuits that start at us, aren't open, and were born
  429. * at least MIN_SECONDS_BEFORE_EXPIRING_CIRC seconds ago.
  430. */
  431. void circuit_expire_building(time_t now) {
  432. circuit_t *victim, *circ = global_circuitlist;
  433. while(circ) {
  434. victim = circ;
  435. circ = circ->next;
  436. if(!CIRCUIT_IS_ORIGIN(victim))
  437. continue; /* didn't originate here */
  438. if(victim->marked_for_close)
  439. continue; /* don't mess with marked circs */
  440. if(victim->timestamp_created + MIN_SECONDS_BEFORE_EXPIRING_CIRC > now)
  441. continue; /* it's young still, don't mess with it */
  442. /* some debug logs, to help track bugs */
  443. if(victim->purpose >= CIRCUIT_PURPOSE_C_INTRODUCING &&
  444. victim->purpose <= CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED) {
  445. if(!victim->timestamp_dirty)
  446. log_fn(LOG_DEBUG,"Considering %sopen purp %d to %s (circid %d). (clean).",
  447. victim->state == CIRCUIT_STATE_OPEN ? "" : "non",
  448. victim->purpose, victim->build_state->chosen_exit,
  449. victim->n_circ_id);
  450. else
  451. log_fn(LOG_DEBUG,"Considering %sopen purp %d to %s (circid %d). %d secs since dirty.",
  452. victim->state == CIRCUIT_STATE_OPEN ? "" : "non",
  453. victim->purpose, victim->build_state->chosen_exit,
  454. victim->n_circ_id,
  455. (int)(now - victim->timestamp_dirty));
  456. }
  457. /* if circ is !open, or if it's open but purpose is a non-finished
  458. * intro or rend, then mark it for close */
  459. if(victim->state != CIRCUIT_STATE_OPEN ||
  460. victim->purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND ||
  461. victim->purpose == CIRCUIT_PURPOSE_C_INTRODUCING ||
  462. victim->purpose == CIRCUIT_PURPOSE_S_ESTABLISH_INTRO ||
  463. /* it's a rend_ready circ, but it's already picked a query */
  464. (victim->purpose == CIRCUIT_PURPOSE_C_REND_READY &&
  465. victim->rend_query[0]) ||
  466. /* c_rend_ready circs measure age since timestamp_dirty,
  467. * because that's set when they switch purposes
  468. */
  469. /* rend and intro circs become dirty each time they
  470. * make an introduction attempt. so timestamp_dirty
  471. * will reflect the time since the last attempt.
  472. */
  473. ((victim->purpose == CIRCUIT_PURPOSE_C_REND_READY ||
  474. victim->purpose == CIRCUIT_PURPOSE_C_REND_READY_INTRO_ACKED ||
  475. victim->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) &&
  476. victim->timestamp_dirty + MIN_SECONDS_BEFORE_EXPIRING_CIRC > now)) {
  477. if(victim->n_conn)
  478. log_fn(LOG_INFO,"Abandoning circ %s:%d:%d (state %d:%s, purpose %d)",
  479. victim->n_conn->address, victim->n_port, victim->n_circ_id,
  480. victim->state, circuit_state_to_string[victim->state], victim->purpose);
  481. else
  482. log_fn(LOG_INFO,"Abandoning circ %d (state %d:%s, purpose %d)", victim->n_circ_id,
  483. victim->state, circuit_state_to_string[victim->state], victim->purpose);
  484. circuit_log_path(LOG_INFO,victim);
  485. circuit_mark_for_close(victim);
  486. }
  487. }
  488. }
  489. /** Count the number of circs originating here that aren't open, and
  490. * that have the specified <b>purpose</b>. */
  491. int circuit_count_building(uint8_t purpose) {
  492. circuit_t *circ;
  493. int num=0;
  494. for(circ=global_circuitlist;circ;circ = circ->next) {
  495. if(CIRCUIT_IS_ORIGIN(circ) &&
  496. circ->state != CIRCUIT_STATE_OPEN &&
  497. circ->purpose == purpose &&
  498. !circ->marked_for_close)
  499. num++;
  500. }
  501. return num;
  502. }
  503. /** How many circuits do we want simultaneously in-progress to handle
  504. * a given stream?
  505. */
  506. #define MIN_CIRCUITS_HANDLING_STREAM 2
  507. /** Return 1 if at least MIN_CIRCUITS_HANDLING_STREAM non-open
  508. * general-purpose circuits will have an acceptable exit node for
  509. * conn. Else return 0.
  510. */
  511. int circuit_stream_is_being_handled(connection_t *conn) {
  512. circuit_t *circ;
  513. routerinfo_t *exitrouter;
  514. int num=0;
  515. time_t now = time(NULL);
  516. for(circ=global_circuitlist;circ;circ = circ->next) {
  517. if(CIRCUIT_IS_ORIGIN(circ) && circ->state != CIRCUIT_STATE_OPEN &&
  518. !circ->marked_for_close && circ->purpose == CIRCUIT_PURPOSE_C_GENERAL &&
  519. (!circ->timestamp_dirty ||
  520. circ->timestamp_dirty + options.NewCircuitPeriod < now)) {
  521. exitrouter = router_get_by_nickname(circ->build_state->chosen_exit);
  522. if(exitrouter && connection_ap_can_use_exit(conn, exitrouter) != ADDR_POLICY_REJECTED)
  523. if(++num >= MIN_CIRCUITS_HANDLING_STREAM)
  524. return 1;
  525. }
  526. }
  527. return 0;
  528. }
  529. /** Return the circuit that is open, has specified <b>purpose</b>,
  530. * has a timestamp_dirty value of 0, and was created most recently,
  531. * or NULL if no circuit fits this description.
  532. */
  533. static circuit_t *
  534. circuit_get_youngest_clean_open(uint8_t purpose) {
  535. circuit_t *circ;
  536. circuit_t *youngest=NULL;
  537. for(circ=global_circuitlist;circ;circ = circ->next) {
  538. if(CIRCUIT_IS_ORIGIN(circ) && circ->state == CIRCUIT_STATE_OPEN &&
  539. !circ->marked_for_close && circ->purpose == purpose &&
  540. !circ->timestamp_dirty &&
  541. (!youngest || youngest->timestamp_created < circ->timestamp_created))
  542. youngest = circ;
  543. }
  544. return youngest;
  545. }
  546. /** Build a new test circuit every 5 minutes */
  547. #define TESTING_CIRCUIT_INTERVAL 300
  548. /** This function is called once a second. Its job is to make sure
  549. * all services we offer have enough circuits available. Some
  550. * services just want enough circuits for current tasks, whereas
  551. * others want a minimum set of idle circuits hanging around.
  552. */
  553. void circuit_build_needed_circs(time_t now) {
  554. static long time_to_new_circuit = 0;
  555. circuit_t *circ;
  556. /* launch a new circ for any pending streams that need one */
  557. connection_ap_attach_pending();
  558. /* make sure any hidden services have enough intro points */
  559. rend_services_introduce();
  560. circ = circuit_get_youngest_clean_open(CIRCUIT_PURPOSE_C_GENERAL);
  561. if(time_to_new_circuit < now) {
  562. circuit_reset_failure_count();
  563. time_to_new_circuit = now + options.NewCircuitPeriod;
  564. if(options.SocksPort)
  565. client_dns_clean();
  566. circuit_expire_old_circuits();
  567. if(options.RunTesting && circ &&
  568. circ->timestamp_created + TESTING_CIRCUIT_INTERVAL < now) {
  569. log_fn(LOG_INFO,"Creating a new testing circuit.");
  570. circuit_launch_new(CIRCUIT_PURPOSE_C_GENERAL, NULL);
  571. }
  572. }
  573. /** How many simultaneous in-progress general-purpose circuits do we
  574. * want to be building at once, if there are no open general-purpose
  575. * circuits?
  576. */
  577. #define CIRCUIT_MIN_BUILDING_GENERAL 3
  578. /* if there's no open circ, and less than 3 are on the way,
  579. * go ahead and try another. */
  580. if(!circ && circuit_count_building(CIRCUIT_PURPOSE_C_GENERAL)
  581. < CIRCUIT_MIN_BUILDING_GENERAL) {
  582. circuit_launch_new(CIRCUIT_PURPOSE_C_GENERAL, NULL);
  583. }
  584. /* XXX count idle rendezvous circs and build more */
  585. }
  586. /** The circuit <b>circ</b> has received a circuit-level sendme
  587. * (on hop <b>layer_hint</b>, if we're the OP). Go through all the
  588. * attached streams and let them resume reading and packaging, if
  589. * their stream windows allow it.
  590. */
  591. void circuit_resume_edge_reading(circuit_t *circ, crypt_path_t *layer_hint) {
  592. log_fn(LOG_DEBUG,"resuming");
  593. /* have to check both n_streams and p_streams, to handle rendezvous */
  594. if(circuit_resume_edge_reading_helper(circ->n_streams, circ, layer_hint) >= 0)
  595. circuit_resume_edge_reading_helper(circ->p_streams, circ, layer_hint);
  596. }
  597. /** A helper function for circuit_resume_edge_reading() above.
  598. * The arguments are the same, except that <b>conn</b> is the head
  599. * of a linked list of edge streams that should each be considered.
  600. */
  601. static int
  602. circuit_resume_edge_reading_helper(connection_t *conn,
  603. circuit_t *circ,
  604. crypt_path_t *layer_hint) {
  605. for( ; conn; conn=conn->next_stream) {
  606. if((!layer_hint && conn->package_window > 0) ||
  607. (layer_hint && conn->package_window > 0 && conn->cpath_layer == layer_hint)) {
  608. connection_start_reading(conn);
  609. /* handle whatever might still be on the inbuf */
  610. connection_edge_package_raw_inbuf(conn);
  611. /* If the circuit won't accept any more data, return without looking
  612. * at any more of the streams. Any connections that should be stopped
  613. * have already been stopped by connection_edge_package_raw_inbuf. */
  614. if(circuit_consider_stop_edge_reading(circ, layer_hint))
  615. return -1;
  616. }
  617. }
  618. return 0;
  619. }
  620. /** Check if the package window for <b>circ</b> is empty (at
  621. * hop <b>layer_hint</b> if it's defined).
  622. *
  623. * If yes, tell edge streams to stop reading and return -1.
  624. * Else return 0.
  625. */
  626. int circuit_consider_stop_edge_reading(circuit_t *circ, crypt_path_t *layer_hint) {
  627. connection_t *conn = NULL;
  628. log_fn(LOG_DEBUG,"considering");
  629. if(!layer_hint && circ->package_window <= 0) {
  630. log_fn(LOG_DEBUG,"yes, not-at-origin. stopped.");
  631. for(conn = circ->n_streams; conn; conn=conn->next_stream)
  632. connection_stop_reading(conn);
  633. return -1;
  634. } else if(layer_hint && layer_hint->package_window <= 0) {
  635. log_fn(LOG_DEBUG,"yes, at-origin. stopped.");
  636. for(conn = circ->n_streams; conn; conn=conn->next_stream)
  637. if(conn->cpath_layer == layer_hint)
  638. connection_stop_reading(conn);
  639. for(conn = circ->p_streams; conn; conn=conn->next_stream)
  640. if(conn->cpath_layer == layer_hint)
  641. connection_stop_reading(conn);
  642. return -1;
  643. }
  644. return 0;
  645. }
  646. /** Check if the deliver_window for circuit <b>circ</b> (at hop
  647. * <b>layer_hint</b> if it's defined) is low enough that we should
  648. * send a circuit-level sendme back down the circuit. If so, send
  649. * enough sendmes that the window would be overfull if we sent any
  650. * more.
  651. */
  652. void circuit_consider_sending_sendme(circuit_t *circ, crypt_path_t *layer_hint) {
  653. // log_fn(LOG_INFO,"Considering: layer_hint is %s",
  654. // layer_hint ? "defined" : "null");
  655. while((layer_hint ? layer_hint->deliver_window : circ->deliver_window) <
  656. CIRCWINDOW_START - CIRCWINDOW_INCREMENT) {
  657. log_fn(LOG_DEBUG,"Queueing circuit sendme.");
  658. if(layer_hint)
  659. layer_hint->deliver_window += CIRCWINDOW_INCREMENT;
  660. else
  661. circ->deliver_window += CIRCWINDOW_INCREMENT;
  662. if(connection_edge_send_command(NULL, circ, RELAY_COMMAND_SENDME,
  663. NULL, 0, layer_hint) < 0) {
  664. log_fn(LOG_WARN,"connection_edge_send_command failed. Circuit's closed.");
  665. return; /* the circuit's closed, don't continue */
  666. }
  667. }
  668. }
  669. /** Mark <b>circ</b> to be closed next time we call
  670. * circuit_close_all_marked(). Do any cleanup needed:
  671. * - If state is onionskin_pending, remove circ from the onion_pending
  672. * list.
  673. * - If circ isn't open yet, call circuit_build_failed() if we're
  674. * the origin, and in case call circuit_rep_hist_note_result()
  675. * to note stats.
  676. * - If purpose is C_INTRODUCE_ACK_WAIT, remove the intro point we
  677. * just tried from our list of intro points for that service
  678. * descriptor.
  679. * - Send appropriate destroys and edge_destroys for conns and
  680. * streams attached to circ.
  681. * - If circ->rend_splice is set (we are the midpoint of a joined
  682. * rendezvous stream), then mark the other circuit to close as well.
  683. */
  684. int _circuit_mark_for_close(circuit_t *circ) {
  685. connection_t *conn;
  686. assert_circuit_ok(circ);
  687. if (circ->marked_for_close)
  688. return -1;
  689. if(circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  690. onion_pending_remove(circ);
  691. }
  692. /* If the circuit ever became OPEN, we sent it to the reputation history
  693. * module then. If it isn't OPEN, we send it there now to remember which
  694. * links worked and which didn't.
  695. */
  696. if (circ->state != CIRCUIT_STATE_OPEN) {
  697. if(CIRCUIT_IS_ORIGIN(circ))
  698. circuit_build_failed(circ); /* take actions if necessary */
  699. circuit_rep_hist_note_result(circ);
  700. }
  701. if (circ->purpose == CIRCUIT_PURPOSE_C_INTRODUCE_ACK_WAIT) {
  702. tor_assert(circ->state == CIRCUIT_STATE_OPEN);
  703. /* treat this like getting a nack from it */
  704. log_fn(LOG_INFO,"Failed intro circ %s to %s (awaiting ack). Removing from descriptor.",
  705. circ->rend_query, circ->build_state->chosen_exit);
  706. rend_client_remove_intro_point(circ->build_state->chosen_exit, circ->rend_query);
  707. }
  708. if(circ->n_conn)
  709. connection_send_destroy(circ->n_circ_id, circ->n_conn);
  710. for(conn=circ->n_streams; conn; conn=conn->next_stream)
  711. connection_edge_destroy(circ->n_circ_id, conn);
  712. while(circ->resolving_streams) {
  713. conn = circ->resolving_streams;
  714. circ->resolving_streams = conn->next_stream;
  715. log_fn(LOG_INFO,"Freeing resolving-conn.");
  716. connection_free(conn);
  717. }
  718. if(circ->p_conn)
  719. connection_send_destroy(circ->p_circ_id, circ->p_conn);
  720. for(conn=circ->p_streams; conn; conn=conn->next_stream)
  721. connection_edge_destroy(circ->p_circ_id, conn);
  722. circ->marked_for_close = 1;
  723. if (circ->rend_splice && !circ->rend_splice->marked_for_close) {
  724. /* do this after marking this circuit, to avoid infinite recursion. */
  725. circuit_mark_for_close(circ->rend_splice);
  726. circ->rend_splice = NULL;
  727. }
  728. return 0;
  729. }
  730. /** If the stream <b>conn</b> is a member of any of the linked
  731. * lists of <b>circ</b>, then remove it from the list.
  732. */
  733. void circuit_detach_stream(circuit_t *circ, connection_t *conn) {
  734. connection_t *prevconn;
  735. tor_assert(circ && conn);
  736. if(conn == circ->p_streams) {
  737. circ->p_streams = conn->next_stream;
  738. return;
  739. }
  740. if(conn == circ->n_streams) {
  741. circ->n_streams = conn->next_stream;
  742. return;
  743. }
  744. if(conn == circ->resolving_streams) {
  745. circ->resolving_streams = conn->next_stream;
  746. return;
  747. }
  748. for(prevconn = circ->p_streams;
  749. prevconn && prevconn->next_stream && prevconn->next_stream != conn;
  750. prevconn = prevconn->next_stream)
  751. ;
  752. if(prevconn && prevconn->next_stream) {
  753. prevconn->next_stream = conn->next_stream;
  754. return;
  755. }
  756. for(prevconn = circ->n_streams;
  757. prevconn && prevconn->next_stream && prevconn->next_stream != conn;
  758. prevconn = prevconn->next_stream)
  759. ;
  760. if(prevconn && prevconn->next_stream) {
  761. prevconn->next_stream = conn->next_stream;
  762. return;
  763. }
  764. for(prevconn = circ->resolving_streams;
  765. prevconn && prevconn->next_stream && prevconn->next_stream != conn;
  766. prevconn = prevconn->next_stream)
  767. ;
  768. if(prevconn && prevconn->next_stream) {
  769. prevconn->next_stream = conn->next_stream;
  770. return;
  771. }
  772. log_fn(LOG_ERR,"edge conn not in circuit's list?");
  773. tor_assert(0); /* should never get here */
  774. }
  775. /** Notify the global circuit list that <b>conn</b> is about to be
  776. * removed and then freed.
  777. *
  778. * If it's an OR conn, then mark-for-close all the circuits that use
  779. * that conn.
  780. *
  781. * If it's an edge conn, then detach it from its circ, so we don't
  782. * try to reference it later.
  783. */
  784. void circuit_about_to_close_connection(connection_t *conn) {
  785. /* currently, we assume it's too late to flush conn's buf here.
  786. * down the road, maybe we'll consider that eof doesn't mean can't-write
  787. */
  788. circuit_t *circ;
  789. switch(conn->type) {
  790. case CONN_TYPE_OR:
  791. /* We must close all the circuits on it. */
  792. while((circ = circuit_get_by_conn(conn))) {
  793. if(circ->n_conn == conn) /* it's closing in front of us */
  794. circ->n_conn = NULL;
  795. if(circ->p_conn == conn) /* it's closing behind us */
  796. circ->p_conn = NULL;
  797. circuit_mark_for_close(circ);
  798. }
  799. return;
  800. case CONN_TYPE_AP:
  801. case CONN_TYPE_EXIT:
  802. /* It's an edge conn. Need to remove it from the linked list of
  803. * conn's for this circuit. Confirm that 'end' relay command has
  804. * been sent. But don't kill the circuit.
  805. */
  806. circ = circuit_get_by_conn(conn);
  807. if(!circ)
  808. return;
  809. circuit_detach_stream(circ, conn);
  810. } /* end switch */
  811. }
  812. /** Log, at severity <b>severity</b>, the nicknames of each router in
  813. * circ's cpath. Also log the length of the cpath, and the intended
  814. * exit point.
  815. */
  816. void circuit_log_path(int severity, circuit_t *circ) {
  817. char buf[1024];
  818. char *s = buf;
  819. struct crypt_path_t *hop;
  820. char *states[] = {"closed", "waiting for keys", "open"};
  821. routerinfo_t *router;
  822. tor_assert(CIRCUIT_IS_ORIGIN(circ) && circ->cpath);
  823. snprintf(s, sizeof(buf)-1, "circ (length %d, exit %s): ",
  824. circ->build_state->desired_path_len, circ->build_state->chosen_exit);
  825. hop=circ->cpath;
  826. do {
  827. s = buf + strlen(buf);
  828. router = router_get_by_addr_port(hop->addr,hop->port);
  829. if(router) {
  830. snprintf(s, sizeof(buf) - (s - buf), "%s(%s) ",
  831. router->nickname, states[hop->state]);
  832. } else {
  833. if(circ->purpose == CIRCUIT_PURPOSE_C_REND_JOINED) {
  834. snprintf(s, sizeof(buf) - (s - buf), "(rendjoin hop)");
  835. } else {
  836. snprintf(s, sizeof(buf) - (s - buf), "UNKNOWN ");
  837. }
  838. }
  839. hop=hop->next;
  840. } while(hop!=circ->cpath);
  841. log_fn(severity,"%s",buf);
  842. }
  843. /** Tell the rep(utation)hist(ory) module about the status of the links
  844. * in circ. Hops that have become OPEN are marked as successfully
  845. * extended; the _first_ hop that isn't open (if any) is marked as
  846. * unable to extend.
  847. */
  848. static void
  849. circuit_rep_hist_note_result(circuit_t *circ)
  850. {
  851. struct crypt_path_t *hop;
  852. char *prev_nickname = NULL;
  853. routerinfo_t *router;
  854. hop = circ->cpath;
  855. if(!hop) {
  856. /* XXX
  857. * if !hop, then we're not the beginning of this circuit.
  858. * for now, just forget about it. later, we should remember when
  859. * extends-through-us failed, too.
  860. */
  861. return;
  862. }
  863. if (options.ORPort) {
  864. prev_nickname = options.Nickname;
  865. }
  866. do {
  867. router = router_get_by_addr_port(hop->addr,hop->port);
  868. if (router) {
  869. if (prev_nickname) {
  870. if (hop->state == CPATH_STATE_OPEN)
  871. rep_hist_note_extend_succeeded(prev_nickname, router->nickname);
  872. else {
  873. rep_hist_note_extend_failed(prev_nickname, router->nickname);
  874. break;
  875. }
  876. }
  877. prev_nickname = router->nickname;
  878. } else {
  879. prev_nickname = NULL;
  880. }
  881. hop=hop->next;
  882. } while (hop!=circ->cpath);
  883. }
  884. /** A helper function for circuit_dump_by_conn() below. Log a bunch
  885. * of information about circuit <b>circ</b>.
  886. */
  887. static void
  888. circuit_dump_details(int severity, circuit_t *circ, int poll_index,
  889. char *type, int this_circid, int other_circid) {
  890. struct crypt_path_t *hop;
  891. log(severity,"Conn %d has %s circuit: circID %d (other side %d), state %d (%s), born %d",
  892. poll_index, type, this_circid, other_circid, circ->state,
  893. circuit_state_to_string[circ->state], (int)circ->timestamp_created);
  894. if(CIRCUIT_IS_ORIGIN(circ)) { /* circ starts at this node */
  895. if(circ->state == CIRCUIT_STATE_BUILDING)
  896. log(severity,"Building: desired len %d, planned exit node %s.",
  897. circ->build_state->desired_path_len, circ->build_state->chosen_exit);
  898. for(hop=circ->cpath;hop->next != circ->cpath; hop=hop->next)
  899. log(severity,"hop: state %d, addr 0x%.8x, port %d", hop->state,
  900. (unsigned int)hop->addr,
  901. (int)hop->port);
  902. }
  903. }
  904. /** Log, at severity <b>severity</b>, information about each circuit
  905. * that is connected to <b>conn</b>.
  906. */
  907. void circuit_dump_by_conn(connection_t *conn, int severity) {
  908. circuit_t *circ;
  909. connection_t *tmpconn;
  910. for(circ=global_circuitlist;circ;circ = circ->next) {
  911. if(circ->p_conn == conn)
  912. circuit_dump_details(severity, circ, conn->poll_index, "App-ward",
  913. circ->p_circ_id, circ->n_circ_id);
  914. for(tmpconn=circ->p_streams; tmpconn; tmpconn=tmpconn->next_stream) {
  915. if(tmpconn == conn) {
  916. circuit_dump_details(severity, circ, conn->poll_index, "App-ward",
  917. circ->p_circ_id, circ->n_circ_id);
  918. }
  919. }
  920. if(circ->n_conn == conn)
  921. circuit_dump_details(severity, circ, conn->poll_index, "Exit-ward",
  922. circ->n_circ_id, circ->p_circ_id);
  923. for(tmpconn=circ->n_streams; tmpconn; tmpconn=tmpconn->next_stream) {
  924. if(tmpconn == conn) {
  925. circuit_dump_details(severity, circ, conn->poll_index, "Exit-ward",
  926. circ->n_circ_id, circ->p_circ_id);
  927. }
  928. }
  929. }
  930. }
  931. /** Don't keep more than 10 unused open circuits around. */
  932. #define MAX_UNUSED_OPEN_CIRCUITS 10
  933. /** Find each circuit that has been dirty for too long, and has
  934. * no streams on it: mark it for close.
  935. *
  936. * Also, if there are more than MAX_UNUSED_OPEN_CIRCUITS open and
  937. * unused circuits, then mark the excess circs for close.
  938. */
  939. void circuit_expire_old_circuits(void) {
  940. circuit_t *circ;
  941. time_t now = time(NULL);
  942. smartlist_t *unused_open_circs;
  943. int i;
  944. unused_open_circs = smartlist_create();
  945. for (circ = global_circuitlist; circ; circ = circ->next) {
  946. if (circ->marked_for_close)
  947. continue;
  948. /* If the circuit has been dirty for too long, and there are no streams
  949. * on it, mark it for close.
  950. */
  951. if (circ->timestamp_dirty &&
  952. circ->timestamp_dirty + options.NewCircuitPeriod < now &&
  953. !circ->p_conn && /* we're the origin */
  954. !circ->p_streams /* nothing attached */ ) {
  955. log_fn(LOG_DEBUG,"Closing n_circ_id %d (dirty %d secs ago, purp %d)",circ->n_circ_id,
  956. (int)(now - circ->timestamp_dirty), circ->purpose);
  957. /* (only general and purpose_c circs can get dirty) */
  958. tor_assert(!circ->n_streams);
  959. tor_assert(circ->purpose <= CIRCUIT_PURPOSE_C_REND_JOINED);
  960. circuit_mark_for_close(circ);
  961. } else if (!circ->timestamp_dirty && CIRCUIT_IS_ORIGIN(circ) &&
  962. circ->state == CIRCUIT_STATE_OPEN &&
  963. circ->purpose == CIRCUIT_PURPOSE_C_GENERAL) {
  964. /* Also, gather a list of open unused general circuits that we created.
  965. * Because we add elements to the front of global_circuitlist,
  966. * the last elements of unused_open_circs will be the oldest
  967. * ones.
  968. */
  969. smartlist_add(unused_open_circs, circ);
  970. }
  971. }
  972. for (i = MAX_UNUSED_OPEN_CIRCUITS; i < smartlist_len(unused_open_circs); ++i) {
  973. circuit_t *circ = smartlist_get(unused_open_circs, i);
  974. log_fn(LOG_DEBUG,"Expiring excess clean circ (n_circ_id %d, purp %d)",
  975. circ->n_circ_id, circ->purpose);
  976. circuit_mark_for_close(circ);
  977. }
  978. smartlist_free(unused_open_circs);
  979. }
  980. /** The circuit <b>circ</b> has just become open. Take the next
  981. * step: for rendezvous circuits, we pass circ to the appropriate
  982. * function in rendclient or rendservice. For general circuits, we
  983. * call connection_ap_attach_pending, which looks for pending streams
  984. * that could use circ.
  985. */
  986. static void circuit_is_open(circuit_t *circ) {
  987. switch(circ->purpose) {
  988. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  989. rend_client_rendcirc_is_open(circ);
  990. break;
  991. case CIRCUIT_PURPOSE_C_INTRODUCING:
  992. rend_client_introcirc_is_open(circ);
  993. break;
  994. case CIRCUIT_PURPOSE_C_GENERAL:
  995. /* Tell any AP connections that have been waiting for a new
  996. * circuit that one is ready. */
  997. connection_ap_attach_pending();
  998. break;
  999. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  1000. /* at Bob, waiting for introductions */
  1001. rend_service_intro_is_ready(circ);
  1002. break;
  1003. case CIRCUIT_PURPOSE_S_CONNECT_REND:
  1004. /* at Bob, connecting to rend point */
  1005. rend_service_rendezvous_is_ready(circ);
  1006. break;
  1007. default:
  1008. log_fn(LOG_ERR,"unhandled purpose %d",circ->purpose);
  1009. tor_assert(0);
  1010. }
  1011. }
  1012. /*~ Called whenever a circuit could not be successfully built.
  1013. */
  1014. static void circuit_build_failed(circuit_t *circ) {
  1015. /* we should examine circ and see if it failed because of
  1016. * the last hop or an earlier hop. then use this info below.
  1017. */
  1018. int failed_at_last_hop = 0;
  1019. /* If the last hop isn't open, and the second-to-last is, we failed
  1020. * at the last hop. */
  1021. if (circ->cpath &&
  1022. circ->cpath->prev->state != CPATH_STATE_OPEN &&
  1023. circ->cpath->prev->prev->state == CPATH_STATE_OPEN) {
  1024. failed_at_last_hop = 1;
  1025. }
  1026. switch(circ->purpose) {
  1027. case CIRCUIT_PURPOSE_C_GENERAL:
  1028. if (circ->state != CIRCUIT_STATE_OPEN) {
  1029. /* If we never built the circuit, note it as a failure. */
  1030. /* Note that we can't just check circ->cpath here, because if
  1031. * circuit-building failed immediately, it won't be set yet. */
  1032. circuit_increment_failure_count();
  1033. }
  1034. break;
  1035. case CIRCUIT_PURPOSE_S_ESTABLISH_INTRO:
  1036. /* at Bob, waiting for introductions */
  1037. if (circ->state != CIRCUIT_STATE_OPEN) {
  1038. circuit_increment_failure_count();
  1039. }
  1040. /* no need to care here, because bob will rebuild intro
  1041. * points periodically. */
  1042. break;
  1043. case CIRCUIT_PURPOSE_C_INTRODUCING:
  1044. /* at Alice, connecting to intro point */
  1045. /* Don't increment failure count, since Bob may have picked
  1046. * the introduction point maliciously */
  1047. /* Alice will pick a new intro point when this one dies, if
  1048. * the stream in question still cares. No need to act here. */
  1049. break;
  1050. case CIRCUIT_PURPOSE_C_ESTABLISH_REND:
  1051. /* at Alice, waiting for Bob */
  1052. if (circ->state != CIRCUIT_STATE_OPEN) {
  1053. circuit_increment_failure_count();
  1054. }
  1055. /* Alice will pick a new rend point when this one dies, if
  1056. * the stream in question still cares. No need to act here. */
  1057. break;
  1058. case CIRCUIT_PURPOSE_S_CONNECT_REND:
  1059. /* at Bob, connecting to rend point */
  1060. /* Don't increment failure count, since Alice may have picked
  1061. * the rendezvous point maliciously */
  1062. if (failed_at_last_hop) {
  1063. log_fn(LOG_INFO,"Couldn't connect to Alice's chosen rend point %s. Sucks to be Alice.", circ->build_state->chosen_exit);
  1064. } else {
  1065. log_fn(LOG_INFO,"Couldn't connect to Alice's chosen rend point %s, because an earlier node failed.",
  1066. circ->build_state->chosen_exit);
  1067. rend_service_relaunch_rendezvous(circ);
  1068. }
  1069. break;
  1070. default:
  1071. /* Other cases are impossible, since this function is only called with
  1072. * unbuilt circuits. */
  1073. tor_assert(0);
  1074. }
  1075. }
  1076. /** Number of consecutive failures so far; should only be touched by
  1077. * circuit_launch_new and circuit_*_failure_count.
  1078. */
  1079. static int n_circuit_failures = 0;
  1080. /** Don't retry launching a new circuit if we try this many times with no
  1081. * success. */
  1082. #define MAX_CIRCUIT_FAILURES 5
  1083. /** Launch a new circuit and return a pointer to it. Return NULL if you failed. */
  1084. circuit_t *circuit_launch_new(uint8_t purpose, const char *exit_nickname) {
  1085. if (n_circuit_failures > MAX_CIRCUIT_FAILURES) {
  1086. /* too many failed circs in a row. don't try. */
  1087. // log_fn(LOG_INFO,"%d failures so far, not trying.",n_circuit_failures);
  1088. return NULL;
  1089. }
  1090. /* try a circ. if it fails, circuit_mark_for_close will increment n_circuit_failures */
  1091. return circuit_establish_circuit(purpose, exit_nickname);
  1092. }
  1093. /** Record another failure at opening a general circuit. When we have
  1094. * too many, we'll stop trying for the remainder of this minute.
  1095. */
  1096. void circuit_increment_failure_count(void) {
  1097. ++n_circuit_failures;
  1098. log_fn(LOG_DEBUG,"n_circuit_failures now %d.",n_circuit_failures);
  1099. }
  1100. /** Reset the failure count for opening general circuits. This means
  1101. * we will try MAX_CIRCUIT_FAILURES times more (if necessary) before
  1102. * stopping again.
  1103. */
  1104. void circuit_reset_failure_count(void) {
  1105. n_circuit_failures = 0;
  1106. }
  1107. /** Build a new circuit for <b>purpose</b>. If <b>exit_nickname</b>
  1108. * is defined, then use that as your exit router, else choose a suitable
  1109. * exit node.
  1110. *
  1111. * Also launch a connection to the first OR in the chosen path, if
  1112. * it's not open already.
  1113. */
  1114. static circuit_t *circuit_establish_circuit(uint8_t purpose,
  1115. const char *exit_nickname) {
  1116. routerinfo_t *firsthop;
  1117. connection_t *n_conn;
  1118. circuit_t *circ;
  1119. circ = circuit_new(0, NULL); /* sets circ->p_circ_id and circ->p_conn */
  1120. circ->state = CIRCUIT_STATE_OR_WAIT;
  1121. circ->build_state = onion_new_cpath_build_state(purpose, exit_nickname);
  1122. circ->purpose = purpose;
  1123. if (! circ->build_state) {
  1124. log_fn(LOG_INFO,"Generating cpath failed.");
  1125. circuit_mark_for_close(circ);
  1126. return NULL;
  1127. }
  1128. onion_extend_cpath(&circ->cpath, circ->build_state, &firsthop);
  1129. if(!CIRCUIT_IS_ORIGIN(circ)) {
  1130. log_fn(LOG_INFO,"Generating first cpath hop failed.");
  1131. circuit_mark_for_close(circ);
  1132. return NULL;
  1133. }
  1134. /* now see if we're already connected to the first OR in 'route' */
  1135. log_fn(LOG_DEBUG,"Looking for firsthop '%s:%u'",
  1136. firsthop->address,firsthop->or_port);
  1137. n_conn = connection_twin_get_by_addr_port(firsthop->addr,firsthop->or_port);
  1138. if(!n_conn || n_conn->state != OR_CONN_STATE_OPEN) { /* not currently connected */
  1139. circ->n_addr = firsthop->addr;
  1140. circ->n_port = firsthop->or_port;
  1141. if(!n_conn) { /* launch the connection */
  1142. n_conn = connection_or_connect(firsthop);
  1143. if(!n_conn) { /* connect failed, forget the whole thing */
  1144. log_fn(LOG_INFO,"connect to firsthop failed. Closing.");
  1145. circuit_mark_for_close(circ);
  1146. return NULL;
  1147. }
  1148. }
  1149. log_fn(LOG_DEBUG,"connecting in progress (or finished). Good.");
  1150. /* return success. The onion/circuit/etc will be taken care of automatically
  1151. * (may already have been) whenever n_conn reaches OR_CONN_STATE_OPEN.
  1152. */
  1153. return circ;
  1154. } else { /* it (or a twin) is already open. use it. */
  1155. circ->n_addr = n_conn->addr;
  1156. circ->n_port = n_conn->port;
  1157. circ->n_conn = n_conn;
  1158. log_fn(LOG_DEBUG,"Conn open. Delivering first onion skin.");
  1159. if(circuit_send_next_onion_skin(circ) < 0) {
  1160. log_fn(LOG_INFO,"circuit_send_next_onion_skin failed.");
  1161. circuit_mark_for_close(circ);
  1162. return NULL;
  1163. }
  1164. }
  1165. return circ;
  1166. }
  1167. /** Find circuits that are waiting on <b>or_conn</b> to become open,
  1168. * if any, and get them to send their create cells forward.
  1169. */
  1170. void circuit_n_conn_open(connection_t *or_conn) {
  1171. circuit_t *circ;
  1172. for(circ=global_circuitlist;circ;circ = circ->next) {
  1173. if (circ->marked_for_close)
  1174. continue;
  1175. if(CIRCUIT_IS_ORIGIN(circ) && circ->n_addr == or_conn->addr && circ->n_port == or_conn->port) {
  1176. tor_assert(circ->state == CIRCUIT_STATE_OR_WAIT);
  1177. log_fn(LOG_DEBUG,"Found circ %d, sending onion skin.", circ->n_circ_id);
  1178. circ->n_conn = or_conn;
  1179. if(circuit_send_next_onion_skin(circ) < 0) {
  1180. log_fn(LOG_INFO,"send_next_onion_skin failed; circuit marked for closing.");
  1181. circuit_mark_for_close(circ);
  1182. continue;
  1183. /* XXX could this be bad, eg if next_onion_skin failed because conn died? */
  1184. }
  1185. }
  1186. }
  1187. }
  1188. extern int has_completed_circuit;
  1189. /** This is the backbone function for building circuits.
  1190. *
  1191. * If circ's first hop is closed, then we need to build a create
  1192. * cell and send it forward.
  1193. *
  1194. * Otherwise, we need to build a relay extend cell and send it
  1195. * forward.
  1196. *
  1197. * Return -1 if we want to tear down circ, else return 0.
  1198. */
  1199. int circuit_send_next_onion_skin(circuit_t *circ) {
  1200. cell_t cell;
  1201. crypt_path_t *hop;
  1202. routerinfo_t *router;
  1203. int r;
  1204. int circ_id_type;
  1205. char payload[2+4+ONIONSKIN_CHALLENGE_LEN];
  1206. tor_assert(circ && CIRCUIT_IS_ORIGIN(circ));
  1207. if(circ->cpath->state == CPATH_STATE_CLOSED) {
  1208. tor_assert(circ->n_conn && circ->n_conn->type == CONN_TYPE_OR);
  1209. log_fn(LOG_DEBUG,"First skin; sending create cell.");
  1210. circ_id_type = decide_circ_id_type(options.Nickname,
  1211. circ->n_conn->nickname);
  1212. circ->n_circ_id = get_unique_circ_id_by_conn(circ->n_conn, circ_id_type);
  1213. memset(&cell, 0, sizeof(cell_t));
  1214. cell.command = CELL_CREATE;
  1215. cell.circ_id = circ->n_circ_id;
  1216. router = router_get_by_nickname(circ->n_conn->nickname);
  1217. if (!router) {
  1218. log_fn(LOG_WARN,"Couldn't find routerinfo for %s",
  1219. circ->n_conn->nickname);
  1220. return -1;
  1221. }
  1222. if(onion_skin_create(router->onion_pkey,
  1223. &(circ->cpath->handshake_state),
  1224. cell.payload) < 0) {
  1225. log_fn(LOG_WARN,"onion_skin_create (first hop) failed.");
  1226. return -1;
  1227. }
  1228. connection_or_write_cell_to_buf(&cell, circ->n_conn);
  1229. circ->cpath->state = CPATH_STATE_AWAITING_KEYS;
  1230. circ->state = CIRCUIT_STATE_BUILDING;
  1231. log_fn(LOG_DEBUG,"first skin; finished sending create cell.");
  1232. } else {
  1233. tor_assert(circ->cpath->state == CPATH_STATE_OPEN);
  1234. tor_assert(circ->state == CIRCUIT_STATE_BUILDING);
  1235. log_fn(LOG_DEBUG,"starting to send subsequent skin.");
  1236. r = onion_extend_cpath(&circ->cpath, circ->build_state, &router);
  1237. if (r==1) {
  1238. /* done building the circuit. whew. */
  1239. circ->state = CIRCUIT_STATE_OPEN;
  1240. log_fn(LOG_INFO,"circuit built!");
  1241. circuit_reset_failure_count();
  1242. if(!has_completed_circuit) {
  1243. has_completed_circuit=1;
  1244. log_fn(LOG_NOTICE,"Tor has successfully opened a circuit. Looks like it's working.");
  1245. }
  1246. circuit_rep_hist_note_result(circ);
  1247. circuit_is_open(circ); /* do other actions as necessary */
  1248. return 0;
  1249. } else if (r<0) {
  1250. log_fn(LOG_INFO,"Unable to extend circuit path.");
  1251. return -1;
  1252. }
  1253. hop = circ->cpath->prev;
  1254. *(uint32_t*)payload = htonl(hop->addr);
  1255. *(uint16_t*)(payload+4) = htons(hop->port);
  1256. if(onion_skin_create(router->onion_pkey, &(hop->handshake_state), payload+6) < 0) {
  1257. log_fn(LOG_WARN,"onion_skin_create failed.");
  1258. return -1;
  1259. }
  1260. log_fn(LOG_DEBUG,"Sending extend relay cell.");
  1261. /* send it to hop->prev, because it will transfer
  1262. * it to a create cell and then send to hop */
  1263. if(connection_edge_send_command(NULL, circ, RELAY_COMMAND_EXTEND,
  1264. payload, sizeof(payload), hop->prev) < 0)
  1265. return 0; /* circuit is closed */
  1266. hop->state = CPATH_STATE_AWAITING_KEYS;
  1267. }
  1268. return 0;
  1269. }
  1270. /** Take the 'extend' cell, pull out addr/port plus the onion skin. Make
  1271. * sure we're connected to the next hop, and pass it the onion skin in
  1272. * a create cell.
  1273. */
  1274. int circuit_extend(cell_t *cell, circuit_t *circ) {
  1275. connection_t *n_conn;
  1276. int circ_id_type;
  1277. cell_t newcell;
  1278. if(circ->n_conn) {
  1279. log_fn(LOG_WARN,"n_conn already set. Bug/attack. Closing.");
  1280. return -1;
  1281. }
  1282. circ->n_addr = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE));
  1283. circ->n_port = ntohs(get_uint16(cell->payload+RELAY_HEADER_SIZE+4));
  1284. n_conn = connection_twin_get_by_addr_port(circ->n_addr,circ->n_port);
  1285. if(!n_conn || n_conn->type != CONN_TYPE_OR) {
  1286. /* I've disabled making connections through OPs, but it's definitely
  1287. * possible here. I'm not sure if it would be a bug or a feature.
  1288. *
  1289. * Note also that this will close circuits where the onion has the same
  1290. * router twice in a row in the path. I think that's ok.
  1291. */
  1292. struct in_addr in;
  1293. in.s_addr = htonl(circ->n_addr);
  1294. log_fn(LOG_INFO,"Next router (%s:%d) not connected. Closing.", inet_ntoa(in), circ->n_port);
  1295. #if 0 /* if we do truncateds, no need to kill circ */
  1296. connection_edge_send_command(NULL, circ, RELAY_COMMAND_TRUNCATED,
  1297. NULL, 0, NULL);
  1298. return 0;
  1299. #endif
  1300. circuit_mark_for_close(circ);
  1301. return 0;
  1302. }
  1303. circ->n_addr = n_conn->addr; /* these are different if we found a twin instead */
  1304. circ->n_port = n_conn->port;
  1305. circ->n_conn = n_conn;
  1306. log_fn(LOG_DEBUG,"n_conn is %s:%u",n_conn->address,n_conn->port);
  1307. circ_id_type = decide_circ_id_type(options.Nickname, n_conn->nickname);
  1308. // log_fn(LOG_DEBUG,"circ_id_type = %u.",circ_id_type);
  1309. circ->n_circ_id = get_unique_circ_id_by_conn(circ->n_conn, circ_id_type);
  1310. if(!circ->n_circ_id) {
  1311. log_fn(LOG_WARN,"failed to get unique circID.");
  1312. return -1;
  1313. }
  1314. log_fn(LOG_DEBUG,"Chosen circID %u.",circ->n_circ_id);
  1315. memset(&newcell, 0, sizeof(cell_t));
  1316. newcell.command = CELL_CREATE;
  1317. newcell.circ_id = circ->n_circ_id;
  1318. memcpy(newcell.payload, cell->payload+RELAY_HEADER_SIZE+2+4,
  1319. ONIONSKIN_CHALLENGE_LEN);
  1320. connection_or_write_cell_to_buf(&newcell, circ->n_conn);
  1321. return 0;
  1322. }
  1323. /** Initialize cpath-\>{f|b}_{crypto|digest} from the key material in
  1324. * key_data. key_data must contain CPATH_KEY_MATERIAL bytes, which are
  1325. * used as follows:
  1326. * - 20 to initialize f_digest
  1327. * - 20 to initialize b_digest
  1328. * - 16 to key f_crypto
  1329. * - 16 to key b_crypto
  1330. *
  1331. * (If 'reverse' is true, then f_XX and b_XX are swapped.)
  1332. */
  1333. int circuit_init_cpath_crypto(crypt_path_t *cpath, char *key_data, int reverse)
  1334. {
  1335. crypto_digest_env_t *tmp_digest;
  1336. crypto_cipher_env_t *tmp_crypto;
  1337. tor_assert(cpath && key_data);
  1338. tor_assert(!(cpath->f_crypto || cpath->b_crypto ||
  1339. cpath->f_digest || cpath->b_digest));
  1340. log_fn(LOG_DEBUG,"hop init digest forward 0x%.8x, backward 0x%.8x.",
  1341. (unsigned int)*(uint32_t*)key_data, (unsigned int)*(uint32_t*)(key_data+20));
  1342. cpath->f_digest = crypto_new_digest_env();
  1343. crypto_digest_add_bytes(cpath->f_digest, key_data, DIGEST_LEN);
  1344. cpath->b_digest = crypto_new_digest_env();
  1345. crypto_digest_add_bytes(cpath->b_digest, key_data+DIGEST_LEN, DIGEST_LEN);
  1346. log_fn(LOG_DEBUG,"hop init cipher forward 0x%.8x, backward 0x%.8x.",
  1347. (unsigned int)*(uint32_t*)(key_data+40), (unsigned int)*(uint32_t*)(key_data+40+16));
  1348. if (!(cpath->f_crypto =
  1349. crypto_create_init_cipher(key_data+(2*DIGEST_LEN),1))) {
  1350. log(LOG_WARN,"forward cipher initialization failed.");
  1351. return -1;
  1352. }
  1353. if (!(cpath->b_crypto =
  1354. crypto_create_init_cipher(key_data+(2*DIGEST_LEN)+CIPHER_KEY_LEN,0))) {
  1355. log(LOG_WARN,"backward cipher initialization failed.");
  1356. return -1;
  1357. }
  1358. if (reverse) {
  1359. tmp_digest = cpath->f_digest;
  1360. cpath->f_digest = cpath->b_digest;
  1361. cpath->b_digest = tmp_digest;
  1362. tmp_crypto = cpath->f_crypto;
  1363. cpath->f_crypto = cpath->b_crypto;
  1364. cpath->b_crypto = tmp_crypto;
  1365. }
  1366. return 0;
  1367. }
  1368. /** A created or extended cell came back to us on the circuit,
  1369. * and it included <b>reply</b> (the second DH key, plus KH).
  1370. *
  1371. * Calculate the appropriate keys and digests, make sure KH is
  1372. * correct, and initialize this hop of the cpath.
  1373. *
  1374. * Return -1 if we want to mark circ for close, else return 0.
  1375. */
  1376. int circuit_finish_handshake(circuit_t *circ, char *reply) {
  1377. unsigned char keys[CPATH_KEY_MATERIAL_LEN];
  1378. crypt_path_t *hop;
  1379. tor_assert(CIRCUIT_IS_ORIGIN(circ));
  1380. if(circ->cpath->state == CPATH_STATE_AWAITING_KEYS)
  1381. hop = circ->cpath;
  1382. else {
  1383. for(hop=circ->cpath->next;
  1384. hop != circ->cpath && hop->state == CPATH_STATE_OPEN;
  1385. hop=hop->next) ;
  1386. if(hop == circ->cpath) { /* got an extended when we're all done? */
  1387. log_fn(LOG_WARN,"got extended when circ already built? Closing.");
  1388. return -1;
  1389. }
  1390. }
  1391. tor_assert(hop->state == CPATH_STATE_AWAITING_KEYS);
  1392. if(onion_skin_client_handshake(hop->handshake_state, reply, keys,
  1393. DIGEST_LEN*2+CIPHER_KEY_LEN*2) < 0) {
  1394. log_fn(LOG_WARN,"onion_skin_client_handshake failed.");
  1395. return -1;
  1396. }
  1397. crypto_dh_free(hop->handshake_state); /* don't need it anymore */
  1398. hop->handshake_state = NULL;
  1399. /* Remember hash of g^xy */
  1400. memcpy(hop->handshake_digest, reply+DH_KEY_LEN, DIGEST_LEN);
  1401. if (circuit_init_cpath_crypto(hop, keys, 0)<0) {
  1402. return -1;
  1403. }
  1404. hop->state = CPATH_STATE_OPEN;
  1405. log_fn(LOG_INFO,"finished");
  1406. circuit_log_path(LOG_INFO,circ);
  1407. return 0;
  1408. }
  1409. /** We received a relay truncated cell on circ.
  1410. *
  1411. * Since we don't ask for truncates currently, getting a truncated
  1412. * means that a connection broke or an extend failed. For now,
  1413. * just give up: for circ to close, and return 0.
  1414. */
  1415. int circuit_truncated(circuit_t *circ, crypt_path_t *layer) {
  1416. crypt_path_t *victim;
  1417. connection_t *stream;
  1418. tor_assert(circ && CIRCUIT_IS_ORIGIN(circ));
  1419. tor_assert(layer);
  1420. /* XXX Since we don't ask for truncates currently, getting a truncated
  1421. * means that a connection broke or an extend failed. For now,
  1422. * just give up.
  1423. */
  1424. circuit_mark_for_close(circ);
  1425. return 0;
  1426. while(layer->next != circ->cpath) {
  1427. /* we need to clear out layer->next */
  1428. victim = layer->next;
  1429. log_fn(LOG_DEBUG, "Killing a layer of the cpath.");
  1430. for(stream = circ->p_streams; stream; stream=stream->next_stream) {
  1431. if(stream->cpath_layer == victim) {
  1432. log_fn(LOG_INFO, "Marking stream %d for close.", stream->stream_id);
  1433. /* no need to send 'end' relay cells,
  1434. * because the other side's already dead
  1435. */
  1436. stream->has_sent_end = 1;
  1437. connection_mark_for_close(stream);
  1438. }
  1439. }
  1440. layer->next = victim->next;
  1441. circuit_free_cpath_node(victim);
  1442. }
  1443. log_fn(LOG_INFO, "finished");
  1444. return 0;
  1445. }
  1446. /** Verify that cpath layer <b>cp</b> has all of its invariants
  1447. * correct. Trigger an assert if anything is invalid.
  1448. */
  1449. void assert_cpath_layer_ok(const crypt_path_t *cp)
  1450. {
  1451. tor_assert(cp->f_crypto);
  1452. tor_assert(cp->b_crypto);
  1453. // tor_assert(cp->addr); /* these are zero for rendezvous extra-hops */
  1454. // tor_assert(cp->port);
  1455. switch(cp->state)
  1456. {
  1457. case CPATH_STATE_CLOSED:
  1458. case CPATH_STATE_OPEN:
  1459. tor_assert(!cp->handshake_state);
  1460. break;
  1461. case CPATH_STATE_AWAITING_KEYS:
  1462. tor_assert(cp->handshake_state);
  1463. break;
  1464. default:
  1465. tor_assert(0);
  1466. }
  1467. tor_assert(cp->package_window >= 0);
  1468. tor_assert(cp->deliver_window >= 0);
  1469. }
  1470. /** Verify that cpath <b>cp</b> has all of its invariants
  1471. * correct. Trigger an assert if anything is invalid.
  1472. */
  1473. void assert_cpath_ok(const crypt_path_t *cp)
  1474. {
  1475. while(cp->prev)
  1476. cp = cp->prev;
  1477. while(cp->next) {
  1478. assert_cpath_layer_ok(cp);
  1479. /* layers must be in sequence of: "open* awaiting? closed*" */
  1480. if (cp->prev) {
  1481. if (cp->prev->state == CPATH_STATE_OPEN) {
  1482. tor_assert(cp->state == CPATH_STATE_CLOSED ||
  1483. cp->state == CPATH_STATE_AWAITING_KEYS);
  1484. } else {
  1485. tor_assert(cp->state == CPATH_STATE_CLOSED);
  1486. }
  1487. }
  1488. cp = cp->next;
  1489. }
  1490. }
  1491. /** Verify that circuit <b>c</b> has all of its invariants
  1492. * correct. Trigger an assert if anything is invalid.
  1493. */
  1494. void assert_circuit_ok(const circuit_t *c)
  1495. {
  1496. connection_t *conn;
  1497. tor_assert(c);
  1498. tor_assert(c->magic == CIRCUIT_MAGIC);
  1499. tor_assert(c->purpose >= _CIRCUIT_PURPOSE_MIN &&
  1500. c->purpose <= _CIRCUIT_PURPOSE_MAX);
  1501. if (c->n_conn)
  1502. tor_assert(c->n_conn->type == CONN_TYPE_OR);
  1503. if (c->p_conn)
  1504. tor_assert(c->p_conn->type == CONN_TYPE_OR);
  1505. for (conn = c->p_streams; conn; conn = conn->next_stream)
  1506. tor_assert(conn->type == CONN_TYPE_AP);
  1507. for (conn = c->n_streams; conn; conn = conn->next_stream)
  1508. tor_assert(conn->type == CONN_TYPE_EXIT);
  1509. tor_assert(c->deliver_window >= 0);
  1510. tor_assert(c->package_window >= 0);
  1511. if (c->state == CIRCUIT_STATE_OPEN) {
  1512. if (c->cpath) {
  1513. tor_assert(CIRCUIT_IS_ORIGIN(c));
  1514. tor_assert(!c->n_crypto);
  1515. tor_assert(!c->p_crypto);
  1516. tor_assert(!c->n_digest);
  1517. tor_assert(!c->p_digest);
  1518. } else {
  1519. tor_assert(!CIRCUIT_IS_ORIGIN(c));
  1520. tor_assert(c->n_crypto);
  1521. tor_assert(c->p_crypto);
  1522. tor_assert(c->n_digest);
  1523. tor_assert(c->p_digest);
  1524. }
  1525. }
  1526. if (c->cpath) {
  1527. //XXX assert_cpath_ok(c->cpath);
  1528. }
  1529. if (c->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED) {
  1530. if (!c->marked_for_close) {
  1531. tor_assert(c->rend_splice);
  1532. tor_assert(c->rend_splice->rend_splice == c);
  1533. }
  1534. tor_assert(c->rend_splice != c);
  1535. } else {
  1536. tor_assert(!c->rend_splice);
  1537. }
  1538. }
  1539. /*
  1540. Local Variables:
  1541. mode:c
  1542. indent-tabs-mode:nil
  1543. c-basic-offset:2
  1544. End:
  1545. */