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 * 6 * This file is part of the SCTP kernel reference Implementation 7 * 8 * These functions handle output processing. 9 * 10 * The SCTP reference implementation is free software; 11 * you can redistribute it and/or modify it under the terms of 12 * the GNU General Public License as published by 13 * the Free Software Foundation; either version 2, or (at your option) 14 * any later version. 15 * 16 * The SCTP reference implementation is distributed in the hope that it 17 * will be useful, but WITHOUT ANY WARRANTY; without even the implied 18 * ************************ 19 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 20 * See the GNU General Public License for more details. 21 * 22 * You should have received a copy of the GNU General Public License 23 * along with GNU CC; see the file COPYING. If not, write to 24 * the Free Software Foundation, 59 Temple Place - Suite 330, 25 * Boston, MA 02111-1307, USA. 26 * 27 * Please send any bug reports or fixes you make to the 28 * email address(es): 29 * lksctp developers <lksctp-developers@lists.sourceforge.net> 30 * 31 * Or submit a bug report through the following website: 32 * http://www.sf.net/projects/lksctp 33 * 34 * Written or modified by: 35 * La Monte H.P. Yarroll <piggy@acm.org> 36 * Karl Knutson <karl@athena.chicago.il.us> 37 * Jon Grimm <jgrimm@austin.ibm.com> 38 * Sridhar Samudrala <sri@us.ibm.com> 39 * 40 * Any bugs reported given to us we will try to fix... any fixes shared will 41 * be incorporated into the next SCTP release. 42 */ 43 44 #include <linux/types.h> 45 #include <linux/kernel.h> 46 #include <linux/wait.h> 47 #include <linux/time.h> 48 #include <linux/ip.h> 49 #include <linux/ipv6.h> 50 #include <linux/init.h> 51 #include <net/inet_ecn.h> 52 #include <net/icmp.h> 53 54 #ifndef TEST_FRAME 55 #include <net/tcp.h> 56 #endif /* TEST_FRAME (not defined) */ 57 58 #include <linux/socket.h> /* for sa_family_t */ 59 #include <net/sock.h> 60 61 #include <net/sctp/sctp.h> 62 #include <net/sctp/sm.h> 63 64 /* Forward declarations for private helpers. */ 65 static sctp_xmit_t sctp_packet_append_data(struct sctp_packet *packet, 66 struct sctp_chunk *chunk); 67 68 /* Config a packet. 69 * This appears to be a followup set of initializations. 70 */ 71 struct sctp_packet *sctp_packet_config(struct sctp_packet *packet, 72 __u32 vtag, int ecn_capable) 73 { 74 struct sctp_chunk *chunk = NULL; 75 76 SCTP_DEBUG_PRINTK("%s: packet:%p vtag:0x%x\n", __FUNCTION__, 77 packet, vtag); 78 79 packet->vtag = vtag; 80 packet->has_cookie_echo = 0; 81 packet->has_sack = 0; 82 packet->ipfragok = 0; 83 84 if (ecn_capable && sctp_packet_empty(packet)) { 85 chunk = sctp_get_ecne_prepend(packet->transport->asoc); 86 87 /* If there a is a prepend chunk stick it on the list before 88 * any other chunks get appended. 89 */ 90 if (chunk) 91 sctp_packet_append_chunk(packet, chunk); 92 } 93 94 return packet; 95 } 96 97 /* Initialize the packet structure. */ 98 struct sctp_packet *sctp_packet_init(struct sctp_packet *packet, 99 struct sctp_transport *transport, 100 __u16 sport, __u16 dport) 101 { 102 struct sctp_association *asoc = transport->asoc; 103 size_t overhead; 104 105 SCTP_DEBUG_PRINTK("%s: packet:%p transport:%p\n", __FUNCTION__, 106 packet, transport); 107 108 packet->transport = transport; 109 packet->source_port = sport; 110 packet->destination_port = dport; 111 INIT_LIST_HEAD(&packet->chunk_list); 112 if (asoc) { 113 struct sctp_sock *sp = sctp_sk(asoc->base.sk); 114 overhead = sp->pf->af->net_header_len; 115 } else { 116 overhead = sizeof(struct ipv6hdr); 117 } 118 overhead += sizeof(struct sctphdr); 119 packet->overhead = overhead; 120 packet->size = overhead; 121 packet->vtag = 0; 122 packet->has_cookie_echo = 0; 123 packet->has_sack = 0; 124 packet->ipfragok = 0; 125 packet->malloced = 0; 126 return packet; 127 } 128 129 /* Free a packet. */ 130 void sctp_packet_free(struct sctp_packet *packet) 131 { 132 struct sctp_chunk *chunk, *tmp; 133 134 SCTP_DEBUG_PRINTK("%s: packet:%p\n", __FUNCTION__, packet); 135 136 list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) { 137 list_del_init(&chunk->list); 138 sctp_chunk_free(chunk); 139 } 140 141 if (packet->malloced) 142 kfree(packet); 143 } 144 145 /* This routine tries to append the chunk to the offered packet. If adding 146 * the chunk causes the packet to exceed the path MTU and COOKIE_ECHO chunk 147 * is not present in the packet, it transmits the input packet. 148 * Data can be bundled with a packet containing a COOKIE_ECHO chunk as long 149 * as it can fit in the packet, but any more data that does not fit in this 150 * packet can be sent only after receiving the COOKIE_ACK. 151 */ 152 sctp_xmit_t sctp_packet_transmit_chunk(struct sctp_packet *packet, 153 struct sctp_chunk *chunk) 154 { 155 sctp_xmit_t retval; 156 int error = 0; 157 158 SCTP_DEBUG_PRINTK("%s: packet:%p chunk:%p\n", __FUNCTION__, 159 packet, chunk); 160 161 switch ((retval = (sctp_packet_append_chunk(packet, chunk)))) { 162 case SCTP_XMIT_PMTU_FULL: 163 if (!packet->has_cookie_echo) { 164 error = sctp_packet_transmit(packet); 165 if (error < 0) 166 chunk->skb->sk->sk_err = -error; 167 168 /* If we have an empty packet, then we can NOT ever 169 * return PMTU_FULL. 170 */ 171 retval = sctp_packet_append_chunk(packet, chunk); 172 } 173 break; 174 175 case SCTP_XMIT_RWND_FULL: 176 case SCTP_XMIT_OK: 177 case SCTP_XMIT_NAGLE_DELAY: 178 break; 179 }; 180 181 return retval; 182 } 183 184 /* Try to bundle a SACK with the packet. */ 185 static sctp_xmit_t sctp_packet_bundle_sack(struct sctp_packet *pkt, 186 struct sctp_chunk *chunk) 187 { 188 sctp_xmit_t retval = SCTP_XMIT_OK; 189 190 /* If sending DATA and haven't aleady bundled a SACK, try to 191 * bundle one in to the packet. 192 */ 193 if (sctp_chunk_is_data(chunk) && !pkt->has_sack && 194 !pkt->has_cookie_echo) { 195 struct sctp_association *asoc; 196 asoc = pkt->transport->asoc; 197 198 if (asoc->a_rwnd > asoc->rwnd) { 199 struct sctp_chunk *sack; 200 asoc->a_rwnd = asoc->rwnd; 201 sack = sctp_make_sack(asoc); 202 if (sack) { 203 struct timer_list *timer; 204 retval = sctp_packet_append_chunk(pkt, sack); 205 asoc->peer.sack_needed = 0; 206 timer = &asoc->timers[SCTP_EVENT_TIMEOUT_SACK]; 207 if (timer_pending(timer) && del_timer(timer)) 208 sctp_association_put(asoc); 209 } 210 } 211 } 212 return retval; 213 } 214 215 /* Append a chunk to the offered packet reporting back any inability to do 216 * so. 217 */ 218 sctp_xmit_t sctp_packet_append_chunk(struct sctp_packet *packet, 219 struct sctp_chunk *chunk) 220 { 221 sctp_xmit_t retval = SCTP_XMIT_OK; 222 __u16 chunk_len = WORD_ROUND(ntohs(chunk->chunk_hdr->length)); 223 size_t psize; 224 size_t pmtu; 225 int too_big; 226 227 SCTP_DEBUG_PRINTK("%s: packet:%p chunk:%p\n", __FUNCTION__, packet, 228 chunk); 229 230 retval = sctp_packet_bundle_sack(packet, chunk); 231 psize = packet->size; 232 233 if (retval != SCTP_XMIT_OK) 234 goto finish; 235 236 pmtu = ((packet->transport->asoc) ? 237 (packet->transport->asoc->pathmtu) : 238 (packet->transport->pathmtu)); 239 240 too_big = (psize + chunk_len > pmtu); 241 242 /* Decide if we need to fragment or resubmit later. */ 243 if (too_big) { 244 /* Both control chunks and data chunks with TSNs are 245 * non-fragmentable. 246 */ 247 if (sctp_packet_empty(packet) || !sctp_chunk_is_data(chunk)) { 248 /* We no longer do re-fragmentation. 249 * Just fragment at the IP layer, if we 250 * actually hit this condition 251 */ 252 packet->ipfragok = 1; 253 goto append; 254 255 } else { 256 retval = SCTP_XMIT_PMTU_FULL; 257 goto finish; 258 } 259 } 260 261 append: 262 /* We believe that this chunk is OK to add to the packet (as 263 * long as we have the cwnd for it). 264 */ 265 266 /* DATA is a special case since we must examine both rwnd and cwnd 267 * before we send DATA. 268 */ 269 if (sctp_chunk_is_data(chunk)) { 270 retval = sctp_packet_append_data(packet, chunk); 271 /* Disallow SACK bundling after DATA. */ 272 packet->has_sack = 1; 273 if (SCTP_XMIT_OK != retval) 274 goto finish; 275 } else if (SCTP_CID_COOKIE_ECHO == chunk->chunk_hdr->type) 276 packet->has_cookie_echo = 1; 277 else if (SCTP_CID_SACK == chunk->chunk_hdr->type) 278 packet->has_sack = 1; 279 280 /* It is OK to send this chunk. */ 281 list_add_tail(&chunk->list, &packet->chunk_list); 282 packet->size += chunk_len; 283 chunk->transport = packet->transport; 284 finish: 285 return retval; 286 } 287 288 /* All packets are sent to the network through this function from 289 * sctp_outq_tail(). 290 * 291 * The return value is a normal kernel error return value. 292 */ 293 int sctp_packet_transmit(struct sctp_packet *packet) 294 { 295 struct sctp_transport *tp = packet->transport; 296 struct sctp_association *asoc = tp->asoc; 297 struct sctphdr *sh; 298 __u32 crc32 = 0; 299 struct sk_buff *nskb; 300 struct sctp_chunk *chunk, *tmp; 301 struct sock *sk; 302 int err = 0; 303 int padding; /* How much padding do we need? */ 304 __u8 has_data = 0; 305 struct dst_entry *dst = tp->dst; 306 307 SCTP_DEBUG_PRINTK("%s: packet:%p\n", __FUNCTION__, packet); 308 309 /* Do NOT generate a chunkless packet. */ 310 if (list_empty(&packet->chunk_list)) 311 return err; 312 313 /* Set up convenience variables... */ 314 chunk = list_entry(packet->chunk_list.next, struct sctp_chunk, list); 315 sk = chunk->skb->sk; 316 317 /* Allocate the new skb. */ 318 nskb = alloc_skb(packet->size + LL_MAX_HEADER, GFP_ATOMIC); 319 if (!nskb) 320 goto nomem; 321 322 /* Make sure the outbound skb has enough header room reserved. */ 323 skb_reserve(nskb, packet->overhead + LL_MAX_HEADER); 324 325 /* Set the owning socket so that we know where to get the 326 * destination IP address. 327 */ 328 skb_set_owner_w(nskb, sk); 329 330 /* The 'obsolete' field of dst is set to 2 when a dst is freed. */ 331 if (!dst || (dst->obsolete > 1)) { 332 dst_release(dst); 333 sctp_transport_route(tp, NULL, sctp_sk(sk)); 334 if (asoc && (asoc->param_flags & SPP_PMTUD_ENABLE)) { 335 sctp_assoc_sync_pmtu(asoc); 336 } 337 } 338 nskb->dst = dst_clone(tp->dst); 339 if (!nskb->dst) 340 goto no_route; 341 dst = nskb->dst; 342 343 /* Build the SCTP header. */ 344 sh = (struct sctphdr *)skb_push(nskb, sizeof(struct sctphdr)); 345 sh->source = htons(packet->source_port); 346 sh->dest = htons(packet->destination_port); 347 348 /* From 6.8 Adler-32 Checksum Calculation: 349 * After the packet is constructed (containing the SCTP common 350 * header and one or more control or DATA chunks), the 351 * transmitter shall: 352 * 353 * 1) Fill in the proper Verification Tag in the SCTP common 354 * header and initialize the checksum field to 0's. 355 */ 356 sh->vtag = htonl(packet->vtag); 357 sh->checksum = 0; 358 359 /* 2) Calculate the Adler-32 checksum of the whole packet, 360 * including the SCTP common header and all the 361 * chunks. 362 * 363 * Note: Adler-32 is no longer applicable, as has been replaced 364 * by CRC32-C as described in <draft-ietf-tsvwg-sctpcsum-02.txt>. 365 */ 366 if (!(dst->dev->features & NETIF_F_NO_CSUM)) 367 crc32 = sctp_start_cksum((__u8 *)sh, sizeof(struct sctphdr)); 368 369 /** 370 * 6.10 Bundling 371 * 372 * An endpoint bundles chunks by simply including multiple 373 * chunks in one outbound SCTP packet. ... 374 */ 375 376 /** 377 * 3.2 Chunk Field Descriptions 378 * 379 * The total length of a chunk (including Type, Length and 380 * Value fields) MUST be a multiple of 4 bytes. If the length 381 * of the chunk is not a multiple of 4 bytes, the sender MUST 382 * pad the chunk with all zero bytes and this padding is not 383 * included in the chunk length field. The sender should 384 * never pad with more than 3 bytes. 385 * 386 * [This whole comment explains WORD_ROUND() below.] 387 */ 388 SCTP_DEBUG_PRINTK("***sctp_transmit_packet***\n"); 389 list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) { 390 list_del_init(&chunk->list); 391 if (sctp_chunk_is_data(chunk)) { 392 393 if (!chunk->has_tsn) { 394 sctp_chunk_assign_ssn(chunk); 395 sctp_chunk_assign_tsn(chunk); 396 397 /* 6.3.1 C4) When data is in flight and when allowed 398 * by rule C5, a new RTT measurement MUST be made each 399 * round trip. Furthermore, new RTT measurements 400 * SHOULD be made no more than once per round-trip 401 * for a given destination transport address. 402 */ 403 404 if (!tp->rto_pending) { 405 chunk->rtt_in_progress = 1; 406 tp->rto_pending = 1; 407 } 408 } else 409 chunk->resent = 1; 410 411 chunk->sent_at = jiffies; 412 has_data = 1; 413 } 414 415 padding = WORD_ROUND(chunk->skb->len) - chunk->skb->len; 416 if (padding) 417 memset(skb_put(chunk->skb, padding), 0, padding); 418 419 if (dst->dev->features & NETIF_F_NO_CSUM) 420 memcpy(skb_put(nskb, chunk->skb->len), 421 chunk->skb->data, chunk->skb->len); 422 else 423 crc32 = sctp_update_copy_cksum(skb_put(nskb, 424 chunk->skb->len), 425 chunk->skb->data, 426 chunk->skb->len, crc32); 427 428 SCTP_DEBUG_PRINTK("%s %p[%s] %s 0x%x, %s %d, %s %d, %s %d\n", 429 "*** Chunk", chunk, 430 sctp_cname(SCTP_ST_CHUNK( 431 chunk->chunk_hdr->type)), 432 chunk->has_tsn ? "TSN" : "No TSN", 433 chunk->has_tsn ? 434 ntohl(chunk->subh.data_hdr->tsn) : 0, 435 "length", ntohs(chunk->chunk_hdr->length), 436 "chunk->skb->len", chunk->skb->len, 437 "rtt_in_progress", chunk->rtt_in_progress); 438 439 /* 440 * If this is a control chunk, this is our last 441 * reference. Free data chunks after they've been 442 * acknowledged or have failed. 443 */ 444 if (!sctp_chunk_is_data(chunk)) 445 sctp_chunk_free(chunk); 446 } 447 448 /* Perform final transformation on checksum. */ 449 if (!(dst->dev->features & NETIF_F_NO_CSUM)) 450 crc32 = sctp_end_cksum(crc32); 451 452 /* 3) Put the resultant value into the checksum field in the 453 * common header, and leave the rest of the bits unchanged. 454 */ 455 sh->checksum = htonl(crc32); 456 457 /* IP layer ECN support 458 * From RFC 2481 459 * "The ECN-Capable Transport (ECT) bit would be set by the 460 * data sender to indicate that the end-points of the 461 * transport protocol are ECN-capable." 462 * 463 * Now setting the ECT bit all the time, as it should not cause 464 * any problems protocol-wise even if our peer ignores it. 465 * 466 * Note: The works for IPv6 layer checks this bit too later 467 * in transmission. See IP6_ECN_flow_xmit(). 468 */ 469 INET_ECN_xmit(nskb->sk); 470 471 /* Set up the IP options. */ 472 /* BUG: not implemented 473 * For v4 this all lives somewhere in sk->sk_opt... 474 */ 475 476 /* Dump that on IP! */ 477 if (asoc && asoc->peer.last_sent_to != tp) { 478 /* Considering the multiple CPU scenario, this is a 479 * "correcter" place for last_sent_to. --xguo 480 */ 481 asoc->peer.last_sent_to = tp; 482 } 483 484 if (has_data) { 485 struct timer_list *timer; 486 unsigned long timeout; 487 488 tp->last_time_used = jiffies; 489 490 /* Restart the AUTOCLOSE timer when sending data. */ 491 if (sctp_state(asoc, ESTABLISHED) && asoc->autoclose) { 492 timer = &asoc->timers[SCTP_EVENT_TIMEOUT_AUTOCLOSE]; 493 timeout = asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]; 494 495 if (!mod_timer(timer, jiffies + timeout)) 496 sctp_association_hold(asoc); 497 } 498 } 499 500 SCTP_DEBUG_PRINTK("***sctp_transmit_packet*** skb len %d\n", 501 nskb->len); 502 503 if (tp->param_flags & SPP_PMTUD_ENABLE) 504 (*tp->af_specific->sctp_xmit)(nskb, tp, packet->ipfragok); 505 else 506 (*tp->af_specific->sctp_xmit)(nskb, tp, 1); 507 508 out: 509 packet->size = packet->overhead; 510 return err; 511 no_route: 512 kfree_skb(nskb); 513 IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES); 514 515 /* FIXME: Returning the 'err' will effect all the associations 516 * associated with a socket, although only one of the paths of the 517 * association is unreachable. 518 * The real failure of a transport or association can be passed on 519 * to the user via notifications. So setting this error may not be 520 * required. 521 */ 522 /* err = -EHOSTUNREACH; */ 523 err: 524 /* Control chunks are unreliable so just drop them. DATA chunks 525 * will get resent or dropped later. 526 */ 527 528 list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) { 529 list_del_init(&chunk->list); 530 if (!sctp_chunk_is_data(chunk)) 531 sctp_chunk_free(chunk); 532 } 533 goto out; 534 nomem: 535 err = -ENOMEM; 536 goto err; 537 } 538 539 /******************************************************************** 540 * 2nd Level Abstractions 541 ********************************************************************/ 542 543 /* This private function handles the specifics of appending DATA chunks. */ 544 static sctp_xmit_t sctp_packet_append_data(struct sctp_packet *packet, 545 struct sctp_chunk *chunk) 546 { 547 sctp_xmit_t retval = SCTP_XMIT_OK; 548 size_t datasize, rwnd, inflight; 549 struct sctp_transport *transport = packet->transport; 550 __u32 max_burst_bytes; 551 struct sctp_association *asoc = transport->asoc; 552 struct sctp_sock *sp = sctp_sk(asoc->base.sk); 553 struct sctp_outq *q = &asoc->outqueue; 554 555 /* RFC 2960 6.1 Transmission of DATA Chunks 556 * 557 * A) At any given time, the data sender MUST NOT transmit new data to 558 * any destination transport address if its peer's rwnd indicates 559 * that the peer has no buffer space (i.e. rwnd is 0, see Section 560 * 6.2.1). However, regardless of the value of rwnd (including if it 561 * is 0), the data sender can always have one DATA chunk in flight to 562 * the receiver if allowed by cwnd (see rule B below). This rule 563 * allows the sender to probe for a change in rwnd that the sender 564 * missed due to the SACK having been lost in transit from the data 565 * receiver to the data sender. 566 */ 567 568 rwnd = asoc->peer.rwnd; 569 inflight = asoc->outqueue.outstanding_bytes; 570 571 datasize = sctp_data_size(chunk); 572 573 if (datasize > rwnd) { 574 if (inflight > 0) { 575 /* We have (at least) one data chunk in flight, 576 * so we can't fall back to rule 6.1 B). 577 */ 578 retval = SCTP_XMIT_RWND_FULL; 579 goto finish; 580 } 581 } 582 583 /* sctpimpguide-05 2.14.2 584 * D) When the time comes for the sender to 585 * transmit new DATA chunks, the protocol parameter Max.Burst MUST 586 * first be applied to limit how many new DATA chunks may be sent. 587 * The limit is applied by adjusting cwnd as follows: 588 * if ((flightsize + Max.Burst * MTU) < cwnd) 589 * cwnd = flightsize + Max.Burst * MTU 590 */ 591 max_burst_bytes = asoc->max_burst * asoc->pathmtu; 592 if ((transport->flight_size + max_burst_bytes) < transport->cwnd) { 593 transport->cwnd = transport->flight_size + max_burst_bytes; 594 SCTP_DEBUG_PRINTK("%s: cwnd limited by max_burst: " 595 "transport: %p, cwnd: %d, " 596 "ssthresh: %d, flight_size: %d, " 597 "pba: %d\n", 598 __FUNCTION__, transport, 599 transport->cwnd, 600 transport->ssthresh, 601 transport->flight_size, 602 transport->partial_bytes_acked); 603 } 604 605 /* RFC 2960 6.1 Transmission of DATA Chunks 606 * 607 * B) At any given time, the sender MUST NOT transmit new data 608 * to a given transport address if it has cwnd or more bytes 609 * of data outstanding to that transport address. 610 */ 611 /* RFC 7.2.4 & the Implementers Guide 2.8. 612 * 613 * 3) ... 614 * When a Fast Retransmit is being performed the sender SHOULD 615 * ignore the value of cwnd and SHOULD NOT delay retransmission. 616 */ 617 if (chunk->fast_retransmit <= 0) 618 if (transport->flight_size >= transport->cwnd) { 619 retval = SCTP_XMIT_RWND_FULL; 620 goto finish; 621 } 622 623 /* Nagle's algorithm to solve small-packet problem: 624 * Inhibit the sending of new chunks when new outgoing data arrives 625 * if any previously transmitted data on the connection remains 626 * unacknowledged. 627 */ 628 if (!sp->nodelay && sctp_packet_empty(packet) && 629 q->outstanding_bytes && sctp_state(asoc, ESTABLISHED)) { 630 unsigned len = datasize + q->out_qlen; 631 632 /* Check whether this chunk and all the rest of pending 633 * data will fit or delay in hopes of bundling a full 634 * sized packet. 635 */ 636 if (len < asoc->pathmtu - packet->overhead) { 637 retval = SCTP_XMIT_NAGLE_DELAY; 638 goto finish; 639 } 640 } 641 642 /* Keep track of how many bytes are in flight over this transport. */ 643 transport->flight_size += datasize; 644 645 /* Keep track of how many bytes are in flight to the receiver. */ 646 asoc->outqueue.outstanding_bytes += datasize; 647 648 /* Update our view of the receiver's rwnd. */ 649 if (datasize < rwnd) 650 rwnd -= datasize; 651 else 652 rwnd = 0; 653 654 asoc->peer.rwnd = rwnd; 655 /* Has been accepted for transmission. */ 656 if (!asoc->peer.prsctp_capable) 657 chunk->msg->can_abandon = 0; 658 659 finish: 660 return retval; 661 } 662