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.
6 * This file is part of the SCTP kernel implementation
8 * These functions handle output processing.
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)
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.
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>
63 #include <net/sctp/checksum.h>
65 /* Forward declarations for private helpers. */
66 static sctp_xmit_t sctp_packet_append_data(struct sctp_packet *packet,
67 struct sctp_chunk *chunk);
70 * This appears to be a followup set of initializations.
72 struct sctp_packet *sctp_packet_config(struct sctp_packet *packet,
73 __u32 vtag, int ecn_capable)
75 struct sctp_chunk *chunk = NULL;
77 SCTP_DEBUG_PRINTK("%s: packet:%p vtag:0x%x\n", __FUNCTION__,
81 packet->has_cookie_echo = 0;
88 if (ecn_capable && sctp_packet_empty(packet)) {
89 chunk = sctp_get_ecne_prepend(packet->transport->asoc);
91 /* If there a is a prepend chunk stick it on the list before
92 * any other chunks get appended.
95 sctp_packet_append_chunk(packet, chunk);
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)
106 struct sctp_association *asoc = transport->asoc;
109 SCTP_DEBUG_PRINTK("%s: packet:%p transport:%p\n", __FUNCTION__,
112 packet->transport = transport;
113 packet->source_port = sport;
114 packet->destination_port = dport;
115 INIT_LIST_HEAD(&packet->chunk_list);
117 struct sctp_sock *sp = sctp_sk(asoc->base.sk);
118 overhead = sp->pf->af->net_header_len;
120 overhead = sizeof(struct ipv6hdr);
122 overhead += sizeof(struct sctphdr);
123 packet->overhead = overhead;
124 packet->size = overhead;
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;
137 void sctp_packet_free(struct sctp_packet *packet)
139 struct sctp_chunk *chunk, *tmp;
141 SCTP_DEBUG_PRINTK("%s: packet:%p\n", __FUNCTION__, packet);
143 list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
144 list_del_init(&chunk->list);
145 sctp_chunk_free(chunk);
148 if (packet->malloced)
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.
159 sctp_xmit_t sctp_packet_transmit_chunk(struct sctp_packet *packet,
160 struct sctp_chunk *chunk)
165 SCTP_DEBUG_PRINTK("%s: packet:%p chunk:%p\n", __FUNCTION__,
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);
173 chunk->skb->sk->sk_err = -error;
175 /* If we have an empty packet, then we can NOT ever
178 retval = sctp_packet_append_chunk(packet, chunk);
182 case SCTP_XMIT_RWND_FULL:
184 case SCTP_XMIT_NAGLE_DELAY:
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)
195 struct sctp_association *asoc = pkt->transport->asoc;
196 struct sctp_chunk *auth;
197 sctp_xmit_t retval = SCTP_XMIT_OK;
199 /* if we don't have an association, we can't do authentication */
203 /* See if this is an auth chunk we are bundling or if
204 * auth is already bundled.
206 if (chunk->chunk_hdr->type == SCTP_CID_AUTH || pkt->auth)
209 /* if the peer did not request this chunk to be authenticated,
215 auth = sctp_make_auth(asoc);
219 retval = sctp_packet_append_chunk(pkt, auth);
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)
228 sctp_xmit_t retval = SCTP_XMIT_OK;
230 /* If sending DATA and haven't aleady bundled a SACK, try to
231 * bundle one in to the packet.
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;
238 if (asoc->a_rwnd > asoc->rwnd) {
239 struct sctp_chunk *sack;
240 asoc->a_rwnd = asoc->rwnd;
241 sack = sctp_make_sack(asoc);
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);
255 /* Append a chunk to the offered packet reporting back any inability to do
258 sctp_xmit_t sctp_packet_append_chunk(struct sctp_packet *packet,
259 struct sctp_chunk *chunk)
261 sctp_xmit_t retval = SCTP_XMIT_OK;
262 __u16 chunk_len = WORD_ROUND(ntohs(chunk->chunk_hdr->length));
267 SCTP_DEBUG_PRINTK("%s: packet:%p chunk:%p\n", __FUNCTION__, packet,
270 /* Try to bundle AUTH chunk */
271 retval = sctp_packet_bundle_auth(packet, chunk);
272 if (retval != SCTP_XMIT_OK)
275 /* Try to bundle SACK chunk */
276 retval = sctp_packet_bundle_sack(packet, chunk);
277 if (retval != SCTP_XMIT_OK)
280 psize = packet->size;
281 pmtu = ((packet->transport->asoc) ?
282 (packet->transport->asoc->pathmtu) :
283 (packet->transport->pathmtu));
285 too_big = (psize + chunk_len > pmtu);
287 /* Decide if we need to fragment or resubmit later. */
289 /* It's OK to fragmet at IP level if any one of the following
291 * 1. The packet is empty (meaning this chunk is greater
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.
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
303 packet->ipfragok = 1;
307 retval = SCTP_XMIT_PMTU_FULL;
313 /* We believe that this chunk is OK to add to the packet (as
314 * long as we have the cwnd for it).
317 /* DATA is a special case since we must examine both rwnd and cwnd
318 * before we send DATA.
320 switch (chunk->chunk_hdr->type) {
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)
332 case SCTP_CID_COOKIE_ECHO:
333 packet->has_cookie_echo = 1;
337 packet->has_sack = 1;
341 packet->has_auth = 1;
342 packet->auth = chunk;
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;
354 /* All packets are sent to the network through this function from
357 * The return value is a normal kernel error return value.
359 int sctp_packet_transmit(struct sctp_packet *packet)
361 struct sctp_transport *tp = packet->transport;
362 struct sctp_association *asoc = tp->asoc;
365 struct sk_buff *nskb;
366 struct sctp_chunk *chunk, *tmp;
369 int padding; /* How much padding do we need? */
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);
375 SCTP_DEBUG_PRINTK("%s: packet:%p\n", __FUNCTION__, packet);
377 /* Do NOT generate a chunkless packet. */
378 if (list_empty(&packet->chunk_list))
381 /* Set up convenience variables... */
382 chunk = list_entry(packet->chunk_list.next, struct sctp_chunk, list);
385 /* Allocate the new skb. */
386 nskb = alloc_skb(packet->size + LL_MAX_HEADER, GFP_ATOMIC);
390 /* Make sure the outbound skb has enough header room reserved. */
391 skb_reserve(nskb, packet->overhead + LL_MAX_HEADER);
393 /* Set the owning socket so that we know where to get the
394 * destination IP address.
396 skb_set_owner_w(nskb, sk);
398 /* The 'obsolete' field of dst is set to 2 when a dst is freed. */
399 if (!dst || (dst->obsolete > 1)) {
401 sctp_transport_route(tp, NULL, sctp_sk(sk));
402 if (asoc && (asoc->param_flags & SPP_PMTUD_ENABLE)) {
403 sctp_assoc_sync_pmtu(asoc);
406 nskb->dst = dst_clone(tp->dst);
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);
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
421 * 1) Fill in the proper Verification Tag in the SCTP common
422 * header and initialize the checksum field to 0's.
424 sh->vtag = htonl(packet->vtag);
430 * An endpoint bundles chunks by simply including multiple
431 * chunks in one outbound SCTP packet. ...
435 * 3.2 Chunk Field Descriptions
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.
444 * [This whole comment explains WORD_ROUND() below.]
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)) {
451 if (!chunk->has_tsn) {
452 sctp_chunk_assign_ssn(chunk);
453 sctp_chunk_assign_tsn(chunk);
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.
462 if (!tp->rto_pending) {
463 chunk->rtt_in_progress = 1;
469 chunk->sent_at = jiffies;
473 padding = WORD_ROUND(chunk->skb->len) - chunk->skb->len;
475 memset(skb_put(chunk->skb, padding), 0, padding);
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.
481 if (chunk == packet->auth)
482 auth = skb_tail_pointer(nskb);
484 cksum_buf_len += chunk->skb->len;
485 memcpy(skb_put(nskb, chunk->skb->len),
486 chunk->skb->data, chunk->skb->len);
488 SCTP_DEBUG_PRINTK("%s %p[%s] %s 0x%x, %s %d, %s %d, %s %d\n",
490 sctp_cname(SCTP_ST_CHUNK(
491 chunk->chunk_hdr->type)),
492 chunk->has_tsn ? "TSN" : "No 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);
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.
504 if (!sctp_chunk_is_data(chunk))
505 sctp_chunk_free(chunk);
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.
518 sctp_auth_calculate_hmac(asoc, nskb,
519 (struct sctp_auth_chunk *)auth,
522 /* 2) Calculate the Adler-32 checksum of the whole packet,
523 * including the SCTP common header and all the
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>.
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);
534 /* 3) Put the resultant value into the checksum field in the
535 * common header, and leave the rest of the bits unchanged.
537 sh->checksum = htonl(crc32);
539 /* IP layer ECN support
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."
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.
548 * Note: The works for IPv6 layer checks this bit too later
549 * in transmission. See IP6_ECN_flow_xmit().
551 INET_ECN_xmit(nskb->sk);
553 /* Set up the IP options. */
554 /* BUG: not implemented
555 * For v4 this all lives somewhere in sk->sk_opt...
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
563 asoc->peer.last_sent_to = tp;
567 struct timer_list *timer;
568 unsigned long timeout;
570 tp->last_time_used = jiffies;
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];
577 if (!mod_timer(timer, jiffies + timeout))
578 sctp_association_hold(asoc);
582 SCTP_DEBUG_PRINTK("***sctp_transmit_packet*** skb len %d\n",
585 if (tp->param_flags & SPP_PMTUD_ENABLE)
586 (*tp->af_specific->sctp_xmit)(nskb, tp, packet->ipfragok);
588 (*tp->af_specific->sctp_xmit)(nskb, tp, 1);
591 packet->size = packet->overhead;
595 IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
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
604 /* err = -EHOSTUNREACH; */
606 /* Control chunks are unreliable so just drop them. DATA chunks
607 * will get resent or dropped later.
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);
621 /********************************************************************
622 * 2nd Level Abstractions
623 ********************************************************************/
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)
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;
637 /* RFC 2960 6.1 Transmission of DATA Chunks
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.
650 rwnd = asoc->peer.rwnd;
651 inflight = asoc->outqueue.outstanding_bytes;
653 datasize = sctp_data_size(chunk);
655 if (datasize > rwnd) {
657 /* We have (at least) one data chunk in flight,
658 * so we can't fall back to rule 6.1 B).
660 retval = SCTP_XMIT_RWND_FULL;
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
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, "
680 __FUNCTION__, transport,
683 transport->flight_size,
684 transport->partial_bytes_acked);
687 /* RFC 2960 6.1 Transmission of DATA Chunks
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.
693 /* RFC 7.2.4 & the Implementers Guide 2.8.
696 * When a Fast Retransmit is being performed the sender SHOULD
697 * ignore the value of cwnd and SHOULD NOT delay retransmission.
699 if (chunk->fast_retransmit <= 0)
700 if (transport->flight_size >= transport->cwnd) {
701 retval = SCTP_XMIT_RWND_FULL;
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
710 if (!sp->nodelay && sctp_packet_empty(packet) &&
711 q->outstanding_bytes && sctp_state(asoc, ESTABLISHED)) {
712 unsigned len = datasize + q->out_qlen;
714 /* Check whether this chunk and all the rest of pending
715 * data will fit or delay in hopes of bundling a full
718 if (len < asoc->frag_point) {
719 retval = SCTP_XMIT_NAGLE_DELAY;
724 /* Keep track of how many bytes are in flight over this transport. */
725 transport->flight_size += datasize;
727 /* Keep track of how many bytes are in flight to the receiver. */
728 asoc->outqueue.outstanding_bytes += datasize;
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.
736 datasize += sizeof(struct sk_buff);
742 asoc->peer.rwnd = rwnd;
743 /* Has been accepted for transmission. */
744 if (!asoc->peer.prsctp_capable)
745 chunk->msg->can_abandon = 0;