blob: 85b3441c4a23fa52b4f2c7daf50e2fc024b9484b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* SCTP kernel reference Implementation
2 * 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 *
9 * This file is part of the SCTP kernel reference Implementation
10 *
11 * These functions handle all input from the IP layer into SCTP.
12 *
13 * The SCTP reference implementation is free software;
14 * 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 *
19 * The SCTP reference implementation is distributed in the hope that it
20 * 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>
63
64/* Forward declarations for internal helpers. */
65static int sctp_rcv_ootb(struct sk_buff *);
66static struct sctp_association *__sctp_rcv_lookup(struct sk_buff *skb,
67 const union sctp_addr *laddr,
68 const union sctp_addr *paddr,
69 struct sctp_transport **transportp);
70static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(const union sctp_addr *laddr);
71static struct sctp_association *__sctp_lookup_association(
72 const union sctp_addr *local,
73 const union sctp_addr *peer,
74 struct sctp_transport **pt);
75
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -070076static void sctp_add_backlog(struct sock *sk, struct sk_buff *skb);
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79/* Calculate the SCTP checksum of an SCTP packet. */
80static inline int sctp_rcv_checksum(struct sk_buff *skb)
81{
82 struct sctphdr *sh;
83 __u32 cmp, val;
84 struct sk_buff *list = skb_shinfo(skb)->frag_list;
85
86 sh = (struct sctphdr *) skb->h.raw;
87 cmp = ntohl(sh->checksum);
88
89 val = sctp_start_cksum((__u8 *)sh, skb_headlen(skb));
90
91 for (; list; list = list->next)
92 val = sctp_update_cksum((__u8 *)list->data, skb_headlen(list),
93 val);
94
95 val = sctp_end_cksum(val);
96
97 if (val != cmp) {
98 /* CRC failure, dump it. */
99 SCTP_INC_STATS_BH(SCTP_MIB_CHECKSUMERRORS);
100 return -1;
101 }
102 return 0;
103}
104
David S. Miller79af02c2005-07-08 21:47:49 -0700105struct sctp_input_cb {
106 union {
107 struct inet_skb_parm h4;
108#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
109 struct inet6_skb_parm h6;
110#endif
111 } header;
112 struct sctp_chunk *chunk;
113};
114#define SCTP_INPUT_CB(__skb) ((struct sctp_input_cb *)&((__skb)->cb[0]))
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116/*
117 * This is the routine which IP calls when receiving an SCTP packet.
118 */
119int sctp_rcv(struct sk_buff *skb)
120{
121 struct sock *sk;
122 struct sctp_association *asoc;
123 struct sctp_endpoint *ep = NULL;
124 struct sctp_ep_common *rcvr;
125 struct sctp_transport *transport = NULL;
126 struct sctp_chunk *chunk;
127 struct sctphdr *sh;
128 union sctp_addr src;
129 union sctp_addr dest;
Al Viroe2fcced2006-11-20 17:08:41 -0800130 union sctp_addr tmp, tmp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 int family;
132 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 if (skb->pkt_type!=PACKET_HOST)
135 goto discard_it;
136
137 SCTP_INC_STATS_BH(SCTP_MIB_INSCTPPACKS);
138
Herbert Xu28cd7752006-10-29 23:46:42 -0800139 if (skb_linearize(skb))
140 goto discard_it;
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 sh = (struct sctphdr *) skb->h.raw;
143
144 /* Pull up the IP and SCTP headers. */
145 __skb_pull(skb, skb->h.raw - skb->data);
146 if (skb->len < sizeof(struct sctphdr))
147 goto discard_it;
Sridhar Samudrala503b55f2006-06-17 22:57:28 -0700148 if ((skb->ip_summed != CHECKSUM_UNNECESSARY) &&
149 (sctp_rcv_checksum(skb) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 goto discard_it;
151
152 skb_pull(skb, sizeof(struct sctphdr));
153
154 /* Make sure we at least have chunk headers worth of data left. */
155 if (skb->len < sizeof(struct sctp_chunkhdr))
156 goto discard_it;
157
158 family = ipver2af(skb->nh.iph->version);
159 af = sctp_get_af_specific(family);
160 if (unlikely(!af))
161 goto discard_it;
162
163 /* Initialize local addresses for lookups. */
164 af->from_skb(&src, skb, 1);
165 af->from_skb(&dest, skb, 0);
166
167 /* If the packet is to or from a non-unicast address,
168 * silently discard the packet.
169 *
170 * This is not clearly defined in the RFC except in section
171 * 8.4 - OOTB handling. However, based on the book "Stream Control
172 * Transmission Protocol" 2.1, "It is important to note that the
173 * IP address of an SCTP transport address must be a routable
174 * unicast address. In other words, IP multicast addresses and
175 * IP broadcast addresses cannot be used in an SCTP transport
176 * address."
177 */
Vlad Yasevich5636bef2006-06-17 22:55:35 -0700178 if (!af->addr_valid(&src, NULL, skb) ||
179 !af->addr_valid(&dest, NULL, skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 goto discard_it;
181
Al Viro1c7d1fc2006-11-20 17:08:09 -0800182 flip_to_n(&tmp, &dest);
Al Viroe2fcced2006-11-20 17:08:41 -0800183 flip_to_n(&tmp2, &src);
184
185 asoc = __sctp_rcv_lookup(skb, &tmp2, &tmp, &transport);
Al Viro1c7d1fc2006-11-20 17:08:09 -0800186
Neil Horman0fd9a652005-06-13 15:11:24 -0700187 if (!asoc)
Al Viro1c7d1fc2006-11-20 17:08:09 -0800188 ep = __sctp_rcv_lookup_endpoint(&tmp);
Neil Horman0fd9a652005-06-13 15:11:24 -0700189
190 /* Retrieve the common input handling substructure. */
191 rcvr = asoc ? &asoc->base : &ep->base;
192 sk = rcvr->sk;
193
194 /*
195 * If a frame arrives on an interface and the receiving socket is
196 * bound to another interface, via SO_BINDTODEVICE, treat it as OOTB
197 */
198 if (sk->sk_bound_dev_if && (sk->sk_bound_dev_if != af->skb_iif(skb)))
199 {
Neil Horman0fd9a652005-06-13 15:11:24 -0700200 if (asoc) {
201 sctp_association_put(asoc);
202 asoc = NULL;
203 } else {
204 sctp_endpoint_put(ep);
205 ep = NULL;
206 }
207 sk = sctp_get_ctl_sock();
208 ep = sctp_sk(sk)->ep;
209 sctp_endpoint_hold(ep);
Neil Horman0fd9a652005-06-13 15:11:24 -0700210 rcvr = &ep->base;
211 }
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 /*
214 * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
215 * An SCTP packet is called an "out of the blue" (OOTB)
216 * packet if it is correctly formed, i.e., passed the
217 * receiver's checksum check, but the receiver is not
218 * able to identify the association to which this
219 * packet belongs.
220 */
221 if (!asoc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 if (sctp_rcv_ootb(skb)) {
223 SCTP_INC_STATS_BH(SCTP_MIB_OUTOFBLUES);
224 goto discard_release;
225 }
226 }
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if (!xfrm_policy_check(sk, XFRM_POLICY_IN, skb, family))
229 goto discard_release;
Patrick McHardyb59c2702006-01-06 23:06:10 -0800230 nf_reset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Dmitry Mishinfda9ef52006-08-31 15:28:39 -0700232 if (sk_filter(sk, skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 goto discard_release;
234
235 /* Create an SCTP packet structure. */
236 chunk = sctp_chunkify(skb, asoc, sk);
Herbert Xu2babf9d2006-03-25 01:25:29 -0800237 if (!chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 goto discard_release;
David S. Miller79af02c2005-07-08 21:47:49 -0700239 SCTP_INPUT_CB(skb)->chunk = chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 /* Remember what endpoint is to handle this packet. */
242 chunk->rcvr = rcvr;
243
244 /* Remember the SCTP header. */
245 chunk->sctp_hdr = sh;
246
247 /* Set the source and destination addresses of the incoming chunk. */
248 sctp_init_addrs(chunk, &src, &dest);
249
250 /* Remember where we came from. */
251 chunk->transport = transport;
252
253 /* Acquire access to the sock lock. Note: We are safe from other
254 * bottom halves on this lock, but a user may be in the lock too,
255 * so check if it is busy.
256 */
257 sctp_bh_lock_sock(sk);
258
Sridhar Samudralaac0b0462006-08-22 00:15:33 -0700259 if (sock_owned_by_user(sk)) {
260 SCTP_INC_STATS_BH(SCTP_MIB_IN_PKT_BACKLOG);
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700261 sctp_add_backlog(sk, skb);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -0700262 } else {
263 SCTP_INC_STATS_BH(SCTP_MIB_IN_PKT_SOFTIRQ);
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700264 sctp_inq_push(&chunk->rcvr->inqueue, chunk);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -0700265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 sctp_bh_unlock_sock(sk);
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700268
269 /* Release the asoc/ep ref we took in the lookup calls. */
270 if (asoc)
271 sctp_association_put(asoc);
272 else
273 sctp_endpoint_put(ep);
Sridhar Samudrala7a48f922006-01-17 11:51:28 -0800274
Herbert Xu2babf9d2006-03-25 01:25:29 -0800275 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277discard_it:
Sridhar Samudralaac0b0462006-08-22 00:15:33 -0700278 SCTP_INC_STATS_BH(SCTP_MIB_IN_PKT_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 kfree_skb(skb);
Herbert Xu2babf9d2006-03-25 01:25:29 -0800280 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282discard_release:
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700283 /* Release the asoc/ep ref we took in the lookup calls. */
Neil Horman0fd9a652005-06-13 15:11:24 -0700284 if (asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 sctp_association_put(asoc);
Neil Horman0fd9a652005-06-13 15:11:24 -0700286 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 sctp_endpoint_put(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 goto discard_it;
290}
291
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700292/* Process the backlog queue of the socket. Every skb on
293 * the backlog holds a ref on an association or endpoint.
294 * We hold this ref throughout the state machine to make
295 * sure that the structure we need is still around.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 */
297int sctp_backlog_rcv(struct sock *sk, struct sk_buff *skb)
298{
David S. Miller79af02c2005-07-08 21:47:49 -0700299 struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700300 struct sctp_inq *inqueue = &chunk->rcvr->inqueue;
Sridhar Samudrala7a48f922006-01-17 11:51:28 -0800301 struct sctp_ep_common *rcvr = NULL;
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700302 int backloged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Sridhar Samudrala7a48f922006-01-17 11:51:28 -0800304 rcvr = chunk->rcvr;
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800305
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700306 /* If the rcvr is dead then the association or endpoint
307 * has been deleted and we can safely drop the chunk
308 * and refs that we are holding.
309 */
310 if (rcvr->dead) {
311 sctp_chunk_free(chunk);
312 goto done;
313 }
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800314
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700315 if (unlikely(rcvr->sk != sk)) {
316 /* In this case, the association moved from one socket to
317 * another. We are currently sitting on the backlog of the
318 * old socket, so we need to move.
319 * However, since we are here in the process context we
320 * need to take make sure that the user doesn't own
321 * the new socket when we process the packet.
322 * If the new socket is user-owned, queue the chunk to the
323 * backlog of the new socket without dropping any refs.
324 * Otherwise, we can safely push the chunk on the inqueue.
325 */
Sridhar Samudrala7a48f922006-01-17 11:51:28 -0800326
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700327 sk = rcvr->sk;
328 sctp_bh_lock_sock(sk);
329
330 if (sock_owned_by_user(sk)) {
331 sk_add_backlog(sk, skb);
332 backloged = 1;
333 } else
334 sctp_inq_push(inqueue, chunk);
335
336 sctp_bh_unlock_sock(sk);
337
338 /* If the chunk was backloged again, don't drop refs */
339 if (backloged)
340 return 0;
341 } else {
342 sctp_inq_push(inqueue, chunk);
343 }
344
345done:
346 /* Release the refs we took in sctp_add_backlog */
347 if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
348 sctp_association_put(sctp_assoc(rcvr));
349 else if (SCTP_EP_TYPE_SOCKET == rcvr->type)
350 sctp_endpoint_put(sctp_ep(rcvr));
351 else
352 BUG();
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return 0;
355}
356
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700357static void sctp_add_backlog(struct sock *sk, struct sk_buff *skb)
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800358{
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700359 struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
360 struct sctp_ep_common *rcvr = chunk->rcvr;
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800361
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700362 /* Hold the assoc/ep while hanging on the backlog queue.
363 * This way, we know structures we need will not disappear from us
364 */
365 if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
366 sctp_association_hold(sctp_assoc(rcvr));
367 else if (SCTP_EP_TYPE_SOCKET == rcvr->type)
368 sctp_endpoint_hold(sctp_ep(rcvr));
369 else
370 BUG();
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800371
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700372 sk_add_backlog(sk, skb);
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800373}
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375/* Handle icmp frag needed error. */
376void sctp_icmp_frag_needed(struct sock *sk, struct sctp_association *asoc,
377 struct sctp_transport *t, __u32 pmtu)
378{
Frank Filz52ccb8e2005-12-22 11:36:46 -0800379 if (sock_owned_by_user(sk) || !t || (t->pathmtu == pmtu))
380 return;
381
382 if (t->param_flags & SPP_PMTUD_ENABLE) {
383 if (unlikely(pmtu < SCTP_DEFAULT_MINSEGMENT)) {
384 printk(KERN_WARNING "%s: Reported pmtu %d too low, "
385 "using default minimum of %d\n",
386 __FUNCTION__, pmtu,
387 SCTP_DEFAULT_MINSEGMENT);
388 /* Use default minimum segment size and disable
389 * pmtu discovery on this transport.
390 */
391 t->pathmtu = SCTP_DEFAULT_MINSEGMENT;
Vlad Yasevichb56bab42006-09-29 17:09:34 -0700392 t->param_flags = (t->param_flags & ~SPP_PMTUD) |
Frank Filz52ccb8e2005-12-22 11:36:46 -0800393 SPP_PMTUD_DISABLE;
394 } else {
395 t->pathmtu = pmtu;
396 }
397
398 /* Update association pmtu. */
399 sctp_assoc_sync_pmtu(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
401
Frank Filz52ccb8e2005-12-22 11:36:46 -0800402 /* Retransmit with the new pmtu setting.
403 * Normally, if PMTU discovery is disabled, an ICMP Fragmentation
404 * Needed will never be sent, but if a message was sent before
405 * PMTU discovery was disabled that was larger than the PMTU, it
406 * would not be fragmented, so it must be re-transmitted fragmented.
407 */
408 sctp_retransmit(&asoc->outqueue, t, SCTP_RTXR_PMTUD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
410
411/*
412 * SCTP Implementer's Guide, 2.37 ICMP handling procedures
413 *
414 * ICMP8) If the ICMP code is a "Unrecognized next header type encountered"
415 * or a "Protocol Unreachable" treat this message as an abort
416 * with the T bit set.
417 *
418 * This function sends an event to the state machine, which will abort the
419 * association.
420 *
421 */
422void sctp_icmp_proto_unreachable(struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 struct sctp_association *asoc,
424 struct sctp_transport *t)
425{
426 SCTP_DEBUG_PRINTK("%s\n", __FUNCTION__);
427
428 sctp_do_sm(SCTP_EVENT_T_OTHER,
429 SCTP_ST_OTHER(SCTP_EVENT_ICMP_PROTO_UNREACH),
Frank Filz3f7a87d2005-06-20 13:14:57 -0700430 asoc->state, asoc->ep, asoc, t,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 GFP_ATOMIC);
432
433}
434
435/* Common lookup code for icmp/icmpv6 error handler. */
436struct sock *sctp_err_lookup(int family, struct sk_buff *skb,
437 struct sctphdr *sctphdr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 struct sctp_association **app,
439 struct sctp_transport **tpp)
440{
441 union sctp_addr saddr;
442 union sctp_addr daddr;
443 struct sctp_af *af;
444 struct sock *sk = NULL;
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700445 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 struct sctp_transport *transport = NULL;
Al Viroe2fcced2006-11-20 17:08:41 -0800447 union sctp_addr tmp, tmp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700449 *app = NULL; *tpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 af = sctp_get_af_specific(family);
452 if (unlikely(!af)) {
453 return NULL;
454 }
455
456 /* Initialize local addresses for lookups. */
457 af->from_skb(&saddr, skb, 1);
458 af->from_skb(&daddr, skb, 0);
Al Viroe2fcced2006-11-20 17:08:41 -0800459 flip_to_n(&tmp, &saddr);
460 flip_to_n(&tmp2, &daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 /* Look for an association that matches the incoming ICMP error
463 * packet.
464 */
Al Viroe2fcced2006-11-20 17:08:41 -0800465 asoc = __sctp_lookup_association(&tmp, &tmp2, &transport);
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700466 if (!asoc)
467 return NULL;
468
469 sk = asoc->base.sk;
470
471 if (ntohl(sctphdr->vtag) != asoc->c.peer_vtag) {
472 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
473 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 sctp_bh_lock_sock(sk);
477
478 /* If too many ICMPs get dropped on busy
479 * servers this needs to be solved differently.
480 */
481 if (sock_owned_by_user(sk))
482 NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 *app = asoc;
485 *tpp = transport;
486 return sk;
487
488out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (asoc)
490 sctp_association_put(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return NULL;
492}
493
494/* Common cleanup code for icmp/icmpv6 error handler. */
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700495void sctp_err_finish(struct sock *sk, struct sctp_association *asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
497 sctp_bh_unlock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (asoc)
499 sctp_association_put(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
502/*
503 * This routine is called by the ICMP module when it gets some
504 * sort of error condition. If err < 0 then the socket should
505 * be closed and the error returned to the user. If err > 0
506 * it's just the icmp type << 8 | icmp code. After adjustment
507 * header points to the first 8 bytes of the sctp header. We need
508 * to find the appropriate port.
509 *
510 * The locking strategy used here is very "optimistic". When
511 * someone else accesses the socket the ICMP is just dropped
512 * and for some paths there is no check at all.
513 * A more general error queue to queue errors for later handling
514 * is probably better.
515 *
516 */
517void sctp_v4_err(struct sk_buff *skb, __u32 info)
518{
519 struct iphdr *iph = (struct iphdr *)skb->data;
520 struct sctphdr *sh = (struct sctphdr *)(skb->data + (iph->ihl <<2));
521 int type = skb->h.icmph->type;
522 int code = skb->h.icmph->code;
523 struct sock *sk;
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700524 struct sctp_association *asoc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 struct sctp_transport *transport;
526 struct inet_sock *inet;
527 char *saveip, *savesctp;
528 int err;
529
530 if (skb->len < ((iph->ihl << 2) + 8)) {
531 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
532 return;
533 }
534
535 /* Fix up skb to look at the embedded net header. */
536 saveip = skb->nh.raw;
537 savesctp = skb->h.raw;
538 skb->nh.iph = iph;
539 skb->h.raw = (char *)sh;
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700540 sk = sctp_err_lookup(AF_INET, skb, sh, &asoc, &transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 /* Put back, the original pointers. */
542 skb->nh.raw = saveip;
543 skb->h.raw = savesctp;
544 if (!sk) {
545 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
546 return;
547 }
548 /* Warning: The sock lock is held. Remember to call
549 * sctp_err_finish!
550 */
551
552 switch (type) {
553 case ICMP_PARAMETERPROB:
554 err = EPROTO;
555 break;
556 case ICMP_DEST_UNREACH:
557 if (code > NR_ICMP_UNREACH)
558 goto out_unlock;
559
560 /* PMTU discovery (RFC1191) */
561 if (ICMP_FRAG_NEEDED == code) {
562 sctp_icmp_frag_needed(sk, asoc, transport, info);
563 goto out_unlock;
564 }
565 else {
566 if (ICMP_PROT_UNREACH == code) {
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700567 sctp_icmp_proto_unreachable(sk, asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 transport);
569 goto out_unlock;
570 }
571 }
572 err = icmp_err_convert[code].errno;
573 break;
574 case ICMP_TIME_EXCEEDED:
575 /* Ignore any time exceeded errors due to fragment reassembly
576 * timeouts.
577 */
578 if (ICMP_EXC_FRAGTIME == code)
579 goto out_unlock;
580
581 err = EHOSTUNREACH;
582 break;
583 default:
584 goto out_unlock;
585 }
586
587 inet = inet_sk(sk);
588 if (!sock_owned_by_user(sk) && inet->recverr) {
589 sk->sk_err = err;
590 sk->sk_error_report(sk);
591 } else { /* Only an error on timeout */
592 sk->sk_err_soft = err;
593 }
594
595out_unlock:
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700596 sctp_err_finish(sk, asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
599/*
600 * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
601 *
602 * This function scans all the chunks in the OOTB packet to determine if
603 * the packet should be discarded right away. If a response might be needed
604 * for this packet, or, if further processing is possible, the packet will
605 * be queued to a proper inqueue for the next phase of handling.
606 *
607 * Output:
608 * Return 0 - If further processing is needed.
609 * Return 1 - If the packet can be discarded right away.
610 */
611int sctp_rcv_ootb(struct sk_buff *skb)
612{
613 sctp_chunkhdr_t *ch;
614 __u8 *ch_end;
615 sctp_errhdr_t *err;
616
617 ch = (sctp_chunkhdr_t *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 /* Scan through all the chunks in the packet. */
Tsutomu Fujiia7d1f1b2006-01-17 11:57:09 -0800620 do {
621 /* Break out if chunk length is less then minimal. */
622 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
623 break;
624
625 ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
626 if (ch_end > skb->tail)
627 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 /* RFC 8.4, 2) If the OOTB packet contains an ABORT chunk, the
630 * receiver MUST silently discard the OOTB packet and take no
631 * further action.
632 */
633 if (SCTP_CID_ABORT == ch->type)
634 goto discard;
635
636 /* RFC 8.4, 6) If the packet contains a SHUTDOWN COMPLETE
637 * chunk, the receiver should silently discard the packet
638 * and take no further action.
639 */
640 if (SCTP_CID_SHUTDOWN_COMPLETE == ch->type)
641 goto discard;
642
643 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
644 * or a COOKIE ACK the SCTP Packet should be silently
645 * discarded.
646 */
647 if (SCTP_CID_COOKIE_ACK == ch->type)
648 goto discard;
649
650 if (SCTP_CID_ERROR == ch->type) {
651 sctp_walk_errors(err, ch) {
652 if (SCTP_ERROR_STALE_COOKIE == err->cause)
653 goto discard;
654 }
655 }
656
657 ch = (sctp_chunkhdr_t *) ch_end;
Tsutomu Fujiia7d1f1b2006-01-17 11:57:09 -0800658 } while (ch_end < skb->tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 return 0;
661
662discard:
663 return 1;
664}
665
666/* Insert endpoint into the hash table. */
667static void __sctp_hash_endpoint(struct sctp_endpoint *ep)
668{
669 struct sctp_ep_common **epp;
670 struct sctp_ep_common *epb;
671 struct sctp_hashbucket *head;
672
673 epb = &ep->base;
674
675 epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);
676 head = &sctp_ep_hashtable[epb->hashent];
677
678 sctp_write_lock(&head->lock);
679 epp = &head->chain;
680 epb->next = *epp;
681 if (epb->next)
682 (*epp)->pprev = &epb->next;
683 *epp = epb;
684 epb->pprev = epp;
685 sctp_write_unlock(&head->lock);
686}
687
688/* Add an endpoint to the hash. Local BH-safe. */
689void sctp_hash_endpoint(struct sctp_endpoint *ep)
690{
691 sctp_local_bh_disable();
692 __sctp_hash_endpoint(ep);
693 sctp_local_bh_enable();
694}
695
696/* Remove endpoint from the hash table. */
697static void __sctp_unhash_endpoint(struct sctp_endpoint *ep)
698{
699 struct sctp_hashbucket *head;
700 struct sctp_ep_common *epb;
701
702 epb = &ep->base;
703
704 epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);
705
706 head = &sctp_ep_hashtable[epb->hashent];
707
708 sctp_write_lock(&head->lock);
709
710 if (epb->pprev) {
711 if (epb->next)
712 epb->next->pprev = epb->pprev;
713 *epb->pprev = epb->next;
714 epb->pprev = NULL;
715 }
716
717 sctp_write_unlock(&head->lock);
718}
719
720/* Remove endpoint from the hash. Local BH-safe. */
721void sctp_unhash_endpoint(struct sctp_endpoint *ep)
722{
723 sctp_local_bh_disable();
724 __sctp_unhash_endpoint(ep);
725 sctp_local_bh_enable();
726}
727
728/* Look up an endpoint. */
729static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(const union sctp_addr *laddr)
730{
731 struct sctp_hashbucket *head;
732 struct sctp_ep_common *epb;
733 struct sctp_endpoint *ep;
734 int hash;
735
Al Viro1c7d1fc2006-11-20 17:08:09 -0800736 hash = sctp_ep_hashfn(ntohs(laddr->v4.sin_port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 head = &sctp_ep_hashtable[hash];
738 read_lock(&head->lock);
739 for (epb = head->chain; epb; epb = epb->next) {
740 ep = sctp_ep(epb);
741 if (sctp_endpoint_is_match(ep, laddr))
742 goto hit;
743 }
744
745 ep = sctp_sk((sctp_get_ctl_sock()))->ep;
746 epb = &ep->base;
747
748hit:
749 sctp_endpoint_hold(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 read_unlock(&head->lock);
751 return ep;
752}
753
754/* Insert association into the hash table. */
755static void __sctp_hash_established(struct sctp_association *asoc)
756{
757 struct sctp_ep_common **epp;
758 struct sctp_ep_common *epb;
759 struct sctp_hashbucket *head;
760
761 epb = &asoc->base;
762
763 /* Calculate which chain this entry will belong to. */
764 epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port, asoc->peer.port);
765
766 head = &sctp_assoc_hashtable[epb->hashent];
767
768 sctp_write_lock(&head->lock);
769 epp = &head->chain;
770 epb->next = *epp;
771 if (epb->next)
772 (*epp)->pprev = &epb->next;
773 *epp = epb;
774 epb->pprev = epp;
775 sctp_write_unlock(&head->lock);
776}
777
778/* Add an association to the hash. Local BH-safe. */
779void sctp_hash_established(struct sctp_association *asoc)
780{
Vlad Yasevichde76e692006-10-30 18:55:11 -0800781 if (asoc->temp)
782 return;
783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 sctp_local_bh_disable();
785 __sctp_hash_established(asoc);
786 sctp_local_bh_enable();
787}
788
789/* Remove association from the hash table. */
790static void __sctp_unhash_established(struct sctp_association *asoc)
791{
792 struct sctp_hashbucket *head;
793 struct sctp_ep_common *epb;
794
795 epb = &asoc->base;
796
797 epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port,
798 asoc->peer.port);
799
800 head = &sctp_assoc_hashtable[epb->hashent];
801
802 sctp_write_lock(&head->lock);
803
804 if (epb->pprev) {
805 if (epb->next)
806 epb->next->pprev = epb->pprev;
807 *epb->pprev = epb->next;
808 epb->pprev = NULL;
809 }
810
811 sctp_write_unlock(&head->lock);
812}
813
814/* Remove association from the hash table. Local BH-safe. */
815void sctp_unhash_established(struct sctp_association *asoc)
816{
Vlad Yasevichde76e692006-10-30 18:55:11 -0800817 if (asoc->temp)
818 return;
819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 sctp_local_bh_disable();
821 __sctp_unhash_established(asoc);
822 sctp_local_bh_enable();
823}
824
825/* Look up an association. */
826static struct sctp_association *__sctp_lookup_association(
827 const union sctp_addr *local,
828 const union sctp_addr *peer,
829 struct sctp_transport **pt)
830{
831 struct sctp_hashbucket *head;
832 struct sctp_ep_common *epb;
833 struct sctp_association *asoc;
834 struct sctp_transport *transport;
835 int hash;
836
837 /* Optimize here for direct hit, only listening connections can
838 * have wildcards anyways.
839 */
Al Viroe2fcced2006-11-20 17:08:41 -0800840 hash = sctp_assoc_hashfn(ntohs(local->v4.sin_port), ntohs(peer->v4.sin_port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 head = &sctp_assoc_hashtable[hash];
842 read_lock(&head->lock);
843 for (epb = head->chain; epb; epb = epb->next) {
844 asoc = sctp_assoc(epb);
845 transport = sctp_assoc_is_match(asoc, local, peer);
846 if (transport)
847 goto hit;
848 }
849
850 read_unlock(&head->lock);
851
852 return NULL;
853
854hit:
855 *pt = transport;
856 sctp_association_hold(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 read_unlock(&head->lock);
858 return asoc;
859}
860
861/* Look up an association. BH-safe. */
862SCTP_STATIC
863struct sctp_association *sctp_lookup_association(const union sctp_addr *laddr,
864 const union sctp_addr *paddr,
865 struct sctp_transport **transportp)
866{
867 struct sctp_association *asoc;
868
869 sctp_local_bh_disable();
870 asoc = __sctp_lookup_association(laddr, paddr, transportp);
871 sctp_local_bh_enable();
872
873 return asoc;
874}
875
876/* Is there an association matching the given local and peer addresses? */
877int sctp_has_association(const union sctp_addr *laddr,
878 const union sctp_addr *paddr)
879{
880 struct sctp_association *asoc;
881 struct sctp_transport *transport;
Al Viroe2fcced2006-11-20 17:08:41 -0800882 union sctp_addr tmp, tmp2;
883 flip_to_n(&tmp, laddr);
884 flip_to_n(&tmp2, paddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Al Viroe2fcced2006-11-20 17:08:41 -0800886 if ((asoc = sctp_lookup_association(&tmp, &tmp2, &transport))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 sctp_association_put(asoc);
888 return 1;
889 }
890
891 return 0;
892}
893
894/*
895 * SCTP Implementors Guide, 2.18 Handling of address
896 * parameters within the INIT or INIT-ACK.
897 *
898 * D) When searching for a matching TCB upon reception of an INIT
899 * or INIT-ACK chunk the receiver SHOULD use not only the
900 * source address of the packet (containing the INIT or
901 * INIT-ACK) but the receiver SHOULD also use all valid
902 * address parameters contained within the chunk.
903 *
904 * 2.18.3 Solution description
905 *
906 * This new text clearly specifies to an implementor the need
907 * to look within the INIT or INIT-ACK. Any implementation that
908 * does not do this, may not be able to establish associations
909 * in certain circumstances.
910 *
911 */
912static struct sctp_association *__sctp_rcv_init_lookup(struct sk_buff *skb,
913 const union sctp_addr *laddr, struct sctp_transport **transportp)
914{
915 struct sctp_association *asoc;
916 union sctp_addr addr;
917 union sctp_addr *paddr = &addr;
918 struct sctphdr *sh = (struct sctphdr *) skb->h.raw;
919 sctp_chunkhdr_t *ch;
920 union sctp_params params;
921 sctp_init_chunk_t *init;
922 struct sctp_transport *transport;
923 struct sctp_af *af;
Al Viroe2fcced2006-11-20 17:08:41 -0800924 union sctp_addr tmp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 ch = (sctp_chunkhdr_t *) skb->data;
927
928 /* If this is INIT/INIT-ACK look inside the chunk too. */
929 switch (ch->type) {
930 case SCTP_CID_INIT:
931 case SCTP_CID_INIT_ACK:
932 break;
933 default:
934 return NULL;
935 }
936
937 /* The code below will attempt to walk the chunk and extract
938 * parameter information. Before we do that, we need to verify
939 * that the chunk length doesn't cause overflow. Otherwise, we'll
940 * walk off the end.
941 */
942 if (WORD_ROUND(ntohs(ch->length)) > skb->len)
943 return NULL;
944
945 /*
946 * This code will NOT touch anything inside the chunk--it is
947 * strictly READ-ONLY.
948 *
949 * RFC 2960 3 SCTP packet Format
950 *
951 * Multiple chunks can be bundled into one SCTP packet up to
952 * the MTU size, except for the INIT, INIT ACK, and SHUTDOWN
953 * COMPLETE chunks. These chunks MUST NOT be bundled with any
954 * other chunk in a packet. See Section 6.10 for more details
955 * on chunk bundling.
956 */
957
958 /* Find the start of the TLVs and the end of the chunk. This is
959 * the region we search for address parameters.
960 */
961 init = (sctp_init_chunk_t *)skb->data;
962
963 /* Walk the parameters looking for embedded addresses. */
964 sctp_walk_params(params, init, init_hdr.params) {
965
966 /* Note: Ignoring hostname addresses. */
967 af = sctp_get_af_specific(param_type2af(params.p->type));
968 if (!af)
969 continue;
970
971 af->from_addr_param(paddr, params.addr, ntohs(sh->source), 0);
Al Viroe2fcced2006-11-20 17:08:41 -0800972 flip_to_n(&tmp2, paddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Al Viroe2fcced2006-11-20 17:08:41 -0800974 asoc = __sctp_lookup_association(laddr, &tmp2, &transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (asoc)
976 return asoc;
977 }
978
979 return NULL;
980}
981
982/* Lookup an association for an inbound skb. */
983static struct sctp_association *__sctp_rcv_lookup(struct sk_buff *skb,
984 const union sctp_addr *paddr,
985 const union sctp_addr *laddr,
986 struct sctp_transport **transportp)
987{
988 struct sctp_association *asoc;
989
990 asoc = __sctp_lookup_association(laddr, paddr, transportp);
991
992 /* Further lookup for INIT/INIT-ACK packets.
993 * SCTP Implementors Guide, 2.18 Handling of address
994 * parameters within the INIT or INIT-ACK.
995 */
996 if (!asoc)
997 asoc = __sctp_rcv_init_lookup(skb, laddr, transportp);
998
999 return asoc;
1000}