blob: d117ebc75cf87ce25aa8699f61b4338b12fc1053 [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
76
77/* Calculate the SCTP checksum of an SCTP packet. */
78static inline int sctp_rcv_checksum(struct sk_buff *skb)
79{
80 struct sctphdr *sh;
81 __u32 cmp, val;
82 struct sk_buff *list = skb_shinfo(skb)->frag_list;
83
84 sh = (struct sctphdr *) skb->h.raw;
85 cmp = ntohl(sh->checksum);
86
87 val = sctp_start_cksum((__u8 *)sh, skb_headlen(skb));
88
89 for (; list; list = list->next)
90 val = sctp_update_cksum((__u8 *)list->data, skb_headlen(list),
91 val);
92
93 val = sctp_end_cksum(val);
94
95 if (val != cmp) {
96 /* CRC failure, dump it. */
97 SCTP_INC_STATS_BH(SCTP_MIB_CHECKSUMERRORS);
98 return -1;
99 }
100 return 0;
101}
102
David S. Miller79af02c2005-07-08 21:47:49 -0700103struct sctp_input_cb {
104 union {
105 struct inet_skb_parm h4;
106#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
107 struct inet6_skb_parm h6;
108#endif
109 } header;
110 struct sctp_chunk *chunk;
111};
112#define SCTP_INPUT_CB(__skb) ((struct sctp_input_cb *)&((__skb)->cb[0]))
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114/*
115 * This is the routine which IP calls when receiving an SCTP packet.
116 */
117int sctp_rcv(struct sk_buff *skb)
118{
119 struct sock *sk;
120 struct sctp_association *asoc;
121 struct sctp_endpoint *ep = NULL;
122 struct sctp_ep_common *rcvr;
123 struct sctp_transport *transport = NULL;
124 struct sctp_chunk *chunk;
125 struct sctphdr *sh;
126 union sctp_addr src;
127 union sctp_addr dest;
128 int family;
129 struct sctp_af *af;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 if (skb->pkt_type!=PACKET_HOST)
132 goto discard_it;
133
134 SCTP_INC_STATS_BH(SCTP_MIB_INSCTPPACKS);
135
136 sh = (struct sctphdr *) skb->h.raw;
137
138 /* Pull up the IP and SCTP headers. */
139 __skb_pull(skb, skb->h.raw - skb->data);
140 if (skb->len < sizeof(struct sctphdr))
141 goto discard_it;
142 if (sctp_rcv_checksum(skb) < 0)
143 goto discard_it;
144
145 skb_pull(skb, sizeof(struct sctphdr));
146
147 /* Make sure we at least have chunk headers worth of data left. */
148 if (skb->len < sizeof(struct sctp_chunkhdr))
149 goto discard_it;
150
151 family = ipver2af(skb->nh.iph->version);
152 af = sctp_get_af_specific(family);
153 if (unlikely(!af))
154 goto discard_it;
155
156 /* Initialize local addresses for lookups. */
157 af->from_skb(&src, skb, 1);
158 af->from_skb(&dest, skb, 0);
159
160 /* If the packet is to or from a non-unicast address,
161 * silently discard the packet.
162 *
163 * This is not clearly defined in the RFC except in section
164 * 8.4 - OOTB handling. However, based on the book "Stream Control
165 * Transmission Protocol" 2.1, "It is important to note that the
166 * IP address of an SCTP transport address must be a routable
167 * unicast address. In other words, IP multicast addresses and
168 * IP broadcast addresses cannot be used in an SCTP transport
169 * address."
170 */
171 if (!af->addr_valid(&src, NULL) || !af->addr_valid(&dest, NULL))
172 goto discard_it;
173
174 asoc = __sctp_rcv_lookup(skb, &src, &dest, &transport);
175
Neil Horman0fd9a652005-06-13 15:11:24 -0700176 if (!asoc)
177 ep = __sctp_rcv_lookup_endpoint(&dest);
178
179 /* Retrieve the common input handling substructure. */
180 rcvr = asoc ? &asoc->base : &ep->base;
181 sk = rcvr->sk;
182
183 /*
184 * If a frame arrives on an interface and the receiving socket is
185 * bound to another interface, via SO_BINDTODEVICE, treat it as OOTB
186 */
187 if (sk->sk_bound_dev_if && (sk->sk_bound_dev_if != af->skb_iif(skb)))
188 {
189 sock_put(sk);
190 if (asoc) {
191 sctp_association_put(asoc);
192 asoc = NULL;
193 } else {
194 sctp_endpoint_put(ep);
195 ep = NULL;
196 }
197 sk = sctp_get_ctl_sock();
198 ep = sctp_sk(sk)->ep;
199 sctp_endpoint_hold(ep);
200 sock_hold(sk);
201 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
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800256 /* It is possible that the association could have moved to a different
257 * socket if it is peeled off. If so, update the sk.
258 */
259 if (sk != rcvr->sk) {
260 sctp_bh_lock_sock(rcvr->sk);
261 sctp_bh_unlock_sock(sk);
262 sk = rcvr->sk;
263 }
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (sock_owned_by_user(sk))
David S. Miller79af02c2005-07-08 21:47:49 -0700266 sk_add_backlog(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 else
David S. Miller79af02c2005-07-08 21:47:49 -0700268 sctp_backlog_rcv(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800270 /* Release the sock and the sock ref we took in the lookup calls.
Sridhar Samudrala7a48f922006-01-17 11:51:28 -0800271 * The asoc/ep ref will be released in sctp_backlog_rcv.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 */
273 sctp_bh_unlock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 sock_put(sk);
Sridhar Samudrala7a48f922006-01-17 11:51:28 -0800275
Herbert Xu2babf9d2006-03-25 01:25:29 -0800276 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278discard_it:
279 kfree_skb(skb);
Herbert Xu2babf9d2006-03-25 01:25:29 -0800280 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282discard_release:
283 /* Release any structures we may be holding. */
Neil Horman0fd9a652005-06-13 15:11:24 -0700284 sock_put(sk);
285 if (asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 sctp_association_put(asoc);
Neil Horman0fd9a652005-06-13 15:11:24 -0700287 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 sctp_endpoint_put(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 goto discard_it;
291}
292
293/* Handle second half of inbound skb processing. If the sock was busy,
294 * we may have need to delay processing until later when the sock is
295 * released (on the backlog). If not busy, we call this routine
296 * directly from the bottom half.
297 */
298int sctp_backlog_rcv(struct sock *sk, struct sk_buff *skb)
299{
David S. Miller79af02c2005-07-08 21:47:49 -0700300 struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
Sridhar Samudrala7a48f922006-01-17 11:51:28 -0800301 struct sctp_inq *inqueue = NULL;
302 struct sctp_ep_common *rcvr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Sridhar Samudrala7a48f922006-01-17 11:51:28 -0800304 rcvr = chunk->rcvr;
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800305
306 BUG_TRAP(rcvr->sk == sk);
307
Sridhar Samudrala7a48f922006-01-17 11:51:28 -0800308 if (rcvr->dead) {
309 sctp_chunk_free(chunk);
310 } else {
311 inqueue = &chunk->rcvr->inqueue;
312 sctp_inq_push(inqueue, chunk);
313 }
314
315 /* Release the asoc/ep ref we took in the lookup calls in sctp_rcv. */
316 if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
317 sctp_association_put(sctp_assoc(rcvr));
318 else
319 sctp_endpoint_put(sctp_ep(rcvr));
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 return 0;
322}
323
Sridhar Samudralac4d24442006-01-17 11:56:26 -0800324void sctp_backlog_migrate(struct sctp_association *assoc,
325 struct sock *oldsk, struct sock *newsk)
326{
327 struct sk_buff *skb;
328 struct sctp_chunk *chunk;
329
330 skb = oldsk->sk_backlog.head;
331 oldsk->sk_backlog.head = oldsk->sk_backlog.tail = NULL;
332 while (skb != NULL) {
333 struct sk_buff *next = skb->next;
334
335 chunk = SCTP_INPUT_CB(skb)->chunk;
336 skb->next = NULL;
337 if (&assoc->base == chunk->rcvr)
338 sk_add_backlog(newsk, skb);
339 else
340 sk_add_backlog(oldsk, skb);
341 skb = next;
342 }
343}
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345/* Handle icmp frag needed error. */
346void sctp_icmp_frag_needed(struct sock *sk, struct sctp_association *asoc,
347 struct sctp_transport *t, __u32 pmtu)
348{
Frank Filz52ccb8e2005-12-22 11:36:46 -0800349 if (sock_owned_by_user(sk) || !t || (t->pathmtu == pmtu))
350 return;
351
352 if (t->param_flags & SPP_PMTUD_ENABLE) {
353 if (unlikely(pmtu < SCTP_DEFAULT_MINSEGMENT)) {
354 printk(KERN_WARNING "%s: Reported pmtu %d too low, "
355 "using default minimum of %d\n",
356 __FUNCTION__, pmtu,
357 SCTP_DEFAULT_MINSEGMENT);
358 /* Use default minimum segment size and disable
359 * pmtu discovery on this transport.
360 */
361 t->pathmtu = SCTP_DEFAULT_MINSEGMENT;
362 t->param_flags = (t->param_flags & ~SPP_HB) |
363 SPP_PMTUD_DISABLE;
364 } else {
365 t->pathmtu = pmtu;
366 }
367
368 /* Update association pmtu. */
369 sctp_assoc_sync_pmtu(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 }
371
Frank Filz52ccb8e2005-12-22 11:36:46 -0800372 /* Retransmit with the new pmtu setting.
373 * Normally, if PMTU discovery is disabled, an ICMP Fragmentation
374 * Needed will never be sent, but if a message was sent before
375 * PMTU discovery was disabled that was larger than the PMTU, it
376 * would not be fragmented, so it must be re-transmitted fragmented.
377 */
378 sctp_retransmit(&asoc->outqueue, t, SCTP_RTXR_PMTUD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
381/*
382 * SCTP Implementer's Guide, 2.37 ICMP handling procedures
383 *
384 * ICMP8) If the ICMP code is a "Unrecognized next header type encountered"
385 * or a "Protocol Unreachable" treat this message as an abort
386 * with the T bit set.
387 *
388 * This function sends an event to the state machine, which will abort the
389 * association.
390 *
391 */
392void sctp_icmp_proto_unreachable(struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 struct sctp_association *asoc,
394 struct sctp_transport *t)
395{
396 SCTP_DEBUG_PRINTK("%s\n", __FUNCTION__);
397
398 sctp_do_sm(SCTP_EVENT_T_OTHER,
399 SCTP_ST_OTHER(SCTP_EVENT_ICMP_PROTO_UNREACH),
Frank Filz3f7a87d2005-06-20 13:14:57 -0700400 asoc->state, asoc->ep, asoc, t,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 GFP_ATOMIC);
402
403}
404
405/* Common lookup code for icmp/icmpv6 error handler. */
406struct sock *sctp_err_lookup(int family, struct sk_buff *skb,
407 struct sctphdr *sctphdr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 struct sctp_association **app,
409 struct sctp_transport **tpp)
410{
411 union sctp_addr saddr;
412 union sctp_addr daddr;
413 struct sctp_af *af;
414 struct sock *sk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 struct sctp_association *asoc = NULL;
416 struct sctp_transport *transport = NULL;
417
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700418 *app = NULL; *tpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 af = sctp_get_af_specific(family);
421 if (unlikely(!af)) {
422 return NULL;
423 }
424
425 /* Initialize local addresses for lookups. */
426 af->from_skb(&saddr, skb, 1);
427 af->from_skb(&daddr, skb, 0);
428
429 /* Look for an association that matches the incoming ICMP error
430 * packet.
431 */
432 asoc = __sctp_lookup_association(&saddr, &daddr, &transport);
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700433 if (!asoc)
434 return NULL;
435
436 sk = asoc->base.sk;
437
438 if (ntohl(sctphdr->vtag) != asoc->c.peer_vtag) {
439 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
440 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 sctp_bh_lock_sock(sk);
444
445 /* If too many ICMPs get dropped on busy
446 * servers this needs to be solved differently.
447 */
448 if (sock_owned_by_user(sk))
449 NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 *app = asoc;
452 *tpp = transport;
453 return sk;
454
455out:
456 sock_put(sk);
457 if (asoc)
458 sctp_association_put(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return NULL;
460}
461
462/* Common cleanup code for icmp/icmpv6 error handler. */
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700463void sctp_err_finish(struct sock *sk, struct sctp_association *asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
465 sctp_bh_unlock_sock(sk);
466 sock_put(sk);
467 if (asoc)
468 sctp_association_put(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
471/*
472 * This routine is called by the ICMP module when it gets some
473 * sort of error condition. If err < 0 then the socket should
474 * be closed and the error returned to the user. If err > 0
475 * it's just the icmp type << 8 | icmp code. After adjustment
476 * header points to the first 8 bytes of the sctp header. We need
477 * to find the appropriate port.
478 *
479 * The locking strategy used here is very "optimistic". When
480 * someone else accesses the socket the ICMP is just dropped
481 * and for some paths there is no check at all.
482 * A more general error queue to queue errors for later handling
483 * is probably better.
484 *
485 */
486void sctp_v4_err(struct sk_buff *skb, __u32 info)
487{
488 struct iphdr *iph = (struct iphdr *)skb->data;
489 struct sctphdr *sh = (struct sctphdr *)(skb->data + (iph->ihl <<2));
490 int type = skb->h.icmph->type;
491 int code = skb->h.icmph->code;
492 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 struct sctp_association *asoc;
494 struct sctp_transport *transport;
495 struct inet_sock *inet;
496 char *saveip, *savesctp;
497 int err;
498
499 if (skb->len < ((iph->ihl << 2) + 8)) {
500 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
501 return;
502 }
503
504 /* Fix up skb to look at the embedded net header. */
505 saveip = skb->nh.raw;
506 savesctp = skb->h.raw;
507 skb->nh.iph = iph;
508 skb->h.raw = (char *)sh;
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700509 sk = sctp_err_lookup(AF_INET, skb, sh, &asoc, &transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 /* Put back, the original pointers. */
511 skb->nh.raw = saveip;
512 skb->h.raw = savesctp;
513 if (!sk) {
514 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
515 return;
516 }
517 /* Warning: The sock lock is held. Remember to call
518 * sctp_err_finish!
519 */
520
521 switch (type) {
522 case ICMP_PARAMETERPROB:
523 err = EPROTO;
524 break;
525 case ICMP_DEST_UNREACH:
526 if (code > NR_ICMP_UNREACH)
527 goto out_unlock;
528
529 /* PMTU discovery (RFC1191) */
530 if (ICMP_FRAG_NEEDED == code) {
531 sctp_icmp_frag_needed(sk, asoc, transport, info);
532 goto out_unlock;
533 }
534 else {
535 if (ICMP_PROT_UNREACH == code) {
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700536 sctp_icmp_proto_unreachable(sk, asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 transport);
538 goto out_unlock;
539 }
540 }
541 err = icmp_err_convert[code].errno;
542 break;
543 case ICMP_TIME_EXCEEDED:
544 /* Ignore any time exceeded errors due to fragment reassembly
545 * timeouts.
546 */
547 if (ICMP_EXC_FRAGTIME == code)
548 goto out_unlock;
549
550 err = EHOSTUNREACH;
551 break;
552 default:
553 goto out_unlock;
554 }
555
556 inet = inet_sk(sk);
557 if (!sock_owned_by_user(sk) && inet->recverr) {
558 sk->sk_err = err;
559 sk->sk_error_report(sk);
560 } else { /* Only an error on timeout */
561 sk->sk_err_soft = err;
562 }
563
564out_unlock:
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700565 sctp_err_finish(sk, asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
568/*
569 * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
570 *
571 * This function scans all the chunks in the OOTB packet to determine if
572 * the packet should be discarded right away. If a response might be needed
573 * for this packet, or, if further processing is possible, the packet will
574 * be queued to a proper inqueue for the next phase of handling.
575 *
576 * Output:
577 * Return 0 - If further processing is needed.
578 * Return 1 - If the packet can be discarded right away.
579 */
580int sctp_rcv_ootb(struct sk_buff *skb)
581{
582 sctp_chunkhdr_t *ch;
583 __u8 *ch_end;
584 sctp_errhdr_t *err;
585
586 ch = (sctp_chunkhdr_t *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 /* Scan through all the chunks in the packet. */
Tsutomu Fujiia7d1f1b2006-01-17 11:57:09 -0800589 do {
590 /* Break out if chunk length is less then minimal. */
591 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
592 break;
593
594 ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
595 if (ch_end > skb->tail)
596 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 /* RFC 8.4, 2) If the OOTB packet contains an ABORT chunk, the
599 * receiver MUST silently discard the OOTB packet and take no
600 * further action.
601 */
602 if (SCTP_CID_ABORT == ch->type)
603 goto discard;
604
605 /* RFC 8.4, 6) If the packet contains a SHUTDOWN COMPLETE
606 * chunk, the receiver should silently discard the packet
607 * and take no further action.
608 */
609 if (SCTP_CID_SHUTDOWN_COMPLETE == ch->type)
610 goto discard;
611
612 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
613 * or a COOKIE ACK the SCTP Packet should be silently
614 * discarded.
615 */
616 if (SCTP_CID_COOKIE_ACK == ch->type)
617 goto discard;
618
619 if (SCTP_CID_ERROR == ch->type) {
620 sctp_walk_errors(err, ch) {
621 if (SCTP_ERROR_STALE_COOKIE == err->cause)
622 goto discard;
623 }
624 }
625
626 ch = (sctp_chunkhdr_t *) ch_end;
Tsutomu Fujiia7d1f1b2006-01-17 11:57:09 -0800627 } while (ch_end < skb->tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 return 0;
630
631discard:
632 return 1;
633}
634
635/* Insert endpoint into the hash table. */
636static void __sctp_hash_endpoint(struct sctp_endpoint *ep)
637{
638 struct sctp_ep_common **epp;
639 struct sctp_ep_common *epb;
640 struct sctp_hashbucket *head;
641
642 epb = &ep->base;
643
644 epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);
645 head = &sctp_ep_hashtable[epb->hashent];
646
647 sctp_write_lock(&head->lock);
648 epp = &head->chain;
649 epb->next = *epp;
650 if (epb->next)
651 (*epp)->pprev = &epb->next;
652 *epp = epb;
653 epb->pprev = epp;
654 sctp_write_unlock(&head->lock);
655}
656
657/* Add an endpoint to the hash. Local BH-safe. */
658void sctp_hash_endpoint(struct sctp_endpoint *ep)
659{
660 sctp_local_bh_disable();
661 __sctp_hash_endpoint(ep);
662 sctp_local_bh_enable();
663}
664
665/* Remove endpoint from the hash table. */
666static void __sctp_unhash_endpoint(struct sctp_endpoint *ep)
667{
668 struct sctp_hashbucket *head;
669 struct sctp_ep_common *epb;
670
671 epb = &ep->base;
672
673 epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);
674
675 head = &sctp_ep_hashtable[epb->hashent];
676
677 sctp_write_lock(&head->lock);
678
679 if (epb->pprev) {
680 if (epb->next)
681 epb->next->pprev = epb->pprev;
682 *epb->pprev = epb->next;
683 epb->pprev = NULL;
684 }
685
686 sctp_write_unlock(&head->lock);
687}
688
689/* Remove endpoint from the hash. Local BH-safe. */
690void sctp_unhash_endpoint(struct sctp_endpoint *ep)
691{
692 sctp_local_bh_disable();
693 __sctp_unhash_endpoint(ep);
694 sctp_local_bh_enable();
695}
696
697/* Look up an endpoint. */
698static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(const union sctp_addr *laddr)
699{
700 struct sctp_hashbucket *head;
701 struct sctp_ep_common *epb;
702 struct sctp_endpoint *ep;
703 int hash;
704
705 hash = sctp_ep_hashfn(laddr->v4.sin_port);
706 head = &sctp_ep_hashtable[hash];
707 read_lock(&head->lock);
708 for (epb = head->chain; epb; epb = epb->next) {
709 ep = sctp_ep(epb);
710 if (sctp_endpoint_is_match(ep, laddr))
711 goto hit;
712 }
713
714 ep = sctp_sk((sctp_get_ctl_sock()))->ep;
715 epb = &ep->base;
716
717hit:
718 sctp_endpoint_hold(ep);
719 sock_hold(epb->sk);
720 read_unlock(&head->lock);
721 return ep;
722}
723
724/* Insert association into the hash table. */
725static void __sctp_hash_established(struct sctp_association *asoc)
726{
727 struct sctp_ep_common **epp;
728 struct sctp_ep_common *epb;
729 struct sctp_hashbucket *head;
730
731 epb = &asoc->base;
732
733 /* Calculate which chain this entry will belong to. */
734 epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port, asoc->peer.port);
735
736 head = &sctp_assoc_hashtable[epb->hashent];
737
738 sctp_write_lock(&head->lock);
739 epp = &head->chain;
740 epb->next = *epp;
741 if (epb->next)
742 (*epp)->pprev = &epb->next;
743 *epp = epb;
744 epb->pprev = epp;
745 sctp_write_unlock(&head->lock);
746}
747
748/* Add an association to the hash. Local BH-safe. */
749void sctp_hash_established(struct sctp_association *asoc)
750{
751 sctp_local_bh_disable();
752 __sctp_hash_established(asoc);
753 sctp_local_bh_enable();
754}
755
756/* Remove association from the hash table. */
757static void __sctp_unhash_established(struct sctp_association *asoc)
758{
759 struct sctp_hashbucket *head;
760 struct sctp_ep_common *epb;
761
762 epb = &asoc->base;
763
764 epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port,
765 asoc->peer.port);
766
767 head = &sctp_assoc_hashtable[epb->hashent];
768
769 sctp_write_lock(&head->lock);
770
771 if (epb->pprev) {
772 if (epb->next)
773 epb->next->pprev = epb->pprev;
774 *epb->pprev = epb->next;
775 epb->pprev = NULL;
776 }
777
778 sctp_write_unlock(&head->lock);
779}
780
781/* Remove association from the hash table. Local BH-safe. */
782void sctp_unhash_established(struct sctp_association *asoc)
783{
784 sctp_local_bh_disable();
785 __sctp_unhash_established(asoc);
786 sctp_local_bh_enable();
787}
788
789/* Look up an association. */
790static struct sctp_association *__sctp_lookup_association(
791 const union sctp_addr *local,
792 const union sctp_addr *peer,
793 struct sctp_transport **pt)
794{
795 struct sctp_hashbucket *head;
796 struct sctp_ep_common *epb;
797 struct sctp_association *asoc;
798 struct sctp_transport *transport;
799 int hash;
800
801 /* Optimize here for direct hit, only listening connections can
802 * have wildcards anyways.
803 */
804 hash = sctp_assoc_hashfn(local->v4.sin_port, peer->v4.sin_port);
805 head = &sctp_assoc_hashtable[hash];
806 read_lock(&head->lock);
807 for (epb = head->chain; epb; epb = epb->next) {
808 asoc = sctp_assoc(epb);
809 transport = sctp_assoc_is_match(asoc, local, peer);
810 if (transport)
811 goto hit;
812 }
813
814 read_unlock(&head->lock);
815
816 return NULL;
817
818hit:
819 *pt = transport;
820 sctp_association_hold(asoc);
821 sock_hold(epb->sk);
822 read_unlock(&head->lock);
823 return asoc;
824}
825
826/* Look up an association. BH-safe. */
827SCTP_STATIC
828struct sctp_association *sctp_lookup_association(const union sctp_addr *laddr,
829 const union sctp_addr *paddr,
830 struct sctp_transport **transportp)
831{
832 struct sctp_association *asoc;
833
834 sctp_local_bh_disable();
835 asoc = __sctp_lookup_association(laddr, paddr, transportp);
836 sctp_local_bh_enable();
837
838 return asoc;
839}
840
841/* Is there an association matching the given local and peer addresses? */
842int sctp_has_association(const union sctp_addr *laddr,
843 const union sctp_addr *paddr)
844{
845 struct sctp_association *asoc;
846 struct sctp_transport *transport;
847
848 if ((asoc = sctp_lookup_association(laddr, paddr, &transport))) {
849 sock_put(asoc->base.sk);
850 sctp_association_put(asoc);
851 return 1;
852 }
853
854 return 0;
855}
856
857/*
858 * SCTP Implementors Guide, 2.18 Handling of address
859 * parameters within the INIT or INIT-ACK.
860 *
861 * D) When searching for a matching TCB upon reception of an INIT
862 * or INIT-ACK chunk the receiver SHOULD use not only the
863 * source address of the packet (containing the INIT or
864 * INIT-ACK) but the receiver SHOULD also use all valid
865 * address parameters contained within the chunk.
866 *
867 * 2.18.3 Solution description
868 *
869 * This new text clearly specifies to an implementor the need
870 * to look within the INIT or INIT-ACK. Any implementation that
871 * does not do this, may not be able to establish associations
872 * in certain circumstances.
873 *
874 */
875static struct sctp_association *__sctp_rcv_init_lookup(struct sk_buff *skb,
876 const union sctp_addr *laddr, struct sctp_transport **transportp)
877{
878 struct sctp_association *asoc;
879 union sctp_addr addr;
880 union sctp_addr *paddr = &addr;
881 struct sctphdr *sh = (struct sctphdr *) skb->h.raw;
882 sctp_chunkhdr_t *ch;
883 union sctp_params params;
884 sctp_init_chunk_t *init;
885 struct sctp_transport *transport;
886 struct sctp_af *af;
887
888 ch = (sctp_chunkhdr_t *) skb->data;
889
890 /* If this is INIT/INIT-ACK look inside the chunk too. */
891 switch (ch->type) {
892 case SCTP_CID_INIT:
893 case SCTP_CID_INIT_ACK:
894 break;
895 default:
896 return NULL;
897 }
898
899 /* The code below will attempt to walk the chunk and extract
900 * parameter information. Before we do that, we need to verify
901 * that the chunk length doesn't cause overflow. Otherwise, we'll
902 * walk off the end.
903 */
904 if (WORD_ROUND(ntohs(ch->length)) > skb->len)
905 return NULL;
906
907 /*
908 * This code will NOT touch anything inside the chunk--it is
909 * strictly READ-ONLY.
910 *
911 * RFC 2960 3 SCTP packet Format
912 *
913 * Multiple chunks can be bundled into one SCTP packet up to
914 * the MTU size, except for the INIT, INIT ACK, and SHUTDOWN
915 * COMPLETE chunks. These chunks MUST NOT be bundled with any
916 * other chunk in a packet. See Section 6.10 for more details
917 * on chunk bundling.
918 */
919
920 /* Find the start of the TLVs and the end of the chunk. This is
921 * the region we search for address parameters.
922 */
923 init = (sctp_init_chunk_t *)skb->data;
924
925 /* Walk the parameters looking for embedded addresses. */
926 sctp_walk_params(params, init, init_hdr.params) {
927
928 /* Note: Ignoring hostname addresses. */
929 af = sctp_get_af_specific(param_type2af(params.p->type));
930 if (!af)
931 continue;
932
933 af->from_addr_param(paddr, params.addr, ntohs(sh->source), 0);
934
935 asoc = __sctp_lookup_association(laddr, paddr, &transport);
936 if (asoc)
937 return asoc;
938 }
939
940 return NULL;
941}
942
943/* Lookup an association for an inbound skb. */
944static struct sctp_association *__sctp_rcv_lookup(struct sk_buff *skb,
945 const union sctp_addr *paddr,
946 const union sctp_addr *laddr,
947 struct sctp_transport **transportp)
948{
949 struct sctp_association *asoc;
950
951 asoc = __sctp_lookup_association(laddr, paddr, transportp);
952
953 /* Further lookup for INIT/INIT-ACK packets.
954 * SCTP Implementors Guide, 2.18 Handling of address
955 * parameters within the INIT or INIT-ACK.
956 */
957 if (!asoc)
958 asoc = __sctp_rcv_init_lookup(skb, laddr, transportp);
959
960 return asoc;
961}