xref: /linux/net/sctp/input.c (revision a27ef749e7be3b06fb58df53d94eb97a21f18707)
11da177e4SLinus Torvalds /* SCTP kernel reference 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  *
91da177e4SLinus Torvalds  * This file is part of the SCTP kernel reference Implementation
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  * These functions handle all input from the IP layer into SCTP.
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  * The SCTP reference 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  *
191da177e4SLinus Torvalds  * The SCTP reference 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):
321da177e4SLinus Torvalds  *    lksctp developers <lksctp-developers@lists.sourceforge.net>
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 */
561da177e4SLinus Torvalds #include <net/ip.h>
571da177e4SLinus Torvalds #include <net/icmp.h>
581da177e4SLinus Torvalds #include <net/snmp.h>
591da177e4SLinus Torvalds #include <net/sock.h>
601da177e4SLinus Torvalds #include <net/xfrm.h>
611da177e4SLinus Torvalds #include <net/sctp/sctp.h>
621da177e4SLinus Torvalds #include <net/sctp/sm.h>
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds /* Forward declarations for internal helpers. */
651da177e4SLinus Torvalds static int sctp_rcv_ootb(struct sk_buff *);
661da177e4SLinus Torvalds static struct sctp_association *__sctp_rcv_lookup(struct sk_buff *skb,
671da177e4SLinus Torvalds 				      const union sctp_addr *laddr,
681da177e4SLinus Torvalds 				      const union sctp_addr *paddr,
691da177e4SLinus Torvalds 				      struct sctp_transport **transportp);
701da177e4SLinus Torvalds static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(const union sctp_addr *laddr);
711da177e4SLinus Torvalds static struct sctp_association *__sctp_lookup_association(
721da177e4SLinus Torvalds 					const union sctp_addr *local,
731da177e4SLinus Torvalds 					const union sctp_addr *peer,
741da177e4SLinus Torvalds 					struct sctp_transport **pt);
751da177e4SLinus Torvalds 
7661c9fed4SVladislav Yasevich static void sctp_add_backlog(struct sock *sk, struct sk_buff *skb);
7761c9fed4SVladislav Yasevich 
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds /* Calculate the SCTP checksum of an SCTP packet.  */
801da177e4SLinus Torvalds static inline int sctp_rcv_checksum(struct sk_buff *skb)
811da177e4SLinus Torvalds {
821da177e4SLinus Torvalds 	struct sk_buff *list = skb_shinfo(skb)->frag_list;
832c0fd387SArnaldo Carvalho de Melo 	struct sctphdr *sh = sctp_hdr(skb);
842c0fd387SArnaldo Carvalho de Melo 	__u32 cmp = ntohl(sh->checksum);
852c0fd387SArnaldo Carvalho de Melo 	__u32 val = sctp_start_cksum((__u8 *)sh, skb_headlen(skb));
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds 	for (; list; list = list->next)
881da177e4SLinus Torvalds 		val = sctp_update_cksum((__u8 *)list->data, skb_headlen(list),
891da177e4SLinus Torvalds 					val);
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds 	val = sctp_end_cksum(val);
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds 	if (val != cmp) {
941da177e4SLinus Torvalds 		/* CRC failure, dump it. */
951da177e4SLinus Torvalds 		SCTP_INC_STATS_BH(SCTP_MIB_CHECKSUMERRORS);
961da177e4SLinus Torvalds 		return -1;
971da177e4SLinus Torvalds 	}
981da177e4SLinus Torvalds 	return 0;
991da177e4SLinus Torvalds }
1001da177e4SLinus Torvalds 
10179af02c2SDavid S. Miller struct sctp_input_cb {
10279af02c2SDavid S. Miller 	union {
10379af02c2SDavid S. Miller 		struct inet_skb_parm	h4;
10479af02c2SDavid S. Miller #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
10579af02c2SDavid S. Miller 		struct inet6_skb_parm	h6;
10679af02c2SDavid S. Miller #endif
10779af02c2SDavid S. Miller 	} header;
10879af02c2SDavid S. Miller 	struct sctp_chunk *chunk;
10979af02c2SDavid S. Miller };
11079af02c2SDavid S. Miller #define SCTP_INPUT_CB(__skb)	((struct sctp_input_cb *)&((__skb)->cb[0]))
11179af02c2SDavid S. Miller 
1121da177e4SLinus Torvalds /*
1131da177e4SLinus Torvalds  * This is the routine which IP calls when receiving an SCTP packet.
1141da177e4SLinus Torvalds  */
1151da177e4SLinus Torvalds int sctp_rcv(struct sk_buff *skb)
1161da177e4SLinus Torvalds {
1171da177e4SLinus Torvalds 	struct sock *sk;
1181da177e4SLinus Torvalds 	struct sctp_association *asoc;
1191da177e4SLinus Torvalds 	struct sctp_endpoint *ep = NULL;
1201da177e4SLinus Torvalds 	struct sctp_ep_common *rcvr;
1211da177e4SLinus Torvalds 	struct sctp_transport *transport = NULL;
1221da177e4SLinus Torvalds 	struct sctp_chunk *chunk;
1231da177e4SLinus Torvalds 	struct sctphdr *sh;
1241da177e4SLinus Torvalds 	union sctp_addr src;
1251da177e4SLinus Torvalds 	union sctp_addr dest;
1261da177e4SLinus Torvalds 	int family;
1271da177e4SLinus Torvalds 	struct sctp_af *af;
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 	if (skb->pkt_type!=PACKET_HOST)
1301da177e4SLinus Torvalds 		goto discard_it;
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds 	SCTP_INC_STATS_BH(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;
143503b55fdSSridhar Samudrala 	if ((skb->ip_summed != CHECKSUM_UNNECESSARY) &&
144503b55fdSSridhar Samudrala 	    (sctp_rcv_checksum(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 
177d55c41b1SAl Viro 	asoc = __sctp_rcv_lookup(skb, &src, &dest, &transport);
1781c7d1fc1SAl Viro 
1790fd9a65aSNeil Horman 	if (!asoc)
180d55c41b1SAl Viro 		ep = __sctp_rcv_lookup_endpoint(&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 		}
1990fd9a65aSNeil Horman 		sk = sctp_get_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)) {
2151da177e4SLinus Torvalds 			SCTP_INC_STATS_BH(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 
251ac0b0462SSridhar Samudrala 	if (sock_owned_by_user(sk)) {
252ac0b0462SSridhar Samudrala 		SCTP_INC_STATS_BH(SCTP_MIB_IN_PKT_BACKLOG);
25361c9fed4SVladislav Yasevich 		sctp_add_backlog(sk, skb);
254ac0b0462SSridhar Samudrala 	} else {
255ac0b0462SSridhar Samudrala 		SCTP_INC_STATS_BH(SCTP_MIB_IN_PKT_SOFTIRQ);
25661c9fed4SVladislav Yasevich 		sctp_inq_push(&chunk->rcvr->inqueue, chunk);
257ac0b0462SSridhar Samudrala 	}
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds 	sctp_bh_unlock_sock(sk);
26061c9fed4SVladislav Yasevich 
26161c9fed4SVladislav Yasevich 	/* Release the asoc/ep ref we took in the lookup calls. */
26261c9fed4SVladislav Yasevich 	if (asoc)
26361c9fed4SVladislav Yasevich 		sctp_association_put(asoc);
26461c9fed4SVladislav Yasevich 	else
26561c9fed4SVladislav Yasevich 		sctp_endpoint_put(ep);
2667a48f923SSridhar Samudrala 
2672babf9daSHerbert Xu 	return 0;
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds discard_it:
270ac0b0462SSridhar Samudrala 	SCTP_INC_STATS_BH(SCTP_MIB_IN_PKT_DISCARDS);
2711da177e4SLinus Torvalds 	kfree_skb(skb);
2722babf9daSHerbert Xu 	return 0;
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds discard_release:
27561c9fed4SVladislav Yasevich 	/* Release the asoc/ep ref we took in the lookup calls. */
2760fd9a65aSNeil Horman 	if (asoc)
2771da177e4SLinus Torvalds 		sctp_association_put(asoc);
2780fd9a65aSNeil Horman 	else
2791da177e4SLinus Torvalds 		sctp_endpoint_put(ep);
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 	goto discard_it;
2821da177e4SLinus Torvalds }
2831da177e4SLinus Torvalds 
28461c9fed4SVladislav Yasevich /* Process the backlog queue of the socket.  Every skb on
28561c9fed4SVladislav Yasevich  * the backlog holds a ref on an association or endpoint.
28661c9fed4SVladislav Yasevich  * We hold this ref throughout the state machine to make
28761c9fed4SVladislav Yasevich  * sure that the structure we need is still around.
2881da177e4SLinus Torvalds  */
2891da177e4SLinus Torvalds int sctp_backlog_rcv(struct sock *sk, struct sk_buff *skb)
2901da177e4SLinus Torvalds {
29179af02c2SDavid S. Miller 	struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
29261c9fed4SVladislav Yasevich 	struct sctp_inq *inqueue = &chunk->rcvr->inqueue;
2937a48f923SSridhar Samudrala 	struct sctp_ep_common *rcvr = NULL;
29461c9fed4SVladislav Yasevich 	int backloged = 0;
2951da177e4SLinus Torvalds 
2967a48f923SSridhar Samudrala 	rcvr = chunk->rcvr;
297c4d2444eSSridhar Samudrala 
29861c9fed4SVladislav Yasevich 	/* If the rcvr is dead then the association or endpoint
29961c9fed4SVladislav Yasevich 	 * has been deleted and we can safely drop the chunk
30061c9fed4SVladislav Yasevich 	 * and refs that we are holding.
30161c9fed4SVladislav Yasevich 	 */
3027a48f923SSridhar Samudrala 	if (rcvr->dead) {
3037a48f923SSridhar Samudrala 		sctp_chunk_free(chunk);
30461c9fed4SVladislav Yasevich 		goto done;
30561c9fed4SVladislav Yasevich 	}
30661c9fed4SVladislav Yasevich 
30761c9fed4SVladislav Yasevich 	if (unlikely(rcvr->sk != sk)) {
30861c9fed4SVladislav Yasevich 		/* In this case, the association moved from one socket to
30961c9fed4SVladislav Yasevich 		 * another.  We are currently sitting on the backlog of the
31061c9fed4SVladislav Yasevich 		 * old socket, so we need to move.
31161c9fed4SVladislav Yasevich 		 * However, since we are here in the process context we
31261c9fed4SVladislav Yasevich 		 * need to take make sure that the user doesn't own
31361c9fed4SVladislav Yasevich 		 * the new socket when we process the packet.
31461c9fed4SVladislav Yasevich 		 * If the new socket is user-owned, queue the chunk to the
31561c9fed4SVladislav Yasevich 		 * backlog of the new socket without dropping any refs.
31661c9fed4SVladislav Yasevich 		 * Otherwise, we can safely push the chunk on the inqueue.
31761c9fed4SVladislav Yasevich 		 */
31861c9fed4SVladislav Yasevich 
31961c9fed4SVladislav Yasevich 		sk = rcvr->sk;
32061c9fed4SVladislav Yasevich 		sctp_bh_lock_sock(sk);
32161c9fed4SVladislav Yasevich 
32261c9fed4SVladislav Yasevich 		if (sock_owned_by_user(sk)) {
32361c9fed4SVladislav Yasevich 			sk_add_backlog(sk, skb);
32461c9fed4SVladislav Yasevich 			backloged = 1;
32561c9fed4SVladislav Yasevich 		} else
32661c9fed4SVladislav Yasevich 			sctp_inq_push(inqueue, chunk);
32761c9fed4SVladislav Yasevich 
32861c9fed4SVladislav Yasevich 		sctp_bh_unlock_sock(sk);
32961c9fed4SVladislav Yasevich 
33061c9fed4SVladislav Yasevich 		/* If the chunk was backloged again, don't drop refs */
33161c9fed4SVladislav Yasevich 		if (backloged)
33261c9fed4SVladislav Yasevich 			return 0;
3337a48f923SSridhar Samudrala 	} else {
3341da177e4SLinus Torvalds 		sctp_inq_push(inqueue, chunk);
3357a48f923SSridhar Samudrala 	}
3367a48f923SSridhar Samudrala 
33761c9fed4SVladislav Yasevich done:
33861c9fed4SVladislav Yasevich 	/* Release the refs we took in sctp_add_backlog */
3397a48f923SSridhar Samudrala 	if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
3407a48f923SSridhar Samudrala 		sctp_association_put(sctp_assoc(rcvr));
34161c9fed4SVladislav Yasevich 	else if (SCTP_EP_TYPE_SOCKET == rcvr->type)
3427a48f923SSridhar Samudrala 		sctp_endpoint_put(sctp_ep(rcvr));
34361c9fed4SVladislav Yasevich 	else
34461c9fed4SVladislav Yasevich 		BUG();
3457a48f923SSridhar Samudrala 
3461da177e4SLinus Torvalds 	return 0;
3471da177e4SLinus Torvalds }
3481da177e4SLinus Torvalds 
34961c9fed4SVladislav Yasevich static void sctp_add_backlog(struct sock *sk, struct sk_buff *skb)
350c4d2444eSSridhar Samudrala {
35161c9fed4SVladislav Yasevich 	struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
35261c9fed4SVladislav Yasevich 	struct sctp_ep_common *rcvr = chunk->rcvr;
353c4d2444eSSridhar Samudrala 
35461c9fed4SVladislav Yasevich 	/* Hold the assoc/ep while hanging on the backlog queue.
35561c9fed4SVladislav Yasevich 	 * This way, we know structures we need will not disappear from us
35661c9fed4SVladislav Yasevich 	 */
35761c9fed4SVladislav Yasevich 	if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
35861c9fed4SVladislav Yasevich 		sctp_association_hold(sctp_assoc(rcvr));
35961c9fed4SVladislav Yasevich 	else if (SCTP_EP_TYPE_SOCKET == rcvr->type)
36061c9fed4SVladislav Yasevich 		sctp_endpoint_hold(sctp_ep(rcvr));
361c4d2444eSSridhar Samudrala 	else
36261c9fed4SVladislav Yasevich 		BUG();
36361c9fed4SVladislav Yasevich 
36461c9fed4SVladislav Yasevich 	sk_add_backlog(sk, skb);
365c4d2444eSSridhar Samudrala }
366c4d2444eSSridhar Samudrala 
3671da177e4SLinus Torvalds /* Handle icmp frag needed error. */
3681da177e4SLinus Torvalds void sctp_icmp_frag_needed(struct sock *sk, struct sctp_association *asoc,
3691da177e4SLinus Torvalds 			   struct sctp_transport *t, __u32 pmtu)
3701da177e4SLinus Torvalds {
37152ccb8e9SFrank Filz 	if (sock_owned_by_user(sk) || !t || (t->pathmtu == pmtu))
37252ccb8e9SFrank Filz 		return;
37352ccb8e9SFrank Filz 
37452ccb8e9SFrank Filz 	if (t->param_flags & SPP_PMTUD_ENABLE) {
3751da177e4SLinus Torvalds 		if (unlikely(pmtu < SCTP_DEFAULT_MINSEGMENT)) {
3761da177e4SLinus Torvalds 			printk(KERN_WARNING "%s: Reported pmtu %d too low, "
37752ccb8e9SFrank Filz 			       "using default minimum of %d\n",
37852ccb8e9SFrank Filz 			       __FUNCTION__, pmtu,
3791da177e4SLinus Torvalds 			       SCTP_DEFAULT_MINSEGMENT);
38052ccb8e9SFrank Filz 			/* Use default minimum segment size and disable
38152ccb8e9SFrank Filz 			 * pmtu discovery on this transport.
38252ccb8e9SFrank Filz 			 */
38352ccb8e9SFrank Filz 			t->pathmtu = SCTP_DEFAULT_MINSEGMENT;
384b56bab46SVlad Yasevich 			t->param_flags = (t->param_flags & ~SPP_PMTUD) |
38552ccb8e9SFrank Filz 				SPP_PMTUD_DISABLE;
38652ccb8e9SFrank Filz 		} else {
38752ccb8e9SFrank Filz 			t->pathmtu = pmtu;
3881da177e4SLinus Torvalds 		}
3891da177e4SLinus Torvalds 
39052ccb8e9SFrank Filz 		/* Update association pmtu. */
3911da177e4SLinus Torvalds 		sctp_assoc_sync_pmtu(asoc);
3921da177e4SLinus Torvalds 	}
39352ccb8e9SFrank Filz 
39452ccb8e9SFrank Filz 	/* Retransmit with the new pmtu setting.
39552ccb8e9SFrank Filz 	 * Normally, if PMTU discovery is disabled, an ICMP Fragmentation
39652ccb8e9SFrank Filz 	 * Needed will never be sent, but if a message was sent before
39752ccb8e9SFrank Filz 	 * PMTU discovery was disabled that was larger than the PMTU, it
39852ccb8e9SFrank Filz 	 * would not be fragmented, so it must be re-transmitted fragmented.
39952ccb8e9SFrank Filz 	 */
40052ccb8e9SFrank Filz 	sctp_retransmit(&asoc->outqueue, t, SCTP_RTXR_PMTUD);
4011da177e4SLinus Torvalds }
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds /*
4041da177e4SLinus Torvalds  * SCTP Implementer's Guide, 2.37 ICMP handling procedures
4051da177e4SLinus Torvalds  *
4061da177e4SLinus Torvalds  * ICMP8) If the ICMP code is a "Unrecognized next header type encountered"
4071da177e4SLinus Torvalds  *        or a "Protocol Unreachable" treat this message as an abort
4081da177e4SLinus Torvalds  *        with the T bit set.
4091da177e4SLinus Torvalds  *
4101da177e4SLinus Torvalds  * This function sends an event to the state machine, which will abort the
4111da177e4SLinus Torvalds  * association.
4121da177e4SLinus Torvalds  *
4131da177e4SLinus Torvalds  */
4141da177e4SLinus Torvalds void sctp_icmp_proto_unreachable(struct sock *sk,
4151da177e4SLinus Torvalds 			   struct sctp_association *asoc,
4161da177e4SLinus Torvalds 			   struct sctp_transport *t)
4171da177e4SLinus Torvalds {
4181da177e4SLinus Torvalds 	SCTP_DEBUG_PRINTK("%s\n",  __FUNCTION__);
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds 	sctp_do_sm(SCTP_EVENT_T_OTHER,
4211da177e4SLinus Torvalds 		   SCTP_ST_OTHER(SCTP_EVENT_ICMP_PROTO_UNREACH),
4223f7a87d2SFrank Filz 		   asoc->state, asoc->ep, asoc, t,
4231da177e4SLinus Torvalds 		   GFP_ATOMIC);
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds }
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds /* Common lookup code for icmp/icmpv6 error handler. */
4281da177e4SLinus Torvalds struct sock *sctp_err_lookup(int family, struct sk_buff *skb,
4291da177e4SLinus Torvalds 			     struct sctphdr *sctphdr,
4301da177e4SLinus Torvalds 			     struct sctp_association **app,
4311da177e4SLinus Torvalds 			     struct sctp_transport **tpp)
4321da177e4SLinus Torvalds {
4331da177e4SLinus Torvalds 	union sctp_addr saddr;
4341da177e4SLinus Torvalds 	union sctp_addr daddr;
4351da177e4SLinus Torvalds 	struct sctp_af *af;
4361da177e4SLinus Torvalds 	struct sock *sk = NULL;
4378de8c873SSridhar Samudrala 	struct sctp_association *asoc;
4381da177e4SLinus Torvalds 	struct sctp_transport *transport = NULL;
4391da177e4SLinus Torvalds 
440d1ad1ff2SSridhar Samudrala 	*app = NULL; *tpp = NULL;
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds 	af = sctp_get_af_specific(family);
4431da177e4SLinus Torvalds 	if (unlikely(!af)) {
4441da177e4SLinus Torvalds 		return NULL;
4451da177e4SLinus Torvalds 	}
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds 	/* Initialize local addresses for lookups. */
4481da177e4SLinus Torvalds 	af->from_skb(&saddr, skb, 1);
4491da177e4SLinus Torvalds 	af->from_skb(&daddr, skb, 0);
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds 	/* Look for an association that matches the incoming ICMP error
4521da177e4SLinus Torvalds 	 * packet.
4531da177e4SLinus Torvalds 	 */
454d55c41b1SAl Viro 	asoc = __sctp_lookup_association(&saddr, &daddr, &transport);
455d1ad1ff2SSridhar Samudrala 	if (!asoc)
4561da177e4SLinus Torvalds 		return NULL;
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds 	sk = asoc->base.sk;
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds 	if (ntohl(sctphdr->vtag) != asoc->c.peer_vtag) {
4611da177e4SLinus Torvalds 		ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
4621da177e4SLinus Torvalds 		goto out;
4631da177e4SLinus Torvalds 	}
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds 	sctp_bh_lock_sock(sk);
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds 	/* If too many ICMPs get dropped on busy
4681da177e4SLinus Torvalds 	 * servers this needs to be solved differently.
4691da177e4SLinus Torvalds 	 */
4701da177e4SLinus Torvalds 	if (sock_owned_by_user(sk))
4711da177e4SLinus Torvalds 		NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds 	*app = asoc;
4741da177e4SLinus Torvalds 	*tpp = transport;
4751da177e4SLinus Torvalds 	return sk;
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds out:
4781da177e4SLinus Torvalds 	if (asoc)
4791da177e4SLinus Torvalds 		sctp_association_put(asoc);
4801da177e4SLinus Torvalds 	return NULL;
4811da177e4SLinus Torvalds }
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds /* Common cleanup code for icmp/icmpv6 error handler. */
484d1ad1ff2SSridhar Samudrala void sctp_err_finish(struct sock *sk, struct sctp_association *asoc)
4851da177e4SLinus Torvalds {
4861da177e4SLinus Torvalds 	sctp_bh_unlock_sock(sk);
4871da177e4SLinus Torvalds 	if (asoc)
4881da177e4SLinus Torvalds 		sctp_association_put(asoc);
4891da177e4SLinus Torvalds }
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds /*
4921da177e4SLinus Torvalds  * This routine is called by the ICMP module when it gets some
4931da177e4SLinus Torvalds  * sort of error condition.  If err < 0 then the socket should
4941da177e4SLinus Torvalds  * be closed and the error returned to the user.  If err > 0
4951da177e4SLinus Torvalds  * it's just the icmp type << 8 | icmp code.  After adjustment
4961da177e4SLinus Torvalds  * header points to the first 8 bytes of the sctp header.  We need
4971da177e4SLinus Torvalds  * to find the appropriate port.
4981da177e4SLinus Torvalds  *
4991da177e4SLinus Torvalds  * The locking strategy used here is very "optimistic". When
5001da177e4SLinus Torvalds  * someone else accesses the socket the ICMP is just dropped
5011da177e4SLinus Torvalds  * and for some paths there is no check at all.
5021da177e4SLinus Torvalds  * A more general error queue to queue errors for later handling
5031da177e4SLinus Torvalds  * is probably better.
5041da177e4SLinus Torvalds  *
5051da177e4SLinus Torvalds  */
5061da177e4SLinus Torvalds void sctp_v4_err(struct sk_buff *skb, __u32 info)
5071da177e4SLinus Torvalds {
5081da177e4SLinus Torvalds 	struct iphdr *iph = (struct iphdr *)skb->data;
509*a27ef749SArnaldo Carvalho de Melo 	const int ihlen = iph->ihl * 4;
51088c7664fSArnaldo Carvalho de Melo 	const int type = icmp_hdr(skb)->type;
51188c7664fSArnaldo Carvalho de Melo 	const int code = icmp_hdr(skb)->code;
5121da177e4SLinus Torvalds 	struct sock *sk;
5138de8c873SSridhar Samudrala 	struct sctp_association *asoc = NULL;
5141da177e4SLinus Torvalds 	struct sctp_transport *transport;
5151da177e4SLinus Torvalds 	struct inet_sock *inet;
5161da177e4SLinus Torvalds 	char *saveip, *savesctp;
5171da177e4SLinus Torvalds 	int err;
5181da177e4SLinus Torvalds 
519*a27ef749SArnaldo Carvalho de Melo 	if (skb->len < ihlen + 8) {
5201da177e4SLinus Torvalds 		ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
5211da177e4SLinus Torvalds 		return;
5221da177e4SLinus Torvalds 	}
5231da177e4SLinus Torvalds 
5241da177e4SLinus Torvalds 	/* Fix up skb to look at the embedded net header. */
5251da177e4SLinus Torvalds 	saveip = skb->nh.raw;
5261da177e4SLinus Torvalds 	savesctp  = skb->h.raw;
52731c7711bSArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
528*a27ef749SArnaldo Carvalho de Melo 	skb_set_transport_header(skb, ihlen);
529*a27ef749SArnaldo Carvalho de Melo 	sk = sctp_err_lookup(AF_INET, skb, sctp_hdr(skb), &asoc, &transport);
5301da177e4SLinus Torvalds 	/* Put back, the original pointers. */
5311da177e4SLinus Torvalds 	skb->nh.raw = saveip;
5321da177e4SLinus Torvalds 	skb->h.raw = savesctp;
5331da177e4SLinus Torvalds 	if (!sk) {
5341da177e4SLinus Torvalds 		ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
5351da177e4SLinus Torvalds 		return;
5361da177e4SLinus Torvalds 	}
5371da177e4SLinus Torvalds 	/* Warning:  The sock lock is held.  Remember to call
5381da177e4SLinus Torvalds 	 * sctp_err_finish!
5391da177e4SLinus Torvalds 	 */
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds 	switch (type) {
5421da177e4SLinus Torvalds 	case ICMP_PARAMETERPROB:
5431da177e4SLinus Torvalds 		err = EPROTO;
5441da177e4SLinus Torvalds 		break;
5451da177e4SLinus Torvalds 	case ICMP_DEST_UNREACH:
5461da177e4SLinus Torvalds 		if (code > NR_ICMP_UNREACH)
5471da177e4SLinus Torvalds 			goto out_unlock;
5481da177e4SLinus Torvalds 
5491da177e4SLinus Torvalds 		/* PMTU discovery (RFC1191) */
5501da177e4SLinus Torvalds 		if (ICMP_FRAG_NEEDED == code) {
5511da177e4SLinus Torvalds 			sctp_icmp_frag_needed(sk, asoc, transport, info);
5521da177e4SLinus Torvalds 			goto out_unlock;
5531da177e4SLinus Torvalds 		}
5541da177e4SLinus Torvalds 		else {
5551da177e4SLinus Torvalds 			if (ICMP_PROT_UNREACH == code) {
556d1ad1ff2SSridhar Samudrala 				sctp_icmp_proto_unreachable(sk, asoc,
5571da177e4SLinus Torvalds 							    transport);
5581da177e4SLinus Torvalds 				goto out_unlock;
5591da177e4SLinus Torvalds 			}
5601da177e4SLinus Torvalds 		}
5611da177e4SLinus Torvalds 		err = icmp_err_convert[code].errno;
5621da177e4SLinus Torvalds 		break;
5631da177e4SLinus Torvalds 	case ICMP_TIME_EXCEEDED:
5641da177e4SLinus Torvalds 		/* Ignore any time exceeded errors due to fragment reassembly
5651da177e4SLinus Torvalds 		 * timeouts.
5661da177e4SLinus Torvalds 		 */
5671da177e4SLinus Torvalds 		if (ICMP_EXC_FRAGTIME == code)
5681da177e4SLinus Torvalds 			goto out_unlock;
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds 		err = EHOSTUNREACH;
5711da177e4SLinus Torvalds 		break;
5721da177e4SLinus Torvalds 	default:
5731da177e4SLinus Torvalds 		goto out_unlock;
5741da177e4SLinus Torvalds 	}
5751da177e4SLinus Torvalds 
5761da177e4SLinus Torvalds 	inet = inet_sk(sk);
5771da177e4SLinus Torvalds 	if (!sock_owned_by_user(sk) && inet->recverr) {
5781da177e4SLinus Torvalds 		sk->sk_err = err;
5791da177e4SLinus Torvalds 		sk->sk_error_report(sk);
5801da177e4SLinus Torvalds 	} else {  /* Only an error on timeout */
5811da177e4SLinus Torvalds 		sk->sk_err_soft = err;
5821da177e4SLinus Torvalds 	}
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds out_unlock:
585d1ad1ff2SSridhar Samudrala 	sctp_err_finish(sk, asoc);
5861da177e4SLinus Torvalds }
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds /*
5891da177e4SLinus Torvalds  * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
5901da177e4SLinus Torvalds  *
5911da177e4SLinus Torvalds  * This function scans all the chunks in the OOTB packet to determine if
5921da177e4SLinus Torvalds  * the packet should be discarded right away.  If a response might be needed
5931da177e4SLinus Torvalds  * for this packet, or, if further processing is possible, the packet will
5941da177e4SLinus Torvalds  * be queued to a proper inqueue for the next phase of handling.
5951da177e4SLinus Torvalds  *
5961da177e4SLinus Torvalds  * Output:
5971da177e4SLinus Torvalds  * Return 0 - If further processing is needed.
5981da177e4SLinus Torvalds  * Return 1 - If the packet can be discarded right away.
5991da177e4SLinus Torvalds  */
6001da177e4SLinus Torvalds int sctp_rcv_ootb(struct sk_buff *skb)
6011da177e4SLinus Torvalds {
6021da177e4SLinus Torvalds 	sctp_chunkhdr_t *ch;
6031da177e4SLinus Torvalds 	__u8 *ch_end;
6041da177e4SLinus Torvalds 	sctp_errhdr_t *err;
6051da177e4SLinus Torvalds 
6061da177e4SLinus Torvalds 	ch = (sctp_chunkhdr_t *) skb->data;
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds 	/* Scan through all the chunks in the packet.  */
609a7d1f1b6STsutomu Fujii 	do {
610a7d1f1b6STsutomu Fujii 		/* Break out if chunk length is less then minimal. */
611a7d1f1b6STsutomu Fujii 		if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
612a7d1f1b6STsutomu Fujii 			break;
613a7d1f1b6STsutomu Fujii 
614a7d1f1b6STsutomu Fujii 		ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
615a7d1f1b6STsutomu Fujii 		if (ch_end > skb->tail)
616a7d1f1b6STsutomu Fujii 			break;
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds 		/* RFC 8.4, 2) If the OOTB packet contains an ABORT chunk, the
6191da177e4SLinus Torvalds 		 * receiver MUST silently discard the OOTB packet and take no
6201da177e4SLinus Torvalds 		 * further action.
6211da177e4SLinus Torvalds 		 */
6221da177e4SLinus Torvalds 		if (SCTP_CID_ABORT == ch->type)
6231da177e4SLinus Torvalds 			goto discard;
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds 		/* RFC 8.4, 6) If the packet contains a SHUTDOWN COMPLETE
6261da177e4SLinus Torvalds 		 * chunk, the receiver should silently discard the packet
6271da177e4SLinus Torvalds 		 * and take no further action.
6281da177e4SLinus Torvalds 		 */
6291da177e4SLinus Torvalds 		if (SCTP_CID_SHUTDOWN_COMPLETE == ch->type)
6301da177e4SLinus Torvalds 			goto discard;
6311da177e4SLinus Torvalds 
6321da177e4SLinus Torvalds 		/* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
6331da177e4SLinus Torvalds 		 * or a COOKIE ACK the SCTP Packet should be silently
6341da177e4SLinus Torvalds 		 * discarded.
6351da177e4SLinus Torvalds 		 */
6361da177e4SLinus Torvalds 		if (SCTP_CID_COOKIE_ACK == ch->type)
6371da177e4SLinus Torvalds 			goto discard;
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds 		if (SCTP_CID_ERROR == ch->type) {
6401da177e4SLinus Torvalds 			sctp_walk_errors(err, ch) {
6411da177e4SLinus Torvalds 				if (SCTP_ERROR_STALE_COOKIE == err->cause)
6421da177e4SLinus Torvalds 					goto discard;
6431da177e4SLinus Torvalds 			}
6441da177e4SLinus Torvalds 		}
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds 		ch = (sctp_chunkhdr_t *) ch_end;
647a7d1f1b6STsutomu Fujii 	} while (ch_end < skb->tail);
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds 	return 0;
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds discard:
6521da177e4SLinus Torvalds 	return 1;
6531da177e4SLinus Torvalds }
6541da177e4SLinus Torvalds 
6551da177e4SLinus Torvalds /* Insert endpoint into the hash table.  */
6561da177e4SLinus Torvalds static void __sctp_hash_endpoint(struct sctp_endpoint *ep)
6571da177e4SLinus Torvalds {
6581da177e4SLinus Torvalds 	struct sctp_ep_common **epp;
6591da177e4SLinus Torvalds 	struct sctp_ep_common *epb;
6601da177e4SLinus Torvalds 	struct sctp_hashbucket *head;
6611da177e4SLinus Torvalds 
6621da177e4SLinus Torvalds 	epb = &ep->base;
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds 	epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);
6651da177e4SLinus Torvalds 	head = &sctp_ep_hashtable[epb->hashent];
6661da177e4SLinus Torvalds 
6671da177e4SLinus Torvalds 	sctp_write_lock(&head->lock);
6681da177e4SLinus Torvalds 	epp = &head->chain;
6691da177e4SLinus Torvalds 	epb->next = *epp;
6701da177e4SLinus Torvalds 	if (epb->next)
6711da177e4SLinus Torvalds 		(*epp)->pprev = &epb->next;
6721da177e4SLinus Torvalds 	*epp = epb;
6731da177e4SLinus Torvalds 	epb->pprev = epp;
6741da177e4SLinus Torvalds 	sctp_write_unlock(&head->lock);
6751da177e4SLinus Torvalds }
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds /* Add an endpoint to the hash. Local BH-safe. */
6781da177e4SLinus Torvalds void sctp_hash_endpoint(struct sctp_endpoint *ep)
6791da177e4SLinus Torvalds {
6801da177e4SLinus Torvalds 	sctp_local_bh_disable();
6811da177e4SLinus Torvalds 	__sctp_hash_endpoint(ep);
6821da177e4SLinus Torvalds 	sctp_local_bh_enable();
6831da177e4SLinus Torvalds }
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds /* Remove endpoint from the hash table.  */
6861da177e4SLinus Torvalds static void __sctp_unhash_endpoint(struct sctp_endpoint *ep)
6871da177e4SLinus Torvalds {
6881da177e4SLinus Torvalds 	struct sctp_hashbucket *head;
6891da177e4SLinus Torvalds 	struct sctp_ep_common *epb;
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds 	epb = &ep->base;
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds 	epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds 	head = &sctp_ep_hashtable[epb->hashent];
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds 	sctp_write_lock(&head->lock);
6981da177e4SLinus Torvalds 
6991da177e4SLinus Torvalds 	if (epb->pprev) {
7001da177e4SLinus Torvalds 		if (epb->next)
7011da177e4SLinus Torvalds 			epb->next->pprev = epb->pprev;
7021da177e4SLinus Torvalds 		*epb->pprev = epb->next;
7031da177e4SLinus Torvalds 		epb->pprev = NULL;
7041da177e4SLinus Torvalds 	}
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds 	sctp_write_unlock(&head->lock);
7071da177e4SLinus Torvalds }
7081da177e4SLinus Torvalds 
7091da177e4SLinus Torvalds /* Remove endpoint from the hash.  Local BH-safe. */
7101da177e4SLinus Torvalds void sctp_unhash_endpoint(struct sctp_endpoint *ep)
7111da177e4SLinus Torvalds {
7121da177e4SLinus Torvalds 	sctp_local_bh_disable();
7131da177e4SLinus Torvalds 	__sctp_unhash_endpoint(ep);
7141da177e4SLinus Torvalds 	sctp_local_bh_enable();
7151da177e4SLinus Torvalds }
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds /* Look up an endpoint. */
7181da177e4SLinus Torvalds static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(const union sctp_addr *laddr)
7191da177e4SLinus Torvalds {
7201da177e4SLinus Torvalds 	struct sctp_hashbucket *head;
7211da177e4SLinus Torvalds 	struct sctp_ep_common *epb;
7221da177e4SLinus Torvalds 	struct sctp_endpoint *ep;
7231da177e4SLinus Torvalds 	int hash;
7241da177e4SLinus Torvalds 
7251c7d1fc1SAl Viro 	hash = sctp_ep_hashfn(ntohs(laddr->v4.sin_port));
7261da177e4SLinus Torvalds 	head = &sctp_ep_hashtable[hash];
7271da177e4SLinus Torvalds 	read_lock(&head->lock);
7281da177e4SLinus Torvalds 	for (epb = head->chain; epb; epb = epb->next) {
7291da177e4SLinus Torvalds 		ep = sctp_ep(epb);
7301da177e4SLinus Torvalds 		if (sctp_endpoint_is_match(ep, laddr))
7311da177e4SLinus Torvalds 			goto hit;
7321da177e4SLinus Torvalds 	}
7331da177e4SLinus Torvalds 
7341da177e4SLinus Torvalds 	ep = sctp_sk((sctp_get_ctl_sock()))->ep;
7351da177e4SLinus Torvalds 	epb = &ep->base;
7361da177e4SLinus Torvalds 
7371da177e4SLinus Torvalds hit:
7381da177e4SLinus Torvalds 	sctp_endpoint_hold(ep);
7391da177e4SLinus Torvalds 	read_unlock(&head->lock);
7401da177e4SLinus Torvalds 	return ep;
7411da177e4SLinus Torvalds }
7421da177e4SLinus Torvalds 
7431da177e4SLinus Torvalds /* Insert association into the hash table.  */
7441da177e4SLinus Torvalds static void __sctp_hash_established(struct sctp_association *asoc)
7451da177e4SLinus Torvalds {
7461da177e4SLinus Torvalds 	struct sctp_ep_common **epp;
7471da177e4SLinus Torvalds 	struct sctp_ep_common *epb;
7481da177e4SLinus Torvalds 	struct sctp_hashbucket *head;
7491da177e4SLinus Torvalds 
7501da177e4SLinus Torvalds 	epb = &asoc->base;
7511da177e4SLinus Torvalds 
7521da177e4SLinus Torvalds 	/* Calculate which chain this entry will belong to. */
7531da177e4SLinus Torvalds 	epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port, asoc->peer.port);
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 	head = &sctp_assoc_hashtable[epb->hashent];
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds 	sctp_write_lock(&head->lock);
7581da177e4SLinus Torvalds 	epp = &head->chain;
7591da177e4SLinus Torvalds 	epb->next = *epp;
7601da177e4SLinus Torvalds 	if (epb->next)
7611da177e4SLinus Torvalds 		(*epp)->pprev = &epb->next;
7621da177e4SLinus Torvalds 	*epp = epb;
7631da177e4SLinus Torvalds 	epb->pprev = epp;
7641da177e4SLinus Torvalds 	sctp_write_unlock(&head->lock);
7651da177e4SLinus Torvalds }
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds /* Add an association to the hash. Local BH-safe. */
7681da177e4SLinus Torvalds void sctp_hash_established(struct sctp_association *asoc)
7691da177e4SLinus Torvalds {
770de76e695SVlad Yasevich 	if (asoc->temp)
771de76e695SVlad Yasevich 		return;
772de76e695SVlad Yasevich 
7731da177e4SLinus Torvalds 	sctp_local_bh_disable();
7741da177e4SLinus Torvalds 	__sctp_hash_established(asoc);
7751da177e4SLinus Torvalds 	sctp_local_bh_enable();
7761da177e4SLinus Torvalds }
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds /* Remove association from the hash table.  */
7791da177e4SLinus Torvalds static void __sctp_unhash_established(struct sctp_association *asoc)
7801da177e4SLinus Torvalds {
7811da177e4SLinus Torvalds 	struct sctp_hashbucket *head;
7821da177e4SLinus Torvalds 	struct sctp_ep_common *epb;
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds 	epb = &asoc->base;
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds 	epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port,
7871da177e4SLinus Torvalds 					 asoc->peer.port);
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds 	head = &sctp_assoc_hashtable[epb->hashent];
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds 	sctp_write_lock(&head->lock);
7921da177e4SLinus Torvalds 
7931da177e4SLinus Torvalds 	if (epb->pprev) {
7941da177e4SLinus Torvalds 		if (epb->next)
7951da177e4SLinus Torvalds 			epb->next->pprev = epb->pprev;
7961da177e4SLinus Torvalds 		*epb->pprev = epb->next;
7971da177e4SLinus Torvalds 		epb->pprev = NULL;
7981da177e4SLinus Torvalds 	}
7991da177e4SLinus Torvalds 
8001da177e4SLinus Torvalds 	sctp_write_unlock(&head->lock);
8011da177e4SLinus Torvalds }
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds /* Remove association from the hash table.  Local BH-safe. */
8041da177e4SLinus Torvalds void sctp_unhash_established(struct sctp_association *asoc)
8051da177e4SLinus Torvalds {
806de76e695SVlad Yasevich 	if (asoc->temp)
807de76e695SVlad Yasevich 		return;
808de76e695SVlad Yasevich 
8091da177e4SLinus Torvalds 	sctp_local_bh_disable();
8101da177e4SLinus Torvalds 	__sctp_unhash_established(asoc);
8111da177e4SLinus Torvalds 	sctp_local_bh_enable();
8121da177e4SLinus Torvalds }
8131da177e4SLinus Torvalds 
8141da177e4SLinus Torvalds /* Look up an association. */
8151da177e4SLinus Torvalds static struct sctp_association *__sctp_lookup_association(
8161da177e4SLinus Torvalds 					const union sctp_addr *local,
8171da177e4SLinus Torvalds 					const union sctp_addr *peer,
8181da177e4SLinus Torvalds 					struct sctp_transport **pt)
8191da177e4SLinus Torvalds {
8201da177e4SLinus Torvalds 	struct sctp_hashbucket *head;
8211da177e4SLinus Torvalds 	struct sctp_ep_common *epb;
8221da177e4SLinus Torvalds 	struct sctp_association *asoc;
8231da177e4SLinus Torvalds 	struct sctp_transport *transport;
8241da177e4SLinus Torvalds 	int hash;
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds 	/* Optimize here for direct hit, only listening connections can
8271da177e4SLinus Torvalds 	 * have wildcards anyways.
8281da177e4SLinus Torvalds 	 */
829e2fccedbSAl Viro 	hash = sctp_assoc_hashfn(ntohs(local->v4.sin_port), ntohs(peer->v4.sin_port));
8301da177e4SLinus Torvalds 	head = &sctp_assoc_hashtable[hash];
8311da177e4SLinus Torvalds 	read_lock(&head->lock);
8321da177e4SLinus Torvalds 	for (epb = head->chain; epb; epb = epb->next) {
8331da177e4SLinus Torvalds 		asoc = sctp_assoc(epb);
8341da177e4SLinus Torvalds 		transport = sctp_assoc_is_match(asoc, local, peer);
8351da177e4SLinus Torvalds 		if (transport)
8361da177e4SLinus Torvalds 			goto hit;
8371da177e4SLinus Torvalds 	}
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds 	read_unlock(&head->lock);
8401da177e4SLinus Torvalds 
8411da177e4SLinus Torvalds 	return NULL;
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds hit:
8441da177e4SLinus Torvalds 	*pt = transport;
8451da177e4SLinus Torvalds 	sctp_association_hold(asoc);
8461da177e4SLinus Torvalds 	read_unlock(&head->lock);
8471da177e4SLinus Torvalds 	return asoc;
8481da177e4SLinus Torvalds }
8491da177e4SLinus Torvalds 
8501da177e4SLinus Torvalds /* Look up an association. BH-safe. */
8511da177e4SLinus Torvalds SCTP_STATIC
8521da177e4SLinus Torvalds struct sctp_association *sctp_lookup_association(const union sctp_addr *laddr,
8531da177e4SLinus Torvalds 						 const union sctp_addr *paddr,
8541da177e4SLinus Torvalds 					    struct sctp_transport **transportp)
8551da177e4SLinus Torvalds {
8561da177e4SLinus Torvalds 	struct sctp_association *asoc;
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds 	sctp_local_bh_disable();
8591da177e4SLinus Torvalds 	asoc = __sctp_lookup_association(laddr, paddr, transportp);
8601da177e4SLinus Torvalds 	sctp_local_bh_enable();
8611da177e4SLinus Torvalds 
8621da177e4SLinus Torvalds 	return asoc;
8631da177e4SLinus Torvalds }
8641da177e4SLinus Torvalds 
8651da177e4SLinus Torvalds /* Is there an association matching the given local and peer addresses? */
8661da177e4SLinus Torvalds int sctp_has_association(const union sctp_addr *laddr,
8671da177e4SLinus Torvalds 			 const union sctp_addr *paddr)
8681da177e4SLinus Torvalds {
8691da177e4SLinus Torvalds 	struct sctp_association *asoc;
8701da177e4SLinus Torvalds 	struct sctp_transport *transport;
8711da177e4SLinus Torvalds 
8726c7be55cSAl Viro 	if ((asoc = sctp_lookup_association(laddr, paddr, &transport))) {
8731da177e4SLinus Torvalds 		sctp_association_put(asoc);
8741da177e4SLinus Torvalds 		return 1;
8751da177e4SLinus Torvalds 	}
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds 	return 0;
8781da177e4SLinus Torvalds }
8791da177e4SLinus Torvalds 
8801da177e4SLinus Torvalds /*
8811da177e4SLinus Torvalds  * SCTP Implementors Guide, 2.18 Handling of address
8821da177e4SLinus Torvalds  * parameters within the INIT or INIT-ACK.
8831da177e4SLinus Torvalds  *
8841da177e4SLinus Torvalds  * D) When searching for a matching TCB upon reception of an INIT
8851da177e4SLinus Torvalds  *    or INIT-ACK chunk the receiver SHOULD use not only the
8861da177e4SLinus Torvalds  *    source address of the packet (containing the INIT or
8871da177e4SLinus Torvalds  *    INIT-ACK) but the receiver SHOULD also use all valid
8881da177e4SLinus Torvalds  *    address parameters contained within the chunk.
8891da177e4SLinus Torvalds  *
8901da177e4SLinus Torvalds  * 2.18.3 Solution description
8911da177e4SLinus Torvalds  *
8921da177e4SLinus Torvalds  * This new text clearly specifies to an implementor the need
8931da177e4SLinus Torvalds  * to look within the INIT or INIT-ACK. Any implementation that
8941da177e4SLinus Torvalds  * does not do this, may not be able to establish associations
8951da177e4SLinus Torvalds  * in certain circumstances.
8961da177e4SLinus Torvalds  *
8971da177e4SLinus Torvalds  */
8981da177e4SLinus Torvalds static struct sctp_association *__sctp_rcv_init_lookup(struct sk_buff *skb,
8991da177e4SLinus Torvalds 	const union sctp_addr *laddr, struct sctp_transport **transportp)
9001da177e4SLinus Torvalds {
9011da177e4SLinus Torvalds 	struct sctp_association *asoc;
9021da177e4SLinus Torvalds 	union sctp_addr addr;
9031da177e4SLinus Torvalds 	union sctp_addr *paddr = &addr;
9042c0fd387SArnaldo Carvalho de Melo 	struct sctphdr *sh = sctp_hdr(skb);
9051da177e4SLinus Torvalds 	sctp_chunkhdr_t *ch;
9061da177e4SLinus Torvalds 	union sctp_params params;
9071da177e4SLinus Torvalds 	sctp_init_chunk_t *init;
9081da177e4SLinus Torvalds 	struct sctp_transport *transport;
9091da177e4SLinus Torvalds 	struct sctp_af *af;
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds 	ch = (sctp_chunkhdr_t *) skb->data;
9121da177e4SLinus Torvalds 
9131da177e4SLinus Torvalds 	/* If this is INIT/INIT-ACK look inside the chunk too. */
9141da177e4SLinus Torvalds 	switch (ch->type) {
9151da177e4SLinus Torvalds 	case SCTP_CID_INIT:
9161da177e4SLinus Torvalds 	case SCTP_CID_INIT_ACK:
9171da177e4SLinus Torvalds 		break;
9181da177e4SLinus Torvalds 	default:
9191da177e4SLinus Torvalds 		return NULL;
9201da177e4SLinus Torvalds 	}
9211da177e4SLinus Torvalds 
9221da177e4SLinus Torvalds 	/* The code below will attempt to walk the chunk and extract
9231da177e4SLinus Torvalds 	 * parameter information.  Before we do that, we need to verify
9241da177e4SLinus Torvalds 	 * that the chunk length doesn't cause overflow.  Otherwise, we'll
9251da177e4SLinus Torvalds 	 * walk off the end.
9261da177e4SLinus Torvalds 	 */
9271da177e4SLinus Torvalds 	if (WORD_ROUND(ntohs(ch->length)) > skb->len)
9281da177e4SLinus Torvalds 		return NULL;
9291da177e4SLinus Torvalds 
9301da177e4SLinus Torvalds 	/*
9311da177e4SLinus Torvalds 	 * This code will NOT touch anything inside the chunk--it is
9321da177e4SLinus Torvalds 	 * strictly READ-ONLY.
9331da177e4SLinus Torvalds 	 *
9341da177e4SLinus Torvalds 	 * RFC 2960 3  SCTP packet Format
9351da177e4SLinus Torvalds 	 *
9361da177e4SLinus Torvalds 	 * Multiple chunks can be bundled into one SCTP packet up to
9371da177e4SLinus Torvalds 	 * the MTU size, except for the INIT, INIT ACK, and SHUTDOWN
9381da177e4SLinus Torvalds 	 * COMPLETE chunks.  These chunks MUST NOT be bundled with any
9391da177e4SLinus Torvalds 	 * other chunk in a packet.  See Section 6.10 for more details
9401da177e4SLinus Torvalds 	 * on chunk bundling.
9411da177e4SLinus Torvalds 	 */
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds 	/* Find the start of the TLVs and the end of the chunk.  This is
9441da177e4SLinus Torvalds 	 * the region we search for address parameters.
9451da177e4SLinus Torvalds 	 */
9461da177e4SLinus Torvalds 	init = (sctp_init_chunk_t *)skb->data;
9471da177e4SLinus Torvalds 
9481da177e4SLinus Torvalds 	/* Walk the parameters looking for embedded addresses. */
9491da177e4SLinus Torvalds 	sctp_walk_params(params, init, init_hdr.params) {
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds 		/* Note: Ignoring hostname addresses. */
9521da177e4SLinus Torvalds 		af = sctp_get_af_specific(param_type2af(params.p->type));
9531da177e4SLinus Torvalds 		if (!af)
9541da177e4SLinus Torvalds 			continue;
9551da177e4SLinus Torvalds 
956dd86d136SAl Viro 		af->from_addr_param(paddr, params.addr, sh->source, 0);
9571da177e4SLinus Torvalds 
958dd86d136SAl Viro 		asoc = __sctp_lookup_association(laddr, paddr, &transport);
9591da177e4SLinus Torvalds 		if (asoc)
9601da177e4SLinus Torvalds 			return asoc;
9611da177e4SLinus Torvalds 	}
9621da177e4SLinus Torvalds 
9631da177e4SLinus Torvalds 	return NULL;
9641da177e4SLinus Torvalds }
9651da177e4SLinus Torvalds 
9661da177e4SLinus Torvalds /* Lookup an association for an inbound skb. */
9671da177e4SLinus Torvalds static struct sctp_association *__sctp_rcv_lookup(struct sk_buff *skb,
9681da177e4SLinus Torvalds 				      const union sctp_addr *paddr,
9691da177e4SLinus Torvalds 				      const union sctp_addr *laddr,
9701da177e4SLinus Torvalds 				      struct sctp_transport **transportp)
9711da177e4SLinus Torvalds {
9721da177e4SLinus Torvalds 	struct sctp_association *asoc;
9731da177e4SLinus Torvalds 
9741da177e4SLinus Torvalds 	asoc = __sctp_lookup_association(laddr, paddr, transportp);
9751da177e4SLinus Torvalds 
9761da177e4SLinus Torvalds 	/* Further lookup for INIT/INIT-ACK packets.
9771da177e4SLinus Torvalds 	 * SCTP Implementors Guide, 2.18 Handling of address
9781da177e4SLinus Torvalds 	 * parameters within the INIT or INIT-ACK.
9791da177e4SLinus Torvalds 	 */
9801da177e4SLinus Torvalds 	if (!asoc)
9811da177e4SLinus Torvalds 		asoc = __sctp_rcv_init_lookup(skb, laddr, transportp);
9821da177e4SLinus Torvalds 
9831da177e4SLinus Torvalds 	return asoc;
9841da177e4SLinus Torvalds }
985