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 /* rxi_syscall is currently not prototyped */
14 #include <afsconfig.h>
15 #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>
36 #if !defined(AFS_AIX_ENV) && !defined(AFS_NT40_ENV)
37 # include <sys/syscall.h>
39 #include <afs/afs_args.h>
40 #include <afs/afsutil.h>
42 #ifndef IPPORT_USERRESERVED
43 /* If in.h doesn't define this, define it anyway. Unfortunately, defining
44 this doesn't put the code into the kernel to restrict kernel assigned
45 port numbers to numbers below IPPORT_USERRESERVED... */
46 #define IPPORT_USERRESERVED 5000
49 #if defined(HAVE_LINUX_ERRQUEUE_H) && defined(ADAPT_PMTU)
50 #include <linux/types.h>
51 #include <linux/errqueue.h>
58 # include <sys/time.h>
61 # include "rx_globals.h"
63 #ifdef AFS_PTHREAD_ENV
67 * The rx_if_init_mutex mutex protects the following global variables:
71 afs_kmutex_t rx_if_init_mutex;
72 #define LOCK_IF_INIT MUTEX_ENTER(&rx_if_init_mutex)
73 #define UNLOCK_IF_INIT MUTEX_EXIT(&rx_if_init_mutex)
76 * The rx_if_mutex mutex protects the following global variables:
82 afs_kmutex_t rx_if_mutex;
83 #define LOCK_IF MUTEX_ENTER(&rx_if_mutex)
84 #define UNLOCK_IF MUTEX_EXIT(&rx_if_mutex)
87 #define UNLOCK_IF_INIT
90 #endif /* AFS_PTHREAD_ENV */
94 * Make a socket for receiving/sending IP packets. Set it into non-blocking
95 * and large buffering modes. If port isn't specified, the kernel will pick
96 * one. Returns the socket (>= 0) on success. Returns OSI_NULLSOCKET on
97 * failure. Port must be in network byte order.
100 rxi_GetHostUDPSocket(u_int ahost, u_short port)
103 osi_socket socketFd = OSI_NULLSOCKET;
104 struct sockaddr_in taddr;
105 char *name = "rxi_GetUDPSocket: ";
106 #ifdef AFS_LINUX22_ENV
107 #if defined(ADAPT_PMTU)
108 int pmtu=IP_PMTUDISC_WANT;
111 int pmtu=IP_PMTUDISC_DONT;
115 #if !defined(AFS_NT40_ENV)
116 if (ntohs(port) >= IPPORT_RESERVED && ntohs(port) < IPPORT_USERRESERVED) {
117 /* (osi_Msg "%s*WARNING* port number %d is not a reserved port number. Use port numbers above %d\n", name, port, IPPORT_USERRESERVED);
120 if (ntohs(port) > 0 && ntohs(port) < IPPORT_RESERVED && geteuid() != 0) {
122 "%sport number %d is a reserved port number which may only be used by root. Use port numbers above %d\n",
123 name, ntohs(port), IPPORT_USERRESERVED);
127 socketFd = socket(AF_INET, SOCK_DGRAM, 0);
135 rxi_xmit_init(socketFd);
136 #endif /* AFS_NT40_ENV */
138 taddr.sin_addr.s_addr = ahost;
139 taddr.sin_family = AF_INET;
140 taddr.sin_port = (u_short) port;
141 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
142 taddr.sin_len = sizeof(struct sockaddr_in);
144 #define MAX_RX_BINDS 10
145 for (binds = 0; binds < MAX_RX_BINDS; binds++) {
148 code = bind(socketFd, (struct sockaddr *)&taddr, sizeof(taddr));
154 (osi_Msg "%sbind failed\n", name);
157 #if !defined(AFS_NT40_ENV)
159 * Set close-on-exec on rx socket
161 fcntl(socketFd, F_SETFD, 1);
164 /* Use one of three different ways of getting a socket buffer expanded to
172 len2 = rx_UdpBufSize;
174 /* find the size closest to rx_UdpBufSize that will be accepted */
175 while (!greedy && len2 > len1) {
178 (socketFd, SOL_SOCKET, SO_RCVBUF, (char *)&len2,
184 /* but do not let it get smaller than 32K */
194 (socketFd, SOL_SOCKET, SO_SNDBUF, (char *)&len1,
198 (socketFd, SOL_SOCKET, SO_RCVBUF, (char *)&len2,
201 (osi_Msg "%s*WARNING* Unable to increase buffering on socket\n",
203 if (rx_stats_active) {
204 MUTEX_ENTER(&rx_stats_mutex);
205 rx_stats.socketGreedy = greedy;
206 MUTEX_EXIT(&rx_stats_mutex);
210 #ifdef AFS_LINUX22_ENV
211 setsockopt(socketFd, SOL_IP, IP_MTU_DISCOVER, &pmtu, sizeof(pmtu));
212 #if defined(ADAPT_PMTU)
213 setsockopt(socketFd, SOL_IP, IP_RECVERR, &recverr, sizeof(recverr));
216 if (rxi_Listen(socketFd) < 0) {
225 closesocket(socketFd);
231 return OSI_NULLSOCKET;
235 rxi_GetUDPSocket(u_short port)
237 return rxi_GetHostUDPSocket(htonl(INADDR_ANY), port);
241 osi_Panic(char *msg, ...)
245 (osi_Msg "Fatal Rx error: ");
254 * osi_AssertFailU() -- used by the osi_Assert() macro.
258 osi_AssertFailU(const char *expr, const char *file, int line)
260 osi_Panic("assertion failed: %s, file: %s, line: %d\n", expr,
264 #if defined(AFS_AIX32_ENV) && !defined(KERNEL)
266 static const char memZero;
268 osi_Alloc(afs_int32 x)
271 * 0-length allocs may return NULL ptr from malloc, so we special-case
272 * things so that NULL returned iff an error occurred
275 return (void *)&memZero;
280 osi_Free(void *x, afs_int32 size)
287 #endif /* defined(AFS_AIX32_ENV) && !defined(KERNEL) */
289 #define ADDRSPERSITE 16
292 static afs_uint32 rxi_NetAddrs[ADDRSPERSITE]; /* host order */
293 static int myNetMTUs[ADDRSPERSITE];
294 static int myNetMasks[ADDRSPERSITE];
295 static int myNetFlags[ADDRSPERSITE];
296 static u_int rxi_numNetAddrs;
297 static int Inited = 0;
299 #if defined(AFS_NT40_ENV)
303 /* The IP address list can change so we must query for it */
306 /* we don't want to use the loopback adapter which is first */
307 /* this is a bad bad hack */
308 if (rxi_numNetAddrs > 1)
309 return htonl(rxi_NetAddrs[1]);
310 else if (rxi_numNetAddrs > 0)
311 return htonl(rxi_NetAddrs[0]);
317 ** return number of addresses
318 ** and the addresses themselves in the buffer
319 ** maxSize - max number of interfaces to return.
322 rx_getAllAddr(afs_uint32 * buffer, int maxSize)
324 int count = 0, offset = 0;
326 /* The IP address list can change so we must query for it */
329 for (count = 0; offset < rxi_numNetAddrs && maxSize > 0;
330 count++, offset++, maxSize--)
331 buffer[count] = htonl(rxi_NetAddrs[offset]);
336 /* this function returns the total number of interface addresses
337 * the buffer has to be passed in by the caller. It also returns
338 * the matching interface mask and mtu. All values are returned
339 * in network byte order.
342 rx_getAllAddrMaskMtu(afs_uint32 addrBuffer[], afs_uint32 maskBuffer[],
343 afs_uint32 mtuBuffer[], int maxSize)
345 int count = 0, offset = 0;
347 /* The IP address list can change so we must query for it */
351 offset < rxi_numNetAddrs && maxSize > 0;
352 count++, offset++, maxSize--) {
353 addrBuffer[count] = htonl(rxi_NetAddrs[offset]);
354 maskBuffer[count] = htonl(myNetMasks[offset]);
355 mtuBuffer[count] = htonl(myNetMTUs[offset]);
362 extern int rxinit_status;
364 rxi_InitMorePackets(void) {
365 int npackets, ncbufs;
367 ncbufs = (rx_maxJumboRecvSize - RX_FIRSTBUFFERSIZE);
369 ncbufs = ncbufs / RX_CBUFFERSIZE;
370 npackets = rx_initSendWindow - 1;
371 rxi_MorePackets(npackets * (ncbufs + 1));
383 if (Inited < 2 && rxinit_status == 0) {
384 /* We couldn't initialize more packets earlier.
386 rxi_InitMorePackets();
396 rxi_numNetAddrs = ADDRSPERSITE;
397 (void)syscfg_GetIFInfo(&rxi_numNetAddrs, rxi_NetAddrs,
398 myNetMasks, myNetMTUs, myNetFlags);
400 for (i = 0; i < rxi_numNetAddrs; i++) {
401 rxsize = rxi_AdjustIfMTU(myNetMTUs[i] - RX_IPUDP_SIZE);
403 rxi_nRecvFrags * rxsize + (rxi_nRecvFrags - 1) * UDP_HDR_SIZE;
404 maxsize = rxi_AdjustMaxMTU(rxsize, maxsize);
405 if (rx_maxReceiveSize > maxsize) {
406 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxsize);
408 MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
410 if (rx_MyMaxSendSize > maxsize) {
411 rx_MyMaxSendSize = MIN(RX_MAX_PACKET_SIZE, maxsize);
417 * If rxinit_status is still set, rx_InitHost() has yet to be called
418 * and we therefore do not have any mutex locks initialized. As a
419 * result we cannot call rxi_MorePackets() without crashing.
424 rxi_InitMorePackets();
429 fudge_netmask(afs_uint32 addr)
435 else if (IN_CLASSB(addr))
437 else if (IN_CLASSC(addr))
447 #if !defined(AFS_AIX_ENV) && !defined(AFS_NT40_ENV) && !defined(AFS_LINUX20_ENV)
449 rxi_syscall(afs_uint32 a3, afs_uint32 a4, void *a5)
454 old = signal(SIGSYS, SIG_IGN);
456 #if defined(AFS_SGI_ENV)
457 rcode = afs_syscall(AFS_SYSCALL, 28, a3, a4, a5);
459 rcode = syscall(AFS_SYSCALL, 28 /* AFSCALL_CALL */ , a3, a4, a5);
460 #endif /* AFS_SGI_ENV */
466 #endif /* AFS_AIX_ENV */
475 struct ifreq ifs[ADDRSPERSITE];
478 char buf[BUFSIZ], *cp, *cplim;
480 struct sockaddr_in *a;
491 memset(rxi_NetAddrs, 0, sizeof(rxi_NetAddrs));
492 memset(myNetFlags, 0, sizeof(myNetFlags));
493 memset(myNetMTUs, 0, sizeof(myNetMTUs));
494 memset(myNetMasks, 0, sizeof(myNetMasks));
496 s = socket(AF_INET, SOCK_DGRAM, 0);
501 ifc.ifc_len = sizeof(buf);
505 ifc.ifc_len = sizeof(ifs);
506 ifc.ifc_buf = (caddr_t) & ifs[0];
507 memset(&ifs[0], 0, sizeof(ifs));
509 res = ioctl(s, SIOCGIFCONF, &ifc);
511 /* fputs(stderr, "ioctl error IFCONF\n"); */
518 #define size(p) MAX((p).sa_len, sizeof(p))
519 cplim = buf + ifc.ifc_len; /*skip over if's with big ifr_addr's */
520 for (cp = buf; cp < cplim;
521 cp += sizeof(ifr->ifr_name) + MAX(a->sin_len, sizeof(*a))) {
522 if (rxi_numNetAddrs >= ADDRSPERSITE)
525 ifr = (struct ifreq *)cp;
527 len = ifc.ifc_len / sizeof(struct ifreq);
528 if (len > ADDRSPERSITE)
531 for (i = 0; i < len; ++i) {
533 res = ioctl(s, SIOCGIFADDR, ifr);
536 /* fputs(stderr, "ioctl error IFADDR\n");
537 * perror(ifr->ifr_name); */
540 a = (struct sockaddr_in *)&ifr->ifr_addr;
541 if (a->sin_family != AF_INET)
543 rxi_NetAddrs[rxi_numNetAddrs] = ntohl(a->sin_addr.s_addr);
544 if (rx_IsLoopbackAddr(rxi_NetAddrs[rxi_numNetAddrs])) {
545 /* we don't really care about "localhost" */
548 for (j = 0; j < rxi_numNetAddrs; j++) {
549 if (rxi_NetAddrs[j] == rxi_NetAddrs[rxi_numNetAddrs])
552 if (j < rxi_numNetAddrs)
555 /* fprintf(stderr, "if %s addr=%x\n", ifr->ifr_name,
556 * rxi_NetAddrs[rxi_numNetAddrs]); */
559 res = ioctl(s, SIOCGIFFLAGS, ifr);
561 myNetFlags[rxi_numNetAddrs] = ifr->ifr_flags;
563 /* Handle aliased loopbacks as well. */
564 if (ifr->ifr_flags & IFF_LOOPBACK)
567 /* fprintf(stderr, "if %s flags=%x\n",
568 * ifr->ifr_name, ifr->ifr_flags); */
570 * fputs(stderr, "ioctl error IFFLAGS\n");
571 * perror(ifr->ifr_name); */
573 #endif /* SIOCGIFFLAGS */
575 #if !defined(AFS_AIX_ENV) && !defined(AFS_LINUX20_ENV)
576 /* this won't run on an AIX system w/o a cache manager */
577 rxi_syscallp = rxi_syscall;
580 /* If I refer to kernel extensions that aren't loaded on AIX, the
581 * program refuses to load and run, so I simply can't include the
582 * following code. Fortunately, AIX is the one operating system in
583 * which the subsequent ioctl works reliably. */
585 if ((*rxi_syscallp) (20 /*AFSOP_GETMTU */ ,
586 htonl(rxi_NetAddrs[rxi_numNetAddrs]),
587 &(myNetMTUs[rxi_numNetAddrs]))) {
588 /* fputs(stderr, "syscall error GETMTU\n");
589 * perror(ifr->ifr_name); */
590 myNetMTUs[rxi_numNetAddrs] = 0;
592 if ((*rxi_syscallp) (42 /*AFSOP_GETMASK */ ,
593 htonl(rxi_NetAddrs[rxi_numNetAddrs]),
594 &(myNetMasks[rxi_numNetAddrs]))) {
595 /* fputs(stderr, "syscall error GETMASK\n");
596 * perror(ifr->ifr_name); */
597 myNetMasks[rxi_numNetAddrs] = 0;
599 myNetMasks[rxi_numNetAddrs] =
600 ntohl(myNetMasks[rxi_numNetAddrs]);
601 /* fprintf(stderr, "if %s mask=0x%x\n",
602 * ifr->ifr_name, myNetMasks[rxi_numNetAddrs]); */
605 if (myNetMTUs[rxi_numNetAddrs] == 0) {
606 myNetMTUs[rxi_numNetAddrs] = OLD_MAX_PACKET_SIZE + RX_IPUDP_SIZE;
608 res = ioctl(s, SIOCGIFMTU, ifr);
609 if ((res == 0) && (ifr->ifr_metric > 128)) { /* sanity check */
610 myNetMTUs[rxi_numNetAddrs] = ifr->ifr_metric;
611 /* fprintf(stderr, "if %s mtu=%d\n",
612 * ifr->ifr_name, ifr->ifr_metric); */
614 /* fputs(stderr, "ioctl error IFMTU\n");
615 * perror(ifr->ifr_name); */
620 if (myNetMasks[rxi_numNetAddrs] == 0) {
621 myNetMasks[rxi_numNetAddrs] =
622 fudge_netmask(rxi_NetAddrs[rxi_numNetAddrs]);
623 #ifdef SIOCGIFNETMASK
624 res = ioctl(s, SIOCGIFNETMASK, ifr);
626 a = (struct sockaddr_in *)&ifr->ifr_addr;
627 myNetMasks[rxi_numNetAddrs] = ntohl(a->sin_addr.s_addr);
628 /* fprintf(stderr, "if %s subnetmask=0x%x\n",
629 * ifr->ifr_name, myNetMasks[rxi_numNetAddrs]); */
631 /* fputs(stderr, "ioctl error IFMASK\n");
632 * perror(ifr->ifr_name); */
637 if (!rx_IsLoopbackAddr(rxi_NetAddrs[rxi_numNetAddrs])) { /* ignore lo0 */
640 rxi_nRecvFrags * (myNetMTUs[rxi_numNetAddrs] - RX_IP_SIZE);
641 maxsize -= UDP_HDR_SIZE; /* only the first frag has a UDP hdr */
642 if (rx_maxReceiveSize < maxsize)
643 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxsize);
650 /* have to allocate at least enough to allow a single packet to reach its
651 * maximum size, so ReadPacket will work. Allocate enough for a couple
652 * of packets to do so, for good measure */
654 int npackets, ncbufs;
656 rx_maxJumboRecvSize =
657 RX_HEADER_SIZE + rxi_nDgramPackets * RX_JUMBOBUFFERSIZE +
658 (rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE;
659 rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
660 ncbufs = (rx_maxJumboRecvSize - RX_FIRSTBUFFERSIZE);
662 ncbufs = ncbufs / RX_CBUFFERSIZE;
663 npackets = rx_initSendWindow - 1;
664 rxi_MorePackets(npackets * (ncbufs + 1));
668 #endif /* AFS_NT40_ENV */
670 /* Called from rxi_FindPeer, when initializing a clear rx_peer structure,
671 * to get interesting information.
672 * Curiously enough, the rx_peerHashTable_lock currently protects the
673 * Inited variable (and hence rx_GetIFInfo). When the fs suite uses
674 * pthreads, this issue will need to be revisited.
678 rxi_InitPeerParams(struct rx_peer *pp)
683 #if defined(ADAPT_PMTU) && defined(IP_MTU)
685 struct sockaddr_in addr;
694 * there's a race here since more than one thread could call
695 * rx_GetIFInfo. The race stops in rx_GetIFInfo.
703 /* try to second-guess IP, and identify which link is most likely to
704 * be used for traffic to/from this host. */
705 ppaddr = ntohl(pp->host);
709 pp->rateFlag = 2; /* start timing after two full packets */
710 /* I don't initialize these, because I presume they are bzero'd...
711 * pp->burstSize pp->burst pp->burstWait.sec pp->burstWait.usec
712 * pp->timeout.usec */
715 for (ix = 0; ix < rxi_numNetAddrs; ++ix) {
716 if ((rxi_NetAddrs[ix] & myNetMasks[ix]) == (ppaddr & myNetMasks[ix])) {
717 #ifdef IFF_POINTOPOINT
718 if (myNetFlags[ix] & IFF_POINTOPOINT)
720 #endif /* IFF_POINTOPOINT */
721 rxmtu = myNetMTUs[ix] - RX_IPUDP_SIZE;
722 if (rxmtu < RX_MIN_PACKET_SIZE)
723 rxmtu = RX_MIN_PACKET_SIZE;
724 if (pp->ifMTU < rxmtu)
725 pp->ifMTU = MIN(rx_MyMaxSendSize, rxmtu);
729 if (!pp->ifMTU) { /* not local */
731 pp->ifMTU = MIN(rx_MyMaxSendSize, RX_REMOTE_PACKET_SIZE);
733 #else /* ADAPT_MTU */
734 pp->rateFlag = 2; /* start timing after two full packets */
736 pp->ifMTU = MIN(rx_MyMaxSendSize, OLD_MAX_PACKET_SIZE);
737 #endif /* ADAPT_MTU */
738 #if defined(ADAPT_PMTU) && defined(IP_MTU)
739 sock=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
741 addr.sin_family = AF_INET;
742 addr.sin_addr.s_addr = pp->host;
743 addr.sin_port = pp->port;
744 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0) {
746 socklen_t s = sizeof(mtu);
747 if (getsockopt(sock, SOL_IP, IP_MTU, &mtu, &s)== 0) {
748 pp->ifMTU = MIN(mtu - RX_IPUDP_SIZE, pp->ifMTU);
754 pp->ifMTU = rxi_AdjustIfMTU(pp->ifMTU);
755 pp->maxMTU = OLD_MAX_PACKET_SIZE; /* for compatibility with old guys */
756 pp->natMTU = MIN((int)pp->ifMTU, OLD_MAX_PACKET_SIZE);
757 pp->maxDgramPackets =
758 MIN(rxi_nDgramPackets,
759 rxi_AdjustDgramPackets(rxi_nSendFrags, pp->ifMTU));
761 MIN(rxi_nDgramPackets,
762 rxi_AdjustDgramPackets(rxi_nSendFrags, pp->ifMTU));
763 pp->maxDgramPackets = 1;
764 /* Initialize slow start parameters */
765 pp->MTU = MIN(pp->natMTU, pp->maxMTU);
767 pp->nDgramPackets = 1;
771 /* Don't expose jumobgram internals. */
775 rx_maxReceiveSize = OLD_MAX_PACKET_SIZE;
776 rxi_nSendFrags = rxi_nRecvFrags = 1;
779 /* Override max MTU. If rx_SetNoJumbo is called, it must be
780 called before calling rx_SetMaxMTU since SetNoJumbo clobbers rx_maxReceiveSize */
782 rx_SetMaxMTU(int mtu)
784 rx_MyMaxSendSize = rx_maxReceiveSizeUser = rx_maxReceiveSize = mtu;
787 #if defined(HAVE_LINUX_ERRQUEUE_H) && defined(ADAPT_PMTU)
789 rxi_HandleSocketError(int socket)
792 struct cmsghdr *cmsg;
793 struct sock_extended_err *err;
794 struct sockaddr_in addr;
795 struct sockaddr *offender;
796 char controlmsgbuf[256];
800 msg.msg_name = &addr;
801 msg.msg_namelen = sizeof(addr);
804 msg.msg_control = controlmsgbuf;
805 msg.msg_controllen = 256;
807 code = recvmsg(socket, &msg, MSG_ERRQUEUE|MSG_DONTWAIT|MSG_TRUNC);
809 if (code < 0 || !(msg.msg_flags & MSG_ERRQUEUE))
812 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
813 if ((char *)cmsg - controlmsgbuf > msg.msg_controllen - CMSG_SPACE(0) ||
814 (char *)cmsg - controlmsgbuf > msg.msg_controllen - CMSG_SPACE(cmsg->cmsg_len) ||
815 cmsg->cmsg_len == 0) {
819 if (cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_RECVERR)
825 err =(struct sock_extended_err *) CMSG_DATA(cmsg);
827 if (err->ee_errno == EMSGSIZE && err->ee_info >= 68) {
828 rxi_SetPeerMtu(NULL, addr.sin_addr.s_addr, addr.sin_port,
829 err->ee_info - RX_IPUDP_SIZE);
831 /* other DEST_UNREACH's and TIME_EXCEEDED should be dealt with too */