Merge master.kernel.org:/home/rmk/linux-2.6-serial
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / sctp / output.c
blob437cba7260a4a1a6970546cbf36f650114a785ab
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)
14 * any later version.
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
28 * email address(es):
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>
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>
54 #ifndef TEST_FRAME
55 #include <net/tcp.h>
56 #endif /* TEST_FRAME (not defined) */
58 #include <linux/socket.h> /* for sa_family_t */
59 #include <net/sock.h>
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);
68 /* Config a packet.
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__,
77 packet, vtag);
79 packet->vtag = vtag;
80 packet->has_cookie_echo = 0;
81 packet->has_sack = 0;
82 packet->ipfragok = 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.
90 if (chunk)
91 sctp_packet_append_chunk(packet, chunk);
94 return packet;
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;
103 size_t overhead;
105 SCTP_DEBUG_PRINTK("%s: packet:%p transport:%p\n", __FUNCTION__,
106 packet, transport);
108 packet->transport = transport;
109 packet->source_port = sport;
110 packet->destination_port = dport;
111 INIT_LIST_HEAD(&packet->chunk_list);
112 if (asoc) {
113 struct sctp_sock *sp = sctp_sk(asoc->base.sk);
114 overhead = sp->pf->af->net_header_len;
115 } else {
116 overhead = sizeof(struct ipv6hdr);
118 overhead += sizeof(struct sctphdr);
119 packet->overhead = overhead;
120 packet->size = overhead;
121 packet->vtag = 0;
122 packet->has_cookie_echo = 0;
123 packet->has_sack = 0;
124 packet->ipfragok = 0;
125 packet->malloced = 0;
126 return packet;
129 /* Free a packet. */
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)
142 kfree(packet);
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)
155 sctp_xmit_t retval;
156 int error = 0;
158 SCTP_DEBUG_PRINTK("%s: packet:%p chunk:%p\n", __FUNCTION__,
159 packet, chunk);
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);
165 if (error < 0)
166 chunk->skb->sk->sk_err = -error;
168 /* If we have an empty packet, then we can NOT ever
169 * return PMTU_FULL.
171 retval = sctp_packet_append_chunk(packet, chunk);
173 break;
175 case SCTP_XMIT_RWND_FULL:
176 case SCTP_XMIT_OK:
177 case SCTP_XMIT_NAGLE_DELAY:
178 break;
181 return retval;
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);
202 if (sack) {
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);
212 return retval;
215 /* Append a chunk to the offered packet reporting back any inability to do
216 * so.
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));
223 size_t psize;
224 size_t pmtu;
225 int too_big;
227 SCTP_DEBUG_PRINTK("%s: packet:%p chunk:%p\n", __FUNCTION__, packet,
228 chunk);
230 retval = sctp_packet_bundle_sack(packet, chunk);
231 psize = packet->size;
233 if (retval != SCTP_XMIT_OK)
234 goto finish;
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. */
243 if (too_big) {
244 /* Both control chunks and data chunks with TSNs are
245 * non-fragmentable.
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;
253 goto append;
255 } else {
256 retval = SCTP_XMIT_PMTU_FULL;
257 goto finish;
261 append:
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)
274 goto finish;
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;
284 finish:
285 return retval;
288 /* All packets are sent to the network through this function from
289 * sctp_outq_tail().
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;
297 struct sctphdr *sh;
298 __u32 crc32;
299 struct sk_buff *nskb;
300 struct sctp_chunk *chunk, *tmp;
301 struct sock *sk;
302 int err = 0;
303 int padding; /* How much padding do we need? */
304 __u8 has_data = 0;
305 struct dst_entry *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))
311 return err;
313 /* Set up convenience variables... */
314 chunk = list_entry(packet->chunk_list.next, struct sctp_chunk, list);
315 sk = chunk->skb->sk;
317 /* Allocate the new skb. */
318 nskb = alloc_skb(packet->size + LL_MAX_HEADER, GFP_ATOMIC);
319 if (!nskb)
320 goto nomem;
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 /* Build the SCTP header. */
331 sh = (struct sctphdr *)skb_push(nskb, sizeof(struct sctphdr));
332 sh->source = htons(packet->source_port);
333 sh->dest = htons(packet->destination_port);
335 /* From 6.8 Adler-32 Checksum Calculation:
336 * After the packet is constructed (containing the SCTP common
337 * header and one or more control or DATA chunks), the
338 * transmitter shall:
340 * 1) Fill in the proper Verification Tag in the SCTP common
341 * header and initialize the checksum field to 0's.
343 sh->vtag = htonl(packet->vtag);
344 sh->checksum = 0;
346 /* 2) Calculate the Adler-32 checksum of the whole packet,
347 * including the SCTP common header and all the
348 * chunks.
350 * Note: Adler-32 is no longer applicable, as has been replaced
351 * by CRC32-C as described in <draft-ietf-tsvwg-sctpcsum-02.txt>.
353 crc32 = sctp_start_cksum((__u8 *)sh, sizeof(struct sctphdr));
356 * 6.10 Bundling
358 * An endpoint bundles chunks by simply including multiple
359 * chunks in one outbound SCTP packet. ...
363 * 3.2 Chunk Field Descriptions
365 * The total length of a chunk (including Type, Length and
366 * Value fields) MUST be a multiple of 4 bytes. If the length
367 * of the chunk is not a multiple of 4 bytes, the sender MUST
368 * pad the chunk with all zero bytes and this padding is not
369 * included in the chunk length field. The sender should
370 * never pad with more than 3 bytes.
372 * [This whole comment explains WORD_ROUND() below.]
374 SCTP_DEBUG_PRINTK("***sctp_transmit_packet***\n");
375 list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
376 list_del_init(&chunk->list);
377 if (sctp_chunk_is_data(chunk)) {
379 if (!chunk->has_tsn) {
380 sctp_chunk_assign_ssn(chunk);
381 sctp_chunk_assign_tsn(chunk);
383 /* 6.3.1 C4) When data is in flight and when allowed
384 * by rule C5, a new RTT measurement MUST be made each
385 * round trip. Furthermore, new RTT measurements
386 * SHOULD be made no more than once per round-trip
387 * for a given destination transport address.
390 if (!tp->rto_pending) {
391 chunk->rtt_in_progress = 1;
392 tp->rto_pending = 1;
394 } else
395 chunk->resent = 1;
397 chunk->sent_at = jiffies;
398 has_data = 1;
401 padding = WORD_ROUND(chunk->skb->len) - chunk->skb->len;
402 if (padding)
403 memset(skb_put(chunk->skb, padding), 0, padding);
405 crc32 = sctp_update_copy_cksum(skb_put(nskb, chunk->skb->len),
406 chunk->skb->data,
407 chunk->skb->len, crc32);
409 SCTP_DEBUG_PRINTK("%s %p[%s] %s 0x%x, %s %d, %s %d, %s %d\n",
410 "*** Chunk", chunk,
411 sctp_cname(SCTP_ST_CHUNK(
412 chunk->chunk_hdr->type)),
413 chunk->has_tsn ? "TSN" : "No TSN",
414 chunk->has_tsn ?
415 ntohl(chunk->subh.data_hdr->tsn) : 0,
416 "length", ntohs(chunk->chunk_hdr->length),
417 "chunk->skb->len", chunk->skb->len,
418 "rtt_in_progress", chunk->rtt_in_progress);
421 * If this is a control chunk, this is our last
422 * reference. Free data chunks after they've been
423 * acknowledged or have failed.
425 if (!sctp_chunk_is_data(chunk))
426 sctp_chunk_free(chunk);
429 /* Perform final transformation on checksum. */
430 crc32 = sctp_end_cksum(crc32);
432 /* 3) Put the resultant value into the checksum field in the
433 * common header, and leave the rest of the bits unchanged.
435 sh->checksum = htonl(crc32);
437 /* IP layer ECN support
438 * From RFC 2481
439 * "The ECN-Capable Transport (ECT) bit would be set by the
440 * data sender to indicate that the end-points of the
441 * transport protocol are ECN-capable."
443 * Now setting the ECT bit all the time, as it should not cause
444 * any problems protocol-wise even if our peer ignores it.
446 * Note: The works for IPv6 layer checks this bit too later
447 * in transmission. See IP6_ECN_flow_xmit().
449 INET_ECN_xmit(nskb->sk);
451 /* Set up the IP options. */
452 /* BUG: not implemented
453 * For v4 this all lives somewhere in sk->sk_opt...
456 /* Dump that on IP! */
457 if (asoc && asoc->peer.last_sent_to != tp) {
458 /* Considering the multiple CPU scenario, this is a
459 * "correcter" place for last_sent_to. --xguo
461 asoc->peer.last_sent_to = tp;
464 if (has_data) {
465 struct timer_list *timer;
466 unsigned long timeout;
468 tp->last_time_used = jiffies;
470 /* Restart the AUTOCLOSE timer when sending data. */
471 if (sctp_state(asoc, ESTABLISHED) && asoc->autoclose) {
472 timer = &asoc->timers[SCTP_EVENT_TIMEOUT_AUTOCLOSE];
473 timeout = asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE];
475 if (!mod_timer(timer, jiffies + timeout))
476 sctp_association_hold(asoc);
480 dst = tp->dst;
481 /* The 'obsolete' field of dst is set to 2 when a dst is freed. */
482 if (!dst || (dst->obsolete > 1)) {
483 dst_release(dst);
484 sctp_transport_route(tp, NULL, sctp_sk(sk));
485 if (asoc->param_flags & SPP_PMTUD_ENABLE) {
486 sctp_assoc_sync_pmtu(asoc);
490 nskb->dst = dst_clone(tp->dst);
491 if (!nskb->dst)
492 goto no_route;
494 SCTP_DEBUG_PRINTK("***sctp_transmit_packet*** skb len %d\n",
495 nskb->len);
497 if (tp->param_flags & SPP_PMTUD_ENABLE)
498 (*tp->af_specific->sctp_xmit)(nskb, tp, packet->ipfragok);
499 else
500 (*tp->af_specific->sctp_xmit)(nskb, tp, 1);
502 out:
503 packet->size = packet->overhead;
504 return err;
505 no_route:
506 kfree_skb(nskb);
507 IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
509 /* FIXME: Returning the 'err' will effect all the associations
510 * associated with a socket, although only one of the paths of the
511 * association is unreachable.
512 * The real failure of a transport or association can be passed on
513 * to the user via notifications. So setting this error may not be
514 * required.
516 /* err = -EHOSTUNREACH; */
517 err:
518 /* Control chunks are unreliable so just drop them. DATA chunks
519 * will get resent or dropped later.
522 list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
523 list_del_init(&chunk->list);
524 if (!sctp_chunk_is_data(chunk))
525 sctp_chunk_free(chunk);
527 goto out;
528 nomem:
529 err = -ENOMEM;
530 goto err;
533 /********************************************************************
534 * 2nd Level Abstractions
535 ********************************************************************/
537 /* This private function handles the specifics of appending DATA chunks. */
538 static sctp_xmit_t sctp_packet_append_data(struct sctp_packet *packet,
539 struct sctp_chunk *chunk)
541 sctp_xmit_t retval = SCTP_XMIT_OK;
542 size_t datasize, rwnd, inflight;
543 struct sctp_transport *transport = packet->transport;
544 __u32 max_burst_bytes;
545 struct sctp_association *asoc = transport->asoc;
546 struct sctp_sock *sp = sctp_sk(asoc->base.sk);
547 struct sctp_outq *q = &asoc->outqueue;
549 /* RFC 2960 6.1 Transmission of DATA Chunks
551 * A) At any given time, the data sender MUST NOT transmit new data to
552 * any destination transport address if its peer's rwnd indicates
553 * that the peer has no buffer space (i.e. rwnd is 0, see Section
554 * 6.2.1). However, regardless of the value of rwnd (including if it
555 * is 0), the data sender can always have one DATA chunk in flight to
556 * the receiver if allowed by cwnd (see rule B below). This rule
557 * allows the sender to probe for a change in rwnd that the sender
558 * missed due to the SACK having been lost in transit from the data
559 * receiver to the data sender.
562 rwnd = asoc->peer.rwnd;
563 inflight = asoc->outqueue.outstanding_bytes;
565 datasize = sctp_data_size(chunk);
567 if (datasize > rwnd) {
568 if (inflight > 0) {
569 /* We have (at least) one data chunk in flight,
570 * so we can't fall back to rule 6.1 B).
572 retval = SCTP_XMIT_RWND_FULL;
573 goto finish;
577 /* sctpimpguide-05 2.14.2
578 * D) When the time comes for the sender to
579 * transmit new DATA chunks, the protocol parameter Max.Burst MUST
580 * first be applied to limit how many new DATA chunks may be sent.
581 * The limit is applied by adjusting cwnd as follows:
582 * if ((flightsize + Max.Burst * MTU) < cwnd)
583 * cwnd = flightsize + Max.Burst * MTU
585 max_burst_bytes = asoc->max_burst * asoc->pathmtu;
586 if ((transport->flight_size + max_burst_bytes) < transport->cwnd) {
587 transport->cwnd = transport->flight_size + max_burst_bytes;
588 SCTP_DEBUG_PRINTK("%s: cwnd limited by max_burst: "
589 "transport: %p, cwnd: %d, "
590 "ssthresh: %d, flight_size: %d, "
591 "pba: %d\n",
592 __FUNCTION__, transport,
593 transport->cwnd,
594 transport->ssthresh,
595 transport->flight_size,
596 transport->partial_bytes_acked);
599 /* RFC 2960 6.1 Transmission of DATA Chunks
601 * B) At any given time, the sender MUST NOT transmit new data
602 * to a given transport address if it has cwnd or more bytes
603 * of data outstanding to that transport address.
605 /* RFC 7.2.4 & the Implementers Guide 2.8.
607 * 3) ...
608 * When a Fast Retransmit is being performed the sender SHOULD
609 * ignore the value of cwnd and SHOULD NOT delay retransmission.
611 if (chunk->fast_retransmit <= 0)
612 if (transport->flight_size >= transport->cwnd) {
613 retval = SCTP_XMIT_RWND_FULL;
614 goto finish;
617 /* Nagle's algorithm to solve small-packet problem:
618 * Inhibit the sending of new chunks when new outgoing data arrives
619 * if any previously transmitted data on the connection remains
620 * unacknowledged.
622 if (!sp->nodelay && sctp_packet_empty(packet) &&
623 q->outstanding_bytes && sctp_state(asoc, ESTABLISHED)) {
624 unsigned len = datasize + q->out_qlen;
626 /* Check whether this chunk and all the rest of pending
627 * data will fit or delay in hopes of bundling a full
628 * sized packet.
630 if (len < asoc->pathmtu - packet->overhead) {
631 retval = SCTP_XMIT_NAGLE_DELAY;
632 goto finish;
636 /* Keep track of how many bytes are in flight over this transport. */
637 transport->flight_size += datasize;
639 /* Keep track of how many bytes are in flight to the receiver. */
640 asoc->outqueue.outstanding_bytes += datasize;
642 /* Update our view of the receiver's rwnd. */
643 if (datasize < rwnd)
644 rwnd -= datasize;
645 else
646 rwnd = 0;
648 asoc->peer.rwnd = rwnd;
649 /* Has been accepted for transmission. */
650 if (!asoc->peer.prsctp_capable)
651 chunk->msg->can_abandon = 0;
653 finish:
654 return retval;