[SCTP]: __nocast annotations
[safe/jmp/linux-2.6] / 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,
71                                                 unsigned int __nocast gfp)
72 {
73         struct sctp_sock *sp = sctp_sk(sk);
74         memset(ep, 0, sizeof(struct sctp_endpoint));
75
76         /* Initialize the base structure. */
77         /* What type of endpoint are we?  */
78         ep->base.type = SCTP_EP_TYPE_SOCKET;
79
80         /* Initialize the basic object fields. */
81         atomic_set(&ep->base.refcnt, 1);
82         ep->base.dead = 0;
83         ep->base.malloced = 1;
84
85         /* Create an input queue.  */
86         sctp_inq_init(&ep->base.inqueue);
87
88         /* Set its top-half handler */
89         sctp_inq_set_th_handler(&ep->base.inqueue,
90                                 (void (*)(void *))sctp_endpoint_bh_rcv, ep);
91
92         /* Initialize the bind addr area */
93         sctp_bind_addr_init(&ep->base.bind_addr, 0);
94         rwlock_init(&ep->base.addr_lock);
95
96         /* Remember who we are attached to.  */
97         ep->base.sk = sk;
98         sock_hold(ep->base.sk);
99
100         /* Create the lists of associations.  */
101         INIT_LIST_HEAD(&ep->asocs);
102
103         /* Set up the base timeout information.  */
104         ep->timeouts[SCTP_EVENT_TIMEOUT_NONE] = 0;
105         ep->timeouts[SCTP_EVENT_TIMEOUT_T1_COOKIE] =
106                 msecs_to_jiffies(sp->rtoinfo.srto_initial);
107         ep->timeouts[SCTP_EVENT_TIMEOUT_T1_INIT] =
108                 msecs_to_jiffies(sp->rtoinfo.srto_initial);
109         ep->timeouts[SCTP_EVENT_TIMEOUT_T2_SHUTDOWN] =
110                 msecs_to_jiffies(sp->rtoinfo.srto_initial);
111         ep->timeouts[SCTP_EVENT_TIMEOUT_T3_RTX] = 0;
112         ep->timeouts[SCTP_EVENT_TIMEOUT_T4_RTO] = 0;
113
114         /* sctpimpguide-05 Section 2.12.2
115          * If the 'T5-shutdown-guard' timer is used, it SHOULD be set to the
116          * recommended value of 5 times 'RTO.Max'.
117          */
118         ep->timeouts[SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD]
119                 = 5 * msecs_to_jiffies(sp->rtoinfo.srto_max);
120
121         ep->timeouts[SCTP_EVENT_TIMEOUT_HEARTBEAT] = 0;
122         ep->timeouts[SCTP_EVENT_TIMEOUT_SACK] = sctp_sack_timeout;
123         ep->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE] = sp->autoclose * HZ;
124
125         /* Use SCTP specific send buffer space queues.  */
126         ep->sndbuf_policy = sctp_sndbuf_policy;
127         sk->sk_write_space = sctp_write_space;
128         sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
129
130         /* Initialize the secret key used with cookie. */
131         get_random_bytes(&ep->secret_key[0], SCTP_SECRET_SIZE);
132         ep->last_key = ep->current_key = 0;
133         ep->key_changed_at = jiffies;
134
135         return ep;
136 }
137
138 /* Create a sctp_endpoint with all that boring stuff initialized.
139  * Returns NULL if there isn't enough memory.
140  */
141 struct sctp_endpoint *sctp_endpoint_new(struct sock *sk,
142                                         unsigned int __nocast gfp)
143 {
144         struct sctp_endpoint *ep;
145
146         /* Build a local endpoint. */
147         ep = t_new(struct sctp_endpoint, gfp);
148         if (!ep)
149                 goto fail;
150         if (!sctp_endpoint_init(ep, sk, gfp))
151                 goto fail_init;
152         ep->base.malloced = 1;
153         SCTP_DBG_OBJCNT_INC(ep);
154         return ep;
155
156 fail_init:
157         kfree(ep);
158 fail:
159         return NULL;
160 }
161
162 /* Add an association to an endpoint.  */
163 void sctp_endpoint_add_asoc(struct sctp_endpoint *ep,
164                             struct sctp_association *asoc)
165 {
166         struct sock *sk = ep->base.sk;
167
168         /* Now just add it to our list of asocs */
169         list_add_tail(&asoc->asocs, &ep->asocs);
170
171         /* Increment the backlog value for a TCP-style listening socket. */
172         if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
173                 sk->sk_ack_backlog++;
174 }
175
176 /* Free the endpoint structure.  Delay cleanup until
177  * all users have released their reference count on this structure.
178  */
179 void sctp_endpoint_free(struct sctp_endpoint *ep)
180 {
181         ep->base.dead = 1;
182         sctp_endpoint_put(ep);
183 }
184
185 /* Final destructor for endpoint.  */
186 static void sctp_endpoint_destroy(struct sctp_endpoint *ep)
187 {
188         SCTP_ASSERT(ep->base.dead, "Endpoint is not dead", return);
189
190         ep->base.sk->sk_state = SCTP_SS_CLOSED;
191
192         /* Unlink this endpoint, so we can't find it again! */
193         sctp_unhash_endpoint(ep);
194
195         /* Free up the HMAC transform. */
196         if (sctp_sk(ep->base.sk)->hmac)
197                 sctp_crypto_free_tfm(sctp_sk(ep->base.sk)->hmac);
198
199         /* Cleanup. */
200         sctp_inq_free(&ep->base.inqueue);
201         sctp_bind_addr_free(&ep->base.bind_addr);
202
203         /* Remove and free the port */
204         if (sctp_sk(ep->base.sk)->bind_hash)
205                 sctp_put_port(ep->base.sk);
206
207         /* Give up our hold on the sock. */
208         if (ep->base.sk)
209                 sock_put(ep->base.sk);
210
211         /* Finally, free up our memory. */
212         if (ep->base.malloced) {
213                 kfree(ep);
214                 SCTP_DBG_OBJCNT_DEC(ep);
215         }
216 }
217
218 /* Hold a reference to an endpoint. */
219 void sctp_endpoint_hold(struct sctp_endpoint *ep)
220 {
221         atomic_inc(&ep->base.refcnt);
222 }
223
224 /* Release a reference to an endpoint and clean up if there are
225  * no more references.
226  */
227 void sctp_endpoint_put(struct sctp_endpoint *ep)
228 {
229         if (atomic_dec_and_test(&ep->base.refcnt))
230                 sctp_endpoint_destroy(ep);
231 }
232
233 /* Is this the endpoint we are looking for?  */
234 struct sctp_endpoint *sctp_endpoint_is_match(struct sctp_endpoint *ep,
235                                                const union sctp_addr *laddr)
236 {
237         struct sctp_endpoint *retval;
238
239         sctp_read_lock(&ep->base.addr_lock);
240         if (ep->base.bind_addr.port == laddr->v4.sin_port) {
241                 if (sctp_bind_addr_match(&ep->base.bind_addr, laddr,
242                                          sctp_sk(ep->base.sk))) {
243                         retval = ep;
244                         goto out;
245                 }
246         }
247
248         retval = NULL;
249
250 out:
251         sctp_read_unlock(&ep->base.addr_lock);
252         return retval;
253 }
254
255 /* Find the association that goes with this chunk.
256  * We do a linear search of the associations for this endpoint.
257  * We return the matching transport address too.
258  */
259 static struct sctp_association *__sctp_endpoint_lookup_assoc(
260         const struct sctp_endpoint *ep,
261         const union sctp_addr *paddr,
262         struct sctp_transport **transport)
263 {
264         int rport;
265         struct sctp_association *asoc;
266         struct list_head *pos;
267
268         rport = paddr->v4.sin_port;
269
270         list_for_each(pos, &ep->asocs) {
271                 asoc = list_entry(pos, struct sctp_association, asocs);
272                 if (rport == asoc->peer.port) {
273                         sctp_read_lock(&asoc->base.addr_lock);
274                         *transport = sctp_assoc_lookup_paddr(asoc, paddr);
275                         sctp_read_unlock(&asoc->base.addr_lock);
276
277                         if (*transport)
278                                 return asoc;
279                 }
280         }
281
282         *transport = NULL;
283         return NULL;
284 }
285
286 /* Lookup association on an endpoint based on a peer address.  BH-safe.  */
287 struct sctp_association *sctp_endpoint_lookup_assoc(
288         const struct sctp_endpoint *ep,
289         const union sctp_addr *paddr,
290         struct sctp_transport **transport)
291 {
292         struct sctp_association *asoc;
293
294         sctp_local_bh_disable();
295         asoc = __sctp_endpoint_lookup_assoc(ep, paddr, transport);
296         sctp_local_bh_enable();
297
298         return asoc;
299 }
300
301 /* Look for any peeled off association from the endpoint that matches the
302  * given peer address.
303  */
304 int sctp_endpoint_is_peeled_off(struct sctp_endpoint *ep,
305                                 const union sctp_addr *paddr)
306 {
307         struct list_head *pos;
308         struct sctp_sockaddr_entry *addr;
309         struct sctp_bind_addr *bp;
310
311         sctp_read_lock(&ep->base.addr_lock);
312         bp = &ep->base.bind_addr;
313         list_for_each(pos, &bp->address_list) {
314                 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
315                 if (sctp_has_association(&addr->a, paddr)) {
316                         sctp_read_unlock(&ep->base.addr_lock);
317                         return 1;
318                 }
319         }
320         sctp_read_unlock(&ep->base.addr_lock);
321
322         return 0;
323 }
324
325 /* Do delayed input processing.  This is scheduled by sctp_rcv().
326  * This may be called on BH or task time.
327  */
328 static void sctp_endpoint_bh_rcv(struct sctp_endpoint *ep)
329 {
330         struct sctp_association *asoc;
331         struct sock *sk;
332         struct sctp_transport *transport;
333         struct sctp_chunk *chunk;
334         struct sctp_inq *inqueue;
335         sctp_subtype_t subtype;
336         sctp_state_t state;
337         int error = 0;
338
339         if (ep->base.dead)
340                 return;
341
342         asoc = NULL;
343         inqueue = &ep->base.inqueue;
344         sk = ep->base.sk;
345
346         while (NULL != (chunk = sctp_inq_pop(inqueue))) {
347                 subtype = SCTP_ST_CHUNK(chunk->chunk_hdr->type);
348
349                 /* We might have grown an association since last we
350                  * looked, so try again.
351                  *
352                  * This happens when we've just processed our
353                  * COOKIE-ECHO chunk.
354                  */
355                 if (NULL == chunk->asoc) {
356                         asoc = sctp_endpoint_lookup_assoc(ep,
357                                                           sctp_source(chunk),
358                                                           &transport);
359                         chunk->asoc = asoc;
360                         chunk->transport = transport;
361                 }
362
363                 state = asoc ? asoc->state : SCTP_STATE_CLOSED;
364
365                 /* Remember where the last DATA chunk came from so we
366                  * know where to send the SACK.
367                  */
368                 if (asoc && sctp_chunk_is_data(chunk))
369                         asoc->peer.last_data_from = chunk->transport;
370                 else
371                         SCTP_INC_STATS(SCTP_MIB_INCTRLCHUNKS);
372
373                 if (chunk->transport)
374                         chunk->transport->last_time_heard = jiffies;
375
376                 error = sctp_do_sm(SCTP_EVENT_T_CHUNK, subtype, state,
377                                    ep, asoc, chunk, GFP_ATOMIC);
378
379                 if (error && chunk)
380                         chunk->pdiscard = 1;
381
382                 /* Check to see if the endpoint is freed in response to
383                  * the incoming chunk. If so, get out of the while loop.
384                  */
385                 if (!sctp_sk(sk)->ep)
386                         break;
387         }
388 }