a58d13c2d4439982d20db28045c2b43a9f308006
[muen/linux.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, see
24  * <http://www.gnu.org/licenses/>.
25  *
26  * Please send any bug reports or fixes you make to the
27  * email address(es):
28  *    lksctp developers <linux-sctp@vger.kernel.org>
29  *
30  * Written or modified by:
31  *    La Monte H.P. Yarroll <piggy@acm.org>
32  *    Karl Knutson          <karl@athena.chicago.il.us>
33  *    Jon Grimm             <jgrimm@austin.ibm.com>
34  *    Sridhar Samudrala     <sri@us.ibm.com>
35  */
36
37 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
38
39 #include <linux/types.h>
40 #include <linux/kernel.h>
41 #include <linux/wait.h>
42 #include <linux/time.h>
43 #include <linux/ip.h>
44 #include <linux/ipv6.h>
45 #include <linux/init.h>
46 #include <linux/slab.h>
47 #include <net/inet_ecn.h>
48 #include <net/ip.h>
49 #include <net/icmp.h>
50 #include <net/net_namespace.h>
51
52 #include <linux/socket.h> /* for sa_family_t */
53 #include <net/sock.h>
54
55 #include <net/sctp/sctp.h>
56 #include <net/sctp/sm.h>
57 #include <net/sctp/checksum.h>
58
59 /* Forward declarations for private helpers. */
60 static enum sctp_xmit __sctp_packet_append_chunk(struct sctp_packet *packet,
61                                                  struct sctp_chunk *chunk);
62 static enum sctp_xmit sctp_packet_can_append_data(struct sctp_packet *packet,
63                                                   struct sctp_chunk *chunk);
64 static void sctp_packet_append_data(struct sctp_packet *packet,
65                                     struct sctp_chunk *chunk);
66 static enum sctp_xmit sctp_packet_will_fit(struct sctp_packet *packet,
67                                            struct sctp_chunk *chunk,
68                                            u16 chunk_len);
69
70 static void sctp_packet_reset(struct sctp_packet *packet)
71 {
72         /* sctp_packet_transmit() relies on this to reset size to the
73          * current overhead after sending packets.
74          */
75         packet->size = packet->overhead;
76
77         packet->has_cookie_echo = 0;
78         packet->has_sack = 0;
79         packet->has_data = 0;
80         packet->has_auth = 0;
81         packet->ipfragok = 0;
82         packet->auth = NULL;
83 }
84
85 /* Config a packet.
86  * This appears to be a followup set of initializations.
87  */
88 void sctp_packet_config(struct sctp_packet *packet, __u32 vtag,
89                         int ecn_capable)
90 {
91         struct sctp_transport *tp = packet->transport;
92         struct sctp_association *asoc = tp->asoc;
93         struct sock *sk;
94         size_t overhead = sizeof(struct ipv6hdr) + sizeof(struct sctphdr);
95
96         pr_debug("%s: packet:%p vtag:0x%x\n", __func__, packet, vtag);
97         packet->vtag = vtag;
98
99         /* do the following jobs only once for a flush schedule */
100         if (!sctp_packet_empty(packet))
101                 return;
102
103         /* set packet max_size with pathmtu, then calculate overhead */
104         packet->max_size = tp->pathmtu;
105         if (asoc) {
106                 struct sctp_sock *sp = sctp_sk(asoc->base.sk);
107                 struct sctp_af *af = sp->pf->af;
108
109                 overhead = af->net_header_len +
110                            af->ip_options_len(asoc->base.sk);
111                 overhead += sizeof(struct sctphdr);
112                 packet->overhead = overhead;
113                 packet->size = overhead;
114         } else {
115                 packet->overhead = overhead;
116                 packet->size = overhead;
117                 return;
118         }
119
120         /* update dst or transport pathmtu if in need */
121         sk = asoc->base.sk;
122         if (!sctp_transport_dst_check(tp)) {
123                 sctp_transport_route(tp, NULL, sctp_sk(sk));
124                 if (asoc->param_flags & SPP_PMTUD_ENABLE)
125                         sctp_assoc_sync_pmtu(asoc);
126         } else if (!sctp_transport_pmtu_check(tp)) {
127                 if (asoc->param_flags & SPP_PMTUD_ENABLE)
128                         sctp_assoc_sync_pmtu(asoc);
129         }
130
131         /* If there a is a prepend chunk stick it on the list before
132          * any other chunks get appended.
133          */
134         if (ecn_capable) {
135                 struct sctp_chunk *chunk = sctp_get_ecne_prepend(asoc);
136
137                 if (chunk)
138                         sctp_packet_append_chunk(packet, chunk);
139         }
140
141         if (!tp->dst)
142                 return;
143
144         /* set packet max_size with gso_max_size if gso is enabled*/
145         rcu_read_lock();
146         if (__sk_dst_get(sk) != tp->dst) {
147                 dst_hold(tp->dst);
148                 sk_setup_caps(sk, tp->dst);
149         }
150         packet->max_size = sk_can_gso(sk) ? tp->dst->dev->gso_max_size
151                                           : asoc->pathmtu;
152         rcu_read_unlock();
153 }
154
155 /* Initialize the packet structure. */
156 void sctp_packet_init(struct sctp_packet *packet,
157                       struct sctp_transport *transport,
158                       __u16 sport, __u16 dport)
159 {
160         pr_debug("%s: packet:%p transport:%p\n", __func__, packet, transport);
161
162         packet->transport = transport;
163         packet->source_port = sport;
164         packet->destination_port = dport;
165         INIT_LIST_HEAD(&packet->chunk_list);
166         /* The overhead will be calculated by sctp_packet_config() */
167         packet->overhead = 0;
168         sctp_packet_reset(packet);
169         packet->vtag = 0;
170 }
171
172 /* Free a packet.  */
173 void sctp_packet_free(struct sctp_packet *packet)
174 {
175         struct sctp_chunk *chunk, *tmp;
176
177         pr_debug("%s: packet:%p\n", __func__, packet);
178
179         list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
180                 list_del_init(&chunk->list);
181                 sctp_chunk_free(chunk);
182         }
183 }
184
185 /* This routine tries to append the chunk to the offered packet. If adding
186  * the chunk causes the packet to exceed the path MTU and COOKIE_ECHO chunk
187  * is not present in the packet, it transmits the input packet.
188  * Data can be bundled with a packet containing a COOKIE_ECHO chunk as long
189  * as it can fit in the packet, but any more data that does not fit in this
190  * packet can be sent only after receiving the COOKIE_ACK.
191  */
192 enum sctp_xmit sctp_packet_transmit_chunk(struct sctp_packet *packet,
193                                           struct sctp_chunk *chunk,
194                                           int one_packet, gfp_t gfp)
195 {
196         enum sctp_xmit retval;
197
198         pr_debug("%s: packet:%p size:%zu chunk:%p size:%d\n", __func__,
199                  packet, packet->size, chunk, chunk->skb ? chunk->skb->len : -1);
200
201         switch ((retval = (sctp_packet_append_chunk(packet, chunk)))) {
202         case SCTP_XMIT_PMTU_FULL:
203                 if (!packet->has_cookie_echo) {
204                         int error = 0;
205
206                         error = sctp_packet_transmit(packet, gfp);
207                         if (error < 0)
208                                 chunk->skb->sk->sk_err = -error;
209
210                         /* If we have an empty packet, then we can NOT ever
211                          * return PMTU_FULL.
212                          */
213                         if (!one_packet)
214                                 retval = sctp_packet_append_chunk(packet,
215                                                                   chunk);
216                 }
217                 break;
218
219         case SCTP_XMIT_RWND_FULL:
220         case SCTP_XMIT_OK:
221         case SCTP_XMIT_DELAY:
222                 break;
223         }
224
225         return retval;
226 }
227
228 /* Try to bundle an auth chunk into the packet. */
229 static enum sctp_xmit sctp_packet_bundle_auth(struct sctp_packet *pkt,
230                                               struct sctp_chunk *chunk)
231 {
232         struct sctp_association *asoc = pkt->transport->asoc;
233         enum sctp_xmit retval = SCTP_XMIT_OK;
234         struct sctp_chunk *auth;
235
236         /* if we don't have an association, we can't do authentication */
237         if (!asoc)
238                 return retval;
239
240         /* See if this is an auth chunk we are bundling or if
241          * auth is already bundled.
242          */
243         if (chunk->chunk_hdr->type == SCTP_CID_AUTH || pkt->has_auth)
244                 return retval;
245
246         /* if the peer did not request this chunk to be authenticated,
247          * don't do it
248          */
249         if (!chunk->auth)
250                 return retval;
251
252         auth = sctp_make_auth(asoc);
253         if (!auth)
254                 return retval;
255
256         retval = __sctp_packet_append_chunk(pkt, auth);
257
258         if (retval != SCTP_XMIT_OK)
259                 sctp_chunk_free(auth);
260
261         return retval;
262 }
263
264 /* Try to bundle a SACK with the packet. */
265 static enum sctp_xmit sctp_packet_bundle_sack(struct sctp_packet *pkt,
266                                               struct sctp_chunk *chunk)
267 {
268         enum sctp_xmit retval = SCTP_XMIT_OK;
269
270         /* If sending DATA and haven't aleady bundled a SACK, try to
271          * bundle one in to the packet.
272          */
273         if (sctp_chunk_is_data(chunk) && !pkt->has_sack &&
274             !pkt->has_cookie_echo) {
275                 struct sctp_association *asoc;
276                 struct timer_list *timer;
277                 asoc = pkt->transport->asoc;
278                 timer = &asoc->timers[SCTP_EVENT_TIMEOUT_SACK];
279
280                 /* If the SACK timer is running, we have a pending SACK */
281                 if (timer_pending(timer)) {
282                         struct sctp_chunk *sack;
283
284                         if (pkt->transport->sack_generation !=
285                             pkt->transport->asoc->peer.sack_generation)
286                                 return retval;
287
288                         asoc->a_rwnd = asoc->rwnd;
289                         sack = sctp_make_sack(asoc);
290                         if (sack) {
291                                 retval = __sctp_packet_append_chunk(pkt, sack);
292                                 if (retval != SCTP_XMIT_OK) {
293                                         sctp_chunk_free(sack);
294                                         goto out;
295                                 }
296                                 asoc->peer.sack_needed = 0;
297                                 if (del_timer(timer))
298                                         sctp_association_put(asoc);
299                         }
300                 }
301         }
302 out:
303         return retval;
304 }
305
306
307 /* Append a chunk to the offered packet reporting back any inability to do
308  * so.
309  */
310 static enum sctp_xmit __sctp_packet_append_chunk(struct sctp_packet *packet,
311                                                  struct sctp_chunk *chunk)
312 {
313         __u16 chunk_len = SCTP_PAD4(ntohs(chunk->chunk_hdr->length));
314         enum sctp_xmit retval = SCTP_XMIT_OK;
315
316         /* Check to see if this chunk will fit into the packet */
317         retval = sctp_packet_will_fit(packet, chunk, chunk_len);
318         if (retval != SCTP_XMIT_OK)
319                 goto finish;
320
321         /* We believe that this chunk is OK to add to the packet */
322         switch (chunk->chunk_hdr->type) {
323         case SCTP_CID_DATA:
324         case SCTP_CID_I_DATA:
325                 /* Account for the data being in the packet */
326                 sctp_packet_append_data(packet, chunk);
327                 /* Disallow SACK bundling after DATA. */
328                 packet->has_sack = 1;
329                 /* Disallow AUTH bundling after DATA */
330                 packet->has_auth = 1;
331                 /* Let it be knows that packet has DATA in it */
332                 packet->has_data = 1;
333                 /* timestamp the chunk for rtx purposes */
334                 chunk->sent_at = jiffies;
335                 /* Mainly used for prsctp RTX policy */
336                 chunk->sent_count++;
337                 break;
338         case SCTP_CID_COOKIE_ECHO:
339                 packet->has_cookie_echo = 1;
340                 break;
341
342         case SCTP_CID_SACK:
343                 packet->has_sack = 1;
344                 if (chunk->asoc)
345                         chunk->asoc->stats.osacks++;
346                 break;
347
348         case SCTP_CID_AUTH:
349                 packet->has_auth = 1;
350                 packet->auth = chunk;
351                 break;
352         }
353
354         /* It is OK to send this chunk.  */
355         list_add_tail(&chunk->list, &packet->chunk_list);
356         packet->size += chunk_len;
357         chunk->transport = packet->transport;
358 finish:
359         return retval;
360 }
361
362 /* Append a chunk to the offered packet reporting back any inability to do
363  * so.
364  */
365 enum sctp_xmit sctp_packet_append_chunk(struct sctp_packet *packet,
366                                         struct sctp_chunk *chunk)
367 {
368         enum sctp_xmit retval = SCTP_XMIT_OK;
369
370         pr_debug("%s: packet:%p chunk:%p\n", __func__, packet, chunk);
371
372         /* Data chunks are special.  Before seeing what else we can
373          * bundle into this packet, check to see if we are allowed to
374          * send this DATA.
375          */
376         if (sctp_chunk_is_data(chunk)) {
377                 retval = sctp_packet_can_append_data(packet, chunk);
378                 if (retval != SCTP_XMIT_OK)
379                         goto finish;
380         }
381
382         /* Try to bundle AUTH chunk */
383         retval = sctp_packet_bundle_auth(packet, chunk);
384         if (retval != SCTP_XMIT_OK)
385                 goto finish;
386
387         /* Try to bundle SACK chunk */
388         retval = sctp_packet_bundle_sack(packet, chunk);
389         if (retval != SCTP_XMIT_OK)
390                 goto finish;
391
392         retval = __sctp_packet_append_chunk(packet, chunk);
393
394 finish:
395         return retval;
396 }
397
398 static void sctp_packet_release_owner(struct sk_buff *skb)
399 {
400         sk_free(skb->sk);
401 }
402
403 static void sctp_packet_set_owner_w(struct sk_buff *skb, struct sock *sk)
404 {
405         skb_orphan(skb);
406         skb->sk = sk;
407         skb->destructor = sctp_packet_release_owner;
408
409         /*
410          * The data chunks have already been accounted for in sctp_sendmsg(),
411          * therefore only reserve a single byte to keep socket around until
412          * the packet has been transmitted.
413          */
414         refcount_inc(&sk->sk_wmem_alloc);
415 }
416
417 static int sctp_packet_pack(struct sctp_packet *packet,
418                             struct sk_buff *head, int gso, gfp_t gfp)
419 {
420         struct sctp_transport *tp = packet->transport;
421         struct sctp_auth_chunk *auth = NULL;
422         struct sctp_chunk *chunk, *tmp;
423         int pkt_count = 0, pkt_size;
424         struct sock *sk = head->sk;
425         struct sk_buff *nskb;
426         int auth_len = 0;
427
428         if (gso) {
429                 skb_shinfo(head)->gso_type = sk->sk_gso_type;
430                 NAPI_GRO_CB(head)->last = head;
431         } else {
432                 nskb = head;
433                 pkt_size = packet->size;
434                 goto merge;
435         }
436
437         do {
438                 /* calculate the pkt_size and alloc nskb */
439                 pkt_size = packet->overhead;
440                 list_for_each_entry_safe(chunk, tmp, &packet->chunk_list,
441                                          list) {
442                         int padded = SCTP_PAD4(chunk->skb->len);
443
444                         if (chunk == packet->auth)
445                                 auth_len = padded;
446                         else if (auth_len + padded + packet->overhead >
447                                  tp->pathmtu)
448                                 return 0;
449                         else if (pkt_size + padded > tp->pathmtu)
450                                 break;
451                         pkt_size += padded;
452                 }
453                 nskb = alloc_skb(pkt_size + MAX_HEADER, gfp);
454                 if (!nskb)
455                         return 0;
456                 skb_reserve(nskb, packet->overhead + MAX_HEADER);
457
458 merge:
459                 /* merge chunks into nskb and append nskb into head list */
460                 pkt_size -= packet->overhead;
461                 list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
462                         int padding;
463
464                         list_del_init(&chunk->list);
465                         if (sctp_chunk_is_data(chunk)) {
466                                 if (!sctp_chunk_retransmitted(chunk) &&
467                                     !tp->rto_pending) {
468                                         chunk->rtt_in_progress = 1;
469                                         tp->rto_pending = 1;
470                                 }
471                         }
472
473                         padding = SCTP_PAD4(chunk->skb->len) - chunk->skb->len;
474                         if (padding)
475                                 skb_put_zero(chunk->skb, padding);
476
477                         if (chunk == packet->auth)
478                                 auth = (struct sctp_auth_chunk *)
479                                                         skb_tail_pointer(nskb);
480
481                         skb_put_data(nskb, chunk->skb->data, chunk->skb->len);
482
483                         pr_debug("*** Chunk:%p[%s] %s 0x%x, length:%d, chunk->skb->len:%d, rtt_in_progress:%d\n",
484                                  chunk,
485                                  sctp_cname(SCTP_ST_CHUNK(chunk->chunk_hdr->type)),
486                                  chunk->has_tsn ? "TSN" : "No TSN",
487                                  chunk->has_tsn ? ntohl(chunk->subh.data_hdr->tsn) : 0,
488                                  ntohs(chunk->chunk_hdr->length), chunk->skb->len,
489                                  chunk->rtt_in_progress);
490
491                         pkt_size -= SCTP_PAD4(chunk->skb->len);
492
493                         if (!sctp_chunk_is_data(chunk) && chunk != packet->auth)
494                                 sctp_chunk_free(chunk);
495
496                         if (!pkt_size)
497                                 break;
498                 }
499
500                 if (auth) {
501                         sctp_auth_calculate_hmac(tp->asoc, nskb, auth, gfp);
502                         /* free auth if no more chunks, or add it back */
503                         if (list_empty(&packet->chunk_list))
504                                 sctp_chunk_free(packet->auth);
505                         else
506                                 list_add(&packet->auth->list,
507                                          &packet->chunk_list);
508                 }
509
510                 if (gso) {
511                         if (skb_gro_receive(&head, nskb)) {
512                                 kfree_skb(nskb);
513                                 return 0;
514                         }
515                         if (WARN_ON_ONCE(skb_shinfo(head)->gso_segs >=
516                                          sk->sk_gso_max_segs))
517                                 return 0;
518                 }
519
520                 pkt_count++;
521         } while (!list_empty(&packet->chunk_list));
522
523         if (gso) {
524                 memset(head->cb, 0, max(sizeof(struct inet_skb_parm),
525                                         sizeof(struct inet6_skb_parm)));
526                 skb_shinfo(head)->gso_segs = pkt_count;
527                 skb_shinfo(head)->gso_size = GSO_BY_FRAGS;
528                 rcu_read_lock();
529                 if (skb_dst(head) != tp->dst) {
530                         dst_hold(tp->dst);
531                         sk_setup_caps(sk, tp->dst);
532                 }
533                 rcu_read_unlock();
534                 goto chksum;
535         }
536
537         if (sctp_checksum_disable)
538                 return 1;
539
540         if (!(skb_dst(head)->dev->features & NETIF_F_SCTP_CRC) ||
541             dst_xfrm(skb_dst(head)) || packet->ipfragok) {
542                 struct sctphdr *sh =
543                         (struct sctphdr *)skb_transport_header(head);
544
545                 sh->checksum = sctp_compute_cksum(head, 0);
546         } else {
547 chksum:
548                 head->ip_summed = CHECKSUM_PARTIAL;
549                 head->csum_not_inet = 1;
550                 head->csum_start = skb_transport_header(head) - head->head;
551                 head->csum_offset = offsetof(struct sctphdr, checksum);
552         }
553
554         return pkt_count;
555 }
556
557 /* All packets are sent to the network through this function from
558  * sctp_outq_tail().
559  *
560  * The return value is always 0 for now.
561  */
562 int sctp_packet_transmit(struct sctp_packet *packet, gfp_t gfp)
563 {
564         struct sctp_transport *tp = packet->transport;
565         struct sctp_association *asoc = tp->asoc;
566         struct sctp_chunk *chunk, *tmp;
567         int pkt_count, gso = 0;
568         struct dst_entry *dst;
569         struct sk_buff *head;
570         struct sctphdr *sh;
571         struct sock *sk;
572
573         pr_debug("%s: packet:%p\n", __func__, packet);
574         if (list_empty(&packet->chunk_list))
575                 return 0;
576         chunk = list_entry(packet->chunk_list.next, struct sctp_chunk, list);
577         sk = chunk->skb->sk;
578
579         /* check gso */
580         if (packet->size > tp->pathmtu && !packet->ipfragok) {
581                 if (!sk_can_gso(sk)) {
582                         pr_err_once("Trying to GSO but underlying device doesn't support it.");
583                         goto out;
584                 }
585                 gso = 1;
586         }
587
588         /* alloc head skb */
589         head = alloc_skb((gso ? packet->overhead : packet->size) +
590                          MAX_HEADER, gfp);
591         if (!head)
592                 goto out;
593         skb_reserve(head, packet->overhead + MAX_HEADER);
594         sctp_packet_set_owner_w(head, sk);
595
596         /* set sctp header */
597         sh = skb_push(head, sizeof(struct sctphdr));
598         skb_reset_transport_header(head);
599         sh->source = htons(packet->source_port);
600         sh->dest = htons(packet->destination_port);
601         sh->vtag = htonl(packet->vtag);
602         sh->checksum = 0;
603
604         /* drop packet if no dst */
605         dst = dst_clone(tp->dst);
606         if (!dst) {
607                 IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTNOROUTES);
608                 kfree_skb(head);
609                 goto out;
610         }
611         skb_dst_set(head, dst);
612
613         /* pack up chunks */
614         pkt_count = sctp_packet_pack(packet, head, gso, gfp);
615         if (!pkt_count) {
616                 kfree_skb(head);
617                 goto out;
618         }
619         pr_debug("***sctp_transmit_packet*** skb->len:%d\n", head->len);
620
621         /* start autoclose timer */
622         if (packet->has_data && sctp_state(asoc, ESTABLISHED) &&
623             asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
624                 struct timer_list *timer =
625                         &asoc->timers[SCTP_EVENT_TIMEOUT_AUTOCLOSE];
626                 unsigned long timeout =
627                         asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE];
628
629                 if (!mod_timer(timer, jiffies + timeout))
630                         sctp_association_hold(asoc);
631         }
632
633         /* sctp xmit */
634         tp->af_specific->ecn_capable(sk);
635         if (asoc) {
636                 asoc->stats.opackets += pkt_count;
637                 if (asoc->peer.last_sent_to != tp)
638                         asoc->peer.last_sent_to = tp;
639         }
640         head->ignore_df = packet->ipfragok;
641         if (tp->dst_pending_confirm)
642                 skb_set_dst_pending_confirm(head, 1);
643         /* neighbour should be confirmed on successful transmission or
644          * positive error
645          */
646         if (tp->af_specific->sctp_xmit(head, tp) >= 0 &&
647             tp->dst_pending_confirm)
648                 tp->dst_pending_confirm = 0;
649
650 out:
651         list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
652                 list_del_init(&chunk->list);
653                 if (!sctp_chunk_is_data(chunk))
654                         sctp_chunk_free(chunk);
655         }
656         sctp_packet_reset(packet);
657         return 0;
658 }
659
660 /********************************************************************
661  * 2nd Level Abstractions
662  ********************************************************************/
663
664 /* This private function check to see if a chunk can be added */
665 static enum sctp_xmit sctp_packet_can_append_data(struct sctp_packet *packet,
666                                                   struct sctp_chunk *chunk)
667 {
668         size_t datasize, rwnd, inflight, flight_size;
669         struct sctp_transport *transport = packet->transport;
670         struct sctp_association *asoc = transport->asoc;
671         struct sctp_outq *q = &asoc->outqueue;
672
673         /* RFC 2960 6.1  Transmission of DATA Chunks
674          *
675          * A) At any given time, the data sender MUST NOT transmit new data to
676          * any destination transport address if its peer's rwnd indicates
677          * that the peer has no buffer space (i.e. rwnd is 0, see Section
678          * 6.2.1).  However, regardless of the value of rwnd (including if it
679          * is 0), the data sender can always have one DATA chunk in flight to
680          * the receiver if allowed by cwnd (see rule B below).  This rule
681          * allows the sender to probe for a change in rwnd that the sender
682          * missed due to the SACK having been lost in transit from the data
683          * receiver to the data sender.
684          */
685
686         rwnd = asoc->peer.rwnd;
687         inflight = q->outstanding_bytes;
688         flight_size = transport->flight_size;
689
690         datasize = sctp_data_size(chunk);
691
692         if (datasize > rwnd && inflight > 0)
693                 /* We have (at least) one data chunk in flight,
694                  * so we can't fall back to rule 6.1 B).
695                  */
696                 return SCTP_XMIT_RWND_FULL;
697
698         /* RFC 2960 6.1  Transmission of DATA Chunks
699          *
700          * B) At any given time, the sender MUST NOT transmit new data
701          * to a given transport address if it has cwnd or more bytes
702          * of data outstanding to that transport address.
703          */
704         /* RFC 7.2.4 & the Implementers Guide 2.8.
705          *
706          * 3) ...
707          *    When a Fast Retransmit is being performed the sender SHOULD
708          *    ignore the value of cwnd and SHOULD NOT delay retransmission.
709          */
710         if (chunk->fast_retransmit != SCTP_NEED_FRTX &&
711             flight_size >= transport->cwnd)
712                 return SCTP_XMIT_RWND_FULL;
713
714         /* Nagle's algorithm to solve small-packet problem:
715          * Inhibit the sending of new chunks when new outgoing data arrives
716          * if any previously transmitted data on the connection remains
717          * unacknowledged.
718          */
719
720         if ((sctp_sk(asoc->base.sk)->nodelay || inflight == 0) &&
721             !asoc->force_delay)
722                 /* Nothing unacked */
723                 return SCTP_XMIT_OK;
724
725         if (!sctp_packet_empty(packet))
726                 /* Append to packet */
727                 return SCTP_XMIT_OK;
728
729         if (!sctp_state(asoc, ESTABLISHED))
730                 return SCTP_XMIT_OK;
731
732         /* Check whether this chunk and all the rest of pending data will fit
733          * or delay in hopes of bundling a full sized packet.
734          */
735         if (chunk->skb->len + q->out_qlen > transport->pathmtu -
736             packet->overhead - sctp_datachk_len(&chunk->asoc->stream) - 4)
737                 /* Enough data queued to fill a packet */
738                 return SCTP_XMIT_OK;
739
740         /* Don't delay large message writes that may have been fragmented */
741         if (!chunk->msg->can_delay)
742                 return SCTP_XMIT_OK;
743
744         /* Defer until all data acked or packet full */
745         return SCTP_XMIT_DELAY;
746 }
747
748 /* This private function does management things when adding DATA chunk */
749 static void sctp_packet_append_data(struct sctp_packet *packet,
750                                 struct sctp_chunk *chunk)
751 {
752         struct sctp_transport *transport = packet->transport;
753         size_t datasize = sctp_data_size(chunk);
754         struct sctp_association *asoc = transport->asoc;
755         u32 rwnd = asoc->peer.rwnd;
756
757         /* Keep track of how many bytes are in flight over this transport. */
758         transport->flight_size += datasize;
759
760         /* Keep track of how many bytes are in flight to the receiver. */
761         asoc->outqueue.outstanding_bytes += datasize;
762
763         /* Update our view of the receiver's rwnd. */
764         if (datasize < rwnd)
765                 rwnd -= datasize;
766         else
767                 rwnd = 0;
768
769         asoc->peer.rwnd = rwnd;
770         sctp_chunk_assign_tsn(chunk);
771         asoc->stream.si->assign_number(chunk);
772 }
773
774 static enum sctp_xmit sctp_packet_will_fit(struct sctp_packet *packet,
775                                            struct sctp_chunk *chunk,
776                                            u16 chunk_len)
777 {
778         enum sctp_xmit retval = SCTP_XMIT_OK;
779         size_t psize, pmtu, maxsize;
780
781         psize = packet->size;
782         if (packet->transport->asoc)
783                 pmtu = packet->transport->asoc->pathmtu;
784         else
785                 pmtu = packet->transport->pathmtu;
786
787         /* Decide if we need to fragment or resubmit later. */
788         if (psize + chunk_len > pmtu) {
789                 /* It's OK to fragment at IP level if any one of the following
790                  * is true:
791                  *      1. The packet is empty (meaning this chunk is greater
792                  *         the MTU)
793                  *      2. The packet doesn't have any data in it yet and data
794                  *         requires authentication.
795                  */
796                 if (sctp_packet_empty(packet) ||
797                     (!packet->has_data && chunk->auth)) {
798                         /* We no longer do re-fragmentation.
799                          * Just fragment at the IP layer, if we
800                          * actually hit this condition
801                          */
802                         packet->ipfragok = 1;
803                         goto out;
804                 }
805
806                 /* Similarly, if this chunk was built before a PMTU
807                  * reduction, we have to fragment it at IP level now. So
808                  * if the packet already contains something, we need to
809                  * flush.
810                  */
811                 maxsize = pmtu - packet->overhead;
812                 if (packet->auth)
813                         maxsize -= SCTP_PAD4(packet->auth->skb->len);
814                 if (chunk_len > maxsize)
815                         retval = SCTP_XMIT_PMTU_FULL;
816
817                 /* It is also okay to fragment if the chunk we are
818                  * adding is a control chunk, but only if current packet
819                  * is not a GSO one otherwise it causes fragmentation of
820                  * a large frame. So in this case we allow the
821                  * fragmentation by forcing it to be in a new packet.
822                  */
823                 if (!sctp_chunk_is_data(chunk) && packet->has_data)
824                         retval = SCTP_XMIT_PMTU_FULL;
825
826                 if (psize + chunk_len > packet->max_size)
827                         /* Hit GSO/PMTU limit, gotta flush */
828                         retval = SCTP_XMIT_PMTU_FULL;
829
830                 if (!packet->transport->burst_limited &&
831                     psize + chunk_len > (packet->transport->cwnd >> 1))
832                         /* Do not allow a single GSO packet to use more
833                          * than half of cwnd.
834                          */
835                         retval = SCTP_XMIT_PMTU_FULL;
836
837                 if (packet->transport->burst_limited &&
838                     psize + chunk_len > (packet->transport->burst_limited >> 1))
839                         /* Do not allow a single GSO packet to use more
840                          * than half of original cwnd.
841                          */
842                         retval = SCTP_XMIT_PMTU_FULL;
843                 /* Otherwise it will fit in the GSO packet */
844         }
845
846 out:
847         return retval;
848 }