blob: 129756908da49992b5a42e07b84f7521a778e891 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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. */
David Howellsc4028952006-11-22 14:57:56 +000064static void sctp_endpoint_bh_rcv(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66/*
67 * Initialize the base fields of the endpoint structure.
68 */
69static struct sctp_endpoint *sctp_endpoint_init(struct sctp_endpoint *ep,
Alexey Dobriyan3182cd82005-07-11 20:57:47 -070070 struct sock *sk,
Al Virodd0fc662005-10-07 07:46:04 +010071 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 memset(ep, 0, sizeof(struct sctp_endpoint));
74
Vlad Yasevichb68dbca2006-11-09 16:29:57 -080075 ep->digest = kzalloc(SCTP_SIGNATURE_SIZE, gfp);
76 if (!ep->digest)
77 return NULL;
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 /* 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 */
David Howellsc4028952006-11-22 14:57:56 +000092 sctp_inq_set_th_handler(&ep->base.inqueue, sctp_endpoint_bh_rcv);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94 /* Initialize the bind addr area */
95 sctp_bind_addr_init(&ep->base.bind_addr, 0);
96 rwlock_init(&ep->base.addr_lock);
97
98 /* Remember who we are attached to. */
99 ep->base.sk = sk;
100 sock_hold(ep->base.sk);
101
102 /* Create the lists of associations. */
103 INIT_LIST_HEAD(&ep->asocs);
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 /* Use SCTP specific send buffer space queues. */
Neil Horman4eb701d2005-04-28 12:02:04 -0700106 ep->sndbuf_policy = sctp_sndbuf_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 sk->sk_write_space = sctp_write_space;
108 sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
109
Neil Horman049b3ff2005-11-11 16:08:24 -0800110 /* Get the receive buffer policy for this endpoint */
111 ep->rcvbuf_policy = sctp_rcvbuf_policy;
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 /* Initialize the secret key used with cookie. */
114 get_random_bytes(&ep->secret_key[0], SCTP_SECRET_SIZE);
115 ep->last_key = ep->current_key = 0;
116 ep->key_changed_at = jiffies;
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return ep;
119}
120
121/* Create a sctp_endpoint with all that boring stuff initialized.
122 * Returns NULL if there isn't enough memory.
123 */
Al Virodd0fc662005-10-07 07:46:04 +0100124struct sctp_endpoint *sctp_endpoint_new(struct sock *sk, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 struct sctp_endpoint *ep;
127
128 /* Build a local endpoint. */
129 ep = t_new(struct sctp_endpoint, gfp);
130 if (!ep)
131 goto fail;
132 if (!sctp_endpoint_init(ep, sk, gfp))
133 goto fail_init;
134 ep->base.malloced = 1;
135 SCTP_DBG_OBJCNT_INC(ep);
136 return ep;
137
138fail_init:
139 kfree(ep);
140fail:
141 return NULL;
142}
143
144/* Add an association to an endpoint. */
145void sctp_endpoint_add_asoc(struct sctp_endpoint *ep,
146 struct sctp_association *asoc)
147{
148 struct sock *sk = ep->base.sk;
149
Vlad Yasevichde76e692006-10-30 18:55:11 -0800150 /* If this is a temporary association, don't bother
151 * since we'll be removing it shortly and don't
152 * want anyone to find it anyway.
153 */
154 if (asoc->temp)
155 return;
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 /* Now just add it to our list of asocs */
158 list_add_tail(&asoc->asocs, &ep->asocs);
159
160 /* Increment the backlog value for a TCP-style listening socket. */
161 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
162 sk->sk_ack_backlog++;
163}
164
165/* Free the endpoint structure. Delay cleanup until
166 * all users have released their reference count on this structure.
167 */
168void sctp_endpoint_free(struct sctp_endpoint *ep)
169{
170 ep->base.dead = 1;
Vlad Yasevichcfdeef32006-07-21 14:48:26 -0700171
172 ep->base.sk->sk_state = SCTP_SS_CLOSED;
173
174 /* Unlink this endpoint, so we can't find it again! */
175 sctp_unhash_endpoint(ep);
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 sctp_endpoint_put(ep);
178}
179
180/* Final destructor for endpoint. */
181static void sctp_endpoint_destroy(struct sctp_endpoint *ep)
182{
183 SCTP_ASSERT(ep->base.dead, "Endpoint is not dead", return);
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 /* Free up the HMAC transform. */
Herbert Xu1b489e12006-08-20 15:07:14 +1000186 crypto_free_hash(sctp_sk(ep->base.sk)->hmac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Vlad Yasevichb68dbca2006-11-09 16:29:57 -0800188 /* Free the digest buffer */
189 kfree(ep->digest);
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 /* Cleanup. */
192 sctp_inq_free(&ep->base.inqueue);
193 sctp_bind_addr_free(&ep->base.bind_addr);
194
195 /* Remove and free the port */
196 if (sctp_sk(ep->base.sk)->bind_hash)
197 sctp_put_port(ep->base.sk);
198
199 /* Give up our hold on the sock. */
200 if (ep->base.sk)
201 sock_put(ep->base.sk);
202
203 /* Finally, free up our memory. */
204 if (ep->base.malloced) {
205 kfree(ep);
206 SCTP_DBG_OBJCNT_DEC(ep);
207 }
208}
209
210/* Hold a reference to an endpoint. */
211void sctp_endpoint_hold(struct sctp_endpoint *ep)
212{
213 atomic_inc(&ep->base.refcnt);
214}
215
216/* Release a reference to an endpoint and clean up if there are
217 * no more references.
218 */
219void sctp_endpoint_put(struct sctp_endpoint *ep)
220{
221 if (atomic_dec_and_test(&ep->base.refcnt))
222 sctp_endpoint_destroy(ep);
223}
224
225/* Is this the endpoint we are looking for? */
226struct sctp_endpoint *sctp_endpoint_is_match(struct sctp_endpoint *ep,
227 const union sctp_addr *laddr)
228{
229 struct sctp_endpoint *retval;
230
231 sctp_read_lock(&ep->base.addr_lock);
Al Viro1c7d1fc2006-11-20 17:08:09 -0800232 if (htons(ep->base.bind_addr.port) == laddr->v4.sin_port) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (sctp_bind_addr_match(&ep->base.bind_addr, laddr,
234 sctp_sk(ep->base.sk))) {
235 retval = ep;
236 goto out;
237 }
238 }
239
240 retval = NULL;
241
242out:
243 sctp_read_unlock(&ep->base.addr_lock);
244 return retval;
245}
246
247/* Find the association that goes with this chunk.
248 * We do a linear search of the associations for this endpoint.
249 * We return the matching transport address too.
250 */
251static struct sctp_association *__sctp_endpoint_lookup_assoc(
252 const struct sctp_endpoint *ep,
253 const union sctp_addr *paddr,
254 struct sctp_transport **transport)
255{
256 int rport;
257 struct sctp_association *asoc;
258 struct list_head *pos;
259
Al Virocd4ff032006-11-20 17:11:33 -0800260 rport = ntohs(paddr->v4.sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 list_for_each(pos, &ep->asocs) {
263 asoc = list_entry(pos, struct sctp_association, asocs);
264 if (rport == asoc->peer.port) {
265 sctp_read_lock(&asoc->base.addr_lock);
266 *transport = sctp_assoc_lookup_paddr(asoc, paddr);
267 sctp_read_unlock(&asoc->base.addr_lock);
268
269 if (*transport)
270 return asoc;
271 }
272 }
273
274 *transport = NULL;
275 return NULL;
276}
277
278/* Lookup association on an endpoint based on a peer address. BH-safe. */
279struct sctp_association *sctp_endpoint_lookup_assoc(
280 const struct sctp_endpoint *ep,
281 const union sctp_addr *paddr,
282 struct sctp_transport **transport)
283{
284 struct sctp_association *asoc;
285
286 sctp_local_bh_disable();
287 asoc = __sctp_endpoint_lookup_assoc(ep, paddr, transport);
288 sctp_local_bh_enable();
289
290 return asoc;
291}
292
293/* Look for any peeled off association from the endpoint that matches the
294 * given peer address.
295 */
296int sctp_endpoint_is_peeled_off(struct sctp_endpoint *ep,
297 const union sctp_addr *paddr)
298{
299 struct list_head *pos;
300 struct sctp_sockaddr_entry *addr;
301 struct sctp_bind_addr *bp;
302
303 sctp_read_lock(&ep->base.addr_lock);
304 bp = &ep->base.bind_addr;
305 list_for_each(pos, &bp->address_list) {
306 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
307 if (sctp_has_association(&addr->a, paddr)) {
308 sctp_read_unlock(&ep->base.addr_lock);
309 return 1;
310 }
311 }
312 sctp_read_unlock(&ep->base.addr_lock);
313
314 return 0;
315}
316
317/* Do delayed input processing. This is scheduled by sctp_rcv().
318 * This may be called on BH or task time.
319 */
David Howellsc4028952006-11-22 14:57:56 +0000320static void sctp_endpoint_bh_rcv(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
David Howellsc4028952006-11-22 14:57:56 +0000322 struct sctp_endpoint *ep =
323 container_of(work, struct sctp_endpoint,
324 base.inqueue.immediate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 struct sctp_association *asoc;
326 struct sock *sk;
327 struct sctp_transport *transport;
328 struct sctp_chunk *chunk;
329 struct sctp_inq *inqueue;
330 sctp_subtype_t subtype;
331 sctp_state_t state;
332 int error = 0;
333
334 if (ep->base.dead)
335 return;
336
337 asoc = NULL;
338 inqueue = &ep->base.inqueue;
339 sk = ep->base.sk;
340
341 while (NULL != (chunk = sctp_inq_pop(inqueue))) {
342 subtype = SCTP_ST_CHUNK(chunk->chunk_hdr->type);
343
344 /* We might have grown an association since last we
345 * looked, so try again.
346 *
347 * This happens when we've just processed our
348 * COOKIE-ECHO chunk.
349 */
350 if (NULL == chunk->asoc) {
351 asoc = sctp_endpoint_lookup_assoc(ep,
352 sctp_source(chunk),
353 &transport);
354 chunk->asoc = asoc;
355 chunk->transport = transport;
356 }
357
358 state = asoc ? asoc->state : SCTP_STATE_CLOSED;
359
360 /* Remember where the last DATA chunk came from so we
361 * know where to send the SACK.
362 */
363 if (asoc && sctp_chunk_is_data(chunk))
364 asoc->peer.last_data_from = chunk->transport;
365 else
366 SCTP_INC_STATS(SCTP_MIB_INCTRLCHUNKS);
367
368 if (chunk->transport)
369 chunk->transport->last_time_heard = jiffies;
370
371 error = sctp_do_sm(SCTP_EVENT_T_CHUNK, subtype, state,
372 ep, asoc, chunk, GFP_ATOMIC);
373
374 if (error && chunk)
375 chunk->pdiscard = 1;
376
377 /* Check to see if the endpoint is freed in response to
378 * the incoming chunk. If so, get out of the while loop.
379 */
380 if (!sctp_sk(sk)->ep)
381 break;
382 }
383}