include-afsconfig-before-param-h-20010712
[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)
29 #include <netinet/in_var.h>
30 #endif /* ! AFS_HPUX110_ENV */
31 #endif /* !defined(UKERNEL) */
32
33 #include "../afs/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 /* Imported variables */
46 /* these are for storing alternate interface addresses - from afs_callback.c */
47 extern struct interfaceAddr afs_cb_interface;
48
49
50 /* Imported functions. */
51 struct rx_securityClass *rxnull_NewClientSecurityObject();
52 struct rx_securityClass *rxkad_NewClientSecurityObject();
53
54 /* Exported variables */
55 afs_rwlock_t afs_xconn;                 /* allocation lock for new things */
56 afs_rwlock_t afs_xinterface;            /* for multiple client address */
57
58 /* Local variables */
59 afs_int32 cryptall = 0;
60
61
62 unsigned int VNOSERVERS = 0;
63 struct conn *afs_Conn(afid, areq, locktype)
64     register struct VenusFid *afid;
65     register struct vrequest *areq;
66     afs_int32 locktype;
67 {
68    u_short fsport=AFS_FSPORT;
69    struct volume *tv;
70    struct conn *tconn = (struct conn *)0;
71    struct srvAddr *lowp= (struct srvAddr *)0;
72    struct unixuser *tu;
73    int notbusy;
74    int i;
75    struct srvAddr *sa1p;
76
77    AFS_STATCNT(afs_Conn);
78    tv = afs_GetVolume(afid, areq, READ_LOCK);
79    if (!tv) {
80       if (areq) {
81          afs_FinalizeReq(areq);
82          areq->volumeError = 1;
83       }
84       return (struct conn *) 0;
85    }
86
87    if (tv->serverHost[0] && tv->serverHost[0]->cell) {
88       fsport = tv->serverHost[0]->cell->fsport;
89    }    
90    else {
91       VNOSERVERS++;
92    }
93
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;
98
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
104     * in this time).
105     */
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)
111                   areq->busyCount++;
112             } else if (tv->status[i] == offline) {
113                if (!areq->volumeError) 
114                   areq->volumeError = VOLMISSING;
115             }
116             continue;
117          }
118          for (sa1p = tv->serverHost[i]->addr; sa1p; sa1p = sa1p->next_sa) {
119             if (sa1p->sa_flags & SRVR_ISDOWN)
120                continue;
121             if (!lowp || (lowp->sa_iprank > sa1p->sa_iprank))
122                lowp = sa1p;
123          }
124       }
125    }
126    afs_PutVolume(tv, READ_LOCK);
127
128    if (lowp) {
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);
132
133       afs_PutUser(tu, SHARED_LOCK);
134    }
135
136    return tconn;
137 } /*afs_Conn*/
138
139
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)
143 {
144     struct conn *tc=0;
145     struct rx_securityClass *csec;      /*Security class object*/
146     int isec;                           /*Security index*/
147     int service;
148
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;
152     }
153
154     ObtainSharedLock(&afs_xconn,15);
155     for (tc = sap->conns; tc; tc=tc->next) {
156        if (tc->user == tu && tc->port == aport) {
157           break;
158        }
159     }           
160
161     if (!tc && !create) {
162        ReleaseSharedLock(&afs_xconn);
163        return (struct conn *)0;
164     }
165
166     if (!tc) {
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''.
172         */
173         UpgradeSToWLock(&afs_xconn,37);
174         tc = (struct conn *) afs_osi_Alloc(sizeof(struct conn));
175         bzero((char *)tc, sizeof(struct conn));
176
177         tc->user = tu;
178         tc->port = aport;
179         tc->srvr = sap;
180         tc->refCount = 0;   /* bumped below */
181         tc->forceConnectFS = 1;
182         tc->id = (struct rx_connection *) 0;
183         tc->next = sap->conns;
184         sap->conns = tc;
185         afs_ActivateServer(sap); 
186
187         ConvertWToSLock(&afs_xconn);
188     }
189     tc->refCount++;
190
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).
198          */
199         if (tc->id && (rx_SecurityClassOf(tc->id) != 0)) {
200            tc->forceConnectFS = 1;      /* force recreation of connection */
201         }
202         tu->vid = UNDEFVID;     /* forcibly disconnect the authentication info */
203     }
204
205     if (tc->forceConnectFS) { 
206         UpgradeSToWLock(&afs_xconn,38);
207         csec = (struct rx_securityClass *) 0;
208         if (tc->id) {
209             AFS_GUNLOCK();
210             rx_DestroyConnection(tc->id);
211             AFS_GLOCK();
212         }
213         /*
214          * Stupid hack to determine if using vldb service or file system
215          * service.
216          */
217         if (aport == sap->server->cell->vlport)
218             service = 52;
219         else
220             service = 1;
221         isec = 0;
222         if (tu->vid != UNDEFVID) {
223               int level;
224
225               if (cryptall) {
226                 level=rxkad_crypt;
227               } else {
228                 level=rxkad_clear;
229               }
230               isec=2;
231               /* kerberos tickets on channel 2 */
232               csec = rxkad_NewClientSecurityObject(level, tu->ct.HandShakeKey,
233                                                    /* kvno */ 
234                                                    tu->ct.AuthHandle, 
235                                                    tu->stLen, tu->stp);
236         }
237         if (isec == 0)
238             csec = rxnull_NewClientSecurityObject();
239         AFS_GUNLOCK();
240         tc->id = rx_NewConnection(sap->sa_ip, aport, service, csec, isec);
241         AFS_GLOCK();
242         if (service == 52) { 
243            rx_SetConnHardDeadTime(tc->id, AFS_HARDDEADTIME);
244        }
245
246
247         tc->forceConnectFS = 0; /* apparently we're appropriately connected now */
248         if (csec)
249             rxs_Release(csec);
250         ConvertWToSLock(&afs_xconn);
251     }
252
253     ReleaseSharedLock(&afs_xconn);
254     return tc;
255 }
256 /*
257  * afs_ConnByHost
258  *
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.
264  */
265 struct conn *afs_ConnByHost(aserver, aport, acell, areq, aforce, locktype)
266     struct server *aserver;
267     afs_int32 acell;
268     unsigned short aport;
269     struct vrequest *areq;
270     int aforce;
271     afs_int32 locktype;
272 { /*afs_ConnByHost*/
273
274     struct unixuser *tu;
275     struct conn *tc=0;
276     struct srvAddr *sa=0;
277
278     AFS_STATCNT(afs_ConnByHost);
279 /* 
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)
283 */
284
285     tu = afs_GetUser(areq->uid, acell, SHARED_LOCK);
286
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*/,
290                          locktype);
291        if (tc) 
292             break;
293     }
294
295     if (!tc) {
296        for (sa = aserver->addr; sa; sa = sa->next_sa) { 
297           tc = afs_ConnBySA(sa, aport, acell, tu, aforce, 
298                             1/*create one*/,
299                             locktype);
300           if (tc) 
301                break;
302        }
303     }
304
305     afs_PutUser(tu, SHARED_LOCK);
306     return tc;
307
308 } /*afs_ConnByHost*/
309
310
311 struct conn *afs_ConnByMHosts(ahosts, aport, acell, areq, locktype)
312     struct server *ahosts[];
313     afs_int32 acell;
314     unsigned short aport;
315     register struct vrequest *areq;
316     afs_int32 locktype;
317 {
318     register afs_int32 i;
319     register struct conn *tconn;
320     register struct server *ts;
321
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, 
327                                areq, 0, locktype);
328         if (tconn) {
329            return tconn;
330         }
331     }
332     return (struct conn *) 0;
333
334 } /*afs_ConnByMHosts*/
335
336
337 void afs_PutConn(ac, locktype)
338     register struct conn *ac;
339     afs_int32 locktype;
340 {
341     AFS_STATCNT(afs_PutConn);
342     ac->refCount--;
343 } /*afs_PutConn*/
344
345
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
349 */
350 ForceNewConnections(sap)
351 struct srvAddr *sap;
352 {
353         struct conn *tc=0;
354
355         if ( !sap) return; /* defensive check */
356
357         /* if client is not multihomed, do nothing */
358         ObtainReadLock(&afs_xinterface);
359         if ( afs_cb_interface.numberOfInterfaces <= 1 )
360         {
361                 ReleaseReadLock(&afs_xinterface);
362                 return;
363         }
364         ReleaseReadLock(&afs_xinterface);
365         
366         ObtainWriteLock(&afs_xconn,413);
367         for (tc = sap->conns; tc; tc=tc->next)
368                 tc->forceConnectFS=1;
369         ReleaseWriteLock(&afs_xconn);
370 }