^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) * (C) Copyright IBM Corp. 2001, 2003
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (c) Cisco 1999,2000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) Motorola 1999,2000,2001
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (c) La Monte H.P. Yarroll 2001
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * This file is part of the SCTP kernel implementation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * A collection class to handle the storage of transport addresses.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * Please send any bug reports or fixes you make to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * email address(es):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * lksctp developers <linux-sctp@vger.kernel.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * Written or modified by:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * La Monte H.P. Yarroll <piggy@acm.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * Karl Knutson <karl@athena.chicago.il.us>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * Jon Grimm <jgrimm@us.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * Daisy Chang <daisyc@us.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/in.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <net/ipv6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <net/if_inet6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <net/sctp/sctp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <net/sctp/sm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) /* Forward declarations for internal helpers. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) static int sctp_copy_one_addr(struct net *net, struct sctp_bind_addr *dest,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) union sctp_addr *addr, enum sctp_scope scope,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) gfp_t gfp, int flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static void sctp_bind_addr_clean(struct sctp_bind_addr *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) /* First Level Abstractions. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) /* Copy 'src' to 'dest' taking 'scope' into account. Omit addresses
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * in 'src' which have a broader scope than 'scope'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) int sctp_bind_addr_copy(struct net *net, struct sctp_bind_addr *dest,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) const struct sctp_bind_addr *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) enum sctp_scope scope, gfp_t gfp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct sctp_sockaddr_entry *addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) /* All addresses share the same port. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) dest->port = src->port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) /* Extract the addresses which are relevant for this scope. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) list_for_each_entry(addr, &src->address_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) error = sctp_copy_one_addr(net, dest, &addr->a, scope,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) gfp, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (error < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) goto out;
^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) /* If there are no addresses matching the scope and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * this is global scope, try to get a link scope address, with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * the assumption that we must be sitting behind a NAT.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) if (list_empty(&dest->address_list) && (SCTP_SCOPE_GLOBAL == scope)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) list_for_each_entry(addr, &src->address_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) error = sctp_copy_one_addr(net, dest, &addr->a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) SCTP_SCOPE_LINK, gfp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (error < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) sctp_bind_addr_clean(dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) /* Exactly duplicate the address lists. This is necessary when doing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * peer-offs and accepts. We don't want to put all the current system
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * addresses into the endpoint. That's useless. But we do want duplicat
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * the list of bound addresses that the older endpoint used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) int sctp_bind_addr_dup(struct sctp_bind_addr *dest,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) const struct sctp_bind_addr *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) gfp_t gfp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) struct sctp_sockaddr_entry *addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) /* All addresses share the same port. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) dest->port = src->port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) list_for_each_entry(addr, &src->address_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) error = sctp_add_bind_addr(dest, &addr->a, sizeof(addr->a),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 1, gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (error < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) /* Initialize the SCTP_bind_addr structure for either an endpoint or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) * an association.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) void sctp_bind_addr_init(struct sctp_bind_addr *bp, __u16 port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) INIT_LIST_HEAD(&bp->address_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) bp->port = port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) /* Dispose of the address list. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) static void sctp_bind_addr_clean(struct sctp_bind_addr *bp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct sctp_sockaddr_entry *addr, *temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) /* Empty the bind address list. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) list_for_each_entry_safe(addr, temp, &bp->address_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) list_del_rcu(&addr->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) kfree_rcu(addr, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) SCTP_DBG_OBJCNT_DEC(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) /* Dispose of an SCTP_bind_addr structure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) void sctp_bind_addr_free(struct sctp_bind_addr *bp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) /* Empty the bind address list. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) sctp_bind_addr_clean(bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) /* Add an address to the bind address list in the SCTP_bind_addr structure. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) int sctp_add_bind_addr(struct sctp_bind_addr *bp, union sctp_addr *new,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) int new_size, __u8 addr_state, gfp_t gfp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) struct sctp_sockaddr_entry *addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) /* Add the address to the bind address list. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) addr = kzalloc(sizeof(*addr), gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if (!addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) memcpy(&addr->a, new, min_t(size_t, sizeof(*new), new_size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) /* Fix up the port if it has not yet been set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * Both v4 and v6 have the port at the same offset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if (!addr->a.v4.sin_port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) addr->a.v4.sin_port = htons(bp->port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) addr->state = addr_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) addr->valid = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) INIT_LIST_HEAD(&addr->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) /* We always hold a socket lock when calling this function,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) * and that acts as a writer synchronizing lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) list_add_tail_rcu(&addr->list, &bp->address_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) SCTP_DBG_OBJCNT_INC(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) /* Delete an address from the bind address list in the SCTP_bind_addr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) * structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) int sctp_del_bind_addr(struct sctp_bind_addr *bp, union sctp_addr *del_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) struct sctp_sockaddr_entry *addr, *temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) int found = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) /* We hold the socket lock when calling this function,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) * and that acts as a writer synchronizing lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) list_for_each_entry_safe(addr, temp, &bp->address_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (sctp_cmp_addr_exact(&addr->a, del_addr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) /* Found the exact match. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) found = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) addr->valid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) list_del_rcu(&addr->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) if (found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) kfree_rcu(addr, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) SCTP_DBG_OBJCNT_DEC(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) /* Create a network byte-order representation of all the addresses
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) * formated as SCTP parameters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) * The second argument is the return value for the length.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) union sctp_params sctp_bind_addrs_to_raw(const struct sctp_bind_addr *bp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) int *addrs_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) gfp_t gfp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) union sctp_params addrparms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) union sctp_params retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) int addrparms_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) union sctp_addr_param rawaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) struct sctp_sockaddr_entry *addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) struct list_head *pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) struct sctp_af *af;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) addrparms_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) /* Allocate enough memory at once. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) list_for_each(pos, &bp->address_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) len += sizeof(union sctp_addr_param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) /* Don't even bother embedding an address if there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) * is only one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) if (len == sizeof(union sctp_addr_param)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) retval.v = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) goto end_raw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) retval.v = kmalloc(len, gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) if (!retval.v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) goto end_raw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) addrparms = retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) list_for_each_entry(addr, &bp->address_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) af = sctp_get_af_specific(addr->a.v4.sin_family);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) len = af->to_addr_param(&addr->a, &rawaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) memcpy(addrparms.v, &rawaddr, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) addrparms.v += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) addrparms_len += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) end_raw:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) *addrs_len = addrparms_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) * Create an address list out of the raw address list format (IPv4 and IPv6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) * address parameters).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) int sctp_raw_to_bind_addrs(struct sctp_bind_addr *bp, __u8 *raw_addr_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) int addrs_len, __u16 port, gfp_t gfp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) union sctp_addr_param *rawaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) struct sctp_paramhdr *param;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) union sctp_addr addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) int retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) struct sctp_af *af;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) /* Convert the raw address to standard address format */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) while (addrs_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) param = (struct sctp_paramhdr *)raw_addr_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) rawaddr = (union sctp_addr_param *)raw_addr_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) af = sctp_get_af_specific(param_type2af(param->type));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) if (unlikely(!af) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) !af->from_addr_param(&addr, rawaddr, htons(port), 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) retval = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) if (sctp_bind_addr_state(bp, &addr) != -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) retval = sctp_add_bind_addr(bp, &addr, sizeof(addr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) SCTP_ADDR_SRC, gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) /* Can't finish building the list, clean up. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) len = ntohs(param->length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) addrs_len -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) raw_addr_list += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) sctp_bind_addr_clean(bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) /********************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) * 2nd Level Abstractions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) ********************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) /* Does this contain a specified address? Allow wildcarding. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) int sctp_bind_addr_match(struct sctp_bind_addr *bp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) const union sctp_addr *addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) struct sctp_sock *opt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) struct sctp_sockaddr_entry *laddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) int match = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) list_for_each_entry_rcu(laddr, &bp->address_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) if (!laddr->valid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) if (opt->pf->cmp_addr(&laddr->a, addr, opt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) match = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) return match;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) int sctp_bind_addrs_check(struct sctp_sock *sp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) struct sctp_sock *sp2, int cnt2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) struct sctp_bind_addr *bp2 = &sp2->ep->base.bind_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) struct sctp_bind_addr *bp = &sp->ep->base.bind_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) struct sctp_sockaddr_entry *laddr, *laddr2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) bool exist = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) int cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) list_for_each_entry_rcu(laddr, &bp->address_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) list_for_each_entry_rcu(laddr2, &bp2->address_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) if (sp->pf->af->cmp_addr(&laddr->a, &laddr2->a) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) laddr->valid && laddr2->valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) exist = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) cnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) return (cnt == cnt2) ? 0 : (exist ? -EEXIST : 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) /* Does the address 'addr' conflict with any addresses in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) * the bp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) int sctp_bind_addr_conflict(struct sctp_bind_addr *bp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) const union sctp_addr *addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) struct sctp_sock *bp_sp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) struct sctp_sock *addr_sp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) struct sctp_sockaddr_entry *laddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) int conflict = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) struct sctp_sock *sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) /* Pick the IPv6 socket as the basis of comparison
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) * since it's usually a superset of the IPv4.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) * If there is no IPv6 socket, then default to bind_addr.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) if (sctp_opt2sk(bp_sp)->sk_family == AF_INET6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) sp = bp_sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) else if (sctp_opt2sk(addr_sp)->sk_family == AF_INET6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) sp = addr_sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) sp = bp_sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) list_for_each_entry_rcu(laddr, &bp->address_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) if (!laddr->valid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) conflict = sp->pf->cmp_addr(&laddr->a, addr, sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) if (conflict)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) return conflict;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) /* Get the state of the entry in the bind_addr_list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) int sctp_bind_addr_state(const struct sctp_bind_addr *bp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) const union sctp_addr *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) struct sctp_sockaddr_entry *laddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) struct sctp_af *af;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) af = sctp_get_af_specific(addr->sa.sa_family);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) if (unlikely(!af))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) list_for_each_entry_rcu(laddr, &bp->address_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) if (!laddr->valid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) if (af->cmp_addr(&laddr->a, addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) return laddr->state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) /* Find the first address in the bind address list that is not present in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) * the addrs packed array.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) union sctp_addr *sctp_find_unmatch_addr(struct sctp_bind_addr *bp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) const union sctp_addr *addrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) int addrcnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) struct sctp_sock *opt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) struct sctp_sockaddr_entry *laddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) union sctp_addr *addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) void *addr_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) struct sctp_af *af;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) /* This is only called sctp_send_asconf_del_ip() and we hold
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) * the socket lock in that code patch, so that address list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) * can't change.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) list_for_each_entry(laddr, &bp->address_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) addr_buf = (union sctp_addr *)addrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) for (i = 0; i < addrcnt; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) addr = addr_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) af = sctp_get_af_specific(addr->v4.sin_family);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) if (!af)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) if (opt->pf->cmp_addr(&laddr->a, addr, opt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) addr_buf += af->sockaddr_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) if (i == addrcnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) return &laddr->a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) /* Copy out addresses from the global local address list. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) static int sctp_copy_one_addr(struct net *net, struct sctp_bind_addr *dest,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) union sctp_addr *addr, enum sctp_scope scope,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) gfp_t gfp, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) if (sctp_is_any(NULL, addr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) error = sctp_copy_local_addr_list(net, dest, scope, gfp, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) } else if (sctp_in_scope(net, addr, scope)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) /* Now that the address is in scope, check to see if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) * the address type is supported by local sock as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) * well as the remote peer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) if ((((AF_INET == addr->sa.sa_family) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) (flags & SCTP_ADDR4_ALLOWED) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) (flags & SCTP_ADDR4_PEERSUPP))) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) (((AF_INET6 == addr->sa.sa_family) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) (flags & SCTP_ADDR6_ALLOWED) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) (flags & SCTP_ADDR6_PEERSUPP))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) error = sctp_add_bind_addr(dest, addr, sizeof(*addr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) SCTP_ADDR_SRC, gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) /* Is this a wildcard address? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) int sctp_is_any(struct sock *sk, const union sctp_addr *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) unsigned short fam = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) struct sctp_af *af;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) /* Try to get the right address family */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) if (addr->sa.sa_family != AF_UNSPEC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) fam = addr->sa.sa_family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) else if (sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) fam = sk->sk_family;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) af = sctp_get_af_specific(fam);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) if (!af)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) return af->is_any(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) /* Is 'addr' valid for 'scope'? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) int sctp_in_scope(struct net *net, const union sctp_addr *addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) enum sctp_scope scope)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) enum sctp_scope addr_scope = sctp_scope(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) /* The unusable SCTP addresses will not be considered with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) * any defined scopes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) if (SCTP_SCOPE_UNUSABLE == addr_scope)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) * For INIT and INIT-ACK address list, let L be the level of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) * requested destination address, sender and receiver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) * SHOULD include all of its addresses with level greater
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) * than or equal to L.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) * Address scoping can be selectively controlled via sysctl
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) * option
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) switch (net->sctp.scope_policy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) case SCTP_SCOPE_POLICY_DISABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) case SCTP_SCOPE_POLICY_ENABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) if (addr_scope <= scope)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) case SCTP_SCOPE_POLICY_PRIVATE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) if (addr_scope <= scope || SCTP_SCOPE_PRIVATE == addr_scope)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) case SCTP_SCOPE_POLICY_LINK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) if (addr_scope <= scope || SCTP_SCOPE_LINK == addr_scope)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) int sctp_is_ep_boundall(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) struct sctp_bind_addr *bp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) struct sctp_sockaddr_entry *addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) bp = &sctp_sk(sk)->ep->base.bind_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) if (sctp_list_single_entry(&bp->address_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) addr = list_entry(bp->address_list.next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) struct sctp_sockaddr_entry, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) if (sctp_is_any(sk, &addr->a))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) /********************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) * 3rd Level Abstractions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) ********************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) /* What is the scope of 'addr'? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) enum sctp_scope sctp_scope(const union sctp_addr *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) struct sctp_af *af;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) af = sctp_get_af_specific(addr->sa.sa_family);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) if (!af)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) return SCTP_SCOPE_UNUSABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) return af->scope((union sctp_addr *)addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) }