connection_edge.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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-2018, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file connection_edge.h
  8. * \brief Header file for connection_edge.c.
  9. **/
  10. #ifndef TOR_CONNECTION_EDGE_H
  11. #define TOR_CONNECTION_EDGE_H
  12. #include "lib/testsupport/testsupport.h"
  13. #include "feature/hs/hs_service.h"
  14. edge_connection_t *TO_EDGE_CONN(connection_t *);
  15. entry_connection_t *TO_ENTRY_CONN(connection_t *);
  16. entry_connection_t *EDGE_TO_ENTRY_CONN(edge_connection_t *);
  17. #define EXIT_CONN_STATE_MIN_ 1
  18. /** State for an exit connection: waiting for response from DNS farm. */
  19. #define EXIT_CONN_STATE_RESOLVING 1
  20. /** State for an exit connection: waiting for connect() to finish. */
  21. #define EXIT_CONN_STATE_CONNECTING 2
  22. /** State for an exit connection: open and ready to transmit data. */
  23. #define EXIT_CONN_STATE_OPEN 3
  24. /** State for an exit connection: waiting to be removed. */
  25. #define EXIT_CONN_STATE_RESOLVEFAILED 4
  26. #define EXIT_CONN_STATE_MAX_ 4
  27. /* The AP state values must be disjoint from the EXIT state values. */
  28. #define AP_CONN_STATE_MIN_ 5
  29. /** State for a SOCKS connection: waiting for SOCKS request. */
  30. #define AP_CONN_STATE_SOCKS_WAIT 5
  31. /** State for a SOCKS connection: got a y.onion URL; waiting to receive
  32. * rendezvous descriptor. */
  33. #define AP_CONN_STATE_RENDDESC_WAIT 6
  34. /** The controller will attach this connection to a circuit; it isn't our
  35. * job to do so. */
  36. #define AP_CONN_STATE_CONTROLLER_WAIT 7
  37. /** State for a SOCKS connection: waiting for a completed circuit. */
  38. #define AP_CONN_STATE_CIRCUIT_WAIT 8
  39. /** State for a SOCKS connection: sent BEGIN, waiting for CONNECTED. */
  40. #define AP_CONN_STATE_CONNECT_WAIT 9
  41. /** State for a SOCKS connection: sent RESOLVE, waiting for RESOLVED. */
  42. #define AP_CONN_STATE_RESOLVE_WAIT 10
  43. /** State for a SOCKS connection: ready to send and receive. */
  44. #define AP_CONN_STATE_OPEN 11
  45. /** State for a transparent natd connection: waiting for original
  46. * destination. */
  47. #define AP_CONN_STATE_NATD_WAIT 12
  48. /** State for an HTTP tunnel: waiting for an HTTP CONNECT command. */
  49. #define AP_CONN_STATE_HTTP_CONNECT_WAIT 13
  50. #define AP_CONN_STATE_MAX_ 13
  51. #define EXIT_PURPOSE_MIN_ 1
  52. /** This exit stream wants to do an ordinary connect. */
  53. #define EXIT_PURPOSE_CONNECT 1
  54. /** This exit stream wants to do a resolve (either normal or reverse). */
  55. #define EXIT_PURPOSE_RESOLVE 2
  56. #define EXIT_PURPOSE_MAX_ 2
  57. /** True iff the AP_CONN_STATE_* value <b>s</b> means that the corresponding
  58. * edge connection is not attached to any circuit. */
  59. #define AP_CONN_STATE_IS_UNATTACHED(s) \
  60. ((s) <= AP_CONN_STATE_CIRCUIT_WAIT || (s) == AP_CONN_STATE_NATD_WAIT)
  61. #define connection_mark_unattached_ap(conn, endreason) \
  62. connection_mark_unattached_ap_((conn), (endreason), __LINE__, SHORT_FILE__)
  63. MOCK_DECL(void,connection_mark_unattached_ap_,
  64. (entry_connection_t *conn, int endreason,
  65. int line, const char *file));
  66. int connection_edge_reached_eof(edge_connection_t *conn);
  67. int connection_edge_process_inbuf(edge_connection_t *conn,
  68. int package_partial);
  69. int connection_edge_destroy(circid_t circ_id, edge_connection_t *conn);
  70. int connection_edge_end(edge_connection_t *conn, uint8_t reason);
  71. int connection_edge_end_errno(edge_connection_t *conn);
  72. int connection_edge_flushed_some(edge_connection_t *conn);
  73. int connection_edge_finished_flushing(edge_connection_t *conn);
  74. int connection_edge_finished_connecting(edge_connection_t *conn);
  75. void connection_ap_about_to_close(entry_connection_t *edge_conn);
  76. void connection_exit_about_to_close(edge_connection_t *edge_conn);
  77. MOCK_DECL(int,
  78. connection_ap_handshake_send_begin,(entry_connection_t *ap_conn));
  79. int connection_ap_handshake_send_resolve(entry_connection_t *ap_conn);
  80. entry_connection_t *connection_ap_make_link(connection_t *partner,
  81. char *address, uint16_t port,
  82. const char *digest,
  83. int session_group,
  84. int isolation_flags,
  85. int use_begindir, int want_onehop);
  86. void connection_ap_handshake_socks_reply(entry_connection_t *conn, char *reply,
  87. size_t replylen,
  88. int endreason);
  89. MOCK_DECL(void,connection_ap_handshake_socks_resolved,
  90. (entry_connection_t *conn,
  91. int answer_type,
  92. size_t answer_len,
  93. const uint8_t *answer,
  94. int ttl,
  95. time_t expires));
  96. void connection_ap_handshake_socks_resolved_addr(entry_connection_t *conn,
  97. const tor_addr_t *answer,
  98. int ttl,
  99. time_t expires);
  100. int connection_exit_begin_conn(cell_t *cell, circuit_t *circ);
  101. int connection_exit_begin_resolve(cell_t *cell, or_circuit_t *circ);
  102. void connection_exit_connect(edge_connection_t *conn);
  103. int connection_edge_is_rendezvous_stream(const edge_connection_t *conn);
  104. int connection_ap_can_use_exit(const entry_connection_t *conn,
  105. const node_t *exit);
  106. void connection_ap_expire_beginning(void);
  107. void connection_ap_rescan_and_attach_pending(void);
  108. void connection_ap_attach_pending(int retry);
  109. void connection_ap_mark_as_pending_circuit_(entry_connection_t *entry_conn,
  110. const char *file, int line);
  111. #define connection_ap_mark_as_pending_circuit(c) \
  112. connection_ap_mark_as_pending_circuit_((c), __FILE__, __LINE__)
  113. void connection_ap_mark_as_non_pending_circuit(entry_connection_t *entry_conn);
  114. #define CONNECTION_AP_EXPECT_NONPENDING(c) do { \
  115. if (ENTRY_TO_CONN(c)->state == AP_CONN_STATE_CIRCUIT_WAIT) { \
  116. log_warn(LD_BUG, "At %s:%d: %p was unexpectedly in circuit_wait.", \
  117. __FILE__, __LINE__, (c)); \
  118. connection_ap_mark_as_non_pending_circuit(c); \
  119. } \
  120. } while (0)
  121. void connection_ap_fail_onehop(const char *failed_digest,
  122. cpath_build_state_t *build_state);
  123. void circuit_discard_optional_exit_enclaves(extend_info_t *info);
  124. int connection_ap_detach_retriable(entry_connection_t *conn,
  125. origin_circuit_t *circ,
  126. int reason);
  127. int connection_ap_process_transparent(entry_connection_t *conn);
  128. int address_is_invalid_destination(const char *address, int client);
  129. MOCK_DECL(int, connection_ap_rewrite_and_attach_if_allowed,
  130. (entry_connection_t *conn,
  131. origin_circuit_t *circ,
  132. crypt_path_t *cpath));
  133. int connection_ap_handshake_rewrite_and_attach(entry_connection_t *conn,
  134. origin_circuit_t *circ,
  135. crypt_path_t *cpath);
  136. /** Possible return values for parse_extended_hostname. */
  137. typedef enum hostname_type_t {
  138. NORMAL_HOSTNAME, ONION_V2_HOSTNAME, ONION_V3_HOSTNAME,
  139. EXIT_HOSTNAME, BAD_HOSTNAME
  140. } hostname_type_t;
  141. hostname_type_t parse_extended_hostname(char *address);
  142. #if defined(HAVE_NET_IF_H) && defined(HAVE_NET_PFVAR_H)
  143. int get_pf_socket(void);
  144. #endif
  145. int connection_edge_compatible_with_circuit(const entry_connection_t *conn,
  146. const origin_circuit_t *circ);
  147. int connection_edge_update_circuit_isolation(const entry_connection_t *conn,
  148. origin_circuit_t *circ,
  149. int dry_run);
  150. void circuit_clear_isolation(origin_circuit_t *circ);
  151. streamid_t get_unique_stream_id_by_circ(origin_circuit_t *circ);
  152. void connection_edge_free_all(void);
  153. void connection_ap_warn_and_unmark_if_pending_circ(
  154. entry_connection_t *entry_conn,
  155. const char *where);
  156. int connection_half_edge_is_valid_data(const smartlist_t *half_conns,
  157. streamid_t stream_id);
  158. int connection_half_edge_is_valid_sendme(const smartlist_t *half_conns,
  159. streamid_t stream_id);
  160. int connection_half_edge_is_valid_connected(const smartlist_t *half_conns,
  161. streamid_t stream_id);
  162. int connection_half_edge_is_valid_end(smartlist_t *half_conns,
  163. streamid_t stream_id);
  164. int connection_half_edge_is_valid_resolved(smartlist_t *half_conns,
  165. streamid_t stream_id);
  166. size_t half_streams_get_total_allocation(void);
  167. struct half_edge_t;
  168. void half_edge_free_(struct half_edge_t *he);
  169. #define half_edge_free(he) \
  170. FREE_AND_NULL(half_edge_t, half_edge_free_, (he))
  171. /** @name Begin-cell flags
  172. *
  173. * These flags are used in RELAY_BEGIN cells to change the default behavior
  174. * of the cell.
  175. *
  176. * @{
  177. **/
  178. /** When this flag is set, the client is willing to get connected to IPv6
  179. * addresses */
  180. #define BEGIN_FLAG_IPV6_OK (1u<<0)
  181. /** When this flag is set, the client DOES NOT support connecting to IPv4
  182. * addresses. (The sense of this flag is inverted from IPV6_OK, so that the
  183. * old default behavior of Tor is equivalent to having all flags set to 0.)
  184. **/
  185. #define BEGIN_FLAG_IPV4_NOT_OK (1u<<1)
  186. /** When this flag is set, if we find both an IPv4 and an IPv6 address,
  187. * we use the IPv6 address. Otherwise we use the IPv4 address. */
  188. #define BEGIN_FLAG_IPV6_PREFERRED (1u<<2)
  189. /**@}*/
  190. #ifdef CONNECTION_EDGE_PRIVATE
  191. /** A parsed BEGIN or BEGIN_DIR cell */
  192. typedef struct begin_cell_t {
  193. /** The address the client has asked us to connect to, or NULL if this is
  194. * a BEGIN_DIR cell*/
  195. char *address;
  196. /** The flags specified in the BEGIN cell's body. One or more of
  197. * BEGIN_FLAG_*. */
  198. uint32_t flags;
  199. /** The client's requested port. */
  200. uint16_t port;
  201. /** The client's requested Stream ID */
  202. uint16_t stream_id;
  203. /** True iff this is a BEGIN_DIR cell. */
  204. unsigned is_begindir : 1;
  205. } begin_cell_t;
  206. STATIC int begin_cell_parse(const cell_t *cell, begin_cell_t *bcell,
  207. uint8_t *end_reason_out);
  208. STATIC int connected_cell_format_payload(uint8_t *payload_out,
  209. const tor_addr_t *addr,
  210. uint32_t ttl);
  211. typedef struct {
  212. /** Original address, after we lowercased it but before we started
  213. * mapping it.
  214. */
  215. char orig_address[MAX_SOCKS_ADDR_LEN];
  216. /** True iff the address has been automatically remapped to a local
  217. * address in VirtualAddrNetwork. (Only set true when we do a resolve
  218. * and get a virtual address; not when we connect to the address.) */
  219. int automap;
  220. /** If this connection has a .exit address, who put it there? */
  221. addressmap_entry_source_t exit_source;
  222. /** If we've rewritten the address, when does this map expire? */
  223. time_t map_expires;
  224. /** If we should close the connection, this is the end_reason to pass
  225. * to connection_mark_unattached_ap */
  226. int end_reason;
  227. /** True iff we should close the connection, either because of error or
  228. * because of successful early RESOLVED reply. */
  229. int should_close;
  230. } rewrite_result_t;
  231. STATIC void connection_ap_handshake_rewrite(entry_connection_t *conn,
  232. rewrite_result_t *out);
  233. STATIC int connection_ap_process_http_connect(entry_connection_t *conn);
  234. STATIC void export_hs_client_circuit_id(edge_connection_t *edge_conn,
  235. hs_circuit_id_protocol_t protocol);
  236. struct half_edge_t;
  237. STATIC void connection_half_edge_add(const edge_connection_t *conn,
  238. origin_circuit_t *circ);
  239. STATIC struct half_edge_t *connection_half_edge_find_stream_id(
  240. const smartlist_t *half_conns,
  241. streamid_t stream_id);
  242. #endif /* defined(CONNECTION_EDGE_PRIVATE) */
  243. #endif /* !defined(TOR_CONNECTION_EDGE_H) */