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 "../afs/param.h" /* Should be always first */
14 #include "../afs/stds.h"
15 #include "../afs/sysincludes.h" /* Standard vendor system headers */
19 #include <netinet/in.h>
22 #include "../h/hashing.h"
24 #if !defined(AFS_HPUX110_ENV) && !defined(AFS_LINUX20_ENV)
25 #include <netinet/in_var.h>
26 #endif /* ! AFS_HPUX110_ENV */
27 #endif /* !defined(UKERNEL) */
29 #include "../afs/afsincludes.h" /* Afs-based standard headers */
30 #include "../afs/afs_stats.h" /* afs statistics */
32 #if defined(AFS_SUN56_ENV)
34 #include <inet/common.h>
35 #if defined(AFS_SUN58_ENV)
36 #include <netinet/ip6.h>
41 /* Imported variables */
42 /* these are for storing alternate interface addresses - from afs_callback.c */
43 extern struct interfaceAddr afs_cb_interface;
46 /* Imported functions. */
47 struct rx_securityClass *rxnull_NewClientSecurityObject();
48 struct rx_securityClass *rxkad_NewClientSecurityObject();
50 /* Exported variables */
51 afs_rwlock_t afs_xconn; /* allocation lock for new things */
52 afs_rwlock_t afs_xinterface; /* for multiple client address */
55 afs_int32 cryptall = 0;
58 unsigned int VNOSERVERS = 0;
59 struct conn *afs_Conn(afid, areq, locktype)
60 register struct VenusFid *afid;
61 register struct vrequest *areq;
64 u_short fsport=AFS_FSPORT;
66 struct conn *tconn = (struct conn *)0;
67 struct srvAddr *lowp= (struct srvAddr *)0;
73 AFS_STATCNT(afs_Conn);
74 tv = afs_GetVolume(afid, areq, READ_LOCK);
77 afs_FinalizeReq(areq);
78 areq->volumeError = 1;
80 return (struct conn *) 0;
83 if (tv->serverHost[0] && tv->serverHost[0]->cell) {
84 fsport = tv->serverHost[0]->cell->fsport;
90 /* First is always lowest rank, if it's up */
91 if ((tv->status[0] == not_busy) && tv->serverHost[0] &&
92 !(tv->serverHost[0]->addr->sa_flags & SRVR_ISDOWN))
93 lowp = tv->serverHost[0]->addr;
95 /* Otherwise we look at all of them. There are seven levels of
96 * not_busy. This means we will check a volume seven times before it
97 * is marked offline. Ideally, we only need two levels, but this
98 * serves a second purpose of waiting some number of seconds before
99 * the client decides the volume is offline (ie: a clone could finish
102 for (notbusy = not_busy; (!lowp && (notbusy <= end_not_busy)); notbusy++) {
103 for (i=0 ;i<MAXHOSTS && tv->serverHost[i];i++) {
104 if (tv->status[i] != notbusy) {
105 if (tv->status[i] == rd_busy || tv->status[i] == rdwr_busy) {
106 if (!areq->busyCount)
108 } else if (tv->status[i] == offline) {
109 if (!areq->volumeError)
110 areq->volumeError = VOLMISSING;
114 for (sa1p = tv->serverHost[i]->addr; sa1p; sa1p = sa1p->next_sa) {
115 if (sa1p->sa_flags & SRVR_ISDOWN)
117 if (!lowp || (lowp->sa_iprank > sa1p->sa_iprank))
122 afs_PutVolume(tv, READ_LOCK);
125 tu = afs_GetUser(areq->uid, afid->Cell, SHARED_LOCK);
126 tconn = afs_ConnBySA(lowp, fsport, afid->Cell, tu,
127 0/*!force*/, 1/*create*/, locktype);
129 afs_PutUser(tu, SHARED_LOCK);
136 struct conn *afs_ConnBySA(struct srvAddr *sap, unsigned short aport,
137 afs_int32 acell, struct unixuser *tu,
138 int force_if_down, afs_int32 create, afs_int32 locktype)
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. */
147 return (struct conn *)0;
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);
159 return (struct conn *)0;
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 bzero((char *)tc, 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, tu->ct.HandShakeKey,
234 csec = rxnull_NewClientSecurityObject();
236 tc->id = rx_NewConnection(sap->sa_ip, aport, service, csec, isec);
239 rx_SetConnHardDeadTime(tc->id, AFS_HARDDEADTIME);
243 tc->forceConnectFS = 0; /* apparently we're appropriately connected now */
246 ConvertWToSLock(&afs_xconn);
249 ReleaseSharedLock(&afs_xconn);
255 * forceConnectFS is set whenever we must recompute the connection. UTokensBad
256 * is true only if we know that the tokens are bad. We thus clear this flag
257 * when we get a new set of tokens..
258 * Having force... true and UTokensBad true simultaneously means that the tokens
259 * went bad and we're supposed to create a new, unauthenticated, connection.
261 struct conn *afs_ConnByHost(aserver, aport, acell, areq, aforce, locktype)
262 struct server *aserver;
264 unsigned short aport;
265 struct vrequest *areq;
272 struct srvAddr *sa=0;
274 AFS_STATCNT(afs_ConnByHost);
276 1. look for an existing connection
277 2. create a connection at an address believed to be up
278 (if aforce is true, create a connection at the first address)
281 tu = afs_GetUser(areq->uid, acell, SHARED_LOCK);
283 for (sa = aserver->addr; sa; sa = sa->next_sa) {
284 tc = afs_ConnBySA(sa, aport, acell, tu, aforce,
285 0/*don't create one*/,
292 for (sa = aserver->addr; sa; sa = sa->next_sa) {
293 tc = afs_ConnBySA(sa, aport, acell, tu, aforce,
301 afs_PutUser(tu, SHARED_LOCK);
307 struct conn *afs_ConnByMHosts(ahosts, aport, acell, areq, locktype)
308 struct server *ahosts[];
310 unsigned short aport;
311 register struct vrequest *areq;
314 register afs_int32 i;
315 register struct conn *tconn;
316 register struct server *ts;
318 /* try to find any connection from the set */
319 AFS_STATCNT(afs_ConnByMHosts);
320 for (i=0;i<MAXCELLHOSTS;i++) {
321 if ((ts = ahosts[i]) == (struct server *) 0) break;
322 tconn = afs_ConnByHost(ts, aport, acell,
328 return (struct conn *) 0;
330 } /*afs_ConnByMHosts*/
333 void afs_PutConn(ac, locktype)
334 register struct conn *ac;
337 AFS_STATCNT(afs_PutConn);
342 /* for multi homed clients, an RPC may timeout because of a
343 client network interface going down. We need to reopen new
344 connections in this case
346 ForceNewConnections(sap)
351 if ( !sap) return; /* defensive check */
353 /* if client is not multihomed, do nothing */
354 ObtainReadLock(&afs_xinterface);
355 if ( afs_cb_interface.numberOfInterfaces <= 1 )
357 ReleaseReadLock(&afs_xinterface);
360 ReleaseReadLock(&afs_xinterface);
362 ObtainWriteLock(&afs_xconn,413);
363 for (tc = sap->conns; tc; tc=tc->next)
364 tc->forceConnectFS=1;
365 ReleaseWriteLock(&afs_xconn);