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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [net/] [sctp/] [associola.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
 * (C) Copyright IBM Corp. 2001, 2003
3
 * Copyright (c) 1999-2000 Cisco, Inc.
4
 * Copyright (c) 1999-2001 Motorola, Inc.
5
 * Copyright (c) 2001 Intel Corp.
6
 * Copyright (c) 2001 La Monte H.P. Yarroll
7
 *
8
 * This file is part of the SCTP kernel reference Implementation
9
 *
10
 * This module provides the abstraction for an SCTP association.
11
 *
12
 * The SCTP reference implementation is free software;
13
 * you can redistribute it and/or modify it under the terms of
14
 * the GNU General Public License as published by
15
 * the Free Software Foundation; either version 2, or (at your option)
16
 * any later version.
17
 *
18
 * The SCTP reference implementation is distributed in the hope that it
19
 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
20
 *                 ************************
21
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22
 * See the GNU General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU General Public License
25
 * along with GNU CC; see the file COPYING.  If not, write to
26
 * the Free Software Foundation, 59 Temple Place - Suite 330,
27
 * Boston, MA 02111-1307, USA.
28
 *
29
 * Please send any bug reports or fixes you make to the
30
 * email address(es):
31
 *    lksctp developers <lksctp-developers@lists.sourceforge.net>
32
 *
33
 * Or submit a bug report through the following website:
34
 *    http://www.sf.net/projects/lksctp
35
 *
36
 * Written or modified by:
37
 *    La Monte H.P. Yarroll <piggy@acm.org>
38
 *    Karl Knutson          <karl@athena.chicago.il.us>
39
 *    Jon Grimm             <jgrimm@us.ibm.com>
40
 *    Xingang Guo           <xingang.guo@intel.com>
41
 *    Hui Huang             <hui.huang@nokia.com>
42
 *    Sridhar Samudrala     <sri@us.ibm.com>
43
 *    Daisy Chang           <daisyc@us.ibm.com>
44
 *    Ryan Layer            <rmlayer@us.ibm.com>
45
 *    Kevin Gao             <kevin.gao@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/fcntl.h>
53
#include <linux/poll.h>
54
#include <linux/init.h>
55
#include <linux/sched.h>
56
 
57
#include <linux/slab.h>
58
#include <linux/in.h>
59
#include <net/ipv6.h>
60
#include <net/sctp/sctp.h>
61
#include <net/sctp/sm.h>
62
 
63
/* Forward declarations for internal functions. */
64
static void sctp_assoc_bh_rcv(struct sctp_association *asoc);
65
 
66
 
67
/* 1st Level Abstractions. */
68
 
69
/* Allocate and initialize a new association */
70
struct sctp_association *sctp_association_new(const struct sctp_endpoint *ep,
71
                                         const struct sock *sk,
72
                                         sctp_scope_t scope, int gfp)
73
{
74
        struct sctp_association *asoc;
75
 
76
        asoc = t_new(struct sctp_association, gfp);
77
        if (!asoc)
78
                goto fail;
79
 
80
        if (!sctp_association_init(asoc, ep, sk, scope, gfp))
81
                goto fail_init;
82
 
83
        asoc->base.malloced = 1;
84
        SCTP_DBG_OBJCNT_INC(assoc);
85
 
86
        return asoc;
87
 
88
fail_init:
89
        kfree(asoc);
90
fail:
91
        return NULL;
92
}
93
 
94
/* Initialize a new association from provided memory. */
95
struct sctp_association *sctp_association_init(struct sctp_association *asoc,
96
                                          const struct sctp_endpoint *ep,
97
                                          const struct sock *sk,
98
                                          sctp_scope_t scope,
99
                                          int gfp)
100
{
101
        struct sctp_opt *sp;
102
        int i;
103
 
104
        /* Retrieve the SCTP per socket area.  */
105
        sp = sctp_sk((struct sock *)sk);
106
 
107
        /* Init all variables to a known value.  */
108
        memset(asoc, 0, sizeof(struct sctp_association));
109
 
110
        /* Discarding const is appropriate here.  */
111
        asoc->ep = (struct sctp_endpoint *)ep;
112
        sctp_endpoint_hold(asoc->ep);
113
 
114
        /* Hold the sock.  */
115
        asoc->base.sk = (struct sock *)sk;
116
        sock_hold(asoc->base.sk);
117
 
118
        /* Initialize the common base substructure.  */
119
        asoc->base.type = SCTP_EP_TYPE_ASSOCIATION;
120
 
121
        /* Initialize the object handling fields.  */
122
        atomic_set(&asoc->base.refcnt, 1);
123
        asoc->base.dead = 0;
124
        asoc->base.malloced = 0;
125
 
126
        /* Initialize the bind addr area.  */
127
        sctp_bind_addr_init(&asoc->base.bind_addr, ep->base.bind_addr.port);
128
        asoc->base.addr_lock = RW_LOCK_UNLOCKED;
129
 
130
        asoc->state = SCTP_STATE_CLOSED;
131
 
132
        /* Set these values from the socket values, a conversion between
133
         * millsecons to seconds/microseconds must also be done.
134
         */
135
        asoc->cookie_life.tv_sec = sp->assocparams.sasoc_cookie_life / 1000;
136
        asoc->cookie_life.tv_usec = (sp->assocparams.sasoc_cookie_life % 1000)
137
                                        * 1000;
138
        asoc->pmtu = 0;
139
        asoc->frag_point = 0;
140
 
141
        /* Set the association max_retrans and RTO values from the
142
         * socket values.
143
         */
144
        asoc->max_retrans = sp->assocparams.sasoc_asocmaxrxt;
145
        asoc->rto_initial = SCTP_MSECS_TO_JIFFIES(sp->rtoinfo.srto_initial);
146
        asoc->rto_max = SCTP_MSECS_TO_JIFFIES(sp->rtoinfo.srto_max);
147
        asoc->rto_min = SCTP_MSECS_TO_JIFFIES(sp->rtoinfo.srto_min);
148
 
149
        asoc->overall_error_count = 0;
150
 
151
        /* Initialize the maximum mumber of new data packets that can be sent
152
         * in a burst.
153
         */
154
        asoc->max_burst = sctp_max_burst;
155
 
156
        /* Copy things from the endpoint.  */
157
        for (i = SCTP_EVENT_TIMEOUT_NONE; i < SCTP_NUM_TIMEOUT_TYPES; ++i) {
158
                asoc->timeouts[i] = ep->timeouts[i];
159
                init_timer(&asoc->timers[i]);
160
                asoc->timers[i].function = sctp_timer_events[i];
161
                asoc->timers[i].data = (unsigned long) asoc;
162
        }
163
 
164
        /* Pull default initialization values from the sock options.
165
         * Note: This assumes that the values have already been
166
         * validated in the sock.
167
         */
168
        asoc->c.sinit_max_instreams = sp->initmsg.sinit_max_instreams;
169
        asoc->c.sinit_num_ostreams  = sp->initmsg.sinit_num_ostreams;
170
        asoc->max_init_attempts = sp->initmsg.sinit_max_attempts;
171
 
172
        asoc->max_init_timeo =
173
                 SCTP_MSECS_TO_JIFFIES(sp->initmsg.sinit_max_init_timeo);
174
 
175
        /* Allocate storage for the ssnmap after the inbound and outbound
176
         * streams have been negotiated during Init.
177
         */
178
        asoc->ssnmap = NULL;
179
 
180
        /* Set the local window size for receive.
181
         * This is also the rcvbuf space per association.
182
         * RFC 6 - A SCTP receiver MUST be able to receive a minimum of
183
         * 1500 bytes in one SCTP packet.
184
         */
185
        if (sk->sk_rcvbuf < SCTP_DEFAULT_MINWINDOW)
186
                asoc->rwnd = SCTP_DEFAULT_MINWINDOW;
187
        else
188
                asoc->rwnd = sk->sk_rcvbuf;
189
 
190
        asoc->a_rwnd = asoc->rwnd;
191
 
192
        asoc->rwnd_over = 0;
193
 
194
        /* Use my own max window until I learn something better.  */
195
        asoc->peer.rwnd = SCTP_DEFAULT_MAXWINDOW;
196
 
197
        /* Set the sndbuf size for transmit.  */
198
        asoc->sndbuf_used = 0;
199
 
200
        init_waitqueue_head(&asoc->wait);
201
 
202
        asoc->c.my_vtag = sctp_generate_tag(ep);
203
        asoc->peer.i.init_tag = 0;     /* INIT needs a vtag of 0. */
204
        asoc->c.peer_vtag = 0;
205
        asoc->c.my_ttag   = 0;
206
        asoc->c.peer_ttag = 0;
207
 
208
        asoc->c.initial_tsn = sctp_generate_tsn(ep);
209
 
210
        asoc->next_tsn = asoc->c.initial_tsn;
211
 
212
        asoc->ctsn_ack_point = asoc->next_tsn - 1;
213
        asoc->highest_sacked = asoc->ctsn_ack_point;
214
        asoc->last_cwr_tsn = asoc->ctsn_ack_point;
215
        asoc->unack_data = 0;
216
 
217
        SCTP_DEBUG_PRINTK("myctsnap for %s INIT as 0x%x.\n",
218
                          asoc->ep->debug_name,
219
                          asoc->ctsn_ack_point);
220
 
221
        /* ADDIP Section 4.1 Asconf Chunk Procedures
222
         *
223
         * When an endpoint has an ASCONF signaled change to be sent to the
224
         * remote endpoint it should do the following:
225
         * ...
226
         * A2) a serial number should be assigned to the chunk. The serial
227
         * number SHOULD be a monotonically increasing number. The serial
228
         * numbers SHOULD be initialized at the start of the
229
         * association to the same value as the initial TSN.
230
         */
231
        asoc->addip_serial = asoc->c.initial_tsn;
232
 
233
        skb_queue_head_init(&asoc->addip_chunks);
234
 
235
        /* Make an empty list of remote transport addresses.  */
236
        INIT_LIST_HEAD(&asoc->peer.transport_addr_list);
237
 
238
        /* RFC 2960 5.1 Normal Establishment of an Association
239
         *
240
         * After the reception of the first data chunk in an
241
         * association the endpoint must immediately respond with a
242
         * sack to acknowledge the data chunk.  Subsequent
243
         * acknowledgements should be done as described in Section
244
         * 6.2.
245
         *
246
         * [We implement this by telling a new association that it
247
         * already received one packet.]
248
         */
249
        asoc->peer.sack_needed = 1;
250
 
251
        /* Assume that the peer recongizes ASCONF until reported otherwise
252
         * via an ERROR chunk.
253
         */
254
        asoc->peer.asconf_capable = 1;
255
 
256
        /* Create an input queue.  */
257
        sctp_inq_init(&asoc->base.inqueue);
258
        sctp_inq_set_th_handler(&asoc->base.inqueue,
259
                                    (void (*)(void *))sctp_assoc_bh_rcv,
260
                                    asoc);
261
 
262
        /* Create an output queue.  */
263
        sctp_outq_init(asoc, &asoc->outqueue);
264
        sctp_outq_set_output_handlers(&asoc->outqueue,
265
                                      sctp_packet_init,
266
                                      sctp_packet_config,
267
                                      sctp_packet_append_chunk,
268
                                      sctp_packet_transmit_chunk,
269
                                      sctp_packet_transmit);
270
 
271
        if (!sctp_ulpq_init(&asoc->ulpq, asoc))
272
                goto fail_init;
273
 
274
        /* Set up the tsn tracking. */
275
        sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_SIZE, 0);
276
 
277
        asoc->need_ecne = 0;
278
 
279
        asoc->eyecatcher = SCTP_ASSOC_EYECATCHER;
280
 
281
        /* Assume that peer would support both address types unless we are
282
         * told otherwise.
283
         */
284
        asoc->peer.ipv4_address = 1;
285
        asoc->peer.ipv6_address = 1;
286
        INIT_LIST_HEAD(&asoc->asocs);
287
 
288
        asoc->autoclose = sp->autoclose;
289
 
290
        asoc->default_stream = sp->default_stream;
291
        asoc->default_ppid = sp->default_ppid;
292
        asoc->default_flags = sp->default_flags;
293
        asoc->default_context = sp->default_context;
294
        asoc->default_timetolive = sp->default_timetolive;
295
 
296
        return asoc;
297
 
298
fail_init:
299
        sctp_endpoint_put(asoc->ep);
300
        sock_put(asoc->base.sk);
301
        return NULL;
302
}
303
 
304
/* Free this association if possible.  There may still be users, so
305
 * the actual deallocation may be delayed.
306
 */
307
void sctp_association_free(struct sctp_association *asoc)
308
{
309
        struct sock *sk = asoc->base.sk;
310
        struct sctp_transport *transport;
311
        struct list_head *pos, *temp;
312
        int i;
313
 
314
        list_del(&asoc->asocs);
315
 
316
        /* Decrement the backlog value for a TCP-style listening socket. */
317
        if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
318
                sk->sk_ack_backlog--;
319
 
320
        /* Mark as dead, so other users can know this structure is
321
         * going away.
322
         */
323
        asoc->base.dead = 1;
324
 
325
        /* Dispose of any data lying around in the outqueue. */
326
        sctp_outq_free(&asoc->outqueue);
327
 
328
        /* Dispose of any pending messages for the upper layer. */
329
        sctp_ulpq_free(&asoc->ulpq);
330
 
331
        /* Dispose of any pending chunks on the inqueue. */
332
        sctp_inq_free(&asoc->base.inqueue);
333
 
334
        /* Free ssnmap storage. */
335
        sctp_ssnmap_free(asoc->ssnmap);
336
 
337
        /* Clean up the bound address list. */
338
        sctp_bind_addr_free(&asoc->base.bind_addr);
339
 
340
        /* Do we need to go through all of our timers and
341
         * delete them?   To be safe we will try to delete all, but we
342
         * should be able to go through and make a guess based
343
         * on our state.
344
         */
345
        for (i = SCTP_EVENT_TIMEOUT_NONE; i < SCTP_NUM_TIMEOUT_TYPES; ++i) {
346
                if (timer_pending(&asoc->timers[i]) &&
347
                    del_timer(&asoc->timers[i]))
348
                        sctp_association_put(asoc);
349
        }
350
 
351
        /* Free peer's cached cookie. */
352
        if (asoc->peer.cookie) {
353
                kfree(asoc->peer.cookie);
354
        }
355
 
356
        /* Release the transport structures. */
357
        list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
358
                transport = list_entry(pos, struct sctp_transport, transports);
359
                list_del(pos);
360
                sctp_transport_free(transport);
361
        }
362
 
363
        asoc->eyecatcher = 0;
364
 
365
        /* Free any cached ASCONF_ACK chunk. */
366
        if (asoc->addip_last_asconf_ack)
367
                sctp_chunk_free(asoc->addip_last_asconf_ack);
368
 
369
        /* Free any cached ASCONF chunk. */
370
        if (asoc->addip_last_asconf)
371
                sctp_chunk_free(asoc->addip_last_asconf);
372
 
373
        sctp_association_put(asoc);
374
}
375
 
376
/* Cleanup and free up an association. */
377
static void sctp_association_destroy(struct sctp_association *asoc)
378
{
379
        SCTP_ASSERT(asoc->base.dead, "Assoc is not dead", return);
380
 
381
        sctp_endpoint_put(asoc->ep);
382
        sock_put(asoc->base.sk);
383
 
384
        if (asoc->base.malloced) {
385
                kfree(asoc);
386
                SCTP_DBG_OBJCNT_DEC(assoc);
387
        }
388
}
389
 
390
/* Change the primary destination address for the peer. */
391
void sctp_assoc_set_primary(struct sctp_association *asoc,
392
                            struct sctp_transport *transport)
393
{
394
        asoc->peer.primary_path = transport;
395
 
396
        /* Set a default msg_name for events. */
397
        memcpy(&asoc->peer.primary_addr, &transport->ipaddr,
398
               sizeof(union sctp_addr));
399
 
400
        /* If the primary path is changing, assume that the
401
         * user wants to use this new path.
402
         */
403
        if (transport->active)
404
                asoc->peer.active_path = transport;
405
 
406
        /*
407
         * SFR-CACC algorithm:
408
         * Upon the receipt of a request to change the primary
409
         * destination address, on the data structure for the new
410
         * primary destination, the sender MUST do the following:
411
         *
412
         * 1) If CHANGEOVER_ACTIVE is set, then there was a switch
413
         * to this destination address earlier. The sender MUST set
414
         * CYCLING_CHANGEOVER to indicate that this switch is a
415
         * double switch to the same destination address.
416
         */
417
        if (transport->cacc.changeover_active)
418
                transport->cacc.cycling_changeover = 1;
419
 
420
        /* 2) The sender MUST set CHANGEOVER_ACTIVE to indicate that
421
         * a changeover has occurred.
422
         */
423
        transport->cacc.changeover_active = 1;
424
 
425
        /* 3) The sender MUST store the next TSN to be sent in
426
         * next_tsn_at_change.
427
         */
428
        transport->cacc.next_tsn_at_change = asoc->next_tsn;
429
}
430
 
431
/* Add a transport address to an association.  */
432
struct sctp_transport *sctp_assoc_add_peer(struct sctp_association *asoc,
433
                                           const union sctp_addr *addr,
434
                                           int gfp)
435
{
436
        struct sctp_transport *peer;
437
        struct sctp_opt *sp;
438
        unsigned short port;
439
 
440
        sp = sctp_sk(asoc->base.sk);
441
 
442
        /* AF_INET and AF_INET6 share common port field. */
443
        port = addr->v4.sin_port;
444
 
445
        /* Set the port if it has not been set yet.  */
446
        if (0 == asoc->peer.port)
447
                asoc->peer.port = port;
448
 
449
        /* Check to see if this is a duplicate. */
450
        peer = sctp_assoc_lookup_paddr(asoc, addr);
451
        if (peer)
452
                return peer;
453
 
454
        peer = sctp_transport_new(addr, gfp);
455
        if (!peer)
456
                return NULL;
457
 
458
        sctp_transport_set_owner(peer, asoc);
459
 
460
        /* Initialize the pmtu of the transport. */
461
        sctp_transport_pmtu(peer);
462
 
463
        /* If this is the first transport addr on this association,
464
         * initialize the association PMTU to the peer's PMTU.
465
         * If not and the current association PMTU is higher than the new
466
         * peer's PMTU, reset the association PMTU to the new peer's PMTU.
467
         */
468
        if (asoc->pmtu)
469
                asoc->pmtu = min_t(int, peer->pmtu, asoc->pmtu);
470
        else
471
                asoc->pmtu = peer->pmtu;
472
 
473
        SCTP_DEBUG_PRINTK("sctp_assoc_add_peer:association %p PMTU set to "
474
                          "%d\n", asoc, asoc->pmtu);
475
 
476
        asoc->frag_point = sctp_frag_point(sp, asoc->pmtu);
477
 
478
        /* The asoc->peer.port might not be meaningful yet, but
479
         * initialize the packet structure anyway.
480
         */
481
        (asoc->outqueue.init_output)(&peer->packet,
482
                                     peer,
483
                                     asoc->base.bind_addr.port,
484
                                     asoc->peer.port);
485
 
486
        /* 7.2.1 Slow-Start
487
         *
488
         * o The initial cwnd before data transmission or after a
489
         *   sufficiently long idle period MUST be <= 2*MTU.
490
         *
491
         * o The initial value of ssthresh MAY be arbitrarily high
492
         *   (for example, implementations MAY use the size of the
493
         *   receiver advertised window).
494
         */
495
        peer->cwnd = asoc->pmtu * 2;
496
 
497
        /* At this point, we may not have the receiver's advertised window,
498
         * so initialize ssthresh to the default value and it will be set
499
         * later when we process the INIT.
500
         */
501
        peer->ssthresh = SCTP_DEFAULT_MAXWINDOW;
502
 
503
        peer->partial_bytes_acked = 0;
504
        peer->flight_size = 0;
505
        peer->error_threshold = peer->max_retrans;
506
 
507
        /* By default, enable heartbeat for peer address. */
508
        peer->hb_allowed = 1;
509
 
510
        /* Initialize the peer's heartbeat interval based on the
511
         * sock configured value.
512
         */
513
        peer->hb_interval = SCTP_MSECS_TO_JIFFIES(sp->paddrparam.spp_hbinterval);
514
 
515
        /* Set the path max_retrans.  */
516
        peer->max_retrans = asoc->max_retrans;
517
 
518
        /* Set the transport's RTO.initial value */
519
        peer->rto = asoc->rto_initial;
520
 
521
        /* Attach the remote transport to our asoc.  */
522
        list_add_tail(&peer->transports, &asoc->peer.transport_addr_list);
523
 
524
        /* If we do not yet have a primary path, set one.  */
525
        if (!asoc->peer.primary_path) {
526
                sctp_assoc_set_primary(asoc, peer);
527
                asoc->peer.retran_path = peer;
528
        }
529
 
530
        if (asoc->peer.active_path == asoc->peer.retran_path)
531
                asoc->peer.retran_path = peer;
532
 
533
        return peer;
534
}
535
 
536
/* Delete a transport address from an association.  */
537
void sctp_assoc_del_peer(struct sctp_association *asoc,
538
                         const union sctp_addr *addr)
539
{
540
        struct list_head        *pos;
541
        struct list_head        *temp;
542
        struct sctp_transport   *peer = NULL;
543
        struct sctp_transport   *transport;
544
 
545
        list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
546
                transport = list_entry(pos, struct sctp_transport, transports);
547
                if (sctp_cmp_addr_exact(addr, &transport->ipaddr)) {
548
                        peer = transport;
549
                        list_del(pos);
550
                        break;
551
                }
552
        }
553
 
554
        /* The address we want delete is not in the association. */
555
        if (!peer)
556
                return;
557
 
558
        /* Get the first transport of asoc. */
559
        pos = asoc->peer.transport_addr_list.next;
560
        transport = list_entry(pos, struct sctp_transport, transports);
561
 
562
        /* Update any entries that match the peer to be deleted. */
563
        if (asoc->peer.primary_path == peer)
564
                sctp_assoc_set_primary(asoc, transport);
565
        if (asoc->peer.active_path == peer)
566
                asoc->peer.active_path = transport;
567
        if (asoc->peer.retran_path == peer)
568
                asoc->peer.retran_path = transport;
569
        if (asoc->peer.last_data_from == peer)
570
                asoc->peer.last_data_from = transport;
571
 
572
        sctp_transport_free(peer);
573
}
574
 
575
/* Lookup a transport by address. */
576
struct sctp_transport *sctp_assoc_lookup_paddr(
577
                                        const struct sctp_association *asoc,
578
                                        const union sctp_addr *address)
579
{
580
        struct sctp_transport *t;
581
        struct list_head *pos;
582
 
583
        /* Cycle through all transports searching for a peer address. */
584
 
585
        list_for_each(pos, &asoc->peer.transport_addr_list) {
586
                t = list_entry(pos, struct sctp_transport, transports);
587
                if (sctp_cmp_addr_exact(address, &t->ipaddr))
588
                        return t;
589
        }
590
 
591
        return NULL;
592
}
593
 
594
/* Engage in transport control operations.
595
 * Mark the transport up or down and send a notification to the user.
596
 * Select and update the new active and retran paths.
597
 */
598
void sctp_assoc_control_transport(struct sctp_association *asoc,
599
                                  struct sctp_transport *transport,
600
                                  sctp_transport_cmd_t command,
601
                                  sctp_sn_error_t error)
602
{
603
        struct sctp_transport *t = NULL;
604
        struct sctp_transport *first;
605
        struct sctp_transport *second;
606
        struct sctp_ulpevent *event;
607
        struct list_head *pos;
608
        int spc_state = 0;
609
 
610
        /* Record the transition on the transport.  */
611
        switch (command) {
612
        case SCTP_TRANSPORT_UP:
613
                transport->active = SCTP_ACTIVE;
614
                spc_state = SCTP_ADDR_REACHABLE;
615
                break;
616
 
617
        case SCTP_TRANSPORT_DOWN:
618
                transport->active = SCTP_INACTIVE;
619
                spc_state = SCTP_ADDR_UNREACHABLE;
620
                break;
621
 
622
        default:
623
                return;
624
        };
625
 
626
        /* Generate and send a SCTP_PEER_ADDR_CHANGE notification to the
627
         * user.
628
         */
629
        event = sctp_ulpevent_make_peer_addr_change(asoc,
630
                                (struct sockaddr_storage *) &transport->ipaddr,
631
                                0, spc_state, error, GFP_ATOMIC);
632
        if (event)
633
                sctp_ulpq_tail_event(&asoc->ulpq, event);
634
 
635
        /* Select new active and retran paths. */
636
 
637
        /* Look for the two most recently used active transports.
638
         *
639
         * This code produces the wrong ordering whenever jiffies
640
         * rolls over, but we still get usable transports, so we don't
641
         * worry about it.
642
         */
643
        first = NULL; second = NULL;
644
 
645
        list_for_each(pos, &asoc->peer.transport_addr_list) {
646
                t = list_entry(pos, struct sctp_transport, transports);
647
 
648
                if (!t->active)
649
                        continue;
650
                if (!first || t->last_time_heard > first->last_time_heard) {
651
                        second = first;
652
                        first = t;
653
                }
654
                if (!second || t->last_time_heard > second->last_time_heard)
655
                        second = t;
656
        }
657
 
658
        /* RFC 2960 6.4 Multi-Homed SCTP Endpoints
659
         *
660
         * By default, an endpoint should always transmit to the
661
         * primary path, unless the SCTP user explicitly specifies the
662
         * destination transport address (and possibly source
663
         * transport address) to use.
664
         *
665
         * [If the primary is active but not most recent, bump the most
666
         * recently used transport.]
667
         */
668
        if (asoc->peer.primary_path->active &&
669
            first != asoc->peer.primary_path) {
670
                second = first;
671
                first = asoc->peer.primary_path;
672
        }
673
 
674
        /* If we failed to find a usable transport, just camp on the
675
         * primary, even if it is inactive.
676
         */
677
        if (!first) {
678
                first = asoc->peer.primary_path;
679
                second = asoc->peer.primary_path;
680
        }
681
 
682
        /* Set the active and retran transports.  */
683
        asoc->peer.active_path = first;
684
        asoc->peer.retran_path = second;
685
}
686
 
687
/* Hold a reference to an association. */
688
void sctp_association_hold(struct sctp_association *asoc)
689
{
690
        atomic_inc(&asoc->base.refcnt);
691
}
692
 
693
/* Release a reference to an association and cleanup
694
 * if there are no more references.
695
 */
696
void sctp_association_put(struct sctp_association *asoc)
697
{
698
        if (atomic_dec_and_test(&asoc->base.refcnt))
699
                sctp_association_destroy(asoc);
700
}
701
 
702
/* Allocate the next TSN, Transmission Sequence Number, for the given
703
 * association.
704
 */
705
__u32 sctp_association_get_next_tsn(struct sctp_association *asoc)
706
{
707
        /* From Section 1.6 Serial Number Arithmetic:
708
         * Transmission Sequence Numbers wrap around when they reach
709
         * 2**32 - 1.  That is, the next TSN a DATA chunk MUST use
710
         * after transmitting TSN = 2*32 - 1 is TSN = 0.
711
         */
712
        __u32 retval = asoc->next_tsn;
713
        asoc->next_tsn++;
714
        asoc->unack_data++;
715
 
716
        return retval;
717
}
718
 
719
/* Allocate 'num' TSNs by incrementing the association's TSN by num. */
720
__u32 sctp_association_get_tsn_block(struct sctp_association *asoc, int num)
721
{
722
        __u32 retval = asoc->next_tsn;
723
 
724
        asoc->next_tsn += num;
725
        asoc->unack_data += num;
726
 
727
        return retval;
728
}
729
 
730
 
731
/* Compare two addresses to see if they match.  Wildcard addresses
732
 * only match themselves.
733
 */
734
int sctp_cmp_addr_exact(const union sctp_addr *ss1,
735
                        const union sctp_addr *ss2)
736
{
737
        struct sctp_af *af;
738
 
739
        af = sctp_get_af_specific(ss1->sa.sa_family);
740
        if (unlikely(!af))
741
                return 0;
742
 
743
        return af->cmp_addr(ss1, ss2);
744
}
745
 
746
/* Return an ecne chunk to get prepended to a packet.
747
 * Note:  We are sly and return a shared, prealloced chunk.  FIXME:
748
 * No we don't, but we could/should.
749
 */
750
struct sctp_chunk *sctp_get_ecne_prepend(struct sctp_association *asoc)
751
{
752
        struct sctp_chunk *chunk;
753
 
754
        /* Send ECNE if needed.
755
         * Not being able to allocate a chunk here is not deadly.
756
         */
757
        if (asoc->need_ecne)
758
                chunk = sctp_make_ecne(asoc, asoc->last_ecne_tsn);
759
        else
760
                chunk = NULL;
761
 
762
        return chunk;
763
}
764
 
765
/* Use this function for the packet prepend callback when no ECNE
766
 * packet is desired (e.g. some packets don't like to be bundled).
767
 */
768
struct sctp_chunk *sctp_get_no_prepend(struct sctp_association *asoc)
769
{
770
        return NULL;
771
}
772
 
773
/*
774
 * Find which transport this TSN was sent on.
775
 */
776
struct sctp_transport *sctp_assoc_lookup_tsn(struct sctp_association *asoc,
777
                                             __u32 tsn)
778
{
779
        struct sctp_transport *active;
780
        struct sctp_transport *match;
781
        struct list_head *entry, *pos;
782
        struct sctp_transport *transport;
783
        struct sctp_chunk *chunk;
784
        __u32 key = htonl(tsn);
785
 
786
        match = NULL;
787
 
788
        /*
789
         * FIXME: In general, find a more efficient data structure for
790
         * searching.
791
         */
792
 
793
        /*
794
         * The general strategy is to search each transport's transmitted
795
         * list.   Return which transport this TSN lives on.
796
         *
797
         * Let's be hopeful and check the active_path first.
798
         * Another optimization would be to know if there is only one
799
         * outbound path and not have to look for the TSN at all.
800
         *
801
         */
802
 
803
        active = asoc->peer.active_path;
804
 
805
        list_for_each(entry, &active->transmitted) {
806
                chunk = list_entry(entry, struct sctp_chunk, transmitted_list);
807
 
808
                if (key == chunk->subh.data_hdr->tsn) {
809
                        match = active;
810
                        goto out;
811
                }
812
        }
813
 
814
        /* If not found, go search all the other transports. */
815
        list_for_each(pos, &asoc->peer.transport_addr_list) {
816
                transport = list_entry(pos, struct sctp_transport, transports);
817
 
818
                if (transport == active)
819
                        break;
820
                list_for_each(entry, &transport->transmitted) {
821
                        chunk = list_entry(entry, struct sctp_chunk,
822
                                           transmitted_list);
823
                        if (key == chunk->subh.data_hdr->tsn) {
824
                                match = transport;
825
                                goto out;
826
                        }
827
                }
828
        }
829
out:
830
        return match;
831
}
832
 
833
/* Is this the association we are looking for? */
834
struct sctp_transport *sctp_assoc_is_match(struct sctp_association *asoc,
835
                                           const union sctp_addr *laddr,
836
                                           const union sctp_addr *paddr)
837
{
838
        struct sctp_transport *transport;
839
 
840
        sctp_read_lock(&asoc->base.addr_lock);
841
 
842
        if ((asoc->base.bind_addr.port == laddr->v4.sin_port) &&
843
            (asoc->peer.port == paddr->v4.sin_port)) {
844
                transport = sctp_assoc_lookup_paddr(asoc, paddr);
845
                if (!transport)
846
                        goto out;
847
 
848
                if (sctp_bind_addr_match(&asoc->base.bind_addr, laddr,
849
                                         sctp_sk(asoc->base.sk)))
850
                        goto out;
851
        }
852
        transport = NULL;
853
 
854
out:
855
        sctp_read_unlock(&asoc->base.addr_lock);
856
        return transport;
857
}
858
 
859
/*  Is this a live association structure. */
860
int sctp_assoc_valid(struct sock *sk, struct sctp_association *asoc)
861
{
862
 
863
        /* First, verify that this is a kernel address. */
864
        if (!sctp_is_valid_kaddr((unsigned long) asoc))
865
                return 0;
866
 
867
        /* Verify that this _is_ an sctp_association
868
         * data structure and if so, that the socket matches.
869
         */
870
        if (SCTP_ASSOC_EYECATCHER != asoc->eyecatcher)
871
                return 0;
872
        if (asoc->base.sk != sk)
873
                return 0;
874
 
875
        /* The association is valid. */
876
        return 1;
877
}
878
 
879
/* Do delayed input processing.  This is scheduled by sctp_rcv(). */
880
static void sctp_assoc_bh_rcv(struct sctp_association *asoc)
881
{
882
        struct sctp_endpoint *ep;
883
        struct sctp_chunk *chunk;
884
        struct sock *sk;
885
        struct sctp_inq *inqueue;
886
        int state, subtype;
887
        int error = 0;
888
 
889
        /* The association should be held so we should be safe. */
890
        ep = asoc->ep;
891
        sk = asoc->base.sk;
892
 
893
        inqueue = &asoc->base.inqueue;
894
        while (NULL != (chunk = sctp_inq_pop(inqueue))) {
895
                state = asoc->state;
896
                subtype = chunk->chunk_hdr->type;
897
 
898
                /* Remember where the last DATA chunk came from so we
899
                 * know where to send the SACK.
900
                 */
901
                if (sctp_chunk_is_data(chunk))
902
                        asoc->peer.last_data_from = chunk->transport;
903
                else
904
                        SCTP_INC_STATS(SctpInCtrlChunks);
905
 
906
                if (chunk->transport)
907
                        chunk->transport->last_time_heard = jiffies;
908
 
909
                /* Run through the state machine. */
910
                error = sctp_do_sm(SCTP_EVENT_T_CHUNK, SCTP_ST_CHUNK(subtype),
911
                                   state, ep, asoc, chunk, GFP_ATOMIC);
912
 
913
                /* Check to see if the association is freed in response to
914
                 * the incoming chunk.  If so, get out of the while loop.
915
                 */
916
                if (!sctp_assoc_valid(sk, asoc))
917
                        break;
918
 
919
                /* If there is an error on chunk, discard this packet. */
920
                if (error && chunk)
921
                        chunk->pdiscard = 1;
922
        }
923
 
924
}
925
 
926
/* This routine moves an association from its old sk to a new sk.  */
927
void sctp_assoc_migrate(struct sctp_association *assoc, struct sock *newsk)
928
{
929
        struct sctp_opt *newsp = sctp_sk(newsk);
930
        struct sock *oldsk = assoc->base.sk;
931
 
932
        /* Delete the association from the old endpoint's list of
933
         * associations.
934
         */
935
        list_del_init(&assoc->asocs);
936
 
937
        /* Decrement the backlog value for a TCP-style socket. */
938
        if (sctp_style(oldsk, TCP))
939
                oldsk->sk_ack_backlog--;
940
 
941
        /* Release references to the old endpoint and the sock.  */
942
        sctp_endpoint_put(assoc->ep);
943
        sock_put(assoc->base.sk);
944
 
945
        /* Get a reference to the new endpoint.  */
946
        assoc->ep = newsp->ep;
947
        sctp_endpoint_hold(assoc->ep);
948
 
949
        /* Get a reference to the new sock.  */
950
        assoc->base.sk = newsk;
951
        sock_hold(assoc->base.sk);
952
 
953
        /* Add the association to the new endpoint's list of associations.  */
954
        sctp_endpoint_add_asoc(newsp->ep, assoc);
955
}
956
 
957
/* Update an association (possibly from unexpected COOKIE-ECHO processing).  */
958
void sctp_assoc_update(struct sctp_association *asoc,
959
                       struct sctp_association *new)
960
{
961
        /* Copy in new parameters of peer. */
962
        asoc->c = new->c;
963
        asoc->peer.rwnd = new->peer.rwnd;
964
        asoc->peer.sack_needed = new->peer.sack_needed;
965
        asoc->peer.i = new->peer.i;
966
        sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_SIZE,
967
                         asoc->peer.i.initial_tsn);
968
 
969
        /* FIXME:
970
         *    Do we need to copy primary_path etc?
971
         *
972
         *    More explicitly, addresses may have been removed and
973
         *    this needs accounting for.
974
         */
975
 
976
        /* If the case is A (association restart), use
977
         * initial_tsn as next_tsn. If the case is B, use
978
         * current next_tsn in case data sent to peer
979
         * has been discarded and needs retransmission.
980
         */
981
        if (sctp_state(asoc, ESTABLISHED)) {
982
 
983
                asoc->next_tsn = new->next_tsn;
984
                asoc->ctsn_ack_point = new->ctsn_ack_point;
985
 
986
                /* Reinitialize SSN for both local streams
987
                 * and peer's streams.
988
                 */
989
                sctp_ssnmap_clear(asoc->ssnmap);
990
 
991
        } else {
992
                asoc->ctsn_ack_point = asoc->next_tsn - 1;
993
                if (!asoc->ssnmap) {
994
                        /* Move the ssnmap. */
995
                        asoc->ssnmap = new->ssnmap;
996
                        new->ssnmap = NULL;
997
                }
998
        }
999
 
1000
}
1001
 
1002
/* Update the retran path for sending a retransmitted packet.
1003
 * Round-robin through the active transports, else round-robin
1004
 * through the inactive transports as this is the next best thing
1005
 * we can try.
1006
 */
1007
void sctp_assoc_update_retran_path(struct sctp_association *asoc)
1008
{
1009
        struct sctp_transport *t, *next;
1010
        struct list_head *head = &asoc->peer.transport_addr_list;
1011
        struct list_head *pos;
1012
 
1013
        /* Find the next transport in a round-robin fashion. */
1014
        t = asoc->peer.retran_path;
1015
        pos = &t->transports;
1016
        next = NULL;
1017
 
1018
        while (1) {
1019
                /* Skip the head. */
1020
                if (pos->next == head)
1021
                        pos = head->next;
1022
                else
1023
                        pos = pos->next;
1024
 
1025
                t = list_entry(pos, struct sctp_transport, transports);
1026
 
1027
                /* Try to find an active transport. */
1028
 
1029
                if (t->active) {
1030
                        break;
1031
                } else {
1032
                        /* Keep track of the next transport in case
1033
                         * we don't find any active transport.
1034
                         */
1035
                        if (!next)
1036
                                next = t;
1037
                }
1038
 
1039
                /* We have exhausted the list, but didn't find any
1040
                 * other active transports.  If so, use the next
1041
                 * transport.
1042
                 */
1043
                if (t == asoc->peer.retran_path) {
1044
                        t = next;
1045
                        break;
1046
                }
1047
        }
1048
 
1049
        asoc->peer.retran_path = t;
1050
}
1051
 
1052
/* Choose the transport for sending a SHUTDOWN packet.  */
1053
struct sctp_transport *sctp_assoc_choose_shutdown_transport(
1054
        struct sctp_association *asoc)
1055
{
1056
        /* If this is the first time SHUTDOWN is sent, use the active path,
1057
         * else use the retran path. If the last SHUTDOWN was sent over the
1058
         * retran path, update the retran path and use it.
1059
         */
1060
        if (!asoc->shutdown_last_sent_to)
1061
                return asoc->peer.active_path;
1062
        else {
1063
                if (asoc->shutdown_last_sent_to == asoc->peer.retran_path)
1064
                        sctp_assoc_update_retran_path(asoc);
1065
                return asoc->peer.retran_path;
1066
        }
1067
 
1068
}
1069
 
1070
/* Update the association's pmtu and frag_point by going through all the
1071
 * transports. This routine is called when a transport's PMTU has changed.
1072
 */
1073
void sctp_assoc_sync_pmtu(struct sctp_association *asoc)
1074
{
1075
        struct sctp_transport *t;
1076
        struct list_head *pos;
1077
        __u32 pmtu = 0;
1078
 
1079
        if (!asoc)
1080
                return;
1081
 
1082
        /* Get the lowest pmtu of all the transports. */
1083
        list_for_each(pos, &asoc->peer.transport_addr_list) {
1084
                t = list_entry(pos, struct sctp_transport, transports);
1085
                if (!pmtu || (t->pmtu < pmtu))
1086
                        pmtu = t->pmtu;
1087
        }
1088
 
1089
        if (pmtu) {
1090
                struct sctp_opt *sp = sctp_sk(asoc->base.sk);
1091
                asoc->pmtu = pmtu;
1092
                asoc->frag_point = sctp_frag_point(sp, pmtu);
1093
        }
1094
 
1095
        SCTP_DEBUG_PRINTK("%s: asoc:%p, pmtu:%d, frag_point:%d\n",
1096
                          __FUNCTION__, asoc, asoc->pmtu, asoc->frag_point);
1097
}
1098
 
1099
/* Should we send a SACK to update our peer? */
1100
static inline int sctp_peer_needs_update(struct sctp_association *asoc)
1101
{
1102
        switch (asoc->state) {
1103
        case SCTP_STATE_ESTABLISHED:
1104
        case SCTP_STATE_SHUTDOWN_PENDING:
1105
        case SCTP_STATE_SHUTDOWN_RECEIVED:
1106
                if ((asoc->rwnd > asoc->a_rwnd) &&
1107
                    ((asoc->rwnd - asoc->a_rwnd) >=
1108
                     min_t(__u32, (asoc->base.sk->sk_rcvbuf >> 1), asoc->pmtu)))
1109
                        return 1;
1110
                break;
1111
        default:
1112
                break;
1113
        }
1114
        return 0;
1115
}
1116
 
1117
/* Increase asoc's rwnd by len and send any window update SACK if needed. */
1118
void sctp_assoc_rwnd_increase(struct sctp_association *asoc, unsigned len)
1119
{
1120
        struct sctp_chunk *sack;
1121
        struct timer_list *timer;
1122
 
1123
        if (asoc->rwnd_over) {
1124
                if (asoc->rwnd_over >= len) {
1125
                        asoc->rwnd_over -= len;
1126
                } else {
1127
                        asoc->rwnd += (len - asoc->rwnd_over);
1128
                        asoc->rwnd_over = 0;
1129
                }
1130
        } else {
1131
                asoc->rwnd += len;
1132
        }
1133
 
1134
        SCTP_DEBUG_PRINTK("%s: asoc %p rwnd increased by %d to (%u, %u) "
1135
                          "- %u\n", __FUNCTION__, asoc, len, asoc->rwnd,
1136
                          asoc->rwnd_over, asoc->a_rwnd);
1137
 
1138
        /* Send a window update SACK if the rwnd has increased by at least the
1139
         * minimum of the association's PMTU and half of the receive buffer.
1140
         * The algorithm used is similar to the one described in
1141
         * Section 4.2.3.3 of RFC 1122.
1142
         */
1143
        if (sctp_peer_needs_update(asoc)) {
1144
                asoc->a_rwnd = asoc->rwnd;
1145
                SCTP_DEBUG_PRINTK("%s: Sending window update SACK- asoc: %p "
1146
                                  "rwnd: %u a_rwnd: %u\n", __FUNCTION__,
1147
                                  asoc, asoc->rwnd, asoc->a_rwnd);
1148
                sack = sctp_make_sack(asoc);
1149
                if (!sack)
1150
                        return;
1151
 
1152
                asoc->peer.sack_needed = 0;
1153
 
1154
                sctp_outq_tail(&asoc->outqueue, sack);
1155
 
1156
                /* Stop the SACK timer.  */
1157
                timer = &asoc->timers[SCTP_EVENT_TIMEOUT_SACK];
1158
                if (timer_pending(timer) && del_timer(timer))
1159
                        sctp_association_put(asoc);
1160
        }
1161
}
1162
 
1163
/* Decrease asoc's rwnd by len. */
1164
void sctp_assoc_rwnd_decrease(struct sctp_association *asoc, unsigned len)
1165
{
1166
        SCTP_ASSERT(asoc->rwnd, "rwnd zero", return);
1167
        SCTP_ASSERT(!asoc->rwnd_over, "rwnd_over not zero", return);
1168
        if (asoc->rwnd >= len) {
1169
                asoc->rwnd -= len;
1170
        } else {
1171
                asoc->rwnd_over = len - asoc->rwnd;
1172
                asoc->rwnd = 0;
1173
        }
1174
        SCTP_DEBUG_PRINTK("%s: asoc %p rwnd decreased by %d to (%u, %u)\n",
1175
                          __FUNCTION__, asoc, len, asoc->rwnd,
1176
                          asoc->rwnd_over);
1177
}
1178
 
1179
/* Build the bind address list for the association based on info from the
1180
 * local endpoint and the remote peer.
1181
 */
1182
int sctp_assoc_set_bind_addr_from_ep(struct sctp_association *asoc, int gfp)
1183
{
1184
        sctp_scope_t scope;
1185
        int flags;
1186
 
1187
        /* Use scoping rules to determine the subset of addresses from
1188
         * the endpoint.
1189
         */
1190
        scope = sctp_scope(&asoc->peer.active_path->ipaddr);
1191
        flags = (PF_INET6 == asoc->base.sk->sk_family) ? SCTP_ADDR6_ALLOWED : 0;
1192
        if (asoc->peer.ipv4_address)
1193
                flags |= SCTP_ADDR4_PEERSUPP;
1194
        if (asoc->peer.ipv6_address)
1195
                flags |= SCTP_ADDR6_PEERSUPP;
1196
 
1197
        return sctp_bind_addr_copy(&asoc->base.bind_addr,
1198
                                   &asoc->ep->base.bind_addr,
1199
                                   scope, gfp, flags);
1200
}
1201
 
1202
/* Build the association's bind address list from the cookie.  */
1203
int sctp_assoc_set_bind_addr_from_cookie(struct sctp_association *asoc,
1204
                                         struct sctp_cookie *cookie, int gfp)
1205
{
1206
        int var_size2 = ntohs(cookie->peer_init->chunk_hdr.length);
1207
        int var_size3 = cookie->raw_addr_list_len;
1208
        __u8 *raw = (__u8 *)cookie + sizeof(struct sctp_cookie) + var_size2;
1209
 
1210
        return sctp_raw_to_bind_addrs(&asoc->base.bind_addr, raw, var_size3,
1211
                                      asoc->ep->base.bind_addr.port, gfp);
1212
}
1213
 
1214
/* Lookup laddr in the bind address list of an association. */
1215
int sctp_assoc_lookup_laddr(struct sctp_association *asoc,
1216
                            const union sctp_addr *laddr)
1217
{
1218
        int found;
1219
 
1220
        sctp_read_lock(&asoc->base.addr_lock);
1221
        if ((asoc->base.bind_addr.port == ntohs(laddr->v4.sin_port)) &&
1222
            sctp_bind_addr_match(&asoc->base.bind_addr, laddr,
1223
                                 sctp_sk(asoc->base.sk))) {
1224
                found = 1;
1225
                goto out;
1226
        }
1227
 
1228
        found = 0;
1229
out:
1230
        sctp_read_unlock(&asoc->base.addr_lock);
1231
        return found;
1232
}

powered by: WebSVN 2.1.0

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