darwin port was unhappy because of this
[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 "../afs/param.h"       /* Should be always first */
14 #include "../afs/stds.h"
15 #include "../afs/sysincludes.h" /* Standard vendor system headers */
16
17 #if !defined(UKERNEL)
18 #include <net/if.h>
19 #include <netinet/in.h>
20
21 #ifdef AFS_SGI62_ENV
22 #include "../h/hashing.h"
23 #endif
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) */
28
29 #include "../afs/afsincludes.h" /* Afs-based standard headers */
30 #include "../afs/afs_stats.h"   /* afs statistics */
31
32 #if     defined(AFS_SUN56_ENV)
33 #include <inet/led.h>
34 #include <inet/common.h>
35 #if     defined(AFS_SUN58_ENV)
36 #include <netinet/ip6.h>
37 #endif
38 #include <inet/ip.h>
39 #endif
40
41 /* Imported variables */
42 /* these are for storing alternate interface addresses - from afs_callback.c */
43 extern struct interfaceAddr afs_cb_interface;
44
45
46 /* Imported functions. */
47 struct rx_securityClass *rxnull_NewClientSecurityObject();
48 struct rx_securityClass *rxkad_NewClientSecurityObject();
49
50 /* Exported variables */
51 afs_rwlock_t afs_xconn;                 /* allocation lock for new things */
52 afs_rwlock_t afs_xinterface;            /* for multiple client address */
53
54 /* Local variables */
55 afs_int32 cryptall = 0;
56
57
58 unsigned int VNOSERVERS = 0;
59 struct conn *afs_Conn(afid, areq, locktype)
60     register struct VenusFid *afid;
61     register struct vrequest *areq;
62     afs_int32 locktype;
63 {
64    u_short fsport=AFS_FSPORT;
65    struct volume *tv;
66    struct conn *tconn = (struct conn *)0;
67    struct srvAddr *lowp= (struct srvAddr *)0;
68    struct unixuser *tu;
69    int notbusy;
70    int i;
71    struct srvAddr *sa1p;
72
73    AFS_STATCNT(afs_Conn);
74    tv = afs_GetVolume(afid, areq, READ_LOCK);
75    if (!tv) {
76       if (areq) {
77          afs_FinalizeReq(areq);
78          areq->volumeError = 1;
79       }
80       return (struct conn *) 0;
81    }
82
83    if (tv->serverHost[0] && tv->serverHost[0]->cell) {
84       fsport = tv->serverHost[0]->cell->fsport;
85    }    
86    else {
87       VNOSERVERS++;
88    }
89
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;
94
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
100     * in this time).
101     */
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)
107                   areq->busyCount++;
108             } else if (tv->status[i] == offline) {
109                if (!areq->volumeError) 
110                   areq->volumeError = VOLMISSING;
111             }
112             continue;
113          }
114          for (sa1p = tv->serverHost[i]->addr; sa1p; sa1p = sa1p->next_sa) {
115             if (sa1p->sa_flags & SRVR_ISDOWN)
116                continue;
117             if (!lowp || (lowp->sa_iprank > sa1p->sa_iprank))
118                lowp = sa1p;
119          }
120       }
121    }
122    afs_PutVolume(tv, READ_LOCK);
123
124    if (lowp) {
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);
128
129       afs_PutUser(tu, SHARED_LOCK);
130    }
131
132    return tconn;
133 } /*afs_Conn*/
134
135
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)
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 (struct conn *)0;
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 (struct conn *)0;
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         bzero((char *)tc, 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, tu->ct.HandShakeKey,
229                                                    /* kvno */ 
230                                                    tu->ct.AuthHandle, 
231                                                    tu->stLen, tu->stp);
232         }
233         if (isec == 0)
234             csec = rxnull_NewClientSecurityObject();
235         AFS_GUNLOCK();
236         tc->id = rx_NewConnection(sap->sa_ip, aport, service, csec, isec);
237         AFS_GLOCK();
238         if (service == 52) { 
239            rx_SetConnHardDeadTime(tc->id, AFS_HARDDEADTIME);
240        }
241
242
243         tc->forceConnectFS = 0; /* apparently we're appropriately connected now */
244         if (csec)
245             rxs_Release(csec);
246         ConvertWToSLock(&afs_xconn);
247     }
248
249     ReleaseSharedLock(&afs_xconn);
250     return tc;
251 }
252 /*
253  * afs_ConnByHost
254  *
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.
260  */
261 struct conn *afs_ConnByHost(aserver, aport, acell, areq, aforce, locktype)
262     struct server *aserver;
263     afs_int32 acell;
264     unsigned short aport;
265     struct vrequest *areq;
266     int aforce;
267     afs_int32 locktype;
268 { /*afs_ConnByHost*/
269
270     struct unixuser *tu;
271     struct conn *tc=0;
272     struct srvAddr *sa=0;
273
274     AFS_STATCNT(afs_ConnByHost);
275 /* 
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)
279 */
280
281     tu = afs_GetUser(areq->uid, acell, SHARED_LOCK);
282
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*/,
286                          locktype);
287        if (tc) 
288             break;
289     }
290
291     if (!tc) {
292        for (sa = aserver->addr; sa; sa = sa->next_sa) { 
293           tc = afs_ConnBySA(sa, aport, acell, tu, aforce, 
294                             1/*create one*/,
295                             locktype);
296           if (tc) 
297                break;
298        }
299     }
300
301     afs_PutUser(tu, SHARED_LOCK);
302     return tc;
303
304 } /*afs_ConnByHost*/
305
306
307 struct conn *afs_ConnByMHosts(ahosts, aport, acell, areq, locktype)
308     struct server *ahosts[];
309     afs_int32 acell;
310     unsigned short aport;
311     register struct vrequest *areq;
312     afs_int32 locktype;
313 {
314     register afs_int32 i;
315     register struct conn *tconn;
316     register struct server *ts;
317
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, 
323                                areq, 0, locktype);
324         if (tconn) {
325            return tconn;
326         }
327     }
328     return (struct conn *) 0;
329
330 } /*afs_ConnByMHosts*/
331
332
333 void afs_PutConn(ac, locktype)
334     register struct conn *ac;
335     afs_int32 locktype;
336 {
337     AFS_STATCNT(afs_PutConn);
338     ac->refCount--;
339 } /*afs_PutConn*/
340
341
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
345 */
346 ForceNewConnections(sap)
347 struct srvAddr *sap;
348 {
349         struct conn *tc=0;
350
351         if ( !sap) return; /* defensive check */
352
353         /* if client is not multihomed, do nothing */
354         ObtainReadLock(&afs_xinterface);
355         if ( afs_cb_interface.numberOfInterfaces <= 1 )
356         {
357                 ReleaseReadLock(&afs_xinterface);
358                 return;
359         }
360         ReleaseReadLock(&afs_xinterface);
361         
362         ObtainWriteLock(&afs_xconn,413);
363         for (tc = sap->conns; tc; tc=tc->next)
364                 tc->forceConnectFS=1;
365         ReleaseWriteLock(&afs_xconn);
366 }