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