rx-rw-locking-20081024
[openafs.git] / src / rx / rx_user.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /* rx_user.c contains routines specific to the user space UNIX implementation of rx */
11
12 #include <afsconfig.h>
13 #include <afs/param.h>
14
15 RCSID
16     ("$Header$");
17
18 # include <sys/types.h>
19 # include <errno.h>
20 # include <signal.h>
21 # include <string.h>
22 #ifdef AFS_NT40_ENV
23 # include <WINNT/syscfg.h>
24 #else
25 # include <sys/socket.h>
26 # include <sys/file.h>
27 # include <netdb.h>
28 # include <sys/stat.h>
29 # include <netinet/in.h>
30 # include <sys/time.h>
31 # include <net/if.h>
32 # include <sys/ioctl.h>
33 #endif
34 # include <fcntl.h>
35 #if !defined(AFS_AIX_ENV) && !defined(AFS_NT40_ENV) 
36 # include <sys/syscall.h>
37 #endif
38 #include <afs/afs_args.h>
39 #include <afs/afsutil.h>
40
41 #ifndef IPPORT_USERRESERVED
42 /* If in.h doesn't define this, define it anyway.  Unfortunately, defining
43    this doesn't put the code into the kernel to restrict kernel assigned
44    port numbers to numbers below IPPORT_USERRESERVED...  */
45 #define IPPORT_USERRESERVED 5000
46 # endif
47
48 #ifndef AFS_NT40_ENV
49 # include <sys/time.h>
50 #endif
51 # include "rx_internal.h"
52 # include "rx.h"
53 # include "rx_globals.h"
54
55 #ifdef AFS_PTHREAD_ENV
56 #include <assert.h>
57
58 /*
59  * The rx_if_init_mutex mutex protects the following global variables:
60  * Inited
61  */
62
63 pthread_mutex_t rx_if_init_mutex;
64 #define LOCK_IF_INIT assert(pthread_mutex_lock(&rx_if_init_mutex)==0)
65 #define UNLOCK_IF_INIT assert(pthread_mutex_unlock(&rx_if_init_mutex)==0)
66
67 /*
68  * The rx_if_mutex mutex protects the following global variables:
69  * myNetFlags
70  * myNetMTUs
71  * myNetMasks
72  */
73
74 pthread_mutex_t rx_if_mutex;
75 #define LOCK_IF assert(pthread_mutex_lock(&rx_if_mutex)==0)
76 #define UNLOCK_IF assert(pthread_mutex_unlock(&rx_if_mutex)==0)
77 #else
78 #define LOCK_IF_INIT
79 #define UNLOCK_IF_INIT
80 #define LOCK_IF
81 #define UNLOCK_IF
82 #endif /* AFS_PTHREAD_ENV */
83
84
85 /*
86  * Make a socket for receiving/sending IP packets.  Set it into non-blocking
87  * and large buffering modes.  If port isn't specified, the kernel will pick
88  * one.  Returns the socket (>= 0) on success.  Returns OSI_NULLSOCKET on
89  * failure. Port must be in network byte order. 
90  */
91 osi_socket
92 rxi_GetHostUDPSocket(u_int ahost, u_short port)
93 {
94     int binds, code = 0;
95     osi_socket socketFd = OSI_NULLSOCKET;
96     struct sockaddr_in taddr;
97     char *name = "rxi_GetUDPSocket: ";
98 #ifdef AFS_LINUX22_ENV
99 #if defined(ADAPT_PMTU)
100     int pmtu=IP_PMTUDISC_WANT;
101     int recverr=1;
102 #else
103     int pmtu=IP_PMTUDISC_DONT;
104 #endif
105 #endif
106 #if defined(HAVE_LINUX_ERRQUEUE_H) && defined(ADAPT_PMTU)
107 #include <linux/types.h>
108 #include <linux/errqueue.h>
109 #ifndef IP_MTU
110 #define IP_MTU 14
111 #endif
112 #endif
113
114 #if !defined(AFS_NT40_ENV) 
115     if (ntohs(port) >= IPPORT_RESERVED && ntohs(port) < IPPORT_USERRESERVED) {
116 /*      (osi_Msg "%s*WARNING* port number %d is not a reserved port number.  Use port numbers above %d\n", name, port, IPPORT_USERRESERVED);
117 */ ;
118     }
119     if (ntohs(port) > 0 && ntohs(port) < IPPORT_RESERVED && geteuid() != 0) {
120         (osi_Msg
121          "%sport number %d is a reserved port number which may only be used by root.  Use port numbers above %d\n",
122          name, ntohs(port), IPPORT_USERRESERVED);
123         goto error;
124     }
125 #endif
126     socketFd = socket(AF_INET, SOCK_DGRAM, 0);
127
128     if (socketFd < 0) {
129         perror("socket");
130         goto error;
131     }
132
133     taddr.sin_addr.s_addr = ahost;
134     taddr.sin_family = AF_INET;
135     taddr.sin_port = (u_short) port;
136 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
137     taddr.sin_len = sizeof(struct sockaddr_in);
138 #endif
139 #define MAX_RX_BINDS 10
140     for (binds = 0; binds < MAX_RX_BINDS; binds++) {
141         if (binds)
142             rxi_Delay(10);
143         code = bind(socketFd, (struct sockaddr *)&taddr, sizeof(taddr));
144         if (!code)
145             break;
146     }
147     if (code) {
148         perror("bind");
149         (osi_Msg "%sbind failed\n", name);
150         goto error;
151     }
152 #if !defined(AFS_NT40_ENV) 
153     /*
154      * Set close-on-exec on rx socket 
155      */
156     fcntl(socketFd, F_SETFD, 1);
157 #endif
158
159     /* Use one of three different ways of getting a socket buffer expanded to
160      * a reasonable size.
161      */
162     {
163         int greedy = 0;
164         int len1, len2;
165
166         len1 = 32766;
167         len2 = rx_UdpBufSize;
168         greedy =
169             (setsockopt
170              (socketFd, SOL_SOCKET, SO_RCVBUF, (char *)&len2,
171               sizeof(len2)) >= 0);
172         if (!greedy) {
173             len2 = 32766;       /* fall back to old size... uh-oh! */
174         }
175
176         greedy =
177             (setsockopt
178              (socketFd, SOL_SOCKET, SO_SNDBUF, (char *)&len1,
179               sizeof(len1)) >= 0)
180             &&
181             (setsockopt
182              (socketFd, SOL_SOCKET, SO_RCVBUF, (char *)&len2,
183               sizeof(len2)) >= 0);
184         if (!greedy)
185             (osi_Msg "%s*WARNING* Unable to increase buffering on socket\n",
186              name);
187         MUTEX_ENTER(&rx_stats_mutex);
188         rx_stats.socketGreedy = greedy;
189         MUTEX_EXIT(&rx_stats_mutex);
190     }
191
192 #ifdef AFS_LINUX22_ENV
193     setsockopt(socketFd, SOL_IP, IP_MTU_DISCOVER, &pmtu, sizeof(pmtu));
194 #if defined(ADAPT_PMTU)
195     setsockopt(socketFd, SOL_IP, IP_RECVERR, &recverr, sizeof(recverr));
196 #endif
197 #endif
198     if (rxi_Listen(socketFd) < 0) {
199         goto error;
200     }
201
202     return socketFd;
203
204   error:
205 #ifdef AFS_NT40_ENV
206     if (socketFd >= 0)
207         closesocket(socketFd);
208 #else
209     if (socketFd >= 0)
210         close(socketFd);
211 #endif
212
213     return OSI_NULLSOCKET;
214 }
215
216 osi_socket
217 rxi_GetUDPSocket(u_short port)
218 {
219     return rxi_GetHostUDPSocket(htonl(INADDR_ANY), port);
220 }
221
222 void
223 osi_Panic(char *msg, ...)
224 {
225     va_list ap;
226     va_start(ap, msg);
227     (osi_Msg "Fatal Rx error: ");
228     (osi_VMsg msg, ap);
229     va_end(ap);
230     fflush(stderr);
231     fflush(stdout);
232     afs_abort();
233 }
234
235 /*
236  * osi_AssertFailU() -- used by the osi_Assert() macro.
237  */
238
239 void
240 osi_AssertFailU(const char *expr, const char *file, int line)
241 {
242     osi_Panic("assertion failed: %s, file: %s, line: %d\n", expr,
243               file, line);
244 }
245
246 #if defined(AFS_AIX32_ENV) && !defined(KERNEL)
247 #ifndef osi_Alloc
248 static const char memZero;
249 void *
250 osi_Alloc(afs_int32 x)
251 {
252     /* 
253      * 0-length allocs may return NULL ptr from malloc, so we special-case
254      * things so that NULL returned iff an error occurred 
255      */
256     if (x == 0)
257         return (void *)&memZero;
258     return(malloc(x));
259 }
260
261 void
262 osi_Free(void *x, afs_int32 size)
263 {
264     if (x == &memZero)
265         return;
266     free(x);
267 }
268 #endif
269 #endif /* defined(AFS_AIX32_ENV) && !defined(KERNEL) */
270
271 #define ADDRSPERSITE    16
272
273
274 static afs_uint32 rxi_NetAddrs[ADDRSPERSITE];   /* host order */
275 static int myNetMTUs[ADDRSPERSITE];
276 static int myNetMasks[ADDRSPERSITE];
277 static int myNetFlags[ADDRSPERSITE];
278 static u_int rxi_numNetAddrs;
279 static int Inited = 0;
280
281 #if defined(AFS_NT40_ENV) 
282 int
283 rxi_getaddr(void)
284 {
285     /* The IP address list can change so we must query for it */
286     rx_GetIFInfo();
287
288     /* we don't want to use the loopback adapter which is first */
289     /* this is a bad bad hack */
290     if (rxi_numNetAddrs > 1)
291         return htonl(rxi_NetAddrs[1]);  
292     else if (rxi_numNetAddrs > 0)
293         return htonl(rxi_NetAddrs[0]);
294     else
295         return 0;
296 }
297
298 /* 
299 ** return number of addresses 
300 ** and the addresses themselves in the buffer
301 ** maxSize - max number of interfaces to return.
302 */
303 int
304 rx_getAllAddr(afs_uint32 * buffer, int maxSize)
305 {
306     int count = 0, offset = 0;
307
308     /* The IP address list can change so we must query for it */
309     rx_GetIFInfo();
310
311 #ifndef AFS_NT40_ENV
312     /* we don't want to use the loopback adapter which is first */
313     /* this is a bad bad hack.
314      * and doesn't hold true on Windows.
315      */
316     if ( rxi_numNetAddrs > 1 )
317         offset = 1;
318 #endif /* AFS_NT40_ENV */
319
320     for (count = 0; offset < rxi_numNetAddrs && maxSize > 0;
321          count++, offset++, maxSize--)
322         buffer[count] = htonl(rxi_NetAddrs[offset]);
323
324     return count;
325 }
326 #endif
327
328 #ifdef AFS_NT40_ENV
329 extern int rxinit_status;
330 void 
331 rxi_InitMorePackets(void) {
332     int npackets, ncbufs;
333
334     ncbufs = (rx_maxJumboRecvSize - RX_FIRSTBUFFERSIZE);
335     if (ncbufs > 0) {
336         ncbufs = ncbufs / RX_CBUFFERSIZE;
337         npackets = rx_initSendWindow - 1;
338         rxi_MorePackets(npackets * (ncbufs + 1));
339     }
340 }
341 void
342 rx_GetIFInfo(void)
343 {
344     u_int maxsize;
345     u_int rxsize;
346     afs_uint32 i;
347
348     LOCK_IF_INIT;
349     if (Inited) {
350         if (Inited < 2 && rxinit_status == 0) {
351             /* We couldn't initialize more packets earlier.
352              * Do it now. */
353             rxi_InitMorePackets();
354             Inited = 2;
355         }
356         UNLOCK_IF_INIT;
357         return;
358     }
359     Inited = 1;
360     UNLOCK_IF_INIT;
361
362     LOCK_IF;
363     rxi_numNetAddrs = ADDRSPERSITE;
364     (void)syscfg_GetIFInfo(&rxi_numNetAddrs, rxi_NetAddrs,
365                            myNetMasks, myNetMTUs, myNetFlags);
366
367     for (i = 0; i < rxi_numNetAddrs; i++) {
368         rxsize = rxi_AdjustIfMTU(myNetMTUs[i] - RX_IPUDP_SIZE);
369         maxsize =
370             rxi_nRecvFrags * rxsize + (rxi_nRecvFrags - 1) * UDP_HDR_SIZE;
371         maxsize = rxi_AdjustMaxMTU(rxsize, maxsize);
372         if (rx_maxReceiveSize < maxsize) {
373             rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxsize);
374             rx_maxReceiveSize =
375                 MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
376         }
377
378     }
379     UNLOCK_IF;
380
381     /*
382      * If rxinit_status is still set, rx_InitHost() has yet to be called
383      * and we therefore do not have any mutex locks initialized.  As a
384      * result we cannot call rxi_MorePackets() without crashing.
385      */
386     if (rxinit_status)
387         return;
388
389     rxi_InitMorePackets();
390 }
391 #endif
392
393 static afs_uint32
394 fudge_netmask(afs_uint32 addr)
395 {
396     afs_uint32 msk;
397
398     if (IN_CLASSA(addr))
399         msk = IN_CLASSA_NET;
400     else if (IN_CLASSB(addr))
401         msk = IN_CLASSB_NET;
402     else if (IN_CLASSC(addr))
403         msk = IN_CLASSC_NET;
404     else
405         msk = 0;
406
407     return msk;
408 }
409
410
411
412 #if !defined(AFS_AIX_ENV) && !defined(AFS_NT40_ENV) && !defined(AFS_LINUX20_ENV) 
413 int
414 rxi_syscall(a3, a4, a5)
415      afs_uint32 a3, a4;
416      void *a5;
417 {
418     afs_uint32 rcode;
419     void (*old) ();
420
421     old = (void (*)())signal(SIGSYS, SIG_IGN);
422
423 #if defined(AFS_SGI_ENV)
424     rcode = afs_syscall(a3, a4, a5);
425 #else
426     rcode = syscall(AFS_SYSCALL, 28 /* AFSCALL_CALL */ , a3, a4, a5);
427 #endif /* AFS_SGI_ENV */
428
429     signal(SIGSYS, old);
430
431     return rcode;
432 }
433 #endif /* AFS_AIX_ENV */
434
435 #ifndef AFS_NT40_ENV
436 void
437 rx_GetIFInfo(void)
438 {
439     int s;
440     int i, j, len, res;
441     struct ifconf ifc;
442     struct ifreq ifs[ADDRSPERSITE];
443     struct ifreq *ifr;
444 #ifdef  AFS_AIX41_ENV
445     char buf[BUFSIZ], *cp, *cplim;
446 #endif
447     struct sockaddr_in *a;
448
449     LOCK_IF_INIT;
450     if (Inited) {
451         UNLOCK_IF_INIT;
452         return;
453     }
454     Inited = 1;
455     UNLOCK_IF_INIT;
456     LOCK_IF;
457     rxi_numNetAddrs = 0;
458     memset(rxi_NetAddrs, 0, sizeof(rxi_NetAddrs));
459     memset(myNetFlags, 0, sizeof(myNetFlags));
460     memset(myNetMTUs, 0, sizeof(myNetMTUs));
461     memset(myNetMasks, 0, sizeof(myNetMasks));
462     UNLOCK_IF;
463     s = socket(AF_INET, SOCK_DGRAM, 0);
464     if (s < 0)
465         return;
466
467 #ifdef  AFS_AIX41_ENV
468     ifc.ifc_len = sizeof(buf);
469     ifc.ifc_buf = buf;
470     ifr = ifc.ifc_req;
471 #else
472     ifc.ifc_len = sizeof(ifs);
473     ifc.ifc_buf = (caddr_t) & ifs[0];
474     memset(&ifs[0], 0, sizeof(ifs));
475 #endif
476     res = ioctl(s, SIOCGIFCONF, &ifc);
477     if (res < 0) {
478         /* fputs(stderr, "ioctl error IFCONF\n"); */
479         close(s);
480         return;
481     }
482
483     LOCK_IF;
484 #ifdef  AFS_AIX41_ENV
485 #define size(p) MAX((p).sa_len, sizeof(p))
486     cplim = buf + ifc.ifc_len;  /*skip over if's with big ifr_addr's */
487     for (cp = buf; cp < cplim;
488          cp += sizeof(ifr->ifr_name) + MAX(a->sin_len, sizeof(*a))) {
489         if (rxi_numNetAddrs >= ADDRSPERSITE)
490             break;
491
492         ifr = (struct ifreq *)cp;
493 #else
494     len = ifc.ifc_len / sizeof(struct ifreq);
495     if (len > ADDRSPERSITE)
496         len = ADDRSPERSITE;
497
498     for (i = 0; i < len; ++i) {
499         ifr = &ifs[i];
500         res = ioctl(s, SIOCGIFADDR, ifr);
501 #endif
502         if (res < 0) {
503             /* fputs(stderr, "ioctl error IFADDR\n");
504              * perror(ifr->ifr_name);   */
505             continue;
506         }
507         a = (struct sockaddr_in *)&ifr->ifr_addr;
508         if (a->sin_family != AF_INET)
509             continue;
510         rxi_NetAddrs[rxi_numNetAddrs] = ntohl(a->sin_addr.s_addr);
511         if (rxi_NetAddrs[rxi_numNetAddrs] == 0x7f000001) {
512             /* we don't really care about "localhost" */
513             continue;
514         }
515         for (j = 0; j < rxi_numNetAddrs; j++) {
516             if (rxi_NetAddrs[j] == rxi_NetAddrs[rxi_numNetAddrs])
517                 break;
518         }
519         if (j < rxi_numNetAddrs)
520             continue;
521
522         /* fprintf(stderr, "if %s addr=%x\n", ifr->ifr_name,
523          * rxi_NetAddrs[rxi_numNetAddrs]); */
524
525 #ifdef SIOCGIFFLAGS
526         res = ioctl(s, SIOCGIFFLAGS, ifr);
527         if (res == 0) {
528             myNetFlags[rxi_numNetAddrs] = ifr->ifr_flags;
529 #ifdef IFF_LOOPBACK
530             /* Handle aliased loopbacks as well. */
531             if (ifr->ifr_flags & IFF_LOOPBACK)
532                 continue;
533 #endif
534             /* fprintf(stderr, "if %s flags=%x\n", 
535              * ifr->ifr_name, ifr->ifr_flags); */
536         } else {                /*
537                                  * fputs(stderr, "ioctl error IFFLAGS\n");
538                                  * perror(ifr->ifr_name); */
539         }
540 #endif /* SIOCGIFFLAGS */
541
542 #if !defined(AFS_AIX_ENV)  && !defined(AFS_LINUX20_ENV)
543         /* this won't run on an AIX system w/o a cache manager */
544         rxi_syscallp = rxi_syscall;
545 #endif
546
547         /* If I refer to kernel extensions that aren't loaded on AIX, the 
548          * program refuses to load and run, so I simply can't include the 
549          * following code.  Fortunately, AIX is the one operating system in
550          * which the subsequent ioctl works reliably. */
551         if (rxi_syscallp) {
552             if ((*rxi_syscallp) (20 /*AFSOP_GETMTU */ ,
553                                  htonl(rxi_NetAddrs[rxi_numNetAddrs]),
554                                  &(myNetMTUs[rxi_numNetAddrs]))) {
555                 /* fputs(stderr, "syscall error GETMTU\n");
556                  * perror(ifr->ifr_name); */
557                 myNetMTUs[rxi_numNetAddrs] = 0;
558             }
559             if ((*rxi_syscallp) (42 /*AFSOP_GETMASK */ ,
560                                  htonl(rxi_NetAddrs[rxi_numNetAddrs]),
561                                  &(myNetMasks[rxi_numNetAddrs]))) {
562                 /* fputs(stderr, "syscall error GETMASK\n");
563                  * perror(ifr->ifr_name); */
564                 myNetMasks[rxi_numNetAddrs] = 0;
565             } else
566                 myNetMasks[rxi_numNetAddrs] =
567                     ntohl(myNetMasks[rxi_numNetAddrs]);
568             /* fprintf(stderr, "if %s mask=0x%x\n", 
569              * ifr->ifr_name, myNetMasks[rxi_numNetAddrs]); */
570         }
571
572         if (myNetMTUs[rxi_numNetAddrs] == 0) {
573             myNetMTUs[rxi_numNetAddrs] = OLD_MAX_PACKET_SIZE + RX_IPUDP_SIZE;
574 #ifdef SIOCGIFMTU
575             res = ioctl(s, SIOCGIFMTU, ifr);
576             if ((res == 0) && (ifr->ifr_metric > 128)) {        /* sanity check */
577                 myNetMTUs[rxi_numNetAddrs] = ifr->ifr_metric;
578                 /* fprintf(stderr, "if %s mtu=%d\n", 
579                  * ifr->ifr_name, ifr->ifr_metric); */
580             } else {
581                 /* fputs(stderr, "ioctl error IFMTU\n");
582                  * perror(ifr->ifr_name); */
583             }
584 #endif
585         }
586
587         if (myNetMasks[rxi_numNetAddrs] == 0) {
588             myNetMasks[rxi_numNetAddrs] =
589                 fudge_netmask(rxi_NetAddrs[rxi_numNetAddrs]);
590 #ifdef SIOCGIFNETMASK
591             res = ioctl(s, SIOCGIFNETMASK, ifr);
592             if ((res == 0)) {
593                 a = (struct sockaddr_in *)&ifr->ifr_addr;
594                 myNetMasks[rxi_numNetAddrs] = ntohl(a->sin_addr.s_addr);
595                 /* fprintf(stderr, "if %s subnetmask=0x%x\n", 
596                  * ifr->ifr_name, myNetMasks[rxi_numNetAddrs]); */
597             } else {
598                 /* fputs(stderr, "ioctl error IFMASK\n");
599                  * perror(ifr->ifr_name); */
600             }
601 #endif
602         }
603
604         if (rxi_NetAddrs[rxi_numNetAddrs] != 0x7f000001) {      /* ignore lo0 */
605             int maxsize;
606             maxsize =
607                 rxi_nRecvFrags * (myNetMTUs[rxi_numNetAddrs] - RX_IP_SIZE);
608             maxsize -= UDP_HDR_SIZE;    /* only the first frag has a UDP hdr */
609             if (rx_maxReceiveSize < maxsize)
610                 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxsize);
611             ++rxi_numNetAddrs;
612         }
613     }
614     UNLOCK_IF;
615     close(s);
616
617     /* have to allocate at least enough to allow a single packet to reach its
618      * maximum size, so ReadPacket will work.  Allocate enough for a couple
619      * of packets to do so, for good measure */
620     {
621         int npackets, ncbufs;
622
623         rx_maxJumboRecvSize =
624             RX_HEADER_SIZE + rxi_nDgramPackets * RX_JUMBOBUFFERSIZE +
625             (rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE;
626         rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
627         ncbufs = (rx_maxJumboRecvSize - RX_FIRSTBUFFERSIZE);
628         if (ncbufs > 0) {
629             ncbufs = ncbufs / RX_CBUFFERSIZE;
630             npackets = rx_initSendWindow - 1;
631             rxi_MorePackets(npackets * (ncbufs + 1));
632         }
633     }
634 }
635 #endif /* AFS_NT40_ENV */
636
637 /* Called from rxi_FindPeer, when initializing a clear rx_peer structure,
638  * to get interesting information.
639  * Curiously enough, the rx_peerHashTable_lock currently protects the
640  * Inited variable (and hence rx_GetIFInfo). When the fs suite uses
641  * pthreads, this issue will need to be revisited.
642  */
643
644 void
645 rxi_InitPeerParams(struct rx_peer *pp)
646 {
647     afs_uint32 ppaddr;
648     u_short rxmtu;
649     int ix;
650 #if defined(ADAPT_PMTU) && defined(IP_MTU)
651     int sock;
652     struct sockaddr_in addr;
653 #endif
654
655     LOCK_IF_INIT;
656     if (!Inited) {
657         UNLOCK_IF_INIT;
658         /*
659          * there's a race here since more than one thread could call
660          * rx_GetIFInfo.  The race stops in rx_GetIFInfo.
661          */
662         rx_GetIFInfo();
663     } else {
664         UNLOCK_IF_INIT;
665     }
666
667 #ifdef ADAPT_MTU
668     /* try to second-guess IP, and identify which link is most likely to
669      * be used for traffic to/from this host. */
670     ppaddr = ntohl(pp->host);
671     
672     pp->ifMTU = 0;
673     pp->timeout.sec = 2;
674     pp->rateFlag = 2;         /* start timing after two full packets */
675     /* I don't initialize these, because I presume they are bzero'd... 
676      * pp->burstSize pp->burst pp->burstWait.sec pp->burstWait.usec
677      * pp->timeout.usec */
678     
679     LOCK_IF;
680     for (ix = 0; ix < rxi_numNetAddrs; ++ix) {
681         if ((rxi_NetAddrs[ix] & myNetMasks[ix]) == (ppaddr & myNetMasks[ix])) {
682 #ifdef IFF_POINTOPOINT
683             if (myNetFlags[ix] & IFF_POINTOPOINT)
684                 pp->timeout.sec = 4;
685 #endif /* IFF_POINTOPOINT */
686             rxmtu = myNetMTUs[ix] - RX_IPUDP_SIZE;
687             if (rxmtu < RX_MIN_PACKET_SIZE)
688                 rxmtu = RX_MIN_PACKET_SIZE;
689             if (pp->ifMTU < rxmtu)
690                 pp->ifMTU = MIN(rx_MyMaxSendSize, rxmtu);
691         }
692     }
693     UNLOCK_IF;
694     if (!pp->ifMTU) {         /* not local */
695         pp->timeout.sec = 3;
696         pp->ifMTU = MIN(rx_MyMaxSendSize, RX_REMOTE_PACKET_SIZE);
697     }
698 #else /* ADAPT_MTU */
699     pp->rateFlag = 2;           /* start timing after two full packets */
700     pp->timeout.sec = 2;
701     pp->ifMTU = MIN(rx_MyMaxSendSize, OLD_MAX_PACKET_SIZE);
702 #endif /* ADAPT_MTU */
703 #if defined(ADAPT_PMTU) && defined(IP_MTU)
704     sock=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
705     if (sock >= 0) {
706       addr.sin_family = AF_INET;
707       addr.sin_addr.s_addr = pp->host;
708       addr.sin_port = pp->port;
709       if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0) {
710         int mtu=0;
711         socklen_t s = sizeof(mtu);
712         if (getsockopt(sock, SOL_IP, IP_MTU, &mtu, &s)== 0) {
713           pp->ifMTU = MIN(mtu - RX_IPUDP_SIZE, pp->ifMTU);
714         }
715       }
716       close(sock);
717     }
718 #endif
719     pp->ifMTU = rxi_AdjustIfMTU(pp->ifMTU);
720     pp->maxMTU = OLD_MAX_PACKET_SIZE;   /* for compatibility with old guys */
721     pp->natMTU = MIN((int)pp->ifMTU, OLD_MAX_PACKET_SIZE);
722     pp->maxDgramPackets =
723         MIN(rxi_nDgramPackets,
724             rxi_AdjustDgramPackets(rxi_nSendFrags, pp->ifMTU));
725     pp->ifDgramPackets =
726         MIN(rxi_nDgramPackets,
727             rxi_AdjustDgramPackets(rxi_nSendFrags, pp->ifMTU));
728     pp->maxDgramPackets = 1;
729     /* Initialize slow start parameters */
730     pp->MTU = MIN(pp->natMTU, pp->maxMTU);
731     pp->cwind = 1;
732     pp->nDgramPackets = 1;
733     pp->congestSeq = 0;
734 }
735
736 /* Don't expose jumobgram internals. */
737 void
738 rx_SetNoJumbo(void)
739 {
740     rx_maxReceiveSize = OLD_MAX_PACKET_SIZE;
741     rxi_nSendFrags = rxi_nRecvFrags = 1;
742 }
743
744 /* Override max MTU.  If rx_SetNoJumbo is called, it must be 
745    called before calling rx_SetMaxMTU since SetNoJumbo clobbers rx_maxReceiveSize */
746 void
747 rx_SetMaxMTU(int mtu)
748 {
749     rx_MyMaxSendSize = rx_maxReceiveSizeUser = rx_maxReceiveSize = mtu;
750 }
751
752 #if defined(HAVE_LINUX_ERRQUEUE_H) && defined(ADAPT_PMTU)
753 int
754 rxi_HandleSocketError(int socket)
755 {
756     struct msghdr msg;
757     struct cmsghdr *cmsg;
758     struct sock_extended_err *err;
759     struct sockaddr_in addr;
760     struct sockaddr *offender;
761     char controlmsgbuf[256];
762     int ret=0;
763     int code;
764
765     msg.msg_name = &addr;
766     msg.msg_namelen = sizeof(addr);
767     msg.msg_iov = NULL;
768     msg.msg_iovlen = 0;
769     msg.msg_control = controlmsgbuf;
770     msg.msg_controllen = 256;
771     msg.msg_flags = 0;
772     code = recvmsg(socket, &msg, MSG_ERRQUEUE|MSG_DONTWAIT|MSG_TRUNC);
773
774     if (code < 0 || !(msg.msg_flags & MSG_ERRQUEUE))
775         goto out;
776
777     for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
778        if ((char *)cmsg - controlmsgbuf > msg.msg_controllen - CMSG_SPACE(0) ||
779            (char *)cmsg - controlmsgbuf > msg.msg_controllen - CMSG_SPACE(cmsg->cmsg_len) ||
780            cmsg->cmsg_len == 0) {
781            cmsg = 0;
782            break;
783         }
784         if (cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_RECVERR)
785             break;
786     }
787     if (!cmsg)
788         goto out;
789     ret=1;
790     err =(struct sock_extended_err *) CMSG_DATA(cmsg);
791     
792     if (err->ee_errno == EMSGSIZE && err->ee_info >= 68) {
793         rxi_SetPeerMtu(addr.sin_addr.s_addr, addr.sin_port,
794                        err->ee_info - RX_IPUDP_SIZE);
795     }
796     /* other DEST_UNREACH's and TIME_EXCEEDED should be dealt with too */
797     
798 out:
799     return ret;
800 }
801 #endif