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