openbsd-20021030
[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("$Header$");
17
18 #include "afs/stds.h"
19 #include "afs/sysincludes.h"    /* Standard vendor system headers */
20
21 #if !defined(UKERNEL)
22 #include <net/if.h>
23 #include <netinet/in.h>
24
25 #ifdef AFS_SGI62_ENV
26 #include "h/hashing.h"
27 #endif
28 #if !defined(AFS_HPUX110_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_DARWIN60_ENV)
29 #include <netinet/in_var.h>
30 #endif /* ! AFS_HPUX110_ENV */
31 #endif /* !defined(UKERNEL) */
32
33 #include "afsincludes.h"        /* Afs-based standard headers */
34 #include "afs/afs_stats.h"   /* afs statistics */
35
36 #if     defined(AFS_SUN56_ENV)
37 #include <inet/led.h>
38 #include <inet/common.h>
39 #if     defined(AFS_SUN58_ENV)
40 #include <netinet/ip6.h>
41 #endif
42 #include <inet/ip.h>
43 #endif
44
45 /* Exported variables */
46 afs_rwlock_t afs_xconn;                 /* allocation lock for new things */
47 afs_rwlock_t afs_xinterface;            /* for multiple client address */
48 afs_int32 cryptall = 0;                 /* encrypt all communications */
49
50
51 unsigned int VNOSERVERS = 0;
52 struct conn *afs_Conn(register struct VenusFid *afid, 
53         register struct vrequest *areq, afs_int32 locktype)
54 {
55    u_short fsport=AFS_FSPORT;
56    struct volume *tv;
57    struct conn *tconn = NULL;
58    struct srvAddr *lowp= NULL;
59    struct unixuser *tu;
60    int notbusy;
61    int i;
62    struct srvAddr *sa1p;
63
64    AFS_STATCNT(afs_Conn);
65    tv = afs_GetVolume(afid, areq, READ_LOCK);
66    if (!tv) {
67       if (areq) {
68          afs_FinalizeReq(areq);
69          areq->volumeError = 1;
70       }
71       return NULL;
72    }
73
74    if (tv->serverHost[0] && tv->serverHost[0]->cell) {
75       fsport = tv->serverHost[0]->cell->fsport;
76    }    
77    else {
78       VNOSERVERS++;
79    }
80
81    /* First is always lowest rank, if it's up */
82    if ((tv->status[0] == not_busy) && tv->serverHost[0] && 
83        !(tv->serverHost[0]->addr->sa_flags & SRVR_ISDOWN))
84       lowp = tv->serverHost[0]->addr;
85
86    /* Otherwise we look at all of them. There are seven levels of
87     * not_busy. This means we will check a volume seven times before it
88     * is marked offline. Ideally, we only need two levels, but this
89     * serves a second purpose of waiting some number of seconds before
90     * the client decides the volume is offline (ie: a clone could finish
91     * in this time).
92     */
93    for (notbusy = not_busy; (!lowp && (notbusy <= end_not_busy)); notbusy++) {
94       for (i=0 ;i<MAXHOSTS && tv->serverHost[i];i++) {
95          if (tv->status[i] != notbusy) {
96             if (tv->status[i] == rd_busy || tv->status[i] == rdwr_busy) {
97                if (!areq->busyCount)
98                   areq->busyCount++;
99             } else if (tv->status[i] == offline) {
100                if (!areq->volumeError) 
101                   areq->volumeError = VOLMISSING;
102             }
103             continue;
104          }
105          for (sa1p = tv->serverHost[i]->addr; sa1p; sa1p = sa1p->next_sa) {
106             if (sa1p->sa_flags & SRVR_ISDOWN)
107                continue;
108             if (!lowp || (lowp->sa_iprank > sa1p->sa_iprank))
109                lowp = sa1p;
110          }
111       }
112    }
113    afs_PutVolume(tv, READ_LOCK);
114
115    if (lowp) {
116       tu = afs_GetUser(areq->uid, afid->Cell, SHARED_LOCK);
117       tconn = afs_ConnBySA(lowp, fsport, afid->Cell, tu,
118                            0/*!force*/, 1/*create*/, locktype);
119
120       afs_PutUser(tu, SHARED_LOCK);
121    }
122
123    return tconn;
124 } /*afs_Conn*/
125
126
127 struct conn *afs_ConnBySA(struct srvAddr *sap, unsigned short aport,
128                           afs_int32 acell, struct unixuser *tu,
129                           int force_if_down, afs_int32 create, afs_int32 locktype)
130 {
131     struct conn *tc=0;
132     struct rx_securityClass *csec;      /*Security class object*/
133     int isec;                           /*Security index*/
134     int service;
135
136     if (!sap || ((sap->sa_flags & SRVR_ISDOWN) && !force_if_down)) {
137         /* sa is known down, and we don't want to force it.  */
138         return NULL;
139     }
140
141     ObtainSharedLock(&afs_xconn,15);
142     for (tc = sap->conns; tc; tc=tc->next) {
143        if (tc->user == tu && tc->port == aport) {
144           break;
145        }
146     }           
147
148     if (!tc && !create) {
149        ReleaseSharedLock(&afs_xconn);
150        return NULL;
151     }
152
153     if (!tc) {
154        /* No such connection structure exists.  Create one and splice it in.
155         * Make sure the server record has been marked as used (for the purposes
156         * of calculating up & down times, it's now considered to be an
157         * ``active'' server).  Also make sure the server's lastUpdateEvalTime
158         * gets set, marking the time of its ``birth''.
159         */
160         UpgradeSToWLock(&afs_xconn,37);
161         tc = (struct conn *) afs_osi_Alloc(sizeof(struct conn));
162         memset((char *)tc, 0, sizeof(struct conn));
163
164         tc->user = tu;
165         tc->port = aport;
166         tc->srvr = sap;
167         tc->refCount = 0;   /* bumped below */
168         tc->forceConnectFS = 1;
169         tc->id = (struct rx_connection *) 0;
170         tc->next = sap->conns;
171         sap->conns = tc;
172         afs_ActivateServer(sap); 
173
174         ConvertWToSLock(&afs_xconn);
175     }
176     tc->refCount++;
177
178     if (tu->states & UTokensBad) {
179         /* we may still have an authenticated RPC connection here,
180          * we'll have to create a new, unauthenticated, connection.
181          * Perhaps a better way to do this would be to set
182          * conn->forceConnectFS on all conns when the token first goes
183          * bad, but that's somewhat trickier, due to locking
184          * constraints (though not impossible).
185          */
186         if (tc->id && (rx_SecurityClassOf(tc->id) != 0)) {
187            tc->forceConnectFS = 1;      /* force recreation of connection */
188         }
189         tu->vid = UNDEFVID;     /* forcibly disconnect the authentication info */
190     }
191
192     if (tc->forceConnectFS) { 
193         UpgradeSToWLock(&afs_xconn,38);
194         csec = (struct rx_securityClass *) 0;
195         if (tc->id) {
196             AFS_GUNLOCK();
197             rx_DestroyConnection(tc->id);
198             AFS_GLOCK();
199         }
200         /*
201          * Stupid hack to determine if using vldb service or file system
202          * service.
203          */
204         if (aport == sap->server->cell->vlport)
205             service = 52;
206         else
207             service = 1;
208         isec = 0;
209         if (tu->vid != UNDEFVID) {
210               int level;
211
212               if (cryptall) {
213                 level=rxkad_crypt;
214               } else {
215                 level=rxkad_clear;
216               }
217               isec=2;
218               /* kerberos tickets on channel 2 */
219               csec = rxkad_NewClientSecurityObject(level, tu->ct.HandShakeKey,
220                                                    /* kvno */ 
221                                                    tu->ct.AuthHandle, 
222                                                    tu->stLen, tu->stp);
223         }
224         if (isec == 0)
225             csec = rxnull_NewClientSecurityObject();
226         AFS_GUNLOCK();
227         tc->id = rx_NewConnection(sap->sa_ip, aport, service, csec, isec);
228         AFS_GLOCK();
229         if (service == 52) { 
230            rx_SetConnHardDeadTime(tc->id, AFS_HARDDEADTIME);
231        }
232
233
234         tc->forceConnectFS = 0; /* apparently we're appropriately connected now */
235         if (csec)
236             rxs_Release(csec);
237         ConvertWToSLock(&afs_xconn);
238     }
239
240     ReleaseSharedLock(&afs_xconn);
241     return tc;
242 }
243 /*
244  * afs_ConnByHost
245  *
246  * forceConnectFS is set whenever we must recompute the connection. UTokensBad
247  * is true only if we know that the tokens are bad.  We thus clear this flag
248  * when we get a new set of tokens..
249  * Having force... true and UTokensBad true simultaneously means that the tokens
250  * went bad and we're supposed to create a new, unauthenticated, connection.
251  */
252 struct conn *afs_ConnByHost(struct server *aserver, unsigned short aport, 
253         afs_int32 acell, struct vrequest *areq, int aforce, afs_int32 locktype)
254 {
255     struct unixuser *tu;
256     struct conn *tc=0;
257     struct srvAddr *sa=0;
258
259     AFS_STATCNT(afs_ConnByHost);
260 /* 
261   1.  look for an existing connection
262   2.  create a connection at an address believed to be up
263       (if aforce is true, create a connection at the first address)
264 */
265
266     tu = afs_GetUser(areq->uid, acell, SHARED_LOCK);
267
268     for (sa = aserver->addr; sa; sa = sa->next_sa) {    
269        tc = afs_ConnBySA(sa, aport, acell, tu, aforce, 
270                          0/*don't create one*/,
271                          locktype);
272        if (tc) 
273             break;
274     }
275
276     if (!tc) {
277        for (sa = aserver->addr; sa; sa = sa->next_sa) { 
278           tc = afs_ConnBySA(sa, aport, acell, tu, aforce, 
279                             1/*create one*/,
280                             locktype);
281           if (tc) 
282                break;
283        }
284     }
285
286     afs_PutUser(tu, SHARED_LOCK);
287     return tc;
288
289 } /*afs_ConnByHost*/
290
291
292 struct conn *afs_ConnByMHosts(struct server *ahosts[], unsigned short aport,    
293         afs_int32 acell, register struct vrequest *areq, afs_int32 locktype)
294 {
295     register afs_int32 i;
296     register struct conn *tconn;
297     register struct server *ts;
298
299     /* try to find any connection from the set */
300     AFS_STATCNT(afs_ConnByMHosts);
301     for (i=0;i<MAXCELLHOSTS;i++) {
302         if ((ts = ahosts[i]) == NULL) break;
303         tconn = afs_ConnByHost(ts, aport, acell, 
304                                areq, 0, locktype);
305         if (tconn) {
306            return tconn;
307         }
308     }
309     return NULL;
310
311 } /*afs_ConnByMHosts*/
312
313
314 void afs_PutConn(register struct conn *ac, afs_int32 locktype)
315 {
316     AFS_STATCNT(afs_PutConn);
317     ac->refCount--;
318 } /*afs_PutConn*/
319
320
321 /* for multi homed clients, an RPC may timeout because of a
322 client network interface going down. We need to reopen new 
323 connections in this case
324 */
325 void ForceNewConnections(struct srvAddr *sap)
326 {
327         struct conn *tc=0;
328
329         if ( !sap) return; /* defensive check */
330
331         /* if client is not multihomed, do nothing */
332         ObtainReadLock(&afs_xinterface);
333         if ( afs_cb_interface.numberOfInterfaces <= 1 )
334         {
335                 ReleaseReadLock(&afs_xinterface);
336                 return;
337         }
338         ReleaseReadLock(&afs_xinterface);
339         
340         ObtainWriteLock(&afs_xconn,413);
341         for (tc = sap->conns; tc; tc=tc->next)
342                 tc->forceConnectFS=1;
343         ReleaseWriteLock(&afs_xconn);
344 }