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