aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlex Xu (Hello71) <alex_y_xu@yahoo.ca>2022-05-21 15:21:25 -0400
committerAlex Xu (Hello71) <alex_y_xu@yahoo.ca>2022-06-15 08:35:40 -0400
commitb65ffa6f06b2d7bc313e0780f3d76a8acb499ac9 (patch)
tree1be521d33b6a19749316e93df222b56f75f89864 /src
parentb733f9d6ace63c710bc4b567627500cfbeb1592d (diff)
downloadtor-b65ffa6f06b2d7bc313e0780f3d76a8acb499ac9.tar.gz
tor-b65ffa6f06b2d7bc313e0780f3d76a8acb499ac9.zip
Enable IP_BIND_ADDRESS_NO_PORT if supported
Diffstat (limited to 'src')
-rw-r--r--src/core/mainloop/connection.c17
-rw-r--r--src/lib/sandbox/sandbox.c8
2 files changed, 25 insertions, 0 deletions
diff --git a/src/core/mainloop/connection.c b/src/core/mainloop/connection.c
index 75fc3c8918..82cd19181d 100644
--- a/src/core/mainloop/connection.c
+++ b/src/core/mainloop/connection.c
@@ -2224,6 +2224,23 @@ connection_connect_sockaddr,(connection_t *conn,
tor_socket_strerror(errno));
}
+#ifdef IP_BIND_ADDRESS_NO_PORT
+ static int try_ip_bind_address_no_port = 1;
+ if (bindaddr && try_ip_bind_address_no_port &&
+ setsockopt(s, SOL_IP, IP_BIND_ADDRESS_NO_PORT, &(int){1}, sizeof(int))) {
+ if (errno == EINVAL) {
+ log_notice(LD_NET, "Tor was built with support for "
+ "IP_BIND_ADDRESS_NO_PORT, but the current kernel "
+ "doesn't support it. This might cause Tor to run out "
+ "of ephemeral ports more quickly.");
+ try_ip_bind_address_no_port = 0;
+ } else {
+ log_warn(LD_NET, "Error setting IP_BIND_ADDRESS_NO_PORT on new "
+ "connection: %s", tor_socket_strerror(errno));
+ }
+ }
+#endif
+
if (bindaddr && bind(s, bindaddr, bindaddr_len) < 0) {
*socket_error = tor_socket_errno(s);
if (ERRNO_IS_EADDRINUSE(*socket_error)) {
diff --git a/src/lib/sandbox/sandbox.c b/src/lib/sandbox/sandbox.c
index 7c024d7e37..a476e57fbc 100644
--- a/src/lib/sandbox/sandbox.c
+++ b/src/lib/sandbox/sandbox.c
@@ -1033,6 +1033,14 @@ sb_setsockopt(scmp_filter_ctx ctx, sandbox_cfg_t *filter)
return rc;
#endif /* defined(IPV6_V6ONLY) */
+#ifdef IP_BIND_ADDRESS_NO_PORT
+ rc = seccomp_rule_add_2(ctx, SCMP_ACT_ALLOW, SCMP_SYS(setsockopt),
+ SCMP_CMP(1, SCMP_CMP_EQ, SOL_IP),
+ SCMP_CMP(2, SCMP_CMP_EQ, IP_BIND_ADDRESS_NO_PORT));
+ if (rc)
+ return rc;
+#endif
+
return 0;
}