From: Marios Makassikis Date: Tue, 2 Apr 2013 04:07:35 +0000 (-0600) Subject: Add TPROXY support for OpenBSD, FreeBSD, NetBSD X-Git-Tag: SQUID_3_3_4~15 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=83bc5256edbdfb17c72c835236b83ebf23637077;p=thirdparty%2Fsquid.git Add TPROXY support for OpenBSD, FreeBSD, NetBSD This adds support for the PF 'divert-to' target which presents the client and remote IPs directly to Squid in accept() parameters the way Linux TPROXY target does. It also adds support for the SO_BINDANY option on outgoing traffic for client IP address spoofing which completes the TPROXY behaviour. To enable these features Squid built with --enable-pf-transparent can be configured with: http_port 1234 tproxy --- diff --git a/src/comm.cc b/src/comm.cc index 8020e0c193..3f107c4203 100644 --- a/src/comm.cc +++ b/src/comm.cc @@ -34,6 +34,7 @@ #include "squid.h" #include "base/AsyncCall.h" +#include "cbdata.h" #include "comm.h" #include "ClientInfo.h" #include "CommCalls.h" @@ -63,11 +64,12 @@ #include "SquidTime.h" #include "StatCounters.h" #include "StoreIOBuffer.h" +#include "tools.h" + #if USE_SSL #include "ssl/support.h" #endif -#include "cbdata.h" #if _SQUID_CYGWIN_ #include #endif @@ -493,12 +495,13 @@ comm_set_v6only(int fd, int tos) } /** - * Set the socket IP_TRANSPARENT option for Linux TPROXY v4 support. + * Set the socket IP_TRANSPARENT option for Linux TPROXY v4 support, + * or set the socket SO_BINDANY option for BSD divert-to support. */ void comm_set_transparent(int fd) { -#if defined(IP_TRANSPARENT) +#if _SQUID_LINUX_ && defined(IP_TRANSPARENT) int tos = 1; if (setsockopt(fd, SOL_IP, IP_TRANSPARENT, (char *) &tos, sizeof(int)) < 0) { debugs(50, DBG_IMPORTANT, "comm_open: setsockopt(IP_TRANSPARENT) on FD " << fd << ": " << xstrerror()); @@ -506,6 +509,18 @@ comm_set_transparent(int fd) /* mark the socket as having transparent options */ fd_table[fd].flags.transparent = 1; } + +#elif defined(SO_BINDANY) + int tos = 1; + enter_suid(); + if (setsockopt(fd, SOL_SOCKET, SO_BINDANY, (char *) &tos, sizeof(int)) < 0) { + debugs(50, DBG_IMPORTANT, "comm_open: setsockopt(SO_BINDANY) on FD " << fd << ": " << xstrerror()); + } else { + /* mark the socket as having transparent options */ + fd_table[fd].flags.transparent = true; + } + leave_suid(); + #else debugs(50, DBG_CRITICAL, "WARNING: comm_open: setsockopt(IP_TRANSPARENT) not supported on this platform"); #endif /* sockopt */ diff --git a/src/ip/Intercept.cc b/src/ip/Intercept.cc index b00ff27596..182b1f6c61 100644 --- a/src/ip/Intercept.cc +++ b/src/ip/Intercept.cc @@ -276,6 +276,21 @@ Ip::Intercept::IpfInterception(const Comm::ConnectionPointer &newConn, int silen return false; } +bool +Ip::Intercept::PfTransparent(const Comm::ConnectionPointer &newConn, int silent) +{ +#if PF_TRANSPARENT && defined(SO_BINDANY) + /* Trust the user configured properly. If not no harm done. + * We will simply attempt a bind outgoing on our own IP. + */ + newConn->remote.SetPort(0); // allow random outgoing port to prevent address clashes + debugs(89, 5, HERE << "address DIVERT: " << newConn); + return true; +#else + return false; +#endif +} + bool Ip::Intercept::PfInterception(const Comm::ConnectionPointer &newConn, int silent) { @@ -352,6 +367,7 @@ Ip::Intercept::Lookup(const Comm::ConnectionPointer &newConn, const Comm::Connec /* NP: try TPROXY first, its much quieter than NAT when non-matching */ if (transparentActive_ && listenConn->flags&COMM_TRANSPARENT) { if (NetfilterTransparent(newConn, silent)) return true; + if (PfTransparent(newConn, silent)) return true; } /* NAT is only available in IPv4 */ @@ -378,9 +394,8 @@ Ip::Intercept::Lookup(const Comm::ConnectionPointer &newConn, const Comm::Connec bool Ip::Intercept::ProbeForTproxy(Ip::Address &test) { - debugs(3, 3, "Detect TPROXY support on port " << test); - #if defined(IP_TRANSPARENT) + debugs(3, 3, "Detect TPROXY support on port " << test); int tos = 1; int tmp_sock = -1; @@ -435,8 +450,51 @@ Ip::Intercept::ProbeForTproxy(Ip::Address &test) } } -#else /* undefined IP_TRANSPARENT */ - debugs(3, 3, "setsockopt(IP_TRANSPARENT) not supported on this platform. Disabling TPROXYv4."); +#elif defined(SO_BINDANY) + debugs(3, 3, "Detect BINDANY support on port " << test); + + int tos = 1; + int tmp_sock = -1; + + if (test.IsIPv6()) { + debugs(3, 3, "...Probing for IPv6 SO_BINDANY support."); + + struct sockaddr_in6 tmp_ip6; + Ip::Address tmp = "::2"; + tmp.SetPort(0); + tmp.GetSockAddr(tmp_ip6); + + if ((tmp_sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP)) >=0 && + (setsockopt(tmp_sock, SOL_SOCKET, SO_BINDANY, (char *)&tos, + sizeof(tos)) == 0) && + (bind(tmp_sock, (struct sockaddr*)&tmp_ip6, sizeof(struct sockaddr_in6)) == 0)) { + debugs(3, 3, "IPv6 BINDANY support detected. Using."); + close(tmp_sock); + return true; + } + } + + if (test.IsIPv4()) { + debugs(3, 3, "...Probing for IPv4 SO_BINDANY support."); + + struct sockaddr_in tmp_ip4; + Ip::Address tmp = "127.0.0.2"; + tmp.SetPort(0); + tmp.GetSockAddr(tmp_ip4); + + if ((tmp_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) >=0 && + (setsockopt(tmp_sock, SOL_SOCKET, SO_BINDANY, (char *)&tos, + sizeof(tos)) == 0) && + (bind(tmp_sock, (struct sockaddr*)&tmp_ip4, sizeof(struct sockaddr_in)) == 0)) { + debugs(3, 3, "IPv4 BINDANY support detected. Using."); + close(tmp_sock); + return true; + } + } + +#else + debugs(3, 3, "TPROXY setsockopt() not supported on this platform. Disabling TPROXY."); + #endif return false; } diff --git a/src/ip/Intercept.h b/src/ip/Intercept.h index 6f8444f11c..f65d8ba8da 100644 --- a/src/ip/Intercept.h +++ b/src/ip/Intercept.h @@ -124,7 +124,7 @@ private: bool IpfInterception(const Comm::ConnectionPointer &newConn, int silent); /** - * perform Lookups on PF interception. + * perform Lookups on PF interception target (REDIRECT). * * \param silent 0 if errors are to be displayed. 1 if errors are to be hidden. * \param newConn Details known, to be updated where relevant. @@ -132,6 +132,15 @@ private: */ bool PfInterception(const Comm::ConnectionPointer &newConn, int silent); + /** + * perform Lookups on PF fully-transparent interception target (DIVERT). + * + * \param silent 0 if errors are to be displayed. 1 if errors are to be hidden. + * \param newConn Details known, to be updated where relevant. + * \return Whether successfuly located the new address. + */ + bool PfTransparent(const Comm::ConnectionPointer &newConn, int silent); + int transparentActive_; int interceptActive_; time_t lastReported_; /**< Time of last error report. Throttles NAT error display to 1 per minute */