[SCTP]: Switch sctp_assoc_is_match to net-endian.
[linux-2.6/mini2440.git] / net / sctp / input.c
blob85b3441c4a23fa52b4f2c7daf50e2fc024b9484b
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 union sctp_addr tmp, tmp2;
131 int family;
132 struct sctp_af *af;
134 if (skb->pkt_type!=PACKET_HOST)
135 goto discard_it;
137 SCTP_INC_STATS_BH(SCTP_MIB_INSCTPPACKS);
139 if (skb_linearize(skb))
140 goto discard_it;
142 sh = (struct sctphdr *) skb->h.raw;
144 /* Pull up the IP and SCTP headers. */
145 __skb_pull(skb, skb->h.raw - skb->data);
146 if (skb->len < sizeof(struct sctphdr))
147 goto discard_it;
148 if ((skb->ip_summed != CHECKSUM_UNNECESSARY) &&
149 (sctp_rcv_checksum(skb) < 0))
150 goto discard_it;
152 skb_pull(skb, sizeof(struct sctphdr));
154 /* Make sure we at least have chunk headers worth of data left. */
155 if (skb->len < sizeof(struct sctp_chunkhdr))
156 goto discard_it;
158 family = ipver2af(skb->nh.iph->version);
159 af = sctp_get_af_specific(family);
160 if (unlikely(!af))
161 goto discard_it;
163 /* Initialize local addresses for lookups. */
164 af->from_skb(&src, skb, 1);
165 af->from_skb(&dest, skb, 0);
167 /* If the packet is to or from a non-unicast address,
168 * silently discard the packet.
170 * This is not clearly defined in the RFC except in section
171 * 8.4 - OOTB handling. However, based on the book "Stream Control
172 * Transmission Protocol" 2.1, "It is important to note that the
173 * IP address of an SCTP transport address must be a routable
174 * unicast address. In other words, IP multicast addresses and
175 * IP broadcast addresses cannot be used in an SCTP transport
176 * address."
178 if (!af->addr_valid(&src, NULL, skb) ||
179 !af->addr_valid(&dest, NULL, skb))
180 goto discard_it;
182 flip_to_n(&tmp, &dest);
183 flip_to_n(&tmp2, &src);
185 asoc = __sctp_rcv_lookup(skb, &tmp2, &tmp, &transport);
187 if (!asoc)
188 ep = __sctp_rcv_lookup_endpoint(&tmp);
190 /* Retrieve the common input handling substructure. */
191 rcvr = asoc ? &asoc->base : &ep->base;
192 sk = rcvr->sk;
195 * If a frame arrives on an interface and the receiving socket is
196 * bound to another interface, via SO_BINDTODEVICE, treat it as OOTB
198 if (sk->sk_bound_dev_if && (sk->sk_bound_dev_if != af->skb_iif(skb)))
200 if (asoc) {
201 sctp_association_put(asoc);
202 asoc = NULL;
203 } else {
204 sctp_endpoint_put(ep);
205 ep = NULL;
207 sk = sctp_get_ctl_sock();
208 ep = sctp_sk(sk)->ep;
209 sctp_endpoint_hold(ep);
210 rcvr = &ep->base;
214 * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
215 * An SCTP packet is called an "out of the blue" (OOTB)
216 * packet if it is correctly formed, i.e., passed the
217 * receiver's checksum check, but the receiver is not
218 * able to identify the association to which this
219 * packet belongs.
221 if (!asoc) {
222 if (sctp_rcv_ootb(skb)) {
223 SCTP_INC_STATS_BH(SCTP_MIB_OUTOFBLUES);
224 goto discard_release;
228 if (!xfrm_policy_check(sk, XFRM_POLICY_IN, skb, family))
229 goto discard_release;
230 nf_reset(skb);
232 if (sk_filter(sk, skb))
233 goto discard_release;
235 /* Create an SCTP packet structure. */
236 chunk = sctp_chunkify(skb, asoc, sk);
237 if (!chunk)
238 goto discard_release;
239 SCTP_INPUT_CB(skb)->chunk = chunk;
241 /* Remember what endpoint is to handle this packet. */
242 chunk->rcvr = rcvr;
244 /* Remember the SCTP header. */
245 chunk->sctp_hdr = sh;
247 /* Set the source and destination addresses of the incoming chunk. */
248 sctp_init_addrs(chunk, &src, &dest);
250 /* Remember where we came from. */
251 chunk->transport = transport;
253 /* Acquire access to the sock lock. Note: We are safe from other
254 * bottom halves on this lock, but a user may be in the lock too,
255 * so check if it is busy.
257 sctp_bh_lock_sock(sk);
259 if (sock_owned_by_user(sk)) {
260 SCTP_INC_STATS_BH(SCTP_MIB_IN_PKT_BACKLOG);
261 sctp_add_backlog(sk, skb);
262 } else {
263 SCTP_INC_STATS_BH(SCTP_MIB_IN_PKT_SOFTIRQ);
264 sctp_inq_push(&chunk->rcvr->inqueue, chunk);
267 sctp_bh_unlock_sock(sk);
269 /* Release the asoc/ep ref we took in the lookup calls. */
270 if (asoc)
271 sctp_association_put(asoc);
272 else
273 sctp_endpoint_put(ep);
275 return 0;
277 discard_it:
278 SCTP_INC_STATS_BH(SCTP_MIB_IN_PKT_DISCARDS);
279 kfree_skb(skb);
280 return 0;
282 discard_release:
283 /* Release the asoc/ep ref we took in the lookup calls. */
284 if (asoc)
285 sctp_association_put(asoc);
286 else
287 sctp_endpoint_put(ep);
289 goto discard_it;
292 /* Process the backlog queue of the socket. Every skb on
293 * the backlog holds a ref on an association or endpoint.
294 * We hold this ref throughout the state machine to make
295 * sure that the structure we need is still around.
297 int sctp_backlog_rcv(struct sock *sk, struct sk_buff *skb)
299 struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
300 struct sctp_inq *inqueue = &chunk->rcvr->inqueue;
301 struct sctp_ep_common *rcvr = NULL;
302 int backloged = 0;
304 rcvr = chunk->rcvr;
306 /* If the rcvr is dead then the association or endpoint
307 * has been deleted and we can safely drop the chunk
308 * and refs that we are holding.
310 if (rcvr->dead) {
311 sctp_chunk_free(chunk);
312 goto done;
315 if (unlikely(rcvr->sk != sk)) {
316 /* In this case, the association moved from one socket to
317 * another. We are currently sitting on the backlog of the
318 * old socket, so we need to move.
319 * However, since we are here in the process context we
320 * need to take make sure that the user doesn't own
321 * the new socket when we process the packet.
322 * If the new socket is user-owned, queue the chunk to the
323 * backlog of the new socket without dropping any refs.
324 * Otherwise, we can safely push the chunk on the inqueue.
327 sk = rcvr->sk;
328 sctp_bh_lock_sock(sk);
330 if (sock_owned_by_user(sk)) {
331 sk_add_backlog(sk, skb);
332 backloged = 1;
333 } else
334 sctp_inq_push(inqueue, chunk);
336 sctp_bh_unlock_sock(sk);
338 /* If the chunk was backloged again, don't drop refs */
339 if (backloged)
340 return 0;
341 } else {
342 sctp_inq_push(inqueue, chunk);
345 done:
346 /* Release the refs we took in sctp_add_backlog */
347 if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
348 sctp_association_put(sctp_assoc(rcvr));
349 else if (SCTP_EP_TYPE_SOCKET == rcvr->type)
350 sctp_endpoint_put(sctp_ep(rcvr));
351 else
352 BUG();
354 return 0;
357 static void sctp_add_backlog(struct sock *sk, struct sk_buff *skb)
359 struct sctp_chunk *chunk = SCTP_INPUT_CB(skb)->chunk;
360 struct sctp_ep_common *rcvr = chunk->rcvr;
362 /* Hold the assoc/ep while hanging on the backlog queue.
363 * This way, we know structures we need will not disappear from us
365 if (SCTP_EP_TYPE_ASSOCIATION == rcvr->type)
366 sctp_association_hold(sctp_assoc(rcvr));
367 else if (SCTP_EP_TYPE_SOCKET == rcvr->type)
368 sctp_endpoint_hold(sctp_ep(rcvr));
369 else
370 BUG();
372 sk_add_backlog(sk, skb);
375 /* Handle icmp frag needed error. */
376 void sctp_icmp_frag_needed(struct sock *sk, struct sctp_association *asoc,
377 struct sctp_transport *t, __u32 pmtu)
379 if (sock_owned_by_user(sk) || !t || (t->pathmtu == pmtu))
380 return;
382 if (t->param_flags & SPP_PMTUD_ENABLE) {
383 if (unlikely(pmtu < SCTP_DEFAULT_MINSEGMENT)) {
384 printk(KERN_WARNING "%s: Reported pmtu %d too low, "
385 "using default minimum of %d\n",
386 __FUNCTION__, pmtu,
387 SCTP_DEFAULT_MINSEGMENT);
388 /* Use default minimum segment size and disable
389 * pmtu discovery on this transport.
391 t->pathmtu = SCTP_DEFAULT_MINSEGMENT;
392 t->param_flags = (t->param_flags & ~SPP_PMTUD) |
393 SPP_PMTUD_DISABLE;
394 } else {
395 t->pathmtu = pmtu;
398 /* Update association pmtu. */
399 sctp_assoc_sync_pmtu(asoc);
402 /* Retransmit with the new pmtu setting.
403 * Normally, if PMTU discovery is disabled, an ICMP Fragmentation
404 * Needed will never be sent, but if a message was sent before
405 * PMTU discovery was disabled that was larger than the PMTU, it
406 * would not be fragmented, so it must be re-transmitted fragmented.
408 sctp_retransmit(&asoc->outqueue, t, SCTP_RTXR_PMTUD);
412 * SCTP Implementer's Guide, 2.37 ICMP handling procedures
414 * ICMP8) If the ICMP code is a "Unrecognized next header type encountered"
415 * or a "Protocol Unreachable" treat this message as an abort
416 * with the T bit set.
418 * This function sends an event to the state machine, which will abort the
419 * association.
422 void sctp_icmp_proto_unreachable(struct sock *sk,
423 struct sctp_association *asoc,
424 struct sctp_transport *t)
426 SCTP_DEBUG_PRINTK("%s\n", __FUNCTION__);
428 sctp_do_sm(SCTP_EVENT_T_OTHER,
429 SCTP_ST_OTHER(SCTP_EVENT_ICMP_PROTO_UNREACH),
430 asoc->state, asoc->ep, asoc, t,
431 GFP_ATOMIC);
435 /* Common lookup code for icmp/icmpv6 error handler. */
436 struct sock *sctp_err_lookup(int family, struct sk_buff *skb,
437 struct sctphdr *sctphdr,
438 struct sctp_association **app,
439 struct sctp_transport **tpp)
441 union sctp_addr saddr;
442 union sctp_addr daddr;
443 struct sctp_af *af;
444 struct sock *sk = NULL;
445 struct sctp_association *asoc;
446 struct sctp_transport *transport = NULL;
447 union sctp_addr tmp, tmp2;
449 *app = NULL; *tpp = NULL;
451 af = sctp_get_af_specific(family);
452 if (unlikely(!af)) {
453 return NULL;
456 /* Initialize local addresses for lookups. */
457 af->from_skb(&saddr, skb, 1);
458 af->from_skb(&daddr, skb, 0);
459 flip_to_n(&tmp, &saddr);
460 flip_to_n(&tmp2, &daddr);
462 /* Look for an association that matches the incoming ICMP error
463 * packet.
465 asoc = __sctp_lookup_association(&tmp, &tmp2, &transport);
466 if (!asoc)
467 return NULL;
469 sk = asoc->base.sk;
471 if (ntohl(sctphdr->vtag) != asoc->c.peer_vtag) {
472 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
473 goto out;
476 sctp_bh_lock_sock(sk);
478 /* If too many ICMPs get dropped on busy
479 * servers this needs to be solved differently.
481 if (sock_owned_by_user(sk))
482 NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);
484 *app = asoc;
485 *tpp = transport;
486 return sk;
488 out:
489 if (asoc)
490 sctp_association_put(asoc);
491 return NULL;
494 /* Common cleanup code for icmp/icmpv6 error handler. */
495 void sctp_err_finish(struct sock *sk, struct sctp_association *asoc)
497 sctp_bh_unlock_sock(sk);
498 if (asoc)
499 sctp_association_put(asoc);
503 * This routine is called by the ICMP module when it gets some
504 * sort of error condition. If err < 0 then the socket should
505 * be closed and the error returned to the user. If err > 0
506 * it's just the icmp type << 8 | icmp code. After adjustment
507 * header points to the first 8 bytes of the sctp header. We need
508 * to find the appropriate port.
510 * The locking strategy used here is very "optimistic". When
511 * someone else accesses the socket the ICMP is just dropped
512 * and for some paths there is no check at all.
513 * A more general error queue to queue errors for later handling
514 * is probably better.
517 void sctp_v4_err(struct sk_buff *skb, __u32 info)
519 struct iphdr *iph = (struct iphdr *)skb->data;
520 struct sctphdr *sh = (struct sctphdr *)(skb->data + (iph->ihl <<2));
521 int type = skb->h.icmph->type;
522 int code = skb->h.icmph->code;
523 struct sock *sk;
524 struct sctp_association *asoc = NULL;
525 struct sctp_transport *transport;
526 struct inet_sock *inet;
527 char *saveip, *savesctp;
528 int err;
530 if (skb->len < ((iph->ihl << 2) + 8)) {
531 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
532 return;
535 /* Fix up skb to look at the embedded net header. */
536 saveip = skb->nh.raw;
537 savesctp = skb->h.raw;
538 skb->nh.iph = iph;
539 skb->h.raw = (char *)sh;
540 sk = sctp_err_lookup(AF_INET, skb, sh, &asoc, &transport);
541 /* Put back, the original pointers. */
542 skb->nh.raw = saveip;
543 skb->h.raw = savesctp;
544 if (!sk) {
545 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
546 return;
548 /* Warning: The sock lock is held. Remember to call
549 * sctp_err_finish!
552 switch (type) {
553 case ICMP_PARAMETERPROB:
554 err = EPROTO;
555 break;
556 case ICMP_DEST_UNREACH:
557 if (code > NR_ICMP_UNREACH)
558 goto out_unlock;
560 /* PMTU discovery (RFC1191) */
561 if (ICMP_FRAG_NEEDED == code) {
562 sctp_icmp_frag_needed(sk, asoc, transport, info);
563 goto out_unlock;
565 else {
566 if (ICMP_PROT_UNREACH == code) {
567 sctp_icmp_proto_unreachable(sk, asoc,
568 transport);
569 goto out_unlock;
572 err = icmp_err_convert[code].errno;
573 break;
574 case ICMP_TIME_EXCEEDED:
575 /* Ignore any time exceeded errors due to fragment reassembly
576 * timeouts.
578 if (ICMP_EXC_FRAGTIME == code)
579 goto out_unlock;
581 err = EHOSTUNREACH;
582 break;
583 default:
584 goto out_unlock;
587 inet = inet_sk(sk);
588 if (!sock_owned_by_user(sk) && inet->recverr) {
589 sk->sk_err = err;
590 sk->sk_error_report(sk);
591 } else { /* Only an error on timeout */
592 sk->sk_err_soft = err;
595 out_unlock:
596 sctp_err_finish(sk, asoc);
600 * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
602 * This function scans all the chunks in the OOTB packet to determine if
603 * the packet should be discarded right away. If a response might be needed
604 * for this packet, or, if further processing is possible, the packet will
605 * be queued to a proper inqueue for the next phase of handling.
607 * Output:
608 * Return 0 - If further processing is needed.
609 * Return 1 - If the packet can be discarded right away.
611 int sctp_rcv_ootb(struct sk_buff *skb)
613 sctp_chunkhdr_t *ch;
614 __u8 *ch_end;
615 sctp_errhdr_t *err;
617 ch = (sctp_chunkhdr_t *) skb->data;
619 /* Scan through all the chunks in the packet. */
620 do {
621 /* Break out if chunk length is less then minimal. */
622 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
623 break;
625 ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
626 if (ch_end > skb->tail)
627 break;
629 /* RFC 8.4, 2) If the OOTB packet contains an ABORT chunk, the
630 * receiver MUST silently discard the OOTB packet and take no
631 * further action.
633 if (SCTP_CID_ABORT == ch->type)
634 goto discard;
636 /* RFC 8.4, 6) If the packet contains a SHUTDOWN COMPLETE
637 * chunk, the receiver should silently discard the packet
638 * and take no further action.
640 if (SCTP_CID_SHUTDOWN_COMPLETE == ch->type)
641 goto discard;
643 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
644 * or a COOKIE ACK the SCTP Packet should be silently
645 * discarded.
647 if (SCTP_CID_COOKIE_ACK == ch->type)
648 goto discard;
650 if (SCTP_CID_ERROR == ch->type) {
651 sctp_walk_errors(err, ch) {
652 if (SCTP_ERROR_STALE_COOKIE == err->cause)
653 goto discard;
657 ch = (sctp_chunkhdr_t *) ch_end;
658 } while (ch_end < skb->tail);
660 return 0;
662 discard:
663 return 1;
666 /* Insert endpoint into the hash table. */
667 static void __sctp_hash_endpoint(struct sctp_endpoint *ep)
669 struct sctp_ep_common **epp;
670 struct sctp_ep_common *epb;
671 struct sctp_hashbucket *head;
673 epb = &ep->base;
675 epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);
676 head = &sctp_ep_hashtable[epb->hashent];
678 sctp_write_lock(&head->lock);
679 epp = &head->chain;
680 epb->next = *epp;
681 if (epb->next)
682 (*epp)->pprev = &epb->next;
683 *epp = epb;
684 epb->pprev = epp;
685 sctp_write_unlock(&head->lock);
688 /* Add an endpoint to the hash. Local BH-safe. */
689 void sctp_hash_endpoint(struct sctp_endpoint *ep)
691 sctp_local_bh_disable();
692 __sctp_hash_endpoint(ep);
693 sctp_local_bh_enable();
696 /* Remove endpoint from the hash table. */
697 static void __sctp_unhash_endpoint(struct sctp_endpoint *ep)
699 struct sctp_hashbucket *head;
700 struct sctp_ep_common *epb;
702 epb = &ep->base;
704 epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);
706 head = &sctp_ep_hashtable[epb->hashent];
708 sctp_write_lock(&head->lock);
710 if (epb->pprev) {
711 if (epb->next)
712 epb->next->pprev = epb->pprev;
713 *epb->pprev = epb->next;
714 epb->pprev = NULL;
717 sctp_write_unlock(&head->lock);
720 /* Remove endpoint from the hash. Local BH-safe. */
721 void sctp_unhash_endpoint(struct sctp_endpoint *ep)
723 sctp_local_bh_disable();
724 __sctp_unhash_endpoint(ep);
725 sctp_local_bh_enable();
728 /* Look up an endpoint. */
729 static struct sctp_endpoint *__sctp_rcv_lookup_endpoint(const union sctp_addr *laddr)
731 struct sctp_hashbucket *head;
732 struct sctp_ep_common *epb;
733 struct sctp_endpoint *ep;
734 int hash;
736 hash = sctp_ep_hashfn(ntohs(laddr->v4.sin_port));
737 head = &sctp_ep_hashtable[hash];
738 read_lock(&head->lock);
739 for (epb = head->chain; epb; epb = epb->next) {
740 ep = sctp_ep(epb);
741 if (sctp_endpoint_is_match(ep, laddr))
742 goto hit;
745 ep = sctp_sk((sctp_get_ctl_sock()))->ep;
746 epb = &ep->base;
748 hit:
749 sctp_endpoint_hold(ep);
750 read_unlock(&head->lock);
751 return ep;
754 /* Insert association into the hash table. */
755 static void __sctp_hash_established(struct sctp_association *asoc)
757 struct sctp_ep_common **epp;
758 struct sctp_ep_common *epb;
759 struct sctp_hashbucket *head;
761 epb = &asoc->base;
763 /* Calculate which chain this entry will belong to. */
764 epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port, asoc->peer.port);
766 head = &sctp_assoc_hashtable[epb->hashent];
768 sctp_write_lock(&head->lock);
769 epp = &head->chain;
770 epb->next = *epp;
771 if (epb->next)
772 (*epp)->pprev = &epb->next;
773 *epp = epb;
774 epb->pprev = epp;
775 sctp_write_unlock(&head->lock);
778 /* Add an association to the hash. Local BH-safe. */
779 void sctp_hash_established(struct sctp_association *asoc)
781 if (asoc->temp)
782 return;
784 sctp_local_bh_disable();
785 __sctp_hash_established(asoc);
786 sctp_local_bh_enable();
789 /* Remove association from the hash table. */
790 static void __sctp_unhash_established(struct sctp_association *asoc)
792 struct sctp_hashbucket *head;
793 struct sctp_ep_common *epb;
795 epb = &asoc->base;
797 epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port,
798 asoc->peer.port);
800 head = &sctp_assoc_hashtable[epb->hashent];
802 sctp_write_lock(&head->lock);
804 if (epb->pprev) {
805 if (epb->next)
806 epb->next->pprev = epb->pprev;
807 *epb->pprev = epb->next;
808 epb->pprev = NULL;
811 sctp_write_unlock(&head->lock);
814 /* Remove association from the hash table. Local BH-safe. */
815 void sctp_unhash_established(struct sctp_association *asoc)
817 if (asoc->temp)
818 return;
820 sctp_local_bh_disable();
821 __sctp_unhash_established(asoc);
822 sctp_local_bh_enable();
825 /* Look up an association. */
826 static struct sctp_association *__sctp_lookup_association(
827 const union sctp_addr *local,
828 const union sctp_addr *peer,
829 struct sctp_transport **pt)
831 struct sctp_hashbucket *head;
832 struct sctp_ep_common *epb;
833 struct sctp_association *asoc;
834 struct sctp_transport *transport;
835 int hash;
837 /* Optimize here for direct hit, only listening connections can
838 * have wildcards anyways.
840 hash = sctp_assoc_hashfn(ntohs(local->v4.sin_port), ntohs(peer->v4.sin_port));
841 head = &sctp_assoc_hashtable[hash];
842 read_lock(&head->lock);
843 for (epb = head->chain; epb; epb = epb->next) {
844 asoc = sctp_assoc(epb);
845 transport = sctp_assoc_is_match(asoc, local, peer);
846 if (transport)
847 goto hit;
850 read_unlock(&head->lock);
852 return NULL;
854 hit:
855 *pt = transport;
856 sctp_association_hold(asoc);
857 read_unlock(&head->lock);
858 return asoc;
861 /* Look up an association. BH-safe. */
862 SCTP_STATIC
863 struct sctp_association *sctp_lookup_association(const union sctp_addr *laddr,
864 const union sctp_addr *paddr,
865 struct sctp_transport **transportp)
867 struct sctp_association *asoc;
869 sctp_local_bh_disable();
870 asoc = __sctp_lookup_association(laddr, paddr, transportp);
871 sctp_local_bh_enable();
873 return asoc;
876 /* Is there an association matching the given local and peer addresses? */
877 int sctp_has_association(const union sctp_addr *laddr,
878 const union sctp_addr *paddr)
880 struct sctp_association *asoc;
881 struct sctp_transport *transport;
882 union sctp_addr tmp, tmp2;
883 flip_to_n(&tmp, laddr);
884 flip_to_n(&tmp2, paddr);
886 if ((asoc = sctp_lookup_association(&tmp, &tmp2, &transport))) {
887 sctp_association_put(asoc);
888 return 1;
891 return 0;
895 * SCTP Implementors Guide, 2.18 Handling of address
896 * parameters within the INIT or INIT-ACK.
898 * D) When searching for a matching TCB upon reception of an INIT
899 * or INIT-ACK chunk the receiver SHOULD use not only the
900 * source address of the packet (containing the INIT or
901 * INIT-ACK) but the receiver SHOULD also use all valid
902 * address parameters contained within the chunk.
904 * 2.18.3 Solution description
906 * This new text clearly specifies to an implementor the need
907 * to look within the INIT or INIT-ACK. Any implementation that
908 * does not do this, may not be able to establish associations
909 * in certain circumstances.
912 static struct sctp_association *__sctp_rcv_init_lookup(struct sk_buff *skb,
913 const union sctp_addr *laddr, struct sctp_transport **transportp)
915 struct sctp_association *asoc;
916 union sctp_addr addr;
917 union sctp_addr *paddr = &addr;
918 struct sctphdr *sh = (struct sctphdr *) skb->h.raw;
919 sctp_chunkhdr_t *ch;
920 union sctp_params params;
921 sctp_init_chunk_t *init;
922 struct sctp_transport *transport;
923 struct sctp_af *af;
924 union sctp_addr tmp2;
926 ch = (sctp_chunkhdr_t *) skb->data;
928 /* If this is INIT/INIT-ACK look inside the chunk too. */
929 switch (ch->type) {
930 case SCTP_CID_INIT:
931 case SCTP_CID_INIT_ACK:
932 break;
933 default:
934 return NULL;
937 /* The code below will attempt to walk the chunk and extract
938 * parameter information. Before we do that, we need to verify
939 * that the chunk length doesn't cause overflow. Otherwise, we'll
940 * walk off the end.
942 if (WORD_ROUND(ntohs(ch->length)) > skb->len)
943 return NULL;
946 * This code will NOT touch anything inside the chunk--it is
947 * strictly READ-ONLY.
949 * RFC 2960 3 SCTP packet Format
951 * Multiple chunks can be bundled into one SCTP packet up to
952 * the MTU size, except for the INIT, INIT ACK, and SHUTDOWN
953 * COMPLETE chunks. These chunks MUST NOT be bundled with any
954 * other chunk in a packet. See Section 6.10 for more details
955 * on chunk bundling.
958 /* Find the start of the TLVs and the end of the chunk. This is
959 * the region we search for address parameters.
961 init = (sctp_init_chunk_t *)skb->data;
963 /* Walk the parameters looking for embedded addresses. */
964 sctp_walk_params(params, init, init_hdr.params) {
966 /* Note: Ignoring hostname addresses. */
967 af = sctp_get_af_specific(param_type2af(params.p->type));
968 if (!af)
969 continue;
971 af->from_addr_param(paddr, params.addr, ntohs(sh->source), 0);
972 flip_to_n(&tmp2, paddr);
974 asoc = __sctp_lookup_association(laddr, &tmp2, &transport);
975 if (asoc)
976 return asoc;
979 return NULL;
982 /* Lookup an association for an inbound skb. */
983 static struct sctp_association *__sctp_rcv_lookup(struct sk_buff *skb,
984 const union sctp_addr *paddr,
985 const union sctp_addr *laddr,
986 struct sctp_transport **transportp)
988 struct sctp_association *asoc;
990 asoc = __sctp_lookup_association(laddr, paddr, transportp);
992 /* Further lookup for INIT/INIT-ACK packets.
993 * SCTP Implementors Guide, 2.18 Handling of address
994 * parameters within the INIT or INIT-ACK.
996 if (!asoc)
997 asoc = __sctp_rcv_init_lookup(skb, laddr, transportp);
999 return asoc;