160c778b2SVlad Yasevich /* SCTP kernel implementation 21da177e4SLinus Torvalds * (C) Copyright IBM Corp. 2003, 2004 31da177e4SLinus Torvalds * 460c778b2SVlad Yasevich * This file is part of the SCTP kernel implementation 51da177e4SLinus Torvalds * 659c51591SMichael Opdenacker * This file contains the code relating the chunk abstraction. 71da177e4SLinus Torvalds * 860c778b2SVlad Yasevich * This SCTP implementation is free software; 91da177e4SLinus Torvalds * you can redistribute it and/or modify it under the terms of 101da177e4SLinus Torvalds * the GNU General Public License as published by 111da177e4SLinus Torvalds * the Free Software Foundation; either version 2, or (at your option) 121da177e4SLinus Torvalds * any later version. 131da177e4SLinus Torvalds * 1460c778b2SVlad Yasevich * This SCTP implementation is distributed in the hope that it 151da177e4SLinus Torvalds * will be useful, but WITHOUT ANY WARRANTY; without even the implied 161da177e4SLinus Torvalds * ************************ 171da177e4SLinus Torvalds * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 181da177e4SLinus Torvalds * See the GNU General Public License for more details. 191da177e4SLinus Torvalds * 201da177e4SLinus Torvalds * You should have received a copy of the GNU General Public License 211da177e4SLinus Torvalds * along with GNU CC; see the file COPYING. If not, write to 221da177e4SLinus Torvalds * the Free Software Foundation, 59 Temple Place - Suite 330, 231da177e4SLinus Torvalds * Boston, MA 02111-1307, USA. 241da177e4SLinus Torvalds * 251da177e4SLinus Torvalds * Please send any bug reports or fixes you make to the 261da177e4SLinus Torvalds * email address(es): 271da177e4SLinus Torvalds * lksctp developers <lksctp-developers@lists.sourceforge.net> 281da177e4SLinus Torvalds * 291da177e4SLinus Torvalds * Or submit a bug report through the following website: 301da177e4SLinus Torvalds * http://www.sf.net/projects/lksctp 311da177e4SLinus Torvalds * 321da177e4SLinus Torvalds * Written or modified by: 331da177e4SLinus Torvalds * Jon Grimm <jgrimm@us.ibm.com> 341da177e4SLinus Torvalds * Sridhar Samudrala <sri@us.ibm.com> 351da177e4SLinus Torvalds * 361da177e4SLinus Torvalds * Any bugs reported given to us we will try to fix... any fixes shared will 371da177e4SLinus Torvalds * be incorporated into the next SCTP release. 381da177e4SLinus Torvalds */ 391da177e4SLinus Torvalds 401da177e4SLinus Torvalds #include <linux/types.h> 411da177e4SLinus Torvalds #include <linux/kernel.h> 421da177e4SLinus Torvalds #include <linux/net.h> 431da177e4SLinus Torvalds #include <linux/inet.h> 441da177e4SLinus Torvalds #include <linux/skbuff.h> 455a0e3ad6STejun Heo #include <linux/slab.h> 461da177e4SLinus Torvalds #include <net/sock.h> 471da177e4SLinus Torvalds #include <net/sctp/sctp.h> 481da177e4SLinus Torvalds #include <net/sctp/sm.h> 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds /* This file is mostly in anticipation of future work, but initially 511da177e4SLinus Torvalds * populate with fragment tracking for an outbound message. 521da177e4SLinus Torvalds */ 531da177e4SLinus Torvalds 541da177e4SLinus Torvalds /* Initialize datamsg from memory. */ 551da177e4SLinus Torvalds static void sctp_datamsg_init(struct sctp_datamsg *msg) 561da177e4SLinus Torvalds { 571da177e4SLinus Torvalds atomic_set(&msg->refcnt, 1); 581da177e4SLinus Torvalds msg->send_failed = 0; 591da177e4SLinus Torvalds msg->send_error = 0; 601da177e4SLinus Torvalds msg->can_abandon = 0; 61*0e3aef8dSVlad Yasevich msg->can_delay = 1; 621da177e4SLinus Torvalds msg->expires_at = 0; 631da177e4SLinus Torvalds INIT_LIST_HEAD(&msg->chunks); 641da177e4SLinus Torvalds } 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds /* Allocate and initialize datamsg. */ 67dd0fc66fSAl Viro SCTP_STATIC struct sctp_datamsg *sctp_datamsg_new(gfp_t gfp) 681da177e4SLinus Torvalds { 691da177e4SLinus Torvalds struct sctp_datamsg *msg; 701da177e4SLinus Torvalds msg = kmalloc(sizeof(struct sctp_datamsg), gfp); 71e8c38751SLi Zefan if (msg) { 721da177e4SLinus Torvalds sctp_datamsg_init(msg); 731da177e4SLinus Torvalds SCTP_DBG_OBJCNT_INC(datamsg); 74e8c38751SLi Zefan } 751da177e4SLinus Torvalds return msg; 761da177e4SLinus Torvalds } 771da177e4SLinus Torvalds 789c5c62beSVlad Yasevich void sctp_datamsg_free(struct sctp_datamsg *msg) 799c5c62beSVlad Yasevich { 809c5c62beSVlad Yasevich struct sctp_chunk *chunk; 819c5c62beSVlad Yasevich 829c5c62beSVlad Yasevich /* This doesn't have to be a _safe vairant because 839c5c62beSVlad Yasevich * sctp_chunk_free() only drops the refs. 849c5c62beSVlad Yasevich */ 859c5c62beSVlad Yasevich list_for_each_entry(chunk, &msg->chunks, frag_list) 869c5c62beSVlad Yasevich sctp_chunk_free(chunk); 879c5c62beSVlad Yasevich 889c5c62beSVlad Yasevich sctp_datamsg_put(msg); 899c5c62beSVlad Yasevich } 909c5c62beSVlad Yasevich 911da177e4SLinus Torvalds /* Final destructruction of datamsg memory. */ 921da177e4SLinus Torvalds static void sctp_datamsg_destroy(struct sctp_datamsg *msg) 931da177e4SLinus Torvalds { 941da177e4SLinus Torvalds struct list_head *pos, *temp; 951da177e4SLinus Torvalds struct sctp_chunk *chunk; 961da177e4SLinus Torvalds struct sctp_sock *sp; 971da177e4SLinus Torvalds struct sctp_ulpevent *ev; 981da177e4SLinus Torvalds struct sctp_association *asoc = NULL; 991da177e4SLinus Torvalds int error = 0, notify; 1001da177e4SLinus Torvalds 1011da177e4SLinus Torvalds /* If we failed, we may need to notify. */ 1021da177e4SLinus Torvalds notify = msg->send_failed ? -1 : 0; 1031da177e4SLinus Torvalds 1041da177e4SLinus Torvalds /* Release all references. */ 1051da177e4SLinus Torvalds list_for_each_safe(pos, temp, &msg->chunks) { 1061da177e4SLinus Torvalds list_del_init(pos); 1071da177e4SLinus Torvalds chunk = list_entry(pos, struct sctp_chunk, frag_list); 1081da177e4SLinus Torvalds /* Check whether we _really_ need to notify. */ 1091da177e4SLinus Torvalds if (notify < 0) { 1101da177e4SLinus Torvalds asoc = chunk->asoc; 1111da177e4SLinus Torvalds if (msg->send_error) 1121da177e4SLinus Torvalds error = msg->send_error; 1131da177e4SLinus Torvalds else 1141da177e4SLinus Torvalds error = asoc->outqueue.error; 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds sp = sctp_sk(asoc->base.sk); 1171da177e4SLinus Torvalds notify = sctp_ulpevent_type_enabled(SCTP_SEND_FAILED, 1181da177e4SLinus Torvalds &sp->subscribe); 1191da177e4SLinus Torvalds } 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds /* Generate a SEND FAILED event only if enabled. */ 1221da177e4SLinus Torvalds if (notify > 0) { 1231da177e4SLinus Torvalds int sent; 1241da177e4SLinus Torvalds if (chunk->has_tsn) 1251da177e4SLinus Torvalds sent = SCTP_DATA_SENT; 1261da177e4SLinus Torvalds else 1271da177e4SLinus Torvalds sent = SCTP_DATA_UNSENT; 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds ev = sctp_ulpevent_make_send_failed(asoc, chunk, sent, 1301da177e4SLinus Torvalds error, GFP_ATOMIC); 1311da177e4SLinus Torvalds if (ev) 1321da177e4SLinus Torvalds sctp_ulpq_tail_event(&asoc->ulpq, ev); 1331da177e4SLinus Torvalds } 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds sctp_chunk_put(chunk); 1361da177e4SLinus Torvalds } 1371da177e4SLinus Torvalds 1381da177e4SLinus Torvalds SCTP_DBG_OBJCNT_DEC(datamsg); 1391da177e4SLinus Torvalds kfree(msg); 1401da177e4SLinus Torvalds } 1411da177e4SLinus Torvalds 1421da177e4SLinus Torvalds /* Hold a reference. */ 1431da177e4SLinus Torvalds static void sctp_datamsg_hold(struct sctp_datamsg *msg) 1441da177e4SLinus Torvalds { 1451da177e4SLinus Torvalds atomic_inc(&msg->refcnt); 1461da177e4SLinus Torvalds } 1471da177e4SLinus Torvalds 1481da177e4SLinus Torvalds /* Release a reference. */ 1491da177e4SLinus Torvalds void sctp_datamsg_put(struct sctp_datamsg *msg) 1501da177e4SLinus Torvalds { 1511da177e4SLinus Torvalds if (atomic_dec_and_test(&msg->refcnt)) 1521da177e4SLinus Torvalds sctp_datamsg_destroy(msg); 1531da177e4SLinus Torvalds } 1541da177e4SLinus Torvalds 1551da177e4SLinus Torvalds /* Assign a chunk to this datamsg. */ 1561da177e4SLinus Torvalds static void sctp_datamsg_assign(struct sctp_datamsg *msg, struct sctp_chunk *chunk) 1571da177e4SLinus Torvalds { 1581da177e4SLinus Torvalds sctp_datamsg_hold(msg); 1591da177e4SLinus Torvalds chunk->msg = msg; 1601da177e4SLinus Torvalds } 1611da177e4SLinus Torvalds 1621da177e4SLinus Torvalds 1631da177e4SLinus Torvalds /* A data chunk can have a maximum payload of (2^16 - 20). Break 1641da177e4SLinus Torvalds * down any such message into smaller chunks. Opportunistically, fragment 1651da177e4SLinus Torvalds * the chunks down to the current MTU constraints. We may get refragmented 1661da177e4SLinus Torvalds * later if the PMTU changes, but it is _much better_ to fragment immediately 1671da177e4SLinus Torvalds * with a reasonable guess than always doing our fragmentation on the 1681da177e4SLinus Torvalds * soft-interrupt. 1691da177e4SLinus Torvalds */ 1701da177e4SLinus Torvalds struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc, 1711da177e4SLinus Torvalds struct sctp_sndrcvinfo *sinfo, 1721da177e4SLinus Torvalds struct msghdr *msgh, int msg_len) 1731da177e4SLinus Torvalds { 1741da177e4SLinus Torvalds int max, whole, i, offset, over, err; 1751da177e4SLinus Torvalds int len, first_len; 1763e62abf9SVlad Yasevich int max_data; 1771da177e4SLinus Torvalds struct sctp_chunk *chunk; 1781da177e4SLinus Torvalds struct sctp_datamsg *msg; 1791da177e4SLinus Torvalds struct list_head *pos, *temp; 1801da177e4SLinus Torvalds __u8 frag; 1811da177e4SLinus Torvalds 1821da177e4SLinus Torvalds msg = sctp_datamsg_new(GFP_KERNEL); 1831da177e4SLinus Torvalds if (!msg) 1841da177e4SLinus Torvalds return NULL; 1851da177e4SLinus Torvalds 1861da177e4SLinus Torvalds /* Note: Calculate this outside of the loop, so that all fragments 1871da177e4SLinus Torvalds * have the same expiration. 1881da177e4SLinus Torvalds */ 1891da177e4SLinus Torvalds if (sinfo->sinfo_timetolive) { 1901da177e4SLinus Torvalds /* sinfo_timetolive is in milliseconds */ 1911da177e4SLinus Torvalds msg->expires_at = jiffies + 1921da177e4SLinus Torvalds msecs_to_jiffies(sinfo->sinfo_timetolive); 1931da177e4SLinus Torvalds msg->can_abandon = 1; 1941da177e4SLinus Torvalds SCTP_DEBUG_PRINTK("%s: msg:%p expires_at: %ld jiffies:%ld\n", 1950dc47877SHarvey Harrison __func__, msg, msg->expires_at, jiffies); 1961da177e4SLinus Torvalds } 1971da177e4SLinus Torvalds 1983e62abf9SVlad Yasevich /* This is the biggest possible DATA chunk that can fit into 1993e62abf9SVlad Yasevich * the packet 2003e62abf9SVlad Yasevich */ 2013e62abf9SVlad Yasevich max_data = asoc->pathmtu - 2023e62abf9SVlad Yasevich sctp_sk(asoc->base.sk)->pf->af->net_header_len - 2033e62abf9SVlad Yasevich sizeof(struct sctphdr) - sizeof(struct sctp_data_chunk); 2041da177e4SLinus Torvalds 2053e62abf9SVlad Yasevich max = asoc->frag_point; 2064cd57c80SVlad Yasevich /* If the the peer requested that we authenticate DATA chunks 2074cd57c80SVlad Yasevich * we need to accound for bundling of the AUTH chunks along with 2084cd57c80SVlad Yasevich * DATA. 2094cd57c80SVlad Yasevich */ 2104cd57c80SVlad Yasevich if (sctp_auth_send_cid(SCTP_CID_DATA, asoc)) { 2114cd57c80SVlad Yasevich struct sctp_hmac *hmac_desc = sctp_auth_asoc_get_hmac(asoc); 2124cd57c80SVlad Yasevich 2134cd57c80SVlad Yasevich if (hmac_desc) 2143e62abf9SVlad Yasevich max_data -= WORD_ROUND(sizeof(sctp_auth_chunk_t) + 2154cd57c80SVlad Yasevich hmac_desc->hmac_len); 2164cd57c80SVlad Yasevich } 2174cd57c80SVlad Yasevich 2183e62abf9SVlad Yasevich /* Now, check if we need to reduce our max */ 2193e62abf9SVlad Yasevich if (max > max_data) 2203e62abf9SVlad Yasevich max = max_data; 2213e62abf9SVlad Yasevich 2221da177e4SLinus Torvalds whole = 0; 2231da177e4SLinus Torvalds first_len = max; 2241da177e4SLinus Torvalds 2255d7ff261SVlad Yasevich /* Check to see if we have a pending SACK and try to let it be bundled 2265d7ff261SVlad Yasevich * with this message. Do this if we don't have any data queued already. 2275d7ff261SVlad Yasevich * To check that, look at out_qlen and retransmit list. 2285d7ff261SVlad Yasevich * NOTE: we will not reduce to account for SACK, if the message would 2295d7ff261SVlad Yasevich * not have been fragmented. 2305d7ff261SVlad Yasevich */ 2315d7ff261SVlad Yasevich if (timer_pending(&asoc->timers[SCTP_EVENT_TIMEOUT_SACK]) && 2325d7ff261SVlad Yasevich asoc->outqueue.out_qlen == 0 && 2335d7ff261SVlad Yasevich list_empty(&asoc->outqueue.retransmit) && 2345d7ff261SVlad Yasevich msg_len > max) 2355d7ff261SVlad Yasevich max_data -= WORD_ROUND(sizeof(sctp_sack_chunk_t)); 2365d7ff261SVlad Yasevich 2371da177e4SLinus Torvalds /* Encourage Cookie-ECHO bundling. */ 2385d7ff261SVlad Yasevich if (asoc->state < SCTP_STATE_COOKIE_ECHOED) 2393e62abf9SVlad Yasevich max_data -= SCTP_ARBITRARY_COOKIE_ECHO_LEN; 2401da177e4SLinus Torvalds 2415d7ff261SVlad Yasevich /* Now that we adjusted completely, reset first_len */ 2423e62abf9SVlad Yasevich if (first_len > max_data) 2433e62abf9SVlad Yasevich first_len = max_data; 2443e62abf9SVlad Yasevich 2453e62abf9SVlad Yasevich /* Account for a different sized first fragment */ 2463e62abf9SVlad Yasevich if (msg_len >= first_len) { 2471da177e4SLinus Torvalds msg_len -= first_len; 2481da177e4SLinus Torvalds whole = 1; 249*0e3aef8dSVlad Yasevich msg->can_delay = 0; 2501da177e4SLinus Torvalds } 2511da177e4SLinus Torvalds 2521da177e4SLinus Torvalds /* How many full sized? How many bytes leftover? */ 2531da177e4SLinus Torvalds whole += msg_len / max; 2541da177e4SLinus Torvalds over = msg_len % max; 2551da177e4SLinus Torvalds offset = 0; 2561da177e4SLinus Torvalds 2571da177e4SLinus Torvalds if ((whole > 1) || (whole && over)) 2581da177e4SLinus Torvalds SCTP_INC_STATS_USER(SCTP_MIB_FRAGUSRMSGS); 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds /* Create chunks for all the full sized DATA chunks. */ 2611da177e4SLinus Torvalds for (i=0, len=first_len; i < whole; i++) { 2621da177e4SLinus Torvalds frag = SCTP_DATA_MIDDLE_FRAG; 2631da177e4SLinus Torvalds 2641da177e4SLinus Torvalds if (0 == i) 2651da177e4SLinus Torvalds frag |= SCTP_DATA_FIRST_FRAG; 2661da177e4SLinus Torvalds 267b93d6471SWei Yongjun if ((i == (whole - 1)) && !over) { 2681da177e4SLinus Torvalds frag |= SCTP_DATA_LAST_FRAG; 2691da177e4SLinus Torvalds 270b93d6471SWei Yongjun /* The application requests to set the I-bit of the 271b93d6471SWei Yongjun * last DATA chunk of a user message when providing 272b93d6471SWei Yongjun * the user message to the SCTP implementation. 273b93d6471SWei Yongjun */ 274b93d6471SWei Yongjun if ((sinfo->sinfo_flags & SCTP_EOF) || 275b93d6471SWei Yongjun (sinfo->sinfo_flags & SCTP_SACK_IMMEDIATELY)) 276b93d6471SWei Yongjun frag |= SCTP_DATA_SACK_IMM; 277b93d6471SWei Yongjun } 278b93d6471SWei Yongjun 2791da177e4SLinus Torvalds chunk = sctp_make_datafrag_empty(asoc, sinfo, len, frag, 0); 2801da177e4SLinus Torvalds 2811da177e4SLinus Torvalds if (!chunk) 2821da177e4SLinus Torvalds goto errout; 2831da177e4SLinus Torvalds err = sctp_user_addto_chunk(chunk, offset, len, msgh->msg_iov); 2841da177e4SLinus Torvalds if (err < 0) 2851da177e4SLinus Torvalds goto errout; 2861da177e4SLinus Torvalds 2871da177e4SLinus Torvalds offset += len; 2881da177e4SLinus Torvalds 2891da177e4SLinus Torvalds /* Put the chunk->skb back into the form expected by send. */ 2901da177e4SLinus Torvalds __skb_pull(chunk->skb, (__u8 *)chunk->chunk_hdr 2911da177e4SLinus Torvalds - (__u8 *)chunk->skb->data); 2921da177e4SLinus Torvalds 2931da177e4SLinus Torvalds sctp_datamsg_assign(msg, chunk); 2941da177e4SLinus Torvalds list_add_tail(&chunk->frag_list, &msg->chunks); 2951da177e4SLinus Torvalds 2961da177e4SLinus Torvalds /* The first chunk, the first chunk was likely short 2971da177e4SLinus Torvalds * to allow bundling, so reset to full size. 2981da177e4SLinus Torvalds */ 2991da177e4SLinus Torvalds if (0 == i) 3001da177e4SLinus Torvalds len = max; 3011da177e4SLinus Torvalds } 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds /* .. now the leftover bytes. */ 3041da177e4SLinus Torvalds if (over) { 3051da177e4SLinus Torvalds if (!whole) 3061da177e4SLinus Torvalds frag = SCTP_DATA_NOT_FRAG; 3071da177e4SLinus Torvalds else 3081da177e4SLinus Torvalds frag = SCTP_DATA_LAST_FRAG; 3091da177e4SLinus Torvalds 310b93d6471SWei Yongjun if ((sinfo->sinfo_flags & SCTP_EOF) || 311b93d6471SWei Yongjun (sinfo->sinfo_flags & SCTP_SACK_IMMEDIATELY)) 312b93d6471SWei Yongjun frag |= SCTP_DATA_SACK_IMM; 313b93d6471SWei Yongjun 3141da177e4SLinus Torvalds chunk = sctp_make_datafrag_empty(asoc, sinfo, over, frag, 0); 3151da177e4SLinus Torvalds 3161da177e4SLinus Torvalds if (!chunk) 3171da177e4SLinus Torvalds goto errout; 3181da177e4SLinus Torvalds 3191da177e4SLinus Torvalds err = sctp_user_addto_chunk(chunk, offset, over,msgh->msg_iov); 3201da177e4SLinus Torvalds 3211da177e4SLinus Torvalds /* Put the chunk->skb back into the form expected by send. */ 3221da177e4SLinus Torvalds __skb_pull(chunk->skb, (__u8 *)chunk->chunk_hdr 3231da177e4SLinus Torvalds - (__u8 *)chunk->skb->data); 3241da177e4SLinus Torvalds if (err < 0) 3251da177e4SLinus Torvalds goto errout; 3261da177e4SLinus Torvalds 3271da177e4SLinus Torvalds sctp_datamsg_assign(msg, chunk); 3281da177e4SLinus Torvalds list_add_tail(&chunk->frag_list, &msg->chunks); 3291da177e4SLinus Torvalds } 3301da177e4SLinus Torvalds 3311da177e4SLinus Torvalds return msg; 3321da177e4SLinus Torvalds 3331da177e4SLinus Torvalds errout: 3341da177e4SLinus Torvalds list_for_each_safe(pos, temp, &msg->chunks) { 3351da177e4SLinus Torvalds list_del_init(pos); 3361da177e4SLinus Torvalds chunk = list_entry(pos, struct sctp_chunk, frag_list); 3371da177e4SLinus Torvalds sctp_chunk_free(chunk); 3381da177e4SLinus Torvalds } 33980445cfbSFlorian Westphal sctp_datamsg_put(msg); 3401da177e4SLinus Torvalds return NULL; 3411da177e4SLinus Torvalds } 3421da177e4SLinus Torvalds 3431da177e4SLinus Torvalds /* Check whether this message has expired. */ 3441da177e4SLinus Torvalds int sctp_chunk_abandoned(struct sctp_chunk *chunk) 3451da177e4SLinus Torvalds { 3461da177e4SLinus Torvalds struct sctp_datamsg *msg = chunk->msg; 3471da177e4SLinus Torvalds 3481da177e4SLinus Torvalds if (!msg->can_abandon) 3491da177e4SLinus Torvalds return 0; 3501da177e4SLinus Torvalds 3511da177e4SLinus Torvalds if (time_after(jiffies, msg->expires_at)) 3521da177e4SLinus Torvalds return 1; 3531da177e4SLinus Torvalds 3541da177e4SLinus Torvalds return 0; 3551da177e4SLinus Torvalds } 3561da177e4SLinus Torvalds 3571da177e4SLinus Torvalds /* This chunk (and consequently entire message) has failed in its sending. */ 3581da177e4SLinus Torvalds void sctp_chunk_fail(struct sctp_chunk *chunk, int error) 3591da177e4SLinus Torvalds { 3601da177e4SLinus Torvalds chunk->msg->send_failed = 1; 3611da177e4SLinus Torvalds chunk->msg->send_error = error; 3621da177e4SLinus Torvalds } 363