OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [net/] [sctp/] [input.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.