1 /* SCTP kernel reference Implementation
2 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
6 * This file is part of the SCTP kernel reference Implementation
8 * These functions handle output processing.
10 * The SCTP reference 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)
16 * The SCTP reference 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.
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.
27 * Please send any bug reports or fixes you make to the
29 * lksctp developers <lksctp-developers@lists.sourceforge.net>
31 * Or submit a bug report through the following website:
32 * http://www.sf.net/projects/lksctp
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>
40 * Any bugs reported given to us we will try to fix... any fixes shared will
41 * be incorporated into the next SCTP release.
44 #include <linux/types.h>
45 #include <linux/kernel.h>
46 #include <linux/wait.h>
47 #include <linux/time.h>
49 #include <linux/ipv6.h>
50 #include <linux/init.h>
51 #include <net/inet_ecn.h>
56 #endif /* TEST_FRAME (not defined) */
58 #include <linux/socket.h> /* for sa_family_t */
61 #include <net/sctp/sctp.h>
62 #include <net/sctp/sm.h>
64 /* Forward declarations for private helpers. */
65 static sctp_xmit_t
sctp_packet_append_data(struct sctp_packet
*packet
,
66 struct sctp_chunk
*chunk
);
69 * This appears to be a followup set of initializations.
71 struct sctp_packet
*sctp_packet_config(struct sctp_packet
*packet
,
72 __u32 vtag
, int ecn_capable
)
74 struct sctp_chunk
*chunk
= NULL
;
76 SCTP_DEBUG_PRINTK("%s: packet:%p vtag:0x%x\n", __FUNCTION__
,
80 packet
->has_cookie_echo
= 0;
84 if (ecn_capable
&& sctp_packet_empty(packet
)) {
85 chunk
= sctp_get_ecne_prepend(packet
->transport
->asoc
);
87 /* If there a is a prepend chunk stick it on the list before
88 * any other chunks get appended.
91 sctp_packet_append_chunk(packet
, chunk
);
97 /* Initialize the packet structure. */
98 struct sctp_packet
*sctp_packet_init(struct sctp_packet
*packet
,
99 struct sctp_transport
*transport
,
100 __u16 sport
, __u16 dport
)
102 struct sctp_association
*asoc
= transport
->asoc
;
105 SCTP_DEBUG_PRINTK("%s: packet:%p transport:%p\n", __FUNCTION__
,
108 packet
->transport
= transport
;
109 packet
->source_port
= sport
;
110 packet
->destination_port
= dport
;
111 INIT_LIST_HEAD(&packet
->chunk_list
);
113 struct sctp_sock
*sp
= sctp_sk(asoc
->base
.sk
);
114 overhead
= sp
->pf
->af
->net_header_len
;
116 overhead
= sizeof(struct ipv6hdr
);
118 overhead
+= sizeof(struct sctphdr
);
119 packet
->overhead
= overhead
;
120 packet
->size
= overhead
;
122 packet
->has_cookie_echo
= 0;
123 packet
->has_sack
= 0;
124 packet
->ipfragok
= 0;
125 packet
->malloced
= 0;
130 void sctp_packet_free(struct sctp_packet
*packet
)
132 struct sctp_chunk
*chunk
, *tmp
;
134 SCTP_DEBUG_PRINTK("%s: packet:%p\n", __FUNCTION__
, packet
);
136 list_for_each_entry_safe(chunk
, tmp
, &packet
->chunk_list
, list
) {
137 list_del_init(&chunk
->list
);
138 sctp_chunk_free(chunk
);
141 if (packet
->malloced
)
145 /* This routine tries to append the chunk to the offered packet. If adding
146 * the chunk causes the packet to exceed the path MTU and COOKIE_ECHO chunk
147 * is not present in the packet, it transmits the input packet.
148 * Data can be bundled with a packet containing a COOKIE_ECHO chunk as long
149 * as it can fit in the packet, but any more data that does not fit in this
150 * packet can be sent only after receiving the COOKIE_ACK.
152 sctp_xmit_t
sctp_packet_transmit_chunk(struct sctp_packet
*packet
,
153 struct sctp_chunk
*chunk
)
158 SCTP_DEBUG_PRINTK("%s: packet:%p chunk:%p\n", __FUNCTION__
,
161 switch ((retval
= (sctp_packet_append_chunk(packet
, chunk
)))) {
162 case SCTP_XMIT_PMTU_FULL
:
163 if (!packet
->has_cookie_echo
) {
164 error
= sctp_packet_transmit(packet
);
166 chunk
->skb
->sk
->sk_err
= -error
;
168 /* If we have an empty packet, then we can NOT ever
171 retval
= sctp_packet_append_chunk(packet
, chunk
);
175 case SCTP_XMIT_RWND_FULL
:
177 case SCTP_XMIT_NAGLE_DELAY
:
184 /* Try to bundle a SACK with the packet. */
185 static sctp_xmit_t
sctp_packet_bundle_sack(struct sctp_packet
*pkt
,
186 struct sctp_chunk
*chunk
)
188 sctp_xmit_t retval
= SCTP_XMIT_OK
;
190 /* If sending DATA and haven't aleady bundled a SACK, try to
191 * bundle one in to the packet.
193 if (sctp_chunk_is_data(chunk
) && !pkt
->has_sack
&&
194 !pkt
->has_cookie_echo
) {
195 struct sctp_association
*asoc
;
196 asoc
= pkt
->transport
->asoc
;
198 if (asoc
->a_rwnd
> asoc
->rwnd
) {
199 struct sctp_chunk
*sack
;
200 asoc
->a_rwnd
= asoc
->rwnd
;
201 sack
= sctp_make_sack(asoc
);
203 struct timer_list
*timer
;
204 retval
= sctp_packet_append_chunk(pkt
, sack
);
205 asoc
->peer
.sack_needed
= 0;
206 timer
= &asoc
->timers
[SCTP_EVENT_TIMEOUT_SACK
];
207 if (timer_pending(timer
) && del_timer(timer
))
208 sctp_association_put(asoc
);
215 /* Append a chunk to the offered packet reporting back any inability to do
218 sctp_xmit_t
sctp_packet_append_chunk(struct sctp_packet
*packet
,
219 struct sctp_chunk
*chunk
)
221 sctp_xmit_t retval
= SCTP_XMIT_OK
;
222 __u16 chunk_len
= WORD_ROUND(ntohs(chunk
->chunk_hdr
->length
));
227 SCTP_DEBUG_PRINTK("%s: packet:%p chunk:%p\n", __FUNCTION__
, packet
,
230 retval
= sctp_packet_bundle_sack(packet
, chunk
);
231 psize
= packet
->size
;
233 if (retval
!= SCTP_XMIT_OK
)
236 pmtu
= ((packet
->transport
->asoc
) ?
237 (packet
->transport
->asoc
->pathmtu
) :
238 (packet
->transport
->pathmtu
));
240 too_big
= (psize
+ chunk_len
> pmtu
);
242 /* Decide if we need to fragment or resubmit later. */
244 /* Both control chunks and data chunks with TSNs are
247 if (sctp_packet_empty(packet
) || !sctp_chunk_is_data(chunk
)) {
248 /* We no longer do re-fragmentation.
249 * Just fragment at the IP layer, if we
250 * actually hit this condition
252 packet
->ipfragok
= 1;
256 retval
= SCTP_XMIT_PMTU_FULL
;
262 /* We believe that this chunk is OK to add to the packet (as
263 * long as we have the cwnd for it).
266 /* DATA is a special case since we must examine both rwnd and cwnd
267 * before we send DATA.
269 if (sctp_chunk_is_data(chunk
)) {
270 retval
= sctp_packet_append_data(packet
, chunk
);
271 /* Disallow SACK bundling after DATA. */
272 packet
->has_sack
= 1;
273 if (SCTP_XMIT_OK
!= retval
)
275 } else if (SCTP_CID_COOKIE_ECHO
== chunk
->chunk_hdr
->type
)
276 packet
->has_cookie_echo
= 1;
277 else if (SCTP_CID_SACK
== chunk
->chunk_hdr
->type
)
278 packet
->has_sack
= 1;
280 /* It is OK to send this chunk. */
281 list_add_tail(&chunk
->list
, &packet
->chunk_list
);
282 packet
->size
+= chunk_len
;
283 chunk
->transport
= packet
->transport
;
288 /* All packets are sent to the network through this function from
291 * The return value is a normal kernel error return value.
293 int sctp_packet_transmit(struct sctp_packet
*packet
)
295 struct sctp_transport
*tp
= packet
->transport
;
296 struct sctp_association
*asoc
= tp
->asoc
;
299 struct sk_buff
*nskb
;
300 struct sctp_chunk
*chunk
, *tmp
;
303 int padding
; /* How much padding do we need? */
305 struct dst_entry
*dst
= tp
->dst
;
307 SCTP_DEBUG_PRINTK("%s: packet:%p\n", __FUNCTION__
, packet
);
309 /* Do NOT generate a chunkless packet. */
310 if (list_empty(&packet
->chunk_list
))
313 /* Set up convenience variables... */
314 chunk
= list_entry(packet
->chunk_list
.next
, struct sctp_chunk
, list
);
317 /* Allocate the new skb. */
318 nskb
= alloc_skb(packet
->size
+ LL_MAX_HEADER
, GFP_ATOMIC
);
322 /* Make sure the outbound skb has enough header room reserved. */
323 skb_reserve(nskb
, packet
->overhead
+ LL_MAX_HEADER
);
325 /* Set the owning socket so that we know where to get the
326 * destination IP address.
328 skb_set_owner_w(nskb
, sk
);
330 /* The 'obsolete' field of dst is set to 2 when a dst is freed. */
331 if (!dst
|| (dst
->obsolete
> 1)) {
333 sctp_transport_route(tp
, NULL
, sctp_sk(sk
));
334 if (asoc
&& (asoc
->param_flags
& SPP_PMTUD_ENABLE
)) {
335 sctp_assoc_sync_pmtu(asoc
);
338 nskb
->dst
= dst_clone(tp
->dst
);
343 /* Build the SCTP header. */
344 sh
= (struct sctphdr
*)skb_push(nskb
, sizeof(struct sctphdr
));
345 sh
->source
= htons(packet
->source_port
);
346 sh
->dest
= htons(packet
->destination_port
);
348 /* From 6.8 Adler-32 Checksum Calculation:
349 * After the packet is constructed (containing the SCTP common
350 * header and one or more control or DATA chunks), the
353 * 1) Fill in the proper Verification Tag in the SCTP common
354 * header and initialize the checksum field to 0's.
356 sh
->vtag
= htonl(packet
->vtag
);
359 /* 2) Calculate the Adler-32 checksum of the whole packet,
360 * including the SCTP common header and all the
363 * Note: Adler-32 is no longer applicable, as has been replaced
364 * by CRC32-C as described in <draft-ietf-tsvwg-sctpcsum-02.txt>.
366 if (!(dst
->dev
->features
& NETIF_F_NO_CSUM
))
367 crc32
= sctp_start_cksum((__u8
*)sh
, sizeof(struct sctphdr
));
372 * An endpoint bundles chunks by simply including multiple
373 * chunks in one outbound SCTP packet. ...
377 * 3.2 Chunk Field Descriptions
379 * The total length of a chunk (including Type, Length and
380 * Value fields) MUST be a multiple of 4 bytes. If the length
381 * of the chunk is not a multiple of 4 bytes, the sender MUST
382 * pad the chunk with all zero bytes and this padding is not
383 * included in the chunk length field. The sender should
384 * never pad with more than 3 bytes.
386 * [This whole comment explains WORD_ROUND() below.]
388 SCTP_DEBUG_PRINTK("***sctp_transmit_packet***\n");
389 list_for_each_entry_safe(chunk
, tmp
, &packet
->chunk_list
, list
) {
390 list_del_init(&chunk
->list
);
391 if (sctp_chunk_is_data(chunk
)) {
393 if (!chunk
->has_tsn
) {
394 sctp_chunk_assign_ssn(chunk
);
395 sctp_chunk_assign_tsn(chunk
);
397 /* 6.3.1 C4) When data is in flight and when allowed
398 * by rule C5, a new RTT measurement MUST be made each
399 * round trip. Furthermore, new RTT measurements
400 * SHOULD be made no more than once per round-trip
401 * for a given destination transport address.
404 if (!tp
->rto_pending
) {
405 chunk
->rtt_in_progress
= 1;
411 chunk
->sent_at
= jiffies
;
415 padding
= WORD_ROUND(chunk
->skb
->len
) - chunk
->skb
->len
;
417 memset(skb_put(chunk
->skb
, padding
), 0, padding
);
419 if (dst
->dev
->features
& NETIF_F_NO_CSUM
)
420 memcpy(skb_put(nskb
, chunk
->skb
->len
),
421 chunk
->skb
->data
, chunk
->skb
->len
);
423 crc32
= sctp_update_copy_cksum(skb_put(nskb
,
426 chunk
->skb
->len
, crc32
);
428 SCTP_DEBUG_PRINTK("%s %p[%s] %s 0x%x, %s %d, %s %d, %s %d\n",
430 sctp_cname(SCTP_ST_CHUNK(
431 chunk
->chunk_hdr
->type
)),
432 chunk
->has_tsn
? "TSN" : "No TSN",
434 ntohl(chunk
->subh
.data_hdr
->tsn
) : 0,
435 "length", ntohs(chunk
->chunk_hdr
->length
),
436 "chunk->skb->len", chunk
->skb
->len
,
437 "rtt_in_progress", chunk
->rtt_in_progress
);
440 * If this is a control chunk, this is our last
441 * reference. Free data chunks after they've been
442 * acknowledged or have failed.
444 if (!sctp_chunk_is_data(chunk
))
445 sctp_chunk_free(chunk
);
448 /* Perform final transformation on checksum. */
449 if (!(dst
->dev
->features
& NETIF_F_NO_CSUM
))
450 crc32
= sctp_end_cksum(crc32
);
452 /* 3) Put the resultant value into the checksum field in the
453 * common header, and leave the rest of the bits unchanged.
455 sh
->checksum
= htonl(crc32
);
457 /* IP layer ECN support
459 * "The ECN-Capable Transport (ECT) bit would be set by the
460 * data sender to indicate that the end-points of the
461 * transport protocol are ECN-capable."
463 * Now setting the ECT bit all the time, as it should not cause
464 * any problems protocol-wise even if our peer ignores it.
466 * Note: The works for IPv6 layer checks this bit too later
467 * in transmission. See IP6_ECN_flow_xmit().
469 INET_ECN_xmit(nskb
->sk
);
471 /* Set up the IP options. */
472 /* BUG: not implemented
473 * For v4 this all lives somewhere in sk->sk_opt...
476 /* Dump that on IP! */
477 if (asoc
&& asoc
->peer
.last_sent_to
!= tp
) {
478 /* Considering the multiple CPU scenario, this is a
479 * "correcter" place for last_sent_to. --xguo
481 asoc
->peer
.last_sent_to
= tp
;
485 struct timer_list
*timer
;
486 unsigned long timeout
;
488 tp
->last_time_used
= jiffies
;
490 /* Restart the AUTOCLOSE timer when sending data. */
491 if (sctp_state(asoc
, ESTABLISHED
) && asoc
->autoclose
) {
492 timer
= &asoc
->timers
[SCTP_EVENT_TIMEOUT_AUTOCLOSE
];
493 timeout
= asoc
->timeouts
[SCTP_EVENT_TIMEOUT_AUTOCLOSE
];
495 if (!mod_timer(timer
, jiffies
+ timeout
))
496 sctp_association_hold(asoc
);
500 SCTP_DEBUG_PRINTK("***sctp_transmit_packet*** skb len %d\n",
503 if (tp
->param_flags
& SPP_PMTUD_ENABLE
)
504 (*tp
->af_specific
->sctp_xmit
)(nskb
, tp
, packet
->ipfragok
);
506 (*tp
->af_specific
->sctp_xmit
)(nskb
, tp
, 1);
509 packet
->size
= packet
->overhead
;
513 IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES
);
515 /* FIXME: Returning the 'err' will effect all the associations
516 * associated with a socket, although only one of the paths of the
517 * association is unreachable.
518 * The real failure of a transport or association can be passed on
519 * to the user via notifications. So setting this error may not be
522 /* err = -EHOSTUNREACH; */
524 /* Control chunks are unreliable so just drop them. DATA chunks
525 * will get resent or dropped later.
528 list_for_each_entry_safe(chunk
, tmp
, &packet
->chunk_list
, list
) {
529 list_del_init(&chunk
->list
);
530 if (!sctp_chunk_is_data(chunk
))
531 sctp_chunk_free(chunk
);
539 /********************************************************************
540 * 2nd Level Abstractions
541 ********************************************************************/
543 /* This private function handles the specifics of appending DATA chunks. */
544 static sctp_xmit_t
sctp_packet_append_data(struct sctp_packet
*packet
,
545 struct sctp_chunk
*chunk
)
547 sctp_xmit_t retval
= SCTP_XMIT_OK
;
548 size_t datasize
, rwnd
, inflight
;
549 struct sctp_transport
*transport
= packet
->transport
;
550 __u32 max_burst_bytes
;
551 struct sctp_association
*asoc
= transport
->asoc
;
552 struct sctp_sock
*sp
= sctp_sk(asoc
->base
.sk
);
553 struct sctp_outq
*q
= &asoc
->outqueue
;
555 /* RFC 2960 6.1 Transmission of DATA Chunks
557 * A) At any given time, the data sender MUST NOT transmit new data to
558 * any destination transport address if its peer's rwnd indicates
559 * that the peer has no buffer space (i.e. rwnd is 0, see Section
560 * 6.2.1). However, regardless of the value of rwnd (including if it
561 * is 0), the data sender can always have one DATA chunk in flight to
562 * the receiver if allowed by cwnd (see rule B below). This rule
563 * allows the sender to probe for a change in rwnd that the sender
564 * missed due to the SACK having been lost in transit from the data
565 * receiver to the data sender.
568 rwnd
= asoc
->peer
.rwnd
;
569 inflight
= asoc
->outqueue
.outstanding_bytes
;
571 datasize
= sctp_data_size(chunk
);
573 if (datasize
> rwnd
) {
575 /* We have (at least) one data chunk in flight,
576 * so we can't fall back to rule 6.1 B).
578 retval
= SCTP_XMIT_RWND_FULL
;
583 /* sctpimpguide-05 2.14.2
584 * D) When the time comes for the sender to
585 * transmit new DATA chunks, the protocol parameter Max.Burst MUST
586 * first be applied to limit how many new DATA chunks may be sent.
587 * The limit is applied by adjusting cwnd as follows:
588 * if ((flightsize + Max.Burst * MTU) < cwnd)
589 * cwnd = flightsize + Max.Burst * MTU
591 max_burst_bytes
= asoc
->max_burst
* asoc
->pathmtu
;
592 if ((transport
->flight_size
+ max_burst_bytes
) < transport
->cwnd
) {
593 transport
->cwnd
= transport
->flight_size
+ max_burst_bytes
;
594 SCTP_DEBUG_PRINTK("%s: cwnd limited by max_burst: "
595 "transport: %p, cwnd: %d, "
596 "ssthresh: %d, flight_size: %d, "
598 __FUNCTION__
, transport
,
601 transport
->flight_size
,
602 transport
->partial_bytes_acked
);
605 /* RFC 2960 6.1 Transmission of DATA Chunks
607 * B) At any given time, the sender MUST NOT transmit new data
608 * to a given transport address if it has cwnd or more bytes
609 * of data outstanding to that transport address.
611 /* RFC 7.2.4 & the Implementers Guide 2.8.
614 * When a Fast Retransmit is being performed the sender SHOULD
615 * ignore the value of cwnd and SHOULD NOT delay retransmission.
617 if (chunk
->fast_retransmit
<= 0)
618 if (transport
->flight_size
>= transport
->cwnd
) {
619 retval
= SCTP_XMIT_RWND_FULL
;
623 /* Nagle's algorithm to solve small-packet problem:
624 * Inhibit the sending of new chunks when new outgoing data arrives
625 * if any previously transmitted data on the connection remains
628 if (!sp
->nodelay
&& sctp_packet_empty(packet
) &&
629 q
->outstanding_bytes
&& sctp_state(asoc
, ESTABLISHED
)) {
630 unsigned len
= datasize
+ q
->out_qlen
;
632 /* Check whether this chunk and all the rest of pending
633 * data will fit or delay in hopes of bundling a full
636 if (len
< asoc
->frag_point
) {
637 retval
= SCTP_XMIT_NAGLE_DELAY
;
642 /* Keep track of how many bytes are in flight over this transport. */
643 transport
->flight_size
+= datasize
;
645 /* Keep track of how many bytes are in flight to the receiver. */
646 asoc
->outqueue
.outstanding_bytes
+= datasize
;
648 /* Update our view of the receiver's rwnd. Include sk_buff overhead
649 * while updating peer.rwnd so that it reduces the chances of a
650 * receiver running out of receive buffer space even when receive
651 * window is still open. This can happen when a sender is sending
652 * sending small messages.
654 datasize
+= sizeof(struct sk_buff
);
660 asoc
->peer
.rwnd
= rwnd
;
661 /* Has been accepted for transmission. */
662 if (!asoc
->peer
.prsctp_capable
)
663 chunk
->msg
->can_abandon
= 0;