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