]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/sctp/output.c
[IPV4] fib_trie: Add credits.
[net-next-2.6.git] / net / sctp / output.c
CommitLineData
1da177e4
LT
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.
5 *
6 * This file is part of the SCTP kernel reference Implementation
7 *
8 * These functions handle output processing.
9 *
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.
15 *
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.
21 *
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.
26 *
27 * Please send any bug reports or fixes you make to the
28 * email address(es):
29 * lksctp developers <lksctp-developers@lists.sourceforge.net>
30 *
31 * Or submit a bug report through the following website:
32 * http://www.sf.net/projects/lksctp
33 *
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>
39 *
40 * Any bugs reported given to us we will try to fix... any fixes shared will
41 * be incorporated into the next SCTP release.
42 */
43
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>
53
54#ifndef TEST_FRAME
55#include <net/tcp.h>
56#endif /* TEST_FRAME (not defined) */
57
58#include <linux/socket.h> /* for sa_family_t */
59#include <net/sock.h>
60
61#include <net/sctp/sctp.h>
62#include <net/sctp/sm.h>
63
64/* Forward declarations for private helpers. */
65static sctp_xmit_t sctp_packet_append_data(struct sctp_packet *packet,
66 struct sctp_chunk *chunk);
67
68/* Config a packet.
69 * This appears to be a followup set of initializations.
70 */
71struct sctp_packet *sctp_packet_config(struct sctp_packet *packet,
72 __u32 vtag, int ecn_capable)
73{
74 struct sctp_chunk *chunk = NULL;
75
76 SCTP_DEBUG_PRINTK("%s: packet:%p vtag:0x%x\n", __FUNCTION__,
77 packet, vtag);
78
79 packet->vtag = vtag;
80 packet->has_cookie_echo = 0;
81 packet->has_sack = 0;
82 packet->ipfragok = 0;
83
84 if (ecn_capable && sctp_packet_empty(packet)) {
85 chunk = sctp_get_ecne_prepend(packet->transport->asoc);
86
87 /* If there a is a prepend chunk stick it on the list before
88 * any other chunks get appended.
89 */
90 if (chunk)
91 sctp_packet_append_chunk(packet, chunk);
92 }
93
94 return packet;
95}
96
97/* Initialize the packet structure. */
98struct sctp_packet *sctp_packet_init(struct sctp_packet *packet,
99 struct sctp_transport *transport,
100 __u16 sport, __u16 dport)
101{
102 struct sctp_association *asoc = transport->asoc;
103 size_t overhead;
104
105 SCTP_DEBUG_PRINTK("%s: packet:%p transport:%p\n", __FUNCTION__,
106 packet, transport);
107
108 packet->transport = transport;
109 packet->source_port = sport;
110 packet->destination_port = dport;
79af02c2 111 INIT_LIST_HEAD(&packet->chunk_list);
1da177e4
LT
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);
117 }
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;
127}
128
129/* Free a packet. */
130void sctp_packet_free(struct sctp_packet *packet)
131{
79af02c2 132 struct sctp_chunk *chunk, *tmp;
1da177e4
LT
133
134 SCTP_DEBUG_PRINTK("%s: packet:%p\n", __FUNCTION__, packet);
135
79af02c2
DM
136 list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
137 list_del_init(&chunk->list);
1da177e4 138 sctp_chunk_free(chunk);
79af02c2 139 }
1da177e4
LT
140
141 if (packet->malloced)
142 kfree(packet);
143}
144
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.
151 */
152sctp_xmit_t sctp_packet_transmit_chunk(struct sctp_packet *packet,
153 struct sctp_chunk *chunk)
154{
155 sctp_xmit_t retval;
156 int error = 0;
157
158 SCTP_DEBUG_PRINTK("%s: packet:%p chunk:%p\n", __FUNCTION__,
159 packet, chunk);
160
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;
167
168 /* If we have an empty packet, then we can NOT ever
169 * return PMTU_FULL.
170 */
171 retval = sctp_packet_append_chunk(packet, chunk);
172 }
173 break;
174
175 case SCTP_XMIT_RWND_FULL:
176 case SCTP_XMIT_OK:
177 case SCTP_XMIT_NAGLE_DELAY:
178 break;
179 };
180
181 return retval;
182}
183
184/* Try to bundle a SACK with the packet. */
185static sctp_xmit_t sctp_packet_bundle_sack(struct sctp_packet *pkt,
186 struct sctp_chunk *chunk)
187{
188 sctp_xmit_t retval = SCTP_XMIT_OK;
189
190 /* If sending DATA and haven't aleady bundled a SACK, try to
191 * bundle one in to the packet.
192 */
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;
197
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);
209 }
210 }
211 }
212 return retval;
213}
214
215/* Append a chunk to the offered packet reporting back any inability to do
216 * so.
217 */
218sctp_xmit_t sctp_packet_append_chunk(struct sctp_packet *packet,
219 struct sctp_chunk *chunk)
220{
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;
226
227 SCTP_DEBUG_PRINTK("%s: packet:%p chunk:%p\n", __FUNCTION__, packet,
228 chunk);
229
230 retval = sctp_packet_bundle_sack(packet, chunk);
231 psize = packet->size;
232
233 if (retval != SCTP_XMIT_OK)
234 goto finish;
235
236 pmtu = ((packet->transport->asoc) ?
237 (packet->transport->asoc->pmtu) :
238 (packet->transport->pmtu));
239
240 too_big = (psize + chunk_len > pmtu);
241
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.
246 */
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
251 */
252 packet->ipfragok = 1;
253 goto append;
254
255 } else {
256 retval = SCTP_XMIT_PMTU_FULL;
257 goto finish;
258 }
259 }
260
261append:
262 /* We believe that this chunk is OK to add to the packet (as
263 * long as we have the cwnd for it).
264 */
265
266 /* DATA is a special case since we must examine both rwnd and cwnd
267 * before we send DATA.
268 */
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;
279
280 /* It is OK to send this chunk. */
79af02c2 281 list_add_tail(&chunk->list, &packet->chunk_list);
1da177e4
LT
282 packet->size += chunk_len;
283 chunk->transport = packet->transport;
284finish:
285 return retval;
286}
287
288/* All packets are sent to the network through this function from
289 * sctp_outq_tail().
290 *
291 * The return value is a normal kernel error return value.
292 */
293int sctp_packet_transmit(struct sctp_packet *packet)
294{
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;
79af02c2 300 struct sctp_chunk *chunk, *tmp;
1da177e4
LT
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;
306
307 SCTP_DEBUG_PRINTK("%s: packet:%p\n", __FUNCTION__, packet);
308
309 /* Do NOT generate a chunkless packet. */
79af02c2 310 if (list_empty(&packet->chunk_list))
1da177e4
LT
311 return err;
312
313 /* Set up convenience variables... */
79af02c2 314 chunk = list_entry(packet->chunk_list.next, struct sctp_chunk, list);
1da177e4
LT
315 sk = chunk->skb->sk;
316
317 /* Allocate the new skb. */
594ccc14 318 nskb = alloc_skb(packet->size + LL_MAX_HEADER, GFP_ATOMIC);
1da177e4
LT
319 if (!nskb)
320 goto nomem;
321
322 /* Make sure the outbound skb has enough header room reserved. */
594ccc14 323 skb_reserve(nskb, packet->overhead + LL_MAX_HEADER);
1da177e4
LT
324
325 /* Set the owning socket so that we know where to get the
326 * destination IP address.
327 */
328 skb_set_owner_w(nskb, sk);
329
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);
334
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:
339 *
340 * 1) Fill in the proper Verification Tag in the SCTP common
341 * header and initialize the checksum field to 0's.
342 */
343 sh->vtag = htonl(packet->vtag);
344 sh->checksum = 0;
345
346 /* 2) Calculate the Adler-32 checksum of the whole packet,
347 * including the SCTP common header and all the
348 * chunks.
349 *
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>.
352 */
353 crc32 = sctp_start_cksum((__u8 *)sh, sizeof(struct sctphdr));
354
355 /**
356 * 6.10 Bundling
357 *
358 * An endpoint bundles chunks by simply including multiple
359 * chunks in one outbound SCTP packet. ...
360 */
361
362 /**
363 * 3.2 Chunk Field Descriptions
364 *
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.
371 *
372 * [This whole comment explains WORD_ROUND() below.]
373 */
374 SCTP_DEBUG_PRINTK("***sctp_transmit_packet***\n");
79af02c2
DM
375 list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
376 list_del_init(&chunk->list);
1da177e4
LT
377 if (sctp_chunk_is_data(chunk)) {
378
379 if (!chunk->has_tsn) {
380 sctp_chunk_assign_ssn(chunk);
381 sctp_chunk_assign_tsn(chunk);
382
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.
388 */
389
390 if (!tp->rto_pending) {
391 chunk->rtt_in_progress = 1;
392 tp->rto_pending = 1;
393 }
394 } else
395 chunk->resent = 1;
396
397 chunk->sent_at = jiffies;
398 has_data = 1;
399 }
400
401 padding = WORD_ROUND(chunk->skb->len) - chunk->skb->len;
402 if (padding)
403 memset(skb_put(chunk->skb, padding), 0, padding);
404
405 crc32 = sctp_update_copy_cksum(skb_put(nskb, chunk->skb->len),
406 chunk->skb->data,
407 chunk->skb->len, crc32);
408
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);
419
420 /*
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.
424 */
425 if (!sctp_chunk_is_data(chunk))
426 sctp_chunk_free(chunk);
427 }
428
429 /* Perform final transformation on checksum. */
430 crc32 = sctp_end_cksum(crc32);
431
432 /* 3) Put the resultant value into the checksum field in the
433 * common header, and leave the rest of the bits unchanged.
434 */
435 sh->checksum = htonl(crc32);
436
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."
442 *
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.
445 *
446 * Note: The works for IPv6 layer checks this bit too later
447 * in transmission. See IP6_ECN_flow_xmit().
448 */
449 INET_ECN_xmit(nskb->sk);
450
451 /* Set up the IP options. */
452 /* BUG: not implemented
453 * For v4 this all lives somewhere in sk->sk_opt...
454 */
455
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
460 */
461 asoc->peer.last_sent_to = tp;
462 }
463
464 if (has_data) {
465 struct timer_list *timer;
466 unsigned long timeout;
467
468 tp->last_time_used = jiffies;
469
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];
474
475 if (!mod_timer(timer, jiffies + timeout))
476 sctp_association_hold(asoc);
477 }
478 }
479
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 sctp_assoc_sync_pmtu(asoc);
486 }
487
488 nskb->dst = dst_clone(tp->dst);
489 if (!nskb->dst)
490 goto no_route;
491
492 SCTP_DEBUG_PRINTK("***sctp_transmit_packet*** skb len %d\n",
493 nskb->len);
494
495 (*tp->af_specific->sctp_xmit)(nskb, tp, packet->ipfragok);
496
497out:
498 packet->size = packet->overhead;
499 return err;
500no_route:
501 kfree_skb(nskb);
502 IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
503
504 /* FIXME: Returning the 'err' will effect all the associations
505 * associated with a socket, although only one of the paths of the
506 * association is unreachable.
507 * The real failure of a transport or association can be passed on
508 * to the user via notifications. So setting this error may not be
509 * required.
510 */
511 /* err = -EHOSTUNREACH; */
512err:
513 /* Control chunks are unreliable so just drop them. DATA chunks
514 * will get resent or dropped later.
515 */
516
79af02c2
DM
517 list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
518 list_del_init(&chunk->list);
1da177e4
LT
519 if (!sctp_chunk_is_data(chunk))
520 sctp_chunk_free(chunk);
521 }
522 goto out;
523nomem:
524 err = -ENOMEM;
525 goto err;
526}
527
528/********************************************************************
529 * 2nd Level Abstractions
530 ********************************************************************/
531
532/* This private function handles the specifics of appending DATA chunks. */
533static sctp_xmit_t sctp_packet_append_data(struct sctp_packet *packet,
534 struct sctp_chunk *chunk)
535{
536 sctp_xmit_t retval = SCTP_XMIT_OK;
537 size_t datasize, rwnd, inflight;
538 struct sctp_transport *transport = packet->transport;
539 __u32 max_burst_bytes;
540 struct sctp_association *asoc = transport->asoc;
541 struct sctp_sock *sp = sctp_sk(asoc->base.sk);
542 struct sctp_outq *q = &asoc->outqueue;
543
544 /* RFC 2960 6.1 Transmission of DATA Chunks
545 *
546 * A) At any given time, the data sender MUST NOT transmit new data to
547 * any destination transport address if its peer's rwnd indicates
548 * that the peer has no buffer space (i.e. rwnd is 0, see Section
549 * 6.2.1). However, regardless of the value of rwnd (including if it
550 * is 0), the data sender can always have one DATA chunk in flight to
551 * the receiver if allowed by cwnd (see rule B below). This rule
552 * allows the sender to probe for a change in rwnd that the sender
553 * missed due to the SACK having been lost in transit from the data
554 * receiver to the data sender.
555 */
556
557 rwnd = asoc->peer.rwnd;
558 inflight = asoc->outqueue.outstanding_bytes;
559
560 datasize = sctp_data_size(chunk);
561
562 if (datasize > rwnd) {
563 if (inflight > 0) {
564 /* We have (at least) one data chunk in flight,
565 * so we can't fall back to rule 6.1 B).
566 */
567 retval = SCTP_XMIT_RWND_FULL;
568 goto finish;
569 }
570 }
571
572 /* sctpimpguide-05 2.14.2
573 * D) When the time comes for the sender to
574 * transmit new DATA chunks, the protocol parameter Max.Burst MUST
575 * first be applied to limit how many new DATA chunks may be sent.
576 * The limit is applied by adjusting cwnd as follows:
577 * if ((flightsize + Max.Burst * MTU) < cwnd)
578 * cwnd = flightsize + Max.Burst * MTU
579 */
580 max_burst_bytes = asoc->max_burst * asoc->pmtu;
581 if ((transport->flight_size + max_burst_bytes) < transport->cwnd) {
582 transport->cwnd = transport->flight_size + max_burst_bytes;
583 SCTP_DEBUG_PRINTK("%s: cwnd limited by max_burst: "
584 "transport: %p, cwnd: %d, "
585 "ssthresh: %d, flight_size: %d, "
586 "pba: %d\n",
587 __FUNCTION__, transport,
588 transport->cwnd,
589 transport->ssthresh,
590 transport->flight_size,
591 transport->partial_bytes_acked);
592 }
593
594 /* RFC 2960 6.1 Transmission of DATA Chunks
595 *
596 * B) At any given time, the sender MUST NOT transmit new data
597 * to a given transport address if it has cwnd or more bytes
598 * of data outstanding to that transport address.
599 */
600 /* RFC 7.2.4 & the Implementers Guide 2.8.
601 *
602 * 3) ...
603 * When a Fast Retransmit is being performed the sender SHOULD
604 * ignore the value of cwnd and SHOULD NOT delay retransmission.
605 */
606 if (!chunk->fast_retransmit)
607 if (transport->flight_size >= transport->cwnd) {
608 retval = SCTP_XMIT_RWND_FULL;
609 goto finish;
610 }
611
612 /* Nagle's algorithm to solve small-packet problem:
613 * Inhibit the sending of new chunks when new outgoing data arrives
614 * if any previously transmitted data on the connection remains
615 * unacknowledged.
616 */
617 if (!sp->nodelay && sctp_packet_empty(packet) &&
618 q->outstanding_bytes && sctp_state(asoc, ESTABLISHED)) {
619 unsigned len = datasize + q->out_qlen;
620
621 /* Check whether this chunk and all the rest of pending
622 * data will fit or delay in hopes of bundling a full
623 * sized packet.
624 */
625 if (len < asoc->pmtu - packet->overhead) {
626 retval = SCTP_XMIT_NAGLE_DELAY;
627 goto finish;
628 }
629 }
630
631 /* Keep track of how many bytes are in flight over this transport. */
632 transport->flight_size += datasize;
633
634 /* Keep track of how many bytes are in flight to the receiver. */
635 asoc->outqueue.outstanding_bytes += datasize;
636
637 /* Update our view of the receiver's rwnd. */
638 if (datasize < rwnd)
639 rwnd -= datasize;
640 else
641 rwnd = 0;
642
643 asoc->peer.rwnd = rwnd;
644 /* Has been accepted for transmission. */
645 if (!asoc->peer.prsctp_capable)
646 chunk->msg->can_abandon = 0;
647
648finish:
649 return retval;
650}