tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1 | /*- |
Michael Tuexen | 866a731 | 2017-11-24 12:44:05 +0100 | [diff] [blame] | 2 | * SPDX-License-Identifier: BSD-3-Clause |
| 3 | * |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4 | * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved. |
tuexen | 194eae1 | 2012-05-23 12:03:48 +0000 | [diff] [blame] | 5 | * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved. |
| 6 | * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved. |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions are met: |
| 10 | * |
| 11 | * a) Redistributions of source code must retain the above copyright notice, |
tuexen | 9784e9a | 2011-12-18 13:04:23 +0000 | [diff] [blame] | 12 | * this list of conditions and the following disclaimer. |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 13 | * |
| 14 | * b) Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in |
tuexen | 9784e9a | 2011-12-18 13:04:23 +0000 | [diff] [blame] | 16 | * the documentation and/or other materials provided with the distribution. |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 17 | * |
| 18 | * c) Neither the name of Cisco Systems, Inc. nor the names of its |
| 19 | * contributors may be used to endorse or promote products derived |
| 20 | * from this software without specific prior written permission. |
| 21 | * |
| 22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 24 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 26 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 28 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 31 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 32 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 33 | */ |
| 34 | |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 35 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 36 | #include <sys/cdefs.h> |
Michael Tuexen | 6e00fe2 | 2021-02-21 17:31:55 +0100 | [diff] [blame] | 37 | __FBSDID("$FreeBSD$"); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 38 | #endif |
| 39 | |
| 40 | #include <netinet/sctp_os.h> |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 41 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 42 | #include <sys/proc.h> |
| 43 | #endif |
| 44 | #include <netinet/sctp_var.h> |
| 45 | #include <netinet/sctp_sysctl.h> |
| 46 | #include <netinet/sctp_pcb.h> |
| 47 | #include <netinet/sctputil.h> |
| 48 | #include <netinet/sctp.h> |
| 49 | #include <netinet/sctp_header.h> |
| 50 | #include <netinet/sctp_asconf.h> |
| 51 | #include <netinet/sctp_output.h> |
| 52 | #include <netinet/sctp_timer.h> |
| 53 | #include <netinet/sctp_bsd_addr.h> |
t00fcxen | 1ce83bb | 2014-07-11 07:17:36 +0000 | [diff] [blame] | 54 | #if defined(INET) || defined(INET6) |
Michael Tuexen | eb61157 | 2020-06-13 17:27:02 +0200 | [diff] [blame] | 55 | #if !defined(_WIN32) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 56 | #include <netinet/udp.h> |
tuexen | 6019b07 | 2011-12-15 18:38:03 +0000 | [diff] [blame] | 57 | #endif |
t00fcxen | 5ab37c8 | 2014-06-30 20:55:44 +0000 | [diff] [blame] | 58 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 59 | #ifdef INET6 |
tuexen | 5154a00 | 2011-12-23 18:47:55 +0000 | [diff] [blame] | 60 | #if defined(__Userspace__) |
| 61 | #include "user_ip6_var.h" |
| 62 | #else |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 63 | #include <netinet6/ip6_var.h> |
| 64 | #endif |
tuexen | 5154a00 | 2011-12-23 18:47:55 +0000 | [diff] [blame] | 65 | #endif |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 66 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 67 | #include <sys/sched.h> |
| 68 | #include <sys/smp.h> |
tuexen | c0bb612 | 2012-01-15 14:21:21 +0000 | [diff] [blame] | 69 | #include <sys/unistd.h> |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 70 | #endif |
tuexen | 5154a00 | 2011-12-23 18:47:55 +0000 | [diff] [blame] | 71 | #if defined(__Userspace__) |
| 72 | #include <user_socketvar.h> |
Michael Tuexen | 1a0619d | 2019-07-28 22:10:44 +0200 | [diff] [blame] | 73 | #include <user_atomic.h> |
Michael Tuexen | eb61157 | 2020-06-13 17:27:02 +0200 | [diff] [blame] | 74 | #if !defined(_WIN32) |
t00fcxen | 37058da | 2015-03-16 21:59:21 +0000 | [diff] [blame] | 75 | #include <netdb.h> |
| 76 | #endif |
tuexen | 5154a00 | 2011-12-23 18:47:55 +0000 | [diff] [blame] | 77 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 78 | |
Michael Tuexen | 47987ce | 2020-06-22 16:41:44 +0200 | [diff] [blame] | 79 | #if !defined(__FreeBSD__) || defined(__Userspace__) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 80 | struct sctp_base_info system_base_info; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 81 | |
Michael Tuexen | 47987ce | 2020-06-22 16:41:44 +0200 | [diff] [blame] | 82 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 83 | /* FIX: we don't handle multiple link local scopes */ |
| 84 | /* "scopeless" replacement IN6_ARE_ADDR_EQUAL */ |
| 85 | #ifdef INET6 |
| 86 | int |
| 87 | SCTP6_ARE_ADDR_EQUAL(struct sockaddr_in6 *a, struct sockaddr_in6 *b) |
| 88 | { |
| 89 | #ifdef SCTP_EMBEDDED_V6_SCOPE |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 90 | #if defined(__APPLE__) && !defined(__Userspace__) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 91 | struct in6_addr tmp_a, tmp_b; |
| 92 | |
| 93 | tmp_a = a->sin6_addr; |
tuexen | 9a218b1 | 2012-08-04 21:17:58 +0000 | [diff] [blame] | 94 | #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 95 | if (in6_embedscope(&tmp_a, a, NULL, NULL) != 0) { |
tuexen | 9a218b1 | 2012-08-04 21:17:58 +0000 | [diff] [blame] | 96 | #else |
| 97 | if (in6_embedscope(&tmp_a, a, NULL, NULL, NULL) != 0) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 98 | #endif |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 99 | return (0); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 100 | } |
| 101 | tmp_b = b->sin6_addr; |
tuexen | 9a218b1 | 2012-08-04 21:17:58 +0000 | [diff] [blame] | 102 | #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 103 | if (in6_embedscope(&tmp_b, b, NULL, NULL) != 0) { |
tuexen | 9a218b1 | 2012-08-04 21:17:58 +0000 | [diff] [blame] | 104 | #else |
| 105 | if (in6_embedscope(&tmp_b, b, NULL, NULL, NULL) != 0) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 106 | #endif |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 107 | return (0); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 108 | } |
| 109 | return (IN6_ARE_ADDR_EQUAL(&tmp_a, &tmp_b)); |
| 110 | #elif defined(SCTP_KAME) |
| 111 | struct sockaddr_in6 tmp_a, tmp_b; |
| 112 | |
| 113 | memcpy(&tmp_a, a, sizeof(struct sockaddr_in6)); |
| 114 | if (sa6_embedscope(&tmp_a, MODULE_GLOBAL(ip6_use_defzone)) != 0) { |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 115 | return (0); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 116 | } |
| 117 | memcpy(&tmp_b, b, sizeof(struct sockaddr_in6)); |
| 118 | if (sa6_embedscope(&tmp_b, MODULE_GLOBAL(ip6_use_defzone)) != 0) { |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 119 | return (0); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 120 | } |
| 121 | return (IN6_ARE_ADDR_EQUAL(&tmp_a.sin6_addr, &tmp_b.sin6_addr)); |
| 122 | #else |
| 123 | struct in6_addr tmp_a, tmp_b; |
| 124 | |
| 125 | tmp_a = a->sin6_addr; |
| 126 | if (in6_embedscope(&tmp_a, a) != 0) { |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 127 | return (0); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 128 | } |
| 129 | tmp_b = b->sin6_addr; |
| 130 | if (in6_embedscope(&tmp_b, b) != 0) { |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 131 | return (0); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 132 | } |
| 133 | return (IN6_ARE_ADDR_EQUAL(&tmp_a, &tmp_b)); |
| 134 | #endif |
| 135 | #else |
tuexen | 5154a00 | 2011-12-23 18:47:55 +0000 | [diff] [blame] | 136 | return (IN6_ARE_ADDR_EQUAL(&(a->sin6_addr), &(b->sin6_addr))); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 137 | #endif /* SCTP_EMBEDDED_V6_SCOPE */ |
| 138 | } |
| 139 | #endif |
| 140 | |
| 141 | void |
| 142 | sctp_fill_pcbinfo(struct sctp_pcbinfo *spcb) |
| 143 | { |
| 144 | /* |
| 145 | * We really don't need to lock this, but I will just because it |
| 146 | * does not hurt. |
| 147 | */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 148 | SCTP_INP_INFO_RLOCK(); |
| 149 | spcb->ep_count = SCTP_BASE_INFO(ipi_count_ep); |
| 150 | spcb->asoc_count = SCTP_BASE_INFO(ipi_count_asoc); |
| 151 | spcb->laddr_count = SCTP_BASE_INFO(ipi_count_laddr); |
| 152 | spcb->raddr_count = SCTP_BASE_INFO(ipi_count_raddr); |
| 153 | spcb->chk_count = SCTP_BASE_INFO(ipi_count_chunk); |
| 154 | spcb->readq_count = SCTP_BASE_INFO(ipi_count_readq); |
| 155 | spcb->stream_oque = SCTP_BASE_INFO(ipi_count_strmoq); |
| 156 | spcb->free_chunks = SCTP_BASE_INFO(ipi_free_chunks); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 157 | SCTP_INP_INFO_RUNLOCK(); |
| 158 | } |
| 159 | |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 160 | /*- |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 161 | * Addresses are added to VRF's (Virtual Router's). For BSD we |
| 162 | * have only the default VRF 0. We maintain a hash list of |
| 163 | * VRF's. Each VRF has its own list of sctp_ifn's. Each of |
| 164 | * these has a list of addresses. When we add a new address |
| 165 | * to a VRF we lookup the ifn/ifn_index, if the ifn does |
| 166 | * not exist we create it and add it to the list of IFN's |
| 167 | * within the VRF. Once we have the sctp_ifn, we add the |
| 168 | * address to the list. So we look something like: |
| 169 | * |
| 170 | * hash-vrf-table |
| 171 | * vrf-> ifn-> ifn -> ifn |
| 172 | * vrf | |
| 173 | * ... +--ifa-> ifa -> ifa |
| 174 | * vrf |
| 175 | * |
| 176 | * We keep these separate lists since the SCTP subsystem will |
| 177 | * point to these from its source address selection nets structure. |
| 178 | * When an address is deleted it does not happen right away on |
| 179 | * the SCTP side, it gets scheduled. What we do when a |
| 180 | * delete happens is immediately remove the address from |
| 181 | * the master list and decrement the refcount. As our |
| 182 | * addip iterator works through and frees the src address |
| 183 | * selection pointing to the sctp_ifa, eventually the refcount |
| 184 | * will reach 0 and we will delete it. Note that it is assumed |
| 185 | * that any locking on system level ifn/ifa is done at the |
| 186 | * caller of these functions and these routines will only |
| 187 | * lock the SCTP structures as they add or delete things. |
| 188 | * |
| 189 | * Other notes on VRF concepts. |
| 190 | * - An endpoint can be in multiple VRF's |
| 191 | * - An association lives within a VRF and only one VRF. |
| 192 | * - Any incoming packet we can deduce the VRF for by |
| 193 | * looking at the mbuf/pak inbound (for BSD its VRF=0 :D) |
| 194 | * - Any downward send call or connect call must supply the |
| 195 | * VRF via ancillary data or via some sort of set default |
| 196 | * VRF socket option call (again for BSD no brainer since |
| 197 | * the VRF is always 0). |
| 198 | * - An endpoint may add multiple VRF's to it. |
| 199 | * - Listening sockets can accept associations in any |
| 200 | * of the VRF's they are in but the assoc will end up |
| 201 | * in only one VRF (gotten from the packet or connect/send). |
| 202 | * |
| 203 | */ |
| 204 | |
| 205 | struct sctp_vrf * |
| 206 | sctp_allocate_vrf(int vrf_id) |
| 207 | { |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 208 | struct sctp_vrf *vrf = NULL; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 209 | struct sctp_vrflist *bucket; |
| 210 | |
| 211 | /* First allocate the VRF structure */ |
| 212 | vrf = sctp_find_vrf(vrf_id); |
| 213 | if (vrf) { |
| 214 | /* Already allocated */ |
| 215 | return (vrf); |
| 216 | } |
| 217 | SCTP_MALLOC(vrf, struct sctp_vrf *, sizeof(struct sctp_vrf), |
| 218 | SCTP_M_VRF); |
t00fcxen | 8dd95b8 | 2014-07-11 20:41:28 +0000 | [diff] [blame] | 219 | if (vrf == NULL) { |
| 220 | /* No memory */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 221 | #ifdef INVARIANTS |
| 222 | panic("No memory for VRF:%d", vrf_id); |
| 223 | #endif |
| 224 | return (NULL); |
| 225 | } |
| 226 | /* setup the VRF */ |
| 227 | memset(vrf, 0, sizeof(struct sctp_vrf)); |
| 228 | vrf->vrf_id = vrf_id; |
| 229 | LIST_INIT(&vrf->ifnlist); |
| 230 | vrf->total_ifa_count = 0; |
| 231 | vrf->refcount = 0; |
| 232 | /* now also setup table ids */ |
| 233 | SCTP_INIT_VRF_TABLEID(vrf); |
| 234 | /* Init the HASH of addresses */ |
| 235 | vrf->vrf_addr_hash = SCTP_HASH_INIT(SCTP_VRF_ADDR_HASH_SIZE, |
| 236 | &vrf->vrf_addr_hashmark); |
| 237 | if (vrf->vrf_addr_hash == NULL) { |
t00fcxen | 8dd95b8 | 2014-07-11 20:41:28 +0000 | [diff] [blame] | 238 | /* No memory */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 239 | #ifdef INVARIANTS |
| 240 | panic("No memory for VRF:%d", vrf_id); |
| 241 | #endif |
| 242 | SCTP_FREE(vrf, SCTP_M_VRF); |
| 243 | return (NULL); |
| 244 | } |
| 245 | |
| 246 | /* Add it to the hash table */ |
| 247 | bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(vrf_id & SCTP_BASE_INFO(hashvrfmark))]; |
| 248 | LIST_INSERT_HEAD(bucket, vrf, next_vrf); |
| 249 | atomic_add_int(&SCTP_BASE_INFO(ipi_count_vrfs), 1); |
| 250 | return (vrf); |
| 251 | } |
| 252 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 253 | struct sctp_ifn * |
| 254 | sctp_find_ifn(void *ifn, uint32_t ifn_index) |
| 255 | { |
| 256 | struct sctp_ifn *sctp_ifnp; |
| 257 | struct sctp_ifnlist *hash_ifn_head; |
| 258 | |
| 259 | /* We assume the lock is held for the addresses |
| 260 | * if that's wrong problems could occur :-) |
| 261 | */ |
Michael Tuexen | 2c5953e | 2020-07-17 17:25:19 +0200 | [diff] [blame] | 262 | SCTP_IPI_ADDR_LOCK_ASSERT(); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 263 | hash_ifn_head = &SCTP_BASE_INFO(vrf_ifn_hash)[(ifn_index & SCTP_BASE_INFO(vrf_ifn_hashmark))]; |
| 264 | LIST_FOREACH(sctp_ifnp, hash_ifn_head, next_bucket) { |
| 265 | if (sctp_ifnp->ifn_index == ifn_index) { |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 266 | return (sctp_ifnp); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 267 | } |
| 268 | if (sctp_ifnp->ifn_p && ifn && (sctp_ifnp->ifn_p == ifn)) { |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 269 | return (sctp_ifnp); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 270 | } |
| 271 | } |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 272 | return (NULL); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 273 | } |
| 274 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 275 | struct sctp_vrf * |
| 276 | sctp_find_vrf(uint32_t vrf_id) |
| 277 | { |
| 278 | struct sctp_vrflist *bucket; |
| 279 | struct sctp_vrf *liste; |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 280 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 281 | bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(vrf_id & SCTP_BASE_INFO(hashvrfmark))]; |
| 282 | LIST_FOREACH(liste, bucket, next_vrf) { |
| 283 | if (vrf_id == liste->vrf_id) { |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 284 | return (liste); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | return (NULL); |
| 288 | } |
| 289 | |
| 290 | void |
| 291 | sctp_free_vrf(struct sctp_vrf *vrf) |
| 292 | { |
| 293 | if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&vrf->refcount)) { |
| 294 | if (vrf->vrf_addr_hash) { |
| 295 | SCTP_HASH_FREE(vrf->vrf_addr_hash, vrf->vrf_addr_hashmark); |
| 296 | vrf->vrf_addr_hash = NULL; |
| 297 | } |
| 298 | /* We zero'd the count */ |
| 299 | LIST_REMOVE(vrf, next_vrf); |
| 300 | SCTP_FREE(vrf, SCTP_M_VRF); |
| 301 | atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_vrfs), 1); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | void |
| 306 | sctp_free_ifn(struct sctp_ifn *sctp_ifnp) |
| 307 | { |
| 308 | if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&sctp_ifnp->refcount)) { |
| 309 | /* We zero'd the count */ |
| 310 | if (sctp_ifnp->vrf) { |
| 311 | sctp_free_vrf(sctp_ifnp->vrf); |
| 312 | } |
| 313 | SCTP_FREE(sctp_ifnp, SCTP_M_IFN); |
| 314 | atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_ifns), 1); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | void |
| 319 | sctp_update_ifn_mtu(uint32_t ifn_index, uint32_t mtu) |
| 320 | { |
| 321 | struct sctp_ifn *sctp_ifnp; |
| 322 | |
| 323 | sctp_ifnp = sctp_find_ifn((void *)NULL, ifn_index); |
| 324 | if (sctp_ifnp != NULL) { |
| 325 | sctp_ifnp->ifn_mtu = mtu; |
| 326 | } |
| 327 | } |
| 328 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 329 | void |
| 330 | sctp_free_ifa(struct sctp_ifa *sctp_ifap) |
| 331 | { |
| 332 | if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&sctp_ifap->refcount)) { |
| 333 | /* We zero'd the count */ |
| 334 | if (sctp_ifap->ifn_p) { |
| 335 | sctp_free_ifn(sctp_ifap->ifn_p); |
| 336 | } |
| 337 | SCTP_FREE(sctp_ifap, SCTP_M_IFA); |
| 338 | atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_ifas), 1); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | static void |
| 343 | sctp_delete_ifn(struct sctp_ifn *sctp_ifnp, int hold_addr_lock) |
| 344 | { |
| 345 | struct sctp_ifn *found; |
| 346 | |
| 347 | found = sctp_find_ifn(sctp_ifnp->ifn_p, sctp_ifnp->ifn_index); |
| 348 | if (found == NULL) { |
| 349 | /* Not in the list.. sorry */ |
| 350 | return; |
| 351 | } |
Michael Tuexen | 2c5953e | 2020-07-17 17:25:19 +0200 | [diff] [blame] | 352 | if (hold_addr_lock == 0) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 353 | SCTP_IPI_ADDR_WLOCK(); |
Michael Tuexen | 2c5953e | 2020-07-17 17:25:19 +0200 | [diff] [blame] | 354 | } else { |
| 355 | SCTP_IPI_ADDR_WLOCK_ASSERT(); |
| 356 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 357 | LIST_REMOVE(sctp_ifnp, next_bucket); |
| 358 | LIST_REMOVE(sctp_ifnp, next_ifn); |
Michael Tuexen | 2c5953e | 2020-07-17 17:25:19 +0200 | [diff] [blame] | 359 | if (hold_addr_lock == 0) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 360 | SCTP_IPI_ADDR_WUNLOCK(); |
Michael Tuexen | 2c5953e | 2020-07-17 17:25:19 +0200 | [diff] [blame] | 361 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 362 | /* Take away the reference, and possibly free it */ |
| 363 | sctp_free_ifn(sctp_ifnp); |
| 364 | } |
| 365 | |
| 366 | void |
| 367 | sctp_mark_ifa_addr_down(uint32_t vrf_id, struct sockaddr *addr, |
| 368 | const char *if_name, uint32_t ifn_index) |
| 369 | { |
| 370 | struct sctp_vrf *vrf; |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 371 | struct sctp_ifa *sctp_ifap; |
| 372 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 373 | SCTP_IPI_ADDR_RLOCK(); |
| 374 | vrf = sctp_find_vrf(vrf_id); |
| 375 | if (vrf == NULL) { |
| 376 | SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id); |
| 377 | goto out; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 378 | } |
| 379 | sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED); |
| 380 | if (sctp_ifap == NULL) { |
| 381 | SCTPDBG(SCTP_DEBUG_PCB4, "Can't find sctp_ifap for address\n"); |
| 382 | goto out; |
tuexen | 0e81394 | 2011-11-18 09:18:32 +0000 | [diff] [blame] | 383 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 384 | if (sctp_ifap->ifn_p == NULL) { |
Michael Tuexen | 34488e7 | 2016-05-03 22:11:59 +0200 | [diff] [blame] | 385 | SCTPDBG(SCTP_DEBUG_PCB4, "IFA has no IFN - can't mark unusable\n"); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 386 | goto out; |
| 387 | } |
| 388 | if (if_name) { |
tuexen | 0e81394 | 2011-11-18 09:18:32 +0000 | [diff] [blame] | 389 | if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, SCTP_IFNAMSIZ) != 0) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 390 | SCTPDBG(SCTP_DEBUG_PCB4, "IFN %s of IFA not the same as %s\n", |
tuexen | 0e81394 | 2011-11-18 09:18:32 +0000 | [diff] [blame] | 391 | sctp_ifap->ifn_p->ifn_name, if_name); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 392 | goto out; |
| 393 | } |
| 394 | } else { |
| 395 | if (sctp_ifap->ifn_p->ifn_index != ifn_index) { |
| 396 | SCTPDBG(SCTP_DEBUG_PCB4, "IFA owned by ifn_index:%d down command for ifn_index:%d - ignored\n", |
| 397 | sctp_ifap->ifn_p->ifn_index, ifn_index); |
| 398 | goto out; |
| 399 | } |
| 400 | } |
tuexen | 0e81394 | 2011-11-18 09:18:32 +0000 | [diff] [blame] | 401 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 402 | sctp_ifap->localifa_flags &= (~SCTP_ADDR_VALID); |
| 403 | sctp_ifap->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE; |
| 404 | out: |
| 405 | SCTP_IPI_ADDR_RUNLOCK(); |
| 406 | } |
| 407 | |
| 408 | void |
| 409 | sctp_mark_ifa_addr_up(uint32_t vrf_id, struct sockaddr *addr, |
| 410 | const char *if_name, uint32_t ifn_index) |
| 411 | { |
| 412 | struct sctp_vrf *vrf; |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 413 | struct sctp_ifa *sctp_ifap; |
| 414 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 415 | SCTP_IPI_ADDR_RLOCK(); |
| 416 | vrf = sctp_find_vrf(vrf_id); |
| 417 | if (vrf == NULL) { |
| 418 | SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id); |
| 419 | goto out; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 420 | } |
| 421 | sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED); |
| 422 | if (sctp_ifap == NULL) { |
| 423 | SCTPDBG(SCTP_DEBUG_PCB4, "Can't find sctp_ifap for address\n"); |
| 424 | goto out; |
tuexen | 0e81394 | 2011-11-18 09:18:32 +0000 | [diff] [blame] | 425 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 426 | if (sctp_ifap->ifn_p == NULL) { |
Michael Tuexen | 34488e7 | 2016-05-03 22:11:59 +0200 | [diff] [blame] | 427 | SCTPDBG(SCTP_DEBUG_PCB4, "IFA has no IFN - can't mark unusable\n"); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 428 | goto out; |
| 429 | } |
| 430 | if (if_name) { |
tuexen | 0e81394 | 2011-11-18 09:18:32 +0000 | [diff] [blame] | 431 | if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, SCTP_IFNAMSIZ) != 0) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 432 | SCTPDBG(SCTP_DEBUG_PCB4, "IFN %s of IFA not the same as %s\n", |
tuexen | 0e81394 | 2011-11-18 09:18:32 +0000 | [diff] [blame] | 433 | sctp_ifap->ifn_p->ifn_name, if_name); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 434 | goto out; |
| 435 | } |
| 436 | } else { |
| 437 | if (sctp_ifap->ifn_p->ifn_index != ifn_index) { |
| 438 | SCTPDBG(SCTP_DEBUG_PCB4, "IFA owned by ifn_index:%d down command for ifn_index:%d - ignored\n", |
| 439 | sctp_ifap->ifn_p->ifn_index, ifn_index); |
| 440 | goto out; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | sctp_ifap->localifa_flags &= (~SCTP_ADDR_IFA_UNUSEABLE); |
| 445 | sctp_ifap->localifa_flags |= SCTP_ADDR_VALID; |
| 446 | out: |
| 447 | SCTP_IPI_ADDR_RUNLOCK(); |
| 448 | } |
| 449 | |
| 450 | /*- |
| 451 | * Add an ifa to an ifn. |
| 452 | * Register the interface as necessary. |
| 453 | * NOTE: ADDR write lock MUST be held. |
| 454 | */ |
| 455 | static void |
| 456 | sctp_add_ifa_to_ifn(struct sctp_ifn *sctp_ifnp, struct sctp_ifa *sctp_ifap) |
| 457 | { |
| 458 | int ifa_af; |
| 459 | |
| 460 | LIST_INSERT_HEAD(&sctp_ifnp->ifalist, sctp_ifap, next_ifa); |
| 461 | sctp_ifap->ifn_p = sctp_ifnp; |
| 462 | atomic_add_int(&sctp_ifap->ifn_p->refcount, 1); |
| 463 | /* update address counts */ |
| 464 | sctp_ifnp->ifa_count++; |
| 465 | ifa_af = sctp_ifap->address.sa.sa_family; |
| 466 | switch (ifa_af) { |
| 467 | #ifdef INET |
| 468 | case AF_INET: |
| 469 | sctp_ifnp->num_v4++; |
| 470 | break; |
| 471 | #endif |
| 472 | #ifdef INET6 |
| 473 | case AF_INET6: |
| 474 | sctp_ifnp->num_v6++; |
| 475 | break; |
| 476 | #endif |
| 477 | default: |
| 478 | break; |
| 479 | } |
| 480 | if (sctp_ifnp->ifa_count == 1) { |
| 481 | /* register the new interface */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 482 | sctp_ifnp->registered_af = ifa_af; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | /*- |
| 487 | * Remove an ifa from its ifn. |
| 488 | * If no more addresses exist, remove the ifn too. Otherwise, re-register |
| 489 | * the interface based on the remaining address families left. |
| 490 | * NOTE: ADDR write lock MUST be held. |
| 491 | */ |
| 492 | static void |
| 493 | sctp_remove_ifa_from_ifn(struct sctp_ifa *sctp_ifap) |
| 494 | { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 495 | LIST_REMOVE(sctp_ifap, next_ifa); |
| 496 | if (sctp_ifap->ifn_p) { |
| 497 | /* update address counts */ |
| 498 | sctp_ifap->ifn_p->ifa_count--; |
| 499 | switch (sctp_ifap->address.sa.sa_family) { |
| 500 | #ifdef INET |
| 501 | case AF_INET: |
| 502 | sctp_ifap->ifn_p->num_v4--; |
| 503 | break; |
| 504 | #endif |
| 505 | #ifdef INET6 |
| 506 | case AF_INET6: |
| 507 | sctp_ifap->ifn_p->num_v6--; |
| 508 | break; |
| 509 | #endif |
| 510 | default: |
| 511 | break; |
| 512 | } |
| 513 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 514 | if (LIST_EMPTY(&sctp_ifap->ifn_p->ifalist)) { |
| 515 | /* remove the ifn, possibly freeing it */ |
| 516 | sctp_delete_ifn(sctp_ifap->ifn_p, SCTP_ADDR_LOCKED); |
| 517 | } else { |
| 518 | /* re-register address family type, if needed */ |
| 519 | if ((sctp_ifap->ifn_p->num_v6 == 0) && |
| 520 | (sctp_ifap->ifn_p->registered_af == AF_INET6)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 521 | sctp_ifap->ifn_p->registered_af = AF_INET; |
| 522 | } else if ((sctp_ifap->ifn_p->num_v4 == 0) && |
| 523 | (sctp_ifap->ifn_p->registered_af == AF_INET)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 524 | sctp_ifap->ifn_p->registered_af = AF_INET6; |
| 525 | } |
| 526 | /* free the ifn refcount */ |
| 527 | sctp_free_ifn(sctp_ifap->ifn_p); |
| 528 | } |
| 529 | sctp_ifap->ifn_p = NULL; |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | struct sctp_ifa * |
| 534 | sctp_add_addr_to_vrf(uint32_t vrf_id, void *ifn, uint32_t ifn_index, |
| 535 | uint32_t ifn_type, const char *if_name, void *ifa, |
| 536 | struct sockaddr *addr, uint32_t ifa_flags, |
| 537 | int dynamic_add) |
| 538 | { |
| 539 | struct sctp_vrf *vrf; |
Michael Tuexen | 2c5953e | 2020-07-17 17:25:19 +0200 | [diff] [blame] | 540 | struct sctp_ifn *sctp_ifnp, *new_sctp_ifnp; |
| 541 | struct sctp_ifa *sctp_ifap, *new_sctp_ifap; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 542 | struct sctp_ifalist *hash_addr_head; |
| 543 | struct sctp_ifnlist *hash_ifn_head; |
| 544 | uint32_t hash_of_addr; |
| 545 | int new_ifn_af = 0; |
| 546 | |
| 547 | #ifdef SCTP_DEBUG |
| 548 | SCTPDBG(SCTP_DEBUG_PCB4, "vrf_id 0x%x: adding address: ", vrf_id); |
| 549 | SCTPDBG_ADDR(SCTP_DEBUG_PCB4, addr); |
| 550 | #endif |
Michael Tuexen | 2c5953e | 2020-07-17 17:25:19 +0200 | [diff] [blame] | 551 | SCTP_MALLOC(new_sctp_ifnp, struct sctp_ifn *, |
| 552 | sizeof(struct sctp_ifn), SCTP_M_IFN); |
| 553 | if (new_sctp_ifnp == NULL) { |
| 554 | #ifdef INVARIANTS |
| 555 | panic("No memory for IFN"); |
| 556 | #endif |
| 557 | return (NULL); |
| 558 | } |
| 559 | SCTP_MALLOC(new_sctp_ifap, struct sctp_ifa *, sizeof(struct sctp_ifa), SCTP_M_IFA); |
| 560 | if (new_sctp_ifap == NULL) { |
| 561 | #ifdef INVARIANTS |
| 562 | panic("No memory for IFA"); |
| 563 | #endif |
| 564 | SCTP_FREE(new_sctp_ifnp, SCTP_M_IFN); |
| 565 | return (NULL); |
| 566 | } |
| 567 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 568 | SCTP_IPI_ADDR_WLOCK(); |
| 569 | sctp_ifnp = sctp_find_ifn(ifn, ifn_index); |
| 570 | if (sctp_ifnp) { |
| 571 | vrf = sctp_ifnp->vrf; |
| 572 | } else { |
| 573 | vrf = sctp_find_vrf(vrf_id); |
| 574 | if (vrf == NULL) { |
| 575 | vrf = sctp_allocate_vrf(vrf_id); |
| 576 | if (vrf == NULL) { |
| 577 | SCTP_IPI_ADDR_WUNLOCK(); |
Michael Tuexen | 2c5953e | 2020-07-17 17:25:19 +0200 | [diff] [blame] | 578 | SCTP_FREE(new_sctp_ifnp, SCTP_M_IFN); |
| 579 | SCTP_FREE(new_sctp_ifap, SCTP_M_IFA); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 580 | return (NULL); |
| 581 | } |
| 582 | } |
| 583 | } |
| 584 | if (sctp_ifnp == NULL) { |
| 585 | /* build one and add it, can't hold lock |
| 586 | * until after malloc done though. |
| 587 | */ |
Michael Tuexen | 2c5953e | 2020-07-17 17:25:19 +0200 | [diff] [blame] | 588 | sctp_ifnp = new_sctp_ifnp; |
| 589 | new_sctp_ifnp = NULL; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 590 | memset(sctp_ifnp, 0, sizeof(struct sctp_ifn)); |
| 591 | sctp_ifnp->ifn_index = ifn_index; |
| 592 | sctp_ifnp->ifn_p = ifn; |
| 593 | sctp_ifnp->ifn_type = ifn_type; |
| 594 | sctp_ifnp->refcount = 0; |
| 595 | sctp_ifnp->vrf = vrf; |
| 596 | atomic_add_int(&vrf->refcount, 1); |
Michael Tuexen | ed352c4 | 2022-02-18 15:23:58 +0100 | [diff] [blame] | 597 | sctp_ifnp->ifn_mtu = SCTP_GATHER_MTU_FROM_IFN_INFO(ifn, ifn_index); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 598 | if (if_name != NULL) { |
Michael Tuexen | edd369d | 2020-05-19 09:42:15 +0200 | [diff] [blame] | 599 | SCTP_SNPRINTF(sctp_ifnp->ifn_name, SCTP_IFNAMSIZ, "%s", if_name); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 600 | } else { |
Michael Tuexen | edd369d | 2020-05-19 09:42:15 +0200 | [diff] [blame] | 601 | SCTP_SNPRINTF(sctp_ifnp->ifn_name, SCTP_IFNAMSIZ, "%s", "unknown"); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 602 | } |
| 603 | hash_ifn_head = &SCTP_BASE_INFO(vrf_ifn_hash)[(ifn_index & SCTP_BASE_INFO(vrf_ifn_hashmark))]; |
| 604 | LIST_INIT(&sctp_ifnp->ifalist); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 605 | LIST_INSERT_HEAD(hash_ifn_head, sctp_ifnp, next_bucket); |
| 606 | LIST_INSERT_HEAD(&vrf->ifnlist, sctp_ifnp, next_ifn); |
| 607 | atomic_add_int(&SCTP_BASE_INFO(ipi_count_ifns), 1); |
| 608 | new_ifn_af = 1; |
| 609 | } |
| 610 | sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED); |
| 611 | if (sctp_ifap) { |
| 612 | /* Hmm, it already exists? */ |
| 613 | if ((sctp_ifap->ifn_p) && |
| 614 | (sctp_ifap->ifn_p->ifn_index == ifn_index)) { |
| 615 | SCTPDBG(SCTP_DEBUG_PCB4, "Using existing ifn %s (0x%x) for ifa %p\n", |
| 616 | sctp_ifap->ifn_p->ifn_name, ifn_index, |
t00fcxen | 8fca7ff | 2012-09-05 18:58:42 +0000 | [diff] [blame] | 617 | (void *)sctp_ifap); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 618 | if (new_ifn_af) { |
| 619 | /* Remove the created one that we don't want */ |
| 620 | sctp_delete_ifn(sctp_ifnp, SCTP_ADDR_LOCKED); |
| 621 | } |
| 622 | if (sctp_ifap->localifa_flags & SCTP_BEING_DELETED) { |
| 623 | /* easy to solve, just switch back to active */ |
| 624 | SCTPDBG(SCTP_DEBUG_PCB4, "Clearing deleted ifa flag\n"); |
| 625 | sctp_ifap->localifa_flags = SCTP_ADDR_VALID; |
| 626 | sctp_ifap->ifn_p = sctp_ifnp; |
| 627 | atomic_add_int(&sctp_ifap->ifn_p->refcount, 1); |
| 628 | } |
| 629 | exit_stage_left: |
| 630 | SCTP_IPI_ADDR_WUNLOCK(); |
Michael Tuexen | 2c5953e | 2020-07-17 17:25:19 +0200 | [diff] [blame] | 631 | if (new_sctp_ifnp != NULL) { |
| 632 | SCTP_FREE(new_sctp_ifnp, SCTP_M_IFN); |
| 633 | } |
| 634 | SCTP_FREE(new_sctp_ifap, SCTP_M_IFA); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 635 | return (sctp_ifap); |
| 636 | } else { |
| 637 | if (sctp_ifap->ifn_p) { |
| 638 | /* |
| 639 | * The last IFN gets the address, remove the |
| 640 | * old one |
| 641 | */ |
| 642 | SCTPDBG(SCTP_DEBUG_PCB4, "Moving ifa %p from %s (0x%x) to %s (0x%x)\n", |
t00fcxen | 8fca7ff | 2012-09-05 18:58:42 +0000 | [diff] [blame] | 643 | (void *)sctp_ifap, sctp_ifap->ifn_p->ifn_name, |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 644 | sctp_ifap->ifn_p->ifn_index, if_name, |
| 645 | ifn_index); |
| 646 | /* remove the address from the old ifn */ |
| 647 | sctp_remove_ifa_from_ifn(sctp_ifap); |
| 648 | /* move the address over to the new ifn */ |
| 649 | sctp_add_ifa_to_ifn(sctp_ifnp, sctp_ifap); |
t00fcxen | 8dd95b8 | 2014-07-11 20:41:28 +0000 | [diff] [blame] | 650 | goto exit_stage_left; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 651 | } else { |
| 652 | /* repair ifnp which was NULL ? */ |
| 653 | sctp_ifap->localifa_flags = SCTP_ADDR_VALID; |
| 654 | SCTPDBG(SCTP_DEBUG_PCB4, "Repairing ifn %p for ifa %p\n", |
t00fcxen | 8fca7ff | 2012-09-05 18:58:42 +0000 | [diff] [blame] | 655 | (void *)sctp_ifnp, (void *)sctp_ifap); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 656 | sctp_add_ifa_to_ifn(sctp_ifnp, sctp_ifap); |
| 657 | } |
| 658 | goto exit_stage_left; |
| 659 | } |
| 660 | } |
Michael Tuexen | 2c5953e | 2020-07-17 17:25:19 +0200 | [diff] [blame] | 661 | sctp_ifap = new_sctp_ifap; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 662 | memset(sctp_ifap, 0, sizeof(struct sctp_ifa)); |
| 663 | sctp_ifap->ifn_p = sctp_ifnp; |
| 664 | atomic_add_int(&sctp_ifnp->refcount, 1); |
| 665 | sctp_ifap->vrf_id = vrf_id; |
| 666 | sctp_ifap->ifa = ifa; |
t00fcxen | a8a26f6 | 2012-09-12 19:24:54 +0000 | [diff] [blame] | 667 | #ifdef HAVE_SA_LEN |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 668 | memcpy(&sctp_ifap->address, addr, addr->sa_len); |
| 669 | #else |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 670 | switch (addr->sa_family) { |
| 671 | #ifdef INET |
| 672 | case AF_INET: |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 673 | memcpy(&sctp_ifap->address, addr, sizeof(struct sockaddr_in)); |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 674 | break; |
| 675 | #endif |
| 676 | #ifdef INET6 |
| 677 | case AF_INET6: |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 678 | memcpy(&sctp_ifap->address, addr, sizeof(struct sockaddr_in6)); |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 679 | break; |
| 680 | #endif |
| 681 | #if defined(__Userspace__) |
| 682 | case AF_CONN: |
t00fcxen | 6df8431 | 2013-02-12 08:58:40 +0000 | [diff] [blame] | 683 | memcpy(&sctp_ifap->address, addr, sizeof(struct sockaddr_conn)); |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 684 | break; |
| 685 | #endif |
| 686 | default: |
| 687 | /* TSNH */ |
| 688 | break; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 689 | } |
| 690 | #endif |
| 691 | sctp_ifap->localifa_flags = SCTP_ADDR_VALID | SCTP_ADDR_DEFER_USE; |
| 692 | sctp_ifap->flags = ifa_flags; |
| 693 | /* Set scope */ |
| 694 | switch (sctp_ifap->address.sa.sa_family) { |
| 695 | #ifdef INET |
| 696 | case AF_INET: |
| 697 | { |
| 698 | struct sockaddr_in *sin; |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 699 | |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 700 | sin = &sctp_ifap->address.sin; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 701 | if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) || |
| 702 | (IN4_ISLOOPBACK_ADDRESS(&sin->sin_addr))) { |
| 703 | sctp_ifap->src_is_loop = 1; |
| 704 | } |
| 705 | if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) { |
| 706 | sctp_ifap->src_is_priv = 1; |
| 707 | } |
| 708 | sctp_ifnp->num_v4++; |
| 709 | if (new_ifn_af) |
| 710 | new_ifn_af = AF_INET; |
| 711 | break; |
| 712 | } |
| 713 | #endif |
| 714 | #ifdef INET6 |
| 715 | case AF_INET6: |
| 716 | { |
| 717 | /* ok to use deprecated addresses? */ |
| 718 | struct sockaddr_in6 *sin6; |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 719 | |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 720 | sin6 = &sctp_ifap->address.sin6; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 721 | if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) || |
| 722 | (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))) { |
| 723 | sctp_ifap->src_is_loop = 1; |
| 724 | } |
| 725 | if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { |
| 726 | sctp_ifap->src_is_priv = 1; |
| 727 | } |
| 728 | sctp_ifnp->num_v6++; |
| 729 | if (new_ifn_af) |
| 730 | new_ifn_af = AF_INET6; |
| 731 | break; |
| 732 | } |
| 733 | #endif |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 734 | #if defined(__Userspace__) |
| 735 | case AF_CONN: |
| 736 | if (new_ifn_af) |
| 737 | new_ifn_af = AF_CONN; |
| 738 | break; |
| 739 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 740 | default: |
| 741 | new_ifn_af = 0; |
| 742 | break; |
| 743 | } |
| 744 | hash_of_addr = sctp_get_ifa_hash_val(&sctp_ifap->address.sa); |
| 745 | |
| 746 | if ((sctp_ifap->src_is_priv == 0) && |
| 747 | (sctp_ifap->src_is_loop == 0)) { |
| 748 | sctp_ifap->src_is_glob = 1; |
| 749 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 750 | hash_addr_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)]; |
| 751 | LIST_INSERT_HEAD(hash_addr_head, sctp_ifap, next_bucket); |
| 752 | sctp_ifap->refcount = 1; |
| 753 | LIST_INSERT_HEAD(&sctp_ifnp->ifalist, sctp_ifap, next_ifa); |
| 754 | sctp_ifnp->ifa_count++; |
| 755 | vrf->total_ifa_count++; |
| 756 | atomic_add_int(&SCTP_BASE_INFO(ipi_count_ifas), 1); |
| 757 | if (new_ifn_af) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 758 | sctp_ifnp->registered_af = new_ifn_af; |
| 759 | } |
| 760 | SCTP_IPI_ADDR_WUNLOCK(); |
Michael Tuexen | 2c5953e | 2020-07-17 17:25:19 +0200 | [diff] [blame] | 761 | if (new_sctp_ifnp != NULL) { |
| 762 | SCTP_FREE(new_sctp_ifnp, SCTP_M_IFN); |
| 763 | } |
| 764 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 765 | if (dynamic_add) { |
| 766 | /* Bump up the refcount so that when the timer |
| 767 | * completes it will drop back down. |
| 768 | */ |
| 769 | struct sctp_laddr *wi; |
| 770 | |
| 771 | atomic_add_int(&sctp_ifap->refcount, 1); |
| 772 | wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr); |
| 773 | if (wi == NULL) { |
| 774 | /* |
| 775 | * Gak, what can we do? We have lost an address |
| 776 | * change can you say HOSED? |
| 777 | */ |
| 778 | SCTPDBG(SCTP_DEBUG_PCB4, "Lost an address change?\n"); |
| 779 | /* Opps, must decrement the count */ |
| 780 | sctp_del_addr_from_vrf(vrf_id, addr, ifn_index, |
| 781 | if_name); |
| 782 | return (NULL); |
| 783 | } |
| 784 | SCTP_INCR_LADDR_COUNT(); |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 785 | memset(wi, 0, sizeof(*wi)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 786 | (void)SCTP_GETTIME_TIMEVAL(&wi->start_time); |
| 787 | wi->ifa = sctp_ifap; |
| 788 | wi->action = SCTP_ADD_IP_ADDRESS; |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 789 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 790 | SCTP_WQ_ADDR_LOCK(); |
| 791 | LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 792 | sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ, |
| 793 | (struct sctp_inpcb *)NULL, |
| 794 | (struct sctp_tcb *)NULL, |
| 795 | (struct sctp_nets *)NULL); |
Michael Tuexen | 96939f1 | 2017-09-20 23:31:24 +0200 | [diff] [blame] | 796 | SCTP_WQ_ADDR_UNLOCK(); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 797 | } else { |
| 798 | /* it's ready for use */ |
| 799 | sctp_ifap->localifa_flags &= ~SCTP_ADDR_DEFER_USE; |
| 800 | } |
| 801 | return (sctp_ifap); |
| 802 | } |
| 803 | |
| 804 | void |
| 805 | sctp_del_addr_from_vrf(uint32_t vrf_id, struct sockaddr *addr, |
| 806 | uint32_t ifn_index, const char *if_name) |
| 807 | { |
| 808 | struct sctp_vrf *vrf; |
| 809 | struct sctp_ifa *sctp_ifap = NULL; |
| 810 | |
| 811 | SCTP_IPI_ADDR_WLOCK(); |
| 812 | vrf = sctp_find_vrf(vrf_id); |
| 813 | if (vrf == NULL) { |
| 814 | SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id); |
| 815 | goto out_now; |
| 816 | } |
| 817 | |
| 818 | #ifdef SCTP_DEBUG |
| 819 | SCTPDBG(SCTP_DEBUG_PCB4, "vrf_id 0x%x: deleting address:", vrf_id); |
| 820 | SCTPDBG_ADDR(SCTP_DEBUG_PCB4, addr); |
| 821 | #endif |
| 822 | sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED); |
| 823 | if (sctp_ifap) { |
| 824 | /* Validate the delete */ |
| 825 | if (sctp_ifap->ifn_p) { |
tuexen | b740ef6 | 2011-11-13 11:58:06 +0000 | [diff] [blame] | 826 | int valid = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 827 | /*- |
| 828 | * The name has priority over the ifn_index |
Michael Tuexen | 3b4263d | 2020-06-06 19:52:50 +0200 | [diff] [blame] | 829 | * if its given. |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 830 | */ |
| 831 | if (if_name) { |
tuexen | b740ef6 | 2011-11-13 11:58:06 +0000 | [diff] [blame] | 832 | if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, SCTP_IFNAMSIZ) == 0) { |
| 833 | /* They match its a correct delete */ |
| 834 | valid = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 835 | } |
| 836 | } |
| 837 | if (!valid) { |
| 838 | /* last ditch check ifn_index */ |
| 839 | if (ifn_index == sctp_ifap->ifn_p->ifn_index) { |
| 840 | valid = 1; |
| 841 | } |
| 842 | } |
| 843 | if (!valid) { |
| 844 | SCTPDBG(SCTP_DEBUG_PCB4, "ifn:%d ifname:%s does not match addresses\n", |
| 845 | ifn_index, ((if_name == NULL) ? "NULL" : if_name)); |
| 846 | SCTPDBG(SCTP_DEBUG_PCB4, "ifn:%d ifname:%s - ignoring delete\n", |
| 847 | sctp_ifap->ifn_p->ifn_index, sctp_ifap->ifn_p->ifn_name); |
| 848 | SCTP_IPI_ADDR_WUNLOCK(); |
| 849 | return; |
| 850 | } |
| 851 | } |
t00fcxen | 8fca7ff | 2012-09-05 18:58:42 +0000 | [diff] [blame] | 852 | SCTPDBG(SCTP_DEBUG_PCB4, "Deleting ifa %p\n", (void *)sctp_ifap); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 853 | sctp_ifap->localifa_flags &= SCTP_ADDR_VALID; |
Michael Tuexen | 2c5953e | 2020-07-17 17:25:19 +0200 | [diff] [blame] | 854 | /* |
t00fcxen | 97887d6 | 2014-03-29 21:31:12 +0000 | [diff] [blame] | 855 | * We don't set the flag. This means that the structure will |
| 856 | * hang around in EP's that have bound specific to it until |
| 857 | * they close. This gives us TCP like behavior if someone |
| 858 | * removes an address (or for that matter adds it right back). |
| 859 | */ |
| 860 | /* sctp_ifap->localifa_flags |= SCTP_BEING_DELETED; */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 861 | vrf->total_ifa_count--; |
| 862 | LIST_REMOVE(sctp_ifap, next_bucket); |
| 863 | sctp_remove_ifa_from_ifn(sctp_ifap); |
| 864 | } |
| 865 | #ifdef SCTP_DEBUG |
| 866 | else { |
| 867 | SCTPDBG(SCTP_DEBUG_PCB4, "Del Addr-ifn:%d Could not find address:", |
| 868 | ifn_index); |
| 869 | SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr); |
| 870 | } |
| 871 | #endif |
| 872 | |
| 873 | out_now: |
| 874 | SCTP_IPI_ADDR_WUNLOCK(); |
| 875 | if (sctp_ifap) { |
| 876 | struct sctp_laddr *wi; |
| 877 | |
| 878 | wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr); |
| 879 | if (wi == NULL) { |
| 880 | /* |
| 881 | * Gak, what can we do? We have lost an address |
| 882 | * change can you say HOSED? |
| 883 | */ |
| 884 | SCTPDBG(SCTP_DEBUG_PCB4, "Lost an address change?\n"); |
| 885 | |
| 886 | /* Oops, must decrement the count */ |
| 887 | sctp_free_ifa(sctp_ifap); |
| 888 | return; |
| 889 | } |
| 890 | SCTP_INCR_LADDR_COUNT(); |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 891 | memset(wi, 0, sizeof(*wi)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 892 | (void)SCTP_GETTIME_TIMEVAL(&wi->start_time); |
| 893 | wi->ifa = sctp_ifap; |
| 894 | wi->action = SCTP_DEL_IP_ADDRESS; |
| 895 | SCTP_WQ_ADDR_LOCK(); |
| 896 | /* |
| 897 | * Should this really be a tailq? As it is we will process the |
| 898 | * newest first :-0 |
| 899 | */ |
| 900 | LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 901 | sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ, |
| 902 | (struct sctp_inpcb *)NULL, |
| 903 | (struct sctp_tcb *)NULL, |
| 904 | (struct sctp_nets *)NULL); |
Michael Tuexen | 96939f1 | 2017-09-20 23:31:24 +0200 | [diff] [blame] | 905 | SCTP_WQ_ADDR_UNLOCK(); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 906 | } |
| 907 | return; |
| 908 | } |
| 909 | |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 910 | static int |
| 911 | sctp_does_stcb_own_this_addr(struct sctp_tcb *stcb, struct sockaddr *to) |
| 912 | { |
t00fcxen | aff14b6 | 2013-11-02 18:02:02 +0000 | [diff] [blame] | 913 | int loopback_scope; |
| 914 | #if defined(INET) |
| 915 | int ipv4_local_scope, ipv4_addr_legal; |
| 916 | #endif |
| 917 | #if defined(INET6) |
| 918 | int local_scope, site_scope, ipv6_addr_legal; |
| 919 | #endif |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 920 | #if defined(__Userspace__) |
| 921 | int conn_addr_legal; |
| 922 | #endif |
| 923 | struct sctp_vrf *vrf; |
| 924 | struct sctp_ifn *sctp_ifn; |
| 925 | struct sctp_ifa *sctp_ifa; |
| 926 | |
| 927 | loopback_scope = stcb->asoc.scope.loopback_scope; |
t00fcxen | aff14b6 | 2013-11-02 18:02:02 +0000 | [diff] [blame] | 928 | #if defined(INET) |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 929 | ipv4_local_scope = stcb->asoc.scope.ipv4_local_scope; |
t00fcxen | aff14b6 | 2013-11-02 18:02:02 +0000 | [diff] [blame] | 930 | ipv4_addr_legal = stcb->asoc.scope.ipv4_addr_legal; |
| 931 | #endif |
| 932 | #if defined(INET6) |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 933 | local_scope = stcb->asoc.scope.local_scope; |
| 934 | site_scope = stcb->asoc.scope.site_scope; |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 935 | ipv6_addr_legal = stcb->asoc.scope.ipv6_addr_legal; |
t00fcxen | aff14b6 | 2013-11-02 18:02:02 +0000 | [diff] [blame] | 936 | #endif |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 937 | #if defined(__Userspace__) |
| 938 | conn_addr_legal = stcb->asoc.scope.conn_addr_legal; |
| 939 | #endif |
| 940 | |
| 941 | SCTP_IPI_ADDR_RLOCK(); |
| 942 | vrf = sctp_find_vrf(stcb->asoc.vrf_id); |
| 943 | if (vrf == NULL) { |
| 944 | /* no vrf, no addresses */ |
| 945 | SCTP_IPI_ADDR_RUNLOCK(); |
| 946 | return (0); |
| 947 | } |
| 948 | |
| 949 | if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { |
| 950 | LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) { |
| 951 | if ((loopback_scope == 0) && |
| 952 | SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) { |
| 953 | continue; |
| 954 | } |
| 955 | LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) { |
| 956 | if (sctp_is_addr_restricted(stcb, sctp_ifa) && |
| 957 | (!sctp_is_addr_pending(stcb, sctp_ifa))) { |
| 958 | /* We allow pending addresses, where we |
| 959 | * have sent an asconf-add to be considered |
| 960 | * valid. |
| 961 | */ |
| 962 | continue; |
| 963 | } |
t00fcxen | ba8aa20 | 2013-11-16 15:37:17 +0000 | [diff] [blame] | 964 | if (sctp_ifa->address.sa.sa_family != to->sa_family) { |
| 965 | continue; |
| 966 | } |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 967 | switch (sctp_ifa->address.sa.sa_family) { |
| 968 | #ifdef INET |
| 969 | case AF_INET: |
| 970 | if (ipv4_addr_legal) { |
| 971 | struct sockaddr_in *sin, *rsin; |
| 972 | |
| 973 | sin = &sctp_ifa->address.sin; |
| 974 | rsin = (struct sockaddr_in *)to; |
| 975 | if ((ipv4_local_scope == 0) && |
| 976 | IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) { |
| 977 | continue; |
| 978 | } |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 979 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 980 | if (prison_check_ip4(stcb->sctp_ep->ip_inp.inp.inp_cred, |
| 981 | &sin->sin_addr) != 0) { |
| 982 | continue; |
| 983 | } |
| 984 | #endif |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 985 | if (sin->sin_addr.s_addr == rsin->sin_addr.s_addr) { |
| 986 | SCTP_IPI_ADDR_RUNLOCK(); |
| 987 | return (1); |
| 988 | } |
| 989 | } |
| 990 | break; |
| 991 | #endif |
| 992 | #ifdef INET6 |
| 993 | case AF_INET6: |
| 994 | if (ipv6_addr_legal) { |
| 995 | struct sockaddr_in6 *sin6, *rsin6; |
| 996 | #if defined(SCTP_EMBEDDED_V6_SCOPE) && !defined(SCTP_KAME) |
| 997 | struct sockaddr_in6 lsa6; |
| 998 | #endif |
| 999 | sin6 = &sctp_ifa->address.sin6; |
| 1000 | rsin6 = (struct sockaddr_in6 *)to; |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 1001 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 1002 | if (prison_check_ip6(stcb->sctp_ep->ip_inp.inp.inp_cred, |
| 1003 | &sin6->sin6_addr) != 0) { |
| 1004 | continue; |
| 1005 | } |
| 1006 | #endif |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 1007 | if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { |
| 1008 | if (local_scope == 0) |
| 1009 | continue; |
| 1010 | #if defined(SCTP_EMBEDDED_V6_SCOPE) |
| 1011 | if (sin6->sin6_scope_id == 0) { |
| 1012 | #ifdef SCTP_KAME |
| 1013 | if (sa6_recoverscope(sin6) != 0) |
| 1014 | continue; |
| 1015 | #else |
| 1016 | lsa6 = *sin6; |
| 1017 | if (in6_recoverscope(&lsa6, |
| 1018 | &lsa6.sin6_addr, |
| 1019 | NULL)) |
| 1020 | continue; |
| 1021 | sin6 = &lsa6; |
| 1022 | #endif /* SCTP_KAME */ |
| 1023 | } |
| 1024 | #endif /* SCTP_EMBEDDED_V6_SCOPE */ |
| 1025 | } |
| 1026 | if ((site_scope == 0) && |
| 1027 | (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) { |
| 1028 | continue; |
| 1029 | } |
| 1030 | if (SCTP6_ARE_ADDR_EQUAL(sin6, rsin6)) { |
| 1031 | SCTP_IPI_ADDR_RUNLOCK(); |
| 1032 | return (1); |
| 1033 | } |
| 1034 | } |
| 1035 | break; |
| 1036 | #endif |
| 1037 | #if defined(__Userspace__) |
| 1038 | case AF_CONN: |
| 1039 | if (conn_addr_legal) { |
| 1040 | struct sockaddr_conn *sconn, *rsconn; |
| 1041 | |
| 1042 | sconn = &sctp_ifa->address.sconn; |
| 1043 | rsconn = (struct sockaddr_conn *)to; |
| 1044 | if (sconn->sconn_addr == rsconn->sconn_addr) { |
| 1045 | SCTP_IPI_ADDR_RUNLOCK(); |
| 1046 | return (1); |
| 1047 | } |
| 1048 | } |
| 1049 | break; |
| 1050 | #endif |
| 1051 | default: |
| 1052 | /* TSNH */ |
| 1053 | break; |
| 1054 | } |
| 1055 | } |
| 1056 | } |
| 1057 | } else { |
| 1058 | struct sctp_laddr *laddr; |
| 1059 | |
| 1060 | LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) { |
| 1061 | if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) { |
| 1062 | SCTPDBG(SCTP_DEBUG_PCB1, "ifa being deleted\n"); |
| 1063 | continue; |
| 1064 | } |
| 1065 | if (sctp_is_addr_restricted(stcb, laddr->ifa) && |
| 1066 | (!sctp_is_addr_pending(stcb, laddr->ifa))) { |
| 1067 | /* We allow pending addresses, where we |
| 1068 | * have sent an asconf-add to be considered |
| 1069 | * valid. |
| 1070 | */ |
| 1071 | continue; |
| 1072 | } |
| 1073 | if (laddr->ifa->address.sa.sa_family != to->sa_family) { |
| 1074 | continue; |
| 1075 | } |
| 1076 | switch (to->sa_family) { |
| 1077 | #ifdef INET |
| 1078 | case AF_INET: |
| 1079 | { |
| 1080 | struct sockaddr_in *sin, *rsin; |
| 1081 | |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 1082 | sin = &laddr->ifa->address.sin; |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 1083 | rsin = (struct sockaddr_in *)to; |
| 1084 | if (sin->sin_addr.s_addr == rsin->sin_addr.s_addr) { |
| 1085 | SCTP_IPI_ADDR_RUNLOCK(); |
| 1086 | return (1); |
| 1087 | } |
| 1088 | break; |
| 1089 | } |
| 1090 | #endif |
| 1091 | #ifdef INET6 |
| 1092 | case AF_INET6: |
| 1093 | { |
| 1094 | struct sockaddr_in6 *sin6, *rsin6; |
| 1095 | |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 1096 | sin6 = &laddr->ifa->address.sin6; |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 1097 | rsin6 = (struct sockaddr_in6 *)to; |
| 1098 | if (SCTP6_ARE_ADDR_EQUAL(sin6, rsin6)) { |
| 1099 | SCTP_IPI_ADDR_RUNLOCK(); |
| 1100 | return (1); |
| 1101 | } |
| 1102 | break; |
| 1103 | } |
| 1104 | |
| 1105 | #endif |
| 1106 | #if defined(__Userspace__) |
| 1107 | case AF_CONN: |
| 1108 | { |
| 1109 | struct sockaddr_conn *sconn, *rsconn; |
| 1110 | |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 1111 | sconn = &laddr->ifa->address.sconn; |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 1112 | rsconn = (struct sockaddr_conn *)to; |
| 1113 | if (sconn->sconn_addr == rsconn->sconn_addr) { |
| 1114 | SCTP_IPI_ADDR_RUNLOCK(); |
| 1115 | return (1); |
| 1116 | } |
| 1117 | break; |
| 1118 | } |
| 1119 | #endif |
| 1120 | default: |
| 1121 | /* TSNH */ |
| 1122 | break; |
| 1123 | } |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 1124 | } |
| 1125 | } |
| 1126 | SCTP_IPI_ADDR_RUNLOCK(); |
| 1127 | return (0); |
| 1128 | } |
| 1129 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1130 | static struct sctp_tcb * |
| 1131 | sctp_tcb_special_locate(struct sctp_inpcb **inp_p, struct sockaddr *from, |
| 1132 | struct sockaddr *to, struct sctp_nets **netp, uint32_t vrf_id) |
| 1133 | { |
| 1134 | /**** ASSUMES THE CALLER holds the INP_INFO_RLOCK */ |
| 1135 | /* |
| 1136 | * If we support the TCP model, then we must now dig through to see |
| 1137 | * if we can find our endpoint in the list of tcp ep's. |
| 1138 | */ |
| 1139 | uint16_t lport, rport; |
| 1140 | struct sctppcbhead *ephead; |
| 1141 | struct sctp_inpcb *inp; |
| 1142 | struct sctp_laddr *laddr; |
| 1143 | struct sctp_tcb *stcb; |
| 1144 | struct sctp_nets *net; |
| 1145 | #ifdef SCTP_MVRF |
| 1146 | int fnd, i; |
| 1147 | #endif |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 1148 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1149 | if ((to == NULL) || (from == NULL)) { |
| 1150 | return (NULL); |
| 1151 | } |
| 1152 | |
| 1153 | switch (to->sa_family) { |
| 1154 | #ifdef INET |
| 1155 | case AF_INET: |
| 1156 | if (from->sa_family == AF_INET) { |
| 1157 | lport = ((struct sockaddr_in *)to)->sin_port; |
| 1158 | rport = ((struct sockaddr_in *)from)->sin_port; |
| 1159 | } else { |
| 1160 | return (NULL); |
| 1161 | } |
| 1162 | break; |
| 1163 | #endif |
| 1164 | #ifdef INET6 |
| 1165 | case AF_INET6: |
| 1166 | if (from->sa_family == AF_INET6) { |
| 1167 | lport = ((struct sockaddr_in6 *)to)->sin6_port; |
| 1168 | rport = ((struct sockaddr_in6 *)from)->sin6_port; |
| 1169 | } else { |
| 1170 | return (NULL); |
| 1171 | } |
| 1172 | break; |
| 1173 | #endif |
tuexen | 5d9b974 | 2012-07-11 21:52:01 +0000 | [diff] [blame] | 1174 | #if defined(__Userspace__) |
| 1175 | case AF_CONN: |
| 1176 | if (from->sa_family == AF_CONN) { |
| 1177 | lport = ((struct sockaddr_conn *)to)->sconn_port; |
| 1178 | rport = ((struct sockaddr_conn *)from)->sconn_port; |
| 1179 | } else { |
| 1180 | return (NULL); |
| 1181 | } |
| 1182 | break; |
| 1183 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1184 | default: |
| 1185 | return (NULL); |
| 1186 | } |
| 1187 | ephead = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR((lport | rport), SCTP_BASE_INFO(hashtcpmark))]; |
| 1188 | /* |
| 1189 | * Ok now for each of the guys in this bucket we must look and see: |
| 1190 | * - Does the remote port match. - Does there single association's |
| 1191 | * addresses match this address (to). If so we update p_ep to point |
| 1192 | * to this ep and return the tcb from it. |
| 1193 | */ |
| 1194 | LIST_FOREACH(inp, ephead, sctp_hash) { |
| 1195 | SCTP_INP_RLOCK(inp); |
| 1196 | if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
| 1197 | SCTP_INP_RUNLOCK(inp); |
| 1198 | continue; |
| 1199 | } |
| 1200 | if (lport != inp->sctp_lport) { |
| 1201 | SCTP_INP_RUNLOCK(inp); |
| 1202 | continue; |
| 1203 | } |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 1204 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 1205 | switch (to->sa_family) { |
| 1206 | #ifdef INET |
| 1207 | case AF_INET: |
| 1208 | { |
| 1209 | struct sockaddr_in *sin; |
| 1210 | |
| 1211 | sin = (struct sockaddr_in *)to; |
| 1212 | if (prison_check_ip4(inp->ip_inp.inp.inp_cred, |
| 1213 | &sin->sin_addr) != 0) { |
| 1214 | SCTP_INP_RUNLOCK(inp); |
| 1215 | continue; |
| 1216 | } |
| 1217 | break; |
| 1218 | } |
| 1219 | #endif |
| 1220 | #ifdef INET6 |
| 1221 | case AF_INET6: |
| 1222 | { |
| 1223 | struct sockaddr_in6 *sin6; |
| 1224 | |
| 1225 | sin6 = (struct sockaddr_in6 *)to; |
| 1226 | if (prison_check_ip6(inp->ip_inp.inp.inp_cred, |
| 1227 | &sin6->sin6_addr) != 0) { |
| 1228 | SCTP_INP_RUNLOCK(inp); |
| 1229 | continue; |
| 1230 | } |
| 1231 | break; |
| 1232 | } |
| 1233 | #endif |
| 1234 | default: |
| 1235 | SCTP_INP_RUNLOCK(inp); |
| 1236 | continue; |
| 1237 | } |
| 1238 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1239 | #ifdef SCTP_MVRF |
| 1240 | fnd = 0; |
tuexen | 9784e9a | 2011-12-18 13:04:23 +0000 | [diff] [blame] | 1241 | for (i = 0; i < inp->num_vrfs; i++) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1242 | if (inp->m_vrf_ids[i] == vrf_id) { |
| 1243 | fnd = 1; |
| 1244 | break; |
| 1245 | } |
| 1246 | } |
| 1247 | if (fnd == 0) { |
| 1248 | SCTP_INP_RUNLOCK(inp); |
| 1249 | continue; |
| 1250 | } |
| 1251 | #else |
| 1252 | if (inp->def_vrf_id != vrf_id) { |
| 1253 | SCTP_INP_RUNLOCK(inp); |
| 1254 | continue; |
| 1255 | } |
| 1256 | #endif |
| 1257 | /* check to see if the ep has one of the addresses */ |
| 1258 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) { |
| 1259 | /* We are NOT bound all, so look further */ |
| 1260 | int match = 0; |
| 1261 | |
| 1262 | LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1263 | if (laddr->ifa == NULL) { |
Saúl Ibarra Corretgé | 8b04b41 | 2015-09-26 09:53:06 +0200 | [diff] [blame] | 1264 | SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n", __func__); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1265 | continue; |
| 1266 | } |
| 1267 | if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) { |
| 1268 | SCTPDBG(SCTP_DEBUG_PCB1, "ifa being deleted\n"); |
| 1269 | continue; |
| 1270 | } |
| 1271 | if (laddr->ifa->address.sa.sa_family == |
| 1272 | to->sa_family) { |
| 1273 | /* see if it matches */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1274 | #ifdef INET |
| 1275 | if (from->sa_family == AF_INET) { |
| 1276 | struct sockaddr_in *intf_addr, *sin; |
| 1277 | |
| 1278 | intf_addr = &laddr->ifa->address.sin; |
| 1279 | sin = (struct sockaddr_in *)to; |
| 1280 | if (sin->sin_addr.s_addr == |
| 1281 | intf_addr->sin_addr.s_addr) { |
| 1282 | match = 1; |
| 1283 | break; |
| 1284 | } |
| 1285 | } |
| 1286 | #endif |
| 1287 | #ifdef INET6 |
| 1288 | if (from->sa_family == AF_INET6) { |
| 1289 | struct sockaddr_in6 *intf_addr6; |
| 1290 | struct sockaddr_in6 *sin6; |
| 1291 | |
| 1292 | sin6 = (struct sockaddr_in6 *) |
| 1293 | to; |
| 1294 | intf_addr6 = &laddr->ifa->address.sin6; |
| 1295 | |
| 1296 | if (SCTP6_ARE_ADDR_EQUAL(sin6, |
| 1297 | intf_addr6)) { |
| 1298 | match = 1; |
| 1299 | break; |
| 1300 | } |
| 1301 | } |
| 1302 | #endif |
tuexen | 5d9b974 | 2012-07-11 21:52:01 +0000 | [diff] [blame] | 1303 | #if defined(__Userspace__) |
| 1304 | if (from->sa_family == AF_CONN) { |
| 1305 | struct sockaddr_conn *intf_addr, *sconn; |
| 1306 | |
| 1307 | intf_addr = &laddr->ifa->address.sconn; |
| 1308 | sconn = (struct sockaddr_conn *)to; |
| 1309 | if (sconn->sconn_addr == |
| 1310 | intf_addr->sconn_addr) { |
| 1311 | match = 1; |
| 1312 | break; |
| 1313 | } |
| 1314 | } |
| 1315 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1316 | } |
| 1317 | } |
| 1318 | if (match == 0) { |
| 1319 | /* This endpoint does not have this address */ |
| 1320 | SCTP_INP_RUNLOCK(inp); |
| 1321 | continue; |
| 1322 | } |
| 1323 | } |
| 1324 | /* |
| 1325 | * Ok if we hit here the ep has the address, does it hold |
| 1326 | * the tcb? |
| 1327 | */ |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 1328 | /* XXX: Why don't we TAILQ_FOREACH through sctp_asoc_list? */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1329 | stcb = LIST_FIRST(&inp->sctp_asoc_list); |
| 1330 | if (stcb == NULL) { |
| 1331 | SCTP_INP_RUNLOCK(inp); |
| 1332 | continue; |
| 1333 | } |
| 1334 | SCTP_TCB_LOCK(stcb); |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 1335 | if (!sctp_does_stcb_own_this_addr(stcb, to)) { |
| 1336 | SCTP_TCB_UNLOCK(stcb); |
| 1337 | SCTP_INP_RUNLOCK(inp); |
| 1338 | continue; |
| 1339 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1340 | if (stcb->rport != rport) { |
| 1341 | /* remote port does not match. */ |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 1342 | SCTP_TCB_UNLOCK(stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1343 | SCTP_INP_RUNLOCK(inp); |
| 1344 | continue; |
| 1345 | } |
| 1346 | if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 1347 | SCTP_TCB_UNLOCK(stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1348 | SCTP_INP_RUNLOCK(inp); |
| 1349 | continue; |
| 1350 | } |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 1351 | if (!sctp_does_stcb_own_this_addr(stcb, to)) { |
| 1352 | SCTP_TCB_UNLOCK(stcb); |
| 1353 | SCTP_INP_RUNLOCK(inp); |
| 1354 | continue; |
| 1355 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1356 | /* Does this TCB have a matching address? */ |
| 1357 | TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1358 | if (net->ro._l_addr.sa.sa_family != from->sa_family) { |
| 1359 | /* not the same family, can't be a match */ |
| 1360 | continue; |
| 1361 | } |
| 1362 | switch (from->sa_family) { |
| 1363 | #ifdef INET |
| 1364 | case AF_INET: |
| 1365 | { |
| 1366 | struct sockaddr_in *sin, *rsin; |
| 1367 | |
| 1368 | sin = (struct sockaddr_in *)&net->ro._l_addr; |
| 1369 | rsin = (struct sockaddr_in *)from; |
| 1370 | if (sin->sin_addr.s_addr == |
| 1371 | rsin->sin_addr.s_addr) { |
| 1372 | /* found it */ |
| 1373 | if (netp != NULL) { |
| 1374 | *netp = net; |
| 1375 | } |
| 1376 | /* Update the endpoint pointer */ |
| 1377 | *inp_p = inp; |
| 1378 | SCTP_INP_RUNLOCK(inp); |
| 1379 | return (stcb); |
| 1380 | } |
| 1381 | break; |
| 1382 | } |
| 1383 | #endif |
| 1384 | #ifdef INET6 |
| 1385 | case AF_INET6: |
| 1386 | { |
| 1387 | struct sockaddr_in6 *sin6, *rsin6; |
| 1388 | |
| 1389 | sin6 = (struct sockaddr_in6 *)&net->ro._l_addr; |
| 1390 | rsin6 = (struct sockaddr_in6 *)from; |
| 1391 | if (SCTP6_ARE_ADDR_EQUAL(sin6, |
| 1392 | rsin6)) { |
| 1393 | /* found it */ |
| 1394 | if (netp != NULL) { |
| 1395 | *netp = net; |
| 1396 | } |
| 1397 | /* Update the endpoint pointer */ |
| 1398 | *inp_p = inp; |
| 1399 | SCTP_INP_RUNLOCK(inp); |
| 1400 | return (stcb); |
| 1401 | } |
| 1402 | break; |
| 1403 | } |
| 1404 | #endif |
tuexen | 5d9b974 | 2012-07-11 21:52:01 +0000 | [diff] [blame] | 1405 | #if defined(__Userspace__) |
| 1406 | case AF_CONN: |
| 1407 | { |
| 1408 | struct sockaddr_conn *sconn, *rsconn; |
| 1409 | |
| 1410 | sconn = (struct sockaddr_conn *)&net->ro._l_addr; |
| 1411 | rsconn = (struct sockaddr_conn *)from; |
| 1412 | if (sconn->sconn_addr == rsconn->sconn_addr) { |
| 1413 | /* found it */ |
| 1414 | if (netp != NULL) { |
| 1415 | *netp = net; |
| 1416 | } |
| 1417 | /* Update the endpoint pointer */ |
| 1418 | *inp_p = inp; |
| 1419 | SCTP_INP_RUNLOCK(inp); |
| 1420 | return (stcb); |
| 1421 | } |
| 1422 | break; |
| 1423 | } |
| 1424 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1425 | default: |
| 1426 | /* TSNH */ |
| 1427 | break; |
| 1428 | } |
| 1429 | } |
| 1430 | SCTP_TCB_UNLOCK(stcb); |
| 1431 | SCTP_INP_RUNLOCK(inp); |
| 1432 | } |
| 1433 | return (NULL); |
| 1434 | } |
| 1435 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1436 | /* |
| 1437 | * rules for use |
| 1438 | * |
| 1439 | * 1) If I return a NULL you must decrement any INP ref cnt. 2) If I find an |
| 1440 | * stcb, both will be locked (locked_tcb and stcb) but decrement will be done |
| 1441 | * (if locked == NULL). 3) Decrement happens on return ONLY if locked == |
| 1442 | * NULL. |
| 1443 | */ |
| 1444 | |
| 1445 | struct sctp_tcb * |
| 1446 | sctp_findassociation_ep_addr(struct sctp_inpcb **inp_p, struct sockaddr *remote, |
| 1447 | struct sctp_nets **netp, struct sockaddr *local, struct sctp_tcb *locked_tcb) |
| 1448 | { |
| 1449 | struct sctpasochead *head; |
| 1450 | struct sctp_inpcb *inp; |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 1451 | struct sctp_tcb *stcb = NULL; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1452 | struct sctp_nets *net; |
| 1453 | uint16_t rport; |
| 1454 | |
| 1455 | inp = *inp_p; |
tuexen | 5d9b974 | 2012-07-11 21:52:01 +0000 | [diff] [blame] | 1456 | switch (remote->sa_family) { |
| 1457 | #ifdef INET |
| 1458 | case AF_INET: |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1459 | rport = (((struct sockaddr_in *)remote)->sin_port); |
tuexen | 5d9b974 | 2012-07-11 21:52:01 +0000 | [diff] [blame] | 1460 | break; |
| 1461 | #endif |
| 1462 | #ifdef INET6 |
| 1463 | case AF_INET6: |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1464 | rport = (((struct sockaddr_in6 *)remote)->sin6_port); |
tuexen | 5d9b974 | 2012-07-11 21:52:01 +0000 | [diff] [blame] | 1465 | break; |
| 1466 | #endif |
| 1467 | #if defined(__Userspace__) |
| 1468 | case AF_CONN: |
Taylor Brandstetter | 7467051 | 2018-03-27 10:56:58 -0700 | [diff] [blame] | 1469 | rport = (((struct sockaddr_conn *)remote)->sconn_port); |
tuexen | 5d9b974 | 2012-07-11 21:52:01 +0000 | [diff] [blame] | 1470 | break; |
| 1471 | #endif |
| 1472 | default: |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1473 | return (NULL); |
| 1474 | } |
| 1475 | if (locked_tcb) { |
| 1476 | /* |
| 1477 | * UN-lock so we can do proper locking here this occurs when |
| 1478 | * called from load_addresses_from_init. |
| 1479 | */ |
| 1480 | atomic_add_int(&locked_tcb->asoc.refcnt, 1); |
| 1481 | SCTP_TCB_UNLOCK(locked_tcb); |
| 1482 | } |
| 1483 | SCTP_INP_INFO_RLOCK(); |
t00fcxen | f2d781c | 2012-11-26 16:47:12 +0000 | [diff] [blame] | 1484 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || |
| 1485 | (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1486 | /*- |
| 1487 | * Now either this guy is our listener or it's the |
| 1488 | * connector. If it is the one that issued the connect, then |
| 1489 | * it's only chance is to be the first TCB in the list. If |
| 1490 | * it is the acceptor, then do the special_lookup to hash |
| 1491 | * and find the real inp. |
| 1492 | */ |
Michael Tuexen | d900e5f | 2017-06-23 10:52:43 +0200 | [diff] [blame] | 1493 | if ((inp->sctp_socket) && SCTP_IS_LISTENING(inp)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1494 | /* to is peer addr, from is my addr */ |
| 1495 | #ifndef SCTP_MVRF |
| 1496 | stcb = sctp_tcb_special_locate(inp_p, remote, local, |
| 1497 | netp, inp->def_vrf_id); |
| 1498 | if ((stcb != NULL) && (locked_tcb == NULL)) { |
| 1499 | /* we have a locked tcb, lower refcount */ |
| 1500 | SCTP_INP_DECR_REF(inp); |
| 1501 | } |
| 1502 | if ((locked_tcb != NULL) && (locked_tcb != stcb)) { |
| 1503 | SCTP_INP_RLOCK(locked_tcb->sctp_ep); |
| 1504 | SCTP_TCB_LOCK(locked_tcb); |
| 1505 | atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); |
| 1506 | SCTP_INP_RUNLOCK(locked_tcb->sctp_ep); |
| 1507 | } |
| 1508 | #else |
| 1509 | /*- |
| 1510 | * MVRF is tricky, we must look in every VRF |
| 1511 | * the endpoint has. |
| 1512 | */ |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 1513 | int i; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1514 | |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 1515 | for (i = 0; i < inp->num_vrfs; i++) { |
| 1516 | stcb = sctp_tcb_special_locate(inp_p, remote, local, |
| 1517 | netp, inp->m_vrf_ids[i]); |
| 1518 | if ((stcb != NULL) && (locked_tcb == NULL)) { |
| 1519 | /* we have a locked tcb, lower refcount */ |
| 1520 | SCTP_INP_DECR_REF(inp); |
| 1521 | break; |
| 1522 | } |
| 1523 | if ((locked_tcb != NULL) && (locked_tcb != stcb)) { |
| 1524 | SCTP_INP_RLOCK(locked_tcb->sctp_ep); |
| 1525 | SCTP_TCB_LOCK(locked_tcb); |
| 1526 | atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); |
| 1527 | SCTP_INP_RUNLOCK(locked_tcb->sctp_ep); |
| 1528 | break; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1529 | } |
| 1530 | } |
| 1531 | #endif |
| 1532 | SCTP_INP_INFO_RUNLOCK(); |
| 1533 | return (stcb); |
| 1534 | } else { |
| 1535 | SCTP_INP_WLOCK(inp); |
| 1536 | if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
| 1537 | goto null_return; |
| 1538 | } |
| 1539 | stcb = LIST_FIRST(&inp->sctp_asoc_list); |
| 1540 | if (stcb == NULL) { |
| 1541 | goto null_return; |
| 1542 | } |
| 1543 | SCTP_TCB_LOCK(stcb); |
| 1544 | |
| 1545 | if (stcb->rport != rport) { |
| 1546 | /* remote port does not match. */ |
| 1547 | SCTP_TCB_UNLOCK(stcb); |
| 1548 | goto null_return; |
| 1549 | } |
| 1550 | if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 1551 | SCTP_TCB_UNLOCK(stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1552 | goto null_return; |
| 1553 | } |
| 1554 | if (local && !sctp_does_stcb_own_this_addr(stcb, local)) { |
| 1555 | SCTP_TCB_UNLOCK(stcb); |
| 1556 | goto null_return; |
| 1557 | } |
| 1558 | /* now look at the list of remote addresses */ |
| 1559 | TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { |
| 1560 | #ifdef INVARIANTS |
| 1561 | if (net == (TAILQ_NEXT(net, sctp_next))) { |
| 1562 | panic("Corrupt net list"); |
| 1563 | } |
| 1564 | #endif |
| 1565 | if (net->ro._l_addr.sa.sa_family != |
| 1566 | remote->sa_family) { |
| 1567 | /* not the same family */ |
| 1568 | continue; |
| 1569 | } |
| 1570 | switch (remote->sa_family) { |
| 1571 | #ifdef INET |
| 1572 | case AF_INET: |
| 1573 | { |
| 1574 | struct sockaddr_in *sin, *rsin; |
| 1575 | |
| 1576 | sin = (struct sockaddr_in *) |
| 1577 | &net->ro._l_addr; |
| 1578 | rsin = (struct sockaddr_in *)remote; |
| 1579 | if (sin->sin_addr.s_addr == |
| 1580 | rsin->sin_addr.s_addr) { |
| 1581 | /* found it */ |
| 1582 | if (netp != NULL) { |
| 1583 | *netp = net; |
| 1584 | } |
| 1585 | if (locked_tcb == NULL) { |
| 1586 | SCTP_INP_DECR_REF(inp); |
| 1587 | } else if (locked_tcb != stcb) { |
| 1588 | SCTP_TCB_LOCK(locked_tcb); |
| 1589 | } |
| 1590 | if (locked_tcb) { |
| 1591 | atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); |
| 1592 | } |
| 1593 | |
| 1594 | SCTP_INP_WUNLOCK(inp); |
| 1595 | SCTP_INP_INFO_RUNLOCK(); |
| 1596 | return (stcb); |
| 1597 | } |
| 1598 | break; |
| 1599 | } |
| 1600 | #endif |
| 1601 | #ifdef INET6 |
| 1602 | case AF_INET6: |
| 1603 | { |
| 1604 | struct sockaddr_in6 *sin6, *rsin6; |
| 1605 | |
| 1606 | sin6 = (struct sockaddr_in6 *)&net->ro._l_addr; |
| 1607 | rsin6 = (struct sockaddr_in6 *)remote; |
| 1608 | if (SCTP6_ARE_ADDR_EQUAL(sin6, |
| 1609 | rsin6)) { |
| 1610 | /* found it */ |
| 1611 | if (netp != NULL) { |
| 1612 | *netp = net; |
| 1613 | } |
| 1614 | if (locked_tcb == NULL) { |
| 1615 | SCTP_INP_DECR_REF(inp); |
| 1616 | } else if (locked_tcb != stcb) { |
| 1617 | SCTP_TCB_LOCK(locked_tcb); |
| 1618 | } |
| 1619 | if (locked_tcb) { |
| 1620 | atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); |
| 1621 | } |
| 1622 | SCTP_INP_WUNLOCK(inp); |
| 1623 | SCTP_INP_INFO_RUNLOCK(); |
| 1624 | return (stcb); |
| 1625 | } |
| 1626 | break; |
| 1627 | } |
| 1628 | #endif |
tuexen | 5d9b974 | 2012-07-11 21:52:01 +0000 | [diff] [blame] | 1629 | #if defined(__Userspace__) |
| 1630 | case AF_CONN: |
| 1631 | { |
| 1632 | struct sockaddr_conn *sconn, *rsconn; |
| 1633 | |
| 1634 | sconn = (struct sockaddr_conn *)&net->ro._l_addr; |
| 1635 | rsconn = (struct sockaddr_conn *)remote; |
| 1636 | if (sconn->sconn_addr == rsconn->sconn_addr) { |
| 1637 | /* found it */ |
| 1638 | if (netp != NULL) { |
| 1639 | *netp = net; |
| 1640 | } |
| 1641 | if (locked_tcb == NULL) { |
| 1642 | SCTP_INP_DECR_REF(inp); |
| 1643 | } else if (locked_tcb != stcb) { |
| 1644 | SCTP_TCB_LOCK(locked_tcb); |
| 1645 | } |
| 1646 | if (locked_tcb) { |
| 1647 | atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); |
| 1648 | } |
| 1649 | SCTP_INP_WUNLOCK(inp); |
| 1650 | SCTP_INP_INFO_RUNLOCK(); |
| 1651 | return (stcb); |
| 1652 | } |
| 1653 | break; |
| 1654 | } |
| 1655 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1656 | default: |
| 1657 | /* TSNH */ |
| 1658 | break; |
| 1659 | } |
| 1660 | } |
| 1661 | SCTP_TCB_UNLOCK(stcb); |
| 1662 | } |
| 1663 | } else { |
| 1664 | SCTP_INP_WLOCK(inp); |
| 1665 | if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
| 1666 | goto null_return; |
| 1667 | } |
| 1668 | head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(rport, |
t00fcxen | 1369cd4 | 2015-01-11 21:57:30 +0000 | [diff] [blame] | 1669 | inp->sctp_hashmark)]; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1670 | LIST_FOREACH(stcb, head, sctp_tcbhash) { |
| 1671 | if (stcb->rport != rport) { |
| 1672 | /* remote port does not match */ |
| 1673 | continue; |
| 1674 | } |
| 1675 | SCTP_TCB_LOCK(stcb); |
| 1676 | if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { |
| 1677 | SCTP_TCB_UNLOCK(stcb); |
| 1678 | continue; |
| 1679 | } |
| 1680 | if (local && !sctp_does_stcb_own_this_addr(stcb, local)) { |
| 1681 | SCTP_TCB_UNLOCK(stcb); |
| 1682 | continue; |
| 1683 | } |
| 1684 | /* now look at the list of remote addresses */ |
| 1685 | TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { |
| 1686 | #ifdef INVARIANTS |
| 1687 | if (net == (TAILQ_NEXT(net, sctp_next))) { |
| 1688 | panic("Corrupt net list"); |
| 1689 | } |
| 1690 | #endif |
| 1691 | if (net->ro._l_addr.sa.sa_family != |
| 1692 | remote->sa_family) { |
| 1693 | /* not the same family */ |
| 1694 | continue; |
| 1695 | } |
| 1696 | switch (remote->sa_family) { |
| 1697 | #ifdef INET |
| 1698 | case AF_INET: |
| 1699 | { |
| 1700 | struct sockaddr_in *sin, *rsin; |
| 1701 | |
| 1702 | sin = (struct sockaddr_in *) |
| 1703 | &net->ro._l_addr; |
| 1704 | rsin = (struct sockaddr_in *)remote; |
| 1705 | if (sin->sin_addr.s_addr == |
| 1706 | rsin->sin_addr.s_addr) { |
| 1707 | /* found it */ |
| 1708 | if (netp != NULL) { |
| 1709 | *netp = net; |
| 1710 | } |
| 1711 | if (locked_tcb == NULL) { |
| 1712 | SCTP_INP_DECR_REF(inp); |
| 1713 | } else if (locked_tcb != stcb) { |
| 1714 | SCTP_TCB_LOCK(locked_tcb); |
| 1715 | } |
| 1716 | if (locked_tcb) { |
| 1717 | atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); |
| 1718 | } |
| 1719 | SCTP_INP_WUNLOCK(inp); |
| 1720 | SCTP_INP_INFO_RUNLOCK(); |
| 1721 | return (stcb); |
| 1722 | } |
| 1723 | break; |
| 1724 | } |
| 1725 | #endif |
| 1726 | #ifdef INET6 |
| 1727 | case AF_INET6: |
| 1728 | { |
| 1729 | struct sockaddr_in6 *sin6, *rsin6; |
| 1730 | |
| 1731 | sin6 = (struct sockaddr_in6 *) |
| 1732 | &net->ro._l_addr; |
| 1733 | rsin6 = (struct sockaddr_in6 *)remote; |
| 1734 | if (SCTP6_ARE_ADDR_EQUAL(sin6, |
| 1735 | rsin6)) { |
| 1736 | /* found it */ |
| 1737 | if (netp != NULL) { |
| 1738 | *netp = net; |
| 1739 | } |
| 1740 | if (locked_tcb == NULL) { |
| 1741 | SCTP_INP_DECR_REF(inp); |
| 1742 | } else if (locked_tcb != stcb) { |
| 1743 | SCTP_TCB_LOCK(locked_tcb); |
| 1744 | } |
| 1745 | if (locked_tcb) { |
| 1746 | atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); |
| 1747 | } |
| 1748 | SCTP_INP_WUNLOCK(inp); |
| 1749 | SCTP_INP_INFO_RUNLOCK(); |
| 1750 | return (stcb); |
| 1751 | } |
| 1752 | break; |
| 1753 | } |
| 1754 | #endif |
tuexen | 5d9b974 | 2012-07-11 21:52:01 +0000 | [diff] [blame] | 1755 | #if defined(__Userspace__) |
| 1756 | case AF_CONN: |
| 1757 | { |
| 1758 | struct sockaddr_conn *sconn, *rsconn; |
| 1759 | |
| 1760 | sconn = (struct sockaddr_conn *)&net->ro._l_addr; |
| 1761 | rsconn = (struct sockaddr_conn *)remote; |
| 1762 | if (sconn->sconn_addr == rsconn->sconn_addr) { |
| 1763 | /* found it */ |
| 1764 | if (netp != NULL) { |
| 1765 | *netp = net; |
| 1766 | } |
| 1767 | if (locked_tcb == NULL) { |
| 1768 | SCTP_INP_DECR_REF(inp); |
| 1769 | } else if (locked_tcb != stcb) { |
| 1770 | SCTP_TCB_LOCK(locked_tcb); |
| 1771 | } |
| 1772 | if (locked_tcb) { |
| 1773 | atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); |
| 1774 | } |
| 1775 | SCTP_INP_WUNLOCK(inp); |
| 1776 | SCTP_INP_INFO_RUNLOCK(); |
| 1777 | return (stcb); |
| 1778 | } |
| 1779 | break; |
| 1780 | } |
| 1781 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1782 | default: |
| 1783 | /* TSNH */ |
| 1784 | break; |
| 1785 | } |
| 1786 | } |
| 1787 | SCTP_TCB_UNLOCK(stcb); |
| 1788 | } |
| 1789 | } |
| 1790 | null_return: |
| 1791 | /* clean up for returning null */ |
| 1792 | if (locked_tcb) { |
| 1793 | SCTP_TCB_LOCK(locked_tcb); |
| 1794 | atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); |
| 1795 | } |
| 1796 | SCTP_INP_WUNLOCK(inp); |
| 1797 | SCTP_INP_INFO_RUNLOCK(); |
| 1798 | /* not found */ |
| 1799 | return (NULL); |
| 1800 | } |
| 1801 | |
| 1802 | /* |
| 1803 | * Find an association for a specific endpoint using the association id given |
| 1804 | * out in the COMM_UP notification |
| 1805 | */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1806 | struct sctp_tcb * |
| 1807 | sctp_findasoc_ep_asocid_locked(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock) |
| 1808 | { |
| 1809 | /* |
| 1810 | * Use my the assoc_id to find a endpoint |
| 1811 | */ |
| 1812 | struct sctpasochead *head; |
| 1813 | struct sctp_tcb *stcb; |
| 1814 | uint32_t id; |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 1815 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1816 | if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
| 1817 | SCTP_PRINTF("TSNH ep_associd0\n"); |
| 1818 | return (NULL); |
| 1819 | } |
| 1820 | id = (uint32_t)asoc_id; |
| 1821 | head = &inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(id, inp->hashasocidmark)]; |
| 1822 | if (head == NULL) { |
| 1823 | /* invalid id TSNH */ |
| 1824 | SCTP_PRINTF("TSNH ep_associd1\n"); |
| 1825 | return (NULL); |
| 1826 | } |
| 1827 | LIST_FOREACH(stcb, head, sctp_tcbasocidhash) { |
| 1828 | if (stcb->asoc.assoc_id == id) { |
| 1829 | if (inp != stcb->sctp_ep) { |
| 1830 | /* |
| 1831 | * some other guy has the same id active (id |
| 1832 | * collision ??). |
| 1833 | */ |
| 1834 | SCTP_PRINTF("TSNH ep_associd2\n"); |
| 1835 | continue; |
| 1836 | } |
| 1837 | if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { |
| 1838 | continue; |
| 1839 | } |
| 1840 | if (want_lock) { |
| 1841 | SCTP_TCB_LOCK(stcb); |
| 1842 | } |
| 1843 | return (stcb); |
| 1844 | } |
| 1845 | } |
| 1846 | return (NULL); |
| 1847 | } |
| 1848 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1849 | struct sctp_tcb * |
| 1850 | sctp_findassociation_ep_asocid(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock) |
| 1851 | { |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 1852 | struct sctp_tcb *stcb; |
| 1853 | |
| 1854 | SCTP_INP_RLOCK(inp); |
| 1855 | stcb = sctp_findasoc_ep_asocid_locked(inp, asoc_id, want_lock); |
| 1856 | SCTP_INP_RUNLOCK(inp); |
| 1857 | return (stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1858 | } |
| 1859 | |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 1860 | /* |
| 1861 | * Endpoint probe expects that the INP_INFO is locked. |
| 1862 | */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1863 | static struct sctp_inpcb * |
| 1864 | sctp_endpoint_probe(struct sockaddr *nam, struct sctppcbhead *head, |
| 1865 | uint16_t lport, uint32_t vrf_id) |
| 1866 | { |
| 1867 | struct sctp_inpcb *inp; |
| 1868 | struct sctp_laddr *laddr; |
| 1869 | #ifdef INET |
| 1870 | struct sockaddr_in *sin; |
| 1871 | #endif |
| 1872 | #ifdef INET6 |
| 1873 | struct sockaddr_in6 *sin6; |
| 1874 | struct sockaddr_in6 *intf_addr6; |
| 1875 | #endif |
tuexen | 09a676e | 2012-07-08 21:03:18 +0000 | [diff] [blame] | 1876 | #if defined(__Userspace__) |
| 1877 | struct sockaddr_conn *sconn; |
| 1878 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1879 | #ifdef SCTP_MVRF |
| 1880 | int i; |
| 1881 | #endif |
| 1882 | int fnd; |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 1883 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1884 | #ifdef INET |
| 1885 | sin = NULL; |
| 1886 | #endif |
| 1887 | #ifdef INET6 |
| 1888 | sin6 = NULL; |
| 1889 | #endif |
t00fcxen | b4c54ac | 2012-09-15 09:17:50 +0000 | [diff] [blame] | 1890 | #if defined(__Userspace__) |
| 1891 | sconn = NULL; |
| 1892 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1893 | switch (nam->sa_family) { |
| 1894 | #ifdef INET |
| 1895 | case AF_INET: |
| 1896 | sin = (struct sockaddr_in *)nam; |
| 1897 | break; |
| 1898 | #endif |
| 1899 | #ifdef INET6 |
| 1900 | case AF_INET6: |
| 1901 | sin6 = (struct sockaddr_in6 *)nam; |
| 1902 | break; |
| 1903 | #endif |
tuexen | 09a676e | 2012-07-08 21:03:18 +0000 | [diff] [blame] | 1904 | #if defined(__Userspace__) |
| 1905 | case AF_CONN: |
| 1906 | sconn = (struct sockaddr_conn *)nam; |
| 1907 | break; |
| 1908 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1909 | default: |
| 1910 | /* unsupported family */ |
| 1911 | return (NULL); |
| 1912 | } |
| 1913 | |
| 1914 | if (head == NULL) |
| 1915 | return (NULL); |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 1916 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1917 | LIST_FOREACH(inp, head, sctp_hash) { |
| 1918 | SCTP_INP_RLOCK(inp); |
| 1919 | if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
| 1920 | SCTP_INP_RUNLOCK(inp); |
| 1921 | continue; |
| 1922 | } |
| 1923 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) && |
| 1924 | (inp->sctp_lport == lport)) { |
| 1925 | /* got it */ |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 1926 | switch (nam->sa_family) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1927 | #ifdef INET |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 1928 | case AF_INET: |
| 1929 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) && |
Michael Tuexen | 72e5cec | 2019-08-05 13:21:02 +0200 | [diff] [blame] | 1930 | SCTP_IPV6_V6ONLY(inp)) { |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 1931 | /* IPv4 on a IPv6 socket with ONLY IPv6 set */ |
| 1932 | SCTP_INP_RUNLOCK(inp); |
| 1933 | continue; |
| 1934 | } |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 1935 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 1936 | if (prison_check_ip4(inp->ip_inp.inp.inp_cred, |
| 1937 | &sin->sin_addr) != 0) { |
| 1938 | SCTP_INP_RUNLOCK(inp); |
| 1939 | continue; |
| 1940 | } |
| 1941 | #endif |
| 1942 | break; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1943 | #endif |
| 1944 | #ifdef INET6 |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 1945 | case AF_INET6: |
| 1946 | /* A V6 address and the endpoint is NOT bound V6 */ |
| 1947 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) { |
| 1948 | SCTP_INP_RUNLOCK(inp); |
| 1949 | continue; |
| 1950 | } |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 1951 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 1952 | if (prison_check_ip6(inp->ip_inp.inp.inp_cred, |
| 1953 | &sin6->sin6_addr) != 0) { |
| 1954 | SCTP_INP_RUNLOCK(inp); |
| 1955 | continue; |
| 1956 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1957 | #endif |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 1958 | break; |
| 1959 | #endif |
| 1960 | default: |
| 1961 | break; |
| 1962 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1963 | /* does a VRF id match? */ |
| 1964 | fnd = 0; |
| 1965 | #ifdef SCTP_MVRF |
| 1966 | for (i = 0; i < inp->num_vrfs; i++) { |
| 1967 | if (inp->m_vrf_ids[i] == vrf_id) { |
| 1968 | fnd = 1; |
| 1969 | break; |
| 1970 | } |
| 1971 | } |
| 1972 | #else |
| 1973 | if (inp->def_vrf_id == vrf_id) |
| 1974 | fnd = 1; |
| 1975 | #endif |
| 1976 | |
| 1977 | SCTP_INP_RUNLOCK(inp); |
| 1978 | if (!fnd) |
| 1979 | continue; |
| 1980 | return (inp); |
| 1981 | } |
| 1982 | SCTP_INP_RUNLOCK(inp); |
| 1983 | } |
| 1984 | switch (nam->sa_family) { |
| 1985 | #ifdef INET |
| 1986 | case AF_INET: |
| 1987 | if (sin->sin_addr.s_addr == INADDR_ANY) { |
| 1988 | /* Can't hunt for one that has no address specified */ |
| 1989 | return (NULL); |
| 1990 | } |
| 1991 | break; |
| 1992 | #endif |
| 1993 | #ifdef INET6 |
| 1994 | case AF_INET6: |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 1995 | if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1996 | /* Can't hunt for one that has no address specified */ |
| 1997 | return (NULL); |
| 1998 | } |
| 1999 | break; |
| 2000 | #endif |
tuexen | 09a676e | 2012-07-08 21:03:18 +0000 | [diff] [blame] | 2001 | #if defined(__Userspace__) |
| 2002 | case AF_CONN: |
| 2003 | if (sconn->sconn_addr == NULL) { |
| 2004 | return (NULL); |
| 2005 | } |
| 2006 | break; |
| 2007 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2008 | default: |
| 2009 | break; |
| 2010 | } |
| 2011 | /* |
| 2012 | * ok, not bound to all so see if we can find a EP bound to this |
| 2013 | * address. |
| 2014 | */ |
| 2015 | LIST_FOREACH(inp, head, sctp_hash) { |
| 2016 | SCTP_INP_RLOCK(inp); |
| 2017 | if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
| 2018 | SCTP_INP_RUNLOCK(inp); |
| 2019 | continue; |
| 2020 | } |
| 2021 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)) { |
| 2022 | SCTP_INP_RUNLOCK(inp); |
| 2023 | continue; |
| 2024 | } |
| 2025 | /* |
| 2026 | * Ok this could be a likely candidate, look at all of its |
| 2027 | * addresses |
| 2028 | */ |
| 2029 | if (inp->sctp_lport != lport) { |
| 2030 | SCTP_INP_RUNLOCK(inp); |
| 2031 | continue; |
| 2032 | } |
| 2033 | /* does a VRF id match? */ |
| 2034 | fnd = 0; |
| 2035 | #ifdef SCTP_MVRF |
| 2036 | for (i = 0; i < inp->num_vrfs; i++) { |
| 2037 | if (inp->m_vrf_ids[i] == vrf_id) { |
| 2038 | fnd = 1; |
| 2039 | break; |
| 2040 | } |
| 2041 | } |
| 2042 | #else |
| 2043 | if (inp->def_vrf_id == vrf_id) |
| 2044 | fnd = 1; |
| 2045 | |
| 2046 | #endif |
| 2047 | if (!fnd) { |
| 2048 | SCTP_INP_RUNLOCK(inp); |
| 2049 | continue; |
| 2050 | } |
| 2051 | LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { |
| 2052 | if (laddr->ifa == NULL) { |
| 2053 | SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n", |
Saúl Ibarra Corretgé | 8b04b41 | 2015-09-26 09:53:06 +0200 | [diff] [blame] | 2054 | __func__); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2055 | continue; |
| 2056 | } |
| 2057 | SCTPDBG(SCTP_DEBUG_PCB1, "Ok laddr->ifa:%p is possible, ", |
t00fcxen | 8fca7ff | 2012-09-05 18:58:42 +0000 | [diff] [blame] | 2058 | (void *)laddr->ifa); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2059 | if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) { |
| 2060 | SCTPDBG(SCTP_DEBUG_PCB1, "Huh IFA being deleted\n"); |
| 2061 | continue; |
| 2062 | } |
| 2063 | if (laddr->ifa->address.sa.sa_family == nam->sa_family) { |
| 2064 | /* possible, see if it matches */ |
| 2065 | switch (nam->sa_family) { |
| 2066 | #ifdef INET |
| 2067 | case AF_INET: |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 2068 | #if defined(__APPLE__) && !defined(__Userspace__) |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 2069 | if (sin == NULL) { |
| 2070 | /* TSNH */ |
| 2071 | break; |
| 2072 | } |
| 2073 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2074 | if (sin->sin_addr.s_addr == |
| 2075 | laddr->ifa->address.sin.sin_addr.s_addr) { |
| 2076 | SCTP_INP_RUNLOCK(inp); |
| 2077 | return (inp); |
| 2078 | } |
| 2079 | break; |
| 2080 | #endif |
| 2081 | #ifdef INET6 |
| 2082 | case AF_INET6: |
| 2083 | intf_addr6 = &laddr->ifa->address.sin6; |
| 2084 | if (SCTP6_ARE_ADDR_EQUAL(sin6, |
| 2085 | intf_addr6)) { |
| 2086 | SCTP_INP_RUNLOCK(inp); |
| 2087 | return (inp); |
| 2088 | } |
| 2089 | break; |
| 2090 | #endif |
tuexen | 09a676e | 2012-07-08 21:03:18 +0000 | [diff] [blame] | 2091 | #if defined(__Userspace__) |
| 2092 | case AF_CONN: |
| 2093 | if (sconn->sconn_addr == laddr->ifa->address.sconn.sconn_addr) { |
| 2094 | SCTP_INP_RUNLOCK(inp); |
| 2095 | return (inp); |
| 2096 | } |
| 2097 | break; |
| 2098 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2099 | } |
| 2100 | } |
| 2101 | } |
| 2102 | SCTP_INP_RUNLOCK(inp); |
| 2103 | } |
| 2104 | return (NULL); |
| 2105 | } |
| 2106 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2107 | static struct sctp_inpcb * |
| 2108 | sctp_isport_inuse(struct sctp_inpcb *inp, uint16_t lport, uint32_t vrf_id) |
| 2109 | { |
| 2110 | struct sctppcbhead *head; |
| 2111 | struct sctp_inpcb *t_inp; |
| 2112 | #ifdef SCTP_MVRF |
| 2113 | int i; |
| 2114 | #endif |
| 2115 | int fnd; |
| 2116 | |
| 2117 | head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport, |
| 2118 | SCTP_BASE_INFO(hashmark))]; |
| 2119 | LIST_FOREACH(t_inp, head, sctp_hash) { |
| 2120 | if (t_inp->sctp_lport != lport) { |
| 2121 | continue; |
| 2122 | } |
| 2123 | /* is it in the VRF in question */ |
| 2124 | fnd = 0; |
| 2125 | #ifdef SCTP_MVRF |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 2126 | for (i = 0; i < inp->num_vrfs; i++) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2127 | if (t_inp->m_vrf_ids[i] == vrf_id) { |
| 2128 | fnd = 1; |
| 2129 | break; |
| 2130 | } |
| 2131 | } |
| 2132 | #else |
| 2133 | if (t_inp->def_vrf_id == vrf_id) |
| 2134 | fnd = 1; |
| 2135 | #endif |
| 2136 | if (!fnd) |
| 2137 | continue; |
| 2138 | |
| 2139 | /* This one is in use. */ |
| 2140 | /* check the v6/v4 binding issue */ |
| 2141 | if ((t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) && |
Michael Tuexen | 72e5cec | 2019-08-05 13:21:02 +0200 | [diff] [blame] | 2142 | SCTP_IPV6_V6ONLY(t_inp)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2143 | if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { |
| 2144 | /* collision in V6 space */ |
| 2145 | return (t_inp); |
| 2146 | } else { |
| 2147 | /* inp is BOUND_V4 no conflict */ |
| 2148 | continue; |
| 2149 | } |
| 2150 | } else if (t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { |
| 2151 | /* t_inp is bound v4 and v6, conflict always */ |
| 2152 | return (t_inp); |
| 2153 | } else { |
| 2154 | /* t_inp is bound only V4 */ |
| 2155 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) && |
Michael Tuexen | 72e5cec | 2019-08-05 13:21:02 +0200 | [diff] [blame] | 2156 | SCTP_IPV6_V6ONLY(inp)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2157 | /* no conflict */ |
| 2158 | continue; |
| 2159 | } |
| 2160 | /* else fall through to conflict */ |
| 2161 | } |
| 2162 | return (t_inp); |
| 2163 | } |
| 2164 | return (NULL); |
| 2165 | } |
| 2166 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2167 | int |
| 2168 | sctp_swap_inpcb_for_listen(struct sctp_inpcb *inp) |
| 2169 | { |
| 2170 | /* For 1-2-1 with port reuse */ |
| 2171 | struct sctppcbhead *head; |
t00fcxen | c00f4bc | 2015-03-24 21:17:16 +0000 | [diff] [blame] | 2172 | struct sctp_inpcb *tinp, *ninp; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2173 | |
Michael Tuexen | b43ed92 | 2021-09-08 22:46:39 +0200 | [diff] [blame] | 2174 | SCTP_INP_INFO_WLOCK_ASSERT(); |
| 2175 | SCTP_INP_WLOCK_ASSERT(inp); |
| 2176 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2177 | if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) { |
| 2178 | /* only works with port reuse on */ |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 2179 | return (-1); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2180 | } |
| 2181 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) { |
| 2182 | return (0); |
| 2183 | } |
Michael Tuexen | b43ed92 | 2021-09-08 22:46:39 +0200 | [diff] [blame] | 2184 | SCTP_INP_WUNLOCK(inp); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2185 | head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(inp->sctp_lport, |
| 2186 | SCTP_BASE_INFO(hashmark))]; |
| 2187 | /* Kick out all non-listeners to the TCP hash */ |
t00fcxen | c00f4bc | 2015-03-24 21:17:16 +0000 | [diff] [blame] | 2188 | LIST_FOREACH_SAFE(tinp, head, sctp_hash, ninp) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2189 | if (tinp->sctp_lport != inp->sctp_lport) { |
| 2190 | continue; |
| 2191 | } |
| 2192 | if (tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
| 2193 | continue; |
| 2194 | } |
| 2195 | if (tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { |
| 2196 | continue; |
| 2197 | } |
Michael Tuexen | d900e5f | 2017-06-23 10:52:43 +0200 | [diff] [blame] | 2198 | if (SCTP_IS_LISTENING(tinp)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2199 | continue; |
| 2200 | } |
| 2201 | SCTP_INP_WLOCK(tinp); |
| 2202 | LIST_REMOVE(tinp, sctp_hash); |
| 2203 | head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR(tinp->sctp_lport, SCTP_BASE_INFO(hashtcpmark))]; |
| 2204 | tinp->sctp_flags |= SCTP_PCB_FLAGS_IN_TCPPOOL; |
| 2205 | LIST_INSERT_HEAD(head, tinp, sctp_hash); |
| 2206 | SCTP_INP_WUNLOCK(tinp); |
| 2207 | } |
| 2208 | SCTP_INP_WLOCK(inp); |
| 2209 | /* Pull from where he was */ |
| 2210 | LIST_REMOVE(inp, sctp_hash); |
| 2211 | inp->sctp_flags &= ~SCTP_PCB_FLAGS_IN_TCPPOOL; |
| 2212 | head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(inp->sctp_lport, SCTP_BASE_INFO(hashmark))]; |
| 2213 | LIST_INSERT_HEAD(head, inp, sctp_hash); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2214 | return (0); |
| 2215 | } |
| 2216 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2217 | struct sctp_inpcb * |
| 2218 | sctp_pcb_findep(struct sockaddr *nam, int find_tcp_pool, int have_lock, |
| 2219 | uint32_t vrf_id) |
| 2220 | { |
| 2221 | /* |
| 2222 | * First we check the hash table to see if someone has this port |
| 2223 | * bound with just the port. |
| 2224 | */ |
| 2225 | struct sctp_inpcb *inp; |
| 2226 | struct sctppcbhead *head; |
| 2227 | int lport; |
| 2228 | unsigned int i; |
| 2229 | #ifdef INET |
| 2230 | struct sockaddr_in *sin; |
| 2231 | #endif |
| 2232 | #ifdef INET6 |
| 2233 | struct sockaddr_in6 *sin6; |
| 2234 | #endif |
tuexen | 09a676e | 2012-07-08 21:03:18 +0000 | [diff] [blame] | 2235 | #if defined(__Userspace__) |
| 2236 | struct sockaddr_conn *sconn; |
| 2237 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2238 | |
| 2239 | switch (nam->sa_family) { |
| 2240 | #ifdef INET |
| 2241 | case AF_INET: |
| 2242 | sin = (struct sockaddr_in *)nam; |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 2243 | lport = sin->sin_port; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2244 | break; |
| 2245 | #endif |
| 2246 | #ifdef INET6 |
| 2247 | case AF_INET6: |
| 2248 | sin6 = (struct sockaddr_in6 *)nam; |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 2249 | lport = sin6->sin6_port; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2250 | break; |
| 2251 | #endif |
tuexen | 09a676e | 2012-07-08 21:03:18 +0000 | [diff] [blame] | 2252 | #if defined(__Userspace__) |
| 2253 | case AF_CONN: |
| 2254 | sconn = (struct sockaddr_conn *)nam; |
| 2255 | lport = sconn->sconn_port; |
| 2256 | break; |
| 2257 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2258 | default: |
| 2259 | return (NULL); |
| 2260 | } |
| 2261 | /* |
| 2262 | * I could cheat here and just cast to one of the types but we will |
| 2263 | * do it right. It also provides the check against an Unsupported |
| 2264 | * type too. |
| 2265 | */ |
| 2266 | /* Find the head of the ALLADDR chain */ |
| 2267 | if (have_lock == 0) { |
| 2268 | SCTP_INP_INFO_RLOCK(); |
| 2269 | } |
| 2270 | head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport, |
| 2271 | SCTP_BASE_INFO(hashmark))]; |
| 2272 | inp = sctp_endpoint_probe(nam, head, lport, vrf_id); |
| 2273 | |
| 2274 | /* |
| 2275 | * If the TCP model exists it could be that the main listening |
| 2276 | * endpoint is gone but there still exists a connected socket for this |
| 2277 | * guy. If so we can return the first one that we find. This may NOT |
| 2278 | * be the correct one so the caller should be wary on the returned INP. |
| 2279 | * Currently the only caller that sets find_tcp_pool is in bindx where |
| 2280 | * we are verifying that a user CAN bind the address. He either |
| 2281 | * has bound it already, or someone else has, or its open to bind, |
| 2282 | * so this is good enough. |
| 2283 | */ |
| 2284 | if (inp == NULL && find_tcp_pool) { |
| 2285 | for (i = 0; i < SCTP_BASE_INFO(hashtcpmark) + 1; i++) { |
| 2286 | head = &SCTP_BASE_INFO(sctp_tcpephash)[i]; |
| 2287 | inp = sctp_endpoint_probe(nam, head, lport, vrf_id); |
| 2288 | if (inp) { |
| 2289 | break; |
| 2290 | } |
| 2291 | } |
| 2292 | } |
| 2293 | if (inp) { |
| 2294 | SCTP_INP_INCR_REF(inp); |
| 2295 | } |
| 2296 | if (have_lock == 0) { |
| 2297 | SCTP_INP_INFO_RUNLOCK(); |
| 2298 | } |
| 2299 | return (inp); |
| 2300 | } |
| 2301 | |
| 2302 | /* |
| 2303 | * Find an association for an endpoint with the pointer to whom you want to |
| 2304 | * send to and the endpoint pointer. The address can be IPv4 or IPv6. We may |
| 2305 | * need to change the *to to some other struct like a mbuf... |
| 2306 | */ |
| 2307 | struct sctp_tcb * |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 2308 | sctp_findassociation_addr_sa(struct sockaddr *from, struct sockaddr *to, |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2309 | struct sctp_inpcb **inp_p, struct sctp_nets **netp, int find_tcp_pool, |
| 2310 | uint32_t vrf_id) |
| 2311 | { |
| 2312 | struct sctp_inpcb *inp = NULL; |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2313 | struct sctp_tcb *stcb; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2314 | |
| 2315 | SCTP_INP_INFO_RLOCK(); |
| 2316 | if (find_tcp_pool) { |
| 2317 | if (inp_p != NULL) { |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2318 | stcb = sctp_tcb_special_locate(inp_p, from, to, netp, |
| 2319 | vrf_id); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2320 | } else { |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2321 | stcb = sctp_tcb_special_locate(&inp, from, to, netp, |
| 2322 | vrf_id); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2323 | } |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2324 | if (stcb != NULL) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2325 | SCTP_INP_INFO_RUNLOCK(); |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2326 | return (stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2327 | } |
| 2328 | } |
| 2329 | inp = sctp_pcb_findep(to, 0, 1, vrf_id); |
| 2330 | if (inp_p != NULL) { |
| 2331 | *inp_p = inp; |
| 2332 | } |
| 2333 | SCTP_INP_INFO_RUNLOCK(); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2334 | if (inp == NULL) { |
| 2335 | return (NULL); |
| 2336 | } |
| 2337 | /* |
| 2338 | * ok, we have an endpoint, now lets find the assoc for it (if any) |
| 2339 | * we now place the source address or from in the to of the find |
| 2340 | * endpoint call. Since in reality this chain is used from the |
| 2341 | * inbound packet side. |
| 2342 | */ |
| 2343 | if (inp_p != NULL) { |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2344 | stcb = sctp_findassociation_ep_addr(inp_p, from, netp, to, |
| 2345 | NULL); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2346 | } else { |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2347 | stcb = sctp_findassociation_ep_addr(&inp, from, netp, to, |
| 2348 | NULL); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2349 | } |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2350 | return (stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2351 | } |
| 2352 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2353 | /* |
| 2354 | * This routine will grub through the mbuf that is a INIT or INIT-ACK and |
| 2355 | * find all addresses that the sender has specified in any address list. Each |
| 2356 | * address will be used to lookup the TCB and see if one exits. |
| 2357 | */ |
| 2358 | static struct sctp_tcb * |
tuexen | 9784e9a | 2011-12-18 13:04:23 +0000 | [diff] [blame] | 2359 | sctp_findassociation_special_addr(struct mbuf *m, int offset, |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2360 | struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp, |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 2361 | struct sockaddr *dst) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2362 | { |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 2363 | struct sctp_paramhdr *phdr, param_buf; |
t00fcxen | aff14b6 | 2013-11-02 18:02:02 +0000 | [diff] [blame] | 2364 | #if defined(INET) || defined(INET6) |
t00fcxen | 6ebb603 | 2013-11-02 18:03:31 +0000 | [diff] [blame] | 2365 | struct sctp_tcb *stcb; |
t00fcxen | aff14b6 | 2013-11-02 18:02:02 +0000 | [diff] [blame] | 2366 | uint16_t ptype; |
| 2367 | #endif |
| 2368 | uint16_t plen; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2369 | #ifdef INET |
| 2370 | struct sockaddr_in sin4; |
| 2371 | #endif |
| 2372 | #ifdef INET6 |
| 2373 | struct sockaddr_in6 sin6; |
| 2374 | #endif |
| 2375 | |
| 2376 | #ifdef INET |
| 2377 | memset(&sin4, 0, sizeof(sin4)); |
t00fcxen | 8fbaee3 | 2012-09-04 16:41:39 +0000 | [diff] [blame] | 2378 | #ifdef HAVE_SIN_LEN |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2379 | sin4.sin_len = sizeof(sin4); |
| 2380 | #endif |
| 2381 | sin4.sin_family = AF_INET; |
| 2382 | sin4.sin_port = sh->src_port; |
| 2383 | #endif |
| 2384 | #ifdef INET6 |
| 2385 | memset(&sin6, 0, sizeof(sin6)); |
t00fcxen | 8fbaee3 | 2012-09-04 16:41:39 +0000 | [diff] [blame] | 2386 | #ifdef HAVE_SIN6_LEN |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2387 | sin6.sin6_len = sizeof(sin6); |
| 2388 | #endif |
| 2389 | sin6.sin6_family = AF_INET6; |
| 2390 | sin6.sin6_port = sh->src_port; |
| 2391 | #endif |
| 2392 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2393 | offset += sizeof(struct sctp_init_chunk); |
| 2394 | |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 2395 | phdr = sctp_get_next_param(m, offset, ¶m_buf, sizeof(param_buf)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2396 | while (phdr != NULL) { |
| 2397 | /* now we must see if we want the parameter */ |
t00fcxen | aff14b6 | 2013-11-02 18:02:02 +0000 | [diff] [blame] | 2398 | #if defined(INET) || defined(INET6) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2399 | ptype = ntohs(phdr->param_type); |
t00fcxen | aff14b6 | 2013-11-02 18:02:02 +0000 | [diff] [blame] | 2400 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2401 | plen = ntohs(phdr->param_length); |
| 2402 | if (plen == 0) { |
| 2403 | break; |
| 2404 | } |
| 2405 | #ifdef INET |
| 2406 | if (ptype == SCTP_IPV4_ADDRESS && |
| 2407 | plen == sizeof(struct sctp_ipv4addr_param)) { |
| 2408 | /* Get the rest of the address */ |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 2409 | struct sctp_ipv4addr_param ip4_param, *p4; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2410 | |
| 2411 | phdr = sctp_get_next_param(m, offset, |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 2412 | (struct sctp_paramhdr *)&ip4_param, sizeof(ip4_param)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2413 | if (phdr == NULL) { |
| 2414 | return (NULL); |
| 2415 | } |
| 2416 | p4 = (struct sctp_ipv4addr_param *)phdr; |
| 2417 | memcpy(&sin4.sin_addr, &p4->addr, sizeof(p4->addr)); |
| 2418 | /* look it up */ |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2419 | stcb = sctp_findassociation_ep_addr(inp_p, |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 2420 | (struct sockaddr *)&sin4, netp, dst, NULL); |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2421 | if (stcb != NULL) { |
| 2422 | return (stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2423 | } |
| 2424 | } |
| 2425 | #endif |
| 2426 | #ifdef INET6 |
| 2427 | if (ptype == SCTP_IPV6_ADDRESS && |
| 2428 | plen == sizeof(struct sctp_ipv6addr_param)) { |
| 2429 | /* Get the rest of the address */ |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 2430 | struct sctp_ipv6addr_param ip6_param, *p6; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2431 | |
| 2432 | phdr = sctp_get_next_param(m, offset, |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 2433 | (struct sctp_paramhdr *)&ip6_param, sizeof(ip6_param)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2434 | if (phdr == NULL) { |
| 2435 | return (NULL); |
| 2436 | } |
| 2437 | p6 = (struct sctp_ipv6addr_param *)phdr; |
| 2438 | memcpy(&sin6.sin6_addr, &p6->addr, sizeof(p6->addr)); |
| 2439 | /* look it up */ |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2440 | stcb = sctp_findassociation_ep_addr(inp_p, |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 2441 | (struct sockaddr *)&sin6, netp, dst, NULL); |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2442 | if (stcb != NULL) { |
| 2443 | return (stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2444 | } |
| 2445 | } |
| 2446 | #endif |
| 2447 | offset += SCTP_SIZE32(plen); |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 2448 | phdr = sctp_get_next_param(m, offset, ¶m_buf, |
| 2449 | sizeof(param_buf)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2450 | } |
| 2451 | return (NULL); |
| 2452 | } |
| 2453 | |
| 2454 | static struct sctp_tcb * |
| 2455 | sctp_findassoc_by_vtag(struct sockaddr *from, struct sockaddr *to, uint32_t vtag, |
| 2456 | struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint16_t rport, |
| 2457 | uint16_t lport, int skip_src_check, uint32_t vrf_id, uint32_t remote_tag) |
| 2458 | { |
| 2459 | /* |
| 2460 | * Use my vtag to hash. If we find it we then verify the source addr |
| 2461 | * is in the assoc. If all goes well we save a bit on rec of a |
| 2462 | * packet. |
| 2463 | */ |
| 2464 | struct sctpasochead *head; |
| 2465 | struct sctp_nets *net; |
| 2466 | struct sctp_tcb *stcb; |
tuexen | 9784e9a | 2011-12-18 13:04:23 +0000 | [diff] [blame] | 2467 | #ifdef SCTP_MVRF |
| 2468 | unsigned int i; |
| 2469 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2470 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2471 | SCTP_INP_INFO_RLOCK(); |
| 2472 | head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(vtag, |
t00fcxen | 0d66488 | 2015-01-11 21:47:36 +0000 | [diff] [blame] | 2473 | SCTP_BASE_INFO(hashasocmark))]; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2474 | LIST_FOREACH(stcb, head, sctp_asocs) { |
| 2475 | SCTP_INP_RLOCK(stcb->sctp_ep); |
| 2476 | if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
| 2477 | SCTP_INP_RUNLOCK(stcb->sctp_ep); |
| 2478 | continue; |
| 2479 | } |
tuexen | 9784e9a | 2011-12-18 13:04:23 +0000 | [diff] [blame] | 2480 | #ifdef SCTP_MVRF |
| 2481 | for (i = 0; i < stcb->sctp_ep->num_vrfs; i++) { |
| 2482 | if (stcb->sctp_ep->m_vrf_ids[i] == vrf_id) { |
| 2483 | break; |
| 2484 | } |
| 2485 | } |
| 2486 | if (i == stcb->sctp_ep->num_vrfs) { |
| 2487 | SCTP_INP_RUNLOCK(inp); |
| 2488 | continue; |
| 2489 | } |
| 2490 | #else |
| 2491 | if (stcb->sctp_ep->def_vrf_id != vrf_id) { |
| 2492 | SCTP_INP_RUNLOCK(stcb->sctp_ep); |
| 2493 | continue; |
| 2494 | } |
| 2495 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2496 | SCTP_TCB_LOCK(stcb); |
| 2497 | SCTP_INP_RUNLOCK(stcb->sctp_ep); |
| 2498 | if (stcb->asoc.my_vtag == vtag) { |
| 2499 | /* candidate */ |
| 2500 | if (stcb->rport != rport) { |
| 2501 | SCTP_TCB_UNLOCK(stcb); |
| 2502 | continue; |
| 2503 | } |
| 2504 | if (stcb->sctp_ep->sctp_lport != lport) { |
| 2505 | SCTP_TCB_UNLOCK(stcb); |
| 2506 | continue; |
| 2507 | } |
| 2508 | if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { |
| 2509 | SCTP_TCB_UNLOCK(stcb); |
| 2510 | continue; |
| 2511 | } |
| 2512 | /* RRS:Need toaddr check here */ |
| 2513 | if (sctp_does_stcb_own_this_addr(stcb, to) == 0) { |
| 2514 | /* Endpoint does not own this address */ |
| 2515 | SCTP_TCB_UNLOCK(stcb); |
| 2516 | continue; |
| 2517 | } |
| 2518 | if (remote_tag) { |
| 2519 | /* If we have both vtags that's all we match on */ |
| 2520 | if (stcb->asoc.peer_vtag == remote_tag) { |
| 2521 | /* If both tags match we consider it conclusive |
| 2522 | * and check NO source/destination addresses |
| 2523 | */ |
| 2524 | goto conclusive; |
| 2525 | } |
| 2526 | } |
| 2527 | if (skip_src_check) { |
| 2528 | conclusive: |
| 2529 | if (from) { |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 2530 | *netp = sctp_findnet(stcb, from); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2531 | } else { |
| 2532 | *netp = NULL; /* unknown */ |
| 2533 | } |
| 2534 | if (inp_p) |
| 2535 | *inp_p = stcb->sctp_ep; |
| 2536 | SCTP_INP_INFO_RUNLOCK(); |
| 2537 | return (stcb); |
| 2538 | } |
| 2539 | net = sctp_findnet(stcb, from); |
| 2540 | if (net) { |
| 2541 | /* yep its him. */ |
| 2542 | *netp = net; |
| 2543 | SCTP_STAT_INCR(sctps_vtagexpress); |
| 2544 | *inp_p = stcb->sctp_ep; |
| 2545 | SCTP_INP_INFO_RUNLOCK(); |
| 2546 | return (stcb); |
| 2547 | } else { |
| 2548 | /* |
| 2549 | * not him, this should only happen in rare |
| 2550 | * cases so I peg it. |
| 2551 | */ |
| 2552 | SCTP_STAT_INCR(sctps_vtagbogus); |
| 2553 | } |
| 2554 | } |
| 2555 | SCTP_TCB_UNLOCK(stcb); |
| 2556 | } |
| 2557 | SCTP_INP_INFO_RUNLOCK(); |
| 2558 | return (NULL); |
| 2559 | } |
| 2560 | |
| 2561 | /* |
| 2562 | * Find an association with the pointer to the inbound IP packet. This can be |
| 2563 | * a IPv4 or IPv6 packet. |
| 2564 | */ |
| 2565 | struct sctp_tcb * |
tuexen | 9784e9a | 2011-12-18 13:04:23 +0000 | [diff] [blame] | 2566 | sctp_findassociation_addr(struct mbuf *m, int offset, |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 2567 | struct sockaddr *src, struct sockaddr *dst, |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2568 | struct sctphdr *sh, struct sctp_chunkhdr *ch, |
| 2569 | struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id) |
| 2570 | { |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2571 | struct sctp_tcb *stcb; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2572 | struct sctp_inpcb *inp; |
| 2573 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2574 | if (sh->v_tag) { |
| 2575 | /* we only go down this path if vtag is non-zero */ |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2576 | stcb = sctp_findassoc_by_vtag(src, dst, ntohl(sh->v_tag), |
t00fcxen | afd2119 | 2013-05-10 18:15:02 +0000 | [diff] [blame] | 2577 | inp_p, netp, sh->src_port, sh->dest_port, 0, vrf_id, 0); |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2578 | if (stcb) { |
| 2579 | return (stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2580 | } |
| 2581 | } |
| 2582 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2583 | if (inp_p) { |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2584 | stcb = sctp_findassociation_addr_sa(src, dst, inp_p, netp, |
Michael Tuexen | 7f20278 | 2016-01-28 17:09:30 +0100 | [diff] [blame] | 2585 | 1, vrf_id); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2586 | inp = *inp_p; |
| 2587 | } else { |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2588 | stcb = sctp_findassociation_addr_sa(src, dst, &inp, netp, |
Michael Tuexen | 7f20278 | 2016-01-28 17:09:30 +0100 | [diff] [blame] | 2589 | 1, vrf_id); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2590 | } |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2591 | SCTPDBG(SCTP_DEBUG_PCB1, "stcb:%p inp:%p\n", (void *)stcb, (void *)inp); |
| 2592 | if (stcb == NULL && inp) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2593 | /* Found a EP but not this address */ |
| 2594 | if ((ch->chunk_type == SCTP_INITIATION) || |
| 2595 | (ch->chunk_type == SCTP_INITIATION_ACK)) { |
| 2596 | /*- |
| 2597 | * special hook, we do NOT return linp or an |
| 2598 | * association that is linked to an existing |
| 2599 | * association that is under the TCP pool (i.e. no |
| 2600 | * listener exists). The endpoint finding routine |
| 2601 | * will always find a listener before examining the |
| 2602 | * TCP pool. |
| 2603 | */ |
| 2604 | if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) { |
| 2605 | if (inp_p) { |
| 2606 | *inp_p = NULL; |
| 2607 | } |
| 2608 | return (NULL); |
| 2609 | } |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2610 | stcb = sctp_findassociation_special_addr(m, |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 2611 | offset, sh, &inp, netp, dst); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2612 | if (inp_p != NULL) { |
| 2613 | *inp_p = inp; |
| 2614 | } |
| 2615 | } |
| 2616 | } |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2617 | SCTPDBG(SCTP_DEBUG_PCB1, "stcb is %p\n", (void *)stcb); |
| 2618 | return (stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2619 | } |
| 2620 | |
| 2621 | /* |
| 2622 | * lookup an association by an ASCONF lookup address. |
| 2623 | * if the lookup address is 0.0.0.0 or ::0, use the vtag to do the lookup |
| 2624 | */ |
| 2625 | struct sctp_tcb * |
tuexen | 9784e9a | 2011-12-18 13:04:23 +0000 | [diff] [blame] | 2626 | sctp_findassociation_ep_asconf(struct mbuf *m, int offset, |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 2627 | struct sockaddr *dst, struct sctphdr *sh, |
| 2628 | struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2629 | { |
| 2630 | struct sctp_tcb *stcb; |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 2631 | union sctp_sockstore remote_store; |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 2632 | struct sctp_paramhdr param_buf, *phdr; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2633 | int ptype; |
| 2634 | int zero_address = 0; |
| 2635 | #ifdef INET |
| 2636 | struct sockaddr_in *sin; |
| 2637 | #endif |
| 2638 | #ifdef INET6 |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2639 | struct sockaddr_in6 *sin6; |
| 2640 | #endif |
| 2641 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2642 | memset(&remote_store, 0, sizeof(remote_store)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2643 | phdr = sctp_get_next_param(m, offset + sizeof(struct sctp_asconf_chunk), |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 2644 | ¶m_buf, sizeof(struct sctp_paramhdr)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2645 | if (phdr == NULL) { |
| 2646 | SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf lookup addr\n", |
Saúl Ibarra Corretgé | 8b04b41 | 2015-09-26 09:53:06 +0200 | [diff] [blame] | 2647 | __func__); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2648 | return NULL; |
| 2649 | } |
| 2650 | ptype = (int)((uint32_t) ntohs(phdr->param_type)); |
| 2651 | /* get the correlation address */ |
| 2652 | switch (ptype) { |
| 2653 | #ifdef INET6 |
| 2654 | case SCTP_IPV6_ADDRESS: |
| 2655 | { |
| 2656 | /* ipv6 address param */ |
| 2657 | struct sctp_ipv6addr_param *p6, p6_buf; |
| 2658 | |
| 2659 | if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv6addr_param)) { |
| 2660 | return NULL; |
| 2661 | } |
| 2662 | p6 = (struct sctp_ipv6addr_param *)sctp_get_next_param(m, |
| 2663 | offset + sizeof(struct sctp_asconf_chunk), |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 2664 | &p6_buf.ph, sizeof(p6_buf)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2665 | if (p6 == NULL) { |
| 2666 | SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v6 lookup addr\n", |
Saúl Ibarra Corretgé | 8b04b41 | 2015-09-26 09:53:06 +0200 | [diff] [blame] | 2667 | __func__); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2668 | return (NULL); |
| 2669 | } |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 2670 | sin6 = &remote_store.sin6; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2671 | sin6->sin6_family = AF_INET6; |
t00fcxen | 8fbaee3 | 2012-09-04 16:41:39 +0000 | [diff] [blame] | 2672 | #ifdef HAVE_SIN6_LEN |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2673 | sin6->sin6_len = sizeof(*sin6); |
| 2674 | #endif |
| 2675 | sin6->sin6_port = sh->src_port; |
| 2676 | memcpy(&sin6->sin6_addr, &p6->addr, sizeof(struct in6_addr)); |
| 2677 | if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) |
| 2678 | zero_address = 1; |
| 2679 | break; |
| 2680 | } |
| 2681 | #endif |
| 2682 | #ifdef INET |
| 2683 | case SCTP_IPV4_ADDRESS: |
| 2684 | { |
| 2685 | /* ipv4 address param */ |
| 2686 | struct sctp_ipv4addr_param *p4, p4_buf; |
| 2687 | |
| 2688 | if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv4addr_param)) { |
| 2689 | return NULL; |
| 2690 | } |
| 2691 | p4 = (struct sctp_ipv4addr_param *)sctp_get_next_param(m, |
| 2692 | offset + sizeof(struct sctp_asconf_chunk), |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 2693 | &p4_buf.ph, sizeof(p4_buf)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2694 | if (p4 == NULL) { |
| 2695 | SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v4 lookup addr\n", |
Saúl Ibarra Corretgé | 8b04b41 | 2015-09-26 09:53:06 +0200 | [diff] [blame] | 2696 | __func__); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2697 | return (NULL); |
| 2698 | } |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 2699 | sin = &remote_store.sin; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2700 | sin->sin_family = AF_INET; |
t00fcxen | 8fbaee3 | 2012-09-04 16:41:39 +0000 | [diff] [blame] | 2701 | #ifdef HAVE_SIN_LEN |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2702 | sin->sin_len = sizeof(*sin); |
| 2703 | #endif |
| 2704 | sin->sin_port = sh->src_port; |
| 2705 | memcpy(&sin->sin_addr, &p4->addr, sizeof(struct in_addr)); |
| 2706 | if (sin->sin_addr.s_addr == INADDR_ANY) |
| 2707 | zero_address = 1; |
| 2708 | break; |
| 2709 | } |
| 2710 | #endif |
| 2711 | default: |
| 2712 | /* invalid address param type */ |
| 2713 | return NULL; |
| 2714 | } |
| 2715 | |
| 2716 | if (zero_address) { |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 2717 | stcb = sctp_findassoc_by_vtag(NULL, dst, ntohl(sh->v_tag), inp_p, |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2718 | netp, sh->src_port, sh->dest_port, 1, vrf_id, 0); |
tuexen | b53ec51 | 2012-08-05 10:55:12 +0000 | [diff] [blame] | 2719 | if (stcb != NULL) { |
| 2720 | SCTP_INP_DECR_REF(*inp_p); |
| 2721 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2722 | } else { |
| 2723 | stcb = sctp_findassociation_ep_addr(inp_p, |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 2724 | &remote_store.sa, netp, |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 2725 | dst, NULL); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2726 | } |
| 2727 | return (stcb); |
| 2728 | } |
| 2729 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2730 | /* |
| 2731 | * allocate a sctp_inpcb and setup a temporary binding to a port/all |
| 2732 | * addresses. This way if we don't get a bind we by default pick a ephemeral |
| 2733 | * port with all addresses bound. |
| 2734 | */ |
| 2735 | int |
| 2736 | sctp_inpcb_alloc(struct socket *so, uint32_t vrf_id) |
| 2737 | { |
| 2738 | /* |
| 2739 | * we get called when a new endpoint starts up. We need to allocate |
| 2740 | * the sctp_inpcb structure from the zone and init it. Mark it as |
| 2741 | * unbound and find a port that we can use as an ephemeral with |
| 2742 | * INADDR_ANY. If the user binds later no problem we can then add in |
| 2743 | * the specific addresses. And setup the default parameters for the |
| 2744 | * EP. |
| 2745 | */ |
| 2746 | int i, error; |
| 2747 | struct sctp_inpcb *inp; |
| 2748 | struct sctp_pcb *m; |
| 2749 | struct timeval time; |
| 2750 | sctp_sharedkey_t *null_key; |
| 2751 | |
| 2752 | error = 0; |
| 2753 | |
| 2754 | SCTP_INP_INFO_WLOCK(); |
| 2755 | inp = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_ep), struct sctp_inpcb); |
| 2756 | if (inp == NULL) { |
| 2757 | SCTP_PRINTF("Out of SCTP-INPCB structures - no resources\n"); |
| 2758 | SCTP_INP_INFO_WUNLOCK(); |
| 2759 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS); |
| 2760 | return (ENOBUFS); |
| 2761 | } |
| 2762 | /* zap it */ |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 2763 | memset(inp, 0, sizeof(*inp)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2764 | |
| 2765 | /* bump generations */ |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 2766 | #if defined(__APPLE__) && !defined(__Userspace__) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2767 | inp->ip_inp.inp.inp_state = INPCB_STATE_INUSE; |
| 2768 | #endif |
| 2769 | /* setup socket pointers */ |
| 2770 | inp->sctp_socket = so; |
| 2771 | inp->ip_inp.inp.inp_socket = so; |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 2772 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 2773 | inp->ip_inp.inp.inp_cred = crhold(so->so_cred); |
| 2774 | #endif |
tuexen | c0bb612 | 2012-01-15 14:21:21 +0000 | [diff] [blame] | 2775 | #ifdef INET6 |
Michael Tuexen | eb61157 | 2020-06-13 17:27:02 +0200 | [diff] [blame] | 2776 | #if !defined(__Userspace__) && !defined(_WIN32) |
t00fcxen | afd2119 | 2013-05-10 18:15:02 +0000 | [diff] [blame] | 2777 | if (INP_SOCKAF(so) == AF_INET6) { |
| 2778 | if (MODULE_GLOBAL(ip6_auto_flowlabel)) { |
| 2779 | inp->ip_inp.inp.inp_flags |= IN6P_AUTOFLOWLABEL; |
| 2780 | } |
| 2781 | if (MODULE_GLOBAL(ip6_v6only)) { |
| 2782 | inp->ip_inp.inp.inp_flags |= IN6P_IPV6_V6ONLY; |
| 2783 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2784 | } |
| 2785 | #endif |
tuexen | c0bb612 | 2012-01-15 14:21:21 +0000 | [diff] [blame] | 2786 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2787 | inp->sctp_associd_counter = 1; |
| 2788 | inp->partial_delivery_point = SCTP_SB_LIMIT_RCV(so) >> SCTP_PARTIAL_DELIVERY_SHIFT; |
Michael Tuexen | dbb9f55 | 2021-12-28 00:48:48 +0100 | [diff] [blame] | 2789 | inp->sctp_frag_point = 0; |
t00fcxen | 3eda0f2 | 2015-03-10 20:10:45 +0000 | [diff] [blame] | 2790 | inp->max_cwnd = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2791 | inp->sctp_cmt_on_off = SCTP_BASE_SYSCTL(sctp_cmt_on_off); |
t00fcxen | 478b772 | 2014-08-02 18:24:12 +0000 | [diff] [blame] | 2792 | inp->ecn_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_ecn_enable); |
t00fcxen | 0e78cef | 2014-08-02 22:05:33 +0000 | [diff] [blame] | 2793 | inp->prsctp_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_pr_enable); |
t00fcxen | b3969a1 | 2014-08-12 13:19:21 +0000 | [diff] [blame] | 2794 | inp->auth_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_auth_enable); |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 2795 | inp->asconf_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_asconf_enable); |
t00fcxen | fa7e1a0 | 2014-08-04 20:41:36 +0000 | [diff] [blame] | 2796 | inp->reconfig_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_reconfig_enable); |
t00fcxen | 3656cf9 | 2014-08-03 15:25:02 +0000 | [diff] [blame] | 2797 | inp->nrsack_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_nrsack_enable); |
t00fcxen | 1cc0ba8 | 2014-08-03 20:32:08 +0000 | [diff] [blame] | 2798 | inp->pktdrop_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_pktdrop_enable); |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 2799 | inp->idata_supported = 0; |
| 2800 | |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 2801 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
t00fcxen | a51e446 | 2015-06-17 15:53:23 +0000 | [diff] [blame] | 2802 | inp->fibnum = so->so_fibnum; |
| 2803 | #else |
| 2804 | inp->fibnum = 0; |
| 2805 | #endif |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 2806 | #if defined(__Userspace__) |
tuexen | eda9dbb | 2012-05-27 11:10:08 +0000 | [diff] [blame] | 2807 | inp->ulp_info = NULL; |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 2808 | inp->recv_callback = NULL; |
| 2809 | inp->send_callback = NULL; |
| 2810 | inp->send_sb_threshold = 0; |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 2811 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2812 | /* init the small hash table we use to track asocid <-> tcb */ |
| 2813 | inp->sctp_asocidhash = SCTP_HASH_INIT(SCTP_STACK_VTAG_HASH_SIZE, &inp->hashasocidmark); |
| 2814 | if (inp->sctp_asocidhash == NULL) { |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 2815 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 2816 | crfree(inp->ip_inp.inp.inp_cred); |
| 2817 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2818 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp); |
| 2819 | SCTP_INP_INFO_WUNLOCK(); |
| 2820 | return (ENOBUFS); |
| 2821 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2822 | SCTP_INCR_EP_COUNT(); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2823 | inp->ip_inp.inp.inp_ip_ttl = MODULE_GLOBAL(ip_defttl); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2824 | SCTP_INP_INFO_WUNLOCK(); |
| 2825 | |
| 2826 | so->so_pcb = (caddr_t)inp; |
| 2827 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2828 | if (SCTP_SO_TYPE(so) == SOCK_SEQPACKET) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2829 | /* UDP style socket */ |
| 2830 | inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE | |
| 2831 | SCTP_PCB_FLAGS_UNBOUND); |
| 2832 | /* Be sure it is NON-BLOCKING IO for UDP */ |
| 2833 | /* SCTP_SET_SO_NBIO(so); */ |
| 2834 | } else if (SCTP_SO_TYPE(so) == SOCK_STREAM) { |
| 2835 | /* TCP style socket */ |
| 2836 | inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE | |
| 2837 | SCTP_PCB_FLAGS_UNBOUND); |
| 2838 | /* Be sure we have blocking IO by default */ |
Michael Tuexen | fe22357 | 2017-09-22 00:19:22 +0200 | [diff] [blame] | 2839 | SOCK_LOCK(so); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2840 | SCTP_CLEAR_SO_NBIO(so); |
Michael Tuexen | fe22357 | 2017-09-22 00:19:22 +0200 | [diff] [blame] | 2841 | SOCK_UNLOCK(so); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2842 | } else { |
| 2843 | /* |
| 2844 | * unsupported socket type (RAW, etc)- in case we missed it |
| 2845 | * in protosw |
| 2846 | */ |
| 2847 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EOPNOTSUPP); |
| 2848 | so->so_pcb = NULL; |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 2849 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 2850 | crfree(inp->ip_inp.inp.inp_cred); |
| 2851 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2852 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp); |
| 2853 | return (EOPNOTSUPP); |
| 2854 | } |
| 2855 | if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_1) { |
| 2856 | sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE); |
| 2857 | sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS); |
| 2858 | } else if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_2) { |
| 2859 | sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE); |
| 2860 | sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS); |
| 2861 | } else if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_0) { |
| 2862 | sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE); |
| 2863 | sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS); |
| 2864 | } |
| 2865 | inp->sctp_tcbhash = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_pcbtblsize), |
| 2866 | &inp->sctp_hashmark); |
| 2867 | if (inp->sctp_tcbhash == NULL) { |
| 2868 | SCTP_PRINTF("Out of SCTP-INPCB->hashinit - no resources\n"); |
| 2869 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS); |
| 2870 | so->so_pcb = NULL; |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 2871 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 2872 | crfree(inp->ip_inp.inp.inp_cred); |
| 2873 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2874 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp); |
| 2875 | return (ENOBUFS); |
| 2876 | } |
| 2877 | #ifdef SCTP_MVRF |
| 2878 | inp->vrf_size = SCTP_DEFAULT_VRF_SIZE; |
| 2879 | SCTP_MALLOC(inp->m_vrf_ids, uint32_t *, |
| 2880 | (sizeof(uint32_t) * inp->vrf_size), SCTP_M_MVRF); |
| 2881 | if (inp->m_vrf_ids == NULL) { |
| 2882 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS); |
| 2883 | so->so_pcb = NULL; |
| 2884 | SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark); |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 2885 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 2886 | crfree(inp->ip_inp.inp.inp_cred); |
| 2887 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2888 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp); |
| 2889 | return (ENOBUFS); |
| 2890 | } |
| 2891 | inp->m_vrf_ids[0] = vrf_id; |
| 2892 | inp->num_vrfs = 1; |
| 2893 | #endif |
| 2894 | inp->def_vrf_id = vrf_id; |
| 2895 | |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 2896 | #if defined(__APPLE__) && !defined(__Userspace__) |
tuexen | 9a218b1 | 2012-08-04 21:17:58 +0000 | [diff] [blame] | 2897 | #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) |
t00fcxen | bc766ab | 2014-01-03 19:29:31 +0000 | [diff] [blame] | 2898 | inp->ip_inp.inp.inpcb_mtx = lck_mtx_alloc_init(SCTP_BASE_INFO(sctbinfo).mtx_grp, SCTP_BASE_INFO(sctbinfo).mtx_attr); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2899 | if (inp->ip_inp.inp.inpcb_mtx == NULL) { |
t00fcxen | 8fca7ff | 2012-09-05 18:58:42 +0000 | [diff] [blame] | 2900 | SCTP_PRINTF("in_pcballoc: can't alloc mutex! so=%p\n", (void *)so); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2901 | #ifdef SCTP_MVRF |
| 2902 | SCTP_FREE(inp->m_vrf_ids, SCTP_M_MVRF); |
| 2903 | #endif |
| 2904 | SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark); |
| 2905 | so->so_pcb = NULL; |
| 2906 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp); |
t00fcxen | bc766ab | 2014-01-03 19:29:31 +0000 | [diff] [blame] | 2907 | SCTP_UNLOCK_EXC(SCTP_BASE_INFO(sctbinfo).ipi_lock); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2908 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM); |
| 2909 | return (ENOMEM); |
| 2910 | } |
t00fcxen | bc766ab | 2014-01-03 19:29:31 +0000 | [diff] [blame] | 2911 | #elif defined(APPLE_LION) || defined(APPLE_MOUNTAINLION) |
| 2912 | lck_mtx_init(&inp->ip_inp.inp.inpcb_mtx, SCTP_BASE_INFO(sctbinfo).mtx_grp, SCTP_BASE_INFO(sctbinfo).mtx_attr); |
tuexen | 9a218b1 | 2012-08-04 21:17:58 +0000 | [diff] [blame] | 2913 | #else |
t00fcxen | bc766ab | 2014-01-03 19:29:31 +0000 | [diff] [blame] | 2914 | lck_mtx_init(&inp->ip_inp.inp.inpcb_mtx, SCTP_BASE_INFO(sctbinfo).ipi_lock_grp, SCTP_BASE_INFO(sctbinfo).ipi_lock_attr); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2915 | #endif |
| 2916 | #endif |
| 2917 | SCTP_INP_INFO_WLOCK(); |
| 2918 | SCTP_INP_LOCK_INIT(inp); |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 2919 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
Michael Tuexen | 24343ce | 2022-01-03 23:10:33 +0100 | [diff] [blame] | 2920 | rw_init_flags(&inp->ip_inp.inp.inp_lock, "sctpinp", |
| 2921 | RW_RECURSE | RW_DUPOK); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2922 | #endif |
| 2923 | SCTP_INP_READ_INIT(inp); |
| 2924 | SCTP_ASOC_CREATE_LOCK_INIT(inp); |
| 2925 | /* lock the new ep */ |
| 2926 | SCTP_INP_WLOCK(inp); |
| 2927 | |
| 2928 | /* add it to the info area */ |
| 2929 | LIST_INSERT_HEAD(&SCTP_BASE_INFO(listhead), inp, sctp_list); |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 2930 | #if defined(__APPLE__) && !defined(__Userspace__) |
t00fcxen | bc766ab | 2014-01-03 19:29:31 +0000 | [diff] [blame] | 2931 | inp->ip_inp.inp.inp_pcbinfo = &SCTP_BASE_INFO(sctbinfo); |
| 2932 | #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) || defined(APPLE_LION) || defined(APPLE_MOUNTAINLION) |
| 2933 | LIST_INSERT_HEAD(SCTP_BASE_INFO(sctbinfo).listhead, &inp->ip_inp.inp, inp_list); |
| 2934 | #else |
| 2935 | LIST_INSERT_HEAD(SCTP_BASE_INFO(sctbinfo).ipi_listhead, &inp->ip_inp.inp, inp_list); |
| 2936 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2937 | #endif |
| 2938 | SCTP_INP_INFO_WUNLOCK(); |
| 2939 | |
| 2940 | TAILQ_INIT(&inp->read_queue); |
| 2941 | LIST_INIT(&inp->sctp_addr_list); |
| 2942 | |
| 2943 | LIST_INIT(&inp->sctp_asoc_list); |
| 2944 | |
| 2945 | #ifdef SCTP_TRACK_FREED_ASOCS |
| 2946 | /* TEMP CODE */ |
| 2947 | LIST_INIT(&inp->sctp_asoc_free_list); |
| 2948 | #endif |
| 2949 | /* Init the timer structure for signature change */ |
| 2950 | SCTP_OS_TIMER_INIT(&inp->sctp_ep.signature_change.timer); |
| 2951 | inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NEWCOOKIE; |
| 2952 | |
| 2953 | /* now init the actual endpoint default data */ |
| 2954 | m = &inp->sctp_ep; |
| 2955 | |
| 2956 | /* setup the base timeout information */ |
Michael Tuexen | 25935e8 | 2020-03-28 22:55:28 +0100 | [diff] [blame] | 2957 | m->sctp_timeoutticks[SCTP_TIMER_SEND] = sctp_secs_to_ticks(SCTP_SEND_SEC); /* needed ? */ |
| 2958 | m->sctp_timeoutticks[SCTP_TIMER_INIT] = sctp_secs_to_ticks(SCTP_INIT_SEC); /* needed ? */ |
| 2959 | m->sctp_timeoutticks[SCTP_TIMER_RECV] = sctp_msecs_to_ticks(SCTP_BASE_SYSCTL(sctp_delayed_sack_time_default)); |
| 2960 | m->sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = sctp_msecs_to_ticks(SCTP_BASE_SYSCTL(sctp_heartbeat_interval_default)); |
| 2961 | m->sctp_timeoutticks[SCTP_TIMER_PMTU] = sctp_secs_to_ticks(SCTP_BASE_SYSCTL(sctp_pmtu_raise_time_default)); |
| 2962 | m->sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] = sctp_secs_to_ticks(SCTP_BASE_SYSCTL(sctp_shutdown_guard_time_default)); |
| 2963 | m->sctp_timeoutticks[SCTP_TIMER_SIGNATURE] = sctp_secs_to_ticks(SCTP_BASE_SYSCTL(sctp_secret_lifetime_default)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2964 | /* all max/min max are in ms */ |
| 2965 | m->sctp_maxrto = SCTP_BASE_SYSCTL(sctp_rto_max_default); |
| 2966 | m->sctp_minrto = SCTP_BASE_SYSCTL(sctp_rto_min_default); |
| 2967 | m->initial_rto = SCTP_BASE_SYSCTL(sctp_rto_initial_default); |
| 2968 | m->initial_init_rto_max = SCTP_BASE_SYSCTL(sctp_init_rto_max_default); |
| 2969 | m->sctp_sack_freq = SCTP_BASE_SYSCTL(sctp_sack_freq_default); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2970 | m->max_init_times = SCTP_BASE_SYSCTL(sctp_init_rtx_max_default); |
| 2971 | m->max_send_times = SCTP_BASE_SYSCTL(sctp_assoc_rtx_max_default); |
| 2972 | m->def_net_failure = SCTP_BASE_SYSCTL(sctp_path_rtx_max_default); |
| 2973 | m->def_net_pf_threshold = SCTP_BASE_SYSCTL(sctp_path_pf_threshold); |
| 2974 | m->sctp_sws_sender = SCTP_SWS_SENDER_DEF; |
| 2975 | m->sctp_sws_receiver = SCTP_SWS_RECEIVER_DEF; |
| 2976 | m->max_burst = SCTP_BASE_SYSCTL(sctp_max_burst_default); |
| 2977 | m->fr_max_burst = SCTP_BASE_SYSCTL(sctp_fr_max_burst_default); |
| 2978 | |
| 2979 | m->sctp_default_cc_module = SCTP_BASE_SYSCTL(sctp_default_cc_module); |
| 2980 | m->sctp_default_ss_module = SCTP_BASE_SYSCTL(sctp_default_ss_module); |
t00fcxen | c45180a | 2013-07-05 10:17:17 +0000 | [diff] [blame] | 2981 | m->max_open_streams_intome = SCTP_BASE_SYSCTL(sctp_nr_incoming_streams_default); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2982 | /* number of streams to pre-open on a association */ |
| 2983 | m->pre_open_stream_count = SCTP_BASE_SYSCTL(sctp_nr_outgoing_streams_default); |
| 2984 | |
Michael Tuexen | de0630d | 2017-11-03 22:04:01 +0100 | [diff] [blame] | 2985 | m->default_mtu = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2986 | /* Add adaptation cookie */ |
t00fcxen | 3637720 | 2013-02-11 21:07:33 +0000 | [diff] [blame] | 2987 | m->adaptation_layer_indicator = 0; |
| 2988 | m->adaptation_layer_indicator_provided = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2989 | |
| 2990 | /* seed random number generator */ |
| 2991 | m->random_counter = 1; |
| 2992 | m->store_at = SCTP_SIGNATURE_SIZE; |
| 2993 | SCTP_READ_RANDOM(m->random_numbers, sizeof(m->random_numbers)); |
| 2994 | sctp_fill_random_store(m); |
| 2995 | |
| 2996 | /* Minimum cookie size */ |
| 2997 | m->size_of_a_cookie = (sizeof(struct sctp_init_msg) * 2) + |
| 2998 | sizeof(struct sctp_state_cookie); |
| 2999 | m->size_of_a_cookie += SCTP_SIGNATURE_SIZE; |
| 3000 | |
| 3001 | /* Setup the initial secret */ |
| 3002 | (void)SCTP_GETTIME_TIMEVAL(&time); |
Michael Tuexen | f2c2b60 | 2021-09-07 15:50:13 +0200 | [diff] [blame] | 3003 | m->time_of_secret_change = (unsigned int)time.tv_sec; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3004 | |
| 3005 | for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) { |
| 3006 | m->secret_key[0][i] = sctp_select_initial_TSN(m); |
| 3007 | } |
| 3008 | sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL); |
| 3009 | |
| 3010 | /* How long is a cookie good for ? */ |
Michael Tuexen | 25935e8 | 2020-03-28 22:55:28 +0100 | [diff] [blame] | 3011 | m->def_cookie_life = sctp_msecs_to_ticks(SCTP_BASE_SYSCTL(sctp_valid_cookie_life_default)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3012 | /* |
| 3013 | * Initialize authentication parameters |
| 3014 | */ |
| 3015 | m->local_hmacs = sctp_default_supported_hmaclist(); |
| 3016 | m->local_auth_chunks = sctp_alloc_chunklist(); |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 3017 | if (inp->asconf_supported) { |
| 3018 | sctp_auth_add_chunk(SCTP_ASCONF, m->local_auth_chunks); |
| 3019 | sctp_auth_add_chunk(SCTP_ASCONF_ACK, m->local_auth_chunks); |
| 3020 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3021 | m->default_dscp = 0; |
| 3022 | #ifdef INET6 |
| 3023 | m->default_flowlabel = 0; |
| 3024 | #endif |
tuexen | 9d7b207 | 2011-11-20 16:35:17 +0000 | [diff] [blame] | 3025 | m->port = 0; /* encapsulation disabled by default */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3026 | LIST_INIT(&m->shared_keys); |
| 3027 | /* add default NULL key as key id 0 */ |
| 3028 | null_key = sctp_alloc_sharedkey(); |
| 3029 | sctp_insert_sharedkey(&m->shared_keys, null_key); |
| 3030 | SCTP_INP_WUNLOCK(inp); |
| 3031 | #ifdef SCTP_LOG_CLOSING |
| 3032 | sctp_log_closing(inp, NULL, 12); |
| 3033 | #endif |
| 3034 | return (error); |
| 3035 | } |
| 3036 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3037 | void |
| 3038 | sctp_move_pcb_and_assoc(struct sctp_inpcb *old_inp, struct sctp_inpcb *new_inp, |
| 3039 | struct sctp_tcb *stcb) |
| 3040 | { |
| 3041 | struct sctp_nets *net; |
| 3042 | uint16_t lport, rport; |
| 3043 | struct sctppcbhead *head; |
| 3044 | struct sctp_laddr *laddr, *oladdr; |
| 3045 | |
| 3046 | atomic_add_int(&stcb->asoc.refcnt, 1); |
| 3047 | SCTP_TCB_UNLOCK(stcb); |
| 3048 | SCTP_INP_INFO_WLOCK(); |
| 3049 | SCTP_INP_WLOCK(old_inp); |
| 3050 | SCTP_INP_WLOCK(new_inp); |
| 3051 | SCTP_TCB_LOCK(stcb); |
| 3052 | atomic_subtract_int(&stcb->asoc.refcnt, 1); |
| 3053 | |
Michael Tuexen | 2dfe194 | 2021-12-04 11:39:46 +0100 | [diff] [blame] | 3054 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
Michael Tuexen | c4e5de5 | 2021-12-04 19:22:59 +0100 | [diff] [blame] | 3055 | #ifdef INET6 |
Michael Tuexen | 2dfe194 | 2021-12-04 11:39:46 +0100 | [diff] [blame] | 3056 | if (old_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { |
| 3057 | new_inp->ip_inp.inp.inp_flags |= old_inp->ip_inp.inp.inp_flags & INP_CONTROLOPTS; |
| 3058 | if (old_inp->ip_inp.inp.in6p_outputopts) { |
| 3059 | new_inp->ip_inp.inp.in6p_outputopts = ip6_copypktopts(old_inp->ip_inp.inp.in6p_outputopts, M_NOWAIT); |
| 3060 | } |
Michael Tuexen | c4e5de5 | 2021-12-04 19:22:59 +0100 | [diff] [blame] | 3061 | } |
| 3062 | #endif |
| 3063 | #if defined(INET) && defined(INET6) |
| 3064 | else |
| 3065 | #endif |
| 3066 | #ifdef INET |
| 3067 | { |
Michael Tuexen | 2dfe194 | 2021-12-04 11:39:46 +0100 | [diff] [blame] | 3068 | new_inp->ip_inp.inp.inp_ip_tos = old_inp->ip_inp.inp.inp_ip_tos; |
| 3069 | new_inp->ip_inp.inp.inp_ip_ttl = old_inp->ip_inp.inp.inp_ip_ttl; |
| 3070 | } |
| 3071 | #endif |
Michael Tuexen | c4e5de5 | 2021-12-04 19:22:59 +0100 | [diff] [blame] | 3072 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3073 | new_inp->sctp_ep.time_of_secret_change = |
| 3074 | old_inp->sctp_ep.time_of_secret_change; |
| 3075 | memcpy(new_inp->sctp_ep.secret_key, old_inp->sctp_ep.secret_key, |
| 3076 | sizeof(old_inp->sctp_ep.secret_key)); |
| 3077 | new_inp->sctp_ep.current_secret_number = |
| 3078 | old_inp->sctp_ep.current_secret_number; |
| 3079 | new_inp->sctp_ep.last_secret_number = |
| 3080 | old_inp->sctp_ep.last_secret_number; |
| 3081 | new_inp->sctp_ep.size_of_a_cookie = old_inp->sctp_ep.size_of_a_cookie; |
| 3082 | |
| 3083 | /* make it so new data pours into the new socket */ |
| 3084 | stcb->sctp_socket = new_inp->sctp_socket; |
| 3085 | stcb->sctp_ep = new_inp; |
| 3086 | |
| 3087 | /* Copy the port across */ |
| 3088 | lport = new_inp->sctp_lport = old_inp->sctp_lport; |
| 3089 | rport = stcb->rport; |
| 3090 | /* Pull the tcb from the old association */ |
| 3091 | LIST_REMOVE(stcb, sctp_tcbhash); |
| 3092 | LIST_REMOVE(stcb, sctp_tcblist); |
| 3093 | if (stcb->asoc.in_asocid_hash) { |
| 3094 | LIST_REMOVE(stcb, sctp_tcbasocidhash); |
| 3095 | } |
| 3096 | /* Now insert the new_inp into the TCP connected hash */ |
| 3097 | head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR((lport | rport), SCTP_BASE_INFO(hashtcpmark))]; |
| 3098 | |
| 3099 | LIST_INSERT_HEAD(head, new_inp, sctp_hash); |
| 3100 | /* Its safe to access */ |
| 3101 | new_inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND; |
| 3102 | |
| 3103 | /* Now move the tcb into the endpoint list */ |
| 3104 | LIST_INSERT_HEAD(&new_inp->sctp_asoc_list, stcb, sctp_tcblist); |
| 3105 | /* |
| 3106 | * Question, do we even need to worry about the ep-hash since we |
| 3107 | * only have one connection? Probably not :> so lets get rid of it |
| 3108 | * and not suck up any kernel memory in that. |
| 3109 | */ |
| 3110 | if (stcb->asoc.in_asocid_hash) { |
| 3111 | struct sctpasochead *lhd; |
| 3112 | lhd = &new_inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(stcb->asoc.assoc_id, |
| 3113 | new_inp->hashasocidmark)]; |
| 3114 | LIST_INSERT_HEAD(lhd, stcb, sctp_tcbasocidhash); |
| 3115 | } |
| 3116 | /* Ok. Let's restart timer. */ |
| 3117 | TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { |
| 3118 | sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, new_inp, |
| 3119 | stcb, net); |
| 3120 | } |
| 3121 | |
| 3122 | SCTP_INP_INFO_WUNLOCK(); |
| 3123 | if (new_inp->sctp_tcbhash != NULL) { |
| 3124 | SCTP_HASH_FREE(new_inp->sctp_tcbhash, new_inp->sctp_hashmark); |
| 3125 | new_inp->sctp_tcbhash = NULL; |
| 3126 | } |
| 3127 | if ((new_inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) { |
| 3128 | /* Subset bound, so copy in the laddr list from the old_inp */ |
| 3129 | LIST_FOREACH(oladdr, &old_inp->sctp_addr_list, sctp_nxt_addr) { |
| 3130 | laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr); |
| 3131 | if (laddr == NULL) { |
| 3132 | /* |
| 3133 | * Gak, what can we do? This assoc is really |
| 3134 | * HOSED. We probably should send an abort |
| 3135 | * here. |
| 3136 | */ |
| 3137 | SCTPDBG(SCTP_DEBUG_PCB1, "Association hosed in TCP model, out of laddr memory\n"); |
| 3138 | continue; |
| 3139 | } |
| 3140 | SCTP_INCR_LADDR_COUNT(); |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 3141 | memset(laddr, 0, sizeof(*laddr)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3142 | (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time); |
| 3143 | laddr->ifa = oladdr->ifa; |
| 3144 | atomic_add_int(&laddr->ifa->refcount, 1); |
| 3145 | LIST_INSERT_HEAD(&new_inp->sctp_addr_list, laddr, |
| 3146 | sctp_nxt_addr); |
| 3147 | new_inp->laddr_count++; |
| 3148 | if (oladdr == stcb->asoc.last_used_address) { |
| 3149 | stcb->asoc.last_used_address = laddr; |
| 3150 | } |
| 3151 | } |
| 3152 | } |
Michael Tuexen | 939d48f | 2020-07-19 14:44:43 +0200 | [diff] [blame] | 3153 | /* Now any running timers need to be adjusted. */ |
| 3154 | if (stcb->asoc.dack_timer.ep == old_inp) { |
| 3155 | SCTP_INP_DECR_REF(old_inp); |
| 3156 | stcb->asoc.dack_timer.ep = new_inp; |
| 3157 | SCTP_INP_INCR_REF(new_inp); |
| 3158 | } |
| 3159 | if (stcb->asoc.asconf_timer.ep == old_inp) { |
| 3160 | SCTP_INP_DECR_REF(old_inp); |
| 3161 | stcb->asoc.asconf_timer.ep = new_inp; |
| 3162 | SCTP_INP_INCR_REF(new_inp); |
| 3163 | } |
| 3164 | if (stcb->asoc.strreset_timer.ep == old_inp) { |
| 3165 | SCTP_INP_DECR_REF(old_inp); |
| 3166 | stcb->asoc.strreset_timer.ep = new_inp; |
| 3167 | SCTP_INP_INCR_REF(new_inp); |
| 3168 | } |
| 3169 | if (stcb->asoc.shut_guard_timer.ep == old_inp) { |
| 3170 | SCTP_INP_DECR_REF(old_inp); |
| 3171 | stcb->asoc.shut_guard_timer.ep = new_inp; |
| 3172 | SCTP_INP_INCR_REF(new_inp); |
| 3173 | } |
| 3174 | if (stcb->asoc.autoclose_timer.ep == old_inp) { |
| 3175 | SCTP_INP_DECR_REF(old_inp); |
| 3176 | stcb->asoc.autoclose_timer.ep = new_inp; |
| 3177 | SCTP_INP_INCR_REF(new_inp); |
| 3178 | } |
| 3179 | if (stcb->asoc.delete_prim_timer.ep == old_inp) { |
| 3180 | SCTP_INP_DECR_REF(old_inp); |
| 3181 | stcb->asoc.delete_prim_timer.ep = new_inp; |
| 3182 | SCTP_INP_INCR_REF(new_inp); |
| 3183 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3184 | /* now what about the nets? */ |
| 3185 | TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { |
Michael Tuexen | 939d48f | 2020-07-19 14:44:43 +0200 | [diff] [blame] | 3186 | if (net->pmtu_timer.ep == old_inp) { |
| 3187 | SCTP_INP_DECR_REF(old_inp); |
| 3188 | net->pmtu_timer.ep = new_inp; |
| 3189 | SCTP_INP_INCR_REF(new_inp); |
| 3190 | } |
| 3191 | if (net->hb_timer.ep == old_inp) { |
| 3192 | SCTP_INP_DECR_REF(old_inp); |
| 3193 | net->hb_timer.ep = new_inp; |
| 3194 | SCTP_INP_INCR_REF(new_inp); |
| 3195 | } |
| 3196 | if (net->rxt_timer.ep == old_inp) { |
| 3197 | SCTP_INP_DECR_REF(old_inp); |
| 3198 | net->rxt_timer.ep = new_inp; |
| 3199 | SCTP_INP_INCR_REF(new_inp); |
| 3200 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3201 | } |
| 3202 | SCTP_INP_WUNLOCK(new_inp); |
| 3203 | SCTP_INP_WUNLOCK(old_inp); |
| 3204 | } |
| 3205 | |
Michael Tuexen | 71a83fb | 2016-02-16 21:52:58 +0100 | [diff] [blame] | 3206 | /* |
| 3207 | * insert an laddr entry with the given ifa for the desired list |
| 3208 | */ |
| 3209 | static int |
| 3210 | sctp_insert_laddr(struct sctpladdr *list, struct sctp_ifa *ifa, uint32_t act) |
| 3211 | { |
| 3212 | struct sctp_laddr *laddr; |
| 3213 | |
| 3214 | laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr); |
| 3215 | if (laddr == NULL) { |
| 3216 | /* out of memory? */ |
| 3217 | SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 3218 | return (EINVAL); |
| 3219 | } |
| 3220 | SCTP_INCR_LADDR_COUNT(); |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 3221 | memset(laddr, 0, sizeof(*laddr)); |
Michael Tuexen | 71a83fb | 2016-02-16 21:52:58 +0100 | [diff] [blame] | 3222 | (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time); |
| 3223 | laddr->ifa = ifa; |
| 3224 | laddr->action = act; |
| 3225 | atomic_add_int(&ifa->refcount, 1); |
| 3226 | /* insert it */ |
| 3227 | LIST_INSERT_HEAD(list, laddr, sctp_nxt_addr); |
| 3228 | |
| 3229 | return (0); |
| 3230 | } |
| 3231 | |
| 3232 | /* |
| 3233 | * Remove an laddr entry from the local address list (on an assoc) |
| 3234 | */ |
| 3235 | static void |
| 3236 | sctp_remove_laddr(struct sctp_laddr *laddr) |
| 3237 | { |
| 3238 | |
| 3239 | /* remove from the list */ |
| 3240 | LIST_REMOVE(laddr, sctp_nxt_addr); |
| 3241 | sctp_free_ifa(laddr->ifa); |
| 3242 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), laddr); |
| 3243 | SCTP_DECR_LADDR_COUNT(); |
| 3244 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3245 | #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Userspace__)) |
Michael Tuexen | f7f9cd8 | 2020-09-24 15:14:13 +0200 | [diff] [blame] | 3246 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3247 | /* |
| 3248 | * Don't know why, but without this there is an unknown reference when |
| 3249 | * compiling NetBSD... hmm |
| 3250 | */ |
| 3251 | extern void in6_sin6_2_sin(struct sockaddr_in *, struct sockaddr_in6 *sin6); |
| 3252 | #endif |
| 3253 | |
Michael Tuexen | a8409b3 | 2021-09-02 01:50:45 +0200 | [diff] [blame] | 3254 | /* |
| 3255 | * Bind the socket, with the PCB and global info locks held. Note, if a |
| 3256 | * socket address is specified, the PCB lock may be dropped and re-acquired. |
| 3257 | * |
| 3258 | * sctp_ifap is used to bypass normal local address validation checks. |
| 3259 | */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3260 | int |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 3261 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
Michael Tuexen | a8409b3 | 2021-09-02 01:50:45 +0200 | [diff] [blame] | 3262 | sctp_inpcb_bind_locked(struct sctp_inpcb *inp, struct sockaddr *addr, |
| 3263 | struct sctp_ifa *sctp_ifap, struct thread *td) |
Michael Tuexen | eb61157 | 2020-06-13 17:27:02 +0200 | [diff] [blame] | 3264 | #elif defined(_WIN32) && !defined(__Userspace__) |
Michael Tuexen | a8409b3 | 2021-09-02 01:50:45 +0200 | [diff] [blame] | 3265 | sctp_inpcb_bind_locked(struct sctp_inpcb *inp, struct sockaddr *addr, |
| 3266 | struct sctp_ifa *sctp_ifap, PKTHREAD p) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3267 | #else |
Michael Tuexen | a8409b3 | 2021-09-02 01:50:45 +0200 | [diff] [blame] | 3268 | sctp_inpcb_bind_locked(struct sctp_inpcb *inp, struct sockaddr *addr, |
| 3269 | struct sctp_ifa *sctp_ifap, struct proc *p) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3270 | #endif |
| 3271 | { |
| 3272 | /* bind a ep to a socket address */ |
| 3273 | struct sctppcbhead *head; |
Michael Tuexen | a8409b3 | 2021-09-02 01:50:45 +0200 | [diff] [blame] | 3274 | struct sctp_inpcb *inp_tmp; |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 3275 | #if (defined(__FreeBSD__) || defined(__APPLE__)) && !defined(__Userspace__) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3276 | struct inpcb *ip_inp; |
t00fcxen | aff14b6 | 2013-11-02 18:02:02 +0000 | [diff] [blame] | 3277 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3278 | int port_reuse_active = 0; |
| 3279 | int bindall; |
| 3280 | #ifdef SCTP_MVRF |
| 3281 | int i; |
| 3282 | #endif |
| 3283 | uint16_t lport; |
| 3284 | int error; |
| 3285 | uint32_t vrf_id; |
| 3286 | |
Michael Tuexen | ff68175 | 2021-09-01 13:00:05 +0200 | [diff] [blame] | 3287 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 3288 | KASSERT(td != NULL, ("%s: null thread", __func__)); |
| 3289 | |
| 3290 | #endif |
| 3291 | error = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3292 | lport = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3293 | bindall = 1; |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 3294 | #if (defined(__FreeBSD__) || defined(__APPLE__)) && !defined(__Userspace__) |
Michael Tuexen | a8409b3 | 2021-09-02 01:50:45 +0200 | [diff] [blame] | 3295 | ip_inp = &inp->ip_inp.inp; |
t00fcxen | aff14b6 | 2013-11-02 18:02:02 +0000 | [diff] [blame] | 3296 | #endif |
Michael Tuexen | a8409b3 | 2021-09-02 01:50:45 +0200 | [diff] [blame] | 3297 | |
| 3298 | SCTP_INP_INFO_WLOCK_ASSERT(); |
| 3299 | SCTP_INP_WLOCK_ASSERT(inp); |
| 3300 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3301 | #ifdef SCTP_DEBUG |
| 3302 | if (addr) { |
t00fcxen | 94dfd1e | 2012-12-27 08:14:10 +0000 | [diff] [blame] | 3303 | SCTPDBG(SCTP_DEBUG_PCB1, "Bind called port: %d\n", |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3304 | ntohs(((struct sockaddr_in *)addr)->sin_port)); |
t00fcxen | 94dfd1e | 2012-12-27 08:14:10 +0000 | [diff] [blame] | 3305 | SCTPDBG(SCTP_DEBUG_PCB1, "Addr: "); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3306 | SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr); |
| 3307 | } |
| 3308 | #endif |
| 3309 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) { |
Michael Tuexen | ff68175 | 2021-09-01 13:00:05 +0200 | [diff] [blame] | 3310 | error = EINVAL; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3311 | /* already did a bind, subsequent binds NOT allowed ! */ |
Michael Tuexen | ff68175 | 2021-09-01 13:00:05 +0200 | [diff] [blame] | 3312 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); |
| 3313 | goto out; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3314 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3315 | if (addr != NULL) { |
| 3316 | switch (addr->sa_family) { |
| 3317 | #ifdef INET |
| 3318 | case AF_INET: |
| 3319 | { |
| 3320 | struct sockaddr_in *sin; |
| 3321 | |
| 3322 | /* IPV6_V6ONLY socket? */ |
Michael Tuexen | 72e5cec | 2019-08-05 13:21:02 +0200 | [diff] [blame] | 3323 | if (SCTP_IPV6_V6ONLY(inp)) { |
Michael Tuexen | ff68175 | 2021-09-01 13:00:05 +0200 | [diff] [blame] | 3324 | error = EINVAL; |
| 3325 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); |
| 3326 | goto out; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3327 | } |
t00fcxen | a8a26f6 | 2012-09-12 19:24:54 +0000 | [diff] [blame] | 3328 | #ifdef HAVE_SA_LEN |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3329 | if (addr->sa_len != sizeof(*sin)) { |
Michael Tuexen | ff68175 | 2021-09-01 13:00:05 +0200 | [diff] [blame] | 3330 | error = EINVAL; |
| 3331 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); |
| 3332 | goto out; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3333 | } |
| 3334 | #endif |
| 3335 | |
| 3336 | sin = (struct sockaddr_in *)addr; |
| 3337 | lport = sin->sin_port; |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 3338 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
t00fcxen | c24796c | 2014-05-06 15:39:13 +0000 | [diff] [blame] | 3339 | /* |
| 3340 | * For LOOPBACK the prison_local_ip4() call will transmute the ip address |
| 3341 | * to the proper value. |
| 3342 | */ |
Michael Tuexen | ff68175 | 2021-09-01 13:00:05 +0200 | [diff] [blame] | 3343 | if ((error = prison_local_ip4(td->td_ucred, &sin->sin_addr)) != 0) { |
t00fcxen | c24796c | 2014-05-06 15:39:13 +0000 | [diff] [blame] | 3344 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); |
Michael Tuexen | ff68175 | 2021-09-01 13:00:05 +0200 | [diff] [blame] | 3345 | goto out; |
t00fcxen | c24796c | 2014-05-06 15:39:13 +0000 | [diff] [blame] | 3346 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3347 | #endif |
| 3348 | if (sin->sin_addr.s_addr != INADDR_ANY) { |
| 3349 | bindall = 0; |
| 3350 | } |
| 3351 | break; |
| 3352 | } |
| 3353 | #endif |
| 3354 | #ifdef INET6 |
| 3355 | case AF_INET6: |
| 3356 | { |
| 3357 | /* Only for pure IPv6 Address. (No IPv4 Mapped!) */ |
| 3358 | struct sockaddr_in6 *sin6; |
| 3359 | |
| 3360 | sin6 = (struct sockaddr_in6 *)addr; |
t00fcxen | a8a26f6 | 2012-09-12 19:24:54 +0000 | [diff] [blame] | 3361 | #ifdef HAVE_SA_LEN |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3362 | if (addr->sa_len != sizeof(*sin6)) { |
Michael Tuexen | ff68175 | 2021-09-01 13:00:05 +0200 | [diff] [blame] | 3363 | error = EINVAL; |
| 3364 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); |
| 3365 | goto out; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3366 | } |
| 3367 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3368 | lport = sin6->sin6_port; |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 3369 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
t00fcxen | c24796c | 2014-05-06 15:39:13 +0000 | [diff] [blame] | 3370 | /* |
| 3371 | * For LOOPBACK the prison_local_ip6() call will transmute the ipv6 address |
| 3372 | * to the proper value. |
| 3373 | */ |
Michael Tuexen | ff68175 | 2021-09-01 13:00:05 +0200 | [diff] [blame] | 3374 | if ((error = prison_local_ip6(td->td_ucred, &sin6->sin6_addr, |
Michael Tuexen | 72e5cec | 2019-08-05 13:21:02 +0200 | [diff] [blame] | 3375 | (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) { |
t00fcxen | c24796c | 2014-05-06 15:39:13 +0000 | [diff] [blame] | 3376 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); |
Michael Tuexen | ff68175 | 2021-09-01 13:00:05 +0200 | [diff] [blame] | 3377 | goto out; |
t00fcxen | c24796c | 2014-05-06 15:39:13 +0000 | [diff] [blame] | 3378 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3379 | #endif |
| 3380 | if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { |
| 3381 | bindall = 0; |
| 3382 | #ifdef SCTP_EMBEDDED_V6_SCOPE |
| 3383 | /* KAME hack: embed scopeid */ |
| 3384 | #if defined(SCTP_KAME) |
| 3385 | if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) { |
Michael Tuexen | ff68175 | 2021-09-01 13:00:05 +0200 | [diff] [blame] | 3386 | error = EINVAL; |
| 3387 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); |
| 3388 | goto out; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3389 | } |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 3390 | #elif defined(__APPLE__) && !defined(__Userspace__) |
tuexen | 9a218b1 | 2012-08-04 21:17:58 +0000 | [diff] [blame] | 3391 | #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) |
| 3392 | if (in6_embedscope(&sin6->sin6_addr, sin6, ip_inp, NULL) != 0) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3393 | #else |
tuexen | 9a218b1 | 2012-08-04 21:17:58 +0000 | [diff] [blame] | 3394 | if (in6_embedscope(&sin6->sin6_addr, sin6, ip_inp, NULL, NULL) != 0) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3395 | #endif |
Michael Tuexen | ff68175 | 2021-09-01 13:00:05 +0200 | [diff] [blame] | 3396 | error = EINVAL; |
| 3397 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); |
| 3398 | goto out; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3399 | } |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 3400 | #elif defined(__FreeBSD__) && !defined(__Userspace__) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3401 | error = scope6_check_id(sin6, MODULE_GLOBAL(ip6_use_defzone)); |
| 3402 | if (error != 0) { |
Michael Tuexen | ff68175 | 2021-09-01 13:00:05 +0200 | [diff] [blame] | 3403 | error = EINVAL; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3404 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); |
Michael Tuexen | ff68175 | 2021-09-01 13:00:05 +0200 | [diff] [blame] | 3405 | goto out; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3406 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3407 | #else |
| 3408 | if (in6_embedscope(&sin6->sin6_addr, sin6) != 0) { |
Michael Tuexen | ff68175 | 2021-09-01 13:00:05 +0200 | [diff] [blame] | 3409 | error = EINVAL; |
| 3410 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); |
| 3411 | goto out; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3412 | } |
| 3413 | #endif |
| 3414 | #endif /* SCTP_EMBEDDED_V6_SCOPE */ |
| 3415 | } |
| 3416 | #ifndef SCOPEDROUTING |
| 3417 | /* this must be cleared for ifa_ifwithaddr() */ |
| 3418 | sin6->sin6_scope_id = 0; |
| 3419 | #endif /* SCOPEDROUTING */ |
| 3420 | break; |
| 3421 | } |
| 3422 | #endif |
tuexen | 09a676e | 2012-07-08 21:03:18 +0000 | [diff] [blame] | 3423 | #if defined(__Userspace__) |
| 3424 | case AF_CONN: |
| 3425 | { |
| 3426 | struct sockaddr_conn *sconn; |
t00fcxen | efb47e7 | 2012-09-09 08:16:51 +0000 | [diff] [blame] | 3427 | |
t00fcxen | a8a26f6 | 2012-09-12 19:24:54 +0000 | [diff] [blame] | 3428 | #ifdef HAVE_SA_LEN |
tuexen | 09a676e | 2012-07-08 21:03:18 +0000 | [diff] [blame] | 3429 | if (addr->sa_len != sizeof(struct sockaddr_conn)) { |
Michael Tuexen | ff68175 | 2021-09-01 13:00:05 +0200 | [diff] [blame] | 3430 | error = EINVAL; |
| 3431 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); |
| 3432 | goto out; |
tuexen | 09a676e | 2012-07-08 21:03:18 +0000 | [diff] [blame] | 3433 | } |
| 3434 | #endif |
| 3435 | sconn = (struct sockaddr_conn *)addr; |
| 3436 | lport = sconn->sconn_port; |
| 3437 | if (sconn->sconn_addr != NULL) { |
| 3438 | bindall = 0; |
| 3439 | } |
| 3440 | break; |
| 3441 | } |
| 3442 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3443 | default: |
Michael Tuexen | ff68175 | 2021-09-01 13:00:05 +0200 | [diff] [blame] | 3444 | error = EAFNOSUPPORT; |
| 3445 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); |
| 3446 | goto out; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3447 | } |
| 3448 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3449 | /* Setup a vrf_id to be the default for the non-bind-all case. */ |
t00fcxen | c24796c | 2014-05-06 15:39:13 +0000 | [diff] [blame] | 3450 | vrf_id = inp->def_vrf_id; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3451 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3452 | if (lport) { |
| 3453 | /* |
t00fcxen | efb47e7 | 2012-09-09 08:16:51 +0000 | [diff] [blame] | 3454 | * Did the caller specify a port? if so we must see if an ep |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3455 | * already has this one bound. |
| 3456 | */ |
| 3457 | /* got to be root to get at low ports */ |
Michael Tuexen | eb61157 | 2020-06-13 17:27:02 +0200 | [diff] [blame] | 3458 | #if !(defined(_WIN32) && !defined(__Userspace__)) |
Michael Tuexen | a8409b3 | 2021-09-02 01:50:45 +0200 | [diff] [blame] | 3459 | if (ntohs(lport) < IPPORT_RESERVED && |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 3460 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
Michael Tuexen | a8409b3 | 2021-09-02 01:50:45 +0200 | [diff] [blame] | 3461 | (error = priv_check(td, PRIV_NETINET_RESERVEDPORT)) != 0) { |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 3462 | #elif defined(__APPLE__) && !defined(__Userspace__) |
Michael Tuexen | a8409b3 | 2021-09-02 01:50:45 +0200 | [diff] [blame] | 3463 | (error = suser(p->p_ucred, &p->p_acflag)) != 0) { |
Michael Tuexen | ff68175 | 2021-09-01 13:00:05 +0200 | [diff] [blame] | 3464 | #elif defined(__Userspace__) |
| 3465 | /* TODO ensure uid is 0, etc... */ |
Michael Tuexen | a8409b3 | 2021-09-02 01:50:45 +0200 | [diff] [blame] | 3466 | 0) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3467 | #else |
Michael Tuexen | a8409b3 | 2021-09-02 01:50:45 +0200 | [diff] [blame] | 3468 | (error = suser(p, 0)) != 0) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3469 | #endif |
Michael Tuexen | a8409b3 | 2021-09-02 01:50:45 +0200 | [diff] [blame] | 3470 | goto out; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3471 | } |
Michael Tuexen | eb61157 | 2020-06-13 17:27:02 +0200 | [diff] [blame] | 3472 | #endif |
Michael Tuexen | a8409b3 | 2021-09-02 01:50:45 +0200 | [diff] [blame] | 3473 | SCTP_INP_INCR_REF(inp); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3474 | SCTP_INP_WUNLOCK(inp); |
| 3475 | if (bindall) { |
| 3476 | #ifdef SCTP_MVRF |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 3477 | for (i = 0; i < inp->num_vrfs; i++) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3478 | vrf_id = inp->m_vrf_ids[i]; |
| 3479 | #else |
| 3480 | vrf_id = inp->def_vrf_id; |
| 3481 | #endif |
| 3482 | inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id); |
| 3483 | if (inp_tmp != NULL) { |
| 3484 | /* |
| 3485 | * lock guy returned and lower count |
| 3486 | * note that we are not bound so |
| 3487 | * inp_tmp should NEVER be inp. And |
| 3488 | * it is this inp (inp_tmp) that gets |
| 3489 | * the reference bump, so we must |
| 3490 | * lower it. |
| 3491 | */ |
| 3492 | SCTP_INP_DECR_REF(inp_tmp); |
| 3493 | /* unlock info */ |
| 3494 | if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) && |
| 3495 | (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) { |
| 3496 | /* Ok, must be one-2-one and allowing port re-use */ |
| 3497 | port_reuse_active = 1; |
| 3498 | goto continue_anyway; |
| 3499 | } |
Michael Tuexen | a8409b3 | 2021-09-02 01:50:45 +0200 | [diff] [blame] | 3500 | SCTP_INP_WLOCK(inp); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3501 | SCTP_INP_DECR_REF(inp); |
Michael Tuexen | ff68175 | 2021-09-01 13:00:05 +0200 | [diff] [blame] | 3502 | error = EADDRINUSE; |
| 3503 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); |
Michael Tuexen | a8409b3 | 2021-09-02 01:50:45 +0200 | [diff] [blame] | 3504 | goto out; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3505 | } |
| 3506 | #ifdef SCTP_MVRF |
| 3507 | } |
| 3508 | #endif |
| 3509 | } else { |
| 3510 | inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id); |
| 3511 | if (inp_tmp != NULL) { |
| 3512 | /* |
| 3513 | * lock guy returned and lower count note |
| 3514 | * that we are not bound so inp_tmp should |
| 3515 | * NEVER be inp. And it is this inp (inp_tmp) |
| 3516 | * that gets the reference bump, so we must |
| 3517 | * lower it. |
| 3518 | */ |
| 3519 | SCTP_INP_DECR_REF(inp_tmp); |
| 3520 | /* unlock info */ |
| 3521 | if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) && |
| 3522 | (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) { |
| 3523 | /* Ok, must be one-2-one and allowing port re-use */ |
| 3524 | port_reuse_active = 1; |
| 3525 | goto continue_anyway; |
| 3526 | } |
Michael Tuexen | a8409b3 | 2021-09-02 01:50:45 +0200 | [diff] [blame] | 3527 | SCTP_INP_WLOCK(inp); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3528 | SCTP_INP_DECR_REF(inp); |
Michael Tuexen | ff68175 | 2021-09-01 13:00:05 +0200 | [diff] [blame] | 3529 | error = EADDRINUSE; |
| 3530 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); |
Michael Tuexen | a8409b3 | 2021-09-02 01:50:45 +0200 | [diff] [blame] | 3531 | goto out; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3532 | } |
| 3533 | } |
| 3534 | continue_anyway: |
| 3535 | SCTP_INP_WLOCK(inp); |
Michael Tuexen | ff68175 | 2021-09-01 13:00:05 +0200 | [diff] [blame] | 3536 | SCTP_INP_DECR_REF(inp); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3537 | if (bindall) { |
| 3538 | /* verify that no lport is not used by a singleton */ |
t00fcxen | efb47e7 | 2012-09-09 08:16:51 +0000 | [diff] [blame] | 3539 | if ((port_reuse_active == 0) && |
| 3540 | (inp_tmp = sctp_isport_inuse(inp, lport, vrf_id))) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3541 | /* Sorry someone already has this one bound */ |
| 3542 | if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) && |
| 3543 | (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) { |
| 3544 | port_reuse_active = 1; |
| 3545 | } else { |
Michael Tuexen | ff68175 | 2021-09-01 13:00:05 +0200 | [diff] [blame] | 3546 | error = EADDRINUSE; |
| 3547 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); |
| 3548 | goto out; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3549 | } |
| 3550 | } |
| 3551 | } |
| 3552 | } else { |
| 3553 | uint16_t first, last, candidate; |
t00fcxen | c24796c | 2014-05-06 15:39:13 +0000 | [diff] [blame] | 3554 | uint16_t count; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3555 | |
Michael Tuexen | ff68175 | 2021-09-01 13:00:05 +0200 | [diff] [blame] | 3556 | #if defined(__Userspace__) |
| 3557 | first = MODULE_GLOBAL(ipport_firstauto); |
| 3558 | last = MODULE_GLOBAL(ipport_lastauto); |
| 3559 | #elif defined(_WIN32) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3560 | first = 1; |
| 3561 | last = 0xffff; |
Michael Tuexen | ff68175 | 2021-09-01 13:00:05 +0200 | [diff] [blame] | 3562 | #elif defined(__FreeBSD__) || defined(__APPLE__) |
t00fcxen | c24796c | 2014-05-06 15:39:13 +0000 | [diff] [blame] | 3563 | if (ip_inp->inp_flags & INP_HIGHPORT) { |
| 3564 | first = MODULE_GLOBAL(ipport_hifirstauto); |
| 3565 | last = MODULE_GLOBAL(ipport_hilastauto); |
| 3566 | } else if (ip_inp->inp_flags & INP_LOWPORT) { |
Michael Tuexen | a508995 | 2020-06-09 23:11:08 +0200 | [diff] [blame] | 3567 | #if defined(__FreeBSD__) |
Michael Tuexen | ff68175 | 2021-09-01 13:00:05 +0200 | [diff] [blame] | 3568 | if ((error = priv_check(td, PRIV_NETINET_RESERVEDPORT)) != 0) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3569 | #else |
Michael Tuexen | ff68175 | 2021-09-01 13:00:05 +0200 | [diff] [blame] | 3570 | if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3571 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3572 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); |
Michael Tuexen | ff68175 | 2021-09-01 13:00:05 +0200 | [diff] [blame] | 3573 | goto out; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3574 | } |
t00fcxen | c24796c | 2014-05-06 15:39:13 +0000 | [diff] [blame] | 3575 | first = MODULE_GLOBAL(ipport_lowfirstauto); |
| 3576 | last = MODULE_GLOBAL(ipport_lowlastauto); |
| 3577 | } else { |
t00fcxen | 8dd95b8 | 2014-07-11 20:41:28 +0000 | [diff] [blame] | 3578 | first = MODULE_GLOBAL(ipport_firstauto); |
| 3579 | last = MODULE_GLOBAL(ipport_lastauto); |
t00fcxen | c24796c | 2014-05-06 15:39:13 +0000 | [diff] [blame] | 3580 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3581 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3582 | if (first > last) { |
| 3583 | uint16_t temp; |
| 3584 | |
| 3585 | temp = first; |
| 3586 | first = last; |
| 3587 | last = temp; |
| 3588 | } |
| 3589 | count = last - first + 1; /* number of candidates */ |
| 3590 | candidate = first + sctp_select_initial_TSN(&inp->sctp_ep) % (count); |
| 3591 | |
Michael Tuexen | ff68175 | 2021-09-01 13:00:05 +0200 | [diff] [blame] | 3592 | for (;;) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3593 | #ifdef SCTP_MVRF |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 3594 | for (i = 0; i < inp->num_vrfs; i++) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3595 | if (sctp_isport_inuse(inp, htons(candidate), inp->m_vrf_ids[i]) != NULL) { |
| 3596 | break; |
| 3597 | } |
| 3598 | } |
| 3599 | if (i == inp->num_vrfs) { |
Michael Tuexen | ff68175 | 2021-09-01 13:00:05 +0200 | [diff] [blame] | 3600 | lport = htons(candidate); |
| 3601 | break; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3602 | } |
| 3603 | #else |
| 3604 | if (sctp_isport_inuse(inp, htons(candidate), inp->def_vrf_id) == NULL) { |
Michael Tuexen | ff68175 | 2021-09-01 13:00:05 +0200 | [diff] [blame] | 3605 | lport = htons(candidate); |
| 3606 | break; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3607 | } |
| 3608 | #endif |
Michael Tuexen | ff68175 | 2021-09-01 13:00:05 +0200 | [diff] [blame] | 3609 | if (--count == 0) { |
| 3610 | error = EADDRINUSE; |
| 3611 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); |
| 3612 | goto out; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3613 | } |
Michael Tuexen | ff68175 | 2021-09-01 13:00:05 +0200 | [diff] [blame] | 3614 | if (candidate == last) |
| 3615 | candidate = first; |
| 3616 | else |
| 3617 | candidate = candidate + 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3618 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3619 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3620 | if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | |
| 3621 | SCTP_PCB_FLAGS_SOCKET_ALLGONE)) { |
| 3622 | /* |
| 3623 | * this really should not happen. The guy did a non-blocking |
| 3624 | * bind and then did a close at the same time. |
| 3625 | */ |
Michael Tuexen | ff68175 | 2021-09-01 13:00:05 +0200 | [diff] [blame] | 3626 | error = EINVAL; |
| 3627 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); |
| 3628 | goto out; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3629 | } |
| 3630 | /* ok we look clear to give out this port, so lets setup the binding */ |
| 3631 | if (bindall) { |
| 3632 | /* binding to all addresses, so just set in the proper flags */ |
| 3633 | inp->sctp_flags |= SCTP_PCB_FLAGS_BOUNDALL; |
| 3634 | /* set the automatic addr changes from kernel flag */ |
| 3635 | if (SCTP_BASE_SYSCTL(sctp_auto_asconf) == 0) { |
| 3636 | sctp_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF); |
| 3637 | sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF); |
| 3638 | } else { |
| 3639 | sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF); |
| 3640 | sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF); |
| 3641 | } |
| 3642 | if (SCTP_BASE_SYSCTL(sctp_multiple_asconfs) == 0) { |
| 3643 | sctp_feature_off(inp, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS); |
| 3644 | } else { |
| 3645 | sctp_feature_on(inp, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS); |
| 3646 | } |
| 3647 | /* set the automatic mobility_base from kernel |
| 3648 | flag (by micchie) |
| 3649 | */ |
| 3650 | if (SCTP_BASE_SYSCTL(sctp_mobility_base) == 0) { |
| 3651 | sctp_mobility_feature_off(inp, SCTP_MOBILITY_BASE); |
| 3652 | sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED); |
t00fcxen | efb47e7 | 2012-09-09 08:16:51 +0000 | [diff] [blame] | 3653 | } else { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3654 | sctp_mobility_feature_on(inp, SCTP_MOBILITY_BASE); |
| 3655 | sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED); |
| 3656 | } |
| 3657 | /* set the automatic mobility_fasthandoff from kernel |
| 3658 | flag (by micchie) |
| 3659 | */ |
| 3660 | if (SCTP_BASE_SYSCTL(sctp_mobility_fasthandoff) == 0) { |
| 3661 | sctp_mobility_feature_off(inp, SCTP_MOBILITY_FASTHANDOFF); |
| 3662 | sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED); |
t00fcxen | efb47e7 | 2012-09-09 08:16:51 +0000 | [diff] [blame] | 3663 | } else { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3664 | sctp_mobility_feature_on(inp, SCTP_MOBILITY_FASTHANDOFF); |
| 3665 | sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED); |
| 3666 | } |
| 3667 | } else { |
| 3668 | /* |
| 3669 | * bind specific, make sure flags is off and add a new |
| 3670 | * address structure to the sctp_addr_list inside the ep |
| 3671 | * structure. |
| 3672 | * |
| 3673 | * We will need to allocate one and insert it at the head. The |
| 3674 | * socketopt call can just insert new addresses in there as |
| 3675 | * well. It will also have to do the embed scope kame hack |
| 3676 | * too (before adding). |
| 3677 | */ |
| 3678 | struct sctp_ifa *ifa; |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 3679 | union sctp_sockstore store; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3680 | |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 3681 | memset(&store, 0, sizeof(store)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3682 | switch (addr->sa_family) { |
tuexen | 310f1bc | 2012-07-15 11:20:56 +0000 | [diff] [blame] | 3683 | #ifdef INET |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3684 | case AF_INET: |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 3685 | memcpy(&store.sin, addr, sizeof(struct sockaddr_in)); |
| 3686 | store.sin.sin_port = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3687 | break; |
tuexen | 310f1bc | 2012-07-15 11:20:56 +0000 | [diff] [blame] | 3688 | #endif |
| 3689 | #ifdef INET6 |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3690 | case AF_INET6: |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 3691 | memcpy(&store.sin6, addr, sizeof(struct sockaddr_in6)); |
| 3692 | store.sin6.sin6_port = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3693 | break; |
tuexen | 310f1bc | 2012-07-15 11:20:56 +0000 | [diff] [blame] | 3694 | #endif |
tuexen | cc2b142 | 2012-07-10 21:50:32 +0000 | [diff] [blame] | 3695 | #if defined(__Userspace__) |
| 3696 | case AF_CONN: |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 3697 | memcpy(&store.sconn, addr, sizeof(struct sockaddr_conn)); |
| 3698 | store.sconn.sconn_port = 0; |
tuexen | cc2b142 | 2012-07-10 21:50:32 +0000 | [diff] [blame] | 3699 | break; |
tuexen | cc2b142 | 2012-07-10 21:50:32 +0000 | [diff] [blame] | 3700 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3701 | default: |
| 3702 | break; |
| 3703 | } |
| 3704 | /* |
| 3705 | * first find the interface with the bound address need to |
| 3706 | * zero out the port to find the address! yuck! can't do |
| 3707 | * this earlier since need port for sctp_pcb_findep() |
| 3708 | */ |
t00fcxen | efb47e7 | 2012-09-09 08:16:51 +0000 | [diff] [blame] | 3709 | if (sctp_ifap != NULL) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3710 | ifa = sctp_ifap; |
t00fcxen | efb47e7 | 2012-09-09 08:16:51 +0000 | [diff] [blame] | 3711 | } else { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3712 | /* Note for BSD we hit here always other |
| 3713 | * O/S's will pass things in via the |
Michael Tuexen | 3b4263d | 2020-06-06 19:52:50 +0200 | [diff] [blame] | 3714 | * sctp_ifap argument. |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3715 | */ |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 3716 | ifa = sctp_find_ifa_by_addr(&store.sa, |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3717 | vrf_id, SCTP_ADDR_NOT_LOCKED); |
| 3718 | } |
| 3719 | if (ifa == NULL) { |
Michael Tuexen | ff68175 | 2021-09-01 13:00:05 +0200 | [diff] [blame] | 3720 | error = EADDRNOTAVAIL; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3721 | /* Can't find an interface with that address */ |
Michael Tuexen | ff68175 | 2021-09-01 13:00:05 +0200 | [diff] [blame] | 3722 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); |
| 3723 | goto out; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3724 | } |
| 3725 | #ifdef INET6 |
| 3726 | if (addr->sa_family == AF_INET6) { |
| 3727 | /* GAK, more FIXME IFA lock? */ |
| 3728 | if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) { |
| 3729 | /* Can't bind a non-existent addr. */ |
Michael Tuexen | ff68175 | 2021-09-01 13:00:05 +0200 | [diff] [blame] | 3730 | error = EINVAL; |
| 3731 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); |
| 3732 | goto out; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3733 | } |
| 3734 | } |
| 3735 | #endif |
| 3736 | /* we're not bound all */ |
| 3737 | inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUNDALL; |
| 3738 | /* allow bindx() to send ASCONF's for binding changes */ |
| 3739 | sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF); |
| 3740 | /* clear automatic addr changes from kernel flag */ |
| 3741 | sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF); |
| 3742 | |
| 3743 | /* add this address to the endpoint list */ |
| 3744 | error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, 0); |
Michael Tuexen | ff68175 | 2021-09-01 13:00:05 +0200 | [diff] [blame] | 3745 | if (error != 0) |
| 3746 | goto out; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3747 | inp->laddr_count++; |
| 3748 | } |
| 3749 | /* find the bucket */ |
tuexen | 98456cf | 2012-04-19 15:37:07 +0000 | [diff] [blame] | 3750 | if (port_reuse_active) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3751 | /* Put it into tcp 1-2-1 hash */ |
| 3752 | head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashtcpmark))]; |
| 3753 | inp->sctp_flags |= SCTP_PCB_FLAGS_IN_TCPPOOL; |
t00fcxen | c24796c | 2014-05-06 15:39:13 +0000 | [diff] [blame] | 3754 | } else { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3755 | head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashmark))]; |
| 3756 | } |
| 3757 | /* put it in the bucket */ |
| 3758 | LIST_INSERT_HEAD(head, inp, sctp_hash); |
| 3759 | SCTPDBG(SCTP_DEBUG_PCB1, "Main hash to bind at head:%p, bound port:%d - in tcp_pool=%d\n", |
t00fcxen | 8fca7ff | 2012-09-05 18:58:42 +0000 | [diff] [blame] | 3760 | (void *)head, ntohs(lport), port_reuse_active); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3761 | /* set in the port */ |
| 3762 | inp->sctp_lport = lport; |
| 3763 | |
| 3764 | /* turn off just the unbound flag */ |
Michael Tuexen | ff68175 | 2021-09-01 13:00:05 +0200 | [diff] [blame] | 3765 | KASSERT((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) != 0, |
| 3766 | ("%s: inp %p is already bound", __func__, inp)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3767 | inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND; |
Michael Tuexen | ff68175 | 2021-09-01 13:00:05 +0200 | [diff] [blame] | 3768 | out: |
Michael Tuexen | a8409b3 | 2021-09-02 01:50:45 +0200 | [diff] [blame] | 3769 | return (error); |
| 3770 | } |
| 3771 | |
| 3772 | int |
| 3773 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 3774 | sctp_inpcb_bind(struct socket *so, struct sockaddr *addr, |
| 3775 | struct sctp_ifa *sctp_ifap, struct thread *td) |
| 3776 | #elif defined(_WIN32) && !defined(__Userspace__) |
| 3777 | sctp_inpcb_bind(struct socket *so, struct sockaddr *addr, |
| 3778 | struct sctp_ifa *sctp_ifap, PKTHREAD p) |
| 3779 | #else |
| 3780 | sctp_inpcb_bind(struct socket *so, struct sockaddr *addr, |
| 3781 | struct sctp_ifa *sctp_ifap, struct proc *p) |
| 3782 | #endif |
| 3783 | { |
| 3784 | struct sctp_inpcb *inp; |
| 3785 | int error; |
| 3786 | |
| 3787 | inp = so->so_pcb; |
| 3788 | SCTP_INP_INFO_WLOCK(); |
| 3789 | SCTP_INP_WLOCK(inp); |
| 3790 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 3791 | error = sctp_inpcb_bind_locked(inp, addr, sctp_ifap, td); |
| 3792 | #else |
| 3793 | error = sctp_inpcb_bind_locked(inp, addr, sctp_ifap, p); |
| 3794 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3795 | SCTP_INP_WUNLOCK(inp); |
| 3796 | SCTP_INP_INFO_WUNLOCK(); |
Michael Tuexen | ff68175 | 2021-09-01 13:00:05 +0200 | [diff] [blame] | 3797 | return (error); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3798 | } |
| 3799 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3800 | static void |
| 3801 | sctp_iterator_inp_being_freed(struct sctp_inpcb *inp) |
| 3802 | { |
| 3803 | struct sctp_iterator *it, *nit; |
| 3804 | |
| 3805 | /* |
| 3806 | * We enter with the only the ITERATOR_LOCK in place and a write |
| 3807 | * lock on the inp_info stuff. |
| 3808 | */ |
| 3809 | it = sctp_it_ctl.cur_it; |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 3810 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3811 | if (it && (it->vn != curvnet)) { |
| 3812 | /* Its not looking at our VNET */ |
| 3813 | return; |
| 3814 | } |
| 3815 | #endif |
| 3816 | if (it && (it->inp == inp)) { |
| 3817 | /* |
| 3818 | * This is tricky and we hold the iterator lock, |
| 3819 | * but when it returns and gets the lock (when we |
| 3820 | * release it) the iterator will try to operate on |
| 3821 | * inp. We need to stop that from happening. But |
| 3822 | * of course the iterator has a reference on the |
| 3823 | * stcb and inp. We can mark it and it will stop. |
| 3824 | * |
| 3825 | * If its a single iterator situation, we |
| 3826 | * set the end iterator flag. Otherwise |
| 3827 | * we set the iterator to go to the next inp. |
| 3828 | * |
| 3829 | */ |
| 3830 | if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) { |
| 3831 | sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_IT; |
| 3832 | } else { |
| 3833 | sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_INP; |
| 3834 | } |
| 3835 | } |
| 3836 | /* Now go through and remove any single reference to |
| 3837 | * our inp that may be still pending on the list |
| 3838 | */ |
| 3839 | SCTP_IPI_ITERATOR_WQ_LOCK(); |
| 3840 | TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) { |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 3841 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3842 | if (it->vn != curvnet) { |
| 3843 | continue; |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 3844 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3845 | #endif |
| 3846 | if (it->inp == inp) { |
| 3847 | /* This one points to me is it inp specific? */ |
| 3848 | if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) { |
| 3849 | /* Remove and free this one */ |
| 3850 | TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, |
| 3851 | it, sctp_nxt_itr); |
| 3852 | if (it->function_atend != NULL) { |
| 3853 | (*it->function_atend) (it->pointer, it->val); |
| 3854 | } |
| 3855 | SCTP_FREE(it, SCTP_M_ITER); |
| 3856 | } else { |
| 3857 | it->inp = LIST_NEXT(it->inp, sctp_list); |
| 3858 | if (it->inp) { |
| 3859 | SCTP_INP_INCR_REF(it->inp); |
| 3860 | } |
| 3861 | } |
| 3862 | /* When its put in the refcnt is incremented so decr it */ |
| 3863 | SCTP_INP_DECR_REF(inp); |
| 3864 | } |
| 3865 | } |
| 3866 | SCTP_IPI_ITERATOR_WQ_UNLOCK(); |
| 3867 | } |
| 3868 | |
| 3869 | /* release sctp_inpcb unbind the port */ |
| 3870 | void |
| 3871 | sctp_inpcb_free(struct sctp_inpcb *inp, int immediate, int from) |
| 3872 | { |
| 3873 | /* |
| 3874 | * Here we free a endpoint. We must find it (if it is in the Hash |
| 3875 | * table) and remove it from there. Then we must also find it in the |
| 3876 | * overall list and remove it from there. After all removals are |
| 3877 | * complete then any timer has to be stopped. Then start the actual |
| 3878 | * freeing. a) Any local lists. b) Any associations. c) The hash of |
| 3879 | * all associations. d) finally the ep itself. |
| 3880 | */ |
Michael Tuexen | 2292a60 | 2022-04-07 17:49:53 +0200 | [diff] [blame] | 3881 | struct sctp_tcb *stcb, *nstcb; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3882 | struct sctp_laddr *laddr, *nladdr; |
| 3883 | struct inpcb *ip_pcb; |
| 3884 | struct socket *so; |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 3885 | int being_refed = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3886 | struct sctp_queued_to_read *sq, *nsq; |
Michael Tuexen | 3b4263d | 2020-06-06 19:52:50 +0200 | [diff] [blame] | 3887 | #if !defined(__Userspace__) |
Michael Tuexen | a508995 | 2020-06-09 23:11:08 +0200 | [diff] [blame] | 3888 | #if !defined(__FreeBSD__) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3889 | sctp_rtentry_t *rt; |
| 3890 | #endif |
| 3891 | #endif |
| 3892 | int cnt; |
| 3893 | sctp_sharedkey_t *shared_key, *nshared_key; |
| 3894 | |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 3895 | #if defined(__APPLE__) && !defined(__Userspace__) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3896 | sctp_lock_assert(SCTP_INP_SO(inp)); |
| 3897 | #endif |
| 3898 | #ifdef SCTP_LOG_CLOSING |
| 3899 | sctp_log_closing(inp, NULL, 0); |
| 3900 | #endif |
| 3901 | SCTP_ITERATOR_LOCK(); |
| 3902 | /* mark any iterators on the list or being processed */ |
| 3903 | sctp_iterator_inp_being_freed(inp); |
| 3904 | SCTP_ITERATOR_UNLOCK(); |
Michael Tuexen | 900def6 | 2021-09-08 13:12:50 +0200 | [diff] [blame] | 3905 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3906 | SCTP_ASOC_CREATE_LOCK(inp); |
| 3907 | SCTP_INP_INFO_WLOCK(); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3908 | SCTP_INP_WLOCK(inp); |
Michael Tuexen | 900def6 | 2021-09-08 13:12:50 +0200 | [diff] [blame] | 3909 | so = inp->sctp_socket; |
| 3910 | KASSERT((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) != 0, |
| 3911 | ("%s: inp %p still has socket", __func__, inp)); |
| 3912 | KASSERT((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0, |
| 3913 | ("%s: double free of inp %p", __func__, inp)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3914 | if (from == SCTP_CALLED_AFTER_CMPSET_OFCLOSE) { |
| 3915 | inp->sctp_flags &= ~SCTP_PCB_FLAGS_CLOSE_IP; |
| 3916 | /* socket is gone, so no more wakeups allowed */ |
| 3917 | inp->sctp_flags |= SCTP_PCB_FLAGS_DONT_WAKE; |
| 3918 | inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT; |
| 3919 | inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3920 | } |
| 3921 | /* First time through we have the socket lock, after that no more. */ |
| 3922 | sctp_timer_stop(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL, |
Michael Tuexen | 390beeb | 2020-02-04 15:05:45 +0100 | [diff] [blame] | 3923 | SCTP_FROM_SCTP_PCB + SCTP_LOC_1); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3924 | |
| 3925 | if (inp->control) { |
| 3926 | sctp_m_freem(inp->control); |
| 3927 | inp->control = NULL; |
| 3928 | } |
| 3929 | if (inp->pkt) { |
| 3930 | sctp_m_freem(inp->pkt); |
| 3931 | inp->pkt = NULL; |
| 3932 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3933 | ip_pcb = &inp->ip_inp.inp; /* we could just cast the main pointer |
| 3934 | * here but I will be nice :> (i.e. |
| 3935 | * ip_pcb = ep;) */ |
| 3936 | if (immediate == SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE) { |
| 3937 | int cnt_in_sd; |
| 3938 | |
| 3939 | cnt_in_sd = 0; |
Michael Tuexen | 2292a60 | 2022-04-07 17:49:53 +0200 | [diff] [blame] | 3940 | LIST_FOREACH_SAFE(stcb, &inp->sctp_asoc_list, sctp_tcblist, nstcb) { |
| 3941 | SCTP_TCB_LOCK(stcb); |
Michael Tuexen | 28e2ea7 | 2022-04-15 15:23:01 +0200 | [diff] [blame] | 3942 | /* Disconnect the socket please. */ |
| 3943 | stcb->sctp_socket = NULL; |
| 3944 | SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_CLOSED_SOCKET); |
Michael Tuexen | 2292a60 | 2022-04-07 17:49:53 +0200 | [diff] [blame] | 3945 | if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3946 | /* Skip guys being freed */ |
| 3947 | cnt_in_sd++; |
Michael Tuexen | 2292a60 | 2022-04-07 17:49:53 +0200 | [diff] [blame] | 3948 | if (stcb->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3949 | /* |
| 3950 | * Special case - we did not start a kill |
| 3951 | * timer on the asoc due to it was not |
| 3952 | * closed. So go ahead and start it now. |
| 3953 | */ |
Michael Tuexen | 2292a60 | 2022-04-07 17:49:53 +0200 | [diff] [blame] | 3954 | SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE); |
| 3955 | sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3956 | } |
Michael Tuexen | 2292a60 | 2022-04-07 17:49:53 +0200 | [diff] [blame] | 3957 | SCTP_TCB_UNLOCK(stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3958 | continue; |
| 3959 | } |
Michael Tuexen | 2292a60 | 2022-04-07 17:49:53 +0200 | [diff] [blame] | 3960 | if (((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) || |
| 3961 | (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) && |
| 3962 | (stcb->asoc.total_output_queue_size == 0)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3963 | /* If we have data in queue, we don't want to just |
| 3964 | * free since the app may have done, send()/close |
| 3965 | * or connect/send/close. And it wants the data |
| 3966 | * to get across first. |
| 3967 | */ |
| 3968 | /* Just abandon things in the front states */ |
Michael Tuexen | 2292a60 | 2022-04-07 17:49:53 +0200 | [diff] [blame] | 3969 | if (sctp_free_assoc(inp, stcb, SCTP_PCBFREE_NOFORCE, |
t00fcxen | 0057a6d | 2015-05-28 16:42:49 +0000 | [diff] [blame] | 3970 | SCTP_FROM_SCTP_PCB + SCTP_LOC_2) == 0) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3971 | cnt_in_sd++; |
| 3972 | } |
| 3973 | continue; |
| 3974 | } |
Michael Tuexen | 2292a60 | 2022-04-07 17:49:53 +0200 | [diff] [blame] | 3975 | if ((stcb->asoc.size_on_reasm_queue > 0) || |
| 3976 | (stcb->asoc.control_pdapi) || |
| 3977 | (stcb->asoc.size_on_all_streams > 0) || |
Michael Tuexen | 13a3b53 | 2022-05-15 22:51:57 +0200 | [diff] [blame] | 3978 | ((so != NULL) && (SCTP_SBAVAIL(&so->so_rcv) > 0))) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3979 | /* Left with Data unread */ |
| 3980 | struct mbuf *op_err; |
| 3981 | |
t00fcxen | 08f9ff9 | 2014-03-16 13:38:54 +0000 | [diff] [blame] | 3982 | op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, ""); |
Michael Tuexen | 2292a60 | 2022-04-07 17:49:53 +0200 | [diff] [blame] | 3983 | stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_3; |
| 3984 | sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3985 | SCTP_STAT_INCR_COUNTER32(sctps_aborted); |
Michael Tuexen | 2292a60 | 2022-04-07 17:49:53 +0200 | [diff] [blame] | 3986 | if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) || |
| 3987 | (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3988 | SCTP_STAT_DECR_GAUGE32(sctps_currestab); |
| 3989 | } |
Michael Tuexen | 2292a60 | 2022-04-07 17:49:53 +0200 | [diff] [blame] | 3990 | if (sctp_free_assoc(inp, stcb, |
t00fcxen | 0057a6d | 2015-05-28 16:42:49 +0000 | [diff] [blame] | 3991 | SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_4) == 0) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3992 | cnt_in_sd++; |
| 3993 | } |
| 3994 | continue; |
Michael Tuexen | 2292a60 | 2022-04-07 17:49:53 +0200 | [diff] [blame] | 3995 | } else if (TAILQ_EMPTY(&stcb->asoc.send_queue) && |
| 3996 | TAILQ_EMPTY(&stcb->asoc.sent_queue) && |
| 3997 | (stcb->asoc.stream_queue_cnt == 0)) { |
| 3998 | if ((*stcb->asoc.ss_functions.sctp_ss_is_user_msgs_incomplete)(stcb, &stcb->asoc)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3999 | goto abort_anyway; |
| 4000 | } |
Michael Tuexen | 2292a60 | 2022-04-07 17:49:53 +0200 | [diff] [blame] | 4001 | if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) && |
| 4002 | (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4003 | struct sctp_nets *netp; |
| 4004 | |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 4005 | /* |
| 4006 | * there is nothing queued to send, |
| 4007 | * so I send shutdown |
| 4008 | */ |
Michael Tuexen | 2292a60 | 2022-04-07 17:49:53 +0200 | [diff] [blame] | 4009 | if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) || |
| 4010 | (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) { |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 4011 | SCTP_STAT_DECR_GAUGE32(sctps_currestab); |
| 4012 | } |
Michael Tuexen | 2292a60 | 2022-04-07 17:49:53 +0200 | [diff] [blame] | 4013 | SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_SENT); |
| 4014 | sctp_stop_timers_for_shutdown(stcb); |
| 4015 | if (stcb->asoc.alternate) { |
| 4016 | netp = stcb->asoc.alternate; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4017 | } else { |
Michael Tuexen | 2292a60 | 2022-04-07 17:49:53 +0200 | [diff] [blame] | 4018 | netp = stcb->asoc.primary_destination; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4019 | } |
Michael Tuexen | 2292a60 | 2022-04-07 17:49:53 +0200 | [diff] [blame] | 4020 | sctp_send_shutdown(stcb, netp); |
| 4021 | sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb, |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4022 | netp); |
Michael Tuexen | 2292a60 | 2022-04-07 17:49:53 +0200 | [diff] [blame] | 4023 | sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb, NULL); |
| 4024 | sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_LOCKED); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4025 | } |
| 4026 | } else { |
| 4027 | /* mark into shutdown pending */ |
Michael Tuexen | 2292a60 | 2022-04-07 17:49:53 +0200 | [diff] [blame] | 4028 | SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_SHUTDOWN_PENDING); |
| 4029 | sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb, NULL); |
| 4030 | if ((*stcb->asoc.ss_functions.sctp_ss_is_user_msgs_incomplete)(stcb, &stcb->asoc)) { |
| 4031 | SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_PARTIAL_MSG_LEFT); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4032 | } |
Michael Tuexen | 2292a60 | 2022-04-07 17:49:53 +0200 | [diff] [blame] | 4033 | if (TAILQ_EMPTY(&stcb->asoc.send_queue) && |
| 4034 | TAILQ_EMPTY(&stcb->asoc.sent_queue) && |
| 4035 | (stcb->asoc.state & SCTP_STATE_PARTIAL_MSG_LEFT)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4036 | struct mbuf *op_err; |
| 4037 | abort_anyway: |
t00fcxen | 08f9ff9 | 2014-03-16 13:38:54 +0000 | [diff] [blame] | 4038 | op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, ""); |
Michael Tuexen | 2292a60 | 2022-04-07 17:49:53 +0200 | [diff] [blame] | 4039 | stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_5; |
| 4040 | sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4041 | SCTP_STAT_INCR_COUNTER32(sctps_aborted); |
Michael Tuexen | 2292a60 | 2022-04-07 17:49:53 +0200 | [diff] [blame] | 4042 | if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) || |
| 4043 | (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4044 | SCTP_STAT_DECR_GAUGE32(sctps_currestab); |
| 4045 | } |
Michael Tuexen | 2292a60 | 2022-04-07 17:49:53 +0200 | [diff] [blame] | 4046 | if (sctp_free_assoc(inp, stcb, |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4047 | SCTP_PCBFREE_NOFORCE, |
t00fcxen | 0057a6d | 2015-05-28 16:42:49 +0000 | [diff] [blame] | 4048 | SCTP_FROM_SCTP_PCB + SCTP_LOC_6) == 0) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4049 | cnt_in_sd++; |
| 4050 | } |
| 4051 | continue; |
| 4052 | } else { |
Michael Tuexen | 2292a60 | 2022-04-07 17:49:53 +0200 | [diff] [blame] | 4053 | sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4054 | } |
| 4055 | } |
| 4056 | cnt_in_sd++; |
Michael Tuexen | 2292a60 | 2022-04-07 17:49:53 +0200 | [diff] [blame] | 4057 | SCTP_TCB_UNLOCK(stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4058 | } |
| 4059 | /* now is there some left in our SHUTDOWN state? */ |
| 4060 | if (cnt_in_sd) { |
| 4061 | #ifdef SCTP_LOG_CLOSING |
| 4062 | sctp_log_closing(inp, NULL, 2); |
| 4063 | #endif |
| 4064 | inp->sctp_socket = NULL; |
| 4065 | SCTP_INP_WUNLOCK(inp); |
| 4066 | SCTP_ASOC_CREATE_UNLOCK(inp); |
| 4067 | SCTP_INP_INFO_WUNLOCK(); |
| 4068 | return; |
| 4069 | } |
| 4070 | } |
| 4071 | inp->sctp_socket = NULL; |
Michael Tuexen | 8d6935d | 2022-05-15 01:51:24 +0200 | [diff] [blame] | 4072 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4073 | /* |
| 4074 | * ok, this guy has been bound. It's port is |
| 4075 | * somewhere in the SCTP_BASE_INFO(hash table). Remove |
| 4076 | * it! |
| 4077 | */ |
| 4078 | LIST_REMOVE(inp, sctp_hash); |
| 4079 | inp->sctp_flags |= SCTP_PCB_FLAGS_UNBOUND; |
| 4080 | } |
| 4081 | |
| 4082 | /* If there is a timer running to kill us, |
| 4083 | * forget it, since it may have a contest |
| 4084 | * on the INP lock.. which would cause us |
| 4085 | * to die ... |
| 4086 | */ |
| 4087 | cnt = 0; |
Michael Tuexen | 2292a60 | 2022-04-07 17:49:53 +0200 | [diff] [blame] | 4088 | LIST_FOREACH_SAFE(stcb, &inp->sctp_asoc_list, sctp_tcblist, nstcb) { |
| 4089 | SCTP_TCB_LOCK(stcb); |
Michael Tuexen | 547d3b4 | 2020-07-23 21:49:32 +0200 | [diff] [blame] | 4090 | if (immediate != SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE) { |
| 4091 | /* Disconnect the socket please */ |
Michael Tuexen | 2292a60 | 2022-04-07 17:49:53 +0200 | [diff] [blame] | 4092 | stcb->sctp_socket = NULL; |
| 4093 | SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_CLOSED_SOCKET); |
Michael Tuexen | 547d3b4 | 2020-07-23 21:49:32 +0200 | [diff] [blame] | 4094 | } |
Michael Tuexen | 2292a60 | 2022-04-07 17:49:53 +0200 | [diff] [blame] | 4095 | if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { |
| 4096 | if (stcb->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE) { |
| 4097 | SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE); |
| 4098 | sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4099 | } |
Michael Tuexen | 61f6683 | 2020-02-09 23:15:19 +0100 | [diff] [blame] | 4100 | cnt++; |
Michael Tuexen | 2292a60 | 2022-04-07 17:49:53 +0200 | [diff] [blame] | 4101 | SCTP_TCB_UNLOCK(stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4102 | continue; |
| 4103 | } |
| 4104 | /* Free associations that are NOT killing us */ |
Michael Tuexen | 2292a60 | 2022-04-07 17:49:53 +0200 | [diff] [blame] | 4105 | if ((SCTP_GET_STATE(stcb) != SCTP_STATE_COOKIE_WAIT) && |
| 4106 | ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4107 | struct mbuf *op_err; |
t00fcxen | 28611aa | 2012-09-23 07:45:40 +0000 | [diff] [blame] | 4108 | |
t00fcxen | 08f9ff9 | 2014-03-16 13:38:54 +0000 | [diff] [blame] | 4109 | op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, ""); |
Michael Tuexen | 2292a60 | 2022-04-07 17:49:53 +0200 | [diff] [blame] | 4110 | stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_7; |
| 4111 | sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4112 | SCTP_STAT_INCR_COUNTER32(sctps_aborted); |
Michael Tuexen | 2292a60 | 2022-04-07 17:49:53 +0200 | [diff] [blame] | 4113 | } else if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4114 | cnt++; |
Michael Tuexen | 2292a60 | 2022-04-07 17:49:53 +0200 | [diff] [blame] | 4115 | SCTP_TCB_UNLOCK(stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4116 | continue; |
| 4117 | } |
Michael Tuexen | 2292a60 | 2022-04-07 17:49:53 +0200 | [diff] [blame] | 4118 | if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) || |
| 4119 | (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4120 | SCTP_STAT_DECR_GAUGE32(sctps_currestab); |
| 4121 | } |
Michael Tuexen | 2292a60 | 2022-04-07 17:49:53 +0200 | [diff] [blame] | 4122 | if (sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE, |
t00fcxen | 0057a6d | 2015-05-28 16:42:49 +0000 | [diff] [blame] | 4123 | SCTP_FROM_SCTP_PCB + SCTP_LOC_8) == 0) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4124 | cnt++; |
| 4125 | } |
| 4126 | } |
| 4127 | if (cnt) { |
| 4128 | /* Ok we have someone out there that will kill us */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4129 | #ifdef SCTP_LOG_CLOSING |
| 4130 | sctp_log_closing(inp, NULL, 3); |
| 4131 | #endif |
| 4132 | SCTP_INP_WUNLOCK(inp); |
| 4133 | SCTP_ASOC_CREATE_UNLOCK(inp); |
| 4134 | SCTP_INP_INFO_WUNLOCK(); |
| 4135 | return; |
| 4136 | } |
| 4137 | if (SCTP_INP_LOCK_CONTENDED(inp)) |
| 4138 | being_refed++; |
| 4139 | if (SCTP_INP_READ_CONTENDED(inp)) |
| 4140 | being_refed++; |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 4141 | if (SCTP_ASOC_CREATE_LOCK_CONTENDED(inp)) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4142 | being_refed++; |
Michael Tuexen | 939d48f | 2020-07-19 14:44:43 +0200 | [diff] [blame] | 4143 | /* NOTE: 0 refcount also means no timers are referencing us. */ |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 4144 | if ((inp->refcount) || |
t00fcxen | 28611aa | 2012-09-23 07:45:40 +0000 | [diff] [blame] | 4145 | (being_refed) || |
| 4146 | (inp->sctp_flags & SCTP_PCB_FLAGS_CLOSE_IP)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4147 | #ifdef SCTP_LOG_CLOSING |
| 4148 | sctp_log_closing(inp, NULL, 4); |
| 4149 | #endif |
| 4150 | sctp_timer_start(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL); |
| 4151 | SCTP_INP_WUNLOCK(inp); |
| 4152 | SCTP_ASOC_CREATE_UNLOCK(inp); |
| 4153 | SCTP_INP_INFO_WUNLOCK(); |
| 4154 | return; |
| 4155 | } |
| 4156 | inp->sctp_ep.signature_change.type = 0; |
| 4157 | inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_ALLGONE; |
| 4158 | /* Remove it from the list .. last thing we need a |
| 4159 | * lock for. |
| 4160 | */ |
| 4161 | LIST_REMOVE(inp, sctp_list); |
| 4162 | SCTP_INP_WUNLOCK(inp); |
| 4163 | SCTP_ASOC_CREATE_UNLOCK(inp); |
| 4164 | SCTP_INP_INFO_WUNLOCK(); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4165 | |
| 4166 | #ifdef SCTP_LOG_CLOSING |
| 4167 | sctp_log_closing(inp, NULL, 5); |
| 4168 | #endif |
Michael Tuexen | eb61157 | 2020-06-13 17:27:02 +0200 | [diff] [blame] | 4169 | #if !(defined(_WIN32) || defined(__Userspace__)) |
Michael Tuexen | 8d6935d | 2022-05-15 01:51:24 +0200 | [diff] [blame] | 4170 | #if !(defined(__FreeBSD__) && !defined(__Userspace__)) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4171 | rt = ip_pcb->inp_route.ro_rt; |
| 4172 | #endif |
| 4173 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4174 | if ((inp->sctp_asocidhash) != NULL) { |
| 4175 | SCTP_HASH_FREE(inp->sctp_asocidhash, inp->hashasocidmark); |
| 4176 | inp->sctp_asocidhash = NULL; |
| 4177 | } |
| 4178 | /*sa_ignore FREED_MEMORY*/ |
| 4179 | TAILQ_FOREACH_SAFE(sq, &inp->read_queue, next, nsq) { |
| 4180 | /* Its only abandoned if it had data left */ |
| 4181 | if (sq->length) |
| 4182 | SCTP_STAT_INCR(sctps_left_abandon); |
| 4183 | |
| 4184 | TAILQ_REMOVE(&inp->read_queue, sq, next); |
| 4185 | sctp_free_remote_addr(sq->whoFrom); |
| 4186 | if (so) |
| 4187 | so->so_rcv.sb_cc -= sq->length; |
| 4188 | if (sq->data) { |
| 4189 | sctp_m_freem(sq->data); |
| 4190 | sq->data = NULL; |
| 4191 | } |
| 4192 | /* |
| 4193 | * no need to free the net count, since at this point all |
| 4194 | * assoc's are gone. |
| 4195 | */ |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 4196 | sctp_free_a_readq(NULL, sq); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4197 | } |
| 4198 | /* Now the sctp_pcb things */ |
| 4199 | /* |
| 4200 | * free each asoc if it is not already closed/free. we can't use the |
| 4201 | * macro here since le_next will get freed as part of the |
| 4202 | * sctp_free_assoc() call. |
| 4203 | */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4204 | if (ip_pcb->inp_options) { |
| 4205 | (void)sctp_m_free(ip_pcb->inp_options); |
| 4206 | ip_pcb->inp_options = 0; |
| 4207 | } |
Michael Tuexen | eb61157 | 2020-06-13 17:27:02 +0200 | [diff] [blame] | 4208 | #if !(defined(_WIN32) || defined(__Userspace__)) |
Michael Tuexen | a508995 | 2020-06-09 23:11:08 +0200 | [diff] [blame] | 4209 | #if !defined(__FreeBSD__) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4210 | if (rt) { |
| 4211 | RTFREE(rt); |
| 4212 | ip_pcb->inp_route.ro_rt = 0; |
| 4213 | } |
| 4214 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4215 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4216 | #ifdef INET6 |
Michael Tuexen | eb61157 | 2020-06-13 17:27:02 +0200 | [diff] [blame] | 4217 | #if !(defined(_WIN32) || defined(__Userspace__)) |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 4218 | #if (defined(__FreeBSD__) || defined(__APPLE__) && !defined(__Userspace__)) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4219 | if (ip_pcb->inp_vflag & INP_IPV6) { |
t00fcxen | 4c76a91 | 2012-09-14 14:31:21 +0000 | [diff] [blame] | 4220 | #else |
| 4221 | if (inp->inp_vflag & INP_IPV6) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4222 | #endif |
Michael Tuexen | 801a64b | 2019-08-05 15:33:37 +0200 | [diff] [blame] | 4223 | ip6_freepcbopts(ip_pcb->in6p_outputopts); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4224 | } |
t00fcxen | 4c76a91 | 2012-09-14 14:31:21 +0000 | [diff] [blame] | 4225 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4226 | #endif /* INET6 */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4227 | ip_pcb->inp_vflag = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4228 | /* free up authentication fields */ |
| 4229 | if (inp->sctp_ep.local_auth_chunks != NULL) |
| 4230 | sctp_free_chunklist(inp->sctp_ep.local_auth_chunks); |
| 4231 | if (inp->sctp_ep.local_hmacs != NULL) |
| 4232 | sctp_free_hmaclist(inp->sctp_ep.local_hmacs); |
| 4233 | |
| 4234 | LIST_FOREACH_SAFE(shared_key, &inp->sctp_ep.shared_keys, next, nshared_key) { |
| 4235 | LIST_REMOVE(shared_key, next); |
| 4236 | sctp_free_sharedkey(shared_key); |
| 4237 | /*sa_ignore FREED_MEMORY*/ |
| 4238 | } |
| 4239 | |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 4240 | #if defined(__APPLE__) && !defined(__Userspace__) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4241 | inp->ip_inp.inp.inp_state = INPCB_STATE_DEAD; |
tuexen | 6d01b94 | 2012-05-04 18:15:17 +0000 | [diff] [blame] | 4242 | if (in_pcb_checkstate(&inp->ip_inp.inp, WNT_STOPUSING, 1) != WNT_STOPUSING) { |
tuexen | 49fdcc8 | 2012-05-04 10:13:12 +0000 | [diff] [blame] | 4243 | #ifdef INVARIANTS |
Michael Tuexen | e19d122 | 2022-08-03 17:35:14 +0200 | [diff] [blame^] | 4244 | panic("sctp_inpcb_free inp = %p couldn't set to STOPUSING", (void *)inp); |
tuexen | 6d01b94 | 2012-05-04 18:15:17 +0000 | [diff] [blame] | 4245 | #else |
t00fcxen | 8fca7ff | 2012-09-05 18:58:42 +0000 | [diff] [blame] | 4246 | SCTP_PRINTF("sctp_inpcb_free inp = %p couldn't set to STOPUSING\n", (void *)inp); |
tuexen | 49fdcc8 | 2012-05-04 10:13:12 +0000 | [diff] [blame] | 4247 | #endif |
tuexen | 6d01b94 | 2012-05-04 18:15:17 +0000 | [diff] [blame] | 4248 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4249 | inp->ip_inp.inp.inp_socket->so_flags |= SOF_PCBCLEARING; |
| 4250 | #endif |
| 4251 | /* |
| 4252 | * if we have an address list the following will free the list of |
| 4253 | * ifaddr's that are set into this ep. Again macro limitations here, |
| 4254 | * since the LIST_FOREACH could be a bad idea. |
| 4255 | */ |
| 4256 | LIST_FOREACH_SAFE(laddr, &inp->sctp_addr_list, sctp_nxt_addr, nladdr) { |
| 4257 | sctp_remove_laddr(laddr); |
| 4258 | } |
| 4259 | |
| 4260 | #ifdef SCTP_TRACK_FREED_ASOCS |
| 4261 | /* TEMP CODE */ |
Michael Tuexen | 2292a60 | 2022-04-07 17:49:53 +0200 | [diff] [blame] | 4262 | LIST_FOREACH_SAFE(stcb, &inp->sctp_asoc_free_list, sctp_tcblist, nstcb) { |
| 4263 | LIST_REMOVE(stcb, sctp_tcblist); |
| 4264 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4265 | SCTP_DECR_ASOC_COUNT(); |
| 4266 | } |
| 4267 | /* *** END TEMP CODE ****/ |
| 4268 | #endif |
| 4269 | #ifdef SCTP_MVRF |
| 4270 | SCTP_FREE(inp->m_vrf_ids, SCTP_M_MVRF); |
| 4271 | #endif |
| 4272 | /* Now lets see about freeing the EP hash table. */ |
| 4273 | if (inp->sctp_tcbhash != NULL) { |
| 4274 | SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark); |
| 4275 | inp->sctp_tcbhash = NULL; |
| 4276 | } |
| 4277 | /* Now we must put the ep memory back into the zone pool */ |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 4278 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 4279 | crfree(inp->ip_inp.inp.inp_cred); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4280 | INP_LOCK_DESTROY(&inp->ip_inp.inp); |
| 4281 | #endif |
| 4282 | SCTP_INP_LOCK_DESTROY(inp); |
| 4283 | SCTP_INP_READ_DESTROY(inp); |
| 4284 | SCTP_ASOC_CREATE_LOCK_DESTROY(inp); |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 4285 | #if !(defined(__APPLE__) && !defined(__Userspace__)) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4286 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp); |
| 4287 | SCTP_DECR_EP_COUNT(); |
| 4288 | #else |
| 4289 | /* For Tiger, we will do this later... */ |
| 4290 | #endif |
| 4291 | } |
| 4292 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4293 | struct sctp_nets * |
| 4294 | sctp_findnet(struct sctp_tcb *stcb, struct sockaddr *addr) |
| 4295 | { |
| 4296 | struct sctp_nets *net; |
| 4297 | /* locate the address */ |
| 4298 | TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { |
| 4299 | if (sctp_cmpaddr(addr, (struct sockaddr *)&net->ro._l_addr)) |
| 4300 | return (net); |
| 4301 | } |
| 4302 | return (NULL); |
| 4303 | } |
| 4304 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4305 | int |
| 4306 | sctp_is_address_on_local_host(struct sockaddr *addr, uint32_t vrf_id) |
| 4307 | { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4308 | struct sctp_ifa *sctp_ifa; |
| 4309 | sctp_ifa = sctp_find_ifa_by_addr(addr, vrf_id, SCTP_ADDR_NOT_LOCKED); |
| 4310 | if (sctp_ifa) { |
| 4311 | return (1); |
| 4312 | } else { |
| 4313 | return (0); |
| 4314 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4315 | } |
| 4316 | |
| 4317 | /* |
| 4318 | * add's a remote endpoint address, done with the INIT/INIT-ACK as well as |
| 4319 | * when a ASCONF arrives that adds it. It will also initialize all the cwnd |
| 4320 | * stats of stuff. |
| 4321 | */ |
| 4322 | int |
| 4323 | sctp_add_remote_addr(struct sctp_tcb *stcb, struct sockaddr *newaddr, |
Michael Tuexen | 2757442 | 2016-04-30 16:35:29 +0200 | [diff] [blame] | 4324 | struct sctp_nets **netp, uint16_t port, int set_scope, int from) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4325 | { |
| 4326 | /* |
| 4327 | * The following is redundant to the same lines in the |
| 4328 | * sctp_aloc_assoc() but is needed since others call the add |
| 4329 | * address function |
| 4330 | */ |
| 4331 | struct sctp_nets *net, *netfirst; |
| 4332 | int addr_inscope; |
| 4333 | |
| 4334 | SCTPDBG(SCTP_DEBUG_PCB1, "Adding an address (from:%d) to the peer: ", |
| 4335 | from); |
| 4336 | SCTPDBG_ADDR(SCTP_DEBUG_PCB1, newaddr); |
| 4337 | |
| 4338 | netfirst = sctp_findnet(stcb, newaddr); |
| 4339 | if (netfirst) { |
| 4340 | /* |
| 4341 | * Lie and return ok, we don't want to make the association |
| 4342 | * go away for this behavior. It will happen in the TCP |
| 4343 | * model in a connected socket. It does not reach the hash |
| 4344 | * table until after the association is built so it can't be |
| 4345 | * found. Mark as reachable, since the initial creation will |
| 4346 | * have been cleared and the NOT_IN_ASSOC flag will have |
| 4347 | * been added... and we don't want to end up removing it |
| 4348 | * back out. |
| 4349 | */ |
| 4350 | if (netfirst->dest_state & SCTP_ADDR_UNCONFIRMED) { |
| 4351 | netfirst->dest_state = (SCTP_ADDR_REACHABLE | |
| 4352 | SCTP_ADDR_UNCONFIRMED); |
| 4353 | } else { |
| 4354 | netfirst->dest_state = SCTP_ADDR_REACHABLE; |
| 4355 | } |
| 4356 | |
| 4357 | return (0); |
| 4358 | } |
| 4359 | addr_inscope = 1; |
| 4360 | switch (newaddr->sa_family) { |
| 4361 | #ifdef INET |
| 4362 | case AF_INET: |
| 4363 | { |
| 4364 | struct sockaddr_in *sin; |
| 4365 | |
| 4366 | sin = (struct sockaddr_in *)newaddr; |
| 4367 | if (sin->sin_addr.s_addr == 0) { |
| 4368 | /* Invalid address */ |
| 4369 | return (-1); |
| 4370 | } |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 4371 | /* zero out the zero area */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4372 | memset(&sin->sin_zero, 0, sizeof(sin->sin_zero)); |
| 4373 | |
| 4374 | /* assure len is set */ |
t00fcxen | 8fbaee3 | 2012-09-04 16:41:39 +0000 | [diff] [blame] | 4375 | #ifdef HAVE_SIN_LEN |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4376 | sin->sin_len = sizeof(struct sockaddr_in); |
| 4377 | #endif |
| 4378 | if (set_scope) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4379 | if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) { |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 4380 | stcb->asoc.scope.ipv4_local_scope = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4381 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4382 | } else { |
| 4383 | /* Validate the address is in scope */ |
| 4384 | if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) && |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 4385 | (stcb->asoc.scope.ipv4_local_scope == 0)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4386 | addr_inscope = 0; |
| 4387 | } |
| 4388 | } |
| 4389 | break; |
| 4390 | } |
| 4391 | #endif |
| 4392 | #ifdef INET6 |
| 4393 | case AF_INET6: |
| 4394 | { |
| 4395 | struct sockaddr_in6 *sin6; |
| 4396 | |
| 4397 | sin6 = (struct sockaddr_in6 *)newaddr; |
| 4398 | if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { |
| 4399 | /* Invalid address */ |
| 4400 | return (-1); |
| 4401 | } |
| 4402 | /* assure len is set */ |
t00fcxen | 8fbaee3 | 2012-09-04 16:41:39 +0000 | [diff] [blame] | 4403 | #ifdef HAVE_SIN6_LEN |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4404 | sin6->sin6_len = sizeof(struct sockaddr_in6); |
| 4405 | #endif |
| 4406 | if (set_scope) { |
| 4407 | if (sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id)) { |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 4408 | stcb->asoc.scope.loopback_scope = 1; |
| 4409 | stcb->asoc.scope.local_scope = 0; |
| 4410 | stcb->asoc.scope.ipv4_local_scope = 1; |
| 4411 | stcb->asoc.scope.site_scope = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4412 | } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { |
| 4413 | /* |
| 4414 | * If the new destination is a LINK_LOCAL we |
| 4415 | * must have common site scope. Don't set |
| 4416 | * the local scope since we may not share |
| 4417 | * all links, only loopback can do this. |
| 4418 | * Links on the local network would also be |
| 4419 | * on our private network for v4 too. |
| 4420 | */ |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 4421 | stcb->asoc.scope.ipv4_local_scope = 1; |
| 4422 | stcb->asoc.scope.site_scope = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4423 | } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) { |
| 4424 | /* |
| 4425 | * If the new destination is SITE_LOCAL then |
| 4426 | * we must have site scope in common. |
| 4427 | */ |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 4428 | stcb->asoc.scope.site_scope = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4429 | } |
| 4430 | } else { |
| 4431 | /* Validate the address is in scope */ |
| 4432 | if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) && |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 4433 | (stcb->asoc.scope.loopback_scope == 0)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4434 | addr_inscope = 0; |
| 4435 | } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) && |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 4436 | (stcb->asoc.scope.local_scope == 0)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4437 | addr_inscope = 0; |
| 4438 | } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) && |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 4439 | (stcb->asoc.scope.site_scope == 0)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4440 | addr_inscope = 0; |
| 4441 | } |
| 4442 | } |
| 4443 | break; |
| 4444 | } |
| 4445 | #endif |
tuexen | cc2b142 | 2012-07-10 21:50:32 +0000 | [diff] [blame] | 4446 | #if defined(__Userspace__) |
| 4447 | case AF_CONN: |
| 4448 | { |
| 4449 | struct sockaddr_conn *sconn; |
| 4450 | |
| 4451 | sconn = (struct sockaddr_conn *)newaddr; |
| 4452 | if (sconn->sconn_addr == NULL) { |
| 4453 | /* Invalid address */ |
| 4454 | return (-1); |
| 4455 | } |
t00fcxen | 8fbaee3 | 2012-09-04 16:41:39 +0000 | [diff] [blame] | 4456 | #ifdef HAVE_SCONN_LEN |
tuexen | cc2b142 | 2012-07-10 21:50:32 +0000 | [diff] [blame] | 4457 | sconn->sconn_len = sizeof(struct sockaddr_conn); |
| 4458 | #endif |
| 4459 | break; |
| 4460 | } |
| 4461 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4462 | default: |
| 4463 | /* not supported family type */ |
| 4464 | return (-1); |
| 4465 | } |
| 4466 | net = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_net), struct sctp_nets); |
| 4467 | if (net == NULL) { |
| 4468 | return (-1); |
| 4469 | } |
| 4470 | SCTP_INCR_RADDR_COUNT(); |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 4471 | memset(net, 0, sizeof(struct sctp_nets)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4472 | (void)SCTP_GETTIME_TIMEVAL(&net->start_time); |
t00fcxen | 907930b | 2012-09-12 19:43:22 +0000 | [diff] [blame] | 4473 | #ifdef HAVE_SA_LEN |
| 4474 | memcpy(&net->ro._l_addr, newaddr, newaddr->sa_len); |
| 4475 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4476 | switch (newaddr->sa_family) { |
| 4477 | #ifdef INET |
| 4478 | case AF_INET: |
t00fcxen | 907930b | 2012-09-12 19:43:22 +0000 | [diff] [blame] | 4479 | #ifndef HAVE_SA_LEN |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4480 | memcpy(&net->ro._l_addr, newaddr, sizeof(struct sockaddr_in)); |
| 4481 | #endif |
| 4482 | ((struct sockaddr_in *)&net->ro._l_addr)->sin_port = stcb->rport; |
| 4483 | break; |
| 4484 | #endif |
| 4485 | #ifdef INET6 |
| 4486 | case AF_INET6: |
t00fcxen | 907930b | 2012-09-12 19:43:22 +0000 | [diff] [blame] | 4487 | #ifndef HAVE_SA_LEN |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4488 | memcpy(&net->ro._l_addr, newaddr, sizeof(struct sockaddr_in6)); |
| 4489 | #endif |
| 4490 | ((struct sockaddr_in6 *)&net->ro._l_addr)->sin6_port = stcb->rport; |
| 4491 | break; |
| 4492 | #endif |
tuexen | cc2b142 | 2012-07-10 21:50:32 +0000 | [diff] [blame] | 4493 | #if defined(__Userspace__) |
| 4494 | case AF_CONN: |
t00fcxen | 907930b | 2012-09-12 19:43:22 +0000 | [diff] [blame] | 4495 | #ifndef HAVE_SA_LEN |
tuexen | cc2b142 | 2012-07-10 21:50:32 +0000 | [diff] [blame] | 4496 | memcpy(&net->ro._l_addr, newaddr, sizeof(struct sockaddr_conn)); |
| 4497 | #endif |
| 4498 | ((struct sockaddr_conn *)&net->ro._l_addr)->sconn_port = stcb->rport; |
| 4499 | break; |
| 4500 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4501 | default: |
| 4502 | break; |
| 4503 | } |
| 4504 | net->addr_is_local = sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id); |
| 4505 | if (net->addr_is_local && ((set_scope || (from == SCTP_ADDR_IS_CONFIRMED)))) { |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 4506 | stcb->asoc.scope.loopback_scope = 1; |
| 4507 | stcb->asoc.scope.ipv4_local_scope = 1; |
| 4508 | stcb->asoc.scope.local_scope = 0; |
| 4509 | stcb->asoc.scope.site_scope = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4510 | addr_inscope = 1; |
| 4511 | } |
| 4512 | net->failure_threshold = stcb->asoc.def_net_failure; |
| 4513 | net->pf_threshold = stcb->asoc.def_net_pf_threshold; |
| 4514 | if (addr_inscope == 0) { |
| 4515 | net->dest_state = (SCTP_ADDR_REACHABLE | |
| 4516 | SCTP_ADDR_OUT_OF_SCOPE); |
| 4517 | } else { |
| 4518 | if (from == SCTP_ADDR_IS_CONFIRMED) |
| 4519 | /* SCTP_ADDR_IS_CONFIRMED is passed by connect_x */ |
| 4520 | net->dest_state = SCTP_ADDR_REACHABLE; |
| 4521 | else |
| 4522 | net->dest_state = SCTP_ADDR_REACHABLE | |
| 4523 | SCTP_ADDR_UNCONFIRMED; |
| 4524 | } |
| 4525 | /* We set this to 0, the timer code knows that |
| 4526 | * this means its an initial value |
| 4527 | */ |
| 4528 | net->rto_needed = 1; |
t00fcxen | 8dd95b8 | 2014-07-11 20:41:28 +0000 | [diff] [blame] | 4529 | net->RTO = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4530 | net->RTO_measured = 0; |
| 4531 | stcb->asoc.numnets++; |
tuexen | 9d7b207 | 2011-11-20 16:35:17 +0000 | [diff] [blame] | 4532 | net->ref_count = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4533 | net->cwr_window_tsn = net->last_cwr_tsn = stcb->asoc.sending_seq - 1; |
Michael Tuexen | 2757442 | 2016-04-30 16:35:29 +0200 | [diff] [blame] | 4534 | net->port = port; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4535 | net->dscp = stcb->asoc.default_dscp; |
| 4536 | #ifdef INET6 |
| 4537 | net->flowlabel = stcb->asoc.default_flowlabel; |
| 4538 | #endif |
| 4539 | if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) { |
| 4540 | net->dest_state |= SCTP_ADDR_NOHB; |
| 4541 | } else { |
| 4542 | net->dest_state &= ~SCTP_ADDR_NOHB; |
| 4543 | } |
| 4544 | if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD)) { |
| 4545 | net->dest_state |= SCTP_ADDR_NO_PMTUD; |
| 4546 | } else { |
| 4547 | net->dest_state &= ~SCTP_ADDR_NO_PMTUD; |
| 4548 | } |
| 4549 | net->heart_beat_delay = stcb->asoc.heart_beat_delay; |
| 4550 | /* Init the timer structure */ |
| 4551 | SCTP_OS_TIMER_INIT(&net->rxt_timer.timer); |
| 4552 | SCTP_OS_TIMER_INIT(&net->pmtu_timer.timer); |
| 4553 | SCTP_OS_TIMER_INIT(&net->hb_timer.timer); |
| 4554 | |
| 4555 | /* Now generate a route for this guy */ |
| 4556 | #ifdef INET6 |
| 4557 | #ifdef SCTP_EMBEDDED_V6_SCOPE |
| 4558 | /* KAME hack: embed scopeid */ |
| 4559 | if (newaddr->sa_family == AF_INET6) { |
| 4560 | struct sockaddr_in6 *sin6; |
| 4561 | |
| 4562 | sin6 = (struct sockaddr_in6 *)&net->ro._l_addr; |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 4563 | #if defined(__APPLE__) && !defined(__Userspace__) |
tuexen | 9a218b1 | 2012-08-04 21:17:58 +0000 | [diff] [blame] | 4564 | #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) |
| 4565 | (void)in6_embedscope(&sin6->sin6_addr, sin6, &stcb->sctp_ep->ip_inp.inp, NULL); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4566 | #else |
tuexen | 9a218b1 | 2012-08-04 21:17:58 +0000 | [diff] [blame] | 4567 | (void)in6_embedscope(&sin6->sin6_addr, sin6, &stcb->sctp_ep->ip_inp.inp, NULL, NULL); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4568 | #endif |
| 4569 | #elif defined(SCTP_KAME) |
| 4570 | (void)sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)); |
| 4571 | #else |
| 4572 | (void)in6_embedscope(&sin6->sin6_addr, sin6); |
| 4573 | #endif |
| 4574 | #ifndef SCOPEDROUTING |
| 4575 | sin6->sin6_scope_id = 0; |
| 4576 | #endif |
| 4577 | } |
| 4578 | #endif /* SCTP_EMBEDDED_V6_SCOPE */ |
| 4579 | #endif |
t00fcxen | a51e446 | 2015-06-17 15:53:23 +0000 | [diff] [blame] | 4580 | SCTP_RTALLOC((sctp_route_t *)&net->ro, |
| 4581 | stcb->asoc.vrf_id, |
| 4582 | stcb->sctp_ep->fibnum); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4583 | |
t00fcxen | 8b6ef09 | 2014-07-11 17:37:40 +0000 | [diff] [blame] | 4584 | net->src_addr_selected = 0; |
Michael Tuexen | d900e5f | 2017-06-23 10:52:43 +0200 | [diff] [blame] | 4585 | #if !defined(__Userspace__) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4586 | if (SCTP_ROUTE_HAS_VALID_IFN(&net->ro)) { |
| 4587 | /* Get source address */ |
| 4588 | net->ro._s_addr = sctp_source_address_selection(stcb->sctp_ep, |
Michael Tuexen | d900e5f | 2017-06-23 10:52:43 +0200 | [diff] [blame] | 4589 | stcb, |
| 4590 | (sctp_route_t *)&net->ro, |
| 4591 | net, |
| 4592 | 0, |
| 4593 | stcb->asoc.vrf_id); |
Michael Tuexen | de0630d | 2017-11-03 22:04:01 +0100 | [diff] [blame] | 4594 | if (stcb->asoc.default_mtu > 0) { |
| 4595 | net->mtu = stcb->asoc.default_mtu; |
| 4596 | switch (net->ro._l_addr.sa.sa_family) { |
| 4597 | #ifdef INET |
| 4598 | case AF_INET: |
| 4599 | net->mtu += SCTP_MIN_V4_OVERHEAD; |
| 4600 | break; |
| 4601 | #endif |
| 4602 | #ifdef INET6 |
| 4603 | case AF_INET6: |
| 4604 | net->mtu += SCTP_MIN_OVERHEAD; |
| 4605 | break; |
| 4606 | #endif |
| 4607 | #if defined(__Userspace__) |
| 4608 | case AF_CONN: |
| 4609 | net->mtu += sizeof(struct sctphdr); |
| 4610 | break; |
| 4611 | #endif |
| 4612 | default: |
| 4613 | break; |
| 4614 | } |
| 4615 | #if defined(INET) || defined(INET6) |
| 4616 | if (net->port) { |
| 4617 | net->mtu += (uint32_t)sizeof(struct udphdr); |
| 4618 | } |
| 4619 | #endif |
| 4620 | } else if (net->ro._s_addr != NULL) { |
Michael Tuexen | d900e5f | 2017-06-23 10:52:43 +0200 | [diff] [blame] | 4621 | uint32_t imtu, rmtu, hcmtu; |
| 4622 | |
t00fcxen | 8b6ef09 | 2014-07-11 17:37:40 +0000 | [diff] [blame] | 4623 | net->src_addr_selected = 1; |
| 4624 | /* Now get the interface MTU */ |
| 4625 | if (net->ro._s_addr->ifn_p != NULL) { |
Michael Tuexen | b29c220 | 2022-02-18 17:32:29 +0100 | [diff] [blame] | 4626 | /* |
| 4627 | * XXX: Should we here just use |
| 4628 | * net->ro._s_addr->ifn_p->ifn_mtu |
| 4629 | */ |
| 4630 | imtu = SCTP_GATHER_MTU_FROM_IFN_INFO(net->ro._s_addr->ifn_p->ifn_p, |
| 4631 | net->ro._s_addr->ifn_p->ifn_index); |
Michael Tuexen | d900e5f | 2017-06-23 10:52:43 +0200 | [diff] [blame] | 4632 | } else { |
| 4633 | imtu = 0; |
t00fcxen | 8b6ef09 | 2014-07-11 17:37:40 +0000 | [diff] [blame] | 4634 | } |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 4635 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4636 | rmtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, net->ro.ro_nh); |
Michael Tuexen | d900e5f | 2017-06-23 10:52:43 +0200 | [diff] [blame] | 4637 | hcmtu = sctp_hc_get_mtu(&net->ro._l_addr, stcb->sctp_ep->fibnum); |
| 4638 | #else |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4639 | rmtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, net->ro.ro_rt); |
Michael Tuexen | d900e5f | 2017-06-23 10:52:43 +0200 | [diff] [blame] | 4640 | hcmtu = 0; |
| 4641 | #endif |
| 4642 | net->mtu = sctp_min_mtu(hcmtu, rmtu, imtu); |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 4643 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4644 | #else |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4645 | if (rmtu == 0) { |
| 4646 | /* Start things off to match mtu of interface please. */ |
| 4647 | SCTP_SET_MTU_OF_ROUTE(&net->ro._l_addr.sa, |
Michael Tuexen | d900e5f | 2017-06-23 10:52:43 +0200 | [diff] [blame] | 4648 | net->ro.ro_rt, net->mtu); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4649 | } |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4650 | #endif |
t00fcxen | 8b6ef09 | 2014-07-11 17:37:40 +0000 | [diff] [blame] | 4651 | } |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 4652 | } |
t00fcxen | 6d60eb0 | 2013-08-30 07:37:01 +0000 | [diff] [blame] | 4653 | #endif |
tuexen | e81177b | 2011-11-15 20:48:08 +0000 | [diff] [blame] | 4654 | if (net->mtu == 0) { |
Michael Tuexen | de0630d | 2017-11-03 22:04:01 +0100 | [diff] [blame] | 4655 | if (stcb->asoc.default_mtu > 0) { |
| 4656 | net->mtu = stcb->asoc.default_mtu; |
| 4657 | switch (net->ro._l_addr.sa.sa_family) { |
tuexen | e81177b | 2011-11-15 20:48:08 +0000 | [diff] [blame] | 4658 | #ifdef INET |
Michael Tuexen | de0630d | 2017-11-03 22:04:01 +0100 | [diff] [blame] | 4659 | case AF_INET: |
| 4660 | net->mtu += SCTP_MIN_V4_OVERHEAD; |
| 4661 | break; |
tuexen | e81177b | 2011-11-15 20:48:08 +0000 | [diff] [blame] | 4662 | #endif |
| 4663 | #ifdef INET6 |
Michael Tuexen | de0630d | 2017-11-03 22:04:01 +0100 | [diff] [blame] | 4664 | case AF_INET6: |
| 4665 | net->mtu += SCTP_MIN_OVERHEAD; |
| 4666 | break; |
tuexen | e81177b | 2011-11-15 20:48:08 +0000 | [diff] [blame] | 4667 | #endif |
tuexen | cc2b142 | 2012-07-10 21:50:32 +0000 | [diff] [blame] | 4668 | #if defined(__Userspace__) |
Michael Tuexen | de0630d | 2017-11-03 22:04:01 +0100 | [diff] [blame] | 4669 | case AF_CONN: |
| 4670 | net->mtu += sizeof(struct sctphdr); |
| 4671 | break; |
tuexen | cc2b142 | 2012-07-10 21:50:32 +0000 | [diff] [blame] | 4672 | #endif |
Michael Tuexen | de0630d | 2017-11-03 22:04:01 +0100 | [diff] [blame] | 4673 | default: |
| 4674 | break; |
| 4675 | } |
| 4676 | #if defined(INET) || defined(INET6) |
| 4677 | if (net->port) { |
| 4678 | net->mtu += (uint32_t)sizeof(struct udphdr); |
| 4679 | } |
| 4680 | #endif |
| 4681 | } else { |
| 4682 | switch (newaddr->sa_family) { |
| 4683 | #ifdef INET |
| 4684 | case AF_INET: |
| 4685 | net->mtu = SCTP_DEFAULT_MTU; |
| 4686 | break; |
| 4687 | #endif |
| 4688 | #ifdef INET6 |
| 4689 | case AF_INET6: |
| 4690 | net->mtu = 1280; |
| 4691 | break; |
| 4692 | #endif |
| 4693 | #if defined(__Userspace__) |
| 4694 | case AF_CONN: |
| 4695 | net->mtu = 1280; |
| 4696 | break; |
| 4697 | #endif |
| 4698 | default: |
| 4699 | break; |
| 4700 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4701 | } |
tuexen | e81177b | 2011-11-15 20:48:08 +0000 | [diff] [blame] | 4702 | } |
t00fcxen | 1ce83bb | 2014-07-11 07:17:36 +0000 | [diff] [blame] | 4703 | #if defined(INET) || defined(INET6) |
tuexen | e81177b | 2011-11-15 20:48:08 +0000 | [diff] [blame] | 4704 | if (net->port) { |
| 4705 | net->mtu -= (uint32_t)sizeof(struct udphdr); |
| 4706 | } |
t00fcxen | 5ab37c8 | 2014-06-30 20:55:44 +0000 | [diff] [blame] | 4707 | #endif |
tuexen | e81177b | 2011-11-15 20:48:08 +0000 | [diff] [blame] | 4708 | if (from == SCTP_ALLOC_ASOC) { |
| 4709 | stcb->asoc.smallest_mtu = net->mtu; |
| 4710 | } |
| 4711 | if (stcb->asoc.smallest_mtu > net->mtu) { |
Michael Tuexen | a40c76b | 2021-12-30 15:53:03 +0100 | [diff] [blame] | 4712 | sctp_pathmtu_adjustment(stcb, net->mtu, true); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4713 | } |
| 4714 | #ifdef INET6 |
| 4715 | #ifdef SCTP_EMBEDDED_V6_SCOPE |
| 4716 | if (newaddr->sa_family == AF_INET6) { |
| 4717 | struct sockaddr_in6 *sin6; |
| 4718 | |
| 4719 | sin6 = (struct sockaddr_in6 *)&net->ro._l_addr; |
| 4720 | #ifdef SCTP_KAME |
| 4721 | (void)sa6_recoverscope(sin6); |
| 4722 | #else |
| 4723 | (void)in6_recoverscope(sin6, &sin6->sin6_addr, NULL); |
| 4724 | #endif /* SCTP_KAME */ |
| 4725 | } |
| 4726 | #endif /* SCTP_EMBEDDED_V6_SCOPE */ |
| 4727 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4728 | |
| 4729 | /* JRS - Use the congestion control given in the CC module */ |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 4730 | if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4731 | (*stcb->asoc.cc_functions.sctp_set_initial_cc_param)(stcb, net); |
| 4732 | |
| 4733 | /* |
| 4734 | * CMT: CUC algo - set find_pseudo_cumack to TRUE (1) at beginning |
| 4735 | * of assoc (2005/06/27, iyengar@cis.udel.edu) |
| 4736 | */ |
| 4737 | net->find_pseudo_cumack = 1; |
| 4738 | net->find_rtx_pseudo_cumack = 1; |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 4739 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4740 | /* Choose an initial flowid. */ |
| 4741 | net->flowid = stcb->asoc.my_vtag ^ |
| 4742 | ntohs(stcb->rport) ^ |
| 4743 | ntohs(stcb->sctp_ep->sctp_lport); |
Michael Tuexen | 15f35f8 | 2016-06-07 09:19:50 +0200 | [diff] [blame] | 4744 | net->flowtype = M_HASHTYPE_OPAQUE_HASH; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4745 | #endif |
| 4746 | if (netp) { |
| 4747 | *netp = net; |
| 4748 | } |
| 4749 | netfirst = TAILQ_FIRST(&stcb->asoc.nets); |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 4750 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4751 | if (net->ro.ro_nh == NULL) { |
| 4752 | #else |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4753 | if (net->ro.ro_rt == NULL) { |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4754 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4755 | /* Since we have no route put it at the back */ |
| 4756 | TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next); |
| 4757 | } else if (netfirst == NULL) { |
| 4758 | /* We are the first one in the pool. */ |
| 4759 | TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next); |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 4760 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4761 | } else if (netfirst->ro.ro_nh == NULL) { |
| 4762 | #else |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4763 | } else if (netfirst->ro.ro_rt == NULL) { |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4764 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4765 | /* |
| 4766 | * First one has NO route. Place this one ahead of the first |
| 4767 | * one. |
| 4768 | */ |
| 4769 | TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next); |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 4770 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4771 | } else if (net->ro.ro_nh->nh_ifp != netfirst->ro.ro_nh->nh_ifp) { |
| 4772 | #else |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4773 | } else if (net->ro.ro_rt->rt_ifp != netfirst->ro.ro_rt->rt_ifp) { |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4774 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4775 | /* |
| 4776 | * This one has a different interface than the one at the |
| 4777 | * top of the list. Place it ahead. |
| 4778 | */ |
| 4779 | TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4780 | } else { |
| 4781 | /* |
| 4782 | * Ok we have the same interface as the first one. Move |
| 4783 | * forward until we find either a) one with a NULL route... |
| 4784 | * insert ahead of that b) one with a different ifp.. insert |
| 4785 | * after that. c) end of the list.. insert at the tail. |
| 4786 | */ |
| 4787 | struct sctp_nets *netlook; |
| 4788 | |
| 4789 | do { |
| 4790 | netlook = TAILQ_NEXT(netfirst, sctp_next); |
| 4791 | if (netlook == NULL) { |
| 4792 | /* End of the list */ |
| 4793 | TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next); |
| 4794 | break; |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 4795 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4796 | } else if (netlook->ro.ro_nh == NULL) { |
| 4797 | #else |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4798 | } else if (netlook->ro.ro_rt == NULL) { |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4799 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4800 | /* next one has NO route */ |
| 4801 | TAILQ_INSERT_BEFORE(netfirst, net, sctp_next); |
| 4802 | break; |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 4803 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4804 | } else if (netlook->ro.ro_nh->nh_ifp != net->ro.ro_nh->nh_ifp) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4805 | #else |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4806 | } else if (netlook->ro.ro_rt->rt_ifp != net->ro.ro_rt->rt_ifp) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4807 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4808 | TAILQ_INSERT_AFTER(&stcb->asoc.nets, netlook, |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4809 | net, sctp_next); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4810 | break; |
| 4811 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4812 | /* Shift forward */ |
| 4813 | netfirst = netlook; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4814 | } while (netlook != NULL); |
| 4815 | } |
| 4816 | |
| 4817 | /* got to have a primary set */ |
| 4818 | if (stcb->asoc.primary_destination == 0) { |
| 4819 | stcb->asoc.primary_destination = net; |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 4820 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4821 | } else if ((stcb->asoc.primary_destination->ro.ro_nh == NULL) && |
| 4822 | (net->ro.ro_nh) && |
| 4823 | #else |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4824 | } else if ((stcb->asoc.primary_destination->ro.ro_rt == NULL) && |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4825 | (net->ro.ro_rt) && |
| 4826 | #endif |
| 4827 | ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4828 | /* No route to current primary adopt new primary */ |
| 4829 | stcb->asoc.primary_destination = net; |
| 4830 | } |
| 4831 | /* Validate primary is first */ |
| 4832 | net = TAILQ_FIRST(&stcb->asoc.nets); |
| 4833 | if ((net != stcb->asoc.primary_destination) && |
| 4834 | (stcb->asoc.primary_destination)) { |
| 4835 | /* first one on the list is NOT the primary |
| 4836 | * sctp_cmpaddr() is much more efficient if |
| 4837 | * the primary is the first on the list, make it |
| 4838 | * so. |
| 4839 | */ |
| 4840 | TAILQ_REMOVE(&stcb->asoc.nets, |
| 4841 | stcb->asoc.primary_destination, sctp_next); |
| 4842 | TAILQ_INSERT_HEAD(&stcb->asoc.nets, |
| 4843 | stcb->asoc.primary_destination, sctp_next); |
| 4844 | } |
| 4845 | return (0); |
| 4846 | } |
| 4847 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4848 | static uint32_t |
| 4849 | sctp_aloc_a_assoc_id(struct sctp_inpcb *inp, struct sctp_tcb *stcb) |
| 4850 | { |
| 4851 | uint32_t id; |
| 4852 | struct sctpasochead *head; |
| 4853 | struct sctp_tcb *lstcb; |
| 4854 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4855 | try_again: |
| 4856 | if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
| 4857 | /* TSNH */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4858 | return (0); |
| 4859 | } |
| 4860 | /* |
| 4861 | * We don't allow assoc id to be one of SCTP_FUTURE_ASSOC, |
| 4862 | * SCTP_CURRENT_ASSOC and SCTP_ALL_ASSOC. |
| 4863 | */ |
| 4864 | if (inp->sctp_associd_counter <= SCTP_ALL_ASSOC) { |
| 4865 | inp->sctp_associd_counter = SCTP_ALL_ASSOC + 1; |
| 4866 | } |
| 4867 | id = inp->sctp_associd_counter; |
| 4868 | inp->sctp_associd_counter++; |
| 4869 | lstcb = sctp_findasoc_ep_asocid_locked(inp, (sctp_assoc_t)id, 0); |
| 4870 | if (lstcb) { |
| 4871 | goto try_again; |
| 4872 | } |
| 4873 | head = &inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(id, inp->hashasocidmark)]; |
| 4874 | LIST_INSERT_HEAD(head, stcb, sctp_tcbasocidhash); |
| 4875 | stcb->asoc.in_asocid_hash = 1; |
Michael Tuexen | be8e0eb | 2019-03-24 00:11:02 +0100 | [diff] [blame] | 4876 | return (id); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4877 | } |
| 4878 | |
| 4879 | /* |
| 4880 | * allocate an association and add it to the endpoint. The caller must be |
| 4881 | * careful to add all additional addresses once they are know right away or |
| 4882 | * else the assoc will be may experience a blackout scenario. |
| 4883 | */ |
Michael Tuexen | 8dd0be7 | 2021-09-12 19:27:59 +0200 | [diff] [blame] | 4884 | static struct sctp_tcb * |
| 4885 | sctp_aloc_assoc_locked(struct sctp_inpcb *inp, struct sockaddr *firstaddr, |
| 4886 | int *error, uint32_t override_tag, uint32_t initial_tsn, |
| 4887 | uint32_t vrf_id, uint16_t o_streams, uint16_t port, |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 4888 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
Michael Tuexen | 8dd0be7 | 2021-09-12 19:27:59 +0200 | [diff] [blame] | 4889 | struct thread *p, |
Michael Tuexen | eb61157 | 2020-06-13 17:27:02 +0200 | [diff] [blame] | 4890 | #elif defined(_WIN32) && !defined(__Userspace__) |
Michael Tuexen | 8dd0be7 | 2021-09-12 19:27:59 +0200 | [diff] [blame] | 4891 | PKTHREAD p, |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4892 | #else |
| 4893 | #if defined(__Userspace__) |
Michael Tuexen | 8dd0be7 | 2021-09-12 19:27:59 +0200 | [diff] [blame] | 4894 | /* __Userspace__ NULL proc is going to be passed here. See sctp_lower_sosend */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4895 | #endif |
Michael Tuexen | 8dd0be7 | 2021-09-12 19:27:59 +0200 | [diff] [blame] | 4896 | struct proc *p, |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4897 | #endif |
Michael Tuexen | 8dd0be7 | 2021-09-12 19:27:59 +0200 | [diff] [blame] | 4898 | int initialize_auth_params) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4899 | { |
| 4900 | /* note the p argument is only valid in unbound sockets */ |
| 4901 | |
| 4902 | struct sctp_tcb *stcb; |
| 4903 | struct sctp_association *asoc; |
| 4904 | struct sctpasochead *head; |
| 4905 | uint16_t rport; |
| 4906 | int err; |
| 4907 | |
Michael Tuexen | 8dd0be7 | 2021-09-12 19:27:59 +0200 | [diff] [blame] | 4908 | SCTP_INP_INFO_WLOCK_ASSERT(); |
| 4909 | SCTP_INP_WLOCK_ASSERT(inp); |
| 4910 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4911 | /* |
| 4912 | * Assumption made here: Caller has done a |
| 4913 | * sctp_findassociation_ep_addr(ep, addr's); to make sure the |
| 4914 | * address does not exist already. |
| 4915 | */ |
| 4916 | if (SCTP_BASE_INFO(ipi_count_asoc) >= SCTP_MAX_NUM_OF_ASOC) { |
| 4917 | /* Hit max assoc, sorry no more */ |
| 4918 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS); |
| 4919 | *error = ENOBUFS; |
| 4920 | return (NULL); |
| 4921 | } |
| 4922 | if (firstaddr == NULL) { |
| 4923 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 4924 | *error = EINVAL; |
| 4925 | return (NULL); |
| 4926 | } |
Michael Tuexen | 8dd0be7 | 2021-09-12 19:27:59 +0200 | [diff] [blame] | 4927 | if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_SOCKET_ALLGONE)) { |
| 4928 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 4929 | *error = EINVAL; |
| 4930 | return (NULL); |
| 4931 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4932 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) && |
| 4933 | ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) || |
| 4934 | (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) { |
| 4935 | /* |
| 4936 | * If its in the TCP pool, its NOT allowed to create an |
| 4937 | * association. The parent listener needs to call |
| 4938 | * sctp_aloc_assoc.. or the one-2-many socket. If a peeled |
| 4939 | * off, or connected one does this.. its an error. |
| 4940 | */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4941 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 4942 | *error = EINVAL; |
| 4943 | return (NULL); |
| 4944 | } |
| 4945 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || |
| 4946 | (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) { |
| 4947 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) || |
| 4948 | (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4949 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 4950 | *error = EINVAL; |
| 4951 | return (NULL); |
| 4952 | } |
| 4953 | } |
| 4954 | SCTPDBG(SCTP_DEBUG_PCB3, "Allocate an association for peer:"); |
| 4955 | #ifdef SCTP_DEBUG |
| 4956 | if (firstaddr) { |
| 4957 | SCTPDBG_ADDR(SCTP_DEBUG_PCB3, firstaddr); |
| 4958 | switch (firstaddr->sa_family) { |
| 4959 | #ifdef INET |
| 4960 | case AF_INET: |
| 4961 | SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n", |
| 4962 | ntohs(((struct sockaddr_in *)firstaddr)->sin_port)); |
| 4963 | break; |
| 4964 | #endif |
| 4965 | #ifdef INET6 |
| 4966 | case AF_INET6: |
| 4967 | SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n", |
| 4968 | ntohs(((struct sockaddr_in6 *)firstaddr)->sin6_port)); |
| 4969 | break; |
| 4970 | #endif |
tuexen | cc2b142 | 2012-07-10 21:50:32 +0000 | [diff] [blame] | 4971 | #if defined(__Userspace__) |
| 4972 | case AF_CONN: |
| 4973 | SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n", |
| 4974 | ntohs(((struct sockaddr_conn *)firstaddr)->sconn_port)); |
| 4975 | break; |
| 4976 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4977 | default: |
| 4978 | break; |
| 4979 | } |
| 4980 | } else { |
| 4981 | SCTPDBG(SCTP_DEBUG_PCB3,"None\n"); |
| 4982 | } |
| 4983 | #endif /* SCTP_DEBUG */ |
| 4984 | switch (firstaddr->sa_family) { |
| 4985 | #ifdef INET |
| 4986 | case AF_INET: |
| 4987 | { |
| 4988 | struct sockaddr_in *sin; |
| 4989 | |
| 4990 | sin = (struct sockaddr_in *)firstaddr; |
| 4991 | if ((ntohs(sin->sin_port) == 0) || |
| 4992 | (sin->sin_addr.s_addr == INADDR_ANY) || |
| 4993 | (sin->sin_addr.s_addr == INADDR_BROADCAST) || |
Michael Tuexen | d844a69 | 2020-09-29 11:38:11 +0200 | [diff] [blame] | 4994 | IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) || |
| 4995 | #if defined(__Userspace__) |
Michael Tuexen | 8d6935d | 2022-05-15 01:51:24 +0200 | [diff] [blame] | 4996 | ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_CONN) || |
| 4997 | ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) && |
Michael Tuexen | d844a69 | 2020-09-29 11:38:11 +0200 | [diff] [blame] | 4998 | (SCTP_IPV6_V6ONLY(inp) != 0)))) { |
| 4999 | #else |
Michael Tuexen | 8d6935d | 2022-05-15 01:51:24 +0200 | [diff] [blame] | 5000 | ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) && |
Michael Tuexen | d844a69 | 2020-09-29 11:38:11 +0200 | [diff] [blame] | 5001 | (SCTP_IPV6_V6ONLY(inp) != 0))) { |
| 5002 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5003 | /* Invalid address */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5004 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 5005 | *error = EINVAL; |
| 5006 | return (NULL); |
| 5007 | } |
| 5008 | rport = sin->sin_port; |
| 5009 | break; |
| 5010 | } |
| 5011 | #endif |
| 5012 | #ifdef INET6 |
| 5013 | case AF_INET6: |
| 5014 | { |
| 5015 | struct sockaddr_in6 *sin6; |
| 5016 | |
| 5017 | sin6 = (struct sockaddr_in6 *)firstaddr; |
| 5018 | if ((ntohs(sin6->sin6_port) == 0) || |
| 5019 | IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) || |
Michael Tuexen | d844a69 | 2020-09-29 11:38:11 +0200 | [diff] [blame] | 5020 | IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) || |
| 5021 | ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5022 | /* Invalid address */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5023 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 5024 | *error = EINVAL; |
| 5025 | return (NULL); |
| 5026 | } |
| 5027 | rport = sin6->sin6_port; |
| 5028 | break; |
| 5029 | } |
| 5030 | #endif |
tuexen | cc2b142 | 2012-07-10 21:50:32 +0000 | [diff] [blame] | 5031 | #if defined(__Userspace__) |
| 5032 | case AF_CONN: |
| 5033 | { |
| 5034 | struct sockaddr_conn *sconn; |
| 5035 | |
| 5036 | sconn = (struct sockaddr_conn *)firstaddr; |
| 5037 | if ((ntohs(sconn->sconn_port) == 0) || |
Michael Tuexen | d844a69 | 2020-09-29 11:38:11 +0200 | [diff] [blame] | 5038 | (sconn->sconn_addr == NULL) || |
| 5039 | ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_CONN) == 0)) { |
tuexen | cc2b142 | 2012-07-10 21:50:32 +0000 | [diff] [blame] | 5040 | /* Invalid address */ |
tuexen | cc2b142 | 2012-07-10 21:50:32 +0000 | [diff] [blame] | 5041 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 5042 | *error = EINVAL; |
| 5043 | return (NULL); |
| 5044 | } |
| 5045 | rport = sconn->sconn_port; |
| 5046 | break; |
| 5047 | } |
| 5048 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5049 | default: |
| 5050 | /* not supported family type */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5051 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 5052 | *error = EINVAL; |
| 5053 | return (NULL); |
| 5054 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5055 | if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) { |
| 5056 | /* |
| 5057 | * If you have not performed a bind, then we need to do the |
| 5058 | * ephemeral bind for you. |
| 5059 | */ |
Michael Tuexen | 8dd0be7 | 2021-09-12 19:27:59 +0200 | [diff] [blame] | 5060 | if ((err = sctp_inpcb_bind_locked(inp, NULL, NULL, p))) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5061 | /* bind error, probably perm */ |
| 5062 | *error = err; |
| 5063 | return (NULL); |
| 5064 | } |
| 5065 | } |
| 5066 | stcb = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_asoc), struct sctp_tcb); |
| 5067 | if (stcb == NULL) { |
| 5068 | /* out of memory? */ |
| 5069 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM); |
| 5070 | *error = ENOMEM; |
| 5071 | return (NULL); |
| 5072 | } |
| 5073 | SCTP_INCR_ASOC_COUNT(); |
| 5074 | |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 5075 | memset(stcb, 0, sizeof(*stcb)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5076 | asoc = &stcb->asoc; |
| 5077 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5078 | SCTP_TCB_LOCK_INIT(stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5079 | stcb->rport = rport; |
| 5080 | /* setup back pointer's */ |
| 5081 | stcb->sctp_ep = inp; |
| 5082 | stcb->sctp_socket = inp->sctp_socket; |
Michael Tuexen | 965b19a | 2021-06-27 19:57:20 +0200 | [diff] [blame] | 5083 | if ((err = sctp_init_asoc(inp, stcb, override_tag, initial_tsn, vrf_id, o_streams))) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5084 | /* failed */ |
| 5085 | SCTP_TCB_LOCK_DESTROY(stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5086 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb); |
| 5087 | SCTP_DECR_ASOC_COUNT(); |
| 5088 | *error = err; |
| 5089 | return (NULL); |
| 5090 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5091 | SCTP_TCB_LOCK(stcb); |
| 5092 | |
Michael Tuexen | be8e0eb | 2019-03-24 00:11:02 +0100 | [diff] [blame] | 5093 | asoc->assoc_id = sctp_aloc_a_assoc_id(inp, stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5094 | /* now that my_vtag is set, add it to the hash */ |
| 5095 | head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))]; |
| 5096 | /* put it in the bucket in the vtag hash of assoc's for the system */ |
| 5097 | LIST_INSERT_HEAD(head, stcb, sctp_asocs); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5098 | |
Michael Tuexen | 5cd6fe9 | 2020-10-06 13:14:37 +0200 | [diff] [blame] | 5099 | if (sctp_add_remote_addr(stcb, firstaddr, NULL, port, SCTP_DO_SETSCOPE, SCTP_ALLOC_ASOC)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5100 | /* failure.. memory error? */ |
| 5101 | if (asoc->strmout) { |
| 5102 | SCTP_FREE(asoc->strmout, SCTP_M_STRMO); |
| 5103 | asoc->strmout = NULL; |
| 5104 | } |
| 5105 | if (asoc->mapping_array) { |
| 5106 | SCTP_FREE(asoc->mapping_array, SCTP_M_MAP); |
| 5107 | asoc->mapping_array = NULL; |
| 5108 | } |
| 5109 | if (asoc->nr_mapping_array) { |
| 5110 | SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP); |
| 5111 | asoc->nr_mapping_array = NULL; |
| 5112 | } |
| 5113 | SCTP_DECR_ASOC_COUNT(); |
t00fcxen | 2c2ff92 | 2013-11-03 14:03:35 +0000 | [diff] [blame] | 5114 | SCTP_TCB_UNLOCK(stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5115 | SCTP_TCB_LOCK_DESTROY(stcb); |
Michael Tuexen | 8dd0be7 | 2021-09-12 19:27:59 +0200 | [diff] [blame] | 5116 | LIST_REMOVE(stcb, sctp_asocs); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5117 | LIST_REMOVE(stcb, sctp_tcbasocidhash); |
| 5118 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb); |
| 5119 | SCTP_INP_WUNLOCK(inp); |
| 5120 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS); |
| 5121 | *error = ENOBUFS; |
| 5122 | return (NULL); |
| 5123 | } |
| 5124 | /* Init all the timers */ |
| 5125 | SCTP_OS_TIMER_INIT(&asoc->dack_timer.timer); |
| 5126 | SCTP_OS_TIMER_INIT(&asoc->strreset_timer.timer); |
| 5127 | SCTP_OS_TIMER_INIT(&asoc->asconf_timer.timer); |
| 5128 | SCTP_OS_TIMER_INIT(&asoc->shut_guard_timer.timer); |
| 5129 | SCTP_OS_TIMER_INIT(&asoc->autoclose_timer.timer); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5130 | SCTP_OS_TIMER_INIT(&asoc->delete_prim_timer.timer); |
| 5131 | |
| 5132 | LIST_INSERT_HEAD(&inp->sctp_asoc_list, stcb, sctp_tcblist); |
| 5133 | /* now file the port under the hash as well */ |
| 5134 | if (inp->sctp_tcbhash != NULL) { |
| 5135 | head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(stcb->rport, |
| 5136 | inp->sctp_hashmark)]; |
| 5137 | LIST_INSERT_HEAD(head, stcb, sctp_tcbhash); |
| 5138 | } |
Michael Tuexen | bfb04f3 | 2019-07-14 14:42:17 +0200 | [diff] [blame] | 5139 | if (initialize_auth_params == SCTP_INITIALIZE_AUTH_PARAMS) { |
| 5140 | sctp_initialize_auth_params(inp, stcb); |
| 5141 | } |
t00fcxen | 8fca7ff | 2012-09-05 18:58:42 +0000 | [diff] [blame] | 5142 | SCTPDBG(SCTP_DEBUG_PCB1, "Association %p now allocated\n", (void *)stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5143 | return (stcb); |
| 5144 | } |
| 5145 | |
Michael Tuexen | 8dd0be7 | 2021-09-12 19:27:59 +0200 | [diff] [blame] | 5146 | struct sctp_tcb * |
| 5147 | sctp_aloc_assoc(struct sctp_inpcb *inp, struct sockaddr *firstaddr, |
| 5148 | int *error, uint32_t override_tag, uint32_t initial_tsn, |
| 5149 | uint32_t vrf_id, uint16_t o_streams, uint16_t port, |
| 5150 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 5151 | struct thread *p, |
| 5152 | #elif defined(_WIN32) && !defined(__Userspace__) |
| 5153 | PKTHREAD p, |
| 5154 | #else |
| 5155 | struct proc *p, |
| 5156 | #endif |
| 5157 | int initialize_auth_params) |
| 5158 | { |
| 5159 | struct sctp_tcb *stcb; |
| 5160 | |
| 5161 | SCTP_INP_INFO_WLOCK(); |
| 5162 | SCTP_INP_WLOCK(inp); |
| 5163 | stcb = sctp_aloc_assoc_locked(inp, firstaddr, error, override_tag, |
| 5164 | initial_tsn, vrf_id, o_streams, port, p, initialize_auth_params); |
| 5165 | SCTP_INP_INFO_WUNLOCK(); |
| 5166 | SCTP_INP_WUNLOCK(inp); |
| 5167 | return (stcb); |
| 5168 | } |
| 5169 | |
| 5170 | struct sctp_tcb * |
| 5171 | sctp_aloc_assoc_connected(struct sctp_inpcb *inp, struct sockaddr *firstaddr, |
| 5172 | int *error, uint32_t override_tag, uint32_t initial_tsn, |
| 5173 | uint32_t vrf_id, uint16_t o_streams, uint16_t port, |
| 5174 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 5175 | struct thread *p, |
| 5176 | #elif defined(_WIN32) && !defined(__Userspace__) |
| 5177 | PKTHREAD p, |
| 5178 | #else |
| 5179 | struct proc *p, |
| 5180 | #endif |
| 5181 | int initialize_auth_params) |
| 5182 | { |
| 5183 | struct sctp_tcb *stcb; |
| 5184 | |
| 5185 | SCTP_INP_INFO_WLOCK(); |
| 5186 | SCTP_INP_WLOCK(inp); |
| 5187 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) && |
| 5188 | SCTP_IS_LISTENING(inp)) { |
| 5189 | SCTP_INP_INFO_WUNLOCK(); |
| 5190 | SCTP_INP_WUNLOCK(inp); |
| 5191 | *error = EINVAL; |
| 5192 | return (NULL); |
| 5193 | } |
| 5194 | stcb = sctp_aloc_assoc_locked(inp, firstaddr, error, override_tag, |
| 5195 | initial_tsn, vrf_id, o_streams, port, p, initialize_auth_params); |
| 5196 | SCTP_INP_INFO_WUNLOCK(); |
| 5197 | if (stcb != NULL && (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) { |
| 5198 | inp->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED; |
| 5199 | soisconnecting(inp->sctp_socket); |
| 5200 | } |
| 5201 | SCTP_INP_WUNLOCK(inp); |
| 5202 | return (stcb); |
| 5203 | } |
| 5204 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5205 | void |
| 5206 | sctp_remove_net(struct sctp_tcb *stcb, struct sctp_nets *net) |
| 5207 | { |
Michael Tuexen | 21b55df | 2020-02-09 23:46:32 +0100 | [diff] [blame] | 5208 | struct sctp_inpcb *inp; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5209 | struct sctp_association *asoc; |
| 5210 | |
Michael Tuexen | 21b55df | 2020-02-09 23:46:32 +0100 | [diff] [blame] | 5211 | inp = stcb->sctp_ep; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5212 | asoc = &stcb->asoc; |
| 5213 | asoc->numnets--; |
| 5214 | TAILQ_REMOVE(&asoc->nets, net, sctp_next); |
| 5215 | if (net == asoc->primary_destination) { |
| 5216 | /* Reset primary */ |
| 5217 | struct sctp_nets *lnet; |
| 5218 | |
| 5219 | lnet = TAILQ_FIRST(&asoc->nets); |
| 5220 | /* Mobility adaptation |
| 5221 | Ideally, if deleted destination is the primary, it becomes |
| 5222 | a fast retransmission trigger by the subsequent SET PRIMARY. |
| 5223 | (by micchie) |
| 5224 | */ |
| 5225 | if (sctp_is_mobility_feature_on(stcb->sctp_ep, |
t00fcxen | 8dd95b8 | 2014-07-11 20:41:28 +0000 | [diff] [blame] | 5226 | SCTP_MOBILITY_BASE) || |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5227 | sctp_is_mobility_feature_on(stcb->sctp_ep, |
t00fcxen | 8dd95b8 | 2014-07-11 20:41:28 +0000 | [diff] [blame] | 5228 | SCTP_MOBILITY_FASTHANDOFF)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5229 | SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: primary dst is deleting\n"); |
| 5230 | if (asoc->deleted_primary != NULL) { |
| 5231 | SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: deleted primary may be already stored\n"); |
| 5232 | goto out; |
| 5233 | } |
| 5234 | asoc->deleted_primary = net; |
| 5235 | atomic_add_int(&net->ref_count, 1); |
| 5236 | memset(&net->lastsa, 0, sizeof(net->lastsa)); |
| 5237 | memset(&net->lastsv, 0, sizeof(net->lastsv)); |
| 5238 | sctp_mobility_feature_on(stcb->sctp_ep, |
| 5239 | SCTP_MOBILITY_PRIM_DELETED); |
| 5240 | sctp_timer_start(SCTP_TIMER_TYPE_PRIM_DELETED, |
| 5241 | stcb->sctp_ep, stcb, NULL); |
| 5242 | } |
| 5243 | out: |
| 5244 | /* Try to find a confirmed primary */ |
| 5245 | asoc->primary_destination = sctp_find_alternate_net(stcb, lnet, 0); |
| 5246 | } |
| 5247 | if (net == asoc->last_data_chunk_from) { |
| 5248 | /* Reset primary */ |
| 5249 | asoc->last_data_chunk_from = TAILQ_FIRST(&asoc->nets); |
| 5250 | } |
| 5251 | if (net == asoc->last_control_chunk_from) { |
| 5252 | /* Clear net */ |
| 5253 | asoc->last_control_chunk_from = NULL; |
| 5254 | } |
Michael Tuexen | 6e00fe2 | 2021-02-21 17:31:55 +0100 | [diff] [blame] | 5255 | if (net == asoc->last_net_cmt_send_started) { |
| 5256 | /* Clear net */ |
| 5257 | asoc->last_net_cmt_send_started = NULL; |
| 5258 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5259 | if (net == stcb->asoc.alternate) { |
| 5260 | sctp_free_remote_addr(stcb->asoc.alternate); |
| 5261 | stcb->asoc.alternate = NULL; |
| 5262 | } |
Michael Tuexen | 21b55df | 2020-02-09 23:46:32 +0100 | [diff] [blame] | 5263 | sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net, |
| 5264 | SCTP_FROM_SCTP_PCB + SCTP_LOC_9); |
| 5265 | sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, |
| 5266 | SCTP_FROM_SCTP_PCB + SCTP_LOC_10); |
Michael Tuexen | f52d3df | 2020-02-11 19:24:34 +0100 | [diff] [blame] | 5267 | net->dest_state |= SCTP_ADDR_BEING_DELETED; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5268 | sctp_free_remote_addr(net); |
| 5269 | } |
| 5270 | |
| 5271 | /* |
| 5272 | * remove a remote endpoint address from an association, it will fail if the |
| 5273 | * address does not exist. |
| 5274 | */ |
| 5275 | int |
| 5276 | sctp_del_remote_addr(struct sctp_tcb *stcb, struct sockaddr *remaddr) |
| 5277 | { |
| 5278 | /* |
| 5279 | * Here we need to remove a remote address. This is quite simple, we |
| 5280 | * first find it in the list of address for the association |
| 5281 | * (tasoc->asoc.nets) and then if it is there, we do a LIST_REMOVE |
| 5282 | * on that item. Note we do not allow it to be removed if there are |
| 5283 | * no other addresses. |
| 5284 | */ |
| 5285 | struct sctp_association *asoc; |
| 5286 | struct sctp_nets *net, *nnet; |
| 5287 | |
| 5288 | asoc = &stcb->asoc; |
| 5289 | |
| 5290 | /* locate the address */ |
| 5291 | TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) { |
| 5292 | if (net->ro._l_addr.sa.sa_family != remaddr->sa_family) { |
| 5293 | continue; |
| 5294 | } |
| 5295 | if (sctp_cmpaddr((struct sockaddr *)&net->ro._l_addr, |
| 5296 | remaddr)) { |
| 5297 | /* we found the guy */ |
| 5298 | if (asoc->numnets < 2) { |
| 5299 | /* Must have at LEAST two remote addresses */ |
| 5300 | return (-1); |
| 5301 | } else { |
| 5302 | sctp_remove_net(stcb, net); |
| 5303 | return (0); |
| 5304 | } |
| 5305 | } |
| 5306 | } |
| 5307 | /* not found. */ |
| 5308 | return (-2); |
| 5309 | } |
| 5310 | |
Michael Tuexen | 4ec13f7 | 2021-09-09 01:22:43 +0200 | [diff] [blame] | 5311 | static bool |
| 5312 | sctp_is_in_timewait(uint32_t tag, uint16_t lport, uint16_t rport, uint32_t now) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5313 | { |
| 5314 | struct sctpvtaghead *chain; |
| 5315 | struct sctp_tagblock *twait_block; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5316 | int i; |
| 5317 | |
Michael Tuexen | 8dd0be7 | 2021-09-12 19:27:59 +0200 | [diff] [blame] | 5318 | SCTP_INP_INFO_LOCK_ASSERT(); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5319 | chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)]; |
t00fcxen | 14f8fbb | 2013-06-25 09:32:47 +0000 | [diff] [blame] | 5320 | LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) { |
| 5321 | for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) { |
Michael Tuexen | 4ec13f7 | 2021-09-09 01:22:43 +0200 | [diff] [blame] | 5322 | if ((twait_block->vtag_block[i].tv_sec_at_expire >= now) && |
| 5323 | (twait_block->vtag_block[i].v_tag == tag) && |
| 5324 | (twait_block->vtag_block[i].lport == lport) && |
t00fcxen | 14f8fbb | 2013-06-25 09:32:47 +0000 | [diff] [blame] | 5325 | (twait_block->vtag_block[i].rport == rport)) { |
Michael Tuexen | 4ec13f7 | 2021-09-09 01:22:43 +0200 | [diff] [blame] | 5326 | return (true); |
t00fcxen | 14f8fbb | 2013-06-25 09:32:47 +0000 | [diff] [blame] | 5327 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5328 | } |
| 5329 | } |
Michael Tuexen | 4ec13f7 | 2021-09-09 01:22:43 +0200 | [diff] [blame] | 5330 | return (false); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5331 | } |
| 5332 | |
Michael Tuexen | 4ec13f7 | 2021-09-09 01:22:43 +0200 | [diff] [blame] | 5333 | static void |
| 5334 | sctp_set_vtag_block(struct sctp_timewait *vtag_block, uint32_t time, |
| 5335 | uint32_t tag, uint16_t lport, uint16_t rport) |
| 5336 | { |
| 5337 | vtag_block->tv_sec_at_expire = time; |
| 5338 | vtag_block->v_tag = tag; |
| 5339 | vtag_block->lport = lport; |
| 5340 | vtag_block->rport = rport; |
| 5341 | } |
| 5342 | |
| 5343 | static void |
| 5344 | sctp_add_vtag_to_timewait(uint32_t tag, uint16_t lport, uint16_t rport) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5345 | { |
| 5346 | struct sctpvtaghead *chain; |
| 5347 | struct sctp_tagblock *twait_block; |
| 5348 | struct timeval now; |
Michael Tuexen | 4ec13f7 | 2021-09-09 01:22:43 +0200 | [diff] [blame] | 5349 | uint32_t time; |
| 5350 | int i; |
| 5351 | bool set; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5352 | |
Michael Tuexen | 4ec13f7 | 2021-09-09 01:22:43 +0200 | [diff] [blame] | 5353 | SCTP_INP_INFO_WLOCK_ASSERT(); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5354 | (void)SCTP_GETTIME_TIMEVAL(&now); |
Michael Tuexen | 6470706 | 2021-09-09 20:08:10 +0200 | [diff] [blame] | 5355 | time = (uint32_t)now.tv_sec + SCTP_BASE_SYSCTL(sctp_vtag_time_wait); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5356 | chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)]; |
Michael Tuexen | 4ec13f7 | 2021-09-09 01:22:43 +0200 | [diff] [blame] | 5357 | set = false; |
t00fcxen | 14f8fbb | 2013-06-25 09:32:47 +0000 | [diff] [blame] | 5358 | LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5359 | /* Block(s) present, lets find space, and expire on the fly */ |
t00fcxen | 14f8fbb | 2013-06-25 09:32:47 +0000 | [diff] [blame] | 5360 | for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) { |
Michael Tuexen | 4ec13f7 | 2021-09-09 01:22:43 +0200 | [diff] [blame] | 5361 | if ((twait_block->vtag_block[i].v_tag == 0) && !set) { |
| 5362 | sctp_set_vtag_block(twait_block->vtag_block + i, time, tag, lport, rport); |
| 5363 | set = true; |
| 5364 | continue; |
| 5365 | } |
| 5366 | if ((twait_block->vtag_block[i].v_tag != 0) && |
| 5367 | (twait_block->vtag_block[i].tv_sec_at_expire < (uint32_t)now.tv_sec)) { |
| 5368 | if (set) { |
| 5369 | /* Audit expires this guy */ |
| 5370 | sctp_set_vtag_block(twait_block->vtag_block + i, 0, 0, 0, 0); |
| 5371 | } else { |
| 5372 | /* Reuse it for the new tag */ |
| 5373 | sctp_set_vtag_block(twait_block->vtag_block + i, time, tag, lport, rport); |
| 5374 | set = true; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5375 | } |
| 5376 | } |
t00fcxen | 14f8fbb | 2013-06-25 09:32:47 +0000 | [diff] [blame] | 5377 | } |
| 5378 | if (set) { |
| 5379 | /* |
| 5380 | * We only do up to the block where we can |
| 5381 | * place our tag for audits |
| 5382 | */ |
| 5383 | break; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5384 | } |
| 5385 | } |
| 5386 | /* Need to add a new block to chain */ |
| 5387 | if (!set) { |
| 5388 | SCTP_MALLOC(twait_block, struct sctp_tagblock *, |
| 5389 | sizeof(struct sctp_tagblock), SCTP_M_TIMW); |
| 5390 | if (twait_block == NULL) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5391 | return; |
| 5392 | } |
| 5393 | memset(twait_block, 0, sizeof(struct sctp_tagblock)); |
| 5394 | LIST_INSERT_HEAD(chain, twait_block, sctp_nxt_tagblock); |
Michael Tuexen | 4ec13f7 | 2021-09-09 01:22:43 +0200 | [diff] [blame] | 5395 | sctp_set_vtag_block(twait_block->vtag_block, time, tag, lport, rport); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5396 | } |
| 5397 | } |
| 5398 | |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 5399 | void |
| 5400 | sctp_clean_up_stream(struct sctp_tcb *stcb, struct sctp_readhead *rh) |
| 5401 | { |
| 5402 | struct sctp_tmit_chunk *chk, *nchk; |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 5403 | struct sctp_queued_to_read *control, *ncontrol; |
| 5404 | |
| 5405 | TAILQ_FOREACH_SAFE(control, rh, next_instrm, ncontrol) { |
| 5406 | TAILQ_REMOVE(rh, control, next_instrm); |
| 5407 | control->on_strm_q = 0; |
| 5408 | if (control->on_read_q == 0) { |
| 5409 | sctp_free_remote_addr(control->whoFrom); |
| 5410 | if (control->data) { |
| 5411 | sctp_m_freem(control->data); |
| 5412 | control->data = NULL; |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 5413 | } |
| 5414 | } |
| 5415 | /* Reassembly free? */ |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 5416 | TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) { |
| 5417 | TAILQ_REMOVE(&control->reasm, chk, sctp_next); |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 5418 | if (chk->data) { |
| 5419 | sctp_m_freem(chk->data); |
| 5420 | chk->data = NULL; |
| 5421 | } |
| 5422 | if (chk->holds_key_ref) |
| 5423 | sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); |
| 5424 | sctp_free_remote_addr(chk->whoTo); |
| 5425 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); |
| 5426 | SCTP_DECR_CHK_COUNT(); |
| 5427 | /*sa_ignore FREED_MEMORY*/ |
| 5428 | } |
| 5429 | /* |
| 5430 | * We don't free the address here |
| 5431 | * since all the net's were freed |
| 5432 | * above. |
| 5433 | */ |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 5434 | if (control->on_read_q == 0) { |
| 5435 | sctp_free_a_readq(stcb, control); |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 5436 | } |
| 5437 | } |
| 5438 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5439 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5440 | /*- |
| 5441 | * Free the association after un-hashing the remote port. This |
| 5442 | * function ALWAYS returns holding NO LOCK on the stcb. It DOES |
| 5443 | * expect that the input to this function IS a locked TCB. |
| 5444 | * It will return 0, if it did NOT destroy the association (instead |
| 5445 | * it unlocks it. It will return NON-zero if it either destroyed the |
| 5446 | * association OR the association is already destroyed. |
| 5447 | */ |
| 5448 | int |
| 5449 | sctp_free_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int from_inpcbfree, int from_location) |
| 5450 | { |
| 5451 | int i; |
| 5452 | struct sctp_association *asoc; |
| 5453 | struct sctp_nets *net, *nnet; |
| 5454 | struct sctp_laddr *laddr, *naddr; |
| 5455 | struct sctp_tmit_chunk *chk, *nchk; |
| 5456 | struct sctp_asconf_addr *aparam, *naparam; |
| 5457 | struct sctp_asconf_ack *aack, *naack; |
| 5458 | struct sctp_stream_reset_list *strrst, *nstrrst; |
| 5459 | struct sctp_queued_to_read *sq, *nsq; |
| 5460 | struct sctp_stream_queue_pending *sp, *nsp; |
| 5461 | sctp_sharedkey_t *shared_key, *nshared_key; |
| 5462 | struct socket *so; |
| 5463 | |
| 5464 | /* first, lets purge the entry from the hash table. */ |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 5465 | #if defined(__APPLE__) && !defined(__Userspace__) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5466 | sctp_lock_assert(SCTP_INP_SO(inp)); |
| 5467 | #endif |
Michael Tuexen | 0586533 | 2022-03-29 21:00:12 +0200 | [diff] [blame] | 5468 | SCTP_TCB_LOCK_ASSERT(stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5469 | |
| 5470 | #ifdef SCTP_LOG_CLOSING |
| 5471 | sctp_log_closing(inp, stcb, 6); |
| 5472 | #endif |
| 5473 | if (stcb->asoc.state == 0) { |
| 5474 | #ifdef SCTP_LOG_CLOSING |
| 5475 | sctp_log_closing(inp, NULL, 7); |
| 5476 | #endif |
| 5477 | /* there is no asoc, really TSNH :-0 */ |
| 5478 | return (1); |
| 5479 | } |
| 5480 | if (stcb->asoc.alternate) { |
| 5481 | sctp_free_remote_addr(stcb->asoc.alternate); |
| 5482 | stcb->asoc.alternate = NULL; |
| 5483 | } |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 5484 | #if !(defined(__APPLE__) && !defined(__Userspace__)) |
| 5485 | /* TEMP CODE */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5486 | if (stcb->freed_from_where == 0) { |
| 5487 | /* Only record the first place free happened from */ |
| 5488 | stcb->freed_from_where = from_location; |
| 5489 | } |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 5490 | /* TEMP CODE */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5491 | #endif |
| 5492 | |
| 5493 | asoc = &stcb->asoc; |
| 5494 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || |
| 5495 | (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) |
| 5496 | /* nothing around */ |
| 5497 | so = NULL; |
| 5498 | else |
| 5499 | so = inp->sctp_socket; |
| 5500 | |
| 5501 | /* |
| 5502 | * We used timer based freeing if a reader or writer is in the way. |
| 5503 | * So we first check if we are actually being called from a timer, |
| 5504 | * if so we abort early if a reader or writer is still in the way. |
| 5505 | */ |
| 5506 | if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) && |
| 5507 | (from_inpcbfree == SCTP_NORMAL_PROC)) { |
| 5508 | /* |
| 5509 | * is it the timer driving us? if so are the reader/writers |
| 5510 | * gone? |
| 5511 | */ |
| 5512 | if (stcb->asoc.refcnt) { |
| 5513 | /* nope, reader or writer in the way */ |
| 5514 | sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL); |
| 5515 | /* no asoc destroyed */ |
| 5516 | SCTP_TCB_UNLOCK(stcb); |
| 5517 | #ifdef SCTP_LOG_CLOSING |
| 5518 | sctp_log_closing(inp, stcb, 8); |
| 5519 | #endif |
| 5520 | return (0); |
| 5521 | } |
| 5522 | } |
Michael Tuexen | 61f6683 | 2020-02-09 23:15:19 +0100 | [diff] [blame] | 5523 | /* Now clean up any other timers */ |
Michael Tuexen | 5f97639 | 2020-02-11 21:20:07 +0100 | [diff] [blame] | 5524 | sctp_stop_association_timers(stcb, false); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5525 | /* Now the read queue needs to be cleaned up (only once) */ |
| 5526 | if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0) { |
Michael Tuexen | 348a36c | 2018-08-13 16:24:47 +0200 | [diff] [blame] | 5527 | SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_ABOUT_TO_BE_FREED); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5528 | SCTP_INP_READ_LOCK(inp); |
| 5529 | TAILQ_FOREACH(sq, &inp->read_queue, next) { |
| 5530 | if (sq->stcb == stcb) { |
| 5531 | sq->do_not_ref_stcb = 1; |
| 5532 | sq->sinfo_cumtsn = stcb->asoc.cumulative_tsn; |
| 5533 | /* If there is no end, there never |
| 5534 | * will be now. |
| 5535 | */ |
| 5536 | if (sq->end_added == 0) { |
| 5537 | /* Held for PD-API clear that. */ |
| 5538 | sq->pdapi_aborted = 1; |
| 5539 | sq->held_length = 0; |
| 5540 | if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT) && (so != NULL)) { |
| 5541 | /* |
| 5542 | * Need to add a PD-API aborted indication. |
| 5543 | * Setting the control_pdapi assures that it will |
| 5544 | * be added right after this msg. |
| 5545 | */ |
| 5546 | uint32_t strseq; |
| 5547 | stcb->asoc.control_pdapi = sq; |
Michael Tuexen | 00657ac | 2016-12-07 21:53:26 +0100 | [diff] [blame] | 5548 | strseq = (sq->sinfo_stream << 16) | (sq->mid & 0x0000ffff); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5549 | sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION, |
| 5550 | stcb, |
| 5551 | SCTP_PARTIAL_DELIVERY_ABORTED, |
| 5552 | (void *)&strseq, |
| 5553 | SCTP_SO_LOCKED); |
| 5554 | stcb->asoc.control_pdapi = NULL; |
| 5555 | } |
| 5556 | } |
| 5557 | /* Add an end to wake them */ |
| 5558 | sq->end_added = 1; |
| 5559 | } |
| 5560 | } |
| 5561 | SCTP_INP_READ_UNLOCK(inp); |
| 5562 | if (stcb->block_entry) { |
| 5563 | SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PCB, ECONNRESET); |
| 5564 | stcb->block_entry->error = ECONNRESET; |
| 5565 | stcb->block_entry = NULL; |
| 5566 | } |
| 5567 | } |
| 5568 | if ((stcb->asoc.refcnt) || (stcb->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE)) { |
| 5569 | /* Someone holds a reference OR the socket is unaccepted yet. |
| 5570 | */ |
Michael Tuexen | 8d6935d | 2022-05-15 01:51:24 +0200 | [diff] [blame] | 5571 | if ((stcb->asoc.refcnt) || |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5572 | (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || |
| 5573 | (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) { |
Michael Tuexen | 348a36c | 2018-08-13 16:24:47 +0200 | [diff] [blame] | 5574 | SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5575 | sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL); |
| 5576 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5577 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || |
| 5578 | (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) |
| 5579 | /* nothing around */ |
| 5580 | so = NULL; |
| 5581 | if (so) { |
| 5582 | /* Wake any reader/writers */ |
| 5583 | sctp_sorwakeup(inp, so); |
| 5584 | sctp_sowwakeup(inp, so); |
| 5585 | } |
Michael Tuexen | a8409b3 | 2021-09-02 01:50:45 +0200 | [diff] [blame] | 5586 | SCTP_TCB_UNLOCK(stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5587 | |
| 5588 | #ifdef SCTP_LOG_CLOSING |
| 5589 | sctp_log_closing(inp, stcb, 9); |
| 5590 | #endif |
| 5591 | /* no asoc destroyed */ |
| 5592 | return (0); |
| 5593 | } |
| 5594 | #ifdef SCTP_LOG_CLOSING |
| 5595 | sctp_log_closing(inp, stcb, 10); |
| 5596 | #endif |
| 5597 | /* When I reach here, no others want |
| 5598 | * to kill the assoc yet.. and I own |
| 5599 | * the lock. Now its possible an abort |
| 5600 | * comes in when I do the lock exchange |
| 5601 | * below to grab all the locks to do |
| 5602 | * the final take out. to prevent this |
| 5603 | * we increment the count, which will |
| 5604 | * start a timer and blow out above thus |
| 5605 | * assuring us that we hold exclusive |
| 5606 | * killing of the asoc. Note that |
| 5607 | * after getting back the TCB lock |
| 5608 | * we will go ahead and increment the |
| 5609 | * counter back up and stop any timer |
| 5610 | * a passing stranger may have started :-S |
| 5611 | */ |
| 5612 | if (from_inpcbfree == SCTP_NORMAL_PROC) { |
| 5613 | atomic_add_int(&stcb->asoc.refcnt, 1); |
| 5614 | |
| 5615 | SCTP_TCB_UNLOCK(stcb); |
| 5616 | SCTP_INP_INFO_WLOCK(); |
| 5617 | SCTP_INP_WLOCK(inp); |
| 5618 | SCTP_TCB_LOCK(stcb); |
| 5619 | } |
| 5620 | /* Double check the GONE flag */ |
| 5621 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || |
| 5622 | (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) |
| 5623 | /* nothing around */ |
| 5624 | so = NULL; |
| 5625 | |
Michael Tuexen | 1f49544 | 2020-03-25 16:41:21 +0100 | [diff] [blame] | 5626 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || |
| 5627 | (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { |
| 5628 | /* |
| 5629 | * For TCP type we need special handling when we are |
| 5630 | * connected. We also include the peel'ed off ones to. |
| 5631 | */ |
| 5632 | if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { |
| 5633 | inp->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED; |
| 5634 | inp->sctp_flags |= SCTP_PCB_FLAGS_WAS_CONNECTED; |
| 5635 | if (so) { |
| 5636 | SOCKBUF_LOCK(&so->so_rcv); |
| 5637 | so->so_state &= ~(SS_ISCONNECTING | |
| 5638 | SS_ISDISCONNECTING | |
| 5639 | SS_ISCONFIRMING | |
| 5640 | SS_ISCONNECTED); |
| 5641 | so->so_state |= SS_ISDISCONNECTED; |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 5642 | #if defined(__APPLE__) && !defined(__Userspace__) |
Michael Tuexen | 1f49544 | 2020-03-25 16:41:21 +0100 | [diff] [blame] | 5643 | socantrcvmore(so); |
| 5644 | #else |
| 5645 | socantrcvmore_locked(so); |
| 5646 | #endif |
| 5647 | socantsendmore(so); |
| 5648 | sctp_sowwakeup(inp, so); |
| 5649 | sctp_sorwakeup(inp, so); |
| 5650 | SCTP_SOWAKEUP(so); |
| 5651 | } |
| 5652 | } |
| 5653 | } |
| 5654 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5655 | /* Make it invalid too, that way if its |
| 5656 | * about to run it will abort and return. |
| 5657 | */ |
| 5658 | /* re-increment the lock */ |
| 5659 | if (from_inpcbfree == SCTP_NORMAL_PROC) { |
Michael Tuexen | a106ab8 | 2021-11-26 14:03:15 +0100 | [diff] [blame] | 5660 | atomic_subtract_int(&stcb->asoc.refcnt, 1); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5661 | } |
| 5662 | if (stcb->asoc.refcnt) { |
Michael Tuexen | 348a36c | 2018-08-13 16:24:47 +0200 | [diff] [blame] | 5663 | SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5664 | sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL); |
| 5665 | if (from_inpcbfree == SCTP_NORMAL_PROC) { |
| 5666 | SCTP_INP_INFO_WUNLOCK(); |
| 5667 | SCTP_INP_WUNLOCK(inp); |
| 5668 | } |
| 5669 | SCTP_TCB_UNLOCK(stcb); |
| 5670 | return (0); |
| 5671 | } |
| 5672 | asoc->state = 0; |
| 5673 | if (inp->sctp_tcbhash) { |
| 5674 | LIST_REMOVE(stcb, sctp_tcbhash); |
| 5675 | } |
| 5676 | if (stcb->asoc.in_asocid_hash) { |
| 5677 | LIST_REMOVE(stcb, sctp_tcbasocidhash); |
| 5678 | } |
Michael Tuexen | a8409b3 | 2021-09-02 01:50:45 +0200 | [diff] [blame] | 5679 | if (inp->sctp_socket == NULL) { |
| 5680 | stcb->sctp_socket = NULL; |
| 5681 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5682 | /* Now lets remove it from the list of ALL associations in the EP */ |
| 5683 | LIST_REMOVE(stcb, sctp_tcblist); |
| 5684 | if (from_inpcbfree == SCTP_NORMAL_PROC) { |
| 5685 | SCTP_INP_INCR_REF(inp); |
| 5686 | SCTP_INP_WUNLOCK(inp); |
| 5687 | } |
| 5688 | /* pull from vtag hash */ |
| 5689 | LIST_REMOVE(stcb, sctp_asocs); |
Michael Tuexen | 4ec13f7 | 2021-09-09 01:22:43 +0200 | [diff] [blame] | 5690 | sctp_add_vtag_to_timewait(asoc->my_vtag, inp->sctp_lport, stcb->rport); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5691 | |
| 5692 | /* Now restop the timers to be sure |
| 5693 | * this is paranoia at is finest! |
| 5694 | */ |
Michael Tuexen | 5f97639 | 2020-02-11 21:20:07 +0100 | [diff] [blame] | 5695 | sctp_stop_association_timers(stcb, true); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5696 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5697 | /* |
| 5698 | * The chunk lists and such SHOULD be empty but we check them just |
| 5699 | * in case. |
| 5700 | */ |
| 5701 | /* anything on the wheel needs to be removed */ |
| 5702 | for (i = 0; i < asoc->streamoutcnt; i++) { |
| 5703 | struct sctp_stream_out *outs; |
| 5704 | |
| 5705 | outs = &asoc->strmout[i]; |
| 5706 | /* now clean up any chunks here */ |
| 5707 | TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) { |
Michael Tuexen | c6ae6c6 | 2016-08-07 15:17:05 +0200 | [diff] [blame] | 5708 | atomic_subtract_int(&asoc->stream_queue_cnt, 1); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5709 | TAILQ_REMOVE(&outs->outqueue, sp, next); |
Michael Tuexen | 5e95fea | 2021-09-21 16:39:42 +0200 | [diff] [blame] | 5710 | stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, outs, sp); |
tuexen | 30ca817 | 2012-07-17 13:09:48 +0000 | [diff] [blame] | 5711 | sctp_free_spbufspace(stcb, asoc, sp); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5712 | if (sp->data) { |
| 5713 | if (so) { |
| 5714 | /* Still an open socket - report */ |
| 5715 | sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb, |
tuexen | da53ff0 | 2012-05-14 09:00:59 +0000 | [diff] [blame] | 5716 | 0, (void *)sp, SCTP_SO_LOCKED); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5717 | } |
| 5718 | if (sp->data) { |
| 5719 | sctp_m_freem(sp->data); |
| 5720 | sp->data = NULL; |
| 5721 | sp->tail_mbuf = NULL; |
tuexen | 30ca817 | 2012-07-17 13:09:48 +0000 | [diff] [blame] | 5722 | sp->length = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5723 | } |
| 5724 | } |
| 5725 | if (sp->net) { |
| 5726 | sctp_free_remote_addr(sp->net); |
| 5727 | sp->net = NULL; |
| 5728 | } |
tuexen | 30ca817 | 2012-07-17 13:09:48 +0000 | [diff] [blame] | 5729 | sctp_free_a_strmoq(stcb, sp, SCTP_SO_LOCKED); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5730 | } |
| 5731 | } |
| 5732 | /*sa_ignore FREED_MEMORY*/ |
| 5733 | TAILQ_FOREACH_SAFE(strrst, &asoc->resetHead, next_resp, nstrrst) { |
| 5734 | TAILQ_REMOVE(&asoc->resetHead, strrst, next_resp); |
| 5735 | SCTP_FREE(strrst, SCTP_M_STRESET); |
| 5736 | } |
| 5737 | TAILQ_FOREACH_SAFE(sq, &asoc->pending_reply_queue, next, nsq) { |
| 5738 | TAILQ_REMOVE(&asoc->pending_reply_queue, sq, next); |
| 5739 | if (sq->data) { |
| 5740 | sctp_m_freem(sq->data); |
| 5741 | sq->data = NULL; |
| 5742 | } |
| 5743 | sctp_free_remote_addr(sq->whoFrom); |
| 5744 | sq->whoFrom = NULL; |
| 5745 | sq->stcb = NULL; |
| 5746 | /* Free the ctl entry */ |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 5747 | sctp_free_a_readq(stcb, sq); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5748 | /*sa_ignore FREED_MEMORY*/ |
| 5749 | } |
| 5750 | TAILQ_FOREACH_SAFE(chk, &asoc->free_chunks, sctp_next, nchk) { |
| 5751 | TAILQ_REMOVE(&asoc->free_chunks, chk, sctp_next); |
| 5752 | if (chk->data) { |
| 5753 | sctp_m_freem(chk->data); |
| 5754 | chk->data = NULL; |
| 5755 | } |
| 5756 | if (chk->holds_key_ref) |
| 5757 | sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); |
| 5758 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); |
| 5759 | SCTP_DECR_CHK_COUNT(); |
| 5760 | atomic_subtract_int(&SCTP_BASE_INFO(ipi_free_chunks), 1); |
| 5761 | asoc->free_chunk_cnt--; |
| 5762 | /*sa_ignore FREED_MEMORY*/ |
| 5763 | } |
| 5764 | /* pending send queue SHOULD be empty */ |
| 5765 | TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) { |
Michael Tuexen | 00657ac | 2016-12-07 21:53:26 +0100 | [diff] [blame] | 5766 | if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) { |
| 5767 | asoc->strmout[chk->rec.data.sid].chunks_on_queues--; |
t00fcxen | 8fcc514 | 2012-11-16 19:46:12 +0000 | [diff] [blame] | 5768 | #ifdef INVARIANTS |
| 5769 | } else { |
Michael Tuexen | 00657ac | 2016-12-07 21:53:26 +0100 | [diff] [blame] | 5770 | panic("No chunks on the queues for sid %u.", chk->rec.data.sid); |
t00fcxen | 8fcc514 | 2012-11-16 19:46:12 +0000 | [diff] [blame] | 5771 | #endif |
| 5772 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5773 | TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next); |
| 5774 | if (chk->data) { |
| 5775 | if (so) { |
| 5776 | /* Still a socket? */ |
tuexen | da53ff0 | 2012-05-14 09:00:59 +0000 | [diff] [blame] | 5777 | sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, |
| 5778 | 0, chk, SCTP_SO_LOCKED); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5779 | } |
| 5780 | if (chk->data) { |
| 5781 | sctp_m_freem(chk->data); |
| 5782 | chk->data = NULL; |
| 5783 | } |
| 5784 | } |
| 5785 | if (chk->holds_key_ref) |
| 5786 | sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); |
| 5787 | if (chk->whoTo) { |
| 5788 | sctp_free_remote_addr(chk->whoTo); |
| 5789 | chk->whoTo = NULL; |
| 5790 | } |
| 5791 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); |
| 5792 | SCTP_DECR_CHK_COUNT(); |
| 5793 | /*sa_ignore FREED_MEMORY*/ |
| 5794 | } |
| 5795 | /* sent queue SHOULD be empty */ |
| 5796 | TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) { |
t00fcxen | 8fcc514 | 2012-11-16 19:46:12 +0000 | [diff] [blame] | 5797 | if (chk->sent != SCTP_DATAGRAM_NR_ACKED) { |
Michael Tuexen | 00657ac | 2016-12-07 21:53:26 +0100 | [diff] [blame] | 5798 | if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) { |
| 5799 | asoc->strmout[chk->rec.data.sid].chunks_on_queues--; |
t00fcxen | 9ad9077 | 2012-11-07 22:19:57 +0000 | [diff] [blame] | 5800 | #ifdef INVARIANTS |
| 5801 | } else { |
Michael Tuexen | 00657ac | 2016-12-07 21:53:26 +0100 | [diff] [blame] | 5802 | panic("No chunks on the queues for sid %u.", chk->rec.data.sid); |
t00fcxen | 9ad9077 | 2012-11-07 22:19:57 +0000 | [diff] [blame] | 5803 | #endif |
| 5804 | } |
| 5805 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5806 | TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next); |
| 5807 | if (chk->data) { |
| 5808 | if (so) { |
| 5809 | /* Still a socket? */ |
tuexen | da53ff0 | 2012-05-14 09:00:59 +0000 | [diff] [blame] | 5810 | sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, |
| 5811 | 0, chk, SCTP_SO_LOCKED); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5812 | } |
| 5813 | if (chk->data) { |
| 5814 | sctp_m_freem(chk->data); |
| 5815 | chk->data = NULL; |
| 5816 | } |
| 5817 | } |
| 5818 | if (chk->holds_key_ref) |
| 5819 | sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); |
| 5820 | sctp_free_remote_addr(chk->whoTo); |
| 5821 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); |
| 5822 | SCTP_DECR_CHK_COUNT(); |
| 5823 | /*sa_ignore FREED_MEMORY*/ |
| 5824 | } |
t00fcxen | 8fcc514 | 2012-11-16 19:46:12 +0000 | [diff] [blame] | 5825 | #ifdef INVARIANTS |
| 5826 | for (i = 0; i < stcb->asoc.streamoutcnt; i++) { |
| 5827 | if (stcb->asoc.strmout[i].chunks_on_queues > 0) { |
| 5828 | panic("%u chunks left for stream %u.", stcb->asoc.strmout[i].chunks_on_queues, i); |
| 5829 | } |
| 5830 | } |
| 5831 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5832 | /* control queue MAY not be empty */ |
| 5833 | TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) { |
| 5834 | TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next); |
| 5835 | if (chk->data) { |
| 5836 | sctp_m_freem(chk->data); |
| 5837 | chk->data = NULL; |
| 5838 | } |
| 5839 | if (chk->holds_key_ref) |
| 5840 | sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); |
| 5841 | sctp_free_remote_addr(chk->whoTo); |
| 5842 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); |
| 5843 | SCTP_DECR_CHK_COUNT(); |
| 5844 | /*sa_ignore FREED_MEMORY*/ |
| 5845 | } |
| 5846 | /* ASCONF queue MAY not be empty */ |
| 5847 | TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) { |
| 5848 | TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next); |
| 5849 | if (chk->data) { |
| 5850 | sctp_m_freem(chk->data); |
| 5851 | chk->data = NULL; |
| 5852 | } |
| 5853 | if (chk->holds_key_ref) |
| 5854 | sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); |
| 5855 | sctp_free_remote_addr(chk->whoTo); |
| 5856 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); |
| 5857 | SCTP_DECR_CHK_COUNT(); |
| 5858 | /*sa_ignore FREED_MEMORY*/ |
| 5859 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5860 | if (asoc->mapping_array) { |
| 5861 | SCTP_FREE(asoc->mapping_array, SCTP_M_MAP); |
| 5862 | asoc->mapping_array = NULL; |
| 5863 | } |
| 5864 | if (asoc->nr_mapping_array) { |
| 5865 | SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP); |
| 5866 | asoc->nr_mapping_array = NULL; |
| 5867 | } |
| 5868 | /* the stream outs */ |
| 5869 | if (asoc->strmout) { |
| 5870 | SCTP_FREE(asoc->strmout, SCTP_M_STRMO); |
| 5871 | asoc->strmout = NULL; |
| 5872 | } |
| 5873 | asoc->strm_realoutsize = asoc->streamoutcnt = 0; |
| 5874 | if (asoc->strmin) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5875 | for (i = 0; i < asoc->streamincnt; i++) { |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 5876 | sctp_clean_up_stream(stcb, &asoc->strmin[i].inqueue); |
| 5877 | sctp_clean_up_stream(stcb, &asoc->strmin[i].uno_inqueue); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5878 | } |
| 5879 | SCTP_FREE(asoc->strmin, SCTP_M_STRMI); |
| 5880 | asoc->strmin = NULL; |
| 5881 | } |
| 5882 | asoc->streamincnt = 0; |
| 5883 | TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) { |
| 5884 | #ifdef INVARIANTS |
| 5885 | if (SCTP_BASE_INFO(ipi_count_raddr) == 0) { |
| 5886 | panic("no net's left alloc'ed, or list points to itself"); |
| 5887 | } |
| 5888 | #endif |
| 5889 | TAILQ_REMOVE(&asoc->nets, net, sctp_next); |
| 5890 | sctp_free_remote_addr(net); |
| 5891 | } |
| 5892 | LIST_FOREACH_SAFE(laddr, &asoc->sctp_restricted_addrs, sctp_nxt_addr, naddr) { |
| 5893 | /*sa_ignore FREED_MEMORY*/ |
| 5894 | sctp_remove_laddr(laddr); |
| 5895 | } |
| 5896 | |
| 5897 | /* pending asconf (address) parameters */ |
| 5898 | TAILQ_FOREACH_SAFE(aparam, &asoc->asconf_queue, next, naparam) { |
| 5899 | /*sa_ignore FREED_MEMORY*/ |
| 5900 | TAILQ_REMOVE(&asoc->asconf_queue, aparam, next); |
| 5901 | SCTP_FREE(aparam,SCTP_M_ASC_ADDR); |
| 5902 | } |
| 5903 | TAILQ_FOREACH_SAFE(aack, &asoc->asconf_ack_sent, next, naack) { |
| 5904 | /*sa_ignore FREED_MEMORY*/ |
| 5905 | TAILQ_REMOVE(&asoc->asconf_ack_sent, aack, next); |
| 5906 | if (aack->data != NULL) { |
| 5907 | sctp_m_freem(aack->data); |
| 5908 | } |
| 5909 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asconf_ack), aack); |
| 5910 | } |
| 5911 | /* clean up auth stuff */ |
| 5912 | if (asoc->local_hmacs) |
| 5913 | sctp_free_hmaclist(asoc->local_hmacs); |
| 5914 | if (asoc->peer_hmacs) |
| 5915 | sctp_free_hmaclist(asoc->peer_hmacs); |
| 5916 | |
| 5917 | if (asoc->local_auth_chunks) |
| 5918 | sctp_free_chunklist(asoc->local_auth_chunks); |
| 5919 | if (asoc->peer_auth_chunks) |
| 5920 | sctp_free_chunklist(asoc->peer_auth_chunks); |
| 5921 | |
| 5922 | sctp_free_authinfo(&asoc->authinfo); |
| 5923 | |
| 5924 | LIST_FOREACH_SAFE(shared_key, &asoc->shared_keys, next, nshared_key) { |
| 5925 | LIST_REMOVE(shared_key, next); |
| 5926 | sctp_free_sharedkey(shared_key); |
| 5927 | /*sa_ignore FREED_MEMORY*/ |
| 5928 | } |
| 5929 | |
| 5930 | /* Insert new items here :> */ |
| 5931 | |
| 5932 | /* Get rid of LOCK */ |
t00fcxen | 2c2ff92 | 2013-11-03 14:03:35 +0000 | [diff] [blame] | 5933 | SCTP_TCB_UNLOCK(stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5934 | SCTP_TCB_LOCK_DESTROY(stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5935 | if (from_inpcbfree == SCTP_NORMAL_PROC) { |
| 5936 | SCTP_INP_INFO_WUNLOCK(); |
| 5937 | SCTP_INP_RLOCK(inp); |
| 5938 | } |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 5939 | #if defined(__APPLE__) && !defined(__Userspace__) |
| 5940 | /* TEMP CODE */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5941 | stcb->freed_from_where = from_location; |
| 5942 | #endif |
| 5943 | #ifdef SCTP_TRACK_FREED_ASOCS |
| 5944 | if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { |
| 5945 | /* now clean up the tasoc itself */ |
| 5946 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb); |
| 5947 | SCTP_DECR_ASOC_COUNT(); |
| 5948 | } else { |
| 5949 | LIST_INSERT_HEAD(&inp->sctp_asoc_free_list, stcb, sctp_tcblist); |
| 5950 | } |
| 5951 | #else |
| 5952 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb); |
| 5953 | SCTP_DECR_ASOC_COUNT(); |
| 5954 | #endif |
| 5955 | if (from_inpcbfree == SCTP_NORMAL_PROC) { |
| 5956 | if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { |
| 5957 | /* If its NOT the inp_free calling us AND |
| 5958 | * sctp_close as been called, we |
| 5959 | * call back... |
| 5960 | */ |
| 5961 | SCTP_INP_RUNLOCK(inp); |
| 5962 | /* This will start the kill timer (if we are |
| 5963 | * the last one) since we hold an increment yet. But |
| 5964 | * this is the only safe way to do this |
| 5965 | * since otherwise if the socket closes |
| 5966 | * at the same time we are here we might |
| 5967 | * collide in the cleanup. |
| 5968 | */ |
| 5969 | sctp_inpcb_free(inp, |
| 5970 | SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE, |
| 5971 | SCTP_CALLED_DIRECTLY_NOCMPSET); |
| 5972 | SCTP_INP_DECR_REF(inp); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5973 | } else { |
| 5974 | /* The socket is still open. */ |
| 5975 | SCTP_INP_DECR_REF(inp); |
Michael Tuexen | c6c994c | 2020-05-18 21:50:12 +0200 | [diff] [blame] | 5976 | SCTP_INP_RUNLOCK(inp); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5977 | } |
| 5978 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5979 | /* destroyed the asoc */ |
| 5980 | #ifdef SCTP_LOG_CLOSING |
| 5981 | sctp_log_closing(inp, NULL, 11); |
| 5982 | #endif |
| 5983 | return (1); |
| 5984 | } |
| 5985 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5986 | /* |
| 5987 | * determine if a destination is "reachable" based upon the addresses bound |
| 5988 | * to the current endpoint (e.g. only v4 or v6 currently bound) |
| 5989 | */ |
| 5990 | /* |
| 5991 | * FIX: if we allow assoc-level bindx(), then this needs to be fixed to use |
| 5992 | * assoc level v4/v6 flags, as the assoc *may* not have the same address |
| 5993 | * types bound as its endpoint |
| 5994 | */ |
| 5995 | int |
| 5996 | sctp_destination_is_reachable(struct sctp_tcb *stcb, struct sockaddr *destaddr) |
| 5997 | { |
| 5998 | struct sctp_inpcb *inp; |
| 5999 | int answer; |
| 6000 | |
| 6001 | /* |
| 6002 | * No locks here, the TCB, in all cases is already locked and an |
| 6003 | * assoc is up. There is either a INP lock by the caller applied (in |
| 6004 | * asconf case when deleting an address) or NOT in the HB case, |
| 6005 | * however if HB then the INP increment is up and the INP will not |
| 6006 | * be removed (on top of the fact that we have a TCB lock). So we |
| 6007 | * only want to read the sctp_flags, which is either bound-all or |
| 6008 | * not.. no protection needed since once an assoc is up you can't be |
| 6009 | * changing your binding. |
| 6010 | */ |
| 6011 | inp = stcb->sctp_ep; |
| 6012 | if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { |
| 6013 | /* if bound all, destination is not restricted */ |
| 6014 | /* |
| 6015 | * RRS: Question during lock work: Is this correct? If you |
| 6016 | * are bound-all you still might need to obey the V4--V6 |
| 6017 | * flags??? IMO this bound-all stuff needs to be removed! |
| 6018 | */ |
| 6019 | return (1); |
| 6020 | } |
| 6021 | /* NOTE: all "scope" checks are done when local addresses are added */ |
| 6022 | switch (destaddr->sa_family) { |
tuexen | 310f1bc | 2012-07-15 11:20:56 +0000 | [diff] [blame] | 6023 | #ifdef INET6 |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6024 | case AF_INET6: |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6025 | answer = inp->ip_inp.inp.inp_vflag & INP_IPV6; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6026 | break; |
tuexen | 310f1bc | 2012-07-15 11:20:56 +0000 | [diff] [blame] | 6027 | #endif |
| 6028 | #ifdef INET |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6029 | case AF_INET: |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6030 | answer = inp->ip_inp.inp.inp_vflag & INP_IPV4; |
tuexen | 4657054 | 2012-07-14 20:37:24 +0000 | [diff] [blame] | 6031 | break; |
tuexen | 310f1bc | 2012-07-15 11:20:56 +0000 | [diff] [blame] | 6032 | #endif |
tuexen | 5d9b974 | 2012-07-11 21:52:01 +0000 | [diff] [blame] | 6033 | #if defined(__Userspace__) |
| 6034 | case AF_CONN: |
| 6035 | answer = inp->ip_inp.inp.inp_vflag & INP_CONN; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6036 | break; |
tuexen | 5d9b974 | 2012-07-11 21:52:01 +0000 | [diff] [blame] | 6037 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6038 | default: |
| 6039 | /* invalid family, so it's unreachable */ |
| 6040 | answer = 0; |
| 6041 | break; |
| 6042 | } |
| 6043 | return (answer); |
| 6044 | } |
| 6045 | |
| 6046 | /* |
| 6047 | * update the inp_vflags on an endpoint |
| 6048 | */ |
| 6049 | static void |
| 6050 | sctp_update_ep_vflag(struct sctp_inpcb *inp) |
| 6051 | { |
| 6052 | struct sctp_laddr *laddr; |
| 6053 | |
| 6054 | /* first clear the flag */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6055 | inp->ip_inp.inp.inp_vflag = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6056 | /* set the flag based on addresses on the ep list */ |
| 6057 | LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { |
| 6058 | if (laddr->ifa == NULL) { |
| 6059 | SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n", |
Saúl Ibarra Corretgé | 8b04b41 | 2015-09-26 09:53:06 +0200 | [diff] [blame] | 6060 | __func__); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6061 | continue; |
| 6062 | } |
| 6063 | |
| 6064 | if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) { |
| 6065 | continue; |
| 6066 | } |
| 6067 | switch (laddr->ifa->address.sa.sa_family) { |
| 6068 | #ifdef INET6 |
| 6069 | case AF_INET6: |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6070 | inp->ip_inp.inp.inp_vflag |= INP_IPV6; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6071 | break; |
| 6072 | #endif |
| 6073 | #ifdef INET |
| 6074 | case AF_INET: |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6075 | inp->ip_inp.inp.inp_vflag |= INP_IPV4; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6076 | break; |
| 6077 | #endif |
tuexen | 5d9b974 | 2012-07-11 21:52:01 +0000 | [diff] [blame] | 6078 | #if defined(__Userspace__) |
| 6079 | case AF_CONN: |
| 6080 | inp->ip_inp.inp.inp_vflag |= INP_CONN; |
| 6081 | break; |
| 6082 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6083 | default: |
| 6084 | break; |
| 6085 | } |
| 6086 | } |
| 6087 | } |
| 6088 | |
| 6089 | /* |
| 6090 | * Add the address to the endpoint local address list There is nothing to be |
| 6091 | * done if we are bound to all addresses |
| 6092 | */ |
| 6093 | void |
| 6094 | sctp_add_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa, uint32_t action) |
| 6095 | { |
| 6096 | struct sctp_laddr *laddr; |
Michael Tuexen | d75dc0c | 2016-03-28 21:38:43 +0200 | [diff] [blame] | 6097 | struct sctp_tcb *stcb; |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 6098 | int fnd, error = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6099 | |
| 6100 | fnd = 0; |
| 6101 | |
| 6102 | if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { |
| 6103 | /* You are already bound to all. You have it already */ |
| 6104 | return; |
| 6105 | } |
| 6106 | #ifdef INET6 |
| 6107 | if (ifa->address.sa.sa_family == AF_INET6) { |
| 6108 | if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) { |
| 6109 | /* Can't bind a non-useable addr. */ |
| 6110 | return; |
| 6111 | } |
| 6112 | } |
| 6113 | #endif |
| 6114 | /* first, is it already present? */ |
| 6115 | LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { |
| 6116 | if (laddr->ifa == ifa) { |
| 6117 | fnd = 1; |
| 6118 | break; |
| 6119 | } |
| 6120 | } |
| 6121 | |
| 6122 | if (fnd == 0) { |
| 6123 | /* Not in the ep list */ |
| 6124 | error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, action); |
| 6125 | if (error != 0) |
| 6126 | return; |
| 6127 | inp->laddr_count++; |
| 6128 | /* update inp_vflag flags */ |
| 6129 | switch (ifa->address.sa.sa_family) { |
| 6130 | #ifdef INET6 |
| 6131 | case AF_INET6: |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6132 | inp->ip_inp.inp.inp_vflag |= INP_IPV6; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6133 | break; |
| 6134 | #endif |
tuexen | 310f1bc | 2012-07-15 11:20:56 +0000 | [diff] [blame] | 6135 | #ifdef INET |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6136 | case AF_INET: |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6137 | inp->ip_inp.inp.inp_vflag |= INP_IPV4; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6138 | break; |
| 6139 | #endif |
tuexen | 5d9b974 | 2012-07-11 21:52:01 +0000 | [diff] [blame] | 6140 | #if defined(__Userspace__) |
| 6141 | case AF_CONN: |
| 6142 | inp->ip_inp.inp.inp_vflag |= INP_CONN; |
| 6143 | break; |
| 6144 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6145 | default: |
| 6146 | break; |
| 6147 | } |
Michael Tuexen | d75dc0c | 2016-03-28 21:38:43 +0200 | [diff] [blame] | 6148 | LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { |
| 6149 | sctp_add_local_addr_restricted(stcb, ifa); |
| 6150 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6151 | } |
| 6152 | return; |
| 6153 | } |
| 6154 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6155 | /* |
| 6156 | * select a new (hopefully reachable) destination net (should only be used |
| 6157 | * when we deleted an ep addr that is the only usable source address to reach |
| 6158 | * the destination net) |
| 6159 | */ |
| 6160 | static void |
| 6161 | sctp_select_primary_destination(struct sctp_tcb *stcb) |
| 6162 | { |
| 6163 | struct sctp_nets *net; |
| 6164 | |
| 6165 | TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { |
| 6166 | /* for now, we'll just pick the first reachable one we find */ |
| 6167 | if (net->dest_state & SCTP_ADDR_UNCONFIRMED) |
| 6168 | continue; |
| 6169 | if (sctp_destination_is_reachable(stcb, |
| 6170 | (struct sockaddr *)&net->ro._l_addr)) { |
| 6171 | /* found a reachable destination */ |
| 6172 | stcb->asoc.primary_destination = net; |
| 6173 | } |
| 6174 | } |
| 6175 | /* I can't there from here! ...we're gonna die shortly... */ |
| 6176 | } |
| 6177 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6178 | /* |
Michael Tuexen | 24e6c73 | 2016-02-19 12:28:21 +0100 | [diff] [blame] | 6179 | * Delete the address from the endpoint local address list. There is nothing |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6180 | * to be done if we are bound to all addresses |
| 6181 | */ |
| 6182 | void |
| 6183 | sctp_del_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa) |
| 6184 | { |
| 6185 | struct sctp_laddr *laddr; |
| 6186 | int fnd; |
| 6187 | |
| 6188 | fnd = 0; |
| 6189 | if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { |
| 6190 | /* You are already bound to all. You have it already */ |
| 6191 | return; |
| 6192 | } |
| 6193 | LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { |
| 6194 | if (laddr->ifa == ifa) { |
| 6195 | fnd = 1; |
| 6196 | break; |
| 6197 | } |
| 6198 | } |
| 6199 | if (fnd && (inp->laddr_count < 2)) { |
| 6200 | /* can't delete unless there are at LEAST 2 addresses */ |
| 6201 | return; |
| 6202 | } |
| 6203 | if (fnd) { |
| 6204 | /* |
| 6205 | * clean up any use of this address go through our |
| 6206 | * associations and clear any last_used_address that match |
| 6207 | * this one for each assoc, see if a new primary_destination |
| 6208 | * is needed |
| 6209 | */ |
| 6210 | struct sctp_tcb *stcb; |
| 6211 | |
| 6212 | /* clean up "next_addr_touse" */ |
| 6213 | if (inp->next_addr_touse == laddr) |
| 6214 | /* delete this address */ |
| 6215 | inp->next_addr_touse = NULL; |
| 6216 | |
| 6217 | /* clean up "last_used_address" */ |
| 6218 | LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { |
| 6219 | struct sctp_nets *net; |
Michael Tuexen | 24e6c73 | 2016-02-19 12:28:21 +0100 | [diff] [blame] | 6220 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6221 | SCTP_TCB_LOCK(stcb); |
| 6222 | if (stcb->asoc.last_used_address == laddr) |
| 6223 | /* delete this address */ |
| 6224 | stcb->asoc.last_used_address = NULL; |
| 6225 | /* Now spin through all the nets and purge any ref to laddr */ |
| 6226 | TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { |
Michael Tuexen | 24e6c73 | 2016-02-19 12:28:21 +0100 | [diff] [blame] | 6227 | if (net->ro._s_addr == laddr->ifa) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6228 | /* Yep, purge src address selected */ |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 6229 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 6230 | RO_NHFREE(&net->ro); |
| 6231 | #else |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6232 | sctp_rtentry_t *rt; |
| 6233 | |
| 6234 | /* delete this address if cached */ |
| 6235 | rt = net->ro.ro_rt; |
| 6236 | if (rt != NULL) { |
| 6237 | RTFREE(rt); |
| 6238 | net->ro.ro_rt = NULL; |
| 6239 | } |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 6240 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6241 | sctp_free_ifa(net->ro._s_addr); |
| 6242 | net->ro._s_addr = NULL; |
| 6243 | net->src_addr_selected = 0; |
| 6244 | } |
| 6245 | } |
| 6246 | SCTP_TCB_UNLOCK(stcb); |
| 6247 | } /* for each tcb */ |
| 6248 | /* remove it from the ep list */ |
| 6249 | sctp_remove_laddr(laddr); |
| 6250 | inp->laddr_count--; |
| 6251 | /* update inp_vflag flags */ |
| 6252 | sctp_update_ep_vflag(inp); |
| 6253 | } |
| 6254 | return; |
| 6255 | } |
| 6256 | |
| 6257 | /* |
| 6258 | * Add the address to the TCB local address restricted list. |
| 6259 | * This is a "pending" address list (eg. addresses waiting for an |
| 6260 | * ASCONF-ACK response) and cannot be used as a valid source address. |
| 6261 | */ |
| 6262 | void |
| 6263 | sctp_add_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa) |
| 6264 | { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6265 | struct sctp_laddr *laddr; |
| 6266 | struct sctpladdr *list; |
| 6267 | |
| 6268 | /* |
| 6269 | * Assumes TCB is locked.. and possibly the INP. May need to |
| 6270 | * confirm/fix that if we need it and is not the case. |
| 6271 | */ |
| 6272 | list = &stcb->asoc.sctp_restricted_addrs; |
| 6273 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6274 | #ifdef INET6 |
| 6275 | if (ifa->address.sa.sa_family == AF_INET6) { |
| 6276 | if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) { |
| 6277 | /* Can't bind a non-existent addr. */ |
| 6278 | return; |
| 6279 | } |
| 6280 | } |
| 6281 | #endif |
| 6282 | /* does the address already exist? */ |
| 6283 | LIST_FOREACH(laddr, list, sctp_nxt_addr) { |
| 6284 | if (laddr->ifa == ifa) { |
| 6285 | return; |
| 6286 | } |
| 6287 | } |
| 6288 | |
| 6289 | /* add to the list */ |
| 6290 | (void)sctp_insert_laddr(list, ifa, 0); |
| 6291 | return; |
| 6292 | } |
| 6293 | |
| 6294 | /* |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6295 | * Remove a local address from the TCB local address restricted list |
| 6296 | */ |
| 6297 | void |
| 6298 | sctp_del_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa) |
| 6299 | { |
| 6300 | struct sctp_inpcb *inp; |
| 6301 | struct sctp_laddr *laddr; |
| 6302 | |
| 6303 | /* |
| 6304 | * This is called by asconf work. It is assumed that a) The TCB is |
| 6305 | * locked and b) The INP is locked. This is true in as much as I can |
| 6306 | * trace through the entry asconf code where I did these locks. |
| 6307 | * Again, the ASCONF code is a bit different in that it does lock |
| 6308 | * the INP during its work often times. This must be since we don't |
| 6309 | * want other proc's looking up things while what they are looking |
| 6310 | * up is changing :-D |
| 6311 | */ |
| 6312 | |
| 6313 | inp = stcb->sctp_ep; |
| 6314 | /* if subset bound and don't allow ASCONF's, can't delete last */ |
| 6315 | if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) && |
| 6316 | sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF)) { |
| 6317 | if (stcb->sctp_ep->laddr_count < 2) { |
| 6318 | /* can't delete last address */ |
| 6319 | return; |
| 6320 | } |
| 6321 | } |
| 6322 | LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) { |
| 6323 | /* remove the address if it exists */ |
| 6324 | if (laddr->ifa == NULL) |
| 6325 | continue; |
| 6326 | if (laddr->ifa == ifa) { |
| 6327 | sctp_remove_laddr(laddr); |
| 6328 | return; |
| 6329 | } |
| 6330 | } |
| 6331 | |
| 6332 | /* address not found! */ |
| 6333 | return; |
| 6334 | } |
| 6335 | |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 6336 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6337 | /* sysctl */ |
| 6338 | static int sctp_max_number_of_assoc = SCTP_MAX_NUM_OF_ASOC; |
| 6339 | static int sctp_scale_up_for_address = SCTP_SCALE_FOR_ADDR; |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 6340 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6341 | |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 6342 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 6343 | #if defined(SCTP_MCORE_INPUT) && defined(SMP) |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 6344 | struct sctp_mcore_ctrl *sctp_mcore_workers = NULL; |
| 6345 | int *sctp_cpuarry = NULL; |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 6346 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6347 | void |
| 6348 | sctp_queue_to_mcore(struct mbuf *m, int off, int cpu_to_use) |
| 6349 | { |
| 6350 | /* Queue a packet to a processor for the specified core */ |
| 6351 | struct sctp_mcore_queue *qent; |
| 6352 | struct sctp_mcore_ctrl *wkq; |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 6353 | int need_wake = 0; |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 6354 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6355 | if (sctp_mcore_workers == NULL) { |
| 6356 | /* Something went way bad during setup */ |
| 6357 | sctp_input_with_port(m, off, 0); |
| 6358 | return; |
| 6359 | } |
| 6360 | SCTP_MALLOC(qent, struct sctp_mcore_queue *, |
| 6361 | (sizeof(struct sctp_mcore_queue)), |
| 6362 | SCTP_M_MCORE); |
| 6363 | if (qent == NULL) { |
| 6364 | /* This is trouble */ |
| 6365 | sctp_input_with_port(m, off, 0); |
| 6366 | return; |
| 6367 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6368 | qent->vn = curvnet; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6369 | qent->m = m; |
| 6370 | qent->off = off; |
| 6371 | qent->v6 = 0; |
| 6372 | wkq = &sctp_mcore_workers[cpu_to_use]; |
| 6373 | SCTP_MCORE_QLOCK(wkq); |
| 6374 | |
| 6375 | TAILQ_INSERT_TAIL(&wkq->que, qent, next); |
| 6376 | if (wkq->running == 0) { |
| 6377 | need_wake = 1; |
| 6378 | } |
| 6379 | SCTP_MCORE_QUNLOCK(wkq); |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 6380 | if (need_wake) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6381 | wakeup(&wkq->running); |
| 6382 | } |
| 6383 | } |
| 6384 | |
| 6385 | static void |
| 6386 | sctp_mcore_thread(void *arg) |
| 6387 | { |
| 6388 | |
| 6389 | struct sctp_mcore_ctrl *wkq; |
| 6390 | struct sctp_mcore_queue *qent; |
| 6391 | |
| 6392 | wkq = (struct sctp_mcore_ctrl *)arg; |
| 6393 | struct mbuf *m; |
| 6394 | int off, v6; |
| 6395 | |
| 6396 | /* Wait for first tickle */ |
| 6397 | SCTP_MCORE_LOCK(wkq); |
| 6398 | wkq->running = 0; |
| 6399 | msleep(&wkq->running, |
| 6400 | &wkq->core_mtx, |
| 6401 | 0, "wait for pkt", 0); |
| 6402 | SCTP_MCORE_UNLOCK(wkq); |
| 6403 | |
| 6404 | /* Bind to our cpu */ |
| 6405 | thread_lock(curthread); |
| 6406 | sched_bind(curthread, wkq->cpuid); |
| 6407 | thread_unlock(curthread); |
| 6408 | |
| 6409 | /* Now lets start working */ |
| 6410 | SCTP_MCORE_LOCK(wkq); |
| 6411 | /* Now grab lock and go */ |
tuexen | 9784e9a | 2011-12-18 13:04:23 +0000 | [diff] [blame] | 6412 | for (;;) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6413 | SCTP_MCORE_QLOCK(wkq); |
| 6414 | skip_sleep: |
| 6415 | wkq->running = 1; |
| 6416 | qent = TAILQ_FIRST(&wkq->que); |
| 6417 | if (qent) { |
| 6418 | TAILQ_REMOVE(&wkq->que, qent, next); |
| 6419 | SCTP_MCORE_QUNLOCK(wkq); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6420 | CURVNET_SET(qent->vn); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6421 | m = qent->m; |
| 6422 | off = qent->off; |
| 6423 | v6 = qent->v6; |
| 6424 | SCTP_FREE(qent, SCTP_M_MCORE); |
| 6425 | if (v6 == 0) { |
| 6426 | sctp_input_with_port(m, off, 0); |
| 6427 | } else { |
tuexen | cb5fe8d | 2012-05-04 09:50:27 +0000 | [diff] [blame] | 6428 | SCTP_PRINTF("V6 not yet supported\n"); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6429 | sctp_m_freem(m); |
| 6430 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6431 | CURVNET_RESTORE(); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6432 | SCTP_MCORE_QLOCK(wkq); |
| 6433 | } |
| 6434 | wkq->running = 0; |
| 6435 | if (!TAILQ_EMPTY(&wkq->que)) { |
| 6436 | goto skip_sleep; |
| 6437 | } |
| 6438 | SCTP_MCORE_QUNLOCK(wkq); |
| 6439 | msleep(&wkq->running, |
| 6440 | &wkq->core_mtx, |
| 6441 | 0, "wait for pkt", 0); |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 6442 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6443 | } |
| 6444 | |
| 6445 | static void |
| 6446 | sctp_startup_mcore_threads(void) |
| 6447 | { |
| 6448 | int i, cpu; |
| 6449 | |
| 6450 | if (mp_ncpus == 1) |
| 6451 | return; |
| 6452 | |
| 6453 | if (sctp_mcore_workers != NULL) { |
| 6454 | /* Already been here in some previous |
| 6455 | * vnet? |
| 6456 | */ |
| 6457 | return; |
| 6458 | } |
| 6459 | SCTP_MALLOC(sctp_mcore_workers, struct sctp_mcore_ctrl *, |
| 6460 | ((mp_maxid+1) * sizeof(struct sctp_mcore_ctrl)), |
| 6461 | SCTP_M_MCORE); |
| 6462 | if (sctp_mcore_workers == NULL) { |
| 6463 | /* TSNH I hope */ |
| 6464 | return; |
| 6465 | } |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 6466 | memset(sctp_mcore_workers, 0 , ((mp_maxid+1) * |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6467 | sizeof(struct sctp_mcore_ctrl))); |
| 6468 | /* Init the structures */ |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 6469 | for (i = 0; i<=mp_maxid; i++) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6470 | TAILQ_INIT(&sctp_mcore_workers[i].que); |
| 6471 | SCTP_MCORE_LOCK_INIT(&sctp_mcore_workers[i]); |
| 6472 | SCTP_MCORE_QLOCK_INIT(&sctp_mcore_workers[i]); |
| 6473 | sctp_mcore_workers[i].cpuid = i; |
| 6474 | } |
| 6475 | if (sctp_cpuarry == NULL) { |
| 6476 | SCTP_MALLOC(sctp_cpuarry, int *, |
| 6477 | (mp_ncpus * sizeof(int)), |
| 6478 | SCTP_M_MCORE); |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 6479 | i = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6480 | CPU_FOREACH(cpu) { |
| 6481 | sctp_cpuarry[i] = cpu; |
| 6482 | i++; |
| 6483 | } |
| 6484 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6485 | /* Now start them all */ |
| 6486 | CPU_FOREACH(cpu) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6487 | (void)kproc_create(sctp_mcore_thread, |
| 6488 | (void *)&sctp_mcore_workers[cpu], |
| 6489 | &sctp_mcore_workers[cpu].thread_proc, |
Michael Tuexen | ad4f4f0 | 2021-03-21 16:54:26 +0100 | [diff] [blame] | 6490 | 0, |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6491 | SCTP_KTHREAD_PAGES, |
| 6492 | SCTP_MCORE_NAME); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6493 | } |
| 6494 | } |
| 6495 | #endif |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 6496 | #endif |
| 6497 | |
| 6498 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 6499 | #if defined(SCTP_NOT_YET) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6500 | static struct mbuf * |
| 6501 | sctp_netisr_hdlr(struct mbuf *m, uintptr_t source) |
| 6502 | { |
| 6503 | struct ip *ip; |
| 6504 | struct sctphdr *sh; |
| 6505 | int offset; |
| 6506 | uint32_t flowid, tag; |
tuexen | 76eb209 | 2012-04-01 16:35:28 +0000 | [diff] [blame] | 6507 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6508 | /* |
| 6509 | * No flow id built by lower layers fix it so we |
| 6510 | * create one. |
| 6511 | */ |
| 6512 | ip = mtod(m, struct ip *); |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 6513 | offset = (ip->ip_hl << 2) + sizeof(struct sctphdr); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6514 | if (SCTP_BUF_LEN(m) < offset) { |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 6515 | if ((m = m_pullup(m, offset)) == NULL) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6516 | SCTP_STAT_INCR(sctps_hdrops); |
| 6517 | return (NULL); |
| 6518 | } |
| 6519 | ip = mtod(m, struct ip *); |
| 6520 | } |
| 6521 | sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6522 | tag = htonl(sh->v_tag); |
| 6523 | flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port); |
| 6524 | m->m_pkthdr.flowid = flowid; |
Michael Tuexen | a508995 | 2020-06-09 23:11:08 +0200 | [diff] [blame] | 6525 | /* FIX ME */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6526 | m->m_flags |= M_FLOWID; |
| 6527 | return (m); |
| 6528 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6529 | |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 6530 | #endif |
| 6531 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6532 | void |
Michael Tuexen | 843cc3b | 2019-08-31 15:25:03 +0200 | [diff] [blame] | 6533 | #if defined(__Userspace__) |
José Luis Millán | f6aa4b0 | 2019-08-12 22:54:25 +0200 | [diff] [blame] | 6534 | sctp_pcb_init(int start_threads) |
Michael Tuexen | 843cc3b | 2019-08-31 15:25:03 +0200 | [diff] [blame] | 6535 | #else |
| 6536 | sctp_pcb_init(void) |
| 6537 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6538 | { |
| 6539 | /* |
| 6540 | * SCTP initialization for the PCB structures should be called by |
Michael Tuexen | 34488e7 | 2016-05-03 22:11:59 +0200 | [diff] [blame] | 6541 | * the sctp_init() function. |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6542 | */ |
| 6543 | int i; |
| 6544 | struct timeval tv; |
tuexen | 6019b07 | 2011-12-15 18:38:03 +0000 | [diff] [blame] | 6545 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6546 | if (SCTP_BASE_VAR(sctp_pcb_initialized) != 0) { |
| 6547 | /* error I was called twice */ |
| 6548 | return; |
| 6549 | } |
| 6550 | SCTP_BASE_VAR(sctp_pcb_initialized) = 1; |
| 6551 | |
Michael Tuexen | cb2878b | 2015-11-13 12:59:40 +0100 | [diff] [blame] | 6552 | #if defined(SCTP_PROCESS_LEVEL_LOCKS) |
Michael Tuexen | eb61157 | 2020-06-13 17:27:02 +0200 | [diff] [blame] | 6553 | #if !defined(_WIN32) |
Michael Tuexen | cb2878b | 2015-11-13 12:59:40 +0100 | [diff] [blame] | 6554 | pthread_mutexattr_init(&SCTP_BASE_VAR(mtx_attr)); |
Paul-Louis Ageneau | 9d6b99b | 2021-10-07 22:30:51 +0200 | [diff] [blame] | 6555 | pthread_rwlockattr_init(&SCTP_BASE_VAR(rwlock_attr)); |
Michael Tuexen | cb2878b | 2015-11-13 12:59:40 +0100 | [diff] [blame] | 6556 | #ifdef INVARIANTS |
| 6557 | pthread_mutexattr_settype(&SCTP_BASE_VAR(mtx_attr), PTHREAD_MUTEX_ERRORCHECK); |
| 6558 | #endif |
| 6559 | #endif |
| 6560 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6561 | #if defined(SCTP_LOCAL_TRACE_BUF) |
Michael Tuexen | eb61157 | 2020-06-13 17:27:02 +0200 | [diff] [blame] | 6562 | #if defined(_WIN32) && !defined(__Userspace__) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6563 | if (SCTP_BASE_SYSCTL(sctp_log) != NULL) { |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 6564 | memset(SCTP_BASE_SYSCTL(sctp_log), 0, sizeof(struct sctp_log)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6565 | } |
| 6566 | #else |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 6567 | memset(&SCTP_BASE_SYSCTL(sctp_log), 0, sizeof(struct sctp_log)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6568 | #endif |
| 6569 | #endif |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 6570 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 6571 | #if defined(SMP) && defined(SCTP_USE_PERCPU_STAT) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6572 | SCTP_MALLOC(SCTP_BASE_STATS, struct sctpstat *, |
| 6573 | ((mp_maxid+1) * sizeof(struct sctpstat)), |
| 6574 | SCTP_M_MCORE); |
| 6575 | #endif |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 6576 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6577 | (void)SCTP_GETTIME_TIMEVAL(&tv); |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 6578 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 6579 | #if defined(SMP) && defined(SCTP_USE_PERCPU_STAT) |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 6580 | memset(SCTP_BASE_STATS, 0, sizeof(struct sctpstat) * (mp_maxid+1)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6581 | SCTP_BASE_STATS[PCPU_GET(cpuid)].sctps_discontinuitytime.tv_sec = (uint32_t)tv.tv_sec; |
| 6582 | SCTP_BASE_STATS[PCPU_GET(cpuid)].sctps_discontinuitytime.tv_usec = (uint32_t)tv.tv_usec; |
| 6583 | #else |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 6584 | memset(&SCTP_BASE_STATS, 0, sizeof(struct sctpstat)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6585 | SCTP_BASE_STAT(sctps_discontinuitytime).tv_sec = (uint32_t)tv.tv_sec; |
| 6586 | SCTP_BASE_STAT(sctps_discontinuitytime).tv_usec = (uint32_t)tv.tv_usec; |
| 6587 | #endif |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 6588 | #else |
| 6589 | memset(&SCTP_BASE_STATS, 0, sizeof(struct sctpstat)); |
| 6590 | SCTP_BASE_STAT(sctps_discontinuitytime).tv_sec = (uint32_t)tv.tv_sec; |
| 6591 | SCTP_BASE_STAT(sctps_discontinuitytime).tv_usec = (uint32_t)tv.tv_usec; |
| 6592 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6593 | /* init the empty list of (All) Endpoints */ |
| 6594 | LIST_INIT(&SCTP_BASE_INFO(listhead)); |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 6595 | #if defined(__APPLE__) && !defined(__Userspace__) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6596 | LIST_INIT(&SCTP_BASE_INFO(inplisthead)); |
t00fcxen | bc766ab | 2014-01-03 19:29:31 +0000 | [diff] [blame] | 6597 | #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) || defined(APPLE_LION) || defined(APPLE_MOUNTAINLION) |
| 6598 | SCTP_BASE_INFO(sctbinfo).listhead = &SCTP_BASE_INFO(inplisthead); |
| 6599 | SCTP_BASE_INFO(sctbinfo).mtx_grp_attr = lck_grp_attr_alloc_init(); |
| 6600 | lck_grp_attr_setdefault(SCTP_BASE_INFO(sctbinfo).mtx_grp_attr); |
| 6601 | SCTP_BASE_INFO(sctbinfo).mtx_grp = lck_grp_alloc_init("sctppcb", SCTP_BASE_INFO(sctbinfo).mtx_grp_attr); |
| 6602 | SCTP_BASE_INFO(sctbinfo).mtx_attr = lck_attr_alloc_init(); |
| 6603 | lck_attr_setdefault(SCTP_BASE_INFO(sctbinfo).mtx_attr); |
| 6604 | #else |
| 6605 | SCTP_BASE_INFO(sctbinfo).ipi_listhead = &SCTP_BASE_INFO(inplisthead); |
| 6606 | SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr = lck_grp_attr_alloc_init(); |
| 6607 | lck_grp_attr_setdefault(SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr); |
| 6608 | SCTP_BASE_INFO(sctbinfo).ipi_lock_grp = lck_grp_alloc_init("sctppcb", SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr); |
| 6609 | SCTP_BASE_INFO(sctbinfo).ipi_lock_attr = lck_attr_alloc_init(); |
| 6610 | lck_attr_setdefault(SCTP_BASE_INFO(sctbinfo).ipi_lock_attr); |
| 6611 | #endif |
| 6612 | #if !defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) && !defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION) |
| 6613 | SCTP_BASE_INFO(sctbinfo).ipi_gc = sctp_gc; |
| 6614 | in_pcbinfo_attach(&SCTP_BASE_INFO(sctbinfo)); |
| 6615 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6616 | #endif |
| 6617 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6618 | /* init the hash table of endpoints */ |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 6619 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6620 | TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", &SCTP_BASE_SYSCTL(sctp_hashtblsize)); |
| 6621 | TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", &SCTP_BASE_SYSCTL(sctp_pcbtblsize)); |
| 6622 | TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", &SCTP_BASE_SYSCTL(sctp_chunkscale)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6623 | #endif |
| 6624 | SCTP_BASE_INFO(sctp_asochash) = SCTP_HASH_INIT((SCTP_BASE_SYSCTL(sctp_hashtblsize) * 31), |
| 6625 | &SCTP_BASE_INFO(hashasocmark)); |
| 6626 | SCTP_BASE_INFO(sctp_ephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize), |
| 6627 | &SCTP_BASE_INFO(hashmark)); |
| 6628 | SCTP_BASE_INFO(sctp_tcpephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize), |
| 6629 | &SCTP_BASE_INFO(hashtcpmark)); |
| 6630 | SCTP_BASE_INFO(hashtblsize) = SCTP_BASE_SYSCTL(sctp_hashtblsize); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6631 | SCTP_BASE_INFO(sctp_vrfhash) = SCTP_HASH_INIT(SCTP_SIZE_OF_VRF_HASH, |
| 6632 | &SCTP_BASE_INFO(hashvrfmark)); |
| 6633 | |
| 6634 | SCTP_BASE_INFO(vrf_ifn_hash) = SCTP_HASH_INIT(SCTP_VRF_IFN_HASH_SIZE, |
| 6635 | &SCTP_BASE_INFO(vrf_ifn_hashmark)); |
| 6636 | /* init the zones */ |
| 6637 | /* |
| 6638 | * FIX ME: Should check for NULL returns, but if it does fail we are |
| 6639 | * doomed to panic anyways... add later maybe. |
| 6640 | */ |
| 6641 | SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_ep), "sctp_ep", |
| 6642 | sizeof(struct sctp_inpcb), maxsockets); |
| 6643 | |
| 6644 | SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asoc), "sctp_asoc", |
| 6645 | sizeof(struct sctp_tcb), sctp_max_number_of_assoc); |
| 6646 | |
| 6647 | SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_laddr), "sctp_laddr", |
| 6648 | sizeof(struct sctp_laddr), |
| 6649 | (sctp_max_number_of_assoc * sctp_scale_up_for_address)); |
| 6650 | |
| 6651 | SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_net), "sctp_raddr", |
| 6652 | sizeof(struct sctp_nets), |
| 6653 | (sctp_max_number_of_assoc * sctp_scale_up_for_address)); |
| 6654 | |
| 6655 | SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_chunk), "sctp_chunk", |
| 6656 | sizeof(struct sctp_tmit_chunk), |
| 6657 | (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale))); |
| 6658 | |
| 6659 | SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_readq), "sctp_readq", |
| 6660 | sizeof(struct sctp_queued_to_read), |
| 6661 | (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale))); |
| 6662 | |
| 6663 | SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_strmoq), "sctp_stream_msg_out", |
| 6664 | sizeof(struct sctp_stream_queue_pending), |
| 6665 | (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale))); |
| 6666 | |
| 6667 | SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf), "sctp_asconf", |
| 6668 | sizeof(struct sctp_asconf), |
| 6669 | (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale))); |
| 6670 | |
| 6671 | SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf_ack), "sctp_asconf_ack", |
| 6672 | sizeof(struct sctp_asconf_ack), |
| 6673 | (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale))); |
| 6674 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6675 | /* Master Lock INIT for info structure */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6676 | SCTP_INP_INFO_LOCK_INIT(); |
| 6677 | SCTP_STATLOG_INIT_LOCK(); |
| 6678 | |
| 6679 | SCTP_IPI_COUNT_INIT(); |
| 6680 | SCTP_IPI_ADDR_INIT(); |
| 6681 | #ifdef SCTP_PACKET_LOGGING |
| 6682 | SCTP_IP_PKTLOG_INIT(); |
| 6683 | #endif |
| 6684 | LIST_INIT(&SCTP_BASE_INFO(addr_wq)); |
| 6685 | |
| 6686 | SCTP_WQ_ADDR_INIT(); |
| 6687 | /* not sure if we need all the counts */ |
| 6688 | SCTP_BASE_INFO(ipi_count_ep) = 0; |
| 6689 | /* assoc/tcb zone info */ |
| 6690 | SCTP_BASE_INFO(ipi_count_asoc) = 0; |
| 6691 | /* local addrlist zone info */ |
| 6692 | SCTP_BASE_INFO(ipi_count_laddr) = 0; |
| 6693 | /* remote addrlist zone info */ |
| 6694 | SCTP_BASE_INFO(ipi_count_raddr) = 0; |
| 6695 | /* chunk info */ |
| 6696 | SCTP_BASE_INFO(ipi_count_chunk) = 0; |
| 6697 | |
| 6698 | /* socket queue zone info */ |
| 6699 | SCTP_BASE_INFO(ipi_count_readq) = 0; |
| 6700 | |
| 6701 | /* stream out queue cont */ |
| 6702 | SCTP_BASE_INFO(ipi_count_strmoq) = 0; |
| 6703 | |
| 6704 | SCTP_BASE_INFO(ipi_free_strmoq) = 0; |
| 6705 | SCTP_BASE_INFO(ipi_free_chunks) = 0; |
| 6706 | |
| 6707 | SCTP_OS_TIMER_INIT(&SCTP_BASE_INFO(addr_wq_timer.timer)); |
| 6708 | |
| 6709 | /* Init the TIMEWAIT list */ |
| 6710 | for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) { |
| 6711 | LIST_INIT(&SCTP_BASE_INFO(vtag_timewait)[i]); |
| 6712 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6713 | #if defined(SCTP_PROCESS_LEVEL_LOCKS) |
Michael Tuexen | eb61157 | 2020-06-13 17:27:02 +0200 | [diff] [blame] | 6714 | #if defined(_WIN32) |
tuexen | 6019b07 | 2011-12-15 18:38:03 +0000 | [diff] [blame] | 6715 | InitializeConditionVariable(&sctp_it_ctl.iterator_wakeup); |
| 6716 | #else |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6717 | (void)pthread_cond_init(&sctp_it_ctl.iterator_wakeup, NULL); |
| 6718 | #endif |
tuexen | 6019b07 | 2011-12-15 18:38:03 +0000 | [diff] [blame] | 6719 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6720 | sctp_startup_iterator(); |
| 6721 | |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 6722 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 6723 | #if defined(SCTP_MCORE_INPUT) && defined(SMP) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6724 | sctp_startup_mcore_threads(); |
| 6725 | #endif |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 6726 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6727 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6728 | /* |
| 6729 | * INIT the default VRF which for BSD is the only one, other O/S's |
| 6730 | * may have more. But initially they must start with one and then |
| 6731 | * add the VRF's as addresses are added. |
| 6732 | */ |
| 6733 | sctp_init_vrf_list(SCTP_DEFAULT_VRF); |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 6734 | #if defined(__FreeBSD__) && !defined(__Userspace__) && defined(SCTP_NOT_YET) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6735 | if (ip_register_flow_handler(sctp_netisr_hdlr, IPPROTO_SCTP)) { |
tuexen | cb5fe8d | 2012-05-04 09:50:27 +0000 | [diff] [blame] | 6736 | SCTP_PRINTF("***SCTP- Error can't register netisr handler***\n"); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6737 | } |
| 6738 | #endif |
| 6739 | #if defined(_SCTP_NEEDS_CALLOUT_) || defined(_USER_SCTP_NEEDS_CALLOUT_) |
| 6740 | /* allocate the lock for the callout/timer queue */ |
| 6741 | SCTP_TIMERQ_LOCK_INIT(); |
| 6742 | TAILQ_INIT(&SCTP_BASE_INFO(callqueue)); |
| 6743 | #endif |
| 6744 | #if defined(__Userspace__) |
Michael Tuexen | 2f6478e | 2017-03-02 09:57:46 +0100 | [diff] [blame] | 6745 | mbuf_initialize(NULL); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6746 | atomic_init(); |
tuexen | c417897 | 2012-07-15 20:40:54 +0000 | [diff] [blame] | 6747 | #if defined(INET) || defined(INET6) |
José Luis Millán | f6aa4b0 | 2019-08-12 22:54:25 +0200 | [diff] [blame] | 6748 | if (start_threads) |
| 6749 | recv_thread_init(); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6750 | #endif |
tuexen | c417897 | 2012-07-15 20:40:54 +0000 | [diff] [blame] | 6751 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6752 | } |
| 6753 | |
| 6754 | /* |
| 6755 | * Assumes that the SCTP_BASE_INFO() lock is NOT held. |
| 6756 | */ |
| 6757 | void |
| 6758 | sctp_pcb_finish(void) |
| 6759 | { |
| 6760 | struct sctp_vrflist *vrf_bucket; |
| 6761 | struct sctp_vrf *vrf, *nvrf; |
| 6762 | struct sctp_ifn *ifn, *nifn; |
| 6763 | struct sctp_ifa *ifa, *nifa; |
| 6764 | struct sctpvtaghead *chain; |
| 6765 | struct sctp_tagblock *twait_block, *prev_twait_block; |
| 6766 | struct sctp_laddr *wi, *nwi; |
| 6767 | int i; |
t00fcxen | c793228 | 2013-11-16 14:57:41 +0000 | [diff] [blame] | 6768 | struct sctp_iterator *it, *nit; |
Michael Tuexen | 20785d4 | 2015-11-13 12:48:35 +0100 | [diff] [blame] | 6769 | |
Michael Tuexen | 71a83fb | 2016-02-16 21:52:58 +0100 | [diff] [blame] | 6770 | if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) { |
| 6771 | SCTP_PRINTF("%s: race condition on teardown.\n", __func__); |
| 6772 | return; |
| 6773 | } |
| 6774 | SCTP_BASE_VAR(sctp_pcb_initialized) = 0; |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 6775 | #if !(defined(__FreeBSD__) && !defined(__Userspace__)) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6776 | /* Notify the iterator to exit. */ |
| 6777 | SCTP_IPI_ITERATOR_WQ_LOCK(); |
| 6778 | sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_MUST_EXIT; |
| 6779 | sctp_wakeup_iterator(); |
| 6780 | SCTP_IPI_ITERATOR_WQ_UNLOCK(); |
| 6781 | #endif |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 6782 | #if defined(__APPLE__) && !defined(__Userspace__) |
t00fcxen | bc766ab | 2014-01-03 19:29:31 +0000 | [diff] [blame] | 6783 | #if !defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) && !defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION) |
| 6784 | in_pcbinfo_detach(&SCTP_BASE_INFO(sctbinfo)); |
| 6785 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6786 | SCTP_IPI_ITERATOR_WQ_LOCK(); |
| 6787 | do { |
| 6788 | msleep(&sctp_it_ctl.iterator_flags, |
| 6789 | sctp_it_ctl.ipi_iterator_wq_mtx, |
| 6790 | 0, "waiting_for_work", 0); |
| 6791 | } while ((sctp_it_ctl.iterator_flags & SCTP_ITERATOR_EXITED) == 0); |
| 6792 | thread_deallocate(sctp_it_ctl.thread_proc); |
| 6793 | SCTP_IPI_ITERATOR_WQ_UNLOCK(); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6794 | #endif |
Michael Tuexen | eb61157 | 2020-06-13 17:27:02 +0200 | [diff] [blame] | 6795 | #if defined(_WIN32) && !defined(__Userspace__) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6796 | if (sctp_it_ctl.iterator_thread_obj != NULL) { |
| 6797 | NTSTATUS status = STATUS_SUCCESS; |
| 6798 | |
| 6799 | KeSetEvent(&sctp_it_ctl.iterator_wakeup[1], IO_NO_INCREMENT, FALSE); |
| 6800 | status = KeWaitForSingleObject(sctp_it_ctl.iterator_thread_obj, |
| 6801 | Executive, |
| 6802 | KernelMode, |
| 6803 | FALSE, |
| 6804 | NULL); |
| 6805 | ObDereferenceObject(sctp_it_ctl.iterator_thread_obj); |
| 6806 | } |
| 6807 | #endif |
t00fcxen | 92a5ac4 | 2013-11-16 12:58:54 +0000 | [diff] [blame] | 6808 | #if defined(__Userspace__) |
Michael Tuexen | 3212d54 | 2020-03-16 12:30:04 +0100 | [diff] [blame] | 6809 | if (SCTP_BASE_VAR(iterator_thread_started)) { |
Michael Tuexen | eb61157 | 2020-06-13 17:27:02 +0200 | [diff] [blame] | 6810 | #if defined(_WIN32) |
t00fcxen | 92a5ac4 | 2013-11-16 12:58:54 +0000 | [diff] [blame] | 6811 | WaitForSingleObject(sctp_it_ctl.thread_proc, INFINITE); |
| 6812 | CloseHandle(sctp_it_ctl.thread_proc); |
| 6813 | sctp_it_ctl.thread_proc = NULL; |
| 6814 | #else |
| 6815 | pthread_join(sctp_it_ctl.thread_proc, NULL); |
| 6816 | sctp_it_ctl.thread_proc = 0; |
| 6817 | #endif |
| 6818 | } |
| 6819 | #endif |
| 6820 | #if defined(SCTP_PROCESS_LEVEL_LOCKS) |
Michael Tuexen | eb61157 | 2020-06-13 17:27:02 +0200 | [diff] [blame] | 6821 | #if defined(_WIN32) |
t00fcxen | 92a5ac4 | 2013-11-16 12:58:54 +0000 | [diff] [blame] | 6822 | DeleteConditionVariable(&sctp_it_ctl.iterator_wakeup); |
| 6823 | #else |
| 6824 | pthread_cond_destroy(&sctp_it_ctl.iterator_wakeup); |
Michael Tuexen | 89e15a7 | 2015-11-13 12:42:55 +0100 | [diff] [blame] | 6825 | pthread_mutexattr_destroy(&SCTP_BASE_VAR(mtx_attr)); |
Paul-Louis Ageneau | 9d6b99b | 2021-10-07 22:30:51 +0200 | [diff] [blame] | 6826 | pthread_rwlockattr_destroy(&SCTP_BASE_VAR(rwlock_attr)); |
t00fcxen | 92a5ac4 | 2013-11-16 12:58:54 +0000 | [diff] [blame] | 6827 | #endif |
| 6828 | #endif |
t00fcxen | 9232a65 | 2013-11-30 09:34:24 +0000 | [diff] [blame] | 6829 | /* In FreeBSD the iterator thread never exits |
| 6830 | * but we do clean up. |
| 6831 | * The only way FreeBSD reaches here is if we have VRF's |
| 6832 | * but we still add the ifdef to make it compile on old versions. |
| 6833 | */ |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 6834 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
Michael Tuexen | 71a83fb | 2016-02-16 21:52:58 +0100 | [diff] [blame] | 6835 | retry: |
| 6836 | #endif |
t00fcxen | 9232a65 | 2013-11-30 09:34:24 +0000 | [diff] [blame] | 6837 | SCTP_IPI_ITERATOR_WQ_LOCK(); |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 6838 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
Michael Tuexen | 71a83fb | 2016-02-16 21:52:58 +0100 | [diff] [blame] | 6839 | /* |
| 6840 | * sctp_iterator_worker() might be working on an it entry without |
| 6841 | * holding the lock. We won't find it on the list either and |
| 6842 | * continue and free/destroy it. While holding the lock, spin, to |
| 6843 | * avoid the race condition as sctp_iterator_worker() will have to |
Michael Tuexen | 5ac7952 | 2020-06-06 23:32:08 +0200 | [diff] [blame] | 6844 | * wait to re-acquire the lock. |
Michael Tuexen | 71a83fb | 2016-02-16 21:52:58 +0100 | [diff] [blame] | 6845 | */ |
| 6846 | if (sctp_it_ctl.iterator_running != 0 || sctp_it_ctl.cur_it != NULL) { |
| 6847 | SCTP_IPI_ITERATOR_WQ_UNLOCK(); |
| 6848 | SCTP_PRINTF("%s: Iterator running while we held the lock. Retry. " |
| 6849 | "cur_it=%p\n", __func__, sctp_it_ctl.cur_it); |
| 6850 | DELAY(10); |
| 6851 | goto retry; |
| 6852 | } |
| 6853 | #endif |
t00fcxen | 9232a65 | 2013-11-30 09:34:24 +0000 | [diff] [blame] | 6854 | TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) { |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 6855 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
t00fcxen | 9232a65 | 2013-11-30 09:34:24 +0000 | [diff] [blame] | 6856 | if (it->vn != curvnet) { |
| 6857 | continue; |
| 6858 | } |
| 6859 | #endif |
| 6860 | TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr); |
| 6861 | if (it->function_atend != NULL) { |
| 6862 | (*it->function_atend) (it->pointer, it->val); |
| 6863 | } |
| 6864 | SCTP_FREE(it,SCTP_M_ITER); |
| 6865 | } |
| 6866 | SCTP_IPI_ITERATOR_WQ_UNLOCK(); |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 6867 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
t00fcxen | 9232a65 | 2013-11-30 09:34:24 +0000 | [diff] [blame] | 6868 | SCTP_ITERATOR_LOCK(); |
| 6869 | if ((sctp_it_ctl.cur_it) && |
| 6870 | (sctp_it_ctl.cur_it->vn == curvnet)) { |
| 6871 | sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_IT; |
| 6872 | } |
| 6873 | SCTP_ITERATOR_UNLOCK(); |
| 6874 | #endif |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 6875 | #if !(defined(__FreeBSD__) && !defined(__Userspace__)) |
t00fcxen | 92a5ac4 | 2013-11-16 12:58:54 +0000 | [diff] [blame] | 6876 | SCTP_IPI_ITERATOR_WQ_DESTROY(); |
t00fcxen | 8dd95b8 | 2014-07-11 20:41:28 +0000 | [diff] [blame] | 6877 | SCTP_ITERATOR_LOCK_DESTROY(); |
t00fcxen | c793228 | 2013-11-16 14:57:41 +0000 | [diff] [blame] | 6878 | #endif |
Michael Tuexen | 71a83fb | 2016-02-16 21:52:58 +0100 | [diff] [blame] | 6879 | SCTP_OS_TIMER_STOP_DRAIN(&SCTP_BASE_INFO(addr_wq_timer.timer)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6880 | SCTP_WQ_ADDR_LOCK(); |
| 6881 | LIST_FOREACH_SAFE(wi, &SCTP_BASE_INFO(addr_wq), sctp_nxt_addr, nwi) { |
| 6882 | LIST_REMOVE(wi, sctp_nxt_addr); |
| 6883 | SCTP_DECR_LADDR_COUNT(); |
t00fcxen | 9892b7f | 2014-09-06 20:06:39 +0000 | [diff] [blame] | 6884 | if (wi->action == SCTP_DEL_IP_ADDRESS) { |
| 6885 | SCTP_FREE(wi->ifa, SCTP_M_IFA); |
| 6886 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6887 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), wi); |
| 6888 | } |
| 6889 | SCTP_WQ_ADDR_UNLOCK(); |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 6890 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6891 | /* |
| 6892 | * free the vrf/ifn/ifa lists and hashes (be sure address monitor |
| 6893 | * is destroyed first). |
| 6894 | */ |
Michael Tuexen | 2c5953e | 2020-07-17 17:25:19 +0200 | [diff] [blame] | 6895 | SCTP_IPI_ADDR_WLOCK(); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6896 | vrf_bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(SCTP_DEFAULT_VRFID & SCTP_BASE_INFO(hashvrfmark))]; |
| 6897 | LIST_FOREACH_SAFE(vrf, vrf_bucket, next_vrf, nvrf) { |
| 6898 | LIST_FOREACH_SAFE(ifn, &vrf->ifnlist, next_ifn, nifn) { |
| 6899 | LIST_FOREACH_SAFE(ifa, &ifn->ifalist, next_ifa, nifa) { |
| 6900 | /* free the ifa */ |
| 6901 | LIST_REMOVE(ifa, next_bucket); |
| 6902 | LIST_REMOVE(ifa, next_ifa); |
| 6903 | SCTP_FREE(ifa, SCTP_M_IFA); |
| 6904 | } |
| 6905 | /* free the ifn */ |
| 6906 | LIST_REMOVE(ifn, next_bucket); |
| 6907 | LIST_REMOVE(ifn, next_ifn); |
| 6908 | SCTP_FREE(ifn, SCTP_M_IFN); |
| 6909 | } |
| 6910 | SCTP_HASH_FREE(vrf->vrf_addr_hash, vrf->vrf_addr_hashmark); |
| 6911 | /* free the vrf */ |
| 6912 | LIST_REMOVE(vrf, next_vrf); |
| 6913 | SCTP_FREE(vrf, SCTP_M_VRF); |
| 6914 | } |
Michael Tuexen | 2c5953e | 2020-07-17 17:25:19 +0200 | [diff] [blame] | 6915 | SCTP_IPI_ADDR_WUNLOCK(); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6916 | /* free the vrf hashes */ |
| 6917 | SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_vrfhash), SCTP_BASE_INFO(hashvrfmark)); |
| 6918 | SCTP_HASH_FREE(SCTP_BASE_INFO(vrf_ifn_hash), SCTP_BASE_INFO(vrf_ifn_hashmark)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6919 | |
| 6920 | /* free the TIMEWAIT list elements malloc'd in the function |
| 6921 | * sctp_add_vtag_to_timewait()... |
| 6922 | */ |
| 6923 | for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) { |
| 6924 | chain = &SCTP_BASE_INFO(vtag_timewait)[i]; |
| 6925 | if (!LIST_EMPTY(chain)) { |
| 6926 | prev_twait_block = NULL; |
| 6927 | LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) { |
| 6928 | if (prev_twait_block) { |
| 6929 | SCTP_FREE(prev_twait_block, SCTP_M_TIMW); |
| 6930 | } |
| 6931 | prev_twait_block = twait_block; |
| 6932 | } |
| 6933 | SCTP_FREE(prev_twait_block, SCTP_M_TIMW); |
| 6934 | } |
| 6935 | } |
| 6936 | |
| 6937 | /* free the locks and mutexes */ |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 6938 | #if defined(__APPLE__) && !defined(__Userspace__) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6939 | SCTP_TIMERQ_LOCK_DESTROY(); |
| 6940 | #endif |
| 6941 | #ifdef SCTP_PACKET_LOGGING |
| 6942 | SCTP_IP_PKTLOG_DESTROY(); |
| 6943 | #endif |
| 6944 | SCTP_IPI_ADDR_DESTROY(); |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 6945 | #if defined(__APPLE__) && !defined(__Userspace__) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6946 | SCTP_IPI_COUNT_DESTROY(); |
| 6947 | #endif |
| 6948 | SCTP_STATLOG_DESTROY(); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6949 | SCTP_INP_INFO_LOCK_DESTROY(); |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 6950 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6951 | SCTP_WQ_ADDR_DESTROY(); |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 6952 | |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 6953 | #if defined(__APPLE__) && !defined(__Userspace__) |
t00fcxen | bc766ab | 2014-01-03 19:29:31 +0000 | [diff] [blame] | 6954 | #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) || defined(APPLE_LION) || defined(APPLE_MOUNTAINLION) |
| 6955 | lck_grp_attr_free(SCTP_BASE_INFO(sctbinfo).mtx_grp_attr); |
| 6956 | lck_grp_free(SCTP_BASE_INFO(sctbinfo).mtx_grp); |
| 6957 | lck_attr_free(SCTP_BASE_INFO(sctbinfo).mtx_attr); |
| 6958 | #else |
| 6959 | lck_grp_attr_free(SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr); |
| 6960 | lck_grp_free(SCTP_BASE_INFO(sctbinfo).ipi_lock_grp); |
| 6961 | lck_attr_free(SCTP_BASE_INFO(sctbinfo).ipi_lock_attr); |
| 6962 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6963 | #endif |
| 6964 | #if defined(__Userspace__) |
| 6965 | SCTP_TIMERQ_LOCK_DESTROY(); |
| 6966 | SCTP_ZONE_DESTROY(zone_mbuf); |
| 6967 | SCTP_ZONE_DESTROY(zone_clust); |
| 6968 | SCTP_ZONE_DESTROY(zone_ext_refcnt); |
| 6969 | #endif |
Michael Tuexen | 71a83fb | 2016-02-16 21:52:58 +0100 | [diff] [blame] | 6970 | /* Get rid of other stuff too. */ |
| 6971 | if (SCTP_BASE_INFO(sctp_asochash) != NULL) |
| 6972 | SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_asochash), SCTP_BASE_INFO(hashasocmark)); |
| 6973 | if (SCTP_BASE_INFO(sctp_ephash) != NULL) |
| 6974 | SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_ephash), SCTP_BASE_INFO(hashmark)); |
| 6975 | if (SCTP_BASE_INFO(sctp_tcpephash) != NULL) |
| 6976 | SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_tcpephash), SCTP_BASE_INFO(hashtcpmark)); |
| 6977 | |
Michael Tuexen | eb61157 | 2020-06-13 17:27:02 +0200 | [diff] [blame] | 6978 | #if defined(_WIN32) || defined(__FreeBSD__) || defined(__Userspace__) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6979 | SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_ep)); |
| 6980 | SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asoc)); |
| 6981 | SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_laddr)); |
| 6982 | SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_net)); |
| 6983 | SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_chunk)); |
| 6984 | SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_readq)); |
| 6985 | SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_strmoq)); |
| 6986 | SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf)); |
| 6987 | SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf_ack)); |
| 6988 | #endif |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 6989 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
| 6990 | #if defined(SMP) && defined(SCTP_USE_PERCPU_STAT) |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 6991 | SCTP_FREE(SCTP_BASE_STATS, SCTP_M_MCORE); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6992 | #endif |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 6993 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6994 | } |
| 6995 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6996 | int |
| 6997 | sctp_load_addresses_from_init(struct sctp_tcb *stcb, struct mbuf *m, |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 6998 | int offset, int limit, |
| 6999 | struct sockaddr *src, struct sockaddr *dst, |
Michael Tuexen | 923f735 | 2016-05-01 23:55:35 +0200 | [diff] [blame] | 7000 | struct sockaddr *altsa, uint16_t port) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7001 | { |
| 7002 | /* |
| 7003 | * grub through the INIT pulling addresses and loading them to the |
| 7004 | * nets structure in the asoc. The from address in the mbuf should |
| 7005 | * also be loaded (if it is not already). This routine can be called |
| 7006 | * with either INIT or INIT-ACK's as long as the m points to the IP |
| 7007 | * packet and the offset points to the beginning of the parameters. |
| 7008 | */ |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 7009 | struct sctp_inpcb *inp; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7010 | struct sctp_nets *net, *nnet, *net_tmp; |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 7011 | struct sctp_paramhdr *phdr, param_buf; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7012 | struct sctp_tcb *stcb_tmp; |
| 7013 | uint16_t ptype, plen; |
| 7014 | struct sockaddr *sa; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7015 | uint8_t random_store[SCTP_PARAM_BUFFER_SIZE]; |
| 7016 | struct sctp_auth_random *p_random = NULL; |
| 7017 | uint16_t random_len = 0; |
| 7018 | uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE]; |
| 7019 | struct sctp_auth_hmac_algo *hmacs = NULL; |
| 7020 | uint16_t hmacs_len = 0; |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 7021 | uint8_t saw_asconf = 0; |
| 7022 | uint8_t saw_asconf_ack = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7023 | uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE]; |
| 7024 | struct sctp_auth_chunk_list *chunks = NULL; |
| 7025 | uint16_t num_chunks = 0; |
| 7026 | sctp_key_t *new_key; |
| 7027 | uint32_t keylen; |
| 7028 | int got_random = 0, got_hmacs = 0, got_chklist = 0; |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7029 | uint8_t peer_supports_ecn; |
| 7030 | uint8_t peer_supports_prsctp; |
| 7031 | uint8_t peer_supports_auth; |
| 7032 | uint8_t peer_supports_asconf; |
| 7033 | uint8_t peer_supports_asconf_ack; |
| 7034 | uint8_t peer_supports_reconfig; |
| 7035 | uint8_t peer_supports_nrsack; |
| 7036 | uint8_t peer_supports_pktdrop; |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7037 | uint8_t peer_supports_idata; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7038 | #ifdef INET |
| 7039 | struct sockaddr_in sin; |
| 7040 | #endif |
| 7041 | #ifdef INET6 |
| 7042 | struct sockaddr_in6 sin6; |
| 7043 | #endif |
| 7044 | |
| 7045 | /* First get the destination address setup too. */ |
| 7046 | #ifdef INET |
| 7047 | memset(&sin, 0, sizeof(sin)); |
| 7048 | sin.sin_family = AF_INET; |
t00fcxen | 8fbaee3 | 2012-09-04 16:41:39 +0000 | [diff] [blame] | 7049 | #ifdef HAVE_SIN_LEN |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7050 | sin.sin_len = sizeof(sin); |
| 7051 | #endif |
| 7052 | sin.sin_port = stcb->rport; |
| 7053 | #endif |
| 7054 | #ifdef INET6 |
| 7055 | memset(&sin6, 0, sizeof(sin6)); |
| 7056 | sin6.sin6_family = AF_INET6; |
t00fcxen | 8fbaee3 | 2012-09-04 16:41:39 +0000 | [diff] [blame] | 7057 | #ifdef HAVE_SIN6_LEN |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7058 | sin6.sin6_len = sizeof(struct sockaddr_in6); |
| 7059 | #endif |
| 7060 | sin6.sin6_port = stcb->rport; |
| 7061 | #endif |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 7062 | if (altsa) { |
| 7063 | sa = altsa; |
| 7064 | } else { |
| 7065 | sa = src; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7066 | } |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7067 | peer_supports_idata = 0; |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7068 | peer_supports_ecn = 0; |
| 7069 | peer_supports_prsctp = 0; |
| 7070 | peer_supports_auth = 0; |
| 7071 | peer_supports_asconf = 0; |
Michael Tuexen | 4544fa2 | 2020-10-06 13:35:42 +0200 | [diff] [blame] | 7072 | peer_supports_asconf_ack = 0; |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7073 | peer_supports_reconfig = 0; |
| 7074 | peer_supports_nrsack = 0; |
| 7075 | peer_supports_pktdrop = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7076 | TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { |
| 7077 | /* mark all addresses that we have currently on the list */ |
| 7078 | net->dest_state |= SCTP_ADDR_NOT_IN_ASSOC; |
| 7079 | } |
| 7080 | /* does the source address already exist? if so skip it */ |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 7081 | inp = stcb->sctp_ep; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7082 | atomic_add_int(&stcb->asoc.refcnt, 1); |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 7083 | stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net_tmp, dst, stcb); |
Michael Tuexen | a106ab8 | 2021-11-26 14:03:15 +0100 | [diff] [blame] | 7084 | atomic_subtract_int(&stcb->asoc.refcnt, 1); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7085 | |
| 7086 | if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || inp == NULL) { |
| 7087 | /* we must add the source address */ |
| 7088 | /* no scope set here since we have a tcb already. */ |
| 7089 | switch (sa->sa_family) { |
| 7090 | #ifdef INET |
| 7091 | case AF_INET: |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 7092 | if (stcb->asoc.scope.ipv4_addr_legal) { |
Michael Tuexen | 923f735 | 2016-05-01 23:55:35 +0200 | [diff] [blame] | 7093 | if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_2)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7094 | return (-1); |
| 7095 | } |
| 7096 | } |
| 7097 | break; |
| 7098 | #endif |
| 7099 | #ifdef INET6 |
| 7100 | case AF_INET6: |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 7101 | if (stcb->asoc.scope.ipv6_addr_legal) { |
Michael Tuexen | 923f735 | 2016-05-01 23:55:35 +0200 | [diff] [blame] | 7102 | if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7103 | return (-2); |
| 7104 | } |
| 7105 | } |
| 7106 | break; |
| 7107 | #endif |
tuexen | 430ca6c | 2012-07-10 23:36:36 +0000 | [diff] [blame] | 7108 | #if defined(__Userspace__) |
| 7109 | case AF_CONN: |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 7110 | if (stcb->asoc.scope.conn_addr_legal) { |
Michael Tuexen | 923f735 | 2016-05-01 23:55:35 +0200 | [diff] [blame] | 7111 | if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) { |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 7112 | return (-2); |
| 7113 | } |
tuexen | 430ca6c | 2012-07-10 23:36:36 +0000 | [diff] [blame] | 7114 | } |
| 7115 | break; |
| 7116 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7117 | default: |
| 7118 | break; |
| 7119 | } |
| 7120 | } else { |
| 7121 | if (net_tmp != NULL && stcb_tmp == stcb) { |
| 7122 | net_tmp->dest_state &= ~SCTP_ADDR_NOT_IN_ASSOC; |
| 7123 | } else if (stcb_tmp != stcb) { |
| 7124 | /* It belongs to another association? */ |
| 7125 | if (stcb_tmp) |
| 7126 | SCTP_TCB_UNLOCK(stcb_tmp); |
| 7127 | return (-3); |
| 7128 | } |
| 7129 | } |
| 7130 | if (stcb->asoc.state == 0) { |
| 7131 | /* the assoc was freed? */ |
| 7132 | return (-4); |
| 7133 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7134 | /* now we must go through each of the params. */ |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 7135 | phdr = sctp_get_next_param(m, offset, ¶m_buf, sizeof(param_buf)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7136 | while (phdr) { |
| 7137 | ptype = ntohs(phdr->param_type); |
| 7138 | plen = ntohs(phdr->param_length); |
| 7139 | /* |
tuexen | cb5fe8d | 2012-05-04 09:50:27 +0000 | [diff] [blame] | 7140 | * SCTP_PRINTF("ptype => %0x, plen => %d\n", (uint32_t)ptype, |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7141 | * (int)plen); |
| 7142 | */ |
| 7143 | if (offset + plen > limit) { |
| 7144 | break; |
| 7145 | } |
Michael Tuexen | 790a7a2 | 2019-12-20 17:02:02 +0100 | [diff] [blame] | 7146 | if (plen < sizeof(struct sctp_paramhdr)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7147 | break; |
| 7148 | } |
| 7149 | #ifdef INET |
| 7150 | if (ptype == SCTP_IPV4_ADDRESS) { |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 7151 | if (stcb->asoc.scope.ipv4_addr_legal) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7152 | struct sctp_ipv4addr_param *p4, p4_buf; |
| 7153 | |
| 7154 | /* ok get the v4 address and check/add */ |
| 7155 | phdr = sctp_get_next_param(m, offset, |
| 7156 | (struct sctp_paramhdr *)&p4_buf, |
| 7157 | sizeof(p4_buf)); |
| 7158 | if (plen != sizeof(struct sctp_ipv4addr_param) || |
| 7159 | phdr == NULL) { |
| 7160 | return (-5); |
| 7161 | } |
| 7162 | p4 = (struct sctp_ipv4addr_param *)phdr; |
| 7163 | sin.sin_addr.s_addr = p4->addr; |
| 7164 | if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) { |
| 7165 | /* Skip multi-cast addresses */ |
| 7166 | goto next_param; |
| 7167 | } |
| 7168 | if ((sin.sin_addr.s_addr == INADDR_BROADCAST) || |
| 7169 | (sin.sin_addr.s_addr == INADDR_ANY)) { |
| 7170 | goto next_param; |
| 7171 | } |
| 7172 | sa = (struct sockaddr *)&sin; |
| 7173 | inp = stcb->sctp_ep; |
| 7174 | atomic_add_int(&stcb->asoc.refcnt, 1); |
| 7175 | stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net, |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 7176 | dst, stcb); |
Michael Tuexen | a106ab8 | 2021-11-26 14:03:15 +0100 | [diff] [blame] | 7177 | atomic_subtract_int(&stcb->asoc.refcnt, 1); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7178 | |
| 7179 | if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || |
| 7180 | inp == NULL) { |
| 7181 | /* we must add the source address */ |
| 7182 | /* |
| 7183 | * no scope set since we have a tcb |
| 7184 | * already |
| 7185 | */ |
| 7186 | |
| 7187 | /* |
| 7188 | * we must validate the state again |
| 7189 | * here |
| 7190 | */ |
| 7191 | add_it_now: |
| 7192 | if (stcb->asoc.state == 0) { |
| 7193 | /* the assoc was freed? */ |
| 7194 | return (-7); |
| 7195 | } |
Michael Tuexen | 923f735 | 2016-05-01 23:55:35 +0200 | [diff] [blame] | 7196 | if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_4)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7197 | return (-8); |
| 7198 | } |
| 7199 | } else if (stcb_tmp == stcb) { |
| 7200 | if (stcb->asoc.state == 0) { |
| 7201 | /* the assoc was freed? */ |
| 7202 | return (-10); |
| 7203 | } |
| 7204 | if (net != NULL) { |
| 7205 | /* clear flag */ |
| 7206 | net->dest_state &= |
| 7207 | ~SCTP_ADDR_NOT_IN_ASSOC; |
| 7208 | } |
| 7209 | } else { |
| 7210 | /* |
| 7211 | * strange, address is in another |
| 7212 | * assoc? straighten out locks. |
| 7213 | */ |
| 7214 | if (stcb_tmp) { |
Michael Tuexen | 348a36c | 2018-08-13 16:24:47 +0200 | [diff] [blame] | 7215 | if (SCTP_GET_STATE(stcb_tmp) == SCTP_STATE_COOKIE_WAIT) { |
Michael Tüxen | ac14228 | 2015-07-28 00:42:03 +0200 | [diff] [blame] | 7216 | struct mbuf *op_err; |
| 7217 | char msg[SCTP_DIAG_INFO_LEN]; |
| 7218 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7219 | /* in setup state we abort this guy */ |
Michael Tuexen | edd369d | 2020-05-19 09:42:15 +0200 | [diff] [blame] | 7220 | SCTP_SNPRINTF(msg, sizeof(msg), |
| 7221 | "%s:%d at %s", __FILE__, __LINE__, __func__); |
Michael Tüxen | ac14228 | 2015-07-28 00:42:03 +0200 | [diff] [blame] | 7222 | op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), |
| 7223 | msg); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7224 | sctp_abort_an_association(stcb_tmp->sctp_ep, |
Michael Tuexen | 1ade45c | 2021-07-09 23:32:42 +0200 | [diff] [blame] | 7225 | stcb_tmp, op_err, false, |
Michael Tüxen | ac14228 | 2015-07-28 00:42:03 +0200 | [diff] [blame] | 7226 | SCTP_SO_NOT_LOCKED); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7227 | goto add_it_now; |
| 7228 | } |
| 7229 | SCTP_TCB_UNLOCK(stcb_tmp); |
| 7230 | } |
| 7231 | |
| 7232 | if (stcb->asoc.state == 0) { |
| 7233 | /* the assoc was freed? */ |
| 7234 | return (-12); |
| 7235 | } |
| 7236 | return (-13); |
| 7237 | } |
| 7238 | } |
| 7239 | } else |
| 7240 | #endif |
| 7241 | #ifdef INET6 |
| 7242 | if (ptype == SCTP_IPV6_ADDRESS) { |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 7243 | if (stcb->asoc.scope.ipv6_addr_legal) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7244 | /* ok get the v6 address and check/add */ |
| 7245 | struct sctp_ipv6addr_param *p6, p6_buf; |
| 7246 | |
| 7247 | phdr = sctp_get_next_param(m, offset, |
| 7248 | (struct sctp_paramhdr *)&p6_buf, |
| 7249 | sizeof(p6_buf)); |
| 7250 | if (plen != sizeof(struct sctp_ipv6addr_param) || |
| 7251 | phdr == NULL) { |
| 7252 | return (-14); |
| 7253 | } |
| 7254 | p6 = (struct sctp_ipv6addr_param *)phdr; |
| 7255 | memcpy((caddr_t)&sin6.sin6_addr, p6->addr, |
| 7256 | sizeof(p6->addr)); |
| 7257 | if (IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) { |
| 7258 | /* Skip multi-cast addresses */ |
| 7259 | goto next_param; |
| 7260 | } |
| 7261 | if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) { |
| 7262 | /* Link local make no sense without scope */ |
| 7263 | goto next_param; |
| 7264 | } |
| 7265 | sa = (struct sockaddr *)&sin6; |
| 7266 | inp = stcb->sctp_ep; |
| 7267 | atomic_add_int(&stcb->asoc.refcnt, 1); |
| 7268 | stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net, |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 7269 | dst, stcb); |
Michael Tuexen | a106ab8 | 2021-11-26 14:03:15 +0100 | [diff] [blame] | 7270 | atomic_subtract_int(&stcb->asoc.refcnt, 1); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7271 | if (stcb_tmp == NULL && |
| 7272 | (inp == stcb->sctp_ep || inp == NULL)) { |
| 7273 | /* |
| 7274 | * we must validate the state again |
| 7275 | * here |
| 7276 | */ |
| 7277 | add_it_now6: |
| 7278 | if (stcb->asoc.state == 0) { |
| 7279 | /* the assoc was freed? */ |
| 7280 | return (-16); |
| 7281 | } |
| 7282 | /* |
| 7283 | * we must add the address, no scope |
| 7284 | * set |
| 7285 | */ |
Michael Tuexen | 923f735 | 2016-05-01 23:55:35 +0200 | [diff] [blame] | 7286 | if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_5)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7287 | return (-17); |
| 7288 | } |
| 7289 | } else if (stcb_tmp == stcb) { |
| 7290 | /* |
| 7291 | * we must validate the state again |
| 7292 | * here |
| 7293 | */ |
| 7294 | if (stcb->asoc.state == 0) { |
| 7295 | /* the assoc was freed? */ |
| 7296 | return (-19); |
| 7297 | } |
| 7298 | if (net != NULL) { |
| 7299 | /* clear flag */ |
| 7300 | net->dest_state &= |
| 7301 | ~SCTP_ADDR_NOT_IN_ASSOC; |
| 7302 | } |
| 7303 | } else { |
| 7304 | /* |
| 7305 | * strange, address is in another |
| 7306 | * assoc? straighten out locks. |
| 7307 | */ |
Michael Tüxen | ac14228 | 2015-07-28 00:42:03 +0200 | [diff] [blame] | 7308 | if (stcb_tmp) { |
Michael Tuexen | 348a36c | 2018-08-13 16:24:47 +0200 | [diff] [blame] | 7309 | if (SCTP_GET_STATE(stcb_tmp) == SCTP_STATE_COOKIE_WAIT) { |
Michael Tüxen | ac14228 | 2015-07-28 00:42:03 +0200 | [diff] [blame] | 7310 | struct mbuf *op_err; |
| 7311 | char msg[SCTP_DIAG_INFO_LEN]; |
| 7312 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7313 | /* in setup state we abort this guy */ |
Michael Tuexen | edd369d | 2020-05-19 09:42:15 +0200 | [diff] [blame] | 7314 | SCTP_SNPRINTF(msg, sizeof(msg), |
| 7315 | "%s:%d at %s", __FILE__, __LINE__, __func__); |
Michael Tüxen | ac14228 | 2015-07-28 00:42:03 +0200 | [diff] [blame] | 7316 | op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), |
| 7317 | msg); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7318 | sctp_abort_an_association(stcb_tmp->sctp_ep, |
Michael Tuexen | 1ade45c | 2021-07-09 23:32:42 +0200 | [diff] [blame] | 7319 | stcb_tmp, op_err, false, |
Michael Tüxen | ac14228 | 2015-07-28 00:42:03 +0200 | [diff] [blame] | 7320 | SCTP_SO_NOT_LOCKED); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7321 | goto add_it_now6; |
| 7322 | } |
Michael Tüxen | ac14228 | 2015-07-28 00:42:03 +0200 | [diff] [blame] | 7323 | SCTP_TCB_UNLOCK(stcb_tmp); |
| 7324 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7325 | if (stcb->asoc.state == 0) { |
| 7326 | /* the assoc was freed? */ |
| 7327 | return (-21); |
| 7328 | } |
| 7329 | return (-22); |
| 7330 | } |
| 7331 | } |
| 7332 | } else |
| 7333 | #endif |
| 7334 | if (ptype == SCTP_ECN_CAPABLE) { |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7335 | peer_supports_ecn = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7336 | } else if (ptype == SCTP_ULP_ADAPTATION) { |
| 7337 | if (stcb->asoc.state != SCTP_STATE_OPEN) { |
| 7338 | struct sctp_adaptation_layer_indication ai, *aip; |
| 7339 | |
| 7340 | phdr = sctp_get_next_param(m, offset, |
| 7341 | (struct sctp_paramhdr *)&ai, sizeof(ai)); |
| 7342 | aip = (struct sctp_adaptation_layer_indication *)phdr; |
| 7343 | if (aip) { |
| 7344 | stcb->asoc.peers_adaptation = ntohl(aip->indication); |
| 7345 | stcb->asoc.adaptation_needed = 1; |
| 7346 | } |
| 7347 | } |
| 7348 | } else if (ptype == SCTP_SET_PRIM_ADDR) { |
| 7349 | struct sctp_asconf_addr_param lstore, *fee; |
| 7350 | int lptype; |
| 7351 | struct sockaddr *lsa = NULL; |
| 7352 | #ifdef INET |
| 7353 | struct sctp_asconf_addrv4_param *fii; |
| 7354 | #endif |
| 7355 | |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7356 | if (stcb->asoc.asconf_supported == 0) { |
| 7357 | return (-100); |
| 7358 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7359 | if (plen > sizeof(lstore)) { |
| 7360 | return (-23); |
| 7361 | } |
Michael Tuexen | 790a7a2 | 2019-12-20 17:02:02 +0100 | [diff] [blame] | 7362 | if (plen < sizeof(struct sctp_asconf_addrv4_param)) { |
| 7363 | return (-101); |
| 7364 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7365 | phdr = sctp_get_next_param(m, offset, |
| 7366 | (struct sctp_paramhdr *)&lstore, |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 7367 | plen); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7368 | if (phdr == NULL) { |
| 7369 | return (-24); |
| 7370 | } |
| 7371 | fee = (struct sctp_asconf_addr_param *)phdr; |
| 7372 | lptype = ntohs(fee->addrp.ph.param_type); |
| 7373 | switch (lptype) { |
| 7374 | #ifdef INET |
| 7375 | case SCTP_IPV4_ADDRESS: |
| 7376 | if (plen != |
| 7377 | sizeof(struct sctp_asconf_addrv4_param)) { |
| 7378 | SCTP_PRINTF("Sizeof setprim in init/init ack not %d but %d - ignored\n", |
| 7379 | (int)sizeof(struct sctp_asconf_addrv4_param), |
| 7380 | plen); |
| 7381 | } else { |
| 7382 | fii = (struct sctp_asconf_addrv4_param *)fee; |
| 7383 | sin.sin_addr.s_addr = fii->addrp.addr; |
| 7384 | lsa = (struct sockaddr *)&sin; |
| 7385 | } |
| 7386 | break; |
| 7387 | #endif |
| 7388 | #ifdef INET6 |
| 7389 | case SCTP_IPV6_ADDRESS: |
| 7390 | if (plen != |
| 7391 | sizeof(struct sctp_asconf_addr_param)) { |
| 7392 | SCTP_PRINTF("Sizeof setprim (v6) in init/init ack not %d but %d - ignored\n", |
| 7393 | (int)sizeof(struct sctp_asconf_addr_param), |
| 7394 | plen); |
| 7395 | } else { |
| 7396 | memcpy(sin6.sin6_addr.s6_addr, |
| 7397 | fee->addrp.addr, |
| 7398 | sizeof(fee->addrp.addr)); |
| 7399 | lsa = (struct sockaddr *)&sin6; |
| 7400 | } |
| 7401 | break; |
| 7402 | #endif |
| 7403 | default: |
| 7404 | break; |
| 7405 | } |
| 7406 | if (lsa) { |
| 7407 | (void)sctp_set_primary_addr(stcb, sa, NULL); |
| 7408 | } |
| 7409 | } else if (ptype == SCTP_HAS_NAT_SUPPORT) { |
| 7410 | stcb->asoc.peer_supports_nat = 1; |
| 7411 | } else if (ptype == SCTP_PRSCTP_SUPPORTED) { |
| 7412 | /* Peer supports pr-sctp */ |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7413 | peer_supports_prsctp = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7414 | } else if (ptype == SCTP_SUPPORTED_CHUNK_EXT) { |
| 7415 | /* A supported extension chunk */ |
| 7416 | struct sctp_supported_chunk_types_param *pr_supported; |
| 7417 | uint8_t local_store[SCTP_PARAM_BUFFER_SIZE]; |
| 7418 | int num_ent, i; |
| 7419 | |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 7420 | if (plen > sizeof(local_store)) { |
| 7421 | return (-35); |
| 7422 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7423 | phdr = sctp_get_next_param(m, offset, |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 7424 | (struct sctp_paramhdr *)&local_store, plen); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7425 | if (phdr == NULL) { |
| 7426 | return (-25); |
| 7427 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7428 | pr_supported = (struct sctp_supported_chunk_types_param *)phdr; |
| 7429 | num_ent = plen - sizeof(struct sctp_paramhdr); |
| 7430 | for (i = 0; i < num_ent; i++) { |
| 7431 | switch (pr_supported->chunk_types[i]) { |
| 7432 | case SCTP_ASCONF: |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7433 | peer_supports_asconf = 1; |
t00fcxen | 10c9698 | 2014-12-17 20:37:01 +0000 | [diff] [blame] | 7434 | break; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7435 | case SCTP_ASCONF_ACK: |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7436 | peer_supports_asconf_ack = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7437 | break; |
| 7438 | case SCTP_FORWARD_CUM_TSN: |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7439 | peer_supports_prsctp = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7440 | break; |
| 7441 | case SCTP_PACKET_DROPPED: |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7442 | peer_supports_pktdrop = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7443 | break; |
| 7444 | case SCTP_NR_SELECTIVE_ACK: |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7445 | peer_supports_nrsack = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7446 | break; |
| 7447 | case SCTP_STREAM_RESET: |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7448 | peer_supports_reconfig = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7449 | break; |
| 7450 | case SCTP_AUTHENTICATION: |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7451 | peer_supports_auth = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7452 | break; |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7453 | case SCTP_IDATA: |
| 7454 | peer_supports_idata = 1; |
| 7455 | break; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7456 | default: |
| 7457 | /* one I have not learned yet */ |
| 7458 | break; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7459 | } |
| 7460 | } |
| 7461 | } else if (ptype == SCTP_RANDOM) { |
| 7462 | if (plen > sizeof(random_store)) |
| 7463 | break; |
| 7464 | if (got_random) { |
| 7465 | /* already processed a RANDOM */ |
| 7466 | goto next_param; |
| 7467 | } |
| 7468 | phdr = sctp_get_next_param(m, offset, |
| 7469 | (struct sctp_paramhdr *)random_store, |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 7470 | plen); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7471 | if (phdr == NULL) |
| 7472 | return (-26); |
| 7473 | p_random = (struct sctp_auth_random *)phdr; |
| 7474 | random_len = plen - sizeof(*p_random); |
| 7475 | /* enforce the random length */ |
| 7476 | if (random_len != SCTP_AUTH_RANDOM_SIZE_REQUIRED) { |
| 7477 | SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: invalid RANDOM len\n"); |
| 7478 | return (-27); |
| 7479 | } |
| 7480 | got_random = 1; |
| 7481 | } else if (ptype == SCTP_HMAC_LIST) { |
t00fcxen | 05d0bb9 | 2014-09-16 14:25:27 +0000 | [diff] [blame] | 7482 | uint16_t num_hmacs; |
| 7483 | uint16_t i; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7484 | |
| 7485 | if (plen > sizeof(hmacs_store)) |
| 7486 | break; |
| 7487 | if (got_hmacs) { |
| 7488 | /* already processed a HMAC list */ |
| 7489 | goto next_param; |
| 7490 | } |
| 7491 | phdr = sctp_get_next_param(m, offset, |
| 7492 | (struct sctp_paramhdr *)hmacs_store, |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 7493 | plen); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7494 | if (phdr == NULL) |
| 7495 | return (-28); |
| 7496 | hmacs = (struct sctp_auth_hmac_algo *)phdr; |
| 7497 | hmacs_len = plen - sizeof(*hmacs); |
| 7498 | num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]); |
| 7499 | /* validate the hmac list */ |
| 7500 | if (sctp_verify_hmac_param(hmacs, num_hmacs)) { |
| 7501 | return (-29); |
| 7502 | } |
| 7503 | if (stcb->asoc.peer_hmacs != NULL) |
| 7504 | sctp_free_hmaclist(stcb->asoc.peer_hmacs); |
| 7505 | stcb->asoc.peer_hmacs = sctp_alloc_hmaclist(num_hmacs); |
| 7506 | if (stcb->asoc.peer_hmacs != NULL) { |
| 7507 | for (i = 0; i < num_hmacs; i++) { |
| 7508 | (void)sctp_auth_add_hmacid(stcb->asoc.peer_hmacs, |
| 7509 | ntohs(hmacs->hmac_ids[i])); |
| 7510 | } |
| 7511 | } |
| 7512 | got_hmacs = 1; |
| 7513 | } else if (ptype == SCTP_CHUNK_LIST) { |
| 7514 | int i; |
| 7515 | |
| 7516 | if (plen > sizeof(chunks_store)) |
| 7517 | break; |
| 7518 | if (got_chklist) { |
| 7519 | /* already processed a Chunks list */ |
| 7520 | goto next_param; |
| 7521 | } |
| 7522 | phdr = sctp_get_next_param(m, offset, |
| 7523 | (struct sctp_paramhdr *)chunks_store, |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 7524 | plen); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7525 | if (phdr == NULL) |
| 7526 | return (-30); |
| 7527 | chunks = (struct sctp_auth_chunk_list *)phdr; |
| 7528 | num_chunks = plen - sizeof(*chunks); |
| 7529 | if (stcb->asoc.peer_auth_chunks != NULL) |
| 7530 | sctp_clear_chunklist(stcb->asoc.peer_auth_chunks); |
| 7531 | else |
| 7532 | stcb->asoc.peer_auth_chunks = sctp_alloc_chunklist(); |
| 7533 | for (i = 0; i < num_chunks; i++) { |
| 7534 | (void)sctp_auth_add_chunk(chunks->chunk_types[i], |
| 7535 | stcb->asoc.peer_auth_chunks); |
| 7536 | /* record asconf/asconf-ack if listed */ |
| 7537 | if (chunks->chunk_types[i] == SCTP_ASCONF) |
| 7538 | saw_asconf = 1; |
| 7539 | if (chunks->chunk_types[i] == SCTP_ASCONF_ACK) |
| 7540 | saw_asconf_ack = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7541 | } |
| 7542 | got_chklist = 1; |
| 7543 | } else if ((ptype == SCTP_HEARTBEAT_INFO) || |
| 7544 | (ptype == SCTP_STATE_COOKIE) || |
| 7545 | (ptype == SCTP_UNRECOG_PARAM) || |
| 7546 | (ptype == SCTP_COOKIE_PRESERVE) || |
| 7547 | (ptype == SCTP_SUPPORTED_ADDRTYPE) || |
| 7548 | (ptype == SCTP_ADD_IP_ADDRESS) || |
| 7549 | (ptype == SCTP_DEL_IP_ADDRESS) || |
| 7550 | (ptype == SCTP_ERROR_CAUSE_IND) || |
| 7551 | (ptype == SCTP_SUCCESS_REPORT)) { |
Michael Tuexen | 196fbbe | 2020-09-28 17:04:25 +0200 | [diff] [blame] | 7552 | /* don't care */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7553 | } else { |
| 7554 | if ((ptype & 0x8000) == 0x0000) { |
| 7555 | /* |
| 7556 | * must stop processing the rest of the |
| 7557 | * param's. Any report bits were handled |
| 7558 | * with the call to |
| 7559 | * sctp_arethere_unrecognized_parameters() |
| 7560 | * when the INIT or INIT-ACK was first seen. |
| 7561 | */ |
| 7562 | break; |
| 7563 | } |
| 7564 | } |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 7565 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7566 | next_param: |
| 7567 | offset += SCTP_SIZE32(plen); |
| 7568 | if (offset >= limit) { |
| 7569 | break; |
| 7570 | } |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 7571 | phdr = sctp_get_next_param(m, offset, ¶m_buf, |
Michael Tuexen | 6e02669 | 2021-05-03 02:56:21 +0200 | [diff] [blame] | 7572 | sizeof(param_buf)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7573 | } |
| 7574 | /* Now check to see if we need to purge any addresses */ |
| 7575 | TAILQ_FOREACH_SAFE(net, &stcb->asoc.nets, sctp_next, nnet) { |
| 7576 | if ((net->dest_state & SCTP_ADDR_NOT_IN_ASSOC) == |
| 7577 | SCTP_ADDR_NOT_IN_ASSOC) { |
| 7578 | /* This address has been removed from the asoc */ |
| 7579 | /* remove and free it */ |
| 7580 | stcb->asoc.numnets--; |
| 7581 | TAILQ_REMOVE(&stcb->asoc.nets, net, sctp_next); |
Michael Tuexen | 6e02669 | 2021-05-03 02:56:21 +0200 | [diff] [blame] | 7582 | if (net == stcb->asoc.alternate) { |
| 7583 | sctp_free_remote_addr(stcb->asoc.alternate); |
| 7584 | stcb->asoc.alternate = NULL; |
| 7585 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7586 | if (net == stcb->asoc.primary_destination) { |
| 7587 | stcb->asoc.primary_destination = NULL; |
| 7588 | sctp_select_primary_destination(stcb); |
| 7589 | } |
Michael Tuexen | 6e02669 | 2021-05-03 02:56:21 +0200 | [diff] [blame] | 7590 | sctp_free_remote_addr(net); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7591 | } |
| 7592 | } |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7593 | if ((stcb->asoc.ecn_supported == 1) && |
| 7594 | (peer_supports_ecn == 0)) { |
| 7595 | stcb->asoc.ecn_supported = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7596 | } |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7597 | if ((stcb->asoc.prsctp_supported == 1) && |
| 7598 | (peer_supports_prsctp == 0)) { |
| 7599 | stcb->asoc.prsctp_supported = 0; |
| 7600 | } |
| 7601 | if ((stcb->asoc.auth_supported == 1) && |
| 7602 | ((peer_supports_auth == 0) || |
| 7603 | (got_random == 0) || (got_hmacs == 0))) { |
| 7604 | stcb->asoc.auth_supported = 0; |
| 7605 | } |
| 7606 | if ((stcb->asoc.asconf_supported == 1) && |
| 7607 | ((peer_supports_asconf == 0) || (peer_supports_asconf_ack == 0) || |
| 7608 | (stcb->asoc.auth_supported == 0) || |
| 7609 | (saw_asconf == 0) || (saw_asconf_ack == 0))) { |
| 7610 | stcb->asoc.asconf_supported = 0; |
| 7611 | } |
| 7612 | if ((stcb->asoc.reconfig_supported == 1) && |
| 7613 | (peer_supports_reconfig == 0)) { |
| 7614 | stcb->asoc.reconfig_supported = 0; |
| 7615 | } |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7616 | if ((stcb->asoc.idata_supported == 1) && |
| 7617 | (peer_supports_idata == 0)) { |
| 7618 | stcb->asoc.idata_supported = 0; |
| 7619 | } |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7620 | if ((stcb->asoc.nrsack_supported == 1) && |
| 7621 | (peer_supports_nrsack == 0)) { |
| 7622 | stcb->asoc.nrsack_supported = 0; |
| 7623 | } |
| 7624 | if ((stcb->asoc.pktdrop_supported == 1) && |
Michael Tuexen | 8d6935d | 2022-05-15 01:51:24 +0200 | [diff] [blame] | 7625 | (peer_supports_pktdrop == 0)) { |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7626 | stcb->asoc.pktdrop_supported = 0; |
| 7627 | } |
| 7628 | /* validate authentication required parameters */ |
| 7629 | if ((peer_supports_auth == 0) && (got_chklist == 1)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7630 | /* peer does not support auth but sent a chunks list? */ |
| 7631 | return (-31); |
| 7632 | } |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7633 | if ((peer_supports_asconf == 1) && (peer_supports_auth == 0)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7634 | /* peer supports asconf but not auth? */ |
| 7635 | return (-32); |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7636 | } else if ((peer_supports_asconf == 1) && |
| 7637 | (peer_supports_auth == 1) && |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7638 | ((saw_asconf == 0) || (saw_asconf_ack == 0))) { |
| 7639 | return (-33); |
| 7640 | } |
| 7641 | /* concatenate the full random key */ |
| 7642 | keylen = sizeof(*p_random) + random_len + sizeof(*hmacs) + hmacs_len; |
| 7643 | if (chunks != NULL) { |
| 7644 | keylen += sizeof(*chunks) + num_chunks; |
| 7645 | } |
| 7646 | new_key = sctp_alloc_key(keylen); |
| 7647 | if (new_key != NULL) { |
| 7648 | /* copy in the RANDOM */ |
| 7649 | if (p_random != NULL) { |
| 7650 | keylen = sizeof(*p_random) + random_len; |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 7651 | memcpy(new_key->key, p_random, keylen); |
Michael Tuexen | 8789a6d | 2018-06-02 18:33:47 +0200 | [diff] [blame] | 7652 | } else { |
| 7653 | keylen = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7654 | } |
| 7655 | /* append in the AUTH chunks */ |
| 7656 | if (chunks != NULL) { |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 7657 | memcpy(new_key->key + keylen, chunks, |
| 7658 | sizeof(*chunks) + num_chunks); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7659 | keylen += sizeof(*chunks) + num_chunks; |
| 7660 | } |
| 7661 | /* append in the HMACs */ |
| 7662 | if (hmacs != NULL) { |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 7663 | memcpy(new_key->key + keylen, hmacs, |
| 7664 | sizeof(*hmacs) + hmacs_len); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7665 | } |
| 7666 | } else { |
| 7667 | /* failed to get memory for the key */ |
| 7668 | return (-34); |
| 7669 | } |
| 7670 | if (stcb->asoc.authinfo.peer_random != NULL) |
| 7671 | sctp_free_key(stcb->asoc.authinfo.peer_random); |
| 7672 | stcb->asoc.authinfo.peer_random = new_key; |
| 7673 | sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid); |
| 7674 | sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid); |
| 7675 | |
| 7676 | return (0); |
| 7677 | } |
| 7678 | |
| 7679 | int |
| 7680 | sctp_set_primary_addr(struct sctp_tcb *stcb, struct sockaddr *sa, |
| 7681 | struct sctp_nets *net) |
| 7682 | { |
| 7683 | /* make sure the requested primary address exists in the assoc */ |
| 7684 | if (net == NULL && sa) |
| 7685 | net = sctp_findnet(stcb, sa); |
| 7686 | |
| 7687 | if (net == NULL) { |
| 7688 | /* didn't find the requested primary address! */ |
| 7689 | return (-1); |
| 7690 | } else { |
| 7691 | /* set the primary address */ |
| 7692 | if (net->dest_state & SCTP_ADDR_UNCONFIRMED) { |
| 7693 | /* Must be confirmed, so queue to set */ |
| 7694 | net->dest_state |= SCTP_ADDR_REQ_PRIMARY; |
| 7695 | return (0); |
| 7696 | } |
| 7697 | stcb->asoc.primary_destination = net; |
Michael Tuexen | 8d6935d | 2022-05-15 01:51:24 +0200 | [diff] [blame] | 7698 | if (((net->dest_state & SCTP_ADDR_PF) == 0) && |
| 7699 | (stcb->asoc.alternate != NULL)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7700 | sctp_free_remote_addr(stcb->asoc.alternate); |
| 7701 | stcb->asoc.alternate = NULL; |
| 7702 | } |
| 7703 | net = TAILQ_FIRST(&stcb->asoc.nets); |
| 7704 | if (net != stcb->asoc.primary_destination) { |
| 7705 | /* first one on the list is NOT the primary |
| 7706 | * sctp_cmpaddr() is much more efficient if |
| 7707 | * the primary is the first on the list, make it |
| 7708 | * so. |
| 7709 | */ |
| 7710 | TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next); |
| 7711 | TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next); |
| 7712 | } |
| 7713 | return (0); |
| 7714 | } |
| 7715 | } |
| 7716 | |
Michael Tuexen | 4ec13f7 | 2021-09-09 01:22:43 +0200 | [diff] [blame] | 7717 | bool |
tuexen | 9784e9a | 2011-12-18 13:04:23 +0000 | [diff] [blame] | 7718 | sctp_is_vtag_good(uint32_t tag, uint16_t lport, uint16_t rport, struct timeval *now) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7719 | { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7720 | struct sctpasochead *head; |
| 7721 | struct sctp_tcb *stcb; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7722 | |
Michael Tuexen | 8dd0be7 | 2021-09-12 19:27:59 +0200 | [diff] [blame] | 7723 | SCTP_INP_INFO_LOCK_ASSERT(); |
| 7724 | |
Michael Tuexen | 4ec13f7 | 2021-09-09 01:22:43 +0200 | [diff] [blame] | 7725 | head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag, SCTP_BASE_INFO(hashasocmark))]; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7726 | LIST_FOREACH(stcb, head, sctp_asocs) { |
| 7727 | /* We choose not to lock anything here. TCB's can't be |
| 7728 | * removed since we have the read lock, so they can't |
| 7729 | * be freed on us, same thing for the INP. I may |
| 7730 | * be wrong with this assumption, but we will go |
| 7731 | * with it for now :-) |
| 7732 | */ |
| 7733 | if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
| 7734 | continue; |
| 7735 | } |
| 7736 | if (stcb->asoc.my_vtag == tag) { |
| 7737 | /* candidate */ |
| 7738 | if (stcb->rport != rport) { |
| 7739 | continue; |
| 7740 | } |
| 7741 | if (stcb->sctp_ep->sctp_lport != lport) { |
| 7742 | continue; |
| 7743 | } |
Michael Tuexen | 4ec13f7 | 2021-09-09 01:22:43 +0200 | [diff] [blame] | 7744 | /* The tag is currently used, so don't use it. */ |
Michael Tuexen | 8dd0be7 | 2021-09-12 19:27:59 +0200 | [diff] [blame] | 7745 | return (false); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7746 | } |
| 7747 | } |
Michael Tuexen | 8dd0be7 | 2021-09-12 19:27:59 +0200 | [diff] [blame] | 7748 | return (!sctp_is_in_timewait(tag, lport, rport, (uint32_t)now->tv_sec)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7749 | } |
| 7750 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7751 | static void |
tuexen | 9784e9a | 2011-12-18 13:04:23 +0000 | [diff] [blame] | 7752 | sctp_drain_mbufs(struct sctp_tcb *stcb) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7753 | { |
| 7754 | /* |
| 7755 | * We must hunt this association for MBUF's past the cumack (i.e. |
| 7756 | * out of order data that we can renege on). |
| 7757 | */ |
| 7758 | struct sctp_association *asoc; |
| 7759 | struct sctp_tmit_chunk *chk, *nchk; |
| 7760 | uint32_t cumulative_tsn_p1; |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7761 | struct sctp_queued_to_read *control, *ncontrol; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7762 | int cnt, strmat; |
| 7763 | uint32_t gap, i; |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 7764 | int fnd = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7765 | |
| 7766 | /* We look for anything larger than the cum-ack + 1 */ |
| 7767 | |
| 7768 | asoc = &stcb->asoc; |
| 7769 | if (asoc->cumulative_tsn == asoc->highest_tsn_inside_map) { |
| 7770 | /* none we can reneg on. */ |
| 7771 | return; |
| 7772 | } |
| 7773 | SCTP_STAT_INCR(sctps_protocol_drains_done); |
| 7774 | cumulative_tsn_p1 = asoc->cumulative_tsn + 1; |
| 7775 | cnt = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7776 | /* Ok that was fun, now we will drain all the inbound streams? */ |
| 7777 | for (strmat = 0; strmat < asoc->streamincnt; strmat++) { |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7778 | TAILQ_FOREACH_SAFE(control, &asoc->strmin[strmat].inqueue, next_instrm, ncontrol) { |
Michael Tuexen | fdcf790 | 2016-08-06 14:39:31 +0200 | [diff] [blame] | 7779 | #ifdef INVARIANTS |
Michael Tuexen | 53579be | 2020-10-07 17:39:17 +0200 | [diff] [blame] | 7780 | if (control->on_strm_q != SCTP_ON_ORDERED) { |
Michael Tuexen | fdcf790 | 2016-08-06 14:39:31 +0200 | [diff] [blame] | 7781 | panic("Huh control: %p on_q: %d -- not ordered?", |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7782 | control, control->on_strm_q); |
Michael Tuexen | fdcf790 | 2016-08-06 14:39:31 +0200 | [diff] [blame] | 7783 | } |
| 7784 | #endif |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7785 | if (SCTP_TSN_GT(control->sinfo_tsn, cumulative_tsn_p1)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7786 | /* Yep it is above cum-ack */ |
| 7787 | cnt++; |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7788 | SCTP_CALC_TSN_TO_GAP(gap, control->sinfo_tsn, asoc->mapping_array_base_tsn); |
| 7789 | KASSERT(control->length > 0, ("control has zero length")); |
| 7790 | if (asoc->size_on_all_streams >= control->length) { |
| 7791 | asoc->size_on_all_streams -= control->length; |
| 7792 | } else { |
| 7793 | #ifdef INVARIANTS |
| 7794 | panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length); |
| 7795 | #else |
| 7796 | asoc->size_on_all_streams = 0; |
| 7797 | #endif |
| 7798 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7799 | sctp_ucount_decr(asoc->cnt_on_all_streams); |
| 7800 | SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap); |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7801 | if (control->on_read_q) { |
| 7802 | TAILQ_REMOVE(&stcb->sctp_ep->read_queue, control, next); |
| 7803 | control->on_read_q = 0; |
Michael Tuexen | fdcf790 | 2016-08-06 14:39:31 +0200 | [diff] [blame] | 7804 | } |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7805 | TAILQ_REMOVE(&asoc->strmin[strmat].inqueue, control, next_instrm); |
| 7806 | control->on_strm_q = 0; |
| 7807 | if (control->data) { |
| 7808 | sctp_m_freem(control->data); |
| 7809 | control->data = NULL; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7810 | } |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7811 | sctp_free_remote_addr(control->whoFrom); |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7812 | /* Now its reasm? */ |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7813 | TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) { |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7814 | cnt++; |
Michael Tuexen | 00657ac | 2016-12-07 21:53:26 +0100 | [diff] [blame] | 7815 | SCTP_CALC_TSN_TO_GAP(gap, chk->rec.data.tsn, asoc->mapping_array_base_tsn); |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7816 | KASSERT(chk->send_size > 0, ("chunk has zero length")); |
| 7817 | if (asoc->size_on_reasm_queue >= chk->send_size) { |
| 7818 | asoc->size_on_reasm_queue -= chk->send_size; |
| 7819 | } else { |
| 7820 | #ifdef INVARIANTS |
| 7821 | panic("size_on_reasm_queue = %u smaller than chunk length %u", asoc->size_on_reasm_queue, chk->send_size); |
| 7822 | #else |
| 7823 | asoc->size_on_reasm_queue = 0; |
| 7824 | #endif |
| 7825 | } |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7826 | sctp_ucount_decr(asoc->cnt_on_reasm_queue); |
| 7827 | SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap); |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7828 | TAILQ_REMOVE(&control->reasm, chk, sctp_next); |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7829 | if (chk->data) { |
| 7830 | sctp_m_freem(chk->data); |
| 7831 | chk->data = NULL; |
| 7832 | } |
| 7833 | sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); |
| 7834 | } |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7835 | sctp_free_a_readq(stcb, control); |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7836 | } |
| 7837 | } |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7838 | TAILQ_FOREACH_SAFE(control, &asoc->strmin[strmat].uno_inqueue, next_instrm, ncontrol) { |
Michael Tuexen | fdcf790 | 2016-08-06 14:39:31 +0200 | [diff] [blame] | 7839 | #ifdef INVARIANTS |
Michael Tuexen | 53579be | 2020-10-07 17:39:17 +0200 | [diff] [blame] | 7840 | if (control->on_strm_q != SCTP_ON_UNORDERED) { |
Michael Tuexen | fdcf790 | 2016-08-06 14:39:31 +0200 | [diff] [blame] | 7841 | panic("Huh control: %p on_q: %d -- not unordered?", |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7842 | control, control->on_strm_q); |
Michael Tuexen | fdcf790 | 2016-08-06 14:39:31 +0200 | [diff] [blame] | 7843 | } |
| 7844 | #endif |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7845 | if (SCTP_TSN_GT(control->sinfo_tsn, cumulative_tsn_p1)) { |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7846 | /* Yep it is above cum-ack */ |
| 7847 | cnt++; |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7848 | SCTP_CALC_TSN_TO_GAP(gap, control->sinfo_tsn, asoc->mapping_array_base_tsn); |
| 7849 | KASSERT(control->length > 0, ("control has zero length")); |
| 7850 | if (asoc->size_on_all_streams >= control->length) { |
| 7851 | asoc->size_on_all_streams -= control->length; |
| 7852 | } else { |
| 7853 | #ifdef INVARIANTS |
| 7854 | panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length); |
| 7855 | #else |
| 7856 | asoc->size_on_all_streams = 0; |
| 7857 | #endif |
| 7858 | } |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7859 | sctp_ucount_decr(asoc->cnt_on_all_streams); |
| 7860 | SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap); |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7861 | if (control->on_read_q) { |
| 7862 | TAILQ_REMOVE(&stcb->sctp_ep->read_queue, control, next); |
| 7863 | control->on_read_q = 0; |
Michael Tuexen | fdcf790 | 2016-08-06 14:39:31 +0200 | [diff] [blame] | 7864 | } |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7865 | TAILQ_REMOVE(&asoc->strmin[strmat].uno_inqueue, control, next_instrm); |
| 7866 | control->on_strm_q = 0; |
| 7867 | if (control->data) { |
| 7868 | sctp_m_freem(control->data); |
| 7869 | control->data = NULL; |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7870 | } |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7871 | sctp_free_remote_addr(control->whoFrom); |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7872 | /* Now its reasm? */ |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7873 | TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) { |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7874 | cnt++; |
Michael Tuexen | 00657ac | 2016-12-07 21:53:26 +0100 | [diff] [blame] | 7875 | SCTP_CALC_TSN_TO_GAP(gap, chk->rec.data.tsn, asoc->mapping_array_base_tsn); |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7876 | KASSERT(chk->send_size > 0, ("chunk has zero length")); |
| 7877 | if (asoc->size_on_reasm_queue >= chk->send_size) { |
| 7878 | asoc->size_on_reasm_queue -= chk->send_size; |
| 7879 | } else { |
| 7880 | #ifdef INVARIANTS |
| 7881 | panic("size_on_reasm_queue = %u smaller than chunk length %u", asoc->size_on_reasm_queue, chk->send_size); |
| 7882 | #else |
| 7883 | asoc->size_on_reasm_queue = 0; |
| 7884 | #endif |
| 7885 | } |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7886 | sctp_ucount_decr(asoc->cnt_on_reasm_queue); |
| 7887 | SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap); |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7888 | TAILQ_REMOVE(&control->reasm, chk, sctp_next); |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7889 | if (chk->data) { |
| 7890 | sctp_m_freem(chk->data); |
| 7891 | chk->data = NULL; |
| 7892 | } |
| 7893 | sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); |
| 7894 | } |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7895 | sctp_free_a_readq(stcb, control); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7896 | } |
| 7897 | } |
| 7898 | } |
| 7899 | if (cnt) { |
| 7900 | /* We must back down to see what the new highest is */ |
| 7901 | for (i = asoc->highest_tsn_inside_map; SCTP_TSN_GE(i, asoc->mapping_array_base_tsn); i--) { |
| 7902 | SCTP_CALC_TSN_TO_GAP(gap, i, asoc->mapping_array_base_tsn); |
| 7903 | if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) { |
| 7904 | asoc->highest_tsn_inside_map = i; |
| 7905 | fnd = 1; |
| 7906 | break; |
| 7907 | } |
| 7908 | } |
| 7909 | if (!fnd) { |
| 7910 | asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn - 1; |
| 7911 | } |
| 7912 | |
| 7913 | /* |
| 7914 | * Question, should we go through the delivery queue? The only |
| 7915 | * reason things are on here is the app not reading OR a p-d-api up. |
| 7916 | * An attacker COULD send enough in to initiate the PD-API and then |
| 7917 | * send a bunch of stuff to other streams... these would wind up on |
| 7918 | * the delivery queue.. and then we would not get to them. But in |
| 7919 | * order to do this I then have to back-track and un-deliver |
| 7920 | * sequence numbers in streams.. el-yucko. I think for now we will |
| 7921 | * NOT look at the delivery queue and leave it to be something to |
| 7922 | * consider later. An alternative would be to abort the P-D-API with |
| 7923 | * a notification and then deliver the data.... Or another method |
| 7924 | * might be to keep track of how many times the situation occurs and |
| 7925 | * if we see a possible attack underway just abort the association. |
| 7926 | */ |
| 7927 | #ifdef SCTP_DEBUG |
| 7928 | SCTPDBG(SCTP_DEBUG_PCB1, "Freed %d chunks from reneg harvest\n", cnt); |
| 7929 | #endif |
| 7930 | /* |
| 7931 | * Now do we need to find a new |
| 7932 | * asoc->highest_tsn_inside_map? |
| 7933 | */ |
| 7934 | asoc->last_revoke_count = cnt; |
Michael Tuexen | 61f6683 | 2020-02-09 23:15:19 +0100 | [diff] [blame] | 7935 | sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL, |
Michael Tuexen | 21b55df | 2020-02-09 23:46:32 +0100 | [diff] [blame] | 7936 | SCTP_FROM_SCTP_PCB + SCTP_LOC_11); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7937 | /*sa_ignore NO_NULL_CHK*/ |
| 7938 | sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED); |
| 7939 | sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_DRAIN, SCTP_SO_NOT_LOCKED); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7940 | } |
| 7941 | /* |
| 7942 | * Another issue, in un-setting the TSN's in the mapping array we |
| 7943 | * DID NOT adjust the highest_tsn marker. This will cause one of two |
| 7944 | * things to occur. It may cause us to do extra work in checking for |
| 7945 | * our mapping array movement. More importantly it may cause us to |
| 7946 | * SACK every datagram. This may not be a bad thing though since we |
| 7947 | * will recover once we get our cum-ack above and all this stuff we |
| 7948 | * dumped recovered. |
| 7949 | */ |
| 7950 | } |
| 7951 | |
| 7952 | void |
Michael Tuexen | e19d122 | 2022-08-03 17:35:14 +0200 | [diff] [blame^] | 7953 | sctp_drain(void) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7954 | { |
| 7955 | /* |
| 7956 | * We must walk the PCB lists for ALL associations here. The system |
| 7957 | * is LOW on MBUF's and needs help. This is where reneging will |
| 7958 | * occur. We really hope this does NOT happen! |
| 7959 | */ |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 7960 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7961 | VNET_ITERATOR_DECL(vnet_iter); |
| 7962 | #else |
| 7963 | struct sctp_inpcb *inp; |
| 7964 | struct sctp_tcb *stcb; |
| 7965 | |
| 7966 | SCTP_STAT_INCR(sctps_protocol_drain_calls); |
| 7967 | if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) { |
| 7968 | return; |
| 7969 | } |
| 7970 | #endif |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 7971 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7972 | VNET_LIST_RLOCK_NOSLEEP(); |
| 7973 | VNET_FOREACH(vnet_iter) { |
| 7974 | CURVNET_SET(vnet_iter); |
| 7975 | struct sctp_inpcb *inp; |
| 7976 | struct sctp_tcb *stcb; |
| 7977 | #endif |
| 7978 | |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 7979 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7980 | SCTP_STAT_INCR(sctps_protocol_drain_calls); |
| 7981 | if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) { |
| 7982 | #ifdef VIMAGE |
| 7983 | continue; |
| 7984 | #else |
| 7985 | return; |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 7986 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7987 | } |
| 7988 | #endif |
| 7989 | SCTP_INP_INFO_RLOCK(); |
| 7990 | LIST_FOREACH(inp, &SCTP_BASE_INFO(listhead), sctp_list) { |
| 7991 | /* For each endpoint */ |
| 7992 | SCTP_INP_RLOCK(inp); |
| 7993 | LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { |
| 7994 | /* For each association */ |
| 7995 | SCTP_TCB_LOCK(stcb); |
tuexen | 9784e9a | 2011-12-18 13:04:23 +0000 | [diff] [blame] | 7996 | sctp_drain_mbufs(stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7997 | SCTP_TCB_UNLOCK(stcb); |
| 7998 | } |
| 7999 | SCTP_INP_RUNLOCK(inp); |
| 8000 | } |
| 8001 | SCTP_INP_INFO_RUNLOCK(); |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 8002 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 8003 | CURVNET_RESTORE(); |
| 8004 | } |
| 8005 | VNET_LIST_RUNLOCK_NOSLEEP(); |
| 8006 | #endif |
| 8007 | } |
| 8008 | |
| 8009 | /* |
| 8010 | * start a new iterator |
| 8011 | * iterates through all endpoints and associations based on the pcb_state |
| 8012 | * flags and asoc_state. "af" (mandatory) is executed for all matching |
| 8013 | * assocs and "ef" (optional) is executed when the iterator completes. |
| 8014 | * "inpf" (optional) is executed for each new endpoint as it is being |
| 8015 | * iterated through. inpe (optional) is called when the inp completes |
| 8016 | * its way through all the stcbs. |
| 8017 | */ |
| 8018 | int |
| 8019 | sctp_initiate_iterator(inp_func inpf, |
| 8020 | asoc_func af, |
| 8021 | inp_func inpe, |
| 8022 | uint32_t pcb_state, |
| 8023 | uint32_t pcb_features, |
| 8024 | uint32_t asoc_state, |
| 8025 | void *argp, |
| 8026 | uint32_t argi, |
| 8027 | end_func ef, |
| 8028 | struct sctp_inpcb *s_inp, |
| 8029 | uint8_t chunk_output_off) |
| 8030 | { |
| 8031 | struct sctp_iterator *it = NULL; |
| 8032 | |
| 8033 | if (af == NULL) { |
| 8034 | return (-1); |
| 8035 | } |
Michael Tuexen | 71a83fb | 2016-02-16 21:52:58 +0100 | [diff] [blame] | 8036 | if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) { |
| 8037 | SCTP_PRINTF("%s: abort on initialize being %d\n", __func__, |
| 8038 | SCTP_BASE_VAR(sctp_pcb_initialized)); |
| 8039 | return (-1); |
| 8040 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 8041 | SCTP_MALLOC(it, struct sctp_iterator *, sizeof(struct sctp_iterator), |
| 8042 | SCTP_M_ITER); |
| 8043 | if (it == NULL) { |
| 8044 | SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM); |
Michael Tuexen | 287284c | 2020-01-12 14:34:21 +0100 | [diff] [blame] | 8045 | return (-1); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 8046 | } |
| 8047 | memset(it, 0, sizeof(*it)); |
| 8048 | it->function_assoc = af; |
| 8049 | it->function_inp = inpf; |
| 8050 | if (inpf) |
| 8051 | it->done_current_ep = 0; |
| 8052 | else |
| 8053 | it->done_current_ep = 1; |
| 8054 | it->function_atend = ef; |
| 8055 | it->pointer = argp; |
| 8056 | it->val = argi; |
| 8057 | it->pcb_flags = pcb_state; |
| 8058 | it->pcb_features = pcb_features; |
| 8059 | it->asoc_state = asoc_state; |
| 8060 | it->function_inp_end = inpe; |
| 8061 | it->no_chunk_output = chunk_output_off; |
Michael Tuexen | 5be0c25 | 2020-06-13 00:53:56 +0200 | [diff] [blame] | 8062 | #if defined(__FreeBSD__) && !defined(__Userspace__) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 8063 | it->vn = curvnet; |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 8064 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 8065 | if (s_inp) { |
| 8066 | /* Assume lock is held here */ |
| 8067 | it->inp = s_inp; |
| 8068 | SCTP_INP_INCR_REF(it->inp); |
| 8069 | it->iterator_flags = SCTP_ITERATOR_DO_SINGLE_INP; |
| 8070 | } else { |
| 8071 | SCTP_INP_INFO_RLOCK(); |
| 8072 | it->inp = LIST_FIRST(&SCTP_BASE_INFO(listhead)); |
| 8073 | if (it->inp) { |
| 8074 | SCTP_INP_INCR_REF(it->inp); |
| 8075 | } |
| 8076 | SCTP_INP_INFO_RUNLOCK(); |
| 8077 | it->iterator_flags = SCTP_ITERATOR_DO_ALL_INP; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 8078 | } |
| 8079 | SCTP_IPI_ITERATOR_WQ_LOCK(); |
Michael Tuexen | 71a83fb | 2016-02-16 21:52:58 +0100 | [diff] [blame] | 8080 | if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) { |
| 8081 | SCTP_IPI_ITERATOR_WQ_UNLOCK(); |
| 8082 | SCTP_PRINTF("%s: rollback on initialize being %d it=%p\n", __func__, |
| 8083 | SCTP_BASE_VAR(sctp_pcb_initialized), it); |
| 8084 | SCTP_FREE(it, SCTP_M_ITER); |
| 8085 | return (-1); |
| 8086 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 8087 | TAILQ_INSERT_TAIL(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr); |
| 8088 | if (sctp_it_ctl.iterator_running == 0) { |
| 8089 | sctp_wakeup_iterator(); |
| 8090 | } |
| 8091 | SCTP_IPI_ITERATOR_WQ_UNLOCK(); |
| 8092 | /* sa_ignore MEMLEAK {memory is put on the tailq for the iterator} */ |
| 8093 | return (0); |
| 8094 | } |
Michael Tuexen | 9b353c6 | 2022-06-04 11:20:39 +0200 | [diff] [blame] | 8095 | |
| 8096 | /* |
| 8097 | * Atomically add flags to the sctp_flags of an inp. |
| 8098 | * To be used when the write lock of the inp is not held. |
| 8099 | */ |
| 8100 | void |
| 8101 | sctp_pcb_add_flags(struct sctp_inpcb *inp, uint32_t flags) |
| 8102 | { |
| 8103 | uint32_t old_flags, new_flags; |
| 8104 | |
| 8105 | do { |
| 8106 | old_flags = inp->sctp_flags; |
| 8107 | new_flags = old_flags | flags; |
| 8108 | } while (atomic_cmpset_int(&inp->sctp_flags, old_flags, new_flags) == 0); |
| 8109 | } |