linux/net/sctp/output.c
<<
>>
Prefs
   1/* SCTP kernel 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 implementation
   7 *
   8 * These functions handle output processing.
   9 *
  10 * This SCTP 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 * This SCTP 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#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  45
  46#include <linux/types.h>
  47#include <linux/kernel.h>
  48#include <linux/wait.h>
  49#include <linux/time.h>
  50#include <linux/ip.h>
  51#include <linux/ipv6.h>
  52#include <linux/init.h>
  53#include <linux/slab.h>
  54#include <net/inet_ecn.h>
  55#include <net/ip.h>
  56#include <net/icmp.h>
  57#include <net/net_namespace.h>
  58
  59#include <linux/socket.h> /* for sa_family_t */
  60#include <net/sock.h>
  61
  62#include <net/sctp/sctp.h>
  63#include <net/sctp/sm.h>
  64#include <net/sctp/checksum.h>
  65
  66/* Forward declarations for private helpers. */
  67static sctp_xmit_t __sctp_packet_append_chunk(struct sctp_packet *packet,
  68                                              struct sctp_chunk *chunk);
  69static sctp_xmit_t sctp_packet_can_append_data(struct sctp_packet *packet,
  70                                           struct sctp_chunk *chunk);
  71static void sctp_packet_append_data(struct sctp_packet *packet,
  72                                           struct sctp_chunk *chunk);
  73static sctp_xmit_t sctp_packet_will_fit(struct sctp_packet *packet,
  74                                        struct sctp_chunk *chunk,
  75                                        u16 chunk_len);
  76
  77static void sctp_packet_reset(struct sctp_packet *packet)
  78{
  79        packet->size = packet->overhead;
  80        packet->has_cookie_echo = 0;
  81        packet->has_sack = 0;
  82        packet->has_data = 0;
  83        packet->has_auth = 0;
  84        packet->ipfragok = 0;
  85        packet->auth = NULL;
  86}
  87
  88/* Config a packet.
  89 * This appears to be a followup set of initializations.
  90 */
  91void sctp_packet_config(struct sctp_packet *packet, __u32 vtag,
  92                        int ecn_capable)
  93{
  94        struct sctp_transport *tp = packet->transport;
  95        struct sctp_association *asoc = tp->asoc;
  96        struct sock *sk;
  97
  98        pr_debug("%s: packet:%p vtag:0x%x\n", __func__, packet, vtag);
  99        packet->vtag = vtag;
 100
 101        /* do the following jobs only once for a flush schedule */
 102        if (!sctp_packet_empty(packet))
 103                return;
 104
 105        /* set packet max_size with pathmtu */
 106        packet->max_size = tp->pathmtu;
 107        if (!asoc)
 108                return;
 109
 110        /* update dst or transport pathmtu if in need */
 111        sk = asoc->base.sk;
 112        if (!sctp_transport_dst_check(tp)) {
 113                sctp_transport_route(tp, NULL, sctp_sk(sk));
 114                if (asoc->param_flags & SPP_PMTUD_ENABLE)
 115                        sctp_assoc_sync_pmtu(asoc);
 116        } else if (!sctp_transport_pmtu_check(tp)) {
 117                if (asoc->param_flags & SPP_PMTUD_ENABLE)
 118                        sctp_assoc_sync_pmtu(asoc);
 119        }
 120
 121        if (asoc->pmtu_pending) {
 122                if (asoc->param_flags & SPP_PMTUD_ENABLE)
 123                        sctp_assoc_sync_pmtu(asoc);
 124                asoc->pmtu_pending = 0;
 125        }
 126
 127        /* If there a is a prepend chunk stick it on the list before
 128         * any other chunks get appended.
 129         */
 130        if (ecn_capable) {
 131                struct sctp_chunk *chunk = sctp_get_ecne_prepend(asoc);
 132
 133                if (chunk)
 134                        sctp_packet_append_chunk(packet, chunk);
 135        }
 136
 137        if (!tp->dst)
 138                return;
 139
 140        /* set packet max_size with gso_max_size if gso is enabled*/
 141        rcu_read_lock();
 142        if (__sk_dst_get(sk) != tp->dst) {
 143                dst_hold(tp->dst);
 144                sk_setup_caps(sk, tp->dst);
 145        }
 146        packet->max_size = sk_can_gso(sk) ? tp->dst->dev->gso_max_size
 147                                          : asoc->pathmtu;
 148        rcu_read_unlock();
 149}
 150
 151/* Initialize the packet structure. */
 152void sctp_packet_init(struct sctp_packet *packet,
 153                      struct sctp_transport *transport,
 154                      __u16 sport, __u16 dport)
 155{
 156        struct sctp_association *asoc = transport->asoc;
 157        size_t overhead;
 158
 159        pr_debug("%s: packet:%p transport:%p\n", __func__, packet, transport);
 160
 161        packet->transport = transport;
 162        packet->source_port = sport;
 163        packet->destination_port = dport;
 164        INIT_LIST_HEAD(&packet->chunk_list);
 165        if (asoc) {
 166                struct sctp_sock *sp = sctp_sk(asoc->base.sk);
 167                overhead = sp->pf->af->net_header_len;
 168        } else {
 169                overhead = sizeof(struct ipv6hdr);
 170        }
 171        overhead += sizeof(struct sctphdr);
 172        packet->overhead = overhead;
 173        sctp_packet_reset(packet);
 174        packet->vtag = 0;
 175}
 176
 177/* Free a packet.  */
 178void sctp_packet_free(struct sctp_packet *packet)
 179{
 180        struct sctp_chunk *chunk, *tmp;
 181
 182        pr_debug("%s: packet:%p\n", __func__, packet);
 183
 184        list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
 185                list_del_init(&chunk->list);
 186                sctp_chunk_free(chunk);
 187        }
 188}
 189
 190/* This routine tries to append the chunk to the offered packet. If adding
 191 * the chunk causes the packet to exceed the path MTU and COOKIE_ECHO chunk
 192 * is not present in the packet, it transmits the input packet.
 193 * Data can be bundled with a packet containing a COOKIE_ECHO chunk as long
 194 * as it can fit in the packet, but any more data that does not fit in this
 195 * packet can be sent only after receiving the COOKIE_ACK.
 196 */
 197sctp_xmit_t sctp_packet_transmit_chunk(struct sctp_packet *packet,
 198                                       struct sctp_chunk *chunk,
 199                                       int one_packet, gfp_t gfp)
 200{
 201        sctp_xmit_t retval;
 202
 203        pr_debug("%s: packet:%p size:%Zu chunk:%p size:%d\n", __func__,
 204                 packet, packet->size, chunk, chunk->skb ? chunk->skb->len : -1);
 205
 206        switch ((retval = (sctp_packet_append_chunk(packet, chunk)))) {
 207        case SCTP_XMIT_PMTU_FULL:
 208                if (!packet->has_cookie_echo) {
 209                        int error = 0;
 210
 211                        error = sctp_packet_transmit(packet, gfp);
 212                        if (error < 0)
 213                                chunk->skb->sk->sk_err = -error;
 214
 215                        /* If we have an empty packet, then we can NOT ever
 216                         * return PMTU_FULL.
 217                         */
 218                        if (!one_packet)
 219                                retval = sctp_packet_append_chunk(packet,
 220                                                                  chunk);
 221                }
 222                break;
 223
 224        case SCTP_XMIT_RWND_FULL:
 225        case SCTP_XMIT_OK:
 226        case SCTP_XMIT_NAGLE_DELAY:
 227                break;
 228        }
 229
 230        return retval;
 231}
 232
 233/* Try to bundle an auth chunk into the packet. */
 234static sctp_xmit_t sctp_packet_bundle_auth(struct sctp_packet *pkt,
 235                                           struct sctp_chunk *chunk)
 236{
 237        struct sctp_association *asoc = pkt->transport->asoc;
 238        struct sctp_chunk *auth;
 239        sctp_xmit_t retval = SCTP_XMIT_OK;
 240
 241        /* if we don't have an association, we can't do authentication */
 242        if (!asoc)
 243                return retval;
 244
 245        /* See if this is an auth chunk we are bundling or if
 246         * auth is already bundled.
 247         */
 248        if (chunk->chunk_hdr->type == SCTP_CID_AUTH || pkt->has_auth)
 249                return retval;
 250
 251        /* if the peer did not request this chunk to be authenticated,
 252         * don't do it
 253         */
 254        if (!chunk->auth)
 255                return retval;
 256
 257        auth = sctp_make_auth(asoc);
 258        if (!auth)
 259                return retval;
 260
 261        retval = __sctp_packet_append_chunk(pkt, auth);
 262
 263        if (retval != SCTP_XMIT_OK)
 264                sctp_chunk_free(auth);
 265
 266        return retval;
 267}
 268
 269/* Try to bundle a SACK with the packet. */
 270static sctp_xmit_t sctp_packet_bundle_sack(struct sctp_packet *pkt,
 271                                           struct sctp_chunk *chunk)
 272{
 273        sctp_xmit_t retval = SCTP_XMIT_OK;
 274
 275        /* If sending DATA and haven't aleady bundled a SACK, try to
 276         * bundle one in to the packet.
 277         */
 278        if (sctp_chunk_is_data(chunk) && !pkt->has_sack &&
 279            !pkt->has_cookie_echo) {
 280                struct sctp_association *asoc;
 281                struct timer_list *timer;
 282                asoc = pkt->transport->asoc;
 283                timer = &asoc->timers[SCTP_EVENT_TIMEOUT_SACK];
 284
 285                /* If the SACK timer is running, we have a pending SACK */
 286                if (timer_pending(timer)) {
 287                        struct sctp_chunk *sack;
 288
 289                        if (pkt->transport->sack_generation !=
 290                            pkt->transport->asoc->peer.sack_generation)
 291                                return retval;
 292
 293                        asoc->a_rwnd = asoc->rwnd;
 294                        sack = sctp_make_sack(asoc);
 295                        if (sack) {
 296                                retval = __sctp_packet_append_chunk(pkt, sack);
 297                                if (retval != SCTP_XMIT_OK) {
 298                                        sctp_chunk_free(sack);
 299                                        goto out;
 300                                }
 301                                SCTP_INC_STATS(sock_net(asoc->base.sk),
 302                                               SCTP_MIB_OUTCTRLCHUNKS);
 303                                asoc->stats.octrlchunks++;
 304                                asoc->peer.sack_needed = 0;
 305                                if (del_timer(timer))
 306                                        sctp_association_put(asoc);
 307                        }
 308                }
 309        }
 310out:
 311        return retval;
 312}
 313
 314
 315/* Append a chunk to the offered packet reporting back any inability to do
 316 * so.
 317 */
 318static sctp_xmit_t __sctp_packet_append_chunk(struct sctp_packet *packet,
 319                                              struct sctp_chunk *chunk)
 320{
 321        sctp_xmit_t retval = SCTP_XMIT_OK;
 322        __u16 chunk_len = SCTP_PAD4(ntohs(chunk->chunk_hdr->length));
 323
 324        /* Check to see if this chunk will fit into the packet */
 325        retval = sctp_packet_will_fit(packet, chunk, chunk_len);
 326        if (retval != SCTP_XMIT_OK)
 327                goto finish;
 328
 329        /* We believe that this chunk is OK to add to the packet */
 330        switch (chunk->chunk_hdr->type) {
 331        case SCTP_CID_DATA:
 332                /* Account for the data being in the packet */
 333                sctp_packet_append_data(packet, chunk);
 334                /* Disallow SACK bundling after DATA. */
 335                packet->has_sack = 1;
 336                /* Disallow AUTH bundling after DATA */
 337                packet->has_auth = 1;
 338                /* Let it be knows that packet has DATA in it */
 339                packet->has_data = 1;
 340                /* timestamp the chunk for rtx purposes */
 341                chunk->sent_at = jiffies;
 342                /* Mainly used for prsctp RTX policy */
 343                chunk->sent_count++;
 344                break;
 345        case SCTP_CID_COOKIE_ECHO:
 346                packet->has_cookie_echo = 1;
 347                break;
 348
 349        case SCTP_CID_SACK:
 350                packet->has_sack = 1;
 351                if (chunk->asoc)
 352                        chunk->asoc->stats.osacks++;
 353                break;
 354
 355        case SCTP_CID_AUTH:
 356                packet->has_auth = 1;
 357                packet->auth = chunk;
 358                break;
 359        }
 360
 361        /* It is OK to send this chunk.  */
 362        list_add_tail(&chunk->list, &packet->chunk_list);
 363        packet->size += chunk_len;
 364        chunk->transport = packet->transport;
 365finish:
 366        return retval;
 367}
 368
 369/* Append a chunk to the offered packet reporting back any inability to do
 370 * so.
 371 */
 372sctp_xmit_t sctp_packet_append_chunk(struct sctp_packet *packet,
 373                                     struct sctp_chunk *chunk)
 374{
 375        sctp_xmit_t retval = SCTP_XMIT_OK;
 376
 377        pr_debug("%s: packet:%p chunk:%p\n", __func__, packet, chunk);
 378
 379        /* Data chunks are special.  Before seeing what else we can
 380         * bundle into this packet, check to see if we are allowed to
 381         * send this DATA.
 382         */
 383        if (sctp_chunk_is_data(chunk)) {
 384                retval = sctp_packet_can_append_data(packet, chunk);
 385                if (retval != SCTP_XMIT_OK)
 386                        goto finish;
 387        }
 388
 389        /* Try to bundle AUTH chunk */
 390        retval = sctp_packet_bundle_auth(packet, chunk);
 391        if (retval != SCTP_XMIT_OK)
 392                goto finish;
 393
 394        /* Try to bundle SACK chunk */
 395        retval = sctp_packet_bundle_sack(packet, chunk);
 396        if (retval != SCTP_XMIT_OK)
 397                goto finish;
 398
 399        retval = __sctp_packet_append_chunk(packet, chunk);
 400
 401finish:
 402        return retval;
 403}
 404
 405static void sctp_packet_release_owner(struct sk_buff *skb)
 406{
 407        sk_free(skb->sk);
 408}
 409
 410static void sctp_packet_set_owner_w(struct sk_buff *skb, struct sock *sk)
 411{
 412        skb_orphan(skb);
 413        skb->sk = sk;
 414        skb->destructor = sctp_packet_release_owner;
 415
 416        /*
 417         * The data chunks have already been accounted for in sctp_sendmsg(),
 418         * therefore only reserve a single byte to keep socket around until
 419         * the packet has been transmitted.
 420         */
 421        atomic_inc(&sk->sk_wmem_alloc);
 422}
 423
 424/* All packets are sent to the network through this function from
 425 * sctp_outq_tail().
 426 *
 427 * The return value is a normal kernel error return value.
 428 */
 429int sctp_packet_transmit(struct sctp_packet *packet, gfp_t gfp)
 430{
 431        struct sctp_transport *tp = packet->transport;
 432        struct sctp_association *asoc = tp->asoc;
 433        struct sctphdr *sh;
 434        struct sk_buff *nskb = NULL, *head = NULL;
 435        struct sctp_chunk *chunk, *tmp;
 436        struct sock *sk;
 437        int err = 0;
 438        int padding;            /* How much padding do we need?  */
 439        int pkt_size;
 440        __u8 has_data = 0;
 441        int gso = 0;
 442        int pktcount = 0;
 443        int auth_len = 0;
 444        int confirm;
 445        struct dst_entry *dst;
 446        unsigned char *auth = NULL;     /* pointer to auth in skb data */
 447
 448        pr_debug("%s: packet:%p\n", __func__, packet);
 449
 450        /* Do NOT generate a chunkless packet. */
 451        if (list_empty(&packet->chunk_list))
 452                return err;
 453
 454        /* Set up convenience variables... */
 455        chunk = list_entry(packet->chunk_list.next, struct sctp_chunk, list);
 456        sk = chunk->skb->sk;
 457
 458        /* Allocate the head skb, or main one if not in GSO */
 459        if (packet->size > tp->pathmtu && !packet->ipfragok) {
 460                if (sk_can_gso(sk)) {
 461                        gso = 1;
 462                        pkt_size = packet->overhead;
 463                } else {
 464                        /* If this happens, we trash this packet and try
 465                         * to build a new one, hopefully correct this
 466                         * time. Application may notice this error.
 467                         */
 468                        pr_err_once("Trying to GSO but underlying device doesn't support it.");
 469                        goto err;
 470                }
 471        } else {
 472                pkt_size = packet->size;
 473        }
 474        head = alloc_skb(pkt_size + MAX_HEADER, gfp);
 475        if (!head)
 476                goto err;
 477        if (gso) {
 478                NAPI_GRO_CB(head)->last = head;
 479                skb_shinfo(head)->gso_type = sk->sk_gso_type;
 480        }
 481
 482        /* Make sure the outbound skb has enough header room reserved. */
 483        skb_reserve(head, packet->overhead + MAX_HEADER);
 484
 485        /* Set the owning socket so that we know where to get the
 486         * destination IP address.
 487         */
 488        sctp_packet_set_owner_w(head, sk);
 489
 490        dst = dst_clone(tp->dst);
 491        if (!dst) {
 492                if (asoc)
 493                        IP_INC_STATS(sock_net(asoc->base.sk),
 494                                     IPSTATS_MIB_OUTNOROUTES);
 495                goto nodst;
 496        }
 497        skb_dst_set(head, dst);
 498
 499        /* Build the SCTP header.  */
 500        sh = skb_push(head, sizeof(struct sctphdr));
 501        skb_reset_transport_header(head);
 502        sh->source = htons(packet->source_port);
 503        sh->dest   = htons(packet->destination_port);
 504
 505        /* From 6.8 Adler-32 Checksum Calculation:
 506         * After the packet is constructed (containing the SCTP common
 507         * header and one or more control or DATA chunks), the
 508         * transmitter shall:
 509         *
 510         * 1) Fill in the proper Verification Tag in the SCTP common
 511         *    header and initialize the checksum field to 0's.
 512         */
 513        sh->vtag     = htonl(packet->vtag);
 514        sh->checksum = 0;
 515
 516        pr_debug("***sctp_transmit_packet***\n");
 517
 518        do {
 519                /* Set up convenience variables... */
 520                chunk = list_entry(packet->chunk_list.next, struct sctp_chunk, list);
 521                pktcount++;
 522
 523                /* Calculate packet size, so it fits in PMTU. Leave
 524                 * other chunks for the next packets.
 525                 */
 526                if (gso) {
 527                        pkt_size = packet->overhead;
 528                        list_for_each_entry(chunk, &packet->chunk_list, list) {
 529                                int padded = SCTP_PAD4(chunk->skb->len);
 530
 531                                if (chunk == packet->auth)
 532                                        auth_len = padded;
 533                                else if (auth_len + padded + packet->overhead >
 534                                         tp->pathmtu)
 535                                        goto nomem;
 536                                else if (pkt_size + padded > tp->pathmtu)
 537                                        break;
 538                                pkt_size += padded;
 539                        }
 540
 541                        /* Allocate a new skb. */
 542                        nskb = alloc_skb(pkt_size + MAX_HEADER, gfp);
 543                        if (!nskb)
 544                                goto nomem;
 545
 546                        /* Make sure the outbound skb has enough header
 547                         * room reserved.
 548                         */
 549                        skb_reserve(nskb, packet->overhead + MAX_HEADER);
 550                } else {
 551                        nskb = head;
 552                }
 553
 554                /**
 555                 * 3.2  Chunk Field Descriptions
 556                 *
 557                 * The total length of a chunk (including Type, Length and
 558                 * Value fields) MUST be a multiple of 4 bytes.  If the length
 559                 * of the chunk is not a multiple of 4 bytes, the sender MUST
 560                 * pad the chunk with all zero bytes and this padding is not
 561                 * included in the chunk length field.  The sender should
 562                 * never pad with more than 3 bytes.
 563                 *
 564                 * [This whole comment explains SCTP_PAD4() below.]
 565                 */
 566
 567                pkt_size -= packet->overhead;
 568                list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
 569                        list_del_init(&chunk->list);
 570                        if (sctp_chunk_is_data(chunk)) {
 571                                /* 6.3.1 C4) When data is in flight and when allowed
 572                                 * by rule C5, a new RTT measurement MUST be made each
 573                                 * round trip.  Furthermore, new RTT measurements
 574                                 * SHOULD be made no more than once per round-trip
 575                                 * for a given destination transport address.
 576                                 */
 577
 578                                if (!chunk->resent && !tp->rto_pending) {
 579                                        chunk->rtt_in_progress = 1;
 580                                        tp->rto_pending = 1;
 581                                }
 582
 583                                has_data = 1;
 584                        }
 585
 586                        padding = SCTP_PAD4(chunk->skb->len) - chunk->skb->len;
 587                        if (padding)
 588                                skb_put_zero(chunk->skb, padding);
 589
 590                        /* if this is the auth chunk that we are adding,
 591                         * store pointer where it will be added and put
 592                         * the auth into the packet.
 593                         */
 594                        if (chunk == packet->auth)
 595                                auth = skb_tail_pointer(nskb);
 596
 597                        skb_put_data(nskb, chunk->skb->data, chunk->skb->len);
 598
 599                        pr_debug("*** Chunk:%p[%s] %s 0x%x, length:%d, chunk->skb->len:%d, rtt_in_progress:%d\n",
 600                                 chunk,
 601                                 sctp_cname(SCTP_ST_CHUNK(chunk->chunk_hdr->type)),
 602                                 chunk->has_tsn ? "TSN" : "No TSN",
 603                                 chunk->has_tsn ? ntohl(chunk->subh.data_hdr->tsn) : 0,
 604                                 ntohs(chunk->chunk_hdr->length), chunk->skb->len,
 605                                 chunk->rtt_in_progress);
 606
 607                        /* If this is a control chunk, this is our last
 608                         * reference. Free data chunks after they've been
 609                         * acknowledged or have failed.
 610                         * Re-queue auth chunks if needed.
 611                         */
 612                        pkt_size -= SCTP_PAD4(chunk->skb->len);
 613
 614                        if (!sctp_chunk_is_data(chunk) && chunk != packet->auth)
 615                                sctp_chunk_free(chunk);
 616
 617                        if (!pkt_size)
 618                                break;
 619                }
 620
 621                /* SCTP-AUTH, Section 6.2
 622                 *    The sender MUST calculate the MAC as described in RFC2104 [2]
 623                 *    using the hash function H as described by the MAC Identifier and
 624                 *    the shared association key K based on the endpoint pair shared key
 625                 *    described by the shared key identifier.  The 'data' used for the
 626                 *    computation of the AUTH-chunk is given by the AUTH chunk with its
 627                 *    HMAC field set to zero (as shown in Figure 6) followed by all
 628                 *    chunks that are placed after the AUTH chunk in the SCTP packet.
 629                 */
 630                if (auth)
 631                        sctp_auth_calculate_hmac(asoc, nskb,
 632                                                 (struct sctp_auth_chunk *)auth,
 633                                                 gfp);
 634
 635                if (packet->auth) {
 636                        if (!list_empty(&packet->chunk_list)) {
 637                                /* We will generate more packets, so re-queue
 638                                 * auth chunk.
 639                                 */
 640                                list_add(&packet->auth->list,
 641                                         &packet->chunk_list);
 642                        } else {
 643                                sctp_chunk_free(packet->auth);
 644                                packet->auth = NULL;
 645                        }
 646                }
 647
 648                if (!gso)
 649                        break;
 650
 651                if (skb_gro_receive(&head, nskb)) {
 652                        kfree_skb(nskb);
 653                        goto nomem;
 654                }
 655                nskb = NULL;
 656                if (WARN_ON_ONCE(skb_shinfo(head)->gso_segs >=
 657                                 sk->sk_gso_max_segs))
 658                        goto nomem;
 659        } while (!list_empty(&packet->chunk_list));
 660
 661        /* 2) Calculate the Adler-32 checksum of the whole packet,
 662         *    including the SCTP common header and all the
 663         *    chunks.
 664         *
 665         * Note: Adler-32 is no longer applicable, as has been replaced
 666         * by CRC32-C as described in <draft-ietf-tsvwg-sctpcsum-02.txt>.
 667         *
 668         * If it's a GSO packet, it's postponed to sctp_skb_segment.
 669         */
 670        if (!sctp_checksum_disable || gso) {
 671                if (!gso && (!(dst->dev->features & NETIF_F_SCTP_CRC) ||
 672                             dst_xfrm(dst) || packet->ipfragok)) {
 673                        sh->checksum = sctp_compute_cksum(head, 0);
 674                } else {
 675                        /* no need to seed pseudo checksum for SCTP */
 676                        head->ip_summed = CHECKSUM_PARTIAL;
 677                        head->csum_not_inet = 1;
 678                        head->csum_start = skb_transport_header(head) - head->head;
 679                        head->csum_offset = offsetof(struct sctphdr, checksum);
 680                }
 681        }
 682
 683        /* IP layer ECN support
 684         * From RFC 2481
 685         *  "The ECN-Capable Transport (ECT) bit would be set by the
 686         *   data sender to indicate that the end-points of the
 687         *   transport protocol are ECN-capable."
 688         *
 689         * Now setting the ECT bit all the time, as it should not cause
 690         * any problems protocol-wise even if our peer ignores it.
 691         *
 692         * Note: The works for IPv6 layer checks this bit too later
 693         * in transmission.  See IP6_ECN_flow_xmit().
 694         */
 695        tp->af_specific->ecn_capable(sk);
 696
 697        /* Set up the IP options.  */
 698        /* BUG: not implemented
 699         * For v4 this all lives somewhere in sk->sk_opt...
 700         */
 701
 702        /* Dump that on IP!  */
 703        if (asoc) {
 704                asoc->stats.opackets += pktcount;
 705                if (asoc->peer.last_sent_to != tp)
 706                        /* Considering the multiple CPU scenario, this is a
 707                         * "correcter" place for last_sent_to.  --xguo
 708                         */
 709                        asoc->peer.last_sent_to = tp;
 710        }
 711
 712        if (has_data) {
 713                struct timer_list *timer;
 714                unsigned long timeout;
 715
 716                /* Restart the AUTOCLOSE timer when sending data. */
 717                if (sctp_state(asoc, ESTABLISHED) &&
 718                    asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
 719                        timer = &asoc->timers[SCTP_EVENT_TIMEOUT_AUTOCLOSE];
 720                        timeout = asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE];
 721
 722                        if (!mod_timer(timer, jiffies + timeout))
 723                                sctp_association_hold(asoc);
 724                }
 725        }
 726
 727        pr_debug("***sctp_transmit_packet*** skb->len:%d\n", head->len);
 728
 729        if (gso) {
 730                /* Cleanup our debris for IP stacks */
 731                memset(head->cb, 0, max(sizeof(struct inet_skb_parm),
 732                                        sizeof(struct inet6_skb_parm)));
 733                skb_shinfo(head)->gso_segs = pktcount;
 734                skb_shinfo(head)->gso_size = GSO_BY_FRAGS;
 735
 736                /* We have to refresh this in case we are xmiting to
 737                 * more than one transport at a time
 738                 */
 739                rcu_read_lock();
 740                if (__sk_dst_get(sk) != tp->dst) {
 741                        dst_hold(tp->dst);
 742                        sk_setup_caps(sk, tp->dst);
 743                }
 744                rcu_read_unlock();
 745        }
 746        head->ignore_df = packet->ipfragok;
 747        confirm = tp->dst_pending_confirm;
 748        if (confirm)
 749                skb_set_dst_pending_confirm(head, 1);
 750        /* neighbour should be confirmed on successful transmission or
 751         * positive error
 752         */
 753        if (tp->af_specific->sctp_xmit(head, tp) >= 0 && confirm)
 754                tp->dst_pending_confirm = 0;
 755        goto out;
 756
 757nomem:
 758        if (packet->auth && list_empty(&packet->auth->list))
 759                sctp_chunk_free(packet->auth);
 760
 761nodst:
 762        /* FIXME: Returning the 'err' will effect all the associations
 763         * associated with a socket, although only one of the paths of the
 764         * association is unreachable.
 765         * The real failure of a transport or association can be passed on
 766         * to the user via notifications. So setting this error may not be
 767         * required.
 768         */
 769         /* err = -EHOSTUNREACH; */
 770        kfree_skb(head);
 771
 772err:
 773        list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
 774                list_del_init(&chunk->list);
 775                if (!sctp_chunk_is_data(chunk))
 776                        sctp_chunk_free(chunk);
 777        }
 778
 779out:
 780        sctp_packet_reset(packet);
 781        return err;
 782}
 783
 784/********************************************************************
 785 * 2nd Level Abstractions
 786 ********************************************************************/
 787
 788/* This private function check to see if a chunk can be added */
 789static sctp_xmit_t sctp_packet_can_append_data(struct sctp_packet *packet,
 790                                           struct sctp_chunk *chunk)
 791{
 792        size_t datasize, rwnd, inflight, flight_size;
 793        struct sctp_transport *transport = packet->transport;
 794        struct sctp_association *asoc = transport->asoc;
 795        struct sctp_outq *q = &asoc->outqueue;
 796
 797        /* RFC 2960 6.1  Transmission of DATA Chunks
 798         *
 799         * A) At any given time, the data sender MUST NOT transmit new data to
 800         * any destination transport address if its peer's rwnd indicates
 801         * that the peer has no buffer space (i.e. rwnd is 0, see Section
 802         * 6.2.1).  However, regardless of the value of rwnd (including if it
 803         * is 0), the data sender can always have one DATA chunk in flight to
 804         * the receiver if allowed by cwnd (see rule B below).  This rule
 805         * allows the sender to probe for a change in rwnd that the sender
 806         * missed due to the SACK having been lost in transit from the data
 807         * receiver to the data sender.
 808         */
 809
 810        rwnd = asoc->peer.rwnd;
 811        inflight = q->outstanding_bytes;
 812        flight_size = transport->flight_size;
 813
 814        datasize = sctp_data_size(chunk);
 815
 816        if (datasize > rwnd && inflight > 0)
 817                /* We have (at least) one data chunk in flight,
 818                 * so we can't fall back to rule 6.1 B).
 819                 */
 820                return SCTP_XMIT_RWND_FULL;
 821
 822        /* RFC 2960 6.1  Transmission of DATA Chunks
 823         *
 824         * B) At any given time, the sender MUST NOT transmit new data
 825         * to a given transport address if it has cwnd or more bytes
 826         * of data outstanding to that transport address.
 827         */
 828        /* RFC 7.2.4 & the Implementers Guide 2.8.
 829         *
 830         * 3) ...
 831         *    When a Fast Retransmit is being performed the sender SHOULD
 832         *    ignore the value of cwnd and SHOULD NOT delay retransmission.
 833         */
 834        if (chunk->fast_retransmit != SCTP_NEED_FRTX &&
 835            flight_size >= transport->cwnd)
 836                return SCTP_XMIT_RWND_FULL;
 837
 838        /* Nagle's algorithm to solve small-packet problem:
 839         * Inhibit the sending of new chunks when new outgoing data arrives
 840         * if any previously transmitted data on the connection remains
 841         * unacknowledged.
 842         */
 843
 844        if ((sctp_sk(asoc->base.sk)->nodelay || inflight == 0) &&
 845            !asoc->force_delay)
 846                /* Nothing unacked */
 847                return SCTP_XMIT_OK;
 848
 849        if (!sctp_packet_empty(packet))
 850                /* Append to packet */
 851                return SCTP_XMIT_OK;
 852
 853        if (!sctp_state(asoc, ESTABLISHED))
 854                return SCTP_XMIT_OK;
 855
 856        /* Check whether this chunk and all the rest of pending data will fit
 857         * or delay in hopes of bundling a full sized packet.
 858         */
 859        if (chunk->skb->len + q->out_qlen >
 860                transport->pathmtu - packet->overhead - sizeof(sctp_data_chunk_t) - 4)
 861                /* Enough data queued to fill a packet */
 862                return SCTP_XMIT_OK;
 863
 864        /* Don't delay large message writes that may have been fragmented */
 865        if (!chunk->msg->can_delay)
 866                return SCTP_XMIT_OK;
 867
 868        /* Defer until all data acked or packet full */
 869        return SCTP_XMIT_NAGLE_DELAY;
 870}
 871
 872/* This private function does management things when adding DATA chunk */
 873static void sctp_packet_append_data(struct sctp_packet *packet,
 874                                struct sctp_chunk *chunk)
 875{
 876        struct sctp_transport *transport = packet->transport;
 877        size_t datasize = sctp_data_size(chunk);
 878        struct sctp_association *asoc = transport->asoc;
 879        u32 rwnd = asoc->peer.rwnd;
 880
 881        /* Keep track of how many bytes are in flight over this transport. */
 882        transport->flight_size += datasize;
 883
 884        /* Keep track of how many bytes are in flight to the receiver. */
 885        asoc->outqueue.outstanding_bytes += datasize;
 886
 887        /* Update our view of the receiver's rwnd. */
 888        if (datasize < rwnd)
 889                rwnd -= datasize;
 890        else
 891                rwnd = 0;
 892
 893        asoc->peer.rwnd = rwnd;
 894        sctp_chunk_assign_tsn(chunk);
 895        sctp_chunk_assign_ssn(chunk);
 896}
 897
 898static sctp_xmit_t sctp_packet_will_fit(struct sctp_packet *packet,
 899                                        struct sctp_chunk *chunk,
 900                                        u16 chunk_len)
 901{
 902        size_t psize, pmtu, maxsize;
 903        sctp_xmit_t retval = SCTP_XMIT_OK;
 904
 905        psize = packet->size;
 906        if (packet->transport->asoc)
 907                pmtu = packet->transport->asoc->pathmtu;
 908        else
 909                pmtu = packet->transport->pathmtu;
 910
 911        /* Decide if we need to fragment or resubmit later. */
 912        if (psize + chunk_len > pmtu) {
 913                /* It's OK to fragment at IP level if any one of the following
 914                 * is true:
 915                 *      1. The packet is empty (meaning this chunk is greater
 916                 *         the MTU)
 917                 *      2. The packet doesn't have any data in it yet and data
 918                 *         requires authentication.
 919                 */
 920                if (sctp_packet_empty(packet) ||
 921                    (!packet->has_data && chunk->auth)) {
 922                        /* We no longer do re-fragmentation.
 923                         * Just fragment at the IP layer, if we
 924                         * actually hit this condition
 925                         */
 926                        packet->ipfragok = 1;
 927                        goto out;
 928                }
 929
 930                /* Similarly, if this chunk was built before a PMTU
 931                 * reduction, we have to fragment it at IP level now. So
 932                 * if the packet already contains something, we need to
 933                 * flush.
 934                 */
 935                maxsize = pmtu - packet->overhead;
 936                if (packet->auth)
 937                        maxsize -= SCTP_PAD4(packet->auth->skb->len);
 938                if (chunk_len > maxsize)
 939                        retval = SCTP_XMIT_PMTU_FULL;
 940
 941                /* It is also okay to fragment if the chunk we are
 942                 * adding is a control chunk, but only if current packet
 943                 * is not a GSO one otherwise it causes fragmentation of
 944                 * a large frame. So in this case we allow the
 945                 * fragmentation by forcing it to be in a new packet.
 946                 */
 947                if (!sctp_chunk_is_data(chunk) && packet->has_data)
 948                        retval = SCTP_XMIT_PMTU_FULL;
 949
 950                if (psize + chunk_len > packet->max_size)
 951                        /* Hit GSO/PMTU limit, gotta flush */
 952                        retval = SCTP_XMIT_PMTU_FULL;
 953
 954                if (!packet->transport->burst_limited &&
 955                    psize + chunk_len > (packet->transport->cwnd >> 1))
 956                        /* Do not allow a single GSO packet to use more
 957                         * than half of cwnd.
 958                         */
 959                        retval = SCTP_XMIT_PMTU_FULL;
 960
 961                if (packet->transport->burst_limited &&
 962                    psize + chunk_len > (packet->transport->burst_limited >> 1))
 963                        /* Do not allow a single GSO packet to use more
 964                         * than half of original cwnd.
 965                         */
 966                        retval = SCTP_XMIT_PMTU_FULL;
 967                /* Otherwise it will fit in the GSO packet */
 968        }
 969
 970out:
 971        return retval;
 972}
 973