firestream: fix spelling mistake: "Inititing" -> "Initializing"
[linux-block.git] / net / sctp / output.c
CommitLineData
60c778b2 1/* SCTP kernel implementation
1da177e4
LT
2 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 *
60c778b2 6 * This file is part of the SCTP kernel implementation
1da177e4
LT
7 *
8 * These functions handle output processing.
9 *
60c778b2 10 * This SCTP implementation is free software;
1da177e4
LT
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 *
60c778b2 16 * This SCTP implementation is distributed in the hope that it
1da177e4
LT
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
4b2f13a2
JK
23 * along with GNU CC; see the file COPYING. If not, see
24 * <http://www.gnu.org/licenses/>.
1da177e4
LT
25 *
26 * Please send any bug reports or fixes you make to the
27 * email address(es):
91705c61 28 * lksctp developers <linux-sctp@vger.kernel.org>
1da177e4 29 *
1da177e4
LT
30 * Written or modified by:
31 * La Monte H.P. Yarroll <piggy@acm.org>
32 * Karl Knutson <karl@athena.chicago.il.us>
33 * Jon Grimm <jgrimm@austin.ibm.com>
34 * Sridhar Samudrala <sri@us.ibm.com>
1da177e4
LT
35 */
36
145ce502
JP
37#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
38
1da177e4
LT
39#include <linux/types.h>
40#include <linux/kernel.h>
41#include <linux/wait.h>
42#include <linux/time.h>
43#include <linux/ip.h>
44#include <linux/ipv6.h>
45#include <linux/init.h>
5a0e3ad6 46#include <linux/slab.h>
1da177e4 47#include <net/inet_ecn.h>
8d2f9e81 48#include <net/ip.h>
1da177e4 49#include <net/icmp.h>
7c73a6fa 50#include <net/net_namespace.h>
1da177e4 51
1da177e4
LT
52#include <linux/socket.h> /* for sa_family_t */
53#include <net/sock.h>
54
55#include <net/sctp/sctp.h>
56#include <net/sctp/sm.h>
9ad0977f 57#include <net/sctp/checksum.h>
1da177e4
LT
58
59/* Forward declarations for private helpers. */
86b36f2a
XL
60static enum sctp_xmit __sctp_packet_append_chunk(struct sctp_packet *packet,
61 struct sctp_chunk *chunk);
62static enum sctp_xmit sctp_packet_can_append_data(struct sctp_packet *packet,
63 struct sctp_chunk *chunk);
e83963b7 64static void sctp_packet_append_data(struct sctp_packet *packet,
86b36f2a
XL
65 struct sctp_chunk *chunk);
66static enum sctp_xmit sctp_packet_will_fit(struct sctp_packet *packet,
67 struct sctp_chunk *chunk,
68 u16 chunk_len);
1da177e4 69
be297143
WY
70static void sctp_packet_reset(struct sctp_packet *packet)
71{
b7e10c25
RH
72 /* sctp_packet_transmit() relies on this to reset size to the
73 * current overhead after sending packets.
74 */
be297143 75 packet->size = packet->overhead;
b7e10c25 76
be297143
WY
77 packet->has_cookie_echo = 0;
78 packet->has_sack = 0;
79 packet->has_data = 0;
80 packet->has_auth = 0;
81 packet->ipfragok = 0;
82 packet->auth = NULL;
83}
84
1da177e4
LT
85/* Config a packet.
86 * This appears to be a followup set of initializations.
87 */
66b91d2c
MRL
88void sctp_packet_config(struct sctp_packet *packet, __u32 vtag,
89 int ecn_capable)
1da177e4 90{
90017acc
MRL
91 struct sctp_transport *tp = packet->transport;
92 struct sctp_association *asoc = tp->asoc;
feddd6c1 93 struct sctp_sock *sp = NULL;
df2729c3 94 struct sock *sk;
1da177e4 95
bb33381d 96 pr_debug("%s: packet:%p vtag:0x%x\n", __func__, packet, vtag);
1da177e4 97 packet->vtag = vtag;
1da177e4 98
df2729c3
XL
99 /* do the following jobs only once for a flush schedule */
100 if (!sctp_packet_empty(packet))
101 return;
90017acc 102
b7e10c25 103 /* set packet max_size with pathmtu, then calculate overhead */
df2729c3 104 packet->max_size = tp->pathmtu;
feddd6c1 105
b7e10c25 106 if (asoc) {
feddd6c1
MRL
107 sk = asoc->base.sk;
108 sp = sctp_sk(sk);
109 }
110 packet->overhead = sctp_mtu_payload(sp, 0, 0);
111 packet->size = packet->overhead;
b7e10c25 112
feddd6c1 113 if (!asoc)
df2729c3 114 return;
90017acc 115
df2729c3 116 /* update dst or transport pathmtu if in need */
df2729c3 117 if (!sctp_transport_dst_check(tp)) {
feddd6c1 118 sctp_transport_route(tp, NULL, sp);
df2729c3 119 if (asoc->param_flags & SPP_PMTUD_ENABLE)
3ebfdf08 120 sctp_assoc_sync_pmtu(asoc);
69fec325
XL
121 } else if (!sctp_transport_pmtu_check(tp)) {
122 if (asoc->param_flags & SPP_PMTUD_ENABLE)
123 sctp_assoc_sync_pmtu(asoc);
90017acc
MRL
124 }
125
d805397c
XL
126 if (asoc->pmtu_pending) {
127 if (asoc->param_flags & SPP_PMTUD_ENABLE)
128 sctp_assoc_sync_pmtu(asoc);
129 asoc->pmtu_pending = 0;
130 }
131
df2729c3
XL
132 /* If there a is a prepend chunk stick it on the list before
133 * any other chunks get appended.
134 */
135 if (ecn_capable) {
136 struct sctp_chunk *chunk = sctp_get_ecne_prepend(asoc);
1da177e4 137
1da177e4
LT
138 if (chunk)
139 sctp_packet_append_chunk(packet, chunk);
140 }
df2729c3
XL
141
142 if (!tp->dst)
143 return;
144
145 /* set packet max_size with gso_max_size if gso is enabled*/
146 rcu_read_lock();
147 if (__sk_dst_get(sk) != tp->dst) {
148 dst_hold(tp->dst);
149 sk_setup_caps(sk, tp->dst);
150 }
151 packet->max_size = sk_can_gso(sk) ? tp->dst->dev->gso_max_size
152 : asoc->pathmtu;
153 rcu_read_unlock();
1da177e4
LT
154}
155
156/* Initialize the packet structure. */
66b91d2c
MRL
157void sctp_packet_init(struct sctp_packet *packet,
158 struct sctp_transport *transport,
159 __u16 sport, __u16 dport)
1da177e4 160{
bb33381d 161 pr_debug("%s: packet:%p transport:%p\n", __func__, packet, transport);
1da177e4
LT
162
163 packet->transport = transport;
164 packet->source_port = sport;
165 packet->destination_port = dport;
79af02c2 166 INIT_LIST_HEAD(&packet->chunk_list);
b7e10c25
RH
167 /* The overhead will be calculated by sctp_packet_config() */
168 packet->overhead = 0;
be297143 169 sctp_packet_reset(packet);
1da177e4 170 packet->vtag = 0;
1da177e4
LT
171}
172
173/* Free a packet. */
174void sctp_packet_free(struct sctp_packet *packet)
175{
79af02c2 176 struct sctp_chunk *chunk, *tmp;
1da177e4 177
bb33381d 178 pr_debug("%s: packet:%p\n", __func__, packet);
1da177e4 179
79af02c2
DM
180 list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
181 list_del_init(&chunk->list);
1da177e4 182 sctp_chunk_free(chunk);
79af02c2 183 }
1da177e4
LT
184}
185
186/* This routine tries to append the chunk to the offered packet. If adding
187 * the chunk causes the packet to exceed the path MTU and COOKIE_ECHO chunk
188 * is not present in the packet, it transmits the input packet.
189 * Data can be bundled with a packet containing a COOKIE_ECHO chunk as long
190 * as it can fit in the packet, but any more data that does not fit in this
191 * packet can be sent only after receiving the COOKIE_ACK.
192 */
86b36f2a
XL
193enum sctp_xmit sctp_packet_transmit_chunk(struct sctp_packet *packet,
194 struct sctp_chunk *chunk,
195 int one_packet, gfp_t gfp)
1da177e4 196{
86b36f2a 197 enum sctp_xmit retval;
1da177e4 198
5b5e0928 199 pr_debug("%s: packet:%p size:%zu chunk:%p size:%d\n", __func__,
942b3235 200 packet, packet->size, chunk, chunk->skb ? chunk->skb->len : -1);
1da177e4
LT
201
202 switch ((retval = (sctp_packet_append_chunk(packet, chunk)))) {
203 case SCTP_XMIT_PMTU_FULL:
204 if (!packet->has_cookie_echo) {
64519440
XL
205 int error = 0;
206
cea8768f 207 error = sctp_packet_transmit(packet, gfp);
1da177e4
LT
208 if (error < 0)
209 chunk->skb->sk->sk_err = -error;
210
211 /* If we have an empty packet, then we can NOT ever
212 * return PMTU_FULL.
213 */
2e3216cd
VY
214 if (!one_packet)
215 retval = sctp_packet_append_chunk(packet,
216 chunk);
1da177e4
LT
217 }
218 break;
219
220 case SCTP_XMIT_RWND_FULL:
221 case SCTP_XMIT_OK:
526cbef7 222 case SCTP_XMIT_DELAY:
1da177e4 223 break;
3ff50b79 224 }
1da177e4
LT
225
226 return retval;
227}
228
4cd57c80 229/* Try to bundle an auth chunk into the packet. */
86b36f2a
XL
230static enum sctp_xmit sctp_packet_bundle_auth(struct sctp_packet *pkt,
231 struct sctp_chunk *chunk)
4cd57c80
VY
232{
233 struct sctp_association *asoc = pkt->transport->asoc;
86b36f2a 234 enum sctp_xmit retval = SCTP_XMIT_OK;
4cd57c80 235 struct sctp_chunk *auth;
4cd57c80
VY
236
237 /* if we don't have an association, we can't do authentication */
238 if (!asoc)
239 return retval;
240
241 /* See if this is an auth chunk we are bundling or if
242 * auth is already bundled.
243 */
4007cc88 244 if (chunk->chunk_hdr->type == SCTP_CID_AUTH || pkt->has_auth)
4cd57c80
VY
245 return retval;
246
247 /* if the peer did not request this chunk to be authenticated,
248 * don't do it
249 */
250 if (!chunk->auth)
251 return retval;
252
1b1e0bc9 253 auth = sctp_make_auth(asoc, chunk->shkey->key_id);
4cd57c80
VY
254 if (!auth)
255 return retval;
256
1b1e0bc9
XL
257 auth->shkey = chunk->shkey;
258 sctp_auth_shkey_hold(auth->shkey);
259
ed106277
NH
260 retval = __sctp_packet_append_chunk(pkt, auth);
261
262 if (retval != SCTP_XMIT_OK)
263 sctp_chunk_free(auth);
4cd57c80
VY
264
265 return retval;
266}
267
1da177e4 268/* Try to bundle a SACK with the packet. */
86b36f2a
XL
269static enum sctp_xmit sctp_packet_bundle_sack(struct sctp_packet *pkt,
270 struct sctp_chunk *chunk)
1da177e4 271{
86b36f2a 272 enum sctp_xmit retval = SCTP_XMIT_OK;
1da177e4
LT
273
274 /* If sending DATA and haven't aleady bundled a SACK, try to
275 * bundle one in to the packet.
276 */
277 if (sctp_chunk_is_data(chunk) && !pkt->has_sack &&
278 !pkt->has_cookie_echo) {
279 struct sctp_association *asoc;
af87b823 280 struct timer_list *timer;
1da177e4 281 asoc = pkt->transport->asoc;
af87b823 282 timer = &asoc->timers[SCTP_EVENT_TIMEOUT_SACK];
1da177e4 283
af87b823
DG
284 /* If the SACK timer is running, we have a pending SACK */
285 if (timer_pending(timer)) {
1da177e4 286 struct sctp_chunk *sack;
4244854d
NH
287
288 if (pkt->transport->sack_generation !=
289 pkt->transport->asoc->peer.sack_generation)
290 return retval;
291
1da177e4
LT
292 asoc->a_rwnd = asoc->rwnd;
293 sack = sctp_make_sack(asoc);
294 if (sack) {
ed106277
NH
295 retval = __sctp_packet_append_chunk(pkt, sack);
296 if (retval != SCTP_XMIT_OK) {
297 sctp_chunk_free(sack);
298 goto out;
299 }
1da177e4 300 asoc->peer.sack_needed = 0;
af87b823 301 if (del_timer(timer))
1da177e4
LT
302 sctp_association_put(asoc);
303 }
304 }
305 }
ed106277 306out:
1da177e4
LT
307 return retval;
308}
309
ed106277 310
1da177e4
LT
311/* Append a chunk to the offered packet reporting back any inability to do
312 * so.
313 */
86b36f2a
XL
314static enum sctp_xmit __sctp_packet_append_chunk(struct sctp_packet *packet,
315 struct sctp_chunk *chunk)
1da177e4 316{
e2f036a9 317 __u16 chunk_len = SCTP_PAD4(ntohs(chunk->chunk_hdr->length));
86b36f2a 318 enum sctp_xmit retval = SCTP_XMIT_OK;
1da177e4 319
e83963b7
VY
320 /* Check to see if this chunk will fit into the packet */
321 retval = sctp_packet_will_fit(packet, chunk, chunk_len);
322 if (retval != SCTP_XMIT_OK)
323 goto finish;
1da177e4 324
e83963b7 325 /* We believe that this chunk is OK to add to the packet */
4cd57c80 326 switch (chunk->chunk_hdr->type) {
f7010e61 327 case SCTP_CID_DATA:
668c9beb 328 case SCTP_CID_I_DATA:
e83963b7
VY
329 /* Account for the data being in the packet */
330 sctp_packet_append_data(packet, chunk);
1da177e4
LT
331 /* Disallow SACK bundling after DATA. */
332 packet->has_sack = 1;
4cd57c80
VY
333 /* Disallow AUTH bundling after DATA */
334 packet->has_auth = 1;
335 /* Let it be knows that packet has DATA in it */
336 packet->has_data = 1;
759af00e
VY
337 /* timestamp the chunk for rtx purposes */
338 chunk->sent_at = jiffies;
a6c2f792
XL
339 /* Mainly used for prsctp RTX policy */
340 chunk->sent_count++;
4cd57c80 341 break;
f7010e61 342 case SCTP_CID_COOKIE_ECHO:
1da177e4 343 packet->has_cookie_echo = 1;
4cd57c80
VY
344 break;
345
f7010e61 346 case SCTP_CID_SACK:
1da177e4 347 packet->has_sack = 1;
196d6759
MB
348 if (chunk->asoc)
349 chunk->asoc->stats.osacks++;
4cd57c80
VY
350 break;
351
f7010e61 352 case SCTP_CID_AUTH:
4cd57c80
VY
353 packet->has_auth = 1;
354 packet->auth = chunk;
355 break;
356 }
1da177e4
LT
357
358 /* It is OK to send this chunk. */
79af02c2 359 list_add_tail(&chunk->list, &packet->chunk_list);
1da177e4
LT
360 packet->size += chunk_len;
361 chunk->transport = packet->transport;
362finish:
363 return retval;
364}
365
ed106277
NH
366/* Append a chunk to the offered packet reporting back any inability to do
367 * so.
368 */
86b36f2a
XL
369enum sctp_xmit sctp_packet_append_chunk(struct sctp_packet *packet,
370 struct sctp_chunk *chunk)
ed106277 371{
86b36f2a 372 enum sctp_xmit retval = SCTP_XMIT_OK;
ed106277 373
bb33381d 374 pr_debug("%s: packet:%p chunk:%p\n", __func__, packet, chunk);
ed106277
NH
375
376 /* Data chunks are special. Before seeing what else we can
377 * bundle into this packet, check to see if we are allowed to
378 * send this DATA.
379 */
380 if (sctp_chunk_is_data(chunk)) {
381 retval = sctp_packet_can_append_data(packet, chunk);
382 if (retval != SCTP_XMIT_OK)
383 goto finish;
384 }
385
386 /* Try to bundle AUTH chunk */
387 retval = sctp_packet_bundle_auth(packet, chunk);
388 if (retval != SCTP_XMIT_OK)
389 goto finish;
390
391 /* Try to bundle SACK chunk */
392 retval = sctp_packet_bundle_sack(packet, chunk);
393 if (retval != SCTP_XMIT_OK)
394 goto finish;
395
396 retval = __sctp_packet_append_chunk(packet, chunk);
397
398finish:
399 return retval;
400}
401
99519122
XL
402static void sctp_packet_gso_append(struct sk_buff *head, struct sk_buff *skb)
403{
404 if (SCTP_OUTPUT_CB(head)->last == head)
405 skb_shinfo(head)->frag_list = skb;
406 else
407 SCTP_OUTPUT_CB(head)->last->next = skb;
408 SCTP_OUTPUT_CB(head)->last = skb;
409
410 head->truesize += skb->truesize;
411 head->data_len += skb->len;
412 head->len += skb->len;
413
414 __skb_header_release(skb);
415}
416
e4ff952a
XL
417static int sctp_packet_pack(struct sctp_packet *packet,
418 struct sk_buff *head, int gso, gfp_t gfp)
1da177e4
LT
419{
420 struct sctp_transport *tp = packet->transport;
e4ff952a 421 struct sctp_auth_chunk *auth = NULL;
79af02c2 422 struct sctp_chunk *chunk, *tmp;
e4ff952a
XL
423 int pkt_count = 0, pkt_size;
424 struct sock *sk = head->sk;
425 struct sk_buff *nskb;
ecc515d7 426 int auth_len = 0;
1da177e4 427
90017acc 428 if (gso) {
90017acc 429 skb_shinfo(head)->gso_type = sk->sk_gso_type;
99519122 430 SCTP_OUTPUT_CB(head)->last = head;
e4ff952a
XL
431 } else {
432 nskb = head;
433 pkt_size = packet->size;
434 goto merge;
90017acc 435 }
1da177e4 436
90017acc 437 do {
e4ff952a
XL
438 /* calculate the pkt_size and alloc nskb */
439 pkt_size = packet->overhead;
440 list_for_each_entry_safe(chunk, tmp, &packet->chunk_list,
441 list) {
442 int padded = SCTP_PAD4(chunk->skb->len);
6eabca54 443
e4ff952a
XL
444 if (chunk == packet->auth)
445 auth_len = padded;
446 else if (auth_len + padded + packet->overhead >
447 tp->pathmtu)
448 return 0;
449 else if (pkt_size + padded > tp->pathmtu)
450 break;
451 pkt_size += padded;
90017acc 452 }
e4ff952a
XL
453 nskb = alloc_skb(pkt_size + MAX_HEADER, gfp);
454 if (!nskb)
455 return 0;
456 skb_reserve(nskb, packet->overhead + MAX_HEADER);
1da177e4 457
e4ff952a
XL
458merge:
459 /* merge chunks into nskb and append nskb into head list */
90017acc
MRL
460 pkt_size -= packet->overhead;
461 list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
e4ff952a
XL
462 int padding;
463
90017acc
MRL
464 list_del_init(&chunk->list);
465 if (sctp_chunk_is_data(chunk)) {
cc6ac9bc
XL
466 if (!sctp_chunk_retransmitted(chunk) &&
467 !tp->rto_pending) {
90017acc
MRL
468 chunk->rtt_in_progress = 1;
469 tp->rto_pending = 1;
470 }
90017acc
MRL
471 }
472
e2f036a9 473 padding = SCTP_PAD4(chunk->skb->len) - chunk->skb->len;
90017acc 474 if (padding)
b080db58 475 skb_put_zero(chunk->skb, padding);
90017acc 476
90017acc 477 if (chunk == packet->auth)
e4ff952a
XL
478 auth = (struct sctp_auth_chunk *)
479 skb_tail_pointer(nskb);
90017acc 480
59ae1d12 481 skb_put_data(nskb, chunk->skb->data, chunk->skb->len);
1da177e4 482
90017acc
MRL
483 pr_debug("*** Chunk:%p[%s] %s 0x%x, length:%d, chunk->skb->len:%d, rtt_in_progress:%d\n",
484 chunk,
485 sctp_cname(SCTP_ST_CHUNK(chunk->chunk_hdr->type)),
486 chunk->has_tsn ? "TSN" : "No TSN",
487 chunk->has_tsn ? ntohl(chunk->subh.data_hdr->tsn) : 0,
488 ntohs(chunk->chunk_hdr->length), chunk->skb->len,
489 chunk->rtt_in_progress);
490
e2f036a9 491 pkt_size -= SCTP_PAD4(chunk->skb->len);
1da177e4 492
f1533cce 493 if (!sctp_chunk_is_data(chunk) && chunk != packet->auth)
90017acc
MRL
494 sctp_chunk_free(chunk);
495
496 if (!pkt_size)
497 break;
498 }
499
e4ff952a 500 if (auth) {
1b1e0bc9
XL
501 sctp_auth_calculate_hmac(tp->asoc, nskb, auth,
502 packet->auth->shkey, gfp);
e4ff952a
XL
503 /* free auth if no more chunks, or add it back */
504 if (list_empty(&packet->chunk_list))
505 sctp_chunk_free(packet->auth);
506 else
1aa25ec2
XL
507 list_add(&packet->auth->list,
508 &packet->chunk_list);
f1533cce
MRL
509 }
510
99519122
XL
511 if (gso)
512 sctp_packet_gso_append(head, nskb);
e4ff952a
XL
513
514 pkt_count++;
90017acc 515 } while (!list_empty(&packet->chunk_list));
4cd57c80 516
e4ff952a
XL
517 if (gso) {
518 memset(head->cb, 0, max(sizeof(struct inet_skb_parm),
519 sizeof(struct inet6_skb_parm)));
520 skb_shinfo(head)->gso_segs = pkt_count;
521 skb_shinfo(head)->gso_size = GSO_BY_FRAGS;
522 rcu_read_lock();
523 if (skb_dst(head) != tp->dst) {
524 dst_hold(tp->dst);
525 sk_setup_caps(sk, tp->dst);
8dc92f7e 526 }
e4ff952a
XL
527 rcu_read_unlock();
528 goto chksum;
8dc92f7e 529 }
1da177e4 530
e4ff952a
XL
531 if (sctp_checksum_disable)
532 return 1;
1da177e4 533
e4ff952a
XL
534 if (!(skb_dst(head)->dev->features & NETIF_F_SCTP_CRC) ||
535 dst_xfrm(skb_dst(head)) || packet->ipfragok) {
536 struct sctphdr *sh =
537 (struct sctphdr *)skb_transport_header(head);
1da177e4 538
e4ff952a
XL
539 sh->checksum = sctp_compute_cksum(head, 0);
540 } else {
541chksum:
542 head->ip_summed = CHECKSUM_PARTIAL;
dba00306 543 head->csum_not_inet = 1;
e4ff952a
XL
544 head->csum_start = skb_transport_header(head) - head->head;
545 head->csum_offset = offsetof(struct sctphdr, checksum);
1da177e4
LT
546 }
547
e4ff952a
XL
548 return pkt_count;
549}
550
551/* All packets are sent to the network through this function from
552 * sctp_outq_tail().
553 *
554 * The return value is always 0 for now.
555 */
556int sctp_packet_transmit(struct sctp_packet *packet, gfp_t gfp)
557{
558 struct sctp_transport *tp = packet->transport;
559 struct sctp_association *asoc = tp->asoc;
560 struct sctp_chunk *chunk, *tmp;
561 int pkt_count, gso = 0;
562 struct dst_entry *dst;
563 struct sk_buff *head;
564 struct sctphdr *sh;
565 struct sock *sk;
1da177e4 566
e4ff952a
XL
567 pr_debug("%s: packet:%p\n", __func__, packet);
568 if (list_empty(&packet->chunk_list))
569 return 0;
570 chunk = list_entry(packet->chunk_list.next, struct sctp_chunk, list);
571 sk = chunk->skb->sk;
1da177e4 572
e4ff952a
XL
573 /* check gso */
574 if (packet->size > tp->pathmtu && !packet->ipfragok) {
575 if (!sk_can_gso(sk)) {
576 pr_err_once("Trying to GSO but underlying device doesn't support it.");
577 goto out;
1da177e4 578 }
e4ff952a
XL
579 gso = 1;
580 }
581
582 /* alloc head skb */
583 head = alloc_skb((gso ? packet->overhead : packet->size) +
584 MAX_HEADER, gfp);
585 if (!head)
586 goto out;
587 skb_reserve(head, packet->overhead + MAX_HEADER);
02968ccf 588 skb_set_owner_w(head, sk);
e4ff952a
XL
589
590 /* set sctp header */
d58ff351 591 sh = skb_push(head, sizeof(struct sctphdr));
e4ff952a
XL
592 skb_reset_transport_header(head);
593 sh->source = htons(packet->source_port);
594 sh->dest = htons(packet->destination_port);
595 sh->vtag = htonl(packet->vtag);
596 sh->checksum = 0;
597
df2729c3 598 /* drop packet if no dst */
e4ff952a
XL
599 dst = dst_clone(tp->dst);
600 if (!dst) {
601 IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTNOROUTES);
602 kfree_skb(head);
603 goto out;
604 }
605 skb_dst_set(head, dst);
1da177e4 606
e4ff952a
XL
607 /* pack up chunks */
608 pkt_count = sctp_packet_pack(packet, head, gso, gfp);
609 if (!pkt_count) {
610 kfree_skb(head);
611 goto out;
612 }
90017acc
MRL
613 pr_debug("***sctp_transmit_packet*** skb->len:%d\n", head->len);
614
e4ff952a
XL
615 /* start autoclose timer */
616 if (packet->has_data && sctp_state(asoc, ESTABLISHED) &&
617 asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
618 struct timer_list *timer =
619 &asoc->timers[SCTP_EVENT_TIMEOUT_AUTOCLOSE];
620 unsigned long timeout =
621 asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE];
1da177e4 622
e4ff952a
XL
623 if (!mod_timer(timer, jiffies + timeout))
624 sctp_association_hold(asoc);
625 }
90017acc 626
e4ff952a
XL
627 /* sctp xmit */
628 tp->af_specific->ecn_capable(sk);
629 if (asoc) {
630 asoc->stats.opackets += pkt_count;
631 if (asoc->peer.last_sent_to != tp)
632 asoc->peer.last_sent_to = tp;
90017acc
MRL
633 }
634 head->ignore_df = packet->ipfragok;
486a43db 635 if (tp->dst_pending_confirm)
c86a773c
JA
636 skb_set_dst_pending_confirm(head, 1);
637 /* neighbour should be confirmed on successful transmission or
638 * positive error
639 */
486a43db
XL
640 if (tp->af_specific->sctp_xmit(head, tp) >= 0 &&
641 tp->dst_pending_confirm)
c86a773c 642 tp->dst_pending_confirm = 0;
1da177e4 643
e4ff952a 644out:
79af02c2
DM
645 list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
646 list_del_init(&chunk->list);
1da177e4 647 if (!sctp_chunk_is_data(chunk))
d808ad9a 648 sctp_chunk_free(chunk);
1da177e4 649 }
41001faf 650 sctp_packet_reset(packet);
e4ff952a 651 return 0;
1da177e4
LT
652}
653
654/********************************************************************
655 * 2nd Level Abstractions
656 ********************************************************************/
657
e83963b7 658/* This private function check to see if a chunk can be added */
86b36f2a
XL
659static enum sctp_xmit sctp_packet_can_append_data(struct sctp_packet *packet,
660 struct sctp_chunk *chunk)
1da177e4 661{
e83963b7 662 size_t datasize, rwnd, inflight, flight_size;
1da177e4 663 struct sctp_transport *transport = packet->transport;
1da177e4 664 struct sctp_association *asoc = transport->asoc;
1da177e4
LT
665 struct sctp_outq *q = &asoc->outqueue;
666
667 /* RFC 2960 6.1 Transmission of DATA Chunks
668 *
669 * A) At any given time, the data sender MUST NOT transmit new data to
670 * any destination transport address if its peer's rwnd indicates
671 * that the peer has no buffer space (i.e. rwnd is 0, see Section
672 * 6.2.1). However, regardless of the value of rwnd (including if it
673 * is 0), the data sender can always have one DATA chunk in flight to
674 * the receiver if allowed by cwnd (see rule B below). This rule
675 * allows the sender to probe for a change in rwnd that the sender
676 * missed due to the SACK having been lost in transit from the data
677 * receiver to the data sender.
678 */
679
680 rwnd = asoc->peer.rwnd;
e83963b7
VY
681 inflight = q->outstanding_bytes;
682 flight_size = transport->flight_size;
1da177e4
LT
683
684 datasize = sctp_data_size(chunk);
685
723189fa
DL
686 if (datasize > rwnd && inflight > 0)
687 /* We have (at least) one data chunk in flight,
688 * so we can't fall back to rule 6.1 B).
689 */
690 return SCTP_XMIT_RWND_FULL;
1da177e4 691
1da177e4
LT
692 /* RFC 2960 6.1 Transmission of DATA Chunks
693 *
694 * B) At any given time, the sender MUST NOT transmit new data
695 * to a given transport address if it has cwnd or more bytes
696 * of data outstanding to that transport address.
697 */
698 /* RFC 7.2.4 & the Implementers Guide 2.8.
699 *
700 * 3) ...
701 * When a Fast Retransmit is being performed the sender SHOULD
702 * ignore the value of cwnd and SHOULD NOT delay retransmission.
703 */
723189fa
DL
704 if (chunk->fast_retransmit != SCTP_NEED_FRTX &&
705 flight_size >= transport->cwnd)
706 return SCTP_XMIT_RWND_FULL;
1da177e4
LT
707
708 /* Nagle's algorithm to solve small-packet problem:
709 * Inhibit the sending of new chunks when new outgoing data arrives
710 * if any previously transmitted data on the connection remains
711 * unacknowledged.
712 */
1da177e4 713
4ea0c32f 714 if ((sctp_sk(asoc->base.sk)->nodelay || inflight == 0) &&
f9ba3501 715 !asoc->force_delay)
4ea0c32f 716 /* Nothing unacked */
723189fa
DL
717 return SCTP_XMIT_OK;
718
719 if (!sctp_packet_empty(packet))
720 /* Append to packet */
721 return SCTP_XMIT_OK;
722
723189fa
DL
723 if (!sctp_state(asoc, ESTABLISHED))
724 return SCTP_XMIT_OK;
725
726 /* Check whether this chunk and all the rest of pending data will fit
727 * or delay in hopes of bundling a full sized packet.
728 */
9f8d3147 729 if (chunk->skb->len + q->out_qlen > transport->pathmtu -
668c9beb 730 packet->overhead - sctp_datachk_len(&chunk->asoc->stream) - 4)
723189fa
DL
731 /* Enough data queued to fill a packet */
732 return SCTP_XMIT_OK;
733
734 /* Don't delay large message writes that may have been fragmented */
735 if (!chunk->msg->can_delay)
736 return SCTP_XMIT_OK;
737
738 /* Defer until all data acked or packet full */
526cbef7 739 return SCTP_XMIT_DELAY;
e83963b7
VY
740}
741
742/* This private function does management things when adding DATA chunk */
743static void sctp_packet_append_data(struct sctp_packet *packet,
744 struct sctp_chunk *chunk)
745{
746 struct sctp_transport *transport = packet->transport;
747 size_t datasize = sctp_data_size(chunk);
748 struct sctp_association *asoc = transport->asoc;
749 u32 rwnd = asoc->peer.rwnd;
750
1da177e4
LT
751 /* Keep track of how many bytes are in flight over this transport. */
752 transport->flight_size += datasize;
753
754 /* Keep track of how many bytes are in flight to the receiver. */
755 asoc->outqueue.outstanding_bytes += datasize;
756
a76c0adf 757 /* Update our view of the receiver's rwnd. */
1da177e4
LT
758 if (datasize < rwnd)
759 rwnd -= datasize;
760 else
761 rwnd = 0;
762
763 asoc->peer.rwnd = rwnd;
d8dd1578 764 sctp_chunk_assign_tsn(chunk);
668c9beb 765 asoc->stream.si->assign_number(chunk);
e83963b7
VY
766}
767
86b36f2a
XL
768static enum sctp_xmit sctp_packet_will_fit(struct sctp_packet *packet,
769 struct sctp_chunk *chunk,
770 u16 chunk_len)
e83963b7 771{
86b36f2a 772 enum sctp_xmit retval = SCTP_XMIT_OK;
7303a147 773 size_t psize, pmtu, maxsize;
e83963b7 774
1b1e0bc9
XL
775 /* Don't bundle in this packet if this chunk's auth key doesn't
776 * match other chunks already enqueued on this packet. Also,
777 * don't bundle the chunk with auth key if other chunks in this
778 * packet don't have auth key.
779 */
780 if ((packet->auth && chunk->shkey != packet->auth->shkey) ||
781 (!packet->auth && chunk->shkey &&
782 chunk->chunk_hdr->type != SCTP_CID_AUTH))
783 return SCTP_XMIT_PMTU_FULL;
784
e83963b7 785 psize = packet->size;
90017acc
MRL
786 if (packet->transport->asoc)
787 pmtu = packet->transport->asoc->pathmtu;
788 else
789 pmtu = packet->transport->pathmtu;
e83963b7
VY
790
791 /* Decide if we need to fragment or resubmit later. */
90017acc
MRL
792 if (psize + chunk_len > pmtu) {
793 /* It's OK to fragment at IP level if any one of the following
e83963b7 794 * is true:
90017acc
MRL
795 * 1. The packet is empty (meaning this chunk is greater
796 * the MTU)
797 * 2. The packet doesn't have any data in it yet and data
798 * requires authentication.
e83963b7 799 */
90017acc 800 if (sctp_packet_empty(packet) ||
e83963b7
VY
801 (!packet->has_data && chunk->auth)) {
802 /* We no longer do re-fragmentation.
803 * Just fragment at the IP layer, if we
804 * actually hit this condition
805 */
806 packet->ipfragok = 1;
90017acc 807 goto out;
e83963b7 808 }
90017acc 809
7303a147
MRL
810 /* Similarly, if this chunk was built before a PMTU
811 * reduction, we have to fragment it at IP level now. So
812 * if the packet already contains something, we need to
813 * flush.
814 */
815 maxsize = pmtu - packet->overhead;
816 if (packet->auth)
e2f036a9 817 maxsize -= SCTP_PAD4(packet->auth->skb->len);
7303a147
MRL
818 if (chunk_len > maxsize)
819 retval = SCTP_XMIT_PMTU_FULL;
820
90017acc
MRL
821 /* It is also okay to fragment if the chunk we are
822 * adding is a control chunk, but only if current packet
823 * is not a GSO one otherwise it causes fragmentation of
824 * a large frame. So in this case we allow the
825 * fragmentation by forcing it to be in a new packet.
826 */
827 if (!sctp_chunk_is_data(chunk) && packet->has_data)
828 retval = SCTP_XMIT_PMTU_FULL;
829
830 if (psize + chunk_len > packet->max_size)
831 /* Hit GSO/PMTU limit, gotta flush */
832 retval = SCTP_XMIT_PMTU_FULL;
833
834 if (!packet->transport->burst_limited &&
835 psize + chunk_len > (packet->transport->cwnd >> 1))
836 /* Do not allow a single GSO packet to use more
837 * than half of cwnd.
838 */
839 retval = SCTP_XMIT_PMTU_FULL;
840
841 if (packet->transport->burst_limited &&
842 psize + chunk_len > (packet->transport->burst_limited >> 1))
843 /* Do not allow a single GSO packet to use more
844 * than half of original cwnd.
845 */
846 retval = SCTP_XMIT_PMTU_FULL;
847 /* Otherwise it will fit in the GSO packet */
e83963b7 848 }
1da177e4 849
90017acc 850out:
1da177e4
LT
851 return retval;
852}