connection_ap.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /* Copyright 2001,2002 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "or.h"
  5. extern int global_role; /* from main.c */
  6. int connection_ap_process_inbuf(connection_t *conn) {
  7. assert(conn && conn->type == CONN_TYPE_AP);
  8. if(conn->inbuf_reached_eof) {
  9. /* eof reached, kill it. */
  10. log(LOG_DEBUG,"connection_ap_process_inbuf(): conn reached eof. Closing.");
  11. return -1;
  12. }
  13. // log(LOG_DEBUG,"connection_ap_process_inbuf(): state %d.",conn->state);
  14. switch(conn->state) {
  15. case AP_CONN_STATE_SOCKS_WAIT:
  16. return ap_handshake_process_socks(conn);
  17. case AP_CONN_STATE_OPEN:
  18. if(connection_package_raw_inbuf(conn) < 0)
  19. return -1;
  20. circuit_consider_stop_edge_reading(circuit_get_by_conn(conn), EDGE_AP);
  21. return 0;
  22. default:
  23. log(LOG_DEBUG,"connection_ap_process_inbuf() called in state where I'm waiting. Ignoring buf for now.");
  24. }
  25. return 0;
  26. }
  27. int ap_handshake_process_socks(connection_t *conn) {
  28. char c;
  29. socks4_t socks4_info;
  30. circuit_t *circ;
  31. assert(conn);
  32. log(LOG_DEBUG,"ap_handshake_process_socks() entered.");
  33. if(!conn->socks_version) { /* try to pull it in */
  34. if(conn->inbuf_datalen < sizeof(socks4_t)) /* basic info available? */
  35. return 0; /* not yet */
  36. if(connection_fetch_from_buf((char *)&socks4_info,sizeof(socks4_t),conn) < 0)
  37. return -1;
  38. log(LOG_DEBUG,"ap_handshake_process_socks(): Successfully read socks info.");
  39. if(socks4_info.version != 4) {
  40. log(LOG_NOTICE,"ap_handshake_process_socks(): Unrecognized version %d.",socks4_info.version);
  41. ap_handshake_socks_reply(conn, SOCKS4_REQUEST_REJECT);
  42. return -1;
  43. }
  44. conn->socks_version = socks4_info.version;
  45. if(socks4_info.command != 1) { /* not a connect? we don't support it. */
  46. log(LOG_NOTICE,"ap_handshake_process_socks(): command %d not '1'.",socks4_info.command);
  47. ap_handshake_socks_reply(conn, SOCKS4_REQUEST_REJECT);
  48. return -1;
  49. }
  50. conn->dest_port = ntohs(*(uint16_t*)&socks4_info.destport);
  51. if(!conn->dest_port) {
  52. log(LOG_NOTICE,"ap_handshake_process_socks(): Port is zero.");
  53. ap_handshake_socks_reply(conn, SOCKS4_REQUEST_REJECT);
  54. return -1;
  55. }
  56. log(LOG_NOTICE,"ap_handshake_process_socks(): Dest port is %d.",conn->dest_port);
  57. if(socks4_info.destip[0] ||
  58. socks4_info.destip[1] ||
  59. socks4_info.destip[2] ||
  60. !socks4_info.destip[3]) { /* not 0.0.0.x */
  61. log(LOG_NOTICE,"ap_handshake_process_socks(): destip not in form 0.0.0.x.");
  62. sprintf(conn->dest_tmp, "%d.%d.%d.%d", socks4_info.destip[0],
  63. socks4_info.destip[1], socks4_info.destip[2], socks4_info.destip[3]);
  64. conn->dest_addr = strdup(conn->dest_tmp);
  65. log(LOG_DEBUG,"ap_handshake_process_socks(): Successfully read destip (%s)", conn->dest_addr);
  66. }
  67. }
  68. if(!conn->read_username) { /* the socks spec says we've got to read stuff until we get a null */
  69. for(;;) {
  70. if(!conn->inbuf_datalen)
  71. return 0; /* maybe next time */
  72. if(connection_fetch_from_buf((char *)&c,1,conn) < 0)
  73. return -1;
  74. if(!c) {
  75. conn->read_username = 1;
  76. log(LOG_DEBUG,"ap_handshake_process_socks(): Successfully read username.");
  77. break;
  78. }
  79. }
  80. }
  81. if(!conn->dest_addr) { /* no dest_addr found yet */
  82. for(;;) {
  83. if(!conn->inbuf_datalen)
  84. return 0; /* maybe next time */
  85. if(connection_fetch_from_buf((char *)&c,1,conn) < 0)
  86. return -1;
  87. conn->dest_tmp[conn->dest_tmplen++] = c;
  88. if(conn->dest_tmplen > 500) {
  89. log(LOG_NOTICE,"ap_handshake_process_socks(): dest_addr too long!");
  90. ap_handshake_socks_reply(conn, SOCKS4_REQUEST_REJECT);
  91. return -1;
  92. }
  93. if(!c) { /* we found the null; we're done */
  94. conn->dest_addr = strdup(conn->dest_tmp);
  95. log(LOG_NOTICE,"ap_handshake_process_socks(): successfully read dest addr '%s'",
  96. conn->dest_addr);
  97. break;
  98. }
  99. }
  100. }
  101. /* find the circuit that we should use, if there is one. */
  102. circ = NULL; /* FIXME don't reuse circs, at least for me. */
  103. /* now we're all ready to make an onion or send a begin */
  104. if(circ) {
  105. if(circ->state == CIRCUIT_STATE_OPEN) {
  106. if(ap_handshake_send_begin(conn, circ) < 0) {
  107. circuit_close(circ);
  108. return -1;
  109. }
  110. }
  111. } else {
  112. if(ap_handshake_create_onion(conn) < 0) {
  113. circuit_close(circ);
  114. return -1;
  115. }
  116. }
  117. return 0;
  118. }
  119. int ap_handshake_create_onion(connection_t *conn) {
  120. int i;
  121. int routelen = 0; /* length of the route */
  122. unsigned int *route = NULL; /* hops in the route as an array of indexes into rarray */
  123. unsigned char *onion = NULL; /* holds the onion */
  124. int onionlen = 0; /* onion length in host order */
  125. crypt_path_t **cpath = NULL; /* defines the crypt operations that need to be performed on incoming/outgoing data */
  126. assert(conn);
  127. /* choose a route */
  128. route = (unsigned int *)router_new_route(&routelen);
  129. if (!route) {
  130. log(LOG_ERR,"ap_handshake_create_onion(): Error choosing a route through the OR network.");
  131. return -1;
  132. }
  133. log(LOG_DEBUG,"ap_handshake_create_onion(): Chosen a route of length %u : ",routelen);
  134. #if 0
  135. for (i=routelen-1;i>=0;i--)
  136. {
  137. log(LOG_DEBUG,"ap_handshake_process_ss() : %u : %s:%u, %u",routelen-i,(routerarray[route[i]])->address,ntohs((routerarray[route[i]])->port),RSA_size((routerarray[route[i]])->pkey));
  138. }
  139. #endif
  140. /* allocate memory for the crypt path */
  141. cpath = malloc(routelen * sizeof(crypt_path_t *));
  142. if (!cpath) {
  143. log(LOG_ERR,"ap_handshake_create_onion(): Error allocating memory for cpath.");
  144. free(route);
  145. return -1;
  146. }
  147. /* create an onion and calculate crypto keys */
  148. onion = router_create_onion(route,routelen,&onionlen,cpath);
  149. if (!onion) {
  150. log(LOG_ERR,"ap_handshake_create_onion(): Error creating an onion.");
  151. free(route);
  152. free(cpath); /* it's got nothing in it, since !onion */
  153. return -1;
  154. }
  155. log(LOG_DEBUG,"ap_handshake_create_onion(): Created an onion of size %u bytes.",onionlen);
  156. log(LOG_DEBUG,"ap_handshake_create_onion(): Crypt path :");
  157. for (i=0;i<routelen;i++) {
  158. log(LOG_DEBUG,"ap_handshake_create_onion() : %u/%u",(cpath[i])->forwf, (cpath[i])->backf);
  159. }
  160. return ap_handshake_establish_circuit(conn, route, routelen, onion, onionlen, cpath);
  161. }
  162. int ap_handshake_establish_circuit(connection_t *conn, unsigned int *route, int routelen, char *onion,
  163. int onionlen, crypt_path_t **cpath) {
  164. routerinfo_t *firsthop;
  165. connection_t *n_conn;
  166. circuit_t *circ;
  167. /* now see if we're already connected to the first OR in 'route' */
  168. firsthop = router_get_first_in_route(route, routelen);
  169. assert(firsthop); /* should always be defined */
  170. free(route); /* we don't need it anymore */
  171. circ = circuit_new(0, conn); /* sets circ->p_aci and circ->p_conn */
  172. circ->state = CIRCUIT_STATE_OR_WAIT;
  173. circ->onion = onion;
  174. circ->onionlen = onionlen;
  175. circ->cpath = cpath;
  176. circ->cpathlen = routelen;
  177. log(LOG_DEBUG,"ap_handshake_establish_circuit(): Looking for firsthop '%s:%u'",
  178. firsthop->address,firsthop->or_port);
  179. n_conn = connection_twin_get_by_addr_port(firsthop->addr,firsthop->or_port);
  180. if(!n_conn || n_conn->state != OR_CONN_STATE_OPEN) { /* not currently connected */
  181. circ->n_addr = firsthop->addr;
  182. circ->n_port = firsthop->or_port;
  183. if(global_role & ROLE_OR_CONNECT_ALL) { /* we would be connected if he were up. but he's not. */
  184. log(LOG_DEBUG,"ap_handshake_establish_circuit(): Route's firsthop isn't connected.");
  185. circuit_close(circ);
  186. return -1;
  187. }
  188. conn->state = AP_CONN_STATE_OR_WAIT;
  189. connection_stop_reading(conn); /* Stop listening for input from the AP! */
  190. if(!n_conn) { /* launch the connection */
  191. n_conn = connection_or_connect_as_op(firsthop);
  192. if(!n_conn) { /* connect failed, forget the whole thing */
  193. log(LOG_DEBUG,"ap_handshake_establish_circuit(): connect to firsthop failed. Closing.");
  194. circuit_close(circ);
  195. return -1;
  196. }
  197. }
  198. return 0; /* return success. The onion/circuit/etc will be taken care of automatically
  199. * (may already have been) whenever n_conn reaches OR_CONN_STATE_OPEN.
  200. */
  201. } else { /* it (or a twin) is already open. use it. */
  202. circ->n_addr = n_conn->addr;
  203. circ->n_port = n_conn->port;
  204. return ap_handshake_send_onion(conn, n_conn, circ);
  205. }
  206. }
  207. /* find circuits that are waiting on me, if any, and get them to send the onion */
  208. void ap_handshake_n_conn_open(connection_t *or_conn) {
  209. circuit_t *circ;
  210. connection_t *p_conn;
  211. log(LOG_DEBUG,"ap_handshake_n_conn_open(): Starting.");
  212. circ = circuit_enumerate_by_naddr_nport(NULL, or_conn->addr, or_conn->port);
  213. for(;;) {
  214. if(!circ)
  215. return;
  216. p_conn = circ->p_conn;
  217. if(p_conn->state != AP_CONN_STATE_OR_WAIT) {
  218. log(LOG_WARNING,"Bug: ap_handshake_n_conn_open() got an ap_conn not in OR_WAIT state.");
  219. }
  220. connection_start_reading(p_conn); /* resume listening for reads */
  221. log(LOG_DEBUG,"ap_handshake_n_conn_open(): Found circ, sending onion.");
  222. if(ap_handshake_send_onion(p_conn, or_conn, circ) < 0) {
  223. log(LOG_DEBUG,"ap_handshake_n_conn_open(): circuit marked for closing.");
  224. circuit_close(circ);
  225. return; /* FIXME will want to try the other circuits too? */
  226. }
  227. for(p_conn = p_conn->next_topic; p_conn; p_conn = p_conn->next_topic) { /* start up any other pending topics */
  228. if(ap_handshake_send_begin(p_conn, circ) < 0) {
  229. circuit_close(circ);
  230. return;
  231. }
  232. }
  233. circ = circuit_enumerate_by_naddr_nport(circ, or_conn->addr, or_conn->port);
  234. }
  235. }
  236. int ap_handshake_send_onion(connection_t *ap_conn, connection_t *n_conn, circuit_t *circ) {
  237. cell_t cell;
  238. int tmpbuflen, dataleft;
  239. char *tmpbuf;
  240. circ->n_aci = get_unique_aci_by_addr_port(circ->n_addr, circ->n_port, ACI_TYPE_BOTH);
  241. circ->n_conn = n_conn;
  242. log(LOG_DEBUG,"ap_handshake_send_onion(): n_conn is %s:%u",n_conn->address,n_conn->port);
  243. /* deliver the onion as one or more create cells */
  244. cell.command = CELL_CREATE;
  245. cell.aci = circ->n_aci;
  246. tmpbuflen = circ->onionlen+4;
  247. tmpbuf = malloc(tmpbuflen);
  248. if(!tmpbuf)
  249. return -1;
  250. *(uint32_t*)tmpbuf = htonl(circ->onionlen);
  251. memcpy(tmpbuf+4, circ->onion, circ->onionlen);
  252. dataleft = tmpbuflen;
  253. while(dataleft) {
  254. cell.command = CELL_CREATE;
  255. cell.aci = circ->n_aci;
  256. log(LOG_DEBUG,"ap_handshake_send_onion(): Sending a create cell for the onion...");
  257. if(dataleft >= CELL_PAYLOAD_SIZE) {
  258. cell.length = CELL_PAYLOAD_SIZE;
  259. memcpy(cell.payload, tmpbuf + tmpbuflen - dataleft, CELL_PAYLOAD_SIZE);
  260. connection_write_cell_to_buf(&cell, n_conn); /* clobbers cell */
  261. dataleft -= CELL_PAYLOAD_SIZE;
  262. } else { /* last cell */
  263. cell.length = dataleft;
  264. memcpy(cell.payload, tmpbuf + tmpbuflen - dataleft, dataleft);
  265. connection_write_cell_to_buf(&cell, n_conn); /* clobbers cell */
  266. dataleft = 0;
  267. }
  268. }
  269. free(tmpbuf);
  270. if(ap_handshake_send_begin(ap_conn, circ) < 0) {
  271. return -1;
  272. }
  273. circ->state = CIRCUIT_STATE_OPEN;
  274. /* FIXME should set circ->expire to something here */
  275. return 0;
  276. }
  277. int ap_handshake_send_begin(connection_t *ap_conn, circuit_t *circ) {
  278. cell_t cell;
  279. memset(&cell, 0, sizeof(cell_t));
  280. /* deliver the dest_addr in a data cell */
  281. cell.command = CELL_DATA;
  282. cell.aci = circ->n_aci;
  283. crypto_pseudo_rand(3, cell.payload+1); /* byte 0 is blank, bytes 1-3 are random */
  284. /* FIXME check for collisions */
  285. cell.payload[0] = 0;
  286. ap_conn->topic_id = *(uint32_t *)cell.payload;
  287. cell.payload[0] = TOPIC_COMMAND_BEGIN;
  288. snprintf(cell.payload+4, CELL_PAYLOAD_SIZE-4, "%s,%d", ap_conn->dest_addr, ap_conn->dest_port);
  289. cell.length = strlen(cell.payload+TOPIC_HEADER_SIZE)+1+TOPIC_HEADER_SIZE;
  290. log(LOG_DEBUG,"ap_handshake_send_begin(): Sending data cell to begin topic %d.", ap_conn->topic_id);
  291. if(circuit_deliver_data_cell_from_edge(&cell, circ, EDGE_AP) < 0) {
  292. log(LOG_DEBUG,"ap_handshake_send_begin(): failed to deliver begin cell. Closing.");
  293. return -1;
  294. }
  295. ap_conn->n_receive_topicwindow = TOPICWINDOW_START;
  296. ap_conn->p_receive_topicwindow = TOPICWINDOW_START;
  297. ap_conn->state = AP_CONN_STATE_OPEN;
  298. log(LOG_INFO,"ap_handshake_send_begin(): Address/port sent, ap socket %d, n_aci %d",ap_conn->s,circ->n_aci);
  299. return 0;
  300. }
  301. int ap_handshake_socks_reply(connection_t *conn, char result) {
  302. socks4_t socks4_info;
  303. assert(conn);
  304. socks4_info.version = 0;
  305. socks4_info.command = result;
  306. socks4_info.destport[0] = socks4_info.destport[1] = 0;
  307. socks4_info.destip[0] = socks4_info.destip[1] = socks4_info.destip[2] = socks4_info.destip[3] = 0;
  308. if(connection_write_to_buf((char *)&socks4_info, sizeof(socks4_t), conn) < 0)
  309. return -1;
  310. return connection_flush_buf(conn); /* try to flush it, in case we're about to close the conn */
  311. }
  312. int connection_ap_process_data_cell(cell_t *cell, circuit_t *circ) {
  313. connection_t *conn;
  314. int topic_command;
  315. int topic_id;
  316. /* an incoming data cell has arrived */
  317. assert(cell && circ);
  318. topic_command = *cell->payload;
  319. *cell->payload = 0;
  320. topic_id = *(uint32_t *)cell->payload;
  321. log(LOG_DEBUG,"connection_ap_process_data_cell(): command %d topic %d", topic_command, topic_id);
  322. circuit_consider_sending_sendme(circ, EDGE_AP);
  323. for(conn = circ->p_conn; conn && conn->topic_id != topic_id; conn = conn->next_topic) ;
  324. /* now conn is either NULL, in which case we don't recognize the topic_id, or
  325. * it is set, in which case cell is talking about this conn.
  326. */
  327. if(conn && conn->state != AP_CONN_STATE_OPEN) {
  328. /* we should not have gotten this cell */
  329. log(LOG_DEBUG,"connection_ap_process_data_cell(): Got a data cell when not in 'open' state. Dropping.");
  330. return 0;
  331. }
  332. switch(topic_command) {
  333. case TOPIC_COMMAND_BEGIN:
  334. log(LOG_INFO,"connection_ap_process_data_cell(): topic begin request unsupported. Dropping.");
  335. break;
  336. case TOPIC_COMMAND_DATA:
  337. if(!conn) {
  338. log(LOG_DEBUG,"connection_ap_process_data_cell(): data cell dropped, unknown topic %d.",topic_id);
  339. return 0;
  340. }
  341. if(--conn->n_receive_topicwindow < 0) { /* is it below 0 after decrement? */
  342. log(LOG_DEBUG,"connection_ap_process_data_cell(): receive_topicwindow at exit below 0. Killing.");
  343. return -1; /* exit node breaking protocol. kill the whole circuit. */
  344. }
  345. log(LOG_DEBUG,"connection_ap_process_data_cell(): willing to receive %d more cells from circ",conn->n_receive_topicwindow);
  346. if(connection_write_to_buf(cell->payload + TOPIC_HEADER_SIZE,
  347. cell->length - TOPIC_HEADER_SIZE, conn) < 0) {
  348. conn->marked_for_close = 1;
  349. return 0;
  350. }
  351. if(connection_consider_sending_sendme(conn, EDGE_AP) < 0)
  352. conn->marked_for_close = 1;
  353. return 0;
  354. case TOPIC_COMMAND_END:
  355. if(!conn) {
  356. log(LOG_DEBUG,"connection_ap_process_data_cell(): end cell dropped, unknown topic %d.",topic_id);
  357. return 0;
  358. }
  359. log(LOG_DEBUG,"connection_ap_process_data_cell(): end cell for topic %d. Removing topic.",topic_id);
  360. /* go through and identify who points to conn. remove conn from the list. */
  361. #if 0
  362. if(conn == circ->p_conn) {
  363. circ->p_conn = conn->next_topic;
  364. }
  365. for(prevconn = circ->p_conn; prevconn->next_topic != conn; prevconn = prevconn->next_topic) ;
  366. prevconn->next_topic = conn->next_topic;
  367. #endif
  368. conn->marked_for_close = 1;
  369. break;
  370. case TOPIC_COMMAND_CONNECTED:
  371. if(!conn) {
  372. log(LOG_DEBUG,"connection_ap_process_data_cell(): connected cell dropped, unknown topic %d.",topic_id);
  373. break;
  374. }
  375. log(LOG_DEBUG,"connection_ap_process_data_cell(): Connected! Notifying application.");
  376. if(ap_handshake_socks_reply(conn, SOCKS4_REQUEST_GRANTED) < 0) {
  377. conn->marked_for_close = 1;
  378. }
  379. break;
  380. case TOPIC_COMMAND_SENDME:
  381. if(!conn) {
  382. log(LOG_DEBUG,"connection_exit_process_data_cell(): sendme cell dropped, unknown topic %d.",topic_id);
  383. return 0;
  384. }
  385. conn->p_receive_topicwindow += TOPICWINDOW_INCREMENT;
  386. connection_start_reading(conn);
  387. connection_package_raw_inbuf(conn); /* handle whatever might still be on the inbuf */
  388. circuit_consider_stop_edge_reading(circ, EDGE_AP);
  389. break;
  390. default:
  391. log(LOG_DEBUG,"connection_ap_process_data_cell(): unknown topic command %d.",topic_command);
  392. }
  393. return 0;
  394. }
  395. int connection_ap_finished_flushing(connection_t *conn) {
  396. assert(conn && conn->type == CONN_TYPE_AP);
  397. switch(conn->state) {
  398. case AP_CONN_STATE_OPEN:
  399. /* FIXME down the road, we'll clear out circuits that are pending to close */
  400. connection_stop_writing(conn);
  401. return connection_consider_sending_sendme(conn, EDGE_AP);
  402. default:
  403. log(LOG_DEBUG,"Bug: connection_ap_finished_flushing() called in unexpected state.");
  404. return 0;
  405. }
  406. return 0;
  407. }
  408. int connection_ap_create_listener(struct sockaddr_in *bindaddr) {
  409. log(LOG_DEBUG,"connection_create_ap_listener starting");
  410. return connection_create_listener(bindaddr, CONN_TYPE_AP_LISTENER);
  411. }
  412. int connection_ap_handle_listener_read(connection_t *conn) {
  413. log(LOG_NOTICE,"AP: Received a connection request. Waiting for socksinfo.");
  414. return connection_handle_listener_read(conn, CONN_TYPE_AP, AP_CONN_STATE_SOCKS_WAIT);
  415. }