blob: 94c110dcaf1d10151d0a69a11bdf3fb6c68e2cbc [file] [log] [blame]
Vlad Yasevich60c778b2008-01-11 09:57:09 -05001/* SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -05006 * This file is part of the SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * These functions handle output processing.
9 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050010 * This SCTP implementation is free software;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * 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 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050016 * This SCTP implementation is distributed in the hope that it
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * 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>
Vlad Yasevich8d2f9e82009-03-21 13:41:09 -070052#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <net/icmp.h>
Pavel Emelyanov7c73a6f2008-07-16 20:20:11 -070054#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <linux/socket.h> /* for sa_family_t */
57#include <net/sock.h>
58
59#include <net/sctp/sctp.h>
60#include <net/sctp/sm.h>
Vlad Yasevich9ad09772007-12-16 14:06:41 -080061#include <net/sctp/checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63/* Forward declarations for private helpers. */
64static sctp_xmit_t sctp_packet_append_data(struct sctp_packet *packet,
65 struct sctp_chunk *chunk);
66
67/* Config a packet.
68 * This appears to be a followup set of initializations.
69 */
70struct sctp_packet *sctp_packet_config(struct sctp_packet *packet,
71 __u32 vtag, int ecn_capable)
72{
73 struct sctp_chunk *chunk = NULL;
74
Harvey Harrison0dc47872008-03-05 20:47:47 -080075 SCTP_DEBUG_PRINTK("%s: packet:%p vtag:0x%x\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 packet, vtag);
77
78 packet->vtag = vtag;
79 packet->has_cookie_echo = 0;
80 packet->has_sack = 0;
Vlad Yasevicha29a5bd2007-09-16 19:31:35 -070081 packet->has_auth = 0;
Vlad Yasevich4cd57c82007-09-16 19:32:45 -070082 packet->has_data = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 packet->ipfragok = 0;
Vlad Yasevicha29a5bd2007-09-16 19:31:35 -070084 packet->auth = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 if (ecn_capable && sctp_packet_empty(packet)) {
87 chunk = sctp_get_ecne_prepend(packet->transport->asoc);
88
89 /* If there a is a prepend chunk stick it on the list before
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +090090 * any other chunks get appended.
91 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 if (chunk)
93 sctp_packet_append_chunk(packet, chunk);
94 }
95
96 return packet;
97}
98
99/* Initialize the packet structure. */
100struct sctp_packet *sctp_packet_init(struct sctp_packet *packet,
101 struct sctp_transport *transport,
102 __u16 sport, __u16 dport)
103{
104 struct sctp_association *asoc = transport->asoc;
105 size_t overhead;
106
Harvey Harrison0dc47872008-03-05 20:47:47 -0800107 SCTP_DEBUG_PRINTK("%s: packet:%p transport:%p\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 packet, transport);
109
110 packet->transport = transport;
111 packet->source_port = sport;
112 packet->destination_port = dport;
David S. Miller79af02c2005-07-08 21:47:49 -0700113 INIT_LIST_HEAD(&packet->chunk_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 if (asoc) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900115 struct sctp_sock *sp = sctp_sk(asoc->base.sk);
116 overhead = sp->pf->af->net_header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 } else {
118 overhead = sizeof(struct ipv6hdr);
119 }
120 overhead += sizeof(struct sctphdr);
121 packet->overhead = overhead;
122 packet->size = overhead;
123 packet->vtag = 0;
124 packet->has_cookie_echo = 0;
125 packet->has_sack = 0;
Vlad Yasevicha29a5bd2007-09-16 19:31:35 -0700126 packet->has_auth = 0;
Vlad Yasevich4cd57c82007-09-16 19:32:45 -0700127 packet->has_data = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 packet->ipfragok = 0;
129 packet->malloced = 0;
Vlad Yasevicha29a5bd2007-09-16 19:31:35 -0700130 packet->auth = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 return packet;
132}
133
134/* Free a packet. */
135void sctp_packet_free(struct sctp_packet *packet)
136{
David S. Miller79af02c2005-07-08 21:47:49 -0700137 struct sctp_chunk *chunk, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Harvey Harrison0dc47872008-03-05 20:47:47 -0800139 SCTP_DEBUG_PRINTK("%s: packet:%p\n", __func__, packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
David S. Miller79af02c2005-07-08 21:47:49 -0700141 list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
142 list_del_init(&chunk->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 sctp_chunk_free(chunk);
David S. Miller79af02c2005-07-08 21:47:49 -0700144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 if (packet->malloced)
147 kfree(packet);
148}
149
150/* This routine tries to append the chunk to the offered packet. If adding
151 * the chunk causes the packet to exceed the path MTU and COOKIE_ECHO chunk
152 * is not present in the packet, it transmits the input packet.
153 * Data can be bundled with a packet containing a COOKIE_ECHO chunk as long
154 * as it can fit in the packet, but any more data that does not fit in this
155 * packet can be sent only after receiving the COOKIE_ACK.
156 */
157sctp_xmit_t sctp_packet_transmit_chunk(struct sctp_packet *packet,
Vlad Yasevich2e3216c2008-06-19 16:08:18 -0700158 struct sctp_chunk *chunk,
159 int one_packet)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 sctp_xmit_t retval;
162 int error = 0;
163
Harvey Harrison0dc47872008-03-05 20:47:47 -0800164 SCTP_DEBUG_PRINTK("%s: packet:%p chunk:%p\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 packet, chunk);
166
167 switch ((retval = (sctp_packet_append_chunk(packet, chunk)))) {
168 case SCTP_XMIT_PMTU_FULL:
169 if (!packet->has_cookie_echo) {
170 error = sctp_packet_transmit(packet);
171 if (error < 0)
172 chunk->skb->sk->sk_err = -error;
173
174 /* If we have an empty packet, then we can NOT ever
175 * return PMTU_FULL.
176 */
Vlad Yasevich2e3216c2008-06-19 16:08:18 -0700177 if (!one_packet)
178 retval = sctp_packet_append_chunk(packet,
179 chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
181 break;
182
183 case SCTP_XMIT_RWND_FULL:
184 case SCTP_XMIT_OK:
185 case SCTP_XMIT_NAGLE_DELAY:
186 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 return retval;
190}
191
Vlad Yasevich4cd57c82007-09-16 19:32:45 -0700192/* Try to bundle an auth chunk into the packet. */
193static sctp_xmit_t sctp_packet_bundle_auth(struct sctp_packet *pkt,
194 struct sctp_chunk *chunk)
195{
196 struct sctp_association *asoc = pkt->transport->asoc;
197 struct sctp_chunk *auth;
198 sctp_xmit_t retval = SCTP_XMIT_OK;
199
200 /* if we don't have an association, we can't do authentication */
201 if (!asoc)
202 return retval;
203
204 /* See if this is an auth chunk we are bundling or if
205 * auth is already bundled.
206 */
207 if (chunk->chunk_hdr->type == SCTP_CID_AUTH || pkt->auth)
208 return retval;
209
210 /* if the peer did not request this chunk to be authenticated,
211 * don't do it
212 */
213 if (!chunk->auth)
214 return retval;
215
216 auth = sctp_make_auth(asoc);
217 if (!auth)
218 return retval;
219
220 retval = sctp_packet_append_chunk(pkt, auth);
221
222 return retval;
223}
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225/* Try to bundle a SACK with the packet. */
226static sctp_xmit_t sctp_packet_bundle_sack(struct sctp_packet *pkt,
227 struct sctp_chunk *chunk)
228{
229 sctp_xmit_t retval = SCTP_XMIT_OK;
230
231 /* If sending DATA and haven't aleady bundled a SACK, try to
232 * bundle one in to the packet.
233 */
234 if (sctp_chunk_is_data(chunk) && !pkt->has_sack &&
235 !pkt->has_cookie_echo) {
236 struct sctp_association *asoc;
Doug Grahamaf87b822009-07-29 12:05:57 -0400237 struct timer_list *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 asoc = pkt->transport->asoc;
Doug Grahamaf87b822009-07-29 12:05:57 -0400239 timer = &asoc->timers[SCTP_EVENT_TIMEOUT_SACK];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Doug Grahamaf87b822009-07-29 12:05:57 -0400241 /* If the SACK timer is running, we have a pending SACK */
242 if (timer_pending(timer)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 struct sctp_chunk *sack;
244 asoc->a_rwnd = asoc->rwnd;
245 sack = sctp_make_sack(asoc);
246 if (sack) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 retval = sctp_packet_append_chunk(pkt, sack);
248 asoc->peer.sack_needed = 0;
Doug Grahamaf87b822009-07-29 12:05:57 -0400249 if (del_timer(timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 sctp_association_put(asoc);
251 }
252 }
253 }
254 return retval;
255}
256
257/* Append a chunk to the offered packet reporting back any inability to do
258 * so.
259 */
260sctp_xmit_t sctp_packet_append_chunk(struct sctp_packet *packet,
261 struct sctp_chunk *chunk)
262{
263 sctp_xmit_t retval = SCTP_XMIT_OK;
264 __u16 chunk_len = WORD_ROUND(ntohs(chunk->chunk_hdr->length));
265 size_t psize;
266 size_t pmtu;
267 int too_big;
268
Harvey Harrison0dc47872008-03-05 20:47:47 -0800269 SCTP_DEBUG_PRINTK("%s: packet:%p chunk:%p\n", __func__, packet,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 chunk);
271
Vlad Yasevich4cd57c82007-09-16 19:32:45 -0700272 /* Try to bundle AUTH chunk */
273 retval = sctp_packet_bundle_auth(packet, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 if (retval != SCTP_XMIT_OK)
275 goto finish;
276
Vlad Yasevich4cd57c82007-09-16 19:32:45 -0700277 /* Try to bundle SACK chunk */
278 retval = sctp_packet_bundle_sack(packet, chunk);
279 if (retval != SCTP_XMIT_OK)
280 goto finish;
281
282 psize = packet->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 pmtu = ((packet->transport->asoc) ?
Frank Filz52ccb8e2005-12-22 11:36:46 -0800284 (packet->transport->asoc->pathmtu) :
285 (packet->transport->pathmtu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 too_big = (psize + chunk_len > pmtu);
288
289 /* Decide if we need to fragment or resubmit later. */
290 if (too_big) {
Vlad Yasevich4cd57c82007-09-16 19:32:45 -0700291 /* It's OK to fragmet at IP level if any one of the following
292 * is true:
293 * 1. The packet is empty (meaning this chunk is greater
294 * the MTU)
295 * 2. The chunk we are adding is a control chunk
296 * 3. The packet doesn't have any data in it yet and data
297 * requires authentication.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 */
Vlad Yasevich4cd57c82007-09-16 19:32:45 -0700299 if (sctp_packet_empty(packet) || !sctp_chunk_is_data(chunk) ||
300 (!packet->has_data && chunk->auth)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 /* We no longer do re-fragmentation.
302 * Just fragment at the IP layer, if we
303 * actually hit this condition
304 */
305 packet->ipfragok = 1;
306 goto append;
307
308 } else {
309 retval = SCTP_XMIT_PMTU_FULL;
310 goto finish;
311 }
312 }
313
314append:
315 /* We believe that this chunk is OK to add to the packet (as
316 * long as we have the cwnd for it).
317 */
318
319 /* DATA is a special case since we must examine both rwnd and cwnd
320 * before we send DATA.
321 */
Vlad Yasevich4cd57c82007-09-16 19:32:45 -0700322 switch (chunk->chunk_hdr->type) {
323 case SCTP_CID_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 retval = sctp_packet_append_data(packet, chunk);
Vlad Yasevich759af002009-01-22 14:53:01 -0800325 if (SCTP_XMIT_OK != retval)
326 goto finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 /* Disallow SACK bundling after DATA. */
328 packet->has_sack = 1;
Vlad Yasevich4cd57c82007-09-16 19:32:45 -0700329 /* Disallow AUTH bundling after DATA */
330 packet->has_auth = 1;
331 /* Let it be knows that packet has DATA in it */
332 packet->has_data = 1;
Vlad Yasevich759af002009-01-22 14:53:01 -0800333 /* timestamp the chunk for rtx purposes */
334 chunk->sent_at = jiffies;
Vlad Yasevich4cd57c82007-09-16 19:32:45 -0700335 break;
336 case SCTP_CID_COOKIE_ECHO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 packet->has_cookie_echo = 1;
Vlad Yasevich4cd57c82007-09-16 19:32:45 -0700338 break;
339
340 case SCTP_CID_SACK:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 packet->has_sack = 1;
Vlad Yasevich4cd57c82007-09-16 19:32:45 -0700342 break;
343
344 case SCTP_CID_AUTH:
345 packet->has_auth = 1;
346 packet->auth = chunk;
347 break;
348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 /* It is OK to send this chunk. */
David S. Miller79af02c2005-07-08 21:47:49 -0700351 list_add_tail(&chunk->list, &packet->chunk_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 packet->size += chunk_len;
353 chunk->transport = packet->transport;
354finish:
355 return retval;
356}
357
358/* All packets are sent to the network through this function from
359 * sctp_outq_tail().
360 *
361 * The return value is a normal kernel error return value.
362 */
363int sctp_packet_transmit(struct sctp_packet *packet)
364{
365 struct sctp_transport *tp = packet->transport;
366 struct sctp_association *asoc = tp->asoc;
367 struct sctphdr *sh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 struct sk_buff *nskb;
David S. Miller79af02c2005-07-08 21:47:49 -0700369 struct sctp_chunk *chunk, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 struct sock *sk;
371 int err = 0;
372 int padding; /* How much padding do we need? */
373 __u8 has_data = 0;
Sridhar Samudrala503b55f2006-06-17 22:57:28 -0700374 struct dst_entry *dst = tp->dst;
Vlad Yasevich4cd57c82007-09-16 19:32:45 -0700375 unsigned char *auth = NULL; /* pointer to auth in skb data */
376 __u32 cksum_buf_len = sizeof(struct sctphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Harvey Harrison0dc47872008-03-05 20:47:47 -0800378 SCTP_DEBUG_PRINTK("%s: packet:%p\n", __func__, packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 /* Do NOT generate a chunkless packet. */
David S. Miller79af02c2005-07-08 21:47:49 -0700381 if (list_empty(&packet->chunk_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return err;
383
384 /* Set up convenience variables... */
David S. Miller79af02c2005-07-08 21:47:49 -0700385 chunk = list_entry(packet->chunk_list.next, struct sctp_chunk, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 sk = chunk->skb->sk;
387
388 /* Allocate the new skb. */
Sridhar Samudrala594ccc12005-04-28 12:00:23 -0700389 nskb = alloc_skb(packet->size + LL_MAX_HEADER, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (!nskb)
391 goto nomem;
392
393 /* Make sure the outbound skb has enough header room reserved. */
Sridhar Samudrala594ccc12005-04-28 12:00:23 -0700394 skb_reserve(nskb, packet->overhead + LL_MAX_HEADER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 /* Set the owning socket so that we know where to get the
397 * destination IP address.
398 */
399 skb_set_owner_w(nskb, sk);
400
Sridhar Samudrala503b55f2006-06-17 22:57:28 -0700401 /* The 'obsolete' field of dst is set to 2 when a dst is freed. */
402 if (!dst || (dst->obsolete > 1)) {
403 dst_release(dst);
404 sctp_transport_route(tp, NULL, sctp_sk(sk));
405 if (asoc && (asoc->param_flags & SPP_PMTUD_ENABLE)) {
406 sctp_assoc_sync_pmtu(asoc);
407 }
408 }
Eric Dumazetadf30902009-06-02 05:19:30 +0000409 dst = dst_clone(tp->dst);
410 skb_dst_set(nskb, dst);
Wei Yongjunff0ac742009-06-28 22:49:37 +0000411 if (!dst)
Sridhar Samudrala503b55f2006-06-17 22:57:28 -0700412 goto no_route;
Sridhar Samudrala503b55f2006-06-17 22:57:28 -0700413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 /* Build the SCTP header. */
415 sh = (struct sctphdr *)skb_push(nskb, sizeof(struct sctphdr));
Jesse Brandeburg8dc92f72009-04-27 22:35:52 +0000416 skb_reset_transport_header(nskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 sh->source = htons(packet->source_port);
418 sh->dest = htons(packet->destination_port);
419
420 /* From 6.8 Adler-32 Checksum Calculation:
421 * After the packet is constructed (containing the SCTP common
422 * header and one or more control or DATA chunks), the
423 * transmitter shall:
424 *
425 * 1) Fill in the proper Verification Tag in the SCTP common
426 * header and initialize the checksum field to 0's.
427 */
428 sh->vtag = htonl(packet->vtag);
429 sh->checksum = 0;
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 /**
432 * 6.10 Bundling
433 *
434 * An endpoint bundles chunks by simply including multiple
435 * chunks in one outbound SCTP packet. ...
436 */
437
438 /**
439 * 3.2 Chunk Field Descriptions
440 *
441 * The total length of a chunk (including Type, Length and
442 * Value fields) MUST be a multiple of 4 bytes. If the length
443 * of the chunk is not a multiple of 4 bytes, the sender MUST
444 * pad the chunk with all zero bytes and this padding is not
445 * included in the chunk length field. The sender should
446 * never pad with more than 3 bytes.
447 *
448 * [This whole comment explains WORD_ROUND() below.]
449 */
450 SCTP_DEBUG_PRINTK("***sctp_transmit_packet***\n");
David S. Miller79af02c2005-07-08 21:47:49 -0700451 list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
452 list_del_init(&chunk->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (sctp_chunk_is_data(chunk)) {
454
455 if (!chunk->has_tsn) {
456 sctp_chunk_assign_ssn(chunk);
457 sctp_chunk_assign_tsn(chunk);
458
459 /* 6.3.1 C4) When data is in flight and when allowed
460 * by rule C5, a new RTT measurement MUST be made each
461 * round trip. Furthermore, new RTT measurements
462 * SHOULD be made no more than once per round-trip
463 * for a given destination transport address.
464 */
465
466 if (!tp->rto_pending) {
467 chunk->rtt_in_progress = 1;
468 tp->rto_pending = 1;
469 }
470 } else
471 chunk->resent = 1;
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 has_data = 1;
474 }
475
476 padding = WORD_ROUND(chunk->skb->len) - chunk->skb->len;
477 if (padding)
478 memset(skb_put(chunk->skb, padding), 0, padding);
479
Vlad Yasevich4cd57c82007-09-16 19:32:45 -0700480 /* if this is the auth chunk that we are adding,
481 * store pointer where it will be added and put
482 * the auth into the packet.
483 */
484 if (chunk == packet->auth)
485 auth = skb_tail_pointer(nskb);
486
487 cksum_buf_len += chunk->skb->len;
488 memcpy(skb_put(nskb, chunk->skb->len),
Sridhar Samudrala503b55f2006-06-17 22:57:28 -0700489 chunk->skb->data, chunk->skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 SCTP_DEBUG_PRINTK("%s %p[%s] %s 0x%x, %s %d, %s %d, %s %d\n",
492 "*** Chunk", chunk,
493 sctp_cname(SCTP_ST_CHUNK(
494 chunk->chunk_hdr->type)),
495 chunk->has_tsn ? "TSN" : "No TSN",
496 chunk->has_tsn ?
497 ntohl(chunk->subh.data_hdr->tsn) : 0,
498 "length", ntohs(chunk->chunk_hdr->length),
499 "chunk->skb->len", chunk->skb->len,
500 "rtt_in_progress", chunk->rtt_in_progress);
501
502 /*
503 * If this is a control chunk, this is our last
504 * reference. Free data chunks after they've been
505 * acknowledged or have failed.
506 */
507 if (!sctp_chunk_is_data(chunk))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900508 sctp_chunk_free(chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
510
Vlad Yasevich4cd57c82007-09-16 19:32:45 -0700511 /* SCTP-AUTH, Section 6.2
512 * The sender MUST calculate the MAC as described in RFC2104 [2]
513 * using the hash function H as described by the MAC Identifier and
514 * the shared association key K based on the endpoint pair shared key
515 * described by the shared key identifier. The 'data' used for the
516 * computation of the AUTH-chunk is given by the AUTH chunk with its
517 * HMAC field set to zero (as shown in Figure 6) followed by all
518 * chunks that are placed after the AUTH chunk in the SCTP packet.
519 */
520 if (auth)
521 sctp_auth_calculate_hmac(asoc, nskb,
522 (struct sctp_auth_chunk *)auth,
523 GFP_ATOMIC);
524
525 /* 2) Calculate the Adler-32 checksum of the whole packet,
526 * including the SCTP common header and all the
527 * chunks.
528 *
529 * Note: Adler-32 is no longer applicable, as has been replaced
530 * by CRC32-C as described in <draft-ietf-tsvwg-sctpcsum-02.txt>.
531 */
Jesse Brandeburg8dc92f72009-04-27 22:35:52 +0000532 if (!sctp_checksum_disable &&
533 !(dst->dev->features & (NETIF_F_NO_CSUM | NETIF_F_SCTP_CSUM))) {
Vlad Yasevich4458f042009-02-13 08:33:42 +0000534 __u32 crc32 = sctp_start_cksum((__u8 *)sh, cksum_buf_len);
535
536 /* 3) Put the resultant value into the checksum field in the
537 * common header, and leave the rest of the bits unchanged.
538 */
539 sh->checksum = sctp_end_cksum(crc32);
Jesse Brandeburg8dc92f72009-04-27 22:35:52 +0000540 } else {
541 if (dst->dev->features & NETIF_F_SCTP_CSUM) {
542 /* no need to seed psuedo checksum for SCTP */
543 nskb->ip_summed = CHECKSUM_PARTIAL;
544 nskb->csum_start = (skb_transport_header(nskb) -
545 nskb->head);
546 nskb->csum_offset = offsetof(struct sctphdr, checksum);
547 } else {
548 nskb->ip_summed = CHECKSUM_UNNECESSARY;
549 }
550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 /* IP layer ECN support
553 * From RFC 2481
554 * "The ECN-Capable Transport (ECT) bit would be set by the
555 * data sender to indicate that the end-points of the
556 * transport protocol are ECN-capable."
557 *
558 * Now setting the ECT bit all the time, as it should not cause
559 * any problems protocol-wise even if our peer ignores it.
560 *
561 * Note: The works for IPv6 layer checks this bit too later
562 * in transmission. See IP6_ECN_flow_xmit().
563 */
Vlad Yasevichb9031d92008-06-04 12:40:15 -0700564 (*tp->af_specific->ecn_capable)(nskb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 /* Set up the IP options. */
567 /* BUG: not implemented
568 * For v4 this all lives somewhere in sk->sk_opt...
569 */
570
571 /* Dump that on IP! */
572 if (asoc && asoc->peer.last_sent_to != tp) {
573 /* Considering the multiple CPU scenario, this is a
574 * "correcter" place for last_sent_to. --xguo
575 */
576 asoc->peer.last_sent_to = tp;
577 }
578
579 if (has_data) {
580 struct timer_list *timer;
581 unsigned long timeout;
582
583 tp->last_time_used = jiffies;
584
585 /* Restart the AUTOCLOSE timer when sending data. */
586 if (sctp_state(asoc, ESTABLISHED) && asoc->autoclose) {
587 timer = &asoc->timers[SCTP_EVENT_TIMEOUT_AUTOCLOSE];
588 timeout = asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE];
589
590 if (!mod_timer(timer, jiffies + timeout))
591 sctp_association_hold(asoc);
592 }
593 }
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 SCTP_DEBUG_PRINTK("***sctp_transmit_packet*** skb len %d\n",
596 nskb->len);
597
Herbert Xuf8803742008-08-03 21:15:08 -0700598 nskb->local_df = packet->ipfragok;
599 (*tp->af_specific->sctp_xmit)(nskb, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601out:
602 packet->size = packet->overhead;
603 return err;
604no_route:
605 kfree_skb(nskb);
Pavel Emelyanov7c73a6f2008-07-16 20:20:11 -0700606 IP_INC_STATS_BH(&init_net, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 /* FIXME: Returning the 'err' will effect all the associations
609 * associated with a socket, although only one of the paths of the
610 * association is unreachable.
611 * The real failure of a transport or association can be passed on
612 * to the user via notifications. So setting this error may not be
613 * required.
614 */
615 /* err = -EHOSTUNREACH; */
616err:
617 /* Control chunks are unreliable so just drop them. DATA chunks
618 * will get resent or dropped later.
619 */
620
David S. Miller79af02c2005-07-08 21:47:49 -0700621 list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
622 list_del_init(&chunk->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (!sctp_chunk_is_data(chunk))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900624 sctp_chunk_free(chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
626 goto out;
627nomem:
628 err = -ENOMEM;
629 goto err;
630}
631
632/********************************************************************
633 * 2nd Level Abstractions
634 ********************************************************************/
635
636/* This private function handles the specifics of appending DATA chunks. */
637static sctp_xmit_t sctp_packet_append_data(struct sctp_packet *packet,
638 struct sctp_chunk *chunk)
639{
640 sctp_xmit_t retval = SCTP_XMIT_OK;
641 size_t datasize, rwnd, inflight;
642 struct sctp_transport *transport = packet->transport;
643 __u32 max_burst_bytes;
644 struct sctp_association *asoc = transport->asoc;
645 struct sctp_sock *sp = sctp_sk(asoc->base.sk);
646 struct sctp_outq *q = &asoc->outqueue;
647
648 /* RFC 2960 6.1 Transmission of DATA Chunks
649 *
650 * A) At any given time, the data sender MUST NOT transmit new data to
651 * any destination transport address if its peer's rwnd indicates
652 * that the peer has no buffer space (i.e. rwnd is 0, see Section
653 * 6.2.1). However, regardless of the value of rwnd (including if it
654 * is 0), the data sender can always have one DATA chunk in flight to
655 * the receiver if allowed by cwnd (see rule B below). This rule
656 * allows the sender to probe for a change in rwnd that the sender
657 * missed due to the SACK having been lost in transit from the data
658 * receiver to the data sender.
659 */
660
661 rwnd = asoc->peer.rwnd;
662 inflight = asoc->outqueue.outstanding_bytes;
663
664 datasize = sctp_data_size(chunk);
665
666 if (datasize > rwnd) {
667 if (inflight > 0) {
668 /* We have (at least) one data chunk in flight,
669 * so we can't fall back to rule 6.1 B).
670 */
671 retval = SCTP_XMIT_RWND_FULL;
672 goto finish;
673 }
674 }
675
676 /* sctpimpguide-05 2.14.2
677 * D) When the time comes for the sender to
678 * transmit new DATA chunks, the protocol parameter Max.Burst MUST
679 * first be applied to limit how many new DATA chunks may be sent.
680 * The limit is applied by adjusting cwnd as follows:
681 * if ((flightsize + Max.Burst * MTU) < cwnd)
682 * cwnd = flightsize + Max.Burst * MTU
683 */
Frank Filz52ccb8e2005-12-22 11:36:46 -0800684 max_burst_bytes = asoc->max_burst * asoc->pathmtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if ((transport->flight_size + max_burst_bytes) < transport->cwnd) {
686 transport->cwnd = transport->flight_size + max_burst_bytes;
687 SCTP_DEBUG_PRINTK("%s: cwnd limited by max_burst: "
688 "transport: %p, cwnd: %d, "
689 "ssthresh: %d, flight_size: %d, "
690 "pba: %d\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800691 __func__, transport,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 transport->cwnd,
693 transport->ssthresh,
694 transport->flight_size,
695 transport->partial_bytes_acked);
696 }
697
698 /* RFC 2960 6.1 Transmission of DATA Chunks
699 *
700 * B) At any given time, the sender MUST NOT transmit new data
701 * to a given transport address if it has cwnd or more bytes
702 * of data outstanding to that transport address.
703 */
704 /* RFC 7.2.4 & the Implementers Guide 2.8.
705 *
706 * 3) ...
707 * When a Fast Retransmit is being performed the sender SHOULD
708 * ignore the value of cwnd and SHOULD NOT delay retransmission.
709 */
Neil Hormanc226ef92008-07-25 12:44:09 -0400710 if (chunk->fast_retransmit != SCTP_NEED_FRTX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (transport->flight_size >= transport->cwnd) {
712 retval = SCTP_XMIT_RWND_FULL;
713 goto finish;
714 }
715
716 /* Nagle's algorithm to solve small-packet problem:
717 * Inhibit the sending of new chunks when new outgoing data arrives
718 * if any previously transmitted data on the connection remains
719 * unacknowledged.
720 */
721 if (!sp->nodelay && sctp_packet_empty(packet) &&
722 q->outstanding_bytes && sctp_state(asoc, ESTABLISHED)) {
723 unsigned len = datasize + q->out_qlen;
724
725 /* Check whether this chunk and all the rest of pending
726 * data will fit or delay in hopes of bundling a full
727 * sized packet.
728 */
Sridhar Samudralacd497882006-09-29 17:09:05 -0700729 if (len < asoc->frag_point) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 retval = SCTP_XMIT_NAGLE_DELAY;
731 goto finish;
732 }
733 }
734
735 /* Keep track of how many bytes are in flight over this transport. */
736 transport->flight_size += datasize;
737
738 /* Keep track of how many bytes are in flight to the receiver. */
739 asoc->outqueue.outstanding_bytes += datasize;
740
Sridhar Samudralacd497882006-09-29 17:09:05 -0700741 /* Update our view of the receiver's rwnd. Include sk_buff overhead
742 * while updating peer.rwnd so that it reduces the chances of a
743 * receiver running out of receive buffer space even when receive
744 * window is still open. This can happen when a sender is sending
745 * sending small messages.
746 */
747 datasize += sizeof(struct sk_buff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 if (datasize < rwnd)
749 rwnd -= datasize;
750 else
751 rwnd = 0;
752
753 asoc->peer.rwnd = rwnd;
754 /* Has been accepted for transmission. */
755 if (!asoc->peer.prsctp_capable)
756 chunk->msg->can_abandon = 0;
757
758finish:
759 return retval;
760}