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