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