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