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