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