/* SCTP kernel implementation1* (C) Copyright IBM Corp. 2001, 20042* Copyright (c) 1999-2000 Cisco, Inc.3* Copyright (c) 1999-2001 Motorola, Inc.4*5* This file is part of the SCTP kernel implementation6*7* These functions handle output processing.8*9* This SCTP implementation is free software;10* you can redistribute it and/or modify it under the terms of11* the GNU General Public License as published by12* the Free Software Foundation; either version 2, or (at your option)13* any later version.14*15* This SCTP implementation is distributed in the hope that it16* will be useful, but WITHOUT ANY WARRANTY; without even the implied17* ************************18* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.19* See the GNU General Public License for more details.20*21* You should have received a copy of the GNU General Public License22* along with GNU CC; see the file COPYING. If not, write to23* the Free Software Foundation, 59 Temple Place - Suite 330,24* Boston, MA 02111-1307, USA.25*26* Please send any bug reports or fixes you make to the27* email address(es):28* lksctp developers <[email protected]>29*30* Or submit a bug report through the following website:31* http://www.sf.net/projects/lksctp32*33* Written or modified by:34* La Monte H.P. Yarroll <[email protected]>35* Karl Knutson <[email protected]>36* Jon Grimm <[email protected]>37* Sridhar Samudrala <[email protected]>38*39* Any bugs reported given to us we will try to fix... any fixes shared will40* be incorporated into the next SCTP release.41*/4243#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt4445#include <linux/types.h>46#include <linux/kernel.h>47#include <linux/wait.h>48#include <linux/time.h>49#include <linux/ip.h>50#include <linux/ipv6.h>51#include <linux/init.h>52#include <linux/slab.h>53#include <net/inet_ecn.h>54#include <net/ip.h>55#include <net/icmp.h>56#include <net/net_namespace.h>5758#include <linux/socket.h> /* for sa_family_t */59#include <net/sock.h>6061#include <net/sctp/sctp.h>62#include <net/sctp/sm.h>63#include <net/sctp/checksum.h>6465/* Forward declarations for private helpers. */66static sctp_xmit_t sctp_packet_can_append_data(struct sctp_packet *packet,67struct sctp_chunk *chunk);68static void sctp_packet_append_data(struct sctp_packet *packet,69struct sctp_chunk *chunk);70static sctp_xmit_t sctp_packet_will_fit(struct sctp_packet *packet,71struct sctp_chunk *chunk,72u16 chunk_len);7374static void sctp_packet_reset(struct sctp_packet *packet)75{76packet->size = packet->overhead;77packet->has_cookie_echo = 0;78packet->has_sack = 0;79packet->has_data = 0;80packet->has_auth = 0;81packet->ipfragok = 0;82packet->auth = NULL;83}8485/* Config a packet.86* This appears to be a followup set of initializations.87*/88struct sctp_packet *sctp_packet_config(struct sctp_packet *packet,89__u32 vtag, int ecn_capable)90{91struct sctp_chunk *chunk = NULL;9293SCTP_DEBUG_PRINTK("%s: packet:%p vtag:0x%x\n", __func__,94packet, vtag);9596packet->vtag = vtag;9798if (ecn_capable && sctp_packet_empty(packet)) {99chunk = sctp_get_ecne_prepend(packet->transport->asoc);100101/* If there a is a prepend chunk stick it on the list before102* any other chunks get appended.103*/104if (chunk)105sctp_packet_append_chunk(packet, chunk);106}107108return packet;109}110111/* Initialize the packet structure. */112struct sctp_packet *sctp_packet_init(struct sctp_packet *packet,113struct sctp_transport *transport,114__u16 sport, __u16 dport)115{116struct sctp_association *asoc = transport->asoc;117size_t overhead;118119SCTP_DEBUG_PRINTK("%s: packet:%p transport:%p\n", __func__,120packet, transport);121122packet->transport = transport;123packet->source_port = sport;124packet->destination_port = dport;125INIT_LIST_HEAD(&packet->chunk_list);126if (asoc) {127struct sctp_sock *sp = sctp_sk(asoc->base.sk);128overhead = sp->pf->af->net_header_len;129} else {130overhead = sizeof(struct ipv6hdr);131}132overhead += sizeof(struct sctphdr);133packet->overhead = overhead;134sctp_packet_reset(packet);135packet->vtag = 0;136packet->malloced = 0;137return packet;138}139140/* Free a packet. */141void sctp_packet_free(struct sctp_packet *packet)142{143struct sctp_chunk *chunk, *tmp;144145SCTP_DEBUG_PRINTK("%s: packet:%p\n", __func__, packet);146147list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {148list_del_init(&chunk->list);149sctp_chunk_free(chunk);150}151152if (packet->malloced)153kfree(packet);154}155156/* This routine tries to append the chunk to the offered packet. If adding157* the chunk causes the packet to exceed the path MTU and COOKIE_ECHO chunk158* is not present in the packet, it transmits the input packet.159* Data can be bundled with a packet containing a COOKIE_ECHO chunk as long160* as it can fit in the packet, but any more data that does not fit in this161* packet can be sent only after receiving the COOKIE_ACK.162*/163sctp_xmit_t sctp_packet_transmit_chunk(struct sctp_packet *packet,164struct sctp_chunk *chunk,165int one_packet)166{167sctp_xmit_t retval;168int error = 0;169170SCTP_DEBUG_PRINTK("%s: packet:%p chunk:%p\n", __func__,171packet, chunk);172173switch ((retval = (sctp_packet_append_chunk(packet, chunk)))) {174case SCTP_XMIT_PMTU_FULL:175if (!packet->has_cookie_echo) {176error = sctp_packet_transmit(packet);177if (error < 0)178chunk->skb->sk->sk_err = -error;179180/* If we have an empty packet, then we can NOT ever181* return PMTU_FULL.182*/183if (!one_packet)184retval = sctp_packet_append_chunk(packet,185chunk);186}187break;188189case SCTP_XMIT_RWND_FULL:190case SCTP_XMIT_OK:191case SCTP_XMIT_NAGLE_DELAY:192break;193}194195return retval;196}197198/* Try to bundle an auth chunk into the packet. */199static sctp_xmit_t sctp_packet_bundle_auth(struct sctp_packet *pkt,200struct sctp_chunk *chunk)201{202struct sctp_association *asoc = pkt->transport->asoc;203struct sctp_chunk *auth;204sctp_xmit_t retval = SCTP_XMIT_OK;205206/* if we don't have an association, we can't do authentication */207if (!asoc)208return retval;209210/* See if this is an auth chunk we are bundling or if211* auth is already bundled.212*/213if (chunk->chunk_hdr->type == SCTP_CID_AUTH || pkt->has_auth)214return retval;215216/* if the peer did not request this chunk to be authenticated,217* don't do it218*/219if (!chunk->auth)220return retval;221222auth = sctp_make_auth(asoc);223if (!auth)224return retval;225226retval = sctp_packet_append_chunk(pkt, auth);227228return retval;229}230231/* Try to bundle a SACK with the packet. */232static sctp_xmit_t sctp_packet_bundle_sack(struct sctp_packet *pkt,233struct sctp_chunk *chunk)234{235sctp_xmit_t retval = SCTP_XMIT_OK;236237/* If sending DATA and haven't aleady bundled a SACK, try to238* bundle one in to the packet.239*/240if (sctp_chunk_is_data(chunk) && !pkt->has_sack &&241!pkt->has_cookie_echo) {242struct sctp_association *asoc;243struct timer_list *timer;244asoc = pkt->transport->asoc;245timer = &asoc->timers[SCTP_EVENT_TIMEOUT_SACK];246247/* If the SACK timer is running, we have a pending SACK */248if (timer_pending(timer)) {249struct sctp_chunk *sack;250asoc->a_rwnd = asoc->rwnd;251sack = sctp_make_sack(asoc);252if (sack) {253retval = sctp_packet_append_chunk(pkt, sack);254asoc->peer.sack_needed = 0;255if (del_timer(timer))256sctp_association_put(asoc);257}258}259}260return retval;261}262263/* Append a chunk to the offered packet reporting back any inability to do264* so.265*/266sctp_xmit_t sctp_packet_append_chunk(struct sctp_packet *packet,267struct sctp_chunk *chunk)268{269sctp_xmit_t retval = SCTP_XMIT_OK;270__u16 chunk_len = WORD_ROUND(ntohs(chunk->chunk_hdr->length));271272SCTP_DEBUG_PRINTK("%s: packet:%p chunk:%p\n", __func__, packet,273chunk);274275/* Data chunks are special. Before seeing what else we can276* bundle into this packet, check to see if we are allowed to277* send this DATA.278*/279if (sctp_chunk_is_data(chunk)) {280retval = sctp_packet_can_append_data(packet, chunk);281if (retval != SCTP_XMIT_OK)282goto finish;283}284285/* Try to bundle AUTH chunk */286retval = sctp_packet_bundle_auth(packet, chunk);287if (retval != SCTP_XMIT_OK)288goto finish;289290/* Try to bundle SACK chunk */291retval = sctp_packet_bundle_sack(packet, chunk);292if (retval != SCTP_XMIT_OK)293goto finish;294295/* Check to see if this chunk will fit into the packet */296retval = sctp_packet_will_fit(packet, chunk, chunk_len);297if (retval != SCTP_XMIT_OK)298goto finish;299300/* We believe that this chunk is OK to add to the packet */301switch (chunk->chunk_hdr->type) {302case SCTP_CID_DATA:303/* Account for the data being in the packet */304sctp_packet_append_data(packet, chunk);305/* Disallow SACK bundling after DATA. */306packet->has_sack = 1;307/* Disallow AUTH bundling after DATA */308packet->has_auth = 1;309/* Let it be knows that packet has DATA in it */310packet->has_data = 1;311/* timestamp the chunk for rtx purposes */312chunk->sent_at = jiffies;313break;314case SCTP_CID_COOKIE_ECHO:315packet->has_cookie_echo = 1;316break;317318case SCTP_CID_SACK:319packet->has_sack = 1;320break;321322case SCTP_CID_AUTH:323packet->has_auth = 1;324packet->auth = chunk;325break;326}327328/* It is OK to send this chunk. */329list_add_tail(&chunk->list, &packet->chunk_list);330packet->size += chunk_len;331chunk->transport = packet->transport;332finish:333return retval;334}335336/* All packets are sent to the network through this function from337* sctp_outq_tail().338*339* The return value is a normal kernel error return value.340*/341int sctp_packet_transmit(struct sctp_packet *packet)342{343struct sctp_transport *tp = packet->transport;344struct sctp_association *asoc = tp->asoc;345struct sctphdr *sh;346struct sk_buff *nskb;347struct sctp_chunk *chunk, *tmp;348struct sock *sk;349int err = 0;350int padding; /* How much padding do we need? */351__u8 has_data = 0;352struct dst_entry *dst = tp->dst;353unsigned char *auth = NULL; /* pointer to auth in skb data */354__u32 cksum_buf_len = sizeof(struct sctphdr);355356SCTP_DEBUG_PRINTK("%s: packet:%p\n", __func__, packet);357358/* Do NOT generate a chunkless packet. */359if (list_empty(&packet->chunk_list))360return err;361362/* Set up convenience variables... */363chunk = list_entry(packet->chunk_list.next, struct sctp_chunk, list);364sk = chunk->skb->sk;365366/* Allocate the new skb. */367nskb = alloc_skb(packet->size + LL_MAX_HEADER, GFP_ATOMIC);368if (!nskb)369goto nomem;370371/* Make sure the outbound skb has enough header room reserved. */372skb_reserve(nskb, packet->overhead + LL_MAX_HEADER);373374/* Set the owning socket so that we know where to get the375* destination IP address.376*/377skb_set_owner_w(nskb, sk);378379/* The 'obsolete' field of dst is set to 2 when a dst is freed. */380if (!dst || (dst->obsolete > 1)) {381dst_release(dst);382sctp_transport_route(tp, NULL, sctp_sk(sk));383if (asoc && (asoc->param_flags & SPP_PMTUD_ENABLE)) {384sctp_assoc_sync_pmtu(asoc);385}386}387dst = dst_clone(tp->dst);388skb_dst_set(nskb, dst);389if (!dst)390goto no_route;391392/* Build the SCTP header. */393sh = (struct sctphdr *)skb_push(nskb, sizeof(struct sctphdr));394skb_reset_transport_header(nskb);395sh->source = htons(packet->source_port);396sh->dest = htons(packet->destination_port);397398/* From 6.8 Adler-32 Checksum Calculation:399* After the packet is constructed (containing the SCTP common400* header and one or more control or DATA chunks), the401* transmitter shall:402*403* 1) Fill in the proper Verification Tag in the SCTP common404* header and initialize the checksum field to 0's.405*/406sh->vtag = htonl(packet->vtag);407sh->checksum = 0;408409/**410* 6.10 Bundling411*412* An endpoint bundles chunks by simply including multiple413* chunks in one outbound SCTP packet. ...414*/415416/**417* 3.2 Chunk Field Descriptions418*419* The total length of a chunk (including Type, Length and420* Value fields) MUST be a multiple of 4 bytes. If the length421* of the chunk is not a multiple of 4 bytes, the sender MUST422* pad the chunk with all zero bytes and this padding is not423* included in the chunk length field. The sender should424* never pad with more than 3 bytes.425*426* [This whole comment explains WORD_ROUND() below.]427*/428SCTP_DEBUG_PRINTK("***sctp_transmit_packet***\n");429list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {430list_del_init(&chunk->list);431if (sctp_chunk_is_data(chunk)) {432/* 6.3.1 C4) When data is in flight and when allowed433* by rule C5, a new RTT measurement MUST be made each434* round trip. Furthermore, new RTT measurements435* SHOULD be made no more than once per round-trip436* for a given destination transport address.437*/438439if (!tp->rto_pending) {440chunk->rtt_in_progress = 1;441tp->rto_pending = 1;442}443has_data = 1;444}445446padding = WORD_ROUND(chunk->skb->len) - chunk->skb->len;447if (padding)448memset(skb_put(chunk->skb, padding), 0, padding);449450/* if this is the auth chunk that we are adding,451* store pointer where it will be added and put452* the auth into the packet.453*/454if (chunk == packet->auth)455auth = skb_tail_pointer(nskb);456457cksum_buf_len += chunk->skb->len;458memcpy(skb_put(nskb, chunk->skb->len),459chunk->skb->data, chunk->skb->len);460461SCTP_DEBUG_PRINTK("%s %p[%s] %s 0x%x, %s %d, %s %d, %s %d\n",462"*** Chunk", chunk,463sctp_cname(SCTP_ST_CHUNK(464chunk->chunk_hdr->type)),465chunk->has_tsn ? "TSN" : "No TSN",466chunk->has_tsn ?467ntohl(chunk->subh.data_hdr->tsn) : 0,468"length", ntohs(chunk->chunk_hdr->length),469"chunk->skb->len", chunk->skb->len,470"rtt_in_progress", chunk->rtt_in_progress);471472/*473* If this is a control chunk, this is our last474* reference. Free data chunks after they've been475* acknowledged or have failed.476*/477if (!sctp_chunk_is_data(chunk))478sctp_chunk_free(chunk);479}480481/* SCTP-AUTH, Section 6.2482* The sender MUST calculate the MAC as described in RFC2104 [2]483* using the hash function H as described by the MAC Identifier and484* the shared association key K based on the endpoint pair shared key485* described by the shared key identifier. The 'data' used for the486* computation of the AUTH-chunk is given by the AUTH chunk with its487* HMAC field set to zero (as shown in Figure 6) followed by all488* chunks that are placed after the AUTH chunk in the SCTP packet.489*/490if (auth)491sctp_auth_calculate_hmac(asoc, nskb,492(struct sctp_auth_chunk *)auth,493GFP_ATOMIC);494495/* 2) Calculate the Adler-32 checksum of the whole packet,496* including the SCTP common header and all the497* chunks.498*499* Note: Adler-32 is no longer applicable, as has been replaced500* by CRC32-C as described in <draft-ietf-tsvwg-sctpcsum-02.txt>.501*/502if (!sctp_checksum_disable) {503if (!(dst->dev->features & NETIF_F_SCTP_CSUM)) {504__u32 crc32 = sctp_start_cksum((__u8 *)sh, cksum_buf_len);505506/* 3) Put the resultant value into the checksum field in the507* common header, and leave the rest of the bits unchanged.508*/509sh->checksum = sctp_end_cksum(crc32);510} else {511/* no need to seed pseudo checksum for SCTP */512nskb->ip_summed = CHECKSUM_PARTIAL;513nskb->csum_start = (skb_transport_header(nskb) -514nskb->head);515nskb->csum_offset = offsetof(struct sctphdr, checksum);516}517}518519/* IP layer ECN support520* From RFC 2481521* "The ECN-Capable Transport (ECT) bit would be set by the522* data sender to indicate that the end-points of the523* transport protocol are ECN-capable."524*525* Now setting the ECT bit all the time, as it should not cause526* any problems protocol-wise even if our peer ignores it.527*528* Note: The works for IPv6 layer checks this bit too later529* in transmission. See IP6_ECN_flow_xmit().530*/531(*tp->af_specific->ecn_capable)(nskb->sk);532533/* Set up the IP options. */534/* BUG: not implemented535* For v4 this all lives somewhere in sk->sk_opt...536*/537538/* Dump that on IP! */539if (asoc && asoc->peer.last_sent_to != tp) {540/* Considering the multiple CPU scenario, this is a541* "correcter" place for last_sent_to. --xguo542*/543asoc->peer.last_sent_to = tp;544}545546if (has_data) {547struct timer_list *timer;548unsigned long timeout;549550/* Restart the AUTOCLOSE timer when sending data. */551if (sctp_state(asoc, ESTABLISHED) && asoc->autoclose) {552timer = &asoc->timers[SCTP_EVENT_TIMEOUT_AUTOCLOSE];553timeout = asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE];554555if (!mod_timer(timer, jiffies + timeout))556sctp_association_hold(asoc);557}558}559560SCTP_DEBUG_PRINTK("***sctp_transmit_packet*** skb len %d\n",561nskb->len);562563nskb->local_df = packet->ipfragok;564(*tp->af_specific->sctp_xmit)(nskb, tp);565566out:567sctp_packet_reset(packet);568return err;569no_route:570kfree_skb(nskb);571IP_INC_STATS_BH(&init_net, IPSTATS_MIB_OUTNOROUTES);572573/* FIXME: Returning the 'err' will effect all the associations574* associated with a socket, although only one of the paths of the575* association is unreachable.576* The real failure of a transport or association can be passed on577* to the user via notifications. So setting this error may not be578* required.579*/580/* err = -EHOSTUNREACH; */581err:582/* Control chunks are unreliable so just drop them. DATA chunks583* will get resent or dropped later.584*/585586list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) {587list_del_init(&chunk->list);588if (!sctp_chunk_is_data(chunk))589sctp_chunk_free(chunk);590}591goto out;592nomem:593err = -ENOMEM;594goto err;595}596597/********************************************************************598* 2nd Level Abstractions599********************************************************************/600601/* This private function check to see if a chunk can be added */602static sctp_xmit_t sctp_packet_can_append_data(struct sctp_packet *packet,603struct sctp_chunk *chunk)604{605sctp_xmit_t retval = SCTP_XMIT_OK;606size_t datasize, rwnd, inflight, flight_size;607struct sctp_transport *transport = packet->transport;608struct sctp_association *asoc = transport->asoc;609struct sctp_outq *q = &asoc->outqueue;610611/* RFC 2960 6.1 Transmission of DATA Chunks612*613* A) At any given time, the data sender MUST NOT transmit new data to614* any destination transport address if its peer's rwnd indicates615* that the peer has no buffer space (i.e. rwnd is 0, see Section616* 6.2.1). However, regardless of the value of rwnd (including if it617* is 0), the data sender can always have one DATA chunk in flight to618* the receiver if allowed by cwnd (see rule B below). This rule619* allows the sender to probe for a change in rwnd that the sender620* missed due to the SACK having been lost in transit from the data621* receiver to the data sender.622*/623624rwnd = asoc->peer.rwnd;625inflight = q->outstanding_bytes;626flight_size = transport->flight_size;627628datasize = sctp_data_size(chunk);629630if (datasize > rwnd) {631if (inflight > 0) {632/* We have (at least) one data chunk in flight,633* so we can't fall back to rule 6.1 B).634*/635retval = SCTP_XMIT_RWND_FULL;636goto finish;637}638}639640/* RFC 2960 6.1 Transmission of DATA Chunks641*642* B) At any given time, the sender MUST NOT transmit new data643* to a given transport address if it has cwnd or more bytes644* of data outstanding to that transport address.645*/646/* RFC 7.2.4 & the Implementers Guide 2.8.647*648* 3) ...649* When a Fast Retransmit is being performed the sender SHOULD650* ignore the value of cwnd and SHOULD NOT delay retransmission.651*/652if (chunk->fast_retransmit != SCTP_NEED_FRTX)653if (flight_size >= transport->cwnd) {654retval = SCTP_XMIT_RWND_FULL;655goto finish;656}657658/* Nagle's algorithm to solve small-packet problem:659* Inhibit the sending of new chunks when new outgoing data arrives660* if any previously transmitted data on the connection remains661* unacknowledged.662*/663if (!sctp_sk(asoc->base.sk)->nodelay && sctp_packet_empty(packet) &&664inflight && sctp_state(asoc, ESTABLISHED)) {665unsigned max = transport->pathmtu - packet->overhead;666unsigned len = chunk->skb->len + q->out_qlen;667668/* Check whether this chunk and all the rest of pending669* data will fit or delay in hopes of bundling a full670* sized packet.671* Don't delay large message writes that may have been672* fragmeneted into small peices.673*/674if ((len < max) && chunk->msg->can_delay) {675retval = SCTP_XMIT_NAGLE_DELAY;676goto finish;677}678}679680finish:681return retval;682}683684/* This private function does management things when adding DATA chunk */685static void sctp_packet_append_data(struct sctp_packet *packet,686struct sctp_chunk *chunk)687{688struct sctp_transport *transport = packet->transport;689size_t datasize = sctp_data_size(chunk);690struct sctp_association *asoc = transport->asoc;691u32 rwnd = asoc->peer.rwnd;692693/* Keep track of how many bytes are in flight over this transport. */694transport->flight_size += datasize;695696/* Keep track of how many bytes are in flight to the receiver. */697asoc->outqueue.outstanding_bytes += datasize;698699/* Update our view of the receiver's rwnd. Include sk_buff overhead700* while updating peer.rwnd so that it reduces the chances of a701* receiver running out of receive buffer space even when receive702* window is still open. This can happen when a sender is sending703* sending small messages.704*/705datasize += sizeof(struct sk_buff);706if (datasize < rwnd)707rwnd -= datasize;708else709rwnd = 0;710711asoc->peer.rwnd = rwnd;712/* Has been accepted for transmission. */713if (!asoc->peer.prsctp_capable)714chunk->msg->can_abandon = 0;715sctp_chunk_assign_tsn(chunk);716sctp_chunk_assign_ssn(chunk);717}718719static sctp_xmit_t sctp_packet_will_fit(struct sctp_packet *packet,720struct sctp_chunk *chunk,721u16 chunk_len)722{723size_t psize;724size_t pmtu;725int too_big;726sctp_xmit_t retval = SCTP_XMIT_OK;727728psize = packet->size;729pmtu = ((packet->transport->asoc) ?730(packet->transport->asoc->pathmtu) :731(packet->transport->pathmtu));732733too_big = (psize + chunk_len > pmtu);734735/* Decide if we need to fragment or resubmit later. */736if (too_big) {737/* It's OK to fragmet at IP level if any one of the following738* is true:739* 1. The packet is empty (meaning this chunk is greater740* the MTU)741* 2. The chunk we are adding is a control chunk742* 3. The packet doesn't have any data in it yet and data743* requires authentication.744*/745if (sctp_packet_empty(packet) || !sctp_chunk_is_data(chunk) ||746(!packet->has_data && chunk->auth)) {747/* We no longer do re-fragmentation.748* Just fragment at the IP layer, if we749* actually hit this condition750*/751packet->ipfragok = 1;752} else {753retval = SCTP_XMIT_PMTU_FULL;754}755}756757return retval;758}759760761