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;
56 afs_Conn(register struct VenusFid *afid, register struct vrequest *areq,
59 u_short fsport = AFS_FSPORT;
61 struct conn *tconn = NULL;
62 struct srvAddr *lowp = NULL;
68 AFS_STATCNT(afs_Conn);
69 tv = afs_GetVolume(afid, areq, READ_LOCK);
72 afs_FinalizeReq(areq);
73 areq->volumeError = 1;
78 if (tv->serverHost[0] && tv->serverHost[0]->cell) {
79 fsport = tv->serverHost[0]->cell->fsport;
84 /* First is always lowest rank, if it's up */
85 if ((tv->status[0] == not_busy) && tv->serverHost[0]
86 && !(tv->serverHost[0]->addr->sa_flags & SRVR_ISDOWN) &&
87 !(((areq->idleError > 0) || (areq->tokenError > 0))
88 && (areq->skipserver[0] == 1)))
89 lowp = tv->serverHost[0]->addr;
91 /* Otherwise we look at all of them. There are seven levels of
92 * not_busy. This means we will check a volume seven times before it
93 * is marked offline. Ideally, we only need two levels, but this
94 * serves a second purpose of waiting some number of seconds before
95 * the client decides the volume is offline (ie: a clone could finish
98 for (notbusy = not_busy; (!lowp && (notbusy <= end_not_busy)); notbusy++) {
99 for (i = 0; i < MAXHOSTS && tv->serverHost[i]; i++) {
100 if (((areq->tokenError > 0)||(areq->idleError > 0))
101 && (areq->skipserver[i] == 1))
103 if (tv->status[i] != notbusy) {
104 if (tv->status[i] == rd_busy || tv->status[i] == rdwr_busy) {
105 if (!areq->busyCount)
107 } else if (tv->status[i] == offline) {
108 if (!areq->volumeError)
109 areq->volumeError = VOLMISSING;
113 for (sa1p = tv->serverHost[i]->addr; sa1p; sa1p = sa1p->next_sa) {
114 if (sa1p->sa_flags & SRVR_ISDOWN)
116 if (!lowp || (lowp->sa_iprank > sa1p->sa_iprank))
121 afs_PutVolume(tv, READ_LOCK);
124 tu = afs_GetUser(areq->uid, afid->Cell, SHARED_LOCK);
125 tconn = afs_ConnBySA(lowp, fsport, afid->Cell, tu, 0 /*!force */ ,
126 1 /*create */ , locktype);
128 afs_PutUser(tu, SHARED_LOCK);
136 afs_ConnBySA(struct srvAddr *sap, unsigned short aport, afs_int32 acell,
137 struct unixuser *tu, int force_if_down, afs_int32 create,
141 struct rx_securityClass *csec; /*Security class object */
142 int isec; /*Security index */
145 if (!sap || ((sap->sa_flags & SRVR_ISDOWN) && !force_if_down)) {
146 /* sa is known down, and we don't want to force it. */
150 ObtainSharedLock(&afs_xconn, 15);
151 for (tc = sap->conns; tc; tc = tc->next) {
152 if (tc->user == tu && tc->port == aport) {
157 if (!tc && !create) {
158 ReleaseSharedLock(&afs_xconn);
163 /* No such connection structure exists. Create one and splice it in.
164 * Make sure the server record has been marked as used (for the purposes
165 * of calculating up & down times, it's now considered to be an
166 * ``active'' server). Also make sure the server's lastUpdateEvalTime
167 * gets set, marking the time of its ``birth''.
169 UpgradeSToWLock(&afs_xconn, 37);
170 tc = (struct conn *)afs_osi_Alloc(sizeof(struct conn));
171 memset((char *)tc, 0, sizeof(struct conn));
176 tc->refCount = 0; /* bumped below */
177 tc->forceConnectFS = 1;
178 tc->id = (struct rx_connection *)0;
179 tc->next = sap->conns;
181 afs_ActivateServer(sap);
183 ConvertWToSLock(&afs_xconn);
187 if (tu->states & UTokensBad) {
188 /* we may still have an authenticated RPC connection here,
189 * we'll have to create a new, unauthenticated, connection.
190 * Perhaps a better way to do this would be to set
191 * conn->forceConnectFS on all conns when the token first goes
192 * bad, but that's somewhat trickier, due to locking
193 * constraints (though not impossible).
195 if (tc->id && (rx_SecurityClassOf(tc->id) != 0)) {
196 tc->forceConnectFS = 1; /* force recreation of connection */
198 tu->vid = UNDEFVID; /* forcibly disconnect the authentication info */
201 if (tc->forceConnectFS) {
202 UpgradeSToWLock(&afs_xconn, 38);
203 csec = (struct rx_securityClass *)0;
206 rx_DestroyConnection(tc->id);
210 * Stupid hack to determine if using vldb service or file system
213 if (aport == sap->server->cell->vlport)
218 if (tu->vid != UNDEFVID) {
227 /* kerberos tickets on channel 2 */
228 csec = rxkad_NewClientSecurityObject(level,
229 (struct ktc_encryptionKey *)tu->ct.HandShakeKey,
231 tu->ct.AuthHandle, tu->stLen,
235 csec = rxnull_NewClientSecurityObject();
237 tc->id = rx_NewConnection(sap->sa_ip, aport, service, csec, isec);
240 rx_SetConnHardDeadTime(tc->id, afs_rx_harddead);
242 rx_SetConnIdleDeadTime(tc->id, afs_rx_idledead);
244 tc->forceConnectFS = 0; /* apparently we're appropriately connected now */
247 ConvertWToSLock(&afs_xconn);
250 ReleaseSharedLock(&afs_xconn);
257 * forceConnectFS is set whenever we must recompute the connection. UTokensBad
258 * is true only if we know that the tokens are bad. We thus clear this flag
259 * when we get a new set of tokens..
260 * Having force... true and UTokensBad true simultaneously means that the tokens
261 * went bad and we're supposed to create a new, unauthenticated, connection.
264 afs_ConnByHost(struct server *aserver, unsigned short aport, afs_int32 acell,
265 struct vrequest *areq, int aforce, afs_int32 locktype)
269 struct srvAddr *sa = 0;
271 AFS_STATCNT(afs_ConnByHost);
273 1. look for an existing connection
274 2. create a connection at an address believed to be up
275 (if aforce is true, create a connection at the first address)
278 tu = afs_GetUser(areq->uid, acell, SHARED_LOCK);
280 for (sa = aserver->addr; sa; sa = sa->next_sa) {
281 tc = afs_ConnBySA(sa, aport, acell, tu, aforce,
282 0 /*don't create one */ ,
289 for (sa = aserver->addr; sa; sa = sa->next_sa) {
290 tc = afs_ConnBySA(sa, aport, acell, tu, aforce,
298 afs_PutUser(tu, SHARED_LOCK);
301 } /*afs_ConnByHost */
305 afs_ConnByMHosts(struct server *ahosts[], unsigned short aport,
306 afs_int32 acell, register struct vrequest *areq,
309 register afs_int32 i;
310 register struct conn *tconn;
311 register struct server *ts;
313 /* try to find any connection from the set */
314 AFS_STATCNT(afs_ConnByMHosts);
315 for (i = 0; i < MAXCELLHOSTS; i++) {
316 if ((ts = ahosts[i]) == NULL)
318 tconn = afs_ConnByHost(ts, aport, acell, areq, 0, locktype);
325 } /*afs_ConnByMHosts */
329 afs_PutConn(register struct conn *ac, afs_int32 locktype)
331 AFS_STATCNT(afs_PutConn);
336 /* for multi homed clients, an RPC may timeout because of a
337 client network interface going down. We need to reopen new
338 connections in this case
341 ForceNewConnections(struct srvAddr *sap)
346 return; /* defensive check */
348 ObtainWriteLock(&afs_xconn, 413);
349 for (tc = sap->conns; tc; tc = tc->next)
350 tc->forceConnectFS = 1;
351 ReleaseWriteLock(&afs_xconn);