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);
66 /* Create a sctp_endpoint with all that boring stuff initialized.
67 * Returns NULL if there isn't enough memory.
69 struct sctp_endpoint *sctp_endpoint_new(struct sock *sk, int gfp)
71 struct sctp_endpoint *ep;
73 /* Build a local endpoint. */
74 ep = t_new(struct sctp_endpoint, gfp);
77 if (!sctp_endpoint_init(ep, sk, gfp))
79 ep->base.malloced = 1;
80 SCTP_DBG_OBJCNT_INC(ep);
90 * Initialize the base fields of the endpoint structure.
92 struct sctp_endpoint *sctp_endpoint_init(struct sctp_endpoint *ep,
93 struct sock *sk, int gfp)
95 struct sctp_opt *sp = sctp_sk(sk);
96 memset(ep, 0, sizeof(struct sctp_endpoint));
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);
105 ep->base.malloced = 1;
107 /* Create an input queue. */
108 sctp_inq_init(&ep->base.inqueue);
110 /* Set its top-half handler */
111 sctp_inq_set_th_handler(&ep->base.inqueue,
112 (void (*)(void *))sctp_endpoint_bh_rcv, ep);
114 /* Initialize the bind addr area */
115 sctp_bind_addr_init(&ep->base.bind_addr, 0);
116 ep->base.addr_lock = RW_LOCK_UNLOCKED;
118 /* Remember who we are attached to. */
120 sock_hold(ep->base.sk);
122 /* Create the lists of associations. */
123 INIT_LIST_HEAD(&ep->asocs);
125 /* Set up the base timeout information. */
126 ep->timeouts[SCTP_EVENT_TIMEOUT_NONE] = 0;
127 ep->timeouts[SCTP_EVENT_TIMEOUT_T1_COOKIE] =
128 SCTP_DEFAULT_TIMEOUT_T1_COOKIE;
129 ep->timeouts[SCTP_EVENT_TIMEOUT_T1_INIT] =
130 SCTP_DEFAULT_TIMEOUT_T1_INIT;
131 ep->timeouts[SCTP_EVENT_TIMEOUT_T2_SHUTDOWN] =
132 sp->rtoinfo.srto_initial * HZ / 1000;
133 ep->timeouts[SCTP_EVENT_TIMEOUT_T3_RTX] = 0;
134 ep->timeouts[SCTP_EVENT_TIMEOUT_T4_RTO] = 0;
136 /* sctpimpguide-05 Section 2.12.2
137 * If the 'T5-shutdown-guard' timer is used, it SHOULD be set to the
138 * recommended value of 5 times 'RTO.Max'.
140 ep->timeouts[SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD]
141 = 5 * (sp->rtoinfo.srto_max * HZ / 1000);
143 ep->timeouts[SCTP_EVENT_TIMEOUT_HEARTBEAT] =
144 SCTP_DEFAULT_TIMEOUT_HEARTBEAT;
145 ep->timeouts[SCTP_EVENT_TIMEOUT_SACK] =
146 SCTP_DEFAULT_TIMEOUT_SACK;
147 ep->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE] =
150 /* Set up the default send/receive buffer space. */
152 /* FIXME - Should the min and max window size be configurable
153 * sysctl parameters as opposed to be constants?
155 sk->sk_rcvbuf = SCTP_DEFAULT_MAXWINDOW;
156 sk->sk_sndbuf = SCTP_DEFAULT_MAXWINDOW * 2;
158 /* Use SCTP specific send buffer space queues. */
159 sk->sk_write_space = sctp_write_space;
160 sk->sk_use_write_queue = 1;
162 /* Initialize the secret key used with cookie. */
163 get_random_bytes(&ep->secret_key[0], SCTP_SECRET_SIZE);
164 ep->last_key = ep->current_key = 0;
165 ep->key_changed_at = jiffies;
167 ep->debug_name = "unnamedEndpoint";
171 /* Add an association to an endpoint. */
172 void sctp_endpoint_add_asoc(struct sctp_endpoint *ep,
173 struct sctp_association *asoc)
175 struct sock *sk = ep->base.sk;
177 /* Now just add it to our list of asocs */
178 list_add_tail(&asoc->asocs, &ep->asocs);
180 /* Increment the backlog value for a TCP-style listening socket. */
181 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
182 sk->sk_ack_backlog++;
185 /* Free the endpoint structure. Delay cleanup until
186 * all users have released their reference count on this structure.
188 void sctp_endpoint_free(struct sctp_endpoint *ep)
191 sctp_endpoint_put(ep);
194 /* Final destructor for endpoint. */
195 void sctp_endpoint_destroy(struct sctp_endpoint *ep)
197 SCTP_ASSERT(ep->base.dead, "Endpoint is not dead", return);
199 ep->base.sk->sk_state = SCTP_SS_CLOSED;
201 /* Unlink this endpoint, so we can't find it again! */
202 sctp_unhash_endpoint(ep);
204 /* Free up the HMAC transform. */
205 if (sctp_sk(ep->base.sk)->hmac)
206 sctp_crypto_free_tfm(sctp_sk(ep->base.sk)->hmac);
209 sctp_inq_free(&ep->base.inqueue);
210 sctp_bind_addr_free(&ep->base.bind_addr);
212 /* Remove and free the port */
213 if (sctp_sk(ep->base.sk)->bind_hash)
214 sctp_put_port(ep->base.sk);
216 /* Give up our hold on the sock. */
218 sock_put(ep->base.sk);
220 /* Finally, free up our memory. */
221 if (ep->base.malloced) {
223 SCTP_DBG_OBJCNT_DEC(ep);
227 /* Hold a reference to an endpoint. */
228 void sctp_endpoint_hold(struct sctp_endpoint *ep)
230 atomic_inc(&ep->base.refcnt);
233 /* Release a reference to an endpoint and clean up if there are
234 * no more references.
236 void sctp_endpoint_put(struct sctp_endpoint *ep)
238 if (atomic_dec_and_test(&ep->base.refcnt))
239 sctp_endpoint_destroy(ep);
242 /* Is this the endpoint we are looking for? */
243 struct sctp_endpoint *sctp_endpoint_is_match(struct sctp_endpoint *ep,
244 const union sctp_addr *laddr)
246 struct sctp_endpoint *retval;
248 sctp_read_lock(&ep->base.addr_lock);
249 if (ep->base.bind_addr.port == laddr->v4.sin_port) {
250 if (sctp_bind_addr_match(&ep->base.bind_addr, laddr,
251 sctp_sk(ep->base.sk))) {
260 sctp_read_unlock(&ep->base.addr_lock);
264 /* Find the association that goes with this chunk.
265 * We do a linear search of the associations for this endpoint.
266 * We return the matching transport address too.
268 struct sctp_association *__sctp_endpoint_lookup_assoc(
269 const struct sctp_endpoint *ep,
270 const union sctp_addr *paddr,
271 struct sctp_transport **transport)
274 struct sctp_association *asoc;
275 struct list_head *pos;
277 rport = paddr->v4.sin_port;
279 list_for_each(pos, &ep->asocs) {
280 asoc = list_entry(pos, struct sctp_association, asocs);
281 if (rport == asoc->peer.port) {
282 sctp_read_lock(&asoc->base.addr_lock);
283 *transport = sctp_assoc_lookup_paddr(asoc, paddr);
284 sctp_read_unlock(&asoc->base.addr_lock);
295 /* Lookup association on an endpoint based on a peer address. BH-safe. */
296 struct sctp_association *sctp_endpoint_lookup_assoc(
297 const struct sctp_endpoint *ep,
298 const union sctp_addr *paddr,
299 struct sctp_transport **transport)
301 struct sctp_association *asoc;
303 sctp_local_bh_disable();
304 asoc = __sctp_endpoint_lookup_assoc(ep, paddr, transport);
305 sctp_local_bh_enable();
310 /* Look for any peeled off association from the endpoint that matches the
311 * given peer address.
313 int sctp_endpoint_is_peeled_off(struct sctp_endpoint *ep,
314 const union sctp_addr *paddr)
316 struct list_head *pos;
317 struct sctp_sockaddr_entry *addr;
318 struct sctp_bind_addr *bp;
320 sctp_read_lock(&ep->base.addr_lock);
321 bp = &ep->base.bind_addr;
322 list_for_each(pos, &bp->address_list) {
323 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
324 if (sctp_has_association(&addr->a, paddr)) {
325 sctp_read_unlock(&ep->base.addr_lock);
329 sctp_read_unlock(&ep->base.addr_lock);
334 /* Do delayed input processing. This is scheduled by sctp_rcv().
335 * This may be called on BH or task time.
337 static void sctp_endpoint_bh_rcv(struct sctp_endpoint *ep)
339 struct sctp_association *asoc;
341 struct sctp_transport *transport;
342 struct sctp_chunk *chunk;
343 struct sctp_inq *inqueue;
344 sctp_subtype_t subtype;
352 inqueue = &ep->base.inqueue;
355 while (NULL != (chunk = sctp_inq_pop(inqueue))) {
356 subtype.chunk = chunk->chunk_hdr->type;
358 /* We might have grown an association since last we
359 * looked, so try again.
361 * This happens when we've just processed our
364 if (NULL == chunk->asoc) {
365 asoc = sctp_endpoint_lookup_assoc(ep,
369 chunk->transport = transport;
372 state = asoc ? asoc->state : SCTP_STATE_CLOSED;
374 /* Remember where the last DATA chunk came from so we
375 * know where to send the SACK.
377 if (asoc && sctp_chunk_is_data(chunk))
378 asoc->peer.last_data_from = chunk->transport;
380 SCTP_INC_STATS(SctpInCtrlChunks);
382 if (chunk->transport)
383 chunk->transport->last_time_heard = jiffies;
385 error = sctp_do_sm(SCTP_EVENT_T_CHUNK, subtype, state,
386 ep, asoc, chunk, GFP_ATOMIC);
391 /* Check to see if the endpoint is freed in response to
392 * the incoming chunk. If so, get out of the while loop.
394 if (!sctp_sk(sk)->ep)