circuit.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "or.h"
  5. extern or_options_t options; /* command-line and config-file options */
  6. static void circuit_free_cpath(crypt_path_t *cpath);
  7. static void circuit_free_cpath_node(crypt_path_t *victim);
  8. static aci_t get_unique_aci_by_addr_port(uint32_t addr, uint16_t port, int aci_type);
  9. unsigned long stats_n_relay_cells_relayed = 0;
  10. unsigned long stats_n_relay_cells_delivered = 0;
  11. /********* START VARIABLES **********/
  12. static circuit_t *global_circuitlist=NULL;
  13. char *circuit_state_to_string[] = {
  14. "receiving the onion", /* 0 */
  15. "waiting to process create", /* 1 */
  16. "connecting to firsthop", /* 2 */
  17. "open" /* 3 */
  18. };
  19. /********* END VARIABLES ************/
  20. void circuit_add(circuit_t *circ) {
  21. if(!global_circuitlist) { /* first one */
  22. global_circuitlist = circ;
  23. circ->next = NULL;
  24. } else {
  25. circ->next = global_circuitlist;
  26. global_circuitlist = circ;
  27. }
  28. }
  29. void circuit_remove(circuit_t *circ) {
  30. circuit_t *tmpcirc;
  31. assert(circ && global_circuitlist);
  32. if(global_circuitlist == circ) {
  33. global_circuitlist = global_circuitlist->next;
  34. return;
  35. }
  36. for(tmpcirc = global_circuitlist;tmpcirc->next;tmpcirc = tmpcirc->next) {
  37. if(tmpcirc->next == circ) {
  38. tmpcirc->next = circ->next;
  39. return;
  40. }
  41. }
  42. }
  43. circuit_t *circuit_new(aci_t p_aci, connection_t *p_conn) {
  44. circuit_t *circ;
  45. circ = (circuit_t *)tor_malloc(sizeof(circuit_t));
  46. memset(circ,0,sizeof(circuit_t)); /* zero it out */
  47. circ->timestamp_created = time(NULL);
  48. circ->p_aci = p_aci;
  49. circ->p_conn = p_conn;
  50. circ->state = CIRCUIT_STATE_ONIONSKIN_PENDING;
  51. /* ACIs */
  52. circ->p_aci = p_aci;
  53. /* circ->n_aci remains 0 because we haven't identified the next hop yet */
  54. circ->package_window = CIRCWINDOW_START;
  55. circ->deliver_window = CIRCWINDOW_START;
  56. circuit_add(circ);
  57. return circ;
  58. }
  59. void circuit_free(circuit_t *circ) {
  60. if (circ->n_crypto)
  61. crypto_free_cipher_env(circ->n_crypto);
  62. if (circ->p_crypto)
  63. crypto_free_cipher_env(circ->p_crypto);
  64. circuit_free_cpath(circ->cpath);
  65. free(circ);
  66. }
  67. static void circuit_free_cpath(crypt_path_t *cpath) {
  68. crypt_path_t *victim, *head=cpath;
  69. if(!cpath)
  70. return;
  71. /* it's a doubly linked list, so we have to notice when we've
  72. * gone through it once. */
  73. while(cpath->next && cpath->next != head) {
  74. victim = cpath;
  75. cpath = victim->next;
  76. circuit_free_cpath_node(victim);
  77. }
  78. circuit_free_cpath_node(cpath);
  79. }
  80. static void circuit_free_cpath_node(crypt_path_t *victim) {
  81. if(victim->f_crypto)
  82. crypto_free_cipher_env(victim->f_crypto);
  83. if(victim->b_crypto)
  84. crypto_free_cipher_env(victim->b_crypto);
  85. if(victim->handshake_state)
  86. crypto_dh_free(victim->handshake_state);
  87. free(victim);
  88. }
  89. /* return 0 if can't get a unique aci. */
  90. static aci_t get_unique_aci_by_addr_port(uint32_t addr, uint16_t port, int aci_type) {
  91. aci_t test_aci;
  92. connection_t *conn;
  93. uint16_t high_bit;
  94. high_bit = (aci_type == ACI_TYPE_HIGHER) ? 1<<15 : 0;
  95. conn = connection_exact_get_by_addr_port(addr,port);
  96. /* XXX race condition: if conn is marked_for_close it won't be noticed */
  97. if (!conn)
  98. return (1|high_bit); /* No connection exists; conflict is impossible. */
  99. do {
  100. /* Sequentially iterate over test_aci=1...1<<15-1 until we find an
  101. * aci such that (high_bit|test_aci) is not already used. */
  102. /* XXX Will loop forever if all aci's in our range are used.
  103. * This matters because it's an external DoS vulnerability. */
  104. test_aci = conn->next_aci++;
  105. if (test_aci == 0 || test_aci >= 1<<15) {
  106. test_aci = 1;
  107. conn->next_aci = 2;
  108. }
  109. test_aci |= high_bit;
  110. } while(circuit_get_by_aci_conn(test_aci, conn));
  111. return test_aci;
  112. }
  113. circuit_t *circuit_enumerate_by_naddr_nport(circuit_t *circ, uint32_t naddr, uint16_t nport) {
  114. if(!circ) /* use circ if it's defined, else start from the beginning */
  115. circ = global_circuitlist;
  116. else
  117. circ = circ->next;
  118. for( ; circ; circ = circ->next) {
  119. if(circ->n_addr == naddr && circ->n_port == nport)
  120. return circ;
  121. }
  122. return NULL;
  123. }
  124. circuit_t *circuit_get_by_aci_conn(aci_t aci, connection_t *conn) {
  125. circuit_t *circ;
  126. connection_t *tmpconn;
  127. for(circ=global_circuitlist;circ;circ = circ->next) {
  128. if(circ->p_aci == aci) {
  129. if(circ->p_conn == conn)
  130. return circ;
  131. for(tmpconn = circ->p_streams; tmpconn; tmpconn = tmpconn->next_stream) {
  132. if(tmpconn == conn)
  133. return circ;
  134. }
  135. }
  136. if(circ->n_aci == aci) {
  137. if(circ->n_conn == conn)
  138. return circ;
  139. for(tmpconn = circ->n_streams; tmpconn; tmpconn = tmpconn->next_stream) {
  140. if(tmpconn == conn)
  141. return circ;
  142. }
  143. }
  144. }
  145. return NULL;
  146. }
  147. circuit_t *circuit_get_by_conn(connection_t *conn) {
  148. circuit_t *circ;
  149. connection_t *tmpconn;
  150. for(circ=global_circuitlist;circ;circ = circ->next) {
  151. if(circ->p_conn == conn)
  152. return circ;
  153. if(circ->n_conn == conn)
  154. return circ;
  155. for(tmpconn = circ->p_streams; tmpconn; tmpconn=tmpconn->next_stream)
  156. if(tmpconn == conn)
  157. return circ;
  158. for(tmpconn = circ->n_streams; tmpconn; tmpconn=tmpconn->next_stream)
  159. if(tmpconn == conn)
  160. return circ;
  161. }
  162. return NULL;
  163. }
  164. circuit_t *circuit_get_newest_open(void) {
  165. circuit_t *circ, *bestcirc=NULL;
  166. for(circ=global_circuitlist;circ;circ = circ->next) {
  167. if(circ->cpath && circ->state == CIRCUIT_STATE_OPEN && circ->n_conn && (!bestcirc ||
  168. bestcirc->timestamp_created < circ->timestamp_created)) {
  169. log_fn(LOG_DEBUG,"Choosing circuit %s:%d:%d.", circ->n_conn->address, circ->n_port, circ->n_aci);
  170. assert(circ->n_aci);
  171. bestcirc = circ;
  172. }
  173. }
  174. return bestcirc;
  175. }
  176. int circuit_deliver_relay_cell(cell_t *cell, circuit_t *circ,
  177. int cell_direction, crypt_path_t *layer_hint) {
  178. connection_t *conn=NULL;
  179. char recognized=0;
  180. char buf[256];
  181. assert(cell && circ);
  182. assert(cell_direction == CELL_DIRECTION_OUT || cell_direction == CELL_DIRECTION_IN);
  183. buf[0] = cell->length;
  184. memcpy(buf+1, cell->payload, CELL_PAYLOAD_SIZE);
  185. log_fn(LOG_DEBUG,"direction %d, streamid %d before crypt.", cell_direction, *(int*)(cell->payload+1));
  186. if(relay_crypt(circ, buf, 1+CELL_PAYLOAD_SIZE, cell_direction, &layer_hint, &recognized, &conn) < 0) {
  187. log_fn(LOG_WARN,"relay crypt failed. Dropping connection.");
  188. return -1;
  189. }
  190. cell->length = buf[0];
  191. memcpy(cell->payload, buf+1, CELL_PAYLOAD_SIZE);
  192. if(recognized) {
  193. if(cell_direction == CELL_DIRECTION_OUT) {
  194. ++stats_n_relay_cells_delivered;
  195. log_fn(LOG_DEBUG,"Sending to exit.");
  196. return connection_edge_process_relay_cell(cell, circ, conn, EDGE_EXIT, NULL);
  197. }
  198. if(cell_direction == CELL_DIRECTION_IN) {
  199. ++stats_n_relay_cells_delivered;
  200. log_fn(LOG_DEBUG,"Sending to AP.");
  201. return connection_edge_process_relay_cell(cell, circ, conn, EDGE_AP, layer_hint);
  202. }
  203. }
  204. ++stats_n_relay_cells_relayed;
  205. /* not recognized. pass it on. */
  206. if(cell_direction == CELL_DIRECTION_OUT)
  207. conn = circ->n_conn;
  208. else
  209. conn = circ->p_conn;
  210. if(!conn) { //|| !connection_speaks_cells(conn)) {
  211. log_fn(LOG_INFO,"Didn't recognize cell (%d), but circ stops here! Dropping.", *(int *)(cell->payload+1));
  212. return 0;
  213. }
  214. log_fn(LOG_DEBUG,"Passing on unrecognized cell.");
  215. connection_or_write_cell_to_buf(cell, conn);
  216. return 0;
  217. }
  218. int relay_crypt(circuit_t *circ, char *in, int inlen, char cell_direction,
  219. crypt_path_t **layer_hint, char *recognized, connection_t **conn) {
  220. crypt_path_t *thishop;
  221. char out[256];
  222. assert(circ && in && recognized && conn);
  223. assert(inlen < 256);
  224. if(cell_direction == CELL_DIRECTION_IN) {
  225. if(circ->cpath) { /* we're at the beginning of the circuit. We'll want to do layered crypts. */
  226. thishop = circ->cpath;
  227. if(thishop->state != CPATH_STATE_OPEN) {
  228. log_fn(LOG_WARN,"Relay cell before first created cell?");
  229. return -1;
  230. }
  231. do { /* Remember: cpath is in forward order, that is, first hop first. */
  232. assert(thishop);
  233. log_fn(LOG_DEBUG,"before decrypt: %d",*(int*)(in+2));
  234. /* decrypt */
  235. if(crypto_cipher_decrypt(thishop->b_crypto, in, inlen, out)) {
  236. log_fn(LOG_WARN,"Error performing onion decryption: %s", crypto_perror());
  237. return -1;
  238. }
  239. memcpy(in,out,inlen);
  240. log_fn(LOG_DEBUG,"after decrypt: %d",*(int*)(in+2));
  241. if( (*recognized = relay_check_recognized(circ, cell_direction, in+2, conn))) {
  242. *layer_hint = thishop;
  243. return 0;
  244. }
  245. thishop = thishop->next;
  246. } while(thishop != circ->cpath && thishop->state == CPATH_STATE_OPEN);
  247. log_fn(LOG_INFO,"in-cell at OP not recognized. Dropping.");
  248. return 0;
  249. } else { /* we're in the middle. Just one crypt. */
  250. log_fn(LOG_DEBUG,"before encrypt: %d",*(int*)(in+2));
  251. if(crypto_cipher_encrypt(circ->p_crypto, in, inlen, out)) {
  252. log_fn(LOG_WARN,"Onion encryption failed for ACI %u: %s",
  253. circ->p_aci, crypto_perror());
  254. return -1;
  255. }
  256. memcpy(in,out,inlen);
  257. log_fn(LOG_DEBUG,"after encrypt: %d",*(int*)(in+2));
  258. log_fn(LOG_DEBUG,"Skipping recognized check, because we're not the OP.");
  259. /* don't check for recognized. only the OP can recognize a stream on the way back. */
  260. }
  261. } else if(cell_direction == CELL_DIRECTION_OUT) {
  262. if(circ->cpath) { /* we're at the beginning of the circuit. We'll want to do layered crypts. */
  263. thishop = *layer_hint; /* we already know which layer, from when we package_raw_inbuf'ed */
  264. /* moving from last to first hop */
  265. do {
  266. assert(thishop);
  267. log_fn(LOG_DEBUG,"before encrypt: %d",*(int*)(in+2));
  268. if(crypto_cipher_encrypt(thishop->f_crypto, in, inlen, out)) {
  269. log_fn(LOG_WARN,"Error performing encryption: %s", crypto_perror());
  270. return -1;
  271. }
  272. memcpy(in,out,inlen);
  273. log_fn(LOG_DEBUG,"after encrypt: %d",*(int*)(in+2));
  274. thishop = thishop->prev;
  275. } while(thishop != circ->cpath->prev);
  276. } else { /* we're in the middle. Just one crypt. */
  277. if(crypto_cipher_decrypt(circ->n_crypto,in, inlen, out)) {
  278. log_fn(LOG_WARN,"Decryption failed for ACI %u: %s",
  279. circ->n_aci, crypto_perror());
  280. return -1;
  281. }
  282. memcpy(in,out,inlen);
  283. if( (*recognized = relay_check_recognized(circ, cell_direction, in+2, conn)))
  284. return 0;
  285. }
  286. } else {
  287. log_fn(LOG_ERR,"unknown cell direction %d.", cell_direction);
  288. assert(0);
  289. }
  290. return 0;
  291. }
  292. int relay_check_recognized(circuit_t *circ, int cell_direction, char *stream, connection_t **conn) {
  293. /* FIXME can optimize by passing thishop in */
  294. connection_t *tmpconn;
  295. if(!memcmp(stream,ZERO_STREAM,STREAM_ID_SIZE)) {
  296. log_fn(LOG_DEBUG,"It's the zero stream. Recognized.");
  297. return 1; /* the zero stream is always recognized */
  298. }
  299. log_fn(LOG_DEBUG,"not the zero stream.");
  300. if(cell_direction == CELL_DIRECTION_OUT)
  301. tmpconn = circ->n_streams;
  302. else
  303. tmpconn = circ->p_streams;
  304. if(!tmpconn) {
  305. log_fn(LOG_DEBUG,"No conns. Not recognized.");
  306. return 0;
  307. }
  308. for( ; tmpconn; tmpconn=tmpconn->next_stream) {
  309. if(!memcmp(stream,tmpconn->stream_id, STREAM_ID_SIZE)) {
  310. log_fn(LOG_DEBUG,"recognized stream %d.", *(int*)stream);
  311. *conn = tmpconn;
  312. return 1;
  313. }
  314. log_fn(LOG_DEBUG,"considered stream %d, not it.",*(int*)tmpconn->stream_id);
  315. }
  316. log_fn(LOG_DEBUG,"Didn't recognize on this iteration of decryption.");
  317. return 0;
  318. }
  319. void circuit_resume_edge_reading(circuit_t *circ, int edge_type, crypt_path_t *layer_hint) {
  320. connection_t *conn;
  321. assert(edge_type == EDGE_EXIT || edge_type == EDGE_AP);
  322. log_fn(LOG_DEBUG,"resuming");
  323. if(edge_type == EDGE_EXIT)
  324. conn = circ->n_streams;
  325. else
  326. conn = circ->p_streams;
  327. for( ; conn; conn=conn->next_stream) {
  328. if((edge_type == EDGE_EXIT && conn->package_window > 0) ||
  329. (edge_type == EDGE_AP && conn->package_window > 0 && conn->cpath_layer == layer_hint)) {
  330. connection_start_reading(conn);
  331. connection_edge_package_raw_inbuf(conn); /* handle whatever might still be on the inbuf */
  332. /* If the circuit won't accept any more data, return without looking
  333. * at any more of the streams. Any connections that should be stopped
  334. * have already been stopped by connection_edge_package_raw_inbuf. */
  335. if(circuit_consider_stop_edge_reading(circ, edge_type, layer_hint))
  336. return;
  337. }
  338. }
  339. }
  340. /* returns 1 if the window is empty, else 0. If it's empty, tell edge conns to stop reading. */
  341. int circuit_consider_stop_edge_reading(circuit_t *circ, int edge_type, crypt_path_t *layer_hint) {
  342. connection_t *conn = NULL;
  343. assert(edge_type == EDGE_EXIT || edge_type == EDGE_AP);
  344. assert(edge_type == EDGE_EXIT || layer_hint);
  345. log_fn(LOG_DEBUG,"considering");
  346. if(edge_type == EDGE_EXIT && circ->package_window <= 0)
  347. conn = circ->n_streams;
  348. else if(edge_type == EDGE_AP && layer_hint->package_window <= 0)
  349. conn = circ->p_streams;
  350. else
  351. return 0;
  352. for( ; conn; conn=conn->next_stream)
  353. if(!layer_hint || conn->cpath_layer == layer_hint)
  354. connection_stop_reading(conn);
  355. log_fn(LOG_DEBUG,"yes. stopped.");
  356. return 1;
  357. }
  358. int circuit_consider_sending_sendme(circuit_t *circ, int edge_type, crypt_path_t *layer_hint) {
  359. cell_t cell;
  360. assert(circ);
  361. memset(&cell, 0, sizeof(cell_t));
  362. cell.command = CELL_RELAY;
  363. SET_CELL_RELAY_COMMAND(cell, RELAY_COMMAND_SENDME);
  364. SET_CELL_STREAM_ID(cell, ZERO_STREAM);
  365. cell.length = RELAY_HEADER_SIZE;
  366. if(edge_type == EDGE_AP) { /* i'm the AP */
  367. cell.aci = circ->n_aci;
  368. while(layer_hint->deliver_window < CIRCWINDOW_START-CIRCWINDOW_INCREMENT) {
  369. log_fn(LOG_DEBUG,"deliver_window %d, Queueing sendme forward.", layer_hint->deliver_window);
  370. layer_hint->deliver_window += CIRCWINDOW_INCREMENT;
  371. if(circuit_deliver_relay_cell(&cell, circ, CELL_DIRECTION_OUT, layer_hint) < 0) {
  372. return -1;
  373. }
  374. }
  375. } else if(edge_type == EDGE_EXIT) { /* i'm the exit */
  376. cell.aci = circ->p_aci;
  377. while(circ->deliver_window < CIRCWINDOW_START-CIRCWINDOW_INCREMENT) {
  378. log_fn(LOG_DEBUG,"deliver_window %d, Queueing sendme back.", circ->deliver_window);
  379. circ->deliver_window += CIRCWINDOW_INCREMENT;
  380. if(circuit_deliver_relay_cell(&cell, circ, CELL_DIRECTION_IN, layer_hint) < 0) {
  381. return -1;
  382. }
  383. }
  384. }
  385. return 0;
  386. }
  387. void circuit_close(circuit_t *circ) {
  388. connection_t *conn;
  389. circuit_t *youngest=NULL;
  390. assert(circ);
  391. if(options.SocksPort) {
  392. youngest = circuit_get_newest_open();
  393. log_fn(LOG_DEBUG,"youngest %d, circ %d.",(int)youngest, (int)circ);
  394. }
  395. circuit_remove(circ);
  396. if(circ->n_conn)
  397. connection_send_destroy(circ->n_aci, circ->n_conn);
  398. for(conn=circ->n_streams; conn; conn=conn->next_stream) {
  399. connection_send_destroy(circ->n_aci, conn);
  400. }
  401. if(circ->p_conn)
  402. connection_send_destroy(circ->n_aci, circ->p_conn);
  403. for(conn=circ->p_streams; conn; conn=conn->next_stream) {
  404. connection_send_destroy(circ->p_aci, conn);
  405. }
  406. if(options.SocksPort && youngest == circ) { /* check this after we've sent the destroys, to reduce races */
  407. /* our current circuit just died. Launch another one pronto. */
  408. log_fn(LOG_INFO,"Youngest circuit dying. Launching a replacement.");
  409. circuit_launch_new(1);
  410. }
  411. circuit_free(circ);
  412. }
  413. void circuit_about_to_close_connection(connection_t *conn) {
  414. /* send destroys for all circuits using conn */
  415. /* currently, we assume it's too late to flush conn's buf here.
  416. * down the road, maybe we'll consider that eof doesn't mean can't-write
  417. */
  418. circuit_t *circ;
  419. connection_t *prevconn;
  420. switch(conn->type) {
  421. case CONN_TYPE_OR:
  422. /* We must close all the circuits on it. */
  423. while((circ = circuit_get_by_conn(conn))) {
  424. if(circ->n_conn == conn) /* it's closing in front of us */
  425. circ->n_conn = NULL;
  426. if(circ->p_conn == conn) /* it's closing behind us */
  427. circ->p_conn = NULL;
  428. circuit_close(circ);
  429. }
  430. return;
  431. case CONN_TYPE_AP:
  432. case CONN_TYPE_EXIT:
  433. /* It's an edge conn. Need to remove it from the linked list of
  434. * conn's for this circuit. Confirm that 'end' relay command has
  435. * been sent. But don't kill the circuit.
  436. */
  437. circ = circuit_get_by_conn(conn);
  438. if(!circ)
  439. return;
  440. if(!conn->has_sent_end) {
  441. log_fn(LOG_INFO,"Edge connection hasn't sent end yet? Bug.");
  442. connection_edge_send_command(conn, circ, RELAY_COMMAND_END,
  443. NULL, 0, conn->cpath_layer);
  444. }
  445. if(conn == circ->p_streams) {
  446. circ->p_streams = conn->next_stream;
  447. return;
  448. }
  449. if(conn == circ->n_streams) {
  450. circ->n_streams = conn->next_stream;
  451. return;
  452. }
  453. for(prevconn = circ->p_streams; prevconn && prevconn->next_stream && prevconn->next_stream != conn; prevconn = prevconn->next_stream) ;
  454. if(prevconn && prevconn->next_stream) {
  455. prevconn->next_stream = conn->next_stream;
  456. return;
  457. }
  458. for(prevconn = circ->n_streams; prevconn && prevconn->next_stream && prevconn->next_stream != conn; prevconn = prevconn->next_stream) ;
  459. if(prevconn && prevconn->next_stream) {
  460. prevconn->next_stream = conn->next_stream;
  461. return;
  462. }
  463. log_fn(LOG_ERR,"edge conn not in circuit's list?");
  464. assert(0); /* should never get here */
  465. } /* end switch */
  466. }
  467. /* FIXME this now leaves some out */
  468. void circuit_dump_by_conn(connection_t *conn, int severity) {
  469. circuit_t *circ;
  470. connection_t *tmpconn;
  471. for(circ=global_circuitlist;circ;circ = circ->next) {
  472. if(circ->p_conn == conn)
  473. log(severity, "Conn %d has App-ward circuit: aci %d (other side %d), state %d (%s)",
  474. conn->poll_index, circ->p_aci, circ->n_aci, circ->state, circuit_state_to_string[circ->state]);
  475. for(tmpconn=circ->p_streams; tmpconn; tmpconn=tmpconn->next_stream) {
  476. if(tmpconn == conn) {
  477. log(severity,"Conn %d has App-ward circuit: aci %d (other side %d), state %d (%s)",
  478. conn->poll_index, circ->p_aci, circ->n_aci, circ->state, circuit_state_to_string[circ->state]);
  479. }
  480. }
  481. if(circ->n_conn == conn)
  482. log(severity,"Conn %d has Exit-ward circuit: aci %d (other side %d), state %d (%s)",
  483. conn->poll_index, circ->n_aci, circ->p_aci, circ->state, circuit_state_to_string[circ->state]);
  484. for(tmpconn=circ->n_streams; tmpconn; tmpconn=tmpconn->next_stream) {
  485. if(tmpconn == conn) {
  486. log(severity,"Conn %d has Exit-ward circuit: aci %d (other side %d), state %d (%s)",
  487. conn->poll_index, circ->n_aci, circ->p_aci, circ->state, circuit_state_to_string[circ->state]);
  488. }
  489. }
  490. }
  491. }
  492. void circuit_expire_unused_circuits(void) {
  493. circuit_t *circ, *tmpcirc;
  494. circuit_t *youngest;
  495. youngest = circuit_get_newest_open();
  496. circ = global_circuitlist;
  497. while(circ) {
  498. tmpcirc = circ;
  499. circ = circ->next;
  500. if(tmpcirc != youngest && !tmpcirc->p_conn && !tmpcirc->p_streams) {
  501. log_fn(LOG_DEBUG,"Closing n_aci %d",tmpcirc->n_aci);
  502. circuit_close(tmpcirc);
  503. }
  504. }
  505. }
  506. /* failure_status code: negative means reset failures to 0. Other values mean
  507. * add that value to the current number of failures, then if we don't have too
  508. * many failures on record, try to make a new circuit.
  509. */
  510. void circuit_launch_new(int failure_status) {
  511. static int failures=0;
  512. if(!options.SocksPort) /* we're not an application proxy. no need for circuits. */
  513. return;
  514. if(failure_status == -1) { /* I was called because a circuit succeeded */
  515. failures = 0;
  516. return;
  517. }
  518. failures += failure_status;
  519. retry_circuit:
  520. if(failures > 5) {
  521. log_fn(LOG_INFO,"Giving up for now, %d failures.", failures);
  522. return;
  523. }
  524. if(circuit_establish_circuit() < 0) {
  525. failures++;
  526. goto retry_circuit;
  527. }
  528. failures = 0;
  529. return;
  530. }
  531. int circuit_establish_circuit(void) {
  532. routerinfo_t *firsthop;
  533. connection_t *n_conn;
  534. circuit_t *circ;
  535. circ = circuit_new(0, NULL); /* sets circ->p_aci and circ->p_conn */
  536. circ->state = CIRCUIT_STATE_OR_WAIT;
  537. circ->cpath = onion_generate_cpath(&firsthop);
  538. if(!circ->cpath) {
  539. log_fn(LOG_INFO,"Generating cpath failed.");
  540. circuit_close(circ);
  541. return -1;
  542. }
  543. /* now see if we're already connected to the first OR in 'route' */
  544. log_fn(LOG_DEBUG,"Looking for firsthop '%s:%u'",
  545. firsthop->address,firsthop->or_port);
  546. n_conn = connection_twin_get_by_addr_port(firsthop->addr,firsthop->or_port);
  547. if(!n_conn || n_conn->state != OR_CONN_STATE_OPEN) { /* not currently connected */
  548. circ->n_addr = firsthop->addr;
  549. circ->n_port = firsthop->or_port;
  550. if(options.OnionRouter) { /* we would be connected if he were up. but he's not. */
  551. log_fn(LOG_INFO,"Route's firsthop isn't connected.");
  552. circuit_close(circ);
  553. return -1;
  554. }
  555. if(!n_conn) { /* launch the connection */
  556. n_conn = connection_or_connect(firsthop);
  557. if(!n_conn) { /* connect failed, forget the whole thing */
  558. log_fn(LOG_INFO,"connect to firsthop failed. Closing.");
  559. circuit_close(circ);
  560. return -1;
  561. }
  562. }
  563. log_fn(LOG_DEBUG,"connecting in progress (or finished). Good.");
  564. return 0; /* return success. The onion/circuit/etc will be taken care of automatically
  565. * (may already have been) whenever n_conn reaches OR_CONN_STATE_OPEN.
  566. */
  567. } else { /* it (or a twin) is already open. use it. */
  568. circ->n_addr = n_conn->addr;
  569. circ->n_port = n_conn->port;
  570. circ->n_conn = n_conn;
  571. log_fn(LOG_DEBUG,"Conn open. Delivering first onion skin.");
  572. if(circuit_send_next_onion_skin(circ) < 0) {
  573. log_fn(LOG_INFO,"circuit_send_next_onion_skin failed.");
  574. circuit_close(circ);
  575. return -1;
  576. }
  577. }
  578. return 0;
  579. }
  580. /* find circuits that are waiting on me, if any, and get them to send the onion */
  581. void circuit_n_conn_open(connection_t *or_conn) {
  582. circuit_t *circ;
  583. log_fn(LOG_DEBUG,"Starting.");
  584. circ = circuit_enumerate_by_naddr_nport(NULL, or_conn->addr, or_conn->port);
  585. for(;;) {
  586. if(!circ)
  587. return;
  588. log_fn(LOG_DEBUG,"Found circ, sending onion skin.");
  589. circ->n_conn = or_conn;
  590. if(circuit_send_next_onion_skin(circ) < 0) {
  591. log_fn(LOG_INFO,"send_next_onion_skin failed; circuit marked for closing.");
  592. circuit_close(circ);
  593. return; /* FIXME will want to try the other circuits too? */
  594. }
  595. circ = circuit_enumerate_by_naddr_nport(circ, or_conn->addr, or_conn->port);
  596. }
  597. }
  598. int circuit_send_next_onion_skin(circuit_t *circ) {
  599. cell_t cell;
  600. crypt_path_t *hop;
  601. routerinfo_t *router;
  602. assert(circ && circ->cpath);
  603. if(circ->cpath->state == CPATH_STATE_CLOSED) {
  604. log_fn(LOG_DEBUG,"First skin; sending create cell.");
  605. circ->n_aci = get_unique_aci_by_addr_port(circ->n_addr, circ->n_port, ACI_TYPE_BOTH);
  606. memset(&cell, 0, sizeof(cell_t));
  607. cell.command = CELL_CREATE;
  608. cell.aci = circ->n_aci;
  609. cell.length = DH_ONIONSKIN_LEN;
  610. if(onion_skin_create(circ->n_conn->onion_pkey, &(circ->cpath->handshake_state), cell.payload) < 0) {
  611. log_fn(LOG_WARN,"onion_skin_create (first hop) failed.");
  612. return -1;
  613. }
  614. connection_or_write_cell_to_buf(&cell, circ->n_conn);
  615. circ->cpath->state = CPATH_STATE_AWAITING_KEYS;
  616. circ->state = CIRCUIT_STATE_BUILDING;
  617. log_fn(LOG_DEBUG,"first skin; finished sending create cell.");
  618. } else {
  619. assert(circ->cpath->state == CPATH_STATE_OPEN);
  620. assert(circ->state == CIRCUIT_STATE_BUILDING);
  621. log_fn(LOG_DEBUG,"starting to send subsequent skin.");
  622. for(hop=circ->cpath->next;
  623. hop != circ->cpath && hop->state == CPATH_STATE_OPEN;
  624. hop=hop->next) ;
  625. if(hop == circ->cpath) { /* done building the circuit. whew. */
  626. circ->state = CIRCUIT_STATE_OPEN;
  627. log_fn(LOG_INFO,"circuit built!");
  628. return 0;
  629. }
  630. router = router_get_by_addr_port(hop->addr,hop->port);
  631. if(!router) {
  632. log_fn(LOG_WARN,"couldn't lookup router %d:%d",hop->addr,hop->port);
  633. return -1;
  634. }
  635. memset(&cell, 0, sizeof(cell_t));
  636. cell.command = CELL_RELAY;
  637. cell.aci = circ->n_aci;
  638. SET_CELL_RELAY_COMMAND(cell, RELAY_COMMAND_EXTEND);
  639. SET_CELL_STREAM_ID(cell, ZERO_STREAM);
  640. cell.length = RELAY_HEADER_SIZE + 6 + DH_ONIONSKIN_LEN;
  641. *(uint32_t*)(cell.payload+RELAY_HEADER_SIZE) = htonl(hop->addr);
  642. *(uint16_t*)(cell.payload+RELAY_HEADER_SIZE+4) = htons(hop->port);
  643. if(onion_skin_create(router->onion_pkey, &(hop->handshake_state), cell.payload+RELAY_HEADER_SIZE+6) < 0) {
  644. log_fn(LOG_WARN,"onion_skin_create failed.");
  645. return -1;
  646. }
  647. log_fn(LOG_DEBUG,"Sending extend relay cell.");
  648. /* send it to hop->prev, because it will transfer it to a create cell and then send to hop */
  649. if(circuit_deliver_relay_cell(&cell, circ, CELL_DIRECTION_OUT, hop->prev) < 0) {
  650. log_fn(LOG_WARN,"failed to deliver extend cell. Closing.");
  651. return -1;
  652. }
  653. hop->state = CPATH_STATE_AWAITING_KEYS;
  654. }
  655. return 0;
  656. }
  657. /* take the 'extend' cell, pull out addr/port plus the onion skin. Make
  658. * sure we're connected to the next hop, and pass it the onion skin in
  659. * a create cell.
  660. */
  661. int circuit_extend(cell_t *cell, circuit_t *circ) {
  662. connection_t *n_conn;
  663. aci_t aci_type;
  664. cell_t newcell;
  665. if(circ->n_conn) {
  666. log_fn(LOG_WARN,"n_conn already set. Bug/attack. Closing.");
  667. return -1;
  668. }
  669. circ->n_addr = ntohl(*(uint32_t*)(cell->payload+RELAY_HEADER_SIZE));
  670. circ->n_port = ntohs(*(uint16_t*)(cell->payload+RELAY_HEADER_SIZE+4));
  671. n_conn = connection_twin_get_by_addr_port(circ->n_addr,circ->n_port);
  672. if(!n_conn || n_conn->type != CONN_TYPE_OR) {
  673. /* i've disabled making connections through OPs, but it's definitely
  674. * possible here. I'm not sure if it would be a bug or a feature. -RD
  675. */
  676. /* note also that this will close circuits where the onion has the same
  677. * router twice in a row in the path. i think that's ok. -RD
  678. */
  679. struct in_addr in;
  680. in.s_addr = htonl(circ->n_addr);
  681. log_fn(LOG_DEBUG,"Next router (%s:%d) not connected. Closing.", inet_ntoa(in), circ->n_port);
  682. /* XXX later we should fail more gracefully here, like with a 'truncated' */
  683. return -1;
  684. }
  685. circ->n_addr = n_conn->addr; /* these are different if we found a twin instead */
  686. circ->n_port = n_conn->port;
  687. circ->n_conn = n_conn;
  688. log_fn(LOG_DEBUG,"n_conn is %s:%u",n_conn->address,n_conn->port);
  689. aci_type = decide_aci_type(options.Nickname, n_conn->nickname);
  690. log_fn(LOG_DEBUG,"aci_type = %u.",aci_type);
  691. circ->n_aci = get_unique_aci_by_addr_port(circ->n_addr, circ->n_port, aci_type);
  692. if(!circ->n_aci) {
  693. log_fn(LOG_WARN,"failed to get unique aci.");
  694. return -1;
  695. }
  696. log_fn(LOG_DEBUG,"Chosen ACI %u.",circ->n_aci);
  697. memset(&newcell, 0, sizeof(cell_t));
  698. newcell.command = CELL_CREATE;
  699. newcell.aci = circ->n_aci;
  700. newcell.length = DH_ONIONSKIN_LEN;
  701. memcpy(newcell.payload, cell->payload+RELAY_HEADER_SIZE+6, DH_ONIONSKIN_LEN);
  702. connection_or_write_cell_to_buf(&newcell, circ->n_conn);
  703. return 0;
  704. }
  705. int circuit_finish_handshake(circuit_t *circ, char *reply) {
  706. unsigned char iv[16];
  707. unsigned char keys[32];
  708. crypt_path_t *hop;
  709. memset(iv, 0, 16);
  710. assert(circ->cpath);
  711. if(circ->cpath->state == CPATH_STATE_AWAITING_KEYS)
  712. hop = circ->cpath;
  713. else {
  714. for(hop=circ->cpath->next;
  715. hop != circ->cpath && hop->state == CPATH_STATE_OPEN;
  716. hop=hop->next) ;
  717. if(hop == circ->cpath) { /* got an extended when we're all done? */
  718. log_fn(LOG_WARN,"got extended when circ already built? Closing.");
  719. return -1;
  720. }
  721. }
  722. assert(hop->state == CPATH_STATE_AWAITING_KEYS);
  723. if(onion_skin_client_handshake(hop->handshake_state, reply, keys, 32) < 0) {
  724. log_fn(LOG_WARN,"onion_skin_client_handshake failed.");
  725. return -1;
  726. }
  727. crypto_dh_free(hop->handshake_state); /* don't need it anymore */
  728. hop->handshake_state = NULL;
  729. log_fn(LOG_DEBUG,"hop %d init cipher forward %d, backward %d.", (uint32_t)hop, *(uint32_t*)keys, *(uint32_t*)(keys+16));
  730. if (!(hop->f_crypto =
  731. crypto_create_init_cipher(CIRCUIT_CIPHER,keys,iv,1))) {
  732. log(LOG_WARN,"forward cipher initialization failed.");
  733. return -1;
  734. }
  735. if (!(hop->b_crypto =
  736. crypto_create_init_cipher(CIRCUIT_CIPHER,keys+16,iv,0))) {
  737. log(LOG_WARN,"backward cipher initialization failed.");
  738. return -1;
  739. }
  740. hop->state = CPATH_STATE_OPEN;
  741. log_fn(LOG_INFO,"finished");
  742. return 0;
  743. }
  744. int circuit_truncated(circuit_t *circ, crypt_path_t *layer) {
  745. crypt_path_t *victim;
  746. connection_t *stream;
  747. assert(circ);
  748. assert(layer);
  749. while(layer->next != circ->cpath) {
  750. /* we need to clear out layer->next */
  751. victim = layer->next;
  752. log_fn(LOG_DEBUG, "Killing a layer of the cpath.");
  753. for(stream = circ->p_streams; stream; stream=stream->next_stream) {
  754. if(stream->cpath_layer == victim) {
  755. log_fn(LOG_INFO, "Marking stream %d for close.", *(int*)stream->stream_id);
  756. /* no need to send 'end' relay cells,
  757. * because the other side's already dead
  758. */
  759. stream->marked_for_close = 1;
  760. stream->has_sent_end = 1;
  761. }
  762. }
  763. layer->next = victim->next;
  764. circuit_free_cpath_node(victim);
  765. }
  766. log_fn(LOG_INFO, "finished");
  767. return 0;
  768. }
  769. void assert_cpath_layer_ok(const crypt_path_t *cp)
  770. {
  771. assert(cp->f_crypto);
  772. assert(cp->b_crypto);
  773. assert(cp->addr);
  774. assert(cp->port);
  775. switch(cp->state)
  776. {
  777. case CPATH_STATE_CLOSED:
  778. case CPATH_STATE_OPEN:
  779. assert(!cp->handshake_state);
  780. break;
  781. case CPATH_STATE_AWAITING_KEYS:
  782. assert(cp->handshake_state);
  783. break;
  784. default:
  785. assert(0);
  786. }
  787. assert(cp->package_window >= 0);
  788. assert(cp->deliver_window >= 0);
  789. }
  790. void assert_cpath_ok(const crypt_path_t *cp)
  791. {
  792. while(cp->prev)
  793. cp = cp->prev;
  794. while(cp->next) {
  795. assert_cpath_layer_ok(cp);
  796. /* layers must be in sequence of: "open* awaiting? closed*" */
  797. if (cp->prev) {
  798. if (cp->prev->state == CPATH_STATE_OPEN) {
  799. assert(cp->state == CPATH_STATE_CLOSED ||
  800. cp->state == CPATH_STATE_AWAITING_KEYS);
  801. } else {
  802. assert(cp->state == CPATH_STATE_CLOSED);
  803. }
  804. }
  805. cp = cp->next;
  806. }
  807. }
  808. void assert_circuit_ok(const circuit_t *c)
  809. {
  810. connection_t *conn;
  811. assert(c->n_addr);
  812. assert(c->n_port);
  813. assert(c->n_conn);
  814. assert(c->n_conn->type == CONN_TYPE_OR);
  815. if (c->p_conn)
  816. assert(c->p_conn->type == CONN_TYPE_OR);
  817. for (conn = c->p_streams; conn; conn = conn->next_stream)
  818. assert(c->p_conn->type == CONN_TYPE_EXIT);
  819. for (conn = c->n_streams; conn; conn = conn->next_stream)
  820. assert(conn->type == CONN_TYPE_EXIT);
  821. assert(c->deliver_window >= 0);
  822. assert(c->package_window >= 0);
  823. if (c->state == CIRCUIT_STATE_OPEN) {
  824. if (c->cpath) {
  825. assert(!c->n_crypto);
  826. assert(!c->p_crypto);
  827. } else {
  828. assert(c->n_crypto);
  829. assert(c->p_crypto);
  830. }
  831. }
  832. if (c->cpath) {
  833. assert_cpath_ok(c->cpath);
  834. }
  835. }
  836. /*
  837. Local Variables:
  838. mode:c
  839. indent-tabs-mode:nil
  840. c-basic-offset:2
  841. End:
  842. */