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