147505b8bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
260c778b2SVlad Yasevich /* SCTP kernel implementation
31da177e4SLinus Torvalds * Copyright (c) 1999-2000 Cisco, Inc.
41da177e4SLinus Torvalds * Copyright (c) 1999-2001 Motorola, Inc.
51da177e4SLinus Torvalds * Copyright (c) 2002 International Business Machines, Corp.
61da177e4SLinus Torvalds *
760c778b2SVlad Yasevich * This file is part of the SCTP kernel implementation
81da177e4SLinus Torvalds *
91da177e4SLinus Torvalds * These functions are the methods for accessing the SCTP inqueue.
101da177e4SLinus Torvalds *
111da177e4SLinus Torvalds * An SCTP inqueue is a queue into which you push SCTP packets
121da177e4SLinus Torvalds * (which might be bundles or fragments of chunks) and out of which you
131da177e4SLinus Torvalds * pop SCTP whole chunks.
141da177e4SLinus Torvalds *
151da177e4SLinus Torvalds * Please send any bug reports or fixes you make to the
161da177e4SLinus Torvalds * email address(es):
1791705c61SDaniel Borkmann * lksctp developers <linux-sctp@vger.kernel.org>
181da177e4SLinus Torvalds *
191da177e4SLinus Torvalds * Written or modified by:
201da177e4SLinus Torvalds * La Monte H.P. Yarroll <piggy@acm.org>
211da177e4SLinus Torvalds * Karl Knutson <karl@athena.chicago.il.us>
221da177e4SLinus Torvalds */
231da177e4SLinus Torvalds
24145ce502SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25145ce502SJoe Perches
261da177e4SLinus Torvalds #include <net/sctp/sctp.h>
271da177e4SLinus Torvalds #include <net/sctp/sm.h>
281da177e4SLinus Torvalds #include <linux/interrupt.h>
295a0e3ad6STejun Heo #include <linux/slab.h>
301da177e4SLinus Torvalds
311da177e4SLinus Torvalds /* Initialize an SCTP inqueue. */
sctp_inq_init(struct sctp_inq * queue)321da177e4SLinus Torvalds void sctp_inq_init(struct sctp_inq *queue)
331da177e4SLinus Torvalds {
3479af02c2SDavid S. Miller INIT_LIST_HEAD(&queue->in_chunk_list);
351da177e4SLinus Torvalds queue->in_progress = NULL;
361da177e4SLinus Torvalds
371da177e4SLinus Torvalds /* Create a task for delivering data. */
38c4028958SDavid Howells INIT_WORK(&queue->immediate, NULL);
391da177e4SLinus Torvalds }
401da177e4SLinus Torvalds
41*4e45170dSDmitry Antipov /* Properly release the chunk which is being worked on. */
sctp_inq_chunk_free(struct sctp_chunk * chunk)42*4e45170dSDmitry Antipov static inline void sctp_inq_chunk_free(struct sctp_chunk *chunk)
43*4e45170dSDmitry Antipov {
44*4e45170dSDmitry Antipov if (chunk->head_skb)
45*4e45170dSDmitry Antipov chunk->skb = chunk->head_skb;
46*4e45170dSDmitry Antipov sctp_chunk_free(chunk);
47*4e45170dSDmitry Antipov }
48*4e45170dSDmitry Antipov
491da177e4SLinus Torvalds /* Release the memory associated with an SCTP inqueue. */
sctp_inq_free(struct sctp_inq * queue)501da177e4SLinus Torvalds void sctp_inq_free(struct sctp_inq *queue)
511da177e4SLinus Torvalds {
5279af02c2SDavid S. Miller struct sctp_chunk *chunk, *tmp;
531da177e4SLinus Torvalds
541da177e4SLinus Torvalds /* Empty the queue. */
5579af02c2SDavid S. Miller list_for_each_entry_safe(chunk, tmp, &queue->in_chunk_list, list) {
5679af02c2SDavid S. Miller list_del_init(&chunk->list);
571da177e4SLinus Torvalds sctp_chunk_free(chunk);
5879af02c2SDavid S. Miller }
591da177e4SLinus Torvalds
601da177e4SLinus Torvalds /* If there is a packet which is currently being worked on,
611da177e4SLinus Torvalds * free it as well.
621da177e4SLinus Torvalds */
637a48f923SSridhar Samudrala if (queue->in_progress) {
64*4e45170dSDmitry Antipov sctp_inq_chunk_free(queue->in_progress);
657a48f923SSridhar Samudrala queue->in_progress = NULL;
667a48f923SSridhar Samudrala }
671da177e4SLinus Torvalds }
681da177e4SLinus Torvalds
691da177e4SLinus Torvalds /* Put a new packet in an SCTP inqueue.
701da177e4SLinus Torvalds * We assume that packet->sctp_hdr is set and in host byte order.
711da177e4SLinus Torvalds */
sctp_inq_push(struct sctp_inq * q,struct sctp_chunk * chunk)72ac0b0462SSridhar Samudrala void sctp_inq_push(struct sctp_inq *q, struct sctp_chunk *chunk)
731da177e4SLinus Torvalds {
741da177e4SLinus Torvalds /* Directly call the packet handling routine. */
75027f6e1aSVlad Yasevich if (chunk->rcvr->dead) {
76027f6e1aSVlad Yasevich sctp_chunk_free(chunk);
77027f6e1aSVlad Yasevich return;
78027f6e1aSVlad Yasevich }
791da177e4SLinus Torvalds
801da177e4SLinus Torvalds /* We are now calling this either from the soft interrupt
811da177e4SLinus Torvalds * or from the backlog processing.
821da177e4SLinus Torvalds * Eventually, we should clean up inqueue to not rely
831da177e4SLinus Torvalds * on the BH related data structures.
841da177e4SLinus Torvalds */
85ac0b0462SSridhar Samudrala list_add_tail(&chunk->list, &q->in_chunk_list);
86196d6759SMichele Baldessari if (chunk->asoc)
87196d6759SMichele Baldessari chunk->asoc->stats.ipackets++;
88c4028958SDavid Howells q->immediate.func(&q->immediate);
891da177e4SLinus Torvalds }
901da177e4SLinus Torvalds
91bbd0d598SVlad Yasevich /* Peek at the next chunk on the inqeue. */
sctp_inq_peek(struct sctp_inq * queue)92bbd0d598SVlad Yasevich struct sctp_chunkhdr *sctp_inq_peek(struct sctp_inq *queue)
93bbd0d598SVlad Yasevich {
94bbd0d598SVlad Yasevich struct sctp_chunk *chunk;
95922dbc5bSXin Long struct sctp_chunkhdr *ch = NULL;
96bbd0d598SVlad Yasevich
97bbd0d598SVlad Yasevich chunk = queue->in_progress;
98bbd0d598SVlad Yasevich /* If there is no more chunks in this packet, say so */
99bbd0d598SVlad Yasevich if (chunk->singleton ||
100bbd0d598SVlad Yasevich chunk->end_of_packet ||
101bbd0d598SVlad Yasevich chunk->pdiscard)
102bbd0d598SVlad Yasevich return NULL;
103bbd0d598SVlad Yasevich
104922dbc5bSXin Long ch = (struct sctp_chunkhdr *)chunk->chunk_end;
105bbd0d598SVlad Yasevich
106bbd0d598SVlad Yasevich return ch;
107bbd0d598SVlad Yasevich }
108bbd0d598SVlad Yasevich
109bbd0d598SVlad Yasevich
1101da177e4SLinus Torvalds /* Extract a chunk from an SCTP inqueue.
1111da177e4SLinus Torvalds *
1121da177e4SLinus Torvalds * WARNING: If you need to put the chunk on another queue, you need to
1131da177e4SLinus Torvalds * make a shallow copy (clone) of it.
1141da177e4SLinus Torvalds */
sctp_inq_pop(struct sctp_inq * queue)1151da177e4SLinus Torvalds struct sctp_chunk *sctp_inq_pop(struct sctp_inq *queue)
1161da177e4SLinus Torvalds {
1171da177e4SLinus Torvalds struct sctp_chunk *chunk;
118922dbc5bSXin Long struct sctp_chunkhdr *ch = NULL;
1191da177e4SLinus Torvalds
1201da177e4SLinus Torvalds /* The assumption is that we are safe to process the chunks
1211da177e4SLinus Torvalds * at this time.
1221da177e4SLinus Torvalds */
1231da177e4SLinus Torvalds
1243acb50c1SMarcelo Ricardo Leitner chunk = queue->in_progress;
1253acb50c1SMarcelo Ricardo Leitner if (chunk) {
1261da177e4SLinus Torvalds /* There is a packet that we have been working on.
1271da177e4SLinus Torvalds * Any post processing work to do before we move on?
1281da177e4SLinus Torvalds */
1291da177e4SLinus Torvalds if (chunk->singleton ||
1301da177e4SLinus Torvalds chunk->end_of_packet ||
1311da177e4SLinus Torvalds chunk->pdiscard) {
13290017accSMarcelo Ricardo Leitner if (chunk->head_skb == chunk->skb) {
13390017accSMarcelo Ricardo Leitner chunk->skb = skb_shinfo(chunk->skb)->frag_list;
13490017accSMarcelo Ricardo Leitner goto new_skb;
13590017accSMarcelo Ricardo Leitner }
13690017accSMarcelo Ricardo Leitner if (chunk->skb->next) {
13790017accSMarcelo Ricardo Leitner chunk->skb = chunk->skb->next;
13890017accSMarcelo Ricardo Leitner goto new_skb;
13990017accSMarcelo Ricardo Leitner }
14090017accSMarcelo Ricardo Leitner
141*4e45170dSDmitry Antipov sctp_inq_chunk_free(chunk);
1421da177e4SLinus Torvalds chunk = queue->in_progress = NULL;
1431da177e4SLinus Torvalds } else {
1441da177e4SLinus Torvalds /* Nothing to do. Next chunk in the packet, please. */
145922dbc5bSXin Long ch = (struct sctp_chunkhdr *)chunk->chunk_end;
1461da177e4SLinus Torvalds /* Force chunk->skb->data to chunk->chunk_end. */
14726b87c78SDaniel Borkmann skb_pull(chunk->skb, chunk->chunk_end - chunk->skb->data);
14826b87c78SDaniel Borkmann /* We are guaranteed to pull a SCTP header. */
1491da177e4SLinus Torvalds }
1501da177e4SLinus Torvalds }
1511da177e4SLinus Torvalds
1521da177e4SLinus Torvalds /* Do we need to take the next packet out of the queue to process? */
1531da177e4SLinus Torvalds if (!chunk) {
15479af02c2SDavid S. Miller struct list_head *entry;
15579af02c2SDavid S. Miller
1563acb50c1SMarcelo Ricardo Leitner next_chunk:
1571da177e4SLinus Torvalds /* Is the queue empty? */
15890017accSMarcelo Ricardo Leitner entry = sctp_list_dequeue(&queue->in_chunk_list);
15990017accSMarcelo Ricardo Leitner if (!entry)
1601da177e4SLinus Torvalds return NULL;
1611da177e4SLinus Torvalds
1623acb50c1SMarcelo Ricardo Leitner chunk = list_entry(entry, struct sctp_chunk, list);
1631da177e4SLinus Torvalds
1641dd27cdeSDaniel Axtens if (skb_is_gso(chunk->skb) && skb_is_gso_sctp(chunk->skb)) {
16590017accSMarcelo Ricardo Leitner /* GSO-marked skbs but without frags, handle
16690017accSMarcelo Ricardo Leitner * them normally
16790017accSMarcelo Ricardo Leitner */
16890017accSMarcelo Ricardo Leitner if (skb_shinfo(chunk->skb)->frag_list)
16990017accSMarcelo Ricardo Leitner chunk->head_skb = chunk->skb;
1703acb50c1SMarcelo Ricardo Leitner
17190017accSMarcelo Ricardo Leitner /* skbs with "cover letter" */
17290017accSMarcelo Ricardo Leitner if (chunk->head_skb && chunk->skb->data_len == chunk->skb->len)
17390017accSMarcelo Ricardo Leitner chunk->skb = skb_shinfo(chunk->skb)->frag_list;
17490017accSMarcelo Ricardo Leitner
17590017accSMarcelo Ricardo Leitner if (WARN_ON(!chunk->skb)) {
17690017accSMarcelo Ricardo Leitner __SCTP_INC_STATS(dev_net(chunk->skb->dev), SCTP_MIB_IN_PKT_DISCARDS);
17790017accSMarcelo Ricardo Leitner sctp_chunk_free(chunk);
17890017accSMarcelo Ricardo Leitner goto next_chunk;
17990017accSMarcelo Ricardo Leitner }
18090017accSMarcelo Ricardo Leitner }
181486bdee0SMarcelo Ricardo Leitner
182486bdee0SMarcelo Ricardo Leitner if (chunk->asoc)
183486bdee0SMarcelo Ricardo Leitner sock_rps_save_rxhash(chunk->asoc->base.sk, chunk->skb);
18490017accSMarcelo Ricardo Leitner
18590017accSMarcelo Ricardo Leitner queue->in_progress = chunk;
18690017accSMarcelo Ricardo Leitner
18790017accSMarcelo Ricardo Leitner new_skb:
18890017accSMarcelo Ricardo Leitner /* This is the first chunk in the packet. */
189922dbc5bSXin Long ch = (struct sctp_chunkhdr *)chunk->skb->data;
19090017accSMarcelo Ricardo Leitner chunk->singleton = 1;
19190017accSMarcelo Ricardo Leitner chunk->data_accepted = 0;
19290017accSMarcelo Ricardo Leitner chunk->pdiscard = 0;
19390017accSMarcelo Ricardo Leitner chunk->auth = 0;
19490017accSMarcelo Ricardo Leitner chunk->has_asconf = 0;
19590017accSMarcelo Ricardo Leitner chunk->end_of_packet = 0;
1961f45f78fSMarcelo Ricardo Leitner if (chunk->head_skb) {
1971f45f78fSMarcelo Ricardo Leitner struct sctp_input_cb
1981f45f78fSMarcelo Ricardo Leitner *cb = SCTP_INPUT_CB(chunk->skb),
1991f45f78fSMarcelo Ricardo Leitner *head_cb = SCTP_INPUT_CB(chunk->head_skb);
2001f45f78fSMarcelo Ricardo Leitner
2011f45f78fSMarcelo Ricardo Leitner cb->chunk = head_cb->chunk;
202e7487c86SMarcelo Ricardo Leitner cb->af = head_cb->af;
2031f45f78fSMarcelo Ricardo Leitner }
2041da177e4SLinus Torvalds }
2051da177e4SLinus Torvalds
2061da177e4SLinus Torvalds chunk->chunk_hdr = ch;
207e2f036a9SMarcelo Ricardo Leitner chunk->chunk_end = ((__u8 *)ch) + SCTP_PAD4(ntohs(ch->length));
208922dbc5bSXin Long skb_pull(chunk->skb, sizeof(*ch));
2091da177e4SLinus Torvalds chunk->subh.v = NULL; /* Subheader is no longer valid. */
2101da177e4SLinus Torvalds
211ce402f04SXin Long if (chunk->chunk_end + sizeof(*ch) <= skb_tail_pointer(chunk->skb)) {
2121da177e4SLinus Torvalds /* This is not a singleton */
2131da177e4SLinus Torvalds chunk->singleton = 0;
21427a884dcSArnaldo Carvalho de Melo } else if (chunk->chunk_end > skb_tail_pointer(chunk->skb)) {
21526b87c78SDaniel Borkmann /* Discard inside state machine. */
21626b87c78SDaniel Borkmann chunk->pdiscard = 1;
21726b87c78SDaniel Borkmann chunk->chunk_end = skb_tail_pointer(chunk->skb);
2181da177e4SLinus Torvalds } else {
2191da177e4SLinus Torvalds /* We are at the end of the packet, so mark the chunk
2201da177e4SLinus Torvalds * in case we need to send a SACK.
2211da177e4SLinus Torvalds */
2221da177e4SLinus Torvalds chunk->end_of_packet = 1;
2231da177e4SLinus Torvalds }
2241da177e4SLinus Torvalds
225bb33381dSDaniel Borkmann pr_debug("+++sctp_inq_pop+++ chunk:%p[%s], length:%d, skb->len:%d\n",
226bb33381dSDaniel Borkmann chunk, sctp_cname(SCTP_ST_CHUNK(chunk->chunk_hdr->type)),
2271da177e4SLinus Torvalds ntohs(chunk->chunk_hdr->length), chunk->skb->len);
228bb33381dSDaniel Borkmann
2291da177e4SLinus Torvalds return chunk;
2301da177e4SLinus Torvalds }
2311da177e4SLinus Torvalds
2321da177e4SLinus Torvalds /* Set a top-half handler.
2331da177e4SLinus Torvalds *
2341da177e4SLinus Torvalds * Originally, we the top-half handler was scheduled as a BH. We now
2351da177e4SLinus Torvalds * call the handler directly in sctp_inq_push() at a time that
2361da177e4SLinus Torvalds * we know we are lock safe.
2371da177e4SLinus Torvalds * The intent is that this routine will pull stuff out of the
2381da177e4SLinus Torvalds * inqueue and process it.
2391da177e4SLinus Torvalds */
sctp_inq_set_th_handler(struct sctp_inq * q,work_func_t callback)240c4028958SDavid Howells void sctp_inq_set_th_handler(struct sctp_inq *q, work_func_t callback)
2411da177e4SLinus Torvalds {
242c4028958SDavid Howells INIT_WORK(&q->immediate, callback);
2431da177e4SLinus Torvalds }
244