xref: /OK3568_Linux_fs/kernel/net/sctp/input.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /* SCTP kernel implementation
3*4882a593Smuzhiyun  * Copyright (c) 1999-2000 Cisco, Inc.
4*4882a593Smuzhiyun  * Copyright (c) 1999-2001 Motorola, Inc.
5*4882a593Smuzhiyun  * Copyright (c) 2001-2003 International Business Machines, Corp.
6*4882a593Smuzhiyun  * Copyright (c) 2001 Intel Corp.
7*4882a593Smuzhiyun  * Copyright (c) 2001 Nokia, Inc.
8*4882a593Smuzhiyun  * Copyright (c) 2001 La Monte H.P. Yarroll
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  * This file is part of the SCTP kernel implementation
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * These functions handle all input from the IP layer into SCTP.
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * Please send any bug reports or fixes you make to the
15*4882a593Smuzhiyun  * email address(es):
16*4882a593Smuzhiyun  *    lksctp developers <linux-sctp@vger.kernel.org>
17*4882a593Smuzhiyun  *
18*4882a593Smuzhiyun  * Written or modified by:
19*4882a593Smuzhiyun  *    La Monte H.P. Yarroll <piggy@acm.org>
20*4882a593Smuzhiyun  *    Karl Knutson <karl@athena.chicago.il.us>
21*4882a593Smuzhiyun  *    Xingang Guo <xingang.guo@intel.com>
22*4882a593Smuzhiyun  *    Jon Grimm <jgrimm@us.ibm.com>
23*4882a593Smuzhiyun  *    Hui Huang <hui.huang@nokia.com>
24*4882a593Smuzhiyun  *    Daisy Chang <daisyc@us.ibm.com>
25*4882a593Smuzhiyun  *    Sridhar Samudrala <sri@us.ibm.com>
26*4882a593Smuzhiyun  *    Ardelle Fan <ardelle.fan@intel.com>
27*4882a593Smuzhiyun  */
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun #include <linux/types.h>
30*4882a593Smuzhiyun #include <linux/list.h> /* For struct list_head */
31*4882a593Smuzhiyun #include <linux/socket.h>
32*4882a593Smuzhiyun #include <linux/ip.h>
33*4882a593Smuzhiyun #include <linux/time.h> /* For struct timeval */
34*4882a593Smuzhiyun #include <linux/slab.h>
35*4882a593Smuzhiyun #include <net/ip.h>
36*4882a593Smuzhiyun #include <net/icmp.h>
37*4882a593Smuzhiyun #include <net/snmp.h>
38*4882a593Smuzhiyun #include <net/sock.h>
39*4882a593Smuzhiyun #include <net/xfrm.h>
40*4882a593Smuzhiyun #include <net/sctp/sctp.h>
41*4882a593Smuzhiyun #include <net/sctp/sm.h>
42*4882a593Smuzhiyun #include <net/sctp/checksum.h>
43*4882a593Smuzhiyun #include <net/net_namespace.h>
44*4882a593Smuzhiyun #include <linux/rhashtable.h>
45*4882a593Smuzhiyun #include <net/sock_reuseport.h>
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun /* Forward declarations for internal helpers. */
48*4882a593Smuzhiyun static int sctp_rcv_ootb(struct sk_buff *);
49*4882a593Smuzhiyun static struct sctp_association *__sctp_rcv_lookup(struct net *net,
50*4882a593Smuzhiyun 				      struct sk_buff *skb,
51*4882a593Smuzhiyun 				      const union sctp_addr *paddr,
52*4882a593Smuzhiyun 				      const union sctp_addr *laddr,
53*4882a593Smuzhiyun 				      struct sctp_transport **transportp);
54*4882a593Smuzhiyun static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(
55*4882a593Smuzhiyun 					struct net *net, struct sk_buff *skb,
56*4882a593Smuzhiyun 					const union sctp_addr *laddr,
57*4882a593Smuzhiyun 					const union sctp_addr *daddr);
58*4882a593Smuzhiyun static struct sctp_association *__sctp_lookup_association(
59*4882a593Smuzhiyun 					struct net *net,
60*4882a593Smuzhiyun 					const union sctp_addr *local,
61*4882a593Smuzhiyun 					const union sctp_addr *peer,
62*4882a593Smuzhiyun 					struct sctp_transport **pt);
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun static int sctp_add_backlog(struct sock *sk, struct sk_buff *skb);
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun /* Calculate the SCTP checksum of an SCTP packet.  */
sctp_rcv_checksum(struct net * net,struct sk_buff * skb)68*4882a593Smuzhiyun static inline int sctp_rcv_checksum(struct net *net, struct sk_buff *skb)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun 	struct sctphdr *sh = sctp_hdr(skb);
71*4882a593Smuzhiyun 	__le32 cmp = sh->checksum;
72*4882a593Smuzhiyun 	__le32 val = sctp_compute_cksum(skb, 0);
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	if (val != cmp) {
75*4882a593Smuzhiyun 		/* CRC failure, dump it. */
76*4882a593Smuzhiyun 		__SCTP_INC_STATS(net, SCTP_MIB_CHECKSUMERRORS);
77*4882a593Smuzhiyun 		return -1;
78*4882a593Smuzhiyun 	}
79*4882a593Smuzhiyun 	return 0;
80*4882a593Smuzhiyun }
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun /*
83*4882a593Smuzhiyun  * This is the routine which IP calls when receiving an SCTP packet.
84*4882a593Smuzhiyun  */
sctp_rcv(struct sk_buff * skb)85*4882a593Smuzhiyun int sctp_rcv(struct sk_buff *skb)
86*4882a593Smuzhiyun {
87*4882a593Smuzhiyun 	struct sock *sk;
88*4882a593Smuzhiyun 	struct sctp_association *asoc;
89*4882a593Smuzhiyun 	struct sctp_endpoint *ep = NULL;
90*4882a593Smuzhiyun 	struct sctp_ep_common *rcvr;
91*4882a593Smuzhiyun 	struct sctp_transport *transport = NULL;
92*4882a593Smuzhiyun 	struct sctp_chunk *chunk;
93*4882a593Smuzhiyun 	union sctp_addr src;
94*4882a593Smuzhiyun 	union sctp_addr dest;
95*4882a593Smuzhiyun 	int bound_dev_if;
96*4882a593Smuzhiyun 	int family;
97*4882a593Smuzhiyun 	struct sctp_af *af;
98*4882a593Smuzhiyun 	struct net *net = dev_net(skb->dev);
99*4882a593Smuzhiyun 	bool is_gso = skb_is_gso(skb) && skb_is_gso_sctp(skb);
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	if (skb->pkt_type != PACKET_HOST)
102*4882a593Smuzhiyun 		goto discard_it;
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 	__SCTP_INC_STATS(net, SCTP_MIB_INSCTPPACKS);
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 	/* If packet is too small to contain a single chunk, let's not
107*4882a593Smuzhiyun 	 * waste time on it anymore.
108*4882a593Smuzhiyun 	 */
109*4882a593Smuzhiyun 	if (skb->len < sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr) +
110*4882a593Smuzhiyun 		       skb_transport_offset(skb))
111*4882a593Smuzhiyun 		goto discard_it;
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	/* If the packet is fragmented and we need to do crc checking,
114*4882a593Smuzhiyun 	 * it's better to just linearize it otherwise crc computing
115*4882a593Smuzhiyun 	 * takes longer.
116*4882a593Smuzhiyun 	 */
117*4882a593Smuzhiyun 	if ((!is_gso && skb_linearize(skb)) ||
118*4882a593Smuzhiyun 	    !pskb_may_pull(skb, sizeof(struct sctphdr)))
119*4882a593Smuzhiyun 		goto discard_it;
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	/* Pull up the IP header. */
122*4882a593Smuzhiyun 	__skb_pull(skb, skb_transport_offset(skb));
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 	skb->csum_valid = 0; /* Previous value not applicable */
125*4882a593Smuzhiyun 	if (skb_csum_unnecessary(skb))
126*4882a593Smuzhiyun 		__skb_decr_checksum_unnecessary(skb);
127*4882a593Smuzhiyun 	else if (!sctp_checksum_disable &&
128*4882a593Smuzhiyun 		 !is_gso &&
129*4882a593Smuzhiyun 		 sctp_rcv_checksum(net, skb) < 0)
130*4882a593Smuzhiyun 		goto discard_it;
131*4882a593Smuzhiyun 	skb->csum_valid = 1;
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun 	__skb_pull(skb, sizeof(struct sctphdr));
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	family = ipver2af(ip_hdr(skb)->version);
136*4882a593Smuzhiyun 	af = sctp_get_af_specific(family);
137*4882a593Smuzhiyun 	if (unlikely(!af))
138*4882a593Smuzhiyun 		goto discard_it;
139*4882a593Smuzhiyun 	SCTP_INPUT_CB(skb)->af = af;
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 	/* Initialize local addresses for lookups. */
142*4882a593Smuzhiyun 	af->from_skb(&src, skb, 1);
143*4882a593Smuzhiyun 	af->from_skb(&dest, skb, 0);
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 	/* If the packet is to or from a non-unicast address,
146*4882a593Smuzhiyun 	 * silently discard the packet.
147*4882a593Smuzhiyun 	 *
148*4882a593Smuzhiyun 	 * This is not clearly defined in the RFC except in section
149*4882a593Smuzhiyun 	 * 8.4 - OOTB handling.  However, based on the book "Stream Control
150*4882a593Smuzhiyun 	 * Transmission Protocol" 2.1, "It is important to note that the
151*4882a593Smuzhiyun 	 * IP address of an SCTP transport address must be a routable
152*4882a593Smuzhiyun 	 * unicast address.  In other words, IP multicast addresses and
153*4882a593Smuzhiyun 	 * IP broadcast addresses cannot be used in an SCTP transport
154*4882a593Smuzhiyun 	 * address."
155*4882a593Smuzhiyun 	 */
156*4882a593Smuzhiyun 	if (!af->addr_valid(&src, NULL, skb) ||
157*4882a593Smuzhiyun 	    !af->addr_valid(&dest, NULL, skb))
158*4882a593Smuzhiyun 		goto discard_it;
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	asoc = __sctp_rcv_lookup(net, skb, &src, &dest, &transport);
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 	if (!asoc)
163*4882a593Smuzhiyun 		ep = __sctp_rcv_lookup_endpoint(net, skb, &dest, &src);
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	/* Retrieve the common input handling substructure. */
166*4882a593Smuzhiyun 	rcvr = asoc ? &asoc->base : &ep->base;
167*4882a593Smuzhiyun 	sk = rcvr->sk;
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	/*
170*4882a593Smuzhiyun 	 * If a frame arrives on an interface and the receiving socket is
171*4882a593Smuzhiyun 	 * bound to another interface, via SO_BINDTODEVICE, treat it as OOTB
172*4882a593Smuzhiyun 	 */
173*4882a593Smuzhiyun 	bound_dev_if = READ_ONCE(sk->sk_bound_dev_if);
174*4882a593Smuzhiyun 	if (bound_dev_if && (bound_dev_if != af->skb_iif(skb))) {
175*4882a593Smuzhiyun 		if (transport) {
176*4882a593Smuzhiyun 			sctp_transport_put(transport);
177*4882a593Smuzhiyun 			asoc = NULL;
178*4882a593Smuzhiyun 			transport = NULL;
179*4882a593Smuzhiyun 		} else {
180*4882a593Smuzhiyun 			sctp_endpoint_put(ep);
181*4882a593Smuzhiyun 			ep = NULL;
182*4882a593Smuzhiyun 		}
183*4882a593Smuzhiyun 		sk = net->sctp.ctl_sock;
184*4882a593Smuzhiyun 		ep = sctp_sk(sk)->ep;
185*4882a593Smuzhiyun 		sctp_endpoint_hold(ep);
186*4882a593Smuzhiyun 		rcvr = &ep->base;
187*4882a593Smuzhiyun 	}
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	/*
190*4882a593Smuzhiyun 	 * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
191*4882a593Smuzhiyun 	 * An SCTP packet is called an "out of the blue" (OOTB)
192*4882a593Smuzhiyun 	 * packet if it is correctly formed, i.e., passed the
193*4882a593Smuzhiyun 	 * receiver's checksum check, but the receiver is not
194*4882a593Smuzhiyun 	 * able to identify the association to which this
195*4882a593Smuzhiyun 	 * packet belongs.
196*4882a593Smuzhiyun 	 */
197*4882a593Smuzhiyun 	if (!asoc) {
198*4882a593Smuzhiyun 		if (sctp_rcv_ootb(skb)) {
199*4882a593Smuzhiyun 			__SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
200*4882a593Smuzhiyun 			goto discard_release;
201*4882a593Smuzhiyun 		}
202*4882a593Smuzhiyun 	}
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 	if (!xfrm_policy_check(sk, XFRM_POLICY_IN, skb, family))
205*4882a593Smuzhiyun 		goto discard_release;
206*4882a593Smuzhiyun 	nf_reset_ct(skb);
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 	if (sk_filter(sk, skb))
209*4882a593Smuzhiyun 		goto discard_release;
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun 	/* Create an SCTP packet structure. */
212*4882a593Smuzhiyun 	chunk = sctp_chunkify(skb, asoc, sk, GFP_ATOMIC);
213*4882a593Smuzhiyun 	if (!chunk)
214*4882a593Smuzhiyun 		goto discard_release;
215*4882a593Smuzhiyun 	SCTP_INPUT_CB(skb)->chunk = chunk;
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 	/* Remember what endpoint is to handle this packet. */
218*4882a593Smuzhiyun 	chunk->rcvr = rcvr;
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun 	/* Remember the SCTP header. */
221*4882a593Smuzhiyun 	chunk->sctp_hdr = sctp_hdr(skb);
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun 	/* Set the source and destination addresses of the incoming chunk.  */
224*4882a593Smuzhiyun 	sctp_init_addrs(chunk, &src, &dest);
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	/* Remember where we came from.  */
227*4882a593Smuzhiyun 	chunk->transport = transport;
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun 	/* Acquire access to the sock lock. Note: We are safe from other
230*4882a593Smuzhiyun 	 * bottom halves on this lock, but a user may be in the lock too,
231*4882a593Smuzhiyun 	 * so check if it is busy.
232*4882a593Smuzhiyun 	 */
233*4882a593Smuzhiyun 	bh_lock_sock(sk);
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	if (sk != rcvr->sk) {
236*4882a593Smuzhiyun 		/* Our cached sk is different from the rcvr->sk.  This is
237*4882a593Smuzhiyun 		 * because migrate()/accept() may have moved the association
238*4882a593Smuzhiyun 		 * to a new socket and released all the sockets.  So now we
239*4882a593Smuzhiyun 		 * are holding a lock on the old socket while the user may
240*4882a593Smuzhiyun 		 * be doing something with the new socket.  Switch our veiw
241*4882a593Smuzhiyun 		 * of the current sk.
242*4882a593Smuzhiyun 		 */
243*4882a593Smuzhiyun 		bh_unlock_sock(sk);
244*4882a593Smuzhiyun 		sk = rcvr->sk;
245*4882a593Smuzhiyun 		bh_lock_sock(sk);
246*4882a593Smuzhiyun 	}
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun 	if (sock_owned_by_user(sk) || !sctp_newsk_ready(sk)) {
249*4882a593Smuzhiyun 		if (sctp_add_backlog(sk, skb)) {
250*4882a593Smuzhiyun 			bh_unlock_sock(sk);
251*4882a593Smuzhiyun 			sctp_chunk_free(chunk);
252*4882a593Smuzhiyun 			skb = NULL; /* sctp_chunk_free already freed the skb */
253*4882a593Smuzhiyun 			goto discard_release;
254*4882a593Smuzhiyun 		}
255*4882a593Smuzhiyun 		__SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_BACKLOG);
256*4882a593Smuzhiyun 	} else {
257*4882a593Smuzhiyun 		__SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_SOFTIRQ);
258*4882a593Smuzhiyun 		sctp_inq_push(&chunk->rcvr->inqueue, chunk);
259*4882a593Smuzhiyun 	}
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 	bh_unlock_sock(sk);
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun 	/* Release the asoc/ep ref we took in the lookup calls. */
264*4882a593Smuzhiyun 	if (transport)
265*4882a593Smuzhiyun 		sctp_transport_put(transport);
266*4882a593Smuzhiyun 	else
267*4882a593Smuzhiyun 		sctp_endpoint_put(ep);
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	return 0;
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun discard_it:
272*4882a593Smuzhiyun 	__SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_DISCARDS);
273*4882a593Smuzhiyun 	kfree_skb(skb);
274*4882a593Smuzhiyun 	return 0;
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun discard_release:
277*4882a593Smuzhiyun 	/* Release the asoc/ep ref we took in the lookup calls. */
278*4882a593Smuzhiyun 	if (transport)
279*4882a593Smuzhiyun 		sctp_transport_put(transport);
280*4882a593Smuzhiyun 	else
281*4882a593Smuzhiyun 		sctp_endpoint_put(ep);
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun 	goto discard_it;
284*4882a593Smuzhiyun }
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun /* Process the backlog queue of the socket.  Every skb on
287*4882a593Smuzhiyun  * the backlog holds a ref on an association or endpoint.
288*4882a593Smuzhiyun  * We hold this ref throughout the state machine to make
289*4882a593Smuzhiyun  * sure that the structure we need is still around.
290*4882a593Smuzhiyun  */
sctp_backlog_rcv(struct sock * sk,struct sk_buff * skb)291*4882a593Smuzhiyun int sctp_backlog_rcv(struct sock *sk, struct sk_buff *skb)
292*4882a593Smuzhiyun {
293*4882a593Smuzhiyun 	struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
294*4882a593Smuzhiyun 	struct sctp_inq *inqueue = &chunk->rcvr->inqueue;
295*4882a593Smuzhiyun 	struct sctp_transport *t = chunk->transport;
296*4882a593Smuzhiyun 	struct sctp_ep_common *rcvr = NULL;
297*4882a593Smuzhiyun 	int backloged = 0;
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun 	rcvr = chunk->rcvr;
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun 	/* If the rcvr is dead then the association or endpoint
302*4882a593Smuzhiyun 	 * has been deleted and we can safely drop the chunk
303*4882a593Smuzhiyun 	 * and refs that we are holding.
304*4882a593Smuzhiyun 	 */
305*4882a593Smuzhiyun 	if (rcvr->dead) {
306*4882a593Smuzhiyun 		sctp_chunk_free(chunk);
307*4882a593Smuzhiyun 		goto done;
308*4882a593Smuzhiyun 	}
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun 	if (unlikely(rcvr->sk != sk)) {
311*4882a593Smuzhiyun 		/* In this case, the association moved from one socket to
312*4882a593Smuzhiyun 		 * another.  We are currently sitting on the backlog of the
313*4882a593Smuzhiyun 		 * old socket, so we need to move.
314*4882a593Smuzhiyun 		 * However, since we are here in the process context we
315*4882a593Smuzhiyun 		 * need to take make sure that the user doesn't own
316*4882a593Smuzhiyun 		 * the new socket when we process the packet.
317*4882a593Smuzhiyun 		 * If the new socket is user-owned, queue the chunk to the
318*4882a593Smuzhiyun 		 * backlog of the new socket without dropping any refs.
319*4882a593Smuzhiyun 		 * Otherwise, we can safely push the chunk on the inqueue.
320*4882a593Smuzhiyun 		 */
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 		sk = rcvr->sk;
323*4882a593Smuzhiyun 		local_bh_disable();
324*4882a593Smuzhiyun 		bh_lock_sock(sk);
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun 		if (sock_owned_by_user(sk) || !sctp_newsk_ready(sk)) {
327*4882a593Smuzhiyun 			if (sk_add_backlog(sk, skb, READ_ONCE(sk->sk_rcvbuf)))
328*4882a593Smuzhiyun 				sctp_chunk_free(chunk);
329*4882a593Smuzhiyun 			else
330*4882a593Smuzhiyun 				backloged = 1;
331*4882a593Smuzhiyun 		} else
332*4882a593Smuzhiyun 			sctp_inq_push(inqueue, chunk);
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun 		bh_unlock_sock(sk);
335*4882a593Smuzhiyun 		local_bh_enable();
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun 		/* If the chunk was backloged again, don't drop refs */
338*4882a593Smuzhiyun 		if (backloged)
339*4882a593Smuzhiyun 			return 0;
340*4882a593Smuzhiyun 	} else {
341*4882a593Smuzhiyun 		if (!sctp_newsk_ready(sk)) {
342*4882a593Smuzhiyun 			if (!sk_add_backlog(sk, skb, READ_ONCE(sk->sk_rcvbuf)))
343*4882a593Smuzhiyun 				return 0;
344*4882a593Smuzhiyun 			sctp_chunk_free(chunk);
345*4882a593Smuzhiyun 		} else {
346*4882a593Smuzhiyun 			sctp_inq_push(inqueue, chunk);
347*4882a593Smuzhiyun 		}
348*4882a593Smuzhiyun 	}
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun done:
351*4882a593Smuzhiyun 	/* Release the refs we took in sctp_add_backlog */
352*4882a593Smuzhiyun 	if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
353*4882a593Smuzhiyun 		sctp_transport_put(t);
354*4882a593Smuzhiyun 	else if (SCTP_EP_TYPE_SOCKET == rcvr->type)
355*4882a593Smuzhiyun 		sctp_endpoint_put(sctp_ep(rcvr));
356*4882a593Smuzhiyun 	else
357*4882a593Smuzhiyun 		BUG();
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun 	return 0;
360*4882a593Smuzhiyun }
361*4882a593Smuzhiyun 
sctp_add_backlog(struct sock * sk,struct sk_buff * skb)362*4882a593Smuzhiyun static int sctp_add_backlog(struct sock *sk, struct sk_buff *skb)
363*4882a593Smuzhiyun {
364*4882a593Smuzhiyun 	struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
365*4882a593Smuzhiyun 	struct sctp_transport *t = chunk->transport;
366*4882a593Smuzhiyun 	struct sctp_ep_common *rcvr = chunk->rcvr;
367*4882a593Smuzhiyun 	int ret;
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun 	ret = sk_add_backlog(sk, skb, READ_ONCE(sk->sk_rcvbuf));
370*4882a593Smuzhiyun 	if (!ret) {
371*4882a593Smuzhiyun 		/* Hold the assoc/ep while hanging on the backlog queue.
372*4882a593Smuzhiyun 		 * This way, we know structures we need will not disappear
373*4882a593Smuzhiyun 		 * from us
374*4882a593Smuzhiyun 		 */
375*4882a593Smuzhiyun 		if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
376*4882a593Smuzhiyun 			sctp_transport_hold(t);
377*4882a593Smuzhiyun 		else if (SCTP_EP_TYPE_SOCKET == rcvr->type)
378*4882a593Smuzhiyun 			sctp_endpoint_hold(sctp_ep(rcvr));
379*4882a593Smuzhiyun 		else
380*4882a593Smuzhiyun 			BUG();
381*4882a593Smuzhiyun 	}
382*4882a593Smuzhiyun 	return ret;
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun }
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun /* Handle icmp frag needed error. */
sctp_icmp_frag_needed(struct sock * sk,struct sctp_association * asoc,struct sctp_transport * t,__u32 pmtu)387*4882a593Smuzhiyun void sctp_icmp_frag_needed(struct sock *sk, struct sctp_association *asoc,
388*4882a593Smuzhiyun 			   struct sctp_transport *t, __u32 pmtu)
389*4882a593Smuzhiyun {
390*4882a593Smuzhiyun 	if (!t || (t->pathmtu <= pmtu))
391*4882a593Smuzhiyun 		return;
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun 	if (sock_owned_by_user(sk)) {
394*4882a593Smuzhiyun 		atomic_set(&t->mtu_info, pmtu);
395*4882a593Smuzhiyun 		asoc->pmtu_pending = 1;
396*4882a593Smuzhiyun 		t->pmtu_pending = 1;
397*4882a593Smuzhiyun 		return;
398*4882a593Smuzhiyun 	}
399*4882a593Smuzhiyun 
400*4882a593Smuzhiyun 	if (!(t->param_flags & SPP_PMTUD_ENABLE))
401*4882a593Smuzhiyun 		/* We can't allow retransmitting in such case, as the
402*4882a593Smuzhiyun 		 * retransmission would be sized just as before, and thus we
403*4882a593Smuzhiyun 		 * would get another icmp, and retransmit again.
404*4882a593Smuzhiyun 		 */
405*4882a593Smuzhiyun 		return;
406*4882a593Smuzhiyun 
407*4882a593Smuzhiyun 	/* Update transports view of the MTU. Return if no update was needed.
408*4882a593Smuzhiyun 	 * If an update wasn't needed/possible, it also doesn't make sense to
409*4882a593Smuzhiyun 	 * try to retransmit now.
410*4882a593Smuzhiyun 	 */
411*4882a593Smuzhiyun 	if (!sctp_transport_update_pmtu(t, pmtu))
412*4882a593Smuzhiyun 		return;
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun 	/* Update association pmtu. */
415*4882a593Smuzhiyun 	sctp_assoc_sync_pmtu(asoc);
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun 	/* Retransmit with the new pmtu setting. */
418*4882a593Smuzhiyun 	sctp_retransmit(&asoc->outqueue, t, SCTP_RTXR_PMTUD);
419*4882a593Smuzhiyun }
420*4882a593Smuzhiyun 
sctp_icmp_redirect(struct sock * sk,struct sctp_transport * t,struct sk_buff * skb)421*4882a593Smuzhiyun void sctp_icmp_redirect(struct sock *sk, struct sctp_transport *t,
422*4882a593Smuzhiyun 			struct sk_buff *skb)
423*4882a593Smuzhiyun {
424*4882a593Smuzhiyun 	struct dst_entry *dst;
425*4882a593Smuzhiyun 
426*4882a593Smuzhiyun 	if (sock_owned_by_user(sk) || !t)
427*4882a593Smuzhiyun 		return;
428*4882a593Smuzhiyun 	dst = sctp_transport_dst_check(t);
429*4882a593Smuzhiyun 	if (dst)
430*4882a593Smuzhiyun 		dst->ops->redirect(dst, sk, skb);
431*4882a593Smuzhiyun }
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun /*
434*4882a593Smuzhiyun  * SCTP Implementer's Guide, 2.37 ICMP handling procedures
435*4882a593Smuzhiyun  *
436*4882a593Smuzhiyun  * ICMP8) If the ICMP code is a "Unrecognized next header type encountered"
437*4882a593Smuzhiyun  *        or a "Protocol Unreachable" treat this message as an abort
438*4882a593Smuzhiyun  *        with the T bit set.
439*4882a593Smuzhiyun  *
440*4882a593Smuzhiyun  * This function sends an event to the state machine, which will abort the
441*4882a593Smuzhiyun  * association.
442*4882a593Smuzhiyun  *
443*4882a593Smuzhiyun  */
sctp_icmp_proto_unreachable(struct sock * sk,struct sctp_association * asoc,struct sctp_transport * t)444*4882a593Smuzhiyun void sctp_icmp_proto_unreachable(struct sock *sk,
445*4882a593Smuzhiyun 			   struct sctp_association *asoc,
446*4882a593Smuzhiyun 			   struct sctp_transport *t)
447*4882a593Smuzhiyun {
448*4882a593Smuzhiyun 	if (sock_owned_by_user(sk)) {
449*4882a593Smuzhiyun 		if (timer_pending(&t->proto_unreach_timer))
450*4882a593Smuzhiyun 			return;
451*4882a593Smuzhiyun 		else {
452*4882a593Smuzhiyun 			if (!mod_timer(&t->proto_unreach_timer,
453*4882a593Smuzhiyun 						jiffies + (HZ/20)))
454*4882a593Smuzhiyun 				sctp_transport_hold(t);
455*4882a593Smuzhiyun 		}
456*4882a593Smuzhiyun 	} else {
457*4882a593Smuzhiyun 		struct net *net = sock_net(sk);
458*4882a593Smuzhiyun 
459*4882a593Smuzhiyun 		pr_debug("%s: unrecognized next header type "
460*4882a593Smuzhiyun 			 "encountered!\n", __func__);
461*4882a593Smuzhiyun 
462*4882a593Smuzhiyun 		if (del_timer(&t->proto_unreach_timer))
463*4882a593Smuzhiyun 			sctp_transport_put(t);
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun 		sctp_do_sm(net, SCTP_EVENT_T_OTHER,
466*4882a593Smuzhiyun 			   SCTP_ST_OTHER(SCTP_EVENT_ICMP_PROTO_UNREACH),
467*4882a593Smuzhiyun 			   asoc->state, asoc->ep, asoc, t,
468*4882a593Smuzhiyun 			   GFP_ATOMIC);
469*4882a593Smuzhiyun 	}
470*4882a593Smuzhiyun }
471*4882a593Smuzhiyun 
472*4882a593Smuzhiyun /* Common lookup code for icmp/icmpv6 error handler. */
sctp_err_lookup(struct net * net,int family,struct sk_buff * skb,struct sctphdr * sctphdr,struct sctp_association ** app,struct sctp_transport ** tpp)473*4882a593Smuzhiyun struct sock *sctp_err_lookup(struct net *net, int family, struct sk_buff *skb,
474*4882a593Smuzhiyun 			     struct sctphdr *sctphdr,
475*4882a593Smuzhiyun 			     struct sctp_association **app,
476*4882a593Smuzhiyun 			     struct sctp_transport **tpp)
477*4882a593Smuzhiyun {
478*4882a593Smuzhiyun 	struct sctp_init_chunk *chunkhdr, _chunkhdr;
479*4882a593Smuzhiyun 	union sctp_addr saddr;
480*4882a593Smuzhiyun 	union sctp_addr daddr;
481*4882a593Smuzhiyun 	struct sctp_af *af;
482*4882a593Smuzhiyun 	struct sock *sk = NULL;
483*4882a593Smuzhiyun 	struct sctp_association *asoc;
484*4882a593Smuzhiyun 	struct sctp_transport *transport = NULL;
485*4882a593Smuzhiyun 	__u32 vtag = ntohl(sctphdr->vtag);
486*4882a593Smuzhiyun 
487*4882a593Smuzhiyun 	*app = NULL; *tpp = NULL;
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun 	af = sctp_get_af_specific(family);
490*4882a593Smuzhiyun 	if (unlikely(!af)) {
491*4882a593Smuzhiyun 		return NULL;
492*4882a593Smuzhiyun 	}
493*4882a593Smuzhiyun 
494*4882a593Smuzhiyun 	/* Initialize local addresses for lookups. */
495*4882a593Smuzhiyun 	af->from_skb(&saddr, skb, 1);
496*4882a593Smuzhiyun 	af->from_skb(&daddr, skb, 0);
497*4882a593Smuzhiyun 
498*4882a593Smuzhiyun 	/* Look for an association that matches the incoming ICMP error
499*4882a593Smuzhiyun 	 * packet.
500*4882a593Smuzhiyun 	 */
501*4882a593Smuzhiyun 	asoc = __sctp_lookup_association(net, &saddr, &daddr, &transport);
502*4882a593Smuzhiyun 	if (!asoc)
503*4882a593Smuzhiyun 		return NULL;
504*4882a593Smuzhiyun 
505*4882a593Smuzhiyun 	sk = asoc->base.sk;
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun 	/* RFC 4960, Appendix C. ICMP Handling
508*4882a593Smuzhiyun 	 *
509*4882a593Smuzhiyun 	 * ICMP6) An implementation MUST validate that the Verification Tag
510*4882a593Smuzhiyun 	 * contained in the ICMP message matches the Verification Tag of
511*4882a593Smuzhiyun 	 * the peer.  If the Verification Tag is not 0 and does NOT
512*4882a593Smuzhiyun 	 * match, discard the ICMP message.  If it is 0 and the ICMP
513*4882a593Smuzhiyun 	 * message contains enough bytes to verify that the chunk type is
514*4882a593Smuzhiyun 	 * an INIT chunk and that the Initiate Tag matches the tag of the
515*4882a593Smuzhiyun 	 * peer, continue with ICMP7.  If the ICMP message is too short
516*4882a593Smuzhiyun 	 * or the chunk type or the Initiate Tag does not match, silently
517*4882a593Smuzhiyun 	 * discard the packet.
518*4882a593Smuzhiyun 	 */
519*4882a593Smuzhiyun 	if (vtag == 0) {
520*4882a593Smuzhiyun 		/* chunk header + first 4 octects of init header */
521*4882a593Smuzhiyun 		chunkhdr = skb_header_pointer(skb, skb_transport_offset(skb) +
522*4882a593Smuzhiyun 					      sizeof(struct sctphdr),
523*4882a593Smuzhiyun 					      sizeof(struct sctp_chunkhdr) +
524*4882a593Smuzhiyun 					      sizeof(__be32), &_chunkhdr);
525*4882a593Smuzhiyun 		if (!chunkhdr ||
526*4882a593Smuzhiyun 		    chunkhdr->chunk_hdr.type != SCTP_CID_INIT ||
527*4882a593Smuzhiyun 		    ntohl(chunkhdr->init_hdr.init_tag) != asoc->c.my_vtag)
528*4882a593Smuzhiyun 			goto out;
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun 	} else if (vtag != asoc->c.peer_vtag) {
531*4882a593Smuzhiyun 		goto out;
532*4882a593Smuzhiyun 	}
533*4882a593Smuzhiyun 
534*4882a593Smuzhiyun 	bh_lock_sock(sk);
535*4882a593Smuzhiyun 
536*4882a593Smuzhiyun 	/* If too many ICMPs get dropped on busy
537*4882a593Smuzhiyun 	 * servers this needs to be solved differently.
538*4882a593Smuzhiyun 	 */
539*4882a593Smuzhiyun 	if (sock_owned_by_user(sk))
540*4882a593Smuzhiyun 		__NET_INC_STATS(net, LINUX_MIB_LOCKDROPPEDICMPS);
541*4882a593Smuzhiyun 
542*4882a593Smuzhiyun 	*app = asoc;
543*4882a593Smuzhiyun 	*tpp = transport;
544*4882a593Smuzhiyun 	return sk;
545*4882a593Smuzhiyun 
546*4882a593Smuzhiyun out:
547*4882a593Smuzhiyun 	sctp_transport_put(transport);
548*4882a593Smuzhiyun 	return NULL;
549*4882a593Smuzhiyun }
550*4882a593Smuzhiyun 
551*4882a593Smuzhiyun /* Common cleanup code for icmp/icmpv6 error handler. */
sctp_err_finish(struct sock * sk,struct sctp_transport * t)552*4882a593Smuzhiyun void sctp_err_finish(struct sock *sk, struct sctp_transport *t)
553*4882a593Smuzhiyun 	__releases(&((__sk)->sk_lock.slock))
554*4882a593Smuzhiyun {
555*4882a593Smuzhiyun 	bh_unlock_sock(sk);
556*4882a593Smuzhiyun 	sctp_transport_put(t);
557*4882a593Smuzhiyun }
558*4882a593Smuzhiyun 
559*4882a593Smuzhiyun /*
560*4882a593Smuzhiyun  * This routine is called by the ICMP module when it gets some
561*4882a593Smuzhiyun  * sort of error condition.  If err < 0 then the socket should
562*4882a593Smuzhiyun  * be closed and the error returned to the user.  If err > 0
563*4882a593Smuzhiyun  * it's just the icmp type << 8 | icmp code.  After adjustment
564*4882a593Smuzhiyun  * header points to the first 8 bytes of the sctp header.  We need
565*4882a593Smuzhiyun  * to find the appropriate port.
566*4882a593Smuzhiyun  *
567*4882a593Smuzhiyun  * The locking strategy used here is very "optimistic". When
568*4882a593Smuzhiyun  * someone else accesses the socket the ICMP is just dropped
569*4882a593Smuzhiyun  * and for some paths there is no check at all.
570*4882a593Smuzhiyun  * A more general error queue to queue errors for later handling
571*4882a593Smuzhiyun  * is probably better.
572*4882a593Smuzhiyun  *
573*4882a593Smuzhiyun  */
sctp_v4_err(struct sk_buff * skb,__u32 info)574*4882a593Smuzhiyun int sctp_v4_err(struct sk_buff *skb, __u32 info)
575*4882a593Smuzhiyun {
576*4882a593Smuzhiyun 	const struct iphdr *iph = (const struct iphdr *)skb->data;
577*4882a593Smuzhiyun 	const int ihlen = iph->ihl * 4;
578*4882a593Smuzhiyun 	const int type = icmp_hdr(skb)->type;
579*4882a593Smuzhiyun 	const int code = icmp_hdr(skb)->code;
580*4882a593Smuzhiyun 	struct sock *sk;
581*4882a593Smuzhiyun 	struct sctp_association *asoc = NULL;
582*4882a593Smuzhiyun 	struct sctp_transport *transport;
583*4882a593Smuzhiyun 	struct inet_sock *inet;
584*4882a593Smuzhiyun 	__u16 saveip, savesctp;
585*4882a593Smuzhiyun 	int err;
586*4882a593Smuzhiyun 	struct net *net = dev_net(skb->dev);
587*4882a593Smuzhiyun 
588*4882a593Smuzhiyun 	/* Fix up skb to look at the embedded net header. */
589*4882a593Smuzhiyun 	saveip = skb->network_header;
590*4882a593Smuzhiyun 	savesctp = skb->transport_header;
591*4882a593Smuzhiyun 	skb_reset_network_header(skb);
592*4882a593Smuzhiyun 	skb_set_transport_header(skb, ihlen);
593*4882a593Smuzhiyun 	sk = sctp_err_lookup(net, AF_INET, skb, sctp_hdr(skb), &asoc, &transport);
594*4882a593Smuzhiyun 	/* Put back, the original values. */
595*4882a593Smuzhiyun 	skb->network_header = saveip;
596*4882a593Smuzhiyun 	skb->transport_header = savesctp;
597*4882a593Smuzhiyun 	if (!sk) {
598*4882a593Smuzhiyun 		__ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
599*4882a593Smuzhiyun 		return -ENOENT;
600*4882a593Smuzhiyun 	}
601*4882a593Smuzhiyun 	/* Warning:  The sock lock is held.  Remember to call
602*4882a593Smuzhiyun 	 * sctp_err_finish!
603*4882a593Smuzhiyun 	 */
604*4882a593Smuzhiyun 
605*4882a593Smuzhiyun 	switch (type) {
606*4882a593Smuzhiyun 	case ICMP_PARAMETERPROB:
607*4882a593Smuzhiyun 		err = EPROTO;
608*4882a593Smuzhiyun 		break;
609*4882a593Smuzhiyun 	case ICMP_DEST_UNREACH:
610*4882a593Smuzhiyun 		if (code > NR_ICMP_UNREACH)
611*4882a593Smuzhiyun 			goto out_unlock;
612*4882a593Smuzhiyun 
613*4882a593Smuzhiyun 		/* PMTU discovery (RFC1191) */
614*4882a593Smuzhiyun 		if (ICMP_FRAG_NEEDED == code) {
615*4882a593Smuzhiyun 			sctp_icmp_frag_needed(sk, asoc, transport,
616*4882a593Smuzhiyun 					      SCTP_TRUNC4(info));
617*4882a593Smuzhiyun 			goto out_unlock;
618*4882a593Smuzhiyun 		} else {
619*4882a593Smuzhiyun 			if (ICMP_PROT_UNREACH == code) {
620*4882a593Smuzhiyun 				sctp_icmp_proto_unreachable(sk, asoc,
621*4882a593Smuzhiyun 							    transport);
622*4882a593Smuzhiyun 				goto out_unlock;
623*4882a593Smuzhiyun 			}
624*4882a593Smuzhiyun 		}
625*4882a593Smuzhiyun 		err = icmp_err_convert[code].errno;
626*4882a593Smuzhiyun 		break;
627*4882a593Smuzhiyun 	case ICMP_TIME_EXCEEDED:
628*4882a593Smuzhiyun 		/* Ignore any time exceeded errors due to fragment reassembly
629*4882a593Smuzhiyun 		 * timeouts.
630*4882a593Smuzhiyun 		 */
631*4882a593Smuzhiyun 		if (ICMP_EXC_FRAGTIME == code)
632*4882a593Smuzhiyun 			goto out_unlock;
633*4882a593Smuzhiyun 
634*4882a593Smuzhiyun 		err = EHOSTUNREACH;
635*4882a593Smuzhiyun 		break;
636*4882a593Smuzhiyun 	case ICMP_REDIRECT:
637*4882a593Smuzhiyun 		sctp_icmp_redirect(sk, transport, skb);
638*4882a593Smuzhiyun 		/* Fall through to out_unlock. */
639*4882a593Smuzhiyun 	default:
640*4882a593Smuzhiyun 		goto out_unlock;
641*4882a593Smuzhiyun 	}
642*4882a593Smuzhiyun 
643*4882a593Smuzhiyun 	inet = inet_sk(sk);
644*4882a593Smuzhiyun 	if (!sock_owned_by_user(sk) && inet->recverr) {
645*4882a593Smuzhiyun 		sk->sk_err = err;
646*4882a593Smuzhiyun 		sk->sk_error_report(sk);
647*4882a593Smuzhiyun 	} else {  /* Only an error on timeout */
648*4882a593Smuzhiyun 		sk->sk_err_soft = err;
649*4882a593Smuzhiyun 	}
650*4882a593Smuzhiyun 
651*4882a593Smuzhiyun out_unlock:
652*4882a593Smuzhiyun 	sctp_err_finish(sk, transport);
653*4882a593Smuzhiyun 	return 0;
654*4882a593Smuzhiyun }
655*4882a593Smuzhiyun 
656*4882a593Smuzhiyun /*
657*4882a593Smuzhiyun  * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
658*4882a593Smuzhiyun  *
659*4882a593Smuzhiyun  * This function scans all the chunks in the OOTB packet to determine if
660*4882a593Smuzhiyun  * the packet should be discarded right away.  If a response might be needed
661*4882a593Smuzhiyun  * for this packet, or, if further processing is possible, the packet will
662*4882a593Smuzhiyun  * be queued to a proper inqueue for the next phase of handling.
663*4882a593Smuzhiyun  *
664*4882a593Smuzhiyun  * Output:
665*4882a593Smuzhiyun  * Return 0 - If further processing is needed.
666*4882a593Smuzhiyun  * Return 1 - If the packet can be discarded right away.
667*4882a593Smuzhiyun  */
sctp_rcv_ootb(struct sk_buff * skb)668*4882a593Smuzhiyun static int sctp_rcv_ootb(struct sk_buff *skb)
669*4882a593Smuzhiyun {
670*4882a593Smuzhiyun 	struct sctp_chunkhdr *ch, _ch;
671*4882a593Smuzhiyun 	int ch_end, offset = 0;
672*4882a593Smuzhiyun 
673*4882a593Smuzhiyun 	/* Scan through all the chunks in the packet.  */
674*4882a593Smuzhiyun 	do {
675*4882a593Smuzhiyun 		/* Make sure we have at least the header there */
676*4882a593Smuzhiyun 		if (offset + sizeof(_ch) > skb->len)
677*4882a593Smuzhiyun 			break;
678*4882a593Smuzhiyun 
679*4882a593Smuzhiyun 		ch = skb_header_pointer(skb, offset, sizeof(*ch), &_ch);
680*4882a593Smuzhiyun 
681*4882a593Smuzhiyun 		/* Break out if chunk length is less then minimal. */
682*4882a593Smuzhiyun 		if (!ch || ntohs(ch->length) < sizeof(_ch))
683*4882a593Smuzhiyun 			break;
684*4882a593Smuzhiyun 
685*4882a593Smuzhiyun 		ch_end = offset + SCTP_PAD4(ntohs(ch->length));
686*4882a593Smuzhiyun 		if (ch_end > skb->len)
687*4882a593Smuzhiyun 			break;
688*4882a593Smuzhiyun 
689*4882a593Smuzhiyun 		/* RFC 8.4, 2) If the OOTB packet contains an ABORT chunk, the
690*4882a593Smuzhiyun 		 * receiver MUST silently discard the OOTB packet and take no
691*4882a593Smuzhiyun 		 * further action.
692*4882a593Smuzhiyun 		 */
693*4882a593Smuzhiyun 		if (SCTP_CID_ABORT == ch->type)
694*4882a593Smuzhiyun 			goto discard;
695*4882a593Smuzhiyun 
696*4882a593Smuzhiyun 		/* RFC 8.4, 6) If the packet contains a SHUTDOWN COMPLETE
697*4882a593Smuzhiyun 		 * chunk, the receiver should silently discard the packet
698*4882a593Smuzhiyun 		 * and take no further action.
699*4882a593Smuzhiyun 		 */
700*4882a593Smuzhiyun 		if (SCTP_CID_SHUTDOWN_COMPLETE == ch->type)
701*4882a593Smuzhiyun 			goto discard;
702*4882a593Smuzhiyun 
703*4882a593Smuzhiyun 		/* RFC 4460, 2.11.2
704*4882a593Smuzhiyun 		 * This will discard packets with INIT chunk bundled as
705*4882a593Smuzhiyun 		 * subsequent chunks in the packet.  When INIT is first,
706*4882a593Smuzhiyun 		 * the normal INIT processing will discard the chunk.
707*4882a593Smuzhiyun 		 */
708*4882a593Smuzhiyun 		if (SCTP_CID_INIT == ch->type && (void *)ch != skb->data)
709*4882a593Smuzhiyun 			goto discard;
710*4882a593Smuzhiyun 
711*4882a593Smuzhiyun 		offset = ch_end;
712*4882a593Smuzhiyun 	} while (ch_end < skb->len);
713*4882a593Smuzhiyun 
714*4882a593Smuzhiyun 	return 0;
715*4882a593Smuzhiyun 
716*4882a593Smuzhiyun discard:
717*4882a593Smuzhiyun 	return 1;
718*4882a593Smuzhiyun }
719*4882a593Smuzhiyun 
720*4882a593Smuzhiyun /* Insert endpoint into the hash table.  */
__sctp_hash_endpoint(struct sctp_endpoint * ep)721*4882a593Smuzhiyun static int __sctp_hash_endpoint(struct sctp_endpoint *ep)
722*4882a593Smuzhiyun {
723*4882a593Smuzhiyun 	struct sock *sk = ep->base.sk;
724*4882a593Smuzhiyun 	struct net *net = sock_net(sk);
725*4882a593Smuzhiyun 	struct sctp_hashbucket *head;
726*4882a593Smuzhiyun 	struct sctp_ep_common *epb;
727*4882a593Smuzhiyun 
728*4882a593Smuzhiyun 	epb = &ep->base;
729*4882a593Smuzhiyun 	epb->hashent = sctp_ep_hashfn(net, epb->bind_addr.port);
730*4882a593Smuzhiyun 	head = &sctp_ep_hashtable[epb->hashent];
731*4882a593Smuzhiyun 
732*4882a593Smuzhiyun 	if (sk->sk_reuseport) {
733*4882a593Smuzhiyun 		bool any = sctp_is_ep_boundall(sk);
734*4882a593Smuzhiyun 		struct sctp_ep_common *epb2;
735*4882a593Smuzhiyun 		struct list_head *list;
736*4882a593Smuzhiyun 		int cnt = 0, err = 1;
737*4882a593Smuzhiyun 
738*4882a593Smuzhiyun 		list_for_each(list, &ep->base.bind_addr.address_list)
739*4882a593Smuzhiyun 			cnt++;
740*4882a593Smuzhiyun 
741*4882a593Smuzhiyun 		sctp_for_each_hentry(epb2, &head->chain) {
742*4882a593Smuzhiyun 			struct sock *sk2 = epb2->sk;
743*4882a593Smuzhiyun 
744*4882a593Smuzhiyun 			if (!net_eq(sock_net(sk2), net) || sk2 == sk ||
745*4882a593Smuzhiyun 			    !uid_eq(sock_i_uid(sk2), sock_i_uid(sk)) ||
746*4882a593Smuzhiyun 			    !sk2->sk_reuseport)
747*4882a593Smuzhiyun 				continue;
748*4882a593Smuzhiyun 
749*4882a593Smuzhiyun 			err = sctp_bind_addrs_check(sctp_sk(sk2),
750*4882a593Smuzhiyun 						    sctp_sk(sk), cnt);
751*4882a593Smuzhiyun 			if (!err) {
752*4882a593Smuzhiyun 				err = reuseport_add_sock(sk, sk2, any);
753*4882a593Smuzhiyun 				if (err)
754*4882a593Smuzhiyun 					return err;
755*4882a593Smuzhiyun 				break;
756*4882a593Smuzhiyun 			} else if (err < 0) {
757*4882a593Smuzhiyun 				return err;
758*4882a593Smuzhiyun 			}
759*4882a593Smuzhiyun 		}
760*4882a593Smuzhiyun 
761*4882a593Smuzhiyun 		if (err) {
762*4882a593Smuzhiyun 			err = reuseport_alloc(sk, any);
763*4882a593Smuzhiyun 			if (err)
764*4882a593Smuzhiyun 				return err;
765*4882a593Smuzhiyun 		}
766*4882a593Smuzhiyun 	}
767*4882a593Smuzhiyun 
768*4882a593Smuzhiyun 	write_lock(&head->lock);
769*4882a593Smuzhiyun 	hlist_add_head(&epb->node, &head->chain);
770*4882a593Smuzhiyun 	write_unlock(&head->lock);
771*4882a593Smuzhiyun 	return 0;
772*4882a593Smuzhiyun }
773*4882a593Smuzhiyun 
774*4882a593Smuzhiyun /* Add an endpoint to the hash. Local BH-safe. */
sctp_hash_endpoint(struct sctp_endpoint * ep)775*4882a593Smuzhiyun int sctp_hash_endpoint(struct sctp_endpoint *ep)
776*4882a593Smuzhiyun {
777*4882a593Smuzhiyun 	int err;
778*4882a593Smuzhiyun 
779*4882a593Smuzhiyun 	local_bh_disable();
780*4882a593Smuzhiyun 	err = __sctp_hash_endpoint(ep);
781*4882a593Smuzhiyun 	local_bh_enable();
782*4882a593Smuzhiyun 
783*4882a593Smuzhiyun 	return err;
784*4882a593Smuzhiyun }
785*4882a593Smuzhiyun 
786*4882a593Smuzhiyun /* Remove endpoint from the hash table.  */
__sctp_unhash_endpoint(struct sctp_endpoint * ep)787*4882a593Smuzhiyun static void __sctp_unhash_endpoint(struct sctp_endpoint *ep)
788*4882a593Smuzhiyun {
789*4882a593Smuzhiyun 	struct sock *sk = ep->base.sk;
790*4882a593Smuzhiyun 	struct sctp_hashbucket *head;
791*4882a593Smuzhiyun 	struct sctp_ep_common *epb;
792*4882a593Smuzhiyun 
793*4882a593Smuzhiyun 	epb = &ep->base;
794*4882a593Smuzhiyun 
795*4882a593Smuzhiyun 	epb->hashent = sctp_ep_hashfn(sock_net(sk), epb->bind_addr.port);
796*4882a593Smuzhiyun 
797*4882a593Smuzhiyun 	head = &sctp_ep_hashtable[epb->hashent];
798*4882a593Smuzhiyun 
799*4882a593Smuzhiyun 	if (rcu_access_pointer(sk->sk_reuseport_cb))
800*4882a593Smuzhiyun 		reuseport_detach_sock(sk);
801*4882a593Smuzhiyun 
802*4882a593Smuzhiyun 	write_lock(&head->lock);
803*4882a593Smuzhiyun 	hlist_del_init(&epb->node);
804*4882a593Smuzhiyun 	write_unlock(&head->lock);
805*4882a593Smuzhiyun }
806*4882a593Smuzhiyun 
807*4882a593Smuzhiyun /* Remove endpoint from the hash.  Local BH-safe. */
sctp_unhash_endpoint(struct sctp_endpoint * ep)808*4882a593Smuzhiyun void sctp_unhash_endpoint(struct sctp_endpoint *ep)
809*4882a593Smuzhiyun {
810*4882a593Smuzhiyun 	local_bh_disable();
811*4882a593Smuzhiyun 	__sctp_unhash_endpoint(ep);
812*4882a593Smuzhiyun 	local_bh_enable();
813*4882a593Smuzhiyun }
814*4882a593Smuzhiyun 
sctp_hashfn(const struct net * net,__be16 lport,const union sctp_addr * paddr,__u32 seed)815*4882a593Smuzhiyun static inline __u32 sctp_hashfn(const struct net *net, __be16 lport,
816*4882a593Smuzhiyun 				const union sctp_addr *paddr, __u32 seed)
817*4882a593Smuzhiyun {
818*4882a593Smuzhiyun 	__u32 addr;
819*4882a593Smuzhiyun 
820*4882a593Smuzhiyun 	if (paddr->sa.sa_family == AF_INET6)
821*4882a593Smuzhiyun 		addr = jhash(&paddr->v6.sin6_addr, 16, seed);
822*4882a593Smuzhiyun 	else
823*4882a593Smuzhiyun 		addr = (__force __u32)paddr->v4.sin_addr.s_addr;
824*4882a593Smuzhiyun 
825*4882a593Smuzhiyun 	return  jhash_3words(addr, ((__force __u32)paddr->v4.sin_port) << 16 |
826*4882a593Smuzhiyun 			     (__force __u32)lport, net_hash_mix(net), seed);
827*4882a593Smuzhiyun }
828*4882a593Smuzhiyun 
829*4882a593Smuzhiyun /* Look up an endpoint. */
__sctp_rcv_lookup_endpoint(struct net * net,struct sk_buff * skb,const union sctp_addr * laddr,const union sctp_addr * paddr)830*4882a593Smuzhiyun static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(
831*4882a593Smuzhiyun 					struct net *net, struct sk_buff *skb,
832*4882a593Smuzhiyun 					const union sctp_addr *laddr,
833*4882a593Smuzhiyun 					const union sctp_addr *paddr)
834*4882a593Smuzhiyun {
835*4882a593Smuzhiyun 	struct sctp_hashbucket *head;
836*4882a593Smuzhiyun 	struct sctp_ep_common *epb;
837*4882a593Smuzhiyun 	struct sctp_endpoint *ep;
838*4882a593Smuzhiyun 	struct sock *sk;
839*4882a593Smuzhiyun 	__be16 lport;
840*4882a593Smuzhiyun 	int hash;
841*4882a593Smuzhiyun 
842*4882a593Smuzhiyun 	lport = laddr->v4.sin_port;
843*4882a593Smuzhiyun 	hash = sctp_ep_hashfn(net, ntohs(lport));
844*4882a593Smuzhiyun 	head = &sctp_ep_hashtable[hash];
845*4882a593Smuzhiyun 	read_lock(&head->lock);
846*4882a593Smuzhiyun 	sctp_for_each_hentry(epb, &head->chain) {
847*4882a593Smuzhiyun 		ep = sctp_ep(epb);
848*4882a593Smuzhiyun 		if (sctp_endpoint_is_match(ep, net, laddr))
849*4882a593Smuzhiyun 			goto hit;
850*4882a593Smuzhiyun 	}
851*4882a593Smuzhiyun 
852*4882a593Smuzhiyun 	ep = sctp_sk(net->sctp.ctl_sock)->ep;
853*4882a593Smuzhiyun 
854*4882a593Smuzhiyun hit:
855*4882a593Smuzhiyun 	sk = ep->base.sk;
856*4882a593Smuzhiyun 	if (sk->sk_reuseport) {
857*4882a593Smuzhiyun 		__u32 phash = sctp_hashfn(net, lport, paddr, 0);
858*4882a593Smuzhiyun 
859*4882a593Smuzhiyun 		sk = reuseport_select_sock(sk, phash, skb,
860*4882a593Smuzhiyun 					   sizeof(struct sctphdr));
861*4882a593Smuzhiyun 		if (sk)
862*4882a593Smuzhiyun 			ep = sctp_sk(sk)->ep;
863*4882a593Smuzhiyun 	}
864*4882a593Smuzhiyun 	sctp_endpoint_hold(ep);
865*4882a593Smuzhiyun 	read_unlock(&head->lock);
866*4882a593Smuzhiyun 	return ep;
867*4882a593Smuzhiyun }
868*4882a593Smuzhiyun 
869*4882a593Smuzhiyun /* rhashtable for transport */
870*4882a593Smuzhiyun struct sctp_hash_cmp_arg {
871*4882a593Smuzhiyun 	const union sctp_addr	*paddr;
872*4882a593Smuzhiyun 	const struct net	*net;
873*4882a593Smuzhiyun 	__be16			lport;
874*4882a593Smuzhiyun };
875*4882a593Smuzhiyun 
sctp_hash_cmp(struct rhashtable_compare_arg * arg,const void * ptr)876*4882a593Smuzhiyun static inline int sctp_hash_cmp(struct rhashtable_compare_arg *arg,
877*4882a593Smuzhiyun 				const void *ptr)
878*4882a593Smuzhiyun {
879*4882a593Smuzhiyun 	struct sctp_transport *t = (struct sctp_transport *)ptr;
880*4882a593Smuzhiyun 	const struct sctp_hash_cmp_arg *x = arg->key;
881*4882a593Smuzhiyun 	int err = 1;
882*4882a593Smuzhiyun 
883*4882a593Smuzhiyun 	if (!sctp_cmp_addr_exact(&t->ipaddr, x->paddr))
884*4882a593Smuzhiyun 		return err;
885*4882a593Smuzhiyun 	if (!sctp_transport_hold(t))
886*4882a593Smuzhiyun 		return err;
887*4882a593Smuzhiyun 
888*4882a593Smuzhiyun 	if (!net_eq(t->asoc->base.net, x->net))
889*4882a593Smuzhiyun 		goto out;
890*4882a593Smuzhiyun 	if (x->lport != htons(t->asoc->base.bind_addr.port))
891*4882a593Smuzhiyun 		goto out;
892*4882a593Smuzhiyun 
893*4882a593Smuzhiyun 	err = 0;
894*4882a593Smuzhiyun out:
895*4882a593Smuzhiyun 	sctp_transport_put(t);
896*4882a593Smuzhiyun 	return err;
897*4882a593Smuzhiyun }
898*4882a593Smuzhiyun 
sctp_hash_obj(const void * data,u32 len,u32 seed)899*4882a593Smuzhiyun static inline __u32 sctp_hash_obj(const void *data, u32 len, u32 seed)
900*4882a593Smuzhiyun {
901*4882a593Smuzhiyun 	const struct sctp_transport *t = data;
902*4882a593Smuzhiyun 
903*4882a593Smuzhiyun 	return sctp_hashfn(t->asoc->base.net,
904*4882a593Smuzhiyun 			   htons(t->asoc->base.bind_addr.port),
905*4882a593Smuzhiyun 			   &t->ipaddr, seed);
906*4882a593Smuzhiyun }
907*4882a593Smuzhiyun 
sctp_hash_key(const void * data,u32 len,u32 seed)908*4882a593Smuzhiyun static inline __u32 sctp_hash_key(const void *data, u32 len, u32 seed)
909*4882a593Smuzhiyun {
910*4882a593Smuzhiyun 	const struct sctp_hash_cmp_arg *x = data;
911*4882a593Smuzhiyun 
912*4882a593Smuzhiyun 	return sctp_hashfn(x->net, x->lport, x->paddr, seed);
913*4882a593Smuzhiyun }
914*4882a593Smuzhiyun 
915*4882a593Smuzhiyun static const struct rhashtable_params sctp_hash_params = {
916*4882a593Smuzhiyun 	.head_offset		= offsetof(struct sctp_transport, node),
917*4882a593Smuzhiyun 	.hashfn			= sctp_hash_key,
918*4882a593Smuzhiyun 	.obj_hashfn		= sctp_hash_obj,
919*4882a593Smuzhiyun 	.obj_cmpfn		= sctp_hash_cmp,
920*4882a593Smuzhiyun 	.automatic_shrinking	= true,
921*4882a593Smuzhiyun };
922*4882a593Smuzhiyun 
sctp_transport_hashtable_init(void)923*4882a593Smuzhiyun int sctp_transport_hashtable_init(void)
924*4882a593Smuzhiyun {
925*4882a593Smuzhiyun 	return rhltable_init(&sctp_transport_hashtable, &sctp_hash_params);
926*4882a593Smuzhiyun }
927*4882a593Smuzhiyun 
sctp_transport_hashtable_destroy(void)928*4882a593Smuzhiyun void sctp_transport_hashtable_destroy(void)
929*4882a593Smuzhiyun {
930*4882a593Smuzhiyun 	rhltable_destroy(&sctp_transport_hashtable);
931*4882a593Smuzhiyun }
932*4882a593Smuzhiyun 
sctp_hash_transport(struct sctp_transport * t)933*4882a593Smuzhiyun int sctp_hash_transport(struct sctp_transport *t)
934*4882a593Smuzhiyun {
935*4882a593Smuzhiyun 	struct sctp_transport *transport;
936*4882a593Smuzhiyun 	struct rhlist_head *tmp, *list;
937*4882a593Smuzhiyun 	struct sctp_hash_cmp_arg arg;
938*4882a593Smuzhiyun 	int err;
939*4882a593Smuzhiyun 
940*4882a593Smuzhiyun 	if (t->asoc->temp)
941*4882a593Smuzhiyun 		return 0;
942*4882a593Smuzhiyun 
943*4882a593Smuzhiyun 	arg.net   = t->asoc->base.net;
944*4882a593Smuzhiyun 	arg.paddr = &t->ipaddr;
945*4882a593Smuzhiyun 	arg.lport = htons(t->asoc->base.bind_addr.port);
946*4882a593Smuzhiyun 
947*4882a593Smuzhiyun 	rcu_read_lock();
948*4882a593Smuzhiyun 	list = rhltable_lookup(&sctp_transport_hashtable, &arg,
949*4882a593Smuzhiyun 			       sctp_hash_params);
950*4882a593Smuzhiyun 
951*4882a593Smuzhiyun 	rhl_for_each_entry_rcu(transport, tmp, list, node)
952*4882a593Smuzhiyun 		if (transport->asoc->ep == t->asoc->ep) {
953*4882a593Smuzhiyun 			rcu_read_unlock();
954*4882a593Smuzhiyun 			return -EEXIST;
955*4882a593Smuzhiyun 		}
956*4882a593Smuzhiyun 	rcu_read_unlock();
957*4882a593Smuzhiyun 
958*4882a593Smuzhiyun 	err = rhltable_insert_key(&sctp_transport_hashtable, &arg,
959*4882a593Smuzhiyun 				  &t->node, sctp_hash_params);
960*4882a593Smuzhiyun 	if (err)
961*4882a593Smuzhiyun 		pr_err_once("insert transport fail, errno %d\n", err);
962*4882a593Smuzhiyun 
963*4882a593Smuzhiyun 	return err;
964*4882a593Smuzhiyun }
965*4882a593Smuzhiyun 
sctp_unhash_transport(struct sctp_transport * t)966*4882a593Smuzhiyun void sctp_unhash_transport(struct sctp_transport *t)
967*4882a593Smuzhiyun {
968*4882a593Smuzhiyun 	if (t->asoc->temp)
969*4882a593Smuzhiyun 		return;
970*4882a593Smuzhiyun 
971*4882a593Smuzhiyun 	rhltable_remove(&sctp_transport_hashtable, &t->node,
972*4882a593Smuzhiyun 			sctp_hash_params);
973*4882a593Smuzhiyun }
974*4882a593Smuzhiyun 
975*4882a593Smuzhiyun /* return a transport with holding it */
sctp_addrs_lookup_transport(struct net * net,const union sctp_addr * laddr,const union sctp_addr * paddr)976*4882a593Smuzhiyun struct sctp_transport *sctp_addrs_lookup_transport(
977*4882a593Smuzhiyun 				struct net *net,
978*4882a593Smuzhiyun 				const union sctp_addr *laddr,
979*4882a593Smuzhiyun 				const union sctp_addr *paddr)
980*4882a593Smuzhiyun {
981*4882a593Smuzhiyun 	struct rhlist_head *tmp, *list;
982*4882a593Smuzhiyun 	struct sctp_transport *t;
983*4882a593Smuzhiyun 	struct sctp_hash_cmp_arg arg = {
984*4882a593Smuzhiyun 		.paddr = paddr,
985*4882a593Smuzhiyun 		.net   = net,
986*4882a593Smuzhiyun 		.lport = laddr->v4.sin_port,
987*4882a593Smuzhiyun 	};
988*4882a593Smuzhiyun 
989*4882a593Smuzhiyun 	list = rhltable_lookup(&sctp_transport_hashtable, &arg,
990*4882a593Smuzhiyun 			       sctp_hash_params);
991*4882a593Smuzhiyun 
992*4882a593Smuzhiyun 	rhl_for_each_entry_rcu(t, tmp, list, node) {
993*4882a593Smuzhiyun 		if (!sctp_transport_hold(t))
994*4882a593Smuzhiyun 			continue;
995*4882a593Smuzhiyun 
996*4882a593Smuzhiyun 		if (sctp_bind_addr_match(&t->asoc->base.bind_addr,
997*4882a593Smuzhiyun 					 laddr, sctp_sk(t->asoc->base.sk)))
998*4882a593Smuzhiyun 			return t;
999*4882a593Smuzhiyun 		sctp_transport_put(t);
1000*4882a593Smuzhiyun 	}
1001*4882a593Smuzhiyun 
1002*4882a593Smuzhiyun 	return NULL;
1003*4882a593Smuzhiyun }
1004*4882a593Smuzhiyun 
1005*4882a593Smuzhiyun /* return a transport without holding it, as it's only used under sock lock */
sctp_epaddr_lookup_transport(const struct sctp_endpoint * ep,const union sctp_addr * paddr)1006*4882a593Smuzhiyun struct sctp_transport *sctp_epaddr_lookup_transport(
1007*4882a593Smuzhiyun 				const struct sctp_endpoint *ep,
1008*4882a593Smuzhiyun 				const union sctp_addr *paddr)
1009*4882a593Smuzhiyun {
1010*4882a593Smuzhiyun 	struct rhlist_head *tmp, *list;
1011*4882a593Smuzhiyun 	struct sctp_transport *t;
1012*4882a593Smuzhiyun 	struct sctp_hash_cmp_arg arg = {
1013*4882a593Smuzhiyun 		.paddr = paddr,
1014*4882a593Smuzhiyun 		.net   = ep->base.net,
1015*4882a593Smuzhiyun 		.lport = htons(ep->base.bind_addr.port),
1016*4882a593Smuzhiyun 	};
1017*4882a593Smuzhiyun 
1018*4882a593Smuzhiyun 	list = rhltable_lookup(&sctp_transport_hashtable, &arg,
1019*4882a593Smuzhiyun 			       sctp_hash_params);
1020*4882a593Smuzhiyun 
1021*4882a593Smuzhiyun 	rhl_for_each_entry_rcu(t, tmp, list, node)
1022*4882a593Smuzhiyun 		if (ep == t->asoc->ep)
1023*4882a593Smuzhiyun 			return t;
1024*4882a593Smuzhiyun 
1025*4882a593Smuzhiyun 	return NULL;
1026*4882a593Smuzhiyun }
1027*4882a593Smuzhiyun 
1028*4882a593Smuzhiyun /* Look up an association. */
__sctp_lookup_association(struct net * net,const union sctp_addr * local,const union sctp_addr * peer,struct sctp_transport ** pt)1029*4882a593Smuzhiyun static struct sctp_association *__sctp_lookup_association(
1030*4882a593Smuzhiyun 					struct net *net,
1031*4882a593Smuzhiyun 					const union sctp_addr *local,
1032*4882a593Smuzhiyun 					const union sctp_addr *peer,
1033*4882a593Smuzhiyun 					struct sctp_transport **pt)
1034*4882a593Smuzhiyun {
1035*4882a593Smuzhiyun 	struct sctp_transport *t;
1036*4882a593Smuzhiyun 	struct sctp_association *asoc = NULL;
1037*4882a593Smuzhiyun 
1038*4882a593Smuzhiyun 	t = sctp_addrs_lookup_transport(net, local, peer);
1039*4882a593Smuzhiyun 	if (!t)
1040*4882a593Smuzhiyun 		goto out;
1041*4882a593Smuzhiyun 
1042*4882a593Smuzhiyun 	asoc = t->asoc;
1043*4882a593Smuzhiyun 	*pt = t;
1044*4882a593Smuzhiyun 
1045*4882a593Smuzhiyun out:
1046*4882a593Smuzhiyun 	return asoc;
1047*4882a593Smuzhiyun }
1048*4882a593Smuzhiyun 
1049*4882a593Smuzhiyun /* Look up an association. protected by RCU read lock */
1050*4882a593Smuzhiyun static
sctp_lookup_association(struct net * net,const union sctp_addr * laddr,const union sctp_addr * paddr,struct sctp_transport ** transportp)1051*4882a593Smuzhiyun struct sctp_association *sctp_lookup_association(struct net *net,
1052*4882a593Smuzhiyun 						 const union sctp_addr *laddr,
1053*4882a593Smuzhiyun 						 const union sctp_addr *paddr,
1054*4882a593Smuzhiyun 						 struct sctp_transport **transportp)
1055*4882a593Smuzhiyun {
1056*4882a593Smuzhiyun 	struct sctp_association *asoc;
1057*4882a593Smuzhiyun 
1058*4882a593Smuzhiyun 	rcu_read_lock();
1059*4882a593Smuzhiyun 	asoc = __sctp_lookup_association(net, laddr, paddr, transportp);
1060*4882a593Smuzhiyun 	rcu_read_unlock();
1061*4882a593Smuzhiyun 
1062*4882a593Smuzhiyun 	return asoc;
1063*4882a593Smuzhiyun }
1064*4882a593Smuzhiyun 
1065*4882a593Smuzhiyun /* Is there an association matching the given local and peer addresses? */
sctp_has_association(struct net * net,const union sctp_addr * laddr,const union sctp_addr * paddr)1066*4882a593Smuzhiyun bool sctp_has_association(struct net *net,
1067*4882a593Smuzhiyun 			  const union sctp_addr *laddr,
1068*4882a593Smuzhiyun 			  const union sctp_addr *paddr)
1069*4882a593Smuzhiyun {
1070*4882a593Smuzhiyun 	struct sctp_transport *transport;
1071*4882a593Smuzhiyun 
1072*4882a593Smuzhiyun 	if (sctp_lookup_association(net, laddr, paddr, &transport)) {
1073*4882a593Smuzhiyun 		sctp_transport_put(transport);
1074*4882a593Smuzhiyun 		return true;
1075*4882a593Smuzhiyun 	}
1076*4882a593Smuzhiyun 
1077*4882a593Smuzhiyun 	return false;
1078*4882a593Smuzhiyun }
1079*4882a593Smuzhiyun 
1080*4882a593Smuzhiyun /*
1081*4882a593Smuzhiyun  * SCTP Implementors Guide, 2.18 Handling of address
1082*4882a593Smuzhiyun  * parameters within the INIT or INIT-ACK.
1083*4882a593Smuzhiyun  *
1084*4882a593Smuzhiyun  * D) When searching for a matching TCB upon reception of an INIT
1085*4882a593Smuzhiyun  *    or INIT-ACK chunk the receiver SHOULD use not only the
1086*4882a593Smuzhiyun  *    source address of the packet (containing the INIT or
1087*4882a593Smuzhiyun  *    INIT-ACK) but the receiver SHOULD also use all valid
1088*4882a593Smuzhiyun  *    address parameters contained within the chunk.
1089*4882a593Smuzhiyun  *
1090*4882a593Smuzhiyun  * 2.18.3 Solution description
1091*4882a593Smuzhiyun  *
1092*4882a593Smuzhiyun  * This new text clearly specifies to an implementor the need
1093*4882a593Smuzhiyun  * to look within the INIT or INIT-ACK. Any implementation that
1094*4882a593Smuzhiyun  * does not do this, may not be able to establish associations
1095*4882a593Smuzhiyun  * in certain circumstances.
1096*4882a593Smuzhiyun  *
1097*4882a593Smuzhiyun  */
__sctp_rcv_init_lookup(struct net * net,struct sk_buff * skb,const union sctp_addr * laddr,struct sctp_transport ** transportp)1098*4882a593Smuzhiyun static struct sctp_association *__sctp_rcv_init_lookup(struct net *net,
1099*4882a593Smuzhiyun 	struct sk_buff *skb,
1100*4882a593Smuzhiyun 	const union sctp_addr *laddr, struct sctp_transport **transportp)
1101*4882a593Smuzhiyun {
1102*4882a593Smuzhiyun 	struct sctp_association *asoc;
1103*4882a593Smuzhiyun 	union sctp_addr addr;
1104*4882a593Smuzhiyun 	union sctp_addr *paddr = &addr;
1105*4882a593Smuzhiyun 	struct sctphdr *sh = sctp_hdr(skb);
1106*4882a593Smuzhiyun 	union sctp_params params;
1107*4882a593Smuzhiyun 	struct sctp_init_chunk *init;
1108*4882a593Smuzhiyun 	struct sctp_af *af;
1109*4882a593Smuzhiyun 
1110*4882a593Smuzhiyun 	/*
1111*4882a593Smuzhiyun 	 * This code will NOT touch anything inside the chunk--it is
1112*4882a593Smuzhiyun 	 * strictly READ-ONLY.
1113*4882a593Smuzhiyun 	 *
1114*4882a593Smuzhiyun 	 * RFC 2960 3  SCTP packet Format
1115*4882a593Smuzhiyun 	 *
1116*4882a593Smuzhiyun 	 * Multiple chunks can be bundled into one SCTP packet up to
1117*4882a593Smuzhiyun 	 * the MTU size, except for the INIT, INIT ACK, and SHUTDOWN
1118*4882a593Smuzhiyun 	 * COMPLETE chunks.  These chunks MUST NOT be bundled with any
1119*4882a593Smuzhiyun 	 * other chunk in a packet.  See Section 6.10 for more details
1120*4882a593Smuzhiyun 	 * on chunk bundling.
1121*4882a593Smuzhiyun 	 */
1122*4882a593Smuzhiyun 
1123*4882a593Smuzhiyun 	/* Find the start of the TLVs and the end of the chunk.  This is
1124*4882a593Smuzhiyun 	 * the region we search for address parameters.
1125*4882a593Smuzhiyun 	 */
1126*4882a593Smuzhiyun 	init = (struct sctp_init_chunk *)skb->data;
1127*4882a593Smuzhiyun 
1128*4882a593Smuzhiyun 	/* Walk the parameters looking for embedded addresses. */
1129*4882a593Smuzhiyun 	sctp_walk_params(params, init, init_hdr.params) {
1130*4882a593Smuzhiyun 
1131*4882a593Smuzhiyun 		/* Note: Ignoring hostname addresses. */
1132*4882a593Smuzhiyun 		af = sctp_get_af_specific(param_type2af(params.p->type));
1133*4882a593Smuzhiyun 		if (!af)
1134*4882a593Smuzhiyun 			continue;
1135*4882a593Smuzhiyun 
1136*4882a593Smuzhiyun 		if (!af->from_addr_param(paddr, params.addr, sh->source, 0))
1137*4882a593Smuzhiyun 			continue;
1138*4882a593Smuzhiyun 
1139*4882a593Smuzhiyun 		asoc = __sctp_lookup_association(net, laddr, paddr, transportp);
1140*4882a593Smuzhiyun 		if (asoc)
1141*4882a593Smuzhiyun 			return asoc;
1142*4882a593Smuzhiyun 	}
1143*4882a593Smuzhiyun 
1144*4882a593Smuzhiyun 	return NULL;
1145*4882a593Smuzhiyun }
1146*4882a593Smuzhiyun 
1147*4882a593Smuzhiyun /* ADD-IP, Section 5.2
1148*4882a593Smuzhiyun  * When an endpoint receives an ASCONF Chunk from the remote peer
1149*4882a593Smuzhiyun  * special procedures may be needed to identify the association the
1150*4882a593Smuzhiyun  * ASCONF Chunk is associated with. To properly find the association
1151*4882a593Smuzhiyun  * the following procedures SHOULD be followed:
1152*4882a593Smuzhiyun  *
1153*4882a593Smuzhiyun  * D2) If the association is not found, use the address found in the
1154*4882a593Smuzhiyun  * Address Parameter TLV combined with the port number found in the
1155*4882a593Smuzhiyun  * SCTP common header. If found proceed to rule D4.
1156*4882a593Smuzhiyun  *
1157*4882a593Smuzhiyun  * D2-ext) If more than one ASCONF Chunks are packed together, use the
1158*4882a593Smuzhiyun  * address found in the ASCONF Address Parameter TLV of each of the
1159*4882a593Smuzhiyun  * subsequent ASCONF Chunks. If found, proceed to rule D4.
1160*4882a593Smuzhiyun  */
__sctp_rcv_asconf_lookup(struct net * net,struct sctp_chunkhdr * ch,const union sctp_addr * laddr,__be16 peer_port,struct sctp_transport ** transportp)1161*4882a593Smuzhiyun static struct sctp_association *__sctp_rcv_asconf_lookup(
1162*4882a593Smuzhiyun 					struct net *net,
1163*4882a593Smuzhiyun 					struct sctp_chunkhdr *ch,
1164*4882a593Smuzhiyun 					const union sctp_addr *laddr,
1165*4882a593Smuzhiyun 					__be16 peer_port,
1166*4882a593Smuzhiyun 					struct sctp_transport **transportp)
1167*4882a593Smuzhiyun {
1168*4882a593Smuzhiyun 	struct sctp_addip_chunk *asconf = (struct sctp_addip_chunk *)ch;
1169*4882a593Smuzhiyun 	struct sctp_af *af;
1170*4882a593Smuzhiyun 	union sctp_addr_param *param;
1171*4882a593Smuzhiyun 	union sctp_addr paddr;
1172*4882a593Smuzhiyun 
1173*4882a593Smuzhiyun 	if (ntohs(ch->length) < sizeof(*asconf) + sizeof(struct sctp_paramhdr))
1174*4882a593Smuzhiyun 		return NULL;
1175*4882a593Smuzhiyun 
1176*4882a593Smuzhiyun 	/* Skip over the ADDIP header and find the Address parameter */
1177*4882a593Smuzhiyun 	param = (union sctp_addr_param *)(asconf + 1);
1178*4882a593Smuzhiyun 
1179*4882a593Smuzhiyun 	af = sctp_get_af_specific(param_type2af(param->p.type));
1180*4882a593Smuzhiyun 	if (unlikely(!af))
1181*4882a593Smuzhiyun 		return NULL;
1182*4882a593Smuzhiyun 
1183*4882a593Smuzhiyun 	if (!af->from_addr_param(&paddr, param, peer_port, 0))
1184*4882a593Smuzhiyun 		return NULL;
1185*4882a593Smuzhiyun 
1186*4882a593Smuzhiyun 	return __sctp_lookup_association(net, laddr, &paddr, transportp);
1187*4882a593Smuzhiyun }
1188*4882a593Smuzhiyun 
1189*4882a593Smuzhiyun 
1190*4882a593Smuzhiyun /* SCTP-AUTH, Section 6.3:
1191*4882a593Smuzhiyun *    If the receiver does not find a STCB for a packet containing an AUTH
1192*4882a593Smuzhiyun *    chunk as the first chunk and not a COOKIE-ECHO chunk as the second
1193*4882a593Smuzhiyun *    chunk, it MUST use the chunks after the AUTH chunk to look up an existing
1194*4882a593Smuzhiyun *    association.
1195*4882a593Smuzhiyun *
1196*4882a593Smuzhiyun * This means that any chunks that can help us identify the association need
1197*4882a593Smuzhiyun * to be looked at to find this association.
1198*4882a593Smuzhiyun */
__sctp_rcv_walk_lookup(struct net * net,struct sk_buff * skb,const union sctp_addr * laddr,struct sctp_transport ** transportp)1199*4882a593Smuzhiyun static struct sctp_association *__sctp_rcv_walk_lookup(struct net *net,
1200*4882a593Smuzhiyun 				      struct sk_buff *skb,
1201*4882a593Smuzhiyun 				      const union sctp_addr *laddr,
1202*4882a593Smuzhiyun 				      struct sctp_transport **transportp)
1203*4882a593Smuzhiyun {
1204*4882a593Smuzhiyun 	struct sctp_association *asoc = NULL;
1205*4882a593Smuzhiyun 	struct sctp_chunkhdr *ch;
1206*4882a593Smuzhiyun 	int have_auth = 0;
1207*4882a593Smuzhiyun 	unsigned int chunk_num = 1;
1208*4882a593Smuzhiyun 	__u8 *ch_end;
1209*4882a593Smuzhiyun 
1210*4882a593Smuzhiyun 	/* Walk through the chunks looking for AUTH or ASCONF chunks
1211*4882a593Smuzhiyun 	 * to help us find the association.
1212*4882a593Smuzhiyun 	 */
1213*4882a593Smuzhiyun 	ch = (struct sctp_chunkhdr *)skb->data;
1214*4882a593Smuzhiyun 	do {
1215*4882a593Smuzhiyun 		/* Break out if chunk length is less then minimal. */
1216*4882a593Smuzhiyun 		if (ntohs(ch->length) < sizeof(*ch))
1217*4882a593Smuzhiyun 			break;
1218*4882a593Smuzhiyun 
1219*4882a593Smuzhiyun 		ch_end = ((__u8 *)ch) + SCTP_PAD4(ntohs(ch->length));
1220*4882a593Smuzhiyun 		if (ch_end > skb_tail_pointer(skb))
1221*4882a593Smuzhiyun 			break;
1222*4882a593Smuzhiyun 
1223*4882a593Smuzhiyun 		switch (ch->type) {
1224*4882a593Smuzhiyun 		case SCTP_CID_AUTH:
1225*4882a593Smuzhiyun 			have_auth = chunk_num;
1226*4882a593Smuzhiyun 			break;
1227*4882a593Smuzhiyun 
1228*4882a593Smuzhiyun 		case SCTP_CID_COOKIE_ECHO:
1229*4882a593Smuzhiyun 			/* If a packet arrives containing an AUTH chunk as
1230*4882a593Smuzhiyun 			 * a first chunk, a COOKIE-ECHO chunk as the second
1231*4882a593Smuzhiyun 			 * chunk, and possibly more chunks after them, and
1232*4882a593Smuzhiyun 			 * the receiver does not have an STCB for that
1233*4882a593Smuzhiyun 			 * packet, then authentication is based on
1234*4882a593Smuzhiyun 			 * the contents of the COOKIE- ECHO chunk.
1235*4882a593Smuzhiyun 			 */
1236*4882a593Smuzhiyun 			if (have_auth == 1 && chunk_num == 2)
1237*4882a593Smuzhiyun 				return NULL;
1238*4882a593Smuzhiyun 			break;
1239*4882a593Smuzhiyun 
1240*4882a593Smuzhiyun 		case SCTP_CID_ASCONF:
1241*4882a593Smuzhiyun 			if (have_auth || net->sctp.addip_noauth)
1242*4882a593Smuzhiyun 				asoc = __sctp_rcv_asconf_lookup(
1243*4882a593Smuzhiyun 						net, ch, laddr,
1244*4882a593Smuzhiyun 						sctp_hdr(skb)->source,
1245*4882a593Smuzhiyun 						transportp);
1246*4882a593Smuzhiyun 		default:
1247*4882a593Smuzhiyun 			break;
1248*4882a593Smuzhiyun 		}
1249*4882a593Smuzhiyun 
1250*4882a593Smuzhiyun 		if (asoc)
1251*4882a593Smuzhiyun 			break;
1252*4882a593Smuzhiyun 
1253*4882a593Smuzhiyun 		ch = (struct sctp_chunkhdr *)ch_end;
1254*4882a593Smuzhiyun 		chunk_num++;
1255*4882a593Smuzhiyun 	} while (ch_end + sizeof(*ch) < skb_tail_pointer(skb));
1256*4882a593Smuzhiyun 
1257*4882a593Smuzhiyun 	return asoc;
1258*4882a593Smuzhiyun }
1259*4882a593Smuzhiyun 
1260*4882a593Smuzhiyun /*
1261*4882a593Smuzhiyun  * There are circumstances when we need to look inside the SCTP packet
1262*4882a593Smuzhiyun  * for information to help us find the association.   Examples
1263*4882a593Smuzhiyun  * include looking inside of INIT/INIT-ACK chunks or after the AUTH
1264*4882a593Smuzhiyun  * chunks.
1265*4882a593Smuzhiyun  */
__sctp_rcv_lookup_harder(struct net * net,struct sk_buff * skb,const union sctp_addr * laddr,struct sctp_transport ** transportp)1266*4882a593Smuzhiyun static struct sctp_association *__sctp_rcv_lookup_harder(struct net *net,
1267*4882a593Smuzhiyun 				      struct sk_buff *skb,
1268*4882a593Smuzhiyun 				      const union sctp_addr *laddr,
1269*4882a593Smuzhiyun 				      struct sctp_transport **transportp)
1270*4882a593Smuzhiyun {
1271*4882a593Smuzhiyun 	struct sctp_chunkhdr *ch;
1272*4882a593Smuzhiyun 
1273*4882a593Smuzhiyun 	/* We do not allow GSO frames here as we need to linearize and
1274*4882a593Smuzhiyun 	 * then cannot guarantee frame boundaries. This shouldn't be an
1275*4882a593Smuzhiyun 	 * issue as packets hitting this are mostly INIT or INIT-ACK and
1276*4882a593Smuzhiyun 	 * those cannot be on GSO-style anyway.
1277*4882a593Smuzhiyun 	 */
1278*4882a593Smuzhiyun 	if (skb_is_gso(skb) && skb_is_gso_sctp(skb))
1279*4882a593Smuzhiyun 		return NULL;
1280*4882a593Smuzhiyun 
1281*4882a593Smuzhiyun 	ch = (struct sctp_chunkhdr *)skb->data;
1282*4882a593Smuzhiyun 
1283*4882a593Smuzhiyun 	/* The code below will attempt to walk the chunk and extract
1284*4882a593Smuzhiyun 	 * parameter information.  Before we do that, we need to verify
1285*4882a593Smuzhiyun 	 * that the chunk length doesn't cause overflow.  Otherwise, we'll
1286*4882a593Smuzhiyun 	 * walk off the end.
1287*4882a593Smuzhiyun 	 */
1288*4882a593Smuzhiyun 	if (SCTP_PAD4(ntohs(ch->length)) > skb->len)
1289*4882a593Smuzhiyun 		return NULL;
1290*4882a593Smuzhiyun 
1291*4882a593Smuzhiyun 	/* If this is INIT/INIT-ACK look inside the chunk too. */
1292*4882a593Smuzhiyun 	if (ch->type == SCTP_CID_INIT || ch->type == SCTP_CID_INIT_ACK)
1293*4882a593Smuzhiyun 		return __sctp_rcv_init_lookup(net, skb, laddr, transportp);
1294*4882a593Smuzhiyun 
1295*4882a593Smuzhiyun 	return __sctp_rcv_walk_lookup(net, skb, laddr, transportp);
1296*4882a593Smuzhiyun }
1297*4882a593Smuzhiyun 
1298*4882a593Smuzhiyun /* Lookup an association for an inbound skb. */
__sctp_rcv_lookup(struct net * net,struct sk_buff * skb,const union sctp_addr * paddr,const union sctp_addr * laddr,struct sctp_transport ** transportp)1299*4882a593Smuzhiyun static struct sctp_association *__sctp_rcv_lookup(struct net *net,
1300*4882a593Smuzhiyun 				      struct sk_buff *skb,
1301*4882a593Smuzhiyun 				      const union sctp_addr *paddr,
1302*4882a593Smuzhiyun 				      const union sctp_addr *laddr,
1303*4882a593Smuzhiyun 				      struct sctp_transport **transportp)
1304*4882a593Smuzhiyun {
1305*4882a593Smuzhiyun 	struct sctp_association *asoc;
1306*4882a593Smuzhiyun 
1307*4882a593Smuzhiyun 	asoc = __sctp_lookup_association(net, laddr, paddr, transportp);
1308*4882a593Smuzhiyun 	if (asoc)
1309*4882a593Smuzhiyun 		goto out;
1310*4882a593Smuzhiyun 
1311*4882a593Smuzhiyun 	/* Further lookup for INIT/INIT-ACK packets.
1312*4882a593Smuzhiyun 	 * SCTP Implementors Guide, 2.18 Handling of address
1313*4882a593Smuzhiyun 	 * parameters within the INIT or INIT-ACK.
1314*4882a593Smuzhiyun 	 */
1315*4882a593Smuzhiyun 	asoc = __sctp_rcv_lookup_harder(net, skb, laddr, transportp);
1316*4882a593Smuzhiyun 	if (asoc)
1317*4882a593Smuzhiyun 		goto out;
1318*4882a593Smuzhiyun 
1319*4882a593Smuzhiyun 	if (paddr->sa.sa_family == AF_INET)
1320*4882a593Smuzhiyun 		pr_debug("sctp: asoc not found for src:%pI4:%d dst:%pI4:%d\n",
1321*4882a593Smuzhiyun 			 &laddr->v4.sin_addr, ntohs(laddr->v4.sin_port),
1322*4882a593Smuzhiyun 			 &paddr->v4.sin_addr, ntohs(paddr->v4.sin_port));
1323*4882a593Smuzhiyun 	else
1324*4882a593Smuzhiyun 		pr_debug("sctp: asoc not found for src:%pI6:%d dst:%pI6:%d\n",
1325*4882a593Smuzhiyun 			 &laddr->v6.sin6_addr, ntohs(laddr->v6.sin6_port),
1326*4882a593Smuzhiyun 			 &paddr->v6.sin6_addr, ntohs(paddr->v6.sin6_port));
1327*4882a593Smuzhiyun 
1328*4882a593Smuzhiyun out:
1329*4882a593Smuzhiyun 	return asoc;
1330*4882a593Smuzhiyun }
1331