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