ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / sctp / input.c
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
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. */
65 static int sctp_rcv_ootb(struct sk_buff *);
66 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 struct sctp_endpoint *__sctp_rcv_lookup_endpoint(const union sctp_addr *laddr);
71
72
73 /* Calculate the SCTP checksum of an SCTP packet.  */
74 static inline int sctp_rcv_checksum(struct sk_buff *skb)
75 {
76         struct sctphdr *sh;
77         __u32 cmp, val;
78         struct sk_buff *list = skb_shinfo(skb)->frag_list;
79
80         sh = (struct sctphdr *) skb->h.raw;
81         cmp = ntohl(sh->checksum);
82
83         val = sctp_start_cksum((__u8 *)sh, skb_headlen(skb));
84
85         for (; list; list = list->next)
86                 val = sctp_update_cksum((__u8 *)list->data, skb_headlen(list),
87                                         val);
88
89         val = sctp_end_cksum(val);
90
91         if (val != cmp) {
92                 /* CRC failure, dump it. */
93                 SCTP_INC_STATS_BH(SctpChecksumErrors);
94                 return -1;
95         }
96         return 0;
97 }
98
99 /*
100  * This is the routine which IP calls when receiving an SCTP packet.
101  */
102 int sctp_rcv(struct sk_buff *skb)
103 {
104         struct sock *sk;
105         struct sctp_association *asoc;
106         struct sctp_endpoint *ep = NULL;
107         struct sctp_ep_common *rcvr;
108         struct sctp_transport *transport = NULL;
109         struct sctp_chunk *chunk;
110         struct sctphdr *sh;
111         union sctp_addr src;
112         union sctp_addr dest;
113         int family;
114         struct sctp_af *af;
115         int ret = 0;
116
117         if (skb->pkt_type!=PACKET_HOST)
118                 goto discard_it;
119
120         SCTP_INC_STATS_BH(SctpInSCTPPacks);
121
122         sh = (struct sctphdr *) skb->h.raw;
123
124         /* Pull up the IP and SCTP headers. */
125         __skb_pull(skb, skb->h.raw - skb->data);
126         if (skb->len < sizeof(struct sctphdr))
127                 goto discard_it;
128         if (sctp_rcv_checksum(skb) < 0)
129                 goto discard_it;
130
131         skb_pull(skb, sizeof(struct sctphdr));
132
133         family = ipver2af(skb->nh.iph->version);
134         af = sctp_get_af_specific(family);
135         if (unlikely(!af))
136                 goto discard_it;
137
138         /* Initialize local addresses for lookups. */
139         af->from_skb(&src, skb, 1);
140         af->from_skb(&dest, skb, 0);
141
142         /* If the packet is to or from a non-unicast address,
143          * silently discard the packet.
144          *
145          * This is not clearly defined in the RFC except in section
146          * 8.4 - OOTB handling.  However, based on the book "Stream Control
147          * Transmission Protocol" 2.1, "It is important to note that the
148          * IP address of an SCTP transport address must be a routable
149          * unicast address.  In other words, IP multicast addresses and
150          * IP broadcast addresses cannot be used in an SCTP transport
151          * address."
152          */
153         if (!af->addr_valid(&src, NULL) || !af->addr_valid(&dest, NULL))
154                 goto discard_it;
155
156         asoc = __sctp_rcv_lookup(skb, &src, &dest, &transport);
157
158         /*
159          * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
160          * An SCTP packet is called an "out of the blue" (OOTB)
161          * packet if it is correctly formed, i.e., passed the
162          * receiver's checksum check, but the receiver is not
163          * able to identify the association to which this
164          * packet belongs.
165          */
166         if (!asoc) {
167                 ep = __sctp_rcv_lookup_endpoint(&dest);
168                 if (sctp_rcv_ootb(skb)) {
169                         SCTP_INC_STATS_BH(SctpOutOfBlues);
170                         goto discard_release;
171                 }
172         }
173
174         /* Retrieve the common input handling substructure. */
175         rcvr = asoc ? &asoc->base : &ep->base;
176         sk = rcvr->sk;
177
178         /* SCTP seems to always need a timestamp right now (FIXME) */
179         if (skb->stamp.tv_sec == 0) {
180                 do_gettimeofday(&skb->stamp);
181                 sock_enable_timestamp(sk); 
182         }
183
184         if (!xfrm_policy_check(sk, XFRM_POLICY_IN, skb, family))
185                 goto discard_release;
186
187         ret = sk_filter(sk, skb, 1);
188         if (ret)
189                 goto discard_release;
190
191         /* Create an SCTP packet structure. */
192         chunk = sctp_chunkify(skb, asoc, sk);
193         if (!chunk) {
194                 ret = -ENOMEM;
195                 goto discard_release;
196         }
197
198         /* Remember what endpoint is to handle this packet. */
199         chunk->rcvr = rcvr;
200
201         /* Remember the SCTP header. */
202         chunk->sctp_hdr = sh;
203
204         /* Set the source and destination addresses of the incoming chunk.  */
205         sctp_init_addrs(chunk, &src, &dest);
206
207         /* Remember where we came from.  */
208         chunk->transport = transport;
209
210         /* Acquire access to the sock lock. Note: We are safe from other
211          * bottom halves on this lock, but a user may be in the lock too,
212          * so check if it is busy.
213          */
214         sctp_bh_lock_sock(sk);
215
216         if (sock_owned_by_user(sk))
217                 sk_add_backlog(sk, (struct sk_buff *) chunk);
218         else
219                 sctp_backlog_rcv(sk, (struct sk_buff *) chunk);
220
221         /* Release the sock and any reference counts we took in the
222          * lookup calls.
223          */
224         sctp_bh_unlock_sock(sk);
225         if (asoc)
226                 sctp_association_put(asoc);
227         else
228                 sctp_endpoint_put(ep);
229         sock_put(sk);
230         return ret;
231
232 discard_it:
233         kfree_skb(skb);
234         return ret;
235
236 discard_release:
237         /* Release any structures we may be holding. */
238         if (asoc) {
239                 sock_put(asoc->base.sk);
240                 sctp_association_put(asoc);
241         } else {
242                 sock_put(ep->base.sk);
243                 sctp_endpoint_put(ep);
244         }
245
246         goto discard_it;
247 }
248
249 /* Handle second half of inbound skb processing.  If the sock was busy,
250  * we may have need to delay processing until later when the sock is
251  * released (on the backlog).   If not busy, we call this routine
252  * directly from the bottom half.
253  */
254 int sctp_backlog_rcv(struct sock *sk, struct sk_buff *skb)
255 {
256         struct sctp_chunk *chunk;
257         struct sctp_inq *inqueue;
258
259         /* One day chunk will live inside the skb, but for
260          * now this works.
261          */
262         chunk = (struct sctp_chunk *) skb;
263         inqueue = &chunk->rcvr->inqueue;
264
265         sctp_inq_push(inqueue, chunk);
266         return 0;
267 }
268
269 /* Handle icmp frag needed error. */
270 void sctp_icmp_frag_needed(struct sock *sk, struct sctp_association *asoc,
271                            struct sctp_transport *t, __u32 pmtu)
272 {
273         if (unlikely(pmtu < SCTP_DEFAULT_MINSEGMENT)) {
274                 printk(KERN_WARNING "%s: Reported pmtu %d too low, "
275                        "using default minimum of %d\n", __FUNCTION__, pmtu,
276                        SCTP_DEFAULT_MINSEGMENT);
277                 pmtu = SCTP_DEFAULT_MINSEGMENT;
278         }
279
280         if (!sock_owned_by_user(sk) && t && (t->pmtu != pmtu)) {
281                 t->pmtu = pmtu;
282                 sctp_assoc_sync_pmtu(asoc);
283                 sctp_retransmit(&asoc->outqueue, t, SCTP_RTXR_PMTUD);
284         }
285 }
286
287 /* Common lookup code for icmp/icmpv6 error handler. */
288 struct sock *sctp_err_lookup(int family, struct sk_buff *skb,
289                              struct sctphdr *sctphdr,
290                              struct sctp_endpoint **epp,
291                              struct sctp_association **app,
292                              struct sctp_transport **tpp)
293 {
294         union sctp_addr saddr;
295         union sctp_addr daddr;
296         struct sctp_af *af;
297         struct sock *sk = NULL;
298         struct sctp_endpoint *ep = NULL;
299         struct sctp_association *asoc = NULL;
300         struct sctp_transport *transport = NULL;
301
302         *app = NULL; *epp = NULL; *tpp = NULL;
303
304         af = sctp_get_af_specific(family);
305         if (unlikely(!af)) {
306                 return NULL;
307         }
308
309         /* Initialize local addresses for lookups. */
310         af->from_skb(&saddr, skb, 1);
311         af->from_skb(&daddr, skb, 0);
312
313         /* Look for an association that matches the incoming ICMP error
314          * packet.
315          */
316         asoc = __sctp_lookup_association(&saddr, &daddr, &transport);
317         if (!asoc) {
318                 /* If there is no matching association, see if it matches any
319                  * endpoint. This may happen for an ICMP error generated in
320                  * response to an INIT_ACK.
321                  */
322                 ep = __sctp_rcv_lookup_endpoint(&daddr);
323                 if (!ep) {
324                         return NULL;
325                 }
326         }
327
328         if (asoc) {
329                 if (ntohl(sctphdr->vtag) != asoc->c.peer_vtag) {
330                         ICMP_INC_STATS_BH(IcmpInErrors);
331                         goto out;
332                 }
333                 sk = asoc->base.sk;
334         } else
335                 sk = ep->base.sk;
336
337         sctp_bh_lock_sock(sk);
338
339         /* If too many ICMPs get dropped on busy
340          * servers this needs to be solved differently.
341          */
342         if (sock_owned_by_user(sk))
343                 NET_INC_STATS_BH(LockDroppedIcmps);
344
345         *epp = ep;
346         *app = asoc;
347         *tpp = transport;
348         return sk;
349
350 out:
351         sock_put(sk);
352         if (asoc)
353                 sctp_association_put(asoc);
354         if (ep)
355                 sctp_endpoint_put(ep);
356         return NULL;
357 }
358
359 /* Common cleanup code for icmp/icmpv6 error handler. */
360 void sctp_err_finish(struct sock *sk, struct sctp_endpoint *ep,
361                      struct sctp_association *asoc)
362 {
363         sctp_bh_unlock_sock(sk);
364         sock_put(sk);
365         if (asoc)
366                 sctp_association_put(asoc);
367         if (ep)
368                 sctp_endpoint_put(ep);
369 }
370
371 /*
372  * This routine is called by the ICMP module when it gets some
373  * sort of error condition.  If err < 0 then the socket should
374  * be closed and the error returned to the user.  If err > 0
375  * it's just the icmp type << 8 | icmp code.  After adjustment
376  * header points to the first 8 bytes of the sctp header.  We need
377  * to find the appropriate port.
378  *
379  * The locking strategy used here is very "optimistic". When
380  * someone else accesses the socket the ICMP is just dropped
381  * and for some paths there is no check at all.
382  * A more general error queue to queue errors for later handling
383  * is probably better.
384  *
385  */
386 void sctp_v4_err(struct sk_buff *skb, __u32 info)
387 {
388         struct iphdr *iph = (struct iphdr *)skb->data;
389         struct sctphdr *sh = (struct sctphdr *)(skb->data + (iph->ihl <<2));
390         int type = skb->h.icmph->type;
391         int code = skb->h.icmph->code;
392         struct sock *sk;
393         struct sctp_endpoint *ep;
394         struct sctp_association *asoc;
395         struct sctp_transport *transport;
396         struct inet_opt *inet;
397         char *saveip, *savesctp;
398         int err;
399
400         if (skb->len < ((iph->ihl << 2) + 8)) {
401                 ICMP_INC_STATS_BH(IcmpInErrors);
402                 return;
403         }
404
405         /* Fix up skb to look at the embedded net header. */
406         saveip = skb->nh.raw;
407         savesctp  = skb->h.raw;
408         skb->nh.iph = iph;
409         skb->h.raw = (char *)sh;
410         sk = sctp_err_lookup(AF_INET, skb, sh, &ep, &asoc, &transport);
411         /* Put back, the original pointers. */
412         skb->nh.raw = saveip;
413         skb->h.raw = savesctp;
414         if (!sk) {
415                 ICMP_INC_STATS_BH(IcmpInErrors);
416                 return;
417         }
418         /* Warning:  The sock lock is held.  Remember to call
419          * sctp_err_finish!
420          */
421
422         switch (type) {
423         case ICMP_PARAMETERPROB:
424                 err = EPROTO;
425                 break;
426         case ICMP_DEST_UNREACH:
427                 if (code > NR_ICMP_UNREACH)
428                         goto out_unlock;
429
430                 /* PMTU discovery (RFC1191) */
431                 if (ICMP_FRAG_NEEDED == code) {
432                         sctp_icmp_frag_needed(sk, asoc, transport, info);
433                         goto out_unlock;
434                 }
435
436                 err = icmp_err_convert[code].errno;
437                 break;
438         case ICMP_TIME_EXCEEDED:
439                 /* Ignore any time exceeded errors due to fragment reassembly
440                  * timeouts.
441                  */
442                 if (ICMP_EXC_FRAGTIME == code)
443                         goto out_unlock;
444
445                 err = EHOSTUNREACH;
446                 break;
447         default:
448                 goto out_unlock;
449         }
450
451         inet = inet_sk(sk);
452         if (!sock_owned_by_user(sk) && inet->recverr) {
453                 sk->sk_err = err;
454                 sk->sk_error_report(sk);
455         } else {  /* Only an error on timeout */
456                 sk->sk_err_soft = err;
457         }
458
459 out_unlock:
460         sctp_err_finish(sk, ep, asoc);
461 }
462
463 /*
464  * RFC 2960, 8.4 - Handle "Out of the blue" Packets.
465  *
466  * This function scans all the chunks in the OOTB packet to determine if
467  * the packet should be discarded right away.  If a response might be needed
468  * for this packet, or, if further processing is possible, the packet will
469  * be queued to a proper inqueue for the next phase of handling.
470  *
471  * Output:
472  * Return 0 - If further processing is needed.
473  * Return 1 - If the packet can be discarded right away.
474  */
475 int sctp_rcv_ootb(struct sk_buff *skb)
476 {
477         sctp_chunkhdr_t *ch;
478         __u8 *ch_end;
479         sctp_errhdr_t *err;
480
481         ch = (sctp_chunkhdr_t *) skb->data;
482
483         /* Scan through all the chunks in the packet.  */
484         do {
485                 ch_end = ((__u8 *) ch) + WORD_ROUND(ntohs(ch->length));
486
487                 /* RFC 8.4, 2) If the OOTB packet contains an ABORT chunk, the
488                  * receiver MUST silently discard the OOTB packet and take no
489                  * further action.
490                  */
491                 if (SCTP_CID_ABORT == ch->type)
492                         goto discard;
493
494                 /* RFC 8.4, 6) If the packet contains a SHUTDOWN COMPLETE
495                  * chunk, the receiver should silently discard the packet
496                  * and take no further action.
497                  */
498                 if (SCTP_CID_SHUTDOWN_COMPLETE == ch->type)
499                         goto discard;
500
501                 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
502                  * or a COOKIE ACK the SCTP Packet should be silently
503                  * discarded.
504                  */
505                 if (SCTP_CID_COOKIE_ACK == ch->type)
506                         goto discard;
507
508                 if (SCTP_CID_ERROR == ch->type) {
509                         sctp_walk_errors(err, ch) {
510                                 if (SCTP_ERROR_STALE_COOKIE == err->cause)
511                                         goto discard;
512                         }
513                 }
514
515                 ch = (sctp_chunkhdr_t *) ch_end;
516         } while (ch_end < skb->tail);
517
518         return 0;
519
520 discard:
521         return 1;
522 }
523
524 /* Insert endpoint into the hash table.  */
525 void __sctp_hash_endpoint(struct sctp_endpoint *ep)
526 {
527         struct sctp_ep_common **epp;
528         struct sctp_ep_common *epb;
529         struct sctp_hashbucket *head;
530
531         epb = &ep->base;
532
533         epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);
534         head = &sctp_ep_hashtable[epb->hashent];
535
536         sctp_write_lock(&head->lock);
537         epp = &head->chain;
538         epb->next = *epp;
539         if (epb->next)
540                 (*epp)->pprev = &epb->next;
541         *epp = epb;
542         epb->pprev = epp;
543         sctp_write_unlock(&head->lock);
544 }
545
546 /* Add an endpoint to the hash. Local BH-safe. */
547 void sctp_hash_endpoint(struct sctp_endpoint *ep)
548 {
549         sctp_local_bh_disable();
550         __sctp_hash_endpoint(ep);
551         sctp_local_bh_enable();
552 }
553
554 /* Remove endpoint from the hash table.  */
555 void __sctp_unhash_endpoint(struct sctp_endpoint *ep)
556 {
557         struct sctp_hashbucket *head;
558         struct sctp_ep_common *epb;
559
560         epb = &ep->base;
561
562         epb->hashent = sctp_ep_hashfn(epb->bind_addr.port);
563
564         head = &sctp_ep_hashtable[epb->hashent];
565
566         sctp_write_lock(&head->lock);
567
568         if (epb->pprev) {
569                 if (epb->next)
570                         epb->next->pprev = epb->pprev;
571                 *epb->pprev = epb->next;
572                 epb->pprev = NULL;
573         }
574
575         sctp_write_unlock(&head->lock);
576 }
577
578 /* Remove endpoint from the hash.  Local BH-safe. */
579 void sctp_unhash_endpoint(struct sctp_endpoint *ep)
580 {
581         sctp_local_bh_disable();
582         __sctp_unhash_endpoint(ep);
583         sctp_local_bh_enable();
584 }
585
586 /* Look up an endpoint. */
587 struct sctp_endpoint *__sctp_rcv_lookup_endpoint(const union sctp_addr *laddr)
588 {
589         struct sctp_hashbucket *head;
590         struct sctp_ep_common *epb;
591         struct sctp_endpoint *ep;
592         int hash;
593
594         hash = sctp_ep_hashfn(laddr->v4.sin_port);
595         head = &sctp_ep_hashtable[hash];
596         read_lock(&head->lock);
597         for (epb = head->chain; epb; epb = epb->next) {
598                 ep = sctp_ep(epb);
599                 if (sctp_endpoint_is_match(ep, laddr))
600                         goto hit;
601         }
602
603         ep = sctp_sk((sctp_get_ctl_sock()))->ep;
604         epb = &ep->base;
605
606 hit:
607         sctp_endpoint_hold(ep);
608         sock_hold(epb->sk);
609         read_unlock(&head->lock);
610         return ep;
611 }
612
613 /* Add an association to the hash. Local BH-safe. */
614 void sctp_hash_established(struct sctp_association *asoc)
615 {
616         sctp_local_bh_disable();
617         __sctp_hash_established(asoc);
618         sctp_local_bh_enable();
619 }
620
621 /* Insert association into the hash table.  */
622 void __sctp_hash_established(struct sctp_association *asoc)
623 {
624         struct sctp_ep_common **epp;
625         struct sctp_ep_common *epb;
626         struct sctp_hashbucket *head;
627
628         epb = &asoc->base;
629
630         /* Calculate which chain this entry will belong to. */
631         epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port, asoc->peer.port);
632
633         head = &sctp_assoc_hashtable[epb->hashent];
634
635         sctp_write_lock(&head->lock);
636         epp = &head->chain;
637         epb->next = *epp;
638         if (epb->next)
639                 (*epp)->pprev = &epb->next;
640         *epp = epb;
641         epb->pprev = epp;
642         sctp_write_unlock(&head->lock);
643 }
644
645 /* Remove association from the hash table.  Local BH-safe. */
646 void sctp_unhash_established(struct sctp_association *asoc)
647 {
648         sctp_local_bh_disable();
649         __sctp_unhash_established(asoc);
650         sctp_local_bh_enable();
651 }
652
653 /* Remove association from the hash table.  */
654 void __sctp_unhash_established(struct sctp_association *asoc)
655 {
656         struct sctp_hashbucket *head;
657         struct sctp_ep_common *epb;
658
659         epb = &asoc->base;
660
661         epb->hashent = sctp_assoc_hashfn(epb->bind_addr.port,
662                                          asoc->peer.port);
663
664         head = &sctp_assoc_hashtable[epb->hashent];
665
666         sctp_write_lock(&head->lock);
667
668         if (epb->pprev) {
669                 if (epb->next)
670                         epb->next->pprev = epb->pprev;
671                 *epb->pprev = epb->next;
672                 epb->pprev = NULL;
673         }
674
675         sctp_write_unlock(&head->lock);
676 }
677
678 /* Look up an association. */
679 struct sctp_association *__sctp_lookup_association(
680                                         const union sctp_addr *local,
681                                         const union sctp_addr *peer,
682                                         struct sctp_transport **pt)
683 {
684         struct sctp_hashbucket *head;
685         struct sctp_ep_common *epb;
686         struct sctp_association *asoc;
687         struct sctp_transport *transport;
688         int hash;
689
690         /* Optimize here for direct hit, only listening connections can
691          * have wildcards anyways.
692          */
693         hash = sctp_assoc_hashfn(local->v4.sin_port, peer->v4.sin_port);
694         head = &sctp_assoc_hashtable[hash];
695         read_lock(&head->lock);
696         for (epb = head->chain; epb; epb = epb->next) {
697                 asoc = sctp_assoc(epb);
698                 transport = sctp_assoc_is_match(asoc, local, peer);
699                 if (transport)
700                         goto hit;
701         }
702
703         read_unlock(&head->lock);
704
705         return NULL;
706
707 hit:
708         *pt = transport;
709         sctp_association_hold(asoc);
710         sock_hold(epb->sk);
711         read_unlock(&head->lock);
712         return asoc;
713 }
714
715 /* Look up an association. BH-safe. */
716 struct sctp_association *sctp_lookup_association(const union sctp_addr *laddr,
717                                             const union sctp_addr *paddr,
718                                             struct sctp_transport **transportp)
719 {
720         struct sctp_association *asoc;
721
722         sctp_local_bh_disable();
723         asoc = __sctp_lookup_association(laddr, paddr, transportp);
724         sctp_local_bh_enable();
725
726         return asoc;
727 }
728
729 /* Is there an association matching the given local and peer addresses? */
730 int sctp_has_association(const union sctp_addr *laddr,
731                          const union sctp_addr *paddr)
732 {
733         struct sctp_association *asoc;
734         struct sctp_transport *transport;
735
736         if ((asoc = sctp_lookup_association(laddr, paddr, &transport))) {
737                 sock_put(asoc->base.sk);
738                 sctp_association_put(asoc);
739                 return 1;
740         }
741
742         return 0;
743 }
744
745 /*
746  * SCTP Implementors Guide, 2.18 Handling of address
747  * parameters within the INIT or INIT-ACK.
748  *
749  * D) When searching for a matching TCB upon reception of an INIT
750  *    or INIT-ACK chunk the receiver SHOULD use not only the
751  *    source address of the packet (containing the INIT or
752  *    INIT-ACK) but the receiver SHOULD also use all valid
753  *    address parameters contained within the chunk.
754  *
755  * 2.18.3 Solution description
756  *
757  * This new text clearly specifies to an implementor the need
758  * to look within the INIT or INIT-ACK. Any implementation that
759  * does not do this, may not be able to establish associations
760  * in certain circumstances.
761  *
762  */
763 static struct sctp_association *__sctp_rcv_init_lookup(struct sk_buff *skb,
764         const union sctp_addr *laddr, struct sctp_transport **transportp)
765 {
766         struct sctp_association *asoc;
767         union sctp_addr addr;
768         union sctp_addr *paddr = &addr;
769         struct sctphdr *sh = (struct sctphdr *) skb->h.raw;
770         sctp_chunkhdr_t *ch;
771         union sctp_params params;
772         sctp_init_chunk_t *init;
773         struct sctp_transport *transport;
774         struct sctp_af *af;
775
776         ch = (sctp_chunkhdr_t *) skb->data;
777
778         /* If this is INIT/INIT-ACK look inside the chunk too. */
779         switch (ch->type) {
780         case SCTP_CID_INIT:
781         case SCTP_CID_INIT_ACK:
782                 break;
783         default:
784                 return NULL;
785         }
786
787         /*
788          * This code will NOT touch anything inside the chunk--it is
789          * strictly READ-ONLY.
790          *
791          * RFC 2960 3  SCTP packet Format
792          *
793          * Multiple chunks can be bundled into one SCTP packet up to
794          * the MTU size, except for the INIT, INIT ACK, and SHUTDOWN
795          * COMPLETE chunks.  These chunks MUST NOT be bundled with any
796          * other chunk in a packet.  See Section 6.10 for more details
797          * on chunk bundling.
798          */
799
800         /* Find the start of the TLVs and the end of the chunk.  This is
801          * the region we search for address parameters.
802          */
803         init = (sctp_init_chunk_t *)skb->data;
804
805         /* Walk the parameters looking for embedded addresses. */
806         sctp_walk_params(params, init, init_hdr.params) {
807
808                 /* Note: Ignoring hostname addresses. */
809                 af = sctp_get_af_specific(param_type2af(params.p->type));
810                 if (!af)
811                         continue;
812
813                 af->from_addr_param(paddr, params.addr, ntohs(sh->source), 0);
814
815                 asoc = __sctp_lookup_association(laddr, paddr, &transport);
816                 if (asoc)
817                         return asoc;
818         }
819
820         return NULL;
821 }
822
823 /* Lookup an association for an inbound skb. */
824 struct sctp_association *__sctp_rcv_lookup(struct sk_buff *skb,
825                                       const union sctp_addr *paddr,
826                                       const union sctp_addr *laddr,
827                                       struct sctp_transport **transportp)
828 {
829         struct sctp_association *asoc;
830
831         asoc = __sctp_lookup_association(laddr, paddr, transportp);
832
833         /* Further lookup for INIT/INIT-ACK packets.
834          * SCTP Implementors Guide, 2.18 Handling of address
835          * parameters within the INIT or INIT-ACK.
836          */
837         if (!asoc)
838                 asoc = __sctp_rcv_init_lookup(skb, laddr, transportp);
839
840         return asoc;
841 }