connection_edge.h 11 KB

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