blob: f2597a9eff74a0a4eacd5eecb06675890cf4a955 [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
Jeff Kirsher4b2f13a2013-12-06 06:28:48 -080023 * along with GNU CC; see the file COPYING. If not, see
24 * <http://www.gnu.org/licenses/>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 *
26 * Please send any bug reports or fixes you make to the
27 * email address(es):
Daniel Borkmann91705c62013-07-23 14:51:47 +020028 * lksctp developers <linux-sctp@vger.kernel.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 */
36
Joe Perches145ce502010-08-24 13:21:08 +000037#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090046#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <net/inet_ecn.h>
Vlad Yasevich8d2f9e812009-03-21 13:41:09 -070048#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <net/icmp.h>
Pavel Emelyanov7c73a6f2008-07-16 20:20:11 -070050#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#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>
Vlad Yasevich9ad09772007-12-16 14:06:41 -080057#include <net/sctp/checksum.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59/* Forward declarations for private helpers. */
Neil Hormaned106272012-07-02 09:59:24 +000060static sctp_xmit_t __sctp_packet_append_chunk(struct sctp_packet *packet,
61 struct sctp_chunk *chunk);
Vlad Yaseviche83963b2009-08-07 10:43:07 -040062static sctp_xmit_t sctp_packet_can_append_data(struct sctp_packet *packet,
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 struct sctp_chunk *chunk);
Vlad Yaseviche83963b2009-08-07 10:43:07 -040064static 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Wei Yongjunbe297142009-09-04 14:34:06 +080070static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/* 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{
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -030087 struct sctp_transport *tp = packet->transport;
88 struct sctp_association *asoc = tp->asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Daniel Borkmannbb333812013-06-28 19:49:40 +020090 pr_debug("%s: packet:%p vtag:0x%x\n", __func__, packet, vtag);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 packet->vtag = vtag;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -030094 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 if (ecn_capable && sctp_packet_empty(packet)) {
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300117 struct sctp_chunk *chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 /* If there a is a prepend chunk stick it on the list before
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900120 * any other chunks get appended.
121 */
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300122 chunk = sctp_get_ecne_prepend(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 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
Daniel Borkmannbb333812013-06-28 19:49:40 +0200138 pr_debug("%s: packet:%p transport:%p\n", __func__, packet, transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 packet->transport = transport;
141 packet->source_port = sport;
142 packet->destination_port = dport;
David S. Miller79af02c2005-07-08 21:47:49 -0700143 INIT_LIST_HEAD(&packet->chunk_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 if (asoc) {
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900145 struct sctp_sock *sp = sctp_sk(asoc->base.sk);
146 overhead = sp->pf->af->net_header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 } else {
148 overhead = sizeof(struct ipv6hdr);
149 }
150 overhead += sizeof(struct sctphdr);
151 packet->overhead = overhead;
Wei Yongjunbe297142009-09-04 14:34:06 +0800152 sctp_packet_reset(packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 packet->vtag = 0;
Daniel Borkmann3e3251b2013-04-18 21:59:37 +0000154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return packet;
156}
157
158/* Free a packet. */
159void sctp_packet_free(struct sctp_packet *packet)
160{
David S. Miller79af02c2005-07-08 21:47:49 -0700161 struct sctp_chunk *chunk, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Daniel Borkmannbb333812013-06-28 19:49:40 +0200163 pr_debug("%s: packet:%p\n", __func__, packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
David S. Miller79af02c2005-07-08 21:47:49 -0700165 list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
166 list_del_init(&chunk->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 sctp_chunk_free(chunk);
David S. Miller79af02c2005-07-08 21:47:49 -0700168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
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,
Vlad Yasevich2e3216c2008-06-19 16:08:18 -0700179 struct sctp_chunk *chunk,
Marcelo Ricardo Leitnercea87682016-03-10 18:33:07 -0300180 int one_packet, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
182 sctp_xmit_t retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
David S. Miller3b55a532016-06-03 22:53:26 -0700184 pr_debug("%s: packet:%p size:%Zu chunk:%p size:%d\n", __func__,
Marcelo Ricardo Leitner942b3232016-06-02 15:05:44 -0300185 packet, packet->size, chunk, chunk->skb ? chunk->skb->len : -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 switch ((retval = (sctp_packet_append_chunk(packet, chunk)))) {
188 case SCTP_XMIT_PMTU_FULL:
189 if (!packet->has_cookie_echo) {
Xin Long64519442016-09-14 02:04:21 +0800190 int error = 0;
191
Marcelo Ricardo Leitnercea87682016-03-10 18:33:07 -0300192 error = sctp_packet_transmit(packet, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 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 */
Vlad Yasevich2e3216c2008-06-19 16:08:18 -0700199 if (!one_packet)
200 retval = sctp_packet_append_chunk(packet,
201 chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
203 break;
204
205 case SCTP_XMIT_RWND_FULL:
206 case SCTP_XMIT_OK:
David Laight526cbef2014-07-22 08:59:14 +0000207 case SCTP_XMIT_DELAY:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 return retval;
212}
213
Vlad Yasevich4cd57c82007-09-16 19:32:45 -0700214/* 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 */
Vlad Yasevich4007cc82009-09-04 18:21:00 -0400229 if (chunk->chunk_hdr->type == SCTP_CID_AUTH || pkt->has_auth)
Vlad Yasevich4cd57c82007-09-16 19:32:45 -0700230 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
Neil Hormaned106272012-07-02 09:59:24 +0000242 retval = __sctp_packet_append_chunk(pkt, auth);
243
244 if (retval != SCTP_XMIT_OK)
245 sctp_chunk_free(auth);
Vlad Yasevich4cd57c82007-09-16 19:32:45 -0700246
247 return retval;
248}
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250/* 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;
Doug Grahamaf87b822009-07-29 12:05:57 -0400262 struct timer_list *timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 asoc = pkt->transport->asoc;
Doug Grahamaf87b822009-07-29 12:05:57 -0400264 timer = &asoc->timers[SCTP_EVENT_TIMEOUT_SACK];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Doug Grahamaf87b822009-07-29 12:05:57 -0400266 /* If the SACK timer is running, we have a pending SACK */
267 if (timer_pending(timer)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 struct sctp_chunk *sack;
Neil Horman42448542012-06-30 03:04:26 +0000269
270 if (pkt->transport->sack_generation !=
271 pkt->transport->asoc->peer.sack_generation)
272 return retval;
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 asoc->a_rwnd = asoc->rwnd;
275 sack = sctp_make_sack(asoc);
276 if (sack) {
Neil Hormaned106272012-07-02 09:59:24 +0000277 retval = __sctp_packet_append_chunk(pkt, sack);
278 if (retval != SCTP_XMIT_OK) {
279 sctp_chunk_free(sack);
280 goto out;
281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 asoc->peer.sack_needed = 0;
Doug Grahamaf87b822009-07-29 12:05:57 -0400283 if (del_timer(timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 sctp_association_put(asoc);
285 }
286 }
287 }
Neil Hormaned106272012-07-02 09:59:24 +0000288out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return retval;
290}
291
Neil Hormaned106272012-07-02 09:59:24 +0000292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293/* Append a chunk to the offered packet reporting back any inability to do
294 * so.
295 */
Neil Hormaned106272012-07-02 09:59:24 +0000296static sctp_xmit_t __sctp_packet_append_chunk(struct sctp_packet *packet,
297 struct sctp_chunk *chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 sctp_xmit_t retval = SCTP_XMIT_OK;
300 __u16 chunk_len = WORD_ROUND(ntohs(chunk->chunk_hdr->length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Vlad Yaseviche83963b2009-08-07 10:43:07 -0400302 /* 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Vlad Yaseviche83963b2009-08-07 10:43:07 -0400307 /* We believe that this chunk is OK to add to the packet */
Vlad Yasevich4cd57c82007-09-16 19:32:45 -0700308 switch (chunk->chunk_hdr->type) {
wangweidongf7010e62013-12-23 12:16:52 +0800309 case SCTP_CID_DATA:
Vlad Yaseviche83963b2009-08-07 10:43:07 -0400310 /* Account for the data being in the packet */
311 sctp_packet_append_data(packet, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 /* Disallow SACK bundling after DATA. */
313 packet->has_sack = 1;
Vlad Yasevich4cd57c82007-09-16 19:32:45 -0700314 /* 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;
Vlad Yasevich759af002009-01-22 14:53:01 -0800318 /* timestamp the chunk for rtx purposes */
319 chunk->sent_at = jiffies;
Xin Longa6c2f792016-07-09 19:47:43 +0800320 /* Mainly used for prsctp RTX policy */
321 chunk->sent_count++;
Vlad Yasevich4cd57c82007-09-16 19:32:45 -0700322 break;
wangweidongf7010e62013-12-23 12:16:52 +0800323 case SCTP_CID_COOKIE_ECHO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 packet->has_cookie_echo = 1;
Vlad Yasevich4cd57c82007-09-16 19:32:45 -0700325 break;
326
wangweidongf7010e62013-12-23 12:16:52 +0800327 case SCTP_CID_SACK:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 packet->has_sack = 1;
Michele Baldessari196d6752012-12-01 04:49:42 +0000329 if (chunk->asoc)
330 chunk->asoc->stats.osacks++;
Vlad Yasevich4cd57c82007-09-16 19:32:45 -0700331 break;
332
wangweidongf7010e62013-12-23 12:16:52 +0800333 case SCTP_CID_AUTH:
Vlad Yasevich4cd57c82007-09-16 19:32:45 -0700334 packet->has_auth = 1;
335 packet->auth = chunk;
336 break;
337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 /* It is OK to send this chunk. */
David S. Miller79af02c2005-07-08 21:47:49 -0700340 list_add_tail(&chunk->list, &packet->chunk_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 packet->size += chunk_len;
342 chunk->transport = packet->transport;
343finish:
344 return retval;
345}
346
Neil Hormaned106272012-07-02 09:59:24 +0000347/* 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
Daniel Borkmannbb333812013-06-28 19:49:40 +0200355 pr_debug("%s: packet:%p chunk:%p\n", __func__, packet, chunk);
Neil Hormaned106272012-07-02 09:59:24 +0000356
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
Thomas Graf4c3a5bd2012-09-03 04:27:42 +0000383static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402/* 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 */
Marcelo Ricardo Leitnercea87682016-03-10 18:33:07 -0300407int sctp_packet_transmit(struct sctp_packet *packet, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
409 struct sctp_transport *tp = packet->transport;
410 struct sctp_association *asoc = tp->asoc;
411 struct sctphdr *sh;
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300412 struct sk_buff *nskb = NULL, *head = NULL;
David S. Miller79af02c2005-07-08 21:47:49 -0700413 struct sctp_chunk *chunk, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 struct sock *sk;
415 int err = 0;
416 int padding; /* How much padding do we need? */
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300417 int pkt_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 __u8 has_data = 0;
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300419 int gso = 0;
420 int pktcount = 0;
wangweidong04388162013-12-26 13:55:56 +0800421 struct dst_entry *dst;
Vlad Yasevich4cd57c82007-09-16 19:32:45 -0700422 unsigned char *auth = NULL; /* pointer to auth in skb data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Daniel Borkmannbb333812013-06-28 19:49:40 +0200424 pr_debug("%s: packet:%p\n", __func__, packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 /* Do NOT generate a chunkless packet. */
David S. Miller79af02c2005-07-08 21:47:49 -0700427 if (list_empty(&packet->chunk_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 return err;
429
430 /* Set up convenience variables... */
David S. Miller79af02c2005-07-08 21:47:49 -0700431 chunk = list_entry(packet->chunk_list.next, struct sctp_chunk, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 sk = chunk->skb->sk;
433
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300434 /* 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.");
445 goto nomem;
446 }
447 } else {
448 pkt_size = packet->size;
449 }
450 head = alloc_skb(pkt_size + MAX_HEADER, gfp);
451 if (!head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 goto nomem;
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300453 if (gso) {
454 NAPI_GRO_CB(head)->last = head;
455 skb_shinfo(head)->gso_type = sk->sk_gso_type;
456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 /* Make sure the outbound skb has enough header room reserved. */
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300459 skb_reserve(head, packet->overhead + MAX_HEADER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 /* Set the owning socket so that we know where to get the
462 * destination IP address.
463 */
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300464 sctp_packet_set_owner_w(head, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Nicolas Dichtele0268862012-05-04 05:24:54 +0000466 if (!sctp_transport_dst_check(tp)) {
Sridhar Samudrala503b55f2006-06-17 22:57:28 -0700467 sctp_transport_route(tp, NULL, sctp_sk(sk));
468 if (asoc && (asoc->param_flags & SPP_PMTUD_ENABLE)) {
David S. Miller02f3d4c2012-07-16 03:57:14 -0700469 sctp_assoc_sync_pmtu(sk, asoc);
Sridhar Samudrala503b55f2006-06-17 22:57:28 -0700470 }
471 }
Eric Dumazetadf30902009-06-02 05:19:30 +0000472 dst = dst_clone(tp->dst);
Wei Yongjunff0ac742009-06-28 22:49:37 +0000473 if (!dst)
Sridhar Samudrala503b55f2006-06-17 22:57:28 -0700474 goto no_route;
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300475 skb_dst_set(head, dst);
Sridhar Samudrala503b55f2006-06-17 22:57:28 -0700476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 /* Build the SCTP header. */
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300478 sh = (struct sctphdr *)skb_push(head, sizeof(struct sctphdr));
479 skb_reset_transport_header(head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 sh->source = htons(packet->source_port);
481 sh->dest = htons(packet->destination_port);
482
483 /* From 6.8 Adler-32 Checksum Calculation:
484 * After the packet is constructed (containing the SCTP common
485 * header and one or more control or DATA chunks), the
486 * transmitter shall:
487 *
488 * 1) Fill in the proper Verification Tag in the SCTP common
489 * header and initialize the checksum field to 0's.
490 */
491 sh->vtag = htonl(packet->vtag);
492 sh->checksum = 0;
493
Daniel Borkmannbb333812013-06-28 19:49:40 +0200494 pr_debug("***sctp_transmit_packet***\n");
495
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300496 do {
497 /* Set up convenience variables... */
498 chunk = list_entry(packet->chunk_list.next, struct sctp_chunk, list);
499 pktcount++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300501 /* Calculate packet size, so it fits in PMTU. Leave
502 * other chunks for the next packets.
503 */
504 if (gso) {
505 pkt_size = packet->overhead;
506 list_for_each_entry(chunk, &packet->chunk_list, list) {
507 int padded = WORD_ROUND(chunk->skb->len);
508
509 if (pkt_size + padded > tp->pathmtu)
510 break;
511 pkt_size += padded;
Neil Hormand8dd1572009-11-23 15:54:00 -0500512 }
Xufeng Zhang6eabca52013-11-25 11:26:57 +0800513
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300514 /* Allocate a new skb. */
515 nskb = alloc_skb(pkt_size + MAX_HEADER, gfp);
516 if (!nskb)
517 goto nomem;
518
519 /* Make sure the outbound skb has enough header
520 * room reserved.
521 */
522 skb_reserve(nskb, packet->overhead + MAX_HEADER);
523 } else {
524 nskb = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
526
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300527 /**
528 * 3.2 Chunk Field Descriptions
529 *
530 * The total length of a chunk (including Type, Length and
531 * Value fields) MUST be a multiple of 4 bytes. If the length
532 * of the chunk is not a multiple of 4 bytes, the sender MUST
533 * pad the chunk with all zero bytes and this padding is not
534 * included in the chunk length field. The sender should
535 * never pad with more than 3 bytes.
536 *
537 * [This whole comment explains WORD_ROUND() below.]
Vlad Yasevich4cd57c82007-09-16 19:32:45 -0700538 */
Vlad Yasevich4cd57c82007-09-16 19:32:45 -0700539
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300540 pkt_size -= packet->overhead;
541 list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
542 list_del_init(&chunk->list);
543 if (sctp_chunk_is_data(chunk)) {
544 /* 6.3.1 C4) When data is in flight and when allowed
545 * by rule C5, a new RTT measurement MUST be made each
546 * round trip. Furthermore, new RTT measurements
547 * SHOULD be made no more than once per round-trip
548 * for a given destination transport address.
549 */
550
551 if (!chunk->resent && !tp->rto_pending) {
552 chunk->rtt_in_progress = 1;
553 tp->rto_pending = 1;
554 }
555
556 has_data = 1;
557 }
558
559 padding = WORD_ROUND(chunk->skb->len) - chunk->skb->len;
560 if (padding)
561 memset(skb_put(chunk->skb, padding), 0, padding);
562
563 /* if this is the auth chunk that we are adding,
564 * store pointer where it will be added and put
565 * the auth into the packet.
566 */
567 if (chunk == packet->auth)
568 auth = skb_tail_pointer(nskb);
569
570 memcpy(skb_put(nskb, chunk->skb->len),
Sridhar Samudrala503b55f2006-06-17 22:57:28 -0700571 chunk->skb->data, chunk->skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300573 pr_debug("*** Chunk:%p[%s] %s 0x%x, length:%d, chunk->skb->len:%d, rtt_in_progress:%d\n",
574 chunk,
575 sctp_cname(SCTP_ST_CHUNK(chunk->chunk_hdr->type)),
576 chunk->has_tsn ? "TSN" : "No TSN",
577 chunk->has_tsn ? ntohl(chunk->subh.data_hdr->tsn) : 0,
578 ntohs(chunk->chunk_hdr->length), chunk->skb->len,
579 chunk->rtt_in_progress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300581 /* If this is a control chunk, this is our last
582 * reference. Free data chunks after they've been
583 * acknowledged or have failed.
584 * Re-queue auth chunks if needed.
585 */
586 pkt_size -= WORD_ROUND(chunk->skb->len);
587
Marcelo Ricardo Leitnerf1533cc2016-07-07 09:39:29 -0300588 if (!sctp_chunk_is_data(chunk) && chunk != packet->auth)
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300589 sctp_chunk_free(chunk);
590
591 if (!pkt_size)
592 break;
593 }
594
595 /* SCTP-AUTH, Section 6.2
596 * The sender MUST calculate the MAC as described in RFC2104 [2]
597 * using the hash function H as described by the MAC Identifier and
598 * the shared association key K based on the endpoint pair shared key
599 * described by the shared key identifier. The 'data' used for the
600 * computation of the AUTH-chunk is given by the AUTH chunk with its
601 * HMAC field set to zero (as shown in Figure 6) followed by all
602 * chunks that are placed after the AUTH chunk in the SCTP packet.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 */
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300604 if (auth)
605 sctp_auth_calculate_hmac(asoc, nskb,
606 (struct sctp_auth_chunk *)auth,
607 gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Marcelo Ricardo Leitnerf1533cc2016-07-07 09:39:29 -0300609 if (packet->auth) {
610 if (!list_empty(&packet->chunk_list)) {
611 /* We will generate more packets, so re-queue
612 * auth chunk.
613 */
Xin Long1aa25ec2016-07-30 13:58:35 +0800614 list_add(&packet->auth->list,
615 &packet->chunk_list);
Marcelo Ricardo Leitnerf1533cc2016-07-07 09:39:29 -0300616 } else {
617 sctp_chunk_free(packet->auth);
618 packet->auth = NULL;
619 }
620 }
621
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300622 if (!gso)
623 break;
624
625 if (skb_gro_receive(&head, nskb))
626 goto nomem;
627 nskb = NULL;
628 if (WARN_ON_ONCE(skb_shinfo(head)->gso_segs >=
629 sk->sk_gso_max_segs))
630 goto nomem;
631 } while (!list_empty(&packet->chunk_list));
Vlad Yasevich4cd57c82007-09-16 19:32:45 -0700632
633 /* 2) Calculate the Adler-32 checksum of the whole packet,
634 * including the SCTP common header and all the
635 * chunks.
636 *
637 * Note: Adler-32 is no longer applicable, as has been replaced
638 * by CRC32-C as described in <draft-ietf-tsvwg-sctpcsum-02.txt>.
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300639 *
640 * If it's a GSO packet, it's postponed to sctp_skb_segment.
Vlad Yasevich4cd57c82007-09-16 19:32:45 -0700641 */
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300642 if (!sctp_checksum_disable || gso) {
643 if (!gso && (!(dst->dev->features & NETIF_F_SCTP_CRC) ||
644 dst_xfrm(dst) || packet->ipfragok)) {
645 sh->checksum = sctp_compute_cksum(head, 0);
Michał Mirosławb73c43f2011-07-13 14:10:29 +0000646 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300647 /* no need to seed pseudo checksum for SCTP */
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300648 head->ip_summed = CHECKSUM_PARTIAL;
649 head->csum_start = skb_transport_header(head) - head->head;
650 head->csum_offset = offsetof(struct sctphdr, checksum);
Jesse Brandeburg8dc92f72009-04-27 22:35:52 +0000651 }
652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 /* IP layer ECN support
655 * From RFC 2481
656 * "The ECN-Capable Transport (ECT) bit would be set by the
657 * data sender to indicate that the end-points of the
658 * transport protocol are ECN-capable."
659 *
660 * Now setting the ECT bit all the time, as it should not cause
661 * any problems protocol-wise even if our peer ignores it.
662 *
663 * Note: The works for IPv6 layer checks this bit too later
664 * in transmission. See IP6_ECN_flow_xmit().
665 */
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300666 tp->af_specific->ecn_capable(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 /* Set up the IP options. */
669 /* BUG: not implemented
670 * For v4 this all lives somewhere in sk->sk_opt...
671 */
672
673 /* Dump that on IP! */
Michele Baldessari196d6752012-12-01 04:49:42 +0000674 if (asoc) {
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300675 asoc->stats.opackets += pktcount;
Michele Baldessari196d6752012-12-01 04:49:42 +0000676 if (asoc->peer.last_sent_to != tp)
677 /* Considering the multiple CPU scenario, this is a
678 * "correcter" place for last_sent_to. --xguo
679 */
680 asoc->peer.last_sent_to = tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
682
683 if (has_data) {
684 struct timer_list *timer;
685 unsigned long timeout;
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 /* Restart the AUTOCLOSE timer when sending data. */
Neil Horman9f70f462013-12-10 06:48:15 -0500688 if (sctp_state(asoc, ESTABLISHED) &&
689 asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 timer = &asoc->timers[SCTP_EVENT_TIMEOUT_AUTOCLOSE];
691 timeout = asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE];
692
693 if (!mod_timer(timer, jiffies + timeout))
694 sctp_association_hold(asoc);
695 }
696 }
697
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300698 pr_debug("***sctp_transmit_packet*** skb->len:%d\n", head->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300700 if (gso) {
701 /* Cleanup our debris for IP stacks */
702 memset(head->cb, 0, max(sizeof(struct inet_skb_parm),
703 sizeof(struct inet6_skb_parm)));
704
705 skb_shinfo(head)->gso_segs = pktcount;
706 skb_shinfo(head)->gso_size = GSO_BY_FRAGS;
707
708 /* We have to refresh this in case we are xmiting to
709 * more than one transport at a time
710 */
711 rcu_read_lock();
712 if (__sk_dst_get(sk) != tp->dst) {
713 dst_hold(tp->dst);
714 sk_setup_caps(sk, tp->dst);
715 }
716 rcu_read_unlock();
717 }
718 head->ignore_df = packet->ipfragok;
719 tp->af_specific->sctp_xmit(head, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721out:
Wei Yongjund521c082009-09-02 13:05:33 +0800722 sctp_packet_reset(packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 return err;
724no_route:
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300725 kfree_skb(head);
726 if (nskb != head)
727 kfree_skb(nskb);
Alexander Sverdlin29c4afc2015-06-29 10:41:03 +0200728
729 if (asoc)
730 IP_INC_STATS(sock_net(asoc->base.sk), IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 /* FIXME: Returning the 'err' will effect all the associations
733 * associated with a socket, although only one of the paths of the
734 * association is unreachable.
735 * The real failure of a transport or association can be passed on
736 * to the user via notifications. So setting this error may not be
737 * required.
738 */
739 /* err = -EHOSTUNREACH; */
740err:
741 /* Control chunks are unreliable so just drop them. DATA chunks
742 * will get resent or dropped later.
743 */
744
David S. Miller79af02c2005-07-08 21:47:49 -0700745 list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {
746 list_del_init(&chunk->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (!sctp_chunk_is_data(chunk))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900748 sctp_chunk_free(chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
750 goto out;
751nomem:
Marcelo Ricardo Leitnerf1533cc2016-07-07 09:39:29 -0300752 if (packet->auth && list_empty(&packet->auth->list))
753 sctp_chunk_free(packet->auth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 err = -ENOMEM;
755 goto err;
756}
757
758/********************************************************************
759 * 2nd Level Abstractions
760 ********************************************************************/
761
Vlad Yaseviche83963b2009-08-07 10:43:07 -0400762/* This private function check to see if a chunk can be added */
763static sctp_xmit_t sctp_packet_can_append_data(struct sctp_packet *packet,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 struct sctp_chunk *chunk)
765{
Vlad Yaseviche83963b2009-08-07 10:43:07 -0400766 size_t datasize, rwnd, inflight, flight_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 struct sctp_transport *transport = packet->transport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 struct sctp_association *asoc = transport->asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 struct sctp_outq *q = &asoc->outqueue;
770
771 /* RFC 2960 6.1 Transmission of DATA Chunks
772 *
773 * A) At any given time, the data sender MUST NOT transmit new data to
774 * any destination transport address if its peer's rwnd indicates
775 * that the peer has no buffer space (i.e. rwnd is 0, see Section
776 * 6.2.1). However, regardless of the value of rwnd (including if it
777 * is 0), the data sender can always have one DATA chunk in flight to
778 * the receiver if allowed by cwnd (see rule B below). This rule
779 * allows the sender to probe for a change in rwnd that the sender
780 * missed due to the SACK having been lost in transit from the data
781 * receiver to the data sender.
782 */
783
784 rwnd = asoc->peer.rwnd;
Vlad Yaseviche83963b2009-08-07 10:43:07 -0400785 inflight = q->outstanding_bytes;
786 flight_size = transport->flight_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 datasize = sctp_data_size(chunk);
789
David Laight723189f2014-07-22 08:59:08 +0000790 if (datasize > rwnd && inflight > 0)
791 /* We have (at least) one data chunk in flight,
792 * so we can't fall back to rule 6.1 B).
793 */
794 return SCTP_XMIT_RWND_FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 /* RFC 2960 6.1 Transmission of DATA Chunks
797 *
798 * B) At any given time, the sender MUST NOT transmit new data
799 * to a given transport address if it has cwnd or more bytes
800 * of data outstanding to that transport address.
801 */
802 /* RFC 7.2.4 & the Implementers Guide 2.8.
803 *
804 * 3) ...
805 * When a Fast Retransmit is being performed the sender SHOULD
806 * ignore the value of cwnd and SHOULD NOT delay retransmission.
807 */
David Laight723189f2014-07-22 08:59:08 +0000808 if (chunk->fast_retransmit != SCTP_NEED_FRTX &&
809 flight_size >= transport->cwnd)
810 return SCTP_XMIT_RWND_FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 /* Nagle's algorithm to solve small-packet problem:
813 * Inhibit the sending of new chunks when new outgoing data arrives
814 * if any previously transmitted data on the connection remains
815 * unacknowledged.
816 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
David Laight723189f2014-07-22 08:59:08 +0000818 if (sctp_sk(asoc->base.sk)->nodelay)
819 /* Nagle disabled */
820 return SCTP_XMIT_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
David Laight723189f2014-07-22 08:59:08 +0000822 if (!sctp_packet_empty(packet))
823 /* Append to packet */
824 return SCTP_XMIT_OK;
825
826 if (inflight == 0)
827 /* Nothing unacked */
828 return SCTP_XMIT_OK;
829
830 if (!sctp_state(asoc, ESTABLISHED))
831 return SCTP_XMIT_OK;
832
833 /* Check whether this chunk and all the rest of pending data will fit
834 * or delay in hopes of bundling a full sized packet.
835 */
Marcelo Ricardo Leitnere43569e2016-04-01 14:05:48 -0300836 if (chunk->skb->len + q->out_qlen >
837 transport->pathmtu - packet->overhead - sizeof(sctp_data_chunk_t) - 4)
David Laight723189f2014-07-22 08:59:08 +0000838 /* Enough data queued to fill a packet */
839 return SCTP_XMIT_OK;
840
841 /* Don't delay large message writes that may have been fragmented */
842 if (!chunk->msg->can_delay)
843 return SCTP_XMIT_OK;
844
845 /* Defer until all data acked or packet full */
David Laight526cbef2014-07-22 08:59:14 +0000846 return SCTP_XMIT_DELAY;
Vlad Yaseviche83963b2009-08-07 10:43:07 -0400847}
848
849/* This private function does management things when adding DATA chunk */
850static void sctp_packet_append_data(struct sctp_packet *packet,
851 struct sctp_chunk *chunk)
852{
853 struct sctp_transport *transport = packet->transport;
854 size_t datasize = sctp_data_size(chunk);
855 struct sctp_association *asoc = transport->asoc;
856 u32 rwnd = asoc->peer.rwnd;
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 /* Keep track of how many bytes are in flight over this transport. */
859 transport->flight_size += datasize;
860
861 /* Keep track of how many bytes are in flight to the receiver. */
862 asoc->outqueue.outstanding_bytes += datasize;
863
Thomas Grafa76c0ad2011-12-19 04:11:40 +0000864 /* Update our view of the receiver's rwnd. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 if (datasize < rwnd)
866 rwnd -= datasize;
867 else
868 rwnd = 0;
869
870 asoc->peer.rwnd = rwnd;
871 /* Has been accepted for transmission. */
872 if (!asoc->peer.prsctp_capable)
873 chunk->msg->can_abandon = 0;
Neil Hormand8dd1572009-11-23 15:54:00 -0500874 sctp_chunk_assign_tsn(chunk);
875 sctp_chunk_assign_ssn(chunk);
Vlad Yaseviche83963b2009-08-07 10:43:07 -0400876}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Vlad Yaseviche83963b2009-08-07 10:43:07 -0400878static sctp_xmit_t sctp_packet_will_fit(struct sctp_packet *packet,
879 struct sctp_chunk *chunk,
880 u16 chunk_len)
881{
Marcelo Ricardo Leitner7303a142016-09-08 17:54:11 +0800882 size_t psize, pmtu, maxsize;
Vlad Yaseviche83963b2009-08-07 10:43:07 -0400883 sctp_xmit_t retval = SCTP_XMIT_OK;
884
885 psize = packet->size;
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300886 if (packet->transport->asoc)
887 pmtu = packet->transport->asoc->pathmtu;
888 else
889 pmtu = packet->transport->pathmtu;
Vlad Yaseviche83963b2009-08-07 10:43:07 -0400890
891 /* Decide if we need to fragment or resubmit later. */
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300892 if (psize + chunk_len > pmtu) {
893 /* It's OK to fragment at IP level if any one of the following
Vlad Yaseviche83963b2009-08-07 10:43:07 -0400894 * is true:
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300895 * 1. The packet is empty (meaning this chunk is greater
896 * the MTU)
897 * 2. The packet doesn't have any data in it yet and data
898 * requires authentication.
Vlad Yaseviche83963b2009-08-07 10:43:07 -0400899 */
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300900 if (sctp_packet_empty(packet) ||
Vlad Yaseviche83963b2009-08-07 10:43:07 -0400901 (!packet->has_data && chunk->auth)) {
902 /* We no longer do re-fragmentation.
903 * Just fragment at the IP layer, if we
904 * actually hit this condition
905 */
906 packet->ipfragok = 1;
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300907 goto out;
Vlad Yaseviche83963b2009-08-07 10:43:07 -0400908 }
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300909
Marcelo Ricardo Leitner7303a142016-09-08 17:54:11 +0800910 /* Similarly, if this chunk was built before a PMTU
911 * reduction, we have to fragment it at IP level now. So
912 * if the packet already contains something, we need to
913 * flush.
914 */
915 maxsize = pmtu - packet->overhead;
916 if (packet->auth)
917 maxsize -= WORD_ROUND(packet->auth->skb->len);
918 if (chunk_len > maxsize)
919 retval = SCTP_XMIT_PMTU_FULL;
920
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300921 /* It is also okay to fragment if the chunk we are
922 * adding is a control chunk, but only if current packet
923 * is not a GSO one otherwise it causes fragmentation of
924 * a large frame. So in this case we allow the
925 * fragmentation by forcing it to be in a new packet.
926 */
927 if (!sctp_chunk_is_data(chunk) && packet->has_data)
928 retval = SCTP_XMIT_PMTU_FULL;
929
930 if (psize + chunk_len > packet->max_size)
931 /* Hit GSO/PMTU limit, gotta flush */
932 retval = SCTP_XMIT_PMTU_FULL;
933
934 if (!packet->transport->burst_limited &&
935 psize + chunk_len > (packet->transport->cwnd >> 1))
936 /* Do not allow a single GSO packet to use more
937 * than half of cwnd.
938 */
939 retval = SCTP_XMIT_PMTU_FULL;
940
941 if (packet->transport->burst_limited &&
942 psize + chunk_len > (packet->transport->burst_limited >> 1))
943 /* Do not allow a single GSO packet to use more
944 * than half of original cwnd.
945 */
946 retval = SCTP_XMIT_PMTU_FULL;
947 /* Otherwise it will fit in the GSO packet */
Vlad Yaseviche83963b2009-08-07 10:43:07 -0400948 }
949
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300950out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 return retval;
952}