^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* SCTP kernel implementation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * (C) Copyright IBM Corp. 2003, 2004
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * This file is part of the SCTP kernel implementation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * This file contains the code relating the chunk abstraction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Please send any bug reports or fixes you make to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * email address(es):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * lksctp developers <linux-sctp@vger.kernel.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * Written or modified by:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * Jon Grimm <jgrimm@us.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * Sridhar Samudrala <sri@us.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/net.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/inet.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <net/sctp/sctp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <net/sctp/sm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) /* This file is mostly in anticipation of future work, but initially
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * populate with fragment tracking for an outbound message.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) /* Initialize datamsg from memory. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static void sctp_datamsg_init(struct sctp_datamsg *msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) refcount_set(&msg->refcnt, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) msg->send_failed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) msg->send_error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) msg->can_delay = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) msg->abandoned = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) msg->expires_at = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) INIT_LIST_HEAD(&msg->chunks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) /* Allocate and initialize datamsg. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static struct sctp_datamsg *sctp_datamsg_new(gfp_t gfp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct sctp_datamsg *msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) msg = kmalloc(sizeof(struct sctp_datamsg), gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) if (msg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) sctp_datamsg_init(msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) SCTP_DBG_OBJCNT_INC(datamsg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) void sctp_datamsg_free(struct sctp_datamsg *msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct sctp_chunk *chunk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) /* This doesn't have to be a _safe vairant because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * sctp_chunk_free() only drops the refs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) list_for_each_entry(chunk, &msg->chunks, frag_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) sctp_chunk_free(chunk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) sctp_datamsg_put(msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) /* Final destructruction of datamsg memory. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) static void sctp_datamsg_destroy(struct sctp_datamsg *msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) struct sctp_association *asoc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) struct list_head *pos, *temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) struct sctp_chunk *chunk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) struct sctp_ulpevent *ev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) int error, sent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) /* Release all references. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) list_for_each_safe(pos, temp, &msg->chunks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) list_del_init(pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) chunk = list_entry(pos, struct sctp_chunk, frag_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (!msg->send_failed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) sctp_chunk_put(chunk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) asoc = chunk->asoc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) error = msg->send_error ?: asoc->outqueue.error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) sent = chunk->has_tsn ? SCTP_DATA_SENT : SCTP_DATA_UNSENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (sctp_ulpevent_type_enabled(asoc->subscribe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) SCTP_SEND_FAILED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) ev = sctp_ulpevent_make_send_failed(asoc, chunk, sent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) error, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (ev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) asoc->stream.si->enqueue_event(&asoc->ulpq, ev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (sctp_ulpevent_type_enabled(asoc->subscribe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) SCTP_SEND_FAILED_EVENT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) ev = sctp_ulpevent_make_send_failed_event(asoc, chunk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) sent, error,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (ev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) asoc->stream.si->enqueue_event(&asoc->ulpq, ev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) sctp_chunk_put(chunk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) SCTP_DBG_OBJCNT_DEC(datamsg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) kfree(msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) /* Hold a reference. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static void sctp_datamsg_hold(struct sctp_datamsg *msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) refcount_inc(&msg->refcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) /* Release a reference. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) void sctp_datamsg_put(struct sctp_datamsg *msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (refcount_dec_and_test(&msg->refcnt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) sctp_datamsg_destroy(msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) /* Assign a chunk to this datamsg. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static void sctp_datamsg_assign(struct sctp_datamsg *msg, struct sctp_chunk *chunk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) sctp_datamsg_hold(msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) chunk->msg = msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) /* A data chunk can have a maximum payload of (2^16 - 20). Break
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * down any such message into smaller chunks. Opportunistically, fragment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * the chunks down to the current MTU constraints. We may get refragmented
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * later if the PMTU changes, but it is _much better_ to fragment immediately
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * with a reasonable guess than always doing our fragmentation on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * soft-interrupt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) struct sctp_sndrcvinfo *sinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) struct iov_iter *from)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) size_t len, first_len, max_data, remaining;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) size_t msg_len = iov_iter_count(from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) struct sctp_shared_key *shkey = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) struct list_head *pos, *temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) struct sctp_chunk *chunk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) struct sctp_datamsg *msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) msg = sctp_datamsg_new(GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if (!msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) /* Note: Calculate this outside of the loop, so that all fragments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * have the same expiration.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (asoc->peer.prsctp_capable && sinfo->sinfo_timetolive &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) (SCTP_PR_TTL_ENABLED(sinfo->sinfo_flags) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) !SCTP_PR_POLICY(sinfo->sinfo_flags)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) msg->expires_at = jiffies +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) msecs_to_jiffies(sinfo->sinfo_timetolive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) /* This is the biggest possible DATA chunk that can fit into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) * the packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) max_data = asoc->frag_point;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (unlikely(!max_data)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) max_data = sctp_min_frag_point(sctp_sk(asoc->base.sk),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) sctp_datachk_len(&asoc->stream));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) pr_warn_ratelimited("%s: asoc:%p frag_point is zero, forcing max_data to default minimum (%zu)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) __func__, asoc, max_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) /* If the peer requested that we authenticate DATA chunks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) * we need to account for bundling of the AUTH chunks along with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) * DATA.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) if (sctp_auth_send_cid(SCTP_CID_DATA, asoc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) struct sctp_hmac *hmac_desc = sctp_auth_asoc_get_hmac(asoc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) if (hmac_desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) max_data -= SCTP_PAD4(sizeof(struct sctp_auth_chunk) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) hmac_desc->hmac_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) if (sinfo->sinfo_tsn &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) sinfo->sinfo_ssn != asoc->active_key_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) shkey = sctp_auth_get_shkey(asoc, sinfo->sinfo_ssn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) if (!shkey) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) shkey = asoc->shkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) /* Set first_len and then account for possible bundles on first frag */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) first_len = max_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) /* Check to see if we have a pending SACK and try to let it be bundled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * with this message. Do this if we don't have any data queued already.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * To check that, look at out_qlen and retransmit list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * NOTE: we will not reduce to account for SACK, if the message would
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) * not have been fragmented.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) if (timer_pending(&asoc->timers[SCTP_EVENT_TIMEOUT_SACK]) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) asoc->outqueue.out_qlen == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) list_empty(&asoc->outqueue.retransmit) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) msg_len > max_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) first_len -= SCTP_PAD4(sizeof(struct sctp_sack_chunk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) /* Encourage Cookie-ECHO bundling. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) if (asoc->state < SCTP_STATE_COOKIE_ECHOED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) first_len -= SCTP_ARBITRARY_COOKIE_ECHO_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) /* Account for a different sized first fragment */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) if (msg_len >= first_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) msg->can_delay = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) if (msg_len > first_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) SCTP_INC_STATS(asoc->base.net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) SCTP_MIB_FRAGUSRMSGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) /* Which may be the only one... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) first_len = msg_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) /* Create chunks for all DATA chunks. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) for (remaining = msg_len; remaining; remaining -= len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) u8 frag = SCTP_DATA_MIDDLE_FRAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) if (remaining == msg_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) /* First frag, which may also be the last */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) frag |= SCTP_DATA_FIRST_FRAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) len = first_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) /* Middle frags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) len = max_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) if (len >= remaining) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) /* Last frag, which may also be the first */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) len = remaining;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) frag |= SCTP_DATA_LAST_FRAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) /* The application requests to set the I-bit of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) * last DATA chunk of a user message when providing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) * the user message to the SCTP implementation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) if ((sinfo->sinfo_flags & SCTP_EOF) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) (sinfo->sinfo_flags & SCTP_SACK_IMMEDIATELY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) frag |= SCTP_DATA_SACK_IMM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) chunk = asoc->stream.si->make_datafrag(asoc, sinfo, len, frag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) if (!chunk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) goto errout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) err = sctp_user_addto_chunk(chunk, len, from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) goto errout_chunk_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) chunk->shkey = shkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) /* Put the chunk->skb back into the form expected by send. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) __skb_pull(chunk->skb, (__u8 *)chunk->chunk_hdr -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) chunk->skb->data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) sctp_datamsg_assign(msg, chunk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) list_add_tail(&chunk->frag_list, &msg->chunks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) return msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) errout_chunk_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) sctp_chunk_free(chunk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) errout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) list_for_each_safe(pos, temp, &msg->chunks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) list_del_init(pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) chunk = list_entry(pos, struct sctp_chunk, frag_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) sctp_chunk_free(chunk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) sctp_datamsg_put(msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) /* Check whether this message has expired. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) int sctp_chunk_abandoned(struct sctp_chunk *chunk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) if (!chunk->asoc->peer.prsctp_capable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) if (chunk->msg->abandoned)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) if (!chunk->has_tsn &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) !(chunk->chunk_hdr->flags & SCTP_DATA_FIRST_FRAG))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) if (SCTP_PR_TTL_ENABLED(chunk->sinfo.sinfo_flags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) time_after(jiffies, chunk->msg->expires_at)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) struct sctp_stream_out *streamout =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) SCTP_SO(&chunk->asoc->stream,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) chunk->sinfo.sinfo_stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) if (chunk->sent_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) chunk->asoc->abandoned_sent[SCTP_PR_INDEX(TTL)]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) streamout->ext->abandoned_sent[SCTP_PR_INDEX(TTL)]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) chunk->asoc->abandoned_unsent[SCTP_PR_INDEX(TTL)]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) streamout->ext->abandoned_unsent[SCTP_PR_INDEX(TTL)]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) chunk->msg->abandoned = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) } else if (SCTP_PR_RTX_ENABLED(chunk->sinfo.sinfo_flags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) chunk->sent_count > chunk->sinfo.sinfo_timetolive) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) struct sctp_stream_out *streamout =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) SCTP_SO(&chunk->asoc->stream,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) chunk->sinfo.sinfo_stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) chunk->asoc->abandoned_sent[SCTP_PR_INDEX(RTX)]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) streamout->ext->abandoned_sent[SCTP_PR_INDEX(RTX)]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) chunk->msg->abandoned = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) } else if (!SCTP_PR_POLICY(chunk->sinfo.sinfo_flags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) chunk->msg->expires_at &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) time_after(jiffies, chunk->msg->expires_at)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) chunk->msg->abandoned = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) /* PRIO policy is processed by sendmsg, not here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) /* This chunk (and consequently entire message) has failed in its sending. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) void sctp_chunk_fail(struct sctp_chunk *chunk, int error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) chunk->msg->send_failed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) chunk->msg->send_error = error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) }