Blame SOURCES/squid-4.11-convert-ipv4.patch

5871b5
From 771908d313ee9c255adfb5e4fdba4d6797c18409 Mon Sep 17 00:00:00 2001
5871b5
From: Amos Jeffries <yadij@users.noreply.github.com>
5871b5
Date: Thu, 7 Mar 2019 13:50:38 +0000
5871b5
Subject: [PATCH] Bug 4928: Cannot convert non-IPv4 to IPv4 (#379)
5871b5
5871b5
... when reaching client_ip_max_connections
5871b5
5871b5
The client_ip_max_connections limit is checked before the TCP dst-IP is located for the newly received TCP connection. This leaves Squid unable to fetch the NFMARK or similar
5871b5
details later on (they do not exist for [::]).
5871b5
5871b5
Move client_ip_max_connections test later in the TCP accept process to ensure dst-IP is known when the error is produced.
5871b5
---
5871b5
 src/comm/TcpAcceptor.cc | 82 ++++++++++++++++++++---------------------
5871b5
 1 file changed, 39 insertions(+), 43 deletions(-)
5871b5
5871b5
diff --git a/src/comm/TcpAcceptor.cc b/src/comm/TcpAcceptor.cc
ceb136
index d4b576d..936aa30 100644
5871b5
--- a/src/comm/TcpAcceptor.cc
5871b5
+++ b/src/comm/TcpAcceptor.cc
ceb136
@@ -282,7 +282,16 @@ Comm::TcpAcceptor::acceptOne()
5871b5
     ConnectionPointer newConnDetails = new Connection();
5871b5
     const Comm::Flag flag = oldAccept(newConnDetails);
5871b5
 
ceb136
-    if (flag == Comm::COMM_ERROR) {
ceb136
+    /* Check for errors */
ceb136
+    if (!newConnDetails->isOpen()) {
ceb136
+
ceb136
+        if (flag == Comm::NOMESSAGE) {
ceb136
+            /* register interest again */
ceb136
+            debugs(5, 5, HERE << "try later: " << conn << " handler Subscription: " << theCallSub);
ceb136
+            SetSelect(conn->fd, COMM_SELECT_READ, doAccept, this, 0);
ceb136
+            return;
ceb136
+        }
ceb136
+
5871b5
         // A non-recoverable error; notify the caller */
5871b5
         debugs(5, 5, HERE << "non-recoverable error:" << status() << " handler Subscription: " << theCallSub);
5871b5
         if (intendedForUserConnections())
ceb136
@@ -292,16 +301,12 @@ Comm::TcpAcceptor::acceptOne()
5871b5
         return;
5871b5
     }
5871b5
 
ceb136
-    if (flag == Comm::NOMESSAGE) {
ceb136
-        /* register interest again */
ceb136
-        debugs(5, 5, "try later: " << conn << " handler Subscription: " << theCallSub);
ceb136
-    } else {
ceb136
-        debugs(5, 5, "Listener: " << conn <<
ceb136
-               " accepted new connection " << newConnDetails <<
ceb136
-               " handler Subscription: " << theCallSub);
ceb136
-        notify(flag, newConnDetails);
ceb136
-    }
ceb136
+    newConnDetails->nfmark = Ip::Qos::getNfmarkFromConnection(newConnDetails, Ip::Qos::dirAccepted);
5871b5
 
ceb136
+    debugs(5, 5, HERE << "Listener: " << conn <<
ceb136
+           " accepted new connection " << newConnDetails <<
ceb136
+           " handler Subscription: " << theCallSub);
ceb136
+    notify(flag, newConnDetails);
5871b5
     SetSelect(conn->fd, COMM_SELECT_READ, doAccept, this, 0);
5871b5
 }
5871b5
 
ceb136
@@ -341,8 +346,8 @@ Comm::TcpAcceptor::notify(const Comm::Flag flag, const Comm::ConnectionPointer &
5871b5
  *
5871b5
  * \retval Comm::OK          success. details parameter filled.
5871b5
  * \retval Comm::NOMESSAGE   attempted accept() but nothing useful came in.
ceb136
- *                           Or this client has too many connections already.
ceb136
  * \retval Comm::COMM_ERROR  an outright failure occurred.
ceb136
+ *                           Or this client has too many connections already.
5871b5
  */
5871b5
 Comm::Flag
5871b5
 Comm::TcpAcceptor::oldAccept(Comm::ConnectionPointer &details)
ceb136
@@ -383,6 +388,15 @@ Comm::TcpAcceptor::oldAccept(Comm::ConnectionPointer &details)
ceb136
 
5871b5
     details->remote = *gai;
5871b5
 
ceb136
+    if ( Config.client_ip_max_connections >= 0) {
ceb136
+        if (clientdbEstablished(details->remote, 0) > Config.client_ip_max_connections) {
ceb136
+            debugs(50, DBG_IMPORTANT, "WARNING: " << details->remote << " attempting more than " << Config.client_ip_max_connections << " connections.");
ceb136
+            Ip::Address::FreeAddr(gai);
ceb136
+            PROF_stop(comm_accept);
ceb136
+            return Comm::COMM_ERROR;
ceb136
+        }
ceb136
+    }
ceb136
+
5871b5
     // lookup the local-end details of this new connection
5871b5
     Ip::Address::InitAddr(gai);
5871b5
     details->local.setEmpty();
ceb136
@@ -396,6 +410,23 @@ Comm::TcpAcceptor::oldAccept(Comm::ConnectionPointer &details)
5871b5
     details->local = *gai;
5871b5
     Ip::Address::FreeAddr(gai);
5871b5
 
ceb136
+    /* fdstat update */
ceb136
+    fdd_table[sock].close_file = NULL;
ceb136
+    fdd_table[sock].close_line = 0;
5871b5
+
ceb136
+    fde *F = &fd_table[sock];
ceb136
+    details->remote.toStr(F->ipaddr,MAX_IPSTRLEN);
ceb136
+    F->remote_port = details->remote.port();
ceb136
+    F->local_addr = details->local;
ceb136
+    F->sock_family = details->local.isIPv6()?AF_INET6:AF_INET;
5871b5
+
ceb136
+    // set socket flags
ceb136
+    commSetCloseOnExec(sock);
ceb136
+    commSetNonBlocking(sock);
5871b5
+
ceb136
+    /* IFF the socket is (tproxy) transparent, pass the flag down to allow spoofing */
ceb136
+    F->flags.transparent = fd_table[conn->fd].flags.transparent; // XXX: can we remove this line yet?
5871b5
+
ceb136
     // Perform NAT or TPROXY operations to retrieve the real client/dest IP addresses
ceb136
     if (conn->flags&(COMM_TRANSPARENT|COMM_INTERCEPTION) && !Ip::Interceptor.Lookup(details, conn)) {
ceb136
         debugs(50, DBG_IMPORTANT, "ERROR: NAT/TPROXY lookup failed to locate original IPs on " << details);
ceb136
@@ -414,33 +445,6 @@ Comm::TcpAcceptor::oldAccept(Comm::ConnectionPointer &details)
ceb136
     }
ceb136
 #endif
5871b5
 
ceb136
-    details->nfmark = Ip::Qos::getNfmarkFromConnection(details, Ip::Qos::dirAccepted);
5871b5
-
ceb136
-    if (Config.client_ip_max_connections >= 0) {
ceb136
-        if (clientdbEstablished(details->remote, 0) > Config.client_ip_max_connections) {
ceb136
-            debugs(50, DBG_IMPORTANT, "WARNING: " << details->remote << " attempting more than " << Config.client_ip_max_connections << " connections.");
ceb136
-            PROF_stop(comm_accept);
ceb136
-            return Comm::NOMESSAGE;
5871b5
-        }
5871b5
-    }
ceb136
-
ceb136
-    /* fdstat update */
ceb136
-    fdd_table[sock].close_file = NULL;
ceb136
-    fdd_table[sock].close_line = 0;
ceb136
-
ceb136
-    fde *F = &fd_table[sock];
ceb136
-    details->remote.toStr(F->ipaddr,MAX_IPSTRLEN);
ceb136
-    F->remote_port = details->remote.port();
ceb136
-    F->local_addr = details->local;
ceb136
-    F->sock_family = details->local.isIPv6()?AF_INET6:AF_INET;
ceb136
-
ceb136
-    // set socket flags
ceb136
-    commSetCloseOnExec(sock);
ceb136
-    commSetNonBlocking(sock);
ceb136
-
ceb136
-    /* IFF the socket is (tproxy) transparent, pass the flag down to allow spoofing */
ceb136
-    F->flags.transparent = fd_table[conn->fd].flags.transparent; // XXX: can we remove this line yet?
5871b5
-
5871b5
     PROF_stop(comm_accept);
5871b5
     return Comm::OK;
5871b5
 }