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