rx: initialize memory before use
[openafs.git] / src / rx / rx_user.c
index e2238c1..598be2b 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Copyright 2000, International Business Machines Corporation and others.
  * All Rights Reserved.
- * 
+ *
  * This software has been released under the terms of the IBM Public
  * License.  For details, see the LICENSE file in the top-level source
  * directory or online at http://www.openafs.org/dl/license10.html
@@ -9,30 +9,21 @@
 
 /* rx_user.c contains routines specific to the user space UNIX implementation of rx */
 
+/* rxi_syscall is currently not prototyped */
+
 #include <afsconfig.h>
 #include <afs/param.h>
 
-RCSID
-    ("$Header$");
+#include <roken.h>
+
+#include <afs/opr.h>
 
-# include <sys/types.h>
-# include <errno.h>
-# include <signal.h>
-# include <string.h>
 #ifdef AFS_NT40_ENV
 # include <WINNT/syscfg.h>
 #else
-# include <sys/socket.h>
-# include <sys/file.h>
-# include <netdb.h>
-# include <sys/stat.h>
-# include <netinet/in.h>
-# include <sys/time.h>
 # include <net/if.h>
-# include <sys/ioctl.h>
 #endif
-# include <fcntl.h>
-#if !defined(AFS_AIX_ENV) && !defined(AFS_NT40_ENV) 
+#if !defined(AFS_AIX_ENV) && !defined(AFS_NT40_ENV)
 # include <sys/syscall.h>
 #endif
 #include <afs/afs_args.h>
@@ -45,23 +36,32 @@ RCSID
 #define IPPORT_USERRESERVED 5000
 # endif
 
-#ifndef AFS_NT40_ENV
-# include <sys/time.h>
+#if defined(AFS_LINUX22_ENV) && defined(AFS_RXERRQ_ENV)
+# include <linux/types.h>
+# include <linux/errqueue.h>
+# if defined(AFS_ADAPT_PMTU) && !defined(IP_MTU)
+#  define IP_MTU 14
+# endif
 #endif
-# include "rx.h"
-# include "rx_globals.h"
+
+#include "rx.h"
+#include "rx_atomic.h"
+#include "rx_globals.h"
+#include "rx_stats.h"
+#include "rx_peer.h"
+#include "rx_packet.h"
+#include "rx_internal.h"
 
 #ifdef AFS_PTHREAD_ENV
-#include <assert.h>
 
 /*
  * The rx_if_init_mutex mutex protects the following global variables:
  * Inited
  */
 
-pthread_mutex_t rx_if_init_mutex;
-#define LOCK_IF_INIT assert(pthread_mutex_lock(&rx_if_init_mutex)==0)
-#define UNLOCK_IF_INIT assert(pthread_mutex_unlock(&rx_if_init_mutex)==0)
+afs_kmutex_t rx_if_init_mutex;
+#define LOCK_IF_INIT MUTEX_ENTER(&rx_if_init_mutex)
+#define UNLOCK_IF_INIT MUTEX_EXIT(&rx_if_init_mutex)
 
 /*
  * The rx_if_mutex mutex protects the following global variables:
@@ -70,9 +70,9 @@ pthread_mutex_t rx_if_init_mutex;
  * myNetMasks
  */
 
-pthread_mutex_t rx_if_mutex;
-#define LOCK_IF assert(pthread_mutex_lock(&rx_if_mutex)==0)
-#define UNLOCK_IF assert(pthread_mutex_unlock(&rx_if_mutex)==0)
+afs_kmutex_t rx_if_mutex;
+#define LOCK_IF MUTEX_ENTER(&rx_if_mutex)
+#define UNLOCK_IF MUTEX_EXIT(&rx_if_mutex)
 #else
 #define LOCK_IF_INIT
 #define UNLOCK_IF_INIT
@@ -85,7 +85,7 @@ pthread_mutex_t rx_if_mutex;
  * Make a socket for receiving/sending IP packets.  Set it into non-blocking
  * and large buffering modes.  If port isn't specified, the kernel will pick
  * one.  Returns the socket (>= 0) on success.  Returns OSI_NULLSOCKET on
- * failure. Port must be in network byte order.        
+ * failure. Port must be in network byte order.
  */
 osi_socket
 rxi_GetHostUDPSocket(u_int ahost, u_short port)
@@ -95,10 +95,14 @@ rxi_GetHostUDPSocket(u_int ahost, u_short port)
     struct sockaddr_in taddr;
     char *name = "rxi_GetUDPSocket: ";
 #ifdef AFS_LINUX22_ENV
-    int pmtu=IP_PMTUDISC_DONT;
+# if defined(AFS_ADAPT_PMTU)
+    int pmtu = IP_PMTUDISC_WANT;
+# else
+    int pmtu = IP_PMTUDISC_DONT;
+# endif
 #endif
 
-#if !defined(AFS_NT40_ENV) 
+#if !defined(AFS_NT40_ENV)
     if (ntohs(port) >= IPPORT_RESERVED && ntohs(port) < IPPORT_USERRESERVED) {
 /*     (osi_Msg "%s*WARNING* port number %d is not a reserved port number.  Use port numbers above %d\n", name, port, IPPORT_USERRESERVED);
 */ ;
@@ -110,16 +114,25 @@ rxi_GetHostUDPSocket(u_int ahost, u_short port)
        goto error;
     }
 #endif
-    socketFd = socket(AF_INET, SOCK_DGRAM, 0);
+    socketFd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
 
-    if (socketFd < 0) {
+    if (socketFd == OSI_NULLSOCKET) {
+#ifdef AFS_NT40_ENV
+        fprintf(stderr, "socket() failed with error %u\n", WSAGetLastError());
+#else
        perror("socket");
+#endif
        goto error;
     }
 
+#ifdef AFS_NT40_ENV
+    rxi_xmit_init(socketFd);
+#endif /* AFS_NT40_ENV */
+
     taddr.sin_addr.s_addr = ahost;
     taddr.sin_family = AF_INET;
     taddr.sin_port = (u_short) port;
+    memset(&taddr.sin_zero, 0, sizeof(taddr.sin_zero));
 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
     taddr.sin_len = sizeof(struct sockaddr_in);
 #endif
@@ -128,17 +141,15 @@ rxi_GetHostUDPSocket(u_int ahost, u_short port)
        if (binds)
            rxi_Delay(10);
        code = bind(socketFd, (struct sockaddr *)&taddr, sizeof(taddr));
-       if (!code)
-           break;
+        break;
     }
     if (code) {
-       perror("bind");
        (osi_Msg "%sbind failed\n", name);
        goto error;
     }
-#if !defined(AFS_NT40_ENV) 
+#if !defined(AFS_NT40_ENV)
     /*
-     * Set close-on-exec on rx socket 
+     * Set close-on-exec on rx socket
      */
     fcntl(socketFd, F_SETFD, 1);
 #endif
@@ -152,15 +163,26 @@ rxi_GetHostUDPSocket(u_int ahost, u_short port)
 
        len1 = 32766;
        len2 = rx_UdpBufSize;
-       greedy =
-           (setsockopt
-            (socketFd, SOL_SOCKET, SO_RCVBUF, (char *)&len2,
-             sizeof(len2)) >= 0);
-       if (!greedy) {
-           len2 = 32766;       /* fall back to old size... uh-oh! */
-       }
 
-       greedy =
+        /* find the size closest to rx_UdpBufSize that will be accepted */
+        while (!greedy && len2 > len1) {
+            greedy =
+                (setsockopt
+                  (socketFd, SOL_SOCKET, SO_RCVBUF, (char *)&len2,
+                   sizeof(len2)) >= 0);
+            if (!greedy)
+                len2 /= 2;
+        }
+
+        /* but do not let it get smaller than 32K */
+        if (len2 < len1)
+            len2 = len1;
+
+        if (len1 < len2)
+            len1 = len2;
+
+
+        greedy =
            (setsockopt
             (socketFd, SOL_SOCKET, SO_SNDBUF, (char *)&len1,
              sizeof(len1)) >= 0)
@@ -171,15 +193,19 @@ rxi_GetHostUDPSocket(u_int ahost, u_short port)
        if (!greedy)
            (osi_Msg "%s*WARNING* Unable to increase buffering on socket\n",
             name);
-       MUTEX_ENTER(&rx_stats_mutex);
-       rx_stats.socketGreedy = greedy;
-       MUTEX_EXIT(&rx_stats_mutex);
+        if (rx_stats_active)
+            rx_atomic_set(&rx_stats.socketGreedy, greedy);
     }
 
 #ifdef AFS_LINUX22_ENV
     setsockopt(socketFd, SOL_IP, IP_MTU_DISCOVER, &pmtu, sizeof(pmtu));
 #endif
-
+#ifdef AFS_RXERRQ_ENV
+    {
+       int recverr = 1;
+       setsockopt(socketFd, SOL_IP, IP_RECVERR, &recverr, sizeof(recverr));
+    }
+#endif
     if (rxi_Listen(socketFd) < 0) {
        goto error;
     }
@@ -188,10 +214,10 @@ rxi_GetHostUDPSocket(u_int ahost, u_short port)
 
   error:
 #ifdef AFS_NT40_ENV
-    if (socketFd >= 0)
+    if (socketFd != OSI_NULLSOCKET)
        closesocket(socketFd);
 #else
-    if (socketFd >= 0)
+    if (socketFd != OSI_NULLSOCKET)
        close(socketFd);
 #endif
 
@@ -205,14 +231,16 @@ rxi_GetUDPSocket(u_short port)
 }
 
 void
-osi_Panic(msg, a1, a2, a3) 
-     char *msg; 
+osi_Panic(char *msg, ...)
 {
+    va_list ap;
+    va_start(ap, msg);
     (osi_Msg "Fatal Rx error: ");
-    (osi_Msg msg, a1, a2, a3);
+    (osi_VMsg msg, ap);
+    va_end(ap);
     fflush(stderr);
     fflush(stdout);
-    afs_abort();
+    opr_abort();
 }
 
 /*
@@ -232,9 +260,9 @@ static const char memZero;
 void *
 osi_Alloc(afs_int32 x)
 {
-    /* 
+    /*
      * 0-length allocs may return NULL ptr from malloc, so we special-case
-     * things so that NULL returned iff an error occurred 
+     * things so that NULL returned iff an error occurred
      */
     if (x == 0)
        return (void *)&memZero;
@@ -261,7 +289,7 @@ static int myNetFlags[ADDRSPERSITE];
 static u_int rxi_numNetAddrs;
 static int Inited = 0;
 
-#if defined(AFS_NT40_ENV) 
+#if defined(AFS_NT40_ENV)
 int
 rxi_getaddr(void)
 {
@@ -271,53 +299,89 @@ rxi_getaddr(void)
     /* we don't want to use the loopback adapter which is first */
     /* this is a bad bad hack */
     if (rxi_numNetAddrs > 1)
-       return htonl(rxi_NetAddrs[1]);  
+       return htonl(rxi_NetAddrs[1]);
     else if (rxi_numNetAddrs > 0)
        return htonl(rxi_NetAddrs[0]);
     else
        return 0;
 }
 
-/* 
-** return number of addresses 
+/*
+** return number of addresses
 ** and the addresses themselves in the buffer
 ** maxSize - max number of interfaces to return.
 */
 int
-rx_getAllAddr(afs_int32 * buffer, int maxSize)
+rx_getAllAddr(afs_uint32 * buffer, int maxSize)
 {
     int count = 0, offset = 0;
 
     /* The IP address list can change so we must query for it */
     rx_GetIFInfo();
 
-#ifndef AFS_NT40_ENV
-    /* we don't want to use the loopback adapter which is first */
-    /* this is a bad bad hack.
-     * and doesn't hold true on Windows.
-     */
-    if ( rxi_numNetAddrs > 1 )
-        offset = 1;
-#endif /* AFS_NT40_ENV */
-
     for (count = 0; offset < rxi_numNetAddrs && maxSize > 0;
         count++, offset++, maxSize--)
        buffer[count] = htonl(rxi_NetAddrs[offset]);
 
     return count;
 }
+
+/* this function returns the total number of interface addresses
+ * the buffer has to be passed in by the caller. It also returns
+ * the matching interface mask and mtu.  All values are returned
+ * in network byte order.
+ */
+int
+rx_getAllAddrMaskMtu(afs_uint32 addrBuffer[], afs_uint32 maskBuffer[],
+                     afs_uint32 mtuBuffer[], int maxSize)
+{
+    int count = 0, offset = 0;
+
+    /* The IP address list can change so we must query for it */
+    rx_GetIFInfo();
+
+    for (count = 0;
+         offset < rxi_numNetAddrs && maxSize > 0;
+         count++, offset++, maxSize--) {
+       addrBuffer[count] = htonl(rxi_NetAddrs[offset]);
+       maskBuffer[count] = htonl(myNetMasks[offset]);
+       mtuBuffer[count]  = htonl(myNetMTUs[offset]);
+    }
+    return count;
+}
 #endif
 
 #ifdef AFS_NT40_ENV
+extern rx_atomic_t rxinit_status;
+void
+rxi_InitMorePackets(void) {
+    int npackets, ncbufs;
+
+    ncbufs = (rx_maxJumboRecvSize - RX_FIRSTBUFFERSIZE);
+    if (ncbufs > 0) {
+        ncbufs = ncbufs / RX_CBUFFERSIZE;
+        npackets = rx_initSendWindow - 1;
+        rxi_MorePackets(npackets * (ncbufs + 1));
+    }
+}
 void
 rx_GetIFInfo(void)
 {
     u_int maxsize;
     u_int rxsize;
-    int npackets, ncbufs;
     afs_uint32 i;
 
     LOCK_IF_INIT;
+    if (Inited) {
+       if (Inited < 2 && !rx_atomic_test_bit(&rxinit_status, 0)) {
+            /* We couldn't initialize more packets earlier.
+             * Do it now. */
+            rxi_InitMorePackets();
+            Inited = 2;
+        }
+        UNLOCK_IF_INIT;
+       return;
+    }
     Inited = 1;
     UNLOCK_IF_INIT;
 
@@ -331,20 +395,26 @@ rx_GetIFInfo(void)
         maxsize =
             rxi_nRecvFrags * rxsize + (rxi_nRecvFrags - 1) * UDP_HDR_SIZE;
         maxsize = rxi_AdjustMaxMTU(rxsize, maxsize);
-        if (rx_maxReceiveSize < maxsize) {
+        if (rx_maxReceiveSize > maxsize) {
             rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxsize);
             rx_maxReceiveSize =
                 MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
         }
-
+        if (rx_MyMaxSendSize > maxsize) {
+            rx_MyMaxSendSize = MIN(RX_MAX_PACKET_SIZE, maxsize);
+        }
     }
     UNLOCK_IF;
-    ncbufs = (rx_maxJumboRecvSize - RX_FIRSTBUFFERSIZE);
-    if (ncbufs > 0) {
-        ncbufs = ncbufs / RX_CBUFFERSIZE;
-        npackets = rx_initSendWindow - 1;
-        rxi_MorePackets(npackets * (ncbufs + 1));
-    }
+
+    /*
+     * If rxinit_status is still set, rx_InitHost() has yet to be called
+     * and we therefore do not have any mutex locks initialized.  As a
+     * result we cannot call rxi_MorePackets() without crashing.
+     */
+    if (rx_atomic_test_bit(&rxinit_status, 0))
+        return;
+
+    rxi_InitMorePackets();
 }
 #endif
 
@@ -367,19 +437,17 @@ fudge_netmask(afs_uint32 addr)
 
 
 
-#if !defined(AFS_AIX_ENV) && !defined(AFS_NT40_ENV) && !defined(AFS_LINUX20_ENV) 
+#if !defined(AFS_AIX_ENV) && !defined(AFS_NT40_ENV) && !defined(AFS_LINUX20_ENV)
 int
-rxi_syscall(a3, a4, a5)
-     afs_uint32 a3, a4;
-     void *a5;
+rxi_syscall(afs_uint32 a3, afs_uint32 a4, void *a5)
 {
     afs_uint32 rcode;
-    void (*old) ();
+    void (*old) (int);
 
-    old = (void (*)())signal(SIGSYS, SIG_IGN);
+    old = signal(SIGSYS, SIG_IGN);
 
 #if defined(AFS_SGI_ENV)
-    rcode = afs_syscall(a3, a4, a5);
+    rcode = afs_syscall(AFS_SYSCALL, 28, a3, a4, a5);
 #else
     rcode = syscall(AFS_SYSCALL, 28 /* AFSCALL_CALL */ , a3, a4, a5);
 #endif /* AFS_SGI_ENV */
@@ -418,10 +486,9 @@ rx_GetIFInfo(void)
     memset(myNetMTUs, 0, sizeof(myNetMTUs));
     memset(myNetMasks, 0, sizeof(myNetMasks));
     UNLOCK_IF;
-    s = socket(AF_INET, SOCK_DGRAM, 0);
-    if (s < 0)
+    s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
+    if (s == OSI_NULLSOCKET)
        return;
-
 #ifdef AFS_AIX41_ENV
     ifc.ifc_len = sizeof(buf);
     ifc.ifc_buf = buf;
@@ -466,7 +533,7 @@ rx_GetIFInfo(void)
        if (a->sin_family != AF_INET)
            continue;
        rxi_NetAddrs[rxi_numNetAddrs] = ntohl(a->sin_addr.s_addr);
-       if (rxi_NetAddrs[rxi_numNetAddrs] == 0x7f000001) {
+       if (rx_IsLoopbackAddr(rxi_NetAddrs[rxi_numNetAddrs])) {
            /* we don't really care about "localhost" */
            continue;
        }
@@ -489,7 +556,7 @@ rx_GetIFInfo(void)
            if (ifr->ifr_flags & IFF_LOOPBACK)
                continue;
 #endif
-           /* fprintf(stderr, "if %s flags=%x\n", 
+           /* fprintf(stderr, "if %s flags=%x\n",
             * ifr->ifr_name, ifr->ifr_flags); */
        } else {                /*
                                 * fputs(stderr, "ioctl error IFFLAGS\n");
@@ -502,8 +569,8 @@ rx_GetIFInfo(void)
        rxi_syscallp = rxi_syscall;
 #endif
 
-       /* If I refer to kernel extensions that aren't loaded on AIX, the 
-        * program refuses to load and run, so I simply can't include the 
+       /* If I refer to kernel extensions that aren't loaded on AIX, the
+        * program refuses to load and run, so I simply can't include the
         * following code.  Fortunately, AIX is the one operating system in
         * which the subsequent ioctl works reliably. */
        if (rxi_syscallp) {
@@ -523,7 +590,7 @@ rx_GetIFInfo(void)
            } else
                myNetMasks[rxi_numNetAddrs] =
                    ntohl(myNetMasks[rxi_numNetAddrs]);
-           /* fprintf(stderr, "if %s mask=0x%x\n", 
+           /* fprintf(stderr, "if %s mask=0x%x\n",
             * ifr->ifr_name, myNetMasks[rxi_numNetAddrs]); */
        }
 
@@ -533,7 +600,7 @@ rx_GetIFInfo(void)
            res = ioctl(s, SIOCGIFMTU, ifr);
            if ((res == 0) && (ifr->ifr_metric > 128)) {        /* sanity check */
                myNetMTUs[rxi_numNetAddrs] = ifr->ifr_metric;
-               /* fprintf(stderr, "if %s mtu=%d\n", 
+               /* fprintf(stderr, "if %s mtu=%d\n",
                 * ifr->ifr_name, ifr->ifr_metric); */
            } else {
                /* fputs(stderr, "ioctl error IFMTU\n");
@@ -547,10 +614,10 @@ rx_GetIFInfo(void)
                fudge_netmask(rxi_NetAddrs[rxi_numNetAddrs]);
 #ifdef SIOCGIFNETMASK
            res = ioctl(s, SIOCGIFNETMASK, ifr);
-           if ((res == 0)) {
+           if (res == 0) {
                a = (struct sockaddr_in *)&ifr->ifr_addr;
                myNetMasks[rxi_numNetAddrs] = ntohl(a->sin_addr.s_addr);
-               /* fprintf(stderr, "if %s subnetmask=0x%x\n", 
+               /* fprintf(stderr, "if %s subnetmask=0x%x\n",
                 * ifr->ifr_name, myNetMasks[rxi_numNetAddrs]); */
            } else {
                /* fputs(stderr, "ioctl error IFMASK\n");
@@ -559,7 +626,7 @@ rx_GetIFInfo(void)
 #endif
        }
 
-       if (rxi_NetAddrs[rxi_numNetAddrs] != 0x7f000001) {      /* ignore lo0 */
+       if (!rx_IsLoopbackAddr(rxi_NetAddrs[rxi_numNetAddrs])) {        /* ignore lo0 */
            int maxsize;
            maxsize =
                rxi_nRecvFrags * (myNetMTUs[rxi_numNetAddrs] - RX_IP_SIZE);
@@ -605,6 +672,10 @@ rxi_InitPeerParams(struct rx_peer *pp)
     afs_uint32 ppaddr;
     u_short rxmtu;
     int ix;
+#ifdef AFS_ADAPT_PMTU
+    int sock;
+    struct sockaddr_in addr;
+#endif
 
     LOCK_IF_INIT;
     if (!Inited) {
@@ -618,25 +689,24 @@ rxi_InitPeerParams(struct rx_peer *pp)
        UNLOCK_IF_INIT;
     }
 
-#ifdef ADAPT_MTU
     /* try to second-guess IP, and identify which link is most likely to
      * be used for traffic to/from this host. */
     ppaddr = ntohl(pp->host);
-    
+
     pp->ifMTU = 0;
-    pp->timeout.sec = 2;
-    pp->rateFlag = 2;         /* start timing after two full packets */
-    /* I don't initialize these, because I presume they are bzero'd... 
+    rx_rto_setPeerTimeoutSecs(pp, 2);
+    /* I don't initialize these, because I presume they are bzero'd...
      * pp->burstSize pp->burst pp->burstWait.sec pp->burstWait.usec
-     * pp->timeout.usec */
-    
+     */
+
     LOCK_IF;
     for (ix = 0; ix < rxi_numNetAddrs; ++ix) {
        if ((rxi_NetAddrs[ix] & myNetMasks[ix]) == (ppaddr & myNetMasks[ix])) {
 #ifdef IFF_POINTOPOINT
            if (myNetFlags[ix] & IFF_POINTOPOINT)
-               pp->timeout.sec = 4;
+               rx_rto_setPeerTimeoutSecs(pp, 4);
 #endif /* IFF_POINTOPOINT */
+
            rxmtu = myNetMTUs[ix] - RX_IPUDP_SIZE;
            if (rxmtu < RX_MIN_PACKET_SIZE)
                rxmtu = RX_MIN_PACKET_SIZE;
@@ -645,24 +715,40 @@ rxi_InitPeerParams(struct rx_peer *pp)
        }
     }
     UNLOCK_IF;
-    if (!pp->ifMTU) {         /* not local */
-       pp->timeout.sec = 3;
+    if (!pp->ifMTU) {          /* not local */
+       rx_rto_setPeerTimeoutSecs(pp, 3);
        pp->ifMTU = MIN(rx_MyMaxSendSize, RX_REMOTE_PACKET_SIZE);
     }
-#else /* ADAPT_MTU */
-    pp->rateFlag = 2;          /* start timing after two full packets */
-    pp->timeout.sec = 2;
-    pp->ifMTU = MIN(rx_MyMaxSendSize, OLD_MAX_PACKET_SIZE);
-#endif /* ADAPT_MTU */
+#ifdef AFS_ADAPT_PMTU
+    sock=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
+    if (sock != OSI_NULLSOCKET) {
+        addr.sin_family = AF_INET;
+        addr.sin_addr.s_addr = pp->host;
+        addr.sin_port = pp->port;
+        memset(&addr.sin_zero, 0, sizeof(addr.sin_zero));
+        if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0) {
+            int mtu=0;
+            socklen_t s = sizeof(mtu);
+            if (getsockopt(sock, SOL_IP, IP_MTU, &mtu, &s)== 0) {
+                pp->ifMTU = MIN(mtu - RX_IPUDP_SIZE, pp->ifMTU);
+            }
+        }
+# ifdef AFS_NT40_ENV
+        closesocket(sock);
+# else
+        close(sock);
+# endif
+    }
+#endif
     pp->ifMTU = rxi_AdjustIfMTU(pp->ifMTU);
     pp->maxMTU = OLD_MAX_PACKET_SIZE;  /* for compatibility with old guys */
     pp->natMTU = MIN((int)pp->ifMTU, OLD_MAX_PACKET_SIZE);
     pp->maxDgramPackets =
        MIN(rxi_nDgramPackets,
-           rxi_AdjustDgramPackets(RX_MAX_FRAGS, pp->ifMTU));
+           rxi_AdjustDgramPackets(rxi_nSendFrags, pp->ifMTU));
     pp->ifDgramPackets =
        MIN(rxi_nDgramPackets,
-           rxi_AdjustDgramPackets(RX_MAX_FRAGS, pp->ifMTU));
+           rxi_AdjustDgramPackets(rxi_nSendFrags, pp->ifMTU));
     pp->maxDgramPackets = 1;
     /* Initialize slow start parameters */
     pp->MTU = MIN(pp->natMTU, pp->maxMTU);
@@ -679,10 +765,49 @@ rx_SetNoJumbo(void)
     rxi_nSendFrags = rxi_nRecvFrags = 1;
 }
 
-/* Override max MTU.  If rx_SetNoJumbo is called, it must be 
+/* Override max MTU.  If rx_SetNoJumbo is called, it must be
    called before calling rx_SetMaxMTU since SetNoJumbo clobbers rx_maxReceiveSize */
-void
+int
 rx_SetMaxMTU(int mtu)
 {
+    if (mtu < RX_MIN_PACKET_SIZE || mtu > RX_MAX_PACKET_DATA_SIZE)
+       return EINVAL;
+
     rx_MyMaxSendSize = rx_maxReceiveSizeUser = rx_maxReceiveSize = mtu;
+
+    return 0;
 }
+
+#ifdef AFS_RXERRQ_ENV
+int
+rxi_HandleSocketError(int socket)
+{
+    struct msghdr msg;
+    struct cmsghdr *cmsg;
+    struct sock_extended_err *err;
+    struct sockaddr_in addr;
+    char controlmsgbuf[256];
+    int code;
+
+    msg.msg_name = &addr;
+    msg.msg_namelen = sizeof(addr);
+    msg.msg_iov = NULL;
+    msg.msg_iovlen = 0;
+    msg.msg_control = controlmsgbuf;
+    msg.msg_controllen = 256;
+    msg.msg_flags = 0;
+    code = recvmsg(socket, &msg, MSG_ERRQUEUE|MSG_DONTWAIT|MSG_TRUNC);
+
+    if (code < 0 || !(msg.msg_flags & MSG_ERRQUEUE))
+        return 0;
+
+    for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
+       if (cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_RECVERR) {
+           err = (struct sock_extended_err *)CMSG_DATA(cmsg);
+           rxi_ProcessNetError(err, addr.sin_addr.s_addr, addr.sin_port);
+       }
+    }
+
+    return 1;
+}
+#endif