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