Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* SCTP kernel implementation
0003  * (C) Copyright IBM Corp. 2001, 2003
0004  * Copyright (c) Cisco 1999,2000
0005  * Copyright (c) Motorola 1999,2000,2001
0006  * Copyright (c) La Monte H.P. Yarroll 2001
0007  *
0008  * This file is part of the SCTP kernel implementation.
0009  *
0010  * A collection class to handle the storage of transport addresses.
0011  *
0012  * Please send any bug reports or fixes you make to the
0013  * email address(es):
0014  *    lksctp developers <linux-sctp@vger.kernel.org>
0015  *
0016  * Written or modified by:
0017  *    La Monte H.P. Yarroll <piggy@acm.org>
0018  *    Karl Knutson          <karl@athena.chicago.il.us>
0019  *    Jon Grimm             <jgrimm@us.ibm.com>
0020  *    Daisy Chang           <daisyc@us.ibm.com>
0021  */
0022 
0023 #include <linux/types.h>
0024 #include <linux/slab.h>
0025 #include <linux/in.h>
0026 #include <net/sock.h>
0027 #include <net/ipv6.h>
0028 #include <net/if_inet6.h>
0029 #include <net/sctp/sctp.h>
0030 #include <net/sctp/sm.h>
0031 
0032 /* Forward declarations for internal helpers. */
0033 static int sctp_copy_one_addr(struct net *net, struct sctp_bind_addr *dest,
0034                   union sctp_addr *addr, enum sctp_scope scope,
0035                   gfp_t gfp, int flags);
0036 static void sctp_bind_addr_clean(struct sctp_bind_addr *);
0037 
0038 /* First Level Abstractions. */
0039 
0040 /* Copy 'src' to 'dest' taking 'scope' into account.  Omit addresses
0041  * in 'src' which have a broader scope than 'scope'.
0042  */
0043 int sctp_bind_addr_copy(struct net *net, struct sctp_bind_addr *dest,
0044             const struct sctp_bind_addr *src,
0045             enum sctp_scope scope, gfp_t gfp,
0046             int flags)
0047 {
0048     struct sctp_sockaddr_entry *addr;
0049     int error = 0;
0050 
0051     /* All addresses share the same port.  */
0052     dest->port = src->port;
0053 
0054     /* Extract the addresses which are relevant for this scope.  */
0055     list_for_each_entry(addr, &src->address_list, list) {
0056         error = sctp_copy_one_addr(net, dest, &addr->a, scope,
0057                        gfp, flags);
0058         if (error < 0)
0059             goto out;
0060     }
0061 
0062     /* If there are no addresses matching the scope and
0063      * this is global scope, try to get a link scope address, with
0064      * the assumption that we must be sitting behind a NAT.
0065      */
0066     if (list_empty(&dest->address_list) && (SCTP_SCOPE_GLOBAL == scope)) {
0067         list_for_each_entry(addr, &src->address_list, list) {
0068             error = sctp_copy_one_addr(net, dest, &addr->a,
0069                            SCTP_SCOPE_LINK, gfp,
0070                            flags);
0071             if (error < 0)
0072                 goto out;
0073         }
0074     }
0075 
0076 out:
0077     if (error)
0078         sctp_bind_addr_clean(dest);
0079 
0080     return error;
0081 }
0082 
0083 /* Exactly duplicate the address lists.  This is necessary when doing
0084  * peer-offs and accepts.  We don't want to put all the current system
0085  * addresses into the endpoint.  That's useless.  But we do want duplicat
0086  * the list of bound addresses that the older endpoint used.
0087  */
0088 int sctp_bind_addr_dup(struct sctp_bind_addr *dest,
0089             const struct sctp_bind_addr *src,
0090             gfp_t gfp)
0091 {
0092     struct sctp_sockaddr_entry *addr;
0093     int error = 0;
0094 
0095     /* All addresses share the same port.  */
0096     dest->port = src->port;
0097 
0098     list_for_each_entry(addr, &src->address_list, list) {
0099         error = sctp_add_bind_addr(dest, &addr->a, sizeof(addr->a),
0100                        1, gfp);
0101         if (error < 0)
0102             break;
0103     }
0104 
0105     return error;
0106 }
0107 
0108 /* Initialize the SCTP_bind_addr structure for either an endpoint or
0109  * an association.
0110  */
0111 void sctp_bind_addr_init(struct sctp_bind_addr *bp, __u16 port)
0112 {
0113     INIT_LIST_HEAD(&bp->address_list);
0114     bp->port = port;
0115 }
0116 
0117 /* Dispose of the address list. */
0118 static void sctp_bind_addr_clean(struct sctp_bind_addr *bp)
0119 {
0120     struct sctp_sockaddr_entry *addr, *temp;
0121 
0122     /* Empty the bind address list. */
0123     list_for_each_entry_safe(addr, temp, &bp->address_list, list) {
0124         list_del_rcu(&addr->list);
0125         kfree_rcu(addr, rcu);
0126         SCTP_DBG_OBJCNT_DEC(addr);
0127     }
0128 }
0129 
0130 /* Dispose of an SCTP_bind_addr structure  */
0131 void sctp_bind_addr_free(struct sctp_bind_addr *bp)
0132 {
0133     /* Empty the bind address list. */
0134     sctp_bind_addr_clean(bp);
0135 }
0136 
0137 /* Add an address to the bind address list in the SCTP_bind_addr structure. */
0138 int sctp_add_bind_addr(struct sctp_bind_addr *bp, union sctp_addr *new,
0139                int new_size, __u8 addr_state, gfp_t gfp)
0140 {
0141     struct sctp_sockaddr_entry *addr;
0142 
0143     /* Add the address to the bind address list.  */
0144     addr = kzalloc(sizeof(*addr), gfp);
0145     if (!addr)
0146         return -ENOMEM;
0147 
0148     memcpy(&addr->a, new, min_t(size_t, sizeof(*new), new_size));
0149 
0150     /* Fix up the port if it has not yet been set.
0151      * Both v4 and v6 have the port at the same offset.
0152      */
0153     if (!addr->a.v4.sin_port)
0154         addr->a.v4.sin_port = htons(bp->port);
0155 
0156     addr->state = addr_state;
0157     addr->valid = 1;
0158 
0159     INIT_LIST_HEAD(&addr->list);
0160 
0161     /* We always hold a socket lock when calling this function,
0162      * and that acts as a writer synchronizing lock.
0163      */
0164     list_add_tail_rcu(&addr->list, &bp->address_list);
0165     SCTP_DBG_OBJCNT_INC(addr);
0166 
0167     return 0;
0168 }
0169 
0170 /* Delete an address from the bind address list in the SCTP_bind_addr
0171  * structure.
0172  */
0173 int sctp_del_bind_addr(struct sctp_bind_addr *bp, union sctp_addr *del_addr)
0174 {
0175     struct sctp_sockaddr_entry *addr, *temp;
0176     int found = 0;
0177 
0178     /* We hold the socket lock when calling this function,
0179      * and that acts as a writer synchronizing lock.
0180      */
0181     list_for_each_entry_safe(addr, temp, &bp->address_list, list) {
0182         if (sctp_cmp_addr_exact(&addr->a, del_addr)) {
0183             /* Found the exact match. */
0184             found = 1;
0185             addr->valid = 0;
0186             list_del_rcu(&addr->list);
0187             break;
0188         }
0189     }
0190 
0191     if (found) {
0192         kfree_rcu(addr, rcu);
0193         SCTP_DBG_OBJCNT_DEC(addr);
0194         return 0;
0195     }
0196 
0197     return -EINVAL;
0198 }
0199 
0200 /* Create a network byte-order representation of all the addresses
0201  * formated as SCTP parameters.
0202  *
0203  * The second argument is the return value for the length.
0204  */
0205 union sctp_params sctp_bind_addrs_to_raw(const struct sctp_bind_addr *bp,
0206                      int *addrs_len,
0207                      gfp_t gfp)
0208 {
0209     union sctp_params addrparms;
0210     union sctp_params retval;
0211     int addrparms_len;
0212     union sctp_addr_param rawaddr;
0213     int len;
0214     struct sctp_sockaddr_entry *addr;
0215     struct list_head *pos;
0216     struct sctp_af *af;
0217 
0218     addrparms_len = 0;
0219     len = 0;
0220 
0221     /* Allocate enough memory at once. */
0222     list_for_each(pos, &bp->address_list) {
0223         len += sizeof(union sctp_addr_param);
0224     }
0225 
0226     /* Don't even bother embedding an address if there
0227      * is only one.
0228      */
0229     if (len == sizeof(union sctp_addr_param)) {
0230         retval.v = NULL;
0231         goto end_raw;
0232     }
0233 
0234     retval.v = kmalloc(len, gfp);
0235     if (!retval.v)
0236         goto end_raw;
0237 
0238     addrparms = retval;
0239 
0240     list_for_each_entry(addr, &bp->address_list, list) {
0241         af = sctp_get_af_specific(addr->a.v4.sin_family);
0242         len = af->to_addr_param(&addr->a, &rawaddr);
0243         memcpy(addrparms.v, &rawaddr, len);
0244         addrparms.v += len;
0245         addrparms_len += len;
0246     }
0247 
0248 end_raw:
0249     *addrs_len = addrparms_len;
0250     return retval;
0251 }
0252 
0253 /*
0254  * Create an address list out of the raw address list format (IPv4 and IPv6
0255  * address parameters).
0256  */
0257 int sctp_raw_to_bind_addrs(struct sctp_bind_addr *bp, __u8 *raw_addr_list,
0258                int addrs_len, __u16 port, gfp_t gfp)
0259 {
0260     union sctp_addr_param *rawaddr;
0261     struct sctp_paramhdr *param;
0262     union sctp_addr addr;
0263     int retval = 0;
0264     int len;
0265     struct sctp_af *af;
0266 
0267     /* Convert the raw address to standard address format */
0268     while (addrs_len) {
0269         param = (struct sctp_paramhdr *)raw_addr_list;
0270         rawaddr = (union sctp_addr_param *)raw_addr_list;
0271 
0272         af = sctp_get_af_specific(param_type2af(param->type));
0273         if (unlikely(!af) ||
0274             !af->from_addr_param(&addr, rawaddr, htons(port), 0)) {
0275             retval = -EINVAL;
0276             goto out_err;
0277         }
0278 
0279         if (sctp_bind_addr_state(bp, &addr) != -1)
0280             goto next;
0281         retval = sctp_add_bind_addr(bp, &addr, sizeof(addr),
0282                         SCTP_ADDR_SRC, gfp);
0283         if (retval)
0284             /* Can't finish building the list, clean up. */
0285             goto out_err;
0286 
0287 next:
0288         len = ntohs(param->length);
0289         addrs_len -= len;
0290         raw_addr_list += len;
0291     }
0292 
0293     return retval;
0294 
0295 out_err:
0296     if (retval)
0297         sctp_bind_addr_clean(bp);
0298 
0299     return retval;
0300 }
0301 
0302 /********************************************************************
0303  * 2nd Level Abstractions
0304  ********************************************************************/
0305 
0306 /* Does this contain a specified address?  Allow wildcarding. */
0307 int sctp_bind_addr_match(struct sctp_bind_addr *bp,
0308              const union sctp_addr *addr,
0309              struct sctp_sock *opt)
0310 {
0311     struct sctp_sockaddr_entry *laddr;
0312     int match = 0;
0313 
0314     rcu_read_lock();
0315     list_for_each_entry_rcu(laddr, &bp->address_list, list) {
0316         if (!laddr->valid)
0317             continue;
0318         if (opt->pf->cmp_addr(&laddr->a, addr, opt)) {
0319             match = 1;
0320             break;
0321         }
0322     }
0323     rcu_read_unlock();
0324 
0325     return match;
0326 }
0327 
0328 int sctp_bind_addrs_check(struct sctp_sock *sp,
0329               struct sctp_sock *sp2, int cnt2)
0330 {
0331     struct sctp_bind_addr *bp2 = &sp2->ep->base.bind_addr;
0332     struct sctp_bind_addr *bp = &sp->ep->base.bind_addr;
0333     struct sctp_sockaddr_entry *laddr, *laddr2;
0334     bool exist = false;
0335     int cnt = 0;
0336 
0337     rcu_read_lock();
0338     list_for_each_entry_rcu(laddr, &bp->address_list, list) {
0339         list_for_each_entry_rcu(laddr2, &bp2->address_list, list) {
0340             if (sp->pf->af->cmp_addr(&laddr->a, &laddr2->a) &&
0341                 laddr->valid && laddr2->valid) {
0342                 exist = true;
0343                 goto next;
0344             }
0345         }
0346         cnt = 0;
0347         break;
0348 next:
0349         cnt++;
0350     }
0351     rcu_read_unlock();
0352 
0353     return (cnt == cnt2) ? 0 : (exist ? -EEXIST : 1);
0354 }
0355 
0356 /* Does the address 'addr' conflict with any addresses in
0357  * the bp.
0358  */
0359 int sctp_bind_addr_conflict(struct sctp_bind_addr *bp,
0360                 const union sctp_addr *addr,
0361                 struct sctp_sock *bp_sp,
0362                 struct sctp_sock *addr_sp)
0363 {
0364     struct sctp_sockaddr_entry *laddr;
0365     int conflict = 0;
0366     struct sctp_sock *sp;
0367 
0368     /* Pick the IPv6 socket as the basis of comparison
0369      * since it's usually a superset of the IPv4.
0370      * If there is no IPv6 socket, then default to bind_addr.
0371      */
0372     if (sctp_opt2sk(bp_sp)->sk_family == AF_INET6)
0373         sp = bp_sp;
0374     else if (sctp_opt2sk(addr_sp)->sk_family == AF_INET6)
0375         sp = addr_sp;
0376     else
0377         sp = bp_sp;
0378 
0379     rcu_read_lock();
0380     list_for_each_entry_rcu(laddr, &bp->address_list, list) {
0381         if (!laddr->valid)
0382             continue;
0383 
0384         conflict = sp->pf->cmp_addr(&laddr->a, addr, sp);
0385         if (conflict)
0386             break;
0387     }
0388     rcu_read_unlock();
0389 
0390     return conflict;
0391 }
0392 
0393 /* Get the state of the entry in the bind_addr_list */
0394 int sctp_bind_addr_state(const struct sctp_bind_addr *bp,
0395              const union sctp_addr *addr)
0396 {
0397     struct sctp_sockaddr_entry *laddr;
0398     struct sctp_af *af;
0399 
0400     af = sctp_get_af_specific(addr->sa.sa_family);
0401     if (unlikely(!af))
0402         return -1;
0403 
0404     list_for_each_entry_rcu(laddr, &bp->address_list, list) {
0405         if (!laddr->valid)
0406             continue;
0407         if (af->cmp_addr(&laddr->a, addr))
0408             return laddr->state;
0409     }
0410 
0411     return -1;
0412 }
0413 
0414 /* Find the first address in the bind address list that is not present in
0415  * the addrs packed array.
0416  */
0417 union sctp_addr *sctp_find_unmatch_addr(struct sctp_bind_addr   *bp,
0418                     const union sctp_addr   *addrs,
0419                     int         addrcnt,
0420                     struct sctp_sock    *opt)
0421 {
0422     struct sctp_sockaddr_entry  *laddr;
0423     union sctp_addr         *addr;
0424     void                *addr_buf;
0425     struct sctp_af          *af;
0426     int             i;
0427 
0428     /* This is only called sctp_send_asconf_del_ip() and we hold
0429      * the socket lock in that code patch, so that address list
0430      * can't change.
0431      */
0432     list_for_each_entry(laddr, &bp->address_list, list) {
0433         addr_buf = (union sctp_addr *)addrs;
0434         for (i = 0; i < addrcnt; i++) {
0435             addr = addr_buf;
0436             af = sctp_get_af_specific(addr->v4.sin_family);
0437             if (!af)
0438                 break;
0439 
0440             if (opt->pf->cmp_addr(&laddr->a, addr, opt))
0441                 break;
0442 
0443             addr_buf += af->sockaddr_len;
0444         }
0445         if (i == addrcnt)
0446             return &laddr->a;
0447     }
0448 
0449     return NULL;
0450 }
0451 
0452 /* Copy out addresses from the global local address list. */
0453 static int sctp_copy_one_addr(struct net *net, struct sctp_bind_addr *dest,
0454                   union sctp_addr *addr, enum sctp_scope scope,
0455                   gfp_t gfp, int flags)
0456 {
0457     int error = 0;
0458 
0459     if (sctp_is_any(NULL, addr)) {
0460         error = sctp_copy_local_addr_list(net, dest, scope, gfp, flags);
0461     } else if (sctp_in_scope(net, addr, scope)) {
0462         /* Now that the address is in scope, check to see if
0463          * the address type is supported by local sock as
0464          * well as the remote peer.
0465          */
0466         if ((((AF_INET == addr->sa.sa_family) &&
0467               (flags & SCTP_ADDR4_ALLOWED) &&
0468               (flags & SCTP_ADDR4_PEERSUPP))) ||
0469             (((AF_INET6 == addr->sa.sa_family) &&
0470               (flags & SCTP_ADDR6_ALLOWED) &&
0471               (flags & SCTP_ADDR6_PEERSUPP))))
0472             error = sctp_add_bind_addr(dest, addr, sizeof(*addr),
0473                            SCTP_ADDR_SRC, gfp);
0474     }
0475 
0476     return error;
0477 }
0478 
0479 /* Is this a wildcard address?  */
0480 int sctp_is_any(struct sock *sk, const union sctp_addr *addr)
0481 {
0482     unsigned short fam = 0;
0483     struct sctp_af *af;
0484 
0485     /* Try to get the right address family */
0486     if (addr->sa.sa_family != AF_UNSPEC)
0487         fam = addr->sa.sa_family;
0488     else if (sk)
0489         fam = sk->sk_family;
0490 
0491     af = sctp_get_af_specific(fam);
0492     if (!af)
0493         return 0;
0494 
0495     return af->is_any(addr);
0496 }
0497 
0498 /* Is 'addr' valid for 'scope'?  */
0499 int sctp_in_scope(struct net *net, const union sctp_addr *addr,
0500           enum sctp_scope scope)
0501 {
0502     enum sctp_scope addr_scope = sctp_scope(addr);
0503 
0504     /* The unusable SCTP addresses will not be considered with
0505      * any defined scopes.
0506      */
0507     if (SCTP_SCOPE_UNUSABLE == addr_scope)
0508         return 0;
0509     /*
0510      * For INIT and INIT-ACK address list, let L be the level of
0511      * requested destination address, sender and receiver
0512      * SHOULD include all of its addresses with level greater
0513      * than or equal to L.
0514      *
0515      * Address scoping can be selectively controlled via sysctl
0516      * option
0517      */
0518     switch (net->sctp.scope_policy) {
0519     case SCTP_SCOPE_POLICY_DISABLE:
0520         return 1;
0521     case SCTP_SCOPE_POLICY_ENABLE:
0522         if (addr_scope <= scope)
0523             return 1;
0524         break;
0525     case SCTP_SCOPE_POLICY_PRIVATE:
0526         if (addr_scope <= scope || SCTP_SCOPE_PRIVATE == addr_scope)
0527             return 1;
0528         break;
0529     case SCTP_SCOPE_POLICY_LINK:
0530         if (addr_scope <= scope || SCTP_SCOPE_LINK == addr_scope)
0531             return 1;
0532         break;
0533     default:
0534         break;
0535     }
0536 
0537     return 0;
0538 }
0539 
0540 int sctp_is_ep_boundall(struct sock *sk)
0541 {
0542     struct sctp_bind_addr *bp;
0543     struct sctp_sockaddr_entry *addr;
0544 
0545     bp = &sctp_sk(sk)->ep->base.bind_addr;
0546     if (sctp_list_single_entry(&bp->address_list)) {
0547         addr = list_entry(bp->address_list.next,
0548                   struct sctp_sockaddr_entry, list);
0549         if (sctp_is_any(sk, &addr->a))
0550             return 1;
0551     }
0552     return 0;
0553 }
0554 
0555 /********************************************************************
0556  * 3rd Level Abstractions
0557  ********************************************************************/
0558 
0559 /* What is the scope of 'addr'?  */
0560 enum sctp_scope sctp_scope(const union sctp_addr *addr)
0561 {
0562     struct sctp_af *af;
0563 
0564     af = sctp_get_af_specific(addr->sa.sa_family);
0565     if (!af)
0566         return SCTP_SCOPE_UNUSABLE;
0567 
0568     return af->scope((union sctp_addr *)addr);
0569 }