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>
17 # 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>
33 #if !defined(AFS_AIX_ENV) && !defined(AFS_NT40_ENV) && !defined(AFS_DJGPP_ENV)
34 # include <sys/syscall.h>
36 #include <afs/afs_args.h>
37 #include <afs/afsutil.h>
46 #ifndef IPPORT_USERRESERVED
47 /* If in.h doesn't define this, define it anyway. Unfortunately, defining
48 this doesn't put the code into the kernel to restrict kernel assigned
49 port numbers to numbers below IPPORT_USERRESERVED... */
50 #define IPPORT_USERRESERVED 5000
54 # include <sys/time.h>
57 # include "rx_globals.h"
59 #ifdef AFS_PTHREAD_ENV
63 * The rx_if_init_mutex mutex protects the following global variables:
67 pthread_mutex_t rx_if_init_mutex;
68 #define LOCK_IF_INIT assert(pthread_mutex_lock(&rx_if_init_mutex)==0);
69 #define UNLOCK_IF_INIT assert(pthread_mutex_unlock(&rx_if_init_mutex)==0);
72 * The rx_if_mutex mutex protects the following global variables:
78 pthread_mutex_t rx_if_mutex;
79 #define LOCK_IF assert(pthread_mutex_lock(&rx_if_mutex)==0);
80 #define UNLOCK_IF assert(pthread_mutex_unlock(&rx_if_mutex)==0);
83 #define UNLOCK_IF_INIT
86 #endif /* AFS_PTHREAD_ENV */
90 * Make a socket for receiving/sending IP packets. Set it into non-blocking
91 * and large buffering modes. If port isn't specified, the kernel will pick
92 * one. Returns the socket (>= 0) on success. Returns OSI_NULLSOCKET on
93 * failure. Port must be in network byte order.
95 osi_socket rxi_GetUDPSocket(u_short port)
98 osi_socket socketFd = OSI_NULLSOCKET;
99 struct sockaddr_in taddr;
100 char *name = "rxi_GetUDPSocket: ";
103 #if !defined(AFS_NT40_ENV) && !defined(AFS_DJGPP_ENV)
104 if (ntohs(port) >= IPPORT_RESERVED && ntohs(port) < IPPORT_USERRESERVED) {
105 /* (osi_Msg "%s*WARNING* port number %d is not a reserved port number. Use port numbers above %d\n", name, port, IPPORT_USERRESERVED);
108 if (ntohs(port) > 0 && ntohs(port) < IPPORT_RESERVED && geteuid() != 0) {
109 (osi_Msg "%sport number %d is a reserved port number which may only be used by root. Use port numbers above %d\n", name, ntohs(port), IPPORT_USERRESERVED);
113 socketFd = socket(AF_INET, SOCK_DGRAM, 0);
120 taddr.sin_addr.s_addr = 0;
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++) {
128 if (binds) rxi_Delay (10);
129 code = bind(socketFd, (struct sockaddr *) &taddr, sizeof(taddr));
134 (osi_Msg "%sbind failed\n", name);
138 #if !defined(AFS_NT40_ENV) && !defined(AFS_DJGPP_ENV)
140 * Set close-on-exec on rx socket
142 fcntl(socketFd, F_SETFD, 1);
145 /* Use one of three different ways of getting a socket buffer expanded to
152 len2 = rx_UdpBufSize;
153 #ifndef AFS_DJGPP_ENV
155 (setsockopt(socketFd, SOL_SOCKET, SO_RCVBUF, (char *)&len2,
158 len2=32766; /* fall back to old size... uh-oh! */
162 (setsockopt(socketFd, SOL_SOCKET, SO_SNDBUF, (char *)&len1,
163 sizeof(len1)) >= 0) &&
164 (setsockopt(socketFd, SOL_SOCKET, SO_RCVBUF, (char *)&len2,
166 #endif /* AFS_DJGPP_ENV */
169 #ifndef AFS_DJGPP_ENV
171 (osi_Msg "%s*WARNING* Unable to increase buffering on socket\n", name);
172 #endif /* AFS_DJGPP_ENV */
173 if (rxi_Listen(socketFd) < 0) {
181 if (socketFd >= 0) closesocket(socketFd);
183 if (socketFd >= 0) close(socketFd);
186 MUTEX_ENTER(&rx_stats_mutex);
187 rx_stats.socketGreedy = greedy;
188 MUTEX_EXIT(&rx_stats_mutex);
189 return OSI_NULLSOCKET;
192 void osi_Panic(char *msg, int a1, int a2, int a3)
194 (osi_Msg "Fatal Rx error: ");
195 (osi_Msg msg, a1, a2, a3);
202 * osi_AssertFailU() -- used by the osi_Assert() macro.
205 void osi_AssertFailU(const char *expr, const char *file, int line)
207 osi_Panic("assertion failed: %s, file: %s, line: %d\n", (int) expr, (int) file, line);
212 static const char memZero;
213 char *osi_Alloc(afs_int32 x)
216 * 0-length allocs may return NULL ptr from osi_kalloc, so we special-case
217 * things so that NULL returned iff an error occurred
219 if (x == 0) return &memZero;
220 return ((char *) malloc(x));
223 void osi_Free(char *x, afs_int32 size)
225 if (x == &memZero) return;
229 #endif /* AFS_AIX32_ENV */
231 #define ADDRSPERSITE 16
234 afs_uint32 rxi_NetAddrs[ADDRSPERSITE]; /* host order */
235 static int myNetMTUs[ADDRSPERSITE];
236 static int myNetMasks[ADDRSPERSITE];
237 static int myNetFlags[ADDRSPERSITE];
238 u_int rxi_numNetAddrs;
239 static int Inited = 0;
241 #if defined(AFS_NT40_ENV) || defined(AFS_DJGPP_ENV)
242 int rxi_getaddr(void)
244 if (rxi_numNetAddrs > 0)
245 return htonl(rxi_NetAddrs[0]);
251 ** return number of addresses
252 ** and the addresses themselves in the buffer
253 ** maxSize - max number of interfaces to return.
255 int rx_getAllAddr (afs_int32 *buffer, int maxSize)
258 for (count = 0; count < rxi_numNetAddrs && maxSize > 0; count++, maxSize--)
259 buffer[count] = htonl(rxi_NetAddrs[count]);
268 void rx_GetIFInfo(void)
278 int npackets, ncbufs;
283 rxi_numNetAddrs = ADDRSPERSITE;
286 (void) syscfg_GetIFInfo(&rxi_numNetAddrs, rxi_NetAddrs,
287 myNetMasks, myNetMTUs, myNetFlags);
289 for (i=0; i<rxi_numNetAddrs; i++) {
290 rxsize = rxi_AdjustIfMTU(myNetMTUs[i] - RX_IPUDP_SIZE);
291 maxsize = rxi_nRecvFrags*rxsize + (rxi_nRecvFrags-1)*UDP_HDR_SIZE;
292 maxsize = rxi_AdjustMaxMTU(rxsize, maxsize);
293 if (rx_maxReceiveSize < maxsize) {
294 rx_maxReceiveSize = MIN( RX_MAX_PACKET_SIZE, maxsize);
295 rx_maxReceiveSize = MIN( rx_maxReceiveSize,
296 rx_maxReceiveSizeUser);
301 ncbufs = (rx_maxJumboRecvSize - RX_FIRSTBUFFERSIZE);
303 ncbufs = ncbufs / RX_CBUFFERSIZE;
304 npackets = rx_initSendWindow -1;
305 rxi_MorePackets(npackets*(ncbufs+1));
312 static afs_uint32 fudge_netmask(afs_uint32 addr)
316 if (IN_CLASSA(addr)) msk = IN_CLASSA_NET;
317 else if (IN_CLASSB(addr)) msk = IN_CLASSB_NET;
318 else if (IN_CLASSC(addr)) msk = IN_CLASSC_NET;
326 #if !defined(AFS_AIX_ENV) && !defined(AFS_NT40_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_DJGPP_ENV)
327 int rxi_syscall(a3, a4, a5)
334 old = (void (*)())signal(SIGSYS, SIG_IGN);
336 #if defined(AFS_SGI_ENV)
337 rcode = afs_syscall(a3, a4, a5);
339 rcode = syscall (AFS_SYSCALL, 28 /* AFSCALL_CALL */, a3, a4, a5);
340 #endif /* AFS_SGI_ENV */
346 #endif /* AFS_AIX_ENV */
349 void rx_GetIFInfo(void)
353 #ifndef AFS_DJGPP_ENV
355 struct ifreq ifs[ADDRSPERSITE];
358 char buf[BUFSIZ], *cp, *cplim;
360 struct sockaddr_in *a;
361 #endif /* AFS_DJGPP_ENV */
373 memset(rxi_NetAddrs, 0, sizeof(rxi_NetAddrs));
374 memset(myNetFlags, 0, sizeof(myNetFlags));
375 memset(myNetMTUs, 0, sizeof(myNetMTUs));
376 memset(myNetMasks, 0, sizeof(myNetMasks));
379 s = socket(AF_INET, SOCK_DGRAM, 0);
382 #ifndef AFS_DJGPP_ENV
384 ifc.ifc_len = sizeof (buf);
388 ifc.ifc_len = sizeof(ifs);
389 ifc.ifc_buf = (caddr_t) &ifs[0];
390 memset(&ifs[0], 0, sizeof(ifs));
392 res = ioctl(s, SIOCGIFCONF, &ifc);
394 /* fputs(stderr, "ioctl error IFCONF\n"); */
402 #define size(p) MAX((p).sa_len, sizeof(p))
403 cplim = buf + ifc.ifc_len; /*skip over if's with big ifr_addr's */
404 for (cp = buf; cp < cplim ;
405 cp += sizeof(ifr->ifr_name) + MAX(a->sin_len, sizeof(*a))) {
406 if (rxi_numNetAddrs >= ADDRSPERSITE)
409 ifr = (struct ifreq *)cp;
411 len = ifc.ifc_len / sizeof(struct ifreq);
412 if (len > ADDRSPERSITE)
415 for (i = 0; i < len; ++i) {
417 res = ioctl(s, SIOCGIFADDR, ifr);
420 /* fputs(stderr, "ioctl error IFADDR\n");
421 perror(ifr->ifr_name); */
424 a = (struct sockaddr_in *) &ifr->ifr_addr;
425 if (a->sin_family != AF_INET) continue;
426 rxi_NetAddrs[rxi_numNetAddrs] = ntohl(a->sin_addr.s_addr);
427 if (rxi_NetAddrs[rxi_numNetAddrs] == 0x7f000001) {
428 /* we don't really care about "localhost" */
431 for (j=0; j < rxi_numNetAddrs; j++) {
432 if (rxi_NetAddrs[j] == rxi_NetAddrs[rxi_numNetAddrs])
435 if (j < rxi_numNetAddrs) continue;
437 /* fprintf(stderr, "if %s addr=%x\n", ifr->ifr_name,
438 rxi_NetAddrs[rxi_numNetAddrs]); */
441 res = ioctl(s, SIOCGIFFLAGS, ifr);
443 myNetFlags[rxi_numNetAddrs] = ifr->ifr_flags;
445 /* Handle aliased loopbacks as well. */
446 if (ifr->ifr_flags & IFF_LOOPBACK)
449 /* fprintf(stderr, "if %s flags=%x\n",
450 ifr->ifr_name, ifr->ifr_flags); */
453 fputs(stderr, "ioctl error IFFLAGS\n");
454 perror(ifr->ifr_name); */
456 #endif /* SIOCGIFFLAGS */
458 #if !defined(AFS_AIX_ENV) && !defined(AFS_LINUX20_ENV)
459 /* this won't run on an AIX system w/o a cache manager */
460 rxi_syscallp = rxi_syscall;
463 /* If I refer to kernel extensions that aren't loaded on AIX, the
464 * program refuses to load and run, so I simply can't include the
465 * following code. Fortunately, AIX is the one operating system in
466 * which the subsequent ioctl works reliably. */
468 if ((*rxi_syscallp)( 20 /*AFSOP_GETMTU*/,
469 htonl(rxi_NetAddrs[rxi_numNetAddrs]),
470 &(myNetMTUs[rxi_numNetAddrs]))) {
471 /* fputs(stderr, "syscall error GETMTU\n");
472 perror(ifr->ifr_name); */
473 myNetMTUs[rxi_numNetAddrs] = 0;
475 if ((*rxi_syscallp)(42 /*AFSOP_GETMASK*/,
476 htonl(rxi_NetAddrs[rxi_numNetAddrs]),
477 &(myNetMasks[rxi_numNetAddrs]))) {
478 /* fputs(stderr, "syscall error GETMASK\n");
479 perror(ifr->ifr_name); */
480 myNetMasks[rxi_numNetAddrs] = 0;
482 else myNetMasks[rxi_numNetAddrs] = ntohl( myNetMasks[rxi_numNetAddrs]);
483 /* fprintf(stderr, "if %s mask=0x%x\n",
484 ifr->ifr_name, myNetMasks[rxi_numNetAddrs]);*/
487 if (myNetMTUs[rxi_numNetAddrs] == 0) {
488 myNetMTUs[rxi_numNetAddrs] = OLD_MAX_PACKET_SIZE + RX_IPUDP_SIZE;
490 res = ioctl(s, SIOCGIFMTU, ifr);
491 if ((res == 0) && (ifr->ifr_metric > 128)) { /* sanity check */
492 myNetMTUs[rxi_numNetAddrs] = ifr->ifr_metric;
493 /* fprintf(stderr, "if %s mtu=%d\n",
494 ifr->ifr_name, ifr->ifr_metric); */
497 /* fputs(stderr, "ioctl error IFMTU\n");
498 perror(ifr->ifr_name); */
503 if (myNetMasks[rxi_numNetAddrs] == 0) {
504 myNetMasks[rxi_numNetAddrs] = fudge_netmask(rxi_NetAddrs[rxi_numNetAddrs]);
505 #ifdef SIOCGIFNETMASK
506 res = ioctl(s, SIOCGIFNETMASK, ifr);
508 a = (struct sockaddr_in *) &ifr->ifr_addr;
509 myNetMasks[rxi_numNetAddrs] = ntohl(a->sin_addr.s_addr);
510 /* fprintf(stderr, "if %s subnetmask=0x%x\n",
511 ifr->ifr_name, myNetMasks[rxi_numNetAddrs]); */
514 /* fputs(stderr, "ioctl error IFMASK\n");
515 perror(ifr->ifr_name); */
520 if (rxi_NetAddrs[rxi_numNetAddrs] != 0x7f000001) { /* ignore lo0 */
522 maxsize = rxi_nRecvFrags*(myNetMTUs[rxi_numNetAddrs] - RX_IP_SIZE);
523 maxsize -= UDP_HDR_SIZE; /* only the first frag has a UDP hdr */
524 if (rx_maxReceiveSize < maxsize)
525 rx_maxReceiveSize = MIN( RX_MAX_PACKET_SIZE, maxsize);
532 /* have to allocate at least enough to allow a single packet to reach its
533 * maximum size, so ReadPacket will work. Allocate enough for a couple
534 * of packets to do so, for good measure */
536 int npackets, ncbufs;
538 rx_maxJumboRecvSize = RX_HEADER_SIZE
539 + rxi_nDgramPackets * RX_JUMBOBUFFERSIZE
540 + (rxi_nDgramPackets-1) * RX_JUMBOHEADERSIZE;
541 rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
542 ncbufs = (rx_maxJumboRecvSize - RX_FIRSTBUFFERSIZE);
544 ncbufs = ncbufs / RX_CBUFFERSIZE;
545 npackets = rx_initSendWindow -1;
546 rxi_MorePackets(npackets*(ncbufs+1));
549 #else /* AFS_DJGPP_ENV */
552 #endif /* AFS_DJGPP_ENV */
554 #endif /* AFS_NT40_ENV */
556 /* Called from rxi_FindPeer, when initializing a clear rx_peer structure,
557 * to get interesting information.
558 * Curiously enough, the rx_peerHashTable_lock currently protects the
559 * Inited variable (and hence rx_GetIFInfo). When the fs suite uses
560 * pthreads, this issue will need to be revisited.
563 void rxi_InitPeerParams(struct rx_peer *pp)
575 * there's a race here since more than one thread could call
576 * rx_GetIFInfo. The race stops in rx_GetIFInfo.
584 /* try to second-guess IP, and identify which link is most likely to
585 * be used for traffic to/from this host. */
586 ppaddr = ntohl(pp->host);
590 pp->rateFlag = 2; /* start timing after two full packets */
591 /* I don't initialize these, because I presume they are bzero'd...
592 * pp->burstSize pp->burst pp->burstWait.sec pp->burstWait.usec
593 * pp->timeout.usec */
596 for (ix = 0; ix < rxi_numNetAddrs; ++ix) {
597 if ((rxi_NetAddrs[ix] & myNetMasks[ix]) == (ppaddr & myNetMasks[ix])) {
598 #ifdef IFF_POINTOPOINT
599 if (myNetFlags[ix] & IFF_POINTOPOINT)
601 #endif /* IFF_POINTOPOINT */
602 rxmtu = myNetMTUs[ix] - RX_IPUDP_SIZE;
603 if ( rxmtu < RX_MIN_PACKET_SIZE )
604 rxmtu = RX_MIN_PACKET_SIZE ;
605 if (pp->ifMTU < rxmtu)
606 pp->ifMTU = MIN(rx_MyMaxSendSize, rxmtu);
611 if (!pp->ifMTU) { /* not local */
613 pp->ifMTU = RX_REMOTE_PACKET_SIZE;
615 #else /* ADAPT_MTU */
616 pp->rateFlag = 2; /* start timing after two full packets */
618 pp->ifMTU = OLD_MAX_PACKET_SIZE;
619 #endif /* ADAPT_MTU */
620 pp->ifMTU = rxi_AdjustIfMTU(pp->ifMTU);
621 pp->maxMTU = OLD_MAX_PACKET_SIZE; /* for compatibility with old guys */
622 pp->natMTU = MIN((int)pp->ifMTU, OLD_MAX_PACKET_SIZE);
623 pp->maxDgramPackets = MIN(rxi_nDgramPackets,
624 rxi_AdjustDgramPackets(RX_MAX_FRAGS, pp->ifMTU));
625 pp->ifDgramPackets = MIN(rxi_nDgramPackets,
626 rxi_AdjustDgramPackets(RX_MAX_FRAGS, pp->ifMTU));
627 pp->maxDgramPackets = 1;
628 /* Initialize slow start parameters */
629 pp->MTU = MIN(pp->natMTU, pp->maxMTU);
631 pp->nDgramPackets = 1;
635 /* Don't expose jumobgram internals. */
636 void rx_SetNoJumbo(void)
638 rx_maxReceiveSize = OLD_MAX_PACKET_SIZE;
639 rxi_nSendFrags = rxi_nRecvFrags = 1;