[NET]: Fix wrong comments for unregister_net*
[linux-2.6/mini2440.git] / net / sctp / endpointola.c
blobde6f505d6ff8250291972d4e53c391dcf1548e57
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
9 * This file is part of the SCTP kernel reference Implementation
11 * This abstraction represents an SCTP endpoint.
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.
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.
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.
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.
34 * Please send any bug reports or fixes you make to the
35 * email address(es):
36 * lksctp developers <lksctp-developers@lists.sourceforge.net>
38 * Or submit a bug report through the following website:
39 * http://www.sf.net/projects/lksctp
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>
48 * Any bugs reported given to us we will try to fix... any fixes shared will
49 * be incorporated into the next SCTP release.
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>
62 /* Forward declarations for internal helpers. */
63 static void sctp_endpoint_bh_rcv(struct work_struct *work);
66 * Initialize the base fields of the endpoint structure.
68 static struct sctp_endpoint *sctp_endpoint_init(struct sctp_endpoint *ep,
69 struct sock *sk,
70 gfp_t gfp)
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;
77 memset(ep, 0, sizeof(struct sctp_endpoint));
79 ep->digest = kzalloc(SCTP_SIGNATURE_SIZE, gfp);
80 if (!ep->digest)
81 return NULL;
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.
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;
93 auth_chunks = kzalloc(sizeof(sctp_chunks_param_t) +
94 SCTP_NUM_CHUNK_TYPES, gfp);
95 if (!auth_chunks)
96 goto nomem;
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.
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);
108 /* Initialize the CHUNKS parameter */
109 auth_chunks->param_hdr.type = SCTP_PARAM_CHUNKS;
111 /* If the Add-IP functionality is enabled, we must
112 * authenticate, ASCONF and ASCONF-ACK chunks
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);
122 /* Initialize the base structure. */
123 /* What type of endpoint are we? */
124 ep->base.type = SCTP_EP_TYPE_SOCKET;
126 /* Initialize the basic object fields. */
127 atomic_set(&ep->base.refcnt, 1);
128 ep->base.dead = 0;
129 ep->base.malloced = 1;
131 /* Create an input queue. */
132 sctp_inq_init(&ep->base.inqueue);
134 /* Set its top-half handler */
135 sctp_inq_set_th_handler(&ep->base.inqueue, sctp_endpoint_bh_rcv);
137 /* Initialize the bind addr area */
138 sctp_bind_addr_init(&ep->base.bind_addr, 0);
140 /* Remember who we are attached to. */
141 ep->base.sk = sk;
142 sock_hold(ep->base.sk);
144 /* Create the lists of associations. */
145 INIT_LIST_HEAD(&ep->asocs);
147 /* Use SCTP specific send buffer space queues. */
148 ep->sndbuf_policy = sctp_sndbuf_policy;
150 sk->sk_write_space = sctp_write_space;
151 sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
153 /* Get the receive buffer policy for this endpoint */
154 ep->rcvbuf_policy = sctp_rcvbuf_policy;
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;
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;
167 list_add(&null_key->key_list, &ep->endpoint_shared_keys);
169 /* Allocate and initialize transorms arrays for suported HMACs. */
170 err = sctp_auth_init_hmacs(ep, gfp);
171 if (err)
172 goto nomem_hmacs;
174 /* Add the null key to the endpoint shared keys list and
175 * set the hmcas and chunks pointers.
177 ep->auth_hmacs_list = auth_hmacs;
178 ep->auth_chunk_list = auth_chunks;
180 return ep;
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;
193 /* Create a sctp_endpoint with all that boring stuff initialized.
194 * Returns NULL if there isn't enough memory.
196 struct sctp_endpoint *sctp_endpoint_new(struct sock *sk, gfp_t gfp)
198 struct sctp_endpoint *ep;
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;
210 fail_init:
211 kfree(ep);
212 fail:
213 return NULL;
216 /* Add an association to an endpoint. */
217 void sctp_endpoint_add_asoc(struct sctp_endpoint *ep,
218 struct sctp_association *asoc)
220 struct sock *sk = ep->base.sk;
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.
226 if (asoc->temp)
227 return;
229 /* Now just add it to our list of asocs */
230 list_add_tail(&asoc->asocs, &ep->asocs);
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++;
237 /* Free the endpoint structure. Delay cleanup until
238 * all users have released their reference count on this structure.
240 void sctp_endpoint_free(struct sctp_endpoint *ep)
242 ep->base.dead = 1;
244 ep->base.sk->sk_state = SCTP_SS_CLOSED;
246 /* Unlink this endpoint, so we can't find it again! */
247 sctp_unhash_endpoint(ep);
249 sctp_endpoint_put(ep);
252 /* Final destructor for endpoint. */
253 static void sctp_endpoint_destroy(struct sctp_endpoint *ep)
255 SCTP_ASSERT(ep->base.dead, "Endpoint is not dead", return);
257 /* Free up the HMAC transform. */
258 crypto_free_hash(sctp_sk(ep->base.sk)->hmac);
260 /* Free the digest buffer */
261 kfree(ep->digest);
263 /* SCTP-AUTH: Free up AUTH releated data such as shared keys
264 * chunks and hmacs arrays that were allocated
266 sctp_auth_destroy_keys(&ep->endpoint_shared_keys);
267 kfree(ep->auth_hmacs_list);
268 kfree(ep->auth_chunk_list);
270 /* AUTH - Free any allocated HMAC transform containers */
271 sctp_auth_destroy_hmacs(ep->auth_hmacs);
273 /* Cleanup. */
274 sctp_inq_free(&ep->base.inqueue);
275 sctp_bind_addr_free(&ep->base.bind_addr);
277 /* Remove and free the port */
278 if (sctp_sk(ep->base.sk)->bind_hash)
279 sctp_put_port(ep->base.sk);
281 /* Give up our hold on the sock. */
282 if (ep->base.sk)
283 sock_put(ep->base.sk);
285 /* Finally, free up our memory. */
286 if (ep->base.malloced) {
287 kfree(ep);
288 SCTP_DBG_OBJCNT_DEC(ep);
292 /* Hold a reference to an endpoint. */
293 void sctp_endpoint_hold(struct sctp_endpoint *ep)
295 atomic_inc(&ep->base.refcnt);
298 /* Release a reference to an endpoint and clean up if there are
299 * no more references.
301 void sctp_endpoint_put(struct sctp_endpoint *ep)
303 if (atomic_dec_and_test(&ep->base.refcnt))
304 sctp_endpoint_destroy(ep);
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)
311 struct sctp_endpoint *retval = NULL;
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;
319 return retval;
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.
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)
331 struct sctp_association *asoc = NULL;
332 struct sctp_transport *t = NULL;
333 struct sctp_hashbucket *head;
334 struct sctp_ep_common *epb;
335 struct hlist_node *node;
336 int hash;
337 int rport;
339 *transport = NULL;
340 rport = ntohs(paddr->v4.sin_port);
342 hash = sctp_assoc_hashfn(ep->base.bind_addr.port, rport);
343 head = &sctp_assoc_hashtable[hash];
344 read_lock(&head->lock);
345 sctp_for_each_hentry(epb, node, &head->chain) {
346 asoc = sctp_assoc(epb);
347 if (asoc->ep != ep || rport != asoc->peer.port)
348 goto next;
350 t = sctp_assoc_lookup_paddr(asoc, paddr);
351 if (t) {
352 *transport = t;
353 break;
355 next:
356 asoc = NULL;
358 read_unlock(&head->lock);
359 return asoc;
362 /* Lookup association on an endpoint based on a peer address. BH-safe. */
363 struct sctp_association *sctp_endpoint_lookup_assoc(
364 const struct sctp_endpoint *ep,
365 const union sctp_addr *paddr,
366 struct sctp_transport **transport)
368 struct sctp_association *asoc;
370 sctp_local_bh_disable();
371 asoc = __sctp_endpoint_lookup_assoc(ep, paddr, transport);
372 sctp_local_bh_enable();
374 return asoc;
377 /* Look for any peeled off association from the endpoint that matches the
378 * given peer address.
380 int sctp_endpoint_is_peeled_off(struct sctp_endpoint *ep,
381 const union sctp_addr *paddr)
383 struct sctp_sockaddr_entry *addr;
384 struct sctp_bind_addr *bp;
386 bp = &ep->base.bind_addr;
387 /* This function is called with the socket lock held,
388 * so the address_list can not change.
390 list_for_each_entry(addr, &bp->address_list, list) {
391 if (sctp_has_association(&addr->a, paddr))
392 return 1;
395 return 0;
398 /* Do delayed input processing. This is scheduled by sctp_rcv().
399 * This may be called on BH or task time.
401 static void sctp_endpoint_bh_rcv(struct work_struct *work)
403 struct sctp_endpoint *ep =
404 container_of(work, struct sctp_endpoint,
405 base.inqueue.immediate);
406 struct sctp_association *asoc;
407 struct sock *sk;
408 struct sctp_transport *transport;
409 struct sctp_chunk *chunk;
410 struct sctp_inq *inqueue;
411 sctp_subtype_t subtype;
412 sctp_state_t state;
413 int error = 0;
414 int first_time = 1; /* is this the first time through the looop */
416 if (ep->base.dead)
417 return;
419 asoc = NULL;
420 inqueue = &ep->base.inqueue;
421 sk = ep->base.sk;
423 while (NULL != (chunk = sctp_inq_pop(inqueue))) {
424 subtype = SCTP_ST_CHUNK(chunk->chunk_hdr->type);
426 /* If the first chunk in the packet is AUTH, do special
427 * processing specified in Section 6.3 of SCTP-AUTH spec
429 if (first_time && (subtype.chunk == SCTP_CID_AUTH)) {
430 struct sctp_chunkhdr *next_hdr;
432 next_hdr = sctp_inq_peek(inqueue);
433 if (!next_hdr)
434 goto normal;
436 /* If the next chunk is COOKIE-ECHO, skip the AUTH
437 * chunk while saving a pointer to it so we can do
438 * Authentication later (during cookie-echo
439 * processing).
441 if (next_hdr->type == SCTP_CID_COOKIE_ECHO) {
442 chunk->auth_chunk = skb_clone(chunk->skb,
443 GFP_ATOMIC);
444 chunk->auth = 1;
445 continue;
448 normal:
449 /* We might have grown an association since last we
450 * looked, so try again.
452 * This happens when we've just processed our
453 * COOKIE-ECHO chunk.
455 if (NULL == chunk->asoc) {
456 asoc = sctp_endpoint_lookup_assoc(ep,
457 sctp_source(chunk),
458 &transport);
459 chunk->asoc = asoc;
460 chunk->transport = transport;
463 state = asoc ? asoc->state : SCTP_STATE_CLOSED;
464 if (sctp_auth_recv_cid(subtype.chunk, asoc) && !chunk->auth)
465 continue;
467 /* Remember where the last DATA chunk came from so we
468 * know where to send the SACK.
470 if (asoc && sctp_chunk_is_data(chunk))
471 asoc->peer.last_data_from = chunk->transport;
472 else
473 SCTP_INC_STATS(SCTP_MIB_INCTRLCHUNKS);
475 if (chunk->transport)
476 chunk->transport->last_time_heard = jiffies;
478 error = sctp_do_sm(SCTP_EVENT_T_CHUNK, subtype, state,
479 ep, asoc, chunk, GFP_ATOMIC);
481 if (error && chunk)
482 chunk->pdiscard = 1;
484 /* Check to see if the endpoint is freed in response to
485 * the incoming chunk. If so, get out of the while loop.
487 if (!sctp_sk(sk)->ep)
488 break;
490 if (first_time)
491 first_time = 0;