bae3f01354ca347d4b23ab0126df5e8c72684b87
[openafs.git] / src / afs / afs_conn.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 /*
11  * Implements:
12  */
13 #include <afsconfig.h>
14 #include "afs/param.h"
15
16 RCSID
17     ("$Header$");
18
19 #include "afs/stds.h"
20 #include "afs/sysincludes.h"    /* Standard vendor system headers */
21
22 #if !defined(UKERNEL)
23 #if !defined(AFS_LINUX20_ENV)
24 #include <net/if.h>
25 #endif
26 #include <netinet/in.h>
27
28 #ifdef AFS_SGI62_ENV
29 #include "h/hashing.h"
30 #endif
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) */
35
36 #include "afsincludes.h"        /* Afs-based standard headers */
37 #include "afs/afs_stats.h"      /* afs statistics */
38
39 #if     defined(AFS_SUN56_ENV)
40 #include <inet/led.h>
41 #include <inet/common.h>
42 #if     defined(AFS_SUN58_ENV)
43 #include <netinet/ip6.h>
44 #endif
45 #include <inet/ip.h>
46 #endif
47
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 */
52
53
54 unsigned int VNOSERVERS = 0;
55 struct conn *
56 afs_Conn(register struct VenusFid *afid, register struct vrequest *areq,
57          afs_int32 locktype)
58 {
59     u_short fsport = AFS_FSPORT;
60     struct volume *tv;
61     struct conn *tconn = NULL;
62     struct srvAddr *lowp = NULL;
63     struct unixuser *tu;
64     int notbusy;
65     int i;
66     struct srvAddr *sa1p;
67
68     AFS_STATCNT(afs_Conn);
69     tv = afs_GetVolume(afid, areq, READ_LOCK);
70     if (!tv) {
71         if (areq) {
72             afs_FinalizeReq(areq);
73             areq->volumeError = 1;
74         }
75         return NULL;
76     }
77
78     if (tv->serverHost[0] && tv->serverHost[0]->cell) {
79         fsport = tv->serverHost[0]->cell->fsport;
80     } else {
81         VNOSERVERS++;
82     }
83
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;
90
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
96      * in this time).
97      */
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))
102                 continue;
103             if (tv->status[i] != notbusy) {
104                 if (tv->status[i] == rd_busy || tv->status[i] == rdwr_busy) {
105                     if (!areq->busyCount)
106                         areq->busyCount++;
107                 } else if (tv->status[i] == offline) {
108                     if (!areq->volumeError)
109                         areq->volumeError = VOLMISSING;
110                 }
111                 continue;
112             }
113             for (sa1p = tv->serverHost[i]->addr; sa1p; sa1p = sa1p->next_sa) {
114                 if (sa1p->sa_flags & SRVR_ISDOWN)
115                     continue;
116                 if (!lowp || (lowp->sa_iprank > sa1p->sa_iprank))
117                     lowp = sa1p;
118             }
119         }
120     }
121     afs_PutVolume(tv, READ_LOCK);
122
123     if (lowp) {
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);
127
128         afs_PutUser(tu, SHARED_LOCK);
129     }
130
131     return tconn;
132 }                               /*afs_Conn */
133
134
135 struct conn *
136 afs_ConnBySA(struct srvAddr *sap, unsigned short aport, afs_int32 acell,
137              struct unixuser *tu, int force_if_down, afs_int32 create,
138              afs_int32 locktype)
139 {
140     struct conn *tc = 0;
141     struct rx_securityClass *csec;      /*Security class object */
142     int isec;                   /*Security index */
143     int service;
144
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 NULL;
148     }
149
150     ObtainSharedLock(&afs_xconn, 15);
151     for (tc = sap->conns; tc; tc = tc->next) {
152         if (tc->user == tu && tc->port == aport) {
153             break;
154         }
155     }
156
157     if (!tc && !create) {
158         ReleaseSharedLock(&afs_xconn);
159         return NULL;
160     }
161
162     if (!tc) {
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''.
168          */
169         UpgradeSToWLock(&afs_xconn, 37);
170         tc = (struct conn *)afs_osi_Alloc(sizeof(struct conn));
171         memset((char *)tc, 0, sizeof(struct conn));
172
173         tc->user = tu;
174         tc->port = aport;
175         tc->srvr = sap;
176         tc->refCount = 0;       /* bumped below */
177         tc->forceConnectFS = 1;
178         tc->id = (struct rx_connection *)0;
179         tc->next = sap->conns;
180         sap->conns = tc;
181         afs_ActivateServer(sap);
182
183         ConvertWToSLock(&afs_xconn);
184     }
185     tc->refCount++;
186
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).
194          */
195         if (tc->id && (rx_SecurityClassOf(tc->id) != 0)) {
196             tc->forceConnectFS = 1;     /* force recreation of connection */
197         }
198         tu->vid = UNDEFVID;     /* forcibly disconnect the authentication info */
199     }
200
201     if (tc->forceConnectFS) {
202         UpgradeSToWLock(&afs_xconn, 38);
203         csec = (struct rx_securityClass *)0;
204         if (tc->id) {
205             AFS_GUNLOCK();
206             rx_DestroyConnection(tc->id);
207             AFS_GLOCK();
208         }
209         /*
210          * Stupid hack to determine if using vldb service or file system
211          * service.
212          */
213         if (aport == sap->server->cell->vlport)
214             service = 52;
215         else
216             service = 1;
217         isec = 0;
218         if (tu->vid != UNDEFVID) {
219             int level;
220
221             if (cryptall) {
222                 level = rxkad_crypt;
223             } else {
224                 level = rxkad_clear;
225             }
226             isec = 2;
227             /* kerberos tickets on channel 2 */
228             csec = rxkad_NewClientSecurityObject(level,
229                                                  (struct ktc_encryptionKey *)tu->ct.HandShakeKey,
230                                                  /* kvno */
231                                                  tu->ct.AuthHandle, tu->stLen,
232                                                  tu->stp);
233         }
234         if (isec == 0)
235             csec = rxnull_NewClientSecurityObject();
236         AFS_GUNLOCK();
237         tc->id = rx_NewConnection(sap->sa_ip, aport, service, csec, isec);
238         AFS_GLOCK();
239         if (service == 52) {
240             rx_SetConnHardDeadTime(tc->id, afs_rx_harddead);
241         }
242         rx_SetConnIdleDeadTime(tc->id, afs_rx_idledead);
243
244         tc->forceConnectFS = 0; /* apparently we're appropriately connected now */
245         if (csec)
246             rxs_Release(csec);
247         ConvertWToSLock(&afs_xconn);
248     }
249
250     ReleaseSharedLock(&afs_xconn);
251     return tc;
252 }
253
254 /*
255  * afs_ConnByHost
256  *
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.
262  */
263 struct conn *
264 afs_ConnByHost(struct server *aserver, unsigned short aport, afs_int32 acell,
265                struct vrequest *areq, int aforce, afs_int32 locktype)
266 {
267     struct unixuser *tu;
268     struct conn *tc = 0;
269     struct srvAddr *sa = 0;
270
271     AFS_STATCNT(afs_ConnByHost);
272 /* 
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)
276 */
277
278     tu = afs_GetUser(areq->uid, acell, SHARED_LOCK);
279
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 */ ,
283                           locktype);
284         if (tc)
285             break;
286     }
287
288     if (!tc) {
289         for (sa = aserver->addr; sa; sa = sa->next_sa) {
290             tc = afs_ConnBySA(sa, aport, acell, tu, aforce,
291                               1 /*create one */ ,
292                               locktype);
293             if (tc)
294                 break;
295         }
296     }
297
298     afs_PutUser(tu, SHARED_LOCK);
299     return tc;
300
301 }                               /*afs_ConnByHost */
302
303
304 struct conn *
305 afs_ConnByMHosts(struct server *ahosts[], unsigned short aport,
306                  afs_int32 acell, register struct vrequest *areq,
307                  afs_int32 locktype)
308 {
309     register afs_int32 i;
310     register struct conn *tconn;
311     register struct server *ts;
312
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)
317             break;
318         tconn = afs_ConnByHost(ts, aport, acell, areq, 0, locktype);
319         if (tconn) {
320             return tconn;
321         }
322     }
323     return NULL;
324
325 }                               /*afs_ConnByMHosts */
326
327
328 void
329 afs_PutConn(register struct conn *ac, afs_int32 locktype)
330 {
331     AFS_STATCNT(afs_PutConn);
332     ac->refCount--;
333 }                               /*afs_PutConn */
334
335
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
339 */
340 void
341 ForceNewConnections(struct srvAddr *sap)
342 {
343     struct conn *tc = 0;
344
345     if (!sap)
346         return;                 /* defensive check */
347
348     ObtainWriteLock(&afs_xconn, 413);
349     for (tc = sap->conns; tc; tc = tc->next)
350         tc->forceConnectFS = 1;
351     ReleaseWriteLock(&afs_xconn);
352 }