Merge with Linux 2.5.59.
[linux-2.6/linux-mips.git] / net / sctp / endpointola.c
blobc6c161a450eea3d2abeccf5ed66850539c9845c8
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/sched.h>
54 #include <linux/slab.h>
55 #include <linux/in.h>
56 #include <linux/random.h> /* get_random_bytes() */
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(sctp_endpoint_t *ep);
65 /* Create a sctp_endpoint_t with all that boring stuff initialized.
66 * Returns NULL if there isn't enough memory.
68 sctp_endpoint_t *sctp_endpoint_new(sctp_protocol_t *proto,
69 struct sock *sk, int priority)
71 sctp_endpoint_t *ep;
73 /* Build a local endpoint. */
74 ep = t_new(sctp_endpoint_t, priority);
75 if (!ep)
76 goto fail;
77 if (!sctp_endpoint_init(ep, proto, sk, priority))
78 goto fail_init;
79 ep->base.malloced = 1;
80 SCTP_DBG_OBJCNT_INC(ep);
81 return ep;
83 fail_init:
84 kfree(ep);
85 fail:
86 return NULL;
90 * Initialize the base fields of the endpoint structure.
92 sctp_endpoint_t *sctp_endpoint_init(sctp_endpoint_t *ep, sctp_protocol_t *proto,
93 struct sock *sk, int priority)
95 sctp_opt_t *sp = sctp_sk(sk);
96 memset(ep, 0, sizeof(sctp_endpoint_t));
98 /* Initialize the base structure. */
99 /* What type of endpoint are we? */
100 ep->base.type = SCTP_EP_TYPE_SOCKET;
102 /* Initialize the basic object fields. */
103 atomic_set(&ep->base.refcnt, 1);
104 ep->base.dead = 0;
105 ep->base.malloced = 1;
107 /* Create an input queue. */
108 sctp_inqueue_init(&ep->base.inqueue);
110 /* Set its top-half handler */
111 sctp_inqueue_set_th_handler(&ep->base.inqueue,
112 (void (*)(void *))sctp_endpoint_bh_rcv,
113 ep);
115 /* Initialize the bind addr area */
116 sctp_bind_addr_init(&ep->base.bind_addr, 0);
117 ep->base.addr_lock = RW_LOCK_UNLOCKED;
119 /* Remember who we are attached to. */
120 ep->base.sk = sk;
121 sock_hold(ep->base.sk);
123 /* This pointer is useful to access the default protocol parameter
124 * values.
126 ep->proto = proto;
128 /* Create the lists of associations. */
129 INIT_LIST_HEAD(&ep->asocs);
131 /* Set up the base timeout information. */
132 ep->timeouts[SCTP_EVENT_TIMEOUT_NONE] = 0;
133 ep->timeouts[SCTP_EVENT_TIMEOUT_T1_COOKIE] =
134 SCTP_DEFAULT_TIMEOUT_T1_COOKIE;
135 ep->timeouts[SCTP_EVENT_TIMEOUT_T1_INIT] =
136 SCTP_DEFAULT_TIMEOUT_T1_INIT;
137 ep->timeouts[SCTP_EVENT_TIMEOUT_T2_SHUTDOWN] =
138 sp->rtoinfo.srto_initial;
139 ep->timeouts[SCTP_EVENT_TIMEOUT_T3_RTX] = 0;
140 ep->timeouts[SCTP_EVENT_TIMEOUT_T4_RTO] = 0;
142 /* sctpimpguide-05 Section 2.12.2
143 * If the 'T5-shutdown-guard' timer is used, it SHOULD be set to the
144 * recommended value of 5 times 'RTO.Max'.
146 ep->timeouts[SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD]
147 = 5 * sp->rtoinfo.srto_max;
149 ep->timeouts[SCTP_EVENT_TIMEOUT_HEARTBEAT] =
150 SCTP_DEFAULT_TIMEOUT_HEARTBEAT;
151 ep->timeouts[SCTP_EVENT_TIMEOUT_SACK] =
152 SCTP_DEFAULT_TIMEOUT_SACK;
153 ep->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE] =
154 sp->autoclose * HZ;
155 ep->timeouts[SCTP_EVENT_TIMEOUT_PMTU_RAISE] =
156 SCTP_DEFAULT_TIMEOUT_PMTU_RAISE;
158 /* Set up the default send/receive buffer space. */
160 /* FIXME - Should the min and max window size be configurable
161 * sysctl parameters as opposed to be constants?
163 sk->rcvbuf = SCTP_DEFAULT_MAXWINDOW;
164 sk->sndbuf = SCTP_DEFAULT_MAXWINDOW * 2;
166 /* Use SCTP specific send buffer space queues. */
167 sk->write_space = sctp_write_space;
168 sk->use_write_queue = 1;
170 /* Initialize the secret key used with cookie. */
171 get_random_bytes(&ep->secret_key[0], SCTP_SECRET_SIZE);
172 ep->last_key = ep->current_key = 0;
173 ep->key_changed_at = jiffies;
175 ep->debug_name = "unnamedEndpoint";
176 return ep;
179 /* Add an association to an endpoint. */
180 void sctp_endpoint_add_asoc(sctp_endpoint_t *ep, sctp_association_t *asoc)
182 /* Now just add it to our list of asocs */
183 list_add_tail(&asoc->asocs, &ep->asocs);
186 /* Free the endpoint structure. Delay cleanup until
187 * all users have released their reference count on this structure.
189 void sctp_endpoint_free(sctp_endpoint_t *ep)
191 ep->base.dead = 1;
192 sctp_endpoint_put(ep);
195 /* Final destructor for endpoint. */
196 void sctp_endpoint_destroy(sctp_endpoint_t *ep)
198 SCTP_ASSERT(ep->base.dead, "Endpoint is not dead", return);
200 /* Unlink this endpoint, so we can't find it again! */
201 sctp_unhash_endpoint(ep);
203 /* Cleanup the inqueue. */
204 sctp_inqueue_free(&ep->base.inqueue);
206 sctp_bind_addr_free(&ep->base.bind_addr);
208 /* Remove and free the port */
209 if (ep->base.sk->prev != NULL)
210 sctp_put_port(ep->base.sk);
212 /* Give up our hold on the sock. */
213 if (ep->base.sk)
214 sock_put(ep->base.sk);
216 /* Finally, free up our memory. */
217 if (ep->base.malloced) {
218 kfree(ep);
219 SCTP_DBG_OBJCNT_DEC(ep);
223 /* Hold a reference to an endpoint. */
224 void sctp_endpoint_hold(sctp_endpoint_t *ep)
226 atomic_inc(&ep->base.refcnt);
229 /* Release a reference to an endpoint and clean up if there are
230 * no more references.
232 void sctp_endpoint_put(sctp_endpoint_t *ep)
234 if (atomic_dec_and_test(&ep->base.refcnt))
235 sctp_endpoint_destroy(ep);
238 /* Is this the endpoint we are looking for? */
239 sctp_endpoint_t *sctp_endpoint_is_match(sctp_endpoint_t *ep,
240 const union sctp_addr *laddr)
242 sctp_endpoint_t *retval;
244 sctp_read_lock(&ep->base.addr_lock);
245 if (ep->base.bind_addr.port == laddr->v4.sin_port) {
246 if (sctp_bind_addr_match(&ep->base.bind_addr, laddr,
247 sctp_sk(ep->base.sk))) {
248 retval = ep;
249 goto out;
253 retval = NULL;
255 out:
256 sctp_read_unlock(&ep->base.addr_lock);
257 return retval;
260 /* Find the association that goes with this chunk.
261 * We do a linear search of the associations for this endpoint.
262 * We return the matching transport address too.
264 sctp_association_t *__sctp_endpoint_lookup_assoc(
265 const sctp_endpoint_t *endpoint,
266 const union sctp_addr *paddr,
267 sctp_transport_t **transport)
269 int rport;
270 sctp_association_t *asoc;
271 struct list_head *pos;
273 rport = paddr->v4.sin_port;
275 list_for_each(pos, &endpoint->asocs) {
276 asoc = list_entry(pos, sctp_association_t, asocs);
277 if (rport == asoc->peer.port) {
278 sctp_read_lock(&asoc->base.addr_lock);
279 *transport = sctp_assoc_lookup_paddr(asoc, paddr);
280 sctp_read_unlock(&asoc->base.addr_lock);
282 if (*transport)
283 return asoc;
287 *transport = NULL;
288 return NULL;
291 /* Lookup association on an endpoint based on a peer address. BH-safe. */
292 sctp_association_t *sctp_endpoint_lookup_assoc(const sctp_endpoint_t *ep,
293 const union sctp_addr *paddr,
294 sctp_transport_t **transport)
296 sctp_association_t *asoc;
298 sctp_local_bh_disable();
299 asoc = __sctp_endpoint_lookup_assoc(ep, paddr, transport);
300 sctp_local_bh_enable();
302 return asoc;
305 /* Look for any peeled off association from the endpoint that matches the
306 * given peer address.
308 int sctp_endpoint_is_peeled_off(sctp_endpoint_t *ep,
309 const union sctp_addr *paddr)
311 struct list_head *pos;
312 struct sockaddr_storage_list *addr;
313 sctp_bind_addr_t *bp;
315 sctp_read_lock(&ep->base.addr_lock);
316 bp = &ep->base.bind_addr;
317 list_for_each(pos, &bp->address_list) {
318 addr = list_entry(pos, struct sockaddr_storage_list, list);
319 if (sctp_has_association(&addr->a, paddr)) {
320 sctp_read_unlock(&ep->base.addr_lock);
321 return 1;
324 sctp_read_unlock(&ep->base.addr_lock);
326 return 0;
329 /* Do delayed input processing. This is scheduled by sctp_rcv().
330 * This may be called on BH or task time.
332 static void sctp_endpoint_bh_rcv(sctp_endpoint_t *ep)
334 sctp_association_t *asoc;
335 struct sock *sk;
336 sctp_transport_t *transport;
337 sctp_chunk_t *chunk;
338 sctp_inqueue_t *inqueue;
339 sctp_subtype_t subtype;
340 sctp_state_t state;
341 int error = 0;
343 if (ep->base.dead)
344 return;
346 asoc = NULL;
347 inqueue = &ep->base.inqueue;
348 sk = ep->base.sk;
350 while (NULL != (chunk = sctp_pop_inqueue(inqueue))) {
351 subtype.chunk = chunk->chunk_hdr->type;
353 /* We might have grown an association since last we
354 * looked, so try again.
356 * This happens when we've just processed our
357 * COOKIE-ECHO chunk.
359 if (NULL == chunk->asoc) {
360 asoc = sctp_endpoint_lookup_assoc(ep,
361 sctp_source(chunk),
362 &transport);
363 chunk->asoc = asoc;
364 chunk->transport = transport;
367 state = asoc ? asoc->state : SCTP_STATE_CLOSED;
369 /* Remember where the last DATA chunk came from so we
370 * know where to send the SACK.
372 if (asoc && sctp_chunk_is_data(chunk))
373 asoc->peer.last_data_from = chunk->transport;
375 if (chunk->transport)
376 chunk->transport->last_time_heard = jiffies;
378 error = sctp_do_sm(SCTP_EVENT_T_CHUNK, subtype, state,
379 ep, asoc, chunk, GFP_ATOMIC);
381 if (error && chunk)
382 chunk->pdiscard = 1;
384 /* Check to see if the endpoint is freed in response to
385 * the incoming chunk. If so, get out of the while loop.
387 if (!sctp_sk(sk)->ep)
388 break;