[PKT_SCHED] RED: Fix overflow in calculation of queue average
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / sctp / input.c
blob2325fee8574819498067979aef55bfcd353c9a0a
1 /* 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
9 * This file is part of the SCTP kernel reference Implementation
11 * These functions handle all input from the IP layer into SCTP.
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.
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.
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.
30 * Please send any bug reports or fixes you make to the
31 * email address(es):
32 * lksctp developers <lksctp-developers@lists.sourceforge.net>
34 * Or submit a bug report through the following website:
35 * http://www.sf.net/projects/lksctp
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>
47 * Any bugs reported given to us we will try to fix... any fixes shared will
48 * be incorporated into the next SCTP release.
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>
64 /* Forward declarations for internal helpers. */
65 static int sctp_rcv_ootb(struct sk_buff *);
66 static 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);
70 static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(const union sctp_addr *laddr);
71 static struct sctp_association *__sctp_lookup_association(
72 const union sctp_addr *local,
73 const union sctp_addr *peer,
74 struct sctp_transport **pt);
77 /* Calculate the SCTP checksum of an SCTP packet. */
78 static inline int sctp_rcv_checksum(struct sk_buff *skb)
80 struct sctphdr *sh;
81 __u32 cmp, val;
82 struct sk_buff *list = skb_shinfo(skb)->frag_list;
84 sh = (struct sctphdr *) skb->h.raw;
85 cmp = ntohl(sh->checksum);
87 val = sctp_start_cksum((__u8 *)sh, skb_headlen(skb));
89 for (; list; list = list->next)
90 val = sctp_update_cksum((__u8 *)list->data, skb_headlen(list),
91 val);
93 val = sctp_end_cksum(val);
95 if (val != cmp) {
96 /* CRC failure, dump it. */
97 SCTP_INC_STATS_BH(SCTP_MIB_CHECKSUMERRORS);
98 return -1;
100 return 0;
103 struct 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;
112 #define SCTP_INPUT_CB(__skb) ((struct sctp_input_cb *)&((__skb)->cb[0]))
115 * This is the routine which IP calls when receiving an SCTP packet.
117 int sctp_rcv(struct sk_buff *skb)
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;
132 if (skb->pkt_type!=PACKET_HOST)
133 goto discard_it;
135 SCTP_INC_STATS_BH(SCTP_MIB_INSCTPPACKS);
137 if (skb_linearize(skb, GFP_ATOMIC))
138 goto discard_it;
140 sh = (struct sctphdr *) skb->h.raw;
142 /* Pull up the IP and SCTP headers. */
143 __skb_pull(skb, skb->h.raw - skb->data);
144 if (skb->len < sizeof(struct sctphdr))
145 goto discard_it;
146 if (sctp_rcv_checksum(skb) < 0)
147 goto discard_it;
149 skb_pull(skb, sizeof(struct sctphdr));
151 /* Make sure we at least have chunk headers worth of data left. */
152 if (skb->len < sizeof(struct sctp_chunkhdr))
153 goto discard_it;
155 family = ipver2af(skb->nh.iph->version);
156 af = sctp_get_af_specific(family);
157 if (unlikely(!af))
158 goto discard_it;
160 /* Initialize local addresses for lookups. */
161 af->from_skb(&src, skb, 1);
162 af->from_skb(&dest, skb, 0);
164 /* If the packet is to or from a non-unicast address,
165 * silently discard the packet.
167 * This is not clearly defined in the RFC except in section
168 * 8.4 - OOTB handling. However, based on the book "Stream Control
169 * Transmission Protocol" 2.1, "It is important to note that the
170 * IP address of an SCTP transport address must be a routable
171 * unicast address. In other words, IP multicast addresses and
172 * IP broadcast addresses cannot be used in an SCTP transport
173 * address."
175 if (!af->addr_valid(&src, NULL, skb) ||
176 !af->addr_valid(&dest, NULL, skb))
177 goto discard_it;
179 asoc = __sctp_rcv_lookup(skb, &src, &dest, &transport);
181 if (!asoc)
182 ep = __sctp_rcv_lookup_endpoint(&dest);
184 /* Retrieve the common input handling substructure. */
185 rcvr = asoc ? &asoc->base : &ep->base;
186 sk = rcvr->sk;
189 * If a frame arrives on an interface and the receiving socket is
190 * bound to another interface, via SO_BINDTODEVICE, treat it as OOTB
192 if (sk->sk_bound_dev_if && (sk->sk_bound_dev_if != af->skb_iif(skb)))
194 sock_put(sk);
195 if (asoc) {
196 sctp_association_put(asoc);
197 asoc = NULL;
198 } else {
199 sctp_endpoint_put(ep);
200 ep = NULL;
202 sk = sctp_get_ctl_sock();
203 ep = sctp_sk(sk)->ep;
204 sctp_endpoint_hold(ep);
205 sock_hold(sk);
206 rcvr = &ep->base;
210 * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
211 * An SCTP packet is called an "out of the blue" (OOTB)
212 * packet if it is correctly formed, i.e., passed the
213 * receiver's checksum check, but the receiver is not
214 * able to identify the association to which this
215 * packet belongs.
217 if (!asoc) {
218 if (sctp_rcv_ootb(skb)) {
219 SCTP_INC_STATS_BH(SCTP_MIB_OUTOFBLUES);
220 goto discard_release;
224 /* SCTP seems to always need a timestamp right now (FIXME) */
225 if (skb->tstamp.off_sec == 0) {
226 __net_timestamp(skb);
227 sock_enable_timestamp(sk);
230 if (!xfrm_policy_check(sk, XFRM_POLICY_IN, skb, family))
231 goto discard_release;
232 nf_reset(skb);
234 ret = sk_filter(sk, skb, 1);
235 if (ret)
236 goto discard_release;
238 /* Create an SCTP packet structure. */
239 chunk = sctp_chunkify(skb, asoc, sk);
240 if (!chunk) {
241 ret = -ENOMEM;
242 goto discard_release;
244 SCTP_INPUT_CB(skb)->chunk = chunk;
246 /* Remember what endpoint is to handle this packet. */
247 chunk->rcvr = rcvr;
249 /* Remember the SCTP header. */
250 chunk->sctp_hdr = sh;
252 /* Set the source and destination addresses of the incoming chunk. */
253 sctp_init_addrs(chunk, &src, &dest);
255 /* Remember where we came from. */
256 chunk->transport = transport;
258 /* Acquire access to the sock lock. Note: We are safe from other
259 * bottom halves on this lock, but a user may be in the lock too,
260 * so check if it is busy.
262 sctp_bh_lock_sock(sk);
264 /* It is possible that the association could have moved to a different
265 * socket if it is peeled off. If so, update the sk.
267 if (sk != rcvr->sk) {
268 sctp_bh_lock_sock(rcvr->sk);
269 sctp_bh_unlock_sock(sk);
270 sk = rcvr->sk;
273 if (sock_owned_by_user(sk))
274 sk_add_backlog(sk, skb);
275 else
276 sctp_backlog_rcv(sk, skb);
278 /* Release the sock and the sock ref we took in the lookup calls.
279 * The asoc/ep ref will be released in sctp_backlog_rcv.
281 sctp_bh_unlock_sock(sk);
282 sock_put(sk);
284 return ret;
286 discard_it:
287 kfree_skb(skb);
288 return ret;
290 discard_release:
291 /* Release any structures we may be holding. */
292 sock_put(sk);
293 if (asoc)
294 sctp_association_put(asoc);
295 else
296 sctp_endpoint_put(ep);
298 goto discard_it;
301 /* Handle second half of inbound skb processing. If the sock was busy,
302 * we may have need to delay processing until later when the sock is
303 * released (on the backlog). If not busy, we call this routine
304 * directly from the bottom half.
306 int sctp_backlog_rcv(struct sock *sk, struct sk_buff *skb)
308 struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
309 struct sctp_inq *inqueue = NULL;
310 struct sctp_ep_common *rcvr = NULL;
312 rcvr = chunk->rcvr;
314 BUG_TRAP(rcvr->sk == sk);
316 if (rcvr->dead) {
317 sctp_chunk_free(chunk);
318 } else {
319 inqueue = &chunk->rcvr->inqueue;
320 sctp_inq_push(inqueue, chunk);
323 /* Release the asoc/ep ref we took in the lookup calls in sctp_rcv. */
324 if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
325 sctp_association_put(sctp_assoc(rcvr));
326 else
327 sctp_endpoint_put(sctp_ep(rcvr));
329 return 0;
332 void sctp_backlog_migrate(struct sctp_association *assoc,
333 struct sock *oldsk, struct sock *newsk)
335 struct sk_buff *skb;
336 struct sctp_chunk *chunk;
338 skb = oldsk->sk_backlog.head;
339 oldsk->sk_backlog.head = oldsk->sk_backlog.tail = NULL;
340 while (skb != NULL) {
341 struct sk_buff *next = skb->next;
343 chunk = SCTP_INPUT_CB(skb)->chunk;
344 skb->next = NULL;
345 if (&assoc->base == chunk->rcvr)
346 sk_add_backlog(newsk, skb);
347 else
348 sk_add_backlog(oldsk, skb);
349 skb = next;
353 /* Handle icmp frag needed error. */
354 void sctp_icmp_frag_needed(struct sock *sk, struct sctp_association *asoc,
355 struct sctp_transport *t, __u32 pmtu)
357 if (sock_owned_by_user(sk) || !t || (t->pathmtu == pmtu))
358 return;
360 if (t->param_flags & SPP_PMTUD_ENABLE) {
361 if (unlikely(pmtu < SCTP_DEFAULT_MINSEGMENT)) {
362 printk(KERN_WARNING "%s: Reported pmtu %d too low, "
363 "using default minimum of %d\n",
364 __FUNCTION__, pmtu,
365 SCTP_DEFAULT_MINSEGMENT);
366 /* Use default minimum segment size and disable
367 * pmtu discovery on this transport.
369 t->pathmtu = SCTP_DEFAULT_MINSEGMENT;
370 t->param_flags = (t->param_flags & ~SPP_HB) |
371 SPP_PMTUD_DISABLE;
372 } else {
373 t->pathmtu = pmtu;
376 /* Update association pmtu. */
377 sctp_assoc_sync_pmtu(asoc);
380 /* Retransmit with the new pmtu setting.
381 * Normally, if PMTU discovery is disabled, an ICMP Fragmentation
382 * Needed will never be sent, but if a message was sent before
383 * PMTU discovery was disabled that was larger than the PMTU, it
384 * would not be fragmented, so it must be re-transmitted fragmented.
386 sctp_retransmit(&asoc->outqueue, t, SCTP_RTXR_PMTUD);
390 * SCTP Implementer's Guide, 2.37 ICMP handling procedures
392 * ICMP8) If the ICMP code is a "Unrecognized next header type encountered"
393 * or a "Protocol Unreachable" treat this message as an abort
394 * with the T bit set.
396 * This function sends an event to the state machine, which will abort the
397 * association.
400 void sctp_icmp_proto_unreachable(struct sock *sk,
401 struct sctp_association *asoc,
402 struct sctp_transport *t)
404 SCTP_DEBUG_PRINTK("%s\n", __FUNCTION__);
406 sctp_do_sm(SCTP_EVENT_T_OTHER,
407 SCTP_ST_OTHER(SCTP_EVENT_ICMP_PROTO_UNREACH),
408 asoc->state, asoc->ep, asoc, t,
409 GFP_ATOMIC);
413 /* Common lookup code for icmp/icmpv6 error handler. */
414 struct sock *sctp_err_lookup(int family, struct sk_buff *skb,
415 struct sctphdr *sctphdr,
416 struct sctp_association **app,
417 struct sctp_transport **tpp)
419 union sctp_addr saddr;
420 union sctp_addr daddr;
421 struct sctp_af *af;
422 struct sock *sk = NULL;
423 struct sctp_association *asoc = NULL;
424 struct sctp_transport *transport = NULL;
426 *app = NULL; *tpp = NULL;
428 af = sctp_get_af_specific(family);
429 if (unlikely(!af)) {
430 return NULL;
433 /* Initialize local addresses for lookups. */
434 af->from_skb(&saddr, skb, 1);
435 af->from_skb(&daddr, skb, 0);
437 /* Look for an association that matches the incoming ICMP error
438 * packet.
440 asoc = __sctp_lookup_association(&saddr, &daddr, &transport);
441 if (!asoc)
442 return NULL;
444 sk = asoc->base.sk;
446 if (ntohl(sctphdr->vtag) != asoc->c.peer_vtag) {
447 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
448 goto out;
451 sctp_bh_lock_sock(sk);
453 /* If too many ICMPs get dropped on busy
454 * servers this needs to be solved differently.
456 if (sock_owned_by_user(sk))
457 NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);
459 *app = asoc;
460 *tpp = transport;
461 return sk;
463 out:
464 sock_put(sk);
465 if (asoc)
466 sctp_association_put(asoc);
467 return NULL;
470 /* Common cleanup code for icmp/icmpv6 error handler. */
471 void sctp_err_finish(struct sock *sk, struct sctp_association *asoc)
473 sctp_bh_unlock_sock(sk);
474 sock_put(sk);
475 if (asoc)
476 sctp_association_put(asoc);
480 * This routine is called by the ICMP module when it gets some
481 * sort of error condition. If err < 0 then the socket should
482 * be closed and the error returned to the user. If err > 0
483 * it's just the icmp type << 8 | icmp code. After adjustment
484 * header points to the first 8 bytes of the sctp header. We need
485 * to find the appropriate port.
487 * The locking strategy used here is very "optimistic". When
488 * someone else accesses the socket the ICMP is just dropped
489 * and for some paths there is no check at all.
490 * A more general error queue to queue errors for later handling
491 * is probably better.
494 void sctp_v4_err(struct sk_buff *skb, __u32 info)
496 struct iphdr *iph = (struct iphdr *)skb->data;
497 struct sctphdr *sh = (struct sctphdr *)(skb->data + (iph->ihl <<2));
498 int type = skb->h.icmph->type;
499 int code = skb->h.icmph->code;
500 struct sock *sk;
501 struct sctp_association *asoc;
502 struct sctp_transport *transport;
503 struct inet_sock *inet;
504 char *saveip, *savesctp;
505 int err;
507 if (skb->len < ((iph->ihl << 2) + 8)) {
508 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
509 return;
512 /* Fix up skb to look at the embedded net header. */
513 saveip = skb->nh.raw;
514 savesctp = skb->h.raw;
515 skb->nh.iph = iph;
516 skb->h.raw = (char *)sh;
517 sk = sctp_err_lookup(AF_INET, skb, sh, &asoc, &transport);
518 /* Put back, the original pointers. */
519 skb->nh.raw = saveip;
520 skb->h.raw = savesctp;
521 if (!sk) {
522 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
523 return;
525 /* Warning: The sock lock is held. Remember to call
526 * sctp_err_finish!
529 switch (type) {
530 case ICMP_PARAMETERPROB:
531 err = EPROTO;
532 break;
533 case ICMP_DEST_UNREACH:
534 if (code > NR_ICMP_UNREACH)
535 goto out_unlock;
537 /* PMTU discovery (RFC1191) */
538 if (ICMP_FRAG_NEEDED == code) {
539 sctp_icmp_frag_needed(sk, asoc, transport, info);
540 goto out_unlock;
542 else {
543 if (ICMP_PROT_UNREACH == code) {
544 sctp_icmp_proto_unreachable(sk, asoc,
545 transport);
546 goto out_unlock;
549 err = icmp_err_convert[code].errno;
550 break;
551 case ICMP_TIME_EXCEEDED:
552 /* Ignore any time exceeded errors due to fragment reassembly
553 * timeouts.
555 if (ICMP_EXC_FRAGTIME == code)
556 goto out_unlock;
558 err = EHOSTUNREACH;
559 break;
560 default:
561 goto out_unlock;
564 inet = inet_sk(sk);
565 if (!sock_owned_by_user(sk) && inet->recverr) {
566 sk->sk_err = err;
567 sk->sk_error_report(sk);
568 } else { /* Only an error on timeout */
569 sk->sk_err_soft = err;
572 out_unlock:
573 sctp_err_finish(sk, asoc);
577 * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
579 * This function scans all the chunks in the OOTB packet to determine if
580 * the packet should be discarded right away. If a response might be needed
581 * for this packet, or, if further processing is possible, the packet will
582 * be queued to a proper inqueue for the next phase of handling.
584 * Output:
585 * Return 0 - If further processing is needed.
586 * Return 1 - If the packet can be discarded right away.
588 int sctp_rcv_ootb(struct sk_buff *skb)
590 sctp_chunkhdr_t *ch;
591 __u8 *ch_end;
592 sctp_errhdr_t *err;
594 ch = (sctp_chunkhdr_t *) skb->data;
596 /* Scan through all the chunks in the packet. */
597 do {
598 /* Break out if chunk length is less then minimal. */
599 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
600 break;
602 ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
603 if (ch_end > skb->tail)
604 break;
606 /* RFC 8.4, 2) If the OOTB packet contains an ABORT chunk, the
607 * receiver MUST silently discard the OOTB packet and take no
608 * further action.
610 if (SCTP_CID_ABORT == ch->type)
611 goto discard;
613 /* RFC 8.4, 6) If the packet contains a SHUTDOWN COMPLETE
614 * chunk, the receiver should silently discard the packet
615 * and take no further action.
617 if (SCTP_CID_SHUTDOWN_COMPLETE == ch->type)
618 goto discard;
620 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
621 * or a COOKIE ACK the SCTP Packet should be silently
622 * discarded.
624 if (SCTP_CID_COOKIE_ACK == ch->type)
625 goto discard;
627 if (SCTP_CID_ERROR == ch->type) {
628 sctp_walk_errors(err, ch) {
629 if (SCTP_ERROR_STALE_COOKIE == err->cause)
630 goto discard;
634 ch = (sctp_chunkhdr_t *) ch_end;
635 } while (ch_end < skb->tail);
637 return 0;
639 discard:
640 return 1;
643 /* Insert endpoint into the hash table. */
644 static void __sctp_hash_endpoint(struct sctp_endpoint *ep)
646 struct sctp_ep_common **epp;
647 struct sctp_ep_common *epb;
648 struct sctp_hashbucket *head;
650 epb = &ep->base;
652 epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);
653 head = &sctp_ep_hashtable[epb->hashent];
655 sctp_write_lock(&head->lock);
656 epp = &head->chain;
657 epb->next = *epp;
658 if (epb->next)
659 (*epp)->pprev = &epb->next;
660 *epp = epb;
661 epb->pprev = epp;
662 sctp_write_unlock(&head->lock);
665 /* Add an endpoint to the hash. Local BH-safe. */
666 void sctp_hash_endpoint(struct sctp_endpoint *ep)
668 sctp_local_bh_disable();
669 __sctp_hash_endpoint(ep);
670 sctp_local_bh_enable();
673 /* Remove endpoint from the hash table. */
674 static void __sctp_unhash_endpoint(struct sctp_endpoint *ep)
676 struct sctp_hashbucket *head;
677 struct sctp_ep_common *epb;
679 epb = &ep->base;
681 epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);
683 head = &sctp_ep_hashtable[epb->hashent];
685 sctp_write_lock(&head->lock);
687 if (epb->pprev) {
688 if (epb->next)
689 epb->next->pprev = epb->pprev;
690 *epb->pprev = epb->next;
691 epb->pprev = NULL;
694 sctp_write_unlock(&head->lock);
697 /* Remove endpoint from the hash. Local BH-safe. */
698 void sctp_unhash_endpoint(struct sctp_endpoint *ep)
700 sctp_local_bh_disable();
701 __sctp_unhash_endpoint(ep);
702 sctp_local_bh_enable();
705 /* Look up an endpoint. */
706 static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(const union sctp_addr *laddr)
708 struct sctp_hashbucket *head;
709 struct sctp_ep_common *epb;
710 struct sctp_endpoint *ep;
711 int hash;
713 hash = sctp_ep_hashfn(laddr->v4.sin_port);
714 head = &sctp_ep_hashtable[hash];
715 read_lock(&head->lock);
716 for (epb = head->chain; epb; epb = epb->next) {
717 ep = sctp_ep(epb);
718 if (sctp_endpoint_is_match(ep, laddr))
719 goto hit;
722 ep = sctp_sk((sctp_get_ctl_sock()))->ep;
723 epb = &ep->base;
725 hit:
726 sctp_endpoint_hold(ep);
727 sock_hold(epb->sk);
728 read_unlock(&head->lock);
729 return ep;
732 /* Insert association into the hash table. */
733 static void __sctp_hash_established(struct sctp_association *asoc)
735 struct sctp_ep_common **epp;
736 struct sctp_ep_common *epb;
737 struct sctp_hashbucket *head;
739 epb = &asoc->base;
741 /* Calculate which chain this entry will belong to. */
742 epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port, asoc->peer.port);
744 head = &sctp_assoc_hashtable[epb->hashent];
746 sctp_write_lock(&head->lock);
747 epp = &head->chain;
748 epb->next = *epp;
749 if (epb->next)
750 (*epp)->pprev = &epb->next;
751 *epp = epb;
752 epb->pprev = epp;
753 sctp_write_unlock(&head->lock);
756 /* Add an association to the hash. Local BH-safe. */
757 void sctp_hash_established(struct sctp_association *asoc)
759 sctp_local_bh_disable();
760 __sctp_hash_established(asoc);
761 sctp_local_bh_enable();
764 /* Remove association from the hash table. */
765 static void __sctp_unhash_established(struct sctp_association *asoc)
767 struct sctp_hashbucket *head;
768 struct sctp_ep_common *epb;
770 epb = &asoc->base;
772 epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port,
773 asoc->peer.port);
775 head = &sctp_assoc_hashtable[epb->hashent];
777 sctp_write_lock(&head->lock);
779 if (epb->pprev) {
780 if (epb->next)
781 epb->next->pprev = epb->pprev;
782 *epb->pprev = epb->next;
783 epb->pprev = NULL;
786 sctp_write_unlock(&head->lock);
789 /* Remove association from the hash table. Local BH-safe. */
790 void sctp_unhash_established(struct sctp_association *asoc)
792 sctp_local_bh_disable();
793 __sctp_unhash_established(asoc);
794 sctp_local_bh_enable();
797 /* Look up an association. */
798 static struct sctp_association *__sctp_lookup_association(
799 const union sctp_addr *local,
800 const union sctp_addr *peer,
801 struct sctp_transport **pt)
803 struct sctp_hashbucket *head;
804 struct sctp_ep_common *epb;
805 struct sctp_association *asoc;
806 struct sctp_transport *transport;
807 int hash;
809 /* Optimize here for direct hit, only listening connections can
810 * have wildcards anyways.
812 hash = sctp_assoc_hashfn(local->v4.sin_port, peer->v4.sin_port);
813 head = &sctp_assoc_hashtable[hash];
814 read_lock(&head->lock);
815 for (epb = head->chain; epb; epb = epb->next) {
816 asoc = sctp_assoc(epb);
817 transport = sctp_assoc_is_match(asoc, local, peer);
818 if (transport)
819 goto hit;
822 read_unlock(&head->lock);
824 return NULL;
826 hit:
827 *pt = transport;
828 sctp_association_hold(asoc);
829 sock_hold(epb->sk);
830 read_unlock(&head->lock);
831 return asoc;
834 /* Look up an association. BH-safe. */
835 SCTP_STATIC
836 struct sctp_association *sctp_lookup_association(const union sctp_addr *laddr,
837 const union sctp_addr *paddr,
838 struct sctp_transport **transportp)
840 struct sctp_association *asoc;
842 sctp_local_bh_disable();
843 asoc = __sctp_lookup_association(laddr, paddr, transportp);
844 sctp_local_bh_enable();
846 return asoc;
849 /* Is there an association matching the given local and peer addresses? */
850 int sctp_has_association(const union sctp_addr *laddr,
851 const union sctp_addr *paddr)
853 struct sctp_association *asoc;
854 struct sctp_transport *transport;
856 if ((asoc = sctp_lookup_association(laddr, paddr, &transport))) {
857 sock_put(asoc->base.sk);
858 sctp_association_put(asoc);
859 return 1;
862 return 0;
866 * SCTP Implementors Guide, 2.18 Handling of address
867 * parameters within the INIT or INIT-ACK.
869 * D) When searching for a matching TCB upon reception of an INIT
870 * or INIT-ACK chunk the receiver SHOULD use not only the
871 * source address of the packet (containing the INIT or
872 * INIT-ACK) but the receiver SHOULD also use all valid
873 * address parameters contained within the chunk.
875 * 2.18.3 Solution description
877 * This new text clearly specifies to an implementor the need
878 * to look within the INIT or INIT-ACK. Any implementation that
879 * does not do this, may not be able to establish associations
880 * in certain circumstances.
883 static struct sctp_association *__sctp_rcv_init_lookup(struct sk_buff *skb,
884 const union sctp_addr *laddr, struct sctp_transport **transportp)
886 struct sctp_association *asoc;
887 union sctp_addr addr;
888 union sctp_addr *paddr = &addr;
889 struct sctphdr *sh = (struct sctphdr *) skb->h.raw;
890 sctp_chunkhdr_t *ch;
891 union sctp_params params;
892 sctp_init_chunk_t *init;
893 struct sctp_transport *transport;
894 struct sctp_af *af;
896 ch = (sctp_chunkhdr_t *) skb->data;
898 /* If this is INIT/INIT-ACK look inside the chunk too. */
899 switch (ch->type) {
900 case SCTP_CID_INIT:
901 case SCTP_CID_INIT_ACK:
902 break;
903 default:
904 return NULL;
907 /* The code below will attempt to walk the chunk and extract
908 * parameter information. Before we do that, we need to verify
909 * that the chunk length doesn't cause overflow. Otherwise, we'll
910 * walk off the end.
912 if (WORD_ROUND(ntohs(ch->length)) > skb->len)
913 return NULL;
916 * This code will NOT touch anything inside the chunk--it is
917 * strictly READ-ONLY.
919 * RFC 2960 3 SCTP packet Format
921 * Multiple chunks can be bundled into one SCTP packet up to
922 * the MTU size, except for the INIT, INIT ACK, and SHUTDOWN
923 * COMPLETE chunks. These chunks MUST NOT be bundled with any
924 * other chunk in a packet. See Section 6.10 for more details
925 * on chunk bundling.
928 /* Find the start of the TLVs and the end of the chunk. This is
929 * the region we search for address parameters.
931 init = (sctp_init_chunk_t *)skb->data;
933 /* Walk the parameters looking for embedded addresses. */
934 sctp_walk_params(params, init, init_hdr.params) {
936 /* Note: Ignoring hostname addresses. */
937 af = sctp_get_af_specific(param_type2af(params.p->type));
938 if (!af)
939 continue;
941 af->from_addr_param(paddr, params.addr, ntohs(sh->source), 0);
943 asoc = __sctp_lookup_association(laddr, paddr, &transport);
944 if (asoc)
945 return asoc;
948 return NULL;
951 /* Lookup an association for an inbound skb. */
952 static struct sctp_association *__sctp_rcv_lookup(struct sk_buff *skb,
953 const union sctp_addr *paddr,
954 const union sctp_addr *laddr,
955 struct sctp_transport **transportp)
957 struct sctp_association *asoc;
959 asoc = __sctp_lookup_association(laddr, paddr, transportp);
961 /* Further lookup for INIT/INIT-ACK packets.
962 * SCTP Implementors Guide, 2.18 Handling of address
963 * parameters within the INIT or INIT-ACK.
965 if (!asoc)
966 asoc = __sctp_rcv_init_lookup(skb, laddr, transportp);
968 return asoc;