[PATCH] build fix: CONFIG_MEMORY_HOTPLUG=y on i386
[linux-2.6/sactl.git] / net / sctp / input.c
blob1662f9cc869e0a1cd463b7f3575c0c243dfc4ded
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);
76 static void sctp_add_backlog(struct sock *sk, struct sk_buff *skb);
79 /* Calculate the SCTP checksum of an SCTP packet. */
80 static inline int sctp_rcv_checksum(struct sk_buff *skb)
82 struct sctphdr *sh;
83 __u32 cmp, val;
84 struct sk_buff *list = skb_shinfo(skb)->frag_list;
86 sh = (struct sctphdr *) skb->h.raw;
87 cmp = ntohl(sh->checksum);
89 val = sctp_start_cksum((__u8 *)sh, skb_headlen(skb));
91 for (; list; list = list->next)
92 val = sctp_update_cksum((__u8 *)list->data, skb_headlen(list),
93 val);
95 val = sctp_end_cksum(val);
97 if (val != cmp) {
98 /* CRC failure, dump it. */
99 SCTP_INC_STATS_BH(SCTP_MIB_CHECKSUMERRORS);
100 return -1;
102 return 0;
105 struct 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;
114 #define SCTP_INPUT_CB(__skb) ((struct sctp_input_cb *)&((__skb)->cb[0]))
117 * This is the routine which IP calls when receiving an SCTP packet.
119 int sctp_rcv(struct sk_buff *skb)
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;
133 if (skb->pkt_type!=PACKET_HOST)
134 goto discard_it;
136 SCTP_INC_STATS_BH(SCTP_MIB_INSCTPPACKS);
138 sh = (struct sctphdr *) skb->h.raw;
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;
147 skb_pull(skb, sizeof(struct sctphdr));
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;
153 family = ipver2af(skb->nh.iph->version);
154 af = sctp_get_af_specific(family);
155 if (unlikely(!af))
156 goto discard_it;
158 /* Initialize local addresses for lookups. */
159 af->from_skb(&src, skb, 1);
160 af->from_skb(&dest, skb, 0);
162 /* If the packet is to or from a non-unicast address,
163 * silently discard the packet.
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."
173 if (!af->addr_valid(&src, NULL) || !af->addr_valid(&dest, NULL))
174 goto discard_it;
176 asoc = __sctp_rcv_lookup(skb, &src, &dest, &transport);
178 if (!asoc)
179 ep = __sctp_rcv_lookup_endpoint(&dest);
181 /* Retrieve the common input handling substructure. */
182 rcvr = asoc ? &asoc->base : &ep->base;
183 sk = rcvr->sk;
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
189 if (sk->sk_bound_dev_if && (sk->sk_bound_dev_if != af->skb_iif(skb)))
191 if (asoc) {
192 sctp_association_put(asoc);
193 asoc = NULL;
194 } else {
195 sctp_endpoint_put(ep);
196 ep = NULL;
198 sk = sctp_get_ctl_sock();
199 ep = sctp_sk(sk)->ep;
200 sctp_endpoint_hold(ep);
201 rcvr = &ep->base;
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.
212 if (!asoc) {
213 if (sctp_rcv_ootb(skb)) {
214 SCTP_INC_STATS_BH(SCTP_MIB_OUTOFBLUES);
215 goto discard_release;
219 /* SCTP seems to always need a timestamp right now (FIXME) */
220 if (skb->tstamp.off_sec == 0) {
221 __net_timestamp(skb);
222 sock_enable_timestamp(sk);
225 if (!xfrm_policy_check(sk, XFRM_POLICY_IN, skb, family))
226 goto discard_release;
227 nf_reset(skb);
229 if (sk_filter(sk, skb, 1))
230 goto discard_release;
232 /* Create an SCTP packet structure. */
233 chunk = sctp_chunkify(skb, asoc, sk);
234 if (!chunk)
235 goto discard_release;
236 SCTP_INPUT_CB(skb)->chunk = chunk;
238 /* Remember what endpoint is to handle this packet. */
239 chunk->rcvr = rcvr;
241 /* Remember the SCTP header. */
242 chunk->sctp_hdr = sh;
244 /* Set the source and destination addresses of the incoming chunk. */
245 sctp_init_addrs(chunk, &src, &dest);
247 /* Remember where we came from. */
248 chunk->transport = transport;
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.
254 sctp_bh_lock_sock(sk);
256 if (sock_owned_by_user(sk))
257 sctp_add_backlog(sk, skb);
258 else
259 sctp_inq_push(&chunk->rcvr->inqueue, chunk);
261 sctp_bh_unlock_sock(sk);
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);
269 return 0;
271 discard_it:
272 kfree_skb(skb);
273 return 0;
275 discard_release:
276 /* Release the asoc/ep ref we took in the lookup calls. */
277 if (asoc)
278 sctp_association_put(asoc);
279 else
280 sctp_endpoint_put(ep);
282 goto discard_it;
285 /* 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.
290 int sctp_backlog_rcv(struct sock *sk, struct sk_buff *skb)
292 struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
293 struct sctp_inq *inqueue = &chunk->rcvr->inqueue;
294 struct sctp_ep_common *rcvr = NULL;
295 int backloged = 0;
297 rcvr = chunk->rcvr;
299 /* 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.
303 if (rcvr->dead) {
304 sctp_chunk_free(chunk);
305 goto done;
308 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.
320 sk = rcvr->sk;
321 sctp_bh_lock_sock(sk);
323 if (sock_owned_by_user(sk)) {
324 sk_add_backlog(sk, skb);
325 backloged = 1;
326 } else
327 sctp_inq_push(inqueue, chunk);
329 sctp_bh_unlock_sock(sk);
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);
338 done:
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();
347 return 0;
350 static void sctp_add_backlog(struct sock *sk, struct sk_buff *skb)
352 struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
353 struct sctp_ep_common *rcvr = chunk->rcvr;
355 /* Hold the assoc/ep while hanging on the backlog queue.
356 * This way, we know structures we need will not disappear from us
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();
365 sk_add_backlog(sk, skb);
368 /* Handle icmp frag needed error. */
369 void sctp_icmp_frag_needed(struct sock *sk, struct sctp_association *asoc,
370 struct sctp_transport *t, __u32 pmtu)
372 if (sock_owned_by_user(sk) || !t || (t->pathmtu == pmtu))
373 return;
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.
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;
391 /* Update association pmtu. */
392 sctp_assoc_sync_pmtu(asoc);
395 /* 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.
401 sctp_retransmit(&asoc->outqueue, t, SCTP_RTXR_PMTUD);
405 * SCTP Implementer's Guide, 2.37 ICMP handling procedures
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.
411 * This function sends an event to the state machine, which will abort the
412 * association.
415 void sctp_icmp_proto_unreachable(struct sock *sk,
416 struct sctp_association *asoc,
417 struct sctp_transport *t)
419 SCTP_DEBUG_PRINTK("%s\n", __FUNCTION__);
421 sctp_do_sm(SCTP_EVENT_T_OTHER,
422 SCTP_ST_OTHER(SCTP_EVENT_ICMP_PROTO_UNREACH),
423 asoc->state, asoc->ep, asoc, t,
424 GFP_ATOMIC);
428 /* Common lookup code for icmp/icmpv6 error handler. */
429 struct sock *sctp_err_lookup(int family, struct sk_buff *skb,
430 struct sctphdr *sctphdr,
431 struct sctp_association **app,
432 struct sctp_transport **tpp)
434 union sctp_addr saddr;
435 union sctp_addr daddr;
436 struct sctp_af *af;
437 struct sock *sk = NULL;
438 struct sctp_association *asoc;
439 struct sctp_transport *transport = NULL;
441 *app = NULL; *tpp = NULL;
443 af = sctp_get_af_specific(family);
444 if (unlikely(!af)) {
445 return NULL;
448 /* Initialize local addresses for lookups. */
449 af->from_skb(&saddr, skb, 1);
450 af->from_skb(&daddr, skb, 0);
452 /* Look for an association that matches the incoming ICMP error
453 * packet.
455 asoc = __sctp_lookup_association(&saddr, &daddr, &transport);
456 if (!asoc)
457 return NULL;
459 sk = asoc->base.sk;
461 if (ntohl(sctphdr->vtag) != asoc->c.peer_vtag) {
462 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
463 goto out;
466 sctp_bh_lock_sock(sk);
468 /* If too many ICMPs get dropped on busy
469 * servers this needs to be solved differently.
471 if (sock_owned_by_user(sk))
472 NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);
474 *app = asoc;
475 *tpp = transport;
476 return sk;
478 out:
479 if (asoc)
480 sctp_association_put(asoc);
481 return NULL;
484 /* Common cleanup code for icmp/icmpv6 error handler. */
485 void sctp_err_finish(struct sock *sk, struct sctp_association *asoc)
487 sctp_bh_unlock_sock(sk);
488 if (asoc)
489 sctp_association_put(asoc);
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.
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.
507 void sctp_v4_err(struct sk_buff *skb, __u32 info)
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;
514 struct sctp_association *asoc = NULL;
515 struct sctp_transport *transport;
516 struct inet_sock *inet;
517 char *saveip, *savesctp;
518 int err;
520 if (skb->len < ((iph->ihl << 2) + 8)) {
521 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
522 return;
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;
530 sk = sctp_err_lookup(AF_INET, skb, sh, &asoc, &transport);
531 /* 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;
538 /* Warning: The sock lock is held. Remember to call
539 * sctp_err_finish!
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;
550 /* PMTU discovery (RFC1191) */
551 if (ICMP_FRAG_NEEDED == code) {
552 sctp_icmp_frag_needed(sk, asoc, transport, info);
553 goto out_unlock;
555 else {
556 if (ICMP_PROT_UNREACH == code) {
557 sctp_icmp_proto_unreachable(sk, asoc,
558 transport);
559 goto out_unlock;
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.
568 if (ICMP_EXC_FRAGTIME == code)
569 goto out_unlock;
571 err = EHOSTUNREACH;
572 break;
573 default:
574 goto out_unlock;
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;
585 out_unlock:
586 sctp_err_finish(sk, asoc);
590 * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
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.
597 * Output:
598 * Return 0 - If further processing is needed.
599 * Return 1 - If the packet can be discarded right away.
601 int sctp_rcv_ootb(struct sk_buff *skb)
603 sctp_chunkhdr_t *ch;
604 __u8 *ch_end;
605 sctp_errhdr_t *err;
607 ch = (sctp_chunkhdr_t *) skb->data;
609 /* Scan through all the chunks in the packet. */
610 do {
611 /* Break out if chunk length is less then minimal. */
612 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
613 break;
615 ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
616 if (ch_end > skb->tail)
617 break;
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.
623 if (SCTP_CID_ABORT == ch->type)
624 goto discard;
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.
630 if (SCTP_CID_SHUTDOWN_COMPLETE == ch->type)
631 goto discard;
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.
637 if (SCTP_CID_COOKIE_ACK == ch->type)
638 goto discard;
640 if (SCTP_CID_ERROR == ch->type) {
641 sctp_walk_errors(err, ch) {
642 if (SCTP_ERROR_STALE_COOKIE == err->cause)
643 goto discard;
647 ch = (sctp_chunkhdr_t *) ch_end;
648 } while (ch_end < skb->tail);
650 return 0;
652 discard:
653 return 1;
656 /* Insert endpoint into the hash table. */
657 static void __sctp_hash_endpoint(struct sctp_endpoint *ep)
659 struct sctp_ep_common **epp;
660 struct sctp_ep_common *epb;
661 struct sctp_hashbucket *head;
663 epb = &ep->base;
665 epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);
666 head = &sctp_ep_hashtable[epb->hashent];
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);
678 /* Add an endpoint to the hash. Local BH-safe. */
679 void sctp_hash_endpoint(struct sctp_endpoint *ep)
681 sctp_local_bh_disable();
682 __sctp_hash_endpoint(ep);
683 sctp_local_bh_enable();
686 /* Remove endpoint from the hash table. */
687 static void __sctp_unhash_endpoint(struct sctp_endpoint *ep)
689 struct sctp_hashbucket *head;
690 struct sctp_ep_common *epb;
692 epb = &ep->base;
694 epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);
696 head = &sctp_ep_hashtable[epb->hashent];
698 sctp_write_lock(&head->lock);
700 if (epb->pprev) {
701 if (epb->next)
702 epb->next->pprev = epb->pprev;
703 *epb->pprev = epb->next;
704 epb->pprev = NULL;
707 sctp_write_unlock(&head->lock);
710 /* Remove endpoint from the hash. Local BH-safe. */
711 void sctp_unhash_endpoint(struct sctp_endpoint *ep)
713 sctp_local_bh_disable();
714 __sctp_unhash_endpoint(ep);
715 sctp_local_bh_enable();
718 /* Look up an endpoint. */
719 static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(const union sctp_addr *laddr)
721 struct sctp_hashbucket *head;
722 struct sctp_ep_common *epb;
723 struct sctp_endpoint *ep;
724 int hash;
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;
735 ep = sctp_sk((sctp_get_ctl_sock()))->ep;
736 epb = &ep->base;
738 hit:
739 sctp_endpoint_hold(ep);
740 read_unlock(&head->lock);
741 return ep;
744 /* Insert association into the hash table. */
745 static void __sctp_hash_established(struct sctp_association *asoc)
747 struct sctp_ep_common **epp;
748 struct sctp_ep_common *epb;
749 struct sctp_hashbucket *head;
751 epb = &asoc->base;
753 /* Calculate which chain this entry will belong to. */
754 epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port, asoc->peer.port);
756 head = &sctp_assoc_hashtable[epb->hashent];
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);
768 /* Add an association to the hash. Local BH-safe. */
769 void sctp_hash_established(struct sctp_association *asoc)
771 sctp_local_bh_disable();
772 __sctp_hash_established(asoc);
773 sctp_local_bh_enable();
776 /* Remove association from the hash table. */
777 static void __sctp_unhash_established(struct sctp_association *asoc)
779 struct sctp_hashbucket *head;
780 struct sctp_ep_common *epb;
782 epb = &asoc->base;
784 epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port,
785 asoc->peer.port);
787 head = &sctp_assoc_hashtable[epb->hashent];
789 sctp_write_lock(&head->lock);
791 if (epb->pprev) {
792 if (epb->next)
793 epb->next->pprev = epb->pprev;
794 *epb->pprev = epb->next;
795 epb->pprev = NULL;
798 sctp_write_unlock(&head->lock);
801 /* Remove association from the hash table. Local BH-safe. */
802 void sctp_unhash_established(struct sctp_association *asoc)
804 sctp_local_bh_disable();
805 __sctp_unhash_established(asoc);
806 sctp_local_bh_enable();
809 /* Look up an association. */
810 static struct sctp_association *__sctp_lookup_association(
811 const union sctp_addr *local,
812 const union sctp_addr *peer,
813 struct sctp_transport **pt)
815 struct sctp_hashbucket *head;
816 struct sctp_ep_common *epb;
817 struct sctp_association *asoc;
818 struct sctp_transport *transport;
819 int hash;
821 /* Optimize here for direct hit, only listening connections can
822 * have wildcards anyways.
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;
834 read_unlock(&head->lock);
836 return NULL;
838 hit:
839 *pt = transport;
840 sctp_association_hold(asoc);
841 read_unlock(&head->lock);
842 return asoc;
845 /* Look up an association. BH-safe. */
846 SCTP_STATIC
847 struct sctp_association *sctp_lookup_association(const union sctp_addr *laddr,
848 const union sctp_addr *paddr,
849 struct sctp_transport **transportp)
851 struct sctp_association *asoc;
853 sctp_local_bh_disable();
854 asoc = __sctp_lookup_association(laddr, paddr, transportp);
855 sctp_local_bh_enable();
857 return asoc;
860 /* Is there an association matching the given local and peer addresses? */
861 int sctp_has_association(const union sctp_addr *laddr,
862 const union sctp_addr *paddr)
864 struct sctp_association *asoc;
865 struct sctp_transport *transport;
867 if ((asoc = sctp_lookup_association(laddr, paddr, &transport))) {
868 sctp_association_put(asoc);
869 return 1;
872 return 0;
876 * SCTP Implementors Guide, 2.18 Handling of address
877 * parameters within the INIT or INIT-ACK.
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.
885 * 2.18.3 Solution description
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.
893 static struct sctp_association *__sctp_rcv_init_lookup(struct sk_buff *skb,
894 const union sctp_addr *laddr, struct sctp_transport **transportp)
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;
906 ch = (sctp_chunkhdr_t *) skb->data;
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;
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.
922 if (WORD_ROUND(ntohs(ch->length)) > skb->len)
923 return NULL;
926 * This code will NOT touch anything inside the chunk--it is
927 * strictly READ-ONLY.
929 * RFC 2960 3 SCTP packet Format
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.
938 /* Find the start of the TLVs and the end of the chunk. This is
939 * the region we search for address parameters.
941 init = (sctp_init_chunk_t *)skb->data;
943 /* Walk the parameters looking for embedded addresses. */
944 sctp_walk_params(params, init, init_hdr.params) {
946 /* Note: Ignoring hostname addresses. */
947 af = sctp_get_af_specific(param_type2af(params.p->type));
948 if (!af)
949 continue;
951 af->from_addr_param(paddr, params.addr, ntohs(sh->source), 0);
953 asoc = __sctp_lookup_association(laddr, paddr, &transport);
954 if (asoc)
955 return asoc;
958 return NULL;
961 /* Lookup an association for an inbound skb. */
962 static 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)
967 struct sctp_association *asoc;
969 asoc = __sctp_lookup_association(laddr, paddr, transportp);
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.
975 if (!asoc)
976 asoc = __sctp_rcv_init_lookup(skb, laddr, transportp);
978 return asoc;