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;
86 if (ecn_capable
&& sctp_packet_empty(packet
)) {
87 chunk
= sctp_get_ecne_prepend(packet
->transport
->asoc
);
89 /* If there a is a prepend chunk stick it on the list before
90 * any other chunks get appended.
93 sctp_packet_append_chunk(packet
, chunk
);
99 /* Initialize the packet structure. */
100 struct sctp_packet
*sctp_packet_init(struct sctp_packet
*packet
,
101 struct sctp_transport
*transport
,
102 __u16 sport
, __u16 dport
)
104 struct sctp_association
*asoc
= transport
->asoc
;
107 SCTP_DEBUG_PRINTK("%s: packet:%p transport:%p\n", __FUNCTION__
,
110 packet
->transport
= transport
;
111 packet
->source_port
= sport
;
112 packet
->destination_port
= dport
;
113 INIT_LIST_HEAD(&packet
->chunk_list
);
115 struct sctp_sock
*sp
= sctp_sk(asoc
->base
.sk
);
116 overhead
= sp
->pf
->af
->net_header_len
;
118 overhead
= sizeof(struct ipv6hdr
);
120 overhead
+= sizeof(struct sctphdr
);
121 packet
->overhead
= overhead
;
122 packet
->size
= overhead
;
124 packet
->has_cookie_echo
= 0;
125 packet
->has_sack
= 0;
126 packet
->has_auth
= 0;
127 packet
->ipfragok
= 0;
128 packet
->malloced
= 0;
134 void sctp_packet_free(struct sctp_packet
*packet
)
136 struct sctp_chunk
*chunk
, *tmp
;
138 SCTP_DEBUG_PRINTK("%s: packet:%p\n", __FUNCTION__
, packet
);
140 list_for_each_entry_safe(chunk
, tmp
, &packet
->chunk_list
, list
) {
141 list_del_init(&chunk
->list
);
142 sctp_chunk_free(chunk
);
145 if (packet
->malloced
)
149 /* This routine tries to append the chunk to the offered packet. If adding
150 * the chunk causes the packet to exceed the path MTU and COOKIE_ECHO chunk
151 * is not present in the packet, it transmits the input packet.
152 * Data can be bundled with a packet containing a COOKIE_ECHO chunk as long
153 * as it can fit in the packet, but any more data that does not fit in this
154 * packet can be sent only after receiving the COOKIE_ACK.
156 sctp_xmit_t
sctp_packet_transmit_chunk(struct sctp_packet
*packet
,
157 struct sctp_chunk
*chunk
)
162 SCTP_DEBUG_PRINTK("%s: packet:%p chunk:%p\n", __FUNCTION__
,
165 switch ((retval
= (sctp_packet_append_chunk(packet
, chunk
)))) {
166 case SCTP_XMIT_PMTU_FULL
:
167 if (!packet
->has_cookie_echo
) {
168 error
= sctp_packet_transmit(packet
);
170 chunk
->skb
->sk
->sk_err
= -error
;
172 /* If we have an empty packet, then we can NOT ever
175 retval
= sctp_packet_append_chunk(packet
, chunk
);
179 case SCTP_XMIT_RWND_FULL
:
181 case SCTP_XMIT_NAGLE_DELAY
:
188 /* Try to bundle a SACK with the packet. */
189 static sctp_xmit_t
sctp_packet_bundle_sack(struct sctp_packet
*pkt
,
190 struct sctp_chunk
*chunk
)
192 sctp_xmit_t retval
= SCTP_XMIT_OK
;
194 /* If sending DATA and haven't aleady bundled a SACK, try to
195 * bundle one in to the packet.
197 if (sctp_chunk_is_data(chunk
) && !pkt
->has_sack
&&
198 !pkt
->has_cookie_echo
) {
199 struct sctp_association
*asoc
;
200 asoc
= pkt
->transport
->asoc
;
202 if (asoc
->a_rwnd
> asoc
->rwnd
) {
203 struct sctp_chunk
*sack
;
204 asoc
->a_rwnd
= asoc
->rwnd
;
205 sack
= sctp_make_sack(asoc
);
207 struct timer_list
*timer
;
208 retval
= sctp_packet_append_chunk(pkt
, sack
);
209 asoc
->peer
.sack_needed
= 0;
210 timer
= &asoc
->timers
[SCTP_EVENT_TIMEOUT_SACK
];
211 if (timer_pending(timer
) && del_timer(timer
))
212 sctp_association_put(asoc
);
219 /* Append a chunk to the offered packet reporting back any inability to do
222 sctp_xmit_t
sctp_packet_append_chunk(struct sctp_packet
*packet
,
223 struct sctp_chunk
*chunk
)
225 sctp_xmit_t retval
= SCTP_XMIT_OK
;
226 __u16 chunk_len
= WORD_ROUND(ntohs(chunk
->chunk_hdr
->length
));
231 SCTP_DEBUG_PRINTK("%s: packet:%p chunk:%p\n", __FUNCTION__
, packet
,
234 retval
= sctp_packet_bundle_sack(packet
, chunk
);
235 psize
= packet
->size
;
237 if (retval
!= SCTP_XMIT_OK
)
240 pmtu
= ((packet
->transport
->asoc
) ?
241 (packet
->transport
->asoc
->pathmtu
) :
242 (packet
->transport
->pathmtu
));
244 too_big
= (psize
+ chunk_len
> pmtu
);
246 /* Decide if we need to fragment or resubmit later. */
248 /* Both control chunks and data chunks with TSNs are
251 if (sctp_packet_empty(packet
) || !sctp_chunk_is_data(chunk
)) {
252 /* We no longer do re-fragmentation.
253 * Just fragment at the IP layer, if we
254 * actually hit this condition
256 packet
->ipfragok
= 1;
260 retval
= SCTP_XMIT_PMTU_FULL
;
266 /* We believe that this chunk is OK to add to the packet (as
267 * long as we have the cwnd for it).
270 /* DATA is a special case since we must examine both rwnd and cwnd
271 * before we send DATA.
273 if (sctp_chunk_is_data(chunk
)) {
274 retval
= sctp_packet_append_data(packet
, chunk
);
275 /* Disallow SACK bundling after DATA. */
276 packet
->has_sack
= 1;
277 if (SCTP_XMIT_OK
!= retval
)
279 } else if (SCTP_CID_COOKIE_ECHO
== chunk
->chunk_hdr
->type
)
280 packet
->has_cookie_echo
= 1;
281 else if (SCTP_CID_SACK
== chunk
->chunk_hdr
->type
)
282 packet
->has_sack
= 1;
284 /* It is OK to send this chunk. */
285 list_add_tail(&chunk
->list
, &packet
->chunk_list
);
286 packet
->size
+= chunk_len
;
287 chunk
->transport
= packet
->transport
;
292 /* All packets are sent to the network through this function from
295 * The return value is a normal kernel error return value.
297 int sctp_packet_transmit(struct sctp_packet
*packet
)
299 struct sctp_transport
*tp
= packet
->transport
;
300 struct sctp_association
*asoc
= tp
->asoc
;
303 struct sk_buff
*nskb
;
304 struct sctp_chunk
*chunk
, *tmp
;
307 int padding
; /* How much padding do we need? */
309 struct dst_entry
*dst
= tp
->dst
;
311 SCTP_DEBUG_PRINTK("%s: packet:%p\n", __FUNCTION__
, packet
);
313 /* Do NOT generate a chunkless packet. */
314 if (list_empty(&packet
->chunk_list
))
317 /* Set up convenience variables... */
318 chunk
= list_entry(packet
->chunk_list
.next
, struct sctp_chunk
, list
);
321 /* Allocate the new skb. */
322 nskb
= alloc_skb(packet
->size
+ LL_MAX_HEADER
, GFP_ATOMIC
);
326 /* Make sure the outbound skb has enough header room reserved. */
327 skb_reserve(nskb
, packet
->overhead
+ LL_MAX_HEADER
);
329 /* Set the owning socket so that we know where to get the
330 * destination IP address.
332 skb_set_owner_w(nskb
, sk
);
334 /* The 'obsolete' field of dst is set to 2 when a dst is freed. */
335 if (!dst
|| (dst
->obsolete
> 1)) {
337 sctp_transport_route(tp
, NULL
, sctp_sk(sk
));
338 if (asoc
&& (asoc
->param_flags
& SPP_PMTUD_ENABLE
)) {
339 sctp_assoc_sync_pmtu(asoc
);
342 nskb
->dst
= dst_clone(tp
->dst
);
347 /* Build the SCTP header. */
348 sh
= (struct sctphdr
*)skb_push(nskb
, sizeof(struct sctphdr
));
349 sh
->source
= htons(packet
->source_port
);
350 sh
->dest
= htons(packet
->destination_port
);
352 /* From 6.8 Adler-32 Checksum Calculation:
353 * After the packet is constructed (containing the SCTP common
354 * header and one or more control or DATA chunks), the
357 * 1) Fill in the proper Verification Tag in the SCTP common
358 * header and initialize the checksum field to 0's.
360 sh
->vtag
= htonl(packet
->vtag
);
363 /* 2) Calculate the Adler-32 checksum of the whole packet,
364 * including the SCTP common header and all the
367 * Note: Adler-32 is no longer applicable, as has been replaced
368 * by CRC32-C as described in <draft-ietf-tsvwg-sctpcsum-02.txt>.
370 if (!(dst
->dev
->features
& NETIF_F_NO_CSUM
))
371 crc32
= sctp_start_cksum((__u8
*)sh
, sizeof(struct sctphdr
));
376 * An endpoint bundles chunks by simply including multiple
377 * chunks in one outbound SCTP packet. ...
381 * 3.2 Chunk Field Descriptions
383 * The total length of a chunk (including Type, Length and
384 * Value fields) MUST be a multiple of 4 bytes. If the length
385 * of the chunk is not a multiple of 4 bytes, the sender MUST
386 * pad the chunk with all zero bytes and this padding is not
387 * included in the chunk length field. The sender should
388 * never pad with more than 3 bytes.
390 * [This whole comment explains WORD_ROUND() below.]
392 SCTP_DEBUG_PRINTK("***sctp_transmit_packet***\n");
393 list_for_each_entry_safe(chunk
, tmp
, &packet
->chunk_list
, list
) {
394 list_del_init(&chunk
->list
);
395 if (sctp_chunk_is_data(chunk
)) {
397 if (!chunk
->has_tsn
) {
398 sctp_chunk_assign_ssn(chunk
);
399 sctp_chunk_assign_tsn(chunk
);
401 /* 6.3.1 C4) When data is in flight and when allowed
402 * by rule C5, a new RTT measurement MUST be made each
403 * round trip. Furthermore, new RTT measurements
404 * SHOULD be made no more than once per round-trip
405 * for a given destination transport address.
408 if (!tp
->rto_pending
) {
409 chunk
->rtt_in_progress
= 1;
415 chunk
->sent_at
= jiffies
;
419 padding
= WORD_ROUND(chunk
->skb
->len
) - chunk
->skb
->len
;
421 memset(skb_put(chunk
->skb
, padding
), 0, padding
);
423 if (dst
->dev
->features
& NETIF_F_NO_CSUM
)
424 memcpy(skb_put(nskb
, chunk
->skb
->len
),
425 chunk
->skb
->data
, chunk
->skb
->len
);
427 crc32
= sctp_update_copy_cksum(skb_put(nskb
,
430 chunk
->skb
->len
, crc32
);
432 SCTP_DEBUG_PRINTK("%s %p[%s] %s 0x%x, %s %d, %s %d, %s %d\n",
434 sctp_cname(SCTP_ST_CHUNK(
435 chunk
->chunk_hdr
->type
)),
436 chunk
->has_tsn
? "TSN" : "No TSN",
438 ntohl(chunk
->subh
.data_hdr
->tsn
) : 0,
439 "length", ntohs(chunk
->chunk_hdr
->length
),
440 "chunk->skb->len", chunk
->skb
->len
,
441 "rtt_in_progress", chunk
->rtt_in_progress
);
444 * If this is a control chunk, this is our last
445 * reference. Free data chunks after they've been
446 * acknowledged or have failed.
448 if (!sctp_chunk_is_data(chunk
))
449 sctp_chunk_free(chunk
);
452 /* Perform final transformation on checksum. */
453 if (!(dst
->dev
->features
& NETIF_F_NO_CSUM
))
454 crc32
= sctp_end_cksum(crc32
);
456 /* 3) Put the resultant value into the checksum field in the
457 * common header, and leave the rest of the bits unchanged.
459 sh
->checksum
= htonl(crc32
);
461 /* IP layer ECN support
463 * "The ECN-Capable Transport (ECT) bit would be set by the
464 * data sender to indicate that the end-points of the
465 * transport protocol are ECN-capable."
467 * Now setting the ECT bit all the time, as it should not cause
468 * any problems protocol-wise even if our peer ignores it.
470 * Note: The works for IPv6 layer checks this bit too later
471 * in transmission. See IP6_ECN_flow_xmit().
473 INET_ECN_xmit(nskb
->sk
);
475 /* Set up the IP options. */
476 /* BUG: not implemented
477 * For v4 this all lives somewhere in sk->sk_opt...
480 /* Dump that on IP! */
481 if (asoc
&& asoc
->peer
.last_sent_to
!= tp
) {
482 /* Considering the multiple CPU scenario, this is a
483 * "correcter" place for last_sent_to. --xguo
485 asoc
->peer
.last_sent_to
= tp
;
489 struct timer_list
*timer
;
490 unsigned long timeout
;
492 tp
->last_time_used
= jiffies
;
494 /* Restart the AUTOCLOSE timer when sending data. */
495 if (sctp_state(asoc
, ESTABLISHED
) && asoc
->autoclose
) {
496 timer
= &asoc
->timers
[SCTP_EVENT_TIMEOUT_AUTOCLOSE
];
497 timeout
= asoc
->timeouts
[SCTP_EVENT_TIMEOUT_AUTOCLOSE
];
499 if (!mod_timer(timer
, jiffies
+ timeout
))
500 sctp_association_hold(asoc
);
504 SCTP_DEBUG_PRINTK("***sctp_transmit_packet*** skb len %d\n",
507 if (tp
->param_flags
& SPP_PMTUD_ENABLE
)
508 (*tp
->af_specific
->sctp_xmit
)(nskb
, tp
, packet
->ipfragok
);
510 (*tp
->af_specific
->sctp_xmit
)(nskb
, tp
, 1);
513 packet
->size
= packet
->overhead
;
517 IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES
);
519 /* FIXME: Returning the 'err' will effect all the associations
520 * associated with a socket, although only one of the paths of the
521 * association is unreachable.
522 * The real failure of a transport or association can be passed on
523 * to the user via notifications. So setting this error may not be
526 /* err = -EHOSTUNREACH; */
528 /* Control chunks are unreliable so just drop them. DATA chunks
529 * will get resent or dropped later.
532 list_for_each_entry_safe(chunk
, tmp
, &packet
->chunk_list
, list
) {
533 list_del_init(&chunk
->list
);
534 if (!sctp_chunk_is_data(chunk
))
535 sctp_chunk_free(chunk
);
543 /********************************************************************
544 * 2nd Level Abstractions
545 ********************************************************************/
547 /* This private function handles the specifics of appending DATA chunks. */
548 static sctp_xmit_t
sctp_packet_append_data(struct sctp_packet
*packet
,
549 struct sctp_chunk
*chunk
)
551 sctp_xmit_t retval
= SCTP_XMIT_OK
;
552 size_t datasize
, rwnd
, inflight
;
553 struct sctp_transport
*transport
= packet
->transport
;
554 __u32 max_burst_bytes
;
555 struct sctp_association
*asoc
= transport
->asoc
;
556 struct sctp_sock
*sp
= sctp_sk(asoc
->base
.sk
);
557 struct sctp_outq
*q
= &asoc
->outqueue
;
559 /* RFC 2960 6.1 Transmission of DATA Chunks
561 * A) At any given time, the data sender MUST NOT transmit new data to
562 * any destination transport address if its peer's rwnd indicates
563 * that the peer has no buffer space (i.e. rwnd is 0, see Section
564 * 6.2.1). However, regardless of the value of rwnd (including if it
565 * is 0), the data sender can always have one DATA chunk in flight to
566 * the receiver if allowed by cwnd (see rule B below). This rule
567 * allows the sender to probe for a change in rwnd that the sender
568 * missed due to the SACK having been lost in transit from the data
569 * receiver to the data sender.
572 rwnd
= asoc
->peer
.rwnd
;
573 inflight
= asoc
->outqueue
.outstanding_bytes
;
575 datasize
= sctp_data_size(chunk
);
577 if (datasize
> rwnd
) {
579 /* We have (at least) one data chunk in flight,
580 * so we can't fall back to rule 6.1 B).
582 retval
= SCTP_XMIT_RWND_FULL
;
587 /* sctpimpguide-05 2.14.2
588 * D) When the time comes for the sender to
589 * transmit new DATA chunks, the protocol parameter Max.Burst MUST
590 * first be applied to limit how many new DATA chunks may be sent.
591 * The limit is applied by adjusting cwnd as follows:
592 * if ((flightsize + Max.Burst * MTU) < cwnd)
593 * cwnd = flightsize + Max.Burst * MTU
595 max_burst_bytes
= asoc
->max_burst
* asoc
->pathmtu
;
596 if ((transport
->flight_size
+ max_burst_bytes
) < transport
->cwnd
) {
597 transport
->cwnd
= transport
->flight_size
+ max_burst_bytes
;
598 SCTP_DEBUG_PRINTK("%s: cwnd limited by max_burst: "
599 "transport: %p, cwnd: %d, "
600 "ssthresh: %d, flight_size: %d, "
602 __FUNCTION__
, transport
,
605 transport
->flight_size
,
606 transport
->partial_bytes_acked
);
609 /* RFC 2960 6.1 Transmission of DATA Chunks
611 * B) At any given time, the sender MUST NOT transmit new data
612 * to a given transport address if it has cwnd or more bytes
613 * of data outstanding to that transport address.
615 /* RFC 7.2.4 & the Implementers Guide 2.8.
618 * When a Fast Retransmit is being performed the sender SHOULD
619 * ignore the value of cwnd and SHOULD NOT delay retransmission.
621 if (chunk
->fast_retransmit
<= 0)
622 if (transport
->flight_size
>= transport
->cwnd
) {
623 retval
= SCTP_XMIT_RWND_FULL
;
627 /* Nagle's algorithm to solve small-packet problem:
628 * Inhibit the sending of new chunks when new outgoing data arrives
629 * if any previously transmitted data on the connection remains
632 if (!sp
->nodelay
&& sctp_packet_empty(packet
) &&
633 q
->outstanding_bytes
&& sctp_state(asoc
, ESTABLISHED
)) {
634 unsigned len
= datasize
+ q
->out_qlen
;
636 /* Check whether this chunk and all the rest of pending
637 * data will fit or delay in hopes of bundling a full
640 if (len
< asoc
->frag_point
) {
641 retval
= SCTP_XMIT_NAGLE_DELAY
;
646 /* Keep track of how many bytes are in flight over this transport. */
647 transport
->flight_size
+= datasize
;
649 /* Keep track of how many bytes are in flight to the receiver. */
650 asoc
->outqueue
.outstanding_bytes
+= datasize
;
652 /* Update our view of the receiver's rwnd. Include sk_buff overhead
653 * while updating peer.rwnd so that it reduces the chances of a
654 * receiver running out of receive buffer space even when receive
655 * window is still open. This can happen when a sender is sending
656 * sending small messages.
658 datasize
+= sizeof(struct sk_buff
);
664 asoc
->peer
.rwnd
= rwnd
;
665 /* Has been accepted for transmission. */
666 if (!asoc
->peer
.prsctp_capable
)
667 chunk
->msg
->can_abandon
= 0;