1*60c778b2SVlad Yasevich /* SCTP kernel implementation 21da177e4SLinus Torvalds * (C) Copyright IBM Corp. 2003, 2004 31da177e4SLinus Torvalds * 4*60c778b2SVlad 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 * 8*60c778b2SVlad 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 * 14*60c778b2SVlad 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> 451da177e4SLinus Torvalds #include <net/sock.h> 461da177e4SLinus Torvalds #include <net/sctp/sctp.h> 471da177e4SLinus Torvalds #include <net/sctp/sm.h> 481da177e4SLinus Torvalds 491da177e4SLinus Torvalds /* This file is mostly in anticipation of future work, but initially 501da177e4SLinus Torvalds * populate with fragment tracking for an outbound message. 511da177e4SLinus Torvalds */ 521da177e4SLinus Torvalds 531da177e4SLinus Torvalds /* Initialize datamsg from memory. */ 541da177e4SLinus Torvalds static void sctp_datamsg_init(struct sctp_datamsg *msg) 551da177e4SLinus Torvalds { 561da177e4SLinus Torvalds atomic_set(&msg->refcnt, 1); 571da177e4SLinus Torvalds msg->send_failed = 0; 581da177e4SLinus Torvalds msg->send_error = 0; 591da177e4SLinus Torvalds msg->can_abandon = 0; 601da177e4SLinus Torvalds msg->expires_at = 0; 611da177e4SLinus Torvalds INIT_LIST_HEAD(&msg->chunks); 621da177e4SLinus Torvalds } 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds /* Allocate and initialize datamsg. */ 65dd0fc66fSAl Viro SCTP_STATIC struct sctp_datamsg *sctp_datamsg_new(gfp_t gfp) 661da177e4SLinus Torvalds { 671da177e4SLinus Torvalds struct sctp_datamsg *msg; 681da177e4SLinus Torvalds msg = kmalloc(sizeof(struct sctp_datamsg), gfp); 691da177e4SLinus Torvalds if (msg) 701da177e4SLinus Torvalds sctp_datamsg_init(msg); 711da177e4SLinus Torvalds SCTP_DBG_OBJCNT_INC(datamsg); 721da177e4SLinus Torvalds return msg; 731da177e4SLinus Torvalds } 741da177e4SLinus Torvalds 751da177e4SLinus Torvalds /* Final destructruction of datamsg memory. */ 761da177e4SLinus Torvalds static void sctp_datamsg_destroy(struct sctp_datamsg *msg) 771da177e4SLinus Torvalds { 781da177e4SLinus Torvalds struct list_head *pos, *temp; 791da177e4SLinus Torvalds struct sctp_chunk *chunk; 801da177e4SLinus Torvalds struct sctp_sock *sp; 811da177e4SLinus Torvalds struct sctp_ulpevent *ev; 821da177e4SLinus Torvalds struct sctp_association *asoc = NULL; 831da177e4SLinus Torvalds int error = 0, notify; 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds /* If we failed, we may need to notify. */ 861da177e4SLinus Torvalds notify = msg->send_failed ? -1 : 0; 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds /* Release all references. */ 891da177e4SLinus Torvalds list_for_each_safe(pos, temp, &msg->chunks) { 901da177e4SLinus Torvalds list_del_init(pos); 911da177e4SLinus Torvalds chunk = list_entry(pos, struct sctp_chunk, frag_list); 921da177e4SLinus Torvalds /* Check whether we _really_ need to notify. */ 931da177e4SLinus Torvalds if (notify < 0) { 941da177e4SLinus Torvalds asoc = chunk->asoc; 951da177e4SLinus Torvalds if (msg->send_error) 961da177e4SLinus Torvalds error = msg->send_error; 971da177e4SLinus Torvalds else 981da177e4SLinus Torvalds error = asoc->outqueue.error; 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds sp = sctp_sk(asoc->base.sk); 1011da177e4SLinus Torvalds notify = sctp_ulpevent_type_enabled(SCTP_SEND_FAILED, 1021da177e4SLinus Torvalds &sp->subscribe); 1031da177e4SLinus Torvalds } 1041da177e4SLinus Torvalds 1051da177e4SLinus Torvalds /* Generate a SEND FAILED event only if enabled. */ 1061da177e4SLinus Torvalds if (notify > 0) { 1071da177e4SLinus Torvalds int sent; 1081da177e4SLinus Torvalds if (chunk->has_tsn) 1091da177e4SLinus Torvalds sent = SCTP_DATA_SENT; 1101da177e4SLinus Torvalds else 1111da177e4SLinus Torvalds sent = SCTP_DATA_UNSENT; 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds ev = sctp_ulpevent_make_send_failed(asoc, chunk, sent, 1141da177e4SLinus Torvalds error, GFP_ATOMIC); 1151da177e4SLinus Torvalds if (ev) 1161da177e4SLinus Torvalds sctp_ulpq_tail_event(&asoc->ulpq, ev); 1171da177e4SLinus Torvalds } 1181da177e4SLinus Torvalds 1191da177e4SLinus Torvalds sctp_chunk_put(chunk); 1201da177e4SLinus Torvalds } 1211da177e4SLinus Torvalds 1221da177e4SLinus Torvalds SCTP_DBG_OBJCNT_DEC(datamsg); 1231da177e4SLinus Torvalds kfree(msg); 1241da177e4SLinus Torvalds } 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds /* Hold a reference. */ 1271da177e4SLinus Torvalds static void sctp_datamsg_hold(struct sctp_datamsg *msg) 1281da177e4SLinus Torvalds { 1291da177e4SLinus Torvalds atomic_inc(&msg->refcnt); 1301da177e4SLinus Torvalds } 1311da177e4SLinus Torvalds 1321da177e4SLinus Torvalds /* Release a reference. */ 1331da177e4SLinus Torvalds void sctp_datamsg_put(struct sctp_datamsg *msg) 1341da177e4SLinus Torvalds { 1351da177e4SLinus Torvalds if (atomic_dec_and_test(&msg->refcnt)) 1361da177e4SLinus Torvalds sctp_datamsg_destroy(msg); 1371da177e4SLinus Torvalds } 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds /* Free a message. Really just give up a reference, the 1401da177e4SLinus Torvalds * really free happens in sctp_datamsg_destroy(). 1411da177e4SLinus Torvalds */ 1421da177e4SLinus Torvalds void sctp_datamsg_free(struct sctp_datamsg *msg) 1431da177e4SLinus Torvalds { 1441da177e4SLinus Torvalds sctp_datamsg_put(msg); 1451da177e4SLinus Torvalds } 1461da177e4SLinus Torvalds 1471da177e4SLinus Torvalds /* Hold on to all the fragments until all chunks have been sent. */ 1481da177e4SLinus Torvalds void sctp_datamsg_track(struct sctp_chunk *chunk) 1491da177e4SLinus Torvalds { 1501da177e4SLinus Torvalds sctp_chunk_hold(chunk); 1511da177e4SLinus Torvalds } 1521da177e4SLinus Torvalds 1531da177e4SLinus Torvalds /* Assign a chunk to this datamsg. */ 1541da177e4SLinus Torvalds static void sctp_datamsg_assign(struct sctp_datamsg *msg, struct sctp_chunk *chunk) 1551da177e4SLinus Torvalds { 1561da177e4SLinus Torvalds sctp_datamsg_hold(msg); 1571da177e4SLinus Torvalds chunk->msg = msg; 1581da177e4SLinus Torvalds } 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds /* A data chunk can have a maximum payload of (2^16 - 20). Break 1621da177e4SLinus Torvalds * down any such message into smaller chunks. Opportunistically, fragment 1631da177e4SLinus Torvalds * the chunks down to the current MTU constraints. We may get refragmented 1641da177e4SLinus Torvalds * later if the PMTU changes, but it is _much better_ to fragment immediately 1651da177e4SLinus Torvalds * with a reasonable guess than always doing our fragmentation on the 1661da177e4SLinus Torvalds * soft-interrupt. 1671da177e4SLinus Torvalds */ 1681da177e4SLinus Torvalds struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc, 1691da177e4SLinus Torvalds struct sctp_sndrcvinfo *sinfo, 1701da177e4SLinus Torvalds struct msghdr *msgh, int msg_len) 1711da177e4SLinus Torvalds { 1721da177e4SLinus Torvalds int max, whole, i, offset, over, err; 1731da177e4SLinus Torvalds int len, first_len; 1741da177e4SLinus Torvalds struct sctp_chunk *chunk; 1751da177e4SLinus Torvalds struct sctp_datamsg *msg; 1761da177e4SLinus Torvalds struct list_head *pos, *temp; 1771da177e4SLinus Torvalds __u8 frag; 1781da177e4SLinus Torvalds 1791da177e4SLinus Torvalds msg = sctp_datamsg_new(GFP_KERNEL); 1801da177e4SLinus Torvalds if (!msg) 1811da177e4SLinus Torvalds return NULL; 1821da177e4SLinus Torvalds 1831da177e4SLinus Torvalds /* Note: Calculate this outside of the loop, so that all fragments 1841da177e4SLinus Torvalds * have the same expiration. 1851da177e4SLinus Torvalds */ 1861da177e4SLinus Torvalds if (sinfo->sinfo_timetolive) { 1871da177e4SLinus Torvalds /* sinfo_timetolive is in milliseconds */ 1881da177e4SLinus Torvalds msg->expires_at = jiffies + 1891da177e4SLinus Torvalds msecs_to_jiffies(sinfo->sinfo_timetolive); 1901da177e4SLinus Torvalds msg->can_abandon = 1; 1911da177e4SLinus Torvalds SCTP_DEBUG_PRINTK("%s: msg:%p expires_at: %ld jiffies:%ld\n", 1921da177e4SLinus Torvalds __FUNCTION__, msg, msg->expires_at, jiffies); 1931da177e4SLinus Torvalds } 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds max = asoc->frag_point; 1961da177e4SLinus Torvalds 1974cd57c80SVlad Yasevich /* If the the peer requested that we authenticate DATA chunks 1984cd57c80SVlad Yasevich * we need to accound for bundling of the AUTH chunks along with 1994cd57c80SVlad Yasevich * DATA. 2004cd57c80SVlad Yasevich */ 2014cd57c80SVlad Yasevich if (sctp_auth_send_cid(SCTP_CID_DATA, asoc)) { 2024cd57c80SVlad Yasevich struct sctp_hmac *hmac_desc = sctp_auth_asoc_get_hmac(asoc); 2034cd57c80SVlad Yasevich 2044cd57c80SVlad Yasevich if (hmac_desc) 2054cd57c80SVlad Yasevich max -= WORD_ROUND(sizeof(sctp_auth_chunk_t) + 2064cd57c80SVlad Yasevich hmac_desc->hmac_len); 2074cd57c80SVlad Yasevich } 2084cd57c80SVlad Yasevich 2091da177e4SLinus Torvalds whole = 0; 2101da177e4SLinus Torvalds first_len = max; 2111da177e4SLinus Torvalds 2121da177e4SLinus Torvalds /* Encourage Cookie-ECHO bundling. */ 2131da177e4SLinus Torvalds if (asoc->state < SCTP_STATE_COOKIE_ECHOED) { 2141da177e4SLinus Torvalds whole = msg_len / (max - SCTP_ARBITRARY_COOKIE_ECHO_LEN); 2151da177e4SLinus Torvalds 2161da177e4SLinus Torvalds /* Account for the DATA to be bundled with the COOKIE-ECHO. */ 2171da177e4SLinus Torvalds if (whole) { 2181da177e4SLinus Torvalds first_len = max - SCTP_ARBITRARY_COOKIE_ECHO_LEN; 2191da177e4SLinus Torvalds msg_len -= first_len; 2201da177e4SLinus Torvalds whole = 1; 2211da177e4SLinus Torvalds } 2221da177e4SLinus Torvalds } 2231da177e4SLinus Torvalds 2241da177e4SLinus Torvalds /* How many full sized? How many bytes leftover? */ 2251da177e4SLinus Torvalds whole += msg_len / max; 2261da177e4SLinus Torvalds over = msg_len % max; 2271da177e4SLinus Torvalds offset = 0; 2281da177e4SLinus Torvalds 2291da177e4SLinus Torvalds if ((whole > 1) || (whole && over)) 2301da177e4SLinus Torvalds SCTP_INC_STATS_USER(SCTP_MIB_FRAGUSRMSGS); 2311da177e4SLinus Torvalds 2321da177e4SLinus Torvalds /* Create chunks for all the full sized DATA chunks. */ 2331da177e4SLinus Torvalds for (i=0, len=first_len; i < whole; i++) { 2341da177e4SLinus Torvalds frag = SCTP_DATA_MIDDLE_FRAG; 2351da177e4SLinus Torvalds 2361da177e4SLinus Torvalds if (0 == i) 2371da177e4SLinus Torvalds frag |= SCTP_DATA_FIRST_FRAG; 2381da177e4SLinus Torvalds 2391da177e4SLinus Torvalds if ((i == (whole - 1)) && !over) 2401da177e4SLinus Torvalds frag |= SCTP_DATA_LAST_FRAG; 2411da177e4SLinus Torvalds 2421da177e4SLinus Torvalds chunk = sctp_make_datafrag_empty(asoc, sinfo, len, frag, 0); 2431da177e4SLinus Torvalds 2441da177e4SLinus Torvalds if (!chunk) 2451da177e4SLinus Torvalds goto errout; 2461da177e4SLinus Torvalds err = sctp_user_addto_chunk(chunk, offset, len, msgh->msg_iov); 2471da177e4SLinus Torvalds if (err < 0) 2481da177e4SLinus Torvalds goto errout; 2491da177e4SLinus Torvalds 2501da177e4SLinus Torvalds offset += len; 2511da177e4SLinus Torvalds 2521da177e4SLinus Torvalds /* Put the chunk->skb back into the form expected by send. */ 2531da177e4SLinus Torvalds __skb_pull(chunk->skb, (__u8 *)chunk->chunk_hdr 2541da177e4SLinus Torvalds - (__u8 *)chunk->skb->data); 2551da177e4SLinus Torvalds 2561da177e4SLinus Torvalds sctp_datamsg_assign(msg, chunk); 2571da177e4SLinus Torvalds list_add_tail(&chunk->frag_list, &msg->chunks); 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds /* The first chunk, the first chunk was likely short 2601da177e4SLinus Torvalds * to allow bundling, so reset to full size. 2611da177e4SLinus Torvalds */ 2621da177e4SLinus Torvalds if (0 == i) 2631da177e4SLinus Torvalds len = max; 2641da177e4SLinus Torvalds } 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds /* .. now the leftover bytes. */ 2671da177e4SLinus Torvalds if (over) { 2681da177e4SLinus Torvalds if (!whole) 2691da177e4SLinus Torvalds frag = SCTP_DATA_NOT_FRAG; 2701da177e4SLinus Torvalds else 2711da177e4SLinus Torvalds frag = SCTP_DATA_LAST_FRAG; 2721da177e4SLinus Torvalds 2731da177e4SLinus Torvalds chunk = sctp_make_datafrag_empty(asoc, sinfo, over, frag, 0); 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds if (!chunk) 2761da177e4SLinus Torvalds goto errout; 2771da177e4SLinus Torvalds 2781da177e4SLinus Torvalds err = sctp_user_addto_chunk(chunk, offset, over,msgh->msg_iov); 2791da177e4SLinus Torvalds 2801da177e4SLinus Torvalds /* Put the chunk->skb back into the form expected by send. */ 2811da177e4SLinus Torvalds __skb_pull(chunk->skb, (__u8 *)chunk->chunk_hdr 2821da177e4SLinus Torvalds - (__u8 *)chunk->skb->data); 2831da177e4SLinus Torvalds if (err < 0) 2841da177e4SLinus Torvalds goto errout; 2851da177e4SLinus Torvalds 2861da177e4SLinus Torvalds sctp_datamsg_assign(msg, chunk); 2871da177e4SLinus Torvalds list_add_tail(&chunk->frag_list, &msg->chunks); 2881da177e4SLinus Torvalds } 2891da177e4SLinus Torvalds 2901da177e4SLinus Torvalds return msg; 2911da177e4SLinus Torvalds 2921da177e4SLinus Torvalds errout: 2931da177e4SLinus Torvalds list_for_each_safe(pos, temp, &msg->chunks) { 2941da177e4SLinus Torvalds list_del_init(pos); 2951da177e4SLinus Torvalds chunk = list_entry(pos, struct sctp_chunk, frag_list); 2961da177e4SLinus Torvalds sctp_chunk_free(chunk); 2971da177e4SLinus Torvalds } 2981da177e4SLinus Torvalds sctp_datamsg_free(msg); 2991da177e4SLinus Torvalds return NULL; 3001da177e4SLinus Torvalds } 3011da177e4SLinus Torvalds 3021da177e4SLinus Torvalds /* Check whether this message has expired. */ 3031da177e4SLinus Torvalds int sctp_chunk_abandoned(struct sctp_chunk *chunk) 3041da177e4SLinus Torvalds { 3051da177e4SLinus Torvalds struct sctp_datamsg *msg = chunk->msg; 3061da177e4SLinus Torvalds 3071da177e4SLinus Torvalds if (!msg->can_abandon) 3081da177e4SLinus Torvalds return 0; 3091da177e4SLinus Torvalds 3101da177e4SLinus Torvalds if (time_after(jiffies, msg->expires_at)) 3111da177e4SLinus Torvalds return 1; 3121da177e4SLinus Torvalds 3131da177e4SLinus Torvalds return 0; 3141da177e4SLinus Torvalds } 3151da177e4SLinus Torvalds 3161da177e4SLinus Torvalds /* This chunk (and consequently entire message) has failed in its sending. */ 3171da177e4SLinus Torvalds void sctp_chunk_fail(struct sctp_chunk *chunk, int error) 3181da177e4SLinus Torvalds { 3191da177e4SLinus Torvalds chunk->msg->send_failed = 1; 3201da177e4SLinus Torvalds chunk->msg->send_error = error; 3211da177e4SLinus Torvalds } 322