rx: Avoid new server calls for big-seq DATA pkts
[openafs.git] / src / rx / rx.c
index 9dc2e2b..2296014 100644 (file)
@@ -135,7 +135,7 @@ static struct rx_packet
                               struct rx_call **newcallp);
 static struct rx_packet
        *rxi_ReceiveAckPacket(struct rx_call *call, struct rx_packet *np,
-                             int istack);
+                             int istack, int *a_invalid);
 static struct rx_packet
        *rxi_ReceiveResponsePacket(struct rx_connection *conn,
                                   struct rx_packet *np, int istack);
@@ -152,7 +152,7 @@ static void rxi_ScheduleNatKeepAliveEvent(struct rx_connection *conn);
 static void rxi_ScheduleGrowMTUEvent(struct rx_call *call, int secs);
 static void rxi_KeepAliveOn(struct rx_call *call);
 static void rxi_GrowMTUOn(struct rx_call *call);
-static void rxi_ChallengeOn(struct rx_connection *conn);
+static int rxi_ChallengeOn(struct rx_connection *conn);
 static int rxi_CheckCall(struct rx_call *call, int haveCTLock);
 static void rxi_AckAllInTransmitQueue(struct rx_call *call);
 static void rxi_CancelKeepAliveEvent(struct rx_call *call);
@@ -160,6 +160,12 @@ static void rxi_CancelDelayedAbortEvent(struct rx_call *call);
 static void rxi_CancelGrowMTUEvent(struct rx_call *call);
 static void update_nextCid(void);
 
+#ifndef KERNEL
+static void rxi_Finalize_locked(void);
+#elif defined(UKERNEL)
+# define rxi_Finalize_locked() do { } while (0)
+#endif
+
 #ifdef RX_ENABLE_LOCKS
 struct rx_tq_debug {
     rx_atomic_t rxi_start_aborted; /* rxi_start awoke after rxi_Send in error.*/
@@ -215,12 +221,16 @@ struct opr_queue rx_incomingCallQueue;
  * calls to process */
 struct opr_queue rx_idleServerQueue;
 
+/* List of free rx_serverQueueEntry structs */
+struct opr_queue rx_freeServerQueue;
+
 #if !defined(offsetof)
 #include <stddef.h>            /* for definition of offsetof() */
 #endif
 
 #ifdef RX_ENABLE_LOCKS
 afs_kmutex_t rx_atomic_mutex;
+static afs_kmutex_t freeSQEList_lock;
 #endif
 
 /* Forward prototypes */
@@ -442,24 +452,40 @@ static int rxdb_fileID = RXDB_FILE_RX;
 #endif /* RX_ENABLE_LOCKS */
 struct rx_serverQueueEntry *rx_waitForPacket = 0;
 
+/*
+ * This mutex serializes calls to our initialization and shutdown routines
+ * (rx_InitHost, rx_Finalize and shutdown_rx). Only one thread can be running
+ * these at any time; all other threads must wait for it to finish running, and
+ * then examine the value of rxi_running afterwards.
+ */
+#ifdef AFS_PTHREAD_ENV
+# define LOCK_RX_INIT MUTEX_ENTER(&rx_init_mutex)
+# define UNLOCK_RX_INIT MUTEX_EXIT(&rx_init_mutex)
+#else
+# define LOCK_RX_INIT
+# define UNLOCK_RX_INIT
+#endif
+
 /* ------------Exported Interfaces------------- */
 
+static rx_atomic_t rxi_running = RX_ATOMIC_INIT(0);
+int
+rxi_IsRunning(void)
+{
+    return rx_atomic_read(&rxi_running);
+}
+
 /* Initialize rx.  A port number may be mentioned, in which case this
  * becomes the default port number for any service installed later.
  * If 0 is provided for the port number, a random port will be chosen
  * by the kernel.  Whether this will ever overlap anything in
  * /etc/services is anybody's guess...  Returns 0 on success, -1 on
  * error. */
-#if !(defined(AFS_NT40_ENV) || defined(RXK_UPCALL_ENV))
-static
-#endif
-rx_atomic_t rxinit_status = RX_ATOMIC_INIT(1);
-
 int
 rx_InitHost(u_int host, u_int port)
 {
 #ifdef KERNEL
-    osi_timeval_t tv;
+    osi_timeval32_t tv;
 #else /* KERNEL */
     struct timeval tv;
 #endif /* KERNEL */
@@ -468,15 +494,17 @@ rx_InitHost(u_int host, u_int port)
     SPLVAR;
 
     INIT_PTHREAD_LOCKS;
-    if (!rx_atomic_test_and_clear_bit(&rxinit_status, 0))
+    LOCK_RX_INIT;
+    if (rxi_IsRunning()) {
+       UNLOCK_RX_INIT;
        return 0; /* already started */
-
+    }
 #ifdef RXDEBUG
     rxi_DebugInit();
 #endif
 #ifdef AFS_NT40_ENV
     if (afs_winsockInit() < 0)
-       return -1;
+       goto error;
 #endif
 
 #ifndef KERNEL
@@ -492,7 +520,7 @@ rx_InitHost(u_int host, u_int port)
 
     rx_socket = rxi_GetHostUDPSocket(host, (u_short) port);
     if (rx_socket == OSI_NULLSOCKET) {
-       return RX_ADDRINUSE;
+        goto addrinuse;
     }
 #if defined(RX_ENABLE_LOCKS) && defined(KERNEL)
 #ifdef RX_LOCKS_DB
@@ -580,19 +608,19 @@ rx_InitHost(u_int host, u_int port)
        socklen_t addrlen = sizeof(addr);
 #endif
        if (getsockname((intptr_t)rx_socket, (struct sockaddr *)&addr, &addrlen)) {
-           rx_Finalize();
+           rxi_Finalize_locked();
            osi_Free(htable, rx_hashTableSize * sizeof(struct rx_connection *));
-           return -1;
+           goto error;
        }
        rx_port = addr.sin_port;
 #endif
     }
     rx_stats.minRtt.sec = 9999999;
     if (RAND_bytes(&rx_epoch, sizeof(rx_epoch)) != 1)
-       return -1;
+       goto error;
     rx_epoch  = (rx_epoch & ~0x40000000) | 0x80000000;
     if (RAND_bytes(&rx_nextCid, sizeof(rx_nextCid)) != 1)
-       return -1;
+       goto error;
     rx_nextCid &= RX_CIDMASK;
     MUTEX_ENTER(&rx_quota_mutex);
     rxi_dataQuota += rx_extraQuota; /* + extra pkts caller asked to rsrv */
@@ -610,6 +638,7 @@ rx_InitHost(u_int host, u_int port)
 
     /* Initialize various global queues */
     opr_queue_Init(&rx_idleServerQueue);
+    opr_queue_Init(&rx_freeServerQueue);
     opr_queue_Init(&rx_incomingCallQueue);
     opr_queue_Init(&rx_freeCallQueue);
 
@@ -623,8 +652,19 @@ rx_InitHost(u_int host, u_int port)
     rxi_StartListener();
 
     USERPRI;
-    rx_atomic_clear_bit(&rxinit_status, 0);
+
+    rx_atomic_set(&rxi_running, 1);
+    UNLOCK_RX_INIT;
+
     return 0;
+
+ addrinuse:
+    UNLOCK_RX_INIT;
+    return RX_ADDRINUSE;
+
+ error:
+    UNLOCK_RX_INIT;
+    return -1;
 }
 
 int
@@ -1010,6 +1050,7 @@ rx_NewConnection(afs_uint32 shost, u_short sport, u_short sservice,
 {
     int hashindex, i;
     struct rx_connection *conn;
+    int code;
 
     SPLVAR;
 
@@ -1053,7 +1094,7 @@ rx_NewConnection(afs_uint32 shost, u_short sport, u_short sservice,
        conn->lastBusy[i] = 0;
     }
 
-    RXS_NewConnection(securityObject, conn);
+    code = RXS_NewConnection(securityObject, conn);
     hashindex =
        CONN_HASH(shost, sport, conn->cid, conn->epoch, RX_CLIENT_CONNECTION);
 
@@ -1064,6 +1105,9 @@ rx_NewConnection(afs_uint32 shost, u_short sport, u_short sservice,
        rx_atomic_inc(&rx_stats.nClientConns);
     MUTEX_EXIT(&rx_connHashTable_lock);
     USERPRI;
+    if (code) {
+       rxi_ConnectionError(conn, code);
+    }
     return conn;
 }
 
@@ -1381,7 +1425,7 @@ static void
 rxi_WakeUpTransmitQueue(struct rx_call *call)
 {
     if (call->tqWaiters || (call->flags & RX_CALL_TQ_WAIT)) {
-       dpf(("call %"AFS_PTR_FMT" has %d waiters and flags %d\n",
+       dpf(("call %p has %d waiters and flags %d\n",
             call, call->tqWaiters, call->flags));
 #ifdef RX_ENABLE_LOCKS
        MUTEX_ASSERT(&call->lock);
@@ -1411,7 +1455,7 @@ rx_NewCall(struct rx_connection *conn)
     SPLVAR;
 
     clock_NewTime();
-    dpf(("rx_NewCall(conn %"AFS_PTR_FMT")\n", conn));
+    dpf(("rx_NewCall(conn %p)\n", conn));
 
     NETPRI;
     clock_GetTime(&queueTime);
@@ -1625,7 +1669,7 @@ rx_NewCall(struct rx_connection *conn)
     MUTEX_EXIT(&call->lock);
     USERPRI;
 
-    dpf(("rx_NewCall(call %"AFS_PTR_FMT")\n", call));
+    dpf(("rx_NewCall(call %p)\n", call));
     return call;
 }
 
@@ -1801,10 +1845,14 @@ rx_SetSecurityConfiguration(struct rx_service *service,
                            void *value)
 {
     int i;
+    int code;
     for (i = 0; i<service->nSecurityObjects; i++) {
        if (service->securityObjects[i]) {
-           RXS_SetConfiguration(service->securityObjects[i], NULL, type,
-                                value, NULL);
+           code = RXS_SetConfiguration(service->securityObjects[i], NULL, type,
+                                       value, NULL);
+           if (code) {
+               return code;
+           }
        }
     }
     return 0;
@@ -1901,7 +1949,7 @@ rxi_ServerProc(int threadID, struct rx_call *newcall, osi_socket * socketp)
 void
 rx_WakeupServerProcs(void)
 {
-    struct rx_serverQueueEntry *np, *tqp;
+    struct rx_serverQueueEntry *np;
     struct opr_queue *cursor;
     SPLVAR;
 
@@ -1916,8 +1964,8 @@ rx_WakeupServerProcs(void)
        osi_rxWakeup(rx_waitForPacket);
 #endif /* RX_ENABLE_LOCKS */
     MUTEX_ENTER(&freeSQEList_lock);
-    for (np = rx_FreeSQEList; np; np = tqp) {
-       tqp = *(struct rx_serverQueueEntry **)np;
+    for (opr_queue_Scan(&rx_freeServerQueue, cursor)) {
+        np = opr_queue_Entry(cursor, struct rx_serverQueueEntry, entry);
 #ifdef RX_ENABLE_LOCKS
        CV_BROADCAST(&np->cv);
 #else /* RX_ENABLE_LOCKS */
@@ -1977,8 +2025,10 @@ rx_GetCall(int tno, struct rx_service *cur_service, osi_socket * socketp)
 
     MUTEX_ENTER(&freeSQEList_lock);
 
-    if ((sq = rx_FreeSQEList)) {
-       rx_FreeSQEList = *(struct rx_serverQueueEntry **)sq;
+    if (!opr_queue_IsEmpty(&rx_freeServerQueue)) {
+       sq = opr_queue_First(&rx_freeServerQueue, struct rx_serverQueueEntry,
+                            entry);
+       opr_queue_Remove(&sq->entry);
        MUTEX_EXIT(&freeSQEList_lock);
     } else {                   /* otherwise allocate a new one and return that */
        MUTEX_EXIT(&freeSQEList_lock);
@@ -2012,7 +2062,7 @@ rx_GetCall(int tno, struct rx_service *cur_service, osi_socket * socketp)
                }
                MUTEX_ENTER(&rx_pthread_mutex);
                if (tno == rxi_fcfs_thread_num
-                       || opr_queue_IsEnd(&rx_incomingCallQueue, cursor)) {
+                       || opr_queue_IsLast(&rx_incomingCallQueue, cursor)) {
                    MUTEX_EXIT(&rx_pthread_mutex);
                    /* If we're the fcfs thread , then  we'll just use
                     * this call. If we haven't been able to find an optimal
@@ -2051,6 +2101,7 @@ rx_GetCall(int tno, struct rx_service *cur_service, osi_socket * socketp)
            opr_queue_Remove(&call->entry);
            MUTEX_EXIT(&rx_serverPool_lock);
            MUTEX_ENTER(&call->lock);
+           CLEAR_CALL_QUEUE_LOCK(call);
 
            if (call->flags & RX_CALL_WAIT_PROC) {
                call->flags &= ~RX_CALL_WAIT_PROC;
@@ -2069,7 +2120,6 @@ rx_GetCall(int tno, struct rx_service *cur_service, osi_socket * socketp)
                || opr_queue_First(&call->rq, struct rx_packet, entry)->header.seq != 1)
                rxi_SendAck(call, 0, 0, RX_ACK_DELAY, 0);
 
-           CLEAR_CALL_QUEUE_LOCK(call);
            break;
        } else {
            /* If there are no eligible incoming calls, add this process
@@ -2088,12 +2138,14 @@ rx_GetCall(int tno, struct rx_service *cur_service, osi_socket * socketp)
                CV_WAIT(&sq->cv, &rx_serverPool_lock);
 #ifdef KERNEL
                if (afs_termState == AFSOP_STOP_RXCALLBACK) {
-                   MUTEX_EXIT(&rx_serverPool_lock);
-                   return (struct rx_call *)0;
+                   break;
                }
 #endif
            } while (!(call = sq->newcall)
                     && !(socketp && *socketp != OSI_NULLSOCKET));
+           if (opr_queue_IsOnQueue(&sq->entry)) {
+               opr_queue_Remove(&sq->entry);
+           }
            MUTEX_EXIT(&rx_serverPool_lock);
            if (call) {
                MUTEX_ENTER(&call->lock);
@@ -2103,8 +2155,7 @@ rx_GetCall(int tno, struct rx_service *cur_service, osi_socket * socketp)
     }
 
     MUTEX_ENTER(&freeSQEList_lock);
-    *(struct rx_serverQueueEntry **)sq = rx_FreeSQEList;
-    rx_FreeSQEList = sq;
+    opr_queue_Prepend(&rx_freeServerQueue, &sq->entry);
     MUTEX_EXIT(&freeSQEList_lock);
 
     if (call) {
@@ -2125,7 +2176,7 @@ rx_GetCall(int tno, struct rx_service *cur_service, osi_socket * socketp)
 #endif
 
        rxi_calltrace(RX_CALL_START, call);
-       dpf(("rx_GetCall(port=%d, service=%d) ==> call %"AFS_PTR_FMT"\n",
+       dpf(("rx_GetCall(port=%d, service=%d) ==> call %p\n",
             call->conn->service->servicePort, call->conn->service->serviceId,
             call));
 
@@ -2149,8 +2200,10 @@ rx_GetCall(int tno, struct rx_service *cur_service, osi_socket * socketp)
     NETPRI;
     MUTEX_ENTER(&freeSQEList_lock);
 
-    if ((sq = rx_FreeSQEList)) {
-       rx_FreeSQEList = *(struct rx_serverQueueEntry **)sq;
+    if (!opr_queue_IsEmpty(&rx_freeServerQueue)) {
+       sq = opr_queue_First(&rx_freeServerQueue, struct rx_serverQueueEntry,
+                            entry);
+       opr_queue_Remove(&sq->entry);
        MUTEX_EXIT(&freeSQEList_lock);
     } else {                   /* otherwise allocate a new one and return that */
        MUTEX_EXIT(&freeSQEList_lock);
@@ -2184,9 +2237,8 @@ rx_GetCall(int tno, struct rx_service *cur_service, osi_socket * socketp)
            service = tcall->conn->service;
            if (QuotaOK(service)) {
                MUTEX_ENTER(&rx_pthread_mutex);
-               /* XXX - If tcall->entry.next is NULL, then we're no longer
-                * on a queue at all. This shouldn't happen. */
-               if (tno == rxi_fcfs_thread_num || !tcall->entry.next) {
+               if (tno == rxi_fcfs_thread_num
+                       || opr_queue_IsLast(&rx_incomingCallQueue, cursor)) {
                    MUTEX_EXIT(&rx_pthread_mutex);
                    /* If we're the fcfs thread, then  we'll just use
                     * this call. If we haven't been able to find an optimal
@@ -2220,6 +2272,7 @@ rx_GetCall(int tno, struct rx_service *cur_service, osi_socket * socketp)
 
     if (call) {
        opr_queue_Remove(&call->entry);
+       CLEAR_CALL_QUEUE_LOCK(call);
        /* we can't schedule a call if there's no data!!! */
        /* send an ack if there's no data, if we're missing the
         * first packet, or we're missing something between first
@@ -2264,8 +2317,7 @@ rx_GetCall(int tno, struct rx_service *cur_service, osi_socket * socketp)
     MUTEX_EXIT(&sq->lock);
 
     MUTEX_ENTER(&freeSQEList_lock);
-    *(struct rx_serverQueueEntry **)sq = rx_FreeSQEList;
-    rx_FreeSQEList = sq;
+    opr_queue_Prepend(&rx_freeServerQueue, &sq->entry);
     MUTEX_EXIT(&freeSQEList_lock);
 
     if (call) {
@@ -2334,7 +2386,7 @@ rx_EndCall(struct rx_call *call, afs_int32 rc)
     afs_int32 error;
     SPLVAR;
 
-    dpf(("rx_EndCall(call %"AFS_PTR_FMT" rc %d error %d abortCode %d)\n",
+    dpf(("rx_EndCall(call %p rc %d error %d abortCode %d)\n",
           call, rc, call->error, call->abortCode));
 
     NETPRI;
@@ -2345,7 +2397,7 @@ rx_EndCall(struct rx_call *call, afs_int32 rc)
        call->abortCount = 0;
     }
 
-    call->arrivalProc = (void (*)())0;
+    call->arrivalProc = NULL;
     if (rc && call->error == 0) {
        rxi_CallError(call, rc);
         call->app.mode = RX_MODE_ERROR;
@@ -2492,12 +2544,21 @@ rx_EndCall(struct rx_call *call, afs_int32 rc)
 void
 rx_Finalize(void)
 {
-    struct rx_connection **conn_ptr, **conn_end;
-
     INIT_PTHREAD_LOCKS;
-    if (rx_atomic_test_and_set_bit(&rxinit_status, 0))
+    LOCK_RX_INIT;
+    if (!rxi_IsRunning()) {
+       UNLOCK_RX_INIT;
        return;                 /* Already shutdown. */
+    }
+    rxi_Finalize_locked();
+    UNLOCK_RX_INIT;
+}
 
+static void
+rxi_Finalize_locked(void)
+{
+    struct rx_connection **conn_ptr, **conn_end;
+    rx_atomic_set(&rxi_running, 0);
     rxi_DeleteCachedConnections();
     if (rx_connHashTable) {
        MUTEX_ENTER(&rx_connHashTable_lock);
@@ -2534,7 +2595,6 @@ rx_Finalize(void)
 #ifdef AFS_NT40_ENV
     afs_winsockCleanup();
 #endif
-
 }
 #endif
 
@@ -2596,7 +2656,7 @@ rxi_NewCall(struct rx_connection *conn, int channel)
     struct opr_queue *cursor;
 #endif
 
-    dpf(("rxi_NewCall(conn %"AFS_PTR_FMT", channel %d)\n", conn, channel));
+    dpf(("rxi_NewCall(conn %p, channel %d)\n", conn, channel));
 
     /* Grab an existing call structure, or allocate a new one.
      * Existing call structures are assumed to have been left reset by
@@ -2794,7 +2854,7 @@ rxi_Alloc(size_t size)
     }
 
 p = (char *)
-#if defined(KERNEL) && !defined(UKERNEL) && defined(AFS_FBSD80_ENV)
+#if defined(KERNEL) && !defined(UKERNEL) && defined(AFS_FBSD_ENV)
   afs_osi_Alloc_NoSleep(size);
 #else
   osi_Alloc(size);
@@ -2808,6 +2868,9 @@ p = (char *)
 void
 rxi_Free(void *addr, size_t size)
 {
+    if (!addr) {
+        return;
+    }
     if (rx_stats_active) {
        rx_atomic_sub(&rxi_Allocsize, (int) size);
         rx_atomic_dec(&rxi_Alloccnt);
@@ -3067,6 +3130,7 @@ rxi_FindConnection(osi_socket socket, afs_uint32 host,
                    int *unknownService)
 {
     int hashindex, flag, i;
+    int code = 0;
     struct rx_connection *conn;
     *unknownService = 0;
     hashindex = CONN_HASH(host, port, cid, epoch, type);
@@ -3141,7 +3205,7 @@ rxi_FindConnection(osi_socket socket, afs_uint32 host,
            conn->rwind[i] = rx_initReceiveWindow;
        }
        /* Notify security object of the new connection */
-       RXS_NewConnection(conn->securityObject, conn);
+       code = RXS_NewConnection(conn->securityObject, conn);
        /* XXXX Connection timeout? */
        if (service->newConnProc)
            (*service->newConnProc) (conn);
@@ -3153,6 +3217,9 @@ rxi_FindConnection(osi_socket socket, afs_uint32 host,
 
     rxLastConn = conn;         /* store this connection as the last conn used */
     MUTEX_EXIT(&rx_connHashTable_lock);
+    if (code) {
+       rxi_ConnectionError(conn, code);
+    }
     return conn;
 }
 
@@ -3165,10 +3232,15 @@ static_inline int
 rxi_AbortIfServerBusy(osi_socket socket, struct rx_connection *conn,
                      struct rx_packet *np)
 {
+    afs_uint32 serial;
+
     if ((rx_BusyThreshold > 0) &&
        (rx_atomic_read(&rx_nWaiting) > rx_BusyThreshold)) {
+       MUTEX_ENTER(&conn->conn_data_lock);
+       serial = ++conn->serial;
+       MUTEX_EXIT(&conn->conn_data_lock);
        rxi_SendRawAbort(socket, conn->peer->host, conn->peer->port,
-                        rx_BusyError, np, 0);
+                        serial, rx_BusyError, np, 0);
        if (rx_stats_active)
            rx_atomic_inc(&rx_stats.nBusies);
        return 1;
@@ -3222,6 +3294,36 @@ rxi_ReceiveServerCall(osi_socket socket, struct rx_packet *np,
     call = conn->call[channel];
 
     if (!call) {
+       if (np->header.type != RX_PACKET_TYPE_DATA) {
+           /*
+            * Clients must send DATA packets at some point to create a new
+            * call. If the first packet we saw for this call channel is
+            * something else, then either the DATA packets got lost/delayed,
+            * or we were restarted and this is an existing call from before we
+            * were restarted. In the latter case, some clients get confused if
+            * we respond to such requests, so just drop the packet to make
+            * things easier for them.
+            */
+           MUTEX_EXIT(&conn->conn_call_lock);
+           if (rx_stats_active)
+               rx_atomic_inc(&rx_stats.spuriousPacketsRead);
+           return NULL;
+       }
+
+       if (np->header.seq > rx_maxReceiveWindow) {
+           /*
+            * This is a DATA packet for further along in the call than is
+            * possible for a new call. This is probably from an existing call
+            * that was in the middle of running when we were restarted; ignore
+            * it to avoid confusing clients. (See above comment about non-DATA
+            * packets.)
+            */
+           MUTEX_EXIT(&conn->conn_call_lock);
+           if (rx_stats_active)
+               rx_atomic_inc(&rx_stats.spuriousPacketsRead);
+           return NULL;
+       }
+
        if (rxi_AbortIfServerBusy(socket, conn, np)) {
            MUTEX_EXIT(&conn->conn_call_lock);
            return NULL;
@@ -3333,6 +3435,7 @@ rxi_ReceivePacket(struct rx_packet *np, osi_socket socket,
     struct rx_connection *conn;
     int type;
     int unknownService = 0;
+    int invalid = 0;
 #ifdef RXDEBUG
     char *packetType;
 #endif
@@ -3345,7 +3448,7 @@ rxi_ReceivePacket(struct rx_packet *np, osi_socket socket,
  * this is the first time the packet has been seen */
     packetType = (np->header.type > 0 && np->header.type < RX_N_PACKET_TYPES)
        ? rx_packetTypes[np->header.type - 1] : "*UNKNOWN*";
-    dpf(("R %d %s: %x.%d.%d.%d.%d.%d.%d flags %d, packet %"AFS_PTR_FMT"\n",
+    dpf(("R %d %s: %x.%d.%d.%d.%d.%d.%d flags %d, packet %p\n",
         np->header.serial, packetType, ntohl(host), ntohs(port), np->header.serviceId,
         np->header.epoch, np->header.cid, np->header.callNumber,
         np->header.seq, np->header.flags, np));
@@ -3395,7 +3498,7 @@ rxi_ReceivePacket(struct rx_packet *np, osi_socket socket,
        memset(&addr.sin_zero, 0, sizeof(addr.sin_zero));
 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
        addr.sin_len = sizeof(addr);
-#endif /* AFS_OSF_ENV */
+#endif
        drop = (*rx_justReceived) (np, &addr);
        /* drop packet if return value is non-zero */
        if (drop)
@@ -3420,7 +3523,7 @@ rxi_ReceivePacket(struct rx_packet *np, osi_socket socket,
        don't abort an abort. */
     if (!conn) {
         if (unknownService && (np->header.type != RX_PACKET_TYPE_ABORT))
-            rxi_SendRawAbort(socket, host, port, RX_INVALID_OPERATION,
+           rxi_SendRawAbort(socket, host, port, 0, RX_INVALID_OPERATION,
                              np, 0);
         return np;
     }
@@ -3523,7 +3626,7 @@ rxi_ReceivePacket(struct rx_packet *np, osi_socket socket,
                (void)rxi_SendAck(call, 0, np->header.serial,
                                  RX_ACK_PING_RESPONSE, 1);
        }
-       np = rxi_ReceiveAckPacket(call, np, 1);
+       np = rxi_ReceiveAckPacket(call, np, 1, &invalid);
        break;
     case RX_PACKET_TYPE_ABORT: {
        /* An abort packet: reset the call, passing the error up to the user. */
@@ -3556,11 +3659,16 @@ rxi_ReceivePacket(struct rx_packet *np, osi_socket socket,
        np = rxi_SendCallAbort(call, np, 1, 0);
        break;
     };
-    /* Note when this last legitimate packet was received, for keep-alive
-     * processing.  Note, we delay getting the time until now in the hope that
-     * the packet will be delivered to the user before any get time is required
-     * (if not, then the time won't actually be re-evaluated here). */
-    call->lastReceiveTime = clock_Sec();
+    if (invalid) {
+       if (rx_stats_active)
+           rx_atomic_inc(&rx_stats.spuriousPacketsRead);
+    } else {
+       /*
+        * Note when this last legitimate packet was received, for keep-alive
+        * processing.
+        */
+       call->lastReceiveTime = clock_Sec();
+    }
     MUTEX_EXIT(&call->lock);
     putConnection(conn);
     return np;
@@ -3750,7 +3858,7 @@ rxi_CheckConnReach(struct rx_connection *conn, struct rx_call *call)
 static void
 TryAttach(struct rx_call *acall, osi_socket socket,
          int *tnop, struct rx_call **newcallp,
-         int reachOverride)
+         int reachOverride, int istack)
 {
     struct rx_connection *conn = acall->conn;
 
@@ -3764,7 +3872,19 @@ TryAttach(struct rx_call *acall, osi_socket socket,
             * may not any proc available
             */
        } else {
-           rxi_ChallengeOn(acall->conn);
+           int code;
+           code = rxi_ChallengeOn(acall->conn);
+           if (code) {
+               /*
+                * Ideally we would rxi_ConnectionError here, but doing that is
+                * difficult, because some callers may have locked 'call',
+                * _and_ another call on the same conn. So we cannot
+                * rxi_ConnectionError, since that needs to lock every call on
+                * the conn. But we can at least abort the call we have.
+                */
+               rxi_CallError(acall, code);
+               rxi_SendCallAbort(acall, NULL, istack, 0);
+           }
        }
     }
 }
@@ -3801,7 +3921,7 @@ rxi_ReceiveDataPacket(struct rx_call *call,
         if (rx_stats_active)
             rx_atomic_inc(&rx_stats.noPacketBuffersOnRead);
        rxi_calltrace(RX_TRACE_DROP, call);
-       dpf(("packet %"AFS_PTR_FMT" dropped on receipt - quota problems\n", np));
+       dpf(("packet %p dropped on receipt - quota problems\n", np));
         /* We used to clear the receive queue here, in an attempt to free
          * packets. However this is unsafe if the queue has received a
          * soft ACK for the final packet */
@@ -3855,7 +3975,7 @@ rxi_ReceiveDataPacket(struct rx_call *call,
                && opr_queue_First(&call->rq, struct rx_packet, entry)->header.seq == seq) {
                 if (rx_stats_active)
                     rx_atomic_inc(&rx_stats.dupPacketsRead);
-               dpf(("packet %"AFS_PTR_FMT" dropped on receipt - duplicate\n", np));
+               dpf(("packet %p dropped on receipt - duplicate\n", np));
                rxi_CancelDelayedAckEvent(call);
                np = rxi_SendAck(call, np, serial, RX_ACK_DUPLICATE, istack);
                ackNeeded = 0;
@@ -3913,7 +4033,7 @@ rxi_ReceiveDataPacket(struct rx_call *call,
            if (call->arrivalProc) {
                (*call->arrivalProc) (call, call->arrivalProcHandle,
                                      call->arrivalProcArg);
-               call->arrivalProc = (void (*)())0;
+               call->arrivalProc = NULL;
            }
 
            /* Update last packet received */
@@ -3924,7 +4044,7 @@ rxi_ReceiveDataPacket(struct rx_call *call,
             * server thread is available, this thread becomes a server
             * thread and the server thread becomes a listener thread. */
            if (isFirst) {
-               TryAttach(call, socket, tnop, newcallp, 0);
+               TryAttach(call, socket, tnop, newcallp, 0, istack);
            }
        }
        /* This is not the expected next packet. */
@@ -4105,7 +4225,8 @@ rxi_ReceiveDataPacket(struct rx_call *call,
 }
 
 static void
-rxi_UpdatePeerReach(struct rx_connection *conn, struct rx_call *acall)
+rxi_UpdatePeerReach(struct rx_connection *conn, struct rx_call *acall,
+                   int istack)
 {
     struct rx_peer *peer = conn->peer;
 
@@ -4126,7 +4247,7 @@ rxi_UpdatePeerReach(struct rx_connection *conn, struct rx_call *acall)
                if (call != acall)
                    MUTEX_ENTER(&call->lock);
                /* tnop can be null if newcallp is null */
-               TryAttach(call, (osi_socket) - 1, NULL, NULL, 1);
+               TryAttach(call, (osi_socket) - 1, NULL, NULL, 1, istack);
                if (call != acall)
                    MUTEX_EXIT(&call->lock);
            }
@@ -4164,11 +4285,46 @@ rx_ack_reason(int reason)
 }
 #endif
 
+static_inline int
+ack_is_valid(struct rx_call *call, afs_uint32 first, afs_uint32 prev)
+{
+    if (first < call->tfirst) {
+       /*
+        * The peer indicated that the window went backwards. That's not
+        * allowed; the window can only move forwards.
+        */
+       return 0;
+    }
+
+    if (first == call->tfirst && prev < call->tprev) {
+       /*
+        * The peer said the last DATA packet it received was seq X, but it
+        * already told us before that it had received data after X. This is
+        * probably just an out-of-order ACK, and so we can ignore it.
+        */
+       if (prev >= call->tfirst + call->twind) {
+           /*
+            * Some peers (OpenAFS libafs before 1.6.23) mistakenly set the
+            * previousPacket field to a serial number, not a sequence number.
+            * The sequence number the peer told us about is further than our
+            * transmit window, so it cannot possibly be correct; it's probably
+            * actually a serial number. Don't ignore packets based on this;
+            * the previousPacket information is not accurate.
+            */
+           return 1;
+       }
+
+       return 0;
+    }
+
+    /* Otherwise, the ack looks valid. */
+    return 1;
+}
 
 /* The real smarts of the whole thing.  */
 static struct rx_packet *
 rxi_ReceiveAckPacket(struct rx_call *call, struct rx_packet *np,
-                    int istack)
+                    int istack, int *a_invalid)
 {
     struct rx_ackPacket *ap;
     int nAcks;
@@ -4189,6 +4345,8 @@ rxi_ReceiveAckPacket(struct rx_call *call, struct rx_packet *np,
     int pktsize = 0;            /* Set if we need to update the peer mtu */
     int conn_data_locked = 0;
 
+    *a_invalid = 1;
+
     if (rx_stats_active)
         rx_atomic_inc(&rx_stats.ackPacketsRead);
     ap = (struct rx_ackPacket *)rx_DataOf(np);
@@ -4202,25 +4360,20 @@ rxi_ReceiveAckPacket(struct rx_call *call, struct rx_packet *np,
     prev = ntohl(ap->previousPacket);
     serial = ntohl(ap->serial);
 
-    /*
-     * Ignore ack packets received out of order while protecting
-     * against peers that set the previousPacket field to a packet
-     * serial number instead of a sequence number.
-     */
-    if (first < call->tfirst ||
-        (first == call->tfirst && prev < call->tprev && prev < call->tfirst
-        + call->twind)) {
+    if (!ack_is_valid(call, first, prev)) {
        return np;
     }
 
     call->tprev = prev;
 
+    *a_invalid = 0;
+
     if (np->header.flags & RX_SLOW_START_OK) {
        call->flags |= RX_CALL_SLOW_START_OK;
     }
 
     if (ap->reason == RX_ACK_PING_RESPONSE)
-       rxi_UpdatePeerReach(conn, call);
+       rxi_UpdatePeerReach(conn, call, istack);
 
     if (conn->lastPacketSizeSeq) {
        MUTEX_ENTER(&conn->conn_data_lock);
@@ -4345,7 +4498,7 @@ rxi_ReceiveAckPacket(struct rx_call *call, struct rx_packet *np,
 
 #ifdef RX_ENABLE_LOCKS
        /* XXX Hack. Because we have to release the global call lock when sending
-        * packets (osi_NetSend) we drop all acks while we're traversing the tq
+        * packets (rxi_NetSend) we drop all acks while we're traversing the tq
         * in rxi_Start sending packets out because packets may move to the
         * freePacketQueue as result of being here! So we drop these packets until
         * we're safely out of the traversing. Really ugly!
@@ -4427,12 +4580,20 @@ rxi_ReceiveAckPacket(struct rx_call *call, struct rx_packet *np,
        rx_packetread(np, rx_AckDataSize(ap->nAcks) + (int)sizeof(afs_int32),
                      (int)sizeof(afs_int32), &tSize);
        tSize = (afs_uint32) ntohl(tSize);
+       if (tSize > RX_MAX_PACKET_SIZE)
+           tSize = RX_MAX_PACKET_SIZE;
+       if (tSize < RX_MIN_PACKET_SIZE)
+           tSize = RX_MIN_PACKET_SIZE;
        peer->natMTU = rxi_AdjustIfMTU(MIN(tSize, peer->ifMTU));
 
        /* Get the maximum packet size to send to this peer */
        rx_packetread(np, rx_AckDataSize(ap->nAcks), (int)sizeof(afs_int32),
                      &tSize);
        tSize = (afs_uint32) ntohl(tSize);
+       if (tSize > RX_MAX_PACKET_SIZE)
+           tSize = RX_MAX_PACKET_SIZE;
+       if (tSize < RX_MIN_PACKET_SIZE)
+           tSize = RX_MIN_PACKET_SIZE;
        tSize = (afs_uint32) MIN(tSize, rx_MyMaxSendSize);
        tSize = rxi_AdjustMaxMTU(peer->natMTU, tSize);
 
@@ -4454,6 +4615,10 @@ rxi_ReceiveAckPacket(struct rx_call *call, struct rx_packet *np,
                          rx_AckDataSize(ap->nAcks) + 2 * (int)sizeof(afs_int32),
                          (int)sizeof(afs_int32), &tSize);
            tSize = (afs_uint32) ntohl(tSize);  /* peer's receive window, if it's */
+           if (tSize == 0)
+               tSize = 1;
+           if (tSize >= rx_maxSendWindow)
+               tSize = rx_maxSendWindow;
            if (tSize < call->twind) {  /* smaller than our send */
                call->twind = tSize;    /* window, we must send less... */
                call->ssthresh = MIN(call->twind, call->ssthresh);
@@ -4475,6 +4640,10 @@ rxi_ReceiveAckPacket(struct rx_call *call, struct rx_packet *np,
                          rx_AckDataSize(ap->nAcks) + 2 * (int)sizeof(afs_int32),
                          sizeof(afs_int32), &tSize);
            tSize = (afs_uint32) ntohl(tSize);
+           if (tSize == 0)
+               tSize = 1;
+           if (tSize >= rx_maxSendWindow)
+               tSize = rx_maxSendWindow;
            /*
             * As of AFS 3.5 we set the send window to match the receive window.
             */
@@ -4748,7 +4917,7 @@ rxi_ReceiveResponsePacket(struct rx_connection *conn,
         * some calls went into attach-wait while we were waiting
         * for authentication..
         */
-       rxi_UpdatePeerReach(conn, NULL);
+       rxi_UpdatePeerReach(conn, NULL, istack);
     }
     return np;
 }
@@ -4856,6 +5025,7 @@ rxi_AttachServerProc(struct rx_call *call,
            if (opr_queue_IsOnQueue(&call->entry)) {
                opr_queue_Remove(&call->entry);
            }
+           CLEAR_CALL_QUEUE_LOCK(call);
        }
        call->state = RX_STATE_ACTIVE;
        call->app.mode = RX_MODE_RECEIVING;
@@ -5046,7 +5216,7 @@ rxi_ClearReceiveQueue(struct rx_call *call)
 #ifdef RXDEBUG_PACKET
         call->rqc -= count;
         if ( call->rqc != 0 )
-            dpf(("rxi_ClearReceiveQueue call %"AFS_PTR_FMT" rqc %u != 0\n", call, call->rqc));
+         dpf(("rxi_ClearReceiveQueue call %p rqc %u != 0\n", call, call->rqc));
 #endif
        call->flags &= ~(RX_CALL_RECEIVE_DONE | RX_CALL_HAVE_LAST);
     }
@@ -5070,7 +5240,14 @@ rxi_SendCallAbort(struct rx_call *call, struct rx_packet *packet,
     if (rx_IsClientConn(call->conn))
        force = 1;
 
-    if (call->abortCode != call->error) {
+    /*
+     * An opcode that has been deprecated or has yet to be implemented is not
+     * a misbehavior of the client.  Do not punish the client by introducing
+     * delays.
+     */
+    if (call->error == RXGEN_OPCODE) {
+       force = 1;
+    } else if (call->abortCode != call->error) {
        call->abortCode = call->error;
        call->abortCount = 0;
     }
@@ -5079,7 +5256,8 @@ rxi_SendCallAbort(struct rx_call *call, struct rx_packet *packet,
        || call->abortCount < rxi_callAbortThreshhold) {
        rxi_CancelDelayedAbortEvent(call);
        error = htonl(call->error);
-       call->abortCount++;
+       if (!force)
+           call->abortCount++;
        packet =
            rxi_SendSpecial(call, call->conn, packet, RX_PACKET_TYPE_ABORT,
                            (char *)&error, sizeof(error), istack);
@@ -5155,7 +5333,7 @@ rxi_ConnectionError(struct rx_connection *conn,
     if (error) {
        int i;
 
-       dpf(("rxi_ConnectionError conn %"AFS_PTR_FMT" error %d\n", conn, error));
+       dpf(("rxi_ConnectionError conn %p error %d\n", conn, error));
 
        MUTEX_ENTER(&conn->conn_data_lock);
        if (rxevent_Cancel(&conn->challengeEvent))
@@ -5200,7 +5378,7 @@ void
 rxi_CallError(struct rx_call *call, afs_int32 error)
 {
     MUTEX_ASSERT(&call->lock);
-    dpf(("rxi_CallError call %"AFS_PTR_FMT" error %d call->error %d\n", call, error, call->error));
+    dpf(("rxi_CallError call %p error %d call->error %d\n", call, error, call->error));
     if (call->error)
        error = call->error;
 
@@ -5229,13 +5407,13 @@ rxi_ResetCall(struct rx_call *call, int newcall)
     struct rx_packet *packet;
 
     MUTEX_ASSERT(&call->lock);
-    dpf(("rxi_ResetCall(call %"AFS_PTR_FMT", newcall %d)\n", call, newcall));
+    dpf(("rxi_ResetCall(call %p, newcall %d)\n", call, newcall));
 
     /* Notify anyone who is waiting for asynchronous packet arrival */
     if (call->arrivalProc) {
        (*call->arrivalProc) (call, call->arrivalProcHandle,
                              call->arrivalProcArg);
-       call->arrivalProc = (void (*)())0;
+       call->arrivalProc = NULL;
     }
 
 
@@ -5290,7 +5468,7 @@ rxi_ResetCall(struct rx_call *call, int newcall)
 
     rxi_ClearTransmitQueue(call, 1);
     if (call->tqWaiters || (flags & RX_CALL_TQ_WAIT)) {
-        dpf(("rcall %"AFS_PTR_FMT" has %d waiters and flags %d\n", call, call->tqWaiters, call->flags));
+       dpf(("rcall %p has %d waiters and flags %d\n", call, call->tqWaiters, call->flags));
     }
     call->flags = 0;
 
@@ -6082,7 +6260,7 @@ rxi_Start(struct rx_call *call, int istack)
                                             nXmitPackets, istack);
                            goto restart;
                        }
-                        dpf(("call %d xmit packet %"AFS_PTR_FMT"\n",
+                      dpf(("call %d xmit packet %p\n",
                               *(call->callNumber), p));
                        call->xmitList[nXmitPackets++] = p;
                    }
@@ -6161,6 +6339,7 @@ void
 rxi_Send(struct rx_call *call, struct rx_packet *p,
         int istack)
 {
+    int code;
     struct rx_connection *conn = call->conn;
 
     /* Stamp each packet with the user supplied status */
@@ -6168,7 +6347,15 @@ rxi_Send(struct rx_call *call, struct rx_packet *p,
 
     /* Allow the security object controlling this call's security to
      * make any last-minute changes to the packet */
-    RXS_SendPacket(conn->securityObject, call, p);
+    code = RXS_SendPacket(conn->securityObject, call, p);
+    if (code) {
+       MUTEX_EXIT(&call->lock);
+       CALL_HOLD(call, RX_CALL_REFCOUNT_SEND);
+       rxi_ConnectionError(conn, code);
+       CALL_RELE(call, RX_CALL_REFCOUNT_SEND);
+       MUTEX_ENTER(&call->lock);
+       return;
+    }
 
     /* Since we're about to send SOME sort of packet to the peer, it's
      * safe to nuke any scheduled end-of-packets ack */
@@ -6384,7 +6571,7 @@ rxi_NatKeepAliveEvent(struct rxevent *event, void *arg1,
     tmpiov[0].iov_base = tbuffer;
     tmpiov[0].iov_len = 1 + sizeof(struct rx_header);
 
-    osi_NetSend(socket, &taddr, tmpiov, 1, 1 + sizeof(struct rx_header), 1);
+    rxi_NetSend(socket, &taddr, tmpiov, 1, 1 + sizeof(struct rx_header), 1);
 
     MUTEX_ENTER(&conn->conn_data_lock);
     /* We ran, so the handle is no longer needed to try to cancel ourselves. */
@@ -6673,27 +6860,30 @@ rxi_SendDelayedCallAbort(struct rxevent *event, void *arg1, void *dummy,
  *
  * This routine is both an event handler and a function called directly;
  * when called directly the passed |event| is NULL and the
- * conn->conn->data>lock must must not be held.
+ * conn->conn->data>lock must must not be held.  Also, when called as an
+ * an event handler, we must putConnection before we exit; but when called
+ * directly (the first challenge), we must NOT putConnection.
  */
 static void
 rxi_ChallengeEvent(struct rxevent *event,
                   void *arg0, void *arg1, int tries)
 {
     struct rx_connection *conn = arg0;
+    int event_raised = 0;      /* assume we were called directly */
 
     MUTEX_ENTER(&conn->conn_data_lock);
-    if (event != NULL && event == conn->challengeEvent)
+    if (event != NULL && event == conn->challengeEvent) {
+       event_raised = 1;       /* called as an event */
        rxevent_Put(&conn->challengeEvent);
+    }
     MUTEX_EXIT(&conn->conn_data_lock);
 
     /* If there are no active calls it is not worth re-issuing the
      * challenge.  If the client issues another call on this connection
      * the challenge can be requested at that time.
      */
-    if (!rxi_HasActiveCalls(conn)) {
-       putConnection(conn);
-        return;
-    }
+    if (!rxi_HasActiveCalls(conn))
+       goto done;
 
     if (RXS_CheckAuthentication(conn->securityObject, conn) != 0) {
        struct rx_packet *packet;
@@ -6719,18 +6909,33 @@ rxi_ChallengeEvent(struct rxevent *event,
                }
            }
            MUTEX_EXIT(&conn->conn_call_lock);
-           putConnection(conn);
-           return;
+           goto done;
        }
 
        packet = rxi_AllocPacket(RX_PACKET_CLASS_SPECIAL);
        if (packet) {
-           /* If there's no packet available, do this later. */
-           RXS_GetChallenge(conn->securityObject, conn, packet);
-           rxi_SendSpecial((struct rx_call *)0, conn, packet,
-                           RX_PACKET_TYPE_CHALLENGE, NULL, -1, 0);
+           int code;
+           code = RXS_GetChallenge(conn->securityObject, conn, packet);
+           if (code && event_raised) {
+               /*
+                * We can only rxi_ConnectionError the connection if we are
+                * running as an event. Otherwise, the caller may have our call
+                * locked, and so we cannot call rxi_ConnectionError (since it
+                * tries to lock each call in the conn).
+                */
+               rxi_FreePacket(packet);
+               rxi_ConnectionError(conn, code);
+               goto done;
+           }
+           if (code == 0) {
+               /* Only send a challenge packet if we were able to allocate a
+                * packet, and the security layer successfully populated the
+                * challenge. */
+               rxi_SendSpecial((struct rx_call *)0, conn, packet,
+                               RX_PACKET_TYPE_CHALLENGE, NULL, -1, 0);
+               conn->securityChallengeSent = 1;
+           }
            rxi_FreePacket(packet);
-           conn->securityChallengeSent = 1;
        }
        clock_GetTime(&now);
        when = now;
@@ -6745,7 +6950,9 @@ rxi_ChallengeEvent(struct rxevent *event,
        }
        MUTEX_EXIT(&conn->conn_data_lock);
     }
-    putConnection(conn);
+ done:
+    if (event_raised)
+       putConnection(conn);
 }
 
 /* Call this routine to start requesting the client to authenticate
@@ -6753,7 +6960,7 @@ rxi_ChallengeEvent(struct rxevent *event,
  * the call times out, or an invalid response is returned.  The
  * security object associated with the connection is asked to create
  * the challenge at this time. */
-static void
+static int
 rxi_ChallengeOn(struct rx_connection *conn)
 {
     int start = 0;
@@ -6762,9 +6969,14 @@ rxi_ChallengeOn(struct rx_connection *conn)
        start = 1;
     MUTEX_EXIT(&conn->conn_data_lock);
     if (start) {
-       RXS_CreateChallenge(conn->securityObject, conn);
+       int code;
+       code = RXS_CreateChallenge(conn->securityObject, conn);
+       if (code) {
+           return code;
+       }
        rxi_ChallengeEvent(NULL, conn, 0, RX_CHALLENGE_MAXTRIES);
-    };
+    }
+    return 0;
 }
 
 
@@ -6820,7 +7032,7 @@ rxi_ComputeRoundTripTime(struct rx_packet *p,
        return;                 /* somebody set the clock back, don't count this time. */
 
     clock_Sub(&thisRtt, sentp);
-    dpf(("rxi_ComputeRoundTripTime(call=%d packet=%"AFS_PTR_FMT" rttp=%d.%06d sec)\n",
+    dpf(("rxi_ComputeRoundTripTime(call=%d packet=%p rttp=%d.%06d sec)\n",
           p->header.callNumber, p, thisRtt.sec, thisRtt.usec));
 
     if (clock_IsZero(&thisRtt)) {
@@ -6919,8 +7131,10 @@ rxi_ComputeRoundTripTime(struct rx_packet *p,
     peer->rtt_dev = call->rtt_dev;
     peer->rtt = call->rtt;
 
-    dpf(("rxi_ComputeRoundTripTime(call=%d packet=%"AFS_PTR_FMT" rtt=%d ms, srtt=%d ms, rtt_dev=%d ms, timeout=%d.%06d sec)\n",
-          p->header.callNumber, p, MSEC(&thisRtt), call->rtt >> 3, call->rtt_dev >> 2, (call->rto.sec), (call->rto.usec)));
+    dpf(("rxi_ComputeRoundTripTime(call=%d packet=%p rtt=%d ms, srtt=%d ms, "
+        "rtt_dev=%d ms, timeout=%d.%06d sec)\n",
+        p->header.callNumber, p, MSEC(&thisRtt), call->rtt >> 3,
+        call->rtt_dev >> 2, (call->rto.sec), (call->rto.usec)));
 }
 
 
@@ -7810,9 +8024,12 @@ shutdown_rx(void)
     struct rx_serverQueueEntry *sq;
 #endif /* KERNEL */
 
-    if (rx_atomic_test_and_set_bit(&rxinit_status, 0))
+    LOCK_RX_INIT;
+    if (!rxi_IsRunning()) {
+       UNLOCK_RX_INIT;
        return;                 /* Already shutdown. */
-
+    }
+    rx_atomic_set(&rxi_running, 0);
 #ifndef KERNEL
     rx_port = 0;
 #ifndef AFS_PTHREAD_ENV
@@ -7909,8 +8126,10 @@ shutdown_rx(void)
 
     MUTEX_ENTER(&freeSQEList_lock);
 
-    while ((np = rx_FreeSQEList)) {
-       rx_FreeSQEList = *(struct rx_serverQueueEntry **)np;
+    while (!opr_queue_IsEmpty(&rx_freeServerQueue)) {
+       np = opr_queue_First(&rx_freeServerQueue, struct rx_serverQueueEntry,
+                            entry);
+       opr_queue_Remove(&np->entry);
        MUTEX_DESTROY(&np->lock);
        rxi_Free(np, sizeof(*np));
     }
@@ -7934,6 +8153,7 @@ shutdown_rx(void)
     rxi_dataQuota = RX_MAX_QUOTA;
     rxi_availProcs = rxi_totalMin = rxi_minDeficit = 0;
     MUTEX_EXIT(&rx_quota_mutex);
+    UNLOCK_RX_INIT;
 }
 
 #ifndef KERNEL
@@ -9264,3 +9484,83 @@ int rx_DumpCalls(FILE *outputFile, char *cookie)
     return 0;
 }
 #endif
+
+#ifdef AFS_RXERRQ_ENV
+void
+rxi_HandleSocketErrors(osi_socket sock)
+{
+    size_t cmsgbuf_len = 256;
+    void *cmsgbuf;
+# ifndef KERNEL
+    int errno_save = errno;
+# endif
+
+    cmsgbuf = rxi_Alloc(cmsgbuf_len);
+    if (cmsgbuf == NULL) {
+       goto done;
+    }
+
+    while (osi_HandleSocketError(sock, cmsgbuf, cmsgbuf_len))
+       ;
+
+    rxi_Free(cmsgbuf, cmsgbuf_len);
+
+ done:
+# ifndef KERNEL
+    errno = errno_save;
+# endif
+    return;
+}
+
+static int
+NetSend_retry(osi_socket sock, void *addr, struct iovec *dvec, int nvecs,
+             int length, int istack)
+{
+    int code;
+    int safety;
+    /*
+     * If an ICMP error comes in for any peer, sendmsg() can return -1 with an
+     * errno of EHOSTUNREACH, ENETUNREACH, etc. There may be no problem with
+     * sending this packet (an error is returned just to indicate we need to
+     * read in pending errors), but the packet wasn't actually sent.
+     *
+     * It's difficult to determine in general whether sendmsg() is returning an
+     * error due to a received ICMP error, or we're getting an actual error for
+     * this specific sendmsg() call, since there may be other threads running
+     * sendmsg/recvmsg/rxi_HandleSocketErrors at the same time. So, just retry
+     * the sendmsg a few times; make sure not to retry forever, in case we are
+     * getting an actual error from this sendmsg() call.
+     *
+     * Also note that if we accidentally drop a packet here that we didn't need
+     * to, it's not the end of the world. Packets get dropped, and we should be
+     * able to recover.
+     */
+    for (safety = 0; safety < RXI_SENDMSG_RETRY; safety++) {
+       code = osi_NetSend(sock, addr, dvec, nvecs, length, istack);
+       if (code == 0) {
+           return 0;
+       }
+       rxi_HandleSocketErrors(sock);
+    }
+    return code;
+
+}
+#endif
+
+int
+rxi_NetSend(osi_socket socket, void *addr, struct iovec *dvec,
+           int nvecs, int length, int istack)
+{
+    if (rxi_IsRunning()) {
+#ifdef AFS_RXERRQ_ENV
+       return NetSend_retry(socket, addr, dvec, nvecs, length, istack);
+#else
+       return osi_NetSend(socket, addr, dvec, nvecs, length, istack);
+#endif
+    }
+#ifdef AFS_NT40_ENV
+    return WSAESHUTDOWN;
+#else
+    return ESHUTDOWN;
+#endif
+}