command.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  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, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /* $Id$ */
  7. const char command_c_id[] =
  8. "$Id$";
  9. /**
  10. * \file command.c
  11. * \brief Functions for processing incoming cells.
  12. **/
  13. /* In-points to command.c:
  14. *
  15. * - command_process_cell(), called from
  16. * connection_or_process_cells_from_inbuf() in connection_or.c
  17. */
  18. #include "or.h"
  19. /** Keep statistics about how many of each type of cell we've received. */
  20. uint64_t stats_n_padding_cells_processed = 0;
  21. uint64_t stats_n_create_cells_processed = 0;
  22. uint64_t stats_n_created_cells_processed = 0;
  23. uint64_t stats_n_relay_cells_processed = 0;
  24. uint64_t stats_n_destroy_cells_processed = 0;
  25. uint64_t stats_n_versions_cells_processed = 0;
  26. uint64_t stats_n_netinfo_cells_processed = 0;
  27. uint64_t stats_n_cert_cells_processed = 0;
  28. uint64_t stats_n_link_auth_cells_processed = 0;
  29. /* These are the main functions for processing cells */
  30. static void command_process_create_cell(cell_t *cell, or_connection_t *conn);
  31. static void command_process_created_cell(cell_t *cell, or_connection_t *conn);
  32. static void command_process_relay_cell(cell_t *cell, or_connection_t *conn);
  33. static void command_process_destroy_cell(cell_t *cell, or_connection_t *conn);
  34. static void command_process_versions_cell(var_cell_t *cell,
  35. or_connection_t *conn);
  36. static void command_process_netinfo_cell(cell_t *cell, or_connection_t *conn);
  37. #if 0
  38. static void command_process_cert_cell(var_cell_t *cell, or_connection_t *conn);
  39. static void command_process_link_auth_cell(cell_t *cell,or_connection_t *conn);
  40. #endif
  41. #ifdef KEEP_TIMING_STATS
  42. /** This is a wrapper function around the actual function that processes the
  43. * <b>cell</b> that just arrived on <b>conn</b>. Increment <b>*time</b>
  44. * by the number of microseconds used by the call to <b>*func(cell, conn)</b>.
  45. */
  46. static void
  47. command_time_process_cell(cell_t *cell, or_connection_t *conn, int *time,
  48. void (*func)(cell_t *, or_connection_t *))
  49. {
  50. struct timeval start, end;
  51. long time_passed;
  52. tor_gettimeofday(&start);
  53. (*func)(cell, conn);
  54. tor_gettimeofday(&end);
  55. time_passed = tv_udiff(&start, &end) ;
  56. if (time_passed > 10000) { /* more than 10ms */
  57. log_debug(LD_OR,"That call just took %ld ms.",time_passed/1000);
  58. }
  59. if (time_passed < 0) {
  60. log_info(LD_GENERAL,"That call took us back in time!");
  61. time_passed = 0;
  62. }
  63. *time += time_passed;
  64. }
  65. #endif
  66. /** Process a <b>cell</b> that was just received on <b>conn</b>. Keep internal
  67. * statistics about how many of each cell we've processed so far
  68. * this second, and the total number of microseconds it took to
  69. * process each type of cell.
  70. */
  71. void
  72. command_process_cell(cell_t *cell, or_connection_t *conn)
  73. {
  74. #ifdef KEEP_TIMING_STATS
  75. /* how many of each cell have we seen so far this second? needs better
  76. * name. */
  77. static int num_create=0, num_created=0, num_relay=0, num_destroy=0;
  78. /* how long has it taken to process each type of cell? */
  79. static int create_time=0, created_time=0, relay_time=0, destroy_time=0;
  80. static time_t current_second = 0; /* from previous calls to time */
  81. time_t now = time(NULL);
  82. if (now > current_second) { /* the second has rolled over */
  83. /* print stats */
  84. log_info(LD_OR,
  85. "At end of second: %d creates (%d ms), %d createds (%d ms), "
  86. "%d relays (%d ms), %d destroys (%d ms)",
  87. num_create, create_time/1000,
  88. num_created, created_time/1000,
  89. num_relay, relay_time/1000,
  90. num_destroy, destroy_time/1000);
  91. /* zero out stats */
  92. num_create = num_created = num_relay = num_destroy = 0;
  93. create_time = created_time = relay_time = destroy_time = 0;
  94. /* remember which second it is, for next time */
  95. current_second = now;
  96. }
  97. #endif
  98. #ifdef KEEP_TIMING_STATS
  99. #define PROCESS_CELL(tp, cl, cn) STMT_BEGIN { \
  100. ++num ## tp; \
  101. command_time_process_cell(cl, cn, & tp ## time , \
  102. command_process_ ## tp ## _cell); \
  103. } STMT_END
  104. #else
  105. #define PROCESS_CELL(tp, cl, cn) command_process_ ## tp ## _cell(cl, cn)
  106. #endif
  107. /*XXXX020 reject all but VERSIONS, NETINFO, CERT, LINK_AUTH when
  108. * handshaking. */
  109. switch (cell->command) {
  110. case CELL_PADDING:
  111. ++stats_n_padding_cells_processed;
  112. /* do nothing */
  113. break;
  114. case CELL_CREATE:
  115. case CELL_CREATE_FAST:
  116. ++stats_n_create_cells_processed;
  117. PROCESS_CELL(create, cell, conn);
  118. break;
  119. case CELL_CREATED:
  120. case CELL_CREATED_FAST:
  121. ++stats_n_created_cells_processed;
  122. PROCESS_CELL(created, cell, conn);
  123. break;
  124. case CELL_RELAY:
  125. case CELL_RELAY_EARLY:
  126. ++stats_n_relay_cells_processed;
  127. PROCESS_CELL(relay, cell, conn);
  128. break;
  129. case CELL_DESTROY:
  130. ++stats_n_destroy_cells_processed;
  131. PROCESS_CELL(destroy, cell, conn);
  132. break;
  133. case CELL_VERSIONS:
  134. tor_fragile_assert();
  135. break;
  136. case CELL_NETINFO:
  137. ++stats_n_netinfo_cells_processed;
  138. PROCESS_CELL(netinfo, cell, conn);
  139. break;
  140. default:
  141. log_fn(LOG_INFO, LD_PROTOCOL,
  142. "Cell of unknown type (%d) received. Dropping.", cell->command);
  143. break;
  144. }
  145. }
  146. /** Process a <b>cell</b> that was just received on <b>conn</b>. Keep internal
  147. * statistics about how many of each cell we've processed so far
  148. * this second, and the total number of microseconds it took to
  149. * process each type of cell.
  150. */
  151. void
  152. command_process_var_cell(var_cell_t *cell, or_connection_t *conn)
  153. {
  154. #ifdef KEEP_TIMING_STATS
  155. /* how many of each cell have we seen so far this second? needs better
  156. * name. */
  157. static int num_versions=0, num_cert=0;
  158. time_t now = time(NULL);
  159. if (now > current_second) { /* the second has rolled over */
  160. /* print stats */
  161. log_info(LD_OR,
  162. "At end of second: %d versions (%d ms), %d cert (%d ms)",
  163. num_versions, versions_time/1000,
  164. cert, cert_time/1000);
  165. num_versions = num_cert = 0;
  166. versions_time = cert_time = 0;
  167. /* remember which second it is, for next time */
  168. current_second = now;
  169. }
  170. #endif
  171. /*XXXX020 reject all when not handshaking. */
  172. switch (cell->command) {
  173. case CELL_VERSIONS:
  174. ++stats_n_versions_cells_processed;
  175. PROCESS_CELL(versions, cell, conn);
  176. break;
  177. default:
  178. log_warn(LD_BUG,
  179. "Variable-length cell of unknown type (%d) received.",
  180. cell->command);
  181. tor_fragile_assert();
  182. break;
  183. }
  184. }
  185. /** Process a 'create' <b>cell</b> that just arrived from <b>conn</b>. Make a
  186. * new circuit with the p_circ_id specified in cell. Put the circuit in state
  187. * onionskin_pending, and pass the onionskin to the cpuworker. Circ will get
  188. * picked up again when the cpuworker finishes decrypting it.
  189. */
  190. static void
  191. command_process_create_cell(cell_t *cell, or_connection_t *conn)
  192. {
  193. or_circuit_t *circ;
  194. int id_is_high;
  195. if (we_are_hibernating()) {
  196. log_info(LD_OR,
  197. "Received create cell but we're shutting down. Sending back "
  198. "destroy.");
  199. connection_or_send_destroy(cell->circ_id, conn,
  200. END_CIRC_REASON_HIBERNATING);
  201. return;
  202. }
  203. if (!server_mode(get_options())) {
  204. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  205. "Received create cell (type %d) from %s:%d, but we're a client. "
  206. "Sending back a destroy.",
  207. (int)cell->command, conn->_base.address, conn->_base.port);
  208. connection_or_send_destroy(cell->circ_id, conn,
  209. END_CIRC_REASON_TORPROTOCOL);
  210. return;
  211. }
  212. /* If the high bit of the circuit ID is not as expected, close the
  213. * circ. */
  214. id_is_high = cell->circ_id & (1<<15);
  215. if ((id_is_high && conn->circ_id_type == CIRC_ID_TYPE_HIGHER) ||
  216. (!id_is_high && conn->circ_id_type == CIRC_ID_TYPE_LOWER)) {
  217. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  218. "Received create cell with unexpected circ_id %d. Closing.",
  219. cell->circ_id);
  220. connection_or_send_destroy(cell->circ_id, conn,
  221. END_CIRC_REASON_TORPROTOCOL);
  222. return;
  223. }
  224. if (circuit_get_by_circid_orconn(cell->circ_id, conn)) {
  225. routerinfo_t *router = router_get_by_digest(conn->identity_digest);
  226. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  227. "Received CREATE cell (circID %d) for known circ. "
  228. "Dropping (age %d).",
  229. cell->circ_id, (int)(time(NULL) - conn->_base.timestamp_created));
  230. if (router)
  231. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  232. "Details: nickname \"%s\", platform %s.",
  233. router->nickname, escaped(router->platform));
  234. return;
  235. }
  236. circ = or_circuit_new(cell->circ_id, conn);
  237. circ->_base.purpose = CIRCUIT_PURPOSE_OR;
  238. circuit_set_state(TO_CIRCUIT(circ), CIRCUIT_STATE_ONIONSKIN_PENDING);
  239. if (cell->command == CELL_CREATE) {
  240. circ->_base.onionskin = tor_malloc(ONIONSKIN_CHALLENGE_LEN);
  241. memcpy(circ->_base.onionskin, cell->payload, ONIONSKIN_CHALLENGE_LEN);
  242. /* hand it off to the cpuworkers, and then return. */
  243. if (assign_to_cpuworker(NULL, CPUWORKER_TASK_ONION, circ) < 0) {
  244. log_warn(LD_GENERAL,"Failed to hand off onionskin. Closing.");
  245. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  246. return;
  247. }
  248. log_debug(LD_OR,"success: handed off onionskin.");
  249. } else {
  250. /* This is a CREATE_FAST cell; we can handle it immediately without using
  251. * a CPU worker. */
  252. char keys[CPATH_KEY_MATERIAL_LEN];
  253. char reply[DIGEST_LEN*2];
  254. tor_assert(cell->command == CELL_CREATE_FAST);
  255. if (fast_server_handshake(cell->payload, reply, keys, sizeof(keys))<0) {
  256. log_warn(LD_OR,"Failed to generate key material. Closing.");
  257. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  258. return;
  259. }
  260. if (onionskin_answer(circ, CELL_CREATED_FAST, reply, keys)<0) {
  261. log_warn(LD_OR,"Failed to reply to CREATE_FAST cell. Closing.");
  262. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  263. return;
  264. }
  265. }
  266. }
  267. /** Process a 'created' <b>cell</b> that just arrived from <b>conn</b>.
  268. * Find the circuit
  269. * that it's intended for. If we're not the origin of the circuit, package
  270. * the 'created' cell in an 'extended' relay cell and pass it back. If we
  271. * are the origin of the circuit, send it to circuit_finish_handshake() to
  272. * finish processing keys, and then call circuit_send_next_onion_skin() to
  273. * extend to the next hop in the circuit if necessary.
  274. */
  275. static void
  276. command_process_created_cell(cell_t *cell, or_connection_t *conn)
  277. {
  278. circuit_t *circ;
  279. circ = circuit_get_by_circid_orconn(cell->circ_id, conn);
  280. if (!circ) {
  281. log_info(LD_OR,
  282. "(circID %d) unknown circ (probably got a destroy earlier). "
  283. "Dropping.", cell->circ_id);
  284. return;
  285. }
  286. if (circ->n_circ_id != cell->circ_id) {
  287. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,
  288. "got created cell from Tor client? Closing.");
  289. circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
  290. return;
  291. }
  292. if (CIRCUIT_IS_ORIGIN(circ)) { /* we're the OP. Handshake this. */
  293. origin_circuit_t *origin_circ = TO_ORIGIN_CIRCUIT(circ);
  294. int err_reason = 0;
  295. log_debug(LD_OR,"at OP. Finishing handshake.");
  296. if ((err_reason = circuit_finish_handshake(origin_circ, cell->command,
  297. cell->payload)) < 0) {
  298. log_warn(LD_OR,"circuit_finish_handshake failed.");
  299. circuit_mark_for_close(circ, -err_reason);
  300. return;
  301. }
  302. log_debug(LD_OR,"Moving to next skin.");
  303. if ((err_reason = circuit_send_next_onion_skin(origin_circ)) < 0) {
  304. log_info(LD_OR,"circuit_send_next_onion_skin failed.");
  305. /* XXX push this circuit_close lower */
  306. circuit_mark_for_close(circ, -err_reason);
  307. return;
  308. }
  309. } else { /* pack it into an extended relay cell, and send it. */
  310. log_debug(LD_OR,
  311. "Converting created cell to extended relay cell, sending.");
  312. relay_send_command_from_edge(0, circ, RELAY_COMMAND_EXTENDED,
  313. cell->payload, ONIONSKIN_REPLY_LEN,
  314. NULL);
  315. }
  316. }
  317. /** Process a 'relay' <b>cell</b> that just arrived from <b>conn</b>. Make sure
  318. * it came in with a recognized circ_id. Pass it on to
  319. * circuit_receive_relay_cell() for actual processing.
  320. */
  321. static void
  322. command_process_relay_cell(cell_t *cell, or_connection_t *conn)
  323. {
  324. circuit_t *circ;
  325. int reason, direction;
  326. circ = circuit_get_by_circid_orconn(cell->circ_id, conn);
  327. if (!circ) {
  328. log_debug(LD_OR,
  329. "unknown circuit %d on connection from %s:%d. Dropping.",
  330. cell->circ_id, conn->_base.address, conn->_base.port);
  331. return;
  332. }
  333. if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  334. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,"circuit in create_wait. Closing.");
  335. circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
  336. return;
  337. }
  338. if (CIRCUIT_IS_ORIGIN(circ)) {
  339. /* if we're a relay and treating connections with recent local
  340. * traffic better, then this is one of them. */
  341. conn->client_used = time(NULL);
  342. }
  343. if (!CIRCUIT_IS_ORIGIN(circ) &&
  344. cell->circ_id == TO_OR_CIRCUIT(circ)->p_circ_id)
  345. direction = CELL_DIRECTION_OUT;
  346. else
  347. direction = CELL_DIRECTION_IN;
  348. if ((reason = circuit_receive_relay_cell(cell, circ, direction)) < 0) {
  349. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,"circuit_receive_relay_cell "
  350. "(%s) failed. Closing.",
  351. direction==CELL_DIRECTION_OUT?"forward":"backward");
  352. circuit_mark_for_close(circ, -reason);
  353. }
  354. }
  355. /** Process a 'destroy' <b>cell</b> that just arrived from
  356. * <b>conn</b>. Find the circ that it refers to (if any).
  357. *
  358. * If the circ is in state
  359. * onionskin_pending, then call onion_pending_remove() to remove it
  360. * from the pending onion list (note that if it's already being
  361. * processed by the cpuworker, it won't be in the list anymore; but
  362. * when the cpuworker returns it, the circuit will be gone, and the
  363. * cpuworker response will be dropped).
  364. *
  365. * Then mark the circuit for close (which marks all edges for close,
  366. * and passes the destroy cell onward if necessary).
  367. */
  368. static void
  369. command_process_destroy_cell(cell_t *cell, or_connection_t *conn)
  370. {
  371. circuit_t *circ;
  372. int reason;
  373. circ = circuit_get_by_circid_orconn(cell->circ_id, conn);
  374. reason = (uint8_t)cell->payload[0];
  375. if (!circ) {
  376. log_info(LD_OR,"unknown circuit %d on connection from %s:%d. Dropping.",
  377. cell->circ_id, conn->_base.address, conn->_base.port);
  378. return;
  379. }
  380. log_debug(LD_OR,"Received for circID %d.",cell->circ_id);
  381. if (!CIRCUIT_IS_ORIGIN(circ) &&
  382. cell->circ_id == TO_OR_CIRCUIT(circ)->p_circ_id) {
  383. /* the destroy came from behind */
  384. circuit_set_p_circid_orconn(TO_OR_CIRCUIT(circ), 0, NULL);
  385. circuit_mark_for_close(circ, reason|END_CIRC_REASON_FLAG_REMOTE);
  386. } else { /* the destroy came from ahead */
  387. circuit_set_n_circid_orconn(circ, 0, NULL);
  388. if (CIRCUIT_IS_ORIGIN(circ)) {
  389. circuit_mark_for_close(circ, reason|END_CIRC_REASON_FLAG_REMOTE);
  390. } else {
  391. char payload[1];
  392. log_debug(LD_OR, "Delivering 'truncated' back.");
  393. payload[0] = (char)reason;
  394. relay_send_command_from_edge(0, circ, RELAY_COMMAND_TRUNCATED,
  395. payload, sizeof(payload), NULL);
  396. }
  397. }
  398. }
  399. /** Process a 'versions' cell. The current link protocol version must be 0
  400. * to indicate that no version has yet been negotiated. DOCDOC say more. */
  401. static void
  402. command_process_versions_cell(var_cell_t *cell, or_connection_t *conn)
  403. {
  404. int highest_supported_version = 0;
  405. const char *cp, *end;
  406. if (conn->link_proto != 0 ||
  407. conn->_base.state != OR_CONN_STATE_OR_HANDSHAKING ||
  408. (conn->handshake_state && conn->handshake_state->received_versions)) {
  409. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  410. "Received a VERSIONS cell on a connection with its version "
  411. "already set to %d; dropping", (int) conn->link_proto);
  412. return;
  413. }
  414. tor_assert(conn->handshake_state);
  415. end = cell->payload + cell->payload_len;
  416. for (cp = cell->payload; cp+1 < end; ++cp) {
  417. uint16_t v = ntohs(get_uint16(cp));
  418. if (v == 1 || v == 2) {
  419. if (v > highest_supported_version)
  420. highest_supported_version = v;
  421. }
  422. }
  423. if (!highest_supported_version) {
  424. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  425. "Couldn't find a version in common between my version list and the "
  426. "list in the VERSIONS cell; closing connection.");
  427. connection_mark_for_close(TO_CONN(conn));
  428. return;
  429. }
  430. conn->link_proto = highest_supported_version;
  431. conn->handshake_state->received_versions = 1;
  432. #if 0
  433. /*XXXX020 not right; references dead functions */
  434. if (highest_supported_version >= 2) {
  435. if (connection_or_send_netinfo(conn) < 0 ||
  436. connection_or_send_cert(conn) < 0) {
  437. connection_mark_for_close(TO_CONN(conn));
  438. return;
  439. }
  440. if (conn->handshake_state->started_here)
  441. connection_or_send_link_auth(conn);
  442. } else {
  443. /* XXXX020 finish v1 verification. */
  444. }
  445. #endif
  446. }
  447. /** Process a 'netinfo' cell. DOCDOC say more. */
  448. static void
  449. command_process_netinfo_cell(cell_t *cell, or_connection_t *conn)
  450. {
  451. time_t timestamp;
  452. uint8_t my_addr_type;
  453. uint8_t my_addr_len;
  454. const char *my_addr_ptr;
  455. const char *cp, *end;
  456. uint8_t n_other_addrs;
  457. time_t now = time(NULL);
  458. if (conn->link_proto < 2) {
  459. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  460. "Received a NETINFO cell on %s connection; dropping.",
  461. conn->link_proto == 0 ? "non-versioned" : "a v1");
  462. return;
  463. }
  464. if (conn->_base.state != OR_CONN_STATE_OR_HANDSHAKING) {
  465. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  466. "Received a NETINFO cell on a non-handshaking; dropping.");
  467. return;
  468. }
  469. tor_assert(conn->handshake_state &&
  470. conn->handshake_state->received_versions);
  471. if (conn->handshake_state->received_netinfo) {
  472. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  473. "Received a duplicate NETINFO cell; dropping.");
  474. return;
  475. }
  476. /* Decode the cell. */
  477. timestamp = ntohl(get_uint32(cell->payload));
  478. if (abs(now - conn->handshake_state->sent_versions_at) < 180) {
  479. conn->handshake_state->apparent_skew = now - timestamp;
  480. }
  481. my_addr_type = (uint8_t) cell->payload[4];
  482. my_addr_len = (uint8_t) cell->payload[5];
  483. my_addr_ptr = cell->payload + 6;
  484. end = cell->payload + CELL_PAYLOAD_SIZE;
  485. cp = cell->payload + 6 + my_addr_len;
  486. if (cp >= end) {
  487. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  488. "Addresses too long in netinfo cell; closing connection.");
  489. connection_mark_for_close(TO_CONN(conn));
  490. return;
  491. } else if (my_addr_type == RESOLVED_TYPE_IPV4 && my_addr_len == 4) {
  492. conn->handshake_state->my_apparent_addr = ntohl(get_uint32(my_addr_ptr));
  493. }
  494. n_other_addrs = (uint8_t) *cp++;
  495. while (n_other_addrs && cp < end-2) {
  496. /* Consider all the other addresses; if any matches, this connection is
  497. * "canonical." */
  498. uint8_t other_addr_type = (uint8_t) *cp++;
  499. uint8_t other_addr_len = (uint8_t) *cp++;
  500. if (cp + other_addr_len >= end) {
  501. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  502. "Address too long in netinfo cell; closing connection.");
  503. connection_mark_for_close(TO_CONN(conn));
  504. return;
  505. }
  506. if (other_addr_type == RESOLVED_TYPE_IPV4 && other_addr_len == 4) {
  507. uint32_t addr = ntohl(get_uint32(cp));
  508. if (addr == conn->real_addr) {
  509. conn->handshake_state->apparently_canonical = 1;
  510. break;
  511. }
  512. }
  513. cp += other_addr_len;
  514. --n_other_addrs;
  515. }
  516. conn->handshake_state->received_netinfo = 1;
  517. }
  518. /*XXXX020 move to connection_or.c */
  519. /** DOCDOC Called when we're done authenticating; act on stuff we
  520. * learned in netinfo. */
  521. int
  522. connection_or_act_on_netinfo(or_connection_t *conn)
  523. {
  524. long delta;
  525. if (!conn->handshake_state)
  526. return -1;
  527. tor_assert(conn->handshake_state->authenticated != 0);
  528. delta = conn->handshake_state->apparent_skew;
  529. /*XXXX020 magic number 3600 */
  530. if (abs(delta) > 3600 &&
  531. router_get_by_digest(conn->identity_digest)) {
  532. char dbuf[64];
  533. /*XXXX020 not always warn!*/
  534. format_time_interval(dbuf, sizeof(dbuf), delta);
  535. log_fn(LOG_WARN, LD_HTTP, "Received NETINFO cell with skewed time from "
  536. "server at %s:%d. It seems that our clock is %s by %s, or "
  537. "that theirs is %s. Tor requires an accurate clock to work: "
  538. "please check your time and date settings.",
  539. conn->_base.address, (int)conn->_base.port,
  540. delta>0 ? "ahead" : "behind", dbuf,
  541. delta>0 ? "behind" : "ahead");
  542. control_event_general_status(LOG_WARN,
  543. "CLOCK_SKEW SKEW=%ld SOURCE=OR:%s:%d",
  544. delta, conn->_base.address, conn->_base.port);
  545. }
  546. /* XXX020 possibly, learn my address from my_apparent_addr */
  547. if (conn->handshake_state->apparently_canonical) {
  548. conn->is_canonical = 1;
  549. }
  550. return 0;
  551. }
  552. #if 0
  553. /*DOCDOC*/
  554. static void
  555. command_process_cert_cell(var_cell_t *cell, or_connection_t *conn)
  556. {
  557. int n_certs = 0;
  558. uint16_t conn_cert_len = 0, id_cert_len = 0;
  559. const char *conn_cert = NULL, *id_cert = NULL;
  560. const char *cp, *end;
  561. int done = 0;
  562. if (conn->_base.state != OR_CONN_STATE_OR_HANDSHAKING) {
  563. log_fn(LOG_PROTOCOL_WARN, LD_OR, "Got CERT cell when not handshaking. "
  564. "Ignoring.");
  565. return;
  566. }
  567. tor_assert(conn->handshake_state);
  568. if (!conn->handshake_state->received_versions ||
  569. !conn->handshake_state->received_netinfo) {
  570. log_fn(LOG_PROTOCOL_WARN, LD_OR, "Got CERT cell before VERSIONS and "
  571. "NETINFO. Closing the connection.");
  572. goto err;
  573. }
  574. if (conn->handshake_state->received_certs) {
  575. log_fn(LOG_PROTOCOL_WARN, LD_OR, "Got duplicate CERT cell. "
  576. "Closing the connection.");
  577. goto err;
  578. }
  579. cp = cell->payload;
  580. end = cell->payload + cell->payload_len;
  581. while (cp < end) {
  582. uint16_t len;
  583. if (end-cp == 1)
  584. goto err;
  585. len = ntohs(get_uint16(cp));
  586. cp += 2;
  587. if (end-cp < len)
  588. goto err;
  589. if (n_certs == 0) {
  590. id_cert = cp;
  591. id_cert_len = len;
  592. } else if (n_certs == 1) {
  593. conn_cert = id_cert;
  594. conn_cert_len = id_cert_len;
  595. id_cert = cp;
  596. id_cert_len = len;
  597. } else {
  598. goto err;
  599. }
  600. cp += len;
  601. ++n_certs;
  602. }
  603. /* Now we have 0, 1, or 2 certs. */
  604. if (n_certs == 0) {
  605. /* The other side is unauthenticated. */
  606. done = 1;
  607. } else {
  608. int r;
  609. r = tor_tls_verify_certs_v2(LOG_PROTOCOL_WARN, conn->tls,
  610. conn_cert, conn_cert_len,
  611. id_cert, id_cert_len,
  612. &conn->handshake_state->signing_key,
  613. (conn->handshake_state->started_here ?
  614. conn->handshake_state->server_cert_digest :
  615. conn->handshake_state->client_cert_digest),
  616. &conn->handshake_state->identity_key,
  617. conn->handshake_state->cert_id_digest);
  618. if (r < 0)
  619. goto err;
  620. if (r == 1) {
  621. done = 1;
  622. conn->handshake_state->authenticated = 1;
  623. }
  624. }
  625. conn->handshake_state->received_certs = 1;
  626. if (done) {
  627. if (connection_or_finish_or_handshake(conn) < 0)
  628. goto err;
  629. }
  630. if (! conn->handshake_state->signing_key)
  631. goto err;
  632. return;
  633. err:
  634. connection_mark_for_close(TO_CONN(conn));
  635. }
  636. #define LINK_AUTH_STRING "Tor initiator certificate verification"
  637. /** DOCDOC */
  638. static void
  639. command_process_link_auth_cell(cell_t *cell, or_connection_t *conn)
  640. {
  641. or_handshake_state_t *s;
  642. char hmac[DIGEST_LEN];
  643. uint16_t len;
  644. size_t sig_len;
  645. const char *sig;
  646. char *checked = NULL;
  647. int checked_len;
  648. tor_assert(conn);
  649. if (conn->_base.state != OR_CONN_STATE_OR_HANDSHAKING) {
  650. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  651. "Received a LINK_AUTH cell on connection in the wrong state; "
  652. "dropping.");
  653. return;
  654. }
  655. s = conn->handshake_state;
  656. tor_assert(s);
  657. if (s->started_here) {
  658. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  659. "Got a LINK_AUTH cell from a server; closing the connection.");
  660. goto err;
  661. }
  662. if (!s->received_netinfo || !s->received_versions || !s->received_certs) {
  663. log_fn(LOG_PROTOCOL_WARN, LD_OR, "Got a LINK_AUTH cell too early; "
  664. "closing the connection");
  665. goto err;
  666. }
  667. len = ntohs(get_uint16(cell->payload));
  668. if (len < 2 || len > CELL_PAYLOAD_SIZE - 2) {
  669. log_fn(LOG_PROTOCOL_WARN, LD_OR, "Bad length field (%d) on LINK_AUTH cell;"
  670. " closing the connection", (int)len);
  671. goto err;
  672. }
  673. if (cell->payload[2] != 0x00) {
  674. log_fn(LOG_PROTOCOL_WARN, LD_OR, "Unrecognized LINK_AUTH signature "
  675. "version; closing the connection");
  676. goto err;
  677. }
  678. connection_or_compute_link_auth_hmac(conn, hmac);
  679. tor_assert(s->signing_key);
  680. sig = cell->payload+3;
  681. sig_len = len-1;
  682. checked = tor_malloc(crypto_pk_keysize(s->signing_key));
  683. checked_len = crypto_pk_public_checksig(s->signing_key,checked,sig,sig_len);
  684. if (checked_len < 0) {
  685. log_fn(LOG_PROTOCOL_WARN, LD_OR, "Bad signature on LINK_AUTH cell; "
  686. "closing the connection");
  687. goto err;
  688. }
  689. if (checked_len != DIGEST_LEN) {
  690. log_fn(LOG_PROTOCOL_WARN, LD_OR, "Bad length (%d) of signed material in "
  691. "LINK_AUTH cell; closing the connection", checked_len);
  692. goto err;
  693. }
  694. if (memcmp(checked, hmac, DIGEST_LEN) != 0) {
  695. log_fn(LOG_PROTOCOL_WARN, LD_OR, "Bad signed data in LINK_AUTH cell; "
  696. "closing the connection.");
  697. goto err;
  698. }
  699. s->authenticated = 1;
  700. if (connection_or_finish_or_handshake(conn)<0)
  701. goto err;
  702. tor_free(checked);
  703. return;
  704. err:
  705. tor_free(checked);
  706. connection_mark_for_close(TO_CONN(conn));
  707. }
  708. #endif