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"
20 #include "afs/sysincludes.h" /* Standard vendor system headers */
23 #if !defined(AFS_LINUX20_ENV)
26 #include <netinet/in.h>
29 #include "h/hashing.h"
31 #if !defined(AFS_HPUX110_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_DARWIN60_ENV)
32 #include <netinet/in_var.h>
33 #endif /* ! AFS_HPUX110_ENV */
34 #endif /* !defined(UKERNEL) */
36 #include "afsincludes.h" /* Afs-based standard headers */
37 #include "afs/afs_stats.h" /* afs statistics */
39 #if defined(AFS_SUN56_ENV)
41 #include <inet/common.h>
42 #if defined(AFS_SUN58_ENV)
43 #include <netinet/ip6.h>
48 /* Exported variables */
49 afs_rwlock_t afs_xconn; /* allocation lock for new things */
50 afs_rwlock_t afs_xinterface; /* for multiple client address */
51 afs_int32 cryptall = 0; /* encrypt all communications */
54 unsigned int VNOSERVERS = 0;
57 * Try setting up a connection to the server containing the specified fid.
58 * Gets the volume, checks if it's up and does the connection by server address.
61 * @param areq Request filled in by the caller.
62 * @param locktype Type of lock that will be used.
64 * @return The conn struct, or NULL.
67 afs_Conn(register struct VenusFid *afid, register struct vrequest *areq,
70 u_short fsport = AFS_FSPORT;
72 struct conn *tconn = NULL;
73 struct srvAddr *lowp = NULL;
79 AFS_STATCNT(afs_Conn);
80 /* Get fid's volume. */
81 tv = afs_GetVolume(afid, areq, READ_LOCK);
84 afs_FinalizeReq(areq);
85 areq->volumeError = 1;
90 if (tv->serverHost[0] && tv->serverHost[0]->cell) {
91 fsport = tv->serverHost[0]->cell->fsport;
96 /* First is always lowest rank, if it's up */
97 if ((tv->status[0] == not_busy) && tv->serverHost[0]
98 && !(tv->serverHost[0]->addr->sa_flags & SRVR_ISDOWN) &&
99 !((areq->initd == 1) &&
100 ((areq->idleError > 0) || (areq->tokenError > 0))
101 && (areq->skipserver[0] == 1)))
102 lowp = tv->serverHost[0]->addr;
104 /* Otherwise we look at all of them. There are seven levels of
105 * not_busy. This means we will check a volume seven times before it
106 * is marked offline. Ideally, we only need two levels, but this
107 * serves a second purpose of waiting some number of seconds before
108 * the client decides the volume is offline (ie: a clone could finish
111 for (notbusy = not_busy; (!lowp && (notbusy <= end_not_busy)); notbusy++) {
112 for (i = 0; i < MAXHOSTS && tv->serverHost[i]; i++) {
113 if (((areq->tokenError > 0)||(areq->idleError > 0))
114 && (areq->skipserver[i] == 1))
116 if (tv->status[i] != notbusy) {
117 if (tv->status[i] == rd_busy || tv->status[i] == rdwr_busy) {
118 if (!areq->busyCount)
120 } else if (tv->status[i] == offline) {
121 if (!areq->volumeError)
122 areq->volumeError = VOLMISSING;
126 for (sa1p = tv->serverHost[i]->addr; sa1p; sa1p = sa1p->next_sa) {
127 if (sa1p->sa_flags & SRVR_ISDOWN)
129 if (!lowp || (lowp->sa_iprank > sa1p->sa_iprank))
134 afs_PutVolume(tv, READ_LOCK);
137 tu = afs_GetUser(areq->uid, afid->Cell, SHARED_LOCK);
138 tconn = afs_ConnBySA(lowp, fsport, afid->Cell, tu, 0 /*!force */ ,
139 1 /*create */ , locktype);
141 afs_PutUser(tu, SHARED_LOCK);
149 * Connects to a server by it's server address.
151 * @param sap Server address.
152 * @param aport Server port.
154 * @param tu Connect as this user.
155 * @param force_if_down
157 * @param locktype Specifies type of lock to be used for this function.
159 * @return The new connection.
162 afs_ConnBySA(struct srvAddr *sap, unsigned short aport, afs_int32 acell,
163 struct unixuser *tu, int force_if_down, afs_int32 create,
167 struct rx_securityClass *csec; /*Security class object */
168 int isec; /*Security index */
171 if (!sap || ((sap->sa_flags & SRVR_ISDOWN) && !force_if_down)) {
172 /* sa is known down, and we don't want to force it. */
176 ObtainSharedLock(&afs_xconn, 15);
177 /* Get conn by port and user. */
178 for (tc = sap->conns; tc; tc = tc->next) {
179 if (tc->user == tu && tc->port == aport) {
184 if (!tc && !create) {
185 /* Not found and can't create a new one. */
186 ReleaseSharedLock(&afs_xconn);
190 if (AFS_IS_DISCONNECTED && !AFS_IN_SYNC) {
191 afs_warnuser("afs_ConnBySA: disconnected\n");
192 ReleaseSharedLock(&afs_xconn);
197 /* No such connection structure exists. Create one and splice it in.
198 * Make sure the server record has been marked as used (for the purposes
199 * of calculating up & down times, it's now considered to be an
200 * ``active'' server). Also make sure the server's lastUpdateEvalTime
201 * gets set, marking the time of its ``birth''.
203 UpgradeSToWLock(&afs_xconn, 37);
204 tc = (struct conn *)afs_osi_Alloc(sizeof(struct conn));
205 memset((char *)tc, 0, sizeof(struct conn));
210 tc->refCount = 0; /* bumped below */
211 tc->forceConnectFS = 1;
212 tc->id = (struct rx_connection *)0;
213 tc->next = sap->conns;
215 afs_ActivateServer(sap);
217 ConvertWToSLock(&afs_xconn);
218 } /* end of if (!tc) */
221 if (tu->states & UTokensBad) {
222 /* we may still have an authenticated RPC connection here,
223 * we'll have to create a new, unauthenticated, connection.
224 * Perhaps a better way to do this would be to set
225 * conn->forceConnectFS on all conns when the token first goes
226 * bad, but that's somewhat trickier, due to locking
227 * constraints (though not impossible).
229 if (tc->id && (rx_SecurityClassOf(tc->id) != 0)) {
230 tc->forceConnectFS = 1; /* force recreation of connection */
232 tu->vid = UNDEFVID; /* forcibly disconnect the authentication info */
235 if (tc->forceConnectFS) {
236 UpgradeSToWLock(&afs_xconn, 38);
237 csec = (struct rx_securityClass *)0;
240 rx_DestroyConnection(tc->id);
244 * Stupid hack to determine if using vldb service or file system
247 if (aport == sap->server->cell->vlport)
252 if (tu->vid != UNDEFVID) {
261 /* kerberos tickets on channel 2 */
262 csec = rxkad_NewClientSecurityObject(level,
263 (struct ktc_encryptionKey *)tu->ct.HandShakeKey,
265 tu->ct.AuthHandle, tu->stLen,
269 csec = rxnull_NewClientSecurityObject();
271 tc->id = rx_NewConnection(sap->sa_ip, aport, service, csec, isec);
274 rx_SetConnHardDeadTime(tc->id, afs_rx_harddead);
276 rx_SetConnIdleDeadTime(tc->id, afs_rx_idledead);
278 tc->forceConnectFS = 0; /* apparently we're appropriately connected now */
281 ConvertWToSLock(&afs_xconn);
282 } /* end of if (tc->forceConnectFS)*/
284 ReleaseSharedLock(&afs_xconn);
289 * forceConnectFS is set whenever we must recompute the connection. UTokensBad
290 * is true only if we know that the tokens are bad. We thus clear this flag
291 * when we get a new set of tokens..
292 * Having force... true and UTokensBad true simultaneously means that the tokens
293 * went bad and we're supposed to create a new, unauthenticated, connection.
295 * @param aserver Server to connect to.
296 * @param aport Connection port.
297 * @param acell The cell where all of this happens.
298 * @param areq The request.
299 * @param aforce Force connection?
300 * @param locktype Type of lock to be used.
302 * @return The established connection.
305 afs_ConnByHost(struct server *aserver, unsigned short aport, afs_int32 acell,
306 struct vrequest *areq, int aforce, afs_int32 locktype)
310 struct srvAddr *sa = 0;
312 AFS_STATCNT(afs_ConnByHost);
314 if (AFS_IS_DISCONNECTED && !AFS_IN_SYNC) {
315 afs_warnuser("afs_ConnByHost: disconnected\n");
320 1. look for an existing connection
321 2. create a connection at an address believed to be up
322 (if aforce is true, create a connection at the first address)
325 tu = afs_GetUser(areq->uid, acell, SHARED_LOCK);
327 for (sa = aserver->addr; sa; sa = sa->next_sa) {
328 tc = afs_ConnBySA(sa, aport, acell, tu, aforce,
329 0 /*don't create one */ ,
336 for (sa = aserver->addr; sa; sa = sa->next_sa) {
337 tc = afs_ConnBySA(sa, aport, acell, tu, aforce,
345 afs_PutUser(tu, SHARED_LOCK);
348 } /*afs_ConnByHost */
352 * Connect by multiple hosts.
353 * Try to connect to one of the hosts from the ahosts array.
355 * @param ahosts Multiple hosts to connect to.
356 * @param aport Connection port.
357 * @param acell The cell where all of this happens.
358 * @param areq The request.
359 * @param locktype Type of lock to be used.
361 * @return The established connection or NULL.
364 afs_ConnByMHosts(struct server *ahosts[], unsigned short aport,
365 afs_int32 acell, register struct vrequest *areq,
368 register afs_int32 i;
369 register struct conn *tconn;
370 register struct server *ts;
372 /* try to find any connection from the set */
373 AFS_STATCNT(afs_ConnByMHosts);
374 for (i = 0; i < MAXCELLHOSTS; i++) {
375 if ((ts = ahosts[i]) == NULL)
377 tconn = afs_ConnByHost(ts, aport, acell, areq, 0, locktype);
384 } /*afs_ConnByMHosts */
388 * Decrement reference count to this connection.
393 afs_PutConn(register struct conn *ac, afs_int32 locktype)
395 AFS_STATCNT(afs_PutConn);
401 * For multi homed clients, a RPC may timeout because of a
402 * client network interface going down. We need to reopen new
403 * connections in this case.
405 * @param sap Server address.
408 ForceNewConnections(struct srvAddr *sap)
413 return; /* defensive check */
415 ObtainWriteLock(&afs_xconn, 413);
416 for (tc = sap->conns; tc; tc = tc->next)
417 tc->forceConnectFS = 1;
418 ReleaseWriteLock(&afs_xconn);