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 #include <afsconfig.h>
11 #include <afs/param.h>
24 #include <netinet/in.h>
37 #include <afs/assert.h>
40 #include <afs/afsint.h>
41 #include <afs/rxgen_consts.h>
43 #include <afs/errors.h>
44 #include <afs/ihandle.h>
45 #include <afs/vnode.h>
46 #include <afs/volume.h>
47 #ifdef AFS_ATHENA_STDENV
51 #include <afs/ptclient.h>
52 #include <afs/prs_fs.h>
54 #include <afs/afsutil.h>
56 #include <afs/cellconfig.h>
58 #include "viced_prototypes.h"
63 #ifdef AFS_PTHREAD_ENV
64 pthread_mutex_t host_glock_mutex;
65 #endif /* AFS_PTHREAD_ENV */
68 extern int CurrentConnections;
70 extern int AnonymousID;
71 extern prlist AnonCPS;
73 extern struct afsconf_dir *confDir; /* config dir object */
74 extern int lwps; /* the max number of server threads */
75 extern afsUUID FS_HostUUID;
77 int CEs = 0; /* active clients */
78 int CEBlocks = 0; /* number of blocks of CEs */
79 struct client *CEFree = 0; /* first free client */
80 struct host *hostList = 0; /* linked list of all hosts */
81 int hostCount = 0; /* number of hosts in hostList */
85 #define CESPERBLOCK 73
86 struct CEBlock { /* block of CESPERBLOCK file entries */
87 struct client entry[CESPERBLOCK];
90 static void h_TossStuff_r(register struct host *host);
91 static int hashDelete_r(afs_uint32 addr, afs_uint16 port, struct host *host);
94 * Make sure the subnet macros have been defined.
97 #define IN_SUBNETA(i) ((((afs_int32)(i))&0x80800000)==0x00800000)
100 #ifndef IN_CLASSA_SUBNET
101 #define IN_CLASSA_SUBNET 0xffff0000
105 #define IN_SUBNETB(i) ((((afs_int32)(i))&0xc0008000)==0x80008000)
108 #ifndef IN_CLASSB_SUBNET
109 #define IN_CLASSB_SUBNET 0xffffff00
113 /* get a new block of CEs and chain it on CEFree */
117 register struct CEBlock *block;
120 block = (struct CEBlock *)malloc(sizeof(struct CEBlock));
122 ViceLog(0, ("Failed malloc in GetCEBlock\n"));
123 ShutDownAndCore(PANIC);
126 for (i = 0; i < (CESPERBLOCK - 1); i++) {
127 Lock_Init(&block->entry[i].lock);
128 block->entry[i].next = &(block->entry[i + 1]);
130 block->entry[CESPERBLOCK - 1].next = 0;
131 Lock_Init(&block->entry[CESPERBLOCK - 1].lock);
132 CEFree = (struct client *)block;
138 /* get the next available CE */
139 static struct client *
142 register struct client *entry;
147 ViceLog(0, ("CEFree NULL in GetCE\n"));
148 ShutDownAndCore(PANIC);
152 CEFree = entry->next;
154 memset((char *)entry, 0, CLIENT_TO_ZERO(entry));
160 /* return an entry to the free list */
162 FreeCE(register struct client *entry)
164 entry->next = CEFree;
171 * The HTs and HTBlocks variables were formerly static, but they are
172 * now referenced elsewhere in the FileServer.
174 int HTs = 0; /* active file entries */
175 int HTBlocks = 0; /* number of blocks of HTs */
176 static struct host *HTFree = 0; /* first free file entry */
179 * Hash tables of host pointers. We need two tables, one
180 * to map IP addresses onto host pointers, and another
181 * to map host UUIDs onto host pointers.
183 static struct h_hashChain *hostHashTable[h_HASHENTRIES];
184 static struct h_hashChain *hostUuidHashTable[h_HASHENTRIES];
185 #define h_HashIndex(hostip) ((hostip) & (h_HASHENTRIES-1))
186 #define h_UuidHashIndex(uuidp) (((int)(afs_uuid_hash(uuidp))) & (h_HASHENTRIES-1))
188 struct HTBlock { /* block of HTSPERBLOCK file entries */
189 struct host entry[h_HTSPERBLOCK];
193 /* get a new block of HTs and chain it on HTFree */
197 register struct HTBlock *block;
199 static int index = 0;
201 block = (struct HTBlock *)malloc(sizeof(struct HTBlock));
203 ViceLog(0, ("Failed malloc in GetHTBlock\n"));
204 ShutDownAndCore(PANIC);
206 #ifdef AFS_PTHREAD_ENV
207 for (i = 0; i < (h_HTSPERBLOCK); i++)
208 assert(pthread_cond_init(&block->entry[i].cond, NULL) == 0);
209 #endif /* AFS_PTHREAD_ENV */
210 for (i = 0; i < (h_HTSPERBLOCK); i++)
211 Lock_Init(&block->entry[i].lock);
212 for (i = 0; i < (h_HTSPERBLOCK - 1); i++)
213 block->entry[i].next = &(block->entry[i + 1]);
214 for (i = 0; i < (h_HTSPERBLOCK); i++)
215 block->entry[i].index = index++;
216 block->entry[h_HTSPERBLOCK - 1].next = 0;
217 HTFree = (struct host *)block;
218 hosttableptrs[HTBlocks++] = block->entry;
223 /* get the next available HT */
227 register struct host *entry;
233 HTFree = entry->next;
235 memset((char *)entry, 0, HOST_TO_ZERO(entry));
241 /* return an entry to the free list */
243 FreeHT(register struct host *entry)
245 entry->next = HTFree;
252 static short consolePort = 0;
255 h_Release(register struct host *host)
264 * If this thread does not have a hold on this host AND
265 * if other threads also dont have any holds on this host AND
266 * If either the HOSTDELETED or CLIENTDELETED flags are set
270 h_Release_r(register struct host *host)
273 if (!((host)->holds[h_holdSlot()] & ~h_holdbit())) {
274 if (!h_OtherHolds_r(host)) {
275 /* must avoid masking this until after h_OtherHolds_r runs
276 * but it should be run before h_TossStuff_r */
277 (host)->holds[h_holdSlot()] &= ~h_holdbit();
278 if ((host->hostFlags & HOSTDELETED)
279 || (host->hostFlags & CLIENTDELETED)) {
283 (host)->holds[h_holdSlot()] &= ~h_holdbit();
285 (host)->holds[h_holdSlot()] &= ~h_holdbit();
291 h_OtherHolds_r(register struct host *host)
293 register int i, bit, slot;
296 for (i = 0; i < h_maxSlots; i++) {
297 if (host->holds[i] != ((i == slot) ? bit : 0)) {
305 h_Lock_r(register struct host *host)
315 * returns 1 if already locked
316 * else returns locks and returns 0
320 h_NBLock_r(register struct host *host)
322 struct Lock *hostLock = &host->lock;
327 if (!(hostLock->excl_locked) && !(hostLock->readers_reading))
328 hostLock->excl_locked = WRITE_LOCK;
332 LOCK_UNLOCK(hostLock);
341 #if FS_STATS_DETAILED
342 /*------------------------------------------------------------------------
343 * PRIVATE h_AddrInSameNetwork
346 * Given a target IP address and a candidate IP address (both
347 * in host byte order), return a non-zero value (1) if the
348 * candidate address is in a different network from the target
352 * a_targetAddr : Target address.
353 * a_candAddr : Candidate address.
356 * 1 if the candidate address is in the same net as the target,
360 * The target and candidate addresses are both in host byte
361 * order, NOT network byte order, when passed in. We return
362 * our value as a character, since that's the type of field in
363 * the host structure, where this info will be stored.
367 *------------------------------------------------------------------------*/
370 h_AddrInSameNetwork(afs_uint32 a_targetAddr, afs_uint32 a_candAddr)
371 { /*h_AddrInSameNetwork */
373 afs_uint32 targetNet;
377 * Pull out the network and subnetwork numbers from the target
378 * and candidate addresses. We can short-circuit this whole
379 * affair if the target and candidate addresses are not of the
382 if (IN_CLASSA(a_targetAddr)) {
383 if (!(IN_CLASSA(a_candAddr))) {
386 targetNet = a_targetAddr & IN_CLASSA_NET;
387 candNet = a_candAddr & IN_CLASSA_NET;
388 } else if (IN_CLASSB(a_targetAddr)) {
389 if (!(IN_CLASSB(a_candAddr))) {
392 targetNet = a_targetAddr & IN_CLASSB_NET;
393 candNet = a_candAddr & IN_CLASSB_NET;
394 } /*Class B target */
395 else if (IN_CLASSC(a_targetAddr)) {
396 if (!(IN_CLASSC(a_candAddr))) {
399 targetNet = a_targetAddr & IN_CLASSC_NET;
400 candNet = a_candAddr & IN_CLASSC_NET;
401 } /*Class C target */
403 targetNet = a_targetAddr;
404 candNet = a_candAddr;
405 } /*Class D address */
408 * Now, simply compare the extracted net values for the two addresses
409 * (which at this point are known to be of the same class)
411 if (targetNet == candNet)
416 } /*h_AddrInSameNetwork */
417 #endif /* FS_STATS_DETAILED */
420 /* Assumptions: called with held host */
422 h_gethostcps_r(register struct host *host, register afs_int32 now)
427 /* wait if somebody else is already doing the getCPS call */
428 while (host->hostFlags & HCPS_INPROGRESS) {
429 slept = 1; /* I did sleep */
430 host->hostFlags |= HCPS_WAITING; /* I am sleeping now */
431 #ifdef AFS_PTHREAD_ENV
432 pthread_cond_wait(&host->cond, &host_glock_mutex);
433 #else /* AFS_PTHREAD_ENV */
434 if ((code = LWP_WaitProcess(&(host->hostFlags))) != LWP_SUCCESS)
435 ViceLog(0, ("LWP_WaitProcess returned %d\n", code));
436 #endif /* AFS_PTHREAD_ENV */
440 host->hostFlags |= HCPS_INPROGRESS; /* mark as CPSCall in progress */
441 if (host->hcps.prlist_val)
442 free(host->hcps.prlist_val); /* this is for hostaclRefresh */
443 host->hcps.prlist_val = NULL;
444 host->hcps.prlist_len = 0;
445 slept ? (host->cpsCall = FT_ApproxTime()) : (host->cpsCall = now);
448 code = pr_GetHostCPS(htonl(host->host), &host->hcps);
452 * Although ubik_Call (called by pr_GetHostCPS) traverses thru all protection servers
453 * and reevaluates things if no sync server or quorum is found we could still end up
454 * with one of these errors. In such case we would like to reevaluate the rpc call to
455 * find if there's cps for this guy. We treat other errors (except network failures
456 * ones - i.e. code < 0) as an indication that there is no CPS for this host. Ideally
457 * we could like to deal this problem the other way around (i.e. if code == NOCPS
458 * ignore else retry next time) but the problem is that there're other errors (i.e.
459 * EPERM) for which we don't want to retry and we don't know the whole code list!
461 if (code < 0 || code == UNOQUORUM || code == UNOTSYNC) {
463 * We would have preferred to use a while loop and try again since ops in protected
464 * acls for this host will fail now but they'll be reevaluated on any subsequent
465 * call. The attempt to wait for a quorum/sync site or network error won't work
466 * since this problems really should only occurs during a complete fileserver
467 * restart. Since the fileserver will start before the ptservers (and thus before
468 * quorums are complete) clients will be utilizing all the fileserver's lwps!!
470 host->hcpsfailed = 1;
472 ("Warning: GetHostCPS failed (%d) for %x; will retry\n",
475 host->hcpsfailed = 0;
477 ("gethost: GetHostCPS failed (%d) for %x; ignored\n",
480 if (host->hcps.prlist_val)
481 free(host->hcps.prlist_val);
482 host->hcps.prlist_val = NULL;
483 host->hcps.prlist_len = 0; /* Make sure it's zero */
485 host->hcpsfailed = 0;
487 host->hostFlags &= ~HCPS_INPROGRESS;
488 /* signal all who are waiting */
489 if (host->hostFlags & HCPS_WAITING) { /* somebody is waiting */
490 host->hostFlags &= ~HCPS_WAITING;
491 #ifdef AFS_PTHREAD_ENV
492 assert(pthread_cond_broadcast(&host->cond) == 0);
493 #else /* AFS_PTHREAD_ENV */
494 if ((code = LWP_NoYieldSignal(&(host->hostFlags))) != LWP_SUCCESS)
495 ViceLog(0, ("LWP_NoYieldSignal returns %d\n", code));
496 #endif /* AFS_PTHREAD_ENV */
500 /* args in net byte order */
502 h_flushhostcps(register afs_uint32 hostaddr, register afs_uint32 hport)
504 register struct host *host;
508 host = h_Lookup_r(hostaddr, hport, &held);
510 host->hcpsfailed = 1;
520 * Allocate a host. It will be identified by the peer (ip,port) info in the
521 * rx connection provided. The host is returned held and locked
523 #define DEF_ROPCONS 2115
526 h_Alloc_r(register struct rx_connection *r_con)
528 struct servent *serverentry;
529 register index = h_HashIndex(rxr_HostOf(r_con));
530 register struct host *host;
531 static struct rx_securityClass *sc = 0;
533 struct h_hashChain *h_hashChain;
534 #if FS_STATS_DETAILED
535 afs_uint32 newHostAddr_HBO; /*New host IP addr, in host byte order */
536 #endif /* FS_STATS_DETAILED */
540 h_hashChain = (struct h_hashChain *)malloc(sizeof(struct h_hashChain));
542 ViceLog(0, ("Failed malloc in h_Alloc_r\n"));
545 h_hashChain->hostPtr = host;
546 h_hashChain->addr = rxr_HostOf(r_con);
547 h_hashChain->next = hostHashTable[index];
548 hostHashTable[index] = h_hashChain;
550 host->host = rxr_HostOf(r_con);
551 host->port = rxr_PortOf(r_con);
552 if (consolePort == 0) { /* find the portal number for console */
553 #if defined(AFS_OSF_ENV)
554 serverentry = getservbyname("ropcons", "");
556 serverentry = getservbyname("ropcons", 0);
559 consolePort = serverentry->s_port;
561 consolePort = htons(DEF_ROPCONS); /* Use a default */
563 if (host->port == consolePort)
565 /* Make a callback channel even for the console, on the off chance that it
566 * makes a request that causes a break call back. It shouldn't. */
569 sc = rxnull_NewClientSecurityObject();
570 host->callback_rxcon =
571 rx_NewConnection(host->host, host->port, 1, sc, 0);
572 rx_SetConnDeadTime(host->callback_rxcon, 50);
573 rx_SetConnHardDeadTime(host->callback_rxcon, AFS_HARDDEADTIME);
575 now = host->LastCall = host->cpsCall = host->ActiveCall = FT_ApproxTime();
577 host->hcps.prlist_val = NULL;
578 host->hcps.prlist_len = 0;
581 host->hcpsfailed = 0; /* save cycles */
582 h_gethostcps(host); /* do this under host hold/lock */
584 host->FirstClient = 0;
587 h_InsertList_r(host); /* update global host List */
588 #if FS_STATS_DETAILED
590 * Compare the new host's IP address (in host byte order) with ours
591 * (the File Server's), remembering if they are in the same network.
593 newHostAddr_HBO = (afs_uint32) ntohl(host->host);
594 host->InSameNetwork =
595 h_AddrInSameNetwork(FS_HostAddr_HBO, newHostAddr_HBO);
596 #endif /* FS_STATS_DETAILED */
602 /* Lookup a host given an IP address and UDP port number. */
603 /* hostaddr and hport are in network order */
604 /* Note: host should be released by caller if 0 == *heldp and non-null */
605 /* hostaddr and hport are in network order */
607 h_Lookup_r(afs_uint32 haddr, afs_uint32 hport, int *heldp)
609 register afs_int32 now;
610 register struct host *host = 0;
611 register struct h_hashChain *chain;
612 register index = h_HashIndex(haddr);
613 extern int hostaclRefresh;
616 for (chain = hostHashTable[index]; chain; chain = chain->next) {
617 host = chain->hostPtr;
619 if (!(host->hostFlags & HOSTDELETED) && chain->addr == haddr
620 && chain->port == hport) {
621 *heldp = h_Held_r(host);
625 if (host->hostFlags & HOSTDELETED) {
632 now = FT_ApproxTime(); /* always evaluate "now" */
633 if (host->hcpsfailed || (host->cpsCall + hostaclRefresh < now)) {
635 * Every hostaclRefresh period (def 2 hrs) get the new
636 * membership list for the host. Note this could be the
637 * first time that the host is added to a group. Also
638 * here we also retry on previous legitimate hcps failures.
640 * If we get here we still have a host hold.
642 h_gethostcps_r(host, now);
652 /* Lookup a host given its UUID. */
654 h_LookupUuid_r(afsUUID * uuidp)
656 register struct host *host = 0;
657 register struct h_hashChain *chain;
658 register index = h_UuidHashIndex(uuidp);
660 for (chain = hostUuidHashTable[index]; chain; chain = chain->next) {
661 host = chain->hostPtr;
663 if (!(host->hostFlags & HOSTDELETED) && host->interface
664 && afs_uuid_equal(&host->interface->uuid, uuidp)) {
675 * h_Hold_r: Establish a hold by the current LWP on this host--the host
676 * or its clients will not be physically deleted until all holds have
678 * NOTE: h_Hold_r is a macro defined in host.h.
681 /* h_TossStuff_r: Toss anything in the host structure (the host or
682 * clients marked for deletion. Called from h_Release_r ONLY.
683 * To be called, there must be no holds, and either host->deleted
684 * or host->clientDeleted must be set.
687 h_TossStuff_r(register struct host *host)
689 register struct client **cp, *client;
692 /* if somebody still has this host held */
693 for (i = 0; (i < h_maxSlots) && (!(host)->holds[i]); i++);
697 /* if somebody still has this host locked */
698 if (h_NBLock_r(host) != 0) {
701 ("Warning: h_TossStuff_r failed; Host %s:%d was locked.\n",
702 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
708 /* ASSUMPTION: rxi_FreeConnection() does not yield */
709 for (cp = &host->FirstClient; (client = *cp);) {
710 if ((host->hostFlags & HOSTDELETED) || client->deleted) {
711 if (client->refCount) {
714 ("Warning: Host %s:%d client %x refcount %d while deleting, failing.\n",
715 afs_inet_ntoa_r(host->host, hoststr),
716 ntohs(host->port), client, client->refCount));
717 /* This is the same thing we do if the host is locked */
720 /* We can't protect this without dropping the H_LOCK */
721 client->CPS.prlist_len = 0;
722 if ((client->ViceId != ANONYMOUSID) && client->CPS.prlist_val) {
723 free(client->CPS.prlist_val);
724 client->CPS.prlist_val = NULL;
727 rx_SetSpecific(client->tcon, rxcon_client_key, (void *)0);
729 CurrentConnections--;
736 /* We've just cleaned out all the deleted clients; clear the flag */
737 host->hostFlags &= ~CLIENTDELETED;
739 if (host->hostFlags & HOSTDELETED) {
740 register struct h_hashChain **hp, *th;
741 register struct rx_connection *rxconn;
743 struct AddrPort hostAddrPort;
746 if (host->Console & 1)
748 if ((rxconn = host->callback_rxcon)) {
749 host->callback_rxcon = (struct rx_connection *)0;
751 * If rx_DestroyConnection calls h_FreeConnection we will
752 * deadlock on the host_glock_mutex. Work around the problem
753 * by unhooking the client from the connection before
754 * destroying the connection.
756 client = rx_GetSpecific(rxconn, rxcon_client_key);
757 if (client && client->tcon == rxconn)
759 rx_SetSpecific(rxconn, rxcon_client_key, (void *)0);
760 rx_DestroyConnection(rxconn);
762 if (host->hcps.prlist_val)
763 free(host->hcps.prlist_val);
764 host->hcps.prlist_val = NULL;
765 host->hcps.prlist_len = 0;
766 DeleteAllCallBacks_r(host, 1);
767 host->hostFlags &= ~RESETDONE; /* just to be safe */
769 /* if alternate addresses do not exist */
770 if (!(host->interface)) {
771 for (hp = &hostHashTable[h_HashIndex(host->host)]; (th = *hp);
774 if (th->hostPtr == host) {
776 h_DeleteList_r(host);
783 /* delete all hash entries for the UUID */
784 uuidp = &host->interface->uuid;
785 for (hp = &hostUuidHashTable[h_UuidHashIndex(uuidp)]; (th = *hp);
788 if (th->hostPtr == host) {
794 /* delete all hash entries for alternate addresses */
795 assert(host->interface->numberOfInterfaces > 0);
796 for (i = 0; i < host->interface->numberOfInterfaces; i++) {
797 hostAddrPort = host->interface->interface[i];
799 for (hp = &hostHashTable[h_HashIndex(hostAddrPort.addr)]; (th = *hp);
802 if (th->hostPtr == host) {
809 free(host->interface);
810 host->interface = NULL;
811 h_DeleteList_r(host); /* remove host from global host List */
813 } /* if alternate address exists */
818 /* Called by rx when a server connection disappears */
820 h_FreeConnection(struct rx_connection *tcon)
822 register struct client *client;
824 client = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
827 if (client->tcon == tcon)
828 client->tcon = (struct rx_connection *)0;
832 } /*h_FreeConnection */
835 /* h_Enumerate: Calls (*proc)(host, held, param) for at least each host in the
836 * system at the start of the enumeration (perhaps more). Hosts may be deleted
837 * (have delete flag set); ditto for clients. (*proc) is always called with
838 * host h_held(). The hold state of the host with respect to this lwp is passed
839 * to (*proc) as the param held. The proc should return 0 if the host should be
840 * released, 1 if it should be held after enumeration.
843 h_Enumerate(int (*proc) (), char *param)
845 register struct host *host, **list;
847 register int i, count;
850 if (hostCount == 0) {
854 list = (struct host **)malloc(hostCount * sizeof(struct host *));
856 ViceLog(0, ("Failed malloc in h_Enumerate\n"));
859 held = (int *)malloc(hostCount * sizeof(int));
861 ViceLog(0, ("Failed malloc in h_Enumerate\n"));
864 for (count = 0, host = hostList; host; host = host->next, count++) {
866 if (!(held[count] = h_Held_r(host)))
869 assert(count == hostCount);
871 for (i = 0; i < count; i++) {
872 held[i] = (*proc) (list[i], held[i], param);
874 h_Release(list[i]); /* this might free up the host */
880 /* h_Enumerate_r (revised):
881 * Calls (*proc)(host, held, param) for each host in hostList, starting
883 * Hosts may be deleted (have delete flag set); ditto for clients.
884 * (*proc) is always called with
885 * host h_held() and the global host lock (H_LOCK) locked.The hold state of the
886 * host with respect to this lwp is passed to (*proc) as the param held.
887 * The proc should return 0 if the host should be released, 1 if it should
888 * be held after enumeration.
891 h_Enumerate_r(int (*proc) (), struct host *enumstart, char *param)
893 register struct host *host, *next;
894 register int held, nheld;
896 if (hostCount == 0) {
899 if (enumstart && !(held = h_Held_r(enumstart)))
901 for (host = enumstart; host; host = next, held = nheld) {
902 held = (*proc) (host, held, param);
904 if (next && !(nheld = h_Held_r(next)))
907 h_Release_r(host); /* this might free up the host */
911 /* inserts a new HashChain structure corresponding to this UUID */
913 hashInsertUuid_r(struct afsUUID *uuid, struct host *host)
916 struct h_hashChain *chain;
918 /* hash into proper bucket */
919 index = h_UuidHashIndex(uuid);
921 /* insert into beginning of list for this bucket */
922 chain = (struct h_hashChain *)malloc(sizeof(struct h_hashChain));
924 ViceLog(0, ("Failed malloc in hashInsertUuid_r\n"));
928 chain->hostPtr = host;
929 chain->next = hostUuidHashTable[index];
930 hostUuidHashTable[index] = chain;
934 /* inserts a new HashChain structure corresponding to this address */
936 hashInsert_r(afs_uint32 addr, afs_uint16 port, struct host *host)
939 struct h_hashChain *chain;
941 /* hash into proper bucket */
942 index = h_HashIndex(addr);
944 /* insert into beginning of list for this bucket */
945 chain = (struct h_hashChain *)malloc(sizeof(struct h_hashChain));
947 ViceLog(0, ("Failed malloc in hashInsert_r\n"));
950 chain->hostPtr = host;
951 chain->next = hostHashTable[index];
954 hostHashTable[index] = chain;
959 * This is called with host locked and held. At this point, the
960 * hostHashTable should not be having entries for the alternate
961 * interfaces. This function has to insert these entries in the
964 * All addresses are in network byte order.
967 addInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
972 struct Interface *interface;
973 char hoststr[16], hoststr2[16];
976 assert(host->interface);
978 ViceLog(125, ("addInterfaceAddr : host %s:d addr %s:%d\n",
979 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
980 afs_inet_ntoa_r(addr, hoststr2), ntohs(port)));
983 * Make sure this address is on the list of known addresses
986 number = host->interface->numberOfInterfaces;
987 for (i = 0, found = 0; i < number && !found; i++) {
988 if (host->interface->interface[i].addr == addr &&
989 host->interface->interface[i].port == port)
993 interface = (struct Interface *)
994 malloc(sizeof(struct Interface) + (sizeof(struct AddrPort) * number));
996 ViceLog(0, ("Failed malloc in addInterfaceAddr_r\n"));
999 interface->numberOfInterfaces = number + 1;
1000 interface->uuid = host->interface->uuid;
1001 for (i = 0; i < number; i++)
1002 interface->interface[i] = host->interface->interface[i];
1003 interface->interface[number].addr = addr;
1004 interface->interface[number].port = port;
1005 free(host->interface);
1006 host->interface = interface;
1010 * Create a hash table entry for this address
1012 hashInsert_r(addr, port, host);
1019 * This is called with host locked and held. At this point, the
1020 * hostHashTable should not be having entries for the alternate
1021 * interfaces. This function has to insert these entries in the
1024 * All addresses are in network byte order.
1027 removeInterfaceAddr_r(struct host *host, afs_uint32 addr, afs_uint16 port)
1032 struct Interface *interface;
1033 char hoststr[16], hoststr2[16];
1036 assert(host->interface);
1038 ViceLog(125, ("removeInterfaceAddr : host %s:%d addr %s:%d\n",
1039 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
1040 afs_inet_ntoa_r(addr, hoststr2), ntohs(port)));
1043 * Make sure this address is on the list of known addresses
1046 interface = host->interface;
1047 number = host->interface->numberOfInterfaces;
1048 for (i = 0, found = 0; i < number; i++) {
1049 if (interface->interface[i].addr == addr &&
1050 interface->interface[i].port == port) {
1057 for (; i < number; i++) {
1058 interface->interface[i].addr = interface->interface[i+1].addr;
1059 interface->interface[i].port = interface->interface[i+1].port;
1061 interface->numberOfInterfaces = number;
1065 * Remove the hash table entry for this address
1067 hashDelete_r(addr, port, host);
1073 /* Host is returned held */
1075 h_GetHost_r(struct rx_connection *tcon)
1078 struct host *oldHost;
1081 struct interfaceAddr interf;
1082 int interfValid = 0;
1083 struct Identity *identP = NULL;
1086 char hoststr[16], hoststr2[16];
1088 struct rx_connection *cb_conn = NULL;
1090 caps.Capabilities_val = NULL;
1092 haddr = rxr_HostOf(tcon);
1093 hport = rxr_PortOf(tcon);
1095 if (caps.Capabilities_val)
1096 free(caps.Capabilities_val);
1097 caps.Capabilities_val = NULL;
1098 caps.Capabilities_len = 0;
1101 host = h_Lookup_r(haddr, hport, &held);
1102 identP = (struct Identity *)rx_GetSpecific(tcon, rxcon_ident_key);
1103 if (host && !identP && !(host->Console & 1)) {
1104 /* This is a new connection, and we already have a host
1105 * structure for this address. Verify that the identity
1106 * of the caller matches the identity in the host structure.
1109 if (!(host->hostFlags & ALTADDR)) {
1110 /* Another thread is doing initialization */
1115 ("Host %s:%d starting h_Lookup again\n",
1116 afs_inet_ntoa_r(host->host, hoststr),
1117 ntohs(host->port)));
1120 host->hostFlags &= ~ALTADDR;
1121 cb_conn = host->callback_rxcon;
1122 rx_GetConnection(cb_conn);
1125 RXAFSCB_TellMeAboutYourself(cb_conn, &interf, &caps);
1126 if (code == RXGEN_OPCODE)
1127 code = RXAFSCB_WhoAreYou(cb_conn, &interf);
1128 rx_PutConnection(cb_conn);
1131 if (code == RXGEN_OPCODE) {
1132 identP = (struct Identity *)malloc(sizeof(struct Identity));
1134 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
1138 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1139 /* The host on this connection was unable to respond to
1140 * the WhoAreYou. We will treat this as a new connection
1141 * from the existing host. The worst that can happen is
1142 * that we maintain some extra callback state information */
1143 if (host->interface) {
1145 ("Host %s:%d used to support WhoAreYou, deleting.\n",
1146 afs_inet_ntoa_r(host->host, hoststr),
1147 ntohs(host->port)));
1148 host->hostFlags |= HOSTDELETED;
1155 } else if (code == 0) {
1157 identP = (struct Identity *)malloc(sizeof(struct Identity));
1159 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
1163 identP->uuid = interf.uuid;
1164 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1165 /* Check whether the UUID on this connection matches
1166 * the UUID in the host structure. If they don't match
1167 * then this is not the same host as before. */
1168 if (!host->interface
1169 || !afs_uuid_equal(&interf.uuid, &host->interface->uuid)) {
1171 ("Host %s:%d has changed its identity, deleting.\n",
1172 afs_inet_ntoa_r(host->host, hoststr), host->port));
1173 host->hostFlags |= HOSTDELETED;
1181 afs_inet_ntoa_r(host->host, hoststr);
1183 ("CB: WhoAreYou failed for %s:%d, error %d\n", hoststr,
1184 ntohs(host->port), code));
1185 host->hostFlags |= VENUSDOWN;
1187 if (caps.Capabilities_val
1188 && (caps.Capabilities_val[0] & CLIENT_CAPABILITY_ERRORTRANS))
1189 host->hostFlags |= HERRORTRANS;
1191 host->hostFlags &= ~(HERRORTRANS);
1192 host->hostFlags |= ALTADDR;
1195 if (!(host->hostFlags & ALTADDR)) {
1196 /* another thread is doing the initialisation */
1198 ("Host %s:%d waiting for host-init to complete\n",
1199 afs_inet_ntoa_r(host->host, hoststr),
1200 ntohs(host->port)));
1206 ("Host %s:%d starting h_Lookup again\n",
1207 afs_inet_ntoa_r(host->host, hoststr),
1208 ntohs(host->port)));
1211 /* We need to check whether the identity in the host structure
1212 * matches the identity on the connection. If they don't match
1213 * then treat this a new host. */
1214 if (!(host->Console & 1)
1215 && ((!identP->valid && host->interface)
1216 || (identP->valid && !host->interface)
1218 && !afs_uuid_equal(&identP->uuid,
1219 &host->interface->uuid)))) {
1220 char uuid1[128], uuid2[128];
1222 afsUUID_to_string(&identP->uuid, uuid1, 127);
1223 if (host->interface)
1224 afsUUID_to_string(&host->interface->uuid, uuid2, 127);
1226 ("CB: new identity for host %s:%d, deleting(%x %x %s %s)\n",
1227 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port),
1228 identP->valid, host->interface,
1229 identP->valid ? uuid1 : "",
1230 host->interface ? uuid2 : ""));
1232 /* The host in the cache is not the host for this connection */
1233 host->hostFlags |= HOSTDELETED;
1240 host = h_Alloc_r(tcon); /* returned held and locked */
1241 h_gethostcps_r(host, FT_ApproxTime());
1242 if (!(host->Console & 1)) {
1244 cb_conn = host->callback_rxcon;
1245 rx_GetConnection(cb_conn);
1248 RXAFSCB_TellMeAboutYourself(cb_conn, &interf, &caps);
1249 if (code == RXGEN_OPCODE)
1250 code = RXAFSCB_WhoAreYou(cb_conn, &interf);
1251 rx_PutConnection(cb_conn);
1254 if (code == RXGEN_OPCODE) {
1257 (struct Identity *)malloc(sizeof(struct Identity));
1262 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
1267 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1269 ("Host %s:%d does not support WhoAreYou.\n",
1270 afs_inet_ntoa_r(host->host, hoststr),
1271 ntohs(host->port)));
1273 } else if (code == 0) {
1276 (struct Identity *)malloc(sizeof(struct Identity));
1281 ViceLog(0, ("Failed malloc in h_GetHost_r\n"));
1286 identP->uuid = interf.uuid;
1288 rx_SetSpecific(tcon, rxcon_ident_key, identP);
1290 ("WhoAreYou success on %s:%d\n",
1291 afs_inet_ntoa_r(host->host, hoststr),
1292 ntohs(host->port)));
1294 if (code == 0 && !identP->valid) {
1295 cb_conn = host->callback_rxcon;
1296 rx_GetConnection(cb_conn);
1298 code = RXAFSCB_InitCallBackState(cb_conn);
1299 rx_PutConnection(cb_conn);
1302 } else if (code == 0) {
1303 oldHost = h_LookupUuid_r(&identP->uuid);
1307 if (!(oheld = h_Held_r(oldHost)))
1311 if (oldHost->interface) {
1312 afsUUID uuid = oldHost->interface->uuid;
1313 cb_conn = oldHost->callback_rxcon;
1314 rx_GetConnection(cb_conn);
1315 rx_SetConnDeadTime(cb_conn, 2);
1316 rx_SetConnHardDeadTime(cb_conn, AFS_HARDDEADTIME);
1318 code = RXAFSCB_ProbeUuid(cb_conn, &uuid);
1320 rx_SetConnDeadTime(cb_conn, 50);
1321 rx_SetConnHardDeadTime(cb_conn, AFS_HARDDEADTIME);
1322 rx_PutConnection(cb_conn);
1324 if (code && MultiProbeAlternateAddress_r(oldHost)) {
1332 /* The old host is either does not have a Uuid,
1333 * is not responding to Probes,
1334 * or does not have a matching Uuid.
1336 oldHost->hostFlags |= HOSTDELETED;
1337 h_Unlock_r(oldHost);
1338 /* Let the holder be last release */
1340 h_Release_r(oldHost);
1346 /* This is a new address for an existing host. Update
1347 * the list of interfaces for the existing host and
1348 * delete the host structure we just allocated. */
1349 if (oldHost->host != haddr || oldHost->port != hport) {
1351 ("CB: new addr %s:%d for old host %s:%d\n",
1352 afs_inet_ntoa_r(haddr, hoststr),
1354 afs_inet_ntoa_r(oldHost->host, hoststr2),
1355 ntohs(oldHost->port)));
1356 if (oldHost->host == haddr) {
1357 /* We have just been contacted by a client behind a NAT */
1358 removeInterfaceAddr_r(oldHost, oldHost->host, oldHost->port);
1361 struct Interface *interface = oldHost->interface;
1362 int number = oldHost->interface->numberOfInterfaces;
1363 for (i = 0, found = 0; i < number; i++) {
1364 if (interface->interface[i].addr == haddr &&
1365 interface->interface[i].port != hport) {
1371 /* We have just been contacted by a client that has been
1372 * seen from behind a NAT and at least one other address.
1374 removeInterfaceAddr_r(oldHost, haddr, interface->interface[i].port);
1377 addInterfaceAddr_r(oldHost, haddr, hport);
1378 oldHost->host = haddr;
1379 oldHost->port = hport;
1381 host->hostFlags |= HOSTDELETED;
1387 /* This really is a new host */
1388 hashInsertUuid_r(&identP->uuid, host);
1389 cb_conn = host->callback_rxcon;
1390 rx_GetConnection(cb_conn);
1393 RXAFSCB_InitCallBackState3(cb_conn,
1395 rx_PutConnection(cb_conn);
1400 ("InitCallBackState3 success on %s:%d\n",
1401 afs_inet_ntoa_r(host->host, hoststr),
1402 ntohs(host->port)));
1403 assert(interfValid == 1);
1404 initInterfaceAddr_r(host, &interf);
1409 afs_inet_ntoa_r(host->host, hoststr);
1411 ("CB: RCallBackConnectBack failed for %s:%d\n",
1412 hoststr, ntohs(host->port)));
1413 host->hostFlags |= VENUSDOWN;
1416 ("CB: RCallBackConnectBack succeeded for %s:%d\n",
1417 hoststr, ntohs(host->port)));
1418 host->hostFlags |= RESETDONE;
1421 if (caps.Capabilities_val
1422 && (caps.Capabilities_val[0] & CLIENT_CAPABILITY_ERRORTRANS))
1423 host->hostFlags |= HERRORTRANS;
1425 host->hostFlags &= ~(HERRORTRANS);
1426 host->hostFlags |= ALTADDR; /* host structure initialization complete */
1429 if (caps.Capabilities_val)
1430 free(caps.Capabilities_val);
1431 caps.Capabilities_val = NULL;
1432 caps.Capabilities_len = 0;
1438 static char localcellname[PR_MAXNAMELEN + 1];
1439 char local_realms[AFS_NUM_LREALMS][AFS_REALM_SZ];
1440 int num_lrealms = -1;
1446 afsconf_GetLocalCell(confDir, localcellname, PR_MAXNAMELEN);
1447 if (num_lrealms == -1) {
1449 for (i=0; i<AFS_NUM_LREALMS; i++) {
1450 if (afs_krb_get_lrealm(local_realms[i], i) != 0 /*KSUCCESS*/)
1456 ("afs_krb_get_lrealm failed, using %s.\n",
1458 strncpy(local_realms[0], localcellname, AFS_REALM_SZ);
1464 /* initialize the rest of the local realms to nullstring for debugging */
1465 for (; i<AFS_NUM_LREALMS; i++)
1466 local_realms[i][0] = '\0';
1468 rxcon_ident_key = rx_KeyCreate((rx_destructor_t) free);
1469 rxcon_client_key = rx_KeyCreate((rx_destructor_t) 0);
1470 #ifdef AFS_PTHREAD_ENV
1471 assert(pthread_mutex_init(&host_glock_mutex, NULL) == 0);
1472 #endif /* AFS_PTHREAD_ENV */
1476 MapName_r(char *aname, char *acell, afs_int32 * aval)
1481 afs_int32 anamelen, cnamelen;
1485 anamelen = strlen(aname);
1486 if (anamelen >= PR_MAXNAMELEN)
1487 return -1; /* bad name -- caller interprets this as anonymous, but retries later */
1489 lnames.namelist_len = 1;
1490 lnames.namelist_val = (prname *) aname; /* don't malloc in the common case */
1491 lids.idlist_len = 0;
1492 lids.idlist_val = NULL;
1494 cnamelen = strlen(acell);
1496 if (afs_is_foreign_ticket_name(aname, "", acell, localcellname)) {
1498 ("MapName: cell is foreign. cell=%s, localcell=%s, localrealms={%s,%s,%s,%s}\n",
1499 acell, localcellname, local_realms[0],local_realms[1],local_realms[2],local_realms[3]));
1500 if ((anamelen + cnamelen + 1) >= PR_MAXNAMELEN) {
1502 ("MapName: Name too long, using AnonymousID for %s@%s\n",
1504 *aval = AnonymousID;
1507 foreign = 1; /* attempt cross-cell authentication */
1508 tname = (char *)malloc(PR_MAXNAMELEN);
1510 ViceLog(0, ("Failed malloc in MapName_r\n"));
1513 strcpy(tname, aname);
1514 tname[anamelen] = '@';
1515 strcpy(tname + anamelen + 1, acell);
1516 lnames.namelist_val = (prname *) tname;
1521 code = pr_NameToId(&lnames, &lids);
1524 if (lids.idlist_val) {
1525 *aval = lids.idlist_val[0];
1526 if (*aval == AnonymousID) {
1528 ("MapName: NameToId on %s returns anonymousID\n",
1529 lnames.namelist_val));
1531 free(lids.idlist_val); /* return parms are not malloced in stub if server proc aborts */
1534 ("MapName: NameToId on '%s' is unknown\n",
1535 lnames.namelist_val));
1541 free(lnames.namelist_val); /* We allocated this above, so we must free it now. */
1549 /* NOTE: this returns the client with a Write lock */
1551 h_ID2Client(afs_int32 vid)
1553 register struct client *client;
1554 register struct host *host;
1557 for (host = hostList; host; host = host->next) {
1558 if (host->hostFlags & HOSTDELETED)
1560 for (client = host->FirstClient; client; client = client->next) {
1561 if (!client->deleted && client->ViceId == vid) {
1564 ObtainWriteLock(&client->lock);
1578 * Called by the server main loop. Returns a h_Held client, which must be
1579 * released later the main loop. Allocates a client if the matching one
1580 * isn't around. The client is returned with its reference count incremented
1581 * by one. The caller must call h_ReleaseClient_r when finished with
1585 h_FindClient_r(struct rx_connection *tcon)
1587 register struct client *client;
1588 register struct host *host;
1589 struct client *oldClient;
1594 #if (64-MAXKTCNAMELEN)
1595 ticket name length != 64
1599 char uname[PR_MAXNAMELEN];
1600 char tcell[MAXKTCREALMLEN];
1604 client = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
1607 h_Hold_r(client->host);
1608 if (!client->deleted && client->prfail != 2) {
1609 /* Could add shared lock on client here */
1610 /* note that we don't have to lock entry in this path to
1611 * ensure CPS is initialized, since we don't call rx_SetSpecific
1612 * until initialization is done, and we only get here if
1613 * rx_GetSpecific located the client structure.
1618 ObtainWriteLock(&client->lock); /* released at end */
1622 authClass = rx_SecurityClassOf((struct rx_connection *)tcon);
1624 ("FindClient: authenticating connection: authClass=%d\n",
1626 if (authClass == 1) {
1627 /* A bcrypt tickets, no longer supported */
1628 ViceLog(1, ("FindClient: bcrypt ticket, using AnonymousID\n"));
1629 viceid = AnonymousID;
1630 expTime = 0x7fffffff;
1631 } else if (authClass == 2) {
1634 /* kerberos ticket */
1635 code = rxkad_GetServerInfo(tcon, /*level */ 0, &expTime,
1636 tname, tinst, tcell, &kvno);
1638 ViceLog(1, ("Failed to get rxkad ticket info\n"));
1639 viceid = AnonymousID;
1640 expTime = 0x7fffffff;
1642 int ilen = strlen(tinst);
1644 ("FindClient: rxkad conn: name=%s,inst=%s,cell=%s,exp=%d,kvno=%d\n",
1645 tname, tinst, tcell, expTime, kvno));
1646 strncpy(uname, tname, sizeof(uname));
1648 if (strlen(uname) + 1 + ilen >= sizeof(uname))
1651 strcat(uname, tinst);
1653 /* translate the name to a vice id */
1654 code = MapName_r(uname, tcell, &viceid);
1658 ("failed to map name=%s, cell=%s -> code=%d\n", uname,
1661 viceid = AnonymousID;
1662 expTime = 0x7fffffff;
1666 viceid = AnonymousID; /* unknown security class */
1667 expTime = 0x7fffffff;
1670 if (!client) { /* loop */
1671 host = h_GetHost_r(tcon); /* Returns it h_Held */
1674 /* First try to find the client structure */
1675 for (client = host->FirstClient; client; client = client->next) {
1676 if (!client->deleted && (client->sid == rxr_CidOf(tcon))
1677 && (client->VenusEpoch == rxr_GetEpoch(tcon))) {
1678 if (client->tcon && (client->tcon != tcon)) {
1680 ("*** Vid=%d, sid=%x, tcon=%x, Tcon=%x ***\n",
1681 client->ViceId, client->sid, client->tcon,
1684 (struct client *)rx_GetSpecific(client->tcon,
1687 if (oldClient == client)
1688 rx_SetSpecific(client->tcon, rxcon_client_key,
1692 ("Client-conn mismatch: CL1=%x, CN=%x, CL2=%x\n",
1693 client, client->tcon, oldClient));
1695 client->tcon = (struct rx_connection *)0;
1699 ObtainWriteLock(&client->lock);
1705 /* Still no client structure - get one */
1708 /* Retry to find the client structure */
1709 for (client = host->FirstClient; client; client = client->next) {
1710 if (!client->deleted && (client->sid == rxr_CidOf(tcon))
1711 && (client->VenusEpoch == rxr_GetEpoch(tcon))) {
1713 goto retryfirstclient;
1718 ObtainWriteLock(&client->lock);
1719 client->refCount = 1;
1720 client->host = host;
1721 #if FS_STATS_DETAILED
1722 client->InSameNetwork = host->InSameNetwork;
1723 #endif /* FS_STATS_DETAILED */
1724 client->ViceId = viceid;
1725 client->expTime = expTime; /* rx only */
1726 client->authClass = authClass; /* rx only */
1727 client->sid = rxr_CidOf(tcon);
1728 client->VenusEpoch = rxr_GetEpoch(tcon);
1729 client->CPS.prlist_val = 0;
1730 client->CPS.prlist_len = 0;
1734 client->prfail = fail;
1736 if (!(client->CPS.prlist_val) || (viceid != client->ViceId)) {
1737 if (client->CPS.prlist_val && (client->ViceId != ANONYMOUSID)) {
1738 free(client->CPS.prlist_val);
1740 client->CPS.prlist_val = NULL;
1741 client->CPS.prlist_len = 0;
1742 client->ViceId = viceid;
1743 client->expTime = expTime;
1745 if (viceid == ANONYMOUSID) {
1746 client->CPS.prlist_len = AnonCPS.prlist_len;
1747 client->CPS.prlist_val = AnonCPS.prlist_val;
1750 code = pr_GetCPS(viceid, &client->CPS);
1755 ("pr_GetCPS failed(%d) for user %d, host %s:%d\n",
1756 code, viceid, afs_inet_ntoa_r(client->host->host,
1758 ntohs(client->host->port)));
1760 /* Although ubik_Call (called by pr_GetCPS) traverses thru
1761 * all protection servers and reevaluates things if no
1762 * sync server or quorum is found we could still end up
1763 * with one of these errors. In such case we would like to
1764 * reevaluate the rpc call to find if there's cps for this
1765 * guy. We treat other errors (except network failures
1766 * ones - i.e. code < 0) as an indication that there is no
1767 * CPS for this host. Ideally we could like to deal this
1768 * problem the other way around (i.e. if code == NOCPS
1769 * ignore else retry next time) but the problem is that
1770 * there're other errors (i.e. EPERM) for which we don't
1771 * want to retry and we don't know the whole code list!
1773 if (code < 0 || code == UNOQUORUM || code == UNOTSYNC)
1777 /* the disabling of system:administrators is so iffy and has so many
1778 * possible failure modes that we will disable it again */
1779 /* Turn off System:Administrator for safety
1780 * if (AL_IsAMember(SystemId, client->CPS) == 0)
1781 * assert(AL_DisableGroup(SystemId, client->CPS) == 0); */
1784 /* Now, tcon may already be set to a rock, since we blocked with no host
1785 * or client locks set above in pr_GetCPS (XXXX some locking is probably
1786 * required). So, before setting the RPC's rock, we should disconnect
1787 * the RPC from the other client structure's rock.
1789 oldClient = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
1790 if (oldClient && oldClient->tcon == tcon) {
1792 if (!oldClient->deleted) {
1793 /* if we didn't create it, it's not ours to put back */
1795 ViceLog(0, ("FindClient: stillborn client %x(%x); conn %x (host %s:%d) had client %x(%x)\n",
1796 client, client->sid, tcon,
1797 afs_inet_ntoa_r(rxr_HostOf(tcon), hoststr),
1798 ntohs(rxr_PortOf(tcon)),
1799 oldClient, oldClient->sid));
1800 if ((client->ViceId != ANONYMOUSID) && client->CPS.prlist_val) {
1801 free(client->CPS.prlist_val);
1802 client->CPS.prlist_val = NULL;
1804 client->CPS.prlist_len = 0;
1806 rx_SetSpecific(client->tcon, rxcon_client_key, (void *)0);
1809 /* We should perhaps check for 0 here */
1811 ReleaseWriteLock(&client->lock);
1816 ObtainWriteLock(&oldClient->lock);
1817 oldClient->refCount++;
1820 oldClient->tcon = (struct rx_connection *)0;
1821 ViceLog(0, ("FindClient: deleted client %x(%x) already had conn %x (host %s:%d), stolen by client %x(%x)\n",
1822 oldClient, oldClient->sid, tcon,
1823 afs_inet_ntoa_r(rxr_HostOf(tcon), hoststr),
1824 ntohs(rxr_PortOf(tcon)),
1825 client, client->sid));
1826 /* rx_SetSpecific will be done immediately below */
1829 /* Avoid chaining in more than once. */
1832 client->next = host->FirstClient;
1833 host->FirstClient = client;
1835 CurrentConnections++; /* increment number of connections */
1837 client->tcon = tcon;
1838 rx_SetSpecific(tcon, rxcon_client_key, client);
1839 ReleaseWriteLock(&client->lock);
1843 } /*h_FindClient_r */
1846 h_ReleaseClient_r(struct client *client)
1848 assert(client->refCount > 0);
1855 * Sigh: this one is used to get the client AGAIN within the individual
1856 * server routines. This does not bother h_Holding the host, since
1857 * this is assumed already have been done by the server main loop.
1858 * It does check tokens, since only the server routines can return the
1859 * VICETOKENDEAD error code
1862 GetClient(struct rx_connection *tcon, struct client **cp)
1864 register struct client *client;
1868 client = (struct client *)rx_GetSpecific(tcon, rxcon_client_key);
1869 if (client == NULL || client->tcon == NULL) {
1871 ("GetClient: no client in conn %x (host %x:%d), VBUSYING\n",
1872 tcon, rxr_HostOf(tcon),ntohs(rxr_PortOf(tcon))));
1876 if (rxr_CidOf(client->tcon) != client->sid) {
1878 ("GetClient: tcon %x tcon sid %d client sid %d\n",
1879 client->tcon, rxr_CidOf(client->tcon), client->sid));
1883 if (!(client && client->tcon && rxr_CidOf(client->tcon) == client->sid)) {
1885 ViceLog(0, ("GetClient: no client in conn %x\n", tcon));
1888 ("GetClient: tcon %x tcon sid %d client sid %d\n",
1889 client->tcon, client->tcon ? rxr_CidOf(client->tcon)
1890 : -1, client->sid));
1893 if (client && client->LastCall > client->expTime && client->expTime) {
1896 ("Token for %s at %s:%d expired %d\n", h_UserName(client),
1897 afs_inet_ntoa_r(client->host->host, hoststr),
1898 ntohs(client->host->port), client->expTime));
1900 return VICETOKENDEAD;
1910 PutClient(struct client **cp)
1916 h_ReleaseClient_r(*cp);
1923 /* Client user name for short term use. Note that this is NOT inexpensive */
1925 h_UserName(struct client *client)
1927 static char User[PR_MAXNAMELEN + 1];
1931 lids.idlist_len = 1;
1932 lids.idlist_val = (afs_int32 *) malloc(1 * sizeof(afs_int32));
1933 if (!lids.idlist_val) {
1934 ViceLog(0, ("Failed malloc in h_UserName\n"));
1937 lnames.namelist_len = 0;
1938 lnames.namelist_val = (prname *) 0;
1939 lids.idlist_val[0] = client->ViceId;
1940 if (pr_IdToName(&lids, &lnames)) {
1941 /* We need to free id we alloced above! */
1942 free(lids.idlist_val);
1943 return "*UNKNOWN USER NAME*";
1945 strncpy(User, lnames.namelist_val[0], PR_MAXNAMELEN);
1946 free(lids.idlist_val);
1947 free(lnames.namelist_val);
1957 ("Total Client entries = %d, blocks = %d; Host entries = %d, blocks = %d\n",
1958 CEs, CEBlocks, HTs, HTBlocks));
1964 h_PrintClient(register struct host *host, int held, StreamHandle_t * file)
1966 register struct client *client;
1973 if (host->hostFlags & HOSTDELETED) {
1977 (void)afs_snprintf(tmpStr, sizeof tmpStr,
1978 "Host %s:%d down = %d, LastCall %s",
1979 afs_inet_ntoa_r(host->host, hoststr),
1980 ntohs(host->port), (host->hostFlags & VENUSDOWN),
1981 afs_ctime((time_t *) & host->LastCall, tbuffer,
1983 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
1984 for (client = host->FirstClient; client; client = client->next) {
1985 if (!client->deleted) {
1987 (void)afs_snprintf(tmpStr, sizeof tmpStr,
1988 " user id=%d, name=%s, sl=%s till %s",
1989 client->ViceId, h_UserName(client),
1991 authClass ? "Authenticated" :
1992 "Not authenticated",
1994 authClass ? afs_ctime((time_t *) & client->
1998 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2000 (void)afs_snprintf(tmpStr, sizeof tmpStr,
2001 " user=%s, no current server connection\n",
2002 h_UserName(client));
2003 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2005 (void)afs_snprintf(tmpStr, sizeof tmpStr, " CPS-%d is [",
2006 client->CPS.prlist_len);
2007 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2008 if (client->CPS.prlist_val) {
2009 for (i = 0; i > client->CPS.prlist_len; i++) {
2010 (void)afs_snprintf(tmpStr, sizeof tmpStr, " %d",
2011 client->CPS.prlist_val[i]);
2012 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2015 sprintf(tmpStr, "]\n");
2016 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2022 } /*h_PrintClient */
2027 * Print a list of clients, with last security level and token value seen,
2037 StreamHandle_t *file = STREAM_OPEN(AFSDIR_SERVER_CLNTDUMP_FILEPATH, "w");
2041 ("Couldn't create client dump file %s\n",
2042 AFSDIR_SERVER_CLNTDUMP_FILEPATH));
2045 now = FT_ApproxTime();
2046 (void)afs_snprintf(tmpStr, sizeof tmpStr, "List of active users at %s\n",
2047 afs_ctime(&now, tbuffer, sizeof(tbuffer)));
2048 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2049 h_Enumerate(h_PrintClient, (char *)file);
2050 STREAM_REALLYCLOSE(file);
2051 ViceLog(0, ("Created client dump %s\n", AFSDIR_SERVER_CLNTDUMP_FILEPATH));
2058 h_DumpHost(register struct host *host, int held, StreamHandle_t * file)
2065 (void)afs_snprintf(tmpStr, sizeof tmpStr,
2066 "ip:%s port:%d hidx:%d cbid:%d lock:%x last:%u active:%u down:%d del:%d cons:%d cldel:%d\n\t hpfailed:%d hcpsCall:%u hcps [",
2067 afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port), host->index,
2068 host->cblist, CheckLock(&host->lock), host->LastCall,
2069 host->ActiveCall, (host->hostFlags & VENUSDOWN),
2070 host->hostFlags & HOSTDELETED, host->Console,
2071 host->hostFlags & CLIENTDELETED, host->hcpsfailed,
2073 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2074 if (host->hcps.prlist_val)
2075 for (i = 0; i < host->hcps.prlist_len; i++) {
2076 (void)afs_snprintf(tmpStr, sizeof tmpStr, " %d",
2077 host->hcps.prlist_val[i]);
2078 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2080 sprintf(tmpStr, "] [");
2081 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2082 if (host->interface)
2083 for (i = 0; i < host->interface->numberOfInterfaces; i++) {
2085 sprintf(tmpStr, " %s:%d",
2086 afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr),
2087 ntohs(host->interface->interface[i].port));
2088 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2090 sprintf(tmpStr, "] holds: ");
2091 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2093 for (i = 0; i < h_maxSlots; i++) {
2094 sprintf(tmpStr, "%04x", host->holds[i]);
2095 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2097 sprintf(tmpStr, " slot/bit: %d/%d\n", h_holdSlot(), h_holdbit());
2098 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2110 StreamHandle_t *file = STREAM_OPEN(AFSDIR_SERVER_HOSTDUMP_FILEPATH, "w");
2116 ("Couldn't create host dump file %s\n",
2117 AFSDIR_SERVER_HOSTDUMP_FILEPATH));
2120 now = FT_ApproxTime();
2121 (void)afs_snprintf(tmpStr, sizeof tmpStr, "List of active hosts at %s\n",
2122 afs_ctime(&now, tbuffer, sizeof(tbuffer)));
2123 (void)STREAM_WRITE(tmpStr, strlen(tmpStr), 1, file);
2124 h_Enumerate(h_DumpHost, (char *)file);
2125 STREAM_REALLYCLOSE(file);
2126 ViceLog(0, ("Created host dump %s\n", AFSDIR_SERVER_HOSTDUMP_FILEPATH));
2132 * This counts the number of workstations, the number of active workstations,
2133 * and the number of workstations declared "down" (i.e. not heard from
2134 * recently). An active workstation has received a call since the cutoff
2135 * time argument passed.
2138 h_GetWorkStats(int *nump, int *activep, int *delp, afs_int32 cutofftime)
2140 register struct host *host;
2141 register int num = 0, active = 0, del = 0;
2144 for (host = hostList; host; host = host->next) {
2145 if (!(host->hostFlags & HOSTDELETED)) {
2147 if (host->ActiveCall > cutofftime)
2149 if (host->hostFlags & VENUSDOWN)
2161 } /*h_GetWorkStats */
2164 /*------------------------------------------------------------------------
2165 * PRIVATE h_ClassifyAddress
2168 * Given a target IP address and a candidate IP address (both
2169 * in host byte order), classify the candidate into one of three
2170 * buckets in relation to the target by bumping the counters passed
2174 * a_targetAddr : Target address.
2175 * a_candAddr : Candidate address.
2176 * a_sameNetOrSubnetP : Ptr to counter to bump when the two
2177 * addresses are either in the same network
2178 * or the same subnet.
2179 * a_diffSubnetP : ...when the candidate is in a different
2181 * a_diffNetworkP : ...when the candidate is in a different
2188 * The target and candidate addresses are both in host byte
2189 * order, NOT network byte order, when passed in.
2193 *------------------------------------------------------------------------*/
2196 h_ClassifyAddress(afs_uint32 a_targetAddr, afs_uint32 a_candAddr,
2197 afs_int32 * a_sameNetOrSubnetP, afs_int32 * a_diffSubnetP,
2198 afs_int32 * a_diffNetworkP)
2199 { /*h_ClassifyAddress */
2201 afs_uint32 targetNet;
2202 afs_uint32 targetSubnet;
2204 afs_uint32 candSubnet;
2207 * Put bad values into the subnet info to start with.
2209 targetSubnet = (afs_uint32) 0;
2210 candSubnet = (afs_uint32) 0;
2213 * Pull out the network and subnetwork numbers from the target
2214 * and candidate addresses. We can short-circuit this whole
2215 * affair if the target and candidate addresses are not of the
2218 if (IN_CLASSA(a_targetAddr)) {
2219 if (!(IN_CLASSA(a_candAddr))) {
2220 (*a_diffNetworkP)++;
2223 targetNet = a_targetAddr & IN_CLASSA_NET;
2224 candNet = a_candAddr & IN_CLASSA_NET;
2225 if (IN_SUBNETA(a_targetAddr))
2226 targetSubnet = a_targetAddr & IN_CLASSA_SUBNET;
2227 if (IN_SUBNETA(a_candAddr))
2228 candSubnet = a_candAddr & IN_CLASSA_SUBNET;
2229 } else if (IN_CLASSB(a_targetAddr)) {
2230 if (!(IN_CLASSB(a_candAddr))) {
2231 (*a_diffNetworkP)++;
2234 targetNet = a_targetAddr & IN_CLASSB_NET;
2235 candNet = a_candAddr & IN_CLASSB_NET;
2236 if (IN_SUBNETB(a_targetAddr))
2237 targetSubnet = a_targetAddr & IN_CLASSB_SUBNET;
2238 if (IN_SUBNETB(a_candAddr))
2239 candSubnet = a_candAddr & IN_CLASSB_SUBNET;
2240 } /*Class B target */
2241 else if (IN_CLASSC(a_targetAddr)) {
2242 if (!(IN_CLASSC(a_candAddr))) {
2243 (*a_diffNetworkP)++;
2246 targetNet = a_targetAddr & IN_CLASSC_NET;
2247 candNet = a_candAddr & IN_CLASSC_NET;
2250 * Note that class C addresses can't have subnets,
2251 * so we leave the defaults untouched.
2253 } /*Class C target */
2255 targetNet = a_targetAddr;
2256 candNet = a_candAddr;
2257 } /*Class D address */
2260 * Now, simply compare the extracted net and subnet values for
2261 * the two addresses (which at this point are known to be of the
2264 if (targetNet == candNet) {
2265 if (targetSubnet == candSubnet)
2266 (*a_sameNetOrSubnetP)++;
2270 (*a_diffNetworkP)++;
2272 } /*h_ClassifyAddress */
2275 /*------------------------------------------------------------------------
2276 * EXPORTED h_GetHostNetStats
2279 * Iterate through the host table, and classify each (non-deleted)
2280 * host entry into ``proximity'' categories (same net or subnet,
2281 * different subnet, different network).
2284 * a_numHostsP : Set to total number of (non-deleted) hosts.
2285 * a_sameNetOrSubnetP : Set to # hosts on same net/subnet as server.
2286 * a_diffSubnetP : Set to # hosts on diff subnet as server.
2287 * a_diffNetworkP : Set to # hosts on diff network as server.
2293 * We only count non-deleted hosts. The storage pointed to by our
2294 * parameters is zeroed upon entry.
2298 *------------------------------------------------------------------------*/
2301 h_GetHostNetStats(afs_int32 * a_numHostsP, afs_int32 * a_sameNetOrSubnetP,
2302 afs_int32 * a_diffSubnetP, afs_int32 * a_diffNetworkP)
2303 { /*h_GetHostNetStats */
2305 register struct host *hostP; /*Ptr to current host entry */
2306 register afs_uint32 currAddr_HBO; /*Curr host addr, host byte order */
2309 * Clear out the storage pointed to by our parameters.
2311 *a_numHostsP = (afs_int32) 0;
2312 *a_sameNetOrSubnetP = (afs_int32) 0;
2313 *a_diffSubnetP = (afs_int32) 0;
2314 *a_diffNetworkP = (afs_int32) 0;
2317 for (hostP = hostList; hostP; hostP = hostP->next) {
2318 if (!(hostP->hostFlags & HOSTDELETED)) {
2320 * Bump the number of undeleted host entries found.
2321 * In classifying the current entry's address, make
2322 * sure to first convert to host byte order.
2325 currAddr_HBO = (afs_uint32) ntohl(hostP->host);
2326 h_ClassifyAddress(FS_HostAddr_HBO, currAddr_HBO,
2327 a_sameNetOrSubnetP, a_diffSubnetP,
2329 } /*Only look at non-deleted hosts */
2330 } /*For each host record hashed to this index */
2332 } /*h_GetHostNetStats */
2334 static afs_uint32 checktime;
2335 static afs_uint32 clientdeletetime;
2336 static struct AFSFid zerofid;
2340 * XXXX: This routine could use Multi-Rx to avoid serializing the timeouts.
2341 * Since it can serialize them, and pile up, it should be a separate LWP
2342 * from other events.
2345 CheckHost(register struct host *host, int held)
2347 register struct client *client;
2348 struct rx_connection *cb_conn = NULL;
2351 /* Host is held by h_Enumerate */
2353 for (client = host->FirstClient; client; client = client->next) {
2354 if (client->refCount == 0 && client->LastCall < clientdeletetime) {
2355 client->deleted = 1;
2356 host->hostFlags |= CLIENTDELETED;
2359 if (host->LastCall < checktime) {
2361 if (!(host->hostFlags & HOSTDELETED)) {
2362 cb_conn = host->callback_rxcon;
2363 rx_GetConnection(cb_conn);
2364 if (host->LastCall < clientdeletetime) {
2365 host->hostFlags |= HOSTDELETED;
2366 if (!(host->hostFlags & VENUSDOWN)) {
2367 host->hostFlags &= ~ALTADDR; /* alternate address invalid */
2368 if (host->interface) {
2371 RXAFSCB_InitCallBackState3(cb_conn,
2377 RXAFSCB_InitCallBackState(cb_conn);
2380 host->hostFlags |= ALTADDR; /* alternate addresses valid */
2383 (void)afs_inet_ntoa_r(host->host, hoststr);
2385 ("CB: RCallBackConnectBack (host.c) failed for host %s:%d\n",
2386 hoststr, ntohs(host->port)));
2387 host->hostFlags |= VENUSDOWN;
2389 /* Note: it's safe to delete hosts even if they have call
2390 * back state, because break delayed callbacks (called when a
2391 * message is received from the workstation) will always send a
2392 * break all call backs to the workstation if there is no
2397 if (!(host->hostFlags & VENUSDOWN) && host->cblist) {
2398 if (host->interface) {
2399 afsUUID uuid = host->interface->uuid;
2401 code = RXAFSCB_ProbeUuid(cb_conn, &uuid);
2404 if (MultiProbeAlternateAddress_r(host)) {
2406 (void)afs_inet_ntoa_r(host->host, hoststr);
2408 ("ProbeUuid failed for host %s:%d\n",
2409 hoststr, ntohs(host->port)));
2410 host->hostFlags |= VENUSDOWN;
2415 code = RXAFSCB_Probe(cb_conn);
2419 (void)afs_inet_ntoa_r(host->host, hoststr);
2421 ("Probe failed for host %s:%d\n", hoststr,
2422 ntohs(host->port)));
2423 host->hostFlags |= VENUSDOWN;
2429 rx_PutConnection(cb_conn);
2442 * Set VenusDown for any hosts that have not had a call in 15 minutes and
2443 * don't respond to a probe. Note that VenusDown can only be cleared if
2444 * a message is received from the host (see ServerLWP in file.c).
2445 * Delete hosts that have not had any calls in 1 hour, clients that
2446 * have not had any calls in 15 minutes.
2448 * This routine is called roughly every 5 minutes.
2453 afs_uint32 now = FT_ApproxTime();
2455 memset((char *)&zerofid, 0, sizeof(zerofid));
2457 * Send a probe to the workstation if it hasn't been heard from in
2460 checktime = now - 15 * 60;
2461 clientdeletetime = now - 120 * 60; /* 2 hours ago */
2462 h_Enumerate(CheckHost, NULL);
2467 * This is called with host locked and held. At this point, the
2468 * hostHashTable should not have any entries for the alternate
2469 * interfaces. This function has to insert these entries in the
2472 * The addresses in the interfaceAddr list are in host byte order.
2475 initInterfaceAddr_r(struct host *host, struct interfaceAddr *interf)
2482 struct Interface *interface;
2488 ("initInterfaceAddr : host %x numAddr %d\n", host->host,
2489 interf->numberOfInterfaces));
2491 number = interf->numberOfInterfaces;
2492 myAddr = host->host; /* current interface address */
2493 myPort = host->port; /* current port */
2495 /* validation checks */
2496 if (number < 0 || number > AFS_MAX_INTERFACE_ADDR) {
2498 ("Number of alternate addresses returned is %d\n", number));
2503 * Convert IP addresses to network byte order, and remove for
2504 * duplicate IP addresses from the interface list.
2506 for (i = 0, count = 0, found = 0; i < number; i++) {
2507 interf->addr_in[i] = htonl(interf->addr_in[i]);
2508 for (j = 0; j < count; j++) {
2509 if (interf->addr_in[j] == interf->addr_in[i])
2513 interf->addr_in[count] = interf->addr_in[i];
2514 if (interf->addr_in[count] == myAddr)
2521 * Allocate and initialize an interface structure for this host.
2524 interface = (struct Interface *)
2525 malloc(sizeof(struct Interface) +
2526 (sizeof(struct AddrPort) * (count - 1)));
2528 ViceLog(0, ("Failed malloc in initInterfaceAddr_r\n"));
2531 interface->numberOfInterfaces = count;
2533 interface = (struct Interface *)
2534 malloc(sizeof(struct Interface) + (sizeof(struct AddrPort) * count));
2536 interface->numberOfInterfaces = count + 1;
2537 interface->interface[count].addr = myAddr;
2538 interface->interface[count].port = myPort;
2540 interface->uuid = interf->uuid;
2541 for (i = 0; i < count; i++) {
2542 interface->interface[i].addr = interf->addr_in[i];
2543 /* We store the port as 7001 because the addresses reported by
2544 * TellMeAboutYourself and WhoAreYou RPCs are only valid if they
2545 * are coming from fully connected hosts (no NAT/PATs)
2547 interface->interface[i].port = htons(7001);
2550 assert(!host->interface);
2551 host->interface = interface;
2553 for (i = 0; i < host->interface->numberOfInterfaces; i++) {
2555 ViceLog(125, ("--- alt address %s:%d\n",
2556 afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr),
2557 ntohs(host->interface->interface[i].port)));
2563 /* deleted a HashChain structure for this address and host */
2564 /* returns 1 on success */
2566 hashDelete_r(afs_uint32 addr, afs_uint16 port, struct host *host)
2569 register struct h_hashChain **hp, *th;
2571 for (hp = &hostHashTable[h_HashIndex(addr)]; (th = *hp);) {
2572 assert(th->hostPtr);
2573 if (th->hostPtr == host && th->addr == addr && th->port == port) {
2587 ** prints out all alternate interface address for the host. The 'level'
2588 ** parameter indicates what level of debugging sets this output
2591 printInterfaceAddr(struct host *host, int level)
2596 if (host->interface) {
2597 /* check alternate addresses */
2598 number = host->interface->numberOfInterfaces;
2600 for (i = 0; i < number; i++)
2601 ViceLog(level, ("%s:%d ", afs_inet_ntoa_r(host->interface->interface[i].addr, hoststr),
2602 ntohs(host->interface->interface[i].port)));
2604 ViceLog(level, ("\n"));