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. */
65 static 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 */
sctp_packet_config(struct sctp_packet * packet,__u32 vtag,int ecn_capable)71 struct 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. */
sctp_packet_init(struct sctp_packet * packet,struct sctp_transport * transport,__u16 sport,__u16 dport)98 struct 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;
111 skb_queue_head_init(&packet->chunks);
112 if (asoc) {
113 struct sctp_opt *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. */
sctp_packet_free(struct sctp_packet * packet)130 void sctp_packet_free(struct sctp_packet *packet)
131 {
132 struct sctp_chunk *chunk;
133
134 SCTP_DEBUG_PRINTK("%s: packet:%p\n", __FUNCTION__, packet);
135
136 while ((chunk = (struct sctp_chunk *)__skb_dequeue(&packet->chunks)) != NULL)
137 sctp_chunk_free(chunk);
138
139 if (packet->malloced)
140 kfree(packet);
141 }
142
143 /* This routine tries to append the chunk to the offered packet. If adding
144 * the chunk causes the packet to exceed the path MTU and COOKIE_ECHO chunk
145 * is not present in the packet, it transmits the input packet.
146 * Data can be bundled with a packet containing a COOKIE_ECHO chunk as long
147 * as it can fit in the packet, but any more data that does not fit in this
148 * packet can be sent only after receiving the COOKIE_ACK.
149 */
sctp_packet_transmit_chunk(struct sctp_packet * packet,struct sctp_chunk * chunk)150 sctp_xmit_t sctp_packet_transmit_chunk(struct sctp_packet *packet,
151 struct sctp_chunk *chunk)
152 {
153 sctp_xmit_t retval;
154 int error = 0;
155
156 SCTP_DEBUG_PRINTK("%s: packet:%p chunk:%p\n", __FUNCTION__,
157 packet, chunk);
158
159 switch ((retval = (sctp_packet_append_chunk(packet, chunk)))) {
160 case SCTP_XMIT_PMTU_FULL:
161 if (!packet->has_cookie_echo) {
162 error = sctp_packet_transmit(packet);
163 if (error < 0)
164 chunk->skb->sk->err = -error;
165
166 /* If we have an empty packet, then we can NOT ever
167 * return PMTU_FULL.
168 */
169 retval = sctp_packet_append_chunk(packet, chunk);
170 }
171 break;
172
173 case SCTP_XMIT_RWND_FULL:
174 case SCTP_XMIT_OK:
175 case SCTP_XMIT_NAGLE_DELAY:
176 break;
177 };
178
179 return retval;
180 }
181
182 /* Try to bundle a SACK with the packet. */
sctp_packet_bundle_sack(struct sctp_packet * pkt,struct sctp_chunk * chunk)183 static sctp_xmit_t sctp_packet_bundle_sack(struct sctp_packet *pkt,
184 struct sctp_chunk *chunk)
185 {
186 sctp_xmit_t retval = SCTP_XMIT_OK;
187
188 /* If sending DATA and haven't aleady bundled a SACK, try to
189 * bundle one in to the packet.
190 */
191 if (sctp_chunk_is_data(chunk) && !pkt->has_sack &&
192 !pkt->has_cookie_echo) {
193 struct sctp_association *asoc;
194 asoc = pkt->transport->asoc;
195
196 if (asoc->a_rwnd > asoc->rwnd) {
197 struct sctp_chunk *sack;
198 asoc->a_rwnd = asoc->rwnd;
199 sack = sctp_make_sack(asoc);
200 if (sack) {
201 struct timer_list *timer;
202 retval = sctp_packet_append_chunk(pkt, sack);
203 asoc->peer.sack_needed = 0;
204 timer = &asoc->timers[SCTP_EVENT_TIMEOUT_SACK];
205 if (timer_pending(timer) && del_timer(timer))
206 sctp_association_put(asoc);
207 }
208 }
209 }
210 return retval;
211 }
212
213 /* Append a chunk to the offered packet reporting back any inability to do
214 * so.
215 */
sctp_packet_append_chunk(struct sctp_packet * packet,struct sctp_chunk * chunk)216 sctp_xmit_t sctp_packet_append_chunk(struct sctp_packet *packet,
217 struct sctp_chunk *chunk)
218 {
219 sctp_xmit_t retval = SCTP_XMIT_OK;
220 __u16 chunk_len = WORD_ROUND(ntohs(chunk->chunk_hdr->length));
221 size_t psize;
222 size_t pmtu;
223 int too_big;
224
225 SCTP_DEBUG_PRINTK("%s: packet:%p chunk:%p\n", __FUNCTION__, packet,
226 chunk);
227
228 retval = sctp_packet_bundle_sack(packet, chunk);
229 psize = packet->size;
230
231 if (retval != SCTP_XMIT_OK)
232 goto finish;
233
234 pmtu = ((packet->transport->asoc) ?
235 (packet->transport->asoc->pmtu) :
236 (packet->transport->pmtu));
237
238 too_big = (psize + chunk_len > pmtu);
239
240 /* Decide if we need to fragment or resubmit later. */
241 if (too_big) {
242 /* Both control chunks and data chunks with TSNs are
243 * non-fragmentable.
244 */
245 if (sctp_packet_empty(packet) || !sctp_chunk_is_data(chunk)) {
246 /* We no longer do re-fragmentation.
247 * Just fragment at the IP layer, if we
248 * actually hit this condition
249 */
250 packet->ipfragok = 1;
251 goto append;
252
253 } else {
254 retval = SCTP_XMIT_PMTU_FULL;
255 goto finish;
256 }
257 }
258
259 append:
260 /* We believe that this chunk is OK to add to the packet (as
261 * long as we have the cwnd for it).
262 */
263
264 /* DATA is a special case since we must examine both rwnd and cwnd
265 * before we send DATA.
266 */
267 if (sctp_chunk_is_data(chunk)) {
268 retval = sctp_packet_append_data(packet, chunk);
269 /* Disallow SACK bundling after DATA. */
270 packet->has_sack = 1;
271 if (SCTP_XMIT_OK != retval)
272 goto finish;
273 } else if (SCTP_CID_COOKIE_ECHO == chunk->chunk_hdr->type)
274 packet->has_cookie_echo = 1;
275 else if (SCTP_CID_SACK == chunk->chunk_hdr->type)
276 packet->has_sack = 1;
277
278 /* It is OK to send this chunk. */
279 __skb_queue_tail(&packet->chunks, (struct sk_buff *)chunk);
280 packet->size += chunk_len;
281 chunk->transport = packet->transport;
282 finish:
283 return retval;
284 }
285
286 /* All packets are sent to the network through this function from
287 * sctp_outq_tail().
288 *
289 * The return value is a normal kernel error return value.
290 */
sctp_packet_transmit(struct sctp_packet * packet)291 int sctp_packet_transmit(struct sctp_packet *packet)
292 {
293 struct sctp_transport *tp = packet->transport;
294 struct sctp_association *asoc = tp->asoc;
295 struct sctphdr *sh;
296 __u32 crc32;
297 struct sk_buff *nskb;
298 struct sctp_chunk *chunk;
299 struct sock *sk;
300 int err = 0;
301 int padding; /* How much padding do we need? */
302 __u8 has_data = 0;
303 struct dst_entry *dst;
304
305 SCTP_DEBUG_PRINTK("%s: packet:%p\n", __FUNCTION__, packet);
306
307 /* Do NOT generate a chunkless packet. */
308 chunk = (struct sctp_chunk *)skb_peek(&packet->chunks);
309 if (unlikely(!chunk))
310 return err;
311
312 /* Set up convenience variables... */
313 sk = chunk->skb->sk;
314
315 /* Allocate the new skb. */
316 nskb = dev_alloc_skb(packet->size);
317 if (!nskb)
318 goto nomem;
319
320 /* Make sure the outbound skb has enough header room reserved. */
321 skb_reserve(nskb, packet->overhead);
322
323 /* Set the owning socket so that we know where to get the
324 * destination IP address.
325 */
326 skb_set_owner_w(nskb, sk);
327
328 /* Build the SCTP header. */
329 sh = (struct sctphdr *)skb_push(nskb, sizeof(struct sctphdr));
330 sh->source = htons(packet->source_port);
331 sh->dest = htons(packet->destination_port);
332
333 /* From 6.8 Adler-32 Checksum Calculation:
334 * After the packet is constructed (containing the SCTP common
335 * header and one or more control or DATA chunks), the
336 * transmitter shall:
337 *
338 * 1) Fill in the proper Verification Tag in the SCTP common
339 * header and initialize the checksum field to 0's.
340 */
341 sh->vtag = htonl(packet->vtag);
342 sh->checksum = 0;
343
344 /* 2) Calculate the Adler-32 checksum of the whole packet,
345 * including the SCTP common header and all the
346 * chunks.
347 *
348 * Note: Adler-32 is no longer applicable, as has been replaced
349 * by CRC32-C as described in <draft-ietf-tsvwg-sctpcsum-02.txt>.
350 */
351 crc32 = sctp_start_cksum((__u8 *)sh, sizeof(struct sctphdr));
352
353 /**
354 * 6.10 Bundling
355 *
356 * An endpoint bundles chunks by simply including multiple
357 * chunks in one outbound SCTP packet. ...
358 */
359
360 /**
361 * 3.2 Chunk Field Descriptions
362 *
363 * The total length of a chunk (including Type, Length and
364 * Value fields) MUST be a multiple of 4 bytes. If the length
365 * of the chunk is not a multiple of 4 bytes, the sender MUST
366 * pad the chunk with all zero bytes and this padding is not
367 * included in the chunk length field. The sender should
368 * never pad with more than 3 bytes.
369 *
370 * [This whole comment explains WORD_ROUND() below.]
371 */
372 SCTP_DEBUG_PRINTK("***sctp_transmit_packet***\n");
373 while ((chunk = (struct sctp_chunk *)__skb_dequeue(&packet->chunks)) != NULL) {
374 if (sctp_chunk_is_data(chunk)) {
375
376 if (!chunk->has_tsn) {
377 sctp_chunk_assign_ssn(chunk);
378 sctp_chunk_assign_tsn(chunk);
379
380 /* 6.3.1 C4) When data is in flight and when allowed
381 * by rule C5, a new RTT measurement MUST be made each
382 * round trip. Furthermore, new RTT measurements
383 * SHOULD be made no more than once per round-trip
384 * for a given destination transport address.
385 */
386
387 if (!tp->rto_pending) {
388 chunk->rtt_in_progress = 1;
389 tp->rto_pending = 1;
390 }
391 } else
392 chunk->resent = 1;
393
394 chunk->sent_at = jiffies;
395 has_data = 1;
396 }
397
398 padding = WORD_ROUND(chunk->skb->len) - chunk->skb->len;
399 if (padding)
400 memset(skb_put(chunk->skb, padding), 0, padding);
401
402 crc32 = sctp_update_copy_cksum(skb_put(nskb, chunk->skb->len),
403 chunk->skb->data,
404 chunk->skb->len, crc32);
405
406 SCTP_DEBUG_PRINTK("%s %p[%s] %s 0x%x, %s %d, %s %d, %s %d\n",
407 "*** Chunk", chunk,
408 sctp_cname(SCTP_ST_CHUNK(
409 chunk->chunk_hdr->type)),
410 chunk->has_tsn ? "TSN" : "No TSN",
411 chunk->has_tsn ?
412 ntohl(chunk->subh.data_hdr->tsn) : 0,
413 "length", ntohs(chunk->chunk_hdr->length),
414 "chunk->skb->len", chunk->skb->len,
415 "rtt_in_progress", chunk->rtt_in_progress);
416
417 /*
418 * If this is a control chunk, this is our last
419 * reference. Free data chunks after they've been
420 * acknowledged or have failed.
421 */
422 if (!sctp_chunk_is_data(chunk))
423 sctp_chunk_free(chunk);
424 }
425
426 /* Perform final transformation on checksum. */
427 crc32 = sctp_end_cksum(crc32);
428
429 /* 3) Put the resultant value into the checksum field in the
430 * common header, and leave the rest of the bits unchanged.
431 */
432 sh->checksum = htonl(crc32);
433
434 /* IP layer ECN support
435 * From RFC 2481
436 * "The ECN-Capable Transport (ECT) bit would be set by the
437 * data sender to indicate that the end-points of the
438 * transport protocol are ECN-capable."
439 *
440 * Now setting the ECT bit all the time, as it should not cause
441 * any problems protocol-wise even if our peer ignores it.
442 *
443 * Note: The works for IPv6 layer checks this bit too later
444 * in transmission. See IP6_ECN_flow_xmit().
445 */
446 INET_ECN_xmit(nskb->sk);
447
448 /* Set up the IP options. */
449 /* BUG: not implemented
450 * For v4 this all lives somewhere in sk->sk_opt...
451 */
452
453 /* Dump that on IP! */
454 if (asoc && asoc->peer.last_sent_to != tp) {
455 /* Considering the multiple CPU scenario, this is a
456 * "correcter" place for last_sent_to. --xguo
457 */
458 asoc->peer.last_sent_to = tp;
459 }
460
461 if (has_data) {
462 struct timer_list *timer;
463 unsigned long timeout;
464
465 tp->last_time_used = jiffies;
466
467 /* Restart the AUTOCLOSE timer when sending data. */
468 if (sctp_state(asoc, ESTABLISHED) && asoc->autoclose) {
469 timer = &asoc->timers[SCTP_EVENT_TIMEOUT_AUTOCLOSE];
470 timeout = asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE];
471
472 if (!mod_timer(timer, jiffies + timeout))
473 sctp_association_hold(asoc);
474 }
475 }
476
477 dst = tp->dst;
478 /* The 'obsolete' field of dst is set to 2 when a dst is freed. */
479 if (!dst || (dst->obsolete > 1)) {
480 dst_release(dst);
481 sctp_transport_route(tp, NULL, sctp_sk(sk));
482 sctp_assoc_sync_pmtu(asoc);
483 }
484
485 nskb->dst = dst_clone(tp->dst);
486 if (!nskb->dst)
487 goto no_route;
488
489 SCTP_DEBUG_PRINTK("***sctp_transmit_packet*** skb len %d\n",
490 nskb->len);
491
492 (*tp->af_specific->sctp_xmit)(nskb, tp, packet->ipfragok);
493
494 out:
495 packet->size = packet->overhead;
496 return err;
497 no_route:
498 kfree_skb(nskb);
499 IP_INC_STATS_BH(IpOutNoRoutes);
500
501 /* FIXME: Returning the 'err' will effect all the associations
502 * associated with a socket, although only one of the paths of the
503 * association is unreachable.
504 * The real failure of a transport or association can be passed on
505 * to the user via notifications. So setting this error may not be
506 * required.
507 */
508 /* err = -EHOSTUNREACH; */
509 err:
510 /* Control chunks are unreliable so just drop them. DATA chunks
511 * will get resent or dropped later.
512 */
513
514 while ((chunk = (struct sctp_chunk *)__skb_dequeue(&packet->chunks)) != NULL) {
515 if (!sctp_chunk_is_data(chunk))
516 sctp_chunk_free(chunk);
517 }
518 goto out;
519 nomem:
520 err = -ENOMEM;
521 goto err;
522 }
523
524 /********************************************************************
525 * 2nd Level Abstractions
526 ********************************************************************/
527
528 /* This private function handles the specifics of appending DATA chunks. */
sctp_packet_append_data(struct sctp_packet * packet,struct sctp_chunk * chunk)529 static sctp_xmit_t sctp_packet_append_data(struct sctp_packet *packet,
530 struct sctp_chunk *chunk)
531 {
532 sctp_xmit_t retval = SCTP_XMIT_OK;
533 size_t datasize, rwnd, inflight;
534 struct sctp_transport *transport = packet->transport;
535 __u32 max_burst_bytes;
536 struct sctp_association *asoc = transport->asoc;
537 struct sctp_opt *sp = sctp_sk(asoc->base.sk);
538 struct sctp_outq *q = &asoc->outqueue;
539
540 /* RFC 2960 6.1 Transmission of DATA Chunks
541 *
542 * A) At any given time, the data sender MUST NOT transmit new data to
543 * any destination transport address if its peer's rwnd indicates
544 * that the peer has no buffer space (i.e. rwnd is 0, see Section
545 * 6.2.1). However, regardless of the value of rwnd (including if it
546 * is 0), the data sender can always have one DATA chunk in flight to
547 * the receiver if allowed by cwnd (see rule B below). This rule
548 * allows the sender to probe for a change in rwnd that the sender
549 * missed due to the SACK having been lost in transit from the data
550 * receiver to the data sender.
551 */
552
553 rwnd = asoc->peer.rwnd;
554 inflight = asoc->outqueue.outstanding_bytes;
555
556 datasize = sctp_data_size(chunk);
557
558 if (datasize > rwnd) {
559 if (inflight > 0) {
560 /* We have (at least) one data chunk in flight,
561 * so we can't fall back to rule 6.1 B).
562 */
563 retval = SCTP_XMIT_RWND_FULL;
564 goto finish;
565 }
566 }
567
568 /* sctpimpguide-05 2.14.2
569 * D) When the time comes for the sender to
570 * transmit new DATA chunks, the protocol parameter Max.Burst MUST
571 * first be applied to limit how many new DATA chunks may be sent.
572 * The limit is applied by adjusting cwnd as follows:
573 * if ((flightsize + Max.Burst * MTU) < cwnd)
574 * cwnd = flightsize + Max.Burst * MTU
575 */
576 max_burst_bytes = asoc->max_burst * asoc->pmtu;
577 if ((transport->flight_size + max_burst_bytes) < transport->cwnd) {
578 transport->cwnd = transport->flight_size + max_burst_bytes;
579 SCTP_DEBUG_PRINTK("%s: cwnd limited by max_burst: "
580 "transport: %p, cwnd: %d, "
581 "ssthresh: %d, flight_size: %d, "
582 "pba: %d\n",
583 __FUNCTION__, transport,
584 transport->cwnd,
585 transport->ssthresh,
586 transport->flight_size,
587 transport->partial_bytes_acked);
588 }
589
590 /* RFC 2960 6.1 Transmission of DATA Chunks
591 *
592 * B) At any given time, the sender MUST NOT transmit new data
593 * to a given transport address if it has cwnd or more bytes
594 * of data outstanding to that transport address.
595 */
596 /* RFC 7.2.4 & the Implementers Guide 2.8.
597 *
598 * 3) ...
599 * When a Fast Retransmit is being performed the sender SHOULD
600 * ignore the value of cwnd and SHOULD NOT delay retransmission.
601 */
602 if (!chunk->fast_retransmit)
603 if (transport->flight_size >= transport->cwnd) {
604 retval = SCTP_XMIT_RWND_FULL;
605 goto finish;
606 }
607
608 /* Nagle's algorithm to solve small-packet problem:
609 * Inhibit the sending of new chunks when new outgoing data arrives
610 * if any previously transmitted data on the connection remains
611 * unacknowledged.
612 */
613 if (!sp->nodelay && sctp_packet_empty(packet) &&
614 q->outstanding_bytes && sctp_state(asoc, ESTABLISHED)) {
615 unsigned len = datasize + q->out_qlen;
616
617 /* Check whether this chunk and all the rest of pending
618 * data will fit or delay in hopes of bundling a full
619 * sized packet.
620 */
621 if (len < asoc->pmtu - packet->overhead) {
622 retval = SCTP_XMIT_NAGLE_DELAY;
623 goto finish;
624 }
625 }
626
627 /* Keep track of how many bytes are in flight over this transport. */
628 transport->flight_size += datasize;
629
630 /* Keep track of how many bytes are in flight to the receiver. */
631 asoc->outqueue.outstanding_bytes += datasize;
632
633 /* Update our view of the receiver's rwnd. */
634 if (datasize < rwnd)
635 rwnd -= datasize;
636 else
637 rwnd = 0;
638
639 asoc->peer.rwnd = rwnd;
640 /* Has been accepted for transmission. */
641 if (!asoc->peer.prsctp_capable)
642 chunk->msg->can_abandon = 0;
643
644 finish:
645 return retval;
646 }
647