sctp: make use of SCTP_TRUNC4 macro
[cascardo/linux.git] / net / sctp / chunk.c
1 /* SCTP kernel implementation
2  * (C) Copyright IBM Corp. 2003, 2004
3  *
4  * This file is part of the SCTP kernel implementation
5  *
6  * This file contains the code relating the chunk abstraction.
7  *
8  * This SCTP implementation is free software;
9  * you can redistribute it and/or modify it under the terms of
10  * the GNU General Public License as published by
11  * the Free Software Foundation; either version 2, or (at your option)
12  * any later version.
13  *
14  * This SCTP implementation is distributed in the hope that it
15  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
16  *                 ************************
17  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18  * See the GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with GNU CC; see the file COPYING.  If not, see
22  * <http://www.gnu.org/licenses/>.
23  *
24  * Please send any bug reports or fixes you make to the
25  * email address(es):
26  *    lksctp developers <linux-sctp@vger.kernel.org>
27  *
28  * Written or modified by:
29  *    Jon Grimm             <jgrimm@us.ibm.com>
30  *    Sridhar Samudrala     <sri@us.ibm.com>
31  */
32
33 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
34
35 #include <linux/types.h>
36 #include <linux/kernel.h>
37 #include <linux/net.h>
38 #include <linux/inet.h>
39 #include <linux/skbuff.h>
40 #include <linux/slab.h>
41 #include <net/sock.h>
42 #include <net/sctp/sctp.h>
43 #include <net/sctp/sm.h>
44
45 /* This file is mostly in anticipation of future work, but initially
46  * populate with fragment tracking for an outbound message.
47  */
48
49 /* Initialize datamsg from memory. */
50 static void sctp_datamsg_init(struct sctp_datamsg *msg)
51 {
52         atomic_set(&msg->refcnt, 1);
53         msg->send_failed = 0;
54         msg->send_error = 0;
55         msg->can_abandon = 0;
56         msg->can_delay = 1;
57         msg->expires_at = 0;
58         INIT_LIST_HEAD(&msg->chunks);
59 }
60
61 /* Allocate and initialize datamsg. */
62 static struct sctp_datamsg *sctp_datamsg_new(gfp_t gfp)
63 {
64         struct sctp_datamsg *msg;
65         msg = kmalloc(sizeof(struct sctp_datamsg), gfp);
66         if (msg) {
67                 sctp_datamsg_init(msg);
68                 SCTP_DBG_OBJCNT_INC(datamsg);
69         }
70         return msg;
71 }
72
73 void sctp_datamsg_free(struct sctp_datamsg *msg)
74 {
75         struct sctp_chunk *chunk;
76
77         /* This doesn't have to be a _safe vairant because
78          * sctp_chunk_free() only drops the refs.
79          */
80         list_for_each_entry(chunk, &msg->chunks, frag_list)
81                 sctp_chunk_free(chunk);
82
83         sctp_datamsg_put(msg);
84 }
85
86 /* Final destructruction of datamsg memory. */
87 static void sctp_datamsg_destroy(struct sctp_datamsg *msg)
88 {
89         struct list_head *pos, *temp;
90         struct sctp_chunk *chunk;
91         struct sctp_sock *sp;
92         struct sctp_ulpevent *ev;
93         struct sctp_association *asoc = NULL;
94         int error = 0, notify;
95
96         /* If we failed, we may need to notify. */
97         notify = msg->send_failed ? -1 : 0;
98
99         /* Release all references. */
100         list_for_each_safe(pos, temp, &msg->chunks) {
101                 list_del_init(pos);
102                 chunk = list_entry(pos, struct sctp_chunk, frag_list);
103                 /* Check whether we _really_ need to notify. */
104                 if (notify < 0) {
105                         asoc = chunk->asoc;
106                         if (msg->send_error)
107                                 error = msg->send_error;
108                         else
109                                 error = asoc->outqueue.error;
110
111                         sp = sctp_sk(asoc->base.sk);
112                         notify = sctp_ulpevent_type_enabled(SCTP_SEND_FAILED,
113                                                             &sp->subscribe);
114                 }
115
116                 /* Generate a SEND FAILED event only if enabled. */
117                 if (notify > 0) {
118                         int sent;
119                         if (chunk->has_tsn)
120                                 sent = SCTP_DATA_SENT;
121                         else
122                                 sent = SCTP_DATA_UNSENT;
123
124                         ev = sctp_ulpevent_make_send_failed(asoc, chunk, sent,
125                                                             error, GFP_ATOMIC);
126                         if (ev)
127                                 sctp_ulpq_tail_event(&asoc->ulpq, ev);
128                 }
129
130                 sctp_chunk_put(chunk);
131         }
132
133         SCTP_DBG_OBJCNT_DEC(datamsg);
134         kfree(msg);
135 }
136
137 /* Hold a reference. */
138 static void sctp_datamsg_hold(struct sctp_datamsg *msg)
139 {
140         atomic_inc(&msg->refcnt);
141 }
142
143 /* Release a reference. */
144 void sctp_datamsg_put(struct sctp_datamsg *msg)
145 {
146         if (atomic_dec_and_test(&msg->refcnt))
147                 sctp_datamsg_destroy(msg);
148 }
149
150 /* Assign a chunk to this datamsg. */
151 static void sctp_datamsg_assign(struct sctp_datamsg *msg, struct sctp_chunk *chunk)
152 {
153         sctp_datamsg_hold(msg);
154         chunk->msg = msg;
155 }
156
157
158 /* A data chunk can have a maximum payload of (2^16 - 20).  Break
159  * down any such message into smaller chunks.  Opportunistically, fragment
160  * the chunks down to the current MTU constraints.  We may get refragmented
161  * later if the PMTU changes, but it is _much better_ to fragment immediately
162  * with a reasonable guess than always doing our fragmentation on the
163  * soft-interrupt.
164  */
165 struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc,
166                                             struct sctp_sndrcvinfo *sinfo,
167                                             struct iov_iter *from)
168 {
169         int max, whole, i, offset, over, err;
170         int len, first_len;
171         int max_data;
172         struct sctp_chunk *chunk;
173         struct sctp_datamsg *msg;
174         struct list_head *pos, *temp;
175         size_t msg_len = iov_iter_count(from);
176         __u8 frag;
177
178         msg = sctp_datamsg_new(GFP_KERNEL);
179         if (!msg)
180                 return ERR_PTR(-ENOMEM);
181
182         /* Note: Calculate this outside of the loop, so that all fragments
183          * have the same expiration.
184          */
185         if (sinfo->sinfo_timetolive) {
186                 /* sinfo_timetolive is in milliseconds */
187                 msg->expires_at = jiffies +
188                                     msecs_to_jiffies(sinfo->sinfo_timetolive);
189                 msg->can_abandon = 1;
190
191                 pr_debug("%s: msg:%p expires_at:%ld jiffies:%ld\n", __func__,
192                          msg, msg->expires_at, jiffies);
193         }
194
195         /* This is the biggest possible DATA chunk that can fit into
196          * the packet
197          */
198         max_data = asoc->pathmtu -
199                    sctp_sk(asoc->base.sk)->pf->af->net_header_len -
200                    sizeof(struct sctphdr) - sizeof(struct sctp_data_chunk);
201         max_data = SCTP_TRUNC4(max_data);
202
203         max = asoc->frag_point;
204         /* If the the peer requested that we authenticate DATA chunks
205          * we need to account for bundling of the AUTH chunks along with
206          * DATA.
207          */
208         if (sctp_auth_send_cid(SCTP_CID_DATA, asoc)) {
209                 struct sctp_hmac *hmac_desc = sctp_auth_asoc_get_hmac(asoc);
210
211                 if (hmac_desc)
212                         max_data -= SCTP_PAD4(sizeof(sctp_auth_chunk_t) +
213                                               hmac_desc->hmac_len);
214         }
215
216         /* Now, check if we need to reduce our max */
217         if (max > max_data)
218                 max = max_data;
219
220         whole = 0;
221         first_len = max;
222
223         /* Check to see if we have a pending SACK and try to let it be bundled
224          * with this message.  Do this if we don't have any data queued already.
225          * To check that, look at out_qlen and retransmit list.
226          * NOTE: we will not reduce to account for SACK, if the message would
227          * not have been fragmented.
228          */
229         if (timer_pending(&asoc->timers[SCTP_EVENT_TIMEOUT_SACK]) &&
230             asoc->outqueue.out_qlen == 0 &&
231             list_empty(&asoc->outqueue.retransmit) &&
232             msg_len > max)
233                 max_data -= SCTP_PAD4(sizeof(sctp_sack_chunk_t));
234
235         /* Encourage Cookie-ECHO bundling. */
236         if (asoc->state < SCTP_STATE_COOKIE_ECHOED)
237                 max_data -= SCTP_ARBITRARY_COOKIE_ECHO_LEN;
238
239         /* Now that we adjusted completely, reset first_len */
240         if (first_len > max_data)
241                 first_len = max_data;
242
243         /* Account for a different sized first fragment */
244         if (msg_len >= first_len) {
245                 msg_len -= first_len;
246                 whole = 1;
247                 msg->can_delay = 0;
248         }
249
250         /* How many full sized?  How many bytes leftover? */
251         whole += msg_len / max;
252         over = msg_len % max;
253         offset = 0;
254
255         if ((whole > 1) || (whole && over))
256                 SCTP_INC_STATS(sock_net(asoc->base.sk), SCTP_MIB_FRAGUSRMSGS);
257
258         /* Create chunks for all the full sized DATA chunks. */
259         for (i = 0, len = first_len; i < whole; i++) {
260                 frag = SCTP_DATA_MIDDLE_FRAG;
261
262                 if (0 == i)
263                         frag |= SCTP_DATA_FIRST_FRAG;
264
265                 if ((i == (whole - 1)) && !over) {
266                         frag |= SCTP_DATA_LAST_FRAG;
267
268                         /* The application requests to set the I-bit of the
269                          * last DATA chunk of a user message when providing
270                          * the user message to the SCTP implementation.
271                          */
272                         if ((sinfo->sinfo_flags & SCTP_EOF) ||
273                             (sinfo->sinfo_flags & SCTP_SACK_IMMEDIATELY))
274                                 frag |= SCTP_DATA_SACK_IMM;
275                 }
276
277                 chunk = sctp_make_datafrag_empty(asoc, sinfo, len, frag,
278                                                  0, GFP_KERNEL);
279
280                 if (!chunk) {
281                         err = -ENOMEM;
282                         goto errout;
283                 }
284
285                 err = sctp_user_addto_chunk(chunk, len, from);
286                 if (err < 0)
287                         goto errout_chunk_free;
288
289                 /* Put the chunk->skb back into the form expected by send.  */
290                 __skb_pull(chunk->skb, (__u8 *)chunk->chunk_hdr
291                            - (__u8 *)chunk->skb->data);
292
293                 sctp_datamsg_assign(msg, chunk);
294                 list_add_tail(&chunk->frag_list, &msg->chunks);
295
296                 /* The first chunk, the first chunk was likely short
297                  * to allow bundling, so reset to full size.
298                  */
299                 if (0 == i)
300                         len = max;
301         }
302
303         /* .. now the leftover bytes. */
304         if (over) {
305                 if (!whole)
306                         frag = SCTP_DATA_NOT_FRAG;
307                 else
308                         frag = SCTP_DATA_LAST_FRAG;
309
310                 if ((sinfo->sinfo_flags & SCTP_EOF) ||
311                     (sinfo->sinfo_flags & SCTP_SACK_IMMEDIATELY))
312                         frag |= SCTP_DATA_SACK_IMM;
313
314                 chunk = sctp_make_datafrag_empty(asoc, sinfo, over, frag,
315                                                  0, GFP_KERNEL);
316
317                 if (!chunk) {
318                         err = -ENOMEM;
319                         goto errout;
320                 }
321
322                 err = sctp_user_addto_chunk(chunk, over, from);
323
324                 /* Put the chunk->skb back into the form expected by send.  */
325                 __skb_pull(chunk->skb, (__u8 *)chunk->chunk_hdr
326                            - (__u8 *)chunk->skb->data);
327                 if (err < 0)
328                         goto errout_chunk_free;
329
330                 sctp_datamsg_assign(msg, chunk);
331                 list_add_tail(&chunk->frag_list, &msg->chunks);
332         }
333
334         return msg;
335
336 errout_chunk_free:
337         sctp_chunk_free(chunk);
338
339 errout:
340         list_for_each_safe(pos, temp, &msg->chunks) {
341                 list_del_init(pos);
342                 chunk = list_entry(pos, struct sctp_chunk, frag_list);
343                 sctp_chunk_free(chunk);
344         }
345         sctp_datamsg_put(msg);
346         return ERR_PTR(err);
347 }
348
349 /* Check whether this message has expired. */
350 int sctp_chunk_abandoned(struct sctp_chunk *chunk)
351 {
352         if (!chunk->asoc->prsctp_enable ||
353             !SCTP_PR_POLICY(chunk->sinfo.sinfo_flags)) {
354                 struct sctp_datamsg *msg = chunk->msg;
355
356                 if (!msg->can_abandon)
357                         return 0;
358
359                 if (time_after(jiffies, msg->expires_at))
360                         return 1;
361
362                 return 0;
363         }
364
365         if (SCTP_PR_TTL_ENABLED(chunk->sinfo.sinfo_flags) &&
366             time_after(jiffies, chunk->prsctp_param)) {
367                 if (chunk->sent_count)
368                         chunk->asoc->abandoned_sent[SCTP_PR_INDEX(TTL)]++;
369                 else
370                         chunk->asoc->abandoned_unsent[SCTP_PR_INDEX(TTL)]++;
371                 return 1;
372         } else if (SCTP_PR_RTX_ENABLED(chunk->sinfo.sinfo_flags) &&
373                    chunk->sent_count > chunk->prsctp_param) {
374                 chunk->asoc->abandoned_sent[SCTP_PR_INDEX(RTX)]++;
375                 return 1;
376         }
377         /* PRIO policy is processed by sendmsg, not here */
378
379         return 0;
380 }
381
382 /* This chunk (and consequently entire message) has failed in its sending. */
383 void sctp_chunk_fail(struct sctp_chunk *chunk, int error)
384 {
385         chunk->msg->send_failed = 1;
386         chunk->msg->send_error = error;
387 }