1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /* SCTP kernel implementation
3*4882a593Smuzhiyun * Copyright (c) 1999-2000 Cisco, Inc.
4*4882a593Smuzhiyun * Copyright (c) 1999-2001 Motorola, Inc.
5*4882a593Smuzhiyun * Copyright (c) 2001-2002 International Business Machines, Corp.
6*4882a593Smuzhiyun * Copyright (c) 2001 Intel Corp.
7*4882a593Smuzhiyun * Copyright (c) 2001 Nokia, Inc.
8*4882a593Smuzhiyun * Copyright (c) 2001 La Monte H.P. Yarroll
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * This file is part of the SCTP kernel implementation
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * This abstraction represents an SCTP endpoint.
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * Please send any bug reports or fixes you make to the
15*4882a593Smuzhiyun * email address(es):
16*4882a593Smuzhiyun * lksctp developers <linux-sctp@vger.kernel.org>
17*4882a593Smuzhiyun *
18*4882a593Smuzhiyun * Written or modified by:
19*4882a593Smuzhiyun * La Monte H.P. Yarroll <piggy@acm.org>
20*4882a593Smuzhiyun * Karl Knutson <karl@athena.chicago.il.us>
21*4882a593Smuzhiyun * Jon Grimm <jgrimm@austin.ibm.com>
22*4882a593Smuzhiyun * Daisy Chang <daisyc@us.ibm.com>
23*4882a593Smuzhiyun * Dajiang Zhang <dajiang.zhang@nokia.com>
24*4882a593Smuzhiyun */
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun #include <linux/types.h>
27*4882a593Smuzhiyun #include <linux/slab.h>
28*4882a593Smuzhiyun #include <linux/in.h>
29*4882a593Smuzhiyun #include <linux/random.h> /* get_random_bytes() */
30*4882a593Smuzhiyun #include <net/sock.h>
31*4882a593Smuzhiyun #include <net/ipv6.h>
32*4882a593Smuzhiyun #include <net/sctp/sctp.h>
33*4882a593Smuzhiyun #include <net/sctp/sm.h>
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun /* Forward declarations for internal helpers. */
36*4882a593Smuzhiyun static void sctp_endpoint_bh_rcv(struct work_struct *work);
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun /*
39*4882a593Smuzhiyun * Initialize the base fields of the endpoint structure.
40*4882a593Smuzhiyun */
sctp_endpoint_init(struct sctp_endpoint * ep,struct sock * sk,gfp_t gfp)41*4882a593Smuzhiyun static struct sctp_endpoint *sctp_endpoint_init(struct sctp_endpoint *ep,
42*4882a593Smuzhiyun struct sock *sk,
43*4882a593Smuzhiyun gfp_t gfp)
44*4882a593Smuzhiyun {
45*4882a593Smuzhiyun struct net *net = sock_net(sk);
46*4882a593Smuzhiyun struct sctp_shared_key *null_key;
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun ep->digest = kzalloc(SCTP_SIGNATURE_SIZE, gfp);
49*4882a593Smuzhiyun if (!ep->digest)
50*4882a593Smuzhiyun return NULL;
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun ep->asconf_enable = net->sctp.addip_enable;
53*4882a593Smuzhiyun ep->auth_enable = net->sctp.auth_enable;
54*4882a593Smuzhiyun if (ep->auth_enable) {
55*4882a593Smuzhiyun if (sctp_auth_init(ep, gfp))
56*4882a593Smuzhiyun goto nomem;
57*4882a593Smuzhiyun if (ep->asconf_enable) {
58*4882a593Smuzhiyun sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF);
59*4882a593Smuzhiyun sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF_ACK);
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun }
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun /* Initialize the base structure. */
64*4882a593Smuzhiyun /* What type of endpoint are we? */
65*4882a593Smuzhiyun ep->base.type = SCTP_EP_TYPE_SOCKET;
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun /* Initialize the basic object fields. */
68*4882a593Smuzhiyun refcount_set(&ep->base.refcnt, 1);
69*4882a593Smuzhiyun ep->base.dead = false;
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun /* Create an input queue. */
72*4882a593Smuzhiyun sctp_inq_init(&ep->base.inqueue);
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun /* Set its top-half handler */
75*4882a593Smuzhiyun sctp_inq_set_th_handler(&ep->base.inqueue, sctp_endpoint_bh_rcv);
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun /* Initialize the bind addr area */
78*4882a593Smuzhiyun sctp_bind_addr_init(&ep->base.bind_addr, 0);
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun /* Create the lists of associations. */
81*4882a593Smuzhiyun INIT_LIST_HEAD(&ep->asocs);
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun /* Use SCTP specific send buffer space queues. */
84*4882a593Smuzhiyun ep->sndbuf_policy = net->sctp.sndbuf_policy;
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun sk->sk_data_ready = sctp_data_ready;
87*4882a593Smuzhiyun sk->sk_write_space = sctp_write_space;
88*4882a593Smuzhiyun sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun /* Get the receive buffer policy for this endpoint */
91*4882a593Smuzhiyun ep->rcvbuf_policy = net->sctp.rcvbuf_policy;
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun /* Initialize the secret key used with cookie. */
94*4882a593Smuzhiyun get_random_bytes(ep->secret_key, sizeof(ep->secret_key));
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun /* SCTP-AUTH extensions*/
97*4882a593Smuzhiyun INIT_LIST_HEAD(&ep->endpoint_shared_keys);
98*4882a593Smuzhiyun null_key = sctp_auth_shkey_create(0, gfp);
99*4882a593Smuzhiyun if (!null_key)
100*4882a593Smuzhiyun goto nomem_shkey;
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun list_add(&null_key->key_list, &ep->endpoint_shared_keys);
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun /* Add the null key to the endpoint shared keys list and
105*4882a593Smuzhiyun * set the hmcas and chunks pointers.
106*4882a593Smuzhiyun */
107*4882a593Smuzhiyun ep->prsctp_enable = net->sctp.prsctp_enable;
108*4882a593Smuzhiyun ep->reconf_enable = net->sctp.reconf_enable;
109*4882a593Smuzhiyun ep->ecn_enable = net->sctp.ecn_enable;
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun /* Remember who we are attached to. */
112*4882a593Smuzhiyun ep->base.sk = sk;
113*4882a593Smuzhiyun ep->base.net = sock_net(sk);
114*4882a593Smuzhiyun sock_hold(ep->base.sk);
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun return ep;
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun nomem_shkey:
119*4882a593Smuzhiyun sctp_auth_free(ep);
120*4882a593Smuzhiyun nomem:
121*4882a593Smuzhiyun kfree(ep->digest);
122*4882a593Smuzhiyun return NULL;
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun /* Create a sctp_endpoint with all that boring stuff initialized.
127*4882a593Smuzhiyun * Returns NULL if there isn't enough memory.
128*4882a593Smuzhiyun */
sctp_endpoint_new(struct sock * sk,gfp_t gfp)129*4882a593Smuzhiyun struct sctp_endpoint *sctp_endpoint_new(struct sock *sk, gfp_t gfp)
130*4882a593Smuzhiyun {
131*4882a593Smuzhiyun struct sctp_endpoint *ep;
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun /* Build a local endpoint. */
134*4882a593Smuzhiyun ep = kzalloc(sizeof(*ep), gfp);
135*4882a593Smuzhiyun if (!ep)
136*4882a593Smuzhiyun goto fail;
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun if (!sctp_endpoint_init(ep, sk, gfp))
139*4882a593Smuzhiyun goto fail_init;
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun SCTP_DBG_OBJCNT_INC(ep);
142*4882a593Smuzhiyun return ep;
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun fail_init:
145*4882a593Smuzhiyun kfree(ep);
146*4882a593Smuzhiyun fail:
147*4882a593Smuzhiyun return NULL;
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun /* Add an association to an endpoint. */
sctp_endpoint_add_asoc(struct sctp_endpoint * ep,struct sctp_association * asoc)151*4882a593Smuzhiyun void sctp_endpoint_add_asoc(struct sctp_endpoint *ep,
152*4882a593Smuzhiyun struct sctp_association *asoc)
153*4882a593Smuzhiyun {
154*4882a593Smuzhiyun struct sock *sk = ep->base.sk;
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun /* If this is a temporary association, don't bother
157*4882a593Smuzhiyun * since we'll be removing it shortly and don't
158*4882a593Smuzhiyun * want anyone to find it anyway.
159*4882a593Smuzhiyun */
160*4882a593Smuzhiyun if (asoc->temp)
161*4882a593Smuzhiyun return;
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun /* Now just add it to our list of asocs */
164*4882a593Smuzhiyun list_add_tail(&asoc->asocs, &ep->asocs);
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun /* Increment the backlog value for a TCP-style listening socket. */
167*4882a593Smuzhiyun if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
168*4882a593Smuzhiyun sk_acceptq_added(sk);
169*4882a593Smuzhiyun }
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun /* Free the endpoint structure. Delay cleanup until
172*4882a593Smuzhiyun * all users have released their reference count on this structure.
173*4882a593Smuzhiyun */
sctp_endpoint_free(struct sctp_endpoint * ep)174*4882a593Smuzhiyun void sctp_endpoint_free(struct sctp_endpoint *ep)
175*4882a593Smuzhiyun {
176*4882a593Smuzhiyun ep->base.dead = true;
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun inet_sk_set_state(ep->base.sk, SCTP_SS_CLOSED);
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun /* Unlink this endpoint, so we can't find it again! */
181*4882a593Smuzhiyun sctp_unhash_endpoint(ep);
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun sctp_endpoint_put(ep);
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun /* Final destructor for endpoint. */
sctp_endpoint_destroy_rcu(struct rcu_head * head)187*4882a593Smuzhiyun static void sctp_endpoint_destroy_rcu(struct rcu_head *head)
188*4882a593Smuzhiyun {
189*4882a593Smuzhiyun struct sctp_endpoint *ep = container_of(head, struct sctp_endpoint, rcu);
190*4882a593Smuzhiyun struct sock *sk = ep->base.sk;
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun sctp_sk(sk)->ep = NULL;
193*4882a593Smuzhiyun sock_put(sk);
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun kfree(ep);
196*4882a593Smuzhiyun SCTP_DBG_OBJCNT_DEC(ep);
197*4882a593Smuzhiyun }
198*4882a593Smuzhiyun
sctp_endpoint_destroy(struct sctp_endpoint * ep)199*4882a593Smuzhiyun static void sctp_endpoint_destroy(struct sctp_endpoint *ep)
200*4882a593Smuzhiyun {
201*4882a593Smuzhiyun struct sock *sk;
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun if (unlikely(!ep->base.dead)) {
204*4882a593Smuzhiyun WARN(1, "Attempt to destroy undead endpoint %p!\n", ep);
205*4882a593Smuzhiyun return;
206*4882a593Smuzhiyun }
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun /* Free the digest buffer */
209*4882a593Smuzhiyun kfree(ep->digest);
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun /* SCTP-AUTH: Free up AUTH releated data such as shared keys
212*4882a593Smuzhiyun * chunks and hmacs arrays that were allocated
213*4882a593Smuzhiyun */
214*4882a593Smuzhiyun sctp_auth_destroy_keys(&ep->endpoint_shared_keys);
215*4882a593Smuzhiyun sctp_auth_free(ep);
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun /* Cleanup. */
218*4882a593Smuzhiyun sctp_inq_free(&ep->base.inqueue);
219*4882a593Smuzhiyun sctp_bind_addr_free(&ep->base.bind_addr);
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun memset(ep->secret_key, 0, sizeof(ep->secret_key));
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun sk = ep->base.sk;
224*4882a593Smuzhiyun /* Remove and free the port */
225*4882a593Smuzhiyun if (sctp_sk(sk)->bind_hash)
226*4882a593Smuzhiyun sctp_put_port(sk);
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun call_rcu(&ep->rcu, sctp_endpoint_destroy_rcu);
229*4882a593Smuzhiyun }
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun /* Hold a reference to an endpoint. */
sctp_endpoint_hold(struct sctp_endpoint * ep)232*4882a593Smuzhiyun int sctp_endpoint_hold(struct sctp_endpoint *ep)
233*4882a593Smuzhiyun {
234*4882a593Smuzhiyun return refcount_inc_not_zero(&ep->base.refcnt);
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun /* Release a reference to an endpoint and clean up if there are
238*4882a593Smuzhiyun * no more references.
239*4882a593Smuzhiyun */
sctp_endpoint_put(struct sctp_endpoint * ep)240*4882a593Smuzhiyun void sctp_endpoint_put(struct sctp_endpoint *ep)
241*4882a593Smuzhiyun {
242*4882a593Smuzhiyun if (refcount_dec_and_test(&ep->base.refcnt))
243*4882a593Smuzhiyun sctp_endpoint_destroy(ep);
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun /* Is this the endpoint we are looking for? */
sctp_endpoint_is_match(struct sctp_endpoint * ep,struct net * net,const union sctp_addr * laddr)247*4882a593Smuzhiyun struct sctp_endpoint *sctp_endpoint_is_match(struct sctp_endpoint *ep,
248*4882a593Smuzhiyun struct net *net,
249*4882a593Smuzhiyun const union sctp_addr *laddr)
250*4882a593Smuzhiyun {
251*4882a593Smuzhiyun struct sctp_endpoint *retval = NULL;
252*4882a593Smuzhiyun
253*4882a593Smuzhiyun if ((htons(ep->base.bind_addr.port) == laddr->v4.sin_port) &&
254*4882a593Smuzhiyun net_eq(ep->base.net, net)) {
255*4882a593Smuzhiyun if (sctp_bind_addr_match(&ep->base.bind_addr, laddr,
256*4882a593Smuzhiyun sctp_sk(ep->base.sk)))
257*4882a593Smuzhiyun retval = ep;
258*4882a593Smuzhiyun }
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun return retval;
261*4882a593Smuzhiyun }
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun /* Find the association that goes with this chunk.
264*4882a593Smuzhiyun * We lookup the transport from hashtable at first, then get association
265*4882a593Smuzhiyun * through t->assoc.
266*4882a593Smuzhiyun */
sctp_endpoint_lookup_assoc(const struct sctp_endpoint * ep,const union sctp_addr * paddr,struct sctp_transport ** transport)267*4882a593Smuzhiyun struct sctp_association *sctp_endpoint_lookup_assoc(
268*4882a593Smuzhiyun const struct sctp_endpoint *ep,
269*4882a593Smuzhiyun const union sctp_addr *paddr,
270*4882a593Smuzhiyun struct sctp_transport **transport)
271*4882a593Smuzhiyun {
272*4882a593Smuzhiyun struct sctp_association *asoc = NULL;
273*4882a593Smuzhiyun struct sctp_transport *t;
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun *transport = NULL;
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun /* If the local port is not set, there can't be any associations
278*4882a593Smuzhiyun * on this endpoint.
279*4882a593Smuzhiyun */
280*4882a593Smuzhiyun if (!ep->base.bind_addr.port)
281*4882a593Smuzhiyun return NULL;
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun rcu_read_lock();
284*4882a593Smuzhiyun t = sctp_epaddr_lookup_transport(ep, paddr);
285*4882a593Smuzhiyun if (!t)
286*4882a593Smuzhiyun goto out;
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun *transport = t;
289*4882a593Smuzhiyun asoc = t->asoc;
290*4882a593Smuzhiyun out:
291*4882a593Smuzhiyun rcu_read_unlock();
292*4882a593Smuzhiyun return asoc;
293*4882a593Smuzhiyun }
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun /* Look for any peeled off association from the endpoint that matches the
296*4882a593Smuzhiyun * given peer address.
297*4882a593Smuzhiyun */
sctp_endpoint_is_peeled_off(struct sctp_endpoint * ep,const union sctp_addr * paddr)298*4882a593Smuzhiyun bool sctp_endpoint_is_peeled_off(struct sctp_endpoint *ep,
299*4882a593Smuzhiyun const union sctp_addr *paddr)
300*4882a593Smuzhiyun {
301*4882a593Smuzhiyun struct sctp_sockaddr_entry *addr;
302*4882a593Smuzhiyun struct net *net = ep->base.net;
303*4882a593Smuzhiyun struct sctp_bind_addr *bp;
304*4882a593Smuzhiyun
305*4882a593Smuzhiyun bp = &ep->base.bind_addr;
306*4882a593Smuzhiyun /* This function is called with the socket lock held,
307*4882a593Smuzhiyun * so the address_list can not change.
308*4882a593Smuzhiyun */
309*4882a593Smuzhiyun list_for_each_entry(addr, &bp->address_list, list) {
310*4882a593Smuzhiyun if (sctp_has_association(net, &addr->a, paddr))
311*4882a593Smuzhiyun return true;
312*4882a593Smuzhiyun }
313*4882a593Smuzhiyun
314*4882a593Smuzhiyun return false;
315*4882a593Smuzhiyun }
316*4882a593Smuzhiyun
317*4882a593Smuzhiyun /* Do delayed input processing. This is scheduled by sctp_rcv().
318*4882a593Smuzhiyun * This may be called on BH or task time.
319*4882a593Smuzhiyun */
sctp_endpoint_bh_rcv(struct work_struct * work)320*4882a593Smuzhiyun static void sctp_endpoint_bh_rcv(struct work_struct *work)
321*4882a593Smuzhiyun {
322*4882a593Smuzhiyun struct sctp_endpoint *ep =
323*4882a593Smuzhiyun container_of(work, struct sctp_endpoint,
324*4882a593Smuzhiyun base.inqueue.immediate);
325*4882a593Smuzhiyun struct sctp_association *asoc;
326*4882a593Smuzhiyun struct sock *sk;
327*4882a593Smuzhiyun struct net *net;
328*4882a593Smuzhiyun struct sctp_transport *transport;
329*4882a593Smuzhiyun struct sctp_chunk *chunk;
330*4882a593Smuzhiyun struct sctp_inq *inqueue;
331*4882a593Smuzhiyun union sctp_subtype subtype;
332*4882a593Smuzhiyun enum sctp_state state;
333*4882a593Smuzhiyun int error = 0;
334*4882a593Smuzhiyun int first_time = 1; /* is this the first time through the loop */
335*4882a593Smuzhiyun
336*4882a593Smuzhiyun if (ep->base.dead)
337*4882a593Smuzhiyun return;
338*4882a593Smuzhiyun
339*4882a593Smuzhiyun asoc = NULL;
340*4882a593Smuzhiyun inqueue = &ep->base.inqueue;
341*4882a593Smuzhiyun sk = ep->base.sk;
342*4882a593Smuzhiyun net = sock_net(sk);
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun while (NULL != (chunk = sctp_inq_pop(inqueue))) {
345*4882a593Smuzhiyun subtype = SCTP_ST_CHUNK(chunk->chunk_hdr->type);
346*4882a593Smuzhiyun
347*4882a593Smuzhiyun /* If the first chunk in the packet is AUTH, do special
348*4882a593Smuzhiyun * processing specified in Section 6.3 of SCTP-AUTH spec
349*4882a593Smuzhiyun */
350*4882a593Smuzhiyun if (first_time && (subtype.chunk == SCTP_CID_AUTH)) {
351*4882a593Smuzhiyun struct sctp_chunkhdr *next_hdr;
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun next_hdr = sctp_inq_peek(inqueue);
354*4882a593Smuzhiyun if (!next_hdr)
355*4882a593Smuzhiyun goto normal;
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun /* If the next chunk is COOKIE-ECHO, skip the AUTH
358*4882a593Smuzhiyun * chunk while saving a pointer to it so we can do
359*4882a593Smuzhiyun * Authentication later (during cookie-echo
360*4882a593Smuzhiyun * processing).
361*4882a593Smuzhiyun */
362*4882a593Smuzhiyun if (next_hdr->type == SCTP_CID_COOKIE_ECHO) {
363*4882a593Smuzhiyun chunk->auth_chunk = skb_clone(chunk->skb,
364*4882a593Smuzhiyun GFP_ATOMIC);
365*4882a593Smuzhiyun chunk->auth = 1;
366*4882a593Smuzhiyun continue;
367*4882a593Smuzhiyun }
368*4882a593Smuzhiyun }
369*4882a593Smuzhiyun normal:
370*4882a593Smuzhiyun /* We might have grown an association since last we
371*4882a593Smuzhiyun * looked, so try again.
372*4882a593Smuzhiyun *
373*4882a593Smuzhiyun * This happens when we've just processed our
374*4882a593Smuzhiyun * COOKIE-ECHO chunk.
375*4882a593Smuzhiyun */
376*4882a593Smuzhiyun if (NULL == chunk->asoc) {
377*4882a593Smuzhiyun asoc = sctp_endpoint_lookup_assoc(ep,
378*4882a593Smuzhiyun sctp_source(chunk),
379*4882a593Smuzhiyun &transport);
380*4882a593Smuzhiyun chunk->asoc = asoc;
381*4882a593Smuzhiyun chunk->transport = transport;
382*4882a593Smuzhiyun }
383*4882a593Smuzhiyun
384*4882a593Smuzhiyun state = asoc ? asoc->state : SCTP_STATE_CLOSED;
385*4882a593Smuzhiyun if (sctp_auth_recv_cid(subtype.chunk, asoc) && !chunk->auth)
386*4882a593Smuzhiyun continue;
387*4882a593Smuzhiyun
388*4882a593Smuzhiyun /* Remember where the last DATA chunk came from so we
389*4882a593Smuzhiyun * know where to send the SACK.
390*4882a593Smuzhiyun */
391*4882a593Smuzhiyun if (asoc && sctp_chunk_is_data(chunk))
392*4882a593Smuzhiyun asoc->peer.last_data_from = chunk->transport;
393*4882a593Smuzhiyun else {
394*4882a593Smuzhiyun SCTP_INC_STATS(ep->base.net, SCTP_MIB_INCTRLCHUNKS);
395*4882a593Smuzhiyun if (asoc)
396*4882a593Smuzhiyun asoc->stats.ictrlchunks++;
397*4882a593Smuzhiyun }
398*4882a593Smuzhiyun
399*4882a593Smuzhiyun if (chunk->transport)
400*4882a593Smuzhiyun chunk->transport->last_time_heard = ktime_get();
401*4882a593Smuzhiyun
402*4882a593Smuzhiyun error = sctp_do_sm(net, SCTP_EVENT_T_CHUNK, subtype, state,
403*4882a593Smuzhiyun ep, asoc, chunk, GFP_ATOMIC);
404*4882a593Smuzhiyun
405*4882a593Smuzhiyun if (error && chunk)
406*4882a593Smuzhiyun chunk->pdiscard = 1;
407*4882a593Smuzhiyun
408*4882a593Smuzhiyun /* Check to see if the endpoint is freed in response to
409*4882a593Smuzhiyun * the incoming chunk. If so, get out of the while loop.
410*4882a593Smuzhiyun */
411*4882a593Smuzhiyun if (!sctp_sk(sk)->ep)
412*4882a593Smuzhiyun break;
413*4882a593Smuzhiyun
414*4882a593Smuzhiyun if (first_time)
415*4882a593Smuzhiyun first_time = 0;
416*4882a593Smuzhiyun }
417*4882a593Smuzhiyun }
418