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