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