Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
| 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. */ |
| 64 | static void sctp_endpoint_bh_rcv(struct sctp_endpoint *ep); |
| 65 | |
| 66 | /* |
| 67 | * Initialize the base fields of the endpoint structure. |
| 68 | */ |
| 69 | static struct sctp_endpoint *sctp_endpoint_init(struct sctp_endpoint *ep, |
Alexey Dobriyan | 3182cd8 | 2005-07-11 20:57:47 -0700 | [diff] [blame] | 70 | struct sock *sk, |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 71 | gfp_t gfp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | memset(ep, 0, sizeof(struct sctp_endpoint)); |
| 74 | |
| 75 | /* Initialize the base structure. */ |
| 76 | /* What type of endpoint are we? */ |
| 77 | ep->base.type = SCTP_EP_TYPE_SOCKET; |
| 78 | |
| 79 | /* Initialize the basic object fields. */ |
| 80 | atomic_set(&ep->base.refcnt, 1); |
| 81 | ep->base.dead = 0; |
| 82 | ep->base.malloced = 1; |
| 83 | |
| 84 | /* Create an input queue. */ |
| 85 | sctp_inq_init(&ep->base.inqueue); |
| 86 | |
| 87 | /* Set its top-half handler */ |
| 88 | sctp_inq_set_th_handler(&ep->base.inqueue, |
| 89 | (void (*)(void *))sctp_endpoint_bh_rcv, ep); |
| 90 | |
| 91 | /* Initialize the bind addr area */ |
| 92 | sctp_bind_addr_init(&ep->base.bind_addr, 0); |
| 93 | rwlock_init(&ep->base.addr_lock); |
| 94 | |
| 95 | /* Remember who we are attached to. */ |
| 96 | ep->base.sk = sk; |
| 97 | sock_hold(ep->base.sk); |
| 98 | |
| 99 | /* Create the lists of associations. */ |
| 100 | INIT_LIST_HEAD(&ep->asocs); |
| 101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | /* Use SCTP specific send buffer space queues. */ |
Neil Horman | 4eb701d | 2005-04-28 12:02:04 -0700 | [diff] [blame] | 103 | ep->sndbuf_policy = sctp_sndbuf_policy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | sk->sk_write_space = sctp_write_space; |
| 105 | sock_set_flag(sk, SOCK_USE_WRITE_QUEUE); |
| 106 | |
Neil Horman | 049b3ff | 2005-11-11 16:08:24 -0800 | [diff] [blame] | 107 | /* Get the receive buffer policy for this endpoint */ |
| 108 | ep->rcvbuf_policy = sctp_rcvbuf_policy; |
| 109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | /* Initialize the secret key used with cookie. */ |
| 111 | get_random_bytes(&ep->secret_key[0], SCTP_SECRET_SIZE); |
| 112 | ep->last_key = ep->current_key = 0; |
| 113 | ep->key_changed_at = jiffies; |
| 114 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | return ep; |
| 116 | } |
| 117 | |
| 118 | /* Create a sctp_endpoint with all that boring stuff initialized. |
| 119 | * Returns NULL if there isn't enough memory. |
| 120 | */ |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 121 | struct sctp_endpoint *sctp_endpoint_new(struct sock *sk, gfp_t gfp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | { |
| 123 | struct sctp_endpoint *ep; |
| 124 | |
| 125 | /* Build a local endpoint. */ |
| 126 | ep = t_new(struct sctp_endpoint, gfp); |
| 127 | if (!ep) |
| 128 | goto fail; |
| 129 | if (!sctp_endpoint_init(ep, sk, gfp)) |
| 130 | goto fail_init; |
| 131 | ep->base.malloced = 1; |
| 132 | SCTP_DBG_OBJCNT_INC(ep); |
| 133 | return ep; |
| 134 | |
| 135 | fail_init: |
| 136 | kfree(ep); |
| 137 | fail: |
| 138 | return NULL; |
| 139 | } |
| 140 | |
| 141 | /* Add an association to an endpoint. */ |
| 142 | void sctp_endpoint_add_asoc(struct sctp_endpoint *ep, |
| 143 | struct sctp_association *asoc) |
| 144 | { |
| 145 | struct sock *sk = ep->base.sk; |
| 146 | |
| 147 | /* Now just add it to our list of asocs */ |
| 148 | list_add_tail(&asoc->asocs, &ep->asocs); |
| 149 | |
| 150 | /* Increment the backlog value for a TCP-style listening socket. */ |
| 151 | if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) |
| 152 | sk->sk_ack_backlog++; |
| 153 | } |
| 154 | |
| 155 | /* Free the endpoint structure. Delay cleanup until |
| 156 | * all users have released their reference count on this structure. |
| 157 | */ |
| 158 | void sctp_endpoint_free(struct sctp_endpoint *ep) |
| 159 | { |
| 160 | ep->base.dead = 1; |
| 161 | sctp_endpoint_put(ep); |
| 162 | } |
| 163 | |
| 164 | /* Final destructor for endpoint. */ |
| 165 | static void sctp_endpoint_destroy(struct sctp_endpoint *ep) |
| 166 | { |
| 167 | SCTP_ASSERT(ep->base.dead, "Endpoint is not dead", return); |
| 168 | |
| 169 | ep->base.sk->sk_state = SCTP_SS_CLOSED; |
| 170 | |
| 171 | /* Unlink this endpoint, so we can't find it again! */ |
| 172 | sctp_unhash_endpoint(ep); |
| 173 | |
| 174 | /* Free up the HMAC transform. */ |
Jesper Juhl | 573dbd9 | 2005-09-01 17:44:29 -0700 | [diff] [blame] | 175 | sctp_crypto_free_tfm(sctp_sk(ep->base.sk)->hmac); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
| 177 | /* Cleanup. */ |
| 178 | sctp_inq_free(&ep->base.inqueue); |
| 179 | sctp_bind_addr_free(&ep->base.bind_addr); |
| 180 | |
| 181 | /* Remove and free the port */ |
| 182 | if (sctp_sk(ep->base.sk)->bind_hash) |
| 183 | sctp_put_port(ep->base.sk); |
| 184 | |
| 185 | /* Give up our hold on the sock. */ |
| 186 | if (ep->base.sk) |
| 187 | sock_put(ep->base.sk); |
| 188 | |
| 189 | /* Finally, free up our memory. */ |
| 190 | if (ep->base.malloced) { |
| 191 | kfree(ep); |
| 192 | SCTP_DBG_OBJCNT_DEC(ep); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | /* Hold a reference to an endpoint. */ |
| 197 | void sctp_endpoint_hold(struct sctp_endpoint *ep) |
| 198 | { |
| 199 | atomic_inc(&ep->base.refcnt); |
| 200 | } |
| 201 | |
| 202 | /* Release a reference to an endpoint and clean up if there are |
| 203 | * no more references. |
| 204 | */ |
| 205 | void sctp_endpoint_put(struct sctp_endpoint *ep) |
| 206 | { |
| 207 | if (atomic_dec_and_test(&ep->base.refcnt)) |
| 208 | sctp_endpoint_destroy(ep); |
| 209 | } |
| 210 | |
| 211 | /* Is this the endpoint we are looking for? */ |
| 212 | struct sctp_endpoint *sctp_endpoint_is_match(struct sctp_endpoint *ep, |
| 213 | const union sctp_addr *laddr) |
| 214 | { |
| 215 | struct sctp_endpoint *retval; |
| 216 | |
| 217 | sctp_read_lock(&ep->base.addr_lock); |
| 218 | if (ep->base.bind_addr.port == laddr->v4.sin_port) { |
| 219 | if (sctp_bind_addr_match(&ep->base.bind_addr, laddr, |
| 220 | sctp_sk(ep->base.sk))) { |
| 221 | retval = ep; |
| 222 | goto out; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | retval = NULL; |
| 227 | |
| 228 | out: |
| 229 | sctp_read_unlock(&ep->base.addr_lock); |
| 230 | return retval; |
| 231 | } |
| 232 | |
| 233 | /* Find the association that goes with this chunk. |
| 234 | * We do a linear search of the associations for this endpoint. |
| 235 | * We return the matching transport address too. |
| 236 | */ |
| 237 | static struct sctp_association *__sctp_endpoint_lookup_assoc( |
| 238 | const struct sctp_endpoint *ep, |
| 239 | const union sctp_addr *paddr, |
| 240 | struct sctp_transport **transport) |
| 241 | { |
| 242 | int rport; |
| 243 | struct sctp_association *asoc; |
| 244 | struct list_head *pos; |
| 245 | |
| 246 | rport = paddr->v4.sin_port; |
| 247 | |
| 248 | list_for_each(pos, &ep->asocs) { |
| 249 | asoc = list_entry(pos, struct sctp_association, asocs); |
| 250 | if (rport == asoc->peer.port) { |
| 251 | sctp_read_lock(&asoc->base.addr_lock); |
| 252 | *transport = sctp_assoc_lookup_paddr(asoc, paddr); |
| 253 | sctp_read_unlock(&asoc->base.addr_lock); |
| 254 | |
| 255 | if (*transport) |
| 256 | return asoc; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | *transport = NULL; |
| 261 | return NULL; |
| 262 | } |
| 263 | |
| 264 | /* Lookup association on an endpoint based on a peer address. BH-safe. */ |
| 265 | struct sctp_association *sctp_endpoint_lookup_assoc( |
| 266 | const struct sctp_endpoint *ep, |
| 267 | const union sctp_addr *paddr, |
| 268 | struct sctp_transport **transport) |
| 269 | { |
| 270 | struct sctp_association *asoc; |
| 271 | |
| 272 | sctp_local_bh_disable(); |
| 273 | asoc = __sctp_endpoint_lookup_assoc(ep, paddr, transport); |
| 274 | sctp_local_bh_enable(); |
| 275 | |
| 276 | return asoc; |
| 277 | } |
| 278 | |
| 279 | /* Look for any peeled off association from the endpoint that matches the |
| 280 | * given peer address. |
| 281 | */ |
| 282 | int sctp_endpoint_is_peeled_off(struct sctp_endpoint *ep, |
| 283 | const union sctp_addr *paddr) |
| 284 | { |
| 285 | struct list_head *pos; |
| 286 | struct sctp_sockaddr_entry *addr; |
| 287 | struct sctp_bind_addr *bp; |
| 288 | |
| 289 | sctp_read_lock(&ep->base.addr_lock); |
| 290 | bp = &ep->base.bind_addr; |
| 291 | list_for_each(pos, &bp->address_list) { |
| 292 | addr = list_entry(pos, struct sctp_sockaddr_entry, list); |
| 293 | if (sctp_has_association(&addr->a, paddr)) { |
| 294 | sctp_read_unlock(&ep->base.addr_lock); |
| 295 | return 1; |
| 296 | } |
| 297 | } |
| 298 | sctp_read_unlock(&ep->base.addr_lock); |
| 299 | |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | /* Do delayed input processing. This is scheduled by sctp_rcv(). |
| 304 | * This may be called on BH or task time. |
| 305 | */ |
| 306 | static void sctp_endpoint_bh_rcv(struct sctp_endpoint *ep) |
| 307 | { |
| 308 | struct sctp_association *asoc; |
| 309 | struct sock *sk; |
| 310 | struct sctp_transport *transport; |
| 311 | struct sctp_chunk *chunk; |
| 312 | struct sctp_inq *inqueue; |
| 313 | sctp_subtype_t subtype; |
| 314 | sctp_state_t state; |
| 315 | int error = 0; |
| 316 | |
| 317 | if (ep->base.dead) |
| 318 | return; |
| 319 | |
| 320 | asoc = NULL; |
| 321 | inqueue = &ep->base.inqueue; |
| 322 | sk = ep->base.sk; |
| 323 | |
| 324 | while (NULL != (chunk = sctp_inq_pop(inqueue))) { |
| 325 | subtype = SCTP_ST_CHUNK(chunk->chunk_hdr->type); |
| 326 | |
| 327 | /* We might have grown an association since last we |
| 328 | * looked, so try again. |
| 329 | * |
| 330 | * This happens when we've just processed our |
| 331 | * COOKIE-ECHO chunk. |
| 332 | */ |
| 333 | if (NULL == chunk->asoc) { |
| 334 | asoc = sctp_endpoint_lookup_assoc(ep, |
| 335 | sctp_source(chunk), |
| 336 | &transport); |
| 337 | chunk->asoc = asoc; |
| 338 | chunk->transport = transport; |
| 339 | } |
| 340 | |
| 341 | state = asoc ? asoc->state : SCTP_STATE_CLOSED; |
| 342 | |
| 343 | /* Remember where the last DATA chunk came from so we |
| 344 | * know where to send the SACK. |
| 345 | */ |
| 346 | if (asoc && sctp_chunk_is_data(chunk)) |
| 347 | asoc->peer.last_data_from = chunk->transport; |
| 348 | else |
| 349 | SCTP_INC_STATS(SCTP_MIB_INCTRLCHUNKS); |
| 350 | |
| 351 | if (chunk->transport) |
| 352 | chunk->transport->last_time_heard = jiffies; |
| 353 | |
| 354 | error = sctp_do_sm(SCTP_EVENT_T_CHUNK, subtype, state, |
| 355 | ep, asoc, chunk, GFP_ATOMIC); |
| 356 | |
| 357 | if (error && chunk) |
| 358 | chunk->pdiscard = 1; |
| 359 | |
| 360 | /* Check to see if the endpoint is freed in response to |
| 361 | * the incoming chunk. If so, get out of the while loop. |
| 362 | */ |
| 363 | if (!sctp_sk(sk)->ep) |
| 364 | break; |
| 365 | } |
| 366 | } |