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