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 40*145ce502SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 41*145ce502SJoe Perches 421da177e4SLinus Torvalds #include <linux/types.h> 431da177e4SLinus Torvalds #include <linux/kernel.h> 441da177e4SLinus Torvalds #include <linux/net.h> 451da177e4SLinus Torvalds #include <linux/inet.h> 461da177e4SLinus Torvalds #include <linux/skbuff.h> 475a0e3ad6STejun Heo #include <linux/slab.h> 481da177e4SLinus Torvalds #include <net/sock.h> 491da177e4SLinus Torvalds #include <net/sctp/sctp.h> 501da177e4SLinus Torvalds #include <net/sctp/sm.h> 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds /* This file is mostly in anticipation of future work, but initially 531da177e4SLinus Torvalds * populate with fragment tracking for an outbound message. 541da177e4SLinus Torvalds */ 551da177e4SLinus Torvalds 561da177e4SLinus Torvalds /* Initialize datamsg from memory. */ 571da177e4SLinus Torvalds static void sctp_datamsg_init(struct sctp_datamsg *msg) 581da177e4SLinus Torvalds { 591da177e4SLinus Torvalds atomic_set(&msg->refcnt, 1); 601da177e4SLinus Torvalds msg->send_failed = 0; 611da177e4SLinus Torvalds msg->send_error = 0; 621da177e4SLinus Torvalds msg->can_abandon = 0; 630e3aef8dSVlad Yasevich msg->can_delay = 1; 641da177e4SLinus Torvalds msg->expires_at = 0; 651da177e4SLinus Torvalds INIT_LIST_HEAD(&msg->chunks); 661da177e4SLinus Torvalds } 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds /* Allocate and initialize datamsg. */ 69dd0fc66fSAl Viro SCTP_STATIC struct sctp_datamsg *sctp_datamsg_new(gfp_t gfp) 701da177e4SLinus Torvalds { 711da177e4SLinus Torvalds struct sctp_datamsg *msg; 721da177e4SLinus Torvalds msg = kmalloc(sizeof(struct sctp_datamsg), gfp); 73e8c38751SLi Zefan if (msg) { 741da177e4SLinus Torvalds sctp_datamsg_init(msg); 751da177e4SLinus Torvalds SCTP_DBG_OBJCNT_INC(datamsg); 76e8c38751SLi Zefan } 771da177e4SLinus Torvalds return msg; 781da177e4SLinus Torvalds } 791da177e4SLinus Torvalds 809c5c62beSVlad Yasevich void sctp_datamsg_free(struct sctp_datamsg *msg) 819c5c62beSVlad Yasevich { 829c5c62beSVlad Yasevich struct sctp_chunk *chunk; 839c5c62beSVlad Yasevich 849c5c62beSVlad Yasevich /* This doesn't have to be a _safe vairant because 859c5c62beSVlad Yasevich * sctp_chunk_free() only drops the refs. 869c5c62beSVlad Yasevich */ 879c5c62beSVlad Yasevich list_for_each_entry(chunk, &msg->chunks, frag_list) 889c5c62beSVlad Yasevich sctp_chunk_free(chunk); 899c5c62beSVlad Yasevich 909c5c62beSVlad Yasevich sctp_datamsg_put(msg); 919c5c62beSVlad Yasevich } 929c5c62beSVlad Yasevich 931da177e4SLinus Torvalds /* Final destructruction of datamsg memory. */ 941da177e4SLinus Torvalds static void sctp_datamsg_destroy(struct sctp_datamsg *msg) 951da177e4SLinus Torvalds { 961da177e4SLinus Torvalds struct list_head *pos, *temp; 971da177e4SLinus Torvalds struct sctp_chunk *chunk; 981da177e4SLinus Torvalds struct sctp_sock *sp; 991da177e4SLinus Torvalds struct sctp_ulpevent *ev; 1001da177e4SLinus Torvalds struct sctp_association *asoc = NULL; 1011da177e4SLinus Torvalds int error = 0, notify; 1021da177e4SLinus Torvalds 1031da177e4SLinus Torvalds /* If we failed, we may need to notify. */ 1041da177e4SLinus Torvalds notify = msg->send_failed ? -1 : 0; 1051da177e4SLinus Torvalds 1061da177e4SLinus Torvalds /* Release all references. */ 1071da177e4SLinus Torvalds list_for_each_safe(pos, temp, &msg->chunks) { 1081da177e4SLinus Torvalds list_del_init(pos); 1091da177e4SLinus Torvalds chunk = list_entry(pos, struct sctp_chunk, frag_list); 1101da177e4SLinus Torvalds /* Check whether we _really_ need to notify. */ 1111da177e4SLinus Torvalds if (notify < 0) { 1121da177e4SLinus Torvalds asoc = chunk->asoc; 1131da177e4SLinus Torvalds if (msg->send_error) 1141da177e4SLinus Torvalds error = msg->send_error; 1151da177e4SLinus Torvalds else 1161da177e4SLinus Torvalds error = asoc->outqueue.error; 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds sp = sctp_sk(asoc->base.sk); 1191da177e4SLinus Torvalds notify = sctp_ulpevent_type_enabled(SCTP_SEND_FAILED, 1201da177e4SLinus Torvalds &sp->subscribe); 1211da177e4SLinus Torvalds } 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds /* Generate a SEND FAILED event only if enabled. */ 1241da177e4SLinus Torvalds if (notify > 0) { 1251da177e4SLinus Torvalds int sent; 1261da177e4SLinus Torvalds if (chunk->has_tsn) 1271da177e4SLinus Torvalds sent = SCTP_DATA_SENT; 1281da177e4SLinus Torvalds else 1291da177e4SLinus Torvalds sent = SCTP_DATA_UNSENT; 1301da177e4SLinus Torvalds 1311da177e4SLinus Torvalds ev = sctp_ulpevent_make_send_failed(asoc, chunk, sent, 1321da177e4SLinus Torvalds error, GFP_ATOMIC); 1331da177e4SLinus Torvalds if (ev) 1341da177e4SLinus Torvalds sctp_ulpq_tail_event(&asoc->ulpq, ev); 1351da177e4SLinus Torvalds } 1361da177e4SLinus Torvalds 1371da177e4SLinus Torvalds sctp_chunk_put(chunk); 1381da177e4SLinus Torvalds } 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds SCTP_DBG_OBJCNT_DEC(datamsg); 1411da177e4SLinus Torvalds kfree(msg); 1421da177e4SLinus Torvalds } 1431da177e4SLinus Torvalds 1441da177e4SLinus Torvalds /* Hold a reference. */ 1451da177e4SLinus Torvalds static void sctp_datamsg_hold(struct sctp_datamsg *msg) 1461da177e4SLinus Torvalds { 1471da177e4SLinus Torvalds atomic_inc(&msg->refcnt); 1481da177e4SLinus Torvalds } 1491da177e4SLinus Torvalds 1501da177e4SLinus Torvalds /* Release a reference. */ 1511da177e4SLinus Torvalds void sctp_datamsg_put(struct sctp_datamsg *msg) 1521da177e4SLinus Torvalds { 1531da177e4SLinus Torvalds if (atomic_dec_and_test(&msg->refcnt)) 1541da177e4SLinus Torvalds sctp_datamsg_destroy(msg); 1551da177e4SLinus Torvalds } 1561da177e4SLinus Torvalds 1571da177e4SLinus Torvalds /* Assign a chunk to this datamsg. */ 1581da177e4SLinus Torvalds static void sctp_datamsg_assign(struct sctp_datamsg *msg, struct sctp_chunk *chunk) 1591da177e4SLinus Torvalds { 1601da177e4SLinus Torvalds sctp_datamsg_hold(msg); 1611da177e4SLinus Torvalds chunk->msg = msg; 1621da177e4SLinus Torvalds } 1631da177e4SLinus Torvalds 1641da177e4SLinus Torvalds 1651da177e4SLinus Torvalds /* A data chunk can have a maximum payload of (2^16 - 20). Break 1661da177e4SLinus Torvalds * down any such message into smaller chunks. Opportunistically, fragment 1671da177e4SLinus Torvalds * the chunks down to the current MTU constraints. We may get refragmented 1681da177e4SLinus Torvalds * later if the PMTU changes, but it is _much better_ to fragment immediately 1691da177e4SLinus Torvalds * with a reasonable guess than always doing our fragmentation on the 1701da177e4SLinus Torvalds * soft-interrupt. 1711da177e4SLinus Torvalds */ 1721da177e4SLinus Torvalds struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc, 1731da177e4SLinus Torvalds struct sctp_sndrcvinfo *sinfo, 1741da177e4SLinus Torvalds struct msghdr *msgh, int msg_len) 1751da177e4SLinus Torvalds { 1761da177e4SLinus Torvalds int max, whole, i, offset, over, err; 1771da177e4SLinus Torvalds int len, first_len; 1783e62abf9SVlad Yasevich int max_data; 1791da177e4SLinus Torvalds struct sctp_chunk *chunk; 1801da177e4SLinus Torvalds struct sctp_datamsg *msg; 1811da177e4SLinus Torvalds struct list_head *pos, *temp; 1821da177e4SLinus Torvalds __u8 frag; 1831da177e4SLinus Torvalds 1841da177e4SLinus Torvalds msg = sctp_datamsg_new(GFP_KERNEL); 1851da177e4SLinus Torvalds if (!msg) 1861da177e4SLinus Torvalds return NULL; 1871da177e4SLinus Torvalds 1881da177e4SLinus Torvalds /* Note: Calculate this outside of the loop, so that all fragments 1891da177e4SLinus Torvalds * have the same expiration. 1901da177e4SLinus Torvalds */ 1911da177e4SLinus Torvalds if (sinfo->sinfo_timetolive) { 1921da177e4SLinus Torvalds /* sinfo_timetolive is in milliseconds */ 1931da177e4SLinus Torvalds msg->expires_at = jiffies + 1941da177e4SLinus Torvalds msecs_to_jiffies(sinfo->sinfo_timetolive); 1951da177e4SLinus Torvalds msg->can_abandon = 1; 1961da177e4SLinus Torvalds SCTP_DEBUG_PRINTK("%s: msg:%p expires_at: %ld jiffies:%ld\n", 1970dc47877SHarvey Harrison __func__, msg, msg->expires_at, jiffies); 1981da177e4SLinus Torvalds } 1991da177e4SLinus Torvalds 2003e62abf9SVlad Yasevich /* This is the biggest possible DATA chunk that can fit into 2013e62abf9SVlad Yasevich * the packet 2023e62abf9SVlad Yasevich */ 2033e62abf9SVlad Yasevich max_data = asoc->pathmtu - 2043e62abf9SVlad Yasevich sctp_sk(asoc->base.sk)->pf->af->net_header_len - 2053e62abf9SVlad Yasevich sizeof(struct sctphdr) - sizeof(struct sctp_data_chunk); 2061da177e4SLinus Torvalds 2073e62abf9SVlad Yasevich max = asoc->frag_point; 2084cd57c80SVlad Yasevich /* If the the peer requested that we authenticate DATA chunks 2094cd57c80SVlad Yasevich * we need to accound for bundling of the AUTH chunks along with 2104cd57c80SVlad Yasevich * DATA. 2114cd57c80SVlad Yasevich */ 2124cd57c80SVlad Yasevich if (sctp_auth_send_cid(SCTP_CID_DATA, asoc)) { 2134cd57c80SVlad Yasevich struct sctp_hmac *hmac_desc = sctp_auth_asoc_get_hmac(asoc); 2144cd57c80SVlad Yasevich 2154cd57c80SVlad Yasevich if (hmac_desc) 2163e62abf9SVlad Yasevich max_data -= WORD_ROUND(sizeof(sctp_auth_chunk_t) + 2174cd57c80SVlad Yasevich hmac_desc->hmac_len); 2184cd57c80SVlad Yasevich } 2194cd57c80SVlad Yasevich 2203e62abf9SVlad Yasevich /* Now, check if we need to reduce our max */ 2213e62abf9SVlad Yasevich if (max > max_data) 2223e62abf9SVlad Yasevich max = max_data; 2233e62abf9SVlad Yasevich 2241da177e4SLinus Torvalds whole = 0; 2251da177e4SLinus Torvalds first_len = max; 2261da177e4SLinus Torvalds 2275d7ff261SVlad Yasevich /* Check to see if we have a pending SACK and try to let it be bundled 2285d7ff261SVlad Yasevich * with this message. Do this if we don't have any data queued already. 2295d7ff261SVlad Yasevich * To check that, look at out_qlen and retransmit list. 2305d7ff261SVlad Yasevich * NOTE: we will not reduce to account for SACK, if the message would 2315d7ff261SVlad Yasevich * not have been fragmented. 2325d7ff261SVlad Yasevich */ 2335d7ff261SVlad Yasevich if (timer_pending(&asoc->timers[SCTP_EVENT_TIMEOUT_SACK]) && 2345d7ff261SVlad Yasevich asoc->outqueue.out_qlen == 0 && 2355d7ff261SVlad Yasevich list_empty(&asoc->outqueue.retransmit) && 2365d7ff261SVlad Yasevich msg_len > max) 2375d7ff261SVlad Yasevich max_data -= WORD_ROUND(sizeof(sctp_sack_chunk_t)); 2385d7ff261SVlad Yasevich 2391da177e4SLinus Torvalds /* Encourage Cookie-ECHO bundling. */ 2405d7ff261SVlad Yasevich if (asoc->state < SCTP_STATE_COOKIE_ECHOED) 2413e62abf9SVlad Yasevich max_data -= SCTP_ARBITRARY_COOKIE_ECHO_LEN; 2421da177e4SLinus Torvalds 2435d7ff261SVlad Yasevich /* Now that we adjusted completely, reset first_len */ 2443e62abf9SVlad Yasevich if (first_len > max_data) 2453e62abf9SVlad Yasevich first_len = max_data; 2463e62abf9SVlad Yasevich 2473e62abf9SVlad Yasevich /* Account for a different sized first fragment */ 2483e62abf9SVlad Yasevich if (msg_len >= first_len) { 2491da177e4SLinus Torvalds msg_len -= first_len; 2501da177e4SLinus Torvalds whole = 1; 2510e3aef8dSVlad Yasevich msg->can_delay = 0; 2521da177e4SLinus Torvalds } 2531da177e4SLinus Torvalds 2541da177e4SLinus Torvalds /* How many full sized? How many bytes leftover? */ 2551da177e4SLinus Torvalds whole += msg_len / max; 2561da177e4SLinus Torvalds over = msg_len % max; 2571da177e4SLinus Torvalds offset = 0; 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds if ((whole > 1) || (whole && over)) 2601da177e4SLinus Torvalds SCTP_INC_STATS_USER(SCTP_MIB_FRAGUSRMSGS); 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds /* Create chunks for all the full sized DATA chunks. */ 2631da177e4SLinus Torvalds for (i=0, len=first_len; i < whole; i++) { 2641da177e4SLinus Torvalds frag = SCTP_DATA_MIDDLE_FRAG; 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds if (0 == i) 2671da177e4SLinus Torvalds frag |= SCTP_DATA_FIRST_FRAG; 2681da177e4SLinus Torvalds 269b93d6471SWei Yongjun if ((i == (whole - 1)) && !over) { 2701da177e4SLinus Torvalds frag |= SCTP_DATA_LAST_FRAG; 2711da177e4SLinus Torvalds 272b93d6471SWei Yongjun /* The application requests to set the I-bit of the 273b93d6471SWei Yongjun * last DATA chunk of a user message when providing 274b93d6471SWei Yongjun * the user message to the SCTP implementation. 275b93d6471SWei Yongjun */ 276b93d6471SWei Yongjun if ((sinfo->sinfo_flags & SCTP_EOF) || 277b93d6471SWei Yongjun (sinfo->sinfo_flags & SCTP_SACK_IMMEDIATELY)) 278b93d6471SWei Yongjun frag |= SCTP_DATA_SACK_IMM; 279b93d6471SWei Yongjun } 280b93d6471SWei Yongjun 2811da177e4SLinus Torvalds chunk = sctp_make_datafrag_empty(asoc, sinfo, len, frag, 0); 2821da177e4SLinus Torvalds 2831da177e4SLinus Torvalds if (!chunk) 2841da177e4SLinus Torvalds goto errout; 2851da177e4SLinus Torvalds err = sctp_user_addto_chunk(chunk, offset, len, msgh->msg_iov); 2861da177e4SLinus Torvalds if (err < 0) 2871da177e4SLinus Torvalds goto errout; 2881da177e4SLinus Torvalds 2891da177e4SLinus Torvalds offset += len; 2901da177e4SLinus Torvalds 2911da177e4SLinus Torvalds /* Put the chunk->skb back into the form expected by send. */ 2921da177e4SLinus Torvalds __skb_pull(chunk->skb, (__u8 *)chunk->chunk_hdr 2931da177e4SLinus Torvalds - (__u8 *)chunk->skb->data); 2941da177e4SLinus Torvalds 2951da177e4SLinus Torvalds sctp_datamsg_assign(msg, chunk); 2961da177e4SLinus Torvalds list_add_tail(&chunk->frag_list, &msg->chunks); 2971da177e4SLinus Torvalds 2981da177e4SLinus Torvalds /* The first chunk, the first chunk was likely short 2991da177e4SLinus Torvalds * to allow bundling, so reset to full size. 3001da177e4SLinus Torvalds */ 3011da177e4SLinus Torvalds if (0 == i) 3021da177e4SLinus Torvalds len = max; 3031da177e4SLinus Torvalds } 3041da177e4SLinus Torvalds 3051da177e4SLinus Torvalds /* .. now the leftover bytes. */ 3061da177e4SLinus Torvalds if (over) { 3071da177e4SLinus Torvalds if (!whole) 3081da177e4SLinus Torvalds frag = SCTP_DATA_NOT_FRAG; 3091da177e4SLinus Torvalds else 3101da177e4SLinus Torvalds frag = SCTP_DATA_LAST_FRAG; 3111da177e4SLinus Torvalds 312b93d6471SWei Yongjun if ((sinfo->sinfo_flags & SCTP_EOF) || 313b93d6471SWei Yongjun (sinfo->sinfo_flags & SCTP_SACK_IMMEDIATELY)) 314b93d6471SWei Yongjun frag |= SCTP_DATA_SACK_IMM; 315b93d6471SWei Yongjun 3161da177e4SLinus Torvalds chunk = sctp_make_datafrag_empty(asoc, sinfo, over, frag, 0); 3171da177e4SLinus Torvalds 3181da177e4SLinus Torvalds if (!chunk) 3191da177e4SLinus Torvalds goto errout; 3201da177e4SLinus Torvalds 3211da177e4SLinus Torvalds err = sctp_user_addto_chunk(chunk, offset, over,msgh->msg_iov); 3221da177e4SLinus Torvalds 3231da177e4SLinus Torvalds /* Put the chunk->skb back into the form expected by send. */ 3241da177e4SLinus Torvalds __skb_pull(chunk->skb, (__u8 *)chunk->chunk_hdr 3251da177e4SLinus Torvalds - (__u8 *)chunk->skb->data); 3261da177e4SLinus Torvalds if (err < 0) 3271da177e4SLinus Torvalds goto errout; 3281da177e4SLinus Torvalds 3291da177e4SLinus Torvalds sctp_datamsg_assign(msg, chunk); 3301da177e4SLinus Torvalds list_add_tail(&chunk->frag_list, &msg->chunks); 3311da177e4SLinus Torvalds } 3321da177e4SLinus Torvalds 3331da177e4SLinus Torvalds return msg; 3341da177e4SLinus Torvalds 3351da177e4SLinus Torvalds errout: 3361da177e4SLinus Torvalds list_for_each_safe(pos, temp, &msg->chunks) { 3371da177e4SLinus Torvalds list_del_init(pos); 3381da177e4SLinus Torvalds chunk = list_entry(pos, struct sctp_chunk, frag_list); 3391da177e4SLinus Torvalds sctp_chunk_free(chunk); 3401da177e4SLinus Torvalds } 34180445cfbSFlorian Westphal sctp_datamsg_put(msg); 3421da177e4SLinus Torvalds return NULL; 3431da177e4SLinus Torvalds } 3441da177e4SLinus Torvalds 3451da177e4SLinus Torvalds /* Check whether this message has expired. */ 3461da177e4SLinus Torvalds int sctp_chunk_abandoned(struct sctp_chunk *chunk) 3471da177e4SLinus Torvalds { 3481da177e4SLinus Torvalds struct sctp_datamsg *msg = chunk->msg; 3491da177e4SLinus Torvalds 3501da177e4SLinus Torvalds if (!msg->can_abandon) 3511da177e4SLinus Torvalds return 0; 3521da177e4SLinus Torvalds 3531da177e4SLinus Torvalds if (time_after(jiffies, msg->expires_at)) 3541da177e4SLinus Torvalds return 1; 3551da177e4SLinus Torvalds 3561da177e4SLinus Torvalds return 0; 3571da177e4SLinus Torvalds } 3581da177e4SLinus Torvalds 3591da177e4SLinus Torvalds /* This chunk (and consequently entire message) has failed in its sending. */ 3601da177e4SLinus Torvalds void sctp_chunk_fail(struct sctp_chunk *chunk, int error) 3611da177e4SLinus Torvalds { 3621da177e4SLinus Torvalds chunk->msg->send_failed = 1; 3631da177e4SLinus Torvalds chunk->msg->send_error = error; 3641da177e4SLinus Torvalds } 365