Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* SCTP kernel implementation
0003  * Copyright (c) 1999-2000 Cisco, Inc.
0004  * Copyright (c) 1999-2001 Motorola, Inc.
0005  * Copyright (c) 2001-2002 International Business Machines, Corp.
0006  * Copyright (c) 2001 Intel Corp.
0007  * Copyright (c) 2001 Nokia, Inc.
0008  * Copyright (c) 2001 La Monte H.P. Yarroll
0009  *
0010  * This file is part of the SCTP kernel implementation
0011  *
0012  * This abstraction represents an SCTP endpoint.
0013  *
0014  * Please send any bug reports or fixes you make to the
0015  * email address(es):
0016  *    lksctp developers <linux-sctp@vger.kernel.org>
0017  *
0018  * Written or modified by:
0019  *    La Monte H.P. Yarroll <piggy@acm.org>
0020  *    Karl Knutson <karl@athena.chicago.il.us>
0021  *    Jon Grimm <jgrimm@austin.ibm.com>
0022  *    Daisy Chang <daisyc@us.ibm.com>
0023  *    Dajiang Zhang <dajiang.zhang@nokia.com>
0024  */
0025 
0026 #include <linux/types.h>
0027 #include <linux/slab.h>
0028 #include <linux/in.h>
0029 #include <linux/random.h>   /* get_random_bytes() */
0030 #include <net/sock.h>
0031 #include <net/ipv6.h>
0032 #include <net/sctp/sctp.h>
0033 #include <net/sctp/sm.h>
0034 
0035 /* Forward declarations for internal helpers. */
0036 static void sctp_endpoint_bh_rcv(struct work_struct *work);
0037 
0038 /*
0039  * Initialize the base fields of the endpoint structure.
0040  */
0041 static struct sctp_endpoint *sctp_endpoint_init(struct sctp_endpoint *ep,
0042                         struct sock *sk,
0043                         gfp_t gfp)
0044 {
0045     struct net *net = sock_net(sk);
0046     struct sctp_shared_key *null_key;
0047 
0048     ep->digest = kzalloc(SCTP_SIGNATURE_SIZE, gfp);
0049     if (!ep->digest)
0050         return NULL;
0051 
0052     ep->asconf_enable = net->sctp.addip_enable;
0053     ep->auth_enable = net->sctp.auth_enable;
0054     if (ep->auth_enable) {
0055         if (sctp_auth_init(ep, gfp))
0056             goto nomem;
0057         if (ep->asconf_enable) {
0058             sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF);
0059             sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF_ACK);
0060         }
0061     }
0062 
0063     /* Initialize the base structure. */
0064     /* What type of endpoint are we?  */
0065     ep->base.type = SCTP_EP_TYPE_SOCKET;
0066 
0067     /* Initialize the basic object fields. */
0068     refcount_set(&ep->base.refcnt, 1);
0069     ep->base.dead = false;
0070 
0071     /* Create an input queue.  */
0072     sctp_inq_init(&ep->base.inqueue);
0073 
0074     /* Set its top-half handler */
0075     sctp_inq_set_th_handler(&ep->base.inqueue, sctp_endpoint_bh_rcv);
0076 
0077     /* Initialize the bind addr area */
0078     sctp_bind_addr_init(&ep->base.bind_addr, 0);
0079 
0080     /* Create the lists of associations.  */
0081     INIT_LIST_HEAD(&ep->asocs);
0082 
0083     /* Use SCTP specific send buffer space queues.  */
0084     ep->sndbuf_policy = net->sctp.sndbuf_policy;
0085 
0086     sk->sk_data_ready = sctp_data_ready;
0087     sk->sk_write_space = sctp_write_space;
0088     sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
0089 
0090     /* Get the receive buffer policy for this endpoint */
0091     ep->rcvbuf_policy = net->sctp.rcvbuf_policy;
0092 
0093     /* Initialize the secret key used with cookie. */
0094     get_random_bytes(ep->secret_key, sizeof(ep->secret_key));
0095 
0096     /* SCTP-AUTH extensions*/
0097     INIT_LIST_HEAD(&ep->endpoint_shared_keys);
0098     null_key = sctp_auth_shkey_create(0, gfp);
0099     if (!null_key)
0100         goto nomem_shkey;
0101 
0102     list_add(&null_key->key_list, &ep->endpoint_shared_keys);
0103 
0104     /* Add the null key to the endpoint shared keys list and
0105      * set the hmcas and chunks pointers.
0106      */
0107     ep->prsctp_enable = net->sctp.prsctp_enable;
0108     ep->reconf_enable = net->sctp.reconf_enable;
0109     ep->ecn_enable = net->sctp.ecn_enable;
0110 
0111     /* Remember who we are attached to.  */
0112     ep->base.sk = sk;
0113     ep->base.net = sock_net(sk);
0114     sock_hold(ep->base.sk);
0115 
0116     return ep;
0117 
0118 nomem_shkey:
0119     sctp_auth_free(ep);
0120 nomem:
0121     kfree(ep->digest);
0122     return NULL;
0123 
0124 }
0125 
0126 /* Create a sctp_endpoint with all that boring stuff initialized.
0127  * Returns NULL if there isn't enough memory.
0128  */
0129 struct sctp_endpoint *sctp_endpoint_new(struct sock *sk, gfp_t gfp)
0130 {
0131     struct sctp_endpoint *ep;
0132 
0133     /* Build a local endpoint. */
0134     ep = kzalloc(sizeof(*ep), gfp);
0135     if (!ep)
0136         goto fail;
0137 
0138     if (!sctp_endpoint_init(ep, sk, gfp))
0139         goto fail_init;
0140 
0141     SCTP_DBG_OBJCNT_INC(ep);
0142     return ep;
0143 
0144 fail_init:
0145     kfree(ep);
0146 fail:
0147     return NULL;
0148 }
0149 
0150 /* Add an association to an endpoint.  */
0151 void sctp_endpoint_add_asoc(struct sctp_endpoint *ep,
0152                 struct sctp_association *asoc)
0153 {
0154     struct sock *sk = ep->base.sk;
0155 
0156     /* If this is a temporary association, don't bother
0157      * since we'll be removing it shortly and don't
0158      * want anyone to find it anyway.
0159      */
0160     if (asoc->temp)
0161         return;
0162 
0163     /* Now just add it to our list of asocs */
0164     list_add_tail(&asoc->asocs, &ep->asocs);
0165 
0166     /* Increment the backlog value for a TCP-style listening socket. */
0167     if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
0168         sk_acceptq_added(sk);
0169 }
0170 
0171 /* Free the endpoint structure.  Delay cleanup until
0172  * all users have released their reference count on this structure.
0173  */
0174 void sctp_endpoint_free(struct sctp_endpoint *ep)
0175 {
0176     ep->base.dead = true;
0177 
0178     inet_sk_set_state(ep->base.sk, SCTP_SS_CLOSED);
0179 
0180     /* Unlink this endpoint, so we can't find it again! */
0181     sctp_unhash_endpoint(ep);
0182 
0183     sctp_endpoint_put(ep);
0184 }
0185 
0186 /* Final destructor for endpoint.  */
0187 static void sctp_endpoint_destroy_rcu(struct rcu_head *head)
0188 {
0189     struct sctp_endpoint *ep = container_of(head, struct sctp_endpoint, rcu);
0190     struct sock *sk = ep->base.sk;
0191 
0192     sctp_sk(sk)->ep = NULL;
0193     sock_put(sk);
0194 
0195     kfree(ep);
0196     SCTP_DBG_OBJCNT_DEC(ep);
0197 }
0198 
0199 static void sctp_endpoint_destroy(struct sctp_endpoint *ep)
0200 {
0201     struct sock *sk;
0202 
0203     if (unlikely(!ep->base.dead)) {
0204         WARN(1, "Attempt to destroy undead endpoint %p!\n", ep);
0205         return;
0206     }
0207 
0208     /* Free the digest buffer */
0209     kfree(ep->digest);
0210 
0211     /* SCTP-AUTH: Free up AUTH releated data such as shared keys
0212      * chunks and hmacs arrays that were allocated
0213      */
0214     sctp_auth_destroy_keys(&ep->endpoint_shared_keys);
0215     sctp_auth_free(ep);
0216 
0217     /* Cleanup. */
0218     sctp_inq_free(&ep->base.inqueue);
0219     sctp_bind_addr_free(&ep->base.bind_addr);
0220 
0221     memset(ep->secret_key, 0, sizeof(ep->secret_key));
0222 
0223     sk = ep->base.sk;
0224     /* Remove and free the port */
0225     if (sctp_sk(sk)->bind_hash)
0226         sctp_put_port(sk);
0227 
0228     call_rcu(&ep->rcu, sctp_endpoint_destroy_rcu);
0229 }
0230 
0231 /* Hold a reference to an endpoint. */
0232 int sctp_endpoint_hold(struct sctp_endpoint *ep)
0233 {
0234     return refcount_inc_not_zero(&ep->base.refcnt);
0235 }
0236 
0237 /* Release a reference to an endpoint and clean up if there are
0238  * no more references.
0239  */
0240 void sctp_endpoint_put(struct sctp_endpoint *ep)
0241 {
0242     if (refcount_dec_and_test(&ep->base.refcnt))
0243         sctp_endpoint_destroy(ep);
0244 }
0245 
0246 /* Is this the endpoint we are looking for?  */
0247 struct sctp_endpoint *sctp_endpoint_is_match(struct sctp_endpoint *ep,
0248                            struct net *net,
0249                            const union sctp_addr *laddr)
0250 {
0251     struct sctp_endpoint *retval = NULL;
0252 
0253     if ((htons(ep->base.bind_addr.port) == laddr->v4.sin_port) &&
0254         net_eq(ep->base.net, net)) {
0255         if (sctp_bind_addr_match(&ep->base.bind_addr, laddr,
0256                      sctp_sk(ep->base.sk)))
0257             retval = ep;
0258     }
0259 
0260     return retval;
0261 }
0262 
0263 /* Find the association that goes with this chunk.
0264  * We lookup the transport from hashtable at first, then get association
0265  * through t->assoc.
0266  */
0267 struct sctp_association *sctp_endpoint_lookup_assoc(
0268     const struct sctp_endpoint *ep,
0269     const union sctp_addr *paddr,
0270     struct sctp_transport **transport)
0271 {
0272     struct sctp_association *asoc = NULL;
0273     struct sctp_transport *t;
0274 
0275     *transport = NULL;
0276 
0277     /* If the local port is not set, there can't be any associations
0278      * on this endpoint.
0279      */
0280     if (!ep->base.bind_addr.port)
0281         return NULL;
0282 
0283     rcu_read_lock();
0284     t = sctp_epaddr_lookup_transport(ep, paddr);
0285     if (!t)
0286         goto out;
0287 
0288     *transport = t;
0289     asoc = t->asoc;
0290 out:
0291     rcu_read_unlock();
0292     return asoc;
0293 }
0294 
0295 /* Look for any peeled off association from the endpoint that matches the
0296  * given peer address.
0297  */
0298 bool sctp_endpoint_is_peeled_off(struct sctp_endpoint *ep,
0299                  const union sctp_addr *paddr)
0300 {
0301     struct sctp_sockaddr_entry *addr;
0302     struct net *net = ep->base.net;
0303     struct sctp_bind_addr *bp;
0304 
0305     bp = &ep->base.bind_addr;
0306     /* This function is called with the socket lock held,
0307      * so the address_list can not change.
0308      */
0309     list_for_each_entry(addr, &bp->address_list, list) {
0310         if (sctp_has_association(net, &addr->a, paddr))
0311             return true;
0312     }
0313 
0314     return false;
0315 }
0316 
0317 /* Do delayed input processing.  This is scheduled by sctp_rcv().
0318  * This may be called on BH or task time.
0319  */
0320 static void sctp_endpoint_bh_rcv(struct work_struct *work)
0321 {
0322     struct sctp_endpoint *ep =
0323         container_of(work, struct sctp_endpoint,
0324                  base.inqueue.immediate);
0325     struct sctp_association *asoc;
0326     struct sock *sk;
0327     struct net *net;
0328     struct sctp_transport *transport;
0329     struct sctp_chunk *chunk;
0330     struct sctp_inq *inqueue;
0331     union sctp_subtype subtype;
0332     enum sctp_state state;
0333     int error = 0;
0334     int first_time = 1; /* is this the first time through the loop */
0335 
0336     if (ep->base.dead)
0337         return;
0338 
0339     asoc = NULL;
0340     inqueue = &ep->base.inqueue;
0341     sk = ep->base.sk;
0342     net = sock_net(sk);
0343 
0344     while (NULL != (chunk = sctp_inq_pop(inqueue))) {
0345         subtype = SCTP_ST_CHUNK(chunk->chunk_hdr->type);
0346 
0347         /* If the first chunk in the packet is AUTH, do special
0348          * processing specified in Section 6.3 of SCTP-AUTH spec
0349          */
0350         if (first_time && (subtype.chunk == SCTP_CID_AUTH)) {
0351             struct sctp_chunkhdr *next_hdr;
0352 
0353             next_hdr = sctp_inq_peek(inqueue);
0354             if (!next_hdr)
0355                 goto normal;
0356 
0357             /* If the next chunk is COOKIE-ECHO, skip the AUTH
0358              * chunk while saving a pointer to it so we can do
0359              * Authentication later (during cookie-echo
0360              * processing).
0361              */
0362             if (next_hdr->type == SCTP_CID_COOKIE_ECHO) {
0363                 chunk->auth_chunk = skb_clone(chunk->skb,
0364                                 GFP_ATOMIC);
0365                 chunk->auth = 1;
0366                 continue;
0367             }
0368         }
0369 normal:
0370         /* We might have grown an association since last we
0371          * looked, so try again.
0372          *
0373          * This happens when we've just processed our
0374          * COOKIE-ECHO chunk.
0375          */
0376         if (NULL == chunk->asoc) {
0377             asoc = sctp_endpoint_lookup_assoc(ep,
0378                               sctp_source(chunk),
0379                               &transport);
0380             chunk->asoc = asoc;
0381             chunk->transport = transport;
0382         }
0383 
0384         state = asoc ? asoc->state : SCTP_STATE_CLOSED;
0385         if (sctp_auth_recv_cid(subtype.chunk, asoc) && !chunk->auth)
0386             continue;
0387 
0388         /* Remember where the last DATA chunk came from so we
0389          * know where to send the SACK.
0390          */
0391         if (asoc && sctp_chunk_is_data(chunk))
0392             asoc->peer.last_data_from = chunk->transport;
0393         else {
0394             SCTP_INC_STATS(ep->base.net, SCTP_MIB_INCTRLCHUNKS);
0395             if (asoc)
0396                 asoc->stats.ictrlchunks++;
0397         }
0398 
0399         if (chunk->transport)
0400             chunk->transport->last_time_heard = ktime_get();
0401 
0402         error = sctp_do_sm(net, SCTP_EVENT_T_CHUNK, subtype, state,
0403                    ep, asoc, chunk, GFP_ATOMIC);
0404 
0405         if (error && chunk)
0406             chunk->pdiscard = 1;
0407 
0408         /* Check to see if the endpoint is freed in response to
0409          * the incoming chunk. If so, get out of the while loop.
0410          */
0411         if (!sctp_sk(sk)->ep)
0412             break;
0413 
0414         if (first_time)
0415             first_time = 0;
0416     }
0417 }