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