blob: c463e4049c524140ec77d9cd6c863d5a59765d70 [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;
130 int ret = 0;
131
132 if (skb->pkt_type!=PACKET_HOST)
133 goto discard_it;
134
135 SCTP_INC_STATS_BH(SCTP_MIB_INSCTPPACKS);
136
137 sh = (struct sctphdr *) skb->h.raw;
138
139 /* Pull up the IP and SCTP headers. */
140 __skb_pull(skb, skb->h.raw - skb->data);
141 if (skb->len < sizeof(struct sctphdr))
142 goto discard_it;
143 if (sctp_rcv_checksum(skb) < 0)
144 goto discard_it;
145
146 skb_pull(skb, sizeof(struct sctphdr));
147
148 /* Make sure we at least have chunk headers worth of data left. */
149 if (skb->len < sizeof(struct sctp_chunkhdr))
150 goto discard_it;
151
152 family = ipver2af(skb->nh.iph->version);
153 af = sctp_get_af_specific(family);
154 if (unlikely(!af))
155 goto discard_it;
156
157 /* Initialize local addresses for lookups. */
158 af->from_skb(&src, skb, 1);
159 af->from_skb(&dest, skb, 0);
160
161 /* If the packet is to or from a non-unicast address,
162 * silently discard the packet.
163 *
164 * This is not clearly defined in the RFC except in section
165 * 8.4 - OOTB handling. However, based on the book "Stream Control
166 * Transmission Protocol" 2.1, "It is important to note that the
167 * IP address of an SCTP transport address must be a routable
168 * unicast address. In other words, IP multicast addresses and
169 * IP broadcast addresses cannot be used in an SCTP transport
170 * address."
171 */
172 if (!af->addr_valid(&src, NULL) || !af->addr_valid(&dest, NULL))
173 goto discard_it;
174
175 asoc = __sctp_rcv_lookup(skb, &src, &dest, &transport);
176
Neil Horman0fd9a652005-06-13 15:11:24 -0700177 if (!asoc)
178 ep = __sctp_rcv_lookup_endpoint(&dest);
179
180 /* Retrieve the common input handling substructure. */
181 rcvr = asoc ? &asoc->base : &ep->base;
182 sk = rcvr->sk;
183
184 /*
185 * If a frame arrives on an interface and the receiving socket is
186 * bound to another interface, via SO_BINDTODEVICE, treat it as OOTB
187 */
188 if (sk->sk_bound_dev_if && (sk->sk_bound_dev_if != af->skb_iif(skb)))
189 {
190 sock_put(sk);
191 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);
201 sock_hold(sk);
202 rcvr = &ep->base;
203 }
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 /*
206 * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
207 * An SCTP packet is called an "out of the blue" (OOTB)
208 * packet if it is correctly formed, i.e., passed the
209 * receiver's checksum check, but the receiver is not
210 * able to identify the association to which this
211 * packet belongs.
212 */
213 if (!asoc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (sctp_rcv_ootb(skb)) {
215 SCTP_INC_STATS_BH(SCTP_MIB_OUTOFBLUES);
216 goto discard_release;
217 }
218 }
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 /* SCTP seems to always need a timestamp right now (FIXME) */
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700221 if (skb->tstamp.off_sec == 0) {
222 __net_timestamp(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 sock_enable_timestamp(sk);
224 }
225
226 if (!xfrm_policy_check(sk, XFRM_POLICY_IN, skb, family))
227 goto discard_release;
Patrick McHardyb59c2702006-01-06 23:06:10 -0800228 nf_reset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 ret = sk_filter(sk, skb, 1);
231 if (ret)
232 goto discard_release;
233
234 /* Create an SCTP packet structure. */
235 chunk = sctp_chunkify(skb, asoc, sk);
236 if (!chunk) {
237 ret = -ENOMEM;
238 goto discard_release;
239 }
David S. Miller79af02c2005-07-08 21:47:49 -0700240 SCTP_INPUT_CB(skb)->chunk = chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 /* Remember what endpoint is to handle this packet. */
243 chunk->rcvr = rcvr;
244
245 /* Remember the SCTP header. */
246 chunk->sctp_hdr = sh;
247
248 /* Set the source and destination addresses of the incoming chunk. */
249 sctp_init_addrs(chunk, &src, &dest);
250
251 /* Remember where we came from. */
252 chunk->transport = transport;
253
254 /* Acquire access to the sock lock. Note: We are safe from other
255 * bottom halves on this lock, but a user may be in the lock too,
256 * so check if it is busy.
257 */
258 sctp_bh_lock_sock(sk);
259
260 if (sock_owned_by_user(sk))
David S. Miller79af02c2005-07-08 21:47:49 -0700261 sk_add_backlog(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 else
David S. Miller79af02c2005-07-08 21:47:49 -0700263 sctp_backlog_rcv(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Sridhar Samudrala7a48f922006-01-17 11:51:28 -0800265 /* Release the sock and the sock ref we took in the lookup calls.
266 * The asoc/ep ref will be released in sctp_backlog_rcv.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 */
268 sctp_bh_unlock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 sock_put(sk);
Sridhar Samudrala7a48f922006-01-17 11:51:28 -0800270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return ret;
272
273discard_it:
274 kfree_skb(skb);
275 return ret;
276
277discard_release:
278 /* Release any structures we may be holding. */
Neil Horman0fd9a652005-06-13 15:11:24 -0700279 sock_put(sk);
280 if (asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 sctp_association_put(asoc);
Neil Horman0fd9a652005-06-13 15:11:24 -0700282 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 sctp_endpoint_put(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 goto discard_it;
286}
287
288/* Handle second half of inbound skb processing. If the sock was busy,
289 * we may have need to delay processing until later when the sock is
290 * released (on the backlog). If not busy, we call this routine
291 * directly from the bottom half.
292 */
293int sctp_backlog_rcv(struct sock *sk, struct sk_buff *skb)
294{
David S. Miller79af02c2005-07-08 21:47:49 -0700295 struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
Sridhar Samudrala7a48f922006-01-17 11:51:28 -0800296 struct sctp_inq *inqueue = NULL;
297 struct sctp_ep_common *rcvr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Sridhar Samudrala7a48f922006-01-17 11:51:28 -0800299 rcvr = chunk->rcvr;
300 if (rcvr->dead) {
301 sctp_chunk_free(chunk);
302 } else {
303 inqueue = &chunk->rcvr->inqueue;
304 sctp_inq_push(inqueue, chunk);
305 }
306
307 /* Release the asoc/ep ref we took in the lookup calls in sctp_rcv. */
308 if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
309 sctp_association_put(sctp_assoc(rcvr));
310 else
311 sctp_endpoint_put(sctp_ep(rcvr));
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 return 0;
314}
315
316/* Handle icmp frag needed error. */
317void sctp_icmp_frag_needed(struct sock *sk, struct sctp_association *asoc,
318 struct sctp_transport *t, __u32 pmtu)
319{
Frank Filz52ccb8e2005-12-22 11:36:46 -0800320 if (sock_owned_by_user(sk) || !t || (t->pathmtu == pmtu))
321 return;
322
323 if (t->param_flags & SPP_PMTUD_ENABLE) {
324 if (unlikely(pmtu < SCTP_DEFAULT_MINSEGMENT)) {
325 printk(KERN_WARNING "%s: Reported pmtu %d too low, "
326 "using default minimum of %d\n",
327 __FUNCTION__, pmtu,
328 SCTP_DEFAULT_MINSEGMENT);
329 /* Use default minimum segment size and disable
330 * pmtu discovery on this transport.
331 */
332 t->pathmtu = SCTP_DEFAULT_MINSEGMENT;
333 t->param_flags = (t->param_flags & ~SPP_HB) |
334 SPP_PMTUD_DISABLE;
335 } else {
336 t->pathmtu = pmtu;
337 }
338
339 /* Update association pmtu. */
340 sctp_assoc_sync_pmtu(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342
Frank Filz52ccb8e2005-12-22 11:36:46 -0800343 /* Retransmit with the new pmtu setting.
344 * Normally, if PMTU discovery is disabled, an ICMP Fragmentation
345 * Needed will never be sent, but if a message was sent before
346 * PMTU discovery was disabled that was larger than the PMTU, it
347 * would not be fragmented, so it must be re-transmitted fragmented.
348 */
349 sctp_retransmit(&asoc->outqueue, t, SCTP_RTXR_PMTUD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
351
352/*
353 * SCTP Implementer's Guide, 2.37 ICMP handling procedures
354 *
355 * ICMP8) If the ICMP code is a "Unrecognized next header type encountered"
356 * or a "Protocol Unreachable" treat this message as an abort
357 * with the T bit set.
358 *
359 * This function sends an event to the state machine, which will abort the
360 * association.
361 *
362 */
363void sctp_icmp_proto_unreachable(struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 struct sctp_association *asoc,
365 struct sctp_transport *t)
366{
367 SCTP_DEBUG_PRINTK("%s\n", __FUNCTION__);
368
369 sctp_do_sm(SCTP_EVENT_T_OTHER,
370 SCTP_ST_OTHER(SCTP_EVENT_ICMP_PROTO_UNREACH),
Frank Filz3f7a87d2005-06-20 13:14:57 -0700371 asoc->state, asoc->ep, asoc, t,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 GFP_ATOMIC);
373
374}
375
376/* Common lookup code for icmp/icmpv6 error handler. */
377struct sock *sctp_err_lookup(int family, struct sk_buff *skb,
378 struct sctphdr *sctphdr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 struct sctp_association **app,
380 struct sctp_transport **tpp)
381{
382 union sctp_addr saddr;
383 union sctp_addr daddr;
384 struct sctp_af *af;
385 struct sock *sk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 struct sctp_association *asoc = NULL;
387 struct sctp_transport *transport = NULL;
388
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700389 *app = NULL; *tpp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 af = sctp_get_af_specific(family);
392 if (unlikely(!af)) {
393 return NULL;
394 }
395
396 /* Initialize local addresses for lookups. */
397 af->from_skb(&saddr, skb, 1);
398 af->from_skb(&daddr, skb, 0);
399
400 /* Look for an association that matches the incoming ICMP error
401 * packet.
402 */
403 asoc = __sctp_lookup_association(&saddr, &daddr, &transport);
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700404 if (!asoc)
405 return NULL;
406
407 sk = asoc->base.sk;
408
409 if (ntohl(sctphdr->vtag) != asoc->c.peer_vtag) {
410 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
411 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 sctp_bh_lock_sock(sk);
415
416 /* If too many ICMPs get dropped on busy
417 * servers this needs to be solved differently.
418 */
419 if (sock_owned_by_user(sk))
420 NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 *app = asoc;
423 *tpp = transport;
424 return sk;
425
426out:
427 sock_put(sk);
428 if (asoc)
429 sctp_association_put(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return NULL;
431}
432
433/* Common cleanup code for icmp/icmpv6 error handler. */
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700434void sctp_err_finish(struct sock *sk, struct sctp_association *asoc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
436 sctp_bh_unlock_sock(sk);
437 sock_put(sk);
438 if (asoc)
439 sctp_association_put(asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
442/*
443 * This routine is called by the ICMP module when it gets some
444 * sort of error condition. If err < 0 then the socket should
445 * be closed and the error returned to the user. If err > 0
446 * it's just the icmp type << 8 | icmp code. After adjustment
447 * header points to the first 8 bytes of the sctp header. We need
448 * to find the appropriate port.
449 *
450 * The locking strategy used here is very "optimistic". When
451 * someone else accesses the socket the ICMP is just dropped
452 * and for some paths there is no check at all.
453 * A more general error queue to queue errors for later handling
454 * is probably better.
455 *
456 */
457void sctp_v4_err(struct sk_buff *skb, __u32 info)
458{
459 struct iphdr *iph = (struct iphdr *)skb->data;
460 struct sctphdr *sh = (struct sctphdr *)(skb->data + (iph->ihl <<2));
461 int type = skb->h.icmph->type;
462 int code = skb->h.icmph->code;
463 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 struct sctp_association *asoc;
465 struct sctp_transport *transport;
466 struct inet_sock *inet;
467 char *saveip, *savesctp;
468 int err;
469
470 if (skb->len < ((iph->ihl << 2) + 8)) {
471 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
472 return;
473 }
474
475 /* Fix up skb to look at the embedded net header. */
476 saveip = skb->nh.raw;
477 savesctp = skb->h.raw;
478 skb->nh.iph = iph;
479 skb->h.raw = (char *)sh;
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700480 sk = sctp_err_lookup(AF_INET, skb, sh, &asoc, &transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 /* Put back, the original pointers. */
482 skb->nh.raw = saveip;
483 skb->h.raw = savesctp;
484 if (!sk) {
485 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
486 return;
487 }
488 /* Warning: The sock lock is held. Remember to call
489 * sctp_err_finish!
490 */
491
492 switch (type) {
493 case ICMP_PARAMETERPROB:
494 err = EPROTO;
495 break;
496 case ICMP_DEST_UNREACH:
497 if (code > NR_ICMP_UNREACH)
498 goto out_unlock;
499
500 /* PMTU discovery (RFC1191) */
501 if (ICMP_FRAG_NEEDED == code) {
502 sctp_icmp_frag_needed(sk, asoc, transport, info);
503 goto out_unlock;
504 }
505 else {
506 if (ICMP_PROT_UNREACH == code) {
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700507 sctp_icmp_proto_unreachable(sk, asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 transport);
509 goto out_unlock;
510 }
511 }
512 err = icmp_err_convert[code].errno;
513 break;
514 case ICMP_TIME_EXCEEDED:
515 /* Ignore any time exceeded errors due to fragment reassembly
516 * timeouts.
517 */
518 if (ICMP_EXC_FRAGTIME == code)
519 goto out_unlock;
520
521 err = EHOSTUNREACH;
522 break;
523 default:
524 goto out_unlock;
525 }
526
527 inet = inet_sk(sk);
528 if (!sock_owned_by_user(sk) && inet->recverr) {
529 sk->sk_err = err;
530 sk->sk_error_report(sk);
531 } else { /* Only an error on timeout */
532 sk->sk_err_soft = err;
533 }
534
535out_unlock:
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700536 sctp_err_finish(sk, asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
539/*
540 * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
541 *
542 * This function scans all the chunks in the OOTB packet to determine if
543 * the packet should be discarded right away. If a response might be needed
544 * for this packet, or, if further processing is possible, the packet will
545 * be queued to a proper inqueue for the next phase of handling.
546 *
547 * Output:
548 * Return 0 - If further processing is needed.
549 * Return 1 - If the packet can be discarded right away.
550 */
551int sctp_rcv_ootb(struct sk_buff *skb)
552{
553 sctp_chunkhdr_t *ch;
554 __u8 *ch_end;
555 sctp_errhdr_t *err;
556
557 ch = (sctp_chunkhdr_t *) skb->data;
558 ch_end = ((__u8 *) ch) + WORD_ROUND(ntohs(ch->length));
559
560 /* Scan through all the chunks in the packet. */
561 while (ch_end > (__u8 *)ch && ch_end < skb->tail) {
562
563 /* RFC 8.4, 2) If the OOTB packet contains an ABORT chunk, the
564 * receiver MUST silently discard the OOTB packet and take no
565 * further action.
566 */
567 if (SCTP_CID_ABORT == ch->type)
568 goto discard;
569
570 /* RFC 8.4, 6) If the packet contains a SHUTDOWN COMPLETE
571 * chunk, the receiver should silently discard the packet
572 * and take no further action.
573 */
574 if (SCTP_CID_SHUTDOWN_COMPLETE == ch->type)
575 goto discard;
576
577 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
578 * or a COOKIE ACK the SCTP Packet should be silently
579 * discarded.
580 */
581 if (SCTP_CID_COOKIE_ACK == ch->type)
582 goto discard;
583
584 if (SCTP_CID_ERROR == ch->type) {
585 sctp_walk_errors(err, ch) {
586 if (SCTP_ERROR_STALE_COOKIE == err->cause)
587 goto discard;
588 }
589 }
590
591 ch = (sctp_chunkhdr_t *) ch_end;
592 ch_end = ((__u8 *) ch) + WORD_ROUND(ntohs(ch->length));
593 }
594
595 return 0;
596
597discard:
598 return 1;
599}
600
601/* Insert endpoint into the hash table. */
602static void __sctp_hash_endpoint(struct sctp_endpoint *ep)
603{
604 struct sctp_ep_common **epp;
605 struct sctp_ep_common *epb;
606 struct sctp_hashbucket *head;
607
608 epb = &ep->base;
609
610 epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);
611 head = &sctp_ep_hashtable[epb->hashent];
612
613 sctp_write_lock(&head->lock);
614 epp = &head->chain;
615 epb->next = *epp;
616 if (epb->next)
617 (*epp)->pprev = &epb->next;
618 *epp = epb;
619 epb->pprev = epp;
620 sctp_write_unlock(&head->lock);
621}
622
623/* Add an endpoint to the hash. Local BH-safe. */
624void sctp_hash_endpoint(struct sctp_endpoint *ep)
625{
626 sctp_local_bh_disable();
627 __sctp_hash_endpoint(ep);
628 sctp_local_bh_enable();
629}
630
631/* Remove endpoint from the hash table. */
632static void __sctp_unhash_endpoint(struct sctp_endpoint *ep)
633{
634 struct sctp_hashbucket *head;
635 struct sctp_ep_common *epb;
636
637 epb = &ep->base;
638
639 epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);
640
641 head = &sctp_ep_hashtable[epb->hashent];
642
643 sctp_write_lock(&head->lock);
644
645 if (epb->pprev) {
646 if (epb->next)
647 epb->next->pprev = epb->pprev;
648 *epb->pprev = epb->next;
649 epb->pprev = NULL;
650 }
651
652 sctp_write_unlock(&head->lock);
653}
654
655/* Remove endpoint from the hash. Local BH-safe. */
656void sctp_unhash_endpoint(struct sctp_endpoint *ep)
657{
658 sctp_local_bh_disable();
659 __sctp_unhash_endpoint(ep);
660 sctp_local_bh_enable();
661}
662
663/* Look up an endpoint. */
664static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(const union sctp_addr *laddr)
665{
666 struct sctp_hashbucket *head;
667 struct sctp_ep_common *epb;
668 struct sctp_endpoint *ep;
669 int hash;
670
671 hash = sctp_ep_hashfn(laddr->v4.sin_port);
672 head = &sctp_ep_hashtable[hash];
673 read_lock(&head->lock);
674 for (epb = head->chain; epb; epb = epb->next) {
675 ep = sctp_ep(epb);
676 if (sctp_endpoint_is_match(ep, laddr))
677 goto hit;
678 }
679
680 ep = sctp_sk((sctp_get_ctl_sock()))->ep;
681 epb = &ep->base;
682
683hit:
684 sctp_endpoint_hold(ep);
685 sock_hold(epb->sk);
686 read_unlock(&head->lock);
687 return ep;
688}
689
690/* Insert association into the hash table. */
691static void __sctp_hash_established(struct sctp_association *asoc)
692{
693 struct sctp_ep_common **epp;
694 struct sctp_ep_common *epb;
695 struct sctp_hashbucket *head;
696
697 epb = &asoc->base;
698
699 /* Calculate which chain this entry will belong to. */
700 epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port, asoc->peer.port);
701
702 head = &sctp_assoc_hashtable[epb->hashent];
703
704 sctp_write_lock(&head->lock);
705 epp = &head->chain;
706 epb->next = *epp;
707 if (epb->next)
708 (*epp)->pprev = &epb->next;
709 *epp = epb;
710 epb->pprev = epp;
711 sctp_write_unlock(&head->lock);
712}
713
714/* Add an association to the hash. Local BH-safe. */
715void sctp_hash_established(struct sctp_association *asoc)
716{
717 sctp_local_bh_disable();
718 __sctp_hash_established(asoc);
719 sctp_local_bh_enable();
720}
721
722/* Remove association from the hash table. */
723static void __sctp_unhash_established(struct sctp_association *asoc)
724{
725 struct sctp_hashbucket *head;
726 struct sctp_ep_common *epb;
727
728 epb = &asoc->base;
729
730 epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port,
731 asoc->peer.port);
732
733 head = &sctp_assoc_hashtable[epb->hashent];
734
735 sctp_write_lock(&head->lock);
736
737 if (epb->pprev) {
738 if (epb->next)
739 epb->next->pprev = epb->pprev;
740 *epb->pprev = epb->next;
741 epb->pprev = NULL;
742 }
743
744 sctp_write_unlock(&head->lock);
745}
746
747/* Remove association from the hash table. Local BH-safe. */
748void sctp_unhash_established(struct sctp_association *asoc)
749{
750 sctp_local_bh_disable();
751 __sctp_unhash_established(asoc);
752 sctp_local_bh_enable();
753}
754
755/* Look up an association. */
756static struct sctp_association *__sctp_lookup_association(
757 const union sctp_addr *local,
758 const union sctp_addr *peer,
759 struct sctp_transport **pt)
760{
761 struct sctp_hashbucket *head;
762 struct sctp_ep_common *epb;
763 struct sctp_association *asoc;
764 struct sctp_transport *transport;
765 int hash;
766
767 /* Optimize here for direct hit, only listening connections can
768 * have wildcards anyways.
769 */
770 hash = sctp_assoc_hashfn(local->v4.sin_port, peer->v4.sin_port);
771 head = &sctp_assoc_hashtable[hash];
772 read_lock(&head->lock);
773 for (epb = head->chain; epb; epb = epb->next) {
774 asoc = sctp_assoc(epb);
775 transport = sctp_assoc_is_match(asoc, local, peer);
776 if (transport)
777 goto hit;
778 }
779
780 read_unlock(&head->lock);
781
782 return NULL;
783
784hit:
785 *pt = transport;
786 sctp_association_hold(asoc);
787 sock_hold(epb->sk);
788 read_unlock(&head->lock);
789 return asoc;
790}
791
792/* Look up an association. BH-safe. */
793SCTP_STATIC
794struct sctp_association *sctp_lookup_association(const union sctp_addr *laddr,
795 const union sctp_addr *paddr,
796 struct sctp_transport **transportp)
797{
798 struct sctp_association *asoc;
799
800 sctp_local_bh_disable();
801 asoc = __sctp_lookup_association(laddr, paddr, transportp);
802 sctp_local_bh_enable();
803
804 return asoc;
805}
806
807/* Is there an association matching the given local and peer addresses? */
808int sctp_has_association(const union sctp_addr *laddr,
809 const union sctp_addr *paddr)
810{
811 struct sctp_association *asoc;
812 struct sctp_transport *transport;
813
814 if ((asoc = sctp_lookup_association(laddr, paddr, &transport))) {
815 sock_put(asoc->base.sk);
816 sctp_association_put(asoc);
817 return 1;
818 }
819
820 return 0;
821}
822
823/*
824 * SCTP Implementors Guide, 2.18 Handling of address
825 * parameters within the INIT or INIT-ACK.
826 *
827 * D) When searching for a matching TCB upon reception of an INIT
828 * or INIT-ACK chunk the receiver SHOULD use not only the
829 * source address of the packet (containing the INIT or
830 * INIT-ACK) but the receiver SHOULD also use all valid
831 * address parameters contained within the chunk.
832 *
833 * 2.18.3 Solution description
834 *
835 * This new text clearly specifies to an implementor the need
836 * to look within the INIT or INIT-ACK. Any implementation that
837 * does not do this, may not be able to establish associations
838 * in certain circumstances.
839 *
840 */
841static struct sctp_association *__sctp_rcv_init_lookup(struct sk_buff *skb,
842 const union sctp_addr *laddr, struct sctp_transport **transportp)
843{
844 struct sctp_association *asoc;
845 union sctp_addr addr;
846 union sctp_addr *paddr = &addr;
847 struct sctphdr *sh = (struct sctphdr *) skb->h.raw;
848 sctp_chunkhdr_t *ch;
849 union sctp_params params;
850 sctp_init_chunk_t *init;
851 struct sctp_transport *transport;
852 struct sctp_af *af;
853
854 ch = (sctp_chunkhdr_t *) skb->data;
855
856 /* If this is INIT/INIT-ACK look inside the chunk too. */
857 switch (ch->type) {
858 case SCTP_CID_INIT:
859 case SCTP_CID_INIT_ACK:
860 break;
861 default:
862 return NULL;
863 }
864
865 /* The code below will attempt to walk the chunk and extract
866 * parameter information. Before we do that, we need to verify
867 * that the chunk length doesn't cause overflow. Otherwise, we'll
868 * walk off the end.
869 */
870 if (WORD_ROUND(ntohs(ch->length)) > skb->len)
871 return NULL;
872
873 /*
874 * This code will NOT touch anything inside the chunk--it is
875 * strictly READ-ONLY.
876 *
877 * RFC 2960 3 SCTP packet Format
878 *
879 * Multiple chunks can be bundled into one SCTP packet up to
880 * the MTU size, except for the INIT, INIT ACK, and SHUTDOWN
881 * COMPLETE chunks. These chunks MUST NOT be bundled with any
882 * other chunk in a packet. See Section 6.10 for more details
883 * on chunk bundling.
884 */
885
886 /* Find the start of the TLVs and the end of the chunk. This is
887 * the region we search for address parameters.
888 */
889 init = (sctp_init_chunk_t *)skb->data;
890
891 /* Walk the parameters looking for embedded addresses. */
892 sctp_walk_params(params, init, init_hdr.params) {
893
894 /* Note: Ignoring hostname addresses. */
895 af = sctp_get_af_specific(param_type2af(params.p->type));
896 if (!af)
897 continue;
898
899 af->from_addr_param(paddr, params.addr, ntohs(sh->source), 0);
900
901 asoc = __sctp_lookup_association(laddr, paddr, &transport);
902 if (asoc)
903 return asoc;
904 }
905
906 return NULL;
907}
908
909/* Lookup an association for an inbound skb. */
910static struct sctp_association *__sctp_rcv_lookup(struct sk_buff *skb,
911 const union sctp_addr *paddr,
912 const union sctp_addr *laddr,
913 struct sctp_transport **transportp)
914{
915 struct sctp_association *asoc;
916
917 asoc = __sctp_lookup_association(laddr, paddr, transportp);
918
919 /* Further lookup for INIT/INIT-ACK packets.
920 * SCTP Implementors Guide, 2.18 Handling of address
921 * parameters within the INIT or INIT-ACK.
922 */
923 if (!asoc)
924 asoc = __sctp_rcv_init_lookup(skb, laddr, transportp);
925
926 return asoc;
927}