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/stds.h"
19 #include "../afs/sysincludes.h" /* Standard vendor system headers */
23 #include <netinet/in.h>
26 #include "../h/hashing.h"
28 #if !defined(AFS_HPUX110_ENV) && !defined(AFS_LINUX20_ENV)
29 #include <netinet/in_var.h>
30 #endif /* ! AFS_HPUX110_ENV */
31 #endif /* !defined(UKERNEL) */
33 #include "../afs/afsincludes.h" /* Afs-based standard headers */
34 #include "../afs/afs_stats.h" /* afs statistics */
36 #if defined(AFS_SUN56_ENV)
38 #include <inet/common.h>
39 #if defined(AFS_SUN58_ENV)
40 #include <netinet/ip6.h>
45 /* Imported variables */
46 /* these are for storing alternate interface addresses - from afs_callback.c */
47 extern struct interfaceAddr afs_cb_interface;
50 /* Imported functions. */
51 struct rx_securityClass *rxnull_NewClientSecurityObject();
52 struct rx_securityClass *rxkad_NewClientSecurityObject();
54 /* Exported variables */
55 afs_rwlock_t afs_xconn; /* allocation lock for new things */
56 afs_rwlock_t afs_xinterface; /* for multiple client address */
59 afs_int32 cryptall = 0;
62 unsigned int VNOSERVERS = 0;
63 struct conn *afs_Conn(afid, areq, locktype)
64 register struct VenusFid *afid;
65 register struct vrequest *areq;
68 u_short fsport=AFS_FSPORT;
70 struct conn *tconn = (struct conn *)0;
71 struct srvAddr *lowp= (struct srvAddr *)0;
77 AFS_STATCNT(afs_Conn);
78 tv = afs_GetVolume(afid, areq, READ_LOCK);
81 afs_FinalizeReq(areq);
82 areq->volumeError = 1;
84 return (struct conn *) 0;
87 if (tv->serverHost[0] && tv->serverHost[0]->cell) {
88 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 lowp = tv->serverHost[0]->addr;
99 /* Otherwise we look at all of them. There are seven levels of
100 * not_busy. This means we will check a volume seven times before it
101 * is marked offline. Ideally, we only need two levels, but this
102 * serves a second purpose of waiting some number of seconds before
103 * the client decides the volume is offline (ie: a clone could finish
106 for (notbusy = not_busy; (!lowp && (notbusy <= end_not_busy)); notbusy++) {
107 for (i=0 ;i<MAXHOSTS && tv->serverHost[i];i++) {
108 if (tv->status[i] != notbusy) {
109 if (tv->status[i] == rd_busy || tv->status[i] == rdwr_busy) {
110 if (!areq->busyCount)
112 } else if (tv->status[i] == offline) {
113 if (!areq->volumeError)
114 areq->volumeError = VOLMISSING;
118 for (sa1p = tv->serverHost[i]->addr; sa1p; sa1p = sa1p->next_sa) {
119 if (sa1p->sa_flags & SRVR_ISDOWN)
121 if (!lowp || (lowp->sa_iprank > sa1p->sa_iprank))
126 afs_PutVolume(tv, READ_LOCK);
129 tu = afs_GetUser(areq->uid, afid->Cell, SHARED_LOCK);
130 tconn = afs_ConnBySA(lowp, fsport, afid->Cell, tu,
131 0/*!force*/, 1/*create*/, locktype);
133 afs_PutUser(tu, SHARED_LOCK);
140 struct conn *afs_ConnBySA(struct srvAddr *sap, unsigned short aport,
141 afs_int32 acell, struct unixuser *tu,
142 int force_if_down, afs_int32 create, afs_int32 locktype)
145 struct rx_securityClass *csec; /*Security class object*/
146 int isec; /*Security index*/
149 if (!sap || ((sap->sa_flags & SRVR_ISDOWN) && !force_if_down)) {
150 /* sa is known down, and we don't want to force it. */
151 return (struct conn *)0;
154 ObtainSharedLock(&afs_xconn,15);
155 for (tc = sap->conns; tc; tc=tc->next) {
156 if (tc->user == tu && tc->port == aport) {
161 if (!tc && !create) {
162 ReleaseSharedLock(&afs_xconn);
163 return (struct conn *)0;
167 /* No such connection structure exists. Create one and splice it in.
168 * Make sure the server record has been marked as used (for the purposes
169 * of calculating up & down times, it's now considered to be an
170 * ``active'' server). Also make sure the server's lastUpdateEvalTime
171 * gets set, marking the time of its ``birth''.
173 UpgradeSToWLock(&afs_xconn,37);
174 tc = (struct conn *) afs_osi_Alloc(sizeof(struct conn));
175 bzero((char *)tc, sizeof(struct conn));
180 tc->refCount = 0; /* bumped below */
181 tc->forceConnectFS = 1;
182 tc->id = (struct rx_connection *) 0;
183 tc->next = sap->conns;
185 afs_ActivateServer(sap);
187 ConvertWToSLock(&afs_xconn);
191 if (tu->states & UTokensBad) {
192 /* we may still have an authenticated RPC connection here,
193 * we'll have to create a new, unauthenticated, connection.
194 * Perhaps a better way to do this would be to set
195 * conn->forceConnectFS on all conns when the token first goes
196 * bad, but that's somewhat trickier, due to locking
197 * constraints (though not impossible).
199 if (tc->id && (rx_SecurityClassOf(tc->id) != 0)) {
200 tc->forceConnectFS = 1; /* force recreation of connection */
202 tu->vid = UNDEFVID; /* forcibly disconnect the authentication info */
205 if (tc->forceConnectFS) {
206 UpgradeSToWLock(&afs_xconn,38);
207 csec = (struct rx_securityClass *) 0;
210 rx_DestroyConnection(tc->id);
214 * Stupid hack to determine if using vldb service or file system
217 if (aport == sap->server->cell->vlport)
222 if (tu->vid != UNDEFVID) {
231 /* kerberos tickets on channel 2 */
232 csec = rxkad_NewClientSecurityObject(level, tu->ct.HandShakeKey,
238 csec = rxnull_NewClientSecurityObject();
240 tc->id = rx_NewConnection(sap->sa_ip, aport, service, csec, isec);
243 rx_SetConnHardDeadTime(tc->id, AFS_HARDDEADTIME);
247 tc->forceConnectFS = 0; /* apparently we're appropriately connected now */
250 ConvertWToSLock(&afs_xconn);
253 ReleaseSharedLock(&afs_xconn);
259 * forceConnectFS is set whenever we must recompute the connection. UTokensBad
260 * is true only if we know that the tokens are bad. We thus clear this flag
261 * when we get a new set of tokens..
262 * Having force... true and UTokensBad true simultaneously means that the tokens
263 * went bad and we're supposed to create a new, unauthenticated, connection.
265 struct conn *afs_ConnByHost(aserver, aport, acell, areq, aforce, locktype)
266 struct server *aserver;
268 unsigned short aport;
269 struct vrequest *areq;
276 struct srvAddr *sa=0;
278 AFS_STATCNT(afs_ConnByHost);
280 1. look for an existing connection
281 2. create a connection at an address believed to be up
282 (if aforce is true, create a connection at the first address)
285 tu = afs_GetUser(areq->uid, acell, SHARED_LOCK);
287 for (sa = aserver->addr; sa; sa = sa->next_sa) {
288 tc = afs_ConnBySA(sa, aport, acell, tu, aforce,
289 0/*don't create one*/,
296 for (sa = aserver->addr; sa; sa = sa->next_sa) {
297 tc = afs_ConnBySA(sa, aport, acell, tu, aforce,
305 afs_PutUser(tu, SHARED_LOCK);
311 struct conn *afs_ConnByMHosts(ahosts, aport, acell, areq, locktype)
312 struct server *ahosts[];
314 unsigned short aport;
315 register struct vrequest *areq;
318 register afs_int32 i;
319 register struct conn *tconn;
320 register struct server *ts;
322 /* try to find any connection from the set */
323 AFS_STATCNT(afs_ConnByMHosts);
324 for (i=0;i<MAXCELLHOSTS;i++) {
325 if ((ts = ahosts[i]) == (struct server *) 0) break;
326 tconn = afs_ConnByHost(ts, aport, acell,
332 return (struct conn *) 0;
334 } /*afs_ConnByMHosts*/
337 void afs_PutConn(ac, locktype)
338 register struct conn *ac;
341 AFS_STATCNT(afs_PutConn);
346 /* for multi homed clients, an RPC may timeout because of a
347 client network interface going down. We need to reopen new
348 connections in this case
350 ForceNewConnections(sap)
355 if ( !sap) return; /* defensive check */
357 /* if client is not multihomed, do nothing */
358 ObtainReadLock(&afs_xinterface);
359 if ( afs_cb_interface.numberOfInterfaces <= 1 )
361 ReleaseReadLock(&afs_xinterface);
364 ReleaseReadLock(&afs_xinterface);
366 ObtainWriteLock(&afs_xconn,413);
367 for (tc = sap->conns; tc; tc=tc->next)
368 tc->forceConnectFS=1;
369 ReleaseWriteLock(&afs_xconn);