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