56#define CONNECTION_EDGE_PRIVATE
76#include "core/or/dos.h"
85#include "feature/client/circpathbias.h"
117#include "core/or/or_circuit_st.h"
123#ifdef HAVE_LINUX_TYPES_H
124#include <linux/types.h>
126#ifdef HAVE_LINUX_NETFILTER_IPV4_H
127#include <linux/netfilter_ipv4.h>
128#define TRANS_NETFILTER
129#define TRANS_NETFILTER_IPV4
132#ifdef HAVE_LINUX_IF_H
136#ifdef HAVE_LINUX_NETFILTER_IPV6_IP6_TABLES_H
137#include <linux/netfilter_ipv6/ip6_tables.h>
138#if defined(IP6T_SO_ORIGINAL_DST)
139#define TRANS_NETFILTER
140#define TRANS_NETFILTER_IPV6
147#ifdef HAVE_SYS_IOCTL_H
148#include <sys/ioctl.h>
150#ifdef HAVE_SYS_PARAM_H
151#include <sys/param.h>
154#if defined(HAVE_NET_IF_H) && defined(HAVE_NET_PFVAR_H)
156#include <net/pfvar.h>
164#define SOCKS4_GRANTED 90
165#define SOCKS4_REJECT 91
184 c->
magic == ENTRY_CONNECTION_MAGIC);
256 int line,
const char *file))
271 "stream (marked at %s:%d) sending two socks replies?",
278 RESOLVED_TYPE_ERROR_TRANSIENT,
294 if (connection_get_inbuf_len(
TO_CONN(conn)) &&
299 log_info(
LD_EDGE,
"conn (fd "TOR_SOCKET_T_FORMAT
") reached eof. Closing.",
310 connection_mark_for_close(
TO_CONN(conn));
330 switch (conn->base_.
state) {
349 if (! conn->base_.
linked) {
357 connection_mark_for_close(
TO_CONN(conn));
364 "data from edge while in '%s' state. Sending it anyway. "
365 "package_partial=%d, buflen=%ld",
368 (
long)connection_get_inbuf_len(
TO_CONN(conn)));
371 connection_mark_for_close(
TO_CONN(conn));
385 "data from edge while in '%s' state. Leaving it on buffer.",
389 log_warn(
LD_BUG,
"Got unexpected state %d. Closing.",conn->base_.
state);
392 connection_mark_for_close(
TO_CONN(conn));
403 log_info(
LD_EDGE,
"CircID %u: At an edge. Marking connection for close.",
407 connection_mark_unattached_ap(entry_conn, END_STREAM_REASON_DESTROY);
410 END_STREAM_REASON_DESTROY);
417 connection_mark_and_flush(
TO_CONN(conn));
438 reason = END_STREAM_REASON_MISC;
441 payload[0] = (char) reason;
447 return relay_send_command_from_edge(stream_id, circ, RELAY_COMMAND_END,
448 payload, 1, cpath_layer);
462 (reason == END_STREAM_REASON_CONNECTREFUSED ||
463 reason == END_STREAM_REASON_TIMEOUT)) {
464#define WARN_FAILED_HS_CONNECTION 300
465 static ratelim_t warn_limit = RATELIM_INIT(WARN_FAILED_HS_CONNECTION);
468 log_warn(
LD_EDGE,
"Onion service connection to %s failed (%s)",
517 size_t payload_len=1;
519 uint8_t control_reason = reason;
522 log_warn(
LD_BUG,
"(Harmless.) Calling connection_edge_end (reason %d) "
523 "on an already ended stream?", reason);
530 "called on conn that's already marked for close at %s:%d.",
540 reason = END_STREAM_REASON_MISC;
543 payload[0] = (char)reason;
544 if (reason == END_STREAM_REASON_EXITPOLICY &&
555 payload_len += 4+addrlen;
559 log_debug(
LD_EDGE,
"Sending end on conn (fd "TOR_SOCKET_T_FORMAT
").",
568 payload, payload_len);
571 warn_if_hs_unreachable(conn, control_reason);
573 log_debug(
LD_EDGE,
"No circ to send end on conn "
574 "(fd "TOR_SOCKET_T_FORMAT
").",
624 log_warn(
LD_BUG,
"Duplicate stream close for stream %d on circuit %d",
651 if (conn->hs_ident) {
694 } SMARTLIST_FOREACH_END(half_conn);
833 int found, remove_idx;
844 half = smartlist_get(half_conns, remove_idx);
846 half_edge_free(half);
883 switch (conn->base_.
state) {
885 if (! conn->base_.
linked) {
912 switch (conn->base_.
state) {
927 log_warn(
LD_BUG,
"Called in unexpected state %d.",conn->base_.
state);
937#define MAX_CONNECTED_CELL_PAYLOAD_LEN 25
951 int connected_payload_len;
956 if (family == AF_INET) {
958 connected_payload_len = 4;
959 }
else if (family == AF_INET6) {
963 connected_payload_len = 21;
968 set_uint32(payload_out + connected_payload_len, htonl(ttl));
969 connected_payload_len += 4;
973 return connected_payload_len;
987 const char dst_ipv6[] =
"::1";
989 const char src_ipv6_prefix[] =
"fc00:dead:beef:4dad:";
990 uint16_t dst_port = 0;
991 uint16_t src_port = 1;
997 src_port = gid & 0x0000ffff;
1001 if (edge_conn->hs_ident) {
1008 gid >> 16, gid & 0x0000ffff,
1009 dst_ipv6, src_port, dst_port);
1011 connection_buf_add(buf, strlen(buf),
TO_CONN(edge_conn));
1029 log_info(LD_EXIT,
"%s established.",
1037 if (connection_get_outbuf_len(conn))
1043 RELAY_COMMAND_CONNECTED, NULL, 0) < 0)
1047 int connected_payload_len =
1050 if (connected_payload_len < 0)
1054 RELAY_COMMAND_CONNECTED,
1055 (
char*)connected_payload, connected_payload_len) < 0)
1071static int untried_pending_connections = 0;
1085 log_warn(
LD_BUG,
"(Harmless.) Edge connection (marked at %s:%d) "
1086 "hasn't sent end yet?",
1106 log_warn(
LD_BUG,
"Closing stream (marked at %s:%d) without sending"
1107 " back a socks reply.",
1111 log_warn(
LD_BUG,
"Closing stream (marked at %s:%d) without having"
1116 log_warn(
LD_BUG,
"Closing stream (marked at %s:%d) without having"
1117 " replied to DNS request.",
1130 connection_ap_warn_and_unmark_if_pending_circ(entry_conn,
1192 time_t now = time(NULL);
1196 int seconds_idle, seconds_since_born;
1200 if (base_conn->type !=
CONN_TYPE_AP || base_conn->marked_for_close)
1207 seconds_idle = (int)( now - base_conn->timestamp_last_read_allowed );
1208 seconds_since_born = (int)( now - base_conn->timestamp_created );
1223 "Tried for %d seconds to get a connection to %s:%d. "
1229 connection_mark_unattached_ap(entry_conn, END_STREAM_REASON_TIMEOUT);
1238 if (seconds_idle < cutoff)
1242 log_info(
LD_APP,
"Conn is waiting (address %s), but lost its circ.",
1244 connection_mark_unattached_ap(entry_conn, END_STREAM_REASON_TIMEOUT);
1250 "Rend stream is %d seconds late. Giving up on address"
1259 connection_mark_unattached_ap(entry_conn, END_STREAM_REASON_TIMEOUT);
1265 circ->
purpose != CIRCUIT_PURPOSE_CONFLUX_LINKED &&
1271 log_warn(
LD_BUG,
"circuit->purpose == CIRCUIT_PURPOSE_C_GENERAL failed. "
1272 "The purpose on the circuit was %s; it was in state %s, "
1281 "We tried for %d seconds to connect to '%s' using exit %s."
1282 " Retrying on a new circuit.",
1303 END_STREAM_REASON_TIMEOUT)<0) {
1304 if (!base_conn->marked_for_close)
1305 connection_mark_unattached_ap(entry_conn,
1308 } SMARTLIST_FOREACH_END(base_conn);
1325 if (conn->marked_for_close ||
1333 log_warn(
LD_BUG,
"Found a connection %p that was supposed to be "
1334 "in pending_entry_connections, but wasn't. No worries; "
1337 untried_pending_connections = 1;
1338 connection_ap_mark_as_pending_circuit(entry_conn);
1341 } SMARTLIST_FOREACH_END(conn);
1360 if (untried_pending_connections == 0 && !retry)
1376 if (conn->
magic != ENTRY_CONNECTION_MAGIC) {
1377 log_warn(
LD_BUG,
"%p has impossible magic value %u.",
1378 entry_conn, (
unsigned)conn->
magic);
1392 connection_mark_unattached_ap(entry_conn,
1410 } SMARTLIST_FOREACH_END(entry_conn);
1412 smartlist_free(pending);
1413 untried_pending_connections = 0;
1433 const char *fname,
int lineno)
1446 attach_pending_entry_connections_cb, NULL);
1450 log_warn(
LD_BUG,
"What?? pending_entry_connections already contains %p! "
1451 "(Called from %s:%d.)",
1452 entry_conn, fname, lineno);
1453#ifdef DEBUGGING_17659
1454 const char *f2 = entry_conn->marked_pending_circ_file;
1455 log_warn(
LD_BUG,
"(Previously called from %s:%d.)\n",
1457 entry_conn->marked_pending_circ_line);
1463#ifdef DEBUGGING_17659
1464 entry_conn->marked_pending_circ_line = (uint16_t) lineno;
1465 entry_conn->marked_pending_circ_file = fname;
1468 untried_pending_connections = 1;
1505 log_warn(
LD_BUG,
"What was %p doing in pending_entry_connections in %s?",
1524 if (conn->marked_for_close ||
1546 log_info(
LD_APP,
"Closing one-hop stream to '%s/%s' because the OR conn "
1549 connection_mark_unattached_ap(entry_conn, END_STREAM_REASON_TIMEOUT);
1550 } SMARTLIST_FOREACH_END(conn);
1565 if (conn->marked_for_close ||
1574 NNF_NO_WARN_UNNAMED);
1576 if (!r1 || !r2 || r1 != r2)
1580 log_info(
LD_APP,
"Giving up on enclave exit '%s' for destination %s.",
1598 } SMARTLIST_FOREACH_END(conn);
1606 CONNECTION_AP_EXPECT_NONPENDING(conn);
1624 ENTRY_TO_CONN(conn)->timestamp_last_read_allowed = time(NULL);
1640 connection_ap_mark_as_pending_circuit(conn);
1658 log_warn(
LD_APP,
"Application request to port %d: this port is "
1659 "commonly used for unencrypted protocols. Please make sure "
1660 "you don't send anything you would mind the rest of the "
1661 "Internet reading!%s", port, reject ?
" Closing." :
"");
1663 port, reject ?
"REJECT" :
"WARN");
1667 log_info(
LD_APP,
"Port %d listed in RejectPlaintextPorts. Closing.", port);
1668 connection_mark_unattached_ap(conn, END_STREAM_REASON_ENTRYPOLICY);
1706 s = strrchr(address,
'.');
1708 *type_out = NORMAL_HOSTNAME;
1711 if (!strcmp(s+1,
"exit")) {
1713 *type_out = EXIT_HOSTNAME;
1716 if (strcmp(s+1,
"onion")) {
1717 *type_out = NORMAL_HOSTNAME;
1724 q = strrchr(address,
'.');
1726 *type_out = BAD_HOSTNAME;
1729 q = (NULL == q) ? address : q + 1;
1732 *type_out = BAD_HOSTNAME;
1736 memmove(address, q, strlen(q) + 1 );
1741 *type_out = ONION_V3_HOSTNAME;
1749 *type_out = BAD_HOSTNAME;
1757 const bool is_onion = (*type_out == ONION_V3_HOSTNAME);
1758 log_warn(
LD_APP,
"Invalid %shostname %s; rejecting",
1759 is_onion ?
"onion " :
"",
1760 safe_str_client(address));
1761 if (*type_out == ONION_V3_HOSTNAME) {
1762 *type_out = BAD_HOSTNAME;
1770#define TRACKHOSTEXITS_RETRIES 5
1798 rewrite_result_t *out)
1807 out->map_expires = TIME_MAX;
1808 out->end_reason = 0;
1809 out->should_close = 0;
1810 out->orig_address[0] = 0;
1815 strlcpy(out->orig_address, socks->
address,
sizeof(out->orig_address));
1816 log_debug(
LD_APP,
"Client asked for %s:%d",
1817 safe_str_client(socks->
address),
1824 static ratelim_t exit_warning_limit = RATELIM_INIT(60*15);
1826 "The \".exit\" notation is disabled in Tor due to "
1830 out->end_reason = END_STREAM_REASON_TORPROTOCOL;
1831 out->should_close = 1;
1850 const unsigned rewrite_flags = AMR_FLAG_USE_MAPADDRESS;
1852 rewrite_flags, &out->map_expires, &out->exit_source)) {
1872 const char *new_addr;
1875 int addr_type = RESOLVED_TYPE_IPV4;
1877 if (!conn->entry_cfg.ipv4_traffic ||
1878 (conn->entry_cfg.ipv6_traffic && conn->entry_cfg.prefer_ipv6) ||
1880 addr_type = RESOLVED_TYPE_IPV6;
1887 addr_type, tor_strdup(socks->
address));
1889 log_warn(
LD_APP,
"Unable to automap address %s",
1891 out->end_reason = END_STREAM_REASON_INTERNAL;
1892 out->should_close = 1;
1895 log_info(
LD_APP,
"Automapping %s to %s",
1897 safe_str_client(new_addr));
1906 unsigned rewrite_flags = 0;
1908 rewrite_flags |= AMR_FLAG_USE_IPV4_DNS;
1909 if (conn->entry_cfg.use_cached_ipv6_answers)
1910 rewrite_flags |= AMR_FLAG_USE_IPV6_DNS;
1913 rewrite_flags, &out->map_expires)) {
1914 char *result = tor_strdup(socks->
address);
1919 strlen(result), (uint8_t*)result,
1923 out->end_reason = END_STREAM_REASON_DONE |
1925 out->should_close = 1;
1937 &addr, socks->
address, AF_UNSPEC, 1);
1938 if (ok == 1 && tor_addr_is_internal(&addr, 0)) {
1940 0, NULL, -1, TIME_MAX);
1943 out->should_close = 1;
1958 unsigned rewrite_flags = AMR_FLAG_USE_AUTOMAP | AMR_FLAG_USE_TRACKEXIT;
1961 rewrite_flags |= AMR_FLAG_USE_IPV4_DNS;
1962 if (conn->entry_cfg.use_cached_ipv6_answers)
1963 rewrite_flags |= AMR_FLAG_USE_IPV6_DNS;
1965 rewrite_flags, &out->map_expires, &exit_source2)) {
1972 out->exit_source = exit_source2;
1985 log_warn(
LD_APP,
"Missing mapping for virtual address '%s'. Refusing.",
1986 safe_str_client(socks->
address));
1987 out->end_reason = END_STREAM_REASON_INTERNAL;
1988 out->should_close = 1;
2005 if (!conn->entry_cfg.onion_traffic) {
2006 log_warn(
LD_APP,
"Onion address %s requested from a port with .onion "
2007 "disabled", safe_str_client(socks->
address));
2008 connection_mark_unattached_ap(conn, END_STREAM_REASON_ENTRYPOLICY);
2014 if (SOCKS_COMMAND_IS_RESOLVE(socks->
command)) {
2018 "Resolve requests to hidden services not allowed. Failing.");
2020 0,NULL,-1,TIME_MAX);
2021 connection_mark_unattached_ap(conn,
2030 log_warn(
LD_CONTROL,
"Attachstream to a circuit is not "
2031 "supported for .onion addresses currently. Failing.");
2032 connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
2036 int descriptor_is_usable = 0;
2045 log_warn(
LD_GENERAL,
"failed to parse hs address");
2053 unsigned int refetch_desc = 0;
2056 descriptor_is_usable =
2064 log_info(
LD_REND,
"Descriptor PoW parameters have expired.");
2065 descriptor_is_usable = 0;
2072 log_info(
LD_GENERAL,
"Found %s descriptor in cache for %s. %s.",
2073 (descriptor_is_usable) ?
"usable" :
"unusable",
2074 safe_str_client(socks->
address),
2075 (descriptor_is_usable) ?
"Not fetching." :
"Refetching.");
2078 log_info(
LD_REND,
"No descriptor found in our cache for %s. Fetching.",
2079 safe_str_client(socks->
address));
2091 if (refetch_desc || !descriptor_is_usable) {
2118 log_info(
LD_REND,
"Descriptor is here. Great.");
2123 connection_ap_mark_as_pending_circuit(conn);
2150 time_t now = time(NULL);
2151 rewrite_result_t rr;
2156 memset(&rr, 0,
sizeof(rr));
2157 connection_ap_handshake_rewrite(conn,&rr);
2159 if (rr.should_close) {
2163 connection_mark_unattached_ap(conn, rr.end_reason);
2170 const time_t map_expires = rr.map_expires;
2171 const int automap = rr.automap;
2180 if (addresstype == BAD_HOSTNAME) {
2183 connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
2193 if (addresstype == EXIT_HOSTNAME) {
2198 const node_t *node = NULL;
2206 log_warn(
LD_APP,
"Stale automapped address for '%s.exit'. Refusing.",
2207 safe_str_client(socks->
address));
2210 connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
2220 log_warn(
LD_BUG,
"Address '%s.exit', with impossible source for the "
2221 ".exit part. Refusing.",
2222 safe_str_client(socks->
address));
2225 connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
2236 char *s = strrchr(socks->
address,
'.');
2251 log_warn(
LD_APP,
"Malformed exit address '%s.exit'. Refusing.",
2252 safe_str_client(socks->
address));
2255 connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
2273 "Unrecognized relay in exit address '%s.exit'. Refusing.",
2274 safe_str_client(socks->
address));
2275 connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
2281 "Excluded relay in exit address '%s.exit'. Refusing.",
2282 safe_str_client(socks->
address));
2283 connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
2292 if (addresstype != ONION_V3_HOSTNAME) {
2303 "Destination '%s' seems to be an invalid hostname. Failing.",
2304 safe_str_client(socks->
address));
2305 connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
2320 if (!conn->entry_cfg.dns_request && !conn->entry_cfg.ipv4_traffic
2321 && !conn->entry_cfg.ipv6_traffic) {
2322 log_warn(
LD_APP,
"Refusing to connect to non-hidden-service hostname "
2323 "or IP address %s because Port has OnionTrafficOnly set (or "
2324 "NoDNSRequest, NoIPv4Traffic, and NoIPv6Traffic).",
2325 safe_str_client(socks->
address));
2326 connection_mark_unattached_ap(conn, END_STREAM_REASON_ENTRYPOLICY);
2335 if (socks_family == -1) {
2336 if (!conn->entry_cfg.dns_request) {
2337 log_warn(
LD_APP,
"Refusing to connect to hostname %s "
2338 "because Port has NoDNSRequest set.",
2339 safe_str_client(socks->
address));
2340 connection_mark_unattached_ap(conn, END_STREAM_REASON_ENTRYPOLICY);
2343 }
else if (socks_family == AF_INET) {
2344 if (!conn->entry_cfg.ipv4_traffic) {
2345 log_warn(
LD_APP,
"Refusing to connect to IPv4 address %s because "
2346 "Port has NoIPv4Traffic set.",
2347 safe_str_client(socks->
address));
2348 connection_mark_unattached_ap(conn, END_STREAM_REASON_ENTRYPOLICY);
2351 }
else if (socks_family == AF_INET6) {
2352 if (!conn->entry_cfg.ipv6_traffic) {
2353 log_warn(
LD_APP,
"Refusing to connect to IPv6 address %s because "
2354 "Port has NoIPv6Traffic set.",
2355 safe_str_client(socks->
address));
2356 connection_mark_unattached_ap(conn, END_STREAM_REASON_ENTRYPOLICY);
2360 tor_assert_nonfatal_unreached_once();
2371 strlcpy(socks->
address, rr.orig_address,
sizeof(socks->
address));
2374 connection_mark_unattached_ap(conn,
2375 END_STREAM_REASON_DONE |
2386 if (socks->
port == 0) {
2387 log_notice(
LD_APP,
"Application asked to connect to port 0. Refusing.");
2388 connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
2402 tor_addr_is_internal(&addr, 0))) {
2407#define WARN_INTRVL_LOOP 300
2408 static ratelim_t loop_warn_limit = RATELIM_INIT(WARN_INTRVL_LOOP);
2412 "Rejecting request for anonymous connection to private "
2413 "address %s on a TransPort or NATDPort. Possible loop "
2414 "in your NAT rules?%s", safe_str_client(socks->
address),
2419#define WARN_INTRVL_PRIV 300
2420 static ratelim_t priv_warn_limit = RATELIM_INIT(WARN_INTRVL_PRIV);
2424 "Rejecting SOCKS request for anonymous connection to "
2425 "private address %s.%s",
2426 safe_str_client(socks->
address),m);
2446 if ((family == AF_INET && ! conn->entry_cfg.ipv4_traffic) ||
2447 (family == AF_INET6 && ! conn->entry_cfg.ipv6_traffic)) {
2450 log_warn(
LD_NET,
"Rejecting SOCKS request for an IP address "
2451 "family that this listener does not support.");
2452 connection_mark_unattached_ap(conn, END_STREAM_REASON_ENTRYPOLICY);
2454 }
else if (family == AF_INET6 && socks->
socks_version == 4) {
2457 log_warn(
LD_NET,
"Rejecting SOCKS4 request for an IPv6 address.");
2458 connection_mark_unattached_ap(conn, END_STREAM_REASON_ENTRYPOLICY);
2461 !conn->entry_cfg.ipv4_traffic) {
2465 log_warn(
LD_NET,
"Rejecting SOCKS4 request on a listener with "
2466 "no IPv4 traffic supported.");
2467 connection_mark_unattached_ap(conn, END_STREAM_REASON_ENTRYPOLICY);
2469 }
else if (family == AF_INET6) {
2472 conn->entry_cfg.ipv4_traffic = 0;
2473 }
else if (family == AF_INET) {
2476 conn->entry_cfg.ipv6_traffic = 0;
2488 log_warn(
LD_APP,
"Not attempting connection to %s:%d because "
2489 "the network would reject it. Are you trying to send "
2490 "Tor traffic over Tor? This traffic can be harmful to "
2491 "the Tor network. If you really need it, try using "
2492 "a bridge as a workaround.",
2494 connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
2502 conn->entry_cfg.ipv6_traffic = 0;
2516 "Redirecting address %s to exit at enclave router %s",
2560 connection_ap_mark_as_pending_circuit(conn);
2577 tor_assert(addresstype == ONION_V3_HOSTNAME);
2586static int pf_socket = -1;
2604 log_warn(
LD_NET,
"open(\"/dev/pf\") failed: %s", strerror(errno));
2613#if defined(TRANS_NETFILTER) || defined(TRANS_PF) || \
2614 defined(TRANS_TPROXY)
2620 struct sockaddr_storage orig_dst;
2621 socklen_t orig_dst_len =
sizeof(orig_dst);
2626 if (getsockname(
ENTRY_TO_CONN(conn)->s, (
struct sockaddr*)&orig_dst,
2627 &orig_dst_len) < 0) {
2629 log_warn(
LD_NET,
"getsockname() failed: %s", tor_socket_strerror(e));
2636#ifdef TRANS_NETFILTER
2639#ifdef TRANS_NETFILTER_IPV4
2641 rv = getsockopt(
ENTRY_TO_CONN(conn)->s, SOL_IP, SO_ORIGINAL_DST,
2642 (
struct sockaddr*)&orig_dst, &orig_dst_len);
2645#ifdef TRANS_NETFILTER_IPV6
2647 rv = getsockopt(
ENTRY_TO_CONN(conn)->s, SOL_IPV6, IP6T_SO_ORIGINAL_DST,
2648 (
struct sockaddr*)&orig_dst, &orig_dst_len);
2652 log_warn(
LD_BUG,
"Received transparent data from an unsupported "
2659 log_warn(
LD_NET,
"getsockopt() failed: %s", tor_socket_strerror(e));
2663#elif defined(TRANS_PF)
2664 if (getsockname(
ENTRY_TO_CONN(conn)->s, (
struct sockaddr*)&orig_dst,
2665 &orig_dst_len) < 0) {
2667 log_warn(
LD_NET,
"getsockname() failed: %s", tor_socket_strerror(e));
2674 log_warn(
LD_BUG,
"Unable to determine destination from socket.");
2690 struct sockaddr_storage proxy_addr;
2691 socklen_t proxy_addr_len =
sizeof(proxy_addr);
2692 struct sockaddr *proxy_sa = (
struct sockaddr*) &proxy_addr;
2693 struct pfioc_natlook pnl;
2697 if (getsockname(
ENTRY_TO_CONN(conn)->s, (
struct sockaddr*)&proxy_addr,
2698 &proxy_addr_len) < 0) {
2700 log_warn(
LD_NET,
"getsockname() to determine transocks destination "
2701 "failed: %s", tor_socket_strerror(e));
2706 if (
get_options()->TransProxyType_parsed == TPT_IPFW) {
2720 memset(&pnl, 0,
sizeof(pnl));
2721 pnl.proto = IPPROTO_TCP;
2722 pnl.direction = PF_OUT;
2723 if (proxy_sa->sa_family == AF_INET) {
2724 struct sockaddr_in *sin = (
struct sockaddr_in *)proxy_sa;
2728 pnl.daddr.v4.s_addr = sin->sin_addr.s_addr;
2729 pnl.dport = sin->sin_port;
2730 }
else if (proxy_sa->sa_family == AF_INET6) {
2737 memcpy(&pnl.saddr.v6, dest_in6,
sizeof(
struct in6_addr));
2739 memcpy(&pnl.daddr.v6, &sin6->sin6_addr,
sizeof(
struct in6_addr));
2740 pnl.dport = sin6->sin6_port;
2742 log_warn(
LD_NET,
"getsockname() gave an unexpected address family (%d)",
2743 (
int)proxy_sa->sa_family);
2747 pf = get_pf_socket();
2751 if (ioctl(pf, DIOCNATLOOK, &pnl) < 0) {
2752 log_warn(
LD_NET,
"ioctl(DIOCNATLOOK) failed: %s", strerror(errno));
2756 if (pnl.af == AF_INET) {
2758 }
else if (pnl.af == AF_INET6) {
2766 req->
port = ntohs(pnl.rdport);
2783#ifdef TRANS_NETFILTER
2784 return destination_from_socket(conn, req);
2785#elif defined(TRANS_PF)
2789 return destination_from_socket(conn, req);
2793 return destination_from_pf(conn, req);
2797 log_warn(
LD_BUG,
"Proxy destination determination mechanism %s unknown.",
2803 log_warn(
LD_BUG,
"Called connection_ap_get_original_destination, but no "
2804 "transparent proxy method was configured.");
2834 log_debug(
LD_APP,
"entered.");
2841 connection_buf_add((
const char*)socks->
reply, socks->
replylen,
2844 if (sockshere == -1) {
2851 if (sockshere == 0) {
2852 log_debug(
LD_APP,
"socks handshake not all here yet.");
2854 }
else if (sockshere == -1) {
2856 log_warn(
LD_APP,
"Fetching socks handshake failed. Closing.");
2860 connection_mark_unattached_ap(conn,
2866 if (SOCKS_COMMAND_IS_CONNECT(socks->
command))
2895 log_debug(
LD_APP,
"entered.");
2898 log_warn(
LD_APP,
"Fetching original destination failed. Closing.");
2899 connection_mark_unattached_ap(conn,
2924 char tmp_buf[36], *tbuf, *daddr;
2934 log_debug(
LD_APP,
"entered.");
2942 log_warn(
LD_APP,
"NATD handshake failed (DEST too long). Closing");
2948 log_warn(
LD_APP,
"NATD handshake was ill-formed; closing. The client "
2955 daddr = tbuf = &tmp_buf[0] + 6;
2956 if (!(tbuf = strchr(tbuf,
' '))) {
2957 log_warn(
LD_APP,
"NATD handshake was ill-formed; closing. The client "
2968 socks->
port = (uint16_t)
2971 log_warn(
LD_APP,
"NATD handshake failed; port %s is ill-formed or out "
2987static const char HTTP_CONNECT_IS_NOT_AN_HTTP_PROXY_MSG[] =
2988 "HTTP/1.0 405 Method Not Allowed\r\n"
2989 "Content-Type: text/html; charset=iso-8859-1\r\n\r\n"
2992 "<title>This is an HTTP CONNECT tunnel, not a full HTTP Proxy</title>\n"
2995 "<h1>This is an HTTP CONNECT tunnel, not an HTTP proxy.</h1>\n"
2997 "It appears you have configured your web browser to use this Tor port as\n"
3000 "This is not correct: This port is configured as a CONNECT tunnel, not\n"
3001 "an HTTP proxy. Please configure your client accordingly. You can also\n"
3002 "use HTTPS; then the client should automatically use HTTP CONNECT."
3005 "See <a href=\"https://www.torproject.org/documentation.html\">"
3006 "https://www.torproject.org/documentation.html</a> for more "
3024 char *headers = NULL, *body = NULL;
3025 char *
command = NULL, *addrport = NULL;
3029 const char *errmsg = NULL;
3032 const int http_status =
3034 &body, &bodylen, 1024, 0);
3035 if (http_status < 0) {
3037 errmsg =
"HTTP/1.0 400 Bad Request\r\n\r\n";
3039 }
else if (http_status == 0) {
3045 if (cmd_status < 0) {
3046 errmsg =
"HTTP/1.0 400 Bad Request\r\n\r\n";
3051 if (strcasecmp(
command,
"connect")) {
3052 errmsg = HTTP_CONNECT_IS_NOT_AN_HTTP_PROXY_MSG;
3060 errmsg =
"HTTP/1.0 400 Bad Request\r\n\r\n";
3063 if (strlen(addr) >= MAX_SOCKS_ADDR_LEN) {
3064 errmsg =
"HTTP/1.0 414 Request-URI Too Long\r\n\r\n";
3071 char *authorization =
http_get_header(headers,
"Proxy-Authorization: ");
3072 if (authorization) {
3076 char *isolation =
http_get_header(headers,
"X-Tor-Stream-Isolation: ");
3097 if (BUG(errmsg == NULL))
3098 errmsg =
"HTTP/1.0 400 Bad Request\r\n\r\n";
3099 log_info(
LD_EDGE,
"HTTP tunnel error: saying %s",
escaped(errmsg));
3100 connection_buf_add(errmsg, strlen(errmsg),
ENTRY_TO_CONN(conn));
3104 connection_mark_unattached_ap(conn,
3125 uint32_t attempts=0;
3129 if (++attempts > 1<<16) {
3131 log_warn(
LD_APP,
"No unused stream IDs. Failing.");
3134 if (test_stream_id == 0)
3137 if (tmpconn->
stream_id == test_stream_id)
3148 return test_stream_id;
3178 const node_t *exitnode = NULL;
3192 if (ap_conn->entry_cfg.ipv4_traffic && !ap_conn->entry_cfg.ipv6_traffic)
3195 if (! cpath_layer ||
3199 if (!ap_conn->entry_cfg.ipv4_traffic)
3204 if (ap_conn->entry_cfg.ipv6_traffic && exitnode) {
3219 if (ap_conn->entry_cfg.prefer_ipv6)
3224 log_warn(
LD_EDGE,
"I'm about to ask a node for a connection that I "
3225 "am telling it to fulfil with neither IPv4 nor IPv6. That's "
3226 "not going to work. Did you perhaps ask for an IPv6 address "
3227 "on an IPv4Only port, or vice versa?");
3260 connection_mark_unattached_ap(ap_conn, END_STREAM_REASON_INTERNAL);
3272 circ->base_.
purpose == CIRCUIT_PURPOSE_CONFLUX_LINKED ||
3276 payload_len = (int)strlen(payload)+1;
3277 if (payload_len <= RELAY_PAYLOAD_SIZE - 4 && edge_conn->begincell_flags) {
3278 set_uint32(payload + payload_len, htonl(edge_conn->begincell_flags));
3283 "Sending relay cell %d on circ %u to begin stream %d.",
3289 RELAY_COMMAND_BEGIN_DIR : RELAY_COMMAND_BEGIN;
3292 if (begin_type == RELAY_COMMAND_BEGIN) {
3296 assert_circ_anonymity_ok(circ, options);
3297 }
else if (begin_type == RELAY_COMMAND_BEGIN_DIR) {
3301 if (BUG(!base_conn->
linked)) {
3307 if (!linked_dir_conn_base) {
3315 TO_DIR_CONN(linked_dir_conn_base)->router_purpose,
3316 TO_DIR_CONN(linked_dir_conn_base)->requested_resource)) {
3317 assert_circ_anonymity_ok(circ, options);
3322 tor_assert_unreached();
3326 begin_type == RELAY_COMMAND_BEGIN ? payload : NULL,
3327 begin_type == RELAY_COMMAND_BEGIN ? payload_len : 0) < 0)
3333 log_info(
LD_APP,
"Address/port sent, ap socket "TOR_SOCKET_T_FORMAT
3335 base_conn->
s, (
unsigned)circ->base_.
n_circ_id);
3339 if ((connection_get_inbuf_len(base_conn) ||
3340 ap_conn->sending_optimistic_data) &&
3342 log_info(
LD_APP,
"Sending up to %ld + %ld bytes of queued-up data",
3343 (
long)connection_get_inbuf_len(base_conn),
3344 ap_conn->sending_optimistic_data ?
3345 (
long)
buf_datalen(ap_conn->sending_optimistic_data) : 0);
3347 connection_mark_for_close(base_conn);
3363 const char *string_addr;
3375 circ->base_.
purpose == CIRCUIT_PURPOSE_CONFLUX_LINKED);
3384 connection_mark_unattached_ap(ap_conn, END_STREAM_REASON_INTERNAL);
3393 payload_len = (int)strlen(string_addr)+1;
3404 log_warn(
LD_APP,
"Rejecting ill-formed reverse lookup of %s",
3405 safe_str_client(a));
3406 connection_mark_unattached_ap(ap_conn, END_STREAM_REASON_INTERNAL);
3412 log_warn(
LD_BUG,
"Couldn't generate reverse lookup hostname of %s",
3413 safe_str_client(a));
3414 connection_mark_unattached_ap(ap_conn, END_STREAM_REASON_INTERNAL);
3418 string_addr = inaddr_buf;
3419 payload_len = (int)strlen(inaddr_buf)+1;
3420 tor_assert(payload_len <= (
int)
sizeof(inaddr_buf));
3424 "Sending relay cell to begin stream %d.", edge_conn->
stream_id);
3427 RELAY_COMMAND_RESOLVE,
3428 string_addr, payload_len) < 0)
3436 log_info(
LD_APP,
"Address sent for resolve, ap socket "TOR_SOCKET_T_FORMAT
3438 base_conn->
s, (
unsigned)circ->base_.
n_circ_id);
3452 char *address, uint16_t port,
3454 int session_group,
int isolation_flags,
3455 int use_begindir,
int want_onehop)
3460 log_info(
LD_APP,
"Making internal %s tunnel to %s:%d ...",
3461 want_onehop ?
"direct" :
"anonymized",
3462 safe_str_client(address), port);
3493 base_conn->
address = tor_strdup(
"(Tor_internal)");
3495 base_conn->
port = 0;
3499 if (connection_add(base_conn) < 0) {
3500 connection_free(base_conn);
3509 connection_ap_mark_as_pending_circuit(conn);
3510 log_info(
LD_APP,
"... application connection created and linked.");
3525 uint64_t stream_id = 0;
3533 expires = time(NULL) + ttl;
3534 if (answer_type == RESOLVED_TYPE_IPV4 && answer_len >= 4) {
3538 cp, expires, NULL, 0, stream_id);
3540 }
else if (answer_type == RESOLVED_TYPE_HOSTNAME && answer_len < 256) {
3541 char *cp = tor_strndup(answer, answer_len);
3543 cp, expires, NULL, 0, stream_id);
3547 "<error>", time(NULL)+ttl,
3548 "error=yes", 0, stream_id);
3573 log_warn(
LD_BUG,
"Got called with address of unexpected family %d",
3576 RESOLVED_TYPE_ERROR,0,NULL,-1,-1);
3593 const uint8_t *answer,
3601 if (answer_type == RESOLVED_TYPE_IPV4 && answer_len == 4) {
3609 }
else if (answer_type == RESOLVED_TYPE_IPV6 && answer_len == 16) {
3617 }
else if (answer_type == RESOLVED_TYPE_HOSTNAME && answer_len < 256) {
3618 char *cp = tor_strndup((
char*)answer, answer_len);
3638 (
char*)answer, ttl, expires);
3647 if (answer_type == RESOLVED_TYPE_IPV4 && answer_len == 4) {
3648 buf[1] = SOCKS4_GRANTED;
3650 memcpy(buf+4, answer, 4);
3653 buf[1] = SOCKS4_REJECT;
3654 memset(buf+2, 0, 6);
3660 if (answer_type == RESOLVED_TYPE_IPV4 && answer_len == 4) {
3661 buf[1] = SOCKS5_SUCCEEDED;
3664 memcpy(buf+4, answer, 4);
3667 }
else if (answer_type == RESOLVED_TYPE_IPV6 && answer_len == 16) {
3668 buf[1] = SOCKS5_SUCCEEDED;
3671 memcpy(buf+4, answer, 16);
3674 }
else if (answer_type == RESOLVED_TYPE_HOSTNAME && answer_len < 256) {
3675 buf[1] = SOCKS5_SUCCEEDED;
3678 buf[4] = (char)answer_len;
3679 memcpy(buf+5, answer, answer_len);
3681 replylen = 5+answer_len+2;
3683 buf[1] = SOCKS5_HOST_UNREACHABLE;
3684 memset(buf+2, 0, 8);
3692 (answer_type == RESOLVED_TYPE_IPV4 ||
3693 answer_type == RESOLVED_TYPE_IPV6 ||
3694 answer_type == RESOLVED_TYPE_HOSTNAME) ?
3695 0 : END_STREAM_REASON_RESOLVEFAILED);
3709 size_t replylen,
int endreason)
3725 STREAM_EVENT_SUCCEEDED : STREAM_EVENT_FAILED,
3731 if (status == SOCKS5_SUCCEEDED ||
3732 endreason == END_STREAM_REASON_RESOLVEFAILED ||
3733 endreason == END_STREAM_REASON_CONNECTREFUSED ||
3734 endreason == END_STREAM_REASON_CONNRESET ||
3735 endreason == END_STREAM_REASON_NOROUTE ||
3736 endreason == END_STREAM_REASON_RESOURCELIMIT) {
3739 if (endreason != END_STREAM_REASON_RESOLVEFAILED) {
3741 "No origin circuit for successful SOCKS stream %"PRIu64
3763 log_warn(
LD_BUG,
"(Harmless.) duplicate calls to "
3764 "connection_ap_handshake_socks_reply.");
3776 response =
"HTTP/1.0 400 Bad Request\r\n\r\n";
3778 connection_buf_add(response, strlen(response),
ENTRY_TO_CONN(conn));
3781 buf[1] = (status==SOCKS5_SUCCEEDED ? SOCKS4_GRANTED : SOCKS4_REJECT);
3786 memset(buf,0,
sizeof(buf));
3789 buf[1] = (char)status;
3796 buf[1] = (char)status;
3820 uint8_t *end_reason_out)
3823 const uint8_t *body, *nul;
3825 memset(bcell, 0,
sizeof(*bcell));
3826 *end_reason_out = END_STREAM_REASON_MISC;
3835 if (rh.
command == RELAY_COMMAND_BEGIN_DIR) {
3836 bcell->is_begindir = 1;
3838 }
else if (rh.
command != RELAY_COMMAND_BEGIN) {
3839 log_warn(
LD_BUG,
"Got an unexpected command %d", (
int)rh.
command);
3840 *end_reason_out = END_STREAM_REASON_INTERNAL;
3845 nul = memchr(body, 0, rh.
length);
3848 "Relay begin cell has no \\0. Closing.");
3849 *end_reason_out = END_STREAM_REASON_TORPROTOCOL;
3855 &bcell->address,&bcell->port)<0) {
3857 "Unable to parse addr:port in relay begin cell. Closing.");
3858 *end_reason_out = END_STREAM_REASON_TORPROTOCOL;
3861 if (bcell->port == 0) {
3863 "Missing port in relay begin cell. Closing.");
3865 *end_reason_out = END_STREAM_REASON_TORPROTOCOL;
3868 if (body + rh.
length >= nul + 4)
3888 log_debug(
LD_REND,
"Connecting the hidden service rendezvous circuit "
3889 "to the service destination.");
3892 conn->base_.
address = tor_strdup(
"(rendezvous)");
3907 log_info(
LD_REND,
"Didn't find rendezvous service at %s",
3916 END_STREAM_REASON_DONE,
3948 if (conn->hs_ident) {
3951 export_hs_client_circuit_id(conn, circuit_id_protocol);
3985 char *address = NULL;
3993 uint8_t end_reason=0;
3994 dos_stream_defense_type_t dos_defense_type;
4007 return -END_CIRC_REASON_TORPROTOCOL;
4012 "Relay begin cell at non-server. Closing.");
4014 END_STREAM_REASON_EXITPOLICY, NULL);
4020 return -END_CIRC_REASON_TORPROTOCOL;
4021 }
else if (rv == -1) {
4027 if (! bcell.is_begindir) {
4030 address = bcell.address;
4033 if (or_circ && or_circ->
p_chan) {
4043 "Attempt by %s to open a stream %s. Closing.",
4045 client_chan ?
"on first hop of circuit" :
4046 "from unknown relay");
4049 END_STREAM_REASON_TORPROTOCOL :
4050 END_STREAM_REASON_MISC,
4056 }
else if (rh.
command == RELAY_COMMAND_BEGIN_DIR) {
4060 END_STREAM_REASON_NOTDIRECTORY, layer_hint);
4068 if (or_circ && or_circ->
p_chan &&
4072 address = tor_strdup(
"127.0.0.1");
4078 log_warn(
LD_BUG,
"Got an unexpected command %d", (
int)rh.
command);
4080 END_STREAM_REASON_INTERNAL, layer_hint);
4086 bcell.flags &= ~BEGIN_FLAG_IPV6_PREFERRED;
4091 END_STREAM_REASON_EXITPOLICY, layer_hint);
4096 log_debug(LD_EXIT,
"Creating new exit connection.");
4106 n_stream->begincell_flags = bcell.flags;
4108 n_stream->base_.
port = port;
4127 n_stream->base_.
address = address;
4134 END_STREAM_REASON_HIBERNATING, NULL);
4141 if (rh.
command == RELAY_COMMAND_BEGIN_DIR) {
4151 log_debug(LD_EXIT,
"about to start the dns_resolve().");
4155 dos_defense_type = dos_stream_new_begin_or_resolve_cell(or_circ);
4156 switch (dos_defense_type) {
4157 case DOS_STREAM_DEFENSE_NONE:
4159 case DOS_STREAM_DEFENSE_REFUSE_STREAM:
4166 case DOS_STREAM_DEFENSE_CLOSE_CIRCUIT:
4168 return -END_CIRC_REASON_RESOURCELIMIT;
4175 log_debug(LD_EXIT,
"about to call connection_exit_connect().");
4180 END_STREAM_REASON_RESOLVEFAILED, NULL);
4203 dos_stream_defense_type_t dos_defense_type;
4222 dummy_conn->base_.
address = tor_strndup(
4225 dummy_conn->base_.
port = 0;
4231 dos_defense_type = dos_stream_new_begin_or_resolve_cell(circ);
4232 switch (dos_defense_type) {
4233 case DOS_STREAM_DEFENSE_NONE:
4235 case DOS_STREAM_DEFENSE_REFUSE_STREAM:
4236 dns_send_resolved_error_cell(dummy_conn, RESOLVED_TYPE_ERROR_TRANSIENT);
4239 case DOS_STREAM_DEFENSE_CLOSE_CIRCUIT:
4241 return -END_CIRC_REASON_RESOURCELIMIT;
4266 const char **why_rejected)
4272 *why_rejected =
" (IPv6 address without IPv6Exit configured)";
4285 int marked_for_close = 0;
4288 int config_change_relevant = 0;
4295 line && !config_change_relevant;
4296 line = line->next) {
4297 const char* exit_policy_options[] = {
4300 "ReducedExitPolicy",
4301 "ReevaluateExitPolicy",
4305 for (
unsigned int i = 0; exit_policy_options[i] != NULL; ++i) {
4306 if (strcmp(line->key, exit_policy_options[i]) == 0) {
4307 config_change_relevant = 1;
4313 if (!config_change_relevant) {
4336 connection_mark_for_close(conn);
4339 } SMARTLIST_FOREACH_END(conn);
4341 smartlist_free(conn_list);
4342 smartlist_free(policy);
4344 log_info(
LD_GENERAL,
"Marked %d connections to be closed as no longer "
4345 "allowed per ExitPolicy", marked_for_close);
4370 int socket_error = 0, result;
4371 const char *why_failed_exit_policy = NULL;
4376 edge_conn->base_.
port,
4377 &why_failed_exit_policy)) {
4378 if (BUG(!why_failed_exit_policy))
4379 why_failed_exit_policy =
"";
4380 log_info(LD_EXIT,
"%s failed exit policy%s. Closing.",
4382 why_failed_exit_policy);
4386 connection_free(conn);
4409 log_info(LD_EXIT,
"%s tried to connect back to a known relay address. "
4414 connection_free(conn);
4433 log_debug(LD_EXIT,
"about to try connecting");
4435 addr, port, &socket_error);
4446 log_debug(LD_EXIT,
"about to try connecting");
4447 result = connection_connect_unix(conn, conn->
address, &socket_error);
4456 connection_free(conn);
4470 if (connection_get_outbuf_len(conn)) {
4481 RELAY_COMMAND_CONNECTED,
4485 int connected_payload_len =
4488 if (connected_payload_len < 0) {
4491 connection_free(conn);
4496 RELAY_COMMAND_CONNECTED,
4497 (
char*)connected_payload,
4498 connected_payload_len);
4515 log_info(LD_EXIT,
"Opening local connection for anonymized directory exit");
4525 dirconn->base_.
port = 0;
4537 if (connection_add(
TO_CONN(exitconn))<0) {
4548 if (connection_add(
TO_CONN(dirconn))<0) {
4551 connection_mark_for_close(
TO_CONN(exitconn));
4560 RELAY_COMMAND_CONNECTED, NULL, 0) < 0) {
4561 connection_mark_for_close(
TO_CONN(exitconn));
4562 connection_mark_for_close(
TO_CONN(dirconn));
4577 if (conn->hs_ident) {
4602 const node_t *chosen_exit =
4623 }
else if (!conn->entry_cfg.ipv4_traffic && conn->entry_cfg.ipv6_traffic) {
4626 }
else if (conn->entry_cfg.ipv4_traffic && !conn->entry_cfg.ipv6_traffic) {
4655memeq_opt(
const char *a,
size_t alen,
const char *b,
size_t blen)
4659 }
else if (b == NULL) {
4661 }
else if (alen != blen) {
4696 log_warn(
LD_BUG,
"Reached connection_edge_compatible_with_circuit without "
4697 "having set conn->original_dest_address");
4714 circ->socks_username, circ->socks_username_len) ||
4716 circ->socks_password, circ->socks_password_len)))
4749 log_warn(
LD_BUG,
"Reached connection_update_circuit_isolation without "
4750 "having set conn->original_dest_address");
4767 circ->socks_username = sr->
username ?
4769 circ->socks_password = sr->
password ?
4783 circ->socks_username, circ->socks_username_len) ||
4785 circ->socks_password, circ->socks_password_len))
4801 log_warn(
LD_BUG,
"Updating a circuit with seemingly incompatible "
4802 "isolation flags.");
4824 log_warn(
LD_BUG,
"Tried to clear the isolation status of a dirty circuit");
4828 log_warn(
LD_BUG,
"Tried to clear the isolation status of a non-open "
4836 circ->client_proto_type = 0;
4837 circ->client_proto_socksver = 0;
4838 circ->dest_port = 0;
4841 circ->session_group = -1;
4842 circ->nym_epoch = 0;
4843 if (circ->socks_username) {
4844 memwipe(circ->socks_username, 0x11, circ->socks_username_len);
4847 if (circ->socks_password) {
4848 memwipe(circ->socks_password, 0x05, circ->socks_password_len);
4851 circ->socks_username_len = circ->socks_password_len = 0;
4870 connection_mark_for_close(
TO_CONN(conn));
4877 untried_pending_connections = 0;
void tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src)
void tor_addr_from_ipv4n(tor_addr_t *dest, uint32_t v4addr)
void tor_addr_make_unspec(tor_addr_t *a)
int tor_addr_hostname_is_local(const char *name)
int tor_addr_parse(tor_addr_t *addr, const char *src)
void tor_addr_make_null(tor_addr_t *a, sa_family_t family)
int tor_addr_port_split(int severity, const char *addrport, char **address_out, uint16_t *port_out)
int tor_addr_is_null(const tor_addr_t *addr)
char * tor_addr_to_str_dup(const tor_addr_t *addr)
int tor_addr_parse_PTR_name(tor_addr_t *result, const char *address, int family, int accept_regular)
void tor_addr_from_in6(tor_addr_t *dest, const struct in6_addr *in6)
const char * tor_addr_to_str(char *dest, const tor_addr_t *addr, size_t len, int decorate)
int tor_addr_from_sockaddr(tor_addr_t *a, const struct sockaddr *sa, uint16_t *port_out)
void tor_addr_from_ipv6_bytes(tor_addr_t *dest, const uint8_t *ipv6_bytes)
char * tor_dup_ip(uint32_t addr)
int tor_addr_to_PTR_name(char *out, size_t outlen, const tor_addr_t *addr)
static uint32_t tor_addr_to_ipv4n(const tor_addr_t *a)
#define REVERSE_LOOKUP_NAME_BUF_LEN
static sa_family_t tor_addr_family(const tor_addr_t *a)
static const struct in6_addr * tor_addr_to_in6(const tor_addr_t *a)
#define tor_addr_to_in6_addr8(x)
#define tor_addr_eq(a, b)
void client_dns_set_reverse_addressmap(entry_connection_t *for_conn, const char *address, const char *v, const char *exitname, int ttl)
void clear_trackexithost_mappings(const char *exitname)
int address_is_in_virtual_range(const char *address)
void client_dns_set_addressmap(entry_connection_t *for_conn, const char *address, const tor_addr_t *val, const char *exitname, int ttl)
int addressmap_rewrite(char *address, size_t maxlen, unsigned flags, time_t *expires_out, addressmap_entry_source_t *exit_source_out)
int addressmap_address_should_automap(const char *address, const or_options_t *options)
const char * addressmap_register_virtual_address(int type, char *new_address)
int addressmap_rewrite_reverse(char *address, size_t maxlen, unsigned flags, time_t *expires_out)
const char * hex_str(const char *from, size_t fromlen)
void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen)
size_t buf_datalen(const buf_t *buf)
int buf_set_to_copy(buf_t **output, const buf_t *input)
Header file for buffers.c.
static void set_uint16(void *cp, uint16_t v)
static void set_uint32(void *cp, uint32_t v)
static void set_uint8(void *cp, uint8_t v)
static uint32_t get_uint32(const void *cp)
Fixed-size cell structure.
int channel_is_client(const channel_t *chan)
int channel_get_addr_if_possible(const channel_t *chan, tor_addr_t *addr_out)
const char * channel_describe_peer(channel_t *chan)
Header file for channel.c.
const char * pathbias_state_to_string(path_state_t state)
void pathbias_mark_use_rollback(origin_circuit_t *circ)
void pathbias_mark_use_success(origin_circuit_t *circ)
Header file for circuitbuild.c.
circuit_t * circuit_get_by_edge_conn(edge_connection_t *conn)
origin_circuit_t * TO_ORIGIN_CIRCUIT(circuit_t *x)
void assert_circuit_ok(const circuit_t *c)
const char * circuit_state_to_string(int state)
or_circuit_t * TO_OR_CIRCUIT(circuit_t *x)
const char * circuit_purpose_to_string(uint8_t purpose)
Header file for circuitlist.c.
#define CIRCUIT_PURPOSE_IS_CLIENT(p)
#define CIRCUIT_PURPOSE_C_MEASURE_TIMEOUT
#define CIRCUIT_PURPOSE_PATH_BIAS_TESTING
#define CIRCUIT_STATE_OPEN
#define CIRCUIT_PURPOSE_C_REND_JOINED
#define CIRCUIT_PURPOSE_CONTROLLER
#define CIRCUIT_IS_ORIGIN(c)
#define CIRCUIT_PURPOSE_OR
#define CIRCUIT_PURPOSE_S_REND_JOINED
#define CIRCUIT_PURPOSE_S_HSDIR_POST
#define CIRCUIT_PURPOSE_C_HSDIR_GET
#define CIRCUIT_PURPOSE_C_GENERAL
void circpad_machine_event_circ_has_streams(origin_circuit_t *circ)
Header file for circuitpadding.c.
double get_circuit_build_timeout_ms(void)
Header file for circuitstats.c.
void circuit_detach_stream(circuit_t *circ, edge_connection_t *conn)
void circuit_read_valid_data(origin_circuit_t *circ, uint16_t relay_body_len)
int connection_ap_handshake_attach_circuit(entry_connection_t *conn)
int connection_ap_handshake_attach_chosen_circuit(entry_connection_t *conn, origin_circuit_t *circ, crypt_path_t *cpath)
void mark_circuit_unusable_for_new_conns(origin_circuit_t *circ)
Header file for circuituse.c.
#define SUBTYPE_P(p, subtype, basemember)
mainloop_event_t * mainloop_event_postloop_new(void(*cb)(mainloop_event_t *, void *), void *userdata)
void mainloop_event_activate(mainloop_event_t *event)
Header for compat_libevent.c.
uint64_t monotime_absolute_usec(void)
const char * escaped_safe_str_client(const char *address)
const char * escaped_safe_str(const char *address)
const or_options_t * get_options(void)
tor_cmdline_mode_t command
Header file for config.c.
uint64_t edge_get_max_rtt(const edge_connection_t *stream)
void conflux_sync_circ_fields(conflux_t *cfx, origin_circuit_t *ref_circ)
void conflux_update_half_streams(origin_circuit_t *circ, smartlist_t *half_streams)
Header file for conflux_util.c.
bool edge_uses_flow_control(const edge_connection_t *stream)
APIs for stream flow control on congestion controlled circuits.
edge_connection_t * edge_connection_new(int type, int socket_family)
void connection_link_connections(connection_t *conn_a, connection_t *conn_b)
int connection_buf_get_line(connection_t *conn, char *data, size_t *data_len)
void connection_mark_for_close_(connection_t *conn, int line, const char *file)
void connection_close_immediate(connection_t *conn)
int connection_state_is_open(connection_t *conn)
const char * connection_describe_peer(const connection_t *conn)
entry_connection_t * entry_connection_new(int type, int socket_family)
const char * connection_describe(const connection_t *conn)
dir_connection_t * dir_connection_new(int socket_family)
void connection_free_(connection_t *conn)
int connection_connect(connection_t *conn, const char *address, const tor_addr_t *addr, uint16_t port, int *socket_error)
const char * conn_state_to_string(int type, int state)
Header file for connection.c.
#define CONN_TYPE_AP_HTTP_CONNECT_LISTENER
#define CONN_TYPE_DIR_LISTENER
#define connection_mark_and_flush_(c, line, file)
int connection_edge_compatible_with_circuit(const entry_connection_t *conn, const origin_circuit_t *circ)
int connection_ap_handshake_rewrite_and_attach(entry_connection_t *conn, origin_circuit_t *circ, crypt_path_t *cpath)
int connection_half_edge_is_valid_data(const smartlist_t *half_conns, streamid_t stream_id)
void connection_ap_mark_as_waiting_for_renddesc(entry_connection_t *entry_conn)
void connection_mark_unattached_ap_(entry_connection_t *conn, int endreason, int line, const char *file)
static uint32_t connection_ap_get_begincell_flags(entry_connection_t *ap_conn)
void connection_edge_free_all(void)
void connection_ap_mark_as_non_pending_circuit(entry_connection_t *entry_conn)
static int connection_half_edge_compare_bsearch(const void *key, const void **member)
static size_t n_half_conns_allocated
int connection_ap_rewrite_and_attach_if_allowed(entry_connection_t *conn, origin_circuit_t *circ, crypt_path_t *cpath)
uint32_t clip_dns_fuzzy_ttl(uint32_t ttl)
static int connection_ap_supports_optimistic_data(const entry_connection_t *)
STATIC int connection_ap_process_http_connect(entry_connection_t *conn)
const edge_connection_t * CONST_TO_EDGE_CONN(const connection_t *c)
#define MAX_CONNECTED_CELL_PAYLOAD_LEN
entry_connection_t * connection_ap_make_link(connection_t *partner, char *address, uint16_t port, const char *digest, int session_group, int isolation_flags, int use_begindir, int want_onehop)
void connection_ap_expire_beginning(void)
void connection_ap_fail_onehop(const char *failed_digest, cpath_build_state_t *build_state)
static void connection_edge_about_to_close(edge_connection_t *edge_conn)
int connection_ap_detach_retriable(entry_connection_t *conn, origin_circuit_t *circ, int reason)
STATIC int connected_cell_format_payload(uint8_t *payload_out, const tor_addr_t *addr, uint32_t ttl)
void connection_ap_rescan_and_attach_pending(void)
int connection_ap_handshake_send_begin(entry_connection_t *ap_conn)
static int connection_ap_process_natd(entry_connection_t *conn)
static int consider_plaintext_ports(entry_connection_t *conn, uint16_t port)
void connection_ap_handshake_socks_reply(entry_connection_t *conn, char *reply, size_t replylen, int endreason)
static smartlist_t * pending_entry_connections
int connection_half_edge_is_valid_end(smartlist_t *half_conns, streamid_t stream_id)
void connection_exit_connect(edge_connection_t *edge_conn)
uint32_t clip_dns_ttl(uint32_t ttl)
static bool network_reentry_is_allowed(void)
bool connection_half_edges_waiting(const origin_circuit_t *circ)
int connection_ap_process_transparent(entry_connection_t *conn)
void connection_ap_mark_as_pending_circuit_(entry_connection_t *entry_conn, const char *fname, int lineno)
#define TRACKHOSTEXITS_RETRIES
static mainloop_event_t * attach_pending_entry_connections_ev
void connection_edge_end_close(edge_connection_t *conn, uint8_t reason)
int connection_edge_finished_connecting(edge_connection_t *edge_conn)
int connection_edge_destroy(circid_t circ_id, edge_connection_t *conn)
static int connection_exit_connect_dir(edge_connection_t *exitconn)
int connection_edge_finished_flushing(edge_connection_t *conn)
static int my_exit_policy_rejects(const tor_addr_t *addr, uint16_t port, const char **why_rejected)
void circuit_clear_isolation(origin_circuit_t *circ)
int connection_exit_begin_resolve(cell_t *cell, or_circuit_t *circ)
void connection_exit_about_to_close(edge_connection_t *edge_conn)
static int connection_ap_get_original_destination(entry_connection_t *conn, socks_request_t *req)
STATIC bool parse_extended_hostname(char *address, hostname_type_t *type_out)
int connection_half_edge_is_valid_connected(const smartlist_t *half_conns, streamid_t stream_id)
int connection_edge_flushed_some(edge_connection_t *conn)
entry_connection_t * EDGE_TO_ENTRY_CONN(edge_connection_t *c)
int connection_ap_handshake_send_resolve(entry_connection_t *ap_conn)
void connection_ap_handshake_socks_resolved_addr(entry_connection_t *conn, const tor_addr_t *answer, int ttl, time_t expires)
int connection_edge_update_circuit_isolation(const entry_connection_t *conn, origin_circuit_t *circ, int dry_run)
int connection_edge_reached_eof(edge_connection_t *conn)
const entry_connection_t * CONST_TO_ENTRY_CONN(const connection_t *c)
static void tell_controller_about_resolved_result(entry_connection_t *conn, int answer_type, size_t answer_len, const char *answer, int ttl, time_t expires)
int connection_ap_can_use_exit(const entry_connection_t *conn, const node_t *exit_node)
int connection_half_edge_is_valid_resolved(smartlist_t *half_conns, streamid_t stream_id)
int connection_edge_end_errno(edge_connection_t *conn)
int connection_edge_end(edge_connection_t *conn, uint8_t reason)
void connection_ap_attach_pending(int retry)
size_t half_streams_get_total_allocation(void)
int connection_half_edge_is_valid_sendme(const smartlist_t *half_conns, streamid_t stream_id)
int connection_edge_is_rendezvous_stream(const edge_connection_t *conn)
static int connection_ap_handle_onion(entry_connection_t *conn, socks_request_t *socks, origin_circuit_t *circ)
STATIC int begin_cell_parse(const cell_t *cell, begin_cell_t *bcell, uint8_t *end_reason_out)
void connection_ap_about_to_close(entry_connection_t *entry_conn)
static int relay_send_end_cell_from_edge(streamid_t stream_id, circuit_t *circ, uint8_t reason, crypt_path_t *cpath_layer)
const entry_connection_t * CONST_EDGE_TO_ENTRY_CONN(const edge_connection_t *c)
entry_connection_t * TO_ENTRY_CONN(connection_t *c)
STATIC void connection_half_edge_add(const edge_connection_t *conn, origin_circuit_t *circ)
static int handle_hs_exit_conn(circuit_t *circ, edge_connection_t *conn)
void connection_ap_handshake_socks_resolved(entry_connection_t *conn, int answer_type, size_t answer_len, const uint8_t *answer, int ttl, time_t expires)
static int memeq_opt(const char *a, size_t alen, const char *b, size_t blen)
int connection_exit_begin_conn(cell_t *cell, circuit_t *circ)
void connection_entry_set_controller_wait(entry_connection_t *conn)
streamid_t get_unique_stream_id_by_circ(origin_circuit_t *circ)
void half_edge_free_(half_edge_t *he)
void circuit_discard_optional_exit_enclaves(extend_info_t *info)
static int connection_ap_handshake_process_socks(entry_connection_t *conn)
edge_connection_t * TO_EDGE_CONN(connection_t *c)
int connection_edge_process_inbuf(edge_connection_t *conn, int package_partial)
STATIC half_edge_t * connection_half_edge_find_stream_id(const smartlist_t *half_conns, streamid_t stream_id)
static int compute_retry_timeout(entry_connection_t *conn)
Header file for connection_edge.c.
#define AP_CONN_STATE_HTTP_CONNECT_WAIT
#define EXIT_CONN_STATE_CONNECTING
#define AP_CONN_STATE_CONTROLLER_WAIT
#define EXIT_CONN_STATE_OPEN
#define AP_CONN_STATE_SOCKS_WAIT
int address_is_invalid_destination(const char *address, int client)
#define BEGIN_FLAG_IPV6_PREFERRED
#define EXIT_CONN_STATE_RESOLVEFAILED
#define AP_CONN_STATE_IS_UNATTACHED(s)
#define AP_CONN_STATE_CONNECT_WAIT
#define AP_CONN_STATE_OPEN
#define EXIT_PURPOSE_CONNECT
#define AP_CONN_STATE_RESOLVE_WAIT
#define BEGIN_FLAG_IPV4_NOT_OK
#define AP_CONN_STATE_CIRCUIT_WAIT
#define EXIT_CONN_STATE_RESOLVING
#define AP_CONN_STATE_NATD_WAIT
#define AP_CONN_STATE_RENDDESC_WAIT
#define BEGIN_FLAG_IPV6_OK
#define EXIT_PURPOSE_RESOLVE
int connection_or_digest_is_known_relay(const char *id_digest)
Header file for connection_or.c.
int control_event_stream_bandwidth(edge_connection_t *edge_conn)
int control_event_client_status(int severity, const char *format,...)
int control_event_stream_status(entry_connection_t *conn, stream_status_event_t tp, int reason_code)
int control_event_address_mapped(const char *from, const char *to, time_t expires, const char *error, const int cached, uint64_t stream_id)
Header file for control_events.c.
#define REMAP_STREAM_SOURCE_CACHE
Circuit-build-stse structure.
Common functions for using (pseudo-)random number generators.
unsigned crypto_rand_uint(unsigned limit)
void memwipe(void *mem, uint8_t byte, size_t sz)
Common functions for cryptographic routines.
const char * extend_info_describe(const extend_info_t *ei)
const char * node_describe(const node_t *node)
Header file for describe.c.
int tor_memeq(const void *a, const void *b, size_t sz)
#define tor_memneq(a, b, sz)
Client/server directory connection structure.
int purpose_needs_anonymity(uint8_t dir_purpose, uint8_t router_purpose, const char *resource)
dir_connection_t * TO_DIR_CONN(connection_t *c)
char * http_get_header(const char *headers, const char *which)
int parse_http_command(const char *headers, char **command_out, char **url_out)
Header file for directory.c.
#define DIR_CONN_STATE_SERVER_COMMAND_WAIT
#define DIR_PURPOSE_SERVER
int directory_permits_begindir_requests(const or_options_t *options)
Header file for dirserv.c.
void connection_dns_remove(edge_connection_t *conn)
int dns_resolve(edge_connection_t *exitconn)
void dnsserv_reject_request(entry_connection_t *conn)
void dnsserv_resolved(entry_connection_t *conn, int answer_type, size_t answer_len, const char *answer, int ttl)
Header file for dnsserv.c.
Entry connection structure.
#define ENTRY_TO_EDGE_CONN(c)
const char * escaped(const char *s)
bool extend_info_has_orport(const extend_info_t *ei, const tor_addr_t *addr, uint16_t port)
Header for core/or/extendinfo.c.
int tor_open_cloexec(const char *path, int flags, unsigned mode)
Half-open connection structure.
int we_are_hibernating(void)
Header file for hibernate.c.
const hs_descriptor_t * hs_cache_lookup_as_client(const ed25519_public_key_t *key)
Header file for hs_cache.c.
Header file containing circuit data for the whole HS subsystem.
int hs_client_any_intro_points_usable(const ed25519_public_key_t *service_pk, const hs_descriptor_t *desc)
int hs_client_refetch_hsdesc(const ed25519_public_key_t *identity_pk)
Header file containing client data for the HS subsystem.
@ HS_CLIENT_FETCH_PENDING
@ HS_CLIENT_FETCH_MISSING_INFO
@ HS_CLIENT_FETCH_NO_HSDIRS
@ HS_CLIENT_FETCH_HAVE_DESC
@ HS_CLIENT_FETCH_NOT_ALLOWED
@ HS_CLIENT_FETCH_LAUNCHED
int hs_parse_address(const char *address, ed25519_public_key_t *key_out, uint8_t *checksum_out, uint8_t *version_out)
int hs_address_is_valid(const char *address)
void hs_inc_rdv_stream_counter(origin_circuit_t *circ)
Header file containing common data for the whole HS subsystem.
#define HS_SERVICE_ADDR_LEN_BASE32
hs_ident_edge_conn_t * hs_ident_edge_conn_new(const ed25519_public_key_t *identity_pk)
hs_circuit_id_protocol_t hs_service_exports_circuit_id(const ed25519_public_key_t *pk)
int hs_service_set_conn_addr_port(const origin_circuit_t *circ, edge_connection_t *conn)