https://git.reactos.org/?p=reactos.git;a=commitdiff;h=874d317a7104e01571ea39b56d3d603246937fda

commit 874d317a7104e01571ea39b56d3d603246937fda
Author:     Doug Lyons <[email protected]>
AuthorDate: Fri Feb 17 16:11:00 2023 -0600
Commit:     GitHub <[email protected]>
CommitDate: Sat Feb 18 00:11:00 2023 +0200

    [IP] Allow 0xFFFF as Valid Network Port Number (#5074)
    
    * Allow 0xFFFF as valid port number
    
    * Note possible reason for failure in TCPAllocatePort
    
    * Return NtStatus error on TCP/IP out-of-ports failure
    
    * Replace unavailable "ERR" with "DbgPrint"
---
 drivers/network/tcpip/ip/transport/tcp/accept.c | 12 ++++++++----
 drivers/network/tcpip/ip/transport/tcp/tcp.c    | 16 +++++++++++-----
 drivers/network/tcpip/tcpip/fileobjs.c          | 19 +++++++++++--------
 3 files changed, 30 insertions(+), 17 deletions(-)

diff --git a/drivers/network/tcpip/ip/transport/tcp/accept.c 
b/drivers/network/tcpip/ip/transport/tcp/accept.c
index 62c96cf5f33..4d12c527fae 100644
--- a/drivers/network/tcpip/ip/transport/tcp/accept.c
+++ b/drivers/network/tcpip/ip/transport/tcp/accept.c
@@ -77,10 +77,14 @@ NTSTATUS TCPListen(PCONNECTION_ENDPOINT Connection, UINT 
Backlog)
             if (NT_SUCCESS(Status))
             {
                 /* Allocate the port in the port bitmap */
-                Connection->AddressFile->Port = 
TCPAllocatePort(LocalAddress.Address[0].Address[0].sin_port);
-
-                /* This should never fail */
-                ASSERT(Connection->AddressFile->Port != 0xFFFF);
+                UINT AllocatedPort = 
TCPAllocatePort(LocalAddress.Address[0].Address[0].sin_port);
+                /* This should never fail unless all ports are in use */
+                if (AllocatedPort == (UINT) -1)
+                {
+                    DbgPrint("ERR: No more ports available.\n");
+                    return STATUS_TOO_MANY_ADDRESSES;
+                }
+                Connection->AddressFile->Port = AllocatedPort;
             }
         }
     }
diff --git a/drivers/network/tcpip/ip/transport/tcp/tcp.c 
b/drivers/network/tcpip/ip/transport/tcp/tcp.c
index 604ce11bd62..cd28ffaf71a 100644
--- a/drivers/network/tcpip/ip/transport/tcp/tcp.c
+++ b/drivers/network/tcpip/ip/transport/tcp/tcp.c
@@ -214,7 +214,7 @@ NTSTATUS TCPStartup(VOID)
 {
     NTSTATUS Status;
 
-    Status = PortsStartup( &TCPPorts, 1, 0xfffe );
+    Status = PortsStartup(&TCPPorts, 1, 0xffff);
     if (!NT_SUCCESS(Status))
     {
         return Status;
@@ -370,6 +370,8 @@ NTSTATUS TCPConnect
     /* Check if we had an unspecified port */
     if (!Connection->AddressFile->Port)
     {
+        UINT AllocatedPort;
+
         /* We did, so we need to copy back the port */
         Status = TCPGetSockAddress(Connection, 
(PTRANSPORT_ADDRESS)&LocalAddress, FALSE);
         if (!NT_SUCCESS(Status))
@@ -379,10 +381,14 @@ NTSTATUS TCPConnect
         }
 
         /* Allocate the port in the port bitmap */
-        Connection->AddressFile->Port = 
TCPAllocatePort(LocalAddress.Address[0].Address[0].sin_port);
-
-        /* This should never fail */
-        ASSERT(Connection->AddressFile->Port != 0xFFFF);
+        AllocatedPort = 
TCPAllocatePort(LocalAddress.Address[0].Address[0].sin_port);
+        /* This should never fail unless all ports are in use */
+        if (AllocatedPort == (UINT) -1)
+        {
+            DbgPrint("ERR: No more ports available.\n");
+            return STATUS_TOO_MANY_ADDRESSES;
+        }
+        Connection->AddressFile->Port = AllocatedPort;
     }
 
     connaddr.addr = RemoteAddress.Address.IPv4Address;
diff --git a/drivers/network/tcpip/tcpip/fileobjs.c 
b/drivers/network/tcpip/tcpip/fileobjs.c
index d926abed97e..d3dee752efa 100644
--- a/drivers/network/tcpip/tcpip/fileobjs.c
+++ b/drivers/network/tcpip/tcpip/fileobjs.c
@@ -404,6 +404,7 @@ NTSTATUS FileOpenAddress(
   PVOID Options)
 {
   PADDRESS_FILE AddrFile;
+  UINT AllocatedPort;
 
   TI_DbgPrint(MID_TRACE, ("Called (Proto %d).\n", Protocol));
 
@@ -472,14 +473,15 @@ NTSTATUS FileOpenAddress(
       if (Address->Address[0].Address[0].sin_port)
       {
           /* The client specified an explicit port so we force a bind to this 
*/
-          AddrFile->Port = 
TCPAllocatePort(Address->Address[0].Address[0].sin_port);
+          AllocatedPort = 
TCPAllocatePort(Address->Address[0].Address[0].sin_port);
 
           /* Check for bind success */
-          if (AddrFile->Port == 0xffff)
+          if (AllocatedPort == (UINT)-1)
           {
               ExFreePoolWithTag(AddrFile, ADDR_FILE_TAG);
               return STATUS_ADDRESS_ALREADY_EXISTS;
           }
+          AddrFile->Port = AllocatedPort;
 
           /* Sanity check */
           ASSERT(Address->Address[0].Address[0].sin_port == AddrFile->Port);
@@ -487,14 +489,15 @@ NTSTATUS FileOpenAddress(
       else if (!AddrIsUnspecified(&AddrFile->Address))
       {
           /* The client is trying to bind to a local address so allocate a 
port now too */
-          AddrFile->Port = TCPAllocatePort(0);
+          AllocatedPort = TCPAllocatePort(0);
 
           /* Check for bind success */
-          if (AddrFile->Port == 0xffff)
+          if (AllocatedPort == (UINT)-1)
           {
               ExFreePoolWithTag(AddrFile, ADDR_FILE_TAG);
               return STATUS_ADDRESS_ALREADY_EXISTS;
           }
+          AddrFile->Port = AllocatedPort;
       }
       else
       {
@@ -509,16 +512,16 @@ NTSTATUS FileOpenAddress(
 
   case IPPROTO_UDP:
       TI_DbgPrint(MID_TRACE,("Allocating udp port\n"));
-      AddrFile->Port =
-         UDPAllocatePort(Address->Address[0].Address[0].sin_port);
+      AllocatedPort = UDPAllocatePort(Address->Address[0].Address[0].sin_port);
 
       if ((Address->Address[0].Address[0].sin_port &&
-           AddrFile->Port != Address->Address[0].Address[0].sin_port) ||
-           AddrFile->Port == 0xffff)
+           AllocatedPort != Address->Address[0].Address[0].sin_port) ||
+           AllocatedPort == (UINT)-1)
       {
           ExFreePoolWithTag(AddrFile, ADDR_FILE_TAG);
           return STATUS_ADDRESS_ALREADY_EXISTS;
       }
+      AddrFile->Port = AllocatedPort;
 
       TI_DbgPrint(MID_TRACE,("Setting port %d (wanted %d)\n",
                              AddrFile->Port,

Reply via email to