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