command.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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 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. /** How many CELL_PADDING cells have we received, ever? */
  20. uint64_t stats_n_padding_cells_processed = 0;
  21. /** How many CELL_CREATE cells have we received, ever? */
  22. uint64_t stats_n_create_cells_processed = 0;
  23. /** How many CELL_CREATED cells have we received, ever? */
  24. uint64_t stats_n_created_cells_processed = 0;
  25. /** How many CELL_RELAY cells have we received, ever? */
  26. uint64_t stats_n_relay_cells_processed = 0;
  27. /** How many CELL_DESTROY cells have we received, ever? */
  28. uint64_t stats_n_destroy_cells_processed = 0;
  29. /** How many CELL_VERSIONS cells have we received, ever? */
  30. uint64_t stats_n_versions_cells_processed = 0;
  31. /** How many CELL_NETINFO cells have we received, ever? */
  32. uint64_t stats_n_netinfo_cells_processed = 0;
  33. /* These are the main functions for processing cells */
  34. static void command_process_create_cell(cell_t *cell, or_connection_t *conn);
  35. static void command_process_created_cell(cell_t *cell, or_connection_t *conn);
  36. static void command_process_relay_cell(cell_t *cell, or_connection_t *conn);
  37. static void command_process_destroy_cell(cell_t *cell, or_connection_t *conn);
  38. static void command_process_versions_cell(var_cell_t *cell,
  39. or_connection_t *conn);
  40. static void command_process_netinfo_cell(cell_t *cell, or_connection_t *conn);
  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. int handshaking = (conn->_base.state == OR_CONN_STATE_OR_HANDSHAKING);
  75. #ifdef KEEP_TIMING_STATS
  76. /* how many of each cell have we seen so far this second? needs better
  77. * name. */
  78. static int num_create=0, num_created=0, num_relay=0, num_destroy=0;
  79. /* how long has it taken to process each type of cell? */
  80. static int create_time=0, created_time=0, relay_time=0, destroy_time=0;
  81. static time_t current_second = 0; /* from previous calls to time */
  82. time_t now = time(NULL);
  83. if (now > current_second) { /* the second has rolled over */
  84. /* print stats */
  85. log_info(LD_OR,
  86. "At end of second: %d creates (%d ms), %d createds (%d ms), "
  87. "%d relays (%d ms), %d destroys (%d ms)",
  88. num_create, create_time/1000,
  89. num_created, created_time/1000,
  90. num_relay, relay_time/1000,
  91. num_destroy, destroy_time/1000);
  92. /* zero out stats */
  93. num_create = num_created = num_relay = num_destroy = 0;
  94. create_time = created_time = relay_time = destroy_time = 0;
  95. /* remember which second it is, for next time */
  96. current_second = now;
  97. }
  98. #endif
  99. #ifdef KEEP_TIMING_STATS
  100. #define PROCESS_CELL(tp, cl, cn) STMT_BEGIN { \
  101. ++num ## tp; \
  102. command_time_process_cell(cl, cn, & tp ## time , \
  103. command_process_ ## tp ## _cell); \
  104. } STMT_END
  105. #else
  106. #define PROCESS_CELL(tp, cl, cn) command_process_ ## tp ## _cell(cl, cn)
  107. #endif
  108. /* Reject all but VERSIONS and NETINFO when handshaking. */
  109. if (handshaking && cell->command != CELL_VERSIONS &&
  110. cell->command != CELL_NETINFO)
  111. return;
  112. switch (cell->command) {
  113. case CELL_PADDING:
  114. ++stats_n_padding_cells_processed;
  115. /* do nothing */
  116. break;
  117. case CELL_CREATE:
  118. case CELL_CREATE_FAST:
  119. ++stats_n_create_cells_processed;
  120. PROCESS_CELL(create, cell, conn);
  121. break;
  122. case CELL_CREATED:
  123. case CELL_CREATED_FAST:
  124. ++stats_n_created_cells_processed;
  125. PROCESS_CELL(created, cell, conn);
  126. break;
  127. case CELL_RELAY:
  128. case CELL_RELAY_EARLY:
  129. ++stats_n_relay_cells_processed;
  130. PROCESS_CELL(relay, cell, conn);
  131. break;
  132. case CELL_DESTROY:
  133. ++stats_n_destroy_cells_processed;
  134. PROCESS_CELL(destroy, cell, conn);
  135. break;
  136. case CELL_VERSIONS:
  137. tor_fragile_assert();
  138. break;
  139. case CELL_NETINFO:
  140. ++stats_n_netinfo_cells_processed;
  141. PROCESS_CELL(netinfo, cell, conn);
  142. break;
  143. default:
  144. log_fn(LOG_INFO, LD_PROTOCOL,
  145. "Cell of unknown type (%d) received. Dropping.", cell->command);
  146. break;
  147. }
  148. }
  149. /** Process a <b>cell</b> that was just received on <b>conn</b>. Keep internal
  150. * statistics about how many of each cell we've processed so far
  151. * this second, and the total number of microseconds it took to
  152. * process each type of cell.
  153. */
  154. void
  155. command_process_var_cell(var_cell_t *cell, or_connection_t *conn)
  156. {
  157. #ifdef KEEP_TIMING_STATS
  158. /* how many of each cell have we seen so far this second? needs better
  159. * name. */
  160. static int num_versions=0, num_cert=0;
  161. time_t now = time(NULL);
  162. if (now > current_second) { /* the second has rolled over */
  163. /* print stats */
  164. log_info(LD_OR,
  165. "At end of second: %d versions (%d ms), %d cert (%d ms)",
  166. num_versions, versions_time/1000,
  167. cert, cert_time/1000);
  168. num_versions = num_cert = 0;
  169. versions_time = cert_time = 0;
  170. /* remember which second it is, for next time */
  171. current_second = now;
  172. }
  173. #endif
  174. /* reject all when not handshaking. */
  175. if (conn->_base.state != OR_CONN_STATE_OR_HANDSHAKING)
  176. return;
  177. switch (cell->command) {
  178. case CELL_VERSIONS:
  179. ++stats_n_versions_cells_processed;
  180. PROCESS_CELL(versions, cell, conn);
  181. break;
  182. default:
  183. log_warn(LD_BUG,
  184. "Variable-length cell of unknown type (%d) received.",
  185. cell->command);
  186. tor_fragile_assert();
  187. break;
  188. }
  189. }
  190. /** Process a 'create' <b>cell</b> that just arrived from <b>conn</b>. Make a
  191. * new circuit with the p_circ_id specified in cell. Put the circuit in state
  192. * onionskin_pending, and pass the onionskin to the cpuworker. Circ will get
  193. * picked up again when the cpuworker finishes decrypting it.
  194. */
  195. static void
  196. command_process_create_cell(cell_t *cell, or_connection_t *conn)
  197. {
  198. or_circuit_t *circ;
  199. int id_is_high;
  200. if (we_are_hibernating()) {
  201. log_info(LD_OR,
  202. "Received create cell but we're shutting down. Sending back "
  203. "destroy.");
  204. connection_or_send_destroy(cell->circ_id, conn,
  205. END_CIRC_REASON_HIBERNATING);
  206. return;
  207. }
  208. if (!server_mode(get_options())) {
  209. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  210. "Received create cell (type %d) from %s:%d, but we're a client. "
  211. "Sending back a destroy.",
  212. (int)cell->command, conn->_base.address, conn->_base.port);
  213. connection_or_send_destroy(cell->circ_id, conn,
  214. END_CIRC_REASON_TORPROTOCOL);
  215. return;
  216. }
  217. /* If the high bit of the circuit ID is not as expected, close the
  218. * circ. */
  219. id_is_high = cell->circ_id & (1<<15);
  220. if ((id_is_high && conn->circ_id_type == CIRC_ID_TYPE_HIGHER) ||
  221. (!id_is_high && conn->circ_id_type == CIRC_ID_TYPE_LOWER)) {
  222. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  223. "Received create cell with unexpected circ_id %d. Closing.",
  224. cell->circ_id);
  225. connection_or_send_destroy(cell->circ_id, conn,
  226. END_CIRC_REASON_TORPROTOCOL);
  227. return;
  228. }
  229. if (circuit_id_in_use_on_orconn(cell->circ_id, conn)) {
  230. routerinfo_t *router = router_get_by_digest(conn->identity_digest);
  231. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  232. "Received CREATE cell (circID %d) for known circ. "
  233. "Dropping (age %d).",
  234. cell->circ_id, (int)(time(NULL) - conn->_base.timestamp_created));
  235. if (router)
  236. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  237. "Details: nickname \"%s\", platform %s.",
  238. router->nickname, escaped(router->platform));
  239. return;
  240. }
  241. circ = or_circuit_new(cell->circ_id, conn);
  242. circ->_base.purpose = CIRCUIT_PURPOSE_OR;
  243. circuit_set_state(TO_CIRCUIT(circ), CIRCUIT_STATE_ONIONSKIN_PENDING);
  244. if (cell->command == CELL_CREATE) {
  245. char *onionskin = tor_malloc(ONIONSKIN_CHALLENGE_LEN);
  246. memcpy(onionskin, cell->payload, ONIONSKIN_CHALLENGE_LEN);
  247. /* hand it off to the cpuworkers, and then return. */
  248. if (assign_onionskin_to_cpuworker(NULL, circ, onionskin) < 0) {
  249. log_warn(LD_GENERAL,"Failed to hand off onionskin. Closing.");
  250. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  251. return;
  252. }
  253. log_debug(LD_OR,"success: handed off onionskin.");
  254. } else {
  255. /* This is a CREATE_FAST cell; we can handle it immediately without using
  256. * a CPU worker. */
  257. char keys[CPATH_KEY_MATERIAL_LEN];
  258. char reply[DIGEST_LEN*2];
  259. tor_assert(cell->command == CELL_CREATE_FAST);
  260. if (fast_server_handshake(cell->payload, reply, keys, sizeof(keys))<0) {
  261. log_warn(LD_OR,"Failed to generate key material. Closing.");
  262. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  263. return;
  264. }
  265. if (onionskin_answer(circ, CELL_CREATED_FAST, reply, keys)<0) {
  266. log_warn(LD_OR,"Failed to reply to CREATE_FAST cell. Closing.");
  267. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_INTERNAL);
  268. return;
  269. }
  270. }
  271. }
  272. /** Process a 'created' <b>cell</b> that just arrived from <b>conn</b>.
  273. * Find the circuit
  274. * that it's intended for. If we're not the origin of the circuit, package
  275. * the 'created' cell in an 'extended' relay cell and pass it back. If we
  276. * are the origin of the circuit, send it to circuit_finish_handshake() to
  277. * finish processing keys, and then call circuit_send_next_onion_skin() to
  278. * extend to the next hop in the circuit if necessary.
  279. */
  280. static void
  281. command_process_created_cell(cell_t *cell, or_connection_t *conn)
  282. {
  283. circuit_t *circ;
  284. circ = circuit_get_by_circid_orconn(cell->circ_id, conn);
  285. if (!circ) {
  286. log_info(LD_OR,
  287. "(circID %d) unknown circ (probably got a destroy earlier). "
  288. "Dropping.", cell->circ_id);
  289. return;
  290. }
  291. if (circ->n_circ_id != cell->circ_id) {
  292. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,
  293. "got created cell from Tor client? Closing.");
  294. circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
  295. return;
  296. }
  297. if (CIRCUIT_IS_ORIGIN(circ)) { /* we're the OP. Handshake this. */
  298. origin_circuit_t *origin_circ = TO_ORIGIN_CIRCUIT(circ);
  299. int err_reason = 0;
  300. log_debug(LD_OR,"at OP. Finishing handshake.");
  301. if ((err_reason = circuit_finish_handshake(origin_circ, cell->command,
  302. cell->payload)) < 0) {
  303. log_warn(LD_OR,"circuit_finish_handshake failed.");
  304. circuit_mark_for_close(circ, -err_reason);
  305. return;
  306. }
  307. log_debug(LD_OR,"Moving to next skin.");
  308. if ((err_reason = circuit_send_next_onion_skin(origin_circ)) < 0) {
  309. log_info(LD_OR,"circuit_send_next_onion_skin failed.");
  310. /* XXX push this circuit_close lower */
  311. circuit_mark_for_close(circ, -err_reason);
  312. return;
  313. }
  314. } else { /* pack it into an extended relay cell, and send it. */
  315. log_debug(LD_OR,
  316. "Converting created cell to extended relay cell, sending.");
  317. relay_send_command_from_edge(0, circ, RELAY_COMMAND_EXTENDED,
  318. cell->payload, ONIONSKIN_REPLY_LEN,
  319. NULL);
  320. }
  321. }
  322. /** Process a 'relay' or 'relay_early' <b>cell</b> that just arrived from
  323. * <b>conn</b>. Make sure it came in with a recognized circ_id. Pass it on to
  324. * circuit_receive_relay_cell() for actual processing.
  325. */
  326. static void
  327. command_process_relay_cell(cell_t *cell, or_connection_t *conn)
  328. {
  329. circuit_t *circ;
  330. int reason, direction;
  331. circ = circuit_get_by_circid_orconn(cell->circ_id, conn);
  332. if (!circ) {
  333. log_debug(LD_OR,
  334. "unknown circuit %d on connection from %s:%d. Dropping.",
  335. cell->circ_id, conn->_base.address, conn->_base.port);
  336. return;
  337. }
  338. if (circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  339. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,"circuit in create_wait. Closing.");
  340. circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
  341. return;
  342. }
  343. if (CIRCUIT_IS_ORIGIN(circ)) {
  344. /* if we're a relay and treating connections with recent local
  345. * traffic better, then this is one of them. */
  346. conn->client_used = time(NULL);
  347. }
  348. if (!CIRCUIT_IS_ORIGIN(circ) &&
  349. cell->circ_id == TO_OR_CIRCUIT(circ)->p_circ_id)
  350. direction = CELL_DIRECTION_OUT;
  351. else
  352. direction = CELL_DIRECTION_IN;
  353. /* If we have a relay_early cell, make sure that it's outbound, and we've
  354. * gotten no more than MAX_RELAY_EARLY_CELLS_PER_CIRCUIT of them. */
  355. if (cell->command == CELL_RELAY_EARLY) {
  356. if (direction == CELL_DIRECTION_IN) {
  357. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  358. "Received an inbound RELAY_EARLY cell on circuit %d from %s:%d."
  359. " Closing circuit.",
  360. cell->circ_id, conn->_base.address, conn->_base.port);
  361. circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
  362. return;
  363. } else {
  364. or_circuit_t *or_circ = TO_OR_CIRCUIT(circ);
  365. if (or_circ->remaining_relay_early_cells == 0) {
  366. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  367. "Received too many RELAY_EARLY cells on circ %d from %s:%d."
  368. " Closing circuit.",
  369. cell->circ_id, safe_str(conn->_base.address), conn->_base.port);
  370. circuit_mark_for_close(circ, END_CIRC_REASON_TORPROTOCOL);
  371. return;
  372. }
  373. --or_circ->remaining_relay_early_cells;
  374. }
  375. }
  376. if ((reason = circuit_receive_relay_cell(cell, circ, direction)) < 0) {
  377. log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,"circuit_receive_relay_cell "
  378. "(%s) failed. Closing.",
  379. direction==CELL_DIRECTION_OUT?"forward":"backward");
  380. circuit_mark_for_close(circ, -reason);
  381. }
  382. }
  383. /** Process a 'destroy' <b>cell</b> that just arrived from
  384. * <b>conn</b>. Find the circ that it refers to (if any).
  385. *
  386. * If the circ is in state
  387. * onionskin_pending, then call onion_pending_remove() to remove it
  388. * from the pending onion list (note that if it's already being
  389. * processed by the cpuworker, it won't be in the list anymore; but
  390. * when the cpuworker returns it, the circuit will be gone, and the
  391. * cpuworker response will be dropped).
  392. *
  393. * Then mark the circuit for close (which marks all edges for close,
  394. * and passes the destroy cell onward if necessary).
  395. */
  396. static void
  397. command_process_destroy_cell(cell_t *cell, or_connection_t *conn)
  398. {
  399. circuit_t *circ;
  400. int reason;
  401. circ = circuit_get_by_circid_orconn(cell->circ_id, conn);
  402. reason = (uint8_t)cell->payload[0];
  403. if (!circ) {
  404. log_info(LD_OR,"unknown circuit %d on connection from %s:%d. Dropping.",
  405. cell->circ_id, conn->_base.address, conn->_base.port);
  406. return;
  407. }
  408. log_debug(LD_OR,"Received for circID %d.",cell->circ_id);
  409. if (!CIRCUIT_IS_ORIGIN(circ) &&
  410. cell->circ_id == TO_OR_CIRCUIT(circ)->p_circ_id) {
  411. /* the destroy came from behind */
  412. circuit_set_p_circid_orconn(TO_OR_CIRCUIT(circ), 0, NULL);
  413. circuit_mark_for_close(circ, reason|END_CIRC_REASON_FLAG_REMOTE);
  414. } else { /* the destroy came from ahead */
  415. circuit_set_n_circid_orconn(circ, 0, NULL);
  416. if (CIRCUIT_IS_ORIGIN(circ)) {
  417. circuit_mark_for_close(circ, reason|END_CIRC_REASON_FLAG_REMOTE);
  418. } else {
  419. char payload[1];
  420. log_debug(LD_OR, "Delivering 'truncated' back.");
  421. payload[0] = (char)reason;
  422. relay_send_command_from_edge(0, circ, RELAY_COMMAND_TRUNCATED,
  423. payload, sizeof(payload), NULL);
  424. }
  425. }
  426. }
  427. /** Process a 'versions' cell. The current link protocol version must be 0
  428. * to indicate that no version has yet been negotiated. We compare the
  429. * versions in the cell to the list of versions we support, pick the
  430. * highest version we have in common, and continue the negotiation from
  431. * there.
  432. */
  433. static void
  434. command_process_versions_cell(var_cell_t *cell, or_connection_t *conn)
  435. {
  436. int highest_supported_version = 0;
  437. const char *cp, *end;
  438. if (conn->link_proto != 0 ||
  439. conn->_base.state != OR_CONN_STATE_OR_HANDSHAKING ||
  440. (conn->handshake_state && conn->handshake_state->received_versions)) {
  441. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  442. "Received a VERSIONS cell on a connection with its version "
  443. "already set to %d; dropping", (int) conn->link_proto);
  444. return;
  445. }
  446. tor_assert(conn->handshake_state);
  447. end = cell->payload + cell->payload_len;
  448. for (cp = cell->payload; cp+1 < end; ++cp) {
  449. uint16_t v = ntohs(get_uint16(cp));
  450. if (is_or_protocol_version_known(v) && v > highest_supported_version)
  451. highest_supported_version = v;
  452. }
  453. if (!highest_supported_version) {
  454. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  455. "Couldn't find a version in common between my version list and the "
  456. "list in the VERSIONS cell; closing connection.");
  457. connection_mark_for_close(TO_CONN(conn));
  458. return;
  459. } else if (highest_supported_version == 1) {
  460. /* Negotiating version 1 makes no sense, since version 1 has no VERSIONS
  461. * cells. */
  462. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  463. "Used version negotiation protocol to negotiate a v1 connection. "
  464. "That's crazily non-compliant. Closing connection.");
  465. connection_mark_for_close(TO_CONN(conn));
  466. return;
  467. }
  468. conn->link_proto = highest_supported_version;
  469. conn->handshake_state->received_versions = 1;
  470. log_info(LD_OR, "Negotiated version %d with %s; sending NETINFO.",
  471. highest_supported_version, safe_str(conn->_base.address));
  472. tor_assert(conn->link_proto >= 2);
  473. if (connection_or_send_netinfo(conn) < 0) {
  474. connection_mark_for_close(TO_CONN(conn));
  475. return;
  476. }
  477. }
  478. /** Process a 'netinfo' cell: read and act on its contents, and set the
  479. * connection state to "open". */
  480. static void
  481. command_process_netinfo_cell(cell_t *cell, or_connection_t *conn)
  482. {
  483. time_t timestamp;
  484. uint8_t my_addr_type;
  485. uint8_t my_addr_len;
  486. const char *my_addr_ptr;
  487. const char *cp, *end;
  488. uint8_t n_other_addrs;
  489. time_t now = time(NULL);
  490. long apparent_skew = 0;
  491. uint32_t my_apparent_addr = 0;
  492. if (conn->link_proto < 2) {
  493. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  494. "Received a NETINFO cell on %s connection; dropping.",
  495. conn->link_proto == 0 ? "non-versioned" : "a v1");
  496. return;
  497. }
  498. if (conn->_base.state != OR_CONN_STATE_OR_HANDSHAKING) {
  499. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  500. "Received a NETINFO cell on non-handshaking connection; dropping.");
  501. return;
  502. }
  503. tor_assert(conn->handshake_state &&
  504. conn->handshake_state->received_versions);
  505. /* Decode the cell. */
  506. timestamp = ntohl(get_uint32(cell->payload));
  507. if (labs(now - conn->handshake_state->sent_versions_at) < 180) {
  508. apparent_skew = now - timestamp;
  509. }
  510. my_addr_type = (uint8_t) cell->payload[4];
  511. my_addr_len = (uint8_t) cell->payload[5];
  512. my_addr_ptr = cell->payload + 6;
  513. end = cell->payload + CELL_PAYLOAD_SIZE;
  514. cp = cell->payload + 6 + my_addr_len;
  515. if (cp >= end) {
  516. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  517. "Addresses too long in netinfo cell; closing connection.");
  518. connection_mark_for_close(TO_CONN(conn));
  519. return;
  520. } else if (my_addr_type == RESOLVED_TYPE_IPV4 && my_addr_len == 4) {
  521. my_apparent_addr = ntohl(get_uint32(my_addr_ptr));
  522. }
  523. n_other_addrs = (uint8_t) *cp++;
  524. while (n_other_addrs && cp < end-2) {
  525. /* Consider all the other addresses; if any matches, this connection is
  526. * "canonical." */
  527. tor_addr_t addr;
  528. const char *next = decode_address_from_payload(&addr, cp, end-cp);
  529. if (next == NULL) {
  530. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  531. "Bad address in netinfo cell; closing connection.");
  532. connection_mark_for_close(TO_CONN(conn));
  533. return;
  534. }
  535. if (tor_addr_eq(&addr, &conn->real_addr)) {
  536. conn->is_canonical = 1;
  537. break;
  538. }
  539. cp = next;
  540. --n_other_addrs;
  541. }
  542. /* Act on apparent skew. */
  543. /** Warn when we get a netinfo skew with at least this value. */
  544. #define NETINFO_NOTICE_SKEW 3600
  545. if (labs(apparent_skew) > NETINFO_NOTICE_SKEW &&
  546. router_get_by_digest(conn->identity_digest)) {
  547. char dbuf[64];
  548. int severity;
  549. /*XXXX be smarter about when everybody says we are skewed. */
  550. if (router_digest_is_trusted_dir(conn->identity_digest))
  551. severity = LOG_WARN;
  552. else
  553. severity = LOG_INFO;
  554. format_time_interval(dbuf, sizeof(dbuf), apparent_skew);
  555. log_fn(severity, LD_GENERAL, "Received NETINFO cell with skewed time from "
  556. "server at %s:%d. It seems that our clock is %s by %s, or "
  557. "that theirs is %s. Tor requires an accurate clock to work: "
  558. "please check your time and date settings.",
  559. conn->_base.address, (int)conn->_base.port,
  560. apparent_skew>0 ? "ahead" : "behind", dbuf,
  561. apparent_skew>0 ? "behind" : "ahead");
  562. control_event_general_status(LOG_WARN,
  563. "CLOCK_SKEW SKEW=%ld SOURCE=OR:%s:%d",
  564. apparent_skew, conn->_base.address, conn->_base.port);
  565. }
  566. /* XXX maybe act on my_apparent_addr, if the source is sufficiently
  567. * trustworthy. */
  568. if (connection_or_set_state_open(conn)<0)
  569. connection_mark_for_close(TO_CONN(conn));
  570. else
  571. log_info(LD_OR, "Got good NETINFO cell from %s; OR connection is now "
  572. "open, using protocol version %d",
  573. safe_str(conn->_base.address), (int)conn->link_proto);
  574. assert_connection_ok(TO_CONN(conn),time(NULL));
  575. }