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