command.c 23 KB

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