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 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 35 | #ifdef __FreeBSD__ |
| 36 | #include <sys/cdefs.h> |
Michael Tuexen | edd369d | 2020-05-19 09:42:15 +0200 | [diff] [blame] | 37 | __FBSDID("$FreeBSD: head/sys/netinet/sctp_pcb.c 361243 2020-05-19 07:23:35Z tuexen $"); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 38 | #endif |
| 39 | |
| 40 | #include <netinet/sctp_os.h> |
| 41 | #ifdef __FreeBSD__ |
| 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) |
tuexen | 9784e9a | 2011-12-18 13:04:23 +0000 | [diff] [blame] | 55 | #if !defined(__Userspace_os_Windows) |
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 |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 66 | #if defined(__FreeBSD__) |
| 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> |
t00fcxen | 37058da | 2015-03-16 21:59:21 +0000 | [diff] [blame] | 74 | #if !defined(__Userspace_os_Windows) |
| 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 | |
| 79 | #if defined(__APPLE__) |
| 80 | #define APPLE_FILE_NO 4 |
| 81 | #endif |
| 82 | |
| 83 | #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 |
| 84 | VNET_DEFINE(struct sctp_base_info, system_base_info); |
| 85 | #else |
| 86 | struct sctp_base_info system_base_info; |
| 87 | #endif |
| 88 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 89 | /* FIX: we don't handle multiple link local scopes */ |
| 90 | /* "scopeless" replacement IN6_ARE_ADDR_EQUAL */ |
| 91 | #ifdef INET6 |
| 92 | int |
| 93 | SCTP6_ARE_ADDR_EQUAL(struct sockaddr_in6 *a, struct sockaddr_in6 *b) |
| 94 | { |
| 95 | #ifdef SCTP_EMBEDDED_V6_SCOPE |
tuexen | e385159 | 2012-06-01 08:31:45 +0000 | [diff] [blame] | 96 | #if defined(__APPLE__) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 97 | struct in6_addr tmp_a, tmp_b; |
| 98 | |
| 99 | tmp_a = a->sin6_addr; |
tuexen | 9a218b1 | 2012-08-04 21:17:58 +0000 | [diff] [blame] | 100 | #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 101 | if (in6_embedscope(&tmp_a, a, NULL, NULL) != 0) { |
tuexen | 9a218b1 | 2012-08-04 21:17:58 +0000 | [diff] [blame] | 102 | #else |
| 103 | if (in6_embedscope(&tmp_a, a, NULL, NULL, NULL) != 0) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 104 | #endif |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 105 | return (0); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 106 | } |
| 107 | tmp_b = b->sin6_addr; |
tuexen | 9a218b1 | 2012-08-04 21:17:58 +0000 | [diff] [blame] | 108 | #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 109 | if (in6_embedscope(&tmp_b, b, NULL, NULL) != 0) { |
tuexen | 9a218b1 | 2012-08-04 21:17:58 +0000 | [diff] [blame] | 110 | #else |
| 111 | if (in6_embedscope(&tmp_b, b, NULL, NULL, NULL) != 0) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 112 | #endif |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 113 | return (0); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 114 | } |
| 115 | return (IN6_ARE_ADDR_EQUAL(&tmp_a, &tmp_b)); |
| 116 | #elif defined(SCTP_KAME) |
| 117 | struct sockaddr_in6 tmp_a, tmp_b; |
| 118 | |
| 119 | memcpy(&tmp_a, a, sizeof(struct sockaddr_in6)); |
| 120 | if (sa6_embedscope(&tmp_a, MODULE_GLOBAL(ip6_use_defzone)) != 0) { |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 121 | return (0); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 122 | } |
| 123 | memcpy(&tmp_b, b, sizeof(struct sockaddr_in6)); |
| 124 | if (sa6_embedscope(&tmp_b, MODULE_GLOBAL(ip6_use_defzone)) != 0) { |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 125 | return (0); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 126 | } |
| 127 | return (IN6_ARE_ADDR_EQUAL(&tmp_a.sin6_addr, &tmp_b.sin6_addr)); |
| 128 | #else |
| 129 | struct in6_addr tmp_a, tmp_b; |
| 130 | |
| 131 | tmp_a = a->sin6_addr; |
| 132 | if (in6_embedscope(&tmp_a, a) != 0) { |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 133 | return (0); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 134 | } |
| 135 | tmp_b = b->sin6_addr; |
| 136 | if (in6_embedscope(&tmp_b, b) != 0) { |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 137 | return (0); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 138 | } |
| 139 | return (IN6_ARE_ADDR_EQUAL(&tmp_a, &tmp_b)); |
| 140 | #endif |
| 141 | #else |
tuexen | 5154a00 | 2011-12-23 18:47:55 +0000 | [diff] [blame] | 142 | return (IN6_ARE_ADDR_EQUAL(&(a->sin6_addr), &(b->sin6_addr))); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 143 | #endif /* SCTP_EMBEDDED_V6_SCOPE */ |
| 144 | } |
| 145 | #endif |
| 146 | |
| 147 | void |
| 148 | sctp_fill_pcbinfo(struct sctp_pcbinfo *spcb) |
| 149 | { |
| 150 | /* |
| 151 | * We really don't need to lock this, but I will just because it |
| 152 | * does not hurt. |
| 153 | */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 154 | SCTP_INP_INFO_RLOCK(); |
| 155 | spcb->ep_count = SCTP_BASE_INFO(ipi_count_ep); |
| 156 | spcb->asoc_count = SCTP_BASE_INFO(ipi_count_asoc); |
| 157 | spcb->laddr_count = SCTP_BASE_INFO(ipi_count_laddr); |
| 158 | spcb->raddr_count = SCTP_BASE_INFO(ipi_count_raddr); |
| 159 | spcb->chk_count = SCTP_BASE_INFO(ipi_count_chunk); |
| 160 | spcb->readq_count = SCTP_BASE_INFO(ipi_count_readq); |
| 161 | spcb->stream_oque = SCTP_BASE_INFO(ipi_count_strmoq); |
| 162 | spcb->free_chunks = SCTP_BASE_INFO(ipi_free_chunks); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 163 | SCTP_INP_INFO_RUNLOCK(); |
| 164 | } |
| 165 | |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 166 | /*- |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 167 | * Addresses are added to VRF's (Virtual Router's). For BSD we |
| 168 | * have only the default VRF 0. We maintain a hash list of |
| 169 | * VRF's. Each VRF has its own list of sctp_ifn's. Each of |
| 170 | * these has a list of addresses. When we add a new address |
| 171 | * to a VRF we lookup the ifn/ifn_index, if the ifn does |
| 172 | * not exist we create it and add it to the list of IFN's |
| 173 | * within the VRF. Once we have the sctp_ifn, we add the |
| 174 | * address to the list. So we look something like: |
| 175 | * |
| 176 | * hash-vrf-table |
| 177 | * vrf-> ifn-> ifn -> ifn |
| 178 | * vrf | |
| 179 | * ... +--ifa-> ifa -> ifa |
| 180 | * vrf |
| 181 | * |
| 182 | * We keep these separate lists since the SCTP subsystem will |
| 183 | * point to these from its source address selection nets structure. |
| 184 | * When an address is deleted it does not happen right away on |
| 185 | * the SCTP side, it gets scheduled. What we do when a |
| 186 | * delete happens is immediately remove the address from |
| 187 | * the master list and decrement the refcount. As our |
| 188 | * addip iterator works through and frees the src address |
| 189 | * selection pointing to the sctp_ifa, eventually the refcount |
| 190 | * will reach 0 and we will delete it. Note that it is assumed |
| 191 | * that any locking on system level ifn/ifa is done at the |
| 192 | * caller of these functions and these routines will only |
| 193 | * lock the SCTP structures as they add or delete things. |
| 194 | * |
| 195 | * Other notes on VRF concepts. |
| 196 | * - An endpoint can be in multiple VRF's |
| 197 | * - An association lives within a VRF and only one VRF. |
| 198 | * - Any incoming packet we can deduce the VRF for by |
| 199 | * looking at the mbuf/pak inbound (for BSD its VRF=0 :D) |
| 200 | * - Any downward send call or connect call must supply the |
| 201 | * VRF via ancillary data or via some sort of set default |
| 202 | * VRF socket option call (again for BSD no brainer since |
| 203 | * the VRF is always 0). |
| 204 | * - An endpoint may add multiple VRF's to it. |
| 205 | * - Listening sockets can accept associations in any |
| 206 | * of the VRF's they are in but the assoc will end up |
| 207 | * in only one VRF (gotten from the packet or connect/send). |
| 208 | * |
| 209 | */ |
| 210 | |
| 211 | struct sctp_vrf * |
| 212 | sctp_allocate_vrf(int vrf_id) |
| 213 | { |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 214 | struct sctp_vrf *vrf = NULL; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 215 | struct sctp_vrflist *bucket; |
| 216 | |
| 217 | /* First allocate the VRF structure */ |
| 218 | vrf = sctp_find_vrf(vrf_id); |
| 219 | if (vrf) { |
| 220 | /* Already allocated */ |
| 221 | return (vrf); |
| 222 | } |
| 223 | SCTP_MALLOC(vrf, struct sctp_vrf *, sizeof(struct sctp_vrf), |
| 224 | SCTP_M_VRF); |
t00fcxen | 8dd95b8 | 2014-07-11 20:41:28 +0000 | [diff] [blame] | 225 | if (vrf == NULL) { |
| 226 | /* No memory */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 227 | #ifdef INVARIANTS |
| 228 | panic("No memory for VRF:%d", vrf_id); |
| 229 | #endif |
| 230 | return (NULL); |
| 231 | } |
| 232 | /* setup the VRF */ |
| 233 | memset(vrf, 0, sizeof(struct sctp_vrf)); |
| 234 | vrf->vrf_id = vrf_id; |
| 235 | LIST_INIT(&vrf->ifnlist); |
| 236 | vrf->total_ifa_count = 0; |
| 237 | vrf->refcount = 0; |
| 238 | /* now also setup table ids */ |
| 239 | SCTP_INIT_VRF_TABLEID(vrf); |
| 240 | /* Init the HASH of addresses */ |
| 241 | vrf->vrf_addr_hash = SCTP_HASH_INIT(SCTP_VRF_ADDR_HASH_SIZE, |
| 242 | &vrf->vrf_addr_hashmark); |
| 243 | if (vrf->vrf_addr_hash == NULL) { |
t00fcxen | 8dd95b8 | 2014-07-11 20:41:28 +0000 | [diff] [blame] | 244 | /* No memory */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 245 | #ifdef INVARIANTS |
| 246 | panic("No memory for VRF:%d", vrf_id); |
| 247 | #endif |
| 248 | SCTP_FREE(vrf, SCTP_M_VRF); |
| 249 | return (NULL); |
| 250 | } |
| 251 | |
| 252 | /* Add it to the hash table */ |
| 253 | bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(vrf_id & SCTP_BASE_INFO(hashvrfmark))]; |
| 254 | LIST_INSERT_HEAD(bucket, vrf, next_vrf); |
| 255 | atomic_add_int(&SCTP_BASE_INFO(ipi_count_vrfs), 1); |
| 256 | return (vrf); |
| 257 | } |
| 258 | |
| 259 | |
| 260 | struct sctp_ifn * |
| 261 | sctp_find_ifn(void *ifn, uint32_t ifn_index) |
| 262 | { |
| 263 | struct sctp_ifn *sctp_ifnp; |
| 264 | struct sctp_ifnlist *hash_ifn_head; |
| 265 | |
| 266 | /* We assume the lock is held for the addresses |
| 267 | * if that's wrong problems could occur :-) |
| 268 | */ |
| 269 | hash_ifn_head = &SCTP_BASE_INFO(vrf_ifn_hash)[(ifn_index & SCTP_BASE_INFO(vrf_ifn_hashmark))]; |
| 270 | LIST_FOREACH(sctp_ifnp, hash_ifn_head, next_bucket) { |
| 271 | if (sctp_ifnp->ifn_index == ifn_index) { |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 272 | return (sctp_ifnp); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 273 | } |
| 274 | if (sctp_ifnp->ifn_p && ifn && (sctp_ifnp->ifn_p == ifn)) { |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 275 | return (sctp_ifnp); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 276 | } |
| 277 | } |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 278 | return (NULL); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 282 | struct sctp_vrf * |
| 283 | sctp_find_vrf(uint32_t vrf_id) |
| 284 | { |
| 285 | struct sctp_vrflist *bucket; |
| 286 | struct sctp_vrf *liste; |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 287 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 288 | bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(vrf_id & SCTP_BASE_INFO(hashvrfmark))]; |
| 289 | LIST_FOREACH(liste, bucket, next_vrf) { |
| 290 | if (vrf_id == liste->vrf_id) { |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 291 | return (liste); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | return (NULL); |
| 295 | } |
| 296 | |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 297 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 298 | void |
| 299 | sctp_free_vrf(struct sctp_vrf *vrf) |
| 300 | { |
| 301 | if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&vrf->refcount)) { |
| 302 | if (vrf->vrf_addr_hash) { |
| 303 | SCTP_HASH_FREE(vrf->vrf_addr_hash, vrf->vrf_addr_hashmark); |
| 304 | vrf->vrf_addr_hash = NULL; |
| 305 | } |
| 306 | /* We zero'd the count */ |
| 307 | LIST_REMOVE(vrf, next_vrf); |
| 308 | SCTP_FREE(vrf, SCTP_M_VRF); |
| 309 | atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_vrfs), 1); |
| 310 | } |
| 311 | } |
| 312 | |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 313 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 314 | void |
| 315 | sctp_free_ifn(struct sctp_ifn *sctp_ifnp) |
| 316 | { |
| 317 | if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&sctp_ifnp->refcount)) { |
| 318 | /* We zero'd the count */ |
| 319 | if (sctp_ifnp->vrf) { |
| 320 | sctp_free_vrf(sctp_ifnp->vrf); |
| 321 | } |
| 322 | SCTP_FREE(sctp_ifnp, SCTP_M_IFN); |
| 323 | atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_ifns), 1); |
| 324 | } |
| 325 | } |
| 326 | |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 327 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 328 | void |
| 329 | sctp_update_ifn_mtu(uint32_t ifn_index, uint32_t mtu) |
| 330 | { |
| 331 | struct sctp_ifn *sctp_ifnp; |
| 332 | |
| 333 | sctp_ifnp = sctp_find_ifn((void *)NULL, ifn_index); |
| 334 | if (sctp_ifnp != NULL) { |
| 335 | sctp_ifnp->ifn_mtu = mtu; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | |
| 340 | void |
| 341 | sctp_free_ifa(struct sctp_ifa *sctp_ifap) |
| 342 | { |
| 343 | if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&sctp_ifap->refcount)) { |
| 344 | /* We zero'd the count */ |
| 345 | if (sctp_ifap->ifn_p) { |
| 346 | sctp_free_ifn(sctp_ifap->ifn_p); |
| 347 | } |
| 348 | SCTP_FREE(sctp_ifap, SCTP_M_IFA); |
| 349 | atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_ifas), 1); |
| 350 | } |
| 351 | } |
| 352 | |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 353 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 354 | static void |
| 355 | sctp_delete_ifn(struct sctp_ifn *sctp_ifnp, int hold_addr_lock) |
| 356 | { |
| 357 | struct sctp_ifn *found; |
| 358 | |
| 359 | found = sctp_find_ifn(sctp_ifnp->ifn_p, sctp_ifnp->ifn_index); |
| 360 | if (found == NULL) { |
| 361 | /* Not in the list.. sorry */ |
| 362 | return; |
| 363 | } |
| 364 | if (hold_addr_lock == 0) |
| 365 | SCTP_IPI_ADDR_WLOCK(); |
| 366 | LIST_REMOVE(sctp_ifnp, next_bucket); |
| 367 | LIST_REMOVE(sctp_ifnp, next_ifn); |
| 368 | SCTP_DEREGISTER_INTERFACE(sctp_ifnp->ifn_index, |
| 369 | sctp_ifnp->registered_af); |
| 370 | if (hold_addr_lock == 0) |
| 371 | SCTP_IPI_ADDR_WUNLOCK(); |
| 372 | /* Take away the reference, and possibly free it */ |
| 373 | sctp_free_ifn(sctp_ifnp); |
| 374 | } |
| 375 | |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 376 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 377 | void |
| 378 | sctp_mark_ifa_addr_down(uint32_t vrf_id, struct sockaddr *addr, |
| 379 | const char *if_name, uint32_t ifn_index) |
| 380 | { |
| 381 | struct sctp_vrf *vrf; |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 382 | struct sctp_ifa *sctp_ifap; |
| 383 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 384 | SCTP_IPI_ADDR_RLOCK(); |
| 385 | vrf = sctp_find_vrf(vrf_id); |
| 386 | if (vrf == NULL) { |
| 387 | SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id); |
| 388 | goto out; |
| 389 | |
| 390 | } |
| 391 | sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED); |
| 392 | if (sctp_ifap == NULL) { |
| 393 | SCTPDBG(SCTP_DEBUG_PCB4, "Can't find sctp_ifap for address\n"); |
| 394 | goto out; |
tuexen | 0e81394 | 2011-11-18 09:18:32 +0000 | [diff] [blame] | 395 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 396 | if (sctp_ifap->ifn_p == NULL) { |
Michael Tuexen | 34488e7 | 2016-05-03 22:11:59 +0200 | [diff] [blame] | 397 | SCTPDBG(SCTP_DEBUG_PCB4, "IFA has no IFN - can't mark unusable\n"); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 398 | goto out; |
| 399 | } |
| 400 | if (if_name) { |
tuexen | 0e81394 | 2011-11-18 09:18:32 +0000 | [diff] [blame] | 401 | if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, SCTP_IFNAMSIZ) != 0) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 402 | 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] | 403 | sctp_ifap->ifn_p->ifn_name, if_name); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 404 | goto out; |
| 405 | } |
| 406 | } else { |
| 407 | if (sctp_ifap->ifn_p->ifn_index != ifn_index) { |
| 408 | SCTPDBG(SCTP_DEBUG_PCB4, "IFA owned by ifn_index:%d down command for ifn_index:%d - ignored\n", |
| 409 | sctp_ifap->ifn_p->ifn_index, ifn_index); |
| 410 | goto out; |
| 411 | } |
| 412 | } |
tuexen | 0e81394 | 2011-11-18 09:18:32 +0000 | [diff] [blame] | 413 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 414 | sctp_ifap->localifa_flags &= (~SCTP_ADDR_VALID); |
| 415 | sctp_ifap->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE; |
| 416 | out: |
| 417 | SCTP_IPI_ADDR_RUNLOCK(); |
| 418 | } |
| 419 | |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 420 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 421 | void |
| 422 | sctp_mark_ifa_addr_up(uint32_t vrf_id, struct sockaddr *addr, |
| 423 | const char *if_name, uint32_t ifn_index) |
| 424 | { |
| 425 | struct sctp_vrf *vrf; |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 426 | struct sctp_ifa *sctp_ifap; |
| 427 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 428 | SCTP_IPI_ADDR_RLOCK(); |
| 429 | vrf = sctp_find_vrf(vrf_id); |
| 430 | if (vrf == NULL) { |
| 431 | SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id); |
| 432 | goto out; |
| 433 | |
| 434 | } |
| 435 | sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED); |
| 436 | if (sctp_ifap == NULL) { |
| 437 | SCTPDBG(SCTP_DEBUG_PCB4, "Can't find sctp_ifap for address\n"); |
| 438 | goto out; |
tuexen | 0e81394 | 2011-11-18 09:18:32 +0000 | [diff] [blame] | 439 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 440 | if (sctp_ifap->ifn_p == NULL) { |
Michael Tuexen | 34488e7 | 2016-05-03 22:11:59 +0200 | [diff] [blame] | 441 | SCTPDBG(SCTP_DEBUG_PCB4, "IFA has no IFN - can't mark unusable\n"); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 442 | goto out; |
| 443 | } |
| 444 | if (if_name) { |
tuexen | 0e81394 | 2011-11-18 09:18:32 +0000 | [diff] [blame] | 445 | if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, SCTP_IFNAMSIZ) != 0) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 446 | 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] | 447 | sctp_ifap->ifn_p->ifn_name, if_name); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 448 | goto out; |
| 449 | } |
| 450 | } else { |
| 451 | if (sctp_ifap->ifn_p->ifn_index != ifn_index) { |
| 452 | SCTPDBG(SCTP_DEBUG_PCB4, "IFA owned by ifn_index:%d down command for ifn_index:%d - ignored\n", |
| 453 | sctp_ifap->ifn_p->ifn_index, ifn_index); |
| 454 | goto out; |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | sctp_ifap->localifa_flags &= (~SCTP_ADDR_IFA_UNUSEABLE); |
| 459 | sctp_ifap->localifa_flags |= SCTP_ADDR_VALID; |
| 460 | out: |
| 461 | SCTP_IPI_ADDR_RUNLOCK(); |
| 462 | } |
| 463 | |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 464 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 465 | /*- |
| 466 | * Add an ifa to an ifn. |
| 467 | * Register the interface as necessary. |
| 468 | * NOTE: ADDR write lock MUST be held. |
| 469 | */ |
| 470 | static void |
| 471 | sctp_add_ifa_to_ifn(struct sctp_ifn *sctp_ifnp, struct sctp_ifa *sctp_ifap) |
| 472 | { |
| 473 | int ifa_af; |
| 474 | |
| 475 | LIST_INSERT_HEAD(&sctp_ifnp->ifalist, sctp_ifap, next_ifa); |
| 476 | sctp_ifap->ifn_p = sctp_ifnp; |
| 477 | atomic_add_int(&sctp_ifap->ifn_p->refcount, 1); |
| 478 | /* update address counts */ |
| 479 | sctp_ifnp->ifa_count++; |
| 480 | ifa_af = sctp_ifap->address.sa.sa_family; |
| 481 | switch (ifa_af) { |
| 482 | #ifdef INET |
| 483 | case AF_INET: |
| 484 | sctp_ifnp->num_v4++; |
| 485 | break; |
| 486 | #endif |
| 487 | #ifdef INET6 |
| 488 | case AF_INET6: |
| 489 | sctp_ifnp->num_v6++; |
| 490 | break; |
| 491 | #endif |
| 492 | default: |
| 493 | break; |
| 494 | } |
| 495 | if (sctp_ifnp->ifa_count == 1) { |
| 496 | /* register the new interface */ |
| 497 | SCTP_REGISTER_INTERFACE(sctp_ifnp->ifn_index, ifa_af); |
| 498 | sctp_ifnp->registered_af = ifa_af; |
| 499 | } |
| 500 | } |
| 501 | |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 502 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 503 | /*- |
| 504 | * Remove an ifa from its ifn. |
| 505 | * If no more addresses exist, remove the ifn too. Otherwise, re-register |
| 506 | * the interface based on the remaining address families left. |
| 507 | * NOTE: ADDR write lock MUST be held. |
| 508 | */ |
| 509 | static void |
| 510 | sctp_remove_ifa_from_ifn(struct sctp_ifa *sctp_ifap) |
| 511 | { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 512 | LIST_REMOVE(sctp_ifap, next_ifa); |
| 513 | if (sctp_ifap->ifn_p) { |
| 514 | /* update address counts */ |
| 515 | sctp_ifap->ifn_p->ifa_count--; |
| 516 | switch (sctp_ifap->address.sa.sa_family) { |
| 517 | #ifdef INET |
| 518 | case AF_INET: |
| 519 | sctp_ifap->ifn_p->num_v4--; |
| 520 | break; |
| 521 | #endif |
| 522 | #ifdef INET6 |
| 523 | case AF_INET6: |
| 524 | sctp_ifap->ifn_p->num_v6--; |
| 525 | break; |
| 526 | #endif |
| 527 | default: |
| 528 | break; |
| 529 | } |
| 530 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 531 | if (LIST_EMPTY(&sctp_ifap->ifn_p->ifalist)) { |
| 532 | /* remove the ifn, possibly freeing it */ |
| 533 | sctp_delete_ifn(sctp_ifap->ifn_p, SCTP_ADDR_LOCKED); |
| 534 | } else { |
| 535 | /* re-register address family type, if needed */ |
| 536 | if ((sctp_ifap->ifn_p->num_v6 == 0) && |
| 537 | (sctp_ifap->ifn_p->registered_af == AF_INET6)) { |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 538 | SCTP_DEREGISTER_INTERFACE(sctp_ifap->ifn_p->ifn_index, AF_INET6); |
| 539 | SCTP_REGISTER_INTERFACE(sctp_ifap->ifn_p->ifn_index, AF_INET); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 540 | sctp_ifap->ifn_p->registered_af = AF_INET; |
| 541 | } else if ((sctp_ifap->ifn_p->num_v4 == 0) && |
| 542 | (sctp_ifap->ifn_p->registered_af == AF_INET)) { |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 543 | SCTP_DEREGISTER_INTERFACE(sctp_ifap->ifn_p->ifn_index, AF_INET); |
| 544 | SCTP_REGISTER_INTERFACE(sctp_ifap->ifn_p->ifn_index, AF_INET6); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 545 | sctp_ifap->ifn_p->registered_af = AF_INET6; |
| 546 | } |
| 547 | /* free the ifn refcount */ |
| 548 | sctp_free_ifn(sctp_ifap->ifn_p); |
| 549 | } |
| 550 | sctp_ifap->ifn_p = NULL; |
| 551 | } |
| 552 | } |
| 553 | |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 554 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 555 | struct sctp_ifa * |
| 556 | sctp_add_addr_to_vrf(uint32_t vrf_id, void *ifn, uint32_t ifn_index, |
| 557 | uint32_t ifn_type, const char *if_name, void *ifa, |
| 558 | struct sockaddr *addr, uint32_t ifa_flags, |
| 559 | int dynamic_add) |
| 560 | { |
| 561 | struct sctp_vrf *vrf; |
| 562 | struct sctp_ifn *sctp_ifnp = NULL; |
| 563 | struct sctp_ifa *sctp_ifap = NULL; |
| 564 | struct sctp_ifalist *hash_addr_head; |
| 565 | struct sctp_ifnlist *hash_ifn_head; |
| 566 | uint32_t hash_of_addr; |
| 567 | int new_ifn_af = 0; |
| 568 | |
| 569 | #ifdef SCTP_DEBUG |
| 570 | SCTPDBG(SCTP_DEBUG_PCB4, "vrf_id 0x%x: adding address: ", vrf_id); |
| 571 | SCTPDBG_ADDR(SCTP_DEBUG_PCB4, addr); |
| 572 | #endif |
| 573 | SCTP_IPI_ADDR_WLOCK(); |
| 574 | sctp_ifnp = sctp_find_ifn(ifn, ifn_index); |
| 575 | if (sctp_ifnp) { |
| 576 | vrf = sctp_ifnp->vrf; |
| 577 | } else { |
| 578 | vrf = sctp_find_vrf(vrf_id); |
| 579 | if (vrf == NULL) { |
| 580 | vrf = sctp_allocate_vrf(vrf_id); |
| 581 | if (vrf == NULL) { |
| 582 | SCTP_IPI_ADDR_WUNLOCK(); |
| 583 | return (NULL); |
| 584 | } |
| 585 | } |
| 586 | } |
| 587 | if (sctp_ifnp == NULL) { |
| 588 | /* build one and add it, can't hold lock |
| 589 | * until after malloc done though. |
| 590 | */ |
| 591 | SCTP_IPI_ADDR_WUNLOCK(); |
| 592 | SCTP_MALLOC(sctp_ifnp, struct sctp_ifn *, |
| 593 | sizeof(struct sctp_ifn), SCTP_M_IFN); |
| 594 | if (sctp_ifnp == NULL) { |
| 595 | #ifdef INVARIANTS |
| 596 | panic("No memory for IFN"); |
| 597 | #endif |
| 598 | return (NULL); |
| 599 | } |
| 600 | memset(sctp_ifnp, 0, sizeof(struct sctp_ifn)); |
| 601 | sctp_ifnp->ifn_index = ifn_index; |
| 602 | sctp_ifnp->ifn_p = ifn; |
| 603 | sctp_ifnp->ifn_type = ifn_type; |
| 604 | sctp_ifnp->refcount = 0; |
| 605 | sctp_ifnp->vrf = vrf; |
| 606 | atomic_add_int(&vrf->refcount, 1); |
| 607 | sctp_ifnp->ifn_mtu = SCTP_GATHER_MTU_FROM_IFN_INFO(ifn, ifn_index, addr->sa_family); |
| 608 | if (if_name != NULL) { |
Michael Tuexen | edd369d | 2020-05-19 09:42:15 +0200 | [diff] [blame] | 609 | SCTP_SNPRINTF(sctp_ifnp->ifn_name, SCTP_IFNAMSIZ, "%s", if_name); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 610 | } else { |
Michael Tuexen | edd369d | 2020-05-19 09:42:15 +0200 | [diff] [blame] | 611 | SCTP_SNPRINTF(sctp_ifnp->ifn_name, SCTP_IFNAMSIZ, "%s", "unknown"); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 612 | } |
| 613 | hash_ifn_head = &SCTP_BASE_INFO(vrf_ifn_hash)[(ifn_index & SCTP_BASE_INFO(vrf_ifn_hashmark))]; |
| 614 | LIST_INIT(&sctp_ifnp->ifalist); |
| 615 | SCTP_IPI_ADDR_WLOCK(); |
| 616 | LIST_INSERT_HEAD(hash_ifn_head, sctp_ifnp, next_bucket); |
| 617 | LIST_INSERT_HEAD(&vrf->ifnlist, sctp_ifnp, next_ifn); |
| 618 | atomic_add_int(&SCTP_BASE_INFO(ipi_count_ifns), 1); |
| 619 | new_ifn_af = 1; |
| 620 | } |
| 621 | sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED); |
| 622 | if (sctp_ifap) { |
| 623 | /* Hmm, it already exists? */ |
| 624 | if ((sctp_ifap->ifn_p) && |
| 625 | (sctp_ifap->ifn_p->ifn_index == ifn_index)) { |
| 626 | SCTPDBG(SCTP_DEBUG_PCB4, "Using existing ifn %s (0x%x) for ifa %p\n", |
| 627 | sctp_ifap->ifn_p->ifn_name, ifn_index, |
t00fcxen | 8fca7ff | 2012-09-05 18:58:42 +0000 | [diff] [blame] | 628 | (void *)sctp_ifap); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 629 | if (new_ifn_af) { |
| 630 | /* Remove the created one that we don't want */ |
| 631 | sctp_delete_ifn(sctp_ifnp, SCTP_ADDR_LOCKED); |
| 632 | } |
| 633 | if (sctp_ifap->localifa_flags & SCTP_BEING_DELETED) { |
| 634 | /* easy to solve, just switch back to active */ |
| 635 | SCTPDBG(SCTP_DEBUG_PCB4, "Clearing deleted ifa flag\n"); |
| 636 | sctp_ifap->localifa_flags = SCTP_ADDR_VALID; |
| 637 | sctp_ifap->ifn_p = sctp_ifnp; |
| 638 | atomic_add_int(&sctp_ifap->ifn_p->refcount, 1); |
| 639 | } |
| 640 | exit_stage_left: |
| 641 | SCTP_IPI_ADDR_WUNLOCK(); |
| 642 | return (sctp_ifap); |
| 643 | } else { |
| 644 | if (sctp_ifap->ifn_p) { |
| 645 | /* |
| 646 | * The last IFN gets the address, remove the |
| 647 | * old one |
| 648 | */ |
| 649 | 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] | 650 | (void *)sctp_ifap, sctp_ifap->ifn_p->ifn_name, |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 651 | sctp_ifap->ifn_p->ifn_index, if_name, |
| 652 | ifn_index); |
| 653 | /* remove the address from the old ifn */ |
| 654 | sctp_remove_ifa_from_ifn(sctp_ifap); |
| 655 | /* move the address over to the new ifn */ |
| 656 | sctp_add_ifa_to_ifn(sctp_ifnp, sctp_ifap); |
t00fcxen | 8dd95b8 | 2014-07-11 20:41:28 +0000 | [diff] [blame] | 657 | goto exit_stage_left; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 658 | } else { |
| 659 | /* repair ifnp which was NULL ? */ |
| 660 | sctp_ifap->localifa_flags = SCTP_ADDR_VALID; |
| 661 | SCTPDBG(SCTP_DEBUG_PCB4, "Repairing ifn %p for ifa %p\n", |
t00fcxen | 8fca7ff | 2012-09-05 18:58:42 +0000 | [diff] [blame] | 662 | (void *)sctp_ifnp, (void *)sctp_ifap); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 663 | sctp_add_ifa_to_ifn(sctp_ifnp, sctp_ifap); |
| 664 | } |
| 665 | goto exit_stage_left; |
| 666 | } |
| 667 | } |
| 668 | SCTP_IPI_ADDR_WUNLOCK(); |
| 669 | SCTP_MALLOC(sctp_ifap, struct sctp_ifa *, sizeof(struct sctp_ifa), SCTP_M_IFA); |
| 670 | if (sctp_ifap == NULL) { |
| 671 | #ifdef INVARIANTS |
| 672 | panic("No memory for IFA"); |
| 673 | #endif |
| 674 | return (NULL); |
| 675 | } |
| 676 | memset(sctp_ifap, 0, sizeof(struct sctp_ifa)); |
| 677 | sctp_ifap->ifn_p = sctp_ifnp; |
| 678 | atomic_add_int(&sctp_ifnp->refcount, 1); |
| 679 | sctp_ifap->vrf_id = vrf_id; |
| 680 | sctp_ifap->ifa = ifa; |
t00fcxen | a8a26f6 | 2012-09-12 19:24:54 +0000 | [diff] [blame] | 681 | #ifdef HAVE_SA_LEN |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 682 | memcpy(&sctp_ifap->address, addr, addr->sa_len); |
| 683 | #else |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 684 | switch (addr->sa_family) { |
| 685 | #ifdef INET |
| 686 | case AF_INET: |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 687 | memcpy(&sctp_ifap->address, addr, sizeof(struct sockaddr_in)); |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 688 | break; |
| 689 | #endif |
| 690 | #ifdef INET6 |
| 691 | case AF_INET6: |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 692 | memcpy(&sctp_ifap->address, addr, sizeof(struct sockaddr_in6)); |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 693 | break; |
| 694 | #endif |
| 695 | #if defined(__Userspace__) |
| 696 | case AF_CONN: |
t00fcxen | 6df8431 | 2013-02-12 08:58:40 +0000 | [diff] [blame] | 697 | memcpy(&sctp_ifap->address, addr, sizeof(struct sockaddr_conn)); |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 698 | break; |
| 699 | #endif |
| 700 | default: |
| 701 | /* TSNH */ |
| 702 | break; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 703 | } |
| 704 | #endif |
| 705 | sctp_ifap->localifa_flags = SCTP_ADDR_VALID | SCTP_ADDR_DEFER_USE; |
| 706 | sctp_ifap->flags = ifa_flags; |
| 707 | /* Set scope */ |
| 708 | switch (sctp_ifap->address.sa.sa_family) { |
| 709 | #ifdef INET |
| 710 | case AF_INET: |
| 711 | { |
| 712 | struct sockaddr_in *sin; |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 713 | |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 714 | sin = &sctp_ifap->address.sin; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 715 | if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) || |
| 716 | (IN4_ISLOOPBACK_ADDRESS(&sin->sin_addr))) { |
| 717 | sctp_ifap->src_is_loop = 1; |
| 718 | } |
| 719 | if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) { |
| 720 | sctp_ifap->src_is_priv = 1; |
| 721 | } |
| 722 | sctp_ifnp->num_v4++; |
| 723 | if (new_ifn_af) |
| 724 | new_ifn_af = AF_INET; |
| 725 | break; |
| 726 | } |
| 727 | #endif |
| 728 | #ifdef INET6 |
| 729 | case AF_INET6: |
| 730 | { |
| 731 | /* ok to use deprecated addresses? */ |
| 732 | struct sockaddr_in6 *sin6; |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 733 | |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 734 | sin6 = &sctp_ifap->address.sin6; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 735 | if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) || |
| 736 | (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))) { |
| 737 | sctp_ifap->src_is_loop = 1; |
| 738 | } |
| 739 | if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { |
| 740 | sctp_ifap->src_is_priv = 1; |
| 741 | } |
| 742 | sctp_ifnp->num_v6++; |
| 743 | if (new_ifn_af) |
| 744 | new_ifn_af = AF_INET6; |
| 745 | break; |
| 746 | } |
| 747 | #endif |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 748 | #if defined(__Userspace__) |
| 749 | case AF_CONN: |
| 750 | if (new_ifn_af) |
| 751 | new_ifn_af = AF_CONN; |
| 752 | break; |
| 753 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 754 | default: |
| 755 | new_ifn_af = 0; |
| 756 | break; |
| 757 | } |
| 758 | hash_of_addr = sctp_get_ifa_hash_val(&sctp_ifap->address.sa); |
| 759 | |
| 760 | if ((sctp_ifap->src_is_priv == 0) && |
| 761 | (sctp_ifap->src_is_loop == 0)) { |
| 762 | sctp_ifap->src_is_glob = 1; |
| 763 | } |
| 764 | SCTP_IPI_ADDR_WLOCK(); |
| 765 | hash_addr_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)]; |
| 766 | LIST_INSERT_HEAD(hash_addr_head, sctp_ifap, next_bucket); |
| 767 | sctp_ifap->refcount = 1; |
| 768 | LIST_INSERT_HEAD(&sctp_ifnp->ifalist, sctp_ifap, next_ifa); |
| 769 | sctp_ifnp->ifa_count++; |
| 770 | vrf->total_ifa_count++; |
| 771 | atomic_add_int(&SCTP_BASE_INFO(ipi_count_ifas), 1); |
| 772 | if (new_ifn_af) { |
| 773 | SCTP_REGISTER_INTERFACE(ifn_index, new_ifn_af); |
| 774 | sctp_ifnp->registered_af = new_ifn_af; |
| 775 | } |
| 776 | SCTP_IPI_ADDR_WUNLOCK(); |
| 777 | if (dynamic_add) { |
| 778 | /* Bump up the refcount so that when the timer |
| 779 | * completes it will drop back down. |
| 780 | */ |
| 781 | struct sctp_laddr *wi; |
| 782 | |
| 783 | atomic_add_int(&sctp_ifap->refcount, 1); |
| 784 | wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr); |
| 785 | if (wi == NULL) { |
| 786 | /* |
| 787 | * Gak, what can we do? We have lost an address |
| 788 | * change can you say HOSED? |
| 789 | */ |
| 790 | SCTPDBG(SCTP_DEBUG_PCB4, "Lost an address change?\n"); |
| 791 | /* Opps, must decrement the count */ |
| 792 | sctp_del_addr_from_vrf(vrf_id, addr, ifn_index, |
| 793 | if_name); |
| 794 | return (NULL); |
| 795 | } |
| 796 | SCTP_INCR_LADDR_COUNT(); |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 797 | memset(wi, 0, sizeof(*wi)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 798 | (void)SCTP_GETTIME_TIMEVAL(&wi->start_time); |
| 799 | wi->ifa = sctp_ifap; |
| 800 | wi->action = SCTP_ADD_IP_ADDRESS; |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 801 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 802 | SCTP_WQ_ADDR_LOCK(); |
| 803 | LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 804 | sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ, |
| 805 | (struct sctp_inpcb *)NULL, |
| 806 | (struct sctp_tcb *)NULL, |
| 807 | (struct sctp_nets *)NULL); |
Michael Tuexen | 96939f1 | 2017-09-20 23:31:24 +0200 | [diff] [blame] | 808 | SCTP_WQ_ADDR_UNLOCK(); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 809 | } else { |
| 810 | /* it's ready for use */ |
| 811 | sctp_ifap->localifa_flags &= ~SCTP_ADDR_DEFER_USE; |
| 812 | } |
| 813 | return (sctp_ifap); |
| 814 | } |
| 815 | |
| 816 | void |
| 817 | sctp_del_addr_from_vrf(uint32_t vrf_id, struct sockaddr *addr, |
| 818 | uint32_t ifn_index, const char *if_name) |
| 819 | { |
| 820 | struct sctp_vrf *vrf; |
| 821 | struct sctp_ifa *sctp_ifap = NULL; |
| 822 | |
| 823 | SCTP_IPI_ADDR_WLOCK(); |
| 824 | vrf = sctp_find_vrf(vrf_id); |
| 825 | if (vrf == NULL) { |
| 826 | SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id); |
| 827 | goto out_now; |
| 828 | } |
| 829 | |
| 830 | #ifdef SCTP_DEBUG |
| 831 | SCTPDBG(SCTP_DEBUG_PCB4, "vrf_id 0x%x: deleting address:", vrf_id); |
| 832 | SCTPDBG_ADDR(SCTP_DEBUG_PCB4, addr); |
| 833 | #endif |
| 834 | sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED); |
| 835 | if (sctp_ifap) { |
| 836 | /* Validate the delete */ |
| 837 | if (sctp_ifap->ifn_p) { |
tuexen | b740ef6 | 2011-11-13 11:58:06 +0000 | [diff] [blame] | 838 | int valid = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 839 | /*- |
| 840 | * The name has priority over the ifn_index |
Michael Tuexen | 3b4263d | 2020-06-06 19:52:50 +0200 | [diff] [blame^] | 841 | * if its given. |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 842 | */ |
| 843 | if (if_name) { |
tuexen | b740ef6 | 2011-11-13 11:58:06 +0000 | [diff] [blame] | 844 | if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, SCTP_IFNAMSIZ) == 0) { |
| 845 | /* They match its a correct delete */ |
| 846 | valid = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 847 | } |
| 848 | } |
| 849 | if (!valid) { |
| 850 | /* last ditch check ifn_index */ |
| 851 | if (ifn_index == sctp_ifap->ifn_p->ifn_index) { |
| 852 | valid = 1; |
| 853 | } |
| 854 | } |
| 855 | if (!valid) { |
| 856 | SCTPDBG(SCTP_DEBUG_PCB4, "ifn:%d ifname:%s does not match addresses\n", |
| 857 | ifn_index, ((if_name == NULL) ? "NULL" : if_name)); |
| 858 | SCTPDBG(SCTP_DEBUG_PCB4, "ifn:%d ifname:%s - ignoring delete\n", |
| 859 | sctp_ifap->ifn_p->ifn_index, sctp_ifap->ifn_p->ifn_name); |
| 860 | SCTP_IPI_ADDR_WUNLOCK(); |
| 861 | return; |
| 862 | } |
| 863 | } |
t00fcxen | 8fca7ff | 2012-09-05 18:58:42 +0000 | [diff] [blame] | 864 | SCTPDBG(SCTP_DEBUG_PCB4, "Deleting ifa %p\n", (void *)sctp_ifap); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 865 | sctp_ifap->localifa_flags &= SCTP_ADDR_VALID; |
t00fcxen | 97887d6 | 2014-03-29 21:31:12 +0000 | [diff] [blame] | 866 | /* |
| 867 | * We don't set the flag. This means that the structure will |
| 868 | * hang around in EP's that have bound specific to it until |
| 869 | * they close. This gives us TCP like behavior if someone |
| 870 | * removes an address (or for that matter adds it right back). |
| 871 | */ |
| 872 | /* sctp_ifap->localifa_flags |= SCTP_BEING_DELETED; */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 873 | vrf->total_ifa_count--; |
| 874 | LIST_REMOVE(sctp_ifap, next_bucket); |
| 875 | sctp_remove_ifa_from_ifn(sctp_ifap); |
| 876 | } |
| 877 | #ifdef SCTP_DEBUG |
| 878 | else { |
| 879 | SCTPDBG(SCTP_DEBUG_PCB4, "Del Addr-ifn:%d Could not find address:", |
| 880 | ifn_index); |
| 881 | SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr); |
| 882 | } |
| 883 | #endif |
| 884 | |
| 885 | out_now: |
| 886 | SCTP_IPI_ADDR_WUNLOCK(); |
| 887 | if (sctp_ifap) { |
| 888 | struct sctp_laddr *wi; |
| 889 | |
| 890 | wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr); |
| 891 | if (wi == NULL) { |
| 892 | /* |
| 893 | * Gak, what can we do? We have lost an address |
| 894 | * change can you say HOSED? |
| 895 | */ |
| 896 | SCTPDBG(SCTP_DEBUG_PCB4, "Lost an address change?\n"); |
| 897 | |
| 898 | /* Oops, must decrement the count */ |
| 899 | sctp_free_ifa(sctp_ifap); |
| 900 | return; |
| 901 | } |
| 902 | SCTP_INCR_LADDR_COUNT(); |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 903 | memset(wi, 0, sizeof(*wi)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 904 | (void)SCTP_GETTIME_TIMEVAL(&wi->start_time); |
| 905 | wi->ifa = sctp_ifap; |
| 906 | wi->action = SCTP_DEL_IP_ADDRESS; |
| 907 | SCTP_WQ_ADDR_LOCK(); |
| 908 | /* |
| 909 | * Should this really be a tailq? As it is we will process the |
| 910 | * newest first :-0 |
| 911 | */ |
| 912 | LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 913 | sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ, |
| 914 | (struct sctp_inpcb *)NULL, |
| 915 | (struct sctp_tcb *)NULL, |
| 916 | (struct sctp_nets *)NULL); |
Michael Tuexen | 96939f1 | 2017-09-20 23:31:24 +0200 | [diff] [blame] | 917 | SCTP_WQ_ADDR_UNLOCK(); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 918 | } |
| 919 | return; |
| 920 | } |
| 921 | |
| 922 | |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 923 | static int |
| 924 | sctp_does_stcb_own_this_addr(struct sctp_tcb *stcb, struct sockaddr *to) |
| 925 | { |
t00fcxen | aff14b6 | 2013-11-02 18:02:02 +0000 | [diff] [blame] | 926 | int loopback_scope; |
| 927 | #if defined(INET) |
| 928 | int ipv4_local_scope, ipv4_addr_legal; |
| 929 | #endif |
| 930 | #if defined(INET6) |
| 931 | int local_scope, site_scope, ipv6_addr_legal; |
| 932 | #endif |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 933 | #if defined(__Userspace__) |
| 934 | int conn_addr_legal; |
| 935 | #endif |
| 936 | struct sctp_vrf *vrf; |
| 937 | struct sctp_ifn *sctp_ifn; |
| 938 | struct sctp_ifa *sctp_ifa; |
| 939 | |
| 940 | loopback_scope = stcb->asoc.scope.loopback_scope; |
t00fcxen | aff14b6 | 2013-11-02 18:02:02 +0000 | [diff] [blame] | 941 | #if defined(INET) |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 942 | ipv4_local_scope = stcb->asoc.scope.ipv4_local_scope; |
t00fcxen | aff14b6 | 2013-11-02 18:02:02 +0000 | [diff] [blame] | 943 | ipv4_addr_legal = stcb->asoc.scope.ipv4_addr_legal; |
| 944 | #endif |
| 945 | #if defined(INET6) |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 946 | local_scope = stcb->asoc.scope.local_scope; |
| 947 | site_scope = stcb->asoc.scope.site_scope; |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 948 | ipv6_addr_legal = stcb->asoc.scope.ipv6_addr_legal; |
t00fcxen | aff14b6 | 2013-11-02 18:02:02 +0000 | [diff] [blame] | 949 | #endif |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 950 | #if defined(__Userspace__) |
| 951 | conn_addr_legal = stcb->asoc.scope.conn_addr_legal; |
| 952 | #endif |
| 953 | |
| 954 | SCTP_IPI_ADDR_RLOCK(); |
| 955 | vrf = sctp_find_vrf(stcb->asoc.vrf_id); |
| 956 | if (vrf == NULL) { |
| 957 | /* no vrf, no addresses */ |
| 958 | SCTP_IPI_ADDR_RUNLOCK(); |
| 959 | return (0); |
| 960 | } |
| 961 | |
| 962 | if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { |
| 963 | LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) { |
| 964 | if ((loopback_scope == 0) && |
| 965 | SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) { |
| 966 | continue; |
| 967 | } |
| 968 | LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) { |
| 969 | if (sctp_is_addr_restricted(stcb, sctp_ifa) && |
| 970 | (!sctp_is_addr_pending(stcb, sctp_ifa))) { |
| 971 | /* We allow pending addresses, where we |
| 972 | * have sent an asconf-add to be considered |
| 973 | * valid. |
| 974 | */ |
| 975 | continue; |
| 976 | } |
t00fcxen | ba8aa20 | 2013-11-16 15:37:17 +0000 | [diff] [blame] | 977 | if (sctp_ifa->address.sa.sa_family != to->sa_family) { |
| 978 | continue; |
| 979 | } |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 980 | switch (sctp_ifa->address.sa.sa_family) { |
| 981 | #ifdef INET |
| 982 | case AF_INET: |
| 983 | if (ipv4_addr_legal) { |
| 984 | struct sockaddr_in *sin, *rsin; |
| 985 | |
| 986 | sin = &sctp_ifa->address.sin; |
| 987 | rsin = (struct sockaddr_in *)to; |
| 988 | if ((ipv4_local_scope == 0) && |
| 989 | IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) { |
| 990 | continue; |
| 991 | } |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 992 | #if defined(__FreeBSD__) |
| 993 | if (prison_check_ip4(stcb->sctp_ep->ip_inp.inp.inp_cred, |
| 994 | &sin->sin_addr) != 0) { |
| 995 | continue; |
| 996 | } |
| 997 | #endif |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 998 | if (sin->sin_addr.s_addr == rsin->sin_addr.s_addr) { |
| 999 | SCTP_IPI_ADDR_RUNLOCK(); |
| 1000 | return (1); |
| 1001 | } |
| 1002 | } |
| 1003 | break; |
| 1004 | #endif |
| 1005 | #ifdef INET6 |
| 1006 | case AF_INET6: |
| 1007 | if (ipv6_addr_legal) { |
| 1008 | struct sockaddr_in6 *sin6, *rsin6; |
| 1009 | #if defined(SCTP_EMBEDDED_V6_SCOPE) && !defined(SCTP_KAME) |
| 1010 | struct sockaddr_in6 lsa6; |
| 1011 | #endif |
| 1012 | sin6 = &sctp_ifa->address.sin6; |
| 1013 | rsin6 = (struct sockaddr_in6 *)to; |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 1014 | #if defined(__FreeBSD__) |
| 1015 | if (prison_check_ip6(stcb->sctp_ep->ip_inp.inp.inp_cred, |
| 1016 | &sin6->sin6_addr) != 0) { |
| 1017 | continue; |
| 1018 | } |
| 1019 | #endif |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 1020 | if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { |
| 1021 | if (local_scope == 0) |
| 1022 | continue; |
| 1023 | #if defined(SCTP_EMBEDDED_V6_SCOPE) |
| 1024 | if (sin6->sin6_scope_id == 0) { |
| 1025 | #ifdef SCTP_KAME |
| 1026 | if (sa6_recoverscope(sin6) != 0) |
| 1027 | continue; |
| 1028 | #else |
| 1029 | lsa6 = *sin6; |
| 1030 | if (in6_recoverscope(&lsa6, |
| 1031 | &lsa6.sin6_addr, |
| 1032 | NULL)) |
| 1033 | continue; |
| 1034 | sin6 = &lsa6; |
| 1035 | #endif /* SCTP_KAME */ |
| 1036 | } |
| 1037 | #endif /* SCTP_EMBEDDED_V6_SCOPE */ |
| 1038 | } |
| 1039 | if ((site_scope == 0) && |
| 1040 | (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) { |
| 1041 | continue; |
| 1042 | } |
| 1043 | if (SCTP6_ARE_ADDR_EQUAL(sin6, rsin6)) { |
| 1044 | SCTP_IPI_ADDR_RUNLOCK(); |
| 1045 | return (1); |
| 1046 | } |
| 1047 | } |
| 1048 | break; |
| 1049 | #endif |
| 1050 | #if defined(__Userspace__) |
| 1051 | case AF_CONN: |
| 1052 | if (conn_addr_legal) { |
| 1053 | struct sockaddr_conn *sconn, *rsconn; |
| 1054 | |
| 1055 | sconn = &sctp_ifa->address.sconn; |
| 1056 | rsconn = (struct sockaddr_conn *)to; |
| 1057 | if (sconn->sconn_addr == rsconn->sconn_addr) { |
| 1058 | SCTP_IPI_ADDR_RUNLOCK(); |
| 1059 | return (1); |
| 1060 | } |
| 1061 | } |
| 1062 | break; |
| 1063 | #endif |
| 1064 | default: |
| 1065 | /* TSNH */ |
| 1066 | break; |
| 1067 | } |
| 1068 | } |
| 1069 | } |
| 1070 | } else { |
| 1071 | struct sctp_laddr *laddr; |
| 1072 | |
| 1073 | LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) { |
| 1074 | if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) { |
| 1075 | SCTPDBG(SCTP_DEBUG_PCB1, "ifa being deleted\n"); |
| 1076 | continue; |
| 1077 | } |
| 1078 | if (sctp_is_addr_restricted(stcb, laddr->ifa) && |
| 1079 | (!sctp_is_addr_pending(stcb, laddr->ifa))) { |
| 1080 | /* We allow pending addresses, where we |
| 1081 | * have sent an asconf-add to be considered |
| 1082 | * valid. |
| 1083 | */ |
| 1084 | continue; |
| 1085 | } |
| 1086 | if (laddr->ifa->address.sa.sa_family != to->sa_family) { |
| 1087 | continue; |
| 1088 | } |
| 1089 | switch (to->sa_family) { |
| 1090 | #ifdef INET |
| 1091 | case AF_INET: |
| 1092 | { |
| 1093 | struct sockaddr_in *sin, *rsin; |
| 1094 | |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 1095 | sin = &laddr->ifa->address.sin; |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 1096 | rsin = (struct sockaddr_in *)to; |
| 1097 | if (sin->sin_addr.s_addr == rsin->sin_addr.s_addr) { |
| 1098 | SCTP_IPI_ADDR_RUNLOCK(); |
| 1099 | return (1); |
| 1100 | } |
| 1101 | break; |
| 1102 | } |
| 1103 | #endif |
| 1104 | #ifdef INET6 |
| 1105 | case AF_INET6: |
| 1106 | { |
| 1107 | struct sockaddr_in6 *sin6, *rsin6; |
| 1108 | |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 1109 | sin6 = &laddr->ifa->address.sin6; |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 1110 | rsin6 = (struct sockaddr_in6 *)to; |
| 1111 | if (SCTP6_ARE_ADDR_EQUAL(sin6, rsin6)) { |
| 1112 | SCTP_IPI_ADDR_RUNLOCK(); |
| 1113 | return (1); |
| 1114 | } |
| 1115 | break; |
| 1116 | } |
| 1117 | |
| 1118 | #endif |
| 1119 | #if defined(__Userspace__) |
| 1120 | case AF_CONN: |
| 1121 | { |
| 1122 | struct sockaddr_conn *sconn, *rsconn; |
| 1123 | |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 1124 | sconn = &laddr->ifa->address.sconn; |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 1125 | rsconn = (struct sockaddr_conn *)to; |
| 1126 | if (sconn->sconn_addr == rsconn->sconn_addr) { |
| 1127 | SCTP_IPI_ADDR_RUNLOCK(); |
| 1128 | return (1); |
| 1129 | } |
| 1130 | break; |
| 1131 | } |
| 1132 | #endif |
| 1133 | default: |
| 1134 | /* TSNH */ |
| 1135 | break; |
| 1136 | } |
| 1137 | |
| 1138 | } |
| 1139 | } |
| 1140 | SCTP_IPI_ADDR_RUNLOCK(); |
| 1141 | return (0); |
| 1142 | } |
| 1143 | |
| 1144 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1145 | static struct sctp_tcb * |
| 1146 | sctp_tcb_special_locate(struct sctp_inpcb **inp_p, struct sockaddr *from, |
| 1147 | struct sockaddr *to, struct sctp_nets **netp, uint32_t vrf_id) |
| 1148 | { |
| 1149 | /**** ASSUMES THE CALLER holds the INP_INFO_RLOCK */ |
| 1150 | /* |
| 1151 | * If we support the TCP model, then we must now dig through to see |
| 1152 | * if we can find our endpoint in the list of tcp ep's. |
| 1153 | */ |
| 1154 | uint16_t lport, rport; |
| 1155 | struct sctppcbhead *ephead; |
| 1156 | struct sctp_inpcb *inp; |
| 1157 | struct sctp_laddr *laddr; |
| 1158 | struct sctp_tcb *stcb; |
| 1159 | struct sctp_nets *net; |
| 1160 | #ifdef SCTP_MVRF |
| 1161 | int fnd, i; |
| 1162 | #endif |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 1163 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1164 | if ((to == NULL) || (from == NULL)) { |
| 1165 | return (NULL); |
| 1166 | } |
| 1167 | |
| 1168 | switch (to->sa_family) { |
| 1169 | #ifdef INET |
| 1170 | case AF_INET: |
| 1171 | if (from->sa_family == AF_INET) { |
| 1172 | lport = ((struct sockaddr_in *)to)->sin_port; |
| 1173 | rport = ((struct sockaddr_in *)from)->sin_port; |
| 1174 | } else { |
| 1175 | return (NULL); |
| 1176 | } |
| 1177 | break; |
| 1178 | #endif |
| 1179 | #ifdef INET6 |
| 1180 | case AF_INET6: |
| 1181 | if (from->sa_family == AF_INET6) { |
| 1182 | lport = ((struct sockaddr_in6 *)to)->sin6_port; |
| 1183 | rport = ((struct sockaddr_in6 *)from)->sin6_port; |
| 1184 | } else { |
| 1185 | return (NULL); |
| 1186 | } |
| 1187 | break; |
| 1188 | #endif |
tuexen | 5d9b974 | 2012-07-11 21:52:01 +0000 | [diff] [blame] | 1189 | #if defined(__Userspace__) |
| 1190 | case AF_CONN: |
| 1191 | if (from->sa_family == AF_CONN) { |
| 1192 | lport = ((struct sockaddr_conn *)to)->sconn_port; |
| 1193 | rport = ((struct sockaddr_conn *)from)->sconn_port; |
| 1194 | } else { |
| 1195 | return (NULL); |
| 1196 | } |
| 1197 | break; |
| 1198 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1199 | default: |
| 1200 | return (NULL); |
| 1201 | } |
| 1202 | ephead = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR((lport | rport), SCTP_BASE_INFO(hashtcpmark))]; |
| 1203 | /* |
| 1204 | * Ok now for each of the guys in this bucket we must look and see: |
| 1205 | * - Does the remote port match. - Does there single association's |
| 1206 | * addresses match this address (to). If so we update p_ep to point |
| 1207 | * to this ep and return the tcb from it. |
| 1208 | */ |
| 1209 | LIST_FOREACH(inp, ephead, sctp_hash) { |
| 1210 | SCTP_INP_RLOCK(inp); |
| 1211 | if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
| 1212 | SCTP_INP_RUNLOCK(inp); |
| 1213 | continue; |
| 1214 | } |
| 1215 | if (lport != inp->sctp_lport) { |
| 1216 | SCTP_INP_RUNLOCK(inp); |
| 1217 | continue; |
| 1218 | } |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 1219 | #if defined(__FreeBSD__) |
| 1220 | switch (to->sa_family) { |
| 1221 | #ifdef INET |
| 1222 | case AF_INET: |
| 1223 | { |
| 1224 | struct sockaddr_in *sin; |
| 1225 | |
| 1226 | sin = (struct sockaddr_in *)to; |
| 1227 | if (prison_check_ip4(inp->ip_inp.inp.inp_cred, |
| 1228 | &sin->sin_addr) != 0) { |
| 1229 | SCTP_INP_RUNLOCK(inp); |
| 1230 | continue; |
| 1231 | } |
| 1232 | break; |
| 1233 | } |
| 1234 | #endif |
| 1235 | #ifdef INET6 |
| 1236 | case AF_INET6: |
| 1237 | { |
| 1238 | struct sockaddr_in6 *sin6; |
| 1239 | |
| 1240 | sin6 = (struct sockaddr_in6 *)to; |
| 1241 | if (prison_check_ip6(inp->ip_inp.inp.inp_cred, |
| 1242 | &sin6->sin6_addr) != 0) { |
| 1243 | SCTP_INP_RUNLOCK(inp); |
| 1244 | continue; |
| 1245 | } |
| 1246 | break; |
| 1247 | } |
| 1248 | #endif |
| 1249 | default: |
| 1250 | SCTP_INP_RUNLOCK(inp); |
| 1251 | continue; |
| 1252 | } |
| 1253 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1254 | #ifdef SCTP_MVRF |
| 1255 | fnd = 0; |
tuexen | 9784e9a | 2011-12-18 13:04:23 +0000 | [diff] [blame] | 1256 | for (i = 0; i < inp->num_vrfs; i++) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1257 | if (inp->m_vrf_ids[i] == vrf_id) { |
| 1258 | fnd = 1; |
| 1259 | break; |
| 1260 | } |
| 1261 | } |
| 1262 | if (fnd == 0) { |
| 1263 | SCTP_INP_RUNLOCK(inp); |
| 1264 | continue; |
| 1265 | } |
| 1266 | #else |
| 1267 | if (inp->def_vrf_id != vrf_id) { |
| 1268 | SCTP_INP_RUNLOCK(inp); |
| 1269 | continue; |
| 1270 | } |
| 1271 | #endif |
| 1272 | /* check to see if the ep has one of the addresses */ |
| 1273 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) { |
| 1274 | /* We are NOT bound all, so look further */ |
| 1275 | int match = 0; |
| 1276 | |
| 1277 | LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { |
| 1278 | |
| 1279 | if (laddr->ifa == NULL) { |
Saúl Ibarra Corretgé | 8b04b41 | 2015-09-26 09:53:06 +0200 | [diff] [blame] | 1280 | SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n", __func__); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1281 | continue; |
| 1282 | } |
| 1283 | if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) { |
| 1284 | SCTPDBG(SCTP_DEBUG_PCB1, "ifa being deleted\n"); |
| 1285 | continue; |
| 1286 | } |
| 1287 | if (laddr->ifa->address.sa.sa_family == |
| 1288 | to->sa_family) { |
| 1289 | /* see if it matches */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1290 | #ifdef INET |
| 1291 | if (from->sa_family == AF_INET) { |
| 1292 | struct sockaddr_in *intf_addr, *sin; |
| 1293 | |
| 1294 | intf_addr = &laddr->ifa->address.sin; |
| 1295 | sin = (struct sockaddr_in *)to; |
| 1296 | if (sin->sin_addr.s_addr == |
| 1297 | intf_addr->sin_addr.s_addr) { |
| 1298 | match = 1; |
| 1299 | break; |
| 1300 | } |
| 1301 | } |
| 1302 | #endif |
| 1303 | #ifdef INET6 |
| 1304 | if (from->sa_family == AF_INET6) { |
| 1305 | struct sockaddr_in6 *intf_addr6; |
| 1306 | struct sockaddr_in6 *sin6; |
| 1307 | |
| 1308 | sin6 = (struct sockaddr_in6 *) |
| 1309 | to; |
| 1310 | intf_addr6 = &laddr->ifa->address.sin6; |
| 1311 | |
| 1312 | if (SCTP6_ARE_ADDR_EQUAL(sin6, |
| 1313 | intf_addr6)) { |
| 1314 | match = 1; |
| 1315 | break; |
| 1316 | } |
| 1317 | } |
| 1318 | #endif |
tuexen | 5d9b974 | 2012-07-11 21:52:01 +0000 | [diff] [blame] | 1319 | #if defined(__Userspace__) |
| 1320 | if (from->sa_family == AF_CONN) { |
| 1321 | struct sockaddr_conn *intf_addr, *sconn; |
| 1322 | |
| 1323 | intf_addr = &laddr->ifa->address.sconn; |
| 1324 | sconn = (struct sockaddr_conn *)to; |
| 1325 | if (sconn->sconn_addr == |
| 1326 | intf_addr->sconn_addr) { |
| 1327 | match = 1; |
| 1328 | break; |
| 1329 | } |
| 1330 | } |
| 1331 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1332 | } |
| 1333 | } |
| 1334 | if (match == 0) { |
| 1335 | /* This endpoint does not have this address */ |
| 1336 | SCTP_INP_RUNLOCK(inp); |
| 1337 | continue; |
| 1338 | } |
| 1339 | } |
| 1340 | /* |
| 1341 | * Ok if we hit here the ep has the address, does it hold |
| 1342 | * the tcb? |
| 1343 | */ |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 1344 | /* XXX: Why don't we TAILQ_FOREACH through sctp_asoc_list? */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1345 | stcb = LIST_FIRST(&inp->sctp_asoc_list); |
| 1346 | if (stcb == NULL) { |
| 1347 | SCTP_INP_RUNLOCK(inp); |
| 1348 | continue; |
| 1349 | } |
| 1350 | SCTP_TCB_LOCK(stcb); |
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 | if (stcb->rport != rport) { |
| 1357 | /* remote port does not match. */ |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 1358 | SCTP_TCB_UNLOCK(stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1359 | SCTP_INP_RUNLOCK(inp); |
| 1360 | continue; |
| 1361 | } |
| 1362 | if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 1363 | SCTP_TCB_UNLOCK(stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1364 | SCTP_INP_RUNLOCK(inp); |
| 1365 | continue; |
| 1366 | } |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 1367 | if (!sctp_does_stcb_own_this_addr(stcb, to)) { |
| 1368 | SCTP_TCB_UNLOCK(stcb); |
| 1369 | SCTP_INP_RUNLOCK(inp); |
| 1370 | continue; |
| 1371 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1372 | /* Does this TCB have a matching address? */ |
| 1373 | TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { |
| 1374 | |
| 1375 | if (net->ro._l_addr.sa.sa_family != from->sa_family) { |
| 1376 | /* not the same family, can't be a match */ |
| 1377 | continue; |
| 1378 | } |
| 1379 | switch (from->sa_family) { |
| 1380 | #ifdef INET |
| 1381 | case AF_INET: |
| 1382 | { |
| 1383 | struct sockaddr_in *sin, *rsin; |
| 1384 | |
| 1385 | sin = (struct sockaddr_in *)&net->ro._l_addr; |
| 1386 | rsin = (struct sockaddr_in *)from; |
| 1387 | if (sin->sin_addr.s_addr == |
| 1388 | rsin->sin_addr.s_addr) { |
| 1389 | /* found it */ |
| 1390 | if (netp != NULL) { |
| 1391 | *netp = net; |
| 1392 | } |
| 1393 | /* Update the endpoint pointer */ |
| 1394 | *inp_p = inp; |
| 1395 | SCTP_INP_RUNLOCK(inp); |
| 1396 | return (stcb); |
| 1397 | } |
| 1398 | break; |
| 1399 | } |
| 1400 | #endif |
| 1401 | #ifdef INET6 |
| 1402 | case AF_INET6: |
| 1403 | { |
| 1404 | struct sockaddr_in6 *sin6, *rsin6; |
| 1405 | |
| 1406 | sin6 = (struct sockaddr_in6 *)&net->ro._l_addr; |
| 1407 | rsin6 = (struct sockaddr_in6 *)from; |
| 1408 | if (SCTP6_ARE_ADDR_EQUAL(sin6, |
| 1409 | rsin6)) { |
| 1410 | /* found it */ |
| 1411 | if (netp != NULL) { |
| 1412 | *netp = net; |
| 1413 | } |
| 1414 | /* Update the endpoint pointer */ |
| 1415 | *inp_p = inp; |
| 1416 | SCTP_INP_RUNLOCK(inp); |
| 1417 | return (stcb); |
| 1418 | } |
| 1419 | break; |
| 1420 | } |
| 1421 | #endif |
tuexen | 5d9b974 | 2012-07-11 21:52:01 +0000 | [diff] [blame] | 1422 | #if defined(__Userspace__) |
| 1423 | case AF_CONN: |
| 1424 | { |
| 1425 | struct sockaddr_conn *sconn, *rsconn; |
| 1426 | |
| 1427 | sconn = (struct sockaddr_conn *)&net->ro._l_addr; |
| 1428 | rsconn = (struct sockaddr_conn *)from; |
| 1429 | if (sconn->sconn_addr == rsconn->sconn_addr) { |
| 1430 | /* found it */ |
| 1431 | if (netp != NULL) { |
| 1432 | *netp = net; |
| 1433 | } |
| 1434 | /* Update the endpoint pointer */ |
| 1435 | *inp_p = inp; |
| 1436 | SCTP_INP_RUNLOCK(inp); |
| 1437 | return (stcb); |
| 1438 | } |
| 1439 | break; |
| 1440 | } |
| 1441 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1442 | default: |
| 1443 | /* TSNH */ |
| 1444 | break; |
| 1445 | } |
| 1446 | } |
| 1447 | SCTP_TCB_UNLOCK(stcb); |
| 1448 | SCTP_INP_RUNLOCK(inp); |
| 1449 | } |
| 1450 | return (NULL); |
| 1451 | } |
| 1452 | |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 1453 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1454 | /* |
| 1455 | * rules for use |
| 1456 | * |
| 1457 | * 1) If I return a NULL you must decrement any INP ref cnt. 2) If I find an |
| 1458 | * stcb, both will be locked (locked_tcb and stcb) but decrement will be done |
| 1459 | * (if locked == NULL). 3) Decrement happens on return ONLY if locked == |
| 1460 | * NULL. |
| 1461 | */ |
| 1462 | |
| 1463 | struct sctp_tcb * |
| 1464 | sctp_findassociation_ep_addr(struct sctp_inpcb **inp_p, struct sockaddr *remote, |
| 1465 | struct sctp_nets **netp, struct sockaddr *local, struct sctp_tcb *locked_tcb) |
| 1466 | { |
| 1467 | struct sctpasochead *head; |
| 1468 | struct sctp_inpcb *inp; |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 1469 | struct sctp_tcb *stcb = NULL; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1470 | struct sctp_nets *net; |
| 1471 | uint16_t rport; |
| 1472 | |
| 1473 | inp = *inp_p; |
tuexen | 5d9b974 | 2012-07-11 21:52:01 +0000 | [diff] [blame] | 1474 | switch (remote->sa_family) { |
| 1475 | #ifdef INET |
| 1476 | case AF_INET: |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1477 | rport = (((struct sockaddr_in *)remote)->sin_port); |
tuexen | 5d9b974 | 2012-07-11 21:52:01 +0000 | [diff] [blame] | 1478 | break; |
| 1479 | #endif |
| 1480 | #ifdef INET6 |
| 1481 | case AF_INET6: |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1482 | rport = (((struct sockaddr_in6 *)remote)->sin6_port); |
tuexen | 5d9b974 | 2012-07-11 21:52:01 +0000 | [diff] [blame] | 1483 | break; |
| 1484 | #endif |
| 1485 | #if defined(__Userspace__) |
| 1486 | case AF_CONN: |
Taylor Brandstetter | 7467051 | 2018-03-27 10:56:58 -0700 | [diff] [blame] | 1487 | rport = (((struct sockaddr_conn *)remote)->sconn_port); |
tuexen | 5d9b974 | 2012-07-11 21:52:01 +0000 | [diff] [blame] | 1488 | break; |
| 1489 | #endif |
| 1490 | default: |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1491 | return (NULL); |
| 1492 | } |
| 1493 | if (locked_tcb) { |
| 1494 | /* |
| 1495 | * UN-lock so we can do proper locking here this occurs when |
| 1496 | * called from load_addresses_from_init. |
| 1497 | */ |
| 1498 | atomic_add_int(&locked_tcb->asoc.refcnt, 1); |
| 1499 | SCTP_TCB_UNLOCK(locked_tcb); |
| 1500 | } |
| 1501 | SCTP_INP_INFO_RLOCK(); |
t00fcxen | f2d781c | 2012-11-26 16:47:12 +0000 | [diff] [blame] | 1502 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || |
| 1503 | (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1504 | /*- |
| 1505 | * Now either this guy is our listener or it's the |
| 1506 | * connector. If it is the one that issued the connect, then |
| 1507 | * it's only chance is to be the first TCB in the list. If |
| 1508 | * it is the acceptor, then do the special_lookup to hash |
| 1509 | * and find the real inp. |
| 1510 | */ |
Michael Tuexen | d900e5f | 2017-06-23 10:52:43 +0200 | [diff] [blame] | 1511 | if ((inp->sctp_socket) && SCTP_IS_LISTENING(inp)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1512 | /* to is peer addr, from is my addr */ |
| 1513 | #ifndef SCTP_MVRF |
| 1514 | stcb = sctp_tcb_special_locate(inp_p, remote, local, |
| 1515 | netp, inp->def_vrf_id); |
| 1516 | if ((stcb != NULL) && (locked_tcb == NULL)) { |
| 1517 | /* we have a locked tcb, lower refcount */ |
| 1518 | SCTP_INP_DECR_REF(inp); |
| 1519 | } |
| 1520 | if ((locked_tcb != NULL) && (locked_tcb != stcb)) { |
| 1521 | SCTP_INP_RLOCK(locked_tcb->sctp_ep); |
| 1522 | SCTP_TCB_LOCK(locked_tcb); |
| 1523 | atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); |
| 1524 | SCTP_INP_RUNLOCK(locked_tcb->sctp_ep); |
| 1525 | } |
| 1526 | #else |
| 1527 | /*- |
| 1528 | * MVRF is tricky, we must look in every VRF |
| 1529 | * the endpoint has. |
| 1530 | */ |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 1531 | int i; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1532 | |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 1533 | for (i = 0; i < inp->num_vrfs; i++) { |
| 1534 | stcb = sctp_tcb_special_locate(inp_p, remote, local, |
| 1535 | netp, inp->m_vrf_ids[i]); |
| 1536 | if ((stcb != NULL) && (locked_tcb == NULL)) { |
| 1537 | /* we have a locked tcb, lower refcount */ |
| 1538 | SCTP_INP_DECR_REF(inp); |
| 1539 | break; |
| 1540 | } |
| 1541 | if ((locked_tcb != NULL) && (locked_tcb != stcb)) { |
| 1542 | SCTP_INP_RLOCK(locked_tcb->sctp_ep); |
| 1543 | SCTP_TCB_LOCK(locked_tcb); |
| 1544 | atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); |
| 1545 | SCTP_INP_RUNLOCK(locked_tcb->sctp_ep); |
| 1546 | break; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1547 | } |
| 1548 | } |
| 1549 | #endif |
| 1550 | SCTP_INP_INFO_RUNLOCK(); |
| 1551 | return (stcb); |
| 1552 | } else { |
| 1553 | SCTP_INP_WLOCK(inp); |
| 1554 | if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
| 1555 | goto null_return; |
| 1556 | } |
| 1557 | stcb = LIST_FIRST(&inp->sctp_asoc_list); |
| 1558 | if (stcb == NULL) { |
| 1559 | goto null_return; |
| 1560 | } |
| 1561 | SCTP_TCB_LOCK(stcb); |
| 1562 | |
| 1563 | if (stcb->rport != rport) { |
| 1564 | /* remote port does not match. */ |
| 1565 | SCTP_TCB_UNLOCK(stcb); |
| 1566 | goto null_return; |
| 1567 | } |
| 1568 | if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 1569 | SCTP_TCB_UNLOCK(stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1570 | goto null_return; |
| 1571 | } |
| 1572 | if (local && !sctp_does_stcb_own_this_addr(stcb, local)) { |
| 1573 | SCTP_TCB_UNLOCK(stcb); |
| 1574 | goto null_return; |
| 1575 | } |
| 1576 | /* now look at the list of remote addresses */ |
| 1577 | TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { |
| 1578 | #ifdef INVARIANTS |
| 1579 | if (net == (TAILQ_NEXT(net, sctp_next))) { |
| 1580 | panic("Corrupt net list"); |
| 1581 | } |
| 1582 | #endif |
| 1583 | if (net->ro._l_addr.sa.sa_family != |
| 1584 | remote->sa_family) { |
| 1585 | /* not the same family */ |
| 1586 | continue; |
| 1587 | } |
| 1588 | switch (remote->sa_family) { |
| 1589 | #ifdef INET |
| 1590 | case AF_INET: |
| 1591 | { |
| 1592 | struct sockaddr_in *sin, *rsin; |
| 1593 | |
| 1594 | sin = (struct sockaddr_in *) |
| 1595 | &net->ro._l_addr; |
| 1596 | rsin = (struct sockaddr_in *)remote; |
| 1597 | if (sin->sin_addr.s_addr == |
| 1598 | rsin->sin_addr.s_addr) { |
| 1599 | /* found it */ |
| 1600 | if (netp != NULL) { |
| 1601 | *netp = net; |
| 1602 | } |
| 1603 | if (locked_tcb == NULL) { |
| 1604 | SCTP_INP_DECR_REF(inp); |
| 1605 | } else if (locked_tcb != stcb) { |
| 1606 | SCTP_TCB_LOCK(locked_tcb); |
| 1607 | } |
| 1608 | if (locked_tcb) { |
| 1609 | atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); |
| 1610 | } |
| 1611 | |
| 1612 | SCTP_INP_WUNLOCK(inp); |
| 1613 | SCTP_INP_INFO_RUNLOCK(); |
| 1614 | return (stcb); |
| 1615 | } |
| 1616 | break; |
| 1617 | } |
| 1618 | #endif |
| 1619 | #ifdef INET6 |
| 1620 | case AF_INET6: |
| 1621 | { |
| 1622 | struct sockaddr_in6 *sin6, *rsin6; |
| 1623 | |
| 1624 | sin6 = (struct sockaddr_in6 *)&net->ro._l_addr; |
| 1625 | rsin6 = (struct sockaddr_in6 *)remote; |
| 1626 | if (SCTP6_ARE_ADDR_EQUAL(sin6, |
| 1627 | rsin6)) { |
| 1628 | /* found it */ |
| 1629 | if (netp != NULL) { |
| 1630 | *netp = net; |
| 1631 | } |
| 1632 | if (locked_tcb == NULL) { |
| 1633 | SCTP_INP_DECR_REF(inp); |
| 1634 | } else if (locked_tcb != stcb) { |
| 1635 | SCTP_TCB_LOCK(locked_tcb); |
| 1636 | } |
| 1637 | if (locked_tcb) { |
| 1638 | atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); |
| 1639 | } |
| 1640 | SCTP_INP_WUNLOCK(inp); |
| 1641 | SCTP_INP_INFO_RUNLOCK(); |
| 1642 | return (stcb); |
| 1643 | } |
| 1644 | break; |
| 1645 | } |
| 1646 | #endif |
tuexen | 5d9b974 | 2012-07-11 21:52:01 +0000 | [diff] [blame] | 1647 | #if defined(__Userspace__) |
| 1648 | case AF_CONN: |
| 1649 | { |
| 1650 | struct sockaddr_conn *sconn, *rsconn; |
| 1651 | |
| 1652 | sconn = (struct sockaddr_conn *)&net->ro._l_addr; |
| 1653 | rsconn = (struct sockaddr_conn *)remote; |
| 1654 | if (sconn->sconn_addr == rsconn->sconn_addr) { |
| 1655 | /* found it */ |
| 1656 | if (netp != NULL) { |
| 1657 | *netp = net; |
| 1658 | } |
| 1659 | if (locked_tcb == NULL) { |
| 1660 | SCTP_INP_DECR_REF(inp); |
| 1661 | } else if (locked_tcb != stcb) { |
| 1662 | SCTP_TCB_LOCK(locked_tcb); |
| 1663 | } |
| 1664 | if (locked_tcb) { |
| 1665 | atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); |
| 1666 | } |
| 1667 | SCTP_INP_WUNLOCK(inp); |
| 1668 | SCTP_INP_INFO_RUNLOCK(); |
| 1669 | return (stcb); |
| 1670 | } |
| 1671 | break; |
| 1672 | } |
| 1673 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1674 | default: |
| 1675 | /* TSNH */ |
| 1676 | break; |
| 1677 | } |
| 1678 | } |
| 1679 | SCTP_TCB_UNLOCK(stcb); |
| 1680 | } |
| 1681 | } else { |
| 1682 | SCTP_INP_WLOCK(inp); |
| 1683 | if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
| 1684 | goto null_return; |
| 1685 | } |
| 1686 | head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(rport, |
t00fcxen | 1369cd4 | 2015-01-11 21:57:30 +0000 | [diff] [blame] | 1687 | inp->sctp_hashmark)]; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1688 | LIST_FOREACH(stcb, head, sctp_tcbhash) { |
| 1689 | if (stcb->rport != rport) { |
| 1690 | /* remote port does not match */ |
| 1691 | continue; |
| 1692 | } |
| 1693 | SCTP_TCB_LOCK(stcb); |
| 1694 | if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { |
| 1695 | SCTP_TCB_UNLOCK(stcb); |
| 1696 | continue; |
| 1697 | } |
| 1698 | if (local && !sctp_does_stcb_own_this_addr(stcb, local)) { |
| 1699 | SCTP_TCB_UNLOCK(stcb); |
| 1700 | continue; |
| 1701 | } |
| 1702 | /* now look at the list of remote addresses */ |
| 1703 | TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { |
| 1704 | #ifdef INVARIANTS |
| 1705 | if (net == (TAILQ_NEXT(net, sctp_next))) { |
| 1706 | panic("Corrupt net list"); |
| 1707 | } |
| 1708 | #endif |
| 1709 | if (net->ro._l_addr.sa.sa_family != |
| 1710 | remote->sa_family) { |
| 1711 | /* not the same family */ |
| 1712 | continue; |
| 1713 | } |
| 1714 | switch (remote->sa_family) { |
| 1715 | #ifdef INET |
| 1716 | case AF_INET: |
| 1717 | { |
| 1718 | struct sockaddr_in *sin, *rsin; |
| 1719 | |
| 1720 | sin = (struct sockaddr_in *) |
| 1721 | &net->ro._l_addr; |
| 1722 | rsin = (struct sockaddr_in *)remote; |
| 1723 | if (sin->sin_addr.s_addr == |
| 1724 | rsin->sin_addr.s_addr) { |
| 1725 | /* found it */ |
| 1726 | if (netp != NULL) { |
| 1727 | *netp = net; |
| 1728 | } |
| 1729 | if (locked_tcb == NULL) { |
| 1730 | SCTP_INP_DECR_REF(inp); |
| 1731 | } else if (locked_tcb != stcb) { |
| 1732 | SCTP_TCB_LOCK(locked_tcb); |
| 1733 | } |
| 1734 | if (locked_tcb) { |
| 1735 | atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); |
| 1736 | } |
| 1737 | SCTP_INP_WUNLOCK(inp); |
| 1738 | SCTP_INP_INFO_RUNLOCK(); |
| 1739 | return (stcb); |
| 1740 | } |
| 1741 | break; |
| 1742 | } |
| 1743 | #endif |
| 1744 | #ifdef INET6 |
| 1745 | case AF_INET6: |
| 1746 | { |
| 1747 | struct sockaddr_in6 *sin6, *rsin6; |
| 1748 | |
| 1749 | sin6 = (struct sockaddr_in6 *) |
| 1750 | &net->ro._l_addr; |
| 1751 | rsin6 = (struct sockaddr_in6 *)remote; |
| 1752 | if (SCTP6_ARE_ADDR_EQUAL(sin6, |
| 1753 | rsin6)) { |
| 1754 | /* found it */ |
| 1755 | if (netp != NULL) { |
| 1756 | *netp = net; |
| 1757 | } |
| 1758 | if (locked_tcb == NULL) { |
| 1759 | SCTP_INP_DECR_REF(inp); |
| 1760 | } else if (locked_tcb != stcb) { |
| 1761 | SCTP_TCB_LOCK(locked_tcb); |
| 1762 | } |
| 1763 | if (locked_tcb) { |
| 1764 | atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); |
| 1765 | } |
| 1766 | SCTP_INP_WUNLOCK(inp); |
| 1767 | SCTP_INP_INFO_RUNLOCK(); |
| 1768 | return (stcb); |
| 1769 | } |
| 1770 | break; |
| 1771 | } |
| 1772 | #endif |
tuexen | 5d9b974 | 2012-07-11 21:52:01 +0000 | [diff] [blame] | 1773 | #if defined(__Userspace__) |
| 1774 | case AF_CONN: |
| 1775 | { |
| 1776 | struct sockaddr_conn *sconn, *rsconn; |
| 1777 | |
| 1778 | sconn = (struct sockaddr_conn *)&net->ro._l_addr; |
| 1779 | rsconn = (struct sockaddr_conn *)remote; |
| 1780 | if (sconn->sconn_addr == rsconn->sconn_addr) { |
| 1781 | /* found it */ |
| 1782 | if (netp != NULL) { |
| 1783 | *netp = net; |
| 1784 | } |
| 1785 | if (locked_tcb == NULL) { |
| 1786 | SCTP_INP_DECR_REF(inp); |
| 1787 | } else if (locked_tcb != stcb) { |
| 1788 | SCTP_TCB_LOCK(locked_tcb); |
| 1789 | } |
| 1790 | if (locked_tcb) { |
| 1791 | atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); |
| 1792 | } |
| 1793 | SCTP_INP_WUNLOCK(inp); |
| 1794 | SCTP_INP_INFO_RUNLOCK(); |
| 1795 | return (stcb); |
| 1796 | } |
| 1797 | break; |
| 1798 | } |
| 1799 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1800 | default: |
| 1801 | /* TSNH */ |
| 1802 | break; |
| 1803 | } |
| 1804 | } |
| 1805 | SCTP_TCB_UNLOCK(stcb); |
| 1806 | } |
| 1807 | } |
| 1808 | null_return: |
| 1809 | /* clean up for returning null */ |
| 1810 | if (locked_tcb) { |
| 1811 | SCTP_TCB_LOCK(locked_tcb); |
| 1812 | atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); |
| 1813 | } |
| 1814 | SCTP_INP_WUNLOCK(inp); |
| 1815 | SCTP_INP_INFO_RUNLOCK(); |
| 1816 | /* not found */ |
| 1817 | return (NULL); |
| 1818 | } |
| 1819 | |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 1820 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1821 | /* |
| 1822 | * Find an association for a specific endpoint using the association id given |
| 1823 | * out in the COMM_UP notification |
| 1824 | */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1825 | struct sctp_tcb * |
| 1826 | sctp_findasoc_ep_asocid_locked(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock) |
| 1827 | { |
| 1828 | /* |
| 1829 | * Use my the assoc_id to find a endpoint |
| 1830 | */ |
| 1831 | struct sctpasochead *head; |
| 1832 | struct sctp_tcb *stcb; |
| 1833 | uint32_t id; |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 1834 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1835 | if (inp == NULL) { |
| 1836 | SCTP_PRINTF("TSNH ep_associd\n"); |
| 1837 | return (NULL); |
| 1838 | } |
| 1839 | if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
| 1840 | SCTP_PRINTF("TSNH ep_associd0\n"); |
| 1841 | return (NULL); |
| 1842 | } |
| 1843 | id = (uint32_t)asoc_id; |
| 1844 | head = &inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(id, inp->hashasocidmark)]; |
| 1845 | if (head == NULL) { |
| 1846 | /* invalid id TSNH */ |
| 1847 | SCTP_PRINTF("TSNH ep_associd1\n"); |
| 1848 | return (NULL); |
| 1849 | } |
| 1850 | LIST_FOREACH(stcb, head, sctp_tcbasocidhash) { |
| 1851 | if (stcb->asoc.assoc_id == id) { |
| 1852 | if (inp != stcb->sctp_ep) { |
| 1853 | /* |
| 1854 | * some other guy has the same id active (id |
| 1855 | * collision ??). |
| 1856 | */ |
| 1857 | SCTP_PRINTF("TSNH ep_associd2\n"); |
| 1858 | continue; |
| 1859 | } |
| 1860 | if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { |
| 1861 | continue; |
| 1862 | } |
| 1863 | if (want_lock) { |
| 1864 | SCTP_TCB_LOCK(stcb); |
| 1865 | } |
| 1866 | return (stcb); |
| 1867 | } |
| 1868 | } |
| 1869 | return (NULL); |
| 1870 | } |
| 1871 | |
| 1872 | |
| 1873 | struct sctp_tcb * |
| 1874 | sctp_findassociation_ep_asocid(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock) |
| 1875 | { |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 1876 | struct sctp_tcb *stcb; |
| 1877 | |
| 1878 | SCTP_INP_RLOCK(inp); |
| 1879 | stcb = sctp_findasoc_ep_asocid_locked(inp, asoc_id, want_lock); |
| 1880 | SCTP_INP_RUNLOCK(inp); |
| 1881 | return (stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1882 | } |
| 1883 | |
| 1884 | |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 1885 | /* |
| 1886 | * Endpoint probe expects that the INP_INFO is locked. |
| 1887 | */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1888 | static struct sctp_inpcb * |
| 1889 | sctp_endpoint_probe(struct sockaddr *nam, struct sctppcbhead *head, |
| 1890 | uint16_t lport, uint32_t vrf_id) |
| 1891 | { |
| 1892 | struct sctp_inpcb *inp; |
| 1893 | struct sctp_laddr *laddr; |
| 1894 | #ifdef INET |
| 1895 | struct sockaddr_in *sin; |
| 1896 | #endif |
| 1897 | #ifdef INET6 |
| 1898 | struct sockaddr_in6 *sin6; |
| 1899 | struct sockaddr_in6 *intf_addr6; |
| 1900 | #endif |
tuexen | 09a676e | 2012-07-08 21:03:18 +0000 | [diff] [blame] | 1901 | #if defined(__Userspace__) |
| 1902 | struct sockaddr_conn *sconn; |
| 1903 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1904 | #ifdef SCTP_MVRF |
| 1905 | int i; |
| 1906 | #endif |
| 1907 | int fnd; |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 1908 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1909 | #ifdef INET |
| 1910 | sin = NULL; |
| 1911 | #endif |
| 1912 | #ifdef INET6 |
| 1913 | sin6 = NULL; |
| 1914 | #endif |
t00fcxen | b4c54ac | 2012-09-15 09:17:50 +0000 | [diff] [blame] | 1915 | #if defined(__Userspace__) |
| 1916 | sconn = NULL; |
| 1917 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1918 | switch (nam->sa_family) { |
| 1919 | #ifdef INET |
| 1920 | case AF_INET: |
| 1921 | sin = (struct sockaddr_in *)nam; |
| 1922 | break; |
| 1923 | #endif |
| 1924 | #ifdef INET6 |
| 1925 | case AF_INET6: |
| 1926 | sin6 = (struct sockaddr_in6 *)nam; |
| 1927 | break; |
| 1928 | #endif |
tuexen | 09a676e | 2012-07-08 21:03:18 +0000 | [diff] [blame] | 1929 | #if defined(__Userspace__) |
| 1930 | case AF_CONN: |
| 1931 | sconn = (struct sockaddr_conn *)nam; |
| 1932 | break; |
| 1933 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1934 | default: |
| 1935 | /* unsupported family */ |
| 1936 | return (NULL); |
| 1937 | } |
| 1938 | |
| 1939 | if (head == NULL) |
| 1940 | return (NULL); |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 1941 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1942 | LIST_FOREACH(inp, head, sctp_hash) { |
| 1943 | SCTP_INP_RLOCK(inp); |
| 1944 | if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
| 1945 | SCTP_INP_RUNLOCK(inp); |
| 1946 | continue; |
| 1947 | } |
| 1948 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) && |
| 1949 | (inp->sctp_lport == lport)) { |
| 1950 | /* got it */ |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 1951 | switch (nam->sa_family) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1952 | #ifdef INET |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 1953 | case AF_INET: |
| 1954 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) && |
Michael Tuexen | 72e5cec | 2019-08-05 13:21:02 +0200 | [diff] [blame] | 1955 | SCTP_IPV6_V6ONLY(inp)) { |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 1956 | /* IPv4 on a IPv6 socket with ONLY IPv6 set */ |
| 1957 | SCTP_INP_RUNLOCK(inp); |
| 1958 | continue; |
| 1959 | } |
| 1960 | #if defined(__FreeBSD__) |
| 1961 | if (prison_check_ip4(inp->ip_inp.inp.inp_cred, |
| 1962 | &sin->sin_addr) != 0) { |
| 1963 | SCTP_INP_RUNLOCK(inp); |
| 1964 | continue; |
| 1965 | } |
| 1966 | #endif |
| 1967 | break; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1968 | #endif |
| 1969 | #ifdef INET6 |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 1970 | case AF_INET6: |
| 1971 | /* A V6 address and the endpoint is NOT bound V6 */ |
| 1972 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) { |
| 1973 | SCTP_INP_RUNLOCK(inp); |
| 1974 | continue; |
| 1975 | } |
| 1976 | #if defined(__FreeBSD__) |
| 1977 | if (prison_check_ip6(inp->ip_inp.inp.inp_cred, |
| 1978 | &sin6->sin6_addr) != 0) { |
| 1979 | SCTP_INP_RUNLOCK(inp); |
| 1980 | continue; |
| 1981 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1982 | #endif |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 1983 | break; |
| 1984 | #endif |
| 1985 | default: |
| 1986 | break; |
| 1987 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1988 | /* does a VRF id match? */ |
| 1989 | fnd = 0; |
| 1990 | #ifdef SCTP_MVRF |
| 1991 | for (i = 0; i < inp->num_vrfs; i++) { |
| 1992 | if (inp->m_vrf_ids[i] == vrf_id) { |
| 1993 | fnd = 1; |
| 1994 | break; |
| 1995 | } |
| 1996 | } |
| 1997 | #else |
| 1998 | if (inp->def_vrf_id == vrf_id) |
| 1999 | fnd = 1; |
| 2000 | #endif |
| 2001 | |
| 2002 | SCTP_INP_RUNLOCK(inp); |
| 2003 | if (!fnd) |
| 2004 | continue; |
| 2005 | return (inp); |
| 2006 | } |
| 2007 | SCTP_INP_RUNLOCK(inp); |
| 2008 | } |
| 2009 | switch (nam->sa_family) { |
| 2010 | #ifdef INET |
| 2011 | case AF_INET: |
| 2012 | if (sin->sin_addr.s_addr == INADDR_ANY) { |
| 2013 | /* Can't hunt for one that has no address specified */ |
| 2014 | return (NULL); |
| 2015 | } |
| 2016 | break; |
| 2017 | #endif |
| 2018 | #ifdef INET6 |
| 2019 | case AF_INET6: |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 2020 | if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2021 | /* Can't hunt for one that has no address specified */ |
| 2022 | return (NULL); |
| 2023 | } |
| 2024 | break; |
| 2025 | #endif |
tuexen | 09a676e | 2012-07-08 21:03:18 +0000 | [diff] [blame] | 2026 | #if defined(__Userspace__) |
| 2027 | case AF_CONN: |
| 2028 | if (sconn->sconn_addr == NULL) { |
| 2029 | return (NULL); |
| 2030 | } |
| 2031 | break; |
| 2032 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2033 | default: |
| 2034 | break; |
| 2035 | } |
| 2036 | /* |
| 2037 | * ok, not bound to all so see if we can find a EP bound to this |
| 2038 | * address. |
| 2039 | */ |
| 2040 | LIST_FOREACH(inp, head, sctp_hash) { |
| 2041 | SCTP_INP_RLOCK(inp); |
| 2042 | if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
| 2043 | SCTP_INP_RUNLOCK(inp); |
| 2044 | continue; |
| 2045 | } |
| 2046 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)) { |
| 2047 | SCTP_INP_RUNLOCK(inp); |
| 2048 | continue; |
| 2049 | } |
| 2050 | /* |
| 2051 | * Ok this could be a likely candidate, look at all of its |
| 2052 | * addresses |
| 2053 | */ |
| 2054 | if (inp->sctp_lport != lport) { |
| 2055 | SCTP_INP_RUNLOCK(inp); |
| 2056 | continue; |
| 2057 | } |
| 2058 | /* does a VRF id match? */ |
| 2059 | fnd = 0; |
| 2060 | #ifdef SCTP_MVRF |
| 2061 | for (i = 0; i < inp->num_vrfs; i++) { |
| 2062 | if (inp->m_vrf_ids[i] == vrf_id) { |
| 2063 | fnd = 1; |
| 2064 | break; |
| 2065 | } |
| 2066 | } |
| 2067 | #else |
| 2068 | if (inp->def_vrf_id == vrf_id) |
| 2069 | fnd = 1; |
| 2070 | |
| 2071 | #endif |
| 2072 | if (!fnd) { |
| 2073 | SCTP_INP_RUNLOCK(inp); |
| 2074 | continue; |
| 2075 | } |
| 2076 | LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { |
| 2077 | if (laddr->ifa == NULL) { |
| 2078 | SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n", |
Saúl Ibarra Corretgé | 8b04b41 | 2015-09-26 09:53:06 +0200 | [diff] [blame] | 2079 | __func__); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2080 | continue; |
| 2081 | } |
| 2082 | SCTPDBG(SCTP_DEBUG_PCB1, "Ok laddr->ifa:%p is possible, ", |
t00fcxen | 8fca7ff | 2012-09-05 18:58:42 +0000 | [diff] [blame] | 2083 | (void *)laddr->ifa); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2084 | if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) { |
| 2085 | SCTPDBG(SCTP_DEBUG_PCB1, "Huh IFA being deleted\n"); |
| 2086 | continue; |
| 2087 | } |
| 2088 | if (laddr->ifa->address.sa.sa_family == nam->sa_family) { |
| 2089 | /* possible, see if it matches */ |
| 2090 | switch (nam->sa_family) { |
| 2091 | #ifdef INET |
| 2092 | case AF_INET: |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 2093 | #if defined(__APPLE__) |
| 2094 | if (sin == NULL) { |
| 2095 | /* TSNH */ |
| 2096 | break; |
| 2097 | } |
| 2098 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2099 | if (sin->sin_addr.s_addr == |
| 2100 | laddr->ifa->address.sin.sin_addr.s_addr) { |
| 2101 | SCTP_INP_RUNLOCK(inp); |
| 2102 | return (inp); |
| 2103 | } |
| 2104 | break; |
| 2105 | #endif |
| 2106 | #ifdef INET6 |
| 2107 | case AF_INET6: |
| 2108 | intf_addr6 = &laddr->ifa->address.sin6; |
| 2109 | if (SCTP6_ARE_ADDR_EQUAL(sin6, |
| 2110 | intf_addr6)) { |
| 2111 | SCTP_INP_RUNLOCK(inp); |
| 2112 | return (inp); |
| 2113 | } |
| 2114 | break; |
| 2115 | #endif |
tuexen | 09a676e | 2012-07-08 21:03:18 +0000 | [diff] [blame] | 2116 | #if defined(__Userspace__) |
| 2117 | case AF_CONN: |
| 2118 | if (sconn->sconn_addr == laddr->ifa->address.sconn.sconn_addr) { |
| 2119 | SCTP_INP_RUNLOCK(inp); |
| 2120 | return (inp); |
| 2121 | } |
| 2122 | break; |
| 2123 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2124 | } |
| 2125 | } |
| 2126 | } |
| 2127 | SCTP_INP_RUNLOCK(inp); |
| 2128 | } |
| 2129 | return (NULL); |
| 2130 | } |
| 2131 | |
| 2132 | |
| 2133 | static struct sctp_inpcb * |
| 2134 | sctp_isport_inuse(struct sctp_inpcb *inp, uint16_t lport, uint32_t vrf_id) |
| 2135 | { |
| 2136 | struct sctppcbhead *head; |
| 2137 | struct sctp_inpcb *t_inp; |
| 2138 | #ifdef SCTP_MVRF |
| 2139 | int i; |
| 2140 | #endif |
| 2141 | int fnd; |
| 2142 | |
| 2143 | head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport, |
| 2144 | SCTP_BASE_INFO(hashmark))]; |
| 2145 | LIST_FOREACH(t_inp, head, sctp_hash) { |
| 2146 | if (t_inp->sctp_lport != lport) { |
| 2147 | continue; |
| 2148 | } |
| 2149 | /* is it in the VRF in question */ |
| 2150 | fnd = 0; |
| 2151 | #ifdef SCTP_MVRF |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 2152 | for (i = 0; i < inp->num_vrfs; i++) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2153 | if (t_inp->m_vrf_ids[i] == vrf_id) { |
| 2154 | fnd = 1; |
| 2155 | break; |
| 2156 | } |
| 2157 | } |
| 2158 | #else |
| 2159 | if (t_inp->def_vrf_id == vrf_id) |
| 2160 | fnd = 1; |
| 2161 | #endif |
| 2162 | if (!fnd) |
| 2163 | continue; |
| 2164 | |
| 2165 | /* This one is in use. */ |
| 2166 | /* check the v6/v4 binding issue */ |
| 2167 | if ((t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) && |
Michael Tuexen | 72e5cec | 2019-08-05 13:21:02 +0200 | [diff] [blame] | 2168 | SCTP_IPV6_V6ONLY(t_inp)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2169 | if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { |
| 2170 | /* collision in V6 space */ |
| 2171 | return (t_inp); |
| 2172 | } else { |
| 2173 | /* inp is BOUND_V4 no conflict */ |
| 2174 | continue; |
| 2175 | } |
| 2176 | } else if (t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { |
| 2177 | /* t_inp is bound v4 and v6, conflict always */ |
| 2178 | return (t_inp); |
| 2179 | } else { |
| 2180 | /* t_inp is bound only V4 */ |
| 2181 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) && |
Michael Tuexen | 72e5cec | 2019-08-05 13:21:02 +0200 | [diff] [blame] | 2182 | SCTP_IPV6_V6ONLY(inp)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2183 | /* no conflict */ |
| 2184 | continue; |
| 2185 | } |
| 2186 | /* else fall through to conflict */ |
| 2187 | } |
| 2188 | return (t_inp); |
| 2189 | } |
| 2190 | return (NULL); |
| 2191 | } |
| 2192 | |
| 2193 | |
| 2194 | int |
| 2195 | sctp_swap_inpcb_for_listen(struct sctp_inpcb *inp) |
| 2196 | { |
| 2197 | /* For 1-2-1 with port reuse */ |
| 2198 | struct sctppcbhead *head; |
t00fcxen | c00f4bc | 2015-03-24 21:17:16 +0000 | [diff] [blame] | 2199 | struct sctp_inpcb *tinp, *ninp; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2200 | |
| 2201 | if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) { |
| 2202 | /* only works with port reuse on */ |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 2203 | return (-1); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2204 | } |
| 2205 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) { |
| 2206 | return (0); |
| 2207 | } |
| 2208 | SCTP_INP_RUNLOCK(inp); |
t00fcxen | c00f4bc | 2015-03-24 21:17:16 +0000 | [diff] [blame] | 2209 | SCTP_INP_INFO_WLOCK(); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2210 | head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(inp->sctp_lport, |
| 2211 | SCTP_BASE_INFO(hashmark))]; |
| 2212 | /* Kick out all non-listeners to the TCP hash */ |
t00fcxen | c00f4bc | 2015-03-24 21:17:16 +0000 | [diff] [blame] | 2213 | LIST_FOREACH_SAFE(tinp, head, sctp_hash, ninp) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2214 | if (tinp->sctp_lport != inp->sctp_lport) { |
| 2215 | continue; |
| 2216 | } |
| 2217 | if (tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
| 2218 | continue; |
| 2219 | } |
| 2220 | if (tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { |
| 2221 | continue; |
| 2222 | } |
Michael Tuexen | d900e5f | 2017-06-23 10:52:43 +0200 | [diff] [blame] | 2223 | if (SCTP_IS_LISTENING(tinp)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2224 | continue; |
| 2225 | } |
| 2226 | SCTP_INP_WLOCK(tinp); |
| 2227 | LIST_REMOVE(tinp, sctp_hash); |
| 2228 | head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR(tinp->sctp_lport, SCTP_BASE_INFO(hashtcpmark))]; |
| 2229 | tinp->sctp_flags |= SCTP_PCB_FLAGS_IN_TCPPOOL; |
| 2230 | LIST_INSERT_HEAD(head, tinp, sctp_hash); |
| 2231 | SCTP_INP_WUNLOCK(tinp); |
| 2232 | } |
| 2233 | SCTP_INP_WLOCK(inp); |
| 2234 | /* Pull from where he was */ |
| 2235 | LIST_REMOVE(inp, sctp_hash); |
| 2236 | inp->sctp_flags &= ~SCTP_PCB_FLAGS_IN_TCPPOOL; |
| 2237 | head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(inp->sctp_lport, SCTP_BASE_INFO(hashmark))]; |
| 2238 | LIST_INSERT_HEAD(head, inp, sctp_hash); |
| 2239 | SCTP_INP_WUNLOCK(inp); |
| 2240 | SCTP_INP_RLOCK(inp); |
t00fcxen | c00f4bc | 2015-03-24 21:17:16 +0000 | [diff] [blame] | 2241 | SCTP_INP_INFO_WUNLOCK(); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2242 | return (0); |
| 2243 | } |
| 2244 | |
| 2245 | |
| 2246 | struct sctp_inpcb * |
| 2247 | sctp_pcb_findep(struct sockaddr *nam, int find_tcp_pool, int have_lock, |
| 2248 | uint32_t vrf_id) |
| 2249 | { |
| 2250 | /* |
| 2251 | * First we check the hash table to see if someone has this port |
| 2252 | * bound with just the port. |
| 2253 | */ |
| 2254 | struct sctp_inpcb *inp; |
| 2255 | struct sctppcbhead *head; |
| 2256 | int lport; |
| 2257 | unsigned int i; |
| 2258 | #ifdef INET |
| 2259 | struct sockaddr_in *sin; |
| 2260 | #endif |
| 2261 | #ifdef INET6 |
| 2262 | struct sockaddr_in6 *sin6; |
| 2263 | #endif |
tuexen | 09a676e | 2012-07-08 21:03:18 +0000 | [diff] [blame] | 2264 | #if defined(__Userspace__) |
| 2265 | struct sockaddr_conn *sconn; |
| 2266 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2267 | |
| 2268 | switch (nam->sa_family) { |
| 2269 | #ifdef INET |
| 2270 | case AF_INET: |
| 2271 | sin = (struct sockaddr_in *)nam; |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 2272 | lport = sin->sin_port; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2273 | break; |
| 2274 | #endif |
| 2275 | #ifdef INET6 |
| 2276 | case AF_INET6: |
| 2277 | sin6 = (struct sockaddr_in6 *)nam; |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 2278 | lport = sin6->sin6_port; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2279 | break; |
| 2280 | #endif |
tuexen | 09a676e | 2012-07-08 21:03:18 +0000 | [diff] [blame] | 2281 | #if defined(__Userspace__) |
| 2282 | case AF_CONN: |
| 2283 | sconn = (struct sockaddr_conn *)nam; |
| 2284 | lport = sconn->sconn_port; |
| 2285 | break; |
| 2286 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2287 | default: |
| 2288 | return (NULL); |
| 2289 | } |
| 2290 | /* |
| 2291 | * I could cheat here and just cast to one of the types but we will |
| 2292 | * do it right. It also provides the check against an Unsupported |
| 2293 | * type too. |
| 2294 | */ |
| 2295 | /* Find the head of the ALLADDR chain */ |
| 2296 | if (have_lock == 0) { |
| 2297 | SCTP_INP_INFO_RLOCK(); |
| 2298 | } |
| 2299 | head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport, |
| 2300 | SCTP_BASE_INFO(hashmark))]; |
| 2301 | inp = sctp_endpoint_probe(nam, head, lport, vrf_id); |
| 2302 | |
| 2303 | /* |
| 2304 | * If the TCP model exists it could be that the main listening |
| 2305 | * endpoint is gone but there still exists a connected socket for this |
| 2306 | * guy. If so we can return the first one that we find. This may NOT |
| 2307 | * be the correct one so the caller should be wary on the returned INP. |
| 2308 | * Currently the only caller that sets find_tcp_pool is in bindx where |
| 2309 | * we are verifying that a user CAN bind the address. He either |
| 2310 | * has bound it already, or someone else has, or its open to bind, |
| 2311 | * so this is good enough. |
| 2312 | */ |
| 2313 | if (inp == NULL && find_tcp_pool) { |
| 2314 | for (i = 0; i < SCTP_BASE_INFO(hashtcpmark) + 1; i++) { |
| 2315 | head = &SCTP_BASE_INFO(sctp_tcpephash)[i]; |
| 2316 | inp = sctp_endpoint_probe(nam, head, lport, vrf_id); |
| 2317 | if (inp) { |
| 2318 | break; |
| 2319 | } |
| 2320 | } |
| 2321 | } |
| 2322 | if (inp) { |
| 2323 | SCTP_INP_INCR_REF(inp); |
| 2324 | } |
| 2325 | if (have_lock == 0) { |
| 2326 | SCTP_INP_INFO_RUNLOCK(); |
| 2327 | } |
| 2328 | return (inp); |
| 2329 | } |
| 2330 | |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 2331 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2332 | /* |
| 2333 | * Find an association for an endpoint with the pointer to whom you want to |
| 2334 | * send to and the endpoint pointer. The address can be IPv4 or IPv6. We may |
| 2335 | * need to change the *to to some other struct like a mbuf... |
| 2336 | */ |
| 2337 | struct sctp_tcb * |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 2338 | sctp_findassociation_addr_sa(struct sockaddr *from, struct sockaddr *to, |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2339 | struct sctp_inpcb **inp_p, struct sctp_nets **netp, int find_tcp_pool, |
| 2340 | uint32_t vrf_id) |
| 2341 | { |
| 2342 | struct sctp_inpcb *inp = NULL; |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2343 | struct sctp_tcb *stcb; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2344 | |
| 2345 | SCTP_INP_INFO_RLOCK(); |
| 2346 | if (find_tcp_pool) { |
| 2347 | if (inp_p != NULL) { |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2348 | stcb = sctp_tcb_special_locate(inp_p, from, to, netp, |
| 2349 | vrf_id); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2350 | } else { |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2351 | stcb = sctp_tcb_special_locate(&inp, from, to, netp, |
| 2352 | vrf_id); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2353 | } |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2354 | if (stcb != NULL) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2355 | SCTP_INP_INFO_RUNLOCK(); |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2356 | return (stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2357 | } |
| 2358 | } |
| 2359 | inp = sctp_pcb_findep(to, 0, 1, vrf_id); |
| 2360 | if (inp_p != NULL) { |
| 2361 | *inp_p = inp; |
| 2362 | } |
| 2363 | SCTP_INP_INFO_RUNLOCK(); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2364 | if (inp == NULL) { |
| 2365 | return (NULL); |
| 2366 | } |
| 2367 | /* |
| 2368 | * ok, we have an endpoint, now lets find the assoc for it (if any) |
| 2369 | * we now place the source address or from in the to of the find |
| 2370 | * endpoint call. Since in reality this chain is used from the |
| 2371 | * inbound packet side. |
| 2372 | */ |
| 2373 | if (inp_p != NULL) { |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2374 | stcb = sctp_findassociation_ep_addr(inp_p, from, netp, to, |
| 2375 | NULL); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2376 | } else { |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2377 | stcb = sctp_findassociation_ep_addr(&inp, from, netp, to, |
| 2378 | NULL); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2379 | } |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2380 | return (stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2381 | } |
| 2382 | |
| 2383 | |
| 2384 | /* |
| 2385 | * This routine will grub through the mbuf that is a INIT or INIT-ACK and |
| 2386 | * find all addresses that the sender has specified in any address list. Each |
| 2387 | * address will be used to lookup the TCB and see if one exits. |
| 2388 | */ |
| 2389 | static struct sctp_tcb * |
tuexen | 9784e9a | 2011-12-18 13:04:23 +0000 | [diff] [blame] | 2390 | sctp_findassociation_special_addr(struct mbuf *m, int offset, |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2391 | struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp, |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 2392 | struct sockaddr *dst) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2393 | { |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 2394 | struct sctp_paramhdr *phdr, param_buf; |
t00fcxen | aff14b6 | 2013-11-02 18:02:02 +0000 | [diff] [blame] | 2395 | #if defined(INET) || defined(INET6) |
t00fcxen | 6ebb603 | 2013-11-02 18:03:31 +0000 | [diff] [blame] | 2396 | struct sctp_tcb *stcb; |
t00fcxen | aff14b6 | 2013-11-02 18:02:02 +0000 | [diff] [blame] | 2397 | uint16_t ptype; |
| 2398 | #endif |
| 2399 | uint16_t plen; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2400 | #ifdef INET |
| 2401 | struct sockaddr_in sin4; |
| 2402 | #endif |
| 2403 | #ifdef INET6 |
| 2404 | struct sockaddr_in6 sin6; |
| 2405 | #endif |
| 2406 | |
| 2407 | #ifdef INET |
| 2408 | memset(&sin4, 0, sizeof(sin4)); |
t00fcxen | 8fbaee3 | 2012-09-04 16:41:39 +0000 | [diff] [blame] | 2409 | #ifdef HAVE_SIN_LEN |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2410 | sin4.sin_len = sizeof(sin4); |
| 2411 | #endif |
| 2412 | sin4.sin_family = AF_INET; |
| 2413 | sin4.sin_port = sh->src_port; |
| 2414 | #endif |
| 2415 | #ifdef INET6 |
| 2416 | memset(&sin6, 0, sizeof(sin6)); |
t00fcxen | 8fbaee3 | 2012-09-04 16:41:39 +0000 | [diff] [blame] | 2417 | #ifdef HAVE_SIN6_LEN |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2418 | sin6.sin6_len = sizeof(sin6); |
| 2419 | #endif |
| 2420 | sin6.sin6_family = AF_INET6; |
| 2421 | sin6.sin6_port = sh->src_port; |
| 2422 | #endif |
| 2423 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2424 | offset += sizeof(struct sctp_init_chunk); |
| 2425 | |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 2426 | phdr = sctp_get_next_param(m, offset, ¶m_buf, sizeof(param_buf)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2427 | while (phdr != NULL) { |
| 2428 | /* now we must see if we want the parameter */ |
t00fcxen | aff14b6 | 2013-11-02 18:02:02 +0000 | [diff] [blame] | 2429 | #if defined(INET) || defined(INET6) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2430 | ptype = ntohs(phdr->param_type); |
t00fcxen | aff14b6 | 2013-11-02 18:02:02 +0000 | [diff] [blame] | 2431 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2432 | plen = ntohs(phdr->param_length); |
| 2433 | if (plen == 0) { |
| 2434 | break; |
| 2435 | } |
| 2436 | #ifdef INET |
| 2437 | if (ptype == SCTP_IPV4_ADDRESS && |
| 2438 | plen == sizeof(struct sctp_ipv4addr_param)) { |
| 2439 | /* Get the rest of the address */ |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 2440 | struct sctp_ipv4addr_param ip4_param, *p4; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2441 | |
| 2442 | phdr = sctp_get_next_param(m, offset, |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 2443 | (struct sctp_paramhdr *)&ip4_param, sizeof(ip4_param)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2444 | if (phdr == NULL) { |
| 2445 | return (NULL); |
| 2446 | } |
| 2447 | p4 = (struct sctp_ipv4addr_param *)phdr; |
| 2448 | memcpy(&sin4.sin_addr, &p4->addr, sizeof(p4->addr)); |
| 2449 | /* look it up */ |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2450 | stcb = sctp_findassociation_ep_addr(inp_p, |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 2451 | (struct sockaddr *)&sin4, netp, dst, NULL); |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2452 | if (stcb != NULL) { |
| 2453 | return (stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2454 | } |
| 2455 | } |
| 2456 | #endif |
| 2457 | #ifdef INET6 |
| 2458 | if (ptype == SCTP_IPV6_ADDRESS && |
| 2459 | plen == sizeof(struct sctp_ipv6addr_param)) { |
| 2460 | /* Get the rest of the address */ |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 2461 | struct sctp_ipv6addr_param ip6_param, *p6; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2462 | |
| 2463 | phdr = sctp_get_next_param(m, offset, |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 2464 | (struct sctp_paramhdr *)&ip6_param, sizeof(ip6_param)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2465 | if (phdr == NULL) { |
| 2466 | return (NULL); |
| 2467 | } |
| 2468 | p6 = (struct sctp_ipv6addr_param *)phdr; |
| 2469 | memcpy(&sin6.sin6_addr, &p6->addr, sizeof(p6->addr)); |
| 2470 | /* look it up */ |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2471 | stcb = sctp_findassociation_ep_addr(inp_p, |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 2472 | (struct sockaddr *)&sin6, netp, dst, NULL); |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2473 | if (stcb != NULL) { |
| 2474 | return (stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2475 | } |
| 2476 | } |
| 2477 | #endif |
| 2478 | offset += SCTP_SIZE32(plen); |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 2479 | phdr = sctp_get_next_param(m, offset, ¶m_buf, |
| 2480 | sizeof(param_buf)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2481 | } |
| 2482 | return (NULL); |
| 2483 | } |
| 2484 | |
| 2485 | static struct sctp_tcb * |
| 2486 | sctp_findassoc_by_vtag(struct sockaddr *from, struct sockaddr *to, uint32_t vtag, |
| 2487 | struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint16_t rport, |
| 2488 | uint16_t lport, int skip_src_check, uint32_t vrf_id, uint32_t remote_tag) |
| 2489 | { |
| 2490 | /* |
| 2491 | * Use my vtag to hash. If we find it we then verify the source addr |
| 2492 | * is in the assoc. If all goes well we save a bit on rec of a |
| 2493 | * packet. |
| 2494 | */ |
| 2495 | struct sctpasochead *head; |
| 2496 | struct sctp_nets *net; |
| 2497 | struct sctp_tcb *stcb; |
tuexen | 9784e9a | 2011-12-18 13:04:23 +0000 | [diff] [blame] | 2498 | #ifdef SCTP_MVRF |
| 2499 | unsigned int i; |
| 2500 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2501 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2502 | SCTP_INP_INFO_RLOCK(); |
| 2503 | head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(vtag, |
t00fcxen | 0d66488 | 2015-01-11 21:47:36 +0000 | [diff] [blame] | 2504 | SCTP_BASE_INFO(hashasocmark))]; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2505 | LIST_FOREACH(stcb, head, sctp_asocs) { |
| 2506 | SCTP_INP_RLOCK(stcb->sctp_ep); |
| 2507 | if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
| 2508 | SCTP_INP_RUNLOCK(stcb->sctp_ep); |
| 2509 | continue; |
| 2510 | } |
tuexen | 9784e9a | 2011-12-18 13:04:23 +0000 | [diff] [blame] | 2511 | #ifdef SCTP_MVRF |
| 2512 | for (i = 0; i < stcb->sctp_ep->num_vrfs; i++) { |
| 2513 | if (stcb->sctp_ep->m_vrf_ids[i] == vrf_id) { |
| 2514 | break; |
| 2515 | } |
| 2516 | } |
| 2517 | if (i == stcb->sctp_ep->num_vrfs) { |
| 2518 | SCTP_INP_RUNLOCK(inp); |
| 2519 | continue; |
| 2520 | } |
| 2521 | #else |
| 2522 | if (stcb->sctp_ep->def_vrf_id != vrf_id) { |
| 2523 | SCTP_INP_RUNLOCK(stcb->sctp_ep); |
| 2524 | continue; |
| 2525 | } |
| 2526 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2527 | SCTP_TCB_LOCK(stcb); |
| 2528 | SCTP_INP_RUNLOCK(stcb->sctp_ep); |
| 2529 | if (stcb->asoc.my_vtag == vtag) { |
| 2530 | /* candidate */ |
| 2531 | if (stcb->rport != rport) { |
| 2532 | SCTP_TCB_UNLOCK(stcb); |
| 2533 | continue; |
| 2534 | } |
| 2535 | if (stcb->sctp_ep->sctp_lport != lport) { |
| 2536 | SCTP_TCB_UNLOCK(stcb); |
| 2537 | continue; |
| 2538 | } |
| 2539 | if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { |
| 2540 | SCTP_TCB_UNLOCK(stcb); |
| 2541 | continue; |
| 2542 | } |
| 2543 | /* RRS:Need toaddr check here */ |
| 2544 | if (sctp_does_stcb_own_this_addr(stcb, to) == 0) { |
| 2545 | /* Endpoint does not own this address */ |
| 2546 | SCTP_TCB_UNLOCK(stcb); |
| 2547 | continue; |
| 2548 | } |
| 2549 | if (remote_tag) { |
| 2550 | /* If we have both vtags that's all we match on */ |
| 2551 | if (stcb->asoc.peer_vtag == remote_tag) { |
| 2552 | /* If both tags match we consider it conclusive |
| 2553 | * and check NO source/destination addresses |
| 2554 | */ |
| 2555 | goto conclusive; |
| 2556 | } |
| 2557 | } |
| 2558 | if (skip_src_check) { |
| 2559 | conclusive: |
| 2560 | if (from) { |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 2561 | *netp = sctp_findnet(stcb, from); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2562 | } else { |
| 2563 | *netp = NULL; /* unknown */ |
| 2564 | } |
| 2565 | if (inp_p) |
| 2566 | *inp_p = stcb->sctp_ep; |
| 2567 | SCTP_INP_INFO_RUNLOCK(); |
| 2568 | return (stcb); |
| 2569 | } |
| 2570 | net = sctp_findnet(stcb, from); |
| 2571 | if (net) { |
| 2572 | /* yep its him. */ |
| 2573 | *netp = net; |
| 2574 | SCTP_STAT_INCR(sctps_vtagexpress); |
| 2575 | *inp_p = stcb->sctp_ep; |
| 2576 | SCTP_INP_INFO_RUNLOCK(); |
| 2577 | return (stcb); |
| 2578 | } else { |
| 2579 | /* |
| 2580 | * not him, this should only happen in rare |
| 2581 | * cases so I peg it. |
| 2582 | */ |
| 2583 | SCTP_STAT_INCR(sctps_vtagbogus); |
| 2584 | } |
| 2585 | } |
| 2586 | SCTP_TCB_UNLOCK(stcb); |
| 2587 | } |
| 2588 | SCTP_INP_INFO_RUNLOCK(); |
| 2589 | return (NULL); |
| 2590 | } |
| 2591 | |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 2592 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2593 | /* |
| 2594 | * Find an association with the pointer to the inbound IP packet. This can be |
| 2595 | * a IPv4 or IPv6 packet. |
| 2596 | */ |
| 2597 | struct sctp_tcb * |
tuexen | 9784e9a | 2011-12-18 13:04:23 +0000 | [diff] [blame] | 2598 | sctp_findassociation_addr(struct mbuf *m, int offset, |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 2599 | struct sockaddr *src, struct sockaddr *dst, |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2600 | struct sctphdr *sh, struct sctp_chunkhdr *ch, |
| 2601 | struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id) |
| 2602 | { |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2603 | struct sctp_tcb *stcb; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2604 | struct sctp_inpcb *inp; |
| 2605 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2606 | if (sh->v_tag) { |
| 2607 | /* we only go down this path if vtag is non-zero */ |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2608 | stcb = sctp_findassoc_by_vtag(src, dst, ntohl(sh->v_tag), |
t00fcxen | afd2119 | 2013-05-10 18:15:02 +0000 | [diff] [blame] | 2609 | inp_p, netp, sh->src_port, sh->dest_port, 0, vrf_id, 0); |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2610 | if (stcb) { |
| 2611 | return (stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2612 | } |
| 2613 | } |
| 2614 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2615 | if (inp_p) { |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2616 | stcb = sctp_findassociation_addr_sa(src, dst, inp_p, netp, |
Michael Tuexen | 7f20278 | 2016-01-28 17:09:30 +0100 | [diff] [blame] | 2617 | 1, vrf_id); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2618 | inp = *inp_p; |
| 2619 | } else { |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2620 | stcb = sctp_findassociation_addr_sa(src, dst, &inp, netp, |
Michael Tuexen | 7f20278 | 2016-01-28 17:09:30 +0100 | [diff] [blame] | 2621 | 1, vrf_id); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2622 | } |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2623 | SCTPDBG(SCTP_DEBUG_PCB1, "stcb:%p inp:%p\n", (void *)stcb, (void *)inp); |
| 2624 | if (stcb == NULL && inp) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2625 | /* Found a EP but not this address */ |
| 2626 | if ((ch->chunk_type == SCTP_INITIATION) || |
| 2627 | (ch->chunk_type == SCTP_INITIATION_ACK)) { |
| 2628 | /*- |
| 2629 | * special hook, we do NOT return linp or an |
| 2630 | * association that is linked to an existing |
| 2631 | * association that is under the TCP pool (i.e. no |
| 2632 | * listener exists). The endpoint finding routine |
| 2633 | * will always find a listener before examining the |
| 2634 | * TCP pool. |
| 2635 | */ |
| 2636 | if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) { |
| 2637 | if (inp_p) { |
| 2638 | *inp_p = NULL; |
| 2639 | } |
| 2640 | return (NULL); |
| 2641 | } |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2642 | stcb = sctp_findassociation_special_addr(m, |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 2643 | offset, sh, &inp, netp, dst); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2644 | if (inp_p != NULL) { |
| 2645 | *inp_p = inp; |
| 2646 | } |
| 2647 | } |
| 2648 | } |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2649 | SCTPDBG(SCTP_DEBUG_PCB1, "stcb is %p\n", (void *)stcb); |
| 2650 | return (stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2651 | } |
| 2652 | |
| 2653 | /* |
| 2654 | * lookup an association by an ASCONF lookup address. |
| 2655 | * if the lookup address is 0.0.0.0 or ::0, use the vtag to do the lookup |
| 2656 | */ |
| 2657 | struct sctp_tcb * |
tuexen | 9784e9a | 2011-12-18 13:04:23 +0000 | [diff] [blame] | 2658 | sctp_findassociation_ep_asconf(struct mbuf *m, int offset, |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 2659 | struct sockaddr *dst, struct sctphdr *sh, |
| 2660 | struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2661 | { |
| 2662 | struct sctp_tcb *stcb; |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 2663 | union sctp_sockstore remote_store; |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 2664 | struct sctp_paramhdr param_buf, *phdr; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2665 | int ptype; |
| 2666 | int zero_address = 0; |
| 2667 | #ifdef INET |
| 2668 | struct sockaddr_in *sin; |
| 2669 | #endif |
| 2670 | #ifdef INET6 |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2671 | struct sockaddr_in6 *sin6; |
| 2672 | #endif |
| 2673 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2674 | memset(&remote_store, 0, sizeof(remote_store)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2675 | phdr = sctp_get_next_param(m, offset + sizeof(struct sctp_asconf_chunk), |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 2676 | ¶m_buf, sizeof(struct sctp_paramhdr)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2677 | if (phdr == NULL) { |
| 2678 | 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] | 2679 | __func__); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2680 | return NULL; |
| 2681 | } |
| 2682 | ptype = (int)((uint32_t) ntohs(phdr->param_type)); |
| 2683 | /* get the correlation address */ |
| 2684 | switch (ptype) { |
| 2685 | #ifdef INET6 |
| 2686 | case SCTP_IPV6_ADDRESS: |
| 2687 | { |
| 2688 | /* ipv6 address param */ |
| 2689 | struct sctp_ipv6addr_param *p6, p6_buf; |
| 2690 | |
| 2691 | if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv6addr_param)) { |
| 2692 | return NULL; |
| 2693 | } |
| 2694 | p6 = (struct sctp_ipv6addr_param *)sctp_get_next_param(m, |
| 2695 | offset + sizeof(struct sctp_asconf_chunk), |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 2696 | &p6_buf.ph, sizeof(p6_buf)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2697 | if (p6 == NULL) { |
| 2698 | 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] | 2699 | __func__); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2700 | return (NULL); |
| 2701 | } |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 2702 | sin6 = &remote_store.sin6; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2703 | sin6->sin6_family = AF_INET6; |
t00fcxen | 8fbaee3 | 2012-09-04 16:41:39 +0000 | [diff] [blame] | 2704 | #ifdef HAVE_SIN6_LEN |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2705 | sin6->sin6_len = sizeof(*sin6); |
| 2706 | #endif |
| 2707 | sin6->sin6_port = sh->src_port; |
| 2708 | memcpy(&sin6->sin6_addr, &p6->addr, sizeof(struct in6_addr)); |
| 2709 | if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) |
| 2710 | zero_address = 1; |
| 2711 | break; |
| 2712 | } |
| 2713 | #endif |
| 2714 | #ifdef INET |
| 2715 | case SCTP_IPV4_ADDRESS: |
| 2716 | { |
| 2717 | /* ipv4 address param */ |
| 2718 | struct sctp_ipv4addr_param *p4, p4_buf; |
| 2719 | |
| 2720 | if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv4addr_param)) { |
| 2721 | return NULL; |
| 2722 | } |
| 2723 | p4 = (struct sctp_ipv4addr_param *)sctp_get_next_param(m, |
| 2724 | offset + sizeof(struct sctp_asconf_chunk), |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 2725 | &p4_buf.ph, sizeof(p4_buf)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2726 | if (p4 == NULL) { |
| 2727 | 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] | 2728 | __func__); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2729 | return (NULL); |
| 2730 | } |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 2731 | sin = &remote_store.sin; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2732 | sin->sin_family = AF_INET; |
t00fcxen | 8fbaee3 | 2012-09-04 16:41:39 +0000 | [diff] [blame] | 2733 | #ifdef HAVE_SIN_LEN |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2734 | sin->sin_len = sizeof(*sin); |
| 2735 | #endif |
| 2736 | sin->sin_port = sh->src_port; |
| 2737 | memcpy(&sin->sin_addr, &p4->addr, sizeof(struct in_addr)); |
| 2738 | if (sin->sin_addr.s_addr == INADDR_ANY) |
| 2739 | zero_address = 1; |
| 2740 | break; |
| 2741 | } |
| 2742 | #endif |
| 2743 | default: |
| 2744 | /* invalid address param type */ |
| 2745 | return NULL; |
| 2746 | } |
| 2747 | |
| 2748 | if (zero_address) { |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 2749 | stcb = sctp_findassoc_by_vtag(NULL, dst, ntohl(sh->v_tag), inp_p, |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2750 | netp, sh->src_port, sh->dest_port, 1, vrf_id, 0); |
tuexen | b53ec51 | 2012-08-05 10:55:12 +0000 | [diff] [blame] | 2751 | if (stcb != NULL) { |
| 2752 | SCTP_INP_DECR_REF(*inp_p); |
| 2753 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2754 | } else { |
| 2755 | stcb = sctp_findassociation_ep_addr(inp_p, |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 2756 | &remote_store.sa, netp, |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 2757 | dst, NULL); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2758 | } |
| 2759 | return (stcb); |
| 2760 | } |
| 2761 | |
| 2762 | |
| 2763 | /* |
| 2764 | * allocate a sctp_inpcb and setup a temporary binding to a port/all |
| 2765 | * addresses. This way if we don't get a bind we by default pick a ephemeral |
| 2766 | * port with all addresses bound. |
| 2767 | */ |
| 2768 | int |
| 2769 | sctp_inpcb_alloc(struct socket *so, uint32_t vrf_id) |
| 2770 | { |
| 2771 | /* |
| 2772 | * we get called when a new endpoint starts up. We need to allocate |
| 2773 | * the sctp_inpcb structure from the zone and init it. Mark it as |
| 2774 | * unbound and find a port that we can use as an ephemeral with |
| 2775 | * INADDR_ANY. If the user binds later no problem we can then add in |
| 2776 | * the specific addresses. And setup the default parameters for the |
| 2777 | * EP. |
| 2778 | */ |
| 2779 | int i, error; |
| 2780 | struct sctp_inpcb *inp; |
| 2781 | struct sctp_pcb *m; |
| 2782 | struct timeval time; |
| 2783 | sctp_sharedkey_t *null_key; |
| 2784 | |
| 2785 | error = 0; |
| 2786 | |
| 2787 | SCTP_INP_INFO_WLOCK(); |
| 2788 | inp = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_ep), struct sctp_inpcb); |
| 2789 | if (inp == NULL) { |
| 2790 | SCTP_PRINTF("Out of SCTP-INPCB structures - no resources\n"); |
| 2791 | SCTP_INP_INFO_WUNLOCK(); |
| 2792 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS); |
| 2793 | return (ENOBUFS); |
| 2794 | } |
| 2795 | /* zap it */ |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 2796 | memset(inp, 0, sizeof(*inp)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2797 | |
| 2798 | /* bump generations */ |
| 2799 | #if defined(__APPLE__) |
| 2800 | inp->ip_inp.inp.inp_state = INPCB_STATE_INUSE; |
| 2801 | #endif |
| 2802 | /* setup socket pointers */ |
| 2803 | inp->sctp_socket = so; |
| 2804 | inp->ip_inp.inp.inp_socket = so; |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 2805 | #if defined(__FreeBSD__) |
| 2806 | inp->ip_inp.inp.inp_cred = crhold(so->so_cred); |
| 2807 | #endif |
tuexen | c0bb612 | 2012-01-15 14:21:21 +0000 | [diff] [blame] | 2808 | #ifdef INET6 |
tuexen | 76eb209 | 2012-04-01 16:35:28 +0000 | [diff] [blame] | 2809 | #if !defined(__Userspace__) && !defined(__Windows__) |
t00fcxen | afd2119 | 2013-05-10 18:15:02 +0000 | [diff] [blame] | 2810 | if (INP_SOCKAF(so) == AF_INET6) { |
| 2811 | if (MODULE_GLOBAL(ip6_auto_flowlabel)) { |
| 2812 | inp->ip_inp.inp.inp_flags |= IN6P_AUTOFLOWLABEL; |
| 2813 | } |
| 2814 | if (MODULE_GLOBAL(ip6_v6only)) { |
| 2815 | inp->ip_inp.inp.inp_flags |= IN6P_IPV6_V6ONLY; |
| 2816 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2817 | } |
| 2818 | #endif |
tuexen | c0bb612 | 2012-01-15 14:21:21 +0000 | [diff] [blame] | 2819 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2820 | inp->sctp_associd_counter = 1; |
| 2821 | inp->partial_delivery_point = SCTP_SB_LIMIT_RCV(so) >> SCTP_PARTIAL_DELIVERY_SHIFT; |
| 2822 | inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT; |
t00fcxen | 3eda0f2 | 2015-03-10 20:10:45 +0000 | [diff] [blame] | 2823 | inp->max_cwnd = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2824 | inp->sctp_cmt_on_off = SCTP_BASE_SYSCTL(sctp_cmt_on_off); |
t00fcxen | 478b772 | 2014-08-02 18:24:12 +0000 | [diff] [blame] | 2825 | inp->ecn_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_ecn_enable); |
t00fcxen | 0e78cef | 2014-08-02 22:05:33 +0000 | [diff] [blame] | 2826 | inp->prsctp_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_pr_enable); |
t00fcxen | b3969a1 | 2014-08-12 13:19:21 +0000 | [diff] [blame] | 2827 | inp->auth_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_auth_enable); |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 2828 | inp->asconf_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_asconf_enable); |
t00fcxen | fa7e1a0 | 2014-08-04 20:41:36 +0000 | [diff] [blame] | 2829 | inp->reconfig_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_reconfig_enable); |
t00fcxen | 3656cf9 | 2014-08-03 15:25:02 +0000 | [diff] [blame] | 2830 | inp->nrsack_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_nrsack_enable); |
t00fcxen | 1cc0ba8 | 2014-08-03 20:32:08 +0000 | [diff] [blame] | 2831 | inp->pktdrop_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_pktdrop_enable); |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 2832 | inp->idata_supported = 0; |
| 2833 | |
t00fcxen | a51e446 | 2015-06-17 15:53:23 +0000 | [diff] [blame] | 2834 | #if defined(__FreeBSD__) |
| 2835 | inp->fibnum = so->so_fibnum; |
| 2836 | #else |
| 2837 | inp->fibnum = 0; |
| 2838 | #endif |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 2839 | #if defined(__Userspace__) |
tuexen | eda9dbb | 2012-05-27 11:10:08 +0000 | [diff] [blame] | 2840 | inp->ulp_info = NULL; |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 2841 | inp->recv_callback = NULL; |
| 2842 | inp->send_callback = NULL; |
| 2843 | inp->send_sb_threshold = 0; |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 2844 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2845 | /* init the small hash table we use to track asocid <-> tcb */ |
| 2846 | inp->sctp_asocidhash = SCTP_HASH_INIT(SCTP_STACK_VTAG_HASH_SIZE, &inp->hashasocidmark); |
| 2847 | if (inp->sctp_asocidhash == NULL) { |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 2848 | #if defined(__FreeBSD__) |
| 2849 | crfree(inp->ip_inp.inp.inp_cred); |
| 2850 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2851 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp); |
| 2852 | SCTP_INP_INFO_WUNLOCK(); |
| 2853 | return (ENOBUFS); |
| 2854 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2855 | SCTP_INCR_EP_COUNT(); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2856 | inp->ip_inp.inp.inp_ip_ttl = MODULE_GLOBAL(ip_defttl); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2857 | SCTP_INP_INFO_WUNLOCK(); |
| 2858 | |
| 2859 | so->so_pcb = (caddr_t)inp; |
| 2860 | |
tuexen | 5fc2284 | 2012-02-11 13:32:19 +0000 | [diff] [blame] | 2861 | #if defined(__FreeBSD__) && __FreeBSD_version < 803000 |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2862 | if ((SCTP_SO_TYPE(so) == SOCK_DGRAM) || |
| 2863 | (SCTP_SO_TYPE(so) == SOCK_SEQPACKET)) { |
| 2864 | #else |
| 2865 | if (SCTP_SO_TYPE(so) == SOCK_SEQPACKET) { |
| 2866 | #endif |
| 2867 | /* UDP style socket */ |
| 2868 | inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE | |
| 2869 | SCTP_PCB_FLAGS_UNBOUND); |
| 2870 | /* Be sure it is NON-BLOCKING IO for UDP */ |
| 2871 | /* SCTP_SET_SO_NBIO(so); */ |
| 2872 | } else if (SCTP_SO_TYPE(so) == SOCK_STREAM) { |
| 2873 | /* TCP style socket */ |
| 2874 | inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE | |
| 2875 | SCTP_PCB_FLAGS_UNBOUND); |
| 2876 | /* Be sure we have blocking IO by default */ |
Michael Tuexen | fe22357 | 2017-09-22 00:19:22 +0200 | [diff] [blame] | 2877 | SOCK_LOCK(so); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2878 | SCTP_CLEAR_SO_NBIO(so); |
Michael Tuexen | fe22357 | 2017-09-22 00:19:22 +0200 | [diff] [blame] | 2879 | SOCK_UNLOCK(so); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2880 | } else { |
| 2881 | /* |
| 2882 | * unsupported socket type (RAW, etc)- in case we missed it |
| 2883 | * in protosw |
| 2884 | */ |
| 2885 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EOPNOTSUPP); |
| 2886 | so->so_pcb = NULL; |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 2887 | #if defined(__FreeBSD__) |
| 2888 | crfree(inp->ip_inp.inp.inp_cred); |
| 2889 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2890 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp); |
| 2891 | return (EOPNOTSUPP); |
| 2892 | } |
| 2893 | if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_1) { |
| 2894 | sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE); |
| 2895 | sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS); |
| 2896 | } else if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_2) { |
| 2897 | sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE); |
| 2898 | sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS); |
| 2899 | } else if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_0) { |
| 2900 | sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE); |
| 2901 | sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS); |
| 2902 | } |
| 2903 | inp->sctp_tcbhash = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_pcbtblsize), |
| 2904 | &inp->sctp_hashmark); |
| 2905 | if (inp->sctp_tcbhash == NULL) { |
| 2906 | SCTP_PRINTF("Out of SCTP-INPCB->hashinit - no resources\n"); |
| 2907 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS); |
| 2908 | so->so_pcb = NULL; |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 2909 | #if defined(__FreeBSD__) |
| 2910 | crfree(inp->ip_inp.inp.inp_cred); |
| 2911 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2912 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp); |
| 2913 | return (ENOBUFS); |
| 2914 | } |
| 2915 | #ifdef SCTP_MVRF |
| 2916 | inp->vrf_size = SCTP_DEFAULT_VRF_SIZE; |
| 2917 | SCTP_MALLOC(inp->m_vrf_ids, uint32_t *, |
| 2918 | (sizeof(uint32_t) * inp->vrf_size), SCTP_M_MVRF); |
| 2919 | if (inp->m_vrf_ids == NULL) { |
| 2920 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS); |
| 2921 | so->so_pcb = NULL; |
| 2922 | SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark); |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 2923 | #if defined(__FreeBSD__) |
| 2924 | crfree(inp->ip_inp.inp.inp_cred); |
| 2925 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2926 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp); |
| 2927 | return (ENOBUFS); |
| 2928 | } |
| 2929 | inp->m_vrf_ids[0] = vrf_id; |
| 2930 | inp->num_vrfs = 1; |
| 2931 | #endif |
| 2932 | inp->def_vrf_id = vrf_id; |
| 2933 | |
| 2934 | #if defined(__APPLE__) |
tuexen | 9a218b1 | 2012-08-04 21:17:58 +0000 | [diff] [blame] | 2935 | #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) |
t00fcxen | bc766ab | 2014-01-03 19:29:31 +0000 | [diff] [blame] | 2936 | 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] | 2937 | if (inp->ip_inp.inp.inpcb_mtx == NULL) { |
t00fcxen | 8fca7ff | 2012-09-05 18:58:42 +0000 | [diff] [blame] | 2938 | SCTP_PRINTF("in_pcballoc: can't alloc mutex! so=%p\n", (void *)so); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2939 | #ifdef SCTP_MVRF |
| 2940 | SCTP_FREE(inp->m_vrf_ids, SCTP_M_MVRF); |
| 2941 | #endif |
| 2942 | SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark); |
| 2943 | so->so_pcb = NULL; |
| 2944 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp); |
t00fcxen | bc766ab | 2014-01-03 19:29:31 +0000 | [diff] [blame] | 2945 | SCTP_UNLOCK_EXC(SCTP_BASE_INFO(sctbinfo).ipi_lock); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2946 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM); |
| 2947 | return (ENOMEM); |
| 2948 | } |
t00fcxen | bc766ab | 2014-01-03 19:29:31 +0000 | [diff] [blame] | 2949 | #elif defined(APPLE_LION) || defined(APPLE_MOUNTAINLION) |
| 2950 | 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] | 2951 | #else |
t00fcxen | bc766ab | 2014-01-03 19:29:31 +0000 | [diff] [blame] | 2952 | 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] | 2953 | #endif |
| 2954 | #endif |
| 2955 | SCTP_INP_INFO_WLOCK(); |
| 2956 | SCTP_INP_LOCK_INIT(inp); |
| 2957 | #if defined(__FreeBSD__) |
| 2958 | INP_LOCK_INIT(&inp->ip_inp.inp, "inp", "sctpinp"); |
| 2959 | #endif |
| 2960 | SCTP_INP_READ_INIT(inp); |
| 2961 | SCTP_ASOC_CREATE_LOCK_INIT(inp); |
| 2962 | /* lock the new ep */ |
| 2963 | SCTP_INP_WLOCK(inp); |
| 2964 | |
| 2965 | /* add it to the info area */ |
| 2966 | LIST_INSERT_HEAD(&SCTP_BASE_INFO(listhead), inp, sctp_list); |
| 2967 | #if defined(__APPLE__) |
t00fcxen | bc766ab | 2014-01-03 19:29:31 +0000 | [diff] [blame] | 2968 | inp->ip_inp.inp.inp_pcbinfo = &SCTP_BASE_INFO(sctbinfo); |
| 2969 | #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) || defined(APPLE_LION) || defined(APPLE_MOUNTAINLION) |
| 2970 | LIST_INSERT_HEAD(SCTP_BASE_INFO(sctbinfo).listhead, &inp->ip_inp.inp, inp_list); |
| 2971 | #else |
| 2972 | LIST_INSERT_HEAD(SCTP_BASE_INFO(sctbinfo).ipi_listhead, &inp->ip_inp.inp, inp_list); |
| 2973 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2974 | #endif |
| 2975 | SCTP_INP_INFO_WUNLOCK(); |
| 2976 | |
| 2977 | TAILQ_INIT(&inp->read_queue); |
| 2978 | LIST_INIT(&inp->sctp_addr_list); |
| 2979 | |
| 2980 | LIST_INIT(&inp->sctp_asoc_list); |
| 2981 | |
| 2982 | #ifdef SCTP_TRACK_FREED_ASOCS |
| 2983 | /* TEMP CODE */ |
| 2984 | LIST_INIT(&inp->sctp_asoc_free_list); |
| 2985 | #endif |
| 2986 | /* Init the timer structure for signature change */ |
| 2987 | SCTP_OS_TIMER_INIT(&inp->sctp_ep.signature_change.timer); |
| 2988 | inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NEWCOOKIE; |
| 2989 | |
| 2990 | /* now init the actual endpoint default data */ |
| 2991 | m = &inp->sctp_ep; |
| 2992 | |
| 2993 | /* setup the base timeout information */ |
Michael Tuexen | 25935e8 | 2020-03-28 22:55:28 +0100 | [diff] [blame] | 2994 | m->sctp_timeoutticks[SCTP_TIMER_SEND] = sctp_secs_to_ticks(SCTP_SEND_SEC); /* needed ? */ |
| 2995 | m->sctp_timeoutticks[SCTP_TIMER_INIT] = sctp_secs_to_ticks(SCTP_INIT_SEC); /* needed ? */ |
| 2996 | m->sctp_timeoutticks[SCTP_TIMER_RECV] = sctp_msecs_to_ticks(SCTP_BASE_SYSCTL(sctp_delayed_sack_time_default)); |
| 2997 | m->sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = sctp_msecs_to_ticks(SCTP_BASE_SYSCTL(sctp_heartbeat_interval_default)); |
| 2998 | m->sctp_timeoutticks[SCTP_TIMER_PMTU] = sctp_secs_to_ticks(SCTP_BASE_SYSCTL(sctp_pmtu_raise_time_default)); |
| 2999 | m->sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] = sctp_secs_to_ticks(SCTP_BASE_SYSCTL(sctp_shutdown_guard_time_default)); |
| 3000 | 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] | 3001 | /* all max/min max are in ms */ |
| 3002 | m->sctp_maxrto = SCTP_BASE_SYSCTL(sctp_rto_max_default); |
| 3003 | m->sctp_minrto = SCTP_BASE_SYSCTL(sctp_rto_min_default); |
| 3004 | m->initial_rto = SCTP_BASE_SYSCTL(sctp_rto_initial_default); |
| 3005 | m->initial_init_rto_max = SCTP_BASE_SYSCTL(sctp_init_rto_max_default); |
| 3006 | m->sctp_sack_freq = SCTP_BASE_SYSCTL(sctp_sack_freq_default); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3007 | m->max_init_times = SCTP_BASE_SYSCTL(sctp_init_rtx_max_default); |
| 3008 | m->max_send_times = SCTP_BASE_SYSCTL(sctp_assoc_rtx_max_default); |
| 3009 | m->def_net_failure = SCTP_BASE_SYSCTL(sctp_path_rtx_max_default); |
| 3010 | m->def_net_pf_threshold = SCTP_BASE_SYSCTL(sctp_path_pf_threshold); |
| 3011 | m->sctp_sws_sender = SCTP_SWS_SENDER_DEF; |
| 3012 | m->sctp_sws_receiver = SCTP_SWS_RECEIVER_DEF; |
| 3013 | m->max_burst = SCTP_BASE_SYSCTL(sctp_max_burst_default); |
| 3014 | m->fr_max_burst = SCTP_BASE_SYSCTL(sctp_fr_max_burst_default); |
| 3015 | |
| 3016 | m->sctp_default_cc_module = SCTP_BASE_SYSCTL(sctp_default_cc_module); |
| 3017 | m->sctp_default_ss_module = SCTP_BASE_SYSCTL(sctp_default_ss_module); |
t00fcxen | c45180a | 2013-07-05 10:17:17 +0000 | [diff] [blame] | 3018 | m->max_open_streams_intome = SCTP_BASE_SYSCTL(sctp_nr_incoming_streams_default); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3019 | /* number of streams to pre-open on a association */ |
| 3020 | m->pre_open_stream_count = SCTP_BASE_SYSCTL(sctp_nr_outgoing_streams_default); |
| 3021 | |
Michael Tuexen | de0630d | 2017-11-03 22:04:01 +0100 | [diff] [blame] | 3022 | m->default_mtu = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3023 | /* Add adaptation cookie */ |
t00fcxen | 3637720 | 2013-02-11 21:07:33 +0000 | [diff] [blame] | 3024 | m->adaptation_layer_indicator = 0; |
| 3025 | m->adaptation_layer_indicator_provided = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3026 | |
| 3027 | /* seed random number generator */ |
| 3028 | m->random_counter = 1; |
| 3029 | m->store_at = SCTP_SIGNATURE_SIZE; |
| 3030 | SCTP_READ_RANDOM(m->random_numbers, sizeof(m->random_numbers)); |
| 3031 | sctp_fill_random_store(m); |
| 3032 | |
| 3033 | /* Minimum cookie size */ |
| 3034 | m->size_of_a_cookie = (sizeof(struct sctp_init_msg) * 2) + |
| 3035 | sizeof(struct sctp_state_cookie); |
| 3036 | m->size_of_a_cookie += SCTP_SIGNATURE_SIZE; |
| 3037 | |
| 3038 | /* Setup the initial secret */ |
| 3039 | (void)SCTP_GETTIME_TIMEVAL(&time); |
| 3040 | m->time_of_secret_change = time.tv_sec; |
| 3041 | |
| 3042 | for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) { |
| 3043 | m->secret_key[0][i] = sctp_select_initial_TSN(m); |
| 3044 | } |
| 3045 | sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL); |
| 3046 | |
| 3047 | /* How long is a cookie good for ? */ |
Michael Tuexen | 25935e8 | 2020-03-28 22:55:28 +0100 | [diff] [blame] | 3048 | 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] | 3049 | /* |
| 3050 | * Initialize authentication parameters |
| 3051 | */ |
| 3052 | m->local_hmacs = sctp_default_supported_hmaclist(); |
| 3053 | m->local_auth_chunks = sctp_alloc_chunklist(); |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 3054 | if (inp->asconf_supported) { |
| 3055 | sctp_auth_add_chunk(SCTP_ASCONF, m->local_auth_chunks); |
| 3056 | sctp_auth_add_chunk(SCTP_ASCONF_ACK, m->local_auth_chunks); |
| 3057 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3058 | m->default_dscp = 0; |
| 3059 | #ifdef INET6 |
| 3060 | m->default_flowlabel = 0; |
| 3061 | #endif |
tuexen | 9d7b207 | 2011-11-20 16:35:17 +0000 | [diff] [blame] | 3062 | m->port = 0; /* encapsulation disabled by default */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3063 | LIST_INIT(&m->shared_keys); |
| 3064 | /* add default NULL key as key id 0 */ |
| 3065 | null_key = sctp_alloc_sharedkey(); |
| 3066 | sctp_insert_sharedkey(&m->shared_keys, null_key); |
| 3067 | SCTP_INP_WUNLOCK(inp); |
| 3068 | #ifdef SCTP_LOG_CLOSING |
| 3069 | sctp_log_closing(inp, NULL, 12); |
| 3070 | #endif |
| 3071 | return (error); |
| 3072 | } |
| 3073 | |
| 3074 | |
| 3075 | void |
| 3076 | sctp_move_pcb_and_assoc(struct sctp_inpcb *old_inp, struct sctp_inpcb *new_inp, |
| 3077 | struct sctp_tcb *stcb) |
| 3078 | { |
| 3079 | struct sctp_nets *net; |
| 3080 | uint16_t lport, rport; |
| 3081 | struct sctppcbhead *head; |
| 3082 | struct sctp_laddr *laddr, *oladdr; |
| 3083 | |
| 3084 | atomic_add_int(&stcb->asoc.refcnt, 1); |
| 3085 | SCTP_TCB_UNLOCK(stcb); |
| 3086 | SCTP_INP_INFO_WLOCK(); |
| 3087 | SCTP_INP_WLOCK(old_inp); |
| 3088 | SCTP_INP_WLOCK(new_inp); |
| 3089 | SCTP_TCB_LOCK(stcb); |
| 3090 | atomic_subtract_int(&stcb->asoc.refcnt, 1); |
| 3091 | |
| 3092 | new_inp->sctp_ep.time_of_secret_change = |
| 3093 | old_inp->sctp_ep.time_of_secret_change; |
| 3094 | memcpy(new_inp->sctp_ep.secret_key, old_inp->sctp_ep.secret_key, |
| 3095 | sizeof(old_inp->sctp_ep.secret_key)); |
| 3096 | new_inp->sctp_ep.current_secret_number = |
| 3097 | old_inp->sctp_ep.current_secret_number; |
| 3098 | new_inp->sctp_ep.last_secret_number = |
| 3099 | old_inp->sctp_ep.last_secret_number; |
| 3100 | new_inp->sctp_ep.size_of_a_cookie = old_inp->sctp_ep.size_of_a_cookie; |
| 3101 | |
| 3102 | /* make it so new data pours into the new socket */ |
| 3103 | stcb->sctp_socket = new_inp->sctp_socket; |
| 3104 | stcb->sctp_ep = new_inp; |
| 3105 | |
| 3106 | /* Copy the port across */ |
| 3107 | lport = new_inp->sctp_lport = old_inp->sctp_lport; |
| 3108 | rport = stcb->rport; |
| 3109 | /* Pull the tcb from the old association */ |
| 3110 | LIST_REMOVE(stcb, sctp_tcbhash); |
| 3111 | LIST_REMOVE(stcb, sctp_tcblist); |
| 3112 | if (stcb->asoc.in_asocid_hash) { |
| 3113 | LIST_REMOVE(stcb, sctp_tcbasocidhash); |
| 3114 | } |
| 3115 | /* Now insert the new_inp into the TCP connected hash */ |
| 3116 | head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR((lport | rport), SCTP_BASE_INFO(hashtcpmark))]; |
| 3117 | |
| 3118 | LIST_INSERT_HEAD(head, new_inp, sctp_hash); |
| 3119 | /* Its safe to access */ |
| 3120 | new_inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND; |
| 3121 | |
| 3122 | /* Now move the tcb into the endpoint list */ |
| 3123 | LIST_INSERT_HEAD(&new_inp->sctp_asoc_list, stcb, sctp_tcblist); |
| 3124 | /* |
| 3125 | * Question, do we even need to worry about the ep-hash since we |
| 3126 | * only have one connection? Probably not :> so lets get rid of it |
| 3127 | * and not suck up any kernel memory in that. |
| 3128 | */ |
| 3129 | if (stcb->asoc.in_asocid_hash) { |
| 3130 | struct sctpasochead *lhd; |
| 3131 | lhd = &new_inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(stcb->asoc.assoc_id, |
| 3132 | new_inp->hashasocidmark)]; |
| 3133 | LIST_INSERT_HEAD(lhd, stcb, sctp_tcbasocidhash); |
| 3134 | } |
| 3135 | /* Ok. Let's restart timer. */ |
| 3136 | TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { |
| 3137 | sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, new_inp, |
| 3138 | stcb, net); |
| 3139 | } |
| 3140 | |
| 3141 | SCTP_INP_INFO_WUNLOCK(); |
| 3142 | if (new_inp->sctp_tcbhash != NULL) { |
| 3143 | SCTP_HASH_FREE(new_inp->sctp_tcbhash, new_inp->sctp_hashmark); |
| 3144 | new_inp->sctp_tcbhash = NULL; |
| 3145 | } |
| 3146 | if ((new_inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) { |
| 3147 | /* Subset bound, so copy in the laddr list from the old_inp */ |
| 3148 | LIST_FOREACH(oladdr, &old_inp->sctp_addr_list, sctp_nxt_addr) { |
| 3149 | laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr); |
| 3150 | if (laddr == NULL) { |
| 3151 | /* |
| 3152 | * Gak, what can we do? This assoc is really |
| 3153 | * HOSED. We probably should send an abort |
| 3154 | * here. |
| 3155 | */ |
| 3156 | SCTPDBG(SCTP_DEBUG_PCB1, "Association hosed in TCP model, out of laddr memory\n"); |
| 3157 | continue; |
| 3158 | } |
| 3159 | SCTP_INCR_LADDR_COUNT(); |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 3160 | memset(laddr, 0, sizeof(*laddr)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3161 | (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time); |
| 3162 | laddr->ifa = oladdr->ifa; |
| 3163 | atomic_add_int(&laddr->ifa->refcount, 1); |
| 3164 | LIST_INSERT_HEAD(&new_inp->sctp_addr_list, laddr, |
| 3165 | sctp_nxt_addr); |
| 3166 | new_inp->laddr_count++; |
| 3167 | if (oladdr == stcb->asoc.last_used_address) { |
| 3168 | stcb->asoc.last_used_address = laddr; |
| 3169 | } |
| 3170 | } |
| 3171 | } |
| 3172 | /* Now any running timers need to be adjusted |
| 3173 | * since we really don't care if they are running |
| 3174 | * or not just blast in the new_inp into all of |
| 3175 | * them. |
| 3176 | */ |
| 3177 | |
| 3178 | stcb->asoc.dack_timer.ep = (void *)new_inp; |
| 3179 | stcb->asoc.asconf_timer.ep = (void *)new_inp; |
| 3180 | stcb->asoc.strreset_timer.ep = (void *)new_inp; |
| 3181 | stcb->asoc.shut_guard_timer.ep = (void *)new_inp; |
| 3182 | stcb->asoc.autoclose_timer.ep = (void *)new_inp; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3183 | stcb->asoc.delete_prim_timer.ep = (void *)new_inp; |
| 3184 | /* now what about the nets? */ |
| 3185 | TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { |
| 3186 | net->pmtu_timer.ep = (void *)new_inp; |
| 3187 | net->hb_timer.ep = (void *)new_inp; |
| 3188 | net->rxt_timer.ep = (void *)new_inp; |
| 3189 | } |
| 3190 | SCTP_INP_WUNLOCK(new_inp); |
| 3191 | SCTP_INP_WUNLOCK(old_inp); |
| 3192 | } |
| 3193 | |
Michael Tuexen | 71a83fb | 2016-02-16 21:52:58 +0100 | [diff] [blame] | 3194 | /* |
| 3195 | * insert an laddr entry with the given ifa for the desired list |
| 3196 | */ |
| 3197 | static int |
| 3198 | sctp_insert_laddr(struct sctpladdr *list, struct sctp_ifa *ifa, uint32_t act) |
| 3199 | { |
| 3200 | struct sctp_laddr *laddr; |
| 3201 | |
| 3202 | laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr); |
| 3203 | if (laddr == NULL) { |
| 3204 | /* out of memory? */ |
| 3205 | SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 3206 | return (EINVAL); |
| 3207 | } |
| 3208 | SCTP_INCR_LADDR_COUNT(); |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 3209 | memset(laddr, 0, sizeof(*laddr)); |
Michael Tuexen | 71a83fb | 2016-02-16 21:52:58 +0100 | [diff] [blame] | 3210 | (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time); |
| 3211 | laddr->ifa = ifa; |
| 3212 | laddr->action = act; |
| 3213 | atomic_add_int(&ifa->refcount, 1); |
| 3214 | /* insert it */ |
| 3215 | LIST_INSERT_HEAD(list, laddr, sctp_nxt_addr); |
| 3216 | |
| 3217 | return (0); |
| 3218 | } |
| 3219 | |
| 3220 | /* |
| 3221 | * Remove an laddr entry from the local address list (on an assoc) |
| 3222 | */ |
| 3223 | static void |
| 3224 | sctp_remove_laddr(struct sctp_laddr *laddr) |
| 3225 | { |
| 3226 | |
| 3227 | /* remove from the list */ |
| 3228 | LIST_REMOVE(laddr, sctp_nxt_addr); |
| 3229 | sctp_free_ifa(laddr->ifa); |
| 3230 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), laddr); |
| 3231 | SCTP_DECR_LADDR_COUNT(); |
| 3232 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3233 | |
| 3234 | #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Userspace__)) |
| 3235 | /* |
| 3236 | * Don't know why, but without this there is an unknown reference when |
| 3237 | * compiling NetBSD... hmm |
| 3238 | */ |
| 3239 | extern void in6_sin6_2_sin(struct sockaddr_in *, struct sockaddr_in6 *sin6); |
| 3240 | #endif |
| 3241 | |
| 3242 | |
| 3243 | /* sctp_ifap is used to bypass normal local address validation checks */ |
| 3244 | int |
| 3245 | #if defined(__FreeBSD__) && __FreeBSD_version >= 500000 |
| 3246 | sctp_inpcb_bind(struct socket *so, struct sockaddr *addr, |
t00fcxen | c24796c | 2014-05-06 15:39:13 +0000 | [diff] [blame] | 3247 | struct sctp_ifa *sctp_ifap, struct thread *p) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3248 | #elif defined(__Windows__) |
| 3249 | sctp_inpcb_bind(struct socket *so, struct sockaddr *addr, |
t00fcxen | c24796c | 2014-05-06 15:39:13 +0000 | [diff] [blame] | 3250 | struct sctp_ifa *sctp_ifap, PKTHREAD p) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3251 | #else |
| 3252 | sctp_inpcb_bind(struct socket *so, struct sockaddr *addr, |
t00fcxen | c24796c | 2014-05-06 15:39:13 +0000 | [diff] [blame] | 3253 | struct sctp_ifa *sctp_ifap, struct proc *p) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3254 | #endif |
| 3255 | { |
| 3256 | /* bind a ep to a socket address */ |
| 3257 | struct sctppcbhead *head; |
| 3258 | struct sctp_inpcb *inp, *inp_tmp; |
Michael Tuexen | 2e04f87 | 2019-08-05 13:56:43 +0200 | [diff] [blame] | 3259 | #if defined(__FreeBSD__) || defined(__APPLE__) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3260 | struct inpcb *ip_inp; |
t00fcxen | aff14b6 | 2013-11-02 18:02:02 +0000 | [diff] [blame] | 3261 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3262 | int port_reuse_active = 0; |
| 3263 | int bindall; |
| 3264 | #ifdef SCTP_MVRF |
| 3265 | int i; |
| 3266 | #endif |
| 3267 | uint16_t lport; |
| 3268 | int error; |
| 3269 | uint32_t vrf_id; |
| 3270 | |
| 3271 | lport = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3272 | bindall = 1; |
| 3273 | inp = (struct sctp_inpcb *)so->so_pcb; |
Michael Tuexen | 2e04f87 | 2019-08-05 13:56:43 +0200 | [diff] [blame] | 3274 | #if defined(__FreeBSD__) || defined(__APPLE__) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3275 | ip_inp = (struct inpcb *)so->so_pcb; |
t00fcxen | aff14b6 | 2013-11-02 18:02:02 +0000 | [diff] [blame] | 3276 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3277 | #ifdef SCTP_DEBUG |
| 3278 | if (addr) { |
t00fcxen | 94dfd1e | 2012-12-27 08:14:10 +0000 | [diff] [blame] | 3279 | SCTPDBG(SCTP_DEBUG_PCB1, "Bind called port: %d\n", |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3280 | ntohs(((struct sockaddr_in *)addr)->sin_port)); |
t00fcxen | 94dfd1e | 2012-12-27 08:14:10 +0000 | [diff] [blame] | 3281 | SCTPDBG(SCTP_DEBUG_PCB1, "Addr: "); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3282 | SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr); |
| 3283 | } |
| 3284 | #endif |
| 3285 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) { |
| 3286 | /* already did a bind, subsequent binds NOT allowed ! */ |
| 3287 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 3288 | return (EINVAL); |
| 3289 | } |
| 3290 | #if defined(__FreeBSD__) && __FreeBSD_version >= 500000 |
| 3291 | #ifdef INVARIANTS |
| 3292 | if (p == NULL) |
| 3293 | panic("null proc/thread"); |
| 3294 | #endif |
| 3295 | #endif |
| 3296 | if (addr != NULL) { |
| 3297 | switch (addr->sa_family) { |
| 3298 | #ifdef INET |
| 3299 | case AF_INET: |
| 3300 | { |
| 3301 | struct sockaddr_in *sin; |
| 3302 | |
| 3303 | /* IPV6_V6ONLY socket? */ |
Michael Tuexen | 72e5cec | 2019-08-05 13:21:02 +0200 | [diff] [blame] | 3304 | if (SCTP_IPV6_V6ONLY(inp)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3305 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 3306 | return (EINVAL); |
| 3307 | } |
t00fcxen | a8a26f6 | 2012-09-12 19:24:54 +0000 | [diff] [blame] | 3308 | #ifdef HAVE_SA_LEN |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3309 | if (addr->sa_len != sizeof(*sin)) { |
| 3310 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 3311 | return (EINVAL); |
| 3312 | } |
| 3313 | #endif |
| 3314 | |
| 3315 | sin = (struct sockaddr_in *)addr; |
| 3316 | lport = sin->sin_port; |
| 3317 | #if defined(__FreeBSD__) && __FreeBSD_version >= 800000 |
t00fcxen | c24796c | 2014-05-06 15:39:13 +0000 | [diff] [blame] | 3318 | /* |
| 3319 | * For LOOPBACK the prison_local_ip4() call will transmute the ip address |
| 3320 | * to the proper value. |
| 3321 | */ |
| 3322 | if (p && (error = prison_local_ip4(p->td_ucred, &sin->sin_addr)) != 0) { |
| 3323 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); |
| 3324 | return (error); |
| 3325 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3326 | #endif |
| 3327 | if (sin->sin_addr.s_addr != INADDR_ANY) { |
| 3328 | bindall = 0; |
| 3329 | } |
| 3330 | break; |
| 3331 | } |
| 3332 | #endif |
| 3333 | #ifdef INET6 |
| 3334 | case AF_INET6: |
| 3335 | { |
| 3336 | /* Only for pure IPv6 Address. (No IPv4 Mapped!) */ |
| 3337 | struct sockaddr_in6 *sin6; |
| 3338 | |
| 3339 | sin6 = (struct sockaddr_in6 *)addr; |
| 3340 | |
t00fcxen | a8a26f6 | 2012-09-12 19:24:54 +0000 | [diff] [blame] | 3341 | #ifdef HAVE_SA_LEN |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3342 | if (addr->sa_len != sizeof(*sin6)) { |
| 3343 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 3344 | return (EINVAL); |
| 3345 | } |
| 3346 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3347 | lport = sin6->sin6_port; |
| 3348 | #if defined(__FreeBSD__) && __FreeBSD_version >= 800000 |
t00fcxen | c24796c | 2014-05-06 15:39:13 +0000 | [diff] [blame] | 3349 | /* |
| 3350 | * For LOOPBACK the prison_local_ip6() call will transmute the ipv6 address |
| 3351 | * to the proper value. |
| 3352 | */ |
| 3353 | if (p && (error = prison_local_ip6(p->td_ucred, &sin6->sin6_addr, |
Michael Tuexen | 72e5cec | 2019-08-05 13:21:02 +0200 | [diff] [blame] | 3354 | (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) { |
t00fcxen | c24796c | 2014-05-06 15:39:13 +0000 | [diff] [blame] | 3355 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); |
| 3356 | return (error); |
| 3357 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3358 | #endif |
| 3359 | if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { |
| 3360 | bindall = 0; |
| 3361 | #ifdef SCTP_EMBEDDED_V6_SCOPE |
| 3362 | /* KAME hack: embed scopeid */ |
| 3363 | #if defined(SCTP_KAME) |
| 3364 | if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) { |
| 3365 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 3366 | return (EINVAL); |
| 3367 | } |
tuexen | e385159 | 2012-06-01 08:31:45 +0000 | [diff] [blame] | 3368 | #elif defined(__APPLE__) |
tuexen | 9a218b1 | 2012-08-04 21:17:58 +0000 | [diff] [blame] | 3369 | #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) |
| 3370 | if (in6_embedscope(&sin6->sin6_addr, sin6, ip_inp, NULL) != 0) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3371 | #else |
tuexen | 9a218b1 | 2012-08-04 21:17:58 +0000 | [diff] [blame] | 3372 | if (in6_embedscope(&sin6->sin6_addr, sin6, ip_inp, NULL, NULL) != 0) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3373 | #endif |
| 3374 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 3375 | return (EINVAL); |
| 3376 | } |
| 3377 | #elif defined(__FreeBSD__) |
| 3378 | error = scope6_check_id(sin6, MODULE_GLOBAL(ip6_use_defzone)); |
| 3379 | if (error != 0) { |
| 3380 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); |
| 3381 | return (error); |
| 3382 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3383 | #else |
| 3384 | if (in6_embedscope(&sin6->sin6_addr, sin6) != 0) { |
| 3385 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 3386 | return (EINVAL); |
| 3387 | } |
| 3388 | #endif |
| 3389 | #endif /* SCTP_EMBEDDED_V6_SCOPE */ |
| 3390 | } |
| 3391 | #ifndef SCOPEDROUTING |
| 3392 | /* this must be cleared for ifa_ifwithaddr() */ |
| 3393 | sin6->sin6_scope_id = 0; |
| 3394 | #endif /* SCOPEDROUTING */ |
| 3395 | break; |
| 3396 | } |
| 3397 | #endif |
tuexen | 09a676e | 2012-07-08 21:03:18 +0000 | [diff] [blame] | 3398 | #if defined(__Userspace__) |
| 3399 | case AF_CONN: |
| 3400 | { |
| 3401 | struct sockaddr_conn *sconn; |
t00fcxen | efb47e7 | 2012-09-09 08:16:51 +0000 | [diff] [blame] | 3402 | |
t00fcxen | a8a26f6 | 2012-09-12 19:24:54 +0000 | [diff] [blame] | 3403 | #ifdef HAVE_SA_LEN |
tuexen | 09a676e | 2012-07-08 21:03:18 +0000 | [diff] [blame] | 3404 | if (addr->sa_len != sizeof(struct sockaddr_conn)) { |
| 3405 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 3406 | return (EINVAL); |
| 3407 | } |
| 3408 | #endif |
| 3409 | sconn = (struct sockaddr_conn *)addr; |
| 3410 | lport = sconn->sconn_port; |
| 3411 | if (sconn->sconn_addr != NULL) { |
| 3412 | bindall = 0; |
| 3413 | } |
| 3414 | break; |
| 3415 | } |
| 3416 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3417 | default: |
| 3418 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EAFNOSUPPORT); |
| 3419 | return (EAFNOSUPPORT); |
| 3420 | } |
| 3421 | } |
| 3422 | SCTP_INP_INFO_WLOCK(); |
| 3423 | SCTP_INP_WLOCK(inp); |
| 3424 | /* 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] | 3425 | vrf_id = inp->def_vrf_id; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3426 | |
| 3427 | /* increase our count due to the unlock we do */ |
| 3428 | SCTP_INP_INCR_REF(inp); |
| 3429 | if (lport) { |
| 3430 | /* |
t00fcxen | efb47e7 | 2012-09-09 08:16:51 +0000 | [diff] [blame] | 3431 | * 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] | 3432 | * already has this one bound. |
| 3433 | */ |
| 3434 | /* got to be root to get at low ports */ |
| 3435 | #if !defined(__Windows__) |
| 3436 | if (ntohs(lport) < IPPORT_RESERVED) { |
Michael Tuexen | 4c4cbd9 | 2017-09-02 21:38:23 +0200 | [diff] [blame] | 3437 | if ((p != NULL) && ((error = |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3438 | #ifdef __FreeBSD__ |
| 3439 | #if __FreeBSD_version > 602000 |
| 3440 | priv_check(p, PRIV_NETINET_RESERVEDPORT) |
| 3441 | #elif __FreeBSD_version >= 500000 |
| 3442 | suser_cred(p->td_ucred, 0) |
| 3443 | #else |
| 3444 | suser(p) |
| 3445 | #endif |
| 3446 | #elif defined(__APPLE__) |
| 3447 | suser(p->p_ucred, &p->p_acflag) |
| 3448 | #elif defined(__Userspace__) /* must be true to use raw socket */ |
t00fcxen | c24796c | 2014-05-06 15:39:13 +0000 | [diff] [blame] | 3449 | 1 |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3450 | #else |
| 3451 | suser(p, 0) |
| 3452 | #endif |
Michael Tuexen | 4c4cbd9 | 2017-09-02 21:38:23 +0200 | [diff] [blame] | 3453 | ) != 0)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3454 | SCTP_INP_DECR_REF(inp); |
| 3455 | SCTP_INP_WUNLOCK(inp); |
| 3456 | SCTP_INP_INFO_WUNLOCK(); |
| 3457 | return (error); |
| 3458 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3459 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3460 | #endif /* __Windows__ */ |
| 3461 | SCTP_INP_WUNLOCK(inp); |
| 3462 | if (bindall) { |
| 3463 | #ifdef SCTP_MVRF |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 3464 | for (i = 0; i < inp->num_vrfs; i++) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3465 | vrf_id = inp->m_vrf_ids[i]; |
| 3466 | #else |
| 3467 | vrf_id = inp->def_vrf_id; |
| 3468 | #endif |
| 3469 | inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id); |
| 3470 | if (inp_tmp != NULL) { |
| 3471 | /* |
| 3472 | * lock guy returned and lower count |
| 3473 | * note that we are not bound so |
| 3474 | * inp_tmp should NEVER be inp. And |
| 3475 | * it is this inp (inp_tmp) that gets |
| 3476 | * the reference bump, so we must |
| 3477 | * lower it. |
| 3478 | */ |
| 3479 | SCTP_INP_DECR_REF(inp_tmp); |
| 3480 | /* unlock info */ |
| 3481 | if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) && |
| 3482 | (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) { |
| 3483 | /* Ok, must be one-2-one and allowing port re-use */ |
| 3484 | port_reuse_active = 1; |
| 3485 | goto continue_anyway; |
| 3486 | } |
| 3487 | SCTP_INP_DECR_REF(inp); |
| 3488 | SCTP_INP_INFO_WUNLOCK(); |
| 3489 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE); |
| 3490 | return (EADDRINUSE); |
| 3491 | } |
| 3492 | #ifdef SCTP_MVRF |
| 3493 | } |
| 3494 | #endif |
| 3495 | } else { |
| 3496 | inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id); |
| 3497 | if (inp_tmp != NULL) { |
| 3498 | /* |
| 3499 | * lock guy returned and lower count note |
| 3500 | * that we are not bound so inp_tmp should |
| 3501 | * NEVER be inp. And it is this inp (inp_tmp) |
| 3502 | * that gets the reference bump, so we must |
| 3503 | * lower it. |
| 3504 | */ |
| 3505 | SCTP_INP_DECR_REF(inp_tmp); |
| 3506 | /* unlock info */ |
| 3507 | if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) && |
| 3508 | (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) { |
| 3509 | /* Ok, must be one-2-one and allowing port re-use */ |
| 3510 | port_reuse_active = 1; |
| 3511 | goto continue_anyway; |
| 3512 | } |
| 3513 | SCTP_INP_DECR_REF(inp); |
| 3514 | SCTP_INP_INFO_WUNLOCK(); |
| 3515 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE); |
| 3516 | return (EADDRINUSE); |
| 3517 | } |
| 3518 | } |
| 3519 | continue_anyway: |
| 3520 | SCTP_INP_WLOCK(inp); |
| 3521 | if (bindall) { |
| 3522 | /* verify that no lport is not used by a singleton */ |
t00fcxen | efb47e7 | 2012-09-09 08:16:51 +0000 | [diff] [blame] | 3523 | if ((port_reuse_active == 0) && |
| 3524 | (inp_tmp = sctp_isport_inuse(inp, lport, vrf_id))) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3525 | /* Sorry someone already has this one bound */ |
| 3526 | if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) && |
| 3527 | (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) { |
| 3528 | port_reuse_active = 1; |
| 3529 | } else { |
| 3530 | SCTP_INP_DECR_REF(inp); |
| 3531 | SCTP_INP_WUNLOCK(inp); |
| 3532 | SCTP_INP_INFO_WUNLOCK(); |
| 3533 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE); |
| 3534 | return (EADDRINUSE); |
| 3535 | } |
| 3536 | } |
| 3537 | } |
| 3538 | } else { |
| 3539 | uint16_t first, last, candidate; |
t00fcxen | c24796c | 2014-05-06 15:39:13 +0000 | [diff] [blame] | 3540 | uint16_t count; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3541 | int done; |
| 3542 | |
| 3543 | #if defined(__Windows__) |
| 3544 | first = 1; |
| 3545 | last = 0xffff; |
| 3546 | #else |
| 3547 | #if defined(__Userspace__) |
t00fcxen | c24796c | 2014-05-06 15:39:13 +0000 | [diff] [blame] | 3548 | /* TODO ensure uid is 0, etc... */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3549 | #elif defined(__FreeBSD__) || defined(__APPLE__) |
t00fcxen | c24796c | 2014-05-06 15:39:13 +0000 | [diff] [blame] | 3550 | if (ip_inp->inp_flags & INP_HIGHPORT) { |
| 3551 | first = MODULE_GLOBAL(ipport_hifirstauto); |
| 3552 | last = MODULE_GLOBAL(ipport_hilastauto); |
| 3553 | } else if (ip_inp->inp_flags & INP_LOWPORT) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3554 | if (p && (error = |
| 3555 | #ifdef __FreeBSD__ |
| 3556 | #if __FreeBSD_version > 602000 |
| 3557 | priv_check(p, PRIV_NETINET_RESERVEDPORT) |
| 3558 | #elif __FreeBSD_version >= 500000 |
| 3559 | suser_cred(p->td_ucred, 0) |
| 3560 | #else |
| 3561 | suser(p) |
| 3562 | #endif |
| 3563 | #elif defined(__APPLE__) |
| 3564 | suser(p->p_ucred, &p->p_acflag) |
| 3565 | #else |
| 3566 | suser(p, 0) |
| 3567 | #endif |
| 3568 | )) { |
| 3569 | SCTP_INP_DECR_REF(inp); |
| 3570 | SCTP_INP_WUNLOCK(inp); |
| 3571 | SCTP_INP_INFO_WUNLOCK(); |
| 3572 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); |
| 3573 | return (error); |
| 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 { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3578 | #endif |
t00fcxen | 8dd95b8 | 2014-07-11 20:41:28 +0000 | [diff] [blame] | 3579 | first = MODULE_GLOBAL(ipport_firstauto); |
| 3580 | last = MODULE_GLOBAL(ipport_lastauto); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3581 | #if defined(__FreeBSD__) || defined(__APPLE__) |
t00fcxen | c24796c | 2014-05-06 15:39:13 +0000 | [diff] [blame] | 3582 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3583 | #endif |
| 3584 | #endif /* __Windows__ */ |
| 3585 | if (first > last) { |
| 3586 | uint16_t temp; |
| 3587 | |
| 3588 | temp = first; |
| 3589 | first = last; |
| 3590 | last = temp; |
| 3591 | } |
| 3592 | count = last - first + 1; /* number of candidates */ |
| 3593 | candidate = first + sctp_select_initial_TSN(&inp->sctp_ep) % (count); |
| 3594 | |
| 3595 | done = 0; |
| 3596 | while (!done) { |
| 3597 | #ifdef SCTP_MVRF |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 3598 | for (i = 0; i < inp->num_vrfs; i++) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3599 | if (sctp_isport_inuse(inp, htons(candidate), inp->m_vrf_ids[i]) != NULL) { |
| 3600 | break; |
| 3601 | } |
| 3602 | } |
| 3603 | if (i == inp->num_vrfs) { |
| 3604 | done = 1; |
| 3605 | } |
| 3606 | #else |
| 3607 | if (sctp_isport_inuse(inp, htons(candidate), inp->def_vrf_id) == NULL) { |
| 3608 | done = 1; |
| 3609 | } |
| 3610 | #endif |
| 3611 | if (!done) { |
| 3612 | if (--count == 0) { |
| 3613 | SCTP_INP_DECR_REF(inp); |
| 3614 | SCTP_INP_WUNLOCK(inp); |
| 3615 | SCTP_INP_INFO_WUNLOCK(); |
| 3616 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE); |
| 3617 | return (EADDRINUSE); |
| 3618 | } |
| 3619 | if (candidate == last) |
| 3620 | candidate = first; |
| 3621 | else |
| 3622 | candidate = candidate + 1; |
| 3623 | } |
| 3624 | } |
| 3625 | lport = htons(candidate); |
| 3626 | } |
| 3627 | SCTP_INP_DECR_REF(inp); |
| 3628 | if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | |
| 3629 | SCTP_PCB_FLAGS_SOCKET_ALLGONE)) { |
| 3630 | /* |
| 3631 | * this really should not happen. The guy did a non-blocking |
| 3632 | * bind and then did a close at the same time. |
| 3633 | */ |
| 3634 | SCTP_INP_WUNLOCK(inp); |
| 3635 | SCTP_INP_INFO_WUNLOCK(); |
| 3636 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 3637 | return (EINVAL); |
| 3638 | } |
| 3639 | /* ok we look clear to give out this port, so lets setup the binding */ |
| 3640 | if (bindall) { |
| 3641 | /* binding to all addresses, so just set in the proper flags */ |
| 3642 | inp->sctp_flags |= SCTP_PCB_FLAGS_BOUNDALL; |
| 3643 | /* set the automatic addr changes from kernel flag */ |
| 3644 | if (SCTP_BASE_SYSCTL(sctp_auto_asconf) == 0) { |
| 3645 | sctp_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF); |
| 3646 | sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF); |
| 3647 | } else { |
| 3648 | sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF); |
| 3649 | sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF); |
| 3650 | } |
| 3651 | if (SCTP_BASE_SYSCTL(sctp_multiple_asconfs) == 0) { |
| 3652 | sctp_feature_off(inp, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS); |
| 3653 | } else { |
| 3654 | sctp_feature_on(inp, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS); |
| 3655 | } |
| 3656 | /* set the automatic mobility_base from kernel |
| 3657 | flag (by micchie) |
| 3658 | */ |
| 3659 | if (SCTP_BASE_SYSCTL(sctp_mobility_base) == 0) { |
| 3660 | sctp_mobility_feature_off(inp, SCTP_MOBILITY_BASE); |
| 3661 | sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED); |
t00fcxen | efb47e7 | 2012-09-09 08:16:51 +0000 | [diff] [blame] | 3662 | } else { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3663 | sctp_mobility_feature_on(inp, SCTP_MOBILITY_BASE); |
| 3664 | sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED); |
| 3665 | } |
| 3666 | /* set the automatic mobility_fasthandoff from kernel |
| 3667 | flag (by micchie) |
| 3668 | */ |
| 3669 | if (SCTP_BASE_SYSCTL(sctp_mobility_fasthandoff) == 0) { |
| 3670 | sctp_mobility_feature_off(inp, SCTP_MOBILITY_FASTHANDOFF); |
| 3671 | sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED); |
t00fcxen | efb47e7 | 2012-09-09 08:16:51 +0000 | [diff] [blame] | 3672 | } else { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3673 | sctp_mobility_feature_on(inp, SCTP_MOBILITY_FASTHANDOFF); |
| 3674 | sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED); |
| 3675 | } |
| 3676 | } else { |
| 3677 | /* |
| 3678 | * bind specific, make sure flags is off and add a new |
| 3679 | * address structure to the sctp_addr_list inside the ep |
| 3680 | * structure. |
| 3681 | * |
| 3682 | * We will need to allocate one and insert it at the head. The |
| 3683 | * socketopt call can just insert new addresses in there as |
| 3684 | * well. It will also have to do the embed scope kame hack |
| 3685 | * too (before adding). |
| 3686 | */ |
| 3687 | struct sctp_ifa *ifa; |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 3688 | union sctp_sockstore store; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3689 | |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 3690 | memset(&store, 0, sizeof(store)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3691 | switch (addr->sa_family) { |
tuexen | 310f1bc | 2012-07-15 11:20:56 +0000 | [diff] [blame] | 3692 | #ifdef INET |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3693 | case AF_INET: |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 3694 | memcpy(&store.sin, addr, sizeof(struct sockaddr_in)); |
| 3695 | store.sin.sin_port = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3696 | break; |
tuexen | 310f1bc | 2012-07-15 11:20:56 +0000 | [diff] [blame] | 3697 | #endif |
| 3698 | #ifdef INET6 |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3699 | case AF_INET6: |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 3700 | memcpy(&store.sin6, addr, sizeof(struct sockaddr_in6)); |
| 3701 | store.sin6.sin6_port = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3702 | break; |
tuexen | 310f1bc | 2012-07-15 11:20:56 +0000 | [diff] [blame] | 3703 | #endif |
tuexen | cc2b142 | 2012-07-10 21:50:32 +0000 | [diff] [blame] | 3704 | #if defined(__Userspace__) |
| 3705 | case AF_CONN: |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 3706 | memcpy(&store.sconn, addr, sizeof(struct sockaddr_conn)); |
| 3707 | store.sconn.sconn_port = 0; |
tuexen | cc2b142 | 2012-07-10 21:50:32 +0000 | [diff] [blame] | 3708 | break; |
tuexen | cc2b142 | 2012-07-10 21:50:32 +0000 | [diff] [blame] | 3709 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3710 | default: |
| 3711 | break; |
| 3712 | } |
| 3713 | /* |
| 3714 | * first find the interface with the bound address need to |
| 3715 | * zero out the port to find the address! yuck! can't do |
| 3716 | * this earlier since need port for sctp_pcb_findep() |
| 3717 | */ |
t00fcxen | efb47e7 | 2012-09-09 08:16:51 +0000 | [diff] [blame] | 3718 | if (sctp_ifap != NULL) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3719 | ifa = sctp_ifap; |
t00fcxen | efb47e7 | 2012-09-09 08:16:51 +0000 | [diff] [blame] | 3720 | } else { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3721 | /* Note for BSD we hit here always other |
| 3722 | * O/S's will pass things in via the |
Michael Tuexen | 3b4263d | 2020-06-06 19:52:50 +0200 | [diff] [blame^] | 3723 | * sctp_ifap argument. |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3724 | */ |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 3725 | ifa = sctp_find_ifa_by_addr(&store.sa, |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3726 | vrf_id, SCTP_ADDR_NOT_LOCKED); |
| 3727 | } |
| 3728 | if (ifa == NULL) { |
| 3729 | /* Can't find an interface with that address */ |
| 3730 | SCTP_INP_WUNLOCK(inp); |
| 3731 | SCTP_INP_INFO_WUNLOCK(); |
| 3732 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRNOTAVAIL); |
| 3733 | return (EADDRNOTAVAIL); |
| 3734 | } |
| 3735 | #ifdef INET6 |
| 3736 | if (addr->sa_family == AF_INET6) { |
| 3737 | /* GAK, more FIXME IFA lock? */ |
| 3738 | if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) { |
| 3739 | /* Can't bind a non-existent addr. */ |
| 3740 | SCTP_INP_WUNLOCK(inp); |
| 3741 | SCTP_INP_INFO_WUNLOCK(); |
| 3742 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 3743 | return (EINVAL); |
| 3744 | } |
| 3745 | } |
| 3746 | #endif |
| 3747 | /* we're not bound all */ |
| 3748 | inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUNDALL; |
| 3749 | /* allow bindx() to send ASCONF's for binding changes */ |
| 3750 | sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF); |
| 3751 | /* clear automatic addr changes from kernel flag */ |
| 3752 | sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF); |
| 3753 | |
| 3754 | /* add this address to the endpoint list */ |
| 3755 | error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, 0); |
| 3756 | if (error != 0) { |
| 3757 | SCTP_INP_WUNLOCK(inp); |
| 3758 | SCTP_INP_INFO_WUNLOCK(); |
| 3759 | return (error); |
| 3760 | } |
| 3761 | inp->laddr_count++; |
| 3762 | } |
| 3763 | /* find the bucket */ |
tuexen | 98456cf | 2012-04-19 15:37:07 +0000 | [diff] [blame] | 3764 | if (port_reuse_active) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3765 | /* Put it into tcp 1-2-1 hash */ |
| 3766 | head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashtcpmark))]; |
| 3767 | inp->sctp_flags |= SCTP_PCB_FLAGS_IN_TCPPOOL; |
t00fcxen | c24796c | 2014-05-06 15:39:13 +0000 | [diff] [blame] | 3768 | } else { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3769 | head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashmark))]; |
| 3770 | } |
| 3771 | /* put it in the bucket */ |
| 3772 | LIST_INSERT_HEAD(head, inp, sctp_hash); |
| 3773 | 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] | 3774 | (void *)head, ntohs(lport), port_reuse_active); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3775 | /* set in the port */ |
| 3776 | inp->sctp_lport = lport; |
| 3777 | |
| 3778 | /* turn off just the unbound flag */ |
| 3779 | inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND; |
| 3780 | SCTP_INP_WUNLOCK(inp); |
| 3781 | SCTP_INP_INFO_WUNLOCK(); |
| 3782 | return (0); |
| 3783 | } |
| 3784 | |
| 3785 | |
| 3786 | static void |
| 3787 | sctp_iterator_inp_being_freed(struct sctp_inpcb *inp) |
| 3788 | { |
| 3789 | struct sctp_iterator *it, *nit; |
| 3790 | |
| 3791 | /* |
| 3792 | * We enter with the only the ITERATOR_LOCK in place and a write |
| 3793 | * lock on the inp_info stuff. |
| 3794 | */ |
| 3795 | it = sctp_it_ctl.cur_it; |
| 3796 | #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 |
| 3797 | if (it && (it->vn != curvnet)) { |
| 3798 | /* Its not looking at our VNET */ |
| 3799 | return; |
| 3800 | } |
| 3801 | #endif |
| 3802 | if (it && (it->inp == inp)) { |
| 3803 | /* |
| 3804 | * This is tricky and we hold the iterator lock, |
| 3805 | * but when it returns and gets the lock (when we |
| 3806 | * release it) the iterator will try to operate on |
| 3807 | * inp. We need to stop that from happening. But |
| 3808 | * of course the iterator has a reference on the |
| 3809 | * stcb and inp. We can mark it and it will stop. |
| 3810 | * |
| 3811 | * If its a single iterator situation, we |
| 3812 | * set the end iterator flag. Otherwise |
| 3813 | * we set the iterator to go to the next inp. |
| 3814 | * |
| 3815 | */ |
| 3816 | if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) { |
| 3817 | sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_IT; |
| 3818 | } else { |
| 3819 | sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_INP; |
| 3820 | } |
| 3821 | } |
| 3822 | /* Now go through and remove any single reference to |
| 3823 | * our inp that may be still pending on the list |
| 3824 | */ |
| 3825 | SCTP_IPI_ITERATOR_WQ_LOCK(); |
| 3826 | TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) { |
| 3827 | #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 |
| 3828 | if (it->vn != curvnet) { |
| 3829 | continue; |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 3830 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3831 | #endif |
| 3832 | if (it->inp == inp) { |
| 3833 | /* This one points to me is it inp specific? */ |
| 3834 | if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) { |
| 3835 | /* Remove and free this one */ |
| 3836 | TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, |
| 3837 | it, sctp_nxt_itr); |
| 3838 | if (it->function_atend != NULL) { |
| 3839 | (*it->function_atend) (it->pointer, it->val); |
| 3840 | } |
| 3841 | SCTP_FREE(it, SCTP_M_ITER); |
| 3842 | } else { |
| 3843 | it->inp = LIST_NEXT(it->inp, sctp_list); |
| 3844 | if (it->inp) { |
| 3845 | SCTP_INP_INCR_REF(it->inp); |
| 3846 | } |
| 3847 | } |
| 3848 | /* When its put in the refcnt is incremented so decr it */ |
| 3849 | SCTP_INP_DECR_REF(inp); |
| 3850 | } |
| 3851 | } |
| 3852 | SCTP_IPI_ITERATOR_WQ_UNLOCK(); |
| 3853 | } |
| 3854 | |
| 3855 | /* release sctp_inpcb unbind the port */ |
| 3856 | void |
| 3857 | sctp_inpcb_free(struct sctp_inpcb *inp, int immediate, int from) |
| 3858 | { |
| 3859 | /* |
| 3860 | * Here we free a endpoint. We must find it (if it is in the Hash |
| 3861 | * table) and remove it from there. Then we must also find it in the |
| 3862 | * overall list and remove it from there. After all removals are |
| 3863 | * complete then any timer has to be stopped. Then start the actual |
| 3864 | * freeing. a) Any local lists. b) Any associations. c) The hash of |
| 3865 | * all associations. d) finally the ep itself. |
| 3866 | */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3867 | struct sctp_tcb *asoc, *nasoc; |
| 3868 | struct sctp_laddr *laddr, *nladdr; |
| 3869 | struct inpcb *ip_pcb; |
| 3870 | struct socket *so; |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 3871 | int being_refed = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3872 | struct sctp_queued_to_read *sq, *nsq; |
Michael Tuexen | 3b4263d | 2020-06-06 19:52:50 +0200 | [diff] [blame^] | 3873 | #if !defined(__Userspace__) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3874 | #if !defined(__FreeBSD__) || __FreeBSD_version < 500000 |
| 3875 | sctp_rtentry_t *rt; |
| 3876 | #endif |
| 3877 | #endif |
| 3878 | int cnt; |
| 3879 | sctp_sharedkey_t *shared_key, *nshared_key; |
| 3880 | |
| 3881 | |
| 3882 | #if defined(__APPLE__) |
| 3883 | sctp_lock_assert(SCTP_INP_SO(inp)); |
| 3884 | #endif |
| 3885 | #ifdef SCTP_LOG_CLOSING |
| 3886 | sctp_log_closing(inp, NULL, 0); |
| 3887 | #endif |
| 3888 | SCTP_ITERATOR_LOCK(); |
| 3889 | /* mark any iterators on the list or being processed */ |
| 3890 | sctp_iterator_inp_being_freed(inp); |
| 3891 | SCTP_ITERATOR_UNLOCK(); |
| 3892 | so = inp->sctp_socket; |
| 3893 | if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
| 3894 | /* been here before.. eeks.. get out of here */ |
| 3895 | SCTP_PRINTF("This conflict in free SHOULD not be happening! from %d, imm %d\n", from, immediate); |
| 3896 | #ifdef SCTP_LOG_CLOSING |
| 3897 | sctp_log_closing(inp, NULL, 1); |
| 3898 | #endif |
| 3899 | return; |
| 3900 | } |
| 3901 | SCTP_ASOC_CREATE_LOCK(inp); |
| 3902 | SCTP_INP_INFO_WLOCK(); |
| 3903 | |
| 3904 | SCTP_INP_WLOCK(inp); |
| 3905 | if (from == SCTP_CALLED_AFTER_CMPSET_OFCLOSE) { |
| 3906 | inp->sctp_flags &= ~SCTP_PCB_FLAGS_CLOSE_IP; |
| 3907 | /* socket is gone, so no more wakeups allowed */ |
| 3908 | inp->sctp_flags |= SCTP_PCB_FLAGS_DONT_WAKE; |
| 3909 | inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT; |
| 3910 | inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT; |
| 3911 | |
| 3912 | } |
| 3913 | /* First time through we have the socket lock, after that no more. */ |
| 3914 | sctp_timer_stop(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL, |
Michael Tuexen | 390beeb | 2020-02-04 15:05:45 +0100 | [diff] [blame] | 3915 | SCTP_FROM_SCTP_PCB + SCTP_LOC_1); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3916 | |
| 3917 | if (inp->control) { |
| 3918 | sctp_m_freem(inp->control); |
| 3919 | inp->control = NULL; |
| 3920 | } |
| 3921 | if (inp->pkt) { |
| 3922 | sctp_m_freem(inp->pkt); |
| 3923 | inp->pkt = NULL; |
| 3924 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3925 | ip_pcb = &inp->ip_inp.inp; /* we could just cast the main pointer |
| 3926 | * here but I will be nice :> (i.e. |
| 3927 | * ip_pcb = ep;) */ |
| 3928 | if (immediate == SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE) { |
| 3929 | int cnt_in_sd; |
| 3930 | |
| 3931 | cnt_in_sd = 0; |
| 3932 | LIST_FOREACH_SAFE(asoc, &inp->sctp_asoc_list, sctp_tcblist, nasoc) { |
| 3933 | SCTP_TCB_LOCK(asoc); |
| 3934 | if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { |
| 3935 | /* Skip guys being freed */ |
| 3936 | cnt_in_sd++; |
| 3937 | if (asoc->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE) { |
| 3938 | /* |
| 3939 | * Special case - we did not start a kill |
| 3940 | * timer on the asoc due to it was not |
| 3941 | * closed. So go ahead and start it now. |
| 3942 | */ |
Michael Tuexen | 348a36c | 2018-08-13 16:24:47 +0200 | [diff] [blame] | 3943 | SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_IN_ACCEPT_QUEUE); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3944 | sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, asoc, NULL); |
| 3945 | } |
| 3946 | SCTP_TCB_UNLOCK(asoc); |
| 3947 | continue; |
| 3948 | } |
Michael Tuexen | 348a36c | 2018-08-13 16:24:47 +0200 | [diff] [blame] | 3949 | if (((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) || |
| 3950 | (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) && |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3951 | (asoc->asoc.total_output_queue_size == 0)) { |
| 3952 | /* If we have data in queue, we don't want to just |
| 3953 | * free since the app may have done, send()/close |
| 3954 | * or connect/send/close. And it wants the data |
| 3955 | * to get across first. |
| 3956 | */ |
| 3957 | /* Just abandon things in the front states */ |
| 3958 | if (sctp_free_assoc(inp, asoc, SCTP_PCBFREE_NOFORCE, |
t00fcxen | 0057a6d | 2015-05-28 16:42:49 +0000 | [diff] [blame] | 3959 | SCTP_FROM_SCTP_PCB + SCTP_LOC_2) == 0) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3960 | cnt_in_sd++; |
| 3961 | } |
| 3962 | continue; |
| 3963 | } |
| 3964 | /* Disconnect the socket please */ |
| 3965 | asoc->sctp_socket = NULL; |
Michael Tuexen | 348a36c | 2018-08-13 16:24:47 +0200 | [diff] [blame] | 3966 | SCTP_ADD_SUBSTATE(asoc, SCTP_STATE_CLOSED_SOCKET); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3967 | if ((asoc->asoc.size_on_reasm_queue > 0) || |
| 3968 | (asoc->asoc.control_pdapi) || |
| 3969 | (asoc->asoc.size_on_all_streams > 0) || |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 3970 | (so && (so->so_rcv.sb_cc > 0))) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3971 | /* Left with Data unread */ |
| 3972 | struct mbuf *op_err; |
| 3973 | |
t00fcxen | 08f9ff9 | 2014-03-16 13:38:54 +0000 | [diff] [blame] | 3974 | op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, ""); |
t00fcxen | 0057a6d | 2015-05-28 16:42:49 +0000 | [diff] [blame] | 3975 | asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_3; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3976 | sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED); |
| 3977 | SCTP_STAT_INCR_COUNTER32(sctps_aborted); |
Michael Tuexen | 348a36c | 2018-08-13 16:24:47 +0200 | [diff] [blame] | 3978 | if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) || |
| 3979 | (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3980 | SCTP_STAT_DECR_GAUGE32(sctps_currestab); |
| 3981 | } |
| 3982 | if (sctp_free_assoc(inp, asoc, |
t00fcxen | 0057a6d | 2015-05-28 16:42:49 +0000 | [diff] [blame] | 3983 | SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_4) == 0) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3984 | cnt_in_sd++; |
| 3985 | } |
| 3986 | continue; |
| 3987 | } else if (TAILQ_EMPTY(&asoc->asoc.send_queue) && |
| 3988 | TAILQ_EMPTY(&asoc->asoc.sent_queue) && |
t00fcxen | 28611aa | 2012-09-23 07:45:40 +0000 | [diff] [blame] | 3989 | (asoc->asoc.stream_queue_cnt == 0)) { |
Michael Tuexen | fdcf790 | 2016-08-06 14:39:31 +0200 | [diff] [blame] | 3990 | if ((*asoc->asoc.ss_functions.sctp_ss_is_user_msgs_incomplete)(asoc, &asoc->asoc)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3991 | goto abort_anyway; |
| 3992 | } |
Michael Tuexen | 348a36c | 2018-08-13 16:24:47 +0200 | [diff] [blame] | 3993 | if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) && |
| 3994 | (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3995 | struct sctp_nets *netp; |
| 3996 | |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 3997 | /* |
| 3998 | * there is nothing queued to send, |
| 3999 | * so I send shutdown |
| 4000 | */ |
Michael Tuexen | 348a36c | 2018-08-13 16:24:47 +0200 | [diff] [blame] | 4001 | if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) || |
| 4002 | (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 4003 | SCTP_STAT_DECR_GAUGE32(sctps_currestab); |
| 4004 | } |
Michael Tuexen | 348a36c | 2018-08-13 16:24:47 +0200 | [diff] [blame] | 4005 | SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT); |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 4006 | sctp_stop_timers_for_shutdown(asoc); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4007 | if (asoc->asoc.alternate) { |
| 4008 | netp = asoc->asoc.alternate; |
| 4009 | } else { |
| 4010 | netp = asoc->asoc.primary_destination; |
| 4011 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4012 | sctp_send_shutdown(asoc, netp); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4013 | sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, asoc->sctp_ep, asoc, |
| 4014 | netp); |
Michael Tuexen | d07a5f2 | 2020-03-19 23:34:46 +0100 | [diff] [blame] | 4015 | sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc, NULL); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4016 | sctp_chunk_output(inp, asoc, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_LOCKED); |
| 4017 | } |
| 4018 | } else { |
| 4019 | /* mark into shutdown pending */ |
Michael Tuexen | 348a36c | 2018-08-13 16:24:47 +0200 | [diff] [blame] | 4020 | SCTP_ADD_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING); |
Michael Tuexen | d07a5f2 | 2020-03-19 23:34:46 +0100 | [diff] [blame] | 4021 | sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc, NULL); |
Michael Tuexen | fdcf790 | 2016-08-06 14:39:31 +0200 | [diff] [blame] | 4022 | if ((*asoc->asoc.ss_functions.sctp_ss_is_user_msgs_incomplete)(asoc, &asoc->asoc)) { |
Michael Tuexen | 348a36c | 2018-08-13 16:24:47 +0200 | [diff] [blame] | 4023 | SCTP_ADD_SUBSTATE(asoc, SCTP_STATE_PARTIAL_MSG_LEFT); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4024 | } |
| 4025 | if (TAILQ_EMPTY(&asoc->asoc.send_queue) && |
| 4026 | TAILQ_EMPTY(&asoc->asoc.sent_queue) && |
| 4027 | (asoc->asoc.state & SCTP_STATE_PARTIAL_MSG_LEFT)) { |
| 4028 | struct mbuf *op_err; |
| 4029 | abort_anyway: |
t00fcxen | 08f9ff9 | 2014-03-16 13:38:54 +0000 | [diff] [blame] | 4030 | op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, ""); |
t00fcxen | 0057a6d | 2015-05-28 16:42:49 +0000 | [diff] [blame] | 4031 | asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_5; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4032 | sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED); |
| 4033 | SCTP_STAT_INCR_COUNTER32(sctps_aborted); |
Michael Tuexen | 348a36c | 2018-08-13 16:24:47 +0200 | [diff] [blame] | 4034 | if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) || |
| 4035 | (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4036 | SCTP_STAT_DECR_GAUGE32(sctps_currestab); |
| 4037 | } |
| 4038 | if (sctp_free_assoc(inp, asoc, |
| 4039 | SCTP_PCBFREE_NOFORCE, |
t00fcxen | 0057a6d | 2015-05-28 16:42:49 +0000 | [diff] [blame] | 4040 | SCTP_FROM_SCTP_PCB + SCTP_LOC_6) == 0) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4041 | cnt_in_sd++; |
| 4042 | } |
| 4043 | continue; |
| 4044 | } else { |
| 4045 | sctp_chunk_output(inp, asoc, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED); |
| 4046 | } |
| 4047 | } |
| 4048 | cnt_in_sd++; |
| 4049 | SCTP_TCB_UNLOCK(asoc); |
| 4050 | } |
| 4051 | /* now is there some left in our SHUTDOWN state? */ |
| 4052 | if (cnt_in_sd) { |
| 4053 | #ifdef SCTP_LOG_CLOSING |
| 4054 | sctp_log_closing(inp, NULL, 2); |
| 4055 | #endif |
| 4056 | inp->sctp_socket = NULL; |
| 4057 | SCTP_INP_WUNLOCK(inp); |
| 4058 | SCTP_ASOC_CREATE_UNLOCK(inp); |
| 4059 | SCTP_INP_INFO_WUNLOCK(); |
| 4060 | return; |
| 4061 | } |
| 4062 | } |
| 4063 | inp->sctp_socket = NULL; |
| 4064 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) != |
| 4065 | SCTP_PCB_FLAGS_UNBOUND) { |
| 4066 | /* |
| 4067 | * ok, this guy has been bound. It's port is |
| 4068 | * somewhere in the SCTP_BASE_INFO(hash table). Remove |
| 4069 | * it! |
| 4070 | */ |
| 4071 | LIST_REMOVE(inp, sctp_hash); |
| 4072 | inp->sctp_flags |= SCTP_PCB_FLAGS_UNBOUND; |
| 4073 | } |
| 4074 | |
| 4075 | /* If there is a timer running to kill us, |
| 4076 | * forget it, since it may have a contest |
| 4077 | * on the INP lock.. which would cause us |
| 4078 | * to die ... |
| 4079 | */ |
| 4080 | cnt = 0; |
| 4081 | LIST_FOREACH_SAFE(asoc, &inp->sctp_asoc_list, sctp_tcblist, nasoc) { |
| 4082 | SCTP_TCB_LOCK(asoc); |
| 4083 | if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { |
| 4084 | if (asoc->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE) { |
Michael Tuexen | 348a36c | 2018-08-13 16:24:47 +0200 | [diff] [blame] | 4085 | SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_IN_ACCEPT_QUEUE); |
t00fcxen | 28611aa | 2012-09-23 07:45:40 +0000 | [diff] [blame] | 4086 | sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, asoc, NULL); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4087 | } |
Michael Tuexen | 61f6683 | 2020-02-09 23:15:19 +0100 | [diff] [blame] | 4088 | cnt++; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4089 | SCTP_TCB_UNLOCK(asoc); |
| 4090 | continue; |
| 4091 | } |
| 4092 | /* Free associations that are NOT killing us */ |
Michael Tuexen | 348a36c | 2018-08-13 16:24:47 +0200 | [diff] [blame] | 4093 | if ((SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) && |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4094 | ((asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0)) { |
| 4095 | struct mbuf *op_err; |
t00fcxen | 28611aa | 2012-09-23 07:45:40 +0000 | [diff] [blame] | 4096 | |
t00fcxen | 08f9ff9 | 2014-03-16 13:38:54 +0000 | [diff] [blame] | 4097 | op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, ""); |
t00fcxen | 0057a6d | 2015-05-28 16:42:49 +0000 | [diff] [blame] | 4098 | asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_7; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4099 | sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED); |
| 4100 | SCTP_STAT_INCR_COUNTER32(sctps_aborted); |
| 4101 | } else if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { |
| 4102 | cnt++; |
| 4103 | SCTP_TCB_UNLOCK(asoc); |
| 4104 | continue; |
| 4105 | } |
Michael Tuexen | 348a36c | 2018-08-13 16:24:47 +0200 | [diff] [blame] | 4106 | if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) || |
| 4107 | (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4108 | SCTP_STAT_DECR_GAUGE32(sctps_currestab); |
| 4109 | } |
t00fcxen | 0057a6d | 2015-05-28 16:42:49 +0000 | [diff] [blame] | 4110 | if (sctp_free_assoc(inp, asoc, SCTP_PCBFREE_FORCE, |
| 4111 | SCTP_FROM_SCTP_PCB + SCTP_LOC_8) == 0) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4112 | cnt++; |
| 4113 | } |
| 4114 | } |
| 4115 | if (cnt) { |
| 4116 | /* Ok we have someone out there that will kill us */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4117 | #ifdef SCTP_LOG_CLOSING |
| 4118 | sctp_log_closing(inp, NULL, 3); |
| 4119 | #endif |
| 4120 | SCTP_INP_WUNLOCK(inp); |
| 4121 | SCTP_ASOC_CREATE_UNLOCK(inp); |
| 4122 | SCTP_INP_INFO_WUNLOCK(); |
| 4123 | return; |
| 4124 | } |
| 4125 | if (SCTP_INP_LOCK_CONTENDED(inp)) |
| 4126 | being_refed++; |
| 4127 | if (SCTP_INP_READ_CONTENDED(inp)) |
| 4128 | being_refed++; |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 4129 | if (SCTP_ASOC_CREATE_LOCK_CONTENDED(inp)) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4130 | being_refed++; |
| 4131 | |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 4132 | if ((inp->refcount) || |
t00fcxen | 28611aa | 2012-09-23 07:45:40 +0000 | [diff] [blame] | 4133 | (being_refed) || |
| 4134 | (inp->sctp_flags & SCTP_PCB_FLAGS_CLOSE_IP)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4135 | #ifdef SCTP_LOG_CLOSING |
| 4136 | sctp_log_closing(inp, NULL, 4); |
| 4137 | #endif |
| 4138 | sctp_timer_start(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL); |
| 4139 | SCTP_INP_WUNLOCK(inp); |
| 4140 | SCTP_ASOC_CREATE_UNLOCK(inp); |
| 4141 | SCTP_INP_INFO_WUNLOCK(); |
| 4142 | return; |
| 4143 | } |
| 4144 | inp->sctp_ep.signature_change.type = 0; |
| 4145 | inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_ALLGONE; |
| 4146 | /* Remove it from the list .. last thing we need a |
| 4147 | * lock for. |
| 4148 | */ |
| 4149 | LIST_REMOVE(inp, sctp_list); |
| 4150 | SCTP_INP_WUNLOCK(inp); |
| 4151 | SCTP_ASOC_CREATE_UNLOCK(inp); |
| 4152 | SCTP_INP_INFO_WUNLOCK(); |
| 4153 | /* Now we release all locks. Since this INP |
| 4154 | * cannot be found anymore except possibly by the |
| 4155 | * kill timer that might be running. We call |
| 4156 | * the drain function here. It should hit the case |
| 4157 | * were it sees the ACTIVE flag cleared and exit |
| 4158 | * out freeing us to proceed and destroy everything. |
| 4159 | */ |
| 4160 | if (from != SCTP_CALLED_FROM_INPKILL_TIMER) { |
| 4161 | (void)SCTP_OS_TIMER_STOP_DRAIN(&inp->sctp_ep.signature_change.timer); |
| 4162 | } else { |
| 4163 | /* Probably un-needed */ |
| 4164 | (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer); |
| 4165 | } |
| 4166 | |
| 4167 | #ifdef SCTP_LOG_CLOSING |
| 4168 | sctp_log_closing(inp, NULL, 5); |
| 4169 | #endif |
| 4170 | |
Michael Tuexen | 3b4263d | 2020-06-06 19:52:50 +0200 | [diff] [blame^] | 4171 | #if !(defined(__Windows__) || defined(__Userspace__)) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4172 | #if !defined(__FreeBSD__) || __FreeBSD_version < 500000 |
| 4173 | rt = ip_pcb->inp_route.ro_rt; |
| 4174 | #endif |
| 4175 | #endif |
| 4176 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4177 | if ((inp->sctp_asocidhash) != NULL) { |
| 4178 | SCTP_HASH_FREE(inp->sctp_asocidhash, inp->hashasocidmark); |
| 4179 | inp->sctp_asocidhash = NULL; |
| 4180 | } |
| 4181 | /*sa_ignore FREED_MEMORY*/ |
| 4182 | TAILQ_FOREACH_SAFE(sq, &inp->read_queue, next, nsq) { |
| 4183 | /* Its only abandoned if it had data left */ |
| 4184 | if (sq->length) |
| 4185 | SCTP_STAT_INCR(sctps_left_abandon); |
| 4186 | |
| 4187 | TAILQ_REMOVE(&inp->read_queue, sq, next); |
| 4188 | sctp_free_remote_addr(sq->whoFrom); |
| 4189 | if (so) |
| 4190 | so->so_rcv.sb_cc -= sq->length; |
| 4191 | if (sq->data) { |
| 4192 | sctp_m_freem(sq->data); |
| 4193 | sq->data = NULL; |
| 4194 | } |
| 4195 | /* |
| 4196 | * no need to free the net count, since at this point all |
| 4197 | * assoc's are gone. |
| 4198 | */ |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 4199 | sctp_free_a_readq(NULL, sq); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4200 | } |
| 4201 | /* Now the sctp_pcb things */ |
| 4202 | /* |
| 4203 | * free each asoc if it is not already closed/free. we can't use the |
| 4204 | * macro here since le_next will get freed as part of the |
| 4205 | * sctp_free_assoc() call. |
| 4206 | */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4207 | if (ip_pcb->inp_options) { |
| 4208 | (void)sctp_m_free(ip_pcb->inp_options); |
| 4209 | ip_pcb->inp_options = 0; |
| 4210 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4211 | |
Michael Tuexen | 3b4263d | 2020-06-06 19:52:50 +0200 | [diff] [blame^] | 4212 | #if !(defined(__Windows__) || defined(__Userspace__)) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4213 | #if !defined(__FreeBSD__) || __FreeBSD_version < 500000 |
| 4214 | if (rt) { |
| 4215 | RTFREE(rt); |
| 4216 | ip_pcb->inp_route.ro_rt = 0; |
| 4217 | } |
| 4218 | #endif |
tuexen | 5fc2284 | 2012-02-11 13:32:19 +0000 | [diff] [blame] | 4219 | #if defined(__FreeBSD__) && __FreeBSD_version < 803000 |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4220 | #ifdef INET |
| 4221 | if (ip_pcb->inp_moptions) { |
| 4222 | inp_freemoptions(ip_pcb->inp_moptions); |
| 4223 | ip_pcb->inp_moptions = 0; |
| 4224 | } |
| 4225 | #endif |
| 4226 | #endif |
| 4227 | #endif |
| 4228 | |
| 4229 | #ifdef INET6 |
Michael Tuexen | 3b4263d | 2020-06-06 19:52:50 +0200 | [diff] [blame^] | 4230 | #if !(defined(__Windows__) || defined(__Userspace__)) |
t00fcxen | 4c76a91 | 2012-09-14 14:31:21 +0000 | [diff] [blame] | 4231 | #if defined(__FreeBSD__) || defined(__APPLE__) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4232 | if (ip_pcb->inp_vflag & INP_IPV6) { |
t00fcxen | 4c76a91 | 2012-09-14 14:31:21 +0000 | [diff] [blame] | 4233 | #else |
| 4234 | if (inp->inp_vflag & INP_IPV6) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4235 | #endif |
Michael Tuexen | 801a64b | 2019-08-05 15:33:37 +0200 | [diff] [blame] | 4236 | ip6_freepcbopts(ip_pcb->in6p_outputopts); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4237 | } |
t00fcxen | 4c76a91 | 2012-09-14 14:31:21 +0000 | [diff] [blame] | 4238 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4239 | #endif /* INET6 */ |
| 4240 | #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)) |
| 4241 | inp->inp_vflag = 0; |
| 4242 | #else |
| 4243 | ip_pcb->inp_vflag = 0; |
| 4244 | #endif |
| 4245 | /* free up authentication fields */ |
| 4246 | if (inp->sctp_ep.local_auth_chunks != NULL) |
| 4247 | sctp_free_chunklist(inp->sctp_ep.local_auth_chunks); |
| 4248 | if (inp->sctp_ep.local_hmacs != NULL) |
| 4249 | sctp_free_hmaclist(inp->sctp_ep.local_hmacs); |
| 4250 | |
| 4251 | LIST_FOREACH_SAFE(shared_key, &inp->sctp_ep.shared_keys, next, nshared_key) { |
| 4252 | LIST_REMOVE(shared_key, next); |
| 4253 | sctp_free_sharedkey(shared_key); |
| 4254 | /*sa_ignore FREED_MEMORY*/ |
| 4255 | } |
| 4256 | |
| 4257 | #if defined(__APPLE__) |
| 4258 | inp->ip_inp.inp.inp_state = INPCB_STATE_DEAD; |
tuexen | 6d01b94 | 2012-05-04 18:15:17 +0000 | [diff] [blame] | 4259 | if (in_pcb_checkstate(&inp->ip_inp.inp, WNT_STOPUSING, 1) != WNT_STOPUSING) { |
tuexen | 49fdcc8 | 2012-05-04 10:13:12 +0000 | [diff] [blame] | 4260 | #ifdef INVARIANTS |
t00fcxen | 8fca7ff | 2012-09-05 18:58:42 +0000 | [diff] [blame] | 4261 | panic("sctp_inpcb_free inp = %p couldn't set to STOPUSING\n", (void *)inp); |
tuexen | 6d01b94 | 2012-05-04 18:15:17 +0000 | [diff] [blame] | 4262 | #else |
t00fcxen | 8fca7ff | 2012-09-05 18:58:42 +0000 | [diff] [blame] | 4263 | 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] | 4264 | #endif |
tuexen | 6d01b94 | 2012-05-04 18:15:17 +0000 | [diff] [blame] | 4265 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4266 | inp->ip_inp.inp.inp_socket->so_flags |= SOF_PCBCLEARING; |
| 4267 | #endif |
| 4268 | /* |
| 4269 | * if we have an address list the following will free the list of |
| 4270 | * ifaddr's that are set into this ep. Again macro limitations here, |
| 4271 | * since the LIST_FOREACH could be a bad idea. |
| 4272 | */ |
| 4273 | LIST_FOREACH_SAFE(laddr, &inp->sctp_addr_list, sctp_nxt_addr, nladdr) { |
| 4274 | sctp_remove_laddr(laddr); |
| 4275 | } |
| 4276 | |
| 4277 | #ifdef SCTP_TRACK_FREED_ASOCS |
| 4278 | /* TEMP CODE */ |
| 4279 | LIST_FOREACH_SAFE(asoc, &inp->sctp_asoc_free_list, sctp_tcblist, nasoc) { |
| 4280 | LIST_REMOVE(asoc, sctp_tcblist); |
| 4281 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), asoc); |
| 4282 | SCTP_DECR_ASOC_COUNT(); |
| 4283 | } |
| 4284 | /* *** END TEMP CODE ****/ |
| 4285 | #endif |
| 4286 | #ifdef SCTP_MVRF |
| 4287 | SCTP_FREE(inp->m_vrf_ids, SCTP_M_MVRF); |
| 4288 | #endif |
| 4289 | /* Now lets see about freeing the EP hash table. */ |
| 4290 | if (inp->sctp_tcbhash != NULL) { |
| 4291 | SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark); |
| 4292 | inp->sctp_tcbhash = NULL; |
| 4293 | } |
| 4294 | /* Now we must put the ep memory back into the zone pool */ |
| 4295 | #if defined(__FreeBSD__) |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 4296 | crfree(inp->ip_inp.inp.inp_cred); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4297 | INP_LOCK_DESTROY(&inp->ip_inp.inp); |
| 4298 | #endif |
| 4299 | SCTP_INP_LOCK_DESTROY(inp); |
| 4300 | SCTP_INP_READ_DESTROY(inp); |
| 4301 | SCTP_ASOC_CREATE_LOCK_DESTROY(inp); |
| 4302 | #if !defined(__APPLE__) |
| 4303 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp); |
| 4304 | SCTP_DECR_EP_COUNT(); |
| 4305 | #else |
| 4306 | /* For Tiger, we will do this later... */ |
| 4307 | #endif |
| 4308 | } |
| 4309 | |
| 4310 | |
| 4311 | struct sctp_nets * |
| 4312 | sctp_findnet(struct sctp_tcb *stcb, struct sockaddr *addr) |
| 4313 | { |
| 4314 | struct sctp_nets *net; |
| 4315 | /* locate the address */ |
| 4316 | TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { |
| 4317 | if (sctp_cmpaddr(addr, (struct sockaddr *)&net->ro._l_addr)) |
| 4318 | return (net); |
| 4319 | } |
| 4320 | return (NULL); |
| 4321 | } |
| 4322 | |
| 4323 | |
| 4324 | int |
| 4325 | sctp_is_address_on_local_host(struct sockaddr *addr, uint32_t vrf_id) |
| 4326 | { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4327 | struct sctp_ifa *sctp_ifa; |
| 4328 | sctp_ifa = sctp_find_ifa_by_addr(addr, vrf_id, SCTP_ADDR_NOT_LOCKED); |
| 4329 | if (sctp_ifa) { |
| 4330 | return (1); |
| 4331 | } else { |
| 4332 | return (0); |
| 4333 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4334 | } |
| 4335 | |
| 4336 | /* |
| 4337 | * add's a remote endpoint address, done with the INIT/INIT-ACK as well as |
| 4338 | * when a ASCONF arrives that adds it. It will also initialize all the cwnd |
| 4339 | * stats of stuff. |
| 4340 | */ |
| 4341 | int |
| 4342 | sctp_add_remote_addr(struct sctp_tcb *stcb, struct sockaddr *newaddr, |
Michael Tuexen | 2757442 | 2016-04-30 16:35:29 +0200 | [diff] [blame] | 4343 | struct sctp_nets **netp, uint16_t port, int set_scope, int from) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4344 | { |
| 4345 | /* |
| 4346 | * The following is redundant to the same lines in the |
| 4347 | * sctp_aloc_assoc() but is needed since others call the add |
| 4348 | * address function |
| 4349 | */ |
| 4350 | struct sctp_nets *net, *netfirst; |
| 4351 | int addr_inscope; |
| 4352 | |
| 4353 | SCTPDBG(SCTP_DEBUG_PCB1, "Adding an address (from:%d) to the peer: ", |
| 4354 | from); |
| 4355 | SCTPDBG_ADDR(SCTP_DEBUG_PCB1, newaddr); |
| 4356 | |
| 4357 | netfirst = sctp_findnet(stcb, newaddr); |
| 4358 | if (netfirst) { |
| 4359 | /* |
| 4360 | * Lie and return ok, we don't want to make the association |
| 4361 | * go away for this behavior. It will happen in the TCP |
| 4362 | * model in a connected socket. It does not reach the hash |
| 4363 | * table until after the association is built so it can't be |
| 4364 | * found. Mark as reachable, since the initial creation will |
| 4365 | * have been cleared and the NOT_IN_ASSOC flag will have |
| 4366 | * been added... and we don't want to end up removing it |
| 4367 | * back out. |
| 4368 | */ |
| 4369 | if (netfirst->dest_state & SCTP_ADDR_UNCONFIRMED) { |
| 4370 | netfirst->dest_state = (SCTP_ADDR_REACHABLE | |
| 4371 | SCTP_ADDR_UNCONFIRMED); |
| 4372 | } else { |
| 4373 | netfirst->dest_state = SCTP_ADDR_REACHABLE; |
| 4374 | } |
| 4375 | |
| 4376 | return (0); |
| 4377 | } |
| 4378 | addr_inscope = 1; |
| 4379 | switch (newaddr->sa_family) { |
| 4380 | #ifdef INET |
| 4381 | case AF_INET: |
| 4382 | { |
| 4383 | struct sockaddr_in *sin; |
| 4384 | |
| 4385 | sin = (struct sockaddr_in *)newaddr; |
| 4386 | if (sin->sin_addr.s_addr == 0) { |
| 4387 | /* Invalid address */ |
| 4388 | return (-1); |
| 4389 | } |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 4390 | /* zero out the zero area */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4391 | memset(&sin->sin_zero, 0, sizeof(sin->sin_zero)); |
| 4392 | |
| 4393 | /* assure len is set */ |
t00fcxen | 8fbaee3 | 2012-09-04 16:41:39 +0000 | [diff] [blame] | 4394 | #ifdef HAVE_SIN_LEN |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4395 | sin->sin_len = sizeof(struct sockaddr_in); |
| 4396 | #endif |
| 4397 | if (set_scope) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4398 | if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) { |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 4399 | stcb->asoc.scope.ipv4_local_scope = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4400 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4401 | } else { |
| 4402 | /* Validate the address is in scope */ |
| 4403 | if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) && |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 4404 | (stcb->asoc.scope.ipv4_local_scope == 0)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4405 | addr_inscope = 0; |
| 4406 | } |
| 4407 | } |
| 4408 | break; |
| 4409 | } |
| 4410 | #endif |
| 4411 | #ifdef INET6 |
| 4412 | case AF_INET6: |
| 4413 | { |
| 4414 | struct sockaddr_in6 *sin6; |
| 4415 | |
| 4416 | sin6 = (struct sockaddr_in6 *)newaddr; |
| 4417 | if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { |
| 4418 | /* Invalid address */ |
| 4419 | return (-1); |
| 4420 | } |
| 4421 | /* assure len is set */ |
t00fcxen | 8fbaee3 | 2012-09-04 16:41:39 +0000 | [diff] [blame] | 4422 | #ifdef HAVE_SIN6_LEN |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4423 | sin6->sin6_len = sizeof(struct sockaddr_in6); |
| 4424 | #endif |
| 4425 | if (set_scope) { |
| 4426 | if (sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id)) { |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 4427 | stcb->asoc.scope.loopback_scope = 1; |
| 4428 | stcb->asoc.scope.local_scope = 0; |
| 4429 | stcb->asoc.scope.ipv4_local_scope = 1; |
| 4430 | stcb->asoc.scope.site_scope = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4431 | } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { |
| 4432 | /* |
| 4433 | * If the new destination is a LINK_LOCAL we |
| 4434 | * must have common site scope. Don't set |
| 4435 | * the local scope since we may not share |
| 4436 | * all links, only loopback can do this. |
| 4437 | * Links on the local network would also be |
| 4438 | * on our private network for v4 too. |
| 4439 | */ |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 4440 | stcb->asoc.scope.ipv4_local_scope = 1; |
| 4441 | stcb->asoc.scope.site_scope = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4442 | } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) { |
| 4443 | /* |
| 4444 | * If the new destination is SITE_LOCAL then |
| 4445 | * we must have site scope in common. |
| 4446 | */ |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 4447 | stcb->asoc.scope.site_scope = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4448 | } |
| 4449 | } else { |
| 4450 | /* Validate the address is in scope */ |
| 4451 | if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) && |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 4452 | (stcb->asoc.scope.loopback_scope == 0)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4453 | addr_inscope = 0; |
| 4454 | } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) && |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 4455 | (stcb->asoc.scope.local_scope == 0)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4456 | addr_inscope = 0; |
| 4457 | } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) && |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 4458 | (stcb->asoc.scope.site_scope == 0)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4459 | addr_inscope = 0; |
| 4460 | } |
| 4461 | } |
| 4462 | break; |
| 4463 | } |
| 4464 | #endif |
tuexen | cc2b142 | 2012-07-10 21:50:32 +0000 | [diff] [blame] | 4465 | #if defined(__Userspace__) |
| 4466 | case AF_CONN: |
| 4467 | { |
| 4468 | struct sockaddr_conn *sconn; |
| 4469 | |
| 4470 | sconn = (struct sockaddr_conn *)newaddr; |
| 4471 | if (sconn->sconn_addr == NULL) { |
| 4472 | /* Invalid address */ |
| 4473 | return (-1); |
| 4474 | } |
t00fcxen | 8fbaee3 | 2012-09-04 16:41:39 +0000 | [diff] [blame] | 4475 | #ifdef HAVE_SCONN_LEN |
tuexen | cc2b142 | 2012-07-10 21:50:32 +0000 | [diff] [blame] | 4476 | sconn->sconn_len = sizeof(struct sockaddr_conn); |
| 4477 | #endif |
| 4478 | break; |
| 4479 | } |
| 4480 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4481 | default: |
| 4482 | /* not supported family type */ |
| 4483 | return (-1); |
| 4484 | } |
| 4485 | net = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_net), struct sctp_nets); |
| 4486 | if (net == NULL) { |
| 4487 | return (-1); |
| 4488 | } |
| 4489 | SCTP_INCR_RADDR_COUNT(); |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 4490 | memset(net, 0, sizeof(struct sctp_nets)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4491 | (void)SCTP_GETTIME_TIMEVAL(&net->start_time); |
t00fcxen | 907930b | 2012-09-12 19:43:22 +0000 | [diff] [blame] | 4492 | #ifdef HAVE_SA_LEN |
| 4493 | memcpy(&net->ro._l_addr, newaddr, newaddr->sa_len); |
| 4494 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4495 | switch (newaddr->sa_family) { |
| 4496 | #ifdef INET |
| 4497 | case AF_INET: |
t00fcxen | 907930b | 2012-09-12 19:43:22 +0000 | [diff] [blame] | 4498 | #ifndef HAVE_SA_LEN |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4499 | memcpy(&net->ro._l_addr, newaddr, sizeof(struct sockaddr_in)); |
| 4500 | #endif |
| 4501 | ((struct sockaddr_in *)&net->ro._l_addr)->sin_port = stcb->rport; |
| 4502 | break; |
| 4503 | #endif |
| 4504 | #ifdef INET6 |
| 4505 | case AF_INET6: |
t00fcxen | 907930b | 2012-09-12 19:43:22 +0000 | [diff] [blame] | 4506 | #ifndef HAVE_SA_LEN |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4507 | memcpy(&net->ro._l_addr, newaddr, sizeof(struct sockaddr_in6)); |
| 4508 | #endif |
| 4509 | ((struct sockaddr_in6 *)&net->ro._l_addr)->sin6_port = stcb->rport; |
| 4510 | break; |
| 4511 | #endif |
tuexen | cc2b142 | 2012-07-10 21:50:32 +0000 | [diff] [blame] | 4512 | #if defined(__Userspace__) |
| 4513 | case AF_CONN: |
t00fcxen | 907930b | 2012-09-12 19:43:22 +0000 | [diff] [blame] | 4514 | #ifndef HAVE_SA_LEN |
tuexen | cc2b142 | 2012-07-10 21:50:32 +0000 | [diff] [blame] | 4515 | memcpy(&net->ro._l_addr, newaddr, sizeof(struct sockaddr_conn)); |
| 4516 | #endif |
| 4517 | ((struct sockaddr_conn *)&net->ro._l_addr)->sconn_port = stcb->rport; |
| 4518 | break; |
| 4519 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4520 | default: |
| 4521 | break; |
| 4522 | } |
| 4523 | net->addr_is_local = sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id); |
| 4524 | if (net->addr_is_local && ((set_scope || (from == SCTP_ADDR_IS_CONFIRMED)))) { |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 4525 | stcb->asoc.scope.loopback_scope = 1; |
| 4526 | stcb->asoc.scope.ipv4_local_scope = 1; |
| 4527 | stcb->asoc.scope.local_scope = 0; |
| 4528 | stcb->asoc.scope.site_scope = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4529 | addr_inscope = 1; |
| 4530 | } |
| 4531 | net->failure_threshold = stcb->asoc.def_net_failure; |
| 4532 | net->pf_threshold = stcb->asoc.def_net_pf_threshold; |
| 4533 | if (addr_inscope == 0) { |
| 4534 | net->dest_state = (SCTP_ADDR_REACHABLE | |
| 4535 | SCTP_ADDR_OUT_OF_SCOPE); |
| 4536 | } else { |
| 4537 | if (from == SCTP_ADDR_IS_CONFIRMED) |
| 4538 | /* SCTP_ADDR_IS_CONFIRMED is passed by connect_x */ |
| 4539 | net->dest_state = SCTP_ADDR_REACHABLE; |
| 4540 | else |
| 4541 | net->dest_state = SCTP_ADDR_REACHABLE | |
| 4542 | SCTP_ADDR_UNCONFIRMED; |
| 4543 | } |
| 4544 | /* We set this to 0, the timer code knows that |
| 4545 | * this means its an initial value |
| 4546 | */ |
| 4547 | net->rto_needed = 1; |
t00fcxen | 8dd95b8 | 2014-07-11 20:41:28 +0000 | [diff] [blame] | 4548 | net->RTO = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4549 | net->RTO_measured = 0; |
| 4550 | stcb->asoc.numnets++; |
tuexen | 9d7b207 | 2011-11-20 16:35:17 +0000 | [diff] [blame] | 4551 | net->ref_count = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4552 | 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] | 4553 | net->port = port; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4554 | net->dscp = stcb->asoc.default_dscp; |
| 4555 | #ifdef INET6 |
| 4556 | net->flowlabel = stcb->asoc.default_flowlabel; |
| 4557 | #endif |
| 4558 | if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) { |
| 4559 | net->dest_state |= SCTP_ADDR_NOHB; |
| 4560 | } else { |
| 4561 | net->dest_state &= ~SCTP_ADDR_NOHB; |
| 4562 | } |
| 4563 | if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD)) { |
| 4564 | net->dest_state |= SCTP_ADDR_NO_PMTUD; |
| 4565 | } else { |
| 4566 | net->dest_state &= ~SCTP_ADDR_NO_PMTUD; |
| 4567 | } |
| 4568 | net->heart_beat_delay = stcb->asoc.heart_beat_delay; |
| 4569 | /* Init the timer structure */ |
| 4570 | SCTP_OS_TIMER_INIT(&net->rxt_timer.timer); |
| 4571 | SCTP_OS_TIMER_INIT(&net->pmtu_timer.timer); |
| 4572 | SCTP_OS_TIMER_INIT(&net->hb_timer.timer); |
| 4573 | |
| 4574 | /* Now generate a route for this guy */ |
| 4575 | #ifdef INET6 |
| 4576 | #ifdef SCTP_EMBEDDED_V6_SCOPE |
| 4577 | /* KAME hack: embed scopeid */ |
| 4578 | if (newaddr->sa_family == AF_INET6) { |
| 4579 | struct sockaddr_in6 *sin6; |
| 4580 | |
| 4581 | sin6 = (struct sockaddr_in6 *)&net->ro._l_addr; |
tuexen | e385159 | 2012-06-01 08:31:45 +0000 | [diff] [blame] | 4582 | #if defined(__APPLE__) |
tuexen | 9a218b1 | 2012-08-04 21:17:58 +0000 | [diff] [blame] | 4583 | #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) |
| 4584 | (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] | 4585 | #else |
tuexen | 9a218b1 | 2012-08-04 21:17:58 +0000 | [diff] [blame] | 4586 | (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] | 4587 | #endif |
| 4588 | #elif defined(SCTP_KAME) |
| 4589 | (void)sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)); |
| 4590 | #else |
| 4591 | (void)in6_embedscope(&sin6->sin6_addr, sin6); |
| 4592 | #endif |
| 4593 | #ifndef SCOPEDROUTING |
| 4594 | sin6->sin6_scope_id = 0; |
| 4595 | #endif |
| 4596 | } |
| 4597 | #endif /* SCTP_EMBEDDED_V6_SCOPE */ |
| 4598 | #endif |
t00fcxen | a51e446 | 2015-06-17 15:53:23 +0000 | [diff] [blame] | 4599 | SCTP_RTALLOC((sctp_route_t *)&net->ro, |
| 4600 | stcb->asoc.vrf_id, |
| 4601 | stcb->sctp_ep->fibnum); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4602 | |
t00fcxen | 8b6ef09 | 2014-07-11 17:37:40 +0000 | [diff] [blame] | 4603 | net->src_addr_selected = 0; |
Michael Tuexen | d900e5f | 2017-06-23 10:52:43 +0200 | [diff] [blame] | 4604 | #if !defined(__Userspace__) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4605 | if (SCTP_ROUTE_HAS_VALID_IFN(&net->ro)) { |
| 4606 | /* Get source address */ |
| 4607 | net->ro._s_addr = sctp_source_address_selection(stcb->sctp_ep, |
Michael Tuexen | d900e5f | 2017-06-23 10:52:43 +0200 | [diff] [blame] | 4608 | stcb, |
| 4609 | (sctp_route_t *)&net->ro, |
| 4610 | net, |
| 4611 | 0, |
| 4612 | stcb->asoc.vrf_id); |
Michael Tuexen | de0630d | 2017-11-03 22:04:01 +0100 | [diff] [blame] | 4613 | if (stcb->asoc.default_mtu > 0) { |
| 4614 | net->mtu = stcb->asoc.default_mtu; |
| 4615 | switch (net->ro._l_addr.sa.sa_family) { |
| 4616 | #ifdef INET |
| 4617 | case AF_INET: |
| 4618 | net->mtu += SCTP_MIN_V4_OVERHEAD; |
| 4619 | break; |
| 4620 | #endif |
| 4621 | #ifdef INET6 |
| 4622 | case AF_INET6: |
| 4623 | net->mtu += SCTP_MIN_OVERHEAD; |
| 4624 | break; |
| 4625 | #endif |
| 4626 | #if defined(__Userspace__) |
| 4627 | case AF_CONN: |
| 4628 | net->mtu += sizeof(struct sctphdr); |
| 4629 | break; |
| 4630 | #endif |
| 4631 | default: |
| 4632 | break; |
| 4633 | } |
| 4634 | #if defined(INET) || defined(INET6) |
| 4635 | if (net->port) { |
| 4636 | net->mtu += (uint32_t)sizeof(struct udphdr); |
| 4637 | } |
| 4638 | #endif |
| 4639 | } else if (net->ro._s_addr != NULL) { |
Michael Tuexen | d900e5f | 2017-06-23 10:52:43 +0200 | [diff] [blame] | 4640 | uint32_t imtu, rmtu, hcmtu; |
| 4641 | |
t00fcxen | 8b6ef09 | 2014-07-11 17:37:40 +0000 | [diff] [blame] | 4642 | net->src_addr_selected = 1; |
| 4643 | /* Now get the interface MTU */ |
| 4644 | if (net->ro._s_addr->ifn_p != NULL) { |
Michael Tuexen | d900e5f | 2017-06-23 10:52:43 +0200 | [diff] [blame] | 4645 | imtu = SCTP_GATHER_MTU_FROM_INTFC(net->ro._s_addr->ifn_p); |
| 4646 | } else { |
| 4647 | imtu = 0; |
t00fcxen | 8b6ef09 | 2014-07-11 17:37:40 +0000 | [diff] [blame] | 4648 | } |
Michael Tuexen | d900e5f | 2017-06-23 10:52:43 +0200 | [diff] [blame] | 4649 | #if defined(__FreeBSD__) |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4650 | 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] | 4651 | hcmtu = sctp_hc_get_mtu(&net->ro._l_addr, stcb->sctp_ep->fibnum); |
| 4652 | #else |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4653 | 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] | 4654 | hcmtu = 0; |
| 4655 | #endif |
| 4656 | net->mtu = sctp_min_mtu(hcmtu, rmtu, imtu); |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4657 | #if defined(__FreeBSD__) |
| 4658 | #else |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4659 | if (rmtu == 0) { |
| 4660 | /* Start things off to match mtu of interface please. */ |
| 4661 | SCTP_SET_MTU_OF_ROUTE(&net->ro._l_addr.sa, |
Michael Tuexen | d900e5f | 2017-06-23 10:52:43 +0200 | [diff] [blame] | 4662 | net->ro.ro_rt, net->mtu); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4663 | } |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4664 | #endif |
t00fcxen | 8b6ef09 | 2014-07-11 17:37:40 +0000 | [diff] [blame] | 4665 | } |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 4666 | } |
t00fcxen | 6d60eb0 | 2013-08-30 07:37:01 +0000 | [diff] [blame] | 4667 | #endif |
tuexen | e81177b | 2011-11-15 20:48:08 +0000 | [diff] [blame] | 4668 | if (net->mtu == 0) { |
Michael Tuexen | de0630d | 2017-11-03 22:04:01 +0100 | [diff] [blame] | 4669 | if (stcb->asoc.default_mtu > 0) { |
| 4670 | net->mtu = stcb->asoc.default_mtu; |
| 4671 | switch (net->ro._l_addr.sa.sa_family) { |
tuexen | e81177b | 2011-11-15 20:48:08 +0000 | [diff] [blame] | 4672 | #ifdef INET |
Michael Tuexen | de0630d | 2017-11-03 22:04:01 +0100 | [diff] [blame] | 4673 | case AF_INET: |
| 4674 | net->mtu += SCTP_MIN_V4_OVERHEAD; |
| 4675 | break; |
tuexen | e81177b | 2011-11-15 20:48:08 +0000 | [diff] [blame] | 4676 | #endif |
| 4677 | #ifdef INET6 |
Michael Tuexen | de0630d | 2017-11-03 22:04:01 +0100 | [diff] [blame] | 4678 | case AF_INET6: |
| 4679 | net->mtu += SCTP_MIN_OVERHEAD; |
| 4680 | break; |
tuexen | e81177b | 2011-11-15 20:48:08 +0000 | [diff] [blame] | 4681 | #endif |
tuexen | cc2b142 | 2012-07-10 21:50:32 +0000 | [diff] [blame] | 4682 | #if defined(__Userspace__) |
Michael Tuexen | de0630d | 2017-11-03 22:04:01 +0100 | [diff] [blame] | 4683 | case AF_CONN: |
| 4684 | net->mtu += sizeof(struct sctphdr); |
| 4685 | break; |
tuexen | cc2b142 | 2012-07-10 21:50:32 +0000 | [diff] [blame] | 4686 | #endif |
Michael Tuexen | de0630d | 2017-11-03 22:04:01 +0100 | [diff] [blame] | 4687 | default: |
| 4688 | break; |
| 4689 | } |
| 4690 | #if defined(INET) || defined(INET6) |
| 4691 | if (net->port) { |
| 4692 | net->mtu += (uint32_t)sizeof(struct udphdr); |
| 4693 | } |
| 4694 | #endif |
| 4695 | } else { |
| 4696 | switch (newaddr->sa_family) { |
| 4697 | #ifdef INET |
| 4698 | case AF_INET: |
| 4699 | net->mtu = SCTP_DEFAULT_MTU; |
| 4700 | break; |
| 4701 | #endif |
| 4702 | #ifdef INET6 |
| 4703 | case AF_INET6: |
| 4704 | net->mtu = 1280; |
| 4705 | break; |
| 4706 | #endif |
| 4707 | #if defined(__Userspace__) |
| 4708 | case AF_CONN: |
| 4709 | net->mtu = 1280; |
| 4710 | break; |
| 4711 | #endif |
| 4712 | default: |
| 4713 | break; |
| 4714 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4715 | } |
tuexen | e81177b | 2011-11-15 20:48:08 +0000 | [diff] [blame] | 4716 | } |
t00fcxen | 1ce83bb | 2014-07-11 07:17:36 +0000 | [diff] [blame] | 4717 | #if defined(INET) || defined(INET6) |
tuexen | e81177b | 2011-11-15 20:48:08 +0000 | [diff] [blame] | 4718 | if (net->port) { |
| 4719 | net->mtu -= (uint32_t)sizeof(struct udphdr); |
| 4720 | } |
t00fcxen | 5ab37c8 | 2014-06-30 20:55:44 +0000 | [diff] [blame] | 4721 | #endif |
tuexen | e81177b | 2011-11-15 20:48:08 +0000 | [diff] [blame] | 4722 | if (from == SCTP_ALLOC_ASOC) { |
| 4723 | stcb->asoc.smallest_mtu = net->mtu; |
| 4724 | } |
| 4725 | if (stcb->asoc.smallest_mtu > net->mtu) { |
Michael Tuexen | 923f735 | 2016-05-01 23:55:35 +0200 | [diff] [blame] | 4726 | sctp_pathmtu_adjustment(stcb, net->mtu); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4727 | } |
| 4728 | #ifdef INET6 |
| 4729 | #ifdef SCTP_EMBEDDED_V6_SCOPE |
| 4730 | if (newaddr->sa_family == AF_INET6) { |
| 4731 | struct sockaddr_in6 *sin6; |
| 4732 | |
| 4733 | sin6 = (struct sockaddr_in6 *)&net->ro._l_addr; |
| 4734 | #ifdef SCTP_KAME |
| 4735 | (void)sa6_recoverscope(sin6); |
| 4736 | #else |
| 4737 | (void)in6_recoverscope(sin6, &sin6->sin6_addr, NULL); |
| 4738 | #endif /* SCTP_KAME */ |
| 4739 | } |
| 4740 | #endif /* SCTP_EMBEDDED_V6_SCOPE */ |
| 4741 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4742 | |
| 4743 | /* JRS - Use the congestion control given in the CC module */ |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 4744 | if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4745 | (*stcb->asoc.cc_functions.sctp_set_initial_cc_param)(stcb, net); |
| 4746 | |
| 4747 | /* |
| 4748 | * CMT: CUC algo - set find_pseudo_cumack to TRUE (1) at beginning |
| 4749 | * of assoc (2005/06/27, iyengar@cis.udel.edu) |
| 4750 | */ |
| 4751 | net->find_pseudo_cumack = 1; |
| 4752 | net->find_rtx_pseudo_cumack = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4753 | #if defined(__FreeBSD__) |
| 4754 | /* Choose an initial flowid. */ |
| 4755 | net->flowid = stcb->asoc.my_vtag ^ |
| 4756 | ntohs(stcb->rport) ^ |
| 4757 | ntohs(stcb->sctp_ep->sctp_lport); |
Michael Tuexen | 15f35f8 | 2016-06-07 09:19:50 +0200 | [diff] [blame] | 4758 | net->flowtype = M_HASHTYPE_OPAQUE_HASH; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4759 | #endif |
| 4760 | if (netp) { |
| 4761 | *netp = net; |
| 4762 | } |
| 4763 | netfirst = TAILQ_FIRST(&stcb->asoc.nets); |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4764 | #if defined(__FreeBSD__) |
| 4765 | if (net->ro.ro_nh == NULL) { |
| 4766 | #else |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4767 | if (net->ro.ro_rt == NULL) { |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4768 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4769 | /* Since we have no route put it at the back */ |
| 4770 | TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next); |
| 4771 | } else if (netfirst == NULL) { |
| 4772 | /* We are the first one in the pool. */ |
| 4773 | TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next); |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4774 | #if defined(__FreeBSD__) |
| 4775 | } else if (netfirst->ro.ro_nh == NULL) { |
| 4776 | #else |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4777 | } else if (netfirst->ro.ro_rt == NULL) { |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4778 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4779 | /* |
| 4780 | * First one has NO route. Place this one ahead of the first |
| 4781 | * one. |
| 4782 | */ |
| 4783 | TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next); |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4784 | #if defined(__FreeBSD__) |
| 4785 | } else if (net->ro.ro_nh->nh_ifp != netfirst->ro.ro_nh->nh_ifp) { |
| 4786 | #else |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4787 | } 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] | 4788 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4789 | /* |
| 4790 | * This one has a different interface than the one at the |
| 4791 | * top of the list. Place it ahead. |
| 4792 | */ |
| 4793 | TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4794 | } else { |
| 4795 | /* |
| 4796 | * Ok we have the same interface as the first one. Move |
| 4797 | * forward until we find either a) one with a NULL route... |
| 4798 | * insert ahead of that b) one with a different ifp.. insert |
| 4799 | * after that. c) end of the list.. insert at the tail. |
| 4800 | */ |
| 4801 | struct sctp_nets *netlook; |
| 4802 | |
| 4803 | do { |
| 4804 | netlook = TAILQ_NEXT(netfirst, sctp_next); |
| 4805 | if (netlook == NULL) { |
| 4806 | /* End of the list */ |
| 4807 | TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next); |
| 4808 | break; |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4809 | #if defined(__FreeBSD__) |
| 4810 | } else if (netlook->ro.ro_nh == NULL) { |
| 4811 | #else |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4812 | } else if (netlook->ro.ro_rt == NULL) { |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4813 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4814 | /* next one has NO route */ |
| 4815 | TAILQ_INSERT_BEFORE(netfirst, net, sctp_next); |
| 4816 | break; |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4817 | #if defined(__FreeBSD__) |
| 4818 | } 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] | 4819 | #else |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4820 | } 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] | 4821 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4822 | TAILQ_INSERT_AFTER(&stcb->asoc.nets, netlook, |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4823 | net, sctp_next); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4824 | break; |
| 4825 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4826 | /* Shift forward */ |
| 4827 | netfirst = netlook; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4828 | } while (netlook != NULL); |
| 4829 | } |
| 4830 | |
| 4831 | /* got to have a primary set */ |
| 4832 | if (stcb->asoc.primary_destination == 0) { |
| 4833 | stcb->asoc.primary_destination = net; |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4834 | #if defined(__FreeBSD__) |
| 4835 | } else if ((stcb->asoc.primary_destination->ro.ro_nh == NULL) && |
| 4836 | (net->ro.ro_nh) && |
| 4837 | #else |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4838 | } else if ((stcb->asoc.primary_destination->ro.ro_rt == NULL) && |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4839 | (net->ro.ro_rt) && |
| 4840 | #endif |
| 4841 | ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4842 | /* No route to current primary adopt new primary */ |
| 4843 | stcb->asoc.primary_destination = net; |
| 4844 | } |
| 4845 | /* Validate primary is first */ |
| 4846 | net = TAILQ_FIRST(&stcb->asoc.nets); |
| 4847 | if ((net != stcb->asoc.primary_destination) && |
| 4848 | (stcb->asoc.primary_destination)) { |
| 4849 | /* first one on the list is NOT the primary |
| 4850 | * sctp_cmpaddr() is much more efficient if |
| 4851 | * the primary is the first on the list, make it |
| 4852 | * so. |
| 4853 | */ |
| 4854 | TAILQ_REMOVE(&stcb->asoc.nets, |
| 4855 | stcb->asoc.primary_destination, sctp_next); |
| 4856 | TAILQ_INSERT_HEAD(&stcb->asoc.nets, |
| 4857 | stcb->asoc.primary_destination, sctp_next); |
| 4858 | } |
| 4859 | return (0); |
| 4860 | } |
| 4861 | |
| 4862 | |
| 4863 | static uint32_t |
| 4864 | sctp_aloc_a_assoc_id(struct sctp_inpcb *inp, struct sctp_tcb *stcb) |
| 4865 | { |
| 4866 | uint32_t id; |
| 4867 | struct sctpasochead *head; |
| 4868 | struct sctp_tcb *lstcb; |
| 4869 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4870 | try_again: |
| 4871 | if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
| 4872 | /* TSNH */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4873 | return (0); |
| 4874 | } |
| 4875 | /* |
| 4876 | * We don't allow assoc id to be one of SCTP_FUTURE_ASSOC, |
| 4877 | * SCTP_CURRENT_ASSOC and SCTP_ALL_ASSOC. |
| 4878 | */ |
| 4879 | if (inp->sctp_associd_counter <= SCTP_ALL_ASSOC) { |
| 4880 | inp->sctp_associd_counter = SCTP_ALL_ASSOC + 1; |
| 4881 | } |
| 4882 | id = inp->sctp_associd_counter; |
| 4883 | inp->sctp_associd_counter++; |
| 4884 | lstcb = sctp_findasoc_ep_asocid_locked(inp, (sctp_assoc_t)id, 0); |
| 4885 | if (lstcb) { |
| 4886 | goto try_again; |
| 4887 | } |
| 4888 | head = &inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(id, inp->hashasocidmark)]; |
| 4889 | LIST_INSERT_HEAD(head, stcb, sctp_tcbasocidhash); |
| 4890 | stcb->asoc.in_asocid_hash = 1; |
Michael Tuexen | be8e0eb | 2019-03-24 00:11:02 +0100 | [diff] [blame] | 4891 | return (id); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4892 | } |
| 4893 | |
| 4894 | /* |
| 4895 | * allocate an association and add it to the endpoint. The caller must be |
| 4896 | * careful to add all additional addresses once they are know right away or |
| 4897 | * else the assoc will be may experience a blackout scenario. |
| 4898 | */ |
| 4899 | struct sctp_tcb * |
| 4900 | sctp_aloc_assoc(struct sctp_inpcb *inp, struct sockaddr *firstaddr, |
Michael Tuexen | 49335e1 | 2015-12-06 17:31:45 +0100 | [diff] [blame] | 4901 | int *error, uint32_t override_tag, uint32_t vrf_id, |
Michael Tuexen | 923f735 | 2016-05-01 23:55:35 +0200 | [diff] [blame] | 4902 | uint16_t o_streams, uint16_t port, |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4903 | #if defined(__FreeBSD__) && __FreeBSD_version >= 500000 |
Michael Tuexen | bfb04f3 | 2019-07-14 14:42:17 +0200 | [diff] [blame] | 4904 | struct thread *p, |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4905 | #elif defined(__Windows__) |
Michael Tuexen | bfb04f3 | 2019-07-14 14:42:17 +0200 | [diff] [blame] | 4906 | PKTHREAD p, |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4907 | #else |
| 4908 | #if defined(__Userspace__) |
| 4909 | /* __Userspace__ NULL proc is going to be passed here. See sctp_lower_sosend */ |
| 4910 | #endif |
Michael Tuexen | bfb04f3 | 2019-07-14 14:42:17 +0200 | [diff] [blame] | 4911 | struct proc *p, |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4912 | #endif |
Michael Tuexen | bfb04f3 | 2019-07-14 14:42:17 +0200 | [diff] [blame] | 4913 | int initialize_auth_params) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4914 | { |
| 4915 | /* note the p argument is only valid in unbound sockets */ |
| 4916 | |
| 4917 | struct sctp_tcb *stcb; |
| 4918 | struct sctp_association *asoc; |
| 4919 | struct sctpasochead *head; |
| 4920 | uint16_t rport; |
| 4921 | int err; |
| 4922 | |
| 4923 | /* |
| 4924 | * Assumption made here: Caller has done a |
| 4925 | * sctp_findassociation_ep_addr(ep, addr's); to make sure the |
| 4926 | * address does not exist already. |
| 4927 | */ |
| 4928 | if (SCTP_BASE_INFO(ipi_count_asoc) >= SCTP_MAX_NUM_OF_ASOC) { |
| 4929 | /* Hit max assoc, sorry no more */ |
| 4930 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS); |
| 4931 | *error = ENOBUFS; |
| 4932 | return (NULL); |
| 4933 | } |
| 4934 | if (firstaddr == NULL) { |
| 4935 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 4936 | *error = EINVAL; |
| 4937 | return (NULL); |
| 4938 | } |
| 4939 | SCTP_INP_RLOCK(inp); |
| 4940 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) && |
| 4941 | ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) || |
| 4942 | (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) { |
| 4943 | /* |
| 4944 | * If its in the TCP pool, its NOT allowed to create an |
| 4945 | * association. The parent listener needs to call |
| 4946 | * sctp_aloc_assoc.. or the one-2-many socket. If a peeled |
| 4947 | * off, or connected one does this.. its an error. |
| 4948 | */ |
| 4949 | SCTP_INP_RUNLOCK(inp); |
| 4950 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 4951 | *error = EINVAL; |
| 4952 | return (NULL); |
| 4953 | } |
| 4954 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || |
| 4955 | (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) { |
| 4956 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) || |
| 4957 | (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED)) { |
| 4958 | SCTP_INP_RUNLOCK(inp); |
| 4959 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 4960 | *error = EINVAL; |
| 4961 | return (NULL); |
| 4962 | } |
| 4963 | } |
| 4964 | SCTPDBG(SCTP_DEBUG_PCB3, "Allocate an association for peer:"); |
| 4965 | #ifdef SCTP_DEBUG |
| 4966 | if (firstaddr) { |
| 4967 | SCTPDBG_ADDR(SCTP_DEBUG_PCB3, firstaddr); |
| 4968 | switch (firstaddr->sa_family) { |
| 4969 | #ifdef INET |
| 4970 | case AF_INET: |
| 4971 | SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n", |
| 4972 | ntohs(((struct sockaddr_in *)firstaddr)->sin_port)); |
| 4973 | break; |
| 4974 | #endif |
| 4975 | #ifdef INET6 |
| 4976 | case AF_INET6: |
| 4977 | SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n", |
| 4978 | ntohs(((struct sockaddr_in6 *)firstaddr)->sin6_port)); |
| 4979 | break; |
| 4980 | #endif |
tuexen | cc2b142 | 2012-07-10 21:50:32 +0000 | [diff] [blame] | 4981 | #if defined(__Userspace__) |
| 4982 | case AF_CONN: |
| 4983 | SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n", |
| 4984 | ntohs(((struct sockaddr_conn *)firstaddr)->sconn_port)); |
| 4985 | break; |
| 4986 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4987 | default: |
| 4988 | break; |
| 4989 | } |
| 4990 | } else { |
| 4991 | SCTPDBG(SCTP_DEBUG_PCB3,"None\n"); |
| 4992 | } |
| 4993 | #endif /* SCTP_DEBUG */ |
| 4994 | switch (firstaddr->sa_family) { |
| 4995 | #ifdef INET |
| 4996 | case AF_INET: |
| 4997 | { |
| 4998 | struct sockaddr_in *sin; |
| 4999 | |
| 5000 | sin = (struct sockaddr_in *)firstaddr; |
| 5001 | if ((ntohs(sin->sin_port) == 0) || |
| 5002 | (sin->sin_addr.s_addr == INADDR_ANY) || |
| 5003 | (sin->sin_addr.s_addr == INADDR_BROADCAST) || |
| 5004 | IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) { |
| 5005 | /* Invalid address */ |
| 5006 | SCTP_INP_RUNLOCK(inp); |
| 5007 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 5008 | *error = EINVAL; |
| 5009 | return (NULL); |
| 5010 | } |
| 5011 | rport = sin->sin_port; |
| 5012 | break; |
| 5013 | } |
| 5014 | #endif |
| 5015 | #ifdef INET6 |
| 5016 | case AF_INET6: |
| 5017 | { |
| 5018 | struct sockaddr_in6 *sin6; |
| 5019 | |
| 5020 | sin6 = (struct sockaddr_in6 *)firstaddr; |
| 5021 | if ((ntohs(sin6->sin6_port) == 0) || |
| 5022 | IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) || |
| 5023 | IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { |
| 5024 | /* Invalid address */ |
| 5025 | SCTP_INP_RUNLOCK(inp); |
| 5026 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 5027 | *error = EINVAL; |
| 5028 | return (NULL); |
| 5029 | } |
| 5030 | rport = sin6->sin6_port; |
| 5031 | break; |
| 5032 | } |
| 5033 | #endif |
tuexen | cc2b142 | 2012-07-10 21:50:32 +0000 | [diff] [blame] | 5034 | #if defined(__Userspace__) |
| 5035 | case AF_CONN: |
| 5036 | { |
| 5037 | struct sockaddr_conn *sconn; |
| 5038 | |
| 5039 | sconn = (struct sockaddr_conn *)firstaddr; |
| 5040 | if ((ntohs(sconn->sconn_port) == 0) || |
| 5041 | (sconn->sconn_addr == NULL)) { |
| 5042 | /* Invalid address */ |
| 5043 | SCTP_INP_RUNLOCK(inp); |
| 5044 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 5045 | *error = EINVAL; |
| 5046 | return (NULL); |
| 5047 | } |
| 5048 | rport = sconn->sconn_port; |
| 5049 | break; |
| 5050 | } |
| 5051 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5052 | default: |
| 5053 | /* not supported family type */ |
| 5054 | SCTP_INP_RUNLOCK(inp); |
| 5055 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 5056 | *error = EINVAL; |
| 5057 | return (NULL); |
| 5058 | } |
| 5059 | SCTP_INP_RUNLOCK(inp); |
| 5060 | if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) { |
| 5061 | /* |
| 5062 | * If you have not performed a bind, then we need to do the |
| 5063 | * ephemeral bind for you. |
| 5064 | */ |
Michael Tuexen | 3b4263d | 2020-06-06 19:52:50 +0200 | [diff] [blame^] | 5065 | if ((err = sctp_inpcb_bind(inp->sctp_socket, NULL, NULL, p))) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5066 | /* bind error, probably perm */ |
| 5067 | *error = err; |
| 5068 | return (NULL); |
| 5069 | } |
| 5070 | } |
| 5071 | stcb = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_asoc), struct sctp_tcb); |
| 5072 | if (stcb == NULL) { |
| 5073 | /* out of memory? */ |
| 5074 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM); |
| 5075 | *error = ENOMEM; |
| 5076 | return (NULL); |
| 5077 | } |
| 5078 | SCTP_INCR_ASOC_COUNT(); |
| 5079 | |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 5080 | memset(stcb, 0, sizeof(*stcb)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5081 | asoc = &stcb->asoc; |
| 5082 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5083 | SCTP_TCB_LOCK_INIT(stcb); |
| 5084 | SCTP_TCB_SEND_LOCK_INIT(stcb); |
| 5085 | stcb->rport = rport; |
| 5086 | /* setup back pointer's */ |
| 5087 | stcb->sctp_ep = inp; |
| 5088 | stcb->sctp_socket = inp->sctp_socket; |
Michael Tuexen | 49335e1 | 2015-12-06 17:31:45 +0100 | [diff] [blame] | 5089 | if ((err = sctp_init_asoc(inp, stcb, override_tag, vrf_id, o_streams))) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5090 | /* failed */ |
| 5091 | SCTP_TCB_LOCK_DESTROY(stcb); |
| 5092 | SCTP_TCB_SEND_LOCK_DESTROY(stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5093 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb); |
| 5094 | SCTP_DECR_ASOC_COUNT(); |
| 5095 | *error = err; |
| 5096 | return (NULL); |
| 5097 | } |
| 5098 | /* and the port */ |
| 5099 | SCTP_INP_INFO_WLOCK(); |
| 5100 | SCTP_INP_WLOCK(inp); |
| 5101 | if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_SOCKET_ALLGONE)) { |
| 5102 | /* inpcb freed while alloc going on */ |
| 5103 | SCTP_TCB_LOCK_DESTROY(stcb); |
| 5104 | SCTP_TCB_SEND_LOCK_DESTROY(stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5105 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb); |
| 5106 | SCTP_INP_WUNLOCK(inp); |
| 5107 | SCTP_INP_INFO_WUNLOCK(); |
| 5108 | SCTP_DECR_ASOC_COUNT(); |
| 5109 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 5110 | *error = EINVAL; |
| 5111 | return (NULL); |
| 5112 | } |
| 5113 | SCTP_TCB_LOCK(stcb); |
| 5114 | |
Michael Tuexen | be8e0eb | 2019-03-24 00:11:02 +0100 | [diff] [blame] | 5115 | asoc->assoc_id = sctp_aloc_a_assoc_id(inp, stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5116 | /* now that my_vtag is set, add it to the hash */ |
| 5117 | head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))]; |
| 5118 | /* put it in the bucket in the vtag hash of assoc's for the system */ |
| 5119 | LIST_INSERT_HEAD(head, stcb, sctp_asocs); |
| 5120 | SCTP_INP_INFO_WUNLOCK(); |
| 5121 | |
Michael Tuexen | 923f735 | 2016-05-01 23:55:35 +0200 | [diff] [blame] | 5122 | if ((err = 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] | 5123 | /* failure.. memory error? */ |
| 5124 | if (asoc->strmout) { |
| 5125 | SCTP_FREE(asoc->strmout, SCTP_M_STRMO); |
| 5126 | asoc->strmout = NULL; |
| 5127 | } |
| 5128 | if (asoc->mapping_array) { |
| 5129 | SCTP_FREE(asoc->mapping_array, SCTP_M_MAP); |
| 5130 | asoc->mapping_array = NULL; |
| 5131 | } |
| 5132 | if (asoc->nr_mapping_array) { |
| 5133 | SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP); |
| 5134 | asoc->nr_mapping_array = NULL; |
| 5135 | } |
| 5136 | SCTP_DECR_ASOC_COUNT(); |
t00fcxen | 2c2ff92 | 2013-11-03 14:03:35 +0000 | [diff] [blame] | 5137 | SCTP_TCB_UNLOCK(stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5138 | SCTP_TCB_LOCK_DESTROY(stcb); |
| 5139 | SCTP_TCB_SEND_LOCK_DESTROY(stcb); |
| 5140 | LIST_REMOVE(stcb, sctp_tcbasocidhash); |
| 5141 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb); |
| 5142 | SCTP_INP_WUNLOCK(inp); |
| 5143 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS); |
| 5144 | *error = ENOBUFS; |
| 5145 | return (NULL); |
| 5146 | } |
| 5147 | /* Init all the timers */ |
| 5148 | SCTP_OS_TIMER_INIT(&asoc->dack_timer.timer); |
| 5149 | SCTP_OS_TIMER_INIT(&asoc->strreset_timer.timer); |
| 5150 | SCTP_OS_TIMER_INIT(&asoc->asconf_timer.timer); |
| 5151 | SCTP_OS_TIMER_INIT(&asoc->shut_guard_timer.timer); |
| 5152 | SCTP_OS_TIMER_INIT(&asoc->autoclose_timer.timer); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5153 | SCTP_OS_TIMER_INIT(&asoc->delete_prim_timer.timer); |
| 5154 | |
| 5155 | LIST_INSERT_HEAD(&inp->sctp_asoc_list, stcb, sctp_tcblist); |
| 5156 | /* now file the port under the hash as well */ |
| 5157 | if (inp->sctp_tcbhash != NULL) { |
| 5158 | head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(stcb->rport, |
| 5159 | inp->sctp_hashmark)]; |
| 5160 | LIST_INSERT_HEAD(head, stcb, sctp_tcbhash); |
| 5161 | } |
Michael Tuexen | bfb04f3 | 2019-07-14 14:42:17 +0200 | [diff] [blame] | 5162 | if (initialize_auth_params == SCTP_INITIALIZE_AUTH_PARAMS) { |
| 5163 | sctp_initialize_auth_params(inp, stcb); |
| 5164 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5165 | SCTP_INP_WUNLOCK(inp); |
t00fcxen | 8fca7ff | 2012-09-05 18:58:42 +0000 | [diff] [blame] | 5166 | SCTPDBG(SCTP_DEBUG_PCB1, "Association %p now allocated\n", (void *)stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5167 | return (stcb); |
| 5168 | } |
| 5169 | |
| 5170 | |
| 5171 | void |
| 5172 | sctp_remove_net(struct sctp_tcb *stcb, struct sctp_nets *net) |
| 5173 | { |
Michael Tuexen | 21b55df | 2020-02-09 23:46:32 +0100 | [diff] [blame] | 5174 | struct sctp_inpcb *inp; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5175 | struct sctp_association *asoc; |
| 5176 | |
Michael Tuexen | 21b55df | 2020-02-09 23:46:32 +0100 | [diff] [blame] | 5177 | inp = stcb->sctp_ep; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5178 | asoc = &stcb->asoc; |
| 5179 | asoc->numnets--; |
| 5180 | TAILQ_REMOVE(&asoc->nets, net, sctp_next); |
| 5181 | if (net == asoc->primary_destination) { |
| 5182 | /* Reset primary */ |
| 5183 | struct sctp_nets *lnet; |
| 5184 | |
| 5185 | lnet = TAILQ_FIRST(&asoc->nets); |
| 5186 | /* Mobility adaptation |
| 5187 | Ideally, if deleted destination is the primary, it becomes |
| 5188 | a fast retransmission trigger by the subsequent SET PRIMARY. |
| 5189 | (by micchie) |
| 5190 | */ |
| 5191 | if (sctp_is_mobility_feature_on(stcb->sctp_ep, |
t00fcxen | 8dd95b8 | 2014-07-11 20:41:28 +0000 | [diff] [blame] | 5192 | SCTP_MOBILITY_BASE) || |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5193 | sctp_is_mobility_feature_on(stcb->sctp_ep, |
t00fcxen | 8dd95b8 | 2014-07-11 20:41:28 +0000 | [diff] [blame] | 5194 | SCTP_MOBILITY_FASTHANDOFF)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5195 | SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: primary dst is deleting\n"); |
| 5196 | if (asoc->deleted_primary != NULL) { |
| 5197 | SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: deleted primary may be already stored\n"); |
| 5198 | goto out; |
| 5199 | } |
| 5200 | asoc->deleted_primary = net; |
| 5201 | atomic_add_int(&net->ref_count, 1); |
| 5202 | memset(&net->lastsa, 0, sizeof(net->lastsa)); |
| 5203 | memset(&net->lastsv, 0, sizeof(net->lastsv)); |
| 5204 | sctp_mobility_feature_on(stcb->sctp_ep, |
| 5205 | SCTP_MOBILITY_PRIM_DELETED); |
| 5206 | sctp_timer_start(SCTP_TIMER_TYPE_PRIM_DELETED, |
| 5207 | stcb->sctp_ep, stcb, NULL); |
| 5208 | } |
| 5209 | out: |
| 5210 | /* Try to find a confirmed primary */ |
| 5211 | asoc->primary_destination = sctp_find_alternate_net(stcb, lnet, 0); |
| 5212 | } |
| 5213 | if (net == asoc->last_data_chunk_from) { |
| 5214 | /* Reset primary */ |
| 5215 | asoc->last_data_chunk_from = TAILQ_FIRST(&asoc->nets); |
| 5216 | } |
| 5217 | if (net == asoc->last_control_chunk_from) { |
| 5218 | /* Clear net */ |
| 5219 | asoc->last_control_chunk_from = NULL; |
| 5220 | } |
| 5221 | if (net == stcb->asoc.alternate) { |
| 5222 | sctp_free_remote_addr(stcb->asoc.alternate); |
| 5223 | stcb->asoc.alternate = NULL; |
| 5224 | } |
Michael Tuexen | 21b55df | 2020-02-09 23:46:32 +0100 | [diff] [blame] | 5225 | sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net, |
| 5226 | SCTP_FROM_SCTP_PCB + SCTP_LOC_9); |
| 5227 | sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, |
| 5228 | SCTP_FROM_SCTP_PCB + SCTP_LOC_10); |
Michael Tuexen | f52d3df | 2020-02-11 19:24:34 +0100 | [diff] [blame] | 5229 | net->dest_state |= SCTP_ADDR_BEING_DELETED; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5230 | sctp_free_remote_addr(net); |
| 5231 | } |
| 5232 | |
| 5233 | /* |
| 5234 | * remove a remote endpoint address from an association, it will fail if the |
| 5235 | * address does not exist. |
| 5236 | */ |
| 5237 | int |
| 5238 | sctp_del_remote_addr(struct sctp_tcb *stcb, struct sockaddr *remaddr) |
| 5239 | { |
| 5240 | /* |
| 5241 | * Here we need to remove a remote address. This is quite simple, we |
| 5242 | * first find it in the list of address for the association |
| 5243 | * (tasoc->asoc.nets) and then if it is there, we do a LIST_REMOVE |
| 5244 | * on that item. Note we do not allow it to be removed if there are |
| 5245 | * no other addresses. |
| 5246 | */ |
| 5247 | struct sctp_association *asoc; |
| 5248 | struct sctp_nets *net, *nnet; |
| 5249 | |
| 5250 | asoc = &stcb->asoc; |
| 5251 | |
| 5252 | /* locate the address */ |
| 5253 | TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) { |
| 5254 | if (net->ro._l_addr.sa.sa_family != remaddr->sa_family) { |
| 5255 | continue; |
| 5256 | } |
| 5257 | if (sctp_cmpaddr((struct sockaddr *)&net->ro._l_addr, |
| 5258 | remaddr)) { |
| 5259 | /* we found the guy */ |
| 5260 | if (asoc->numnets < 2) { |
| 5261 | /* Must have at LEAST two remote addresses */ |
| 5262 | return (-1); |
| 5263 | } else { |
| 5264 | sctp_remove_net(stcb, net); |
| 5265 | return (0); |
| 5266 | } |
| 5267 | } |
| 5268 | } |
| 5269 | /* not found. */ |
| 5270 | return (-2); |
| 5271 | } |
| 5272 | |
| 5273 | void |
| 5274 | sctp_delete_from_timewait(uint32_t tag, uint16_t lport, uint16_t rport) |
| 5275 | { |
| 5276 | struct sctpvtaghead *chain; |
| 5277 | struct sctp_tagblock *twait_block; |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 5278 | int found = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5279 | int i; |
| 5280 | |
| 5281 | chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)]; |
t00fcxen | 14f8fbb | 2013-06-25 09:32:47 +0000 | [diff] [blame] | 5282 | LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) { |
| 5283 | for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) { |
| 5284 | if ((twait_block->vtag_block[i].v_tag == tag) && |
| 5285 | (twait_block->vtag_block[i].lport == lport) && |
| 5286 | (twait_block->vtag_block[i].rport == rport)) { |
| 5287 | twait_block->vtag_block[i].tv_sec_at_expire = 0; |
| 5288 | twait_block->vtag_block[i].v_tag = 0; |
| 5289 | twait_block->vtag_block[i].lport = 0; |
| 5290 | twait_block->vtag_block[i].rport = 0; |
| 5291 | found = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5292 | break; |
t00fcxen | 14f8fbb | 2013-06-25 09:32:47 +0000 | [diff] [blame] | 5293 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5294 | } |
t00fcxen | 14f8fbb | 2013-06-25 09:32:47 +0000 | [diff] [blame] | 5295 | if (found) |
| 5296 | break; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5297 | } |
| 5298 | } |
| 5299 | |
| 5300 | int |
| 5301 | sctp_is_in_timewait(uint32_t tag, uint16_t lport, uint16_t rport) |
| 5302 | { |
| 5303 | struct sctpvtaghead *chain; |
| 5304 | struct sctp_tagblock *twait_block; |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 5305 | int found = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5306 | int i; |
| 5307 | |
| 5308 | SCTP_INP_INFO_WLOCK(); |
| 5309 | chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)]; |
t00fcxen | 14f8fbb | 2013-06-25 09:32:47 +0000 | [diff] [blame] | 5310 | LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) { |
| 5311 | for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) { |
| 5312 | if ((twait_block->vtag_block[i].v_tag == tag) && |
| 5313 | (twait_block->vtag_block[i].lport == lport) && |
| 5314 | (twait_block->vtag_block[i].rport == rport)) { |
| 5315 | found = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5316 | break; |
t00fcxen | 14f8fbb | 2013-06-25 09:32:47 +0000 | [diff] [blame] | 5317 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5318 | } |
t00fcxen | 14f8fbb | 2013-06-25 09:32:47 +0000 | [diff] [blame] | 5319 | if (found) |
| 5320 | break; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5321 | } |
| 5322 | SCTP_INP_INFO_WUNLOCK(); |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 5323 | return (found); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5324 | } |
| 5325 | |
| 5326 | |
| 5327 | void |
| 5328 | sctp_add_vtag_to_timewait(uint32_t tag, uint32_t time, uint16_t lport, uint16_t rport) |
| 5329 | { |
| 5330 | struct sctpvtaghead *chain; |
| 5331 | struct sctp_tagblock *twait_block; |
| 5332 | struct timeval now; |
| 5333 | int set, i; |
| 5334 | |
| 5335 | if (time == 0) { |
| 5336 | /* Its disabled */ |
| 5337 | return; |
| 5338 | } |
| 5339 | (void)SCTP_GETTIME_TIMEVAL(&now); |
| 5340 | chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)]; |
| 5341 | set = 0; |
t00fcxen | 14f8fbb | 2013-06-25 09:32:47 +0000 | [diff] [blame] | 5342 | LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5343 | /* Block(s) present, lets find space, and expire on the fly */ |
t00fcxen | 14f8fbb | 2013-06-25 09:32:47 +0000 | [diff] [blame] | 5344 | for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) { |
| 5345 | if ((twait_block->vtag_block[i].v_tag == 0) && |
| 5346 | !set) { |
| 5347 | twait_block->vtag_block[i].tv_sec_at_expire = |
| 5348 | now.tv_sec + time; |
| 5349 | twait_block->vtag_block[i].v_tag = tag; |
| 5350 | twait_block->vtag_block[i].lport = lport; |
| 5351 | twait_block->vtag_block[i].rport = rport; |
| 5352 | set = 1; |
| 5353 | } else if ((twait_block->vtag_block[i].v_tag) && |
| 5354 | ((long)twait_block->vtag_block[i].tv_sec_at_expire < now.tv_sec)) { |
| 5355 | /* Audit expires this guy */ |
| 5356 | twait_block->vtag_block[i].tv_sec_at_expire = 0; |
| 5357 | twait_block->vtag_block[i].v_tag = 0; |
| 5358 | twait_block->vtag_block[i].lport = 0; |
| 5359 | twait_block->vtag_block[i].rport = 0; |
| 5360 | if (set == 0) { |
| 5361 | /* Reuse it for my new tag */ |
| 5362 | twait_block->vtag_block[i].tv_sec_at_expire = now.tv_sec + time; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5363 | twait_block->vtag_block[i].v_tag = tag; |
| 5364 | twait_block->vtag_block[i].lport = lport; |
| 5365 | twait_block->vtag_block[i].rport = rport; |
| 5366 | set = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5367 | } |
| 5368 | } |
t00fcxen | 14f8fbb | 2013-06-25 09:32:47 +0000 | [diff] [blame] | 5369 | } |
| 5370 | if (set) { |
| 5371 | /* |
| 5372 | * We only do up to the block where we can |
| 5373 | * place our tag for audits |
| 5374 | */ |
| 5375 | break; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5376 | } |
| 5377 | } |
| 5378 | /* Need to add a new block to chain */ |
| 5379 | if (!set) { |
| 5380 | SCTP_MALLOC(twait_block, struct sctp_tagblock *, |
| 5381 | sizeof(struct sctp_tagblock), SCTP_M_TIMW); |
| 5382 | if (twait_block == NULL) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5383 | return; |
| 5384 | } |
| 5385 | memset(twait_block, 0, sizeof(struct sctp_tagblock)); |
| 5386 | LIST_INSERT_HEAD(chain, twait_block, sctp_nxt_tagblock); |
| 5387 | twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec + time; |
| 5388 | twait_block->vtag_block[0].v_tag = tag; |
| 5389 | twait_block->vtag_block[0].lport = lport; |
| 5390 | twait_block->vtag_block[0].rport = rport; |
| 5391 | } |
| 5392 | } |
| 5393 | |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 5394 | void |
| 5395 | sctp_clean_up_stream(struct sctp_tcb *stcb, struct sctp_readhead *rh) |
| 5396 | { |
| 5397 | struct sctp_tmit_chunk *chk, *nchk; |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 5398 | struct sctp_queued_to_read *control, *ncontrol; |
| 5399 | |
| 5400 | TAILQ_FOREACH_SAFE(control, rh, next_instrm, ncontrol) { |
| 5401 | TAILQ_REMOVE(rh, control, next_instrm); |
| 5402 | control->on_strm_q = 0; |
| 5403 | if (control->on_read_q == 0) { |
| 5404 | sctp_free_remote_addr(control->whoFrom); |
| 5405 | if (control->data) { |
| 5406 | sctp_m_freem(control->data); |
| 5407 | control->data = NULL; |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 5408 | } |
| 5409 | } |
| 5410 | /* Reassembly free? */ |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 5411 | TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) { |
| 5412 | TAILQ_REMOVE(&control->reasm, chk, sctp_next); |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 5413 | if (chk->data) { |
| 5414 | sctp_m_freem(chk->data); |
| 5415 | chk->data = NULL; |
| 5416 | } |
| 5417 | if (chk->holds_key_ref) |
| 5418 | sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); |
| 5419 | sctp_free_remote_addr(chk->whoTo); |
| 5420 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); |
| 5421 | SCTP_DECR_CHK_COUNT(); |
| 5422 | /*sa_ignore FREED_MEMORY*/ |
| 5423 | } |
| 5424 | /* |
| 5425 | * We don't free the address here |
| 5426 | * since all the net's were freed |
| 5427 | * above. |
| 5428 | */ |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 5429 | if (control->on_read_q == 0) { |
| 5430 | sctp_free_a_readq(stcb, control); |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 5431 | } |
| 5432 | } |
| 5433 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5434 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5435 | /*- |
| 5436 | * Free the association after un-hashing the remote port. This |
| 5437 | * function ALWAYS returns holding NO LOCK on the stcb. It DOES |
| 5438 | * expect that the input to this function IS a locked TCB. |
| 5439 | * It will return 0, if it did NOT destroy the association (instead |
| 5440 | * it unlocks it. It will return NON-zero if it either destroyed the |
| 5441 | * association OR the association is already destroyed. |
| 5442 | */ |
| 5443 | int |
| 5444 | sctp_free_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int from_inpcbfree, int from_location) |
| 5445 | { |
| 5446 | int i; |
| 5447 | struct sctp_association *asoc; |
| 5448 | struct sctp_nets *net, *nnet; |
| 5449 | struct sctp_laddr *laddr, *naddr; |
| 5450 | struct sctp_tmit_chunk *chk, *nchk; |
| 5451 | struct sctp_asconf_addr *aparam, *naparam; |
| 5452 | struct sctp_asconf_ack *aack, *naack; |
| 5453 | struct sctp_stream_reset_list *strrst, *nstrrst; |
| 5454 | struct sctp_queued_to_read *sq, *nsq; |
| 5455 | struct sctp_stream_queue_pending *sp, *nsp; |
| 5456 | sctp_sharedkey_t *shared_key, *nshared_key; |
| 5457 | struct socket *so; |
| 5458 | |
| 5459 | /* first, lets purge the entry from the hash table. */ |
| 5460 | #if defined(__APPLE__) |
| 5461 | sctp_lock_assert(SCTP_INP_SO(inp)); |
| 5462 | #endif |
| 5463 | |
| 5464 | #ifdef SCTP_LOG_CLOSING |
| 5465 | sctp_log_closing(inp, stcb, 6); |
| 5466 | #endif |
| 5467 | if (stcb->asoc.state == 0) { |
| 5468 | #ifdef SCTP_LOG_CLOSING |
| 5469 | sctp_log_closing(inp, NULL, 7); |
| 5470 | #endif |
| 5471 | /* there is no asoc, really TSNH :-0 */ |
| 5472 | return (1); |
| 5473 | } |
| 5474 | if (stcb->asoc.alternate) { |
| 5475 | sctp_free_remote_addr(stcb->asoc.alternate); |
| 5476 | stcb->asoc.alternate = NULL; |
| 5477 | } |
| 5478 | #if !defined(__APPLE__) /* TEMP: moved to below */ |
| 5479 | /* TEMP CODE */ |
| 5480 | if (stcb->freed_from_where == 0) { |
| 5481 | /* Only record the first place free happened from */ |
| 5482 | stcb->freed_from_where = from_location; |
| 5483 | } |
| 5484 | /* TEMP CODE */ |
| 5485 | #endif |
| 5486 | |
| 5487 | asoc = &stcb->asoc; |
| 5488 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || |
| 5489 | (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) |
| 5490 | /* nothing around */ |
| 5491 | so = NULL; |
| 5492 | else |
| 5493 | so = inp->sctp_socket; |
| 5494 | |
| 5495 | /* |
| 5496 | * We used timer based freeing if a reader or writer is in the way. |
| 5497 | * So we first check if we are actually being called from a timer, |
| 5498 | * if so we abort early if a reader or writer is still in the way. |
| 5499 | */ |
| 5500 | if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) && |
| 5501 | (from_inpcbfree == SCTP_NORMAL_PROC)) { |
| 5502 | /* |
| 5503 | * is it the timer driving us? if so are the reader/writers |
| 5504 | * gone? |
| 5505 | */ |
| 5506 | if (stcb->asoc.refcnt) { |
| 5507 | /* nope, reader or writer in the way */ |
| 5508 | sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL); |
| 5509 | /* no asoc destroyed */ |
| 5510 | SCTP_TCB_UNLOCK(stcb); |
| 5511 | #ifdef SCTP_LOG_CLOSING |
| 5512 | sctp_log_closing(inp, stcb, 8); |
| 5513 | #endif |
| 5514 | return (0); |
| 5515 | } |
| 5516 | } |
Michael Tuexen | 61f6683 | 2020-02-09 23:15:19 +0100 | [diff] [blame] | 5517 | /* Now clean up any other timers */ |
Michael Tuexen | 5f97639 | 2020-02-11 21:20:07 +0100 | [diff] [blame] | 5518 | sctp_stop_association_timers(stcb, false); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5519 | /* Now the read queue needs to be cleaned up (only once) */ |
| 5520 | if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0) { |
Michael Tuexen | 348a36c | 2018-08-13 16:24:47 +0200 | [diff] [blame] | 5521 | SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_ABOUT_TO_BE_FREED); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5522 | SCTP_INP_READ_LOCK(inp); |
| 5523 | TAILQ_FOREACH(sq, &inp->read_queue, next) { |
| 5524 | if (sq->stcb == stcb) { |
| 5525 | sq->do_not_ref_stcb = 1; |
| 5526 | sq->sinfo_cumtsn = stcb->asoc.cumulative_tsn; |
| 5527 | /* If there is no end, there never |
| 5528 | * will be now. |
| 5529 | */ |
| 5530 | if (sq->end_added == 0) { |
| 5531 | /* Held for PD-API clear that. */ |
| 5532 | sq->pdapi_aborted = 1; |
| 5533 | sq->held_length = 0; |
| 5534 | if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT) && (so != NULL)) { |
| 5535 | /* |
| 5536 | * Need to add a PD-API aborted indication. |
| 5537 | * Setting the control_pdapi assures that it will |
| 5538 | * be added right after this msg. |
| 5539 | */ |
| 5540 | uint32_t strseq; |
| 5541 | stcb->asoc.control_pdapi = sq; |
Michael Tuexen | 00657ac | 2016-12-07 21:53:26 +0100 | [diff] [blame] | 5542 | strseq = (sq->sinfo_stream << 16) | (sq->mid & 0x0000ffff); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5543 | sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION, |
| 5544 | stcb, |
| 5545 | SCTP_PARTIAL_DELIVERY_ABORTED, |
| 5546 | (void *)&strseq, |
| 5547 | SCTP_SO_LOCKED); |
| 5548 | stcb->asoc.control_pdapi = NULL; |
| 5549 | } |
| 5550 | } |
| 5551 | /* Add an end to wake them */ |
| 5552 | sq->end_added = 1; |
| 5553 | } |
| 5554 | } |
| 5555 | SCTP_INP_READ_UNLOCK(inp); |
| 5556 | if (stcb->block_entry) { |
| 5557 | SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PCB, ECONNRESET); |
| 5558 | stcb->block_entry->error = ECONNRESET; |
| 5559 | stcb->block_entry = NULL; |
| 5560 | } |
| 5561 | } |
| 5562 | if ((stcb->asoc.refcnt) || (stcb->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE)) { |
| 5563 | /* Someone holds a reference OR the socket is unaccepted yet. |
| 5564 | */ |
| 5565 | if ((stcb->asoc.refcnt) || |
| 5566 | (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || |
| 5567 | (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) { |
Michael Tuexen | 348a36c | 2018-08-13 16:24:47 +0200 | [diff] [blame] | 5568 | SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5569 | sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL); |
| 5570 | } |
| 5571 | SCTP_TCB_UNLOCK(stcb); |
| 5572 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || |
| 5573 | (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) |
| 5574 | /* nothing around */ |
| 5575 | so = NULL; |
| 5576 | if (so) { |
| 5577 | /* Wake any reader/writers */ |
| 5578 | sctp_sorwakeup(inp, so); |
| 5579 | sctp_sowwakeup(inp, so); |
| 5580 | } |
| 5581 | |
| 5582 | #ifdef SCTP_LOG_CLOSING |
| 5583 | sctp_log_closing(inp, stcb, 9); |
| 5584 | #endif |
| 5585 | /* no asoc destroyed */ |
| 5586 | return (0); |
| 5587 | } |
| 5588 | #ifdef SCTP_LOG_CLOSING |
| 5589 | sctp_log_closing(inp, stcb, 10); |
| 5590 | #endif |
| 5591 | /* When I reach here, no others want |
| 5592 | * to kill the assoc yet.. and I own |
| 5593 | * the lock. Now its possible an abort |
| 5594 | * comes in when I do the lock exchange |
| 5595 | * below to grab all the locks to do |
| 5596 | * the final take out. to prevent this |
| 5597 | * we increment the count, which will |
| 5598 | * start a timer and blow out above thus |
| 5599 | * assuring us that we hold exclusive |
| 5600 | * killing of the asoc. Note that |
| 5601 | * after getting back the TCB lock |
| 5602 | * we will go ahead and increment the |
| 5603 | * counter back up and stop any timer |
| 5604 | * a passing stranger may have started :-S |
| 5605 | */ |
| 5606 | if (from_inpcbfree == SCTP_NORMAL_PROC) { |
| 5607 | atomic_add_int(&stcb->asoc.refcnt, 1); |
| 5608 | |
| 5609 | SCTP_TCB_UNLOCK(stcb); |
| 5610 | SCTP_INP_INFO_WLOCK(); |
| 5611 | SCTP_INP_WLOCK(inp); |
| 5612 | SCTP_TCB_LOCK(stcb); |
| 5613 | } |
| 5614 | /* Double check the GONE flag */ |
| 5615 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || |
| 5616 | (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) |
| 5617 | /* nothing around */ |
| 5618 | so = NULL; |
| 5619 | |
Michael Tuexen | 1f49544 | 2020-03-25 16:41:21 +0100 | [diff] [blame] | 5620 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || |
| 5621 | (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { |
| 5622 | /* |
| 5623 | * For TCP type we need special handling when we are |
| 5624 | * connected. We also include the peel'ed off ones to. |
| 5625 | */ |
| 5626 | if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { |
| 5627 | inp->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED; |
| 5628 | inp->sctp_flags |= SCTP_PCB_FLAGS_WAS_CONNECTED; |
| 5629 | if (so) { |
| 5630 | SOCKBUF_LOCK(&so->so_rcv); |
| 5631 | so->so_state &= ~(SS_ISCONNECTING | |
| 5632 | SS_ISDISCONNECTING | |
| 5633 | SS_ISCONFIRMING | |
| 5634 | SS_ISCONNECTED); |
| 5635 | so->so_state |= SS_ISDISCONNECTED; |
| 5636 | #if defined(__APPLE__) |
| 5637 | socantrcvmore(so); |
| 5638 | #else |
| 5639 | socantrcvmore_locked(so); |
| 5640 | #endif |
| 5641 | socantsendmore(so); |
| 5642 | sctp_sowwakeup(inp, so); |
| 5643 | sctp_sorwakeup(inp, so); |
| 5644 | SCTP_SOWAKEUP(so); |
| 5645 | } |
| 5646 | } |
| 5647 | } |
| 5648 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5649 | /* Make it invalid too, that way if its |
| 5650 | * about to run it will abort and return. |
| 5651 | */ |
| 5652 | /* re-increment the lock */ |
| 5653 | if (from_inpcbfree == SCTP_NORMAL_PROC) { |
| 5654 | atomic_add_int(&stcb->asoc.refcnt, -1); |
| 5655 | } |
| 5656 | if (stcb->asoc.refcnt) { |
Michael Tuexen | 348a36c | 2018-08-13 16:24:47 +0200 | [diff] [blame] | 5657 | SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5658 | sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL); |
| 5659 | if (from_inpcbfree == SCTP_NORMAL_PROC) { |
| 5660 | SCTP_INP_INFO_WUNLOCK(); |
| 5661 | SCTP_INP_WUNLOCK(inp); |
| 5662 | } |
| 5663 | SCTP_TCB_UNLOCK(stcb); |
| 5664 | return (0); |
| 5665 | } |
| 5666 | asoc->state = 0; |
| 5667 | if (inp->sctp_tcbhash) { |
| 5668 | LIST_REMOVE(stcb, sctp_tcbhash); |
| 5669 | } |
| 5670 | if (stcb->asoc.in_asocid_hash) { |
| 5671 | LIST_REMOVE(stcb, sctp_tcbasocidhash); |
| 5672 | } |
| 5673 | /* Now lets remove it from the list of ALL associations in the EP */ |
| 5674 | LIST_REMOVE(stcb, sctp_tcblist); |
| 5675 | if (from_inpcbfree == SCTP_NORMAL_PROC) { |
| 5676 | SCTP_INP_INCR_REF(inp); |
| 5677 | SCTP_INP_WUNLOCK(inp); |
| 5678 | } |
| 5679 | /* pull from vtag hash */ |
| 5680 | LIST_REMOVE(stcb, sctp_asocs); |
| 5681 | sctp_add_vtag_to_timewait(asoc->my_vtag, SCTP_BASE_SYSCTL(sctp_vtag_time_wait), |
| 5682 | inp->sctp_lport, stcb->rport); |
| 5683 | |
| 5684 | /* Now restop the timers to be sure |
| 5685 | * this is paranoia at is finest! |
| 5686 | */ |
Michael Tuexen | 5f97639 | 2020-02-11 21:20:07 +0100 | [diff] [blame] | 5687 | sctp_stop_association_timers(stcb, true); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5688 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5689 | /* |
| 5690 | * The chunk lists and such SHOULD be empty but we check them just |
| 5691 | * in case. |
| 5692 | */ |
| 5693 | /* anything on the wheel needs to be removed */ |
Michael Tuexen | c3dd8e2 | 2019-03-26 14:09:18 +0100 | [diff] [blame] | 5694 | SCTP_TCB_SEND_LOCK(stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5695 | for (i = 0; i < asoc->streamoutcnt; i++) { |
| 5696 | struct sctp_stream_out *outs; |
| 5697 | |
| 5698 | outs = &asoc->strmout[i]; |
| 5699 | /* now clean up any chunks here */ |
| 5700 | TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) { |
Michael Tuexen | c6ae6c6 | 2016-08-07 15:17:05 +0200 | [diff] [blame] | 5701 | atomic_subtract_int(&asoc->stream_queue_cnt, 1); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5702 | TAILQ_REMOVE(&outs->outqueue, sp, next); |
Michael Tuexen | c3dd8e2 | 2019-03-26 14:09:18 +0100 | [diff] [blame] | 5703 | stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, outs, sp, 1); |
tuexen | 30ca817 | 2012-07-17 13:09:48 +0000 | [diff] [blame] | 5704 | sctp_free_spbufspace(stcb, asoc, sp); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5705 | if (sp->data) { |
| 5706 | if (so) { |
| 5707 | /* Still an open socket - report */ |
| 5708 | sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb, |
tuexen | da53ff0 | 2012-05-14 09:00:59 +0000 | [diff] [blame] | 5709 | 0, (void *)sp, SCTP_SO_LOCKED); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5710 | } |
| 5711 | if (sp->data) { |
| 5712 | sctp_m_freem(sp->data); |
| 5713 | sp->data = NULL; |
| 5714 | sp->tail_mbuf = NULL; |
tuexen | 30ca817 | 2012-07-17 13:09:48 +0000 | [diff] [blame] | 5715 | sp->length = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5716 | } |
| 5717 | } |
| 5718 | if (sp->net) { |
| 5719 | sctp_free_remote_addr(sp->net); |
| 5720 | sp->net = NULL; |
| 5721 | } |
tuexen | 30ca817 | 2012-07-17 13:09:48 +0000 | [diff] [blame] | 5722 | sctp_free_a_strmoq(stcb, sp, SCTP_SO_LOCKED); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5723 | } |
| 5724 | } |
Michael Tuexen | c3dd8e2 | 2019-03-26 14:09:18 +0100 | [diff] [blame] | 5725 | SCTP_TCB_SEND_UNLOCK(stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5726 | /*sa_ignore FREED_MEMORY*/ |
| 5727 | TAILQ_FOREACH_SAFE(strrst, &asoc->resetHead, next_resp, nstrrst) { |
| 5728 | TAILQ_REMOVE(&asoc->resetHead, strrst, next_resp); |
| 5729 | SCTP_FREE(strrst, SCTP_M_STRESET); |
| 5730 | } |
| 5731 | TAILQ_FOREACH_SAFE(sq, &asoc->pending_reply_queue, next, nsq) { |
| 5732 | TAILQ_REMOVE(&asoc->pending_reply_queue, sq, next); |
| 5733 | if (sq->data) { |
| 5734 | sctp_m_freem(sq->data); |
| 5735 | sq->data = NULL; |
| 5736 | } |
| 5737 | sctp_free_remote_addr(sq->whoFrom); |
| 5738 | sq->whoFrom = NULL; |
| 5739 | sq->stcb = NULL; |
| 5740 | /* Free the ctl entry */ |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 5741 | sctp_free_a_readq(stcb, sq); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5742 | /*sa_ignore FREED_MEMORY*/ |
| 5743 | } |
| 5744 | TAILQ_FOREACH_SAFE(chk, &asoc->free_chunks, sctp_next, nchk) { |
| 5745 | TAILQ_REMOVE(&asoc->free_chunks, chk, sctp_next); |
| 5746 | if (chk->data) { |
| 5747 | sctp_m_freem(chk->data); |
| 5748 | chk->data = NULL; |
| 5749 | } |
| 5750 | if (chk->holds_key_ref) |
| 5751 | sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); |
| 5752 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); |
| 5753 | SCTP_DECR_CHK_COUNT(); |
| 5754 | atomic_subtract_int(&SCTP_BASE_INFO(ipi_free_chunks), 1); |
| 5755 | asoc->free_chunk_cnt--; |
| 5756 | /*sa_ignore FREED_MEMORY*/ |
| 5757 | } |
| 5758 | /* pending send queue SHOULD be empty */ |
| 5759 | TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) { |
Michael Tuexen | 00657ac | 2016-12-07 21:53:26 +0100 | [diff] [blame] | 5760 | if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) { |
| 5761 | asoc->strmout[chk->rec.data.sid].chunks_on_queues--; |
t00fcxen | 8fcc514 | 2012-11-16 19:46:12 +0000 | [diff] [blame] | 5762 | #ifdef INVARIANTS |
| 5763 | } else { |
Michael Tuexen | 00657ac | 2016-12-07 21:53:26 +0100 | [diff] [blame] | 5764 | panic("No chunks on the queues for sid %u.", chk->rec.data.sid); |
t00fcxen | 8fcc514 | 2012-11-16 19:46:12 +0000 | [diff] [blame] | 5765 | #endif |
| 5766 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5767 | TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next); |
| 5768 | if (chk->data) { |
| 5769 | if (so) { |
| 5770 | /* Still a socket? */ |
tuexen | da53ff0 | 2012-05-14 09:00:59 +0000 | [diff] [blame] | 5771 | sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, |
| 5772 | 0, chk, SCTP_SO_LOCKED); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5773 | } |
| 5774 | if (chk->data) { |
| 5775 | sctp_m_freem(chk->data); |
| 5776 | chk->data = NULL; |
| 5777 | } |
| 5778 | } |
| 5779 | if (chk->holds_key_ref) |
| 5780 | sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); |
| 5781 | if (chk->whoTo) { |
| 5782 | sctp_free_remote_addr(chk->whoTo); |
| 5783 | chk->whoTo = NULL; |
| 5784 | } |
| 5785 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); |
| 5786 | SCTP_DECR_CHK_COUNT(); |
| 5787 | /*sa_ignore FREED_MEMORY*/ |
| 5788 | } |
| 5789 | /* sent queue SHOULD be empty */ |
| 5790 | TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) { |
t00fcxen | 8fcc514 | 2012-11-16 19:46:12 +0000 | [diff] [blame] | 5791 | if (chk->sent != SCTP_DATAGRAM_NR_ACKED) { |
Michael Tuexen | 00657ac | 2016-12-07 21:53:26 +0100 | [diff] [blame] | 5792 | if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) { |
| 5793 | asoc->strmout[chk->rec.data.sid].chunks_on_queues--; |
t00fcxen | 9ad9077 | 2012-11-07 22:19:57 +0000 | [diff] [blame] | 5794 | #ifdef INVARIANTS |
| 5795 | } else { |
Michael Tuexen | 00657ac | 2016-12-07 21:53:26 +0100 | [diff] [blame] | 5796 | panic("No chunks on the queues for sid %u.", chk->rec.data.sid); |
t00fcxen | 9ad9077 | 2012-11-07 22:19:57 +0000 | [diff] [blame] | 5797 | #endif |
| 5798 | } |
| 5799 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5800 | TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next); |
| 5801 | if (chk->data) { |
| 5802 | if (so) { |
| 5803 | /* Still a socket? */ |
tuexen | da53ff0 | 2012-05-14 09:00:59 +0000 | [diff] [blame] | 5804 | sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, |
| 5805 | 0, chk, SCTP_SO_LOCKED); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5806 | } |
| 5807 | if (chk->data) { |
| 5808 | sctp_m_freem(chk->data); |
| 5809 | chk->data = NULL; |
| 5810 | } |
| 5811 | } |
| 5812 | if (chk->holds_key_ref) |
| 5813 | sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); |
| 5814 | sctp_free_remote_addr(chk->whoTo); |
| 5815 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); |
| 5816 | SCTP_DECR_CHK_COUNT(); |
| 5817 | /*sa_ignore FREED_MEMORY*/ |
| 5818 | } |
t00fcxen | 8fcc514 | 2012-11-16 19:46:12 +0000 | [diff] [blame] | 5819 | #ifdef INVARIANTS |
| 5820 | for (i = 0; i < stcb->asoc.streamoutcnt; i++) { |
| 5821 | if (stcb->asoc.strmout[i].chunks_on_queues > 0) { |
| 5822 | panic("%u chunks left for stream %u.", stcb->asoc.strmout[i].chunks_on_queues, i); |
| 5823 | } |
| 5824 | } |
| 5825 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5826 | /* control queue MAY not be empty */ |
| 5827 | TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) { |
| 5828 | TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next); |
| 5829 | if (chk->data) { |
| 5830 | sctp_m_freem(chk->data); |
| 5831 | chk->data = NULL; |
| 5832 | } |
| 5833 | if (chk->holds_key_ref) |
| 5834 | sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); |
| 5835 | sctp_free_remote_addr(chk->whoTo); |
| 5836 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); |
| 5837 | SCTP_DECR_CHK_COUNT(); |
| 5838 | /*sa_ignore FREED_MEMORY*/ |
| 5839 | } |
| 5840 | /* ASCONF queue MAY not be empty */ |
| 5841 | TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) { |
| 5842 | TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next); |
| 5843 | if (chk->data) { |
| 5844 | sctp_m_freem(chk->data); |
| 5845 | chk->data = NULL; |
| 5846 | } |
| 5847 | if (chk->holds_key_ref) |
| 5848 | sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); |
| 5849 | sctp_free_remote_addr(chk->whoTo); |
| 5850 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); |
| 5851 | SCTP_DECR_CHK_COUNT(); |
| 5852 | /*sa_ignore FREED_MEMORY*/ |
| 5853 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5854 | if (asoc->mapping_array) { |
| 5855 | SCTP_FREE(asoc->mapping_array, SCTP_M_MAP); |
| 5856 | asoc->mapping_array = NULL; |
| 5857 | } |
| 5858 | if (asoc->nr_mapping_array) { |
| 5859 | SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP); |
| 5860 | asoc->nr_mapping_array = NULL; |
| 5861 | } |
| 5862 | /* the stream outs */ |
| 5863 | if (asoc->strmout) { |
| 5864 | SCTP_FREE(asoc->strmout, SCTP_M_STRMO); |
| 5865 | asoc->strmout = NULL; |
| 5866 | } |
| 5867 | asoc->strm_realoutsize = asoc->streamoutcnt = 0; |
| 5868 | if (asoc->strmin) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5869 | for (i = 0; i < asoc->streamincnt; i++) { |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 5870 | sctp_clean_up_stream(stcb, &asoc->strmin[i].inqueue); |
| 5871 | sctp_clean_up_stream(stcb, &asoc->strmin[i].uno_inqueue); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5872 | } |
| 5873 | SCTP_FREE(asoc->strmin, SCTP_M_STRMI); |
| 5874 | asoc->strmin = NULL; |
| 5875 | } |
| 5876 | asoc->streamincnt = 0; |
| 5877 | TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) { |
| 5878 | #ifdef INVARIANTS |
| 5879 | if (SCTP_BASE_INFO(ipi_count_raddr) == 0) { |
| 5880 | panic("no net's left alloc'ed, or list points to itself"); |
| 5881 | } |
| 5882 | #endif |
| 5883 | TAILQ_REMOVE(&asoc->nets, net, sctp_next); |
| 5884 | sctp_free_remote_addr(net); |
| 5885 | } |
| 5886 | LIST_FOREACH_SAFE(laddr, &asoc->sctp_restricted_addrs, sctp_nxt_addr, naddr) { |
| 5887 | /*sa_ignore FREED_MEMORY*/ |
| 5888 | sctp_remove_laddr(laddr); |
| 5889 | } |
| 5890 | |
| 5891 | /* pending asconf (address) parameters */ |
| 5892 | TAILQ_FOREACH_SAFE(aparam, &asoc->asconf_queue, next, naparam) { |
| 5893 | /*sa_ignore FREED_MEMORY*/ |
| 5894 | TAILQ_REMOVE(&asoc->asconf_queue, aparam, next); |
| 5895 | SCTP_FREE(aparam,SCTP_M_ASC_ADDR); |
| 5896 | } |
| 5897 | TAILQ_FOREACH_SAFE(aack, &asoc->asconf_ack_sent, next, naack) { |
| 5898 | /*sa_ignore FREED_MEMORY*/ |
| 5899 | TAILQ_REMOVE(&asoc->asconf_ack_sent, aack, next); |
| 5900 | if (aack->data != NULL) { |
| 5901 | sctp_m_freem(aack->data); |
| 5902 | } |
| 5903 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asconf_ack), aack); |
| 5904 | } |
| 5905 | /* clean up auth stuff */ |
| 5906 | if (asoc->local_hmacs) |
| 5907 | sctp_free_hmaclist(asoc->local_hmacs); |
| 5908 | if (asoc->peer_hmacs) |
| 5909 | sctp_free_hmaclist(asoc->peer_hmacs); |
| 5910 | |
| 5911 | if (asoc->local_auth_chunks) |
| 5912 | sctp_free_chunklist(asoc->local_auth_chunks); |
| 5913 | if (asoc->peer_auth_chunks) |
| 5914 | sctp_free_chunklist(asoc->peer_auth_chunks); |
| 5915 | |
| 5916 | sctp_free_authinfo(&asoc->authinfo); |
| 5917 | |
| 5918 | LIST_FOREACH_SAFE(shared_key, &asoc->shared_keys, next, nshared_key) { |
| 5919 | LIST_REMOVE(shared_key, next); |
| 5920 | sctp_free_sharedkey(shared_key); |
| 5921 | /*sa_ignore FREED_MEMORY*/ |
| 5922 | } |
| 5923 | |
| 5924 | /* Insert new items here :> */ |
| 5925 | |
| 5926 | /* Get rid of LOCK */ |
t00fcxen | 2c2ff92 | 2013-11-03 14:03:35 +0000 | [diff] [blame] | 5927 | SCTP_TCB_UNLOCK(stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5928 | SCTP_TCB_LOCK_DESTROY(stcb); |
| 5929 | SCTP_TCB_SEND_LOCK_DESTROY(stcb); |
| 5930 | if (from_inpcbfree == SCTP_NORMAL_PROC) { |
| 5931 | SCTP_INP_INFO_WUNLOCK(); |
| 5932 | SCTP_INP_RLOCK(inp); |
| 5933 | } |
| 5934 | #if defined(__APPLE__) /* TEMP CODE */ |
| 5935 | stcb->freed_from_where = from_location; |
| 5936 | #endif |
| 5937 | #ifdef SCTP_TRACK_FREED_ASOCS |
| 5938 | if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { |
| 5939 | /* now clean up the tasoc itself */ |
| 5940 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb); |
| 5941 | SCTP_DECR_ASOC_COUNT(); |
| 5942 | } else { |
| 5943 | LIST_INSERT_HEAD(&inp->sctp_asoc_free_list, stcb, sctp_tcblist); |
| 5944 | } |
| 5945 | #else |
| 5946 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb); |
| 5947 | SCTP_DECR_ASOC_COUNT(); |
| 5948 | #endif |
| 5949 | if (from_inpcbfree == SCTP_NORMAL_PROC) { |
| 5950 | if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { |
| 5951 | /* If its NOT the inp_free calling us AND |
| 5952 | * sctp_close as been called, we |
| 5953 | * call back... |
| 5954 | */ |
| 5955 | SCTP_INP_RUNLOCK(inp); |
| 5956 | /* This will start the kill timer (if we are |
| 5957 | * the last one) since we hold an increment yet. But |
| 5958 | * this is the only safe way to do this |
| 5959 | * since otherwise if the socket closes |
| 5960 | * at the same time we are here we might |
| 5961 | * collide in the cleanup. |
| 5962 | */ |
| 5963 | sctp_inpcb_free(inp, |
| 5964 | SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE, |
| 5965 | SCTP_CALLED_DIRECTLY_NOCMPSET); |
| 5966 | SCTP_INP_DECR_REF(inp); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5967 | } else { |
| 5968 | /* The socket is still open. */ |
| 5969 | SCTP_INP_DECR_REF(inp); |
Michael Tuexen | c6c994c | 2020-05-18 21:50:12 +0200 | [diff] [blame] | 5970 | SCTP_INP_RUNLOCK(inp); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5971 | } |
| 5972 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5973 | /* destroyed the asoc */ |
| 5974 | #ifdef SCTP_LOG_CLOSING |
| 5975 | sctp_log_closing(inp, NULL, 11); |
| 5976 | #endif |
| 5977 | return (1); |
| 5978 | } |
| 5979 | |
| 5980 | |
| 5981 | |
| 5982 | /* |
| 5983 | * determine if a destination is "reachable" based upon the addresses bound |
| 5984 | * to the current endpoint (e.g. only v4 or v6 currently bound) |
| 5985 | */ |
| 5986 | /* |
| 5987 | * FIX: if we allow assoc-level bindx(), then this needs to be fixed to use |
| 5988 | * assoc level v4/v6 flags, as the assoc *may* not have the same address |
| 5989 | * types bound as its endpoint |
| 5990 | */ |
| 5991 | int |
| 5992 | sctp_destination_is_reachable(struct sctp_tcb *stcb, struct sockaddr *destaddr) |
| 5993 | { |
| 5994 | struct sctp_inpcb *inp; |
| 5995 | int answer; |
| 5996 | |
| 5997 | /* |
| 5998 | * No locks here, the TCB, in all cases is already locked and an |
| 5999 | * assoc is up. There is either a INP lock by the caller applied (in |
| 6000 | * asconf case when deleting an address) or NOT in the HB case, |
| 6001 | * however if HB then the INP increment is up and the INP will not |
| 6002 | * be removed (on top of the fact that we have a TCB lock). So we |
| 6003 | * only want to read the sctp_flags, which is either bound-all or |
| 6004 | * not.. no protection needed since once an assoc is up you can't be |
| 6005 | * changing your binding. |
| 6006 | */ |
| 6007 | inp = stcb->sctp_ep; |
| 6008 | if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { |
| 6009 | /* if bound all, destination is not restricted */ |
| 6010 | /* |
| 6011 | * RRS: Question during lock work: Is this correct? If you |
| 6012 | * are bound-all you still might need to obey the V4--V6 |
| 6013 | * flags??? IMO this bound-all stuff needs to be removed! |
| 6014 | */ |
| 6015 | return (1); |
| 6016 | } |
| 6017 | /* NOTE: all "scope" checks are done when local addresses are added */ |
| 6018 | switch (destaddr->sa_family) { |
tuexen | 310f1bc | 2012-07-15 11:20:56 +0000 | [diff] [blame] | 6019 | #ifdef INET6 |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6020 | case AF_INET6: |
| 6021 | #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)) |
| 6022 | answer = inp->inp_vflag & INP_IPV6; |
| 6023 | #else |
| 6024 | answer = inp->ip_inp.inp.inp_vflag & INP_IPV6; |
| 6025 | #endif |
| 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: |
| 6030 | #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)) |
| 6031 | answer = inp->inp_vflag & INP_IPV4; |
| 6032 | #else |
| 6033 | answer = inp->ip_inp.inp.inp_vflag & INP_IPV4; |
| 6034 | #endif |
tuexen | 4657054 | 2012-07-14 20:37:24 +0000 | [diff] [blame] | 6035 | break; |
tuexen | 310f1bc | 2012-07-15 11:20:56 +0000 | [diff] [blame] | 6036 | #endif |
tuexen | 5d9b974 | 2012-07-11 21:52:01 +0000 | [diff] [blame] | 6037 | #if defined(__Userspace__) |
| 6038 | case AF_CONN: |
| 6039 | answer = inp->ip_inp.inp.inp_vflag & INP_CONN; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6040 | break; |
tuexen | 5d9b974 | 2012-07-11 21:52:01 +0000 | [diff] [blame] | 6041 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6042 | default: |
| 6043 | /* invalid family, so it's unreachable */ |
| 6044 | answer = 0; |
| 6045 | break; |
| 6046 | } |
| 6047 | return (answer); |
| 6048 | } |
| 6049 | |
| 6050 | /* |
| 6051 | * update the inp_vflags on an endpoint |
| 6052 | */ |
| 6053 | static void |
| 6054 | sctp_update_ep_vflag(struct sctp_inpcb *inp) |
| 6055 | { |
| 6056 | struct sctp_laddr *laddr; |
| 6057 | |
| 6058 | /* first clear the flag */ |
| 6059 | #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)) |
| 6060 | inp->inp_vflag = 0; |
| 6061 | #else |
| 6062 | inp->ip_inp.inp.inp_vflag = 0; |
| 6063 | #endif |
| 6064 | /* set the flag based on addresses on the ep list */ |
| 6065 | LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { |
| 6066 | if (laddr->ifa == NULL) { |
| 6067 | SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n", |
Saúl Ibarra Corretgé | 8b04b41 | 2015-09-26 09:53:06 +0200 | [diff] [blame] | 6068 | __func__); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6069 | continue; |
| 6070 | } |
| 6071 | |
| 6072 | if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) { |
| 6073 | continue; |
| 6074 | } |
| 6075 | switch (laddr->ifa->address.sa.sa_family) { |
| 6076 | #ifdef INET6 |
| 6077 | case AF_INET6: |
| 6078 | #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)) |
| 6079 | inp->inp_vflag |= INP_IPV6; |
| 6080 | #else |
| 6081 | inp->ip_inp.inp.inp_vflag |= INP_IPV6; |
| 6082 | #endif |
| 6083 | break; |
| 6084 | #endif |
| 6085 | #ifdef INET |
| 6086 | case AF_INET: |
| 6087 | #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)) |
| 6088 | inp->inp_vflag |= INP_IPV4; |
| 6089 | #else |
| 6090 | inp->ip_inp.inp.inp_vflag |= INP_IPV4; |
| 6091 | #endif |
| 6092 | break; |
| 6093 | #endif |
tuexen | 5d9b974 | 2012-07-11 21:52:01 +0000 | [diff] [blame] | 6094 | #if defined(__Userspace__) |
| 6095 | case AF_CONN: |
| 6096 | inp->ip_inp.inp.inp_vflag |= INP_CONN; |
| 6097 | break; |
| 6098 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6099 | default: |
| 6100 | break; |
| 6101 | } |
| 6102 | } |
| 6103 | } |
| 6104 | |
| 6105 | /* |
| 6106 | * Add the address to the endpoint local address list There is nothing to be |
| 6107 | * done if we are bound to all addresses |
| 6108 | */ |
| 6109 | void |
| 6110 | sctp_add_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa, uint32_t action) |
| 6111 | { |
| 6112 | struct sctp_laddr *laddr; |
Michael Tuexen | d75dc0c | 2016-03-28 21:38:43 +0200 | [diff] [blame] | 6113 | struct sctp_tcb *stcb; |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 6114 | int fnd, error = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6115 | |
| 6116 | fnd = 0; |
| 6117 | |
| 6118 | if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { |
| 6119 | /* You are already bound to all. You have it already */ |
| 6120 | return; |
| 6121 | } |
| 6122 | #ifdef INET6 |
| 6123 | if (ifa->address.sa.sa_family == AF_INET6) { |
| 6124 | if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) { |
| 6125 | /* Can't bind a non-useable addr. */ |
| 6126 | return; |
| 6127 | } |
| 6128 | } |
| 6129 | #endif |
| 6130 | /* first, is it already present? */ |
| 6131 | LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { |
| 6132 | if (laddr->ifa == ifa) { |
| 6133 | fnd = 1; |
| 6134 | break; |
| 6135 | } |
| 6136 | } |
| 6137 | |
| 6138 | if (fnd == 0) { |
| 6139 | /* Not in the ep list */ |
| 6140 | error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, action); |
| 6141 | if (error != 0) |
| 6142 | return; |
| 6143 | inp->laddr_count++; |
| 6144 | /* update inp_vflag flags */ |
| 6145 | switch (ifa->address.sa.sa_family) { |
| 6146 | #ifdef INET6 |
| 6147 | case AF_INET6: |
| 6148 | #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)) |
| 6149 | inp->inp_vflag |= INP_IPV6; |
| 6150 | #else |
| 6151 | inp->ip_inp.inp.inp_vflag |= INP_IPV6; |
| 6152 | #endif |
| 6153 | break; |
| 6154 | #endif |
tuexen | 310f1bc | 2012-07-15 11:20:56 +0000 | [diff] [blame] | 6155 | #ifdef INET |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6156 | case AF_INET: |
| 6157 | #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)) |
| 6158 | inp->inp_vflag |= INP_IPV4; |
| 6159 | #else |
| 6160 | inp->ip_inp.inp.inp_vflag |= INP_IPV4; |
| 6161 | #endif |
| 6162 | break; |
| 6163 | #endif |
tuexen | 5d9b974 | 2012-07-11 21:52:01 +0000 | [diff] [blame] | 6164 | #if defined(__Userspace__) |
| 6165 | case AF_CONN: |
| 6166 | inp->ip_inp.inp.inp_vflag |= INP_CONN; |
| 6167 | break; |
| 6168 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6169 | default: |
| 6170 | break; |
| 6171 | } |
Michael Tuexen | d75dc0c | 2016-03-28 21:38:43 +0200 | [diff] [blame] | 6172 | LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { |
| 6173 | sctp_add_local_addr_restricted(stcb, ifa); |
| 6174 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6175 | } |
| 6176 | return; |
| 6177 | } |
| 6178 | |
| 6179 | |
| 6180 | /* |
| 6181 | * select a new (hopefully reachable) destination net (should only be used |
| 6182 | * when we deleted an ep addr that is the only usable source address to reach |
| 6183 | * the destination net) |
| 6184 | */ |
| 6185 | static void |
| 6186 | sctp_select_primary_destination(struct sctp_tcb *stcb) |
| 6187 | { |
| 6188 | struct sctp_nets *net; |
| 6189 | |
| 6190 | TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { |
| 6191 | /* for now, we'll just pick the first reachable one we find */ |
| 6192 | if (net->dest_state & SCTP_ADDR_UNCONFIRMED) |
| 6193 | continue; |
| 6194 | if (sctp_destination_is_reachable(stcb, |
| 6195 | (struct sockaddr *)&net->ro._l_addr)) { |
| 6196 | /* found a reachable destination */ |
| 6197 | stcb->asoc.primary_destination = net; |
| 6198 | } |
| 6199 | } |
| 6200 | /* I can't there from here! ...we're gonna die shortly... */ |
| 6201 | } |
| 6202 | |
| 6203 | |
| 6204 | /* |
Michael Tuexen | 24e6c73 | 2016-02-19 12:28:21 +0100 | [diff] [blame] | 6205 | * Delete the address from the endpoint local address list. There is nothing |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6206 | * to be done if we are bound to all addresses |
| 6207 | */ |
| 6208 | void |
| 6209 | sctp_del_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa) |
| 6210 | { |
| 6211 | struct sctp_laddr *laddr; |
| 6212 | int fnd; |
| 6213 | |
| 6214 | fnd = 0; |
| 6215 | if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { |
| 6216 | /* You are already bound to all. You have it already */ |
| 6217 | return; |
| 6218 | } |
| 6219 | LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { |
| 6220 | if (laddr->ifa == ifa) { |
| 6221 | fnd = 1; |
| 6222 | break; |
| 6223 | } |
| 6224 | } |
| 6225 | if (fnd && (inp->laddr_count < 2)) { |
| 6226 | /* can't delete unless there are at LEAST 2 addresses */ |
| 6227 | return; |
| 6228 | } |
| 6229 | if (fnd) { |
| 6230 | /* |
| 6231 | * clean up any use of this address go through our |
| 6232 | * associations and clear any last_used_address that match |
| 6233 | * this one for each assoc, see if a new primary_destination |
| 6234 | * is needed |
| 6235 | */ |
| 6236 | struct sctp_tcb *stcb; |
| 6237 | |
| 6238 | /* clean up "next_addr_touse" */ |
| 6239 | if (inp->next_addr_touse == laddr) |
| 6240 | /* delete this address */ |
| 6241 | inp->next_addr_touse = NULL; |
| 6242 | |
| 6243 | /* clean up "last_used_address" */ |
| 6244 | LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { |
| 6245 | struct sctp_nets *net; |
Michael Tuexen | 24e6c73 | 2016-02-19 12:28:21 +0100 | [diff] [blame] | 6246 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6247 | SCTP_TCB_LOCK(stcb); |
| 6248 | if (stcb->asoc.last_used_address == laddr) |
| 6249 | /* delete this address */ |
| 6250 | stcb->asoc.last_used_address = NULL; |
| 6251 | /* Now spin through all the nets and purge any ref to laddr */ |
| 6252 | TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { |
Michael Tuexen | 24e6c73 | 2016-02-19 12:28:21 +0100 | [diff] [blame] | 6253 | if (net->ro._s_addr == laddr->ifa) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6254 | /* Yep, purge src address selected */ |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 6255 | #if defined(__FreeBSD__) |
| 6256 | RO_NHFREE(&net->ro); |
| 6257 | #else |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6258 | sctp_rtentry_t *rt; |
| 6259 | |
| 6260 | /* delete this address if cached */ |
| 6261 | rt = net->ro.ro_rt; |
| 6262 | if (rt != NULL) { |
| 6263 | RTFREE(rt); |
| 6264 | net->ro.ro_rt = NULL; |
| 6265 | } |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 6266 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6267 | sctp_free_ifa(net->ro._s_addr); |
| 6268 | net->ro._s_addr = NULL; |
| 6269 | net->src_addr_selected = 0; |
| 6270 | } |
| 6271 | } |
| 6272 | SCTP_TCB_UNLOCK(stcb); |
| 6273 | } /* for each tcb */ |
| 6274 | /* remove it from the ep list */ |
| 6275 | sctp_remove_laddr(laddr); |
| 6276 | inp->laddr_count--; |
| 6277 | /* update inp_vflag flags */ |
| 6278 | sctp_update_ep_vflag(inp); |
| 6279 | } |
| 6280 | return; |
| 6281 | } |
| 6282 | |
| 6283 | /* |
| 6284 | * Add the address to the TCB local address restricted list. |
| 6285 | * This is a "pending" address list (eg. addresses waiting for an |
| 6286 | * ASCONF-ACK response) and cannot be used as a valid source address. |
| 6287 | */ |
| 6288 | void |
| 6289 | sctp_add_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa) |
| 6290 | { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6291 | struct sctp_laddr *laddr; |
| 6292 | struct sctpladdr *list; |
| 6293 | |
| 6294 | /* |
| 6295 | * Assumes TCB is locked.. and possibly the INP. May need to |
| 6296 | * confirm/fix that if we need it and is not the case. |
| 6297 | */ |
| 6298 | list = &stcb->asoc.sctp_restricted_addrs; |
| 6299 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6300 | #ifdef INET6 |
| 6301 | if (ifa->address.sa.sa_family == AF_INET6) { |
| 6302 | if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) { |
| 6303 | /* Can't bind a non-existent addr. */ |
| 6304 | return; |
| 6305 | } |
| 6306 | } |
| 6307 | #endif |
| 6308 | /* does the address already exist? */ |
| 6309 | LIST_FOREACH(laddr, list, sctp_nxt_addr) { |
| 6310 | if (laddr->ifa == ifa) { |
| 6311 | return; |
| 6312 | } |
| 6313 | } |
| 6314 | |
| 6315 | /* add to the list */ |
| 6316 | (void)sctp_insert_laddr(list, ifa, 0); |
| 6317 | return; |
| 6318 | } |
| 6319 | |
| 6320 | /* |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6321 | * Remove a local address from the TCB local address restricted list |
| 6322 | */ |
| 6323 | void |
| 6324 | sctp_del_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa) |
| 6325 | { |
| 6326 | struct sctp_inpcb *inp; |
| 6327 | struct sctp_laddr *laddr; |
| 6328 | |
| 6329 | /* |
| 6330 | * This is called by asconf work. It is assumed that a) The TCB is |
| 6331 | * locked and b) The INP is locked. This is true in as much as I can |
| 6332 | * trace through the entry asconf code where I did these locks. |
| 6333 | * Again, the ASCONF code is a bit different in that it does lock |
| 6334 | * the INP during its work often times. This must be since we don't |
| 6335 | * want other proc's looking up things while what they are looking |
| 6336 | * up is changing :-D |
| 6337 | */ |
| 6338 | |
| 6339 | inp = stcb->sctp_ep; |
| 6340 | /* if subset bound and don't allow ASCONF's, can't delete last */ |
| 6341 | if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) && |
| 6342 | sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF)) { |
| 6343 | if (stcb->sctp_ep->laddr_count < 2) { |
| 6344 | /* can't delete last address */ |
| 6345 | return; |
| 6346 | } |
| 6347 | } |
| 6348 | LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) { |
| 6349 | /* remove the address if it exists */ |
| 6350 | if (laddr->ifa == NULL) |
| 6351 | continue; |
| 6352 | if (laddr->ifa == ifa) { |
| 6353 | sctp_remove_laddr(laddr); |
| 6354 | return; |
| 6355 | } |
| 6356 | } |
| 6357 | |
| 6358 | /* address not found! */ |
| 6359 | return; |
| 6360 | } |
| 6361 | |
| 6362 | #if defined(__FreeBSD__) |
| 6363 | /* |
| 6364 | * Temporarily remove for __APPLE__ until we use the Tiger equivalents |
| 6365 | */ |
| 6366 | /* sysctl */ |
| 6367 | static int sctp_max_number_of_assoc = SCTP_MAX_NUM_OF_ASOC; |
| 6368 | static int sctp_scale_up_for_address = SCTP_SCALE_FOR_ADDR; |
| 6369 | #endif /* FreeBSD || APPLE */ |
| 6370 | |
| 6371 | |
| 6372 | |
| 6373 | #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP) |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 6374 | struct sctp_mcore_ctrl *sctp_mcore_workers = NULL; |
| 6375 | int *sctp_cpuarry = NULL; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6376 | void |
| 6377 | sctp_queue_to_mcore(struct mbuf *m, int off, int cpu_to_use) |
| 6378 | { |
| 6379 | /* Queue a packet to a processor for the specified core */ |
| 6380 | struct sctp_mcore_queue *qent; |
| 6381 | struct sctp_mcore_ctrl *wkq; |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 6382 | int need_wake = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6383 | if (sctp_mcore_workers == NULL) { |
| 6384 | /* Something went way bad during setup */ |
| 6385 | sctp_input_with_port(m, off, 0); |
| 6386 | return; |
| 6387 | } |
| 6388 | SCTP_MALLOC(qent, struct sctp_mcore_queue *, |
| 6389 | (sizeof(struct sctp_mcore_queue)), |
| 6390 | SCTP_M_MCORE); |
| 6391 | if (qent == NULL) { |
| 6392 | /* This is trouble */ |
| 6393 | sctp_input_with_port(m, off, 0); |
| 6394 | return; |
| 6395 | } |
| 6396 | #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 |
| 6397 | qent->vn = curvnet; |
| 6398 | #endif |
| 6399 | qent->m = m; |
| 6400 | qent->off = off; |
| 6401 | qent->v6 = 0; |
| 6402 | wkq = &sctp_mcore_workers[cpu_to_use]; |
| 6403 | SCTP_MCORE_QLOCK(wkq); |
| 6404 | |
| 6405 | TAILQ_INSERT_TAIL(&wkq->que, qent, next); |
| 6406 | if (wkq->running == 0) { |
| 6407 | need_wake = 1; |
| 6408 | } |
| 6409 | SCTP_MCORE_QUNLOCK(wkq); |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 6410 | if (need_wake) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6411 | wakeup(&wkq->running); |
| 6412 | } |
| 6413 | } |
| 6414 | |
| 6415 | static void |
| 6416 | sctp_mcore_thread(void *arg) |
| 6417 | { |
| 6418 | |
| 6419 | struct sctp_mcore_ctrl *wkq; |
| 6420 | struct sctp_mcore_queue *qent; |
| 6421 | |
| 6422 | wkq = (struct sctp_mcore_ctrl *)arg; |
| 6423 | struct mbuf *m; |
| 6424 | int off, v6; |
| 6425 | |
| 6426 | /* Wait for first tickle */ |
| 6427 | SCTP_MCORE_LOCK(wkq); |
| 6428 | wkq->running = 0; |
| 6429 | msleep(&wkq->running, |
| 6430 | &wkq->core_mtx, |
| 6431 | 0, "wait for pkt", 0); |
| 6432 | SCTP_MCORE_UNLOCK(wkq); |
| 6433 | |
| 6434 | /* Bind to our cpu */ |
| 6435 | thread_lock(curthread); |
| 6436 | sched_bind(curthread, wkq->cpuid); |
| 6437 | thread_unlock(curthread); |
| 6438 | |
| 6439 | /* Now lets start working */ |
| 6440 | SCTP_MCORE_LOCK(wkq); |
| 6441 | /* Now grab lock and go */ |
tuexen | 9784e9a | 2011-12-18 13:04:23 +0000 | [diff] [blame] | 6442 | for (;;) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6443 | SCTP_MCORE_QLOCK(wkq); |
| 6444 | skip_sleep: |
| 6445 | wkq->running = 1; |
| 6446 | qent = TAILQ_FIRST(&wkq->que); |
| 6447 | if (qent) { |
| 6448 | TAILQ_REMOVE(&wkq->que, qent, next); |
| 6449 | SCTP_MCORE_QUNLOCK(wkq); |
| 6450 | #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 |
| 6451 | CURVNET_SET(qent->vn); |
| 6452 | #endif |
| 6453 | m = qent->m; |
| 6454 | off = qent->off; |
| 6455 | v6 = qent->v6; |
| 6456 | SCTP_FREE(qent, SCTP_M_MCORE); |
| 6457 | if (v6 == 0) { |
| 6458 | sctp_input_with_port(m, off, 0); |
| 6459 | } else { |
tuexen | cb5fe8d | 2012-05-04 09:50:27 +0000 | [diff] [blame] | 6460 | SCTP_PRINTF("V6 not yet supported\n"); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6461 | sctp_m_freem(m); |
| 6462 | } |
| 6463 | #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 |
| 6464 | CURVNET_RESTORE(); |
| 6465 | #endif |
| 6466 | SCTP_MCORE_QLOCK(wkq); |
| 6467 | } |
| 6468 | wkq->running = 0; |
| 6469 | if (!TAILQ_EMPTY(&wkq->que)) { |
| 6470 | goto skip_sleep; |
| 6471 | } |
| 6472 | SCTP_MCORE_QUNLOCK(wkq); |
| 6473 | msleep(&wkq->running, |
| 6474 | &wkq->core_mtx, |
| 6475 | 0, "wait for pkt", 0); |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 6476 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6477 | } |
| 6478 | |
| 6479 | static void |
| 6480 | sctp_startup_mcore_threads(void) |
| 6481 | { |
| 6482 | int i, cpu; |
| 6483 | |
| 6484 | if (mp_ncpus == 1) |
| 6485 | return; |
| 6486 | |
| 6487 | if (sctp_mcore_workers != NULL) { |
| 6488 | /* Already been here in some previous |
| 6489 | * vnet? |
| 6490 | */ |
| 6491 | return; |
| 6492 | } |
| 6493 | SCTP_MALLOC(sctp_mcore_workers, struct sctp_mcore_ctrl *, |
| 6494 | ((mp_maxid+1) * sizeof(struct sctp_mcore_ctrl)), |
| 6495 | SCTP_M_MCORE); |
| 6496 | if (sctp_mcore_workers == NULL) { |
| 6497 | /* TSNH I hope */ |
| 6498 | return; |
| 6499 | } |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 6500 | memset(sctp_mcore_workers, 0 , ((mp_maxid+1) * |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6501 | sizeof(struct sctp_mcore_ctrl))); |
| 6502 | /* Init the structures */ |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 6503 | for (i = 0; i<=mp_maxid; i++) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6504 | TAILQ_INIT(&sctp_mcore_workers[i].que); |
| 6505 | SCTP_MCORE_LOCK_INIT(&sctp_mcore_workers[i]); |
| 6506 | SCTP_MCORE_QLOCK_INIT(&sctp_mcore_workers[i]); |
| 6507 | sctp_mcore_workers[i].cpuid = i; |
| 6508 | } |
| 6509 | if (sctp_cpuarry == NULL) { |
| 6510 | SCTP_MALLOC(sctp_cpuarry, int *, |
| 6511 | (mp_ncpus * sizeof(int)), |
| 6512 | SCTP_M_MCORE); |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 6513 | i = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6514 | CPU_FOREACH(cpu) { |
| 6515 | sctp_cpuarry[i] = cpu; |
| 6516 | i++; |
| 6517 | } |
| 6518 | } |
| 6519 | |
| 6520 | /* Now start them all */ |
| 6521 | CPU_FOREACH(cpu) { |
| 6522 | #if __FreeBSD_version <= 701000 |
| 6523 | (void)kthread_create(sctp_mcore_thread, |
| 6524 | (void *)&sctp_mcore_workers[cpu], |
| 6525 | &sctp_mcore_workers[cpu].thread_proc, |
| 6526 | RFPROC, |
| 6527 | SCTP_KTHREAD_PAGES, |
| 6528 | SCTP_MCORE_NAME); |
| 6529 | |
| 6530 | #else |
| 6531 | (void)kproc_create(sctp_mcore_thread, |
| 6532 | (void *)&sctp_mcore_workers[cpu], |
| 6533 | &sctp_mcore_workers[cpu].thread_proc, |
| 6534 | RFPROC, |
| 6535 | SCTP_KTHREAD_PAGES, |
| 6536 | SCTP_MCORE_NAME); |
| 6537 | #endif |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 6538 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6539 | } |
| 6540 | } |
| 6541 | #endif |
Michael Tuexen | be8e0eb | 2019-03-24 00:11:02 +0100 | [diff] [blame] | 6542 | #if defined(__FreeBSD__) && __FreeBSD_cc_version >= 1400000 |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6543 | static struct mbuf * |
| 6544 | sctp_netisr_hdlr(struct mbuf *m, uintptr_t source) |
| 6545 | { |
| 6546 | struct ip *ip; |
| 6547 | struct sctphdr *sh; |
| 6548 | int offset; |
| 6549 | uint32_t flowid, tag; |
tuexen | 76eb209 | 2012-04-01 16:35:28 +0000 | [diff] [blame] | 6550 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6551 | /* |
| 6552 | * No flow id built by lower layers fix it so we |
| 6553 | * create one. |
| 6554 | */ |
| 6555 | ip = mtod(m, struct ip *); |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 6556 | offset = (ip->ip_hl << 2) + sizeof(struct sctphdr); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6557 | if (SCTP_BUF_LEN(m) < offset) { |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 6558 | if ((m = m_pullup(m, offset)) == NULL) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6559 | SCTP_STAT_INCR(sctps_hdrops); |
| 6560 | return (NULL); |
| 6561 | } |
| 6562 | ip = mtod(m, struct ip *); |
| 6563 | } |
| 6564 | sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6565 | tag = htonl(sh->v_tag); |
| 6566 | flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port); |
| 6567 | m->m_pkthdr.flowid = flowid; |
Michael Tuexen | fdcf790 | 2016-08-06 14:39:31 +0200 | [diff] [blame] | 6568 | /* FIX ME */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6569 | m->m_flags |= M_FLOWID; |
| 6570 | return (m); |
| 6571 | } |
| 6572 | #endif |
| 6573 | |
| 6574 | void |
Michael Tuexen | 843cc3b | 2019-08-31 15:25:03 +0200 | [diff] [blame] | 6575 | #if defined(__Userspace__) |
José Luis Millán | f6aa4b0 | 2019-08-12 22:54:25 +0200 | [diff] [blame] | 6576 | sctp_pcb_init(int start_threads) |
Michael Tuexen | 843cc3b | 2019-08-31 15:25:03 +0200 | [diff] [blame] | 6577 | #else |
| 6578 | sctp_pcb_init(void) |
| 6579 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6580 | { |
| 6581 | /* |
| 6582 | * SCTP initialization for the PCB structures should be called by |
Michael Tuexen | 34488e7 | 2016-05-03 22:11:59 +0200 | [diff] [blame] | 6583 | * the sctp_init() function. |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6584 | */ |
| 6585 | int i; |
| 6586 | struct timeval tv; |
tuexen | 6019b07 | 2011-12-15 18:38:03 +0000 | [diff] [blame] | 6587 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6588 | if (SCTP_BASE_VAR(sctp_pcb_initialized) != 0) { |
| 6589 | /* error I was called twice */ |
| 6590 | return; |
| 6591 | } |
| 6592 | SCTP_BASE_VAR(sctp_pcb_initialized) = 1; |
| 6593 | |
Michael Tuexen | cb2878b | 2015-11-13 12:59:40 +0100 | [diff] [blame] | 6594 | #if defined(SCTP_PROCESS_LEVEL_LOCKS) |
| 6595 | #if !defined(__Userspace_os_Windows) |
| 6596 | pthread_mutexattr_init(&SCTP_BASE_VAR(mtx_attr)); |
| 6597 | #ifdef INVARIANTS |
| 6598 | pthread_mutexattr_settype(&SCTP_BASE_VAR(mtx_attr), PTHREAD_MUTEX_ERRORCHECK); |
| 6599 | #endif |
| 6600 | #endif |
| 6601 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6602 | #if defined(SCTP_LOCAL_TRACE_BUF) |
| 6603 | #if defined(__Windows__) |
| 6604 | if (SCTP_BASE_SYSCTL(sctp_log) != NULL) { |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 6605 | memset(SCTP_BASE_SYSCTL(sctp_log), 0, sizeof(struct sctp_log)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6606 | } |
| 6607 | #else |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 6608 | memset(&SCTP_BASE_SYSCTL(sctp_log), 0, sizeof(struct sctp_log)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6609 | #endif |
| 6610 | #endif |
| 6611 | #if defined(__FreeBSD__) && defined(SMP) && defined(SCTP_USE_PERCPU_STAT) |
| 6612 | SCTP_MALLOC(SCTP_BASE_STATS, struct sctpstat *, |
| 6613 | ((mp_maxid+1) * sizeof(struct sctpstat)), |
| 6614 | SCTP_M_MCORE); |
| 6615 | #endif |
| 6616 | (void)SCTP_GETTIME_TIMEVAL(&tv); |
| 6617 | #if defined(__FreeBSD__) && defined(SMP) && defined(SCTP_USE_PERCPU_STAT) |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 6618 | memset(SCTP_BASE_STATS, 0, sizeof(struct sctpstat) * (mp_maxid+1)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6619 | SCTP_BASE_STATS[PCPU_GET(cpuid)].sctps_discontinuitytime.tv_sec = (uint32_t)tv.tv_sec; |
| 6620 | SCTP_BASE_STATS[PCPU_GET(cpuid)].sctps_discontinuitytime.tv_usec = (uint32_t)tv.tv_usec; |
| 6621 | #else |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 6622 | memset(&SCTP_BASE_STATS, 0, sizeof(struct sctpstat)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6623 | SCTP_BASE_STAT(sctps_discontinuitytime).tv_sec = (uint32_t)tv.tv_sec; |
| 6624 | SCTP_BASE_STAT(sctps_discontinuitytime).tv_usec = (uint32_t)tv.tv_usec; |
| 6625 | #endif |
| 6626 | /* init the empty list of (All) Endpoints */ |
| 6627 | LIST_INIT(&SCTP_BASE_INFO(listhead)); |
| 6628 | #if defined(__APPLE__) |
| 6629 | LIST_INIT(&SCTP_BASE_INFO(inplisthead)); |
t00fcxen | bc766ab | 2014-01-03 19:29:31 +0000 | [diff] [blame] | 6630 | #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) || defined(APPLE_LION) || defined(APPLE_MOUNTAINLION) |
| 6631 | SCTP_BASE_INFO(sctbinfo).listhead = &SCTP_BASE_INFO(inplisthead); |
| 6632 | SCTP_BASE_INFO(sctbinfo).mtx_grp_attr = lck_grp_attr_alloc_init(); |
| 6633 | lck_grp_attr_setdefault(SCTP_BASE_INFO(sctbinfo).mtx_grp_attr); |
| 6634 | SCTP_BASE_INFO(sctbinfo).mtx_grp = lck_grp_alloc_init("sctppcb", SCTP_BASE_INFO(sctbinfo).mtx_grp_attr); |
| 6635 | SCTP_BASE_INFO(sctbinfo).mtx_attr = lck_attr_alloc_init(); |
| 6636 | lck_attr_setdefault(SCTP_BASE_INFO(sctbinfo).mtx_attr); |
| 6637 | #else |
| 6638 | SCTP_BASE_INFO(sctbinfo).ipi_listhead = &SCTP_BASE_INFO(inplisthead); |
| 6639 | SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr = lck_grp_attr_alloc_init(); |
| 6640 | lck_grp_attr_setdefault(SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr); |
| 6641 | SCTP_BASE_INFO(sctbinfo).ipi_lock_grp = lck_grp_alloc_init("sctppcb", SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr); |
| 6642 | SCTP_BASE_INFO(sctbinfo).ipi_lock_attr = lck_attr_alloc_init(); |
| 6643 | lck_attr_setdefault(SCTP_BASE_INFO(sctbinfo).ipi_lock_attr); |
| 6644 | #endif |
| 6645 | #if !defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) && !defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION) |
| 6646 | SCTP_BASE_INFO(sctbinfo).ipi_gc = sctp_gc; |
| 6647 | in_pcbinfo_attach(&SCTP_BASE_INFO(sctbinfo)); |
| 6648 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6649 | #endif |
| 6650 | |
| 6651 | |
| 6652 | /* init the hash table of endpoints */ |
| 6653 | #if defined(__FreeBSD__) |
| 6654 | #if defined(__FreeBSD_cc_version) && __FreeBSD_cc_version >= 440000 |
| 6655 | TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", &SCTP_BASE_SYSCTL(sctp_hashtblsize)); |
| 6656 | TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", &SCTP_BASE_SYSCTL(sctp_pcbtblsize)); |
| 6657 | TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", &SCTP_BASE_SYSCTL(sctp_chunkscale)); |
| 6658 | #else |
| 6659 | TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", SCTP_TCBHASHSIZE, |
| 6660 | SCTP_BASE_SYSCTL(sctp_hashtblsize)); |
| 6661 | TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", SCTP_PCBHASHSIZE, |
| 6662 | SCTP_BASE_SYSCTL(sctp_pcbtblsize)); |
| 6663 | TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", SCTP_CHUNKQUEUE_SCALE, |
| 6664 | SCTP_BASE_SYSCTL(sctp_chunkscale)); |
| 6665 | #endif |
| 6666 | #endif |
| 6667 | SCTP_BASE_INFO(sctp_asochash) = SCTP_HASH_INIT((SCTP_BASE_SYSCTL(sctp_hashtblsize) * 31), |
| 6668 | &SCTP_BASE_INFO(hashasocmark)); |
| 6669 | SCTP_BASE_INFO(sctp_ephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize), |
| 6670 | &SCTP_BASE_INFO(hashmark)); |
| 6671 | SCTP_BASE_INFO(sctp_tcpephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize), |
| 6672 | &SCTP_BASE_INFO(hashtcpmark)); |
| 6673 | SCTP_BASE_INFO(hashtblsize) = SCTP_BASE_SYSCTL(sctp_hashtblsize); |
| 6674 | |
| 6675 | |
| 6676 | SCTP_BASE_INFO(sctp_vrfhash) = SCTP_HASH_INIT(SCTP_SIZE_OF_VRF_HASH, |
| 6677 | &SCTP_BASE_INFO(hashvrfmark)); |
| 6678 | |
| 6679 | SCTP_BASE_INFO(vrf_ifn_hash) = SCTP_HASH_INIT(SCTP_VRF_IFN_HASH_SIZE, |
| 6680 | &SCTP_BASE_INFO(vrf_ifn_hashmark)); |
| 6681 | /* init the zones */ |
| 6682 | /* |
| 6683 | * FIX ME: Should check for NULL returns, but if it does fail we are |
| 6684 | * doomed to panic anyways... add later maybe. |
| 6685 | */ |
| 6686 | SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_ep), "sctp_ep", |
| 6687 | sizeof(struct sctp_inpcb), maxsockets); |
| 6688 | |
| 6689 | SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asoc), "sctp_asoc", |
| 6690 | sizeof(struct sctp_tcb), sctp_max_number_of_assoc); |
| 6691 | |
| 6692 | SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_laddr), "sctp_laddr", |
| 6693 | sizeof(struct sctp_laddr), |
| 6694 | (sctp_max_number_of_assoc * sctp_scale_up_for_address)); |
| 6695 | |
| 6696 | SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_net), "sctp_raddr", |
| 6697 | sizeof(struct sctp_nets), |
| 6698 | (sctp_max_number_of_assoc * sctp_scale_up_for_address)); |
| 6699 | |
| 6700 | SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_chunk), "sctp_chunk", |
| 6701 | sizeof(struct sctp_tmit_chunk), |
| 6702 | (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale))); |
| 6703 | |
| 6704 | SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_readq), "sctp_readq", |
| 6705 | sizeof(struct sctp_queued_to_read), |
| 6706 | (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale))); |
| 6707 | |
| 6708 | SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_strmoq), "sctp_stream_msg_out", |
| 6709 | sizeof(struct sctp_stream_queue_pending), |
| 6710 | (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale))); |
| 6711 | |
| 6712 | SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf), "sctp_asconf", |
| 6713 | sizeof(struct sctp_asconf), |
| 6714 | (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale))); |
| 6715 | |
| 6716 | SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf_ack), "sctp_asconf_ack", |
| 6717 | sizeof(struct sctp_asconf_ack), |
| 6718 | (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale))); |
| 6719 | |
tuexen | 9784e9a | 2011-12-18 13:04:23 +0000 | [diff] [blame] | 6720 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6721 | /* Master Lock INIT for info structure */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6722 | SCTP_INP_INFO_LOCK_INIT(); |
| 6723 | SCTP_STATLOG_INIT_LOCK(); |
| 6724 | |
| 6725 | SCTP_IPI_COUNT_INIT(); |
| 6726 | SCTP_IPI_ADDR_INIT(); |
| 6727 | #ifdef SCTP_PACKET_LOGGING |
| 6728 | SCTP_IP_PKTLOG_INIT(); |
| 6729 | #endif |
| 6730 | LIST_INIT(&SCTP_BASE_INFO(addr_wq)); |
| 6731 | |
| 6732 | SCTP_WQ_ADDR_INIT(); |
| 6733 | /* not sure if we need all the counts */ |
| 6734 | SCTP_BASE_INFO(ipi_count_ep) = 0; |
| 6735 | /* assoc/tcb zone info */ |
| 6736 | SCTP_BASE_INFO(ipi_count_asoc) = 0; |
| 6737 | /* local addrlist zone info */ |
| 6738 | SCTP_BASE_INFO(ipi_count_laddr) = 0; |
| 6739 | /* remote addrlist zone info */ |
| 6740 | SCTP_BASE_INFO(ipi_count_raddr) = 0; |
| 6741 | /* chunk info */ |
| 6742 | SCTP_BASE_INFO(ipi_count_chunk) = 0; |
| 6743 | |
| 6744 | /* socket queue zone info */ |
| 6745 | SCTP_BASE_INFO(ipi_count_readq) = 0; |
| 6746 | |
| 6747 | /* stream out queue cont */ |
| 6748 | SCTP_BASE_INFO(ipi_count_strmoq) = 0; |
| 6749 | |
| 6750 | SCTP_BASE_INFO(ipi_free_strmoq) = 0; |
| 6751 | SCTP_BASE_INFO(ipi_free_chunks) = 0; |
| 6752 | |
| 6753 | SCTP_OS_TIMER_INIT(&SCTP_BASE_INFO(addr_wq_timer.timer)); |
| 6754 | |
| 6755 | /* Init the TIMEWAIT list */ |
| 6756 | for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) { |
| 6757 | LIST_INIT(&SCTP_BASE_INFO(vtag_timewait)[i]); |
| 6758 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6759 | #if defined(SCTP_PROCESS_LEVEL_LOCKS) |
tuexen | 5154a00 | 2011-12-23 18:47:55 +0000 | [diff] [blame] | 6760 | #if defined(__Userspace_os_Windows) |
tuexen | 6019b07 | 2011-12-15 18:38:03 +0000 | [diff] [blame] | 6761 | InitializeConditionVariable(&sctp_it_ctl.iterator_wakeup); |
| 6762 | #else |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6763 | (void)pthread_cond_init(&sctp_it_ctl.iterator_wakeup, NULL); |
| 6764 | #endif |
tuexen | 6019b07 | 2011-12-15 18:38:03 +0000 | [diff] [blame] | 6765 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6766 | sctp_startup_iterator(); |
| 6767 | |
| 6768 | #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP) |
| 6769 | sctp_startup_mcore_threads(); |
| 6770 | #endif |
| 6771 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6772 | /* |
| 6773 | * INIT the default VRF which for BSD is the only one, other O/S's |
| 6774 | * may have more. But initially they must start with one and then |
| 6775 | * add the VRF's as addresses are added. |
| 6776 | */ |
| 6777 | sctp_init_vrf_list(SCTP_DEFAULT_VRF); |
Michael Tuexen | be8e0eb | 2019-03-24 00:11:02 +0100 | [diff] [blame] | 6778 | #if defined(__FreeBSD__) && __FreeBSD_cc_version >= 1400000 |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6779 | if (ip_register_flow_handler(sctp_netisr_hdlr, IPPROTO_SCTP)) { |
tuexen | cb5fe8d | 2012-05-04 09:50:27 +0000 | [diff] [blame] | 6780 | SCTP_PRINTF("***SCTP- Error can't register netisr handler***\n"); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6781 | } |
| 6782 | #endif |
| 6783 | #if defined(_SCTP_NEEDS_CALLOUT_) || defined(_USER_SCTP_NEEDS_CALLOUT_) |
| 6784 | /* allocate the lock for the callout/timer queue */ |
| 6785 | SCTP_TIMERQ_LOCK_INIT(); |
| 6786 | TAILQ_INIT(&SCTP_BASE_INFO(callqueue)); |
| 6787 | #endif |
| 6788 | #if defined(__Userspace__) |
Michael Tuexen | 2f6478e | 2017-03-02 09:57:46 +0100 | [diff] [blame] | 6789 | mbuf_initialize(NULL); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6790 | atomic_init(); |
tuexen | c417897 | 2012-07-15 20:40:54 +0000 | [diff] [blame] | 6791 | #if defined(INET) || defined(INET6) |
José Luis Millán | f6aa4b0 | 2019-08-12 22:54:25 +0200 | [diff] [blame] | 6792 | if (start_threads) |
| 6793 | recv_thread_init(); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6794 | #endif |
tuexen | c417897 | 2012-07-15 20:40:54 +0000 | [diff] [blame] | 6795 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6796 | } |
| 6797 | |
| 6798 | /* |
| 6799 | * Assumes that the SCTP_BASE_INFO() lock is NOT held. |
| 6800 | */ |
| 6801 | void |
| 6802 | sctp_pcb_finish(void) |
| 6803 | { |
| 6804 | struct sctp_vrflist *vrf_bucket; |
| 6805 | struct sctp_vrf *vrf, *nvrf; |
| 6806 | struct sctp_ifn *ifn, *nifn; |
| 6807 | struct sctp_ifa *ifa, *nifa; |
| 6808 | struct sctpvtaghead *chain; |
| 6809 | struct sctp_tagblock *twait_block, *prev_twait_block; |
| 6810 | struct sctp_laddr *wi, *nwi; |
| 6811 | int i; |
t00fcxen | c793228 | 2013-11-16 14:57:41 +0000 | [diff] [blame] | 6812 | struct sctp_iterator *it, *nit; |
Michael Tuexen | 20785d4 | 2015-11-13 12:48:35 +0100 | [diff] [blame] | 6813 | |
Michael Tuexen | 71a83fb | 2016-02-16 21:52:58 +0100 | [diff] [blame] | 6814 | if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) { |
| 6815 | SCTP_PRINTF("%s: race condition on teardown.\n", __func__); |
| 6816 | return; |
| 6817 | } |
| 6818 | SCTP_BASE_VAR(sctp_pcb_initialized) = 0; |
t00fcxen | 9232a65 | 2013-11-30 09:34:24 +0000 | [diff] [blame] | 6819 | #if !defined(__FreeBSD__) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6820 | /* Notify the iterator to exit. */ |
| 6821 | SCTP_IPI_ITERATOR_WQ_LOCK(); |
| 6822 | sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_MUST_EXIT; |
| 6823 | sctp_wakeup_iterator(); |
| 6824 | SCTP_IPI_ITERATOR_WQ_UNLOCK(); |
| 6825 | #endif |
| 6826 | #if defined(__APPLE__) |
t00fcxen | bc766ab | 2014-01-03 19:29:31 +0000 | [diff] [blame] | 6827 | #if !defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) && !defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION) |
| 6828 | in_pcbinfo_detach(&SCTP_BASE_INFO(sctbinfo)); |
| 6829 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6830 | SCTP_IPI_ITERATOR_WQ_LOCK(); |
| 6831 | do { |
| 6832 | msleep(&sctp_it_ctl.iterator_flags, |
| 6833 | sctp_it_ctl.ipi_iterator_wq_mtx, |
| 6834 | 0, "waiting_for_work", 0); |
| 6835 | } while ((sctp_it_ctl.iterator_flags & SCTP_ITERATOR_EXITED) == 0); |
| 6836 | thread_deallocate(sctp_it_ctl.thread_proc); |
| 6837 | SCTP_IPI_ITERATOR_WQ_UNLOCK(); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6838 | #endif |
| 6839 | #if defined(__Windows__) |
| 6840 | if (sctp_it_ctl.iterator_thread_obj != NULL) { |
| 6841 | NTSTATUS status = STATUS_SUCCESS; |
| 6842 | |
| 6843 | KeSetEvent(&sctp_it_ctl.iterator_wakeup[1], IO_NO_INCREMENT, FALSE); |
| 6844 | status = KeWaitForSingleObject(sctp_it_ctl.iterator_thread_obj, |
| 6845 | Executive, |
| 6846 | KernelMode, |
| 6847 | FALSE, |
| 6848 | NULL); |
| 6849 | ObDereferenceObject(sctp_it_ctl.iterator_thread_obj); |
| 6850 | } |
| 6851 | #endif |
t00fcxen | 92a5ac4 | 2013-11-16 12:58:54 +0000 | [diff] [blame] | 6852 | #if defined(__Userspace__) |
Michael Tuexen | 3212d54 | 2020-03-16 12:30:04 +0100 | [diff] [blame] | 6853 | if (SCTP_BASE_VAR(iterator_thread_started)) { |
t00fcxen | 92a5ac4 | 2013-11-16 12:58:54 +0000 | [diff] [blame] | 6854 | #if defined(__Userspace_os_Windows) |
| 6855 | WaitForSingleObject(sctp_it_ctl.thread_proc, INFINITE); |
| 6856 | CloseHandle(sctp_it_ctl.thread_proc); |
| 6857 | sctp_it_ctl.thread_proc = NULL; |
| 6858 | #else |
| 6859 | pthread_join(sctp_it_ctl.thread_proc, NULL); |
| 6860 | sctp_it_ctl.thread_proc = 0; |
| 6861 | #endif |
| 6862 | } |
| 6863 | #endif |
| 6864 | #if defined(SCTP_PROCESS_LEVEL_LOCKS) |
| 6865 | #if defined(__Userspace_os_Windows) |
| 6866 | DeleteConditionVariable(&sctp_it_ctl.iterator_wakeup); |
| 6867 | #else |
| 6868 | pthread_cond_destroy(&sctp_it_ctl.iterator_wakeup); |
Michael Tuexen | 89e15a7 | 2015-11-13 12:42:55 +0100 | [diff] [blame] | 6869 | pthread_mutexattr_destroy(&SCTP_BASE_VAR(mtx_attr)); |
t00fcxen | 92a5ac4 | 2013-11-16 12:58:54 +0000 | [diff] [blame] | 6870 | #endif |
| 6871 | #endif |
t00fcxen | 9232a65 | 2013-11-30 09:34:24 +0000 | [diff] [blame] | 6872 | /* In FreeBSD the iterator thread never exits |
| 6873 | * but we do clean up. |
| 6874 | * The only way FreeBSD reaches here is if we have VRF's |
| 6875 | * but we still add the ifdef to make it compile on old versions. |
| 6876 | */ |
Michael Tuexen | 71a83fb | 2016-02-16 21:52:58 +0100 | [diff] [blame] | 6877 | #if defined(__FreeBSD__) |
| 6878 | retry: |
| 6879 | #endif |
t00fcxen | 9232a65 | 2013-11-30 09:34:24 +0000 | [diff] [blame] | 6880 | SCTP_IPI_ITERATOR_WQ_LOCK(); |
Michael Tuexen | 71a83fb | 2016-02-16 21:52:58 +0100 | [diff] [blame] | 6881 | #if defined(__FreeBSD__) |
| 6882 | /* |
| 6883 | * sctp_iterator_worker() might be working on an it entry without |
| 6884 | * holding the lock. We won't find it on the list either and |
| 6885 | * continue and free/destroy it. While holding the lock, spin, to |
| 6886 | * avoid the race condition as sctp_iterator_worker() will have to |
| 6887 | * wait to re-aquire the lock. |
| 6888 | */ |
| 6889 | if (sctp_it_ctl.iterator_running != 0 || sctp_it_ctl.cur_it != NULL) { |
| 6890 | SCTP_IPI_ITERATOR_WQ_UNLOCK(); |
| 6891 | SCTP_PRINTF("%s: Iterator running while we held the lock. Retry. " |
| 6892 | "cur_it=%p\n", __func__, sctp_it_ctl.cur_it); |
| 6893 | DELAY(10); |
| 6894 | goto retry; |
| 6895 | } |
| 6896 | #endif |
t00fcxen | 9232a65 | 2013-11-30 09:34:24 +0000 | [diff] [blame] | 6897 | TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) { |
t00fcxen | 433c6e1 | 2013-11-30 09:45:11 +0000 | [diff] [blame] | 6898 | #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 |
t00fcxen | 9232a65 | 2013-11-30 09:34:24 +0000 | [diff] [blame] | 6899 | if (it->vn != curvnet) { |
| 6900 | continue; |
| 6901 | } |
| 6902 | #endif |
| 6903 | TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr); |
| 6904 | if (it->function_atend != NULL) { |
| 6905 | (*it->function_atend) (it->pointer, it->val); |
| 6906 | } |
| 6907 | SCTP_FREE(it,SCTP_M_ITER); |
| 6908 | } |
| 6909 | SCTP_IPI_ITERATOR_WQ_UNLOCK(); |
t00fcxen | 433c6e1 | 2013-11-30 09:45:11 +0000 | [diff] [blame] | 6910 | #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 |
t00fcxen | 9232a65 | 2013-11-30 09:34:24 +0000 | [diff] [blame] | 6911 | SCTP_ITERATOR_LOCK(); |
| 6912 | if ((sctp_it_ctl.cur_it) && |
| 6913 | (sctp_it_ctl.cur_it->vn == curvnet)) { |
| 6914 | sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_IT; |
| 6915 | } |
| 6916 | SCTP_ITERATOR_UNLOCK(); |
| 6917 | #endif |
t00fcxen | c793228 | 2013-11-16 14:57:41 +0000 | [diff] [blame] | 6918 | #if !defined(__FreeBSD__) |
t00fcxen | 92a5ac4 | 2013-11-16 12:58:54 +0000 | [diff] [blame] | 6919 | SCTP_IPI_ITERATOR_WQ_DESTROY(); |
t00fcxen | 8dd95b8 | 2014-07-11 20:41:28 +0000 | [diff] [blame] | 6920 | SCTP_ITERATOR_LOCK_DESTROY(); |
t00fcxen | c793228 | 2013-11-16 14:57:41 +0000 | [diff] [blame] | 6921 | #endif |
Michael Tuexen | 71a83fb | 2016-02-16 21:52:58 +0100 | [diff] [blame] | 6922 | SCTP_OS_TIMER_STOP_DRAIN(&SCTP_BASE_INFO(addr_wq_timer.timer)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6923 | SCTP_WQ_ADDR_LOCK(); |
| 6924 | LIST_FOREACH_SAFE(wi, &SCTP_BASE_INFO(addr_wq), sctp_nxt_addr, nwi) { |
| 6925 | LIST_REMOVE(wi, sctp_nxt_addr); |
| 6926 | SCTP_DECR_LADDR_COUNT(); |
t00fcxen | 9892b7f | 2014-09-06 20:06:39 +0000 | [diff] [blame] | 6927 | if (wi->action == SCTP_DEL_IP_ADDRESS) { |
| 6928 | SCTP_FREE(wi->ifa, SCTP_M_IFA); |
| 6929 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6930 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), wi); |
| 6931 | } |
| 6932 | SCTP_WQ_ADDR_UNLOCK(); |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 6933 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6934 | /* |
| 6935 | * free the vrf/ifn/ifa lists and hashes (be sure address monitor |
| 6936 | * is destroyed first). |
| 6937 | */ |
| 6938 | vrf_bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(SCTP_DEFAULT_VRFID & SCTP_BASE_INFO(hashvrfmark))]; |
| 6939 | LIST_FOREACH_SAFE(vrf, vrf_bucket, next_vrf, nvrf) { |
| 6940 | LIST_FOREACH_SAFE(ifn, &vrf->ifnlist, next_ifn, nifn) { |
| 6941 | LIST_FOREACH_SAFE(ifa, &ifn->ifalist, next_ifa, nifa) { |
| 6942 | /* free the ifa */ |
| 6943 | LIST_REMOVE(ifa, next_bucket); |
| 6944 | LIST_REMOVE(ifa, next_ifa); |
| 6945 | SCTP_FREE(ifa, SCTP_M_IFA); |
| 6946 | } |
| 6947 | /* free the ifn */ |
| 6948 | LIST_REMOVE(ifn, next_bucket); |
| 6949 | LIST_REMOVE(ifn, next_ifn); |
| 6950 | SCTP_FREE(ifn, SCTP_M_IFN); |
| 6951 | } |
| 6952 | SCTP_HASH_FREE(vrf->vrf_addr_hash, vrf->vrf_addr_hashmark); |
| 6953 | /* free the vrf */ |
| 6954 | LIST_REMOVE(vrf, next_vrf); |
| 6955 | SCTP_FREE(vrf, SCTP_M_VRF); |
| 6956 | } |
| 6957 | /* free the vrf hashes */ |
| 6958 | SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_vrfhash), SCTP_BASE_INFO(hashvrfmark)); |
| 6959 | 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] | 6960 | |
| 6961 | /* free the TIMEWAIT list elements malloc'd in the function |
| 6962 | * sctp_add_vtag_to_timewait()... |
| 6963 | */ |
| 6964 | for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) { |
| 6965 | chain = &SCTP_BASE_INFO(vtag_timewait)[i]; |
| 6966 | if (!LIST_EMPTY(chain)) { |
| 6967 | prev_twait_block = NULL; |
| 6968 | LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) { |
| 6969 | if (prev_twait_block) { |
| 6970 | SCTP_FREE(prev_twait_block, SCTP_M_TIMW); |
| 6971 | } |
| 6972 | prev_twait_block = twait_block; |
| 6973 | } |
| 6974 | SCTP_FREE(prev_twait_block, SCTP_M_TIMW); |
| 6975 | } |
| 6976 | } |
| 6977 | |
| 6978 | /* free the locks and mutexes */ |
| 6979 | #if defined(__APPLE__) |
| 6980 | SCTP_TIMERQ_LOCK_DESTROY(); |
| 6981 | #endif |
| 6982 | #ifdef SCTP_PACKET_LOGGING |
| 6983 | SCTP_IP_PKTLOG_DESTROY(); |
| 6984 | #endif |
| 6985 | SCTP_IPI_ADDR_DESTROY(); |
| 6986 | #if defined(__APPLE__) |
| 6987 | SCTP_IPI_COUNT_DESTROY(); |
| 6988 | #endif |
| 6989 | SCTP_STATLOG_DESTROY(); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6990 | SCTP_INP_INFO_LOCK_DESTROY(); |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 6991 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6992 | SCTP_WQ_ADDR_DESTROY(); |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 6993 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6994 | #if defined(__APPLE__) |
t00fcxen | bc766ab | 2014-01-03 19:29:31 +0000 | [diff] [blame] | 6995 | #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) || defined(APPLE_LION) || defined(APPLE_MOUNTAINLION) |
| 6996 | lck_grp_attr_free(SCTP_BASE_INFO(sctbinfo).mtx_grp_attr); |
| 6997 | lck_grp_free(SCTP_BASE_INFO(sctbinfo).mtx_grp); |
| 6998 | lck_attr_free(SCTP_BASE_INFO(sctbinfo).mtx_attr); |
| 6999 | #else |
| 7000 | lck_grp_attr_free(SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr); |
| 7001 | lck_grp_free(SCTP_BASE_INFO(sctbinfo).ipi_lock_grp); |
| 7002 | lck_attr_free(SCTP_BASE_INFO(sctbinfo).ipi_lock_attr); |
| 7003 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7004 | #endif |
| 7005 | #if defined(__Userspace__) |
| 7006 | SCTP_TIMERQ_LOCK_DESTROY(); |
| 7007 | SCTP_ZONE_DESTROY(zone_mbuf); |
| 7008 | SCTP_ZONE_DESTROY(zone_clust); |
| 7009 | SCTP_ZONE_DESTROY(zone_ext_refcnt); |
| 7010 | #endif |
Michael Tuexen | 71a83fb | 2016-02-16 21:52:58 +0100 | [diff] [blame] | 7011 | /* Get rid of other stuff too. */ |
| 7012 | if (SCTP_BASE_INFO(sctp_asochash) != NULL) |
| 7013 | SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_asochash), SCTP_BASE_INFO(hashasocmark)); |
| 7014 | if (SCTP_BASE_INFO(sctp_ephash) != NULL) |
| 7015 | SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_ephash), SCTP_BASE_INFO(hashmark)); |
| 7016 | if (SCTP_BASE_INFO(sctp_tcpephash) != NULL) |
| 7017 | SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_tcpephash), SCTP_BASE_INFO(hashtcpmark)); |
| 7018 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7019 | #if defined(__Windows__) || defined(__FreeBSD__) || defined(__Userspace__) |
| 7020 | SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_ep)); |
| 7021 | SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asoc)); |
| 7022 | SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_laddr)); |
| 7023 | SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_net)); |
| 7024 | SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_chunk)); |
| 7025 | SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_readq)); |
| 7026 | SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_strmoq)); |
| 7027 | SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf)); |
| 7028 | SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf_ack)); |
| 7029 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7030 | #if defined(__FreeBSD__) && defined(SMP) && defined(SCTP_USE_PERCPU_STAT) |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 7031 | SCTP_FREE(SCTP_BASE_STATS, SCTP_M_MCORE); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7032 | #endif |
| 7033 | } |
| 7034 | |
| 7035 | |
| 7036 | int |
| 7037 | sctp_load_addresses_from_init(struct sctp_tcb *stcb, struct mbuf *m, |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 7038 | int offset, int limit, |
| 7039 | struct sockaddr *src, struct sockaddr *dst, |
Michael Tuexen | 923f735 | 2016-05-01 23:55:35 +0200 | [diff] [blame] | 7040 | struct sockaddr *altsa, uint16_t port) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7041 | { |
| 7042 | /* |
| 7043 | * grub through the INIT pulling addresses and loading them to the |
| 7044 | * nets structure in the asoc. The from address in the mbuf should |
| 7045 | * also be loaded (if it is not already). This routine can be called |
| 7046 | * with either INIT or INIT-ACK's as long as the m points to the IP |
| 7047 | * packet and the offset points to the beginning of the parameters. |
| 7048 | */ |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 7049 | struct sctp_inpcb *inp; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7050 | struct sctp_nets *net, *nnet, *net_tmp; |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 7051 | struct sctp_paramhdr *phdr, param_buf; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7052 | struct sctp_tcb *stcb_tmp; |
| 7053 | uint16_t ptype, plen; |
| 7054 | struct sockaddr *sa; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7055 | uint8_t random_store[SCTP_PARAM_BUFFER_SIZE]; |
| 7056 | struct sctp_auth_random *p_random = NULL; |
| 7057 | uint16_t random_len = 0; |
| 7058 | uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE]; |
| 7059 | struct sctp_auth_hmac_algo *hmacs = NULL; |
| 7060 | uint16_t hmacs_len = 0; |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 7061 | uint8_t saw_asconf = 0; |
| 7062 | uint8_t saw_asconf_ack = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7063 | uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE]; |
| 7064 | struct sctp_auth_chunk_list *chunks = NULL; |
| 7065 | uint16_t num_chunks = 0; |
| 7066 | sctp_key_t *new_key; |
| 7067 | uint32_t keylen; |
| 7068 | int got_random = 0, got_hmacs = 0, got_chklist = 0; |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7069 | uint8_t peer_supports_ecn; |
| 7070 | uint8_t peer_supports_prsctp; |
| 7071 | uint8_t peer_supports_auth; |
| 7072 | uint8_t peer_supports_asconf; |
| 7073 | uint8_t peer_supports_asconf_ack; |
| 7074 | uint8_t peer_supports_reconfig; |
| 7075 | uint8_t peer_supports_nrsack; |
| 7076 | uint8_t peer_supports_pktdrop; |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7077 | uint8_t peer_supports_idata; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7078 | #ifdef INET |
| 7079 | struct sockaddr_in sin; |
| 7080 | #endif |
| 7081 | #ifdef INET6 |
| 7082 | struct sockaddr_in6 sin6; |
| 7083 | #endif |
| 7084 | |
| 7085 | /* First get the destination address setup too. */ |
| 7086 | #ifdef INET |
| 7087 | memset(&sin, 0, sizeof(sin)); |
| 7088 | sin.sin_family = AF_INET; |
t00fcxen | 8fbaee3 | 2012-09-04 16:41:39 +0000 | [diff] [blame] | 7089 | #ifdef HAVE_SIN_LEN |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7090 | sin.sin_len = sizeof(sin); |
| 7091 | #endif |
| 7092 | sin.sin_port = stcb->rport; |
| 7093 | #endif |
| 7094 | #ifdef INET6 |
| 7095 | memset(&sin6, 0, sizeof(sin6)); |
| 7096 | sin6.sin6_family = AF_INET6; |
t00fcxen | 8fbaee3 | 2012-09-04 16:41:39 +0000 | [diff] [blame] | 7097 | #ifdef HAVE_SIN6_LEN |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7098 | sin6.sin6_len = sizeof(struct sockaddr_in6); |
| 7099 | #endif |
| 7100 | sin6.sin6_port = stcb->rport; |
| 7101 | #endif |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 7102 | if (altsa) { |
| 7103 | sa = altsa; |
| 7104 | } else { |
| 7105 | sa = src; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7106 | } |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7107 | peer_supports_idata = 0; |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7108 | peer_supports_ecn = 0; |
| 7109 | peer_supports_prsctp = 0; |
| 7110 | peer_supports_auth = 0; |
| 7111 | peer_supports_asconf = 0; |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7112 | peer_supports_reconfig = 0; |
| 7113 | peer_supports_nrsack = 0; |
| 7114 | peer_supports_pktdrop = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7115 | TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { |
| 7116 | /* mark all addresses that we have currently on the list */ |
| 7117 | net->dest_state |= SCTP_ADDR_NOT_IN_ASSOC; |
| 7118 | } |
| 7119 | /* does the source address already exist? if so skip it */ |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 7120 | inp = stcb->sctp_ep; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7121 | atomic_add_int(&stcb->asoc.refcnt, 1); |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 7122 | stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net_tmp, dst, stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7123 | atomic_add_int(&stcb->asoc.refcnt, -1); |
| 7124 | |
| 7125 | if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || inp == NULL) { |
| 7126 | /* we must add the source address */ |
| 7127 | /* no scope set here since we have a tcb already. */ |
| 7128 | switch (sa->sa_family) { |
| 7129 | #ifdef INET |
| 7130 | case AF_INET: |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 7131 | if (stcb->asoc.scope.ipv4_addr_legal) { |
Michael Tuexen | 923f735 | 2016-05-01 23:55:35 +0200 | [diff] [blame] | 7132 | 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] | 7133 | return (-1); |
| 7134 | } |
| 7135 | } |
| 7136 | break; |
| 7137 | #endif |
| 7138 | #ifdef INET6 |
| 7139 | case AF_INET6: |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 7140 | if (stcb->asoc.scope.ipv6_addr_legal) { |
Michael Tuexen | 923f735 | 2016-05-01 23:55:35 +0200 | [diff] [blame] | 7141 | 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] | 7142 | return (-2); |
| 7143 | } |
| 7144 | } |
| 7145 | break; |
| 7146 | #endif |
tuexen | 430ca6c | 2012-07-10 23:36:36 +0000 | [diff] [blame] | 7147 | #if defined(__Userspace__) |
| 7148 | case AF_CONN: |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 7149 | if (stcb->asoc.scope.conn_addr_legal) { |
Michael Tuexen | 923f735 | 2016-05-01 23:55:35 +0200 | [diff] [blame] | 7150 | 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] | 7151 | return (-2); |
| 7152 | } |
tuexen | 430ca6c | 2012-07-10 23:36:36 +0000 | [diff] [blame] | 7153 | } |
| 7154 | break; |
| 7155 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7156 | default: |
| 7157 | break; |
| 7158 | } |
| 7159 | } else { |
| 7160 | if (net_tmp != NULL && stcb_tmp == stcb) { |
| 7161 | net_tmp->dest_state &= ~SCTP_ADDR_NOT_IN_ASSOC; |
| 7162 | } else if (stcb_tmp != stcb) { |
| 7163 | /* It belongs to another association? */ |
| 7164 | if (stcb_tmp) |
| 7165 | SCTP_TCB_UNLOCK(stcb_tmp); |
| 7166 | return (-3); |
| 7167 | } |
| 7168 | } |
| 7169 | if (stcb->asoc.state == 0) { |
| 7170 | /* the assoc was freed? */ |
| 7171 | return (-4); |
| 7172 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7173 | /* now we must go through each of the params. */ |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 7174 | phdr = sctp_get_next_param(m, offset, ¶m_buf, sizeof(param_buf)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7175 | while (phdr) { |
| 7176 | ptype = ntohs(phdr->param_type); |
| 7177 | plen = ntohs(phdr->param_length); |
| 7178 | /* |
tuexen | cb5fe8d | 2012-05-04 09:50:27 +0000 | [diff] [blame] | 7179 | * SCTP_PRINTF("ptype => %0x, plen => %d\n", (uint32_t)ptype, |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7180 | * (int)plen); |
| 7181 | */ |
| 7182 | if (offset + plen > limit) { |
| 7183 | break; |
| 7184 | } |
Michael Tuexen | 790a7a2 | 2019-12-20 17:02:02 +0100 | [diff] [blame] | 7185 | if (plen < sizeof(struct sctp_paramhdr)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7186 | break; |
| 7187 | } |
| 7188 | #ifdef INET |
| 7189 | if (ptype == SCTP_IPV4_ADDRESS) { |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 7190 | if (stcb->asoc.scope.ipv4_addr_legal) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7191 | struct sctp_ipv4addr_param *p4, p4_buf; |
| 7192 | |
| 7193 | /* ok get the v4 address and check/add */ |
| 7194 | phdr = sctp_get_next_param(m, offset, |
| 7195 | (struct sctp_paramhdr *)&p4_buf, |
| 7196 | sizeof(p4_buf)); |
| 7197 | if (plen != sizeof(struct sctp_ipv4addr_param) || |
| 7198 | phdr == NULL) { |
| 7199 | return (-5); |
| 7200 | } |
| 7201 | p4 = (struct sctp_ipv4addr_param *)phdr; |
| 7202 | sin.sin_addr.s_addr = p4->addr; |
| 7203 | if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) { |
| 7204 | /* Skip multi-cast addresses */ |
| 7205 | goto next_param; |
| 7206 | } |
| 7207 | if ((sin.sin_addr.s_addr == INADDR_BROADCAST) || |
| 7208 | (sin.sin_addr.s_addr == INADDR_ANY)) { |
| 7209 | goto next_param; |
| 7210 | } |
| 7211 | sa = (struct sockaddr *)&sin; |
| 7212 | inp = stcb->sctp_ep; |
| 7213 | atomic_add_int(&stcb->asoc.refcnt, 1); |
| 7214 | stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net, |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 7215 | dst, stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7216 | atomic_add_int(&stcb->asoc.refcnt, -1); |
| 7217 | |
| 7218 | if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || |
| 7219 | inp == NULL) { |
| 7220 | /* we must add the source address */ |
| 7221 | /* |
| 7222 | * no scope set since we have a tcb |
| 7223 | * already |
| 7224 | */ |
| 7225 | |
| 7226 | /* |
| 7227 | * we must validate the state again |
| 7228 | * here |
| 7229 | */ |
| 7230 | add_it_now: |
| 7231 | if (stcb->asoc.state == 0) { |
| 7232 | /* the assoc was freed? */ |
| 7233 | return (-7); |
| 7234 | } |
Michael Tuexen | 923f735 | 2016-05-01 23:55:35 +0200 | [diff] [blame] | 7235 | 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] | 7236 | return (-8); |
| 7237 | } |
| 7238 | } else if (stcb_tmp == stcb) { |
| 7239 | if (stcb->asoc.state == 0) { |
| 7240 | /* the assoc was freed? */ |
| 7241 | return (-10); |
| 7242 | } |
| 7243 | if (net != NULL) { |
| 7244 | /* clear flag */ |
| 7245 | net->dest_state &= |
| 7246 | ~SCTP_ADDR_NOT_IN_ASSOC; |
| 7247 | } |
| 7248 | } else { |
| 7249 | /* |
| 7250 | * strange, address is in another |
| 7251 | * assoc? straighten out locks. |
| 7252 | */ |
| 7253 | if (stcb_tmp) { |
Michael Tuexen | 348a36c | 2018-08-13 16:24:47 +0200 | [diff] [blame] | 7254 | if (SCTP_GET_STATE(stcb_tmp) == SCTP_STATE_COOKIE_WAIT) { |
Michael Tüxen | ac14228 | 2015-07-28 00:42:03 +0200 | [diff] [blame] | 7255 | struct mbuf *op_err; |
| 7256 | char msg[SCTP_DIAG_INFO_LEN]; |
| 7257 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7258 | /* in setup state we abort this guy */ |
Michael Tuexen | edd369d | 2020-05-19 09:42:15 +0200 | [diff] [blame] | 7259 | SCTP_SNPRINTF(msg, sizeof(msg), |
| 7260 | "%s:%d at %s", __FILE__, __LINE__, __func__); |
Michael Tüxen | ac14228 | 2015-07-28 00:42:03 +0200 | [diff] [blame] | 7261 | op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), |
| 7262 | msg); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7263 | sctp_abort_an_association(stcb_tmp->sctp_ep, |
Michael Tüxen | ac14228 | 2015-07-28 00:42:03 +0200 | [diff] [blame] | 7264 | stcb_tmp, op_err, |
| 7265 | SCTP_SO_NOT_LOCKED); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7266 | goto add_it_now; |
| 7267 | } |
| 7268 | SCTP_TCB_UNLOCK(stcb_tmp); |
| 7269 | } |
| 7270 | |
| 7271 | if (stcb->asoc.state == 0) { |
| 7272 | /* the assoc was freed? */ |
| 7273 | return (-12); |
| 7274 | } |
| 7275 | return (-13); |
| 7276 | } |
| 7277 | } |
| 7278 | } else |
| 7279 | #endif |
| 7280 | #ifdef INET6 |
| 7281 | if (ptype == SCTP_IPV6_ADDRESS) { |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 7282 | if (stcb->asoc.scope.ipv6_addr_legal) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7283 | /* ok get the v6 address and check/add */ |
| 7284 | struct sctp_ipv6addr_param *p6, p6_buf; |
| 7285 | |
| 7286 | phdr = sctp_get_next_param(m, offset, |
| 7287 | (struct sctp_paramhdr *)&p6_buf, |
| 7288 | sizeof(p6_buf)); |
| 7289 | if (plen != sizeof(struct sctp_ipv6addr_param) || |
| 7290 | phdr == NULL) { |
| 7291 | return (-14); |
| 7292 | } |
| 7293 | p6 = (struct sctp_ipv6addr_param *)phdr; |
| 7294 | memcpy((caddr_t)&sin6.sin6_addr, p6->addr, |
| 7295 | sizeof(p6->addr)); |
| 7296 | if (IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) { |
| 7297 | /* Skip multi-cast addresses */ |
| 7298 | goto next_param; |
| 7299 | } |
| 7300 | if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) { |
| 7301 | /* Link local make no sense without scope */ |
| 7302 | goto next_param; |
| 7303 | } |
| 7304 | sa = (struct sockaddr *)&sin6; |
| 7305 | inp = stcb->sctp_ep; |
| 7306 | atomic_add_int(&stcb->asoc.refcnt, 1); |
| 7307 | stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net, |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 7308 | dst, stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7309 | atomic_add_int(&stcb->asoc.refcnt, -1); |
| 7310 | if (stcb_tmp == NULL && |
| 7311 | (inp == stcb->sctp_ep || inp == NULL)) { |
| 7312 | /* |
| 7313 | * we must validate the state again |
| 7314 | * here |
| 7315 | */ |
| 7316 | add_it_now6: |
| 7317 | if (stcb->asoc.state == 0) { |
| 7318 | /* the assoc was freed? */ |
| 7319 | return (-16); |
| 7320 | } |
| 7321 | /* |
| 7322 | * we must add the address, no scope |
| 7323 | * set |
| 7324 | */ |
Michael Tuexen | 923f735 | 2016-05-01 23:55:35 +0200 | [diff] [blame] | 7325 | 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] | 7326 | return (-17); |
| 7327 | } |
| 7328 | } else if (stcb_tmp == stcb) { |
| 7329 | /* |
| 7330 | * we must validate the state again |
| 7331 | * here |
| 7332 | */ |
| 7333 | if (stcb->asoc.state == 0) { |
| 7334 | /* the assoc was freed? */ |
| 7335 | return (-19); |
| 7336 | } |
| 7337 | if (net != NULL) { |
| 7338 | /* clear flag */ |
| 7339 | net->dest_state &= |
| 7340 | ~SCTP_ADDR_NOT_IN_ASSOC; |
| 7341 | } |
| 7342 | } else { |
| 7343 | /* |
| 7344 | * strange, address is in another |
| 7345 | * assoc? straighten out locks. |
| 7346 | */ |
Michael Tüxen | ac14228 | 2015-07-28 00:42:03 +0200 | [diff] [blame] | 7347 | if (stcb_tmp) { |
Michael Tuexen | 348a36c | 2018-08-13 16:24:47 +0200 | [diff] [blame] | 7348 | if (SCTP_GET_STATE(stcb_tmp) == SCTP_STATE_COOKIE_WAIT) { |
Michael Tüxen | ac14228 | 2015-07-28 00:42:03 +0200 | [diff] [blame] | 7349 | struct mbuf *op_err; |
| 7350 | char msg[SCTP_DIAG_INFO_LEN]; |
| 7351 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7352 | /* in setup state we abort this guy */ |
Michael Tuexen | edd369d | 2020-05-19 09:42:15 +0200 | [diff] [blame] | 7353 | SCTP_SNPRINTF(msg, sizeof(msg), |
| 7354 | "%s:%d at %s", __FILE__, __LINE__, __func__); |
Michael Tüxen | ac14228 | 2015-07-28 00:42:03 +0200 | [diff] [blame] | 7355 | op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), |
| 7356 | msg); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7357 | sctp_abort_an_association(stcb_tmp->sctp_ep, |
Michael Tüxen | ac14228 | 2015-07-28 00:42:03 +0200 | [diff] [blame] | 7358 | stcb_tmp, op_err, |
| 7359 | SCTP_SO_NOT_LOCKED); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7360 | goto add_it_now6; |
| 7361 | } |
Michael Tüxen | ac14228 | 2015-07-28 00:42:03 +0200 | [diff] [blame] | 7362 | SCTP_TCB_UNLOCK(stcb_tmp); |
| 7363 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7364 | if (stcb->asoc.state == 0) { |
| 7365 | /* the assoc was freed? */ |
| 7366 | return (-21); |
| 7367 | } |
| 7368 | return (-22); |
| 7369 | } |
| 7370 | } |
| 7371 | } else |
| 7372 | #endif |
| 7373 | if (ptype == SCTP_ECN_CAPABLE) { |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7374 | peer_supports_ecn = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7375 | } else if (ptype == SCTP_ULP_ADAPTATION) { |
| 7376 | if (stcb->asoc.state != SCTP_STATE_OPEN) { |
| 7377 | struct sctp_adaptation_layer_indication ai, *aip; |
| 7378 | |
| 7379 | phdr = sctp_get_next_param(m, offset, |
| 7380 | (struct sctp_paramhdr *)&ai, sizeof(ai)); |
| 7381 | aip = (struct sctp_adaptation_layer_indication *)phdr; |
| 7382 | if (aip) { |
| 7383 | stcb->asoc.peers_adaptation = ntohl(aip->indication); |
| 7384 | stcb->asoc.adaptation_needed = 1; |
| 7385 | } |
| 7386 | } |
| 7387 | } else if (ptype == SCTP_SET_PRIM_ADDR) { |
| 7388 | struct sctp_asconf_addr_param lstore, *fee; |
| 7389 | int lptype; |
| 7390 | struct sockaddr *lsa = NULL; |
| 7391 | #ifdef INET |
| 7392 | struct sctp_asconf_addrv4_param *fii; |
| 7393 | #endif |
| 7394 | |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7395 | if (stcb->asoc.asconf_supported == 0) { |
| 7396 | return (-100); |
| 7397 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7398 | if (plen > sizeof(lstore)) { |
| 7399 | return (-23); |
| 7400 | } |
Michael Tuexen | 790a7a2 | 2019-12-20 17:02:02 +0100 | [diff] [blame] | 7401 | if (plen < sizeof(struct sctp_asconf_addrv4_param)) { |
| 7402 | return (-101); |
| 7403 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7404 | phdr = sctp_get_next_param(m, offset, |
| 7405 | (struct sctp_paramhdr *)&lstore, |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 7406 | plen); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7407 | if (phdr == NULL) { |
| 7408 | return (-24); |
| 7409 | } |
| 7410 | fee = (struct sctp_asconf_addr_param *)phdr; |
| 7411 | lptype = ntohs(fee->addrp.ph.param_type); |
| 7412 | switch (lptype) { |
| 7413 | #ifdef INET |
| 7414 | case SCTP_IPV4_ADDRESS: |
| 7415 | if (plen != |
| 7416 | sizeof(struct sctp_asconf_addrv4_param)) { |
| 7417 | SCTP_PRINTF("Sizeof setprim in init/init ack not %d but %d - ignored\n", |
| 7418 | (int)sizeof(struct sctp_asconf_addrv4_param), |
| 7419 | plen); |
| 7420 | } else { |
| 7421 | fii = (struct sctp_asconf_addrv4_param *)fee; |
| 7422 | sin.sin_addr.s_addr = fii->addrp.addr; |
| 7423 | lsa = (struct sockaddr *)&sin; |
| 7424 | } |
| 7425 | break; |
| 7426 | #endif |
| 7427 | #ifdef INET6 |
| 7428 | case SCTP_IPV6_ADDRESS: |
| 7429 | if (plen != |
| 7430 | sizeof(struct sctp_asconf_addr_param)) { |
| 7431 | SCTP_PRINTF("Sizeof setprim (v6) in init/init ack not %d but %d - ignored\n", |
| 7432 | (int)sizeof(struct sctp_asconf_addr_param), |
| 7433 | plen); |
| 7434 | } else { |
| 7435 | memcpy(sin6.sin6_addr.s6_addr, |
| 7436 | fee->addrp.addr, |
| 7437 | sizeof(fee->addrp.addr)); |
| 7438 | lsa = (struct sockaddr *)&sin6; |
| 7439 | } |
| 7440 | break; |
| 7441 | #endif |
| 7442 | default: |
| 7443 | break; |
| 7444 | } |
| 7445 | if (lsa) { |
| 7446 | (void)sctp_set_primary_addr(stcb, sa, NULL); |
| 7447 | } |
| 7448 | } else if (ptype == SCTP_HAS_NAT_SUPPORT) { |
| 7449 | stcb->asoc.peer_supports_nat = 1; |
| 7450 | } else if (ptype == SCTP_PRSCTP_SUPPORTED) { |
| 7451 | /* Peer supports pr-sctp */ |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7452 | peer_supports_prsctp = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7453 | } else if (ptype == SCTP_SUPPORTED_CHUNK_EXT) { |
| 7454 | /* A supported extension chunk */ |
| 7455 | struct sctp_supported_chunk_types_param *pr_supported; |
| 7456 | uint8_t local_store[SCTP_PARAM_BUFFER_SIZE]; |
| 7457 | int num_ent, i; |
| 7458 | |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 7459 | if (plen > sizeof(local_store)) { |
| 7460 | return (-35); |
| 7461 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7462 | phdr = sctp_get_next_param(m, offset, |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 7463 | (struct sctp_paramhdr *)&local_store, plen); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7464 | if (phdr == NULL) { |
| 7465 | return (-25); |
| 7466 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7467 | pr_supported = (struct sctp_supported_chunk_types_param *)phdr; |
| 7468 | num_ent = plen - sizeof(struct sctp_paramhdr); |
| 7469 | for (i = 0; i < num_ent; i++) { |
| 7470 | switch (pr_supported->chunk_types[i]) { |
| 7471 | case SCTP_ASCONF: |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7472 | peer_supports_asconf = 1; |
t00fcxen | 10c9698 | 2014-12-17 20:37:01 +0000 | [diff] [blame] | 7473 | break; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7474 | case SCTP_ASCONF_ACK: |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7475 | peer_supports_asconf_ack = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7476 | break; |
| 7477 | case SCTP_FORWARD_CUM_TSN: |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7478 | peer_supports_prsctp = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7479 | break; |
| 7480 | case SCTP_PACKET_DROPPED: |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7481 | peer_supports_pktdrop = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7482 | break; |
| 7483 | case SCTP_NR_SELECTIVE_ACK: |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7484 | peer_supports_nrsack = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7485 | break; |
| 7486 | case SCTP_STREAM_RESET: |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7487 | peer_supports_reconfig = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7488 | break; |
| 7489 | case SCTP_AUTHENTICATION: |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7490 | peer_supports_auth = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7491 | break; |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7492 | case SCTP_IDATA: |
| 7493 | peer_supports_idata = 1; |
| 7494 | break; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7495 | default: |
| 7496 | /* one I have not learned yet */ |
| 7497 | break; |
| 7498 | |
| 7499 | } |
| 7500 | } |
| 7501 | } else if (ptype == SCTP_RANDOM) { |
| 7502 | if (plen > sizeof(random_store)) |
| 7503 | break; |
| 7504 | if (got_random) { |
| 7505 | /* already processed a RANDOM */ |
| 7506 | goto next_param; |
| 7507 | } |
| 7508 | phdr = sctp_get_next_param(m, offset, |
| 7509 | (struct sctp_paramhdr *)random_store, |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 7510 | plen); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7511 | if (phdr == NULL) |
| 7512 | return (-26); |
| 7513 | p_random = (struct sctp_auth_random *)phdr; |
| 7514 | random_len = plen - sizeof(*p_random); |
| 7515 | /* enforce the random length */ |
| 7516 | if (random_len != SCTP_AUTH_RANDOM_SIZE_REQUIRED) { |
| 7517 | SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: invalid RANDOM len\n"); |
| 7518 | return (-27); |
| 7519 | } |
| 7520 | got_random = 1; |
| 7521 | } else if (ptype == SCTP_HMAC_LIST) { |
t00fcxen | 05d0bb9 | 2014-09-16 14:25:27 +0000 | [diff] [blame] | 7522 | uint16_t num_hmacs; |
| 7523 | uint16_t i; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7524 | |
| 7525 | if (plen > sizeof(hmacs_store)) |
| 7526 | break; |
| 7527 | if (got_hmacs) { |
| 7528 | /* already processed a HMAC list */ |
| 7529 | goto next_param; |
| 7530 | } |
| 7531 | phdr = sctp_get_next_param(m, offset, |
| 7532 | (struct sctp_paramhdr *)hmacs_store, |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 7533 | plen); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7534 | if (phdr == NULL) |
| 7535 | return (-28); |
| 7536 | hmacs = (struct sctp_auth_hmac_algo *)phdr; |
| 7537 | hmacs_len = plen - sizeof(*hmacs); |
| 7538 | num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]); |
| 7539 | /* validate the hmac list */ |
| 7540 | if (sctp_verify_hmac_param(hmacs, num_hmacs)) { |
| 7541 | return (-29); |
| 7542 | } |
| 7543 | if (stcb->asoc.peer_hmacs != NULL) |
| 7544 | sctp_free_hmaclist(stcb->asoc.peer_hmacs); |
| 7545 | stcb->asoc.peer_hmacs = sctp_alloc_hmaclist(num_hmacs); |
| 7546 | if (stcb->asoc.peer_hmacs != NULL) { |
| 7547 | for (i = 0; i < num_hmacs; i++) { |
| 7548 | (void)sctp_auth_add_hmacid(stcb->asoc.peer_hmacs, |
| 7549 | ntohs(hmacs->hmac_ids[i])); |
| 7550 | } |
| 7551 | } |
| 7552 | got_hmacs = 1; |
| 7553 | } else if (ptype == SCTP_CHUNK_LIST) { |
| 7554 | int i; |
| 7555 | |
| 7556 | if (plen > sizeof(chunks_store)) |
| 7557 | break; |
| 7558 | if (got_chklist) { |
| 7559 | /* already processed a Chunks list */ |
| 7560 | goto next_param; |
| 7561 | } |
| 7562 | phdr = sctp_get_next_param(m, offset, |
| 7563 | (struct sctp_paramhdr *)chunks_store, |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 7564 | plen); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7565 | if (phdr == NULL) |
| 7566 | return (-30); |
| 7567 | chunks = (struct sctp_auth_chunk_list *)phdr; |
| 7568 | num_chunks = plen - sizeof(*chunks); |
| 7569 | if (stcb->asoc.peer_auth_chunks != NULL) |
| 7570 | sctp_clear_chunklist(stcb->asoc.peer_auth_chunks); |
| 7571 | else |
| 7572 | stcb->asoc.peer_auth_chunks = sctp_alloc_chunklist(); |
| 7573 | for (i = 0; i < num_chunks; i++) { |
| 7574 | (void)sctp_auth_add_chunk(chunks->chunk_types[i], |
| 7575 | stcb->asoc.peer_auth_chunks); |
| 7576 | /* record asconf/asconf-ack if listed */ |
| 7577 | if (chunks->chunk_types[i] == SCTP_ASCONF) |
| 7578 | saw_asconf = 1; |
| 7579 | if (chunks->chunk_types[i] == SCTP_ASCONF_ACK) |
| 7580 | saw_asconf_ack = 1; |
| 7581 | |
| 7582 | } |
| 7583 | got_chklist = 1; |
| 7584 | } else if ((ptype == SCTP_HEARTBEAT_INFO) || |
| 7585 | (ptype == SCTP_STATE_COOKIE) || |
| 7586 | (ptype == SCTP_UNRECOG_PARAM) || |
| 7587 | (ptype == SCTP_COOKIE_PRESERVE) || |
| 7588 | (ptype == SCTP_SUPPORTED_ADDRTYPE) || |
| 7589 | (ptype == SCTP_ADD_IP_ADDRESS) || |
| 7590 | (ptype == SCTP_DEL_IP_ADDRESS) || |
| 7591 | (ptype == SCTP_ERROR_CAUSE_IND) || |
| 7592 | (ptype == SCTP_SUCCESS_REPORT)) { |
| 7593 | /* don't care */ ; |
| 7594 | } else { |
| 7595 | if ((ptype & 0x8000) == 0x0000) { |
| 7596 | /* |
| 7597 | * must stop processing the rest of the |
| 7598 | * param's. Any report bits were handled |
| 7599 | * with the call to |
| 7600 | * sctp_arethere_unrecognized_parameters() |
| 7601 | * when the INIT or INIT-ACK was first seen. |
| 7602 | */ |
| 7603 | break; |
| 7604 | } |
| 7605 | } |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 7606 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7607 | next_param: |
| 7608 | offset += SCTP_SIZE32(plen); |
| 7609 | if (offset >= limit) { |
| 7610 | break; |
| 7611 | } |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 7612 | phdr = sctp_get_next_param(m, offset, ¶m_buf, |
| 7613 | sizeof(param_buf)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7614 | } |
| 7615 | /* Now check to see if we need to purge any addresses */ |
| 7616 | TAILQ_FOREACH_SAFE(net, &stcb->asoc.nets, sctp_next, nnet) { |
| 7617 | if ((net->dest_state & SCTP_ADDR_NOT_IN_ASSOC) == |
| 7618 | SCTP_ADDR_NOT_IN_ASSOC) { |
| 7619 | /* This address has been removed from the asoc */ |
| 7620 | /* remove and free it */ |
| 7621 | stcb->asoc.numnets--; |
| 7622 | TAILQ_REMOVE(&stcb->asoc.nets, net, sctp_next); |
| 7623 | sctp_free_remote_addr(net); |
| 7624 | if (net == stcb->asoc.primary_destination) { |
| 7625 | stcb->asoc.primary_destination = NULL; |
| 7626 | sctp_select_primary_destination(stcb); |
| 7627 | } |
| 7628 | } |
| 7629 | } |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7630 | if ((stcb->asoc.ecn_supported == 1) && |
| 7631 | (peer_supports_ecn == 0)) { |
| 7632 | stcb->asoc.ecn_supported = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7633 | } |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7634 | if ((stcb->asoc.prsctp_supported == 1) && |
| 7635 | (peer_supports_prsctp == 0)) { |
| 7636 | stcb->asoc.prsctp_supported = 0; |
| 7637 | } |
| 7638 | if ((stcb->asoc.auth_supported == 1) && |
| 7639 | ((peer_supports_auth == 0) || |
| 7640 | (got_random == 0) || (got_hmacs == 0))) { |
| 7641 | stcb->asoc.auth_supported = 0; |
| 7642 | } |
| 7643 | if ((stcb->asoc.asconf_supported == 1) && |
| 7644 | ((peer_supports_asconf == 0) || (peer_supports_asconf_ack == 0) || |
| 7645 | (stcb->asoc.auth_supported == 0) || |
| 7646 | (saw_asconf == 0) || (saw_asconf_ack == 0))) { |
| 7647 | stcb->asoc.asconf_supported = 0; |
| 7648 | } |
| 7649 | if ((stcb->asoc.reconfig_supported == 1) && |
| 7650 | (peer_supports_reconfig == 0)) { |
| 7651 | stcb->asoc.reconfig_supported = 0; |
| 7652 | } |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7653 | if ((stcb->asoc.idata_supported == 1) && |
| 7654 | (peer_supports_idata == 0)) { |
| 7655 | stcb->asoc.idata_supported = 0; |
| 7656 | } |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7657 | if ((stcb->asoc.nrsack_supported == 1) && |
| 7658 | (peer_supports_nrsack == 0)) { |
| 7659 | stcb->asoc.nrsack_supported = 0; |
| 7660 | } |
| 7661 | if ((stcb->asoc.pktdrop_supported == 1) && |
| 7662 | (peer_supports_pktdrop == 0)){ |
| 7663 | stcb->asoc.pktdrop_supported = 0; |
| 7664 | } |
| 7665 | /* validate authentication required parameters */ |
| 7666 | if ((peer_supports_auth == 0) && (got_chklist == 1)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7667 | /* peer does not support auth but sent a chunks list? */ |
| 7668 | return (-31); |
| 7669 | } |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7670 | if ((peer_supports_asconf == 1) && (peer_supports_auth == 0)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7671 | /* peer supports asconf but not auth? */ |
| 7672 | return (-32); |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7673 | } else if ((peer_supports_asconf == 1) && |
| 7674 | (peer_supports_auth == 1) && |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7675 | ((saw_asconf == 0) || (saw_asconf_ack == 0))) { |
| 7676 | return (-33); |
| 7677 | } |
| 7678 | /* concatenate the full random key */ |
| 7679 | keylen = sizeof(*p_random) + random_len + sizeof(*hmacs) + hmacs_len; |
| 7680 | if (chunks != NULL) { |
| 7681 | keylen += sizeof(*chunks) + num_chunks; |
| 7682 | } |
| 7683 | new_key = sctp_alloc_key(keylen); |
| 7684 | if (new_key != NULL) { |
| 7685 | /* copy in the RANDOM */ |
| 7686 | if (p_random != NULL) { |
| 7687 | keylen = sizeof(*p_random) + random_len; |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 7688 | memcpy(new_key->key, p_random, keylen); |
Michael Tuexen | 8789a6d | 2018-06-02 18:33:47 +0200 | [diff] [blame] | 7689 | } else { |
| 7690 | keylen = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7691 | } |
| 7692 | /* append in the AUTH chunks */ |
| 7693 | if (chunks != NULL) { |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 7694 | memcpy(new_key->key + keylen, chunks, |
| 7695 | sizeof(*chunks) + num_chunks); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7696 | keylen += sizeof(*chunks) + num_chunks; |
| 7697 | } |
| 7698 | /* append in the HMACs */ |
| 7699 | if (hmacs != NULL) { |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 7700 | memcpy(new_key->key + keylen, hmacs, |
| 7701 | sizeof(*hmacs) + hmacs_len); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7702 | } |
| 7703 | } else { |
| 7704 | /* failed to get memory for the key */ |
| 7705 | return (-34); |
| 7706 | } |
| 7707 | if (stcb->asoc.authinfo.peer_random != NULL) |
| 7708 | sctp_free_key(stcb->asoc.authinfo.peer_random); |
| 7709 | stcb->asoc.authinfo.peer_random = new_key; |
| 7710 | sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid); |
| 7711 | sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid); |
| 7712 | |
| 7713 | return (0); |
| 7714 | } |
| 7715 | |
| 7716 | int |
| 7717 | sctp_set_primary_addr(struct sctp_tcb *stcb, struct sockaddr *sa, |
| 7718 | struct sctp_nets *net) |
| 7719 | { |
| 7720 | /* make sure the requested primary address exists in the assoc */ |
| 7721 | if (net == NULL && sa) |
| 7722 | net = sctp_findnet(stcb, sa); |
| 7723 | |
| 7724 | if (net == NULL) { |
| 7725 | /* didn't find the requested primary address! */ |
| 7726 | return (-1); |
| 7727 | } else { |
| 7728 | /* set the primary address */ |
| 7729 | if (net->dest_state & SCTP_ADDR_UNCONFIRMED) { |
| 7730 | /* Must be confirmed, so queue to set */ |
| 7731 | net->dest_state |= SCTP_ADDR_REQ_PRIMARY; |
| 7732 | return (0); |
| 7733 | } |
| 7734 | stcb->asoc.primary_destination = net; |
| 7735 | if (!(net->dest_state & SCTP_ADDR_PF) && (stcb->asoc.alternate)) { |
| 7736 | sctp_free_remote_addr(stcb->asoc.alternate); |
| 7737 | stcb->asoc.alternate = NULL; |
| 7738 | } |
| 7739 | net = TAILQ_FIRST(&stcb->asoc.nets); |
| 7740 | if (net != stcb->asoc.primary_destination) { |
| 7741 | /* first one on the list is NOT the primary |
| 7742 | * sctp_cmpaddr() is much more efficient if |
| 7743 | * the primary is the first on the list, make it |
| 7744 | * so. |
| 7745 | */ |
| 7746 | TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next); |
| 7747 | TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next); |
| 7748 | } |
| 7749 | return (0); |
| 7750 | } |
| 7751 | } |
| 7752 | |
| 7753 | int |
tuexen | 9784e9a | 2011-12-18 13:04:23 +0000 | [diff] [blame] | 7754 | 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] | 7755 | { |
| 7756 | /* |
| 7757 | * This function serves two purposes. It will see if a TAG can be |
| 7758 | * re-used and return 1 for yes it is ok and 0 for don't use that |
| 7759 | * tag. A secondary function it will do is purge out old tags that |
| 7760 | * can be removed. |
| 7761 | */ |
| 7762 | struct sctpvtaghead *chain; |
| 7763 | struct sctp_tagblock *twait_block; |
| 7764 | struct sctpasochead *head; |
| 7765 | struct sctp_tcb *stcb; |
| 7766 | int i; |
| 7767 | |
| 7768 | SCTP_INP_INFO_RLOCK(); |
| 7769 | head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag, |
| 7770 | SCTP_BASE_INFO(hashasocmark))]; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7771 | LIST_FOREACH(stcb, head, sctp_asocs) { |
| 7772 | /* We choose not to lock anything here. TCB's can't be |
| 7773 | * removed since we have the read lock, so they can't |
| 7774 | * be freed on us, same thing for the INP. I may |
| 7775 | * be wrong with this assumption, but we will go |
| 7776 | * with it for now :-) |
| 7777 | */ |
| 7778 | if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
| 7779 | continue; |
| 7780 | } |
| 7781 | if (stcb->asoc.my_vtag == tag) { |
| 7782 | /* candidate */ |
| 7783 | if (stcb->rport != rport) { |
| 7784 | continue; |
| 7785 | } |
| 7786 | if (stcb->sctp_ep->sctp_lport != lport) { |
| 7787 | continue; |
| 7788 | } |
| 7789 | /* Its a used tag set */ |
| 7790 | SCTP_INP_INFO_RUNLOCK(); |
| 7791 | return (0); |
| 7792 | } |
| 7793 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7794 | chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)]; |
| 7795 | /* Now what about timed wait ? */ |
t00fcxen | 14f8fbb | 2013-06-25 09:32:47 +0000 | [diff] [blame] | 7796 | LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7797 | /* |
| 7798 | * Block(s) are present, lets see if we have this tag in the |
| 7799 | * list |
| 7800 | */ |
t00fcxen | 14f8fbb | 2013-06-25 09:32:47 +0000 | [diff] [blame] | 7801 | for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) { |
| 7802 | if (twait_block->vtag_block[i].v_tag == 0) { |
| 7803 | /* not used */ |
| 7804 | continue; |
| 7805 | } else if ((long)twait_block->vtag_block[i].tv_sec_at_expire < |
| 7806 | now->tv_sec) { |
| 7807 | /* Audit expires this guy */ |
| 7808 | twait_block->vtag_block[i].tv_sec_at_expire = 0; |
| 7809 | twait_block->vtag_block[i].v_tag = 0; |
| 7810 | twait_block->vtag_block[i].lport = 0; |
| 7811 | twait_block->vtag_block[i].rport = 0; |
| 7812 | } else if ((twait_block->vtag_block[i].v_tag == tag) && |
| 7813 | (twait_block->vtag_block[i].lport == lport) && |
| 7814 | (twait_block->vtag_block[i].rport == rport)) { |
| 7815 | /* Bad tag, sorry :< */ |
| 7816 | SCTP_INP_INFO_RUNLOCK(); |
| 7817 | return (0); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7818 | } |
| 7819 | } |
| 7820 | } |
| 7821 | SCTP_INP_INFO_RUNLOCK(); |
| 7822 | return (1); |
| 7823 | } |
| 7824 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7825 | static void |
tuexen | 9784e9a | 2011-12-18 13:04:23 +0000 | [diff] [blame] | 7826 | sctp_drain_mbufs(struct sctp_tcb *stcb) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7827 | { |
| 7828 | /* |
| 7829 | * We must hunt this association for MBUF's past the cumack (i.e. |
| 7830 | * out of order data that we can renege on). |
| 7831 | */ |
| 7832 | struct sctp_association *asoc; |
| 7833 | struct sctp_tmit_chunk *chk, *nchk; |
| 7834 | uint32_t cumulative_tsn_p1; |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7835 | struct sctp_queued_to_read *control, *ncontrol; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7836 | int cnt, strmat; |
| 7837 | uint32_t gap, i; |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 7838 | int fnd = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7839 | |
| 7840 | /* We look for anything larger than the cum-ack + 1 */ |
| 7841 | |
| 7842 | asoc = &stcb->asoc; |
| 7843 | if (asoc->cumulative_tsn == asoc->highest_tsn_inside_map) { |
| 7844 | /* none we can reneg on. */ |
| 7845 | return; |
| 7846 | } |
| 7847 | SCTP_STAT_INCR(sctps_protocol_drains_done); |
| 7848 | cumulative_tsn_p1 = asoc->cumulative_tsn + 1; |
| 7849 | cnt = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7850 | /* Ok that was fun, now we will drain all the inbound streams? */ |
| 7851 | for (strmat = 0; strmat < asoc->streamincnt; strmat++) { |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7852 | TAILQ_FOREACH_SAFE(control, &asoc->strmin[strmat].inqueue, next_instrm, ncontrol) { |
Michael Tuexen | fdcf790 | 2016-08-06 14:39:31 +0200 | [diff] [blame] | 7853 | #ifdef INVARIANTS |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7854 | if (control->on_strm_q != SCTP_ON_ORDERED ) { |
Michael Tuexen | fdcf790 | 2016-08-06 14:39:31 +0200 | [diff] [blame] | 7855 | panic("Huh control: %p on_q: %d -- not ordered?", |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7856 | control, control->on_strm_q); |
Michael Tuexen | fdcf790 | 2016-08-06 14:39:31 +0200 | [diff] [blame] | 7857 | } |
| 7858 | #endif |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7859 | if (SCTP_TSN_GT(control->sinfo_tsn, cumulative_tsn_p1)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7860 | /* Yep it is above cum-ack */ |
| 7861 | cnt++; |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7862 | SCTP_CALC_TSN_TO_GAP(gap, control->sinfo_tsn, asoc->mapping_array_base_tsn); |
| 7863 | KASSERT(control->length > 0, ("control has zero length")); |
| 7864 | if (asoc->size_on_all_streams >= control->length) { |
| 7865 | asoc->size_on_all_streams -= control->length; |
| 7866 | } else { |
| 7867 | #ifdef INVARIANTS |
| 7868 | panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length); |
| 7869 | #else |
| 7870 | asoc->size_on_all_streams = 0; |
| 7871 | #endif |
| 7872 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7873 | sctp_ucount_decr(asoc->cnt_on_all_streams); |
| 7874 | SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap); |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7875 | if (control->on_read_q) { |
| 7876 | TAILQ_REMOVE(&stcb->sctp_ep->read_queue, control, next); |
| 7877 | control->on_read_q = 0; |
Michael Tuexen | fdcf790 | 2016-08-06 14:39:31 +0200 | [diff] [blame] | 7878 | } |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7879 | TAILQ_REMOVE(&asoc->strmin[strmat].inqueue, control, next_instrm); |
| 7880 | control->on_strm_q = 0; |
| 7881 | if (control->data) { |
| 7882 | sctp_m_freem(control->data); |
| 7883 | control->data = NULL; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7884 | } |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7885 | sctp_free_remote_addr(control->whoFrom); |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7886 | /* Now its reasm? */ |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7887 | TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) { |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7888 | cnt++; |
Michael Tuexen | 00657ac | 2016-12-07 21:53:26 +0100 | [diff] [blame] | 7889 | 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] | 7890 | KASSERT(chk->send_size > 0, ("chunk has zero length")); |
| 7891 | if (asoc->size_on_reasm_queue >= chk->send_size) { |
| 7892 | asoc->size_on_reasm_queue -= chk->send_size; |
| 7893 | } else { |
| 7894 | #ifdef INVARIANTS |
| 7895 | panic("size_on_reasm_queue = %u smaller than chunk length %u", asoc->size_on_reasm_queue, chk->send_size); |
| 7896 | #else |
| 7897 | asoc->size_on_reasm_queue = 0; |
| 7898 | #endif |
| 7899 | } |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7900 | sctp_ucount_decr(asoc->cnt_on_reasm_queue); |
| 7901 | SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap); |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7902 | TAILQ_REMOVE(&control->reasm, chk, sctp_next); |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7903 | if (chk->data) { |
| 7904 | sctp_m_freem(chk->data); |
| 7905 | chk->data = NULL; |
| 7906 | } |
| 7907 | sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); |
| 7908 | } |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7909 | sctp_free_a_readq(stcb, control); |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7910 | } |
| 7911 | } |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7912 | TAILQ_FOREACH_SAFE(control, &asoc->strmin[strmat].uno_inqueue, next_instrm, ncontrol) { |
Michael Tuexen | fdcf790 | 2016-08-06 14:39:31 +0200 | [diff] [blame] | 7913 | #ifdef INVARIANTS |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7914 | if (control->on_strm_q != SCTP_ON_UNORDERED ) { |
Michael Tuexen | fdcf790 | 2016-08-06 14:39:31 +0200 | [diff] [blame] | 7915 | panic("Huh control: %p on_q: %d -- not unordered?", |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7916 | control, control->on_strm_q); |
Michael Tuexen | fdcf790 | 2016-08-06 14:39:31 +0200 | [diff] [blame] | 7917 | } |
| 7918 | #endif |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7919 | if (SCTP_TSN_GT(control->sinfo_tsn, cumulative_tsn_p1)) { |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7920 | /* Yep it is above cum-ack */ |
| 7921 | cnt++; |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7922 | SCTP_CALC_TSN_TO_GAP(gap, control->sinfo_tsn, asoc->mapping_array_base_tsn); |
| 7923 | KASSERT(control->length > 0, ("control has zero length")); |
| 7924 | if (asoc->size_on_all_streams >= control->length) { |
| 7925 | asoc->size_on_all_streams -= control->length; |
| 7926 | } else { |
| 7927 | #ifdef INVARIANTS |
| 7928 | panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length); |
| 7929 | #else |
| 7930 | asoc->size_on_all_streams = 0; |
| 7931 | #endif |
| 7932 | } |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7933 | sctp_ucount_decr(asoc->cnt_on_all_streams); |
| 7934 | SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap); |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7935 | if (control->on_read_q) { |
| 7936 | TAILQ_REMOVE(&stcb->sctp_ep->read_queue, control, next); |
| 7937 | control->on_read_q = 0; |
Michael Tuexen | fdcf790 | 2016-08-06 14:39:31 +0200 | [diff] [blame] | 7938 | } |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7939 | TAILQ_REMOVE(&asoc->strmin[strmat].uno_inqueue, control, next_instrm); |
| 7940 | control->on_strm_q = 0; |
| 7941 | if (control->data) { |
| 7942 | sctp_m_freem(control->data); |
| 7943 | control->data = NULL; |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7944 | } |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7945 | sctp_free_remote_addr(control->whoFrom); |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7946 | /* Now its reasm? */ |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7947 | TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) { |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7948 | cnt++; |
Michael Tuexen | 00657ac | 2016-12-07 21:53:26 +0100 | [diff] [blame] | 7949 | 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] | 7950 | KASSERT(chk->send_size > 0, ("chunk has zero length")); |
| 7951 | if (asoc->size_on_reasm_queue >= chk->send_size) { |
| 7952 | asoc->size_on_reasm_queue -= chk->send_size; |
| 7953 | } else { |
| 7954 | #ifdef INVARIANTS |
| 7955 | panic("size_on_reasm_queue = %u smaller than chunk length %u", asoc->size_on_reasm_queue, chk->send_size); |
| 7956 | #else |
| 7957 | asoc->size_on_reasm_queue = 0; |
| 7958 | #endif |
| 7959 | } |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7960 | sctp_ucount_decr(asoc->cnt_on_reasm_queue); |
| 7961 | SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap); |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7962 | TAILQ_REMOVE(&control->reasm, chk, sctp_next); |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7963 | if (chk->data) { |
| 7964 | sctp_m_freem(chk->data); |
| 7965 | chk->data = NULL; |
| 7966 | } |
| 7967 | sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); |
| 7968 | } |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7969 | sctp_free_a_readq(stcb, control); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7970 | } |
| 7971 | } |
| 7972 | } |
| 7973 | if (cnt) { |
| 7974 | /* We must back down to see what the new highest is */ |
| 7975 | for (i = asoc->highest_tsn_inside_map; SCTP_TSN_GE(i, asoc->mapping_array_base_tsn); i--) { |
| 7976 | SCTP_CALC_TSN_TO_GAP(gap, i, asoc->mapping_array_base_tsn); |
| 7977 | if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) { |
| 7978 | asoc->highest_tsn_inside_map = i; |
| 7979 | fnd = 1; |
| 7980 | break; |
| 7981 | } |
| 7982 | } |
| 7983 | if (!fnd) { |
| 7984 | asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn - 1; |
| 7985 | } |
| 7986 | |
| 7987 | /* |
| 7988 | * Question, should we go through the delivery queue? The only |
| 7989 | * reason things are on here is the app not reading OR a p-d-api up. |
| 7990 | * An attacker COULD send enough in to initiate the PD-API and then |
| 7991 | * send a bunch of stuff to other streams... these would wind up on |
| 7992 | * the delivery queue.. and then we would not get to them. But in |
| 7993 | * order to do this I then have to back-track and un-deliver |
| 7994 | * sequence numbers in streams.. el-yucko. I think for now we will |
| 7995 | * NOT look at the delivery queue and leave it to be something to |
| 7996 | * consider later. An alternative would be to abort the P-D-API with |
| 7997 | * a notification and then deliver the data.... Or another method |
| 7998 | * might be to keep track of how many times the situation occurs and |
| 7999 | * if we see a possible attack underway just abort the association. |
| 8000 | */ |
| 8001 | #ifdef SCTP_DEBUG |
| 8002 | SCTPDBG(SCTP_DEBUG_PCB1, "Freed %d chunks from reneg harvest\n", cnt); |
| 8003 | #endif |
| 8004 | /* |
| 8005 | * Now do we need to find a new |
| 8006 | * asoc->highest_tsn_inside_map? |
| 8007 | */ |
| 8008 | asoc->last_revoke_count = cnt; |
Michael Tuexen | 61f6683 | 2020-02-09 23:15:19 +0100 | [diff] [blame] | 8009 | sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL, |
Michael Tuexen | 21b55df | 2020-02-09 23:46:32 +0100 | [diff] [blame] | 8010 | SCTP_FROM_SCTP_PCB + SCTP_LOC_11); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 8011 | /*sa_ignore NO_NULL_CHK*/ |
| 8012 | sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED); |
| 8013 | 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] | 8014 | } |
| 8015 | /* |
| 8016 | * Another issue, in un-setting the TSN's in the mapping array we |
| 8017 | * DID NOT adjust the highest_tsn marker. This will cause one of two |
| 8018 | * things to occur. It may cause us to do extra work in checking for |
| 8019 | * our mapping array movement. More importantly it may cause us to |
| 8020 | * SACK every datagram. This may not be a bad thing though since we |
| 8021 | * will recover once we get our cum-ack above and all this stuff we |
| 8022 | * dumped recovered. |
| 8023 | */ |
| 8024 | } |
| 8025 | |
| 8026 | void |
| 8027 | sctp_drain() |
| 8028 | { |
| 8029 | /* |
| 8030 | * We must walk the PCB lists for ALL associations here. The system |
| 8031 | * is LOW on MBUF's and needs help. This is where reneging will |
| 8032 | * occur. We really hope this does NOT happen! |
| 8033 | */ |
| 8034 | #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 |
| 8035 | VNET_ITERATOR_DECL(vnet_iter); |
| 8036 | #else |
| 8037 | struct sctp_inpcb *inp; |
| 8038 | struct sctp_tcb *stcb; |
| 8039 | |
| 8040 | SCTP_STAT_INCR(sctps_protocol_drain_calls); |
| 8041 | if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) { |
| 8042 | return; |
| 8043 | } |
| 8044 | #endif |
| 8045 | #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 |
| 8046 | VNET_LIST_RLOCK_NOSLEEP(); |
| 8047 | VNET_FOREACH(vnet_iter) { |
| 8048 | CURVNET_SET(vnet_iter); |
| 8049 | struct sctp_inpcb *inp; |
| 8050 | struct sctp_tcb *stcb; |
| 8051 | #endif |
| 8052 | |
| 8053 | #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 |
| 8054 | SCTP_STAT_INCR(sctps_protocol_drain_calls); |
| 8055 | if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) { |
| 8056 | #ifdef VIMAGE |
| 8057 | continue; |
| 8058 | #else |
| 8059 | return; |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 8060 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 8061 | } |
| 8062 | #endif |
| 8063 | SCTP_INP_INFO_RLOCK(); |
| 8064 | LIST_FOREACH(inp, &SCTP_BASE_INFO(listhead), sctp_list) { |
| 8065 | /* For each endpoint */ |
| 8066 | SCTP_INP_RLOCK(inp); |
| 8067 | LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { |
| 8068 | /* For each association */ |
| 8069 | SCTP_TCB_LOCK(stcb); |
tuexen | 9784e9a | 2011-12-18 13:04:23 +0000 | [diff] [blame] | 8070 | sctp_drain_mbufs(stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 8071 | SCTP_TCB_UNLOCK(stcb); |
| 8072 | } |
| 8073 | SCTP_INP_RUNLOCK(inp); |
| 8074 | } |
| 8075 | SCTP_INP_INFO_RUNLOCK(); |
| 8076 | #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 |
| 8077 | CURVNET_RESTORE(); |
| 8078 | } |
| 8079 | VNET_LIST_RUNLOCK_NOSLEEP(); |
| 8080 | #endif |
| 8081 | } |
| 8082 | |
| 8083 | /* |
| 8084 | * start a new iterator |
| 8085 | * iterates through all endpoints and associations based on the pcb_state |
| 8086 | * flags and asoc_state. "af" (mandatory) is executed for all matching |
| 8087 | * assocs and "ef" (optional) is executed when the iterator completes. |
| 8088 | * "inpf" (optional) is executed for each new endpoint as it is being |
| 8089 | * iterated through. inpe (optional) is called when the inp completes |
| 8090 | * its way through all the stcbs. |
| 8091 | */ |
| 8092 | int |
| 8093 | sctp_initiate_iterator(inp_func inpf, |
| 8094 | asoc_func af, |
| 8095 | inp_func inpe, |
| 8096 | uint32_t pcb_state, |
| 8097 | uint32_t pcb_features, |
| 8098 | uint32_t asoc_state, |
| 8099 | void *argp, |
| 8100 | uint32_t argi, |
| 8101 | end_func ef, |
| 8102 | struct sctp_inpcb *s_inp, |
| 8103 | uint8_t chunk_output_off) |
| 8104 | { |
| 8105 | struct sctp_iterator *it = NULL; |
| 8106 | |
| 8107 | if (af == NULL) { |
| 8108 | return (-1); |
| 8109 | } |
Michael Tuexen | 71a83fb | 2016-02-16 21:52:58 +0100 | [diff] [blame] | 8110 | if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) { |
| 8111 | SCTP_PRINTF("%s: abort on initialize being %d\n", __func__, |
| 8112 | SCTP_BASE_VAR(sctp_pcb_initialized)); |
| 8113 | return (-1); |
| 8114 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 8115 | SCTP_MALLOC(it, struct sctp_iterator *, sizeof(struct sctp_iterator), |
| 8116 | SCTP_M_ITER); |
| 8117 | if (it == NULL) { |
| 8118 | SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM); |
Michael Tuexen | 287284c | 2020-01-12 14:34:21 +0100 | [diff] [blame] | 8119 | return (-1); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 8120 | } |
| 8121 | memset(it, 0, sizeof(*it)); |
| 8122 | it->function_assoc = af; |
| 8123 | it->function_inp = inpf; |
| 8124 | if (inpf) |
| 8125 | it->done_current_ep = 0; |
| 8126 | else |
| 8127 | it->done_current_ep = 1; |
| 8128 | it->function_atend = ef; |
| 8129 | it->pointer = argp; |
| 8130 | it->val = argi; |
| 8131 | it->pcb_flags = pcb_state; |
| 8132 | it->pcb_features = pcb_features; |
| 8133 | it->asoc_state = asoc_state; |
| 8134 | it->function_inp_end = inpe; |
| 8135 | it->no_chunk_output = chunk_output_off; |
| 8136 | #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 |
| 8137 | it->vn = curvnet; |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 8138 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 8139 | if (s_inp) { |
| 8140 | /* Assume lock is held here */ |
| 8141 | it->inp = s_inp; |
| 8142 | SCTP_INP_INCR_REF(it->inp); |
| 8143 | it->iterator_flags = SCTP_ITERATOR_DO_SINGLE_INP; |
| 8144 | } else { |
| 8145 | SCTP_INP_INFO_RLOCK(); |
| 8146 | it->inp = LIST_FIRST(&SCTP_BASE_INFO(listhead)); |
| 8147 | if (it->inp) { |
| 8148 | SCTP_INP_INCR_REF(it->inp); |
| 8149 | } |
| 8150 | SCTP_INP_INFO_RUNLOCK(); |
| 8151 | it->iterator_flags = SCTP_ITERATOR_DO_ALL_INP; |
| 8152 | |
| 8153 | } |
| 8154 | SCTP_IPI_ITERATOR_WQ_LOCK(); |
Michael Tuexen | 71a83fb | 2016-02-16 21:52:58 +0100 | [diff] [blame] | 8155 | if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) { |
| 8156 | SCTP_IPI_ITERATOR_WQ_UNLOCK(); |
| 8157 | SCTP_PRINTF("%s: rollback on initialize being %d it=%p\n", __func__, |
| 8158 | SCTP_BASE_VAR(sctp_pcb_initialized), it); |
| 8159 | SCTP_FREE(it, SCTP_M_ITER); |
| 8160 | return (-1); |
| 8161 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 8162 | TAILQ_INSERT_TAIL(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr); |
| 8163 | if (sctp_it_ctl.iterator_running == 0) { |
| 8164 | sctp_wakeup_iterator(); |
| 8165 | } |
| 8166 | SCTP_IPI_ITERATOR_WQ_UNLOCK(); |
| 8167 | /* sa_ignore MEMLEAK {memory is put on the tailq for the iterator} */ |
| 8168 | return (0); |
| 8169 | } |