]> err.no Git - linux-2.6/blob - net/sctp/output.c
sctp: Follow security requirement of responding with 1 packet
[linux-2.6] / net / sctp / output.c
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 #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 #include <net/sctp/checksum.h>
64
65 /* Forward declarations for private helpers. */
66 static sctp_xmit_t sctp_packet_append_data(struct sctp_packet *packet,
67                                            struct sctp_chunk *chunk);
68
69 /* Config a packet.
70  * This appears to be a followup set of initializations.
71  */
72 struct sctp_packet *sctp_packet_config(struct sctp_packet *packet,
73                                        __u32 vtag, int ecn_capable)
74 {
75         struct sctp_chunk *chunk = NULL;
76
77         SCTP_DEBUG_PRINTK("%s: packet:%p vtag:0x%x\n", __func__,
78                           packet, vtag);
79
80         packet->vtag = vtag;
81         packet->has_cookie_echo = 0;
82         packet->has_sack = 0;
83         packet->has_auth = 0;
84         packet->has_data = 0;
85         packet->ipfragok = 0;
86         packet->auth = NULL;
87
88         if (ecn_capable && sctp_packet_empty(packet)) {
89                 chunk = sctp_get_ecne_prepend(packet->transport->asoc);
90
91                 /* If there a is a prepend chunk stick it on the list before
92                  * any other chunks get appended.
93                  */
94                 if (chunk)
95                         sctp_packet_append_chunk(packet, chunk);
96         }
97
98         return packet;
99 }
100
101 /* Initialize the packet structure. */
102 struct sctp_packet *sctp_packet_init(struct sctp_packet *packet,
103                                      struct sctp_transport *transport,
104                                      __u16 sport, __u16 dport)
105 {
106         struct sctp_association *asoc = transport->asoc;
107         size_t overhead;
108
109         SCTP_DEBUG_PRINTK("%s: packet:%p transport:%p\n", __func__,
110                           packet, transport);
111
112         packet->transport = transport;
113         packet->source_port = sport;
114         packet->destination_port = dport;
115         INIT_LIST_HEAD(&packet->chunk_list);
116         if (asoc) {
117                 struct sctp_sock *sp = sctp_sk(asoc->base.sk);
118                 overhead = sp->pf->af->net_header_len;
119         } else {
120                 overhead = sizeof(struct ipv6hdr);
121         }
122         overhead += sizeof(struct sctphdr);
123         packet->overhead = overhead;
124         packet->size = overhead;
125         packet->vtag = 0;
126         packet->has_cookie_echo = 0;
127         packet->has_sack = 0;
128         packet->has_auth = 0;
129         packet->has_data = 0;
130         packet->ipfragok = 0;
131         packet->malloced = 0;
132         packet->auth = NULL;
133         return packet;
134 }
135
136 /* Free a packet.  */
137 void sctp_packet_free(struct sctp_packet *packet)
138 {
139         struct sctp_chunk *chunk, *tmp;
140
141         SCTP_DEBUG_PRINTK("%s: packet:%p\n", __func__, packet);
142
143         list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
144                 list_del_init(&chunk->list);
145                 sctp_chunk_free(chunk);
146         }
147
148         if (packet->malloced)
149                 kfree(packet);
150 }
151
152 /* This routine tries to append the chunk to the offered packet. If adding
153  * the chunk causes the packet to exceed the path MTU and COOKIE_ECHO chunk
154  * is not present in the packet, it transmits the input packet.
155  * Data can be bundled with a packet containing a COOKIE_ECHO chunk as long
156  * as it can fit in the packet, but any more data that does not fit in this
157  * packet can be sent only after receiving the COOKIE_ACK.
158  */
159 sctp_xmit_t sctp_packet_transmit_chunk(struct sctp_packet *packet,
160                                        struct sctp_chunk *chunk,
161                                        int one_packet)
162 {
163         sctp_xmit_t retval;
164         int error = 0;
165
166         SCTP_DEBUG_PRINTK("%s: packet:%p chunk:%p\n", __func__,
167                           packet, chunk);
168
169         switch ((retval = (sctp_packet_append_chunk(packet, chunk)))) {
170         case SCTP_XMIT_PMTU_FULL:
171                 if (!packet->has_cookie_echo) {
172                         error = sctp_packet_transmit(packet);
173                         if (error < 0)
174                                 chunk->skb->sk->sk_err = -error;
175
176                         /* If we have an empty packet, then we can NOT ever
177                          * return PMTU_FULL.
178                          */
179                         if (!one_packet)
180                                 retval = sctp_packet_append_chunk(packet,
181                                                                   chunk);
182                 }
183                 break;
184
185         case SCTP_XMIT_RWND_FULL:
186         case SCTP_XMIT_OK:
187         case SCTP_XMIT_NAGLE_DELAY:
188                 break;
189         }
190
191         return retval;
192 }
193
194 /* Try to bundle an auth chunk into the packet. */
195 static sctp_xmit_t sctp_packet_bundle_auth(struct sctp_packet *pkt,
196                                            struct sctp_chunk *chunk)
197 {
198         struct sctp_association *asoc = pkt->transport->asoc;
199         struct sctp_chunk *auth;
200         sctp_xmit_t retval = SCTP_XMIT_OK;
201
202         /* if we don't have an association, we can't do authentication */
203         if (!asoc)
204                 return retval;
205
206         /* See if this is an auth chunk we are bundling or if
207          * auth is already bundled.
208          */
209         if (chunk->chunk_hdr->type == SCTP_CID_AUTH || pkt->auth)
210                 return retval;
211
212         /* if the peer did not request this chunk to be authenticated,
213          * don't do it
214          */
215         if (!chunk->auth)
216                 return retval;
217
218         auth = sctp_make_auth(asoc);
219         if (!auth)
220                 return retval;
221
222         retval = sctp_packet_append_chunk(pkt, auth);
223
224         return retval;
225 }
226
227 /* Try to bundle a SACK with the packet. */
228 static sctp_xmit_t sctp_packet_bundle_sack(struct sctp_packet *pkt,
229                                            struct sctp_chunk *chunk)
230 {
231         sctp_xmit_t retval = SCTP_XMIT_OK;
232
233         /* If sending DATA and haven't aleady bundled a SACK, try to
234          * bundle one in to the packet.
235          */
236         if (sctp_chunk_is_data(chunk) && !pkt->has_sack &&
237             !pkt->has_cookie_echo) {
238                 struct sctp_association *asoc;
239                 asoc = pkt->transport->asoc;
240
241                 if (asoc->a_rwnd > asoc->rwnd) {
242                         struct sctp_chunk *sack;
243                         asoc->a_rwnd = asoc->rwnd;
244                         sack = sctp_make_sack(asoc);
245                         if (sack) {
246                                 struct timer_list *timer;
247                                 retval = sctp_packet_append_chunk(pkt, sack);
248                                 asoc->peer.sack_needed = 0;
249                                 timer = &asoc->timers[SCTP_EVENT_TIMEOUT_SACK];
250                                 if (timer_pending(timer) && del_timer(timer))
251                                         sctp_association_put(asoc);
252                         }
253                 }
254         }
255         return retval;
256 }
257
258 /* Append a chunk to the offered packet reporting back any inability to do
259  * so.
260  */
261 sctp_xmit_t sctp_packet_append_chunk(struct sctp_packet *packet,
262                                      struct sctp_chunk *chunk)
263 {
264         sctp_xmit_t retval = SCTP_XMIT_OK;
265         __u16 chunk_len = WORD_ROUND(ntohs(chunk->chunk_hdr->length));
266         size_t psize;
267         size_t pmtu;
268         int too_big;
269
270         SCTP_DEBUG_PRINTK("%s: packet:%p chunk:%p\n", __func__, packet,
271                           chunk);
272
273         /* Try to bundle AUTH chunk */
274         retval = sctp_packet_bundle_auth(packet, chunk);
275         if (retval != SCTP_XMIT_OK)
276                 goto finish;
277
278         /* Try to bundle SACK chunk */
279         retval = sctp_packet_bundle_sack(packet, chunk);
280         if (retval != SCTP_XMIT_OK)
281                 goto finish;
282
283         psize = packet->size;
284         pmtu  = ((packet->transport->asoc) ?
285                  (packet->transport->asoc->pathmtu) :
286                  (packet->transport->pathmtu));
287
288         too_big = (psize + chunk_len > pmtu);
289
290         /* Decide if we need to fragment or resubmit later. */
291         if (too_big) {
292                 /* It's OK to fragmet at IP level if any one of the following
293                  * is true:
294                  *      1. The packet is empty (meaning this chunk is greater
295                  *         the MTU)
296                  *      2. The chunk we are adding is a control chunk
297                  *      3. The packet doesn't have any data in it yet and data
298                  *      requires authentication.
299                  */
300                 if (sctp_packet_empty(packet) || !sctp_chunk_is_data(chunk) ||
301                     (!packet->has_data && chunk->auth)) {
302                         /* We no longer do re-fragmentation.
303                          * Just fragment at the IP layer, if we
304                          * actually hit this condition
305                          */
306                         packet->ipfragok = 1;
307                         goto append;
308
309                 } else {
310                         retval = SCTP_XMIT_PMTU_FULL;
311                         goto finish;
312                 }
313         }
314
315 append:
316         /* We believe that this chunk is OK to add to the packet (as
317          * long as we have the cwnd for it).
318          */
319
320         /* DATA is a special case since we must examine both rwnd and cwnd
321          * before we send DATA.
322          */
323         switch (chunk->chunk_hdr->type) {
324             case SCTP_CID_DATA:
325                 retval = sctp_packet_append_data(packet, chunk);
326                 /* Disallow SACK bundling after DATA. */
327                 packet->has_sack = 1;
328                 /* Disallow AUTH bundling after DATA */
329                 packet->has_auth = 1;
330                 /* Let it be knows that packet has DATA in it */
331                 packet->has_data = 1;
332                 if (SCTP_XMIT_OK != retval)
333                         goto finish;
334                 break;
335             case SCTP_CID_COOKIE_ECHO:
336                 packet->has_cookie_echo = 1;
337                 break;
338
339             case SCTP_CID_SACK:
340                 packet->has_sack = 1;
341                 break;
342
343             case SCTP_CID_AUTH:
344                 packet->has_auth = 1;
345                 packet->auth = chunk;
346                 break;
347         }
348
349         /* It is OK to send this chunk.  */
350         list_add_tail(&chunk->list, &packet->chunk_list);
351         packet->size += chunk_len;
352         chunk->transport = packet->transport;
353 finish:
354         return retval;
355 }
356
357 /* All packets are sent to the network through this function from
358  * sctp_outq_tail().
359  *
360  * The return value is a normal kernel error return value.
361  */
362 int sctp_packet_transmit(struct sctp_packet *packet)
363 {
364         struct sctp_transport *tp = packet->transport;
365         struct sctp_association *asoc = tp->asoc;
366         struct sctphdr *sh;
367         __u32 crc32 = 0;
368         struct sk_buff *nskb;
369         struct sctp_chunk *chunk, *tmp;
370         struct sock *sk;
371         int err = 0;
372         int padding;            /* How much padding do we need?  */
373         __u8 has_data = 0;
374         struct dst_entry *dst = tp->dst;
375         unsigned char *auth = NULL;     /* pointer to auth in skb data */
376         __u32 cksum_buf_len = sizeof(struct sctphdr);
377
378         SCTP_DEBUG_PRINTK("%s: packet:%p\n", __func__, packet);
379
380         /* Do NOT generate a chunkless packet. */
381         if (list_empty(&packet->chunk_list))
382                 return err;
383
384         /* Set up convenience variables... */
385         chunk = list_entry(packet->chunk_list.next, struct sctp_chunk, list);
386         sk = chunk->skb->sk;
387
388         /* Allocate the new skb.  */
389         nskb = alloc_skb(packet->size + LL_MAX_HEADER, GFP_ATOMIC);
390         if (!nskb)
391                 goto nomem;
392
393         /* Make sure the outbound skb has enough header room reserved. */
394         skb_reserve(nskb, packet->overhead + LL_MAX_HEADER);
395
396         /* Set the owning socket so that we know where to get the
397          * destination IP address.
398          */
399         skb_set_owner_w(nskb, sk);
400
401         /* The 'obsolete' field of dst is set to 2 when a dst is freed. */
402         if (!dst || (dst->obsolete > 1)) {
403                 dst_release(dst);
404                 sctp_transport_route(tp, NULL, sctp_sk(sk));
405                 if (asoc && (asoc->param_flags & SPP_PMTUD_ENABLE)) {
406                         sctp_assoc_sync_pmtu(asoc);
407                 }
408         }
409         nskb->dst = dst_clone(tp->dst);
410         if (!nskb->dst)
411                 goto no_route;
412         dst = nskb->dst;
413
414         /* Build the SCTP header.  */
415         sh = (struct sctphdr *)skb_push(nskb, sizeof(struct sctphdr));
416         sh->source = htons(packet->source_port);
417         sh->dest   = htons(packet->destination_port);
418
419         /* From 6.8 Adler-32 Checksum Calculation:
420          * After the packet is constructed (containing the SCTP common
421          * header and one or more control or DATA chunks), the
422          * transmitter shall:
423          *
424          * 1) Fill in the proper Verification Tag in the SCTP common
425          *    header and initialize the checksum field to 0's.
426          */
427         sh->vtag     = htonl(packet->vtag);
428         sh->checksum = 0;
429
430         /**
431          * 6.10 Bundling
432          *
433          *    An endpoint bundles chunks by simply including multiple
434          *    chunks in one outbound SCTP packet.  ...
435          */
436
437         /**
438          * 3.2  Chunk Field Descriptions
439          *
440          * The total length of a chunk (including Type, Length and
441          * Value fields) MUST be a multiple of 4 bytes.  If the length
442          * of the chunk is not a multiple of 4 bytes, the sender MUST
443          * pad the chunk with all zero bytes and this padding is not
444          * included in the chunk length field.  The sender should
445          * never pad with more than 3 bytes.
446          *
447          * [This whole comment explains WORD_ROUND() below.]
448          */
449         SCTP_DEBUG_PRINTK("***sctp_transmit_packet***\n");
450         list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
451                 list_del_init(&chunk->list);
452                 if (sctp_chunk_is_data(chunk)) {
453
454                         if (!chunk->has_tsn) {
455                                 sctp_chunk_assign_ssn(chunk);
456                                 sctp_chunk_assign_tsn(chunk);
457
458                         /* 6.3.1 C4) When data is in flight and when allowed
459                          * by rule C5, a new RTT measurement MUST be made each
460                          * round trip.  Furthermore, new RTT measurements
461                          * SHOULD be made no more than once per round-trip
462                          * for a given destination transport address.
463                          */
464
465                                 if (!tp->rto_pending) {
466                                         chunk->rtt_in_progress = 1;
467                                         tp->rto_pending = 1;
468                                 }
469                         } else
470                                 chunk->resent = 1;
471
472                         chunk->sent_at = jiffies;
473                         has_data = 1;
474                 }
475
476                 padding = WORD_ROUND(chunk->skb->len) - chunk->skb->len;
477                 if (padding)
478                         memset(skb_put(chunk->skb, padding), 0, padding);
479
480                 /* if this is the auth chunk that we are adding,
481                  * store pointer where it will be added and put
482                  * the auth into the packet.
483                  */
484                 if (chunk == packet->auth)
485                         auth = skb_tail_pointer(nskb);
486
487                 cksum_buf_len += chunk->skb->len;
488                 memcpy(skb_put(nskb, chunk->skb->len),
489                                chunk->skb->data, chunk->skb->len);
490
491                 SCTP_DEBUG_PRINTK("%s %p[%s] %s 0x%x, %s %d, %s %d, %s %d\n",
492                                   "*** Chunk", chunk,
493                                   sctp_cname(SCTP_ST_CHUNK(
494                                           chunk->chunk_hdr->type)),
495                                   chunk->has_tsn ? "TSN" : "No TSN",
496                                   chunk->has_tsn ?
497                                   ntohl(chunk->subh.data_hdr->tsn) : 0,
498                                   "length", ntohs(chunk->chunk_hdr->length),
499                                   "chunk->skb->len", chunk->skb->len,
500                                   "rtt_in_progress", chunk->rtt_in_progress);
501
502                 /*
503                  * If this is a control chunk, this is our last
504                  * reference. Free data chunks after they've been
505                  * acknowledged or have failed.
506                  */
507                 if (!sctp_chunk_is_data(chunk))
508                         sctp_chunk_free(chunk);
509         }
510
511         /* SCTP-AUTH, Section 6.2
512          *    The sender MUST calculate the MAC as described in RFC2104 [2]
513          *    using the hash function H as described by the MAC Identifier and
514          *    the shared association key K based on the endpoint pair shared key
515          *    described by the shared key identifier.  The 'data' used for the
516          *    computation of the AUTH-chunk is given by the AUTH chunk with its
517          *    HMAC field set to zero (as shown in Figure 6) followed by all
518          *    chunks that are placed after the AUTH chunk in the SCTP packet.
519          */
520         if (auth)
521                 sctp_auth_calculate_hmac(asoc, nskb,
522                                         (struct sctp_auth_chunk *)auth,
523                                         GFP_ATOMIC);
524
525         /* 2) Calculate the Adler-32 checksum of the whole packet,
526          *    including the SCTP common header and all the
527          *    chunks.
528          *
529          * Note: Adler-32 is no longer applicable, as has been replaced
530          * by CRC32-C as described in <draft-ietf-tsvwg-sctpcsum-02.txt>.
531          */
532         if (!(dst->dev->features & NETIF_F_NO_CSUM)) {
533                 crc32 = sctp_start_cksum((__u8 *)sh, cksum_buf_len);
534                 crc32 = sctp_end_cksum(crc32);
535         }
536
537         /* 3) Put the resultant value into the checksum field in the
538          *    common header, and leave the rest of the bits unchanged.
539          */
540         sh->checksum = htonl(crc32);
541
542         /* IP layer ECN support
543          * From RFC 2481
544          *  "The ECN-Capable Transport (ECT) bit would be set by the
545          *   data sender to indicate that the end-points of the
546          *   transport protocol are ECN-capable."
547          *
548          * Now setting the ECT bit all the time, as it should not cause
549          * any problems protocol-wise even if our peer ignores it.
550          *
551          * Note: The works for IPv6 layer checks this bit too later
552          * in transmission.  See IP6_ECN_flow_xmit().
553          */
554         (*tp->af_specific->ecn_capable)(nskb->sk);
555
556         /* Set up the IP options.  */
557         /* BUG: not implemented
558          * For v4 this all lives somewhere in sk->sk_opt...
559          */
560
561         /* Dump that on IP!  */
562         if (asoc && asoc->peer.last_sent_to != tp) {
563                 /* Considering the multiple CPU scenario, this is a
564                  * "correcter" place for last_sent_to.  --xguo
565                  */
566                 asoc->peer.last_sent_to = tp;
567         }
568
569         if (has_data) {
570                 struct timer_list *timer;
571                 unsigned long timeout;
572
573                 tp->last_time_used = jiffies;
574
575                 /* Restart the AUTOCLOSE timer when sending data. */
576                 if (sctp_state(asoc, ESTABLISHED) && asoc->autoclose) {
577                         timer = &asoc->timers[SCTP_EVENT_TIMEOUT_AUTOCLOSE];
578                         timeout = asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE];
579
580                         if (!mod_timer(timer, jiffies + timeout))
581                                 sctp_association_hold(asoc);
582                 }
583         }
584
585         SCTP_DEBUG_PRINTK("***sctp_transmit_packet*** skb len %d\n",
586                           nskb->len);
587
588         if (tp->param_flags & SPP_PMTUD_ENABLE)
589                 (*tp->af_specific->sctp_xmit)(nskb, tp, packet->ipfragok);
590         else
591                 (*tp->af_specific->sctp_xmit)(nskb, tp, 1);
592
593 out:
594         packet->size = packet->overhead;
595         return err;
596 no_route:
597         kfree_skb(nskb);
598         IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
599
600         /* FIXME: Returning the 'err' will effect all the associations
601          * associated with a socket, although only one of the paths of the
602          * association is unreachable.
603          * The real failure of a transport or association can be passed on
604          * to the user via notifications. So setting this error may not be
605          * required.
606          */
607          /* err = -EHOSTUNREACH; */
608 err:
609         /* Control chunks are unreliable so just drop them.  DATA chunks
610          * will get resent or dropped later.
611          */
612
613         list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
614                 list_del_init(&chunk->list);
615                 if (!sctp_chunk_is_data(chunk))
616                         sctp_chunk_free(chunk);
617         }
618         goto out;
619 nomem:
620         err = -ENOMEM;
621         goto err;
622 }
623
624 /********************************************************************
625  * 2nd Level Abstractions
626  ********************************************************************/
627
628 /* This private function handles the specifics of appending DATA chunks.  */
629 static sctp_xmit_t sctp_packet_append_data(struct sctp_packet *packet,
630                                            struct sctp_chunk *chunk)
631 {
632         sctp_xmit_t retval = SCTP_XMIT_OK;
633         size_t datasize, rwnd, inflight;
634         struct sctp_transport *transport = packet->transport;
635         __u32 max_burst_bytes;
636         struct sctp_association *asoc = transport->asoc;
637         struct sctp_sock *sp = sctp_sk(asoc->base.sk);
638         struct sctp_outq *q = &asoc->outqueue;
639
640         /* RFC 2960 6.1  Transmission of DATA Chunks
641          *
642          * A) At any given time, the data sender MUST NOT transmit new data to
643          * any destination transport address if its peer's rwnd indicates
644          * that the peer has no buffer space (i.e. rwnd is 0, see Section
645          * 6.2.1).  However, regardless of the value of rwnd (including if it
646          * is 0), the data sender can always have one DATA chunk in flight to
647          * the receiver if allowed by cwnd (see rule B below).  This rule
648          * allows the sender to probe for a change in rwnd that the sender
649          * missed due to the SACK having been lost in transit from the data
650          * receiver to the data sender.
651          */
652
653         rwnd = asoc->peer.rwnd;
654         inflight = asoc->outqueue.outstanding_bytes;
655
656         datasize = sctp_data_size(chunk);
657
658         if (datasize > rwnd) {
659                 if (inflight > 0) {
660                         /* We have (at least) one data chunk in flight,
661                          * so we can't fall back to rule 6.1 B).
662                          */
663                         retval = SCTP_XMIT_RWND_FULL;
664                         goto finish;
665                 }
666         }
667
668         /* sctpimpguide-05 2.14.2
669          * D) When the time comes for the sender to
670          * transmit new DATA chunks, the protocol parameter Max.Burst MUST
671          * first be applied to limit how many new DATA chunks may be sent.
672          * The limit is applied by adjusting cwnd as follows:
673          *      if ((flightsize + Max.Burst * MTU) < cwnd)
674          *              cwnd = flightsize + Max.Burst * MTU
675          */
676         max_burst_bytes = asoc->max_burst * asoc->pathmtu;
677         if ((transport->flight_size + max_burst_bytes) < transport->cwnd) {
678                 transport->cwnd = transport->flight_size + max_burst_bytes;
679                 SCTP_DEBUG_PRINTK("%s: cwnd limited by max_burst: "
680                                   "transport: %p, cwnd: %d, "
681                                   "ssthresh: %d, flight_size: %d, "
682                                   "pba: %d\n",
683                                   __func__, transport,
684                                   transport->cwnd,
685                                   transport->ssthresh,
686                                   transport->flight_size,
687                                   transport->partial_bytes_acked);
688         }
689
690         /* RFC 2960 6.1  Transmission of DATA Chunks
691          *
692          * B) At any given time, the sender MUST NOT transmit new data
693          * to a given transport address if it has cwnd or more bytes
694          * of data outstanding to that transport address.
695          */
696         /* RFC 7.2.4 & the Implementers Guide 2.8.
697          *
698          * 3) ...
699          *    When a Fast Retransmit is being performed the sender SHOULD
700          *    ignore the value of cwnd and SHOULD NOT delay retransmission.
701          */
702         if (chunk->fast_retransmit <= 0)
703                 if (transport->flight_size >= transport->cwnd) {
704                         retval = SCTP_XMIT_RWND_FULL;
705                         goto finish;
706                 }
707
708         /* Nagle's algorithm to solve small-packet problem:
709          * Inhibit the sending of new chunks when new outgoing data arrives
710          * if any previously transmitted data on the connection remains
711          * unacknowledged.
712          */
713         if (!sp->nodelay && sctp_packet_empty(packet) &&
714             q->outstanding_bytes && sctp_state(asoc, ESTABLISHED)) {
715                 unsigned len = datasize + q->out_qlen;
716
717                 /* Check whether this chunk and all the rest of pending
718                  * data will fit or delay in hopes of bundling a full
719                  * sized packet.
720                  */
721                 if (len < asoc->frag_point) {
722                         retval = SCTP_XMIT_NAGLE_DELAY;
723                         goto finish;
724                 }
725         }
726
727         /* Keep track of how many bytes are in flight over this transport. */
728         transport->flight_size += datasize;
729
730         /* Keep track of how many bytes are in flight to the receiver. */
731         asoc->outqueue.outstanding_bytes += datasize;
732
733         /* Update our view of the receiver's rwnd. Include sk_buff overhead
734          * while updating peer.rwnd so that it reduces the chances of a
735          * receiver running out of receive buffer space even when receive
736          * window is still open. This can happen when a sender is sending
737          * sending small messages.
738          */
739         datasize += sizeof(struct sk_buff);
740         if (datasize < rwnd)
741                 rwnd -= datasize;
742         else
743                 rwnd = 0;
744
745         asoc->peer.rwnd = rwnd;
746         /* Has been accepted for transmission. */
747         if (!asoc->peer.prsctp_capable)
748                 chunk->msg->can_abandon = 0;
749
750 finish:
751         return retval;
752 }