blob: c0ca893ab1d1147e0c3df9318671fec9328a4b7c [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
26 * along with GNU CC; see the file COPYING. If not, write to
27 * the Free Software Foundation, 59 Temple Place - Suite 330,
28 * Boston, MA 02111-1307, USA.
29 *
30 * Please send any bug reports or fixes you make to the
31 * email address(es):
32 * lksctp developers <lksctp-developers@lists.sourceforge.net>
33 *
34 * Or submit a bug report through the following website:
35 * http://www.sf.net/projects/lksctp
36 *
37 * Written or modified by:
38 * La Monte H.P. Yarroll <piggy@acm.org>
39 * Karl Knutson <karl@athena.chicago.il.us>
40 * Xingang Guo <xingang.guo@intel.com>
41 * Jon Grimm <jgrimm@us.ibm.com>
42 * Hui Huang <hui.huang@nokia.com>
43 * Daisy Chang <daisyc@us.ibm.com>
44 * Sridhar Samudrala <sri@us.ibm.com>
45 * Ardelle Fan <ardelle.fan@intel.com>
46 *
47 * Any bugs reported given to us we will try to fix... any fixes shared will
48 * be incorporated into the next SCTP release.
49 */
50
51#include <linux/types.h>
52#include <linux/list.h> /* For struct list_head */
53#include <linux/socket.h>
54#include <linux/ip.h>
55#include <linux/time.h> /* For struct timeval */
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090056#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <net/ip.h>
58#include <net/icmp.h>
59#include <net/snmp.h>
60#include <net/sock.h>
61#include <net/xfrm.h>
62#include <net/sctp/sctp.h>
63#include <net/sctp/sm.h>
Vlad Yasevich9ad09772007-12-16 14:06:41 -080064#include <net/sctp/checksum.h>
Pavel Emelyanovdcfc23c2008-07-14 23:03:00 -070065#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67/* Forward declarations for internal helpers. */
68static int sctp_rcv_ootb(struct sk_buff *);
69static struct sctp_association *__sctp_rcv_lookup(struct sk_buff *skb,
70 const union sctp_addr *laddr,
71 const union sctp_addr *paddr,
72 struct sctp_transport **transportp);
Eric W. Biederman4cdadcb2012-08-06 08:40:21 +000073static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(struct net *net,
74 const union sctp_addr *laddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075static struct sctp_association *__sctp_lookup_association(
76 const union sctp_addr *local,
77 const union sctp_addr *peer,
78 struct sctp_transport **pt);
79
Zhu Yi50b1a782010-03-04 18:01:44 +000080static int sctp_add_backlog(struct sock *sk, struct sk_buff *skb);
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -070081
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83/* Calculate the SCTP checksum of an SCTP packet. */
84static inline int sctp_rcv_checksum(struct sk_buff *skb)
85{
Arnaldo Carvalho de Melo2c0fd382007-03-13 13:59:32 -030086 struct sctphdr *sh = sctp_hdr(skb);
Vlad Yasevich4458f042009-02-13 08:33:42 +000087 __le32 cmp = sh->checksum;
David S. Miller1b003be2009-06-09 00:22:35 -070088 struct sk_buff *list;
Vlad Yasevich4458f042009-02-13 08:33:42 +000089 __le32 val;
90 __u32 tmp = sctp_start_cksum((__u8 *)sh, skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
David S. Miller1b003be2009-06-09 00:22:35 -070092 skb_walk_frags(skb, list)
Vlad Yasevich4458f042009-02-13 08:33:42 +000093 tmp = sctp_update_cksum((__u8 *)list->data, skb_headlen(list),
94 tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Vlad Yasevich4458f042009-02-13 08:33:42 +000096 val = sctp_end_cksum(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 if (val != cmp) {
99 /* CRC failure, dump it. */
100 SCTP_INC_STATS_BH(SCTP_MIB_CHECKSUMERRORS);
101 return -1;
102 }
103 return 0;
104}
105
David S. Miller79af02c2005-07-08 21:47:49 -0700106struct sctp_input_cb {
107 union {
108 struct inet_skb_parm h4;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000109#if IS_ENABLED(CONFIG_IPV6)
David S. Miller79af02c2005-07-08 21:47:49 -0700110 struct inet6_skb_parm h6;
111#endif
112 } header;
113 struct sctp_chunk *chunk;
114};
115#define SCTP_INPUT_CB(__skb) ((struct sctp_input_cb *)&((__skb)->cb[0]))
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117/*
118 * This is the routine which IP calls when receiving an SCTP packet.
119 */
120int sctp_rcv(struct sk_buff *skb)
121{
122 struct sock *sk;
123 struct sctp_association *asoc;
124 struct sctp_endpoint *ep = NULL;
125 struct sctp_ep_common *rcvr;
126 struct sctp_transport *transport = NULL;
127 struct sctp_chunk *chunk;
128 struct sctphdr *sh;
129 union sctp_addr src;
130 union sctp_addr dest;
131 int family;
132 struct sctp_af *af;
Eric W. Biederman4cdadcb2012-08-06 08:40:21 +0000133 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 if (skb->pkt_type!=PACKET_HOST)
136 goto discard_it;
137
138 SCTP_INC_STATS_BH(SCTP_MIB_INSCTPPACKS);
139
Herbert Xu28cd7752006-10-29 23:46:42 -0800140 if (skb_linearize(skb))
141 goto discard_it;
142
Arnaldo Carvalho de Melo2c0fd382007-03-13 13:59:32 -0300143 sh = sctp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 /* Pull up the IP and SCTP headers. */
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -0700146 __skb_pull(skb, skb_transport_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (skb->len < sizeof(struct sctphdr))
148 goto discard_it;
Lucas Nussbaum06e86802009-02-13 08:33:41 +0000149 if (!sctp_checksum_disable && !skb_csum_unnecessary(skb) &&
150 sctp_rcv_checksum(skb) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 goto discard_it;
152
153 skb_pull(skb, sizeof(struct sctphdr));
154
155 /* Make sure we at least have chunk headers worth of data left. */
156 if (skb->len < sizeof(struct sctp_chunkhdr))
157 goto discard_it;
158
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700159 family = ipver2af(ip_hdr(skb)->version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 af = sctp_get_af_specific(family);
161 if (unlikely(!af))
162 goto discard_it;
163
164 /* Initialize local addresses for lookups. */
165 af->from_skb(&src, skb, 1);
166 af->from_skb(&dest, skb, 0);
167
168 /* If the packet is to or from a non-unicast address,
169 * silently discard the packet.
170 *
171 * This is not clearly defined in the RFC except in section
172 * 8.4 - OOTB handling. However, based on the book "Stream Control
173 * Transmission Protocol" 2.1, "It is important to note that the
174 * IP address of an SCTP transport address must be a routable
175 * unicast address. In other words, IP multicast addresses and
176 * IP broadcast addresses cannot be used in an SCTP transport
177 * address."
178 */
Vlad Yasevich5636bef2006-06-17 22:55:35 -0700179 if (!af->addr_valid(&src, NULL, skb) ||
180 !af->addr_valid(&dest, NULL, skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 goto discard_it;
182
Al Virod55c41b2006-11-20 17:09:40 -0800183 asoc = __sctp_rcv_lookup(skb, &src, &dest, &transport);
Al Viro1c7d1fc2006-11-20 17:08:09 -0800184
Neil Horman0fd9a652005-06-13 15:11:24 -0700185 if (!asoc)
Eric W. Biederman4cdadcb2012-08-06 08:40:21 +0000186 ep = __sctp_rcv_lookup_endpoint(net, &dest);
Neil Horman0fd9a652005-06-13 15:11:24 -0700187
188 /* Retrieve the common input handling substructure. */
189 rcvr = asoc ? &asoc->base : &ep->base;
190 sk = rcvr->sk;
191
192 /*
193 * If a frame arrives on an interface and the receiving socket is
194 * bound to another interface, via SO_BINDTODEVICE, treat it as OOTB
195 */
196 if (sk->sk_bound_dev_if && (sk->sk_bound_dev_if != af->skb_iif(skb)))
197 {
Neil Horman0fd9a652005-06-13 15:11:24 -0700198 if (asoc) {
199 sctp_association_put(asoc);
200 asoc = NULL;
201 } else {
202 sctp_endpoint_put(ep);
203 ep = NULL;
204 }
205 sk = sctp_get_ctl_sock();
206 ep = sctp_sk(sk)->ep;
207 sctp_endpoint_hold(ep);
Neil Horman0fd9a652005-06-13 15:11:24 -0700208 rcvr = &ep->base;
209 }
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 /*
212 * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
213 * An SCTP packet is called an "out of the blue" (OOTB)
214 * packet if it is correctly formed, i.e., passed the
215 * receiver's checksum check, but the receiver is not
216 * able to identify the association to which this
217 * packet belongs.
218 */
219 if (!asoc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (sctp_rcv_ootb(skb)) {
221 SCTP_INC_STATS_BH(SCTP_MIB_OUTOFBLUES);
222 goto discard_release;
223 }
224 }
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (!xfrm_policy_check(sk, XFRM_POLICY_IN, skb, family))
227 goto discard_release;
Patrick McHardyb59c2702006-01-06 23:06:10 -0800228 nf_reset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Dmitry Mishinfda9ef52006-08-31 15:28:39 -0700230 if (sk_filter(sk, skb))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900231 goto discard_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 /* Create an SCTP packet structure. */
234 chunk = sctp_chunkify(skb, asoc, sk);
Herbert Xu2babf9d2006-03-25 01:25:29 -0800235 if (!chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 goto discard_release;
David S. Miller79af02c2005-07-08 21:47:49 -0700237 SCTP_INPUT_CB(skb)->chunk = chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 /* Remember what endpoint is to handle this packet. */
240 chunk->rcvr = rcvr;
241
242 /* Remember the SCTP header. */
243 chunk->sctp_hdr = sh;
244
245 /* Set the source and destination addresses of the incoming chunk. */
Al Virod55c41b2006-11-20 17:09:40 -0800246 sctp_init_addrs(chunk, &src, &dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 /* Remember where we came from. */
249 chunk->transport = transport;
250
251 /* Acquire access to the sock lock. Note: We are safe from other
252 * bottom halves on this lock, but a user may be in the lock too,
253 * so check if it is busy.
254 */
255 sctp_bh_lock_sock(sk);
256
Vlad Yasevichae53b5b2009-01-22 14:53:23 -0800257 if (sk != rcvr->sk) {
258 /* Our cached sk is different from the rcvr->sk. This is
259 * because migrate()/accept() may have moved the association
260 * to a new socket and released all the sockets. So now we
261 * are holding a lock on the old socket while the user may
262 * be doing something with the new socket. Switch our veiw
263 * of the current sk.
264 */
265 sctp_bh_unlock_sock(sk);
266 sk = rcvr->sk;
267 sctp_bh_lock_sock(sk);
268 }
269
Sridhar Samudralaac0b0462006-08-22 00:15:33 -0700270 if (sock_owned_by_user(sk)) {
Zhu Yi50b1a782010-03-04 18:01:44 +0000271 if (sctp_add_backlog(sk, skb)) {
272 sctp_bh_unlock_sock(sk);
273 sctp_chunk_free(chunk);
274 skb = NULL; /* sctp_chunk_free already freed the skb */
275 goto discard_release;
276 }
Sridhar Samudralaac0b0462006-08-22 00:15:33 -0700277 SCTP_INC_STATS_BH(SCTP_MIB_IN_PKT_BACKLOG);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -0700278 } else {
279 SCTP_INC_STATS_BH(SCTP_MIB_IN_PKT_SOFTIRQ);
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700280 sctp_inq_push(&chunk->rcvr->inqueue, chunk);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -0700281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 sctp_bh_unlock_sock(sk);
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700284
285 /* Release the asoc/ep ref we took in the lookup calls. */
286 if (asoc)
287 sctp_association_put(asoc);
288 else
289 sctp_endpoint_put(ep);
Sridhar Samudrala7a48f922006-01-17 11:51:28 -0800290
Herbert Xu2babf9d2006-03-25 01:25:29 -0800291 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293discard_it:
Sridhar Samudralaac0b0462006-08-22 00:15:33 -0700294 SCTP_INC_STATS_BH(SCTP_MIB_IN_PKT_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 kfree_skb(skb);
Herbert Xu2babf9d2006-03-25 01:25:29 -0800296 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298discard_release:
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700299 /* Release the asoc/ep ref we took in the lookup calls. */
Neil Horman0fd9a652005-06-13 15:11:24 -0700300 if (asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 sctp_association_put(asoc);
Neil Horman0fd9a652005-06-13 15:11:24 -0700302 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 sctp_endpoint_put(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 goto discard_it;
306}
307
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700308/* Process the backlog queue of the socket. Every skb on
309 * the backlog holds a ref on an association or endpoint.
310 * We hold this ref throughout the state machine to make
311 * sure that the structure we need is still around.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 */
313int sctp_backlog_rcv(struct sock *sk, struct sk_buff *skb)
314{
David S. Miller79af02c2005-07-08 21:47:49 -0700315 struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900316 struct sctp_inq *inqueue = &chunk->rcvr->inqueue;
317 struct sctp_ep_common *rcvr = NULL;
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700318 int backloged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900320 rcvr = chunk->rcvr;
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800321
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700322 /* If the rcvr is dead then the association or endpoint
323 * has been deleted and we can safely drop the chunk
324 * and refs that we are holding.
325 */
326 if (rcvr->dead) {
327 sctp_chunk_free(chunk);
328 goto done;
329 }
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800330
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700331 if (unlikely(rcvr->sk != sk)) {
332 /* In this case, the association moved from one socket to
333 * another. We are currently sitting on the backlog of the
334 * old socket, so we need to move.
335 * However, since we are here in the process context we
336 * need to take make sure that the user doesn't own
337 * the new socket when we process the packet.
338 * If the new socket is user-owned, queue the chunk to the
339 * backlog of the new socket without dropping any refs.
340 * Otherwise, we can safely push the chunk on the inqueue.
341 */
Sridhar Samudrala7a48f922006-01-17 11:51:28 -0800342
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700343 sk = rcvr->sk;
344 sctp_bh_lock_sock(sk);
345
346 if (sock_owned_by_user(sk)) {
Eric Dumazetf545a382012-04-22 23:34:26 +0000347 if (sk_add_backlog(sk, skb, sk->sk_rcvbuf))
Zhu Yi50b1a782010-03-04 18:01:44 +0000348 sctp_chunk_free(chunk);
349 else
350 backloged = 1;
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700351 } else
352 sctp_inq_push(inqueue, chunk);
353
354 sctp_bh_unlock_sock(sk);
355
356 /* If the chunk was backloged again, don't drop refs */
357 if (backloged)
358 return 0;
359 } else {
360 sctp_inq_push(inqueue, chunk);
361 }
362
363done:
364 /* Release the refs we took in sctp_add_backlog */
365 if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
366 sctp_association_put(sctp_assoc(rcvr));
367 else if (SCTP_EP_TYPE_SOCKET == rcvr->type)
368 sctp_endpoint_put(sctp_ep(rcvr));
369 else
370 BUG();
371
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900372 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
Zhu Yi50b1a782010-03-04 18:01:44 +0000375static int sctp_add_backlog(struct sock *sk, struct sk_buff *skb)
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800376{
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700377 struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
378 struct sctp_ep_common *rcvr = chunk->rcvr;
Zhu Yi50b1a782010-03-04 18:01:44 +0000379 int ret;
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800380
Eric Dumazetf545a382012-04-22 23:34:26 +0000381 ret = sk_add_backlog(sk, skb, sk->sk_rcvbuf);
Zhu Yi50b1a782010-03-04 18:01:44 +0000382 if (!ret) {
383 /* Hold the assoc/ep while hanging on the backlog queue.
384 * This way, we know structures we need will not disappear
385 * from us
386 */
387 if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
388 sctp_association_hold(sctp_assoc(rcvr));
389 else if (SCTP_EP_TYPE_SOCKET == rcvr->type)
390 sctp_endpoint_hold(sctp_ep(rcvr));
391 else
392 BUG();
393 }
394 return ret;
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800395
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800396}
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398/* Handle icmp frag needed error. */
399void sctp_icmp_frag_needed(struct sock *sk, struct sctp_association *asoc,
400 struct sctp_transport *t, __u32 pmtu)
401{
Wei Yongjun91bd6b12008-10-23 00:59:52 -0700402 if (!t || (t->pathmtu <= pmtu))
Frank Filz52ccb8e2005-12-22 11:36:46 -0800403 return;
404
Vlad Yasevich8a479492007-06-07 14:21:05 -0400405 if (sock_owned_by_user(sk)) {
406 asoc->pmtu_pending = 1;
407 t->pmtu_pending = 1;
408 return;
409 }
410
Frank Filz52ccb8e2005-12-22 11:36:46 -0800411 if (t->param_flags & SPP_PMTUD_ENABLE) {
Vlad Yasevichc910b472007-06-07 13:47:03 -0400412 /* Update transports view of the MTU */
David S. Miller02f3d4c2012-07-16 03:57:14 -0700413 sctp_transport_update_pmtu(sk, t, pmtu);
Frank Filz52ccb8e2005-12-22 11:36:46 -0800414
415 /* Update association pmtu. */
David S. Miller02f3d4c2012-07-16 03:57:14 -0700416 sctp_assoc_sync_pmtu(sk, asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418
Frank Filz52ccb8e2005-12-22 11:36:46 -0800419 /* Retransmit with the new pmtu setting.
420 * Normally, if PMTU discovery is disabled, an ICMP Fragmentation
421 * Needed will never be sent, but if a message was sent before
422 * PMTU discovery was disabled that was larger than the PMTU, it
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900423 * would not be fragmented, so it must be re-transmitted fragmented.
Frank Filz52ccb8e2005-12-22 11:36:46 -0800424 */
425 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
433 if (!t)
434 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{
Harvey Harrison0dc47872008-03-05 20:47:47 -0800455 SCTP_DEBUG_PRINTK("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Vlad Yasevich50b5d6a2010-05-06 00:56:07 -0700457 if (sock_owned_by_user(sk)) {
458 if (timer_pending(&t->proto_unreach_timer))
459 return;
460 else {
461 if (!mod_timer(&t->proto_unreach_timer,
462 jiffies + (HZ/20)))
463 sctp_association_hold(asoc);
464 }
465
466 } else {
467 if (timer_pending(&t->proto_unreach_timer) &&
468 del_timer(&t->proto_unreach_timer))
469 sctp_association_put(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Vlad Yasevich50b5d6a2010-05-06 00:56:07 -0700471 sctp_do_sm(SCTP_EVENT_T_OTHER,
472 SCTP_ST_OTHER(SCTP_EVENT_ICMP_PROTO_UNREACH),
473 asoc->state, asoc->ep, asoc, t,
474 GFP_ATOMIC);
475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
478/* Common lookup code for icmp/icmpv6 error handler. */
479struct sock *sctp_err_lookup(int family, struct sk_buff *skb,
480 struct sctphdr *sctphdr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 struct sctp_association **app,
482 struct sctp_transport **tpp)
483{
484 union sctp_addr saddr;
485 union sctp_addr daddr;
486 struct sctp_af *af;
487 struct sock *sk = NULL;
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700488 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 struct sctp_transport *transport = NULL;
Wei Yongjun7115e632008-06-19 16:07:48 -0700490 struct sctp_init_chunk *chunkhdr;
491 __u32 vtag = ntohl(sctphdr->vtag);
492 int len = skb->len - ((void *)sctphdr - (void *)skb->data);
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 */
Al Virod55c41b2006-11-20 17:09:40 -0800508 asoc = __sctp_lookup_association(&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) {
Joe Perchesea110732011-06-13 16:21:26 +0000527 chunkhdr = (void *)sctphdr + sizeof(struct sctphdr);
Wei Yongjun7115e632008-06-19 16:07:48 -0700528 if (len < sizeof(struct sctphdr) + sizeof(sctp_chunkhdr_t)
529 + sizeof(__be32) ||
530 chunkhdr->chunk_hdr.type != SCTP_CID_INIT ||
531 ntohl(chunkhdr->init_hdr.init_tag) != asoc->c.my_vtag) {
532 goto out;
533 }
534 } else if (vtag != asoc->c.peer_vtag) {
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700535 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 sctp_bh_lock_sock(sk);
539
540 /* If too many ICMPs get dropped on busy
541 * servers this needs to be solved differently.
542 */
543 if (sock_owned_by_user(sk))
Pavel Emelyanovde0744a2008-07-16 20:31:16 -0700544 NET_INC_STATS_BH(&init_net, LINUX_MIB_LOCKDROPPEDICMPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 *app = asoc;
547 *tpp = transport;
548 return sk;
549
550out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (asoc)
552 sctp_association_put(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return NULL;
554}
555
556/* Common cleanup code for icmp/icmpv6 error handler. */
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700557void sctp_err_finish(struct sock *sk, struct sctp_association *asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
559 sctp_bh_unlock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (asoc)
561 sctp_association_put(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
564/*
565 * This routine is called by the ICMP module when it gets some
566 * sort of error condition. If err < 0 then the socket should
567 * be closed and the error returned to the user. If err > 0
568 * it's just the icmp type << 8 | icmp code. After adjustment
569 * header points to the first 8 bytes of the sctp header. We need
570 * to find the appropriate port.
571 *
572 * The locking strategy used here is very "optimistic". When
573 * someone else accesses the socket the ICMP is just dropped
574 * and for some paths there is no check at all.
575 * A more general error queue to queue errors for later handling
576 * is probably better.
577 *
578 */
579void sctp_v4_err(struct sk_buff *skb, __u32 info)
580{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000581 const struct iphdr *iph = (const struct iphdr *)skb->data;
Arnaldo Carvalho de Meloa27ef742007-03-13 17:17:10 -0300582 const int ihlen = iph->ihl * 4;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300583 const int type = icmp_hdr(skb)->type;
584 const int code = icmp_hdr(skb)->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 struct sock *sk;
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700586 struct sctp_association *asoc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 struct sctp_transport *transport;
588 struct inet_sock *inet;
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700589 sk_buff_data_t saveip, savesctp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 int err;
591
Arnaldo Carvalho de Meloa27ef742007-03-13 17:17:10 -0300592 if (skb->len < ihlen + 8) {
Pavel Emelyanovdcfc23c2008-07-14 23:03:00 -0700593 ICMP_INC_STATS_BH(&init_net, ICMP_MIB_INERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return;
595 }
596
597 /* Fix up skb to look at the embedded net header. */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700598 saveip = skb->network_header;
599 savesctp = skb->transport_header;
Arnaldo Carvalho de Melo31c77112007-03-10 19:04:55 -0300600 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloa27ef742007-03-13 17:17:10 -0300601 skb_set_transport_header(skb, ihlen);
602 sk = sctp_err_lookup(AF_INET, skb, sctp_hdr(skb), &asoc, &transport);
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700603 /* Put back, the original values. */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700604 skb->network_header = saveip;
605 skb->transport_header = savesctp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 if (!sk) {
Pavel Emelyanovdcfc23c2008-07-14 23:03:00 -0700607 ICMP_INC_STATS_BH(&init_net, ICMP_MIB_INERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return;
609 }
610 /* Warning: The sock lock is held. Remember to call
611 * sctp_err_finish!
612 */
613
614 switch (type) {
615 case ICMP_PARAMETERPROB:
616 err = EPROTO;
617 break;
618 case ICMP_DEST_UNREACH:
619 if (code > NR_ICMP_UNREACH)
620 goto out_unlock;
621
622 /* PMTU discovery (RFC1191) */
623 if (ICMP_FRAG_NEEDED == code) {
624 sctp_icmp_frag_needed(sk, asoc, transport, info);
625 goto out_unlock;
626 }
627 else {
628 if (ICMP_PROT_UNREACH == code) {
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700629 sctp_icmp_proto_unreachable(sk, asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 transport);
631 goto out_unlock;
632 }
633 }
634 err = icmp_err_convert[code].errno;
635 break;
636 case ICMP_TIME_EXCEEDED:
637 /* Ignore any time exceeded errors due to fragment reassembly
638 * timeouts.
639 */
640 if (ICMP_EXC_FRAGTIME == code)
641 goto out_unlock;
642
643 err = EHOSTUNREACH;
644 break;
David S. Miller55be7a92012-07-11 21:27:49 -0700645 case ICMP_REDIRECT:
646 sctp_icmp_redirect(sk, transport, skb);
647 err = 0;
648 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 default:
650 goto out_unlock;
651 }
652
653 inet = inet_sk(sk);
654 if (!sock_owned_by_user(sk) && inet->recverr) {
655 sk->sk_err = err;
656 sk->sk_error_report(sk);
657 } else { /* Only an error on timeout */
658 sk->sk_err_soft = err;
659 }
660
661out_unlock:
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700662 sctp_err_finish(sk, asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
664
665/*
666 * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
667 *
668 * This function scans all the chunks in the OOTB packet to determine if
669 * the packet should be discarded right away. If a response might be needed
670 * for this packet, or, if further processing is possible, the packet will
671 * be queued to a proper inqueue for the next phase of handling.
672 *
673 * Output:
674 * Return 0 - If further processing is needed.
675 * Return 1 - If the packet can be discarded right away.
676 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200677static int sctp_rcv_ootb(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
679 sctp_chunkhdr_t *ch;
680 __u8 *ch_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 ch = (sctp_chunkhdr_t *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684 /* Scan through all the chunks in the packet. */
Tsutomu Fujiia7d1f1b2006-01-17 11:57:09 -0800685 do {
686 /* Break out if chunk length is less then minimal. */
687 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
688 break;
689
690 ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700691 if (ch_end > skb_tail_pointer(skb))
Tsutomu Fujiia7d1f1b2006-01-17 11:57:09 -0800692 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 /* RFC 8.4, 2) If the OOTB packet contains an ABORT chunk, the
695 * receiver MUST silently discard the OOTB packet and take no
696 * further action.
697 */
698 if (SCTP_CID_ABORT == ch->type)
699 goto discard;
700
701 /* RFC 8.4, 6) If the packet contains a SHUTDOWN COMPLETE
702 * chunk, the receiver should silently discard the packet
703 * and take no further action.
704 */
705 if (SCTP_CID_SHUTDOWN_COMPLETE == ch->type)
706 goto discard;
707
Vlad Yasevich3c77f962007-09-17 15:14:28 -0400708 /* RFC 4460, 2.11.2
709 * This will discard packets with INIT chunk bundled as
710 * subsequent chunks in the packet. When INIT is first,
711 * the normal INIT processing will discard the chunk.
712 */
713 if (SCTP_CID_INIT == ch->type && (void *)ch != skb->data)
714 goto discard;
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 ch = (sctp_chunkhdr_t *) ch_end;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700717 } while (ch_end < skb_tail_pointer(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 return 0;
720
721discard:
722 return 1;
723}
724
725/* Insert endpoint into the hash table. */
726static void __sctp_hash_endpoint(struct sctp_endpoint *ep)
727{
Eric W. Biederman4cdadcb2012-08-06 08:40:21 +0000728 struct net *net = sock_net(ep->base.sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 struct sctp_ep_common *epb;
730 struct sctp_hashbucket *head;
731
732 epb = &ep->base;
733
Eric W. Biederman4cdadcb2012-08-06 08:40:21 +0000734 epb->hashent = sctp_ep_hashfn(net, epb->bind_addr.port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 head = &sctp_ep_hashtable[epb->hashent];
736
737 sctp_write_lock(&head->lock);
Vlad Yasevichd970dbf2007-11-09 11:43:40 -0500738 hlist_add_head(&epb->node, &head->chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 sctp_write_unlock(&head->lock);
740}
741
742/* Add an endpoint to the hash. Local BH-safe. */
743void sctp_hash_endpoint(struct sctp_endpoint *ep)
744{
745 sctp_local_bh_disable();
746 __sctp_hash_endpoint(ep);
747 sctp_local_bh_enable();
748}
749
750/* Remove endpoint from the hash table. */
751static void __sctp_unhash_endpoint(struct sctp_endpoint *ep)
752{
Eric W. Biederman4cdadcb2012-08-06 08:40:21 +0000753 struct net *net = sock_net(ep->base.sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 struct sctp_hashbucket *head;
755 struct sctp_ep_common *epb;
756
757 epb = &ep->base;
758
Eric W. Biederman4cdadcb2012-08-06 08:40:21 +0000759 epb->hashent = sctp_ep_hashfn(net, epb->bind_addr.port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 head = &sctp_ep_hashtable[epb->hashent];
762
763 sctp_write_lock(&head->lock);
Neil Horman2eebc1e2012-07-16 09:13:51 +0000764 hlist_del_init(&epb->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 sctp_write_unlock(&head->lock);
766}
767
768/* Remove endpoint from the hash. Local BH-safe. */
769void sctp_unhash_endpoint(struct sctp_endpoint *ep)
770{
771 sctp_local_bh_disable();
772 __sctp_unhash_endpoint(ep);
773 sctp_local_bh_enable();
774}
775
776/* Look up an endpoint. */
Eric W. Biederman4cdadcb2012-08-06 08:40:21 +0000777static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(struct net *net,
778 const union sctp_addr *laddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
780 struct sctp_hashbucket *head;
781 struct sctp_ep_common *epb;
782 struct sctp_endpoint *ep;
Vlad Yasevichd970dbf2007-11-09 11:43:40 -0500783 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 int hash;
785
Eric W. Biederman4cdadcb2012-08-06 08:40:21 +0000786 hash = sctp_ep_hashfn(net, ntohs(laddr->v4.sin_port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 head = &sctp_ep_hashtable[hash];
788 read_lock(&head->lock);
Vlad Yasevichd970dbf2007-11-09 11:43:40 -0500789 sctp_for_each_hentry(epb, node, &head->chain) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 ep = sctp_ep(epb);
Eric W. Biederman4cdadcb2012-08-06 08:40:21 +0000791 if (sctp_endpoint_is_match(ep, net, laddr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 goto hit;
793 }
794
795 ep = sctp_sk((sctp_get_ctl_sock()))->ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797hit:
798 sctp_endpoint_hold(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 read_unlock(&head->lock);
800 return ep;
801}
802
803/* Insert association into the hash table. */
804static void __sctp_hash_established(struct sctp_association *asoc)
805{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 struct sctp_ep_common *epb;
807 struct sctp_hashbucket *head;
808
809 epb = &asoc->base;
810
811 /* Calculate which chain this entry will belong to. */
812 epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port, asoc->peer.port);
813
814 head = &sctp_assoc_hashtable[epb->hashent];
815
816 sctp_write_lock(&head->lock);
Vlad Yasevichd970dbf2007-11-09 11:43:40 -0500817 hlist_add_head(&epb->node, &head->chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 sctp_write_unlock(&head->lock);
819}
820
821/* Add an association to the hash. Local BH-safe. */
822void sctp_hash_established(struct sctp_association *asoc)
823{
Vlad Yasevichde76e692006-10-30 18:55:11 -0800824 if (asoc->temp)
825 return;
826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 sctp_local_bh_disable();
828 __sctp_hash_established(asoc);
829 sctp_local_bh_enable();
830}
831
832/* Remove association from the hash table. */
833static void __sctp_unhash_established(struct sctp_association *asoc)
834{
835 struct sctp_hashbucket *head;
836 struct sctp_ep_common *epb;
837
838 epb = &asoc->base;
839
840 epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port,
841 asoc->peer.port);
842
843 head = &sctp_assoc_hashtable[epb->hashent];
844
845 sctp_write_lock(&head->lock);
Neil Horman2eebc1e2012-07-16 09:13:51 +0000846 hlist_del_init(&epb->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 sctp_write_unlock(&head->lock);
848}
849
850/* Remove association from the hash table. Local BH-safe. */
851void sctp_unhash_established(struct sctp_association *asoc)
852{
Vlad Yasevichde76e692006-10-30 18:55:11 -0800853 if (asoc->temp)
854 return;
855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 sctp_local_bh_disable();
857 __sctp_unhash_established(asoc);
858 sctp_local_bh_enable();
859}
860
861/* Look up an association. */
862static struct sctp_association *__sctp_lookup_association(
863 const union sctp_addr *local,
864 const union sctp_addr *peer,
865 struct sctp_transport **pt)
866{
867 struct sctp_hashbucket *head;
868 struct sctp_ep_common *epb;
869 struct sctp_association *asoc;
870 struct sctp_transport *transport;
Vlad Yasevichd970dbf2007-11-09 11:43:40 -0500871 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 int hash;
873
874 /* Optimize here for direct hit, only listening connections can
875 * have wildcards anyways.
876 */
Al Viroe2fcced2006-11-20 17:08:41 -0800877 hash = sctp_assoc_hashfn(ntohs(local->v4.sin_port), ntohs(peer->v4.sin_port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 head = &sctp_assoc_hashtable[hash];
879 read_lock(&head->lock);
Vlad Yasevichd970dbf2007-11-09 11:43:40 -0500880 sctp_for_each_hentry(epb, node, &head->chain) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 asoc = sctp_assoc(epb);
882 transport = sctp_assoc_is_match(asoc, local, peer);
883 if (transport)
884 goto hit;
885 }
886
887 read_unlock(&head->lock);
888
889 return NULL;
890
891hit:
892 *pt = transport;
893 sctp_association_hold(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 read_unlock(&head->lock);
895 return asoc;
896}
897
898/* Look up an association. BH-safe. */
899SCTP_STATIC
900struct sctp_association *sctp_lookup_association(const union sctp_addr *laddr,
901 const union sctp_addr *paddr,
902 struct sctp_transport **transportp)
903{
904 struct sctp_association *asoc;
905
906 sctp_local_bh_disable();
907 asoc = __sctp_lookup_association(laddr, paddr, transportp);
908 sctp_local_bh_enable();
909
910 return asoc;
911}
912
913/* Is there an association matching the given local and peer addresses? */
914int sctp_has_association(const union sctp_addr *laddr,
915 const union sctp_addr *paddr)
916{
917 struct sctp_association *asoc;
918 struct sctp_transport *transport;
919
Al Viro6c7be552006-11-20 17:11:50 -0800920 if ((asoc = sctp_lookup_association(laddr, paddr, &transport))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 sctp_association_put(asoc);
922 return 1;
923 }
924
925 return 0;
926}
927
928/*
929 * SCTP Implementors Guide, 2.18 Handling of address
930 * parameters within the INIT or INIT-ACK.
931 *
932 * D) When searching for a matching TCB upon reception of an INIT
933 * or INIT-ACK chunk the receiver SHOULD use not only the
934 * source address of the packet (containing the INIT or
935 * INIT-ACK) but the receiver SHOULD also use all valid
936 * address parameters contained within the chunk.
937 *
938 * 2.18.3 Solution description
939 *
940 * This new text clearly specifies to an implementor the need
941 * to look within the INIT or INIT-ACK. Any implementation that
942 * does not do this, may not be able to establish associations
943 * in certain circumstances.
944 *
945 */
946static struct sctp_association *__sctp_rcv_init_lookup(struct sk_buff *skb,
947 const union sctp_addr *laddr, struct sctp_transport **transportp)
948{
949 struct sctp_association *asoc;
950 union sctp_addr addr;
951 union sctp_addr *paddr = &addr;
Arnaldo Carvalho de Melo2c0fd382007-03-13 13:59:32 -0300952 struct sctphdr *sh = sctp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 union sctp_params params;
954 sctp_init_chunk_t *init;
955 struct sctp_transport *transport;
956 struct sctp_af *af;
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 /*
959 * This code will NOT touch anything inside the chunk--it is
960 * strictly READ-ONLY.
961 *
962 * RFC 2960 3 SCTP packet Format
963 *
964 * Multiple chunks can be bundled into one SCTP packet up to
965 * the MTU size, except for the INIT, INIT ACK, and SHUTDOWN
966 * COMPLETE chunks. These chunks MUST NOT be bundled with any
967 * other chunk in a packet. See Section 6.10 for more details
968 * on chunk bundling.
969 */
970
971 /* Find the start of the TLVs and the end of the chunk. This is
972 * the region we search for address parameters.
973 */
974 init = (sctp_init_chunk_t *)skb->data;
975
976 /* Walk the parameters looking for embedded addresses. */
977 sctp_walk_params(params, init, init_hdr.params) {
978
979 /* Note: Ignoring hostname addresses. */
980 af = sctp_get_af_specific(param_type2af(params.p->type));
981 if (!af)
982 continue;
983
Al Virodd86d132006-11-20 17:11:13 -0800984 af->from_addr_param(paddr, params.addr, sh->source, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Al Virodd86d132006-11-20 17:11:13 -0800986 asoc = __sctp_lookup_association(laddr, paddr, &transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 if (asoc)
988 return asoc;
989 }
990
991 return NULL;
992}
993
Vlad Yasevichdf218572007-12-20 14:10:38 -0800994/* ADD-IP, Section 5.2
995 * When an endpoint receives an ASCONF Chunk from the remote peer
996 * special procedures may be needed to identify the association the
997 * ASCONF Chunk is associated with. To properly find the association
998 * the following procedures SHOULD be followed:
999 *
1000 * D2) If the association is not found, use the address found in the
1001 * Address Parameter TLV combined with the port number found in the
1002 * SCTP common header. If found proceed to rule D4.
1003 *
1004 * D2-ext) If more than one ASCONF Chunks are packed together, use the
1005 * address found in the ASCONF Address Parameter TLV of each of the
1006 * subsequent ASCONF Chunks. If found, proceed to rule D4.
1007 */
1008static struct sctp_association *__sctp_rcv_asconf_lookup(
1009 sctp_chunkhdr_t *ch,
1010 const union sctp_addr *laddr,
Al Virobc92dd12008-03-17 22:47:32 -07001011 __be16 peer_port,
Vlad Yasevichdf218572007-12-20 14:10:38 -08001012 struct sctp_transport **transportp)
1013{
1014 sctp_addip_chunk_t *asconf = (struct sctp_addip_chunk *)ch;
1015 struct sctp_af *af;
1016 union sctp_addr_param *param;
1017 union sctp_addr paddr;
1018
1019 /* Skip over the ADDIP header and find the Address parameter */
1020 param = (union sctp_addr_param *)(asconf + 1);
1021
Shan Wei6a435732011-04-18 19:11:47 +00001022 af = sctp_get_af_specific(param_type2af(param->p.type));
Vlad Yasevichdf218572007-12-20 14:10:38 -08001023 if (unlikely(!af))
1024 return NULL;
1025
1026 af->from_addr_param(&paddr, param, peer_port, 0);
1027
1028 return __sctp_lookup_association(laddr, &paddr, transportp);
1029}
1030
1031
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07001032/* SCTP-AUTH, Section 6.3:
1033* If the receiver does not find a STCB for a packet containing an AUTH
1034* chunk as the first chunk and not a COOKIE-ECHO chunk as the second
1035* chunk, it MUST use the chunks after the AUTH chunk to look up an existing
1036* association.
1037*
1038* This means that any chunks that can help us identify the association need
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001039* to be looked at to find this association.
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07001040*/
Vlad Yasevichdf218572007-12-20 14:10:38 -08001041static struct sctp_association *__sctp_rcv_walk_lookup(struct sk_buff *skb,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07001042 const union sctp_addr *laddr,
1043 struct sctp_transport **transportp)
1044{
Vlad Yasevichdf218572007-12-20 14:10:38 -08001045 struct sctp_association *asoc = NULL;
1046 sctp_chunkhdr_t *ch;
1047 int have_auth = 0;
1048 unsigned int chunk_num = 1;
1049 __u8 *ch_end;
1050
1051 /* Walk through the chunks looking for AUTH or ASCONF chunks
1052 * to help us find the association.
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07001053 */
Vlad Yasevichdf218572007-12-20 14:10:38 -08001054 ch = (sctp_chunkhdr_t *) skb->data;
1055 do {
1056 /* Break out if chunk length is less then minimal. */
1057 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
1058 break;
1059
1060 ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
1061 if (ch_end > skb_tail_pointer(skb))
1062 break;
1063
1064 switch(ch->type) {
1065 case SCTP_CID_AUTH:
1066 have_auth = chunk_num;
1067 break;
1068
1069 case SCTP_CID_COOKIE_ECHO:
1070 /* If a packet arrives containing an AUTH chunk as
1071 * a first chunk, a COOKIE-ECHO chunk as the second
1072 * chunk, and possibly more chunks after them, and
1073 * the receiver does not have an STCB for that
1074 * packet, then authentication is based on
1075 * the contents of the COOKIE- ECHO chunk.
1076 */
1077 if (have_auth == 1 && chunk_num == 2)
1078 return NULL;
1079 break;
1080
1081 case SCTP_CID_ASCONF:
1082 if (have_auth || sctp_addip_noauth)
1083 asoc = __sctp_rcv_asconf_lookup(ch, laddr,
1084 sctp_hdr(skb)->source,
1085 transportp);
1086 default:
1087 break;
1088 }
1089
1090 if (asoc)
1091 break;
1092
1093 ch = (sctp_chunkhdr_t *) ch_end;
1094 chunk_num++;
1095 } while (ch_end < skb_tail_pointer(skb));
1096
1097 return asoc;
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07001098}
1099
1100/*
1101 * There are circumstances when we need to look inside the SCTP packet
1102 * for information to help us find the association. Examples
1103 * include looking inside of INIT/INIT-ACK chunks or after the AUTH
1104 * chunks.
1105 */
1106static struct sctp_association *__sctp_rcv_lookup_harder(struct sk_buff *skb,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07001107 const union sctp_addr *laddr,
1108 struct sctp_transport **transportp)
1109{
1110 sctp_chunkhdr_t *ch;
1111
1112 ch = (sctp_chunkhdr_t *) skb->data;
1113
Vlad Yasevichdf218572007-12-20 14:10:38 -08001114 /* The code below will attempt to walk the chunk and extract
1115 * parameter information. Before we do that, we need to verify
1116 * that the chunk length doesn't cause overflow. Otherwise, we'll
1117 * walk off the end.
1118 */
1119 if (WORD_ROUND(ntohs(ch->length)) > skb->len)
1120 return NULL;
1121
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07001122 /* If this is INIT/INIT-ACK look inside the chunk too. */
1123 switch (ch->type) {
1124 case SCTP_CID_INIT:
1125 case SCTP_CID_INIT_ACK:
1126 return __sctp_rcv_init_lookup(skb, laddr, transportp);
1127 break;
1128
Vlad Yasevichdf218572007-12-20 14:10:38 -08001129 default:
1130 return __sctp_rcv_walk_lookup(skb, laddr, transportp);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07001131 break;
1132 }
1133
Vlad Yasevichdf218572007-12-20 14:10:38 -08001134
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07001135 return NULL;
1136}
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138/* Lookup an association for an inbound skb. */
1139static struct sctp_association *__sctp_rcv_lookup(struct sk_buff *skb,
1140 const union sctp_addr *paddr,
1141 const union sctp_addr *laddr,
1142 struct sctp_transport **transportp)
1143{
1144 struct sctp_association *asoc;
1145
1146 asoc = __sctp_lookup_association(laddr, paddr, transportp);
1147
1148 /* Further lookup for INIT/INIT-ACK packets.
1149 * SCTP Implementors Guide, 2.18 Handling of address
1150 * parameters within the INIT or INIT-ACK.
1151 */
1152 if (!asoc)
Vlad Yasevichdf218572007-12-20 14:10:38 -08001153 asoc = __sctp_rcv_lookup_harder(skb, laddr, transportp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
1155 return asoc;
1156}