xref: /linux/net/sctp/input.c (revision 52ccb8e90c0ace233b8b740f2fc5de0dbd706b27)
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 
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds /* Calculate the SCTP checksum of an SCTP packet.  */
781da177e4SLinus Torvalds static inline int sctp_rcv_checksum(struct sk_buff *skb)
791da177e4SLinus Torvalds {
801da177e4SLinus Torvalds 	struct sctphdr *sh;
811da177e4SLinus Torvalds 	__u32 cmp, val;
821da177e4SLinus Torvalds 	struct sk_buff *list = skb_shinfo(skb)->frag_list;
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds 	sh = (struct sctphdr *) skb->h.raw;
851da177e4SLinus Torvalds 	cmp = ntohl(sh->checksum);
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds 	val = sctp_start_cksum((__u8 *)sh, skb_headlen(skb));
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds 	for (; list; list = list->next)
901da177e4SLinus Torvalds 		val = sctp_update_cksum((__u8 *)list->data, skb_headlen(list),
911da177e4SLinus Torvalds 					val);
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds 	val = sctp_end_cksum(val);
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds 	if (val != cmp) {
961da177e4SLinus Torvalds 		/* CRC failure, dump it. */
971da177e4SLinus Torvalds 		SCTP_INC_STATS_BH(SCTP_MIB_CHECKSUMERRORS);
981da177e4SLinus Torvalds 		return -1;
991da177e4SLinus Torvalds 	}
1001da177e4SLinus Torvalds 	return 0;
1011da177e4SLinus Torvalds }
1021da177e4SLinus Torvalds 
10379af02c2SDavid S. Miller struct sctp_input_cb {
10479af02c2SDavid S. Miller 	union {
10579af02c2SDavid S. Miller 		struct inet_skb_parm	h4;
10679af02c2SDavid S. Miller #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
10779af02c2SDavid S. Miller 		struct inet6_skb_parm	h6;
10879af02c2SDavid S. Miller #endif
10979af02c2SDavid S. Miller 	} header;
11079af02c2SDavid S. Miller 	struct sctp_chunk *chunk;
11179af02c2SDavid S. Miller };
11279af02c2SDavid S. Miller #define SCTP_INPUT_CB(__skb)	((struct sctp_input_cb *)&((__skb)->cb[0]))
11379af02c2SDavid S. Miller 
1141da177e4SLinus Torvalds /*
1151da177e4SLinus Torvalds  * This is the routine which IP calls when receiving an SCTP packet.
1161da177e4SLinus Torvalds  */
1171da177e4SLinus Torvalds int sctp_rcv(struct sk_buff *skb)
1181da177e4SLinus Torvalds {
1191da177e4SLinus Torvalds 	struct sock *sk;
1201da177e4SLinus Torvalds 	struct sctp_association *asoc;
1211da177e4SLinus Torvalds 	struct sctp_endpoint *ep = NULL;
1221da177e4SLinus Torvalds 	struct sctp_ep_common *rcvr;
1231da177e4SLinus Torvalds 	struct sctp_transport *transport = NULL;
1241da177e4SLinus Torvalds 	struct sctp_chunk *chunk;
1251da177e4SLinus Torvalds 	struct sctphdr *sh;
1261da177e4SLinus Torvalds 	union sctp_addr src;
1271da177e4SLinus Torvalds 	union sctp_addr dest;
1281da177e4SLinus Torvalds 	int family;
1291da177e4SLinus Torvalds 	struct sctp_af *af;
1301da177e4SLinus Torvalds 	int ret = 0;
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds 	if (skb->pkt_type!=PACKET_HOST)
1331da177e4SLinus Torvalds 		goto discard_it;
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds 	SCTP_INC_STATS_BH(SCTP_MIB_INSCTPPACKS);
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds 	sh = (struct sctphdr *) skb->h.raw;
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds 	/* Pull up the IP and SCTP headers. */
1401da177e4SLinus Torvalds 	__skb_pull(skb, skb->h.raw - skb->data);
1411da177e4SLinus Torvalds 	if (skb->len < sizeof(struct sctphdr))
1421da177e4SLinus Torvalds 		goto discard_it;
1431da177e4SLinus Torvalds 	if (sctp_rcv_checksum(skb) < 0)
1441da177e4SLinus Torvalds 		goto discard_it;
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds 	skb_pull(skb, sizeof(struct sctphdr));
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds 	/* Make sure we at least have chunk headers worth of data left. */
1491da177e4SLinus Torvalds 	if (skb->len < sizeof(struct sctp_chunkhdr))
1501da177e4SLinus Torvalds 		goto discard_it;
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 	family = ipver2af(skb->nh.iph->version);
1531da177e4SLinus Torvalds 	af = sctp_get_af_specific(family);
1541da177e4SLinus Torvalds 	if (unlikely(!af))
1551da177e4SLinus Torvalds 		goto discard_it;
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds 	/* Initialize local addresses for lookups. */
1581da177e4SLinus Torvalds 	af->from_skb(&src, skb, 1);
1591da177e4SLinus Torvalds 	af->from_skb(&dest, skb, 0);
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds 	/* If the packet is to or from a non-unicast address,
1621da177e4SLinus Torvalds 	 * silently discard the packet.
1631da177e4SLinus Torvalds 	 *
1641da177e4SLinus Torvalds 	 * This is not clearly defined in the RFC except in section
1651da177e4SLinus Torvalds 	 * 8.4 - OOTB handling.  However, based on the book "Stream Control
1661da177e4SLinus Torvalds 	 * Transmission Protocol" 2.1, "It is important to note that the
1671da177e4SLinus Torvalds 	 * IP address of an SCTP transport address must be a routable
1681da177e4SLinus Torvalds 	 * unicast address.  In other words, IP multicast addresses and
1691da177e4SLinus Torvalds 	 * IP broadcast addresses cannot be used in an SCTP transport
1701da177e4SLinus Torvalds 	 * address."
1711da177e4SLinus Torvalds 	 */
1721da177e4SLinus Torvalds 	if (!af->addr_valid(&src, NULL) || !af->addr_valid(&dest, NULL))
1731da177e4SLinus Torvalds 		goto discard_it;
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds 	asoc = __sctp_rcv_lookup(skb, &src, &dest, &transport);
1761da177e4SLinus Torvalds 
1770fd9a65aSNeil Horman 	if (!asoc)
1780fd9a65aSNeil Horman 		ep = __sctp_rcv_lookup_endpoint(&dest);
1790fd9a65aSNeil Horman 
1800fd9a65aSNeil Horman 	/* Retrieve the common input handling substructure. */
1810fd9a65aSNeil Horman 	rcvr = asoc ? &asoc->base : &ep->base;
1820fd9a65aSNeil Horman 	sk = rcvr->sk;
1830fd9a65aSNeil Horman 
1840fd9a65aSNeil Horman 	/*
1850fd9a65aSNeil Horman 	 * If a frame arrives on an interface and the receiving socket is
1860fd9a65aSNeil Horman 	 * bound to another interface, via SO_BINDTODEVICE, treat it as OOTB
1870fd9a65aSNeil Horman 	 */
1880fd9a65aSNeil Horman 	if (sk->sk_bound_dev_if && (sk->sk_bound_dev_if != af->skb_iif(skb)))
1890fd9a65aSNeil Horman 	{
1900fd9a65aSNeil Horman 		sock_put(sk);
1910fd9a65aSNeil Horman 		if (asoc) {
1920fd9a65aSNeil Horman 			sctp_association_put(asoc);
1930fd9a65aSNeil Horman 			asoc = NULL;
1940fd9a65aSNeil Horman 		} else {
1950fd9a65aSNeil Horman 			sctp_endpoint_put(ep);
1960fd9a65aSNeil Horman 			ep = NULL;
1970fd9a65aSNeil Horman 		}
1980fd9a65aSNeil Horman 		sk = sctp_get_ctl_sock();
1990fd9a65aSNeil Horman 		ep = sctp_sk(sk)->ep;
2000fd9a65aSNeil Horman 		sctp_endpoint_hold(ep);
2010fd9a65aSNeil Horman 		sock_hold(sk);
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 	/* SCTP seems to always need a timestamp right now (FIXME) */
221a61bbcf2SPatrick McHardy 	if (skb->tstamp.off_sec == 0) {
222a61bbcf2SPatrick McHardy 		__net_timestamp(skb);
2231da177e4SLinus Torvalds 		sock_enable_timestamp(sk);
2241da177e4SLinus Torvalds 	}
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds 	if (!xfrm_policy_check(sk, XFRM_POLICY_IN, skb, family))
2271da177e4SLinus Torvalds 		goto discard_release;
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds 	ret = sk_filter(sk, skb, 1);
2301da177e4SLinus Torvalds 	if (ret)
2311da177e4SLinus Torvalds                 goto discard_release;
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds 	/* Create an SCTP packet structure. */
2341da177e4SLinus Torvalds 	chunk = sctp_chunkify(skb, asoc, sk);
2351da177e4SLinus Torvalds 	if (!chunk) {
2361da177e4SLinus Torvalds 		ret = -ENOMEM;
2371da177e4SLinus Torvalds 		goto discard_release;
2381da177e4SLinus Torvalds 	}
23979af02c2SDavid S. Miller 	SCTP_INPUT_CB(skb)->chunk = chunk;
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 	/* Remember what endpoint is to handle this packet. */
2421da177e4SLinus Torvalds 	chunk->rcvr = rcvr;
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 	/* Remember the SCTP header. */
2451da177e4SLinus Torvalds 	chunk->sctp_hdr = sh;
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds 	/* Set the source and destination addresses of the incoming chunk.  */
2481da177e4SLinus Torvalds 	sctp_init_addrs(chunk, &src, &dest);
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds 	/* Remember where we came from.  */
2511da177e4SLinus Torvalds 	chunk->transport = transport;
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds 	/* Acquire access to the sock lock. Note: We are safe from other
2541da177e4SLinus Torvalds 	 * bottom halves on this lock, but a user may be in the lock too,
2551da177e4SLinus Torvalds 	 * so check if it is busy.
2561da177e4SLinus Torvalds 	 */
2571da177e4SLinus Torvalds 	sctp_bh_lock_sock(sk);
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds 	if (sock_owned_by_user(sk))
26079af02c2SDavid S. Miller 		sk_add_backlog(sk, skb);
2611da177e4SLinus Torvalds 	else
26279af02c2SDavid S. Miller 		sctp_backlog_rcv(sk, skb);
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 	/* Release the sock and any reference counts we took in the
2651da177e4SLinus Torvalds 	 * lookup calls.
2661da177e4SLinus Torvalds 	 */
2671da177e4SLinus Torvalds 	sctp_bh_unlock_sock(sk);
2681da177e4SLinus Torvalds 	if (asoc)
2691da177e4SLinus Torvalds 		sctp_association_put(asoc);
2701da177e4SLinus Torvalds 	else
2711da177e4SLinus Torvalds 		sctp_endpoint_put(ep);
2721da177e4SLinus Torvalds 	sock_put(sk);
2731da177e4SLinus Torvalds 	return ret;
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds discard_it:
2761da177e4SLinus Torvalds 	kfree_skb(skb);
2771da177e4SLinus Torvalds 	return ret;
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds discard_release:
2801da177e4SLinus Torvalds 	/* Release any structures we may be holding. */
2810fd9a65aSNeil Horman 	sock_put(sk);
2820fd9a65aSNeil Horman 	if (asoc)
2831da177e4SLinus Torvalds 		sctp_association_put(asoc);
2840fd9a65aSNeil Horman 	else
2851da177e4SLinus Torvalds 		sctp_endpoint_put(ep);
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds 	goto discard_it;
2881da177e4SLinus Torvalds }
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds /* Handle second half of inbound skb processing.  If the sock was busy,
2911da177e4SLinus Torvalds  * we may have need to delay processing until later when the sock is
2921da177e4SLinus Torvalds  * released (on the backlog).   If not busy, we call this routine
2931da177e4SLinus Torvalds  * directly from the bottom half.
2941da177e4SLinus Torvalds  */
2951da177e4SLinus Torvalds int sctp_backlog_rcv(struct sock *sk, struct sk_buff *skb)
2961da177e4SLinus Torvalds {
29779af02c2SDavid S. Miller 	struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
29879af02c2SDavid S. Miller 	struct sctp_inq *inqueue = &chunk->rcvr->inqueue;
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds 	sctp_inq_push(inqueue, chunk);
3011da177e4SLinus Torvalds         return 0;
3021da177e4SLinus Torvalds }
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds /* Handle icmp frag needed error. */
3051da177e4SLinus Torvalds void sctp_icmp_frag_needed(struct sock *sk, struct sctp_association *asoc,
3061da177e4SLinus Torvalds 			   struct sctp_transport *t, __u32 pmtu)
3071da177e4SLinus Torvalds {
308*52ccb8e9SFrank Filz 	if (sock_owned_by_user(sk) || !t || (t->pathmtu == pmtu))
309*52ccb8e9SFrank Filz 		return;
310*52ccb8e9SFrank Filz 
311*52ccb8e9SFrank Filz 	if (t->param_flags & SPP_PMTUD_ENABLE) {
3121da177e4SLinus Torvalds 		if (unlikely(pmtu < SCTP_DEFAULT_MINSEGMENT)) {
3131da177e4SLinus Torvalds 			printk(KERN_WARNING "%s: Reported pmtu %d too low, "
314*52ccb8e9SFrank Filz 			       "using default minimum of %d\n",
315*52ccb8e9SFrank Filz 			       __FUNCTION__, pmtu,
3161da177e4SLinus Torvalds 			       SCTP_DEFAULT_MINSEGMENT);
317*52ccb8e9SFrank Filz 			/* Use default minimum segment size and disable
318*52ccb8e9SFrank Filz 			 * pmtu discovery on this transport.
319*52ccb8e9SFrank Filz 			 */
320*52ccb8e9SFrank Filz 			t->pathmtu = SCTP_DEFAULT_MINSEGMENT;
321*52ccb8e9SFrank Filz 			t->param_flags = (t->param_flags & ~SPP_HB) |
322*52ccb8e9SFrank Filz 				SPP_PMTUD_DISABLE;
323*52ccb8e9SFrank Filz 		} else {
324*52ccb8e9SFrank Filz 			t->pathmtu = pmtu;
3251da177e4SLinus Torvalds 		}
3261da177e4SLinus Torvalds 
327*52ccb8e9SFrank Filz 		/* Update association pmtu. */
3281da177e4SLinus Torvalds 		sctp_assoc_sync_pmtu(asoc);
3291da177e4SLinus Torvalds 	}
330*52ccb8e9SFrank Filz 
331*52ccb8e9SFrank Filz 	/* Retransmit with the new pmtu setting.
332*52ccb8e9SFrank Filz 	 * Normally, if PMTU discovery is disabled, an ICMP Fragmentation
333*52ccb8e9SFrank Filz 	 * Needed will never be sent, but if a message was sent before
334*52ccb8e9SFrank Filz 	 * PMTU discovery was disabled that was larger than the PMTU, it
335*52ccb8e9SFrank Filz 	 * would not be fragmented, so it must be re-transmitted fragmented.
336*52ccb8e9SFrank Filz 	 */
337*52ccb8e9SFrank Filz 	sctp_retransmit(&asoc->outqueue, t, SCTP_RTXR_PMTUD);
3381da177e4SLinus Torvalds }
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds /*
3411da177e4SLinus Torvalds  * SCTP Implementer's Guide, 2.37 ICMP handling procedures
3421da177e4SLinus Torvalds  *
3431da177e4SLinus Torvalds  * ICMP8) If the ICMP code is a "Unrecognized next header type encountered"
3441da177e4SLinus Torvalds  *        or a "Protocol Unreachable" treat this message as an abort
3451da177e4SLinus Torvalds  *        with the T bit set.
3461da177e4SLinus Torvalds  *
3471da177e4SLinus Torvalds  * This function sends an event to the state machine, which will abort the
3481da177e4SLinus Torvalds  * association.
3491da177e4SLinus Torvalds  *
3501da177e4SLinus Torvalds  */
3511da177e4SLinus Torvalds void sctp_icmp_proto_unreachable(struct sock *sk,
3521da177e4SLinus Torvalds                            struct sctp_association *asoc,
3531da177e4SLinus Torvalds                            struct sctp_transport *t)
3541da177e4SLinus Torvalds {
3551da177e4SLinus Torvalds 	SCTP_DEBUG_PRINTK("%s\n",  __FUNCTION__);
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 	sctp_do_sm(SCTP_EVENT_T_OTHER,
3581da177e4SLinus Torvalds 		   SCTP_ST_OTHER(SCTP_EVENT_ICMP_PROTO_UNREACH),
3593f7a87d2SFrank Filz 		   asoc->state, asoc->ep, asoc, t,
3601da177e4SLinus Torvalds 		   GFP_ATOMIC);
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds }
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds /* Common lookup code for icmp/icmpv6 error handler. */
3651da177e4SLinus Torvalds struct sock *sctp_err_lookup(int family, struct sk_buff *skb,
3661da177e4SLinus Torvalds 			     struct sctphdr *sctphdr,
3671da177e4SLinus Torvalds 			     struct sctp_association **app,
3681da177e4SLinus Torvalds 			     struct sctp_transport **tpp)
3691da177e4SLinus Torvalds {
3701da177e4SLinus Torvalds 	union sctp_addr saddr;
3711da177e4SLinus Torvalds 	union sctp_addr daddr;
3721da177e4SLinus Torvalds 	struct sctp_af *af;
3731da177e4SLinus Torvalds 	struct sock *sk = NULL;
3741da177e4SLinus Torvalds 	struct sctp_association *asoc = NULL;
3751da177e4SLinus Torvalds 	struct sctp_transport *transport = NULL;
3761da177e4SLinus Torvalds 
377d1ad1ff2SSridhar Samudrala 	*app = NULL; *tpp = NULL;
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 	af = sctp_get_af_specific(family);
3801da177e4SLinus Torvalds 	if (unlikely(!af)) {
3811da177e4SLinus Torvalds 		return NULL;
3821da177e4SLinus Torvalds 	}
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 	/* Initialize local addresses for lookups. */
3851da177e4SLinus Torvalds 	af->from_skb(&saddr, skb, 1);
3861da177e4SLinus Torvalds 	af->from_skb(&daddr, skb, 0);
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds 	/* Look for an association that matches the incoming ICMP error
3891da177e4SLinus Torvalds 	 * packet.
3901da177e4SLinus Torvalds 	 */
3911da177e4SLinus Torvalds 	asoc = __sctp_lookup_association(&saddr, &daddr, &transport);
392d1ad1ff2SSridhar Samudrala 	if (!asoc)
3931da177e4SLinus Torvalds 		return NULL;
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds 	sk = asoc->base.sk;
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 	if (ntohl(sctphdr->vtag) != asoc->c.peer_vtag) {
3981da177e4SLinus Torvalds 		ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
3991da177e4SLinus Torvalds 		goto out;
4001da177e4SLinus Torvalds 	}
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds 	sctp_bh_lock_sock(sk);
4031da177e4SLinus Torvalds 
4041da177e4SLinus Torvalds 	/* If too many ICMPs get dropped on busy
4051da177e4SLinus Torvalds 	 * servers this needs to be solved differently.
4061da177e4SLinus Torvalds 	 */
4071da177e4SLinus Torvalds 	if (sock_owned_by_user(sk))
4081da177e4SLinus Torvalds 		NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 	*app = asoc;
4111da177e4SLinus Torvalds 	*tpp = transport;
4121da177e4SLinus Torvalds 	return sk;
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds out:
4151da177e4SLinus Torvalds 	sock_put(sk);
4161da177e4SLinus Torvalds 	if (asoc)
4171da177e4SLinus Torvalds 		sctp_association_put(asoc);
4181da177e4SLinus Torvalds 	return NULL;
4191da177e4SLinus Torvalds }
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds /* Common cleanup code for icmp/icmpv6 error handler. */
422d1ad1ff2SSridhar Samudrala void sctp_err_finish(struct sock *sk, struct sctp_association *asoc)
4231da177e4SLinus Torvalds {
4241da177e4SLinus Torvalds 	sctp_bh_unlock_sock(sk);
4251da177e4SLinus Torvalds 	sock_put(sk);
4261da177e4SLinus Torvalds 	if (asoc)
4271da177e4SLinus Torvalds 		sctp_association_put(asoc);
4281da177e4SLinus Torvalds }
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds /*
4311da177e4SLinus Torvalds  * This routine is called by the ICMP module when it gets some
4321da177e4SLinus Torvalds  * sort of error condition.  If err < 0 then the socket should
4331da177e4SLinus Torvalds  * be closed and the error returned to the user.  If err > 0
4341da177e4SLinus Torvalds  * it's just the icmp type << 8 | icmp code.  After adjustment
4351da177e4SLinus Torvalds  * header points to the first 8 bytes of the sctp header.  We need
4361da177e4SLinus Torvalds  * to find the appropriate port.
4371da177e4SLinus Torvalds  *
4381da177e4SLinus Torvalds  * The locking strategy used here is very "optimistic". When
4391da177e4SLinus Torvalds  * someone else accesses the socket the ICMP is just dropped
4401da177e4SLinus Torvalds  * and for some paths there is no check at all.
4411da177e4SLinus Torvalds  * A more general error queue to queue errors for later handling
4421da177e4SLinus Torvalds  * is probably better.
4431da177e4SLinus Torvalds  *
4441da177e4SLinus Torvalds  */
4451da177e4SLinus Torvalds void sctp_v4_err(struct sk_buff *skb, __u32 info)
4461da177e4SLinus Torvalds {
4471da177e4SLinus Torvalds 	struct iphdr *iph = (struct iphdr *)skb->data;
4481da177e4SLinus Torvalds 	struct sctphdr *sh = (struct sctphdr *)(skb->data + (iph->ihl <<2));
4491da177e4SLinus Torvalds 	int type = skb->h.icmph->type;
4501da177e4SLinus Torvalds 	int code = skb->h.icmph->code;
4511da177e4SLinus Torvalds 	struct sock *sk;
4521da177e4SLinus Torvalds 	struct sctp_association *asoc;
4531da177e4SLinus Torvalds 	struct sctp_transport *transport;
4541da177e4SLinus Torvalds 	struct inet_sock *inet;
4551da177e4SLinus Torvalds 	char *saveip, *savesctp;
4561da177e4SLinus Torvalds 	int err;
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds 	if (skb->len < ((iph->ihl << 2) + 8)) {
4591da177e4SLinus Torvalds 		ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
4601da177e4SLinus Torvalds 		return;
4611da177e4SLinus Torvalds 	}
4621da177e4SLinus Torvalds 
4631da177e4SLinus Torvalds 	/* Fix up skb to look at the embedded net header. */
4641da177e4SLinus Torvalds 	saveip = skb->nh.raw;
4651da177e4SLinus Torvalds 	savesctp  = skb->h.raw;
4661da177e4SLinus Torvalds 	skb->nh.iph = iph;
4671da177e4SLinus Torvalds 	skb->h.raw = (char *)sh;
468d1ad1ff2SSridhar Samudrala 	sk = sctp_err_lookup(AF_INET, skb, sh, &asoc, &transport);
4691da177e4SLinus Torvalds 	/* Put back, the original pointers. */
4701da177e4SLinus Torvalds 	skb->nh.raw = saveip;
4711da177e4SLinus Torvalds 	skb->h.raw = savesctp;
4721da177e4SLinus Torvalds 	if (!sk) {
4731da177e4SLinus Torvalds 		ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
4741da177e4SLinus Torvalds 		return;
4751da177e4SLinus Torvalds 	}
4761da177e4SLinus Torvalds 	/* Warning:  The sock lock is held.  Remember to call
4771da177e4SLinus Torvalds 	 * sctp_err_finish!
4781da177e4SLinus Torvalds 	 */
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds 	switch (type) {
4811da177e4SLinus Torvalds 	case ICMP_PARAMETERPROB:
4821da177e4SLinus Torvalds 		err = EPROTO;
4831da177e4SLinus Torvalds 		break;
4841da177e4SLinus Torvalds 	case ICMP_DEST_UNREACH:
4851da177e4SLinus Torvalds 		if (code > NR_ICMP_UNREACH)
4861da177e4SLinus Torvalds 			goto out_unlock;
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds 		/* PMTU discovery (RFC1191) */
4891da177e4SLinus Torvalds 		if (ICMP_FRAG_NEEDED == code) {
4901da177e4SLinus Torvalds 			sctp_icmp_frag_needed(sk, asoc, transport, info);
4911da177e4SLinus Torvalds 			goto out_unlock;
4921da177e4SLinus Torvalds 		}
4931da177e4SLinus Torvalds 		else {
4941da177e4SLinus Torvalds 			if (ICMP_PROT_UNREACH == code) {
495d1ad1ff2SSridhar Samudrala 				sctp_icmp_proto_unreachable(sk, asoc,
4961da177e4SLinus Torvalds 							    transport);
4971da177e4SLinus Torvalds 				goto out_unlock;
4981da177e4SLinus Torvalds 			}
4991da177e4SLinus Torvalds 		}
5001da177e4SLinus Torvalds 		err = icmp_err_convert[code].errno;
5011da177e4SLinus Torvalds 		break;
5021da177e4SLinus Torvalds 	case ICMP_TIME_EXCEEDED:
5031da177e4SLinus Torvalds 		/* Ignore any time exceeded errors due to fragment reassembly
5041da177e4SLinus Torvalds 		 * timeouts.
5051da177e4SLinus Torvalds 		 */
5061da177e4SLinus Torvalds 		if (ICMP_EXC_FRAGTIME == code)
5071da177e4SLinus Torvalds 			goto out_unlock;
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds 		err = EHOSTUNREACH;
5101da177e4SLinus Torvalds 		break;
5111da177e4SLinus Torvalds 	default:
5121da177e4SLinus Torvalds 		goto out_unlock;
5131da177e4SLinus Torvalds 	}
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds 	inet = inet_sk(sk);
5161da177e4SLinus Torvalds 	if (!sock_owned_by_user(sk) && inet->recverr) {
5171da177e4SLinus Torvalds 		sk->sk_err = err;
5181da177e4SLinus Torvalds 		sk->sk_error_report(sk);
5191da177e4SLinus Torvalds 	} else {  /* Only an error on timeout */
5201da177e4SLinus Torvalds 		sk->sk_err_soft = err;
5211da177e4SLinus Torvalds 	}
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds out_unlock:
524d1ad1ff2SSridhar Samudrala 	sctp_err_finish(sk, asoc);
5251da177e4SLinus Torvalds }
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds /*
5281da177e4SLinus Torvalds  * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
5291da177e4SLinus Torvalds  *
5301da177e4SLinus Torvalds  * This function scans all the chunks in the OOTB packet to determine if
5311da177e4SLinus Torvalds  * the packet should be discarded right away.  If a response might be needed
5321da177e4SLinus Torvalds  * for this packet, or, if further processing is possible, the packet will
5331da177e4SLinus Torvalds  * be queued to a proper inqueue for the next phase of handling.
5341da177e4SLinus Torvalds  *
5351da177e4SLinus Torvalds  * Output:
5361da177e4SLinus Torvalds  * Return 0 - If further processing is needed.
5371da177e4SLinus Torvalds  * Return 1 - If the packet can be discarded right away.
5381da177e4SLinus Torvalds  */
5391da177e4SLinus Torvalds int sctp_rcv_ootb(struct sk_buff *skb)
5401da177e4SLinus Torvalds {
5411da177e4SLinus Torvalds 	sctp_chunkhdr_t *ch;
5421da177e4SLinus Torvalds 	__u8 *ch_end;
5431da177e4SLinus Torvalds 	sctp_errhdr_t *err;
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds 	ch = (sctp_chunkhdr_t *) skb->data;
5461da177e4SLinus Torvalds 	ch_end = ((__u8 *) ch) + WORD_ROUND(ntohs(ch->length));
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds 	/* Scan through all the chunks in the packet.  */
5491da177e4SLinus Torvalds 	while (ch_end > (__u8 *)ch && ch_end < skb->tail) {
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds 		/* RFC 8.4, 2) If the OOTB packet contains an ABORT chunk, the
5521da177e4SLinus Torvalds 		 * receiver MUST silently discard the OOTB packet and take no
5531da177e4SLinus Torvalds 		 * further action.
5541da177e4SLinus Torvalds 		 */
5551da177e4SLinus Torvalds 		if (SCTP_CID_ABORT == ch->type)
5561da177e4SLinus Torvalds 			goto discard;
5571da177e4SLinus Torvalds 
5581da177e4SLinus Torvalds 		/* RFC 8.4, 6) If the packet contains a SHUTDOWN COMPLETE
5591da177e4SLinus Torvalds 		 * chunk, the receiver should silently discard the packet
5601da177e4SLinus Torvalds 		 * and take no further action.
5611da177e4SLinus Torvalds 		 */
5621da177e4SLinus Torvalds 		if (SCTP_CID_SHUTDOWN_COMPLETE == ch->type)
5631da177e4SLinus Torvalds 			goto discard;
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 		/* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
5661da177e4SLinus Torvalds 		 * or a COOKIE ACK the SCTP Packet should be silently
5671da177e4SLinus Torvalds 		 * discarded.
5681da177e4SLinus Torvalds 		 */
5691da177e4SLinus Torvalds 		if (SCTP_CID_COOKIE_ACK == ch->type)
5701da177e4SLinus Torvalds 			goto discard;
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 		if (SCTP_CID_ERROR == ch->type) {
5731da177e4SLinus Torvalds 			sctp_walk_errors(err, ch) {
5741da177e4SLinus Torvalds 				if (SCTP_ERROR_STALE_COOKIE == err->cause)
5751da177e4SLinus Torvalds 					goto discard;
5761da177e4SLinus Torvalds 			}
5771da177e4SLinus Torvalds 		}
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds 		ch = (sctp_chunkhdr_t *) ch_end;
5801da177e4SLinus Torvalds 	        ch_end = ((__u8 *) ch) + WORD_ROUND(ntohs(ch->length));
5811da177e4SLinus Torvalds 	}
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds 	return 0;
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds discard:
5861da177e4SLinus Torvalds 	return 1;
5871da177e4SLinus Torvalds }
5881da177e4SLinus Torvalds 
5891da177e4SLinus Torvalds /* Insert endpoint into the hash table.  */
5901da177e4SLinus Torvalds static void __sctp_hash_endpoint(struct sctp_endpoint *ep)
5911da177e4SLinus Torvalds {
5921da177e4SLinus Torvalds 	struct sctp_ep_common **epp;
5931da177e4SLinus Torvalds 	struct sctp_ep_common *epb;
5941da177e4SLinus Torvalds 	struct sctp_hashbucket *head;
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds 	epb = &ep->base;
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds 	epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);
5991da177e4SLinus Torvalds 	head = &sctp_ep_hashtable[epb->hashent];
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds 	sctp_write_lock(&head->lock);
6021da177e4SLinus Torvalds 	epp = &head->chain;
6031da177e4SLinus Torvalds 	epb->next = *epp;
6041da177e4SLinus Torvalds 	if (epb->next)
6051da177e4SLinus Torvalds 		(*epp)->pprev = &epb->next;
6061da177e4SLinus Torvalds 	*epp = epb;
6071da177e4SLinus Torvalds 	epb->pprev = epp;
6081da177e4SLinus Torvalds 	sctp_write_unlock(&head->lock);
6091da177e4SLinus Torvalds }
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds /* Add an endpoint to the hash. Local BH-safe. */
6121da177e4SLinus Torvalds void sctp_hash_endpoint(struct sctp_endpoint *ep)
6131da177e4SLinus Torvalds {
6141da177e4SLinus Torvalds 	sctp_local_bh_disable();
6151da177e4SLinus Torvalds 	__sctp_hash_endpoint(ep);
6161da177e4SLinus Torvalds 	sctp_local_bh_enable();
6171da177e4SLinus Torvalds }
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds /* Remove endpoint from the hash table.  */
6201da177e4SLinus Torvalds static void __sctp_unhash_endpoint(struct sctp_endpoint *ep)
6211da177e4SLinus Torvalds {
6221da177e4SLinus Torvalds 	struct sctp_hashbucket *head;
6231da177e4SLinus Torvalds 	struct sctp_ep_common *epb;
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds 	epb = &ep->base;
6261da177e4SLinus Torvalds 
6271da177e4SLinus Torvalds 	epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds 	head = &sctp_ep_hashtable[epb->hashent];
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds 	sctp_write_lock(&head->lock);
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds 	if (epb->pprev) {
6341da177e4SLinus Torvalds 		if (epb->next)
6351da177e4SLinus Torvalds 			epb->next->pprev = epb->pprev;
6361da177e4SLinus Torvalds 		*epb->pprev = epb->next;
6371da177e4SLinus Torvalds 		epb->pprev = NULL;
6381da177e4SLinus Torvalds 	}
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds 	sctp_write_unlock(&head->lock);
6411da177e4SLinus Torvalds }
6421da177e4SLinus Torvalds 
6431da177e4SLinus Torvalds /* Remove endpoint from the hash.  Local BH-safe. */
6441da177e4SLinus Torvalds void sctp_unhash_endpoint(struct sctp_endpoint *ep)
6451da177e4SLinus Torvalds {
6461da177e4SLinus Torvalds 	sctp_local_bh_disable();
6471da177e4SLinus Torvalds 	__sctp_unhash_endpoint(ep);
6481da177e4SLinus Torvalds 	sctp_local_bh_enable();
6491da177e4SLinus Torvalds }
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds /* Look up an endpoint. */
6521da177e4SLinus Torvalds static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(const union sctp_addr *laddr)
6531da177e4SLinus Torvalds {
6541da177e4SLinus Torvalds 	struct sctp_hashbucket *head;
6551da177e4SLinus Torvalds 	struct sctp_ep_common *epb;
6561da177e4SLinus Torvalds 	struct sctp_endpoint *ep;
6571da177e4SLinus Torvalds 	int hash;
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds 	hash = sctp_ep_hashfn(laddr->v4.sin_port);
6601da177e4SLinus Torvalds 	head = &sctp_ep_hashtable[hash];
6611da177e4SLinus Torvalds 	read_lock(&head->lock);
6621da177e4SLinus Torvalds 	for (epb = head->chain; epb; epb = epb->next) {
6631da177e4SLinus Torvalds 		ep = sctp_ep(epb);
6641da177e4SLinus Torvalds 		if (sctp_endpoint_is_match(ep, laddr))
6651da177e4SLinus Torvalds 			goto hit;
6661da177e4SLinus Torvalds 	}
6671da177e4SLinus Torvalds 
6681da177e4SLinus Torvalds 	ep = sctp_sk((sctp_get_ctl_sock()))->ep;
6691da177e4SLinus Torvalds 	epb = &ep->base;
6701da177e4SLinus Torvalds 
6711da177e4SLinus Torvalds hit:
6721da177e4SLinus Torvalds 	sctp_endpoint_hold(ep);
6731da177e4SLinus Torvalds 	sock_hold(epb->sk);
6741da177e4SLinus Torvalds 	read_unlock(&head->lock);
6751da177e4SLinus Torvalds 	return ep;
6761da177e4SLinus Torvalds }
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds /* Insert association into the hash table.  */
6791da177e4SLinus Torvalds static void __sctp_hash_established(struct sctp_association *asoc)
6801da177e4SLinus Torvalds {
6811da177e4SLinus Torvalds 	struct sctp_ep_common **epp;
6821da177e4SLinus Torvalds 	struct sctp_ep_common *epb;
6831da177e4SLinus Torvalds 	struct sctp_hashbucket *head;
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds 	epb = &asoc->base;
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds 	/* Calculate which chain this entry will belong to. */
6881da177e4SLinus Torvalds 	epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port, asoc->peer.port);
6891da177e4SLinus Torvalds 
6901da177e4SLinus Torvalds 	head = &sctp_assoc_hashtable[epb->hashent];
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds 	sctp_write_lock(&head->lock);
6931da177e4SLinus Torvalds 	epp = &head->chain;
6941da177e4SLinus Torvalds 	epb->next = *epp;
6951da177e4SLinus Torvalds 	if (epb->next)
6961da177e4SLinus Torvalds 		(*epp)->pprev = &epb->next;
6971da177e4SLinus Torvalds 	*epp = epb;
6981da177e4SLinus Torvalds 	epb->pprev = epp;
6991da177e4SLinus Torvalds 	sctp_write_unlock(&head->lock);
7001da177e4SLinus Torvalds }
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds /* Add an association to the hash. Local BH-safe. */
7031da177e4SLinus Torvalds void sctp_hash_established(struct sctp_association *asoc)
7041da177e4SLinus Torvalds {
7051da177e4SLinus Torvalds 	sctp_local_bh_disable();
7061da177e4SLinus Torvalds 	__sctp_hash_established(asoc);
7071da177e4SLinus Torvalds 	sctp_local_bh_enable();
7081da177e4SLinus Torvalds }
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds /* Remove association from the hash table.  */
7111da177e4SLinus Torvalds static void __sctp_unhash_established(struct sctp_association *asoc)
7121da177e4SLinus Torvalds {
7131da177e4SLinus Torvalds 	struct sctp_hashbucket *head;
7141da177e4SLinus Torvalds 	struct sctp_ep_common *epb;
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds 	epb = &asoc->base;
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds 	epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port,
7191da177e4SLinus Torvalds 					 asoc->peer.port);
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds 	head = &sctp_assoc_hashtable[epb->hashent];
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds 	sctp_write_lock(&head->lock);
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds 	if (epb->pprev) {
7261da177e4SLinus Torvalds 		if (epb->next)
7271da177e4SLinus Torvalds 			epb->next->pprev = epb->pprev;
7281da177e4SLinus Torvalds 		*epb->pprev = epb->next;
7291da177e4SLinus Torvalds 		epb->pprev = NULL;
7301da177e4SLinus Torvalds 	}
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds 	sctp_write_unlock(&head->lock);
7331da177e4SLinus Torvalds }
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds /* Remove association from the hash table.  Local BH-safe. */
7361da177e4SLinus Torvalds void sctp_unhash_established(struct sctp_association *asoc)
7371da177e4SLinus Torvalds {
7381da177e4SLinus Torvalds 	sctp_local_bh_disable();
7391da177e4SLinus Torvalds 	__sctp_unhash_established(asoc);
7401da177e4SLinus Torvalds 	sctp_local_bh_enable();
7411da177e4SLinus Torvalds }
7421da177e4SLinus Torvalds 
7431da177e4SLinus Torvalds /* Look up an association. */
7441da177e4SLinus Torvalds static struct sctp_association *__sctp_lookup_association(
7451da177e4SLinus Torvalds 					const union sctp_addr *local,
7461da177e4SLinus Torvalds 					const union sctp_addr *peer,
7471da177e4SLinus Torvalds 					struct sctp_transport **pt)
7481da177e4SLinus Torvalds {
7491da177e4SLinus Torvalds 	struct sctp_hashbucket *head;
7501da177e4SLinus Torvalds 	struct sctp_ep_common *epb;
7511da177e4SLinus Torvalds 	struct sctp_association *asoc;
7521da177e4SLinus Torvalds 	struct sctp_transport *transport;
7531da177e4SLinus Torvalds 	int hash;
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 	/* Optimize here for direct hit, only listening connections can
7561da177e4SLinus Torvalds 	 * have wildcards anyways.
7571da177e4SLinus Torvalds 	 */
7581da177e4SLinus Torvalds 	hash = sctp_assoc_hashfn(local->v4.sin_port, peer->v4.sin_port);
7591da177e4SLinus Torvalds 	head = &sctp_assoc_hashtable[hash];
7601da177e4SLinus Torvalds 	read_lock(&head->lock);
7611da177e4SLinus Torvalds 	for (epb = head->chain; epb; epb = epb->next) {
7621da177e4SLinus Torvalds 		asoc = sctp_assoc(epb);
7631da177e4SLinus Torvalds 		transport = sctp_assoc_is_match(asoc, local, peer);
7641da177e4SLinus Torvalds 		if (transport)
7651da177e4SLinus Torvalds 			goto hit;
7661da177e4SLinus Torvalds 	}
7671da177e4SLinus Torvalds 
7681da177e4SLinus Torvalds 	read_unlock(&head->lock);
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds 	return NULL;
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds hit:
7731da177e4SLinus Torvalds 	*pt = transport;
7741da177e4SLinus Torvalds 	sctp_association_hold(asoc);
7751da177e4SLinus Torvalds 	sock_hold(epb->sk);
7761da177e4SLinus Torvalds 	read_unlock(&head->lock);
7771da177e4SLinus Torvalds 	return asoc;
7781da177e4SLinus Torvalds }
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds /* Look up an association. BH-safe. */
7811da177e4SLinus Torvalds SCTP_STATIC
7821da177e4SLinus Torvalds struct sctp_association *sctp_lookup_association(const union sctp_addr *laddr,
7831da177e4SLinus Torvalds 						 const union sctp_addr *paddr,
7841da177e4SLinus Torvalds 					    struct sctp_transport **transportp)
7851da177e4SLinus Torvalds {
7861da177e4SLinus Torvalds 	struct sctp_association *asoc;
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds 	sctp_local_bh_disable();
7891da177e4SLinus Torvalds 	asoc = __sctp_lookup_association(laddr, paddr, transportp);
7901da177e4SLinus Torvalds 	sctp_local_bh_enable();
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds 	return asoc;
7931da177e4SLinus Torvalds }
7941da177e4SLinus Torvalds 
7951da177e4SLinus Torvalds /* Is there an association matching the given local and peer addresses? */
7961da177e4SLinus Torvalds int sctp_has_association(const union sctp_addr *laddr,
7971da177e4SLinus Torvalds 			 const union sctp_addr *paddr)
7981da177e4SLinus Torvalds {
7991da177e4SLinus Torvalds 	struct sctp_association *asoc;
8001da177e4SLinus Torvalds 	struct sctp_transport *transport;
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds 	if ((asoc = sctp_lookup_association(laddr, paddr, &transport))) {
8031da177e4SLinus Torvalds 		sock_put(asoc->base.sk);
8041da177e4SLinus Torvalds 		sctp_association_put(asoc);
8051da177e4SLinus Torvalds 		return 1;
8061da177e4SLinus Torvalds 	}
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds 	return 0;
8091da177e4SLinus Torvalds }
8101da177e4SLinus Torvalds 
8111da177e4SLinus Torvalds /*
8121da177e4SLinus Torvalds  * SCTP Implementors Guide, 2.18 Handling of address
8131da177e4SLinus Torvalds  * parameters within the INIT or INIT-ACK.
8141da177e4SLinus Torvalds  *
8151da177e4SLinus Torvalds  * D) When searching for a matching TCB upon reception of an INIT
8161da177e4SLinus Torvalds  *    or INIT-ACK chunk the receiver SHOULD use not only the
8171da177e4SLinus Torvalds  *    source address of the packet (containing the INIT or
8181da177e4SLinus Torvalds  *    INIT-ACK) but the receiver SHOULD also use all valid
8191da177e4SLinus Torvalds  *    address parameters contained within the chunk.
8201da177e4SLinus Torvalds  *
8211da177e4SLinus Torvalds  * 2.18.3 Solution description
8221da177e4SLinus Torvalds  *
8231da177e4SLinus Torvalds  * This new text clearly specifies to an implementor the need
8241da177e4SLinus Torvalds  * to look within the INIT or INIT-ACK. Any implementation that
8251da177e4SLinus Torvalds  * does not do this, may not be able to establish associations
8261da177e4SLinus Torvalds  * in certain circumstances.
8271da177e4SLinus Torvalds  *
8281da177e4SLinus Torvalds  */
8291da177e4SLinus Torvalds static struct sctp_association *__sctp_rcv_init_lookup(struct sk_buff *skb,
8301da177e4SLinus Torvalds 	const union sctp_addr *laddr, struct sctp_transport **transportp)
8311da177e4SLinus Torvalds {
8321da177e4SLinus Torvalds 	struct sctp_association *asoc;
8331da177e4SLinus Torvalds 	union sctp_addr addr;
8341da177e4SLinus Torvalds 	union sctp_addr *paddr = &addr;
8351da177e4SLinus Torvalds 	struct sctphdr *sh = (struct sctphdr *) skb->h.raw;
8361da177e4SLinus Torvalds 	sctp_chunkhdr_t *ch;
8371da177e4SLinus Torvalds 	union sctp_params params;
8381da177e4SLinus Torvalds 	sctp_init_chunk_t *init;
8391da177e4SLinus Torvalds 	struct sctp_transport *transport;
8401da177e4SLinus Torvalds 	struct sctp_af *af;
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds 	ch = (sctp_chunkhdr_t *) skb->data;
8431da177e4SLinus Torvalds 
8441da177e4SLinus Torvalds 	/* If this is INIT/INIT-ACK look inside the chunk too. */
8451da177e4SLinus Torvalds 	switch (ch->type) {
8461da177e4SLinus Torvalds 	case SCTP_CID_INIT:
8471da177e4SLinus Torvalds 	case SCTP_CID_INIT_ACK:
8481da177e4SLinus Torvalds 		break;
8491da177e4SLinus Torvalds 	default:
8501da177e4SLinus Torvalds 		return NULL;
8511da177e4SLinus Torvalds 	}
8521da177e4SLinus Torvalds 
8531da177e4SLinus Torvalds 	/* The code below will attempt to walk the chunk and extract
8541da177e4SLinus Torvalds 	 * parameter information.  Before we do that, we need to verify
8551da177e4SLinus Torvalds 	 * that the chunk length doesn't cause overflow.  Otherwise, we'll
8561da177e4SLinus Torvalds 	 * walk off the end.
8571da177e4SLinus Torvalds 	 */
8581da177e4SLinus Torvalds 	if (WORD_ROUND(ntohs(ch->length)) > skb->len)
8591da177e4SLinus Torvalds 		return NULL;
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds 	/*
8621da177e4SLinus Torvalds 	 * This code will NOT touch anything inside the chunk--it is
8631da177e4SLinus Torvalds 	 * strictly READ-ONLY.
8641da177e4SLinus Torvalds 	 *
8651da177e4SLinus Torvalds 	 * RFC 2960 3  SCTP packet Format
8661da177e4SLinus Torvalds 	 *
8671da177e4SLinus Torvalds 	 * Multiple chunks can be bundled into one SCTP packet up to
8681da177e4SLinus Torvalds 	 * the MTU size, except for the INIT, INIT ACK, and SHUTDOWN
8691da177e4SLinus Torvalds 	 * COMPLETE chunks.  These chunks MUST NOT be bundled with any
8701da177e4SLinus Torvalds 	 * other chunk in a packet.  See Section 6.10 for more details
8711da177e4SLinus Torvalds 	 * on chunk bundling.
8721da177e4SLinus Torvalds 	 */
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds 	/* Find the start of the TLVs and the end of the chunk.  This is
8751da177e4SLinus Torvalds 	 * the region we search for address parameters.
8761da177e4SLinus Torvalds 	 */
8771da177e4SLinus Torvalds 	init = (sctp_init_chunk_t *)skb->data;
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds 	/* Walk the parameters looking for embedded addresses. */
8801da177e4SLinus Torvalds 	sctp_walk_params(params, init, init_hdr.params) {
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 		/* Note: Ignoring hostname addresses. */
8831da177e4SLinus Torvalds 		af = sctp_get_af_specific(param_type2af(params.p->type));
8841da177e4SLinus Torvalds 		if (!af)
8851da177e4SLinus Torvalds 			continue;
8861da177e4SLinus Torvalds 
8871da177e4SLinus Torvalds 		af->from_addr_param(paddr, params.addr, ntohs(sh->source), 0);
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds 		asoc = __sctp_lookup_association(laddr, paddr, &transport);
8901da177e4SLinus Torvalds 		if (asoc)
8911da177e4SLinus Torvalds 			return asoc;
8921da177e4SLinus Torvalds 	}
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds 	return NULL;
8951da177e4SLinus Torvalds }
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds /* Lookup an association for an inbound skb. */
8981da177e4SLinus Torvalds static struct sctp_association *__sctp_rcv_lookup(struct sk_buff *skb,
8991da177e4SLinus Torvalds 				      const union sctp_addr *paddr,
9001da177e4SLinus Torvalds 				      const union sctp_addr *laddr,
9011da177e4SLinus Torvalds 				      struct sctp_transport **transportp)
9021da177e4SLinus Torvalds {
9031da177e4SLinus Torvalds 	struct sctp_association *asoc;
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds 	asoc = __sctp_lookup_association(laddr, paddr, transportp);
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds 	/* Further lookup for INIT/INIT-ACK packets.
9081da177e4SLinus Torvalds 	 * SCTP Implementors Guide, 2.18 Handling of address
9091da177e4SLinus Torvalds 	 * parameters within the INIT or INIT-ACK.
9101da177e4SLinus Torvalds 	 */
9111da177e4SLinus Torvalds 	if (!asoc)
9121da177e4SLinus Torvalds 		asoc = __sctp_rcv_init_lookup(skb, laddr, transportp);
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds 	return asoc;
9151da177e4SLinus Torvalds }
916