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