2 * Copyright 2000, International Business Machines Corporation and others.
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
10 /* rx_user.c contains routines specific to the user space UNIX implementation of rx */
12 #include <afsconfig.h>
13 #include <afs/param.h>
18 # include <sys/types.h>
22 # include <WINNT/syscfg.h>
24 # include <sys/socket.h>
25 # include <sys/file.h>
27 # include <sys/stat.h>
28 # include <netinet/in.h>
29 # include <sys/time.h>
31 # include <sys/ioctl.h>
34 #if !defined(AFS_AIX_ENV) && !defined(AFS_NT40_ENV) && !defined(AFS_DJGPP_ENV)
35 # include <sys/syscall.h>
37 #include <afs/afs_args.h>
38 #include <afs/afsutil.h>
47 #ifndef IPPORT_USERRESERVED
48 /* If in.h doesn't define this, define it anyway. Unfortunately, defining
49 this doesn't put the code into the kernel to restrict kernel assigned
50 port numbers to numbers below IPPORT_USERRESERVED... */
51 #define IPPORT_USERRESERVED 5000
55 # include <sys/time.h>
58 # include "rx_globals.h"
60 #ifdef AFS_PTHREAD_ENV
64 * The rx_if_init_mutex mutex protects the following global variables:
68 pthread_mutex_t rx_if_init_mutex;
69 #define LOCK_IF_INIT assert(pthread_mutex_lock(&rx_if_init_mutex)==0)
70 #define UNLOCK_IF_INIT assert(pthread_mutex_unlock(&rx_if_init_mutex)==0)
73 * The rx_if_mutex mutex protects the following global variables:
79 pthread_mutex_t rx_if_mutex;
80 #define LOCK_IF assert(pthread_mutex_lock(&rx_if_mutex)==0)
81 #define UNLOCK_IF assert(pthread_mutex_unlock(&rx_if_mutex)==0)
84 #define UNLOCK_IF_INIT
87 #endif /* AFS_PTHREAD_ENV */
91 * Make a socket for receiving/sending IP packets. Set it into non-blocking
92 * and large buffering modes. If port isn't specified, the kernel will pick
93 * one. Returns the socket (>= 0) on success. Returns OSI_NULLSOCKET on
94 * failure. Port must be in network byte order.
97 rxi_GetHostUDPSocket(u_int ahost, u_short port)
100 osi_socket socketFd = OSI_NULLSOCKET;
101 struct sockaddr_in taddr;
102 char *name = "rxi_GetUDPSocket: ";
103 #ifdef AFS_LINUX22_ENV
104 int pmtu=IP_PMTUDISC_DONT;
107 #if !defined(AFS_NT40_ENV) && !defined(AFS_DJGPP_ENV)
108 if (ntohs(port) >= IPPORT_RESERVED && ntohs(port) < IPPORT_USERRESERVED) {
109 /* (osi_Msg "%s*WARNING* port number %d is not a reserved port number. Use port numbers above %d\n", name, port, IPPORT_USERRESERVED);
112 if (ntohs(port) > 0 && ntohs(port) < IPPORT_RESERVED && geteuid() != 0) {
114 "%sport number %d is a reserved port number which may only be used by root. Use port numbers above %d\n",
115 name, ntohs(port), IPPORT_USERRESERVED);
119 socketFd = socket(AF_INET, SOCK_DGRAM, 0);
126 taddr.sin_addr.s_addr = ahost;
127 taddr.sin_family = AF_INET;
128 taddr.sin_port = (u_short) port;
129 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
130 taddr.sin_len = sizeof(struct sockaddr_in);
132 #define MAX_RX_BINDS 10
133 for (binds = 0; binds < MAX_RX_BINDS; binds++) {
136 code = bind(socketFd, (struct sockaddr *)&taddr, sizeof(taddr));
142 (osi_Msg "%sbind failed\n", name);
145 #if !defined(AFS_NT40_ENV) && !defined(AFS_DJGPP_ENV)
147 * Set close-on-exec on rx socket
149 fcntl(socketFd, F_SETFD, 1);
152 #ifndef AFS_DJGPP_ENV
153 /* Use one of three different ways of getting a socket buffer expanded to
161 len2 = rx_UdpBufSize;
164 (socketFd, SOL_SOCKET, SO_RCVBUF, (char *)&len2,
167 len2 = 32766; /* fall back to old size... uh-oh! */
172 (socketFd, SOL_SOCKET, SO_SNDBUF, (char *)&len1,
176 (socketFd, SOL_SOCKET, SO_RCVBUF, (char *)&len2,
179 (osi_Msg "%s*WARNING* Unable to increase buffering on socket\n",
181 MUTEX_ENTER(&rx_stats_mutex);
182 rx_stats.socketGreedy = greedy;
183 MUTEX_EXIT(&rx_stats_mutex);
185 #endif /* AFS_DJGPP_ENV */
187 #ifdef AFS_LINUX22_ENV
188 setsockopt(socketFd, SOL_IP, IP_MTU_DISCOVER, &pmtu, sizeof(pmtu));
191 if (rxi_Listen(socketFd) < 0) {
200 closesocket(socketFd);
206 return OSI_NULLSOCKET;
210 rxi_GetUDPSocket(u_short port)
212 return rxi_GetHostUDPSocket(htonl(INADDR_ANY), port);
216 osi_Panic(msg, a1, a2, a3)
219 (osi_Msg "Fatal Rx error: ");
220 (osi_Msg msg, a1, a2, a3);
227 * osi_AssertFailU() -- used by the osi_Assert() macro.
231 osi_AssertFailU(const char *expr, const char *file, int line)
233 osi_Panic("assertion failed: %s, file: %s, line: %d\n", expr,
237 #if defined(AFS_AIX32_ENV) && !defined(KERNEL)
239 static const char memZero;
241 osi_Alloc(afs_int32 x)
244 * 0-length allocs may return NULL ptr from malloc, so we special-case
245 * things so that NULL returned iff an error occurred
248 return (void *)&memZero;
253 osi_Free(void *x, afs_int32 size)
260 #endif /* defined(AFS_AIX32_ENV) && !defined(KERNEL) */
262 #define ADDRSPERSITE 16
265 static afs_uint32 rxi_NetAddrs[ADDRSPERSITE]; /* host order */
266 static int myNetMTUs[ADDRSPERSITE];
267 static int myNetMasks[ADDRSPERSITE];
268 static int myNetFlags[ADDRSPERSITE];
269 static u_int rxi_numNetAddrs;
270 static int Inited = 0;
272 #if defined(AFS_NT40_ENV) || defined(AFS_DJGPP_ENV)
276 /* The IP address list can change so we must query for it */
279 /* we don't want to use the loopback adapter which is first */
280 /* this is a bad bad hack */
281 if (rxi_numNetAddrs > 1)
282 return htonl(rxi_NetAddrs[1]);
283 else if (rxi_numNetAddrs > 0)
284 return htonl(rxi_NetAddrs[0]);
290 ** return number of addresses
291 ** and the addresses themselves in the buffer
292 ** maxSize - max number of interfaces to return.
295 rx_getAllAddr(afs_int32 * buffer, int maxSize)
297 int count = 0, offset = 0;
299 /* The IP address list can change so we must query for it */
303 /* we don't want to use the loopback adapter which is first */
304 /* this is a bad bad hack.
305 * and doesn't hold true on Windows.
307 if ( rxi_numNetAddrs > 1 )
309 #endif /* AFS_NT40_ENV */
311 for (count = 0; offset < rxi_numNetAddrs && maxSize > 0;
312 count++, offset++, maxSize--)
313 buffer[count] = htonl(rxi_NetAddrs[offset]);
325 int npackets, ncbufs;
333 rxi_numNetAddrs = ADDRSPERSITE;
334 (void)syscfg_GetIFInfo(&rxi_numNetAddrs, rxi_NetAddrs,
335 myNetMasks, myNetMTUs, myNetFlags);
337 for (i = 0; i < rxi_numNetAddrs; i++) {
338 rxsize = rxi_AdjustIfMTU(myNetMTUs[i] - RX_IPUDP_SIZE);
340 rxi_nRecvFrags * rxsize + (rxi_nRecvFrags - 1) * UDP_HDR_SIZE;
341 maxsize = rxi_AdjustMaxMTU(rxsize, maxsize);
342 if (rx_maxReceiveSize < maxsize) {
343 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxsize);
345 MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
350 ncbufs = (rx_maxJumboRecvSize - RX_FIRSTBUFFERSIZE);
352 ncbufs = ncbufs / RX_CBUFFERSIZE;
353 npackets = rx_initSendWindow - 1;
354 rxi_MorePackets(npackets * (ncbufs + 1));
360 fudge_netmask(afs_uint32 addr)
366 else if (IN_CLASSB(addr))
368 else if (IN_CLASSC(addr))
378 #if !defined(AFS_AIX_ENV) && !defined(AFS_NT40_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_DJGPP_ENV)
380 rxi_syscall(a3, a4, a5)
387 old = (void (*)())signal(SIGSYS, SIG_IGN);
389 #if defined(AFS_SGI_ENV)
390 rcode = afs_syscall(a3, a4, a5);
392 rcode = syscall(AFS_SYSCALL, 28 /* AFSCALL_CALL */ , a3, a4, a5);
393 #endif /* AFS_SGI_ENV */
399 #endif /* AFS_AIX_ENV */
407 #ifndef AFS_DJGPP_ENV
409 struct ifreq ifs[ADDRSPERSITE];
412 char buf[BUFSIZ], *cp, *cplim;
414 struct sockaddr_in *a;
415 #endif /* AFS_DJGPP_ENV */
426 memset(rxi_NetAddrs, 0, sizeof(rxi_NetAddrs));
427 memset(myNetFlags, 0, sizeof(myNetFlags));
428 memset(myNetMTUs, 0, sizeof(myNetMTUs));
429 memset(myNetMasks, 0, sizeof(myNetMasks));
431 s = socket(AF_INET, SOCK_DGRAM, 0);
435 #ifndef AFS_DJGPP_ENV
437 ifc.ifc_len = sizeof(buf);
441 ifc.ifc_len = sizeof(ifs);
442 ifc.ifc_buf = (caddr_t) & ifs[0];
443 memset(&ifs[0], 0, sizeof(ifs));
445 res = ioctl(s, SIOCGIFCONF, &ifc);
447 /* fputs(stderr, "ioctl error IFCONF\n"); */
454 #define size(p) MAX((p).sa_len, sizeof(p))
455 cplim = buf + ifc.ifc_len; /*skip over if's with big ifr_addr's */
456 for (cp = buf; cp < cplim;
457 cp += sizeof(ifr->ifr_name) + MAX(a->sin_len, sizeof(*a))) {
458 if (rxi_numNetAddrs >= ADDRSPERSITE)
461 ifr = (struct ifreq *)cp;
463 len = ifc.ifc_len / sizeof(struct ifreq);
464 if (len > ADDRSPERSITE)
467 for (i = 0; i < len; ++i) {
469 res = ioctl(s, SIOCGIFADDR, ifr);
472 /* fputs(stderr, "ioctl error IFADDR\n");
473 * perror(ifr->ifr_name); */
476 a = (struct sockaddr_in *)&ifr->ifr_addr;
477 if (a->sin_family != AF_INET)
479 rxi_NetAddrs[rxi_numNetAddrs] = ntohl(a->sin_addr.s_addr);
480 if (rxi_NetAddrs[rxi_numNetAddrs] == 0x7f000001) {
481 /* we don't really care about "localhost" */
484 for (j = 0; j < rxi_numNetAddrs; j++) {
485 if (rxi_NetAddrs[j] == rxi_NetAddrs[rxi_numNetAddrs])
488 if (j < rxi_numNetAddrs)
491 /* fprintf(stderr, "if %s addr=%x\n", ifr->ifr_name,
492 * rxi_NetAddrs[rxi_numNetAddrs]); */
495 res = ioctl(s, SIOCGIFFLAGS, ifr);
497 myNetFlags[rxi_numNetAddrs] = ifr->ifr_flags;
499 /* Handle aliased loopbacks as well. */
500 if (ifr->ifr_flags & IFF_LOOPBACK)
503 /* fprintf(stderr, "if %s flags=%x\n",
504 * ifr->ifr_name, ifr->ifr_flags); */
506 * fputs(stderr, "ioctl error IFFLAGS\n");
507 * perror(ifr->ifr_name); */
509 #endif /* SIOCGIFFLAGS */
511 #if !defined(AFS_AIX_ENV) && !defined(AFS_LINUX20_ENV)
512 /* this won't run on an AIX system w/o a cache manager */
513 rxi_syscallp = rxi_syscall;
516 /* If I refer to kernel extensions that aren't loaded on AIX, the
517 * program refuses to load and run, so I simply can't include the
518 * following code. Fortunately, AIX is the one operating system in
519 * which the subsequent ioctl works reliably. */
521 if ((*rxi_syscallp) (20 /*AFSOP_GETMTU */ ,
522 htonl(rxi_NetAddrs[rxi_numNetAddrs]),
523 &(myNetMTUs[rxi_numNetAddrs]))) {
524 /* fputs(stderr, "syscall error GETMTU\n");
525 * perror(ifr->ifr_name); */
526 myNetMTUs[rxi_numNetAddrs] = 0;
528 if ((*rxi_syscallp) (42 /*AFSOP_GETMASK */ ,
529 htonl(rxi_NetAddrs[rxi_numNetAddrs]),
530 &(myNetMasks[rxi_numNetAddrs]))) {
531 /* fputs(stderr, "syscall error GETMASK\n");
532 * perror(ifr->ifr_name); */
533 myNetMasks[rxi_numNetAddrs] = 0;
535 myNetMasks[rxi_numNetAddrs] =
536 ntohl(myNetMasks[rxi_numNetAddrs]);
537 /* fprintf(stderr, "if %s mask=0x%x\n",
538 * ifr->ifr_name, myNetMasks[rxi_numNetAddrs]); */
541 if (myNetMTUs[rxi_numNetAddrs] == 0) {
542 myNetMTUs[rxi_numNetAddrs] = OLD_MAX_PACKET_SIZE + RX_IPUDP_SIZE;
544 res = ioctl(s, SIOCGIFMTU, ifr);
545 if ((res == 0) && (ifr->ifr_metric > 128)) { /* sanity check */
546 myNetMTUs[rxi_numNetAddrs] = ifr->ifr_metric;
547 /* fprintf(stderr, "if %s mtu=%d\n",
548 * ifr->ifr_name, ifr->ifr_metric); */
550 /* fputs(stderr, "ioctl error IFMTU\n");
551 * perror(ifr->ifr_name); */
556 if (myNetMasks[rxi_numNetAddrs] == 0) {
557 myNetMasks[rxi_numNetAddrs] =
558 fudge_netmask(rxi_NetAddrs[rxi_numNetAddrs]);
559 #ifdef SIOCGIFNETMASK
560 res = ioctl(s, SIOCGIFNETMASK, ifr);
562 a = (struct sockaddr_in *)&ifr->ifr_addr;
563 myNetMasks[rxi_numNetAddrs] = ntohl(a->sin_addr.s_addr);
564 /* fprintf(stderr, "if %s subnetmask=0x%x\n",
565 * ifr->ifr_name, myNetMasks[rxi_numNetAddrs]); */
567 /* fputs(stderr, "ioctl error IFMASK\n");
568 * perror(ifr->ifr_name); */
573 if (rxi_NetAddrs[rxi_numNetAddrs] != 0x7f000001) { /* ignore lo0 */
576 rxi_nRecvFrags * (myNetMTUs[rxi_numNetAddrs] - RX_IP_SIZE);
577 maxsize -= UDP_HDR_SIZE; /* only the first frag has a UDP hdr */
578 if (rx_maxReceiveSize < maxsize)
579 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxsize);
586 /* have to allocate at least enough to allow a single packet to reach its
587 * maximum size, so ReadPacket will work. Allocate enough for a couple
588 * of packets to do so, for good measure */
590 int npackets, ncbufs;
592 rx_maxJumboRecvSize =
593 RX_HEADER_SIZE + rxi_nDgramPackets * RX_JUMBOBUFFERSIZE +
594 (rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE;
595 rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
596 ncbufs = (rx_maxJumboRecvSize - RX_FIRSTBUFFERSIZE);
598 ncbufs = ncbufs / RX_CBUFFERSIZE;
599 npackets = rx_initSendWindow - 1;
600 rxi_MorePackets(npackets * (ncbufs + 1));
603 #else /* AFS_DJGPP_ENV */
606 #endif /* AFS_DJGPP_ENV */
608 #endif /* AFS_NT40_ENV */
610 /* Called from rxi_FindPeer, when initializing a clear rx_peer structure,
611 * to get interesting information.
612 * Curiously enough, the rx_peerHashTable_lock currently protects the
613 * Inited variable (and hence rx_GetIFInfo). When the fs suite uses
614 * pthreads, this issue will need to be revisited.
618 rxi_InitPeerParams(struct rx_peer *pp)
630 * there's a race here since more than one thread could call
631 * rx_GetIFInfo. The race stops in rx_GetIFInfo.
639 /* try to second-guess IP, and identify which link is most likely to
640 * be used for traffic to/from this host. */
641 ppaddr = ntohl(pp->host);
645 pp->rateFlag = 2; /* start timing after two full packets */
646 /* I don't initialize these, because I presume they are bzero'd...
647 * pp->burstSize pp->burst pp->burstWait.sec pp->burstWait.usec
648 * pp->timeout.usec */
651 for (ix = 0; ix < rxi_numNetAddrs; ++ix) {
652 if ((rxi_NetAddrs[ix] & myNetMasks[ix]) == (ppaddr & myNetMasks[ix])) {
653 #ifdef IFF_POINTOPOINT
654 if (myNetFlags[ix] & IFF_POINTOPOINT)
656 #endif /* IFF_POINTOPOINT */
657 rxmtu = myNetMTUs[ix] - RX_IPUDP_SIZE;
658 if (rxmtu < RX_MIN_PACKET_SIZE)
659 rxmtu = RX_MIN_PACKET_SIZE;
660 if (pp->ifMTU < rxmtu)
661 pp->ifMTU = MIN(rx_MyMaxSendSize, rxmtu);
665 if (!pp->ifMTU) { /* not local */
667 pp->ifMTU = MIN(rx_MyMaxSendSize, RX_REMOTE_PACKET_SIZE);
669 #else /* ADAPT_MTU */
670 pp->rateFlag = 2; /* start timing after two full packets */
672 pp->ifMTU = MIN(rx_MyMaxSendSize, OLD_MAX_PACKET_SIZE);
673 #endif /* ADAPT_MTU */
674 pp->ifMTU = rxi_AdjustIfMTU(pp->ifMTU);
675 pp->maxMTU = OLD_MAX_PACKET_SIZE; /* for compatibility with old guys */
676 pp->natMTU = MIN((int)pp->ifMTU, OLD_MAX_PACKET_SIZE);
677 pp->maxDgramPackets =
678 MIN(rxi_nDgramPackets,
679 rxi_AdjustDgramPackets(RX_MAX_FRAGS, pp->ifMTU));
681 MIN(rxi_nDgramPackets,
682 rxi_AdjustDgramPackets(RX_MAX_FRAGS, pp->ifMTU));
683 pp->maxDgramPackets = 1;
684 /* Initialize slow start parameters */
685 pp->MTU = MIN(pp->natMTU, pp->maxMTU);
687 pp->nDgramPackets = 1;
691 /* Don't expose jumobgram internals. */
695 rx_maxReceiveSize = OLD_MAX_PACKET_SIZE;
696 rxi_nSendFrags = rxi_nRecvFrags = 1;
699 /* Override max MTU. If rx_SetNoJumbo is called, it must be
700 called before calling rx_SetMaxMTU since SetNoJumbo clobbers rx_maxReceiveSize */
702 rx_SetMaxMTU(int mtu)
704 rx_MyMaxSendSize = rx_maxReceiveSizeUser = rx_maxReceiveSize = mtu;