[ALSA] es1688 - Use platform_device
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / sctp / associola.c
blobdec68a60477310c700dfdec911cc2ebcd719218b
1 /* SCTP kernel reference Implementation
2 * (C) Copyright IBM Corp. 2001, 2004
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
8 * This file is part of the SCTP kernel reference Implementation
10 * This module provides the abstraction for an SCTP association.
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.
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.
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.
29 * Please send any bug reports or fixes you make to the
30 * email address(es):
31 * lksctp developers <lksctp-developers@lists.sourceforge.net>
33 * Or submit a bug report through the following website:
34 * http://www.sf.net/projects/lksctp
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>
47 * Any bugs reported given to us we will try to fix... any fixes shared will
48 * be incorporated into the next SCTP release.
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>
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>
63 /* Forward declarations for internal functions. */
64 static void sctp_assoc_bh_rcv(struct sctp_association *asoc);
67 /* 1st Level Abstractions. */
69 /* Initialize a new association from provided memory. */
70 static struct sctp_association *sctp_association_init(struct sctp_association *asoc,
71 const struct sctp_endpoint *ep,
72 const struct sock *sk,
73 sctp_scope_t scope,
74 gfp_t gfp)
76 struct sctp_sock *sp;
77 int i;
79 /* Retrieve the SCTP per socket area. */
80 sp = sctp_sk((struct sock *)sk);
82 /* Init all variables to a known value. */
83 memset(asoc, 0, sizeof(struct sctp_association));
85 /* Discarding const is appropriate here. */
86 asoc->ep = (struct sctp_endpoint *)ep;
87 sctp_endpoint_hold(asoc->ep);
89 /* Hold the sock. */
90 asoc->base.sk = (struct sock *)sk;
91 sock_hold(asoc->base.sk);
93 /* Initialize the common base substructure. */
94 asoc->base.type = SCTP_EP_TYPE_ASSOCIATION;
96 /* Initialize the object handling fields. */
97 atomic_set(&asoc->base.refcnt, 1);
98 asoc->base.dead = 0;
99 asoc->base.malloced = 0;
101 /* Initialize the bind addr area. */
102 sctp_bind_addr_init(&asoc->base.bind_addr, ep->base.bind_addr.port);
103 rwlock_init(&asoc->base.addr_lock);
105 asoc->state = SCTP_STATE_CLOSED;
107 /* Set these values from the socket values, a conversion between
108 * millsecons to seconds/microseconds must also be done.
110 asoc->cookie_life.tv_sec = sp->assocparams.sasoc_cookie_life / 1000;
111 asoc->cookie_life.tv_usec = (sp->assocparams.sasoc_cookie_life % 1000)
112 * 1000;
113 asoc->pmtu = 0;
114 asoc->frag_point = 0;
116 /* Set the association max_retrans and RTO values from the
117 * socket values.
119 asoc->max_retrans = sp->assocparams.sasoc_asocmaxrxt;
120 asoc->rto_initial = msecs_to_jiffies(sp->rtoinfo.srto_initial);
121 asoc->rto_max = msecs_to_jiffies(sp->rtoinfo.srto_max);
122 asoc->rto_min = msecs_to_jiffies(sp->rtoinfo.srto_min);
124 asoc->overall_error_count = 0;
126 /* Initialize the maximum mumber of new data packets that can be sent
127 * in a burst.
129 asoc->max_burst = sctp_max_burst;
131 /* initialize association timers */
132 asoc->timeouts[SCTP_EVENT_TIMEOUT_NONE] = 0;
133 asoc->timeouts[SCTP_EVENT_TIMEOUT_T1_COOKIE] = asoc->rto_initial;
134 asoc->timeouts[SCTP_EVENT_TIMEOUT_T1_INIT] = asoc->rto_initial;
135 asoc->timeouts[SCTP_EVENT_TIMEOUT_T2_SHUTDOWN] = asoc->rto_initial;
136 asoc->timeouts[SCTP_EVENT_TIMEOUT_T3_RTX] = 0;
137 asoc->timeouts[SCTP_EVENT_TIMEOUT_T4_RTO] = 0;
139 /* sctpimpguide Section 2.12.2
140 * If the 'T5-shutdown-guard' timer is used, it SHOULD be set to the
141 * recommended value of 5 times 'RTO.Max'.
143 asoc->timeouts[SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD]
144 = 5 * asoc->rto_max;
146 asoc->timeouts[SCTP_EVENT_TIMEOUT_HEARTBEAT] = 0;
147 asoc->timeouts[SCTP_EVENT_TIMEOUT_SACK] =
148 SCTP_DEFAULT_TIMEOUT_SACK;
149 asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE] =
150 sp->autoclose * HZ;
152 /* Initilizes the timers */
153 for (i = SCTP_EVENT_TIMEOUT_NONE; i < SCTP_NUM_TIMEOUT_TYPES; ++i) {
154 init_timer(&asoc->timers[i]);
155 asoc->timers[i].function = sctp_timer_events[i];
156 asoc->timers[i].data = (unsigned long) asoc;
159 /* Pull default initialization values from the sock options.
160 * Note: This assumes that the values have already been
161 * validated in the sock.
163 asoc->c.sinit_max_instreams = sp->initmsg.sinit_max_instreams;
164 asoc->c.sinit_num_ostreams = sp->initmsg.sinit_num_ostreams;
165 asoc->max_init_attempts = sp->initmsg.sinit_max_attempts;
167 asoc->max_init_timeo =
168 msecs_to_jiffies(sp->initmsg.sinit_max_init_timeo);
170 /* Allocate storage for the ssnmap after the inbound and outbound
171 * streams have been negotiated during Init.
173 asoc->ssnmap = NULL;
175 /* Set the local window size for receive.
176 * This is also the rcvbuf space per association.
177 * RFC 6 - A SCTP receiver MUST be able to receive a minimum of
178 * 1500 bytes in one SCTP packet.
180 if ((sk->sk_rcvbuf/2) < SCTP_DEFAULT_MINWINDOW)
181 asoc->rwnd = SCTP_DEFAULT_MINWINDOW;
182 else
183 asoc->rwnd = sk->sk_rcvbuf/2;
185 asoc->a_rwnd = asoc->rwnd;
187 asoc->rwnd_over = 0;
189 /* Use my own max window until I learn something better. */
190 asoc->peer.rwnd = SCTP_DEFAULT_MAXWINDOW;
192 /* Set the sndbuf size for transmit. */
193 asoc->sndbuf_used = 0;
195 /* Initialize the receive memory counter */
196 atomic_set(&asoc->rmem_alloc, 0);
198 init_waitqueue_head(&asoc->wait);
200 asoc->c.my_vtag = sctp_generate_tag(ep);
201 asoc->peer.i.init_tag = 0; /* INIT needs a vtag of 0. */
202 asoc->c.peer_vtag = 0;
203 asoc->c.my_ttag = 0;
204 asoc->c.peer_ttag = 0;
205 asoc->c.my_port = ep->base.bind_addr.port;
207 asoc->c.initial_tsn = sctp_generate_tsn(ep);
209 asoc->next_tsn = asoc->c.initial_tsn;
211 asoc->ctsn_ack_point = asoc->next_tsn - 1;
212 asoc->adv_peer_ack_point = asoc->ctsn_ack_point;
213 asoc->highest_sacked = asoc->ctsn_ack_point;
214 asoc->last_cwr_tsn = asoc->ctsn_ack_point;
215 asoc->unack_data = 0;
217 /* ADDIP Section 4.1 Asconf Chunk Procedures
219 * When an endpoint has an ASCONF signaled change to be sent to the
220 * remote endpoint it should do the following:
221 * ...
222 * A2) a serial number should be assigned to the chunk. The serial
223 * number SHOULD be a monotonically increasing number. The serial
224 * numbers SHOULD be initialized at the start of the
225 * association to the same value as the initial TSN.
227 asoc->addip_serial = asoc->c.initial_tsn;
229 INIT_LIST_HEAD(&asoc->addip_chunk_list);
231 /* Make an empty list of remote transport addresses. */
232 INIT_LIST_HEAD(&asoc->peer.transport_addr_list);
233 asoc->peer.transport_count = 0;
235 /* RFC 2960 5.1 Normal Establishment of an Association
237 * After the reception of the first data chunk in an
238 * association the endpoint must immediately respond with a
239 * sack to acknowledge the data chunk. Subsequent
240 * acknowledgements should be done as described in Section
241 * 6.2.
243 * [We implement this by telling a new association that it
244 * already received one packet.]
246 asoc->peer.sack_needed = 1;
248 /* Assume that the peer recongizes ASCONF until reported otherwise
249 * via an ERROR chunk.
251 asoc->peer.asconf_capable = 1;
253 /* Create an input queue. */
254 sctp_inq_init(&asoc->base.inqueue);
255 sctp_inq_set_th_handler(&asoc->base.inqueue,
256 (void (*)(void *))sctp_assoc_bh_rcv,
257 asoc);
259 /* Create an output queue. */
260 sctp_outq_init(asoc, &asoc->outqueue);
262 if (!sctp_ulpq_init(&asoc->ulpq, asoc))
263 goto fail_init;
265 /* Set up the tsn tracking. */
266 sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_SIZE, 0);
268 asoc->need_ecne = 0;
270 asoc->assoc_id = 0;
272 /* Assume that peer would support both address types unless we are
273 * told otherwise.
275 asoc->peer.ipv4_address = 1;
276 asoc->peer.ipv6_address = 1;
277 INIT_LIST_HEAD(&asoc->asocs);
279 asoc->autoclose = sp->autoclose;
281 asoc->default_stream = sp->default_stream;
282 asoc->default_ppid = sp->default_ppid;
283 asoc->default_flags = sp->default_flags;
284 asoc->default_context = sp->default_context;
285 asoc->default_timetolive = sp->default_timetolive;
287 return asoc;
289 fail_init:
290 sctp_endpoint_put(asoc->ep);
291 sock_put(asoc->base.sk);
292 return NULL;
295 /* Allocate and initialize a new association */
296 struct sctp_association *sctp_association_new(const struct sctp_endpoint *ep,
297 const struct sock *sk,
298 sctp_scope_t scope,
299 gfp_t gfp)
301 struct sctp_association *asoc;
303 asoc = t_new(struct sctp_association, gfp);
304 if (!asoc)
305 goto fail;
307 if (!sctp_association_init(asoc, ep, sk, scope, gfp))
308 goto fail_init;
310 asoc->base.malloced = 1;
311 SCTP_DBG_OBJCNT_INC(assoc);
312 SCTP_DEBUG_PRINTK("Created asoc %p\n", asoc);
314 return asoc;
316 fail_init:
317 kfree(asoc);
318 fail:
319 return NULL;
322 /* Free this association if possible. There may still be users, so
323 * the actual deallocation may be delayed.
325 void sctp_association_free(struct sctp_association *asoc)
327 struct sock *sk = asoc->base.sk;
328 struct sctp_transport *transport;
329 struct list_head *pos, *temp;
330 int i;
332 list_del(&asoc->asocs);
334 /* Decrement the backlog value for a TCP-style listening socket. */
335 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
336 sk->sk_ack_backlog--;
338 /* Mark as dead, so other users can know this structure is
339 * going away.
341 asoc->base.dead = 1;
343 /* Dispose of any data lying around in the outqueue. */
344 sctp_outq_free(&asoc->outqueue);
346 /* Dispose of any pending messages for the upper layer. */
347 sctp_ulpq_free(&asoc->ulpq);
349 /* Dispose of any pending chunks on the inqueue. */
350 sctp_inq_free(&asoc->base.inqueue);
352 /* Free ssnmap storage. */
353 sctp_ssnmap_free(asoc->ssnmap);
355 /* Clean up the bound address list. */
356 sctp_bind_addr_free(&asoc->base.bind_addr);
358 /* Do we need to go through all of our timers and
359 * delete them? To be safe we will try to delete all, but we
360 * should be able to go through and make a guess based
361 * on our state.
363 for (i = SCTP_EVENT_TIMEOUT_NONE; i < SCTP_NUM_TIMEOUT_TYPES; ++i) {
364 if (timer_pending(&asoc->timers[i]) &&
365 del_timer(&asoc->timers[i]))
366 sctp_association_put(asoc);
369 /* Free peer's cached cookie. */
370 kfree(asoc->peer.cookie);
372 /* Release the transport structures. */
373 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
374 transport = list_entry(pos, struct sctp_transport, transports);
375 list_del(pos);
376 sctp_transport_free(transport);
379 asoc->peer.transport_count = 0;
381 /* Free any cached ASCONF_ACK chunk. */
382 if (asoc->addip_last_asconf_ack)
383 sctp_chunk_free(asoc->addip_last_asconf_ack);
385 /* Free any cached ASCONF chunk. */
386 if (asoc->addip_last_asconf)
387 sctp_chunk_free(asoc->addip_last_asconf);
389 sctp_association_put(asoc);
392 /* Cleanup and free up an association. */
393 static void sctp_association_destroy(struct sctp_association *asoc)
395 SCTP_ASSERT(asoc->base.dead, "Assoc is not dead", return);
397 sctp_endpoint_put(asoc->ep);
398 sock_put(asoc->base.sk);
400 if (asoc->assoc_id != 0) {
401 spin_lock_bh(&sctp_assocs_id_lock);
402 idr_remove(&sctp_assocs_id, asoc->assoc_id);
403 spin_unlock_bh(&sctp_assocs_id_lock);
406 BUG_TRAP(!atomic_read(&asoc->rmem_alloc));
408 if (asoc->base.malloced) {
409 kfree(asoc);
410 SCTP_DBG_OBJCNT_DEC(assoc);
414 /* Change the primary destination address for the peer. */
415 void sctp_assoc_set_primary(struct sctp_association *asoc,
416 struct sctp_transport *transport)
418 asoc->peer.primary_path = transport;
420 /* Set a default msg_name for events. */
421 memcpy(&asoc->peer.primary_addr, &transport->ipaddr,
422 sizeof(union sctp_addr));
424 /* If the primary path is changing, assume that the
425 * user wants to use this new path.
427 if (transport->state != SCTP_INACTIVE)
428 asoc->peer.active_path = transport;
431 * SFR-CACC algorithm:
432 * Upon the receipt of a request to change the primary
433 * destination address, on the data structure for the new
434 * primary destination, the sender MUST do the following:
436 * 1) If CHANGEOVER_ACTIVE is set, then there was a switch
437 * to this destination address earlier. The sender MUST set
438 * CYCLING_CHANGEOVER to indicate that this switch is a
439 * double switch to the same destination address.
441 if (transport->cacc.changeover_active)
442 transport->cacc.cycling_changeover = 1;
444 /* 2) The sender MUST set CHANGEOVER_ACTIVE to indicate that
445 * a changeover has occurred.
447 transport->cacc.changeover_active = 1;
449 /* 3) The sender MUST store the next TSN to be sent in
450 * next_tsn_at_change.
452 transport->cacc.next_tsn_at_change = asoc->next_tsn;
455 /* Remove a transport from an association. */
456 void sctp_assoc_rm_peer(struct sctp_association *asoc,
457 struct sctp_transport *peer)
459 struct list_head *pos;
460 struct sctp_transport *transport;
462 SCTP_DEBUG_PRINTK_IPADDR("sctp_assoc_rm_peer:association %p addr: ",
463 " port: %d\n",
464 asoc,
465 (&peer->ipaddr),
466 peer->ipaddr.v4.sin_port);
468 /* If we are to remove the current retran_path, update it
469 * to the next peer before removing this peer from the list.
471 if (asoc->peer.retran_path == peer)
472 sctp_assoc_update_retran_path(asoc);
474 /* Remove this peer from the list. */
475 list_del(&peer->transports);
477 /* Get the first transport of asoc. */
478 pos = asoc->peer.transport_addr_list.next;
479 transport = list_entry(pos, struct sctp_transport, transports);
481 /* Update any entries that match the peer to be deleted. */
482 if (asoc->peer.primary_path == peer)
483 sctp_assoc_set_primary(asoc, transport);
484 if (asoc->peer.active_path == peer)
485 asoc->peer.active_path = transport;
486 if (asoc->peer.last_data_from == peer)
487 asoc->peer.last_data_from = transport;
489 /* If we remove the transport an INIT was last sent to, set it to
490 * NULL. Combined with the update of the retran path above, this
491 * will cause the next INIT to be sent to the next available
492 * transport, maintaining the cycle.
494 if (asoc->init_last_sent_to == peer)
495 asoc->init_last_sent_to = NULL;
497 asoc->peer.transport_count--;
499 sctp_transport_free(peer);
502 /* Add a transport address to an association. */
503 struct sctp_transport *sctp_assoc_add_peer(struct sctp_association *asoc,
504 const union sctp_addr *addr,
505 const gfp_t gfp,
506 const int peer_state)
508 struct sctp_transport *peer;
509 struct sctp_sock *sp;
510 unsigned short port;
512 sp = sctp_sk(asoc->base.sk);
514 /* AF_INET and AF_INET6 share common port field. */
515 port = addr->v4.sin_port;
517 SCTP_DEBUG_PRINTK_IPADDR("sctp_assoc_add_peer:association %p addr: ",
518 " port: %d state:%s\n",
519 asoc,
520 addr,
521 addr->v4.sin_port,
522 peer_state == SCTP_UNKNOWN?"UNKNOWN":"ACTIVE");
524 /* Set the port if it has not been set yet. */
525 if (0 == asoc->peer.port)
526 asoc->peer.port = port;
528 /* Check to see if this is a duplicate. */
529 peer = sctp_assoc_lookup_paddr(asoc, addr);
530 if (peer) {
531 if (peer_state == SCTP_ACTIVE &&
532 peer->state == SCTP_UNKNOWN)
533 peer->state = SCTP_ACTIVE;
534 return peer;
537 peer = sctp_transport_new(addr, gfp);
538 if (!peer)
539 return NULL;
541 sctp_transport_set_owner(peer, asoc);
543 /* Initialize the pmtu of the transport. */
544 sctp_transport_pmtu(peer);
546 /* If this is the first transport addr on this association,
547 * initialize the association PMTU to the peer's PMTU.
548 * If not and the current association PMTU is higher than the new
549 * peer's PMTU, reset the association PMTU to the new peer's PMTU.
551 if (asoc->pmtu)
552 asoc->pmtu = min_t(int, peer->pmtu, asoc->pmtu);
553 else
554 asoc->pmtu = peer->pmtu;
556 SCTP_DEBUG_PRINTK("sctp_assoc_add_peer:association %p PMTU set to "
557 "%d\n", asoc, asoc->pmtu);
559 asoc->frag_point = sctp_frag_point(sp, asoc->pmtu);
561 /* The asoc->peer.port might not be meaningful yet, but
562 * initialize the packet structure anyway.
564 sctp_packet_init(&peer->packet, peer, asoc->base.bind_addr.port,
565 asoc->peer.port);
567 /* 7.2.1 Slow-Start
569 * o The initial cwnd before DATA transmission or after a sufficiently
570 * long idle period MUST be set to
571 * min(4*MTU, max(2*MTU, 4380 bytes))
573 * o The initial value of ssthresh MAY be arbitrarily high
574 * (for example, implementations MAY use the size of the
575 * receiver advertised window).
577 peer->cwnd = min(4*asoc->pmtu, max_t(__u32, 2*asoc->pmtu, 4380));
579 /* At this point, we may not have the receiver's advertised window,
580 * so initialize ssthresh to the default value and it will be set
581 * later when we process the INIT.
583 peer->ssthresh = SCTP_DEFAULT_MAXWINDOW;
585 peer->partial_bytes_acked = 0;
586 peer->flight_size = 0;
588 /* By default, enable heartbeat for peer address. */
589 peer->hb_allowed = 1;
591 /* Initialize the peer's heartbeat interval based on the
592 * sock configured value.
594 peer->hb_interval = msecs_to_jiffies(sp->paddrparam.spp_hbinterval);
596 /* Set the path max_retrans. */
597 peer->max_retrans = sp->paddrparam.spp_pathmaxrxt;
599 /* Set the transport's RTO.initial value */
600 peer->rto = asoc->rto_initial;
602 /* Set the peer's active state. */
603 peer->state = peer_state;
605 /* Attach the remote transport to our asoc. */
606 list_add_tail(&peer->transports, &asoc->peer.transport_addr_list);
607 asoc->peer.transport_count++;
609 /* If we do not yet have a primary path, set one. */
610 if (!asoc->peer.primary_path) {
611 sctp_assoc_set_primary(asoc, peer);
612 asoc->peer.retran_path = peer;
615 if (asoc->peer.active_path == asoc->peer.retran_path) {
616 asoc->peer.retran_path = peer;
619 return peer;
622 /* Delete a transport address from an association. */
623 void sctp_assoc_del_peer(struct sctp_association *asoc,
624 const union sctp_addr *addr)
626 struct list_head *pos;
627 struct list_head *temp;
628 struct sctp_transport *transport;
630 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
631 transport = list_entry(pos, struct sctp_transport, transports);
632 if (sctp_cmp_addr_exact(addr, &transport->ipaddr)) {
633 /* Do book keeping for removing the peer and free it. */
634 sctp_assoc_rm_peer(asoc, transport);
635 break;
640 /* Lookup a transport by address. */
641 struct sctp_transport *sctp_assoc_lookup_paddr(
642 const struct sctp_association *asoc,
643 const union sctp_addr *address)
645 struct sctp_transport *t;
646 struct list_head *pos;
648 /* Cycle through all transports searching for a peer address. */
650 list_for_each(pos, &asoc->peer.transport_addr_list) {
651 t = list_entry(pos, struct sctp_transport, transports);
652 if (sctp_cmp_addr_exact(address, &t->ipaddr))
653 return t;
656 return NULL;
659 /* Engage in transport control operations.
660 * Mark the transport up or down and send a notification to the user.
661 * Select and update the new active and retran paths.
663 void sctp_assoc_control_transport(struct sctp_association *asoc,
664 struct sctp_transport *transport,
665 sctp_transport_cmd_t command,
666 sctp_sn_error_t error)
668 struct sctp_transport *t = NULL;
669 struct sctp_transport *first;
670 struct sctp_transport *second;
671 struct sctp_ulpevent *event;
672 struct list_head *pos;
673 int spc_state = 0;
675 /* Record the transition on the transport. */
676 switch (command) {
677 case SCTP_TRANSPORT_UP:
678 transport->state = SCTP_ACTIVE;
679 spc_state = SCTP_ADDR_AVAILABLE;
680 break;
682 case SCTP_TRANSPORT_DOWN:
683 transport->state = SCTP_INACTIVE;
684 spc_state = SCTP_ADDR_UNREACHABLE;
685 break;
687 default:
688 return;
691 /* Generate and send a SCTP_PEER_ADDR_CHANGE notification to the
692 * user.
694 event = sctp_ulpevent_make_peer_addr_change(asoc,
695 (struct sockaddr_storage *) &transport->ipaddr,
696 0, spc_state, error, GFP_ATOMIC);
697 if (event)
698 sctp_ulpq_tail_event(&asoc->ulpq, event);
700 /* Select new active and retran paths. */
702 /* Look for the two most recently used active transports.
704 * This code produces the wrong ordering whenever jiffies
705 * rolls over, but we still get usable transports, so we don't
706 * worry about it.
708 first = NULL; second = NULL;
710 list_for_each(pos, &asoc->peer.transport_addr_list) {
711 t = list_entry(pos, struct sctp_transport, transports);
713 if (t->state == SCTP_INACTIVE)
714 continue;
715 if (!first || t->last_time_heard > first->last_time_heard) {
716 second = first;
717 first = t;
719 if (!second || t->last_time_heard > second->last_time_heard)
720 second = t;
723 /* RFC 2960 6.4 Multi-Homed SCTP Endpoints
725 * By default, an endpoint should always transmit to the
726 * primary path, unless the SCTP user explicitly specifies the
727 * destination transport address (and possibly source
728 * transport address) to use.
730 * [If the primary is active but not most recent, bump the most
731 * recently used transport.]
733 if (asoc->peer.primary_path->state != SCTP_INACTIVE &&
734 first != asoc->peer.primary_path) {
735 second = first;
736 first = asoc->peer.primary_path;
739 /* If we failed to find a usable transport, just camp on the
740 * primary, even if it is inactive.
742 if (!first) {
743 first = asoc->peer.primary_path;
744 second = asoc->peer.primary_path;
747 /* Set the active and retran transports. */
748 asoc->peer.active_path = first;
749 asoc->peer.retran_path = second;
752 /* Hold a reference to an association. */
753 void sctp_association_hold(struct sctp_association *asoc)
755 atomic_inc(&asoc->base.refcnt);
758 /* Release a reference to an association and cleanup
759 * if there are no more references.
761 void sctp_association_put(struct sctp_association *asoc)
763 if (atomic_dec_and_test(&asoc->base.refcnt))
764 sctp_association_destroy(asoc);
767 /* Allocate the next TSN, Transmission Sequence Number, for the given
768 * association.
770 __u32 sctp_association_get_next_tsn(struct sctp_association *asoc)
772 /* From Section 1.6 Serial Number Arithmetic:
773 * Transmission Sequence Numbers wrap around when they reach
774 * 2**32 - 1. That is, the next TSN a DATA chunk MUST use
775 * after transmitting TSN = 2*32 - 1 is TSN = 0.
777 __u32 retval = asoc->next_tsn;
778 asoc->next_tsn++;
779 asoc->unack_data++;
781 return retval;
784 /* Compare two addresses to see if they match. Wildcard addresses
785 * only match themselves.
787 int sctp_cmp_addr_exact(const union sctp_addr *ss1,
788 const union sctp_addr *ss2)
790 struct sctp_af *af;
792 af = sctp_get_af_specific(ss1->sa.sa_family);
793 if (unlikely(!af))
794 return 0;
796 return af->cmp_addr(ss1, ss2);
799 /* Return an ecne chunk to get prepended to a packet.
800 * Note: We are sly and return a shared, prealloced chunk. FIXME:
801 * No we don't, but we could/should.
803 struct sctp_chunk *sctp_get_ecne_prepend(struct sctp_association *asoc)
805 struct sctp_chunk *chunk;
807 /* Send ECNE if needed.
808 * Not being able to allocate a chunk here is not deadly.
810 if (asoc->need_ecne)
811 chunk = sctp_make_ecne(asoc, asoc->last_ecne_tsn);
812 else
813 chunk = NULL;
815 return chunk;
819 * Find which transport this TSN was sent on.
821 struct sctp_transport *sctp_assoc_lookup_tsn(struct sctp_association *asoc,
822 __u32 tsn)
824 struct sctp_transport *active;
825 struct sctp_transport *match;
826 struct list_head *entry, *pos;
827 struct sctp_transport *transport;
828 struct sctp_chunk *chunk;
829 __u32 key = htonl(tsn);
831 match = NULL;
834 * FIXME: In general, find a more efficient data structure for
835 * searching.
839 * The general strategy is to search each transport's transmitted
840 * list. Return which transport this TSN lives on.
842 * Let's be hopeful and check the active_path first.
843 * Another optimization would be to know if there is only one
844 * outbound path and not have to look for the TSN at all.
848 active = asoc->peer.active_path;
850 list_for_each(entry, &active->transmitted) {
851 chunk = list_entry(entry, struct sctp_chunk, transmitted_list);
853 if (key == chunk->subh.data_hdr->tsn) {
854 match = active;
855 goto out;
859 /* If not found, go search all the other transports. */
860 list_for_each(pos, &asoc->peer.transport_addr_list) {
861 transport = list_entry(pos, struct sctp_transport, transports);
863 if (transport == active)
864 break;
865 list_for_each(entry, &transport->transmitted) {
866 chunk = list_entry(entry, struct sctp_chunk,
867 transmitted_list);
868 if (key == chunk->subh.data_hdr->tsn) {
869 match = transport;
870 goto out;
874 out:
875 return match;
878 /* Is this the association we are looking for? */
879 struct sctp_transport *sctp_assoc_is_match(struct sctp_association *asoc,
880 const union sctp_addr *laddr,
881 const union sctp_addr *paddr)
883 struct sctp_transport *transport;
885 sctp_read_lock(&asoc->base.addr_lock);
887 if ((asoc->base.bind_addr.port == laddr->v4.sin_port) &&
888 (asoc->peer.port == paddr->v4.sin_port)) {
889 transport = sctp_assoc_lookup_paddr(asoc, paddr);
890 if (!transport)
891 goto out;
893 if (sctp_bind_addr_match(&asoc->base.bind_addr, laddr,
894 sctp_sk(asoc->base.sk)))
895 goto out;
897 transport = NULL;
899 out:
900 sctp_read_unlock(&asoc->base.addr_lock);
901 return transport;
904 /* Do delayed input processing. This is scheduled by sctp_rcv(). */
905 static void sctp_assoc_bh_rcv(struct sctp_association *asoc)
907 struct sctp_endpoint *ep;
908 struct sctp_chunk *chunk;
909 struct sock *sk;
910 struct sctp_inq *inqueue;
911 int state;
912 sctp_subtype_t subtype;
913 int error = 0;
915 /* The association should be held so we should be safe. */
916 ep = asoc->ep;
917 sk = asoc->base.sk;
919 inqueue = &asoc->base.inqueue;
920 sctp_association_hold(asoc);
921 while (NULL != (chunk = sctp_inq_pop(inqueue))) {
922 state = asoc->state;
923 subtype = SCTP_ST_CHUNK(chunk->chunk_hdr->type);
925 /* Remember where the last DATA chunk came from so we
926 * know where to send the SACK.
928 if (sctp_chunk_is_data(chunk))
929 asoc->peer.last_data_from = chunk->transport;
930 else
931 SCTP_INC_STATS(SCTP_MIB_INCTRLCHUNKS);
933 if (chunk->transport)
934 chunk->transport->last_time_heard = jiffies;
936 /* Run through the state machine. */
937 error = sctp_do_sm(SCTP_EVENT_T_CHUNK, subtype,
938 state, ep, asoc, chunk, GFP_ATOMIC);
940 /* Check to see if the association is freed in response to
941 * the incoming chunk. If so, get out of the while loop.
943 if (asoc->base.dead)
944 break;
946 /* If there is an error on chunk, discard this packet. */
947 if (error && chunk)
948 chunk->pdiscard = 1;
950 sctp_association_put(asoc);
953 /* This routine moves an association from its old sk to a new sk. */
954 void sctp_assoc_migrate(struct sctp_association *assoc, struct sock *newsk)
956 struct sctp_sock *newsp = sctp_sk(newsk);
957 struct sock *oldsk = assoc->base.sk;
959 /* Delete the association from the old endpoint's list of
960 * associations.
962 list_del_init(&assoc->asocs);
964 /* Decrement the backlog value for a TCP-style socket. */
965 if (sctp_style(oldsk, TCP))
966 oldsk->sk_ack_backlog--;
968 /* Release references to the old endpoint and the sock. */
969 sctp_endpoint_put(assoc->ep);
970 sock_put(assoc->base.sk);
972 /* Get a reference to the new endpoint. */
973 assoc->ep = newsp->ep;
974 sctp_endpoint_hold(assoc->ep);
976 /* Get a reference to the new sock. */
977 assoc->base.sk = newsk;
978 sock_hold(assoc->base.sk);
980 /* Add the association to the new endpoint's list of associations. */
981 sctp_endpoint_add_asoc(newsp->ep, assoc);
984 /* Update an association (possibly from unexpected COOKIE-ECHO processing). */
985 void sctp_assoc_update(struct sctp_association *asoc,
986 struct sctp_association *new)
988 struct sctp_transport *trans;
989 struct list_head *pos, *temp;
991 /* Copy in new parameters of peer. */
992 asoc->c = new->c;
993 asoc->peer.rwnd = new->peer.rwnd;
994 asoc->peer.sack_needed = new->peer.sack_needed;
995 asoc->peer.i = new->peer.i;
996 sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_SIZE,
997 asoc->peer.i.initial_tsn);
999 /* Remove any peer addresses not present in the new association. */
1000 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
1001 trans = list_entry(pos, struct sctp_transport, transports);
1002 if (!sctp_assoc_lookup_paddr(new, &trans->ipaddr))
1003 sctp_assoc_del_peer(asoc, &trans->ipaddr);
1006 /* If the case is A (association restart), use
1007 * initial_tsn as next_tsn. If the case is B, use
1008 * current next_tsn in case data sent to peer
1009 * has been discarded and needs retransmission.
1011 if (asoc->state >= SCTP_STATE_ESTABLISHED) {
1012 asoc->next_tsn = new->next_tsn;
1013 asoc->ctsn_ack_point = new->ctsn_ack_point;
1014 asoc->adv_peer_ack_point = new->adv_peer_ack_point;
1016 /* Reinitialize SSN for both local streams
1017 * and peer's streams.
1019 sctp_ssnmap_clear(asoc->ssnmap);
1021 } else {
1022 /* Add any peer addresses from the new association. */
1023 list_for_each(pos, &new->peer.transport_addr_list) {
1024 trans = list_entry(pos, struct sctp_transport,
1025 transports);
1026 if (!sctp_assoc_lookup_paddr(asoc, &trans->ipaddr))
1027 sctp_assoc_add_peer(asoc, &trans->ipaddr,
1028 GFP_ATOMIC, SCTP_ACTIVE);
1031 asoc->ctsn_ack_point = asoc->next_tsn - 1;
1032 asoc->adv_peer_ack_point = asoc->ctsn_ack_point;
1033 if (!asoc->ssnmap) {
1034 /* Move the ssnmap. */
1035 asoc->ssnmap = new->ssnmap;
1036 new->ssnmap = NULL;
1041 /* Update the retran path for sending a retransmitted packet.
1042 * Round-robin through the active transports, else round-robin
1043 * through the inactive transports as this is the next best thing
1044 * we can try.
1046 void sctp_assoc_update_retran_path(struct sctp_association *asoc)
1048 struct sctp_transport *t, *next;
1049 struct list_head *head = &asoc->peer.transport_addr_list;
1050 struct list_head *pos;
1052 /* Find the next transport in a round-robin fashion. */
1053 t = asoc->peer.retran_path;
1054 pos = &t->transports;
1055 next = NULL;
1057 while (1) {
1058 /* Skip the head. */
1059 if (pos->next == head)
1060 pos = head->next;
1061 else
1062 pos = pos->next;
1064 t = list_entry(pos, struct sctp_transport, transports);
1066 /* Try to find an active transport. */
1068 if (t->state != SCTP_INACTIVE) {
1069 break;
1070 } else {
1071 /* Keep track of the next transport in case
1072 * we don't find any active transport.
1074 if (!next)
1075 next = t;
1078 /* We have exhausted the list, but didn't find any
1079 * other active transports. If so, use the next
1080 * transport.
1082 if (t == asoc->peer.retran_path) {
1083 t = next;
1084 break;
1088 asoc->peer.retran_path = t;
1090 SCTP_DEBUG_PRINTK_IPADDR("sctp_assoc_update_retran_path:association"
1091 " %p addr: ",
1092 " port: %d\n",
1093 asoc,
1094 (&t->ipaddr),
1095 t->ipaddr.v4.sin_port);
1098 /* Choose the transport for sending a INIT packet. */
1099 struct sctp_transport *sctp_assoc_choose_init_transport(
1100 struct sctp_association *asoc)
1102 struct sctp_transport *t;
1104 /* Use the retran path. If the last INIT was sent over the
1105 * retran path, update the retran path and use it.
1107 if (!asoc->init_last_sent_to) {
1108 t = asoc->peer.active_path;
1109 } else {
1110 if (asoc->init_last_sent_to == asoc->peer.retran_path)
1111 sctp_assoc_update_retran_path(asoc);
1112 t = asoc->peer.retran_path;
1115 SCTP_DEBUG_PRINTK_IPADDR("sctp_assoc_update_retran_path:association"
1116 " %p addr: ",
1117 " port: %d\n",
1118 asoc,
1119 (&t->ipaddr),
1120 t->ipaddr.v4.sin_port);
1122 return t;
1125 /* Choose the transport for sending a SHUTDOWN packet. */
1126 struct sctp_transport *sctp_assoc_choose_shutdown_transport(
1127 struct sctp_association *asoc)
1129 /* If this is the first time SHUTDOWN is sent, use the active path,
1130 * else use the retran path. If the last SHUTDOWN was sent over the
1131 * retran path, update the retran path and use it.
1133 if (!asoc->shutdown_last_sent_to)
1134 return asoc->peer.active_path;
1135 else {
1136 if (asoc->shutdown_last_sent_to == asoc->peer.retran_path)
1137 sctp_assoc_update_retran_path(asoc);
1138 return asoc->peer.retran_path;
1143 /* Update the association's pmtu and frag_point by going through all the
1144 * transports. This routine is called when a transport's PMTU has changed.
1146 void sctp_assoc_sync_pmtu(struct sctp_association *asoc)
1148 struct sctp_transport *t;
1149 struct list_head *pos;
1150 __u32 pmtu = 0;
1152 if (!asoc)
1153 return;
1155 /* Get the lowest pmtu of all the transports. */
1156 list_for_each(pos, &asoc->peer.transport_addr_list) {
1157 t = list_entry(pos, struct sctp_transport, transports);
1158 if (!pmtu || (t->pmtu < pmtu))
1159 pmtu = t->pmtu;
1162 if (pmtu) {
1163 struct sctp_sock *sp = sctp_sk(asoc->base.sk);
1164 asoc->pmtu = pmtu;
1165 asoc->frag_point = sctp_frag_point(sp, pmtu);
1168 SCTP_DEBUG_PRINTK("%s: asoc:%p, pmtu:%d, frag_point:%d\n",
1169 __FUNCTION__, asoc, asoc->pmtu, asoc->frag_point);
1172 /* Should we send a SACK to update our peer? */
1173 static inline int sctp_peer_needs_update(struct sctp_association *asoc)
1175 switch (asoc->state) {
1176 case SCTP_STATE_ESTABLISHED:
1177 case SCTP_STATE_SHUTDOWN_PENDING:
1178 case SCTP_STATE_SHUTDOWN_RECEIVED:
1179 case SCTP_STATE_SHUTDOWN_SENT:
1180 if ((asoc->rwnd > asoc->a_rwnd) &&
1181 ((asoc->rwnd - asoc->a_rwnd) >=
1182 min_t(__u32, (asoc->base.sk->sk_rcvbuf >> 1), asoc->pmtu)))
1183 return 1;
1184 break;
1185 default:
1186 break;
1188 return 0;
1191 /* Increase asoc's rwnd by len and send any window update SACK if needed. */
1192 void sctp_assoc_rwnd_increase(struct sctp_association *asoc, unsigned len)
1194 struct sctp_chunk *sack;
1195 struct timer_list *timer;
1197 if (asoc->rwnd_over) {
1198 if (asoc->rwnd_over >= len) {
1199 asoc->rwnd_over -= len;
1200 } else {
1201 asoc->rwnd += (len - asoc->rwnd_over);
1202 asoc->rwnd_over = 0;
1204 } else {
1205 asoc->rwnd += len;
1208 SCTP_DEBUG_PRINTK("%s: asoc %p rwnd increased by %d to (%u, %u) "
1209 "- %u\n", __FUNCTION__, asoc, len, asoc->rwnd,
1210 asoc->rwnd_over, asoc->a_rwnd);
1212 /* Send a window update SACK if the rwnd has increased by at least the
1213 * minimum of the association's PMTU and half of the receive buffer.
1214 * The algorithm used is similar to the one described in
1215 * Section 4.2.3.3 of RFC 1122.
1217 if (sctp_peer_needs_update(asoc)) {
1218 asoc->a_rwnd = asoc->rwnd;
1219 SCTP_DEBUG_PRINTK("%s: Sending window update SACK- asoc: %p "
1220 "rwnd: %u a_rwnd: %u\n", __FUNCTION__,
1221 asoc, asoc->rwnd, asoc->a_rwnd);
1222 sack = sctp_make_sack(asoc);
1223 if (!sack)
1224 return;
1226 asoc->peer.sack_needed = 0;
1228 sctp_outq_tail(&asoc->outqueue, sack);
1230 /* Stop the SACK timer. */
1231 timer = &asoc->timers[SCTP_EVENT_TIMEOUT_SACK];
1232 if (timer_pending(timer) && del_timer(timer))
1233 sctp_association_put(asoc);
1237 /* Decrease asoc's rwnd by len. */
1238 void sctp_assoc_rwnd_decrease(struct sctp_association *asoc, unsigned len)
1240 SCTP_ASSERT(asoc->rwnd, "rwnd zero", return);
1241 SCTP_ASSERT(!asoc->rwnd_over, "rwnd_over not zero", return);
1242 if (asoc->rwnd >= len) {
1243 asoc->rwnd -= len;
1244 } else {
1245 asoc->rwnd_over = len - asoc->rwnd;
1246 asoc->rwnd = 0;
1248 SCTP_DEBUG_PRINTK("%s: asoc %p rwnd decreased by %d to (%u, %u)\n",
1249 __FUNCTION__, asoc, len, asoc->rwnd,
1250 asoc->rwnd_over);
1253 /* Build the bind address list for the association based on info from the
1254 * local endpoint and the remote peer.
1256 int sctp_assoc_set_bind_addr_from_ep(struct sctp_association *asoc,
1257 gfp_t gfp)
1259 sctp_scope_t scope;
1260 int flags;
1262 /* Use scoping rules to determine the subset of addresses from
1263 * the endpoint.
1265 scope = sctp_scope(&asoc->peer.active_path->ipaddr);
1266 flags = (PF_INET6 == asoc->base.sk->sk_family) ? SCTP_ADDR6_ALLOWED : 0;
1267 if (asoc->peer.ipv4_address)
1268 flags |= SCTP_ADDR4_PEERSUPP;
1269 if (asoc->peer.ipv6_address)
1270 flags |= SCTP_ADDR6_PEERSUPP;
1272 return sctp_bind_addr_copy(&asoc->base.bind_addr,
1273 &asoc->ep->base.bind_addr,
1274 scope, gfp, flags);
1277 /* Build the association's bind address list from the cookie. */
1278 int sctp_assoc_set_bind_addr_from_cookie(struct sctp_association *asoc,
1279 struct sctp_cookie *cookie,
1280 gfp_t gfp)
1282 int var_size2 = ntohs(cookie->peer_init->chunk_hdr.length);
1283 int var_size3 = cookie->raw_addr_list_len;
1284 __u8 *raw = (__u8 *)cookie->peer_init + var_size2;
1286 return sctp_raw_to_bind_addrs(&asoc->base.bind_addr, raw, var_size3,
1287 asoc->ep->base.bind_addr.port, gfp);
1290 /* Lookup laddr in the bind address list of an association. */
1291 int sctp_assoc_lookup_laddr(struct sctp_association *asoc,
1292 const union sctp_addr *laddr)
1294 int found;
1296 sctp_read_lock(&asoc->base.addr_lock);
1297 if ((asoc->base.bind_addr.port == ntohs(laddr->v4.sin_port)) &&
1298 sctp_bind_addr_match(&asoc->base.bind_addr, laddr,
1299 sctp_sk(asoc->base.sk))) {
1300 found = 1;
1301 goto out;
1304 found = 0;
1305 out:
1306 sctp_read_unlock(&asoc->base.addr_lock);
1307 return found;