xref: /openbmc/linux/net/sctp/chunk.c (revision 9c5c62be2f794c7cee533d856f9f34c3cf21ff1b)
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>
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);
69e8c38751SLi Zefan 	if (msg) {
701da177e4SLinus Torvalds 		sctp_datamsg_init(msg);
711da177e4SLinus Torvalds 		SCTP_DBG_OBJCNT_INC(datamsg);
72e8c38751SLi Zefan 	}
731da177e4SLinus Torvalds 	return msg;
741da177e4SLinus Torvalds }
751da177e4SLinus Torvalds 
76*9c5c62beSVlad Yasevich void sctp_datamsg_free(struct sctp_datamsg *msg)
77*9c5c62beSVlad Yasevich {
78*9c5c62beSVlad Yasevich 	struct sctp_chunk *chunk;
79*9c5c62beSVlad Yasevich 
80*9c5c62beSVlad Yasevich 	/* This doesn't have to be a _safe vairant because
81*9c5c62beSVlad Yasevich 	 * sctp_chunk_free() only drops the refs.
82*9c5c62beSVlad Yasevich 	 */
83*9c5c62beSVlad Yasevich 	list_for_each_entry(chunk, &msg->chunks, frag_list)
84*9c5c62beSVlad Yasevich 		sctp_chunk_free(chunk);
85*9c5c62beSVlad Yasevich 
86*9c5c62beSVlad Yasevich 	sctp_datamsg_put(msg);
87*9c5c62beSVlad Yasevich }
88*9c5c62beSVlad Yasevich 
891da177e4SLinus Torvalds /* Final destructruction of datamsg memory. */
901da177e4SLinus Torvalds static void sctp_datamsg_destroy(struct sctp_datamsg *msg)
911da177e4SLinus Torvalds {
921da177e4SLinus Torvalds 	struct list_head *pos, *temp;
931da177e4SLinus Torvalds 	struct sctp_chunk *chunk;
941da177e4SLinus Torvalds 	struct sctp_sock *sp;
951da177e4SLinus Torvalds 	struct sctp_ulpevent *ev;
961da177e4SLinus Torvalds 	struct sctp_association *asoc = NULL;
971da177e4SLinus Torvalds 	int error = 0, notify;
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds 	/* If we failed, we may need to notify. */
1001da177e4SLinus Torvalds 	notify = msg->send_failed ? -1 : 0;
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds 	/* Release all references. */
1031da177e4SLinus Torvalds 	list_for_each_safe(pos, temp, &msg->chunks) {
1041da177e4SLinus Torvalds 		list_del_init(pos);
1051da177e4SLinus Torvalds 		chunk = list_entry(pos, struct sctp_chunk, frag_list);
1061da177e4SLinus Torvalds 		/* Check whether we _really_ need to notify. */
1071da177e4SLinus Torvalds 		if (notify < 0) {
1081da177e4SLinus Torvalds 			asoc = chunk->asoc;
1091da177e4SLinus Torvalds 			if (msg->send_error)
1101da177e4SLinus Torvalds 				error = msg->send_error;
1111da177e4SLinus Torvalds 			else
1121da177e4SLinus Torvalds 				error = asoc->outqueue.error;
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds 			sp = sctp_sk(asoc->base.sk);
1151da177e4SLinus Torvalds 			notify = sctp_ulpevent_type_enabled(SCTP_SEND_FAILED,
1161da177e4SLinus Torvalds 							    &sp->subscribe);
1171da177e4SLinus Torvalds 		}
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds 		/* Generate a SEND FAILED event only if enabled. */
1201da177e4SLinus Torvalds 		if (notify > 0) {
1211da177e4SLinus Torvalds 			int sent;
1221da177e4SLinus Torvalds 			if (chunk->has_tsn)
1231da177e4SLinus Torvalds 				sent = SCTP_DATA_SENT;
1241da177e4SLinus Torvalds 			else
1251da177e4SLinus Torvalds 				sent = SCTP_DATA_UNSENT;
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds 			ev = sctp_ulpevent_make_send_failed(asoc, chunk, sent,
1281da177e4SLinus Torvalds 							    error, GFP_ATOMIC);
1291da177e4SLinus Torvalds 			if (ev)
1301da177e4SLinus Torvalds 				sctp_ulpq_tail_event(&asoc->ulpq, ev);
1311da177e4SLinus Torvalds 		}
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds 		sctp_chunk_put(chunk);
1341da177e4SLinus Torvalds 	}
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds 	SCTP_DBG_OBJCNT_DEC(datamsg);
1371da177e4SLinus Torvalds 	kfree(msg);
1381da177e4SLinus Torvalds }
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds /* Hold a reference. */
1411da177e4SLinus Torvalds static void sctp_datamsg_hold(struct sctp_datamsg *msg)
1421da177e4SLinus Torvalds {
1431da177e4SLinus Torvalds 	atomic_inc(&msg->refcnt);
1441da177e4SLinus Torvalds }
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds /* Release a reference. */
1471da177e4SLinus Torvalds void sctp_datamsg_put(struct sctp_datamsg *msg)
1481da177e4SLinus Torvalds {
1491da177e4SLinus Torvalds 	if (atomic_dec_and_test(&msg->refcnt))
1501da177e4SLinus Torvalds 		sctp_datamsg_destroy(msg);
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;
1743e62abf9SVlad Yasevich 	int max_data;
1751da177e4SLinus Torvalds 	struct sctp_chunk *chunk;
1761da177e4SLinus Torvalds 	struct sctp_datamsg *msg;
1771da177e4SLinus Torvalds 	struct list_head *pos, *temp;
1781da177e4SLinus Torvalds 	__u8 frag;
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds 	msg = sctp_datamsg_new(GFP_KERNEL);
1811da177e4SLinus Torvalds 	if (!msg)
1821da177e4SLinus Torvalds 		return NULL;
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds 	/* Note: Calculate this outside of the loop, so that all fragments
1851da177e4SLinus Torvalds 	 * have the same expiration.
1861da177e4SLinus Torvalds 	 */
1871da177e4SLinus Torvalds 	if (sinfo->sinfo_timetolive) {
1881da177e4SLinus Torvalds 		/* sinfo_timetolive is in milliseconds */
1891da177e4SLinus Torvalds 		msg->expires_at = jiffies +
1901da177e4SLinus Torvalds 				    msecs_to_jiffies(sinfo->sinfo_timetolive);
1911da177e4SLinus Torvalds 		msg->can_abandon = 1;
1921da177e4SLinus Torvalds 		SCTP_DEBUG_PRINTK("%s: msg:%p expires_at: %ld jiffies:%ld\n",
1930dc47877SHarvey Harrison 				  __func__, msg, msg->expires_at, jiffies);
1941da177e4SLinus Torvalds 	}
1951da177e4SLinus Torvalds 
1963e62abf9SVlad Yasevich 	/* This is the biggest possible DATA chunk that can fit into
1973e62abf9SVlad Yasevich 	 * the packet
1983e62abf9SVlad Yasevich 	 */
1993e62abf9SVlad Yasevich 	max_data = asoc->pathmtu -
2003e62abf9SVlad Yasevich 		sctp_sk(asoc->base.sk)->pf->af->net_header_len -
2013e62abf9SVlad Yasevich 		sizeof(struct sctphdr) - sizeof(struct sctp_data_chunk);
2021da177e4SLinus Torvalds 
2033e62abf9SVlad Yasevich 	max = asoc->frag_point;
2044cd57c80SVlad Yasevich 	/* If the the peer requested that we authenticate DATA chunks
2054cd57c80SVlad Yasevich 	 * we need to accound for bundling of the AUTH chunks along with
2064cd57c80SVlad Yasevich 	 * DATA.
2074cd57c80SVlad Yasevich 	 */
2084cd57c80SVlad Yasevich 	if (sctp_auth_send_cid(SCTP_CID_DATA, asoc)) {
2094cd57c80SVlad Yasevich 		struct sctp_hmac *hmac_desc = sctp_auth_asoc_get_hmac(asoc);
2104cd57c80SVlad Yasevich 
2114cd57c80SVlad Yasevich 		if (hmac_desc)
2123e62abf9SVlad Yasevich 			max_data -= WORD_ROUND(sizeof(sctp_auth_chunk_t) +
2134cd57c80SVlad Yasevich 					    hmac_desc->hmac_len);
2144cd57c80SVlad Yasevich 	}
2154cd57c80SVlad Yasevich 
2163e62abf9SVlad Yasevich 	/* Now, check if we need to reduce our max */
2173e62abf9SVlad Yasevich 	if (max > max_data)
2183e62abf9SVlad Yasevich 		max = max_data;
2193e62abf9SVlad Yasevich 
2201da177e4SLinus Torvalds 	whole = 0;
2211da177e4SLinus Torvalds 	first_len = max;
2221da177e4SLinus Torvalds 
2235d7ff261SVlad Yasevich 	/* Check to see if we have a pending SACK and try to let it be bundled
2245d7ff261SVlad Yasevich 	 * with this message.  Do this if we don't have any data queued already.
2255d7ff261SVlad Yasevich 	 * To check that, look at out_qlen and retransmit list.
2265d7ff261SVlad Yasevich 	 * NOTE: we will not reduce to account for SACK, if the message would
2275d7ff261SVlad Yasevich 	 * not have been fragmented.
2285d7ff261SVlad Yasevich 	 */
2295d7ff261SVlad Yasevich 	if (timer_pending(&asoc->timers[SCTP_EVENT_TIMEOUT_SACK]) &&
2305d7ff261SVlad Yasevich 	    asoc->outqueue.out_qlen == 0 &&
2315d7ff261SVlad Yasevich 	    list_empty(&asoc->outqueue.retransmit) &&
2325d7ff261SVlad Yasevich 	    msg_len > max)
2335d7ff261SVlad Yasevich 		max_data -= WORD_ROUND(sizeof(sctp_sack_chunk_t));
2345d7ff261SVlad Yasevich 
2351da177e4SLinus Torvalds 	/* Encourage Cookie-ECHO bundling. */
2365d7ff261SVlad Yasevich 	if (asoc->state < SCTP_STATE_COOKIE_ECHOED)
2373e62abf9SVlad Yasevich 		max_data -= SCTP_ARBITRARY_COOKIE_ECHO_LEN;
2381da177e4SLinus Torvalds 
2395d7ff261SVlad Yasevich 	/* Now that we adjusted completely, reset first_len */
2403e62abf9SVlad Yasevich 	if (first_len > max_data)
2413e62abf9SVlad Yasevich 		first_len = max_data;
2423e62abf9SVlad Yasevich 
2433e62abf9SVlad Yasevich 	/* Account for a different sized first fragment */
2443e62abf9SVlad Yasevich 	if (msg_len >= first_len) {
2451da177e4SLinus Torvalds 		msg_len -= first_len;
2461da177e4SLinus Torvalds 		whole = 1;
2471da177e4SLinus Torvalds 	}
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds 	/* How many full sized?  How many bytes leftover? */
2501da177e4SLinus Torvalds 	whole += msg_len / max;
2511da177e4SLinus Torvalds 	over = msg_len % max;
2521da177e4SLinus Torvalds 	offset = 0;
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 	if ((whole > 1) || (whole && over))
2551da177e4SLinus Torvalds 		SCTP_INC_STATS_USER(SCTP_MIB_FRAGUSRMSGS);
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds 	/* Create chunks for all the full sized DATA chunks. */
2581da177e4SLinus Torvalds 	for (i=0, len=first_len; i < whole; i++) {
2591da177e4SLinus Torvalds 		frag = SCTP_DATA_MIDDLE_FRAG;
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 		if (0 == i)
2621da177e4SLinus Torvalds 			frag |= SCTP_DATA_FIRST_FRAG;
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 		if ((i == (whole - 1)) && !over)
2651da177e4SLinus Torvalds 			frag |= SCTP_DATA_LAST_FRAG;
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds 		chunk = sctp_make_datafrag_empty(asoc, sinfo, len, frag, 0);
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 		if (!chunk)
2701da177e4SLinus Torvalds 			goto errout;
2711da177e4SLinus Torvalds 		err = sctp_user_addto_chunk(chunk, offset, len, msgh->msg_iov);
2721da177e4SLinus Torvalds 		if (err < 0)
2731da177e4SLinus Torvalds 			goto errout;
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 		offset += len;
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 		/* Put the chunk->skb back into the form expected by send.  */
2781da177e4SLinus Torvalds 		__skb_pull(chunk->skb, (__u8 *)chunk->chunk_hdr
2791da177e4SLinus Torvalds 			   - (__u8 *)chunk->skb->data);
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 		sctp_datamsg_assign(msg, chunk);
2821da177e4SLinus Torvalds 		list_add_tail(&chunk->frag_list, &msg->chunks);
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 		/* The first chunk, the first chunk was likely short
2851da177e4SLinus Torvalds 		 * to allow bundling, so reset to full size.
2861da177e4SLinus Torvalds 		 */
2871da177e4SLinus Torvalds 		if (0 == i)
2881da177e4SLinus Torvalds 			len = max;
2891da177e4SLinus Torvalds 	}
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	/* .. now the leftover bytes. */
2921da177e4SLinus Torvalds 	if (over) {
2931da177e4SLinus Torvalds 		if (!whole)
2941da177e4SLinus Torvalds 			frag = SCTP_DATA_NOT_FRAG;
2951da177e4SLinus Torvalds 		else
2961da177e4SLinus Torvalds 			frag = SCTP_DATA_LAST_FRAG;
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds 		chunk = sctp_make_datafrag_empty(asoc, sinfo, over, frag, 0);
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds 		if (!chunk)
3011da177e4SLinus Torvalds 			goto errout;
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds 		err = sctp_user_addto_chunk(chunk, offset, over,msgh->msg_iov);
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds 		/* Put the chunk->skb back into the form expected by send.  */
3061da177e4SLinus Torvalds 		__skb_pull(chunk->skb, (__u8 *)chunk->chunk_hdr
3071da177e4SLinus Torvalds 			   - (__u8 *)chunk->skb->data);
3081da177e4SLinus Torvalds 		if (err < 0)
3091da177e4SLinus Torvalds 			goto errout;
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds 		sctp_datamsg_assign(msg, chunk);
3121da177e4SLinus Torvalds 		list_add_tail(&chunk->frag_list, &msg->chunks);
3131da177e4SLinus Torvalds 	}
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds 	return msg;
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds errout:
3181da177e4SLinus Torvalds 	list_for_each_safe(pos, temp, &msg->chunks) {
3191da177e4SLinus Torvalds 		list_del_init(pos);
3201da177e4SLinus Torvalds 		chunk = list_entry(pos, struct sctp_chunk, frag_list);
3211da177e4SLinus Torvalds 		sctp_chunk_free(chunk);
3221da177e4SLinus Torvalds 	}
32380445cfbSFlorian Westphal 	sctp_datamsg_put(msg);
3241da177e4SLinus Torvalds 	return NULL;
3251da177e4SLinus Torvalds }
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds /* Check whether this message has expired. */
3281da177e4SLinus Torvalds int sctp_chunk_abandoned(struct sctp_chunk *chunk)
3291da177e4SLinus Torvalds {
3301da177e4SLinus Torvalds 	struct sctp_datamsg *msg = chunk->msg;
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds 	if (!msg->can_abandon)
3331da177e4SLinus Torvalds 		return 0;
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 	if (time_after(jiffies, msg->expires_at))
3361da177e4SLinus Torvalds 		return 1;
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds 	return 0;
3391da177e4SLinus Torvalds }
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds /* This chunk (and consequently entire message) has failed in its sending. */
3421da177e4SLinus Torvalds void sctp_chunk_fail(struct sctp_chunk *chunk, int error)
3431da177e4SLinus Torvalds {
3441da177e4SLinus Torvalds 	chunk->msg->send_failed = 1;
3451da177e4SLinus Torvalds 	chunk->msg->send_error = error;
3461da177e4SLinus Torvalds }
347