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