SCTP: Use hashed lookup when looking for an association.
[pandora-kernel.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/slab.h>
54 #include <linux/in.h>
55 #include <linux/random.h>       /* get_random_bytes() */
56 #include <linux/crypto.h>
57 #include <net/sock.h>
58 #include <net/ipv6.h>
59 #include <net/sctp/sctp.h>
60 #include <net/sctp/sm.h>
61
62 /* Forward declarations for internal helpers. */
63 static void sctp_endpoint_bh_rcv(struct work_struct *work);
64
65 /*
66  * Initialize the base fields of the endpoint structure.
67  */
68 static struct sctp_endpoint *sctp_endpoint_init(struct sctp_endpoint *ep,
69                                                 struct sock *sk,
70                                                 gfp_t gfp)
71 {
72         struct sctp_hmac_algo_param *auth_hmacs = NULL;
73         struct sctp_chunks_param *auth_chunks = NULL;
74         struct sctp_shared_key *null_key;
75         int err;
76
77         memset(ep, 0, sizeof(struct sctp_endpoint));
78
79         ep->digest = kzalloc(SCTP_SIGNATURE_SIZE, gfp);
80         if (!ep->digest)
81                 return NULL;
82
83         if (sctp_auth_enable) {
84                 /* Allocate space for HMACS and CHUNKS authentication
85                  * variables.  There are arrays that we encode directly
86                  * into parameters to make the rest of the operations easier.
87                  */
88                 auth_hmacs = kzalloc(sizeof(sctp_hmac_algo_param_t) +
89                                 sizeof(__u16) * SCTP_AUTH_NUM_HMACS, gfp);
90                 if (!auth_hmacs)
91                         goto nomem;
92
93                 auth_chunks = kzalloc(sizeof(sctp_chunks_param_t) +
94                                         SCTP_NUM_CHUNK_TYPES, gfp);
95                 if (!auth_chunks)
96                         goto nomem;
97
98                 /* Initialize the HMACS parameter.
99                  * SCTP-AUTH: Section 3.3
100                  *    Every endpoint supporting SCTP chunk authentication MUST
101                  *    support the HMAC based on the SHA-1 algorithm.
102                  */
103                 auth_hmacs->param_hdr.type = SCTP_PARAM_HMAC_ALGO;
104                 auth_hmacs->param_hdr.length =
105                                         htons(sizeof(sctp_paramhdr_t) + 2);
106                 auth_hmacs->hmac_ids[0] = htons(SCTP_AUTH_HMAC_ID_SHA1);
107
108                 /* Initialize the CHUNKS parameter */
109                 auth_chunks->param_hdr.type = SCTP_PARAM_CHUNKS;
110
111                 /* If the Add-IP functionality is enabled, we must
112                  * authenticate, ASCONF and ASCONF-ACK chunks
113                  */
114                 if (sctp_addip_enable) {
115                         auth_chunks->chunks[0] = SCTP_CID_ASCONF;
116                         auth_chunks->chunks[1] = SCTP_CID_ASCONF_ACK;
117                         auth_chunks->param_hdr.length =
118                                         htons(sizeof(sctp_paramhdr_t) + 2);
119                 }
120         }
121
122         /* Initialize the base structure. */
123         /* What type of endpoint are we?  */
124         ep->base.type = SCTP_EP_TYPE_SOCKET;
125
126         /* Initialize the basic object fields. */
127         atomic_set(&ep->base.refcnt, 1);
128         ep->base.dead = 0;
129         ep->base.malloced = 1;
130
131         /* Create an input queue.  */
132         sctp_inq_init(&ep->base.inqueue);
133
134         /* Set its top-half handler */
135         sctp_inq_set_th_handler(&ep->base.inqueue, sctp_endpoint_bh_rcv);
136
137         /* Initialize the bind addr area */
138         sctp_bind_addr_init(&ep->base.bind_addr, 0);
139
140         /* Remember who we are attached to.  */
141         ep->base.sk = sk;
142         sock_hold(ep->base.sk);
143
144         /* Create the lists of associations.  */
145         INIT_LIST_HEAD(&ep->asocs);
146
147         /* Use SCTP specific send buffer space queues.  */
148         ep->sndbuf_policy = sctp_sndbuf_policy;
149
150         sk->sk_write_space = sctp_write_space;
151         sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
152
153         /* Get the receive buffer policy for this endpoint */
154         ep->rcvbuf_policy = sctp_rcvbuf_policy;
155
156         /* Initialize the secret key used with cookie. */
157         get_random_bytes(&ep->secret_key[0], SCTP_SECRET_SIZE);
158         ep->last_key = ep->current_key = 0;
159         ep->key_changed_at = jiffies;
160
161         /* SCTP-AUTH extensions*/
162         INIT_LIST_HEAD(&ep->endpoint_shared_keys);
163         null_key = sctp_auth_shkey_create(0, GFP_KERNEL);
164         if (!null_key)
165                 goto nomem;
166
167         list_add(&null_key->key_list, &ep->endpoint_shared_keys);
168
169         /* Allocate and initialize transorms arrays for suported HMACs. */
170         err = sctp_auth_init_hmacs(ep, gfp);
171         if (err)
172                 goto nomem_hmacs;
173
174         /* Add the null key to the endpoint shared keys list and
175          * set the hmcas and chunks pointers.
176          */
177         ep->auth_hmacs_list = auth_hmacs;
178         ep->auth_chunk_list = auth_chunks;
179
180         return ep;
181
182 nomem_hmacs:
183         sctp_auth_destroy_keys(&ep->endpoint_shared_keys);
184 nomem:
185         /* Free all allocations */
186         kfree(auth_hmacs);
187         kfree(auth_chunks);
188         kfree(ep->digest);
189         return NULL;
190
191 }
192
193 /* Create a sctp_endpoint with all that boring stuff initialized.
194  * Returns NULL if there isn't enough memory.
195  */
196 struct sctp_endpoint *sctp_endpoint_new(struct sock *sk, gfp_t gfp)
197 {
198         struct sctp_endpoint *ep;
199
200         /* Build a local endpoint. */
201         ep = t_new(struct sctp_endpoint, gfp);
202         if (!ep)
203                 goto fail;
204         if (!sctp_endpoint_init(ep, sk, gfp))
205                 goto fail_init;
206         ep->base.malloced = 1;
207         SCTP_DBG_OBJCNT_INC(ep);
208         return ep;
209
210 fail_init:
211         kfree(ep);
212 fail:
213         return NULL;
214 }
215
216 /* Add an association to an endpoint.  */
217 void sctp_endpoint_add_asoc(struct sctp_endpoint *ep,
218                             struct sctp_association *asoc)
219 {
220         struct sock *sk = ep->base.sk;
221
222         /* If this is a temporary association, don't bother
223          * since we'll be removing it shortly and don't
224          * want anyone to find it anyway.
225          */
226         if (asoc->temp)
227                 return;
228
229         /* Now just add it to our list of asocs */
230         list_add_tail(&asoc->asocs, &ep->asocs);
231
232         /* Increment the backlog value for a TCP-style listening socket. */
233         if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
234                 sk->sk_ack_backlog++;
235 }
236
237 /* Free the endpoint structure.  Delay cleanup until
238  * all users have released their reference count on this structure.
239  */
240 void sctp_endpoint_free(struct sctp_endpoint *ep)
241 {
242         ep->base.dead = 1;
243
244         ep->base.sk->sk_state = SCTP_SS_CLOSED;
245
246         /* Unlink this endpoint, so we can't find it again! */
247         sctp_unhash_endpoint(ep);
248
249         sctp_endpoint_put(ep);
250 }
251
252 /* Final destructor for endpoint.  */
253 static void sctp_endpoint_destroy(struct sctp_endpoint *ep)
254 {
255         SCTP_ASSERT(ep->base.dead, "Endpoint is not dead", return);
256
257         /* Free up the HMAC transform. */
258         crypto_free_hash(sctp_sk(ep->base.sk)->hmac);
259
260         /* Free the digest buffer */
261         kfree(ep->digest);
262
263         /* SCTP-AUTH: Free up AUTH releated data such as shared keys
264          * chunks and hmacs arrays that were allocated
265          */
266         sctp_auth_destroy_keys(&ep->endpoint_shared_keys);
267         kfree(ep->auth_hmacs_list);
268         kfree(ep->auth_chunk_list);
269
270         /* AUTH - Free any allocated HMAC transform containers */
271         sctp_auth_destroy_hmacs(ep->auth_hmacs);
272
273         /* Cleanup. */
274         sctp_inq_free(&ep->base.inqueue);
275         sctp_bind_addr_free(&ep->base.bind_addr);
276
277         /* Remove and free the port */
278         if (sctp_sk(ep->base.sk)->bind_hash)
279                 sctp_put_port(ep->base.sk);
280
281         /* Give up our hold on the sock. */
282         if (ep->base.sk)
283                 sock_put(ep->base.sk);
284
285         /* Finally, free up our memory. */
286         if (ep->base.malloced) {
287                 kfree(ep);
288                 SCTP_DBG_OBJCNT_DEC(ep);
289         }
290 }
291
292 /* Hold a reference to an endpoint. */
293 void sctp_endpoint_hold(struct sctp_endpoint *ep)
294 {
295         atomic_inc(&ep->base.refcnt);
296 }
297
298 /* Release a reference to an endpoint and clean up if there are
299  * no more references.
300  */
301 void sctp_endpoint_put(struct sctp_endpoint *ep)
302 {
303         if (atomic_dec_and_test(&ep->base.refcnt))
304                 sctp_endpoint_destroy(ep);
305 }
306
307 /* Is this the endpoint we are looking for?  */
308 struct sctp_endpoint *sctp_endpoint_is_match(struct sctp_endpoint *ep,
309                                                const union sctp_addr *laddr)
310 {
311         struct sctp_endpoint *retval = NULL;
312
313         if (htons(ep->base.bind_addr.port) == laddr->v4.sin_port) {
314                 if (sctp_bind_addr_match(&ep->base.bind_addr, laddr,
315                                          sctp_sk(ep->base.sk)))
316                         retval = ep;
317         }
318
319         return retval;
320 }
321
322 /* Find the association that goes with this chunk.
323  * We do a linear search of the associations for this endpoint.
324  * We return the matching transport address too.
325  */
326 static struct sctp_association *__sctp_endpoint_lookup_assoc(
327         const struct sctp_endpoint *ep,
328         const union sctp_addr *paddr,
329         struct sctp_transport **transport)
330 {
331         struct sctp_association *asoc = NULL;
332         struct sctp_transport *t = NULL;
333         struct sctp_hashbucket *head;
334         struct sctp_ep_common *epb;
335         int hash;
336         int rport;
337
338         *transport = NULL;
339         rport = ntohs(paddr->v4.sin_port);
340
341         hash = sctp_assoc_hashfn(ep->base.bind_addr.port, rport);
342         head = &sctp_assoc_hashtable[hash];
343         read_lock(&head->lock);
344         for (epb = head->chain; epb; epb = epb->next) {
345                 asoc = sctp_assoc(epb);
346                 if (asoc->ep != ep || rport != asoc->peer.port)
347                         goto next;
348
349                 t = sctp_assoc_lookup_paddr(asoc, paddr);
350                 if (t) {
351                         *transport = t;
352                         break;
353                 }
354 next:
355                 asoc = NULL;
356         }
357         read_unlock(&head->lock);
358         return asoc;
359 }
360
361 /* Lookup association on an endpoint based on a peer address.  BH-safe.  */
362 struct sctp_association *sctp_endpoint_lookup_assoc(
363         const struct sctp_endpoint *ep,
364         const union sctp_addr *paddr,
365         struct sctp_transport **transport)
366 {
367         struct sctp_association *asoc;
368
369         sctp_local_bh_disable();
370         asoc = __sctp_endpoint_lookup_assoc(ep, paddr, transport);
371         sctp_local_bh_enable();
372
373         return asoc;
374 }
375
376 /* Look for any peeled off association from the endpoint that matches the
377  * given peer address.
378  */
379 int sctp_endpoint_is_peeled_off(struct sctp_endpoint *ep,
380                                 const union sctp_addr *paddr)
381 {
382         struct sctp_sockaddr_entry *addr;
383         struct sctp_bind_addr *bp;
384
385         bp = &ep->base.bind_addr;
386         /* This function is called with the socket lock held,
387          * so the address_list can not change.
388          */
389         list_for_each_entry(addr, &bp->address_list, list) {
390                 if (sctp_has_association(&addr->a, paddr))
391                         return 1;
392         }
393
394         return 0;
395 }
396
397 /* Do delayed input processing.  This is scheduled by sctp_rcv().
398  * This may be called on BH or task time.
399  */
400 static void sctp_endpoint_bh_rcv(struct work_struct *work)
401 {
402         struct sctp_endpoint *ep =
403                 container_of(work, struct sctp_endpoint,
404                              base.inqueue.immediate);
405         struct sctp_association *asoc;
406         struct sock *sk;
407         struct sctp_transport *transport;
408         struct sctp_chunk *chunk;
409         struct sctp_inq *inqueue;
410         sctp_subtype_t subtype;
411         sctp_state_t state;
412         int error = 0;
413         int first_time = 1;     /* is this the first time through the looop */
414
415         if (ep->base.dead)
416                 return;
417
418         asoc = NULL;
419         inqueue = &ep->base.inqueue;
420         sk = ep->base.sk;
421
422         while (NULL != (chunk = sctp_inq_pop(inqueue))) {
423                 subtype = SCTP_ST_CHUNK(chunk->chunk_hdr->type);
424
425                 /* If the first chunk in the packet is AUTH, do special
426                  * processing specified in Section 6.3 of SCTP-AUTH spec
427                  */
428                 if (first_time && (subtype.chunk == SCTP_CID_AUTH)) {
429                         struct sctp_chunkhdr *next_hdr;
430
431                         next_hdr = sctp_inq_peek(inqueue);
432                         if (!next_hdr)
433                                 goto normal;
434
435                         /* If the next chunk is COOKIE-ECHO, skip the AUTH
436                          * chunk while saving a pointer to it so we can do
437                          * Authentication later (during cookie-echo
438                          * processing).
439                          */
440                         if (next_hdr->type == SCTP_CID_COOKIE_ECHO) {
441                                 chunk->auth_chunk = skb_clone(chunk->skb,
442                                                                 GFP_ATOMIC);
443                                 chunk->auth = 1;
444                                 continue;
445                         }
446                 }
447 normal:
448                 /* We might have grown an association since last we
449                  * looked, so try again.
450                  *
451                  * This happens when we've just processed our
452                  * COOKIE-ECHO chunk.
453                  */
454                 if (NULL == chunk->asoc) {
455                         asoc = sctp_endpoint_lookup_assoc(ep,
456                                                           sctp_source(chunk),
457                                                           &transport);
458                         chunk->asoc = asoc;
459                         chunk->transport = transport;
460                 }
461
462                 state = asoc ? asoc->state : SCTP_STATE_CLOSED;
463                 if (sctp_auth_recv_cid(subtype.chunk, asoc) && !chunk->auth)
464                         continue;
465
466                 /* Remember where the last DATA chunk came from so we
467                  * know where to send the SACK.
468                  */
469                 if (asoc && sctp_chunk_is_data(chunk))
470                         asoc->peer.last_data_from = chunk->transport;
471                 else
472                         SCTP_INC_STATS(SCTP_MIB_INCTRLCHUNKS);
473
474                 if (chunk->transport)
475                         chunk->transport->last_time_heard = jiffies;
476
477                 error = sctp_do_sm(SCTP_EVENT_T_CHUNK, subtype, state,
478                                    ep, asoc, chunk, GFP_ATOMIC);
479
480                 if (error && chunk)
481                         chunk->pdiscard = 1;
482
483                 /* Check to see if the endpoint is freed in response to
484                  * the incoming chunk. If so, get out of the while loop.
485                  */
486                 if (!sctp_sk(sk)->ep)
487                         break;
488
489                 if (first_time)
490                         first_time = 0;
491         }
492 }