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