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>
23 # include <WINNT/syscfg.h>
25 # include <sys/socket.h>
26 # include <sys/file.h>
28 # include <sys/stat.h>
29 # include <netinet/in.h>
30 # include <sys/time.h>
32 # include <sys/ioctl.h>
35 #if !defined(AFS_AIX_ENV) && !defined(AFS_NT40_ENV) && !defined(AFS_DJGPP_ENV)
36 # include <sys/syscall.h>
38 #include <afs/afs_args.h>
39 #include <afs/afsutil.h>
41 #ifndef IPPORT_USERRESERVED
42 /* If in.h doesn't define this, define it anyway. Unfortunately, defining
43 this doesn't put the code into the kernel to restrict kernel assigned
44 port numbers to numbers below IPPORT_USERRESERVED... */
45 #define IPPORT_USERRESERVED 5000
49 # include <sys/time.h>
52 # include "rx_globals.h"
54 #ifdef AFS_PTHREAD_ENV
58 * The rx_if_init_mutex mutex protects the following global variables:
62 pthread_mutex_t rx_if_init_mutex;
63 #define LOCK_IF_INIT assert(pthread_mutex_lock(&rx_if_init_mutex)==0)
64 #define UNLOCK_IF_INIT assert(pthread_mutex_unlock(&rx_if_init_mutex)==0)
67 * The rx_if_mutex mutex protects the following global variables:
73 pthread_mutex_t rx_if_mutex;
74 #define LOCK_IF assert(pthread_mutex_lock(&rx_if_mutex)==0)
75 #define UNLOCK_IF assert(pthread_mutex_unlock(&rx_if_mutex)==0)
78 #define UNLOCK_IF_INIT
81 #endif /* AFS_PTHREAD_ENV */
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.
91 rxi_GetHostUDPSocket(u_int ahost, u_short port)
94 osi_socket socketFd = OSI_NULLSOCKET;
95 struct sockaddr_in taddr;
96 char *name = "rxi_GetUDPSocket: ";
97 #ifdef AFS_LINUX22_ENV
98 int pmtu=IP_PMTUDISC_DONT;
101 #if !defined(AFS_NT40_ENV) && !defined(AFS_DJGPP_ENV)
102 if (ntohs(port) >= IPPORT_RESERVED && ntohs(port) < IPPORT_USERRESERVED) {
103 /* (osi_Msg "%s*WARNING* port number %d is not a reserved port number. Use port numbers above %d\n", name, port, IPPORT_USERRESERVED);
106 if (ntohs(port) > 0 && ntohs(port) < IPPORT_RESERVED && geteuid() != 0) {
108 "%sport number %d is a reserved port number which may only be used by root. Use port numbers above %d\n",
109 name, ntohs(port), IPPORT_USERRESERVED);
113 socketFd = socket(AF_INET, SOCK_DGRAM, 0);
120 taddr.sin_addr.s_addr = ahost;
121 taddr.sin_family = AF_INET;
122 taddr.sin_port = (u_short) port;
123 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
124 taddr.sin_len = sizeof(struct sockaddr_in);
126 #define MAX_RX_BINDS 10
127 for (binds = 0; binds < MAX_RX_BINDS; binds++) {
130 code = bind(socketFd, (struct sockaddr *)&taddr, sizeof(taddr));
136 (osi_Msg "%sbind failed\n", name);
139 #if !defined(AFS_NT40_ENV) && !defined(AFS_DJGPP_ENV)
141 * Set close-on-exec on rx socket
143 fcntl(socketFd, F_SETFD, 1);
146 #ifndef AFS_DJGPP_ENV
147 /* Use one of three different ways of getting a socket buffer expanded to
155 len2 = rx_UdpBufSize;
158 (socketFd, SOL_SOCKET, SO_RCVBUF, (char *)&len2,
161 len2 = 32766; /* fall back to old size... uh-oh! */
166 (socketFd, SOL_SOCKET, SO_SNDBUF, (char *)&len1,
170 (socketFd, SOL_SOCKET, SO_RCVBUF, (char *)&len2,
173 (osi_Msg "%s*WARNING* Unable to increase buffering on socket\n",
175 MUTEX_ENTER(&rx_stats_mutex);
176 rx_stats.socketGreedy = greedy;
177 MUTEX_EXIT(&rx_stats_mutex);
179 #endif /* AFS_DJGPP_ENV */
181 #ifdef AFS_LINUX22_ENV
182 setsockopt(socketFd, SOL_IP, IP_MTU_DISCOVER, &pmtu, sizeof(pmtu));
185 if (rxi_Listen(socketFd) < 0) {
194 closesocket(socketFd);
200 return OSI_NULLSOCKET;
204 rxi_GetUDPSocket(u_short port)
206 return rxi_GetHostUDPSocket(htonl(INADDR_ANY), port);
210 osi_Panic(msg, a1, a2, a3)
213 (osi_Msg "Fatal Rx error: ");
214 (osi_Msg msg, a1, a2, a3);
221 * osi_AssertFailU() -- used by the osi_Assert() macro.
225 osi_AssertFailU(const char *expr, const char *file, int line)
227 osi_Panic("assertion failed: %s, file: %s, line: %d\n", expr,
231 #if defined(AFS_AIX32_ENV) && !defined(KERNEL)
233 static const char memZero;
235 osi_Alloc(afs_int32 x)
238 * 0-length allocs may return NULL ptr from malloc, so we special-case
239 * things so that NULL returned iff an error occurred
242 return (void *)&memZero;
247 osi_Free(void *x, afs_int32 size)
254 #endif /* defined(AFS_AIX32_ENV) && !defined(KERNEL) */
256 #define ADDRSPERSITE 16
259 static afs_uint32 rxi_NetAddrs[ADDRSPERSITE]; /* host order */
260 static int myNetMTUs[ADDRSPERSITE];
261 static int myNetMasks[ADDRSPERSITE];
262 static int myNetFlags[ADDRSPERSITE];
263 static u_int rxi_numNetAddrs;
264 static int Inited = 0;
266 #if defined(AFS_NT40_ENV) || defined(AFS_DJGPP_ENV)
270 /* The IP address list can change so we must query for it */
273 /* we don't want to use the loopback adapter which is first */
274 /* this is a bad bad hack */
275 if (rxi_numNetAddrs > 1)
276 return htonl(rxi_NetAddrs[1]);
277 else if (rxi_numNetAddrs > 0)
278 return htonl(rxi_NetAddrs[0]);
284 ** return number of addresses
285 ** and the addresses themselves in the buffer
286 ** maxSize - max number of interfaces to return.
289 rx_getAllAddr(afs_int32 * buffer, int maxSize)
291 int count = 0, offset = 0;
293 /* The IP address list can change so we must query for it */
297 /* we don't want to use the loopback adapter which is first */
298 /* this is a bad bad hack.
299 * and doesn't hold true on Windows.
301 if ( rxi_numNetAddrs > 1 )
303 #endif /* AFS_NT40_ENV */
305 for (count = 0; offset < rxi_numNetAddrs && maxSize > 0;
306 count++, offset++, maxSize--)
307 buffer[count] = htonl(rxi_NetAddrs[offset]);
319 int npackets, ncbufs;
327 rxi_numNetAddrs = ADDRSPERSITE;
328 (void)syscfg_GetIFInfo(&rxi_numNetAddrs, rxi_NetAddrs,
329 myNetMasks, myNetMTUs, myNetFlags);
331 for (i = 0; i < rxi_numNetAddrs; i++) {
332 rxsize = rxi_AdjustIfMTU(myNetMTUs[i] - RX_IPUDP_SIZE);
334 rxi_nRecvFrags * rxsize + (rxi_nRecvFrags - 1) * UDP_HDR_SIZE;
335 maxsize = rxi_AdjustMaxMTU(rxsize, maxsize);
336 if (rx_maxReceiveSize < maxsize) {
337 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxsize);
339 MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
344 ncbufs = (rx_maxJumboRecvSize - RX_FIRSTBUFFERSIZE);
346 ncbufs = ncbufs / RX_CBUFFERSIZE;
347 npackets = rx_initSendWindow - 1;
348 rxi_MorePackets(npackets * (ncbufs + 1));
354 fudge_netmask(afs_uint32 addr)
360 else if (IN_CLASSB(addr))
362 else if (IN_CLASSC(addr))
372 #if !defined(AFS_AIX_ENV) && !defined(AFS_NT40_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_DJGPP_ENV)
374 rxi_syscall(a3, a4, a5)
381 old = (void (*)())signal(SIGSYS, SIG_IGN);
383 #if defined(AFS_SGI_ENV)
384 rcode = afs_syscall(a3, a4, a5);
386 rcode = syscall(AFS_SYSCALL, 28 /* AFSCALL_CALL */ , a3, a4, a5);
387 #endif /* AFS_SGI_ENV */
393 #endif /* AFS_AIX_ENV */
401 #ifndef AFS_DJGPP_ENV
403 struct ifreq ifs[ADDRSPERSITE];
406 char buf[BUFSIZ], *cp, *cplim;
408 struct sockaddr_in *a;
409 #endif /* AFS_DJGPP_ENV */
420 memset(rxi_NetAddrs, 0, sizeof(rxi_NetAddrs));
421 memset(myNetFlags, 0, sizeof(myNetFlags));
422 memset(myNetMTUs, 0, sizeof(myNetMTUs));
423 memset(myNetMasks, 0, sizeof(myNetMasks));
425 s = socket(AF_INET, SOCK_DGRAM, 0);
429 #ifndef AFS_DJGPP_ENV
431 ifc.ifc_len = sizeof(buf);
435 ifc.ifc_len = sizeof(ifs);
436 ifc.ifc_buf = (caddr_t) & ifs[0];
437 memset(&ifs[0], 0, sizeof(ifs));
439 res = ioctl(s, SIOCGIFCONF, &ifc);
441 /* fputs(stderr, "ioctl error IFCONF\n"); */
448 #define size(p) MAX((p).sa_len, sizeof(p))
449 cplim = buf + ifc.ifc_len; /*skip over if's with big ifr_addr's */
450 for (cp = buf; cp < cplim;
451 cp += sizeof(ifr->ifr_name) + MAX(a->sin_len, sizeof(*a))) {
452 if (rxi_numNetAddrs >= ADDRSPERSITE)
455 ifr = (struct ifreq *)cp;
457 len = ifc.ifc_len / sizeof(struct ifreq);
458 if (len > ADDRSPERSITE)
461 for (i = 0; i < len; ++i) {
463 res = ioctl(s, SIOCGIFADDR, ifr);
466 /* fputs(stderr, "ioctl error IFADDR\n");
467 * perror(ifr->ifr_name); */
470 a = (struct sockaddr_in *)&ifr->ifr_addr;
471 if (a->sin_family != AF_INET)
473 rxi_NetAddrs[rxi_numNetAddrs] = ntohl(a->sin_addr.s_addr);
474 if (rxi_NetAddrs[rxi_numNetAddrs] == 0x7f000001) {
475 /* we don't really care about "localhost" */
478 for (j = 0; j < rxi_numNetAddrs; j++) {
479 if (rxi_NetAddrs[j] == rxi_NetAddrs[rxi_numNetAddrs])
482 if (j < rxi_numNetAddrs)
485 /* fprintf(stderr, "if %s addr=%x\n", ifr->ifr_name,
486 * rxi_NetAddrs[rxi_numNetAddrs]); */
489 res = ioctl(s, SIOCGIFFLAGS, ifr);
491 myNetFlags[rxi_numNetAddrs] = ifr->ifr_flags;
493 /* Handle aliased loopbacks as well. */
494 if (ifr->ifr_flags & IFF_LOOPBACK)
497 /* fprintf(stderr, "if %s flags=%x\n",
498 * ifr->ifr_name, ifr->ifr_flags); */
500 * fputs(stderr, "ioctl error IFFLAGS\n");
501 * perror(ifr->ifr_name); */
503 #endif /* SIOCGIFFLAGS */
505 #if !defined(AFS_AIX_ENV) && !defined(AFS_LINUX20_ENV)
506 /* this won't run on an AIX system w/o a cache manager */
507 rxi_syscallp = rxi_syscall;
510 /* If I refer to kernel extensions that aren't loaded on AIX, the
511 * program refuses to load and run, so I simply can't include the
512 * following code. Fortunately, AIX is the one operating system in
513 * which the subsequent ioctl works reliably. */
515 if ((*rxi_syscallp) (20 /*AFSOP_GETMTU */ ,
516 htonl(rxi_NetAddrs[rxi_numNetAddrs]),
517 &(myNetMTUs[rxi_numNetAddrs]))) {
518 /* fputs(stderr, "syscall error GETMTU\n");
519 * perror(ifr->ifr_name); */
520 myNetMTUs[rxi_numNetAddrs] = 0;
522 if ((*rxi_syscallp) (42 /*AFSOP_GETMASK */ ,
523 htonl(rxi_NetAddrs[rxi_numNetAddrs]),
524 &(myNetMasks[rxi_numNetAddrs]))) {
525 /* fputs(stderr, "syscall error GETMASK\n");
526 * perror(ifr->ifr_name); */
527 myNetMasks[rxi_numNetAddrs] = 0;
529 myNetMasks[rxi_numNetAddrs] =
530 ntohl(myNetMasks[rxi_numNetAddrs]);
531 /* fprintf(stderr, "if %s mask=0x%x\n",
532 * ifr->ifr_name, myNetMasks[rxi_numNetAddrs]); */
535 if (myNetMTUs[rxi_numNetAddrs] == 0) {
536 myNetMTUs[rxi_numNetAddrs] = OLD_MAX_PACKET_SIZE + RX_IPUDP_SIZE;
538 res = ioctl(s, SIOCGIFMTU, ifr);
539 if ((res == 0) && (ifr->ifr_metric > 128)) { /* sanity check */
540 myNetMTUs[rxi_numNetAddrs] = ifr->ifr_metric;
541 /* fprintf(stderr, "if %s mtu=%d\n",
542 * ifr->ifr_name, ifr->ifr_metric); */
544 /* fputs(stderr, "ioctl error IFMTU\n");
545 * perror(ifr->ifr_name); */
550 if (myNetMasks[rxi_numNetAddrs] == 0) {
551 myNetMasks[rxi_numNetAddrs] =
552 fudge_netmask(rxi_NetAddrs[rxi_numNetAddrs]);
553 #ifdef SIOCGIFNETMASK
554 res = ioctl(s, SIOCGIFNETMASK, ifr);
556 a = (struct sockaddr_in *)&ifr->ifr_addr;
557 myNetMasks[rxi_numNetAddrs] = ntohl(a->sin_addr.s_addr);
558 /* fprintf(stderr, "if %s subnetmask=0x%x\n",
559 * ifr->ifr_name, myNetMasks[rxi_numNetAddrs]); */
561 /* fputs(stderr, "ioctl error IFMASK\n");
562 * perror(ifr->ifr_name); */
567 if (rxi_NetAddrs[rxi_numNetAddrs] != 0x7f000001) { /* ignore lo0 */
570 rxi_nRecvFrags * (myNetMTUs[rxi_numNetAddrs] - RX_IP_SIZE);
571 maxsize -= UDP_HDR_SIZE; /* only the first frag has a UDP hdr */
572 if (rx_maxReceiveSize < maxsize)
573 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxsize);
580 /* have to allocate at least enough to allow a single packet to reach its
581 * maximum size, so ReadPacket will work. Allocate enough for a couple
582 * of packets to do so, for good measure */
584 int npackets, ncbufs;
586 rx_maxJumboRecvSize =
587 RX_HEADER_SIZE + rxi_nDgramPackets * RX_JUMBOBUFFERSIZE +
588 (rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE;
589 rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
590 ncbufs = (rx_maxJumboRecvSize - RX_FIRSTBUFFERSIZE);
592 ncbufs = ncbufs / RX_CBUFFERSIZE;
593 npackets = rx_initSendWindow - 1;
594 rxi_MorePackets(npackets * (ncbufs + 1));
597 #else /* AFS_DJGPP_ENV */
600 #endif /* AFS_DJGPP_ENV */
602 #endif /* AFS_NT40_ENV */
604 /* Called from rxi_FindPeer, when initializing a clear rx_peer structure,
605 * to get interesting information.
606 * Curiously enough, the rx_peerHashTable_lock currently protects the
607 * Inited variable (and hence rx_GetIFInfo). When the fs suite uses
608 * pthreads, this issue will need to be revisited.
612 rxi_InitPeerParams(struct rx_peer *pp)
624 * there's a race here since more than one thread could call
625 * rx_GetIFInfo. The race stops in rx_GetIFInfo.
633 /* try to second-guess IP, and identify which link is most likely to
634 * be used for traffic to/from this host. */
635 ppaddr = ntohl(pp->host);
639 pp->rateFlag = 2; /* start timing after two full packets */
640 /* I don't initialize these, because I presume they are bzero'd...
641 * pp->burstSize pp->burst pp->burstWait.sec pp->burstWait.usec
642 * pp->timeout.usec */
645 for (ix = 0; ix < rxi_numNetAddrs; ++ix) {
646 if ((rxi_NetAddrs[ix] & myNetMasks[ix]) == (ppaddr & myNetMasks[ix])) {
647 #ifdef IFF_POINTOPOINT
648 if (myNetFlags[ix] & IFF_POINTOPOINT)
650 #endif /* IFF_POINTOPOINT */
651 rxmtu = myNetMTUs[ix] - RX_IPUDP_SIZE;
652 if (rxmtu < RX_MIN_PACKET_SIZE)
653 rxmtu = RX_MIN_PACKET_SIZE;
654 if (pp->ifMTU < rxmtu)
655 pp->ifMTU = MIN(rx_MyMaxSendSize, rxmtu);
659 if (!pp->ifMTU) { /* not local */
661 pp->ifMTU = MIN(rx_MyMaxSendSize, RX_REMOTE_PACKET_SIZE);
663 #else /* ADAPT_MTU */
664 pp->rateFlag = 2; /* start timing after two full packets */
666 pp->ifMTU = MIN(rx_MyMaxSendSize, OLD_MAX_PACKET_SIZE);
667 #endif /* ADAPT_MTU */
668 pp->ifMTU = rxi_AdjustIfMTU(pp->ifMTU);
669 pp->maxMTU = OLD_MAX_PACKET_SIZE; /* for compatibility with old guys */
670 pp->natMTU = MIN((int)pp->ifMTU, OLD_MAX_PACKET_SIZE);
671 pp->maxDgramPackets =
672 MIN(rxi_nDgramPackets,
673 rxi_AdjustDgramPackets(RX_MAX_FRAGS, pp->ifMTU));
675 MIN(rxi_nDgramPackets,
676 rxi_AdjustDgramPackets(RX_MAX_FRAGS, pp->ifMTU));
677 pp->maxDgramPackets = 1;
678 /* Initialize slow start parameters */
679 pp->MTU = MIN(pp->natMTU, pp->maxMTU);
681 pp->nDgramPackets = 1;
685 /* Don't expose jumobgram internals. */
689 rx_maxReceiveSize = OLD_MAX_PACKET_SIZE;
690 rxi_nSendFrags = rxi_nRecvFrags = 1;
693 /* Override max MTU. If rx_SetNoJumbo is called, it must be
694 called before calling rx_SetMaxMTU since SetNoJumbo clobbers rx_maxReceiveSize */
696 rx_SetMaxMTU(int mtu)
698 rx_MyMaxSendSize = rx_maxReceiveSizeUser = rx_maxReceiveSize = mtu;