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