connection_edge.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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-2019, 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. void connection_ap_mark_as_waiting_for_renddesc(
  115. entry_connection_t *entry_conn);
  116. #define CONNECTION_AP_EXPECT_NONPENDING(c) do { \
  117. if (ENTRY_TO_CONN(c)->state == AP_CONN_STATE_CIRCUIT_WAIT) { \
  118. log_warn(LD_BUG, "At %s:%d: %p was unexpectedly in circuit_wait.", \
  119. __FILE__, __LINE__, (c)); \
  120. connection_ap_mark_as_non_pending_circuit(c); \
  121. } \
  122. } while (0)
  123. void connection_ap_fail_onehop(const char *failed_digest,
  124. cpath_build_state_t *build_state);
  125. void circuit_discard_optional_exit_enclaves(extend_info_t *info);
  126. int connection_ap_detach_retriable(entry_connection_t *conn,
  127. origin_circuit_t *circ,
  128. int reason);
  129. int connection_ap_process_transparent(entry_connection_t *conn);
  130. int address_is_invalid_destination(const char *address, int client);
  131. MOCK_DECL(int, connection_ap_rewrite_and_attach_if_allowed,
  132. (entry_connection_t *conn,
  133. origin_circuit_t *circ,
  134. crypt_path_t *cpath));
  135. int connection_ap_handshake_rewrite_and_attach(entry_connection_t *conn,
  136. origin_circuit_t *circ,
  137. crypt_path_t *cpath);
  138. /** Possible return values for parse_extended_hostname. */
  139. typedef enum hostname_type_t {
  140. NORMAL_HOSTNAME, ONION_V2_HOSTNAME, ONION_V3_HOSTNAME,
  141. EXIT_HOSTNAME, BAD_HOSTNAME
  142. } hostname_type_t;
  143. hostname_type_t parse_extended_hostname(char *address);
  144. #if defined(HAVE_NET_IF_H) && defined(HAVE_NET_PFVAR_H)
  145. int get_pf_socket(void);
  146. #endif
  147. int connection_edge_compatible_with_circuit(const entry_connection_t *conn,
  148. const origin_circuit_t *circ);
  149. int connection_edge_update_circuit_isolation(const entry_connection_t *conn,
  150. origin_circuit_t *circ,
  151. int dry_run);
  152. void circuit_clear_isolation(origin_circuit_t *circ);
  153. streamid_t get_unique_stream_id_by_circ(origin_circuit_t *circ);
  154. void connection_edge_free_all(void);
  155. void connection_ap_warn_and_unmark_if_pending_circ(
  156. entry_connection_t *entry_conn,
  157. const char *where);
  158. int connection_half_edge_is_valid_data(const smartlist_t *half_conns,
  159. streamid_t stream_id);
  160. int connection_half_edge_is_valid_sendme(const smartlist_t *half_conns,
  161. streamid_t stream_id);
  162. int connection_half_edge_is_valid_connected(const smartlist_t *half_conns,
  163. streamid_t stream_id);
  164. int connection_half_edge_is_valid_end(smartlist_t *half_conns,
  165. streamid_t stream_id);
  166. int connection_half_edge_is_valid_resolved(smartlist_t *half_conns,
  167. streamid_t stream_id);
  168. size_t half_streams_get_total_allocation(void);
  169. struct half_edge_t;
  170. void half_edge_free_(struct half_edge_t *he);
  171. #define half_edge_free(he) \
  172. FREE_AND_NULL(half_edge_t, half_edge_free_, (he))
  173. /** @name Begin-cell flags
  174. *
  175. * These flags are used in RELAY_BEGIN cells to change the default behavior
  176. * of the cell.
  177. *
  178. * @{
  179. **/
  180. /** When this flag is set, the client is willing to get connected to IPv6
  181. * addresses */
  182. #define BEGIN_FLAG_IPV6_OK (1u<<0)
  183. /** When this flag is set, the client DOES NOT support connecting to IPv4
  184. * addresses. (The sense of this flag is inverted from IPV6_OK, so that the
  185. * old default behavior of Tor is equivalent to having all flags set to 0.)
  186. **/
  187. #define BEGIN_FLAG_IPV4_NOT_OK (1u<<1)
  188. /** When this flag is set, if we find both an IPv4 and an IPv6 address,
  189. * we use the IPv6 address. Otherwise we use the IPv4 address. */
  190. #define BEGIN_FLAG_IPV6_PREFERRED (1u<<2)
  191. /**@}*/
  192. #ifdef CONNECTION_EDGE_PRIVATE
  193. /** A parsed BEGIN or BEGIN_DIR cell */
  194. typedef struct begin_cell_t {
  195. /** The address the client has asked us to connect to, or NULL if this is
  196. * a BEGIN_DIR cell*/
  197. char *address;
  198. /** The flags specified in the BEGIN cell's body. One or more of
  199. * BEGIN_FLAG_*. */
  200. uint32_t flags;
  201. /** The client's requested port. */
  202. uint16_t port;
  203. /** The client's requested Stream ID */
  204. uint16_t stream_id;
  205. /** True iff this is a BEGIN_DIR cell. */
  206. unsigned is_begindir : 1;
  207. } begin_cell_t;
  208. STATIC int begin_cell_parse(const cell_t *cell, begin_cell_t *bcell,
  209. uint8_t *end_reason_out);
  210. STATIC int connected_cell_format_payload(uint8_t *payload_out,
  211. const tor_addr_t *addr,
  212. uint32_t ttl);
  213. typedef struct {
  214. /** Original address, after we lowercased it but before we started
  215. * mapping it.
  216. */
  217. char orig_address[MAX_SOCKS_ADDR_LEN];
  218. /** True iff the address has been automatically remapped to a local
  219. * address in VirtualAddrNetwork. (Only set true when we do a resolve
  220. * and get a virtual address; not when we connect to the address.) */
  221. int automap;
  222. /** If this connection has a .exit address, who put it there? */
  223. addressmap_entry_source_t exit_source;
  224. /** If we've rewritten the address, when does this map expire? */
  225. time_t map_expires;
  226. /** If we should close the connection, this is the end_reason to pass
  227. * to connection_mark_unattached_ap */
  228. int end_reason;
  229. /** True iff we should close the connection, either because of error or
  230. * because of successful early RESOLVED reply. */
  231. int should_close;
  232. } rewrite_result_t;
  233. STATIC void connection_ap_handshake_rewrite(entry_connection_t *conn,
  234. rewrite_result_t *out);
  235. STATIC int connection_ap_process_http_connect(entry_connection_t *conn);
  236. STATIC void export_hs_client_circuit_id(edge_connection_t *edge_conn,
  237. hs_circuit_id_protocol_t protocol);
  238. struct half_edge_t;
  239. STATIC void connection_half_edge_add(const edge_connection_t *conn,
  240. origin_circuit_t *circ);
  241. STATIC struct half_edge_t *connection_half_edge_find_stream_id(
  242. const smartlist_t *half_conns,
  243. streamid_t stream_id);
  244. #endif /* defined(CONNECTION_EDGE_PRIVATE) */
  245. #endif /* !defined(TOR_CONNECTION_EDGE_H) */