blob: d7a649d240e5cf4ccf653af08bb66e1a3dcecc8e [file] [log] [blame]
Vlad Yasevich60c778b2008-01-11 09:57:09 -05001/* SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (c) 1999-2000 Cisco, Inc.
3 * Copyright (c) 1999-2001 Motorola, Inc.
4 * Copyright (c) 2001-2003 International Business Machines, Corp.
5 * Copyright (c) 2001 Intel Corp.
6 * Copyright (c) 2001 Nokia, Inc.
7 * Copyright (c) 2001 La Monte H.P. Yarroll
8 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -05009 * This file is part of the SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * These functions handle all input from the IP layer into SCTP.
12 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050013 * This SCTP implementation is free software;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * you can redistribute it and/or modify it under the terms of
15 * the GNU General Public License as published by
16 * the Free Software Foundation; either version 2, or (at your option)
17 * any later version.
18 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050019 * This SCTP implementation is distributed in the hope that it
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
21 * ************************
22 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23 * See the GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
Jeff Kirsher4b2f13a2013-12-06 06:28:48 -080026 * along with GNU CC; see the file COPYING. If not, see
27 * <http://www.gnu.org/licenses/>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 *
29 * Please send any bug reports or fixes you make to the
30 * email address(es):
Daniel Borkmann91705c62013-07-23 14:51:47 +020031 * lksctp developers <linux-sctp@vger.kernel.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 * Written or modified by:
34 * La Monte H.P. Yarroll <piggy@acm.org>
35 * Karl Knutson <karl@athena.chicago.il.us>
36 * Xingang Guo <xingang.guo@intel.com>
37 * Jon Grimm <jgrimm@us.ibm.com>
38 * Hui Huang <hui.huang@nokia.com>
39 * Daisy Chang <daisyc@us.ibm.com>
40 * Sridhar Samudrala <sri@us.ibm.com>
41 * Ardelle Fan <ardelle.fan@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 */
43
44#include <linux/types.h>
45#include <linux/list.h> /* For struct list_head */
46#include <linux/socket.h>
47#include <linux/ip.h>
48#include <linux/time.h> /* For struct timeval */
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090049#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <net/ip.h>
51#include <net/icmp.h>
52#include <net/snmp.h>
53#include <net/sock.h>
54#include <net/xfrm.h>
55#include <net/sctp/sctp.h>
56#include <net/sctp/sm.h>
Vlad Yasevich9ad09772007-12-16 14:06:41 -080057#include <net/sctp/checksum.h>
Pavel Emelyanovdcfc23c2008-07-14 23:03:00 -070058#include <net/net_namespace.h>
NeilBrown0eb71a92018-06-18 12:52:50 +100059#include <linux/rhashtable.h>
Xin Long532ae2f2018-11-12 18:27:15 +080060#include <net/sock_reuseport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62/* Forward declarations for internal helpers. */
63static int sctp_rcv_ootb(struct sk_buff *);
Eric W. Biederman4110cc22012-08-06 08:41:13 +000064static struct sctp_association *__sctp_rcv_lookup(struct net *net,
65 struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 const union sctp_addr *paddr,
Nicolas Dichtel57565992012-10-03 05:43:21 +000067 const union sctp_addr *laddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 struct sctp_transport **transportp);
Xin Long532ae2f2018-11-12 18:27:15 +080069static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(
70 struct net *net, struct sk_buff *skb,
71 const union sctp_addr *laddr,
72 const union sctp_addr *daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static struct sctp_association *__sctp_lookup_association(
Eric W. Biederman4110cc22012-08-06 08:41:13 +000074 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 const union sctp_addr *local,
76 const union sctp_addr *peer,
77 struct sctp_transport **pt);
78
Zhu Yi50b1a782010-03-04 18:01:44 +000079static int sctp_add_backlog(struct sock *sk, struct sk_buff *skb);
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -070080
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82/* Calculate the SCTP checksum of an SCTP packet. */
Eric W. Biedermanb01a2402012-08-06 08:47:55 +000083static inline int sctp_rcv_checksum(struct net *net, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
Arnaldo Carvalho de Melo2c0fd382007-03-13 13:59:32 -030085 struct sctphdr *sh = sctp_hdr(skb);
Vlad Yasevich4458f042009-02-13 08:33:42 +000086 __le32 cmp = sh->checksum;
Joe Stringer024ec3d2013-07-25 10:52:05 +090087 __le32 val = sctp_compute_cksum(skb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 if (val != cmp) {
90 /* CRC failure, dump it. */
Eric Dumazet08e3bae2016-04-27 16:44:34 -070091 __SCTP_INC_STATS(net, SCTP_MIB_CHECKSUMERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 return -1;
93 }
94 return 0;
95}
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097/*
98 * This is the routine which IP calls when receiving an SCTP packet.
99 */
100int sctp_rcv(struct sk_buff *skb)
101{
102 struct sock *sk;
103 struct sctp_association *asoc;
104 struct sctp_endpoint *ep = NULL;
105 struct sctp_ep_common *rcvr;
106 struct sctp_transport *transport = NULL;
107 struct sctp_chunk *chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 union sctp_addr src;
109 union sctp_addr dest;
110 int family;
111 struct sctp_af *af;
Eric W. Biederman4cdadcb2012-08-06 08:40:21 +0000112 struct net *net = dev_net(skb->dev);
Daniel Axtens1dd27cd2018-03-09 14:06:09 +1100113 bool is_gso = skb_is_gso(skb) && skb_is_gso_sctp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
wangweidongcb3f8372013-12-23 12:16:50 +0800115 if (skb->pkt_type != PACKET_HOST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 goto discard_it;
117
Eric Dumazet08e3bae2016-04-27 16:44:34 -0700118 __SCTP_INC_STATS(net, SCTP_MIB_INSCTPPACKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Marcelo Ricardo Leitner3acb50c2016-06-02 15:05:42 -0300120 /* If packet is too small to contain a single chunk, let's not
121 * waste time on it anymore.
122 */
123 if (skb->len < sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr) +
124 skb_transport_offset(skb))
Herbert Xu28cd7752006-10-29 23:46:42 -0800125 goto discard_it;
126
Marcelo Ricardo Leitner4c2f2452016-08-18 14:58:35 -0300127 /* If the packet is fragmented and we need to do crc checking,
128 * it's better to just linearize it otherwise crc computing
129 * takes longer.
130 */
Daniel Axtens1dd27cd2018-03-09 14:06:09 +1100131 if ((!is_gso && skb_linearize(skb)) ||
Marcelo Ricardo Leitner4c2f2452016-08-18 14:58:35 -0300132 !pskb_may_pull(skb, sizeof(struct sctphdr)))
Marcelo Ricardo Leitner3acb50c2016-06-02 15:05:42 -0300133 goto discard_it;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Marcelo Ricardo Leitner3acb50c2016-06-02 15:05:42 -0300135 /* Pull up the IP header. */
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -0700136 __skb_pull(skb, skb_transport_offset(skb));
Tom Herbert202863f2014-08-27 21:27:06 -0700137
138 skb->csum_valid = 0; /* Previous value not applicable */
139 if (skb_csum_unnecessary(skb))
140 __skb_decr_checksum_unnecessary(skb);
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300141 else if (!sctp_checksum_disable &&
Daniel Axtens1dd27cd2018-03-09 14:06:09 +1100142 !is_gso &&
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -0300143 sctp_rcv_checksum(net, skb) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 goto discard_it;
Tom Herbert202863f2014-08-27 21:27:06 -0700145 skb->csum_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Marcelo Ricardo Leitner3acb50c2016-06-02 15:05:42 -0300147 __skb_pull(skb, sizeof(struct sctphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700149 family = ipver2af(ip_hdr(skb)->version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 af = sctp_get_af_specific(family);
151 if (unlikely(!af))
152 goto discard_it;
Marcelo Ricardo Leitnere7487c82016-07-13 15:08:58 -0300153 SCTP_INPUT_CB(skb)->af = af;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 /* Initialize local addresses for lookups. */
156 af->from_skb(&src, skb, 1);
157 af->from_skb(&dest, skb, 0);
158
159 /* If the packet is to or from a non-unicast address,
160 * silently discard the packet.
161 *
162 * This is not clearly defined in the RFC except in section
163 * 8.4 - OOTB handling. However, based on the book "Stream Control
164 * Transmission Protocol" 2.1, "It is important to note that the
165 * IP address of an SCTP transport address must be a routable
166 * unicast address. In other words, IP multicast addresses and
167 * IP broadcast addresses cannot be used in an SCTP transport
168 * address."
169 */
Vlad Yasevich5636bef2006-06-17 22:55:35 -0700170 if (!af->addr_valid(&src, NULL, skb) ||
171 !af->addr_valid(&dest, NULL, skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 goto discard_it;
173
Eric W. Biederman4110cc22012-08-06 08:41:13 +0000174 asoc = __sctp_rcv_lookup(net, skb, &src, &dest, &transport);
Al Viro1c7d1fc2006-11-20 17:08:09 -0800175
Neil Horman0fd9a652005-06-13 15:11:24 -0700176 if (!asoc)
Xin Long532ae2f2018-11-12 18:27:15 +0800177 ep = __sctp_rcv_lookup_endpoint(net, skb, &dest, &src);
Neil Horman0fd9a652005-06-13 15:11:24 -0700178
179 /* Retrieve the common input handling substructure. */
180 rcvr = asoc ? &asoc->base : &ep->base;
181 sk = rcvr->sk;
182
183 /*
184 * If a frame arrives on an interface and the receiving socket is
185 * bound to another interface, via SO_BINDTODEVICE, treat it as OOTB
186 */
wangweidong8d726512013-12-23 12:16:53 +0800187 if (sk->sk_bound_dev_if && (sk->sk_bound_dev_if != af->skb_iif(skb))) {
Xin Longdae399d2016-10-31 20:32:33 +0800188 if (transport) {
189 sctp_transport_put(transport);
Neil Horman0fd9a652005-06-13 15:11:24 -0700190 asoc = NULL;
Xin Longdae399d2016-10-31 20:32:33 +0800191 transport = NULL;
Neil Horman0fd9a652005-06-13 15:11:24 -0700192 } else {
193 sctp_endpoint_put(ep);
194 ep = NULL;
195 }
Eric W. Biederman2ce95502012-08-06 08:43:06 +0000196 sk = net->sctp.ctl_sock;
Neil Horman0fd9a652005-06-13 15:11:24 -0700197 ep = sctp_sk(sk)->ep;
198 sctp_endpoint_hold(ep);
Neil Horman0fd9a652005-06-13 15:11:24 -0700199 rcvr = &ep->base;
200 }
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 /*
203 * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
204 * An SCTP packet is called an "out of the blue" (OOTB)
205 * packet if it is correctly formed, i.e., passed the
206 * receiver's checksum check, but the receiver is not
207 * able to identify the association to which this
208 * packet belongs.
209 */
210 if (!asoc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (sctp_rcv_ootb(skb)) {
Eric Dumazet08e3bae2016-04-27 16:44:34 -0700212 __SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 goto discard_release;
214 }
215 }
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (!xfrm_policy_check(sk, XFRM_POLICY_IN, skb, family))
218 goto discard_release;
Patrick McHardyb59c2702006-01-06 23:06:10 -0800219 nf_reset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Dmitry Mishinfda9ef52006-08-31 15:28:39 -0700221 if (sk_filter(sk, skb))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900222 goto discard_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 /* Create an SCTP packet structure. */
Marcelo Ricardo Leitnercea87682016-03-10 18:33:07 -0300225 chunk = sctp_chunkify(skb, asoc, sk, GFP_ATOMIC);
Herbert Xu2babf9d2006-03-25 01:25:29 -0800226 if (!chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 goto discard_release;
David S. Miller79af02c2005-07-08 21:47:49 -0700228 SCTP_INPUT_CB(skb)->chunk = chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 /* Remember what endpoint is to handle this packet. */
231 chunk->rcvr = rcvr;
232
233 /* Remember the SCTP header. */
Marcelo Ricardo Leitner3acb50c2016-06-02 15:05:42 -0300234 chunk->sctp_hdr = sctp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 /* Set the source and destination addresses of the incoming chunk. */
Al Virod55c41b2006-11-20 17:09:40 -0800237 sctp_init_addrs(chunk, &src, &dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 /* Remember where we came from. */
240 chunk->transport = transport;
241
242 /* Acquire access to the sock lock. Note: We are safe from other
243 * bottom halves on this lock, but a user may be in the lock too,
244 * so check if it is busy.
245 */
wangweidong5bc1d1b2014-01-21 15:44:12 +0800246 bh_lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Vlad Yasevichae53b5b2009-01-22 14:53:23 -0800248 if (sk != rcvr->sk) {
249 /* Our cached sk is different from the rcvr->sk. This is
250 * because migrate()/accept() may have moved the association
251 * to a new socket and released all the sockets. So now we
252 * are holding a lock on the old socket while the user may
253 * be doing something with the new socket. Switch our veiw
254 * of the current sk.
255 */
wangweidong5bc1d1b2014-01-21 15:44:12 +0800256 bh_unlock_sock(sk);
Vlad Yasevichae53b5b2009-01-22 14:53:23 -0800257 sk = rcvr->sk;
wangweidong5bc1d1b2014-01-21 15:44:12 +0800258 bh_lock_sock(sk);
Vlad Yasevichae53b5b2009-01-22 14:53:23 -0800259 }
260
Sridhar Samudralaac0b0462006-08-22 00:15:33 -0700261 if (sock_owned_by_user(sk)) {
Zhu Yi50b1a782010-03-04 18:01:44 +0000262 if (sctp_add_backlog(sk, skb)) {
wangweidong5bc1d1b2014-01-21 15:44:12 +0800263 bh_unlock_sock(sk);
Zhu Yi50b1a782010-03-04 18:01:44 +0000264 sctp_chunk_free(chunk);
265 skb = NULL; /* sctp_chunk_free already freed the skb */
266 goto discard_release;
267 }
Eric Dumazet08e3bae2016-04-27 16:44:34 -0700268 __SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_BACKLOG);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -0700269 } else {
Eric Dumazet08e3bae2016-04-27 16:44:34 -0700270 __SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_SOFTIRQ);
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700271 sctp_inq_push(&chunk->rcvr->inqueue, chunk);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -0700272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
wangweidong5bc1d1b2014-01-21 15:44:12 +0800274 bh_unlock_sock(sk);
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700275
276 /* Release the asoc/ep ref we took in the lookup calls. */
Xin Longdae399d2016-10-31 20:32:33 +0800277 if (transport)
278 sctp_transport_put(transport);
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700279 else
280 sctp_endpoint_put(ep);
Sridhar Samudrala7a48f922006-01-17 11:51:28 -0800281
Herbert Xu2babf9d2006-03-25 01:25:29 -0800282 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284discard_it:
Eric Dumazet08e3bae2016-04-27 16:44:34 -0700285 __SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 kfree_skb(skb);
Herbert Xu2babf9d2006-03-25 01:25:29 -0800287 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289discard_release:
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700290 /* Release the asoc/ep ref we took in the lookup calls. */
Xin Longdae399d2016-10-31 20:32:33 +0800291 if (transport)
292 sctp_transport_put(transport);
Neil Horman0fd9a652005-06-13 15:11:24 -0700293 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 sctp_endpoint_put(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 goto discard_it;
297}
298
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700299/* Process the backlog queue of the socket. Every skb on
300 * the backlog holds a ref on an association or endpoint.
301 * We hold this ref throughout the state machine to make
302 * sure that the structure we need is still around.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 */
304int sctp_backlog_rcv(struct sock *sk, struct sk_buff *skb)
305{
David S. Miller79af02c2005-07-08 21:47:49 -0700306 struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900307 struct sctp_inq *inqueue = &chunk->rcvr->inqueue;
Xin Longdae399d2016-10-31 20:32:33 +0800308 struct sctp_transport *t = chunk->transport;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900309 struct sctp_ep_common *rcvr = NULL;
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700310 int backloged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900312 rcvr = chunk->rcvr;
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800313
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700314 /* If the rcvr is dead then the association or endpoint
315 * has been deleted and we can safely drop the chunk
316 * and refs that we are holding.
317 */
318 if (rcvr->dead) {
319 sctp_chunk_free(chunk);
320 goto done;
321 }
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800322
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700323 if (unlikely(rcvr->sk != sk)) {
324 /* In this case, the association moved from one socket to
325 * another. We are currently sitting on the backlog of the
326 * old socket, so we need to move.
327 * However, since we are here in the process context we
328 * need to take make sure that the user doesn't own
329 * the new socket when we process the packet.
330 * If the new socket is user-owned, queue the chunk to the
331 * backlog of the new socket without dropping any refs.
332 * Otherwise, we can safely push the chunk on the inqueue.
333 */
Sridhar Samudrala7a48f922006-01-17 11:51:28 -0800334
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700335 sk = rcvr->sk;
Marcelo Ricardo Leitnereefc1b12016-07-23 00:32:48 -0300336 local_bh_disable();
wangweidong5bc1d1b2014-01-21 15:44:12 +0800337 bh_lock_sock(sk);
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700338
339 if (sock_owned_by_user(sk)) {
Eric Dumazetf545a382012-04-22 23:34:26 +0000340 if (sk_add_backlog(sk, skb, sk->sk_rcvbuf))
Zhu Yi50b1a782010-03-04 18:01:44 +0000341 sctp_chunk_free(chunk);
342 else
343 backloged = 1;
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700344 } else
345 sctp_inq_push(inqueue, chunk);
346
wangweidong5bc1d1b2014-01-21 15:44:12 +0800347 bh_unlock_sock(sk);
Marcelo Ricardo Leitnereefc1b12016-07-23 00:32:48 -0300348 local_bh_enable();
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700349
350 /* If the chunk was backloged again, don't drop refs */
351 if (backloged)
352 return 0;
353 } else {
354 sctp_inq_push(inqueue, chunk);
355 }
356
357done:
358 /* Release the refs we took in sctp_add_backlog */
359 if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
Xin Longdae399d2016-10-31 20:32:33 +0800360 sctp_transport_put(t);
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700361 else if (SCTP_EP_TYPE_SOCKET == rcvr->type)
362 sctp_endpoint_put(sctp_ep(rcvr));
363 else
364 BUG();
365
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900366 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
Zhu Yi50b1a782010-03-04 18:01:44 +0000369static int sctp_add_backlog(struct sock *sk, struct sk_buff *skb)
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800370{
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700371 struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
Xin Longdae399d2016-10-31 20:32:33 +0800372 struct sctp_transport *t = chunk->transport;
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700373 struct sctp_ep_common *rcvr = chunk->rcvr;
Zhu Yi50b1a782010-03-04 18:01:44 +0000374 int ret;
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800375
Eric Dumazetf545a382012-04-22 23:34:26 +0000376 ret = sk_add_backlog(sk, skb, sk->sk_rcvbuf);
Zhu Yi50b1a782010-03-04 18:01:44 +0000377 if (!ret) {
378 /* Hold the assoc/ep while hanging on the backlog queue.
379 * This way, we know structures we need will not disappear
380 * from us
381 */
382 if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
Xin Longdae399d2016-10-31 20:32:33 +0800383 sctp_transport_hold(t);
Zhu Yi50b1a782010-03-04 18:01:44 +0000384 else if (SCTP_EP_TYPE_SOCKET == rcvr->type)
385 sctp_endpoint_hold(sctp_ep(rcvr));
386 else
387 BUG();
388 }
389 return ret;
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800390
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800391}
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393/* Handle icmp frag needed error. */
394void sctp_icmp_frag_needed(struct sock *sk, struct sctp_association *asoc,
395 struct sctp_transport *t, __u32 pmtu)
396{
Wei Yongjun91bd6b12008-10-23 00:59:52 -0700397 if (!t || (t->pathmtu <= pmtu))
Frank Filz52ccb8e2005-12-22 11:36:46 -0800398 return;
399
Vlad Yasevich8a479492007-06-07 14:21:05 -0400400 if (sock_owned_by_user(sk)) {
Xin Longd8053972018-10-15 19:58:29 +0800401 atomic_set(&t->mtu_info, pmtu);
Vlad Yasevich8a479492007-06-07 14:21:05 -0400402 asoc->pmtu_pending = 1;
403 t->pmtu_pending = 1;
404 return;
405 }
406
Marcelo Ricardo Leitnercc35c3d2018-01-05 11:17:17 -0200407 if (!(t->param_flags & SPP_PMTUD_ENABLE))
408 /* We can't allow retransmitting in such case, as the
409 * retransmission would be sized just as before, and thus we
410 * would get another icmp, and retransmit again.
411 */
412 return;
Frank Filz52ccb8e2005-12-22 11:36:46 -0800413
Marcelo Ricardo Leitnerb6c57342018-01-05 11:17:18 -0200414 /* Update transports view of the MTU. Return if no update was needed.
415 * If an update wasn't needed/possible, it also doesn't make sense to
416 * try to retransmit now.
417 */
418 if (!sctp_transport_update_pmtu(t, pmtu))
419 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Marcelo Ricardo Leitnercc35c3d2018-01-05 11:17:17 -0200421 /* Update association pmtu. */
422 sctp_assoc_sync_pmtu(asoc);
423
424 /* Retransmit with the new pmtu setting. */
Frank Filz52ccb8e2005-12-22 11:36:46 -0800425 sctp_retransmit(&asoc->outqueue, t, SCTP_RTXR_PMTUD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
David S. Millerec18d9a2012-07-12 00:25:15 -0700428void sctp_icmp_redirect(struct sock *sk, struct sctp_transport *t,
429 struct sk_buff *skb)
David S. Miller55be7a92012-07-11 21:27:49 -0700430{
431 struct dst_entry *dst;
432
Xin Long1cc276c2017-10-18 21:37:49 +0800433 if (sock_owned_by_user(sk) || !t)
David S. Miller55be7a92012-07-11 21:27:49 -0700434 return;
435 dst = sctp_transport_dst_check(t);
David S. Miller1ed5c482012-07-12 00:41:25 -0700436 if (dst)
David S. Miller6700c272012-07-17 03:29:28 -0700437 dst->ops->redirect(dst, sk, skb);
David S. Miller55be7a92012-07-11 21:27:49 -0700438}
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440/*
441 * SCTP Implementer's Guide, 2.37 ICMP handling procedures
442 *
443 * ICMP8) If the ICMP code is a "Unrecognized next header type encountered"
444 * or a "Protocol Unreachable" treat this message as an abort
445 * with the T bit set.
446 *
447 * This function sends an event to the state machine, which will abort the
448 * association.
449 *
450 */
451void sctp_icmp_proto_unreachable(struct sock *sk,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900452 struct sctp_association *asoc,
453 struct sctp_transport *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Vlad Yasevich50b5d6a2010-05-06 00:56:07 -0700455 if (sock_owned_by_user(sk)) {
456 if (timer_pending(&t->proto_unreach_timer))
457 return;
458 else {
459 if (!mod_timer(&t->proto_unreach_timer,
460 jiffies + (HZ/20)))
461 sctp_association_hold(asoc);
462 }
Vlad Yasevich50b5d6a2010-05-06 00:56:07 -0700463 } else {
Eric W. Biederman55e26eb2012-08-07 07:25:24 +0000464 struct net *net = sock_net(sk);
465
Daniel Borkmannbb333812013-06-28 19:49:40 +0200466 pr_debug("%s: unrecognized next header type "
467 "encountered!\n", __func__);
468
Ying Xue25cc4ae2013-02-03 20:32:57 +0000469 if (del_timer(&t->proto_unreach_timer))
Vlad Yasevich50b5d6a2010-05-06 00:56:07 -0700470 sctp_association_put(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Eric W. Biederman55e26eb2012-08-07 07:25:24 +0000472 sctp_do_sm(net, SCTP_EVENT_T_OTHER,
Vlad Yasevich50b5d6a2010-05-06 00:56:07 -0700473 SCTP_ST_OTHER(SCTP_EVENT_ICMP_PROTO_UNREACH),
474 asoc->state, asoc->ep, asoc, t,
475 GFP_ATOMIC);
476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
479/* Common lookup code for icmp/icmpv6 error handler. */
Eric W. Biederman4110cc22012-08-06 08:41:13 +0000480struct sock *sctp_err_lookup(struct net *net, int family, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 struct sctphdr *sctphdr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 struct sctp_association **app,
483 struct sctp_transport **tpp)
484{
Davide Caratti804ec7e2017-05-25 19:14:56 +0200485 struct sctp_init_chunk *chunkhdr, _chunkhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 union sctp_addr saddr;
487 union sctp_addr daddr;
488 struct sctp_af *af;
489 struct sock *sk = NULL;
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700490 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 struct sctp_transport *transport = NULL;
Wei Yongjun7115e632008-06-19 16:07:48 -0700492 __u32 vtag = ntohl(sctphdr->vtag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700494 *app = NULL; *tpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 af = sctp_get_af_specific(family);
497 if (unlikely(!af)) {
498 return NULL;
499 }
500
501 /* Initialize local addresses for lookups. */
502 af->from_skb(&saddr, skb, 1);
503 af->from_skb(&daddr, skb, 0);
504
505 /* Look for an association that matches the incoming ICMP error
506 * packet.
507 */
Eric W. Biederman4110cc22012-08-06 08:41:13 +0000508 asoc = __sctp_lookup_association(net, &saddr, &daddr, &transport);
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700509 if (!asoc)
510 return NULL;
511
512 sk = asoc->base.sk;
513
Wei Yongjun7115e632008-06-19 16:07:48 -0700514 /* RFC 4960, Appendix C. ICMP Handling
515 *
516 * ICMP6) An implementation MUST validate that the Verification Tag
517 * contained in the ICMP message matches the Verification Tag of
518 * the peer. If the Verification Tag is not 0 and does NOT
519 * match, discard the ICMP message. If it is 0 and the ICMP
520 * message contains enough bytes to verify that the chunk type is
521 * an INIT chunk and that the Initiate Tag matches the tag of the
522 * peer, continue with ICMP7. If the ICMP message is too short
523 * or the chunk type or the Initiate Tag does not match, silently
524 * discard the packet.
525 */
526 if (vtag == 0) {
Davide Caratti804ec7e2017-05-25 19:14:56 +0200527 /* chunk header + first 4 octects of init header */
528 chunkhdr = skb_header_pointer(skb, skb_transport_offset(skb) +
529 sizeof(struct sctphdr),
530 sizeof(struct sctp_chunkhdr) +
531 sizeof(__be32), &_chunkhdr);
532 if (!chunkhdr ||
Wei Yongjun7115e632008-06-19 16:07:48 -0700533 chunkhdr->chunk_hdr.type != SCTP_CID_INIT ||
Davide Caratti804ec7e2017-05-25 19:14:56 +0200534 ntohl(chunkhdr->init_hdr.init_tag) != asoc->c.my_vtag)
Wei Yongjun7115e632008-06-19 16:07:48 -0700535 goto out;
Davide Caratti804ec7e2017-05-25 19:14:56 +0200536
Wei Yongjun7115e632008-06-19 16:07:48 -0700537 } else if (vtag != asoc->c.peer_vtag) {
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700538 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
540
wangweidong5bc1d1b2014-01-21 15:44:12 +0800541 bh_lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 /* If too many ICMPs get dropped on busy
544 * servers this needs to be solved differently.
545 */
546 if (sock_owned_by_user(sk))
Eric Dumazet02a1d6e2016-04-27 16:44:39 -0700547 __NET_INC_STATS(net, LINUX_MIB_LOCKDROPPEDICMPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 *app = asoc;
550 *tpp = transport;
551 return sk;
552
553out:
Xin Longdae399d2016-10-31 20:32:33 +0800554 sctp_transport_put(transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return NULL;
556}
557
558/* Common cleanup code for icmp/icmpv6 error handler. */
Xin Longdae399d2016-10-31 20:32:33 +0800559void sctp_err_finish(struct sock *sk, struct sctp_transport *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
wangweidong5bc1d1b2014-01-21 15:44:12 +0800561 bh_unlock_sock(sk);
Xin Longdae399d2016-10-31 20:32:33 +0800562 sctp_transport_put(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
565/*
566 * This routine is called by the ICMP module when it gets some
567 * sort of error condition. If err < 0 then the socket should
568 * be closed and the error returned to the user. If err > 0
569 * it's just the icmp type << 8 | icmp code. After adjustment
570 * header points to the first 8 bytes of the sctp header. We need
571 * to find the appropriate port.
572 *
573 * The locking strategy used here is very "optimistic". When
574 * someone else accesses the socket the ICMP is just dropped
575 * and for some paths there is no check at all.
576 * A more general error queue to queue errors for later handling
577 * is probably better.
578 *
579 */
Stefano Brivio32bbd872018-11-08 12:19:21 +0100580int sctp_v4_err(struct sk_buff *skb, __u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000582 const struct iphdr *iph = (const struct iphdr *)skb->data;
Arnaldo Carvalho de Meloa27ef742007-03-13 17:17:10 -0300583 const int ihlen = iph->ihl * 4;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300584 const int type = icmp_hdr(skb)->type;
585 const int code = icmp_hdr(skb)->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 struct sock *sk;
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700587 struct sctp_association *asoc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 struct sctp_transport *transport;
589 struct inet_sock *inet;
Simon Hormanaef6de52013-05-30 20:36:11 +0000590 __u16 saveip, savesctp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 int err;
Eric W. Biederman4110cc22012-08-06 08:41:13 +0000592 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 /* Fix up skb to look at the embedded net header. */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700595 saveip = skb->network_header;
596 savesctp = skb->transport_header;
Arnaldo Carvalho de Melo31c77112007-03-10 19:04:55 -0300597 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloa27ef742007-03-13 17:17:10 -0300598 skb_set_transport_header(skb, ihlen);
Eric W. Biederman4110cc22012-08-06 08:41:13 +0000599 sk = sctp_err_lookup(net, AF_INET, skb, sctp_hdr(skb), &asoc, &transport);
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700600 /* Put back, the original values. */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700601 skb->network_header = saveip;
602 skb->transport_header = savesctp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (!sk) {
Eric Dumazet5d3848b2016-04-27 16:44:29 -0700604 __ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
Stefano Brivio32bbd872018-11-08 12:19:21 +0100605 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607 /* Warning: The sock lock is held. Remember to call
608 * sctp_err_finish!
609 */
610
611 switch (type) {
612 case ICMP_PARAMETERPROB:
613 err = EPROTO;
614 break;
615 case ICMP_DEST_UNREACH:
616 if (code > NR_ICMP_UNREACH)
617 goto out_unlock;
618
619 /* PMTU discovery (RFC1191) */
620 if (ICMP_FRAG_NEEDED == code) {
Marcelo Ricardo Leitner3822a5f2016-03-19 12:17:20 -0300621 sctp_icmp_frag_needed(sk, asoc, transport,
Marcelo Ricardo Leitnere2f036a2016-09-21 08:45:55 -0300622 SCTP_TRUNC4(info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 goto out_unlock;
wangweidong8d726512013-12-23 12:16:53 +0800624 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if (ICMP_PROT_UNREACH == code) {
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700626 sctp_icmp_proto_unreachable(sk, asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 transport);
628 goto out_unlock;
629 }
630 }
631 err = icmp_err_convert[code].errno;
632 break;
633 case ICMP_TIME_EXCEEDED:
634 /* Ignore any time exceeded errors due to fragment reassembly
635 * timeouts.
636 */
637 if (ICMP_EXC_FRAGTIME == code)
638 goto out_unlock;
639
640 err = EHOSTUNREACH;
641 break;
David S. Miller55be7a92012-07-11 21:27:49 -0700642 case ICMP_REDIRECT:
643 sctp_icmp_redirect(sk, transport, skb);
Daniel Borkmann3f96a532013-09-16 12:36:02 +0200644 /* Fall through to out_unlock. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 default:
646 goto out_unlock;
647 }
648
649 inet = inet_sk(sk);
650 if (!sock_owned_by_user(sk) && inet->recverr) {
651 sk->sk_err = err;
652 sk->sk_error_report(sk);
653 } else { /* Only an error on timeout */
654 sk->sk_err_soft = err;
655 }
656
657out_unlock:
Xin Longdae399d2016-10-31 20:32:33 +0800658 sctp_err_finish(sk, transport);
Stefano Brivio32bbd872018-11-08 12:19:21 +0100659 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
662/*
663 * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
664 *
665 * This function scans all the chunks in the OOTB packet to determine if
666 * the packet should be discarded right away. If a response might be needed
667 * for this packet, or, if further processing is possible, the packet will
668 * be queued to a proper inqueue for the next phase of handling.
669 *
670 * Output:
671 * Return 0 - If further processing is needed.
672 * Return 1 - If the packet can be discarded right away.
673 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200674static int sctp_rcv_ootb(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Xin Long922dbc52017-06-30 11:52:13 +0800676 struct sctp_chunkhdr *ch, _ch;
Marcelo Ricardo Leitner3acb50c2016-06-02 15:05:42 -0300677 int ch_end, offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679 /* Scan through all the chunks in the packet. */
Tsutomu Fujiia7d1f1b2006-01-17 11:57:09 -0800680 do {
Marcelo Ricardo Leitner3acb50c2016-06-02 15:05:42 -0300681 /* Make sure we have at least the header there */
Xin Long922dbc52017-06-30 11:52:13 +0800682 if (offset + sizeof(_ch) > skb->len)
Marcelo Ricardo Leitner3acb50c2016-06-02 15:05:42 -0300683 break;
684
685 ch = skb_header_pointer(skb, offset, sizeof(*ch), &_ch);
686
Tsutomu Fujiia7d1f1b2006-01-17 11:57:09 -0800687 /* Break out if chunk length is less then minimal. */
Xin Long922dbc52017-06-30 11:52:13 +0800688 if (ntohs(ch->length) < sizeof(_ch))
Tsutomu Fujiia7d1f1b2006-01-17 11:57:09 -0800689 break;
690
Marcelo Ricardo Leitnere2f036a2016-09-21 08:45:55 -0300691 ch_end = offset + SCTP_PAD4(ntohs(ch->length));
Marcelo Ricardo Leitner3acb50c2016-06-02 15:05:42 -0300692 if (ch_end > skb->len)
Tsutomu Fujiia7d1f1b2006-01-17 11:57:09 -0800693 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 /* RFC 8.4, 2) If the OOTB packet contains an ABORT chunk, the
696 * receiver MUST silently discard the OOTB packet and take no
697 * further action.
698 */
699 if (SCTP_CID_ABORT == ch->type)
700 goto discard;
701
702 /* RFC 8.4, 6) If the packet contains a SHUTDOWN COMPLETE
703 * chunk, the receiver should silently discard the packet
704 * and take no further action.
705 */
706 if (SCTP_CID_SHUTDOWN_COMPLETE == ch->type)
707 goto discard;
708
Vlad Yasevich3c77f962007-09-17 15:14:28 -0400709 /* RFC 4460, 2.11.2
710 * This will discard packets with INIT chunk bundled as
711 * subsequent chunks in the packet. When INIT is first,
712 * the normal INIT processing will discard the chunk.
713 */
714 if (SCTP_CID_INIT == ch->type && (void *)ch != skb->data)
715 goto discard;
716
Marcelo Ricardo Leitner3acb50c2016-06-02 15:05:42 -0300717 offset = ch_end;
718 } while (ch_end < skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 return 0;
721
722discard:
723 return 1;
724}
725
726/* Insert endpoint into the hash table. */
Xin Long76c6d982018-11-12 18:27:16 +0800727static int __sctp_hash_endpoint(struct sctp_endpoint *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
Xin Long76c6d982018-11-12 18:27:16 +0800729 struct sock *sk = ep->base.sk;
730 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 struct sctp_hashbucket *head;
Xin Long76c6d982018-11-12 18:27:16 +0800732 struct sctp_ep_common *epb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 epb = &ep->base;
Eric W. Biederman4cdadcb2012-08-06 08:40:21 +0000735 epb->hashent = sctp_ep_hashfn(net, epb->bind_addr.port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 head = &sctp_ep_hashtable[epb->hashent];
737
Xin Long76c6d982018-11-12 18:27:16 +0800738 if (sk->sk_reuseport) {
739 bool any = sctp_is_ep_boundall(sk);
740 struct sctp_ep_common *epb2;
741 struct list_head *list;
742 int cnt = 0, err = 1;
743
744 list_for_each(list, &ep->base.bind_addr.address_list)
745 cnt++;
746
747 sctp_for_each_hentry(epb2, &head->chain) {
748 struct sock *sk2 = epb2->sk;
749
750 if (!net_eq(sock_net(sk2), net) || sk2 == sk ||
751 !uid_eq(sock_i_uid(sk2), sock_i_uid(sk)) ||
752 !sk2->sk_reuseport)
753 continue;
754
755 err = sctp_bind_addrs_check(sctp_sk(sk2),
756 sctp_sk(sk), cnt);
757 if (!err) {
758 err = reuseport_add_sock(sk, sk2, any);
759 if (err)
760 return err;
761 break;
762 } else if (err < 0) {
763 return err;
764 }
765 }
766
767 if (err) {
768 err = reuseport_alloc(sk, any);
769 if (err)
770 return err;
771 }
772 }
773
wangweidong387602d2014-01-21 15:44:09 +0800774 write_lock(&head->lock);
Vlad Yasevichd970dbf2007-11-09 11:43:40 -0500775 hlist_add_head(&epb->node, &head->chain);
wangweidong387602d2014-01-21 15:44:09 +0800776 write_unlock(&head->lock);
Xin Long76c6d982018-11-12 18:27:16 +0800777 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
779
780/* Add an endpoint to the hash. Local BH-safe. */
Xin Long76c6d982018-11-12 18:27:16 +0800781int sctp_hash_endpoint(struct sctp_endpoint *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Xin Long76c6d982018-11-12 18:27:16 +0800783 int err;
784
wangweidong79b91132014-01-21 15:44:07 +0800785 local_bh_disable();
Xin Long76c6d982018-11-12 18:27:16 +0800786 err = __sctp_hash_endpoint(ep);
wangweidong79b91132014-01-21 15:44:07 +0800787 local_bh_enable();
Xin Long76c6d982018-11-12 18:27:16 +0800788
789 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
792/* Remove endpoint from the hash table. */
793static void __sctp_unhash_endpoint(struct sctp_endpoint *ep)
794{
Xin Long76c6d982018-11-12 18:27:16 +0800795 struct sock *sk = ep->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 struct sctp_hashbucket *head;
797 struct sctp_ep_common *epb;
798
799 epb = &ep->base;
800
Xin Long76c6d982018-11-12 18:27:16 +0800801 epb->hashent = sctp_ep_hashfn(sock_net(sk), epb->bind_addr.port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 head = &sctp_ep_hashtable[epb->hashent];
804
Xin Long76c6d982018-11-12 18:27:16 +0800805 if (rcu_access_pointer(sk->sk_reuseport_cb))
806 reuseport_detach_sock(sk);
807
wangweidong387602d2014-01-21 15:44:09 +0800808 write_lock(&head->lock);
Neil Horman2eebc1e2012-07-16 09:13:51 +0000809 hlist_del_init(&epb->node);
wangweidong387602d2014-01-21 15:44:09 +0800810 write_unlock(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
812
813/* Remove endpoint from the hash. Local BH-safe. */
814void sctp_unhash_endpoint(struct sctp_endpoint *ep)
815{
wangweidong79b91132014-01-21 15:44:07 +0800816 local_bh_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 __sctp_unhash_endpoint(ep);
wangweidong79b91132014-01-21 15:44:07 +0800818 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820
Xin Long532ae2f2018-11-12 18:27:15 +0800821static inline __u32 sctp_hashfn(const struct net *net, __be16 lport,
822 const union sctp_addr *paddr, __u32 seed)
823{
824 __u32 addr;
825
826 if (paddr->sa.sa_family == AF_INET6)
827 addr = jhash(&paddr->v6.sin6_addr, 16, seed);
828 else
829 addr = (__force __u32)paddr->v4.sin_addr.s_addr;
830
831 return jhash_3words(addr, ((__force __u32)paddr->v4.sin_port) << 16 |
832 (__force __u32)lport, net_hash_mix(net), seed);
833}
834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835/* Look up an endpoint. */
Xin Long532ae2f2018-11-12 18:27:15 +0800836static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(
837 struct net *net, struct sk_buff *skb,
838 const union sctp_addr *laddr,
839 const union sctp_addr *paddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
841 struct sctp_hashbucket *head;
842 struct sctp_ep_common *epb;
843 struct sctp_endpoint *ep;
Xin Long532ae2f2018-11-12 18:27:15 +0800844 struct sock *sk;
845 __be16 lport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 int hash;
847
Xin Long532ae2f2018-11-12 18:27:15 +0800848 lport = laddr->v4.sin_port;
849 hash = sctp_ep_hashfn(net, ntohs(lport));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 head = &sctp_ep_hashtable[hash];
851 read_lock(&head->lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800852 sctp_for_each_hentry(epb, &head->chain) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 ep = sctp_ep(epb);
Eric W. Biederman4cdadcb2012-08-06 08:40:21 +0000854 if (sctp_endpoint_is_match(ep, net, laddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 goto hit;
856 }
857
Eric W. Biederman2ce95502012-08-06 08:43:06 +0000858 ep = sctp_sk(net->sctp.ctl_sock)->ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
860hit:
Xin Long532ae2f2018-11-12 18:27:15 +0800861 sk = ep->base.sk;
862 if (sk->sk_reuseport) {
863 __u32 phash = sctp_hashfn(net, lport, paddr, 0);
864
865 sk = reuseport_select_sock(sk, phash, skb,
866 sizeof(struct sctphdr));
867 if (sk)
868 ep = sctp_sk(sk)->ep;
869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 sctp_endpoint_hold(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 read_unlock(&head->lock);
872 return ep;
873}
874
Xin Longd6c02562015-12-30 23:50:46 +0800875/* rhashtable for transport */
876struct sctp_hash_cmp_arg {
Xin Long7fda7022016-11-15 23:23:11 +0800877 const union sctp_addr *paddr;
878 const struct net *net;
Xin Long8d325032017-10-28 19:43:55 +0800879 __be16 lport;
Xin Longd6c02562015-12-30 23:50:46 +0800880};
881
882static inline int sctp_hash_cmp(struct rhashtable_compare_arg *arg,
883 const void *ptr)
884{
Xin Long715f5552016-09-10 23:11:23 +0800885 struct sctp_transport *t = (struct sctp_transport *)ptr;
Xin Longd6c02562015-12-30 23:50:46 +0800886 const struct sctp_hash_cmp_arg *x = arg->key;
Xin Long715f5552016-09-10 23:11:23 +0800887 int err = 1;
Xin Longd6c02562015-12-30 23:50:46 +0800888
Xin Longd6c02562015-12-30 23:50:46 +0800889 if (!sctp_cmp_addr_exact(&t->ipaddr, x->paddr))
Xin Long715f5552016-09-10 23:11:23 +0800890 return err;
891 if (!sctp_transport_hold(t))
892 return err;
893
Xin Long7fda7022016-11-15 23:23:11 +0800894 if (!net_eq(sock_net(t->asoc->base.sk), x->net))
Xin Long715f5552016-09-10 23:11:23 +0800895 goto out;
Xin Long7fda7022016-11-15 23:23:11 +0800896 if (x->lport != htons(t->asoc->base.bind_addr.port))
897 goto out;
Xin Longd6c02562015-12-30 23:50:46 +0800898
Xin Long715f5552016-09-10 23:11:23 +0800899 err = 0;
900out:
901 sctp_transport_put(t);
902 return err;
Xin Longd6c02562015-12-30 23:50:46 +0800903}
904
Xin Long8d325032017-10-28 19:43:55 +0800905static inline __u32 sctp_hash_obj(const void *data, u32 len, u32 seed)
Xin Longd6c02562015-12-30 23:50:46 +0800906{
907 const struct sctp_transport *t = data;
Xin Longd6c02562015-12-30 23:50:46 +0800908
Xin Long532ae2f2018-11-12 18:27:15 +0800909 return sctp_hashfn(sock_net(t->asoc->base.sk),
910 htons(t->asoc->base.bind_addr.port),
911 &t->ipaddr, seed);
Xin Longd6c02562015-12-30 23:50:46 +0800912}
913
Xin Long8d325032017-10-28 19:43:55 +0800914static inline __u32 sctp_hash_key(const void *data, u32 len, u32 seed)
Xin Longd6c02562015-12-30 23:50:46 +0800915{
916 const struct sctp_hash_cmp_arg *x = data;
Xin Longd6c02562015-12-30 23:50:46 +0800917
Xin Long532ae2f2018-11-12 18:27:15 +0800918 return sctp_hashfn(x->net, x->lport, x->paddr, seed);
Xin Longd6c02562015-12-30 23:50:46 +0800919}
920
921static const struct rhashtable_params sctp_hash_params = {
922 .head_offset = offsetof(struct sctp_transport, node),
923 .hashfn = sctp_hash_key,
924 .obj_hashfn = sctp_hash_obj,
925 .obj_cmpfn = sctp_hash_cmp,
926 .automatic_shrinking = true,
927};
928
929int sctp_transport_hashtable_init(void)
930{
Xin Long7fda7022016-11-15 23:23:11 +0800931 return rhltable_init(&sctp_transport_hashtable, &sctp_hash_params);
Xin Longd6c02562015-12-30 23:50:46 +0800932}
933
934void sctp_transport_hashtable_destroy(void)
935{
Xin Long7fda7022016-11-15 23:23:11 +0800936 rhltable_destroy(&sctp_transport_hashtable);
Xin Longd6c02562015-12-30 23:50:46 +0800937}
938
Xin Long7fda7022016-11-15 23:23:11 +0800939int sctp_hash_transport(struct sctp_transport *t)
Xin Longd6c02562015-12-30 23:50:46 +0800940{
Xin Longcd2b7082017-02-17 16:35:24 +0800941 struct sctp_transport *transport;
942 struct rhlist_head *tmp, *list;
Xin Longd6c02562015-12-30 23:50:46 +0800943 struct sctp_hash_cmp_arg arg;
Xin Long7fda7022016-11-15 23:23:11 +0800944 int err;
Xin Longd6c02562015-12-30 23:50:46 +0800945
Xin Longdd7445a2016-01-16 20:17:17 +0800946 if (t->asoc->temp)
Xin Long7fda7022016-11-15 23:23:11 +0800947 return 0;
Xin Longdd7445a2016-01-16 20:17:17 +0800948
Xin Longd6c02562015-12-30 23:50:46 +0800949 arg.net = sock_net(t->asoc->base.sk);
Xin Long7fda7022016-11-15 23:23:11 +0800950 arg.paddr = &t->ipaddr;
951 arg.lport = htons(t->asoc->base.bind_addr.port);
Xin Longd6c02562015-12-30 23:50:46 +0800952
Xin Long5179b262017-02-28 12:41:29 +0800953 rcu_read_lock();
Xin Longcd2b7082017-02-17 16:35:24 +0800954 list = rhltable_lookup(&sctp_transport_hashtable, &arg,
955 sctp_hash_params);
956
957 rhl_for_each_entry_rcu(transport, tmp, list, node)
958 if (transport->asoc->ep == t->asoc->ep) {
Xin Long5179b262017-02-28 12:41:29 +0800959 rcu_read_unlock();
Xin Long27af86b2018-02-12 18:29:06 +0800960 return -EEXIST;
Xin Longcd2b7082017-02-17 16:35:24 +0800961 }
Xin Long5179b262017-02-28 12:41:29 +0800962 rcu_read_unlock();
Xin Longcd2b7082017-02-17 16:35:24 +0800963
Xin Long7fda7022016-11-15 23:23:11 +0800964 err = rhltable_insert_key(&sctp_transport_hashtable, &arg,
965 &t->node, sctp_hash_params);
966 if (err)
967 pr_err_once("insert transport fail, errno %d\n", err);
968
969 return err;
Xin Longd6c02562015-12-30 23:50:46 +0800970}
971
972void sctp_unhash_transport(struct sctp_transport *t)
973{
Xin Longdd7445a2016-01-16 20:17:17 +0800974 if (t->asoc->temp)
975 return;
976
Xin Long7fda7022016-11-15 23:23:11 +0800977 rhltable_remove(&sctp_transport_hashtable, &t->node,
978 sctp_hash_params);
Xin Longd6c02562015-12-30 23:50:46 +0800979}
980
Xin Long7fda7022016-11-15 23:23:11 +0800981/* return a transport with holding it */
Xin Longd6c02562015-12-30 23:50:46 +0800982struct sctp_transport *sctp_addrs_lookup_transport(
983 struct net *net,
984 const union sctp_addr *laddr,
985 const union sctp_addr *paddr)
986{
Xin Long7fda7022016-11-15 23:23:11 +0800987 struct rhlist_head *tmp, *list;
988 struct sctp_transport *t;
Xin Longd6c02562015-12-30 23:50:46 +0800989 struct sctp_hash_cmp_arg arg = {
Xin Longd6c02562015-12-30 23:50:46 +0800990 .paddr = paddr,
991 .net = net,
Xin Long7fda7022016-11-15 23:23:11 +0800992 .lport = laddr->v4.sin_port,
Xin Longd6c02562015-12-30 23:50:46 +0800993 };
994
Xin Long7fda7022016-11-15 23:23:11 +0800995 list = rhltable_lookup(&sctp_transport_hashtable, &arg,
996 sctp_hash_params);
997
998 rhl_for_each_entry_rcu(t, tmp, list, node) {
999 if (!sctp_transport_hold(t))
1000 continue;
1001
1002 if (sctp_bind_addr_match(&t->asoc->base.bind_addr,
1003 laddr, sctp_sk(t->asoc->base.sk)))
1004 return t;
1005 sctp_transport_put(t);
1006 }
1007
1008 return NULL;
Xin Longd6c02562015-12-30 23:50:46 +08001009}
1010
Xin Long7fda7022016-11-15 23:23:11 +08001011/* return a transport without holding it, as it's only used under sock lock */
Xin Longd6c02562015-12-30 23:50:46 +08001012struct sctp_transport *sctp_epaddr_lookup_transport(
1013 const struct sctp_endpoint *ep,
1014 const union sctp_addr *paddr)
1015{
Xin Longd6c02562015-12-30 23:50:46 +08001016 struct net *net = sock_net(ep->base.sk);
Xin Long7fda7022016-11-15 23:23:11 +08001017 struct rhlist_head *tmp, *list;
1018 struct sctp_transport *t;
Xin Long65a51242016-01-14 13:49:34 +08001019 struct sctp_hash_cmp_arg arg = {
Xin Long65a51242016-01-14 13:49:34 +08001020 .paddr = paddr,
1021 .net = net,
Xin Long7fda7022016-11-15 23:23:11 +08001022 .lport = htons(ep->base.bind_addr.port),
Xin Long65a51242016-01-14 13:49:34 +08001023 };
Xin Longd6c02562015-12-30 23:50:46 +08001024
Xin Long7fda7022016-11-15 23:23:11 +08001025 list = rhltable_lookup(&sctp_transport_hashtable, &arg,
1026 sctp_hash_params);
1027
1028 rhl_for_each_entry_rcu(t, tmp, list, node)
1029 if (ep == t->asoc->ep)
1030 return t;
1031
1032 return NULL;
Xin Longd6c02562015-12-30 23:50:46 +08001033}
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035/* Look up an association. */
1036static struct sctp_association *__sctp_lookup_association(
Eric W. Biederman4110cc22012-08-06 08:41:13 +00001037 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 const union sctp_addr *local,
1039 const union sctp_addr *peer,
1040 struct sctp_transport **pt)
1041{
Xin Long4f008782015-12-30 23:50:47 +08001042 struct sctp_transport *t;
Xin Long1eed6772016-01-22 01:49:07 +08001043 struct sctp_association *asoc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Xin Long4f008782015-12-30 23:50:47 +08001045 t = sctp_addrs_lookup_transport(net, local, peer);
Xin Long7fda7022016-11-15 23:23:11 +08001046 if (!t)
Xin Long1eed6772016-01-22 01:49:07 +08001047 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Xin Long1eed6772016-01-22 01:49:07 +08001049 asoc = t->asoc;
Xin Long4f008782015-12-30 23:50:47 +08001050 *pt = t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Xin Long1eed6772016-01-22 01:49:07 +08001052out:
Xin Long1eed6772016-01-22 01:49:07 +08001053 return asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054}
1055
Xin Long4f008782015-12-30 23:50:47 +08001056/* Look up an association. protected by RCU read lock */
Daniel Borkmanndda91922013-06-17 11:40:05 +02001057static
Eric W. Biederman4110cc22012-08-06 08:41:13 +00001058struct sctp_association *sctp_lookup_association(struct net *net,
1059 const union sctp_addr *laddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 const union sctp_addr *paddr,
Daniel Borkmanndda91922013-06-17 11:40:05 +02001061 struct sctp_transport **transportp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
1063 struct sctp_association *asoc;
1064
Xin Longf46c7012016-02-15 14:28:03 +08001065 rcu_read_lock();
Eric W. Biederman4110cc22012-08-06 08:41:13 +00001066 asoc = __sctp_lookup_association(net, laddr, paddr, transportp);
Xin Longf46c7012016-02-15 14:28:03 +08001067 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 return asoc;
1070}
1071
1072/* Is there an association matching the given local and peer addresses? */
Xin Long53066532018-03-26 16:55:00 +08001073bool sctp_has_association(struct net *net,
1074 const union sctp_addr *laddr,
1075 const union sctp_addr *paddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 struct sctp_transport *transport;
1078
Xin Long53066532018-03-26 16:55:00 +08001079 if (sctp_lookup_association(net, laddr, paddr, &transport)) {
Xin Longdae399d2016-10-31 20:32:33 +08001080 sctp_transport_put(transport);
Xin Long53066532018-03-26 16:55:00 +08001081 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
1083
Xin Long53066532018-03-26 16:55:00 +08001084 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085}
1086
1087/*
1088 * SCTP Implementors Guide, 2.18 Handling of address
1089 * parameters within the INIT or INIT-ACK.
1090 *
1091 * D) When searching for a matching TCB upon reception of an INIT
1092 * or INIT-ACK chunk the receiver SHOULD use not only the
1093 * source address of the packet (containing the INIT or
1094 * INIT-ACK) but the receiver SHOULD also use all valid
1095 * address parameters contained within the chunk.
1096 *
1097 * 2.18.3 Solution description
1098 *
1099 * This new text clearly specifies to an implementor the need
1100 * to look within the INIT or INIT-ACK. Any implementation that
1101 * does not do this, may not be able to establish associations
1102 * in certain circumstances.
1103 *
1104 */
Eric W. Biederman4110cc22012-08-06 08:41:13 +00001105static struct sctp_association *__sctp_rcv_init_lookup(struct net *net,
1106 struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 const union sctp_addr *laddr, struct sctp_transport **transportp)
1108{
1109 struct sctp_association *asoc;
1110 union sctp_addr addr;
1111 union sctp_addr *paddr = &addr;
Arnaldo Carvalho de Melo2c0fd382007-03-13 13:59:32 -03001112 struct sctphdr *sh = sctp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 union sctp_params params;
Xin Long01a992b2017-06-30 11:52:22 +08001114 struct sctp_init_chunk *init;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 struct sctp_af *af;
1116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 /*
1118 * This code will NOT touch anything inside the chunk--it is
1119 * strictly READ-ONLY.
1120 *
1121 * RFC 2960 3 SCTP packet Format
1122 *
1123 * Multiple chunks can be bundled into one SCTP packet up to
1124 * the MTU size, except for the INIT, INIT ACK, and SHUTDOWN
1125 * COMPLETE chunks. These chunks MUST NOT be bundled with any
1126 * other chunk in a packet. See Section 6.10 for more details
1127 * on chunk bundling.
1128 */
1129
1130 /* Find the start of the TLVs and the end of the chunk. This is
1131 * the region we search for address parameters.
1132 */
Xin Long01a992b2017-06-30 11:52:22 +08001133 init = (struct sctp_init_chunk *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135 /* Walk the parameters looking for embedded addresses. */
1136 sctp_walk_params(params, init, init_hdr.params) {
1137
1138 /* Note: Ignoring hostname addresses. */
1139 af = sctp_get_af_specific(param_type2af(params.p->type));
1140 if (!af)
1141 continue;
1142
Al Virodd86d132006-11-20 17:11:13 -08001143 af->from_addr_param(paddr, params.addr, sh->source, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Xin Long7c17fcc2016-10-31 20:32:32 +08001145 asoc = __sctp_lookup_association(net, laddr, paddr, transportp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 if (asoc)
1147 return asoc;
1148 }
1149
1150 return NULL;
1151}
1152
Vlad Yasevichdf218572007-12-20 14:10:38 -08001153/* ADD-IP, Section 5.2
1154 * When an endpoint receives an ASCONF Chunk from the remote peer
1155 * special procedures may be needed to identify the association the
1156 * ASCONF Chunk is associated with. To properly find the association
1157 * the following procedures SHOULD be followed:
1158 *
1159 * D2) If the association is not found, use the address found in the
1160 * Address Parameter TLV combined with the port number found in the
1161 * SCTP common header. If found proceed to rule D4.
1162 *
1163 * D2-ext) If more than one ASCONF Chunks are packed together, use the
1164 * address found in the ASCONF Address Parameter TLV of each of the
1165 * subsequent ASCONF Chunks. If found, proceed to rule D4.
1166 */
1167static struct sctp_association *__sctp_rcv_asconf_lookup(
Eric W. Biederman4110cc22012-08-06 08:41:13 +00001168 struct net *net,
Xin Long922dbc52017-06-30 11:52:13 +08001169 struct sctp_chunkhdr *ch,
Vlad Yasevichdf218572007-12-20 14:10:38 -08001170 const union sctp_addr *laddr,
Al Virobc92dd12008-03-17 22:47:32 -07001171 __be16 peer_port,
Vlad Yasevichdf218572007-12-20 14:10:38 -08001172 struct sctp_transport **transportp)
1173{
Xin Long68d75462017-08-03 15:42:20 +08001174 struct sctp_addip_chunk *asconf = (struct sctp_addip_chunk *)ch;
Vlad Yasevichdf218572007-12-20 14:10:38 -08001175 struct sctp_af *af;
1176 union sctp_addr_param *param;
1177 union sctp_addr paddr;
1178
1179 /* Skip over the ADDIP header and find the Address parameter */
1180 param = (union sctp_addr_param *)(asconf + 1);
1181
Shan Wei6a435732011-04-18 19:11:47 +00001182 af = sctp_get_af_specific(param_type2af(param->p.type));
Vlad Yasevichdf218572007-12-20 14:10:38 -08001183 if (unlikely(!af))
1184 return NULL;
1185
1186 af->from_addr_param(&paddr, param, peer_port, 0);
1187
Eric W. Biederman4110cc22012-08-06 08:41:13 +00001188 return __sctp_lookup_association(net, laddr, &paddr, transportp);
Vlad Yasevichdf218572007-12-20 14:10:38 -08001189}
1190
1191
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07001192/* SCTP-AUTH, Section 6.3:
1193* If the receiver does not find a STCB for a packet containing an AUTH
1194* chunk as the first chunk and not a COOKIE-ECHO chunk as the second
1195* chunk, it MUST use the chunks after the AUTH chunk to look up an existing
1196* association.
1197*
1198* This means that any chunks that can help us identify the association need
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001199* to be looked at to find this association.
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07001200*/
Eric W. Biederman4110cc22012-08-06 08:41:13 +00001201static struct sctp_association *__sctp_rcv_walk_lookup(struct net *net,
1202 struct sk_buff *skb,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07001203 const union sctp_addr *laddr,
1204 struct sctp_transport **transportp)
1205{
Vlad Yasevichdf218572007-12-20 14:10:38 -08001206 struct sctp_association *asoc = NULL;
Xin Long922dbc52017-06-30 11:52:13 +08001207 struct sctp_chunkhdr *ch;
Vlad Yasevichdf218572007-12-20 14:10:38 -08001208 int have_auth = 0;
1209 unsigned int chunk_num = 1;
1210 __u8 *ch_end;
1211
1212 /* Walk through the chunks looking for AUTH or ASCONF chunks
1213 * to help us find the association.
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07001214 */
Xin Long922dbc52017-06-30 11:52:13 +08001215 ch = (struct sctp_chunkhdr *)skb->data;
Vlad Yasevichdf218572007-12-20 14:10:38 -08001216 do {
1217 /* Break out if chunk length is less then minimal. */
Xin Long922dbc52017-06-30 11:52:13 +08001218 if (ntohs(ch->length) < sizeof(*ch))
Vlad Yasevichdf218572007-12-20 14:10:38 -08001219 break;
1220
Marcelo Ricardo Leitnere2f036a2016-09-21 08:45:55 -03001221 ch_end = ((__u8 *)ch) + SCTP_PAD4(ntohs(ch->length));
Vlad Yasevichdf218572007-12-20 14:10:38 -08001222 if (ch_end > skb_tail_pointer(skb))
1223 break;
1224
wangweidongcb3f8372013-12-23 12:16:50 +08001225 switch (ch->type) {
wangweidongf7010e62013-12-23 12:16:52 +08001226 case SCTP_CID_AUTH:
1227 have_auth = chunk_num;
1228 break;
Vlad Yasevichdf218572007-12-20 14:10:38 -08001229
wangweidongf7010e62013-12-23 12:16:52 +08001230 case SCTP_CID_COOKIE_ECHO:
1231 /* If a packet arrives containing an AUTH chunk as
1232 * a first chunk, a COOKIE-ECHO chunk as the second
1233 * chunk, and possibly more chunks after them, and
1234 * the receiver does not have an STCB for that
1235 * packet, then authentication is based on
1236 * the contents of the COOKIE- ECHO chunk.
1237 */
1238 if (have_auth == 1 && chunk_num == 2)
1239 return NULL;
1240 break;
Vlad Yasevichdf218572007-12-20 14:10:38 -08001241
wangweidongf7010e62013-12-23 12:16:52 +08001242 case SCTP_CID_ASCONF:
1243 if (have_auth || net->sctp.addip_noauth)
1244 asoc = __sctp_rcv_asconf_lookup(
1245 net, ch, laddr,
1246 sctp_hdr(skb)->source,
1247 transportp);
1248 default:
1249 break;
Vlad Yasevichdf218572007-12-20 14:10:38 -08001250 }
1251
1252 if (asoc)
1253 break;
1254
Xin Long922dbc52017-06-30 11:52:13 +08001255 ch = (struct sctp_chunkhdr *)ch_end;
Vlad Yasevichdf218572007-12-20 14:10:38 -08001256 chunk_num++;
1257 } while (ch_end < skb_tail_pointer(skb));
1258
1259 return asoc;
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07001260}
1261
1262/*
1263 * There are circumstances when we need to look inside the SCTP packet
1264 * for information to help us find the association. Examples
1265 * include looking inside of INIT/INIT-ACK chunks or after the AUTH
1266 * chunks.
1267 */
Eric W. Biederman4110cc22012-08-06 08:41:13 +00001268static struct sctp_association *__sctp_rcv_lookup_harder(struct net *net,
1269 struct sk_buff *skb,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07001270 const union sctp_addr *laddr,
1271 struct sctp_transport **transportp)
1272{
Xin Long922dbc52017-06-30 11:52:13 +08001273 struct sctp_chunkhdr *ch;
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07001274
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -03001275 /* We do not allow GSO frames here as we need to linearize and
1276 * then cannot guarantee frame boundaries. This shouldn't be an
1277 * issue as packets hitting this are mostly INIT or INIT-ACK and
1278 * those cannot be on GSO-style anyway.
1279 */
Daniel Axtens1dd27cd2018-03-09 14:06:09 +11001280 if (skb_is_gso(skb) && skb_is_gso_sctp(skb))
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -03001281 return NULL;
1282
Xin Long922dbc52017-06-30 11:52:13 +08001283 ch = (struct sctp_chunkhdr *)skb->data;
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07001284
Vlad Yasevichdf218572007-12-20 14:10:38 -08001285 /* The code below will attempt to walk the chunk and extract
1286 * parameter information. Before we do that, we need to verify
1287 * that the chunk length doesn't cause overflow. Otherwise, we'll
1288 * walk off the end.
1289 */
Marcelo Ricardo Leitnere2f036a2016-09-21 08:45:55 -03001290 if (SCTP_PAD4(ntohs(ch->length)) > skb->len)
Vlad Yasevichdf218572007-12-20 14:10:38 -08001291 return NULL;
1292
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07001293 /* If this is INIT/INIT-ACK look inside the chunk too. */
wangweidongf482f2f2013-12-19 10:02:42 +08001294 if (ch->type == SCTP_CID_INIT || ch->type == SCTP_CID_INIT_ACK)
Eric W. Biederman4110cc22012-08-06 08:41:13 +00001295 return __sctp_rcv_init_lookup(net, skb, laddr, transportp);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07001296
wangweidongf482f2f2013-12-19 10:02:42 +08001297 return __sctp_rcv_walk_lookup(net, skb, laddr, transportp);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07001298}
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300/* Lookup an association for an inbound skb. */
Eric W. Biederman4110cc22012-08-06 08:41:13 +00001301static struct sctp_association *__sctp_rcv_lookup(struct net *net,
1302 struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 const union sctp_addr *paddr,
1304 const union sctp_addr *laddr,
1305 struct sctp_transport **transportp)
1306{
1307 struct sctp_association *asoc;
1308
Eric W. Biederman4110cc22012-08-06 08:41:13 +00001309 asoc = __sctp_lookup_association(net, laddr, paddr, transportp);
Marcelo Ricardo Leitnerb77b7562016-12-28 09:51:56 -02001310 if (asoc)
1311 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 /* Further lookup for INIT/INIT-ACK packets.
1314 * SCTP Implementors Guide, 2.18 Handling of address
1315 * parameters within the INIT or INIT-ACK.
1316 */
Marcelo Ricardo Leitnerb77b7562016-12-28 09:51:56 -02001317 asoc = __sctp_rcv_lookup_harder(net, skb, laddr, transportp);
1318 if (asoc)
1319 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Marcelo Ricardo Leitnerb77b7562016-12-28 09:51:56 -02001321 if (paddr->sa.sa_family == AF_INET)
1322 pr_debug("sctp: asoc not found for src:%pI4:%d dst:%pI4:%d\n",
1323 &laddr->v4.sin_addr, ntohs(laddr->v4.sin_port),
1324 &paddr->v4.sin_addr, ntohs(paddr->v4.sin_port));
1325 else
1326 pr_debug("sctp: asoc not found for src:%pI6:%d dst:%pI6:%d\n",
1327 &laddr->v6.sin6_addr, ntohs(laddr->v6.sin6_port),
1328 &paddr->v6.sin6_addr, ntohs(paddr->v6.sin6_port));
1329
1330out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 return asoc;
1332}