netrestrict-netinfo-20081129
[openafs.git] / src / rx / rx_user.c
index bd59a87..efd5f22 100644 (file)
@@ -18,6 +18,7 @@ RCSID
 # include <sys/types.h>
 # include <errno.h>
 # include <signal.h>
+# include <string.h>
 #ifdef AFS_NT40_ENV
 # include <WINNT/syscfg.h>
 #else
@@ -36,13 +37,6 @@ RCSID
 #endif
 #include <afs/afs_args.h>
 #include <afs/afsutil.h>
-#ifdef HAVE_STRING_H
-#include <string.h>
-#else
-#ifdef HAVE_STRINGS_H
-#include <strings.h>
-#endif
-#endif
 
 #ifndef        IPPORT_USERRESERVED
 /* If in.h doesn't define this, define it anyway.  Unfortunately, defining
@@ -54,6 +48,7 @@ RCSID
 #ifndef AFS_NT40_ENV
 # include <sys/time.h>
 #endif
+# include "rx_internal.h"
 # include "rx.h"
 # include "rx_globals.h"
 
@@ -101,8 +96,20 @@ rxi_GetHostUDPSocket(u_int ahost, u_short port)
     struct sockaddr_in taddr;
     char *name = "rxi_GetUDPSocket: ";
 #ifdef AFS_LINUX22_ENV
+#if defined(ADAPT_PMTU)
+    int pmtu=IP_PMTUDISC_WANT;
+    int recverr=1;
+#else
     int pmtu=IP_PMTUDISC_DONT;
 #endif
+#endif
+#if defined(HAVE_LINUX_ERRQUEUE_H) && defined(ADAPT_PMTU)
+#include <linux/types.h>
+#include <linux/errqueue.h>
+#ifndef IP_MTU
+#define IP_MTU 14
+#endif
+#endif
 
 #if !defined(AFS_NT40_ENV) 
     if (ntohs(port) >= IPPORT_RESERVED && ntohs(port) < IPPORT_USERRESERVED) {
@@ -184,8 +191,10 @@ rxi_GetHostUDPSocket(u_int ahost, u_short port)
 
 #ifdef AFS_LINUX22_ENV
     setsockopt(socketFd, SOL_IP, IP_MTU_DISCOVER, &pmtu, sizeof(pmtu));
+#if defined(ADAPT_PMTU)
+    setsockopt(socketFd, SOL_IP, IP_RECVERR, &recverr, sizeof(recverr));
+#endif
 #endif
-
     if (rxi_Listen(socketFd) < 0) {
        goto error;
     }
@@ -211,11 +220,13 @@ 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();
@@ -290,21 +301,21 @@ rxi_getaddr(void)
 ** 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
+#ifdef AFS_DJGPP_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 */
+#endif /* AFS_DJGPP_ENV */
 
     for (count = 0; offset < rxi_numNetAddrs && maxSize > 0;
         count++, offset++, maxSize--)
@@ -312,18 +323,72 @@ rx_getAllAddr(afs_int32 * buffer, int maxSize)
 
     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();
+
+#ifdef AFS_DJGPP_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_DJGPP_ENV */
+
+    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 int 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 && 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;
 
@@ -345,12 +410,16 @@ rx_GetIFInfo(void)
 
     }
     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 (rxinit_status)
+        return;
+
+    rxi_InitMorePackets();
 }
 #endif
 
@@ -611,6 +680,10 @@ rxi_InitPeerParams(struct rx_peer *pp)
     afs_uint32 ppaddr;
     u_short rxmtu;
     int ix;
+#if defined(ADAPT_PMTU) && defined(IP_MTU)
+    int sock;
+    struct sockaddr_in addr;
+#endif
 
     LOCK_IF_INIT;
     if (!Inited) {
@@ -660,15 +733,31 @@ rxi_InitPeerParams(struct rx_peer *pp)
     pp->timeout.sec = 2;
     pp->ifMTU = MIN(rx_MyMaxSendSize, OLD_MAX_PACKET_SIZE);
 #endif /* ADAPT_MTU */
+#if defined(ADAPT_PMTU) && defined(IP_MTU)
+    sock=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
+    if (sock >= 0) {
+      addr.sin_family = AF_INET;
+      addr.sin_addr.s_addr = pp->host;
+      addr.sin_port = pp->port;
+      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);
+       }
+      }
+      close(sock);
+    }
+#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);
@@ -692,3 +781,54 @@ rx_SetMaxMTU(int mtu)
 {
     rx_MyMaxSendSize = rx_maxReceiveSizeUser = rx_maxReceiveSize = mtu;
 }
+
+#if defined(HAVE_LINUX_ERRQUEUE_H) && defined(ADAPT_PMTU)
+int
+rxi_HandleSocketError(int socket)
+{
+    struct msghdr msg;
+    struct cmsghdr *cmsg;
+    struct sock_extended_err *err;
+    struct sockaddr_in addr;
+    struct sockaddr *offender;
+    char controlmsgbuf[256];
+    int ret=0;
+    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))
+        goto out;
+
+    for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
+       if ((char *)cmsg - controlmsgbuf > msg.msg_controllen - CMSG_SPACE(0) ||
+           (char *)cmsg - controlmsgbuf > msg.msg_controllen - CMSG_SPACE(cmsg->cmsg_len) ||
+          cmsg->cmsg_len == 0) {
+          cmsg = 0;
+           break;
+       }
+        if (cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_RECVERR)
+            break;
+    }
+    if (!cmsg)
+        goto out;
+    ret=1;
+    err =(struct sock_extended_err *) CMSG_DATA(cmsg);
+    
+    if (err->ee_errno == EMSGSIZE && err->ee_info >= 68) {
+        rxi_SetPeerMtu(addr.sin_addr.s_addr, addr.sin_port,
+                       err->ee_info - RX_IPUDP_SIZE);
+    }
+    /* other DEST_UNREACH's and TIME_EXCEEDED should be dealt with too */
+    
+out:
+    return ret;
+}
+#endif