Merge with Linux 2.5.59.
[linux-2.6/linux-mips.git] / net / sctp / output.c
blob0a93a0f8707e995753d2042dfd4af14b6e0bfbf8
1 /* SCTP kernel reference Implementation
2 * Copyright (c) 1999-2000 Cisco, Inc.
3 * Copyright (c) 1999-2001 Motorola, Inc.
4 * Copyright (c) 2001 International Business Machines, Corp.
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 __u32 count_crc(__u8 *ptr, __u16 count);
66 static void sctp_packet_reset(sctp_packet_t *packet);
67 static sctp_xmit_t sctp_packet_append_data(sctp_packet_t *packet,
68 sctp_chunk_t *chunk);
70 /* Config a packet.
71 * This appears to be a followup set of initializations.)
73 sctp_packet_t *sctp_packet_config(sctp_packet_t *packet,
74 __u32 vtag,
75 int ecn_capable,
76 sctp_packet_phandler_t *prepend_handler)
78 int packet_empty = (packet->size == SCTP_IP_OVERHEAD);
80 packet->vtag = vtag;
81 packet->ecn_capable = ecn_capable;
82 packet->get_prepend_chunk = prepend_handler;
83 packet->has_cookie_echo = 0;
85 /* We might need to call the prepend_handler right away. */
86 if (packet_empty)
87 sctp_packet_reset(packet);
88 return packet;
91 /* Initialize the packet structure. */
92 sctp_packet_t *sctp_packet_init(sctp_packet_t *packet,
93 sctp_transport_t *transport,
94 __u16 sport,
95 __u16 dport)
97 packet->transport = transport;
98 packet->source_port = sport;
99 packet->destination_port = dport;
100 skb_queue_head_init(&packet->chunks);
101 packet->vtag = 0;
102 packet->ecn_capable = 0;
103 packet->get_prepend_chunk = NULL;
104 packet->has_cookie_echo = 0;
105 packet->malloced = 0;
106 sctp_packet_reset(packet);
107 return packet;
110 /* Free a packet. */
111 void sctp_packet_free(sctp_packet_t *packet)
113 sctp_chunk_t *chunk;
115 while (NULL !=
116 (chunk = (sctp_chunk_t *)skb_dequeue(&packet->chunks))) {
117 sctp_free_chunk(chunk);
120 if (packet->malloced)
121 kfree(packet);
124 /* This routine tries to append the chunk to the offered packet. If adding
125 * the chunk causes the packet to exceed the path MTU and COOKIE_ECHO chunk
126 * is not present in the packet, it transmits the input packet.
127 * Data can be bundled with a packet containing a COOKIE_ECHO chunk as long
128 * as it can fit in the packet, but any more data that does not fit in this
129 * packet can be sent only after receiving the COOKIE_ACK.
131 sctp_xmit_t sctp_packet_transmit_chunk(sctp_packet_t *packet,
132 sctp_chunk_t *chunk)
134 sctp_xmit_t retval;
135 int error = 0;
137 switch ((retval = (sctp_packet_append_chunk(packet, chunk)))) {
138 case SCTP_XMIT_PMTU_FULL:
139 if (!packet->has_cookie_echo) {
140 error = sctp_packet_transmit(packet);
141 if (error < 0)
142 chunk->skb->sk->err = -error;
144 /* If we have an empty packet, then we can NOT ever
145 * return PMTU_FULL.
147 retval = sctp_packet_append_chunk(packet, chunk);
149 break;
151 case SCTP_XMIT_MUST_FRAG:
152 case SCTP_XMIT_RWND_FULL:
153 case SCTP_XMIT_OK:
154 break;
157 return retval;
160 /* Append a chunk to the offered packet reporting back any inability to do
161 * so.
163 sctp_xmit_t sctp_packet_append_chunk(sctp_packet_t *packet, sctp_chunk_t *chunk)
165 sctp_xmit_t retval = SCTP_XMIT_OK;
166 __u16 chunk_len = WORD_ROUND(ntohs(chunk->chunk_hdr->length));
167 size_t psize = packet->size;
168 size_t pmtu;
169 int too_big;
171 pmtu = ((packet->transport->asoc) ?
172 (packet->transport->asoc->pmtu) :
173 (packet->transport->pmtu));
175 too_big = (psize + chunk_len > pmtu);
177 /* Decide if we need to fragment or resubmit later. */
178 if (too_big) {
179 int packet_empty = (packet->size == SCTP_IP_OVERHEAD);
181 /* Both control chunks and data chunks with TSNs are
182 * non-fragmentable.
184 int fragmentable = sctp_chunk_is_data(chunk) &&
185 (!chunk->has_tsn);
186 if (packet_empty) {
187 if (fragmentable) {
188 retval = SCTP_XMIT_MUST_FRAG;
189 goto finish;
190 } else {
191 /* The packet is too big but we can
192 * not fragment it--we have to just
193 * transmit and rely on IP
194 * fragmentation.
196 goto append;
198 } else { /* !packet_empty */
199 retval = SCTP_XMIT_PMTU_FULL;
200 goto finish;
202 } else {
203 /* The chunk fits in the packet. */
204 goto append;
207 append:
208 /* We believe that this chunk is OK to add to the packet (as
209 * long as we have the cwnd for it).
212 /* DATA is a special case since we must examine both rwnd and cwnd
213 * before we send DATA.
215 if (sctp_chunk_is_data(chunk)) {
216 retval = sctp_packet_append_data(packet, chunk);
217 if (SCTP_XMIT_OK != retval)
218 goto finish;
219 } else if (SCTP_CID_COOKIE_ECHO == chunk->chunk_hdr->type) {
220 packet->has_cookie_echo = 1;
223 /* It is OK to send this chunk. */
224 skb_queue_tail(&packet->chunks, (struct sk_buff *)chunk);
225 packet->size += chunk_len;
226 finish:
227 return retval;
230 /* All packets are sent to the network through this function from
231 * sctp_push_outqueue().
233 * The return value is a normal kernel error return value.
235 int sctp_packet_transmit(sctp_packet_t *packet)
237 sctp_transport_t *transport = packet->transport;
238 sctp_association_t *asoc = transport->asoc;
239 struct sctphdr *sh;
240 __u32 crc32;
241 struct sk_buff *nskb;
242 sctp_chunk_t *chunk;
243 struct sock *sk;
244 int err = 0;
245 int padding; /* How much padding do we need? */
246 __u8 packet_has_data = 0;
247 struct dst_entry *dst;
249 /* Do NOT generate a chunkless packet... */
250 if (skb_queue_empty(&packet->chunks))
251 return err;
253 /* Set up convenience variables... */
254 chunk = (sctp_chunk_t *) (packet->chunks.next);
255 sk = chunk->skb->sk;
257 /* Allocate the new skb. */
258 nskb = dev_alloc_skb(packet->size);
259 if (!nskb) {
260 err = -ENOMEM;
261 goto out;
264 /* Make sure the outbound skb has enough header room reserved. */
265 skb_reserve(nskb, SCTP_IP_OVERHEAD);
267 /* Set the owning socket so that we know where to get the
268 * destination IP address.
270 skb_set_owner_w(nskb, sk);
273 * 6.10 Bundling
275 * An endpoint bundles chunks by simply including multiple
276 * chunks in one outbound SCTP packet. ...
280 * 3.2 Chunk Field Descriptions
282 * The total length of a chunk (including Type, Length and
283 * Value fields) MUST be a multiple of 4 bytes. If the length
284 * of the chunk is not a multiple of 4 bytes, the sender MUST
285 * pad the chunk with all zero bytes and this padding is not
286 * included in the chunk length field. The sender should
287 * never pad with more than 3 bytes.
289 * [This whole comment explains WORD_ROUND() below.]
291 SCTP_DEBUG_PRINTK("***sctp_transmit_packet***\n");
292 while (NULL != (chunk = (sctp_chunk_t *)
293 skb_dequeue(&packet->chunks))) {
294 chunk->num_times_sent++;
295 chunk->sent_at = jiffies;
296 if (sctp_chunk_is_data(chunk)) {
297 sctp_chunk_assign_tsn(chunk);
299 /* 6.3.1 C4) When data is in flight and when allowed
300 * by rule C5, a new RTT measurement MUST be made each
301 * round trip. Furthermore, new RTT measurements
302 * SHOULD be made no more than once per round-trip
303 * for a given destination transport address.
305 if ((1 == chunk->num_times_sent) &&
306 (!transport->rto_pending)) {
307 chunk->rtt_in_progress = 1;
308 transport->rto_pending = 1;
310 packet_has_data = 1;
312 memcpy(skb_put(nskb, chunk->skb->len),
313 chunk->skb->data, chunk->skb->len);
314 padding = WORD_ROUND(chunk->skb->len) - chunk->skb->len;
315 memset(skb_put(nskb, padding), 0, padding);
316 SCTP_DEBUG_PRINTK("%s %p[%s] %s 0x%x, %s %d, %s %d, %s %d, "
317 "%s %d\n",
318 "*** Chunk", chunk,
319 sctp_cname(SCTP_ST_CHUNK(
320 chunk->chunk_hdr->type)),
321 chunk->has_tsn ? "TSN" : "No TSN",
322 chunk->has_tsn ?
323 ntohl(chunk->subh.data_hdr->tsn) : 0,
324 "length", ntohs(chunk->chunk_hdr->length),
325 "chunk->skb->len", chunk->skb->len,
326 "num_times_sent", chunk->num_times_sent,
327 "rtt_in_progress", chunk->rtt_in_progress);
330 * If this is a control chunk, this is our last
331 * reference. Free data chunks after they've been
332 * acknowledged or have failed.
334 if (!sctp_chunk_is_data(chunk))
335 sctp_free_chunk(chunk);
338 /* Build the SCTP header. */
339 sh = (struct sctphdr *)skb_push(nskb, sizeof(struct sctphdr));
340 sh->source = htons(packet->source_port);
341 sh->dest = htons(packet->destination_port);
343 /* From 6.8 Adler-32 Checksum Calculation:
344 * After the packet is constructed (containing the SCTP common
345 * header and one or more control or DATA chunks), the
346 * transmitter shall:
348 * 1) Fill in the proper Verification Tag in the SCTP common
349 * header and initialize the checksum field to 0's.
351 sh->vtag = htonl(packet->vtag);
352 sh->checksum = 0;
354 /* 2) Calculate the Adler-32 checksum of the whole packet,
355 * including the SCTP common header and all the
356 * chunks.
358 * Note: Adler-32 is no longer applicable, as has been replaced
359 * by CRC32-C as described in <draft-ietf-tsvwg-sctpcsum-02.txt>.
361 crc32 = count_crc((__u8 *)sh, nskb->len);
363 /* 3) Put the resultant value into the checksum field in the
364 * common header, and leave the rest of the bits unchanged.
366 sh->checksum = htonl(crc32);
368 /* FIXME: Delete the rest of this switch statement once phase 2
369 * of address selection (ipv6 support) drops in.
371 switch (transport->ipaddr.sa.sa_family) {
372 case AF_INET6:
373 SCTP_V6(inet6_sk(sk)->daddr = transport->ipaddr.v6.sin6_addr;)
374 break;
377 /* IP layer ECN support
378 * From RFC 2481
379 * "The ECN-Capable Transport (ECT) bit would be set by the
380 * data sender to indicate that the end-points of the
381 * transport protocol are ECN-capable."
383 * If ECN capable && negotiated && it makes sense for
384 * this packet to support it (e.g. post ECN negotiation)
385 * then lets set the ECT bit
387 * FIXME: Need to do something else for IPv6
389 if (packet->ecn_capable) {
390 INET_ECN_xmit(nskb->sk);
391 } else {
392 INET_ECN_dontxmit(nskb->sk);
395 /* Set up the IP options. */
396 /* BUG: not implemented
397 * For v4 this all lives somewhere in sk->opt...
400 /* Dump that on IP! */
401 if (asoc && asoc->peer.last_sent_to != transport) {
402 /* Considering the multiple CPU scenario, this is a
403 * "correcter" place for last_sent_to. --xguo
405 asoc->peer.last_sent_to = transport;
408 if (packet_has_data) {
409 struct timer_list *timer;
410 unsigned long timeout;
412 transport->last_time_used = jiffies;
414 /* Restart the AUTOCLOSE timer when sending data. */
415 if ((SCTP_STATE_ESTABLISHED == asoc->state) &&
416 (asoc->autoclose)) {
417 timer = &asoc->timers[SCTP_EVENT_TIMEOUT_AUTOCLOSE];
418 timeout = asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE];
420 if (!mod_timer(timer, jiffies + timeout))
421 sctp_association_hold(asoc);
425 dst = transport->dst;
426 if (!dst || dst->obsolete) {
427 sctp_transport_route(transport, NULL, sctp_sk(sk));
430 nskb->dst = dst_clone(transport->dst);
431 if (!nskb->dst)
432 goto no_route;
434 SCTP_DEBUG_PRINTK("***sctp_transmit_packet*** skb length %d\n",
435 nskb->len);
436 (*transport->af_specific->queue_xmit)(nskb);
437 out:
438 packet->size = SCTP_IP_OVERHEAD;
439 return err;
440 no_route:
441 kfree_skb(nskb);
442 IP_INC_STATS_BH(IpOutNoRoutes);
443 err = -EHOSTUNREACH;
444 goto out;
447 /********************************************************************
448 * 2nd Level Abstractions
449 ********************************************************************/
452 * This private function resets the packet to a fresh state.
454 static void sctp_packet_reset(sctp_packet_t *packet)
456 sctp_chunk_t *chunk = NULL;
458 packet->size = SCTP_IP_OVERHEAD;
460 if (packet->get_prepend_chunk)
461 chunk = packet->get_prepend_chunk(packet->transport->asoc);
463 /* If there a is a prepend chunk stick it on the list before
464 * any other chunks get appended.
466 if (chunk)
467 sctp_packet_append_chunk(packet, chunk);
470 /* This private function handles the specifics of appending DATA chunks. */
471 static sctp_xmit_t sctp_packet_append_data(sctp_packet_t *packet,
472 sctp_chunk_t *chunk)
474 sctp_xmit_t retval = SCTP_XMIT_OK;
475 size_t datasize, rwnd, inflight;
476 sctp_transport_t *transport = packet->transport;
477 __u32 max_burst_bytes;
479 /* RFC 2960 6.1 Transmission of DATA Chunks
481 * A) At any given time, the data sender MUST NOT transmit new data to
482 * any destination transport address if its peer's rwnd indicates
483 * that the peer has no buffer space (i.e. rwnd is 0, see Section
484 * 6.2.1). However, regardless of the value of rwnd (including if it
485 * is 0), the data sender can always have one DATA chunk in flight to
486 * the receiver if allowed by cwnd (see rule B below). This rule
487 * allows the sender to probe for a change in rwnd that the sender
488 * missed due to the SACK having been lost in transit from the data
489 * receiver to the data sender.
492 rwnd = transport->asoc->peer.rwnd;
493 inflight = transport->asoc->outqueue.outstanding_bytes;
495 datasize = sctp_data_size(chunk);
497 if (datasize > rwnd) {
498 if (inflight > 0) {
499 /* We have (at least) one data chunk in flight,
500 * so we can't fall back to rule 6.1 B).
502 retval = SCTP_XMIT_RWND_FULL;
503 goto finish;
507 /* sctpimpguide-05 2.14.2
508 * D) When the time comes for the sender to
509 * transmit new DATA chunks, the protocol parameter Max.Burst MUST
510 * first be applied to limit how many new DATA chunks may be sent.
511 * The limit is applied by adjusting cwnd as follows:
512 * if ((flightsize + Max.Burst * MTU) < cwnd)
513 * cwnd = flightsize + Max.Burst * MTU
515 max_burst_bytes = transport->asoc->max_burst * transport->asoc->pmtu;
516 if ((transport->flight_size + max_burst_bytes) < transport->cwnd) {
517 transport->cwnd = transport->flight_size + max_burst_bytes;
518 SCTP_DEBUG_PRINTK("%s: cwnd limited by max_burst: "
519 "transport: %p, cwnd: %d, "
520 "ssthresh: %d, flight_size: %d, "
521 "pba: %d\n",
522 __FUNCTION__, transport,
523 transport->cwnd,
524 transport->ssthresh,
525 transport->flight_size,
526 transport->partial_bytes_acked);
529 /* RFC 2960 6.1 Transmission of DATA Chunks
531 * B) At any given time, the sender MUST NOT transmit new data
532 * to a given transport address if it has cwnd or more bytes
533 * of data outstanding to that transport address.
535 /* RFC 7.2.4 & the Implementers Guide 2.8.
537 * 3) ...
538 * When a Fast Retransmit is being performed the sender SHOULD
539 * ignore the value of cwnd and SHOULD NOT delay retransmission.
541 if (!chunk->fast_retransmit) {
542 if (transport->flight_size >= transport->cwnd) {
543 retval = SCTP_XMIT_RWND_FULL;
544 goto finish;
548 /* Keep track of how many bytes are in flight over this transport. */
549 transport->flight_size += datasize;
551 /* Keep track of how many bytes are in flight to the receiver. */
552 transport->asoc->outqueue.outstanding_bytes += datasize;
554 /* Update our view of the receiver's rwnd. */
555 if (datasize < rwnd) {
556 rwnd -= datasize;
557 } else {
558 rwnd = 0;
561 transport->asoc->peer.rwnd = rwnd;
563 finish:
564 return retval;