relay.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2008, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /* $Id$ */
  7. const char relay_c_id[] =
  8. "$Id$";
  9. /**
  10. * \file relay.c
  11. * \brief Handle relay cell encryption/decryption, plus packaging and
  12. * receiving from circuits, plus queueing on circuits.
  13. **/
  14. #include "or.h"
  15. #include "mempool.h"
  16. static int relay_crypt(circuit_t *circ, cell_t *cell, int cell_direction,
  17. crypt_path_t **layer_hint, char *recognized);
  18. static edge_connection_t *relay_lookup_conn(circuit_t *circ, cell_t *cell,
  19. int cell_direction,
  20. crypt_path_t *layer_hint);
  21. static int
  22. connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ,
  23. edge_connection_t *conn,
  24. crypt_path_t *layer_hint);
  25. static void
  26. circuit_consider_sending_sendme(circuit_t *circ, crypt_path_t *layer_hint);
  27. static void
  28. circuit_resume_edge_reading(circuit_t *circ, crypt_path_t *layer_hint);
  29. static int
  30. circuit_resume_edge_reading_helper(edge_connection_t *conn,
  31. circuit_t *circ,
  32. crypt_path_t *layer_hint);
  33. static int
  34. circuit_consider_stop_edge_reading(circuit_t *circ, crypt_path_t *layer_hint);
  35. /** Stats: how many relay cells have originated at this hop, or have
  36. * been relayed onward (not recognized at this hop)?
  37. */
  38. uint64_t stats_n_relay_cells_relayed = 0;
  39. /** Stats: how many relay cells have been delivered to streams at this
  40. * hop?
  41. */
  42. uint64_t stats_n_relay_cells_delivered = 0;
  43. /** Update digest from the payload of cell. Assign integrity part to
  44. * cell.
  45. */
  46. static void
  47. relay_set_digest(crypto_digest_env_t *digest, cell_t *cell)
  48. {
  49. char integrity[4];
  50. relay_header_t rh;
  51. crypto_digest_add_bytes(digest, cell->payload, CELL_PAYLOAD_SIZE);
  52. crypto_digest_get_digest(digest, integrity, 4);
  53. // log_fn(LOG_DEBUG,"Putting digest of %u %u %u %u into relay cell.",
  54. // integrity[0], integrity[1], integrity[2], integrity[3]);
  55. relay_header_unpack(&rh, cell->payload);
  56. memcpy(rh.integrity, integrity, 4);
  57. relay_header_pack(cell->payload, &rh);
  58. }
  59. /** Does the digest for this circuit indicate that this cell is for us?
  60. *
  61. * Update digest from the payload of cell (with the integrity part set
  62. * to 0). If the integrity part is valid, return 1, else restore digest
  63. * and cell to their original state and return 0.
  64. */
  65. static int
  66. relay_digest_matches(crypto_digest_env_t *digest, cell_t *cell)
  67. {
  68. char received_integrity[4], calculated_integrity[4];
  69. relay_header_t rh;
  70. crypto_digest_env_t *backup_digest=NULL;
  71. backup_digest = crypto_digest_dup(digest);
  72. relay_header_unpack(&rh, cell->payload);
  73. memcpy(received_integrity, rh.integrity, 4);
  74. memset(rh.integrity, 0, 4);
  75. relay_header_pack(cell->payload, &rh);
  76. // log_fn(LOG_DEBUG,"Reading digest of %u %u %u %u from relay cell.",
  77. // received_integrity[0], received_integrity[1],
  78. // received_integrity[2], received_integrity[3]);
  79. crypto_digest_add_bytes(digest, cell->payload, CELL_PAYLOAD_SIZE);
  80. crypto_digest_get_digest(digest, calculated_integrity, 4);
  81. if (memcmp(received_integrity, calculated_integrity, 4)) {
  82. // log_fn(LOG_INFO,"Recognized=0 but bad digest. Not recognizing.");
  83. // (%d vs %d).", received_integrity, calculated_integrity);
  84. /* restore digest to its old form */
  85. crypto_digest_assign(digest, backup_digest);
  86. /* restore the relay header */
  87. memcpy(rh.integrity, received_integrity, 4);
  88. relay_header_pack(cell->payload, &rh);
  89. crypto_free_digest_env(backup_digest);
  90. return 0;
  91. }
  92. crypto_free_digest_env(backup_digest);
  93. return 1;
  94. }
  95. /** Apply <b>cipher</b> to CELL_PAYLOAD_SIZE bytes of <b>in</b>
  96. * (in place).
  97. *
  98. * If <b>encrypt_mode</b> is 1 then encrypt, else decrypt.
  99. *
  100. * Return -1 if the crypto fails, else return 0.
  101. */
  102. static int
  103. relay_crypt_one_payload(crypto_cipher_env_t *cipher, char *in,
  104. int encrypt_mode)
  105. {
  106. int r;
  107. (void)encrypt_mode;
  108. r = crypto_cipher_crypt_inplace(cipher, in, CELL_PAYLOAD_SIZE);
  109. if (r) {
  110. log_warn(LD_BUG,"Error during relay encryption");
  111. return -1;
  112. }
  113. return 0;
  114. }
  115. /** Receive a relay cell:
  116. * - Crypt it (encrypt if headed toward the origin or if we <b>are</b> the
  117. * origin; decrypt if we're headed toward the exit).
  118. * - Check if recognized (if exitward).
  119. * - If recognized and the digest checks out, then find if there's a stream
  120. * that the cell is intended for, and deliver it to the right
  121. * connection_edge.
  122. * - If not recognized, then we need to relay it: append it to the appropriate
  123. * cell_queue on <b>circ</b>.
  124. *
  125. * Return -<b>reason</b> on failure.
  126. */
  127. int
  128. circuit_receive_relay_cell(cell_t *cell, circuit_t *circ, int cell_direction)
  129. {
  130. or_connection_t *or_conn=NULL;
  131. crypt_path_t *layer_hint=NULL;
  132. char recognized=0;
  133. int reason;
  134. tor_assert(cell);
  135. tor_assert(circ);
  136. tor_assert(cell_direction == CELL_DIRECTION_OUT ||
  137. cell_direction == CELL_DIRECTION_IN);
  138. if (circ->marked_for_close)
  139. return 0;
  140. if (relay_crypt(circ, cell, cell_direction, &layer_hint, &recognized) < 0) {
  141. log_warn(LD_BUG,"relay crypt failed. Dropping connection.");
  142. return -END_CIRC_REASON_INTERNAL;
  143. }
  144. if (recognized) {
  145. edge_connection_t *conn = relay_lookup_conn(circ, cell, cell_direction,
  146. layer_hint);
  147. if (cell_direction == CELL_DIRECTION_OUT) {
  148. ++stats_n_relay_cells_delivered;
  149. log_debug(LD_OR,"Sending away from origin.");
  150. if ((reason=connection_edge_process_relay_cell(cell, circ, conn, NULL))
  151. < 0) {
  152. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  153. "connection_edge_process_relay_cell (away from origin) "
  154. "failed.");
  155. return reason;
  156. }
  157. }
  158. if (cell_direction == CELL_DIRECTION_IN) {
  159. ++stats_n_relay_cells_delivered;
  160. log_debug(LD_OR,"Sending to origin.");
  161. if ((reason = connection_edge_process_relay_cell(cell, circ, conn,
  162. layer_hint)) < 0) {
  163. log_warn(LD_OR,
  164. "connection_edge_process_relay_cell (at origin) failed.");
  165. return reason;
  166. }
  167. }
  168. return 0;
  169. }
  170. /* not recognized. pass it on. */
  171. if (cell_direction == CELL_DIRECTION_OUT) {
  172. cell->circ_id = circ->n_circ_id; /* switch it */
  173. or_conn = circ->n_conn;
  174. } else if (! CIRCUIT_IS_ORIGIN(circ)) {
  175. cell->circ_id = TO_OR_CIRCUIT(circ)->p_circ_id; /* switch it */
  176. or_conn = TO_OR_CIRCUIT(circ)->p_conn;
  177. } else {
  178. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  179. "Dropping unrecognized inbound cell on origin circuit.");
  180. return 0;
  181. }
  182. if (!or_conn) {
  183. // XXXX Can this splice stuff be done more cleanly?
  184. if (! CIRCUIT_IS_ORIGIN(circ) &&
  185. TO_OR_CIRCUIT(circ)->rend_splice &&
  186. cell_direction == CELL_DIRECTION_OUT) {
  187. or_circuit_t *splice = TO_OR_CIRCUIT(circ)->rend_splice;
  188. tor_assert(circ->purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED);
  189. tor_assert(splice->_base.purpose == CIRCUIT_PURPOSE_REND_ESTABLISHED);
  190. cell->circ_id = splice->p_circ_id;
  191. if ((reason = circuit_receive_relay_cell(cell, TO_CIRCUIT(splice),
  192. CELL_DIRECTION_IN)) < 0) {
  193. log_warn(LD_REND, "Error relaying cell across rendezvous; closing "
  194. "circuits");
  195. /* XXXX Do this here, or just return -1? */
  196. circuit_mark_for_close(circ, -reason);
  197. return reason;
  198. }
  199. return 0;
  200. }
  201. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  202. "Didn't recognize cell, but circ stops here! Closing circ.");
  203. return -END_CIRC_REASON_TORPROTOCOL;
  204. }
  205. log_debug(LD_OR,"Passing on unrecognized cell.");
  206. ++stats_n_relay_cells_relayed; /* XXXX no longer quite accurate {cells}
  207. * we might kill the circ before we relay
  208. * the cells. */
  209. append_cell_to_circuit_queue(circ, or_conn, cell, cell_direction);
  210. return 0;
  211. }
  212. /** Do the appropriate en/decryptions for <b>cell</b> arriving on
  213. * <b>circ</b> in direction <b>cell_direction</b>.
  214. *
  215. * If cell_direction == CELL_DIRECTION_IN:
  216. * - If we're at the origin (we're the OP), for hops 1..N,
  217. * decrypt cell. If recognized, stop.
  218. * - Else (we're not the OP), encrypt one hop. Cell is not recognized.
  219. *
  220. * If cell_direction == CELL_DIRECTION_OUT:
  221. * - decrypt one hop. Check if recognized.
  222. *
  223. * If cell is recognized, set *recognized to 1, and set
  224. * *layer_hint to the hop that recognized it.
  225. *
  226. * Return -1 to indicate that we should mark the circuit for close,
  227. * else return 0.
  228. */
  229. static int
  230. relay_crypt(circuit_t *circ, cell_t *cell, int cell_direction,
  231. crypt_path_t **layer_hint, char *recognized)
  232. {
  233. relay_header_t rh;
  234. tor_assert(circ);
  235. tor_assert(cell);
  236. tor_assert(recognized);
  237. tor_assert(cell_direction == CELL_DIRECTION_IN ||
  238. cell_direction == CELL_DIRECTION_OUT);
  239. if (cell_direction == CELL_DIRECTION_IN) {
  240. if (CIRCUIT_IS_ORIGIN(circ)) { /* We're at the beginning of the circuit.
  241. * We'll want to do layered decrypts. */
  242. crypt_path_t *thishop, *cpath = TO_ORIGIN_CIRCUIT(circ)->cpath;
  243. thishop = cpath;
  244. if (thishop->state != CPATH_STATE_OPEN) {
  245. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  246. "Relay cell before first created cell? Closing.");
  247. return -1;
  248. }
  249. do { /* Remember: cpath is in forward order, that is, first hop first. */
  250. tor_assert(thishop);
  251. if (relay_crypt_one_payload(thishop->b_crypto, cell->payload, 0) < 0)
  252. return -1;
  253. relay_header_unpack(&rh, cell->payload);
  254. if (rh.recognized == 0) {
  255. /* it's possibly recognized. have to check digest to be sure. */
  256. if (relay_digest_matches(thishop->b_digest, cell)) {
  257. *recognized = 1;
  258. *layer_hint = thishop;
  259. return 0;
  260. }
  261. }
  262. thishop = thishop->next;
  263. } while (thishop != cpath && thishop->state == CPATH_STATE_OPEN);
  264. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  265. "Incoming cell at client not recognized. Closing.");
  266. return -1;
  267. } else { /* we're in the middle. Just one crypt. */
  268. if (relay_crypt_one_payload(TO_OR_CIRCUIT(circ)->p_crypto,
  269. cell->payload, 1) < 0)
  270. return -1;
  271. // log_fn(LOG_DEBUG,"Skipping recognized check, because we're not "
  272. // "the client.");
  273. }
  274. } else /* cell_direction == CELL_DIRECTION_OUT */ {
  275. /* we're in the middle. Just one crypt. */
  276. if (relay_crypt_one_payload(TO_OR_CIRCUIT(circ)->n_crypto,
  277. cell->payload, 0) < 0)
  278. return -1;
  279. relay_header_unpack(&rh, cell->payload);
  280. if (rh.recognized == 0) {
  281. /* it's possibly recognized. have to check digest to be sure. */
  282. if (relay_digest_matches(TO_OR_CIRCUIT(circ)->n_digest, cell)) {
  283. *recognized = 1;
  284. return 0;
  285. }
  286. }
  287. }
  288. return 0;
  289. }
  290. /** Package a relay cell from an edge:
  291. * - Encrypt it to the right layer
  292. * - Append it to the appropriate cell_queue on <b>circ</b>.
  293. */
  294. static int
  295. circuit_package_relay_cell(cell_t *cell, circuit_t *circ,
  296. int cell_direction,
  297. crypt_path_t *layer_hint)
  298. {
  299. or_connection_t *conn; /* where to send the cell */
  300. if (cell_direction == CELL_DIRECTION_OUT) {
  301. crypt_path_t *thishop; /* counter for repeated crypts */
  302. conn = circ->n_conn;
  303. if (!CIRCUIT_IS_ORIGIN(circ) || !conn) {
  304. log_warn(LD_BUG,"outgoing relay cell has n_conn==NULL. Dropping.");
  305. return 0; /* just drop it */
  306. }
  307. relay_set_digest(layer_hint->f_digest, cell);
  308. thishop = layer_hint;
  309. /* moving from farthest to nearest hop */
  310. do {
  311. tor_assert(thishop);
  312. /* XXXX RD This is a bug, right? */
  313. log_debug(LD_OR,"crypting a layer of the relay cell.");
  314. if (relay_crypt_one_payload(thishop->f_crypto, cell->payload, 1) < 0) {
  315. return -1;
  316. }
  317. thishop = thishop->prev;
  318. } while (thishop != TO_ORIGIN_CIRCUIT(circ)->cpath->prev);
  319. } else { /* incoming cell */
  320. or_circuit_t *or_circ;
  321. if (CIRCUIT_IS_ORIGIN(circ)) {
  322. /* We should never package an _incoming_ cell from the circuit
  323. * origin; that means we messed up somewhere. */
  324. log_warn(LD_BUG,"incoming relay cell at origin circuit. Dropping.");
  325. assert_circuit_ok(circ);
  326. return 0; /* just drop it */
  327. }
  328. or_circ = TO_OR_CIRCUIT(circ);
  329. conn = or_circ->p_conn;
  330. relay_set_digest(or_circ->p_digest, cell);
  331. if (relay_crypt_one_payload(or_circ->p_crypto, cell->payload, 1) < 0)
  332. return -1;
  333. }
  334. ++stats_n_relay_cells_relayed;
  335. append_cell_to_circuit_queue(circ, conn, cell, cell_direction);
  336. return 0;
  337. }
  338. /** If cell's stream_id matches the stream_id of any conn that's
  339. * attached to circ, return that conn, else return NULL.
  340. */
  341. static edge_connection_t *
  342. relay_lookup_conn(circuit_t *circ, cell_t *cell, int cell_direction,
  343. crypt_path_t *layer_hint)
  344. {
  345. edge_connection_t *tmpconn;
  346. relay_header_t rh;
  347. relay_header_unpack(&rh, cell->payload);
  348. if (!rh.stream_id)
  349. return NULL;
  350. /* IN or OUT cells could have come from either direction, now
  351. * that we allow rendezvous *to* an OP.
  352. */
  353. if (CIRCUIT_IS_ORIGIN(circ)) {
  354. for (tmpconn = TO_ORIGIN_CIRCUIT(circ)->p_streams; tmpconn;
  355. tmpconn=tmpconn->next_stream) {
  356. if (rh.stream_id == tmpconn->stream_id &&
  357. !tmpconn->_base.marked_for_close &&
  358. tmpconn->cpath_layer == layer_hint) {
  359. log_debug(LD_APP,"found conn for stream %d.", rh.stream_id);
  360. return tmpconn;
  361. }
  362. }
  363. } else {
  364. for (tmpconn = TO_OR_CIRCUIT(circ)->n_streams; tmpconn;
  365. tmpconn=tmpconn->next_stream) {
  366. if (rh.stream_id == tmpconn->stream_id &&
  367. !tmpconn->_base.marked_for_close) {
  368. log_debug(LD_EXIT,"found conn for stream %d.", rh.stream_id);
  369. if (cell_direction == CELL_DIRECTION_OUT ||
  370. connection_edge_is_rendezvous_stream(tmpconn))
  371. return tmpconn;
  372. }
  373. }
  374. for (tmpconn = TO_OR_CIRCUIT(circ)->resolving_streams; tmpconn;
  375. tmpconn=tmpconn->next_stream) {
  376. if (rh.stream_id == tmpconn->stream_id &&
  377. !tmpconn->_base.marked_for_close) {
  378. log_debug(LD_EXIT,"found conn for stream %d.", rh.stream_id);
  379. return tmpconn;
  380. }
  381. }
  382. }
  383. return NULL; /* probably a begin relay cell */
  384. }
  385. /** Pack the relay_header_t host-order structure <b>src</b> into
  386. * network-order in the buffer <b>dest</b>. See tor-spec.txt for details
  387. * about the wire format.
  388. */
  389. void
  390. relay_header_pack(char *dest, const relay_header_t *src)
  391. {
  392. *(uint8_t*)(dest) = src->command;
  393. set_uint16(dest+1, htons(src->recognized));
  394. set_uint16(dest+3, htons(src->stream_id));
  395. memcpy(dest+5, src->integrity, 4);
  396. set_uint16(dest+9, htons(src->length));
  397. }
  398. /** Unpack the network-order buffer <b>src</b> into a host-order
  399. * relay_header_t structure <b>dest</b>.
  400. */
  401. void
  402. relay_header_unpack(relay_header_t *dest, const char *src)
  403. {
  404. dest->command = *(uint8_t*)(src);
  405. dest->recognized = ntohs(get_uint16(src+1));
  406. dest->stream_id = ntohs(get_uint16(src+3));
  407. memcpy(dest->integrity, src+5, 4);
  408. dest->length = ntohs(get_uint16(src+9));
  409. }
  410. /** Make a relay cell out of <b>relay_command</b> and <b>payload</b>, and send
  411. * it onto the open circuit <b>circ</b>. <b>stream_id</b> is the ID on
  412. * <b>circ</b> for the stream that's sending the relay cell, or 0 if it's a
  413. * control cell. <b>cpath_layer</b> is NULL for OR->OP cells, or the
  414. * destination hop for OP->OR cells.
  415. *
  416. * If you can't send the cell, mark the circuit for close and return -1. Else
  417. * return 0.
  418. */
  419. int
  420. relay_send_command_from_edge(uint16_t stream_id, circuit_t *circ,
  421. uint8_t relay_command, const char *payload,
  422. size_t payload_len, crypt_path_t *cpath_layer)
  423. {
  424. cell_t cell;
  425. relay_header_t rh;
  426. int cell_direction;
  427. /* XXXX NM Split this function into a separate versions per circuit type? */
  428. tor_assert(circ);
  429. tor_assert(payload_len <= RELAY_PAYLOAD_SIZE);
  430. memset(&cell, 0, sizeof(cell_t));
  431. cell.command = CELL_RELAY;
  432. if (cpath_layer) {
  433. cell.circ_id = circ->n_circ_id;
  434. cell_direction = CELL_DIRECTION_OUT;
  435. } else if (! CIRCUIT_IS_ORIGIN(circ)) {
  436. cell.circ_id = TO_OR_CIRCUIT(circ)->p_circ_id;
  437. cell_direction = CELL_DIRECTION_IN;
  438. } else {
  439. return -1;
  440. }
  441. memset(&rh, 0, sizeof(rh));
  442. rh.command = relay_command;
  443. rh.stream_id = stream_id;
  444. rh.length = payload_len;
  445. relay_header_pack(cell.payload, &rh);
  446. if (payload_len)
  447. memcpy(cell.payload+RELAY_HEADER_SIZE, payload, payload_len);
  448. log_debug(LD_OR,"delivering %d cell %s.", relay_command,
  449. cell_direction == CELL_DIRECTION_OUT ? "forward" : "backward");
  450. if (cell_direction == CELL_DIRECTION_OUT && circ->n_conn) {
  451. /* if we're using relaybandwidthrate, this conn wants priority */
  452. /* XXXX021 the call to time() seems little too frequent */
  453. circ->n_conn->client_used = time(NULL);
  454. }
  455. if (circuit_package_relay_cell(&cell, circ, cell_direction, cpath_layer)
  456. < 0) {
  457. log_warn(LD_BUG,"circuit_package_relay_cell failed. Closing.");
  458. circuit_mark_for_close(circ, END_CIRC_REASON_INTERNAL);
  459. return -1;
  460. }
  461. return 0;
  462. }
  463. /** Make a relay cell out of <b>relay_command</b> and <b>payload</b>, and
  464. * send it onto the open circuit <b>circ</b>. <b>fromconn</b> is the stream
  465. * that's sending the relay cell, or NULL if it's a control cell.
  466. * <b>cpath_layer</b> is NULL for OR->OP cells, or the destination hop
  467. * for OP->OR cells.
  468. *
  469. * If you can't send the cell, mark the circuit for close and
  470. * return -1. Else return 0.
  471. */
  472. int
  473. connection_edge_send_command(edge_connection_t *fromconn,
  474. uint8_t relay_command, const char *payload,
  475. size_t payload_len)
  476. {
  477. /* XXXX NM Split this function into a separate versions per circuit type? */
  478. circuit_t *circ;
  479. tor_assert(fromconn);
  480. circ = fromconn->on_circuit;
  481. if (fromconn->_base.marked_for_close) {
  482. log_warn(LD_BUG,
  483. "called on conn that's already marked for close at %s:%d.",
  484. fromconn->_base.marked_for_close_file,
  485. fromconn->_base.marked_for_close);
  486. return 0;
  487. }
  488. if (!circ) {
  489. if (fromconn->_base.type == CONN_TYPE_AP) {
  490. log_info(LD_APP,"no circ. Closing conn.");
  491. connection_mark_unattached_ap(fromconn, END_STREAM_REASON_INTERNAL);
  492. } else {
  493. log_info(LD_EXIT,"no circ. Closing conn.");
  494. fromconn->_base.edge_has_sent_end = 1; /* no circ to send to */
  495. fromconn->end_reason = END_STREAM_REASON_INTERNAL;
  496. connection_mark_for_close(TO_CONN(fromconn));
  497. }
  498. return -1;
  499. }
  500. return relay_send_command_from_edge(fromconn->stream_id, circ,
  501. relay_command, payload,
  502. payload_len, fromconn->cpath_layer);
  503. }
  504. /** Translate <b>reason</b>, which came from a relay 'end' cell,
  505. * into a static const string describing why the stream is closing.
  506. * <b>reason</b> is -1 if no reason was provided.
  507. */
  508. static const char *
  509. connection_edge_end_reason_str(int reason)
  510. {
  511. switch (reason) {
  512. case -1:
  513. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  514. "End cell arrived with length 0. Should be at least 1.");
  515. return "MALFORMED";
  516. case END_STREAM_REASON_MISC: return "misc error";
  517. case END_STREAM_REASON_RESOLVEFAILED: return "resolve failed";
  518. case END_STREAM_REASON_CONNECTREFUSED: return "connection refused";
  519. case END_STREAM_REASON_EXITPOLICY: return "exit policy failed";
  520. case END_STREAM_REASON_DESTROY: return "destroyed";
  521. case END_STREAM_REASON_DONE: return "closed normally";
  522. case END_STREAM_REASON_TIMEOUT: return "gave up (timeout)";
  523. case END_STREAM_REASON_HIBERNATING: return "server is hibernating";
  524. case END_STREAM_REASON_INTERNAL: return "internal error at server";
  525. case END_STREAM_REASON_RESOURCELIMIT: return "server out of resources";
  526. case END_STREAM_REASON_CONNRESET: return "connection reset";
  527. case END_STREAM_REASON_TORPROTOCOL: return "Tor protocol error";
  528. case END_STREAM_REASON_NOTDIRECTORY: return "not a directory";
  529. default:
  530. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  531. "Reason for ending (%d) not recognized.",reason);
  532. return "unknown";
  533. }
  534. }
  535. /** Translate <b>reason</b> (as from a relay 'end' cell) into an
  536. * appropriate SOCKS5 reply code.
  537. *
  538. * A reason of 0 means that we're not actually expecting to send
  539. * this code back to the socks client; we just call it 'succeeded'
  540. * to keep things simple.
  541. */
  542. socks5_reply_status_t
  543. connection_edge_end_reason_socks5_response(int reason)
  544. {
  545. switch (reason & END_STREAM_REASON_MASK) {
  546. case 0:
  547. return SOCKS5_SUCCEEDED;
  548. case END_STREAM_REASON_MISC:
  549. return SOCKS5_GENERAL_ERROR;
  550. case END_STREAM_REASON_RESOLVEFAILED:
  551. return SOCKS5_HOST_UNREACHABLE;
  552. case END_STREAM_REASON_CONNECTREFUSED:
  553. return SOCKS5_CONNECTION_REFUSED;
  554. case END_STREAM_REASON_ENTRYPOLICY:
  555. return SOCKS5_NOT_ALLOWED;
  556. case END_STREAM_REASON_EXITPOLICY:
  557. return SOCKS5_NOT_ALLOWED;
  558. case END_STREAM_REASON_DESTROY:
  559. return SOCKS5_GENERAL_ERROR;
  560. case END_STREAM_REASON_DONE:
  561. return SOCKS5_SUCCEEDED;
  562. case END_STREAM_REASON_TIMEOUT:
  563. return SOCKS5_TTL_EXPIRED;
  564. case END_STREAM_REASON_RESOURCELIMIT:
  565. return SOCKS5_GENERAL_ERROR;
  566. case END_STREAM_REASON_HIBERNATING:
  567. return SOCKS5_GENERAL_ERROR;
  568. case END_STREAM_REASON_INTERNAL:
  569. return SOCKS5_GENERAL_ERROR;
  570. case END_STREAM_REASON_CONNRESET:
  571. return SOCKS5_CONNECTION_REFUSED;
  572. case END_STREAM_REASON_TORPROTOCOL:
  573. return SOCKS5_GENERAL_ERROR;
  574. case END_STREAM_REASON_CANT_ATTACH:
  575. return SOCKS5_GENERAL_ERROR;
  576. case END_STREAM_REASON_NET_UNREACHABLE:
  577. return SOCKS5_NET_UNREACHABLE;
  578. case END_STREAM_REASON_SOCKSPROTOCOL:
  579. return SOCKS5_GENERAL_ERROR;
  580. default:
  581. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  582. "Reason for ending (%d) not recognized; "
  583. "sending generic socks error.", reason);
  584. return SOCKS5_GENERAL_ERROR;
  585. }
  586. }
  587. /* We need to use a few macros to deal with the fact that Windows
  588. * decided that their sockets interface should be a permakludge.
  589. * E_CASE is for errors where windows has both a EFOO and a WSAEFOO
  590. * version, and S_CASE is for errors where windows has only a WSAEFOO
  591. * version. (The E is for 'error', the S is for 'socket'). */
  592. #ifdef MS_WINDOWS
  593. #define E_CASE(s) case s: case WSA ## s
  594. #define S_CASE(s) case WSA ## s
  595. #else
  596. #define E_CASE(s) case s
  597. #define S_CASE(s) case s
  598. #endif
  599. /** Given an errno from a failed exit connection, return a reason code
  600. * appropriate for use in a RELAY END cell.
  601. */
  602. int
  603. errno_to_end_reason(int e)
  604. {
  605. switch (e) {
  606. case EPIPE:
  607. return END_STREAM_REASON_DONE;
  608. E_CASE(EBADF):
  609. E_CASE(EFAULT):
  610. E_CASE(EINVAL):
  611. S_CASE(EISCONN):
  612. S_CASE(ENOTSOCK):
  613. S_CASE(EPROTONOSUPPORT):
  614. S_CASE(EAFNOSUPPORT):
  615. E_CASE(EACCES):
  616. S_CASE(ENOTCONN):
  617. S_CASE(ENETUNREACH):
  618. return END_STREAM_REASON_INTERNAL;
  619. S_CASE(ECONNREFUSED):
  620. return END_STREAM_REASON_CONNECTREFUSED;
  621. S_CASE(ECONNRESET):
  622. return END_STREAM_REASON_CONNRESET;
  623. S_CASE(ETIMEDOUT):
  624. return END_STREAM_REASON_TIMEOUT;
  625. S_CASE(ENOBUFS):
  626. case ENOMEM:
  627. case ENFILE:
  628. E_CASE(EMFILE):
  629. return END_STREAM_REASON_RESOURCELIMIT;
  630. default:
  631. log_info(LD_EXIT, "Didn't recognize errno %d (%s); telling the client "
  632. "that we are ending a stream for 'misc' reason.",
  633. e, tor_socket_strerror(e));
  634. return END_STREAM_REASON_MISC;
  635. }
  636. }
  637. /** How many times will I retry a stream that fails due to DNS
  638. * resolve failure or misc error?
  639. */
  640. #define MAX_RESOLVE_FAILURES 3
  641. /** Return 1 if reason is something that you should retry if you
  642. * get the end cell before you've connected; else return 0. */
  643. static int
  644. edge_reason_is_retriable(int reason)
  645. {
  646. return reason == END_STREAM_REASON_HIBERNATING ||
  647. reason == END_STREAM_REASON_RESOURCELIMIT ||
  648. reason == END_STREAM_REASON_EXITPOLICY ||
  649. reason == END_STREAM_REASON_RESOLVEFAILED ||
  650. reason == END_STREAM_REASON_MISC;
  651. }
  652. /** Called when we receive an END cell on a stream that isn't open yet.
  653. * Arguments are as for connection_edge_process_relay_cell().
  654. */
  655. static int
  656. connection_edge_process_end_not_open(
  657. relay_header_t *rh, cell_t *cell, origin_circuit_t *circ,
  658. edge_connection_t *conn, crypt_path_t *layer_hint)
  659. {
  660. struct in_addr in;
  661. routerinfo_t *exitrouter;
  662. int reason = *(cell->payload+RELAY_HEADER_SIZE);
  663. int control_reason = reason | END_STREAM_REASON_FLAG_REMOTE;
  664. (void) layer_hint; /* unused */
  665. if (rh->length > 0 && edge_reason_is_retriable(reason) &&
  666. conn->_base.type == CONN_TYPE_AP) {
  667. log_info(LD_APP,"Address '%s' refused due to '%s'. Considering retrying.",
  668. safe_str(conn->socks_request->address),
  669. connection_edge_end_reason_str(reason));
  670. exitrouter =
  671. router_get_by_digest(circ->build_state->chosen_exit->identity_digest);
  672. switch (reason) {
  673. case END_STREAM_REASON_EXITPOLICY:
  674. if (rh->length >= 5) {
  675. uint32_t addr = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+1));
  676. int ttl;
  677. if (!addr) {
  678. log_info(LD_APP,"Address '%s' resolved to 0.0.0.0. Closing,",
  679. safe_str(conn->socks_request->address));
  680. connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
  681. return 0;
  682. }
  683. if (rh->length >= 9)
  684. ttl = (int)ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+5));
  685. else
  686. ttl = -1;
  687. client_dns_set_addressmap(conn->socks_request->address, addr,
  688. conn->chosen_exit_name, ttl);
  689. }
  690. /* check if he *ought* to have allowed it */
  691. if (exitrouter &&
  692. (rh->length < 5 ||
  693. (tor_inet_aton(conn->socks_request->address, &in) &&
  694. !conn->chosen_exit_name))) {
  695. log_info(LD_APP,
  696. "Exitrouter '%s' seems to be more restrictive than its exit "
  697. "policy. Not using this router as exit for now.",
  698. exitrouter->nickname);
  699. policies_set_router_exitpolicy_to_reject_all(exitrouter);
  700. }
  701. /* rewrite it to an IP if we learned one. */
  702. if (addressmap_rewrite(conn->socks_request->address,
  703. sizeof(conn->socks_request->address),
  704. NULL)) {
  705. control_event_stream_status(conn, STREAM_EVENT_REMAP, 0);
  706. }
  707. if (conn->_base.chosen_exit_optional ||
  708. conn->_base.chosen_exit_retries) {
  709. /* stop wanting a specific exit */
  710. conn->_base.chosen_exit_optional = 0;
  711. /* A non-zero chosen_exit_retries can happen if we set a
  712. * TrackHostExits for this address under a port that the exit
  713. * relay allows, but then try the same address with a different
  714. * port that it doesn't allow to exit. We shouldn't unregister
  715. * the mapping, since it is probably still wanted on the
  716. * original port. But now we give away to the exit relay that
  717. * we probably have a TrackHostExits on it. So be it. */
  718. conn->_base.chosen_exit_retries = 0;
  719. tor_free(conn->chosen_exit_name); /* clears it */
  720. }
  721. if (connection_ap_detach_retriable(conn, circ, control_reason) >= 0)
  722. return 0;
  723. /* else, conn will get closed below */
  724. break;
  725. case END_STREAM_REASON_CONNECTREFUSED:
  726. if (!conn->_base.chosen_exit_optional)
  727. break; /* break means it'll close, below */
  728. /* Else fall through: expire this circuit, clear the
  729. * chosen_exit_name field, and try again. */
  730. case END_STREAM_REASON_RESOLVEFAILED:
  731. case END_STREAM_REASON_TIMEOUT:
  732. case END_STREAM_REASON_MISC:
  733. if (client_dns_incr_failures(conn->socks_request->address)
  734. < MAX_RESOLVE_FAILURES) {
  735. /* We haven't retried too many times; reattach the connection. */
  736. circuit_log_path(LOG_INFO,LD_APP,circ);
  737. tor_assert(circ->_base.timestamp_dirty);
  738. circ->_base.timestamp_dirty -= get_options()->MaxCircuitDirtiness;
  739. if (conn->_base.chosen_exit_optional) {
  740. /* stop wanting a specific exit */
  741. conn->_base.chosen_exit_optional = 0;
  742. tor_free(conn->chosen_exit_name); /* clears it */
  743. }
  744. if (connection_ap_detach_retriable(conn, circ, control_reason) >= 0)
  745. return 0;
  746. /* else, conn will get closed below */
  747. } else {
  748. log_notice(LD_APP,
  749. "Have tried resolving or connecting to address '%s' "
  750. "at %d different places. Giving up.",
  751. safe_str(conn->socks_request->address),
  752. MAX_RESOLVE_FAILURES);
  753. /* clear the failures, so it will have a full try next time */
  754. client_dns_clear_failures(conn->socks_request->address);
  755. }
  756. break;
  757. case END_STREAM_REASON_HIBERNATING:
  758. case END_STREAM_REASON_RESOURCELIMIT:
  759. if (exitrouter) {
  760. policies_set_router_exitpolicy_to_reject_all(exitrouter);
  761. }
  762. if (conn->_base.chosen_exit_optional) {
  763. /* stop wanting a specific exit */
  764. conn->_base.chosen_exit_optional = 0;
  765. tor_free(conn->chosen_exit_name); /* clears it */
  766. }
  767. if (connection_ap_detach_retriable(conn, circ, control_reason) >= 0)
  768. return 0;
  769. /* else, will close below */
  770. break;
  771. } /* end switch */
  772. log_info(LD_APP,"Giving up on retrying; conn can't be handled.");
  773. }
  774. log_info(LD_APP,
  775. "Edge got end (%s) before we're connected. Marking for close.",
  776. connection_edge_end_reason_str(rh->length > 0 ? reason : -1));
  777. if (conn->_base.type == CONN_TYPE_AP) {
  778. circuit_log_path(LOG_INFO,LD_APP,circ);
  779. /* need to test because of detach_retriable*/
  780. if (!conn->_base.marked_for_close)
  781. connection_mark_unattached_ap(conn, control_reason);
  782. } else {
  783. /* we just got an 'end', don't need to send one */
  784. conn->_base.edge_has_sent_end = 1;
  785. conn->end_reason = control_reason;
  786. connection_mark_for_close(TO_CONN(conn));
  787. }
  788. return 0;
  789. }
  790. /** Helper: change the socks_request-&gt;address field on conn to the
  791. * dotted-quad representation of <b>new_addr</b> (given in host order),
  792. * and send an appropriate REMAP event. */
  793. static void
  794. remap_event_helper(edge_connection_t *conn, uint32_t new_addr)
  795. {
  796. struct in_addr in;
  797. in.s_addr = htonl(new_addr);
  798. tor_inet_ntoa(&in, conn->socks_request->address,
  799. sizeof(conn->socks_request->address));
  800. control_event_stream_status(conn, STREAM_EVENT_REMAP,
  801. REMAP_STREAM_SOURCE_EXIT);
  802. }
  803. /** An incoming relay cell has arrived from circuit <b>circ</b> to
  804. * stream <b>conn</b>.
  805. *
  806. * The arguments here are the same as in
  807. * connection_edge_process_relay_cell() below; this function is called
  808. * from there when <b>conn</b> is defined and not in an open state.
  809. */
  810. static int
  811. connection_edge_process_relay_cell_not_open(
  812. relay_header_t *rh, cell_t *cell, circuit_t *circ,
  813. edge_connection_t *conn, crypt_path_t *layer_hint)
  814. {
  815. if (rh->command == RELAY_COMMAND_END) {
  816. if (CIRCUIT_IS_ORIGIN(circ))
  817. return connection_edge_process_end_not_open(rh, cell,
  818. TO_ORIGIN_CIRCUIT(circ), conn,
  819. layer_hint);
  820. else
  821. return 0;
  822. }
  823. if (conn->_base.type == CONN_TYPE_AP &&
  824. rh->command == RELAY_COMMAND_CONNECTED) {
  825. tor_assert(CIRCUIT_IS_ORIGIN(circ));
  826. if (conn->_base.state != AP_CONN_STATE_CONNECT_WAIT) {
  827. log_fn(LOG_PROTOCOL_WARN, LD_APP,
  828. "Got 'connected' while not in state connect_wait. Dropping.");
  829. return 0;
  830. }
  831. conn->_base.state = AP_CONN_STATE_OPEN;
  832. log_info(LD_APP,"'connected' received after %d seconds.",
  833. (int)(time(NULL) - conn->_base.timestamp_lastread));
  834. if (rh->length >= 4) {
  835. uint32_t addr = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE));
  836. int ttl;
  837. if (!addr || (get_options()->ClientDNSRejectInternalAddresses &&
  838. is_internal_IP(addr, 0))) {
  839. char buf[INET_NTOA_BUF_LEN];
  840. struct in_addr a;
  841. a.s_addr = htonl(addr);
  842. tor_inet_ntoa(&a, buf, sizeof(buf));
  843. log_info(LD_APP,
  844. "...but it claims the IP address was %s. Closing.", buf);
  845. connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL);
  846. connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
  847. return 0;
  848. }
  849. if (rh->length >= 8)
  850. ttl = (int)ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+4));
  851. else
  852. ttl = -1;
  853. client_dns_set_addressmap(conn->socks_request->address, addr,
  854. conn->chosen_exit_name, ttl);
  855. remap_event_helper(conn, addr);
  856. }
  857. circuit_log_path(LOG_INFO,LD_APP,TO_ORIGIN_CIRCUIT(circ));
  858. /* don't send a socks reply to transparent conns */
  859. if (!conn->socks_request->has_finished)
  860. connection_ap_handshake_socks_reply(conn, NULL, 0, 0);
  861. /* handle anything that might have queued */
  862. if (connection_edge_package_raw_inbuf(conn, 1) < 0) {
  863. /* (We already sent an end cell if possible) */
  864. connection_mark_for_close(TO_CONN(conn));
  865. return 0;
  866. }
  867. return 0;
  868. }
  869. if (conn->_base.type == CONN_TYPE_AP &&
  870. rh->command == RELAY_COMMAND_RESOLVED) {
  871. int ttl;
  872. int answer_len;
  873. uint8_t answer_type;
  874. if (conn->_base.state != AP_CONN_STATE_RESOLVE_WAIT) {
  875. log_fn(LOG_PROTOCOL_WARN, LD_APP, "Got a 'resolved' cell while "
  876. "not in state resolve_wait. Dropping.");
  877. return 0;
  878. }
  879. tor_assert(SOCKS_COMMAND_IS_RESOLVE(conn->socks_request->command));
  880. answer_len = cell->payload[RELAY_HEADER_SIZE+1];
  881. if (rh->length < 2 || answer_len+2>rh->length) {
  882. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  883. "Dropping malformed 'resolved' cell");
  884. connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
  885. return 0;
  886. }
  887. answer_type = cell->payload[RELAY_HEADER_SIZE];
  888. if (rh->length >= answer_len+6)
  889. ttl = (int)ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+
  890. 2+answer_len));
  891. else
  892. ttl = -1;
  893. if (answer_type == RESOLVED_TYPE_IPV4 && answer_len >= 4) {
  894. uint32_t addr = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+2));
  895. if (get_options()->ClientDNSRejectInternalAddresses &&
  896. is_internal_IP(addr, 0)) {
  897. char buf[INET_NTOA_BUF_LEN];
  898. struct in_addr a;
  899. a.s_addr = htonl(addr);
  900. tor_inet_ntoa(&a, buf, sizeof(buf));
  901. log_info(LD_APP,"Got a resolve with answer %s. Rejecting.", buf);
  902. connection_ap_handshake_socks_resolved(conn,
  903. RESOLVED_TYPE_ERROR_TRANSIENT,
  904. 0, NULL, 0, TIME_MAX);
  905. connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
  906. return 0;
  907. }
  908. }
  909. connection_ap_handshake_socks_resolved(conn,
  910. answer_type,
  911. cell->payload[RELAY_HEADER_SIZE+1], /*answer_len*/
  912. cell->payload+RELAY_HEADER_SIZE+2, /*answer*/
  913. ttl,
  914. -1);
  915. if (answer_type == RESOLVED_TYPE_IPV4) {
  916. uint32_t addr = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+2));
  917. remap_event_helper(conn, addr);
  918. }
  919. connection_mark_unattached_ap(conn,
  920. END_STREAM_REASON_DONE |
  921. END_STREAM_REASON_FLAG_ALREADY_SOCKS_REPLIED);
  922. return 0;
  923. }
  924. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  925. "Got an unexpected relay command %d, in state %d (%s). Dropping.",
  926. rh->command, conn->_base.state,
  927. conn_state_to_string(conn->_base.type, conn->_base.state));
  928. return 0; /* for forward compatibility, don't kill the circuit */
  929. // connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL);
  930. // connection_mark_for_close(conn);
  931. // return -1;
  932. }
  933. /** An incoming relay cell has arrived on circuit <b>circ</b>. If
  934. * <b>conn</b> is NULL this is a control cell, else <b>cell</b> is
  935. * destined for <b>conn</b>.
  936. *
  937. * If <b>layer_hint</b> is defined, then we're the origin of the
  938. * circuit, and it specifies the hop that packaged <b>cell</b>.
  939. *
  940. * Return -reason if you want to warn and tear down the circuit, else 0.
  941. */
  942. static int
  943. connection_edge_process_relay_cell(cell_t *cell, circuit_t *circ,
  944. edge_connection_t *conn,
  945. crypt_path_t *layer_hint)
  946. {
  947. static int num_seen=0;
  948. relay_header_t rh;
  949. unsigned domain = layer_hint?LD_APP:LD_EXIT;
  950. int reason;
  951. tor_assert(cell);
  952. tor_assert(circ);
  953. relay_header_unpack(&rh, cell->payload);
  954. // log_fn(LOG_DEBUG,"command %d stream %d", rh.command, rh.stream_id);
  955. num_seen++;
  956. log_debug(domain, "Now seen %d relay cells here.", num_seen);
  957. if (rh.length > RELAY_PAYLOAD_SIZE) {
  958. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  959. "Relay cell length field too long. Closing circuit.");
  960. return - END_CIRC_REASON_TORPROTOCOL;
  961. }
  962. /* either conn is NULL, in which case we've got a control cell, or else
  963. * conn points to the recognized stream. */
  964. if (conn && !connection_state_is_open(TO_CONN(conn)))
  965. return connection_edge_process_relay_cell_not_open(
  966. &rh, cell, circ, conn, layer_hint);
  967. switch (rh.command) {
  968. case RELAY_COMMAND_DROP:
  969. // log_info(domain,"Got a relay-level padding cell. Dropping.");
  970. return 0;
  971. case RELAY_COMMAND_BEGIN:
  972. case RELAY_COMMAND_BEGIN_DIR:
  973. if (layer_hint &&
  974. circ->purpose != CIRCUIT_PURPOSE_S_REND_JOINED) {
  975. log_fn(LOG_PROTOCOL_WARN, LD_APP,
  976. "Relay begin request unsupported at AP. Dropping.");
  977. return 0;
  978. }
  979. if (conn) {
  980. log_fn(LOG_PROTOCOL_WARN, domain,
  981. "Begin cell for known stream. Dropping.");
  982. return 0;
  983. }
  984. return connection_exit_begin_conn(cell, circ);
  985. case RELAY_COMMAND_DATA:
  986. ++stats_n_data_cells_received;
  987. if (( layer_hint && --layer_hint->deliver_window < 0) ||
  988. (!layer_hint && --circ->deliver_window < 0)) {
  989. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  990. "(relay data) circ deliver_window below 0. Killing.");
  991. connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL);
  992. connection_mark_for_close(TO_CONN(conn));
  993. return -END_CIRC_REASON_TORPROTOCOL;
  994. }
  995. log_debug(domain,"circ deliver_window now %d.", layer_hint ?
  996. layer_hint->deliver_window : circ->deliver_window);
  997. circuit_consider_sending_sendme(circ, layer_hint);
  998. if (!conn) {
  999. log_info(domain,"data cell dropped, unknown stream.");
  1000. return 0;
  1001. }
  1002. if (--conn->deliver_window < 0) { /* is it below 0 after decrement? */
  1003. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  1004. "(relay data) conn deliver_window below 0. Killing.");
  1005. return -END_CIRC_REASON_TORPROTOCOL;
  1006. }
  1007. stats_n_data_bytes_received += rh.length;
  1008. connection_write_to_buf(cell->payload + RELAY_HEADER_SIZE,
  1009. rh.length, TO_CONN(conn));
  1010. connection_edge_consider_sending_sendme(conn);
  1011. return 0;
  1012. case RELAY_COMMAND_END:
  1013. reason = rh.length > 0 ?
  1014. *(uint8_t *)(cell->payload+RELAY_HEADER_SIZE) : END_STREAM_REASON_MISC;
  1015. if (!conn) {
  1016. log_info(domain,"end cell (%s) dropped, unknown stream.",
  1017. connection_edge_end_reason_str(reason));
  1018. return 0;
  1019. }
  1020. /* XXX add to this log_fn the exit node's nickname? */
  1021. log_info(domain,"%d: end cell (%s) for stream %d. Removing stream.",
  1022. conn->_base.s,
  1023. connection_edge_end_reason_str(reason),
  1024. conn->stream_id);
  1025. if (conn->socks_request && !conn->socks_request->has_finished)
  1026. log_warn(LD_BUG,
  1027. "open stream hasn't sent socks answer yet? Closing.");
  1028. /* We just *got* an end; no reason to send one. */
  1029. conn->_base.edge_has_sent_end = 1;
  1030. if (!conn->end_reason)
  1031. conn->end_reason = reason | END_STREAM_REASON_FLAG_REMOTE;
  1032. if (!conn->_base.marked_for_close) {
  1033. /* only mark it if not already marked. it's possible to
  1034. * get the 'end' right around when the client hangs up on us. */
  1035. connection_mark_for_close(TO_CONN(conn));
  1036. conn->_base.hold_open_until_flushed = 1;
  1037. }
  1038. return 0;
  1039. case RELAY_COMMAND_EXTEND:
  1040. if (conn) {
  1041. log_fn(LOG_PROTOCOL_WARN, domain,
  1042. "'extend' cell received for non-zero stream. Dropping.");
  1043. return 0;
  1044. }
  1045. return circuit_extend(cell, circ);
  1046. case RELAY_COMMAND_EXTENDED:
  1047. if (!layer_hint) {
  1048. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  1049. "'extended' unsupported at non-origin. Dropping.");
  1050. return 0;
  1051. }
  1052. log_debug(domain,"Got an extended cell! Yay.");
  1053. if ((reason = circuit_finish_handshake(TO_ORIGIN_CIRCUIT(circ),
  1054. CELL_CREATED,
  1055. cell->payload+RELAY_HEADER_SIZE)) < 0) {
  1056. log_warn(domain,"circuit_finish_handshake failed.");
  1057. return reason;
  1058. }
  1059. if ((reason=circuit_send_next_onion_skin(TO_ORIGIN_CIRCUIT(circ)))<0) {
  1060. log_info(domain,"circuit_send_next_onion_skin() failed.");
  1061. return reason;
  1062. }
  1063. return 0;
  1064. case RELAY_COMMAND_TRUNCATE:
  1065. if (layer_hint) {
  1066. log_fn(LOG_PROTOCOL_WARN, LD_APP,
  1067. "'truncate' unsupported at origin. Dropping.");
  1068. return 0;
  1069. }
  1070. if (circ->n_conn) {
  1071. uint8_t trunc_reason = *(uint8_t*)(cell->payload + RELAY_HEADER_SIZE);
  1072. connection_or_send_destroy(circ->n_circ_id, circ->n_conn,
  1073. trunc_reason);
  1074. circuit_set_n_circid_orconn(circ, 0, NULL);
  1075. }
  1076. log_debug(LD_EXIT, "Processed 'truncate', replying.");
  1077. {
  1078. char payload[1];
  1079. payload[0] = (char)END_CIRC_REASON_REQUESTED;
  1080. relay_send_command_from_edge(0, circ, RELAY_COMMAND_TRUNCATED,
  1081. payload, sizeof(payload), NULL);
  1082. }
  1083. return 0;
  1084. case RELAY_COMMAND_TRUNCATED:
  1085. if (!layer_hint) {
  1086. log_fn(LOG_PROTOCOL_WARN, LD_EXIT,
  1087. "'truncated' unsupported at non-origin. Dropping.");
  1088. return 0;
  1089. }
  1090. circuit_truncated(TO_ORIGIN_CIRCUIT(circ), layer_hint);
  1091. return 0;
  1092. case RELAY_COMMAND_CONNECTED:
  1093. if (conn) {
  1094. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  1095. "'connected' unsupported while open. Closing circ.");
  1096. return -END_CIRC_REASON_TORPROTOCOL;
  1097. }
  1098. log_info(domain,
  1099. "'connected' received, no conn attached anymore. Ignoring.");
  1100. return 0;
  1101. case RELAY_COMMAND_SENDME:
  1102. if (!conn) {
  1103. if (layer_hint) {
  1104. layer_hint->package_window += CIRCWINDOW_INCREMENT;
  1105. log_debug(LD_APP,"circ-level sendme at origin, packagewindow %d.",
  1106. layer_hint->package_window);
  1107. circuit_resume_edge_reading(circ, layer_hint);
  1108. } else {
  1109. circ->package_window += CIRCWINDOW_INCREMENT;
  1110. log_debug(LD_APP,
  1111. "circ-level sendme at non-origin, packagewindow %d.",
  1112. circ->package_window);
  1113. circuit_resume_edge_reading(circ, layer_hint);
  1114. }
  1115. return 0;
  1116. }
  1117. conn->package_window += STREAMWINDOW_INCREMENT;
  1118. log_debug(domain,"stream-level sendme, packagewindow now %d.",
  1119. conn->package_window);
  1120. connection_start_reading(TO_CONN(conn));
  1121. /* handle whatever might still be on the inbuf */
  1122. if (connection_edge_package_raw_inbuf(conn, 1) < 0) {
  1123. /* (We already sent an end cell if possible) */
  1124. connection_mark_for_close(TO_CONN(conn));
  1125. return 0;
  1126. }
  1127. return 0;
  1128. case RELAY_COMMAND_RESOLVE:
  1129. if (layer_hint) {
  1130. log_fn(LOG_PROTOCOL_WARN, LD_APP,
  1131. "resolve request unsupported at AP; dropping.");
  1132. return 0;
  1133. } else if (conn) {
  1134. log_fn(LOG_PROTOCOL_WARN, domain,
  1135. "resolve request for known stream; dropping.");
  1136. return 0;
  1137. } else if (circ->purpose != CIRCUIT_PURPOSE_OR) {
  1138. log_fn(LOG_PROTOCOL_WARN, domain,
  1139. "resolve request on circ with purpose %d; dropping",
  1140. circ->purpose);
  1141. return 0;
  1142. }
  1143. connection_exit_begin_resolve(cell, TO_OR_CIRCUIT(circ));
  1144. return 0;
  1145. case RELAY_COMMAND_RESOLVED:
  1146. if (conn) {
  1147. log_fn(LOG_PROTOCOL_WARN, domain,
  1148. "'resolved' unsupported while open. Closing circ.");
  1149. return -END_CIRC_REASON_TORPROTOCOL;
  1150. }
  1151. log_info(domain,
  1152. "'resolved' received, no conn attached anymore. Ignoring.");
  1153. return 0;
  1154. case RELAY_COMMAND_ESTABLISH_INTRO:
  1155. case RELAY_COMMAND_ESTABLISH_RENDEZVOUS:
  1156. case RELAY_COMMAND_INTRODUCE1:
  1157. case RELAY_COMMAND_INTRODUCE2:
  1158. case RELAY_COMMAND_INTRODUCE_ACK:
  1159. case RELAY_COMMAND_RENDEZVOUS1:
  1160. case RELAY_COMMAND_RENDEZVOUS2:
  1161. case RELAY_COMMAND_INTRO_ESTABLISHED:
  1162. case RELAY_COMMAND_RENDEZVOUS_ESTABLISHED:
  1163. rend_process_relay_cell(circ, rh.command, rh.length,
  1164. cell->payload+RELAY_HEADER_SIZE);
  1165. return 0;
  1166. }
  1167. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  1168. "Received unknown relay command %d. Perhaps the other side is using "
  1169. "a newer version of Tor? Dropping.",
  1170. rh.command);
  1171. return 0; /* for forward compatibility, don't kill the circuit */
  1172. }
  1173. uint64_t stats_n_data_cells_packaged = 0;
  1174. uint64_t stats_n_data_bytes_packaged = 0;
  1175. uint64_t stats_n_data_cells_received = 0;
  1176. uint64_t stats_n_data_bytes_received = 0;
  1177. /** While conn->inbuf has an entire relay payload of bytes on it,
  1178. * and the appropriate package windows aren't empty, grab a cell
  1179. * and send it down the circuit.
  1180. *
  1181. * Return -1 (and send a RELAY_END cell if necessary) if conn should
  1182. * be marked for close, else return 0.
  1183. */
  1184. int
  1185. connection_edge_package_raw_inbuf(edge_connection_t *conn, int package_partial)
  1186. {
  1187. size_t amount_to_process, length;
  1188. char payload[CELL_PAYLOAD_SIZE];
  1189. circuit_t *circ;
  1190. unsigned domain = conn->cpath_layer ? LD_APP : LD_EXIT;
  1191. tor_assert(conn);
  1192. if (conn->_base.marked_for_close) {
  1193. log_warn(LD_BUG,
  1194. "called on conn that's already marked for close at %s:%d.",
  1195. conn->_base.marked_for_close_file, conn->_base.marked_for_close);
  1196. return 0;
  1197. }
  1198. repeat_connection_edge_package_raw_inbuf:
  1199. circ = circuit_get_by_edge_conn(conn);
  1200. if (!circ) {
  1201. log_info(domain,"conn has no circuit! Closing.");
  1202. conn->end_reason = END_STREAM_REASON_CANT_ATTACH;
  1203. return -1;
  1204. }
  1205. if (circuit_consider_stop_edge_reading(circ, conn->cpath_layer))
  1206. return 0;
  1207. if (conn->package_window <= 0) {
  1208. log_info(domain,"called with package_window %d. Skipping.",
  1209. conn->package_window);
  1210. connection_stop_reading(TO_CONN(conn));
  1211. return 0;
  1212. }
  1213. amount_to_process = buf_datalen(conn->_base.inbuf);
  1214. if (!amount_to_process)
  1215. return 0;
  1216. if (!package_partial && amount_to_process < RELAY_PAYLOAD_SIZE)
  1217. return 0;
  1218. if (amount_to_process > RELAY_PAYLOAD_SIZE) {
  1219. length = RELAY_PAYLOAD_SIZE;
  1220. } else {
  1221. length = amount_to_process;
  1222. }
  1223. stats_n_data_bytes_packaged += length;
  1224. stats_n_data_cells_packaged += 1;
  1225. connection_fetch_from_buf(payload, length, TO_CONN(conn));
  1226. log_debug(domain,"(%d) Packaging %d bytes (%d waiting).", conn->_base.s,
  1227. (int)length, (int)buf_datalen(conn->_base.inbuf));
  1228. if (connection_edge_send_command(conn, RELAY_COMMAND_DATA,
  1229. payload, length) < 0 )
  1230. /* circuit got marked for close, don't continue, don't need to mark conn */
  1231. return 0;
  1232. if (!conn->cpath_layer) { /* non-rendezvous exit */
  1233. tor_assert(circ->package_window > 0);
  1234. circ->package_window--;
  1235. } else { /* we're an AP, or an exit on a rendezvous circ */
  1236. tor_assert(conn->cpath_layer->package_window > 0);
  1237. conn->cpath_layer->package_window--;
  1238. }
  1239. if (--conn->package_window <= 0) { /* is it 0 after decrement? */
  1240. connection_stop_reading(TO_CONN(conn));
  1241. log_debug(domain,"conn->package_window reached 0.");
  1242. circuit_consider_stop_edge_reading(circ, conn->cpath_layer);
  1243. return 0; /* don't process the inbuf any more */
  1244. }
  1245. log_debug(domain,"conn->package_window is now %d",conn->package_window);
  1246. /* handle more if there's more, or return 0 if there isn't */
  1247. goto repeat_connection_edge_package_raw_inbuf;
  1248. }
  1249. /** Called when we've just received a relay data cell, or when
  1250. * we've just finished flushing all bytes to stream <b>conn</b>.
  1251. *
  1252. * If conn->outbuf is not too full, and our deliver window is
  1253. * low, send back a suitable number of stream-level sendme cells.
  1254. */
  1255. void
  1256. connection_edge_consider_sending_sendme(edge_connection_t *conn)
  1257. {
  1258. circuit_t *circ;
  1259. if (connection_outbuf_too_full(TO_CONN(conn)))
  1260. return;
  1261. circ = circuit_get_by_edge_conn(conn);
  1262. if (!circ) {
  1263. /* this can legitimately happen if the destroy has already
  1264. * arrived and torn down the circuit */
  1265. log_info(LD_APP,"No circuit associated with conn. Skipping.");
  1266. return;
  1267. }
  1268. while (conn->deliver_window < STREAMWINDOW_START - STREAMWINDOW_INCREMENT) {
  1269. log_debug(conn->cpath_layer?LD_APP:LD_EXIT,
  1270. "Outbuf %d, Queueing stream sendme.",
  1271. (int)conn->_base.outbuf_flushlen);
  1272. conn->deliver_window += STREAMWINDOW_INCREMENT;
  1273. if (connection_edge_send_command(conn, RELAY_COMMAND_SENDME,
  1274. NULL, 0) < 0) {
  1275. log_warn(LD_APP,"connection_edge_send_command failed. Skipping.");
  1276. return; /* the circuit's closed, don't continue */
  1277. }
  1278. }
  1279. }
  1280. /** The circuit <b>circ</b> has received a circuit-level sendme
  1281. * (on hop <b>layer_hint</b>, if we're the OP). Go through all the
  1282. * attached streams and let them resume reading and packaging, if
  1283. * their stream windows allow it.
  1284. */
  1285. static void
  1286. circuit_resume_edge_reading(circuit_t *circ, crypt_path_t *layer_hint)
  1287. {
  1288. log_debug(layer_hint?LD_APP:LD_EXIT,"resuming");
  1289. if (CIRCUIT_IS_ORIGIN(circ))
  1290. circuit_resume_edge_reading_helper(TO_ORIGIN_CIRCUIT(circ)->p_streams,
  1291. circ, layer_hint);
  1292. else
  1293. circuit_resume_edge_reading_helper(TO_OR_CIRCUIT(circ)->n_streams,
  1294. circ, layer_hint);
  1295. }
  1296. /** A helper function for circuit_resume_edge_reading() above.
  1297. * The arguments are the same, except that <b>conn</b> is the head
  1298. * of a linked list of edge streams that should each be considered.
  1299. */
  1300. static int
  1301. circuit_resume_edge_reading_helper(edge_connection_t *conn,
  1302. circuit_t *circ,
  1303. crypt_path_t *layer_hint)
  1304. {
  1305. for ( ; conn; conn=conn->next_stream) {
  1306. if (conn->_base.marked_for_close)
  1307. continue;
  1308. if ((!layer_hint && conn->package_window > 0) ||
  1309. (layer_hint && conn->package_window > 0 &&
  1310. conn->cpath_layer == layer_hint)) {
  1311. connection_start_reading(TO_CONN(conn));
  1312. /* handle whatever might still be on the inbuf */
  1313. if (connection_edge_package_raw_inbuf(conn, 1)<0) {
  1314. /* (We already sent an end cell if possible) */
  1315. connection_mark_for_close(TO_CONN(conn));
  1316. continue;
  1317. }
  1318. /* If the circuit won't accept any more data, return without looking
  1319. * at any more of the streams. Any connections that should be stopped
  1320. * have already been stopped by connection_edge_package_raw_inbuf. */
  1321. if (circuit_consider_stop_edge_reading(circ, layer_hint))
  1322. return -1;
  1323. }
  1324. }
  1325. return 0;
  1326. }
  1327. /** Check if the package window for <b>circ</b> is empty (at
  1328. * hop <b>layer_hint</b> if it's defined).
  1329. *
  1330. * If yes, tell edge streams to stop reading and return 1.
  1331. * Else return 0.
  1332. */
  1333. static int
  1334. circuit_consider_stop_edge_reading(circuit_t *circ, crypt_path_t *layer_hint)
  1335. {
  1336. edge_connection_t *conn = NULL;
  1337. unsigned domain = layer_hint ? LD_APP : LD_EXIT;
  1338. if (!layer_hint) {
  1339. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  1340. log_debug(domain,"considering circ->package_window %d",
  1341. circ->package_window);
  1342. if (circ->package_window <= 0) {
  1343. log_debug(domain,"yes, not-at-origin. stopped.");
  1344. for (conn = or_circ->n_streams; conn; conn=conn->next_stream)
  1345. connection_stop_reading(TO_CONN(conn));
  1346. return 1;
  1347. }
  1348. return 0;
  1349. }
  1350. /* else, layer hint is defined, use it */
  1351. log_debug(domain,"considering layer_hint->package_window %d",
  1352. layer_hint->package_window);
  1353. if (layer_hint->package_window <= 0) {
  1354. log_debug(domain,"yes, at-origin. stopped.");
  1355. for (conn = TO_ORIGIN_CIRCUIT(circ)->p_streams; conn;
  1356. conn=conn->next_stream)
  1357. if (conn->cpath_layer == layer_hint)
  1358. connection_stop_reading(TO_CONN(conn));
  1359. return 1;
  1360. }
  1361. return 0;
  1362. }
  1363. /** Check if the deliver_window for circuit <b>circ</b> (at hop
  1364. * <b>layer_hint</b> if it's defined) is low enough that we should
  1365. * send a circuit-level sendme back down the circuit. If so, send
  1366. * enough sendmes that the window would be overfull if we sent any
  1367. * more.
  1368. */
  1369. static void
  1370. circuit_consider_sending_sendme(circuit_t *circ, crypt_path_t *layer_hint)
  1371. {
  1372. // log_fn(LOG_INFO,"Considering: layer_hint is %s",
  1373. // layer_hint ? "defined" : "null");
  1374. while ((layer_hint ? layer_hint->deliver_window : circ->deliver_window) <
  1375. CIRCWINDOW_START - CIRCWINDOW_INCREMENT) {
  1376. log_debug(LD_CIRC,"Queueing circuit sendme.");
  1377. if (layer_hint)
  1378. layer_hint->deliver_window += CIRCWINDOW_INCREMENT;
  1379. else
  1380. circ->deliver_window += CIRCWINDOW_INCREMENT;
  1381. if (relay_send_command_from_edge(0, circ, RELAY_COMMAND_SENDME,
  1382. NULL, 0, layer_hint) < 0) {
  1383. log_warn(LD_CIRC,
  1384. "relay_send_command_from_edge failed. Circuit's closed.");
  1385. return; /* the circuit's closed, don't continue */
  1386. }
  1387. }
  1388. }
  1389. /** Stop reading on edge connections when we have this many cells
  1390. * waiting on the appropriate queue. */
  1391. #define CELL_QUEUE_HIGHWATER_SIZE 256
  1392. /** Start reading from edge connections again when we get down to this many
  1393. * cells. */
  1394. #define CELL_QUEUE_LOWWATER_SIZE 64
  1395. #ifdef ACTIVE_CIRCUITS_PARANOIA
  1396. #define assert_active_circuits_ok_paranoid(conn) \
  1397. assert_active_circuits_ok(conn)
  1398. #else
  1399. #define assert_active_circuits_ok_paranoid(conn)
  1400. #endif
  1401. /** The total number of cells we have allocated from the memory pool. */
  1402. static int total_cells_allocated = 0;
  1403. #ifdef ENABLE_CELL_POOL /* Defined in ./configure. True by default. */
  1404. /* XXX021 make cell pools the only option once we know they work and improve
  1405. * matters? -RD */
  1406. static mp_pool_t *cell_pool = NULL;
  1407. /** Allocate structures to hold cells. */
  1408. void
  1409. init_cell_pool(void)
  1410. {
  1411. tor_assert(!cell_pool);
  1412. cell_pool = mp_pool_new(sizeof(packed_cell_t), 128*1024);
  1413. }
  1414. /** Free all storage used to hold cells. */
  1415. void
  1416. free_cell_pool(void)
  1417. {
  1418. /* Maybe we haven't called init_cell_pool yet; need to check for it. */
  1419. if (cell_pool) {
  1420. mp_pool_destroy(cell_pool);
  1421. cell_pool = NULL;
  1422. }
  1423. }
  1424. /** Free excess storage in cell pool. */
  1425. void
  1426. clean_cell_pool(void)
  1427. {
  1428. tor_assert(cell_pool);
  1429. mp_pool_clean(cell_pool, 0, 1);
  1430. }
  1431. /** Release storage held by <b>cell</b>. */
  1432. static INLINE void
  1433. packed_cell_free(packed_cell_t *cell)
  1434. {
  1435. --total_cells_allocated;
  1436. mp_pool_release(cell);
  1437. }
  1438. /** Allocate and return a new packed_cell_t. */
  1439. static INLINE packed_cell_t *
  1440. packed_cell_alloc(void)
  1441. {
  1442. ++total_cells_allocated;
  1443. return mp_pool_get(cell_pool);
  1444. }
  1445. void
  1446. dump_cell_pool_usage(int severity)
  1447. {
  1448. circuit_t *c;
  1449. int n_circs = 0;
  1450. int n_cells = 0;
  1451. for (c = _circuit_get_global_list(); c; c = c->next) {
  1452. n_cells += c->n_conn_cells.n;
  1453. if (!CIRCUIT_IS_ORIGIN(c))
  1454. n_cells += TO_OR_CIRCUIT(c)->p_conn_cells.n;
  1455. ++n_circs;
  1456. }
  1457. log(severity, LD_MM, "%d cells allocated on %d circuits. %d cells leaked.",
  1458. n_cells, n_circs, total_cells_allocated - n_cells);
  1459. mp_pool_log_status(cell_pool, severity);
  1460. }
  1461. #else
  1462. /* ENABLE_CELL_POOL isn't defined: here are some stubs to use tor_malloc()
  1463. * and tor_free() instead. */
  1464. void
  1465. init_cell_pool(void)
  1466. {
  1467. }
  1468. void
  1469. free_cell_pool(void)
  1470. {
  1471. }
  1472. void
  1473. clean_cell_pool(void)
  1474. {
  1475. }
  1476. static INLINE void
  1477. packed_cell_free(packed_cell_t *cell)
  1478. {
  1479. --total_cells_allocated;
  1480. tor_free(cell);
  1481. }
  1482. static INLINE packed_cell_t *
  1483. packed_cell_alloc(void)
  1484. {
  1485. ++total_cells_allocated;
  1486. return tor_malloc(sizeof(packed_cell_t));
  1487. }
  1488. void
  1489. dump_cell_pool_usage(int severity)
  1490. {
  1491. (void) severity;
  1492. }
  1493. #endif
  1494. /** Allocate a new copy of packed <b>cell</b>. */
  1495. static INLINE packed_cell_t *
  1496. packed_cell_copy(const cell_t *cell)
  1497. {
  1498. packed_cell_t *c = packed_cell_alloc();
  1499. cell_pack(c, cell);
  1500. c->next = NULL;
  1501. return c;
  1502. }
  1503. /** Append <b>cell</b> to the end of <b>queue</b>. */
  1504. void
  1505. cell_queue_append(cell_queue_t *queue, packed_cell_t *cell)
  1506. {
  1507. if (queue->tail) {
  1508. tor_assert(!queue->tail->next);
  1509. queue->tail->next = cell;
  1510. } else {
  1511. queue->head = cell;
  1512. }
  1513. queue->tail = cell;
  1514. cell->next = NULL;
  1515. ++queue->n;
  1516. }
  1517. /** Append a newly allocated copy of <b>cell</b> to the end of <b>queue</b> */
  1518. void
  1519. cell_queue_append_packed_copy(cell_queue_t *queue, const cell_t *cell)
  1520. {
  1521. cell_queue_append(queue, packed_cell_copy(cell));
  1522. }
  1523. /** Remove and free every cell in <b>queue</b>. */
  1524. void
  1525. cell_queue_clear(cell_queue_t *queue)
  1526. {
  1527. packed_cell_t *cell, *next;
  1528. cell = queue->head;
  1529. while (cell) {
  1530. next = cell->next;
  1531. packed_cell_free(cell);
  1532. cell = next;
  1533. }
  1534. queue->head = queue->tail = NULL;
  1535. queue->n = 0;
  1536. }
  1537. /** Extract and return the cell at the head of <b>queue</b>; return NULL if
  1538. * <b>queue</b> is empty. */
  1539. static INLINE packed_cell_t *
  1540. cell_queue_pop(cell_queue_t *queue)
  1541. {
  1542. packed_cell_t *cell = queue->head;
  1543. if (!cell)
  1544. return NULL;
  1545. queue->head = cell->next;
  1546. if (cell == queue->tail) {
  1547. tor_assert(!queue->head);
  1548. queue->tail = NULL;
  1549. }
  1550. --queue->n;
  1551. return cell;
  1552. }
  1553. /** Return a pointer to the "next_active_on_{n,p}_conn" pointer of <b>circ</b>,
  1554. * depending on whether <b>conn</b> matches n_conn or p_conn. */
  1555. static INLINE circuit_t **
  1556. next_circ_on_conn_p(circuit_t *circ, or_connection_t *conn)
  1557. {
  1558. tor_assert(circ);
  1559. tor_assert(conn);
  1560. if (conn == circ->n_conn) {
  1561. return &circ->next_active_on_n_conn;
  1562. } else {
  1563. or_circuit_t *orcirc = TO_OR_CIRCUIT(circ);
  1564. tor_assert(conn == orcirc->p_conn);
  1565. return &orcirc->next_active_on_p_conn;
  1566. }
  1567. }
  1568. /** Return a pointer to the "prev_active_on_{n,p}_conn" pointer of <b>circ</b>,
  1569. * depending on whether <b>conn</b> matches n_conn or p_conn. */
  1570. static INLINE circuit_t **
  1571. prev_circ_on_conn_p(circuit_t *circ, or_connection_t *conn)
  1572. {
  1573. tor_assert(circ);
  1574. tor_assert(conn);
  1575. if (conn == circ->n_conn) {
  1576. return &circ->prev_active_on_n_conn;
  1577. } else {
  1578. or_circuit_t *orcirc = TO_OR_CIRCUIT(circ);
  1579. tor_assert(conn == orcirc->p_conn);
  1580. return &orcirc->prev_active_on_p_conn;
  1581. }
  1582. }
  1583. /** Add <b>circ</b> to the list of circuits with pending cells on
  1584. * <b>conn</b>. No effect if <b>circ</b> is already unlinked. */
  1585. void
  1586. make_circuit_active_on_conn(circuit_t *circ, or_connection_t *conn)
  1587. {
  1588. circuit_t **nextp = next_circ_on_conn_p(circ, conn);
  1589. circuit_t **prevp = prev_circ_on_conn_p(circ, conn);
  1590. if (*nextp && *prevp) {
  1591. /* Already active. */
  1592. return;
  1593. }
  1594. if (! conn->active_circuits) {
  1595. conn->active_circuits = circ;
  1596. *prevp = *nextp = circ;
  1597. } else {
  1598. circuit_t *head = conn->active_circuits;
  1599. circuit_t *old_tail = *prev_circ_on_conn_p(head, conn);
  1600. *next_circ_on_conn_p(old_tail, conn) = circ;
  1601. *nextp = head;
  1602. *prev_circ_on_conn_p(head, conn) = circ;
  1603. *prevp = old_tail;
  1604. }
  1605. assert_active_circuits_ok_paranoid(conn);
  1606. }
  1607. /** Remove <b>circ</b> to the list of circuits with pending cells on
  1608. * <b>conn</b>. No effect if <b>circ</b> is already unlinked. */
  1609. void
  1610. make_circuit_inactive_on_conn(circuit_t *circ, or_connection_t *conn)
  1611. {
  1612. circuit_t **nextp = next_circ_on_conn_p(circ, conn);
  1613. circuit_t **prevp = prev_circ_on_conn_p(circ, conn);
  1614. circuit_t *next = *nextp, *prev = *prevp;
  1615. if (!next && !prev) {
  1616. /* Already inactive. */
  1617. return;
  1618. }
  1619. tor_assert(next && prev);
  1620. tor_assert(*prev_circ_on_conn_p(next, conn) == circ);
  1621. tor_assert(*next_circ_on_conn_p(prev, conn) == circ);
  1622. if (next == circ) {
  1623. conn->active_circuits = NULL;
  1624. } else {
  1625. *prev_circ_on_conn_p(next, conn) = prev;
  1626. *next_circ_on_conn_p(prev, conn) = next;
  1627. if (conn->active_circuits == circ)
  1628. conn->active_circuits = next;
  1629. }
  1630. *prevp = *nextp = NULL;
  1631. assert_active_circuits_ok_paranoid(conn);
  1632. }
  1633. /** Remove all circuits from the list of circuits with pending cells on
  1634. * <b>conn</b>. */
  1635. void
  1636. connection_or_unlink_all_active_circs(or_connection_t *orconn)
  1637. {
  1638. circuit_t *head = orconn->active_circuits;
  1639. circuit_t *cur = head;
  1640. if (! head)
  1641. return;
  1642. do {
  1643. circuit_t *next = *next_circ_on_conn_p(cur, orconn);
  1644. *prev_circ_on_conn_p(cur, orconn) = NULL;
  1645. *next_circ_on_conn_p(cur, orconn) = NULL;
  1646. cur = next;
  1647. } while (cur != head);
  1648. orconn->active_circuits = NULL;
  1649. }
  1650. /** Block (if <b>block</b> is true) or unblock (if <b>block</b> is false)
  1651. * every edge connection that is using <b>circ</b> to write to <b>orconn</b>,
  1652. * and start or stop reading as appropriate. */
  1653. static void
  1654. set_streams_blocked_on_circ(circuit_t *circ, or_connection_t *orconn,
  1655. int block)
  1656. {
  1657. edge_connection_t *edge = NULL;
  1658. if (circ->n_conn == orconn) {
  1659. circ->streams_blocked_on_n_conn = block;
  1660. if (CIRCUIT_IS_ORIGIN(circ))
  1661. edge = TO_ORIGIN_CIRCUIT(circ)->p_streams;
  1662. } else {
  1663. circ->streams_blocked_on_p_conn = block;
  1664. tor_assert(!CIRCUIT_IS_ORIGIN(circ));
  1665. edge = TO_OR_CIRCUIT(circ)->n_streams;
  1666. }
  1667. for (; edge; edge = edge->next_stream) {
  1668. connection_t *conn = TO_CONN(edge);
  1669. conn->edge_blocked_on_circ = block;
  1670. if (!conn->read_event) {
  1671. /* This connection is a placeholder for something; probably a DNS
  1672. * request. It can't actually stop or start reading.*/
  1673. continue;
  1674. }
  1675. if (block) {
  1676. if (connection_is_reading(conn))
  1677. connection_stop_reading(conn);
  1678. } else {
  1679. /* Is this right? */
  1680. if (!connection_is_reading(conn))
  1681. connection_start_reading(conn);
  1682. }
  1683. }
  1684. }
  1685. /** Pull as many cells as possible (but no more than <b>max</b>) from the
  1686. * queue of the first active circuit on <b>conn</b>, and write then to
  1687. * <b>conn</b>-&gt;outbuf. Return the number of cells written. Advance
  1688. * the active circuit pointer to the next active circuit in the ring. */
  1689. int
  1690. connection_or_flush_from_first_active_circuit(or_connection_t *conn, int max)
  1691. {
  1692. int n_flushed;
  1693. cell_queue_t *queue;
  1694. circuit_t *circ;
  1695. int streams_blocked;
  1696. circ = conn->active_circuits;
  1697. if (!circ) return 0;
  1698. assert_active_circuits_ok_paranoid(conn);
  1699. if (circ->n_conn == conn) {
  1700. queue = &circ->n_conn_cells;
  1701. streams_blocked = circ->streams_blocked_on_n_conn;
  1702. } else {
  1703. queue = &TO_OR_CIRCUIT(circ)->p_conn_cells;
  1704. streams_blocked = circ->streams_blocked_on_p_conn;
  1705. }
  1706. tor_assert(*next_circ_on_conn_p(circ,conn));
  1707. for (n_flushed = 0; n_flushed < max && queue->head; ) {
  1708. packed_cell_t *cell = cell_queue_pop(queue);
  1709. tor_assert(*next_circ_on_conn_p(circ,conn));
  1710. connection_write_to_buf(cell->body, CELL_NETWORK_SIZE, TO_CONN(conn));
  1711. packed_cell_free(cell);
  1712. ++n_flushed;
  1713. if (circ != conn->active_circuits) {
  1714. /* If this happens, the current circuit just got made inactive by
  1715. * a call in connection_write_to_buf(). That's nothing to worry about:
  1716. * circuit_make_inactive_on_conn() already advanced conn->active_circuits
  1717. * for us.
  1718. */
  1719. assert_active_circuits_ok_paranoid(conn);
  1720. goto done;
  1721. }
  1722. }
  1723. tor_assert(*next_circ_on_conn_p(circ,conn));
  1724. assert_active_circuits_ok_paranoid(conn);
  1725. conn->active_circuits = *next_circ_on_conn_p(circ, conn);
  1726. /* Is the cell queue low enough to unblock all the streams that are waiting
  1727. * to write to this circuit? */
  1728. if (streams_blocked && queue->n <= CELL_QUEUE_LOWWATER_SIZE)
  1729. set_streams_blocked_on_circ(circ, conn, 0); /* unblock streams */
  1730. /* Did we just ran out of cells on this queue? */
  1731. if (queue->n == 0) {
  1732. log_debug(LD_GENERAL, "Made a circuit inactive.");
  1733. make_circuit_inactive_on_conn(circ, conn);
  1734. }
  1735. done:
  1736. if (n_flushed)
  1737. conn->timestamp_last_added_nonpadding = time(NULL);
  1738. return n_flushed;
  1739. }
  1740. /** Add <b>cell</b> to the queue of <b>circ</b> writing to <b>orconn</b>
  1741. * transmitting in <b>direction</b>. */
  1742. void
  1743. append_cell_to_circuit_queue(circuit_t *circ, or_connection_t *orconn,
  1744. cell_t *cell, int direction)
  1745. {
  1746. cell_queue_t *queue;
  1747. int streams_blocked;
  1748. if (direction == CELL_DIRECTION_OUT) {
  1749. queue = &circ->n_conn_cells;
  1750. streams_blocked = circ->streams_blocked_on_n_conn;
  1751. } else {
  1752. or_circuit_t *orcirc = TO_OR_CIRCUIT(circ);
  1753. queue = &orcirc->p_conn_cells;
  1754. streams_blocked = circ->streams_blocked_on_p_conn;
  1755. }
  1756. if (cell->command == CELL_RELAY_EARLY && orconn->link_proto < 2) {
  1757. /* V1 connections don't understand RELAY_EARLY. */
  1758. cell->command = CELL_RELAY;
  1759. }
  1760. cell_queue_append_packed_copy(queue, cell);
  1761. /* If we have too many cells on the circuit, we should stop reading from
  1762. * the edge streams for a while. */
  1763. if (!streams_blocked && queue->n >= CELL_QUEUE_HIGHWATER_SIZE)
  1764. set_streams_blocked_on_circ(circ, orconn, 1); /* block streams */
  1765. if (queue->n == 1) {
  1766. /* This was the first cell added to the queue. We need to make this
  1767. * circuit active. */
  1768. log_debug(LD_GENERAL, "Made a circuit active.");
  1769. make_circuit_active_on_conn(circ, orconn);
  1770. }
  1771. if (! buf_datalen(orconn->_base.outbuf)) {
  1772. /* There is no data at all waiting to be sent on the outbuf. Add a
  1773. * cell, so that we can notice when it gets flushed, flushed_some can
  1774. * get called, and we can start putting more data onto the buffer then.
  1775. */
  1776. log_debug(LD_GENERAL, "Primed a buffer.");
  1777. connection_or_flush_from_first_active_circuit(orconn, 1);
  1778. }
  1779. }
  1780. /** Fail with an assert if the active circuits ring on <b>orconn</b> is
  1781. * corrupt. */
  1782. void
  1783. assert_active_circuits_ok(or_connection_t *orconn)
  1784. {
  1785. circuit_t *head = orconn->active_circuits;
  1786. circuit_t *cur = head;
  1787. if (! head)
  1788. return;
  1789. do {
  1790. circuit_t *next = *next_circ_on_conn_p(cur, orconn);
  1791. circuit_t *prev = *prev_circ_on_conn_p(cur, orconn);
  1792. tor_assert(next);
  1793. tor_assert(prev);
  1794. tor_assert(*next_circ_on_conn_p(prev, orconn) == cur);
  1795. tor_assert(*prev_circ_on_conn_p(next, orconn) == cur);
  1796. cur = next;
  1797. } while (cur != head);
  1798. }