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