command.c 22 KB

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