xref: /linux/net/sctp/input.c (revision 6b5f43ea)
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 	}
584*6b5f43eaSEric 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;
7381da177e4SLinus Torvalds 
7393d3b2f57SXin Long 	ep->hashent = sctp_ep_hashfn(net, ep->base.bind_addr.port);
7403d3b2f57SXin Long 	head = &sctp_ep_hashtable[ep->hashent];
7411da177e4SLinus Torvalds 
74276c6d988SXin Long 	if (sk->sk_reuseport) {
74376c6d988SXin Long 		bool any = sctp_is_ep_boundall(sk);
7443d3b2f57SXin Long 		struct sctp_endpoint *ep2;
74576c6d988SXin Long 		struct list_head *list;
74676c6d988SXin Long 		int cnt = 0, err = 1;
74776c6d988SXin Long 
74876c6d988SXin Long 		list_for_each(list, &ep->base.bind_addr.address_list)
74976c6d988SXin Long 			cnt++;
75076c6d988SXin Long 
7513d3b2f57SXin Long 		sctp_for_each_hentry(ep2, &head->chain) {
7523d3b2f57SXin Long 			struct sock *sk2 = ep2->base.sk;
75376c6d988SXin Long 
75476c6d988SXin Long 			if (!net_eq(sock_net(sk2), net) || sk2 == sk ||
75576c6d988SXin Long 			    !uid_eq(sock_i_uid(sk2), sock_i_uid(sk)) ||
75676c6d988SXin Long 			    !sk2->sk_reuseport)
75776c6d988SXin Long 				continue;
75876c6d988SXin Long 
75976c6d988SXin Long 			err = sctp_bind_addrs_check(sctp_sk(sk2),
76076c6d988SXin Long 						    sctp_sk(sk), cnt);
76176c6d988SXin Long 			if (!err) {
76276c6d988SXin Long 				err = reuseport_add_sock(sk, sk2, any);
76376c6d988SXin Long 				if (err)
76476c6d988SXin Long 					return err;
76576c6d988SXin Long 				break;
76676c6d988SXin Long 			} else if (err < 0) {
76776c6d988SXin Long 				return err;
76876c6d988SXin Long 			}
76976c6d988SXin Long 		}
77076c6d988SXin Long 
77176c6d988SXin Long 		if (err) {
77276c6d988SXin Long 			err = reuseport_alloc(sk, any);
77376c6d988SXin Long 			if (err)
77476c6d988SXin Long 				return err;
77576c6d988SXin Long 		}
77676c6d988SXin Long 	}
77776c6d988SXin Long 
778387602dfSwangweidong 	write_lock(&head->lock);
7793d3b2f57SXin Long 	hlist_add_head(&ep->node, &head->chain);
780387602dfSwangweidong 	write_unlock(&head->lock);
78176c6d988SXin Long 	return 0;
7821da177e4SLinus Torvalds }
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds /* Add an endpoint to the hash. Local BH-safe. */
sctp_hash_endpoint(struct sctp_endpoint * ep)78576c6d988SXin Long int sctp_hash_endpoint(struct sctp_endpoint *ep)
7861da177e4SLinus Torvalds {
78776c6d988SXin Long 	int err;
78876c6d988SXin Long 
78979b91130Swangweidong 	local_bh_disable();
79076c6d988SXin Long 	err = __sctp_hash_endpoint(ep);
79179b91130Swangweidong 	local_bh_enable();
79276c6d988SXin Long 
79376c6d988SXin Long 	return err;
7941da177e4SLinus Torvalds }
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds /* Remove endpoint from the hash table.  */
__sctp_unhash_endpoint(struct sctp_endpoint * ep)7971da177e4SLinus Torvalds static void __sctp_unhash_endpoint(struct sctp_endpoint *ep)
7981da177e4SLinus Torvalds {
79976c6d988SXin Long 	struct sock *sk = ep->base.sk;
8001da177e4SLinus Torvalds 	struct sctp_hashbucket *head;
8011da177e4SLinus Torvalds 
8023d3b2f57SXin Long 	ep->hashent = sctp_ep_hashfn(sock_net(sk), ep->base.bind_addr.port);
8031da177e4SLinus Torvalds 
8043d3b2f57SXin Long 	head = &sctp_ep_hashtable[ep->hashent];
8051da177e4SLinus Torvalds 
80676c6d988SXin Long 	if (rcu_access_pointer(sk->sk_reuseport_cb))
80776c6d988SXin Long 		reuseport_detach_sock(sk);
80876c6d988SXin Long 
809387602dfSwangweidong 	write_lock(&head->lock);
8103d3b2f57SXin Long 	hlist_del_init(&ep->node);
811387602dfSwangweidong 	write_unlock(&head->lock);
8121da177e4SLinus Torvalds }
8131da177e4SLinus Torvalds 
8141da177e4SLinus Torvalds /* Remove endpoint from the hash.  Local BH-safe. */
sctp_unhash_endpoint(struct sctp_endpoint * ep)8151da177e4SLinus Torvalds void sctp_unhash_endpoint(struct sctp_endpoint *ep)
8161da177e4SLinus Torvalds {
81779b91130Swangweidong 	local_bh_disable();
8181da177e4SLinus Torvalds 	__sctp_unhash_endpoint(ep);
81979b91130Swangweidong 	local_bh_enable();
8201da177e4SLinus Torvalds }
8211da177e4SLinus Torvalds 
sctp_hashfn(const struct net * net,__be16 lport,const union sctp_addr * paddr,__u32 seed)822532ae2f1SXin Long static inline __u32 sctp_hashfn(const struct net *net, __be16 lport,
823532ae2f1SXin Long 				const union sctp_addr *paddr, __u32 seed)
824532ae2f1SXin Long {
825532ae2f1SXin Long 	__u32 addr;
826532ae2f1SXin Long 
827532ae2f1SXin Long 	if (paddr->sa.sa_family == AF_INET6)
828532ae2f1SXin Long 		addr = jhash(&paddr->v6.sin6_addr, 16, seed);
829532ae2f1SXin Long 	else
830532ae2f1SXin Long 		addr = (__force __u32)paddr->v4.sin_addr.s_addr;
831532ae2f1SXin Long 
832532ae2f1SXin Long 	return  jhash_3words(addr, ((__force __u32)paddr->v4.sin_port) << 16 |
833532ae2f1SXin Long 			     (__force __u32)lport, net_hash_mix(net), seed);
834532ae2f1SXin Long }
835532ae2f1SXin Long 
8361da177e4SLinus 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)837532ae2f1SXin Long static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(
838532ae2f1SXin Long 					struct net *net, struct sk_buff *skb,
839532ae2f1SXin Long 					const union sctp_addr *laddr,
8400af03170SXin Long 					const union sctp_addr *paddr,
8410af03170SXin Long 					int dif, int sdif)
8421da177e4SLinus Torvalds {
8431da177e4SLinus Torvalds 	struct sctp_hashbucket *head;
8441da177e4SLinus Torvalds 	struct sctp_endpoint *ep;
845532ae2f1SXin Long 	struct sock *sk;
846532ae2f1SXin Long 	__be16 lport;
8471da177e4SLinus Torvalds 	int hash;
8481da177e4SLinus Torvalds 
849532ae2f1SXin Long 	lport = laddr->v4.sin_port;
850532ae2f1SXin Long 	hash = sctp_ep_hashfn(net, ntohs(lport));
8511da177e4SLinus Torvalds 	head = &sctp_ep_hashtable[hash];
8521da177e4SLinus Torvalds 	read_lock(&head->lock);
8533d3b2f57SXin Long 	sctp_for_each_hentry(ep, &head->chain) {
8540af03170SXin Long 		if (sctp_endpoint_is_match(ep, net, laddr, dif, sdif))
8551da177e4SLinus Torvalds 			goto hit;
8561da177e4SLinus Torvalds 	}
8571da177e4SLinus Torvalds 
8582ce95503SEric W. Biederman 	ep = sctp_sk(net->sctp.ctl_sock)->ep;
8591da177e4SLinus Torvalds 
8601da177e4SLinus Torvalds hit:
861532ae2f1SXin Long 	sk = ep->base.sk;
862532ae2f1SXin Long 	if (sk->sk_reuseport) {
863532ae2f1SXin Long 		__u32 phash = sctp_hashfn(net, lport, paddr, 0);
864532ae2f1SXin Long 
865532ae2f1SXin Long 		sk = reuseport_select_sock(sk, phash, skb,
866532ae2f1SXin Long 					   sizeof(struct sctphdr));
867532ae2f1SXin Long 		if (sk)
868532ae2f1SXin Long 			ep = sctp_sk(sk)->ep;
869532ae2f1SXin Long 	}
8701da177e4SLinus Torvalds 	sctp_endpoint_hold(ep);
8711da177e4SLinus Torvalds 	read_unlock(&head->lock);
8721da177e4SLinus Torvalds 	return ep;
8731da177e4SLinus Torvalds }
8741da177e4SLinus Torvalds 
875d6c0256aSXin Long /* rhashtable for transport */
876d6c0256aSXin Long struct sctp_hash_cmp_arg {
877d6c0256aSXin Long 	const union sctp_addr	*paddr;
878d6c0256aSXin Long 	const struct net	*net;
8798d32503eSXin Long 	__be16			lport;
880d6c0256aSXin Long };
881d6c0256aSXin Long 
sctp_hash_cmp(struct rhashtable_compare_arg * arg,const void * ptr)882d6c0256aSXin Long static inline int sctp_hash_cmp(struct rhashtable_compare_arg *arg,
883d6c0256aSXin Long 				const void *ptr)
884d6c0256aSXin Long {
885715f5552SXin Long 	struct sctp_transport *t = (struct sctp_transport *)ptr;
886d6c0256aSXin Long 	const struct sctp_hash_cmp_arg *x = arg->key;
887715f5552SXin Long 	int err = 1;
888d6c0256aSXin Long 
889d6c0256aSXin Long 	if (!sctp_cmp_addr_exact(&t->ipaddr, x->paddr))
890715f5552SXin Long 		return err;
891715f5552SXin Long 	if (!sctp_transport_hold(t))
892715f5552SXin Long 		return err;
893715f5552SXin Long 
89431243461SXin Long 	if (!net_eq(t->asoc->base.net, x->net))
895715f5552SXin Long 		goto out;
8967fda702fSXin Long 	if (x->lport != htons(t->asoc->base.bind_addr.port))
897715f5552SXin Long 		goto out;
898d6c0256aSXin Long 
899715f5552SXin Long 	err = 0;
900715f5552SXin Long out:
901715f5552SXin Long 	sctp_transport_put(t);
902715f5552SXin Long 	return err;
903d6c0256aSXin Long }
904d6c0256aSXin Long 
sctp_hash_obj(const void * data,u32 len,u32 seed)9058d32503eSXin Long static inline __u32 sctp_hash_obj(const void *data, u32 len, u32 seed)
906d6c0256aSXin Long {
907d6c0256aSXin Long 	const struct sctp_transport *t = data;
908d6c0256aSXin Long 
90931243461SXin Long 	return sctp_hashfn(t->asoc->base.net,
910532ae2f1SXin Long 			   htons(t->asoc->base.bind_addr.port),
911532ae2f1SXin Long 			   &t->ipaddr, seed);
912d6c0256aSXin Long }
913d6c0256aSXin Long 
sctp_hash_key(const void * data,u32 len,u32 seed)9148d32503eSXin Long static inline __u32 sctp_hash_key(const void *data, u32 len, u32 seed)
915d6c0256aSXin Long {
916d6c0256aSXin Long 	const struct sctp_hash_cmp_arg *x = data;
917d6c0256aSXin Long 
918532ae2f1SXin Long 	return sctp_hashfn(x->net, x->lport, x->paddr, seed);
919d6c0256aSXin Long }
920d6c0256aSXin Long 
921d6c0256aSXin Long static const struct rhashtable_params sctp_hash_params = {
922d6c0256aSXin Long 	.head_offset		= offsetof(struct sctp_transport, node),
923d6c0256aSXin Long 	.hashfn			= sctp_hash_key,
924d6c0256aSXin Long 	.obj_hashfn		= sctp_hash_obj,
925d6c0256aSXin Long 	.obj_cmpfn		= sctp_hash_cmp,
926d6c0256aSXin Long 	.automatic_shrinking	= true,
927d6c0256aSXin Long };
928d6c0256aSXin Long 
sctp_transport_hashtable_init(void)929d6c0256aSXin Long int sctp_transport_hashtable_init(void)
930d6c0256aSXin Long {
9317fda702fSXin Long 	return rhltable_init(&sctp_transport_hashtable, &sctp_hash_params);
932d6c0256aSXin Long }
933d6c0256aSXin Long 
sctp_transport_hashtable_destroy(void)934d6c0256aSXin Long void sctp_transport_hashtable_destroy(void)
935d6c0256aSXin Long {
9367fda702fSXin Long 	rhltable_destroy(&sctp_transport_hashtable);
937d6c0256aSXin Long }
938d6c0256aSXin Long 
sctp_hash_transport(struct sctp_transport * t)9397fda702fSXin Long int sctp_hash_transport(struct sctp_transport *t)
940d6c0256aSXin Long {
941cd2b7087SXin Long 	struct sctp_transport *transport;
942cd2b7087SXin Long 	struct rhlist_head *tmp, *list;
943d6c0256aSXin Long 	struct sctp_hash_cmp_arg arg;
9447fda702fSXin Long 	int err;
945d6c0256aSXin Long 
946dd7445adSXin Long 	if (t->asoc->temp)
9477fda702fSXin Long 		return 0;
948dd7445adSXin Long 
9494e7696d9SXin Long 	arg.net   = t->asoc->base.net;
9507fda702fSXin Long 	arg.paddr = &t->ipaddr;
9517fda702fSXin Long 	arg.lport = htons(t->asoc->base.bind_addr.port);
952d6c0256aSXin Long 
9535179b266SXin Long 	rcu_read_lock();
954cd2b7087SXin Long 	list = rhltable_lookup(&sctp_transport_hashtable, &arg,
955cd2b7087SXin Long 			       sctp_hash_params);
956cd2b7087SXin Long 
957cd2b7087SXin Long 	rhl_for_each_entry_rcu(transport, tmp, list, node)
958cd2b7087SXin Long 		if (transport->asoc->ep == t->asoc->ep) {
9595179b266SXin Long 			rcu_read_unlock();
96027af86bbSXin Long 			return -EEXIST;
961cd2b7087SXin Long 		}
9625179b266SXin Long 	rcu_read_unlock();
963cd2b7087SXin Long 
9647fda702fSXin Long 	err = rhltable_insert_key(&sctp_transport_hashtable, &arg,
9657fda702fSXin Long 				  &t->node, sctp_hash_params);
9667fda702fSXin Long 	if (err)
9677fda702fSXin Long 		pr_err_once("insert transport fail, errno %d\n", err);
9687fda702fSXin Long 
9697fda702fSXin Long 	return err;
970d6c0256aSXin Long }
971d6c0256aSXin Long 
sctp_unhash_transport(struct sctp_transport * t)972d6c0256aSXin Long void sctp_unhash_transport(struct sctp_transport *t)
973d6c0256aSXin Long {
974dd7445adSXin Long 	if (t->asoc->temp)
975dd7445adSXin Long 		return;
976dd7445adSXin Long 
9777fda702fSXin Long 	rhltable_remove(&sctp_transport_hashtable, &t->node,
978d6c0256aSXin Long 			sctp_hash_params);
979d6c0256aSXin Long }
980d6c0256aSXin Long 
sctp_sk_bound_dev_eq(struct net * net,int bound_dev_if,int dif,int sdif)9810af03170SXin Long bool sctp_sk_bound_dev_eq(struct net *net, int bound_dev_if, int dif, int sdif)
9820af03170SXin Long {
9830af03170SXin Long 	bool l3mdev_accept = true;
9840af03170SXin Long 
9850af03170SXin Long #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV)
9860af03170SXin Long 	l3mdev_accept = !!READ_ONCE(net->sctp.l3mdev_accept);
9870af03170SXin Long #endif
9880af03170SXin Long 	return inet_bound_dev_eq(l3mdev_accept, bound_dev_if, dif, sdif);
9890af03170SXin Long }
9900af03170SXin Long 
9917fda702fSXin 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)992d6c0256aSXin Long struct sctp_transport *sctp_addrs_lookup_transport(
993d6c0256aSXin Long 				struct net *net,
994d6c0256aSXin Long 				const union sctp_addr *laddr,
9950af03170SXin Long 				const union sctp_addr *paddr,
9960af03170SXin Long 				int dif, int sdif)
997d6c0256aSXin Long {
9987fda702fSXin Long 	struct rhlist_head *tmp, *list;
9997fda702fSXin Long 	struct sctp_transport *t;
10000af03170SXin Long 	int bound_dev_if;
1001d6c0256aSXin Long 	struct sctp_hash_cmp_arg arg = {
1002d6c0256aSXin Long 		.paddr = paddr,
1003d6c0256aSXin Long 		.net   = net,
10047fda702fSXin Long 		.lport = laddr->v4.sin_port,
1005d6c0256aSXin Long 	};
1006d6c0256aSXin Long 
10077fda702fSXin Long 	list = rhltable_lookup(&sctp_transport_hashtable, &arg,
1008d6c0256aSXin Long 			       sctp_hash_params);
10097fda702fSXin Long 
10107fda702fSXin Long 	rhl_for_each_entry_rcu(t, tmp, list, node) {
10117fda702fSXin Long 		if (!sctp_transport_hold(t))
10127fda702fSXin Long 			continue;
10137fda702fSXin Long 
10140af03170SXin Long 		bound_dev_if = READ_ONCE(t->asoc->base.sk->sk_bound_dev_if);
10150af03170SXin Long 		if (sctp_sk_bound_dev_eq(net, bound_dev_if, dif, sdif) &&
10160af03170SXin Long 		    sctp_bind_addr_match(&t->asoc->base.bind_addr,
10177fda702fSXin Long 					 laddr, sctp_sk(t->asoc->base.sk)))
10187fda702fSXin Long 			return t;
10197fda702fSXin Long 		sctp_transport_put(t);
1020d6c0256aSXin Long 	}
1021d6c0256aSXin Long 
10227fda702fSXin Long 	return NULL;
10237fda702fSXin Long }
10247fda702fSXin Long 
10257fda702fSXin 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)1026d6c0256aSXin Long struct sctp_transport *sctp_epaddr_lookup_transport(
1027d6c0256aSXin Long 				const struct sctp_endpoint *ep,
1028d6c0256aSXin Long 				const union sctp_addr *paddr)
1029d6c0256aSXin Long {
10307fda702fSXin Long 	struct rhlist_head *tmp, *list;
10317fda702fSXin Long 	struct sctp_transport *t;
103265a5124aSXin Long 	struct sctp_hash_cmp_arg arg = {
103365a5124aSXin Long 		.paddr = paddr,
10344e7696d9SXin Long 		.net   = ep->base.net,
10357fda702fSXin Long 		.lport = htons(ep->base.bind_addr.port),
103665a5124aSXin Long 	};
1037d6c0256aSXin Long 
10387fda702fSXin Long 	list = rhltable_lookup(&sctp_transport_hashtable, &arg,
103965a5124aSXin Long 			       sctp_hash_params);
10407fda702fSXin Long 
10417fda702fSXin Long 	rhl_for_each_entry_rcu(t, tmp, list, node)
10427fda702fSXin Long 		if (ep == t->asoc->ep)
10437fda702fSXin Long 			return t;
10447fda702fSXin Long 
10457fda702fSXin Long 	return NULL;
1046d6c0256aSXin Long }
1047d6c0256aSXin Long 
10481da177e4SLinus 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)10491da177e4SLinus Torvalds static struct sctp_association *__sctp_lookup_association(
10504110cc25SEric W. Biederman 					struct net *net,
10511da177e4SLinus Torvalds 					const union sctp_addr *local,
10521da177e4SLinus Torvalds 					const union sctp_addr *peer,
10530af03170SXin Long 					struct sctp_transport **pt,
10540af03170SXin Long 					int dif, int sdif)
10551da177e4SLinus Torvalds {
10564f008781SXin Long 	struct sctp_transport *t;
10571eed6779SXin Long 	struct sctp_association *asoc = NULL;
10581da177e4SLinus Torvalds 
10590af03170SXin Long 	t = sctp_addrs_lookup_transport(net, local, peer, dif, sdif);
10607fda702fSXin Long 	if (!t)
10611eed6779SXin Long 		goto out;
10621da177e4SLinus Torvalds 
10631eed6779SXin Long 	asoc = t->asoc;
10644f008781SXin Long 	*pt = t;
10654f008781SXin Long 
10661eed6779SXin Long out:
10671eed6779SXin Long 	return asoc;
10681da177e4SLinus Torvalds }
10691da177e4SLinus Torvalds 
10704f008781SXin Long /* Look up an association. protected by RCU read lock */
1071dda91928SDaniel 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)10724110cc25SEric W. Biederman struct sctp_association *sctp_lookup_association(struct net *net,
10734110cc25SEric W. Biederman 						 const union sctp_addr *laddr,
10741da177e4SLinus Torvalds 						 const union sctp_addr *paddr,
10750af03170SXin Long 						 struct sctp_transport **transportp,
10760af03170SXin Long 						 int dif, int sdif)
10771da177e4SLinus Torvalds {
10781da177e4SLinus Torvalds 	struct sctp_association *asoc;
10791da177e4SLinus Torvalds 
1080f46c7011SXin Long 	rcu_read_lock();
10810af03170SXin Long 	asoc = __sctp_lookup_association(net, laddr, paddr, transportp, dif, sdif);
1082f46c7011SXin Long 	rcu_read_unlock();
10831da177e4SLinus Torvalds 
10841da177e4SLinus Torvalds 	return asoc;
10851da177e4SLinus Torvalds }
10861da177e4SLinus Torvalds 
10871da177e4SLinus 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)108853066538SXin Long bool sctp_has_association(struct net *net,
10894110cc25SEric W. Biederman 			  const union sctp_addr *laddr,
10900af03170SXin Long 			  const union sctp_addr *paddr,
10910af03170SXin Long 			  int dif, int sdif)
10921da177e4SLinus Torvalds {
10931da177e4SLinus Torvalds 	struct sctp_transport *transport;
10941da177e4SLinus Torvalds 
10950af03170SXin Long 	if (sctp_lookup_association(net, laddr, paddr, &transport, dif, sdif)) {
1096dae399d7SXin Long 		sctp_transport_put(transport);
109753066538SXin Long 		return true;
10981da177e4SLinus Torvalds 	}
10991da177e4SLinus Torvalds 
110053066538SXin Long 	return false;
11011da177e4SLinus Torvalds }
11021da177e4SLinus Torvalds 
11031da177e4SLinus Torvalds /*
11041da177e4SLinus Torvalds  * SCTP Implementors Guide, 2.18 Handling of address
11051da177e4SLinus Torvalds  * parameters within the INIT or INIT-ACK.
11061da177e4SLinus Torvalds  *
11071da177e4SLinus Torvalds  * D) When searching for a matching TCB upon reception of an INIT
11081da177e4SLinus Torvalds  *    or INIT-ACK chunk the receiver SHOULD use not only the
11091da177e4SLinus Torvalds  *    source address of the packet (containing the INIT or
11101da177e4SLinus Torvalds  *    INIT-ACK) but the receiver SHOULD also use all valid
11111da177e4SLinus Torvalds  *    address parameters contained within the chunk.
11121da177e4SLinus Torvalds  *
11131da177e4SLinus Torvalds  * 2.18.3 Solution description
11141da177e4SLinus Torvalds  *
11151da177e4SLinus Torvalds  * This new text clearly specifies to an implementor the need
11161da177e4SLinus Torvalds  * to look within the INIT or INIT-ACK. Any implementation that
11171da177e4SLinus Torvalds  * does not do this, may not be able to establish associations
11181da177e4SLinus Torvalds  * in certain circumstances.
11191da177e4SLinus Torvalds  *
11201da177e4SLinus 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)11214110cc25SEric W. Biederman static struct sctp_association *__sctp_rcv_init_lookup(struct net *net,
11224110cc25SEric W. Biederman 	struct sk_buff *skb,
11230af03170SXin Long 	const union sctp_addr *laddr, struct sctp_transport **transportp,
11240af03170SXin Long 	int dif, int sdif)
11251da177e4SLinus Torvalds {
11261da177e4SLinus Torvalds 	struct sctp_association *asoc;
11271da177e4SLinus Torvalds 	union sctp_addr addr;
11281da177e4SLinus Torvalds 	union sctp_addr *paddr = &addr;
11292c0fd387SArnaldo Carvalho de Melo 	struct sctphdr *sh = sctp_hdr(skb);
11301da177e4SLinus Torvalds 	union sctp_params params;
113101a992beSXin Long 	struct sctp_init_chunk *init;
11321da177e4SLinus Torvalds 	struct sctp_af *af;
11331da177e4SLinus Torvalds 
11341da177e4SLinus Torvalds 	/*
11351da177e4SLinus Torvalds 	 * This code will NOT touch anything inside the chunk--it is
11361da177e4SLinus Torvalds 	 * strictly READ-ONLY.
11371da177e4SLinus Torvalds 	 *
11381da177e4SLinus Torvalds 	 * RFC 2960 3  SCTP packet Format
11391da177e4SLinus Torvalds 	 *
11401da177e4SLinus Torvalds 	 * Multiple chunks can be bundled into one SCTP packet up to
11411da177e4SLinus Torvalds 	 * the MTU size, except for the INIT, INIT ACK, and SHUTDOWN
11421da177e4SLinus Torvalds 	 * COMPLETE chunks.  These chunks MUST NOT be bundled with any
11431da177e4SLinus Torvalds 	 * other chunk in a packet.  See Section 6.10 for more details
11441da177e4SLinus Torvalds 	 * on chunk bundling.
11451da177e4SLinus Torvalds 	 */
11461da177e4SLinus Torvalds 
11471da177e4SLinus Torvalds 	/* Find the start of the TLVs and the end of the chunk.  This is
11481da177e4SLinus Torvalds 	 * the region we search for address parameters.
11491da177e4SLinus Torvalds 	 */
115001a992beSXin Long 	init = (struct sctp_init_chunk *)skb->data;
11511da177e4SLinus Torvalds 
11521da177e4SLinus Torvalds 	/* Walk the parameters looking for embedded addresses. */
1153add7370aSXin Long 	sctp_walk_params(params, init) {
11541da177e4SLinus Torvalds 
11551da177e4SLinus Torvalds 		/* Note: Ignoring hostname addresses. */
11561da177e4SLinus Torvalds 		af = sctp_get_af_specific(param_type2af(params.p->type));
11571da177e4SLinus Torvalds 		if (!af)
11581da177e4SLinus Torvalds 			continue;
11591da177e4SLinus Torvalds 
11600c5dc070SMarcelo Ricardo Leitner 		if (!af->from_addr_param(paddr, params.addr, sh->source, 0))
11610c5dc070SMarcelo Ricardo Leitner 			continue;
11621da177e4SLinus Torvalds 
11630af03170SXin Long 		asoc = __sctp_lookup_association(net, laddr, paddr, transportp, dif, sdif);
11641da177e4SLinus Torvalds 		if (asoc)
11651da177e4SLinus Torvalds 			return asoc;
11661da177e4SLinus Torvalds 	}
11671da177e4SLinus Torvalds 
11681da177e4SLinus Torvalds 	return NULL;
11691da177e4SLinus Torvalds }
11701da177e4SLinus Torvalds 
1171df218577SVlad Yasevich /* ADD-IP, Section 5.2
1172df218577SVlad Yasevich  * When an endpoint receives an ASCONF Chunk from the remote peer
1173df218577SVlad Yasevich  * special procedures may be needed to identify the association the
1174df218577SVlad Yasevich  * ASCONF Chunk is associated with. To properly find the association
1175df218577SVlad Yasevich  * the following procedures SHOULD be followed:
1176df218577SVlad Yasevich  *
1177df218577SVlad Yasevich  * D2) If the association is not found, use the address found in the
1178df218577SVlad Yasevich  * Address Parameter TLV combined with the port number found in the
1179df218577SVlad Yasevich  * SCTP common header. If found proceed to rule D4.
1180df218577SVlad Yasevich  *
1181df218577SVlad Yasevich  * D2-ext) If more than one ASCONF Chunks are packed together, use the
1182df218577SVlad Yasevich  * address found in the ASCONF Address Parameter TLV of each of the
1183df218577SVlad Yasevich  * subsequent ASCONF Chunks. If found, proceed to rule D4.
1184df218577SVlad 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)1185df218577SVlad Yasevich static struct sctp_association *__sctp_rcv_asconf_lookup(
11864110cc25SEric W. Biederman 					struct net *net,
1187922dbc5bSXin Long 					struct sctp_chunkhdr *ch,
1188df218577SVlad Yasevich 					const union sctp_addr *laddr,
1189bc92dd19SAl Viro 					__be16 peer_port,
11900af03170SXin Long 					struct sctp_transport **transportp,
11910af03170SXin Long 					int dif, int sdif)
1192df218577SVlad Yasevich {
119368d75469SXin Long 	struct sctp_addip_chunk *asconf = (struct sctp_addip_chunk *)ch;
1194df218577SVlad Yasevich 	struct sctp_af *af;
1195df218577SVlad Yasevich 	union sctp_addr_param *param;
1196df218577SVlad Yasevich 	union sctp_addr paddr;
1197df218577SVlad Yasevich 
1198b6ffe767SMarcelo Ricardo Leitner 	if (ntohs(ch->length) < sizeof(*asconf) + sizeof(struct sctp_paramhdr))
1199b6ffe767SMarcelo Ricardo Leitner 		return NULL;
1200b6ffe767SMarcelo Ricardo Leitner 
1201df218577SVlad Yasevich 	/* Skip over the ADDIP header and find the Address parameter */
1202df218577SVlad Yasevich 	param = (union sctp_addr_param *)(asconf + 1);
1203df218577SVlad Yasevich 
12046a435732SShan Wei 	af = sctp_get_af_specific(param_type2af(param->p.type));
1205df218577SVlad Yasevich 	if (unlikely(!af))
1206df218577SVlad Yasevich 		return NULL;
1207df218577SVlad Yasevich 
1208557fb586SMarcelo Ricardo Leitner 	if (!af->from_addr_param(&paddr, param, peer_port, 0))
12090c5dc070SMarcelo Ricardo Leitner 		return NULL;
1210df218577SVlad Yasevich 
12110af03170SXin Long 	return __sctp_lookup_association(net, laddr, &paddr, transportp, dif, sdif);
1212df218577SVlad Yasevich }
1213df218577SVlad Yasevich 
1214df218577SVlad Yasevich 
1215bbd0d598SVlad Yasevich /* SCTP-AUTH, Section 6.3:
1216bbd0d598SVlad Yasevich *    If the receiver does not find a STCB for a packet containing an AUTH
1217bbd0d598SVlad Yasevich *    chunk as the first chunk and not a COOKIE-ECHO chunk as the second
1218bbd0d598SVlad Yasevich *    chunk, it MUST use the chunks after the AUTH chunk to look up an existing
1219bbd0d598SVlad Yasevich *    association.
1220bbd0d598SVlad Yasevich *
1221bbd0d598SVlad Yasevich * This means that any chunks that can help us identify the association need
122225985edcSLucas De Marchi * to be looked at to find this association.
1223bbd0d598SVlad 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)12244110cc25SEric W. Biederman static struct sctp_association *__sctp_rcv_walk_lookup(struct net *net,
12254110cc25SEric W. Biederman 				      struct sk_buff *skb,
1226bbd0d598SVlad Yasevich 				      const union sctp_addr *laddr,
12270af03170SXin Long 				      struct sctp_transport **transportp,
12280af03170SXin Long 				      int dif, int sdif)
1229bbd0d598SVlad Yasevich {
1230df218577SVlad Yasevich 	struct sctp_association *asoc = NULL;
1231922dbc5bSXin Long 	struct sctp_chunkhdr *ch;
1232df218577SVlad Yasevich 	int have_auth = 0;
1233df218577SVlad Yasevich 	unsigned int chunk_num = 1;
1234df218577SVlad Yasevich 	__u8 *ch_end;
1235df218577SVlad Yasevich 
1236df218577SVlad Yasevich 	/* Walk through the chunks looking for AUTH or ASCONF chunks
1237df218577SVlad Yasevich 	 * to help us find the association.
1238bbd0d598SVlad Yasevich 	 */
1239922dbc5bSXin Long 	ch = (struct sctp_chunkhdr *)skb->data;
1240df218577SVlad Yasevich 	do {
1241df218577SVlad Yasevich 		/* Break out if chunk length is less then minimal. */
1242922dbc5bSXin Long 		if (ntohs(ch->length) < sizeof(*ch))
1243df218577SVlad Yasevich 			break;
1244df218577SVlad Yasevich 
1245e2f036a9SMarcelo Ricardo Leitner 		ch_end = ((__u8 *)ch) + SCTP_PAD4(ntohs(ch->length));
1246df218577SVlad Yasevich 		if (ch_end > skb_tail_pointer(skb))
1247df218577SVlad Yasevich 			break;
1248df218577SVlad Yasevich 
1249df218577SVlad Yasevich 		switch (ch->type) {
1250df218577SVlad Yasevich 		case SCTP_CID_AUTH:
1251df218577SVlad Yasevich 			have_auth = chunk_num;
1252df218577SVlad Yasevich 			break;
1253df218577SVlad Yasevich 
1254df218577SVlad Yasevich 		case SCTP_CID_COOKIE_ECHO:
1255df218577SVlad Yasevich 			/* If a packet arrives containing an AUTH chunk as
1256df218577SVlad Yasevich 			 * a first chunk, a COOKIE-ECHO chunk as the second
1257df218577SVlad Yasevich 			 * chunk, and possibly more chunks after them, and
1258df218577SVlad Yasevich 			 * the receiver does not have an STCB for that
1259df218577SVlad Yasevich 			 * packet, then authentication is based on
1260df218577SVlad Yasevich 			 * the contents of the COOKIE- ECHO chunk.
1261df218577SVlad Yasevich 			 */
1262df218577SVlad Yasevich 			if (have_auth == 1 && chunk_num == 2)
1263bbd0d598SVlad Yasevich 				return NULL;
1264df218577SVlad Yasevich 			break;
1265df218577SVlad Yasevich 
1266df218577SVlad Yasevich 		case SCTP_CID_ASCONF:
1267e1fc3b14SEric W. Biederman 			if (have_auth || net->sctp.addip_noauth)
12684110cc25SEric W. Biederman 				asoc = __sctp_rcv_asconf_lookup(
12694110cc25SEric W. Biederman 						net, ch, laddr,
1270df218577SVlad Yasevich 						sctp_hdr(skb)->source,
12710af03170SXin Long 						transportp, dif, sdif);
12720572b37bSGustavo A. R. Silva 			break;
1273df218577SVlad Yasevich 		default:
1274df218577SVlad Yasevich 			break;
1275df218577SVlad Yasevich 		}
1276df218577SVlad Yasevich 
1277df218577SVlad Yasevich 		if (asoc)
1278df218577SVlad Yasevich 			break;
1279df218577SVlad Yasevich 
1280922dbc5bSXin Long 		ch = (struct sctp_chunkhdr *)ch_end;
1281df218577SVlad Yasevich 		chunk_num++;
128250619dbfSMarcelo Ricardo Leitner 	} while (ch_end + sizeof(*ch) < skb_tail_pointer(skb));
1283df218577SVlad Yasevich 
1284df218577SVlad Yasevich 	return asoc;
1285bbd0d598SVlad Yasevich }
1286bbd0d598SVlad Yasevich 
1287bbd0d598SVlad Yasevich /*
1288bbd0d598SVlad Yasevich  * There are circumstances when we need to look inside the SCTP packet
1289bbd0d598SVlad Yasevich  * for information to help us find the association.   Examples
1290bbd0d598SVlad Yasevich  * include looking inside of INIT/INIT-ACK chunks or after the AUTH
1291bbd0d598SVlad Yasevich  * chunks.
1292bbd0d598SVlad 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)12934110cc25SEric W. Biederman static struct sctp_association *__sctp_rcv_lookup_harder(struct net *net,
12944110cc25SEric W. Biederman 				      struct sk_buff *skb,
1295bbd0d598SVlad Yasevich 				      const union sctp_addr *laddr,
12960af03170SXin Long 				      struct sctp_transport **transportp,
12970af03170SXin Long 				      int dif, int sdif)
1298bbd0d598SVlad Yasevich {
1299922dbc5bSXin Long 	struct sctp_chunkhdr *ch;
1300bbd0d598SVlad Yasevich 
130190017accSMarcelo Ricardo Leitner 	/* We do not allow GSO frames here as we need to linearize and
130290017accSMarcelo Ricardo Leitner 	 * then cannot guarantee frame boundaries. This shouldn't be an
130390017accSMarcelo Ricardo Leitner 	 * issue as packets hitting this are mostly INIT or INIT-ACK and
130490017accSMarcelo Ricardo Leitner 	 * those cannot be on GSO-style anyway.
130590017accSMarcelo Ricardo Leitner 	 */
13061dd27cdeSDaniel Axtens 	if (skb_is_gso(skb) && skb_is_gso_sctp(skb))
130790017accSMarcelo Ricardo Leitner 		return NULL;
130890017accSMarcelo Ricardo Leitner 
1309922dbc5bSXin Long 	ch = (struct sctp_chunkhdr *)skb->data;
1310bbd0d598SVlad Yasevich 
1311df218577SVlad Yasevich 	/* The code below will attempt to walk the chunk and extract
1312df218577SVlad Yasevich 	 * parameter information.  Before we do that, we need to verify
1313df218577SVlad Yasevich 	 * that the chunk length doesn't cause overflow.  Otherwise, we'll
1314df218577SVlad Yasevich 	 * walk off the end.
1315df218577SVlad Yasevich 	 */
1316e2f036a9SMarcelo Ricardo Leitner 	if (SCTP_PAD4(ntohs(ch->length)) > skb->len)
1317df218577SVlad Yasevich 		return NULL;
1318df218577SVlad Yasevich 
1319bbd0d598SVlad Yasevich 	/* If this is INIT/INIT-ACK look inside the chunk too. */
1320f482f2fcSwangweidong 	if (ch->type == SCTP_CID_INIT || ch->type == SCTP_CID_INIT_ACK)
13210af03170SXin Long 		return __sctp_rcv_init_lookup(net, skb, laddr, transportp, dif, sdif);
1322bbd0d598SVlad Yasevich 
13230af03170SXin Long 	return __sctp_rcv_walk_lookup(net, skb, laddr, transportp, dif, sdif);
1324bbd0d598SVlad Yasevich }
1325bbd0d598SVlad Yasevich 
13261da177e4SLinus 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)13274110cc25SEric W. Biederman static struct sctp_association *__sctp_rcv_lookup(struct net *net,
13284110cc25SEric W. Biederman 				      struct sk_buff *skb,
13291da177e4SLinus Torvalds 				      const union sctp_addr *paddr,
13301da177e4SLinus Torvalds 				      const union sctp_addr *laddr,
13310af03170SXin Long 				      struct sctp_transport **transportp,
13320af03170SXin Long 				      int dif, int sdif)
13331da177e4SLinus Torvalds {
13341da177e4SLinus Torvalds 	struct sctp_association *asoc;
13351da177e4SLinus Torvalds 
13360af03170SXin Long 	asoc = __sctp_lookup_association(net, laddr, paddr, transportp, dif, sdif);
1337b77b7565SMarcelo Ricardo Leitner 	if (asoc)
1338b77b7565SMarcelo Ricardo Leitner 		goto out;
13391da177e4SLinus Torvalds 
13401da177e4SLinus Torvalds 	/* Further lookup for INIT/INIT-ACK packets.
13411da177e4SLinus Torvalds 	 * SCTP Implementors Guide, 2.18 Handling of address
13421da177e4SLinus Torvalds 	 * parameters within the INIT or INIT-ACK.
13431da177e4SLinus Torvalds 	 */
13440af03170SXin Long 	asoc = __sctp_rcv_lookup_harder(net, skb, laddr, transportp, dif, sdif);
1345b77b7565SMarcelo Ricardo Leitner 	if (asoc)
1346b77b7565SMarcelo Ricardo Leitner 		goto out;
13471da177e4SLinus Torvalds 
1348b77b7565SMarcelo Ricardo Leitner 	if (paddr->sa.sa_family == AF_INET)
1349b77b7565SMarcelo Ricardo Leitner 		pr_debug("sctp: asoc not found for src:%pI4:%d dst:%pI4:%d\n",
1350b77b7565SMarcelo Ricardo Leitner 			 &laddr->v4.sin_addr, ntohs(laddr->v4.sin_port),
1351b77b7565SMarcelo Ricardo Leitner 			 &paddr->v4.sin_addr, ntohs(paddr->v4.sin_port));
1352b77b7565SMarcelo Ricardo Leitner 	else
1353b77b7565SMarcelo Ricardo Leitner 		pr_debug("sctp: asoc not found for src:%pI6:%d dst:%pI6:%d\n",
1354b77b7565SMarcelo Ricardo Leitner 			 &laddr->v6.sin6_addr, ntohs(laddr->v6.sin6_port),
1355b77b7565SMarcelo Ricardo Leitner 			 &paddr->v6.sin6_addr, ntohs(paddr->v6.sin6_port));
1356b77b7565SMarcelo Ricardo Leitner 
1357b77b7565SMarcelo Ricardo Leitner out:
13581da177e4SLinus Torvalds 	return asoc;
13591da177e4SLinus Torvalds }
1360