blob: 7182c5a450fb5b804d19fdb1b04b75a2c34eb2d0 [file] [log] [blame]
Thomas Gleixner47505b82019-05-23 11:14:41 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Vlad Yasevich60c778b2008-01-11 09:57:09 -05002/* SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2002 International Business Machines, Corp.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -05007 * This file is part of the SCTP kernel implementation
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * These functions are the methods for accessing the SCTP inqueue.
10 *
11 * An SCTP inqueue is a queue into which you push SCTP packets
12 * (which might be bundles or fragments of chunks) and out of which you
13 * pop SCTP whole chunks.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +090014 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Please send any bug reports or fixes you make to the
16 * email address(es):
Daniel Borkmann91705c62013-07-23 14:51:47 +020017 * lksctp developers <linux-sctp@vger.kernel.org>
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +090018 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +090019 * Written or modified by:
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 * La Monte H.P. Yarroll <piggy@acm.org>
21 * Karl Knutson <karl@athena.chicago.il.us>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 */
23
Joe Perches145ce502010-08-24 13:21:08 +000024#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <net/sctp/sctp.h>
27#include <net/sctp/sm.h>
28#include <linux/interrupt.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31/* Initialize an SCTP inqueue. */
32void sctp_inq_init(struct sctp_inq *queue)
33{
David S. Miller79af02c2005-07-08 21:47:49 -070034 INIT_LIST_HEAD(&queue->in_chunk_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 queue->in_progress = NULL;
36
37 /* Create a task for delivering data. */
David Howellsc4028952006-11-22 14:57:56 +000038 INIT_WORK(&queue->immediate, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039}
40
41/* Release the memory associated with an SCTP inqueue. */
42void sctp_inq_free(struct sctp_inq *queue)
43{
David S. Miller79af02c2005-07-08 21:47:49 -070044 struct sctp_chunk *chunk, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46 /* Empty the queue. */
David S. Miller79af02c2005-07-08 21:47:49 -070047 list_for_each_entry_safe(chunk, tmp, &queue->in_chunk_list, list) {
48 list_del_init(&chunk->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 sctp_chunk_free(chunk);
David S. Miller79af02c2005-07-08 21:47:49 -070050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52 /* If there is a packet which is currently being worked on,
53 * free it as well.
54 */
Sridhar Samudrala7a48f922006-01-17 11:51:28 -080055 if (queue->in_progress) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 sctp_chunk_free(queue->in_progress);
Sridhar Samudrala7a48f922006-01-17 11:51:28 -080057 queue->in_progress = NULL;
58 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
60
61/* Put a new packet in an SCTP inqueue.
62 * We assume that packet->sctp_hdr is set and in host byte order.
63 */
Sridhar Samudralaac0b0462006-08-22 00:15:33 -070064void sctp_inq_push(struct sctp_inq *q, struct sctp_chunk *chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
66 /* Directly call the packet handling routine. */
Vlad Yasevich027f6e12007-11-07 11:39:27 -050067 if (chunk->rcvr->dead) {
68 sctp_chunk_free(chunk);
69 return;
70 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 /* We are now calling this either from the soft interrupt
73 * or from the backlog processing.
74 * Eventually, we should clean up inqueue to not rely
75 * on the BH related data structures.
76 */
Sridhar Samudralaac0b0462006-08-22 00:15:33 -070077 list_add_tail(&chunk->list, &q->in_chunk_list);
Michele Baldessari196d6752012-12-01 04:49:42 +000078 if (chunk->asoc)
79 chunk->asoc->stats.ipackets++;
David Howellsc4028952006-11-22 14:57:56 +000080 q->immediate.func(&q->immediate);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
Vlad Yasevichbbd0d592007-10-03 17:51:34 -070083/* Peek at the next chunk on the inqeue. */
84struct sctp_chunkhdr *sctp_inq_peek(struct sctp_inq *queue)
85{
86 struct sctp_chunk *chunk;
Xin Long922dbc52017-06-30 11:52:13 +080087 struct sctp_chunkhdr *ch = NULL;
Vlad Yasevichbbd0d592007-10-03 17:51:34 -070088
89 chunk = queue->in_progress;
90 /* If there is no more chunks in this packet, say so */
91 if (chunk->singleton ||
92 chunk->end_of_packet ||
93 chunk->pdiscard)
94 return NULL;
95
Xin Long922dbc52017-06-30 11:52:13 +080096 ch = (struct sctp_chunkhdr *)chunk->chunk_end;
Vlad Yasevichbbd0d592007-10-03 17:51:34 -070097
98 return ch;
99}
100
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102/* Extract a chunk from an SCTP inqueue.
103 *
104 * WARNING: If you need to put the chunk on another queue, you need to
105 * make a shallow copy (clone) of it.
106 */
107struct sctp_chunk *sctp_inq_pop(struct sctp_inq *queue)
108{
109 struct sctp_chunk *chunk;
Xin Long922dbc52017-06-30 11:52:13 +0800110 struct sctp_chunkhdr *ch = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 /* The assumption is that we are safe to process the chunks
113 * at this time.
114 */
115
Marcelo Ricardo Leitner3acb50c2016-06-02 15:05:42 -0300116 chunk = queue->in_progress;
117 if (chunk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 /* There is a packet that we have been working on.
119 * Any post processing work to do before we move on?
120 */
121 if (chunk->singleton ||
122 chunk->end_of_packet ||
123 chunk->pdiscard) {
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300124 if (chunk->head_skb == chunk->skb) {
125 chunk->skb = skb_shinfo(chunk->skb)->frag_list;
126 goto new_skb;
127 }
128 if (chunk->skb->next) {
129 chunk->skb = chunk->skb->next;
130 goto new_skb;
131 }
132
133 if (chunk->head_skb)
134 chunk->skb = chunk->head_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 sctp_chunk_free(chunk);
136 chunk = queue->in_progress = NULL;
137 } else {
138 /* Nothing to do. Next chunk in the packet, please. */
Xin Long922dbc52017-06-30 11:52:13 +0800139 ch = (struct sctp_chunkhdr *)chunk->chunk_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 /* Force chunk->skb->data to chunk->chunk_end. */
Daniel Borkmann26b87c72014-10-09 22:55:33 +0200141 skb_pull(chunk->skb, chunk->chunk_end - chunk->skb->data);
142 /* We are guaranteed to pull a SCTP header. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
144 }
145
146 /* Do we need to take the next packet out of the queue to process? */
147 if (!chunk) {
David S. Miller79af02c2005-07-08 21:47:49 -0700148 struct list_head *entry;
149
Marcelo Ricardo Leitner3acb50c2016-06-02 15:05:42 -0300150next_chunk:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 /* Is the queue empty? */
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300152 entry = sctp_list_dequeue(&queue->in_chunk_list);
153 if (!entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 return NULL;
155
Marcelo Ricardo Leitner3acb50c2016-06-02 15:05:42 -0300156 chunk = list_entry(entry, struct sctp_chunk, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Daniel Axtens1dd27cd2018-03-09 14:06:09 +1100158 if (skb_is_gso(chunk->skb) && skb_is_gso_sctp(chunk->skb)) {
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300159 /* GSO-marked skbs but without frags, handle
160 * them normally
161 */
162 if (skb_shinfo(chunk->skb)->frag_list)
163 chunk->head_skb = chunk->skb;
Marcelo Ricardo Leitner3acb50c2016-06-02 15:05:42 -0300164
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300165 /* skbs with "cover letter" */
166 if (chunk->head_skb && chunk->skb->data_len == chunk->skb->len)
167 chunk->skb = skb_shinfo(chunk->skb)->frag_list;
168
169 if (WARN_ON(!chunk->skb)) {
170 __SCTP_INC_STATS(dev_net(chunk->skb->dev), SCTP_MIB_IN_PKT_DISCARDS);
171 sctp_chunk_free(chunk);
172 goto next_chunk;
173 }
174 }
Marcelo Ricardo Leitner486bdee2016-04-12 18:11:31 -0300175
176 if (chunk->asoc)
177 sock_rps_save_rxhash(chunk->asoc->base.sk, chunk->skb);
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300178
179 queue->in_progress = chunk;
180
181new_skb:
182 /* This is the first chunk in the packet. */
Xin Long922dbc52017-06-30 11:52:13 +0800183 ch = (struct sctp_chunkhdr *)chunk->skb->data;
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300184 chunk->singleton = 1;
185 chunk->data_accepted = 0;
186 chunk->pdiscard = 0;
187 chunk->auth = 0;
188 chunk->has_asconf = 0;
189 chunk->end_of_packet = 0;
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -0300190 if (chunk->head_skb) {
191 struct sctp_input_cb
192 *cb = SCTP_INPUT_CB(chunk->skb),
193 *head_cb = SCTP_INPUT_CB(chunk->head_skb);
194
195 cb->chunk = head_cb->chunk;
Marcelo Ricardo Leitnere7487c82016-07-13 15:08:58 -0300196 cb->af = head_cb->af;
Marcelo Ricardo Leitner1f45f782016-07-13 15:08:57 -0300197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
199
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900200 chunk->chunk_hdr = ch;
Marcelo Ricardo Leitnere2f036a2016-09-21 08:45:55 -0300201 chunk->chunk_end = ((__u8 *)ch) + SCTP_PAD4(ntohs(ch->length));
Xin Long922dbc52017-06-30 11:52:13 +0800202 skb_pull(chunk->skb, sizeof(*ch));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 chunk->subh.v = NULL; /* Subheader is no longer valid. */
204
Xin Longce402f02018-05-02 13:45:12 +0800205 if (chunk->chunk_end + sizeof(*ch) <= skb_tail_pointer(chunk->skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 /* This is not a singleton */
207 chunk->singleton = 0;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700208 } else if (chunk->chunk_end > skb_tail_pointer(chunk->skb)) {
Daniel Borkmann26b87c72014-10-09 22:55:33 +0200209 /* Discard inside state machine. */
210 chunk->pdiscard = 1;
211 chunk->chunk_end = skb_tail_pointer(chunk->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 } else {
213 /* We are at the end of the packet, so mark the chunk
214 * in case we need to send a SACK.
215 */
216 chunk->end_of_packet = 1;
217 }
218
Daniel Borkmannbb333812013-06-28 19:49:40 +0200219 pr_debug("+++sctp_inq_pop+++ chunk:%p[%s], length:%d, skb->len:%d\n",
220 chunk, sctp_cname(SCTP_ST_CHUNK(chunk->chunk_hdr->type)),
221 ntohs(chunk->chunk_hdr->length), chunk->skb->len);
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return chunk;
224}
225
226/* Set a top-half handler.
227 *
228 * Originally, we the top-half handler was scheduled as a BH. We now
229 * call the handler directly in sctp_inq_push() at a time that
230 * we know we are lock safe.
231 * The intent is that this routine will pull stuff out of the
232 * inqueue and process it.
233 */
David Howellsc4028952006-11-22 14:57:56 +0000234void sctp_inq_set_th_handler(struct sctp_inq *q, work_func_t callback)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
David Howellsc4028952006-11-22 14:57:56 +0000236 INIT_WORK(&q->immediate, callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}