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>
16 # include <sys/types.h>
21 # include <WINNT/syscfg.h>
23 # include <sys/socket.h>
24 # include <sys/file.h>
26 # include <sys/stat.h>
27 # include <netinet/in.h>
28 # include <sys/time.h>
30 # include <sys/ioctl.h>
34 #if !defined(AFS_AIX_ENV) && !defined(AFS_NT40_ENV)
35 # include <sys/syscall.h>
37 #include <afs/afs_args.h>
38 #include <afs/afsutil.h>
40 #ifndef IPPORT_USERRESERVED
41 /* If in.h doesn't define this, define it anyway. Unfortunately, defining
42 this doesn't put the code into the kernel to restrict kernel assigned
43 port numbers to numbers below IPPORT_USERRESERVED... */
44 #define IPPORT_USERRESERVED 5000
48 # include <sys/time.h>
51 # include "rx_globals.h"
53 #ifdef AFS_PTHREAD_ENV
57 * The rx_if_init_mutex mutex protects the following global variables:
61 afs_kmutex_t rx_if_init_mutex;
62 #define LOCK_IF_INIT MUTEX_ENTER(&rx_if_init_mutex)
63 #define UNLOCK_IF_INIT MUTEX_EXIT(&rx_if_init_mutex)
66 * The rx_if_mutex mutex protects the following global variables:
72 afs_kmutex_t rx_if_mutex;
73 #define LOCK_IF MUTEX_ENTER(&rx_if_mutex)
74 #define UNLOCK_IF MUTEX_EXIT(&rx_if_mutex)
77 #define UNLOCK_IF_INIT
80 #endif /* AFS_PTHREAD_ENV */
84 * Make a socket for receiving/sending IP packets. Set it into non-blocking
85 * and large buffering modes. If port isn't specified, the kernel will pick
86 * one. Returns the socket (>= 0) on success. Returns OSI_NULLSOCKET on
87 * failure. Port must be in network byte order.
90 rxi_GetHostUDPSocket(u_int ahost, u_short port)
93 osi_socket socketFd = OSI_NULLSOCKET;
94 struct sockaddr_in taddr;
95 char *name = "rxi_GetUDPSocket: ";
96 #ifdef AFS_LINUX22_ENV
97 #if defined(ADAPT_PMTU)
98 int pmtu=IP_PMTUDISC_WANT;
101 int pmtu=IP_PMTUDISC_DONT;
104 #if defined(HAVE_LINUX_ERRQUEUE_H) && defined(ADAPT_PMTU)
105 #include <linux/types.h>
106 #include <linux/errqueue.h>
112 #if !defined(AFS_NT40_ENV)
113 if (ntohs(port) >= IPPORT_RESERVED && ntohs(port) < IPPORT_USERRESERVED) {
114 /* (osi_Msg "%s*WARNING* port number %d is not a reserved port number. Use port numbers above %d\n", name, port, IPPORT_USERRESERVED);
117 if (ntohs(port) > 0 && ntohs(port) < IPPORT_RESERVED && geteuid() != 0) {
119 "%sport number %d is a reserved port number which may only be used by root. Use port numbers above %d\n",
120 name, ntohs(port), IPPORT_USERRESERVED);
124 socketFd = socket(AF_INET, SOCK_DGRAM, 0);
132 rxi_xmit_init(socketFd);
133 #endif /* AFS_NT40_ENV */
135 taddr.sin_addr.s_addr = ahost;
136 taddr.sin_family = AF_INET;
137 taddr.sin_port = (u_short) port;
138 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
139 taddr.sin_len = sizeof(struct sockaddr_in);
141 #define MAX_RX_BINDS 10
142 for (binds = 0; binds < MAX_RX_BINDS; binds++) {
145 code = bind(socketFd, (struct sockaddr *)&taddr, sizeof(taddr));
151 (osi_Msg "%sbind failed\n", name);
154 #if !defined(AFS_NT40_ENV)
156 * Set close-on-exec on rx socket
158 fcntl(socketFd, F_SETFD, 1);
161 /* Use one of three different ways of getting a socket buffer expanded to
169 len2 = rx_UdpBufSize;
171 /* find the size closest to rx_UdpBufSize that will be accepted */
172 while (!greedy && len2 > len1) {
175 (socketFd, SOL_SOCKET, SO_RCVBUF, (char *)&len2,
181 /* but do not let it get smaller than 32K */
191 (socketFd, SOL_SOCKET, SO_SNDBUF, (char *)&len1,
195 (socketFd, SOL_SOCKET, SO_RCVBUF, (char *)&len2,
198 (osi_Msg "%s*WARNING* Unable to increase buffering on socket\n",
200 if (rx_stats_active) {
201 MUTEX_ENTER(&rx_stats_mutex);
202 rx_stats.socketGreedy = greedy;
203 MUTEX_EXIT(&rx_stats_mutex);
207 #ifdef AFS_LINUX22_ENV
208 setsockopt(socketFd, SOL_IP, IP_MTU_DISCOVER, &pmtu, sizeof(pmtu));
209 #if defined(ADAPT_PMTU)
210 setsockopt(socketFd, SOL_IP, IP_RECVERR, &recverr, sizeof(recverr));
213 if (rxi_Listen(socketFd) < 0) {
222 closesocket(socketFd);
228 return OSI_NULLSOCKET;
232 rxi_GetUDPSocket(u_short port)
234 return rxi_GetHostUDPSocket(htonl(INADDR_ANY), port);
238 osi_Panic(char *msg, ...)
242 (osi_Msg "Fatal Rx error: ");
251 * osi_AssertFailU() -- used by the osi_Assert() macro.
255 osi_AssertFailU(const char *expr, const char *file, int line)
257 osi_Panic("assertion failed: %s, file: %s, line: %d\n", expr,
261 #if defined(AFS_AIX32_ENV) && !defined(KERNEL)
263 static const char memZero;
265 osi_Alloc(afs_int32 x)
268 * 0-length allocs may return NULL ptr from malloc, so we special-case
269 * things so that NULL returned iff an error occurred
272 return (void *)&memZero;
277 osi_Free(void *x, afs_int32 size)
284 #endif /* defined(AFS_AIX32_ENV) && !defined(KERNEL) */
286 #define ADDRSPERSITE 16
289 static afs_uint32 rxi_NetAddrs[ADDRSPERSITE]; /* host order */
290 static int myNetMTUs[ADDRSPERSITE];
291 static int myNetMasks[ADDRSPERSITE];
292 static int myNetFlags[ADDRSPERSITE];
293 static u_int rxi_numNetAddrs;
294 static int Inited = 0;
296 #if defined(AFS_NT40_ENV)
300 /* 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 if (rxi_numNetAddrs > 1)
306 return htonl(rxi_NetAddrs[1]);
307 else if (rxi_numNetAddrs > 0)
308 return htonl(rxi_NetAddrs[0]);
314 ** return number of addresses
315 ** and the addresses themselves in the buffer
316 ** maxSize - max number of interfaces to return.
319 rx_getAllAddr(afs_uint32 * buffer, int maxSize)
321 int count = 0, offset = 0;
323 /* The IP address list can change so we must query for it */
327 /* we don't want to use the loopback adapter which is first */
328 /* this is a bad bad hack.
329 * and doesn't hold true on Windows.
331 if ( rxi_numNetAddrs > 1 )
333 #endif /* AFS_DJGPP_ENV */
335 for (count = 0; offset < rxi_numNetAddrs && maxSize > 0;
336 count++, offset++, maxSize--)
337 buffer[count] = htonl(rxi_NetAddrs[offset]);
342 /* this function returns the total number of interface addresses
343 * the buffer has to be passed in by the caller. It also returns
344 * the matching interface mask and mtu. All values are returned
345 * in network byte order.
348 rx_getAllAddrMaskMtu(afs_uint32 addrBuffer[], afs_uint32 maskBuffer[],
349 afs_uint32 mtuBuffer[], int maxSize)
351 int count = 0, offset = 0;
353 /* The IP address list can change so we must query for it */
357 /* we don't want to use the loopback adapter which is first */
358 /* this is a bad bad hack.
359 * and doesn't hold true on Windows.
361 if ( rxi_numNetAddrs > 1 )
363 #endif /* AFS_DJGPP_ENV */
366 offset < rxi_numNetAddrs && maxSize > 0;
367 count++, offset++, maxSize--) {
368 addrBuffer[count] = htonl(rxi_NetAddrs[offset]);
369 maskBuffer[count] = htonl(myNetMasks[offset]);
370 mtuBuffer[count] = htonl(myNetMTUs[offset]);
377 extern int rxinit_status;
379 rxi_InitMorePackets(void) {
380 int npackets, ncbufs;
382 ncbufs = (rx_maxJumboRecvSize - RX_FIRSTBUFFERSIZE);
384 ncbufs = ncbufs / RX_CBUFFERSIZE;
385 npackets = rx_initSendWindow - 1;
386 rxi_MorePackets(npackets * (ncbufs + 1));
398 if (Inited < 2 && rxinit_status == 0) {
399 /* We couldn't initialize more packets earlier.
401 rxi_InitMorePackets();
411 rxi_numNetAddrs = ADDRSPERSITE;
412 (void)syscfg_GetIFInfo(&rxi_numNetAddrs, rxi_NetAddrs,
413 myNetMasks, myNetMTUs, myNetFlags);
415 for (i = 0; i < rxi_numNetAddrs; i++) {
416 rxsize = rxi_AdjustIfMTU(myNetMTUs[i] - RX_IPUDP_SIZE);
418 rxi_nRecvFrags * rxsize + (rxi_nRecvFrags - 1) * UDP_HDR_SIZE;
419 maxsize = rxi_AdjustMaxMTU(rxsize, maxsize);
420 if (rx_maxReceiveSize < maxsize) {
421 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxsize);
423 MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
430 * If rxinit_status is still set, rx_InitHost() has yet to be called
431 * and we therefore do not have any mutex locks initialized. As a
432 * result we cannot call rxi_MorePackets() without crashing.
437 rxi_InitMorePackets();
442 fudge_netmask(afs_uint32 addr)
448 else if (IN_CLASSB(addr))
450 else if (IN_CLASSC(addr))
460 #if !defined(AFS_AIX_ENV) && !defined(AFS_NT40_ENV) && !defined(AFS_LINUX20_ENV)
462 rxi_syscall(a3, a4, a5)
469 old = (void (*)())signal(SIGSYS, SIG_IGN);
471 #if defined(AFS_SGI_ENV)
472 rcode = afs_syscall(a3, a4, a5);
474 rcode = syscall(AFS_SYSCALL, 28 /* AFSCALL_CALL */ , a3, a4, a5);
475 #endif /* AFS_SGI_ENV */
481 #endif /* AFS_AIX_ENV */
490 struct ifreq ifs[ADDRSPERSITE];
493 char buf[BUFSIZ], *cp, *cplim;
495 struct sockaddr_in *a;
506 memset(rxi_NetAddrs, 0, sizeof(rxi_NetAddrs));
507 memset(myNetFlags, 0, sizeof(myNetFlags));
508 memset(myNetMTUs, 0, sizeof(myNetMTUs));
509 memset(myNetMasks, 0, sizeof(myNetMasks));
511 s = socket(AF_INET, SOCK_DGRAM, 0);
516 ifc.ifc_len = sizeof(buf);
520 ifc.ifc_len = sizeof(ifs);
521 ifc.ifc_buf = (caddr_t) & ifs[0];
522 memset(&ifs[0], 0, sizeof(ifs));
524 res = ioctl(s, SIOCGIFCONF, &ifc);
526 /* fputs(stderr, "ioctl error IFCONF\n"); */
533 #define size(p) MAX((p).sa_len, sizeof(p))
534 cplim = buf + ifc.ifc_len; /*skip over if's with big ifr_addr's */
535 for (cp = buf; cp < cplim;
536 cp += sizeof(ifr->ifr_name) + MAX(a->sin_len, sizeof(*a))) {
537 if (rxi_numNetAddrs >= ADDRSPERSITE)
540 ifr = (struct ifreq *)cp;
542 len = ifc.ifc_len / sizeof(struct ifreq);
543 if (len > ADDRSPERSITE)
546 for (i = 0; i < len; ++i) {
548 res = ioctl(s, SIOCGIFADDR, ifr);
551 /* fputs(stderr, "ioctl error IFADDR\n");
552 * perror(ifr->ifr_name); */
555 a = (struct sockaddr_in *)&ifr->ifr_addr;
556 if (a->sin_family != AF_INET)
558 rxi_NetAddrs[rxi_numNetAddrs] = ntohl(a->sin_addr.s_addr);
559 if (rxi_NetAddrs[rxi_numNetAddrs] == 0x7f000001) {
560 /* we don't really care about "localhost" */
563 for (j = 0; j < rxi_numNetAddrs; j++) {
564 if (rxi_NetAddrs[j] == rxi_NetAddrs[rxi_numNetAddrs])
567 if (j < rxi_numNetAddrs)
570 /* fprintf(stderr, "if %s addr=%x\n", ifr->ifr_name,
571 * rxi_NetAddrs[rxi_numNetAddrs]); */
574 res = ioctl(s, SIOCGIFFLAGS, ifr);
576 myNetFlags[rxi_numNetAddrs] = ifr->ifr_flags;
578 /* Handle aliased loopbacks as well. */
579 if (ifr->ifr_flags & IFF_LOOPBACK)
582 /* fprintf(stderr, "if %s flags=%x\n",
583 * ifr->ifr_name, ifr->ifr_flags); */
585 * fputs(stderr, "ioctl error IFFLAGS\n");
586 * perror(ifr->ifr_name); */
588 #endif /* SIOCGIFFLAGS */
590 #if !defined(AFS_AIX_ENV) && !defined(AFS_LINUX20_ENV)
591 /* this won't run on an AIX system w/o a cache manager */
592 rxi_syscallp = rxi_syscall;
595 /* If I refer to kernel extensions that aren't loaded on AIX, the
596 * program refuses to load and run, so I simply can't include the
597 * following code. Fortunately, AIX is the one operating system in
598 * which the subsequent ioctl works reliably. */
600 if ((*rxi_syscallp) (20 /*AFSOP_GETMTU */ ,
601 htonl(rxi_NetAddrs[rxi_numNetAddrs]),
602 &(myNetMTUs[rxi_numNetAddrs]))) {
603 /* fputs(stderr, "syscall error GETMTU\n");
604 * perror(ifr->ifr_name); */
605 myNetMTUs[rxi_numNetAddrs] = 0;
607 if ((*rxi_syscallp) (42 /*AFSOP_GETMASK */ ,
608 htonl(rxi_NetAddrs[rxi_numNetAddrs]),
609 &(myNetMasks[rxi_numNetAddrs]))) {
610 /* fputs(stderr, "syscall error GETMASK\n");
611 * perror(ifr->ifr_name); */
612 myNetMasks[rxi_numNetAddrs] = 0;
614 myNetMasks[rxi_numNetAddrs] =
615 ntohl(myNetMasks[rxi_numNetAddrs]);
616 /* fprintf(stderr, "if %s mask=0x%x\n",
617 * ifr->ifr_name, myNetMasks[rxi_numNetAddrs]); */
620 if (myNetMTUs[rxi_numNetAddrs] == 0) {
621 myNetMTUs[rxi_numNetAddrs] = OLD_MAX_PACKET_SIZE + RX_IPUDP_SIZE;
623 res = ioctl(s, SIOCGIFMTU, ifr);
624 if ((res == 0) && (ifr->ifr_metric > 128)) { /* sanity check */
625 myNetMTUs[rxi_numNetAddrs] = ifr->ifr_metric;
626 /* fprintf(stderr, "if %s mtu=%d\n",
627 * ifr->ifr_name, ifr->ifr_metric); */
629 /* fputs(stderr, "ioctl error IFMTU\n");
630 * perror(ifr->ifr_name); */
635 if (myNetMasks[rxi_numNetAddrs] == 0) {
636 myNetMasks[rxi_numNetAddrs] =
637 fudge_netmask(rxi_NetAddrs[rxi_numNetAddrs]);
638 #ifdef SIOCGIFNETMASK
639 res = ioctl(s, SIOCGIFNETMASK, ifr);
641 a = (struct sockaddr_in *)&ifr->ifr_addr;
642 myNetMasks[rxi_numNetAddrs] = ntohl(a->sin_addr.s_addr);
643 /* fprintf(stderr, "if %s subnetmask=0x%x\n",
644 * ifr->ifr_name, myNetMasks[rxi_numNetAddrs]); */
646 /* fputs(stderr, "ioctl error IFMASK\n");
647 * perror(ifr->ifr_name); */
652 if (rxi_NetAddrs[rxi_numNetAddrs] != 0x7f000001) { /* ignore lo0 */
655 rxi_nRecvFrags * (myNetMTUs[rxi_numNetAddrs] - RX_IP_SIZE);
656 maxsize -= UDP_HDR_SIZE; /* only the first frag has a UDP hdr */
657 if (rx_maxReceiveSize < maxsize)
658 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxsize);
665 /* have to allocate at least enough to allow a single packet to reach its
666 * maximum size, so ReadPacket will work. Allocate enough for a couple
667 * of packets to do so, for good measure */
669 int npackets, ncbufs;
671 rx_maxJumboRecvSize =
672 RX_HEADER_SIZE + rxi_nDgramPackets * RX_JUMBOBUFFERSIZE +
673 (rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE;
674 rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
675 ncbufs = (rx_maxJumboRecvSize - RX_FIRSTBUFFERSIZE);
677 ncbufs = ncbufs / RX_CBUFFERSIZE;
678 npackets = rx_initSendWindow - 1;
679 rxi_MorePackets(npackets * (ncbufs + 1));
683 #endif /* AFS_NT40_ENV */
685 /* Called from rxi_FindPeer, when initializing a clear rx_peer structure,
686 * to get interesting information.
687 * Curiously enough, the rx_peerHashTable_lock currently protects the
688 * Inited variable (and hence rx_GetIFInfo). When the fs suite uses
689 * pthreads, this issue will need to be revisited.
693 rxi_InitPeerParams(struct rx_peer *pp)
698 #if defined(ADAPT_PMTU) && defined(IP_MTU)
700 struct sockaddr_in addr;
709 * there's a race here since more than one thread could call
710 * rx_GetIFInfo. The race stops in rx_GetIFInfo.
718 /* try to second-guess IP, and identify which link is most likely to
719 * be used for traffic to/from this host. */
720 ppaddr = ntohl(pp->host);
724 pp->rateFlag = 2; /* start timing after two full packets */
725 /* I don't initialize these, because I presume they are bzero'd...
726 * pp->burstSize pp->burst pp->burstWait.sec pp->burstWait.usec
727 * pp->timeout.usec */
730 for (ix = 0; ix < rxi_numNetAddrs; ++ix) {
731 if ((rxi_NetAddrs[ix] & myNetMasks[ix]) == (ppaddr & myNetMasks[ix])) {
732 #ifdef IFF_POINTOPOINT
733 if (myNetFlags[ix] & IFF_POINTOPOINT)
735 #endif /* IFF_POINTOPOINT */
736 rxmtu = myNetMTUs[ix] - RX_IPUDP_SIZE;
737 if (rxmtu < RX_MIN_PACKET_SIZE)
738 rxmtu = RX_MIN_PACKET_SIZE;
739 if (pp->ifMTU < rxmtu)
740 pp->ifMTU = MIN(rx_MyMaxSendSize, rxmtu);
744 if (!pp->ifMTU) { /* not local */
746 pp->ifMTU = MIN(rx_MyMaxSendSize, RX_REMOTE_PACKET_SIZE);
748 #else /* ADAPT_MTU */
749 pp->rateFlag = 2; /* start timing after two full packets */
751 pp->ifMTU = MIN(rx_MyMaxSendSize, OLD_MAX_PACKET_SIZE);
752 #endif /* ADAPT_MTU */
753 #if defined(ADAPT_PMTU) && defined(IP_MTU)
754 sock=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
756 addr.sin_family = AF_INET;
757 addr.sin_addr.s_addr = pp->host;
758 addr.sin_port = pp->port;
759 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0) {
761 socklen_t s = sizeof(mtu);
762 if (getsockopt(sock, SOL_IP, IP_MTU, &mtu, &s)== 0) {
763 pp->ifMTU = MIN(mtu - RX_IPUDP_SIZE, pp->ifMTU);
769 pp->ifMTU = rxi_AdjustIfMTU(pp->ifMTU);
770 pp->maxMTU = OLD_MAX_PACKET_SIZE; /* for compatibility with old guys */
771 pp->natMTU = MIN((int)pp->ifMTU, OLD_MAX_PACKET_SIZE);
772 pp->maxDgramPackets =
773 MIN(rxi_nDgramPackets,
774 rxi_AdjustDgramPackets(rxi_nSendFrags, pp->ifMTU));
776 MIN(rxi_nDgramPackets,
777 rxi_AdjustDgramPackets(rxi_nSendFrags, pp->ifMTU));
778 pp->maxDgramPackets = 1;
779 /* Initialize slow start parameters */
780 pp->MTU = MIN(pp->natMTU, pp->maxMTU);
782 pp->nDgramPackets = 1;
786 /* Don't expose jumobgram internals. */
790 rx_maxReceiveSize = OLD_MAX_PACKET_SIZE;
791 rxi_nSendFrags = rxi_nRecvFrags = 1;
794 /* Override max MTU. If rx_SetNoJumbo is called, it must be
795 called before calling rx_SetMaxMTU since SetNoJumbo clobbers rx_maxReceiveSize */
797 rx_SetMaxMTU(int mtu)
799 rx_MyMaxSendSize = rx_maxReceiveSizeUser = rx_maxReceiveSize = mtu;
802 #if defined(HAVE_LINUX_ERRQUEUE_H) && defined(ADAPT_PMTU)
804 rxi_HandleSocketError(int socket)
807 struct cmsghdr *cmsg;
808 struct sock_extended_err *err;
809 struct sockaddr_in addr;
810 struct sockaddr *offender;
811 char controlmsgbuf[256];
815 msg.msg_name = &addr;
816 msg.msg_namelen = sizeof(addr);
819 msg.msg_control = controlmsgbuf;
820 msg.msg_controllen = 256;
822 code = recvmsg(socket, &msg, MSG_ERRQUEUE|MSG_DONTWAIT|MSG_TRUNC);
824 if (code < 0 || !(msg.msg_flags & MSG_ERRQUEUE))
827 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
828 if ((char *)cmsg - controlmsgbuf > msg.msg_controllen - CMSG_SPACE(0) ||
829 (char *)cmsg - controlmsgbuf > msg.msg_controllen - CMSG_SPACE(cmsg->cmsg_len) ||
830 cmsg->cmsg_len == 0) {
834 if (cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_RECVERR)
840 err =(struct sock_extended_err *) CMSG_DATA(cmsg);
842 if (err->ee_errno == EMSGSIZE && err->ee_info >= 68) {
843 rxi_SetPeerMtu(addr.sin_addr.s_addr, addr.sin_port,
844 err->ee_info - RX_IPUDP_SIZE);
846 /* other DEST_UNREACH's and TIME_EXCEEDED should be dealt with too */