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) 2001-2003 International Business Machines, Corp.
61da177e4SLinus Torvalds * Copyright (c) 2001 Intel Corp.
71da177e4SLinus Torvalds * Copyright (c) 2001 Nokia, Inc.
81da177e4SLinus Torvalds * Copyright (c) 2001 La Monte H.P. Yarroll
91da177e4SLinus Torvalds *
1060c778b2SVlad Yasevich * This file is part of the SCTP kernel implementation
111da177e4SLinus Torvalds *
121da177e4SLinus Torvalds * These functions handle all input from the IP layer into SCTP.
131da177e4SLinus Torvalds *
141da177e4SLinus Torvalds * Please send any bug reports or fixes you make to the
151da177e4SLinus Torvalds * email address(es):
1691705c61SDaniel Borkmann * lksctp developers <linux-sctp@vger.kernel.org>
171da177e4SLinus Torvalds *
181da177e4SLinus Torvalds * Written or modified by:
191da177e4SLinus Torvalds * La Monte H.P. Yarroll <piggy@acm.org>
201da177e4SLinus Torvalds * Karl Knutson <karl@athena.chicago.il.us>
211da177e4SLinus Torvalds * Xingang Guo <xingang.guo@intel.com>
221da177e4SLinus Torvalds * Jon Grimm <jgrimm@us.ibm.com>
231da177e4SLinus Torvalds * Hui Huang <hui.huang@nokia.com>
241da177e4SLinus Torvalds * Daisy Chang <daisyc@us.ibm.com>
251da177e4SLinus Torvalds * Sridhar Samudrala <sri@us.ibm.com>
261da177e4SLinus Torvalds * Ardelle Fan <ardelle.fan@intel.com>
271da177e4SLinus Torvalds */
281da177e4SLinus Torvalds
291da177e4SLinus Torvalds #include <linux/types.h>
301da177e4SLinus Torvalds #include <linux/list.h> /* For struct list_head */
311da177e4SLinus Torvalds #include <linux/socket.h>
321da177e4SLinus Torvalds #include <linux/ip.h>
331da177e4SLinus Torvalds #include <linux/time.h> /* For struct timeval */
345a0e3ad6STejun Heo #include <linux/slab.h>
351da177e4SLinus Torvalds #include <net/ip.h>
361da177e4SLinus Torvalds #include <net/icmp.h>
371da177e4SLinus Torvalds #include <net/snmp.h>
381da177e4SLinus Torvalds #include <net/sock.h>
391da177e4SLinus Torvalds #include <net/xfrm.h>
401da177e4SLinus Torvalds #include <net/sctp/sctp.h>
411da177e4SLinus Torvalds #include <net/sctp/sm.h>
429ad0977fSVlad Yasevich #include <net/sctp/checksum.h>
43dcfc23caSPavel Emelyanov #include <net/net_namespace.h>
440eb71a9dSNeilBrown #include <linux/rhashtable.h>
45532ae2f1SXin Long #include <net/sock_reuseport.h>
461da177e4SLinus Torvalds
471da177e4SLinus Torvalds /* Forward declarations for internal helpers. */
481da177e4SLinus Torvalds static int sctp_rcv_ootb(struct sk_buff *);
494110cc25SEric W. Biederman static struct sctp_association *__sctp_rcv_lookup(struct net *net,
504110cc25SEric W. Biederman struct sk_buff *skb,
511da177e4SLinus Torvalds const union sctp_addr *paddr,
5257565993SNicolas Dichtel const union sctp_addr *laddr,
530af03170SXin Long struct sctp_transport **transportp,
540af03170SXin Long int dif, int sdif);
55532ae2f1SXin Long static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(
56532ae2f1SXin Long struct net *net, struct sk_buff *skb,
57532ae2f1SXin Long const union sctp_addr *laddr,
580af03170SXin Long const union sctp_addr *daddr,
590af03170SXin Long int dif, int sdif);
601da177e4SLinus Torvalds static struct sctp_association *__sctp_lookup_association(
614110cc25SEric W. Biederman struct net *net,
621da177e4SLinus Torvalds const union sctp_addr *local,
631da177e4SLinus Torvalds const union sctp_addr *peer,
640af03170SXin Long struct sctp_transport **pt,
650af03170SXin Long int dif, int sdif);
661da177e4SLinus Torvalds
6750b1a782SZhu Yi static int sctp_add_backlog(struct sock *sk, struct sk_buff *skb);
6861c9fed4SVladislav Yasevich
691da177e4SLinus Torvalds
701da177e4SLinus Torvalds /* Calculate the SCTP checksum of an SCTP packet. */
sctp_rcv_checksum(struct net * net,struct sk_buff * skb)71b01a2407SEric W. Biederman static inline int sctp_rcv_checksum(struct net *net, struct sk_buff *skb)
721da177e4SLinus Torvalds {
732c0fd387SArnaldo Carvalho de Melo struct sctphdr *sh = sctp_hdr(skb);
744458f04cSVlad Yasevich __le32 cmp = sh->checksum;
75024ec3deSJoe Stringer __le32 val = sctp_compute_cksum(skb, 0);
761da177e4SLinus Torvalds
771da177e4SLinus Torvalds if (val != cmp) {
781da177e4SLinus Torvalds /* CRC failure, dump it. */
7908e3baefSEric Dumazet __SCTP_INC_STATS(net, SCTP_MIB_CHECKSUMERRORS);
801da177e4SLinus Torvalds return -1;
811da177e4SLinus Torvalds }
821da177e4SLinus Torvalds return 0;
831da177e4SLinus Torvalds }
841da177e4SLinus Torvalds
851da177e4SLinus Torvalds /*
861da177e4SLinus Torvalds * This is the routine which IP calls when receiving an SCTP packet.
871da177e4SLinus Torvalds */
sctp_rcv(struct sk_buff * skb)881da177e4SLinus Torvalds int sctp_rcv(struct sk_buff *skb)
891da177e4SLinus Torvalds {
901da177e4SLinus Torvalds struct sock *sk;
911da177e4SLinus Torvalds struct sctp_association *asoc;
921da177e4SLinus Torvalds struct sctp_endpoint *ep = NULL;
931da177e4SLinus Torvalds struct sctp_ep_common *rcvr;
941da177e4SLinus Torvalds struct sctp_transport *transport = NULL;
951da177e4SLinus Torvalds struct sctp_chunk *chunk;
961da177e4SLinus Torvalds union sctp_addr src;
971da177e4SLinus Torvalds union sctp_addr dest;
981da177e4SLinus Torvalds int family;
991da177e4SLinus Torvalds struct sctp_af *af;
1004cdadcbcSEric W. Biederman struct net *net = dev_net(skb->dev);
1011dd27cdeSDaniel Axtens bool is_gso = skb_is_gso(skb) && skb_is_gso_sctp(skb);
1020af03170SXin Long int dif, sdif;
1031da177e4SLinus Torvalds
1041da177e4SLinus Torvalds if (skb->pkt_type != PACKET_HOST)
1051da177e4SLinus Torvalds goto discard_it;
1061da177e4SLinus Torvalds
10708e3baefSEric Dumazet __SCTP_INC_STATS(net, SCTP_MIB_INSCTPPACKS);
1081da177e4SLinus Torvalds
1093acb50c1SMarcelo Ricardo Leitner /* If packet is too small to contain a single chunk, let's not
1103acb50c1SMarcelo Ricardo Leitner * waste time on it anymore.
1113acb50c1SMarcelo Ricardo Leitner */
1123acb50c1SMarcelo Ricardo Leitner if (skb->len < sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr) +
1133acb50c1SMarcelo Ricardo Leitner skb_transport_offset(skb))
11428cd7752SHerbert Xu goto discard_it;
11528cd7752SHerbert Xu
1164c2f2454SMarcelo Ricardo Leitner /* If the packet is fragmented and we need to do crc checking,
1174c2f2454SMarcelo Ricardo Leitner * it's better to just linearize it otherwise crc computing
1184c2f2454SMarcelo Ricardo Leitner * takes longer.
1194c2f2454SMarcelo Ricardo Leitner */
1201dd27cdeSDaniel Axtens if ((!is_gso && skb_linearize(skb)) ||
1214c2f2454SMarcelo Ricardo Leitner !pskb_may_pull(skb, sizeof(struct sctphdr)))
1223acb50c1SMarcelo Ricardo Leitner goto discard_it;
1231da177e4SLinus Torvalds
1243acb50c1SMarcelo Ricardo Leitner /* Pull up the IP header. */
125ea2ae17dSArnaldo Carvalho de Melo __skb_pull(skb, skb_transport_offset(skb));
126202863feSTom Herbert
127202863feSTom Herbert skb->csum_valid = 0; /* Previous value not applicable */
128202863feSTom Herbert if (skb_csum_unnecessary(skb))
129202863feSTom Herbert __skb_decr_checksum_unnecessary(skb);
13090017accSMarcelo Ricardo Leitner else if (!sctp_checksum_disable &&
1311dd27cdeSDaniel Axtens !is_gso &&
13290017accSMarcelo Ricardo Leitner sctp_rcv_checksum(net, skb) < 0)
1331da177e4SLinus Torvalds goto discard_it;
134202863feSTom Herbert skb->csum_valid = 1;
1351da177e4SLinus Torvalds
1363acb50c1SMarcelo Ricardo Leitner __skb_pull(skb, sizeof(struct sctphdr));
1371da177e4SLinus Torvalds
138eddc9ec5SArnaldo Carvalho de Melo family = ipver2af(ip_hdr(skb)->version);
1391da177e4SLinus Torvalds af = sctp_get_af_specific(family);
1401da177e4SLinus Torvalds if (unlikely(!af))
1411da177e4SLinus Torvalds goto discard_it;
142e7487c86SMarcelo Ricardo Leitner SCTP_INPUT_CB(skb)->af = af;
1431da177e4SLinus Torvalds
1441da177e4SLinus Torvalds /* Initialize local addresses for lookups. */
1451da177e4SLinus Torvalds af->from_skb(&src, skb, 1);
1461da177e4SLinus Torvalds af->from_skb(&dest, skb, 0);
1470af03170SXin Long dif = af->skb_iif(skb);
1480af03170SXin Long sdif = af->skb_sdif(skb);
1491da177e4SLinus Torvalds
1501da177e4SLinus Torvalds /* If the packet is to or from a non-unicast address,
1511da177e4SLinus Torvalds * silently discard the packet.
1521da177e4SLinus Torvalds *
1531da177e4SLinus Torvalds * This is not clearly defined in the RFC except in section
1541da177e4SLinus Torvalds * 8.4 - OOTB handling. However, based on the book "Stream Control
1551da177e4SLinus Torvalds * Transmission Protocol" 2.1, "It is important to note that the
1561da177e4SLinus Torvalds * IP address of an SCTP transport address must be a routable
1571da177e4SLinus Torvalds * unicast address. In other words, IP multicast addresses and
1581da177e4SLinus Torvalds * IP broadcast addresses cannot be used in an SCTP transport
1591da177e4SLinus Torvalds * address."
1601da177e4SLinus Torvalds */
1615636bef7SVlad Yasevich if (!af->addr_valid(&src, NULL, skb) ||
1625636bef7SVlad Yasevich !af->addr_valid(&dest, NULL, skb))
1631da177e4SLinus Torvalds goto discard_it;
1641da177e4SLinus Torvalds
1650af03170SXin Long asoc = __sctp_rcv_lookup(net, skb, &src, &dest, &transport, dif, sdif);
1661c7d1fc1SAl Viro
1670fd9a65aSNeil Horman if (!asoc)
1680af03170SXin Long ep = __sctp_rcv_lookup_endpoint(net, skb, &dest, &src, dif, sdif);
1690fd9a65aSNeil Horman
1700fd9a65aSNeil Horman /* Retrieve the common input handling substructure. */
1710fd9a65aSNeil Horman rcvr = asoc ? &asoc->base : &ep->base;
1720fd9a65aSNeil Horman sk = rcvr->sk;
1730fd9a65aSNeil Horman
1740fd9a65aSNeil Horman /*
1751da177e4SLinus Torvalds * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
1761da177e4SLinus Torvalds * An SCTP packet is called an "out of the blue" (OOTB)
1771da177e4SLinus Torvalds * packet if it is correctly formed, i.e., passed the
1781da177e4SLinus Torvalds * receiver's checksum check, but the receiver is not
1791da177e4SLinus Torvalds * able to identify the association to which this
1801da177e4SLinus Torvalds * packet belongs.
1811da177e4SLinus Torvalds */
1821da177e4SLinus Torvalds if (!asoc) {
1831da177e4SLinus Torvalds if (sctp_rcv_ootb(skb)) {
18408e3baefSEric Dumazet __SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
1851da177e4SLinus Torvalds goto discard_release;
1861da177e4SLinus Torvalds }
1871da177e4SLinus Torvalds }
1881da177e4SLinus Torvalds
1891da177e4SLinus Torvalds if (!xfrm_policy_check(sk, XFRM_POLICY_IN, skb, family))
1901da177e4SLinus Torvalds goto discard_release;
191895b5c9fSFlorian Westphal nf_reset_ct(skb);
1921da177e4SLinus Torvalds
193fda9ef5dSDmitry Mishin if (sk_filter(sk, skb))
1941da177e4SLinus Torvalds goto discard_release;
1951da177e4SLinus Torvalds
1961da177e4SLinus Torvalds /* Create an SCTP packet structure. */
197cea8768fSMarcelo Ricardo Leitner chunk = sctp_chunkify(skb, asoc, sk, GFP_ATOMIC);
1982babf9daSHerbert Xu if (!chunk)
1991da177e4SLinus Torvalds goto discard_release;
20079af02c2SDavid S. Miller SCTP_INPUT_CB(skb)->chunk = chunk;
2011da177e4SLinus Torvalds
2021da177e4SLinus Torvalds /* Remember what endpoint is to handle this packet. */
2031da177e4SLinus Torvalds chunk->rcvr = rcvr;
2041da177e4SLinus Torvalds
2051da177e4SLinus Torvalds /* Remember the SCTP header. */
2063acb50c1SMarcelo Ricardo Leitner chunk->sctp_hdr = sctp_hdr(skb);
2071da177e4SLinus Torvalds
2081da177e4SLinus Torvalds /* Set the source and destination addresses of the incoming chunk. */
209d55c41b1SAl Viro sctp_init_addrs(chunk, &src, &dest);
2101da177e4SLinus Torvalds
2111da177e4SLinus Torvalds /* Remember where we came from. */
2121da177e4SLinus Torvalds chunk->transport = transport;
2131da177e4SLinus Torvalds
2141da177e4SLinus Torvalds /* Acquire access to the sock lock. Note: We are safe from other
2151da177e4SLinus Torvalds * bottom halves on this lock, but a user may be in the lock too,
2161da177e4SLinus Torvalds * so check if it is busy.
2171da177e4SLinus Torvalds */
2185bc1d1b4Swangweidong bh_lock_sock(sk);
2191da177e4SLinus Torvalds
220ae53b5bdSVlad Yasevich if (sk != rcvr->sk) {
221ae53b5bdSVlad Yasevich /* Our cached sk is different from the rcvr->sk. This is
222ae53b5bdSVlad Yasevich * because migrate()/accept() may have moved the association
223ae53b5bdSVlad Yasevich * to a new socket and released all the sockets. So now we
224ae53b5bdSVlad Yasevich * are holding a lock on the old socket while the user may
225ae53b5bdSVlad Yasevich * be doing something with the new socket. Switch our veiw
226ae53b5bdSVlad Yasevich * of the current sk.
227ae53b5bdSVlad Yasevich */
2285bc1d1b4Swangweidong bh_unlock_sock(sk);
229ae53b5bdSVlad Yasevich sk = rcvr->sk;
2305bc1d1b4Swangweidong bh_lock_sock(sk);
231ae53b5bdSVlad Yasevich }
232ae53b5bdSVlad Yasevich
233819be810SXin Long if (sock_owned_by_user(sk) || !sctp_newsk_ready(sk)) {
23450b1a782SZhu Yi if (sctp_add_backlog(sk, skb)) {
2355bc1d1b4Swangweidong bh_unlock_sock(sk);
23650b1a782SZhu Yi sctp_chunk_free(chunk);
23750b1a782SZhu Yi skb = NULL; /* sctp_chunk_free already freed the skb */
23850b1a782SZhu Yi goto discard_release;
23950b1a782SZhu Yi }
24008e3baefSEric Dumazet __SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_BACKLOG);
241ac0b0462SSridhar Samudrala } else {
24208e3baefSEric Dumazet __SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_SOFTIRQ);
24361c9fed4SVladislav Yasevich sctp_inq_push(&chunk->rcvr->inqueue, chunk);
244ac0b0462SSridhar Samudrala }
2451da177e4SLinus Torvalds
2465bc1d1b4Swangweidong bh_unlock_sock(sk);
24761c9fed4SVladislav Yasevich
24861c9fed4SVladislav Yasevich /* Release the asoc/ep ref we took in the lookup calls. */
249dae399d7SXin Long if (transport)
250dae399d7SXin Long sctp_transport_put(transport);
25161c9fed4SVladislav Yasevich else
25261c9fed4SVladislav Yasevich sctp_endpoint_put(ep);
2537a48f923SSridhar Samudrala
2542babf9daSHerbert Xu return 0;
2551da177e4SLinus Torvalds
2561da177e4SLinus Torvalds discard_it:
25708e3baefSEric Dumazet __SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_DISCARDS);
2581da177e4SLinus Torvalds kfree_skb(skb);
2592babf9daSHerbert Xu return 0;
2601da177e4SLinus Torvalds
2611da177e4SLinus Torvalds discard_release:
26261c9fed4SVladislav Yasevich /* Release the asoc/ep ref we took in the lookup calls. */
263dae399d7SXin Long if (transport)
264dae399d7SXin Long sctp_transport_put(transport);
2650fd9a65aSNeil Horman else
2661da177e4SLinus Torvalds sctp_endpoint_put(ep);
2671da177e4SLinus Torvalds
2681da177e4SLinus Torvalds goto discard_it;
2691da177e4SLinus Torvalds }
2701da177e4SLinus Torvalds
27161c9fed4SVladislav Yasevich /* Process the backlog queue of the socket. Every skb on
27261c9fed4SVladislav Yasevich * the backlog holds a ref on an association or endpoint.
27361c9fed4SVladislav Yasevich * We hold this ref throughout the state machine to make
27461c9fed4SVladislav Yasevich * sure that the structure we need is still around.
2751da177e4SLinus Torvalds */
sctp_backlog_rcv(struct sock * sk,struct sk_buff * skb)2761da177e4SLinus Torvalds int sctp_backlog_rcv(struct sock *sk, struct sk_buff *skb)
2771da177e4SLinus Torvalds {
27879af02c2SDavid S. Miller struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
27961c9fed4SVladislav Yasevich struct sctp_inq *inqueue = &chunk->rcvr->inqueue;
280dae399d7SXin Long struct sctp_transport *t = chunk->transport;
2817a48f923SSridhar Samudrala struct sctp_ep_common *rcvr = NULL;
28261c9fed4SVladislav Yasevich int backloged = 0;
2831da177e4SLinus Torvalds
2847a48f923SSridhar Samudrala rcvr = chunk->rcvr;
285c4d2444eSSridhar Samudrala
28661c9fed4SVladislav Yasevich /* If the rcvr is dead then the association or endpoint
28761c9fed4SVladislav Yasevich * has been deleted and we can safely drop the chunk
28861c9fed4SVladislav Yasevich * and refs that we are holding.
28961c9fed4SVladislav Yasevich */
2907a48f923SSridhar Samudrala if (rcvr->dead) {
2917a48f923SSridhar Samudrala sctp_chunk_free(chunk);
29261c9fed4SVladislav Yasevich goto done;
29361c9fed4SVladislav Yasevich }
29461c9fed4SVladislav Yasevich
29561c9fed4SVladislav Yasevich if (unlikely(rcvr->sk != sk)) {
29661c9fed4SVladislav Yasevich /* In this case, the association moved from one socket to
29761c9fed4SVladislav Yasevich * another. We are currently sitting on the backlog of the
29861c9fed4SVladislav Yasevich * old socket, so we need to move.
29961c9fed4SVladislav Yasevich * However, since we are here in the process context we
30061c9fed4SVladislav Yasevich * need to take make sure that the user doesn't own
30161c9fed4SVladislav Yasevich * the new socket when we process the packet.
30261c9fed4SVladislav Yasevich * If the new socket is user-owned, queue the chunk to the
30361c9fed4SVladislav Yasevich * backlog of the new socket without dropping any refs.
30461c9fed4SVladislav Yasevich * Otherwise, we can safely push the chunk on the inqueue.
30561c9fed4SVladislav Yasevich */
30661c9fed4SVladislav Yasevich
30761c9fed4SVladislav Yasevich sk = rcvr->sk;
308eefc1b1dSMarcelo Ricardo Leitner local_bh_disable();
3095bc1d1b4Swangweidong bh_lock_sock(sk);
31061c9fed4SVladislav Yasevich
311819be810SXin Long if (sock_owned_by_user(sk) || !sctp_newsk_ready(sk)) {
3128265792bSEric Dumazet if (sk_add_backlog(sk, skb, READ_ONCE(sk->sk_rcvbuf)))
31350b1a782SZhu Yi sctp_chunk_free(chunk);
31450b1a782SZhu Yi else
31561c9fed4SVladislav Yasevich backloged = 1;
31661c9fed4SVladislav Yasevich } else
31761c9fed4SVladislav Yasevich sctp_inq_push(inqueue, chunk);
31861c9fed4SVladislav Yasevich
3195bc1d1b4Swangweidong bh_unlock_sock(sk);
320eefc1b1dSMarcelo Ricardo Leitner local_bh_enable();
32161c9fed4SVladislav Yasevich
32261c9fed4SVladislav Yasevich /* If the chunk was backloged again, don't drop refs */
32361c9fed4SVladislav Yasevich if (backloged)
32461c9fed4SVladislav Yasevich return 0;
3257a48f923SSridhar Samudrala } else {
326819be810SXin Long if (!sctp_newsk_ready(sk)) {
3278265792bSEric Dumazet if (!sk_add_backlog(sk, skb, READ_ONCE(sk->sk_rcvbuf)))
328819be810SXin Long return 0;
329819be810SXin Long sctp_chunk_free(chunk);
330819be810SXin Long } else {
3311da177e4SLinus Torvalds sctp_inq_push(inqueue, chunk);
3327a48f923SSridhar Samudrala }
333819be810SXin Long }
3347a48f923SSridhar Samudrala
33561c9fed4SVladislav Yasevich done:
33661c9fed4SVladislav Yasevich /* Release the refs we took in sctp_add_backlog */
3377a48f923SSridhar Samudrala if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
338dae399d7SXin Long sctp_transport_put(t);
33961c9fed4SVladislav Yasevich else if (SCTP_EP_TYPE_SOCKET == rcvr->type)
3407a48f923SSridhar Samudrala sctp_endpoint_put(sctp_ep(rcvr));
34161c9fed4SVladislav Yasevich else
34261c9fed4SVladislav Yasevich BUG();
3437a48f923SSridhar Samudrala
3441da177e4SLinus Torvalds return 0;
3451da177e4SLinus Torvalds }
3461da177e4SLinus Torvalds
sctp_add_backlog(struct sock * sk,struct sk_buff * skb)34750b1a782SZhu Yi static int sctp_add_backlog(struct sock *sk, struct sk_buff *skb)
348c4d2444eSSridhar Samudrala {
34961c9fed4SVladislav Yasevich struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
350dae399d7SXin Long struct sctp_transport *t = chunk->transport;
35161c9fed4SVladislav Yasevich struct sctp_ep_common *rcvr = chunk->rcvr;
35250b1a782SZhu Yi int ret;
353c4d2444eSSridhar Samudrala
3548265792bSEric Dumazet ret = sk_add_backlog(sk, skb, READ_ONCE(sk->sk_rcvbuf));
35550b1a782SZhu Yi if (!ret) {
35661c9fed4SVladislav Yasevich /* Hold the assoc/ep while hanging on the backlog queue.
35750b1a782SZhu Yi * This way, we know structures we need will not disappear
35850b1a782SZhu Yi * from us
35961c9fed4SVladislav Yasevich */
36061c9fed4SVladislav Yasevich if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
361dae399d7SXin Long sctp_transport_hold(t);
36261c9fed4SVladislav Yasevich else if (SCTP_EP_TYPE_SOCKET == rcvr->type)
36361c9fed4SVladislav Yasevich sctp_endpoint_hold(sctp_ep(rcvr));
364c4d2444eSSridhar Samudrala else
36561c9fed4SVladislav Yasevich BUG();
36650b1a782SZhu Yi }
36750b1a782SZhu Yi return ret;
36861c9fed4SVladislav Yasevich
369c4d2444eSSridhar Samudrala }
370c4d2444eSSridhar Samudrala
3711da177e4SLinus Torvalds /* Handle icmp frag needed error. */
sctp_icmp_frag_needed(struct sock * sk,struct sctp_association * asoc,struct sctp_transport * t,__u32 pmtu)3721da177e4SLinus Torvalds void sctp_icmp_frag_needed(struct sock *sk, struct sctp_association *asoc,
3731da177e4SLinus Torvalds struct sctp_transport *t, __u32 pmtu)
3741da177e4SLinus Torvalds {
37583696408SXin Long if (!t ||
37683696408SXin Long (t->pathmtu <= pmtu &&
37783696408SXin Long t->pl.probe_size + sctp_transport_pl_hlen(t) <= pmtu))
37852ccb8e9SFrank Filz return;
37952ccb8e9SFrank Filz
3808a479491SVlad Yasevich if (sock_owned_by_user(sk)) {
381d805397cSXin Long atomic_set(&t->mtu_info, pmtu);
3828a479491SVlad Yasevich asoc->pmtu_pending = 1;
3838a479491SVlad Yasevich t->pmtu_pending = 1;
3848a479491SVlad Yasevich return;
3858a479491SVlad Yasevich }
3868a479491SVlad Yasevich
387cc35c3d1SMarcelo Ricardo Leitner if (!(t->param_flags & SPP_PMTUD_ENABLE))
388cc35c3d1SMarcelo Ricardo Leitner /* We can't allow retransmitting in such case, as the
389cc35c3d1SMarcelo Ricardo Leitner * retransmission would be sized just as before, and thus we
390cc35c3d1SMarcelo Ricardo Leitner * would get another icmp, and retransmit again.
391cc35c3d1SMarcelo Ricardo Leitner */
392cc35c3d1SMarcelo Ricardo Leitner return;
393cc35c3d1SMarcelo Ricardo Leitner
394b6c5734dSMarcelo Ricardo Leitner /* Update transports view of the MTU. Return if no update was needed.
395b6c5734dSMarcelo Ricardo Leitner * If an update wasn't needed/possible, it also doesn't make sense to
396b6c5734dSMarcelo Ricardo Leitner * try to retransmit now.
397b6c5734dSMarcelo Ricardo Leitner */
398b6c5734dSMarcelo Ricardo Leitner if (!sctp_transport_update_pmtu(t, pmtu))
399b6c5734dSMarcelo Ricardo Leitner return;
4001da177e4SLinus Torvalds
40152ccb8e9SFrank Filz /* Update association pmtu. */
4023ebfdf08SXin Long sctp_assoc_sync_pmtu(asoc);
40352ccb8e9SFrank Filz
404cc35c3d1SMarcelo Ricardo Leitner /* Retransmit with the new pmtu setting. */
40552ccb8e9SFrank Filz sctp_retransmit(&asoc->outqueue, t, SCTP_RTXR_PMTUD);
4061da177e4SLinus Torvalds }
4071da177e4SLinus Torvalds
sctp_icmp_redirect(struct sock * sk,struct sctp_transport * t,struct sk_buff * skb)408ec18d9a2SDavid S. Miller void sctp_icmp_redirect(struct sock *sk, struct sctp_transport *t,
40955be7a9cSDavid S. Miller struct sk_buff *skb)
41055be7a9cSDavid S. Miller {
41155be7a9cSDavid S. Miller struct dst_entry *dst;
41255be7a9cSDavid S. Miller
4131cc276ceSXin Long if (sock_owned_by_user(sk) || !t)
41455be7a9cSDavid S. Miller return;
41555be7a9cSDavid S. Miller dst = sctp_transport_dst_check(t);
4161ed5c48fSDavid S. Miller if (dst)
4176700c270SDavid S. Miller dst->ops->redirect(dst, sk, skb);
41855be7a9cSDavid S. Miller }
41955be7a9cSDavid S. Miller
4201da177e4SLinus Torvalds /*
4211da177e4SLinus Torvalds * SCTP Implementer's Guide, 2.37 ICMP handling procedures
4221da177e4SLinus Torvalds *
4231da177e4SLinus Torvalds * ICMP8) If the ICMP code is a "Unrecognized next header type encountered"
4241da177e4SLinus Torvalds * or a "Protocol Unreachable" treat this message as an abort
4251da177e4SLinus Torvalds * with the T bit set.
4261da177e4SLinus Torvalds *
4271da177e4SLinus Torvalds * This function sends an event to the state machine, which will abort the
4281da177e4SLinus Torvalds * association.
4291da177e4SLinus Torvalds *
4301da177e4SLinus Torvalds */
sctp_icmp_proto_unreachable(struct sock * sk,struct sctp_association * asoc,struct sctp_transport * t)4311da177e4SLinus Torvalds void sctp_icmp_proto_unreachable(struct sock *sk,
4321da177e4SLinus Torvalds struct sctp_association *asoc,
4331da177e4SLinus Torvalds struct sctp_transport *t)
4341da177e4SLinus Torvalds {
43550b5d6adSVlad Yasevich if (sock_owned_by_user(sk)) {
43650b5d6adSVlad Yasevich if (timer_pending(&t->proto_unreach_timer))
43750b5d6adSVlad Yasevich return;
43850b5d6adSVlad Yasevich else {
43950b5d6adSVlad Yasevich if (!mod_timer(&t->proto_unreach_timer,
44050b5d6adSVlad Yasevich jiffies + (HZ/20)))
441057a10faSXin Long sctp_transport_hold(t);
44250b5d6adSVlad Yasevich }
44350b5d6adSVlad Yasevich } else {
44455e26eb9SEric W. Biederman struct net *net = sock_net(sk);
44555e26eb9SEric W. Biederman
446bb33381dSDaniel Borkmann pr_debug("%s: unrecognized next header type "
447bb33381dSDaniel Borkmann "encountered!\n", __func__);
448bb33381dSDaniel Borkmann
44925cc4ae9SYing Xue if (del_timer(&t->proto_unreach_timer))
450057a10faSXin Long sctp_transport_put(t);
45150b5d6adSVlad Yasevich
45255e26eb9SEric W. Biederman sctp_do_sm(net, SCTP_EVENT_T_OTHER,
4531da177e4SLinus Torvalds SCTP_ST_OTHER(SCTP_EVENT_ICMP_PROTO_UNREACH),
4543f7a87d2SFrank Filz asoc->state, asoc->ep, asoc, t,
4551da177e4SLinus Torvalds GFP_ATOMIC);
45650b5d6adSVlad Yasevich }
4571da177e4SLinus Torvalds }
4581da177e4SLinus Torvalds
4591da177e4SLinus Torvalds /* Common lookup code for icmp/icmpv6 error handler. */
sctp_err_lookup(struct net * net,int family,struct sk_buff * skb,struct sctphdr * sctphdr,struct sctp_association ** app,struct sctp_transport ** tpp)4604110cc25SEric W. Biederman struct sock *sctp_err_lookup(struct net *net, int family, struct sk_buff *skb,
4611da177e4SLinus Torvalds struct sctphdr *sctphdr,
4621da177e4SLinus Torvalds struct sctp_association **app,
4631da177e4SLinus Torvalds struct sctp_transport **tpp)
4641da177e4SLinus Torvalds {
465804ec7ebSDavide Caratti struct sctp_init_chunk *chunkhdr, _chunkhdr;
4661da177e4SLinus Torvalds union sctp_addr saddr;
4671da177e4SLinus Torvalds union sctp_addr daddr;
4681da177e4SLinus Torvalds struct sctp_af *af;
4691da177e4SLinus Torvalds struct sock *sk = NULL;
4708de8c873SSridhar Samudrala struct sctp_association *asoc;
4711da177e4SLinus Torvalds struct sctp_transport *transport = NULL;
4727115e632SWei Yongjun __u32 vtag = ntohl(sctphdr->vtag);
4730af03170SXin Long int sdif = inet_sdif(skb);
4740af03170SXin Long int dif = inet_iif(skb);
4751da177e4SLinus Torvalds
476d1ad1ff2SSridhar Samudrala *app = NULL; *tpp = NULL;
4771da177e4SLinus Torvalds
4781da177e4SLinus Torvalds af = sctp_get_af_specific(family);
4791da177e4SLinus Torvalds if (unlikely(!af)) {
4801da177e4SLinus Torvalds return NULL;
4811da177e4SLinus Torvalds }
4821da177e4SLinus Torvalds
4831da177e4SLinus Torvalds /* Initialize local addresses for lookups. */
4841da177e4SLinus Torvalds af->from_skb(&saddr, skb, 1);
4851da177e4SLinus Torvalds af->from_skb(&daddr, skb, 0);
4861da177e4SLinus Torvalds
4871da177e4SLinus Torvalds /* Look for an association that matches the incoming ICMP error
4881da177e4SLinus Torvalds * packet.
4891da177e4SLinus Torvalds */
4900af03170SXin Long asoc = __sctp_lookup_association(net, &saddr, &daddr, &transport, dif, sdif);
491d1ad1ff2SSridhar Samudrala if (!asoc)
4921da177e4SLinus Torvalds return NULL;
4931da177e4SLinus Torvalds
4941da177e4SLinus Torvalds sk = asoc->base.sk;
4951da177e4SLinus Torvalds
4967115e632SWei Yongjun /* RFC 4960, Appendix C. ICMP Handling
4977115e632SWei Yongjun *
4987115e632SWei Yongjun * ICMP6) An implementation MUST validate that the Verification Tag
4997115e632SWei Yongjun * contained in the ICMP message matches the Verification Tag of
5007115e632SWei Yongjun * the peer. If the Verification Tag is not 0 and does NOT
5017115e632SWei Yongjun * match, discard the ICMP message. If it is 0 and the ICMP
5027115e632SWei Yongjun * message contains enough bytes to verify that the chunk type is
5037115e632SWei Yongjun * an INIT chunk and that the Initiate Tag matches the tag of the
5047115e632SWei Yongjun * peer, continue with ICMP7. If the ICMP message is too short
5057115e632SWei Yongjun * or the chunk type or the Initiate Tag does not match, silently
5067115e632SWei Yongjun * discard the packet.
5077115e632SWei Yongjun */
5087115e632SWei Yongjun if (vtag == 0) {
509804ec7ebSDavide Caratti /* chunk header + first 4 octects of init header */
510804ec7ebSDavide Caratti chunkhdr = skb_header_pointer(skb, skb_transport_offset(skb) +
511804ec7ebSDavide Caratti sizeof(struct sctphdr),
512804ec7ebSDavide Caratti sizeof(struct sctp_chunkhdr) +
513804ec7ebSDavide Caratti sizeof(__be32), &_chunkhdr);
514804ec7ebSDavide Caratti if (!chunkhdr ||
5157115e632SWei Yongjun chunkhdr->chunk_hdr.type != SCTP_CID_INIT ||
516804ec7ebSDavide Caratti ntohl(chunkhdr->init_hdr.init_tag) != asoc->c.my_vtag)
5177115e632SWei Yongjun goto out;
518804ec7ebSDavide Caratti
5197115e632SWei Yongjun } else if (vtag != asoc->c.peer_vtag) {
5201da177e4SLinus Torvalds goto out;
5211da177e4SLinus Torvalds }
5221da177e4SLinus Torvalds
5235bc1d1b4Swangweidong bh_lock_sock(sk);
5241da177e4SLinus Torvalds
5251da177e4SLinus Torvalds /* If too many ICMPs get dropped on busy
5261da177e4SLinus Torvalds * servers this needs to be solved differently.
5271da177e4SLinus Torvalds */
5281da177e4SLinus Torvalds if (sock_owned_by_user(sk))
52902a1d6e7SEric Dumazet __NET_INC_STATS(net, LINUX_MIB_LOCKDROPPEDICMPS);
5301da177e4SLinus Torvalds
5311da177e4SLinus Torvalds *app = asoc;
5321da177e4SLinus Torvalds *tpp = transport;
5331da177e4SLinus Torvalds return sk;
5341da177e4SLinus Torvalds
5351da177e4SLinus Torvalds out:
536dae399d7SXin Long sctp_transport_put(transport);
5371da177e4SLinus Torvalds return NULL;
5381da177e4SLinus Torvalds }
5391da177e4SLinus Torvalds
5401da177e4SLinus Torvalds /* Common cleanup code for icmp/icmpv6 error handler. */
sctp_err_finish(struct sock * sk,struct sctp_transport * t)541dae399d7SXin Long void sctp_err_finish(struct sock *sk, struct sctp_transport *t)
542887cf3d1SJules Irenge __releases(&((__sk)->sk_lock.slock))
5431da177e4SLinus Torvalds {
5445bc1d1b4Swangweidong bh_unlock_sock(sk);
545dae399d7SXin Long sctp_transport_put(t);
5461da177e4SLinus Torvalds }
5471da177e4SLinus Torvalds
sctp_v4_err_handle(struct sctp_transport * t,struct sk_buff * skb,__u8 type,__u8 code,__u32 info)548d8306075SXin Long static void sctp_v4_err_handle(struct sctp_transport *t, struct sk_buff *skb,
549d8306075SXin Long __u8 type, __u8 code, __u32 info)
550d8306075SXin Long {
551d8306075SXin Long struct sctp_association *asoc = t->asoc;
552d8306075SXin Long struct sock *sk = asoc->base.sk;
553d8306075SXin Long int err = 0;
554d8306075SXin Long
555d8306075SXin Long switch (type) {
556d8306075SXin Long case ICMP_PARAMETERPROB:
557d8306075SXin Long err = EPROTO;
558d8306075SXin Long break;
559d8306075SXin Long case ICMP_DEST_UNREACH:
560d8306075SXin Long if (code > NR_ICMP_UNREACH)
561d8306075SXin Long return;
562d8306075SXin Long if (code == ICMP_FRAG_NEEDED) {
563d8306075SXin Long sctp_icmp_frag_needed(sk, asoc, t, SCTP_TRUNC4(info));
564d8306075SXin Long return;
565d8306075SXin Long }
566d8306075SXin Long if (code == ICMP_PROT_UNREACH) {
567d8306075SXin Long sctp_icmp_proto_unreachable(sk, asoc, t);
568d8306075SXin Long return;
569d8306075SXin Long }
570d8306075SXin Long err = icmp_err_convert[code].errno;
571d8306075SXin Long break;
572d8306075SXin Long case ICMP_TIME_EXCEEDED:
573d8306075SXin Long if (code == ICMP_EXC_FRAGTIME)
574d8306075SXin Long return;
575d8306075SXin Long
576d8306075SXin Long err = EHOSTUNREACH;
577d8306075SXin Long break;
578d8306075SXin Long case ICMP_REDIRECT:
579d8306075SXin Long sctp_icmp_redirect(sk, t, skb);
580dbe69e43SLinus Torvalds return;
581d8306075SXin Long default:
582d8306075SXin Long return;
583d8306075SXin Long }
5846b5f43eaSEric Dumazet if (!sock_owned_by_user(sk) && inet_test_bit(RECVERR, sk)) {
585d8306075SXin Long sk->sk_err = err;
586e3ae2365SAlexander Aring sk_error_report(sk);
587d8306075SXin Long } else { /* Only an error on timeout */
5882f2d9972SEric Dumazet WRITE_ONCE(sk->sk_err_soft, err);
589d8306075SXin Long }
590d8306075SXin Long }
591d8306075SXin Long
5921da177e4SLinus Torvalds /*
5931da177e4SLinus Torvalds * This routine is called by the ICMP module when it gets some
5941da177e4SLinus Torvalds * sort of error condition. If err < 0 then the socket should
5951da177e4SLinus Torvalds * be closed and the error returned to the user. If err > 0
5961da177e4SLinus Torvalds * it's just the icmp type << 8 | icmp code. After adjustment
5971da177e4SLinus Torvalds * header points to the first 8 bytes of the sctp header. We need
5981da177e4SLinus Torvalds * to find the appropriate port.
5991da177e4SLinus Torvalds *
6001da177e4SLinus Torvalds * The locking strategy used here is very "optimistic". When
6011da177e4SLinus Torvalds * someone else accesses the socket the ICMP is just dropped
6021da177e4SLinus Torvalds * and for some paths there is no check at all.
6031da177e4SLinus Torvalds * A more general error queue to queue errors for later handling
6041da177e4SLinus Torvalds * is probably better.
6051da177e4SLinus Torvalds *
6061da177e4SLinus Torvalds */
sctp_v4_err(struct sk_buff * skb,__u32 info)60732bbd879SStefano Brivio int sctp_v4_err(struct sk_buff *skb, __u32 info)
6081da177e4SLinus Torvalds {
609b71d1d42SEric Dumazet const struct iphdr *iph = (const struct iphdr *)skb->data;
61088c7664fSArnaldo Carvalho de Melo const int type = icmp_hdr(skb)->type;
61188c7664fSArnaldo Carvalho de Melo const int code = icmp_hdr(skb)->code;
6124110cc25SEric W. Biederman struct net *net = dev_net(skb->dev);
613d8306075SXin Long struct sctp_transport *transport;
614d8306075SXin Long struct sctp_association *asoc;
615d8306075SXin Long __u16 saveip, savesctp;
616d8306075SXin Long struct sock *sk;
6171da177e4SLinus Torvalds
6181da177e4SLinus Torvalds /* Fix up skb to look at the embedded net header. */
619b0e380b1SArnaldo Carvalho de Melo saveip = skb->network_header;
620b0e380b1SArnaldo Carvalho de Melo savesctp = skb->transport_header;
62131c7711bSArnaldo Carvalho de Melo skb_reset_network_header(skb);
622d8306075SXin Long skb_set_transport_header(skb, iph->ihl * 4);
6234110cc25SEric W. Biederman sk = sctp_err_lookup(net, AF_INET, skb, sctp_hdr(skb), &asoc, &transport);
6242e07fa9cSArnaldo Carvalho de Melo /* Put back, the original values. */
625b0e380b1SArnaldo Carvalho de Melo skb->network_header = saveip;
626b0e380b1SArnaldo Carvalho de Melo skb->transport_header = savesctp;
6271da177e4SLinus Torvalds if (!sk) {
6285d3848bcSEric Dumazet __ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
62932bbd879SStefano Brivio return -ENOENT;
6301da177e4SLinus Torvalds }
6311da177e4SLinus Torvalds
632d8306075SXin Long sctp_v4_err_handle(transport, skb, type, code, info);
633dae399d7SXin Long sctp_err_finish(sk, transport);
634d8306075SXin Long
63532bbd879SStefano Brivio return 0;
6361da177e4SLinus Torvalds }
6371da177e4SLinus Torvalds
sctp_udp_v4_err(struct sock * sk,struct sk_buff * skb)6389e47df00SXin Long int sctp_udp_v4_err(struct sock *sk, struct sk_buff *skb)
6399e47df00SXin Long {
6409e47df00SXin Long struct net *net = dev_net(skb->dev);
6419e47df00SXin Long struct sctp_association *asoc;
6429e47df00SXin Long struct sctp_transport *t;
6439e47df00SXin Long struct icmphdr *hdr;
6449e47df00SXin Long __u32 info = 0;
6459e47df00SXin Long
6469e47df00SXin Long skb->transport_header += sizeof(struct udphdr);
6479e47df00SXin Long sk = sctp_err_lookup(net, AF_INET, skb, sctp_hdr(skb), &asoc, &t);
6489e47df00SXin Long if (!sk) {
6499e47df00SXin Long __ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
6509e47df00SXin Long return -ENOENT;
6519e47df00SXin Long }
6529e47df00SXin Long
6539e47df00SXin Long skb->transport_header -= sizeof(struct udphdr);
6549e47df00SXin Long hdr = (struct icmphdr *)(skb_network_header(skb) - sizeof(struct icmphdr));
6559e47df00SXin Long if (hdr->type == ICMP_REDIRECT) {
6569e47df00SXin Long /* can't be handled without outer iphdr known, leave it to udp_err */
6579e47df00SXin Long sctp_err_finish(sk, t);
6589e47df00SXin Long return 0;
6599e47df00SXin Long }
6609e47df00SXin Long if (hdr->type == ICMP_DEST_UNREACH && hdr->code == ICMP_FRAG_NEEDED)
6619e47df00SXin Long info = ntohs(hdr->un.frag.mtu);
6629e47df00SXin Long sctp_v4_err_handle(t, skb, hdr->type, hdr->code, info);
6639e47df00SXin Long
6649e47df00SXin Long sctp_err_finish(sk, t);
6659e47df00SXin Long return 1;
6669e47df00SXin Long }
6679e47df00SXin Long
6681da177e4SLinus Torvalds /*
6691da177e4SLinus Torvalds * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
6701da177e4SLinus Torvalds *
6711da177e4SLinus Torvalds * This function scans all the chunks in the OOTB packet to determine if
6721da177e4SLinus Torvalds * the packet should be discarded right away. If a response might be needed
6731da177e4SLinus Torvalds * for this packet, or, if further processing is possible, the packet will
6741da177e4SLinus Torvalds * be queued to a proper inqueue for the next phase of handling.
6751da177e4SLinus Torvalds *
6761da177e4SLinus Torvalds * Output:
6771da177e4SLinus Torvalds * Return 0 - If further processing is needed.
6781da177e4SLinus Torvalds * Return 1 - If the packet can be discarded right away.
6791da177e4SLinus Torvalds */
sctp_rcv_ootb(struct sk_buff * skb)68004675210Ssebastian@breakpoint.cc static int sctp_rcv_ootb(struct sk_buff *skb)
6811da177e4SLinus Torvalds {
682922dbc5bSXin Long struct sctp_chunkhdr *ch, _ch;
6833acb50c1SMarcelo Ricardo Leitner int ch_end, offset = 0;
6841da177e4SLinus Torvalds
6851da177e4SLinus Torvalds /* Scan through all the chunks in the packet. */
686a7d1f1b6STsutomu Fujii do {
6873acb50c1SMarcelo Ricardo Leitner /* Make sure we have at least the header there */
688922dbc5bSXin Long if (offset + sizeof(_ch) > skb->len)
6893acb50c1SMarcelo Ricardo Leitner break;
6903acb50c1SMarcelo Ricardo Leitner
6913acb50c1SMarcelo Ricardo Leitner ch = skb_header_pointer(skb, offset, sizeof(*ch), &_ch);
6923acb50c1SMarcelo Ricardo Leitner
693a7d1f1b6STsutomu Fujii /* Break out if chunk length is less then minimal. */
694f7e745f8SXin Long if (!ch || ntohs(ch->length) < sizeof(_ch))
695a7d1f1b6STsutomu Fujii break;
696a7d1f1b6STsutomu Fujii
697e2f036a9SMarcelo Ricardo Leitner ch_end = offset + SCTP_PAD4(ntohs(ch->length));
6983acb50c1SMarcelo Ricardo Leitner if (ch_end > skb->len)
699a7d1f1b6STsutomu Fujii break;
7001da177e4SLinus Torvalds
7011da177e4SLinus Torvalds /* RFC 8.4, 2) If the OOTB packet contains an ABORT chunk, the
7021da177e4SLinus Torvalds * receiver MUST silently discard the OOTB packet and take no
7031da177e4SLinus Torvalds * further action.
7041da177e4SLinus Torvalds */
7051da177e4SLinus Torvalds if (SCTP_CID_ABORT == ch->type)
7061da177e4SLinus Torvalds goto discard;
7071da177e4SLinus Torvalds
7081da177e4SLinus Torvalds /* RFC 8.4, 6) If the packet contains a SHUTDOWN COMPLETE
7091da177e4SLinus Torvalds * chunk, the receiver should silently discard the packet
7101da177e4SLinus Torvalds * and take no further action.
7111da177e4SLinus Torvalds */
7121da177e4SLinus Torvalds if (SCTP_CID_SHUTDOWN_COMPLETE == ch->type)
7131da177e4SLinus Torvalds goto discard;
7141da177e4SLinus Torvalds
7153c77f961SVlad Yasevich /* RFC 4460, 2.11.2
7163c77f961SVlad Yasevich * This will discard packets with INIT chunk bundled as
7173c77f961SVlad Yasevich * subsequent chunks in the packet. When INIT is first,
7183c77f961SVlad Yasevich * the normal INIT processing will discard the chunk.
7193c77f961SVlad Yasevich */
7203c77f961SVlad Yasevich if (SCTP_CID_INIT == ch->type && (void *)ch != skb->data)
7213c77f961SVlad Yasevich goto discard;
7223c77f961SVlad Yasevich
7233acb50c1SMarcelo Ricardo Leitner offset = ch_end;
7243acb50c1SMarcelo Ricardo Leitner } while (ch_end < skb->len);
7251da177e4SLinus Torvalds
7261da177e4SLinus Torvalds return 0;
7271da177e4SLinus Torvalds
7281da177e4SLinus Torvalds discard:
7291da177e4SLinus Torvalds return 1;
7301da177e4SLinus Torvalds }
7311da177e4SLinus Torvalds
7321da177e4SLinus Torvalds /* Insert endpoint into the hash table. */
__sctp_hash_endpoint(struct sctp_endpoint * ep)73376c6d988SXin Long static int __sctp_hash_endpoint(struct sctp_endpoint *ep)
7341da177e4SLinus Torvalds {
73576c6d988SXin Long struct sock *sk = ep->base.sk;
73676c6d988SXin Long struct net *net = sock_net(sk);
7371da177e4SLinus Torvalds struct sctp_hashbucket *head;
738*1407be30SKuniyuki Iwashima int err = 0;
7391da177e4SLinus Torvalds
7403d3b2f57SXin Long ep->hashent = sctp_ep_hashfn(net, ep->base.bind_addr.port);
7413d3b2f57SXin Long head = &sctp_ep_hashtable[ep->hashent];
7421da177e4SLinus Torvalds
743*1407be30SKuniyuki Iwashima write_lock(&head->lock);
74476c6d988SXin Long if (sk->sk_reuseport) {
74576c6d988SXin Long bool any = sctp_is_ep_boundall(sk);
7463d3b2f57SXin Long struct sctp_endpoint *ep2;
74776c6d988SXin Long struct list_head *list;
748*1407be30SKuniyuki Iwashima int cnt = 0;
749*1407be30SKuniyuki Iwashima
750*1407be30SKuniyuki Iwashima err = 1;
75176c6d988SXin Long
75276c6d988SXin Long list_for_each(list, &ep->base.bind_addr.address_list)
75376c6d988SXin Long cnt++;
75476c6d988SXin Long
7553d3b2f57SXin Long sctp_for_each_hentry(ep2, &head->chain) {
7563d3b2f57SXin Long struct sock *sk2 = ep2->base.sk;
75776c6d988SXin Long
75876c6d988SXin Long if (!net_eq(sock_net(sk2), net) || sk2 == sk ||
75976c6d988SXin Long !uid_eq(sock_i_uid(sk2), sock_i_uid(sk)) ||
76076c6d988SXin Long !sk2->sk_reuseport)
76176c6d988SXin Long continue;
76276c6d988SXin Long
76376c6d988SXin Long err = sctp_bind_addrs_check(sctp_sk(sk2),
76476c6d988SXin Long sctp_sk(sk), cnt);
76576c6d988SXin Long if (!err) {
76676c6d988SXin Long err = reuseport_add_sock(sk, sk2, any);
76776c6d988SXin Long if (err)
768*1407be30SKuniyuki Iwashima goto out;
76976c6d988SXin Long break;
77076c6d988SXin Long } else if (err < 0) {
771*1407be30SKuniyuki Iwashima goto out;
77276c6d988SXin Long }
77376c6d988SXin Long }
77476c6d988SXin Long
77576c6d988SXin Long if (err) {
77676c6d988SXin Long err = reuseport_alloc(sk, any);
77776c6d988SXin Long if (err)
778*1407be30SKuniyuki Iwashima goto out;
77976c6d988SXin Long }
78076c6d988SXin Long }
78176c6d988SXin Long
7823d3b2f57SXin Long hlist_add_head(&ep->node, &head->chain);
783*1407be30SKuniyuki Iwashima out:
784387602dfSwangweidong write_unlock(&head->lock);
785*1407be30SKuniyuki Iwashima return err;
7861da177e4SLinus Torvalds }
7871da177e4SLinus Torvalds
7881da177e4SLinus Torvalds /* Add an endpoint to the hash. Local BH-safe. */
sctp_hash_endpoint(struct sctp_endpoint * ep)78976c6d988SXin Long int sctp_hash_endpoint(struct sctp_endpoint *ep)
7901da177e4SLinus Torvalds {
79176c6d988SXin Long int err;
79276c6d988SXin Long
79379b91130Swangweidong local_bh_disable();
79476c6d988SXin Long err = __sctp_hash_endpoint(ep);
79579b91130Swangweidong local_bh_enable();
79676c6d988SXin Long
79776c6d988SXin Long return err;
7981da177e4SLinus Torvalds }
7991da177e4SLinus Torvalds
8001da177e4SLinus Torvalds /* Remove endpoint from the hash table. */
__sctp_unhash_endpoint(struct sctp_endpoint * ep)8011da177e4SLinus Torvalds static void __sctp_unhash_endpoint(struct sctp_endpoint *ep)
8021da177e4SLinus Torvalds {
80376c6d988SXin Long struct sock *sk = ep->base.sk;
8041da177e4SLinus Torvalds struct sctp_hashbucket *head;
8051da177e4SLinus Torvalds
8063d3b2f57SXin Long ep->hashent = sctp_ep_hashfn(sock_net(sk), ep->base.bind_addr.port);
8071da177e4SLinus Torvalds
8083d3b2f57SXin Long head = &sctp_ep_hashtable[ep->hashent];
8091da177e4SLinus Torvalds
810*1407be30SKuniyuki Iwashima write_lock(&head->lock);
81176c6d988SXin Long if (rcu_access_pointer(sk->sk_reuseport_cb))
81276c6d988SXin Long reuseport_detach_sock(sk);
8133d3b2f57SXin Long hlist_del_init(&ep->node);
814387602dfSwangweidong write_unlock(&head->lock);
8151da177e4SLinus Torvalds }
8161da177e4SLinus Torvalds
8171da177e4SLinus Torvalds /* Remove endpoint from the hash. Local BH-safe. */
sctp_unhash_endpoint(struct sctp_endpoint * ep)8181da177e4SLinus Torvalds void sctp_unhash_endpoint(struct sctp_endpoint *ep)
8191da177e4SLinus Torvalds {
82079b91130Swangweidong local_bh_disable();
8211da177e4SLinus Torvalds __sctp_unhash_endpoint(ep);
82279b91130Swangweidong local_bh_enable();
8231da177e4SLinus Torvalds }
8241da177e4SLinus Torvalds
sctp_hashfn(const struct net * net,__be16 lport,const union sctp_addr * paddr,__u32 seed)825532ae2f1SXin Long static inline __u32 sctp_hashfn(const struct net *net, __be16 lport,
826532ae2f1SXin Long const union sctp_addr *paddr, __u32 seed)
827532ae2f1SXin Long {
828532ae2f1SXin Long __u32 addr;
829532ae2f1SXin Long
830532ae2f1SXin Long if (paddr->sa.sa_family == AF_INET6)
831532ae2f1SXin Long addr = jhash(&paddr->v6.sin6_addr, 16, seed);
832532ae2f1SXin Long else
833532ae2f1SXin Long addr = (__force __u32)paddr->v4.sin_addr.s_addr;
834532ae2f1SXin Long
835532ae2f1SXin Long return jhash_3words(addr, ((__force __u32)paddr->v4.sin_port) << 16 |
836532ae2f1SXin Long (__force __u32)lport, net_hash_mix(net), seed);
837532ae2f1SXin Long }
838532ae2f1SXin Long
8391da177e4SLinus Torvalds /* Look up an endpoint. */
__sctp_rcv_lookup_endpoint(struct net * net,struct sk_buff * skb,const union sctp_addr * laddr,const union sctp_addr * paddr,int dif,int sdif)840532ae2f1SXin Long static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(
841532ae2f1SXin Long struct net *net, struct sk_buff *skb,
842532ae2f1SXin Long const union sctp_addr *laddr,
8430af03170SXin Long const union sctp_addr *paddr,
8440af03170SXin Long int dif, int sdif)
8451da177e4SLinus Torvalds {
8461da177e4SLinus Torvalds struct sctp_hashbucket *head;
8471da177e4SLinus Torvalds struct sctp_endpoint *ep;
848532ae2f1SXin Long struct sock *sk;
849532ae2f1SXin Long __be16 lport;
8501da177e4SLinus Torvalds int hash;
8511da177e4SLinus Torvalds
852532ae2f1SXin Long lport = laddr->v4.sin_port;
853532ae2f1SXin Long hash = sctp_ep_hashfn(net, ntohs(lport));
8541da177e4SLinus Torvalds head = &sctp_ep_hashtable[hash];
8551da177e4SLinus Torvalds read_lock(&head->lock);
8563d3b2f57SXin Long sctp_for_each_hentry(ep, &head->chain) {
8570af03170SXin Long if (sctp_endpoint_is_match(ep, net, laddr, dif, sdif))
8581da177e4SLinus Torvalds goto hit;
8591da177e4SLinus Torvalds }
8601da177e4SLinus Torvalds
8612ce95503SEric W. Biederman ep = sctp_sk(net->sctp.ctl_sock)->ep;
8621da177e4SLinus Torvalds
8631da177e4SLinus Torvalds hit:
864532ae2f1SXin Long sk = ep->base.sk;
865532ae2f1SXin Long if (sk->sk_reuseport) {
866532ae2f1SXin Long __u32 phash = sctp_hashfn(net, lport, paddr, 0);
867532ae2f1SXin Long
868532ae2f1SXin Long sk = reuseport_select_sock(sk, phash, skb,
869532ae2f1SXin Long sizeof(struct sctphdr));
870532ae2f1SXin Long if (sk)
871532ae2f1SXin Long ep = sctp_sk(sk)->ep;
872532ae2f1SXin Long }
8731da177e4SLinus Torvalds sctp_endpoint_hold(ep);
8741da177e4SLinus Torvalds read_unlock(&head->lock);
8751da177e4SLinus Torvalds return ep;
8761da177e4SLinus Torvalds }
8771da177e4SLinus Torvalds
878d6c0256aSXin Long /* rhashtable for transport */
879d6c0256aSXin Long struct sctp_hash_cmp_arg {
880d6c0256aSXin Long const union sctp_addr *paddr;
881d6c0256aSXin Long const struct net *net;
8828d32503eSXin Long __be16 lport;
883d6c0256aSXin Long };
884d6c0256aSXin Long
sctp_hash_cmp(struct rhashtable_compare_arg * arg,const void * ptr)885d6c0256aSXin Long static inline int sctp_hash_cmp(struct rhashtable_compare_arg *arg,
886d6c0256aSXin Long const void *ptr)
887d6c0256aSXin Long {
888715f5552SXin Long struct sctp_transport *t = (struct sctp_transport *)ptr;
889d6c0256aSXin Long const struct sctp_hash_cmp_arg *x = arg->key;
890715f5552SXin Long int err = 1;
891d6c0256aSXin Long
892d6c0256aSXin Long if (!sctp_cmp_addr_exact(&t->ipaddr, x->paddr))
893715f5552SXin Long return err;
894715f5552SXin Long if (!sctp_transport_hold(t))
895715f5552SXin Long return err;
896715f5552SXin Long
89731243461SXin Long if (!net_eq(t->asoc->base.net, x->net))
898715f5552SXin Long goto out;
8997fda702fSXin Long if (x->lport != htons(t->asoc->base.bind_addr.port))
900715f5552SXin Long goto out;
901d6c0256aSXin Long
902715f5552SXin Long err = 0;
903715f5552SXin Long out:
904715f5552SXin Long sctp_transport_put(t);
905715f5552SXin Long return err;
906d6c0256aSXin Long }
907d6c0256aSXin Long
sctp_hash_obj(const void * data,u32 len,u32 seed)9088d32503eSXin Long static inline __u32 sctp_hash_obj(const void *data, u32 len, u32 seed)
909d6c0256aSXin Long {
910d6c0256aSXin Long const struct sctp_transport *t = data;
911d6c0256aSXin Long
91231243461SXin Long return sctp_hashfn(t->asoc->base.net,
913532ae2f1SXin Long htons(t->asoc->base.bind_addr.port),
914532ae2f1SXin Long &t->ipaddr, seed);
915d6c0256aSXin Long }
916d6c0256aSXin Long
sctp_hash_key(const void * data,u32 len,u32 seed)9178d32503eSXin Long static inline __u32 sctp_hash_key(const void *data, u32 len, u32 seed)
918d6c0256aSXin Long {
919d6c0256aSXin Long const struct sctp_hash_cmp_arg *x = data;
920d6c0256aSXin Long
921532ae2f1SXin Long return sctp_hashfn(x->net, x->lport, x->paddr, seed);
922d6c0256aSXin Long }
923d6c0256aSXin Long
924d6c0256aSXin Long static const struct rhashtable_params sctp_hash_params = {
925d6c0256aSXin Long .head_offset = offsetof(struct sctp_transport, node),
926d6c0256aSXin Long .hashfn = sctp_hash_key,
927d6c0256aSXin Long .obj_hashfn = sctp_hash_obj,
928d6c0256aSXin Long .obj_cmpfn = sctp_hash_cmp,
929d6c0256aSXin Long .automatic_shrinking = true,
930d6c0256aSXin Long };
931d6c0256aSXin Long
sctp_transport_hashtable_init(void)932d6c0256aSXin Long int sctp_transport_hashtable_init(void)
933d6c0256aSXin Long {
9347fda702fSXin Long return rhltable_init(&sctp_transport_hashtable, &sctp_hash_params);
935d6c0256aSXin Long }
936d6c0256aSXin Long
sctp_transport_hashtable_destroy(void)937d6c0256aSXin Long void sctp_transport_hashtable_destroy(void)
938d6c0256aSXin Long {
9397fda702fSXin Long rhltable_destroy(&sctp_transport_hashtable);
940d6c0256aSXin Long }
941d6c0256aSXin Long
sctp_hash_transport(struct sctp_transport * t)9427fda702fSXin Long int sctp_hash_transport(struct sctp_transport *t)
943d6c0256aSXin Long {
944cd2b7087SXin Long struct sctp_transport *transport;
945cd2b7087SXin Long struct rhlist_head *tmp, *list;
946d6c0256aSXin Long struct sctp_hash_cmp_arg arg;
9477fda702fSXin Long int err;
948d6c0256aSXin Long
949dd7445adSXin Long if (t->asoc->temp)
9507fda702fSXin Long return 0;
951dd7445adSXin Long
9524e7696d9SXin Long arg.net = t->asoc->base.net;
9537fda702fSXin Long arg.paddr = &t->ipaddr;
9547fda702fSXin Long arg.lport = htons(t->asoc->base.bind_addr.port);
955d6c0256aSXin Long
9565179b266SXin Long rcu_read_lock();
957cd2b7087SXin Long list = rhltable_lookup(&sctp_transport_hashtable, &arg,
958cd2b7087SXin Long sctp_hash_params);
959cd2b7087SXin Long
960cd2b7087SXin Long rhl_for_each_entry_rcu(transport, tmp, list, node)
961cd2b7087SXin Long if (transport->asoc->ep == t->asoc->ep) {
9625179b266SXin Long rcu_read_unlock();
96327af86bbSXin Long return -EEXIST;
964cd2b7087SXin Long }
9655179b266SXin Long rcu_read_unlock();
966cd2b7087SXin Long
9677fda702fSXin Long err = rhltable_insert_key(&sctp_transport_hashtable, &arg,
9687fda702fSXin Long &t->node, sctp_hash_params);
9697fda702fSXin Long if (err)
9707fda702fSXin Long pr_err_once("insert transport fail, errno %d\n", err);
9717fda702fSXin Long
9727fda702fSXin Long return err;
973d6c0256aSXin Long }
974d6c0256aSXin Long
sctp_unhash_transport(struct sctp_transport * t)975d6c0256aSXin Long void sctp_unhash_transport(struct sctp_transport *t)
976d6c0256aSXin Long {
977dd7445adSXin Long if (t->asoc->temp)
978dd7445adSXin Long return;
979dd7445adSXin Long
9807fda702fSXin Long rhltable_remove(&sctp_transport_hashtable, &t->node,
981d6c0256aSXin Long sctp_hash_params);
982d6c0256aSXin Long }
983d6c0256aSXin Long
sctp_sk_bound_dev_eq(struct net * net,int bound_dev_if,int dif,int sdif)9840af03170SXin Long bool sctp_sk_bound_dev_eq(struct net *net, int bound_dev_if, int dif, int sdif)
9850af03170SXin Long {
9860af03170SXin Long bool l3mdev_accept = true;
9870af03170SXin Long
9880af03170SXin Long #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV)
9890af03170SXin Long l3mdev_accept = !!READ_ONCE(net->sctp.l3mdev_accept);
9900af03170SXin Long #endif
9910af03170SXin Long return inet_bound_dev_eq(l3mdev_accept, bound_dev_if, dif, sdif);
9920af03170SXin Long }
9930af03170SXin Long
9947fda702fSXin Long /* return a transport with holding it */
sctp_addrs_lookup_transport(struct net * net,const union sctp_addr * laddr,const union sctp_addr * paddr,int dif,int sdif)995d6c0256aSXin Long struct sctp_transport *sctp_addrs_lookup_transport(
996d6c0256aSXin Long struct net *net,
997d6c0256aSXin Long const union sctp_addr *laddr,
9980af03170SXin Long const union sctp_addr *paddr,
9990af03170SXin Long int dif, int sdif)
1000d6c0256aSXin Long {
10017fda702fSXin Long struct rhlist_head *tmp, *list;
10027fda702fSXin Long struct sctp_transport *t;
10030af03170SXin Long int bound_dev_if;
1004d6c0256aSXin Long struct sctp_hash_cmp_arg arg = {
1005d6c0256aSXin Long .paddr = paddr,
1006d6c0256aSXin Long .net = net,
10077fda702fSXin Long .lport = laddr->v4.sin_port,
1008d6c0256aSXin Long };
1009d6c0256aSXin Long
10107fda702fSXin Long list = rhltable_lookup(&sctp_transport_hashtable, &arg,
1011d6c0256aSXin Long sctp_hash_params);
10127fda702fSXin Long
10137fda702fSXin Long rhl_for_each_entry_rcu(t, tmp, list, node) {
10147fda702fSXin Long if (!sctp_transport_hold(t))
10157fda702fSXin Long continue;
10167fda702fSXin Long
10170af03170SXin Long bound_dev_if = READ_ONCE(t->asoc->base.sk->sk_bound_dev_if);
10180af03170SXin Long if (sctp_sk_bound_dev_eq(net, bound_dev_if, dif, sdif) &&
10190af03170SXin Long sctp_bind_addr_match(&t->asoc->base.bind_addr,
10207fda702fSXin Long laddr, sctp_sk(t->asoc->base.sk)))
10217fda702fSXin Long return t;
10227fda702fSXin Long sctp_transport_put(t);
1023d6c0256aSXin Long }
1024d6c0256aSXin Long
10257fda702fSXin Long return NULL;
10267fda702fSXin Long }
10277fda702fSXin Long
10287fda702fSXin Long /* return a transport without holding it, as it's only used under sock lock */
sctp_epaddr_lookup_transport(const struct sctp_endpoint * ep,const union sctp_addr * paddr)1029d6c0256aSXin Long struct sctp_transport *sctp_epaddr_lookup_transport(
1030d6c0256aSXin Long const struct sctp_endpoint *ep,
1031d6c0256aSXin Long const union sctp_addr *paddr)
1032d6c0256aSXin Long {
10337fda702fSXin Long struct rhlist_head *tmp, *list;
10347fda702fSXin Long struct sctp_transport *t;
103565a5124aSXin Long struct sctp_hash_cmp_arg arg = {
103665a5124aSXin Long .paddr = paddr,
10374e7696d9SXin Long .net = ep->base.net,
10387fda702fSXin Long .lport = htons(ep->base.bind_addr.port),
103965a5124aSXin Long };
1040d6c0256aSXin Long
10417fda702fSXin Long list = rhltable_lookup(&sctp_transport_hashtable, &arg,
104265a5124aSXin Long sctp_hash_params);
10437fda702fSXin Long
10447fda702fSXin Long rhl_for_each_entry_rcu(t, tmp, list, node)
10457fda702fSXin Long if (ep == t->asoc->ep)
10467fda702fSXin Long return t;
10477fda702fSXin Long
10487fda702fSXin Long return NULL;
1049d6c0256aSXin Long }
1050d6c0256aSXin Long
10511da177e4SLinus Torvalds /* Look up an association. */
__sctp_lookup_association(struct net * net,const union sctp_addr * local,const union sctp_addr * peer,struct sctp_transport ** pt,int dif,int sdif)10521da177e4SLinus Torvalds static struct sctp_association *__sctp_lookup_association(
10534110cc25SEric W. Biederman struct net *net,
10541da177e4SLinus Torvalds const union sctp_addr *local,
10551da177e4SLinus Torvalds const union sctp_addr *peer,
10560af03170SXin Long struct sctp_transport **pt,
10570af03170SXin Long int dif, int sdif)
10581da177e4SLinus Torvalds {
10594f008781SXin Long struct sctp_transport *t;
10601eed6779SXin Long struct sctp_association *asoc = NULL;
10611da177e4SLinus Torvalds
10620af03170SXin Long t = sctp_addrs_lookup_transport(net, local, peer, dif, sdif);
10637fda702fSXin Long if (!t)
10641eed6779SXin Long goto out;
10651da177e4SLinus Torvalds
10661eed6779SXin Long asoc = t->asoc;
10674f008781SXin Long *pt = t;
10684f008781SXin Long
10691eed6779SXin Long out:
10701eed6779SXin Long return asoc;
10711da177e4SLinus Torvalds }
10721da177e4SLinus Torvalds
10734f008781SXin Long /* Look up an association. protected by RCU read lock */
1074dda91928SDaniel Borkmann static
sctp_lookup_association(struct net * net,const union sctp_addr * laddr,const union sctp_addr * paddr,struct sctp_transport ** transportp,int dif,int sdif)10754110cc25SEric W. Biederman struct sctp_association *sctp_lookup_association(struct net *net,
10764110cc25SEric W. Biederman const union sctp_addr *laddr,
10771da177e4SLinus Torvalds const union sctp_addr *paddr,
10780af03170SXin Long struct sctp_transport **transportp,
10790af03170SXin Long int dif, int sdif)
10801da177e4SLinus Torvalds {
10811da177e4SLinus Torvalds struct sctp_association *asoc;
10821da177e4SLinus Torvalds
1083f46c7011SXin Long rcu_read_lock();
10840af03170SXin Long asoc = __sctp_lookup_association(net, laddr, paddr, transportp, dif, sdif);
1085f46c7011SXin Long rcu_read_unlock();
10861da177e4SLinus Torvalds
10871da177e4SLinus Torvalds return asoc;
10881da177e4SLinus Torvalds }
10891da177e4SLinus Torvalds
10901da177e4SLinus Torvalds /* Is there an association matching the given local and peer addresses? */
sctp_has_association(struct net * net,const union sctp_addr * laddr,const union sctp_addr * paddr,int dif,int sdif)109153066538SXin Long bool sctp_has_association(struct net *net,
10924110cc25SEric W. Biederman const union sctp_addr *laddr,
10930af03170SXin Long const union sctp_addr *paddr,
10940af03170SXin Long int dif, int sdif)
10951da177e4SLinus Torvalds {
10961da177e4SLinus Torvalds struct sctp_transport *transport;
10971da177e4SLinus Torvalds
10980af03170SXin Long if (sctp_lookup_association(net, laddr, paddr, &transport, dif, sdif)) {
1099dae399d7SXin Long sctp_transport_put(transport);
110053066538SXin Long return true;
11011da177e4SLinus Torvalds }
11021da177e4SLinus Torvalds
110353066538SXin Long return false;
11041da177e4SLinus Torvalds }
11051da177e4SLinus Torvalds
11061da177e4SLinus Torvalds /*
11071da177e4SLinus Torvalds * SCTP Implementors Guide, 2.18 Handling of address
11081da177e4SLinus Torvalds * parameters within the INIT or INIT-ACK.
11091da177e4SLinus Torvalds *
11101da177e4SLinus Torvalds * D) When searching for a matching TCB upon reception of an INIT
11111da177e4SLinus Torvalds * or INIT-ACK chunk the receiver SHOULD use not only the
11121da177e4SLinus Torvalds * source address of the packet (containing the INIT or
11131da177e4SLinus Torvalds * INIT-ACK) but the receiver SHOULD also use all valid
11141da177e4SLinus Torvalds * address parameters contained within the chunk.
11151da177e4SLinus Torvalds *
11161da177e4SLinus Torvalds * 2.18.3 Solution description
11171da177e4SLinus Torvalds *
11181da177e4SLinus Torvalds * This new text clearly specifies to an implementor the need
11191da177e4SLinus Torvalds * to look within the INIT or INIT-ACK. Any implementation that
11201da177e4SLinus Torvalds * does not do this, may not be able to establish associations
11211da177e4SLinus Torvalds * in certain circumstances.
11221da177e4SLinus Torvalds *
11231da177e4SLinus Torvalds */
__sctp_rcv_init_lookup(struct net * net,struct sk_buff * skb,const union sctp_addr * laddr,struct sctp_transport ** transportp,int dif,int sdif)11244110cc25SEric W. Biederman static struct sctp_association *__sctp_rcv_init_lookup(struct net *net,
11254110cc25SEric W. Biederman struct sk_buff *skb,
11260af03170SXin Long const union sctp_addr *laddr, struct sctp_transport **transportp,
11270af03170SXin Long int dif, int sdif)
11281da177e4SLinus Torvalds {
11291da177e4SLinus Torvalds struct sctp_association *asoc;
11301da177e4SLinus Torvalds union sctp_addr addr;
11311da177e4SLinus Torvalds union sctp_addr *paddr = &addr;
11322c0fd387SArnaldo Carvalho de Melo struct sctphdr *sh = sctp_hdr(skb);
11331da177e4SLinus Torvalds union sctp_params params;
113401a992beSXin Long struct sctp_init_chunk *init;
11351da177e4SLinus Torvalds struct sctp_af *af;
11361da177e4SLinus Torvalds
11371da177e4SLinus Torvalds /*
11381da177e4SLinus Torvalds * This code will NOT touch anything inside the chunk--it is
11391da177e4SLinus Torvalds * strictly READ-ONLY.
11401da177e4SLinus Torvalds *
11411da177e4SLinus Torvalds * RFC 2960 3 SCTP packet Format
11421da177e4SLinus Torvalds *
11431da177e4SLinus Torvalds * Multiple chunks can be bundled into one SCTP packet up to
11441da177e4SLinus Torvalds * the MTU size, except for the INIT, INIT ACK, and SHUTDOWN
11451da177e4SLinus Torvalds * COMPLETE chunks. These chunks MUST NOT be bundled with any
11461da177e4SLinus Torvalds * other chunk in a packet. See Section 6.10 for more details
11471da177e4SLinus Torvalds * on chunk bundling.
11481da177e4SLinus Torvalds */
11491da177e4SLinus Torvalds
11501da177e4SLinus Torvalds /* Find the start of the TLVs and the end of the chunk. This is
11511da177e4SLinus Torvalds * the region we search for address parameters.
11521da177e4SLinus Torvalds */
115301a992beSXin Long init = (struct sctp_init_chunk *)skb->data;
11541da177e4SLinus Torvalds
11551da177e4SLinus Torvalds /* Walk the parameters looking for embedded addresses. */
1156add7370aSXin Long sctp_walk_params(params, init) {
11571da177e4SLinus Torvalds
11581da177e4SLinus Torvalds /* Note: Ignoring hostname addresses. */
11591da177e4SLinus Torvalds af = sctp_get_af_specific(param_type2af(params.p->type));
11601da177e4SLinus Torvalds if (!af)
11611da177e4SLinus Torvalds continue;
11621da177e4SLinus Torvalds
11630c5dc070SMarcelo Ricardo Leitner if (!af->from_addr_param(paddr, params.addr, sh->source, 0))
11640c5dc070SMarcelo Ricardo Leitner continue;
11651da177e4SLinus Torvalds
11660af03170SXin Long asoc = __sctp_lookup_association(net, laddr, paddr, transportp, dif, sdif);
11671da177e4SLinus Torvalds if (asoc)
11681da177e4SLinus Torvalds return asoc;
11691da177e4SLinus Torvalds }
11701da177e4SLinus Torvalds
11711da177e4SLinus Torvalds return NULL;
11721da177e4SLinus Torvalds }
11731da177e4SLinus Torvalds
1174df218577SVlad Yasevich /* ADD-IP, Section 5.2
1175df218577SVlad Yasevich * When an endpoint receives an ASCONF Chunk from the remote peer
1176df218577SVlad Yasevich * special procedures may be needed to identify the association the
1177df218577SVlad Yasevich * ASCONF Chunk is associated with. To properly find the association
1178df218577SVlad Yasevich * the following procedures SHOULD be followed:
1179df218577SVlad Yasevich *
1180df218577SVlad Yasevich * D2) If the association is not found, use the address found in the
1181df218577SVlad Yasevich * Address Parameter TLV combined with the port number found in the
1182df218577SVlad Yasevich * SCTP common header. If found proceed to rule D4.
1183df218577SVlad Yasevich *
1184df218577SVlad Yasevich * D2-ext) If more than one ASCONF Chunks are packed together, use the
1185df218577SVlad Yasevich * address found in the ASCONF Address Parameter TLV of each of the
1186df218577SVlad Yasevich * subsequent ASCONF Chunks. If found, proceed to rule D4.
1187df218577SVlad Yasevich */
__sctp_rcv_asconf_lookup(struct net * net,struct sctp_chunkhdr * ch,const union sctp_addr * laddr,__be16 peer_port,struct sctp_transport ** transportp,int dif,int sdif)1188df218577SVlad Yasevich static struct sctp_association *__sctp_rcv_asconf_lookup(
11894110cc25SEric W. Biederman struct net *net,
1190922dbc5bSXin Long struct sctp_chunkhdr *ch,
1191df218577SVlad Yasevich const union sctp_addr *laddr,
1192bc92dd19SAl Viro __be16 peer_port,
11930af03170SXin Long struct sctp_transport **transportp,
11940af03170SXin Long int dif, int sdif)
1195df218577SVlad Yasevich {
119668d75469SXin Long struct sctp_addip_chunk *asconf = (struct sctp_addip_chunk *)ch;
1197df218577SVlad Yasevich struct sctp_af *af;
1198df218577SVlad Yasevich union sctp_addr_param *param;
1199df218577SVlad Yasevich union sctp_addr paddr;
1200df218577SVlad Yasevich
1201b6ffe767SMarcelo Ricardo Leitner if (ntohs(ch->length) < sizeof(*asconf) + sizeof(struct sctp_paramhdr))
1202b6ffe767SMarcelo Ricardo Leitner return NULL;
1203b6ffe767SMarcelo Ricardo Leitner
1204df218577SVlad Yasevich /* Skip over the ADDIP header and find the Address parameter */
1205df218577SVlad Yasevich param = (union sctp_addr_param *)(asconf + 1);
1206df218577SVlad Yasevich
12076a435732SShan Wei af = sctp_get_af_specific(param_type2af(param->p.type));
1208df218577SVlad Yasevich if (unlikely(!af))
1209df218577SVlad Yasevich return NULL;
1210df218577SVlad Yasevich
1211557fb586SMarcelo Ricardo Leitner if (!af->from_addr_param(&paddr, param, peer_port, 0))
12120c5dc070SMarcelo Ricardo Leitner return NULL;
1213df218577SVlad Yasevich
12140af03170SXin Long return __sctp_lookup_association(net, laddr, &paddr, transportp, dif, sdif);
1215df218577SVlad Yasevich }
1216df218577SVlad Yasevich
1217df218577SVlad Yasevich
1218bbd0d598SVlad Yasevich /* SCTP-AUTH, Section 6.3:
1219bbd0d598SVlad Yasevich * If the receiver does not find a STCB for a packet containing an AUTH
1220bbd0d598SVlad Yasevich * chunk as the first chunk and not a COOKIE-ECHO chunk as the second
1221bbd0d598SVlad Yasevich * chunk, it MUST use the chunks after the AUTH chunk to look up an existing
1222bbd0d598SVlad Yasevich * association.
1223bbd0d598SVlad Yasevich *
1224bbd0d598SVlad Yasevich * This means that any chunks that can help us identify the association need
122525985edcSLucas De Marchi * to be looked at to find this association.
1226bbd0d598SVlad Yasevich */
__sctp_rcv_walk_lookup(struct net * net,struct sk_buff * skb,const union sctp_addr * laddr,struct sctp_transport ** transportp,int dif,int sdif)12274110cc25SEric W. Biederman static struct sctp_association *__sctp_rcv_walk_lookup(struct net *net,
12284110cc25SEric W. Biederman struct sk_buff *skb,
1229bbd0d598SVlad Yasevich const union sctp_addr *laddr,
12300af03170SXin Long struct sctp_transport **transportp,
12310af03170SXin Long int dif, int sdif)
1232bbd0d598SVlad Yasevich {
1233df218577SVlad Yasevich struct sctp_association *asoc = NULL;
1234922dbc5bSXin Long struct sctp_chunkhdr *ch;
1235df218577SVlad Yasevich int have_auth = 0;
1236df218577SVlad Yasevich unsigned int chunk_num = 1;
1237df218577SVlad Yasevich __u8 *ch_end;
1238df218577SVlad Yasevich
1239df218577SVlad Yasevich /* Walk through the chunks looking for AUTH or ASCONF chunks
1240df218577SVlad Yasevich * to help us find the association.
1241bbd0d598SVlad Yasevich */
1242922dbc5bSXin Long ch = (struct sctp_chunkhdr *)skb->data;
1243df218577SVlad Yasevich do {
1244df218577SVlad Yasevich /* Break out if chunk length is less then minimal. */
1245922dbc5bSXin Long if (ntohs(ch->length) < sizeof(*ch))
1246df218577SVlad Yasevich break;
1247df218577SVlad Yasevich
1248e2f036a9SMarcelo Ricardo Leitner ch_end = ((__u8 *)ch) + SCTP_PAD4(ntohs(ch->length));
1249df218577SVlad Yasevich if (ch_end > skb_tail_pointer(skb))
1250df218577SVlad Yasevich break;
1251df218577SVlad Yasevich
1252df218577SVlad Yasevich switch (ch->type) {
1253df218577SVlad Yasevich case SCTP_CID_AUTH:
1254df218577SVlad Yasevich have_auth = chunk_num;
1255df218577SVlad Yasevich break;
1256df218577SVlad Yasevich
1257df218577SVlad Yasevich case SCTP_CID_COOKIE_ECHO:
1258df218577SVlad Yasevich /* If a packet arrives containing an AUTH chunk as
1259df218577SVlad Yasevich * a first chunk, a COOKIE-ECHO chunk as the second
1260df218577SVlad Yasevich * chunk, and possibly more chunks after them, and
1261df218577SVlad Yasevich * the receiver does not have an STCB for that
1262df218577SVlad Yasevich * packet, then authentication is based on
1263df218577SVlad Yasevich * the contents of the COOKIE- ECHO chunk.
1264df218577SVlad Yasevich */
1265df218577SVlad Yasevich if (have_auth == 1 && chunk_num == 2)
1266bbd0d598SVlad Yasevich return NULL;
1267df218577SVlad Yasevich break;
1268df218577SVlad Yasevich
1269df218577SVlad Yasevich case SCTP_CID_ASCONF:
1270e1fc3b14SEric W. Biederman if (have_auth || net->sctp.addip_noauth)
12714110cc25SEric W. Biederman asoc = __sctp_rcv_asconf_lookup(
12724110cc25SEric W. Biederman net, ch, laddr,
1273df218577SVlad Yasevich sctp_hdr(skb)->source,
12740af03170SXin Long transportp, dif, sdif);
12750572b37bSGustavo A. R. Silva break;
1276df218577SVlad Yasevich default:
1277df218577SVlad Yasevich break;
1278df218577SVlad Yasevich }
1279df218577SVlad Yasevich
1280df218577SVlad Yasevich if (asoc)
1281df218577SVlad Yasevich break;
1282df218577SVlad Yasevich
1283922dbc5bSXin Long ch = (struct sctp_chunkhdr *)ch_end;
1284df218577SVlad Yasevich chunk_num++;
128550619dbfSMarcelo Ricardo Leitner } while (ch_end + sizeof(*ch) < skb_tail_pointer(skb));
1286df218577SVlad Yasevich
1287df218577SVlad Yasevich return asoc;
1288bbd0d598SVlad Yasevich }
1289bbd0d598SVlad Yasevich
1290bbd0d598SVlad Yasevich /*
1291bbd0d598SVlad Yasevich * There are circumstances when we need to look inside the SCTP packet
1292bbd0d598SVlad Yasevich * for information to help us find the association. Examples
1293bbd0d598SVlad Yasevich * include looking inside of INIT/INIT-ACK chunks or after the AUTH
1294bbd0d598SVlad Yasevich * chunks.
1295bbd0d598SVlad Yasevich */
__sctp_rcv_lookup_harder(struct net * net,struct sk_buff * skb,const union sctp_addr * laddr,struct sctp_transport ** transportp,int dif,int sdif)12964110cc25SEric W. Biederman static struct sctp_association *__sctp_rcv_lookup_harder(struct net *net,
12974110cc25SEric W. Biederman struct sk_buff *skb,
1298bbd0d598SVlad Yasevich const union sctp_addr *laddr,
12990af03170SXin Long struct sctp_transport **transportp,
13000af03170SXin Long int dif, int sdif)
1301bbd0d598SVlad Yasevich {
1302922dbc5bSXin Long struct sctp_chunkhdr *ch;
1303bbd0d598SVlad Yasevich
130490017accSMarcelo Ricardo Leitner /* We do not allow GSO frames here as we need to linearize and
130590017accSMarcelo Ricardo Leitner * then cannot guarantee frame boundaries. This shouldn't be an
130690017accSMarcelo Ricardo Leitner * issue as packets hitting this are mostly INIT or INIT-ACK and
130790017accSMarcelo Ricardo Leitner * those cannot be on GSO-style anyway.
130890017accSMarcelo Ricardo Leitner */
13091dd27cdeSDaniel Axtens if (skb_is_gso(skb) && skb_is_gso_sctp(skb))
131090017accSMarcelo Ricardo Leitner return NULL;
131190017accSMarcelo Ricardo Leitner
1312922dbc5bSXin Long ch = (struct sctp_chunkhdr *)skb->data;
1313bbd0d598SVlad Yasevich
1314df218577SVlad Yasevich /* The code below will attempt to walk the chunk and extract
1315df218577SVlad Yasevich * parameter information. Before we do that, we need to verify
1316df218577SVlad Yasevich * that the chunk length doesn't cause overflow. Otherwise, we'll
1317df218577SVlad Yasevich * walk off the end.
1318df218577SVlad Yasevich */
1319e2f036a9SMarcelo Ricardo Leitner if (SCTP_PAD4(ntohs(ch->length)) > skb->len)
1320df218577SVlad Yasevich return NULL;
1321df218577SVlad Yasevich
1322bbd0d598SVlad Yasevich /* If this is INIT/INIT-ACK look inside the chunk too. */
1323f482f2fcSwangweidong if (ch->type == SCTP_CID_INIT || ch->type == SCTP_CID_INIT_ACK)
13240af03170SXin Long return __sctp_rcv_init_lookup(net, skb, laddr, transportp, dif, sdif);
1325bbd0d598SVlad Yasevich
13260af03170SXin Long return __sctp_rcv_walk_lookup(net, skb, laddr, transportp, dif, sdif);
1327bbd0d598SVlad Yasevich }
1328bbd0d598SVlad Yasevich
13291da177e4SLinus Torvalds /* Lookup an association for an inbound skb. */
__sctp_rcv_lookup(struct net * net,struct sk_buff * skb,const union sctp_addr * paddr,const union sctp_addr * laddr,struct sctp_transport ** transportp,int dif,int sdif)13304110cc25SEric W. Biederman static struct sctp_association *__sctp_rcv_lookup(struct net *net,
13314110cc25SEric W. Biederman struct sk_buff *skb,
13321da177e4SLinus Torvalds const union sctp_addr *paddr,
13331da177e4SLinus Torvalds const union sctp_addr *laddr,
13340af03170SXin Long struct sctp_transport **transportp,
13350af03170SXin Long int dif, int sdif)
13361da177e4SLinus Torvalds {
13371da177e4SLinus Torvalds struct sctp_association *asoc;
13381da177e4SLinus Torvalds
13390af03170SXin Long asoc = __sctp_lookup_association(net, laddr, paddr, transportp, dif, sdif);
1340b77b7565SMarcelo Ricardo Leitner if (asoc)
1341b77b7565SMarcelo Ricardo Leitner goto out;
13421da177e4SLinus Torvalds
13431da177e4SLinus Torvalds /* Further lookup for INIT/INIT-ACK packets.
13441da177e4SLinus Torvalds * SCTP Implementors Guide, 2.18 Handling of address
13451da177e4SLinus Torvalds * parameters within the INIT or INIT-ACK.
13461da177e4SLinus Torvalds */
13470af03170SXin Long asoc = __sctp_rcv_lookup_harder(net, skb, laddr, transportp, dif, sdif);
1348b77b7565SMarcelo Ricardo Leitner if (asoc)
1349b77b7565SMarcelo Ricardo Leitner goto out;
13501da177e4SLinus Torvalds
1351b77b7565SMarcelo Ricardo Leitner if (paddr->sa.sa_family == AF_INET)
1352b77b7565SMarcelo Ricardo Leitner pr_debug("sctp: asoc not found for src:%pI4:%d dst:%pI4:%d\n",
1353b77b7565SMarcelo Ricardo Leitner &laddr->v4.sin_addr, ntohs(laddr->v4.sin_port),
1354b77b7565SMarcelo Ricardo Leitner &paddr->v4.sin_addr, ntohs(paddr->v4.sin_port));
1355b77b7565SMarcelo Ricardo Leitner else
1356b77b7565SMarcelo Ricardo Leitner pr_debug("sctp: asoc not found for src:%pI6:%d dst:%pI6:%d\n",
1357b77b7565SMarcelo Ricardo Leitner &laddr->v6.sin6_addr, ntohs(laddr->v6.sin6_port),
1358b77b7565SMarcelo Ricardo Leitner &paddr->v6.sin6_addr, ntohs(paddr->v6.sin6_port));
1359b77b7565SMarcelo Ricardo Leitner
1360b77b7565SMarcelo Ricardo Leitner out:
13611da177e4SLinus Torvalds return asoc;
13621da177e4SLinus Torvalds }
1363