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