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)
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
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>
56 #include <linux/random.h> /* get_random_bytes() */
57 #include <linux/crypto.h>
60 #include <net/sctp/sctp.h>
61 #include <net/sctp/sm.h>
63 /* Forward declarations for internal helpers. */
64 static void sctp_endpoint_bh_rcv(struct sctp_endpoint *ep);
67 * Initialize the base fields of the endpoint structure.
69 static struct sctp_endpoint *sctp_endpoint_init(struct sctp_endpoint *ep,
70 struct sock *sk, int gfp)
72 struct sctp_sock *sp = sctp_sk(sk);
73 memset(ep, 0, sizeof(struct sctp_endpoint));
75 /* Initialize the base structure. */
76 /* What type of endpoint are we? */
77 ep->base.type = SCTP_EP_TYPE_SOCKET;
79 /* Initialize the basic object fields. */
80 atomic_set(&ep->base.refcnt, 1);
82 ep->base.malloced = 1;
84 /* Create an input queue. */
85 sctp_inq_init(&ep->base.inqueue);
87 /* Set its top-half handler */
88 sctp_inq_set_th_handler(&ep->base.inqueue,
89 (void (*)(void *))sctp_endpoint_bh_rcv, ep);
91 /* Initialize the bind addr area */
92 sctp_bind_addr_init(&ep->base.bind_addr, 0);
93 rwlock_init(&ep->base.addr_lock);
95 /* Remember who we are attached to. */
97 sock_hold(ep->base.sk);
99 /* Create the lists of associations. */
100 INIT_LIST_HEAD(&ep->asocs);
102 /* Set up the base timeout information. */
103 ep->timeouts[SCTP_EVENT_TIMEOUT_NONE] = 0;
104 ep->timeouts[SCTP_EVENT_TIMEOUT_T1_COOKIE] =
105 msecs_to_jiffies(sp->rtoinfo.srto_initial);
106 ep->timeouts[SCTP_EVENT_TIMEOUT_T1_INIT] =
107 msecs_to_jiffies(sp->rtoinfo.srto_initial);
108 ep->timeouts[SCTP_EVENT_TIMEOUT_T2_SHUTDOWN] =
109 msecs_to_jiffies(sp->rtoinfo.srto_initial);
110 ep->timeouts[SCTP_EVENT_TIMEOUT_T3_RTX] = 0;
111 ep->timeouts[SCTP_EVENT_TIMEOUT_T4_RTO] = 0;
113 /* sctpimpguide-05 Section 2.12.2
114 * If the 'T5-shutdown-guard' timer is used, it SHOULD be set to the
115 * recommended value of 5 times 'RTO.Max'.
117 ep->timeouts[SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD]
118 = 5 * msecs_to_jiffies(sp->rtoinfo.srto_max);
120 ep->timeouts[SCTP_EVENT_TIMEOUT_HEARTBEAT] = 0;
121 ep->timeouts[SCTP_EVENT_TIMEOUT_SACK] = sctp_sack_timeout;
122 ep->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE] = sp->autoclose * HZ;
124 /* Use SCTP specific send buffer space queues. */
125 ep->sndbuf_policy = sctp_sndbuf_policy;
126 sk->sk_write_space = sctp_write_space;
127 sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
129 /* Initialize the secret key used with cookie. */
130 get_random_bytes(&ep->secret_key[0], SCTP_SECRET_SIZE);
131 ep->last_key = ep->current_key = 0;
132 ep->key_changed_at = jiffies;
137 /* Create a sctp_endpoint with all that boring stuff initialized.
138 * Returns NULL if there isn't enough memory.
140 struct sctp_endpoint *sctp_endpoint_new(struct sock *sk, int gfp)
142 struct sctp_endpoint *ep;
144 /* Build a local endpoint. */
145 ep = t_new(struct sctp_endpoint, gfp);
148 if (!sctp_endpoint_init(ep, sk, gfp))
150 ep->base.malloced = 1;
151 SCTP_DBG_OBJCNT_INC(ep);
160 /* Add an association to an endpoint. */
161 void sctp_endpoint_add_asoc(struct sctp_endpoint *ep,
162 struct sctp_association *asoc)
164 struct sock *sk = ep->base.sk;
166 /* Now just add it to our list of asocs */
167 list_add_tail(&asoc->asocs, &ep->asocs);
169 /* Increment the backlog value for a TCP-style listening socket. */
170 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
171 sk->sk_ack_backlog++;
174 /* Free the endpoint structure. Delay cleanup until
175 * all users have released their reference count on this structure.
177 void sctp_endpoint_free(struct sctp_endpoint *ep)
180 sctp_endpoint_put(ep);
183 /* Final destructor for endpoint. */
184 static void sctp_endpoint_destroy(struct sctp_endpoint *ep)
186 SCTP_ASSERT(ep->base.dead, "Endpoint is not dead", return);
188 ep->base.sk->sk_state = SCTP_SS_CLOSED;
190 /* Unlink this endpoint, so we can't find it again! */
191 sctp_unhash_endpoint(ep);
193 /* Free up the HMAC transform. */
194 if (sctp_sk(ep->base.sk)->hmac)
195 sctp_crypto_free_tfm(sctp_sk(ep->base.sk)->hmac);
198 sctp_inq_free(&ep->base.inqueue);
199 sctp_bind_addr_free(&ep->base.bind_addr);
201 /* Remove and free the port */
202 if (sctp_sk(ep->base.sk)->bind_hash)
203 sctp_put_port(ep->base.sk);
205 /* Give up our hold on the sock. */
207 sock_put(ep->base.sk);
209 /* Finally, free up our memory. */
210 if (ep->base.malloced) {
212 SCTP_DBG_OBJCNT_DEC(ep);
216 /* Hold a reference to an endpoint. */
217 void sctp_endpoint_hold(struct sctp_endpoint *ep)
219 atomic_inc(&ep->base.refcnt);
222 /* Release a reference to an endpoint and clean up if there are
223 * no more references.
225 void sctp_endpoint_put(struct sctp_endpoint *ep)
227 if (atomic_dec_and_test(&ep->base.refcnt))
228 sctp_endpoint_destroy(ep);
231 /* Is this the endpoint we are looking for? */
232 struct sctp_endpoint *sctp_endpoint_is_match(struct sctp_endpoint *ep,
233 const union sctp_addr *laddr)
235 struct sctp_endpoint *retval;
237 sctp_read_lock(&ep->base.addr_lock);
238 if (ep->base.bind_addr.port == laddr->v4.sin_port) {
239 if (sctp_bind_addr_match(&ep->base.bind_addr, laddr,
240 sctp_sk(ep->base.sk))) {
249 sctp_read_unlock(&ep->base.addr_lock);
253 /* Find the association that goes with this chunk.
254 * We do a linear search of the associations for this endpoint.
255 * We return the matching transport address too.
257 static struct sctp_association *__sctp_endpoint_lookup_assoc(
258 const struct sctp_endpoint *ep,
259 const union sctp_addr *paddr,
260 struct sctp_transport **transport)
263 struct sctp_association *asoc;
264 struct list_head *pos;
266 rport = paddr->v4.sin_port;
268 list_for_each(pos, &ep->asocs) {
269 asoc = list_entry(pos, struct sctp_association, asocs);
270 if (rport == asoc->peer.port) {
271 sctp_read_lock(&asoc->base.addr_lock);
272 *transport = sctp_assoc_lookup_paddr(asoc, paddr);
273 sctp_read_unlock(&asoc->base.addr_lock);
284 /* Lookup association on an endpoint based on a peer address. BH-safe. */
285 struct sctp_association *sctp_endpoint_lookup_assoc(
286 const struct sctp_endpoint *ep,
287 const union sctp_addr *paddr,
288 struct sctp_transport **transport)
290 struct sctp_association *asoc;
292 sctp_local_bh_disable();
293 asoc = __sctp_endpoint_lookup_assoc(ep, paddr, transport);
294 sctp_local_bh_enable();
299 /* Look for any peeled off association from the endpoint that matches the
300 * given peer address.
302 int sctp_endpoint_is_peeled_off(struct sctp_endpoint *ep,
303 const union sctp_addr *paddr)
305 struct list_head *pos;
306 struct sctp_sockaddr_entry *addr;
307 struct sctp_bind_addr *bp;
309 sctp_read_lock(&ep->base.addr_lock);
310 bp = &ep->base.bind_addr;
311 list_for_each(pos, &bp->address_list) {
312 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
313 if (sctp_has_association(&addr->a, paddr)) {
314 sctp_read_unlock(&ep->base.addr_lock);
318 sctp_read_unlock(&ep->base.addr_lock);
323 /* Do delayed input processing. This is scheduled by sctp_rcv().
324 * This may be called on BH or task time.
326 static void sctp_endpoint_bh_rcv(struct sctp_endpoint *ep)
328 struct sctp_association *asoc;
330 struct sctp_transport *transport;
331 struct sctp_chunk *chunk;
332 struct sctp_inq *inqueue;
333 sctp_subtype_t subtype;
341 inqueue = &ep->base.inqueue;
344 while (NULL != (chunk = sctp_inq_pop(inqueue))) {
345 subtype = SCTP_ST_CHUNK(chunk->chunk_hdr->type);
347 /* We might have grown an association since last we
348 * looked, so try again.
350 * This happens when we've just processed our
353 if (NULL == chunk->asoc) {
354 asoc = sctp_endpoint_lookup_assoc(ep,
358 chunk->transport = transport;
361 state = asoc ? asoc->state : SCTP_STATE_CLOSED;
363 /* Remember where the last DATA chunk came from so we
364 * know where to send the SACK.
366 if (asoc && sctp_chunk_is_data(chunk))
367 asoc->peer.last_data_from = chunk->transport;
369 SCTP_INC_STATS(SCTP_MIB_INCTRLCHUNKS);
371 if (chunk->transport)
372 chunk->transport->last_time_heard = jiffies;
374 error = sctp_do_sm(SCTP_EVENT_T_CHUNK, subtype, state,
375 ep, asoc, chunk, GFP_ATOMIC);
380 /* Check to see if the endpoint is freed in response to
381 * the incoming chunk. If so, get out of the while loop.
383 if (!sctp_sk(sk)->ep)