vserver 2.0 rc7
[linux-2.6.git] / net / sctp / endpointola.c
1 /* SCTP kernel reference Implementation
2  * Copyright (c) 1999-2000 Cisco, Inc.
3  * Copyright (c) 1999-2001 Motorola, Inc.
4  * Copyright (c) 2001-2002 International Business Machines, Corp.
5  * Copyright (c) 2001 Intel Corp.
6  * Copyright (c) 2001 Nokia, Inc.
7  * Copyright (c) 2001 La Monte H.P. Yarroll
8  *
9  * This file is part of the SCTP kernel reference Implementation
10  *
11  * This abstraction represents an SCTP endpoint.
12  *
13  * This file is part of the implementation of the add-IP extension,
14  * based on <draft-ietf-tsvwg-addip-sctp-02.txt> June 29, 2001,
15  * for the SCTP kernel reference Implementation.
16  *
17  * The SCTP reference implementation is free software;
18  * you can redistribute it and/or modify it under the terms of
19  * the GNU General Public License as published by
20  * the Free Software Foundation; either version 2, or (at your option)
21  * any later version.
22  *
23  * The SCTP reference implementation is distributed in the hope that it
24  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
25  *                 ************************
26  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
27  * See the GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with GNU CC; see the file COPYING.  If not, write to
31  * the Free Software Foundation, 59 Temple Place - Suite 330,
32  * Boston, MA 02111-1307, USA.
33  *
34  * Please send any bug reports or fixes you make to the
35  * email address(es):
36  *    lksctp developers <lksctp-developers@lists.sourceforge.net>
37  *
38  * Or submit a bug report through the following website:
39  *    http://www.sf.net/projects/lksctp
40  *
41  * Written or modified by:
42  *    La Monte H.P. Yarroll <piggy@acm.org>
43  *    Karl Knutson <karl@athena.chicago.il.us>
44  *    Jon Grimm <jgrimm@austin.ibm.com>
45  *    Daisy Chang <daisyc@us.ibm.com>
46  *    Dajiang Zhang <dajiang.zhang@nokia.com>
47  *
48  * Any bugs reported given to us we will try to fix... any fixes shared will
49  * be incorporated into the next SCTP release.
50  */
51
52 #include <linux/types.h>
53 #include <linux/sched.h>
54 #include <linux/slab.h>
55 #include <linux/in.h>
56 #include <linux/random.h>       /* get_random_bytes() */
57 #include <linux/crypto.h>
58 #include <net/sock.h>
59 #include <net/ipv6.h>
60 #include <net/sctp/sctp.h>
61 #include <net/sctp/sm.h>
62
63 /* Forward declarations for internal helpers. */
64 static void sctp_endpoint_bh_rcv(struct sctp_endpoint *ep);
65
66 /*
67  * Initialize the base fields of the endpoint structure.
68  */
69 static struct sctp_endpoint *sctp_endpoint_init(struct sctp_endpoint *ep,
70                                                 struct sock *sk, int gfp)
71 {
72         struct sctp_sock *sp = sctp_sk(sk);
73         memset(ep, 0, sizeof(struct sctp_endpoint));
74
75         /* Initialize the base structure. */
76         /* What type of endpoint are we?  */
77         ep->base.type = SCTP_EP_TYPE_SOCKET;
78
79         /* Initialize the basic object fields. */
80         atomic_set(&ep->base.refcnt, 1);
81         ep->base.dead = 0;
82         ep->base.malloced = 1;
83
84         /* Create an input queue.  */
85         sctp_inq_init(&ep->base.inqueue);
86
87         /* Set its top-half handler */
88         sctp_inq_set_th_handler(&ep->base.inqueue,
89                                 (void (*)(void *))sctp_endpoint_bh_rcv, ep);
90
91         /* Initialize the bind addr area */
92         sctp_bind_addr_init(&ep->base.bind_addr, 0);
93         rwlock_init(&ep->base.addr_lock);
94
95         /* Remember who we are attached to.  */
96         ep->base.sk = sk;
97         sock_hold(ep->base.sk);
98
99         /* Create the lists of associations.  */
100         INIT_LIST_HEAD(&ep->asocs);
101
102         /* Set up the base timeout information.  */
103         ep->timeouts[SCTP_EVENT_TIMEOUT_NONE] = 0;
104         ep->timeouts[SCTP_EVENT_TIMEOUT_T1_COOKIE] =
105                 SCTP_DEFAULT_TIMEOUT_T1_COOKIE;
106         ep->timeouts[SCTP_EVENT_TIMEOUT_T1_INIT] =
107                 SCTP_DEFAULT_TIMEOUT_T1_INIT;
108         ep->timeouts[SCTP_EVENT_TIMEOUT_T2_SHUTDOWN] =
109                 msecs_to_jiffies(sp->rtoinfo.srto_initial);
110         ep->timeouts[SCTP_EVENT_TIMEOUT_T3_RTX] = 0;
111         ep->timeouts[SCTP_EVENT_TIMEOUT_T4_RTO] = 0;
112
113         /* sctpimpguide-05 Section 2.12.2
114          * If the 'T5-shutdown-guard' timer is used, it SHOULD be set to the
115          * recommended value of 5 times 'RTO.Max'.
116          */
117         ep->timeouts[SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD]
118                 = 5 * msecs_to_jiffies(sp->rtoinfo.srto_max);
119
120         ep->timeouts[SCTP_EVENT_TIMEOUT_HEARTBEAT] =
121                 SCTP_DEFAULT_TIMEOUT_HEARTBEAT;
122         ep->timeouts[SCTP_EVENT_TIMEOUT_SACK] =
123                 SCTP_DEFAULT_TIMEOUT_SACK;
124         ep->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE] =
125                 sp->autoclose * HZ;
126
127         /* Use SCTP specific send buffer space queues.  */
128         ep->sndbuf_policy = sctp_sndbuf_policy;
129         sk->sk_write_space = sctp_write_space;
130         sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
131
132         /* Initialize the secret key used with cookie. */
133         get_random_bytes(&ep->secret_key[0], SCTP_SECRET_SIZE);
134         ep->last_key = ep->current_key = 0;
135         ep->key_changed_at = jiffies;
136
137         ep->debug_name = "unnamedEndpoint";
138         return ep;
139 }
140
141 /* Create a sctp_endpoint with all that boring stuff initialized.
142  * Returns NULL if there isn't enough memory.
143  */
144 struct sctp_endpoint *sctp_endpoint_new(struct sock *sk, int gfp)
145 {
146         struct sctp_endpoint *ep;
147
148         /* Build a local endpoint. */
149         ep = t_new(struct sctp_endpoint, gfp);
150         if (!ep)
151                 goto fail;
152         if (!sctp_endpoint_init(ep, sk, gfp))
153                 goto fail_init;
154         ep->base.malloced = 1;
155         SCTP_DBG_OBJCNT_INC(ep);
156         return ep;
157
158 fail_init:
159         kfree(ep);
160 fail:
161         return NULL;
162 }
163
164 /* Add an association to an endpoint.  */
165 void sctp_endpoint_add_asoc(struct sctp_endpoint *ep,
166                             struct sctp_association *asoc)
167 {
168         struct sock *sk = ep->base.sk;
169
170         /* Now just add it to our list of asocs */
171         list_add_tail(&asoc->asocs, &ep->asocs);
172
173         /* Increment the backlog value for a TCP-style listening socket. */
174         if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
175                 sk->sk_ack_backlog++;
176 }
177
178 /* Free the endpoint structure.  Delay cleanup until
179  * all users have released their reference count on this structure.
180  */
181 void sctp_endpoint_free(struct sctp_endpoint *ep)
182 {
183         ep->base.dead = 1;
184         sctp_endpoint_put(ep);
185 }
186
187 /* Final destructor for endpoint.  */
188 static void sctp_endpoint_destroy(struct sctp_endpoint *ep)
189 {
190         SCTP_ASSERT(ep->base.dead, "Endpoint is not dead", return);
191
192         ep->base.sk->sk_state = SCTP_SS_CLOSED;
193
194         /* Unlink this endpoint, so we can't find it again! */
195         sctp_unhash_endpoint(ep);
196
197         /* Free up the HMAC transform. */
198         if (sctp_sk(ep->base.sk)->hmac)
199                 sctp_crypto_free_tfm(sctp_sk(ep->base.sk)->hmac);
200
201         /* Cleanup. */
202         sctp_inq_free(&ep->base.inqueue);
203         sctp_bind_addr_free(&ep->base.bind_addr);
204
205         /* Remove and free the port */
206         if (sctp_sk(ep->base.sk)->bind_hash)
207                 sctp_put_port(ep->base.sk);
208
209         /* Give up our hold on the sock. */
210         if (ep->base.sk)
211                 sock_put(ep->base.sk);
212
213         /* Finally, free up our memory. */
214         if (ep->base.malloced) {
215                 kfree(ep);
216                 SCTP_DBG_OBJCNT_DEC(ep);
217         }
218 }
219
220 /* Hold a reference to an endpoint. */
221 void sctp_endpoint_hold(struct sctp_endpoint *ep)
222 {
223         atomic_inc(&ep->base.refcnt);
224 }
225
226 /* Release a reference to an endpoint and clean up if there are
227  * no more references.
228  */
229 void sctp_endpoint_put(struct sctp_endpoint *ep)
230 {
231         if (atomic_dec_and_test(&ep->base.refcnt))
232                 sctp_endpoint_destroy(ep);
233 }
234
235 /* Is this the endpoint we are looking for?  */
236 struct sctp_endpoint *sctp_endpoint_is_match(struct sctp_endpoint *ep,
237                                                const union sctp_addr *laddr)
238 {
239         struct sctp_endpoint *retval;
240
241         sctp_read_lock(&ep->base.addr_lock);
242         if (ep->base.bind_addr.port == laddr->v4.sin_port) {
243                 if (sctp_bind_addr_match(&ep->base.bind_addr, laddr,
244                                          sctp_sk(ep->base.sk))) {
245                         retval = ep;
246                         goto out;
247                 }
248         }
249
250         retval = NULL;
251
252 out:
253         sctp_read_unlock(&ep->base.addr_lock);
254         return retval;
255 }
256
257 /* Find the association that goes with this chunk.
258  * We do a linear search of the associations for this endpoint.
259  * We return the matching transport address too.
260  */
261 static struct sctp_association *__sctp_endpoint_lookup_assoc(
262         const struct sctp_endpoint *ep,
263         const union sctp_addr *paddr,
264         struct sctp_transport **transport)
265 {
266         int rport;
267         struct sctp_association *asoc;
268         struct list_head *pos;
269
270         rport = paddr->v4.sin_port;
271
272         list_for_each(pos, &ep->asocs) {
273                 asoc = list_entry(pos, struct sctp_association, asocs);
274                 if (rport == asoc->peer.port) {
275                         sctp_read_lock(&asoc->base.addr_lock);
276                         *transport = sctp_assoc_lookup_paddr(asoc, paddr);
277                         sctp_read_unlock(&asoc->base.addr_lock);
278
279                         if (*transport)
280                                 return asoc;
281                 }
282         }
283
284         *transport = NULL;
285         return NULL;
286 }
287
288 /* Lookup association on an endpoint based on a peer address.  BH-safe.  */
289 struct sctp_association *sctp_endpoint_lookup_assoc(
290         const struct sctp_endpoint *ep,
291         const union sctp_addr *paddr,
292         struct sctp_transport **transport)
293 {
294         struct sctp_association *asoc;
295
296         sctp_local_bh_disable();
297         asoc = __sctp_endpoint_lookup_assoc(ep, paddr, transport);
298         sctp_local_bh_enable();
299
300         return asoc;
301 }
302
303 /* Look for any peeled off association from the endpoint that matches the
304  * given peer address.
305  */
306 int sctp_endpoint_is_peeled_off(struct sctp_endpoint *ep,
307                                 const union sctp_addr *paddr)
308 {
309         struct list_head *pos;
310         struct sctp_sockaddr_entry *addr;
311         struct sctp_bind_addr *bp;
312
313         sctp_read_lock(&ep->base.addr_lock);
314         bp = &ep->base.bind_addr;
315         list_for_each(pos, &bp->address_list) {
316                 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
317                 if (sctp_has_association(&addr->a, paddr)) {
318                         sctp_read_unlock(&ep->base.addr_lock);
319                         return 1;
320                 }
321         }
322         sctp_read_unlock(&ep->base.addr_lock);
323
324         return 0;
325 }
326
327 /* Do delayed input processing.  This is scheduled by sctp_rcv().
328  * This may be called on BH or task time.
329  */
330 static void sctp_endpoint_bh_rcv(struct sctp_endpoint *ep)
331 {
332         struct sctp_association *asoc;
333         struct sock *sk;
334         struct sctp_transport *transport;
335         struct sctp_chunk *chunk;
336         struct sctp_inq *inqueue;
337         sctp_subtype_t subtype;
338         sctp_state_t state;
339         int error = 0;
340
341         if (ep->base.dead)
342                 return;
343
344         asoc = NULL;
345         inqueue = &ep->base.inqueue;
346         sk = ep->base.sk;
347
348         while (NULL != (chunk = sctp_inq_pop(inqueue))) {
349                 subtype = SCTP_ST_CHUNK(chunk->chunk_hdr->type);
350
351                 /* We might have grown an association since last we
352                  * looked, so try again.
353                  *
354                  * This happens when we've just processed our
355                  * COOKIE-ECHO chunk.
356                  */
357                 if (NULL == chunk->asoc) {
358                         asoc = sctp_endpoint_lookup_assoc(ep,
359                                                           sctp_source(chunk),
360                                                           &transport);
361                         chunk->asoc = asoc;
362                         chunk->transport = transport;
363                 }
364
365                 state = asoc ? asoc->state : SCTP_STATE_CLOSED;
366
367                 /* Remember where the last DATA chunk came from so we
368                  * know where to send the SACK.
369                  */
370                 if (asoc && sctp_chunk_is_data(chunk))
371                         asoc->peer.last_data_from = chunk->transport;
372                 else
373                         SCTP_INC_STATS(SCTP_MIB_INCTRLCHUNKS);
374
375                 if (chunk->transport)
376                         chunk->transport->last_time_heard = jiffies;
377
378                 error = sctp_do_sm(SCTP_EVENT_T_CHUNK, subtype, state,
379                                    ep, asoc, chunk, GFP_ATOMIC);
380
381                 if (error && chunk)
382                         chunk->pdiscard = 1;
383
384                 /* Check to see if the endpoint is freed in response to
385                  * the incoming chunk. If so, get out of the while loop.
386                  */
387                 if (!sctp_sk(sk)->ep)
388                         break;
389         }
390 }