blob: 1bb3f264da1f3f278e9835f6b133aa32ac7dfc7d [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 Viro1c7d1fc2006-11-20 17:08:09 -0800130 union sctp_addr tmp;
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
182 asoc = __sctp_rcv_lookup(skb, &src, &dest, &transport);
183
Al Viro1c7d1fc2006-11-20 17:08:09 -0800184 flip_to_n(&tmp, &dest);
185
Neil Horman0fd9a652005-06-13 15:11:24 -0700186 if (!asoc)
Al Viro1c7d1fc2006-11-20 17:08:09 -0800187 ep = __sctp_rcv_lookup_endpoint(&tmp);
Neil Horman0fd9a652005-06-13 15:11:24 -0700188
189 /* Retrieve the common input handling substructure. */
190 rcvr = asoc ? &asoc->base : &ep->base;
191 sk = rcvr->sk;
192
193 /*
194 * If a frame arrives on an interface and the receiving socket is
195 * bound to another interface, via SO_BINDTODEVICE, treat it as OOTB
196 */
197 if (sk->sk_bound_dev_if && (sk->sk_bound_dev_if != af->skb_iif(skb)))
198 {
Neil Horman0fd9a652005-06-13 15:11:24 -0700199 if (asoc) {
200 sctp_association_put(asoc);
201 asoc = NULL;
202 } else {
203 sctp_endpoint_put(ep);
204 ep = NULL;
205 }
206 sk = sctp_get_ctl_sock();
207 ep = sctp_sk(sk)->ep;
208 sctp_endpoint_hold(ep);
Neil Horman0fd9a652005-06-13 15:11:24 -0700209 rcvr = &ep->base;
210 }
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 /*
213 * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
214 * An SCTP packet is called an "out of the blue" (OOTB)
215 * packet if it is correctly formed, i.e., passed the
216 * receiver's checksum check, but the receiver is not
217 * able to identify the association to which this
218 * packet belongs.
219 */
220 if (!asoc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 if (sctp_rcv_ootb(skb)) {
222 SCTP_INC_STATS_BH(SCTP_MIB_OUTOFBLUES);
223 goto discard_release;
224 }
225 }
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 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;
Vlad Yasevichb56bab42006-09-29 17:09:34 -0700391 t->param_flags = (t->param_flags & ~SPP_PMTUD) |
Frank Filz52ccb8e2005-12-22 11:36:46 -0800392 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
Al Viro1c7d1fc2006-11-20 17:08:09 -0800732 hash = sctp_ep_hashfn(ntohs(laddr->v4.sin_port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 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{
Vlad Yasevichde76e692006-10-30 18:55:11 -0800777 if (asoc->temp)
778 return;
779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 sctp_local_bh_disable();
781 __sctp_hash_established(asoc);
782 sctp_local_bh_enable();
783}
784
785/* Remove association from the hash table. */
786static void __sctp_unhash_established(struct sctp_association *asoc)
787{
788 struct sctp_hashbucket *head;
789 struct sctp_ep_common *epb;
790
791 epb = &asoc->base;
792
793 epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port,
794 asoc->peer.port);
795
796 head = &sctp_assoc_hashtable[epb->hashent];
797
798 sctp_write_lock(&head->lock);
799
800 if (epb->pprev) {
801 if (epb->next)
802 epb->next->pprev = epb->pprev;
803 *epb->pprev = epb->next;
804 epb->pprev = NULL;
805 }
806
807 sctp_write_unlock(&head->lock);
808}
809
810/* Remove association from the hash table. Local BH-safe. */
811void sctp_unhash_established(struct sctp_association *asoc)
812{
Vlad Yasevichde76e692006-10-30 18:55:11 -0800813 if (asoc->temp)
814 return;
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 sctp_local_bh_disable();
817 __sctp_unhash_established(asoc);
818 sctp_local_bh_enable();
819}
820
821/* Look up an association. */
822static struct sctp_association *__sctp_lookup_association(
823 const union sctp_addr *local,
824 const union sctp_addr *peer,
825 struct sctp_transport **pt)
826{
827 struct sctp_hashbucket *head;
828 struct sctp_ep_common *epb;
829 struct sctp_association *asoc;
830 struct sctp_transport *transport;
831 int hash;
832
833 /* Optimize here for direct hit, only listening connections can
834 * have wildcards anyways.
835 */
836 hash = sctp_assoc_hashfn(local->v4.sin_port, peer->v4.sin_port);
837 head = &sctp_assoc_hashtable[hash];
838 read_lock(&head->lock);
839 for (epb = head->chain; epb; epb = epb->next) {
840 asoc = sctp_assoc(epb);
841 transport = sctp_assoc_is_match(asoc, local, peer);
842 if (transport)
843 goto hit;
844 }
845
846 read_unlock(&head->lock);
847
848 return NULL;
849
850hit:
851 *pt = transport;
852 sctp_association_hold(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 read_unlock(&head->lock);
854 return asoc;
855}
856
857/* Look up an association. BH-safe. */
858SCTP_STATIC
859struct sctp_association *sctp_lookup_association(const union sctp_addr *laddr,
860 const union sctp_addr *paddr,
861 struct sctp_transport **transportp)
862{
863 struct sctp_association *asoc;
864
865 sctp_local_bh_disable();
866 asoc = __sctp_lookup_association(laddr, paddr, transportp);
867 sctp_local_bh_enable();
868
869 return asoc;
870}
871
872/* Is there an association matching the given local and peer addresses? */
873int sctp_has_association(const union sctp_addr *laddr,
874 const union sctp_addr *paddr)
875{
876 struct sctp_association *asoc;
877 struct sctp_transport *transport;
878
879 if ((asoc = sctp_lookup_association(laddr, paddr, &transport))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 sctp_association_put(asoc);
881 return 1;
882 }
883
884 return 0;
885}
886
887/*
888 * SCTP Implementors Guide, 2.18 Handling of address
889 * parameters within the INIT or INIT-ACK.
890 *
891 * D) When searching for a matching TCB upon reception of an INIT
892 * or INIT-ACK chunk the receiver SHOULD use not only the
893 * source address of the packet (containing the INIT or
894 * INIT-ACK) but the receiver SHOULD also use all valid
895 * address parameters contained within the chunk.
896 *
897 * 2.18.3 Solution description
898 *
899 * This new text clearly specifies to an implementor the need
900 * to look within the INIT or INIT-ACK. Any implementation that
901 * does not do this, may not be able to establish associations
902 * in certain circumstances.
903 *
904 */
905static struct sctp_association *__sctp_rcv_init_lookup(struct sk_buff *skb,
906 const union sctp_addr *laddr, struct sctp_transport **transportp)
907{
908 struct sctp_association *asoc;
909 union sctp_addr addr;
910 union sctp_addr *paddr = &addr;
911 struct sctphdr *sh = (struct sctphdr *) skb->h.raw;
912 sctp_chunkhdr_t *ch;
913 union sctp_params params;
914 sctp_init_chunk_t *init;
915 struct sctp_transport *transport;
916 struct sctp_af *af;
917
918 ch = (sctp_chunkhdr_t *) skb->data;
919
920 /* If this is INIT/INIT-ACK look inside the chunk too. */
921 switch (ch->type) {
922 case SCTP_CID_INIT:
923 case SCTP_CID_INIT_ACK:
924 break;
925 default:
926 return NULL;
927 }
928
929 /* The code below will attempt to walk the chunk and extract
930 * parameter information. Before we do that, we need to verify
931 * that the chunk length doesn't cause overflow. Otherwise, we'll
932 * walk off the end.
933 */
934 if (WORD_ROUND(ntohs(ch->length)) > skb->len)
935 return NULL;
936
937 /*
938 * This code will NOT touch anything inside the chunk--it is
939 * strictly READ-ONLY.
940 *
941 * RFC 2960 3 SCTP packet Format
942 *
943 * Multiple chunks can be bundled into one SCTP packet up to
944 * the MTU size, except for the INIT, INIT ACK, and SHUTDOWN
945 * COMPLETE chunks. These chunks MUST NOT be bundled with any
946 * other chunk in a packet. See Section 6.10 for more details
947 * on chunk bundling.
948 */
949
950 /* Find the start of the TLVs and the end of the chunk. This is
951 * the region we search for address parameters.
952 */
953 init = (sctp_init_chunk_t *)skb->data;
954
955 /* Walk the parameters looking for embedded addresses. */
956 sctp_walk_params(params, init, init_hdr.params) {
957
958 /* Note: Ignoring hostname addresses. */
959 af = sctp_get_af_specific(param_type2af(params.p->type));
960 if (!af)
961 continue;
962
963 af->from_addr_param(paddr, params.addr, ntohs(sh->source), 0);
964
965 asoc = __sctp_lookup_association(laddr, paddr, &transport);
966 if (asoc)
967 return asoc;
968 }
969
970 return NULL;
971}
972
973/* Lookup an association for an inbound skb. */
974static struct sctp_association *__sctp_rcv_lookup(struct sk_buff *skb,
975 const union sctp_addr *paddr,
976 const union sctp_addr *laddr,
977 struct sctp_transport **transportp)
978{
979 struct sctp_association *asoc;
980
981 asoc = __sctp_lookup_association(laddr, paddr, transportp);
982
983 /* Further lookup for INIT/INIT-ACK packets.
984 * SCTP Implementors Guide, 2.18 Handling of address
985 * parameters within the INIT or INIT-ACK.
986 */
987 if (!asoc)
988 asoc = __sctp_rcv_init_lookup(skb, laddr, transportp);
989
990 return asoc;
991}