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)
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 #if defined(ADAPT_PMTU)
99 int pmtu=IP_PMTUDISC_WANT;
102 int pmtu=IP_PMTUDISC_DONT;
105 #if defined(HAVE_LINUX_ERRQUEUE_H) && defined(ADAPT_PMTU)
106 #include <linux/types.h>
107 #include <linux/errqueue.h>
113 #if !defined(AFS_NT40_ENV)
114 if (ntohs(port) >= IPPORT_RESERVED && ntohs(port) < IPPORT_USERRESERVED) {
115 /* (osi_Msg "%s*WARNING* port number %d is not a reserved port number. Use port numbers above %d\n", name, port, IPPORT_USERRESERVED);
118 if (ntohs(port) > 0 && ntohs(port) < IPPORT_RESERVED && geteuid() != 0) {
120 "%sport number %d is a reserved port number which may only be used by root. Use port numbers above %d\n",
121 name, ntohs(port), IPPORT_USERRESERVED);
125 socketFd = socket(AF_INET, SOCK_DGRAM, 0);
132 taddr.sin_addr.s_addr = ahost;
133 taddr.sin_family = AF_INET;
134 taddr.sin_port = (u_short) port;
135 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
136 taddr.sin_len = sizeof(struct sockaddr_in);
138 #define MAX_RX_BINDS 10
139 for (binds = 0; binds < MAX_RX_BINDS; binds++) {
142 code = bind(socketFd, (struct sockaddr *)&taddr, sizeof(taddr));
148 (osi_Msg "%sbind failed\n", name);
151 #if !defined(AFS_NT40_ENV)
153 * Set close-on-exec on rx socket
155 fcntl(socketFd, F_SETFD, 1);
158 /* Use one of three different ways of getting a socket buffer expanded to
166 len2 = rx_UdpBufSize;
169 (socketFd, SOL_SOCKET, SO_RCVBUF, (char *)&len2,
172 len2 = 32766; /* fall back to old size... uh-oh! */
177 (socketFd, SOL_SOCKET, SO_SNDBUF, (char *)&len1,
181 (socketFd, SOL_SOCKET, SO_RCVBUF, (char *)&len2,
184 (osi_Msg "%s*WARNING* Unable to increase buffering on socket\n",
186 MUTEX_ENTER(&rx_stats_mutex);
187 rx_stats.socketGreedy = greedy;
188 MUTEX_EXIT(&rx_stats_mutex);
191 #ifdef AFS_LINUX22_ENV
192 setsockopt(socketFd, SOL_IP, IP_MTU_DISCOVER, &pmtu, sizeof(pmtu));
193 #if defined(ADAPT_PMTU)
194 setsockopt(socketFd, SOL_IP, IP_RECVERR, &recverr, sizeof(recverr));
197 if (rxi_Listen(socketFd) < 0) {
206 closesocket(socketFd);
212 return OSI_NULLSOCKET;
216 rxi_GetUDPSocket(u_short port)
218 return rxi_GetHostUDPSocket(htonl(INADDR_ANY), port);
222 osi_Panic(char *msg, ...)
226 (osi_Msg "Fatal Rx error: ");
235 * osi_AssertFailU() -- used by the osi_Assert() macro.
239 osi_AssertFailU(const char *expr, const char *file, int line)
241 osi_Panic("assertion failed: %s, file: %s, line: %d\n", expr,
245 #if defined(AFS_AIX32_ENV) && !defined(KERNEL)
247 static const char memZero;
249 osi_Alloc(afs_int32 x)
252 * 0-length allocs may return NULL ptr from malloc, so we special-case
253 * things so that NULL returned iff an error occurred
256 return (void *)&memZero;
261 osi_Free(void *x, afs_int32 size)
268 #endif /* defined(AFS_AIX32_ENV) && !defined(KERNEL) */
270 #define ADDRSPERSITE 16
273 static afs_uint32 rxi_NetAddrs[ADDRSPERSITE]; /* host order */
274 static int myNetMTUs[ADDRSPERSITE];
275 static int myNetMasks[ADDRSPERSITE];
276 static int myNetFlags[ADDRSPERSITE];
277 static u_int rxi_numNetAddrs;
278 static int Inited = 0;
280 #if defined(AFS_NT40_ENV)
284 /* The IP address list can change so we must query for it */
287 /* we don't want to use the loopback adapter which is first */
288 /* this is a bad bad hack */
289 if (rxi_numNetAddrs > 1)
290 return htonl(rxi_NetAddrs[1]);
291 else if (rxi_numNetAddrs > 0)
292 return htonl(rxi_NetAddrs[0]);
298 ** return number of addresses
299 ** and the addresses themselves in the buffer
300 ** maxSize - max number of interfaces to return.
303 rx_getAllAddr(afs_uint32 * buffer, int maxSize)
305 int count = 0, offset = 0;
307 /* The IP address list can change so we must query for it */
311 /* we don't want to use the loopback adapter which is first */
312 /* this is a bad bad hack.
313 * and doesn't hold true on Windows.
315 if ( rxi_numNetAddrs > 1 )
317 #endif /* AFS_NT40_ENV */
319 for (count = 0; offset < rxi_numNetAddrs && maxSize > 0;
320 count++, offset++, maxSize--)
321 buffer[count] = htonl(rxi_NetAddrs[offset]);
328 extern int rxinit_status;
330 rxi_InitMorePackets(void) {
331 int npackets, ncbufs;
333 ncbufs = (rx_maxJumboRecvSize - RX_FIRSTBUFFERSIZE);
335 ncbufs = ncbufs / RX_CBUFFERSIZE;
336 npackets = rx_initSendWindow - 1;
337 rxi_MorePackets(npackets * (ncbufs + 1));
349 if (Inited < 2 && rxinit_status == 0) {
350 /* We couldn't initialize more packets earlier.
352 rxi_InitMorePackets();
362 rxi_numNetAddrs = ADDRSPERSITE;
363 (void)syscfg_GetIFInfo(&rxi_numNetAddrs, rxi_NetAddrs,
364 myNetMasks, myNetMTUs, myNetFlags);
366 for (i = 0; i < rxi_numNetAddrs; i++) {
367 rxsize = rxi_AdjustIfMTU(myNetMTUs[i] - RX_IPUDP_SIZE);
369 rxi_nRecvFrags * rxsize + (rxi_nRecvFrags - 1) * UDP_HDR_SIZE;
370 maxsize = rxi_AdjustMaxMTU(rxsize, maxsize);
371 if (rx_maxReceiveSize < maxsize) {
372 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxsize);
374 MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
381 * If rxinit_status is still set, rx_InitHost() has yet to be called
382 * and we therefore do not have any mutex locks initialized. As a
383 * result we cannot call rxi_MorePackets() without crashing.
388 rxi_InitMorePackets();
393 fudge_netmask(afs_uint32 addr)
399 else if (IN_CLASSB(addr))
401 else if (IN_CLASSC(addr))
411 #if !defined(AFS_AIX_ENV) && !defined(AFS_NT40_ENV) && !defined(AFS_LINUX20_ENV)
413 rxi_syscall(a3, a4, a5)
420 old = (void (*)())signal(SIGSYS, SIG_IGN);
422 #if defined(AFS_SGI_ENV)
423 rcode = afs_syscall(a3, a4, a5);
425 rcode = syscall(AFS_SYSCALL, 28 /* AFSCALL_CALL */ , a3, a4, a5);
426 #endif /* AFS_SGI_ENV */
432 #endif /* AFS_AIX_ENV */
441 struct ifreq ifs[ADDRSPERSITE];
444 char buf[BUFSIZ], *cp, *cplim;
446 struct sockaddr_in *a;
457 memset(rxi_NetAddrs, 0, sizeof(rxi_NetAddrs));
458 memset(myNetFlags, 0, sizeof(myNetFlags));
459 memset(myNetMTUs, 0, sizeof(myNetMTUs));
460 memset(myNetMasks, 0, sizeof(myNetMasks));
462 s = socket(AF_INET, SOCK_DGRAM, 0);
467 ifc.ifc_len = sizeof(buf);
471 ifc.ifc_len = sizeof(ifs);
472 ifc.ifc_buf = (caddr_t) & ifs[0];
473 memset(&ifs[0], 0, sizeof(ifs));
475 res = ioctl(s, SIOCGIFCONF, &ifc);
477 /* fputs(stderr, "ioctl error IFCONF\n"); */
484 #define size(p) MAX((p).sa_len, sizeof(p))
485 cplim = buf + ifc.ifc_len; /*skip over if's with big ifr_addr's */
486 for (cp = buf; cp < cplim;
487 cp += sizeof(ifr->ifr_name) + MAX(a->sin_len, sizeof(*a))) {
488 if (rxi_numNetAddrs >= ADDRSPERSITE)
491 ifr = (struct ifreq *)cp;
493 len = ifc.ifc_len / sizeof(struct ifreq);
494 if (len > ADDRSPERSITE)
497 for (i = 0; i < len; ++i) {
499 res = ioctl(s, SIOCGIFADDR, ifr);
502 /* fputs(stderr, "ioctl error IFADDR\n");
503 * perror(ifr->ifr_name); */
506 a = (struct sockaddr_in *)&ifr->ifr_addr;
507 if (a->sin_family != AF_INET)
509 rxi_NetAddrs[rxi_numNetAddrs] = ntohl(a->sin_addr.s_addr);
510 if (rxi_NetAddrs[rxi_numNetAddrs] == 0x7f000001) {
511 /* we don't really care about "localhost" */
514 for (j = 0; j < rxi_numNetAddrs; j++) {
515 if (rxi_NetAddrs[j] == rxi_NetAddrs[rxi_numNetAddrs])
518 if (j < rxi_numNetAddrs)
521 /* fprintf(stderr, "if %s addr=%x\n", ifr->ifr_name,
522 * rxi_NetAddrs[rxi_numNetAddrs]); */
525 res = ioctl(s, SIOCGIFFLAGS, ifr);
527 myNetFlags[rxi_numNetAddrs] = ifr->ifr_flags;
529 /* Handle aliased loopbacks as well. */
530 if (ifr->ifr_flags & IFF_LOOPBACK)
533 /* fprintf(stderr, "if %s flags=%x\n",
534 * ifr->ifr_name, ifr->ifr_flags); */
536 * fputs(stderr, "ioctl error IFFLAGS\n");
537 * perror(ifr->ifr_name); */
539 #endif /* SIOCGIFFLAGS */
541 #if !defined(AFS_AIX_ENV) && !defined(AFS_LINUX20_ENV)
542 /* this won't run on an AIX system w/o a cache manager */
543 rxi_syscallp = rxi_syscall;
546 /* If I refer to kernel extensions that aren't loaded on AIX, the
547 * program refuses to load and run, so I simply can't include the
548 * following code. Fortunately, AIX is the one operating system in
549 * which the subsequent ioctl works reliably. */
551 if ((*rxi_syscallp) (20 /*AFSOP_GETMTU */ ,
552 htonl(rxi_NetAddrs[rxi_numNetAddrs]),
553 &(myNetMTUs[rxi_numNetAddrs]))) {
554 /* fputs(stderr, "syscall error GETMTU\n");
555 * perror(ifr->ifr_name); */
556 myNetMTUs[rxi_numNetAddrs] = 0;
558 if ((*rxi_syscallp) (42 /*AFSOP_GETMASK */ ,
559 htonl(rxi_NetAddrs[rxi_numNetAddrs]),
560 &(myNetMasks[rxi_numNetAddrs]))) {
561 /* fputs(stderr, "syscall error GETMASK\n");
562 * perror(ifr->ifr_name); */
563 myNetMasks[rxi_numNetAddrs] = 0;
565 myNetMasks[rxi_numNetAddrs] =
566 ntohl(myNetMasks[rxi_numNetAddrs]);
567 /* fprintf(stderr, "if %s mask=0x%x\n",
568 * ifr->ifr_name, myNetMasks[rxi_numNetAddrs]); */
571 if (myNetMTUs[rxi_numNetAddrs] == 0) {
572 myNetMTUs[rxi_numNetAddrs] = OLD_MAX_PACKET_SIZE + RX_IPUDP_SIZE;
574 res = ioctl(s, SIOCGIFMTU, ifr);
575 if ((res == 0) && (ifr->ifr_metric > 128)) { /* sanity check */
576 myNetMTUs[rxi_numNetAddrs] = ifr->ifr_metric;
577 /* fprintf(stderr, "if %s mtu=%d\n",
578 * ifr->ifr_name, ifr->ifr_metric); */
580 /* fputs(stderr, "ioctl error IFMTU\n");
581 * perror(ifr->ifr_name); */
586 if (myNetMasks[rxi_numNetAddrs] == 0) {
587 myNetMasks[rxi_numNetAddrs] =
588 fudge_netmask(rxi_NetAddrs[rxi_numNetAddrs]);
589 #ifdef SIOCGIFNETMASK
590 res = ioctl(s, SIOCGIFNETMASK, ifr);
592 a = (struct sockaddr_in *)&ifr->ifr_addr;
593 myNetMasks[rxi_numNetAddrs] = ntohl(a->sin_addr.s_addr);
594 /* fprintf(stderr, "if %s subnetmask=0x%x\n",
595 * ifr->ifr_name, myNetMasks[rxi_numNetAddrs]); */
597 /* fputs(stderr, "ioctl error IFMASK\n");
598 * perror(ifr->ifr_name); */
603 if (rxi_NetAddrs[rxi_numNetAddrs] != 0x7f000001) { /* ignore lo0 */
606 rxi_nRecvFrags * (myNetMTUs[rxi_numNetAddrs] - RX_IP_SIZE);
607 maxsize -= UDP_HDR_SIZE; /* only the first frag has a UDP hdr */
608 if (rx_maxReceiveSize < maxsize)
609 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxsize);
616 /* have to allocate at least enough to allow a single packet to reach its
617 * maximum size, so ReadPacket will work. Allocate enough for a couple
618 * of packets to do so, for good measure */
620 int npackets, ncbufs;
622 rx_maxJumboRecvSize =
623 RX_HEADER_SIZE + rxi_nDgramPackets * RX_JUMBOBUFFERSIZE +
624 (rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE;
625 rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
626 ncbufs = (rx_maxJumboRecvSize - RX_FIRSTBUFFERSIZE);
628 ncbufs = ncbufs / RX_CBUFFERSIZE;
629 npackets = rx_initSendWindow - 1;
630 rxi_MorePackets(npackets * (ncbufs + 1));
634 #endif /* AFS_NT40_ENV */
636 /* Called from rxi_FindPeer, when initializing a clear rx_peer structure,
637 * to get interesting information.
638 * Curiously enough, the rx_peerHashTable_lock currently protects the
639 * Inited variable (and hence rx_GetIFInfo). When the fs suite uses
640 * pthreads, this issue will need to be revisited.
644 rxi_InitPeerParams(struct rx_peer *pp)
649 #if defined(ADAPT_PMTU) && defined(IP_MTU)
651 struct sockaddr_in addr;
658 * there's a race here since more than one thread could call
659 * rx_GetIFInfo. The race stops in rx_GetIFInfo.
667 /* try to second-guess IP, and identify which link is most likely to
668 * be used for traffic to/from this host. */
669 ppaddr = ntohl(pp->host);
673 pp->rateFlag = 2; /* start timing after two full packets */
674 /* I don't initialize these, because I presume they are bzero'd...
675 * pp->burstSize pp->burst pp->burstWait.sec pp->burstWait.usec
676 * pp->timeout.usec */
679 for (ix = 0; ix < rxi_numNetAddrs; ++ix) {
680 if ((rxi_NetAddrs[ix] & myNetMasks[ix]) == (ppaddr & myNetMasks[ix])) {
681 #ifdef IFF_POINTOPOINT
682 if (myNetFlags[ix] & IFF_POINTOPOINT)
684 #endif /* IFF_POINTOPOINT */
685 rxmtu = myNetMTUs[ix] - RX_IPUDP_SIZE;
686 if (rxmtu < RX_MIN_PACKET_SIZE)
687 rxmtu = RX_MIN_PACKET_SIZE;
688 if (pp->ifMTU < rxmtu)
689 pp->ifMTU = MIN(rx_MyMaxSendSize, rxmtu);
693 if (!pp->ifMTU) { /* not local */
695 pp->ifMTU = MIN(rx_MyMaxSendSize, RX_REMOTE_PACKET_SIZE);
697 #else /* ADAPT_MTU */
698 pp->rateFlag = 2; /* start timing after two full packets */
700 pp->ifMTU = MIN(rx_MyMaxSendSize, OLD_MAX_PACKET_SIZE);
701 #endif /* ADAPT_MTU */
702 #if defined(ADAPT_PMTU) && defined(IP_MTU)
703 sock=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
705 addr.sin_family = AF_INET;
706 addr.sin_addr.s_addr = pp->host;
707 addr.sin_port = pp->port;
708 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0) {
710 socklen_t s = sizeof(mtu);
711 if (getsockopt(sock, SOL_IP, IP_MTU, &mtu, &s)== 0) {
712 pp->ifMTU = MIN(mtu - RX_IPUDP_SIZE, pp->ifMTU);
718 pp->ifMTU = rxi_AdjustIfMTU(pp->ifMTU);
719 pp->maxMTU = OLD_MAX_PACKET_SIZE; /* for compatibility with old guys */
720 pp->natMTU = MIN((int)pp->ifMTU, OLD_MAX_PACKET_SIZE);
721 pp->maxDgramPackets =
722 MIN(rxi_nDgramPackets,
723 rxi_AdjustDgramPackets(rxi_nSendFrags, pp->ifMTU));
725 MIN(rxi_nDgramPackets,
726 rxi_AdjustDgramPackets(rxi_nSendFrags, pp->ifMTU));
727 pp->maxDgramPackets = 1;
728 /* Initialize slow start parameters */
729 pp->MTU = MIN(pp->natMTU, pp->maxMTU);
731 pp->nDgramPackets = 1;
735 /* Don't expose jumobgram internals. */
739 rx_maxReceiveSize = OLD_MAX_PACKET_SIZE;
740 rxi_nSendFrags = rxi_nRecvFrags = 1;
743 /* Override max MTU. If rx_SetNoJumbo is called, it must be
744 called before calling rx_SetMaxMTU since SetNoJumbo clobbers rx_maxReceiveSize */
746 rx_SetMaxMTU(int mtu)
748 rx_MyMaxSendSize = rx_maxReceiveSizeUser = rx_maxReceiveSize = mtu;
751 #if defined(HAVE_LINUX_ERRQUEUE_H) && defined(ADAPT_PMTU)
753 rxi_HandleSocketError(int socket)
756 struct cmsghdr *cmsg;
757 struct sock_extended_err *err;
758 struct sockaddr_in addr;
759 struct sockaddr *offender;
760 char controlmsgbuf[256];
764 msg.msg_name = &addr;
765 msg.msg_namelen = sizeof(addr);
768 msg.msg_control = controlmsgbuf;
769 msg.msg_controllen = 256;
771 code = recvmsg(socket, &msg, MSG_ERRQUEUE|MSG_DONTWAIT|MSG_TRUNC);
773 if (code < 0 || !(msg.msg_flags & MSG_ERRQUEUE))
776 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
777 if ((char *)cmsg - controlmsgbuf > msg.msg_controllen - CMSG_SPACE(0) ||
778 (char *)cmsg - controlmsgbuf > msg.msg_controllen - CMSG_SPACE(cmsg->cmsg_len) ||
779 cmsg->cmsg_len == 0) {
783 if (cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_RECVERR)
789 err =(struct sock_extended_err *) CMSG_DATA(cmsg);
791 if (err->ee_errno == EMSGSIZE && err->ee_info >= 68) {
792 rxi_SetPeerMtu(addr.sin_addr.s_addr, addr.sin_port,
793 err->ee_info - RX_IPUDP_SIZE);
795 /* other DEST_UNREACH's and TIME_EXCEEDED should be dealt with too */