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_GetUDPSocket(u_short port)
100 osi_socket socketFd = OSI_NULLSOCKET;
101 struct sockaddr_in taddr;
102 char *name = "rxi_GetUDPSocket: ";
105 #if !defined(AFS_NT40_ENV) && !defined(AFS_DJGPP_ENV)
106 if (ntohs(port) >= IPPORT_RESERVED && ntohs(port) < IPPORT_USERRESERVED) {
107 /* (osi_Msg "%s*WARNING* port number %d is not a reserved port number. Use port numbers above %d\n", name, port, IPPORT_USERRESERVED);
110 if (ntohs(port) > 0 && ntohs(port) < IPPORT_RESERVED && geteuid() != 0) {
112 "%sport number %d is a reserved port number which may only be used by root. Use port numbers above %d\n",
113 name, ntohs(port), IPPORT_USERRESERVED);
117 socketFd = socket(AF_INET, SOCK_DGRAM, 0);
124 taddr.sin_addr.s_addr = INADDR_ANY;
125 taddr.sin_family = AF_INET;
126 taddr.sin_port = (u_short) port;
127 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
128 taddr.sin_len = sizeof(struct sockaddr_in);
130 #define MAX_RX_BINDS 10
131 for (binds = 0; binds < MAX_RX_BINDS; binds++) {
134 code = bind(socketFd, (struct sockaddr *)&taddr, sizeof(taddr));
140 (osi_Msg "%sbind failed\n", name);
143 #if !defined(AFS_NT40_ENV) && !defined(AFS_DJGPP_ENV)
145 * Set close-on-exec on rx socket
147 fcntl(socketFd, F_SETFD, 1);
150 /* Use one of three different ways of getting a socket buffer expanded to
157 len2 = rx_UdpBufSize;
158 #ifndef AFS_DJGPP_ENV
161 (socketFd, SOL_SOCKET, SO_RCVBUF, (char *)&len2,
164 len2 = 32766; /* fall back to old size... uh-oh! */
169 (socketFd, SOL_SOCKET, SO_SNDBUF, (char *)&len1,
173 (socketFd, SOL_SOCKET, SO_RCVBUF, (char *)&len2,
175 #endif /* AFS_DJGPP_ENV */
178 #ifndef AFS_DJGPP_ENV
180 (osi_Msg "%s*WARNING* Unable to increase buffering on socket\n",
182 #endif /* AFS_DJGPP_ENV */
183 if (rxi_Listen(socketFd) < 0) {
192 closesocket(socketFd);
198 MUTEX_ENTER(&rx_stats_mutex);
199 rx_stats.socketGreedy = greedy;
200 MUTEX_EXIT(&rx_stats_mutex);
201 return OSI_NULLSOCKET;
205 osi_Panic(char *msg, int a1, int a2, int a3)
207 (osi_Msg "Fatal Rx error: ");
208 (osi_Msg msg, a1, a2, a3);
215 * osi_AssertFailU() -- used by the osi_Assert() macro.
219 osi_AssertFailU(const char *expr, const char *file, int line)
221 osi_Panic("assertion failed: %s, file: %s, line: %d\n", (int)expr,
227 static const char memZero;
229 osi_Alloc(afs_int32 x)
232 * 0-length allocs may return NULL ptr from osi_kalloc, so we special-case
233 * things so that NULL returned iff an error occurred
237 return ((char *)malloc(x));
241 osi_Free(char *x, afs_int32 size)
248 #endif /* AFS_AIX32_ENV */
250 #define ADDRSPERSITE 16
253 afs_uint32 rxi_NetAddrs[ADDRSPERSITE]; /* host order */
254 static int myNetMTUs[ADDRSPERSITE];
255 static int myNetMasks[ADDRSPERSITE];
256 static int myNetFlags[ADDRSPERSITE];
257 u_int rxi_numNetAddrs;
258 static int Inited = 0;
260 #if defined(AFS_NT40_ENV) || defined(AFS_DJGPP_ENV)
264 if (rxi_numNetAddrs > 0)
265 return htonl(rxi_NetAddrs[0]);
271 ** return number of addresses
272 ** and the addresses themselves in the buffer
273 ** maxSize - max number of interfaces to return.
276 rx_getAllAddr(afs_int32 * buffer, int maxSize)
279 for (count = 0; count < rxi_numNetAddrs && maxSize > 0;
281 buffer[count] = htonl(rxi_NetAddrs[count]);
293 LOCK_IF_INIT if (Inited) {
294 UNLOCK_IF_INIT return;
298 int npackets, ncbufs;
302 UNLOCK_IF_INIT rxi_numNetAddrs = ADDRSPERSITE;
304 LOCK_IF(void) syscfg_GetIFInfo(&rxi_numNetAddrs, rxi_NetAddrs,
305 myNetMasks, myNetMTUs, myNetFlags);
307 for (i = 0; i < rxi_numNetAddrs; i++) {
308 rxsize = rxi_AdjustIfMTU(myNetMTUs[i] - RX_IPUDP_SIZE);
310 rxi_nRecvFrags * rxsize + (rxi_nRecvFrags - 1) * UDP_HDR_SIZE;
311 maxsize = rxi_AdjustMaxMTU(rxsize, maxsize);
312 if (rx_maxReceiveSize < maxsize) {
313 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxsize);
315 MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
319 UNLOCK_IF ncbufs = (rx_maxJumboRecvSize - RX_FIRSTBUFFERSIZE);
321 ncbufs = ncbufs / RX_CBUFFERSIZE;
322 npackets = rx_initSendWindow - 1;
323 rxi_MorePackets(npackets * (ncbufs + 1));
331 fudge_netmask(afs_uint32 addr)
337 else if (IN_CLASSB(addr))
339 else if (IN_CLASSC(addr))
349 #if !defined(AFS_AIX_ENV) && !defined(AFS_NT40_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_DJGPP_ENV)
351 rxi_syscall(a3, a4, a5)
358 old = (void (*)())signal(SIGSYS, SIG_IGN);
360 #if defined(AFS_SGI_ENV)
361 rcode = afs_syscall(a3, a4, a5);
363 rcode = syscall(AFS_SYSCALL, 28 /* AFSCALL_CALL */ , a3, a4, a5);
364 #endif /* AFS_SGI_ENV */
370 #endif /* AFS_AIX_ENV */
378 #ifndef AFS_DJGPP_ENV
380 struct ifreq ifs[ADDRSPERSITE];
383 char buf[BUFSIZ], *cp, *cplim;
385 struct sockaddr_in *a;
386 #endif /* AFS_DJGPP_ENV */
388 LOCK_IF_INIT if (Inited) {
389 UNLOCK_IF_INIT return;
392 UNLOCK_IF_INIT LOCK_IF rxi_numNetAddrs = 0;
393 memset(rxi_NetAddrs, 0, sizeof(rxi_NetAddrs));
394 memset(myNetFlags, 0, sizeof(myNetFlags));
395 memset(myNetMTUs, 0, sizeof(myNetMTUs));
396 memset(myNetMasks, 0, sizeof(myNetMasks));
397 UNLOCK_IF s = socket(AF_INET, SOCK_DGRAM, 0);
401 #ifndef AFS_DJGPP_ENV
403 ifc.ifc_len = sizeof(buf);
407 ifc.ifc_len = sizeof(ifs);
408 ifc.ifc_buf = (caddr_t) & ifs[0];
409 memset(&ifs[0], 0, sizeof(ifs));
411 res = ioctl(s, SIOCGIFCONF, &ifc);
413 /* fputs(stderr, "ioctl error IFCONF\n"); */
420 #define size(p) MAX((p).sa_len, sizeof(p))
421 cplim = buf + ifc.ifc_len; /*skip over if's with big ifr_addr's */
422 for (cp = buf; cp < cplim;
423 cp += sizeof(ifr->ifr_name) + MAX(a->sin_len, sizeof(*a))) {
424 if (rxi_numNetAddrs >= ADDRSPERSITE)
427 ifr = (struct ifreq *)cp;
429 len = ifc.ifc_len / sizeof(struct ifreq);
430 if (len > ADDRSPERSITE)
433 for (i = 0; i < len; ++i) {
435 res = ioctl(s, SIOCGIFADDR, ifr);
438 /* fputs(stderr, "ioctl error IFADDR\n");
439 * perror(ifr->ifr_name); */
442 a = (struct sockaddr_in *)&ifr->ifr_addr;
443 if (a->sin_family != AF_INET)
445 rxi_NetAddrs[rxi_numNetAddrs] = ntohl(a->sin_addr.s_addr);
446 if (rxi_NetAddrs[rxi_numNetAddrs] == 0x7f000001) {
447 /* we don't really care about "localhost" */
450 for (j = 0; j < rxi_numNetAddrs; j++) {
451 if (rxi_NetAddrs[j] == rxi_NetAddrs[rxi_numNetAddrs])
454 if (j < rxi_numNetAddrs)
457 /* fprintf(stderr, "if %s addr=%x\n", ifr->ifr_name,
458 * rxi_NetAddrs[rxi_numNetAddrs]); */
461 res = ioctl(s, SIOCGIFFLAGS, ifr);
463 myNetFlags[rxi_numNetAddrs] = ifr->ifr_flags;
465 /* Handle aliased loopbacks as well. */
466 if (ifr->ifr_flags & IFF_LOOPBACK)
469 /* fprintf(stderr, "if %s flags=%x\n",
470 * ifr->ifr_name, ifr->ifr_flags); */
472 * fputs(stderr, "ioctl error IFFLAGS\n");
473 * perror(ifr->ifr_name); */
475 #endif /* SIOCGIFFLAGS */
477 #if !defined(AFS_AIX_ENV) && !defined(AFS_LINUX20_ENV)
478 /* this won't run on an AIX system w/o a cache manager */
479 rxi_syscallp = rxi_syscall;
482 /* If I refer to kernel extensions that aren't loaded on AIX, the
483 * program refuses to load and run, so I simply can't include the
484 * following code. Fortunately, AIX is the one operating system in
485 * which the subsequent ioctl works reliably. */
487 if ((*rxi_syscallp) (20 /*AFSOP_GETMTU */ ,
488 htonl(rxi_NetAddrs[rxi_numNetAddrs]),
489 &(myNetMTUs[rxi_numNetAddrs]))) {
490 /* fputs(stderr, "syscall error GETMTU\n");
491 * perror(ifr->ifr_name); */
492 myNetMTUs[rxi_numNetAddrs] = 0;
494 if ((*rxi_syscallp) (42 /*AFSOP_GETMASK */ ,
495 htonl(rxi_NetAddrs[rxi_numNetAddrs]),
496 &(myNetMasks[rxi_numNetAddrs]))) {
497 /* fputs(stderr, "syscall error GETMASK\n");
498 * perror(ifr->ifr_name); */
499 myNetMasks[rxi_numNetAddrs] = 0;
501 myNetMasks[rxi_numNetAddrs] =
502 ntohl(myNetMasks[rxi_numNetAddrs]);
503 /* fprintf(stderr, "if %s mask=0x%x\n",
504 * ifr->ifr_name, myNetMasks[rxi_numNetAddrs]); */
507 if (myNetMTUs[rxi_numNetAddrs] == 0) {
508 myNetMTUs[rxi_numNetAddrs] = OLD_MAX_PACKET_SIZE + RX_IPUDP_SIZE;
510 res = ioctl(s, SIOCGIFMTU, ifr);
511 if ((res == 0) && (ifr->ifr_metric > 128)) { /* sanity check */
512 myNetMTUs[rxi_numNetAddrs] = ifr->ifr_metric;
513 /* fprintf(stderr, "if %s mtu=%d\n",
514 * ifr->ifr_name, ifr->ifr_metric); */
516 /* fputs(stderr, "ioctl error IFMTU\n");
517 * perror(ifr->ifr_name); */
522 if (myNetMasks[rxi_numNetAddrs] == 0) {
523 myNetMasks[rxi_numNetAddrs] =
524 fudge_netmask(rxi_NetAddrs[rxi_numNetAddrs]);
525 #ifdef SIOCGIFNETMASK
526 res = ioctl(s, SIOCGIFNETMASK, ifr);
528 a = (struct sockaddr_in *)&ifr->ifr_addr;
529 myNetMasks[rxi_numNetAddrs] = ntohl(a->sin_addr.s_addr);
530 /* fprintf(stderr, "if %s subnetmask=0x%x\n",
531 * ifr->ifr_name, myNetMasks[rxi_numNetAddrs]); */
533 /* fputs(stderr, "ioctl error IFMASK\n");
534 * perror(ifr->ifr_name); */
539 if (rxi_NetAddrs[rxi_numNetAddrs] != 0x7f000001) { /* ignore lo0 */
542 rxi_nRecvFrags * (myNetMTUs[rxi_numNetAddrs] - RX_IP_SIZE);
543 maxsize -= UDP_HDR_SIZE; /* only the first frag has a UDP hdr */
544 if (rx_maxReceiveSize < maxsize)
545 rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxsize);
551 /* have to allocate at least enough to allow a single packet to reach its
552 * maximum size, so ReadPacket will work. Allocate enough for a couple
553 * of packets to do so, for good measure */
555 int npackets, ncbufs;
557 rx_maxJumboRecvSize =
558 RX_HEADER_SIZE + rxi_nDgramPackets * RX_JUMBOBUFFERSIZE +
559 (rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE;
560 rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
561 ncbufs = (rx_maxJumboRecvSize - RX_FIRSTBUFFERSIZE);
563 ncbufs = ncbufs / RX_CBUFFERSIZE;
564 npackets = rx_initSendWindow - 1;
565 rxi_MorePackets(npackets * (ncbufs + 1));
568 #else /* AFS_DJGPP_ENV */
571 #endif /* AFS_DJGPP_ENV */
573 #endif /* AFS_NT40_ENV */
575 /* Called from rxi_FindPeer, when initializing a clear rx_peer structure,
576 * to get interesting information.
577 * Curiously enough, the rx_peerHashTable_lock currently protects the
578 * Inited variable (and hence rx_GetIFInfo). When the fs suite uses
579 * pthreads, this issue will need to be revisited.
583 rxi_InitPeerParams(struct rx_peer *pp)
591 LOCK_IF_INIT if (!Inited) {
594 * there's a race here since more than one thread could call
595 * rx_GetIFInfo. The race stops in rx_GetIFInfo.
602 /* try to second-guess IP, and identify which link is most likely to
603 * be used for traffic to/from this host. */
604 ppaddr = ntohl(pp->host);
608 pp->rateFlag = 2; /* start timing after two full packets */
609 /* I don't initialize these, because I presume they are bzero'd...
610 * pp->burstSize pp->burst pp->burstWait.sec pp->burstWait.usec
611 * pp->timeout.usec */
613 LOCK_IF for (ix = 0; ix < rxi_numNetAddrs; ++ix) {
614 if ((rxi_NetAddrs[ix] & myNetMasks[ix]) == (ppaddr & myNetMasks[ix])) {
615 #ifdef IFF_POINTOPOINT
616 if (myNetFlags[ix] & IFF_POINTOPOINT)
618 #endif /* IFF_POINTOPOINT */
619 rxmtu = myNetMTUs[ix] - RX_IPUDP_SIZE;
620 if (rxmtu < RX_MIN_PACKET_SIZE)
621 rxmtu = RX_MIN_PACKET_SIZE;
622 if (pp->ifMTU < rxmtu)
623 pp->ifMTU = MIN(rx_MyMaxSendSize, rxmtu);
626 UNLOCK_IF if (!pp->ifMTU) { /* not local */
628 pp->ifMTU = MIN(rx_MyMaxSendSize, RX_REMOTE_PACKET_SIZE);
630 #else /* ADAPT_MTU */
631 pp->rateFlag = 2; /* start timing after two full packets */
633 pp->ifMTU = MIN(rx_MyMaxSendSize, OLD_MAX_PACKET_SIZE);
634 #endif /* ADAPT_MTU */
635 pp->ifMTU = rxi_AdjustIfMTU(pp->ifMTU);
636 pp->maxMTU = OLD_MAX_PACKET_SIZE; /* for compatibility with old guys */
637 pp->natMTU = MIN((int)pp->ifMTU, OLD_MAX_PACKET_SIZE);
638 pp->maxDgramPackets =
639 MIN(rxi_nDgramPackets,
640 rxi_AdjustDgramPackets(RX_MAX_FRAGS, pp->ifMTU));
642 MIN(rxi_nDgramPackets,
643 rxi_AdjustDgramPackets(RX_MAX_FRAGS, pp->ifMTU));
644 pp->maxDgramPackets = 1;
645 /* Initialize slow start parameters */
646 pp->MTU = MIN(pp->natMTU, pp->maxMTU);
648 pp->nDgramPackets = 1;
652 /* Don't expose jumobgram internals. */
656 rx_maxReceiveSize = OLD_MAX_PACKET_SIZE;
657 rxi_nSendFrags = rxi_nRecvFrags = 1;
660 /* Override max MTU. If rx_SetNoJumbo is called, it must be
661 called before calling rx_SetMaxMTU since SetNoJumbo clobbers rx_maxReceiveSize */
662 void rx_SetMaxMTU(int mtu)
664 rx_MyMaxSendSize = rx_maxReceiveSizeUser = rx_maxReceiveSize = mtu;