1/* SCTP kernel implementation
2 * Copyright (c) 1999-2000 Cisco, Inc.
3 * Copyright (c) 1999-2001 Motorola, Inc.
4 * Copyright (c) 2002 International Business Machines, Corp.
5 *
6 * This file is part of the SCTP kernel implementation
7 *
8 * These functions are the methods for accessing the SCTP inqueue.
9 *
10 * An SCTP inqueue is a queue into which you push SCTP packets
11 * (which might be bundles or fragments of chunks) and out of which you
12 * pop SCTP whole chunks.
13 *
14 * This SCTP implementation is free software;
15 * you can redistribute it and/or modify it under the terms of
16 * the GNU General Public License as published by
17 * the Free Software Foundation; either version 2, or (at your option)
18 * any later version.
19 *
20 * This SCTP implementation is distributed in the hope that it
21 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
22 *                 ************************
23 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
24 * See the GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with GNU CC; see the file COPYING.  If not, see
28 * <http://www.gnu.org/licenses/>.
29 *
30 * Please send any bug reports or fixes you make to the
31 * email address(es):
32 *    lksctp developers <linux-sctp@vger.kernel.org>
33 *
34 * Written or modified by:
35 *    La Monte H.P. Yarroll <piggy@acm.org>
36 *    Karl Knutson <karl@athena.chicago.il.us>
37 */
38
39#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
40
41#include <net/sctp/sctp.h>
42#include <net/sctp/sm.h>
43#include <linux/interrupt.h>
44#include <linux/slab.h>
45
46/* Initialize an SCTP inqueue.  */
47void sctp_inq_init(struct sctp_inq *queue)
48{
49	INIT_LIST_HEAD(&queue->in_chunk_list);
50	queue->in_progress = NULL;
51
52	/* Create a task for delivering data.  */
53	INIT_WORK(&queue->immediate, NULL);
54}
55
56/* Release the memory associated with an SCTP inqueue.  */
57void sctp_inq_free(struct sctp_inq *queue)
58{
59	struct sctp_chunk *chunk, *tmp;
60
61	/* Empty the queue.  */
62	list_for_each_entry_safe(chunk, tmp, &queue->in_chunk_list, list) {
63		list_del_init(&chunk->list);
64		sctp_chunk_free(chunk);
65	}
66
67	/* If there is a packet which is currently being worked on,
68	 * free it as well.
69	 */
70	if (queue->in_progress) {
71		sctp_chunk_free(queue->in_progress);
72		queue->in_progress = NULL;
73	}
74}
75
76/* Put a new packet in an SCTP inqueue.
77 * We assume that packet->sctp_hdr is set and in host byte order.
78 */
79void sctp_inq_push(struct sctp_inq *q, struct sctp_chunk *chunk)
80{
81	/* Directly call the packet handling routine. */
82	if (chunk->rcvr->dead) {
83		sctp_chunk_free(chunk);
84		return;
85	}
86
87	/* We are now calling this either from the soft interrupt
88	 * or from the backlog processing.
89	 * Eventually, we should clean up inqueue to not rely
90	 * on the BH related data structures.
91	 */
92	list_add_tail(&chunk->list, &q->in_chunk_list);
93	if (chunk->asoc)
94		chunk->asoc->stats.ipackets++;
95	q->immediate.func(&q->immediate);
96}
97
98/* Peek at the next chunk on the inqeue. */
99struct sctp_chunkhdr *sctp_inq_peek(struct sctp_inq *queue)
100{
101	struct sctp_chunk *chunk;
102	sctp_chunkhdr_t *ch = NULL;
103
104	chunk = queue->in_progress;
105	/* If there is no more chunks in this packet, say so */
106	if (chunk->singleton ||
107	    chunk->end_of_packet ||
108	    chunk->pdiscard)
109		    return NULL;
110
111	ch = (sctp_chunkhdr_t *)chunk->chunk_end;
112
113	return ch;
114}
115
116
117/* Extract a chunk from an SCTP inqueue.
118 *
119 * WARNING:  If you need to put the chunk on another queue, you need to
120 * make a shallow copy (clone) of it.
121 */
122struct sctp_chunk *sctp_inq_pop(struct sctp_inq *queue)
123{
124	struct sctp_chunk *chunk;
125	sctp_chunkhdr_t *ch = NULL;
126
127	/* The assumption is that we are safe to process the chunks
128	 * at this time.
129	 */
130
131	if ((chunk = queue->in_progress)) {
132		/* There is a packet that we have been working on.
133		 * Any post processing work to do before we move on?
134		 */
135		if (chunk->singleton ||
136		    chunk->end_of_packet ||
137		    chunk->pdiscard) {
138			sctp_chunk_free(chunk);
139			chunk = queue->in_progress = NULL;
140		} else {
141			/* Nothing to do. Next chunk in the packet, please. */
142			ch = (sctp_chunkhdr_t *) chunk->chunk_end;
143			/* Force chunk->skb->data to chunk->chunk_end.  */
144			skb_pull(chunk->skb, chunk->chunk_end - chunk->skb->data);
145			/* We are guaranteed to pull a SCTP header. */
146		}
147	}
148
149	/* Do we need to take the next packet out of the queue to process? */
150	if (!chunk) {
151		struct list_head *entry;
152
153		/* Is the queue empty?  */
154		if (list_empty(&queue->in_chunk_list))
155			return NULL;
156
157		entry = queue->in_chunk_list.next;
158		chunk = queue->in_progress =
159			list_entry(entry, struct sctp_chunk, list);
160		list_del_init(entry);
161
162		/* This is the first chunk in the packet.  */
163		chunk->singleton = 1;
164		ch = (sctp_chunkhdr_t *) chunk->skb->data;
165		chunk->data_accepted = 0;
166	}
167
168	chunk->chunk_hdr = ch;
169	chunk->chunk_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
170	/* In the unlikely case of an IP reassembly, the skb could be
171	 * non-linear. If so, update chunk_end so that it doesn't go past
172	 * the skb->tail.
173	 */
174	if (unlikely(skb_is_nonlinear(chunk->skb))) {
175		if (chunk->chunk_end > skb_tail_pointer(chunk->skb))
176			chunk->chunk_end = skb_tail_pointer(chunk->skb);
177	}
178	skb_pull(chunk->skb, sizeof(sctp_chunkhdr_t));
179	chunk->subh.v = NULL; /* Subheader is no longer valid.  */
180
181	if (chunk->chunk_end + sizeof(sctp_chunkhdr_t) <
182	    skb_tail_pointer(chunk->skb)) {
183		/* This is not a singleton */
184		chunk->singleton = 0;
185	} else if (chunk->chunk_end > skb_tail_pointer(chunk->skb)) {
186		/* Discard inside state machine. */
187		chunk->pdiscard = 1;
188		chunk->chunk_end = skb_tail_pointer(chunk->skb);
189	} else {
190		/* We are at the end of the packet, so mark the chunk
191		 * in case we need to send a SACK.
192		 */
193		chunk->end_of_packet = 1;
194	}
195
196	pr_debug("+++sctp_inq_pop+++ chunk:%p[%s], length:%d, skb->len:%d\n",
197		 chunk, sctp_cname(SCTP_ST_CHUNK(chunk->chunk_hdr->type)),
198		 ntohs(chunk->chunk_hdr->length), chunk->skb->len);
199
200	return chunk;
201}
202
203/* Set a top-half handler.
204 *
205 * Originally, we the top-half handler was scheduled as a BH.  We now
206 * call the handler directly in sctp_inq_push() at a time that
207 * we know we are lock safe.
208 * The intent is that this routine will pull stuff out of the
209 * inqueue and process it.
210 */
211void sctp_inq_set_th_handler(struct sctp_inq *q, work_func_t callback)
212{
213	INIT_WORK(&q->immediate, callback);
214}
215