blob: 90e12bafdd4894624b3a63d625ed21c85b60a4f0 [file] [log] [blame]
Thomas Gleixner47505b82019-05-23 11:14:41 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Vlad Yasevich60c778b2008-01-11 09:57:09 -05002/* SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001-2003 International Business Machines, Corp.
6 * Copyright (c) 2001 Intel Corp.
7 * Copyright (c) 2001 Nokia, Inc.
8 * Copyright (c) 2001 La Monte H.P. Yarroll
9 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050010 * This file is part of the SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 * These functions handle all input from the IP layer into SCTP.
13 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * Please send any bug reports or fixes you make to the
15 * email address(es):
Daniel Borkmann91705c62013-07-23 14:51:47 +020016 * lksctp developers <linux-sctp@vger.kernel.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * Written or modified by:
19 * La Monte H.P. Yarroll <piggy@acm.org>
20 * Karl Knutson <karl@athena.chicago.il.us>
21 * Xingang Guo <xingang.guo@intel.com>
22 * Jon Grimm <jgrimm@us.ibm.com>
23 * Hui Huang <hui.huang@nokia.com>
24 * Daisy Chang <daisyc@us.ibm.com>
25 * Sridhar Samudrala <sri@us.ibm.com>
26 * Ardelle Fan <ardelle.fan@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 */
28
29#include <linux/types.h>
30#include <linux/list.h> /* For struct list_head */
31#include <linux/socket.h>
32#include <linux/ip.h>
33#include <linux/time.h> /* For struct timeval */
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <net/ip.h>
36#include <net/icmp.h>
37#include <net/snmp.h>
38#include <net/sock.h>
39#include <net/xfrm.h>
40#include <net/sctp/sctp.h>
41#include <net/sctp/sm.h>
Vlad Yasevich9ad09772007-12-16 14:06:41 -080042#include <net/sctp/checksum.h>
Pavel Emelyanovdcfc23c2008-07-14 23:03:00 -070043#include <net/net_namespace.h>
NeilBrown0eb71a92018-06-18 12:52:50 +100044#include <linux/rhashtable.h>
Xin Long532ae2f2018-11-12 18:27:15 +080045#include <net/sock_reuseport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47/* Forward declarations for internal helpers. */
48static int sctp_rcv_ootb(struct sk_buff *);
Eric W. Biederman4110cc22012-08-06 08:41:13 +000049static struct sctp_association *__sctp_rcv_lookup(struct net *net,
50 struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 const union sctp_addr *paddr,
Nicolas Dichtel57565992012-10-03 05:43:21 +000052 const union sctp_addr *laddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 struct sctp_transport **transportp);
Xin Long532ae2f2018-11-12 18:27:15 +080054static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(
55 struct net *net, struct sk_buff *skb,
56 const union sctp_addr *laddr,
57 const union sctp_addr *daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static struct sctp_association *__sctp_lookup_association(
Eric W. Biederman4110cc22012-08-06 08:41:13 +000059 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 const union sctp_addr *local,
61 const union sctp_addr *peer,
62 struct sctp_transport **pt);
63
Zhu Yi50b1a782010-03-04 18:01:44 +000064static int sctp_add_backlog(struct sock *sk, struct sk_buff *skb);
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -070065
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67/* Calculate the SCTP checksum of an SCTP packet. */
Eric W. Biedermanb01a2402012-08-06 08:47:55 +000068static inline int sctp_rcv_checksum(struct net *net, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Arnaldo Carvalho de Melo2c0fd382007-03-13 13:59:32 -030070 struct sctphdr *sh = sctp_hdr(skb);
Vlad Yasevich4458f042009-02-13 08:33:42 +000071 __le32 cmp = sh->checksum;
Joe Stringer024ec3d2013-07-25 10:52:05 +090072 __le32 val = sctp_compute_cksum(skb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 if (val != cmp) {
75 /* CRC failure, dump it. */
Eric Dumazet08e3bae2016-04-27 16:44:34 -070076 __SCTP_INC_STATS(net, SCTP_MIB_CHECKSUMERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 return -1;
78 }
79 return 0;
80}
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082/*
83 * This is the routine which IP calls when receiving an SCTP packet.
84 */
85int sctp_rcv(struct sk_buff *skb)
86{
87 struct sock *sk;
88 struct sctp_association *asoc;
89 struct sctp_endpoint *ep = NULL;
90 struct sctp_ep_common *rcvr;
91 struct sctp_transport *transport = NULL;
92 struct sctp_chunk *chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 union sctp_addr src;
94 union sctp_addr dest;
95 int family;
96 struct sctp_af *af;
Eric W. Biederman4cdadcb2012-08-06 08:40:21 +000097 struct net *net = dev_net(skb->dev);
Daniel Axtens1dd27cd2018-03-09 14:06:09 +110098 bool is_gso = skb_is_gso(skb) && skb_is_gso_sctp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
wangweidongcb3f8372013-12-23 12:16:50 +0800100 if (skb->pkt_type != PACKET_HOST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 goto discard_it;
102
Eric Dumazet08e3bae2016-04-27 16:44:34 -0700103 __SCTP_INC_STATS(net, SCTP_MIB_INSCTPPACKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Marcelo Ricardo Leitner3acb50c2016-06-02 15:05:42 -0300105 /* If packet is too small to contain a single chunk, let's not
106 * waste time on it anymore.
107 */
108 if (skb->len < sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr) +
109 skb_transport_offset(skb))
Herbert Xu28cd7752006-10-29 23:46:42 -0800110 goto discard_it;
111
Marcelo Ricardo Leitner4c2f2452016-08-18 14:58:35 -0300112 /* If the packet is fragmented and we need to do crc checking,
113 * it's better to just linearize it otherwise crc computing
114 * takes longer.
115 */
Daniel Axtens1dd27cd2018-03-09 14:06:09 +1100116 if ((!is_gso && skb_linearize(skb)) ||
Marcelo Ricardo Leitner4c2f2452016-08-18 14:58:35 -0300117 !pskb_may_pull(skb, sizeof(struct sctphdr)))
Marcelo Ricardo Leitner3acb50c2016-06-02 15:05:42 -0300118 goto discard_it;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Marcelo Ricardo Leitner3acb50c2016-06-02 15:05:42 -0300120 /* Pull up the IP header. */
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -0700121 __skb_pull(skb, skb_transport_offset(skb));
Tom Herbert202863f2014-08-27 21:27:06 -0700122
123 skb->csum_valid = 0; /* Previous value not applicable */
124 if (skb_csum_unnecessary(skb))
125 __skb_decr_checksum_unnecessary(skb);
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300126 else if (!sctp_checksum_disable &&
Daniel Axtens1dd27cd2018-03-09 14:06:09 +1100127 !is_gso &&
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300128 sctp_rcv_checksum(net, skb) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 goto discard_it;
Tom Herbert202863f2014-08-27 21:27:06 -0700130 skb->csum_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Marcelo Ricardo Leitner3acb50c2016-06-02 15:05:42 -0300132 __skb_pull(skb, sizeof(struct sctphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700134 family = ipver2af(ip_hdr(skb)->version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 af = sctp_get_af_specific(family);
136 if (unlikely(!af))
137 goto discard_it;
Marcelo Ricardo Leitnere7487c82016-07-13 15:08:58 -0300138 SCTP_INPUT_CB(skb)->af = af;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 /* Initialize local addresses for lookups. */
141 af->from_skb(&src, skb, 1);
142 af->from_skb(&dest, skb, 0);
143
144 /* If the packet is to or from a non-unicast address,
145 * silently discard the packet.
146 *
147 * This is not clearly defined in the RFC except in section
148 * 8.4 - OOTB handling. However, based on the book "Stream Control
149 * Transmission Protocol" 2.1, "It is important to note that the
150 * IP address of an SCTP transport address must be a routable
151 * unicast address. In other words, IP multicast addresses and
152 * IP broadcast addresses cannot be used in an SCTP transport
153 * address."
154 */
Vlad Yasevich5636bef2006-06-17 22:55:35 -0700155 if (!af->addr_valid(&src, NULL, skb) ||
156 !af->addr_valid(&dest, NULL, skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 goto discard_it;
158
Eric W. Biederman4110cc22012-08-06 08:41:13 +0000159 asoc = __sctp_rcv_lookup(net, skb, &src, &dest, &transport);
Al Viro1c7d1fc2006-11-20 17:08:09 -0800160
Neil Horman0fd9a652005-06-13 15:11:24 -0700161 if (!asoc)
Xin Long532ae2f2018-11-12 18:27:15 +0800162 ep = __sctp_rcv_lookup_endpoint(net, skb, &dest, &src);
Neil Horman0fd9a652005-06-13 15:11:24 -0700163
164 /* Retrieve the common input handling substructure. */
165 rcvr = asoc ? &asoc->base : &ep->base;
166 sk = rcvr->sk;
167
168 /*
169 * If a frame arrives on an interface and the receiving socket is
170 * bound to another interface, via SO_BINDTODEVICE, treat it as OOTB
171 */
wangweidong8d726512013-12-23 12:16:53 +0800172 if (sk->sk_bound_dev_if && (sk->sk_bound_dev_if != af->skb_iif(skb))) {
Xin Longdae399d2016-10-31 20:32:33 +0800173 if (transport) {
174 sctp_transport_put(transport);
Neil Horman0fd9a652005-06-13 15:11:24 -0700175 asoc = NULL;
Xin Longdae399d2016-10-31 20:32:33 +0800176 transport = NULL;
Neil Horman0fd9a652005-06-13 15:11:24 -0700177 } else {
178 sctp_endpoint_put(ep);
179 ep = NULL;
180 }
Eric W. Biederman2ce95502012-08-06 08:43:06 +0000181 sk = net->sctp.ctl_sock;
Neil Horman0fd9a652005-06-13 15:11:24 -0700182 ep = sctp_sk(sk)->ep;
183 sctp_endpoint_hold(ep);
Neil Horman0fd9a652005-06-13 15:11:24 -0700184 rcvr = &ep->base;
185 }
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 /*
188 * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
189 * An SCTP packet is called an "out of the blue" (OOTB)
190 * packet if it is correctly formed, i.e., passed the
191 * receiver's checksum check, but the receiver is not
192 * able to identify the association to which this
193 * packet belongs.
194 */
195 if (!asoc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (sctp_rcv_ootb(skb)) {
Eric Dumazet08e3bae2016-04-27 16:44:34 -0700197 __SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 goto discard_release;
199 }
200 }
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (!xfrm_policy_check(sk, XFRM_POLICY_IN, skb, family))
203 goto discard_release;
Florian Westphal895b5c92019-09-29 20:54:03 +0200204 nf_reset_ct(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Dmitry Mishinfda9ef52006-08-31 15:28:39 -0700206 if (sk_filter(sk, skb))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900207 goto discard_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 /* Create an SCTP packet structure. */
Marcelo Ricardo Leitnercea87682016-03-10 18:33:07 -0300210 chunk = sctp_chunkify(skb, asoc, sk, GFP_ATOMIC);
Herbert Xu2babf9d2006-03-25 01:25:29 -0800211 if (!chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 goto discard_release;
David S. Miller79af02c2005-07-08 21:47:49 -0700213 SCTP_INPUT_CB(skb)->chunk = chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 /* Remember what endpoint is to handle this packet. */
216 chunk->rcvr = rcvr;
217
218 /* Remember the SCTP header. */
Marcelo Ricardo Leitner3acb50c2016-06-02 15:05:42 -0300219 chunk->sctp_hdr = sctp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 /* Set the source and destination addresses of the incoming chunk. */
Al Virod55c41b2006-11-20 17:09:40 -0800222 sctp_init_addrs(chunk, &src, &dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 /* Remember where we came from. */
225 chunk->transport = transport;
226
227 /* Acquire access to the sock lock. Note: We are safe from other
228 * bottom halves on this lock, but a user may be in the lock too,
229 * so check if it is busy.
230 */
wangweidong5bc1d1b2014-01-21 15:44:12 +0800231 bh_lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Vlad Yasevichae53b5b2009-01-22 14:53:23 -0800233 if (sk != rcvr->sk) {
234 /* Our cached sk is different from the rcvr->sk. This is
235 * because migrate()/accept() may have moved the association
236 * to a new socket and released all the sockets. So now we
237 * are holding a lock on the old socket while the user may
238 * be doing something with the new socket. Switch our veiw
239 * of the current sk.
240 */
wangweidong5bc1d1b2014-01-21 15:44:12 +0800241 bh_unlock_sock(sk);
Vlad Yasevichae53b5b2009-01-22 14:53:23 -0800242 sk = rcvr->sk;
wangweidong5bc1d1b2014-01-21 15:44:12 +0800243 bh_lock_sock(sk);
Vlad Yasevichae53b5b2009-01-22 14:53:23 -0800244 }
245
Xin Long819be812019-10-08 19:09:23 +0800246 if (sock_owned_by_user(sk) || !sctp_newsk_ready(sk)) {
Zhu Yi50b1a782010-03-04 18:01:44 +0000247 if (sctp_add_backlog(sk, skb)) {
wangweidong5bc1d1b2014-01-21 15:44:12 +0800248 bh_unlock_sock(sk);
Zhu Yi50b1a782010-03-04 18:01:44 +0000249 sctp_chunk_free(chunk);
250 skb = NULL; /* sctp_chunk_free already freed the skb */
251 goto discard_release;
252 }
Eric Dumazet08e3bae2016-04-27 16:44:34 -0700253 __SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_BACKLOG);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -0700254 } else {
Eric Dumazet08e3bae2016-04-27 16:44:34 -0700255 __SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_SOFTIRQ);
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700256 sctp_inq_push(&chunk->rcvr->inqueue, chunk);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -0700257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
wangweidong5bc1d1b2014-01-21 15:44:12 +0800259 bh_unlock_sock(sk);
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700260
261 /* Release the asoc/ep ref we took in the lookup calls. */
Xin Longdae399d2016-10-31 20:32:33 +0800262 if (transport)
263 sctp_transport_put(transport);
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700264 else
265 sctp_endpoint_put(ep);
Sridhar Samudrala7a48f922006-01-17 11:51:28 -0800266
Herbert Xu2babf9d2006-03-25 01:25:29 -0800267 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269discard_it:
Eric Dumazet08e3bae2016-04-27 16:44:34 -0700270 __SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 kfree_skb(skb);
Herbert Xu2babf9d2006-03-25 01:25:29 -0800272 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274discard_release:
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700275 /* Release the asoc/ep ref we took in the lookup calls. */
Xin Longdae399d2016-10-31 20:32:33 +0800276 if (transport)
277 sctp_transport_put(transport);
Neil Horman0fd9a652005-06-13 15:11:24 -0700278 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 sctp_endpoint_put(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 goto discard_it;
282}
283
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700284/* Process the backlog queue of the socket. Every skb on
285 * the backlog holds a ref on an association or endpoint.
286 * We hold this ref throughout the state machine to make
287 * sure that the structure we need is still around.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 */
289int sctp_backlog_rcv(struct sock *sk, struct sk_buff *skb)
290{
David S. Miller79af02c2005-07-08 21:47:49 -0700291 struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900292 struct sctp_inq *inqueue = &chunk->rcvr->inqueue;
Xin Longdae399d2016-10-31 20:32:33 +0800293 struct sctp_transport *t = chunk->transport;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900294 struct sctp_ep_common *rcvr = NULL;
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700295 int backloged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900297 rcvr = chunk->rcvr;
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800298
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700299 /* If the rcvr is dead then the association or endpoint
300 * has been deleted and we can safely drop the chunk
301 * and refs that we are holding.
302 */
303 if (rcvr->dead) {
304 sctp_chunk_free(chunk);
305 goto done;
306 }
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800307
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700308 if (unlikely(rcvr->sk != sk)) {
309 /* In this case, the association moved from one socket to
310 * another. We are currently sitting on the backlog of the
311 * old socket, so we need to move.
312 * However, since we are here in the process context we
313 * need to take make sure that the user doesn't own
314 * the new socket when we process the packet.
315 * If the new socket is user-owned, queue the chunk to the
316 * backlog of the new socket without dropping any refs.
317 * Otherwise, we can safely push the chunk on the inqueue.
318 */
Sridhar Samudrala7a48f922006-01-17 11:51:28 -0800319
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700320 sk = rcvr->sk;
Marcelo Ricardo Leitnereefc1b12016-07-23 00:32:48 -0300321 local_bh_disable();
wangweidong5bc1d1b2014-01-21 15:44:12 +0800322 bh_lock_sock(sk);
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700323
Xin Long819be812019-10-08 19:09:23 +0800324 if (sock_owned_by_user(sk) || !sctp_newsk_ready(sk)) {
Eric Dumazet82657922019-10-09 15:21:13 -0700325 if (sk_add_backlog(sk, skb, READ_ONCE(sk->sk_rcvbuf)))
Zhu Yi50b1a782010-03-04 18:01:44 +0000326 sctp_chunk_free(chunk);
327 else
328 backloged = 1;
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700329 } else
330 sctp_inq_push(inqueue, chunk);
331
wangweidong5bc1d1b2014-01-21 15:44:12 +0800332 bh_unlock_sock(sk);
Marcelo Ricardo Leitnereefc1b12016-07-23 00:32:48 -0300333 local_bh_enable();
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700334
335 /* If the chunk was backloged again, don't drop refs */
336 if (backloged)
337 return 0;
338 } else {
Xin Long819be812019-10-08 19:09:23 +0800339 if (!sctp_newsk_ready(sk)) {
Eric Dumazet82657922019-10-09 15:21:13 -0700340 if (!sk_add_backlog(sk, skb, READ_ONCE(sk->sk_rcvbuf)))
Xin Long819be812019-10-08 19:09:23 +0800341 return 0;
342 sctp_chunk_free(chunk);
343 } else {
344 sctp_inq_push(inqueue, chunk);
345 }
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700346 }
347
348done:
349 /* Release the refs we took in sctp_add_backlog */
350 if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
Xin Longdae399d2016-10-31 20:32:33 +0800351 sctp_transport_put(t);
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700352 else if (SCTP_EP_TYPE_SOCKET == rcvr->type)
353 sctp_endpoint_put(sctp_ep(rcvr));
354 else
355 BUG();
356
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900357 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
Zhu Yi50b1a782010-03-04 18:01:44 +0000360static int sctp_add_backlog(struct sock *sk, struct sk_buff *skb)
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800361{
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700362 struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
Xin Longdae399d2016-10-31 20:32:33 +0800363 struct sctp_transport *t = chunk->transport;
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700364 struct sctp_ep_common *rcvr = chunk->rcvr;
Zhu Yi50b1a782010-03-04 18:01:44 +0000365 int ret;
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800366
Eric Dumazet82657922019-10-09 15:21:13 -0700367 ret = sk_add_backlog(sk, skb, READ_ONCE(sk->sk_rcvbuf));
Zhu Yi50b1a782010-03-04 18:01:44 +0000368 if (!ret) {
369 /* Hold the assoc/ep while hanging on the backlog queue.
370 * This way, we know structures we need will not disappear
371 * from us
372 */
373 if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
Xin Longdae399d2016-10-31 20:32:33 +0800374 sctp_transport_hold(t);
Zhu Yi50b1a782010-03-04 18:01:44 +0000375 else if (SCTP_EP_TYPE_SOCKET == rcvr->type)
376 sctp_endpoint_hold(sctp_ep(rcvr));
377 else
378 BUG();
379 }
380 return ret;
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800381
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800382}
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384/* Handle icmp frag needed error. */
385void sctp_icmp_frag_needed(struct sock *sk, struct sctp_association *asoc,
386 struct sctp_transport *t, __u32 pmtu)
387{
Xin Long83696402021-06-22 14:04:55 -0400388 if (!t ||
389 (t->pathmtu <= pmtu &&
390 t->pl.probe_size + sctp_transport_pl_hlen(t) <= pmtu))
Frank Filz52ccb8e2005-12-22 11:36:46 -0800391 return;
392
Vlad Yasevich8a479492007-06-07 14:21:05 -0400393 if (sock_owned_by_user(sk)) {
Xin Longd8053972018-10-15 19:58:29 +0800394 atomic_set(&t->mtu_info, pmtu);
Vlad Yasevich8a479492007-06-07 14:21:05 -0400395 asoc->pmtu_pending = 1;
396 t->pmtu_pending = 1;
397 return;
398 }
399
Marcelo Ricardo Leitnercc35c3d2018-01-05 11:17:17 -0200400 if (!(t->param_flags & SPP_PMTUD_ENABLE))
401 /* We can't allow retransmitting in such case, as the
402 * retransmission would be sized just as before, and thus we
403 * would get another icmp, and retransmit again.
404 */
405 return;
Frank Filz52ccb8e2005-12-22 11:36:46 -0800406
Marcelo Ricardo Leitnerb6c57342018-01-05 11:17:18 -0200407 /* Update transports view of the MTU. Return if no update was needed.
408 * If an update wasn't needed/possible, it also doesn't make sense to
409 * try to retransmit now.
410 */
411 if (!sctp_transport_update_pmtu(t, pmtu))
412 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Marcelo Ricardo Leitnercc35c3d2018-01-05 11:17:17 -0200414 /* Update association pmtu. */
415 sctp_assoc_sync_pmtu(asoc);
416
417 /* Retransmit with the new pmtu setting. */
Frank Filz52ccb8e2005-12-22 11:36:46 -0800418 sctp_retransmit(&asoc->outqueue, t, SCTP_RTXR_PMTUD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
David S. Millerec18d9a2012-07-12 00:25:15 -0700421void sctp_icmp_redirect(struct sock *sk, struct sctp_transport *t,
422 struct sk_buff *skb)
David S. Miller55be7a92012-07-11 21:27:49 -0700423{
424 struct dst_entry *dst;
425
Xin Long1cc276c2017-10-18 21:37:49 +0800426 if (sock_owned_by_user(sk) || !t)
David S. Miller55be7a92012-07-11 21:27:49 -0700427 return;
428 dst = sctp_transport_dst_check(t);
David S. Miller1ed5c482012-07-12 00:41:25 -0700429 if (dst)
David S. Miller6700c272012-07-17 03:29:28 -0700430 dst->ops->redirect(dst, sk, skb);
David S. Miller55be7a92012-07-11 21:27:49 -0700431}
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433/*
434 * SCTP Implementer's Guide, 2.37 ICMP handling procedures
435 *
436 * ICMP8) If the ICMP code is a "Unrecognized next header type encountered"
437 * or a "Protocol Unreachable" treat this message as an abort
438 * with the T bit set.
439 *
440 * This function sends an event to the state machine, which will abort the
441 * association.
442 *
443 */
444void sctp_icmp_proto_unreachable(struct sock *sk,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900445 struct sctp_association *asoc,
446 struct sctp_transport *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Vlad Yasevich50b5d6a2010-05-06 00:56:07 -0700448 if (sock_owned_by_user(sk)) {
449 if (timer_pending(&t->proto_unreach_timer))
450 return;
451 else {
452 if (!mod_timer(&t->proto_unreach_timer,
453 jiffies + (HZ/20)))
Xin Long057a10f2020-11-14 13:22:53 +0800454 sctp_transport_hold(t);
Vlad Yasevich50b5d6a2010-05-06 00:56:07 -0700455 }
Vlad Yasevich50b5d6a2010-05-06 00:56:07 -0700456 } else {
Eric W. Biederman55e26eb2012-08-07 07:25:24 +0000457 struct net *net = sock_net(sk);
458
Daniel Borkmannbb333812013-06-28 19:49:40 +0200459 pr_debug("%s: unrecognized next header type "
460 "encountered!\n", __func__);
461
Ying Xue25cc4ae2013-02-03 20:32:57 +0000462 if (del_timer(&t->proto_unreach_timer))
Xin Long057a10f2020-11-14 13:22:53 +0800463 sctp_transport_put(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Eric W. Biederman55e26eb2012-08-07 07:25:24 +0000465 sctp_do_sm(net, SCTP_EVENT_T_OTHER,
Vlad Yasevich50b5d6a2010-05-06 00:56:07 -0700466 SCTP_ST_OTHER(SCTP_EVENT_ICMP_PROTO_UNREACH),
467 asoc->state, asoc->ep, asoc, t,
468 GFP_ATOMIC);
469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471
472/* Common lookup code for icmp/icmpv6 error handler. */
Eric W. Biederman4110cc22012-08-06 08:41:13 +0000473struct sock *sctp_err_lookup(struct net *net, int family, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 struct sctphdr *sctphdr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 struct sctp_association **app,
476 struct sctp_transport **tpp)
477{
Davide Caratti804ec7e2017-05-25 19:14:56 +0200478 struct sctp_init_chunk *chunkhdr, _chunkhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 union sctp_addr saddr;
480 union sctp_addr daddr;
481 struct sctp_af *af;
482 struct sock *sk = NULL;
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700483 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 struct sctp_transport *transport = NULL;
Wei Yongjun7115e632008-06-19 16:07:48 -0700485 __u32 vtag = ntohl(sctphdr->vtag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700487 *app = NULL; *tpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 af = sctp_get_af_specific(family);
490 if (unlikely(!af)) {
491 return NULL;
492 }
493
494 /* Initialize local addresses for lookups. */
495 af->from_skb(&saddr, skb, 1);
496 af->from_skb(&daddr, skb, 0);
497
498 /* Look for an association that matches the incoming ICMP error
499 * packet.
500 */
Eric W. Biederman4110cc22012-08-06 08:41:13 +0000501 asoc = __sctp_lookup_association(net, &saddr, &daddr, &transport);
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700502 if (!asoc)
503 return NULL;
504
505 sk = asoc->base.sk;
506
Wei Yongjun7115e632008-06-19 16:07:48 -0700507 /* RFC 4960, Appendix C. ICMP Handling
508 *
509 * ICMP6) An implementation MUST validate that the Verification Tag
510 * contained in the ICMP message matches the Verification Tag of
511 * the peer. If the Verification Tag is not 0 and does NOT
512 * match, discard the ICMP message. If it is 0 and the ICMP
513 * message contains enough bytes to verify that the chunk type is
514 * an INIT chunk and that the Initiate Tag matches the tag of the
515 * peer, continue with ICMP7. If the ICMP message is too short
516 * or the chunk type or the Initiate Tag does not match, silently
517 * discard the packet.
518 */
519 if (vtag == 0) {
Davide Caratti804ec7e2017-05-25 19:14:56 +0200520 /* chunk header + first 4 octects of init header */
521 chunkhdr = skb_header_pointer(skb, skb_transport_offset(skb) +
522 sizeof(struct sctphdr),
523 sizeof(struct sctp_chunkhdr) +
524 sizeof(__be32), &_chunkhdr);
525 if (!chunkhdr ||
Wei Yongjun7115e632008-06-19 16:07:48 -0700526 chunkhdr->chunk_hdr.type != SCTP_CID_INIT ||
Davide Caratti804ec7e2017-05-25 19:14:56 +0200527 ntohl(chunkhdr->init_hdr.init_tag) != asoc->c.my_vtag)
Wei Yongjun7115e632008-06-19 16:07:48 -0700528 goto out;
Davide Caratti804ec7e2017-05-25 19:14:56 +0200529
Wei Yongjun7115e632008-06-19 16:07:48 -0700530 } else if (vtag != asoc->c.peer_vtag) {
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700531 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
533
wangweidong5bc1d1b2014-01-21 15:44:12 +0800534 bh_lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 /* If too many ICMPs get dropped on busy
537 * servers this needs to be solved differently.
538 */
539 if (sock_owned_by_user(sk))
Eric Dumazet02a1d6e2016-04-27 16:44:39 -0700540 __NET_INC_STATS(net, LINUX_MIB_LOCKDROPPEDICMPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 *app = asoc;
543 *tpp = transport;
544 return sk;
545
546out:
Xin Longdae399d2016-10-31 20:32:33 +0800547 sctp_transport_put(transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 return NULL;
549}
550
551/* Common cleanup code for icmp/icmpv6 error handler. */
Xin Longdae399d2016-10-31 20:32:33 +0800552void sctp_err_finish(struct sock *sk, struct sctp_transport *t)
Jules Irenge887cf3d2020-02-23 23:16:45 +0000553 __releases(&((__sk)->sk_lock.slock))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
wangweidong5bc1d1b2014-01-21 15:44:12 +0800555 bh_unlock_sock(sk);
Xin Longdae399d2016-10-31 20:32:33 +0800556 sctp_transport_put(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
558
Xin Longd8306072021-06-22 14:04:59 -0400559static void sctp_v4_err_handle(struct sctp_transport *t, struct sk_buff *skb,
560 __u8 type, __u8 code, __u32 info)
561{
562 struct sctp_association *asoc = t->asoc;
563 struct sock *sk = asoc->base.sk;
564 int err = 0;
565
566 switch (type) {
567 case ICMP_PARAMETERPROB:
568 err = EPROTO;
569 break;
570 case ICMP_DEST_UNREACH:
571 if (code > NR_ICMP_UNREACH)
572 return;
573 if (code == ICMP_FRAG_NEEDED) {
574 sctp_icmp_frag_needed(sk, asoc, t, SCTP_TRUNC4(info));
575 return;
576 }
577 if (code == ICMP_PROT_UNREACH) {
578 sctp_icmp_proto_unreachable(sk, asoc, t);
579 return;
580 }
581 err = icmp_err_convert[code].errno;
582 break;
583 case ICMP_TIME_EXCEEDED:
584 if (code == ICMP_EXC_FRAGTIME)
585 return;
586
587 err = EHOSTUNREACH;
588 break;
589 case ICMP_REDIRECT:
590 sctp_icmp_redirect(sk, t, skb);
Linus Torvaldsdbe69e42021-06-30 15:51:09 -0700591 return;
Xin Longd8306072021-06-22 14:04:59 -0400592 default:
593 return;
594 }
595 if (!sock_owned_by_user(sk) && inet_sk(sk)->recverr) {
596 sk->sk_err = err;
Alexander Aringe3ae2362021-06-27 18:48:21 -0400597 sk_error_report(sk);
Xin Longd8306072021-06-22 14:04:59 -0400598 } else { /* Only an error on timeout */
599 sk->sk_err_soft = err;
600 }
601}
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603/*
604 * This routine is called by the ICMP module when it gets some
605 * sort of error condition. If err < 0 then the socket should
606 * be closed and the error returned to the user. If err > 0
607 * it's just the icmp type << 8 | icmp code. After adjustment
608 * header points to the first 8 bytes of the sctp header. We need
609 * to find the appropriate port.
610 *
611 * The locking strategy used here is very "optimistic". When
612 * someone else accesses the socket the ICMP is just dropped
613 * and for some paths there is no check at all.
614 * A more general error queue to queue errors for later handling
615 * is probably better.
616 *
617 */
Stefano Brivio32bbd872018-11-08 12:19:21 +0100618int sctp_v4_err(struct sk_buff *skb, __u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000620 const struct iphdr *iph = (const struct iphdr *)skb->data;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300621 const int type = icmp_hdr(skb)->type;
622 const int code = icmp_hdr(skb)->code;
Eric W. Biederman4110cc22012-08-06 08:41:13 +0000623 struct net *net = dev_net(skb->dev);
Xin Longd8306072021-06-22 14:04:59 -0400624 struct sctp_transport *transport;
625 struct sctp_association *asoc;
626 __u16 saveip, savesctp;
627 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 /* Fix up skb to look at the embedded net header. */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700630 saveip = skb->network_header;
631 savesctp = skb->transport_header;
Arnaldo Carvalho de Melo31c77112007-03-10 19:04:55 -0300632 skb_reset_network_header(skb);
Xin Longd8306072021-06-22 14:04:59 -0400633 skb_set_transport_header(skb, iph->ihl * 4);
Eric W. Biederman4110cc22012-08-06 08:41:13 +0000634 sk = sctp_err_lookup(net, AF_INET, skb, sctp_hdr(skb), &asoc, &transport);
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700635 /* Put back, the original values. */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700636 skb->network_header = saveip;
637 skb->transport_header = savesctp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 if (!sk) {
Eric Dumazet5d3848b2016-04-27 16:44:29 -0700639 __ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
Stefano Brivio32bbd872018-11-08 12:19:21 +0100640 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Xin Longd8306072021-06-22 14:04:59 -0400643 sctp_v4_err_handle(transport, skb, type, code, info);
Xin Longdae399d2016-10-31 20:32:33 +0800644 sctp_err_finish(sk, transport);
Xin Longd8306072021-06-22 14:04:59 -0400645
Stefano Brivio32bbd872018-11-08 12:19:21 +0100646 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
Xin Long9e47df02021-06-22 14:05:00 -0400649int sctp_udp_v4_err(struct sock *sk, struct sk_buff *skb)
650{
651 struct net *net = dev_net(skb->dev);
652 struct sctp_association *asoc;
653 struct sctp_transport *t;
654 struct icmphdr *hdr;
655 __u32 info = 0;
656
657 skb->transport_header += sizeof(struct udphdr);
658 sk = sctp_err_lookup(net, AF_INET, skb, sctp_hdr(skb), &asoc, &t);
659 if (!sk) {
660 __ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
661 return -ENOENT;
662 }
663
664 skb->transport_header -= sizeof(struct udphdr);
665 hdr = (struct icmphdr *)(skb_network_header(skb) - sizeof(struct icmphdr));
666 if (hdr->type == ICMP_REDIRECT) {
667 /* can't be handled without outer iphdr known, leave it to udp_err */
668 sctp_err_finish(sk, t);
669 return 0;
670 }
671 if (hdr->type == ICMP_DEST_UNREACH && hdr->code == ICMP_FRAG_NEEDED)
672 info = ntohs(hdr->un.frag.mtu);
673 sctp_v4_err_handle(t, skb, hdr->type, hdr->code, info);
674
675 sctp_err_finish(sk, t);
676 return 1;
677}
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679/*
680 * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
681 *
682 * This function scans all the chunks in the OOTB packet to determine if
683 * the packet should be discarded right away. If a response might be needed
684 * for this packet, or, if further processing is possible, the packet will
685 * be queued to a proper inqueue for the next phase of handling.
686 *
687 * Output:
688 * Return 0 - If further processing is needed.
689 * Return 1 - If the packet can be discarded right away.
690 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200691static int sctp_rcv_ootb(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
Xin Long922dbc52017-06-30 11:52:13 +0800693 struct sctp_chunkhdr *ch, _ch;
Marcelo Ricardo Leitner3acb50c2016-06-02 15:05:42 -0300694 int ch_end, offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 /* Scan through all the chunks in the packet. */
Tsutomu Fujiia7d1f1b2006-01-17 11:57:09 -0800697 do {
Marcelo Ricardo Leitner3acb50c2016-06-02 15:05:42 -0300698 /* Make sure we have at least the header there */
Xin Long922dbc52017-06-30 11:52:13 +0800699 if (offset + sizeof(_ch) > skb->len)
Marcelo Ricardo Leitner3acb50c2016-06-02 15:05:42 -0300700 break;
701
702 ch = skb_header_pointer(skb, offset, sizeof(*ch), &_ch);
703
Tsutomu Fujiia7d1f1b2006-01-17 11:57:09 -0800704 /* Break out if chunk length is less then minimal. */
Xin Longf7e745f2021-09-23 00:05:04 -0400705 if (!ch || ntohs(ch->length) < sizeof(_ch))
Tsutomu Fujiia7d1f1b2006-01-17 11:57:09 -0800706 break;
707
Marcelo Ricardo Leitnere2f036a2016-09-21 08:45:55 -0300708 ch_end = offset + SCTP_PAD4(ntohs(ch->length));
Marcelo Ricardo Leitner3acb50c2016-06-02 15:05:42 -0300709 if (ch_end > skb->len)
Tsutomu Fujiia7d1f1b2006-01-17 11:57:09 -0800710 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 /* RFC 8.4, 2) If the OOTB packet contains an ABORT chunk, the
713 * receiver MUST silently discard the OOTB packet and take no
714 * further action.
715 */
716 if (SCTP_CID_ABORT == ch->type)
717 goto discard;
718
719 /* RFC 8.4, 6) If the packet contains a SHUTDOWN COMPLETE
720 * chunk, the receiver should silently discard the packet
721 * and take no further action.
722 */
723 if (SCTP_CID_SHUTDOWN_COMPLETE == ch->type)
724 goto discard;
725
Vlad Yasevich3c77f962007-09-17 15:14:28 -0400726 /* RFC 4460, 2.11.2
727 * This will discard packets with INIT chunk bundled as
728 * subsequent chunks in the packet. When INIT is first,
729 * the normal INIT processing will discard the chunk.
730 */
731 if (SCTP_CID_INIT == ch->type && (void *)ch != skb->data)
732 goto discard;
733
Marcelo Ricardo Leitner3acb50c2016-06-02 15:05:42 -0300734 offset = ch_end;
735 } while (ch_end < skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 return 0;
738
739discard:
740 return 1;
741}
742
743/* Insert endpoint into the hash table. */
Xin Long76c6d982018-11-12 18:27:16 +0800744static int __sctp_hash_endpoint(struct sctp_endpoint *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
Xin Long76c6d982018-11-12 18:27:16 +0800746 struct sock *sk = ep->base.sk;
747 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 struct sctp_hashbucket *head;
749
Xin Long3d3b2f52021-12-21 16:40:30 -0500750 ep->hashent = sctp_ep_hashfn(net, ep->base.bind_addr.port);
751 head = &sctp_ep_hashtable[ep->hashent];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Xin Long76c6d982018-11-12 18:27:16 +0800753 if (sk->sk_reuseport) {
754 bool any = sctp_is_ep_boundall(sk);
Xin Long3d3b2f52021-12-21 16:40:30 -0500755 struct sctp_endpoint *ep2;
Xin Long76c6d982018-11-12 18:27:16 +0800756 struct list_head *list;
757 int cnt = 0, err = 1;
758
759 list_for_each(list, &ep->base.bind_addr.address_list)
760 cnt++;
761
Xin Long3d3b2f52021-12-21 16:40:30 -0500762 sctp_for_each_hentry(ep2, &head->chain) {
763 struct sock *sk2 = ep2->base.sk;
Xin Long76c6d982018-11-12 18:27:16 +0800764
765 if (!net_eq(sock_net(sk2), net) || sk2 == sk ||
766 !uid_eq(sock_i_uid(sk2), sock_i_uid(sk)) ||
767 !sk2->sk_reuseport)
768 continue;
769
770 err = sctp_bind_addrs_check(sctp_sk(sk2),
771 sctp_sk(sk), cnt);
772 if (!err) {
773 err = reuseport_add_sock(sk, sk2, any);
774 if (err)
775 return err;
776 break;
777 } else if (err < 0) {
778 return err;
779 }
780 }
781
782 if (err) {
783 err = reuseport_alloc(sk, any);
784 if (err)
785 return err;
786 }
787 }
788
wangweidong387602d2014-01-21 15:44:09 +0800789 write_lock(&head->lock);
Xin Long3d3b2f52021-12-21 16:40:30 -0500790 hlist_add_head(&ep->node, &head->chain);
wangweidong387602d2014-01-21 15:44:09 +0800791 write_unlock(&head->lock);
Xin Long76c6d982018-11-12 18:27:16 +0800792 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793}
794
795/* Add an endpoint to the hash. Local BH-safe. */
Xin Long76c6d982018-11-12 18:27:16 +0800796int sctp_hash_endpoint(struct sctp_endpoint *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Xin Long76c6d982018-11-12 18:27:16 +0800798 int err;
799
wangweidong79b91132014-01-21 15:44:07 +0800800 local_bh_disable();
Xin Long76c6d982018-11-12 18:27:16 +0800801 err = __sctp_hash_endpoint(ep);
wangweidong79b91132014-01-21 15:44:07 +0800802 local_bh_enable();
Xin Long76c6d982018-11-12 18:27:16 +0800803
804 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805}
806
807/* Remove endpoint from the hash table. */
808static void __sctp_unhash_endpoint(struct sctp_endpoint *ep)
809{
Xin Long76c6d982018-11-12 18:27:16 +0800810 struct sock *sk = ep->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 struct sctp_hashbucket *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Xin Long3d3b2f52021-12-21 16:40:30 -0500813 ep->hashent = sctp_ep_hashfn(sock_net(sk), ep->base.bind_addr.port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Xin Long3d3b2f52021-12-21 16:40:30 -0500815 head = &sctp_ep_hashtable[ep->hashent];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Xin Long76c6d982018-11-12 18:27:16 +0800817 if (rcu_access_pointer(sk->sk_reuseport_cb))
818 reuseport_detach_sock(sk);
819
wangweidong387602d2014-01-21 15:44:09 +0800820 write_lock(&head->lock);
Xin Long3d3b2f52021-12-21 16:40:30 -0500821 hlist_del_init(&ep->node);
wangweidong387602d2014-01-21 15:44:09 +0800822 write_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
824
825/* Remove endpoint from the hash. Local BH-safe. */
826void sctp_unhash_endpoint(struct sctp_endpoint *ep)
827{
wangweidong79b91132014-01-21 15:44:07 +0800828 local_bh_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 __sctp_unhash_endpoint(ep);
wangweidong79b91132014-01-21 15:44:07 +0800830 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
832
Xin Long532ae2f2018-11-12 18:27:15 +0800833static inline __u32 sctp_hashfn(const struct net *net, __be16 lport,
834 const union sctp_addr *paddr, __u32 seed)
835{
836 __u32 addr;
837
838 if (paddr->sa.sa_family == AF_INET6)
839 addr = jhash(&paddr->v6.sin6_addr, 16, seed);
840 else
841 addr = (__force __u32)paddr->v4.sin_addr.s_addr;
842
843 return jhash_3words(addr, ((__force __u32)paddr->v4.sin_port) << 16 |
844 (__force __u32)lport, net_hash_mix(net), seed);
845}
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847/* Look up an endpoint. */
Xin Long532ae2f2018-11-12 18:27:15 +0800848static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(
849 struct net *net, struct sk_buff *skb,
850 const union sctp_addr *laddr,
851 const union sctp_addr *paddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
853 struct sctp_hashbucket *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 struct sctp_endpoint *ep;
Xin Long532ae2f2018-11-12 18:27:15 +0800855 struct sock *sk;
856 __be16 lport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 int hash;
858
Xin Long532ae2f2018-11-12 18:27:15 +0800859 lport = laddr->v4.sin_port;
860 hash = sctp_ep_hashfn(net, ntohs(lport));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 head = &sctp_ep_hashtable[hash];
862 read_lock(&head->lock);
Xin Long3d3b2f52021-12-21 16:40:30 -0500863 sctp_for_each_hentry(ep, &head->chain) {
Eric W. Biederman4cdadcb2012-08-06 08:40:21 +0000864 if (sctp_endpoint_is_match(ep, net, laddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 goto hit;
866 }
867
Eric W. Biederman2ce95502012-08-06 08:43:06 +0000868 ep = sctp_sk(net->sctp.ctl_sock)->ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870hit:
Xin Long532ae2f2018-11-12 18:27:15 +0800871 sk = ep->base.sk;
872 if (sk->sk_reuseport) {
873 __u32 phash = sctp_hashfn(net, lport, paddr, 0);
874
875 sk = reuseport_select_sock(sk, phash, skb,
876 sizeof(struct sctphdr));
877 if (sk)
878 ep = sctp_sk(sk)->ep;
879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 sctp_endpoint_hold(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 read_unlock(&head->lock);
882 return ep;
883}
884
Xin Longd6c02562015-12-30 23:50:46 +0800885/* rhashtable for transport */
886struct sctp_hash_cmp_arg {
Xin Long7fda7022016-11-15 23:23:11 +0800887 const union sctp_addr *paddr;
888 const struct net *net;
Xin Long8d325032017-10-28 19:43:55 +0800889 __be16 lport;
Xin Longd6c02562015-12-30 23:50:46 +0800890};
891
892static inline int sctp_hash_cmp(struct rhashtable_compare_arg *arg,
893 const void *ptr)
894{
Xin Long715f5552016-09-10 23:11:23 +0800895 struct sctp_transport *t = (struct sctp_transport *)ptr;
Xin Longd6c02562015-12-30 23:50:46 +0800896 const struct sctp_hash_cmp_arg *x = arg->key;
Xin Long715f5552016-09-10 23:11:23 +0800897 int err = 1;
Xin Longd6c02562015-12-30 23:50:46 +0800898
Xin Longd6c02562015-12-30 23:50:46 +0800899 if (!sctp_cmp_addr_exact(&t->ipaddr, x->paddr))
Xin Long715f5552016-09-10 23:11:23 +0800900 return err;
901 if (!sctp_transport_hold(t))
902 return err;
903
Xin Long31243462019-11-23 11:56:49 +0800904 if (!net_eq(t->asoc->base.net, x->net))
Xin Long715f5552016-09-10 23:11:23 +0800905 goto out;
Xin Long7fda7022016-11-15 23:23:11 +0800906 if (x->lport != htons(t->asoc->base.bind_addr.port))
907 goto out;
Xin Longd6c02562015-12-30 23:50:46 +0800908
Xin Long715f5552016-09-10 23:11:23 +0800909 err = 0;
910out:
911 sctp_transport_put(t);
912 return err;
Xin Longd6c02562015-12-30 23:50:46 +0800913}
914
Xin Long8d325032017-10-28 19:43:55 +0800915static inline __u32 sctp_hash_obj(const void *data, u32 len, u32 seed)
Xin Longd6c02562015-12-30 23:50:46 +0800916{
917 const struct sctp_transport *t = data;
Xin Longd6c02562015-12-30 23:50:46 +0800918
Xin Long31243462019-11-23 11:56:49 +0800919 return sctp_hashfn(t->asoc->base.net,
Xin Long532ae2f2018-11-12 18:27:15 +0800920 htons(t->asoc->base.bind_addr.port),
921 &t->ipaddr, seed);
Xin Longd6c02562015-12-30 23:50:46 +0800922}
923
Xin Long8d325032017-10-28 19:43:55 +0800924static inline __u32 sctp_hash_key(const void *data, u32 len, u32 seed)
Xin Longd6c02562015-12-30 23:50:46 +0800925{
926 const struct sctp_hash_cmp_arg *x = data;
Xin Longd6c02562015-12-30 23:50:46 +0800927
Xin Long532ae2f2018-11-12 18:27:15 +0800928 return sctp_hashfn(x->net, x->lport, x->paddr, seed);
Xin Longd6c02562015-12-30 23:50:46 +0800929}
930
931static const struct rhashtable_params sctp_hash_params = {
932 .head_offset = offsetof(struct sctp_transport, node),
933 .hashfn = sctp_hash_key,
934 .obj_hashfn = sctp_hash_obj,
935 .obj_cmpfn = sctp_hash_cmp,
936 .automatic_shrinking = true,
937};
938
939int sctp_transport_hashtable_init(void)
940{
Xin Long7fda7022016-11-15 23:23:11 +0800941 return rhltable_init(&sctp_transport_hashtable, &sctp_hash_params);
Xin Longd6c02562015-12-30 23:50:46 +0800942}
943
944void sctp_transport_hashtable_destroy(void)
945{
Xin Long7fda7022016-11-15 23:23:11 +0800946 rhltable_destroy(&sctp_transport_hashtable);
Xin Longd6c02562015-12-30 23:50:46 +0800947}
948
Xin Long7fda7022016-11-15 23:23:11 +0800949int sctp_hash_transport(struct sctp_transport *t)
Xin Longd6c02562015-12-30 23:50:46 +0800950{
Xin Longcd2b7082017-02-17 16:35:24 +0800951 struct sctp_transport *transport;
952 struct rhlist_head *tmp, *list;
Xin Longd6c02562015-12-30 23:50:46 +0800953 struct sctp_hash_cmp_arg arg;
Xin Long7fda7022016-11-15 23:23:11 +0800954 int err;
Xin Longd6c02562015-12-30 23:50:46 +0800955
Xin Longdd7445a2016-01-16 20:17:17 +0800956 if (t->asoc->temp)
Xin Long7fda7022016-11-15 23:23:11 +0800957 return 0;
Xin Longdd7445a2016-01-16 20:17:17 +0800958
Xin Long4e7696d2019-12-09 13:45:18 +0800959 arg.net = t->asoc->base.net;
Xin Long7fda7022016-11-15 23:23:11 +0800960 arg.paddr = &t->ipaddr;
961 arg.lport = htons(t->asoc->base.bind_addr.port);
Xin Longd6c02562015-12-30 23:50:46 +0800962
Xin Long5179b262017-02-28 12:41:29 +0800963 rcu_read_lock();
Xin Longcd2b7082017-02-17 16:35:24 +0800964 list = rhltable_lookup(&sctp_transport_hashtable, &arg,
965 sctp_hash_params);
966
967 rhl_for_each_entry_rcu(transport, tmp, list, node)
968 if (transport->asoc->ep == t->asoc->ep) {
Xin Long5179b262017-02-28 12:41:29 +0800969 rcu_read_unlock();
Xin Long27af86b2018-02-12 18:29:06 +0800970 return -EEXIST;
Xin Longcd2b7082017-02-17 16:35:24 +0800971 }
Xin Long5179b262017-02-28 12:41:29 +0800972 rcu_read_unlock();
Xin Longcd2b7082017-02-17 16:35:24 +0800973
Xin Long7fda7022016-11-15 23:23:11 +0800974 err = rhltable_insert_key(&sctp_transport_hashtable, &arg,
975 &t->node, sctp_hash_params);
976 if (err)
977 pr_err_once("insert transport fail, errno %d\n", err);
978
979 return err;
Xin Longd6c02562015-12-30 23:50:46 +0800980}
981
982void sctp_unhash_transport(struct sctp_transport *t)
983{
Xin Longdd7445a2016-01-16 20:17:17 +0800984 if (t->asoc->temp)
985 return;
986
Xin Long7fda7022016-11-15 23:23:11 +0800987 rhltable_remove(&sctp_transport_hashtable, &t->node,
988 sctp_hash_params);
Xin Longd6c02562015-12-30 23:50:46 +0800989}
990
Xin Long7fda7022016-11-15 23:23:11 +0800991/* return a transport with holding it */
Xin Longd6c02562015-12-30 23:50:46 +0800992struct sctp_transport *sctp_addrs_lookup_transport(
993 struct net *net,
994 const union sctp_addr *laddr,
995 const union sctp_addr *paddr)
996{
Xin Long7fda7022016-11-15 23:23:11 +0800997 struct rhlist_head *tmp, *list;
998 struct sctp_transport *t;
Xin Longd6c02562015-12-30 23:50:46 +0800999 struct sctp_hash_cmp_arg arg = {
Xin Longd6c02562015-12-30 23:50:46 +08001000 .paddr = paddr,
1001 .net = net,
Xin Long7fda7022016-11-15 23:23:11 +08001002 .lport = laddr->v4.sin_port,
Xin Longd6c02562015-12-30 23:50:46 +08001003 };
1004
Xin Long7fda7022016-11-15 23:23:11 +08001005 list = rhltable_lookup(&sctp_transport_hashtable, &arg,
1006 sctp_hash_params);
1007
1008 rhl_for_each_entry_rcu(t, tmp, list, node) {
1009 if (!sctp_transport_hold(t))
1010 continue;
1011
1012 if (sctp_bind_addr_match(&t->asoc->base.bind_addr,
1013 laddr, sctp_sk(t->asoc->base.sk)))
1014 return t;
1015 sctp_transport_put(t);
1016 }
1017
1018 return NULL;
Xin Longd6c02562015-12-30 23:50:46 +08001019}
1020
Xin Long7fda7022016-11-15 23:23:11 +08001021/* return a transport without holding it, as it's only used under sock lock */
Xin Longd6c02562015-12-30 23:50:46 +08001022struct sctp_transport *sctp_epaddr_lookup_transport(
1023 const struct sctp_endpoint *ep,
1024 const union sctp_addr *paddr)
1025{
Xin Long7fda7022016-11-15 23:23:11 +08001026 struct rhlist_head *tmp, *list;
1027 struct sctp_transport *t;
Xin Long65a51242016-01-14 13:49:34 +08001028 struct sctp_hash_cmp_arg arg = {
Xin Long65a51242016-01-14 13:49:34 +08001029 .paddr = paddr,
Xin Long4e7696d2019-12-09 13:45:18 +08001030 .net = ep->base.net,
Xin Long7fda7022016-11-15 23:23:11 +08001031 .lport = htons(ep->base.bind_addr.port),
Xin Long65a51242016-01-14 13:49:34 +08001032 };
Xin Longd6c02562015-12-30 23:50:46 +08001033
Xin Long7fda7022016-11-15 23:23:11 +08001034 list = rhltable_lookup(&sctp_transport_hashtable, &arg,
1035 sctp_hash_params);
1036
1037 rhl_for_each_entry_rcu(t, tmp, list, node)
1038 if (ep == t->asoc->ep)
1039 return t;
1040
1041 return NULL;
Xin Longd6c02562015-12-30 23:50:46 +08001042}
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044/* Look up an association. */
1045static struct sctp_association *__sctp_lookup_association(
Eric W. Biederman4110cc22012-08-06 08:41:13 +00001046 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 const union sctp_addr *local,
1048 const union sctp_addr *peer,
1049 struct sctp_transport **pt)
1050{
Xin Long4f008782015-12-30 23:50:47 +08001051 struct sctp_transport *t;
Xin Long1eed6772016-01-22 01:49:07 +08001052 struct sctp_association *asoc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Xin Long4f008782015-12-30 23:50:47 +08001054 t = sctp_addrs_lookup_transport(net, local, peer);
Xin Long7fda7022016-11-15 23:23:11 +08001055 if (!t)
Xin Long1eed6772016-01-22 01:49:07 +08001056 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Xin Long1eed6772016-01-22 01:49:07 +08001058 asoc = t->asoc;
Xin Long4f008782015-12-30 23:50:47 +08001059 *pt = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Xin Long1eed6772016-01-22 01:49:07 +08001061out:
Xin Long1eed6772016-01-22 01:49:07 +08001062 return asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
1064
Xin Long4f008782015-12-30 23:50:47 +08001065/* Look up an association. protected by RCU read lock */
Daniel Borkmanndda91922013-06-17 11:40:05 +02001066static
Eric W. Biederman4110cc22012-08-06 08:41:13 +00001067struct sctp_association *sctp_lookup_association(struct net *net,
1068 const union sctp_addr *laddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 const union sctp_addr *paddr,
Daniel Borkmanndda91922013-06-17 11:40:05 +02001070 struct sctp_transport **transportp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
1072 struct sctp_association *asoc;
1073
Xin Longf46c7012016-02-15 14:28:03 +08001074 rcu_read_lock();
Eric W. Biederman4110cc22012-08-06 08:41:13 +00001075 asoc = __sctp_lookup_association(net, laddr, paddr, transportp);
Xin Longf46c7012016-02-15 14:28:03 +08001076 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078 return asoc;
1079}
1080
1081/* Is there an association matching the given local and peer addresses? */
Xin Long53066532018-03-26 16:55:00 +08001082bool sctp_has_association(struct net *net,
1083 const union sctp_addr *laddr,
1084 const union sctp_addr *paddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 struct sctp_transport *transport;
1087
Xin Long53066532018-03-26 16:55:00 +08001088 if (sctp_lookup_association(net, laddr, paddr, &transport)) {
Xin Longdae399d2016-10-31 20:32:33 +08001089 sctp_transport_put(transport);
Xin Long53066532018-03-26 16:55:00 +08001090 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 }
1092
Xin Long53066532018-03-26 16:55:00 +08001093 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094}
1095
1096/*
1097 * SCTP Implementors Guide, 2.18 Handling of address
1098 * parameters within the INIT or INIT-ACK.
1099 *
1100 * D) When searching for a matching TCB upon reception of an INIT
1101 * or INIT-ACK chunk the receiver SHOULD use not only the
1102 * source address of the packet (containing the INIT or
1103 * INIT-ACK) but the receiver SHOULD also use all valid
1104 * address parameters contained within the chunk.
1105 *
1106 * 2.18.3 Solution description
1107 *
1108 * This new text clearly specifies to an implementor the need
1109 * to look within the INIT or INIT-ACK. Any implementation that
1110 * does not do this, may not be able to establish associations
1111 * in certain circumstances.
1112 *
1113 */
Eric W. Biederman4110cc22012-08-06 08:41:13 +00001114static struct sctp_association *__sctp_rcv_init_lookup(struct net *net,
1115 struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 const union sctp_addr *laddr, struct sctp_transport **transportp)
1117{
1118 struct sctp_association *asoc;
1119 union sctp_addr addr;
1120 union sctp_addr *paddr = &addr;
Arnaldo Carvalho de Melo2c0fd382007-03-13 13:59:32 -03001121 struct sctphdr *sh = sctp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 union sctp_params params;
Xin Long01a992b2017-06-30 11:52:22 +08001123 struct sctp_init_chunk *init;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 struct sctp_af *af;
1125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 /*
1127 * This code will NOT touch anything inside the chunk--it is
1128 * strictly READ-ONLY.
1129 *
1130 * RFC 2960 3 SCTP packet Format
1131 *
1132 * Multiple chunks can be bundled into one SCTP packet up to
1133 * the MTU size, except for the INIT, INIT ACK, and SHUTDOWN
1134 * COMPLETE chunks. These chunks MUST NOT be bundled with any
1135 * other chunk in a packet. See Section 6.10 for more details
1136 * on chunk bundling.
1137 */
1138
1139 /* Find the start of the TLVs and the end of the chunk. This is
1140 * the region we search for address parameters.
1141 */
Xin Long01a992b2017-06-30 11:52:22 +08001142 init = (struct sctp_init_chunk *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
1144 /* Walk the parameters looking for embedded addresses. */
1145 sctp_walk_params(params, init, init_hdr.params) {
1146
1147 /* Note: Ignoring hostname addresses. */
1148 af = sctp_get_af_specific(param_type2af(params.p->type));
1149 if (!af)
1150 continue;
1151
Marcelo Ricardo Leitner0c5dc072021-06-28 16:13:41 -03001152 if (!af->from_addr_param(paddr, params.addr, sh->source, 0))
1153 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Xin Long7c17fcc2016-10-31 20:32:32 +08001155 asoc = __sctp_lookup_association(net, laddr, paddr, transportp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 if (asoc)
1157 return asoc;
1158 }
1159
1160 return NULL;
1161}
1162
Vlad Yasevichdf218572007-12-20 14:10:38 -08001163/* ADD-IP, Section 5.2
1164 * When an endpoint receives an ASCONF Chunk from the remote peer
1165 * special procedures may be needed to identify the association the
1166 * ASCONF Chunk is associated with. To properly find the association
1167 * the following procedures SHOULD be followed:
1168 *
1169 * D2) If the association is not found, use the address found in the
1170 * Address Parameter TLV combined with the port number found in the
1171 * SCTP common header. If found proceed to rule D4.
1172 *
1173 * D2-ext) If more than one ASCONF Chunks are packed together, use the
1174 * address found in the ASCONF Address Parameter TLV of each of the
1175 * subsequent ASCONF Chunks. If found, proceed to rule D4.
1176 */
1177static struct sctp_association *__sctp_rcv_asconf_lookup(
Eric W. Biederman4110cc22012-08-06 08:41:13 +00001178 struct net *net,
Xin Long922dbc52017-06-30 11:52:13 +08001179 struct sctp_chunkhdr *ch,
Vlad Yasevichdf218572007-12-20 14:10:38 -08001180 const union sctp_addr *laddr,
Al Virobc92dd12008-03-17 22:47:32 -07001181 __be16 peer_port,
Vlad Yasevichdf218572007-12-20 14:10:38 -08001182 struct sctp_transport **transportp)
1183{
Xin Long68d75462017-08-03 15:42:20 +08001184 struct sctp_addip_chunk *asconf = (struct sctp_addip_chunk *)ch;
Vlad Yasevichdf218572007-12-20 14:10:38 -08001185 struct sctp_af *af;
1186 union sctp_addr_param *param;
1187 union sctp_addr paddr;
1188
Marcelo Ricardo Leitnerb6ffe762021-06-28 16:13:43 -03001189 if (ntohs(ch->length) < sizeof(*asconf) + sizeof(struct sctp_paramhdr))
1190 return NULL;
1191
Vlad Yasevichdf218572007-12-20 14:10:38 -08001192 /* Skip over the ADDIP header and find the Address parameter */
1193 param = (union sctp_addr_param *)(asconf + 1);
1194
Shan Wei6a435732011-04-18 19:11:47 +00001195 af = sctp_get_af_specific(param_type2af(param->p.type));
Vlad Yasevichdf218572007-12-20 14:10:38 -08001196 if (unlikely(!af))
1197 return NULL;
1198
Marcelo Ricardo Leitner557fb582021-07-27 23:40:54 -03001199 if (!af->from_addr_param(&paddr, param, peer_port, 0))
Marcelo Ricardo Leitner0c5dc072021-06-28 16:13:41 -03001200 return NULL;
Vlad Yasevichdf218572007-12-20 14:10:38 -08001201
Eric W. Biederman4110cc22012-08-06 08:41:13 +00001202 return __sctp_lookup_association(net, laddr, &paddr, transportp);
Vlad Yasevichdf218572007-12-20 14:10:38 -08001203}
1204
1205
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07001206/* SCTP-AUTH, Section 6.3:
1207* If the receiver does not find a STCB for a packet containing an AUTH
1208* chunk as the first chunk and not a COOKIE-ECHO chunk as the second
1209* chunk, it MUST use the chunks after the AUTH chunk to look up an existing
1210* association.
1211*
1212* This means that any chunks that can help us identify the association need
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001213* to be looked at to find this association.
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07001214*/
Eric W. Biederman4110cc22012-08-06 08:41:13 +00001215static struct sctp_association *__sctp_rcv_walk_lookup(struct net *net,
1216 struct sk_buff *skb,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07001217 const union sctp_addr *laddr,
1218 struct sctp_transport **transportp)
1219{
Vlad Yasevichdf218572007-12-20 14:10:38 -08001220 struct sctp_association *asoc = NULL;
Xin Long922dbc52017-06-30 11:52:13 +08001221 struct sctp_chunkhdr *ch;
Vlad Yasevichdf218572007-12-20 14:10:38 -08001222 int have_auth = 0;
1223 unsigned int chunk_num = 1;
1224 __u8 *ch_end;
1225
1226 /* Walk through the chunks looking for AUTH or ASCONF chunks
1227 * to help us find the association.
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07001228 */
Xin Long922dbc52017-06-30 11:52:13 +08001229 ch = (struct sctp_chunkhdr *)skb->data;
Vlad Yasevichdf218572007-12-20 14:10:38 -08001230 do {
1231 /* Break out if chunk length is less then minimal. */
Xin Long922dbc52017-06-30 11:52:13 +08001232 if (ntohs(ch->length) < sizeof(*ch))
Vlad Yasevichdf218572007-12-20 14:10:38 -08001233 break;
1234
Marcelo Ricardo Leitnere2f036a2016-09-21 08:45:55 -03001235 ch_end = ((__u8 *)ch) + SCTP_PAD4(ntohs(ch->length));
Vlad Yasevichdf218572007-12-20 14:10:38 -08001236 if (ch_end > skb_tail_pointer(skb))
1237 break;
1238
wangweidongcb3f8372013-12-23 12:16:50 +08001239 switch (ch->type) {
wangweidongf7010e62013-12-23 12:16:52 +08001240 case SCTP_CID_AUTH:
1241 have_auth = chunk_num;
1242 break;
Vlad Yasevichdf218572007-12-20 14:10:38 -08001243
wangweidongf7010e62013-12-23 12:16:52 +08001244 case SCTP_CID_COOKIE_ECHO:
1245 /* If a packet arrives containing an AUTH chunk as
1246 * a first chunk, a COOKIE-ECHO chunk as the second
1247 * chunk, and possibly more chunks after them, and
1248 * the receiver does not have an STCB for that
1249 * packet, then authentication is based on
1250 * the contents of the COOKIE- ECHO chunk.
1251 */
1252 if (have_auth == 1 && chunk_num == 2)
1253 return NULL;
1254 break;
Vlad Yasevichdf218572007-12-20 14:10:38 -08001255
wangweidongf7010e62013-12-23 12:16:52 +08001256 case SCTP_CID_ASCONF:
1257 if (have_auth || net->sctp.addip_noauth)
1258 asoc = __sctp_rcv_asconf_lookup(
1259 net, ch, laddr,
1260 sctp_hdr(skb)->source,
1261 transportp);
Gustavo A. R. Silva0572b372020-11-20 12:39:41 -06001262 break;
wangweidongf7010e62013-12-23 12:16:52 +08001263 default:
1264 break;
Vlad Yasevichdf218572007-12-20 14:10:38 -08001265 }
1266
1267 if (asoc)
1268 break;
1269
Xin Long922dbc52017-06-30 11:52:13 +08001270 ch = (struct sctp_chunkhdr *)ch_end;
Vlad Yasevichdf218572007-12-20 14:10:38 -08001271 chunk_num++;
Marcelo Ricardo Leitner50619db2021-06-28 16:13:42 -03001272 } while (ch_end + sizeof(*ch) < skb_tail_pointer(skb));
Vlad Yasevichdf218572007-12-20 14:10:38 -08001273
1274 return asoc;
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07001275}
1276
1277/*
1278 * There are circumstances when we need to look inside the SCTP packet
1279 * for information to help us find the association. Examples
1280 * include looking inside of INIT/INIT-ACK chunks or after the AUTH
1281 * chunks.
1282 */
Eric W. Biederman4110cc22012-08-06 08:41:13 +00001283static struct sctp_association *__sctp_rcv_lookup_harder(struct net *net,
1284 struct sk_buff *skb,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07001285 const union sctp_addr *laddr,
1286 struct sctp_transport **transportp)
1287{
Xin Long922dbc52017-06-30 11:52:13 +08001288 struct sctp_chunkhdr *ch;
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07001289
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -03001290 /* We do not allow GSO frames here as we need to linearize and
1291 * then cannot guarantee frame boundaries. This shouldn't be an
1292 * issue as packets hitting this are mostly INIT or INIT-ACK and
1293 * those cannot be on GSO-style anyway.
1294 */
Daniel Axtens1dd27cd2018-03-09 14:06:09 +11001295 if (skb_is_gso(skb) && skb_is_gso_sctp(skb))
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -03001296 return NULL;
1297
Xin Long922dbc52017-06-30 11:52:13 +08001298 ch = (struct sctp_chunkhdr *)skb->data;
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07001299
Vlad Yasevichdf218572007-12-20 14:10:38 -08001300 /* The code below will attempt to walk the chunk and extract
1301 * parameter information. Before we do that, we need to verify
1302 * that the chunk length doesn't cause overflow. Otherwise, we'll
1303 * walk off the end.
1304 */
Marcelo Ricardo Leitnere2f036a2016-09-21 08:45:55 -03001305 if (SCTP_PAD4(ntohs(ch->length)) > skb->len)
Vlad Yasevichdf218572007-12-20 14:10:38 -08001306 return NULL;
1307
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07001308 /* If this is INIT/INIT-ACK look inside the chunk too. */
wangweidongf482f2f2013-12-19 10:02:42 +08001309 if (ch->type == SCTP_CID_INIT || ch->type == SCTP_CID_INIT_ACK)
Eric W. Biederman4110cc22012-08-06 08:41:13 +00001310 return __sctp_rcv_init_lookup(net, skb, laddr, transportp);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07001311
wangweidongf482f2f2013-12-19 10:02:42 +08001312 return __sctp_rcv_walk_lookup(net, skb, laddr, transportp);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07001313}
1314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315/* Lookup an association for an inbound skb. */
Eric W. Biederman4110cc22012-08-06 08:41:13 +00001316static struct sctp_association *__sctp_rcv_lookup(struct net *net,
1317 struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 const union sctp_addr *paddr,
1319 const union sctp_addr *laddr,
1320 struct sctp_transport **transportp)
1321{
1322 struct sctp_association *asoc;
1323
Eric W. Biederman4110cc22012-08-06 08:41:13 +00001324 asoc = __sctp_lookup_association(net, laddr, paddr, transportp);
Marcelo Ricardo Leitnerb77b7562016-12-28 09:51:56 -02001325 if (asoc)
1326 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328 /* Further lookup for INIT/INIT-ACK packets.
1329 * SCTP Implementors Guide, 2.18 Handling of address
1330 * parameters within the INIT or INIT-ACK.
1331 */
Marcelo Ricardo Leitnerb77b7562016-12-28 09:51:56 -02001332 asoc = __sctp_rcv_lookup_harder(net, skb, laddr, transportp);
1333 if (asoc)
1334 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Marcelo Ricardo Leitnerb77b7562016-12-28 09:51:56 -02001336 if (paddr->sa.sa_family == AF_INET)
1337 pr_debug("sctp: asoc not found for src:%pI4:%d dst:%pI4:%d\n",
1338 &laddr->v4.sin_addr, ntohs(laddr->v4.sin_port),
1339 &paddr->v4.sin_addr, ntohs(paddr->v4.sin_port));
1340 else
1341 pr_debug("sctp: asoc not found for src:%pI6:%d dst:%pI6:%d\n",
1342 &laddr->v6.sin6_addr, ntohs(laddr->v6.sin6_port),
1343 &paddr->v6.sin6_addr, ntohs(paddr->v6.sin6_port));
1344
1345out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 return asoc;
1347}