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
13 #include <afsconfig.h>
14 #include "afs/param.h"
18 #include "afs/sysincludes.h" /* Standard vendor system headers */
21 #if !defined(AFS_LINUX20_ENV)
24 #include <netinet/in.h>
27 #include "h/hashing.h"
29 #if !defined(AFS_HPUX110_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_DARWIN_ENV)
30 #include <netinet/in_var.h>
31 #endif /* ! AFS_HPUX110_ENV */
32 #endif /* !defined(UKERNEL) */
34 #include "afsincludes.h" /* Afs-based standard headers */
35 #include "afs/afs_stats.h" /* afs statistics */
37 #if defined(AFS_SUN5_ENV)
39 #include <inet/common.h>
40 #include <netinet/ip6.h>
44 /* Exported variables */
45 afs_rwlock_t afs_xconn; /* allocation lock for new things */
46 afs_rwlock_t afs_xinterface; /* for multiple client address */
47 afs_int32 cryptall = 0; /* encrypt all communications */
49 /* some connection macros */
52 #define new_conn_vector(xcv) \
54 xcv = (struct sa_conn_vector *) \
55 afs_osi_Alloc(sizeof(struct sa_conn_vector)); \
57 memset((char *)xcv, 0, sizeof(struct sa_conn_vector)); \
61 /* select a connection to return (if no connection has lower utilization
63 #define conn_vec_select_conn(xcv, bix, conn) \
65 (bix) = ((xcv)->select_index)++ % CVEC_LEN; \
66 (conn) = &((xcv)->cvec[bix]); \
69 #define struct_conn(s) ((struct afs_conn *)(s))
71 #define REPORT_CONNECTIONS_ISSUED 0 /* enable to see utilization */
74 * Find a connection with call slots available, allocating one
75 * if nothing is available and we find an allocated slot
76 * @param xcv A connection vector
77 * @param create If set, a new connection may be created
79 static struct afs_conn *
80 find_preferred_connection(struct sa_conn_vector *xcv, int create)
83 struct afs_conn *tc = NULL;
86 for(cix = 0; cix < CVEC_LEN; ++cix) {
87 tc = &(xcv->cvec[cix]);
91 tc->forceConnectFS = 1;
97 if (tc->refCount < (RX_MAXCALLS-1)) {
100 } else if (cix == (CVEC_LEN-1))
101 conn_vec_select_conn(xcv, bix, tc);
103 } /* for cix < CVEC_LEN */
106 afs_warn("find_preferred_connection: no connection and !create\n");
115 #if REPORT_CONNECTIONS_ISSUED
116 afs_warn("Issuing conn %d refCount=%d parent refCount=%d\n", bix,
117 tc->refCount, xcv->refCount);
123 } /* find_preferred_connection */
127 * Release all connections for unix user xu at server xs
132 release_conns_user_server(struct unixuser *xu, struct server *xs)
137 struct sa_conn_vector *tcv, **lcv;
138 for (sa = (xs)->addr; sa; sa = sa->next_sa) {
140 for (tcv = *lcv; tcv; lcv = &tcv->next, tcv = *lcv) {
141 if (tcv->user == (xu) && tcv->refCount == 0) {
143 /* our old friend, the GLOCK */
144 glocked = ISAFS_GLOCK();
147 for(cix = 0; cix < CVEC_LEN; ++cix) {
148 tc = &(tcv->cvec[cix]);
150 rx_DestroyConnection(tc->id);
154 afs_osi_Free(tcv, sizeof(struct sa_conn_vector));
155 break; /* at most one instance per server */
156 } /*Found unreferenced connection for user */
158 } /*For each connection on the server */
160 } /* release_conns_user_server */
164 release_conns_vector(struct sa_conn_vector *xcv)
168 struct sa_conn_vector *tcv = NULL;
169 struct sa_conn_vector **lcv = NULL;
170 for (tcv = xcv; tcv; lcv = &tcv->next, tcv = *lcv) {
172 /* you know it, you love it, the GLOCK */
173 glocked = ISAFS_GLOCK();
176 for(cix = 0; cix < CVEC_LEN; ++cix) {
177 tc = &(tcv->cvec[cix]);
179 rx_DestroyConnection( tc->id );
183 afs_osi_Free(tcv, sizeof(struct sa_conn_vector));
186 } /* release_conns_vector */
189 unsigned int VNOSERVERS = 0;
192 * Pick a security object to use for a connection to a given server,
196 * The AFS connection for which the security object is required
197 * @param[out] secLevel
198 * The security level of the returned object
201 * An rx security object. This function is guaranteed to return
202 * an object, although that object may be rxnull (with a secLevel
205 static struct rx_securityClass *
206 afs_pickSecurityObject(struct afs_conn *conn, int *secLevel)
208 struct rx_securityClass *secObj = NULL;
209 union tokenUnion *token;
211 /* Do we have tokens ? */
212 if (conn->parent->user->states & UHasTokens) {
213 token = afs_FindToken(conn->parent->user->tokens, RX_SECIDX_KAD);
215 *secLevel = RX_SECIDX_KAD;
216 /* kerberos tickets on channel 2 */
217 secObj = rxkad_NewClientSecurityObject(
218 cryptall ? rxkad_crypt : rxkad_clear,
219 (struct ktc_encryptionKey *)
220 token->rxkad.clearToken.HandShakeKey,
221 token->rxkad.clearToken.AuthHandle,
222 token->rxkad.ticketLen, token->rxkad.ticket);
223 /* We're going to use this token, so populate the viced */
224 conn->parent->user->viceId = token->rxkad.clearToken.ViceId;
227 if (secObj == NULL) {
229 secObj = rxnull_NewClientSecurityObject();
237 * Try setting up a connection to the server containing the specified fid.
238 * Gets the volume, checks if it's up and does the connection by server address.
241 * @param areq Request filled in by the caller.
242 * @param locktype Type of lock that will be used.
244 * @return The conn struct, or NULL.
247 afs_Conn(struct VenusFid *afid, struct vrequest *areq,
248 afs_int32 locktype, struct rx_connection **rxconn)
250 u_short fsport = AFS_FSPORT;
252 struct afs_conn *tconn = NULL;
253 struct srvAddr *lowp = NULL;
257 struct srvAddr *sa1p;
261 AFS_STATCNT(afs_Conn);
262 /* Get fid's volume. */
263 tv = afs_GetVolume(afid, areq, READ_LOCK);
266 afs_FinalizeReq(areq);
267 areq->volumeError = 1;
272 if (tv->serverHost[0] && tv->serverHost[0]->cell) {
273 fsport = tv->serverHost[0]->cell->fsport;
278 /* First is always lowest rank, if it's up */
279 if ((tv->status[0] == not_busy) && tv->serverHost[0]
280 && !(tv->serverHost[0]->addr->sa_flags & SRVR_ISDOWN) &&
281 !(((areq->idleError > 0) || (areq->tokenError > 0))
282 && (areq->skipserver[0] == 1)))
283 lowp = tv->serverHost[0]->addr;
285 /* Otherwise we look at all of them. There are seven levels of
286 * not_busy. This means we will check a volume seven times before it
287 * is marked offline. Ideally, we only need two levels, but this
288 * serves a second purpose of waiting some number of seconds before
289 * the client decides the volume is offline (ie: a clone could finish
292 for (notbusy = not_busy; (!lowp && (notbusy <= end_not_busy)); notbusy++) {
293 for (i = 0; i < AFS_MAXHOSTS && tv->serverHost[i]; i++) {
294 if (((areq->tokenError > 0)||(areq->idleError > 0))
295 && (areq->skipserver[i] == 1))
297 if (tv->status[i] != notbusy) {
298 if (tv->status[i] == rd_busy || tv->status[i] == rdwr_busy) {
299 if (!areq->busyCount)
301 } else if (tv->status[i] == offline) {
302 if (!areq->volumeError)
303 areq->volumeError = VOLMISSING;
307 for (sa1p = tv->serverHost[i]->addr; sa1p; sa1p = sa1p->next_sa) {
308 if (sa1p->sa_flags & SRVR_ISDOWN)
310 if (!lowp || (lowp->sa_iprank > sa1p->sa_iprank))
315 afs_PutVolume(tv, READ_LOCK);
318 tu = afs_GetUser(areq->uid, afid->Cell, SHARED_LOCK);
319 tconn = afs_ConnBySA(lowp, fsport, afid->Cell, tu, 0 /*!force */ ,
320 1 /*create */ , locktype, rxconn);
322 afs_PutUser(tu, SHARED_LOCK);
330 * Connects to a server by it's server address.
332 * @param sap Server address.
333 * @param aport Server port.
335 * @param tu Connect as this user.
336 * @param force_if_down
338 * @param locktype Specifies type of lock to be used for this function.
340 * @return The new connection.
343 afs_ConnBySA(struct srvAddr *sap, unsigned short aport, afs_int32 acell,
344 struct unixuser *tu, int force_if_down, afs_int32 create,
345 afs_int32 locktype, struct rx_connection **rxconn)
347 int glocked, foundvec;
348 struct afs_conn *tc = NULL;
349 struct sa_conn_vector *tcv = NULL;
350 struct rx_securityClass *csec; /*Security class object */
351 int isec; /*Security index */
356 /* find cached connection */
357 ObtainSharedLock(&afs_xconn, 15);
359 for (tcv = sap->conns; tcv; tcv = tcv->next) {
360 if (tcv->user == tu && tcv->port == aport) {
361 /* return most eligible conn */
364 UpgradeSToWLock(&afs_xconn, 37);
365 tc = find_preferred_connection(tcv, create);
366 ConvertWToSLock(&afs_xconn);
371 if (!tc && !create) {
372 /* Not found and can't create a new one. */
373 ReleaseSharedLock(&afs_xconn);
377 if (AFS_IS_DISCONNECTED && !AFS_IN_SYNC) {
378 afs_warnuser("afs_ConnBySA: disconnected\n");
379 ReleaseSharedLock(&afs_xconn);
383 if (!foundvec && create) {
384 /* No such connection vector exists. Create one and splice it in.
385 * Make sure the server record has been marked as used (for the purposes
386 * of calculating up & down times, it's now considered to be an
387 * ``active'' server). Also make sure the server's lastUpdateEvalTime
388 * gets set, marking the time of its ``birth''.
390 UpgradeSToWLock(&afs_xconn, 37);
391 new_conn_vector(tcv);
396 tcv->next = sap->conns;
399 /* all struct afs_conn ptrs come from here */
400 tc = find_preferred_connection(tcv, create);
402 afs_ActivateServer(sap);
404 ConvertWToSLock(&afs_xconn);
405 } /* end of if (!tcv) */
408 /* Not found and no alternatives. */
409 ReleaseSharedLock(&afs_xconn);
413 if (tu->states & UTokensBad) {
414 /* we may still have an authenticated RPC connection here,
415 * we'll have to create a new, unauthenticated, connection.
416 * Perhaps a better way to do this would be to set
417 * conn->forceConnectFS on all conns when the token first goes
418 * bad, but that's somewhat trickier, due to locking
419 * constraints (though not impossible).
421 if (tc->id && (rx_SecurityClassOf(tc->id) != 0)) {
422 tc->forceConnectFS = 1; /* force recreation of connection */
424 tu->states &= ~UHasTokens; /* remove the authentication info */
427 glocked = ISAFS_GLOCK();
428 if (tc->forceConnectFS) {
429 UpgradeSToWLock(&afs_xconn, 38);
430 csec = (struct rx_securityClass *)0;
434 rx_DestroyConnection(tc->id);
439 * Stupid hack to determine if using vldb service or file system
442 if (aport == sap->server->cell->vlport)
448 csec = afs_pickSecurityObject(tc, &isec);
452 tc->id = rx_NewConnection(sap->sa_ip, aport, service, csec, isec);
456 rx_SetConnHardDeadTime(tc->id, afs_rx_harddead);
458 /* set to a RX_CALL_TIMEOUT error to allow MTU retry to trigger */
459 rx_SetServerConnIdleDeadErr(tc->id, RX_CALL_DEAD);
460 rx_SetConnIdleDeadTime(tc->id, afs_rx_idledead);
461 rx_SetMsgsizeRetryErr(tc->id, RX_MSGSIZE);
464 * Only do this for the base connection, not per-user.
465 * Will need to be revisited if/when CB gets security.
467 if ((isec == 0) && (service != 52) && !(tu->states & UTokensBad) &&
468 (tu->viceId == UNDEFVID))
469 rx_SetConnSecondsUntilNatPing(tc->id, 20);
471 tc->forceConnectFS = 0; /* apparently we're appropriately connected now */
474 ConvertWToSLock(&afs_xconn);
475 } /* end of if (tc->forceConnectFS)*/
478 rx_GetConnection(*rxconn);
480 ReleaseSharedLock(&afs_xconn);
485 * forceConnectFS is set whenever we must recompute the connection. UTokensBad
486 * is true only if we know that the tokens are bad. We thus clear this flag
487 * when we get a new set of tokens..
488 * Having force... true and UTokensBad true simultaneously means that the tokens
489 * went bad and we're supposed to create a new, unauthenticated, connection.
491 * @param aserver Server to connect to.
492 * @param aport Connection port.
493 * @param acell The cell where all of this happens.
494 * @param areq The request.
495 * @param aforce Force connection?
496 * @param locktype Type of lock to be used.
498 * @return The established connection.
501 afs_ConnByHost(struct server *aserver, unsigned short aport, afs_int32 acell,
502 struct vrequest *areq, int aforce, afs_int32 locktype,
503 struct rx_connection **rxconn)
506 struct afs_conn *tc = NULL;
507 struct srvAddr *sa = NULL;
511 AFS_STATCNT(afs_ConnByHost);
513 if (AFS_IS_DISCONNECTED && !AFS_IN_SYNC) {
514 afs_warnuser("afs_ConnByHost: disconnected\n");
519 1. look for an existing connection
520 2. create a connection at an address believed to be up
521 (if aforce is true, create a connection at the first address)
524 tu = afs_GetUser(areq->uid, acell, SHARED_LOCK);
526 for (sa = aserver->addr; sa; sa = sa->next_sa) {
527 tc = afs_ConnBySA(sa, aport, acell, tu, aforce,
528 0 /*don't create one */ ,
535 for (sa = aserver->addr; sa; sa = sa->next_sa) {
536 tc = afs_ConnBySA(sa, aport, acell, tu, aforce,
544 afs_PutUser(tu, SHARED_LOCK);
547 } /*afs_ConnByHost */
551 * Connect by multiple hosts.
552 * Try to connect to one of the hosts from the ahosts array.
554 * @param ahosts Multiple hosts to connect to.
555 * @param aport Connection port.
556 * @param acell The cell where all of this happens.
557 * @param areq The request.
558 * @param locktype Type of lock to be used.
560 * @return The established connection or NULL.
563 afs_ConnByMHosts(struct server *ahosts[], unsigned short aport,
564 afs_int32 acell, struct vrequest *areq,
565 afs_int32 locktype, struct rx_connection **rxconn)
568 struct afs_conn *tconn;
573 /* try to find any connection from the set */
574 AFS_STATCNT(afs_ConnByMHosts);
575 for (i = 0; i < AFS_MAXCELLHOSTS; i++) {
576 if ((ts = ahosts[i]) == NULL)
578 tconn = afs_ConnByHost(ts, aport, acell, areq, 0, locktype, rxconn);
585 } /*afs_ConnByMHosts */
589 * Decrement reference count to this connection.
594 afs_PutConn(struct afs_conn *ac, struct rx_connection *rxconn,
597 AFS_STATCNT(afs_PutConn);
599 ac->parent->refCount--;
600 rx_PutConnection(rxconn);
605 * Free up a connection vector, allowing, eg, code in afs_user.c
606 * to ignore how connections are stored/pooled
610 afs_ReleaseConns(struct sa_conn_vector *tcv) {
611 release_conns_vector(tcv);
616 * Free connection vector(s) for a user
620 afs_ReleaseConnsUser(struct unixuser *au) {
625 for (i = 0; i < NSERVERS; i++) {
626 for (ts = afs_servers[i]; ts; ts = ts->next) {
627 release_conns_user_server(au, ts);
628 } /*For each server on chain */
629 } /*For each chain */
634 * For multi homed clients, a RPC may timeout because of a
635 * client network interface going down. We need to reopen new
636 * connections in this case.
638 * @param sap Server address.
641 ForceNewConnections(struct srvAddr *sap)
644 struct afs_conn *tc = NULL;
645 struct sa_conn_vector *tcv = NULL;
648 return; /* defensive check */
650 ObtainWriteLock(&afs_xconn, 413);
651 for (tcv = sap->conns; tcv; tcv = tcv->next) {
652 for(cix = 0; cix < CVEC_LEN; ++cix) {
653 tc = &(tcv->cvec[cix]);
655 tc->forceConnectFS = 1;
658 ReleaseWriteLock(&afs_xconn);