1 /* SCTP kernel reference Implementation 2 * (C) Copyright IBM Corp. 2001, 2004 3 * Copyright (c) 1999-2000 Cisco, Inc. 4 * Copyright (c) 1999-2001 Motorola, Inc. 5 * Copyright (c) 2001 Intel Corp. 6 * Copyright (c) 2001 La Monte H.P. Yarroll 7 * 8 * This file is part of the SCTP kernel reference Implementation 9 * 10 * This module provides the abstraction for an SCTP association. 11 * 12 * The SCTP reference implementation is free software; 13 * you can redistribute it and/or modify it under the terms of 14 * the GNU General Public License as published by 15 * the Free Software Foundation; either version 2, or (at your option) 16 * any later version. 17 * 18 * The SCTP reference implementation is distributed in the hope that it 19 * will be useful, but WITHOUT ANY WARRANTY; without even the implied 20 * ************************ 21 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 22 * See the GNU General Public License for more details. 23 * 24 * You should have received a copy of the GNU General Public License 25 * along with GNU CC; see the file COPYING. If not, write to 26 * the Free Software Foundation, 59 Temple Place - Suite 330, 27 * Boston, MA 02111-1307, USA. 28 * 29 * Please send any bug reports or fixes you make to the 30 * email address(es): 31 * lksctp developers <lksctp-developers@lists.sourceforge.net> 32 * 33 * Or submit a bug report through the following website: 34 * http://www.sf.net/projects/lksctp 35 * 36 * Written or modified by: 37 * La Monte H.P. Yarroll <piggy@acm.org> 38 * Karl Knutson <karl@athena.chicago.il.us> 39 * Jon Grimm <jgrimm@us.ibm.com> 40 * Xingang Guo <xingang.guo@intel.com> 41 * Hui Huang <hui.huang@nokia.com> 42 * Sridhar Samudrala <sri@us.ibm.com> 43 * Daisy Chang <daisyc@us.ibm.com> 44 * Ryan Layer <rmlayer@us.ibm.com> 45 * Kevin Gao <kevin.gao@intel.com> 46 * 47 * Any bugs reported given to us we will try to fix... any fixes shared will 48 * be incorporated into the next SCTP release. 49 */ 50 51 #include <linux/types.h> 52 #include <linux/fcntl.h> 53 #include <linux/poll.h> 54 #include <linux/init.h> 55 #include <linux/sched.h> 56 57 #include <linux/slab.h> 58 #include <linux/in.h> 59 #include <net/ipv6.h> 60 #include <net/sctp/sctp.h> 61 #include <net/sctp/sm.h> 62 63 /* Forward declarations for internal functions. */ 64 static void sctp_assoc_bh_rcv(struct sctp_association *asoc); 65 66 67 /* 1st Level Abstractions. */ 68 69 /* Initialize a new association from provided memory. */ 70 static struct sctp_association *sctp_association_init(struct sctp_association *asoc, 71 const struct sctp_endpoint *ep, 72 const struct sock *sk, 73 sctp_scope_t scope, 74 gfp_t gfp) 75 { 76 struct sctp_sock *sp; 77 int i; 78 79 /* Retrieve the SCTP per socket area. */ 80 sp = sctp_sk((struct sock *)sk); 81 82 /* Init all variables to a known value. */ 83 memset(asoc, 0, sizeof(struct sctp_association)); 84 85 /* Discarding const is appropriate here. */ 86 asoc->ep = (struct sctp_endpoint *)ep; 87 sctp_endpoint_hold(asoc->ep); 88 89 /* Hold the sock. */ 90 asoc->base.sk = (struct sock *)sk; 91 sock_hold(asoc->base.sk); 92 93 /* Initialize the common base substructure. */ 94 asoc->base.type = SCTP_EP_TYPE_ASSOCIATION; 95 96 /* Initialize the object handling fields. */ 97 atomic_set(&asoc->base.refcnt, 1); 98 asoc->base.dead = 0; 99 asoc->base.malloced = 0; 100 101 /* Initialize the bind addr area. */ 102 sctp_bind_addr_init(&asoc->base.bind_addr, ep->base.bind_addr.port); 103 rwlock_init(&asoc->base.addr_lock); 104 105 asoc->state = SCTP_STATE_CLOSED; 106 107 /* Set these values from the socket values, a conversion between 108 * millsecons to seconds/microseconds must also be done. 109 */ 110 asoc->cookie_life.tv_sec = sp->assocparams.sasoc_cookie_life / 1000; 111 asoc->cookie_life.tv_usec = (sp->assocparams.sasoc_cookie_life % 1000) 112 * 1000; 113 asoc->pmtu = 0; 114 asoc->frag_point = 0; 115 116 /* Set the association max_retrans and RTO values from the 117 * socket values. 118 */ 119 asoc->max_retrans = sp->assocparams.sasoc_asocmaxrxt; 120 asoc->rto_initial = msecs_to_jiffies(sp->rtoinfo.srto_initial); 121 asoc->rto_max = msecs_to_jiffies(sp->rtoinfo.srto_max); 122 asoc->rto_min = msecs_to_jiffies(sp->rtoinfo.srto_min); 123 124 asoc->overall_error_count = 0; 125 126 /* Initialize the maximum mumber of new data packets that can be sent 127 * in a burst. 128 */ 129 asoc->max_burst = sctp_max_burst; 130 131 /* initialize association timers */ 132 asoc->timeouts[SCTP_EVENT_TIMEOUT_NONE] = 0; 133 asoc->timeouts[SCTP_EVENT_TIMEOUT_T1_COOKIE] = asoc->rto_initial; 134 asoc->timeouts[SCTP_EVENT_TIMEOUT_T1_INIT] = asoc->rto_initial; 135 asoc->timeouts[SCTP_EVENT_TIMEOUT_T2_SHUTDOWN] = asoc->rto_initial; 136 asoc->timeouts[SCTP_EVENT_TIMEOUT_T3_RTX] = 0; 137 asoc->timeouts[SCTP_EVENT_TIMEOUT_T4_RTO] = 0; 138 139 /* sctpimpguide Section 2.12.2 140 * If the 'T5-shutdown-guard' timer is used, it SHOULD be set to the 141 * recommended value of 5 times 'RTO.Max'. 142 */ 143 asoc->timeouts[SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD] 144 = 5 * asoc->rto_max; 145 146 asoc->timeouts[SCTP_EVENT_TIMEOUT_HEARTBEAT] = 0; 147 asoc->timeouts[SCTP_EVENT_TIMEOUT_SACK] = 148 SCTP_DEFAULT_TIMEOUT_SACK; 149 asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE] = 150 sp->autoclose * HZ; 151 152 /* Initilizes the timers */ 153 for (i = SCTP_EVENT_TIMEOUT_NONE; i < SCTP_NUM_TIMEOUT_TYPES; ++i) { 154 init_timer(&asoc->timers[i]); 155 asoc->timers[i].function = sctp_timer_events[i]; 156 asoc->timers[i].data = (unsigned long) asoc; 157 } 158 159 /* Pull default initialization values from the sock options. 160 * Note: This assumes that the values have already been 161 * validated in the sock. 162 */ 163 asoc->c.sinit_max_instreams = sp->initmsg.sinit_max_instreams; 164 asoc->c.sinit_num_ostreams = sp->initmsg.sinit_num_ostreams; 165 asoc->max_init_attempts = sp->initmsg.sinit_max_attempts; 166 167 asoc->max_init_timeo = 168 msecs_to_jiffies(sp->initmsg.sinit_max_init_timeo); 169 170 /* Allocate storage for the ssnmap after the inbound and outbound 171 * streams have been negotiated during Init. 172 */ 173 asoc->ssnmap = NULL; 174 175 /* Set the local window size for receive. 176 * This is also the rcvbuf space per association. 177 * RFC 6 - A SCTP receiver MUST be able to receive a minimum of 178 * 1500 bytes in one SCTP packet. 179 */ 180 if ((sk->sk_rcvbuf/2) < SCTP_DEFAULT_MINWINDOW) 181 asoc->rwnd = SCTP_DEFAULT_MINWINDOW; 182 else 183 asoc->rwnd = sk->sk_rcvbuf/2; 184 185 asoc->a_rwnd = asoc->rwnd; 186 187 asoc->rwnd_over = 0; 188 189 /* Use my own max window until I learn something better. */ 190 asoc->peer.rwnd = SCTP_DEFAULT_MAXWINDOW; 191 192 /* Set the sndbuf size for transmit. */ 193 asoc->sndbuf_used = 0; 194 195 /* Initialize the receive memory counter */ 196 atomic_set(&asoc->rmem_alloc, 0); 197 198 init_waitqueue_head(&asoc->wait); 199 200 asoc->c.my_vtag = sctp_generate_tag(ep); 201 asoc->peer.i.init_tag = 0; /* INIT needs a vtag of 0. */ 202 asoc->c.peer_vtag = 0; 203 asoc->c.my_ttag = 0; 204 asoc->c.peer_ttag = 0; 205 asoc->c.my_port = ep->base.bind_addr.port; 206 207 asoc->c.initial_tsn = sctp_generate_tsn(ep); 208 209 asoc->next_tsn = asoc->c.initial_tsn; 210 211 asoc->ctsn_ack_point = asoc->next_tsn - 1; 212 asoc->adv_peer_ack_point = asoc->ctsn_ack_point; 213 asoc->highest_sacked = asoc->ctsn_ack_point; 214 asoc->last_cwr_tsn = asoc->ctsn_ack_point; 215 asoc->unack_data = 0; 216 217 /* ADDIP Section 4.1 Asconf Chunk Procedures 218 * 219 * When an endpoint has an ASCONF signaled change to be sent to the 220 * remote endpoint it should do the following: 221 * ... 222 * A2) a serial number should be assigned to the chunk. The serial 223 * number SHOULD be a monotonically increasing number. The serial 224 * numbers SHOULD be initialized at the start of the 225 * association to the same value as the initial TSN. 226 */ 227 asoc->addip_serial = asoc->c.initial_tsn; 228 229 INIT_LIST_HEAD(&asoc->addip_chunk_list); 230 231 /* Make an empty list of remote transport addresses. */ 232 INIT_LIST_HEAD(&asoc->peer.transport_addr_list); 233 asoc->peer.transport_count = 0; 234 235 /* RFC 2960 5.1 Normal Establishment of an Association 236 * 237 * After the reception of the first data chunk in an 238 * association the endpoint must immediately respond with a 239 * sack to acknowledge the data chunk. Subsequent 240 * acknowledgements should be done as described in Section 241 * 6.2. 242 * 243 * [We implement this by telling a new association that it 244 * already received one packet.] 245 */ 246 asoc->peer.sack_needed = 1; 247 248 /* Assume that the peer recongizes ASCONF until reported otherwise 249 * via an ERROR chunk. 250 */ 251 asoc->peer.asconf_capable = 1; 252 253 /* Create an input queue. */ 254 sctp_inq_init(&asoc->base.inqueue); 255 sctp_inq_set_th_handler(&asoc->base.inqueue, 256 (void (*)(void *))sctp_assoc_bh_rcv, 257 asoc); 258 259 /* Create an output queue. */ 260 sctp_outq_init(asoc, &asoc->outqueue); 261 262 if (!sctp_ulpq_init(&asoc->ulpq, asoc)) 263 goto fail_init; 264 265 /* Set up the tsn tracking. */ 266 sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_SIZE, 0); 267 268 asoc->need_ecne = 0; 269 270 asoc->assoc_id = 0; 271 272 /* Assume that peer would support both address types unless we are 273 * told otherwise. 274 */ 275 asoc->peer.ipv4_address = 1; 276 asoc->peer.ipv6_address = 1; 277 INIT_LIST_HEAD(&asoc->asocs); 278 279 asoc->autoclose = sp->autoclose; 280 281 asoc->default_stream = sp->default_stream; 282 asoc->default_ppid = sp->default_ppid; 283 asoc->default_flags = sp->default_flags; 284 asoc->default_context = sp->default_context; 285 asoc->default_timetolive = sp->default_timetolive; 286 287 return asoc; 288 289 fail_init: 290 sctp_endpoint_put(asoc->ep); 291 sock_put(asoc->base.sk); 292 return NULL; 293 } 294 295 /* Allocate and initialize a new association */ 296 struct sctp_association *sctp_association_new(const struct sctp_endpoint *ep, 297 const struct sock *sk, 298 sctp_scope_t scope, 299 gfp_t gfp) 300 { 301 struct sctp_association *asoc; 302 303 asoc = t_new(struct sctp_association, gfp); 304 if (!asoc) 305 goto fail; 306 307 if (!sctp_association_init(asoc, ep, sk, scope, gfp)) 308 goto fail_init; 309 310 asoc->base.malloced = 1; 311 SCTP_DBG_OBJCNT_INC(assoc); 312 SCTP_DEBUG_PRINTK("Created asoc %p\n", asoc); 313 314 return asoc; 315 316 fail_init: 317 kfree(asoc); 318 fail: 319 return NULL; 320 } 321 322 /* Free this association if possible. There may still be users, so 323 * the actual deallocation may be delayed. 324 */ 325 void sctp_association_free(struct sctp_association *asoc) 326 { 327 struct sock *sk = asoc->base.sk; 328 struct sctp_transport *transport; 329 struct list_head *pos, *temp; 330 int i; 331 332 list_del(&asoc->asocs); 333 334 /* Decrement the backlog value for a TCP-style listening socket. */ 335 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) 336 sk->sk_ack_backlog--; 337 338 /* Mark as dead, so other users can know this structure is 339 * going away. 340 */ 341 asoc->base.dead = 1; 342 343 /* Dispose of any data lying around in the outqueue. */ 344 sctp_outq_free(&asoc->outqueue); 345 346 /* Dispose of any pending messages for the upper layer. */ 347 sctp_ulpq_free(&asoc->ulpq); 348 349 /* Dispose of any pending chunks on the inqueue. */ 350 sctp_inq_free(&asoc->base.inqueue); 351 352 /* Free ssnmap storage. */ 353 sctp_ssnmap_free(asoc->ssnmap); 354 355 /* Clean up the bound address list. */ 356 sctp_bind_addr_free(&asoc->base.bind_addr); 357 358 /* Do we need to go through all of our timers and 359 * delete them? To be safe we will try to delete all, but we 360 * should be able to go through and make a guess based 361 * on our state. 362 */ 363 for (i = SCTP_EVENT_TIMEOUT_NONE; i < SCTP_NUM_TIMEOUT_TYPES; ++i) { 364 if (timer_pending(&asoc->timers[i]) && 365 del_timer(&asoc->timers[i])) 366 sctp_association_put(asoc); 367 } 368 369 /* Free peer's cached cookie. */ 370 kfree(asoc->peer.cookie); 371 372 /* Release the transport structures. */ 373 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) { 374 transport = list_entry(pos, struct sctp_transport, transports); 375 list_del(pos); 376 sctp_transport_free(transport); 377 } 378 379 asoc->peer.transport_count = 0; 380 381 /* Free any cached ASCONF_ACK chunk. */ 382 if (asoc->addip_last_asconf_ack) 383 sctp_chunk_free(asoc->addip_last_asconf_ack); 384 385 /* Free any cached ASCONF chunk. */ 386 if (asoc->addip_last_asconf) 387 sctp_chunk_free(asoc->addip_last_asconf); 388 389 sctp_association_put(asoc); 390 } 391 392 /* Cleanup and free up an association. */ 393 static void sctp_association_destroy(struct sctp_association *asoc) 394 { 395 SCTP_ASSERT(asoc->base.dead, "Assoc is not dead", return); 396 397 sctp_endpoint_put(asoc->ep); 398 sock_put(asoc->base.sk); 399 400 if (asoc->assoc_id != 0) { 401 spin_lock_bh(&sctp_assocs_id_lock); 402 idr_remove(&sctp_assocs_id, asoc->assoc_id); 403 spin_unlock_bh(&sctp_assocs_id_lock); 404 } 405 406 BUG_TRAP(!atomic_read(&asoc->rmem_alloc)); 407 408 if (asoc->base.malloced) { 409 kfree(asoc); 410 SCTP_DBG_OBJCNT_DEC(assoc); 411 } 412 } 413 414 /* Change the primary destination address for the peer. */ 415 void sctp_assoc_set_primary(struct sctp_association *asoc, 416 struct sctp_transport *transport) 417 { 418 asoc->peer.primary_path = transport; 419 420 /* Set a default msg_name for events. */ 421 memcpy(&asoc->peer.primary_addr, &transport->ipaddr, 422 sizeof(union sctp_addr)); 423 424 /* If the primary path is changing, assume that the 425 * user wants to use this new path. 426 */ 427 if (transport->state != SCTP_INACTIVE) 428 asoc->peer.active_path = transport; 429 430 /* 431 * SFR-CACC algorithm: 432 * Upon the receipt of a request to change the primary 433 * destination address, on the data structure for the new 434 * primary destination, the sender MUST do the following: 435 * 436 * 1) If CHANGEOVER_ACTIVE is set, then there was a switch 437 * to this destination address earlier. The sender MUST set 438 * CYCLING_CHANGEOVER to indicate that this switch is a 439 * double switch to the same destination address. 440 */ 441 if (transport->cacc.changeover_active) 442 transport->cacc.cycling_changeover = 1; 443 444 /* 2) The sender MUST set CHANGEOVER_ACTIVE to indicate that 445 * a changeover has occurred. 446 */ 447 transport->cacc.changeover_active = 1; 448 449 /* 3) The sender MUST store the next TSN to be sent in 450 * next_tsn_at_change. 451 */ 452 transport->cacc.next_tsn_at_change = asoc->next_tsn; 453 } 454 455 /* Remove a transport from an association. */ 456 void sctp_assoc_rm_peer(struct sctp_association *asoc, 457 struct sctp_transport *peer) 458 { 459 struct list_head *pos; 460 struct sctp_transport *transport; 461 462 SCTP_DEBUG_PRINTK_IPADDR("sctp_assoc_rm_peer:association %p addr: ", 463 " port: %d\n", 464 asoc, 465 (&peer->ipaddr), 466 peer->ipaddr.v4.sin_port); 467 468 /* If we are to remove the current retran_path, update it 469 * to the next peer before removing this peer from the list. 470 */ 471 if (asoc->peer.retran_path == peer) 472 sctp_assoc_update_retran_path(asoc); 473 474 /* Remove this peer from the list. */ 475 list_del(&peer->transports); 476 477 /* Get the first transport of asoc. */ 478 pos = asoc->peer.transport_addr_list.next; 479 transport = list_entry(pos, struct sctp_transport, transports); 480 481 /* Update any entries that match the peer to be deleted. */ 482 if (asoc->peer.primary_path == peer) 483 sctp_assoc_set_primary(asoc, transport); 484 if (asoc->peer.active_path == peer) 485 asoc->peer.active_path = transport; 486 if (asoc->peer.last_data_from == peer) 487 asoc->peer.last_data_from = transport; 488 489 /* If we remove the transport an INIT was last sent to, set it to 490 * NULL. Combined with the update of the retran path above, this 491 * will cause the next INIT to be sent to the next available 492 * transport, maintaining the cycle. 493 */ 494 if (asoc->init_last_sent_to == peer) 495 asoc->init_last_sent_to = NULL; 496 497 asoc->peer.transport_count--; 498 499 sctp_transport_free(peer); 500 } 501 502 /* Add a transport address to an association. */ 503 struct sctp_transport *sctp_assoc_add_peer(struct sctp_association *asoc, 504 const union sctp_addr *addr, 505 const gfp_t gfp, 506 const int peer_state) 507 { 508 struct sctp_transport *peer; 509 struct sctp_sock *sp; 510 unsigned short port; 511 512 sp = sctp_sk(asoc->base.sk); 513 514 /* AF_INET and AF_INET6 share common port field. */ 515 port = addr->v4.sin_port; 516 517 SCTP_DEBUG_PRINTK_IPADDR("sctp_assoc_add_peer:association %p addr: ", 518 " port: %d state:%s\n", 519 asoc, 520 addr, 521 addr->v4.sin_port, 522 peer_state == SCTP_UNKNOWN?"UNKNOWN":"ACTIVE"); 523 524 /* Set the port if it has not been set yet. */ 525 if (0 == asoc->peer.port) 526 asoc->peer.port = port; 527 528 /* Check to see if this is a duplicate. */ 529 peer = sctp_assoc_lookup_paddr(asoc, addr); 530 if (peer) { 531 if (peer_state == SCTP_ACTIVE && 532 peer->state == SCTP_UNKNOWN) 533 peer->state = SCTP_ACTIVE; 534 return peer; 535 } 536 537 peer = sctp_transport_new(addr, gfp); 538 if (!peer) 539 return NULL; 540 541 sctp_transport_set_owner(peer, asoc); 542 543 /* Initialize the pmtu of the transport. */ 544 sctp_transport_pmtu(peer); 545 546 /* If this is the first transport addr on this association, 547 * initialize the association PMTU to the peer's PMTU. 548 * If not and the current association PMTU is higher than the new 549 * peer's PMTU, reset the association PMTU to the new peer's PMTU. 550 */ 551 if (asoc->pmtu) 552 asoc->pmtu = min_t(int, peer->pmtu, asoc->pmtu); 553 else 554 asoc->pmtu = peer->pmtu; 555 556 SCTP_DEBUG_PRINTK("sctp_assoc_add_peer:association %p PMTU set to " 557 "%d\n", asoc, asoc->pmtu); 558 559 asoc->frag_point = sctp_frag_point(sp, asoc->pmtu); 560 561 /* The asoc->peer.port might not be meaningful yet, but 562 * initialize the packet structure anyway. 563 */ 564 sctp_packet_init(&peer->packet, peer, asoc->base.bind_addr.port, 565 asoc->peer.port); 566 567 /* 7.2.1 Slow-Start 568 * 569 * o The initial cwnd before DATA transmission or after a sufficiently 570 * long idle period MUST be set to 571 * min(4*MTU, max(2*MTU, 4380 bytes)) 572 * 573 * o The initial value of ssthresh MAY be arbitrarily high 574 * (for example, implementations MAY use the size of the 575 * receiver advertised window). 576 */ 577 peer->cwnd = min(4*asoc->pmtu, max_t(__u32, 2*asoc->pmtu, 4380)); 578 579 /* At this point, we may not have the receiver's advertised window, 580 * so initialize ssthresh to the default value and it will be set 581 * later when we process the INIT. 582 */ 583 peer->ssthresh = SCTP_DEFAULT_MAXWINDOW; 584 585 peer->partial_bytes_acked = 0; 586 peer->flight_size = 0; 587 588 /* By default, enable heartbeat for peer address. */ 589 peer->hb_allowed = 1; 590 591 /* Initialize the peer's heartbeat interval based on the 592 * sock configured value. 593 */ 594 peer->hb_interval = msecs_to_jiffies(sp->paddrparam.spp_hbinterval); 595 596 /* Set the path max_retrans. */ 597 peer->max_retrans = sp->paddrparam.spp_pathmaxrxt; 598 599 /* Set the transport's RTO.initial value */ 600 peer->rto = asoc->rto_initial; 601 602 /* Set the peer's active state. */ 603 peer->state = peer_state; 604 605 /* Attach the remote transport to our asoc. */ 606 list_add_tail(&peer->transports, &asoc->peer.transport_addr_list); 607 asoc->peer.transport_count++; 608 609 /* If we do not yet have a primary path, set one. */ 610 if (!asoc->peer.primary_path) { 611 sctp_assoc_set_primary(asoc, peer); 612 asoc->peer.retran_path = peer; 613 } 614 615 if (asoc->peer.active_path == asoc->peer.retran_path) { 616 asoc->peer.retran_path = peer; 617 } 618 619 return peer; 620 } 621 622 /* Delete a transport address from an association. */ 623 void sctp_assoc_del_peer(struct sctp_association *asoc, 624 const union sctp_addr *addr) 625 { 626 struct list_head *pos; 627 struct list_head *temp; 628 struct sctp_transport *transport; 629 630 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) { 631 transport = list_entry(pos, struct sctp_transport, transports); 632 if (sctp_cmp_addr_exact(addr, &transport->ipaddr)) { 633 /* Do book keeping for removing the peer and free it. */ 634 sctp_assoc_rm_peer(asoc, transport); 635 break; 636 } 637 } 638 } 639 640 /* Lookup a transport by address. */ 641 struct sctp_transport *sctp_assoc_lookup_paddr( 642 const struct sctp_association *asoc, 643 const union sctp_addr *address) 644 { 645 struct sctp_transport *t; 646 struct list_head *pos; 647 648 /* Cycle through all transports searching for a peer address. */ 649 650 list_for_each(pos, &asoc->peer.transport_addr_list) { 651 t = list_entry(pos, struct sctp_transport, transports); 652 if (sctp_cmp_addr_exact(address, &t->ipaddr)) 653 return t; 654 } 655 656 return NULL; 657 } 658 659 /* Engage in transport control operations. 660 * Mark the transport up or down and send a notification to the user. 661 * Select and update the new active and retran paths. 662 */ 663 void sctp_assoc_control_transport(struct sctp_association *asoc, 664 struct sctp_transport *transport, 665 sctp_transport_cmd_t command, 666 sctp_sn_error_t error) 667 { 668 struct sctp_transport *t = NULL; 669 struct sctp_transport *first; 670 struct sctp_transport *second; 671 struct sctp_ulpevent *event; 672 struct list_head *pos; 673 int spc_state = 0; 674 675 /* Record the transition on the transport. */ 676 switch (command) { 677 case SCTP_TRANSPORT_UP: 678 transport->state = SCTP_ACTIVE; 679 spc_state = SCTP_ADDR_AVAILABLE; 680 break; 681 682 case SCTP_TRANSPORT_DOWN: 683 transport->state = SCTP_INACTIVE; 684 spc_state = SCTP_ADDR_UNREACHABLE; 685 break; 686 687 default: 688 return; 689 }; 690 691 /* Generate and send a SCTP_PEER_ADDR_CHANGE notification to the 692 * user. 693 */ 694 event = sctp_ulpevent_make_peer_addr_change(asoc, 695 (struct sockaddr_storage *) &transport->ipaddr, 696 0, spc_state, error, GFP_ATOMIC); 697 if (event) 698 sctp_ulpq_tail_event(&asoc->ulpq, event); 699 700 /* Select new active and retran paths. */ 701 702 /* Look for the two most recently used active transports. 703 * 704 * This code produces the wrong ordering whenever jiffies 705 * rolls over, but we still get usable transports, so we don't 706 * worry about it. 707 */ 708 first = NULL; second = NULL; 709 710 list_for_each(pos, &asoc->peer.transport_addr_list) { 711 t = list_entry(pos, struct sctp_transport, transports); 712 713 if (t->state == SCTP_INACTIVE) 714 continue; 715 if (!first || t->last_time_heard > first->last_time_heard) { 716 second = first; 717 first = t; 718 } 719 if (!second || t->last_time_heard > second->last_time_heard) 720 second = t; 721 } 722 723 /* RFC 2960 6.4 Multi-Homed SCTP Endpoints 724 * 725 * By default, an endpoint should always transmit to the 726 * primary path, unless the SCTP user explicitly specifies the 727 * destination transport address (and possibly source 728 * transport address) to use. 729 * 730 * [If the primary is active but not most recent, bump the most 731 * recently used transport.] 732 */ 733 if (asoc->peer.primary_path->state != SCTP_INACTIVE && 734 first != asoc->peer.primary_path) { 735 second = first; 736 first = asoc->peer.primary_path; 737 } 738 739 /* If we failed to find a usable transport, just camp on the 740 * primary, even if it is inactive. 741 */ 742 if (!first) { 743 first = asoc->peer.primary_path; 744 second = asoc->peer.primary_path; 745 } 746 747 /* Set the active and retran transports. */ 748 asoc->peer.active_path = first; 749 asoc->peer.retran_path = second; 750 } 751 752 /* Hold a reference to an association. */ 753 void sctp_association_hold(struct sctp_association *asoc) 754 { 755 atomic_inc(&asoc->base.refcnt); 756 } 757 758 /* Release a reference to an association and cleanup 759 * if there are no more references. 760 */ 761 void sctp_association_put(struct sctp_association *asoc) 762 { 763 if (atomic_dec_and_test(&asoc->base.refcnt)) 764 sctp_association_destroy(asoc); 765 } 766 767 /* Allocate the next TSN, Transmission Sequence Number, for the given 768 * association. 769 */ 770 __u32 sctp_association_get_next_tsn(struct sctp_association *asoc) 771 { 772 /* From Section 1.6 Serial Number Arithmetic: 773 * Transmission Sequence Numbers wrap around when they reach 774 * 2**32 - 1. That is, the next TSN a DATA chunk MUST use 775 * after transmitting TSN = 2*32 - 1 is TSN = 0. 776 */ 777 __u32 retval = asoc->next_tsn; 778 asoc->next_tsn++; 779 asoc->unack_data++; 780 781 return retval; 782 } 783 784 /* Compare two addresses to see if they match. Wildcard addresses 785 * only match themselves. 786 */ 787 int sctp_cmp_addr_exact(const union sctp_addr *ss1, 788 const union sctp_addr *ss2) 789 { 790 struct sctp_af *af; 791 792 af = sctp_get_af_specific(ss1->sa.sa_family); 793 if (unlikely(!af)) 794 return 0; 795 796 return af->cmp_addr(ss1, ss2); 797 } 798 799 /* Return an ecne chunk to get prepended to a packet. 800 * Note: We are sly and return a shared, prealloced chunk. FIXME: 801 * No we don't, but we could/should. 802 */ 803 struct sctp_chunk *sctp_get_ecne_prepend(struct sctp_association *asoc) 804 { 805 struct sctp_chunk *chunk; 806 807 /* Send ECNE if needed. 808 * Not being able to allocate a chunk here is not deadly. 809 */ 810 if (asoc->need_ecne) 811 chunk = sctp_make_ecne(asoc, asoc->last_ecne_tsn); 812 else 813 chunk = NULL; 814 815 return chunk; 816 } 817 818 /* 819 * Find which transport this TSN was sent on. 820 */ 821 struct sctp_transport *sctp_assoc_lookup_tsn(struct sctp_association *asoc, 822 __u32 tsn) 823 { 824 struct sctp_transport *active; 825 struct sctp_transport *match; 826 struct list_head *entry, *pos; 827 struct sctp_transport *transport; 828 struct sctp_chunk *chunk; 829 __u32 key = htonl(tsn); 830 831 match = NULL; 832 833 /* 834 * FIXME: In general, find a more efficient data structure for 835 * searching. 836 */ 837 838 /* 839 * The general strategy is to search each transport's transmitted 840 * list. Return which transport this TSN lives on. 841 * 842 * Let's be hopeful and check the active_path first. 843 * Another optimization would be to know if there is only one 844 * outbound path and not have to look for the TSN at all. 845 * 846 */ 847 848 active = asoc->peer.active_path; 849 850 list_for_each(entry, &active->transmitted) { 851 chunk = list_entry(entry, struct sctp_chunk, transmitted_list); 852 853 if (key == chunk->subh.data_hdr->tsn) { 854 match = active; 855 goto out; 856 } 857 } 858 859 /* If not found, go search all the other transports. */ 860 list_for_each(pos, &asoc->peer.transport_addr_list) { 861 transport = list_entry(pos, struct sctp_transport, transports); 862 863 if (transport == active) 864 break; 865 list_for_each(entry, &transport->transmitted) { 866 chunk = list_entry(entry, struct sctp_chunk, 867 transmitted_list); 868 if (key == chunk->subh.data_hdr->tsn) { 869 match = transport; 870 goto out; 871 } 872 } 873 } 874 out: 875 return match; 876 } 877 878 /* Is this the association we are looking for? */ 879 struct sctp_transport *sctp_assoc_is_match(struct sctp_association *asoc, 880 const union sctp_addr *laddr, 881 const union sctp_addr *paddr) 882 { 883 struct sctp_transport *transport; 884 885 sctp_read_lock(&asoc->base.addr_lock); 886 887 if ((asoc->base.bind_addr.port == laddr->v4.sin_port) && 888 (asoc->peer.port == paddr->v4.sin_port)) { 889 transport = sctp_assoc_lookup_paddr(asoc, paddr); 890 if (!transport) 891 goto out; 892 893 if (sctp_bind_addr_match(&asoc->base.bind_addr, laddr, 894 sctp_sk(asoc->base.sk))) 895 goto out; 896 } 897 transport = NULL; 898 899 out: 900 sctp_read_unlock(&asoc->base.addr_lock); 901 return transport; 902 } 903 904 /* Do delayed input processing. This is scheduled by sctp_rcv(). */ 905 static void sctp_assoc_bh_rcv(struct sctp_association *asoc) 906 { 907 struct sctp_endpoint *ep; 908 struct sctp_chunk *chunk; 909 struct sock *sk; 910 struct sctp_inq *inqueue; 911 int state; 912 sctp_subtype_t subtype; 913 int error = 0; 914 915 /* The association should be held so we should be safe. */ 916 ep = asoc->ep; 917 sk = asoc->base.sk; 918 919 inqueue = &asoc->base.inqueue; 920 sctp_association_hold(asoc); 921 while (NULL != (chunk = sctp_inq_pop(inqueue))) { 922 state = asoc->state; 923 subtype = SCTP_ST_CHUNK(chunk->chunk_hdr->type); 924 925 /* Remember where the last DATA chunk came from so we 926 * know where to send the SACK. 927 */ 928 if (sctp_chunk_is_data(chunk)) 929 asoc->peer.last_data_from = chunk->transport; 930 else 931 SCTP_INC_STATS(SCTP_MIB_INCTRLCHUNKS); 932 933 if (chunk->transport) 934 chunk->transport->last_time_heard = jiffies; 935 936 /* Run through the state machine. */ 937 error = sctp_do_sm(SCTP_EVENT_T_CHUNK, subtype, 938 state, ep, asoc, chunk, GFP_ATOMIC); 939 940 /* Check to see if the association is freed in response to 941 * the incoming chunk. If so, get out of the while loop. 942 */ 943 if (asoc->base.dead) 944 break; 945 946 /* If there is an error on chunk, discard this packet. */ 947 if (error && chunk) 948 chunk->pdiscard = 1; 949 } 950 sctp_association_put(asoc); 951 } 952 953 /* This routine moves an association from its old sk to a new sk. */ 954 void sctp_assoc_migrate(struct sctp_association *assoc, struct sock *newsk) 955 { 956 struct sctp_sock *newsp = sctp_sk(newsk); 957 struct sock *oldsk = assoc->base.sk; 958 959 /* Delete the association from the old endpoint's list of 960 * associations. 961 */ 962 list_del_init(&assoc->asocs); 963 964 /* Decrement the backlog value for a TCP-style socket. */ 965 if (sctp_style(oldsk, TCP)) 966 oldsk->sk_ack_backlog--; 967 968 /* Release references to the old endpoint and the sock. */ 969 sctp_endpoint_put(assoc->ep); 970 sock_put(assoc->base.sk); 971 972 /* Get a reference to the new endpoint. */ 973 assoc->ep = newsp->ep; 974 sctp_endpoint_hold(assoc->ep); 975 976 /* Get a reference to the new sock. */ 977 assoc->base.sk = newsk; 978 sock_hold(assoc->base.sk); 979 980 /* Add the association to the new endpoint's list of associations. */ 981 sctp_endpoint_add_asoc(newsp->ep, assoc); 982 } 983 984 /* Update an association (possibly from unexpected COOKIE-ECHO processing). */ 985 void sctp_assoc_update(struct sctp_association *asoc, 986 struct sctp_association *new) 987 { 988 struct sctp_transport *trans; 989 struct list_head *pos, *temp; 990 991 /* Copy in new parameters of peer. */ 992 asoc->c = new->c; 993 asoc->peer.rwnd = new->peer.rwnd; 994 asoc->peer.sack_needed = new->peer.sack_needed; 995 asoc->peer.i = new->peer.i; 996 sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_SIZE, 997 asoc->peer.i.initial_tsn); 998 999 /* Remove any peer addresses not present in the new association. */ 1000 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) { 1001 trans = list_entry(pos, struct sctp_transport, transports); 1002 if (!sctp_assoc_lookup_paddr(new, &trans->ipaddr)) 1003 sctp_assoc_del_peer(asoc, &trans->ipaddr); 1004 } 1005 1006 /* If the case is A (association restart), use 1007 * initial_tsn as next_tsn. If the case is B, use 1008 * current next_tsn in case data sent to peer 1009 * has been discarded and needs retransmission. 1010 */ 1011 if (asoc->state >= SCTP_STATE_ESTABLISHED) { 1012 asoc->next_tsn = new->next_tsn; 1013 asoc->ctsn_ack_point = new->ctsn_ack_point; 1014 asoc->adv_peer_ack_point = new->adv_peer_ack_point; 1015 1016 /* Reinitialize SSN for both local streams 1017 * and peer's streams. 1018 */ 1019 sctp_ssnmap_clear(asoc->ssnmap); 1020 1021 } else { 1022 /* Add any peer addresses from the new association. */ 1023 list_for_each(pos, &new->peer.transport_addr_list) { 1024 trans = list_entry(pos, struct sctp_transport, 1025 transports); 1026 if (!sctp_assoc_lookup_paddr(asoc, &trans->ipaddr)) 1027 sctp_assoc_add_peer(asoc, &trans->ipaddr, 1028 GFP_ATOMIC, SCTP_ACTIVE); 1029 } 1030 1031 asoc->ctsn_ack_point = asoc->next_tsn - 1; 1032 asoc->adv_peer_ack_point = asoc->ctsn_ack_point; 1033 if (!asoc->ssnmap) { 1034 /* Move the ssnmap. */ 1035 asoc->ssnmap = new->ssnmap; 1036 new->ssnmap = NULL; 1037 } 1038 } 1039 } 1040 1041 /* Update the retran path for sending a retransmitted packet. 1042 * Round-robin through the active transports, else round-robin 1043 * through the inactive transports as this is the next best thing 1044 * we can try. 1045 */ 1046 void sctp_assoc_update_retran_path(struct sctp_association *asoc) 1047 { 1048 struct sctp_transport *t, *next; 1049 struct list_head *head = &asoc->peer.transport_addr_list; 1050 struct list_head *pos; 1051 1052 /* Find the next transport in a round-robin fashion. */ 1053 t = asoc->peer.retran_path; 1054 pos = &t->transports; 1055 next = NULL; 1056 1057 while (1) { 1058 /* Skip the head. */ 1059 if (pos->next == head) 1060 pos = head->next; 1061 else 1062 pos = pos->next; 1063 1064 t = list_entry(pos, struct sctp_transport, transports); 1065 1066 /* Try to find an active transport. */ 1067 1068 if (t->state != SCTP_INACTIVE) { 1069 break; 1070 } else { 1071 /* Keep track of the next transport in case 1072 * we don't find any active transport. 1073 */ 1074 if (!next) 1075 next = t; 1076 } 1077 1078 /* We have exhausted the list, but didn't find any 1079 * other active transports. If so, use the next 1080 * transport. 1081 */ 1082 if (t == asoc->peer.retran_path) { 1083 t = next; 1084 break; 1085 } 1086 } 1087 1088 asoc->peer.retran_path = t; 1089 1090 SCTP_DEBUG_PRINTK_IPADDR("sctp_assoc_update_retran_path:association" 1091 " %p addr: ", 1092 " port: %d\n", 1093 asoc, 1094 (&t->ipaddr), 1095 t->ipaddr.v4.sin_port); 1096 } 1097 1098 /* Choose the transport for sending a INIT packet. */ 1099 struct sctp_transport *sctp_assoc_choose_init_transport( 1100 struct sctp_association *asoc) 1101 { 1102 struct sctp_transport *t; 1103 1104 /* Use the retran path. If the last INIT was sent over the 1105 * retran path, update the retran path and use it. 1106 */ 1107 if (!asoc->init_last_sent_to) { 1108 t = asoc->peer.active_path; 1109 } else { 1110 if (asoc->init_last_sent_to == asoc->peer.retran_path) 1111 sctp_assoc_update_retran_path(asoc); 1112 t = asoc->peer.retran_path; 1113 } 1114 1115 SCTP_DEBUG_PRINTK_IPADDR("sctp_assoc_update_retran_path:association" 1116 " %p addr: ", 1117 " port: %d\n", 1118 asoc, 1119 (&t->ipaddr), 1120 t->ipaddr.v4.sin_port); 1121 1122 return t; 1123 } 1124 1125 /* Choose the transport for sending a SHUTDOWN packet. */ 1126 struct sctp_transport *sctp_assoc_choose_shutdown_transport( 1127 struct sctp_association *asoc) 1128 { 1129 /* If this is the first time SHUTDOWN is sent, use the active path, 1130 * else use the retran path. If the last SHUTDOWN was sent over the 1131 * retran path, update the retran path and use it. 1132 */ 1133 if (!asoc->shutdown_last_sent_to) 1134 return asoc->peer.active_path; 1135 else { 1136 if (asoc->shutdown_last_sent_to == asoc->peer.retran_path) 1137 sctp_assoc_update_retran_path(asoc); 1138 return asoc->peer.retran_path; 1139 } 1140 1141 } 1142 1143 /* Update the association's pmtu and frag_point by going through all the 1144 * transports. This routine is called when a transport's PMTU has changed. 1145 */ 1146 void sctp_assoc_sync_pmtu(struct sctp_association *asoc) 1147 { 1148 struct sctp_transport *t; 1149 struct list_head *pos; 1150 __u32 pmtu = 0; 1151 1152 if (!asoc) 1153 return; 1154 1155 /* Get the lowest pmtu of all the transports. */ 1156 list_for_each(pos, &asoc->peer.transport_addr_list) { 1157 t = list_entry(pos, struct sctp_transport, transports); 1158 if (!pmtu || (t->pmtu < pmtu)) 1159 pmtu = t->pmtu; 1160 } 1161 1162 if (pmtu) { 1163 struct sctp_sock *sp = sctp_sk(asoc->base.sk); 1164 asoc->pmtu = pmtu; 1165 asoc->frag_point = sctp_frag_point(sp, pmtu); 1166 } 1167 1168 SCTP_DEBUG_PRINTK("%s: asoc:%p, pmtu:%d, frag_point:%d\n", 1169 __FUNCTION__, asoc, asoc->pmtu, asoc->frag_point); 1170 } 1171 1172 /* Should we send a SACK to update our peer? */ 1173 static inline int sctp_peer_needs_update(struct sctp_association *asoc) 1174 { 1175 switch (asoc->state) { 1176 case SCTP_STATE_ESTABLISHED: 1177 case SCTP_STATE_SHUTDOWN_PENDING: 1178 case SCTP_STATE_SHUTDOWN_RECEIVED: 1179 case SCTP_STATE_SHUTDOWN_SENT: 1180 if ((asoc->rwnd > asoc->a_rwnd) && 1181 ((asoc->rwnd - asoc->a_rwnd) >= 1182 min_t(__u32, (asoc->base.sk->sk_rcvbuf >> 1), asoc->pmtu))) 1183 return 1; 1184 break; 1185 default: 1186 break; 1187 } 1188 return 0; 1189 } 1190 1191 /* Increase asoc's rwnd by len and send any window update SACK if needed. */ 1192 void sctp_assoc_rwnd_increase(struct sctp_association *asoc, unsigned len) 1193 { 1194 struct sctp_chunk *sack; 1195 struct timer_list *timer; 1196 1197 if (asoc->rwnd_over) { 1198 if (asoc->rwnd_over >= len) { 1199 asoc->rwnd_over -= len; 1200 } else { 1201 asoc->rwnd += (len - asoc->rwnd_over); 1202 asoc->rwnd_over = 0; 1203 } 1204 } else { 1205 asoc->rwnd += len; 1206 } 1207 1208 SCTP_DEBUG_PRINTK("%s: asoc %p rwnd increased by %d to (%u, %u) " 1209 "- %u\n", __FUNCTION__, asoc, len, asoc->rwnd, 1210 asoc->rwnd_over, asoc->a_rwnd); 1211 1212 /* Send a window update SACK if the rwnd has increased by at least the 1213 * minimum of the association's PMTU and half of the receive buffer. 1214 * The algorithm used is similar to the one described in 1215 * Section 4.2.3.3 of RFC 1122. 1216 */ 1217 if (sctp_peer_needs_update(asoc)) { 1218 asoc->a_rwnd = asoc->rwnd; 1219 SCTP_DEBUG_PRINTK("%s: Sending window update SACK- asoc: %p " 1220 "rwnd: %u a_rwnd: %u\n", __FUNCTION__, 1221 asoc, asoc->rwnd, asoc->a_rwnd); 1222 sack = sctp_make_sack(asoc); 1223 if (!sack) 1224 return; 1225 1226 asoc->peer.sack_needed = 0; 1227 1228 sctp_outq_tail(&asoc->outqueue, sack); 1229 1230 /* Stop the SACK timer. */ 1231 timer = &asoc->timers[SCTP_EVENT_TIMEOUT_SACK]; 1232 if (timer_pending(timer) && del_timer(timer)) 1233 sctp_association_put(asoc); 1234 } 1235 } 1236 1237 /* Decrease asoc's rwnd by len. */ 1238 void sctp_assoc_rwnd_decrease(struct sctp_association *asoc, unsigned len) 1239 { 1240 SCTP_ASSERT(asoc->rwnd, "rwnd zero", return); 1241 SCTP_ASSERT(!asoc->rwnd_over, "rwnd_over not zero", return); 1242 if (asoc->rwnd >= len) { 1243 asoc->rwnd -= len; 1244 } else { 1245 asoc->rwnd_over = len - asoc->rwnd; 1246 asoc->rwnd = 0; 1247 } 1248 SCTP_DEBUG_PRINTK("%s: asoc %p rwnd decreased by %d to (%u, %u)\n", 1249 __FUNCTION__, asoc, len, asoc->rwnd, 1250 asoc->rwnd_over); 1251 } 1252 1253 /* Build the bind address list for the association based on info from the 1254 * local endpoint and the remote peer. 1255 */ 1256 int sctp_assoc_set_bind_addr_from_ep(struct sctp_association *asoc, 1257 gfp_t gfp) 1258 { 1259 sctp_scope_t scope; 1260 int flags; 1261 1262 /* Use scoping rules to determine the subset of addresses from 1263 * the endpoint. 1264 */ 1265 scope = sctp_scope(&asoc->peer.active_path->ipaddr); 1266 flags = (PF_INET6 == asoc->base.sk->sk_family) ? SCTP_ADDR6_ALLOWED : 0; 1267 if (asoc->peer.ipv4_address) 1268 flags |= SCTP_ADDR4_PEERSUPP; 1269 if (asoc->peer.ipv6_address) 1270 flags |= SCTP_ADDR6_PEERSUPP; 1271 1272 return sctp_bind_addr_copy(&asoc->base.bind_addr, 1273 &asoc->ep->base.bind_addr, 1274 scope, gfp, flags); 1275 } 1276 1277 /* Build the association's bind address list from the cookie. */ 1278 int sctp_assoc_set_bind_addr_from_cookie(struct sctp_association *asoc, 1279 struct sctp_cookie *cookie, 1280 gfp_t gfp) 1281 { 1282 int var_size2 = ntohs(cookie->peer_init->chunk_hdr.length); 1283 int var_size3 = cookie->raw_addr_list_len; 1284 __u8 *raw = (__u8 *)cookie->peer_init + var_size2; 1285 1286 return sctp_raw_to_bind_addrs(&asoc->base.bind_addr, raw, var_size3, 1287 asoc->ep->base.bind_addr.port, gfp); 1288 } 1289 1290 /* Lookup laddr in the bind address list of an association. */ 1291 int sctp_assoc_lookup_laddr(struct sctp_association *asoc, 1292 const union sctp_addr *laddr) 1293 { 1294 int found; 1295 1296 sctp_read_lock(&asoc->base.addr_lock); 1297 if ((asoc->base.bind_addr.port == ntohs(laddr->v4.sin_port)) && 1298 sctp_bind_addr_match(&asoc->base.bind_addr, laddr, 1299 sctp_sk(asoc->base.sk))) { 1300 found = 1; 1301 goto out; 1302 } 1303 1304 found = 0; 1305 out: 1306 sctp_read_unlock(&asoc->base.addr_lock); 1307 return found; 1308 } 1309