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