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->idleError > 0) || (areq->tokenError > 0))
100 && (areq->skipserver[0] == 1)))
101 lowp = tv->serverHost[0]->addr;
103 /* Otherwise we look at all of them. There are seven levels of
104 * not_busy. This means we will check a volume seven times before it
105 * is marked offline. Ideally, we only need two levels, but this
106 * serves a second purpose of waiting some number of seconds before
107 * the client decides the volume is offline (ie: a clone could finish
110 for (notbusy = not_busy; (!lowp && (notbusy <= end_not_busy)); notbusy++) {
111 for (i = 0; i < MAXHOSTS && tv->serverHost[i]; i++) {
112 if (((areq->tokenError > 0)||(areq->idleError > 0))
113 && (areq->skipserver[i] == 1))
115 if (tv->status[i] != notbusy) {
116 if (tv->status[i] == rd_busy || tv->status[i] == rdwr_busy) {
117 if (!areq->busyCount)
119 } else if (tv->status[i] == offline) {
120 if (!areq->volumeError)
121 areq->volumeError = VOLMISSING;
125 for (sa1p = tv->serverHost[i]->addr; sa1p; sa1p = sa1p->next_sa) {
126 if (sa1p->sa_flags & SRVR_ISDOWN)
128 if (!lowp || (lowp->sa_iprank > sa1p->sa_iprank))
133 afs_PutVolume(tv, READ_LOCK);
136 tu = afs_GetUser(areq->uid, afid->Cell, SHARED_LOCK);
137 tconn = afs_ConnBySA(lowp, fsport, afid->Cell, tu, 0 /*!force */ ,
138 1 /*create */ , locktype);
140 afs_PutUser(tu, SHARED_LOCK);
148 * Connects to a server by it's server address.
150 * @param sap Server address.
151 * @param aport Server port.
153 * @param tu Connect as this user.
154 * @param force_if_down
156 * @param locktype Specifies type of lock to be used for this function.
158 * @return The new connection.
161 afs_ConnBySA(struct srvAddr *sap, unsigned short aport, afs_int32 acell,
162 struct unixuser *tu, int force_if_down, afs_int32 create,
166 struct rx_securityClass *csec; /*Security class object */
167 int isec; /*Security index */
170 if (!sap || ((sap->sa_flags & SRVR_ISDOWN) && !force_if_down)) {
171 /* sa is known down, and we don't want to force it. */
175 ObtainSharedLock(&afs_xconn, 15);
176 /* Get conn by port and user. */
177 for (tc = sap->conns; tc; tc = tc->next) {
178 if (tc->user == tu && tc->port == aport) {
183 if (!tc && !create) {
184 /* Not found and can't create a new one. */
185 ReleaseSharedLock(&afs_xconn);
189 if (AFS_IS_DISCONNECTED && !AFS_IN_SYNC) {
190 afs_warnuser("afs_ConnBySA: disconnected\n");
191 ReleaseSharedLock(&afs_xconn);
196 /* No such connection structure exists. Create one and splice it in.
197 * Make sure the server record has been marked as used (for the purposes
198 * of calculating up & down times, it's now considered to be an
199 * ``active'' server). Also make sure the server's lastUpdateEvalTime
200 * gets set, marking the time of its ``birth''.
202 UpgradeSToWLock(&afs_xconn, 37);
203 tc = (struct conn *)afs_osi_Alloc(sizeof(struct conn));
204 memset((char *)tc, 0, sizeof(struct conn));
209 tc->refCount = 0; /* bumped below */
210 tc->forceConnectFS = 1;
211 tc->id = (struct rx_connection *)0;
212 tc->next = sap->conns;
214 afs_ActivateServer(sap);
216 ConvertWToSLock(&afs_xconn);
217 } /* end of if (!tc) */
220 if (tu->states & UTokensBad) {
221 /* we may still have an authenticated RPC connection here,
222 * we'll have to create a new, unauthenticated, connection.
223 * Perhaps a better way to do this would be to set
224 * conn->forceConnectFS on all conns when the token first goes
225 * bad, but that's somewhat trickier, due to locking
226 * constraints (though not impossible).
228 if (tc->id && (rx_SecurityClassOf(tc->id) != 0)) {
229 tc->forceConnectFS = 1; /* force recreation of connection */
231 tu->vid = UNDEFVID; /* forcibly disconnect the authentication info */
234 if (tc->forceConnectFS) {
235 UpgradeSToWLock(&afs_xconn, 38);
236 csec = (struct rx_securityClass *)0;
239 rx_DestroyConnection(tc->id);
243 * Stupid hack to determine if using vldb service or file system
246 if (aport == sap->server->cell->vlport)
251 if (tu->vid != UNDEFVID) {
260 /* kerberos tickets on channel 2 */
261 csec = rxkad_NewClientSecurityObject(level,
262 (struct ktc_encryptionKey *)tu->ct.HandShakeKey,
264 tu->ct.AuthHandle, tu->stLen,
268 csec = rxnull_NewClientSecurityObject();
270 tc->id = rx_NewConnection(sap->sa_ip, aport, service, csec, isec);
273 rx_SetConnHardDeadTime(tc->id, afs_rx_harddead);
275 rx_SetConnIdleDeadTime(tc->id, afs_rx_idledead);
277 tc->forceConnectFS = 0; /* apparently we're appropriately connected now */
280 ConvertWToSLock(&afs_xconn);
281 } /* end of if (tc->forceConnectFS)*/
283 ReleaseSharedLock(&afs_xconn);
288 * forceConnectFS is set whenever we must recompute the connection. UTokensBad
289 * is true only if we know that the tokens are bad. We thus clear this flag
290 * when we get a new set of tokens..
291 * Having force... true and UTokensBad true simultaneously means that the tokens
292 * went bad and we're supposed to create a new, unauthenticated, connection.
294 * @param aserver Server to connect to.
295 * @param aport Connection port.
296 * @param acell The cell where all of this happens.
297 * @param areq The request.
298 * @param aforce Force connection?
299 * @param locktype Type of lock to be used.
301 * @return The established connection.
304 afs_ConnByHost(struct server *aserver, unsigned short aport, afs_int32 acell,
305 struct vrequest *areq, int aforce, afs_int32 locktype)
309 struct srvAddr *sa = 0;
311 AFS_STATCNT(afs_ConnByHost);
313 if (AFS_IS_DISCONNECTED && !AFS_IN_SYNC) {
314 afs_warnuser("afs_ConnByHost: disconnected\n");
319 1. look for an existing connection
320 2. create a connection at an address believed to be up
321 (if aforce is true, create a connection at the first address)
324 tu = afs_GetUser(areq->uid, acell, SHARED_LOCK);
326 for (sa = aserver->addr; sa; sa = sa->next_sa) {
327 tc = afs_ConnBySA(sa, aport, acell, tu, aforce,
328 0 /*don't create one */ ,
335 for (sa = aserver->addr; sa; sa = sa->next_sa) {
336 tc = afs_ConnBySA(sa, aport, acell, tu, aforce,
344 afs_PutUser(tu, SHARED_LOCK);
347 } /*afs_ConnByHost */
351 * Connect by multiple hosts.
352 * Try to connect to one of the hosts from the ahosts array.
354 * @param ahosts Multiple hosts to connect to.
355 * @param aport Connection port.
356 * @param acell The cell where all of this happens.
357 * @param areq The request.
358 * @param locktype Type of lock to be used.
360 * @return The established connection or NULL.
363 afs_ConnByMHosts(struct server *ahosts[], unsigned short aport,
364 afs_int32 acell, register struct vrequest *areq,
367 register afs_int32 i;
368 register struct conn *tconn;
369 register struct server *ts;
371 /* try to find any connection from the set */
372 AFS_STATCNT(afs_ConnByMHosts);
373 for (i = 0; i < MAXCELLHOSTS; i++) {
374 if ((ts = ahosts[i]) == NULL)
376 tconn = afs_ConnByHost(ts, aport, acell, areq, 0, locktype);
383 } /*afs_ConnByMHosts */
387 * Decrement reference count to this connection.
392 afs_PutConn(register struct conn *ac, afs_int32 locktype)
394 AFS_STATCNT(afs_PutConn);
400 * For multi homed clients, a RPC may timeout because of a
401 * client network interface going down. We need to reopen new
402 * connections in this case.
404 * @param sap Server address.
407 ForceNewConnections(struct srvAddr *sap)
412 return; /* defensive check */
414 ObtainWriteLock(&afs_xconn, 413);
415 for (tc = sap->conns; tc; tc = tc->next)
416 tc->forceConnectFS = 1;
417 ReleaseWriteLock(&afs_xconn);