]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/sctp/output.c
Merge branch 'centralize-netdevice-mtu-bounds-checking'
[mirror_ubuntu-bionic-kernel.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. */
ed106277
NH
60static sctp_xmit_t __sctp_packet_append_chunk(struct sctp_packet *packet,
61 struct sctp_chunk *chunk);
e83963b7 62static sctp_xmit_t sctp_packet_can_append_data(struct sctp_packet *packet,
1da177e4 63 struct sctp_chunk *chunk);
e83963b7
VY
64static void sctp_packet_append_data(struct sctp_packet *packet,
65 struct sctp_chunk *chunk);
66static sctp_xmit_t 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{
72 packet->size = packet->overhead;
73 packet->has_cookie_echo = 0;
74 packet->has_sack = 0;
75 packet->has_data = 0;
76 packet->has_auth = 0;
77 packet->ipfragok = 0;
78 packet->auth = NULL;
79}
80
1da177e4
LT
81/* Config a packet.
82 * This appears to be a followup set of initializations.
83 */
84struct sctp_packet *sctp_packet_config(struct sctp_packet *packet,
85 __u32 vtag, int ecn_capable)
86{
90017acc
MRL
87 struct sctp_transport *tp = packet->transport;
88 struct sctp_association *asoc = tp->asoc;
1da177e4 89
bb33381d 90 pr_debug("%s: packet:%p vtag:0x%x\n", __func__, packet, vtag);
1da177e4
LT
91
92 packet->vtag = vtag;
1da177e4 93
90017acc
MRL
94 if (asoc && tp->dst) {
95 struct sock *sk = asoc->base.sk;
96
97 rcu_read_lock();
98 if (__sk_dst_get(sk) != tp->dst) {
99 dst_hold(tp->dst);
100 sk_setup_caps(sk, tp->dst);
101 }
102
103 if (sk_can_gso(sk)) {
104 struct net_device *dev = tp->dst->dev;
105
106 packet->max_size = dev->gso_max_size;
107 } else {
108 packet->max_size = asoc->pathmtu;
109 }
110 rcu_read_unlock();
111
112 } else {
113 packet->max_size = tp->pathmtu;
114 }
115
1da177e4 116 if (ecn_capable && sctp_packet_empty(packet)) {
90017acc 117 struct sctp_chunk *chunk;
1da177e4
LT
118
119 /* If there a is a prepend chunk stick it on the list before
d808ad9a
YH
120 * any other chunks get appended.
121 */
90017acc 122 chunk = sctp_get_ecne_prepend(asoc);
1da177e4
LT
123 if (chunk)
124 sctp_packet_append_chunk(packet, chunk);
125 }
126
127 return packet;
128}
129
130/* Initialize the packet structure. */
131struct sctp_packet *sctp_packet_init(struct sctp_packet *packet,
132 struct sctp_transport *transport,
133 __u16 sport, __u16 dport)
134{
135 struct sctp_association *asoc = transport->asoc;
136 size_t overhead;
137
bb33381d 138 pr_debug("%s: packet:%p transport:%p\n", __func__, packet, transport);
1da177e4
LT
139
140 packet->transport = transport;
141 packet->source_port = sport;
142 packet->destination_port = dport;
79af02c2 143 INIT_LIST_HEAD(&packet->chunk_list);
1da177e4 144 if (asoc) {
d808ad9a
YH
145 struct sctp_sock *sp = sctp_sk(asoc->base.sk);
146 overhead = sp->pf->af->net_header_len;
1da177e4
LT
147 } else {
148 overhead = sizeof(struct ipv6hdr);
149 }
150 overhead += sizeof(struct sctphdr);
151 packet->overhead = overhead;
be297143 152 sctp_packet_reset(packet);
1da177e4 153 packet->vtag = 0;
3e3251b3 154
1da177e4
LT
155 return packet;
156}
157
158/* Free a packet. */
159void sctp_packet_free(struct sctp_packet *packet)
160{
79af02c2 161 struct sctp_chunk *chunk, *tmp;
1da177e4 162
bb33381d 163 pr_debug("%s: packet:%p\n", __func__, packet);
1da177e4 164
79af02c2
DM
165 list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
166 list_del_init(&chunk->list);
1da177e4 167 sctp_chunk_free(chunk);
79af02c2 168 }
1da177e4
LT
169}
170
171/* This routine tries to append the chunk to the offered packet. If adding
172 * the chunk causes the packet to exceed the path MTU and COOKIE_ECHO chunk
173 * is not present in the packet, it transmits the input packet.
174 * Data can be bundled with a packet containing a COOKIE_ECHO chunk as long
175 * as it can fit in the packet, but any more data that does not fit in this
176 * packet can be sent only after receiving the COOKIE_ACK.
177 */
178sctp_xmit_t sctp_packet_transmit_chunk(struct sctp_packet *packet,
2e3216cd 179 struct sctp_chunk *chunk,
cea8768f 180 int one_packet, gfp_t gfp)
1da177e4
LT
181{
182 sctp_xmit_t retval;
1da177e4 183
3b55a537 184 pr_debug("%s: packet:%p size:%Zu chunk:%p size:%d\n", __func__,
942b3235 185 packet, packet->size, chunk, chunk->skb ? chunk->skb->len : -1);
1da177e4
LT
186
187 switch ((retval = (sctp_packet_append_chunk(packet, chunk)))) {
188 case SCTP_XMIT_PMTU_FULL:
189 if (!packet->has_cookie_echo) {
64519440
XL
190 int error = 0;
191
cea8768f 192 error = sctp_packet_transmit(packet, gfp);
1da177e4
LT
193 if (error < 0)
194 chunk->skb->sk->sk_err = -error;
195
196 /* If we have an empty packet, then we can NOT ever
197 * return PMTU_FULL.
198 */
2e3216cd
VY
199 if (!one_packet)
200 retval = sctp_packet_append_chunk(packet,
201 chunk);
1da177e4
LT
202 }
203 break;
204
205 case SCTP_XMIT_RWND_FULL:
206 case SCTP_XMIT_OK:
526cbef7 207 case SCTP_XMIT_DELAY:
1da177e4 208 break;
3ff50b79 209 }
1da177e4
LT
210
211 return retval;
212}
213
4cd57c80
VY
214/* Try to bundle an auth chunk into the packet. */
215static sctp_xmit_t sctp_packet_bundle_auth(struct sctp_packet *pkt,
216 struct sctp_chunk *chunk)
217{
218 struct sctp_association *asoc = pkt->transport->asoc;
219 struct sctp_chunk *auth;
220 sctp_xmit_t retval = SCTP_XMIT_OK;
221
222 /* if we don't have an association, we can't do authentication */
223 if (!asoc)
224 return retval;
225
226 /* See if this is an auth chunk we are bundling or if
227 * auth is already bundled.
228 */
4007cc88 229 if (chunk->chunk_hdr->type == SCTP_CID_AUTH || pkt->has_auth)
4cd57c80
VY
230 return retval;
231
232 /* if the peer did not request this chunk to be authenticated,
233 * don't do it
234 */
235 if (!chunk->auth)
236 return retval;
237
238 auth = sctp_make_auth(asoc);
239 if (!auth)
240 return retval;
241
ed106277
NH
242 retval = __sctp_packet_append_chunk(pkt, auth);
243
244 if (retval != SCTP_XMIT_OK)
245 sctp_chunk_free(auth);
4cd57c80
VY
246
247 return retval;
248}
249
1da177e4
LT
250/* Try to bundle a SACK with the packet. */
251static sctp_xmit_t sctp_packet_bundle_sack(struct sctp_packet *pkt,
252 struct sctp_chunk *chunk)
253{
254 sctp_xmit_t retval = SCTP_XMIT_OK;
255
256 /* If sending DATA and haven't aleady bundled a SACK, try to
257 * bundle one in to the packet.
258 */
259 if (sctp_chunk_is_data(chunk) && !pkt->has_sack &&
260 !pkt->has_cookie_echo) {
261 struct sctp_association *asoc;
af87b823 262 struct timer_list *timer;
1da177e4 263 asoc = pkt->transport->asoc;
af87b823 264 timer = &asoc->timers[SCTP_EVENT_TIMEOUT_SACK];
1da177e4 265
af87b823
DG
266 /* If the SACK timer is running, we have a pending SACK */
267 if (timer_pending(timer)) {
1da177e4 268 struct sctp_chunk *sack;
4244854d
NH
269
270 if (pkt->transport->sack_generation !=
271 pkt->transport->asoc->peer.sack_generation)
272 return retval;
273
1da177e4
LT
274 asoc->a_rwnd = asoc->rwnd;
275 sack = sctp_make_sack(asoc);
276 if (sack) {
ed106277
NH
277 retval = __sctp_packet_append_chunk(pkt, sack);
278 if (retval != SCTP_XMIT_OK) {
279 sctp_chunk_free(sack);
280 goto out;
281 }
1da177e4 282 asoc->peer.sack_needed = 0;
af87b823 283 if (del_timer(timer))
1da177e4
LT
284 sctp_association_put(asoc);
285 }
286 }
287 }
ed106277 288out:
1da177e4
LT
289 return retval;
290}
291
ed106277 292
1da177e4
LT
293/* Append a chunk to the offered packet reporting back any inability to do
294 * so.
295 */
ed106277
NH
296static sctp_xmit_t __sctp_packet_append_chunk(struct sctp_packet *packet,
297 struct sctp_chunk *chunk)
1da177e4
LT
298{
299 sctp_xmit_t retval = SCTP_XMIT_OK;
e2f036a9 300 __u16 chunk_len = SCTP_PAD4(ntohs(chunk->chunk_hdr->length));
1da177e4 301
e83963b7
VY
302 /* Check to see if this chunk will fit into the packet */
303 retval = sctp_packet_will_fit(packet, chunk, chunk_len);
304 if (retval != SCTP_XMIT_OK)
305 goto finish;
1da177e4 306
e83963b7 307 /* We believe that this chunk is OK to add to the packet */
4cd57c80 308 switch (chunk->chunk_hdr->type) {
f7010e61 309 case SCTP_CID_DATA:
e83963b7
VY
310 /* Account for the data being in the packet */
311 sctp_packet_append_data(packet, chunk);
1da177e4
LT
312 /* Disallow SACK bundling after DATA. */
313 packet->has_sack = 1;
4cd57c80
VY
314 /* Disallow AUTH bundling after DATA */
315 packet->has_auth = 1;
316 /* Let it be knows that packet has DATA in it */
317 packet->has_data = 1;
759af00e
VY
318 /* timestamp the chunk for rtx purposes */
319 chunk->sent_at = jiffies;
a6c2f792
XL
320 /* Mainly used for prsctp RTX policy */
321 chunk->sent_count++;
4cd57c80 322 break;
f7010e61 323 case SCTP_CID_COOKIE_ECHO:
1da177e4 324 packet->has_cookie_echo = 1;
4cd57c80
VY
325 break;
326
f7010e61 327 case SCTP_CID_SACK:
1da177e4 328 packet->has_sack = 1;
196d6759
MB
329 if (chunk->asoc)
330 chunk->asoc->stats.osacks++;
4cd57c80
VY
331 break;
332
f7010e61 333 case SCTP_CID_AUTH:
4cd57c80
VY
334 packet->has_auth = 1;
335 packet->auth = chunk;
336 break;
337 }
1da177e4
LT
338
339 /* It is OK to send this chunk. */
79af02c2 340 list_add_tail(&chunk->list, &packet->chunk_list);
1da177e4
LT
341 packet->size += chunk_len;
342 chunk->transport = packet->transport;
343finish:
344 return retval;
345}
346
ed106277
NH
347/* Append a chunk to the offered packet reporting back any inability to do
348 * so.
349 */
350sctp_xmit_t sctp_packet_append_chunk(struct sctp_packet *packet,
351 struct sctp_chunk *chunk)
352{
353 sctp_xmit_t retval = SCTP_XMIT_OK;
354
bb33381d 355 pr_debug("%s: packet:%p chunk:%p\n", __func__, packet, chunk);
ed106277
NH
356
357 /* Data chunks are special. Before seeing what else we can
358 * bundle into this packet, check to see if we are allowed to
359 * send this DATA.
360 */
361 if (sctp_chunk_is_data(chunk)) {
362 retval = sctp_packet_can_append_data(packet, chunk);
363 if (retval != SCTP_XMIT_OK)
364 goto finish;
365 }
366
367 /* Try to bundle AUTH chunk */
368 retval = sctp_packet_bundle_auth(packet, chunk);
369 if (retval != SCTP_XMIT_OK)
370 goto finish;
371
372 /* Try to bundle SACK chunk */
373 retval = sctp_packet_bundle_sack(packet, chunk);
374 if (retval != SCTP_XMIT_OK)
375 goto finish;
376
377 retval = __sctp_packet_append_chunk(packet, chunk);
378
379finish:
380 return retval;
381}
382
4c3a5bda
TG
383static void sctp_packet_release_owner(struct sk_buff *skb)
384{
385 sk_free(skb->sk);
386}
387
388static void sctp_packet_set_owner_w(struct sk_buff *skb, struct sock *sk)
389{
390 skb_orphan(skb);
391 skb->sk = sk;
392 skb->destructor = sctp_packet_release_owner;
393
394 /*
395 * The data chunks have already been accounted for in sctp_sendmsg(),
396 * therefore only reserve a single byte to keep socket around until
397 * the packet has been transmitted.
398 */
399 atomic_inc(&sk->sk_wmem_alloc);
400}
401
1da177e4
LT
402/* All packets are sent to the network through this function from
403 * sctp_outq_tail().
404 *
405 * The return value is a normal kernel error return value.
406 */
cea8768f 407int sctp_packet_transmit(struct sctp_packet *packet, gfp_t gfp)
1da177e4
LT
408{
409 struct sctp_transport *tp = packet->transport;
410 struct sctp_association *asoc = tp->asoc;
411 struct sctphdr *sh;
90017acc 412 struct sk_buff *nskb = NULL, *head = NULL;
79af02c2 413 struct sctp_chunk *chunk, *tmp;
1da177e4
LT
414 struct sock *sk;
415 int err = 0;
416 int padding; /* How much padding do we need? */
90017acc 417 int pkt_size;
1da177e4 418 __u8 has_data = 0;
90017acc
MRL
419 int gso = 0;
420 int pktcount = 0;
0438816e 421 struct dst_entry *dst;
4cd57c80 422 unsigned char *auth = NULL; /* pointer to auth in skb data */
1da177e4 423
bb33381d 424 pr_debug("%s: packet:%p\n", __func__, packet);
1da177e4
LT
425
426 /* Do NOT generate a chunkless packet. */
79af02c2 427 if (list_empty(&packet->chunk_list))
1da177e4
LT
428 return err;
429
430 /* Set up convenience variables... */
79af02c2 431 chunk = list_entry(packet->chunk_list.next, struct sctp_chunk, list);
1da177e4
LT
432 sk = chunk->skb->sk;
433
90017acc
MRL
434 /* Allocate the head skb, or main one if not in GSO */
435 if (packet->size > tp->pathmtu && !packet->ipfragok) {
436 if (sk_can_gso(sk)) {
437 gso = 1;
438 pkt_size = packet->overhead;
439 } else {
440 /* If this happens, we trash this packet and try
441 * to build a new one, hopefully correct this
442 * time. Application may notice this error.
443 */
444 pr_err_once("Trying to GSO but underlying device doesn't support it.");
41001faf 445 goto err;
90017acc
MRL
446 }
447 } else {
448 pkt_size = packet->size;
449 }
450 head = alloc_skb(pkt_size + MAX_HEADER, gfp);
451 if (!head)
41001faf 452 goto err;
90017acc
MRL
453 if (gso) {
454 NAPI_GRO_CB(head)->last = head;
455 skb_shinfo(head)->gso_type = sk->sk_gso_type;
456 }
1da177e4
LT
457
458 /* Make sure the outbound skb has enough header room reserved. */
90017acc 459 skb_reserve(head, packet->overhead + MAX_HEADER);
1da177e4
LT
460
461 /* Set the owning socket so that we know where to get the
462 * destination IP address.
463 */
90017acc 464 sctp_packet_set_owner_w(head, sk);
1da177e4 465
e0268868 466 if (!sctp_transport_dst_check(tp)) {
503b55fd
SS
467 sctp_transport_route(tp, NULL, sctp_sk(sk));
468 if (asoc && (asoc->param_flags & SPP_PMTUD_ENABLE)) {
02f3d4ce 469 sctp_assoc_sync_pmtu(sk, asoc);
503b55fd
SS
470 }
471 }
adf30907 472 dst = dst_clone(tp->dst);
41001faf
XL
473 if (!dst) {
474 if (asoc)
475 IP_INC_STATS(sock_net(asoc->base.sk),
476 IPSTATS_MIB_OUTNOROUTES);
477 goto nodst;
478 }
90017acc 479 skb_dst_set(head, dst);
503b55fd 480
1da177e4 481 /* Build the SCTP header. */
90017acc
MRL
482 sh = (struct sctphdr *)skb_push(head, sizeof(struct sctphdr));
483 skb_reset_transport_header(head);
1da177e4
LT
484 sh->source = htons(packet->source_port);
485 sh->dest = htons(packet->destination_port);
486
487 /* From 6.8 Adler-32 Checksum Calculation:
488 * After the packet is constructed (containing the SCTP common
489 * header and one or more control or DATA chunks), the
490 * transmitter shall:
491 *
492 * 1) Fill in the proper Verification Tag in the SCTP common
493 * header and initialize the checksum field to 0's.
494 */
495 sh->vtag = htonl(packet->vtag);
496 sh->checksum = 0;
497
bb33381d
DB
498 pr_debug("***sctp_transmit_packet***\n");
499
90017acc
MRL
500 do {
501 /* Set up convenience variables... */
502 chunk = list_entry(packet->chunk_list.next, struct sctp_chunk, list);
503 pktcount++;
1da177e4 504
90017acc
MRL
505 /* Calculate packet size, so it fits in PMTU. Leave
506 * other chunks for the next packets.
507 */
508 if (gso) {
509 pkt_size = packet->overhead;
510 list_for_each_entry(chunk, &packet->chunk_list, list) {
e2f036a9 511 int padded = SCTP_PAD4(chunk->skb->len);
90017acc
MRL
512
513 if (pkt_size + padded > tp->pathmtu)
514 break;
515 pkt_size += padded;
d8dd1578 516 }
6eabca54 517
90017acc
MRL
518 /* Allocate a new skb. */
519 nskb = alloc_skb(pkt_size + MAX_HEADER, gfp);
520 if (!nskb)
521 goto nomem;
1da177e4 522
90017acc
MRL
523 /* Make sure the outbound skb has enough header
524 * room reserved.
525 */
526 skb_reserve(nskb, packet->overhead + MAX_HEADER);
527 } else {
528 nskb = head;
529 }
1da177e4 530
90017acc
MRL
531 /**
532 * 3.2 Chunk Field Descriptions
533 *
534 * The total length of a chunk (including Type, Length and
535 * Value fields) MUST be a multiple of 4 bytes. If the length
536 * of the chunk is not a multiple of 4 bytes, the sender MUST
537 * pad the chunk with all zero bytes and this padding is not
538 * included in the chunk length field. The sender should
539 * never pad with more than 3 bytes.
540 *
e2f036a9 541 * [This whole comment explains SCTP_PAD4() below.]
4cd57c80 542 */
4cd57c80 543
90017acc
MRL
544 pkt_size -= packet->overhead;
545 list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
546 list_del_init(&chunk->list);
547 if (sctp_chunk_is_data(chunk)) {
548 /* 6.3.1 C4) When data is in flight and when allowed
549 * by rule C5, a new RTT measurement MUST be made each
550 * round trip. Furthermore, new RTT measurements
551 * SHOULD be made no more than once per round-trip
552 * for a given destination transport address.
553 */
554
555 if (!chunk->resent && !tp->rto_pending) {
556 chunk->rtt_in_progress = 1;
557 tp->rto_pending = 1;
558 }
559
560 has_data = 1;
561 }
562
e2f036a9 563 padding = SCTP_PAD4(chunk->skb->len) - chunk->skb->len;
90017acc
MRL
564 if (padding)
565 memset(skb_put(chunk->skb, padding), 0, padding);
566
567 /* if this is the auth chunk that we are adding,
568 * store pointer where it will be added and put
569 * the auth into the packet.
570 */
571 if (chunk == packet->auth)
572 auth = skb_tail_pointer(nskb);
573
574 memcpy(skb_put(nskb, chunk->skb->len),
503b55fd 575 chunk->skb->data, chunk->skb->len);
1da177e4 576
90017acc
MRL
577 pr_debug("*** Chunk:%p[%s] %s 0x%x, length:%d, chunk->skb->len:%d, rtt_in_progress:%d\n",
578 chunk,
579 sctp_cname(SCTP_ST_CHUNK(chunk->chunk_hdr->type)),
580 chunk->has_tsn ? "TSN" : "No TSN",
581 chunk->has_tsn ? ntohl(chunk->subh.data_hdr->tsn) : 0,
582 ntohs(chunk->chunk_hdr->length), chunk->skb->len,
583 chunk->rtt_in_progress);
584
585 /* If this is a control chunk, this is our last
586 * reference. Free data chunks after they've been
587 * acknowledged or have failed.
588 * Re-queue auth chunks if needed.
589 */
e2f036a9 590 pkt_size -= SCTP_PAD4(chunk->skb->len);
1da177e4 591
f1533cce 592 if (!sctp_chunk_is_data(chunk) && chunk != packet->auth)
90017acc
MRL
593 sctp_chunk_free(chunk);
594
595 if (!pkt_size)
596 break;
597 }
598
599 /* SCTP-AUTH, Section 6.2
600 * The sender MUST calculate the MAC as described in RFC2104 [2]
601 * using the hash function H as described by the MAC Identifier and
602 * the shared association key K based on the endpoint pair shared key
603 * described by the shared key identifier. The 'data' used for the
604 * computation of the AUTH-chunk is given by the AUTH chunk with its
605 * HMAC field set to zero (as shown in Figure 6) followed by all
606 * chunks that are placed after the AUTH chunk in the SCTP packet.
607 */
608 if (auth)
609 sctp_auth_calculate_hmac(asoc, nskb,
610 (struct sctp_auth_chunk *)auth,
611 gfp);
612
f1533cce
MRL
613 if (packet->auth) {
614 if (!list_empty(&packet->chunk_list)) {
615 /* We will generate more packets, so re-queue
616 * auth chunk.
617 */
1aa25ec2
XL
618 list_add(&packet->auth->list,
619 &packet->chunk_list);
f1533cce
MRL
620 } else {
621 sctp_chunk_free(packet->auth);
622 packet->auth = NULL;
623 }
624 }
625
90017acc
MRL
626 if (!gso)
627 break;
628
41001faf
XL
629 if (skb_gro_receive(&head, nskb)) {
630 kfree_skb(nskb);
90017acc 631 goto nomem;
41001faf 632 }
90017acc
MRL
633 nskb = NULL;
634 if (WARN_ON_ONCE(skb_shinfo(head)->gso_segs >=
635 sk->sk_gso_max_segs))
636 goto nomem;
637 } while (!list_empty(&packet->chunk_list));
4cd57c80
VY
638
639 /* 2) Calculate the Adler-32 checksum of the whole packet,
640 * including the SCTP common header and all the
641 * chunks.
642 *
643 * Note: Adler-32 is no longer applicable, as has been replaced
644 * by CRC32-C as described in <draft-ietf-tsvwg-sctpcsum-02.txt>.
90017acc
MRL
645 *
646 * If it's a GSO packet, it's postponed to sctp_skb_segment.
4cd57c80 647 */
90017acc
MRL
648 if (!sctp_checksum_disable || gso) {
649 if (!gso && (!(dst->dev->features & NETIF_F_SCTP_CRC) ||
650 dst_xfrm(dst) || packet->ipfragok)) {
651 sh->checksum = sctp_compute_cksum(head, 0);
b73c43f8 652 } else {
25985edc 653 /* no need to seed pseudo checksum for SCTP */
90017acc
MRL
654 head->ip_summed = CHECKSUM_PARTIAL;
655 head->csum_start = skb_transport_header(head) - head->head;
656 head->csum_offset = offsetof(struct sctphdr, checksum);
8dc92f7e
JB
657 }
658 }
1da177e4 659
1da177e4
LT
660 /* IP layer ECN support
661 * From RFC 2481
662 * "The ECN-Capable Transport (ECT) bit would be set by the
663 * data sender to indicate that the end-points of the
664 * transport protocol are ECN-capable."
665 *
666 * Now setting the ECT bit all the time, as it should not cause
667 * any problems protocol-wise even if our peer ignores it.
668 *
669 * Note: The works for IPv6 layer checks this bit too later
670 * in transmission. See IP6_ECN_flow_xmit().
671 */
90017acc 672 tp->af_specific->ecn_capable(sk);
1da177e4
LT
673
674 /* Set up the IP options. */
675 /* BUG: not implemented
676 * For v4 this all lives somewhere in sk->sk_opt...
677 */
678
679 /* Dump that on IP! */
196d6759 680 if (asoc) {
90017acc 681 asoc->stats.opackets += pktcount;
196d6759
MB
682 if (asoc->peer.last_sent_to != tp)
683 /* Considering the multiple CPU scenario, this is a
684 * "correcter" place for last_sent_to. --xguo
685 */
686 asoc->peer.last_sent_to = tp;
1da177e4
LT
687 }
688
689 if (has_data) {
690 struct timer_list *timer;
691 unsigned long timeout;
692
1da177e4 693 /* Restart the AUTOCLOSE timer when sending data. */
9f70f46b
NH
694 if (sctp_state(asoc, ESTABLISHED) &&
695 asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
1da177e4
LT
696 timer = &asoc->timers[SCTP_EVENT_TIMEOUT_AUTOCLOSE];
697 timeout = asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE];
698
699 if (!mod_timer(timer, jiffies + timeout))
700 sctp_association_hold(asoc);
701 }
702 }
703
90017acc
MRL
704 pr_debug("***sctp_transmit_packet*** skb->len:%d\n", head->len);
705
706 if (gso) {
707 /* Cleanup our debris for IP stacks */
708 memset(head->cb, 0, max(sizeof(struct inet_skb_parm),
709 sizeof(struct inet6_skb_parm)));
1da177e4 710
90017acc
MRL
711 skb_shinfo(head)->gso_segs = pktcount;
712 skb_shinfo(head)->gso_size = GSO_BY_FRAGS;
713
714 /* We have to refresh this in case we are xmiting to
715 * more than one transport at a time
716 */
717 rcu_read_lock();
718 if (__sk_dst_get(sk) != tp->dst) {
719 dst_hold(tp->dst);
720 sk_setup_caps(sk, tp->dst);
721 }
722 rcu_read_unlock();
723 }
724 head->ignore_df = packet->ipfragok;
725 tp->af_specific->sctp_xmit(head, tp);
41001faf 726 goto out;
1da177e4 727
41001faf
XL
728nomem:
729 if (packet->auth && list_empty(&packet->auth->list))
730 sctp_chunk_free(packet->auth);
1da177e4 731
41001faf 732nodst:
1da177e4
LT
733 /* FIXME: Returning the 'err' will effect all the associations
734 * associated with a socket, although only one of the paths of the
735 * association is unreachable.
736 * The real failure of a transport or association can be passed on
737 * to the user via notifications. So setting this error may not be
738 * required.
739 */
740 /* err = -EHOSTUNREACH; */
41001faf 741 kfree_skb(head);
1da177e4 742
41001faf 743err:
79af02c2
DM
744 list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
745 list_del_init(&chunk->list);
1da177e4 746 if (!sctp_chunk_is_data(chunk))
d808ad9a 747 sctp_chunk_free(chunk);
1da177e4 748 }
41001faf
XL
749
750out:
751 sctp_packet_reset(packet);
752 return err;
1da177e4
LT
753}
754
755/********************************************************************
756 * 2nd Level Abstractions
757 ********************************************************************/
758
e83963b7
VY
759/* This private function check to see if a chunk can be added */
760static sctp_xmit_t sctp_packet_can_append_data(struct sctp_packet *packet,
1da177e4
LT
761 struct sctp_chunk *chunk)
762{
e83963b7 763 size_t datasize, rwnd, inflight, flight_size;
1da177e4 764 struct sctp_transport *transport = packet->transport;
1da177e4 765 struct sctp_association *asoc = transport->asoc;
1da177e4
LT
766 struct sctp_outq *q = &asoc->outqueue;
767
768 /* RFC 2960 6.1 Transmission of DATA Chunks
769 *
770 * A) At any given time, the data sender MUST NOT transmit new data to
771 * any destination transport address if its peer's rwnd indicates
772 * that the peer has no buffer space (i.e. rwnd is 0, see Section
773 * 6.2.1). However, regardless of the value of rwnd (including if it
774 * is 0), the data sender can always have one DATA chunk in flight to
775 * the receiver if allowed by cwnd (see rule B below). This rule
776 * allows the sender to probe for a change in rwnd that the sender
777 * missed due to the SACK having been lost in transit from the data
778 * receiver to the data sender.
779 */
780
781 rwnd = asoc->peer.rwnd;
e83963b7
VY
782 inflight = q->outstanding_bytes;
783 flight_size = transport->flight_size;
1da177e4
LT
784
785 datasize = sctp_data_size(chunk);
786
723189fa
DL
787 if (datasize > rwnd && inflight > 0)
788 /* We have (at least) one data chunk in flight,
789 * so we can't fall back to rule 6.1 B).
790 */
791 return SCTP_XMIT_RWND_FULL;
1da177e4 792
1da177e4
LT
793 /* RFC 2960 6.1 Transmission of DATA Chunks
794 *
795 * B) At any given time, the sender MUST NOT transmit new data
796 * to a given transport address if it has cwnd or more bytes
797 * of data outstanding to that transport address.
798 */
799 /* RFC 7.2.4 & the Implementers Guide 2.8.
800 *
801 * 3) ...
802 * When a Fast Retransmit is being performed the sender SHOULD
803 * ignore the value of cwnd and SHOULD NOT delay retransmission.
804 */
723189fa
DL
805 if (chunk->fast_retransmit != SCTP_NEED_FRTX &&
806 flight_size >= transport->cwnd)
807 return SCTP_XMIT_RWND_FULL;
1da177e4
LT
808
809 /* Nagle's algorithm to solve small-packet problem:
810 * Inhibit the sending of new chunks when new outgoing data arrives
811 * if any previously transmitted data on the connection remains
812 * unacknowledged.
813 */
1da177e4 814
723189fa
DL
815 if (sctp_sk(asoc->base.sk)->nodelay)
816 /* Nagle disabled */
817 return SCTP_XMIT_OK;
818
819 if (!sctp_packet_empty(packet))
820 /* Append to packet */
821 return SCTP_XMIT_OK;
822
823 if (inflight == 0)
824 /* Nothing unacked */
825 return SCTP_XMIT_OK;
826
827 if (!sctp_state(asoc, ESTABLISHED))
828 return SCTP_XMIT_OK;
829
830 /* Check whether this chunk and all the rest of pending data will fit
831 * or delay in hopes of bundling a full sized packet.
832 */
e43569e6
MRL
833 if (chunk->skb->len + q->out_qlen >
834 transport->pathmtu - packet->overhead - sizeof(sctp_data_chunk_t) - 4)
723189fa
DL
835 /* Enough data queued to fill a packet */
836 return SCTP_XMIT_OK;
837
838 /* Don't delay large message writes that may have been fragmented */
839 if (!chunk->msg->can_delay)
840 return SCTP_XMIT_OK;
841
842 /* Defer until all data acked or packet full */
526cbef7 843 return SCTP_XMIT_DELAY;
e83963b7
VY
844}
845
846/* This private function does management things when adding DATA chunk */
847static void sctp_packet_append_data(struct sctp_packet *packet,
848 struct sctp_chunk *chunk)
849{
850 struct sctp_transport *transport = packet->transport;
851 size_t datasize = sctp_data_size(chunk);
852 struct sctp_association *asoc = transport->asoc;
853 u32 rwnd = asoc->peer.rwnd;
854
1da177e4
LT
855 /* Keep track of how many bytes are in flight over this transport. */
856 transport->flight_size += datasize;
857
858 /* Keep track of how many bytes are in flight to the receiver. */
859 asoc->outqueue.outstanding_bytes += datasize;
860
a76c0adf 861 /* Update our view of the receiver's rwnd. */
1da177e4
LT
862 if (datasize < rwnd)
863 rwnd -= datasize;
864 else
865 rwnd = 0;
866
867 asoc->peer.rwnd = rwnd;
868 /* Has been accepted for transmission. */
869 if (!asoc->peer.prsctp_capable)
870 chunk->msg->can_abandon = 0;
d8dd1578
NH
871 sctp_chunk_assign_tsn(chunk);
872 sctp_chunk_assign_ssn(chunk);
e83963b7
VY
873}
874
875static sctp_xmit_t sctp_packet_will_fit(struct sctp_packet *packet,
876 struct sctp_chunk *chunk,
877 u16 chunk_len)
878{
7303a147 879 size_t psize, pmtu, maxsize;
e83963b7
VY
880 sctp_xmit_t retval = SCTP_XMIT_OK;
881
882 psize = packet->size;
90017acc
MRL
883 if (packet->transport->asoc)
884 pmtu = packet->transport->asoc->pathmtu;
885 else
886 pmtu = packet->transport->pathmtu;
e83963b7
VY
887
888 /* Decide if we need to fragment or resubmit later. */
90017acc
MRL
889 if (psize + chunk_len > pmtu) {
890 /* It's OK to fragment at IP level if any one of the following
e83963b7 891 * is true:
90017acc
MRL
892 * 1. The packet is empty (meaning this chunk is greater
893 * the MTU)
894 * 2. The packet doesn't have any data in it yet and data
895 * requires authentication.
e83963b7 896 */
90017acc 897 if (sctp_packet_empty(packet) ||
e83963b7
VY
898 (!packet->has_data && chunk->auth)) {
899 /* We no longer do re-fragmentation.
900 * Just fragment at the IP layer, if we
901 * actually hit this condition
902 */
903 packet->ipfragok = 1;
90017acc 904 goto out;
e83963b7 905 }
90017acc 906
7303a147
MRL
907 /* Similarly, if this chunk was built before a PMTU
908 * reduction, we have to fragment it at IP level now. So
909 * if the packet already contains something, we need to
910 * flush.
911 */
912 maxsize = pmtu - packet->overhead;
913 if (packet->auth)
e2f036a9 914 maxsize -= SCTP_PAD4(packet->auth->skb->len);
7303a147
MRL
915 if (chunk_len > maxsize)
916 retval = SCTP_XMIT_PMTU_FULL;
917
90017acc
MRL
918 /* It is also okay to fragment if the chunk we are
919 * adding is a control chunk, but only if current packet
920 * is not a GSO one otherwise it causes fragmentation of
921 * a large frame. So in this case we allow the
922 * fragmentation by forcing it to be in a new packet.
923 */
924 if (!sctp_chunk_is_data(chunk) && packet->has_data)
925 retval = SCTP_XMIT_PMTU_FULL;
926
927 if (psize + chunk_len > packet->max_size)
928 /* Hit GSO/PMTU limit, gotta flush */
929 retval = SCTP_XMIT_PMTU_FULL;
930
931 if (!packet->transport->burst_limited &&
932 psize + chunk_len > (packet->transport->cwnd >> 1))
933 /* Do not allow a single GSO packet to use more
934 * than half of cwnd.
935 */
936 retval = SCTP_XMIT_PMTU_FULL;
937
938 if (packet->transport->burst_limited &&
939 psize + chunk_len > (packet->transport->burst_limited >> 1))
940 /* Do not allow a single GSO packet to use more
941 * than half of original cwnd.
942 */
943 retval = SCTP_XMIT_PMTU_FULL;
944 /* Otherwise it will fit in the GSO packet */
e83963b7 945 }
1da177e4 946
90017acc 947out:
1da177e4
LT
948 return retval;
949}