blob: 1662f9cc869e0a1cd463b7f3575c0c243dfc4ded [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;
144 if (sctp_rcv_checksum(skb) < 0)
145 goto discard_it;
146
147 skb_pull(skb, sizeof(struct sctphdr));
148
149 /* Make sure we at least have chunk headers worth of data left. */
150 if (skb->len < sizeof(struct sctp_chunkhdr))
151 goto discard_it;
152
153 family = ipver2af(skb->nh.iph->version);
154 af = sctp_get_af_specific(family);
155 if (unlikely(!af))
156 goto discard_it;
157
158 /* Initialize local addresses for lookups. */
159 af->from_skb(&src, skb, 1);
160 af->from_skb(&dest, skb, 0);
161
162 /* If the packet is to or from a non-unicast address,
163 * silently discard the packet.
164 *
165 * This is not clearly defined in the RFC except in section
166 * 8.4 - OOTB handling. However, based on the book "Stream Control
167 * Transmission Protocol" 2.1, "It is important to note that the
168 * IP address of an SCTP transport address must be a routable
169 * unicast address. In other words, IP multicast addresses and
170 * IP broadcast addresses cannot be used in an SCTP transport
171 * address."
172 */
173 if (!af->addr_valid(&src, NULL) || !af->addr_valid(&dest, NULL))
174 goto discard_it;
175
176 asoc = __sctp_rcv_lookup(skb, &src, &dest, &transport);
177
Neil Horman0fd9a652005-06-13 15:11:24 -0700178 if (!asoc)
179 ep = __sctp_rcv_lookup_endpoint(&dest);
180
181 /* Retrieve the common input handling substructure. */
182 rcvr = asoc ? &asoc->base : &ep->base;
183 sk = rcvr->sk;
184
185 /*
186 * If a frame arrives on an interface and the receiving socket is
187 * bound to another interface, via SO_BINDTODEVICE, treat it as OOTB
188 */
189 if (sk->sk_bound_dev_if && (sk->sk_bound_dev_if != af->skb_iif(skb)))
190 {
Neil Horman0fd9a652005-06-13 15:11:24 -0700191 if (asoc) {
192 sctp_association_put(asoc);
193 asoc = NULL;
194 } else {
195 sctp_endpoint_put(ep);
196 ep = NULL;
197 }
198 sk = sctp_get_ctl_sock();
199 ep = sctp_sk(sk)->ep;
200 sctp_endpoint_hold(ep);
Neil Horman0fd9a652005-06-13 15:11:24 -0700201 rcvr = &ep->base;
202 }
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 /*
205 * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
206 * An SCTP packet is called an "out of the blue" (OOTB)
207 * packet if it is correctly formed, i.e., passed the
208 * receiver's checksum check, but the receiver is not
209 * able to identify the association to which this
210 * packet belongs.
211 */
212 if (!asoc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if (sctp_rcv_ootb(skb)) {
214 SCTP_INC_STATS_BH(SCTP_MIB_OUTOFBLUES);
215 goto discard_release;
216 }
217 }
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 /* SCTP seems to always need a timestamp right now (FIXME) */
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700220 if (skb->tstamp.off_sec == 0) {
221 __net_timestamp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 sock_enable_timestamp(sk);
223 }
224
225 if (!xfrm_policy_check(sk, XFRM_POLICY_IN, skb, family))
226 goto discard_release;
Patrick McHardyb59c2702006-01-06 23:06:10 -0800227 nf_reset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Herbert Xu2babf9d2006-03-25 01:25:29 -0800229 if (sk_filter(sk, skb, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 goto discard_release;
231
232 /* Create an SCTP packet structure. */
233 chunk = sctp_chunkify(skb, asoc, sk);
Herbert Xu2babf9d2006-03-25 01:25:29 -0800234 if (!chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 goto discard_release;
David S. Miller79af02c2005-07-08 21:47:49 -0700236 SCTP_INPUT_CB(skb)->chunk = chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 /* Remember what endpoint is to handle this packet. */
239 chunk->rcvr = rcvr;
240
241 /* Remember the SCTP header. */
242 chunk->sctp_hdr = sh;
243
244 /* Set the source and destination addresses of the incoming chunk. */
245 sctp_init_addrs(chunk, &src, &dest);
246
247 /* Remember where we came from. */
248 chunk->transport = transport;
249
250 /* Acquire access to the sock lock. Note: We are safe from other
251 * bottom halves on this lock, but a user may be in the lock too,
252 * so check if it is busy.
253 */
254 sctp_bh_lock_sock(sk);
255
256 if (sock_owned_by_user(sk))
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700257 sctp_add_backlog(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 else
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700259 sctp_inq_push(&chunk->rcvr->inqueue, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 sctp_bh_unlock_sock(sk);
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700262
263 /* Release the asoc/ep ref we took in the lookup calls. */
264 if (asoc)
265 sctp_association_put(asoc);
266 else
267 sctp_endpoint_put(ep);
Sridhar Samudrala7a48f922006-01-17 11:51:28 -0800268
Herbert Xu2babf9d2006-03-25 01:25:29 -0800269 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271discard_it:
272 kfree_skb(skb);
Herbert Xu2babf9d2006-03-25 01:25:29 -0800273 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275discard_release:
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700276 /* Release the asoc/ep ref we took in the lookup calls. */
Neil Horman0fd9a652005-06-13 15:11:24 -0700277 if (asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 sctp_association_put(asoc);
Neil Horman0fd9a652005-06-13 15:11:24 -0700279 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 sctp_endpoint_put(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 goto discard_it;
283}
284
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700285/* Process the backlog queue of the socket. Every skb on
286 * the backlog holds a ref on an association or endpoint.
287 * We hold this ref throughout the state machine to make
288 * sure that the structure we need is still around.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 */
290int sctp_backlog_rcv(struct sock *sk, struct sk_buff *skb)
291{
David S. Miller79af02c2005-07-08 21:47:49 -0700292 struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700293 struct sctp_inq *inqueue = &chunk->rcvr->inqueue;
Sridhar Samudrala7a48f922006-01-17 11:51:28 -0800294 struct sctp_ep_common *rcvr = NULL;
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700295 int backloged = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Sridhar Samudrala7a48f922006-01-17 11:51:28 -0800297 rcvr = chunk->rcvr;
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800298
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700299 /* If the rcvr is dead then the association or endpoint
300 * has been deleted and we can safely drop the chunk
301 * and refs that we are holding.
302 */
303 if (rcvr->dead) {
304 sctp_chunk_free(chunk);
305 goto done;
306 }
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800307
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700308 if (unlikely(rcvr->sk != sk)) {
309 /* In this case, the association moved from one socket to
310 * another. We are currently sitting on the backlog of the
311 * old socket, so we need to move.
312 * However, since we are here in the process context we
313 * need to take make sure that the user doesn't own
314 * the new socket when we process the packet.
315 * If the new socket is user-owned, queue the chunk to the
316 * backlog of the new socket without dropping any refs.
317 * Otherwise, we can safely push the chunk on the inqueue.
318 */
Sridhar Samudrala7a48f922006-01-17 11:51:28 -0800319
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700320 sk = rcvr->sk;
321 sctp_bh_lock_sock(sk);
322
323 if (sock_owned_by_user(sk)) {
324 sk_add_backlog(sk, skb);
325 backloged = 1;
326 } else
327 sctp_inq_push(inqueue, chunk);
328
329 sctp_bh_unlock_sock(sk);
330
331 /* If the chunk was backloged again, don't drop refs */
332 if (backloged)
333 return 0;
334 } else {
335 sctp_inq_push(inqueue, chunk);
336 }
337
338done:
339 /* Release the refs we took in sctp_add_backlog */
340 if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
341 sctp_association_put(sctp_assoc(rcvr));
342 else if (SCTP_EP_TYPE_SOCKET == rcvr->type)
343 sctp_endpoint_put(sctp_ep(rcvr));
344 else
345 BUG();
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return 0;
348}
349
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700350static void sctp_add_backlog(struct sock *sk, struct sk_buff *skb)
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800351{
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700352 struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
353 struct sctp_ep_common *rcvr = chunk->rcvr;
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800354
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700355 /* Hold the assoc/ep while hanging on the backlog queue.
356 * This way, we know structures we need will not disappear from us
357 */
358 if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
359 sctp_association_hold(sctp_assoc(rcvr));
360 else if (SCTP_EP_TYPE_SOCKET == rcvr->type)
361 sctp_endpoint_hold(sctp_ep(rcvr));
362 else
363 BUG();
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800364
Vladislav Yasevich61c9fed2006-05-19 11:01:18 -0700365 sk_add_backlog(sk, skb);
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800366}
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368/* Handle icmp frag needed error. */
369void sctp_icmp_frag_needed(struct sock *sk, struct sctp_association *asoc,
370 struct sctp_transport *t, __u32 pmtu)
371{
Frank Filz52ccb8e2005-12-22 11:36:46 -0800372 if (sock_owned_by_user(sk) || !t || (t->pathmtu == pmtu))
373 return;
374
375 if (t->param_flags & SPP_PMTUD_ENABLE) {
376 if (unlikely(pmtu < SCTP_DEFAULT_MINSEGMENT)) {
377 printk(KERN_WARNING "%s: Reported pmtu %d too low, "
378 "using default minimum of %d\n",
379 __FUNCTION__, pmtu,
380 SCTP_DEFAULT_MINSEGMENT);
381 /* Use default minimum segment size and disable
382 * pmtu discovery on this transport.
383 */
384 t->pathmtu = SCTP_DEFAULT_MINSEGMENT;
385 t->param_flags = (t->param_flags & ~SPP_HB) |
386 SPP_PMTUD_DISABLE;
387 } else {
388 t->pathmtu = pmtu;
389 }
390
391 /* Update association pmtu. */
392 sctp_assoc_sync_pmtu(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
394
Frank Filz52ccb8e2005-12-22 11:36:46 -0800395 /* Retransmit with the new pmtu setting.
396 * Normally, if PMTU discovery is disabled, an ICMP Fragmentation
397 * Needed will never be sent, but if a message was sent before
398 * PMTU discovery was disabled that was larger than the PMTU, it
399 * would not be fragmented, so it must be re-transmitted fragmented.
400 */
401 sctp_retransmit(&asoc->outqueue, t, SCTP_RTXR_PMTUD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
404/*
405 * SCTP Implementer's Guide, 2.37 ICMP handling procedures
406 *
407 * ICMP8) If the ICMP code is a "Unrecognized next header type encountered"
408 * or a "Protocol Unreachable" treat this message as an abort
409 * with the T bit set.
410 *
411 * This function sends an event to the state machine, which will abort the
412 * association.
413 *
414 */
415void sctp_icmp_proto_unreachable(struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 struct sctp_association *asoc,
417 struct sctp_transport *t)
418{
419 SCTP_DEBUG_PRINTK("%s\n", __FUNCTION__);
420
421 sctp_do_sm(SCTP_EVENT_T_OTHER,
422 SCTP_ST_OTHER(SCTP_EVENT_ICMP_PROTO_UNREACH),
Frank Filz3f7a87d2005-06-20 13:14:57 -0700423 asoc->state, asoc->ep, asoc, t,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 GFP_ATOMIC);
425
426}
427
428/* Common lookup code for icmp/icmpv6 error handler. */
429struct sock *sctp_err_lookup(int family, struct sk_buff *skb,
430 struct sctphdr *sctphdr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 struct sctp_association **app,
432 struct sctp_transport **tpp)
433{
434 union sctp_addr saddr;
435 union sctp_addr daddr;
436 struct sctp_af *af;
437 struct sock *sk = NULL;
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700438 struct sctp_association *asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 struct sctp_transport *transport = NULL;
440
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700441 *app = NULL; *tpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 af = sctp_get_af_specific(family);
444 if (unlikely(!af)) {
445 return NULL;
446 }
447
448 /* Initialize local addresses for lookups. */
449 af->from_skb(&saddr, skb, 1);
450 af->from_skb(&daddr, skb, 0);
451
452 /* Look for an association that matches the incoming ICMP error
453 * packet.
454 */
455 asoc = __sctp_lookup_association(&saddr, &daddr, &transport);
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700456 if (!asoc)
457 return NULL;
458
459 sk = asoc->base.sk;
460
461 if (ntohl(sctphdr->vtag) != asoc->c.peer_vtag) {
462 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
463 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 sctp_bh_lock_sock(sk);
467
468 /* If too many ICMPs get dropped on busy
469 * servers this needs to be solved differently.
470 */
471 if (sock_owned_by_user(sk))
472 NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 *app = asoc;
475 *tpp = transport;
476 return sk;
477
478out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (asoc)
480 sctp_association_put(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 return NULL;
482}
483
484/* Common cleanup code for icmp/icmpv6 error handler. */
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700485void sctp_err_finish(struct sock *sk, struct sctp_association *asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
487 sctp_bh_unlock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (asoc)
489 sctp_association_put(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
492/*
493 * This routine is called by the ICMP module when it gets some
494 * sort of error condition. If err < 0 then the socket should
495 * be closed and the error returned to the user. If err > 0
496 * it's just the icmp type << 8 | icmp code. After adjustment
497 * header points to the first 8 bytes of the sctp header. We need
498 * to find the appropriate port.
499 *
500 * The locking strategy used here is very "optimistic". When
501 * someone else accesses the socket the ICMP is just dropped
502 * and for some paths there is no check at all.
503 * A more general error queue to queue errors for later handling
504 * is probably better.
505 *
506 */
507void sctp_v4_err(struct sk_buff *skb, __u32 info)
508{
509 struct iphdr *iph = (struct iphdr *)skb->data;
510 struct sctphdr *sh = (struct sctphdr *)(skb->data + (iph->ihl <<2));
511 int type = skb->h.icmph->type;
512 int code = skb->h.icmph->code;
513 struct sock *sk;
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700514 struct sctp_association *asoc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 struct sctp_transport *transport;
516 struct inet_sock *inet;
517 char *saveip, *savesctp;
518 int err;
519
520 if (skb->len < ((iph->ihl << 2) + 8)) {
521 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
522 return;
523 }
524
525 /* Fix up skb to look at the embedded net header. */
526 saveip = skb->nh.raw;
527 savesctp = skb->h.raw;
528 skb->nh.iph = iph;
529 skb->h.raw = (char *)sh;
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700530 sk = sctp_err_lookup(AF_INET, skb, sh, &asoc, &transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 /* Put back, the original pointers. */
532 skb->nh.raw = saveip;
533 skb->h.raw = savesctp;
534 if (!sk) {
535 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
536 return;
537 }
538 /* Warning: The sock lock is held. Remember to call
539 * sctp_err_finish!
540 */
541
542 switch (type) {
543 case ICMP_PARAMETERPROB:
544 err = EPROTO;
545 break;
546 case ICMP_DEST_UNREACH:
547 if (code > NR_ICMP_UNREACH)
548 goto out_unlock;
549
550 /* PMTU discovery (RFC1191) */
551 if (ICMP_FRAG_NEEDED == code) {
552 sctp_icmp_frag_needed(sk, asoc, transport, info);
553 goto out_unlock;
554 }
555 else {
556 if (ICMP_PROT_UNREACH == code) {
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700557 sctp_icmp_proto_unreachable(sk, asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 transport);
559 goto out_unlock;
560 }
561 }
562 err = icmp_err_convert[code].errno;
563 break;
564 case ICMP_TIME_EXCEEDED:
565 /* Ignore any time exceeded errors due to fragment reassembly
566 * timeouts.
567 */
568 if (ICMP_EXC_FRAGTIME == code)
569 goto out_unlock;
570
571 err = EHOSTUNREACH;
572 break;
573 default:
574 goto out_unlock;
575 }
576
577 inet = inet_sk(sk);
578 if (!sock_owned_by_user(sk) && inet->recverr) {
579 sk->sk_err = err;
580 sk->sk_error_report(sk);
581 } else { /* Only an error on timeout */
582 sk->sk_err_soft = err;
583 }
584
585out_unlock:
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700586 sctp_err_finish(sk, asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
588
589/*
590 * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
591 *
592 * This function scans all the chunks in the OOTB packet to determine if
593 * the packet should be discarded right away. If a response might be needed
594 * for this packet, or, if further processing is possible, the packet will
595 * be queued to a proper inqueue for the next phase of handling.
596 *
597 * Output:
598 * Return 0 - If further processing is needed.
599 * Return 1 - If the packet can be discarded right away.
600 */
601int sctp_rcv_ootb(struct sk_buff *skb)
602{
603 sctp_chunkhdr_t *ch;
604 __u8 *ch_end;
605 sctp_errhdr_t *err;
606
607 ch = (sctp_chunkhdr_t *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 /* Scan through all the chunks in the packet. */
Tsutomu Fujiia7d1f1b2006-01-17 11:57:09 -0800610 do {
611 /* Break out if chunk length is less then minimal. */
612 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
613 break;
614
615 ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
616 if (ch_end > skb->tail)
617 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 /* RFC 8.4, 2) If the OOTB packet contains an ABORT chunk, the
620 * receiver MUST silently discard the OOTB packet and take no
621 * further action.
622 */
623 if (SCTP_CID_ABORT == ch->type)
624 goto discard;
625
626 /* RFC 8.4, 6) If the packet contains a SHUTDOWN COMPLETE
627 * chunk, the receiver should silently discard the packet
628 * and take no further action.
629 */
630 if (SCTP_CID_SHUTDOWN_COMPLETE == ch->type)
631 goto discard;
632
633 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
634 * or a COOKIE ACK the SCTP Packet should be silently
635 * discarded.
636 */
637 if (SCTP_CID_COOKIE_ACK == ch->type)
638 goto discard;
639
640 if (SCTP_CID_ERROR == ch->type) {
641 sctp_walk_errors(err, ch) {
642 if (SCTP_ERROR_STALE_COOKIE == err->cause)
643 goto discard;
644 }
645 }
646
647 ch = (sctp_chunkhdr_t *) ch_end;
Tsutomu Fujiia7d1f1b2006-01-17 11:57:09 -0800648 } while (ch_end < skb->tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 return 0;
651
652discard:
653 return 1;
654}
655
656/* Insert endpoint into the hash table. */
657static void __sctp_hash_endpoint(struct sctp_endpoint *ep)
658{
659 struct sctp_ep_common **epp;
660 struct sctp_ep_common *epb;
661 struct sctp_hashbucket *head;
662
663 epb = &ep->base;
664
665 epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);
666 head = &sctp_ep_hashtable[epb->hashent];
667
668 sctp_write_lock(&head->lock);
669 epp = &head->chain;
670 epb->next = *epp;
671 if (epb->next)
672 (*epp)->pprev = &epb->next;
673 *epp = epb;
674 epb->pprev = epp;
675 sctp_write_unlock(&head->lock);
676}
677
678/* Add an endpoint to the hash. Local BH-safe. */
679void sctp_hash_endpoint(struct sctp_endpoint *ep)
680{
681 sctp_local_bh_disable();
682 __sctp_hash_endpoint(ep);
683 sctp_local_bh_enable();
684}
685
686/* Remove endpoint from the hash table. */
687static void __sctp_unhash_endpoint(struct sctp_endpoint *ep)
688{
689 struct sctp_hashbucket *head;
690 struct sctp_ep_common *epb;
691
692 epb = &ep->base;
693
694 epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);
695
696 head = &sctp_ep_hashtable[epb->hashent];
697
698 sctp_write_lock(&head->lock);
699
700 if (epb->pprev) {
701 if (epb->next)
702 epb->next->pprev = epb->pprev;
703 *epb->pprev = epb->next;
704 epb->pprev = NULL;
705 }
706
707 sctp_write_unlock(&head->lock);
708}
709
710/* Remove endpoint from the hash. Local BH-safe. */
711void sctp_unhash_endpoint(struct sctp_endpoint *ep)
712{
713 sctp_local_bh_disable();
714 __sctp_unhash_endpoint(ep);
715 sctp_local_bh_enable();
716}
717
718/* Look up an endpoint. */
719static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(const union sctp_addr *laddr)
720{
721 struct sctp_hashbucket *head;
722 struct sctp_ep_common *epb;
723 struct sctp_endpoint *ep;
724 int hash;
725
726 hash = sctp_ep_hashfn(laddr->v4.sin_port);
727 head = &sctp_ep_hashtable[hash];
728 read_lock(&head->lock);
729 for (epb = head->chain; epb; epb = epb->next) {
730 ep = sctp_ep(epb);
731 if (sctp_endpoint_is_match(ep, laddr))
732 goto hit;
733 }
734
735 ep = sctp_sk((sctp_get_ctl_sock()))->ep;
736 epb = &ep->base;
737
738hit:
739 sctp_endpoint_hold(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 read_unlock(&head->lock);
741 return ep;
742}
743
744/* Insert association into the hash table. */
745static void __sctp_hash_established(struct sctp_association *asoc)
746{
747 struct sctp_ep_common **epp;
748 struct sctp_ep_common *epb;
749 struct sctp_hashbucket *head;
750
751 epb = &asoc->base;
752
753 /* Calculate which chain this entry will belong to. */
754 epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port, asoc->peer.port);
755
756 head = &sctp_assoc_hashtable[epb->hashent];
757
758 sctp_write_lock(&head->lock);
759 epp = &head->chain;
760 epb->next = *epp;
761 if (epb->next)
762 (*epp)->pprev = &epb->next;
763 *epp = epb;
764 epb->pprev = epp;
765 sctp_write_unlock(&head->lock);
766}
767
768/* Add an association to the hash. Local BH-safe. */
769void sctp_hash_established(struct sctp_association *asoc)
770{
771 sctp_local_bh_disable();
772 __sctp_hash_established(asoc);
773 sctp_local_bh_enable();
774}
775
776/* Remove association from the hash table. */
777static void __sctp_unhash_established(struct sctp_association *asoc)
778{
779 struct sctp_hashbucket *head;
780 struct sctp_ep_common *epb;
781
782 epb = &asoc->base;
783
784 epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port,
785 asoc->peer.port);
786
787 head = &sctp_assoc_hashtable[epb->hashent];
788
789 sctp_write_lock(&head->lock);
790
791 if (epb->pprev) {
792 if (epb->next)
793 epb->next->pprev = epb->pprev;
794 *epb->pprev = epb->next;
795 epb->pprev = NULL;
796 }
797
798 sctp_write_unlock(&head->lock);
799}
800
801/* Remove association from the hash table. Local BH-safe. */
802void sctp_unhash_established(struct sctp_association *asoc)
803{
804 sctp_local_bh_disable();
805 __sctp_unhash_established(asoc);
806 sctp_local_bh_enable();
807}
808
809/* Look up an association. */
810static struct sctp_association *__sctp_lookup_association(
811 const union sctp_addr *local,
812 const union sctp_addr *peer,
813 struct sctp_transport **pt)
814{
815 struct sctp_hashbucket *head;
816 struct sctp_ep_common *epb;
817 struct sctp_association *asoc;
818 struct sctp_transport *transport;
819 int hash;
820
821 /* Optimize here for direct hit, only listening connections can
822 * have wildcards anyways.
823 */
824 hash = sctp_assoc_hashfn(local->v4.sin_port, peer->v4.sin_port);
825 head = &sctp_assoc_hashtable[hash];
826 read_lock(&head->lock);
827 for (epb = head->chain; epb; epb = epb->next) {
828 asoc = sctp_assoc(epb);
829 transport = sctp_assoc_is_match(asoc, local, peer);
830 if (transport)
831 goto hit;
832 }
833
834 read_unlock(&head->lock);
835
836 return NULL;
837
838hit:
839 *pt = transport;
840 sctp_association_hold(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 read_unlock(&head->lock);
842 return asoc;
843}
844
845/* Look up an association. BH-safe. */
846SCTP_STATIC
847struct sctp_association *sctp_lookup_association(const union sctp_addr *laddr,
848 const union sctp_addr *paddr,
849 struct sctp_transport **transportp)
850{
851 struct sctp_association *asoc;
852
853 sctp_local_bh_disable();
854 asoc = __sctp_lookup_association(laddr, paddr, transportp);
855 sctp_local_bh_enable();
856
857 return asoc;
858}
859
860/* Is there an association matching the given local and peer addresses? */
861int sctp_has_association(const union sctp_addr *laddr,
862 const union sctp_addr *paddr)
863{
864 struct sctp_association *asoc;
865 struct sctp_transport *transport;
866
867 if ((asoc = sctp_lookup_association(laddr, paddr, &transport))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 sctp_association_put(asoc);
869 return 1;
870 }
871
872 return 0;
873}
874
875/*
876 * SCTP Implementors Guide, 2.18 Handling of address
877 * parameters within the INIT or INIT-ACK.
878 *
879 * D) When searching for a matching TCB upon reception of an INIT
880 * or INIT-ACK chunk the receiver SHOULD use not only the
881 * source address of the packet (containing the INIT or
882 * INIT-ACK) but the receiver SHOULD also use all valid
883 * address parameters contained within the chunk.
884 *
885 * 2.18.3 Solution description
886 *
887 * This new text clearly specifies to an implementor the need
888 * to look within the INIT or INIT-ACK. Any implementation that
889 * does not do this, may not be able to establish associations
890 * in certain circumstances.
891 *
892 */
893static struct sctp_association *__sctp_rcv_init_lookup(struct sk_buff *skb,
894 const union sctp_addr *laddr, struct sctp_transport **transportp)
895{
896 struct sctp_association *asoc;
897 union sctp_addr addr;
898 union sctp_addr *paddr = &addr;
899 struct sctphdr *sh = (struct sctphdr *) skb->h.raw;
900 sctp_chunkhdr_t *ch;
901 union sctp_params params;
902 sctp_init_chunk_t *init;
903 struct sctp_transport *transport;
904 struct sctp_af *af;
905
906 ch = (sctp_chunkhdr_t *) skb->data;
907
908 /* If this is INIT/INIT-ACK look inside the chunk too. */
909 switch (ch->type) {
910 case SCTP_CID_INIT:
911 case SCTP_CID_INIT_ACK:
912 break;
913 default:
914 return NULL;
915 }
916
917 /* The code below will attempt to walk the chunk and extract
918 * parameter information. Before we do that, we need to verify
919 * that the chunk length doesn't cause overflow. Otherwise, we'll
920 * walk off the end.
921 */
922 if (WORD_ROUND(ntohs(ch->length)) > skb->len)
923 return NULL;
924
925 /*
926 * This code will NOT touch anything inside the chunk--it is
927 * strictly READ-ONLY.
928 *
929 * RFC 2960 3 SCTP packet Format
930 *
931 * Multiple chunks can be bundled into one SCTP packet up to
932 * the MTU size, except for the INIT, INIT ACK, and SHUTDOWN
933 * COMPLETE chunks. These chunks MUST NOT be bundled with any
934 * other chunk in a packet. See Section 6.10 for more details
935 * on chunk bundling.
936 */
937
938 /* Find the start of the TLVs and the end of the chunk. This is
939 * the region we search for address parameters.
940 */
941 init = (sctp_init_chunk_t *)skb->data;
942
943 /* Walk the parameters looking for embedded addresses. */
944 sctp_walk_params(params, init, init_hdr.params) {
945
946 /* Note: Ignoring hostname addresses. */
947 af = sctp_get_af_specific(param_type2af(params.p->type));
948 if (!af)
949 continue;
950
951 af->from_addr_param(paddr, params.addr, ntohs(sh->source), 0);
952
953 asoc = __sctp_lookup_association(laddr, paddr, &transport);
954 if (asoc)
955 return asoc;
956 }
957
958 return NULL;
959}
960
961/* Lookup an association for an inbound skb. */
962static struct sctp_association *__sctp_rcv_lookup(struct sk_buff *skb,
963 const union sctp_addr *paddr,
964 const union sctp_addr *laddr,
965 struct sctp_transport **transportp)
966{
967 struct sctp_association *asoc;
968
969 asoc = __sctp_lookup_association(laddr, paddr, transportp);
970
971 /* Further lookup for INIT/INIT-ACK packets.
972 * SCTP Implementors Guide, 2.18 Handling of address
973 * parameters within the INIT or INIT-ACK.
974 */
975 if (!asoc)
976 asoc = __sctp_rcv_init_lookup(skb, laddr, transportp);
977
978 return asoc;
979}