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_DARWIN60_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_SUN56_ENV)
39 #include <inet/common.h>
40 #if defined(AFS_SUN58_ENV)
41 #include <netinet/ip6.h>
46 /* Exported variables */
47 afs_rwlock_t afs_xconn; /* allocation lock for new things */
48 afs_rwlock_t afs_xinterface; /* for multiple client address */
49 afs_int32 cryptall = 0; /* encrypt all communications */
52 unsigned int VNOSERVERS = 0;
55 * Try setting up a connection to the server containing the specified fid.
56 * Gets the volume, checks if it's up and does the connection by server address.
59 * @param areq Request filled in by the caller.
60 * @param locktype Type of lock that will be used.
62 * @return The conn struct, or NULL.
65 afs_Conn(register struct VenusFid *afid, register struct vrequest *areq,
68 u_short fsport = AFS_FSPORT;
70 struct afs_conn *tconn = NULL;
71 struct srvAddr *lowp = NULL;
77 AFS_STATCNT(afs_Conn);
78 /* Get fid's volume. */
79 tv = afs_GetVolume(afid, areq, READ_LOCK);
82 afs_FinalizeReq(areq);
83 areq->volumeError = 1;
88 if (tv->serverHost[0] && tv->serverHost[0]->cell) {
89 fsport = tv->serverHost[0]->cell->fsport;
94 /* First is always lowest rank, if it's up */
95 if ((tv->status[0] == not_busy) && tv->serverHost[0]
96 && !(tv->serverHost[0]->addr->sa_flags & SRVR_ISDOWN) &&
97 !(((areq->idleError > 0) || (areq->tokenError > 0))
98 && (areq->skipserver[0] == 1)))
99 lowp = tv->serverHost[0]->addr;
101 /* Otherwise we look at all of them. There are seven levels of
102 * not_busy. This means we will check a volume seven times before it
103 * is marked offline. Ideally, we only need two levels, but this
104 * serves a second purpose of waiting some number of seconds before
105 * the client decides the volume is offline (ie: a clone could finish
108 for (notbusy = not_busy; (!lowp && (notbusy <= end_not_busy)); notbusy++) {
109 for (i = 0; i < MAXHOSTS && tv->serverHost[i]; i++) {
110 if (((areq->tokenError > 0)||(areq->idleError > 0))
111 && (areq->skipserver[i] == 1))
113 if (tv->status[i] != notbusy) {
114 if (tv->status[i] == rd_busy || tv->status[i] == rdwr_busy) {
115 if (!areq->busyCount)
117 } else if (tv->status[i] == offline) {
118 if (!areq->volumeError)
119 areq->volumeError = VOLMISSING;
123 for (sa1p = tv->serverHost[i]->addr; sa1p; sa1p = sa1p->next_sa) {
124 if (sa1p->sa_flags & SRVR_ISDOWN)
126 if (!lowp || (lowp->sa_iprank > sa1p->sa_iprank))
131 afs_PutVolume(tv, READ_LOCK);
134 tu = afs_GetUser(areq->uid, afid->Cell, SHARED_LOCK);
135 tconn = afs_ConnBySA(lowp, fsport, afid->Cell, tu, 0 /*!force */ ,
136 1 /*create */ , locktype);
138 afs_PutUser(tu, SHARED_LOCK);
146 * Connects to a server by it's server address.
148 * @param sap Server address.
149 * @param aport Server port.
151 * @param tu Connect as this user.
152 * @param force_if_down
154 * @param locktype Specifies type of lock to be used for this function.
156 * @return The new connection.
159 afs_ConnBySA(struct srvAddr *sap, unsigned short aport, afs_int32 acell,
160 struct unixuser *tu, int force_if_down, afs_int32 create,
163 struct afs_conn *tc = 0;
164 struct rx_securityClass *csec; /*Security class object */
165 int isec; /*Security index */
168 if (!sap || ((sap->sa_flags & SRVR_ISDOWN) && !force_if_down)) {
169 /* sa is known down, and we don't want to force it. */
173 ObtainSharedLock(&afs_xconn, 15);
174 /* Get conn by port and user. */
175 for (tc = sap->conns; tc; tc = tc->next) {
176 if (tc->user == tu && tc->port == aport) {
181 if (!tc && !create) {
182 /* Not found and can't create a new one. */
183 ReleaseSharedLock(&afs_xconn);
187 if (AFS_IS_DISCONNECTED && !AFS_IN_SYNC) {
188 afs_warnuser("afs_ConnBySA: disconnected\n");
189 ReleaseSharedLock(&afs_xconn);
194 /* No such connection structure exists. Create one and splice it in.
195 * Make sure the server record has been marked as used (for the purposes
196 * of calculating up & down times, it's now considered to be an
197 * ``active'' server). Also make sure the server's lastUpdateEvalTime
198 * gets set, marking the time of its ``birth''.
200 UpgradeSToWLock(&afs_xconn, 37);
201 tc = (struct afs_conn *)afs_osi_Alloc(sizeof(struct afs_conn));
202 memset(tc, 0, sizeof(struct afs_conn));
207 tc->refCount = 0; /* bumped below */
208 tc->forceConnectFS = 1;
209 tc->id = (struct rx_connection *)0;
210 tc->next = sap->conns;
212 afs_ActivateServer(sap);
214 ConvertWToSLock(&afs_xconn);
215 } /* end of if (!tc) */
218 if (tu->states & UTokensBad) {
219 /* we may still have an authenticated RPC connection here,
220 * we'll have to create a new, unauthenticated, connection.
221 * Perhaps a better way to do this would be to set
222 * conn->forceConnectFS on all conns when the token first goes
223 * bad, but that's somewhat trickier, due to locking
224 * constraints (though not impossible).
226 if (tc->id && (rx_SecurityClassOf(tc->id) != 0)) {
227 tc->forceConnectFS = 1; /* force recreation of connection */
229 tu->vid = UNDEFVID; /* forcibly disconnect the authentication info */
232 if (tc->forceConnectFS) {
233 UpgradeSToWLock(&afs_xconn, 38);
234 csec = (struct rx_securityClass *)0;
237 rx_DestroyConnection(tc->id);
241 * Stupid hack to determine if using vldb service or file system
244 if (aport == sap->server->cell->vlport)
249 if (tu->vid != UNDEFVID) {
258 /* kerberos tickets on channel 2 */
259 csec = rxkad_NewClientSecurityObject(level,
260 (struct ktc_encryptionKey *)tu->ct.HandShakeKey,
262 tu->ct.AuthHandle, tu->stLen,
266 csec = rxnull_NewClientSecurityObject();
268 tc->id = rx_NewConnection(sap->sa_ip, aport, service, csec, isec);
271 rx_SetConnHardDeadTime(tc->id, afs_rx_harddead);
273 rx_SetConnIdleDeadTime(tc->id, afs_rx_idledead);
275 tc->forceConnectFS = 0; /* apparently we're appropriately connected now */
278 ConvertWToSLock(&afs_xconn);
279 } /* end of if (tc->forceConnectFS)*/
281 ReleaseSharedLock(&afs_xconn);
286 * forceConnectFS is set whenever we must recompute the connection. UTokensBad
287 * is true only if we know that the tokens are bad. We thus clear this flag
288 * when we get a new set of tokens..
289 * Having force... true and UTokensBad true simultaneously means that the tokens
290 * went bad and we're supposed to create a new, unauthenticated, connection.
292 * @param aserver Server to connect to.
293 * @param aport Connection port.
294 * @param acell The cell where all of this happens.
295 * @param areq The request.
296 * @param aforce Force connection?
297 * @param locktype Type of lock to be used.
299 * @return The established connection.
302 afs_ConnByHost(struct server *aserver, unsigned short aport, afs_int32 acell,
303 struct vrequest *areq, int aforce, afs_int32 locktype)
306 struct afs_conn *tc = 0;
307 struct srvAddr *sa = 0;
309 AFS_STATCNT(afs_ConnByHost);
311 if (AFS_IS_DISCONNECTED && !AFS_IN_SYNC) {
312 afs_warnuser("afs_ConnByHost: disconnected\n");
317 1. look for an existing connection
318 2. create a connection at an address believed to be up
319 (if aforce is true, create a connection at the first address)
322 tu = afs_GetUser(areq->uid, acell, SHARED_LOCK);
324 for (sa = aserver->addr; sa; sa = sa->next_sa) {
325 tc = afs_ConnBySA(sa, aport, acell, tu, aforce,
326 0 /*don't create one */ ,
333 for (sa = aserver->addr; sa; sa = sa->next_sa) {
334 tc = afs_ConnBySA(sa, aport, acell, tu, aforce,
342 afs_PutUser(tu, SHARED_LOCK);
345 } /*afs_ConnByHost */
349 * Connect by multiple hosts.
350 * Try to connect to one of the hosts from the ahosts array.
352 * @param ahosts Multiple hosts to connect to.
353 * @param aport Connection port.
354 * @param acell The cell where all of this happens.
355 * @param areq The request.
356 * @param locktype Type of lock to be used.
358 * @return The established connection or NULL.
361 afs_ConnByMHosts(struct server *ahosts[], unsigned short aport,
362 afs_int32 acell, register struct vrequest *areq,
365 register afs_int32 i;
366 register struct afs_conn *tconn;
367 register struct server *ts;
369 /* try to find any connection from the set */
370 AFS_STATCNT(afs_ConnByMHosts);
371 for (i = 0; i < MAXCELLHOSTS; i++) {
372 if ((ts = ahosts[i]) == NULL)
374 tconn = afs_ConnByHost(ts, aport, acell, areq, 0, locktype);
381 } /*afs_ConnByMHosts */
385 * Decrement reference count to this connection.
390 afs_PutConn(register struct afs_conn *ac, afs_int32 locktype)
392 AFS_STATCNT(afs_PutConn);
398 * For multi homed clients, a RPC may timeout because of a
399 * client network interface going down. We need to reopen new
400 * connections in this case.
402 * @param sap Server address.
405 ForceNewConnections(struct srvAddr *sap)
407 struct afs_conn *tc = 0;
410 return; /* defensive check */
412 ObtainWriteLock(&afs_xconn, 413);
413 for (tc = sap->conns; tc; tc = tc->next)
414 tc->forceConnectFS = 1;
415 ReleaseWriteLock(&afs_xconn);