0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
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
0031
0032
0033
0034
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
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
0063
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
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
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
0119 static void sctp_datamsg_hold(struct sctp_datamsg *msg)
0120 {
0121 refcount_inc(&msg->refcnt);
0122 }
0123
0124
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
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
0140
0141
0142
0143
0144
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
0163
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
0172
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
0183
0184
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
0206 first_len = max_data;
0207
0208
0209
0210
0211
0212
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
0221 if (asoc->state < SCTP_STATE_COOKIE_ECHOED)
0222 first_len -= SCTP_ARBITRARY_COOKIE_ECHO_LEN;
0223
0224
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
0232 first_len = msg_len;
0233 }
0234
0235
0236 for (remaining = msg_len; remaining; remaining -= len) {
0237 u8 frag = SCTP_DATA_MIDDLE_FRAG;
0238
0239 if (remaining == msg_len) {
0240
0241 frag |= SCTP_DATA_FIRST_FRAG;
0242 len = first_len;
0243 } else {
0244
0245 len = max_data;
0246 }
0247
0248 if (len >= remaining) {
0249
0250 len = remaining;
0251 frag |= SCTP_DATA_LAST_FRAG;
0252
0253
0254
0255
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
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
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
0344
0345 return 0;
0346 }
0347
0348
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 }