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-2021, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
6
7 /**
8 * \file connection.c
9 * \brief General high-level functions to handle reading and writing
10 * on connections.
11 *
12 * Each connection (ideally) represents a TLS connection, a TCP socket, a unix
13 * socket, or a UDP socket on which reads and writes can occur. (But see
14 * connection_edge.c for cases where connections can also represent streams
15 * that do not have a corresponding socket.)
16 *
17 * The module implements the abstract type, connection_t. The subtypes are:
18 * <ul>
19 * <li>listener_connection_t, implemented here in connection.c
20 * <li>dir_connection_t, implemented in directory.c
21 * <li>or_connection_t, implemented in connection_or.c
22 * <li>edge_connection_t, implemented in connection_edge.c, along with
23 * its subtype(s):
24 * <ul><li>entry_connection_t, also implemented in connection_edge.c
25 * </ul>
26 * <li>control_connection_t, implemented in control.c
27 * </ul>
28 *
29 * The base type implemented in this module is responsible for basic
30 * rate limiting, flow control, and marshalling bytes onto and off of the
31 * network (either directly or via TLS).
32 *
33 * Connections are registered with the main loop with connection_add(). As
34 * they become able to read or write register the fact with the event main
35 * loop by calling connection_watch_events(), connection_start_reading(), or
36 * connection_start_writing(). When they no longer want to read or write,
37 * they call connection_stop_reading() or connection_stop_writing().
38 *
39 * To queue data to be written on a connection, call
40 * connection_buf_add(). When data arrives, the
41 * connection_process_inbuf() callback is invoked, which dispatches to a
42 * type-specific function (such as connection_edge_process_inbuf() for
43 * example). Connection types that need notice of when data has been written
44 * receive notification via connection_flushed_some() and
45 * connection_finished_flushing(). These functions all delegate to
46 * type-specific implementations.
47 *
48 * Additionally, beyond the core of connection_t, this module also implements:
49 * <ul>
50 * <li>Listeners, which wait for incoming sockets and launch connections
51 * <li>Outgoing SOCKS proxy support
52 * <li>Outgoing HTTP proxy support
53 * <li>An out-of-sockets handler for dealing with socket exhaustion
54 * </ul>
55 **/
56
57 #define CONNECTION_PRIVATE
58 #include "core/or/or.h"
59 #include "feature/client/bridges.h"
60 #include "lib/buf/buffers.h"
61 #include "lib/tls/buffers_tls.h"
62 #include "lib/err/backtrace.h"
63
64 /*
65 * Define this so we get channel internal functions, since we're implementing
66 * part of a subclass (channel_tls_t).
67 */
68 #define CHANNEL_OBJECT_PRIVATE
69 #include "app/config/config.h"
70 #include "app/config/resolve_addr.h"
71 #include "core/mainloop/connection.h"
72 #include "core/mainloop/mainloop.h"
73 #include "core/mainloop/netstatus.h"
74 #include "core/or/channel.h"
75 #include "core/or/channeltls.h"
76 #include "core/or/circuitbuild.h"
77 #include "core/or/circuitlist.h"
78 #include "core/or/circuituse.h"
79 #include "core/or/connection_edge.h"
80 #include "core/or/connection_or.h"
81 #include "core/or/dos.h"
82 #include "core/or/policies.h"
83 #include "core/or/reasons.h"
84 #include "core/or/relay.h"
85 #include "core/or/status.h"
86 #include "core/or/crypt_path.h"
87 #include "core/proto/proto_haproxy.h"
88 #include "core/proto/proto_http.h"
89 #include "core/proto/proto_socks.h"
90 #include "feature/client/dnsserv.h"
91 #include "feature/client/entrynodes.h"
92 #include "feature/client/transports.h"
93 #include "feature/control/control.h"
94 #include "feature/control/control_events.h"
95 #include "feature/dirauth/authmode.h"
96 #include "feature/dirauth/dirauth_config.h"
97 #include "feature/dircache/dirserv.h"
98 #include "feature/dircommon/directory.h"
99 #include "feature/hibernate/hibernate.h"
100 #include "feature/hs/hs_common.h"
101 #include "feature/hs/hs_ident.h"
102 #include "feature/hs/hs_metrics.h"
103 #include "feature/metrics/metrics.h"
104 #include "feature/nodelist/nodelist.h"
105 #include "feature/nodelist/routerlist.h"
106 #include "feature/relay/dns.h"
107 #include "feature/relay/ext_orport.h"
108 #include "feature/relay/routermode.h"
109 #include "feature/rend/rendcommon.h"
110 #include "feature/stats/connstats.h"
111 #include "feature/stats/rephist.h"
112 #include "feature/stats/bwhist.h"
113 #include "lib/crypt_ops/crypto_util.h"
114 #include "lib/crypt_ops/crypto_format.h"
115 #include "lib/geoip/geoip.h"
116
117 #include "lib/cc/ctassert.h"
118 #include "lib/sandbox/sandbox.h"
119 #include "lib/net/buffers_net.h"
120 #include "lib/net/address.h"
121 #include "lib/tls/tortls.h"
122 #include "lib/evloop/compat_libevent.h"
123 #include "lib/compress/compress.h"
124
125 #ifdef HAVE_PWD_H
126 #include <pwd.h>
127 #endif
128
129 #ifdef HAVE_UNISTD_H
130 #include <unistd.h>
131 #endif
132 #ifdef HAVE_SYS_STAT_H
133 #include <sys/stat.h>
134 #endif
135
136 #ifdef HAVE_SYS_UN_H
137 #include <sys/socket.h>
138 #include <sys/un.h>
139 #endif
140
141 #include "feature/dircommon/dir_connection_st.h"
142 #include "feature/control/control_connection_st.h"
143 #include "core/or/entry_connection_st.h"
144 #include "core/or/listener_connection_st.h"
145 #include "core/or/or_connection_st.h"
146 #include "core/or/port_cfg_st.h"
147 #include "feature/nodelist/routerinfo_st.h"
148 #include "core/or/socks_request_st.h"
149
150 #include "core/or/congestion_control_flow.h"
151
152 /**
153 * On Windows and Linux we cannot reliably bind() a socket to an
154 * address and port if: 1) There's already a socket bound to wildcard
155 * address (0.0.0.0 or ::) with the same port; 2) We try to bind()
156 * to wildcard address and there's another socket bound to a
157 * specific address and the same port.
158 *
159 * To address this problem on these two platforms we implement a
160 * routine that:
161 * 1) Checks if first attempt to bind() a new socket failed with
162 * EADDRINUSE.
163 * 2) If so, it will close the appropriate old listener connection and
164 * 3) Attempts bind()'ing the new listener socket again.
165 *
166 * Just to be safe, we are enabling listener rebind code on all platforms,
167 * to account for unexpected cases where it may be needed.
168 */
169 #define ENABLE_LISTENER_REBIND
170
171 static connection_t *connection_listener_new(
172 const struct sockaddr *listensockaddr,
173 socklen_t listensocklen, int type,
174 const char *address,
175 const port_cfg_t *portcfg,
176 int *addr_in_use);
177 static connection_t *connection_listener_new_for_port(
178 const port_cfg_t *port,
179 int *defer, int *addr_in_use);
180 static void connection_init(time_t now, connection_t *conn, int type,
181 int socket_family);
182 static int connection_handle_listener_read(connection_t *conn, int new_type);
183 static int connection_finished_flushing(connection_t *conn);
184 static int connection_flushed_some(connection_t *conn);
185 static int connection_finished_connecting(connection_t *conn);
186 static int connection_reached_eof(connection_t *conn);
187 static int connection_buf_read_from_socket(connection_t *conn,
188 ssize_t *max_to_read,
189 int *socket_error);
190 static int connection_process_inbuf(connection_t *conn, int package_partial);
191 static void client_check_address_changed(tor_socket_t sock);
192 static void set_constrained_socket_buffers(tor_socket_t sock, int size);
193
194 static const char *connection_proxy_state_to_string(int state);
195 static int connection_read_https_proxy_response(connection_t *conn);
196 static void connection_send_socks5_connect(connection_t *conn);
197 static const char *proxy_type_to_string(int proxy_type);
198 static int conn_get_proxy_type(const connection_t *conn);
199 const tor_addr_t *conn_get_outbound_address(sa_family_t family,
200 const or_options_t *options, unsigned int conn_type);
201 static void reenable_blocked_connection_init(const or_options_t *options);
202 static void reenable_blocked_connection_schedule(void);
203
204 /** The last addresses that our network interface seemed to have been
205 * binding to. We use this as one way to detect when our IP changes.
206 *
207 * XXXX+ We should really use the entire list of interfaces here.
208 **/
209 static tor_addr_t *last_interface_ipv4 = NULL;
210 /* DOCDOC last_interface_ipv6 */
211 static tor_addr_t *last_interface_ipv6 = NULL;
212 /** A list of tor_addr_t for addresses we've used in outgoing connections.
213 * Used to detect IP address changes. */
214 static smartlist_t *outgoing_addrs = NULL;
215
216 #define CASE_ANY_LISTENER_TYPE \
217 case CONN_TYPE_OR_LISTENER: \
218 case CONN_TYPE_EXT_OR_LISTENER: \
219 case CONN_TYPE_AP_LISTENER: \
220 case CONN_TYPE_DIR_LISTENER: \
221 case CONN_TYPE_CONTROL_LISTENER: \
222 case CONN_TYPE_AP_TRANS_LISTENER: \
223 case CONN_TYPE_AP_NATD_LISTENER: \
224 case CONN_TYPE_AP_DNS_LISTENER: \
225 case CONN_TYPE_AP_HTTP_CONNECT_LISTENER: \
226 case CONN_TYPE_METRICS_LISTENER
227
228 /**************************************************************/
229
230 /**
231 * Cast a `connection_t *` to a `listener_connection_t *`.
232 *
233 * Exit with an assertion failure if the input is not a
234 * `listener_connection_t`.
235 **/
236 listener_connection_t *
TO_LISTENER_CONN(connection_t * c)237 TO_LISTENER_CONN(connection_t *c)
238 {
239 tor_assert(c->magic == LISTENER_CONNECTION_MAGIC);
240 return DOWNCAST(listener_connection_t, c);
241 }
242
243 /**
244 * Cast a `const connection_t *` to a `const listener_connection_t *`.
245 *
246 * Exit with an assertion failure if the input is not a
247 * `listener_connection_t`.
248 **/
249 const listener_connection_t *
CONST_TO_LISTENER_CONN(const connection_t * c)250 CONST_TO_LISTENER_CONN(const connection_t *c)
251 {
252 return TO_LISTENER_CONN((connection_t *)c);
253 }
254
255 size_t
connection_get_inbuf_len(const connection_t * conn)256 connection_get_inbuf_len(const connection_t *conn)
257 {
258 return conn->inbuf ? buf_datalen(conn->inbuf) : 0;
259 }
260
261 size_t
connection_get_outbuf_len(const connection_t * conn)262 connection_get_outbuf_len(const connection_t *conn)
263 {
264 return conn->outbuf ? buf_datalen(conn->outbuf) : 0;
265 }
266
267 /**
268 * Return the human-readable name for the connection type <b>type</b>
269 */
270 const char *
conn_type_to_string(int type)271 conn_type_to_string(int type)
272 {
273 static char buf[64];
274 switch (type) {
275 case CONN_TYPE_OR_LISTENER: return "OR listener";
276 case CONN_TYPE_OR: return "OR";
277 case CONN_TYPE_EXIT: return "Exit";
278 case CONN_TYPE_AP_LISTENER: return "Socks listener";
279 case CONN_TYPE_AP_TRANS_LISTENER:
280 return "Transparent pf/netfilter listener";
281 case CONN_TYPE_AP_NATD_LISTENER: return "Transparent natd listener";
282 case CONN_TYPE_AP_DNS_LISTENER: return "DNS listener";
283 case CONN_TYPE_AP: return "Socks";
284 case CONN_TYPE_DIR_LISTENER: return "Directory listener";
285 case CONN_TYPE_DIR: return "Directory";
286 case CONN_TYPE_CONTROL_LISTENER: return "Control listener";
287 case CONN_TYPE_CONTROL: return "Control";
288 case CONN_TYPE_EXT_OR: return "Extended OR";
289 case CONN_TYPE_EXT_OR_LISTENER: return "Extended OR listener";
290 case CONN_TYPE_AP_HTTP_CONNECT_LISTENER: return "HTTP tunnel listener";
291 case CONN_TYPE_METRICS_LISTENER: return "Metrics listener";
292 case CONN_TYPE_METRICS: return "Metrics";
293 default:
294 log_warn(LD_BUG, "unknown connection type %d", type);
295 tor_snprintf(buf, sizeof(buf), "unknown [%d]", type);
296 return buf;
297 }
298 }
299
300 /**
301 * Return the human-readable name for the connection state <b>state</b>
302 * for the connection type <b>type</b>
303 */
304 const char *
conn_state_to_string(int type,int state)305 conn_state_to_string(int type, int state)
306 {
307 static char buf[96];
308 switch (type) {
309 CASE_ANY_LISTENER_TYPE:
310 if (state == LISTENER_STATE_READY)
311 return "ready";
312 break;
313 case CONN_TYPE_OR:
314 switch (state) {
315 case OR_CONN_STATE_CONNECTING: return "connect()ing";
316 case OR_CONN_STATE_PROXY_HANDSHAKING: return "handshaking (proxy)";
317 case OR_CONN_STATE_TLS_HANDSHAKING: return "handshaking (TLS)";
318 case OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING:
319 return "renegotiating (TLS, v2 handshake)";
320 case OR_CONN_STATE_TLS_SERVER_RENEGOTIATING:
321 return "waiting for renegotiation or V3 handshake";
322 case OR_CONN_STATE_OR_HANDSHAKING_V2:
323 return "handshaking (Tor, v2 handshake)";
324 case OR_CONN_STATE_OR_HANDSHAKING_V3:
325 return "handshaking (Tor, v3 handshake)";
326 case OR_CONN_STATE_OPEN: return "open";
327 }
328 break;
329 case CONN_TYPE_EXT_OR:
330 switch (state) {
331 case EXT_OR_CONN_STATE_AUTH_WAIT_AUTH_TYPE:
332 return "waiting for authentication type";
333 case EXT_OR_CONN_STATE_AUTH_WAIT_CLIENT_NONCE:
334 return "waiting for client nonce";
335 case EXT_OR_CONN_STATE_AUTH_WAIT_CLIENT_HASH:
336 return "waiting for client hash";
337 case EXT_OR_CONN_STATE_OPEN: return "open";
338 case EXT_OR_CONN_STATE_FLUSHING: return "flushing final OKAY";
339 }
340 break;
341 case CONN_TYPE_EXIT:
342 switch (state) {
343 case EXIT_CONN_STATE_RESOLVING: return "waiting for dest info";
344 case EXIT_CONN_STATE_CONNECTING: return "connecting";
345 case EXIT_CONN_STATE_OPEN: return "open";
346 case EXIT_CONN_STATE_RESOLVEFAILED: return "resolve failed";
347 }
348 break;
349 case CONN_TYPE_AP:
350 switch (state) {
351 case AP_CONN_STATE_SOCKS_WAIT: return "waiting for socks info";
352 case AP_CONN_STATE_NATD_WAIT: return "waiting for natd dest info";
353 case AP_CONN_STATE_RENDDESC_WAIT: return "waiting for rendezvous desc";
354 case AP_CONN_STATE_CONTROLLER_WAIT: return "waiting for controller";
355 case AP_CONN_STATE_CIRCUIT_WAIT: return "waiting for circuit";
356 case AP_CONN_STATE_CONNECT_WAIT: return "waiting for connect response";
357 case AP_CONN_STATE_RESOLVE_WAIT: return "waiting for resolve response";
358 case AP_CONN_STATE_OPEN: return "open";
359 }
360 break;
361 case CONN_TYPE_DIR:
362 switch (state) {
363 case DIR_CONN_STATE_CONNECTING: return "connecting";
364 case DIR_CONN_STATE_CLIENT_SENDING: return "client sending";
365 case DIR_CONN_STATE_CLIENT_READING: return "client reading";
366 case DIR_CONN_STATE_CLIENT_FINISHED: return "client finished";
367 case DIR_CONN_STATE_SERVER_COMMAND_WAIT: return "waiting for command";
368 case DIR_CONN_STATE_SERVER_WRITING: return "writing";
369 }
370 break;
371 case CONN_TYPE_CONTROL:
372 switch (state) {
373 case CONTROL_CONN_STATE_OPEN: return "open (protocol v1)";
374 case CONTROL_CONN_STATE_NEEDAUTH:
375 return "waiting for authentication (protocol v1)";
376 }
377 break;
378 }
379
380 if (state == 0) {
381 return "uninitialized";
382 }
383
384 log_warn(LD_BUG, "unknown connection state %d (type %d)", state, type);
385 tor_snprintf(buf, sizeof(buf),
386 "unknown state [%d] on unknown [%s] connection",
387 state, conn_type_to_string(type));
388 tor_assert_nonfatal_unreached_once();
389 return buf;
390 }
391
392 /**
393 * Helper: describe the peer or address of connection @a conn in a
394 * human-readable manner.
395 *
396 * Returns a pointer to a static buffer; future calls to
397 * connection_describe_peer_internal() will invalidate this buffer.
398 *
399 * If <b>include_preposition</b> is true, include a preposition before the
400 * peer address.
401 *
402 * Nobody should parse the output of this function; it can and will change in
403 * future versions of tor.
404 **/
405 static const char *
connection_describe_peer_internal(const connection_t * conn,bool include_preposition)406 connection_describe_peer_internal(const connection_t *conn,
407 bool include_preposition)
408 {
409 IF_BUG_ONCE(!conn) {
410 return "null peer";
411 }
412
413 static char peer_buf[256];
414 const tor_addr_t *addr = &conn->addr;
415 const char *address = NULL;
416 const char *prep;
417 bool scrub = false;
418 char extra_buf[128];
419 extra_buf[0] = 0;
420
421 /* First, figure out the preposition to use */
422 switch (conn->type) {
423 CASE_ANY_LISTENER_TYPE:
424 prep = "on";
425 break;
426 case CONN_TYPE_EXIT:
427 prep = "to";
428 break;
429 case CONN_TYPE_CONTROL:
430 case CONN_TYPE_AP:
431 case CONN_TYPE_EXT_OR:
432 prep = "from";
433 break;
434 default:
435 prep = "with";
436 break;
437 }
438
439 /* Now figure out the address. */
440 if (conn->socket_family == AF_UNIX) {
441 /* For unix sockets, we always use the `address` string. */
442 address = conn->address ? conn->address : "unix socket";
443 } else if (conn->type == CONN_TYPE_OR) {
444 /* For OR connections, we have a lot to do. */
445 const or_connection_t *or_conn = CONST_TO_OR_CONN(conn);
446 /* We report the IDs we're talking to... */
447 if (fast_digest_is_zero(or_conn->identity_digest)) {
448 // This could be a client, so scrub it. No identity to report.
449 scrub = true;
450 } else {
451 const ed25519_public_key_t *ed_id =
452 connection_or_get_alleged_ed25519_id(or_conn);
453 char ed_id_buf[ED25519_BASE64_LEN+1];
454 char rsa_id_buf[HEX_DIGEST_LEN+1];
455 if (ed_id) {
456 ed25519_public_to_base64(ed_id_buf, ed_id);
457 } else {
458 strlcpy(ed_id_buf, "<none>", sizeof(ed_id_buf));
459 }
460 base16_encode(rsa_id_buf, sizeof(rsa_id_buf),
461 or_conn->identity_digest, DIGEST_LEN);
462 tor_snprintf(extra_buf, sizeof(extra_buf),
463 " ID=%s RSA_ID=%s", ed_id_buf, rsa_id_buf);
464 }
465 if (! scrub && (! tor_addr_eq(addr, &or_conn->canonical_orport.addr) ||
466 conn->port != or_conn->canonical_orport.port)) {
467 /* We report canonical address, if it's different */
468 char canonical_addr_buf[TOR_ADDR_BUF_LEN];
469 if (tor_addr_to_str(canonical_addr_buf, &or_conn->canonical_orport.addr,
470 sizeof(canonical_addr_buf), 1)) {
471 tor_snprintf(extra_buf+strlen(extra_buf),
472 sizeof(extra_buf)-strlen(extra_buf),
473 " canonical_addr=%s:%"PRIu16,
474 canonical_addr_buf,
475 or_conn->canonical_orport.port);
476 }
477 }
478 } else if (conn->type == CONN_TYPE_EXIT) {
479 scrub = true; /* This is a client's request; scrub it with SafeLogging. */
480 if (tor_addr_is_null(addr)) {
481 address = conn->address;
482 strlcpy(extra_buf, " (DNS lookup pending)", sizeof(extra_buf));
483 }
484 }
485
486 char addr_buf[TOR_ADDR_BUF_LEN];
487 if (address == NULL) {
488 if (tor_addr_family(addr) == 0) {
489 address = "<unset>";
490 } else {
491 address = tor_addr_to_str(addr_buf, addr, sizeof(addr_buf), 1);
492 if (!address) {
493 address = "<can't format!>";
494 tor_assert_nonfatal_unreached_once();
495 }
496 }
497 }
498
499 char portbuf[7];
500 portbuf[0]=0;
501 if (scrub && get_options()->SafeLogging_ != SAFELOG_SCRUB_NONE) {
502 address = "[scrubbed]";
503 } else {
504 /* Only set the port if we're not scrubbing the address. */
505 if (conn->port != 0) {
506 tor_snprintf(portbuf, sizeof(portbuf), ":%d", conn->port);
507 }
508 }
509
510 const char *sp = include_preposition ? " " : "";
511 if (! include_preposition)
512 prep = "";
513
514 tor_snprintf(peer_buf, sizeof(peer_buf),
515 "%s%s%s%s%s", prep, sp, address, portbuf, extra_buf);
516 return peer_buf;
517 }
518
519 /**
520 * Describe the peer or address of connection @a conn in a
521 * human-readable manner.
522 *
523 * Returns a pointer to a static buffer; future calls to
524 * connection_describe_peer() or connection_describe() will invalidate this
525 * buffer.
526 *
527 * Nobody should parse the output of this function; it can and will change in
528 * future versions of tor.
529 **/
530 const char *
connection_describe_peer(const connection_t * conn)531 connection_describe_peer(const connection_t *conn)
532 {
533 return connection_describe_peer_internal(conn, false);
534 }
535
536 /**
537 * Describe a connection for logging purposes.
538 *
539 * Returns a pointer to a static buffer; future calls to connection_describe()
540 * will invalidate this buffer.
541 *
542 * Nobody should parse the output of this function; it can and will change in
543 * future versions of tor.
544 **/
545 const char *
connection_describe(const connection_t * conn)546 connection_describe(const connection_t *conn)
547 {
548 IF_BUG_ONCE(!conn) {
549 return "null connection";
550 }
551 static char desc_buf[256];
552 const char *peer = connection_describe_peer_internal(conn, true);
553 tor_snprintf(desc_buf, sizeof(desc_buf),
554 "%s connection (%s) %s",
555 conn_type_to_string(conn->type),
556 conn_state_to_string(conn->type, conn->state),
557 peer);
558 return desc_buf;
559 }
560
561 /** Allocate and return a new dir_connection_t, initialized as by
562 * connection_init(). */
563 dir_connection_t *
dir_connection_new(int socket_family)564 dir_connection_new(int socket_family)
565 {
566 dir_connection_t *dir_conn = tor_malloc_zero(sizeof(dir_connection_t));
567 connection_init(time(NULL), TO_CONN(dir_conn), CONN_TYPE_DIR, socket_family);
568 return dir_conn;
569 }
570
571 /** Allocate and return a new or_connection_t, initialized as by
572 * connection_init().
573 *
574 * Initialize active_circuit_pqueue.
575 *
576 * Set active_circuit_pqueue_last_recalibrated to current cell_ewma tick.
577 */
578 or_connection_t *
or_connection_new(int type,int socket_family)579 or_connection_new(int type, int socket_family)
580 {
581 or_connection_t *or_conn = tor_malloc_zero(sizeof(or_connection_t));
582 time_t now = time(NULL);
583 tor_assert(type == CONN_TYPE_OR || type == CONN_TYPE_EXT_OR);
584 connection_init(now, TO_CONN(or_conn), type, socket_family);
585
586 tor_addr_make_unspec(&or_conn->canonical_orport.addr);
587 connection_or_set_canonical(or_conn, 0);
588
589 if (type == CONN_TYPE_EXT_OR) {
590 /* If we aren't told an address for this connection, we should
591 * presume it isn't local, and should be rate-limited. */
592 TO_CONN(or_conn)->always_rate_limit_as_remote = 1;
593 connection_or_set_ext_or_identifier(or_conn);
594 }
595
596 return or_conn;
597 }
598
599 /** Allocate and return a new entry_connection_t, initialized as by
600 * connection_init().
601 *
602 * Allocate space to store the socks_request.
603 */
604 entry_connection_t *
entry_connection_new(int type,int socket_family)605 entry_connection_new(int type, int socket_family)
606 {
607 entry_connection_t *entry_conn = tor_malloc_zero(sizeof(entry_connection_t));
608 tor_assert(type == CONN_TYPE_AP);
609 connection_init(time(NULL), ENTRY_TO_CONN(entry_conn), type, socket_family);
610 entry_conn->socks_request = socks_request_new();
611 /* If this is coming from a listener, we'll set it up based on the listener
612 * in a little while. Otherwise, we're doing this as a linked connection
613 * of some kind, and we should set it up here based on the socket family */
614 if (socket_family == AF_INET)
615 entry_conn->entry_cfg.ipv4_traffic = 1;
616 else if (socket_family == AF_INET6)
617 entry_conn->entry_cfg.ipv6_traffic = 1;
618
619 /* Initialize the read token bucket to the maximum value which is the same as
620 * no rate limiting. */
621 token_bucket_rw_init(&ENTRY_TO_EDGE_CONN(entry_conn)->bucket, INT32_MAX,
622 INT32_MAX, monotime_coarse_get_stamp());
623 return entry_conn;
624 }
625
626 /** Allocate and return a new edge_connection_t, initialized as by
627 * connection_init(). */
628 edge_connection_t *
edge_connection_new(int type,int socket_family)629 edge_connection_new(int type, int socket_family)
630 {
631 edge_connection_t *edge_conn = tor_malloc_zero(sizeof(edge_connection_t));
632 tor_assert(type == CONN_TYPE_EXIT);
633 connection_init(time(NULL), TO_CONN(edge_conn), type, socket_family);
634 /* Initialize the read token bucket to the maximum value which is the same as
635 * no rate limiting. */
636 token_bucket_rw_init(&edge_conn->bucket, INT32_MAX, INT32_MAX,
637 monotime_coarse_get_stamp());
638 return edge_conn;
639 }
640
641 /** Allocate and return a new control_connection_t, initialized as by
642 * connection_init(). */
643 control_connection_t *
control_connection_new(int socket_family)644 control_connection_new(int socket_family)
645 {
646 control_connection_t *control_conn =
647 tor_malloc_zero(sizeof(control_connection_t));
648 connection_init(time(NULL),
649 TO_CONN(control_conn), CONN_TYPE_CONTROL, socket_family);
650 return control_conn;
651 }
652
653 /** Allocate and return a new listener_connection_t, initialized as by
654 * connection_init(). */
655 listener_connection_t *
listener_connection_new(int type,int socket_family)656 listener_connection_new(int type, int socket_family)
657 {
658 listener_connection_t *listener_conn =
659 tor_malloc_zero(sizeof(listener_connection_t));
660 connection_init(time(NULL), TO_CONN(listener_conn), type, socket_family);
661 return listener_conn;
662 }
663
664 /** Allocate, initialize, and return a new connection_t subtype of <b>type</b>
665 * to make or receive connections of address family <b>socket_family</b>. The
666 * type should be one of the CONN_TYPE_* constants. */
667 connection_t *
connection_new(int type,int socket_family)668 connection_new(int type, int socket_family)
669 {
670 switch (type) {
671 case CONN_TYPE_OR:
672 case CONN_TYPE_EXT_OR:
673 return TO_CONN(or_connection_new(type, socket_family));
674
675 case CONN_TYPE_EXIT:
676 return TO_CONN(edge_connection_new(type, socket_family));
677
678 case CONN_TYPE_AP:
679 return ENTRY_TO_CONN(entry_connection_new(type, socket_family));
680
681 case CONN_TYPE_DIR:
682 return TO_CONN(dir_connection_new(socket_family));
683
684 case CONN_TYPE_CONTROL:
685 return TO_CONN(control_connection_new(socket_family));
686
687 CASE_ANY_LISTENER_TYPE:
688 return TO_CONN(listener_connection_new(type, socket_family));
689
690 default: {
691 connection_t *conn = tor_malloc_zero(sizeof(connection_t));
692 connection_init(time(NULL), conn, type, socket_family);
693 return conn;
694 }
695 }
696 }
697
698 /** Initializes conn. (you must call connection_add() to link it into the main
699 * array).
700 *
701 * Set conn-\>magic to the correct value.
702 *
703 * Set conn-\>type to <b>type</b>. Set conn-\>s and conn-\>conn_array_index to
704 * -1 to signify they are not yet assigned.
705 *
706 * Initialize conn's timestamps to now.
707 */
708 static void
connection_init(time_t now,connection_t * conn,int type,int socket_family)709 connection_init(time_t now, connection_t *conn, int type, int socket_family)
710 {
711 static uint64_t n_connections_allocated = 1;
712
713 switch (type) {
714 case CONN_TYPE_OR:
715 case CONN_TYPE_EXT_OR:
716 conn->magic = OR_CONNECTION_MAGIC;
717 break;
718 case CONN_TYPE_EXIT:
719 conn->magic = EDGE_CONNECTION_MAGIC;
720 break;
721 case CONN_TYPE_AP:
722 conn->magic = ENTRY_CONNECTION_MAGIC;
723 break;
724 case CONN_TYPE_DIR:
725 conn->magic = DIR_CONNECTION_MAGIC;
726 break;
727 case CONN_TYPE_CONTROL:
728 conn->magic = CONTROL_CONNECTION_MAGIC;
729 break;
730 CASE_ANY_LISTENER_TYPE:
731 conn->magic = LISTENER_CONNECTION_MAGIC;
732 break;
733 default:
734 conn->magic = BASE_CONNECTION_MAGIC;
735 break;
736 }
737
738 conn->s = TOR_INVALID_SOCKET; /* give it a default of 'not used' */
739 conn->conn_array_index = -1; /* also default to 'not used' */
740 conn->global_identifier = n_connections_allocated++;
741
742 conn->type = type;
743 conn->socket_family = socket_family;
744 if (!connection_is_listener(conn)) {
745 /* listeners never use their buf */
746 conn->inbuf = buf_new();
747 conn->outbuf = buf_new();
748 }
749
750 conn->timestamp_created = now;
751 conn->timestamp_last_read_allowed = now;
752 conn->timestamp_last_write_allowed = now;
753 }
754
755 /** Create a link between <b>conn_a</b> and <b>conn_b</b>. */
756 void
connection_link_connections(connection_t * conn_a,connection_t * conn_b)757 connection_link_connections(connection_t *conn_a, connection_t *conn_b)
758 {
759 tor_assert(! SOCKET_OK(conn_a->s));
760 tor_assert(! SOCKET_OK(conn_b->s));
761
762 conn_a->linked = 1;
763 conn_b->linked = 1;
764 conn_a->linked_conn = conn_b;
765 conn_b->linked_conn = conn_a;
766 }
767
768 /** Return true iff the provided connection listener type supports AF_UNIX
769 * sockets. */
770 int
conn_listener_type_supports_af_unix(int type)771 conn_listener_type_supports_af_unix(int type)
772 {
773 /* For now only control ports or SOCKS ports can be Unix domain sockets
774 * and listeners at the same time */
775 switch (type) {
776 case CONN_TYPE_CONTROL_LISTENER:
777 case CONN_TYPE_AP_LISTENER:
778 return 1;
779 default:
780 return 0;
781 }
782 }
783
784 /** Deallocate memory used by <b>conn</b>. Deallocate its buffers if
785 * necessary, close its socket if necessary, and mark the directory as dirty
786 * if <b>conn</b> is an OR or OP connection.
787 */
788 STATIC void
connection_free_minimal(connection_t * conn)789 connection_free_minimal(connection_t *conn)
790 {
791 void *mem;
792 size_t memlen;
793 if (!conn)
794 return;
795
796 switch (conn->type) {
797 case CONN_TYPE_OR:
798 case CONN_TYPE_EXT_OR:
799 tor_assert(conn->magic == OR_CONNECTION_MAGIC);
800 mem = TO_OR_CONN(conn);
801 memlen = sizeof(or_connection_t);
802 break;
803 case CONN_TYPE_AP:
804 tor_assert(conn->magic == ENTRY_CONNECTION_MAGIC);
805 mem = TO_ENTRY_CONN(conn);
806 memlen = sizeof(entry_connection_t);
807 break;
808 case CONN_TYPE_EXIT:
809 tor_assert(conn->magic == EDGE_CONNECTION_MAGIC);
810 mem = TO_EDGE_CONN(conn);
811 memlen = sizeof(edge_connection_t);
812 break;
813 case CONN_TYPE_DIR:
814 tor_assert(conn->magic == DIR_CONNECTION_MAGIC);
815 mem = TO_DIR_CONN(conn);
816 memlen = sizeof(dir_connection_t);
817 break;
818 case CONN_TYPE_CONTROL:
819 tor_assert(conn->magic == CONTROL_CONNECTION_MAGIC);
820 mem = TO_CONTROL_CONN(conn);
821 memlen = sizeof(control_connection_t);
822 break;
823 CASE_ANY_LISTENER_TYPE:
824 tor_assert(conn->magic == LISTENER_CONNECTION_MAGIC);
825 mem = TO_LISTENER_CONN(conn);
826 memlen = sizeof(listener_connection_t);
827 break;
828 default:
829 tor_assert(conn->magic == BASE_CONNECTION_MAGIC);
830 mem = conn;
831 memlen = sizeof(connection_t);
832 break;
833 }
834
835 if (conn->linked) {
836 log_info(LD_GENERAL, "Freeing linked %s connection [%s] with %d "
837 "bytes on inbuf, %d on outbuf.",
838 conn_type_to_string(conn->type),
839 conn_state_to_string(conn->type, conn->state),
840 (int)connection_get_inbuf_len(conn),
841 (int)connection_get_outbuf_len(conn));
842 }
843
844 if (!connection_is_listener(conn)) {
845 buf_free(conn->inbuf);
846 buf_free(conn->outbuf);
847 } else {
848 if (conn->socket_family == AF_UNIX) {
849 /* For now only control and SOCKS ports can be Unix domain sockets
850 * and listeners at the same time */
851 tor_assert(conn_listener_type_supports_af_unix(conn->type));
852
853 if (unlink(conn->address) < 0 && errno != ENOENT) {
854 log_warn(LD_NET, "Could not unlink %s: %s", conn->address,
855 strerror(errno));
856 }
857 }
858 }
859
860 tor_str_wipe_and_free(conn->address);
861
862 if (connection_speaks_cells(conn)) {
863 or_connection_t *or_conn = TO_OR_CONN(conn);
864 if (or_conn->tls) {
865 if (! SOCKET_OK(conn->s)) {
866 /* The socket has been closed by somebody else; we must tell the
867 * TLS object not to close it. */
868 tor_tls_release_socket(or_conn->tls);
869 } else {
870 /* The tor_tls_free() call below will close the socket; we must tell
871 * the code below not to close it a second time. */
872 tor_release_socket_ownership(conn->s);
873 conn->s = TOR_INVALID_SOCKET;
874 }
875 tor_tls_free(or_conn->tls);
876 or_conn->tls = NULL;
877 }
878 or_handshake_state_free(or_conn->handshake_state);
879 or_conn->handshake_state = NULL;
880 tor_str_wipe_and_free(or_conn->nickname);
881 if (or_conn->chan) {
882 /* Owww, this shouldn't happen, but... */
883 channel_t *base_chan = TLS_CHAN_TO_BASE(or_conn->chan);
884 tor_assert(base_chan);
885 log_info(LD_CHANNEL,
886 "Freeing orconn at %p, saw channel %p with ID "
887 "%"PRIu64 " left un-NULLed",
888 or_conn, base_chan,
889 base_chan->global_identifier);
890 if (!CHANNEL_FINISHED(base_chan)) {
891 channel_close_for_error(base_chan);
892 }
893
894 or_conn->chan->conn = NULL;
895 or_conn->chan = NULL;
896 }
897 }
898 if (conn->type == CONN_TYPE_AP) {
899 entry_connection_t *entry_conn = TO_ENTRY_CONN(conn);
900 tor_str_wipe_and_free(entry_conn->chosen_exit_name);
901 tor_str_wipe_and_free(entry_conn->original_dest_address);
902 if (entry_conn->socks_request)
903 socks_request_free(entry_conn->socks_request);
904 if (entry_conn->pending_optimistic_data) {
905 buf_free(entry_conn->pending_optimistic_data);
906 }
907 if (entry_conn->sending_optimistic_data) {
908 buf_free(entry_conn->sending_optimistic_data);
909 }
910 }
911 if (CONN_IS_EDGE(conn)) {
912 hs_ident_edge_conn_free(TO_EDGE_CONN(conn)->hs_ident);
913 }
914 if (conn->type == CONN_TYPE_CONTROL) {
915 control_connection_t *control_conn = TO_CONTROL_CONN(conn);
916 tor_free(control_conn->safecookie_client_hash);
917 tor_free(control_conn->incoming_cmd);
918 tor_free(control_conn->current_cmd);
919 if (control_conn->ephemeral_onion_services) {
920 SMARTLIST_FOREACH(control_conn->ephemeral_onion_services, char *, cp, {
921 memwipe(cp, 0, strlen(cp));
922 tor_free(cp);
923 });
924 smartlist_free(control_conn->ephemeral_onion_services);
925 }
926 }
927
928 /* Probably already freed by connection_free. */
929 tor_event_free(conn->read_event);
930 tor_event_free(conn->write_event);
931 conn->read_event = conn->write_event = NULL;
932
933 if (conn->type == CONN_TYPE_DIR) {
934 dir_connection_t *dir_conn = TO_DIR_CONN(conn);
935 tor_free(dir_conn->requested_resource);
936
937 tor_compress_free(dir_conn->compress_state);
938 dir_conn_clear_spool(dir_conn);
939
940 hs_ident_dir_conn_free(dir_conn->hs_ident);
941 if (dir_conn->guard_state) {
942 /* Cancel before freeing, if it's still there. */
943 entry_guard_cancel(&dir_conn->guard_state);
944 }
945 circuit_guard_state_free(dir_conn->guard_state);
946 }
947
948 if (SOCKET_OK(conn->s)) {
949 log_debug(LD_NET,"closing fd %d.",(int)conn->s);
950 tor_close_socket(conn->s);
951 conn->s = TOR_INVALID_SOCKET;
952 }
953
954 if (conn->type == CONN_TYPE_OR &&
955 !tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest)) {
956 log_warn(LD_BUG, "called on OR conn with non-zeroed identity_digest");
957 connection_or_clear_identity(TO_OR_CONN(conn));
958 }
959 if (conn->type == CONN_TYPE_OR || conn->type == CONN_TYPE_EXT_OR) {
960 tor_free(TO_OR_CONN(conn)->ext_or_conn_id);
961 tor_free(TO_OR_CONN(conn)->ext_or_auth_correct_client_hash);
962 tor_free(TO_OR_CONN(conn)->ext_or_transport);
963 }
964
965 memwipe(mem, 0xCC, memlen); /* poison memory */
966 tor_free(mem);
967 }
968
969 /** Make sure <b>conn</b> isn't in any of the global conn lists; then free it.
970 */
971 MOCK_IMPL(void,
972 connection_free_,(connection_t *conn))
973 {
974 if (!conn)
975 return;
976 tor_assert(!connection_is_on_closeable_list(conn));
977 tor_assert(!connection_in_array(conn));
978 if (BUG(conn->linked_conn)) {
979 conn->linked_conn->linked_conn = NULL;
980 if (! conn->linked_conn->marked_for_close &&
981 conn->linked_conn->reading_from_linked_conn)
982 connection_start_reading(conn->linked_conn);
983 conn->linked_conn = NULL;
984 }
985 if (connection_speaks_cells(conn)) {
986 if (!tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest)) {
987 connection_or_clear_identity(TO_OR_CONN(conn));
988 }
989 }
990 if (conn->type == CONN_TYPE_CONTROL) {
991 connection_control_closed(TO_CONTROL_CONN(conn));
992 }
993 #if 1
994 /* DEBUGGING */
995 if (conn->type == CONN_TYPE_AP) {
996 connection_ap_warn_and_unmark_if_pending_circ(TO_ENTRY_CONN(conn),
997 "connection_free");
998 }
999 #endif /* 1 */
1000
1001 /* Notify the circuit creation DoS mitigation subsystem that an OR client
1002 * connection has been closed. And only do that if we track it. */
1003 if (conn->type == CONN_TYPE_OR) {
1004 dos_close_client_conn(TO_OR_CONN(conn));
1005 }
1006
1007 connection_unregister_events(conn);
1008 connection_free_minimal(conn);
1009 }
1010
1011 /**
1012 * Called when we're about to finally unlink and free a connection:
1013 * perform necessary accounting and cleanup
1014 * - Directory conns that failed to fetch a rendezvous descriptor
1015 * need to inform pending rendezvous streams.
1016 * - OR conns need to call rep_hist_note_*() to record status.
1017 * - AP conns need to send a socks reject if necessary.
1018 * - Exit conns need to call connection_dns_remove() if necessary.
1019 * - AP and Exit conns need to send an end cell if they can.
1020 * - DNS conns need to fail any resolves that are pending on them.
1021 * - OR and edge connections need to be unlinked from circuits.
1022 */
1023 void
connection_about_to_close_connection(connection_t * conn)1024 connection_about_to_close_connection(connection_t *conn)
1025 {
1026 tor_assert(conn->marked_for_close);
1027
1028 switch (conn->type) {
1029 case CONN_TYPE_DIR:
1030 connection_dir_about_to_close(TO_DIR_CONN(conn));
1031 break;
1032 case CONN_TYPE_OR:
1033 case CONN_TYPE_EXT_OR:
1034 connection_or_about_to_close(TO_OR_CONN(conn));
1035 break;
1036 case CONN_TYPE_AP:
1037 connection_ap_about_to_close(TO_ENTRY_CONN(conn));
1038 break;
1039 case CONN_TYPE_EXIT:
1040 connection_exit_about_to_close(TO_EDGE_CONN(conn));
1041 break;
1042 }
1043 }
1044
1045 /** Return true iff connection_close_immediate() has been called on this
1046 * connection. */
1047 #define CONN_IS_CLOSED(c) \
1048 ((c)->linked ? ((c)->linked_conn_is_closed) : (! SOCKET_OK(c->s)))
1049
1050 /** Close the underlying socket for <b>conn</b>, so we don't try to
1051 * flush it. Must be used in conjunction with (right before)
1052 * connection_mark_for_close().
1053 */
1054 void
connection_close_immediate(connection_t * conn)1055 connection_close_immediate(connection_t *conn)
1056 {
1057 assert_connection_ok(conn,0);
1058 if (CONN_IS_CLOSED(conn)) {
1059 log_err(LD_BUG,"Attempt to close already-closed connection.");
1060 tor_fragile_assert();
1061 return;
1062 }
1063 if (connection_get_outbuf_len(conn)) {
1064 log_info(LD_NET,"fd %d, type %s, state %s, %"TOR_PRIuSZ" bytes on outbuf.",
1065 (int)conn->s, conn_type_to_string(conn->type),
1066 conn_state_to_string(conn->type, conn->state),
1067 buf_datalen(conn->outbuf));
1068 }
1069
1070 connection_unregister_events(conn);
1071
1072 /* Prevent the event from getting unblocked. */
1073 conn->read_blocked_on_bw = 0;
1074 conn->write_blocked_on_bw = 0;
1075
1076 if (SOCKET_OK(conn->s))
1077 tor_close_socket(conn->s);
1078 conn->s = TOR_INVALID_SOCKET;
1079 if (conn->linked)
1080 conn->linked_conn_is_closed = 1;
1081 if (conn->outbuf)
1082 buf_clear(conn->outbuf);
1083 }
1084
1085 /** Mark <b>conn</b> to be closed next time we loop through
1086 * conn_close_if_marked() in main.c. */
1087 void
connection_mark_for_close_(connection_t * conn,int line,const char * file)1088 connection_mark_for_close_(connection_t *conn, int line, const char *file)
1089 {
1090 assert_connection_ok(conn,0);
1091 tor_assert(line);
1092 tor_assert(line < 1<<16); /* marked_for_close can only fit a uint16_t. */
1093 tor_assert(file);
1094
1095 if (conn->type == CONN_TYPE_OR) {
1096 /*
1097 * An or_connection should have been closed through one of the channel-
1098 * aware functions in connection_or.c. We'll assume this is an error
1099 * close and do that, and log a bug warning.
1100 */
1101 log_warn(LD_CHANNEL | LD_BUG,
1102 "Something tried to close an or_connection_t without going "
1103 "through channels at %s:%d",
1104 file, line);
1105 connection_or_close_for_error(TO_OR_CONN(conn), 0);
1106 } else {
1107 /* Pass it down to the real function */
1108 connection_mark_for_close_internal_(conn, line, file);
1109 }
1110 }
1111
1112 /** Mark <b>conn</b> to be closed next time we loop through
1113 * conn_close_if_marked() in main.c.
1114 *
1115 * This _internal version bypasses the CONN_TYPE_OR checks; this should be
1116 * called when you either are sure that if this is an or_connection_t the
1117 * controlling channel has been notified (e.g. with
1118 * connection_or_notify_error()), or you actually are the
1119 * connection_or_close_for_error() or connection_or_close_normally() function.
1120 * For all other cases, use connection_mark_and_flush() which checks for
1121 * or_connection_t properly, instead. See below.
1122 *
1123 * We want to keep this function simple and quick, since it can be called from
1124 * quite deep in the call chain, and hence it should avoid having side-effects
1125 * that interfere with its callers view of the connection.
1126 */
1127 MOCK_IMPL(void,
1128 connection_mark_for_close_internal_, (connection_t *conn,
1129 int line, const char *file))
1130 {
1131 assert_connection_ok(conn,0);
1132 tor_assert(line);
1133 tor_assert(line < 1<<16); /* marked_for_close can only fit a uint16_t. */
1134 tor_assert(file);
1135
1136 if (conn->marked_for_close) {
1137 log_warn(LD_BUG,"Duplicate call to connection_mark_for_close at %s:%d"
1138 " (first at %s:%d)", file, line, conn->marked_for_close_file,
1139 conn->marked_for_close);
1140 tor_fragile_assert();
1141 return;
1142 }
1143
1144 if (conn->type == CONN_TYPE_OR) {
1145 /*
1146 * Bad news if this happens without telling the controlling channel; do
1147 * this so we can find things that call this wrongly when the asserts hit.
1148 */
1149 log_debug(LD_CHANNEL,
1150 "Calling connection_mark_for_close_internal_() on an OR conn "
1151 "at %s:%d",
1152 file, line);
1153 }
1154
1155 conn->marked_for_close = line;
1156 conn->marked_for_close_file = file;
1157 add_connection_to_closeable_list(conn);
1158
1159 /* in case we're going to be held-open-til-flushed, reset
1160 * the number of seconds since last successful write, so
1161 * we get our whole 15 seconds */
1162 conn->timestamp_last_write_allowed = time(NULL);
1163 }
1164
1165 /** Find each connection that has hold_open_until_flushed set to
1166 * 1 but hasn't written in the past 15 seconds, and set
1167 * hold_open_until_flushed to 0. This means it will get cleaned
1168 * up in the next loop through close_if_marked() in main.c.
1169 */
1170 void
connection_expire_held_open(void)1171 connection_expire_held_open(void)
1172 {
1173 time_t now;
1174 smartlist_t *conns = get_connection_array();
1175
1176 now = time(NULL);
1177
1178 SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn) {
1179 /* If we've been holding the connection open, but we haven't written
1180 * for 15 seconds...
1181 */
1182 if (conn->hold_open_until_flushed) {
1183 tor_assert(conn->marked_for_close);
1184 if (now - conn->timestamp_last_write_allowed >= 15) {
1185 int severity;
1186 if (conn->type == CONN_TYPE_EXIT ||
1187 (conn->type == CONN_TYPE_DIR &&
1188 conn->purpose == DIR_PURPOSE_SERVER))
1189 severity = LOG_INFO;
1190 else
1191 severity = LOG_NOTICE;
1192 log_fn(severity, LD_NET,
1193 "Giving up on marked_for_close conn that's been flushing "
1194 "for 15s (fd %d, type %s, state %s).",
1195 (int)conn->s, conn_type_to_string(conn->type),
1196 conn_state_to_string(conn->type, conn->state));
1197 conn->hold_open_until_flushed = 0;
1198 }
1199 }
1200 } SMARTLIST_FOREACH_END(conn);
1201 }
1202
1203 #if defined(HAVE_SYS_UN_H) || defined(RUNNING_DOXYGEN)
1204 /** Create an AF_UNIX listenaddr struct.
1205 * <b>listenaddress</b> provides the path to the Unix socket.
1206 *
1207 * Eventually <b>listenaddress</b> will also optionally contain user, group,
1208 * and file permissions for the new socket. But not yet. XXX
1209 * Also, since we do not create the socket here the information doesn't help
1210 * here.
1211 *
1212 * If not NULL <b>readable_address</b> will contain a copy of the path part of
1213 * <b>listenaddress</b>.
1214 *
1215 * The listenaddr struct has to be freed by the caller.
1216 */
1217 static struct sockaddr_un *
create_unix_sockaddr(const char * listenaddress,char ** readable_address,socklen_t * len_out)1218 create_unix_sockaddr(const char *listenaddress, char **readable_address,
1219 socklen_t *len_out)
1220 {
1221 struct sockaddr_un *sockaddr = NULL;
1222
1223 sockaddr = tor_malloc_zero(sizeof(struct sockaddr_un));
1224 sockaddr->sun_family = AF_UNIX;
1225 if (strlcpy(sockaddr->sun_path, listenaddress, sizeof(sockaddr->sun_path))
1226 >= sizeof(sockaddr->sun_path)) {
1227 log_warn(LD_CONFIG, "Unix socket path '%s' is too long to fit.",
1228 escaped(listenaddress));
1229 tor_free(sockaddr);
1230 return NULL;
1231 }
1232
1233 if (readable_address)
1234 *readable_address = tor_strdup(listenaddress);
1235
1236 *len_out = sizeof(struct sockaddr_un);
1237 return sockaddr;
1238 }
1239 #else /* !(defined(HAVE_SYS_UN_H) || defined(RUNNING_DOXYGEN)) */
1240 static struct sockaddr *
create_unix_sockaddr(const char * listenaddress,char ** readable_address,socklen_t * len_out)1241 create_unix_sockaddr(const char *listenaddress, char **readable_address,
1242 socklen_t *len_out)
1243 {
1244 (void)listenaddress;
1245 (void)readable_address;
1246 log_fn(LOG_ERR, LD_BUG,
1247 "Unix domain sockets not supported, yet we tried to create one.");
1248 *len_out = 0;
1249 tor_fragile_assert();
1250 return NULL;
1251 }
1252 #endif /* defined(HAVE_SYS_UN_H) || defined(RUNNING_DOXYGEN) */
1253
1254 /**
1255 * A socket failed from resource exhaustion.
1256 *
1257 * Among other actions, warn that an accept or a connect has failed because
1258 * we're running out of TCP sockets we can use on current system. Rate-limit
1259 * these warnings so that we don't spam the log. */
1260 static void
socket_failed_from_resource_exhaustion(void)1261 socket_failed_from_resource_exhaustion(void)
1262 {
1263 /* When we get to this point we know that a socket could not be
1264 * established. However the kernel does not let us know whether the reason is
1265 * because we ran out of TCP source ports, or because we exhausted all the
1266 * FDs on this system, or for any other reason.
1267 *
1268 * For this reason, we are going to use the following heuristic: If our
1269 * system supports a lot of sockets, we will assume that it's a problem of
1270 * TCP port exhaustion. Otherwise, if our system does not support many
1271 * sockets, we will assume that this is because of file descriptor
1272 * exhaustion.
1273 */
1274 if (get_max_sockets() > 65535) {
1275 /* TCP port exhaustion */
1276 rep_hist_note_tcp_exhaustion();
1277 } else {
1278 /* File descriptor exhaustion */
1279 rep_hist_note_overload(OVERLOAD_FD_EXHAUSTED);
1280 }
1281
1282 #define WARN_TOO_MANY_CONNS_INTERVAL (6*60*60)
1283 static ratelim_t last_warned = RATELIM_INIT(WARN_TOO_MANY_CONNS_INTERVAL);
1284 char *m;
1285 if ((m = rate_limit_log(&last_warned, approx_time()))) {
1286 int n_conns = get_n_open_sockets();
1287 log_warn(LD_NET,"Failing because we have %d connections already. Please "
1288 "read doc/TUNING for guidance.%s", n_conns, m);
1289 tor_free(m);
1290 control_event_general_status(LOG_WARN, "TOO_MANY_CONNECTIONS CURRENT=%d",
1291 n_conns);
1292 }
1293 }
1294
1295 #ifdef HAVE_SYS_UN_H
1296
1297 #define UNIX_SOCKET_PURPOSE_CONTROL_SOCKET 0
1298 #define UNIX_SOCKET_PURPOSE_SOCKS_SOCKET 1
1299
1300 /** Check if the purpose isn't one of the ones we know what to do with */
1301
1302 static int
is_valid_unix_socket_purpose(int purpose)1303 is_valid_unix_socket_purpose(int purpose)
1304 {
1305 int valid = 0;
1306
1307 switch (purpose) {
1308 case UNIX_SOCKET_PURPOSE_CONTROL_SOCKET:
1309 case UNIX_SOCKET_PURPOSE_SOCKS_SOCKET:
1310 valid = 1;
1311 break;
1312 }
1313
1314 return valid;
1315 }
1316
1317 /** Return a string description of a unix socket purpose */
1318 static const char *
unix_socket_purpose_to_string(int purpose)1319 unix_socket_purpose_to_string(int purpose)
1320 {
1321 const char *s = "unknown-purpose socket";
1322
1323 switch (purpose) {
1324 case UNIX_SOCKET_PURPOSE_CONTROL_SOCKET:
1325 s = "control socket";
1326 break;
1327 case UNIX_SOCKET_PURPOSE_SOCKS_SOCKET:
1328 s = "SOCKS socket";
1329 break;
1330 }
1331
1332 return s;
1333 }
1334
1335 /** Check whether we should be willing to open an AF_UNIX socket in
1336 * <b>path</b>. Return 0 if we should go ahead and -1 if we shouldn't. */
1337 static int
check_location_for_unix_socket(const or_options_t * options,const char * path,int purpose,const port_cfg_t * port)1338 check_location_for_unix_socket(const or_options_t *options, const char *path,
1339 int purpose, const port_cfg_t *port)
1340 {
1341 int r = -1;
1342 char *p = NULL;
1343
1344 tor_assert(is_valid_unix_socket_purpose(purpose));
1345
1346 p = tor_strdup(path);
1347 cpd_check_t flags = CPD_CHECK_MODE_ONLY;
1348 if (get_parent_directory(p)<0 || p[0] != '/') {
1349 log_warn(LD_GENERAL, "Bad unix socket address '%s'. Tor does not support "
1350 "relative paths for unix sockets.", path);
1351 goto done;
1352 }
1353
1354 if (port->is_world_writable) {
1355 /* World-writable sockets can go anywhere. */
1356 r = 0;
1357 goto done;
1358 }
1359
1360 if (port->is_group_writable) {
1361 flags |= CPD_GROUP_OK;
1362 }
1363
1364 if (port->relax_dirmode_check) {
1365 flags |= CPD_RELAX_DIRMODE_CHECK;
1366 }
1367
1368 if (check_private_dir(p, flags, options->User) < 0) {
1369 char *escpath, *escdir;
1370 escpath = esc_for_log(path);
1371 escdir = esc_for_log(p);
1372 log_warn(LD_GENERAL, "Before Tor can create a %s in %s, the directory "
1373 "%s needs to exist, and to be accessible only by the user%s "
1374 "account that is running Tor. (On some Unix systems, anybody "
1375 "who can list a socket can connect to it, so Tor is being "
1376 "careful.)",
1377 unix_socket_purpose_to_string(purpose), escpath, escdir,
1378 port->is_group_writable ? " and group" : "");
1379 tor_free(escpath);
1380 tor_free(escdir);
1381 goto done;
1382 }
1383
1384 r = 0;
1385 done:
1386 tor_free(p);
1387 return r;
1388 }
1389 #endif /* defined(HAVE_SYS_UN_H) */
1390
1391 /** Tell the TCP stack that it shouldn't wait for a long time after
1392 * <b>sock</b> has closed before reusing its port. Return 0 on success,
1393 * -1 on failure. */
1394 static int
make_socket_reuseable(tor_socket_t sock)1395 make_socket_reuseable(tor_socket_t sock)
1396 {
1397 #ifdef _WIN32
1398 (void) sock;
1399 return 0;
1400 #else
1401 int one=1;
1402
1403 /* REUSEADDR on normal places means you can rebind to the port
1404 * right after somebody else has let it go. But REUSEADDR on win32
1405 * means you can bind to the port _even when somebody else
1406 * already has it bound_. So, don't do that on Win32. */
1407 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*) &one,
1408 (socklen_t)sizeof(one)) == -1) {
1409 return -1;
1410 }
1411 return 0;
1412 #endif /* defined(_WIN32) */
1413 }
1414
1415 #ifdef _WIN32
1416 /** Tell the Windows TCP stack to prevent other applications from receiving
1417 * traffic from tor's open ports. Return 0 on success, -1 on failure. */
1418 static int
make_win32_socket_exclusive(tor_socket_t sock)1419 make_win32_socket_exclusive(tor_socket_t sock)
1420 {
1421 #ifdef SO_EXCLUSIVEADDRUSE
1422 int one=1;
1423
1424 /* Any socket that sets REUSEADDR on win32 can bind to a port _even when
1425 * somebody else already has it bound_, and _even if the original socket
1426 * didn't set REUSEADDR_. Use EXCLUSIVEADDRUSE to prevent this port-stealing
1427 * on win32. */
1428 if (setsockopt(sock, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, (void*) &one,
1429 (socklen_t)sizeof(one))) {
1430 return -1;
1431 }
1432 return 0;
1433 #else /* !defined(SO_EXCLUSIVEADDRUSE) */
1434 (void) sock;
1435 return 0;
1436 #endif /* defined(SO_EXCLUSIVEADDRUSE) */
1437 }
1438 #endif /* defined(_WIN32) */
1439
1440 /** Max backlog to pass to listen. We start at */
1441 static int listen_limit = INT_MAX;
1442
1443 /* Listen on <b>fd</b> with appropriate backlog. Return as for listen. */
1444 static int
tor_listen(tor_socket_t fd)1445 tor_listen(tor_socket_t fd)
1446 {
1447 int r;
1448
1449 if ((r = listen(fd, listen_limit)) < 0) {
1450 if (listen_limit == SOMAXCONN)
1451 return r;
1452 if ((r = listen(fd, SOMAXCONN)) == 0) {
1453 listen_limit = SOMAXCONN;
1454 log_warn(LD_NET, "Setting listen backlog to INT_MAX connections "
1455 "didn't work, but SOMAXCONN did. Lowering backlog limit.");
1456 }
1457 }
1458 return r;
1459 }
1460
1461 /** Bind a new non-blocking socket listening to the socket described
1462 * by <b>listensockaddr</b>.
1463 *
1464 * <b>address</b> is only used for logging purposes and to add the information
1465 * to the conn.
1466 *
1467 * Set <b>addr_in_use</b> to true in case socket binding fails with
1468 * EADDRINUSE.
1469 */
1470 static connection_t *
connection_listener_new(const struct sockaddr * listensockaddr,socklen_t socklen,int type,const char * address,const port_cfg_t * port_cfg,int * addr_in_use)1471 connection_listener_new(const struct sockaddr *listensockaddr,
1472 socklen_t socklen,
1473 int type, const char *address,
1474 const port_cfg_t *port_cfg,
1475 int *addr_in_use)
1476 {
1477 listener_connection_t *lis_conn;
1478 connection_t *conn = NULL;
1479 tor_socket_t s = TOR_INVALID_SOCKET; /* the socket we're going to make */
1480 or_options_t const *options = get_options();
1481 (void) options; /* Windows doesn't use this. */
1482 #if defined(HAVE_PWD_H) && defined(HAVE_SYS_UN_H)
1483 const struct passwd *pw = NULL;
1484 #endif
1485 uint16_t usePort = 0, gotPort = 0;
1486 int start_reading = 0;
1487 static int global_next_session_group = SESSION_GROUP_FIRST_AUTO;
1488 tor_addr_t addr;
1489 int exhaustion = 0;
1490
1491 if (addr_in_use)
1492 *addr_in_use = 0;
1493
1494 if (listensockaddr->sa_family == AF_INET ||
1495 listensockaddr->sa_family == AF_INET6) {
1496 int is_stream = (type != CONN_TYPE_AP_DNS_LISTENER);
1497 if (is_stream)
1498 start_reading = 1;
1499
1500 tor_addr_from_sockaddr(&addr, listensockaddr, &usePort);
1501 log_notice(LD_NET, "Opening %s on %s",
1502 conn_type_to_string(type), fmt_addrport(&addr, usePort));
1503
1504 s = tor_open_socket_nonblocking(tor_addr_family(&addr),
1505 is_stream ? SOCK_STREAM : SOCK_DGRAM,
1506 is_stream ? IPPROTO_TCP: IPPROTO_UDP);
1507 if (!SOCKET_OK(s)) {
1508 int e = tor_socket_errno(s);
1509 if (ERRNO_IS_RESOURCE_LIMIT(e)) {
1510 socket_failed_from_resource_exhaustion();
1511 /*
1512 * We'll call the OOS handler at the error exit, so set the
1513 * exhaustion flag for it.
1514 */
1515 exhaustion = 1;
1516 } else {
1517 log_warn(LD_NET, "Socket creation failed: %s",
1518 tor_socket_strerror(e));
1519 }
1520 goto err;
1521 }
1522
1523 if (make_socket_reuseable(s) < 0) {
1524 log_warn(LD_NET, "Error setting SO_REUSEADDR flag on %s: %s",
1525 conn_type_to_string(type),
1526 tor_socket_strerror(errno));
1527 }
1528
1529 #ifdef _WIN32
1530 if (make_win32_socket_exclusive(s) < 0) {
1531 log_warn(LD_NET, "Error setting SO_EXCLUSIVEADDRUSE flag on %s: %s",
1532 conn_type_to_string(type),
1533 tor_socket_strerror(errno));
1534 }
1535 #endif /* defined(_WIN32) */
1536
1537 #if defined(USE_TRANSPARENT) && defined(IP_TRANSPARENT)
1538 if (options->TransProxyType_parsed == TPT_TPROXY &&
1539 type == CONN_TYPE_AP_TRANS_LISTENER) {
1540 int one = 1;
1541 if (setsockopt(s, SOL_IP, IP_TRANSPARENT, (void*)&one,
1542 (socklen_t)sizeof(one)) < 0) {
1543 const char *extra = "";
1544 int e = tor_socket_errno(s);
1545 if (e == EPERM)
1546 extra = "TransTPROXY requires root privileges or similar"
1547 " capabilities.";
1548 log_warn(LD_NET, "Error setting IP_TRANSPARENT flag: %s.%s",
1549 tor_socket_strerror(e), extra);
1550 }
1551 }
1552 #endif /* defined(USE_TRANSPARENT) && defined(IP_TRANSPARENT) */
1553
1554 #ifdef IPV6_V6ONLY
1555 if (listensockaddr->sa_family == AF_INET6) {
1556 int one = 1;
1557 /* We need to set IPV6_V6ONLY so that this socket can't get used for
1558 * IPv4 connections. */
1559 if (setsockopt(s,IPPROTO_IPV6, IPV6_V6ONLY,
1560 (void*)&one, (socklen_t)sizeof(one)) < 0) {
1561 int e = tor_socket_errno(s);
1562 log_warn(LD_NET, "Error setting IPV6_V6ONLY flag: %s",
1563 tor_socket_strerror(e));
1564 /* Keep going; probably not harmful. */
1565 }
1566 }
1567 #endif /* defined(IPV6_V6ONLY) */
1568
1569 if (bind(s,listensockaddr,socklen) < 0) {
1570 const char *helpfulhint = "";
1571 int e = tor_socket_errno(s);
1572 if (ERRNO_IS_EADDRINUSE(e)) {
1573 helpfulhint = ". Is Tor already running?";
1574 if (addr_in_use)
1575 *addr_in_use = 1;
1576 }
1577 log_warn(LD_NET, "Could not bind to %s:%u: %s%s", address, usePort,
1578 tor_socket_strerror(e), helpfulhint);
1579 goto err;
1580 }
1581
1582 if (is_stream) {
1583 if (tor_listen(s) < 0) {
1584 log_warn(LD_NET, "Could not listen on %s:%u: %s", address, usePort,
1585 tor_socket_strerror(tor_socket_errno(s)));
1586 goto err;
1587 }
1588 }
1589
1590 if (usePort != 0) {
1591 gotPort = usePort;
1592 } else {
1593 tor_addr_t addr2;
1594 struct sockaddr_storage ss;
1595 socklen_t ss_len=sizeof(ss);
1596 if (getsockname(s, (struct sockaddr*)&ss, &ss_len)<0) {
1597 log_warn(LD_NET, "getsockname() couldn't learn address for %s: %s",
1598 conn_type_to_string(type),
1599 tor_socket_strerror(tor_socket_errno(s)));
1600 gotPort = 0;
1601 }
1602 tor_addr_from_sockaddr(&addr2, (struct sockaddr*)&ss, &gotPort);
1603 }
1604 #ifdef HAVE_SYS_UN_H
1605 /*
1606 * AF_UNIX generic setup stuff
1607 */
1608 } else if (listensockaddr->sa_family == AF_UNIX) {
1609 /* We want to start reading for both AF_UNIX cases */
1610 start_reading = 1;
1611
1612 tor_assert(conn_listener_type_supports_af_unix(type));
1613
1614 if (check_location_for_unix_socket(options, address,
1615 (type == CONN_TYPE_CONTROL_LISTENER) ?
1616 UNIX_SOCKET_PURPOSE_CONTROL_SOCKET :
1617 UNIX_SOCKET_PURPOSE_SOCKS_SOCKET, port_cfg) < 0) {
1618 goto err;
1619 }
1620
1621 log_notice(LD_NET, "Opening %s on %s",
1622 conn_type_to_string(type), address);
1623
1624 tor_addr_make_unspec(&addr);
1625
1626 if (unlink(address) < 0 && errno != ENOENT) {
1627 log_warn(LD_NET, "Could not unlink %s: %s", address,
1628 strerror(errno));
1629 goto err;
1630 }
1631
1632 s = tor_open_socket_nonblocking(AF_UNIX, SOCK_STREAM, 0);
1633 if (! SOCKET_OK(s)) {
1634 int e = tor_socket_errno(s);
1635 if (ERRNO_IS_RESOURCE_LIMIT(e)) {
1636 socket_failed_from_resource_exhaustion();
1637 /*
1638 * We'll call the OOS handler at the error exit, so set the
1639 * exhaustion flag for it.
1640 */
1641 exhaustion = 1;
1642 } else {
1643 log_warn(LD_NET,"Socket creation failed: %s.", strerror(e));
1644 }
1645 goto err;
1646 }
1647
1648 if (bind(s, listensockaddr,
1649 (socklen_t)sizeof(struct sockaddr_un)) == -1) {
1650 log_warn(LD_NET,"Bind to %s failed: %s.", address,
1651 tor_socket_strerror(tor_socket_errno(s)));
1652 goto err;
1653 }
1654
1655 #ifdef HAVE_PWD_H
1656 if (options->User) {
1657 pw = tor_getpwnam(options->User);
1658 struct stat st;
1659 if (pw == NULL) {
1660 log_warn(LD_NET,"Unable to chown() %s socket: user %s not found.",
1661 address, options->User);
1662 goto err;
1663 } else if (fstat(s, &st) == 0 &&
1664 st.st_uid == pw->pw_uid && st.st_gid == pw->pw_gid) {
1665 /* No change needed */
1666 } else if (chown(sandbox_intern_string(address),
1667 pw->pw_uid, pw->pw_gid) < 0) {
1668 log_warn(LD_NET,"Unable to chown() %s socket: %s.",
1669 address, strerror(errno));
1670 goto err;
1671 }
1672 }
1673 #endif /* defined(HAVE_PWD_H) */
1674
1675 {
1676 unsigned mode;
1677 const char *status;
1678 struct stat st;
1679 if (port_cfg->is_world_writable) {
1680 mode = 0666;
1681 status = "world-writable";
1682 } else if (port_cfg->is_group_writable) {
1683 mode = 0660;
1684 status = "group-writable";
1685 } else {
1686 mode = 0600;
1687 status = "private";
1688 }
1689 /* We need to use chmod; fchmod doesn't work on sockets on all
1690 * platforms. */
1691 if (fstat(s, &st) == 0 && (st.st_mode & 0777) == mode) {
1692 /* no change needed */
1693 } else if (chmod(sandbox_intern_string(address), mode) < 0) {
1694 log_warn(LD_FS,"Unable to make %s %s.", address, status);
1695 goto err;
1696 }
1697 }
1698
1699 if (listen(s, SOMAXCONN) < 0) {
1700 log_warn(LD_NET, "Could not listen on %s: %s", address,
1701 tor_socket_strerror(tor_socket_errno(s)));
1702 goto err;
1703 }
1704
1705 #ifndef __APPLE__
1706 /* This code was introduced to help debug #28229. */
1707 int value;
1708 socklen_t len = sizeof(value);
1709
1710 if (!getsockopt(s, SOL_SOCKET, SO_ACCEPTCONN, &value, &len)) {
1711 if (value == 0) {
1712 log_err(LD_NET, "Could not listen on %s - "
1713 "getsockopt(.,SO_ACCEPTCONN,.) yields 0.", address);
1714 goto err;
1715 }
1716 }
1717 #endif /* !defined(__APPLE__) */
1718 #endif /* defined(HAVE_SYS_UN_H) */
1719 } else {
1720 log_err(LD_BUG, "Got unexpected address family %d.",
1721 listensockaddr->sa_family);
1722 tor_assert(0);
1723 }
1724
1725 lis_conn = listener_connection_new(type, listensockaddr->sa_family);
1726 conn = TO_CONN(lis_conn);
1727 conn->socket_family = listensockaddr->sa_family;
1728 conn->s = s;
1729 s = TOR_INVALID_SOCKET; /* Prevent double-close */
1730 conn->address = tor_strdup(address);
1731 conn->port = gotPort;
1732 tor_addr_copy(&conn->addr, &addr);
1733
1734 memcpy(&lis_conn->entry_cfg, &port_cfg->entry_cfg, sizeof(entry_port_cfg_t));
1735
1736 if (port_cfg->entry_cfg.isolation_flags) {
1737 lis_conn->entry_cfg.isolation_flags = port_cfg->entry_cfg.isolation_flags;
1738 if (port_cfg->entry_cfg.session_group >= 0) {
1739 lis_conn->entry_cfg.session_group = port_cfg->entry_cfg.session_group;
1740 } else {
1741 /* This can wrap after around INT_MAX listeners are opened. But I don't
1742 * believe that matters, since you would need to open a ridiculous
1743 * number of listeners while keeping the early ones open before you ever
1744 * hit this. An OR with a dozen ports open, for example, would have to
1745 * close and re-open its listeners every second for 4 years nonstop.
1746 */
1747 lis_conn->entry_cfg.session_group = global_next_session_group--;
1748 }
1749 }
1750
1751 if (connection_add(conn) < 0) { /* no space, forget it */
1752 log_warn(LD_NET,"connection_add for listener failed. Giving up.");
1753 goto err;
1754 }
1755
1756 log_fn(usePort==gotPort ? LOG_DEBUG : LOG_NOTICE, LD_NET,
1757 "%s listening on port %u.",
1758 conn_type_to_string(type), gotPort);
1759
1760 conn->state = LISTENER_STATE_READY;
1761 if (start_reading) {
1762 connection_start_reading(conn);
1763 } else {
1764 tor_assert(type == CONN_TYPE_AP_DNS_LISTENER);
1765 dnsserv_configure_listener(conn);
1766 }
1767
1768 /*
1769 * Normal exit; call the OOS handler since connection count just changed;
1770 * the exhaustion flag will always be zero here though.
1771 */
1772 connection_check_oos(get_n_open_sockets(), 0);
1773
1774 log_notice(LD_NET, "Opened %s", connection_describe(conn));
1775
1776 return conn;
1777
1778 err:
1779 if (SOCKET_OK(s))
1780 tor_close_socket(s);
1781 if (conn)
1782 connection_free(conn);
1783
1784 /* Call the OOS handler, indicate if we saw an exhaustion-related error */
1785 connection_check_oos(get_n_open_sockets(), exhaustion);
1786
1787 return NULL;
1788 }
1789
1790 /**
1791 * Create a new listener connection for a given <b>port</b>. In case we
1792 * for a reason that is not an error condition, set <b>defer</b>
1793 * to true. If we cannot bind listening socket because address is already
1794 * in use, set <b>addr_in_use</b> to true.
1795 */
1796 static connection_t *
connection_listener_new_for_port(const port_cfg_t * port,int * defer,int * addr_in_use)1797 connection_listener_new_for_port(const port_cfg_t *port,
1798 int *defer, int *addr_in_use)
1799 {
1800 connection_t *conn;
1801 struct sockaddr *listensockaddr;
1802 socklen_t listensocklen = 0;
1803 char *address=NULL;
1804 int real_port = port->port == CFG_AUTO_PORT ? 0 : port->port;
1805 tor_assert(real_port <= UINT16_MAX);
1806
1807 if (defer)
1808 *defer = 0;
1809
1810 if (port->server_cfg.no_listen) {
1811 if (defer)
1812 *defer = 1;
1813 return NULL;
1814 }
1815
1816 #ifndef _WIN32
1817 /* We don't need to be root to create a UNIX socket, so defer until after
1818 * setuid. */
1819 const or_options_t *options = get_options();
1820 if (port->is_unix_addr && !geteuid() && (options->User) &&
1821 strcmp(options->User, "root")) {
1822 if (defer)
1823 *defer = 1;
1824 return NULL;
1825 }
1826 #endif /* !defined(_WIN32) */
1827
1828 if (port->is_unix_addr) {
1829 listensockaddr = (struct sockaddr *)
1830 create_unix_sockaddr(port->unix_addr,
1831 &address, &listensocklen);
1832 } else {
1833 listensockaddr = tor_malloc(sizeof(struct sockaddr_storage));
1834 listensocklen = tor_addr_to_sockaddr(&port->addr,
1835 real_port,
1836 listensockaddr,
1837 sizeof(struct sockaddr_storage));
1838 address = tor_addr_to_str_dup(&port->addr);
1839 }
1840
1841 if (listensockaddr) {
1842 conn = connection_listener_new(listensockaddr, listensocklen,
1843 port->type, address, port,
1844 addr_in_use);
1845 tor_free(listensockaddr);
1846 tor_free(address);
1847 } else {
1848 conn = NULL;
1849 }
1850
1851 return conn;
1852 }
1853
1854 /** Do basic sanity checking on a newly received socket. Return 0
1855 * if it looks ok, else return -1.
1856 *
1857 * Notably, some TCP stacks can erroneously have accept() return successfully
1858 * with socklen 0, when the client sends an RST before the accept call (as
1859 * nmap does). We want to detect that, and not go on with the connection.
1860 */
1861 static int
check_sockaddr(const struct sockaddr * sa,int len,int level)1862 check_sockaddr(const struct sockaddr *sa, int len, int level)
1863 {
1864 int ok = 1;
1865
1866 if (sa->sa_family == AF_INET) {
1867 struct sockaddr_in *sin=(struct sockaddr_in*)sa;
1868 if (len != sizeof(struct sockaddr_in)) {
1869 log_fn(level, LD_NET, "Length of address not as expected: %d vs %d",
1870 len,(int)sizeof(struct sockaddr_in));
1871 ok = 0;
1872 }
1873 if (sin->sin_addr.s_addr == 0 || sin->sin_port == 0) {
1874 log_fn(level, LD_NET,
1875 "Address for new connection has address/port equal to zero.");
1876 ok = 0;
1877 }
1878 } else if (sa->sa_family == AF_INET6) {
1879 struct sockaddr_in6 *sin6=(struct sockaddr_in6*)sa;
1880 if (len != sizeof(struct sockaddr_in6)) {
1881 log_fn(level, LD_NET, "Length of address not as expected: %d vs %d",
1882 len,(int)sizeof(struct sockaddr_in6));
1883 ok = 0;
1884 }
1885 if (fast_mem_is_zero((void*)sin6->sin6_addr.s6_addr, 16) ||
1886 sin6->sin6_port == 0) {
1887 log_fn(level, LD_NET,
1888 "Address for new connection has address/port equal to zero.");
1889 ok = 0;
1890 }
1891 } else if (sa->sa_family == AF_UNIX) {
1892 ok = 1;
1893 } else {
1894 ok = 0;
1895 }
1896 return ok ? 0 : -1;
1897 }
1898
1899 /** Check whether the socket family from an accepted socket <b>got</b> is the
1900 * same as the one that <b>listener</b> is waiting for. If it isn't, log
1901 * a useful message and return -1. Else return 0.
1902 *
1903 * This is annoying, but can apparently happen on some Darwins. */
1904 static int
check_sockaddr_family_match(sa_family_t got,connection_t * listener)1905 check_sockaddr_family_match(sa_family_t got, connection_t *listener)
1906 {
1907 if (got != listener->socket_family) {
1908 log_info(LD_BUG, "A listener connection returned a socket with a "
1909 "mismatched family. %s for addr_family %d gave us a socket "
1910 "with address family %d. Dropping.",
1911 conn_type_to_string(listener->type),
1912 (int)listener->socket_family,
1913 (int)got);
1914 return -1;
1915 }
1916 return 0;
1917 }
1918
1919 /** The listener connection <b>conn</b> told poll() it wanted to read.
1920 * Call accept() on conn-\>s, and add the new connection if necessary.
1921 */
1922 static int
connection_handle_listener_read(connection_t * conn,int new_type)1923 connection_handle_listener_read(connection_t *conn, int new_type)
1924 {
1925 tor_socket_t news; /* the new socket */
1926 connection_t *newconn = 0;
1927 /* information about the remote peer when connecting to other routers */
1928 struct sockaddr_storage addrbuf;
1929 struct sockaddr *remote = (struct sockaddr*)&addrbuf;
1930 /* length of the remote address. Must be whatever accept() needs. */
1931 socklen_t remotelen = (socklen_t)sizeof(addrbuf);
1932 const or_options_t *options = get_options();
1933
1934 tor_assert((size_t)remotelen >= sizeof(struct sockaddr_in));
1935 memset(&addrbuf, 0, sizeof(addrbuf));
1936
1937 news = tor_accept_socket_nonblocking(conn->s,remote,&remotelen);
1938 if (!SOCKET_OK(news)) { /* accept() error */
1939 int e = tor_socket_errno(conn->s);
1940 if (ERRNO_IS_ACCEPT_EAGAIN(e)) {
1941 /*
1942 * they hung up before we could accept(). that's fine.
1943 *
1944 * give the OOS handler a chance to run though
1945 */
1946 connection_check_oos(get_n_open_sockets(), 0);
1947 return 0;
1948 } else if (ERRNO_IS_RESOURCE_LIMIT(e)) {
1949 socket_failed_from_resource_exhaustion();
1950 /* Exhaustion; tell the OOS handler */
1951 connection_check_oos(get_n_open_sockets(), 1);
1952 return 0;
1953 }
1954 /* else there was a real error. */
1955 log_warn(LD_NET,"accept() failed: %s. Closing listener.",
1956 tor_socket_strerror(e));
1957 connection_mark_for_close(conn);
1958 /* Tell the OOS handler about this too */
1959 connection_check_oos(get_n_open_sockets(), 0);
1960 return -1;
1961 }
1962 log_debug(LD_NET,
1963 "Connection accepted on socket %d (child of fd %d).",
1964 (int)news,(int)conn->s);
1965
1966 /* We accepted a new conn; run OOS handler */
1967 connection_check_oos(get_n_open_sockets(), 0);
1968
1969 if (make_socket_reuseable(news) < 0) {
1970 if (tor_socket_errno(news) == EINVAL) {
1971 /* This can happen on OSX if we get a badly timed shutdown. */
1972 log_debug(LD_NET, "make_socket_reuseable returned EINVAL");
1973 } else {
1974 log_warn(LD_NET, "Error setting SO_REUSEADDR flag on %s: %s",
1975 conn_type_to_string(new_type),
1976 tor_socket_strerror(errno));
1977 }
1978 tor_close_socket(news);
1979 return 0;
1980 }
1981
1982 if (options->ConstrainedSockets)
1983 set_constrained_socket_buffers(news, (int)options->ConstrainedSockSize);
1984
1985 if (check_sockaddr_family_match(remote->sa_family, conn) < 0) {
1986 tor_close_socket(news);
1987 return 0;
1988 }
1989
1990 if (conn->socket_family == AF_INET || conn->socket_family == AF_INET6 ||
1991 (conn->socket_family == AF_UNIX && new_type == CONN_TYPE_AP)) {
1992 tor_addr_t addr;
1993 uint16_t port;
1994 if (check_sockaddr(remote, remotelen, LOG_INFO)<0) {
1995 log_info(LD_NET,
1996 "accept() returned a strange address; closing connection.");
1997 tor_close_socket(news);
1998 return 0;
1999 }
2000
2001 tor_addr_from_sockaddr(&addr, remote, &port);
2002
2003 /* process entrance policies here, before we even create the connection */
2004 if (new_type == CONN_TYPE_AP) {
2005 /* check sockspolicy to see if we should accept it */
2006 if (socks_policy_permits_address(&addr) == 0) {
2007 log_notice(LD_APP,
2008 "Denying socks connection from untrusted address %s.",
2009 fmt_and_decorate_addr(&addr));
2010 tor_close_socket(news);
2011 return 0;
2012 }
2013 }
2014 if (new_type == CONN_TYPE_DIR) {
2015 /* check dirpolicy to see if we should accept it */
2016 if (dir_policy_permits_address(&addr) == 0) {
2017 log_notice(LD_DIRSERV,"Denying dir connection from address %s.",
2018 fmt_and_decorate_addr(&addr));
2019 tor_close_socket(news);
2020 return 0;
2021 }
2022 }
2023 if (new_type == CONN_TYPE_OR) {
2024 /* Assess with the connection DoS mitigation subsystem if this address
2025 * can open a new connection. */
2026 if (dos_conn_addr_get_defense_type(&addr) == DOS_CONN_DEFENSE_CLOSE) {
2027 tor_close_socket(news);
2028 return 0;
2029 }
2030 }
2031
2032 newconn = connection_new(new_type, conn->socket_family);
2033 newconn->s = news;
2034
2035 /* remember the remote address */
2036 tor_addr_copy(&newconn->addr, &addr);
2037 if (new_type == CONN_TYPE_AP && conn->socket_family == AF_UNIX) {
2038 newconn->port = 0;
2039 newconn->address = tor_strdup(conn->address);
2040 } else {
2041 newconn->port = port;
2042 newconn->address = tor_addr_to_str_dup(&addr);
2043 }
2044
2045 if (new_type == CONN_TYPE_AP && conn->socket_family != AF_UNIX) {
2046 log_info(LD_NET, "New SOCKS connection opened from %s.",
2047 fmt_and_decorate_addr(&addr));
2048 }
2049 if (new_type == CONN_TYPE_AP && conn->socket_family == AF_UNIX) {
2050 log_info(LD_NET, "New SOCKS AF_UNIX connection opened");
2051 }
2052 if (new_type == CONN_TYPE_CONTROL) {
2053 log_notice(LD_CONTROL, "New control connection opened from %s.",
2054 fmt_and_decorate_addr(&addr));
2055 }
2056 if (new_type == CONN_TYPE_METRICS) {
2057 log_info(LD_CONTROL, "New metrics connection opened from %s.",
2058 fmt_and_decorate_addr(&addr));
2059 }
2060
2061 } else if (conn->socket_family == AF_UNIX && conn->type != CONN_TYPE_AP) {
2062 tor_assert(conn->type == CONN_TYPE_CONTROL_LISTENER);
2063 tor_assert(new_type == CONN_TYPE_CONTROL);
2064 log_notice(LD_CONTROL, "New control connection opened.");
2065
2066 newconn = connection_new(new_type, conn->socket_family);
2067 newconn->s = news;
2068
2069 /* remember the remote address -- do we have anything sane to put here? */
2070 tor_addr_make_unspec(&newconn->addr);
2071 newconn->port = 1;
2072 newconn->address = tor_strdup(conn->address);
2073 } else {
2074 tor_assert(0);
2075 };
2076
2077 if (connection_add(newconn) < 0) { /* no space, forget it */
2078 connection_free(newconn);
2079 return 0; /* no need to tear down the parent */
2080 }
2081
2082 if (connection_init_accepted_conn(newconn, TO_LISTENER_CONN(conn)) < 0) {
2083 if (! newconn->marked_for_close)
2084 connection_mark_for_close(newconn);
2085 return 0;
2086 }
2087
2088 note_connection(true /* inbound */, conn->socket_family);
2089
2090 return 0;
2091 }
2092
2093 /** Initialize states for newly accepted connection <b>conn</b>.
2094 *
2095 * If conn is an OR, start the TLS handshake.
2096 *
2097 * If conn is a transparent AP, get its original destination
2098 * and place it in circuit_wait.
2099 *
2100 * The <b>listener</b> parameter is only used for AP connections.
2101 */
2102 int
connection_init_accepted_conn(connection_t * conn,const listener_connection_t * listener)2103 connection_init_accepted_conn(connection_t *conn,
2104 const listener_connection_t *listener)
2105 {
2106 int rv;
2107
2108 connection_start_reading(conn);
2109
2110 switch (conn->type) {
2111 case CONN_TYPE_EXT_OR:
2112 /* Initiate Extended ORPort authentication. */
2113 return connection_ext_or_start_auth(TO_OR_CONN(conn));
2114 case CONN_TYPE_OR:
2115 connection_or_event_status(TO_OR_CONN(conn), OR_CONN_EVENT_NEW, 0);
2116 rv = connection_tls_start_handshake(TO_OR_CONN(conn), 1);
2117 if (rv < 0) {
2118 connection_or_close_for_error(TO_OR_CONN(conn), 0);
2119 }
2120 return rv;
2121 break;
2122 case CONN_TYPE_AP:
2123 memcpy(&TO_ENTRY_CONN(conn)->entry_cfg, &listener->entry_cfg,
2124 sizeof(entry_port_cfg_t));
2125 TO_ENTRY_CONN(conn)->nym_epoch = get_signewnym_epoch();
2126 TO_ENTRY_CONN(conn)->socks_request->listener_type = listener->base_.type;
2127
2128 /* Any incoming connection on an entry port counts as user activity. */
2129 note_user_activity(approx_time());
2130
2131 switch (TO_CONN(listener)->type) {
2132 case CONN_TYPE_AP_LISTENER:
2133 conn->state = AP_CONN_STATE_SOCKS_WAIT;
2134 TO_ENTRY_CONN(conn)->socks_request->socks_prefer_no_auth =
2135 listener->entry_cfg.socks_prefer_no_auth;
2136 TO_ENTRY_CONN(conn)->socks_request->socks_use_extended_errors =
2137 listener->entry_cfg.extended_socks5_codes;
2138 break;
2139 case CONN_TYPE_AP_TRANS_LISTENER:
2140 TO_ENTRY_CONN(conn)->is_transparent_ap = 1;
2141 /* XXXX028 -- is this correct still, with the addition of
2142 * pending_entry_connections ? */
2143 conn->state = AP_CONN_STATE_CIRCUIT_WAIT;
2144 return connection_ap_process_transparent(TO_ENTRY_CONN(conn));
2145 case CONN_TYPE_AP_NATD_LISTENER:
2146 TO_ENTRY_CONN(conn)->is_transparent_ap = 1;
2147 conn->state = AP_CONN_STATE_NATD_WAIT;
2148 break;
2149 case CONN_TYPE_AP_HTTP_CONNECT_LISTENER:
2150 conn->state = AP_CONN_STATE_HTTP_CONNECT_WAIT;
2151 }
2152 break;
2153 case CONN_TYPE_DIR:
2154 conn->purpose = DIR_PURPOSE_SERVER;
2155 conn->state = DIR_CONN_STATE_SERVER_COMMAND_WAIT;
2156 break;
2157 case CONN_TYPE_CONTROL:
2158 conn->state = CONTROL_CONN_STATE_NEEDAUTH;
2159 break;
2160 }
2161 return 0;
2162 }
2163
2164 /** Take conn, make a nonblocking socket; try to connect to
2165 * sa, binding to bindaddr if sa is not localhost. If fail, return -1 and if
2166 * applicable put your best guess about errno into *<b>socket_error</b>.
2167 * If connected return 1, if EAGAIN return 0.
2168 */
2169 MOCK_IMPL(STATIC int,
2170 connection_connect_sockaddr,(connection_t *conn,
2171 const struct sockaddr *sa,
2172 socklen_t sa_len,
2173 const struct sockaddr *bindaddr,
2174 socklen_t bindaddr_len,
2175 int *socket_error))
2176 {
2177 tor_socket_t s;
2178 int inprogress = 0;
2179 const or_options_t *options = get_options();
2180
2181 tor_assert(conn);
2182 tor_assert(sa);
2183 tor_assert(socket_error);
2184
2185 if (net_is_completely_disabled()) {
2186 /* We should never even try to connect anyplace if the network is
2187 * completely shut off.
2188 *
2189 * (We don't check net_is_disabled() here, since we still sometimes
2190 * want to open connections when we're in soft hibernation.)
2191 */
2192 static ratelim_t disablenet_violated = RATELIM_INIT(30*60);
2193 *socket_error = SOCK_ERRNO(ENETUNREACH);
2194 log_fn_ratelim(&disablenet_violated, LOG_WARN, LD_BUG,
2195 "Tried to open a socket with DisableNetwork set.");
2196 tor_fragile_assert();
2197 return -1;
2198 }
2199
2200 const int protocol_family = sa->sa_family;
2201 const int proto = (sa->sa_family == AF_INET6 ||
2202 sa->sa_family == AF_INET) ? IPPROTO_TCP : 0;
2203
2204 s = tor_open_socket_nonblocking(protocol_family, SOCK_STREAM, proto);
2205 if (! SOCKET_OK(s)) {
2206 /*
2207 * Early OOS handler calls; it matters if it's an exhaustion-related
2208 * error or not.
2209 */
2210 *socket_error = tor_socket_errno(s);
2211 if (ERRNO_IS_RESOURCE_LIMIT(*socket_error)) {
2212 socket_failed_from_resource_exhaustion();
2213 connection_check_oos(get_n_open_sockets(), 1);
2214 } else {
2215 log_warn(LD_NET,"Error creating network socket: %s",
2216 tor_socket_strerror(*socket_error));
2217 connection_check_oos(get_n_open_sockets(), 0);
2218 }
2219 return -1;
2220 }
2221
2222 if (make_socket_reuseable(s) < 0) {
2223 log_warn(LD_NET, "Error setting SO_REUSEADDR flag on new connection: %s",
2224 tor_socket_strerror(errno));
2225 }
2226
2227 /*
2228 * We've got the socket open; give the OOS handler a chance to check
2229 * against configured maximum socket number, but tell it no exhaustion
2230 * failure.
2231 */
2232 connection_check_oos(get_n_open_sockets(), 0);
2233
2234 if (bindaddr && bind(s, bindaddr, bindaddr_len) < 0) {
2235 *socket_error = tor_socket_errno(s);
2236 log_warn(LD_NET,"Error binding network socket: %s",
2237 tor_socket_strerror(*socket_error));
2238 tor_close_socket(s);
2239 return -1;
2240 }
2241
2242 tor_assert(options);
2243 if (options->ConstrainedSockets)
2244 set_constrained_socket_buffers(s, (int)options->ConstrainedSockSize);
2245
2246 if (connect(s, sa, sa_len) < 0) {
2247 int e = tor_socket_errno(s);
2248 if (!ERRNO_IS_CONN_EINPROGRESS(e)) {
2249 /* yuck. kill it. */
2250 *socket_error = e;
2251 log_info(LD_NET,
2252 "connect() to socket failed: %s",
2253 tor_socket_strerror(e));
2254 tor_close_socket(s);
2255 return -1;
2256 } else {
2257 inprogress = 1;
2258 }
2259 }
2260
2261 note_connection(false /* outbound */, conn->socket_family);
2262
2263 /* it succeeded. we're connected. */
2264 log_fn(inprogress ? LOG_DEBUG : LOG_INFO, LD_NET,
2265 "Connection to socket %s (sock "TOR_SOCKET_T_FORMAT").",
2266 inprogress ? "in progress" : "established", s);
2267 conn->s = s;
2268 if (connection_add_connecting(conn) < 0) {
2269 /* no space, forget it */
2270 *socket_error = SOCK_ERRNO(ENOBUFS);
2271 return -1;
2272 }
2273
2274 return inprogress ? 0 : 1;
2275 }
2276
2277 /* Log a message if connection attempt is made when IPv4 or IPv6 is disabled.
2278 * Log a less severe message if we couldn't conform to ClientPreferIPv6ORPort
2279 * or ClientPreferIPv6ORPort. */
2280 static void
connection_connect_log_client_use_ip_version(const connection_t * conn)2281 connection_connect_log_client_use_ip_version(const connection_t *conn)
2282 {
2283 const or_options_t *options = get_options();
2284
2285 /* Only clients care about ClientUseIPv4/6, bail out early on servers, and
2286 * on connections we don't care about */
2287 if (server_mode(options) || !conn || conn->type == CONN_TYPE_EXIT) {
2288 return;
2289 }
2290
2291 /* We're only prepared to log OR and DIR connections here */
2292 if (conn->type != CONN_TYPE_OR && conn->type != CONN_TYPE_DIR) {
2293 return;
2294 }
2295
2296 const int must_ipv4 = !reachable_addr_use_ipv6(options);
2297 const int must_ipv6 = (options->ClientUseIPv4 == 0);
2298 const int pref_ipv6 = (conn->type == CONN_TYPE_OR
2299 ? reachable_addr_prefer_ipv6_orport(options)
2300 : reachable_addr_prefer_ipv6_dirport(options));
2301 tor_addr_t real_addr;
2302 tor_addr_copy(&real_addr, &conn->addr);
2303
2304 /* Check if we broke a mandatory address family restriction */
2305 if ((must_ipv4 && tor_addr_family(&real_addr) == AF_INET6)
2306 || (must_ipv6 && tor_addr_family(&real_addr) == AF_INET)) {
2307 static int logged_backtrace = 0;
2308 log_info(LD_BUG, "Outgoing %s connection to %s violated ClientUseIPv%s 0.",
2309 conn->type == CONN_TYPE_OR ? "OR" : "Dir",
2310 fmt_addr(&real_addr),
2311 options->ClientUseIPv4 == 0 ? "4" : "6");
2312 if (!logged_backtrace) {
2313 log_backtrace(LOG_INFO, LD_BUG, "Address came from");
2314 logged_backtrace = 1;
2315 }
2316 }
2317
2318 /* Bridges are allowed to break IPv4/IPv6 ORPort preferences to connect to
2319 * the node's configured address when ClientPreferIPv6ORPort is auto */
2320 if (options->UseBridges && conn->type == CONN_TYPE_OR
2321 && options->ClientPreferIPv6ORPort == -1) {
2322 return;
2323 }
2324
2325 if (reachable_addr_use_ipv6(options)) {
2326 log_info(LD_NET, "Our outgoing connection is using IPv%d.",
2327 tor_addr_family(&real_addr) == AF_INET6 ? 6 : 4);
2328 }
2329
2330 /* Check if we couldn't satisfy an address family preference */
2331 if ((!pref_ipv6 && tor_addr_family(&real_addr) == AF_INET6)
2332 || (pref_ipv6 && tor_addr_family(&real_addr) == AF_INET)) {
2333 log_info(LD_NET, "Outgoing connection to %s doesn't satisfy "
2334 "ClientPreferIPv6%sPort %d, with ClientUseIPv4 %d, and "
2335 "reachable_addr_use_ipv6 %d (ClientUseIPv6 %d and UseBridges "
2336 "%d).",
2337 fmt_addr(&real_addr),
2338 conn->type == CONN_TYPE_OR ? "OR" : "Dir",
2339 conn->type == CONN_TYPE_OR ? options->ClientPreferIPv6ORPort
2340 : options->ClientPreferIPv6DirPort,
2341 options->ClientUseIPv4, reachable_addr_use_ipv6(options),
2342 options->ClientUseIPv6, options->UseBridges);
2343 }
2344 }
2345
2346 /** Retrieve the outbound address depending on the protocol (IPv4 or IPv6)
2347 * and the connection type (relay, exit, ...)
2348 * Return a socket address or NULL in case nothing is configured.
2349 **/
2350 const tor_addr_t *
conn_get_outbound_address(sa_family_t family,const or_options_t * options,unsigned int conn_type)2351 conn_get_outbound_address(sa_family_t family,
2352 const or_options_t *options, unsigned int conn_type)
2353 {
2354 const tor_addr_t *ext_addr = NULL;
2355
2356 int fam_index;
2357 switch (family) {
2358 case AF_INET:
2359 fam_index = 0;
2360 break;
2361 case AF_INET6:
2362 fam_index = 1;
2363 break;
2364 default:
2365 return NULL;
2366 }
2367
2368 // If an exit connection, use the exit address (if present)
2369 if (conn_type == CONN_TYPE_EXIT) {
2370 if (!tor_addr_is_null(
2371 &options->OutboundBindAddresses[OUTBOUND_ADDR_EXIT][fam_index])) {
2372 ext_addr = &options->OutboundBindAddresses[OUTBOUND_ADDR_EXIT]
2373 [fam_index];
2374 } else if (!tor_addr_is_null(
2375 &options->OutboundBindAddresses[OUTBOUND_ADDR_ANY]
2376 [fam_index])) {
2377 ext_addr = &options->OutboundBindAddresses[OUTBOUND_ADDR_ANY]
2378 [fam_index];
2379 }
2380 } else { // All non-exit connections
2381 if (!tor_addr_is_null(
2382 &options->OutboundBindAddresses[OUTBOUND_ADDR_OR][fam_index])) {
2383 ext_addr = &options->OutboundBindAddresses[OUTBOUND_ADDR_OR]
2384 [fam_index];
2385 } else if (!tor_addr_is_null(
2386 &options->OutboundBindAddresses[OUTBOUND_ADDR_ANY]
2387 [fam_index])) {
2388 ext_addr = &options->OutboundBindAddresses[OUTBOUND_ADDR_ANY]
2389 [fam_index];
2390 }
2391 }
2392 return ext_addr;
2393 }
2394
2395 /** Take conn, make a nonblocking socket; try to connect to
2396 * addr:port (port arrives in *host order*). If fail, return -1 and if
2397 * applicable put your best guess about errno into *<b>socket_error</b>.
2398 * Else assign s to conn-\>s: if connected return 1, if EAGAIN return 0.
2399 *
2400 * addr:port can be different to conn->addr:conn->port if connecting through
2401 * a proxy.
2402 *
2403 * address is used to make the logs useful.
2404 *
2405 * On success, add conn to the list of polled connections.
2406 */
2407 int
connection_connect(connection_t * conn,const char * address,const tor_addr_t * addr,uint16_t port,int * socket_error)2408 connection_connect(connection_t *conn, const char *address,
2409 const tor_addr_t *addr, uint16_t port, int *socket_error)
2410 {
2411 struct sockaddr_storage addrbuf;
2412 struct sockaddr_storage bind_addr_ss;
2413 struct sockaddr *bind_addr = NULL;
2414 struct sockaddr *dest_addr;
2415 int dest_addr_len, bind_addr_len = 0;
2416
2417 /* Log if we didn't stick to ClientUseIPv4/6 or ClientPreferIPv6OR/DirPort
2418 */
2419 connection_connect_log_client_use_ip_version(conn);
2420
2421 if (!tor_addr_is_loopback(addr)) {
2422 const tor_addr_t *ext_addr = NULL;
2423 ext_addr = conn_get_outbound_address(tor_addr_family(addr), get_options(),
2424 conn->type);
2425 if (ext_addr) {
2426 memset(&bind_addr_ss, 0, sizeof(bind_addr_ss));
2427 bind_addr_len = tor_addr_to_sockaddr(ext_addr, 0,
2428 (struct sockaddr *) &bind_addr_ss,
2429 sizeof(bind_addr_ss));
2430 if (bind_addr_len == 0) {
2431 log_warn(LD_NET,
2432 "Error converting OutboundBindAddress %s into sockaddr. "
2433 "Ignoring.", fmt_and_decorate_addr(ext_addr));
2434 } else {
2435 bind_addr = (struct sockaddr *)&bind_addr_ss;
2436 }
2437 }
2438 }
2439
2440 memset(&addrbuf,0,sizeof(addrbuf));
2441 dest_addr = (struct sockaddr*) &addrbuf;
2442 dest_addr_len = tor_addr_to_sockaddr(addr, port, dest_addr, sizeof(addrbuf));
2443 tor_assert(dest_addr_len > 0);
2444
2445 log_debug(LD_NET, "Connecting to %s:%u.",
2446 escaped_safe_str_client(address), port);
2447
2448 return connection_connect_sockaddr(conn, dest_addr, dest_addr_len,
2449 bind_addr, bind_addr_len, socket_error);
2450 }
2451
2452 #ifdef HAVE_SYS_UN_H
2453
2454 /** Take conn, make a nonblocking socket; try to connect to
2455 * an AF_UNIX socket at socket_path. If fail, return -1 and if applicable
2456 * put your best guess about errno into *<b>socket_error</b>. Else assign s
2457 * to conn-\>s: if connected return 1, if EAGAIN return 0.
2458 *
2459 * On success, add conn to the list of polled connections.
2460 */
2461 int
connection_connect_unix(connection_t * conn,const char * socket_path,int * socket_error)2462 connection_connect_unix(connection_t *conn, const char *socket_path,
2463 int *socket_error)
2464 {
2465 struct sockaddr_un dest_addr;
2466
2467 tor_assert(socket_path);
2468
2469 /* Check that we'll be able to fit it into dest_addr later */
2470 if (strlen(socket_path) + 1 > sizeof(dest_addr.sun_path)) {
2471 log_warn(LD_NET,
2472 "Path %s is too long for an AF_UNIX socket\n",
2473 escaped_safe_str_client(socket_path));
2474 *socket_error = SOCK_ERRNO(ENAMETOOLONG);
2475 return -1;
2476 }
2477
2478 memset(&dest_addr, 0, sizeof(dest_addr));
2479 dest_addr.sun_family = AF_UNIX;
2480 strlcpy(dest_addr.sun_path, socket_path, sizeof(dest_addr.sun_path));
2481
2482 log_debug(LD_NET,
2483 "Connecting to AF_UNIX socket at %s.",
2484 escaped_safe_str_client(socket_path));
2485
2486 return connection_connect_sockaddr(conn,
2487 (struct sockaddr *)&dest_addr, sizeof(dest_addr),
2488 NULL, 0, socket_error);
2489 }
2490
2491 #endif /* defined(HAVE_SYS_UN_H) */
2492
2493 /** Convert state number to string representation for logging purposes.
2494 */
2495 static const char *
connection_proxy_state_to_string(int state)2496 connection_proxy_state_to_string(int state)
2497 {
2498 static const char *unknown = "???";
2499 static const char *states[] = {
2500 "PROXY_NONE",
2501 "PROXY_INFANT",
2502 "PROXY_HTTPS_WANT_CONNECT_OK",
2503 "PROXY_SOCKS4_WANT_CONNECT_OK",
2504 "PROXY_SOCKS5_WANT_AUTH_METHOD_NONE",
2505 "PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929",
2506 "PROXY_SOCKS5_WANT_AUTH_RFC1929_OK",
2507 "PROXY_SOCKS5_WANT_CONNECT_OK",
2508 "PROXY_HAPROXY_WAIT_FOR_FLUSH",
2509 "PROXY_CONNECTED",
2510 };
2511
2512 CTASSERT(ARRAY_LENGTH(states) == PROXY_CONNECTED+1);
2513
2514 if (state < PROXY_NONE || state > PROXY_CONNECTED)
2515 return unknown;
2516
2517 return states[state];
2518 }
2519
2520 /** Returns the proxy type used by tor for a single connection, for
2521 * logging or high-level purposes. Don't use it to fill the
2522 * <b>proxy_type</b> field of or_connection_t; use the actual proxy
2523 * protocol instead.*/
2524 static int
conn_get_proxy_type(const connection_t * conn)2525 conn_get_proxy_type(const connection_t *conn)
2526 {
2527 const or_options_t *options = get_options();
2528
2529 if (options->ClientTransportPlugin) {
2530 /* If we have plugins configured *and* this addr/port is a known bridge
2531 * with a transport, then we should be PROXY_PLUGGABLE. */
2532 const transport_t *transport = NULL;
2533 int r;
2534 r = get_transport_by_bridge_addrport(&conn->addr, conn->port, &transport);
2535 if (r == 0 && transport)
2536 return PROXY_PLUGGABLE;
2537 }
2538
2539 /* In all other cases, we're using a global proxy. */
2540 if (options->HTTPSProxy)
2541 return PROXY_CONNECT;
2542 else if (options->Socks4Proxy)
2543 return PROXY_SOCKS4;
2544 else if (options->Socks5Proxy)
2545 return PROXY_SOCKS5;
2546 else if (options->TCPProxy) {
2547 /* The only supported protocol in TCPProxy is haproxy. */
2548 tor_assert(options->TCPProxyProtocol == TCP_PROXY_PROTOCOL_HAPROXY);
2549 return PROXY_HAPROXY;
2550 } else
2551 return PROXY_NONE;
2552 }
2553
2554 /* One byte for the version, one for the command, two for the
2555 port, and four for the addr... and, one more for the
2556 username NUL: */
2557 #define SOCKS4_STANDARD_BUFFER_SIZE (1 + 1 + 2 + 4 + 1)
2558
2559 /** Write a proxy request of https to conn for conn->addr:conn->port,
2560 * authenticating with the auth details given in the configuration
2561 * (if available).
2562 *
2563 * Returns -1 if conn->addr is incompatible with the proxy protocol, and
2564 * 0 otherwise.
2565 */
2566 static int
connection_https_proxy_connect(connection_t * conn)2567 connection_https_proxy_connect(connection_t *conn)
2568 {
2569 tor_assert(conn);
2570
2571 const or_options_t *options = get_options();
2572 char buf[1024];
2573 char *base64_authenticator = NULL;
2574 const char *authenticator = options->HTTPSProxyAuthenticator;
2575
2576 /* Send HTTP CONNECT and authentication (if available) in
2577 * one request */
2578
2579 if (authenticator) {
2580 base64_authenticator = alloc_http_authenticator(authenticator);
2581 if (!base64_authenticator)
2582 log_warn(LD_OR, "Encoding https authenticator failed");
2583 }
2584
2585 if (base64_authenticator) {
2586 const char *addrport = fmt_addrport(&conn->addr, conn->port);
2587 tor_snprintf(buf, sizeof(buf), "CONNECT %s HTTP/1.1\r\n"
2588 "Host: %s\r\n"
2589 "Proxy-Authorization: Basic %s\r\n\r\n",
2590 addrport,
2591 addrport,
2592 base64_authenticator);
2593 tor_free(base64_authenticator);
2594 } else {
2595 tor_snprintf(buf, sizeof(buf), "CONNECT %s HTTP/1.0\r\n\r\n",
2596 fmt_addrport(&conn->addr, conn->port));
2597 }
2598
2599 connection_buf_add(buf, strlen(buf), conn);
2600 conn->proxy_state = PROXY_HTTPS_WANT_CONNECT_OK;
2601
2602 return 0;
2603 }
2604
2605 /** Write a proxy request of socks4 to conn for conn->addr:conn->port.
2606 *
2607 * Returns -1 if conn->addr is incompatible with the proxy protocol, and
2608 * 0 otherwise.
2609 */
2610 static int
connection_socks4_proxy_connect(connection_t * conn)2611 connection_socks4_proxy_connect(connection_t *conn)
2612 {
2613 tor_assert(conn);
2614
2615 unsigned char *buf;
2616 uint16_t portn;
2617 uint32_t ip4addr;
2618 size_t buf_size = 0;
2619 char *socks_args_string = NULL;
2620
2621 /* Send a SOCKS4 connect request */
2622
2623 if (tor_addr_family(&conn->addr) != AF_INET) {
2624 log_warn(LD_NET, "SOCKS4 client is incompatible with IPv6");
2625 return -1;
2626 }
2627
2628 { /* If we are here because we are trying to connect to a
2629 pluggable transport proxy, check if we have any SOCKS
2630 arguments to transmit. If we do, compress all arguments to
2631 a single string in 'socks_args_string': */
2632
2633 if (conn_get_proxy_type(conn) == PROXY_PLUGGABLE) {
2634 socks_args_string =
2635 pt_get_socks_args_for_proxy_addrport(&conn->addr, conn->port);
2636 if (socks_args_string)
2637 log_debug(LD_NET, "Sending out '%s' as our SOCKS argument string.",
2638 socks_args_string);
2639 }
2640 }
2641
2642 { /* Figure out the buffer size we need for the SOCKS message: */
2643
2644 buf_size = SOCKS4_STANDARD_BUFFER_SIZE;
2645
2646 /* If we have a SOCKS argument string, consider its size when
2647 calculating the buffer size: */
2648 if (socks_args_string)
2649 buf_size += strlen(socks_args_string);
2650 }
2651
2652 buf = tor_malloc_zero(buf_size);
2653
2654 ip4addr = tor_addr_to_ipv4n(&conn->addr);
2655 portn = htons(conn->port);
2656
2657 buf[0] = 4; /* version */
2658 buf[1] = SOCKS_COMMAND_CONNECT; /* command */
2659 memcpy(buf + 2, &portn, 2); /* port */
2660 memcpy(buf + 4, &ip4addr, 4); /* addr */
2661
2662 /* Next packet field is the userid. If we have pluggable
2663 transport SOCKS arguments, we have to embed them
2664 there. Otherwise, we use an empty userid. */
2665 if (socks_args_string) { /* place the SOCKS args string: */
2666 tor_assert(strlen(socks_args_string) > 0);
2667 tor_assert(buf_size >=
2668 SOCKS4_STANDARD_BUFFER_SIZE + strlen(socks_args_string));
2669 strlcpy((char *)buf + 8, socks_args_string, buf_size - 8);
2670 tor_free(socks_args_string);
2671 } else {
2672 buf[8] = 0; /* no userid */
2673 }
2674
2675 connection_buf_add((char *)buf, buf_size, conn);
2676 tor_free(buf);
2677
2678 conn->proxy_state = PROXY_SOCKS4_WANT_CONNECT_OK;
2679 return 0;
2680 }
2681
2682 /** Write a proxy request of socks5 to conn for conn->addr:conn->port,
2683 * authenticating with the auth details given in the configuration
2684 * (if available).
2685 *
2686 * Returns -1 if conn->addr is incompatible with the proxy protocol, and
2687 * 0 otherwise.
2688 */
2689 static int
connection_socks5_proxy_connect(connection_t * conn)2690 connection_socks5_proxy_connect(connection_t *conn)
2691 {
2692 tor_assert(conn);
2693
2694 const or_options_t *options = get_options();
2695 unsigned char buf[4]; /* fields: vers, num methods, method list */
2696
2697 /* Send a SOCKS5 greeting (connect request must wait) */
2698
2699 buf[0] = 5; /* version */
2700
2701 /* We have to use SOCKS5 authentication, if we have a
2702 Socks5ProxyUsername or if we want to pass arguments to our
2703 pluggable transport proxy: */
2704 if ((options->Socks5ProxyUsername) ||
2705 (conn_get_proxy_type(conn) == PROXY_PLUGGABLE &&
2706 (get_socks_args_by_bridge_addrport(&conn->addr, conn->port)))) {
2707 /* number of auth methods */
2708 buf[1] = 2;
2709 buf[2] = 0x00; /* no authentication */
2710 buf[3] = 0x02; /* rfc1929 Username/Passwd auth */
2711 conn->proxy_state = PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929;
2712 } else {
2713 buf[1] = 1;
2714 buf[2] = 0x00; /* no authentication */
2715 conn->proxy_state = PROXY_SOCKS5_WANT_AUTH_METHOD_NONE;
2716 }
2717
2718 connection_buf_add((char *)buf, 2 + buf[1], conn);
2719 return 0;
2720 }
2721
2722 /** Write a proxy request of haproxy to conn for conn->addr:conn->port.
2723 *
2724 * Returns -1 if conn->addr is incompatible with the proxy protocol, and
2725 * 0 otherwise.
2726 */
2727 static int
connection_haproxy_proxy_connect(connection_t * conn)2728 connection_haproxy_proxy_connect(connection_t *conn)
2729 {
2730 int ret = 0;
2731 tor_addr_port_t *addr_port = tor_addr_port_new(&conn->addr, conn->port);
2732 char *buf = haproxy_format_proxy_header_line(addr_port);
2733
2734 if (buf == NULL) {
2735 ret = -1;
2736 goto done;
2737 }
2738
2739 connection_buf_add(buf, strlen(buf), conn);
2740 /* In haproxy, we don't have to wait for the response, but we wait for ack.
2741 * So we can set the state to be PROXY_HAPROXY_WAIT_FOR_FLUSH. */
2742 conn->proxy_state = PROXY_HAPROXY_WAIT_FOR_FLUSH;
2743
2744 ret = 0;
2745 done:
2746 tor_free(buf);
2747 tor_free(addr_port);
2748 return ret;
2749 }
2750
2751 /** Write a proxy request of <b>type</b> (socks4, socks5, https, haproxy)
2752 * to conn for conn->addr:conn->port, authenticating with the auth details
2753 * given in the configuration (if available). SOCKS 5 and HTTP CONNECT
2754 * proxies support authentication.
2755 *
2756 * Returns -1 if conn->addr is incompatible with the proxy protocol, and
2757 * 0 otherwise.
2758 *
2759 * Use connection_read_proxy_handshake() to complete the handshake.
2760 */
2761 int
connection_proxy_connect(connection_t * conn,int type)2762 connection_proxy_connect(connection_t *conn, int type)
2763 {
2764 int ret = 0;
2765
2766 tor_assert(conn);
2767
2768 switch (type) {
2769 case PROXY_CONNECT:
2770 ret = connection_https_proxy_connect(conn);
2771 break;
2772
2773 case PROXY_SOCKS4:
2774 ret = connection_socks4_proxy_connect(conn);
2775 break;
2776
2777 case PROXY_SOCKS5:
2778 ret = connection_socks5_proxy_connect(conn);
2779 break;
2780
2781 case PROXY_HAPROXY:
2782 ret = connection_haproxy_proxy_connect(conn);
2783 break;
2784
2785 default:
2786 log_err(LD_BUG, "Invalid proxy protocol, %d", type);
2787 tor_fragile_assert();
2788 ret = -1;
2789 break;
2790 }
2791
2792 if (ret == 0) {
2793 log_debug(LD_NET, "set state %s",
2794 connection_proxy_state_to_string(conn->proxy_state));
2795 }
2796
2797 return ret;
2798 }
2799
2800 /** Read conn's inbuf. If the http response from the proxy is all
2801 * here, make sure it's good news, then return 1. If it's bad news,
2802 * return -1. Else return 0 and hope for better luck next time.
2803 */
2804 static int
connection_read_https_proxy_response(connection_t * conn)2805 connection_read_https_proxy_response(connection_t *conn)
2806 {
2807 char *headers;
2808 char *reason=NULL;
2809 int status_code;
2810 time_t date_header;
2811
2812 switch (fetch_from_buf_http(conn->inbuf,
2813 &headers, MAX_HEADERS_SIZE,
2814 NULL, NULL, 10000, 0)) {
2815 case -1: /* overflow */
2816 log_warn(LD_PROTOCOL,
2817 "Your https proxy sent back an oversized response. Closing.");
2818 return -1;
2819 case 0:
2820 log_info(LD_NET,"https proxy response not all here yet. Waiting.");
2821 return 0;
2822 /* case 1, fall through */
2823 }
2824
2825 if (parse_http_response(headers, &status_code, &date_header,
2826 NULL, &reason) < 0) {
2827 log_warn(LD_NET,
2828 "Unparseable headers from proxy (%s). Closing.",
2829 connection_describe(conn));
2830 tor_free(headers);
2831 return -1;
2832 }
2833 tor_free(headers);
2834 if (!reason) reason = tor_strdup("[no reason given]");
2835
2836 if (status_code == 200) {
2837 log_info(LD_NET,
2838 "HTTPS connect for %s successful! (200 %s) Starting TLS.",
2839 connection_describe(conn), escaped(reason));
2840 tor_free(reason);
2841 return 1;
2842 }
2843 /* else, bad news on the status code */
2844 switch (status_code) {
2845 case 403:
2846 log_warn(LD_NET,
2847 "The https proxy refused to allow connection to %s "
2848 "(status code %d, %s). Closing.",
2849 conn->address, status_code, escaped(reason));
2850 break;
2851 default:
2852 log_warn(LD_NET,
2853 "The https proxy sent back an unexpected status code %d (%s). "
2854 "Closing.",
2855 status_code, escaped(reason));
2856 break;
2857 }
2858 tor_free(reason);
2859 return -1;
2860 }
2861
2862 /** Send SOCKS5 CONNECT command to <b>conn</b>, copying <b>conn->addr</b>
2863 * and <b>conn->port</b> into the request.
2864 */
2865 static void
connection_send_socks5_connect(connection_t * conn)2866 connection_send_socks5_connect(connection_t *conn)
2867 {
2868 unsigned char buf[1024];
2869 size_t reqsize = 6;
2870 uint16_t port = htons(conn->port);
2871
2872 buf[0] = 5; /* version */
2873 buf[1] = SOCKS_COMMAND_CONNECT; /* command */
2874 buf[2] = 0; /* reserved */
2875
2876 if (tor_addr_family(&conn->addr) == AF_INET) {
2877 uint32_t addr = tor_addr_to_ipv4n(&conn->addr);
2878
2879 buf[3] = 1;
2880 reqsize += 4;
2881 memcpy(buf + 4, &addr, 4);
2882 memcpy(buf + 8, &port, 2);
2883 } else { /* AF_INET6 */
2884 buf[3] = 4;
2885 reqsize += 16;
2886 memcpy(buf + 4, tor_addr_to_in6_addr8(&conn->addr), 16);
2887 memcpy(buf + 20, &port, 2);
2888 }
2889
2890 connection_buf_add((char *)buf, reqsize, conn);
2891
2892 conn->proxy_state = PROXY_SOCKS5_WANT_CONNECT_OK;
2893 }
2894
2895 /** Wrapper around fetch_from_buf_socks_client: see that functions
2896 * for documentation of its behavior. */
2897 static int
connection_fetch_from_buf_socks_client(connection_t * conn,int state,char ** reason)2898 connection_fetch_from_buf_socks_client(connection_t *conn,
2899 int state, char **reason)
2900 {
2901 return fetch_from_buf_socks_client(conn->inbuf, state, reason);
2902 }
2903
2904 /** Call this from connection_*_process_inbuf() to advance the proxy
2905 * handshake.
2906 *
2907 * No matter what proxy protocol is used, if this function returns 1, the
2908 * handshake is complete, and the data remaining on inbuf may contain the
2909 * start of the communication with the requested server.
2910 *
2911 * Returns 0 if the current buffer contains an incomplete response, and -1
2912 * on error.
2913 */
2914 int
connection_read_proxy_handshake(connection_t * conn)2915 connection_read_proxy_handshake(connection_t *conn)
2916 {
2917 int ret = 0;
2918 char *reason = NULL;
2919
2920 log_debug(LD_NET, "enter state %s",
2921 connection_proxy_state_to_string(conn->proxy_state));
2922
2923 switch (conn->proxy_state) {
2924 case PROXY_HTTPS_WANT_CONNECT_OK:
2925 ret = connection_read_https_proxy_response(conn);
2926 if (ret == 1)
2927 conn->proxy_state = PROXY_CONNECTED;
2928 break;
2929
2930 case PROXY_SOCKS4_WANT_CONNECT_OK:
2931 ret = connection_fetch_from_buf_socks_client(conn,
2932 conn->proxy_state,
2933 &reason);
2934 if (ret == 1)
2935 conn->proxy_state = PROXY_CONNECTED;
2936 break;
2937
2938 case PROXY_SOCKS5_WANT_AUTH_METHOD_NONE:
2939 ret = connection_fetch_from_buf_socks_client(conn,
2940 conn->proxy_state,
2941 &reason);
2942 /* no auth needed, do connect */
2943 if (ret == 1) {
2944 connection_send_socks5_connect(conn);
2945 ret = 0;
2946 }
2947 break;
2948
2949 case PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929:
2950 ret = connection_fetch_from_buf_socks_client(conn,
2951 conn->proxy_state,
2952 &reason);
2953
2954 /* send auth if needed, otherwise do connect */
2955 if (ret == 1) {
2956 connection_send_socks5_connect(conn);
2957 ret = 0;
2958 } else if (ret == 2) {
2959 unsigned char buf[1024];
2960 size_t reqsize, usize, psize;
2961 const char *user, *pass;
2962 char *socks_args_string = NULL;
2963
2964 if (conn_get_proxy_type(conn) == PROXY_PLUGGABLE) {
2965 socks_args_string =
2966 pt_get_socks_args_for_proxy_addrport(&conn->addr, conn->port);
2967 if (!socks_args_string) {
2968 log_warn(LD_NET, "Could not create SOCKS args string for PT.");
2969 ret = -1;
2970 break;
2971 }
2972
2973 log_debug(LD_NET, "PT SOCKS5 arguments: %s", socks_args_string);
2974 tor_assert(strlen(socks_args_string) > 0);
2975 tor_assert(strlen(socks_args_string) <= MAX_SOCKS5_AUTH_SIZE_TOTAL);
2976
2977 if (strlen(socks_args_string) > MAX_SOCKS5_AUTH_FIELD_SIZE) {
2978 user = socks_args_string;
2979 usize = MAX_SOCKS5_AUTH_FIELD_SIZE;
2980 pass = socks_args_string + MAX_SOCKS5_AUTH_FIELD_SIZE;
2981 psize = strlen(socks_args_string) - MAX_SOCKS5_AUTH_FIELD_SIZE;
2982 } else {
2983 user = socks_args_string;
2984 usize = strlen(socks_args_string);
2985 pass = "\0";
2986 psize = 1;
2987 }
2988 } else if (get_options()->Socks5ProxyUsername) {
2989 user = get_options()->Socks5ProxyUsername;
2990 pass = get_options()->Socks5ProxyPassword;
2991 tor_assert(user && pass);
2992 usize = strlen(user);
2993 psize = strlen(pass);
2994 } else {
2995 log_err(LD_BUG, "We entered %s for no reason!", __func__);
2996 tor_fragile_assert();
2997 ret = -1;
2998 break;
2999 }
3000
3001 /* Username and password lengths should have been checked
3002 above and during torrc parsing. */
3003 tor_assert(usize <= MAX_SOCKS5_AUTH_FIELD_SIZE &&
3004 psize <= MAX_SOCKS5_AUTH_FIELD_SIZE);
3005 reqsize = 3 + usize + psize;
3006
3007 buf[0] = 1; /* negotiation version */
3008 buf[1] = usize;
3009 memcpy(buf + 2, user, usize);
3010 buf[2 + usize] = psize;
3011 memcpy(buf + 3 + usize, pass, psize);
3012
3013 if (socks_args_string)
3014 tor_free(socks_args_string);
3015
3016 connection_buf_add((char *)buf, reqsize, conn);
3017
3018 conn->proxy_state = PROXY_SOCKS5_WANT_AUTH_RFC1929_OK;
3019 ret = 0;
3020 }
3021 break;
3022
3023 case PROXY_SOCKS5_WANT_AUTH_RFC1929_OK:
3024 ret = connection_fetch_from_buf_socks_client(conn,
3025 conn->proxy_state,
3026 &reason);
3027 /* send the connect request */
3028 if (ret == 1) {
3029 connection_send_socks5_connect(conn);
3030 ret = 0;
3031 }
3032 break;
3033
3034 case PROXY_SOCKS5_WANT_CONNECT_OK:
3035 ret = connection_fetch_from_buf_socks_client(conn,
3036 conn->proxy_state,
3037 &reason);
3038 if (ret == 1)
3039 conn->proxy_state = PROXY_CONNECTED;
3040 break;
3041
3042 default:
3043 log_err(LD_BUG, "Invalid proxy_state for reading, %d",
3044 conn->proxy_state);
3045 tor_fragile_assert();
3046 ret = -1;
3047 break;
3048 }
3049
3050 log_debug(LD_NET, "leaving state %s",
3051 connection_proxy_state_to_string(conn->proxy_state));
3052
3053 if (ret < 0) {
3054 if (reason) {
3055 log_warn(LD_NET, "Proxy Client: unable to connect %s (%s)",
3056 connection_describe(conn), escaped(reason));
3057 tor_free(reason);
3058 } else {
3059 log_warn(LD_NET, "Proxy Client: unable to connect %s",
3060 connection_describe(conn));
3061 }
3062 } else if (ret == 1) {
3063 log_info(LD_NET, "Proxy Client: %s successful",
3064 connection_describe(conn));
3065 }
3066
3067 return ret;
3068 }
3069
3070 /** Given a list of listener connections in <b>old_conns</b>, and list of
3071 * port_cfg_t entries in <b>ports</b>, open a new listener for every port in
3072 * <b>ports</b> that does not already have a listener in <b>old_conns</b>.
3073 *
3074 * Remove from <b>old_conns</b> every connection that has a corresponding
3075 * entry in <b>ports</b>. Add to <b>new_conns</b> new every connection we
3076 * launch. If we may need to perform socket rebind when creating new
3077 * listener that replaces old one, create a <b>listener_replacement_t</b>
3078 * struct for affected pair and add it to <b>replacements</b>.
3079 *
3080 * If <b>control_listeners_only</b> is true, then we only open control
3081 * listeners, and we do not remove any noncontrol listeners from
3082 * old_conns.
3083 *
3084 * Return 0 on success, -1 on failure.
3085 **/
3086 static int
retry_listener_ports(smartlist_t * old_conns,const smartlist_t * ports,smartlist_t * new_conns,smartlist_t * replacements,int control_listeners_only)3087 retry_listener_ports(smartlist_t *old_conns,
3088 const smartlist_t *ports,
3089 smartlist_t *new_conns,
3090 smartlist_t *replacements,
3091 int control_listeners_only)
3092 {
3093 #ifndef ENABLE_LISTENER_REBIND
3094 (void)replacements;
3095 #endif
3096
3097 smartlist_t *launch = smartlist_new();
3098 int r = 0;
3099
3100 if (control_listeners_only) {
3101 SMARTLIST_FOREACH(ports, port_cfg_t *, p, {
3102 if (p->type == CONN_TYPE_CONTROL_LISTENER)
3103 smartlist_add(launch, p);
3104 });
3105 } else {
3106 smartlist_add_all(launch, ports);
3107 }
3108
3109 /* Iterate through old_conns, comparing it to launch: remove from both lists
3110 * each pair of elements that corresponds to the same port. */
3111 SMARTLIST_FOREACH_BEGIN(old_conns, connection_t *, conn) {
3112 const port_cfg_t *found_port = NULL;
3113
3114 /* Okay, so this is a listener. Is it configured? */
3115 /* That is, is it either: 1) exact match - address and port
3116 * pair match exactly between old listener and new port; or 2)
3117 * wildcard match - port matches exactly, but *one* of the
3118 * addresses is wildcard (0.0.0.0 or ::)?
3119 */
3120 SMARTLIST_FOREACH_BEGIN(launch, const port_cfg_t *, wanted) {
3121 if (conn->type != wanted->type)
3122 continue;
3123 if ((conn->socket_family != AF_UNIX && wanted->is_unix_addr) ||
3124 (conn->socket_family == AF_UNIX && ! wanted->is_unix_addr))
3125 continue;
3126
3127 if (wanted->server_cfg.no_listen)
3128 continue; /* We don't want to open a listener for this one */
3129
3130 if (wanted->is_unix_addr) {
3131 if (conn->socket_family == AF_UNIX &&
3132 !strcmp(wanted->unix_addr, conn->address)) {
3133 found_port = wanted;
3134 break;
3135 }
3136 } else {
3137 /* Numeric values of old and new port match exactly. */
3138 const int port_matches_exact = (wanted->port == conn->port);
3139 /* Ports match semantically - either their specific values
3140 match exactly, or new port is 'auto'.
3141 */
3142 const int port_matches = (wanted->port == CFG_AUTO_PORT ||
3143 port_matches_exact);
3144
3145 if (port_matches && tor_addr_eq(&wanted->addr, &conn->addr)) {
3146 found_port = wanted;
3147 break;
3148 }
3149 #ifdef ENABLE_LISTENER_REBIND
3150 /* Rebinding may be needed if all of the following are true:
3151 * 1) Address family is the same in old and new listeners.
3152 * 2) Port number matches exactly (numeric value is the same).
3153 * 3) *One* of listeners (either old one or new one) has a
3154 * wildcard IP address (0.0.0.0 or [::]).
3155 *
3156 * These are the exact conditions for a first bind() syscall
3157 * to fail with EADDRINUSE.
3158 */
3159 const int may_need_rebind =
3160 tor_addr_family(&wanted->addr) == tor_addr_family(&conn->addr) &&
3161 port_matches_exact && bool_neq(tor_addr_is_null(&wanted->addr),
3162 tor_addr_is_null(&conn->addr));
3163 if (replacements && may_need_rebind) {
3164 listener_replacement_t *replacement =
3165 tor_malloc(sizeof(listener_replacement_t));
3166
3167 replacement->old_conn = conn;
3168 replacement->new_port = wanted;
3169 smartlist_add(replacements, replacement);
3170
3171 SMARTLIST_DEL_CURRENT(launch, wanted);
3172 SMARTLIST_DEL_CURRENT(old_conns, conn);
3173 break;
3174 }
3175 #endif /* defined(ENABLE_LISTENER_REBIND) */
3176 }
3177 } SMARTLIST_FOREACH_END(wanted);
3178
3179 if (found_port) {
3180 /* This listener is already running; we don't need to launch it. */
3181 //log_debug(LD_NET, "Already have %s on %s:%d",
3182 // conn_type_to_string(found_port->type), conn->address, conn->port);
3183 smartlist_remove(launch, found_port);
3184 /* And we can remove the connection from old_conns too. */
3185 SMARTLIST_DEL_CURRENT(old_conns, conn);
3186 }
3187 } SMARTLIST_FOREACH_END(conn);
3188
3189 /* Now open all the listeners that are configured but not opened. */
3190 SMARTLIST_FOREACH_BEGIN(launch, const port_cfg_t *, port) {
3191 int skip = 0;
3192 connection_t *conn = connection_listener_new_for_port(port, &skip, NULL);
3193
3194 if (conn && new_conns)
3195 smartlist_add(new_conns, conn);
3196 else if (!skip)
3197 r = -1;
3198 } SMARTLIST_FOREACH_END(port);
3199
3200 smartlist_free(launch);
3201
3202 return r;
3203 }
3204
3205 /** Launch listeners for each port you should have open. Only launch
3206 * listeners who are not already open, and only close listeners we no longer
3207 * want.
3208 *
3209 * Add all new connections to <b>new_conns</b>.
3210 *
3211 * If <b>close_all_noncontrol</b> is true, then we only open control
3212 * listeners, and we close all other listeners.
3213 */
3214 int
retry_all_listeners(smartlist_t * new_conns,int close_all_noncontrol)3215 retry_all_listeners(smartlist_t *new_conns, int close_all_noncontrol)
3216 {
3217 smartlist_t *listeners = smartlist_new();
3218 smartlist_t *replacements = smartlist_new();
3219 const or_options_t *options = get_options();
3220 int retval = 0;
3221 const uint16_t old_or_port = routerconf_find_or_port(options, AF_INET);
3222 const uint16_t old_or_port_ipv6 =
3223 routerconf_find_or_port(options,AF_INET6);
3224 const uint16_t old_dir_port = routerconf_find_dir_port(options, 0);
3225
3226 SMARTLIST_FOREACH_BEGIN(get_connection_array(), connection_t *, conn) {
3227 if (connection_is_listener(conn) && !conn->marked_for_close)
3228 smartlist_add(listeners, conn);
3229 } SMARTLIST_FOREACH_END(conn);
3230
3231 if (retry_listener_ports(listeners,
3232 get_configured_ports(),
3233 new_conns,
3234 replacements,
3235 close_all_noncontrol) < 0)
3236 retval = -1;
3237
3238 #ifdef ENABLE_LISTENER_REBIND
3239 if (smartlist_len(replacements))
3240 log_debug(LD_NET, "%d replacements - starting rebinding loop.",
3241 smartlist_len(replacements));
3242
3243 SMARTLIST_FOREACH_BEGIN(replacements, listener_replacement_t *, r) {
3244 int addr_in_use = 0;
3245 int skip = 0;
3246
3247 tor_assert(r->new_port);
3248 tor_assert(r->old_conn);
3249
3250 connection_t *new_conn =
3251 connection_listener_new_for_port(r->new_port, &skip, &addr_in_use);
3252 connection_t *old_conn = r->old_conn;
3253
3254 if (skip) {
3255 log_debug(LD_NET, "Skipping creating new listener for %s",
3256 connection_describe(old_conn));
3257 continue;
3258 }
3259
3260 connection_close_immediate(old_conn);
3261 connection_mark_for_close(old_conn);
3262
3263 if (addr_in_use) {
3264 new_conn = connection_listener_new_for_port(r->new_port,
3265 &skip, &addr_in_use);
3266 }
3267
3268 /* There are many reasons why we can't open a new listener port so in case
3269 * we hit those, bail early so tor can stop. */
3270 if (!new_conn) {
3271 log_warn(LD_NET, "Unable to create listener port: %s:%d",
3272 fmt_and_decorate_addr(&r->new_port->addr), r->new_port->port);
3273 retval = -1;
3274 break;
3275 }
3276
3277 smartlist_add(new_conns, new_conn);
3278
3279 char *old_desc = tor_strdup(connection_describe(old_conn));
3280 log_notice(LD_NET, "Closed no-longer-configured %s "
3281 "(replaced by %s)",
3282 old_desc, connection_describe(new_conn));
3283 tor_free(old_desc);
3284 } SMARTLIST_FOREACH_END(r);
3285 #endif /* defined(ENABLE_LISTENER_REBIND) */
3286
3287 /* Any members that were still in 'listeners' don't correspond to
3288 * any configured port. Kill 'em. */
3289 SMARTLIST_FOREACH_BEGIN(listeners, connection_t *, conn) {
3290 log_notice(LD_NET, "Closing no-longer-configured %s on %s:%d",
3291 conn_type_to_string(conn->type),
3292 fmt_and_decorate_addr(&conn->addr), conn->port);
3293 connection_close_immediate(conn);
3294 connection_mark_for_close(conn);
3295 } SMARTLIST_FOREACH_END(conn);
3296
3297 smartlist_free(listeners);
3298 /* Cleanup any remaining listener replacement. */
3299 SMARTLIST_FOREACH(replacements, listener_replacement_t *, r, tor_free(r));
3300 smartlist_free(replacements);
3301
3302 if (old_or_port != routerconf_find_or_port(options, AF_INET) ||
3303 old_or_port_ipv6 != routerconf_find_or_port(options, AF_INET6) ||
3304 old_dir_port != routerconf_find_dir_port(options, 0)) {
3305 /* Our chosen ORPort or DirPort is not what it used to be: the
3306 * descriptor we had (if any) should be regenerated. (We won't
3307 * automatically notice this because of changes in the option,
3308 * since the value could be "auto".) */
3309 mark_my_descriptor_dirty("Chosen Or/DirPort changed");
3310 }
3311
3312 return retval;
3313 }
3314
3315 /** Mark every listener of type other than CONTROL_LISTENER to be closed. */
3316 void
connection_mark_all_noncontrol_listeners(void)3317 connection_mark_all_noncontrol_listeners(void)
3318 {
3319 SMARTLIST_FOREACH_BEGIN(get_connection_array(), connection_t *, conn) {
3320 if (conn->marked_for_close)
3321 continue;
3322 if (conn->type == CONN_TYPE_CONTROL_LISTENER)
3323 continue;
3324 if (connection_is_listener(conn))
3325 connection_mark_for_close(conn);
3326 } SMARTLIST_FOREACH_END(conn);
3327 }
3328
3329 /** Mark every external connection not used for controllers for close. */
3330 void
connection_mark_all_noncontrol_connections(void)3331 connection_mark_all_noncontrol_connections(void)
3332 {
3333 SMARTLIST_FOREACH_BEGIN(get_connection_array(), connection_t *, conn) {
3334 if (conn->marked_for_close)
3335 continue;
3336 switch (conn->type) {
3337 case CONN_TYPE_CONTROL_LISTENER:
3338 case CONN_TYPE_CONTROL:
3339 break;
3340 case CONN_TYPE_AP:
3341 connection_mark_unattached_ap(TO_ENTRY_CONN(conn),
3342 END_STREAM_REASON_HIBERNATING);
3343 break;
3344 case CONN_TYPE_OR:
3345 {
3346 or_connection_t *orconn = TO_OR_CONN(conn);
3347 if (orconn->chan) {
3348 connection_or_close_normally(orconn, 0);
3349 } else {
3350 /*
3351 * There should have been one, but mark for close and hope
3352 * for the best..
3353 */
3354 connection_mark_for_close(conn);
3355 }
3356 }
3357 break;
3358 default:
3359 connection_mark_for_close(conn);
3360 break;
3361 }
3362 } SMARTLIST_FOREACH_END(conn);
3363 }
3364
3365 /** Return 1 if we should apply rate limiting to <b>conn</b>, and 0
3366 * otherwise.
3367 * Right now this just checks if it's an internal IP address or an
3368 * internal connection. We also should, but don't, check if the connection
3369 * uses pluggable transports, since we should then limit it even if it
3370 * comes from an internal IP address. */
3371 static int
connection_is_rate_limited(const connection_t * conn)3372 connection_is_rate_limited(const connection_t *conn)
3373 {
3374 const or_options_t *options = get_options();
3375 if (conn->linked)
3376 return 0; /* Internal connection */
3377 else if (! options->CountPrivateBandwidth &&
3378 ! conn->always_rate_limit_as_remote &&
3379 (tor_addr_family(&conn->addr) == AF_UNSPEC || /* no address */
3380 tor_addr_family(&conn->addr) == AF_UNIX || /* no address */
3381 tor_addr_is_internal(&conn->addr, 0)))
3382 return 0; /* Internal address */
3383 else
3384 return 1;
3385 }
3386
3387 /** When was either global write bucket last empty? If this was recent, then
3388 * we're probably low on bandwidth, and we should be stingy with our bandwidth
3389 * usage. */
3390 static time_t write_buckets_last_empty_at = -100;
3391
3392 /** How many seconds of no active local circuits will make the
3393 * connection revert to the "relayed" bandwidth class? */
3394 #define CLIENT_IDLE_TIME_FOR_PRIORITY 30
3395
3396 /** Return 1 if <b>conn</b> should use tokens from the "relayed"
3397 * bandwidth rates, else 0. Currently, only OR conns with bandwidth
3398 * class 1, and directory conns that are serving data out, count.
3399 */
3400 static int
connection_counts_as_relayed_traffic(connection_t * conn,time_t now)3401 connection_counts_as_relayed_traffic(connection_t *conn, time_t now)
3402 {
3403 if (conn->type == CONN_TYPE_OR &&
3404 connection_or_client_used(TO_OR_CONN(conn)) +
3405 CLIENT_IDLE_TIME_FOR_PRIORITY < now)
3406 return 1;
3407 if (conn->type == CONN_TYPE_DIR && DIR_CONN_IS_SERVER(conn))
3408 return 1;
3409 return 0;
3410 }
3411
3412 /** Helper function to decide how many bytes out of <b>global_bucket</b>
3413 * we're willing to use for this transaction. <b>base</b> is the size
3414 * of a cell on the network; <b>priority</b> says whether we should
3415 * write many of them or just a few; and <b>conn_bucket</b> (if
3416 * non-negative) provides an upper limit for our answer. */
3417 static ssize_t
connection_bucket_get_share(int base,int priority,ssize_t global_bucket_val,ssize_t conn_bucket)3418 connection_bucket_get_share(int base, int priority,
3419 ssize_t global_bucket_val, ssize_t conn_bucket)
3420 {
3421 ssize_t at_most;
3422 ssize_t num_bytes_high = (priority ? 32 : 16) * base;
3423 ssize_t num_bytes_low = (priority ? 4 : 2) * base;
3424
3425 /* Do a rudimentary limiting so one circuit can't hog a connection.
3426 * Pick at most 32 cells, at least 4 cells if possible, and if we're in
3427 * the middle pick 1/8 of the available bandwidth. */
3428 at_most = global_bucket_val / 8;
3429 at_most -= (at_most % base); /* round down */
3430 if (at_most > num_bytes_high) /* 16 KB, or 8 KB for low-priority */
3431 at_most = num_bytes_high;
3432 else if (at_most < num_bytes_low) /* 2 KB, or 1 KB for low-priority */
3433 at_most = num_bytes_low;
3434
3435 if (at_most > global_bucket_val)
3436 at_most = global_bucket_val;
3437
3438 if (conn_bucket >= 0 && at_most > conn_bucket)
3439 at_most = conn_bucket;
3440
3441 if (at_most < 0)
3442 return 0;
3443 return at_most;
3444 }
3445
3446 /** How many bytes at most can we read onto this connection? */
3447 static ssize_t
connection_bucket_read_limit(connection_t * conn,time_t now)3448 connection_bucket_read_limit(connection_t *conn, time_t now)
3449 {
3450 int base = RELAY_PAYLOAD_SIZE;
3451 int priority = conn->type != CONN_TYPE_DIR;
3452 ssize_t conn_bucket = -1;
3453 size_t global_bucket_val = token_bucket_rw_get_read(&global_bucket);
3454 if (global_bucket_val == 0) {
3455 /* We reached our global read limit: count this as an overload.
3456 *
3457 * The token bucket is always initialized (see connection_bucket_init() and
3458 * options_validate_relay_bandwidth()) and hence we can assume that if the
3459 * token ever hits zero, it's a limit that got popped and not the bucket
3460 * being uninitialized.
3461 */
3462 rep_hist_note_overload(OVERLOAD_READ);
3463 }
3464
3465 if (connection_speaks_cells(conn)) {
3466 or_connection_t *or_conn = TO_OR_CONN(conn);
3467 if (conn->state == OR_CONN_STATE_OPEN)
3468 conn_bucket = token_bucket_rw_get_read(&or_conn->bucket);
3469 base = get_cell_network_size(or_conn->wide_circ_ids);
3470 }
3471
3472 /* Edge connection have their own read bucket due to flow control being able
3473 * to set a rate limit for them. However, for exit connections, we still need
3474 * to honor the global bucket as well. */
3475 if (CONN_IS_EDGE(conn)) {
3476 const edge_connection_t *edge_conn = CONST_TO_EDGE_CONN(conn);
3477 conn_bucket = token_bucket_rw_get_read(&edge_conn->bucket);
3478 if (conn->type == CONN_TYPE_EXIT) {
3479 /* Decide between our limit and the global one. */
3480 goto end;
3481 }
3482 return conn_bucket;
3483 }
3484
3485 if (!connection_is_rate_limited(conn)) {
3486 /* be willing to read on local conns even if our buckets are empty */
3487 return conn_bucket>=0 ? conn_bucket : 1<<14;
3488 }
3489
3490 if (connection_counts_as_relayed_traffic(conn, now)) {
3491 size_t relayed = token_bucket_rw_get_read(&global_relayed_bucket);
3492 global_bucket_val = MIN(global_bucket_val, relayed);
3493 }
3494
3495 end:
3496 return connection_bucket_get_share(base, priority,
3497 global_bucket_val, conn_bucket);
3498 }
3499
3500 /** How many bytes at most can we write onto this connection? */
3501 ssize_t
connection_bucket_write_limit(connection_t * conn,time_t now)3502 connection_bucket_write_limit(connection_t *conn, time_t now)
3503 {
3504 int base = RELAY_PAYLOAD_SIZE;
3505 int priority = conn->type != CONN_TYPE_DIR;
3506 size_t conn_bucket = buf_datalen(conn->outbuf);
3507 size_t global_bucket_val = token_bucket_rw_get_write(&global_bucket);
3508 if (global_bucket_val == 0) {
3509 /* We reached our global write limit: We should count this as an overload.
3510 * See above function for more information */
3511 rep_hist_note_overload(OVERLOAD_WRITE);
3512 }
3513
3514 if (!connection_is_rate_limited(conn)) {
3515 /* be willing to write to local conns even if our buckets are empty */
3516 return conn_bucket;
3517 }
3518
3519 if (connection_speaks_cells(conn)) {
3520 /* use the per-conn write limit if it's lower */
3521 or_connection_t *or_conn = TO_OR_CONN(conn);
3522 if (conn->state == OR_CONN_STATE_OPEN)
3523 conn_bucket = MIN(conn_bucket,
3524 token_bucket_rw_get_write(&or_conn->bucket));
3525 base = get_cell_network_size(or_conn->wide_circ_ids);
3526 }
3527
3528 if (connection_counts_as_relayed_traffic(conn, now)) {
3529 size_t relayed = token_bucket_rw_get_write(&global_relayed_bucket);
3530 global_bucket_val = MIN(global_bucket_val, relayed);
3531 }
3532
3533 return connection_bucket_get_share(base, priority,
3534 global_bucket_val, conn_bucket);
3535 }
3536
3537 /** Return true iff the global write buckets are low enough that we
3538 * shouldn't send <b>attempt</b> bytes of low-priority directory stuff
3539 * out to <b>conn</b>.
3540 *
3541 * If we are a directory authority, always answer dir requests thus true is
3542 * always returned.
3543 *
3544 * Note: There are a lot of parameters we could use here:
3545 * - global_relayed_write_bucket. Low is bad.
3546 * - global_write_bucket. Low is bad.
3547 * - bandwidthrate. Low is bad.
3548 * - bandwidthburst. Not a big factor?
3549 * - attempt. High is bad.
3550 * - total bytes queued on outbufs. High is bad. But I'm wary of
3551 * using this, since a few slow-flushing queues will pump up the
3552 * number without meaning what we meant to mean. What we really
3553 * mean is "total directory bytes added to outbufs recently", but
3554 * that's harder to quantify and harder to keep track of.
3555 */
3556 bool
connection_dir_is_global_write_low(const connection_t * conn,size_t attempt)3557 connection_dir_is_global_write_low(const connection_t *conn, size_t attempt)
3558 {
3559 size_t smaller_bucket =
3560 MIN(token_bucket_rw_get_write(&global_bucket),
3561 token_bucket_rw_get_write(&global_relayed_bucket));
3562
3563 /* Special case for authorities (directory only). */
3564 if (authdir_mode_v3(get_options())) {
3565 /* Are we configured to possibly reject requests under load? */
3566 if (!dirauth_should_reject_requests_under_load()) {
3567 /* Answer request no matter what. */
3568 return false;
3569 }
3570 /* Always answer requests from a known relay which includes the other
3571 * authorities. The following looks up the addresses for relays that we
3572 * have their descriptor _and_ any configured trusted directories. */
3573 if (nodelist_probably_contains_address(&conn->addr)) {
3574 return false;
3575 }
3576 }
3577
3578 if (!connection_is_rate_limited(conn))
3579 return false; /* local conns don't get limited */
3580
3581 if (smaller_bucket < attempt)
3582 return true; /* not enough space. */
3583
3584 {
3585 const time_t diff = approx_time() - write_buckets_last_empty_at;
3586 if (diff <= 1)
3587 return true; /* we're already hitting our limits, no more please */
3588 }
3589 return false;
3590 }
3591
3592 /** When did we last tell the accounting subsystem about transmitted
3593 * bandwidth? */
3594 static time_t last_recorded_accounting_at = 0;
3595
3596 /** Helper: adjusts our bandwidth history and informs the controller as
3597 * appropriate, given that we have just read <b>num_read</b> bytes and written
3598 * <b>num_written</b> bytes on <b>conn</b>. */
3599 static void
record_num_bytes_transferred_impl(connection_t * conn,time_t now,size_t num_read,size_t num_written)3600 record_num_bytes_transferred_impl(connection_t *conn,
3601 time_t now, size_t num_read, size_t num_written)
3602 {
3603 /* Count bytes of answering direct and tunneled directory requests */
3604 if (conn->type == CONN_TYPE_DIR && conn->purpose == DIR_PURPOSE_SERVER) {
3605 if (num_read > 0)
3606 bwhist_note_dir_bytes_read(num_read, now);
3607 if (num_written > 0)
3608 bwhist_note_dir_bytes_written(num_written, now);
3609 }
3610
3611 /* Linked connections and internal IPs aren't counted for statistics or
3612 * accounting:
3613 * - counting linked connections would double-count BEGINDIR bytes, because
3614 * they are sent as Dir bytes on the linked connection, and OR bytes on
3615 * the OR connection;
3616 * - relays and clients don't connect to internal IPs, unless specifically
3617 * configured to do so. If they are configured that way, we don't count
3618 * internal bytes.
3619 */
3620 if (!connection_is_rate_limited(conn))
3621 return;
3622
3623 const bool is_ipv6 = (conn->socket_family == AF_INET6);
3624 if (conn->type == CONN_TYPE_OR)
3625 conn_stats_note_or_conn_bytes(conn->global_identifier, num_read,
3626 num_written, now, is_ipv6);
3627
3628 if (num_read > 0) {
3629 bwhist_note_bytes_read(num_read, now, is_ipv6);
3630 }
3631 if (num_written > 0) {
3632 bwhist_note_bytes_written(num_written, now, is_ipv6);
3633 }
3634 if (conn->type == CONN_TYPE_EXIT)
3635 rep_hist_note_exit_bytes(conn->port, num_written, num_read);
3636
3637 /* Remember these bytes towards statistics. */
3638 stats_increment_bytes_read_and_written(num_read, num_written);
3639
3640 /* Remember these bytes towards accounting. */
3641 if (accounting_is_enabled(get_options())) {
3642 if (now > last_recorded_accounting_at && last_recorded_accounting_at) {
3643 accounting_add_bytes(num_read, num_written,
3644 (int)(now - last_recorded_accounting_at));
3645 } else {
3646 accounting_add_bytes(num_read, num_written, 0);
3647 }
3648 last_recorded_accounting_at = now;
3649 }
3650 }
3651
3652 /** We just read <b>num_read</b> and wrote <b>num_written</b> bytes
3653 * onto <b>conn</b>. Decrement buckets appropriately. */
3654 static void
connection_buckets_decrement(connection_t * conn,time_t now,size_t num_read,size_t num_written)3655 connection_buckets_decrement(connection_t *conn, time_t now,
3656 size_t num_read, size_t num_written)
3657 {
3658 if (num_written >= INT_MAX || num_read >= INT_MAX) {
3659 log_err(LD_BUG, "Value out of range. num_read=%lu, num_written=%lu, "
3660 "connection type=%s, state=%s",
3661 (unsigned long)num_read, (unsigned long)num_written,
3662 conn_type_to_string(conn->type),
3663 conn_state_to_string(conn->type, conn->state));
3664 tor_assert_nonfatal_unreached();
3665 if (num_written >= INT_MAX)
3666 num_written = 1;
3667 if (num_read >= INT_MAX)
3668 num_read = 1;
3669 }
3670
3671 record_num_bytes_transferred_impl(conn, now, num_read, num_written);
3672
3673 /* Edge connection need to decrement the read side of the bucket used by our
3674 * congestion control. */
3675 if (CONN_IS_EDGE(conn) && num_read > 0) {
3676 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
3677 token_bucket_rw_dec(&edge_conn->bucket, num_read, 0);
3678 }
3679
3680 if (!connection_is_rate_limited(conn))
3681 return; /* local IPs are free */
3682
3683 unsigned flags = 0;
3684 if (connection_counts_as_relayed_traffic(conn, now)) {
3685 flags = token_bucket_rw_dec(&global_relayed_bucket, num_read, num_written);
3686 }
3687 flags |= token_bucket_rw_dec(&global_bucket, num_read, num_written);
3688
3689 if (flags & TB_WRITE) {
3690 write_buckets_last_empty_at = now;
3691 }
3692 if (connection_speaks_cells(conn) && conn->state == OR_CONN_STATE_OPEN) {
3693 or_connection_t *or_conn = TO_OR_CONN(conn);
3694 token_bucket_rw_dec(&or_conn->bucket, num_read, num_written);
3695 }
3696 }
3697
3698 /**
3699 * Mark <b>conn</b> as needing to stop reading because bandwidth has been
3700 * exhausted. If <b>is_global_bw</b>, it is closing because global bandwidth
3701 * limit has been exhausted. Otherwise, it is closing because its own
3702 * bandwidth limit has been exhausted.
3703 */
3704 void
connection_read_bw_exhausted(connection_t * conn,bool is_global_bw)3705 connection_read_bw_exhausted(connection_t *conn, bool is_global_bw)
3706 {
3707 (void)is_global_bw;
3708 conn->read_blocked_on_bw = 1;
3709 connection_stop_reading(conn);
3710 reenable_blocked_connection_schedule();
3711 }
3712
3713 /**
3714 * Mark <b>conn</b> as needing to stop reading because write bandwidth has
3715 * been exhausted. If <b>is_global_bw</b>, it is closing because global
3716 * bandwidth limit has been exhausted. Otherwise, it is closing because its
3717 * own bandwidth limit has been exhausted.
3718 */
3719 void
connection_write_bw_exhausted(connection_t * conn,bool is_global_bw)3720 connection_write_bw_exhausted(connection_t *conn, bool is_global_bw)
3721 {
3722 (void)is_global_bw;
3723 conn->write_blocked_on_bw = 1;
3724 connection_stop_writing(conn);
3725 reenable_blocked_connection_schedule();
3726 }
3727
3728 /** If we have exhausted our global buckets, or the buckets for conn,
3729 * stop reading. */
3730 void
connection_consider_empty_read_buckets(connection_t * conn)3731 connection_consider_empty_read_buckets(connection_t *conn)
3732 {
3733 int is_global = 1;
3734 const char *reason;
3735
3736 if (CONN_IS_EDGE(conn) &&
3737 token_bucket_rw_get_read(&TO_EDGE_CONN(conn)->bucket) <= 0) {
3738 reason = "edge connection read bucket exhausted. Pausing.";
3739 is_global = false;
3740 } else if (!connection_is_rate_limited(conn)) {
3741 return; /* Always okay. */
3742 } else if (token_bucket_rw_get_read(&global_bucket) <= 0) {
3743 reason = "global read bucket exhausted. Pausing.";
3744 } else if (connection_counts_as_relayed_traffic(conn, approx_time()) &&
3745 token_bucket_rw_get_read(&global_relayed_bucket) <= 0) {
3746 reason = "global relayed read bucket exhausted. Pausing.";
3747 } else if (connection_speaks_cells(conn) &&
3748 conn->state == OR_CONN_STATE_OPEN &&
3749 token_bucket_rw_get_read(&TO_OR_CONN(conn)->bucket) <= 0) {
3750 reason = "connection read bucket exhausted. Pausing.";
3751 is_global = false;
3752 } else {
3753 return; /* all good, no need to stop it */
3754 }
3755
3756 LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "%s", reason));
3757 connection_read_bw_exhausted(conn, is_global);
3758 }
3759
3760 /** If we have exhausted our global buckets, or the buckets for conn,
3761 * stop writing. */
3762 void
connection_consider_empty_write_buckets(connection_t * conn)3763 connection_consider_empty_write_buckets(connection_t *conn)
3764 {
3765 const char *reason;
3766
3767 if (!connection_is_rate_limited(conn))
3768 return; /* Always okay. */
3769
3770 bool is_global = true;
3771 if (token_bucket_rw_get_write(&global_bucket) <= 0) {
3772 reason = "global write bucket exhausted. Pausing.";
3773 } else if (connection_counts_as_relayed_traffic(conn, approx_time()) &&
3774 token_bucket_rw_get_write(&global_relayed_bucket) <= 0) {
3775 reason = "global relayed write bucket exhausted. Pausing.";
3776 } else if (connection_speaks_cells(conn) &&
3777 conn->state == OR_CONN_STATE_OPEN &&
3778 token_bucket_rw_get_write(&TO_OR_CONN(conn)->bucket) <= 0) {
3779 reason = "connection write bucket exhausted. Pausing.";
3780 is_global = false;
3781 } else
3782 return; /* all good, no need to stop it */
3783
3784 LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "%s", reason));
3785 connection_write_bw_exhausted(conn, is_global);
3786 }
3787
3788 /** Initialize the global buckets to the values configured in the
3789 * options */
3790 void
connection_bucket_init(void)3791 connection_bucket_init(void)
3792 {
3793 const or_options_t *options = get_options();
3794 const uint32_t now_ts = monotime_coarse_get_stamp();
3795 token_bucket_rw_init(&global_bucket,
3796 (int32_t)options->BandwidthRate,
3797 (int32_t)options->BandwidthBurst,
3798 now_ts);
3799 if (options->RelayBandwidthRate) {
3800 token_bucket_rw_init(&global_relayed_bucket,
3801 (int32_t)options->RelayBandwidthRate,
3802 (int32_t)options->RelayBandwidthBurst,
3803 now_ts);
3804 } else {
3805 token_bucket_rw_init(&global_relayed_bucket,
3806 (int32_t)options->BandwidthRate,
3807 (int32_t)options->BandwidthBurst,
3808 now_ts);
3809 }
3810
3811 reenable_blocked_connection_init(options);
3812 }
3813
3814 /** Update the global connection bucket settings to a new value. */
3815 void
connection_bucket_adjust(const or_options_t * options)3816 connection_bucket_adjust(const or_options_t *options)
3817 {
3818 token_bucket_rw_adjust(&global_bucket,
3819 (int32_t)options->BandwidthRate,
3820 (int32_t)options->BandwidthBurst);
3821 if (options->RelayBandwidthRate) {
3822 token_bucket_rw_adjust(&global_relayed_bucket,
3823 (int32_t)options->RelayBandwidthRate,
3824 (int32_t)options->RelayBandwidthBurst);
3825 } else {
3826 token_bucket_rw_adjust(&global_relayed_bucket,
3827 (int32_t)options->BandwidthRate,
3828 (int32_t)options->BandwidthBurst);
3829 }
3830 }
3831
3832 /**
3833 * Cached value of the last coarse-timestamp when we refilled the
3834 * global buckets.
3835 */
3836 static uint32_t last_refilled_global_buckets_ts=0;
3837 /**
3838 * Refill the token buckets for a single connection <b>conn</b>, and the
3839 * global token buckets as appropriate. Requires that <b>now_ts</b> is
3840 * the time in coarse timestamp units.
3841 */
3842 static void
connection_bucket_refill_single(connection_t * conn,uint32_t now_ts)3843 connection_bucket_refill_single(connection_t *conn, uint32_t now_ts)
3844 {
3845 /* Note that we only check for equality here: the underlying
3846 * token bucket functions can handle moving backwards in time if they
3847 * need to. */
3848 if (now_ts != last_refilled_global_buckets_ts) {
3849 token_bucket_rw_refill(&global_bucket, now_ts);
3850 token_bucket_rw_refill(&global_relayed_bucket, now_ts);
3851 last_refilled_global_buckets_ts = now_ts;
3852 }
3853
3854 if (connection_speaks_cells(conn) && conn->state == OR_CONN_STATE_OPEN) {
3855 or_connection_t *or_conn = TO_OR_CONN(conn);
3856 token_bucket_rw_refill(&or_conn->bucket, now_ts);
3857 }
3858
3859 if (CONN_IS_EDGE(conn)) {
3860 token_bucket_rw_refill(&TO_EDGE_CONN(conn)->bucket, now_ts);
3861 }
3862 }
3863
3864 /**
3865 * Event to re-enable all connections that were previously blocked on read or
3866 * write.
3867 */
3868 static mainloop_event_t *reenable_blocked_connections_ev = NULL;
3869
3870 /** True iff reenable_blocked_connections_ev is currently scheduled. */
3871 static int reenable_blocked_connections_is_scheduled = 0;
3872
3873 /** Delay after which to run reenable_blocked_connections_ev. */
3874 static struct timeval reenable_blocked_connections_delay;
3875
3876 /**
3877 * Re-enable all connections that were previously blocked on read or write.
3878 * This event is scheduled after enough time has elapsed to be sure
3879 * that the buckets will refill when the connections have something to do.
3880 */
3881 static void
reenable_blocked_connections_cb(mainloop_event_t * ev,void * arg)3882 reenable_blocked_connections_cb(mainloop_event_t *ev, void *arg)
3883 {
3884 (void)ev;
3885 (void)arg;
3886 SMARTLIST_FOREACH_BEGIN(get_connection_array(), connection_t *, conn) {
3887 if (conn->read_blocked_on_bw == 1) {
3888 connection_start_reading(conn);
3889 conn->read_blocked_on_bw = 0;
3890 }
3891 if (conn->write_blocked_on_bw == 1) {
3892 connection_start_writing(conn);
3893 conn->write_blocked_on_bw = 0;
3894 }
3895 } SMARTLIST_FOREACH_END(conn);
3896
3897 reenable_blocked_connections_is_scheduled = 0;
3898 }
3899
3900 /**
3901 * Initialize the mainloop event that we use to wake up connections that
3902 * find themselves blocked on bandwidth.
3903 */
3904 static void
reenable_blocked_connection_init(const or_options_t * options)3905 reenable_blocked_connection_init(const or_options_t *options)
3906 {
3907 if (! reenable_blocked_connections_ev) {
3908 reenable_blocked_connections_ev =
3909 mainloop_event_new(reenable_blocked_connections_cb, NULL);
3910 reenable_blocked_connections_is_scheduled = 0;
3911 }
3912 time_t sec = options->TokenBucketRefillInterval / 1000;
3913 int msec = (options->TokenBucketRefillInterval % 1000);
3914 reenable_blocked_connections_delay.tv_sec = sec;
3915 reenable_blocked_connections_delay.tv_usec = msec * 1000;
3916 }
3917
3918 /**
3919 * Called when we have blocked a connection for being low on bandwidth:
3920 * schedule an event to reenable such connections, if it is not already
3921 * scheduled.
3922 */
3923 static void
reenable_blocked_connection_schedule(void)3924 reenable_blocked_connection_schedule(void)
3925 {
3926 if (reenable_blocked_connections_is_scheduled)
3927 return;
3928 if (BUG(reenable_blocked_connections_ev == NULL)) {
3929 reenable_blocked_connection_init(get_options());
3930 }
3931 mainloop_event_schedule(reenable_blocked_connections_ev,
3932 &reenable_blocked_connections_delay);
3933 reenable_blocked_connections_is_scheduled = 1;
3934 }
3935
3936 /** Read bytes from conn-\>s and process them.
3937 *
3938 * It calls connection_buf_read_from_socket() to bring in any new bytes,
3939 * and then calls connection_process_inbuf() to process them.
3940 *
3941 * Mark the connection and return -1 if you want to close it, else
3942 * return 0.
3943 */
3944 static int
connection_handle_read_impl(connection_t * conn)3945 connection_handle_read_impl(connection_t *conn)
3946 {
3947 ssize_t max_to_read=-1, try_to_read;
3948 size_t before, n_read = 0;
3949 int socket_error = 0;
3950
3951 if (conn->marked_for_close)
3952 return 0; /* do nothing */
3953
3954 conn->timestamp_last_read_allowed = approx_time();
3955
3956 connection_bucket_refill_single(conn, monotime_coarse_get_stamp());
3957
3958 switch (conn->type) {
3959 case CONN_TYPE_OR_LISTENER:
3960 return connection_handle_listener_read(conn, CONN_TYPE_OR);
3961 case CONN_TYPE_EXT_OR_LISTENER:
3962 return connection_handle_listener_read(conn, CONN_TYPE_EXT_OR);
3963 case CONN_TYPE_AP_LISTENER:
3964 case CONN_TYPE_AP_TRANS_LISTENER:
3965 case CONN_TYPE_AP_NATD_LISTENER:
3966 case CONN_TYPE_AP_HTTP_CONNECT_LISTENER:
3967 return connection_handle_listener_read(conn, CONN_TYPE_AP);
3968 case CONN_TYPE_DIR_LISTENER:
3969 return connection_handle_listener_read(conn, CONN_TYPE_DIR);
3970 case CONN_TYPE_CONTROL_LISTENER:
3971 return connection_handle_listener_read(conn, CONN_TYPE_CONTROL);
3972 case CONN_TYPE_METRICS_LISTENER:
3973 return connection_handle_listener_read(conn, CONN_TYPE_METRICS);
3974 case CONN_TYPE_AP_DNS_LISTENER:
3975 /* This should never happen; eventdns.c handles the reads here. */
3976 tor_fragile_assert();
3977 return 0;
3978 }
3979
3980 loop_again:
3981 try_to_read = max_to_read;
3982 tor_assert(!conn->marked_for_close);
3983
3984 before = buf_datalen(conn->inbuf);
3985 if (connection_buf_read_from_socket(conn, &max_to_read, &socket_error) < 0) {
3986 /* There's a read error; kill the connection.*/
3987 if (conn->type == CONN_TYPE_OR) {
3988 connection_or_notify_error(TO_OR_CONN(conn),
3989 socket_error != 0 ?
3990 errno_to_orconn_end_reason(socket_error) :
3991 END_OR_CONN_REASON_CONNRESET,
3992 socket_error != 0 ?
3993 tor_socket_strerror(socket_error) :
3994 "(unknown, errno was 0)");
3995 }
3996 if (CONN_IS_EDGE(conn)) {
3997 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
3998 connection_edge_end_errno(edge_conn);
3999 if (conn->type == CONN_TYPE_AP && TO_ENTRY_CONN(conn)->socks_request) {
4000 /* broken, don't send a socks reply back */
4001 TO_ENTRY_CONN(conn)->socks_request->has_finished = 1;
4002 }
4003 }
4004 connection_close_immediate(conn); /* Don't flush; connection is dead. */
4005 /*
4006 * This can bypass normal channel checking since we did
4007 * connection_or_notify_error() above.
4008 */
4009 connection_mark_for_close_internal(conn);
4010 return -1;
4011 }
4012 n_read += buf_datalen(conn->inbuf) - before;
4013 if (CONN_IS_EDGE(conn) && try_to_read != max_to_read) {
4014 /* instruct it not to try to package partial cells. */
4015 if (connection_process_inbuf(conn, 0) < 0) {
4016 return -1;
4017 }
4018 if (!conn->marked_for_close &&
4019 connection_is_reading(conn) &&
4020 !conn->inbuf_reached_eof &&
4021 max_to_read > 0)
4022 goto loop_again; /* try reading again, in case more is here now */
4023 }
4024 /* one last try, packaging partial cells and all. */
4025 if (!conn->marked_for_close &&
4026 connection_process_inbuf(conn, 1) < 0) {
4027 return -1;
4028 }
4029 if (conn->linked_conn) {
4030 /* The other side's handle_write() will never actually get called, so
4031 * we need to invoke the appropriate callbacks ourself. */
4032 connection_t *linked = conn->linked_conn;
4033
4034 if (n_read) {
4035 /* Probably a no-op, since linked conns typically don't count for
4036 * bandwidth rate limiting. But do it anyway so we can keep stats
4037 * accurately. Note that since we read the bytes from conn, and
4038 * we're writing the bytes onto the linked connection, we count
4039 * these as <i>written</i> bytes. */
4040 connection_buckets_decrement(linked, approx_time(), 0, n_read);
4041
4042 if (connection_flushed_some(linked) < 0)
4043 connection_mark_for_close(linked);
4044 if (!connection_wants_to_flush(linked))
4045 connection_finished_flushing(linked);
4046 }
4047
4048 if (!buf_datalen(linked->outbuf) && conn->active_on_link)
4049 connection_stop_reading_from_linked_conn(conn);
4050 }
4051 /* If we hit the EOF, call connection_reached_eof(). */
4052 if (!conn->marked_for_close &&
4053 conn->inbuf_reached_eof &&
4054 connection_reached_eof(conn) < 0) {
4055 return -1;
4056 }
4057 return 0;
4058 }
4059
4060 /* DOCDOC connection_handle_read */
4061 int
connection_handle_read(connection_t * conn)4062 connection_handle_read(connection_t *conn)
4063 {
4064 int res;
4065 update_current_time(time(NULL));
4066 res = connection_handle_read_impl(conn);
4067 return res;
4068 }
4069
4070 /** Pull in new bytes from conn-\>s or conn-\>linked_conn onto conn-\>inbuf,
4071 * either directly or via TLS. Reduce the token buckets by the number of bytes
4072 * read.
4073 *
4074 * If *max_to_read is -1, then decide it ourselves, else go with the
4075 * value passed to us. When returning, if it's changed, subtract the
4076 * number of bytes we read from *max_to_read.
4077 *
4078 * Return -1 if we want to break conn, else return 0.
4079 */
4080 static int
connection_buf_read_from_socket(connection_t * conn,ssize_t * max_to_read,int * socket_error)4081 connection_buf_read_from_socket(connection_t *conn, ssize_t *max_to_read,
4082 int *socket_error)
4083 {
4084 int result;
4085 ssize_t at_most = *max_to_read;
4086 size_t slack_in_buf, more_to_read;
4087 size_t n_read = 0, n_written = 0;
4088
4089 if (at_most == -1) { /* we need to initialize it */
4090 /* how many bytes are we allowed to read? */
4091 at_most = connection_bucket_read_limit(conn, approx_time());
4092 }
4093
4094 /* Do not allow inbuf to grow past BUF_MAX_LEN. */
4095 const ssize_t maximum = BUF_MAX_LEN - buf_datalen(conn->inbuf);
4096 if (at_most > maximum) {
4097 at_most = maximum;
4098 }
4099
4100 slack_in_buf = buf_slack(conn->inbuf);
4101 again:
4102 if ((size_t)at_most > slack_in_buf && slack_in_buf >= 1024) {
4103 more_to_read = at_most - slack_in_buf;
4104 at_most = slack_in_buf;
4105 } else {
4106 more_to_read = 0;
4107 }
4108
4109 if (connection_speaks_cells(conn) &&
4110 conn->state > OR_CONN_STATE_PROXY_HANDSHAKING) {
4111 int pending;
4112 or_connection_t *or_conn = TO_OR_CONN(conn);
4113 size_t initial_size;
4114 if (conn->state == OR_CONN_STATE_TLS_HANDSHAKING ||
4115 conn->state == OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING) {
4116 /* continue handshaking even if global token bucket is empty */
4117 return connection_tls_continue_handshake(or_conn);
4118 }
4119
4120 log_debug(LD_NET,
4121 "%d: starting, inbuf_datalen %ld (%d pending in tls object)."
4122 " at_most %ld.",
4123 (int)conn->s,(long)buf_datalen(conn->inbuf),
4124 tor_tls_get_pending_bytes(or_conn->tls), (long)at_most);
4125
4126 initial_size = buf_datalen(conn->inbuf);
4127 /* else open, or closing */
4128 result = buf_read_from_tls(conn->inbuf, or_conn->tls, at_most);
4129 if (TOR_TLS_IS_ERROR(result) || result == TOR_TLS_CLOSE)
4130 or_conn->tls_error = result;
4131 else
4132 or_conn->tls_error = 0;
4133
4134 switch (result) {
4135 case TOR_TLS_CLOSE:
4136 case TOR_TLS_ERROR_IO:
4137 log_debug(LD_NET,"TLS %s closed %son read. Closing.",
4138 connection_describe(conn),
4139 result == TOR_TLS_CLOSE ? "cleanly " : "");
4140 return result;
4141 CASE_TOR_TLS_ERROR_ANY_NONIO:
4142 log_debug(LD_NET,"tls error [%s] from %s. Breaking.",
4143 tor_tls_err_to_string(result),
4144 connection_describe(conn));
4145 return result;
4146 case TOR_TLS_WANTWRITE:
4147 connection_start_writing(conn);
4148 return 0;
4149 case TOR_TLS_WANTREAD:
4150 if (conn->in_connection_handle_write) {
4151 /* We've been invoked from connection_handle_write, because we're
4152 * waiting for a TLS renegotiation, the renegotiation started, and
4153 * SSL_read returned WANTWRITE. But now SSL_read is saying WANTREAD
4154 * again. Stop waiting for write events now, or else we'll
4155 * busy-loop until data arrives for us to read.
4156 * XXX: remove this when v2 handshakes support is dropped. */
4157 connection_stop_writing(conn);
4158 if (!connection_is_reading(conn))
4159 connection_start_reading(conn);
4160 }
4161 /* we're already reading, one hopes */
4162 break;
4163 case TOR_TLS_DONE: /* no data read, so nothing to process */
4164 break; /* so we call bucket_decrement below */
4165 default:
4166 break;
4167 }
4168 pending = tor_tls_get_pending_bytes(or_conn->tls);
4169 if (pending) {
4170 /* If we have any pending bytes, we read them now. This *can*
4171 * take us over our read allotment, but really we shouldn't be
4172 * believing that SSL bytes are the same as TCP bytes anyway. */
4173 int r2 = buf_read_from_tls(conn->inbuf, or_conn->tls, pending);
4174 if (BUG(r2<0)) {
4175 log_warn(LD_BUG, "apparently, reading pending bytes can fail.");
4176 return -1;
4177 }
4178 }
4179 result = (int)(buf_datalen(conn->inbuf)-initial_size);
4180 tor_tls_get_n_raw_bytes(or_conn->tls, &n_read, &n_written);
4181 log_debug(LD_GENERAL, "After TLS read of %d: %ld read, %ld written",
4182 result, (long)n_read, (long)n_written);
4183 } else if (conn->linked) {
4184 if (conn->linked_conn) {
4185 result = (int) buf_move_all(conn->inbuf, conn->linked_conn->outbuf);
4186 } else {
4187 result = 0;
4188 }
4189 //log_notice(LD_GENERAL, "Moved %d bytes on an internal link!", result);
4190 /* If the other side has disappeared, or if it's been marked for close and
4191 * we flushed its outbuf, then we should set our inbuf_reached_eof. */
4192 if (!conn->linked_conn ||
4193 (conn->linked_conn->marked_for_close &&
4194 buf_datalen(conn->linked_conn->outbuf) == 0))
4195 conn->inbuf_reached_eof = 1;
4196
4197 n_read = (size_t) result;
4198 } else {
4199 /* !connection_speaks_cells, !conn->linked_conn. */
4200 int reached_eof = 0;
4201 CONN_LOG_PROTECT(conn,
4202 result = buf_read_from_socket(conn->inbuf, conn->s,
4203 at_most,
4204 &reached_eof,
4205 socket_error));
4206 if (reached_eof)
4207 conn->inbuf_reached_eof = 1;
4208
4209 // log_fn(LOG_DEBUG,"read_to_buf returned %d.",read_result);
4210
4211 if (result < 0)
4212 return -1;
4213 n_read = (size_t) result;
4214 }
4215
4216 if (n_read > 0) {
4217 /* change *max_to_read */
4218 *max_to_read = at_most - n_read;
4219
4220 /* Onion service application connection. Note read bytes for metrics. */
4221 if (CONN_IS_EDGE(conn) && TO_EDGE_CONN(conn)->hs_ident) {
4222 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
4223 hs_metrics_app_read_bytes(&edge_conn->hs_ident->identity_pk,
4224 edge_conn->hs_ident->orig_virtual_port,
4225 n_read);
4226 }
4227
4228 /* Update edge_conn->n_read */
4229 if (conn->type == CONN_TYPE_AP) {
4230 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
4231
4232 /* Check for overflow: */
4233 if (PREDICT_LIKELY(UINT32_MAX - edge_conn->n_read > n_read))
4234 edge_conn->n_read += (int)n_read;
4235 else
4236 edge_conn->n_read = UINT32_MAX;
4237 }
4238
4239 /* If CONN_BW events are enabled, update conn->n_read_conn_bw for
4240 * OR/DIR/EXIT connections, checking for overflow. */
4241 if (get_options()->TestingEnableConnBwEvent &&
4242 (conn->type == CONN_TYPE_OR ||
4243 conn->type == CONN_TYPE_DIR ||
4244 conn->type == CONN_TYPE_EXIT)) {
4245 if (PREDICT_LIKELY(UINT32_MAX - conn->n_read_conn_bw > n_read))
4246 conn->n_read_conn_bw += (int)n_read;
4247 else
4248 conn->n_read_conn_bw = UINT32_MAX;
4249 }
4250 }
4251
4252 connection_buckets_decrement(conn, approx_time(), n_read, n_written);
4253
4254 if (more_to_read && result == at_most) {
4255 slack_in_buf = buf_slack(conn->inbuf);
4256 at_most = more_to_read;
4257 goto again;
4258 }
4259
4260 /* Call even if result is 0, since the global read bucket may
4261 * have reached 0 on a different conn, and this connection needs to
4262 * know to stop reading. */
4263 connection_consider_empty_read_buckets(conn);
4264 if (n_written > 0 && connection_is_writing(conn))
4265 connection_consider_empty_write_buckets(conn);
4266
4267 return 0;
4268 }
4269
4270 /** A pass-through to fetch_from_buf. */
4271 int
connection_buf_get_bytes(char * string,size_t len,connection_t * conn)4272 connection_buf_get_bytes(char *string, size_t len, connection_t *conn)
4273 {
4274 return buf_get_bytes(conn->inbuf, string, len);
4275 }
4276
4277 /** As buf_get_line(), but read from a connection's input buffer. */
4278 int
connection_buf_get_line(connection_t * conn,char * data,size_t * data_len)4279 connection_buf_get_line(connection_t *conn, char *data,
4280 size_t *data_len)
4281 {
4282 return buf_get_line(conn->inbuf, data, data_len);
4283 }
4284
4285 /** As fetch_from_buf_http, but fetches from a connection's input buffer_t as
4286 * appropriate. */
4287 int
connection_fetch_from_buf_http(connection_t * conn,char ** headers_out,size_t max_headerlen,char ** body_out,size_t * body_used,size_t max_bodylen,int force_complete)4288 connection_fetch_from_buf_http(connection_t *conn,
4289 char **headers_out, size_t max_headerlen,
4290 char **body_out, size_t *body_used,
4291 size_t max_bodylen, int force_complete)
4292 {
4293 return fetch_from_buf_http(conn->inbuf, headers_out, max_headerlen,
4294 body_out, body_used, max_bodylen, force_complete);
4295 }
4296
4297 /** Return true if this connection has data to flush. */
4298 int
connection_wants_to_flush(connection_t * conn)4299 connection_wants_to_flush(connection_t *conn)
4300 {
4301 return connection_get_outbuf_len(conn) > 0;
4302 }
4303
4304 /** Are there too many bytes on edge connection <b>conn</b>'s outbuf to
4305 * send back a relay-level sendme yet? Return 1 if so, 0 if not. Used by
4306 * connection_edge_consider_sending_sendme().
4307 */
4308 int
connection_outbuf_too_full(connection_t * conn)4309 connection_outbuf_too_full(connection_t *conn)
4310 {
4311 return connection_get_outbuf_len(conn) > 10*CELL_PAYLOAD_SIZE;
4312 }
4313
4314 /**
4315 * On Windows Vista and Windows 7, tune the send buffer size according to a
4316 * hint from the OS.
4317 *
4318 * This should help fix slow upload rates.
4319 */
4320 static void
update_send_buffer_size(tor_socket_t sock)4321 update_send_buffer_size(tor_socket_t sock)
4322 {
4323 #ifdef _WIN32
4324 /* We only do this on Vista and 7, because earlier versions of Windows
4325 * don't have the SIO_IDEAL_SEND_BACKLOG_QUERY functionality, and on
4326 * later versions it isn't necessary. */
4327 static int isVistaOr7 = -1;
4328 if (isVistaOr7 == -1) {
4329 isVistaOr7 = 0;
4330 OSVERSIONINFO osvi = { 0 };
4331 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
4332 GetVersionEx(&osvi);
4333 if (osvi.dwMajorVersion == 6 && osvi.dwMinorVersion < 2)
4334 isVistaOr7 = 1;
4335 }
4336 if (!isVistaOr7)
4337 return;
4338 if (get_options()->ConstrainedSockets)
4339 return;
4340 ULONG isb = 0;
4341 DWORD bytesReturned = 0;
4342 if (!WSAIoctl(sock, SIO_IDEAL_SEND_BACKLOG_QUERY, NULL, 0,
4343 &isb, sizeof(isb), &bytesReturned, NULL, NULL)) {
4344 setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (const char*)&isb, sizeof(isb));
4345 }
4346 #else /* !defined(_WIN32) */
4347 (void) sock;
4348 #endif /* defined(_WIN32) */
4349 }
4350
4351 /** Try to flush more bytes onto <b>conn</b>-\>s.
4352 *
4353 * This function is called in connection_handle_write(), which gets
4354 * called from conn_write_callback() in main.c when libevent tells us
4355 * that <b>conn</b> wants to write.
4356 *
4357 * Update <b>conn</b>-\>timestamp_last_write_allowed to now, and call flush_buf
4358 * or flush_buf_tls appropriately. If it succeeds and there are no more
4359 * more bytes on <b>conn</b>-\>outbuf, then call connection_finished_flushing
4360 * on it too.
4361 *
4362 * If <b>force</b>, then write as many bytes as possible, ignoring bandwidth
4363 * limits. (Used for flushing messages to controller connections on fatal
4364 * errors.)
4365 *
4366 * Mark the connection and return -1 if you want to close it, else
4367 * return 0.
4368 */
4369 static int
connection_handle_write_impl(connection_t * conn,int force)4370 connection_handle_write_impl(connection_t *conn, int force)
4371 {
4372 int e;
4373 socklen_t len=(socklen_t)sizeof(e);
4374 int result;
4375 ssize_t max_to_write;
4376 time_t now = approx_time();
4377 size_t n_read = 0, n_written = 0;
4378 int dont_stop_writing = 0;
4379
4380 tor_assert(!connection_is_listener(conn));
4381
4382 if (conn->marked_for_close || !SOCKET_OK(conn->s))
4383 return 0; /* do nothing */
4384
4385 if (conn->in_flushed_some) {
4386 log_warn(LD_BUG, "called recursively from inside conn->in_flushed_some");
4387 return 0;
4388 }
4389
4390 conn->timestamp_last_write_allowed = now;
4391
4392 connection_bucket_refill_single(conn, monotime_coarse_get_stamp());
4393
4394 /* Sometimes, "writable" means "connected". */
4395 if (connection_state_is_connecting(conn)) {
4396 if (getsockopt(conn->s, SOL_SOCKET, SO_ERROR, (void*)&e, &len) < 0) {
4397 log_warn(LD_BUG, "getsockopt() syscall failed");
4398 if (conn->type == CONN_TYPE_OR) {
4399 or_connection_t *orconn = TO_OR_CONN(conn);
4400 connection_or_close_for_error(orconn, 0);
4401 } else {
4402 if (CONN_IS_EDGE(conn)) {
4403 connection_edge_end_errno(TO_EDGE_CONN(conn));
4404 }
4405 connection_mark_for_close(conn);
4406 }
4407 return -1;
4408 }
4409 if (e) {
4410 /* some sort of error, but maybe just inprogress still */
4411 if (!ERRNO_IS_CONN_EINPROGRESS(e)) {
4412 log_info(LD_NET,"in-progress connect failed. Removing. (%s)",
4413 tor_socket_strerror(e));
4414 if (CONN_IS_EDGE(conn))
4415 connection_edge_end_errno(TO_EDGE_CONN(conn));
4416 if (conn->type == CONN_TYPE_OR)
4417 connection_or_notify_error(TO_OR_CONN(conn),
4418 errno_to_orconn_end_reason(e),
4419 tor_socket_strerror(e));
4420
4421 connection_close_immediate(conn);
4422 /*
4423 * This can bypass normal channel checking since we did
4424 * connection_or_notify_error() above.
4425 */
4426 connection_mark_for_close_internal(conn);
4427 return -1;
4428 } else {
4429 return 0; /* no change, see if next time is better */
4430 }
4431 }
4432 /* The connection is successful. */
4433 if (connection_finished_connecting(conn)<0)
4434 return -1;
4435 }
4436
4437 max_to_write = force ? (ssize_t)buf_datalen(conn->outbuf)
4438 : connection_bucket_write_limit(conn, now);
4439
4440 if (connection_speaks_cells(conn) &&
4441 conn->state > OR_CONN_STATE_PROXY_HANDSHAKING) {
4442 or_connection_t *or_conn = TO_OR_CONN(conn);
4443 size_t initial_size;
4444 if (conn->state == OR_CONN_STATE_TLS_HANDSHAKING ||
4445 conn->state == OR_CONN_STATE_TLS_CLIENT_RENEGOTIATING) {
4446 connection_stop_writing(conn);
4447 if (connection_tls_continue_handshake(or_conn) < 0) {
4448 /* Don't flush; connection is dead. */
4449 connection_or_notify_error(or_conn,
4450 END_OR_CONN_REASON_MISC,
4451 "TLS error in connection_tls_"
4452 "continue_handshake()");
4453 connection_close_immediate(conn);
4454 /*
4455 * This can bypass normal channel checking since we did
4456 * connection_or_notify_error() above.
4457 */
4458 connection_mark_for_close_internal(conn);
4459 return -1;
4460 }
4461 return 0;
4462 } else if (conn->state == OR_CONN_STATE_TLS_SERVER_RENEGOTIATING) {
4463 return connection_handle_read(conn);
4464 }
4465
4466 /* else open, or closing */
4467 initial_size = buf_datalen(conn->outbuf);
4468 result = buf_flush_to_tls(conn->outbuf, or_conn->tls,
4469 max_to_write);
4470
4471 if (result >= 0)
4472 update_send_buffer_size(conn->s);
4473
4474 /* If we just flushed the last bytes, tell the channel on the
4475 * or_conn to check if it needs to geoip_change_dirreq_state() */
4476 /* XXXX move this to flushed_some or finished_flushing -NM */
4477 if (buf_datalen(conn->outbuf) == 0 && or_conn->chan)
4478 channel_notify_flushed(TLS_CHAN_TO_BASE(or_conn->chan));
4479
4480 switch (result) {
4481 CASE_TOR_TLS_ERROR_ANY:
4482 case TOR_TLS_CLOSE:
4483 or_conn->tls_error = result;
4484 log_info(LD_NET, result != TOR_TLS_CLOSE ?
4485 "tls error. breaking.":"TLS connection closed on flush");
4486 /* Don't flush; connection is dead. */
4487 connection_or_notify_error(or_conn,
4488 END_OR_CONN_REASON_MISC,
4489 result != TOR_TLS_CLOSE ?
4490 "TLS error in during flush" :
4491 "TLS closed during flush");
4492 connection_close_immediate(conn);
4493 /*
4494 * This can bypass normal channel checking since we did
4495 * connection_or_notify_error() above.
4496 */
4497 connection_mark_for_close_internal(conn);
4498 return -1;
4499 case TOR_TLS_WANTWRITE:
4500 log_debug(LD_NET,"wanted write.");
4501 /* we're already writing */
4502 dont_stop_writing = 1;
4503 break;
4504 case TOR_TLS_WANTREAD:
4505 /* Make sure to avoid a loop if the receive buckets are empty. */
4506 log_debug(LD_NET,"wanted read.");
4507 if (!connection_is_reading(conn)) {
4508 connection_write_bw_exhausted(conn, true);
4509 /* we'll start reading again when we get more tokens in our
4510 * read bucket; then we'll start writing again too.
4511 */
4512 }
4513 /* else no problem, we're already reading */
4514 return 0;
4515 /* case TOR_TLS_DONE:
4516 * for TOR_TLS_DONE, fall through to check if the flushlen
4517 * is empty, so we can stop writing.
4518 */
4519 }
4520
4521 tor_tls_get_n_raw_bytes(or_conn->tls, &n_read, &n_written);
4522 log_debug(LD_GENERAL, "After TLS write of %d: %ld read, %ld written",
4523 result, (long)n_read, (long)n_written);
4524 or_conn->bytes_xmitted += result;
4525 or_conn->bytes_xmitted_by_tls += n_written;
4526 /* So we notice bytes were written even on error */
4527 /* XXXX This cast is safe since we can never write INT_MAX bytes in a
4528 * single set of TLS operations. But it looks kinda ugly. If we refactor
4529 * the *_buf_tls functions, we should make them return ssize_t or size_t
4530 * or something. */
4531 result = (int)(initial_size-buf_datalen(conn->outbuf));
4532 } else {
4533 CONN_LOG_PROTECT(conn,
4534 result = buf_flush_to_socket(conn->outbuf, conn->s,
4535 max_to_write));
4536 if (result < 0) {
4537 if (CONN_IS_EDGE(conn))
4538 connection_edge_end_errno(TO_EDGE_CONN(conn));
4539 if (conn->type == CONN_TYPE_AP) {
4540 /* writing failed; we couldn't send a SOCKS reply if we wanted to */
4541 TO_ENTRY_CONN(conn)->socks_request->has_finished = 1;
4542 }
4543
4544 connection_close_immediate(conn); /* Don't flush; connection is dead. */
4545 connection_mark_for_close(conn);
4546 return -1;
4547 }
4548 update_send_buffer_size(conn->s);
4549 n_written = (size_t) result;
4550 }
4551
4552 if (n_written && conn->type == CONN_TYPE_AP) {
4553 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
4554
4555 /* Check for overflow: */
4556 if (PREDICT_LIKELY(UINT32_MAX - edge_conn->n_written > n_written))
4557 edge_conn->n_written += (int)n_written;
4558 else
4559 edge_conn->n_written = UINT32_MAX;
4560 }
4561
4562 /* If CONN_BW events are enabled, update conn->n_written_conn_bw for
4563 * OR/DIR/EXIT connections, checking for overflow. */
4564 if (n_written && get_options()->TestingEnableConnBwEvent &&
4565 (conn->type == CONN_TYPE_OR ||
4566 conn->type == CONN_TYPE_DIR ||
4567 conn->type == CONN_TYPE_EXIT)) {
4568 if (PREDICT_LIKELY(UINT32_MAX - conn->n_written_conn_bw > n_written))
4569 conn->n_written_conn_bw += (int)n_written;
4570 else
4571 conn->n_written_conn_bw = UINT32_MAX;
4572 }
4573
4574 connection_buckets_decrement(conn, approx_time(), n_read, n_written);
4575
4576 if (result > 0) {
4577 /* If we wrote any bytes from our buffer, then call the appropriate
4578 * functions. */
4579 if (connection_flushed_some(conn) < 0) {
4580 if (connection_speaks_cells(conn)) {
4581 connection_or_notify_error(TO_OR_CONN(conn),
4582 END_OR_CONN_REASON_MISC,
4583 "Got error back from "
4584 "connection_flushed_some()");
4585 }
4586
4587 /*
4588 * This can bypass normal channel checking since we did
4589 * connection_or_notify_error() above.
4590 */
4591 connection_mark_for_close_internal(conn);
4592 }
4593 }
4594
4595 if (!connection_wants_to_flush(conn) &&
4596 !dont_stop_writing) { /* it's done flushing */
4597 if (connection_finished_flushing(conn) < 0) {
4598 /* already marked */
4599 goto err;
4600 }
4601 goto done;
4602 }
4603
4604 /* Call even if result is 0, since the global write bucket may
4605 * have reached 0 on a different conn, and this connection needs to
4606 * know to stop writing. */
4607 connection_consider_empty_write_buckets(conn);
4608 if (n_read > 0 && connection_is_reading(conn))
4609 connection_consider_empty_read_buckets(conn);
4610
4611 done:
4612 /* If this is an edge connection with congestion control, check to see
4613 * if it is time to send an xon */
4614 if (conn_uses_flow_control(conn)) {
4615 flow_control_decide_xon(TO_EDGE_CONN(conn), n_written);
4616 }
4617
4618 return 0;
4619
4620 err:
4621 return -1;
4622 }
4623
4624 /* DOCDOC connection_handle_write */
4625 int
connection_handle_write(connection_t * conn,int force)4626 connection_handle_write(connection_t *conn, int force)
4627 {
4628 int res;
4629 update_current_time(time(NULL));
4630 /* connection_handle_write_impl() might call connection_handle_read()
4631 * if we're in the middle of a v2 handshake, in which case it needs this
4632 * flag set. */
4633 conn->in_connection_handle_write = 1;
4634 res = connection_handle_write_impl(conn, force);
4635 conn->in_connection_handle_write = 0;
4636 return res;
4637 }
4638
4639 /**
4640 * Try to flush data that's waiting for a write on <b>conn</b>. Return
4641 * -1 on failure, 0 on success.
4642 *
4643 * Don't use this function for regular writing; the buffers
4644 * system should be good enough at scheduling writes there. Instead, this
4645 * function is for cases when we're about to exit or something and we want
4646 * to report it right away.
4647 */
4648 int
connection_flush(connection_t * conn)4649 connection_flush(connection_t *conn)
4650 {
4651 return connection_handle_write(conn, 1);
4652 }
4653
4654 /** Helper for connection_write_to_buf_impl and connection_write_buf_to_buf:
4655 *
4656 * Return true iff it is okay to queue bytes on <b>conn</b>'s outbuf for
4657 * writing.
4658 */
4659 static int
connection_may_write_to_buf(connection_t * conn)4660 connection_may_write_to_buf(connection_t *conn)
4661 {
4662 /* if it's marked for close, only allow write if we mean to flush it */
4663 if (conn->marked_for_close && !conn->hold_open_until_flushed)
4664 return 0;
4665
4666 return 1;
4667 }
4668
4669 /** Helper for connection_write_to_buf_impl and connection_write_buf_to_buf:
4670 *
4671 * Called when an attempt to add bytes on <b>conn</b>'s outbuf has failed;
4672 * mark the connection and warn as appropriate.
4673 */
4674 static void
connection_write_to_buf_failed(connection_t * conn)4675 connection_write_to_buf_failed(connection_t *conn)
4676 {
4677 if (CONN_IS_EDGE(conn)) {
4678 /* if it failed, it means we have our package/delivery windows set
4679 wrong compared to our max outbuf size. close the whole circuit. */
4680 log_warn(LD_NET,
4681 "write_to_buf failed. Closing circuit (fd %d).", (int)conn->s);
4682 circuit_mark_for_close(circuit_get_by_edge_conn(TO_EDGE_CONN(conn)),
4683 END_CIRC_REASON_INTERNAL);
4684 } else if (conn->type == CONN_TYPE_OR) {
4685 or_connection_t *orconn = TO_OR_CONN(conn);
4686 log_warn(LD_NET,
4687 "write_to_buf failed on an orconn; notifying of error "
4688 "(fd %d)", (int)(conn->s));
4689 connection_or_close_for_error(orconn, 0);
4690 } else {
4691 log_warn(LD_NET,
4692 "write_to_buf failed. Closing connection (fd %d).",
4693 (int)conn->s);
4694 connection_mark_for_close(conn);
4695 }
4696 }
4697
4698 /** Helper for connection_write_to_buf_impl and connection_write_buf_to_buf:
4699 *
4700 * Called when an attempt to add bytes on <b>conn</b>'s outbuf has succeeded:
4701 * start writing if appropriate.
4702 */
4703 static void
connection_write_to_buf_commit(connection_t * conn)4704 connection_write_to_buf_commit(connection_t *conn)
4705 {
4706 /* If we receive optimistic data in the EXIT_CONN_STATE_RESOLVING
4707 * state, we don't want to try to write it right away, since
4708 * conn->write_event won't be set yet. Otherwise, write data from
4709 * this conn as the socket is available. */
4710 if (conn->write_event) {
4711 connection_start_writing(conn);
4712 }
4713 }
4714
4715 /** Append <b>len</b> bytes of <b>string</b> onto <b>conn</b>'s
4716 * outbuf, and ask it to start writing.
4717 *
4718 * If <b>zlib</b> is nonzero, this is a directory connection that should get
4719 * its contents compressed or decompressed as they're written. If zlib is
4720 * negative, this is the last data to be compressed, and the connection's zlib
4721 * state should be flushed.
4722 */
4723 MOCK_IMPL(void,
4724 connection_write_to_buf_impl_,(const char *string, size_t len,
4725 connection_t *conn, int zlib))
4726 {
4727 /* XXXX This function really needs to return -1 on failure. */
4728 int r;
4729 if (!len && !(zlib<0))
4730 return;
4731
4732 if (!connection_may_write_to_buf(conn))
4733 return;
4734
4735 if (zlib) {
4736 dir_connection_t *dir_conn = TO_DIR_CONN(conn);
4737 int done = zlib < 0;
4738 CONN_LOG_PROTECT(conn, r = buf_add_compress(conn->outbuf,
4739 dir_conn->compress_state,
4740 string, len, done));
4741 } else {
4742 CONN_LOG_PROTECT(conn, r = buf_add(conn->outbuf, string, len));
4743 }
4744 if (r < 0) {
4745 connection_write_to_buf_failed(conn);
4746 return;
4747 }
4748 connection_write_to_buf_commit(conn);
4749 }
4750
4751 /**
4752 * Write a <b>string</b> (of size <b>len</b> to directory connection
4753 * <b>dir_conn</b>. Apply compression if connection is configured to use
4754 * it and finalize it if <b>done</b> is true.
4755 */
4756 void
connection_dir_buf_add(const char * string,size_t len,dir_connection_t * dir_conn,int done)4757 connection_dir_buf_add(const char *string, size_t len,
4758 dir_connection_t *dir_conn, int done)
4759 {
4760 if (dir_conn->compress_state != NULL) {
4761 connection_buf_add_compress(string, len, dir_conn, done);
4762 return;
4763 }
4764
4765 connection_buf_add(string, len, TO_CONN(dir_conn));
4766 }
4767
4768 void
connection_buf_add_compress(const char * string,size_t len,dir_connection_t * conn,int done)4769 connection_buf_add_compress(const char *string, size_t len,
4770 dir_connection_t *conn, int done)
4771 {
4772 connection_write_to_buf_impl_(string, len, TO_CONN(conn), done ? -1 : 1);
4773 }
4774
4775 /**
4776 * Add all bytes from <b>buf</b> to <b>conn</b>'s outbuf, draining them
4777 * from <b>buf</b>. (If the connection is marked and will soon be closed,
4778 * nothing is drained.)
4779 */
4780 void
connection_buf_add_buf(connection_t * conn,buf_t * buf)4781 connection_buf_add_buf(connection_t *conn, buf_t *buf)
4782 {
4783 tor_assert(conn);
4784 tor_assert(buf);
4785 size_t len = buf_datalen(buf);
4786 if (len == 0)
4787 return;
4788
4789 if (!connection_may_write_to_buf(conn))
4790 return;
4791
4792 buf_move_all(conn->outbuf, buf);
4793 connection_write_to_buf_commit(conn);
4794 }
4795
4796 #define CONN_GET_ALL_TEMPLATE(var, test) \
4797 STMT_BEGIN \
4798 smartlist_t *conns = get_connection_array(); \
4799 smartlist_t *ret_conns = smartlist_new(); \
4800 SMARTLIST_FOREACH_BEGIN(conns, connection_t *, var) { \
4801 if (var && (test) && !var->marked_for_close) \
4802 smartlist_add(ret_conns, var); \
4803 } SMARTLIST_FOREACH_END(var); \
4804 return ret_conns; \
4805 STMT_END
4806
4807 /* Return a list of connections that aren't close and matches the given type
4808 * and state. The returned list can be empty and must be freed using
4809 * smartlist_free(). The caller does NOT have ownership of the objects in the
4810 * list so it must not free them nor reference them as they can disappear. */
4811 smartlist_t *
connection_list_by_type_state(int type,int state)4812 connection_list_by_type_state(int type, int state)
4813 {
4814 CONN_GET_ALL_TEMPLATE(conn, (conn->type == type && conn->state == state));
4815 }
4816
4817 /* Return a list of connections that aren't close and matches the given type
4818 * and purpose. The returned list can be empty and must be freed using
4819 * smartlist_free(). The caller does NOT have ownership of the objects in the
4820 * list so it must not free them nor reference them as they can disappear. */
4821 smartlist_t *
connection_list_by_type_purpose(int type,int purpose)4822 connection_list_by_type_purpose(int type, int purpose)
4823 {
4824 CONN_GET_ALL_TEMPLATE(conn,
4825 (conn->type == type && conn->purpose == purpose));
4826 }
4827
4828 /** Return a connection_t * from get_connection_array() that satisfies test on
4829 * var, and that is not marked for close. */
4830 #define CONN_GET_TEMPLATE(var, test) \
4831 STMT_BEGIN \
4832 smartlist_t *conns = get_connection_array(); \
4833 SMARTLIST_FOREACH(conns, connection_t *, var, \
4834 { \
4835 if (var && (test) && !var->marked_for_close) \
4836 return var; \
4837 }); \
4838 return NULL; \
4839 STMT_END
4840
4841 /** Return a connection with given type, address, port, and purpose;
4842 * or NULL if no such connection exists (or if all such connections are marked
4843 * for close). */
4844 MOCK_IMPL(connection_t *,
4845 connection_get_by_type_addr_port_purpose,(int type,
4846 const tor_addr_t *addr, uint16_t port,
4847 int purpose))
4848 {
4849 CONN_GET_TEMPLATE(conn,
4850 (conn->type == type &&
4851 tor_addr_eq(&conn->addr, addr) &&
4852 conn->port == port &&
4853 conn->purpose == purpose));
4854 }
4855
4856 /** Return the stream with id <b>id</b> if it is not already marked for
4857 * close.
4858 */
4859 connection_t *
connection_get_by_global_id(uint64_t id)4860 connection_get_by_global_id(uint64_t id)
4861 {
4862 CONN_GET_TEMPLATE(conn, conn->global_identifier == id);
4863 }
4864
4865 /** Return a connection of type <b>type</b> that is not marked for close.
4866 */
4867 connection_t *
connection_get_by_type(int type)4868 connection_get_by_type(int type)
4869 {
4870 CONN_GET_TEMPLATE(conn, conn->type == type);
4871 }
4872
4873 /** Return a connection of type <b>type</b> that is in state <b>state</b>,
4874 * and that is not marked for close.
4875 */
4876 connection_t *
connection_get_by_type_state(int type,int state)4877 connection_get_by_type_state(int type, int state)
4878 {
4879 CONN_GET_TEMPLATE(conn, conn->type == type && conn->state == state);
4880 }
4881
4882 /**
4883 * Return a connection of type <b>type</b> that is not an internally linked
4884 * connection, and is not marked for close.
4885 **/
4886 MOCK_IMPL(connection_t *,
4887 connection_get_by_type_nonlinked,(int type))
4888 {
4889 CONN_GET_TEMPLATE(conn, conn->type == type && !conn->linked);
4890 }
4891
4892 /** Return a new smartlist of dir_connection_t * from get_connection_array()
4893 * that satisfy conn_test on connection_t *conn_var, and dirconn_test on
4894 * dir_connection_t *dirconn_var. conn_var must be of CONN_TYPE_DIR and not
4895 * marked for close to be included in the list. */
4896 #define DIR_CONN_LIST_TEMPLATE(conn_var, conn_test, \
4897 dirconn_var, dirconn_test) \
4898 STMT_BEGIN \
4899 smartlist_t *conns = get_connection_array(); \
4900 smartlist_t *dir_conns = smartlist_new(); \
4901 SMARTLIST_FOREACH_BEGIN(conns, connection_t *, conn_var) { \
4902 if (conn_var && (conn_test) \
4903 && conn_var->type == CONN_TYPE_DIR \
4904 && !conn_var->marked_for_close) { \
4905 dir_connection_t *dirconn_var = TO_DIR_CONN(conn_var); \
4906 if (dirconn_var && (dirconn_test)) { \
4907 smartlist_add(dir_conns, dirconn_var); \
4908 } \
4909 } \
4910 } SMARTLIST_FOREACH_END(conn_var); \
4911 return dir_conns; \
4912 STMT_END
4913
4914 /** Return a list of directory connections that are fetching the item
4915 * described by <b>purpose</b>/<b>resource</b>. If there are none,
4916 * return an empty list. This list must be freed using smartlist_free,
4917 * but the pointers in it must not be freed.
4918 * Note that this list should not be cached, as the pointers in it can be
4919 * freed if their connections close. */
4920 smartlist_t *
connection_dir_list_by_purpose_and_resource(int purpose,const char * resource)4921 connection_dir_list_by_purpose_and_resource(
4922 int purpose,
4923 const char *resource)
4924 {
4925 DIR_CONN_LIST_TEMPLATE(conn,
4926 conn->purpose == purpose,
4927 dirconn,
4928 0 == strcmp_opt(resource,
4929 dirconn->requested_resource));
4930 }
4931
4932 /** Return a list of directory connections that are fetching the item
4933 * described by <b>purpose</b>/<b>resource</b>/<b>state</b>. If there are
4934 * none, return an empty list. This list must be freed using smartlist_free,
4935 * but the pointers in it must not be freed.
4936 * Note that this list should not be cached, as the pointers in it can be
4937 * freed if their connections close. */
4938 smartlist_t *
connection_dir_list_by_purpose_resource_and_state(int purpose,const char * resource,int state)4939 connection_dir_list_by_purpose_resource_and_state(
4940 int purpose,
4941 const char *resource,
4942 int state)
4943 {
4944 DIR_CONN_LIST_TEMPLATE(conn,
4945 conn->purpose == purpose && conn->state == state,
4946 dirconn,
4947 0 == strcmp_opt(resource,
4948 dirconn->requested_resource));
4949 }
4950
4951 #undef DIR_CONN_LIST_TEMPLATE
4952
4953 /** Return an arbitrary active OR connection that isn't <b>this_conn</b>.
4954 *
4955 * We use this to guess if we should tell the controller that we
4956 * didn't manage to connect to any of our bridges. */
4957 static connection_t *
connection_get_another_active_or_conn(const or_connection_t * this_conn)4958 connection_get_another_active_or_conn(const or_connection_t *this_conn)
4959 {
4960 CONN_GET_TEMPLATE(conn,
4961 conn != TO_CONN(this_conn) && conn->type == CONN_TYPE_OR);
4962 }
4963
4964 /** Return 1 if there are any active OR connections apart from
4965 * <b>this_conn</b>.
4966 *
4967 * We use this to guess if we should tell the controller that we
4968 * didn't manage to connect to any of our bridges. */
4969 int
any_other_active_or_conns(const or_connection_t * this_conn)4970 any_other_active_or_conns(const or_connection_t *this_conn)
4971 {
4972 connection_t *conn = connection_get_another_active_or_conn(this_conn);
4973 if (conn != NULL) {
4974 log_debug(LD_DIR, "%s: Found an OR connection: %s",
4975 __func__, connection_describe(conn));
4976 return 1;
4977 }
4978
4979 return 0;
4980 }
4981
4982 #undef CONN_GET_TEMPLATE
4983
4984 /** Return 1 if <b>conn</b> is a listener conn, else return 0. */
4985 int
connection_is_listener(connection_t * conn)4986 connection_is_listener(connection_t *conn)
4987 {
4988 if (conn->type == CONN_TYPE_OR_LISTENER ||
4989 conn->type == CONN_TYPE_EXT_OR_LISTENER ||
4990 conn->type == CONN_TYPE_AP_LISTENER ||
4991 conn->type == CONN_TYPE_AP_TRANS_LISTENER ||
4992 conn->type == CONN_TYPE_AP_DNS_LISTENER ||
4993 conn->type == CONN_TYPE_AP_NATD_LISTENER ||
4994 conn->type == CONN_TYPE_AP_HTTP_CONNECT_LISTENER ||
4995 conn->type == CONN_TYPE_DIR_LISTENER ||
4996 conn->type == CONN_TYPE_METRICS_LISTENER ||
4997 conn->type == CONN_TYPE_CONTROL_LISTENER)
4998 return 1;
4999 return 0;
5000 }
5001
5002 /** Return 1 if <b>conn</b> is in state "open" and is not marked
5003 * for close, else return 0.
5004 */
5005 int
connection_state_is_open(connection_t * conn)5006 connection_state_is_open(connection_t *conn)
5007 {
5008 tor_assert(conn);
5009
5010 if (conn->marked_for_close)
5011 return 0;
5012
5013 if ((conn->type == CONN_TYPE_OR && conn->state == OR_CONN_STATE_OPEN) ||
5014 (conn->type == CONN_TYPE_EXT_OR) ||
5015 (conn->type == CONN_TYPE_AP && conn->state == AP_CONN_STATE_OPEN) ||
5016 (conn->type == CONN_TYPE_EXIT && conn->state == EXIT_CONN_STATE_OPEN) ||
5017 (conn->type == CONN_TYPE_CONTROL &&
5018 conn->state == CONTROL_CONN_STATE_OPEN))
5019 return 1;
5020
5021 return 0;
5022 }
5023
5024 /** Return 1 if conn is in 'connecting' state, else return 0. */
5025 int
connection_state_is_connecting(connection_t * conn)5026 connection_state_is_connecting(connection_t *conn)
5027 {
5028 tor_assert(conn);
5029
5030 if (conn->marked_for_close)
5031 return 0;
5032 switch (conn->type)
5033 {
5034 case CONN_TYPE_OR:
5035 return conn->state == OR_CONN_STATE_CONNECTING;
5036 case CONN_TYPE_EXIT:
5037 return conn->state == EXIT_CONN_STATE_CONNECTING;
5038 case CONN_TYPE_DIR:
5039 return conn->state == DIR_CONN_STATE_CONNECTING;
5040 }
5041
5042 return 0;
5043 }
5044
5045 /** Allocates a base64'ed authenticator for use in http or https
5046 * auth, based on the input string <b>authenticator</b>. Returns it
5047 * if success, else returns NULL. */
5048 char *
alloc_http_authenticator(const char * authenticator)5049 alloc_http_authenticator(const char *authenticator)
5050 {
5051 /* an authenticator in Basic authentication
5052 * is just the string "username:password" */
5053 const size_t authenticator_length = strlen(authenticator);
5054 const size_t base64_authenticator_length =
5055 base64_encode_size(authenticator_length, 0) + 1;
5056 char *base64_authenticator = tor_malloc(base64_authenticator_length);
5057 if (base64_encode(base64_authenticator, base64_authenticator_length,
5058 authenticator, authenticator_length, 0) < 0) {
5059 tor_free(base64_authenticator); /* free and set to null */
5060 }
5061 return base64_authenticator;
5062 }
5063
5064 /** Given a socket handle, check whether the local address (sockname) of the
5065 * socket is one that we've connected from before. If so, double-check
5066 * whether our address has changed and we need to generate keys. If we do,
5067 * call init_keys().
5068 */
5069 static void
client_check_address_changed(tor_socket_t sock)5070 client_check_address_changed(tor_socket_t sock)
5071 {
5072 tor_addr_t out_addr, iface_addr;
5073 tor_addr_t **last_interface_ip_ptr;
5074 sa_family_t family;
5075
5076 if (!outgoing_addrs)
5077 outgoing_addrs = smartlist_new();
5078
5079 if (tor_addr_from_getsockname(&out_addr, sock) < 0) {
5080 int e = tor_socket_errno(sock);
5081 log_warn(LD_NET, "getsockname() to check for address change failed: %s",
5082 tor_socket_strerror(e));
5083 return;
5084 }
5085 family = tor_addr_family(&out_addr);
5086
5087 if (family == AF_INET)
5088 last_interface_ip_ptr = &last_interface_ipv4;
5089 else if (family == AF_INET6)
5090 last_interface_ip_ptr = &last_interface_ipv6;
5091 else
5092 return;
5093
5094 if (! *last_interface_ip_ptr) {
5095 tor_addr_t *a = tor_malloc_zero(sizeof(tor_addr_t));
5096 if (get_interface_address6(LOG_INFO, family, a)==0) {
5097 *last_interface_ip_ptr = a;
5098 } else {
5099 tor_free(a);
5100 }
5101 }
5102
5103 /* If we've used this address previously, we're okay. */
5104 SMARTLIST_FOREACH(outgoing_addrs, const tor_addr_t *, a_ptr,
5105 if (tor_addr_eq(a_ptr, &out_addr))
5106 return;
5107 );
5108
5109 /* Uh-oh. We haven't connected from this address before. Has the interface
5110 * address changed? */
5111 if (get_interface_address6(LOG_INFO, family, &iface_addr)<0)
5112 return;
5113
5114 if (tor_addr_eq(&iface_addr, *last_interface_ip_ptr)) {
5115 /* Nope, it hasn't changed. Add this address to the list. */
5116 smartlist_add(outgoing_addrs, tor_memdup(&out_addr, sizeof(tor_addr_t)));
5117 } else {
5118 /* The interface changed. We're a client, so we need to regenerate our
5119 * keys. First, reset the state. */
5120 log_notice(LD_NET, "Our IP address has changed. Rotating keys...");
5121 tor_addr_copy(*last_interface_ip_ptr, &iface_addr);
5122 SMARTLIST_FOREACH(outgoing_addrs, tor_addr_t*, a_ptr, tor_free(a_ptr));
5123 smartlist_clear(outgoing_addrs);
5124 smartlist_add(outgoing_addrs, tor_memdup(&out_addr, sizeof(tor_addr_t)));
5125 /* We'll need to resolve ourselves again. */
5126 resolved_addr_reset_last(AF_INET);
5127 /* Okay, now change our keys. */
5128 ip_address_changed(1);
5129 }
5130 }
5131
5132 /** Some systems have limited system buffers for recv and xmit on
5133 * sockets allocated in a virtual server or similar environment. For a Tor
5134 * server this can produce the "Error creating network socket: No buffer
5135 * space available" error once all available TCP buffer space is consumed.
5136 * This method will attempt to constrain the buffers allocated for the socket
5137 * to the desired size to stay below system TCP buffer limits.
5138 */
5139 static void
set_constrained_socket_buffers(tor_socket_t sock,int size)5140 set_constrained_socket_buffers(tor_socket_t sock, int size)
5141 {
5142 void *sz = (void*)&size;
5143 socklen_t sz_sz = (socklen_t) sizeof(size);
5144 if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, sz, sz_sz) < 0) {
5145 int e = tor_socket_errno(sock);
5146 log_warn(LD_NET, "setsockopt() to constrain send "
5147 "buffer to %d bytes failed: %s", size, tor_socket_strerror(e));
5148 }
5149 if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, sz, sz_sz) < 0) {
5150 int e = tor_socket_errno(sock);
5151 log_warn(LD_NET, "setsockopt() to constrain recv "
5152 "buffer to %d bytes failed: %s", size, tor_socket_strerror(e));
5153 }
5154 }
5155
5156 /** Process new bytes that have arrived on conn-\>inbuf.
5157 *
5158 * This function just passes conn to the connection-specific
5159 * connection_*_process_inbuf() function. It also passes in
5160 * package_partial if wanted.
5161 */
5162 static int
connection_process_inbuf(connection_t * conn,int package_partial)5163 connection_process_inbuf(connection_t *conn, int package_partial)
5164 {
5165 tor_assert(conn);
5166
5167 switch (conn->type) {
5168 case CONN_TYPE_OR:
5169 return connection_or_process_inbuf(TO_OR_CONN(conn));
5170 case CONN_TYPE_EXT_OR:
5171 return connection_ext_or_process_inbuf(TO_OR_CONN(conn));
5172 case CONN_TYPE_EXIT:
5173 case CONN_TYPE_AP:
5174 return connection_edge_process_inbuf(TO_EDGE_CONN(conn),
5175 package_partial);
5176 case CONN_TYPE_DIR:
5177 return connection_dir_process_inbuf(TO_DIR_CONN(conn));
5178 case CONN_TYPE_CONTROL:
5179 return connection_control_process_inbuf(TO_CONTROL_CONN(conn));
5180 case CONN_TYPE_METRICS:
5181 return metrics_connection_process_inbuf(conn);
5182 default:
5183 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
5184 tor_fragile_assert();
5185 return -1;
5186 }
5187 }
5188
5189 /** Called whenever we've written data on a connection. */
5190 static int
connection_flushed_some(connection_t * conn)5191 connection_flushed_some(connection_t *conn)
5192 {
5193 int r = 0;
5194 tor_assert(!conn->in_flushed_some);
5195 conn->in_flushed_some = 1;
5196 if (conn->type == CONN_TYPE_DIR &&
5197 conn->state == DIR_CONN_STATE_SERVER_WRITING) {
5198 r = connection_dirserv_flushed_some(TO_DIR_CONN(conn));
5199 } else if (conn->type == CONN_TYPE_OR) {
5200 r = connection_or_flushed_some(TO_OR_CONN(conn));
5201 } else if (CONN_IS_EDGE(conn)) {
5202 r = connection_edge_flushed_some(TO_EDGE_CONN(conn));
5203 }
5204 conn->in_flushed_some = 0;
5205 return r;
5206 }
5207
5208 /** We just finished flushing bytes to the appropriately low network layer,
5209 * and there are no more bytes remaining in conn-\>outbuf or
5210 * conn-\>tls to be flushed.
5211 *
5212 * This function just passes conn to the connection-specific
5213 * connection_*_finished_flushing() function.
5214 */
5215 static int
connection_finished_flushing(connection_t * conn)5216 connection_finished_flushing(connection_t *conn)
5217 {
5218 tor_assert(conn);
5219
5220 /* If the connection is closed, don't try to do anything more here. */
5221 if (CONN_IS_CLOSED(conn))
5222 return 0;
5223
5224 // log_fn(LOG_DEBUG,"entered. Socket %u.", conn->s);
5225
5226 connection_stop_writing(conn);
5227
5228 switch (conn->type) {
5229 case CONN_TYPE_OR:
5230 return connection_or_finished_flushing(TO_OR_CONN(conn));
5231 case CONN_TYPE_EXT_OR:
5232 return connection_ext_or_finished_flushing(TO_OR_CONN(conn));
5233 case CONN_TYPE_AP:
5234 case CONN_TYPE_EXIT:
5235 return connection_edge_finished_flushing(TO_EDGE_CONN(conn));
5236 case CONN_TYPE_DIR:
5237 return connection_dir_finished_flushing(TO_DIR_CONN(conn));
5238 case CONN_TYPE_CONTROL:
5239 return connection_control_finished_flushing(TO_CONTROL_CONN(conn));
5240 case CONN_TYPE_METRICS:
5241 return metrics_connection_finished_flushing(conn);
5242 default:
5243 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
5244 tor_fragile_assert();
5245 return -1;
5246 }
5247 }
5248
5249 /** Called when our attempt to connect() to a server has just succeeded.
5250 *
5251 * This function checks if the interface address has changed (clients only),
5252 * and then passes conn to the connection-specific
5253 * connection_*_finished_connecting() function.
5254 */
5255 static int
connection_finished_connecting(connection_t * conn)5256 connection_finished_connecting(connection_t *conn)
5257 {
5258 tor_assert(conn);
5259
5260 if (!server_mode(get_options())) {
5261 /* See whether getsockname() says our address changed. We need to do this
5262 * now that the connection has finished, because getsockname() on Windows
5263 * won't work until then. */
5264 client_check_address_changed(conn->s);
5265 }
5266
5267 switch (conn->type)
5268 {
5269 case CONN_TYPE_OR:
5270 return connection_or_finished_connecting(TO_OR_CONN(conn));
5271 case CONN_TYPE_EXIT:
5272 return connection_edge_finished_connecting(TO_EDGE_CONN(conn));
5273 case CONN_TYPE_DIR:
5274 return connection_dir_finished_connecting(TO_DIR_CONN(conn));
5275 default:
5276 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
5277 tor_fragile_assert();
5278 return -1;
5279 }
5280 }
5281
5282 /** Callback: invoked when a connection reaches an EOF event. */
5283 static int
connection_reached_eof(connection_t * conn)5284 connection_reached_eof(connection_t *conn)
5285 {
5286 switch (conn->type) {
5287 case CONN_TYPE_OR:
5288 case CONN_TYPE_EXT_OR:
5289 return connection_or_reached_eof(TO_OR_CONN(conn));
5290 case CONN_TYPE_AP:
5291 case CONN_TYPE_EXIT:
5292 return connection_edge_reached_eof(TO_EDGE_CONN(conn));
5293 case CONN_TYPE_DIR:
5294 return connection_dir_reached_eof(TO_DIR_CONN(conn));
5295 case CONN_TYPE_CONTROL:
5296 return connection_control_reached_eof(TO_CONTROL_CONN(conn));
5297 case CONN_TYPE_METRICS:
5298 return metrics_connection_reached_eof(conn);
5299 default:
5300 log_err(LD_BUG,"got unexpected conn type %d.", conn->type);
5301 tor_fragile_assert();
5302 return -1;
5303 }
5304 }
5305
5306 /** Comparator for the two-orconn case in OOS victim sort */
5307 static int
oos_victim_comparator_for_orconns(or_connection_t * a,or_connection_t * b)5308 oos_victim_comparator_for_orconns(or_connection_t *a, or_connection_t *b)
5309 {
5310 int a_circs, b_circs;
5311 /* Fewer circuits == higher priority for OOS kill, sort earlier */
5312
5313 a_circs = connection_or_get_num_circuits(a);
5314 b_circs = connection_or_get_num_circuits(b);
5315
5316 if (a_circs < b_circs) return 1;
5317 else if (a_circs > b_circs) return -1;
5318 else return 0;
5319 }
5320
5321 /** Sort comparator for OOS victims; better targets sort before worse
5322 * ones. */
5323 static int
oos_victim_comparator(const void ** a_v,const void ** b_v)5324 oos_victim_comparator(const void **a_v, const void **b_v)
5325 {
5326 connection_t *a = NULL, *b = NULL;
5327
5328 /* Get connection pointers out */
5329
5330 a = (connection_t *)(*a_v);
5331 b = (connection_t *)(*b_v);
5332
5333 tor_assert(a != NULL);
5334 tor_assert(b != NULL);
5335
5336 /*
5337 * We always prefer orconns as victims currently; we won't even see
5338 * these non-orconn cases, but if we do, sort them after orconns.
5339 */
5340 if (a->type == CONN_TYPE_OR && b->type == CONN_TYPE_OR) {
5341 return oos_victim_comparator_for_orconns(TO_OR_CONN(a), TO_OR_CONN(b));
5342 } else {
5343 /*
5344 * One isn't an orconn; if one is, it goes first. We currently have no
5345 * opinions about cases where neither is an orconn.
5346 */
5347 if (a->type == CONN_TYPE_OR) return -1;
5348 else if (b->type == CONN_TYPE_OR) return 1;
5349 else return 0;
5350 }
5351 }
5352
5353 /** Pick n victim connections for the OOS handler and return them in a
5354 * smartlist.
5355 */
5356 MOCK_IMPL(STATIC smartlist_t *,
5357 pick_oos_victims, (int n))
5358 {
5359 smartlist_t *eligible = NULL, *victims = NULL;
5360 smartlist_t *conns;
5361 int conn_counts_by_type[CONN_TYPE_MAX_ + 1], i;
5362
5363 /*
5364 * Big damn assumption (someone improve this someday!):
5365 *
5366 * Socket exhaustion normally happens on high-volume relays, and so
5367 * most of the connections involved are orconns. We should pick victims
5368 * by assembling a list of all orconns, and sorting them in order of
5369 * how much 'damage' by some metric we'd be doing by dropping them.
5370 *
5371 * If we move on from orconns, we should probably think about incoming
5372 * directory connections next, or exit connections. Things we should
5373 * probably never kill are controller connections and listeners.
5374 *
5375 * This function will count how many connections of different types
5376 * exist and log it for purposes of gathering data on typical OOS
5377 * situations to guide future improvements.
5378 */
5379
5380 /* First, get the connection array */
5381 conns = get_connection_array();
5382 /*
5383 * Iterate it and pick out eligible connection types, and log some stats
5384 * along the way.
5385 */
5386 eligible = smartlist_new();
5387 memset(conn_counts_by_type, 0, sizeof(conn_counts_by_type));
SMARTLIST_FOREACH_BEGIN(conns,connection_t *,c)5388 SMARTLIST_FOREACH_BEGIN(conns, connection_t *, c) {
5389 /* Bump the counter */
5390 tor_assert(c->type <= CONN_TYPE_MAX_);
5391 ++(conn_counts_by_type[c->type]);
5392
5393 /* Skip anything without a socket we can free */
5394 if (!(SOCKET_OK(c->s))) {
5395 continue;
5396 }
5397
5398 /* Skip anything we would count as moribund */
5399 if (connection_is_moribund(c)) {
5400 continue;
5401 }
5402
5403 switch (c->type) {
5404 case CONN_TYPE_OR:
5405 /* We've got an orconn, it's eligible to be OOSed */
5406 smartlist_add(eligible, c);
5407 break;
5408 default:
5409 /* We don't know what to do with it, ignore it */
5410 break;
5411 }
5412 } SMARTLIST_FOREACH_END(c);
5413
5414 /* Log some stats */
5415 if (smartlist_len(conns) > 0) {
5416 /* At least one counter must be non-zero */
5417 log_info(LD_NET, "Some stats on conn types seen during OOS follow");
5418 for (i = CONN_TYPE_MIN_; i <= CONN_TYPE_MAX_; ++i) {
5419 /* Did we see any? */
5420 if (conn_counts_by_type[i] > 0) {
5421 log_info(LD_NET, "%s: %d conns",
5422 conn_type_to_string(i),
5423 conn_counts_by_type[i]);
5424 }
5425 }
5426 log_info(LD_NET, "Done with OOS conn type stats");
5427 }
5428
5429 /* Did we find more eligible targets than we want to kill? */
5430 if (smartlist_len(eligible) > n) {
5431 /* Sort the list in order of target preference */
5432 smartlist_sort(eligible, oos_victim_comparator);
5433 /* Pick first n as victims */
5434 victims = smartlist_new();
5435 for (i = 0; i < n; ++i) {
5436 smartlist_add(victims, smartlist_get(eligible, i));
5437 }
5438 /* Free the original list */
5439 smartlist_free(eligible);
5440 } else {
5441 /* No, we can just call them all victims */
5442 victims = eligible;
5443 }
5444
5445 return victims;
5446 }
5447
5448 /** Kill a list of connections for the OOS handler. */
5449 MOCK_IMPL(STATIC void,
5450 kill_conn_list_for_oos, (smartlist_t *conns))
5451 {
5452 if (!conns) return;
5453
SMARTLIST_FOREACH_BEGIN(conns,connection_t *,c)5454 SMARTLIST_FOREACH_BEGIN(conns, connection_t *, c) {
5455 /* Make sure the channel layer gets told about orconns */
5456 if (c->type == CONN_TYPE_OR) {
5457 connection_or_close_for_error(TO_OR_CONN(c), 1);
5458 } else {
5459 connection_mark_for_close(c);
5460 }
5461 } SMARTLIST_FOREACH_END(c);
5462
5463 log_notice(LD_NET,
5464 "OOS handler marked %d connections",
5465 smartlist_len(conns));
5466 }
5467
5468 /** Check if a connection is on the way out so the OOS handler doesn't try
5469 * to kill more than it needs. */
5470 int
connection_is_moribund(connection_t * conn)5471 connection_is_moribund(connection_t *conn)
5472 {
5473 if (conn != NULL &&
5474 (conn->conn_array_index < 0 ||
5475 conn->marked_for_close)) {
5476 return 1;
5477 } else {
5478 return 0;
5479 }
5480 }
5481
5482 /** Out-of-Sockets handler; n_socks is the current number of open
5483 * sockets, and failed is non-zero if a socket exhaustion related
5484 * error immediately preceded this call. This is where to do
5485 * circuit-killing heuristics as needed.
5486 */
5487 void
connection_check_oos(int n_socks,int failed)5488 connection_check_oos(int n_socks, int failed)
5489 {
5490 int target_n_socks = 0, moribund_socks, socks_to_kill;
5491 smartlist_t *conns;
5492
5493 /* Early exit: is OOS checking disabled? */
5494 if (get_options()->DisableOOSCheck) {
5495 return;
5496 }
5497
5498 /* Sanity-check args */
5499 tor_assert(n_socks >= 0);
5500
5501 /*
5502 * Make some log noise; keep it at debug level since this gets a chance
5503 * to run on every connection attempt.
5504 */
5505 log_debug(LD_NET,
5506 "Running the OOS handler (%d open sockets, %s)",
5507 n_socks, (failed != 0) ? "exhaustion seen" : "no exhaustion");
5508
5509 /*
5510 * Check if we're really handling an OOS condition, and if so decide how
5511 * many sockets we want to get down to. Be sure we check if the threshold
5512 * is distinct from zero first; it's possible for this to be called a few
5513 * times before we've finished reading the config.
5514 */
5515 if (n_socks >= get_options()->ConnLimit_high_thresh &&
5516 get_options()->ConnLimit_high_thresh != 0 &&
5517 get_options()->ConnLimit_ != 0) {
5518 /* Try to get down to the low threshold */
5519 target_n_socks = get_options()->ConnLimit_low_thresh;
5520 log_notice(LD_NET,
5521 "Current number of sockets %d is greater than configured "
5522 "limit %d; OOS handler trying to get down to %d",
5523 n_socks, get_options()->ConnLimit_high_thresh,
5524 target_n_socks);
5525 } else if (failed) {
5526 /*
5527 * If we're not at the limit but we hit a socket exhaustion error, try to
5528 * drop some (but not as aggressively as ConnLimit_low_threshold, which is
5529 * 3/4 of ConnLimit_)
5530 */
5531 target_n_socks = (n_socks * 9) / 10;
5532 log_notice(LD_NET,
5533 "We saw socket exhaustion at %d open sockets; OOS handler "
5534 "trying to get down to %d",
5535 n_socks, target_n_socks);
5536 }
5537
5538 if (target_n_socks > 0) {
5539 /*
5540 * It's an OOS!
5541 *
5542 * Count moribund sockets; it's be important that anything we decide
5543 * to get rid of here but don't immediately close get counted as moribund
5544 * on subsequent invocations so we don't try to kill too many things if
5545 * connection_check_oos() gets called multiple times.
5546 */
5547 moribund_socks = connection_count_moribund();
5548
5549 if (moribund_socks < n_socks - target_n_socks) {
5550 socks_to_kill = n_socks - target_n_socks - moribund_socks;
5551
5552 conns = pick_oos_victims(socks_to_kill);
5553 if (conns) {
5554 kill_conn_list_for_oos(conns);
5555 log_notice(LD_NET,
5556 "OOS handler killed %d conns", smartlist_len(conns));
5557 smartlist_free(conns);
5558 } else {
5559 log_notice(LD_NET, "OOS handler failed to pick any victim conns");
5560 }
5561 } else {
5562 log_notice(LD_NET,
5563 "Not killing any sockets for OOS because there are %d "
5564 "already moribund, and we only want to eliminate %d",
5565 moribund_socks, n_socks - target_n_socks);
5566 }
5567 }
5568 }
5569
5570 /** Log how many bytes are used by buffers of different kinds and sizes. */
5571 void
connection_dump_buffer_mem_stats(int severity)5572 connection_dump_buffer_mem_stats(int severity)
5573 {
5574 uint64_t used_by_type[CONN_TYPE_MAX_+1];
5575 uint64_t alloc_by_type[CONN_TYPE_MAX_+1];
5576 int n_conns_by_type[CONN_TYPE_MAX_+1];
5577 uint64_t total_alloc = 0;
5578 uint64_t total_used = 0;
5579 int i;
5580 smartlist_t *conns = get_connection_array();
5581
5582 memset(used_by_type, 0, sizeof(used_by_type));
5583 memset(alloc_by_type, 0, sizeof(alloc_by_type));
5584 memset(n_conns_by_type, 0, sizeof(n_conns_by_type));
5585
5586 SMARTLIST_FOREACH_BEGIN(conns, connection_t *, c) {
5587 int tp = c->type;
5588 ++n_conns_by_type[tp];
5589 if (c->inbuf) {
5590 used_by_type[tp] += buf_datalen(c->inbuf);
5591 alloc_by_type[tp] += buf_allocation(c->inbuf);
5592 }
5593 if (c->outbuf) {
5594 used_by_type[tp] += buf_datalen(c->outbuf);
5595 alloc_by_type[tp] += buf_allocation(c->outbuf);
5596 }
5597 } SMARTLIST_FOREACH_END(c);
5598 for (i=0; i <= CONN_TYPE_MAX_; ++i) {
5599 total_used += used_by_type[i];
5600 total_alloc += alloc_by_type[i];
5601 }
5602
5603 tor_log(severity, LD_GENERAL,
5604 "In buffers for %d connections: %"PRIu64" used/%"PRIu64" allocated",
5605 smartlist_len(conns),
5606 (total_used), (total_alloc));
5607 for (i=CONN_TYPE_MIN_; i <= CONN_TYPE_MAX_; ++i) {
5608 if (!n_conns_by_type[i])
5609 continue;
5610 tor_log(severity, LD_GENERAL,
5611 " For %d %s connections: %"PRIu64" used/%"PRIu64" allocated",
5612 n_conns_by_type[i], conn_type_to_string(i),
5613 (used_by_type[i]), (alloc_by_type[i]));
5614 }
5615 }
5616
5617 /** Verify that connection <b>conn</b> has all of its invariants
5618 * correct. Trigger an assert if anything is invalid.
5619 */
5620 void
assert_connection_ok(connection_t * conn,time_t now)5621 assert_connection_ok(connection_t *conn, time_t now)
5622 {
5623 (void) now; /* XXXX unused. */
5624 tor_assert(conn);
5625 tor_assert(conn->type >= CONN_TYPE_MIN_);
5626 tor_assert(conn->type <= CONN_TYPE_MAX_);
5627
5628 switch (conn->type) {
5629 case CONN_TYPE_OR:
5630 case CONN_TYPE_EXT_OR:
5631 tor_assert(conn->magic == OR_CONNECTION_MAGIC);
5632 break;
5633 case CONN_TYPE_AP:
5634 tor_assert(conn->magic == ENTRY_CONNECTION_MAGIC);
5635 break;
5636 case CONN_TYPE_EXIT:
5637 tor_assert(conn->magic == EDGE_CONNECTION_MAGIC);
5638 break;
5639 case CONN_TYPE_DIR:
5640 tor_assert(conn->magic == DIR_CONNECTION_MAGIC);
5641 break;
5642 case CONN_TYPE_CONTROL:
5643 tor_assert(conn->magic == CONTROL_CONNECTION_MAGIC);
5644 break;
5645 CASE_ANY_LISTENER_TYPE:
5646 tor_assert(conn->magic == LISTENER_CONNECTION_MAGIC);
5647 break;
5648 default:
5649 tor_assert(conn->magic == BASE_CONNECTION_MAGIC);
5650 break;
5651 }
5652
5653 if (conn->linked_conn) {
5654 tor_assert(conn->linked_conn->linked_conn == conn);
5655 tor_assert(conn->linked);
5656 }
5657 if (conn->linked)
5658 tor_assert(!SOCKET_OK(conn->s));
5659
5660 if (conn->hold_open_until_flushed)
5661 tor_assert(conn->marked_for_close);
5662
5663 /* XXXX check: read_blocked_on_bw, write_blocked_on_bw, s, conn_array_index,
5664 * marked_for_close. */
5665
5666 /* buffers */
5667 if (conn->inbuf)
5668 buf_assert_ok(conn->inbuf);
5669 if (conn->outbuf)
5670 buf_assert_ok(conn->outbuf);
5671
5672 if (conn->type == CONN_TYPE_OR) {
5673 or_connection_t *or_conn = TO_OR_CONN(conn);
5674 if (conn->state == OR_CONN_STATE_OPEN) {
5675 /* tor_assert(conn->bandwidth > 0); */
5676 /* the above isn't necessarily true: if we just did a TLS
5677 * handshake but we didn't recognize the other peer, or it
5678 * gave a bad cert/etc, then we won't have assigned bandwidth,
5679 * yet it will be open. -RD
5680 */
5681 // tor_assert(conn->read_bucket >= 0);
5682 }
5683 // tor_assert(conn->addr && conn->port);
5684 tor_assert(conn->address);
5685 if (conn->state > OR_CONN_STATE_PROXY_HANDSHAKING)
5686 tor_assert(or_conn->tls);
5687 }
5688
5689 if (CONN_IS_EDGE(conn)) {
5690 /* XXX unchecked: package window, deliver window. */
5691 if (conn->type == CONN_TYPE_AP) {
5692 entry_connection_t *entry_conn = TO_ENTRY_CONN(conn);
5693 if (entry_conn->chosen_exit_optional || entry_conn->chosen_exit_retries)
5694 tor_assert(entry_conn->chosen_exit_name);
5695
5696 tor_assert(entry_conn->socks_request);
5697 if (conn->state == AP_CONN_STATE_OPEN) {
5698 tor_assert(entry_conn->socks_request->has_finished);
5699 if (!conn->marked_for_close) {
5700 tor_assert(ENTRY_TO_EDGE_CONN(entry_conn)->cpath_layer);
5701 cpath_assert_layer_ok(ENTRY_TO_EDGE_CONN(entry_conn)->cpath_layer);
5702 }
5703 }
5704 }
5705 if (conn->type == CONN_TYPE_EXIT) {
5706 tor_assert(conn->purpose == EXIT_PURPOSE_CONNECT ||
5707 conn->purpose == EXIT_PURPOSE_RESOLVE);
5708 }
5709 } else if (conn->type == CONN_TYPE_DIR) {
5710 } else {
5711 /* Purpose is only used for dir and exit types currently */
5712 tor_assert(!conn->purpose);
5713 }
5714
5715 switch (conn->type)
5716 {
5717 CASE_ANY_LISTENER_TYPE:
5718 tor_assert(conn->state == LISTENER_STATE_READY);
5719 break;
5720 case CONN_TYPE_OR:
5721 tor_assert(conn->state >= OR_CONN_STATE_MIN_);
5722 tor_assert(conn->state <= OR_CONN_STATE_MAX_);
5723 break;
5724 case CONN_TYPE_EXT_OR:
5725 tor_assert(conn->state >= EXT_OR_CONN_STATE_MIN_);
5726 tor_assert(conn->state <= EXT_OR_CONN_STATE_MAX_);
5727 break;
5728 case CONN_TYPE_EXIT:
5729 tor_assert(conn->state >= EXIT_CONN_STATE_MIN_);
5730 tor_assert(conn->state <= EXIT_CONN_STATE_MAX_);
5731 tor_assert(conn->purpose >= EXIT_PURPOSE_MIN_);
5732 tor_assert(conn->purpose <= EXIT_PURPOSE_MAX_);
5733 break;
5734 case CONN_TYPE_AP:
5735 tor_assert(conn->state >= AP_CONN_STATE_MIN_);
5736 tor_assert(conn->state <= AP_CONN_STATE_MAX_);
5737 tor_assert(TO_ENTRY_CONN(conn)->socks_request);
5738 break;
5739 case CONN_TYPE_DIR:
5740 tor_assert(conn->state >= DIR_CONN_STATE_MIN_);
5741 tor_assert(conn->state <= DIR_CONN_STATE_MAX_);
5742 tor_assert(conn->purpose >= DIR_PURPOSE_MIN_);
5743 tor_assert(conn->purpose <= DIR_PURPOSE_MAX_);
5744 break;
5745 case CONN_TYPE_CONTROL:
5746 tor_assert(conn->state >= CONTROL_CONN_STATE_MIN_);
5747 tor_assert(conn->state <= CONTROL_CONN_STATE_MAX_);
5748 break;
5749 case CONN_TYPE_METRICS:
5750 /* No state. */
5751 break;
5752 default:
5753 tor_assert(0);
5754 }
5755 }
5756
5757 /** Fills <b>addr</b> and <b>port</b> with the details of the global
5758 * proxy server we are using. Store a 1 to the int pointed to by
5759 * <b>is_put_out</b> if the connection is using a pluggable
5760 * transport; store 0 otherwise. <b>conn</b> contains the connection
5761 * we are using the proxy for.
5762 *
5763 * Return 0 on success, -1 on failure.
5764 */
5765 int
get_proxy_addrport(tor_addr_t * addr,uint16_t * port,int * proxy_type,int * is_pt_out,const connection_t * conn)5766 get_proxy_addrport(tor_addr_t *addr, uint16_t *port, int *proxy_type,
5767 int *is_pt_out, const connection_t *conn)
5768 {
5769 const or_options_t *options = get_options();
5770
5771 *is_pt_out = 0;
5772 /* Client Transport Plugins can use another proxy, but that should be hidden
5773 * from the rest of tor (as the plugin is responsible for dealing with the
5774 * proxy), check it first, then check the rest of the proxy types to allow
5775 * the config to have unused ClientTransportPlugin entries.
5776 */
5777 if (options->ClientTransportPlugin) {
5778 const transport_t *transport = NULL;
5779 int r;
5780 r = get_transport_by_bridge_addrport(&conn->addr, conn->port, &transport);
5781 if (r<0)
5782 return -1;
5783 if (transport) { /* transport found */
5784 tor_addr_copy(addr, &transport->addr);
5785 *port = transport->port;
5786 *proxy_type = transport->socks_version;
5787 *is_pt_out = 1;
5788 return 0;
5789 }
5790
5791 /* Unused ClientTransportPlugin. */
5792 }
5793
5794 if (options->HTTPSProxy) {
5795 tor_addr_copy(addr, &options->HTTPSProxyAddr);
5796 *port = options->HTTPSProxyPort;
5797 *proxy_type = PROXY_CONNECT;
5798 return 0;
5799 } else if (options->Socks4Proxy) {
5800 tor_addr_copy(addr, &options->Socks4ProxyAddr);
5801 *port = options->Socks4ProxyPort;
5802 *proxy_type = PROXY_SOCKS4;
5803 return 0;
5804 } else if (options->Socks5Proxy) {
5805 tor_addr_copy(addr, &options->Socks5ProxyAddr);
5806 *port = options->Socks5ProxyPort;
5807 *proxy_type = PROXY_SOCKS5;
5808 return 0;
5809 } else if (options->TCPProxy) {
5810 tor_addr_copy(addr, &options->TCPProxyAddr);
5811 *port = options->TCPProxyPort;
5812 /* The only supported protocol in TCPProxy is haproxy. */
5813 tor_assert(options->TCPProxyProtocol == TCP_PROXY_PROTOCOL_HAPROXY);
5814 *proxy_type = PROXY_HAPROXY;
5815 return 0;
5816 }
5817
5818 tor_addr_make_unspec(addr);
5819 *port = 0;
5820 *proxy_type = PROXY_NONE;
5821 return 0;
5822 }
5823
5824 /** Log a failed connection to a proxy server.
5825 * <b>conn</b> is the connection we use the proxy server for. */
5826 void
log_failed_proxy_connection(connection_t * conn)5827 log_failed_proxy_connection(connection_t *conn)
5828 {
5829 tor_addr_t proxy_addr;
5830 uint16_t proxy_port;
5831 int proxy_type, is_pt;
5832
5833 if (get_proxy_addrport(&proxy_addr, &proxy_port, &proxy_type, &is_pt,
5834 conn) != 0)
5835 return; /* if we have no proxy set up, leave this function. */
5836
5837 (void)is_pt;
5838 log_warn(LD_NET,
5839 "The connection to the %s proxy server at %s just failed. "
5840 "Make sure that the proxy server is up and running.",
5841 proxy_type_to_string(proxy_type),
5842 fmt_addrport(&proxy_addr, proxy_port));
5843 }
5844
5845 /** Return string representation of <b>proxy_type</b>. */
5846 static const char *
proxy_type_to_string(int proxy_type)5847 proxy_type_to_string(int proxy_type)
5848 {
5849 switch (proxy_type) {
5850 case PROXY_CONNECT: return "HTTP";
5851 case PROXY_SOCKS4: return "SOCKS4";
5852 case PROXY_SOCKS5: return "SOCKS5";
5853 case PROXY_HAPROXY: return "HAPROXY";
5854 case PROXY_PLUGGABLE: return "pluggable transports SOCKS";
5855 case PROXY_NONE: return "NULL";
5856 default: tor_assert(0);
5857 }
5858 return NULL; /*Unreached*/
5859 }
5860
5861 /** Call connection_free_minimal() on every connection in our array, and
5862 * release all storage held by connection.c.
5863 *
5864 * Don't do the checks in connection_free(), because they will
5865 * fail.
5866 */
5867 void
connection_free_all(void)5868 connection_free_all(void)
5869 {
5870 smartlist_t *conns = get_connection_array();
5871
5872 /* We don't want to log any messages to controllers. */
5873 SMARTLIST_FOREACH(conns, connection_t *, conn,
5874 if (conn->type == CONN_TYPE_CONTROL)
5875 TO_CONTROL_CONN(conn)->event_mask = 0);
5876
5877 control_update_global_event_mask();
5878
5879 /* Unlink everything from the identity map. */
5880 connection_or_clear_identity_map();
5881
5882 /* Clear out our list of broken connections */
5883 clear_broken_connection_map(0);
5884
5885 SMARTLIST_FOREACH(conns, connection_t *, conn,
5886 connection_free_minimal(conn));
5887
5888 if (outgoing_addrs) {
5889 SMARTLIST_FOREACH(outgoing_addrs, tor_addr_t *, addr, tor_free(addr));
5890 smartlist_free(outgoing_addrs);
5891 outgoing_addrs = NULL;
5892 }
5893
5894 tor_free(last_interface_ipv4);
5895 tor_free(last_interface_ipv6);
5896 last_recorded_accounting_at = 0;
5897
5898 mainloop_event_free(reenable_blocked_connections_ev);
5899 reenable_blocked_connections_is_scheduled = 0;
5900 memset(&reenable_blocked_connections_delay, 0, sizeof(struct timeval));
5901 }
5902
5903 /** Log a warning, and possibly emit a control event, that <b>received</b> came
5904 * at a skewed time. <b>trusted</b> indicates that the <b>source</b> was one
5905 * that we had more faith in and therefore the warning level should have higher
5906 * severity.
5907 */
5908 MOCK_IMPL(void,
5909 clock_skew_warning, (const connection_t *conn, long apparent_skew, int trusted,
5910 log_domain_mask_t domain, const char *received,
5911 const char *source))
5912 {
5913 char dbuf[64];
5914 char *ext_source = NULL, *warn = NULL;
5915 format_time_interval(dbuf, sizeof(dbuf), apparent_skew);
5916 if (conn)
5917 tor_asprintf(&ext_source, "%s:%s:%d", source,
5918 fmt_and_decorate_addr(&conn->addr), conn->port);
5919 else
5920 ext_source = tor_strdup(source);
5921 log_fn(trusted ? LOG_WARN : LOG_INFO, domain,
5922 "Received %s with skewed time (%s): "
5923 "It seems that our clock is %s by %s, or that theirs is %s%s. "
5924 "Tor requires an accurate clock to work: please check your time, "
5925 "timezone, and date settings.", received, ext_source,
5926 apparent_skew > 0 ? "ahead" : "behind", dbuf,
5927 apparent_skew > 0 ? "behind" : "ahead",
5928 (!conn || trusted) ? "" : ", or they are sending us the wrong time");
5929 if (trusted) {
5930 control_event_general_status(LOG_WARN, "CLOCK_SKEW SKEW=%ld SOURCE=%s",
5931 apparent_skew, ext_source);
5932 tor_asprintf(&warn, "Clock skew %ld in %s from %s", apparent_skew,
5933 received, source);
5934 control_event_bootstrap_problem(warn, "CLOCK_SKEW", conn, 1);
5935 }
5936 tor_free(warn);
5937 tor_free(ext_source);
5938 }
5939