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