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