blob: 03f65de75d88280c0096f4aac8efbbe4330e9383 [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;
130 int family;
131 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 if (skb->pkt_type!=PACKET_HOST)
134 goto discard_it;
135
136 SCTP_INC_STATS_BH(SCTP_MIB_INSCTPPACKS);
137
138 sh = (struct sctphdr *) skb->h.raw;
139
140 /* Pull up the IP and SCTP headers. */
141 __skb_pull(skb, skb->h.raw - skb->data);
142 if (skb->len < sizeof(struct sctphdr))
143 goto discard_it;
Sridhar Samudrala503b55f2006-06-17 22:57:28 -0700144 if ((skb->ip_summed != CHECKSUM_UNNECESSARY) &&
145 (sctp_rcv_checksum(skb) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 goto discard_it;
147
148 skb_pull(skb, sizeof(struct sctphdr));
149
150 /* Make sure we at least have chunk headers worth of data left. */
151 if (skb->len < sizeof(struct sctp_chunkhdr))
152 goto discard_it;
153
154 family = ipver2af(skb->nh.iph->version);
155 af = sctp_get_af_specific(family);
156 if (unlikely(!af))
157 goto discard_it;
158
159 /* Initialize local addresses for lookups. */
160 af->from_skb(&src, skb, 1);
161 af->from_skb(&dest, skb, 0);
162
163 /* If the packet is to or from a non-unicast address,
164 * silently discard the packet.
165 *
166 * This is not clearly defined in the RFC except in section
167 * 8.4 - OOTB handling. However, based on the book "Stream Control
168 * Transmission Protocol" 2.1, "It is important to note that the
169 * IP address of an SCTP transport address must be a routable
170 * unicast address. In other words, IP multicast addresses and
171 * IP broadcast addresses cannot be used in an SCTP transport
172 * address."
173 */
Vlad Yasevich5636bef2006-06-17 22:55:35 -0700174 if (!af->addr_valid(&src, NULL, skb) ||
175 !af->addr_valid(&dest, NULL, skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 goto discard_it;
177
178 asoc = __sctp_rcv_lookup(skb, &src, &dest, &transport);
179
Neil Horman0fd9a652005-06-13 15:11:24 -0700180 if (!asoc)
181 ep = __sctp_rcv_lookup_endpoint(&dest);
182
183 /* Retrieve the common input handling substructure. */
184 rcvr = asoc ? &asoc->base : &ep->base;
185 sk = rcvr->sk;
186
187 /*
188 * If a frame arrives on an interface and the receiving socket is
189 * bound to another interface, via SO_BINDTODEVICE, treat it as OOTB
190 */
191 if (sk->sk_bound_dev_if && (sk->sk_bound_dev_if != af->skb_iif(skb)))
192 {
Neil Horman0fd9a652005-06-13 15:11:24 -0700193 if (asoc) {
194 sctp_association_put(asoc);
195 asoc = NULL;
196 } else {
197 sctp_endpoint_put(ep);
198 ep = NULL;
199 }
200 sk = sctp_get_ctl_sock();
201 ep = sctp_sk(sk)->ep;
202 sctp_endpoint_hold(ep);
Neil Horman0fd9a652005-06-13 15:11:24 -0700203 rcvr = &ep->base;
204 }
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 /*
207 * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
208 * An SCTP packet is called an "out of the blue" (OOTB)
209 * packet if it is correctly formed, i.e., passed the
210 * receiver's checksum check, but the receiver is not
211 * able to identify the association to which this
212 * packet belongs.
213 */
214 if (!asoc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if (sctp_rcv_ootb(skb)) {
216 SCTP_INC_STATS_BH(SCTP_MIB_OUTOFBLUES);
217 goto discard_release;
218 }
219 }
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 /* SCTP seems to always need a timestamp right now (FIXME) */
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700222 if (skb->tstamp.off_sec == 0) {
223 __net_timestamp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 sock_enable_timestamp(sk);
225 }
226
227 if (!xfrm_policy_check(sk, XFRM_POLICY_IN, skb, family))
228 goto discard_release;
Patrick McHardyb59c2702006-01-06 23:06:10 -0800229 nf_reset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Dmitry Mishinfda9ef52006-08-31 15:28:39 -0700231 if (sk_filter(sk, skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 goto discard_release;
233
234 /* Create an SCTP packet structure. */
235 chunk = sctp_chunkify(skb, asoc, sk);
Herbert Xu2babf9d2006-03-25 01:25:29 -0800236 if (!chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 goto discard_release;
David S. Miller79af02c2005-07-08 21:47:49 -0700238 SCTP_INPUT_CB(skb)->chunk = chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 /* Remember what endpoint is to handle this packet. */
241 chunk->rcvr = rcvr;
242
243 /* Remember the SCTP header. */
244 chunk->sctp_hdr = sh;
245
246 /* Set the source and destination addresses of the incoming chunk. */
247 sctp_init_addrs(chunk, &src, &dest);
248
249 /* Remember where we came from. */
250 chunk->transport = transport;
251
252 /* Acquire access to the sock lock. Note: We are safe from other
253 * bottom halves on this lock, but a user may be in the lock too,
254 * so check if it is busy.
255 */
256 sctp_bh_lock_sock(sk);
257
Sridhar Samudralaac0b0462006-08-22 00:15:33 -0700258 if (sock_owned_by_user(sk)) {
259 SCTP_INC_STATS_BH(SCTP_MIB_IN_PKT_BACKLOG);
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700260 sctp_add_backlog(sk, skb);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -0700261 } else {
262 SCTP_INC_STATS_BH(SCTP_MIB_IN_PKT_SOFTIRQ);
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700263 sctp_inq_push(&chunk->rcvr->inqueue, chunk);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -0700264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 sctp_bh_unlock_sock(sk);
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700267
268 /* Release the asoc/ep ref we took in the lookup calls. */
269 if (asoc)
270 sctp_association_put(asoc);
271 else
272 sctp_endpoint_put(ep);
Sridhar Samudrala7a48f922006-01-17 11:51:28 -0800273
Herbert Xu2babf9d2006-03-25 01:25:29 -0800274 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276discard_it:
Sridhar Samudralaac0b0462006-08-22 00:15:33 -0700277 SCTP_INC_STATS_BH(SCTP_MIB_IN_PKT_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 kfree_skb(skb);
Herbert Xu2babf9d2006-03-25 01:25:29 -0800279 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281discard_release:
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700282 /* Release the asoc/ep ref we took in the lookup calls. */
Neil Horman0fd9a652005-06-13 15:11:24 -0700283 if (asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 sctp_association_put(asoc);
Neil Horman0fd9a652005-06-13 15:11:24 -0700285 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 sctp_endpoint_put(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 goto discard_it;
289}
290
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700291/* Process the backlog queue of the socket. Every skb on
292 * the backlog holds a ref on an association or endpoint.
293 * We hold this ref throughout the state machine to make
294 * sure that the structure we need is still around.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 */
296int sctp_backlog_rcv(struct sock *sk, struct sk_buff *skb)
297{
David S. Miller79af02c2005-07-08 21:47:49 -0700298 struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700299 struct sctp_inq *inqueue = &chunk->rcvr->inqueue;
Sridhar Samudrala7a48f922006-01-17 11:51:28 -0800300 struct sctp_ep_common *rcvr = NULL;
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700301 int backloged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Sridhar Samudrala7a48f922006-01-17 11:51:28 -0800303 rcvr = chunk->rcvr;
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800304
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700305 /* If the rcvr is dead then the association or endpoint
306 * has been deleted and we can safely drop the chunk
307 * and refs that we are holding.
308 */
309 if (rcvr->dead) {
310 sctp_chunk_free(chunk);
311 goto done;
312 }
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800313
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700314 if (unlikely(rcvr->sk != sk)) {
315 /* In this case, the association moved from one socket to
316 * another. We are currently sitting on the backlog of the
317 * old socket, so we need to move.
318 * However, since we are here in the process context we
319 * need to take make sure that the user doesn't own
320 * the new socket when we process the packet.
321 * If the new socket is user-owned, queue the chunk to the
322 * backlog of the new socket without dropping any refs.
323 * Otherwise, we can safely push the chunk on the inqueue.
324 */
Sridhar Samudrala7a48f922006-01-17 11:51:28 -0800325
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700326 sk = rcvr->sk;
327 sctp_bh_lock_sock(sk);
328
329 if (sock_owned_by_user(sk)) {
330 sk_add_backlog(sk, skb);
331 backloged = 1;
332 } else
333 sctp_inq_push(inqueue, chunk);
334
335 sctp_bh_unlock_sock(sk);
336
337 /* If the chunk was backloged again, don't drop refs */
338 if (backloged)
339 return 0;
340 } else {
341 sctp_inq_push(inqueue, chunk);
342 }
343
344done:
345 /* Release the refs we took in sctp_add_backlog */
346 if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
347 sctp_association_put(sctp_assoc(rcvr));
348 else if (SCTP_EP_TYPE_SOCKET == rcvr->type)
349 sctp_endpoint_put(sctp_ep(rcvr));
350 else
351 BUG();
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return 0;
354}
355
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700356static void sctp_add_backlog(struct sock *sk, struct sk_buff *skb)
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800357{
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700358 struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
359 struct sctp_ep_common *rcvr = chunk->rcvr;
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800360
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700361 /* Hold the assoc/ep while hanging on the backlog queue.
362 * This way, we know structures we need will not disappear from us
363 */
364 if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
365 sctp_association_hold(sctp_assoc(rcvr));
366 else if (SCTP_EP_TYPE_SOCKET == rcvr->type)
367 sctp_endpoint_hold(sctp_ep(rcvr));
368 else
369 BUG();
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800370
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700371 sk_add_backlog(sk, skb);
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800372}
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374/* Handle icmp frag needed error. */
375void sctp_icmp_frag_needed(struct sock *sk, struct sctp_association *asoc,
376 struct sctp_transport *t, __u32 pmtu)
377{
Frank Filz52ccb8e2005-12-22 11:36:46 -0800378 if (sock_owned_by_user(sk) || !t || (t->pathmtu == pmtu))
379 return;
380
381 if (t->param_flags & SPP_PMTUD_ENABLE) {
382 if (unlikely(pmtu < SCTP_DEFAULT_MINSEGMENT)) {
383 printk(KERN_WARNING "%s: Reported pmtu %d too low, "
384 "using default minimum of %d\n",
385 __FUNCTION__, pmtu,
386 SCTP_DEFAULT_MINSEGMENT);
387 /* Use default minimum segment size and disable
388 * pmtu discovery on this transport.
389 */
390 t->pathmtu = SCTP_DEFAULT_MINSEGMENT;
391 t->param_flags = (t->param_flags & ~SPP_HB) |
392 SPP_PMTUD_DISABLE;
393 } else {
394 t->pathmtu = pmtu;
395 }
396
397 /* Update association pmtu. */
398 sctp_assoc_sync_pmtu(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400
Frank Filz52ccb8e2005-12-22 11:36:46 -0800401 /* Retransmit with the new pmtu setting.
402 * Normally, if PMTU discovery is disabled, an ICMP Fragmentation
403 * Needed will never be sent, but if a message was sent before
404 * PMTU discovery was disabled that was larger than the PMTU, it
405 * would not be fragmented, so it must be re-transmitted fragmented.
406 */
407 sctp_retransmit(&asoc->outqueue, t, SCTP_RTXR_PMTUD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409
410/*
411 * SCTP Implementer's Guide, 2.37 ICMP handling procedures
412 *
413 * ICMP8) If the ICMP code is a "Unrecognized next header type encountered"
414 * or a "Protocol Unreachable" treat this message as an abort
415 * with the T bit set.
416 *
417 * This function sends an event to the state machine, which will abort the
418 * association.
419 *
420 */
421void sctp_icmp_proto_unreachable(struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 struct sctp_association *asoc,
423 struct sctp_transport *t)
424{
425 SCTP_DEBUG_PRINTK("%s\n", __FUNCTION__);
426
427 sctp_do_sm(SCTP_EVENT_T_OTHER,
428 SCTP_ST_OTHER(SCTP_EVENT_ICMP_PROTO_UNREACH),
Frank Filz3f7a87d2005-06-20 13:14:57 -0700429 asoc->state, asoc->ep, asoc, t,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 GFP_ATOMIC);
431
432}
433
434/* Common lookup code for icmp/icmpv6 error handler. */
435struct sock *sctp_err_lookup(int family, struct sk_buff *skb,
436 struct sctphdr *sctphdr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 struct sctp_association **app,
438 struct sctp_transport **tpp)
439{
440 union sctp_addr saddr;
441 union sctp_addr daddr;
442 struct sctp_af *af;
443 struct sock *sk = NULL;
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700444 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 struct sctp_transport *transport = NULL;
446
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700447 *app = NULL; *tpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 af = sctp_get_af_specific(family);
450 if (unlikely(!af)) {
451 return NULL;
452 }
453
454 /* Initialize local addresses for lookups. */
455 af->from_skb(&saddr, skb, 1);
456 af->from_skb(&daddr, skb, 0);
457
458 /* Look for an association that matches the incoming ICMP error
459 * packet.
460 */
461 asoc = __sctp_lookup_association(&saddr, &daddr, &transport);
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700462 if (!asoc)
463 return NULL;
464
465 sk = asoc->base.sk;
466
467 if (ntohl(sctphdr->vtag) != asoc->c.peer_vtag) {
468 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
469 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 }
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 sctp_bh_lock_sock(sk);
473
474 /* If too many ICMPs get dropped on busy
475 * servers this needs to be solved differently.
476 */
477 if (sock_owned_by_user(sk))
478 NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 *app = asoc;
481 *tpp = transport;
482 return sk;
483
484out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (asoc)
486 sctp_association_put(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return NULL;
488}
489
490/* Common cleanup code for icmp/icmpv6 error handler. */
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700491void sctp_err_finish(struct sock *sk, struct sctp_association *asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
493 sctp_bh_unlock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (asoc)
495 sctp_association_put(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
498/*
499 * This routine is called by the ICMP module when it gets some
500 * sort of error condition. If err < 0 then the socket should
501 * be closed and the error returned to the user. If err > 0
502 * it's just the icmp type << 8 | icmp code. After adjustment
503 * header points to the first 8 bytes of the sctp header. We need
504 * to find the appropriate port.
505 *
506 * The locking strategy used here is very "optimistic". When
507 * someone else accesses the socket the ICMP is just dropped
508 * and for some paths there is no check at all.
509 * A more general error queue to queue errors for later handling
510 * is probably better.
511 *
512 */
513void sctp_v4_err(struct sk_buff *skb, __u32 info)
514{
515 struct iphdr *iph = (struct iphdr *)skb->data;
516 struct sctphdr *sh = (struct sctphdr *)(skb->data + (iph->ihl <<2));
517 int type = skb->h.icmph->type;
518 int code = skb->h.icmph->code;
519 struct sock *sk;
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700520 struct sctp_association *asoc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 struct sctp_transport *transport;
522 struct inet_sock *inet;
523 char *saveip, *savesctp;
524 int err;
525
526 if (skb->len < ((iph->ihl << 2) + 8)) {
527 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
528 return;
529 }
530
531 /* Fix up skb to look at the embedded net header. */
532 saveip = skb->nh.raw;
533 savesctp = skb->h.raw;
534 skb->nh.iph = iph;
535 skb->h.raw = (char *)sh;
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700536 sk = sctp_err_lookup(AF_INET, skb, sh, &asoc, &transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 /* Put back, the original pointers. */
538 skb->nh.raw = saveip;
539 skb->h.raw = savesctp;
540 if (!sk) {
541 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
542 return;
543 }
544 /* Warning: The sock lock is held. Remember to call
545 * sctp_err_finish!
546 */
547
548 switch (type) {
549 case ICMP_PARAMETERPROB:
550 err = EPROTO;
551 break;
552 case ICMP_DEST_UNREACH:
553 if (code > NR_ICMP_UNREACH)
554 goto out_unlock;
555
556 /* PMTU discovery (RFC1191) */
557 if (ICMP_FRAG_NEEDED == code) {
558 sctp_icmp_frag_needed(sk, asoc, transport, info);
559 goto out_unlock;
560 }
561 else {
562 if (ICMP_PROT_UNREACH == code) {
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700563 sctp_icmp_proto_unreachable(sk, asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 transport);
565 goto out_unlock;
566 }
567 }
568 err = icmp_err_convert[code].errno;
569 break;
570 case ICMP_TIME_EXCEEDED:
571 /* Ignore any time exceeded errors due to fragment reassembly
572 * timeouts.
573 */
574 if (ICMP_EXC_FRAGTIME == code)
575 goto out_unlock;
576
577 err = EHOSTUNREACH;
578 break;
579 default:
580 goto out_unlock;
581 }
582
583 inet = inet_sk(sk);
584 if (!sock_owned_by_user(sk) && inet->recverr) {
585 sk->sk_err = err;
586 sk->sk_error_report(sk);
587 } else { /* Only an error on timeout */
588 sk->sk_err_soft = err;
589 }
590
591out_unlock:
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700592 sctp_err_finish(sk, asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594
595/*
596 * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
597 *
598 * This function scans all the chunks in the OOTB packet to determine if
599 * the packet should be discarded right away. If a response might be needed
600 * for this packet, or, if further processing is possible, the packet will
601 * be queued to a proper inqueue for the next phase of handling.
602 *
603 * Output:
604 * Return 0 - If further processing is needed.
605 * Return 1 - If the packet can be discarded right away.
606 */
607int sctp_rcv_ootb(struct sk_buff *skb)
608{
609 sctp_chunkhdr_t *ch;
610 __u8 *ch_end;
611 sctp_errhdr_t *err;
612
613 ch = (sctp_chunkhdr_t *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 /* Scan through all the chunks in the packet. */
Tsutomu Fujiia7d1f1b2006-01-17 11:57:09 -0800616 do {
617 /* Break out if chunk length is less then minimal. */
618 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
619 break;
620
621 ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
622 if (ch_end > skb->tail)
623 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 /* RFC 8.4, 2) If the OOTB packet contains an ABORT chunk, the
626 * receiver MUST silently discard the OOTB packet and take no
627 * further action.
628 */
629 if (SCTP_CID_ABORT == ch->type)
630 goto discard;
631
632 /* RFC 8.4, 6) If the packet contains a SHUTDOWN COMPLETE
633 * chunk, the receiver should silently discard the packet
634 * and take no further action.
635 */
636 if (SCTP_CID_SHUTDOWN_COMPLETE == ch->type)
637 goto discard;
638
639 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
640 * or a COOKIE ACK the SCTP Packet should be silently
641 * discarded.
642 */
643 if (SCTP_CID_COOKIE_ACK == ch->type)
644 goto discard;
645
646 if (SCTP_CID_ERROR == ch->type) {
647 sctp_walk_errors(err, ch) {
648 if (SCTP_ERROR_STALE_COOKIE == err->cause)
649 goto discard;
650 }
651 }
652
653 ch = (sctp_chunkhdr_t *) ch_end;
Tsutomu Fujiia7d1f1b2006-01-17 11:57:09 -0800654 } while (ch_end < skb->tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 return 0;
657
658discard:
659 return 1;
660}
661
662/* Insert endpoint into the hash table. */
663static void __sctp_hash_endpoint(struct sctp_endpoint *ep)
664{
665 struct sctp_ep_common **epp;
666 struct sctp_ep_common *epb;
667 struct sctp_hashbucket *head;
668
669 epb = &ep->base;
670
671 epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);
672 head = &sctp_ep_hashtable[epb->hashent];
673
674 sctp_write_lock(&head->lock);
675 epp = &head->chain;
676 epb->next = *epp;
677 if (epb->next)
678 (*epp)->pprev = &epb->next;
679 *epp = epb;
680 epb->pprev = epp;
681 sctp_write_unlock(&head->lock);
682}
683
684/* Add an endpoint to the hash. Local BH-safe. */
685void sctp_hash_endpoint(struct sctp_endpoint *ep)
686{
687 sctp_local_bh_disable();
688 __sctp_hash_endpoint(ep);
689 sctp_local_bh_enable();
690}
691
692/* Remove endpoint from the hash table. */
693static void __sctp_unhash_endpoint(struct sctp_endpoint *ep)
694{
695 struct sctp_hashbucket *head;
696 struct sctp_ep_common *epb;
697
698 epb = &ep->base;
699
700 epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);
701
702 head = &sctp_ep_hashtable[epb->hashent];
703
704 sctp_write_lock(&head->lock);
705
706 if (epb->pprev) {
707 if (epb->next)
708 epb->next->pprev = epb->pprev;
709 *epb->pprev = epb->next;
710 epb->pprev = NULL;
711 }
712
713 sctp_write_unlock(&head->lock);
714}
715
716/* Remove endpoint from the hash. Local BH-safe. */
717void sctp_unhash_endpoint(struct sctp_endpoint *ep)
718{
719 sctp_local_bh_disable();
720 __sctp_unhash_endpoint(ep);
721 sctp_local_bh_enable();
722}
723
724/* Look up an endpoint. */
725static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(const union sctp_addr *laddr)
726{
727 struct sctp_hashbucket *head;
728 struct sctp_ep_common *epb;
729 struct sctp_endpoint *ep;
730 int hash;
731
732 hash = sctp_ep_hashfn(laddr->v4.sin_port);
733 head = &sctp_ep_hashtable[hash];
734 read_lock(&head->lock);
735 for (epb = head->chain; epb; epb = epb->next) {
736 ep = sctp_ep(epb);
737 if (sctp_endpoint_is_match(ep, laddr))
738 goto hit;
739 }
740
741 ep = sctp_sk((sctp_get_ctl_sock()))->ep;
742 epb = &ep->base;
743
744hit:
745 sctp_endpoint_hold(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 read_unlock(&head->lock);
747 return ep;
748}
749
750/* Insert association into the hash table. */
751static void __sctp_hash_established(struct sctp_association *asoc)
752{
753 struct sctp_ep_common **epp;
754 struct sctp_ep_common *epb;
755 struct sctp_hashbucket *head;
756
757 epb = &asoc->base;
758
759 /* Calculate which chain this entry will belong to. */
760 epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port, asoc->peer.port);
761
762 head = &sctp_assoc_hashtable[epb->hashent];
763
764 sctp_write_lock(&head->lock);
765 epp = &head->chain;
766 epb->next = *epp;
767 if (epb->next)
768 (*epp)->pprev = &epb->next;
769 *epp = epb;
770 epb->pprev = epp;
771 sctp_write_unlock(&head->lock);
772}
773
774/* Add an association to the hash. Local BH-safe. */
775void sctp_hash_established(struct sctp_association *asoc)
776{
777 sctp_local_bh_disable();
778 __sctp_hash_established(asoc);
779 sctp_local_bh_enable();
780}
781
782/* Remove association from the hash table. */
783static void __sctp_unhash_established(struct sctp_association *asoc)
784{
785 struct sctp_hashbucket *head;
786 struct sctp_ep_common *epb;
787
788 epb = &asoc->base;
789
790 epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port,
791 asoc->peer.port);
792
793 head = &sctp_assoc_hashtable[epb->hashent];
794
795 sctp_write_lock(&head->lock);
796
797 if (epb->pprev) {
798 if (epb->next)
799 epb->next->pprev = epb->pprev;
800 *epb->pprev = epb->next;
801 epb->pprev = NULL;
802 }
803
804 sctp_write_unlock(&head->lock);
805}
806
807/* Remove association from the hash table. Local BH-safe. */
808void sctp_unhash_established(struct sctp_association *asoc)
809{
810 sctp_local_bh_disable();
811 __sctp_unhash_established(asoc);
812 sctp_local_bh_enable();
813}
814
815/* Look up an association. */
816static struct sctp_association *__sctp_lookup_association(
817 const union sctp_addr *local,
818 const union sctp_addr *peer,
819 struct sctp_transport **pt)
820{
821 struct sctp_hashbucket *head;
822 struct sctp_ep_common *epb;
823 struct sctp_association *asoc;
824 struct sctp_transport *transport;
825 int hash;
826
827 /* Optimize here for direct hit, only listening connections can
828 * have wildcards anyways.
829 */
830 hash = sctp_assoc_hashfn(local->v4.sin_port, peer->v4.sin_port);
831 head = &sctp_assoc_hashtable[hash];
832 read_lock(&head->lock);
833 for (epb = head->chain; epb; epb = epb->next) {
834 asoc = sctp_assoc(epb);
835 transport = sctp_assoc_is_match(asoc, local, peer);
836 if (transport)
837 goto hit;
838 }
839
840 read_unlock(&head->lock);
841
842 return NULL;
843
844hit:
845 *pt = transport;
846 sctp_association_hold(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 read_unlock(&head->lock);
848 return asoc;
849}
850
851/* Look up an association. BH-safe. */
852SCTP_STATIC
853struct sctp_association *sctp_lookup_association(const union sctp_addr *laddr,
854 const union sctp_addr *paddr,
855 struct sctp_transport **transportp)
856{
857 struct sctp_association *asoc;
858
859 sctp_local_bh_disable();
860 asoc = __sctp_lookup_association(laddr, paddr, transportp);
861 sctp_local_bh_enable();
862
863 return asoc;
864}
865
866/* Is there an association matching the given local and peer addresses? */
867int sctp_has_association(const union sctp_addr *laddr,
868 const union sctp_addr *paddr)
869{
870 struct sctp_association *asoc;
871 struct sctp_transport *transport;
872
873 if ((asoc = sctp_lookup_association(laddr, paddr, &transport))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 sctp_association_put(asoc);
875 return 1;
876 }
877
878 return 0;
879}
880
881/*
882 * SCTP Implementors Guide, 2.18 Handling of address
883 * parameters within the INIT or INIT-ACK.
884 *
885 * D) When searching for a matching TCB upon reception of an INIT
886 * or INIT-ACK chunk the receiver SHOULD use not only the
887 * source address of the packet (containing the INIT or
888 * INIT-ACK) but the receiver SHOULD also use all valid
889 * address parameters contained within the chunk.
890 *
891 * 2.18.3 Solution description
892 *
893 * This new text clearly specifies to an implementor the need
894 * to look within the INIT or INIT-ACK. Any implementation that
895 * does not do this, may not be able to establish associations
896 * in certain circumstances.
897 *
898 */
899static struct sctp_association *__sctp_rcv_init_lookup(struct sk_buff *skb,
900 const union sctp_addr *laddr, struct sctp_transport **transportp)
901{
902 struct sctp_association *asoc;
903 union sctp_addr addr;
904 union sctp_addr *paddr = &addr;
905 struct sctphdr *sh = (struct sctphdr *) skb->h.raw;
906 sctp_chunkhdr_t *ch;
907 union sctp_params params;
908 sctp_init_chunk_t *init;
909 struct sctp_transport *transport;
910 struct sctp_af *af;
911
912 ch = (sctp_chunkhdr_t *) skb->data;
913
914 /* If this is INIT/INIT-ACK look inside the chunk too. */
915 switch (ch->type) {
916 case SCTP_CID_INIT:
917 case SCTP_CID_INIT_ACK:
918 break;
919 default:
920 return NULL;
921 }
922
923 /* The code below will attempt to walk the chunk and extract
924 * parameter information. Before we do that, we need to verify
925 * that the chunk length doesn't cause overflow. Otherwise, we'll
926 * walk off the end.
927 */
928 if (WORD_ROUND(ntohs(ch->length)) > skb->len)
929 return NULL;
930
931 /*
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
957 af->from_addr_param(paddr, params.addr, ntohs(sh->source), 0);
958
959 asoc = __sctp_lookup_association(laddr, paddr, &transport);
960 if (asoc)
961 return asoc;
962 }
963
964 return NULL;
965}
966
967/* Lookup an association for an inbound skb. */
968static struct sctp_association *__sctp_rcv_lookup(struct sk_buff *skb,
969 const union sctp_addr *paddr,
970 const union sctp_addr *laddr,
971 struct sctp_transport **transportp)
972{
973 struct sctp_association *asoc;
974
975 asoc = __sctp_lookup_association(laddr, paddr, transportp);
976
977 /* Further lookup for INIT/INIT-ACK packets.
978 * SCTP Implementors Guide, 2.18 Handling of address
979 * parameters within the INIT or INIT-ACK.
980 */
981 if (!asoc)
982 asoc = __sctp_rcv_init_lookup(skb, laddr, transportp);
983
984 return asoc;
985}