Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* SCTP kernel implementation
0003  * (C) Copyright IBM Corp. 2003, 2004
0004  *
0005  * This file is part of the SCTP kernel implementation
0006  *
0007  * This file contains the code relating the chunk abstraction.
0008  *
0009  * Please send any bug reports or fixes you make to the
0010  * email address(es):
0011  *    lksctp developers <linux-sctp@vger.kernel.org>
0012  *
0013  * Written or modified by:
0014  *    Jon Grimm             <jgrimm@us.ibm.com>
0015  *    Sridhar Samudrala     <sri@us.ibm.com>
0016  */
0017 
0018 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0019 
0020 #include <linux/types.h>
0021 #include <linux/kernel.h>
0022 #include <linux/net.h>
0023 #include <linux/inet.h>
0024 #include <linux/skbuff.h>
0025 #include <linux/slab.h>
0026 #include <net/sock.h>
0027 #include <net/sctp/sctp.h>
0028 #include <net/sctp/sm.h>
0029 
0030 /* This file is mostly in anticipation of future work, but initially
0031  * populate with fragment tracking for an outbound message.
0032  */
0033 
0034 /* Initialize datamsg from memory. */
0035 static void sctp_datamsg_init(struct sctp_datamsg *msg)
0036 {
0037     refcount_set(&msg->refcnt, 1);
0038     msg->send_failed = 0;
0039     msg->send_error = 0;
0040     msg->can_delay = 1;
0041     msg->abandoned = 0;
0042     msg->expires_at = 0;
0043     INIT_LIST_HEAD(&msg->chunks);
0044 }
0045 
0046 /* Allocate and initialize datamsg. */
0047 static struct sctp_datamsg *sctp_datamsg_new(gfp_t gfp)
0048 {
0049     struct sctp_datamsg *msg;
0050     msg = kmalloc(sizeof(struct sctp_datamsg), gfp);
0051     if (msg) {
0052         sctp_datamsg_init(msg);
0053         SCTP_DBG_OBJCNT_INC(datamsg);
0054     }
0055     return msg;
0056 }
0057 
0058 void sctp_datamsg_free(struct sctp_datamsg *msg)
0059 {
0060     struct sctp_chunk *chunk;
0061 
0062     /* This doesn't have to be a _safe vairant because
0063      * sctp_chunk_free() only drops the refs.
0064      */
0065     list_for_each_entry(chunk, &msg->chunks, frag_list)
0066         sctp_chunk_free(chunk);
0067 
0068     sctp_datamsg_put(msg);
0069 }
0070 
0071 /* Final destructruction of datamsg memory. */
0072 static void sctp_datamsg_destroy(struct sctp_datamsg *msg)
0073 {
0074     struct sctp_association *asoc = NULL;
0075     struct list_head *pos, *temp;
0076     struct sctp_chunk *chunk;
0077     struct sctp_ulpevent *ev;
0078     int error, sent;
0079 
0080     /* Release all references. */
0081     list_for_each_safe(pos, temp, &msg->chunks) {
0082         list_del_init(pos);
0083         chunk = list_entry(pos, struct sctp_chunk, frag_list);
0084 
0085         if (!msg->send_failed) {
0086             sctp_chunk_put(chunk);
0087             continue;
0088         }
0089 
0090         asoc = chunk->asoc;
0091         error = msg->send_error ?: asoc->outqueue.error;
0092         sent = chunk->has_tsn ? SCTP_DATA_SENT : SCTP_DATA_UNSENT;
0093 
0094         if (sctp_ulpevent_type_enabled(asoc->subscribe,
0095                            SCTP_SEND_FAILED)) {
0096             ev = sctp_ulpevent_make_send_failed(asoc, chunk, sent,
0097                                 error, GFP_ATOMIC);
0098             if (ev)
0099                 asoc->stream.si->enqueue_event(&asoc->ulpq, ev);
0100         }
0101 
0102         if (sctp_ulpevent_type_enabled(asoc->subscribe,
0103                            SCTP_SEND_FAILED_EVENT)) {
0104             ev = sctp_ulpevent_make_send_failed_event(asoc, chunk,
0105                                   sent, error,
0106                                   GFP_ATOMIC);
0107             if (ev)
0108                 asoc->stream.si->enqueue_event(&asoc->ulpq, ev);
0109         }
0110 
0111         sctp_chunk_put(chunk);
0112     }
0113 
0114     SCTP_DBG_OBJCNT_DEC(datamsg);
0115     kfree(msg);
0116 }
0117 
0118 /* Hold a reference. */
0119 static void sctp_datamsg_hold(struct sctp_datamsg *msg)
0120 {
0121     refcount_inc(&msg->refcnt);
0122 }
0123 
0124 /* Release a reference. */
0125 void sctp_datamsg_put(struct sctp_datamsg *msg)
0126 {
0127     if (refcount_dec_and_test(&msg->refcnt))
0128         sctp_datamsg_destroy(msg);
0129 }
0130 
0131 /* Assign a chunk to this datamsg. */
0132 static void sctp_datamsg_assign(struct sctp_datamsg *msg, struct sctp_chunk *chunk)
0133 {
0134     sctp_datamsg_hold(msg);
0135     chunk->msg = msg;
0136 }
0137 
0138 
0139 /* A data chunk can have a maximum payload of (2^16 - 20).  Break
0140  * down any such message into smaller chunks.  Opportunistically, fragment
0141  * the chunks down to the current MTU constraints.  We may get refragmented
0142  * later if the PMTU changes, but it is _much better_ to fragment immediately
0143  * with a reasonable guess than always doing our fragmentation on the
0144  * soft-interrupt.
0145  */
0146 struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc,
0147                         struct sctp_sndrcvinfo *sinfo,
0148                         struct iov_iter *from)
0149 {
0150     size_t len, first_len, max_data, remaining;
0151     size_t msg_len = iov_iter_count(from);
0152     struct sctp_shared_key *shkey = NULL;
0153     struct list_head *pos, *temp;
0154     struct sctp_chunk *chunk;
0155     struct sctp_datamsg *msg;
0156     int err;
0157 
0158     msg = sctp_datamsg_new(GFP_KERNEL);
0159     if (!msg)
0160         return ERR_PTR(-ENOMEM);
0161 
0162     /* Note: Calculate this outside of the loop, so that all fragments
0163      * have the same expiration.
0164      */
0165     if (asoc->peer.prsctp_capable && sinfo->sinfo_timetolive &&
0166         (SCTP_PR_TTL_ENABLED(sinfo->sinfo_flags) ||
0167          !SCTP_PR_POLICY(sinfo->sinfo_flags)))
0168         msg->expires_at = jiffies +
0169                   msecs_to_jiffies(sinfo->sinfo_timetolive);
0170 
0171     /* This is the biggest possible DATA chunk that can fit into
0172      * the packet
0173      */
0174     max_data = asoc->frag_point;
0175     if (unlikely(!max_data)) {
0176         max_data = sctp_min_frag_point(sctp_sk(asoc->base.sk),
0177                            sctp_datachk_len(&asoc->stream));
0178         pr_warn_ratelimited("%s: asoc:%p frag_point is zero, forcing max_data to default minimum (%zu)",
0179                     __func__, asoc, max_data);
0180     }
0181 
0182     /* If the peer requested that we authenticate DATA chunks
0183      * we need to account for bundling of the AUTH chunks along with
0184      * DATA.
0185      */
0186     if (sctp_auth_send_cid(SCTP_CID_DATA, asoc)) {
0187         struct sctp_hmac *hmac_desc = sctp_auth_asoc_get_hmac(asoc);
0188 
0189         if (hmac_desc)
0190             max_data -= SCTP_PAD4(sizeof(struct sctp_auth_chunk) +
0191                           hmac_desc->hmac_len);
0192 
0193         if (sinfo->sinfo_tsn &&
0194             sinfo->sinfo_ssn != asoc->active_key_id) {
0195             shkey = sctp_auth_get_shkey(asoc, sinfo->sinfo_ssn);
0196             if (!shkey) {
0197                 err = -EINVAL;
0198                 goto errout;
0199             }
0200         } else {
0201             shkey = asoc->shkey;
0202         }
0203     }
0204 
0205     /* Set first_len and then account for possible bundles on first frag */
0206     first_len = max_data;
0207 
0208     /* Check to see if we have a pending SACK and try to let it be bundled
0209      * with this message.  Do this if we don't have any data queued already.
0210      * To check that, look at out_qlen and retransmit list.
0211      * NOTE: we will not reduce to account for SACK, if the message would
0212      * not have been fragmented.
0213      */
0214     if (timer_pending(&asoc->timers[SCTP_EVENT_TIMEOUT_SACK]) &&
0215         asoc->outqueue.out_qlen == 0 &&
0216         list_empty(&asoc->outqueue.retransmit) &&
0217         msg_len > max_data)
0218         first_len -= SCTP_PAD4(sizeof(struct sctp_sack_chunk));
0219 
0220     /* Encourage Cookie-ECHO bundling. */
0221     if (asoc->state < SCTP_STATE_COOKIE_ECHOED)
0222         first_len -= SCTP_ARBITRARY_COOKIE_ECHO_LEN;
0223 
0224     /* Account for a different sized first fragment */
0225     if (msg_len >= first_len) {
0226         msg->can_delay = 0;
0227         if (msg_len > first_len)
0228             SCTP_INC_STATS(asoc->base.net,
0229                        SCTP_MIB_FRAGUSRMSGS);
0230     } else {
0231         /* Which may be the only one... */
0232         first_len = msg_len;
0233     }
0234 
0235     /* Create chunks for all DATA chunks. */
0236     for (remaining = msg_len; remaining; remaining -= len) {
0237         u8 frag = SCTP_DATA_MIDDLE_FRAG;
0238 
0239         if (remaining == msg_len) {
0240             /* First frag, which may also be the last */
0241             frag |= SCTP_DATA_FIRST_FRAG;
0242             len = first_len;
0243         } else {
0244             /* Middle frags */
0245             len = max_data;
0246         }
0247 
0248         if (len >= remaining) {
0249             /* Last frag, which may also be the first */
0250             len = remaining;
0251             frag |= SCTP_DATA_LAST_FRAG;
0252 
0253             /* The application requests to set the I-bit of the
0254              * last DATA chunk of a user message when providing
0255              * the user message to the SCTP implementation.
0256              */
0257             if ((sinfo->sinfo_flags & SCTP_EOF) ||
0258                 (sinfo->sinfo_flags & SCTP_SACK_IMMEDIATELY))
0259                 frag |= SCTP_DATA_SACK_IMM;
0260         }
0261 
0262         chunk = asoc->stream.si->make_datafrag(asoc, sinfo, len, frag,
0263                                GFP_KERNEL);
0264         if (!chunk) {
0265             err = -ENOMEM;
0266             goto errout;
0267         }
0268 
0269         err = sctp_user_addto_chunk(chunk, len, from);
0270         if (err < 0)
0271             goto errout_chunk_free;
0272 
0273         chunk->shkey = shkey;
0274 
0275         /* Put the chunk->skb back into the form expected by send.  */
0276         __skb_pull(chunk->skb, (__u8 *)chunk->chunk_hdr -
0277                        chunk->skb->data);
0278 
0279         sctp_datamsg_assign(msg, chunk);
0280         list_add_tail(&chunk->frag_list, &msg->chunks);
0281     }
0282 
0283     return msg;
0284 
0285 errout_chunk_free:
0286     sctp_chunk_free(chunk);
0287 
0288 errout:
0289     list_for_each_safe(pos, temp, &msg->chunks) {
0290         list_del_init(pos);
0291         chunk = list_entry(pos, struct sctp_chunk, frag_list);
0292         sctp_chunk_free(chunk);
0293     }
0294     sctp_datamsg_put(msg);
0295 
0296     return ERR_PTR(err);
0297 }
0298 
0299 /* Check whether this message has expired. */
0300 int sctp_chunk_abandoned(struct sctp_chunk *chunk)
0301 {
0302     if (!chunk->asoc->peer.prsctp_capable)
0303         return 0;
0304 
0305     if (chunk->msg->abandoned)
0306         return 1;
0307 
0308     if (!chunk->has_tsn &&
0309         !(chunk->chunk_hdr->flags & SCTP_DATA_FIRST_FRAG))
0310         return 0;
0311 
0312     if (SCTP_PR_TTL_ENABLED(chunk->sinfo.sinfo_flags) &&
0313         time_after(jiffies, chunk->msg->expires_at)) {
0314         struct sctp_stream_out *streamout =
0315             SCTP_SO(&chunk->asoc->stream,
0316                 chunk->sinfo.sinfo_stream);
0317 
0318         if (chunk->sent_count) {
0319             chunk->asoc->abandoned_sent[SCTP_PR_INDEX(TTL)]++;
0320             streamout->ext->abandoned_sent[SCTP_PR_INDEX(TTL)]++;
0321         } else {
0322             chunk->asoc->abandoned_unsent[SCTP_PR_INDEX(TTL)]++;
0323             streamout->ext->abandoned_unsent[SCTP_PR_INDEX(TTL)]++;
0324         }
0325         chunk->msg->abandoned = 1;
0326         return 1;
0327     } else if (SCTP_PR_RTX_ENABLED(chunk->sinfo.sinfo_flags) &&
0328            chunk->sent_count > chunk->sinfo.sinfo_timetolive) {
0329         struct sctp_stream_out *streamout =
0330             SCTP_SO(&chunk->asoc->stream,
0331                 chunk->sinfo.sinfo_stream);
0332 
0333         chunk->asoc->abandoned_sent[SCTP_PR_INDEX(RTX)]++;
0334         streamout->ext->abandoned_sent[SCTP_PR_INDEX(RTX)]++;
0335         chunk->msg->abandoned = 1;
0336         return 1;
0337     } else if (!SCTP_PR_POLICY(chunk->sinfo.sinfo_flags) &&
0338            chunk->msg->expires_at &&
0339            time_after(jiffies, chunk->msg->expires_at)) {
0340         chunk->msg->abandoned = 1;
0341         return 1;
0342     }
0343     /* PRIO policy is processed by sendmsg, not here */
0344 
0345     return 0;
0346 }
0347 
0348 /* This chunk (and consequently entire message) has failed in its sending. */
0349 void sctp_chunk_fail(struct sctp_chunk *chunk, int error)
0350 {
0351     chunk->msg->send_failed = 1;
0352     chunk->msg->send_error = error;
0353 }