Commit | Line | Data |
---|---|---|
47505b8b | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
60c778b2 | 2 | /* SCTP kernel implementation |
1da177e4 LT |
3 | * (C) Copyright IBM Corp. 2001, 2004 |
4 | * Copyright (c) 1999-2000 Cisco, Inc. | |
5 | * Copyright (c) 1999-2001 Motorola, Inc. | |
6 | * Copyright (c) 2001 Intel Corp. | |
7 | * Copyright (c) 2001 La Monte H.P. Yarroll | |
8 | * | |
60c778b2 | 9 | * This file is part of the SCTP kernel implementation |
1da177e4 LT |
10 | * |
11 | * This module provides the abstraction for an SCTP association. | |
12 | * | |
1da177e4 LT |
13 | * Please send any bug reports or fixes you make to the |
14 | * email address(es): | |
91705c61 | 15 | * lksctp developers <linux-sctp@vger.kernel.org> |
1da177e4 | 16 | * |
1da177e4 LT |
17 | * Written or modified by: |
18 | * La Monte H.P. Yarroll <piggy@acm.org> | |
19 | * Karl Knutson <karl@athena.chicago.il.us> | |
20 | * Jon Grimm <jgrimm@us.ibm.com> | |
21 | * Xingang Guo <xingang.guo@intel.com> | |
22 | * Hui Huang <hui.huang@nokia.com> | |
23 | * Sridhar Samudrala <sri@us.ibm.com> | |
24 | * Daisy Chang <daisyc@us.ibm.com> | |
25 | * Ryan Layer <rmlayer@us.ibm.com> | |
26 | * Kevin Gao <kevin.gao@intel.com> | |
1da177e4 LT |
27 | */ |
28 | ||
145ce502 JP |
29 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
30 | ||
1da177e4 LT |
31 | #include <linux/types.h> |
32 | #include <linux/fcntl.h> | |
33 | #include <linux/poll.h> | |
34 | #include <linux/init.h> | |
1da177e4 LT |
35 | |
36 | #include <linux/slab.h> | |
37 | #include <linux/in.h> | |
38 | #include <net/ipv6.h> | |
39 | #include <net/sctp/sctp.h> | |
40 | #include <net/sctp/sm.h> | |
41 | ||
42 | /* Forward declarations for internal functions. */ | |
b82e8f31 | 43 | static void sctp_select_active_and_retran_path(struct sctp_association *asoc); |
c4028958 | 44 | static void sctp_assoc_bh_rcv(struct work_struct *work); |
a08de64d | 45 | static void sctp_assoc_free_asconf_acks(struct sctp_association *asoc); |
8b4472cc | 46 | static void sctp_assoc_free_asconf_queue(struct sctp_association *asoc); |
1da177e4 | 47 | |
1da177e4 LT |
48 | /* 1st Level Abstractions. */ |
49 | ||
50 | /* Initialize a new association from provided memory. */ | |
1c662018 XL |
51 | static struct sctp_association *sctp_association_init( |
52 | struct sctp_association *asoc, | |
53 | const struct sctp_endpoint *ep, | |
54 | const struct sock *sk, | |
55 | enum sctp_scope scope, gfp_t gfp) | |
1da177e4 LT |
56 | { |
57 | struct sctp_sock *sp; | |
3c918704 | 58 | struct sctp_paramhdr *p; |
58194778 | 59 | int i; |
1da177e4 LT |
60 | |
61 | /* Retrieve the SCTP per socket area. */ | |
62 | sp = sctp_sk((struct sock *)sk); | |
63 | ||
1da177e4 LT |
64 | /* Discarding const is appropriate here. */ |
65 | asoc->ep = (struct sctp_endpoint *)ep; | |
1da177e4 | 66 | asoc->base.sk = (struct sock *)sk; |
31243461 | 67 | asoc->base.net = sock_net(sk); |
2e0c9e79 DB |
68 | |
69 | sctp_endpoint_hold(asoc->ep); | |
1da177e4 LT |
70 | sock_hold(asoc->base.sk); |
71 | ||
72 | /* Initialize the common base substructure. */ | |
73 | asoc->base.type = SCTP_EP_TYPE_ASSOCIATION; | |
74 | ||
75 | /* Initialize the object handling fields. */ | |
c638457a | 76 | refcount_set(&asoc->base.refcnt, 1); |
1da177e4 LT |
77 | |
78 | /* Initialize the bind addr area. */ | |
79 | sctp_bind_addr_init(&asoc->base.bind_addr, ep->base.bind_addr.port); | |
1da177e4 LT |
80 | |
81 | asoc->state = SCTP_STATE_CLOSED; | |
52db882f | 82 | asoc->cookie_life = ms_to_ktime(sp->assocparams.sasoc_cookie_life); |
f68b2e05 | 83 | asoc->user_frag = sp->user_frag; |
1da177e4 LT |
84 | |
85 | /* Set the association max_retrans and RTO values from the | |
86 | * socket values. | |
87 | */ | |
88 | asoc->max_retrans = sp->assocparams.sasoc_asocmaxrxt; | |
8add543e | 89 | asoc->pf_retrans = sp->pf_retrans; |
34515e94 | 90 | asoc->ps_retrans = sp->ps_retrans; |
aef587be | 91 | asoc->pf_expose = sp->pf_expose; |
5aa93bcf | 92 | |
1da177e4 LT |
93 | asoc->rto_initial = msecs_to_jiffies(sp->rtoinfo.srto_initial); |
94 | asoc->rto_max = msecs_to_jiffies(sp->rtoinfo.srto_max); | |
95 | asoc->rto_min = msecs_to_jiffies(sp->rtoinfo.srto_min); | |
96 | ||
52ccb8e9 FF |
97 | /* Initialize the association's heartbeat interval based on the |
98 | * sock configured value. | |
99 | */ | |
100 | asoc->hbinterval = msecs_to_jiffies(sp->hbinterval); | |
d1e462a7 | 101 | asoc->probe_interval = msecs_to_jiffies(sp->probe_interval); |
52ccb8e9 | 102 | |
e8a3001c XL |
103 | asoc->encap_port = sp->encap_port; |
104 | ||
52ccb8e9 FF |
105 | /* Initialize path max retrans value. */ |
106 | asoc->pathmaxrxt = sp->pathmaxrxt; | |
107 | ||
8a9c58d2 XL |
108 | asoc->flowlabel = sp->flowlabel; |
109 | asoc->dscp = sp->dscp; | |
110 | ||
52ccb8e9 FF |
111 | /* Set association default SACK delay */ |
112 | asoc->sackdelay = msecs_to_jiffies(sp->sackdelay); | |
d364d927 | 113 | asoc->sackfreq = sp->sackfreq; |
52ccb8e9 FF |
114 | |
115 | /* Set the association default flags controlling | |
116 | * Heartbeat, SACK delay, and Path MTU Discovery. | |
117 | */ | |
118 | asoc->param_flags = sp->param_flags; | |
119 | ||
9d2c881a | 120 | /* Initialize the maximum number of new data packets that can be sent |
1da177e4 LT |
121 | * in a burst. |
122 | */ | |
70331571 | 123 | asoc->max_burst = sp->max_burst; |
1da177e4 | 124 | |
a1e3a059 XL |
125 | asoc->subscribe = sp->subscribe; |
126 | ||
1e7d3d90 | 127 | /* initialize association timers */ |
1e7d3d90 VY |
128 | asoc->timeouts[SCTP_EVENT_TIMEOUT_T1_COOKIE] = asoc->rto_initial; |
129 | asoc->timeouts[SCTP_EVENT_TIMEOUT_T1_INIT] = asoc->rto_initial; | |
130 | asoc->timeouts[SCTP_EVENT_TIMEOUT_T2_SHUTDOWN] = asoc->rto_initial; | |
1e7d3d90 VY |
131 | |
132 | /* sctpimpguide Section 2.12.2 | |
133 | * If the 'T5-shutdown-guard' timer is used, it SHOULD be set to the | |
134 | * recommended value of 5 times 'RTO.Max'. | |
135 | */ | |
d808ad9a | 136 | asoc->timeouts[SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD] |
1e7d3d90 VY |
137 | = 5 * asoc->rto_max; |
138 | ||
52ccb8e9 | 139 | asoc->timeouts[SCTP_EVENT_TIMEOUT_SACK] = asoc->sackdelay; |
4e86729d NK |
140 | asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE] = |
141 | (unsigned long)sp->autoclose * HZ; | |
d808ad9a | 142 | |
421f91d2 | 143 | /* Initializes the timers */ |
b24b8a24 | 144 | for (i = SCTP_EVENT_TIMEOUT_NONE; i < SCTP_NUM_TIMEOUT_TYPES; ++i) |
9c3b5751 | 145 | timer_setup(&asoc->timers[i], sctp_timer_events[i], 0); |
1da177e4 LT |
146 | |
147 | /* Pull default initialization values from the sock options. | |
148 | * Note: This assumes that the values have already been | |
149 | * validated in the sock. | |
150 | */ | |
151 | asoc->c.sinit_max_instreams = sp->initmsg.sinit_max_instreams; | |
152 | asoc->c.sinit_num_ostreams = sp->initmsg.sinit_num_ostreams; | |
153 | asoc->max_init_attempts = sp->initmsg.sinit_max_attempts; | |
154 | ||
155 | asoc->max_init_timeo = | |
156 | msecs_to_jiffies(sp->initmsg.sinit_max_init_timeo); | |
157 | ||
1da177e4 LT |
158 | /* Set the local window size for receive. |
159 | * This is also the rcvbuf space per association. | |
160 | * RFC 6 - A SCTP receiver MUST be able to receive a minimum of | |
161 | * 1500 bytes in one SCTP packet. | |
162 | */ | |
049b3ff5 | 163 | if ((sk->sk_rcvbuf/2) < SCTP_DEFAULT_MINWINDOW) |
1da177e4 LT |
164 | asoc->rwnd = SCTP_DEFAULT_MINWINDOW; |
165 | else | |
049b3ff5 | 166 | asoc->rwnd = sk->sk_rcvbuf/2; |
1da177e4 LT |
167 | |
168 | asoc->a_rwnd = asoc->rwnd; | |
169 | ||
1da177e4 LT |
170 | /* Use my own max window until I learn something better. */ |
171 | asoc->peer.rwnd = SCTP_DEFAULT_MAXWINDOW; | |
172 | ||
049b3ff5 NH |
173 | /* Initialize the receive memory counter */ |
174 | atomic_set(&asoc->rmem_alloc, 0); | |
175 | ||
1da177e4 LT |
176 | init_waitqueue_head(&asoc->wait); |
177 | ||
178 | asoc->c.my_vtag = sctp_generate_tag(ep); | |
1da177e4 LT |
179 | asoc->c.my_port = ep->base.bind_addr.port; |
180 | ||
181 | asoc->c.initial_tsn = sctp_generate_tsn(ep); | |
182 | ||
183 | asoc->next_tsn = asoc->c.initial_tsn; | |
184 | ||
185 | asoc->ctsn_ack_point = asoc->next_tsn - 1; | |
186 | asoc->adv_peer_ack_point = asoc->ctsn_ack_point; | |
187 | asoc->highest_sacked = asoc->ctsn_ack_point; | |
188 | asoc->last_cwr_tsn = asoc->ctsn_ack_point; | |
1da177e4 | 189 | |
1da177e4 LT |
190 | /* ADDIP Section 4.1 Asconf Chunk Procedures |
191 | * | |
192 | * When an endpoint has an ASCONF signaled change to be sent to the | |
193 | * remote endpoint it should do the following: | |
194 | * ... | |
195 | * A2) a serial number should be assigned to the chunk. The serial | |
196 | * number SHOULD be a monotonically increasing number. The serial | |
197 | * numbers SHOULD be initialized at the start of the | |
198 | * association to the same value as the initial TSN. | |
199 | */ | |
200 | asoc->addip_serial = asoc->c.initial_tsn; | |
cc16f00f | 201 | asoc->strreset_outseq = asoc->c.initial_tsn; |
1da177e4 | 202 | |
79af02c2 | 203 | INIT_LIST_HEAD(&asoc->addip_chunk_list); |
a08de64d | 204 | INIT_LIST_HEAD(&asoc->asconf_ack_list); |
1da177e4 LT |
205 | |
206 | /* Make an empty list of remote transport addresses. */ | |
207 | INIT_LIST_HEAD(&asoc->peer.transport_addr_list); | |
208 | ||
209 | /* RFC 2960 5.1 Normal Establishment of an Association | |
210 | * | |
211 | * After the reception of the first data chunk in an | |
212 | * association the endpoint must immediately respond with a | |
213 | * sack to acknowledge the data chunk. Subsequent | |
214 | * acknowledgements should be done as described in Section | |
215 | * 6.2. | |
216 | * | |
217 | * [We implement this by telling a new association that it | |
218 | * already received one packet.] | |
219 | */ | |
220 | asoc->peer.sack_needed = 1; | |
4244854d | 221 | asoc->peer.sack_generation = 1; |
1da177e4 | 222 | |
1da177e4 LT |
223 | /* Create an input queue. */ |
224 | sctp_inq_init(&asoc->base.inqueue); | |
c4028958 | 225 | sctp_inq_set_th_handler(&asoc->base.inqueue, sctp_assoc_bh_rcv); |
1da177e4 LT |
226 | |
227 | /* Create an output queue. */ | |
228 | sctp_outq_init(asoc, &asoc->outqueue); | |
229 | ||
6fdfdef7 | 230 | sctp_ulpq_init(&asoc->ulpq, asoc); |
1da177e4 | 231 | |
181d8d20 XL |
232 | if (sctp_stream_init(&asoc->stream, asoc->c.sinit_num_ostreams, 0, gfp)) |
233 | goto stream_free; | |
3dbcc105 | 234 | |
4135cce7 XL |
235 | /* Initialize default path MTU. */ |
236 | asoc->pathmtu = sp->pathmtu; | |
237 | sctp_assoc_update_frag_point(asoc); | |
238 | ||
1da177e4 LT |
239 | /* Assume that peer would support both address types unless we are |
240 | * told otherwise. | |
241 | */ | |
242 | asoc->peer.ipv4_address = 1; | |
a2c39584 WY |
243 | if (asoc->base.sk->sk_family == PF_INET6) |
244 | asoc->peer.ipv6_address = 1; | |
1da177e4 LT |
245 | INIT_LIST_HEAD(&asoc->asocs); |
246 | ||
1da177e4 LT |
247 | asoc->default_stream = sp->default_stream; |
248 | asoc->default_ppid = sp->default_ppid; | |
249 | asoc->default_flags = sp->default_flags; | |
250 | asoc->default_context = sp->default_context; | |
251 | asoc->default_timetolive = sp->default_timetolive; | |
6ab792f5 | 252 | asoc->default_rcv_context = sp->default_rcv_context; |
1da177e4 | 253 | |
a29a5bd4 VY |
254 | /* AUTH related initializations */ |
255 | INIT_LIST_HEAD(&asoc->endpoint_shared_keys); | |
58194778 | 256 | if (sctp_auth_asoc_copy_shkeys(ep, asoc, gfp)) |
3dbcc105 | 257 | goto stream_free; |
a29a5bd4 VY |
258 | |
259 | asoc->active_key_id = ep->active_key_id; | |
9fb657ae | 260 | asoc->strreset_enable = ep->strreset_enable; |
a29a5bd4 | 261 | |
a29a5bd4 VY |
262 | /* Save the hmacs and chunks list into this association */ |
263 | if (ep->auth_hmacs_list) | |
264 | memcpy(asoc->c.auth_hmacs, ep->auth_hmacs_list, | |
265 | ntohs(ep->auth_hmacs_list->param_hdr.length)); | |
266 | if (ep->auth_chunk_list) | |
267 | memcpy(asoc->c.auth_chunks, ep->auth_chunk_list, | |
268 | ntohs(ep->auth_chunk_list->param_hdr.length)); | |
269 | ||
270 | /* Get the AUTH random number for this association */ | |
3c918704 | 271 | p = (struct sctp_paramhdr *)asoc->c.auth_random; |
a29a5bd4 | 272 | p->type = SCTP_PARAM_RANDOM; |
3c918704 | 273 | p->length = htons(sizeof(*p) + SCTP_AUTH_RANDOM_LENGTH); |
a29a5bd4 VY |
274 | get_random_bytes(p+1, SCTP_AUTH_RANDOM_LENGTH); |
275 | ||
1da177e4 LT |
276 | return asoc; |
277 | ||
3dbcc105 | 278 | stream_free: |
cee360ab | 279 | sctp_stream_free(&asoc->stream); |
1da177e4 | 280 | sock_put(asoc->base.sk); |
2e0c9e79 | 281 | sctp_endpoint_put(asoc->ep); |
1da177e4 LT |
282 | return NULL; |
283 | } | |
284 | ||
285 | /* Allocate and initialize a new association */ | |
286 | struct sctp_association *sctp_association_new(const struct sctp_endpoint *ep, | |
1c662018 XL |
287 | const struct sock *sk, |
288 | enum sctp_scope scope, gfp_t gfp) | |
1da177e4 LT |
289 | { |
290 | struct sctp_association *asoc; | |
291 | ||
939cfa75 | 292 | asoc = kzalloc(sizeof(*asoc), gfp); |
1da177e4 LT |
293 | if (!asoc) |
294 | goto fail; | |
295 | ||
296 | if (!sctp_association_init(asoc, ep, sk, scope, gfp)) | |
297 | goto fail_init; | |
298 | ||
1da177e4 | 299 | SCTP_DBG_OBJCNT_INC(assoc); |
bb33381d DB |
300 | |
301 | pr_debug("Created asoc %p\n", asoc); | |
1da177e4 LT |
302 | |
303 | return asoc; | |
304 | ||
305 | fail_init: | |
306 | kfree(asoc); | |
307 | fail: | |
308 | return NULL; | |
309 | } | |
310 | ||
311 | /* Free this association if possible. There may still be users, so | |
312 | * the actual deallocation may be delayed. | |
313 | */ | |
314 | void sctp_association_free(struct sctp_association *asoc) | |
315 | { | |
316 | struct sock *sk = asoc->base.sk; | |
317 | struct sctp_transport *transport; | |
318 | struct list_head *pos, *temp; | |
319 | int i; | |
320 | ||
de76e695 VY |
321 | /* Only real associations count against the endpoint, so |
322 | * don't bother for if this is a temporary association. | |
323 | */ | |
d3217b15 | 324 | if (!list_empty(&asoc->asocs)) { |
de76e695 VY |
325 | list_del(&asoc->asocs); |
326 | ||
327 | /* Decrement the backlog value for a TCP-style listening | |
328 | * socket. | |
329 | */ | |
330 | if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) | |
7976a11b | 331 | sk_acceptq_removed(sk); |
de76e695 | 332 | } |
1da177e4 LT |
333 | |
334 | /* Mark as dead, so other users can know this structure is | |
335 | * going away. | |
336 | */ | |
0022d2dd | 337 | asoc->base.dead = true; |
1da177e4 LT |
338 | |
339 | /* Dispose of any data lying around in the outqueue. */ | |
340 | sctp_outq_free(&asoc->outqueue); | |
341 | ||
342 | /* Dispose of any pending messages for the upper layer. */ | |
343 | sctp_ulpq_free(&asoc->ulpq); | |
344 | ||
345 | /* Dispose of any pending chunks on the inqueue. */ | |
346 | sctp_inq_free(&asoc->base.inqueue); | |
347 | ||
8e1ee18c VY |
348 | sctp_tsnmap_free(&asoc->peer.tsn_map); |
349 | ||
a8386317 | 350 | /* Free stream information. */ |
cee360ab | 351 | sctp_stream_free(&asoc->stream); |
1da177e4 | 352 | |
7b9438de XL |
353 | if (asoc->strreset_chunk) |
354 | sctp_chunk_free(asoc->strreset_chunk); | |
355 | ||
1da177e4 LT |
356 | /* Clean up the bound address list. */ |
357 | sctp_bind_addr_free(&asoc->base.bind_addr); | |
358 | ||
359 | /* Do we need to go through all of our timers and | |
360 | * delete them? To be safe we will try to delete all, but we | |
361 | * should be able to go through and make a guess based | |
362 | * on our state. | |
363 | */ | |
364 | for (i = SCTP_EVENT_TIMEOUT_NONE; i < SCTP_NUM_TIMEOUT_TYPES; ++i) { | |
8fa7292f | 365 | if (timer_delete(&asoc->timers[i])) |
1da177e4 LT |
366 | sctp_association_put(asoc); |
367 | } | |
368 | ||
369 | /* Free peer's cached cookie. */ | |
a51482bd | 370 | kfree(asoc->peer.cookie); |
730fc3d0 VY |
371 | kfree(asoc->peer.peer_random); |
372 | kfree(asoc->peer.peer_chunks); | |
373 | kfree(asoc->peer.peer_hmacs); | |
1da177e4 LT |
374 | |
375 | /* Release the transport structures. */ | |
376 | list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) { | |
377 | transport = list_entry(pos, struct sctp_transport, transports); | |
45122ca2 | 378 | list_del_rcu(pos); |
4f008781 | 379 | sctp_unhash_transport(transport); |
1da177e4 LT |
380 | sctp_transport_free(transport); |
381 | } | |
382 | ||
3f7a87d2 FF |
383 | asoc->peer.transport_count = 0; |
384 | ||
a000c01e | 385 | sctp_asconf_queue_teardown(asoc); |
1da177e4 | 386 | |
8a07eb0a | 387 | /* Free pending address space being deleted */ |
7d37d0c1 | 388 | kfree(asoc->asconf_addr_del_pending); |
8a07eb0a | 389 | |
a29a5bd4 VY |
390 | /* AUTH - Free the endpoint shared keys */ |
391 | sctp_auth_destroy_keys(&asoc->endpoint_shared_keys); | |
392 | ||
393 | /* AUTH - Free the association shared key */ | |
394 | sctp_auth_key_put(asoc->asoc_shared_key); | |
395 | ||
1da177e4 LT |
396 | sctp_association_put(asoc); |
397 | } | |
398 | ||
399 | /* Cleanup and free up an association. */ | |
400 | static void sctp_association_destroy(struct sctp_association *asoc) | |
401 | { | |
bb33381d DB |
402 | if (unlikely(!asoc->base.dead)) { |
403 | WARN(1, "Attempt to destroy undead association %p!\n", asoc); | |
404 | return; | |
405 | } | |
1da177e4 LT |
406 | |
407 | sctp_endpoint_put(asoc->ep); | |
408 | sock_put(asoc->base.sk); | |
409 | ||
410 | if (asoc->assoc_id != 0) { | |
411 | spin_lock_bh(&sctp_assocs_id_lock); | |
412 | idr_remove(&sctp_assocs_id, asoc->assoc_id); | |
413 | spin_unlock_bh(&sctp_assocs_id_lock); | |
414 | } | |
415 | ||
547b792c | 416 | WARN_ON(atomic_read(&asoc->rmem_alloc)); |
049b3ff5 | 417 | |
fb6df5a6 | 418 | kfree_rcu(asoc, rcu); |
ff2266cd | 419 | SCTP_DBG_OBJCNT_DEC(assoc); |
1da177e4 LT |
420 | } |
421 | ||
422 | /* Change the primary destination address for the peer. */ | |
423 | void sctp_assoc_set_primary(struct sctp_association *asoc, | |
424 | struct sctp_transport *transport) | |
425 | { | |
319fa2a2 VY |
426 | int changeover = 0; |
427 | ||
428 | /* it's a changeover only if we already have a primary path | |
429 | * that we are changing | |
430 | */ | |
431 | if (asoc->peer.primary_path != NULL && | |
432 | asoc->peer.primary_path != transport) | |
433 | changeover = 1 ; | |
434 | ||
1da177e4 | 435 | asoc->peer.primary_path = transport; |
50ce4c09 | 436 | sctp_ulpevent_notify_peer_addr_change(transport, |
5cd0b917 | 437 | SCTP_ADDR_MADE_PRIM, 0); |
1da177e4 LT |
438 | |
439 | /* Set a default msg_name for events. */ | |
440 | memcpy(&asoc->peer.primary_addr, &transport->ipaddr, | |
441 | sizeof(union sctp_addr)); | |
442 | ||
443 | /* If the primary path is changing, assume that the | |
444 | * user wants to use this new path. | |
445 | */ | |
ad8fec17 SS |
446 | if ((transport->state == SCTP_ACTIVE) || |
447 | (transport->state == SCTP_UNKNOWN)) | |
1da177e4 LT |
448 | asoc->peer.active_path = transport; |
449 | ||
450 | /* | |
451 | * SFR-CACC algorithm: | |
452 | * Upon the receipt of a request to change the primary | |
453 | * destination address, on the data structure for the new | |
454 | * primary destination, the sender MUST do the following: | |
455 | * | |
456 | * 1) If CHANGEOVER_ACTIVE is set, then there was a switch | |
457 | * to this destination address earlier. The sender MUST set | |
458 | * CYCLING_CHANGEOVER to indicate that this switch is a | |
459 | * double switch to the same destination address. | |
e0e9db17 VY |
460 | * |
461 | * Really, only bother is we have data queued or outstanding on | |
462 | * the association. | |
1da177e4 | 463 | */ |
e0e9db17 VY |
464 | if (!asoc->outqueue.outstanding_bytes && !asoc->outqueue.out_qlen) |
465 | return; | |
466 | ||
1da177e4 | 467 | if (transport->cacc.changeover_active) |
319fa2a2 | 468 | transport->cacc.cycling_changeover = changeover; |
1da177e4 LT |
469 | |
470 | /* 2) The sender MUST set CHANGEOVER_ACTIVE to indicate that | |
471 | * a changeover has occurred. | |
472 | */ | |
319fa2a2 | 473 | transport->cacc.changeover_active = changeover; |
1da177e4 LT |
474 | |
475 | /* 3) The sender MUST store the next TSN to be sent in | |
476 | * next_tsn_at_change. | |
477 | */ | |
478 | transport->cacc.next_tsn_at_change = asoc->next_tsn; | |
479 | } | |
480 | ||
3f7a87d2 FF |
481 | /* Remove a transport from an association. */ |
482 | void sctp_assoc_rm_peer(struct sctp_association *asoc, | |
483 | struct sctp_transport *peer) | |
484 | { | |
df132eff XL |
485 | struct sctp_transport *transport; |
486 | struct list_head *pos; | |
487 | struct sctp_chunk *ch; | |
3f7a87d2 | 488 | |
bb33381d DB |
489 | pr_debug("%s: association:%p addr:%pISpc\n", |
490 | __func__, asoc, &peer->ipaddr.sa); | |
3f7a87d2 FF |
491 | |
492 | /* If we are to remove the current retran_path, update it | |
493 | * to the next peer before removing this peer from the list. | |
494 | */ | |
495 | if (asoc->peer.retran_path == peer) | |
496 | sctp_assoc_update_retran_path(asoc); | |
497 | ||
498 | /* Remove this peer from the list. */ | |
45122ca2 | 499 | list_del_rcu(&peer->transports); |
4f008781 XL |
500 | /* Remove this peer from the transport hashtable */ |
501 | sctp_unhash_transport(peer); | |
3f7a87d2 FF |
502 | |
503 | /* Get the first transport of asoc. */ | |
504 | pos = asoc->peer.transport_addr_list.next; | |
505 | transport = list_entry(pos, struct sctp_transport, transports); | |
506 | ||
507 | /* Update any entries that match the peer to be deleted. */ | |
508 | if (asoc->peer.primary_path == peer) | |
509 | sctp_assoc_set_primary(asoc, transport); | |
510 | if (asoc->peer.active_path == peer) | |
511 | asoc->peer.active_path = transport; | |
9494c7c5 WY |
512 | if (asoc->peer.retran_path == peer) |
513 | asoc->peer.retran_path = transport; | |
3f7a87d2 FF |
514 | if (asoc->peer.last_data_from == peer) |
515 | asoc->peer.last_data_from = transport; | |
516 | ||
7b9438de XL |
517 | if (asoc->strreset_chunk && |
518 | asoc->strreset_chunk->transport == peer) { | |
519 | asoc->strreset_chunk->transport = transport; | |
520 | sctp_transport_reset_reconf_timer(transport); | |
521 | } | |
522 | ||
3f7a87d2 FF |
523 | /* If we remove the transport an INIT was last sent to, set it to |
524 | * NULL. Combined with the update of the retran path above, this | |
525 | * will cause the next INIT to be sent to the next available | |
526 | * transport, maintaining the cycle. | |
527 | */ | |
528 | if (asoc->init_last_sent_to == peer) | |
529 | asoc->init_last_sent_to = NULL; | |
530 | ||
6345b199 WY |
531 | /* If we remove the transport an SHUTDOWN was last sent to, set it |
532 | * to NULL. Combined with the update of the retran path above, this | |
533 | * will cause the next SHUTDOWN to be sent to the next available | |
534 | * transport, maintaining the cycle. | |
535 | */ | |
536 | if (asoc->shutdown_last_sent_to == peer) | |
537 | asoc->shutdown_last_sent_to = NULL; | |
538 | ||
10a43cea WY |
539 | /* If we remove the transport an ASCONF was last sent to, set it to |
540 | * NULL. | |
541 | */ | |
542 | if (asoc->addip_last_asconf && | |
543 | asoc->addip_last_asconf->transport == peer) | |
544 | asoc->addip_last_asconf->transport = NULL; | |
545 | ||
31b02e15 VY |
546 | /* If we have something on the transmitted list, we have to |
547 | * save it off. The best place is the active path. | |
548 | */ | |
549 | if (!list_empty(&peer->transmitted)) { | |
550 | struct sctp_transport *active = asoc->peer.active_path; | |
31b02e15 VY |
551 | |
552 | /* Reset the transport of each chunk on this list */ | |
553 | list_for_each_entry(ch, &peer->transmitted, | |
554 | transmitted_list) { | |
555 | ch->transport = NULL; | |
556 | ch->rtt_in_progress = 0; | |
557 | } | |
558 | ||
559 | list_splice_tail_init(&peer->transmitted, | |
560 | &active->transmitted); | |
561 | ||
562 | /* Start a T3 timer here in case it wasn't running so | |
563 | * that these migrated packets have a chance to get | |
2bccbadf | 564 | * retransmitted. |
31b02e15 VY |
565 | */ |
566 | if (!timer_pending(&active->T3_rtx_timer)) | |
567 | if (!mod_timer(&active->T3_rtx_timer, | |
568 | jiffies + active->rto)) | |
569 | sctp_transport_hold(active); | |
570 | } | |
571 | ||
df132eff XL |
572 | list_for_each_entry(ch, &asoc->outqueue.out_chunk_list, list) |
573 | if (ch->transport == peer) | |
574 | ch->transport = NULL; | |
575 | ||
3f7a87d2 FF |
576 | asoc->peer.transport_count--; |
577 | ||
50ce4c09 | 578 | sctp_ulpevent_notify_peer_addr_change(peer, SCTP_ADDR_REMOVED, 0); |
3f7a87d2 FF |
579 | sctp_transport_free(peer); |
580 | } | |
581 | ||
1da177e4 LT |
582 | /* Add a transport address to an association. */ |
583 | struct sctp_transport *sctp_assoc_add_peer(struct sctp_association *asoc, | |
584 | const union sctp_addr *addr, | |
dd0fc66f | 585 | const gfp_t gfp, |
3f7a87d2 | 586 | const int peer_state) |
1da177e4 LT |
587 | { |
588 | struct sctp_transport *peer; | |
589 | struct sctp_sock *sp; | |
590 | unsigned short port; | |
591 | ||
592 | sp = sctp_sk(asoc->base.sk); | |
593 | ||
594 | /* AF_INET and AF_INET6 share common port field. */ | |
4bdf4b5f | 595 | port = ntohs(addr->v4.sin_port); |
1da177e4 | 596 | |
bb33381d DB |
597 | pr_debug("%s: association:%p addr:%pISpc state:%d\n", __func__, |
598 | asoc, &addr->sa, peer_state); | |
3f7a87d2 | 599 | |
1da177e4 LT |
600 | /* Set the port if it has not been set yet. */ |
601 | if (0 == asoc->peer.port) | |
602 | asoc->peer.port = port; | |
603 | ||
604 | /* Check to see if this is a duplicate. */ | |
605 | peer = sctp_assoc_lookup_paddr(asoc, addr); | |
3f7a87d2 | 606 | if (peer) { |
add52379 VY |
607 | /* An UNKNOWN state is only set on transports added by |
608 | * user in sctp_connectx() call. Such transports should be | |
609 | * considered CONFIRMED per RFC 4960, Section 5.4. | |
610 | */ | |
ad8fec17 | 611 | if (peer->state == SCTP_UNKNOWN) { |
add52379 | 612 | peer->state = SCTP_ACTIVE; |
ad8fec17 | 613 | } |
1da177e4 | 614 | return peer; |
3f7a87d2 | 615 | } |
1da177e4 | 616 | |
4e7696d9 | 617 | peer = sctp_transport_new(asoc->base.net, addr, gfp); |
1da177e4 LT |
618 | if (!peer) |
619 | return NULL; | |
620 | ||
621 | sctp_transport_set_owner(peer, asoc); | |
622 | ||
52ccb8e9 FF |
623 | /* Initialize the peer's heartbeat interval based on the |
624 | * association configured value. | |
625 | */ | |
626 | peer->hbinterval = asoc->hbinterval; | |
d1e462a7 | 627 | peer->probe_interval = asoc->probe_interval; |
52ccb8e9 | 628 | |
e8a3001c XL |
629 | peer->encap_port = asoc->encap_port; |
630 | ||
52ccb8e9 FF |
631 | /* Set the path max_retrans. */ |
632 | peer->pathmaxrxt = asoc->pathmaxrxt; | |
633 | ||
2bccbadf | 634 | /* And the partial failure retrans threshold */ |
5aa93bcf | 635 | peer->pf_retrans = asoc->pf_retrans; |
34515e94 XL |
636 | /* And the primary path switchover retrans threshold */ |
637 | peer->ps_retrans = asoc->ps_retrans; | |
5aa93bcf | 638 | |
52ccb8e9 FF |
639 | /* Initialize the peer's SACK delay timeout based on the |
640 | * association configured value. | |
641 | */ | |
642 | peer->sackdelay = asoc->sackdelay; | |
d364d927 | 643 | peer->sackfreq = asoc->sackfreq; |
52ccb8e9 | 644 | |
4be4139f XL |
645 | if (addr->sa.sa_family == AF_INET6) { |
646 | __be32 info = addr->v6.sin6_flowinfo; | |
647 | ||
648 | if (info) { | |
649 | peer->flowlabel = ntohl(info & IPV6_FLOWLABEL_MASK); | |
650 | peer->flowlabel |= SCTP_FLOWLABEL_SET_MASK; | |
651 | } else { | |
652 | peer->flowlabel = asoc->flowlabel; | |
653 | } | |
654 | } | |
8a9c58d2 XL |
655 | peer->dscp = asoc->dscp; |
656 | ||
52ccb8e9 FF |
657 | /* Enable/disable heartbeat, SACK delay, and path MTU discovery |
658 | * based on association setting. | |
659 | */ | |
660 | peer->param_flags = asoc->param_flags; | |
661 | ||
1da177e4 | 662 | /* Initialize the pmtu of the transport. */ |
800e00c1 | 663 | sctp_transport_route(peer, NULL, sp); |
1da177e4 LT |
664 | |
665 | /* If this is the first transport addr on this association, | |
666 | * initialize the association PMTU to the peer's PMTU. | |
667 | * If not and the current association PMTU is higher than the new | |
668 | * peer's PMTU, reset the association PMTU to the new peer's PMTU. | |
669 | */ | |
c4b2893d MRL |
670 | sctp_assoc_set_pmtu(asoc, asoc->pathmtu ? |
671 | min_t(int, peer->pathmtu, asoc->pathmtu) : | |
672 | peer->pathmtu); | |
bb33381d | 673 | |
6d0ccbac | 674 | peer->pmtu_pending = 0; |
1da177e4 | 675 | |
1da177e4 LT |
676 | /* The asoc->peer.port might not be meaningful yet, but |
677 | * initialize the packet structure anyway. | |
678 | */ | |
679 | sctp_packet_init(&peer->packet, peer, asoc->base.bind_addr.port, | |
680 | asoc->peer.port); | |
681 | ||
682 | /* 7.2.1 Slow-Start | |
683 | * | |
684 | * o The initial cwnd before DATA transmission or after a sufficiently | |
685 | * long idle period MUST be set to | |
686 | * min(4*MTU, max(2*MTU, 4380 bytes)) | |
687 | * | |
688 | * o The initial value of ssthresh MAY be arbitrarily high | |
689 | * (for example, implementations MAY use the size of the | |
690 | * receiver advertised window). | |
691 | */ | |
52ccb8e9 | 692 | peer->cwnd = min(4*asoc->pathmtu, max_t(__u32, 2*asoc->pathmtu, 4380)); |
1da177e4 LT |
693 | |
694 | /* At this point, we may not have the receiver's advertised window, | |
695 | * so initialize ssthresh to the default value and it will be set | |
696 | * later when we process the INIT. | |
697 | */ | |
698 | peer->ssthresh = SCTP_DEFAULT_MAXWINDOW; | |
699 | ||
700 | peer->partial_bytes_acked = 0; | |
701 | peer->flight_size = 0; | |
46d5a808 | 702 | peer->burst_limited = 0; |
1da177e4 | 703 | |
1da177e4 LT |
704 | /* Set the transport's RTO.initial value */ |
705 | peer->rto = asoc->rto_initial; | |
196d6759 | 706 | sctp_max_rto(asoc, peer); |
1da177e4 | 707 | |
3f7a87d2 FF |
708 | /* Set the peer's active state. */ |
709 | peer->state = peer_state; | |
710 | ||
7fda702f XL |
711 | /* Add this peer into the transport hashtable */ |
712 | if (sctp_hash_transport(peer)) { | |
713 | sctp_transport_free(peer); | |
714 | return NULL; | |
715 | } | |
716 | ||
7307e4fa XL |
717 | sctp_transport_pl_reset(peer); |
718 | ||
1da177e4 | 719 | /* Attach the remote transport to our asoc. */ |
45122ca2 | 720 | list_add_tail_rcu(&peer->transports, &asoc->peer.transport_addr_list); |
3f7a87d2 | 721 | asoc->peer.transport_count++; |
1da177e4 | 722 | |
50ce4c09 | 723 | sctp_ulpevent_notify_peer_addr_change(peer, SCTP_ADDR_ADDED, 0); |
4b774032 | 724 | |
1da177e4 LT |
725 | /* If we do not yet have a primary path, set one. */ |
726 | if (!asoc->peer.primary_path) { | |
727 | sctp_assoc_set_primary(asoc, peer); | |
728 | asoc->peer.retran_path = peer; | |
729 | } | |
730 | ||
fbdf501c VY |
731 | if (asoc->peer.active_path == asoc->peer.retran_path && |
732 | peer->state != SCTP_UNCONFIRMED) { | |
1da177e4 | 733 | asoc->peer.retran_path = peer; |
3f7a87d2 | 734 | } |
1da177e4 LT |
735 | |
736 | return peer; | |
737 | } | |
738 | ||
1da177e4 LT |
739 | /* Lookup a transport by address. */ |
740 | struct sctp_transport *sctp_assoc_lookup_paddr( | |
741 | const struct sctp_association *asoc, | |
742 | const union sctp_addr *address) | |
743 | { | |
744 | struct sctp_transport *t; | |
1da177e4 LT |
745 | |
746 | /* Cycle through all transports searching for a peer address. */ | |
747 | ||
9dbc15f0 RD |
748 | list_for_each_entry(t, &asoc->peer.transport_addr_list, |
749 | transports) { | |
1da177e4 LT |
750 | if (sctp_cmp_addr_exact(address, &t->ipaddr)) |
751 | return t; | |
752 | } | |
753 | ||
754 | return NULL; | |
755 | } | |
756 | ||
42e30bf3 VY |
757 | /* Remove all transports except a give one */ |
758 | void sctp_assoc_del_nonprimary_peers(struct sctp_association *asoc, | |
759 | struct sctp_transport *primary) | |
760 | { | |
761 | struct sctp_transport *temp; | |
762 | struct sctp_transport *t; | |
763 | ||
764 | list_for_each_entry_safe(t, temp, &asoc->peer.transport_addr_list, | |
765 | transports) { | |
766 | /* if the current transport is not the primary one, delete it */ | |
767 | if (t != primary) | |
768 | sctp_assoc_rm_peer(asoc, t); | |
769 | } | |
42e30bf3 VY |
770 | } |
771 | ||
1da177e4 LT |
772 | /* Engage in transport control operations. |
773 | * Mark the transport up or down and send a notification to the user. | |
774 | * Select and update the new active and retran paths. | |
775 | */ | |
776 | void sctp_assoc_control_transport(struct sctp_association *asoc, | |
777 | struct sctp_transport *transport, | |
0ceaeebe | 778 | enum sctp_transport_cmd command, |
1da177e4 LT |
779 | sctp_sn_error_t error) |
780 | { | |
768e1518 | 781 | int spc_state = SCTP_ADDR_AVAILABLE; |
5aa93bcf | 782 | bool ulp_notify = true; |
1da177e4 LT |
783 | |
784 | /* Record the transition on the transport. */ | |
785 | switch (command) { | |
786 | case SCTP_TRANSPORT_UP: | |
1ae4114d VY |
787 | /* If we are moving from UNCONFIRMED state due |
788 | * to heartbeat success, report the SCTP_ADDR_CONFIRMED | |
789 | * state to the user, otherwise report SCTP_ADDR_AVAILABLE. | |
790 | */ | |
768e1518 XL |
791 | if (transport->state == SCTP_PF && |
792 | asoc->pf_expose != SCTP_PF_EXPOSE_ENABLE) | |
5aa93bcf | 793 | ulp_notify = false; |
768e1518 XL |
794 | else if (transport->state == SCTP_UNCONFIRMED && |
795 | error == SCTP_HEARTBEAT_SUCCESS) | |
796 | spc_state = SCTP_ADDR_CONFIRMED; | |
797 | ||
3f7a87d2 | 798 | transport->state = SCTP_ACTIVE; |
7307e4fa | 799 | sctp_transport_pl_reset(transport); |
1da177e4 LT |
800 | break; |
801 | ||
802 | case SCTP_TRANSPORT_DOWN: | |
40187886 VY |
803 | /* If the transport was never confirmed, do not transition it |
804 | * to inactive state. Also, release the cached route since | |
805 | * there may be a better route next time. | |
cc75689a | 806 | */ |
768e1518 | 807 | if (transport->state != SCTP_UNCONFIRMED) { |
cc75689a | 808 | transport->state = SCTP_INACTIVE; |
7307e4fa | 809 | sctp_transport_pl_reset(transport); |
768e1518 XL |
810 | spc_state = SCTP_ADDR_UNREACHABLE; |
811 | } else { | |
c86a773c | 812 | sctp_transport_dst_release(transport); |
061079ac | 813 | ulp_notify = false; |
40187886 | 814 | } |
1da177e4 LT |
815 | break; |
816 | ||
5aa93bcf NH |
817 | case SCTP_TRANSPORT_PF: |
818 | transport->state = SCTP_PF; | |
768e1518 XL |
819 | if (asoc->pf_expose != SCTP_PF_EXPOSE_ENABLE) |
820 | ulp_notify = false; | |
821 | else | |
822 | spc_state = SCTP_ADDR_POTENTIALLY_FAILED; | |
5aa93bcf NH |
823 | break; |
824 | ||
1da177e4 LT |
825 | default: |
826 | return; | |
3ff50b79 | 827 | } |
1da177e4 | 828 | |
b82e8f31 DB |
829 | /* Generate and send a SCTP_PEER_ADDR_CHANGE notification |
830 | * to the user. | |
1da177e4 | 831 | */ |
4b774032 | 832 | if (ulp_notify) |
50ce4c09 | 833 | sctp_ulpevent_notify_peer_addr_change(transport, |
4b774032 | 834 | spc_state, error); |
1da177e4 LT |
835 | |
836 | /* Select new active and retran paths. */ | |
b82e8f31 | 837 | sctp_select_active_and_retran_path(asoc); |
1da177e4 LT |
838 | } |
839 | ||
840 | /* Hold a reference to an association. */ | |
841 | void sctp_association_hold(struct sctp_association *asoc) | |
842 | { | |
c638457a | 843 | refcount_inc(&asoc->base.refcnt); |
1da177e4 LT |
844 | } |
845 | ||
846 | /* Release a reference to an association and cleanup | |
847 | * if there are no more references. | |
848 | */ | |
849 | void sctp_association_put(struct sctp_association *asoc) | |
850 | { | |
c638457a | 851 | if (refcount_dec_and_test(&asoc->base.refcnt)) |
1da177e4 LT |
852 | sctp_association_destroy(asoc); |
853 | } | |
854 | ||
855 | /* Allocate the next TSN, Transmission Sequence Number, for the given | |
856 | * association. | |
857 | */ | |
858 | __u32 sctp_association_get_next_tsn(struct sctp_association *asoc) | |
859 | { | |
860 | /* From Section 1.6 Serial Number Arithmetic: | |
861 | * Transmission Sequence Numbers wrap around when they reach | |
862 | * 2**32 - 1. That is, the next TSN a DATA chunk MUST use | |
863 | * after transmitting TSN = 2*32 - 1 is TSN = 0. | |
864 | */ | |
865 | __u32 retval = asoc->next_tsn; | |
866 | asoc->next_tsn++; | |
867 | asoc->unack_data++; | |
868 | ||
869 | return retval; | |
870 | } | |
871 | ||
872 | /* Compare two addresses to see if they match. Wildcard addresses | |
873 | * only match themselves. | |
874 | */ | |
875 | int sctp_cmp_addr_exact(const union sctp_addr *ss1, | |
876 | const union sctp_addr *ss2) | |
877 | { | |
878 | struct sctp_af *af; | |
879 | ||
880 | af = sctp_get_af_specific(ss1->sa.sa_family); | |
881 | if (unlikely(!af)) | |
882 | return 0; | |
883 | ||
884 | return af->cmp_addr(ss1, ss2); | |
885 | } | |
886 | ||
887 | /* Return an ecne chunk to get prepended to a packet. | |
888 | * Note: We are sly and return a shared, prealloced chunk. FIXME: | |
889 | * No we don't, but we could/should. | |
890 | */ | |
891 | struct sctp_chunk *sctp_get_ecne_prepend(struct sctp_association *asoc) | |
892 | { | |
8b7318d3 | 893 | if (!asoc->need_ecne) |
894 | return NULL; | |
1da177e4 LT |
895 | |
896 | /* Send ECNE if needed. | |
897 | * Not being able to allocate a chunk here is not deadly. | |
898 | */ | |
8b7318d3 | 899 | return sctp_make_ecne(asoc, asoc->last_ecne_tsn); |
1da177e4 LT |
900 | } |
901 | ||
902 | /* | |
903 | * Find which transport this TSN was sent on. | |
904 | */ | |
905 | struct sctp_transport *sctp_assoc_lookup_tsn(struct sctp_association *asoc, | |
906 | __u32 tsn) | |
907 | { | |
908 | struct sctp_transport *active; | |
909 | struct sctp_transport *match; | |
1da177e4 LT |
910 | struct sctp_transport *transport; |
911 | struct sctp_chunk *chunk; | |
dbc16db1 | 912 | __be32 key = htonl(tsn); |
1da177e4 LT |
913 | |
914 | match = NULL; | |
915 | ||
916 | /* | |
917 | * FIXME: In general, find a more efficient data structure for | |
918 | * searching. | |
919 | */ | |
920 | ||
921 | /* | |
922 | * The general strategy is to search each transport's transmitted | |
923 | * list. Return which transport this TSN lives on. | |
924 | * | |
925 | * Let's be hopeful and check the active_path first. | |
926 | * Another optimization would be to know if there is only one | |
927 | * outbound path and not have to look for the TSN at all. | |
928 | * | |
929 | */ | |
930 | ||
931 | active = asoc->peer.active_path; | |
932 | ||
9dbc15f0 RD |
933 | list_for_each_entry(chunk, &active->transmitted, |
934 | transmitted_list) { | |
1da177e4 LT |
935 | |
936 | if (key == chunk->subh.data_hdr->tsn) { | |
937 | match = active; | |
938 | goto out; | |
939 | } | |
940 | } | |
941 | ||
942 | /* If not found, go search all the other transports. */ | |
9dbc15f0 RD |
943 | list_for_each_entry(transport, &asoc->peer.transport_addr_list, |
944 | transports) { | |
1da177e4 LT |
945 | |
946 | if (transport == active) | |
2317f449 | 947 | continue; |
9dbc15f0 RD |
948 | list_for_each_entry(chunk, &transport->transmitted, |
949 | transmitted_list) { | |
1da177e4 LT |
950 | if (key == chunk->subh.data_hdr->tsn) { |
951 | match = transport; | |
952 | goto out; | |
953 | } | |
954 | } | |
955 | } | |
956 | out: | |
957 | return match; | |
958 | } | |
959 | ||
1da177e4 | 960 | /* Do delayed input processing. This is scheduled by sctp_rcv(). */ |
c4028958 | 961 | static void sctp_assoc_bh_rcv(struct work_struct *work) |
1da177e4 | 962 | { |
c4028958 DH |
963 | struct sctp_association *asoc = |
964 | container_of(work, struct sctp_association, | |
965 | base.inqueue.immediate); | |
4e7696d9 | 966 | struct net *net = asoc->base.net; |
bfc6f827 | 967 | union sctp_subtype subtype; |
1da177e4 LT |
968 | struct sctp_endpoint *ep; |
969 | struct sctp_chunk *chunk; | |
1da177e4 | 970 | struct sctp_inq *inqueue; |
59d8d443 | 971 | int first_time = 1; /* is this the first time through the loop */ |
1da177e4 | 972 | int error = 0; |
59d8d443 | 973 | int state; |
1da177e4 LT |
974 | |
975 | /* The association should be held so we should be safe. */ | |
976 | ep = asoc->ep; | |
1da177e4 LT |
977 | |
978 | inqueue = &asoc->base.inqueue; | |
979 | sctp_association_hold(asoc); | |
980 | while (NULL != (chunk = sctp_inq_pop(inqueue))) { | |
981 | state = asoc->state; | |
982 | subtype = SCTP_ST_CHUNK(chunk->chunk_hdr->type); | |
983 | ||
59d8d443 XL |
984 | /* If the first chunk in the packet is AUTH, do special |
985 | * processing specified in Section 6.3 of SCTP-AUTH spec | |
986 | */ | |
987 | if (first_time && subtype.chunk == SCTP_CID_AUTH) { | |
988 | struct sctp_chunkhdr *next_hdr; | |
989 | ||
990 | next_hdr = sctp_inq_peek(inqueue); | |
991 | if (!next_hdr) | |
992 | goto normal; | |
993 | ||
994 | /* If the next chunk is COOKIE-ECHO, skip the AUTH | |
995 | * chunk while saving a pointer to it so we can do | |
996 | * Authentication later (during cookie-echo | |
997 | * processing). | |
998 | */ | |
999 | if (next_hdr->type == SCTP_CID_COOKIE_ECHO) { | |
1000 | chunk->auth_chunk = skb_clone(chunk->skb, | |
1001 | GFP_ATOMIC); | |
1002 | chunk->auth = 1; | |
1003 | continue; | |
1004 | } | |
1005 | } | |
1006 | ||
1007 | normal: | |
bbd0d598 VY |
1008 | /* SCTP-AUTH, Section 6.3: |
1009 | * The receiver has a list of chunk types which it expects | |
1010 | * to be received only after an AUTH-chunk. This list has | |
1011 | * been sent to the peer during the association setup. It | |
1012 | * MUST silently discard these chunks if they are not placed | |
1013 | * after an AUTH chunk in the packet. | |
1014 | */ | |
1015 | if (sctp_auth_recv_cid(subtype.chunk, asoc) && !chunk->auth) | |
1016 | continue; | |
1017 | ||
1da177e4 LT |
1018 | /* Remember where the last DATA chunk came from so we |
1019 | * know where to send the SACK. | |
1020 | */ | |
1021 | if (sctp_chunk_is_data(chunk)) | |
1022 | asoc->peer.last_data_from = chunk->transport; | |
196d6759 | 1023 | else { |
55e26eb9 | 1024 | SCTP_INC_STATS(net, SCTP_MIB_INCTRLCHUNKS); |
196d6759 MB |
1025 | asoc->stats.ictrlchunks++; |
1026 | if (chunk->chunk_hdr->type == SCTP_CID_SACK) | |
1027 | asoc->stats.isacks++; | |
1028 | } | |
1da177e4 LT |
1029 | |
1030 | if (chunk->transport) | |
e575235f | 1031 | chunk->transport->last_time_heard = ktime_get(); |
1da177e4 LT |
1032 | |
1033 | /* Run through the state machine. */ | |
55e26eb9 | 1034 | error = sctp_do_sm(net, SCTP_EVENT_T_CHUNK, subtype, |
1da177e4 LT |
1035 | state, ep, asoc, chunk, GFP_ATOMIC); |
1036 | ||
1037 | /* Check to see if the association is freed in response to | |
1038 | * the incoming chunk. If so, get out of the while loop. | |
1039 | */ | |
1040 | if (asoc->base.dead) | |
1041 | break; | |
1042 | ||
1043 | /* If there is an error on chunk, discard this packet. */ | |
1044 | if (error && chunk) | |
1045 | chunk->pdiscard = 1; | |
59d8d443 XL |
1046 | |
1047 | if (first_time) | |
1048 | first_time = 0; | |
1da177e4 LT |
1049 | } |
1050 | sctp_association_put(asoc); | |
1051 | } | |
1052 | ||
1053 | /* This routine moves an association from its old sk to a new sk. */ | |
1054 | void sctp_assoc_migrate(struct sctp_association *assoc, struct sock *newsk) | |
1055 | { | |
1056 | struct sctp_sock *newsp = sctp_sk(newsk); | |
1057 | struct sock *oldsk = assoc->base.sk; | |
1058 | ||
1059 | /* Delete the association from the old endpoint's list of | |
1060 | * associations. | |
1061 | */ | |
1062 | list_del_init(&assoc->asocs); | |
1063 | ||
1064 | /* Decrement the backlog value for a TCP-style socket. */ | |
1065 | if (sctp_style(oldsk, TCP)) | |
7976a11b | 1066 | sk_acceptq_removed(oldsk); |
1da177e4 LT |
1067 | |
1068 | /* Release references to the old endpoint and the sock. */ | |
1069 | sctp_endpoint_put(assoc->ep); | |
1070 | sock_put(assoc->base.sk); | |
1071 | ||
1072 | /* Get a reference to the new endpoint. */ | |
1073 | assoc->ep = newsp->ep; | |
1074 | sctp_endpoint_hold(assoc->ep); | |
1075 | ||
1076 | /* Get a reference to the new sock. */ | |
1077 | assoc->base.sk = newsk; | |
1078 | sock_hold(assoc->base.sk); | |
1079 | ||
1080 | /* Add the association to the new endpoint's list of associations. */ | |
1081 | sctp_endpoint_add_asoc(newsp->ep, assoc); | |
1082 | } | |
1083 | ||
1084 | /* Update an association (possibly from unexpected COOKIE-ECHO processing). */ | |
5ee8aa68 XL |
1085 | int sctp_assoc_update(struct sctp_association *asoc, |
1086 | struct sctp_association *new) | |
1da177e4 LT |
1087 | { |
1088 | struct sctp_transport *trans; | |
1089 | struct list_head *pos, *temp; | |
1090 | ||
1091 | /* Copy in new parameters of peer. */ | |
1092 | asoc->c = new->c; | |
1093 | asoc->peer.rwnd = new->peer.rwnd; | |
1094 | asoc->peer.sack_needed = new->peer.sack_needed; | |
1be9a950 | 1095 | asoc->peer.auth_capable = new->peer.auth_capable; |
1da177e4 | 1096 | asoc->peer.i = new->peer.i; |
5ee8aa68 XL |
1097 | |
1098 | if (!sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_INITIAL, | |
1099 | asoc->peer.i.initial_tsn, GFP_ATOMIC)) | |
1100 | return -ENOMEM; | |
1da177e4 LT |
1101 | |
1102 | /* Remove any peer addresses not present in the new association. */ | |
1103 | list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) { | |
1104 | trans = list_entry(pos, struct sctp_transport, transports); | |
0c42749c VY |
1105 | if (!sctp_assoc_lookup_paddr(new, &trans->ipaddr)) { |
1106 | sctp_assoc_rm_peer(asoc, trans); | |
1107 | continue; | |
1108 | } | |
749bf921 VY |
1109 | |
1110 | if (asoc->state >= SCTP_STATE_ESTABLISHED) | |
1111 | sctp_transport_reset(trans); | |
1da177e4 LT |
1112 | } |
1113 | ||
1114 | /* If the case is A (association restart), use | |
1115 | * initial_tsn as next_tsn. If the case is B, use | |
1116 | * current next_tsn in case data sent to peer | |
1117 | * has been discarded and needs retransmission. | |
1118 | */ | |
1119 | if (asoc->state >= SCTP_STATE_ESTABLISHED) { | |
1120 | asoc->next_tsn = new->next_tsn; | |
1121 | asoc->ctsn_ack_point = new->ctsn_ack_point; | |
1122 | asoc->adv_peer_ack_point = new->adv_peer_ack_point; | |
1123 | ||
1124 | /* Reinitialize SSN for both local streams | |
1125 | * and peer's streams. | |
1126 | */ | |
cee360ab | 1127 | sctp_stream_clear(&asoc->stream); |
1da177e4 | 1128 | |
0b58a811 VY |
1129 | /* Flush the ULP reassembly and ordered queue. |
1130 | * Any data there will now be stale and will | |
1131 | * cause problems. | |
1132 | */ | |
1133 | sctp_ulpq_flush(&asoc->ulpq); | |
1134 | ||
749bf921 VY |
1135 | /* reset the overall association error count so |
1136 | * that the restarted association doesn't get torn | |
1137 | * down on the next retransmission timer. | |
1138 | */ | |
1139 | asoc->overall_error_count = 0; | |
1140 | ||
1da177e4 LT |
1141 | } else { |
1142 | /* Add any peer addresses from the new association. */ | |
9dbc15f0 | 1143 | list_for_each_entry(trans, &new->peer.transport_addr_list, |
5ee8aa68 | 1144 | transports) |
2222a780 | 1145 | if (!sctp_assoc_add_peer(asoc, &trans->ipaddr, |
5ee8aa68 XL |
1146 | GFP_ATOMIC, trans->state)) |
1147 | return -ENOMEM; | |
1da177e4 LT |
1148 | |
1149 | asoc->ctsn_ack_point = asoc->next_tsn - 1; | |
1150 | asoc->adv_peer_ack_point = asoc->ctsn_ack_point; | |
3ab21379 | 1151 | |
cee360ab XL |
1152 | if (sctp_state(asoc, COOKIE_WAIT)) |
1153 | sctp_stream_update(&asoc->stream, &new->stream); | |
07d93967 | 1154 | |
4abf5a65 | 1155 | /* get a new assoc id if we don't have one yet. */ |
5ee8aa68 XL |
1156 | if (sctp_assoc_set_id(asoc, GFP_ATOMIC)) |
1157 | return -ENOMEM; | |
1da177e4 | 1158 | } |
a29a5bd4 | 1159 | |
9d2c881a | 1160 | /* SCTP-AUTH: Save the peer parameters from the new associations |
730fc3d0 VY |
1161 | * and also move the association shared keys over |
1162 | */ | |
1163 | kfree(asoc->peer.peer_random); | |
1164 | asoc->peer.peer_random = new->peer.peer_random; | |
1165 | new->peer.peer_random = NULL; | |
1166 | ||
1167 | kfree(asoc->peer.peer_chunks); | |
1168 | asoc->peer.peer_chunks = new->peer.peer_chunks; | |
1169 | new->peer.peer_chunks = NULL; | |
1170 | ||
1171 | kfree(asoc->peer.peer_hmacs); | |
1172 | asoc->peer.peer_hmacs = new->peer.peer_hmacs; | |
1173 | new->peer.peer_hmacs = NULL; | |
1174 | ||
5ee8aa68 | 1175 | return sctp_auth_asoc_init_active_key(asoc, GFP_ATOMIC); |
1da177e4 LT |
1176 | } |
1177 | ||
1178 | /* Update the retran path for sending a retransmitted packet. | |
4c47af4d DB |
1179 | * See also RFC4960, 6.4. Multi-Homed SCTP Endpoints: |
1180 | * | |
1181 | * When there is outbound data to send and the primary path | |
1182 | * becomes inactive (e.g., due to failures), or where the | |
1183 | * SCTP user explicitly requests to send data to an | |
1184 | * inactive destination transport address, before reporting | |
1185 | * an error to its ULP, the SCTP endpoint should try to send | |
1186 | * the data to an alternate active destination transport | |
1187 | * address if one exists. | |
1188 | * | |
1189 | * When retransmitting data that timed out, if the endpoint | |
1190 | * is multihomed, it should consider each source-destination | |
1191 | * address pair in its retransmission selection policy. | |
1192 | * When retransmitting timed-out data, the endpoint should | |
1193 | * attempt to pick the most divergent source-destination | |
1194 | * pair from the original source-destination pair to which | |
1195 | * the packet was transmitted. | |
1196 | * | |
1197 | * Note: Rules for picking the most divergent source-destination | |
1198 | * pair are an implementation decision and are not specified | |
1199 | * within this document. | |
1200 | * | |
1201 | * Our basic strategy is to round-robin transports in priorities | |
2103d6b8 | 1202 | * according to sctp_trans_score() e.g., if no such |
4c47af4d DB |
1203 | * transport with state SCTP_ACTIVE exists, round-robin through |
1204 | * SCTP_UNKNOWN, etc. You get the picture. | |
1da177e4 | 1205 | */ |
4c47af4d | 1206 | static u8 sctp_trans_score(const struct sctp_transport *trans) |
1da177e4 | 1207 | { |
2103d6b8 DV |
1208 | switch (trans->state) { |
1209 | case SCTP_ACTIVE: | |
1210 | return 3; /* best case */ | |
1211 | case SCTP_UNKNOWN: | |
1212 | return 2; | |
1213 | case SCTP_PF: | |
1214 | return 1; | |
1215 | default: /* case SCTP_INACTIVE */ | |
1216 | return 0; /* worst case */ | |
1217 | } | |
4c47af4d | 1218 | } |
1da177e4 | 1219 | |
a7288c4d DB |
1220 | static struct sctp_transport *sctp_trans_elect_tie(struct sctp_transport *trans1, |
1221 | struct sctp_transport *trans2) | |
1222 | { | |
1223 | if (trans1->error_count > trans2->error_count) { | |
1224 | return trans2; | |
1225 | } else if (trans1->error_count == trans2->error_count && | |
1226 | ktime_after(trans2->last_time_heard, | |
1227 | trans1->last_time_heard)) { | |
1228 | return trans2; | |
1229 | } else { | |
1230 | return trans1; | |
1231 | } | |
1232 | } | |
1233 | ||
4c47af4d DB |
1234 | static struct sctp_transport *sctp_trans_elect_best(struct sctp_transport *curr, |
1235 | struct sctp_transport *best) | |
1236 | { | |
a7288c4d DB |
1237 | u8 score_curr, score_best; |
1238 | ||
ea4f19c1 | 1239 | if (best == NULL || curr == best) |
4c47af4d | 1240 | return curr; |
4141ddc0 | 1241 | |
a7288c4d DB |
1242 | score_curr = sctp_trans_score(curr); |
1243 | score_best = sctp_trans_score(best); | |
1244 | ||
1245 | /* First, try a score-based selection if both transport states | |
1246 | * differ. If we're in a tie, lets try to make a more clever | |
1247 | * decision here based on error counts and last time heard. | |
1248 | */ | |
1249 | if (score_curr > score_best) | |
1250 | return curr; | |
1251 | else if (score_curr == score_best) | |
39d2adeb | 1252 | return sctp_trans_elect_tie(best, curr); |
a7288c4d DB |
1253 | else |
1254 | return best; | |
4c47af4d | 1255 | } |
1da177e4 | 1256 | |
4c47af4d DB |
1257 | void sctp_assoc_update_retran_path(struct sctp_association *asoc) |
1258 | { | |
1259 | struct sctp_transport *trans = asoc->peer.retran_path; | |
1260 | struct sctp_transport *trans_next = NULL; | |
1da177e4 | 1261 | |
4c47af4d DB |
1262 | /* We're done as we only have the one and only path. */ |
1263 | if (asoc->peer.transport_count == 1) | |
1264 | return; | |
1265 | /* If active_path and retran_path are the same and active, | |
1266 | * then this is the only active path. Use it. | |
1267 | */ | |
1268 | if (asoc->peer.active_path == asoc->peer.retran_path && | |
1269 | asoc->peer.active_path->state == SCTP_ACTIVE) | |
1270 | return; | |
1da177e4 | 1271 | |
4c47af4d DB |
1272 | /* Iterate from retran_path's successor back to retran_path. */ |
1273 | for (trans = list_next_entry(trans, transports); 1; | |
1274 | trans = list_next_entry(trans, transports)) { | |
1275 | /* Manually skip the head element. */ | |
1276 | if (&trans->transports == &asoc->peer.transport_addr_list) | |
1277 | continue; | |
1278 | if (trans->state == SCTP_UNCONFIRMED) | |
1279 | continue; | |
1280 | trans_next = sctp_trans_elect_best(trans, trans_next); | |
1281 | /* Active is good enough for immediate return. */ | |
1282 | if (trans_next->state == SCTP_ACTIVE) | |
4141ddc0 | 1283 | break; |
4c47af4d DB |
1284 | /* We've reached the end, time to update path. */ |
1285 | if (trans == asoc->peer.retran_path) | |
1da177e4 | 1286 | break; |
1da177e4 LT |
1287 | } |
1288 | ||
433131ba | 1289 | asoc->peer.retran_path = trans_next; |
3f7a87d2 | 1290 | |
4c47af4d DB |
1291 | pr_debug("%s: association:%p updated new path to addr:%pISpc\n", |
1292 | __func__, asoc, &asoc->peer.retran_path->ipaddr.sa); | |
3f7a87d2 FF |
1293 | } |
1294 | ||
b82e8f31 DB |
1295 | static void sctp_select_active_and_retran_path(struct sctp_association *asoc) |
1296 | { | |
1297 | struct sctp_transport *trans, *trans_pri = NULL, *trans_sec = NULL; | |
a7288c4d | 1298 | struct sctp_transport *trans_pf = NULL; |
b82e8f31 DB |
1299 | |
1300 | /* Look for the two most recently used active transports. */ | |
1301 | list_for_each_entry(trans, &asoc->peer.transport_addr_list, | |
1302 | transports) { | |
a7288c4d | 1303 | /* Skip uninteresting transports. */ |
b82e8f31 | 1304 | if (trans->state == SCTP_INACTIVE || |
a7288c4d | 1305 | trans->state == SCTP_UNCONFIRMED) |
b82e8f31 | 1306 | continue; |
a7288c4d DB |
1307 | /* Keep track of the best PF transport from our |
1308 | * list in case we don't find an active one. | |
1309 | */ | |
1310 | if (trans->state == SCTP_PF) { | |
1311 | trans_pf = sctp_trans_elect_best(trans, trans_pf); | |
1312 | continue; | |
1313 | } | |
1314 | /* For active transports, pick the most recent ones. */ | |
b82e8f31 | 1315 | if (trans_pri == NULL || |
e575235f DB |
1316 | ktime_after(trans->last_time_heard, |
1317 | trans_pri->last_time_heard)) { | |
b82e8f31 DB |
1318 | trans_sec = trans_pri; |
1319 | trans_pri = trans; | |
1320 | } else if (trans_sec == NULL || | |
e575235f DB |
1321 | ktime_after(trans->last_time_heard, |
1322 | trans_sec->last_time_heard)) { | |
b82e8f31 DB |
1323 | trans_sec = trans; |
1324 | } | |
1325 | } | |
1326 | ||
1327 | /* RFC 2960 6.4 Multi-Homed SCTP Endpoints | |
1328 | * | |
1329 | * By default, an endpoint should always transmit to the primary | |
1330 | * path, unless the SCTP user explicitly specifies the | |
1331 | * destination transport address (and possibly source transport | |
1332 | * address) to use. [If the primary is active but not most recent, | |
1333 | * bump the most recently used transport.] | |
1334 | */ | |
1335 | if ((asoc->peer.primary_path->state == SCTP_ACTIVE || | |
1336 | asoc->peer.primary_path->state == SCTP_UNKNOWN) && | |
1337 | asoc->peer.primary_path != trans_pri) { | |
1338 | trans_sec = trans_pri; | |
1339 | trans_pri = asoc->peer.primary_path; | |
1340 | } | |
1341 | ||
1342 | /* We did not find anything useful for a possible retransmission | |
5e80a0cc | 1343 | * path; either primary path that we found is the same as |
b82e8f31 DB |
1344 | * the current one, or we didn't generally find an active one. |
1345 | */ | |
1346 | if (trans_sec == NULL) | |
1347 | trans_sec = trans_pri; | |
1348 | ||
1349 | /* If we failed to find a usable transport, just camp on the | |
aa4a83ee | 1350 | * active or pick a PF iff it's the better choice. |
b82e8f31 DB |
1351 | */ |
1352 | if (trans_pri == NULL) { | |
aa4a83ee DB |
1353 | trans_pri = sctp_trans_elect_best(asoc->peer.active_path, trans_pf); |
1354 | trans_sec = trans_pri; | |
b82e8f31 DB |
1355 | } |
1356 | ||
1357 | /* Set the active and retran transports. */ | |
1358 | asoc->peer.active_path = trans_pri; | |
1359 | asoc->peer.retran_path = trans_sec; | |
1360 | } | |
1361 | ||
4c47af4d DB |
1362 | struct sctp_transport * |
1363 | sctp_assoc_choose_alter_transport(struct sctp_association *asoc, | |
1364 | struct sctp_transport *last_sent_to) | |
3f7a87d2 | 1365 | { |
9919b455 WY |
1366 | /* If this is the first time packet is sent, use the active path, |
1367 | * else use the retran path. If the last packet was sent over the | |
3f7a87d2 FF |
1368 | * retran path, update the retran path and use it. |
1369 | */ | |
4c47af4d | 1370 | if (last_sent_to == NULL) { |
1da177e4 | 1371 | return asoc->peer.active_path; |
4c47af4d | 1372 | } else { |
9919b455 | 1373 | if (last_sent_to == asoc->peer.retran_path) |
1da177e4 | 1374 | sctp_assoc_update_retran_path(asoc); |
4c47af4d | 1375 | |
1da177e4 LT |
1376 | return asoc->peer.retran_path; |
1377 | } | |
1da177e4 LT |
1378 | } |
1379 | ||
2f5e3c9d MRL |
1380 | void sctp_assoc_update_frag_point(struct sctp_association *asoc) |
1381 | { | |
1382 | int frag = sctp_mtu_payload(sctp_sk(asoc->base.sk), asoc->pathmtu, | |
1383 | sctp_datachk_len(&asoc->stream)); | |
1384 | ||
1385 | if (asoc->user_frag) | |
1386 | frag = min_t(int, frag, asoc->user_frag); | |
1387 | ||
1388 | frag = min_t(int, frag, SCTP_MAX_CHUNK_LEN - | |
1389 | sctp_datachk_len(&asoc->stream)); | |
1390 | ||
1391 | asoc->frag_point = SCTP_TRUNC4(frag); | |
1392 | } | |
1393 | ||
c4b2893d MRL |
1394 | void sctp_assoc_set_pmtu(struct sctp_association *asoc, __u32 pmtu) |
1395 | { | |
2f5e3c9d | 1396 | if (asoc->pathmtu != pmtu) { |
c4b2893d | 1397 | asoc->pathmtu = pmtu; |
2f5e3c9d MRL |
1398 | sctp_assoc_update_frag_point(asoc); |
1399 | } | |
c4b2893d MRL |
1400 | |
1401 | pr_debug("%s: asoc:%p, pmtu:%d, frag_point:%d\n", __func__, asoc, | |
1402 | asoc->pathmtu, asoc->frag_point); | |
1403 | } | |
1404 | ||
1da177e4 LT |
1405 | /* Update the association's pmtu and frag_point by going through all the |
1406 | * transports. This routine is called when a transport's PMTU has changed. | |
1407 | */ | |
3ebfdf08 | 1408 | void sctp_assoc_sync_pmtu(struct sctp_association *asoc) |
1da177e4 LT |
1409 | { |
1410 | struct sctp_transport *t; | |
1da177e4 LT |
1411 | __u32 pmtu = 0; |
1412 | ||
1413 | if (!asoc) | |
1414 | return; | |
1415 | ||
1416 | /* Get the lowest pmtu of all the transports. */ | |
6ff0f871 | 1417 | list_for_each_entry(t, &asoc->peer.transport_addr_list, transports) { |
8a479491 | 1418 | if (t->pmtu_pending && t->dst) { |
d805397c XL |
1419 | sctp_transport_update_pmtu(t, |
1420 | atomic_read(&t->mtu_info)); | |
8a479491 VY |
1421 | t->pmtu_pending = 0; |
1422 | } | |
52ccb8e9 FF |
1423 | if (!pmtu || (t->pathmtu < pmtu)) |
1424 | pmtu = t->pathmtu; | |
1da177e4 LT |
1425 | } |
1426 | ||
c4b2893d | 1427 | sctp_assoc_set_pmtu(asoc, pmtu); |
1da177e4 LT |
1428 | } |
1429 | ||
1430 | /* Should we send a SACK to update our peer? */ | |
ce4a03db | 1431 | static inline bool sctp_peer_needs_update(struct sctp_association *asoc) |
1da177e4 | 1432 | { |
4e7696d9 XL |
1433 | struct net *net = asoc->base.net; |
1434 | ||
1da177e4 LT |
1435 | switch (asoc->state) { |
1436 | case SCTP_STATE_ESTABLISHED: | |
1437 | case SCTP_STATE_SHUTDOWN_PENDING: | |
1438 | case SCTP_STATE_SHUTDOWN_RECEIVED: | |
1439 | case SCTP_STATE_SHUTDOWN_SENT: | |
1440 | if ((asoc->rwnd > asoc->a_rwnd) && | |
90f2f531 | 1441 | ((asoc->rwnd - asoc->a_rwnd) >= max_t(__u32, |
e1fc3b14 | 1442 | (asoc->base.sk->sk_rcvbuf >> net->sctp.rwnd_upd_shift), |
90f2f531 | 1443 | asoc->pathmtu))) |
ce4a03db | 1444 | return true; |
1da177e4 LT |
1445 | break; |
1446 | default: | |
1447 | break; | |
1448 | } | |
ce4a03db | 1449 | return false; |
1da177e4 LT |
1450 | } |
1451 | ||
362d5204 DB |
1452 | /* Increase asoc's rwnd by len and send any window update SACK if needed. */ |
1453 | void sctp_assoc_rwnd_increase(struct sctp_association *asoc, unsigned int len) | |
1da177e4 LT |
1454 | { |
1455 | struct sctp_chunk *sack; | |
1456 | struct timer_list *timer; | |
1457 | ||
362d5204 DB |
1458 | if (asoc->rwnd_over) { |
1459 | if (asoc->rwnd_over >= len) { | |
1460 | asoc->rwnd_over -= len; | |
1461 | } else { | |
1462 | asoc->rwnd += (len - asoc->rwnd_over); | |
1463 | asoc->rwnd_over = 0; | |
1464 | } | |
1465 | } else { | |
1466 | asoc->rwnd += len; | |
1467 | } | |
1da177e4 | 1468 | |
362d5204 DB |
1469 | /* If we had window pressure, start recovering it |
1470 | * once our rwnd had reached the accumulated pressure | |
1471 | * threshold. The idea is to recover slowly, but up | |
1472 | * to the initial advertised window. | |
1473 | */ | |
1636098c | 1474 | if (asoc->rwnd_press) { |
362d5204 DB |
1475 | int change = min(asoc->pathmtu, asoc->rwnd_press); |
1476 | asoc->rwnd += change; | |
1477 | asoc->rwnd_press -= change; | |
1478 | } | |
4d3c46e6 | 1479 | |
362d5204 DB |
1480 | pr_debug("%s: asoc:%p rwnd increased by %d to (%u, %u) - %u\n", |
1481 | __func__, asoc, len, asoc->rwnd, asoc->rwnd_over, | |
1482 | asoc->a_rwnd); | |
1da177e4 LT |
1483 | |
1484 | /* Send a window update SACK if the rwnd has increased by at least the | |
1485 | * minimum of the association's PMTU and half of the receive buffer. | |
1486 | * The algorithm used is similar to the one described in | |
1487 | * Section 4.2.3.3 of RFC 1122. | |
1488 | */ | |
362d5204 | 1489 | if (sctp_peer_needs_update(asoc)) { |
1da177e4 | 1490 | asoc->a_rwnd = asoc->rwnd; |
bb33381d DB |
1491 | |
1492 | pr_debug("%s: sending window update SACK- asoc:%p rwnd:%u " | |
1493 | "a_rwnd:%u\n", __func__, asoc, asoc->rwnd, | |
1494 | asoc->a_rwnd); | |
1495 | ||
1da177e4 LT |
1496 | sack = sctp_make_sack(asoc); |
1497 | if (!sack) | |
1498 | return; | |
1499 | ||
1500 | asoc->peer.sack_needed = 0; | |
1501 | ||
cea8768f | 1502 | sctp_outq_tail(&asoc->outqueue, sack, GFP_ATOMIC); |
1da177e4 LT |
1503 | |
1504 | /* Stop the SACK timer. */ | |
1505 | timer = &asoc->timers[SCTP_EVENT_TIMEOUT_SACK]; | |
8fa7292f | 1506 | if (timer_delete(timer)) |
1da177e4 LT |
1507 | sctp_association_put(asoc); |
1508 | } | |
1509 | } | |
1510 | ||
362d5204 DB |
1511 | /* Decrease asoc's rwnd by len. */ |
1512 | void sctp_assoc_rwnd_decrease(struct sctp_association *asoc, unsigned int len) | |
1513 | { | |
1514 | int rx_count; | |
1515 | int over = 0; | |
1516 | ||
1517 | if (unlikely(!asoc->rwnd || asoc->rwnd_over)) | |
1518 | pr_debug("%s: association:%p has asoc->rwnd:%u, " | |
1519 | "asoc->rwnd_over:%u!\n", __func__, asoc, | |
1520 | asoc->rwnd, asoc->rwnd_over); | |
1521 | ||
1522 | if (asoc->ep->rcvbuf_policy) | |
1523 | rx_count = atomic_read(&asoc->rmem_alloc); | |
1524 | else | |
1525 | rx_count = atomic_read(&asoc->base.sk->sk_rmem_alloc); | |
1526 | ||
1527 | /* If we've reached or overflowed our receive buffer, announce | |
1528 | * a 0 rwnd if rwnd would still be positive. Store the | |
5e80a0cc | 1529 | * potential pressure overflow so that the window can be restored |
362d5204 DB |
1530 | * back to original value. |
1531 | */ | |
1532 | if (rx_count >= asoc->base.sk->sk_rcvbuf) | |
1533 | over = 1; | |
1534 | ||
1535 | if (asoc->rwnd >= len) { | |
1536 | asoc->rwnd -= len; | |
1537 | if (over) { | |
1538 | asoc->rwnd_press += asoc->rwnd; | |
1539 | asoc->rwnd = 0; | |
1540 | } | |
1541 | } else { | |
58b94d88 | 1542 | asoc->rwnd_over += len - asoc->rwnd; |
362d5204 DB |
1543 | asoc->rwnd = 0; |
1544 | } | |
1545 | ||
1546 | pr_debug("%s: asoc:%p rwnd decreased by %d to (%u, %u, %u)\n", | |
1547 | __func__, asoc, len, asoc->rwnd, asoc->rwnd_over, | |
1548 | asoc->rwnd_press); | |
1549 | } | |
1da177e4 LT |
1550 | |
1551 | /* Build the bind address list for the association based on info from the | |
1552 | * local endpoint and the remote peer. | |
1553 | */ | |
3182cd84 | 1554 | int sctp_assoc_set_bind_addr_from_ep(struct sctp_association *asoc, |
1c662018 | 1555 | enum sctp_scope scope, gfp_t gfp) |
1da177e4 | 1556 | { |
471e39df | 1557 | struct sock *sk = asoc->base.sk; |
1da177e4 LT |
1558 | int flags; |
1559 | ||
1560 | /* Use scoping rules to determine the subset of addresses from | |
1561 | * the endpoint. | |
1562 | */ | |
471e39df MRL |
1563 | flags = (PF_INET6 == sk->sk_family) ? SCTP_ADDR6_ALLOWED : 0; |
1564 | if (!inet_v6_ipv6only(sk)) | |
1565 | flags |= SCTP_ADDR4_ALLOWED; | |
1da177e4 LT |
1566 | if (asoc->peer.ipv4_address) |
1567 | flags |= SCTP_ADDR4_PEERSUPP; | |
1568 | if (asoc->peer.ipv6_address) | |
1569 | flags |= SCTP_ADDR6_PEERSUPP; | |
1570 | ||
4e7696d9 | 1571 | return sctp_bind_addr_copy(asoc->base.net, |
4db67e80 | 1572 | &asoc->base.bind_addr, |
1da177e4 LT |
1573 | &asoc->ep->base.bind_addr, |
1574 | scope, gfp, flags); | |
1575 | } | |
1576 | ||
1577 | /* Build the association's bind address list from the cookie. */ | |
1578 | int sctp_assoc_set_bind_addr_from_cookie(struct sctp_association *asoc, | |
3182cd84 | 1579 | struct sctp_cookie *cookie, |
dd0fc66f | 1580 | gfp_t gfp) |
1da177e4 | 1581 | { |
f97278ff XL |
1582 | struct sctp_init_chunk *peer_init = (struct sctp_init_chunk *)(cookie + 1); |
1583 | int var_size2 = ntohs(peer_init->chunk_hdr.length); | |
1da177e4 | 1584 | int var_size3 = cookie->raw_addr_list_len; |
f97278ff | 1585 | __u8 *raw = (__u8 *)peer_init + var_size2; |
1da177e4 LT |
1586 | |
1587 | return sctp_raw_to_bind_addrs(&asoc->base.bind_addr, raw, var_size3, | |
1588 | asoc->ep->base.bind_addr.port, gfp); | |
1589 | } | |
1590 | ||
d808ad9a YH |
1591 | /* Lookup laddr in the bind address list of an association. */ |
1592 | int sctp_assoc_lookup_laddr(struct sctp_association *asoc, | |
1da177e4 LT |
1593 | const union sctp_addr *laddr) |
1594 | { | |
559cf710 | 1595 | int found = 0; |
1da177e4 | 1596 | |
1da177e4 LT |
1597 | if ((asoc->base.bind_addr.port == ntohs(laddr->v4.sin_port)) && |
1598 | sctp_bind_addr_match(&asoc->base.bind_addr, laddr, | |
559cf710 | 1599 | sctp_sk(asoc->base.sk))) |
1da177e4 | 1600 | found = 1; |
1da177e4 | 1601 | |
1da177e4 LT |
1602 | return found; |
1603 | } | |
07d93967 VY |
1604 | |
1605 | /* Set an association id for a given association */ | |
1606 | int sctp_assoc_set_id(struct sctp_association *asoc, gfp_t gfp) | |
1607 | { | |
d0164adc | 1608 | bool preload = gfpflags_allow_blocking(gfp); |
94960e8c | 1609 | int ret; |
c6ba68a2 VY |
1610 | |
1611 | /* If the id is already assigned, keep it. */ | |
1612 | if (asoc->assoc_id) | |
94960e8c | 1613 | return 0; |
07d93967 | 1614 | |
94960e8c TH |
1615 | if (preload) |
1616 | idr_preload(gfp); | |
07d93967 | 1617 | spin_lock_bh(&sctp_assocs_id_lock); |
80df2704 XL |
1618 | /* 0, 1, 2 are used as SCTP_FUTURE_ASSOC, SCTP_CURRENT_ASSOC and |
1619 | * SCTP_ALL_ASSOC, so an available id must be > SCTP_ALL_ASSOC. | |
1620 | */ | |
1621 | ret = idr_alloc_cyclic(&sctp_assocs_id, asoc, SCTP_ALL_ASSOC + 1, 0, | |
1622 | GFP_NOWAIT); | |
07d93967 | 1623 | spin_unlock_bh(&sctp_assocs_id_lock); |
94960e8c TH |
1624 | if (preload) |
1625 | idr_preload_end(); | |
1626 | if (ret < 0) | |
1627 | return ret; | |
07d93967 | 1628 | |
94960e8c TH |
1629 | asoc->assoc_id = (sctp_assoc_t)ret; |
1630 | return 0; | |
07d93967 | 1631 | } |
a08de64d | 1632 | |
8b4472cc WY |
1633 | /* Free the ASCONF queue */ |
1634 | static void sctp_assoc_free_asconf_queue(struct sctp_association *asoc) | |
1635 | { | |
1636 | struct sctp_chunk *asconf; | |
1637 | struct sctp_chunk *tmp; | |
1638 | ||
1639 | list_for_each_entry_safe(asconf, tmp, &asoc->addip_chunk_list, list) { | |
1640 | list_del_init(&asconf->list); | |
1641 | sctp_chunk_free(asconf); | |
1642 | } | |
1643 | } | |
1644 | ||
a08de64d VY |
1645 | /* Free asconf_ack cache */ |
1646 | static void sctp_assoc_free_asconf_acks(struct sctp_association *asoc) | |
1647 | { | |
1648 | struct sctp_chunk *ack; | |
1649 | struct sctp_chunk *tmp; | |
1650 | ||
1651 | list_for_each_entry_safe(ack, tmp, &asoc->asconf_ack_list, | |
1652 | transmitted_list) { | |
1653 | list_del_init(&ack->transmitted_list); | |
1654 | sctp_chunk_free(ack); | |
1655 | } | |
1656 | } | |
1657 | ||
1658 | /* Clean up the ASCONF_ACK queue */ | |
1659 | void sctp_assoc_clean_asconf_ack_cache(const struct sctp_association *asoc) | |
1660 | { | |
1661 | struct sctp_chunk *ack; | |
1662 | struct sctp_chunk *tmp; | |
1663 | ||
25985edc | 1664 | /* We can remove all the entries from the queue up to |
a08de64d VY |
1665 | * the "Peer-Sequence-Number". |
1666 | */ | |
1667 | list_for_each_entry_safe(ack, tmp, &asoc->asconf_ack_list, | |
1668 | transmitted_list) { | |
1669 | if (ack->subh.addip_hdr->serial == | |
1670 | htonl(asoc->peer.addip_serial)) | |
1671 | break; | |
1672 | ||
1673 | list_del_init(&ack->transmitted_list); | |
1674 | sctp_chunk_free(ack); | |
1675 | } | |
1676 | } | |
1677 | ||
1678 | /* Find the ASCONF_ACK whose serial number matches ASCONF */ | |
1679 | struct sctp_chunk *sctp_assoc_lookup_asconf_ack( | |
1680 | const struct sctp_association *asoc, | |
1681 | __be32 serial) | |
1682 | { | |
a8699814 | 1683 | struct sctp_chunk *ack; |
a08de64d VY |
1684 | |
1685 | /* Walk through the list of cached ASCONF-ACKs and find the | |
1686 | * ack chunk whose serial number matches that of the request. | |
1687 | */ | |
1688 | list_for_each_entry(ack, &asoc->asconf_ack_list, transmitted_list) { | |
b69040d8 DB |
1689 | if (sctp_chunk_pending(ack)) |
1690 | continue; | |
a08de64d VY |
1691 | if (ack->subh.addip_hdr->serial == serial) { |
1692 | sctp_chunk_hold(ack); | |
a8699814 | 1693 | return ack; |
a08de64d VY |
1694 | } |
1695 | } | |
1696 | ||
a8699814 | 1697 | return NULL; |
a08de64d | 1698 | } |
a000c01e WY |
1699 | |
1700 | void sctp_asconf_queue_teardown(struct sctp_association *asoc) | |
1701 | { | |
1702 | /* Free any cached ASCONF_ACK chunk. */ | |
1703 | sctp_assoc_free_asconf_acks(asoc); | |
1704 | ||
1705 | /* Free the ASCONF queue. */ | |
1706 | sctp_assoc_free_asconf_queue(asoc); | |
1707 | ||
1708 | /* Free any cached ASCONF chunk. */ | |
1709 | if (asoc->addip_last_asconf) | |
1710 | sctp_chunk_free(asoc->addip_last_asconf); | |
1711 | } |