xref: /linux/net/sctp/input.c (revision 024ec3deac33ddbd81f3c887506f132b24ea21a7)
160c778b2SVlad Yasevich /* SCTP kernel implementation
21da177e4SLinus Torvalds  * Copyright (c) 1999-2000 Cisco, Inc.
31da177e4SLinus Torvalds  * Copyright (c) 1999-2001 Motorola, Inc.
41da177e4SLinus Torvalds  * Copyright (c) 2001-2003 International Business Machines, Corp.
51da177e4SLinus Torvalds  * Copyright (c) 2001 Intel Corp.
61da177e4SLinus Torvalds  * Copyright (c) 2001 Nokia, Inc.
71da177e4SLinus Torvalds  * Copyright (c) 2001 La Monte H.P. Yarroll
81da177e4SLinus Torvalds  *
960c778b2SVlad Yasevich  * This file is part of the SCTP kernel implementation
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  * These functions handle all input from the IP layer into SCTP.
121da177e4SLinus Torvalds  *
1360c778b2SVlad Yasevich  * This SCTP implementation is free software;
141da177e4SLinus Torvalds  * you can redistribute it and/or modify it under the terms of
151da177e4SLinus Torvalds  * the GNU General Public License as published by
161da177e4SLinus Torvalds  * the Free Software Foundation; either version 2, or (at your option)
171da177e4SLinus Torvalds  * any later version.
181da177e4SLinus Torvalds  *
1960c778b2SVlad Yasevich  * This SCTP implementation is distributed in the hope that it
201da177e4SLinus Torvalds  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
211da177e4SLinus Torvalds  *                 ************************
221da177e4SLinus Torvalds  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
231da177e4SLinus Torvalds  * See the GNU General Public License for more details.
241da177e4SLinus Torvalds  *
251da177e4SLinus Torvalds  * You should have received a copy of the GNU General Public License
261da177e4SLinus Torvalds  * along with GNU CC; see the file COPYING.  If not, write to
271da177e4SLinus Torvalds  * the Free Software Foundation, 59 Temple Place - Suite 330,
281da177e4SLinus Torvalds  * Boston, MA 02111-1307, USA.
291da177e4SLinus Torvalds  *
301da177e4SLinus Torvalds  * Please send any bug reports or fixes you make to the
311da177e4SLinus Torvalds  * email address(es):
3291705c61SDaniel Borkmann  *    lksctp developers <linux-sctp@vger.kernel.org>
331da177e4SLinus Torvalds  *
341da177e4SLinus Torvalds  * Or submit a bug report through the following website:
351da177e4SLinus Torvalds  *    http://www.sf.net/projects/lksctp
361da177e4SLinus Torvalds  *
371da177e4SLinus Torvalds  * Written or modified by:
381da177e4SLinus Torvalds  *    La Monte H.P. Yarroll <piggy@acm.org>
391da177e4SLinus Torvalds  *    Karl Knutson <karl@athena.chicago.il.us>
401da177e4SLinus Torvalds  *    Xingang Guo <xingang.guo@intel.com>
411da177e4SLinus Torvalds  *    Jon Grimm <jgrimm@us.ibm.com>
421da177e4SLinus Torvalds  *    Hui Huang <hui.huang@nokia.com>
431da177e4SLinus Torvalds  *    Daisy Chang <daisyc@us.ibm.com>
441da177e4SLinus Torvalds  *    Sridhar Samudrala <sri@us.ibm.com>
451da177e4SLinus Torvalds  *    Ardelle Fan <ardelle.fan@intel.com>
461da177e4SLinus Torvalds  *
471da177e4SLinus Torvalds  * Any bugs reported given to us we will try to fix... any fixes shared will
481da177e4SLinus Torvalds  * be incorporated into the next SCTP release.
491da177e4SLinus Torvalds  */
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds #include <linux/types.h>
521da177e4SLinus Torvalds #include <linux/list.h> /* For struct list_head */
531da177e4SLinus Torvalds #include <linux/socket.h>
541da177e4SLinus Torvalds #include <linux/ip.h>
551da177e4SLinus Torvalds #include <linux/time.h> /* For struct timeval */
565a0e3ad6STejun Heo #include <linux/slab.h>
571da177e4SLinus Torvalds #include <net/ip.h>
581da177e4SLinus Torvalds #include <net/icmp.h>
591da177e4SLinus Torvalds #include <net/snmp.h>
601da177e4SLinus Torvalds #include <net/sock.h>
611da177e4SLinus Torvalds #include <net/xfrm.h>
621da177e4SLinus Torvalds #include <net/sctp/sctp.h>
631da177e4SLinus Torvalds #include <net/sctp/sm.h>
649ad0977fSVlad Yasevich #include <net/sctp/checksum.h>
65dcfc23caSPavel Emelyanov #include <net/net_namespace.h>
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds /* Forward declarations for internal helpers. */
681da177e4SLinus Torvalds static int sctp_rcv_ootb(struct sk_buff *);
694110cc25SEric W. Biederman static struct sctp_association *__sctp_rcv_lookup(struct net *net,
704110cc25SEric W. Biederman 				      struct sk_buff *skb,
711da177e4SLinus Torvalds 				      const union sctp_addr *paddr,
7257565993SNicolas Dichtel 				      const union sctp_addr *laddr,
731da177e4SLinus Torvalds 				      struct sctp_transport **transportp);
744cdadcbcSEric W. Biederman static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(struct net *net,
754cdadcbcSEric W. Biederman 						const union sctp_addr *laddr);
761da177e4SLinus Torvalds static struct sctp_association *__sctp_lookup_association(
774110cc25SEric W. Biederman 					struct net *net,
781da177e4SLinus Torvalds 					const union sctp_addr *local,
791da177e4SLinus Torvalds 					const union sctp_addr *peer,
801da177e4SLinus Torvalds 					struct sctp_transport **pt);
811da177e4SLinus Torvalds 
8250b1a782SZhu Yi static int sctp_add_backlog(struct sock *sk, struct sk_buff *skb);
8361c9fed4SVladislav Yasevich 
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds /* Calculate the SCTP checksum of an SCTP packet.  */
86b01a2407SEric W. Biederman static inline int sctp_rcv_checksum(struct net *net, struct sk_buff *skb)
871da177e4SLinus Torvalds {
882c0fd387SArnaldo Carvalho de Melo 	struct sctphdr *sh = sctp_hdr(skb);
894458f04cSVlad Yasevich 	__le32 cmp = sh->checksum;
90*024ec3deSJoe Stringer 	__le32 val = sctp_compute_cksum(skb, 0);
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds 	if (val != cmp) {
931da177e4SLinus Torvalds 		/* CRC failure, dump it. */
94b01a2407SEric W. Biederman 		SCTP_INC_STATS_BH(net, SCTP_MIB_CHECKSUMERRORS);
951da177e4SLinus Torvalds 		return -1;
961da177e4SLinus Torvalds 	}
971da177e4SLinus Torvalds 	return 0;
981da177e4SLinus Torvalds }
991da177e4SLinus Torvalds 
10079af02c2SDavid S. Miller struct sctp_input_cb {
10179af02c2SDavid S. Miller 	union {
10279af02c2SDavid S. Miller 		struct inet_skb_parm	h4;
103dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
10479af02c2SDavid S. Miller 		struct inet6_skb_parm	h6;
10579af02c2SDavid S. Miller #endif
10679af02c2SDavid S. Miller 	} header;
10779af02c2SDavid S. Miller 	struct sctp_chunk *chunk;
10879af02c2SDavid S. Miller };
10979af02c2SDavid S. Miller #define SCTP_INPUT_CB(__skb)	((struct sctp_input_cb *)&((__skb)->cb[0]))
11079af02c2SDavid S. Miller 
1111da177e4SLinus Torvalds /*
1121da177e4SLinus Torvalds  * This is the routine which IP calls when receiving an SCTP packet.
1131da177e4SLinus Torvalds  */
1141da177e4SLinus Torvalds int sctp_rcv(struct sk_buff *skb)
1151da177e4SLinus Torvalds {
1161da177e4SLinus Torvalds 	struct sock *sk;
1171da177e4SLinus Torvalds 	struct sctp_association *asoc;
1181da177e4SLinus Torvalds 	struct sctp_endpoint *ep = NULL;
1191da177e4SLinus Torvalds 	struct sctp_ep_common *rcvr;
1201da177e4SLinus Torvalds 	struct sctp_transport *transport = NULL;
1211da177e4SLinus Torvalds 	struct sctp_chunk *chunk;
1221da177e4SLinus Torvalds 	struct sctphdr *sh;
1231da177e4SLinus Torvalds 	union sctp_addr src;
1241da177e4SLinus Torvalds 	union sctp_addr dest;
1251da177e4SLinus Torvalds 	int family;
1261da177e4SLinus Torvalds 	struct sctp_af *af;
1274cdadcbcSEric W. Biederman 	struct net *net = dev_net(skb->dev);
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 	if (skb->pkt_type!=PACKET_HOST)
1301da177e4SLinus Torvalds 		goto discard_it;
1311da177e4SLinus Torvalds 
132b01a2407SEric W. Biederman 	SCTP_INC_STATS_BH(net, SCTP_MIB_INSCTPPACKS);
1331da177e4SLinus Torvalds 
13428cd7752SHerbert Xu 	if (skb_linearize(skb))
13528cd7752SHerbert Xu 		goto discard_it;
13628cd7752SHerbert Xu 
1372c0fd387SArnaldo Carvalho de Melo 	sh = sctp_hdr(skb);
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds 	/* Pull up the IP and SCTP headers. */
140ea2ae17dSArnaldo Carvalho de Melo 	__skb_pull(skb, skb_transport_offset(skb));
1411da177e4SLinus Torvalds 	if (skb->len < sizeof(struct sctphdr))
1421da177e4SLinus Torvalds 		goto discard_it;
14306e86806SLucas Nussbaum 	if (!sctp_checksum_disable && !skb_csum_unnecessary(skb) &&
144b01a2407SEric W. Biederman 		  sctp_rcv_checksum(net, skb) < 0)
1451da177e4SLinus Torvalds 		goto discard_it;
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds 	skb_pull(skb, sizeof(struct sctphdr));
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds 	/* Make sure we at least have chunk headers worth of data left. */
1501da177e4SLinus Torvalds 	if (skb->len < sizeof(struct sctp_chunkhdr))
1511da177e4SLinus Torvalds 		goto discard_it;
1521da177e4SLinus Torvalds 
153eddc9ec5SArnaldo Carvalho de Melo 	family = ipver2af(ip_hdr(skb)->version);
1541da177e4SLinus Torvalds 	af = sctp_get_af_specific(family);
1551da177e4SLinus Torvalds 	if (unlikely(!af))
1561da177e4SLinus Torvalds 		goto discard_it;
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds 	/* Initialize local addresses for lookups. */
1591da177e4SLinus Torvalds 	af->from_skb(&src, skb, 1);
1601da177e4SLinus Torvalds 	af->from_skb(&dest, skb, 0);
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds 	/* If the packet is to or from a non-unicast address,
1631da177e4SLinus Torvalds 	 * silently discard the packet.
1641da177e4SLinus Torvalds 	 *
1651da177e4SLinus Torvalds 	 * This is not clearly defined in the RFC except in section
1661da177e4SLinus Torvalds 	 * 8.4 - OOTB handling.  However, based on the book "Stream Control
1671da177e4SLinus Torvalds 	 * Transmission Protocol" 2.1, "It is important to note that the
1681da177e4SLinus Torvalds 	 * IP address of an SCTP transport address must be a routable
1691da177e4SLinus Torvalds 	 * unicast address.  In other words, IP multicast addresses and
1701da177e4SLinus Torvalds 	 * IP broadcast addresses cannot be used in an SCTP transport
1711da177e4SLinus Torvalds 	 * address."
1721da177e4SLinus Torvalds 	 */
1735636bef7SVlad Yasevich 	if (!af->addr_valid(&src, NULL, skb) ||
1745636bef7SVlad Yasevich 	    !af->addr_valid(&dest, NULL, skb))
1751da177e4SLinus Torvalds 		goto discard_it;
1761da177e4SLinus Torvalds 
1774110cc25SEric W. Biederman 	asoc = __sctp_rcv_lookup(net, skb, &src, &dest, &transport);
1781c7d1fc1SAl Viro 
1790fd9a65aSNeil Horman 	if (!asoc)
1804cdadcbcSEric W. Biederman 		ep = __sctp_rcv_lookup_endpoint(net, &dest);
1810fd9a65aSNeil Horman 
1820fd9a65aSNeil Horman 	/* Retrieve the common input handling substructure. */
1830fd9a65aSNeil Horman 	rcvr = asoc ? &asoc->base : &ep->base;
1840fd9a65aSNeil Horman 	sk = rcvr->sk;
1850fd9a65aSNeil Horman 
1860fd9a65aSNeil Horman 	/*
1870fd9a65aSNeil Horman 	 * If a frame arrives on an interface and the receiving socket is
1880fd9a65aSNeil Horman 	 * bound to another interface, via SO_BINDTODEVICE, treat it as OOTB
1890fd9a65aSNeil Horman 	 */
1900fd9a65aSNeil Horman 	if (sk->sk_bound_dev_if && (sk->sk_bound_dev_if != af->skb_iif(skb)))
1910fd9a65aSNeil Horman 	{
1920fd9a65aSNeil Horman 		if (asoc) {
1930fd9a65aSNeil Horman 			sctp_association_put(asoc);
1940fd9a65aSNeil Horman 			asoc = NULL;
1950fd9a65aSNeil Horman 		} else {
1960fd9a65aSNeil Horman 			sctp_endpoint_put(ep);
1970fd9a65aSNeil Horman 			ep = NULL;
1980fd9a65aSNeil Horman 		}
1992ce95503SEric W. Biederman 		sk = net->sctp.ctl_sock;
2000fd9a65aSNeil Horman 		ep = sctp_sk(sk)->ep;
2010fd9a65aSNeil Horman 		sctp_endpoint_hold(ep);
2020fd9a65aSNeil Horman 		rcvr = &ep->base;
2030fd9a65aSNeil Horman 	}
2040fd9a65aSNeil Horman 
2051da177e4SLinus Torvalds 	/*
2061da177e4SLinus Torvalds 	 * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
2071da177e4SLinus Torvalds 	 * An SCTP packet is called an "out of the blue" (OOTB)
2081da177e4SLinus Torvalds 	 * packet if it is correctly formed, i.e., passed the
2091da177e4SLinus Torvalds 	 * receiver's checksum check, but the receiver is not
2101da177e4SLinus Torvalds 	 * able to identify the association to which this
2111da177e4SLinus Torvalds 	 * packet belongs.
2121da177e4SLinus Torvalds 	 */
2131da177e4SLinus Torvalds 	if (!asoc) {
2141da177e4SLinus Torvalds 		if (sctp_rcv_ootb(skb)) {
215b01a2407SEric W. Biederman 			SCTP_INC_STATS_BH(net, SCTP_MIB_OUTOFBLUES);
2161da177e4SLinus Torvalds 			goto discard_release;
2171da177e4SLinus Torvalds 		}
2181da177e4SLinus Torvalds 	}
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds 	if (!xfrm_policy_check(sk, XFRM_POLICY_IN, skb, family))
2211da177e4SLinus Torvalds 		goto discard_release;
222b59c2701SPatrick McHardy 	nf_reset(skb);
2231da177e4SLinus Torvalds 
224fda9ef5dSDmitry Mishin 	if (sk_filter(sk, skb))
2251da177e4SLinus Torvalds 		goto discard_release;
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds 	/* Create an SCTP packet structure. */
2281da177e4SLinus Torvalds 	chunk = sctp_chunkify(skb, asoc, sk);
2292babf9daSHerbert Xu 	if (!chunk)
2301da177e4SLinus Torvalds 		goto discard_release;
23179af02c2SDavid S. Miller 	SCTP_INPUT_CB(skb)->chunk = chunk;
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds 	/* Remember what endpoint is to handle this packet. */
2341da177e4SLinus Torvalds 	chunk->rcvr = rcvr;
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds 	/* Remember the SCTP header. */
2371da177e4SLinus Torvalds 	chunk->sctp_hdr = sh;
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds 	/* Set the source and destination addresses of the incoming chunk.  */
240d55c41b1SAl Viro 	sctp_init_addrs(chunk, &src, &dest);
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds 	/* Remember where we came from.  */
2431da177e4SLinus Torvalds 	chunk->transport = transport;
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds 	/* Acquire access to the sock lock. Note: We are safe from other
2461da177e4SLinus Torvalds 	 * bottom halves on this lock, but a user may be in the lock too,
2471da177e4SLinus Torvalds 	 * so check if it is busy.
2481da177e4SLinus Torvalds 	 */
2491da177e4SLinus Torvalds 	sctp_bh_lock_sock(sk);
2501da177e4SLinus Torvalds 
251ae53b5bdSVlad Yasevich 	if (sk != rcvr->sk) {
252ae53b5bdSVlad Yasevich 		/* Our cached sk is different from the rcvr->sk.  This is
253ae53b5bdSVlad Yasevich 		 * because migrate()/accept() may have moved the association
254ae53b5bdSVlad Yasevich 		 * to a new socket and released all the sockets.  So now we
255ae53b5bdSVlad Yasevich 		 * are holding a lock on the old socket while the user may
256ae53b5bdSVlad Yasevich 		 * be doing something with the new socket.  Switch our veiw
257ae53b5bdSVlad Yasevich 		 * of the current sk.
258ae53b5bdSVlad Yasevich 		 */
259ae53b5bdSVlad Yasevich 		sctp_bh_unlock_sock(sk);
260ae53b5bdSVlad Yasevich 		sk = rcvr->sk;
261ae53b5bdSVlad Yasevich 		sctp_bh_lock_sock(sk);
262ae53b5bdSVlad Yasevich 	}
263ae53b5bdSVlad Yasevich 
264ac0b0462SSridhar Samudrala 	if (sock_owned_by_user(sk)) {
26550b1a782SZhu Yi 		if (sctp_add_backlog(sk, skb)) {
26650b1a782SZhu Yi 			sctp_bh_unlock_sock(sk);
26750b1a782SZhu Yi 			sctp_chunk_free(chunk);
26850b1a782SZhu Yi 			skb = NULL; /* sctp_chunk_free already freed the skb */
26950b1a782SZhu Yi 			goto discard_release;
27050b1a782SZhu Yi 		}
271b01a2407SEric W. Biederman 		SCTP_INC_STATS_BH(net, SCTP_MIB_IN_PKT_BACKLOG);
272ac0b0462SSridhar Samudrala 	} else {
273b01a2407SEric W. Biederman 		SCTP_INC_STATS_BH(net, SCTP_MIB_IN_PKT_SOFTIRQ);
27461c9fed4SVladislav Yasevich 		sctp_inq_push(&chunk->rcvr->inqueue, chunk);
275ac0b0462SSridhar Samudrala 	}
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 	sctp_bh_unlock_sock(sk);
27861c9fed4SVladislav Yasevich 
27961c9fed4SVladislav Yasevich 	/* Release the asoc/ep ref we took in the lookup calls. */
28061c9fed4SVladislav Yasevich 	if (asoc)
28161c9fed4SVladislav Yasevich 		sctp_association_put(asoc);
28261c9fed4SVladislav Yasevich 	else
28361c9fed4SVladislav Yasevich 		sctp_endpoint_put(ep);
2847a48f923SSridhar Samudrala 
2852babf9daSHerbert Xu 	return 0;
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds discard_it:
288b01a2407SEric W. Biederman 	SCTP_INC_STATS_BH(net, SCTP_MIB_IN_PKT_DISCARDS);
2891da177e4SLinus Torvalds 	kfree_skb(skb);
2902babf9daSHerbert Xu 	return 0;
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds discard_release:
29361c9fed4SVladislav Yasevich 	/* Release the asoc/ep ref we took in the lookup calls. */
2940fd9a65aSNeil Horman 	if (asoc)
2951da177e4SLinus Torvalds 		sctp_association_put(asoc);
2960fd9a65aSNeil Horman 	else
2971da177e4SLinus Torvalds 		sctp_endpoint_put(ep);
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 	goto discard_it;
3001da177e4SLinus Torvalds }
3011da177e4SLinus Torvalds 
30261c9fed4SVladislav Yasevich /* Process the backlog queue of the socket.  Every skb on
30361c9fed4SVladislav Yasevich  * the backlog holds a ref on an association or endpoint.
30461c9fed4SVladislav Yasevich  * We hold this ref throughout the state machine to make
30561c9fed4SVladislav Yasevich  * sure that the structure we need is still around.
3061da177e4SLinus Torvalds  */
3071da177e4SLinus Torvalds int sctp_backlog_rcv(struct sock *sk, struct sk_buff *skb)
3081da177e4SLinus Torvalds {
30979af02c2SDavid S. Miller 	struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
31061c9fed4SVladislav Yasevich 	struct sctp_inq *inqueue = &chunk->rcvr->inqueue;
3117a48f923SSridhar Samudrala 	struct sctp_ep_common *rcvr = NULL;
31261c9fed4SVladislav Yasevich 	int backloged = 0;
3131da177e4SLinus Torvalds 
3147a48f923SSridhar Samudrala 	rcvr = chunk->rcvr;
315c4d2444eSSridhar Samudrala 
31661c9fed4SVladislav Yasevich 	/* If the rcvr is dead then the association or endpoint
31761c9fed4SVladislav Yasevich 	 * has been deleted and we can safely drop the chunk
31861c9fed4SVladislav Yasevich 	 * and refs that we are holding.
31961c9fed4SVladislav Yasevich 	 */
3207a48f923SSridhar Samudrala 	if (rcvr->dead) {
3217a48f923SSridhar Samudrala 		sctp_chunk_free(chunk);
32261c9fed4SVladislav Yasevich 		goto done;
32361c9fed4SVladislav Yasevich 	}
32461c9fed4SVladislav Yasevich 
32561c9fed4SVladislav Yasevich 	if (unlikely(rcvr->sk != sk)) {
32661c9fed4SVladislav Yasevich 		/* In this case, the association moved from one socket to
32761c9fed4SVladislav Yasevich 		 * another.  We are currently sitting on the backlog of the
32861c9fed4SVladislav Yasevich 		 * old socket, so we need to move.
32961c9fed4SVladislav Yasevich 		 * However, since we are here in the process context we
33061c9fed4SVladislav Yasevich 		 * need to take make sure that the user doesn't own
33161c9fed4SVladislav Yasevich 		 * the new socket when we process the packet.
33261c9fed4SVladislav Yasevich 		 * If the new socket is user-owned, queue the chunk to the
33361c9fed4SVladislav Yasevich 		 * backlog of the new socket without dropping any refs.
33461c9fed4SVladislav Yasevich 		 * Otherwise, we can safely push the chunk on the inqueue.
33561c9fed4SVladislav Yasevich 		 */
33661c9fed4SVladislav Yasevich 
33761c9fed4SVladislav Yasevich 		sk = rcvr->sk;
33861c9fed4SVladislav Yasevich 		sctp_bh_lock_sock(sk);
33961c9fed4SVladislav Yasevich 
34061c9fed4SVladislav Yasevich 		if (sock_owned_by_user(sk)) {
341f545a38fSEric Dumazet 			if (sk_add_backlog(sk, skb, sk->sk_rcvbuf))
34250b1a782SZhu Yi 				sctp_chunk_free(chunk);
34350b1a782SZhu Yi 			else
34461c9fed4SVladislav Yasevich 				backloged = 1;
34561c9fed4SVladislav Yasevich 		} else
34661c9fed4SVladislav Yasevich 			sctp_inq_push(inqueue, chunk);
34761c9fed4SVladislav Yasevich 
34861c9fed4SVladislav Yasevich 		sctp_bh_unlock_sock(sk);
34961c9fed4SVladislav Yasevich 
35061c9fed4SVladislav Yasevich 		/* If the chunk was backloged again, don't drop refs */
35161c9fed4SVladislav Yasevich 		if (backloged)
35261c9fed4SVladislav Yasevich 			return 0;
3537a48f923SSridhar Samudrala 	} else {
3541da177e4SLinus Torvalds 		sctp_inq_push(inqueue, chunk);
3557a48f923SSridhar Samudrala 	}
3567a48f923SSridhar Samudrala 
35761c9fed4SVladislav Yasevich done:
35861c9fed4SVladislav Yasevich 	/* Release the refs we took in sctp_add_backlog */
3597a48f923SSridhar Samudrala 	if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
3607a48f923SSridhar Samudrala 		sctp_association_put(sctp_assoc(rcvr));
36161c9fed4SVladislav Yasevich 	else if (SCTP_EP_TYPE_SOCKET == rcvr->type)
3627a48f923SSridhar Samudrala 		sctp_endpoint_put(sctp_ep(rcvr));
36361c9fed4SVladislav Yasevich 	else
36461c9fed4SVladislav Yasevich 		BUG();
3657a48f923SSridhar Samudrala 
3661da177e4SLinus Torvalds 	return 0;
3671da177e4SLinus Torvalds }
3681da177e4SLinus Torvalds 
36950b1a782SZhu Yi static int sctp_add_backlog(struct sock *sk, struct sk_buff *skb)
370c4d2444eSSridhar Samudrala {
37161c9fed4SVladislav Yasevich 	struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
37261c9fed4SVladislav Yasevich 	struct sctp_ep_common *rcvr = chunk->rcvr;
37350b1a782SZhu Yi 	int ret;
374c4d2444eSSridhar Samudrala 
375f545a38fSEric Dumazet 	ret = sk_add_backlog(sk, skb, sk->sk_rcvbuf);
37650b1a782SZhu Yi 	if (!ret) {
37761c9fed4SVladislav Yasevich 		/* Hold the assoc/ep while hanging on the backlog queue.
37850b1a782SZhu Yi 		 * This way, we know structures we need will not disappear
37950b1a782SZhu Yi 		 * from us
38061c9fed4SVladislav Yasevich 		 */
38161c9fed4SVladislav Yasevich 		if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
38261c9fed4SVladislav Yasevich 			sctp_association_hold(sctp_assoc(rcvr));
38361c9fed4SVladislav Yasevich 		else if (SCTP_EP_TYPE_SOCKET == rcvr->type)
38461c9fed4SVladislav Yasevich 			sctp_endpoint_hold(sctp_ep(rcvr));
385c4d2444eSSridhar Samudrala 		else
38661c9fed4SVladislav Yasevich 			BUG();
38750b1a782SZhu Yi 	}
38850b1a782SZhu Yi 	return ret;
38961c9fed4SVladislav Yasevich 
390c4d2444eSSridhar Samudrala }
391c4d2444eSSridhar Samudrala 
3921da177e4SLinus Torvalds /* Handle icmp frag needed error. */
3931da177e4SLinus Torvalds void sctp_icmp_frag_needed(struct sock *sk, struct sctp_association *asoc,
3941da177e4SLinus Torvalds 			   struct sctp_transport *t, __u32 pmtu)
3951da177e4SLinus Torvalds {
39691bd6b1eSWei Yongjun 	if (!t || (t->pathmtu <= pmtu))
39752ccb8e9SFrank Filz 		return;
39852ccb8e9SFrank Filz 
3998a479491SVlad Yasevich 	if (sock_owned_by_user(sk)) {
4008a479491SVlad Yasevich 		asoc->pmtu_pending = 1;
4018a479491SVlad Yasevich 		t->pmtu_pending = 1;
4028a479491SVlad Yasevich 		return;
4038a479491SVlad Yasevich 	}
4048a479491SVlad Yasevich 
40552ccb8e9SFrank Filz 	if (t->param_flags & SPP_PMTUD_ENABLE) {
406c910b47eSVlad Yasevich 		/* Update transports view of the MTU */
40702f3d4ceSDavid S. Miller 		sctp_transport_update_pmtu(sk, t, pmtu);
4081da177e4SLinus Torvalds 
40952ccb8e9SFrank Filz 		/* Update association pmtu. */
41002f3d4ceSDavid S. Miller 		sctp_assoc_sync_pmtu(sk, asoc);
4111da177e4SLinus Torvalds 	}
41252ccb8e9SFrank Filz 
41352ccb8e9SFrank Filz 	/* Retransmit with the new pmtu setting.
41452ccb8e9SFrank Filz 	 * Normally, if PMTU discovery is disabled, an ICMP Fragmentation
41552ccb8e9SFrank Filz 	 * Needed will never be sent, but if a message was sent before
41652ccb8e9SFrank Filz 	 * PMTU discovery was disabled that was larger than the PMTU, it
41752ccb8e9SFrank Filz 	 * would not be fragmented, so it must be re-transmitted fragmented.
41852ccb8e9SFrank Filz 	 */
41952ccb8e9SFrank Filz 	sctp_retransmit(&asoc->outqueue, t, SCTP_RTXR_PMTUD);
4201da177e4SLinus Torvalds }
4211da177e4SLinus Torvalds 
422ec18d9a2SDavid S. Miller void sctp_icmp_redirect(struct sock *sk, struct sctp_transport *t,
42355be7a9cSDavid S. Miller 			struct sk_buff *skb)
42455be7a9cSDavid S. Miller {
42555be7a9cSDavid S. Miller 	struct dst_entry *dst;
42655be7a9cSDavid S. Miller 
42755be7a9cSDavid S. Miller 	if (!t)
42855be7a9cSDavid S. Miller 		return;
42955be7a9cSDavid S. Miller 	dst = sctp_transport_dst_check(t);
4301ed5c48fSDavid S. Miller 	if (dst)
4316700c270SDavid S. Miller 		dst->ops->redirect(dst, sk, skb);
43255be7a9cSDavid S. Miller }
43355be7a9cSDavid S. Miller 
4341da177e4SLinus Torvalds /*
4351da177e4SLinus Torvalds  * SCTP Implementer's Guide, 2.37 ICMP handling procedures
4361da177e4SLinus Torvalds  *
4371da177e4SLinus Torvalds  * ICMP8) If the ICMP code is a "Unrecognized next header type encountered"
4381da177e4SLinus Torvalds  *        or a "Protocol Unreachable" treat this message as an abort
4391da177e4SLinus Torvalds  *        with the T bit set.
4401da177e4SLinus Torvalds  *
4411da177e4SLinus Torvalds  * This function sends an event to the state machine, which will abort the
4421da177e4SLinus Torvalds  * association.
4431da177e4SLinus Torvalds  *
4441da177e4SLinus Torvalds  */
4451da177e4SLinus Torvalds void sctp_icmp_proto_unreachable(struct sock *sk,
4461da177e4SLinus Torvalds 			   struct sctp_association *asoc,
4471da177e4SLinus Torvalds 			   struct sctp_transport *t)
4481da177e4SLinus Torvalds {
44950b5d6adSVlad Yasevich 	if (sock_owned_by_user(sk)) {
45050b5d6adSVlad Yasevich 		if (timer_pending(&t->proto_unreach_timer))
45150b5d6adSVlad Yasevich 			return;
45250b5d6adSVlad Yasevich 		else {
45350b5d6adSVlad Yasevich 			if (!mod_timer(&t->proto_unreach_timer,
45450b5d6adSVlad Yasevich 						jiffies + (HZ/20)))
45550b5d6adSVlad Yasevich 				sctp_association_hold(asoc);
45650b5d6adSVlad Yasevich 		}
45750b5d6adSVlad Yasevich 	} else {
45855e26eb9SEric W. Biederman 		struct net *net = sock_net(sk);
45955e26eb9SEric W. Biederman 
460bb33381dSDaniel Borkmann 		pr_debug("%s: unrecognized next header type "
461bb33381dSDaniel Borkmann 			 "encountered!\n", __func__);
462bb33381dSDaniel Borkmann 
46325cc4ae9SYing Xue 		if (del_timer(&t->proto_unreach_timer))
46450b5d6adSVlad Yasevich 			sctp_association_put(asoc);
46550b5d6adSVlad Yasevich 
46655e26eb9SEric W. Biederman 		sctp_do_sm(net, SCTP_EVENT_T_OTHER,
4671da177e4SLinus Torvalds 			   SCTP_ST_OTHER(SCTP_EVENT_ICMP_PROTO_UNREACH),
4683f7a87d2SFrank Filz 			   asoc->state, asoc->ep, asoc, t,
4691da177e4SLinus Torvalds 			   GFP_ATOMIC);
47050b5d6adSVlad Yasevich 	}
4711da177e4SLinus Torvalds }
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds /* Common lookup code for icmp/icmpv6 error handler. */
4744110cc25SEric W. Biederman struct sock *sctp_err_lookup(struct net *net, int family, struct sk_buff *skb,
4751da177e4SLinus Torvalds 			     struct sctphdr *sctphdr,
4761da177e4SLinus Torvalds 			     struct sctp_association **app,
4771da177e4SLinus Torvalds 			     struct sctp_transport **tpp)
4781da177e4SLinus Torvalds {
4791da177e4SLinus Torvalds 	union sctp_addr saddr;
4801da177e4SLinus Torvalds 	union sctp_addr daddr;
4811da177e4SLinus Torvalds 	struct sctp_af *af;
4821da177e4SLinus Torvalds 	struct sock *sk = NULL;
4838de8c873SSridhar Samudrala 	struct sctp_association *asoc;
4841da177e4SLinus Torvalds 	struct sctp_transport *transport = NULL;
4857115e632SWei Yongjun 	struct sctp_init_chunk *chunkhdr;
4867115e632SWei Yongjun 	__u32 vtag = ntohl(sctphdr->vtag);
4877115e632SWei Yongjun 	int len = skb->len - ((void *)sctphdr - (void *)skb->data);
4881da177e4SLinus Torvalds 
489d1ad1ff2SSridhar Samudrala 	*app = NULL; *tpp = NULL;
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds 	af = sctp_get_af_specific(family);
4921da177e4SLinus Torvalds 	if (unlikely(!af)) {
4931da177e4SLinus Torvalds 		return NULL;
4941da177e4SLinus Torvalds 	}
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds 	/* Initialize local addresses for lookups. */
4971da177e4SLinus Torvalds 	af->from_skb(&saddr, skb, 1);
4981da177e4SLinus Torvalds 	af->from_skb(&daddr, skb, 0);
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds 	/* Look for an association that matches the incoming ICMP error
5011da177e4SLinus Torvalds 	 * packet.
5021da177e4SLinus Torvalds 	 */
5034110cc25SEric W. Biederman 	asoc = __sctp_lookup_association(net, &saddr, &daddr, &transport);
504d1ad1ff2SSridhar Samudrala 	if (!asoc)
5051da177e4SLinus Torvalds 		return NULL;
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds 	sk = asoc->base.sk;
5081da177e4SLinus Torvalds 
5097115e632SWei Yongjun 	/* RFC 4960, Appendix C. ICMP Handling
5107115e632SWei Yongjun 	 *
5117115e632SWei Yongjun 	 * ICMP6) An implementation MUST validate that the Verification Tag
5127115e632SWei Yongjun 	 * contained in the ICMP message matches the Verification Tag of
5137115e632SWei Yongjun 	 * the peer.  If the Verification Tag is not 0 and does NOT
5147115e632SWei Yongjun 	 * match, discard the ICMP message.  If it is 0 and the ICMP
5157115e632SWei Yongjun 	 * message contains enough bytes to verify that the chunk type is
5167115e632SWei Yongjun 	 * an INIT chunk and that the Initiate Tag matches the tag of the
5177115e632SWei Yongjun 	 * peer, continue with ICMP7.  If the ICMP message is too short
5187115e632SWei Yongjun 	 * or the chunk type or the Initiate Tag does not match, silently
5197115e632SWei Yongjun 	 * discard the packet.
5207115e632SWei Yongjun 	 */
5217115e632SWei Yongjun 	if (vtag == 0) {
522ea110733SJoe Perches 		chunkhdr = (void *)sctphdr + sizeof(struct sctphdr);
5237115e632SWei Yongjun 		if (len < sizeof(struct sctphdr) + sizeof(sctp_chunkhdr_t)
5247115e632SWei Yongjun 			  + sizeof(__be32) ||
5257115e632SWei Yongjun 		    chunkhdr->chunk_hdr.type != SCTP_CID_INIT ||
5267115e632SWei Yongjun 		    ntohl(chunkhdr->init_hdr.init_tag) != asoc->c.my_vtag) {
5277115e632SWei Yongjun 			goto out;
5287115e632SWei Yongjun 		}
5297115e632SWei Yongjun 	} else if (vtag != asoc->c.peer_vtag) {
5301da177e4SLinus Torvalds 		goto out;
5311da177e4SLinus Torvalds 	}
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds 	sctp_bh_lock_sock(sk);
5341da177e4SLinus Torvalds 
5351da177e4SLinus Torvalds 	/* If too many ICMPs get dropped on busy
5361da177e4SLinus Torvalds 	 * servers this needs to be solved differently.
5371da177e4SLinus Torvalds 	 */
5381da177e4SLinus Torvalds 	if (sock_owned_by_user(sk))
539b01a2407SEric W. Biederman 		NET_INC_STATS_BH(net, LINUX_MIB_LOCKDROPPEDICMPS);
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds 	*app = asoc;
5421da177e4SLinus Torvalds 	*tpp = transport;
5431da177e4SLinus Torvalds 	return sk;
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds out:
5461da177e4SLinus Torvalds 	if (asoc)
5471da177e4SLinus Torvalds 		sctp_association_put(asoc);
5481da177e4SLinus Torvalds 	return NULL;
5491da177e4SLinus Torvalds }
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds /* Common cleanup code for icmp/icmpv6 error handler. */
552d1ad1ff2SSridhar Samudrala void sctp_err_finish(struct sock *sk, struct sctp_association *asoc)
5531da177e4SLinus Torvalds {
5541da177e4SLinus Torvalds 	sctp_bh_unlock_sock(sk);
5551da177e4SLinus Torvalds 	if (asoc)
5561da177e4SLinus Torvalds 		sctp_association_put(asoc);
5571da177e4SLinus Torvalds }
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds /*
5601da177e4SLinus Torvalds  * This routine is called by the ICMP module when it gets some
5611da177e4SLinus Torvalds  * sort of error condition.  If err < 0 then the socket should
5621da177e4SLinus Torvalds  * be closed and the error returned to the user.  If err > 0
5631da177e4SLinus Torvalds  * it's just the icmp type << 8 | icmp code.  After adjustment
5641da177e4SLinus Torvalds  * header points to the first 8 bytes of the sctp header.  We need
5651da177e4SLinus Torvalds  * to find the appropriate port.
5661da177e4SLinus Torvalds  *
5671da177e4SLinus Torvalds  * The locking strategy used here is very "optimistic". When
5681da177e4SLinus Torvalds  * someone else accesses the socket the ICMP is just dropped
5691da177e4SLinus Torvalds  * and for some paths there is no check at all.
5701da177e4SLinus Torvalds  * A more general error queue to queue errors for later handling
5711da177e4SLinus Torvalds  * is probably better.
5721da177e4SLinus Torvalds  *
5731da177e4SLinus Torvalds  */
5741da177e4SLinus Torvalds void sctp_v4_err(struct sk_buff *skb, __u32 info)
5751da177e4SLinus Torvalds {
576b71d1d42SEric Dumazet 	const struct iphdr *iph = (const struct iphdr *)skb->data;
577a27ef749SArnaldo Carvalho de Melo 	const int ihlen = iph->ihl * 4;
57888c7664fSArnaldo Carvalho de Melo 	const int type = icmp_hdr(skb)->type;
57988c7664fSArnaldo Carvalho de Melo 	const int code = icmp_hdr(skb)->code;
5801da177e4SLinus Torvalds 	struct sock *sk;
5818de8c873SSridhar Samudrala 	struct sctp_association *asoc = NULL;
5821da177e4SLinus Torvalds 	struct sctp_transport *transport;
5831da177e4SLinus Torvalds 	struct inet_sock *inet;
584aef6de51SSimon Horman 	__u16 saveip, savesctp;
5851da177e4SLinus Torvalds 	int err;
5864110cc25SEric W. Biederman 	struct net *net = dev_net(skb->dev);
5871da177e4SLinus Torvalds 
588a27ef749SArnaldo Carvalho de Melo 	if (skb->len < ihlen + 8) {
589b01a2407SEric W. Biederman 		ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
5901da177e4SLinus Torvalds 		return;
5911da177e4SLinus Torvalds 	}
5921da177e4SLinus Torvalds 
5931da177e4SLinus Torvalds 	/* Fix up skb to look at the embedded net header. */
594b0e380b1SArnaldo Carvalho de Melo 	saveip = skb->network_header;
595b0e380b1SArnaldo Carvalho de Melo 	savesctp = skb->transport_header;
59631c7711bSArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
597a27ef749SArnaldo Carvalho de Melo 	skb_set_transport_header(skb, ihlen);
5984110cc25SEric W. Biederman 	sk = sctp_err_lookup(net, AF_INET, skb, sctp_hdr(skb), &asoc, &transport);
5992e07fa9cSArnaldo Carvalho de Melo 	/* Put back, the original values. */
600b0e380b1SArnaldo Carvalho de Melo 	skb->network_header = saveip;
601b0e380b1SArnaldo Carvalho de Melo 	skb->transport_header = savesctp;
6021da177e4SLinus Torvalds 	if (!sk) {
603b01a2407SEric W. Biederman 		ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
6041da177e4SLinus Torvalds 		return;
6051da177e4SLinus Torvalds 	}
6061da177e4SLinus Torvalds 	/* Warning:  The sock lock is held.  Remember to call
6071da177e4SLinus Torvalds 	 * sctp_err_finish!
6081da177e4SLinus Torvalds 	 */
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds 	switch (type) {
6111da177e4SLinus Torvalds 	case ICMP_PARAMETERPROB:
6121da177e4SLinus Torvalds 		err = EPROTO;
6131da177e4SLinus Torvalds 		break;
6141da177e4SLinus Torvalds 	case ICMP_DEST_UNREACH:
6151da177e4SLinus Torvalds 		if (code > NR_ICMP_UNREACH)
6161da177e4SLinus Torvalds 			goto out_unlock;
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds 		/* PMTU discovery (RFC1191) */
6191da177e4SLinus Torvalds 		if (ICMP_FRAG_NEEDED == code) {
6201da177e4SLinus Torvalds 			sctp_icmp_frag_needed(sk, asoc, transport, info);
6211da177e4SLinus Torvalds 			goto out_unlock;
6221da177e4SLinus Torvalds 		}
6231da177e4SLinus Torvalds 		else {
6241da177e4SLinus Torvalds 			if (ICMP_PROT_UNREACH == code) {
625d1ad1ff2SSridhar Samudrala 				sctp_icmp_proto_unreachable(sk, asoc,
6261da177e4SLinus Torvalds 							    transport);
6271da177e4SLinus Torvalds 				goto out_unlock;
6281da177e4SLinus Torvalds 			}
6291da177e4SLinus Torvalds 		}
6301da177e4SLinus Torvalds 		err = icmp_err_convert[code].errno;
6311da177e4SLinus Torvalds 		break;
6321da177e4SLinus Torvalds 	case ICMP_TIME_EXCEEDED:
6331da177e4SLinus Torvalds 		/* Ignore any time exceeded errors due to fragment reassembly
6341da177e4SLinus Torvalds 		 * timeouts.
6351da177e4SLinus Torvalds 		 */
6361da177e4SLinus Torvalds 		if (ICMP_EXC_FRAGTIME == code)
6371da177e4SLinus Torvalds 			goto out_unlock;
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds 		err = EHOSTUNREACH;
6401da177e4SLinus Torvalds 		break;
64155be7a9cSDavid S. Miller 	case ICMP_REDIRECT:
64255be7a9cSDavid S. Miller 		sctp_icmp_redirect(sk, transport, skb);
64355be7a9cSDavid S. Miller 		err = 0;
64455be7a9cSDavid S. Miller 		break;
6451da177e4SLinus Torvalds 	default:
6461da177e4SLinus Torvalds 		goto out_unlock;
6471da177e4SLinus Torvalds 	}
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds 	inet = inet_sk(sk);
6501da177e4SLinus Torvalds 	if (!sock_owned_by_user(sk) && inet->recverr) {
6511da177e4SLinus Torvalds 		sk->sk_err = err;
6521da177e4SLinus Torvalds 		sk->sk_error_report(sk);
6531da177e4SLinus Torvalds 	} else {  /* Only an error on timeout */
6541da177e4SLinus Torvalds 		sk->sk_err_soft = err;
6551da177e4SLinus Torvalds 	}
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds out_unlock:
658d1ad1ff2SSridhar Samudrala 	sctp_err_finish(sk, asoc);
6591da177e4SLinus Torvalds }
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds /*
6621da177e4SLinus Torvalds  * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
6631da177e4SLinus Torvalds  *
6641da177e4SLinus Torvalds  * This function scans all the chunks in the OOTB packet to determine if
6651da177e4SLinus Torvalds  * the packet should be discarded right away.  If a response might be needed
6661da177e4SLinus Torvalds  * for this packet, or, if further processing is possible, the packet will
6671da177e4SLinus Torvalds  * be queued to a proper inqueue for the next phase of handling.
6681da177e4SLinus Torvalds  *
6691da177e4SLinus Torvalds  * Output:
6701da177e4SLinus Torvalds  * Return 0 - If further processing is needed.
6711da177e4SLinus Torvalds  * Return 1 - If the packet can be discarded right away.
6721da177e4SLinus Torvalds  */
67304675210Ssebastian@breakpoint.cc static int sctp_rcv_ootb(struct sk_buff *skb)
6741da177e4SLinus Torvalds {
6751da177e4SLinus Torvalds 	sctp_chunkhdr_t *ch;
6761da177e4SLinus Torvalds 	__u8 *ch_end;
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds 	ch = (sctp_chunkhdr_t *) skb->data;
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds 	/* Scan through all the chunks in the packet.  */
681a7d1f1b6STsutomu Fujii 	do {
682a7d1f1b6STsutomu Fujii 		/* Break out if chunk length is less then minimal. */
683a7d1f1b6STsutomu Fujii 		if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
684a7d1f1b6STsutomu Fujii 			break;
685a7d1f1b6STsutomu Fujii 
686a7d1f1b6STsutomu Fujii 		ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
68727a884dcSArnaldo Carvalho de Melo 		if (ch_end > skb_tail_pointer(skb))
688a7d1f1b6STsutomu Fujii 			break;
6891da177e4SLinus Torvalds 
6901da177e4SLinus Torvalds 		/* RFC 8.4, 2) If the OOTB packet contains an ABORT chunk, the
6911da177e4SLinus Torvalds 		 * receiver MUST silently discard the OOTB packet and take no
6921da177e4SLinus Torvalds 		 * further action.
6931da177e4SLinus Torvalds 		 */
6941da177e4SLinus Torvalds 		if (SCTP_CID_ABORT == ch->type)
6951da177e4SLinus Torvalds 			goto discard;
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds 		/* RFC 8.4, 6) If the packet contains a SHUTDOWN COMPLETE
6981da177e4SLinus Torvalds 		 * chunk, the receiver should silently discard the packet
6991da177e4SLinus Torvalds 		 * and take no further action.
7001da177e4SLinus Torvalds 		 */
7011da177e4SLinus Torvalds 		if (SCTP_CID_SHUTDOWN_COMPLETE == ch->type)
7021da177e4SLinus Torvalds 			goto discard;
7031da177e4SLinus Torvalds 
7043c77f961SVlad Yasevich 		/* RFC 4460, 2.11.2
7053c77f961SVlad Yasevich 		 * This will discard packets with INIT chunk bundled as
7063c77f961SVlad Yasevich 		 * subsequent chunks in the packet.  When INIT is first,
7073c77f961SVlad Yasevich 		 * the normal INIT processing will discard the chunk.
7083c77f961SVlad Yasevich 		 */
7093c77f961SVlad Yasevich 		if (SCTP_CID_INIT == ch->type && (void *)ch != skb->data)
7103c77f961SVlad Yasevich 			goto discard;
7113c77f961SVlad Yasevich 
7121da177e4SLinus Torvalds 		ch = (sctp_chunkhdr_t *) ch_end;
71327a884dcSArnaldo Carvalho de Melo 	} while (ch_end < skb_tail_pointer(skb));
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds 	return 0;
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds discard:
7181da177e4SLinus Torvalds 	return 1;
7191da177e4SLinus Torvalds }
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds /* Insert endpoint into the hash table.  */
7221da177e4SLinus Torvalds static void __sctp_hash_endpoint(struct sctp_endpoint *ep)
7231da177e4SLinus Torvalds {
7244cdadcbcSEric W. Biederman 	struct net *net = sock_net(ep->base.sk);
7251da177e4SLinus Torvalds 	struct sctp_ep_common *epb;
7261da177e4SLinus Torvalds 	struct sctp_hashbucket *head;
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds 	epb = &ep->base;
7291da177e4SLinus Torvalds 
7304cdadcbcSEric W. Biederman 	epb->hashent = sctp_ep_hashfn(net, epb->bind_addr.port);
7311da177e4SLinus Torvalds 	head = &sctp_ep_hashtable[epb->hashent];
7321da177e4SLinus Torvalds 
7331da177e4SLinus Torvalds 	sctp_write_lock(&head->lock);
734d970dbf8SVlad Yasevich 	hlist_add_head(&epb->node, &head->chain);
7351da177e4SLinus Torvalds 	sctp_write_unlock(&head->lock);
7361da177e4SLinus Torvalds }
7371da177e4SLinus Torvalds 
7381da177e4SLinus Torvalds /* Add an endpoint to the hash. Local BH-safe. */
7391da177e4SLinus Torvalds void sctp_hash_endpoint(struct sctp_endpoint *ep)
7401da177e4SLinus Torvalds {
7411da177e4SLinus Torvalds 	sctp_local_bh_disable();
7421da177e4SLinus Torvalds 	__sctp_hash_endpoint(ep);
7431da177e4SLinus Torvalds 	sctp_local_bh_enable();
7441da177e4SLinus Torvalds }
7451da177e4SLinus Torvalds 
7461da177e4SLinus Torvalds /* Remove endpoint from the hash table.  */
7471da177e4SLinus Torvalds static void __sctp_unhash_endpoint(struct sctp_endpoint *ep)
7481da177e4SLinus Torvalds {
7494cdadcbcSEric W. Biederman 	struct net *net = sock_net(ep->base.sk);
7501da177e4SLinus Torvalds 	struct sctp_hashbucket *head;
7511da177e4SLinus Torvalds 	struct sctp_ep_common *epb;
7521da177e4SLinus Torvalds 
7531da177e4SLinus Torvalds 	epb = &ep->base;
7541da177e4SLinus Torvalds 
7554cdadcbcSEric W. Biederman 	epb->hashent = sctp_ep_hashfn(net, epb->bind_addr.port);
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds 	head = &sctp_ep_hashtable[epb->hashent];
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds 	sctp_write_lock(&head->lock);
7602eebc1e1SNeil Horman 	hlist_del_init(&epb->node);
7611da177e4SLinus Torvalds 	sctp_write_unlock(&head->lock);
7621da177e4SLinus Torvalds }
7631da177e4SLinus Torvalds 
7641da177e4SLinus Torvalds /* Remove endpoint from the hash.  Local BH-safe. */
7651da177e4SLinus Torvalds void sctp_unhash_endpoint(struct sctp_endpoint *ep)
7661da177e4SLinus Torvalds {
7671da177e4SLinus Torvalds 	sctp_local_bh_disable();
7681da177e4SLinus Torvalds 	__sctp_unhash_endpoint(ep);
7691da177e4SLinus Torvalds 	sctp_local_bh_enable();
7701da177e4SLinus Torvalds }
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds /* Look up an endpoint. */
7734cdadcbcSEric W. Biederman static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(struct net *net,
7744cdadcbcSEric W. Biederman 						const union sctp_addr *laddr)
7751da177e4SLinus Torvalds {
7761da177e4SLinus Torvalds 	struct sctp_hashbucket *head;
7771da177e4SLinus Torvalds 	struct sctp_ep_common *epb;
7781da177e4SLinus Torvalds 	struct sctp_endpoint *ep;
7791da177e4SLinus Torvalds 	int hash;
7801da177e4SLinus Torvalds 
7814cdadcbcSEric W. Biederman 	hash = sctp_ep_hashfn(net, ntohs(laddr->v4.sin_port));
7821da177e4SLinus Torvalds 	head = &sctp_ep_hashtable[hash];
7831da177e4SLinus Torvalds 	read_lock(&head->lock);
784b67bfe0dSSasha Levin 	sctp_for_each_hentry(epb, &head->chain) {
7851da177e4SLinus Torvalds 		ep = sctp_ep(epb);
7864cdadcbcSEric W. Biederman 		if (sctp_endpoint_is_match(ep, net, laddr))
7871da177e4SLinus Torvalds 			goto hit;
7881da177e4SLinus Torvalds 	}
7891da177e4SLinus Torvalds 
7902ce95503SEric W. Biederman 	ep = sctp_sk(net->sctp.ctl_sock)->ep;
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds hit:
7931da177e4SLinus Torvalds 	sctp_endpoint_hold(ep);
7941da177e4SLinus Torvalds 	read_unlock(&head->lock);
7951da177e4SLinus Torvalds 	return ep;
7961da177e4SLinus Torvalds }
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds /* Insert association into the hash table.  */
7991da177e4SLinus Torvalds static void __sctp_hash_established(struct sctp_association *asoc)
8001da177e4SLinus Torvalds {
8014110cc25SEric W. Biederman 	struct net *net = sock_net(asoc->base.sk);
8021da177e4SLinus Torvalds 	struct sctp_ep_common *epb;
8031da177e4SLinus Torvalds 	struct sctp_hashbucket *head;
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds 	epb = &asoc->base;
8061da177e4SLinus Torvalds 
8071da177e4SLinus Torvalds 	/* Calculate which chain this entry will belong to. */
8084110cc25SEric W. Biederman 	epb->hashent = sctp_assoc_hashfn(net, epb->bind_addr.port,
8094110cc25SEric W. Biederman 					 asoc->peer.port);
8101da177e4SLinus Torvalds 
8111da177e4SLinus Torvalds 	head = &sctp_assoc_hashtable[epb->hashent];
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds 	sctp_write_lock(&head->lock);
814d970dbf8SVlad Yasevich 	hlist_add_head(&epb->node, &head->chain);
8151da177e4SLinus Torvalds 	sctp_write_unlock(&head->lock);
8161da177e4SLinus Torvalds }
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds /* Add an association to the hash. Local BH-safe. */
8191da177e4SLinus Torvalds void sctp_hash_established(struct sctp_association *asoc)
8201da177e4SLinus Torvalds {
821de76e695SVlad Yasevich 	if (asoc->temp)
822de76e695SVlad Yasevich 		return;
823de76e695SVlad Yasevich 
8241da177e4SLinus Torvalds 	sctp_local_bh_disable();
8251da177e4SLinus Torvalds 	__sctp_hash_established(asoc);
8261da177e4SLinus Torvalds 	sctp_local_bh_enable();
8271da177e4SLinus Torvalds }
8281da177e4SLinus Torvalds 
8291da177e4SLinus Torvalds /* Remove association from the hash table.  */
8301da177e4SLinus Torvalds static void __sctp_unhash_established(struct sctp_association *asoc)
8311da177e4SLinus Torvalds {
8324110cc25SEric W. Biederman 	struct net *net = sock_net(asoc->base.sk);
8331da177e4SLinus Torvalds 	struct sctp_hashbucket *head;
8341da177e4SLinus Torvalds 	struct sctp_ep_common *epb;
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds 	epb = &asoc->base;
8371da177e4SLinus Torvalds 
8384110cc25SEric W. Biederman 	epb->hashent = sctp_assoc_hashfn(net, epb->bind_addr.port,
8391da177e4SLinus Torvalds 					 asoc->peer.port);
8401da177e4SLinus Torvalds 
8411da177e4SLinus Torvalds 	head = &sctp_assoc_hashtable[epb->hashent];
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds 	sctp_write_lock(&head->lock);
8442eebc1e1SNeil Horman 	hlist_del_init(&epb->node);
8451da177e4SLinus Torvalds 	sctp_write_unlock(&head->lock);
8461da177e4SLinus Torvalds }
8471da177e4SLinus Torvalds 
8481da177e4SLinus Torvalds /* Remove association from the hash table.  Local BH-safe. */
8491da177e4SLinus Torvalds void sctp_unhash_established(struct sctp_association *asoc)
8501da177e4SLinus Torvalds {
851de76e695SVlad Yasevich 	if (asoc->temp)
852de76e695SVlad Yasevich 		return;
853de76e695SVlad Yasevich 
8541da177e4SLinus Torvalds 	sctp_local_bh_disable();
8551da177e4SLinus Torvalds 	__sctp_unhash_established(asoc);
8561da177e4SLinus Torvalds 	sctp_local_bh_enable();
8571da177e4SLinus Torvalds }
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds /* Look up an association. */
8601da177e4SLinus Torvalds static struct sctp_association *__sctp_lookup_association(
8614110cc25SEric W. Biederman 					struct net *net,
8621da177e4SLinus Torvalds 					const union sctp_addr *local,
8631da177e4SLinus Torvalds 					const union sctp_addr *peer,
8641da177e4SLinus Torvalds 					struct sctp_transport **pt)
8651da177e4SLinus Torvalds {
8661da177e4SLinus Torvalds 	struct sctp_hashbucket *head;
8671da177e4SLinus Torvalds 	struct sctp_ep_common *epb;
8681da177e4SLinus Torvalds 	struct sctp_association *asoc;
8691da177e4SLinus Torvalds 	struct sctp_transport *transport;
8701da177e4SLinus Torvalds 	int hash;
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds 	/* Optimize here for direct hit, only listening connections can
8731da177e4SLinus Torvalds 	 * have wildcards anyways.
8741da177e4SLinus Torvalds 	 */
8754110cc25SEric W. Biederman 	hash = sctp_assoc_hashfn(net, ntohs(local->v4.sin_port),
8764110cc25SEric W. Biederman 				 ntohs(peer->v4.sin_port));
8771da177e4SLinus Torvalds 	head = &sctp_assoc_hashtable[hash];
8781da177e4SLinus Torvalds 	read_lock(&head->lock);
879b67bfe0dSSasha Levin 	sctp_for_each_hentry(epb, &head->chain) {
8801da177e4SLinus Torvalds 		asoc = sctp_assoc(epb);
8814110cc25SEric W. Biederman 		transport = sctp_assoc_is_match(asoc, net, local, peer);
8821da177e4SLinus Torvalds 		if (transport)
8831da177e4SLinus Torvalds 			goto hit;
8841da177e4SLinus Torvalds 	}
8851da177e4SLinus Torvalds 
8861da177e4SLinus Torvalds 	read_unlock(&head->lock);
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds 	return NULL;
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds hit:
8911da177e4SLinus Torvalds 	*pt = transport;
8921da177e4SLinus Torvalds 	sctp_association_hold(asoc);
8931da177e4SLinus Torvalds 	read_unlock(&head->lock);
8941da177e4SLinus Torvalds 	return asoc;
8951da177e4SLinus Torvalds }
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds /* Look up an association. BH-safe. */
898dda91928SDaniel Borkmann static
8994110cc25SEric W. Biederman struct sctp_association *sctp_lookup_association(struct net *net,
9004110cc25SEric W. Biederman 						 const union sctp_addr *laddr,
9011da177e4SLinus Torvalds 						 const union sctp_addr *paddr,
9021da177e4SLinus Torvalds 						 struct sctp_transport **transportp)
9031da177e4SLinus Torvalds {
9041da177e4SLinus Torvalds 	struct sctp_association *asoc;
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds 	sctp_local_bh_disable();
9074110cc25SEric W. Biederman 	asoc = __sctp_lookup_association(net, laddr, paddr, transportp);
9081da177e4SLinus Torvalds 	sctp_local_bh_enable();
9091da177e4SLinus Torvalds 
9101da177e4SLinus Torvalds 	return asoc;
9111da177e4SLinus Torvalds }
9121da177e4SLinus Torvalds 
9131da177e4SLinus Torvalds /* Is there an association matching the given local and peer addresses? */
9144110cc25SEric W. Biederman int sctp_has_association(struct net *net,
9154110cc25SEric W. Biederman 			 const union sctp_addr *laddr,
9161da177e4SLinus Torvalds 			 const union sctp_addr *paddr)
9171da177e4SLinus Torvalds {
9181da177e4SLinus Torvalds 	struct sctp_association *asoc;
9191da177e4SLinus Torvalds 	struct sctp_transport *transport;
9201da177e4SLinus Torvalds 
9214110cc25SEric W. Biederman 	if ((asoc = sctp_lookup_association(net, laddr, paddr, &transport))) {
9221da177e4SLinus Torvalds 		sctp_association_put(asoc);
9231da177e4SLinus Torvalds 		return 1;
9241da177e4SLinus Torvalds 	}
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds 	return 0;
9271da177e4SLinus Torvalds }
9281da177e4SLinus Torvalds 
9291da177e4SLinus Torvalds /*
9301da177e4SLinus Torvalds  * SCTP Implementors Guide, 2.18 Handling of address
9311da177e4SLinus Torvalds  * parameters within the INIT or INIT-ACK.
9321da177e4SLinus Torvalds  *
9331da177e4SLinus Torvalds  * D) When searching for a matching TCB upon reception of an INIT
9341da177e4SLinus Torvalds  *    or INIT-ACK chunk the receiver SHOULD use not only the
9351da177e4SLinus Torvalds  *    source address of the packet (containing the INIT or
9361da177e4SLinus Torvalds  *    INIT-ACK) but the receiver SHOULD also use all valid
9371da177e4SLinus Torvalds  *    address parameters contained within the chunk.
9381da177e4SLinus Torvalds  *
9391da177e4SLinus Torvalds  * 2.18.3 Solution description
9401da177e4SLinus Torvalds  *
9411da177e4SLinus Torvalds  * This new text clearly specifies to an implementor the need
9421da177e4SLinus Torvalds  * to look within the INIT or INIT-ACK. Any implementation that
9431da177e4SLinus Torvalds  * does not do this, may not be able to establish associations
9441da177e4SLinus Torvalds  * in certain circumstances.
9451da177e4SLinus Torvalds  *
9461da177e4SLinus Torvalds  */
9474110cc25SEric W. Biederman static struct sctp_association *__sctp_rcv_init_lookup(struct net *net,
9484110cc25SEric W. Biederman 	struct sk_buff *skb,
9491da177e4SLinus Torvalds 	const union sctp_addr *laddr, struct sctp_transport **transportp)
9501da177e4SLinus Torvalds {
9511da177e4SLinus Torvalds 	struct sctp_association *asoc;
9521da177e4SLinus Torvalds 	union sctp_addr addr;
9531da177e4SLinus Torvalds 	union sctp_addr *paddr = &addr;
9542c0fd387SArnaldo Carvalho de Melo 	struct sctphdr *sh = sctp_hdr(skb);
9551da177e4SLinus Torvalds 	union sctp_params params;
9561da177e4SLinus Torvalds 	sctp_init_chunk_t *init;
9571da177e4SLinus Torvalds 	struct sctp_transport *transport;
9581da177e4SLinus Torvalds 	struct sctp_af *af;
9591da177e4SLinus Torvalds 
9601da177e4SLinus Torvalds 	/*
9611da177e4SLinus Torvalds 	 * This code will NOT touch anything inside the chunk--it is
9621da177e4SLinus Torvalds 	 * strictly READ-ONLY.
9631da177e4SLinus Torvalds 	 *
9641da177e4SLinus Torvalds 	 * RFC 2960 3  SCTP packet Format
9651da177e4SLinus Torvalds 	 *
9661da177e4SLinus Torvalds 	 * Multiple chunks can be bundled into one SCTP packet up to
9671da177e4SLinus Torvalds 	 * the MTU size, except for the INIT, INIT ACK, and SHUTDOWN
9681da177e4SLinus Torvalds 	 * COMPLETE chunks.  These chunks MUST NOT be bundled with any
9691da177e4SLinus Torvalds 	 * other chunk in a packet.  See Section 6.10 for more details
9701da177e4SLinus Torvalds 	 * on chunk bundling.
9711da177e4SLinus Torvalds 	 */
9721da177e4SLinus Torvalds 
9731da177e4SLinus Torvalds 	/* Find the start of the TLVs and the end of the chunk.  This is
9741da177e4SLinus Torvalds 	 * the region we search for address parameters.
9751da177e4SLinus Torvalds 	 */
9761da177e4SLinus Torvalds 	init = (sctp_init_chunk_t *)skb->data;
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds 	/* Walk the parameters looking for embedded addresses. */
9791da177e4SLinus Torvalds 	sctp_walk_params(params, init, init_hdr.params) {
9801da177e4SLinus Torvalds 
9811da177e4SLinus Torvalds 		/* Note: Ignoring hostname addresses. */
9821da177e4SLinus Torvalds 		af = sctp_get_af_specific(param_type2af(params.p->type));
9831da177e4SLinus Torvalds 		if (!af)
9841da177e4SLinus Torvalds 			continue;
9851da177e4SLinus Torvalds 
986dd86d136SAl Viro 		af->from_addr_param(paddr, params.addr, sh->source, 0);
9871da177e4SLinus Torvalds 
9884110cc25SEric W. Biederman 		asoc = __sctp_lookup_association(net, laddr, paddr, &transport);
9891da177e4SLinus Torvalds 		if (asoc)
9901da177e4SLinus Torvalds 			return asoc;
9911da177e4SLinus Torvalds 	}
9921da177e4SLinus Torvalds 
9931da177e4SLinus Torvalds 	return NULL;
9941da177e4SLinus Torvalds }
9951da177e4SLinus Torvalds 
996df218577SVlad Yasevich /* ADD-IP, Section 5.2
997df218577SVlad Yasevich  * When an endpoint receives an ASCONF Chunk from the remote peer
998df218577SVlad Yasevich  * special procedures may be needed to identify the association the
999df218577SVlad Yasevich  * ASCONF Chunk is associated with. To properly find the association
1000df218577SVlad Yasevich  * the following procedures SHOULD be followed:
1001df218577SVlad Yasevich  *
1002df218577SVlad Yasevich  * D2) If the association is not found, use the address found in the
1003df218577SVlad Yasevich  * Address Parameter TLV combined with the port number found in the
1004df218577SVlad Yasevich  * SCTP common header. If found proceed to rule D4.
1005df218577SVlad Yasevich  *
1006df218577SVlad Yasevich  * D2-ext) If more than one ASCONF Chunks are packed together, use the
1007df218577SVlad Yasevich  * address found in the ASCONF Address Parameter TLV of each of the
1008df218577SVlad Yasevich  * subsequent ASCONF Chunks. If found, proceed to rule D4.
1009df218577SVlad Yasevich  */
1010df218577SVlad Yasevich static struct sctp_association *__sctp_rcv_asconf_lookup(
10114110cc25SEric W. Biederman 					struct net *net,
1012df218577SVlad Yasevich 					sctp_chunkhdr_t *ch,
1013df218577SVlad Yasevich 					const union sctp_addr *laddr,
1014bc92dd19SAl Viro 					__be16 peer_port,
1015df218577SVlad Yasevich 					struct sctp_transport **transportp)
1016df218577SVlad Yasevich {
1017df218577SVlad Yasevich 	sctp_addip_chunk_t *asconf = (struct sctp_addip_chunk *)ch;
1018df218577SVlad Yasevich 	struct sctp_af *af;
1019df218577SVlad Yasevich 	union sctp_addr_param *param;
1020df218577SVlad Yasevich 	union sctp_addr paddr;
1021df218577SVlad Yasevich 
1022df218577SVlad Yasevich 	/* Skip over the ADDIP header and find the Address parameter */
1023df218577SVlad Yasevich 	param = (union sctp_addr_param *)(asconf + 1);
1024df218577SVlad Yasevich 
10256a435732SShan Wei 	af = sctp_get_af_specific(param_type2af(param->p.type));
1026df218577SVlad Yasevich 	if (unlikely(!af))
1027df218577SVlad Yasevich 		return NULL;
1028df218577SVlad Yasevich 
1029df218577SVlad Yasevich 	af->from_addr_param(&paddr, param, peer_port, 0);
1030df218577SVlad Yasevich 
10314110cc25SEric W. Biederman 	return __sctp_lookup_association(net, laddr, &paddr, transportp);
1032df218577SVlad Yasevich }
1033df218577SVlad Yasevich 
1034df218577SVlad Yasevich 
1035bbd0d598SVlad Yasevich /* SCTP-AUTH, Section 6.3:
1036bbd0d598SVlad Yasevich *    If the receiver does not find a STCB for a packet containing an AUTH
1037bbd0d598SVlad Yasevich *    chunk as the first chunk and not a COOKIE-ECHO chunk as the second
1038bbd0d598SVlad Yasevich *    chunk, it MUST use the chunks after the AUTH chunk to look up an existing
1039bbd0d598SVlad Yasevich *    association.
1040bbd0d598SVlad Yasevich *
1041bbd0d598SVlad Yasevich * This means that any chunks that can help us identify the association need
104225985edcSLucas De Marchi * to be looked at to find this association.
1043bbd0d598SVlad Yasevich */
10444110cc25SEric W. Biederman static struct sctp_association *__sctp_rcv_walk_lookup(struct net *net,
10454110cc25SEric W. Biederman 				      struct sk_buff *skb,
1046bbd0d598SVlad Yasevich 				      const union sctp_addr *laddr,
1047bbd0d598SVlad Yasevich 				      struct sctp_transport **transportp)
1048bbd0d598SVlad Yasevich {
1049df218577SVlad Yasevich 	struct sctp_association *asoc = NULL;
1050df218577SVlad Yasevich 	sctp_chunkhdr_t *ch;
1051df218577SVlad Yasevich 	int have_auth = 0;
1052df218577SVlad Yasevich 	unsigned int chunk_num = 1;
1053df218577SVlad Yasevich 	__u8 *ch_end;
1054df218577SVlad Yasevich 
1055df218577SVlad Yasevich 	/* Walk through the chunks looking for AUTH or ASCONF chunks
1056df218577SVlad Yasevich 	 * to help us find the association.
1057bbd0d598SVlad Yasevich 	 */
1058df218577SVlad Yasevich 	ch = (sctp_chunkhdr_t *) skb->data;
1059df218577SVlad Yasevich 	do {
1060df218577SVlad Yasevich 		/* Break out if chunk length is less then minimal. */
1061df218577SVlad Yasevich 		if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
1062df218577SVlad Yasevich 			break;
1063df218577SVlad Yasevich 
1064df218577SVlad Yasevich 		ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
1065df218577SVlad Yasevich 		if (ch_end > skb_tail_pointer(skb))
1066df218577SVlad Yasevich 			break;
1067df218577SVlad Yasevich 
1068df218577SVlad Yasevich 		switch(ch->type) {
1069df218577SVlad Yasevich 		    case SCTP_CID_AUTH:
1070df218577SVlad Yasevich 			    have_auth = chunk_num;
1071df218577SVlad Yasevich 			    break;
1072df218577SVlad Yasevich 
1073df218577SVlad Yasevich 		    case SCTP_CID_COOKIE_ECHO:
1074df218577SVlad Yasevich 			    /* If a packet arrives containing an AUTH chunk as
1075df218577SVlad Yasevich 			     * a first chunk, a COOKIE-ECHO chunk as the second
1076df218577SVlad Yasevich 			     * chunk, and possibly more chunks after them, and
1077df218577SVlad Yasevich 			     * the receiver does not have an STCB for that
1078df218577SVlad Yasevich 			     * packet, then authentication is based on
1079df218577SVlad Yasevich 			     * the contents of the COOKIE- ECHO chunk.
1080df218577SVlad Yasevich 			     */
1081df218577SVlad Yasevich 			    if (have_auth == 1 && chunk_num == 2)
1082bbd0d598SVlad Yasevich 				    return NULL;
1083df218577SVlad Yasevich 			    break;
1084df218577SVlad Yasevich 
1085df218577SVlad Yasevich 		    case SCTP_CID_ASCONF:
1086e1fc3b14SEric W. Biederman 			    if (have_auth || net->sctp.addip_noauth)
10874110cc25SEric W. Biederman 				    asoc = __sctp_rcv_asconf_lookup(
10884110cc25SEric W. Biederman 							net, ch, laddr,
1089df218577SVlad Yasevich 							sctp_hdr(skb)->source,
1090df218577SVlad Yasevich 							transportp);
1091df218577SVlad Yasevich 		    default:
1092df218577SVlad Yasevich 			    break;
1093df218577SVlad Yasevich 		}
1094df218577SVlad Yasevich 
1095df218577SVlad Yasevich 		if (asoc)
1096df218577SVlad Yasevich 			break;
1097df218577SVlad Yasevich 
1098df218577SVlad Yasevich 		ch = (sctp_chunkhdr_t *) ch_end;
1099df218577SVlad Yasevich 		chunk_num++;
1100df218577SVlad Yasevich 	} while (ch_end < skb_tail_pointer(skb));
1101df218577SVlad Yasevich 
1102df218577SVlad Yasevich 	return asoc;
1103bbd0d598SVlad Yasevich }
1104bbd0d598SVlad Yasevich 
1105bbd0d598SVlad Yasevich /*
1106bbd0d598SVlad Yasevich  * There are circumstances when we need to look inside the SCTP packet
1107bbd0d598SVlad Yasevich  * for information to help us find the association.   Examples
1108bbd0d598SVlad Yasevich  * include looking inside of INIT/INIT-ACK chunks or after the AUTH
1109bbd0d598SVlad Yasevich  * chunks.
1110bbd0d598SVlad Yasevich  */
11114110cc25SEric W. Biederman static struct sctp_association *__sctp_rcv_lookup_harder(struct net *net,
11124110cc25SEric W. Biederman 				      struct sk_buff *skb,
1113bbd0d598SVlad Yasevich 				      const union sctp_addr *laddr,
1114bbd0d598SVlad Yasevich 				      struct sctp_transport **transportp)
1115bbd0d598SVlad Yasevich {
1116bbd0d598SVlad Yasevich 	sctp_chunkhdr_t *ch;
1117bbd0d598SVlad Yasevich 
1118bbd0d598SVlad Yasevich 	ch = (sctp_chunkhdr_t *) skb->data;
1119bbd0d598SVlad Yasevich 
1120df218577SVlad Yasevich 	/* The code below will attempt to walk the chunk and extract
1121df218577SVlad Yasevich 	 * parameter information.  Before we do that, we need to verify
1122df218577SVlad Yasevich 	 * that the chunk length doesn't cause overflow.  Otherwise, we'll
1123df218577SVlad Yasevich 	 * walk off the end.
1124df218577SVlad Yasevich 	 */
1125df218577SVlad Yasevich 	if (WORD_ROUND(ntohs(ch->length)) > skb->len)
1126df218577SVlad Yasevich 		return NULL;
1127df218577SVlad Yasevich 
1128bbd0d598SVlad Yasevich 	/* If this is INIT/INIT-ACK look inside the chunk too. */
1129bbd0d598SVlad Yasevich 	switch (ch->type) {
1130bbd0d598SVlad Yasevich 	case SCTP_CID_INIT:
1131bbd0d598SVlad Yasevich 	case SCTP_CID_INIT_ACK:
11324110cc25SEric W. Biederman 		return __sctp_rcv_init_lookup(net, skb, laddr, transportp);
1133bbd0d598SVlad Yasevich 		break;
1134bbd0d598SVlad Yasevich 
1135df218577SVlad Yasevich 	default:
11364110cc25SEric W. Biederman 		return __sctp_rcv_walk_lookup(net, skb, laddr, transportp);
1137bbd0d598SVlad Yasevich 		break;
1138bbd0d598SVlad Yasevich 	}
1139bbd0d598SVlad Yasevich 
1140df218577SVlad Yasevich 
1141bbd0d598SVlad Yasevich 	return NULL;
1142bbd0d598SVlad Yasevich }
1143bbd0d598SVlad Yasevich 
11441da177e4SLinus Torvalds /* Lookup an association for an inbound skb. */
11454110cc25SEric W. Biederman static struct sctp_association *__sctp_rcv_lookup(struct net *net,
11464110cc25SEric W. Biederman 				      struct sk_buff *skb,
11471da177e4SLinus Torvalds 				      const union sctp_addr *paddr,
11481da177e4SLinus Torvalds 				      const union sctp_addr *laddr,
11491da177e4SLinus Torvalds 				      struct sctp_transport **transportp)
11501da177e4SLinus Torvalds {
11511da177e4SLinus Torvalds 	struct sctp_association *asoc;
11521da177e4SLinus Torvalds 
11534110cc25SEric W. Biederman 	asoc = __sctp_lookup_association(net, laddr, paddr, transportp);
11541da177e4SLinus Torvalds 
11551da177e4SLinus Torvalds 	/* Further lookup for INIT/INIT-ACK packets.
11561da177e4SLinus Torvalds 	 * SCTP Implementors Guide, 2.18 Handling of address
11571da177e4SLinus Torvalds 	 * parameters within the INIT or INIT-ACK.
11581da177e4SLinus Torvalds 	 */
11591da177e4SLinus Torvalds 	if (!asoc)
11604110cc25SEric W. Biederman 		asoc = __sctp_rcv_lookup_harder(net, skb, laddr, transportp);
11611da177e4SLinus Torvalds 
11621da177e4SLinus Torvalds 	return asoc;
11631da177e4SLinus Torvalds }
1164