blob: 693fd0804810b37462d76af1b764615a7580852c [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 */
56#include <net/ip.h>
57#include <net/icmp.h>
58#include <net/snmp.h>
59#include <net/sock.h>
60#include <net/xfrm.h>
61#include <net/sctp/sctp.h>
62#include <net/sctp/sm.h>
Vlad Yasevich9ad09772007-12-16 14:06:41 -080063#include <net/sctp/checksum.h>
Pavel Emelyanovdcfc23c2008-07-14 23:03:00 -070064#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66/* Forward declarations for internal helpers. */
67static int sctp_rcv_ootb(struct sk_buff *);
68static struct sctp_association *__sctp_rcv_lookup(struct sk_buff *skb,
69 const union sctp_addr *laddr,
70 const union sctp_addr *paddr,
71 struct sctp_transport **transportp);
72static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(const union sctp_addr *laddr);
73static struct sctp_association *__sctp_lookup_association(
74 const union sctp_addr *local,
75 const union sctp_addr *peer,
76 struct sctp_transport **pt);
77
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -070078static void sctp_add_backlog(struct sock *sk, struct sk_buff *skb);
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81/* Calculate the SCTP checksum of an SCTP packet. */
82static inline int sctp_rcv_checksum(struct sk_buff *skb)
83{
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 struct sk_buff *list = skb_shinfo(skb)->frag_list;
Arnaldo Carvalho de Melo2c0fd382007-03-13 13:59:32 -030085 struct sctphdr *sh = sctp_hdr(skb);
Harvey Harrison336d3262008-07-18 23:07:09 -070086 __be32 cmp = sh->checksum;
87 __be32 val = sctp_start_cksum((__u8 *)sh, skb_headlen(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 for (; list; list = list->next)
90 val = sctp_update_cksum((__u8 *)list->data, skb_headlen(list),
91 val);
92
93 val = sctp_end_cksum(val);
94
95 if (val != cmp) {
96 /* CRC failure, dump it. */
97 SCTP_INC_STATS_BH(SCTP_MIB_CHECKSUMERRORS);
98 return -1;
99 }
100 return 0;
101}
102
David S. Miller79af02c2005-07-08 21:47:49 -0700103struct sctp_input_cb {
104 union {
105 struct inet_skb_parm h4;
106#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
107 struct inet6_skb_parm h6;
108#endif
109 } header;
110 struct sctp_chunk *chunk;
111};
112#define SCTP_INPUT_CB(__skb) ((struct sctp_input_cb *)&((__skb)->cb[0]))
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114/*
115 * This is the routine which IP calls when receiving an SCTP packet.
116 */
117int sctp_rcv(struct sk_buff *skb)
118{
119 struct sock *sk;
120 struct sctp_association *asoc;
121 struct sctp_endpoint *ep = NULL;
122 struct sctp_ep_common *rcvr;
123 struct sctp_transport *transport = NULL;
124 struct sctp_chunk *chunk;
125 struct sctphdr *sh;
126 union sctp_addr src;
127 union sctp_addr dest;
128 int family;
129 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 if (skb->pkt_type!=PACKET_HOST)
132 goto discard_it;
133
134 SCTP_INC_STATS_BH(SCTP_MIB_INSCTPPACKS);
135
Herbert Xu28cd7752006-10-29 23:46:42 -0800136 if (skb_linearize(skb))
137 goto discard_it;
138
Arnaldo Carvalho de Melo2c0fd382007-03-13 13:59:32 -0300139 sh = sctp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 /* Pull up the IP and SCTP headers. */
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -0700142 __skb_pull(skb, skb_transport_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 if (skb->len < sizeof(struct sctphdr))
144 goto discard_it;
Lucas Nussbaum06e86802009-02-13 08:33:41 +0000145 if (!sctp_checksum_disable && !skb_csum_unnecessary(skb) &&
146 sctp_rcv_checksum(skb) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 goto discard_it;
148
149 skb_pull(skb, sizeof(struct sctphdr));
150
151 /* Make sure we at least have chunk headers worth of data left. */
152 if (skb->len < sizeof(struct sctp_chunkhdr))
153 goto discard_it;
154
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700155 family = ipver2af(ip_hdr(skb)->version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 af = sctp_get_af_specific(family);
157 if (unlikely(!af))
158 goto discard_it;
159
160 /* Initialize local addresses for lookups. */
161 af->from_skb(&src, skb, 1);
162 af->from_skb(&dest, skb, 0);
163
164 /* If the packet is to or from a non-unicast address,
165 * silently discard the packet.
166 *
167 * This is not clearly defined in the RFC except in section
168 * 8.4 - OOTB handling. However, based on the book "Stream Control
169 * Transmission Protocol" 2.1, "It is important to note that the
170 * IP address of an SCTP transport address must be a routable
171 * unicast address. In other words, IP multicast addresses and
172 * IP broadcast addresses cannot be used in an SCTP transport
173 * address."
174 */
Vlad Yasevich5636bef2006-06-17 22:55:35 -0700175 if (!af->addr_valid(&src, NULL, skb) ||
176 !af->addr_valid(&dest, NULL, skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 goto discard_it;
178
Al Virod55c41b2006-11-20 17:09:40 -0800179 asoc = __sctp_rcv_lookup(skb, &src, &dest, &transport);
Al Viro1c7d1fc2006-11-20 17:08:09 -0800180
Neil Horman0fd9a652005-06-13 15:11:24 -0700181 if (!asoc)
Al Virod55c41b2006-11-20 17:09:40 -0800182 ep = __sctp_rcv_lookup_endpoint(&dest);
Neil Horman0fd9a652005-06-13 15:11:24 -0700183
184 /* Retrieve the common input handling substructure. */
185 rcvr = asoc ? &asoc->base : &ep->base;
186 sk = rcvr->sk;
187
188 /*
189 * If a frame arrives on an interface and the receiving socket is
190 * bound to another interface, via SO_BINDTODEVICE, treat it as OOTB
191 */
192 if (sk->sk_bound_dev_if && (sk->sk_bound_dev_if != af->skb_iif(skb)))
193 {
Neil Horman0fd9a652005-06-13 15:11:24 -0700194 if (asoc) {
195 sctp_association_put(asoc);
196 asoc = NULL;
197 } else {
198 sctp_endpoint_put(ep);
199 ep = NULL;
200 }
201 sk = sctp_get_ctl_sock();
202 ep = sctp_sk(sk)->ep;
203 sctp_endpoint_hold(ep);
Neil Horman0fd9a652005-06-13 15:11:24 -0700204 rcvr = &ep->base;
205 }
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 /*
208 * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
209 * An SCTP packet is called an "out of the blue" (OOTB)
210 * packet if it is correctly formed, i.e., passed the
211 * receiver's checksum check, but the receiver is not
212 * able to identify the association to which this
213 * packet belongs.
214 */
215 if (!asoc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 if (sctp_rcv_ootb(skb)) {
217 SCTP_INC_STATS_BH(SCTP_MIB_OUTOFBLUES);
218 goto discard_release;
219 }
220 }
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 if (!xfrm_policy_check(sk, XFRM_POLICY_IN, skb, family))
223 goto discard_release;
Patrick McHardyb59c2702006-01-06 23:06:10 -0800224 nf_reset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Dmitry Mishinfda9ef52006-08-31 15:28:39 -0700226 if (sk_filter(sk, skb))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900227 goto discard_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 /* Create an SCTP packet structure. */
230 chunk = sctp_chunkify(skb, asoc, sk);
Herbert Xu2babf9d2006-03-25 01:25:29 -0800231 if (!chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 goto discard_release;
David S. Miller79af02c2005-07-08 21:47:49 -0700233 SCTP_INPUT_CB(skb)->chunk = chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 /* Remember what endpoint is to handle this packet. */
236 chunk->rcvr = rcvr;
237
238 /* Remember the SCTP header. */
239 chunk->sctp_hdr = sh;
240
241 /* Set the source and destination addresses of the incoming chunk. */
Al Virod55c41b2006-11-20 17:09:40 -0800242 sctp_init_addrs(chunk, &src, &dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 /* Remember where we came from. */
245 chunk->transport = transport;
246
247 /* Acquire access to the sock lock. Note: We are safe from other
248 * bottom halves on this lock, but a user may be in the lock too,
249 * so check if it is busy.
250 */
251 sctp_bh_lock_sock(sk);
252
Vlad Yasevichae53b5b2009-01-22 14:53:23 -0800253 if (sk != rcvr->sk) {
254 /* Our cached sk is different from the rcvr->sk. This is
255 * because migrate()/accept() may have moved the association
256 * to a new socket and released all the sockets. So now we
257 * are holding a lock on the old socket while the user may
258 * be doing something with the new socket. Switch our veiw
259 * of the current sk.
260 */
261 sctp_bh_unlock_sock(sk);
262 sk = rcvr->sk;
263 sctp_bh_lock_sock(sk);
264 }
265
Sridhar Samudralaac0b0462006-08-22 00:15:33 -0700266 if (sock_owned_by_user(sk)) {
267 SCTP_INC_STATS_BH(SCTP_MIB_IN_PKT_BACKLOG);
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700268 sctp_add_backlog(sk, skb);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -0700269 } else {
270 SCTP_INC_STATS_BH(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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 sctp_bh_unlock_sock(sk);
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700275
276 /* Release the asoc/ep ref we took in the lookup calls. */
277 if (asoc)
278 sctp_association_put(asoc);
279 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:
Sridhar Samudralaac0b0462006-08-22 00:15:33 -0700285 SCTP_INC_STATS_BH(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. */
Neil Horman0fd9a652005-06-13 15:11:24 -0700291 if (asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 sctp_association_put(asoc);
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;
308 struct sctp_ep_common *rcvr = NULL;
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700309 int backloged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900311 rcvr = chunk->rcvr;
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800312
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700313 /* If the rcvr is dead then the association or endpoint
314 * has been deleted and we can safely drop the chunk
315 * and refs that we are holding.
316 */
317 if (rcvr->dead) {
318 sctp_chunk_free(chunk);
319 goto done;
320 }
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800321
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700322 if (unlikely(rcvr->sk != sk)) {
323 /* In this case, the association moved from one socket to
324 * another. We are currently sitting on the backlog of the
325 * old socket, so we need to move.
326 * However, since we are here in the process context we
327 * need to take make sure that the user doesn't own
328 * the new socket when we process the packet.
329 * If the new socket is user-owned, queue the chunk to the
330 * backlog of the new socket without dropping any refs.
331 * Otherwise, we can safely push the chunk on the inqueue.
332 */
Sridhar Samudrala7a48f922006-01-17 11:51:28 -0800333
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700334 sk = rcvr->sk;
335 sctp_bh_lock_sock(sk);
336
337 if (sock_owned_by_user(sk)) {
338 sk_add_backlog(sk, skb);
339 backloged = 1;
340 } else
341 sctp_inq_push(inqueue, chunk);
342
343 sctp_bh_unlock_sock(sk);
344
345 /* If the chunk was backloged again, don't drop refs */
346 if (backloged)
347 return 0;
348 } else {
349 sctp_inq_push(inqueue, chunk);
350 }
351
352done:
353 /* Release the refs we took in sctp_add_backlog */
354 if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
355 sctp_association_put(sctp_assoc(rcvr));
356 else if (SCTP_EP_TYPE_SOCKET == rcvr->type)
357 sctp_endpoint_put(sctp_ep(rcvr));
358 else
359 BUG();
360
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900361 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
363
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700364static void sctp_add_backlog(struct sock *sk, struct sk_buff *skb)
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800365{
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700366 struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
367 struct sctp_ep_common *rcvr = chunk->rcvr;
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800368
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700369 /* Hold the assoc/ep while hanging on the backlog queue.
370 * This way, we know structures we need will not disappear from us
371 */
372 if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
373 sctp_association_hold(sctp_assoc(rcvr));
374 else if (SCTP_EP_TYPE_SOCKET == rcvr->type)
375 sctp_endpoint_hold(sctp_ep(rcvr));
376 else
377 BUG();
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800378
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700379 sk_add_backlog(sk, skb);
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800380}
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382/* Handle icmp frag needed error. */
383void sctp_icmp_frag_needed(struct sock *sk, struct sctp_association *asoc,
384 struct sctp_transport *t, __u32 pmtu)
385{
Wei Yongjun91bd6b12008-10-23 00:59:52 -0700386 if (!t || (t->pathmtu <= pmtu))
Frank Filz52ccb8e2005-12-22 11:36:46 -0800387 return;
388
Vlad Yasevich8a479492007-06-07 14:21:05 -0400389 if (sock_owned_by_user(sk)) {
390 asoc->pmtu_pending = 1;
391 t->pmtu_pending = 1;
392 return;
393 }
394
Frank Filz52ccb8e2005-12-22 11:36:46 -0800395 if (t->param_flags & SPP_PMTUD_ENABLE) {
Vlad Yasevichc910b472007-06-07 13:47:03 -0400396 /* Update transports view of the MTU */
397 sctp_transport_update_pmtu(t, pmtu);
Frank Filz52ccb8e2005-12-22 11:36:46 -0800398
399 /* Update association pmtu. */
400 sctp_assoc_sync_pmtu(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402
Frank Filz52ccb8e2005-12-22 11:36:46 -0800403 /* Retransmit with the new pmtu setting.
404 * Normally, if PMTU discovery is disabled, an ICMP Fragmentation
405 * Needed will never be sent, but if a message was sent before
406 * PMTU discovery was disabled that was larger than the PMTU, it
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900407 * would not be fragmented, so it must be re-transmitted fragmented.
Frank Filz52ccb8e2005-12-22 11:36:46 -0800408 */
409 sctp_retransmit(&asoc->outqueue, t, SCTP_RTXR_PMTUD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
412/*
413 * SCTP Implementer's Guide, 2.37 ICMP handling procedures
414 *
415 * ICMP8) If the ICMP code is a "Unrecognized next header type encountered"
416 * or a "Protocol Unreachable" treat this message as an abort
417 * with the T bit set.
418 *
419 * This function sends an event to the state machine, which will abort the
420 * association.
421 *
422 */
423void sctp_icmp_proto_unreachable(struct sock *sk,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900424 struct sctp_association *asoc,
425 struct sctp_transport *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Harvey Harrison0dc47872008-03-05 20:47:47 -0800427 SCTP_DEBUG_PRINTK("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 sctp_do_sm(SCTP_EVENT_T_OTHER,
430 SCTP_ST_OTHER(SCTP_EVENT_ICMP_PROTO_UNREACH),
Frank Filz3f7a87d2005-06-20 13:14:57 -0700431 asoc->state, asoc->ep, asoc, t,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 GFP_ATOMIC);
433
434}
435
436/* Common lookup code for icmp/icmpv6 error handler. */
437struct sock *sctp_err_lookup(int family, struct sk_buff *skb,
438 struct sctphdr *sctphdr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 struct sctp_association **app,
440 struct sctp_transport **tpp)
441{
442 union sctp_addr saddr;
443 union sctp_addr daddr;
444 struct sctp_af *af;
445 struct sock *sk = NULL;
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700446 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 struct sctp_transport *transport = NULL;
Wei Yongjun7115e632008-06-19 16:07:48 -0700448 struct sctp_init_chunk *chunkhdr;
449 __u32 vtag = ntohl(sctphdr->vtag);
450 int len = skb->len - ((void *)sctphdr - (void *)skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700452 *app = NULL; *tpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 af = sctp_get_af_specific(family);
455 if (unlikely(!af)) {
456 return NULL;
457 }
458
459 /* Initialize local addresses for lookups. */
460 af->from_skb(&saddr, skb, 1);
461 af->from_skb(&daddr, skb, 0);
462
463 /* Look for an association that matches the incoming ICMP error
464 * packet.
465 */
Al Virod55c41b2006-11-20 17:09:40 -0800466 asoc = __sctp_lookup_association(&saddr, &daddr, &transport);
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700467 if (!asoc)
468 return NULL;
469
470 sk = asoc->base.sk;
471
Wei Yongjun7115e632008-06-19 16:07:48 -0700472 /* RFC 4960, Appendix C. ICMP Handling
473 *
474 * ICMP6) An implementation MUST validate that the Verification Tag
475 * contained in the ICMP message matches the Verification Tag of
476 * the peer. If the Verification Tag is not 0 and does NOT
477 * match, discard the ICMP message. If it is 0 and the ICMP
478 * message contains enough bytes to verify that the chunk type is
479 * an INIT chunk and that the Initiate Tag matches the tag of the
480 * peer, continue with ICMP7. If the ICMP message is too short
481 * or the chunk type or the Initiate Tag does not match, silently
482 * discard the packet.
483 */
484 if (vtag == 0) {
485 chunkhdr = (struct sctp_init_chunk *)((void *)sctphdr
486 + sizeof(struct sctphdr));
487 if (len < sizeof(struct sctphdr) + sizeof(sctp_chunkhdr_t)
488 + sizeof(__be32) ||
489 chunkhdr->chunk_hdr.type != SCTP_CID_INIT ||
490 ntohl(chunkhdr->init_hdr.init_tag) != asoc->c.my_vtag) {
491 goto out;
492 }
493 } else if (vtag != asoc->c.peer_vtag) {
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700494 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 sctp_bh_lock_sock(sk);
498
499 /* If too many ICMPs get dropped on busy
500 * servers this needs to be solved differently.
501 */
502 if (sock_owned_by_user(sk))
Pavel Emelyanovde0744a2008-07-16 20:31:16 -0700503 NET_INC_STATS_BH(&init_net, LINUX_MIB_LOCKDROPPEDICMPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 *app = asoc;
506 *tpp = transport;
507 return sk;
508
509out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (asoc)
511 sctp_association_put(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return NULL;
513}
514
515/* Common cleanup code for icmp/icmpv6 error handler. */
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700516void sctp_err_finish(struct sock *sk, struct sctp_association *asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 sctp_bh_unlock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (asoc)
520 sctp_association_put(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
523/*
524 * This routine is called by the ICMP module when it gets some
525 * sort of error condition. If err < 0 then the socket should
526 * be closed and the error returned to the user. If err > 0
527 * it's just the icmp type << 8 | icmp code. After adjustment
528 * header points to the first 8 bytes of the sctp header. We need
529 * to find the appropriate port.
530 *
531 * The locking strategy used here is very "optimistic". When
532 * someone else accesses the socket the ICMP is just dropped
533 * and for some paths there is no check at all.
534 * A more general error queue to queue errors for later handling
535 * is probably better.
536 *
537 */
538void sctp_v4_err(struct sk_buff *skb, __u32 info)
539{
540 struct iphdr *iph = (struct iphdr *)skb->data;
Arnaldo Carvalho de Meloa27ef742007-03-13 17:17:10 -0300541 const int ihlen = iph->ihl * 4;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300542 const int type = icmp_hdr(skb)->type;
543 const int code = icmp_hdr(skb)->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 struct sock *sk;
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700545 struct sctp_association *asoc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 struct sctp_transport *transport;
547 struct inet_sock *inet;
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700548 sk_buff_data_t saveip, savesctp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 int err;
550
Arnaldo Carvalho de Meloa27ef742007-03-13 17:17:10 -0300551 if (skb->len < ihlen + 8) {
Pavel Emelyanovdcfc23c2008-07-14 23:03:00 -0700552 ICMP_INC_STATS_BH(&init_net, ICMP_MIB_INERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return;
554 }
555
556 /* Fix up skb to look at the embedded net header. */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700557 saveip = skb->network_header;
558 savesctp = skb->transport_header;
Arnaldo Carvalho de Melo31c77112007-03-10 19:04:55 -0300559 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloa27ef742007-03-13 17:17:10 -0300560 skb_set_transport_header(skb, ihlen);
561 sk = sctp_err_lookup(AF_INET, skb, sctp_hdr(skb), &asoc, &transport);
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700562 /* Put back, the original values. */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700563 skb->network_header = saveip;
564 skb->transport_header = savesctp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (!sk) {
Pavel Emelyanovdcfc23c2008-07-14 23:03:00 -0700566 ICMP_INC_STATS_BH(&init_net, ICMP_MIB_INERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 return;
568 }
569 /* Warning: The sock lock is held. Remember to call
570 * sctp_err_finish!
571 */
572
573 switch (type) {
574 case ICMP_PARAMETERPROB:
575 err = EPROTO;
576 break;
577 case ICMP_DEST_UNREACH:
578 if (code > NR_ICMP_UNREACH)
579 goto out_unlock;
580
581 /* PMTU discovery (RFC1191) */
582 if (ICMP_FRAG_NEEDED == code) {
583 sctp_icmp_frag_needed(sk, asoc, transport, info);
584 goto out_unlock;
585 }
586 else {
587 if (ICMP_PROT_UNREACH == code) {
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700588 sctp_icmp_proto_unreachable(sk, asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 transport);
590 goto out_unlock;
591 }
592 }
593 err = icmp_err_convert[code].errno;
594 break;
595 case ICMP_TIME_EXCEEDED:
596 /* Ignore any time exceeded errors due to fragment reassembly
597 * timeouts.
598 */
599 if (ICMP_EXC_FRAGTIME == code)
600 goto out_unlock;
601
602 err = EHOSTUNREACH;
603 break;
604 default:
605 goto out_unlock;
606 }
607
608 inet = inet_sk(sk);
609 if (!sock_owned_by_user(sk) && inet->recverr) {
610 sk->sk_err = err;
611 sk->sk_error_report(sk);
612 } else { /* Only an error on timeout */
613 sk->sk_err_soft = err;
614 }
615
616out_unlock:
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700617 sctp_err_finish(sk, asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
620/*
621 * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
622 *
623 * This function scans all the chunks in the OOTB packet to determine if
624 * the packet should be discarded right away. If a response might be needed
625 * for this packet, or, if further processing is possible, the packet will
626 * be queued to a proper inqueue for the next phase of handling.
627 *
628 * Output:
629 * Return 0 - If further processing is needed.
630 * Return 1 - If the packet can be discarded right away.
631 */
sebastian@breakpoint.cc04675212007-07-26 23:21:31 +0200632static int sctp_rcv_ootb(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
634 sctp_chunkhdr_t *ch;
635 __u8 *ch_end;
636 sctp_errhdr_t *err;
637
638 ch = (sctp_chunkhdr_t *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 /* Scan through all the chunks in the packet. */
Tsutomu Fujiia7d1f1b2006-01-17 11:57:09 -0800641 do {
642 /* Break out if chunk length is less then minimal. */
643 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
644 break;
645
646 ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700647 if (ch_end > skb_tail_pointer(skb))
Tsutomu Fujiia7d1f1b2006-01-17 11:57:09 -0800648 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 /* RFC 8.4, 2) If the OOTB packet contains an ABORT chunk, the
651 * receiver MUST silently discard the OOTB packet and take no
652 * further action.
653 */
654 if (SCTP_CID_ABORT == ch->type)
655 goto discard;
656
657 /* RFC 8.4, 6) If the packet contains a SHUTDOWN COMPLETE
658 * chunk, the receiver should silently discard the packet
659 * and take no further action.
660 */
661 if (SCTP_CID_SHUTDOWN_COMPLETE == ch->type)
662 goto discard;
663
Vlad Yasevich3c77f962007-09-17 15:14:28 -0400664 /* RFC 4460, 2.11.2
665 * This will discard packets with INIT chunk bundled as
666 * subsequent chunks in the packet. When INIT is first,
667 * the normal INIT processing will discard the chunk.
668 */
669 if (SCTP_CID_INIT == ch->type && (void *)ch != skb->data)
670 goto discard;
671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
673 * or a COOKIE ACK the SCTP Packet should be silently
674 * discarded.
675 */
676 if (SCTP_CID_COOKIE_ACK == ch->type)
677 goto discard;
678
679 if (SCTP_CID_ERROR == ch->type) {
680 sctp_walk_errors(err, ch) {
681 if (SCTP_ERROR_STALE_COOKIE == err->cause)
682 goto discard;
683 }
684 }
685
686 ch = (sctp_chunkhdr_t *) ch_end;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700687 } while (ch_end < skb_tail_pointer(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 return 0;
690
691discard:
692 return 1;
693}
694
695/* Insert endpoint into the hash table. */
696static void __sctp_hash_endpoint(struct sctp_endpoint *ep)
697{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 struct sctp_ep_common *epb;
699 struct sctp_hashbucket *head;
700
701 epb = &ep->base;
702
703 epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);
704 head = &sctp_ep_hashtable[epb->hashent];
705
706 sctp_write_lock(&head->lock);
Vlad Yasevichd970dbf2007-11-09 11:43:40 -0500707 hlist_add_head(&epb->node, &head->chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 sctp_write_unlock(&head->lock);
709}
710
711/* Add an endpoint to the hash. Local BH-safe. */
712void sctp_hash_endpoint(struct sctp_endpoint *ep)
713{
714 sctp_local_bh_disable();
715 __sctp_hash_endpoint(ep);
716 sctp_local_bh_enable();
717}
718
719/* Remove endpoint from the hash table. */
720static void __sctp_unhash_endpoint(struct sctp_endpoint *ep)
721{
722 struct sctp_hashbucket *head;
723 struct sctp_ep_common *epb;
724
725 epb = &ep->base;
726
Vlad Yasevichd970dbf2007-11-09 11:43:40 -0500727 if (hlist_unhashed(&epb->node))
728 return;
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);
731
732 head = &sctp_ep_hashtable[epb->hashent];
733
734 sctp_write_lock(&head->lock);
Vlad Yasevichd970dbf2007-11-09 11:43:40 -0500735 __hlist_del(&epb->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 sctp_write_unlock(&head->lock);
737}
738
739/* Remove endpoint from the hash. Local BH-safe. */
740void sctp_unhash_endpoint(struct sctp_endpoint *ep)
741{
742 sctp_local_bh_disable();
743 __sctp_unhash_endpoint(ep);
744 sctp_local_bh_enable();
745}
746
747/* Look up an endpoint. */
748static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(const union sctp_addr *laddr)
749{
750 struct sctp_hashbucket *head;
751 struct sctp_ep_common *epb;
752 struct sctp_endpoint *ep;
Vlad Yasevichd970dbf2007-11-09 11:43:40 -0500753 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 int hash;
755
Al Viro1c7d1fc2006-11-20 17:08:09 -0800756 hash = sctp_ep_hashfn(ntohs(laddr->v4.sin_port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 head = &sctp_ep_hashtable[hash];
758 read_lock(&head->lock);
Vlad Yasevichd970dbf2007-11-09 11:43:40 -0500759 sctp_for_each_hentry(epb, node, &head->chain) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 ep = sctp_ep(epb);
761 if (sctp_endpoint_is_match(ep, laddr))
762 goto hit;
763 }
764
765 ep = sctp_sk((sctp_get_ctl_sock()))->ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767hit:
768 sctp_endpoint_hold(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 read_unlock(&head->lock);
770 return ep;
771}
772
773/* Insert association into the hash table. */
774static void __sctp_hash_established(struct sctp_association *asoc)
775{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 struct sctp_ep_common *epb;
777 struct sctp_hashbucket *head;
778
779 epb = &asoc->base;
780
781 /* Calculate which chain this entry will belong to. */
782 epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port, asoc->peer.port);
783
784 head = &sctp_assoc_hashtable[epb->hashent];
785
786 sctp_write_lock(&head->lock);
Vlad Yasevichd970dbf2007-11-09 11:43:40 -0500787 hlist_add_head(&epb->node, &head->chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 sctp_write_unlock(&head->lock);
789}
790
791/* Add an association to the hash. Local BH-safe. */
792void sctp_hash_established(struct sctp_association *asoc)
793{
Vlad Yasevichde76e692006-10-30 18:55:11 -0800794 if (asoc->temp)
795 return;
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 sctp_local_bh_disable();
798 __sctp_hash_established(asoc);
799 sctp_local_bh_enable();
800}
801
802/* Remove association from the hash table. */
803static void __sctp_unhash_established(struct sctp_association *asoc)
804{
805 struct sctp_hashbucket *head;
806 struct sctp_ep_common *epb;
807
808 epb = &asoc->base;
809
810 epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port,
811 asoc->peer.port);
812
813 head = &sctp_assoc_hashtable[epb->hashent];
814
815 sctp_write_lock(&head->lock);
Vlad Yasevichd970dbf2007-11-09 11:43:40 -0500816 __hlist_del(&epb->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 sctp_write_unlock(&head->lock);
818}
819
820/* Remove association from the hash table. Local BH-safe. */
821void sctp_unhash_established(struct sctp_association *asoc)
822{
Vlad Yasevichde76e692006-10-30 18:55:11 -0800823 if (asoc->temp)
824 return;
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 sctp_local_bh_disable();
827 __sctp_unhash_established(asoc);
828 sctp_local_bh_enable();
829}
830
831/* Look up an association. */
832static struct sctp_association *__sctp_lookup_association(
833 const union sctp_addr *local,
834 const union sctp_addr *peer,
835 struct sctp_transport **pt)
836{
837 struct sctp_hashbucket *head;
838 struct sctp_ep_common *epb;
839 struct sctp_association *asoc;
840 struct sctp_transport *transport;
Vlad Yasevichd970dbf2007-11-09 11:43:40 -0500841 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 int hash;
843
844 /* Optimize here for direct hit, only listening connections can
845 * have wildcards anyways.
846 */
Al Viroe2fcced2006-11-20 17:08:41 -0800847 hash = sctp_assoc_hashfn(ntohs(local->v4.sin_port), ntohs(peer->v4.sin_port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 head = &sctp_assoc_hashtable[hash];
849 read_lock(&head->lock);
Vlad Yasevichd970dbf2007-11-09 11:43:40 -0500850 sctp_for_each_hentry(epb, node, &head->chain) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 asoc = sctp_assoc(epb);
852 transport = sctp_assoc_is_match(asoc, local, peer);
853 if (transport)
854 goto hit;
855 }
856
857 read_unlock(&head->lock);
858
859 return NULL;
860
861hit:
862 *pt = transport;
863 sctp_association_hold(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 read_unlock(&head->lock);
865 return asoc;
866}
867
868/* Look up an association. BH-safe. */
869SCTP_STATIC
870struct sctp_association *sctp_lookup_association(const union sctp_addr *laddr,
871 const union sctp_addr *paddr,
872 struct sctp_transport **transportp)
873{
874 struct sctp_association *asoc;
875
876 sctp_local_bh_disable();
877 asoc = __sctp_lookup_association(laddr, paddr, transportp);
878 sctp_local_bh_enable();
879
880 return asoc;
881}
882
883/* Is there an association matching the given local and peer addresses? */
884int sctp_has_association(const union sctp_addr *laddr,
885 const union sctp_addr *paddr)
886{
887 struct sctp_association *asoc;
888 struct sctp_transport *transport;
889
Al Viro6c7be552006-11-20 17:11:50 -0800890 if ((asoc = sctp_lookup_association(laddr, paddr, &transport))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 sctp_association_put(asoc);
892 return 1;
893 }
894
895 return 0;
896}
897
898/*
899 * SCTP Implementors Guide, 2.18 Handling of address
900 * parameters within the INIT or INIT-ACK.
901 *
902 * D) When searching for a matching TCB upon reception of an INIT
903 * or INIT-ACK chunk the receiver SHOULD use not only the
904 * source address of the packet (containing the INIT or
905 * INIT-ACK) but the receiver SHOULD also use all valid
906 * address parameters contained within the chunk.
907 *
908 * 2.18.3 Solution description
909 *
910 * This new text clearly specifies to an implementor the need
911 * to look within the INIT or INIT-ACK. Any implementation that
912 * does not do this, may not be able to establish associations
913 * in certain circumstances.
914 *
915 */
916static struct sctp_association *__sctp_rcv_init_lookup(struct sk_buff *skb,
917 const union sctp_addr *laddr, struct sctp_transport **transportp)
918{
919 struct sctp_association *asoc;
920 union sctp_addr addr;
921 union sctp_addr *paddr = &addr;
Arnaldo Carvalho de Melo2c0fd382007-03-13 13:59:32 -0300922 struct sctphdr *sh = sctp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 sctp_chunkhdr_t *ch;
924 union sctp_params params;
925 sctp_init_chunk_t *init;
926 struct sctp_transport *transport;
927 struct sctp_af *af;
928
929 ch = (sctp_chunkhdr_t *) skb->data;
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 /*
932 * This code will NOT touch anything inside the chunk--it is
933 * strictly READ-ONLY.
934 *
935 * RFC 2960 3 SCTP packet Format
936 *
937 * Multiple chunks can be bundled into one SCTP packet up to
938 * the MTU size, except for the INIT, INIT ACK, and SHUTDOWN
939 * COMPLETE chunks. These chunks MUST NOT be bundled with any
940 * other chunk in a packet. See Section 6.10 for more details
941 * on chunk bundling.
942 */
943
944 /* Find the start of the TLVs and the end of the chunk. This is
945 * the region we search for address parameters.
946 */
947 init = (sctp_init_chunk_t *)skb->data;
948
949 /* Walk the parameters looking for embedded addresses. */
950 sctp_walk_params(params, init, init_hdr.params) {
951
952 /* Note: Ignoring hostname addresses. */
953 af = sctp_get_af_specific(param_type2af(params.p->type));
954 if (!af)
955 continue;
956
Al Virodd86d132006-11-20 17:11:13 -0800957 af->from_addr_param(paddr, params.addr, sh->source, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Al Virodd86d132006-11-20 17:11:13 -0800959 asoc = __sctp_lookup_association(laddr, paddr, &transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 if (asoc)
961 return asoc;
962 }
963
964 return NULL;
965}
966
Vlad Yasevichdf218572007-12-20 14:10:38 -0800967/* ADD-IP, Section 5.2
968 * When an endpoint receives an ASCONF Chunk from the remote peer
969 * special procedures may be needed to identify the association the
970 * ASCONF Chunk is associated with. To properly find the association
971 * the following procedures SHOULD be followed:
972 *
973 * D2) If the association is not found, use the address found in the
974 * Address Parameter TLV combined with the port number found in the
975 * SCTP common header. If found proceed to rule D4.
976 *
977 * D2-ext) If more than one ASCONF Chunks are packed together, use the
978 * address found in the ASCONF Address Parameter TLV of each of the
979 * subsequent ASCONF Chunks. If found, proceed to rule D4.
980 */
981static struct sctp_association *__sctp_rcv_asconf_lookup(
982 sctp_chunkhdr_t *ch,
983 const union sctp_addr *laddr,
Al Virobc92dd12008-03-17 22:47:32 -0700984 __be16 peer_port,
Vlad Yasevichdf218572007-12-20 14:10:38 -0800985 struct sctp_transport **transportp)
986{
987 sctp_addip_chunk_t *asconf = (struct sctp_addip_chunk *)ch;
988 struct sctp_af *af;
989 union sctp_addr_param *param;
990 union sctp_addr paddr;
991
992 /* Skip over the ADDIP header and find the Address parameter */
993 param = (union sctp_addr_param *)(asconf + 1);
994
995 af = sctp_get_af_specific(param_type2af(param->v4.param_hdr.type));
996 if (unlikely(!af))
997 return NULL;
998
999 af->from_addr_param(&paddr, param, peer_port, 0);
1000
1001 return __sctp_lookup_association(laddr, &paddr, transportp);
1002}
1003
1004
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07001005/* SCTP-AUTH, Section 6.3:
1006* If the receiver does not find a STCB for a packet containing an AUTH
1007* chunk as the first chunk and not a COOKIE-ECHO chunk as the second
1008* chunk, it MUST use the chunks after the AUTH chunk to look up an existing
1009* association.
1010*
1011* This means that any chunks that can help us identify the association need
1012* to be looked at to find this assocation.
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07001013*/
Vlad Yasevichdf218572007-12-20 14:10:38 -08001014static struct sctp_association *__sctp_rcv_walk_lookup(struct sk_buff *skb,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07001015 const union sctp_addr *laddr,
1016 struct sctp_transport **transportp)
1017{
Vlad Yasevichdf218572007-12-20 14:10:38 -08001018 struct sctp_association *asoc = NULL;
1019 sctp_chunkhdr_t *ch;
1020 int have_auth = 0;
1021 unsigned int chunk_num = 1;
1022 __u8 *ch_end;
1023
1024 /* Walk through the chunks looking for AUTH or ASCONF chunks
1025 * to help us find the association.
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07001026 */
Vlad Yasevichdf218572007-12-20 14:10:38 -08001027 ch = (sctp_chunkhdr_t *) skb->data;
1028 do {
1029 /* Break out if chunk length is less then minimal. */
1030 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
1031 break;
1032
1033 ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
1034 if (ch_end > skb_tail_pointer(skb))
1035 break;
1036
1037 switch(ch->type) {
1038 case SCTP_CID_AUTH:
1039 have_auth = chunk_num;
1040 break;
1041
1042 case SCTP_CID_COOKIE_ECHO:
1043 /* If a packet arrives containing an AUTH chunk as
1044 * a first chunk, a COOKIE-ECHO chunk as the second
1045 * chunk, and possibly more chunks after them, and
1046 * the receiver does not have an STCB for that
1047 * packet, then authentication is based on
1048 * the contents of the COOKIE- ECHO chunk.
1049 */
1050 if (have_auth == 1 && chunk_num == 2)
1051 return NULL;
1052 break;
1053
1054 case SCTP_CID_ASCONF:
1055 if (have_auth || sctp_addip_noauth)
1056 asoc = __sctp_rcv_asconf_lookup(ch, laddr,
1057 sctp_hdr(skb)->source,
1058 transportp);
1059 default:
1060 break;
1061 }
1062
1063 if (asoc)
1064 break;
1065
1066 ch = (sctp_chunkhdr_t *) ch_end;
1067 chunk_num++;
1068 } while (ch_end < skb_tail_pointer(skb));
1069
1070 return asoc;
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07001071}
1072
1073/*
1074 * There are circumstances when we need to look inside the SCTP packet
1075 * for information to help us find the association. Examples
1076 * include looking inside of INIT/INIT-ACK chunks or after the AUTH
1077 * chunks.
1078 */
1079static struct sctp_association *__sctp_rcv_lookup_harder(struct sk_buff *skb,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07001080 const union sctp_addr *laddr,
1081 struct sctp_transport **transportp)
1082{
1083 sctp_chunkhdr_t *ch;
1084
1085 ch = (sctp_chunkhdr_t *) skb->data;
1086
Vlad Yasevichdf218572007-12-20 14:10:38 -08001087 /* The code below will attempt to walk the chunk and extract
1088 * parameter information. Before we do that, we need to verify
1089 * that the chunk length doesn't cause overflow. Otherwise, we'll
1090 * walk off the end.
1091 */
1092 if (WORD_ROUND(ntohs(ch->length)) > skb->len)
1093 return NULL;
1094
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07001095 /* If this is INIT/INIT-ACK look inside the chunk too. */
1096 switch (ch->type) {
1097 case SCTP_CID_INIT:
1098 case SCTP_CID_INIT_ACK:
1099 return __sctp_rcv_init_lookup(skb, laddr, transportp);
1100 break;
1101
Vlad Yasevichdf218572007-12-20 14:10:38 -08001102 default:
1103 return __sctp_rcv_walk_lookup(skb, laddr, transportp);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07001104 break;
1105 }
1106
Vlad Yasevichdf218572007-12-20 14:10:38 -08001107
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07001108 return NULL;
1109}
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111/* Lookup an association for an inbound skb. */
1112static struct sctp_association *__sctp_rcv_lookup(struct sk_buff *skb,
1113 const union sctp_addr *paddr,
1114 const union sctp_addr *laddr,
1115 struct sctp_transport **transportp)
1116{
1117 struct sctp_association *asoc;
1118
1119 asoc = __sctp_lookup_association(laddr, paddr, transportp);
1120
1121 /* Further lookup for INIT/INIT-ACK packets.
1122 * SCTP Implementors Guide, 2.18 Handling of address
1123 * parameters within the INIT or INIT-ACK.
1124 */
1125 if (!asoc)
Vlad Yasevichdf218572007-12-20 14:10:38 -08001126 asoc = __sctp_rcv_lookup_harder(skb, laddr, transportp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 return asoc;
1129}