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