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 |
| 841 | * if its given. We do this especially for |
| 842 | * panda who might recycle indexes fast. |
| 843 | */ |
| 844 | if (if_name) { |
tuexen | b740ef6 | 2011-11-13 11:58:06 +0000 | [diff] [blame] | 845 | if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, SCTP_IFNAMSIZ) == 0) { |
| 846 | /* They match its a correct delete */ |
| 847 | valid = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 848 | } |
| 849 | } |
| 850 | if (!valid) { |
| 851 | /* last ditch check ifn_index */ |
| 852 | if (ifn_index == sctp_ifap->ifn_p->ifn_index) { |
| 853 | valid = 1; |
| 854 | } |
| 855 | } |
| 856 | if (!valid) { |
| 857 | SCTPDBG(SCTP_DEBUG_PCB4, "ifn:%d ifname:%s does not match addresses\n", |
| 858 | ifn_index, ((if_name == NULL) ? "NULL" : if_name)); |
| 859 | SCTPDBG(SCTP_DEBUG_PCB4, "ifn:%d ifname:%s - ignoring delete\n", |
| 860 | sctp_ifap->ifn_p->ifn_index, sctp_ifap->ifn_p->ifn_name); |
| 861 | SCTP_IPI_ADDR_WUNLOCK(); |
| 862 | return; |
| 863 | } |
| 864 | } |
t00fcxen | 8fca7ff | 2012-09-05 18:58:42 +0000 | [diff] [blame] | 865 | SCTPDBG(SCTP_DEBUG_PCB4, "Deleting ifa %p\n", (void *)sctp_ifap); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 866 | sctp_ifap->localifa_flags &= SCTP_ADDR_VALID; |
t00fcxen | 97887d6 | 2014-03-29 21:31:12 +0000 | [diff] [blame] | 867 | /* |
| 868 | * We don't set the flag. This means that the structure will |
| 869 | * hang around in EP's that have bound specific to it until |
| 870 | * they close. This gives us TCP like behavior if someone |
| 871 | * removes an address (or for that matter adds it right back). |
| 872 | */ |
| 873 | /* sctp_ifap->localifa_flags |= SCTP_BEING_DELETED; */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 874 | vrf->total_ifa_count--; |
| 875 | LIST_REMOVE(sctp_ifap, next_bucket); |
| 876 | sctp_remove_ifa_from_ifn(sctp_ifap); |
| 877 | } |
| 878 | #ifdef SCTP_DEBUG |
| 879 | else { |
| 880 | SCTPDBG(SCTP_DEBUG_PCB4, "Del Addr-ifn:%d Could not find address:", |
| 881 | ifn_index); |
| 882 | SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr); |
| 883 | } |
| 884 | #endif |
| 885 | |
| 886 | out_now: |
| 887 | SCTP_IPI_ADDR_WUNLOCK(); |
| 888 | if (sctp_ifap) { |
| 889 | struct sctp_laddr *wi; |
| 890 | |
| 891 | wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr); |
| 892 | if (wi == NULL) { |
| 893 | /* |
| 894 | * Gak, what can we do? We have lost an address |
| 895 | * change can you say HOSED? |
| 896 | */ |
| 897 | SCTPDBG(SCTP_DEBUG_PCB4, "Lost an address change?\n"); |
| 898 | |
| 899 | /* Oops, must decrement the count */ |
| 900 | sctp_free_ifa(sctp_ifap); |
| 901 | return; |
| 902 | } |
| 903 | SCTP_INCR_LADDR_COUNT(); |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 904 | memset(wi, 0, sizeof(*wi)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 905 | (void)SCTP_GETTIME_TIMEVAL(&wi->start_time); |
| 906 | wi->ifa = sctp_ifap; |
| 907 | wi->action = SCTP_DEL_IP_ADDRESS; |
| 908 | SCTP_WQ_ADDR_LOCK(); |
| 909 | /* |
| 910 | * Should this really be a tailq? As it is we will process the |
| 911 | * newest first :-0 |
| 912 | */ |
| 913 | LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 914 | sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ, |
| 915 | (struct sctp_inpcb *)NULL, |
| 916 | (struct sctp_tcb *)NULL, |
| 917 | (struct sctp_nets *)NULL); |
Michael Tuexen | 96939f1 | 2017-09-20 23:31:24 +0200 | [diff] [blame] | 918 | SCTP_WQ_ADDR_UNLOCK(); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 919 | } |
| 920 | return; |
| 921 | } |
| 922 | |
| 923 | |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 924 | static int |
| 925 | sctp_does_stcb_own_this_addr(struct sctp_tcb *stcb, struct sockaddr *to) |
| 926 | { |
t00fcxen | aff14b6 | 2013-11-02 18:02:02 +0000 | [diff] [blame] | 927 | int loopback_scope; |
| 928 | #if defined(INET) |
| 929 | int ipv4_local_scope, ipv4_addr_legal; |
| 930 | #endif |
| 931 | #if defined(INET6) |
| 932 | int local_scope, site_scope, ipv6_addr_legal; |
| 933 | #endif |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 934 | #if defined(__Userspace__) |
| 935 | int conn_addr_legal; |
| 936 | #endif |
| 937 | struct sctp_vrf *vrf; |
| 938 | struct sctp_ifn *sctp_ifn; |
| 939 | struct sctp_ifa *sctp_ifa; |
| 940 | |
| 941 | loopback_scope = stcb->asoc.scope.loopback_scope; |
t00fcxen | aff14b6 | 2013-11-02 18:02:02 +0000 | [diff] [blame] | 942 | #if defined(INET) |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 943 | ipv4_local_scope = stcb->asoc.scope.ipv4_local_scope; |
t00fcxen | aff14b6 | 2013-11-02 18:02:02 +0000 | [diff] [blame] | 944 | ipv4_addr_legal = stcb->asoc.scope.ipv4_addr_legal; |
| 945 | #endif |
| 946 | #if defined(INET6) |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 947 | local_scope = stcb->asoc.scope.local_scope; |
| 948 | site_scope = stcb->asoc.scope.site_scope; |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 949 | ipv6_addr_legal = stcb->asoc.scope.ipv6_addr_legal; |
t00fcxen | aff14b6 | 2013-11-02 18:02:02 +0000 | [diff] [blame] | 950 | #endif |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 951 | #if defined(__Userspace__) |
| 952 | conn_addr_legal = stcb->asoc.scope.conn_addr_legal; |
| 953 | #endif |
| 954 | |
| 955 | SCTP_IPI_ADDR_RLOCK(); |
| 956 | vrf = sctp_find_vrf(stcb->asoc.vrf_id); |
| 957 | if (vrf == NULL) { |
| 958 | /* no vrf, no addresses */ |
| 959 | SCTP_IPI_ADDR_RUNLOCK(); |
| 960 | return (0); |
| 961 | } |
| 962 | |
| 963 | if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { |
| 964 | LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) { |
| 965 | if ((loopback_scope == 0) && |
| 966 | SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) { |
| 967 | continue; |
| 968 | } |
| 969 | LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) { |
| 970 | if (sctp_is_addr_restricted(stcb, sctp_ifa) && |
| 971 | (!sctp_is_addr_pending(stcb, sctp_ifa))) { |
| 972 | /* We allow pending addresses, where we |
| 973 | * have sent an asconf-add to be considered |
| 974 | * valid. |
| 975 | */ |
| 976 | continue; |
| 977 | } |
t00fcxen | ba8aa20 | 2013-11-16 15:37:17 +0000 | [diff] [blame] | 978 | if (sctp_ifa->address.sa.sa_family != to->sa_family) { |
| 979 | continue; |
| 980 | } |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 981 | switch (sctp_ifa->address.sa.sa_family) { |
| 982 | #ifdef INET |
| 983 | case AF_INET: |
| 984 | if (ipv4_addr_legal) { |
| 985 | struct sockaddr_in *sin, *rsin; |
| 986 | |
| 987 | sin = &sctp_ifa->address.sin; |
| 988 | rsin = (struct sockaddr_in *)to; |
| 989 | if ((ipv4_local_scope == 0) && |
| 990 | IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) { |
| 991 | continue; |
| 992 | } |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 993 | #if defined(__FreeBSD__) |
| 994 | if (prison_check_ip4(stcb->sctp_ep->ip_inp.inp.inp_cred, |
| 995 | &sin->sin_addr) != 0) { |
| 996 | continue; |
| 997 | } |
| 998 | #endif |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 999 | if (sin->sin_addr.s_addr == rsin->sin_addr.s_addr) { |
| 1000 | SCTP_IPI_ADDR_RUNLOCK(); |
| 1001 | return (1); |
| 1002 | } |
| 1003 | } |
| 1004 | break; |
| 1005 | #endif |
| 1006 | #ifdef INET6 |
| 1007 | case AF_INET6: |
| 1008 | if (ipv6_addr_legal) { |
| 1009 | struct sockaddr_in6 *sin6, *rsin6; |
| 1010 | #if defined(SCTP_EMBEDDED_V6_SCOPE) && !defined(SCTP_KAME) |
| 1011 | struct sockaddr_in6 lsa6; |
| 1012 | #endif |
| 1013 | sin6 = &sctp_ifa->address.sin6; |
| 1014 | rsin6 = (struct sockaddr_in6 *)to; |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 1015 | #if defined(__FreeBSD__) |
| 1016 | if (prison_check_ip6(stcb->sctp_ep->ip_inp.inp.inp_cred, |
| 1017 | &sin6->sin6_addr) != 0) { |
| 1018 | continue; |
| 1019 | } |
| 1020 | #endif |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 1021 | if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { |
| 1022 | if (local_scope == 0) |
| 1023 | continue; |
| 1024 | #if defined(SCTP_EMBEDDED_V6_SCOPE) |
| 1025 | if (sin6->sin6_scope_id == 0) { |
| 1026 | #ifdef SCTP_KAME |
| 1027 | if (sa6_recoverscope(sin6) != 0) |
| 1028 | continue; |
| 1029 | #else |
| 1030 | lsa6 = *sin6; |
| 1031 | if (in6_recoverscope(&lsa6, |
| 1032 | &lsa6.sin6_addr, |
| 1033 | NULL)) |
| 1034 | continue; |
| 1035 | sin6 = &lsa6; |
| 1036 | #endif /* SCTP_KAME */ |
| 1037 | } |
| 1038 | #endif /* SCTP_EMBEDDED_V6_SCOPE */ |
| 1039 | } |
| 1040 | if ((site_scope == 0) && |
| 1041 | (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) { |
| 1042 | continue; |
| 1043 | } |
| 1044 | if (SCTP6_ARE_ADDR_EQUAL(sin6, rsin6)) { |
| 1045 | SCTP_IPI_ADDR_RUNLOCK(); |
| 1046 | return (1); |
| 1047 | } |
| 1048 | } |
| 1049 | break; |
| 1050 | #endif |
| 1051 | #if defined(__Userspace__) |
| 1052 | case AF_CONN: |
| 1053 | if (conn_addr_legal) { |
| 1054 | struct sockaddr_conn *sconn, *rsconn; |
| 1055 | |
| 1056 | sconn = &sctp_ifa->address.sconn; |
| 1057 | rsconn = (struct sockaddr_conn *)to; |
| 1058 | if (sconn->sconn_addr == rsconn->sconn_addr) { |
| 1059 | SCTP_IPI_ADDR_RUNLOCK(); |
| 1060 | return (1); |
| 1061 | } |
| 1062 | } |
| 1063 | break; |
| 1064 | #endif |
| 1065 | default: |
| 1066 | /* TSNH */ |
| 1067 | break; |
| 1068 | } |
| 1069 | } |
| 1070 | } |
| 1071 | } else { |
| 1072 | struct sctp_laddr *laddr; |
| 1073 | |
| 1074 | LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) { |
| 1075 | if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) { |
| 1076 | SCTPDBG(SCTP_DEBUG_PCB1, "ifa being deleted\n"); |
| 1077 | continue; |
| 1078 | } |
| 1079 | if (sctp_is_addr_restricted(stcb, laddr->ifa) && |
| 1080 | (!sctp_is_addr_pending(stcb, laddr->ifa))) { |
| 1081 | /* We allow pending addresses, where we |
| 1082 | * have sent an asconf-add to be considered |
| 1083 | * valid. |
| 1084 | */ |
| 1085 | continue; |
| 1086 | } |
| 1087 | if (laddr->ifa->address.sa.sa_family != to->sa_family) { |
| 1088 | continue; |
| 1089 | } |
| 1090 | switch (to->sa_family) { |
| 1091 | #ifdef INET |
| 1092 | case AF_INET: |
| 1093 | { |
| 1094 | struct sockaddr_in *sin, *rsin; |
| 1095 | |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 1096 | sin = &laddr->ifa->address.sin; |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 1097 | rsin = (struct sockaddr_in *)to; |
| 1098 | if (sin->sin_addr.s_addr == rsin->sin_addr.s_addr) { |
| 1099 | SCTP_IPI_ADDR_RUNLOCK(); |
| 1100 | return (1); |
| 1101 | } |
| 1102 | break; |
| 1103 | } |
| 1104 | #endif |
| 1105 | #ifdef INET6 |
| 1106 | case AF_INET6: |
| 1107 | { |
| 1108 | struct sockaddr_in6 *sin6, *rsin6; |
| 1109 | |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 1110 | sin6 = &laddr->ifa->address.sin6; |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 1111 | rsin6 = (struct sockaddr_in6 *)to; |
| 1112 | if (SCTP6_ARE_ADDR_EQUAL(sin6, rsin6)) { |
| 1113 | SCTP_IPI_ADDR_RUNLOCK(); |
| 1114 | return (1); |
| 1115 | } |
| 1116 | break; |
| 1117 | } |
| 1118 | |
| 1119 | #endif |
| 1120 | #if defined(__Userspace__) |
| 1121 | case AF_CONN: |
| 1122 | { |
| 1123 | struct sockaddr_conn *sconn, *rsconn; |
| 1124 | |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 1125 | sconn = &laddr->ifa->address.sconn; |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 1126 | rsconn = (struct sockaddr_conn *)to; |
| 1127 | if (sconn->sconn_addr == rsconn->sconn_addr) { |
| 1128 | SCTP_IPI_ADDR_RUNLOCK(); |
| 1129 | return (1); |
| 1130 | } |
| 1131 | break; |
| 1132 | } |
| 1133 | #endif |
| 1134 | default: |
| 1135 | /* TSNH */ |
| 1136 | break; |
| 1137 | } |
| 1138 | |
| 1139 | } |
| 1140 | } |
| 1141 | SCTP_IPI_ADDR_RUNLOCK(); |
| 1142 | return (0); |
| 1143 | } |
| 1144 | |
| 1145 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1146 | static struct sctp_tcb * |
| 1147 | sctp_tcb_special_locate(struct sctp_inpcb **inp_p, struct sockaddr *from, |
| 1148 | struct sockaddr *to, struct sctp_nets **netp, uint32_t vrf_id) |
| 1149 | { |
| 1150 | /**** ASSUMES THE CALLER holds the INP_INFO_RLOCK */ |
| 1151 | /* |
| 1152 | * If we support the TCP model, then we must now dig through to see |
| 1153 | * if we can find our endpoint in the list of tcp ep's. |
| 1154 | */ |
| 1155 | uint16_t lport, rport; |
| 1156 | struct sctppcbhead *ephead; |
| 1157 | struct sctp_inpcb *inp; |
| 1158 | struct sctp_laddr *laddr; |
| 1159 | struct sctp_tcb *stcb; |
| 1160 | struct sctp_nets *net; |
| 1161 | #ifdef SCTP_MVRF |
| 1162 | int fnd, i; |
| 1163 | #endif |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 1164 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1165 | if ((to == NULL) || (from == NULL)) { |
| 1166 | return (NULL); |
| 1167 | } |
| 1168 | |
| 1169 | switch (to->sa_family) { |
| 1170 | #ifdef INET |
| 1171 | case AF_INET: |
| 1172 | if (from->sa_family == AF_INET) { |
| 1173 | lport = ((struct sockaddr_in *)to)->sin_port; |
| 1174 | rport = ((struct sockaddr_in *)from)->sin_port; |
| 1175 | } else { |
| 1176 | return (NULL); |
| 1177 | } |
| 1178 | break; |
| 1179 | #endif |
| 1180 | #ifdef INET6 |
| 1181 | case AF_INET6: |
| 1182 | if (from->sa_family == AF_INET6) { |
| 1183 | lport = ((struct sockaddr_in6 *)to)->sin6_port; |
| 1184 | rport = ((struct sockaddr_in6 *)from)->sin6_port; |
| 1185 | } else { |
| 1186 | return (NULL); |
| 1187 | } |
| 1188 | break; |
| 1189 | #endif |
tuexen | 5d9b974 | 2012-07-11 21:52:01 +0000 | [diff] [blame] | 1190 | #if defined(__Userspace__) |
| 1191 | case AF_CONN: |
| 1192 | if (from->sa_family == AF_CONN) { |
| 1193 | lport = ((struct sockaddr_conn *)to)->sconn_port; |
| 1194 | rport = ((struct sockaddr_conn *)from)->sconn_port; |
| 1195 | } else { |
| 1196 | return (NULL); |
| 1197 | } |
| 1198 | break; |
| 1199 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1200 | default: |
| 1201 | return (NULL); |
| 1202 | } |
| 1203 | ephead = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR((lport | rport), SCTP_BASE_INFO(hashtcpmark))]; |
| 1204 | /* |
| 1205 | * Ok now for each of the guys in this bucket we must look and see: |
| 1206 | * - Does the remote port match. - Does there single association's |
| 1207 | * addresses match this address (to). If so we update p_ep to point |
| 1208 | * to this ep and return the tcb from it. |
| 1209 | */ |
| 1210 | LIST_FOREACH(inp, ephead, sctp_hash) { |
| 1211 | SCTP_INP_RLOCK(inp); |
| 1212 | if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
| 1213 | SCTP_INP_RUNLOCK(inp); |
| 1214 | continue; |
| 1215 | } |
| 1216 | if (lport != inp->sctp_lport) { |
| 1217 | SCTP_INP_RUNLOCK(inp); |
| 1218 | continue; |
| 1219 | } |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 1220 | #if defined(__FreeBSD__) |
| 1221 | switch (to->sa_family) { |
| 1222 | #ifdef INET |
| 1223 | case AF_INET: |
| 1224 | { |
| 1225 | struct sockaddr_in *sin; |
| 1226 | |
| 1227 | sin = (struct sockaddr_in *)to; |
| 1228 | if (prison_check_ip4(inp->ip_inp.inp.inp_cred, |
| 1229 | &sin->sin_addr) != 0) { |
| 1230 | SCTP_INP_RUNLOCK(inp); |
| 1231 | continue; |
| 1232 | } |
| 1233 | break; |
| 1234 | } |
| 1235 | #endif |
| 1236 | #ifdef INET6 |
| 1237 | case AF_INET6: |
| 1238 | { |
| 1239 | struct sockaddr_in6 *sin6; |
| 1240 | |
| 1241 | sin6 = (struct sockaddr_in6 *)to; |
| 1242 | if (prison_check_ip6(inp->ip_inp.inp.inp_cred, |
| 1243 | &sin6->sin6_addr) != 0) { |
| 1244 | SCTP_INP_RUNLOCK(inp); |
| 1245 | continue; |
| 1246 | } |
| 1247 | break; |
| 1248 | } |
| 1249 | #endif |
| 1250 | default: |
| 1251 | SCTP_INP_RUNLOCK(inp); |
| 1252 | continue; |
| 1253 | } |
| 1254 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1255 | #ifdef SCTP_MVRF |
| 1256 | fnd = 0; |
tuexen | 9784e9a | 2011-12-18 13:04:23 +0000 | [diff] [blame] | 1257 | for (i = 0; i < inp->num_vrfs; i++) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1258 | if (inp->m_vrf_ids[i] == vrf_id) { |
| 1259 | fnd = 1; |
| 1260 | break; |
| 1261 | } |
| 1262 | } |
| 1263 | if (fnd == 0) { |
| 1264 | SCTP_INP_RUNLOCK(inp); |
| 1265 | continue; |
| 1266 | } |
| 1267 | #else |
| 1268 | if (inp->def_vrf_id != vrf_id) { |
| 1269 | SCTP_INP_RUNLOCK(inp); |
| 1270 | continue; |
| 1271 | } |
| 1272 | #endif |
| 1273 | /* check to see if the ep has one of the addresses */ |
| 1274 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) { |
| 1275 | /* We are NOT bound all, so look further */ |
| 1276 | int match = 0; |
| 1277 | |
| 1278 | LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { |
| 1279 | |
| 1280 | if (laddr->ifa == NULL) { |
Saúl Ibarra Corretgé | 8b04b41 | 2015-09-26 09:53:06 +0200 | [diff] [blame] | 1281 | SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n", __func__); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1282 | continue; |
| 1283 | } |
| 1284 | if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) { |
| 1285 | SCTPDBG(SCTP_DEBUG_PCB1, "ifa being deleted\n"); |
| 1286 | continue; |
| 1287 | } |
| 1288 | if (laddr->ifa->address.sa.sa_family == |
| 1289 | to->sa_family) { |
| 1290 | /* see if it matches */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1291 | #ifdef INET |
| 1292 | if (from->sa_family == AF_INET) { |
| 1293 | struct sockaddr_in *intf_addr, *sin; |
| 1294 | |
| 1295 | intf_addr = &laddr->ifa->address.sin; |
| 1296 | sin = (struct sockaddr_in *)to; |
| 1297 | if (sin->sin_addr.s_addr == |
| 1298 | intf_addr->sin_addr.s_addr) { |
| 1299 | match = 1; |
| 1300 | break; |
| 1301 | } |
| 1302 | } |
| 1303 | #endif |
| 1304 | #ifdef INET6 |
| 1305 | if (from->sa_family == AF_INET6) { |
| 1306 | struct sockaddr_in6 *intf_addr6; |
| 1307 | struct sockaddr_in6 *sin6; |
| 1308 | |
| 1309 | sin6 = (struct sockaddr_in6 *) |
| 1310 | to; |
| 1311 | intf_addr6 = &laddr->ifa->address.sin6; |
| 1312 | |
| 1313 | if (SCTP6_ARE_ADDR_EQUAL(sin6, |
| 1314 | intf_addr6)) { |
| 1315 | match = 1; |
| 1316 | break; |
| 1317 | } |
| 1318 | } |
| 1319 | #endif |
tuexen | 5d9b974 | 2012-07-11 21:52:01 +0000 | [diff] [blame] | 1320 | #if defined(__Userspace__) |
| 1321 | if (from->sa_family == AF_CONN) { |
| 1322 | struct sockaddr_conn *intf_addr, *sconn; |
| 1323 | |
| 1324 | intf_addr = &laddr->ifa->address.sconn; |
| 1325 | sconn = (struct sockaddr_conn *)to; |
| 1326 | if (sconn->sconn_addr == |
| 1327 | intf_addr->sconn_addr) { |
| 1328 | match = 1; |
| 1329 | break; |
| 1330 | } |
| 1331 | } |
| 1332 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1333 | } |
| 1334 | } |
| 1335 | if (match == 0) { |
| 1336 | /* This endpoint does not have this address */ |
| 1337 | SCTP_INP_RUNLOCK(inp); |
| 1338 | continue; |
| 1339 | } |
| 1340 | } |
| 1341 | /* |
| 1342 | * Ok if we hit here the ep has the address, does it hold |
| 1343 | * the tcb? |
| 1344 | */ |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 1345 | /* XXX: Why don't we TAILQ_FOREACH through sctp_asoc_list? */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1346 | stcb = LIST_FIRST(&inp->sctp_asoc_list); |
| 1347 | if (stcb == NULL) { |
| 1348 | SCTP_INP_RUNLOCK(inp); |
| 1349 | continue; |
| 1350 | } |
| 1351 | SCTP_TCB_LOCK(stcb); |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 1352 | if (!sctp_does_stcb_own_this_addr(stcb, to)) { |
| 1353 | SCTP_TCB_UNLOCK(stcb); |
| 1354 | SCTP_INP_RUNLOCK(inp); |
| 1355 | continue; |
| 1356 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1357 | if (stcb->rport != rport) { |
| 1358 | /* remote port does not match. */ |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 1359 | SCTP_TCB_UNLOCK(stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1360 | SCTP_INP_RUNLOCK(inp); |
| 1361 | continue; |
| 1362 | } |
| 1363 | if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 1364 | SCTP_TCB_UNLOCK(stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1365 | SCTP_INP_RUNLOCK(inp); |
| 1366 | continue; |
| 1367 | } |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 1368 | if (!sctp_does_stcb_own_this_addr(stcb, to)) { |
| 1369 | SCTP_TCB_UNLOCK(stcb); |
| 1370 | SCTP_INP_RUNLOCK(inp); |
| 1371 | continue; |
| 1372 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1373 | /* Does this TCB have a matching address? */ |
| 1374 | TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { |
| 1375 | |
| 1376 | if (net->ro._l_addr.sa.sa_family != from->sa_family) { |
| 1377 | /* not the same family, can't be a match */ |
| 1378 | continue; |
| 1379 | } |
| 1380 | switch (from->sa_family) { |
| 1381 | #ifdef INET |
| 1382 | case AF_INET: |
| 1383 | { |
| 1384 | struct sockaddr_in *sin, *rsin; |
| 1385 | |
| 1386 | sin = (struct sockaddr_in *)&net->ro._l_addr; |
| 1387 | rsin = (struct sockaddr_in *)from; |
| 1388 | if (sin->sin_addr.s_addr == |
| 1389 | rsin->sin_addr.s_addr) { |
| 1390 | /* found it */ |
| 1391 | if (netp != NULL) { |
| 1392 | *netp = net; |
| 1393 | } |
| 1394 | /* Update the endpoint pointer */ |
| 1395 | *inp_p = inp; |
| 1396 | SCTP_INP_RUNLOCK(inp); |
| 1397 | return (stcb); |
| 1398 | } |
| 1399 | break; |
| 1400 | } |
| 1401 | #endif |
| 1402 | #ifdef INET6 |
| 1403 | case AF_INET6: |
| 1404 | { |
| 1405 | struct sockaddr_in6 *sin6, *rsin6; |
| 1406 | |
| 1407 | sin6 = (struct sockaddr_in6 *)&net->ro._l_addr; |
| 1408 | rsin6 = (struct sockaddr_in6 *)from; |
| 1409 | if (SCTP6_ARE_ADDR_EQUAL(sin6, |
| 1410 | rsin6)) { |
| 1411 | /* found it */ |
| 1412 | if (netp != NULL) { |
| 1413 | *netp = net; |
| 1414 | } |
| 1415 | /* Update the endpoint pointer */ |
| 1416 | *inp_p = inp; |
| 1417 | SCTP_INP_RUNLOCK(inp); |
| 1418 | return (stcb); |
| 1419 | } |
| 1420 | break; |
| 1421 | } |
| 1422 | #endif |
tuexen | 5d9b974 | 2012-07-11 21:52:01 +0000 | [diff] [blame] | 1423 | #if defined(__Userspace__) |
| 1424 | case AF_CONN: |
| 1425 | { |
| 1426 | struct sockaddr_conn *sconn, *rsconn; |
| 1427 | |
| 1428 | sconn = (struct sockaddr_conn *)&net->ro._l_addr; |
| 1429 | rsconn = (struct sockaddr_conn *)from; |
| 1430 | if (sconn->sconn_addr == rsconn->sconn_addr) { |
| 1431 | /* found it */ |
| 1432 | if (netp != NULL) { |
| 1433 | *netp = net; |
| 1434 | } |
| 1435 | /* Update the endpoint pointer */ |
| 1436 | *inp_p = inp; |
| 1437 | SCTP_INP_RUNLOCK(inp); |
| 1438 | return (stcb); |
| 1439 | } |
| 1440 | break; |
| 1441 | } |
| 1442 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1443 | default: |
| 1444 | /* TSNH */ |
| 1445 | break; |
| 1446 | } |
| 1447 | } |
| 1448 | SCTP_TCB_UNLOCK(stcb); |
| 1449 | SCTP_INP_RUNLOCK(inp); |
| 1450 | } |
| 1451 | return (NULL); |
| 1452 | } |
| 1453 | |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 1454 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1455 | /* |
| 1456 | * rules for use |
| 1457 | * |
| 1458 | * 1) If I return a NULL you must decrement any INP ref cnt. 2) If I find an |
| 1459 | * stcb, both will be locked (locked_tcb and stcb) but decrement will be done |
| 1460 | * (if locked == NULL). 3) Decrement happens on return ONLY if locked == |
| 1461 | * NULL. |
| 1462 | */ |
| 1463 | |
| 1464 | struct sctp_tcb * |
| 1465 | sctp_findassociation_ep_addr(struct sctp_inpcb **inp_p, struct sockaddr *remote, |
| 1466 | struct sctp_nets **netp, struct sockaddr *local, struct sctp_tcb *locked_tcb) |
| 1467 | { |
| 1468 | struct sctpasochead *head; |
| 1469 | struct sctp_inpcb *inp; |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 1470 | struct sctp_tcb *stcb = NULL; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1471 | struct sctp_nets *net; |
| 1472 | uint16_t rport; |
| 1473 | |
| 1474 | inp = *inp_p; |
tuexen | 5d9b974 | 2012-07-11 21:52:01 +0000 | [diff] [blame] | 1475 | switch (remote->sa_family) { |
| 1476 | #ifdef INET |
| 1477 | case AF_INET: |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1478 | rport = (((struct sockaddr_in *)remote)->sin_port); |
tuexen | 5d9b974 | 2012-07-11 21:52:01 +0000 | [diff] [blame] | 1479 | break; |
| 1480 | #endif |
| 1481 | #ifdef INET6 |
| 1482 | case AF_INET6: |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1483 | rport = (((struct sockaddr_in6 *)remote)->sin6_port); |
tuexen | 5d9b974 | 2012-07-11 21:52:01 +0000 | [diff] [blame] | 1484 | break; |
| 1485 | #endif |
| 1486 | #if defined(__Userspace__) |
| 1487 | case AF_CONN: |
Taylor Brandstetter | 7467051 | 2018-03-27 10:56:58 -0700 | [diff] [blame] | 1488 | rport = (((struct sockaddr_conn *)remote)->sconn_port); |
tuexen | 5d9b974 | 2012-07-11 21:52:01 +0000 | [diff] [blame] | 1489 | break; |
| 1490 | #endif |
| 1491 | default: |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1492 | return (NULL); |
| 1493 | } |
| 1494 | if (locked_tcb) { |
| 1495 | /* |
| 1496 | * UN-lock so we can do proper locking here this occurs when |
| 1497 | * called from load_addresses_from_init. |
| 1498 | */ |
| 1499 | atomic_add_int(&locked_tcb->asoc.refcnt, 1); |
| 1500 | SCTP_TCB_UNLOCK(locked_tcb); |
| 1501 | } |
| 1502 | SCTP_INP_INFO_RLOCK(); |
t00fcxen | f2d781c | 2012-11-26 16:47:12 +0000 | [diff] [blame] | 1503 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || |
| 1504 | (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1505 | /*- |
| 1506 | * Now either this guy is our listener or it's the |
| 1507 | * connector. If it is the one that issued the connect, then |
| 1508 | * it's only chance is to be the first TCB in the list. If |
| 1509 | * it is the acceptor, then do the special_lookup to hash |
| 1510 | * and find the real inp. |
| 1511 | */ |
Michael Tuexen | d900e5f | 2017-06-23 10:52:43 +0200 | [diff] [blame] | 1512 | if ((inp->sctp_socket) && SCTP_IS_LISTENING(inp)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1513 | /* to is peer addr, from is my addr */ |
| 1514 | #ifndef SCTP_MVRF |
| 1515 | stcb = sctp_tcb_special_locate(inp_p, remote, local, |
| 1516 | netp, inp->def_vrf_id); |
| 1517 | if ((stcb != NULL) && (locked_tcb == NULL)) { |
| 1518 | /* we have a locked tcb, lower refcount */ |
| 1519 | SCTP_INP_DECR_REF(inp); |
| 1520 | } |
| 1521 | if ((locked_tcb != NULL) && (locked_tcb != stcb)) { |
| 1522 | SCTP_INP_RLOCK(locked_tcb->sctp_ep); |
| 1523 | SCTP_TCB_LOCK(locked_tcb); |
| 1524 | atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); |
| 1525 | SCTP_INP_RUNLOCK(locked_tcb->sctp_ep); |
| 1526 | } |
| 1527 | #else |
| 1528 | /*- |
| 1529 | * MVRF is tricky, we must look in every VRF |
| 1530 | * the endpoint has. |
| 1531 | */ |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 1532 | int i; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1533 | |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 1534 | for (i = 0; i < inp->num_vrfs; i++) { |
| 1535 | stcb = sctp_tcb_special_locate(inp_p, remote, local, |
| 1536 | netp, inp->m_vrf_ids[i]); |
| 1537 | if ((stcb != NULL) && (locked_tcb == NULL)) { |
| 1538 | /* we have a locked tcb, lower refcount */ |
| 1539 | SCTP_INP_DECR_REF(inp); |
| 1540 | break; |
| 1541 | } |
| 1542 | if ((locked_tcb != NULL) && (locked_tcb != stcb)) { |
| 1543 | SCTP_INP_RLOCK(locked_tcb->sctp_ep); |
| 1544 | SCTP_TCB_LOCK(locked_tcb); |
| 1545 | atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); |
| 1546 | SCTP_INP_RUNLOCK(locked_tcb->sctp_ep); |
| 1547 | break; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1548 | } |
| 1549 | } |
| 1550 | #endif |
| 1551 | SCTP_INP_INFO_RUNLOCK(); |
| 1552 | return (stcb); |
| 1553 | } else { |
| 1554 | SCTP_INP_WLOCK(inp); |
| 1555 | if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
| 1556 | goto null_return; |
| 1557 | } |
| 1558 | stcb = LIST_FIRST(&inp->sctp_asoc_list); |
| 1559 | if (stcb == NULL) { |
| 1560 | goto null_return; |
| 1561 | } |
| 1562 | SCTP_TCB_LOCK(stcb); |
| 1563 | |
| 1564 | if (stcb->rport != rport) { |
| 1565 | /* remote port does not match. */ |
| 1566 | SCTP_TCB_UNLOCK(stcb); |
| 1567 | goto null_return; |
| 1568 | } |
| 1569 | if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 1570 | SCTP_TCB_UNLOCK(stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1571 | goto null_return; |
| 1572 | } |
| 1573 | if (local && !sctp_does_stcb_own_this_addr(stcb, local)) { |
| 1574 | SCTP_TCB_UNLOCK(stcb); |
| 1575 | goto null_return; |
| 1576 | } |
| 1577 | /* now look at the list of remote addresses */ |
| 1578 | TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { |
| 1579 | #ifdef INVARIANTS |
| 1580 | if (net == (TAILQ_NEXT(net, sctp_next))) { |
| 1581 | panic("Corrupt net list"); |
| 1582 | } |
| 1583 | #endif |
| 1584 | if (net->ro._l_addr.sa.sa_family != |
| 1585 | remote->sa_family) { |
| 1586 | /* not the same family */ |
| 1587 | continue; |
| 1588 | } |
| 1589 | switch (remote->sa_family) { |
| 1590 | #ifdef INET |
| 1591 | case AF_INET: |
| 1592 | { |
| 1593 | struct sockaddr_in *sin, *rsin; |
| 1594 | |
| 1595 | sin = (struct sockaddr_in *) |
| 1596 | &net->ro._l_addr; |
| 1597 | rsin = (struct sockaddr_in *)remote; |
| 1598 | if (sin->sin_addr.s_addr == |
| 1599 | rsin->sin_addr.s_addr) { |
| 1600 | /* found it */ |
| 1601 | if (netp != NULL) { |
| 1602 | *netp = net; |
| 1603 | } |
| 1604 | if (locked_tcb == NULL) { |
| 1605 | SCTP_INP_DECR_REF(inp); |
| 1606 | } else if (locked_tcb != stcb) { |
| 1607 | SCTP_TCB_LOCK(locked_tcb); |
| 1608 | } |
| 1609 | if (locked_tcb) { |
| 1610 | atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); |
| 1611 | } |
| 1612 | |
| 1613 | SCTP_INP_WUNLOCK(inp); |
| 1614 | SCTP_INP_INFO_RUNLOCK(); |
| 1615 | return (stcb); |
| 1616 | } |
| 1617 | break; |
| 1618 | } |
| 1619 | #endif |
| 1620 | #ifdef INET6 |
| 1621 | case AF_INET6: |
| 1622 | { |
| 1623 | struct sockaddr_in6 *sin6, *rsin6; |
| 1624 | |
| 1625 | sin6 = (struct sockaddr_in6 *)&net->ro._l_addr; |
| 1626 | rsin6 = (struct sockaddr_in6 *)remote; |
| 1627 | if (SCTP6_ARE_ADDR_EQUAL(sin6, |
| 1628 | rsin6)) { |
| 1629 | /* found it */ |
| 1630 | if (netp != NULL) { |
| 1631 | *netp = net; |
| 1632 | } |
| 1633 | if (locked_tcb == NULL) { |
| 1634 | SCTP_INP_DECR_REF(inp); |
| 1635 | } else if (locked_tcb != stcb) { |
| 1636 | SCTP_TCB_LOCK(locked_tcb); |
| 1637 | } |
| 1638 | if (locked_tcb) { |
| 1639 | atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); |
| 1640 | } |
| 1641 | SCTP_INP_WUNLOCK(inp); |
| 1642 | SCTP_INP_INFO_RUNLOCK(); |
| 1643 | return (stcb); |
| 1644 | } |
| 1645 | break; |
| 1646 | } |
| 1647 | #endif |
tuexen | 5d9b974 | 2012-07-11 21:52:01 +0000 | [diff] [blame] | 1648 | #if defined(__Userspace__) |
| 1649 | case AF_CONN: |
| 1650 | { |
| 1651 | struct sockaddr_conn *sconn, *rsconn; |
| 1652 | |
| 1653 | sconn = (struct sockaddr_conn *)&net->ro._l_addr; |
| 1654 | rsconn = (struct sockaddr_conn *)remote; |
| 1655 | if (sconn->sconn_addr == rsconn->sconn_addr) { |
| 1656 | /* found it */ |
| 1657 | if (netp != NULL) { |
| 1658 | *netp = net; |
| 1659 | } |
| 1660 | if (locked_tcb == NULL) { |
| 1661 | SCTP_INP_DECR_REF(inp); |
| 1662 | } else if (locked_tcb != stcb) { |
| 1663 | SCTP_TCB_LOCK(locked_tcb); |
| 1664 | } |
| 1665 | if (locked_tcb) { |
| 1666 | atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); |
| 1667 | } |
| 1668 | SCTP_INP_WUNLOCK(inp); |
| 1669 | SCTP_INP_INFO_RUNLOCK(); |
| 1670 | return (stcb); |
| 1671 | } |
| 1672 | break; |
| 1673 | } |
| 1674 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1675 | default: |
| 1676 | /* TSNH */ |
| 1677 | break; |
| 1678 | } |
| 1679 | } |
| 1680 | SCTP_TCB_UNLOCK(stcb); |
| 1681 | } |
| 1682 | } else { |
| 1683 | SCTP_INP_WLOCK(inp); |
| 1684 | if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
| 1685 | goto null_return; |
| 1686 | } |
| 1687 | head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(rport, |
t00fcxen | 1369cd4 | 2015-01-11 21:57:30 +0000 | [diff] [blame] | 1688 | inp->sctp_hashmark)]; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1689 | LIST_FOREACH(stcb, head, sctp_tcbhash) { |
| 1690 | if (stcb->rport != rport) { |
| 1691 | /* remote port does not match */ |
| 1692 | continue; |
| 1693 | } |
| 1694 | SCTP_TCB_LOCK(stcb); |
| 1695 | if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { |
| 1696 | SCTP_TCB_UNLOCK(stcb); |
| 1697 | continue; |
| 1698 | } |
| 1699 | if (local && !sctp_does_stcb_own_this_addr(stcb, local)) { |
| 1700 | SCTP_TCB_UNLOCK(stcb); |
| 1701 | continue; |
| 1702 | } |
| 1703 | /* now look at the list of remote addresses */ |
| 1704 | TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { |
| 1705 | #ifdef INVARIANTS |
| 1706 | if (net == (TAILQ_NEXT(net, sctp_next))) { |
| 1707 | panic("Corrupt net list"); |
| 1708 | } |
| 1709 | #endif |
| 1710 | if (net->ro._l_addr.sa.sa_family != |
| 1711 | remote->sa_family) { |
| 1712 | /* not the same family */ |
| 1713 | continue; |
| 1714 | } |
| 1715 | switch (remote->sa_family) { |
| 1716 | #ifdef INET |
| 1717 | case AF_INET: |
| 1718 | { |
| 1719 | struct sockaddr_in *sin, *rsin; |
| 1720 | |
| 1721 | sin = (struct sockaddr_in *) |
| 1722 | &net->ro._l_addr; |
| 1723 | rsin = (struct sockaddr_in *)remote; |
| 1724 | if (sin->sin_addr.s_addr == |
| 1725 | rsin->sin_addr.s_addr) { |
| 1726 | /* found it */ |
| 1727 | if (netp != NULL) { |
| 1728 | *netp = net; |
| 1729 | } |
| 1730 | if (locked_tcb == NULL) { |
| 1731 | SCTP_INP_DECR_REF(inp); |
| 1732 | } else if (locked_tcb != stcb) { |
| 1733 | SCTP_TCB_LOCK(locked_tcb); |
| 1734 | } |
| 1735 | if (locked_tcb) { |
| 1736 | atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); |
| 1737 | } |
| 1738 | SCTP_INP_WUNLOCK(inp); |
| 1739 | SCTP_INP_INFO_RUNLOCK(); |
| 1740 | return (stcb); |
| 1741 | } |
| 1742 | break; |
| 1743 | } |
| 1744 | #endif |
| 1745 | #ifdef INET6 |
| 1746 | case AF_INET6: |
| 1747 | { |
| 1748 | struct sockaddr_in6 *sin6, *rsin6; |
| 1749 | |
| 1750 | sin6 = (struct sockaddr_in6 *) |
| 1751 | &net->ro._l_addr; |
| 1752 | rsin6 = (struct sockaddr_in6 *)remote; |
| 1753 | if (SCTP6_ARE_ADDR_EQUAL(sin6, |
| 1754 | rsin6)) { |
| 1755 | /* found it */ |
| 1756 | if (netp != NULL) { |
| 1757 | *netp = net; |
| 1758 | } |
| 1759 | if (locked_tcb == NULL) { |
| 1760 | SCTP_INP_DECR_REF(inp); |
| 1761 | } else if (locked_tcb != stcb) { |
| 1762 | SCTP_TCB_LOCK(locked_tcb); |
| 1763 | } |
| 1764 | if (locked_tcb) { |
| 1765 | atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); |
| 1766 | } |
| 1767 | SCTP_INP_WUNLOCK(inp); |
| 1768 | SCTP_INP_INFO_RUNLOCK(); |
| 1769 | return (stcb); |
| 1770 | } |
| 1771 | break; |
| 1772 | } |
| 1773 | #endif |
tuexen | 5d9b974 | 2012-07-11 21:52:01 +0000 | [diff] [blame] | 1774 | #if defined(__Userspace__) |
| 1775 | case AF_CONN: |
| 1776 | { |
| 1777 | struct sockaddr_conn *sconn, *rsconn; |
| 1778 | |
| 1779 | sconn = (struct sockaddr_conn *)&net->ro._l_addr; |
| 1780 | rsconn = (struct sockaddr_conn *)remote; |
| 1781 | if (sconn->sconn_addr == rsconn->sconn_addr) { |
| 1782 | /* found it */ |
| 1783 | if (netp != NULL) { |
| 1784 | *netp = net; |
| 1785 | } |
| 1786 | if (locked_tcb == NULL) { |
| 1787 | SCTP_INP_DECR_REF(inp); |
| 1788 | } else if (locked_tcb != stcb) { |
| 1789 | SCTP_TCB_LOCK(locked_tcb); |
| 1790 | } |
| 1791 | if (locked_tcb) { |
| 1792 | atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); |
| 1793 | } |
| 1794 | SCTP_INP_WUNLOCK(inp); |
| 1795 | SCTP_INP_INFO_RUNLOCK(); |
| 1796 | return (stcb); |
| 1797 | } |
| 1798 | break; |
| 1799 | } |
| 1800 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1801 | default: |
| 1802 | /* TSNH */ |
| 1803 | break; |
| 1804 | } |
| 1805 | } |
| 1806 | SCTP_TCB_UNLOCK(stcb); |
| 1807 | } |
| 1808 | } |
| 1809 | null_return: |
| 1810 | /* clean up for returning null */ |
| 1811 | if (locked_tcb) { |
| 1812 | SCTP_TCB_LOCK(locked_tcb); |
| 1813 | atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); |
| 1814 | } |
| 1815 | SCTP_INP_WUNLOCK(inp); |
| 1816 | SCTP_INP_INFO_RUNLOCK(); |
| 1817 | /* not found */ |
| 1818 | return (NULL); |
| 1819 | } |
| 1820 | |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 1821 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1822 | /* |
| 1823 | * Find an association for a specific endpoint using the association id given |
| 1824 | * out in the COMM_UP notification |
| 1825 | */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1826 | struct sctp_tcb * |
| 1827 | sctp_findasoc_ep_asocid_locked(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock) |
| 1828 | { |
| 1829 | /* |
| 1830 | * Use my the assoc_id to find a endpoint |
| 1831 | */ |
| 1832 | struct sctpasochead *head; |
| 1833 | struct sctp_tcb *stcb; |
| 1834 | uint32_t id; |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 1835 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1836 | if (inp == NULL) { |
| 1837 | SCTP_PRINTF("TSNH ep_associd\n"); |
| 1838 | return (NULL); |
| 1839 | } |
| 1840 | if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
| 1841 | SCTP_PRINTF("TSNH ep_associd0\n"); |
| 1842 | return (NULL); |
| 1843 | } |
| 1844 | id = (uint32_t)asoc_id; |
| 1845 | head = &inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(id, inp->hashasocidmark)]; |
| 1846 | if (head == NULL) { |
| 1847 | /* invalid id TSNH */ |
| 1848 | SCTP_PRINTF("TSNH ep_associd1\n"); |
| 1849 | return (NULL); |
| 1850 | } |
| 1851 | LIST_FOREACH(stcb, head, sctp_tcbasocidhash) { |
| 1852 | if (stcb->asoc.assoc_id == id) { |
| 1853 | if (inp != stcb->sctp_ep) { |
| 1854 | /* |
| 1855 | * some other guy has the same id active (id |
| 1856 | * collision ??). |
| 1857 | */ |
| 1858 | SCTP_PRINTF("TSNH ep_associd2\n"); |
| 1859 | continue; |
| 1860 | } |
| 1861 | if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { |
| 1862 | continue; |
| 1863 | } |
| 1864 | if (want_lock) { |
| 1865 | SCTP_TCB_LOCK(stcb); |
| 1866 | } |
| 1867 | return (stcb); |
| 1868 | } |
| 1869 | } |
| 1870 | return (NULL); |
| 1871 | } |
| 1872 | |
| 1873 | |
| 1874 | struct sctp_tcb * |
| 1875 | sctp_findassociation_ep_asocid(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock) |
| 1876 | { |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 1877 | struct sctp_tcb *stcb; |
| 1878 | |
| 1879 | SCTP_INP_RLOCK(inp); |
| 1880 | stcb = sctp_findasoc_ep_asocid_locked(inp, asoc_id, want_lock); |
| 1881 | SCTP_INP_RUNLOCK(inp); |
| 1882 | return (stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1883 | } |
| 1884 | |
| 1885 | |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 1886 | /* |
| 1887 | * Endpoint probe expects that the INP_INFO is locked. |
| 1888 | */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1889 | static struct sctp_inpcb * |
| 1890 | sctp_endpoint_probe(struct sockaddr *nam, struct sctppcbhead *head, |
| 1891 | uint16_t lport, uint32_t vrf_id) |
| 1892 | { |
| 1893 | struct sctp_inpcb *inp; |
| 1894 | struct sctp_laddr *laddr; |
| 1895 | #ifdef INET |
| 1896 | struct sockaddr_in *sin; |
| 1897 | #endif |
| 1898 | #ifdef INET6 |
| 1899 | struct sockaddr_in6 *sin6; |
| 1900 | struct sockaddr_in6 *intf_addr6; |
| 1901 | #endif |
tuexen | 09a676e | 2012-07-08 21:03:18 +0000 | [diff] [blame] | 1902 | #if defined(__Userspace__) |
| 1903 | struct sockaddr_conn *sconn; |
| 1904 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1905 | #ifdef SCTP_MVRF |
| 1906 | int i; |
| 1907 | #endif |
| 1908 | int fnd; |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 1909 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1910 | #ifdef INET |
| 1911 | sin = NULL; |
| 1912 | #endif |
| 1913 | #ifdef INET6 |
| 1914 | sin6 = NULL; |
| 1915 | #endif |
t00fcxen | b4c54ac | 2012-09-15 09:17:50 +0000 | [diff] [blame] | 1916 | #if defined(__Userspace__) |
| 1917 | sconn = NULL; |
| 1918 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1919 | switch (nam->sa_family) { |
| 1920 | #ifdef INET |
| 1921 | case AF_INET: |
| 1922 | sin = (struct sockaddr_in *)nam; |
| 1923 | break; |
| 1924 | #endif |
| 1925 | #ifdef INET6 |
| 1926 | case AF_INET6: |
| 1927 | sin6 = (struct sockaddr_in6 *)nam; |
| 1928 | break; |
| 1929 | #endif |
tuexen | 09a676e | 2012-07-08 21:03:18 +0000 | [diff] [blame] | 1930 | #if defined(__Userspace__) |
| 1931 | case AF_CONN: |
| 1932 | sconn = (struct sockaddr_conn *)nam; |
| 1933 | break; |
| 1934 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1935 | default: |
| 1936 | /* unsupported family */ |
| 1937 | return (NULL); |
| 1938 | } |
| 1939 | |
| 1940 | if (head == NULL) |
| 1941 | return (NULL); |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 1942 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1943 | LIST_FOREACH(inp, head, sctp_hash) { |
| 1944 | SCTP_INP_RLOCK(inp); |
| 1945 | if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
| 1946 | SCTP_INP_RUNLOCK(inp); |
| 1947 | continue; |
| 1948 | } |
| 1949 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) && |
| 1950 | (inp->sctp_lport == lport)) { |
| 1951 | /* got it */ |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 1952 | switch (nam->sa_family) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1953 | #ifdef INET |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 1954 | case AF_INET: |
| 1955 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) && |
Michael Tuexen | 72e5cec | 2019-08-05 13:21:02 +0200 | [diff] [blame] | 1956 | SCTP_IPV6_V6ONLY(inp)) { |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 1957 | /* IPv4 on a IPv6 socket with ONLY IPv6 set */ |
| 1958 | SCTP_INP_RUNLOCK(inp); |
| 1959 | continue; |
| 1960 | } |
| 1961 | #if defined(__FreeBSD__) |
| 1962 | if (prison_check_ip4(inp->ip_inp.inp.inp_cred, |
| 1963 | &sin->sin_addr) != 0) { |
| 1964 | SCTP_INP_RUNLOCK(inp); |
| 1965 | continue; |
| 1966 | } |
| 1967 | #endif |
| 1968 | break; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1969 | #endif |
| 1970 | #ifdef INET6 |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 1971 | case AF_INET6: |
| 1972 | /* A V6 address and the endpoint is NOT bound V6 */ |
| 1973 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) { |
| 1974 | SCTP_INP_RUNLOCK(inp); |
| 1975 | continue; |
| 1976 | } |
| 1977 | #if defined(__FreeBSD__) |
| 1978 | if (prison_check_ip6(inp->ip_inp.inp.inp_cred, |
| 1979 | &sin6->sin6_addr) != 0) { |
| 1980 | SCTP_INP_RUNLOCK(inp); |
| 1981 | continue; |
| 1982 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1983 | #endif |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 1984 | break; |
| 1985 | #endif |
| 1986 | default: |
| 1987 | break; |
| 1988 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 1989 | /* does a VRF id match? */ |
| 1990 | fnd = 0; |
| 1991 | #ifdef SCTP_MVRF |
| 1992 | for (i = 0; i < inp->num_vrfs; i++) { |
| 1993 | if (inp->m_vrf_ids[i] == vrf_id) { |
| 1994 | fnd = 1; |
| 1995 | break; |
| 1996 | } |
| 1997 | } |
| 1998 | #else |
| 1999 | if (inp->def_vrf_id == vrf_id) |
| 2000 | fnd = 1; |
| 2001 | #endif |
| 2002 | |
| 2003 | SCTP_INP_RUNLOCK(inp); |
| 2004 | if (!fnd) |
| 2005 | continue; |
| 2006 | return (inp); |
| 2007 | } |
| 2008 | SCTP_INP_RUNLOCK(inp); |
| 2009 | } |
| 2010 | switch (nam->sa_family) { |
| 2011 | #ifdef INET |
| 2012 | case AF_INET: |
| 2013 | if (sin->sin_addr.s_addr == INADDR_ANY) { |
| 2014 | /* Can't hunt for one that has no address specified */ |
| 2015 | return (NULL); |
| 2016 | } |
| 2017 | break; |
| 2018 | #endif |
| 2019 | #ifdef INET6 |
| 2020 | case AF_INET6: |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 2021 | if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2022 | /* Can't hunt for one that has no address specified */ |
| 2023 | return (NULL); |
| 2024 | } |
| 2025 | break; |
| 2026 | #endif |
tuexen | 09a676e | 2012-07-08 21:03:18 +0000 | [diff] [blame] | 2027 | #if defined(__Userspace__) |
| 2028 | case AF_CONN: |
| 2029 | if (sconn->sconn_addr == NULL) { |
| 2030 | return (NULL); |
| 2031 | } |
| 2032 | break; |
| 2033 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2034 | default: |
| 2035 | break; |
| 2036 | } |
| 2037 | /* |
| 2038 | * ok, not bound to all so see if we can find a EP bound to this |
| 2039 | * address. |
| 2040 | */ |
| 2041 | LIST_FOREACH(inp, head, sctp_hash) { |
| 2042 | SCTP_INP_RLOCK(inp); |
| 2043 | if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
| 2044 | SCTP_INP_RUNLOCK(inp); |
| 2045 | continue; |
| 2046 | } |
| 2047 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)) { |
| 2048 | SCTP_INP_RUNLOCK(inp); |
| 2049 | continue; |
| 2050 | } |
| 2051 | /* |
| 2052 | * Ok this could be a likely candidate, look at all of its |
| 2053 | * addresses |
| 2054 | */ |
| 2055 | if (inp->sctp_lport != lport) { |
| 2056 | SCTP_INP_RUNLOCK(inp); |
| 2057 | continue; |
| 2058 | } |
| 2059 | /* does a VRF id match? */ |
| 2060 | fnd = 0; |
| 2061 | #ifdef SCTP_MVRF |
| 2062 | for (i = 0; i < inp->num_vrfs; i++) { |
| 2063 | if (inp->m_vrf_ids[i] == vrf_id) { |
| 2064 | fnd = 1; |
| 2065 | break; |
| 2066 | } |
| 2067 | } |
| 2068 | #else |
| 2069 | if (inp->def_vrf_id == vrf_id) |
| 2070 | fnd = 1; |
| 2071 | |
| 2072 | #endif |
| 2073 | if (!fnd) { |
| 2074 | SCTP_INP_RUNLOCK(inp); |
| 2075 | continue; |
| 2076 | } |
| 2077 | LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { |
| 2078 | if (laddr->ifa == NULL) { |
| 2079 | SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n", |
Saúl Ibarra Corretgé | 8b04b41 | 2015-09-26 09:53:06 +0200 | [diff] [blame] | 2080 | __func__); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2081 | continue; |
| 2082 | } |
| 2083 | SCTPDBG(SCTP_DEBUG_PCB1, "Ok laddr->ifa:%p is possible, ", |
t00fcxen | 8fca7ff | 2012-09-05 18:58:42 +0000 | [diff] [blame] | 2084 | (void *)laddr->ifa); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2085 | if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) { |
| 2086 | SCTPDBG(SCTP_DEBUG_PCB1, "Huh IFA being deleted\n"); |
| 2087 | continue; |
| 2088 | } |
| 2089 | if (laddr->ifa->address.sa.sa_family == nam->sa_family) { |
| 2090 | /* possible, see if it matches */ |
| 2091 | switch (nam->sa_family) { |
| 2092 | #ifdef INET |
| 2093 | case AF_INET: |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 2094 | #if defined(__APPLE__) |
| 2095 | if (sin == NULL) { |
| 2096 | /* TSNH */ |
| 2097 | break; |
| 2098 | } |
| 2099 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2100 | if (sin->sin_addr.s_addr == |
| 2101 | laddr->ifa->address.sin.sin_addr.s_addr) { |
| 2102 | SCTP_INP_RUNLOCK(inp); |
| 2103 | return (inp); |
| 2104 | } |
| 2105 | break; |
| 2106 | #endif |
| 2107 | #ifdef INET6 |
| 2108 | case AF_INET6: |
| 2109 | intf_addr6 = &laddr->ifa->address.sin6; |
| 2110 | if (SCTP6_ARE_ADDR_EQUAL(sin6, |
| 2111 | intf_addr6)) { |
| 2112 | SCTP_INP_RUNLOCK(inp); |
| 2113 | return (inp); |
| 2114 | } |
| 2115 | break; |
| 2116 | #endif |
tuexen | 09a676e | 2012-07-08 21:03:18 +0000 | [diff] [blame] | 2117 | #if defined(__Userspace__) |
| 2118 | case AF_CONN: |
| 2119 | if (sconn->sconn_addr == laddr->ifa->address.sconn.sconn_addr) { |
| 2120 | SCTP_INP_RUNLOCK(inp); |
| 2121 | return (inp); |
| 2122 | } |
| 2123 | break; |
| 2124 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2125 | } |
| 2126 | } |
| 2127 | } |
| 2128 | SCTP_INP_RUNLOCK(inp); |
| 2129 | } |
| 2130 | return (NULL); |
| 2131 | } |
| 2132 | |
| 2133 | |
| 2134 | static struct sctp_inpcb * |
| 2135 | sctp_isport_inuse(struct sctp_inpcb *inp, uint16_t lport, uint32_t vrf_id) |
| 2136 | { |
| 2137 | struct sctppcbhead *head; |
| 2138 | struct sctp_inpcb *t_inp; |
| 2139 | #ifdef SCTP_MVRF |
| 2140 | int i; |
| 2141 | #endif |
| 2142 | int fnd; |
| 2143 | |
| 2144 | head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport, |
| 2145 | SCTP_BASE_INFO(hashmark))]; |
| 2146 | LIST_FOREACH(t_inp, head, sctp_hash) { |
| 2147 | if (t_inp->sctp_lport != lport) { |
| 2148 | continue; |
| 2149 | } |
| 2150 | /* is it in the VRF in question */ |
| 2151 | fnd = 0; |
| 2152 | #ifdef SCTP_MVRF |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 2153 | for (i = 0; i < inp->num_vrfs; i++) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2154 | if (t_inp->m_vrf_ids[i] == vrf_id) { |
| 2155 | fnd = 1; |
| 2156 | break; |
| 2157 | } |
| 2158 | } |
| 2159 | #else |
| 2160 | if (t_inp->def_vrf_id == vrf_id) |
| 2161 | fnd = 1; |
| 2162 | #endif |
| 2163 | if (!fnd) |
| 2164 | continue; |
| 2165 | |
| 2166 | /* This one is in use. */ |
| 2167 | /* check the v6/v4 binding issue */ |
| 2168 | if ((t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) && |
Michael Tuexen | 72e5cec | 2019-08-05 13:21:02 +0200 | [diff] [blame] | 2169 | SCTP_IPV6_V6ONLY(t_inp)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2170 | if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { |
| 2171 | /* collision in V6 space */ |
| 2172 | return (t_inp); |
| 2173 | } else { |
| 2174 | /* inp is BOUND_V4 no conflict */ |
| 2175 | continue; |
| 2176 | } |
| 2177 | } else if (t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { |
| 2178 | /* t_inp is bound v4 and v6, conflict always */ |
| 2179 | return (t_inp); |
| 2180 | } else { |
| 2181 | /* t_inp is bound only V4 */ |
| 2182 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) && |
Michael Tuexen | 72e5cec | 2019-08-05 13:21:02 +0200 | [diff] [blame] | 2183 | SCTP_IPV6_V6ONLY(inp)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2184 | /* no conflict */ |
| 2185 | continue; |
| 2186 | } |
| 2187 | /* else fall through to conflict */ |
| 2188 | } |
| 2189 | return (t_inp); |
| 2190 | } |
| 2191 | return (NULL); |
| 2192 | } |
| 2193 | |
| 2194 | |
| 2195 | int |
| 2196 | sctp_swap_inpcb_for_listen(struct sctp_inpcb *inp) |
| 2197 | { |
| 2198 | /* For 1-2-1 with port reuse */ |
| 2199 | struct sctppcbhead *head; |
t00fcxen | c00f4bc | 2015-03-24 21:17:16 +0000 | [diff] [blame] | 2200 | struct sctp_inpcb *tinp, *ninp; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2201 | |
| 2202 | if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) { |
| 2203 | /* only works with port reuse on */ |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 2204 | return (-1); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2205 | } |
| 2206 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) { |
| 2207 | return (0); |
| 2208 | } |
| 2209 | SCTP_INP_RUNLOCK(inp); |
t00fcxen | c00f4bc | 2015-03-24 21:17:16 +0000 | [diff] [blame] | 2210 | SCTP_INP_INFO_WLOCK(); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2211 | head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(inp->sctp_lport, |
| 2212 | SCTP_BASE_INFO(hashmark))]; |
| 2213 | /* Kick out all non-listeners to the TCP hash */ |
t00fcxen | c00f4bc | 2015-03-24 21:17:16 +0000 | [diff] [blame] | 2214 | LIST_FOREACH_SAFE(tinp, head, sctp_hash, ninp) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2215 | if (tinp->sctp_lport != inp->sctp_lport) { |
| 2216 | continue; |
| 2217 | } |
| 2218 | if (tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
| 2219 | continue; |
| 2220 | } |
| 2221 | if (tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { |
| 2222 | continue; |
| 2223 | } |
Michael Tuexen | d900e5f | 2017-06-23 10:52:43 +0200 | [diff] [blame] | 2224 | if (SCTP_IS_LISTENING(tinp)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2225 | continue; |
| 2226 | } |
| 2227 | SCTP_INP_WLOCK(tinp); |
| 2228 | LIST_REMOVE(tinp, sctp_hash); |
| 2229 | head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR(tinp->sctp_lport, SCTP_BASE_INFO(hashtcpmark))]; |
| 2230 | tinp->sctp_flags |= SCTP_PCB_FLAGS_IN_TCPPOOL; |
| 2231 | LIST_INSERT_HEAD(head, tinp, sctp_hash); |
| 2232 | SCTP_INP_WUNLOCK(tinp); |
| 2233 | } |
| 2234 | SCTP_INP_WLOCK(inp); |
| 2235 | /* Pull from where he was */ |
| 2236 | LIST_REMOVE(inp, sctp_hash); |
| 2237 | inp->sctp_flags &= ~SCTP_PCB_FLAGS_IN_TCPPOOL; |
| 2238 | head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(inp->sctp_lport, SCTP_BASE_INFO(hashmark))]; |
| 2239 | LIST_INSERT_HEAD(head, inp, sctp_hash); |
| 2240 | SCTP_INP_WUNLOCK(inp); |
| 2241 | SCTP_INP_RLOCK(inp); |
t00fcxen | c00f4bc | 2015-03-24 21:17:16 +0000 | [diff] [blame] | 2242 | SCTP_INP_INFO_WUNLOCK(); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2243 | return (0); |
| 2244 | } |
| 2245 | |
| 2246 | |
| 2247 | struct sctp_inpcb * |
| 2248 | sctp_pcb_findep(struct sockaddr *nam, int find_tcp_pool, int have_lock, |
| 2249 | uint32_t vrf_id) |
| 2250 | { |
| 2251 | /* |
| 2252 | * First we check the hash table to see if someone has this port |
| 2253 | * bound with just the port. |
| 2254 | */ |
| 2255 | struct sctp_inpcb *inp; |
| 2256 | struct sctppcbhead *head; |
| 2257 | int lport; |
| 2258 | unsigned int i; |
| 2259 | #ifdef INET |
| 2260 | struct sockaddr_in *sin; |
| 2261 | #endif |
| 2262 | #ifdef INET6 |
| 2263 | struct sockaddr_in6 *sin6; |
| 2264 | #endif |
tuexen | 09a676e | 2012-07-08 21:03:18 +0000 | [diff] [blame] | 2265 | #if defined(__Userspace__) |
| 2266 | struct sockaddr_conn *sconn; |
| 2267 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2268 | |
| 2269 | switch (nam->sa_family) { |
| 2270 | #ifdef INET |
| 2271 | case AF_INET: |
| 2272 | sin = (struct sockaddr_in *)nam; |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 2273 | lport = sin->sin_port; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2274 | break; |
| 2275 | #endif |
| 2276 | #ifdef INET6 |
| 2277 | case AF_INET6: |
| 2278 | sin6 = (struct sockaddr_in6 *)nam; |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 2279 | lport = sin6->sin6_port; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2280 | break; |
| 2281 | #endif |
tuexen | 09a676e | 2012-07-08 21:03:18 +0000 | [diff] [blame] | 2282 | #if defined(__Userspace__) |
| 2283 | case AF_CONN: |
| 2284 | sconn = (struct sockaddr_conn *)nam; |
| 2285 | lport = sconn->sconn_port; |
| 2286 | break; |
| 2287 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2288 | default: |
| 2289 | return (NULL); |
| 2290 | } |
| 2291 | /* |
| 2292 | * I could cheat here and just cast to one of the types but we will |
| 2293 | * do it right. It also provides the check against an Unsupported |
| 2294 | * type too. |
| 2295 | */ |
| 2296 | /* Find the head of the ALLADDR chain */ |
| 2297 | if (have_lock == 0) { |
| 2298 | SCTP_INP_INFO_RLOCK(); |
| 2299 | } |
| 2300 | head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport, |
| 2301 | SCTP_BASE_INFO(hashmark))]; |
| 2302 | inp = sctp_endpoint_probe(nam, head, lport, vrf_id); |
| 2303 | |
| 2304 | /* |
| 2305 | * If the TCP model exists it could be that the main listening |
| 2306 | * endpoint is gone but there still exists a connected socket for this |
| 2307 | * guy. If so we can return the first one that we find. This may NOT |
| 2308 | * be the correct one so the caller should be wary on the returned INP. |
| 2309 | * Currently the only caller that sets find_tcp_pool is in bindx where |
| 2310 | * we are verifying that a user CAN bind the address. He either |
| 2311 | * has bound it already, or someone else has, or its open to bind, |
| 2312 | * so this is good enough. |
| 2313 | */ |
| 2314 | if (inp == NULL && find_tcp_pool) { |
| 2315 | for (i = 0; i < SCTP_BASE_INFO(hashtcpmark) + 1; i++) { |
| 2316 | head = &SCTP_BASE_INFO(sctp_tcpephash)[i]; |
| 2317 | inp = sctp_endpoint_probe(nam, head, lport, vrf_id); |
| 2318 | if (inp) { |
| 2319 | break; |
| 2320 | } |
| 2321 | } |
| 2322 | } |
| 2323 | if (inp) { |
| 2324 | SCTP_INP_INCR_REF(inp); |
| 2325 | } |
| 2326 | if (have_lock == 0) { |
| 2327 | SCTP_INP_INFO_RUNLOCK(); |
| 2328 | } |
| 2329 | return (inp); |
| 2330 | } |
| 2331 | |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 2332 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2333 | /* |
| 2334 | * Find an association for an endpoint with the pointer to whom you want to |
| 2335 | * send to and the endpoint pointer. The address can be IPv4 or IPv6. We may |
| 2336 | * need to change the *to to some other struct like a mbuf... |
| 2337 | */ |
| 2338 | struct sctp_tcb * |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 2339 | sctp_findassociation_addr_sa(struct sockaddr *from, struct sockaddr *to, |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2340 | struct sctp_inpcb **inp_p, struct sctp_nets **netp, int find_tcp_pool, |
| 2341 | uint32_t vrf_id) |
| 2342 | { |
| 2343 | struct sctp_inpcb *inp = NULL; |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2344 | struct sctp_tcb *stcb; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2345 | |
| 2346 | SCTP_INP_INFO_RLOCK(); |
| 2347 | if (find_tcp_pool) { |
| 2348 | if (inp_p != NULL) { |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2349 | stcb = sctp_tcb_special_locate(inp_p, from, to, netp, |
| 2350 | vrf_id); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2351 | } else { |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2352 | stcb = sctp_tcb_special_locate(&inp, from, to, netp, |
| 2353 | vrf_id); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2354 | } |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2355 | if (stcb != NULL) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2356 | SCTP_INP_INFO_RUNLOCK(); |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2357 | return (stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2358 | } |
| 2359 | } |
| 2360 | inp = sctp_pcb_findep(to, 0, 1, vrf_id); |
| 2361 | if (inp_p != NULL) { |
| 2362 | *inp_p = inp; |
| 2363 | } |
| 2364 | SCTP_INP_INFO_RUNLOCK(); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2365 | if (inp == NULL) { |
| 2366 | return (NULL); |
| 2367 | } |
| 2368 | /* |
| 2369 | * ok, we have an endpoint, now lets find the assoc for it (if any) |
| 2370 | * we now place the source address or from in the to of the find |
| 2371 | * endpoint call. Since in reality this chain is used from the |
| 2372 | * inbound packet side. |
| 2373 | */ |
| 2374 | if (inp_p != NULL) { |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2375 | stcb = sctp_findassociation_ep_addr(inp_p, from, netp, to, |
| 2376 | NULL); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2377 | } else { |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2378 | stcb = sctp_findassociation_ep_addr(&inp, from, netp, to, |
| 2379 | NULL); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2380 | } |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2381 | return (stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2382 | } |
| 2383 | |
| 2384 | |
| 2385 | /* |
| 2386 | * This routine will grub through the mbuf that is a INIT or INIT-ACK and |
| 2387 | * find all addresses that the sender has specified in any address list. Each |
| 2388 | * address will be used to lookup the TCB and see if one exits. |
| 2389 | */ |
| 2390 | static struct sctp_tcb * |
tuexen | 9784e9a | 2011-12-18 13:04:23 +0000 | [diff] [blame] | 2391 | sctp_findassociation_special_addr(struct mbuf *m, int offset, |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2392 | struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp, |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 2393 | struct sockaddr *dst) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2394 | { |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 2395 | struct sctp_paramhdr *phdr, param_buf; |
t00fcxen | aff14b6 | 2013-11-02 18:02:02 +0000 | [diff] [blame] | 2396 | #if defined(INET) || defined(INET6) |
t00fcxen | 6ebb603 | 2013-11-02 18:03:31 +0000 | [diff] [blame] | 2397 | struct sctp_tcb *stcb; |
t00fcxen | aff14b6 | 2013-11-02 18:02:02 +0000 | [diff] [blame] | 2398 | uint16_t ptype; |
| 2399 | #endif |
| 2400 | uint16_t plen; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2401 | #ifdef INET |
| 2402 | struct sockaddr_in sin4; |
| 2403 | #endif |
| 2404 | #ifdef INET6 |
| 2405 | struct sockaddr_in6 sin6; |
| 2406 | #endif |
| 2407 | |
| 2408 | #ifdef INET |
| 2409 | memset(&sin4, 0, sizeof(sin4)); |
t00fcxen | 8fbaee3 | 2012-09-04 16:41:39 +0000 | [diff] [blame] | 2410 | #ifdef HAVE_SIN_LEN |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2411 | sin4.sin_len = sizeof(sin4); |
| 2412 | #endif |
| 2413 | sin4.sin_family = AF_INET; |
| 2414 | sin4.sin_port = sh->src_port; |
| 2415 | #endif |
| 2416 | #ifdef INET6 |
| 2417 | memset(&sin6, 0, sizeof(sin6)); |
t00fcxen | 8fbaee3 | 2012-09-04 16:41:39 +0000 | [diff] [blame] | 2418 | #ifdef HAVE_SIN6_LEN |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2419 | sin6.sin6_len = sizeof(sin6); |
| 2420 | #endif |
| 2421 | sin6.sin6_family = AF_INET6; |
| 2422 | sin6.sin6_port = sh->src_port; |
| 2423 | #endif |
| 2424 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2425 | offset += sizeof(struct sctp_init_chunk); |
| 2426 | |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 2427 | phdr = sctp_get_next_param(m, offset, ¶m_buf, sizeof(param_buf)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2428 | while (phdr != NULL) { |
| 2429 | /* now we must see if we want the parameter */ |
t00fcxen | aff14b6 | 2013-11-02 18:02:02 +0000 | [diff] [blame] | 2430 | #if defined(INET) || defined(INET6) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2431 | ptype = ntohs(phdr->param_type); |
t00fcxen | aff14b6 | 2013-11-02 18:02:02 +0000 | [diff] [blame] | 2432 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2433 | plen = ntohs(phdr->param_length); |
| 2434 | if (plen == 0) { |
| 2435 | break; |
| 2436 | } |
| 2437 | #ifdef INET |
| 2438 | if (ptype == SCTP_IPV4_ADDRESS && |
| 2439 | plen == sizeof(struct sctp_ipv4addr_param)) { |
| 2440 | /* Get the rest of the address */ |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 2441 | struct sctp_ipv4addr_param ip4_param, *p4; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2442 | |
| 2443 | phdr = sctp_get_next_param(m, offset, |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 2444 | (struct sctp_paramhdr *)&ip4_param, sizeof(ip4_param)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2445 | if (phdr == NULL) { |
| 2446 | return (NULL); |
| 2447 | } |
| 2448 | p4 = (struct sctp_ipv4addr_param *)phdr; |
| 2449 | memcpy(&sin4.sin_addr, &p4->addr, sizeof(p4->addr)); |
| 2450 | /* look it up */ |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2451 | stcb = sctp_findassociation_ep_addr(inp_p, |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 2452 | (struct sockaddr *)&sin4, netp, dst, NULL); |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2453 | if (stcb != NULL) { |
| 2454 | return (stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2455 | } |
| 2456 | } |
| 2457 | #endif |
| 2458 | #ifdef INET6 |
| 2459 | if (ptype == SCTP_IPV6_ADDRESS && |
| 2460 | plen == sizeof(struct sctp_ipv6addr_param)) { |
| 2461 | /* Get the rest of the address */ |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 2462 | struct sctp_ipv6addr_param ip6_param, *p6; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2463 | |
| 2464 | phdr = sctp_get_next_param(m, offset, |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 2465 | (struct sctp_paramhdr *)&ip6_param, sizeof(ip6_param)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2466 | if (phdr == NULL) { |
| 2467 | return (NULL); |
| 2468 | } |
| 2469 | p6 = (struct sctp_ipv6addr_param *)phdr; |
| 2470 | memcpy(&sin6.sin6_addr, &p6->addr, sizeof(p6->addr)); |
| 2471 | /* look it up */ |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2472 | stcb = sctp_findassociation_ep_addr(inp_p, |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 2473 | (struct sockaddr *)&sin6, netp, dst, NULL); |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2474 | if (stcb != NULL) { |
| 2475 | return (stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2476 | } |
| 2477 | } |
| 2478 | #endif |
| 2479 | offset += SCTP_SIZE32(plen); |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 2480 | phdr = sctp_get_next_param(m, offset, ¶m_buf, |
| 2481 | sizeof(param_buf)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2482 | } |
| 2483 | return (NULL); |
| 2484 | } |
| 2485 | |
| 2486 | static struct sctp_tcb * |
| 2487 | sctp_findassoc_by_vtag(struct sockaddr *from, struct sockaddr *to, uint32_t vtag, |
| 2488 | struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint16_t rport, |
| 2489 | uint16_t lport, int skip_src_check, uint32_t vrf_id, uint32_t remote_tag) |
| 2490 | { |
| 2491 | /* |
| 2492 | * Use my vtag to hash. If we find it we then verify the source addr |
| 2493 | * is in the assoc. If all goes well we save a bit on rec of a |
| 2494 | * packet. |
| 2495 | */ |
| 2496 | struct sctpasochead *head; |
| 2497 | struct sctp_nets *net; |
| 2498 | struct sctp_tcb *stcb; |
tuexen | 9784e9a | 2011-12-18 13:04:23 +0000 | [diff] [blame] | 2499 | #ifdef SCTP_MVRF |
| 2500 | unsigned int i; |
| 2501 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2502 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2503 | SCTP_INP_INFO_RLOCK(); |
| 2504 | head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(vtag, |
t00fcxen | 0d66488 | 2015-01-11 21:47:36 +0000 | [diff] [blame] | 2505 | SCTP_BASE_INFO(hashasocmark))]; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2506 | LIST_FOREACH(stcb, head, sctp_asocs) { |
| 2507 | SCTP_INP_RLOCK(stcb->sctp_ep); |
| 2508 | if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
| 2509 | SCTP_INP_RUNLOCK(stcb->sctp_ep); |
| 2510 | continue; |
| 2511 | } |
tuexen | 9784e9a | 2011-12-18 13:04:23 +0000 | [diff] [blame] | 2512 | #ifdef SCTP_MVRF |
| 2513 | for (i = 0; i < stcb->sctp_ep->num_vrfs; i++) { |
| 2514 | if (stcb->sctp_ep->m_vrf_ids[i] == vrf_id) { |
| 2515 | break; |
| 2516 | } |
| 2517 | } |
| 2518 | if (i == stcb->sctp_ep->num_vrfs) { |
| 2519 | SCTP_INP_RUNLOCK(inp); |
| 2520 | continue; |
| 2521 | } |
| 2522 | #else |
| 2523 | if (stcb->sctp_ep->def_vrf_id != vrf_id) { |
| 2524 | SCTP_INP_RUNLOCK(stcb->sctp_ep); |
| 2525 | continue; |
| 2526 | } |
| 2527 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2528 | SCTP_TCB_LOCK(stcb); |
| 2529 | SCTP_INP_RUNLOCK(stcb->sctp_ep); |
| 2530 | if (stcb->asoc.my_vtag == vtag) { |
| 2531 | /* candidate */ |
| 2532 | if (stcb->rport != rport) { |
| 2533 | SCTP_TCB_UNLOCK(stcb); |
| 2534 | continue; |
| 2535 | } |
| 2536 | if (stcb->sctp_ep->sctp_lport != lport) { |
| 2537 | SCTP_TCB_UNLOCK(stcb); |
| 2538 | continue; |
| 2539 | } |
| 2540 | if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { |
| 2541 | SCTP_TCB_UNLOCK(stcb); |
| 2542 | continue; |
| 2543 | } |
| 2544 | /* RRS:Need toaddr check here */ |
| 2545 | if (sctp_does_stcb_own_this_addr(stcb, to) == 0) { |
| 2546 | /* Endpoint does not own this address */ |
| 2547 | SCTP_TCB_UNLOCK(stcb); |
| 2548 | continue; |
| 2549 | } |
| 2550 | if (remote_tag) { |
| 2551 | /* If we have both vtags that's all we match on */ |
| 2552 | if (stcb->asoc.peer_vtag == remote_tag) { |
| 2553 | /* If both tags match we consider it conclusive |
| 2554 | * and check NO source/destination addresses |
| 2555 | */ |
| 2556 | goto conclusive; |
| 2557 | } |
| 2558 | } |
| 2559 | if (skip_src_check) { |
| 2560 | conclusive: |
| 2561 | if (from) { |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 2562 | *netp = sctp_findnet(stcb, from); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2563 | } else { |
| 2564 | *netp = NULL; /* unknown */ |
| 2565 | } |
| 2566 | if (inp_p) |
| 2567 | *inp_p = stcb->sctp_ep; |
| 2568 | SCTP_INP_INFO_RUNLOCK(); |
| 2569 | return (stcb); |
| 2570 | } |
| 2571 | net = sctp_findnet(stcb, from); |
| 2572 | if (net) { |
| 2573 | /* yep its him. */ |
| 2574 | *netp = net; |
| 2575 | SCTP_STAT_INCR(sctps_vtagexpress); |
| 2576 | *inp_p = stcb->sctp_ep; |
| 2577 | SCTP_INP_INFO_RUNLOCK(); |
| 2578 | return (stcb); |
| 2579 | } else { |
| 2580 | /* |
| 2581 | * not him, this should only happen in rare |
| 2582 | * cases so I peg it. |
| 2583 | */ |
| 2584 | SCTP_STAT_INCR(sctps_vtagbogus); |
| 2585 | } |
| 2586 | } |
| 2587 | SCTP_TCB_UNLOCK(stcb); |
| 2588 | } |
| 2589 | SCTP_INP_INFO_RUNLOCK(); |
| 2590 | return (NULL); |
| 2591 | } |
| 2592 | |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 2593 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2594 | /* |
| 2595 | * Find an association with the pointer to the inbound IP packet. This can be |
| 2596 | * a IPv4 or IPv6 packet. |
| 2597 | */ |
| 2598 | struct sctp_tcb * |
tuexen | 9784e9a | 2011-12-18 13:04:23 +0000 | [diff] [blame] | 2599 | sctp_findassociation_addr(struct mbuf *m, int offset, |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 2600 | struct sockaddr *src, struct sockaddr *dst, |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2601 | struct sctphdr *sh, struct sctp_chunkhdr *ch, |
| 2602 | struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id) |
| 2603 | { |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2604 | struct sctp_tcb *stcb; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2605 | struct sctp_inpcb *inp; |
| 2606 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2607 | if (sh->v_tag) { |
| 2608 | /* we only go down this path if vtag is non-zero */ |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2609 | stcb = sctp_findassoc_by_vtag(src, dst, ntohl(sh->v_tag), |
t00fcxen | afd2119 | 2013-05-10 18:15:02 +0000 | [diff] [blame] | 2610 | inp_p, netp, sh->src_port, sh->dest_port, 0, vrf_id, 0); |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2611 | if (stcb) { |
| 2612 | return (stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2613 | } |
| 2614 | } |
| 2615 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2616 | if (inp_p) { |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2617 | stcb = sctp_findassociation_addr_sa(src, dst, inp_p, netp, |
Michael Tuexen | 7f20278 | 2016-01-28 17:09:30 +0100 | [diff] [blame] | 2618 | 1, vrf_id); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2619 | inp = *inp_p; |
| 2620 | } else { |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2621 | stcb = sctp_findassociation_addr_sa(src, dst, &inp, netp, |
Michael Tuexen | 7f20278 | 2016-01-28 17:09:30 +0100 | [diff] [blame] | 2622 | 1, vrf_id); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2623 | } |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2624 | SCTPDBG(SCTP_DEBUG_PCB1, "stcb:%p inp:%p\n", (void *)stcb, (void *)inp); |
| 2625 | if (stcb == NULL && inp) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2626 | /* Found a EP but not this address */ |
| 2627 | if ((ch->chunk_type == SCTP_INITIATION) || |
| 2628 | (ch->chunk_type == SCTP_INITIATION_ACK)) { |
| 2629 | /*- |
| 2630 | * special hook, we do NOT return linp or an |
| 2631 | * association that is linked to an existing |
| 2632 | * association that is under the TCP pool (i.e. no |
| 2633 | * listener exists). The endpoint finding routine |
| 2634 | * will always find a listener before examining the |
| 2635 | * TCP pool. |
| 2636 | */ |
| 2637 | if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) { |
| 2638 | if (inp_p) { |
| 2639 | *inp_p = NULL; |
| 2640 | } |
| 2641 | return (NULL); |
| 2642 | } |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2643 | stcb = sctp_findassociation_special_addr(m, |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 2644 | offset, sh, &inp, netp, dst); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2645 | if (inp_p != NULL) { |
| 2646 | *inp_p = inp; |
| 2647 | } |
| 2648 | } |
| 2649 | } |
t00fcxen | 60d0dea | 2013-02-10 20:04:03 +0000 | [diff] [blame] | 2650 | SCTPDBG(SCTP_DEBUG_PCB1, "stcb is %p\n", (void *)stcb); |
| 2651 | return (stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2652 | } |
| 2653 | |
| 2654 | /* |
| 2655 | * lookup an association by an ASCONF lookup address. |
| 2656 | * if the lookup address is 0.0.0.0 or ::0, use the vtag to do the lookup |
| 2657 | */ |
| 2658 | struct sctp_tcb * |
tuexen | 9784e9a | 2011-12-18 13:04:23 +0000 | [diff] [blame] | 2659 | sctp_findassociation_ep_asconf(struct mbuf *m, int offset, |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 2660 | struct sockaddr *dst, struct sctphdr *sh, |
| 2661 | struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2662 | { |
| 2663 | struct sctp_tcb *stcb; |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 2664 | union sctp_sockstore remote_store; |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 2665 | struct sctp_paramhdr param_buf, *phdr; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2666 | int ptype; |
| 2667 | int zero_address = 0; |
| 2668 | #ifdef INET |
| 2669 | struct sockaddr_in *sin; |
| 2670 | #endif |
| 2671 | #ifdef INET6 |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2672 | struct sockaddr_in6 *sin6; |
| 2673 | #endif |
| 2674 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2675 | memset(&remote_store, 0, sizeof(remote_store)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2676 | phdr = sctp_get_next_param(m, offset + sizeof(struct sctp_asconf_chunk), |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 2677 | ¶m_buf, sizeof(struct sctp_paramhdr)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2678 | if (phdr == NULL) { |
| 2679 | 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] | 2680 | __func__); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2681 | return NULL; |
| 2682 | } |
| 2683 | ptype = (int)((uint32_t) ntohs(phdr->param_type)); |
| 2684 | /* get the correlation address */ |
| 2685 | switch (ptype) { |
| 2686 | #ifdef INET6 |
| 2687 | case SCTP_IPV6_ADDRESS: |
| 2688 | { |
| 2689 | /* ipv6 address param */ |
| 2690 | struct sctp_ipv6addr_param *p6, p6_buf; |
| 2691 | |
| 2692 | if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv6addr_param)) { |
| 2693 | return NULL; |
| 2694 | } |
| 2695 | p6 = (struct sctp_ipv6addr_param *)sctp_get_next_param(m, |
| 2696 | offset + sizeof(struct sctp_asconf_chunk), |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 2697 | &p6_buf.ph, sizeof(p6_buf)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2698 | if (p6 == NULL) { |
| 2699 | 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] | 2700 | __func__); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2701 | return (NULL); |
| 2702 | } |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 2703 | sin6 = &remote_store.sin6; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2704 | sin6->sin6_family = AF_INET6; |
t00fcxen | 8fbaee3 | 2012-09-04 16:41:39 +0000 | [diff] [blame] | 2705 | #ifdef HAVE_SIN6_LEN |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2706 | sin6->sin6_len = sizeof(*sin6); |
| 2707 | #endif |
| 2708 | sin6->sin6_port = sh->src_port; |
| 2709 | memcpy(&sin6->sin6_addr, &p6->addr, sizeof(struct in6_addr)); |
| 2710 | if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) |
| 2711 | zero_address = 1; |
| 2712 | break; |
| 2713 | } |
| 2714 | #endif |
| 2715 | #ifdef INET |
| 2716 | case SCTP_IPV4_ADDRESS: |
| 2717 | { |
| 2718 | /* ipv4 address param */ |
| 2719 | struct sctp_ipv4addr_param *p4, p4_buf; |
| 2720 | |
| 2721 | if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv4addr_param)) { |
| 2722 | return NULL; |
| 2723 | } |
| 2724 | p4 = (struct sctp_ipv4addr_param *)sctp_get_next_param(m, |
| 2725 | offset + sizeof(struct sctp_asconf_chunk), |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 2726 | &p4_buf.ph, sizeof(p4_buf)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2727 | if (p4 == NULL) { |
| 2728 | 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] | 2729 | __func__); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2730 | return (NULL); |
| 2731 | } |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 2732 | sin = &remote_store.sin; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2733 | sin->sin_family = AF_INET; |
t00fcxen | 8fbaee3 | 2012-09-04 16:41:39 +0000 | [diff] [blame] | 2734 | #ifdef HAVE_SIN_LEN |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2735 | sin->sin_len = sizeof(*sin); |
| 2736 | #endif |
| 2737 | sin->sin_port = sh->src_port; |
| 2738 | memcpy(&sin->sin_addr, &p4->addr, sizeof(struct in_addr)); |
| 2739 | if (sin->sin_addr.s_addr == INADDR_ANY) |
| 2740 | zero_address = 1; |
| 2741 | break; |
| 2742 | } |
| 2743 | #endif |
| 2744 | default: |
| 2745 | /* invalid address param type */ |
| 2746 | return NULL; |
| 2747 | } |
| 2748 | |
| 2749 | if (zero_address) { |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 2750 | stcb = sctp_findassoc_by_vtag(NULL, dst, ntohl(sh->v_tag), inp_p, |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2751 | netp, sh->src_port, sh->dest_port, 1, vrf_id, 0); |
tuexen | b53ec51 | 2012-08-05 10:55:12 +0000 | [diff] [blame] | 2752 | if (stcb != NULL) { |
| 2753 | SCTP_INP_DECR_REF(*inp_p); |
| 2754 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2755 | } else { |
| 2756 | stcb = sctp_findassociation_ep_addr(inp_p, |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 2757 | &remote_store.sa, netp, |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 2758 | dst, NULL); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2759 | } |
| 2760 | return (stcb); |
| 2761 | } |
| 2762 | |
| 2763 | |
| 2764 | /* |
| 2765 | * allocate a sctp_inpcb and setup a temporary binding to a port/all |
| 2766 | * addresses. This way if we don't get a bind we by default pick a ephemeral |
| 2767 | * port with all addresses bound. |
| 2768 | */ |
| 2769 | int |
| 2770 | sctp_inpcb_alloc(struct socket *so, uint32_t vrf_id) |
| 2771 | { |
| 2772 | /* |
| 2773 | * we get called when a new endpoint starts up. We need to allocate |
| 2774 | * the sctp_inpcb structure from the zone and init it. Mark it as |
| 2775 | * unbound and find a port that we can use as an ephemeral with |
| 2776 | * INADDR_ANY. If the user binds later no problem we can then add in |
| 2777 | * the specific addresses. And setup the default parameters for the |
| 2778 | * EP. |
| 2779 | */ |
| 2780 | int i, error; |
| 2781 | struct sctp_inpcb *inp; |
| 2782 | struct sctp_pcb *m; |
| 2783 | struct timeval time; |
| 2784 | sctp_sharedkey_t *null_key; |
| 2785 | |
| 2786 | error = 0; |
| 2787 | |
| 2788 | SCTP_INP_INFO_WLOCK(); |
| 2789 | inp = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_ep), struct sctp_inpcb); |
| 2790 | if (inp == NULL) { |
| 2791 | SCTP_PRINTF("Out of SCTP-INPCB structures - no resources\n"); |
| 2792 | SCTP_INP_INFO_WUNLOCK(); |
| 2793 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS); |
| 2794 | return (ENOBUFS); |
| 2795 | } |
| 2796 | /* zap it */ |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 2797 | memset(inp, 0, sizeof(*inp)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2798 | |
| 2799 | /* bump generations */ |
| 2800 | #if defined(__APPLE__) |
| 2801 | inp->ip_inp.inp.inp_state = INPCB_STATE_INUSE; |
| 2802 | #endif |
| 2803 | /* setup socket pointers */ |
| 2804 | inp->sctp_socket = so; |
| 2805 | inp->ip_inp.inp.inp_socket = so; |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 2806 | #if defined(__FreeBSD__) |
| 2807 | inp->ip_inp.inp.inp_cred = crhold(so->so_cred); |
| 2808 | #endif |
tuexen | c0bb612 | 2012-01-15 14:21:21 +0000 | [diff] [blame] | 2809 | #ifdef INET6 |
tuexen | 76eb209 | 2012-04-01 16:35:28 +0000 | [diff] [blame] | 2810 | #if !defined(__Userspace__) && !defined(__Windows__) |
t00fcxen | afd2119 | 2013-05-10 18:15:02 +0000 | [diff] [blame] | 2811 | if (INP_SOCKAF(so) == AF_INET6) { |
| 2812 | if (MODULE_GLOBAL(ip6_auto_flowlabel)) { |
| 2813 | inp->ip_inp.inp.inp_flags |= IN6P_AUTOFLOWLABEL; |
| 2814 | } |
| 2815 | if (MODULE_GLOBAL(ip6_v6only)) { |
| 2816 | inp->ip_inp.inp.inp_flags |= IN6P_IPV6_V6ONLY; |
| 2817 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2818 | } |
| 2819 | #endif |
tuexen | c0bb612 | 2012-01-15 14:21:21 +0000 | [diff] [blame] | 2820 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2821 | inp->sctp_associd_counter = 1; |
| 2822 | inp->partial_delivery_point = SCTP_SB_LIMIT_RCV(so) >> SCTP_PARTIAL_DELIVERY_SHIFT; |
| 2823 | inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT; |
t00fcxen | 3eda0f2 | 2015-03-10 20:10:45 +0000 | [diff] [blame] | 2824 | inp->max_cwnd = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2825 | inp->sctp_cmt_on_off = SCTP_BASE_SYSCTL(sctp_cmt_on_off); |
t00fcxen | 478b772 | 2014-08-02 18:24:12 +0000 | [diff] [blame] | 2826 | inp->ecn_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_ecn_enable); |
t00fcxen | 0e78cef | 2014-08-02 22:05:33 +0000 | [diff] [blame] | 2827 | inp->prsctp_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_pr_enable); |
t00fcxen | b3969a1 | 2014-08-12 13:19:21 +0000 | [diff] [blame] | 2828 | inp->auth_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_auth_enable); |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 2829 | inp->asconf_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_asconf_enable); |
t00fcxen | fa7e1a0 | 2014-08-04 20:41:36 +0000 | [diff] [blame] | 2830 | inp->reconfig_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_reconfig_enable); |
t00fcxen | 3656cf9 | 2014-08-03 15:25:02 +0000 | [diff] [blame] | 2831 | inp->nrsack_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_nrsack_enable); |
t00fcxen | 1cc0ba8 | 2014-08-03 20:32:08 +0000 | [diff] [blame] | 2832 | inp->pktdrop_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_pktdrop_enable); |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 2833 | inp->idata_supported = 0; |
| 2834 | |
t00fcxen | a51e446 | 2015-06-17 15:53:23 +0000 | [diff] [blame] | 2835 | #if defined(__FreeBSD__) |
| 2836 | inp->fibnum = so->so_fibnum; |
| 2837 | #else |
| 2838 | inp->fibnum = 0; |
| 2839 | #endif |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 2840 | #if defined(__Userspace__) |
tuexen | eda9dbb | 2012-05-27 11:10:08 +0000 | [diff] [blame] | 2841 | inp->ulp_info = NULL; |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 2842 | inp->recv_callback = NULL; |
| 2843 | inp->send_callback = NULL; |
| 2844 | inp->send_sb_threshold = 0; |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 2845 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2846 | /* init the small hash table we use to track asocid <-> tcb */ |
| 2847 | inp->sctp_asocidhash = SCTP_HASH_INIT(SCTP_STACK_VTAG_HASH_SIZE, &inp->hashasocidmark); |
| 2848 | if (inp->sctp_asocidhash == NULL) { |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 2849 | #if defined(__FreeBSD__) |
| 2850 | crfree(inp->ip_inp.inp.inp_cred); |
| 2851 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2852 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp); |
| 2853 | SCTP_INP_INFO_WUNLOCK(); |
| 2854 | return (ENOBUFS); |
| 2855 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2856 | SCTP_INCR_EP_COUNT(); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2857 | inp->ip_inp.inp.inp_ip_ttl = MODULE_GLOBAL(ip_defttl); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2858 | SCTP_INP_INFO_WUNLOCK(); |
| 2859 | |
| 2860 | so->so_pcb = (caddr_t)inp; |
| 2861 | |
tuexen | 5fc2284 | 2012-02-11 13:32:19 +0000 | [diff] [blame] | 2862 | #if defined(__FreeBSD__) && __FreeBSD_version < 803000 |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2863 | if ((SCTP_SO_TYPE(so) == SOCK_DGRAM) || |
| 2864 | (SCTP_SO_TYPE(so) == SOCK_SEQPACKET)) { |
| 2865 | #else |
| 2866 | if (SCTP_SO_TYPE(so) == SOCK_SEQPACKET) { |
| 2867 | #endif |
| 2868 | /* UDP style socket */ |
| 2869 | inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE | |
| 2870 | SCTP_PCB_FLAGS_UNBOUND); |
| 2871 | /* Be sure it is NON-BLOCKING IO for UDP */ |
| 2872 | /* SCTP_SET_SO_NBIO(so); */ |
| 2873 | } else if (SCTP_SO_TYPE(so) == SOCK_STREAM) { |
| 2874 | /* TCP style socket */ |
| 2875 | inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE | |
| 2876 | SCTP_PCB_FLAGS_UNBOUND); |
| 2877 | /* Be sure we have blocking IO by default */ |
Michael Tuexen | fe22357 | 2017-09-22 00:19:22 +0200 | [diff] [blame] | 2878 | SOCK_LOCK(so); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2879 | SCTP_CLEAR_SO_NBIO(so); |
Michael Tuexen | fe22357 | 2017-09-22 00:19:22 +0200 | [diff] [blame] | 2880 | SOCK_UNLOCK(so); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2881 | #if defined(__Panda__) |
| 2882 | } else if (SCTP_SO_TYPE(so) == SOCK_FASTSEQPACKET) { |
| 2883 | inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE | |
| 2884 | SCTP_PCB_FLAGS_UNBOUND); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2885 | } else if (SCTP_SO_TYPE(so) == SOCK_FASTSTREAM) { |
| 2886 | inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE | |
| 2887 | SCTP_PCB_FLAGS_UNBOUND); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2888 | #endif |
| 2889 | } else { |
| 2890 | /* |
| 2891 | * unsupported socket type (RAW, etc)- in case we missed it |
| 2892 | * in protosw |
| 2893 | */ |
| 2894 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EOPNOTSUPP); |
| 2895 | so->so_pcb = NULL; |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 2896 | #if defined(__FreeBSD__) |
| 2897 | crfree(inp->ip_inp.inp.inp_cred); |
| 2898 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2899 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp); |
| 2900 | return (EOPNOTSUPP); |
| 2901 | } |
| 2902 | if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_1) { |
| 2903 | sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE); |
| 2904 | sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS); |
| 2905 | } else if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_2) { |
| 2906 | sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE); |
| 2907 | sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS); |
| 2908 | } else if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_0) { |
| 2909 | sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE); |
| 2910 | sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS); |
| 2911 | } |
| 2912 | inp->sctp_tcbhash = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_pcbtblsize), |
| 2913 | &inp->sctp_hashmark); |
| 2914 | if (inp->sctp_tcbhash == NULL) { |
| 2915 | SCTP_PRINTF("Out of SCTP-INPCB->hashinit - no resources\n"); |
| 2916 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS); |
| 2917 | so->so_pcb = NULL; |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 2918 | #if defined(__FreeBSD__) |
| 2919 | crfree(inp->ip_inp.inp.inp_cred); |
| 2920 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2921 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp); |
| 2922 | return (ENOBUFS); |
| 2923 | } |
| 2924 | #ifdef SCTP_MVRF |
| 2925 | inp->vrf_size = SCTP_DEFAULT_VRF_SIZE; |
| 2926 | SCTP_MALLOC(inp->m_vrf_ids, uint32_t *, |
| 2927 | (sizeof(uint32_t) * inp->vrf_size), SCTP_M_MVRF); |
| 2928 | if (inp->m_vrf_ids == NULL) { |
| 2929 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS); |
| 2930 | so->so_pcb = NULL; |
| 2931 | SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark); |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 2932 | #if defined(__FreeBSD__) |
| 2933 | crfree(inp->ip_inp.inp.inp_cred); |
| 2934 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2935 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp); |
| 2936 | return (ENOBUFS); |
| 2937 | } |
| 2938 | inp->m_vrf_ids[0] = vrf_id; |
| 2939 | inp->num_vrfs = 1; |
| 2940 | #endif |
| 2941 | inp->def_vrf_id = vrf_id; |
| 2942 | |
| 2943 | #if defined(__APPLE__) |
tuexen | 9a218b1 | 2012-08-04 21:17:58 +0000 | [diff] [blame] | 2944 | #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) |
t00fcxen | bc766ab | 2014-01-03 19:29:31 +0000 | [diff] [blame] | 2945 | 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] | 2946 | if (inp->ip_inp.inp.inpcb_mtx == NULL) { |
t00fcxen | 8fca7ff | 2012-09-05 18:58:42 +0000 | [diff] [blame] | 2947 | SCTP_PRINTF("in_pcballoc: can't alloc mutex! so=%p\n", (void *)so); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2948 | #ifdef SCTP_MVRF |
| 2949 | SCTP_FREE(inp->m_vrf_ids, SCTP_M_MVRF); |
| 2950 | #endif |
| 2951 | SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark); |
| 2952 | so->so_pcb = NULL; |
| 2953 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp); |
t00fcxen | bc766ab | 2014-01-03 19:29:31 +0000 | [diff] [blame] | 2954 | SCTP_UNLOCK_EXC(SCTP_BASE_INFO(sctbinfo).ipi_lock); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2955 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM); |
| 2956 | return (ENOMEM); |
| 2957 | } |
t00fcxen | bc766ab | 2014-01-03 19:29:31 +0000 | [diff] [blame] | 2958 | #elif defined(APPLE_LION) || defined(APPLE_MOUNTAINLION) |
| 2959 | 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] | 2960 | #else |
t00fcxen | bc766ab | 2014-01-03 19:29:31 +0000 | [diff] [blame] | 2961 | 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] | 2962 | #endif |
| 2963 | #endif |
| 2964 | SCTP_INP_INFO_WLOCK(); |
| 2965 | SCTP_INP_LOCK_INIT(inp); |
| 2966 | #if defined(__FreeBSD__) |
| 2967 | INP_LOCK_INIT(&inp->ip_inp.inp, "inp", "sctpinp"); |
| 2968 | #endif |
| 2969 | SCTP_INP_READ_INIT(inp); |
| 2970 | SCTP_ASOC_CREATE_LOCK_INIT(inp); |
| 2971 | /* lock the new ep */ |
| 2972 | SCTP_INP_WLOCK(inp); |
| 2973 | |
| 2974 | /* add it to the info area */ |
| 2975 | LIST_INSERT_HEAD(&SCTP_BASE_INFO(listhead), inp, sctp_list); |
| 2976 | #if defined(__APPLE__) |
t00fcxen | bc766ab | 2014-01-03 19:29:31 +0000 | [diff] [blame] | 2977 | inp->ip_inp.inp.inp_pcbinfo = &SCTP_BASE_INFO(sctbinfo); |
| 2978 | #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) || defined(APPLE_LION) || defined(APPLE_MOUNTAINLION) |
| 2979 | LIST_INSERT_HEAD(SCTP_BASE_INFO(sctbinfo).listhead, &inp->ip_inp.inp, inp_list); |
| 2980 | #else |
| 2981 | LIST_INSERT_HEAD(SCTP_BASE_INFO(sctbinfo).ipi_listhead, &inp->ip_inp.inp, inp_list); |
| 2982 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 2983 | #endif |
| 2984 | SCTP_INP_INFO_WUNLOCK(); |
| 2985 | |
| 2986 | TAILQ_INIT(&inp->read_queue); |
| 2987 | LIST_INIT(&inp->sctp_addr_list); |
| 2988 | |
| 2989 | LIST_INIT(&inp->sctp_asoc_list); |
| 2990 | |
| 2991 | #ifdef SCTP_TRACK_FREED_ASOCS |
| 2992 | /* TEMP CODE */ |
| 2993 | LIST_INIT(&inp->sctp_asoc_free_list); |
| 2994 | #endif |
| 2995 | /* Init the timer structure for signature change */ |
| 2996 | SCTP_OS_TIMER_INIT(&inp->sctp_ep.signature_change.timer); |
| 2997 | inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NEWCOOKIE; |
| 2998 | |
| 2999 | /* now init the actual endpoint default data */ |
| 3000 | m = &inp->sctp_ep; |
| 3001 | |
| 3002 | /* setup the base timeout information */ |
Michael Tuexen | 25935e8 | 2020-03-28 22:55:28 +0100 | [diff] [blame] | 3003 | m->sctp_timeoutticks[SCTP_TIMER_SEND] = sctp_secs_to_ticks(SCTP_SEND_SEC); /* needed ? */ |
| 3004 | m->sctp_timeoutticks[SCTP_TIMER_INIT] = sctp_secs_to_ticks(SCTP_INIT_SEC); /* needed ? */ |
| 3005 | m->sctp_timeoutticks[SCTP_TIMER_RECV] = sctp_msecs_to_ticks(SCTP_BASE_SYSCTL(sctp_delayed_sack_time_default)); |
| 3006 | m->sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = sctp_msecs_to_ticks(SCTP_BASE_SYSCTL(sctp_heartbeat_interval_default)); |
| 3007 | m->sctp_timeoutticks[SCTP_TIMER_PMTU] = sctp_secs_to_ticks(SCTP_BASE_SYSCTL(sctp_pmtu_raise_time_default)); |
| 3008 | m->sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] = sctp_secs_to_ticks(SCTP_BASE_SYSCTL(sctp_shutdown_guard_time_default)); |
| 3009 | 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] | 3010 | /* all max/min max are in ms */ |
| 3011 | m->sctp_maxrto = SCTP_BASE_SYSCTL(sctp_rto_max_default); |
| 3012 | m->sctp_minrto = SCTP_BASE_SYSCTL(sctp_rto_min_default); |
| 3013 | m->initial_rto = SCTP_BASE_SYSCTL(sctp_rto_initial_default); |
| 3014 | m->initial_init_rto_max = SCTP_BASE_SYSCTL(sctp_init_rto_max_default); |
| 3015 | m->sctp_sack_freq = SCTP_BASE_SYSCTL(sctp_sack_freq_default); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3016 | m->max_init_times = SCTP_BASE_SYSCTL(sctp_init_rtx_max_default); |
| 3017 | m->max_send_times = SCTP_BASE_SYSCTL(sctp_assoc_rtx_max_default); |
| 3018 | m->def_net_failure = SCTP_BASE_SYSCTL(sctp_path_rtx_max_default); |
| 3019 | m->def_net_pf_threshold = SCTP_BASE_SYSCTL(sctp_path_pf_threshold); |
| 3020 | m->sctp_sws_sender = SCTP_SWS_SENDER_DEF; |
| 3021 | m->sctp_sws_receiver = SCTP_SWS_RECEIVER_DEF; |
| 3022 | m->max_burst = SCTP_BASE_SYSCTL(sctp_max_burst_default); |
| 3023 | m->fr_max_burst = SCTP_BASE_SYSCTL(sctp_fr_max_burst_default); |
| 3024 | |
| 3025 | m->sctp_default_cc_module = SCTP_BASE_SYSCTL(sctp_default_cc_module); |
| 3026 | m->sctp_default_ss_module = SCTP_BASE_SYSCTL(sctp_default_ss_module); |
t00fcxen | c45180a | 2013-07-05 10:17:17 +0000 | [diff] [blame] | 3027 | m->max_open_streams_intome = SCTP_BASE_SYSCTL(sctp_nr_incoming_streams_default); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3028 | /* number of streams to pre-open on a association */ |
| 3029 | m->pre_open_stream_count = SCTP_BASE_SYSCTL(sctp_nr_outgoing_streams_default); |
| 3030 | |
Michael Tuexen | de0630d | 2017-11-03 22:04:01 +0100 | [diff] [blame] | 3031 | m->default_mtu = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3032 | /* Add adaptation cookie */ |
t00fcxen | 3637720 | 2013-02-11 21:07:33 +0000 | [diff] [blame] | 3033 | m->adaptation_layer_indicator = 0; |
| 3034 | m->adaptation_layer_indicator_provided = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3035 | |
| 3036 | /* seed random number generator */ |
| 3037 | m->random_counter = 1; |
| 3038 | m->store_at = SCTP_SIGNATURE_SIZE; |
| 3039 | SCTP_READ_RANDOM(m->random_numbers, sizeof(m->random_numbers)); |
| 3040 | sctp_fill_random_store(m); |
| 3041 | |
| 3042 | /* Minimum cookie size */ |
| 3043 | m->size_of_a_cookie = (sizeof(struct sctp_init_msg) * 2) + |
| 3044 | sizeof(struct sctp_state_cookie); |
| 3045 | m->size_of_a_cookie += SCTP_SIGNATURE_SIZE; |
| 3046 | |
| 3047 | /* Setup the initial secret */ |
| 3048 | (void)SCTP_GETTIME_TIMEVAL(&time); |
| 3049 | m->time_of_secret_change = time.tv_sec; |
| 3050 | |
| 3051 | for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) { |
| 3052 | m->secret_key[0][i] = sctp_select_initial_TSN(m); |
| 3053 | } |
| 3054 | sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL); |
| 3055 | |
| 3056 | /* How long is a cookie good for ? */ |
Michael Tuexen | 25935e8 | 2020-03-28 22:55:28 +0100 | [diff] [blame] | 3057 | 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] | 3058 | /* |
| 3059 | * Initialize authentication parameters |
| 3060 | */ |
| 3061 | m->local_hmacs = sctp_default_supported_hmaclist(); |
| 3062 | m->local_auth_chunks = sctp_alloc_chunklist(); |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 3063 | if (inp->asconf_supported) { |
| 3064 | sctp_auth_add_chunk(SCTP_ASCONF, m->local_auth_chunks); |
| 3065 | sctp_auth_add_chunk(SCTP_ASCONF_ACK, m->local_auth_chunks); |
| 3066 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3067 | m->default_dscp = 0; |
| 3068 | #ifdef INET6 |
| 3069 | m->default_flowlabel = 0; |
| 3070 | #endif |
tuexen | 9d7b207 | 2011-11-20 16:35:17 +0000 | [diff] [blame] | 3071 | m->port = 0; /* encapsulation disabled by default */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3072 | LIST_INIT(&m->shared_keys); |
| 3073 | /* add default NULL key as key id 0 */ |
| 3074 | null_key = sctp_alloc_sharedkey(); |
| 3075 | sctp_insert_sharedkey(&m->shared_keys, null_key); |
| 3076 | SCTP_INP_WUNLOCK(inp); |
| 3077 | #ifdef SCTP_LOG_CLOSING |
| 3078 | sctp_log_closing(inp, NULL, 12); |
| 3079 | #endif |
| 3080 | return (error); |
| 3081 | } |
| 3082 | |
| 3083 | |
| 3084 | void |
| 3085 | sctp_move_pcb_and_assoc(struct sctp_inpcb *old_inp, struct sctp_inpcb *new_inp, |
| 3086 | struct sctp_tcb *stcb) |
| 3087 | { |
| 3088 | struct sctp_nets *net; |
| 3089 | uint16_t lport, rport; |
| 3090 | struct sctppcbhead *head; |
| 3091 | struct sctp_laddr *laddr, *oladdr; |
| 3092 | |
| 3093 | atomic_add_int(&stcb->asoc.refcnt, 1); |
| 3094 | SCTP_TCB_UNLOCK(stcb); |
| 3095 | SCTP_INP_INFO_WLOCK(); |
| 3096 | SCTP_INP_WLOCK(old_inp); |
| 3097 | SCTP_INP_WLOCK(new_inp); |
| 3098 | SCTP_TCB_LOCK(stcb); |
| 3099 | atomic_subtract_int(&stcb->asoc.refcnt, 1); |
| 3100 | |
| 3101 | new_inp->sctp_ep.time_of_secret_change = |
| 3102 | old_inp->sctp_ep.time_of_secret_change; |
| 3103 | memcpy(new_inp->sctp_ep.secret_key, old_inp->sctp_ep.secret_key, |
| 3104 | sizeof(old_inp->sctp_ep.secret_key)); |
| 3105 | new_inp->sctp_ep.current_secret_number = |
| 3106 | old_inp->sctp_ep.current_secret_number; |
| 3107 | new_inp->sctp_ep.last_secret_number = |
| 3108 | old_inp->sctp_ep.last_secret_number; |
| 3109 | new_inp->sctp_ep.size_of_a_cookie = old_inp->sctp_ep.size_of_a_cookie; |
| 3110 | |
| 3111 | /* make it so new data pours into the new socket */ |
| 3112 | stcb->sctp_socket = new_inp->sctp_socket; |
| 3113 | stcb->sctp_ep = new_inp; |
| 3114 | |
| 3115 | /* Copy the port across */ |
| 3116 | lport = new_inp->sctp_lport = old_inp->sctp_lport; |
| 3117 | rport = stcb->rport; |
| 3118 | /* Pull the tcb from the old association */ |
| 3119 | LIST_REMOVE(stcb, sctp_tcbhash); |
| 3120 | LIST_REMOVE(stcb, sctp_tcblist); |
| 3121 | if (stcb->asoc.in_asocid_hash) { |
| 3122 | LIST_REMOVE(stcb, sctp_tcbasocidhash); |
| 3123 | } |
| 3124 | /* Now insert the new_inp into the TCP connected hash */ |
| 3125 | head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR((lport | rport), SCTP_BASE_INFO(hashtcpmark))]; |
| 3126 | |
| 3127 | LIST_INSERT_HEAD(head, new_inp, sctp_hash); |
| 3128 | /* Its safe to access */ |
| 3129 | new_inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND; |
| 3130 | |
| 3131 | /* Now move the tcb into the endpoint list */ |
| 3132 | LIST_INSERT_HEAD(&new_inp->sctp_asoc_list, stcb, sctp_tcblist); |
| 3133 | /* |
| 3134 | * Question, do we even need to worry about the ep-hash since we |
| 3135 | * only have one connection? Probably not :> so lets get rid of it |
| 3136 | * and not suck up any kernel memory in that. |
| 3137 | */ |
| 3138 | if (stcb->asoc.in_asocid_hash) { |
| 3139 | struct sctpasochead *lhd; |
| 3140 | lhd = &new_inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(stcb->asoc.assoc_id, |
| 3141 | new_inp->hashasocidmark)]; |
| 3142 | LIST_INSERT_HEAD(lhd, stcb, sctp_tcbasocidhash); |
| 3143 | } |
| 3144 | /* Ok. Let's restart timer. */ |
| 3145 | TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { |
| 3146 | sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, new_inp, |
| 3147 | stcb, net); |
| 3148 | } |
| 3149 | |
| 3150 | SCTP_INP_INFO_WUNLOCK(); |
| 3151 | if (new_inp->sctp_tcbhash != NULL) { |
| 3152 | SCTP_HASH_FREE(new_inp->sctp_tcbhash, new_inp->sctp_hashmark); |
| 3153 | new_inp->sctp_tcbhash = NULL; |
| 3154 | } |
| 3155 | if ((new_inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) { |
| 3156 | /* Subset bound, so copy in the laddr list from the old_inp */ |
| 3157 | LIST_FOREACH(oladdr, &old_inp->sctp_addr_list, sctp_nxt_addr) { |
| 3158 | laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr); |
| 3159 | if (laddr == NULL) { |
| 3160 | /* |
| 3161 | * Gak, what can we do? This assoc is really |
| 3162 | * HOSED. We probably should send an abort |
| 3163 | * here. |
| 3164 | */ |
| 3165 | SCTPDBG(SCTP_DEBUG_PCB1, "Association hosed in TCP model, out of laddr memory\n"); |
| 3166 | continue; |
| 3167 | } |
| 3168 | SCTP_INCR_LADDR_COUNT(); |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 3169 | memset(laddr, 0, sizeof(*laddr)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3170 | (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time); |
| 3171 | laddr->ifa = oladdr->ifa; |
| 3172 | atomic_add_int(&laddr->ifa->refcount, 1); |
| 3173 | LIST_INSERT_HEAD(&new_inp->sctp_addr_list, laddr, |
| 3174 | sctp_nxt_addr); |
| 3175 | new_inp->laddr_count++; |
| 3176 | if (oladdr == stcb->asoc.last_used_address) { |
| 3177 | stcb->asoc.last_used_address = laddr; |
| 3178 | } |
| 3179 | } |
| 3180 | } |
| 3181 | /* Now any running timers need to be adjusted |
| 3182 | * since we really don't care if they are running |
| 3183 | * or not just blast in the new_inp into all of |
| 3184 | * them. |
| 3185 | */ |
| 3186 | |
| 3187 | stcb->asoc.dack_timer.ep = (void *)new_inp; |
| 3188 | stcb->asoc.asconf_timer.ep = (void *)new_inp; |
| 3189 | stcb->asoc.strreset_timer.ep = (void *)new_inp; |
| 3190 | stcb->asoc.shut_guard_timer.ep = (void *)new_inp; |
| 3191 | stcb->asoc.autoclose_timer.ep = (void *)new_inp; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3192 | stcb->asoc.delete_prim_timer.ep = (void *)new_inp; |
| 3193 | /* now what about the nets? */ |
| 3194 | TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { |
| 3195 | net->pmtu_timer.ep = (void *)new_inp; |
| 3196 | net->hb_timer.ep = (void *)new_inp; |
| 3197 | net->rxt_timer.ep = (void *)new_inp; |
| 3198 | } |
| 3199 | SCTP_INP_WUNLOCK(new_inp); |
| 3200 | SCTP_INP_WUNLOCK(old_inp); |
| 3201 | } |
| 3202 | |
Michael Tuexen | 71a83fb | 2016-02-16 21:52:58 +0100 | [diff] [blame] | 3203 | /* |
| 3204 | * insert an laddr entry with the given ifa for the desired list |
| 3205 | */ |
| 3206 | static int |
| 3207 | sctp_insert_laddr(struct sctpladdr *list, struct sctp_ifa *ifa, uint32_t act) |
| 3208 | { |
| 3209 | struct sctp_laddr *laddr; |
| 3210 | |
| 3211 | laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr); |
| 3212 | if (laddr == NULL) { |
| 3213 | /* out of memory? */ |
| 3214 | SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 3215 | return (EINVAL); |
| 3216 | } |
| 3217 | SCTP_INCR_LADDR_COUNT(); |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 3218 | memset(laddr, 0, sizeof(*laddr)); |
Michael Tuexen | 71a83fb | 2016-02-16 21:52:58 +0100 | [diff] [blame] | 3219 | (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time); |
| 3220 | laddr->ifa = ifa; |
| 3221 | laddr->action = act; |
| 3222 | atomic_add_int(&ifa->refcount, 1); |
| 3223 | /* insert it */ |
| 3224 | LIST_INSERT_HEAD(list, laddr, sctp_nxt_addr); |
| 3225 | |
| 3226 | return (0); |
| 3227 | } |
| 3228 | |
| 3229 | /* |
| 3230 | * Remove an laddr entry from the local address list (on an assoc) |
| 3231 | */ |
| 3232 | static void |
| 3233 | sctp_remove_laddr(struct sctp_laddr *laddr) |
| 3234 | { |
| 3235 | |
| 3236 | /* remove from the list */ |
| 3237 | LIST_REMOVE(laddr, sctp_nxt_addr); |
| 3238 | sctp_free_ifa(laddr->ifa); |
| 3239 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), laddr); |
| 3240 | SCTP_DECR_LADDR_COUNT(); |
| 3241 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3242 | |
| 3243 | #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Userspace__)) |
| 3244 | /* |
| 3245 | * Don't know why, but without this there is an unknown reference when |
| 3246 | * compiling NetBSD... hmm |
| 3247 | */ |
| 3248 | extern void in6_sin6_2_sin(struct sockaddr_in *, struct sockaddr_in6 *sin6); |
| 3249 | #endif |
| 3250 | |
| 3251 | |
| 3252 | /* sctp_ifap is used to bypass normal local address validation checks */ |
| 3253 | int |
| 3254 | #if defined(__FreeBSD__) && __FreeBSD_version >= 500000 |
| 3255 | sctp_inpcb_bind(struct socket *so, struct sockaddr *addr, |
t00fcxen | c24796c | 2014-05-06 15:39:13 +0000 | [diff] [blame] | 3256 | struct sctp_ifa *sctp_ifap, struct thread *p) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3257 | #elif defined(__Windows__) |
| 3258 | sctp_inpcb_bind(struct socket *so, struct sockaddr *addr, |
t00fcxen | c24796c | 2014-05-06 15:39:13 +0000 | [diff] [blame] | 3259 | struct sctp_ifa *sctp_ifap, PKTHREAD p) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3260 | #else |
| 3261 | sctp_inpcb_bind(struct socket *so, struct sockaddr *addr, |
t00fcxen | c24796c | 2014-05-06 15:39:13 +0000 | [diff] [blame] | 3262 | struct sctp_ifa *sctp_ifap, struct proc *p) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3263 | #endif |
| 3264 | { |
| 3265 | /* bind a ep to a socket address */ |
| 3266 | struct sctppcbhead *head; |
| 3267 | struct sctp_inpcb *inp, *inp_tmp; |
Michael Tuexen | 2e04f87 | 2019-08-05 13:56:43 +0200 | [diff] [blame] | 3268 | #if defined(__FreeBSD__) || defined(__APPLE__) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3269 | struct inpcb *ip_inp; |
t00fcxen | aff14b6 | 2013-11-02 18:02:02 +0000 | [diff] [blame] | 3270 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3271 | int port_reuse_active = 0; |
| 3272 | int bindall; |
| 3273 | #ifdef SCTP_MVRF |
| 3274 | int i; |
| 3275 | #endif |
| 3276 | uint16_t lport; |
| 3277 | int error; |
| 3278 | uint32_t vrf_id; |
| 3279 | |
| 3280 | lport = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3281 | bindall = 1; |
| 3282 | inp = (struct sctp_inpcb *)so->so_pcb; |
Michael Tuexen | 2e04f87 | 2019-08-05 13:56:43 +0200 | [diff] [blame] | 3283 | #if defined(__FreeBSD__) || defined(__APPLE__) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3284 | ip_inp = (struct inpcb *)so->so_pcb; |
t00fcxen | aff14b6 | 2013-11-02 18:02:02 +0000 | [diff] [blame] | 3285 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3286 | #ifdef SCTP_DEBUG |
| 3287 | if (addr) { |
t00fcxen | 94dfd1e | 2012-12-27 08:14:10 +0000 | [diff] [blame] | 3288 | SCTPDBG(SCTP_DEBUG_PCB1, "Bind called port: %d\n", |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3289 | ntohs(((struct sockaddr_in *)addr)->sin_port)); |
t00fcxen | 94dfd1e | 2012-12-27 08:14:10 +0000 | [diff] [blame] | 3290 | SCTPDBG(SCTP_DEBUG_PCB1, "Addr: "); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3291 | SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr); |
| 3292 | } |
| 3293 | #endif |
| 3294 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) { |
| 3295 | /* already did a bind, subsequent binds NOT allowed ! */ |
| 3296 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 3297 | return (EINVAL); |
| 3298 | } |
| 3299 | #if defined(__FreeBSD__) && __FreeBSD_version >= 500000 |
| 3300 | #ifdef INVARIANTS |
| 3301 | if (p == NULL) |
| 3302 | panic("null proc/thread"); |
| 3303 | #endif |
| 3304 | #endif |
| 3305 | if (addr != NULL) { |
| 3306 | switch (addr->sa_family) { |
| 3307 | #ifdef INET |
| 3308 | case AF_INET: |
| 3309 | { |
| 3310 | struct sockaddr_in *sin; |
| 3311 | |
| 3312 | /* IPV6_V6ONLY socket? */ |
Michael Tuexen | 72e5cec | 2019-08-05 13:21:02 +0200 | [diff] [blame] | 3313 | if (SCTP_IPV6_V6ONLY(inp)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3314 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 3315 | return (EINVAL); |
| 3316 | } |
t00fcxen | a8a26f6 | 2012-09-12 19:24:54 +0000 | [diff] [blame] | 3317 | #ifdef HAVE_SA_LEN |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3318 | if (addr->sa_len != sizeof(*sin)) { |
| 3319 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 3320 | return (EINVAL); |
| 3321 | } |
| 3322 | #endif |
| 3323 | |
| 3324 | sin = (struct sockaddr_in *)addr; |
| 3325 | lport = sin->sin_port; |
| 3326 | #if defined(__FreeBSD__) && __FreeBSD_version >= 800000 |
t00fcxen | c24796c | 2014-05-06 15:39:13 +0000 | [diff] [blame] | 3327 | /* |
| 3328 | * For LOOPBACK the prison_local_ip4() call will transmute the ip address |
| 3329 | * to the proper value. |
| 3330 | */ |
| 3331 | if (p && (error = prison_local_ip4(p->td_ucred, &sin->sin_addr)) != 0) { |
| 3332 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); |
| 3333 | return (error); |
| 3334 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3335 | #endif |
| 3336 | if (sin->sin_addr.s_addr != INADDR_ANY) { |
| 3337 | bindall = 0; |
| 3338 | } |
| 3339 | break; |
| 3340 | } |
| 3341 | #endif |
| 3342 | #ifdef INET6 |
| 3343 | case AF_INET6: |
| 3344 | { |
| 3345 | /* Only for pure IPv6 Address. (No IPv4 Mapped!) */ |
| 3346 | struct sockaddr_in6 *sin6; |
| 3347 | |
| 3348 | sin6 = (struct sockaddr_in6 *)addr; |
| 3349 | |
t00fcxen | a8a26f6 | 2012-09-12 19:24:54 +0000 | [diff] [blame] | 3350 | #ifdef HAVE_SA_LEN |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3351 | if (addr->sa_len != sizeof(*sin6)) { |
| 3352 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 3353 | return (EINVAL); |
| 3354 | } |
| 3355 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3356 | lport = sin6->sin6_port; |
| 3357 | #if defined(__FreeBSD__) && __FreeBSD_version >= 800000 |
t00fcxen | c24796c | 2014-05-06 15:39:13 +0000 | [diff] [blame] | 3358 | /* |
| 3359 | * For LOOPBACK the prison_local_ip6() call will transmute the ipv6 address |
| 3360 | * to the proper value. |
| 3361 | */ |
| 3362 | if (p && (error = prison_local_ip6(p->td_ucred, &sin6->sin6_addr, |
Michael Tuexen | 72e5cec | 2019-08-05 13:21:02 +0200 | [diff] [blame] | 3363 | (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) { |
t00fcxen | c24796c | 2014-05-06 15:39:13 +0000 | [diff] [blame] | 3364 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); |
| 3365 | return (error); |
| 3366 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3367 | #endif |
| 3368 | if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { |
| 3369 | bindall = 0; |
| 3370 | #ifdef SCTP_EMBEDDED_V6_SCOPE |
| 3371 | /* KAME hack: embed scopeid */ |
| 3372 | #if defined(SCTP_KAME) |
| 3373 | if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) { |
| 3374 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 3375 | return (EINVAL); |
| 3376 | } |
tuexen | e385159 | 2012-06-01 08:31:45 +0000 | [diff] [blame] | 3377 | #elif defined(__APPLE__) |
tuexen | 9a218b1 | 2012-08-04 21:17:58 +0000 | [diff] [blame] | 3378 | #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) |
| 3379 | if (in6_embedscope(&sin6->sin6_addr, sin6, ip_inp, NULL) != 0) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3380 | #else |
tuexen | 9a218b1 | 2012-08-04 21:17:58 +0000 | [diff] [blame] | 3381 | if (in6_embedscope(&sin6->sin6_addr, sin6, ip_inp, NULL, NULL) != 0) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3382 | #endif |
| 3383 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 3384 | return (EINVAL); |
| 3385 | } |
| 3386 | #elif defined(__FreeBSD__) |
| 3387 | error = scope6_check_id(sin6, MODULE_GLOBAL(ip6_use_defzone)); |
| 3388 | if (error != 0) { |
| 3389 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); |
| 3390 | return (error); |
| 3391 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3392 | #else |
| 3393 | if (in6_embedscope(&sin6->sin6_addr, sin6) != 0) { |
| 3394 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 3395 | return (EINVAL); |
| 3396 | } |
| 3397 | #endif |
| 3398 | #endif /* SCTP_EMBEDDED_V6_SCOPE */ |
| 3399 | } |
| 3400 | #ifndef SCOPEDROUTING |
| 3401 | /* this must be cleared for ifa_ifwithaddr() */ |
| 3402 | sin6->sin6_scope_id = 0; |
| 3403 | #endif /* SCOPEDROUTING */ |
| 3404 | break; |
| 3405 | } |
| 3406 | #endif |
tuexen | 09a676e | 2012-07-08 21:03:18 +0000 | [diff] [blame] | 3407 | #if defined(__Userspace__) |
| 3408 | case AF_CONN: |
| 3409 | { |
| 3410 | struct sockaddr_conn *sconn; |
t00fcxen | efb47e7 | 2012-09-09 08:16:51 +0000 | [diff] [blame] | 3411 | |
t00fcxen | a8a26f6 | 2012-09-12 19:24:54 +0000 | [diff] [blame] | 3412 | #ifdef HAVE_SA_LEN |
tuexen | 09a676e | 2012-07-08 21:03:18 +0000 | [diff] [blame] | 3413 | if (addr->sa_len != sizeof(struct sockaddr_conn)) { |
| 3414 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 3415 | return (EINVAL); |
| 3416 | } |
| 3417 | #endif |
| 3418 | sconn = (struct sockaddr_conn *)addr; |
| 3419 | lport = sconn->sconn_port; |
| 3420 | if (sconn->sconn_addr != NULL) { |
| 3421 | bindall = 0; |
| 3422 | } |
| 3423 | break; |
| 3424 | } |
| 3425 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3426 | default: |
| 3427 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EAFNOSUPPORT); |
| 3428 | return (EAFNOSUPPORT); |
| 3429 | } |
| 3430 | } |
| 3431 | SCTP_INP_INFO_WLOCK(); |
| 3432 | SCTP_INP_WLOCK(inp); |
| 3433 | /* 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] | 3434 | vrf_id = inp->def_vrf_id; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3435 | |
| 3436 | /* increase our count due to the unlock we do */ |
| 3437 | SCTP_INP_INCR_REF(inp); |
| 3438 | if (lport) { |
| 3439 | /* |
t00fcxen | efb47e7 | 2012-09-09 08:16:51 +0000 | [diff] [blame] | 3440 | * 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] | 3441 | * already has this one bound. |
| 3442 | */ |
| 3443 | /* got to be root to get at low ports */ |
| 3444 | #if !defined(__Windows__) |
| 3445 | if (ntohs(lport) < IPPORT_RESERVED) { |
Michael Tuexen | 4c4cbd9 | 2017-09-02 21:38:23 +0200 | [diff] [blame] | 3446 | if ((p != NULL) && ((error = |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3447 | #ifdef __FreeBSD__ |
| 3448 | #if __FreeBSD_version > 602000 |
| 3449 | priv_check(p, PRIV_NETINET_RESERVEDPORT) |
| 3450 | #elif __FreeBSD_version >= 500000 |
| 3451 | suser_cred(p->td_ucred, 0) |
| 3452 | #else |
| 3453 | suser(p) |
| 3454 | #endif |
| 3455 | #elif defined(__APPLE__) |
| 3456 | suser(p->p_ucred, &p->p_acflag) |
| 3457 | #elif defined(__Userspace__) /* must be true to use raw socket */ |
t00fcxen | c24796c | 2014-05-06 15:39:13 +0000 | [diff] [blame] | 3458 | 1 |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3459 | #else |
| 3460 | suser(p, 0) |
| 3461 | #endif |
Michael Tuexen | 4c4cbd9 | 2017-09-02 21:38:23 +0200 | [diff] [blame] | 3462 | ) != 0)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3463 | SCTP_INP_DECR_REF(inp); |
| 3464 | SCTP_INP_WUNLOCK(inp); |
| 3465 | SCTP_INP_INFO_WUNLOCK(); |
| 3466 | return (error); |
| 3467 | } |
| 3468 | #if defined(__Panda__) |
| 3469 | if (!SCTP_IS_PRIVILEDGED(so)) { |
| 3470 | SCTP_INP_DECR_REF(inp); |
| 3471 | SCTP_INP_WUNLOCK(inp); |
| 3472 | SCTP_INP_INFO_WUNLOCK(); |
| 3473 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EACCES); |
| 3474 | return (EACCES); |
| 3475 | } |
| 3476 | #endif |
| 3477 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3478 | #endif /* __Windows__ */ |
| 3479 | SCTP_INP_WUNLOCK(inp); |
| 3480 | if (bindall) { |
| 3481 | #ifdef SCTP_MVRF |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 3482 | for (i = 0; i < inp->num_vrfs; i++) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3483 | vrf_id = inp->m_vrf_ids[i]; |
| 3484 | #else |
| 3485 | vrf_id = inp->def_vrf_id; |
| 3486 | #endif |
| 3487 | inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id); |
| 3488 | if (inp_tmp != NULL) { |
| 3489 | /* |
| 3490 | * lock guy returned and lower count |
| 3491 | * note that we are not bound so |
| 3492 | * inp_tmp should NEVER be inp. And |
| 3493 | * it is this inp (inp_tmp) that gets |
| 3494 | * the reference bump, so we must |
| 3495 | * lower it. |
| 3496 | */ |
| 3497 | SCTP_INP_DECR_REF(inp_tmp); |
| 3498 | /* unlock info */ |
| 3499 | if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) && |
| 3500 | (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) { |
| 3501 | /* Ok, must be one-2-one and allowing port re-use */ |
| 3502 | port_reuse_active = 1; |
| 3503 | goto continue_anyway; |
| 3504 | } |
| 3505 | SCTP_INP_DECR_REF(inp); |
| 3506 | SCTP_INP_INFO_WUNLOCK(); |
| 3507 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE); |
| 3508 | return (EADDRINUSE); |
| 3509 | } |
| 3510 | #ifdef SCTP_MVRF |
| 3511 | } |
| 3512 | #endif |
| 3513 | } else { |
| 3514 | inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id); |
| 3515 | if (inp_tmp != NULL) { |
| 3516 | /* |
| 3517 | * lock guy returned and lower count note |
| 3518 | * that we are not bound so inp_tmp should |
| 3519 | * NEVER be inp. And it is this inp (inp_tmp) |
| 3520 | * that gets the reference bump, so we must |
| 3521 | * lower it. |
| 3522 | */ |
| 3523 | SCTP_INP_DECR_REF(inp_tmp); |
| 3524 | /* unlock info */ |
| 3525 | if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) && |
| 3526 | (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) { |
| 3527 | /* Ok, must be one-2-one and allowing port re-use */ |
| 3528 | port_reuse_active = 1; |
| 3529 | goto continue_anyway; |
| 3530 | } |
| 3531 | SCTP_INP_DECR_REF(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 | continue_anyway: |
| 3538 | SCTP_INP_WLOCK(inp); |
| 3539 | if (bindall) { |
| 3540 | /* verify that no lport is not used by a singleton */ |
t00fcxen | efb47e7 | 2012-09-09 08:16:51 +0000 | [diff] [blame] | 3541 | if ((port_reuse_active == 0) && |
| 3542 | (inp_tmp = sctp_isport_inuse(inp, lport, vrf_id))) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3543 | /* Sorry someone already has this one bound */ |
| 3544 | if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) && |
| 3545 | (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) { |
| 3546 | port_reuse_active = 1; |
| 3547 | } else { |
| 3548 | SCTP_INP_DECR_REF(inp); |
| 3549 | SCTP_INP_WUNLOCK(inp); |
| 3550 | SCTP_INP_INFO_WUNLOCK(); |
| 3551 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE); |
| 3552 | return (EADDRINUSE); |
| 3553 | } |
| 3554 | } |
| 3555 | } |
| 3556 | } else { |
| 3557 | uint16_t first, last, candidate; |
t00fcxen | c24796c | 2014-05-06 15:39:13 +0000 | [diff] [blame] | 3558 | uint16_t count; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3559 | int done; |
| 3560 | |
| 3561 | #if defined(__Windows__) |
| 3562 | first = 1; |
| 3563 | last = 0xffff; |
| 3564 | #else |
| 3565 | #if defined(__Userspace__) |
t00fcxen | c24796c | 2014-05-06 15:39:13 +0000 | [diff] [blame] | 3566 | /* TODO ensure uid is 0, etc... */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3567 | #elif defined(__FreeBSD__) || defined(__APPLE__) |
t00fcxen | c24796c | 2014-05-06 15:39:13 +0000 | [diff] [blame] | 3568 | if (ip_inp->inp_flags & INP_HIGHPORT) { |
| 3569 | first = MODULE_GLOBAL(ipport_hifirstauto); |
| 3570 | last = MODULE_GLOBAL(ipport_hilastauto); |
| 3571 | } else if (ip_inp->inp_flags & INP_LOWPORT) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3572 | if (p && (error = |
| 3573 | #ifdef __FreeBSD__ |
| 3574 | #if __FreeBSD_version > 602000 |
| 3575 | priv_check(p, PRIV_NETINET_RESERVEDPORT) |
| 3576 | #elif __FreeBSD_version >= 500000 |
| 3577 | suser_cred(p->td_ucred, 0) |
| 3578 | #else |
| 3579 | suser(p) |
| 3580 | #endif |
| 3581 | #elif defined(__APPLE__) |
| 3582 | suser(p->p_ucred, &p->p_acflag) |
| 3583 | #else |
| 3584 | suser(p, 0) |
| 3585 | #endif |
| 3586 | )) { |
| 3587 | SCTP_INP_DECR_REF(inp); |
| 3588 | SCTP_INP_WUNLOCK(inp); |
| 3589 | SCTP_INP_INFO_WUNLOCK(); |
| 3590 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); |
| 3591 | return (error); |
| 3592 | } |
t00fcxen | c24796c | 2014-05-06 15:39:13 +0000 | [diff] [blame] | 3593 | first = MODULE_GLOBAL(ipport_lowfirstauto); |
| 3594 | last = MODULE_GLOBAL(ipport_lowlastauto); |
| 3595 | } else { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3596 | #endif |
t00fcxen | 8dd95b8 | 2014-07-11 20:41:28 +0000 | [diff] [blame] | 3597 | first = MODULE_GLOBAL(ipport_firstauto); |
| 3598 | last = MODULE_GLOBAL(ipport_lastauto); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3599 | #if defined(__FreeBSD__) || defined(__APPLE__) |
t00fcxen | c24796c | 2014-05-06 15:39:13 +0000 | [diff] [blame] | 3600 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3601 | #endif |
| 3602 | #endif /* __Windows__ */ |
| 3603 | if (first > last) { |
| 3604 | uint16_t temp; |
| 3605 | |
| 3606 | temp = first; |
| 3607 | first = last; |
| 3608 | last = temp; |
| 3609 | } |
| 3610 | count = last - first + 1; /* number of candidates */ |
| 3611 | candidate = first + sctp_select_initial_TSN(&inp->sctp_ep) % (count); |
| 3612 | |
| 3613 | done = 0; |
| 3614 | while (!done) { |
| 3615 | #ifdef SCTP_MVRF |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 3616 | for (i = 0; i < inp->num_vrfs; i++) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3617 | if (sctp_isport_inuse(inp, htons(candidate), inp->m_vrf_ids[i]) != NULL) { |
| 3618 | break; |
| 3619 | } |
| 3620 | } |
| 3621 | if (i == inp->num_vrfs) { |
| 3622 | done = 1; |
| 3623 | } |
| 3624 | #else |
| 3625 | if (sctp_isport_inuse(inp, htons(candidate), inp->def_vrf_id) == NULL) { |
| 3626 | done = 1; |
| 3627 | } |
| 3628 | #endif |
| 3629 | if (!done) { |
| 3630 | if (--count == 0) { |
| 3631 | SCTP_INP_DECR_REF(inp); |
| 3632 | SCTP_INP_WUNLOCK(inp); |
| 3633 | SCTP_INP_INFO_WUNLOCK(); |
| 3634 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE); |
| 3635 | return (EADDRINUSE); |
| 3636 | } |
| 3637 | if (candidate == last) |
| 3638 | candidate = first; |
| 3639 | else |
| 3640 | candidate = candidate + 1; |
| 3641 | } |
| 3642 | } |
| 3643 | lport = htons(candidate); |
| 3644 | } |
| 3645 | SCTP_INP_DECR_REF(inp); |
| 3646 | if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | |
| 3647 | SCTP_PCB_FLAGS_SOCKET_ALLGONE)) { |
| 3648 | /* |
| 3649 | * this really should not happen. The guy did a non-blocking |
| 3650 | * bind and then did a close at the same time. |
| 3651 | */ |
| 3652 | SCTP_INP_WUNLOCK(inp); |
| 3653 | SCTP_INP_INFO_WUNLOCK(); |
| 3654 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 3655 | return (EINVAL); |
| 3656 | } |
| 3657 | /* ok we look clear to give out this port, so lets setup the binding */ |
| 3658 | if (bindall) { |
| 3659 | /* binding to all addresses, so just set in the proper flags */ |
| 3660 | inp->sctp_flags |= SCTP_PCB_FLAGS_BOUNDALL; |
| 3661 | /* set the automatic addr changes from kernel flag */ |
| 3662 | if (SCTP_BASE_SYSCTL(sctp_auto_asconf) == 0) { |
| 3663 | sctp_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF); |
| 3664 | sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF); |
| 3665 | } else { |
| 3666 | sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF); |
| 3667 | sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF); |
| 3668 | } |
| 3669 | if (SCTP_BASE_SYSCTL(sctp_multiple_asconfs) == 0) { |
| 3670 | sctp_feature_off(inp, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS); |
| 3671 | } else { |
| 3672 | sctp_feature_on(inp, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS); |
| 3673 | } |
| 3674 | /* set the automatic mobility_base from kernel |
| 3675 | flag (by micchie) |
| 3676 | */ |
| 3677 | if (SCTP_BASE_SYSCTL(sctp_mobility_base) == 0) { |
| 3678 | sctp_mobility_feature_off(inp, SCTP_MOBILITY_BASE); |
| 3679 | sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED); |
t00fcxen | efb47e7 | 2012-09-09 08:16:51 +0000 | [diff] [blame] | 3680 | } else { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3681 | sctp_mobility_feature_on(inp, SCTP_MOBILITY_BASE); |
| 3682 | sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED); |
| 3683 | } |
| 3684 | /* set the automatic mobility_fasthandoff from kernel |
| 3685 | flag (by micchie) |
| 3686 | */ |
| 3687 | if (SCTP_BASE_SYSCTL(sctp_mobility_fasthandoff) == 0) { |
| 3688 | sctp_mobility_feature_off(inp, SCTP_MOBILITY_FASTHANDOFF); |
| 3689 | sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED); |
t00fcxen | efb47e7 | 2012-09-09 08:16:51 +0000 | [diff] [blame] | 3690 | } else { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3691 | sctp_mobility_feature_on(inp, SCTP_MOBILITY_FASTHANDOFF); |
| 3692 | sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED); |
| 3693 | } |
| 3694 | } else { |
| 3695 | /* |
| 3696 | * bind specific, make sure flags is off and add a new |
| 3697 | * address structure to the sctp_addr_list inside the ep |
| 3698 | * structure. |
| 3699 | * |
| 3700 | * We will need to allocate one and insert it at the head. The |
| 3701 | * socketopt call can just insert new addresses in there as |
| 3702 | * well. It will also have to do the embed scope kame hack |
| 3703 | * too (before adding). |
| 3704 | */ |
| 3705 | struct sctp_ifa *ifa; |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 3706 | union sctp_sockstore store; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3707 | |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 3708 | memset(&store, 0, sizeof(store)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3709 | switch (addr->sa_family) { |
tuexen | 310f1bc | 2012-07-15 11:20:56 +0000 | [diff] [blame] | 3710 | #ifdef INET |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3711 | case AF_INET: |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 3712 | memcpy(&store.sin, addr, sizeof(struct sockaddr_in)); |
| 3713 | store.sin.sin_port = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3714 | break; |
tuexen | 310f1bc | 2012-07-15 11:20:56 +0000 | [diff] [blame] | 3715 | #endif |
| 3716 | #ifdef INET6 |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3717 | case AF_INET6: |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 3718 | memcpy(&store.sin6, addr, sizeof(struct sockaddr_in6)); |
| 3719 | store.sin6.sin6_port = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3720 | break; |
tuexen | 310f1bc | 2012-07-15 11:20:56 +0000 | [diff] [blame] | 3721 | #endif |
tuexen | cc2b142 | 2012-07-10 21:50:32 +0000 | [diff] [blame] | 3722 | #if defined(__Userspace__) |
| 3723 | case AF_CONN: |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 3724 | memcpy(&store.sconn, addr, sizeof(struct sockaddr_conn)); |
| 3725 | store.sconn.sconn_port = 0; |
tuexen | cc2b142 | 2012-07-10 21:50:32 +0000 | [diff] [blame] | 3726 | break; |
tuexen | cc2b142 | 2012-07-10 21:50:32 +0000 | [diff] [blame] | 3727 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3728 | default: |
| 3729 | break; |
| 3730 | } |
| 3731 | /* |
| 3732 | * first find the interface with the bound address need to |
| 3733 | * zero out the port to find the address! yuck! can't do |
| 3734 | * this earlier since need port for sctp_pcb_findep() |
| 3735 | */ |
t00fcxen | efb47e7 | 2012-09-09 08:16:51 +0000 | [diff] [blame] | 3736 | if (sctp_ifap != NULL) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3737 | ifa = sctp_ifap; |
t00fcxen | efb47e7 | 2012-09-09 08:16:51 +0000 | [diff] [blame] | 3738 | } else { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3739 | /* Note for BSD we hit here always other |
| 3740 | * O/S's will pass things in via the |
| 3741 | * sctp_ifap argument (Panda). |
| 3742 | */ |
t00fcxen | 05f07c0 | 2014-09-07 10:15:26 +0000 | [diff] [blame] | 3743 | ifa = sctp_find_ifa_by_addr(&store.sa, |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3744 | vrf_id, SCTP_ADDR_NOT_LOCKED); |
| 3745 | } |
| 3746 | if (ifa == NULL) { |
| 3747 | /* Can't find an interface with that address */ |
| 3748 | SCTP_INP_WUNLOCK(inp); |
| 3749 | SCTP_INP_INFO_WUNLOCK(); |
| 3750 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRNOTAVAIL); |
| 3751 | return (EADDRNOTAVAIL); |
| 3752 | } |
| 3753 | #ifdef INET6 |
| 3754 | if (addr->sa_family == AF_INET6) { |
| 3755 | /* GAK, more FIXME IFA lock? */ |
| 3756 | if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) { |
| 3757 | /* Can't bind a non-existent addr. */ |
| 3758 | SCTP_INP_WUNLOCK(inp); |
| 3759 | SCTP_INP_INFO_WUNLOCK(); |
| 3760 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 3761 | return (EINVAL); |
| 3762 | } |
| 3763 | } |
| 3764 | #endif |
| 3765 | /* we're not bound all */ |
| 3766 | inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUNDALL; |
| 3767 | /* allow bindx() to send ASCONF's for binding changes */ |
| 3768 | sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF); |
| 3769 | /* clear automatic addr changes from kernel flag */ |
| 3770 | sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF); |
| 3771 | |
| 3772 | /* add this address to the endpoint list */ |
| 3773 | error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, 0); |
| 3774 | if (error != 0) { |
| 3775 | SCTP_INP_WUNLOCK(inp); |
| 3776 | SCTP_INP_INFO_WUNLOCK(); |
| 3777 | return (error); |
| 3778 | } |
| 3779 | inp->laddr_count++; |
| 3780 | } |
| 3781 | /* find the bucket */ |
tuexen | 98456cf | 2012-04-19 15:37:07 +0000 | [diff] [blame] | 3782 | if (port_reuse_active) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3783 | /* Put it into tcp 1-2-1 hash */ |
| 3784 | head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashtcpmark))]; |
| 3785 | inp->sctp_flags |= SCTP_PCB_FLAGS_IN_TCPPOOL; |
t00fcxen | c24796c | 2014-05-06 15:39:13 +0000 | [diff] [blame] | 3786 | } else { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3787 | head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashmark))]; |
| 3788 | } |
| 3789 | /* put it in the bucket */ |
| 3790 | LIST_INSERT_HEAD(head, inp, sctp_hash); |
| 3791 | 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] | 3792 | (void *)head, ntohs(lport), port_reuse_active); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3793 | /* set in the port */ |
| 3794 | inp->sctp_lport = lport; |
| 3795 | |
| 3796 | /* turn off just the unbound flag */ |
| 3797 | inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND; |
| 3798 | SCTP_INP_WUNLOCK(inp); |
| 3799 | SCTP_INP_INFO_WUNLOCK(); |
| 3800 | return (0); |
| 3801 | } |
| 3802 | |
| 3803 | |
| 3804 | static void |
| 3805 | sctp_iterator_inp_being_freed(struct sctp_inpcb *inp) |
| 3806 | { |
| 3807 | struct sctp_iterator *it, *nit; |
| 3808 | |
| 3809 | /* |
| 3810 | * We enter with the only the ITERATOR_LOCK in place and a write |
| 3811 | * lock on the inp_info stuff. |
| 3812 | */ |
| 3813 | it = sctp_it_ctl.cur_it; |
| 3814 | #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 |
| 3815 | if (it && (it->vn != curvnet)) { |
| 3816 | /* Its not looking at our VNET */ |
| 3817 | return; |
| 3818 | } |
| 3819 | #endif |
| 3820 | if (it && (it->inp == inp)) { |
| 3821 | /* |
| 3822 | * This is tricky and we hold the iterator lock, |
| 3823 | * but when it returns and gets the lock (when we |
| 3824 | * release it) the iterator will try to operate on |
| 3825 | * inp. We need to stop that from happening. But |
| 3826 | * of course the iterator has a reference on the |
| 3827 | * stcb and inp. We can mark it and it will stop. |
| 3828 | * |
| 3829 | * If its a single iterator situation, we |
| 3830 | * set the end iterator flag. Otherwise |
| 3831 | * we set the iterator to go to the next inp. |
| 3832 | * |
| 3833 | */ |
| 3834 | if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) { |
| 3835 | sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_IT; |
| 3836 | } else { |
| 3837 | sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_INP; |
| 3838 | } |
| 3839 | } |
| 3840 | /* Now go through and remove any single reference to |
| 3841 | * our inp that may be still pending on the list |
| 3842 | */ |
| 3843 | SCTP_IPI_ITERATOR_WQ_LOCK(); |
| 3844 | TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) { |
| 3845 | #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 |
| 3846 | if (it->vn != curvnet) { |
| 3847 | continue; |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 3848 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3849 | #endif |
| 3850 | if (it->inp == inp) { |
| 3851 | /* This one points to me is it inp specific? */ |
| 3852 | if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) { |
| 3853 | /* Remove and free this one */ |
| 3854 | TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, |
| 3855 | it, sctp_nxt_itr); |
| 3856 | if (it->function_atend != NULL) { |
| 3857 | (*it->function_atend) (it->pointer, it->val); |
| 3858 | } |
| 3859 | SCTP_FREE(it, SCTP_M_ITER); |
| 3860 | } else { |
| 3861 | it->inp = LIST_NEXT(it->inp, sctp_list); |
| 3862 | if (it->inp) { |
| 3863 | SCTP_INP_INCR_REF(it->inp); |
| 3864 | } |
| 3865 | } |
| 3866 | /* When its put in the refcnt is incremented so decr it */ |
| 3867 | SCTP_INP_DECR_REF(inp); |
| 3868 | } |
| 3869 | } |
| 3870 | SCTP_IPI_ITERATOR_WQ_UNLOCK(); |
| 3871 | } |
| 3872 | |
| 3873 | /* release sctp_inpcb unbind the port */ |
| 3874 | void |
| 3875 | sctp_inpcb_free(struct sctp_inpcb *inp, int immediate, int from) |
| 3876 | { |
| 3877 | /* |
| 3878 | * Here we free a endpoint. We must find it (if it is in the Hash |
| 3879 | * table) and remove it from there. Then we must also find it in the |
| 3880 | * overall list and remove it from there. After all removals are |
| 3881 | * complete then any timer has to be stopped. Then start the actual |
| 3882 | * freeing. a) Any local lists. b) Any associations. c) The hash of |
| 3883 | * all associations. d) finally the ep itself. |
| 3884 | */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3885 | struct sctp_tcb *asoc, *nasoc; |
| 3886 | struct sctp_laddr *laddr, *nladdr; |
| 3887 | struct inpcb *ip_pcb; |
| 3888 | struct socket *so; |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 3889 | int being_refed = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3890 | struct sctp_queued_to_read *sq, *nsq; |
| 3891 | #if !defined(__Panda__) && !defined(__Userspace__) |
| 3892 | #if !defined(__FreeBSD__) || __FreeBSD_version < 500000 |
| 3893 | sctp_rtentry_t *rt; |
| 3894 | #endif |
| 3895 | #endif |
| 3896 | int cnt; |
| 3897 | sctp_sharedkey_t *shared_key, *nshared_key; |
| 3898 | |
| 3899 | |
| 3900 | #if defined(__APPLE__) |
| 3901 | sctp_lock_assert(SCTP_INP_SO(inp)); |
| 3902 | #endif |
| 3903 | #ifdef SCTP_LOG_CLOSING |
| 3904 | sctp_log_closing(inp, NULL, 0); |
| 3905 | #endif |
| 3906 | SCTP_ITERATOR_LOCK(); |
| 3907 | /* mark any iterators on the list or being processed */ |
| 3908 | sctp_iterator_inp_being_freed(inp); |
| 3909 | SCTP_ITERATOR_UNLOCK(); |
| 3910 | so = inp->sctp_socket; |
| 3911 | if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
| 3912 | /* been here before.. eeks.. get out of here */ |
| 3913 | SCTP_PRINTF("This conflict in free SHOULD not be happening! from %d, imm %d\n", from, immediate); |
| 3914 | #ifdef SCTP_LOG_CLOSING |
| 3915 | sctp_log_closing(inp, NULL, 1); |
| 3916 | #endif |
| 3917 | return; |
| 3918 | } |
| 3919 | SCTP_ASOC_CREATE_LOCK(inp); |
| 3920 | SCTP_INP_INFO_WLOCK(); |
| 3921 | |
| 3922 | SCTP_INP_WLOCK(inp); |
| 3923 | if (from == SCTP_CALLED_AFTER_CMPSET_OFCLOSE) { |
| 3924 | inp->sctp_flags &= ~SCTP_PCB_FLAGS_CLOSE_IP; |
| 3925 | /* socket is gone, so no more wakeups allowed */ |
| 3926 | inp->sctp_flags |= SCTP_PCB_FLAGS_DONT_WAKE; |
| 3927 | inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT; |
| 3928 | inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT; |
| 3929 | |
| 3930 | } |
| 3931 | /* First time through we have the socket lock, after that no more. */ |
| 3932 | sctp_timer_stop(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL, |
Michael Tuexen | 390beeb | 2020-02-04 15:05:45 +0100 | [diff] [blame] | 3933 | SCTP_FROM_SCTP_PCB + SCTP_LOC_1); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3934 | |
| 3935 | if (inp->control) { |
| 3936 | sctp_m_freem(inp->control); |
| 3937 | inp->control = NULL; |
| 3938 | } |
| 3939 | if (inp->pkt) { |
| 3940 | sctp_m_freem(inp->pkt); |
| 3941 | inp->pkt = NULL; |
| 3942 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3943 | ip_pcb = &inp->ip_inp.inp; /* we could just cast the main pointer |
| 3944 | * here but I will be nice :> (i.e. |
| 3945 | * ip_pcb = ep;) */ |
| 3946 | if (immediate == SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE) { |
| 3947 | int cnt_in_sd; |
| 3948 | |
| 3949 | cnt_in_sd = 0; |
| 3950 | LIST_FOREACH_SAFE(asoc, &inp->sctp_asoc_list, sctp_tcblist, nasoc) { |
| 3951 | SCTP_TCB_LOCK(asoc); |
| 3952 | if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { |
| 3953 | /* Skip guys being freed */ |
| 3954 | cnt_in_sd++; |
| 3955 | if (asoc->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE) { |
| 3956 | /* |
| 3957 | * Special case - we did not start a kill |
| 3958 | * timer on the asoc due to it was not |
| 3959 | * closed. So go ahead and start it now. |
| 3960 | */ |
Michael Tuexen | 348a36c | 2018-08-13 16:24:47 +0200 | [diff] [blame] | 3961 | SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_IN_ACCEPT_QUEUE); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3962 | sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, asoc, NULL); |
| 3963 | } |
| 3964 | SCTP_TCB_UNLOCK(asoc); |
| 3965 | continue; |
| 3966 | } |
Michael Tuexen | 348a36c | 2018-08-13 16:24:47 +0200 | [diff] [blame] | 3967 | if (((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) || |
| 3968 | (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) && |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3969 | (asoc->asoc.total_output_queue_size == 0)) { |
| 3970 | /* If we have data in queue, we don't want to just |
| 3971 | * free since the app may have done, send()/close |
| 3972 | * or connect/send/close. And it wants the data |
| 3973 | * to get across first. |
| 3974 | */ |
| 3975 | /* Just abandon things in the front states */ |
| 3976 | if (sctp_free_assoc(inp, asoc, SCTP_PCBFREE_NOFORCE, |
t00fcxen | 0057a6d | 2015-05-28 16:42:49 +0000 | [diff] [blame] | 3977 | SCTP_FROM_SCTP_PCB + SCTP_LOC_2) == 0) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3978 | cnt_in_sd++; |
| 3979 | } |
| 3980 | continue; |
| 3981 | } |
| 3982 | /* Disconnect the socket please */ |
| 3983 | asoc->sctp_socket = NULL; |
Michael Tuexen | 348a36c | 2018-08-13 16:24:47 +0200 | [diff] [blame] | 3984 | SCTP_ADD_SUBSTATE(asoc, SCTP_STATE_CLOSED_SOCKET); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3985 | if ((asoc->asoc.size_on_reasm_queue > 0) || |
| 3986 | (asoc->asoc.control_pdapi) || |
| 3987 | (asoc->asoc.size_on_all_streams > 0) || |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 3988 | (so && (so->so_rcv.sb_cc > 0))) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3989 | /* Left with Data unread */ |
| 3990 | struct mbuf *op_err; |
| 3991 | |
t00fcxen | 08f9ff9 | 2014-03-16 13:38:54 +0000 | [diff] [blame] | 3992 | op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, ""); |
t00fcxen | 0057a6d | 2015-05-28 16:42:49 +0000 | [diff] [blame] | 3993 | asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_3; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3994 | sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED); |
| 3995 | SCTP_STAT_INCR_COUNTER32(sctps_aborted); |
Michael Tuexen | 348a36c | 2018-08-13 16:24:47 +0200 | [diff] [blame] | 3996 | if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) || |
| 3997 | (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 3998 | SCTP_STAT_DECR_GAUGE32(sctps_currestab); |
| 3999 | } |
| 4000 | if (sctp_free_assoc(inp, asoc, |
t00fcxen | 0057a6d | 2015-05-28 16:42:49 +0000 | [diff] [blame] | 4001 | SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_4) == 0) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4002 | cnt_in_sd++; |
| 4003 | } |
| 4004 | continue; |
| 4005 | } else if (TAILQ_EMPTY(&asoc->asoc.send_queue) && |
| 4006 | TAILQ_EMPTY(&asoc->asoc.sent_queue) && |
t00fcxen | 28611aa | 2012-09-23 07:45:40 +0000 | [diff] [blame] | 4007 | (asoc->asoc.stream_queue_cnt == 0)) { |
Michael Tuexen | fdcf790 | 2016-08-06 14:39:31 +0200 | [diff] [blame] | 4008 | 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] | 4009 | goto abort_anyway; |
| 4010 | } |
Michael Tuexen | 348a36c | 2018-08-13 16:24:47 +0200 | [diff] [blame] | 4011 | if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) && |
| 4012 | (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4013 | struct sctp_nets *netp; |
| 4014 | |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 4015 | /* |
| 4016 | * there is nothing queued to send, |
| 4017 | * so I send shutdown |
| 4018 | */ |
Michael Tuexen | 348a36c | 2018-08-13 16:24:47 +0200 | [diff] [blame] | 4019 | if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) || |
| 4020 | (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 4021 | SCTP_STAT_DECR_GAUGE32(sctps_currestab); |
| 4022 | } |
Michael Tuexen | 348a36c | 2018-08-13 16:24:47 +0200 | [diff] [blame] | 4023 | SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT); |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 4024 | sctp_stop_timers_for_shutdown(asoc); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4025 | if (asoc->asoc.alternate) { |
| 4026 | netp = asoc->asoc.alternate; |
| 4027 | } else { |
| 4028 | netp = asoc->asoc.primary_destination; |
| 4029 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4030 | sctp_send_shutdown(asoc, netp); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4031 | sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, asoc->sctp_ep, asoc, |
| 4032 | netp); |
Michael Tuexen | d07a5f2 | 2020-03-19 23:34:46 +0100 | [diff] [blame] | 4033 | sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc, NULL); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4034 | sctp_chunk_output(inp, asoc, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_LOCKED); |
| 4035 | } |
| 4036 | } else { |
| 4037 | /* mark into shutdown pending */ |
Michael Tuexen | 348a36c | 2018-08-13 16:24:47 +0200 | [diff] [blame] | 4038 | SCTP_ADD_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING); |
Michael Tuexen | d07a5f2 | 2020-03-19 23:34:46 +0100 | [diff] [blame] | 4039 | sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc, NULL); |
Michael Tuexen | fdcf790 | 2016-08-06 14:39:31 +0200 | [diff] [blame] | 4040 | 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] | 4041 | SCTP_ADD_SUBSTATE(asoc, SCTP_STATE_PARTIAL_MSG_LEFT); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4042 | } |
| 4043 | if (TAILQ_EMPTY(&asoc->asoc.send_queue) && |
| 4044 | TAILQ_EMPTY(&asoc->asoc.sent_queue) && |
| 4045 | (asoc->asoc.state & SCTP_STATE_PARTIAL_MSG_LEFT)) { |
| 4046 | struct mbuf *op_err; |
| 4047 | abort_anyway: |
t00fcxen | 08f9ff9 | 2014-03-16 13:38:54 +0000 | [diff] [blame] | 4048 | op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, ""); |
t00fcxen | 0057a6d | 2015-05-28 16:42:49 +0000 | [diff] [blame] | 4049 | asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_5; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4050 | sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED); |
| 4051 | SCTP_STAT_INCR_COUNTER32(sctps_aborted); |
Michael Tuexen | 348a36c | 2018-08-13 16:24:47 +0200 | [diff] [blame] | 4052 | if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) || |
| 4053 | (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4054 | SCTP_STAT_DECR_GAUGE32(sctps_currestab); |
| 4055 | } |
| 4056 | if (sctp_free_assoc(inp, asoc, |
| 4057 | SCTP_PCBFREE_NOFORCE, |
t00fcxen | 0057a6d | 2015-05-28 16:42:49 +0000 | [diff] [blame] | 4058 | SCTP_FROM_SCTP_PCB + SCTP_LOC_6) == 0) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4059 | cnt_in_sd++; |
| 4060 | } |
| 4061 | continue; |
| 4062 | } else { |
| 4063 | sctp_chunk_output(inp, asoc, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED); |
| 4064 | } |
| 4065 | } |
| 4066 | cnt_in_sd++; |
| 4067 | SCTP_TCB_UNLOCK(asoc); |
| 4068 | } |
| 4069 | /* now is there some left in our SHUTDOWN state? */ |
| 4070 | if (cnt_in_sd) { |
| 4071 | #ifdef SCTP_LOG_CLOSING |
| 4072 | sctp_log_closing(inp, NULL, 2); |
| 4073 | #endif |
| 4074 | inp->sctp_socket = NULL; |
| 4075 | SCTP_INP_WUNLOCK(inp); |
| 4076 | SCTP_ASOC_CREATE_UNLOCK(inp); |
| 4077 | SCTP_INP_INFO_WUNLOCK(); |
| 4078 | return; |
| 4079 | } |
| 4080 | } |
| 4081 | inp->sctp_socket = NULL; |
| 4082 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) != |
| 4083 | SCTP_PCB_FLAGS_UNBOUND) { |
| 4084 | /* |
| 4085 | * ok, this guy has been bound. It's port is |
| 4086 | * somewhere in the SCTP_BASE_INFO(hash table). Remove |
| 4087 | * it! |
| 4088 | */ |
| 4089 | LIST_REMOVE(inp, sctp_hash); |
| 4090 | inp->sctp_flags |= SCTP_PCB_FLAGS_UNBOUND; |
| 4091 | } |
| 4092 | |
| 4093 | /* If there is a timer running to kill us, |
| 4094 | * forget it, since it may have a contest |
| 4095 | * on the INP lock.. which would cause us |
| 4096 | * to die ... |
| 4097 | */ |
| 4098 | cnt = 0; |
| 4099 | LIST_FOREACH_SAFE(asoc, &inp->sctp_asoc_list, sctp_tcblist, nasoc) { |
| 4100 | SCTP_TCB_LOCK(asoc); |
| 4101 | if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { |
| 4102 | if (asoc->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE) { |
Michael Tuexen | 348a36c | 2018-08-13 16:24:47 +0200 | [diff] [blame] | 4103 | SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_IN_ACCEPT_QUEUE); |
t00fcxen | 28611aa | 2012-09-23 07:45:40 +0000 | [diff] [blame] | 4104 | sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, asoc, NULL); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4105 | } |
Michael Tuexen | 61f6683 | 2020-02-09 23:15:19 +0100 | [diff] [blame] | 4106 | cnt++; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4107 | SCTP_TCB_UNLOCK(asoc); |
| 4108 | continue; |
| 4109 | } |
| 4110 | /* Free associations that are NOT killing us */ |
Michael Tuexen | 348a36c | 2018-08-13 16:24:47 +0200 | [diff] [blame] | 4111 | if ((SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) && |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4112 | ((asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0)) { |
| 4113 | struct mbuf *op_err; |
t00fcxen | 28611aa | 2012-09-23 07:45:40 +0000 | [diff] [blame] | 4114 | |
t00fcxen | 08f9ff9 | 2014-03-16 13:38:54 +0000 | [diff] [blame] | 4115 | op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, ""); |
t00fcxen | 0057a6d | 2015-05-28 16:42:49 +0000 | [diff] [blame] | 4116 | asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_7; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4117 | sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED); |
| 4118 | SCTP_STAT_INCR_COUNTER32(sctps_aborted); |
| 4119 | } else if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { |
| 4120 | cnt++; |
| 4121 | SCTP_TCB_UNLOCK(asoc); |
| 4122 | continue; |
| 4123 | } |
Michael Tuexen | 348a36c | 2018-08-13 16:24:47 +0200 | [diff] [blame] | 4124 | if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) || |
| 4125 | (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4126 | SCTP_STAT_DECR_GAUGE32(sctps_currestab); |
| 4127 | } |
t00fcxen | 0057a6d | 2015-05-28 16:42:49 +0000 | [diff] [blame] | 4128 | if (sctp_free_assoc(inp, asoc, SCTP_PCBFREE_FORCE, |
| 4129 | SCTP_FROM_SCTP_PCB + SCTP_LOC_8) == 0) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4130 | cnt++; |
| 4131 | } |
| 4132 | } |
| 4133 | if (cnt) { |
| 4134 | /* Ok we have someone out there that will kill us */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4135 | #ifdef SCTP_LOG_CLOSING |
| 4136 | sctp_log_closing(inp, NULL, 3); |
| 4137 | #endif |
| 4138 | SCTP_INP_WUNLOCK(inp); |
| 4139 | SCTP_ASOC_CREATE_UNLOCK(inp); |
| 4140 | SCTP_INP_INFO_WUNLOCK(); |
| 4141 | return; |
| 4142 | } |
| 4143 | if (SCTP_INP_LOCK_CONTENDED(inp)) |
| 4144 | being_refed++; |
| 4145 | if (SCTP_INP_READ_CONTENDED(inp)) |
| 4146 | being_refed++; |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 4147 | if (SCTP_ASOC_CREATE_LOCK_CONTENDED(inp)) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4148 | being_refed++; |
| 4149 | |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 4150 | if ((inp->refcount) || |
t00fcxen | 28611aa | 2012-09-23 07:45:40 +0000 | [diff] [blame] | 4151 | (being_refed) || |
| 4152 | (inp->sctp_flags & SCTP_PCB_FLAGS_CLOSE_IP)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4153 | #ifdef SCTP_LOG_CLOSING |
| 4154 | sctp_log_closing(inp, NULL, 4); |
| 4155 | #endif |
| 4156 | sctp_timer_start(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL); |
| 4157 | SCTP_INP_WUNLOCK(inp); |
| 4158 | SCTP_ASOC_CREATE_UNLOCK(inp); |
| 4159 | SCTP_INP_INFO_WUNLOCK(); |
| 4160 | return; |
| 4161 | } |
| 4162 | inp->sctp_ep.signature_change.type = 0; |
| 4163 | inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_ALLGONE; |
| 4164 | /* Remove it from the list .. last thing we need a |
| 4165 | * lock for. |
| 4166 | */ |
| 4167 | LIST_REMOVE(inp, sctp_list); |
| 4168 | SCTP_INP_WUNLOCK(inp); |
| 4169 | SCTP_ASOC_CREATE_UNLOCK(inp); |
| 4170 | SCTP_INP_INFO_WUNLOCK(); |
| 4171 | /* Now we release all locks. Since this INP |
| 4172 | * cannot be found anymore except possibly by the |
| 4173 | * kill timer that might be running. We call |
| 4174 | * the drain function here. It should hit the case |
| 4175 | * were it sees the ACTIVE flag cleared and exit |
| 4176 | * out freeing us to proceed and destroy everything. |
| 4177 | */ |
| 4178 | if (from != SCTP_CALLED_FROM_INPKILL_TIMER) { |
| 4179 | (void)SCTP_OS_TIMER_STOP_DRAIN(&inp->sctp_ep.signature_change.timer); |
| 4180 | } else { |
| 4181 | /* Probably un-needed */ |
| 4182 | (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer); |
| 4183 | } |
| 4184 | |
| 4185 | #ifdef SCTP_LOG_CLOSING |
| 4186 | sctp_log_closing(inp, NULL, 5); |
| 4187 | #endif |
| 4188 | |
| 4189 | #if !(defined(__Panda__) || defined(__Windows__) || defined(__Userspace__)) |
| 4190 | #if !defined(__FreeBSD__) || __FreeBSD_version < 500000 |
| 4191 | rt = ip_pcb->inp_route.ro_rt; |
| 4192 | #endif |
| 4193 | #endif |
| 4194 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4195 | if ((inp->sctp_asocidhash) != NULL) { |
| 4196 | SCTP_HASH_FREE(inp->sctp_asocidhash, inp->hashasocidmark); |
| 4197 | inp->sctp_asocidhash = NULL; |
| 4198 | } |
| 4199 | /*sa_ignore FREED_MEMORY*/ |
| 4200 | TAILQ_FOREACH_SAFE(sq, &inp->read_queue, next, nsq) { |
| 4201 | /* Its only abandoned if it had data left */ |
| 4202 | if (sq->length) |
| 4203 | SCTP_STAT_INCR(sctps_left_abandon); |
| 4204 | |
| 4205 | TAILQ_REMOVE(&inp->read_queue, sq, next); |
| 4206 | sctp_free_remote_addr(sq->whoFrom); |
| 4207 | if (so) |
| 4208 | so->so_rcv.sb_cc -= sq->length; |
| 4209 | if (sq->data) { |
| 4210 | sctp_m_freem(sq->data); |
| 4211 | sq->data = NULL; |
| 4212 | } |
| 4213 | /* |
| 4214 | * no need to free the net count, since at this point all |
| 4215 | * assoc's are gone. |
| 4216 | */ |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 4217 | sctp_free_a_readq(NULL, sq); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4218 | } |
| 4219 | /* Now the sctp_pcb things */ |
| 4220 | /* |
| 4221 | * free each asoc if it is not already closed/free. we can't use the |
| 4222 | * macro here since le_next will get freed as part of the |
| 4223 | * sctp_free_assoc() call. |
| 4224 | */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4225 | #ifndef __Panda__ |
| 4226 | if (ip_pcb->inp_options) { |
| 4227 | (void)sctp_m_free(ip_pcb->inp_options); |
| 4228 | ip_pcb->inp_options = 0; |
| 4229 | } |
| 4230 | #endif |
| 4231 | |
| 4232 | #if !(defined(__Panda__) || defined(__Windows__) || defined(__Userspace__)) |
| 4233 | #if !defined(__FreeBSD__) || __FreeBSD_version < 500000 |
| 4234 | if (rt) { |
| 4235 | RTFREE(rt); |
| 4236 | ip_pcb->inp_route.ro_rt = 0; |
| 4237 | } |
| 4238 | #endif |
tuexen | 5fc2284 | 2012-02-11 13:32:19 +0000 | [diff] [blame] | 4239 | #if defined(__FreeBSD__) && __FreeBSD_version < 803000 |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4240 | #ifdef INET |
| 4241 | if (ip_pcb->inp_moptions) { |
| 4242 | inp_freemoptions(ip_pcb->inp_moptions); |
| 4243 | ip_pcb->inp_moptions = 0; |
| 4244 | } |
| 4245 | #endif |
| 4246 | #endif |
| 4247 | #endif |
| 4248 | |
| 4249 | #ifdef INET6 |
t00fcxen | 4c76a91 | 2012-09-14 14:31:21 +0000 | [diff] [blame] | 4250 | #if !(defined(__Panda__) || defined(__Windows__) || defined(__Userspace__)) |
| 4251 | #if defined(__FreeBSD__) || defined(__APPLE__) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4252 | if (ip_pcb->inp_vflag & INP_IPV6) { |
t00fcxen | 4c76a91 | 2012-09-14 14:31:21 +0000 | [diff] [blame] | 4253 | #else |
| 4254 | if (inp->inp_vflag & INP_IPV6) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4255 | #endif |
Michael Tuexen | 801a64b | 2019-08-05 15:33:37 +0200 | [diff] [blame] | 4256 | ip6_freepcbopts(ip_pcb->in6p_outputopts); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4257 | } |
t00fcxen | 4c76a91 | 2012-09-14 14:31:21 +0000 | [diff] [blame] | 4258 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4259 | #endif /* INET6 */ |
| 4260 | #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)) |
| 4261 | inp->inp_vflag = 0; |
| 4262 | #else |
| 4263 | ip_pcb->inp_vflag = 0; |
| 4264 | #endif |
| 4265 | /* free up authentication fields */ |
| 4266 | if (inp->sctp_ep.local_auth_chunks != NULL) |
| 4267 | sctp_free_chunklist(inp->sctp_ep.local_auth_chunks); |
| 4268 | if (inp->sctp_ep.local_hmacs != NULL) |
| 4269 | sctp_free_hmaclist(inp->sctp_ep.local_hmacs); |
| 4270 | |
| 4271 | LIST_FOREACH_SAFE(shared_key, &inp->sctp_ep.shared_keys, next, nshared_key) { |
| 4272 | LIST_REMOVE(shared_key, next); |
| 4273 | sctp_free_sharedkey(shared_key); |
| 4274 | /*sa_ignore FREED_MEMORY*/ |
| 4275 | } |
| 4276 | |
| 4277 | #if defined(__APPLE__) |
| 4278 | inp->ip_inp.inp.inp_state = INPCB_STATE_DEAD; |
tuexen | 6d01b94 | 2012-05-04 18:15:17 +0000 | [diff] [blame] | 4279 | if (in_pcb_checkstate(&inp->ip_inp.inp, WNT_STOPUSING, 1) != WNT_STOPUSING) { |
tuexen | 49fdcc8 | 2012-05-04 10:13:12 +0000 | [diff] [blame] | 4280 | #ifdef INVARIANTS |
t00fcxen | 8fca7ff | 2012-09-05 18:58:42 +0000 | [diff] [blame] | 4281 | 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] | 4282 | #else |
t00fcxen | 8fca7ff | 2012-09-05 18:58:42 +0000 | [diff] [blame] | 4283 | 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] | 4284 | #endif |
tuexen | 6d01b94 | 2012-05-04 18:15:17 +0000 | [diff] [blame] | 4285 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4286 | inp->ip_inp.inp.inp_socket->so_flags |= SOF_PCBCLEARING; |
| 4287 | #endif |
| 4288 | /* |
| 4289 | * if we have an address list the following will free the list of |
| 4290 | * ifaddr's that are set into this ep. Again macro limitations here, |
| 4291 | * since the LIST_FOREACH could be a bad idea. |
| 4292 | */ |
| 4293 | LIST_FOREACH_SAFE(laddr, &inp->sctp_addr_list, sctp_nxt_addr, nladdr) { |
| 4294 | sctp_remove_laddr(laddr); |
| 4295 | } |
| 4296 | |
| 4297 | #ifdef SCTP_TRACK_FREED_ASOCS |
| 4298 | /* TEMP CODE */ |
| 4299 | LIST_FOREACH_SAFE(asoc, &inp->sctp_asoc_free_list, sctp_tcblist, nasoc) { |
| 4300 | LIST_REMOVE(asoc, sctp_tcblist); |
| 4301 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), asoc); |
| 4302 | SCTP_DECR_ASOC_COUNT(); |
| 4303 | } |
| 4304 | /* *** END TEMP CODE ****/ |
| 4305 | #endif |
| 4306 | #ifdef SCTP_MVRF |
| 4307 | SCTP_FREE(inp->m_vrf_ids, SCTP_M_MVRF); |
| 4308 | #endif |
| 4309 | /* Now lets see about freeing the EP hash table. */ |
| 4310 | if (inp->sctp_tcbhash != NULL) { |
| 4311 | SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark); |
| 4312 | inp->sctp_tcbhash = NULL; |
| 4313 | } |
| 4314 | /* Now we must put the ep memory back into the zone pool */ |
| 4315 | #if defined(__FreeBSD__) |
t00fcxen | 326aa4b | 2014-06-20 13:42:13 +0000 | [diff] [blame] | 4316 | crfree(inp->ip_inp.inp.inp_cred); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4317 | INP_LOCK_DESTROY(&inp->ip_inp.inp); |
| 4318 | #endif |
| 4319 | SCTP_INP_LOCK_DESTROY(inp); |
| 4320 | SCTP_INP_READ_DESTROY(inp); |
| 4321 | SCTP_ASOC_CREATE_LOCK_DESTROY(inp); |
| 4322 | #if !defined(__APPLE__) |
| 4323 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp); |
| 4324 | SCTP_DECR_EP_COUNT(); |
| 4325 | #else |
| 4326 | /* For Tiger, we will do this later... */ |
| 4327 | #endif |
| 4328 | } |
| 4329 | |
| 4330 | |
| 4331 | struct sctp_nets * |
| 4332 | sctp_findnet(struct sctp_tcb *stcb, struct sockaddr *addr) |
| 4333 | { |
| 4334 | struct sctp_nets *net; |
| 4335 | /* locate the address */ |
| 4336 | TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { |
| 4337 | if (sctp_cmpaddr(addr, (struct sockaddr *)&net->ro._l_addr)) |
| 4338 | return (net); |
| 4339 | } |
| 4340 | return (NULL); |
| 4341 | } |
| 4342 | |
| 4343 | |
| 4344 | int |
| 4345 | sctp_is_address_on_local_host(struct sockaddr *addr, uint32_t vrf_id) |
| 4346 | { |
| 4347 | #ifdef __Panda__ |
| 4348 | return (0); |
| 4349 | #else |
| 4350 | struct sctp_ifa *sctp_ifa; |
| 4351 | sctp_ifa = sctp_find_ifa_by_addr(addr, vrf_id, SCTP_ADDR_NOT_LOCKED); |
| 4352 | if (sctp_ifa) { |
| 4353 | return (1); |
| 4354 | } else { |
| 4355 | return (0); |
| 4356 | } |
| 4357 | #endif |
| 4358 | } |
| 4359 | |
| 4360 | /* |
| 4361 | * add's a remote endpoint address, done with the INIT/INIT-ACK as well as |
| 4362 | * when a ASCONF arrives that adds it. It will also initialize all the cwnd |
| 4363 | * stats of stuff. |
| 4364 | */ |
| 4365 | int |
| 4366 | sctp_add_remote_addr(struct sctp_tcb *stcb, struct sockaddr *newaddr, |
Michael Tuexen | 2757442 | 2016-04-30 16:35:29 +0200 | [diff] [blame] | 4367 | struct sctp_nets **netp, uint16_t port, int set_scope, int from) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4368 | { |
| 4369 | /* |
| 4370 | * The following is redundant to the same lines in the |
| 4371 | * sctp_aloc_assoc() but is needed since others call the add |
| 4372 | * address function |
| 4373 | */ |
| 4374 | struct sctp_nets *net, *netfirst; |
| 4375 | int addr_inscope; |
| 4376 | |
| 4377 | SCTPDBG(SCTP_DEBUG_PCB1, "Adding an address (from:%d) to the peer: ", |
| 4378 | from); |
| 4379 | SCTPDBG_ADDR(SCTP_DEBUG_PCB1, newaddr); |
| 4380 | |
| 4381 | netfirst = sctp_findnet(stcb, newaddr); |
| 4382 | if (netfirst) { |
| 4383 | /* |
| 4384 | * Lie and return ok, we don't want to make the association |
| 4385 | * go away for this behavior. It will happen in the TCP |
| 4386 | * model in a connected socket. It does not reach the hash |
| 4387 | * table until after the association is built so it can't be |
| 4388 | * found. Mark as reachable, since the initial creation will |
| 4389 | * have been cleared and the NOT_IN_ASSOC flag will have |
| 4390 | * been added... and we don't want to end up removing it |
| 4391 | * back out. |
| 4392 | */ |
| 4393 | if (netfirst->dest_state & SCTP_ADDR_UNCONFIRMED) { |
| 4394 | netfirst->dest_state = (SCTP_ADDR_REACHABLE | |
| 4395 | SCTP_ADDR_UNCONFIRMED); |
| 4396 | } else { |
| 4397 | netfirst->dest_state = SCTP_ADDR_REACHABLE; |
| 4398 | } |
| 4399 | |
| 4400 | return (0); |
| 4401 | } |
| 4402 | addr_inscope = 1; |
| 4403 | switch (newaddr->sa_family) { |
| 4404 | #ifdef INET |
| 4405 | case AF_INET: |
| 4406 | { |
| 4407 | struct sockaddr_in *sin; |
| 4408 | |
| 4409 | sin = (struct sockaddr_in *)newaddr; |
| 4410 | if (sin->sin_addr.s_addr == 0) { |
| 4411 | /* Invalid address */ |
| 4412 | return (-1); |
| 4413 | } |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 4414 | /* zero out the zero area */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4415 | memset(&sin->sin_zero, 0, sizeof(sin->sin_zero)); |
| 4416 | |
| 4417 | /* assure len is set */ |
t00fcxen | 8fbaee3 | 2012-09-04 16:41:39 +0000 | [diff] [blame] | 4418 | #ifdef HAVE_SIN_LEN |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4419 | sin->sin_len = sizeof(struct sockaddr_in); |
| 4420 | #endif |
| 4421 | if (set_scope) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4422 | if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) { |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 4423 | stcb->asoc.scope.ipv4_local_scope = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4424 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4425 | } else { |
| 4426 | /* Validate the address is in scope */ |
| 4427 | if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) && |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 4428 | (stcb->asoc.scope.ipv4_local_scope == 0)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4429 | addr_inscope = 0; |
| 4430 | } |
| 4431 | } |
| 4432 | break; |
| 4433 | } |
| 4434 | #endif |
| 4435 | #ifdef INET6 |
| 4436 | case AF_INET6: |
| 4437 | { |
| 4438 | struct sockaddr_in6 *sin6; |
| 4439 | |
| 4440 | sin6 = (struct sockaddr_in6 *)newaddr; |
| 4441 | if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { |
| 4442 | /* Invalid address */ |
| 4443 | return (-1); |
| 4444 | } |
| 4445 | /* assure len is set */ |
t00fcxen | 8fbaee3 | 2012-09-04 16:41:39 +0000 | [diff] [blame] | 4446 | #ifdef HAVE_SIN6_LEN |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4447 | sin6->sin6_len = sizeof(struct sockaddr_in6); |
| 4448 | #endif |
| 4449 | if (set_scope) { |
| 4450 | if (sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id)) { |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 4451 | stcb->asoc.scope.loopback_scope = 1; |
| 4452 | stcb->asoc.scope.local_scope = 0; |
| 4453 | stcb->asoc.scope.ipv4_local_scope = 1; |
| 4454 | stcb->asoc.scope.site_scope = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4455 | } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { |
| 4456 | /* |
| 4457 | * If the new destination is a LINK_LOCAL we |
| 4458 | * must have common site scope. Don't set |
| 4459 | * the local scope since we may not share |
| 4460 | * all links, only loopback can do this. |
| 4461 | * Links on the local network would also be |
| 4462 | * on our private network for v4 too. |
| 4463 | */ |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 4464 | stcb->asoc.scope.ipv4_local_scope = 1; |
| 4465 | stcb->asoc.scope.site_scope = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4466 | } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) { |
| 4467 | /* |
| 4468 | * If the new destination is SITE_LOCAL then |
| 4469 | * we must have site scope in common. |
| 4470 | */ |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 4471 | stcb->asoc.scope.site_scope = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4472 | } |
| 4473 | } else { |
| 4474 | /* Validate the address is in scope */ |
| 4475 | if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) && |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 4476 | (stcb->asoc.scope.loopback_scope == 0)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4477 | addr_inscope = 0; |
| 4478 | } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) && |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 4479 | (stcb->asoc.scope.local_scope == 0)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4480 | addr_inscope = 0; |
| 4481 | } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) && |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 4482 | (stcb->asoc.scope.site_scope == 0)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4483 | addr_inscope = 0; |
| 4484 | } |
| 4485 | } |
| 4486 | break; |
| 4487 | } |
| 4488 | #endif |
tuexen | cc2b142 | 2012-07-10 21:50:32 +0000 | [diff] [blame] | 4489 | #if defined(__Userspace__) |
| 4490 | case AF_CONN: |
| 4491 | { |
| 4492 | struct sockaddr_conn *sconn; |
| 4493 | |
| 4494 | sconn = (struct sockaddr_conn *)newaddr; |
| 4495 | if (sconn->sconn_addr == NULL) { |
| 4496 | /* Invalid address */ |
| 4497 | return (-1); |
| 4498 | } |
t00fcxen | 8fbaee3 | 2012-09-04 16:41:39 +0000 | [diff] [blame] | 4499 | #ifdef HAVE_SCONN_LEN |
tuexen | cc2b142 | 2012-07-10 21:50:32 +0000 | [diff] [blame] | 4500 | sconn->sconn_len = sizeof(struct sockaddr_conn); |
| 4501 | #endif |
| 4502 | break; |
| 4503 | } |
| 4504 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4505 | default: |
| 4506 | /* not supported family type */ |
| 4507 | return (-1); |
| 4508 | } |
| 4509 | net = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_net), struct sctp_nets); |
| 4510 | if (net == NULL) { |
| 4511 | return (-1); |
| 4512 | } |
| 4513 | SCTP_INCR_RADDR_COUNT(); |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 4514 | memset(net, 0, sizeof(struct sctp_nets)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4515 | (void)SCTP_GETTIME_TIMEVAL(&net->start_time); |
t00fcxen | 907930b | 2012-09-12 19:43:22 +0000 | [diff] [blame] | 4516 | #ifdef HAVE_SA_LEN |
| 4517 | memcpy(&net->ro._l_addr, newaddr, newaddr->sa_len); |
| 4518 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4519 | switch (newaddr->sa_family) { |
| 4520 | #ifdef INET |
| 4521 | case AF_INET: |
t00fcxen | 907930b | 2012-09-12 19:43:22 +0000 | [diff] [blame] | 4522 | #ifndef HAVE_SA_LEN |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4523 | memcpy(&net->ro._l_addr, newaddr, sizeof(struct sockaddr_in)); |
| 4524 | #endif |
| 4525 | ((struct sockaddr_in *)&net->ro._l_addr)->sin_port = stcb->rport; |
| 4526 | break; |
| 4527 | #endif |
| 4528 | #ifdef INET6 |
| 4529 | case AF_INET6: |
t00fcxen | 907930b | 2012-09-12 19:43:22 +0000 | [diff] [blame] | 4530 | #ifndef HAVE_SA_LEN |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4531 | memcpy(&net->ro._l_addr, newaddr, sizeof(struct sockaddr_in6)); |
| 4532 | #endif |
| 4533 | ((struct sockaddr_in6 *)&net->ro._l_addr)->sin6_port = stcb->rport; |
| 4534 | break; |
| 4535 | #endif |
tuexen | cc2b142 | 2012-07-10 21:50:32 +0000 | [diff] [blame] | 4536 | #if defined(__Userspace__) |
| 4537 | case AF_CONN: |
t00fcxen | 907930b | 2012-09-12 19:43:22 +0000 | [diff] [blame] | 4538 | #ifndef HAVE_SA_LEN |
tuexen | cc2b142 | 2012-07-10 21:50:32 +0000 | [diff] [blame] | 4539 | memcpy(&net->ro._l_addr, newaddr, sizeof(struct sockaddr_conn)); |
| 4540 | #endif |
| 4541 | ((struct sockaddr_conn *)&net->ro._l_addr)->sconn_port = stcb->rport; |
| 4542 | break; |
| 4543 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4544 | default: |
| 4545 | break; |
| 4546 | } |
| 4547 | net->addr_is_local = sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id); |
| 4548 | if (net->addr_is_local && ((set_scope || (from == SCTP_ADDR_IS_CONFIRMED)))) { |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 4549 | stcb->asoc.scope.loopback_scope = 1; |
| 4550 | stcb->asoc.scope.ipv4_local_scope = 1; |
| 4551 | stcb->asoc.scope.local_scope = 0; |
| 4552 | stcb->asoc.scope.site_scope = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4553 | addr_inscope = 1; |
| 4554 | } |
| 4555 | net->failure_threshold = stcb->asoc.def_net_failure; |
| 4556 | net->pf_threshold = stcb->asoc.def_net_pf_threshold; |
| 4557 | if (addr_inscope == 0) { |
| 4558 | net->dest_state = (SCTP_ADDR_REACHABLE | |
| 4559 | SCTP_ADDR_OUT_OF_SCOPE); |
| 4560 | } else { |
| 4561 | if (from == SCTP_ADDR_IS_CONFIRMED) |
| 4562 | /* SCTP_ADDR_IS_CONFIRMED is passed by connect_x */ |
| 4563 | net->dest_state = SCTP_ADDR_REACHABLE; |
| 4564 | else |
| 4565 | net->dest_state = SCTP_ADDR_REACHABLE | |
| 4566 | SCTP_ADDR_UNCONFIRMED; |
| 4567 | } |
| 4568 | /* We set this to 0, the timer code knows that |
| 4569 | * this means its an initial value |
| 4570 | */ |
| 4571 | net->rto_needed = 1; |
t00fcxen | 8dd95b8 | 2014-07-11 20:41:28 +0000 | [diff] [blame] | 4572 | net->RTO = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4573 | net->RTO_measured = 0; |
| 4574 | stcb->asoc.numnets++; |
tuexen | 9d7b207 | 2011-11-20 16:35:17 +0000 | [diff] [blame] | 4575 | net->ref_count = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4576 | 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] | 4577 | net->port = port; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4578 | net->dscp = stcb->asoc.default_dscp; |
| 4579 | #ifdef INET6 |
| 4580 | net->flowlabel = stcb->asoc.default_flowlabel; |
| 4581 | #endif |
| 4582 | if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) { |
| 4583 | net->dest_state |= SCTP_ADDR_NOHB; |
| 4584 | } else { |
| 4585 | net->dest_state &= ~SCTP_ADDR_NOHB; |
| 4586 | } |
| 4587 | if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD)) { |
| 4588 | net->dest_state |= SCTP_ADDR_NO_PMTUD; |
| 4589 | } else { |
| 4590 | net->dest_state &= ~SCTP_ADDR_NO_PMTUD; |
| 4591 | } |
| 4592 | net->heart_beat_delay = stcb->asoc.heart_beat_delay; |
| 4593 | /* Init the timer structure */ |
| 4594 | SCTP_OS_TIMER_INIT(&net->rxt_timer.timer); |
| 4595 | SCTP_OS_TIMER_INIT(&net->pmtu_timer.timer); |
| 4596 | SCTP_OS_TIMER_INIT(&net->hb_timer.timer); |
| 4597 | |
| 4598 | /* Now generate a route for this guy */ |
| 4599 | #ifdef INET6 |
| 4600 | #ifdef SCTP_EMBEDDED_V6_SCOPE |
| 4601 | /* KAME hack: embed scopeid */ |
| 4602 | if (newaddr->sa_family == AF_INET6) { |
| 4603 | struct sockaddr_in6 *sin6; |
| 4604 | |
| 4605 | sin6 = (struct sockaddr_in6 *)&net->ro._l_addr; |
tuexen | e385159 | 2012-06-01 08:31:45 +0000 | [diff] [blame] | 4606 | #if defined(__APPLE__) |
tuexen | 9a218b1 | 2012-08-04 21:17:58 +0000 | [diff] [blame] | 4607 | #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) |
| 4608 | (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] | 4609 | #else |
tuexen | 9a218b1 | 2012-08-04 21:17:58 +0000 | [diff] [blame] | 4610 | (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] | 4611 | #endif |
| 4612 | #elif defined(SCTP_KAME) |
| 4613 | (void)sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)); |
| 4614 | #else |
| 4615 | (void)in6_embedscope(&sin6->sin6_addr, sin6); |
| 4616 | #endif |
| 4617 | #ifndef SCOPEDROUTING |
| 4618 | sin6->sin6_scope_id = 0; |
| 4619 | #endif |
| 4620 | } |
| 4621 | #endif /* SCTP_EMBEDDED_V6_SCOPE */ |
| 4622 | #endif |
t00fcxen | a51e446 | 2015-06-17 15:53:23 +0000 | [diff] [blame] | 4623 | SCTP_RTALLOC((sctp_route_t *)&net->ro, |
| 4624 | stcb->asoc.vrf_id, |
| 4625 | stcb->sctp_ep->fibnum); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4626 | |
t00fcxen | 8b6ef09 | 2014-07-11 17:37:40 +0000 | [diff] [blame] | 4627 | net->src_addr_selected = 0; |
Michael Tuexen | d900e5f | 2017-06-23 10:52:43 +0200 | [diff] [blame] | 4628 | #if !defined(__Userspace__) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4629 | if (SCTP_ROUTE_HAS_VALID_IFN(&net->ro)) { |
| 4630 | /* Get source address */ |
| 4631 | net->ro._s_addr = sctp_source_address_selection(stcb->sctp_ep, |
Michael Tuexen | d900e5f | 2017-06-23 10:52:43 +0200 | [diff] [blame] | 4632 | stcb, |
| 4633 | (sctp_route_t *)&net->ro, |
| 4634 | net, |
| 4635 | 0, |
| 4636 | stcb->asoc.vrf_id); |
Michael Tuexen | de0630d | 2017-11-03 22:04:01 +0100 | [diff] [blame] | 4637 | if (stcb->asoc.default_mtu > 0) { |
| 4638 | net->mtu = stcb->asoc.default_mtu; |
| 4639 | switch (net->ro._l_addr.sa.sa_family) { |
| 4640 | #ifdef INET |
| 4641 | case AF_INET: |
| 4642 | net->mtu += SCTP_MIN_V4_OVERHEAD; |
| 4643 | break; |
| 4644 | #endif |
| 4645 | #ifdef INET6 |
| 4646 | case AF_INET6: |
| 4647 | net->mtu += SCTP_MIN_OVERHEAD; |
| 4648 | break; |
| 4649 | #endif |
| 4650 | #if defined(__Userspace__) |
| 4651 | case AF_CONN: |
| 4652 | net->mtu += sizeof(struct sctphdr); |
| 4653 | break; |
| 4654 | #endif |
| 4655 | default: |
| 4656 | break; |
| 4657 | } |
| 4658 | #if defined(INET) || defined(INET6) |
| 4659 | if (net->port) { |
| 4660 | net->mtu += (uint32_t)sizeof(struct udphdr); |
| 4661 | } |
| 4662 | #endif |
| 4663 | } else if (net->ro._s_addr != NULL) { |
Michael Tuexen | d900e5f | 2017-06-23 10:52:43 +0200 | [diff] [blame] | 4664 | uint32_t imtu, rmtu, hcmtu; |
| 4665 | |
t00fcxen | 8b6ef09 | 2014-07-11 17:37:40 +0000 | [diff] [blame] | 4666 | net->src_addr_selected = 1; |
| 4667 | /* Now get the interface MTU */ |
| 4668 | if (net->ro._s_addr->ifn_p != NULL) { |
Michael Tuexen | d900e5f | 2017-06-23 10:52:43 +0200 | [diff] [blame] | 4669 | imtu = SCTP_GATHER_MTU_FROM_INTFC(net->ro._s_addr->ifn_p); |
| 4670 | } else { |
| 4671 | imtu = 0; |
t00fcxen | 8b6ef09 | 2014-07-11 17:37:40 +0000 | [diff] [blame] | 4672 | } |
Michael Tuexen | d900e5f | 2017-06-23 10:52:43 +0200 | [diff] [blame] | 4673 | #if defined(__FreeBSD__) |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4674 | 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] | 4675 | hcmtu = sctp_hc_get_mtu(&net->ro._l_addr, stcb->sctp_ep->fibnum); |
| 4676 | #else |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4677 | 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] | 4678 | hcmtu = 0; |
| 4679 | #endif |
| 4680 | net->mtu = sctp_min_mtu(hcmtu, rmtu, imtu); |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4681 | #if defined(__FreeBSD__) |
| 4682 | #else |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4683 | if (rmtu == 0) { |
| 4684 | /* Start things off to match mtu of interface please. */ |
| 4685 | SCTP_SET_MTU_OF_ROUTE(&net->ro._l_addr.sa, |
Michael Tuexen | d900e5f | 2017-06-23 10:52:43 +0200 | [diff] [blame] | 4686 | net->ro.ro_rt, net->mtu); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4687 | } |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4688 | #endif |
t00fcxen | 8b6ef09 | 2014-07-11 17:37:40 +0000 | [diff] [blame] | 4689 | } |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 4690 | } |
t00fcxen | 6d60eb0 | 2013-08-30 07:37:01 +0000 | [diff] [blame] | 4691 | #endif |
tuexen | e81177b | 2011-11-15 20:48:08 +0000 | [diff] [blame] | 4692 | if (net->mtu == 0) { |
Michael Tuexen | de0630d | 2017-11-03 22:04:01 +0100 | [diff] [blame] | 4693 | if (stcb->asoc.default_mtu > 0) { |
| 4694 | net->mtu = stcb->asoc.default_mtu; |
| 4695 | switch (net->ro._l_addr.sa.sa_family) { |
tuexen | e81177b | 2011-11-15 20:48:08 +0000 | [diff] [blame] | 4696 | #ifdef INET |
Michael Tuexen | de0630d | 2017-11-03 22:04:01 +0100 | [diff] [blame] | 4697 | case AF_INET: |
| 4698 | net->mtu += SCTP_MIN_V4_OVERHEAD; |
| 4699 | break; |
tuexen | e81177b | 2011-11-15 20:48:08 +0000 | [diff] [blame] | 4700 | #endif |
| 4701 | #ifdef INET6 |
Michael Tuexen | de0630d | 2017-11-03 22:04:01 +0100 | [diff] [blame] | 4702 | case AF_INET6: |
| 4703 | net->mtu += SCTP_MIN_OVERHEAD; |
| 4704 | break; |
tuexen | e81177b | 2011-11-15 20:48:08 +0000 | [diff] [blame] | 4705 | #endif |
tuexen | cc2b142 | 2012-07-10 21:50:32 +0000 | [diff] [blame] | 4706 | #if defined(__Userspace__) |
Michael Tuexen | de0630d | 2017-11-03 22:04:01 +0100 | [diff] [blame] | 4707 | case AF_CONN: |
| 4708 | net->mtu += sizeof(struct sctphdr); |
| 4709 | break; |
tuexen | cc2b142 | 2012-07-10 21:50:32 +0000 | [diff] [blame] | 4710 | #endif |
Michael Tuexen | de0630d | 2017-11-03 22:04:01 +0100 | [diff] [blame] | 4711 | default: |
| 4712 | break; |
| 4713 | } |
| 4714 | #if defined(INET) || defined(INET6) |
| 4715 | if (net->port) { |
| 4716 | net->mtu += (uint32_t)sizeof(struct udphdr); |
| 4717 | } |
| 4718 | #endif |
| 4719 | } else { |
| 4720 | switch (newaddr->sa_family) { |
| 4721 | #ifdef INET |
| 4722 | case AF_INET: |
| 4723 | net->mtu = SCTP_DEFAULT_MTU; |
| 4724 | break; |
| 4725 | #endif |
| 4726 | #ifdef INET6 |
| 4727 | case AF_INET6: |
| 4728 | net->mtu = 1280; |
| 4729 | break; |
| 4730 | #endif |
| 4731 | #if defined(__Userspace__) |
| 4732 | case AF_CONN: |
| 4733 | net->mtu = 1280; |
| 4734 | break; |
| 4735 | #endif |
| 4736 | default: |
| 4737 | break; |
| 4738 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4739 | } |
tuexen | e81177b | 2011-11-15 20:48:08 +0000 | [diff] [blame] | 4740 | } |
t00fcxen | 1ce83bb | 2014-07-11 07:17:36 +0000 | [diff] [blame] | 4741 | #if defined(INET) || defined(INET6) |
tuexen | e81177b | 2011-11-15 20:48:08 +0000 | [diff] [blame] | 4742 | if (net->port) { |
| 4743 | net->mtu -= (uint32_t)sizeof(struct udphdr); |
| 4744 | } |
t00fcxen | 5ab37c8 | 2014-06-30 20:55:44 +0000 | [diff] [blame] | 4745 | #endif |
tuexen | e81177b | 2011-11-15 20:48:08 +0000 | [diff] [blame] | 4746 | if (from == SCTP_ALLOC_ASOC) { |
| 4747 | stcb->asoc.smallest_mtu = net->mtu; |
| 4748 | } |
| 4749 | if (stcb->asoc.smallest_mtu > net->mtu) { |
Michael Tuexen | 923f735 | 2016-05-01 23:55:35 +0200 | [diff] [blame] | 4750 | sctp_pathmtu_adjustment(stcb, net->mtu); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4751 | } |
| 4752 | #ifdef INET6 |
| 4753 | #ifdef SCTP_EMBEDDED_V6_SCOPE |
| 4754 | if (newaddr->sa_family == AF_INET6) { |
| 4755 | struct sockaddr_in6 *sin6; |
| 4756 | |
| 4757 | sin6 = (struct sockaddr_in6 *)&net->ro._l_addr; |
| 4758 | #ifdef SCTP_KAME |
| 4759 | (void)sa6_recoverscope(sin6); |
| 4760 | #else |
| 4761 | (void)in6_recoverscope(sin6, &sin6->sin6_addr, NULL); |
| 4762 | #endif /* SCTP_KAME */ |
| 4763 | } |
| 4764 | #endif /* SCTP_EMBEDDED_V6_SCOPE */ |
| 4765 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4766 | |
| 4767 | /* JRS - Use the congestion control given in the CC module */ |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 4768 | if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4769 | (*stcb->asoc.cc_functions.sctp_set_initial_cc_param)(stcb, net); |
| 4770 | |
| 4771 | /* |
| 4772 | * CMT: CUC algo - set find_pseudo_cumack to TRUE (1) at beginning |
| 4773 | * of assoc (2005/06/27, iyengar@cis.udel.edu) |
| 4774 | */ |
| 4775 | net->find_pseudo_cumack = 1; |
| 4776 | net->find_rtx_pseudo_cumack = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4777 | #if defined(__FreeBSD__) |
| 4778 | /* Choose an initial flowid. */ |
| 4779 | net->flowid = stcb->asoc.my_vtag ^ |
| 4780 | ntohs(stcb->rport) ^ |
| 4781 | ntohs(stcb->sctp_ep->sctp_lport); |
Michael Tuexen | 15f35f8 | 2016-06-07 09:19:50 +0200 | [diff] [blame] | 4782 | net->flowtype = M_HASHTYPE_OPAQUE_HASH; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4783 | #endif |
| 4784 | if (netp) { |
| 4785 | *netp = net; |
| 4786 | } |
| 4787 | netfirst = TAILQ_FIRST(&stcb->asoc.nets); |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4788 | #if defined(__FreeBSD__) |
| 4789 | if (net->ro.ro_nh == NULL) { |
| 4790 | #else |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4791 | if (net->ro.ro_rt == NULL) { |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4792 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4793 | /* Since we have no route put it at the back */ |
| 4794 | TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next); |
| 4795 | } else if (netfirst == NULL) { |
| 4796 | /* We are the first one in the pool. */ |
| 4797 | TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next); |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4798 | #if defined(__FreeBSD__) |
| 4799 | } else if (netfirst->ro.ro_nh == NULL) { |
| 4800 | #else |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4801 | } else if (netfirst->ro.ro_rt == NULL) { |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4802 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4803 | /* |
| 4804 | * First one has NO route. Place this one ahead of the first |
| 4805 | * one. |
| 4806 | */ |
| 4807 | TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next); |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4808 | #if defined(__FreeBSD__) |
| 4809 | } else if (net->ro.ro_nh->nh_ifp != netfirst->ro.ro_nh->nh_ifp) { |
| 4810 | #else |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4811 | } 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] | 4812 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4813 | /* |
| 4814 | * This one has a different interface than the one at the |
| 4815 | * top of the list. Place it ahead. |
| 4816 | */ |
| 4817 | TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4818 | } else { |
| 4819 | /* |
| 4820 | * Ok we have the same interface as the first one. Move |
| 4821 | * forward until we find either a) one with a NULL route... |
| 4822 | * insert ahead of that b) one with a different ifp.. insert |
| 4823 | * after that. c) end of the list.. insert at the tail. |
| 4824 | */ |
| 4825 | struct sctp_nets *netlook; |
| 4826 | |
| 4827 | do { |
| 4828 | netlook = TAILQ_NEXT(netfirst, sctp_next); |
| 4829 | if (netlook == NULL) { |
| 4830 | /* End of the list */ |
| 4831 | TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next); |
| 4832 | break; |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4833 | #if defined(__FreeBSD__) |
| 4834 | } else if (netlook->ro.ro_nh == NULL) { |
| 4835 | #else |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4836 | } else if (netlook->ro.ro_rt == NULL) { |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4837 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4838 | /* next one has NO route */ |
| 4839 | TAILQ_INSERT_BEFORE(netfirst, net, sctp_next); |
| 4840 | break; |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4841 | #if defined(__FreeBSD__) |
| 4842 | } 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] | 4843 | #else |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4844 | } 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] | 4845 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4846 | TAILQ_INSERT_AFTER(&stcb->asoc.nets, netlook, |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4847 | net, sctp_next); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4848 | break; |
| 4849 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4850 | /* Shift forward */ |
| 4851 | netfirst = netlook; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4852 | } while (netlook != NULL); |
| 4853 | } |
| 4854 | |
| 4855 | /* got to have a primary set */ |
| 4856 | if (stcb->asoc.primary_destination == 0) { |
| 4857 | stcb->asoc.primary_destination = net; |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4858 | #if defined(__FreeBSD__) |
| 4859 | } else if ((stcb->asoc.primary_destination->ro.ro_nh == NULL) && |
| 4860 | (net->ro.ro_nh) && |
| 4861 | #else |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4862 | } else if ((stcb->asoc.primary_destination->ro.ro_rt == NULL) && |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 4863 | (net->ro.ro_rt) && |
| 4864 | #endif |
| 4865 | ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4866 | /* No route to current primary adopt new primary */ |
| 4867 | stcb->asoc.primary_destination = net; |
| 4868 | } |
| 4869 | /* Validate primary is first */ |
| 4870 | net = TAILQ_FIRST(&stcb->asoc.nets); |
| 4871 | if ((net != stcb->asoc.primary_destination) && |
| 4872 | (stcb->asoc.primary_destination)) { |
| 4873 | /* first one on the list is NOT the primary |
| 4874 | * sctp_cmpaddr() is much more efficient if |
| 4875 | * the primary is the first on the list, make it |
| 4876 | * so. |
| 4877 | */ |
| 4878 | TAILQ_REMOVE(&stcb->asoc.nets, |
| 4879 | stcb->asoc.primary_destination, sctp_next); |
| 4880 | TAILQ_INSERT_HEAD(&stcb->asoc.nets, |
| 4881 | stcb->asoc.primary_destination, sctp_next); |
| 4882 | } |
| 4883 | return (0); |
| 4884 | } |
| 4885 | |
| 4886 | |
| 4887 | static uint32_t |
| 4888 | sctp_aloc_a_assoc_id(struct sctp_inpcb *inp, struct sctp_tcb *stcb) |
| 4889 | { |
| 4890 | uint32_t id; |
| 4891 | struct sctpasochead *head; |
| 4892 | struct sctp_tcb *lstcb; |
| 4893 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4894 | try_again: |
| 4895 | if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
| 4896 | /* TSNH */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4897 | return (0); |
| 4898 | } |
| 4899 | /* |
| 4900 | * We don't allow assoc id to be one of SCTP_FUTURE_ASSOC, |
| 4901 | * SCTP_CURRENT_ASSOC and SCTP_ALL_ASSOC. |
| 4902 | */ |
| 4903 | if (inp->sctp_associd_counter <= SCTP_ALL_ASSOC) { |
| 4904 | inp->sctp_associd_counter = SCTP_ALL_ASSOC + 1; |
| 4905 | } |
| 4906 | id = inp->sctp_associd_counter; |
| 4907 | inp->sctp_associd_counter++; |
| 4908 | lstcb = sctp_findasoc_ep_asocid_locked(inp, (sctp_assoc_t)id, 0); |
| 4909 | if (lstcb) { |
| 4910 | goto try_again; |
| 4911 | } |
| 4912 | head = &inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(id, inp->hashasocidmark)]; |
| 4913 | LIST_INSERT_HEAD(head, stcb, sctp_tcbasocidhash); |
| 4914 | stcb->asoc.in_asocid_hash = 1; |
Michael Tuexen | be8e0eb | 2019-03-24 00:11:02 +0100 | [diff] [blame] | 4915 | return (id); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4916 | } |
| 4917 | |
| 4918 | /* |
| 4919 | * allocate an association and add it to the endpoint. The caller must be |
| 4920 | * careful to add all additional addresses once they are know right away or |
| 4921 | * else the assoc will be may experience a blackout scenario. |
| 4922 | */ |
| 4923 | struct sctp_tcb * |
| 4924 | sctp_aloc_assoc(struct sctp_inpcb *inp, struct sockaddr *firstaddr, |
Michael Tuexen | 49335e1 | 2015-12-06 17:31:45 +0100 | [diff] [blame] | 4925 | int *error, uint32_t override_tag, uint32_t vrf_id, |
Michael Tuexen | 923f735 | 2016-05-01 23:55:35 +0200 | [diff] [blame] | 4926 | uint16_t o_streams, uint16_t port, |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4927 | #if defined(__FreeBSD__) && __FreeBSD_version >= 500000 |
Michael Tuexen | bfb04f3 | 2019-07-14 14:42:17 +0200 | [diff] [blame] | 4928 | struct thread *p, |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4929 | #elif defined(__Windows__) |
Michael Tuexen | bfb04f3 | 2019-07-14 14:42:17 +0200 | [diff] [blame] | 4930 | PKTHREAD p, |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4931 | #else |
| 4932 | #if defined(__Userspace__) |
| 4933 | /* __Userspace__ NULL proc is going to be passed here. See sctp_lower_sosend */ |
| 4934 | #endif |
Michael Tuexen | bfb04f3 | 2019-07-14 14:42:17 +0200 | [diff] [blame] | 4935 | struct proc *p, |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4936 | #endif |
Michael Tuexen | bfb04f3 | 2019-07-14 14:42:17 +0200 | [diff] [blame] | 4937 | int initialize_auth_params) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 4938 | { |
| 4939 | /* note the p argument is only valid in unbound sockets */ |
| 4940 | |
| 4941 | struct sctp_tcb *stcb; |
| 4942 | struct sctp_association *asoc; |
| 4943 | struct sctpasochead *head; |
| 4944 | uint16_t rport; |
| 4945 | int err; |
| 4946 | |
| 4947 | /* |
| 4948 | * Assumption made here: Caller has done a |
| 4949 | * sctp_findassociation_ep_addr(ep, addr's); to make sure the |
| 4950 | * address does not exist already. |
| 4951 | */ |
| 4952 | if (SCTP_BASE_INFO(ipi_count_asoc) >= SCTP_MAX_NUM_OF_ASOC) { |
| 4953 | /* Hit max assoc, sorry no more */ |
| 4954 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS); |
| 4955 | *error = ENOBUFS; |
| 4956 | return (NULL); |
| 4957 | } |
| 4958 | if (firstaddr == NULL) { |
| 4959 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 4960 | *error = EINVAL; |
| 4961 | return (NULL); |
| 4962 | } |
| 4963 | SCTP_INP_RLOCK(inp); |
| 4964 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) && |
| 4965 | ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) || |
| 4966 | (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) { |
| 4967 | /* |
| 4968 | * If its in the TCP pool, its NOT allowed to create an |
| 4969 | * association. The parent listener needs to call |
| 4970 | * sctp_aloc_assoc.. or the one-2-many socket. If a peeled |
| 4971 | * off, or connected one does this.. its an error. |
| 4972 | */ |
| 4973 | SCTP_INP_RUNLOCK(inp); |
| 4974 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 4975 | *error = EINVAL; |
| 4976 | return (NULL); |
| 4977 | } |
| 4978 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || |
| 4979 | (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) { |
| 4980 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) || |
| 4981 | (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED)) { |
| 4982 | SCTP_INP_RUNLOCK(inp); |
| 4983 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 4984 | *error = EINVAL; |
| 4985 | return (NULL); |
| 4986 | } |
| 4987 | } |
| 4988 | SCTPDBG(SCTP_DEBUG_PCB3, "Allocate an association for peer:"); |
| 4989 | #ifdef SCTP_DEBUG |
| 4990 | if (firstaddr) { |
| 4991 | SCTPDBG_ADDR(SCTP_DEBUG_PCB3, firstaddr); |
| 4992 | switch (firstaddr->sa_family) { |
| 4993 | #ifdef INET |
| 4994 | case AF_INET: |
| 4995 | SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n", |
| 4996 | ntohs(((struct sockaddr_in *)firstaddr)->sin_port)); |
| 4997 | break; |
| 4998 | #endif |
| 4999 | #ifdef INET6 |
| 5000 | case AF_INET6: |
| 5001 | SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n", |
| 5002 | ntohs(((struct sockaddr_in6 *)firstaddr)->sin6_port)); |
| 5003 | break; |
| 5004 | #endif |
tuexen | cc2b142 | 2012-07-10 21:50:32 +0000 | [diff] [blame] | 5005 | #if defined(__Userspace__) |
| 5006 | case AF_CONN: |
| 5007 | SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n", |
| 5008 | ntohs(((struct sockaddr_conn *)firstaddr)->sconn_port)); |
| 5009 | break; |
| 5010 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5011 | default: |
| 5012 | break; |
| 5013 | } |
| 5014 | } else { |
| 5015 | SCTPDBG(SCTP_DEBUG_PCB3,"None\n"); |
| 5016 | } |
| 5017 | #endif /* SCTP_DEBUG */ |
| 5018 | switch (firstaddr->sa_family) { |
| 5019 | #ifdef INET |
| 5020 | case AF_INET: |
| 5021 | { |
| 5022 | struct sockaddr_in *sin; |
| 5023 | |
| 5024 | sin = (struct sockaddr_in *)firstaddr; |
| 5025 | if ((ntohs(sin->sin_port) == 0) || |
| 5026 | (sin->sin_addr.s_addr == INADDR_ANY) || |
| 5027 | (sin->sin_addr.s_addr == INADDR_BROADCAST) || |
| 5028 | IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) { |
| 5029 | /* Invalid address */ |
| 5030 | SCTP_INP_RUNLOCK(inp); |
| 5031 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 5032 | *error = EINVAL; |
| 5033 | return (NULL); |
| 5034 | } |
| 5035 | rport = sin->sin_port; |
| 5036 | break; |
| 5037 | } |
| 5038 | #endif |
| 5039 | #ifdef INET6 |
| 5040 | case AF_INET6: |
| 5041 | { |
| 5042 | struct sockaddr_in6 *sin6; |
| 5043 | |
| 5044 | sin6 = (struct sockaddr_in6 *)firstaddr; |
| 5045 | if ((ntohs(sin6->sin6_port) == 0) || |
| 5046 | IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) || |
| 5047 | IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { |
| 5048 | /* Invalid address */ |
| 5049 | SCTP_INP_RUNLOCK(inp); |
| 5050 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 5051 | *error = EINVAL; |
| 5052 | return (NULL); |
| 5053 | } |
| 5054 | rport = sin6->sin6_port; |
| 5055 | break; |
| 5056 | } |
| 5057 | #endif |
tuexen | cc2b142 | 2012-07-10 21:50:32 +0000 | [diff] [blame] | 5058 | #if defined(__Userspace__) |
| 5059 | case AF_CONN: |
| 5060 | { |
| 5061 | struct sockaddr_conn *sconn; |
| 5062 | |
| 5063 | sconn = (struct sockaddr_conn *)firstaddr; |
| 5064 | if ((ntohs(sconn->sconn_port) == 0) || |
| 5065 | (sconn->sconn_addr == NULL)) { |
| 5066 | /* Invalid address */ |
| 5067 | SCTP_INP_RUNLOCK(inp); |
| 5068 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 5069 | *error = EINVAL; |
| 5070 | return (NULL); |
| 5071 | } |
| 5072 | rport = sconn->sconn_port; |
| 5073 | break; |
| 5074 | } |
| 5075 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5076 | default: |
| 5077 | /* not supported family type */ |
| 5078 | SCTP_INP_RUNLOCK(inp); |
| 5079 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 5080 | *error = EINVAL; |
| 5081 | return (NULL); |
| 5082 | } |
| 5083 | SCTP_INP_RUNLOCK(inp); |
| 5084 | if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) { |
| 5085 | /* |
| 5086 | * If you have not performed a bind, then we need to do the |
| 5087 | * ephemeral bind for you. |
| 5088 | */ |
| 5089 | if ((err = sctp_inpcb_bind(inp->sctp_socket, |
| 5090 | (struct sockaddr *)NULL, |
| 5091 | (struct sctp_ifa *)NULL, |
| 5092 | #ifndef __Panda__ |
| 5093 | p |
| 5094 | #else |
| 5095 | (struct proc *)NULL |
| 5096 | #endif |
| 5097 | ))) { |
| 5098 | /* bind error, probably perm */ |
| 5099 | *error = err; |
| 5100 | return (NULL); |
| 5101 | } |
| 5102 | } |
| 5103 | stcb = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_asoc), struct sctp_tcb); |
| 5104 | if (stcb == NULL) { |
| 5105 | /* out of memory? */ |
| 5106 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM); |
| 5107 | *error = ENOMEM; |
| 5108 | return (NULL); |
| 5109 | } |
| 5110 | SCTP_INCR_ASOC_COUNT(); |
| 5111 | |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 5112 | memset(stcb, 0, sizeof(*stcb)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5113 | asoc = &stcb->asoc; |
| 5114 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5115 | SCTP_TCB_LOCK_INIT(stcb); |
| 5116 | SCTP_TCB_SEND_LOCK_INIT(stcb); |
| 5117 | stcb->rport = rport; |
| 5118 | /* setup back pointer's */ |
| 5119 | stcb->sctp_ep = inp; |
| 5120 | stcb->sctp_socket = inp->sctp_socket; |
Michael Tuexen | 49335e1 | 2015-12-06 17:31:45 +0100 | [diff] [blame] | 5121 | if ((err = sctp_init_asoc(inp, stcb, override_tag, vrf_id, o_streams))) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5122 | /* failed */ |
| 5123 | SCTP_TCB_LOCK_DESTROY(stcb); |
| 5124 | SCTP_TCB_SEND_LOCK_DESTROY(stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5125 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb); |
| 5126 | SCTP_DECR_ASOC_COUNT(); |
| 5127 | *error = err; |
| 5128 | return (NULL); |
| 5129 | } |
| 5130 | /* and the port */ |
| 5131 | SCTP_INP_INFO_WLOCK(); |
| 5132 | SCTP_INP_WLOCK(inp); |
| 5133 | if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_SOCKET_ALLGONE)) { |
| 5134 | /* inpcb freed while alloc going on */ |
| 5135 | SCTP_TCB_LOCK_DESTROY(stcb); |
| 5136 | SCTP_TCB_SEND_LOCK_DESTROY(stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5137 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb); |
| 5138 | SCTP_INP_WUNLOCK(inp); |
| 5139 | SCTP_INP_INFO_WUNLOCK(); |
| 5140 | SCTP_DECR_ASOC_COUNT(); |
| 5141 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
| 5142 | *error = EINVAL; |
| 5143 | return (NULL); |
| 5144 | } |
| 5145 | SCTP_TCB_LOCK(stcb); |
| 5146 | |
Michael Tuexen | be8e0eb | 2019-03-24 00:11:02 +0100 | [diff] [blame] | 5147 | asoc->assoc_id = sctp_aloc_a_assoc_id(inp, stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5148 | /* now that my_vtag is set, add it to the hash */ |
| 5149 | head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))]; |
| 5150 | /* put it in the bucket in the vtag hash of assoc's for the system */ |
| 5151 | LIST_INSERT_HEAD(head, stcb, sctp_asocs); |
| 5152 | SCTP_INP_INFO_WUNLOCK(); |
| 5153 | |
Michael Tuexen | 923f735 | 2016-05-01 23:55:35 +0200 | [diff] [blame] | 5154 | 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] | 5155 | /* failure.. memory error? */ |
| 5156 | if (asoc->strmout) { |
| 5157 | SCTP_FREE(asoc->strmout, SCTP_M_STRMO); |
| 5158 | asoc->strmout = NULL; |
| 5159 | } |
| 5160 | if (asoc->mapping_array) { |
| 5161 | SCTP_FREE(asoc->mapping_array, SCTP_M_MAP); |
| 5162 | asoc->mapping_array = NULL; |
| 5163 | } |
| 5164 | if (asoc->nr_mapping_array) { |
| 5165 | SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP); |
| 5166 | asoc->nr_mapping_array = NULL; |
| 5167 | } |
| 5168 | SCTP_DECR_ASOC_COUNT(); |
t00fcxen | 2c2ff92 | 2013-11-03 14:03:35 +0000 | [diff] [blame] | 5169 | SCTP_TCB_UNLOCK(stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5170 | SCTP_TCB_LOCK_DESTROY(stcb); |
| 5171 | SCTP_TCB_SEND_LOCK_DESTROY(stcb); |
| 5172 | LIST_REMOVE(stcb, sctp_tcbasocidhash); |
| 5173 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb); |
| 5174 | SCTP_INP_WUNLOCK(inp); |
| 5175 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS); |
| 5176 | *error = ENOBUFS; |
| 5177 | return (NULL); |
| 5178 | } |
| 5179 | /* Init all the timers */ |
| 5180 | SCTP_OS_TIMER_INIT(&asoc->dack_timer.timer); |
| 5181 | SCTP_OS_TIMER_INIT(&asoc->strreset_timer.timer); |
| 5182 | SCTP_OS_TIMER_INIT(&asoc->asconf_timer.timer); |
| 5183 | SCTP_OS_TIMER_INIT(&asoc->shut_guard_timer.timer); |
| 5184 | SCTP_OS_TIMER_INIT(&asoc->autoclose_timer.timer); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5185 | SCTP_OS_TIMER_INIT(&asoc->delete_prim_timer.timer); |
| 5186 | |
| 5187 | LIST_INSERT_HEAD(&inp->sctp_asoc_list, stcb, sctp_tcblist); |
| 5188 | /* now file the port under the hash as well */ |
| 5189 | if (inp->sctp_tcbhash != NULL) { |
| 5190 | head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(stcb->rport, |
| 5191 | inp->sctp_hashmark)]; |
| 5192 | LIST_INSERT_HEAD(head, stcb, sctp_tcbhash); |
| 5193 | } |
Michael Tuexen | bfb04f3 | 2019-07-14 14:42:17 +0200 | [diff] [blame] | 5194 | if (initialize_auth_params == SCTP_INITIALIZE_AUTH_PARAMS) { |
| 5195 | sctp_initialize_auth_params(inp, stcb); |
| 5196 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5197 | SCTP_INP_WUNLOCK(inp); |
t00fcxen | 8fca7ff | 2012-09-05 18:58:42 +0000 | [diff] [blame] | 5198 | SCTPDBG(SCTP_DEBUG_PCB1, "Association %p now allocated\n", (void *)stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5199 | return (stcb); |
| 5200 | } |
| 5201 | |
| 5202 | |
| 5203 | void |
| 5204 | sctp_remove_net(struct sctp_tcb *stcb, struct sctp_nets *net) |
| 5205 | { |
Michael Tuexen | 21b55df | 2020-02-09 23:46:32 +0100 | [diff] [blame] | 5206 | struct sctp_inpcb *inp; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5207 | struct sctp_association *asoc; |
| 5208 | |
Michael Tuexen | 21b55df | 2020-02-09 23:46:32 +0100 | [diff] [blame] | 5209 | inp = stcb->sctp_ep; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5210 | asoc = &stcb->asoc; |
| 5211 | asoc->numnets--; |
| 5212 | TAILQ_REMOVE(&asoc->nets, net, sctp_next); |
| 5213 | if (net == asoc->primary_destination) { |
| 5214 | /* Reset primary */ |
| 5215 | struct sctp_nets *lnet; |
| 5216 | |
| 5217 | lnet = TAILQ_FIRST(&asoc->nets); |
| 5218 | /* Mobility adaptation |
| 5219 | Ideally, if deleted destination is the primary, it becomes |
| 5220 | a fast retransmission trigger by the subsequent SET PRIMARY. |
| 5221 | (by micchie) |
| 5222 | */ |
| 5223 | if (sctp_is_mobility_feature_on(stcb->sctp_ep, |
t00fcxen | 8dd95b8 | 2014-07-11 20:41:28 +0000 | [diff] [blame] | 5224 | SCTP_MOBILITY_BASE) || |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5225 | sctp_is_mobility_feature_on(stcb->sctp_ep, |
t00fcxen | 8dd95b8 | 2014-07-11 20:41:28 +0000 | [diff] [blame] | 5226 | SCTP_MOBILITY_FASTHANDOFF)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5227 | SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: primary dst is deleting\n"); |
| 5228 | if (asoc->deleted_primary != NULL) { |
| 5229 | SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: deleted primary may be already stored\n"); |
| 5230 | goto out; |
| 5231 | } |
| 5232 | asoc->deleted_primary = net; |
| 5233 | atomic_add_int(&net->ref_count, 1); |
| 5234 | memset(&net->lastsa, 0, sizeof(net->lastsa)); |
| 5235 | memset(&net->lastsv, 0, sizeof(net->lastsv)); |
| 5236 | sctp_mobility_feature_on(stcb->sctp_ep, |
| 5237 | SCTP_MOBILITY_PRIM_DELETED); |
| 5238 | sctp_timer_start(SCTP_TIMER_TYPE_PRIM_DELETED, |
| 5239 | stcb->sctp_ep, stcb, NULL); |
| 5240 | } |
| 5241 | out: |
| 5242 | /* Try to find a confirmed primary */ |
| 5243 | asoc->primary_destination = sctp_find_alternate_net(stcb, lnet, 0); |
| 5244 | } |
| 5245 | if (net == asoc->last_data_chunk_from) { |
| 5246 | /* Reset primary */ |
| 5247 | asoc->last_data_chunk_from = TAILQ_FIRST(&asoc->nets); |
| 5248 | } |
| 5249 | if (net == asoc->last_control_chunk_from) { |
| 5250 | /* Clear net */ |
| 5251 | asoc->last_control_chunk_from = NULL; |
| 5252 | } |
| 5253 | if (net == stcb->asoc.alternate) { |
| 5254 | sctp_free_remote_addr(stcb->asoc.alternate); |
| 5255 | stcb->asoc.alternate = NULL; |
| 5256 | } |
Michael Tuexen | 21b55df | 2020-02-09 23:46:32 +0100 | [diff] [blame] | 5257 | sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net, |
| 5258 | SCTP_FROM_SCTP_PCB + SCTP_LOC_9); |
| 5259 | sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net, |
| 5260 | SCTP_FROM_SCTP_PCB + SCTP_LOC_10); |
Michael Tuexen | f52d3df | 2020-02-11 19:24:34 +0100 | [diff] [blame] | 5261 | net->dest_state |= SCTP_ADDR_BEING_DELETED; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5262 | sctp_free_remote_addr(net); |
| 5263 | } |
| 5264 | |
| 5265 | /* |
| 5266 | * remove a remote endpoint address from an association, it will fail if the |
| 5267 | * address does not exist. |
| 5268 | */ |
| 5269 | int |
| 5270 | sctp_del_remote_addr(struct sctp_tcb *stcb, struct sockaddr *remaddr) |
| 5271 | { |
| 5272 | /* |
| 5273 | * Here we need to remove a remote address. This is quite simple, we |
| 5274 | * first find it in the list of address for the association |
| 5275 | * (tasoc->asoc.nets) and then if it is there, we do a LIST_REMOVE |
| 5276 | * on that item. Note we do not allow it to be removed if there are |
| 5277 | * no other addresses. |
| 5278 | */ |
| 5279 | struct sctp_association *asoc; |
| 5280 | struct sctp_nets *net, *nnet; |
| 5281 | |
| 5282 | asoc = &stcb->asoc; |
| 5283 | |
| 5284 | /* locate the address */ |
| 5285 | TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) { |
| 5286 | if (net->ro._l_addr.sa.sa_family != remaddr->sa_family) { |
| 5287 | continue; |
| 5288 | } |
| 5289 | if (sctp_cmpaddr((struct sockaddr *)&net->ro._l_addr, |
| 5290 | remaddr)) { |
| 5291 | /* we found the guy */ |
| 5292 | if (asoc->numnets < 2) { |
| 5293 | /* Must have at LEAST two remote addresses */ |
| 5294 | return (-1); |
| 5295 | } else { |
| 5296 | sctp_remove_net(stcb, net); |
| 5297 | return (0); |
| 5298 | } |
| 5299 | } |
| 5300 | } |
| 5301 | /* not found. */ |
| 5302 | return (-2); |
| 5303 | } |
| 5304 | |
| 5305 | void |
| 5306 | sctp_delete_from_timewait(uint32_t tag, uint16_t lport, uint16_t rport) |
| 5307 | { |
| 5308 | struct sctpvtaghead *chain; |
| 5309 | struct sctp_tagblock *twait_block; |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 5310 | int found = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5311 | int i; |
| 5312 | |
| 5313 | chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)]; |
t00fcxen | 14f8fbb | 2013-06-25 09:32:47 +0000 | [diff] [blame] | 5314 | LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) { |
| 5315 | for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) { |
| 5316 | if ((twait_block->vtag_block[i].v_tag == tag) && |
| 5317 | (twait_block->vtag_block[i].lport == lport) && |
| 5318 | (twait_block->vtag_block[i].rport == rport)) { |
| 5319 | twait_block->vtag_block[i].tv_sec_at_expire = 0; |
| 5320 | twait_block->vtag_block[i].v_tag = 0; |
| 5321 | twait_block->vtag_block[i].lport = 0; |
| 5322 | twait_block->vtag_block[i].rport = 0; |
| 5323 | found = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5324 | break; |
t00fcxen | 14f8fbb | 2013-06-25 09:32:47 +0000 | [diff] [blame] | 5325 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5326 | } |
t00fcxen | 14f8fbb | 2013-06-25 09:32:47 +0000 | [diff] [blame] | 5327 | if (found) |
| 5328 | break; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5329 | } |
| 5330 | } |
| 5331 | |
| 5332 | int |
| 5333 | sctp_is_in_timewait(uint32_t tag, uint16_t lport, uint16_t rport) |
| 5334 | { |
| 5335 | struct sctpvtaghead *chain; |
| 5336 | struct sctp_tagblock *twait_block; |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 5337 | int found = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5338 | int i; |
| 5339 | |
| 5340 | SCTP_INP_INFO_WLOCK(); |
| 5341 | chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)]; |
t00fcxen | 14f8fbb | 2013-06-25 09:32:47 +0000 | [diff] [blame] | 5342 | LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) { |
| 5343 | for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) { |
| 5344 | if ((twait_block->vtag_block[i].v_tag == tag) && |
| 5345 | (twait_block->vtag_block[i].lport == lport) && |
| 5346 | (twait_block->vtag_block[i].rport == rport)) { |
| 5347 | found = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5348 | break; |
t00fcxen | 14f8fbb | 2013-06-25 09:32:47 +0000 | [diff] [blame] | 5349 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5350 | } |
t00fcxen | 14f8fbb | 2013-06-25 09:32:47 +0000 | [diff] [blame] | 5351 | if (found) |
| 5352 | break; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5353 | } |
| 5354 | SCTP_INP_INFO_WUNLOCK(); |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 5355 | return (found); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5356 | } |
| 5357 | |
| 5358 | |
| 5359 | void |
| 5360 | sctp_add_vtag_to_timewait(uint32_t tag, uint32_t time, uint16_t lport, uint16_t rport) |
| 5361 | { |
| 5362 | struct sctpvtaghead *chain; |
| 5363 | struct sctp_tagblock *twait_block; |
| 5364 | struct timeval now; |
| 5365 | int set, i; |
| 5366 | |
| 5367 | if (time == 0) { |
| 5368 | /* Its disabled */ |
| 5369 | return; |
| 5370 | } |
| 5371 | (void)SCTP_GETTIME_TIMEVAL(&now); |
| 5372 | chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)]; |
| 5373 | set = 0; |
t00fcxen | 14f8fbb | 2013-06-25 09:32:47 +0000 | [diff] [blame] | 5374 | LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5375 | /* Block(s) present, lets find space, and expire on the fly */ |
t00fcxen | 14f8fbb | 2013-06-25 09:32:47 +0000 | [diff] [blame] | 5376 | for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) { |
| 5377 | if ((twait_block->vtag_block[i].v_tag == 0) && |
| 5378 | !set) { |
| 5379 | twait_block->vtag_block[i].tv_sec_at_expire = |
| 5380 | now.tv_sec + time; |
| 5381 | twait_block->vtag_block[i].v_tag = tag; |
| 5382 | twait_block->vtag_block[i].lport = lport; |
| 5383 | twait_block->vtag_block[i].rport = rport; |
| 5384 | set = 1; |
| 5385 | } else if ((twait_block->vtag_block[i].v_tag) && |
| 5386 | ((long)twait_block->vtag_block[i].tv_sec_at_expire < now.tv_sec)) { |
| 5387 | /* Audit expires this guy */ |
| 5388 | twait_block->vtag_block[i].tv_sec_at_expire = 0; |
| 5389 | twait_block->vtag_block[i].v_tag = 0; |
| 5390 | twait_block->vtag_block[i].lport = 0; |
| 5391 | twait_block->vtag_block[i].rport = 0; |
| 5392 | if (set == 0) { |
| 5393 | /* Reuse it for my new tag */ |
| 5394 | twait_block->vtag_block[i].tv_sec_at_expire = now.tv_sec + time; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5395 | twait_block->vtag_block[i].v_tag = tag; |
| 5396 | twait_block->vtag_block[i].lport = lport; |
| 5397 | twait_block->vtag_block[i].rport = rport; |
| 5398 | set = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5399 | } |
| 5400 | } |
t00fcxen | 14f8fbb | 2013-06-25 09:32:47 +0000 | [diff] [blame] | 5401 | } |
| 5402 | if (set) { |
| 5403 | /* |
| 5404 | * We only do up to the block where we can |
| 5405 | * place our tag for audits |
| 5406 | */ |
| 5407 | break; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5408 | } |
| 5409 | } |
| 5410 | /* Need to add a new block to chain */ |
| 5411 | if (!set) { |
| 5412 | SCTP_MALLOC(twait_block, struct sctp_tagblock *, |
| 5413 | sizeof(struct sctp_tagblock), SCTP_M_TIMW); |
| 5414 | if (twait_block == NULL) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5415 | return; |
| 5416 | } |
| 5417 | memset(twait_block, 0, sizeof(struct sctp_tagblock)); |
| 5418 | LIST_INSERT_HEAD(chain, twait_block, sctp_nxt_tagblock); |
| 5419 | twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec + time; |
| 5420 | twait_block->vtag_block[0].v_tag = tag; |
| 5421 | twait_block->vtag_block[0].lport = lport; |
| 5422 | twait_block->vtag_block[0].rport = rport; |
| 5423 | } |
| 5424 | } |
| 5425 | |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 5426 | void |
| 5427 | sctp_clean_up_stream(struct sctp_tcb *stcb, struct sctp_readhead *rh) |
| 5428 | { |
| 5429 | struct sctp_tmit_chunk *chk, *nchk; |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 5430 | struct sctp_queued_to_read *control, *ncontrol; |
| 5431 | |
| 5432 | TAILQ_FOREACH_SAFE(control, rh, next_instrm, ncontrol) { |
| 5433 | TAILQ_REMOVE(rh, control, next_instrm); |
| 5434 | control->on_strm_q = 0; |
| 5435 | if (control->on_read_q == 0) { |
| 5436 | sctp_free_remote_addr(control->whoFrom); |
| 5437 | if (control->data) { |
| 5438 | sctp_m_freem(control->data); |
| 5439 | control->data = NULL; |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 5440 | } |
| 5441 | } |
| 5442 | /* Reassembly free? */ |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 5443 | TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) { |
| 5444 | TAILQ_REMOVE(&control->reasm, chk, sctp_next); |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 5445 | if (chk->data) { |
| 5446 | sctp_m_freem(chk->data); |
| 5447 | chk->data = NULL; |
| 5448 | } |
| 5449 | if (chk->holds_key_ref) |
| 5450 | sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); |
| 5451 | sctp_free_remote_addr(chk->whoTo); |
| 5452 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); |
| 5453 | SCTP_DECR_CHK_COUNT(); |
| 5454 | /*sa_ignore FREED_MEMORY*/ |
| 5455 | } |
| 5456 | /* |
| 5457 | * We don't free the address here |
| 5458 | * since all the net's were freed |
| 5459 | * above. |
| 5460 | */ |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 5461 | if (control->on_read_q == 0) { |
| 5462 | sctp_free_a_readq(stcb, control); |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 5463 | } |
| 5464 | } |
| 5465 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5466 | |
| 5467 | #ifdef __Panda__ |
| 5468 | void panda_wakeup_socket(struct socket *so); |
| 5469 | #endif |
| 5470 | |
| 5471 | /*- |
| 5472 | * Free the association after un-hashing the remote port. This |
| 5473 | * function ALWAYS returns holding NO LOCK on the stcb. It DOES |
| 5474 | * expect that the input to this function IS a locked TCB. |
| 5475 | * It will return 0, if it did NOT destroy the association (instead |
| 5476 | * it unlocks it. It will return NON-zero if it either destroyed the |
| 5477 | * association OR the association is already destroyed. |
| 5478 | */ |
| 5479 | int |
| 5480 | sctp_free_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int from_inpcbfree, int from_location) |
| 5481 | { |
| 5482 | int i; |
| 5483 | struct sctp_association *asoc; |
| 5484 | struct sctp_nets *net, *nnet; |
| 5485 | struct sctp_laddr *laddr, *naddr; |
| 5486 | struct sctp_tmit_chunk *chk, *nchk; |
| 5487 | struct sctp_asconf_addr *aparam, *naparam; |
| 5488 | struct sctp_asconf_ack *aack, *naack; |
| 5489 | struct sctp_stream_reset_list *strrst, *nstrrst; |
| 5490 | struct sctp_queued_to_read *sq, *nsq; |
| 5491 | struct sctp_stream_queue_pending *sp, *nsp; |
| 5492 | sctp_sharedkey_t *shared_key, *nshared_key; |
| 5493 | struct socket *so; |
| 5494 | |
| 5495 | /* first, lets purge the entry from the hash table. */ |
| 5496 | #if defined(__APPLE__) |
| 5497 | sctp_lock_assert(SCTP_INP_SO(inp)); |
| 5498 | #endif |
| 5499 | |
| 5500 | #ifdef SCTP_LOG_CLOSING |
| 5501 | sctp_log_closing(inp, stcb, 6); |
| 5502 | #endif |
| 5503 | if (stcb->asoc.state == 0) { |
| 5504 | #ifdef SCTP_LOG_CLOSING |
| 5505 | sctp_log_closing(inp, NULL, 7); |
| 5506 | #endif |
| 5507 | /* there is no asoc, really TSNH :-0 */ |
| 5508 | return (1); |
| 5509 | } |
| 5510 | if (stcb->asoc.alternate) { |
| 5511 | sctp_free_remote_addr(stcb->asoc.alternate); |
| 5512 | stcb->asoc.alternate = NULL; |
| 5513 | } |
| 5514 | #if !defined(__APPLE__) /* TEMP: moved to below */ |
| 5515 | /* TEMP CODE */ |
| 5516 | if (stcb->freed_from_where == 0) { |
| 5517 | /* Only record the first place free happened from */ |
| 5518 | stcb->freed_from_where = from_location; |
| 5519 | } |
| 5520 | /* TEMP CODE */ |
| 5521 | #endif |
| 5522 | |
| 5523 | asoc = &stcb->asoc; |
| 5524 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || |
| 5525 | (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) |
| 5526 | /* nothing around */ |
| 5527 | so = NULL; |
| 5528 | else |
| 5529 | so = inp->sctp_socket; |
| 5530 | |
| 5531 | /* |
| 5532 | * We used timer based freeing if a reader or writer is in the way. |
| 5533 | * So we first check if we are actually being called from a timer, |
| 5534 | * if so we abort early if a reader or writer is still in the way. |
| 5535 | */ |
| 5536 | if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) && |
| 5537 | (from_inpcbfree == SCTP_NORMAL_PROC)) { |
| 5538 | /* |
| 5539 | * is it the timer driving us? if so are the reader/writers |
| 5540 | * gone? |
| 5541 | */ |
| 5542 | if (stcb->asoc.refcnt) { |
| 5543 | /* nope, reader or writer in the way */ |
| 5544 | sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL); |
| 5545 | /* no asoc destroyed */ |
| 5546 | SCTP_TCB_UNLOCK(stcb); |
| 5547 | #ifdef SCTP_LOG_CLOSING |
| 5548 | sctp_log_closing(inp, stcb, 8); |
| 5549 | #endif |
| 5550 | return (0); |
| 5551 | } |
| 5552 | } |
Michael Tuexen | 61f6683 | 2020-02-09 23:15:19 +0100 | [diff] [blame] | 5553 | /* Now clean up any other timers */ |
Michael Tuexen | 5f97639 | 2020-02-11 21:20:07 +0100 | [diff] [blame] | 5554 | sctp_stop_association_timers(stcb, false); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5555 | /* Now the read queue needs to be cleaned up (only once) */ |
| 5556 | if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0) { |
Michael Tuexen | 348a36c | 2018-08-13 16:24:47 +0200 | [diff] [blame] | 5557 | SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_ABOUT_TO_BE_FREED); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5558 | SCTP_INP_READ_LOCK(inp); |
| 5559 | TAILQ_FOREACH(sq, &inp->read_queue, next) { |
| 5560 | if (sq->stcb == stcb) { |
| 5561 | sq->do_not_ref_stcb = 1; |
| 5562 | sq->sinfo_cumtsn = stcb->asoc.cumulative_tsn; |
| 5563 | /* If there is no end, there never |
| 5564 | * will be now. |
| 5565 | */ |
| 5566 | if (sq->end_added == 0) { |
| 5567 | /* Held for PD-API clear that. */ |
| 5568 | sq->pdapi_aborted = 1; |
| 5569 | sq->held_length = 0; |
| 5570 | if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT) && (so != NULL)) { |
| 5571 | /* |
| 5572 | * Need to add a PD-API aborted indication. |
| 5573 | * Setting the control_pdapi assures that it will |
| 5574 | * be added right after this msg. |
| 5575 | */ |
| 5576 | uint32_t strseq; |
| 5577 | stcb->asoc.control_pdapi = sq; |
Michael Tuexen | 00657ac | 2016-12-07 21:53:26 +0100 | [diff] [blame] | 5578 | strseq = (sq->sinfo_stream << 16) | (sq->mid & 0x0000ffff); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5579 | sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION, |
| 5580 | stcb, |
| 5581 | SCTP_PARTIAL_DELIVERY_ABORTED, |
| 5582 | (void *)&strseq, |
| 5583 | SCTP_SO_LOCKED); |
| 5584 | stcb->asoc.control_pdapi = NULL; |
| 5585 | } |
| 5586 | } |
| 5587 | /* Add an end to wake them */ |
| 5588 | sq->end_added = 1; |
| 5589 | } |
| 5590 | } |
| 5591 | SCTP_INP_READ_UNLOCK(inp); |
| 5592 | if (stcb->block_entry) { |
| 5593 | SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PCB, ECONNRESET); |
| 5594 | stcb->block_entry->error = ECONNRESET; |
| 5595 | stcb->block_entry = NULL; |
| 5596 | } |
| 5597 | } |
| 5598 | if ((stcb->asoc.refcnt) || (stcb->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE)) { |
| 5599 | /* Someone holds a reference OR the socket is unaccepted yet. |
| 5600 | */ |
| 5601 | if ((stcb->asoc.refcnt) || |
| 5602 | (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || |
| 5603 | (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) { |
Michael Tuexen | 348a36c | 2018-08-13 16:24:47 +0200 | [diff] [blame] | 5604 | SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5605 | sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL); |
| 5606 | } |
| 5607 | SCTP_TCB_UNLOCK(stcb); |
| 5608 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || |
| 5609 | (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) |
| 5610 | /* nothing around */ |
| 5611 | so = NULL; |
| 5612 | if (so) { |
| 5613 | /* Wake any reader/writers */ |
| 5614 | sctp_sorwakeup(inp, so); |
| 5615 | sctp_sowwakeup(inp, so); |
| 5616 | } |
| 5617 | |
| 5618 | #ifdef SCTP_LOG_CLOSING |
| 5619 | sctp_log_closing(inp, stcb, 9); |
| 5620 | #endif |
| 5621 | /* no asoc destroyed */ |
| 5622 | return (0); |
| 5623 | } |
| 5624 | #ifdef SCTP_LOG_CLOSING |
| 5625 | sctp_log_closing(inp, stcb, 10); |
| 5626 | #endif |
| 5627 | /* When I reach here, no others want |
| 5628 | * to kill the assoc yet.. and I own |
| 5629 | * the lock. Now its possible an abort |
| 5630 | * comes in when I do the lock exchange |
| 5631 | * below to grab all the locks to do |
| 5632 | * the final take out. to prevent this |
| 5633 | * we increment the count, which will |
| 5634 | * start a timer and blow out above thus |
| 5635 | * assuring us that we hold exclusive |
| 5636 | * killing of the asoc. Note that |
| 5637 | * after getting back the TCB lock |
| 5638 | * we will go ahead and increment the |
| 5639 | * counter back up and stop any timer |
| 5640 | * a passing stranger may have started :-S |
| 5641 | */ |
| 5642 | if (from_inpcbfree == SCTP_NORMAL_PROC) { |
| 5643 | atomic_add_int(&stcb->asoc.refcnt, 1); |
| 5644 | |
| 5645 | SCTP_TCB_UNLOCK(stcb); |
| 5646 | SCTP_INP_INFO_WLOCK(); |
| 5647 | SCTP_INP_WLOCK(inp); |
| 5648 | SCTP_TCB_LOCK(stcb); |
| 5649 | } |
| 5650 | /* Double check the GONE flag */ |
| 5651 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || |
| 5652 | (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) |
| 5653 | /* nothing around */ |
| 5654 | so = NULL; |
| 5655 | |
Michael Tuexen | 1f49544 | 2020-03-25 16:41:21 +0100 | [diff] [blame] | 5656 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || |
| 5657 | (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { |
| 5658 | /* |
| 5659 | * For TCP type we need special handling when we are |
| 5660 | * connected. We also include the peel'ed off ones to. |
| 5661 | */ |
| 5662 | if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { |
| 5663 | inp->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED; |
| 5664 | inp->sctp_flags |= SCTP_PCB_FLAGS_WAS_CONNECTED; |
| 5665 | if (so) { |
| 5666 | SOCKBUF_LOCK(&so->so_rcv); |
| 5667 | so->so_state &= ~(SS_ISCONNECTING | |
| 5668 | SS_ISDISCONNECTING | |
| 5669 | SS_ISCONFIRMING | |
| 5670 | SS_ISCONNECTED); |
| 5671 | so->so_state |= SS_ISDISCONNECTED; |
| 5672 | #if defined(__APPLE__) |
| 5673 | socantrcvmore(so); |
| 5674 | #else |
| 5675 | socantrcvmore_locked(so); |
| 5676 | #endif |
| 5677 | socantsendmore(so); |
| 5678 | sctp_sowwakeup(inp, so); |
| 5679 | sctp_sorwakeup(inp, so); |
| 5680 | SCTP_SOWAKEUP(so); |
| 5681 | } |
| 5682 | } |
| 5683 | } |
| 5684 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5685 | /* Make it invalid too, that way if its |
| 5686 | * about to run it will abort and return. |
| 5687 | */ |
| 5688 | /* re-increment the lock */ |
| 5689 | if (from_inpcbfree == SCTP_NORMAL_PROC) { |
| 5690 | atomic_add_int(&stcb->asoc.refcnt, -1); |
| 5691 | } |
| 5692 | if (stcb->asoc.refcnt) { |
Michael Tuexen | 348a36c | 2018-08-13 16:24:47 +0200 | [diff] [blame] | 5693 | SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5694 | sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL); |
| 5695 | if (from_inpcbfree == SCTP_NORMAL_PROC) { |
| 5696 | SCTP_INP_INFO_WUNLOCK(); |
| 5697 | SCTP_INP_WUNLOCK(inp); |
| 5698 | } |
| 5699 | SCTP_TCB_UNLOCK(stcb); |
| 5700 | return (0); |
| 5701 | } |
| 5702 | asoc->state = 0; |
| 5703 | if (inp->sctp_tcbhash) { |
| 5704 | LIST_REMOVE(stcb, sctp_tcbhash); |
| 5705 | } |
| 5706 | if (stcb->asoc.in_asocid_hash) { |
| 5707 | LIST_REMOVE(stcb, sctp_tcbasocidhash); |
| 5708 | } |
| 5709 | /* Now lets remove it from the list of ALL associations in the EP */ |
| 5710 | LIST_REMOVE(stcb, sctp_tcblist); |
| 5711 | if (from_inpcbfree == SCTP_NORMAL_PROC) { |
| 5712 | SCTP_INP_INCR_REF(inp); |
| 5713 | SCTP_INP_WUNLOCK(inp); |
| 5714 | } |
| 5715 | /* pull from vtag hash */ |
| 5716 | LIST_REMOVE(stcb, sctp_asocs); |
| 5717 | sctp_add_vtag_to_timewait(asoc->my_vtag, SCTP_BASE_SYSCTL(sctp_vtag_time_wait), |
| 5718 | inp->sctp_lport, stcb->rport); |
| 5719 | |
| 5720 | /* Now restop the timers to be sure |
| 5721 | * this is paranoia at is finest! |
| 5722 | */ |
Michael Tuexen | 5f97639 | 2020-02-11 21:20:07 +0100 | [diff] [blame] | 5723 | sctp_stop_association_timers(stcb, true); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5724 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5725 | /* |
| 5726 | * The chunk lists and such SHOULD be empty but we check them just |
| 5727 | * in case. |
| 5728 | */ |
| 5729 | /* anything on the wheel needs to be removed */ |
Michael Tuexen | c3dd8e2 | 2019-03-26 14:09:18 +0100 | [diff] [blame] | 5730 | SCTP_TCB_SEND_LOCK(stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5731 | for (i = 0; i < asoc->streamoutcnt; i++) { |
| 5732 | struct sctp_stream_out *outs; |
| 5733 | |
| 5734 | outs = &asoc->strmout[i]; |
| 5735 | /* now clean up any chunks here */ |
| 5736 | TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) { |
Michael Tuexen | c6ae6c6 | 2016-08-07 15:17:05 +0200 | [diff] [blame] | 5737 | atomic_subtract_int(&asoc->stream_queue_cnt, 1); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5738 | TAILQ_REMOVE(&outs->outqueue, sp, next); |
Michael Tuexen | c3dd8e2 | 2019-03-26 14:09:18 +0100 | [diff] [blame] | 5739 | 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] | 5740 | sctp_free_spbufspace(stcb, asoc, sp); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5741 | if (sp->data) { |
| 5742 | if (so) { |
| 5743 | /* Still an open socket - report */ |
| 5744 | sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb, |
tuexen | da53ff0 | 2012-05-14 09:00:59 +0000 | [diff] [blame] | 5745 | 0, (void *)sp, SCTP_SO_LOCKED); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5746 | } |
| 5747 | if (sp->data) { |
| 5748 | sctp_m_freem(sp->data); |
| 5749 | sp->data = NULL; |
| 5750 | sp->tail_mbuf = NULL; |
tuexen | 30ca817 | 2012-07-17 13:09:48 +0000 | [diff] [blame] | 5751 | sp->length = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5752 | } |
| 5753 | } |
| 5754 | if (sp->net) { |
| 5755 | sctp_free_remote_addr(sp->net); |
| 5756 | sp->net = NULL; |
| 5757 | } |
tuexen | 30ca817 | 2012-07-17 13:09:48 +0000 | [diff] [blame] | 5758 | sctp_free_a_strmoq(stcb, sp, SCTP_SO_LOCKED); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5759 | } |
| 5760 | } |
Michael Tuexen | c3dd8e2 | 2019-03-26 14:09:18 +0100 | [diff] [blame] | 5761 | SCTP_TCB_SEND_UNLOCK(stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5762 | /*sa_ignore FREED_MEMORY*/ |
| 5763 | TAILQ_FOREACH_SAFE(strrst, &asoc->resetHead, next_resp, nstrrst) { |
| 5764 | TAILQ_REMOVE(&asoc->resetHead, strrst, next_resp); |
| 5765 | SCTP_FREE(strrst, SCTP_M_STRESET); |
| 5766 | } |
| 5767 | TAILQ_FOREACH_SAFE(sq, &asoc->pending_reply_queue, next, nsq) { |
| 5768 | TAILQ_REMOVE(&asoc->pending_reply_queue, sq, next); |
| 5769 | if (sq->data) { |
| 5770 | sctp_m_freem(sq->data); |
| 5771 | sq->data = NULL; |
| 5772 | } |
| 5773 | sctp_free_remote_addr(sq->whoFrom); |
| 5774 | sq->whoFrom = NULL; |
| 5775 | sq->stcb = NULL; |
| 5776 | /* Free the ctl entry */ |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 5777 | sctp_free_a_readq(stcb, sq); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5778 | /*sa_ignore FREED_MEMORY*/ |
| 5779 | } |
| 5780 | TAILQ_FOREACH_SAFE(chk, &asoc->free_chunks, sctp_next, nchk) { |
| 5781 | TAILQ_REMOVE(&asoc->free_chunks, chk, sctp_next); |
| 5782 | if (chk->data) { |
| 5783 | sctp_m_freem(chk->data); |
| 5784 | chk->data = NULL; |
| 5785 | } |
| 5786 | if (chk->holds_key_ref) |
| 5787 | sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); |
| 5788 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); |
| 5789 | SCTP_DECR_CHK_COUNT(); |
| 5790 | atomic_subtract_int(&SCTP_BASE_INFO(ipi_free_chunks), 1); |
| 5791 | asoc->free_chunk_cnt--; |
| 5792 | /*sa_ignore FREED_MEMORY*/ |
| 5793 | } |
| 5794 | /* pending send queue SHOULD be empty */ |
| 5795 | TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) { |
Michael Tuexen | 00657ac | 2016-12-07 21:53:26 +0100 | [diff] [blame] | 5796 | if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) { |
| 5797 | asoc->strmout[chk->rec.data.sid].chunks_on_queues--; |
t00fcxen | 8fcc514 | 2012-11-16 19:46:12 +0000 | [diff] [blame] | 5798 | #ifdef INVARIANTS |
| 5799 | } else { |
Michael Tuexen | 00657ac | 2016-12-07 21:53:26 +0100 | [diff] [blame] | 5800 | panic("No chunks on the queues for sid %u.", chk->rec.data.sid); |
t00fcxen | 8fcc514 | 2012-11-16 19:46:12 +0000 | [diff] [blame] | 5801 | #endif |
| 5802 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5803 | TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next); |
| 5804 | if (chk->data) { |
| 5805 | if (so) { |
| 5806 | /* Still a socket? */ |
tuexen | da53ff0 | 2012-05-14 09:00:59 +0000 | [diff] [blame] | 5807 | sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, |
| 5808 | 0, chk, SCTP_SO_LOCKED); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5809 | } |
| 5810 | if (chk->data) { |
| 5811 | sctp_m_freem(chk->data); |
| 5812 | chk->data = NULL; |
| 5813 | } |
| 5814 | } |
| 5815 | if (chk->holds_key_ref) |
| 5816 | sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); |
| 5817 | if (chk->whoTo) { |
| 5818 | sctp_free_remote_addr(chk->whoTo); |
| 5819 | chk->whoTo = NULL; |
| 5820 | } |
| 5821 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); |
| 5822 | SCTP_DECR_CHK_COUNT(); |
| 5823 | /*sa_ignore FREED_MEMORY*/ |
| 5824 | } |
| 5825 | /* sent queue SHOULD be empty */ |
| 5826 | TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) { |
t00fcxen | 8fcc514 | 2012-11-16 19:46:12 +0000 | [diff] [blame] | 5827 | if (chk->sent != SCTP_DATAGRAM_NR_ACKED) { |
Michael Tuexen | 00657ac | 2016-12-07 21:53:26 +0100 | [diff] [blame] | 5828 | if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) { |
| 5829 | asoc->strmout[chk->rec.data.sid].chunks_on_queues--; |
t00fcxen | 9ad9077 | 2012-11-07 22:19:57 +0000 | [diff] [blame] | 5830 | #ifdef INVARIANTS |
| 5831 | } else { |
Michael Tuexen | 00657ac | 2016-12-07 21:53:26 +0100 | [diff] [blame] | 5832 | panic("No chunks on the queues for sid %u.", chk->rec.data.sid); |
t00fcxen | 9ad9077 | 2012-11-07 22:19:57 +0000 | [diff] [blame] | 5833 | #endif |
| 5834 | } |
| 5835 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5836 | TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next); |
| 5837 | if (chk->data) { |
| 5838 | if (so) { |
| 5839 | /* Still a socket? */ |
tuexen | da53ff0 | 2012-05-14 09:00:59 +0000 | [diff] [blame] | 5840 | sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, |
| 5841 | 0, chk, SCTP_SO_LOCKED); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5842 | } |
| 5843 | if (chk->data) { |
| 5844 | sctp_m_freem(chk->data); |
| 5845 | chk->data = NULL; |
| 5846 | } |
| 5847 | } |
| 5848 | if (chk->holds_key_ref) |
| 5849 | sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); |
| 5850 | sctp_free_remote_addr(chk->whoTo); |
| 5851 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); |
| 5852 | SCTP_DECR_CHK_COUNT(); |
| 5853 | /*sa_ignore FREED_MEMORY*/ |
| 5854 | } |
t00fcxen | 8fcc514 | 2012-11-16 19:46:12 +0000 | [diff] [blame] | 5855 | #ifdef INVARIANTS |
| 5856 | for (i = 0; i < stcb->asoc.streamoutcnt; i++) { |
| 5857 | if (stcb->asoc.strmout[i].chunks_on_queues > 0) { |
| 5858 | panic("%u chunks left for stream %u.", stcb->asoc.strmout[i].chunks_on_queues, i); |
| 5859 | } |
| 5860 | } |
| 5861 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5862 | /* control queue MAY not be empty */ |
| 5863 | TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) { |
| 5864 | TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next); |
| 5865 | if (chk->data) { |
| 5866 | sctp_m_freem(chk->data); |
| 5867 | chk->data = NULL; |
| 5868 | } |
| 5869 | if (chk->holds_key_ref) |
| 5870 | sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); |
| 5871 | sctp_free_remote_addr(chk->whoTo); |
| 5872 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); |
| 5873 | SCTP_DECR_CHK_COUNT(); |
| 5874 | /*sa_ignore FREED_MEMORY*/ |
| 5875 | } |
| 5876 | /* ASCONF queue MAY not be empty */ |
| 5877 | TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) { |
| 5878 | TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next); |
| 5879 | if (chk->data) { |
| 5880 | sctp_m_freem(chk->data); |
| 5881 | chk->data = NULL; |
| 5882 | } |
| 5883 | if (chk->holds_key_ref) |
| 5884 | sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); |
| 5885 | sctp_free_remote_addr(chk->whoTo); |
| 5886 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); |
| 5887 | SCTP_DECR_CHK_COUNT(); |
| 5888 | /*sa_ignore FREED_MEMORY*/ |
| 5889 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5890 | if (asoc->mapping_array) { |
| 5891 | SCTP_FREE(asoc->mapping_array, SCTP_M_MAP); |
| 5892 | asoc->mapping_array = NULL; |
| 5893 | } |
| 5894 | if (asoc->nr_mapping_array) { |
| 5895 | SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP); |
| 5896 | asoc->nr_mapping_array = NULL; |
| 5897 | } |
| 5898 | /* the stream outs */ |
| 5899 | if (asoc->strmout) { |
| 5900 | SCTP_FREE(asoc->strmout, SCTP_M_STRMO); |
| 5901 | asoc->strmout = NULL; |
| 5902 | } |
| 5903 | asoc->strm_realoutsize = asoc->streamoutcnt = 0; |
| 5904 | if (asoc->strmin) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5905 | for (i = 0; i < asoc->streamincnt; i++) { |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 5906 | sctp_clean_up_stream(stcb, &asoc->strmin[i].inqueue); |
| 5907 | sctp_clean_up_stream(stcb, &asoc->strmin[i].uno_inqueue); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5908 | } |
| 5909 | SCTP_FREE(asoc->strmin, SCTP_M_STRMI); |
| 5910 | asoc->strmin = NULL; |
| 5911 | } |
| 5912 | asoc->streamincnt = 0; |
| 5913 | TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) { |
| 5914 | #ifdef INVARIANTS |
| 5915 | if (SCTP_BASE_INFO(ipi_count_raddr) == 0) { |
| 5916 | panic("no net's left alloc'ed, or list points to itself"); |
| 5917 | } |
| 5918 | #endif |
| 5919 | TAILQ_REMOVE(&asoc->nets, net, sctp_next); |
| 5920 | sctp_free_remote_addr(net); |
| 5921 | } |
| 5922 | LIST_FOREACH_SAFE(laddr, &asoc->sctp_restricted_addrs, sctp_nxt_addr, naddr) { |
| 5923 | /*sa_ignore FREED_MEMORY*/ |
| 5924 | sctp_remove_laddr(laddr); |
| 5925 | } |
| 5926 | |
| 5927 | /* pending asconf (address) parameters */ |
| 5928 | TAILQ_FOREACH_SAFE(aparam, &asoc->asconf_queue, next, naparam) { |
| 5929 | /*sa_ignore FREED_MEMORY*/ |
| 5930 | TAILQ_REMOVE(&asoc->asconf_queue, aparam, next); |
| 5931 | SCTP_FREE(aparam,SCTP_M_ASC_ADDR); |
| 5932 | } |
| 5933 | TAILQ_FOREACH_SAFE(aack, &asoc->asconf_ack_sent, next, naack) { |
| 5934 | /*sa_ignore FREED_MEMORY*/ |
| 5935 | TAILQ_REMOVE(&asoc->asconf_ack_sent, aack, next); |
| 5936 | if (aack->data != NULL) { |
| 5937 | sctp_m_freem(aack->data); |
| 5938 | } |
| 5939 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asconf_ack), aack); |
| 5940 | } |
| 5941 | /* clean up auth stuff */ |
| 5942 | if (asoc->local_hmacs) |
| 5943 | sctp_free_hmaclist(asoc->local_hmacs); |
| 5944 | if (asoc->peer_hmacs) |
| 5945 | sctp_free_hmaclist(asoc->peer_hmacs); |
| 5946 | |
| 5947 | if (asoc->local_auth_chunks) |
| 5948 | sctp_free_chunklist(asoc->local_auth_chunks); |
| 5949 | if (asoc->peer_auth_chunks) |
| 5950 | sctp_free_chunklist(asoc->peer_auth_chunks); |
| 5951 | |
| 5952 | sctp_free_authinfo(&asoc->authinfo); |
| 5953 | |
| 5954 | LIST_FOREACH_SAFE(shared_key, &asoc->shared_keys, next, nshared_key) { |
| 5955 | LIST_REMOVE(shared_key, next); |
| 5956 | sctp_free_sharedkey(shared_key); |
| 5957 | /*sa_ignore FREED_MEMORY*/ |
| 5958 | } |
| 5959 | |
| 5960 | /* Insert new items here :> */ |
| 5961 | |
| 5962 | /* Get rid of LOCK */ |
t00fcxen | 2c2ff92 | 2013-11-03 14:03:35 +0000 | [diff] [blame] | 5963 | SCTP_TCB_UNLOCK(stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 5964 | SCTP_TCB_LOCK_DESTROY(stcb); |
| 5965 | SCTP_TCB_SEND_LOCK_DESTROY(stcb); |
| 5966 | if (from_inpcbfree == SCTP_NORMAL_PROC) { |
| 5967 | SCTP_INP_INFO_WUNLOCK(); |
| 5968 | SCTP_INP_RLOCK(inp); |
| 5969 | } |
| 5970 | #if defined(__APPLE__) /* TEMP CODE */ |
| 5971 | stcb->freed_from_where = from_location; |
| 5972 | #endif |
| 5973 | #ifdef SCTP_TRACK_FREED_ASOCS |
| 5974 | if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { |
| 5975 | /* now clean up the tasoc itself */ |
| 5976 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb); |
| 5977 | SCTP_DECR_ASOC_COUNT(); |
| 5978 | } else { |
| 5979 | LIST_INSERT_HEAD(&inp->sctp_asoc_free_list, stcb, sctp_tcblist); |
| 5980 | } |
| 5981 | #else |
| 5982 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb); |
| 5983 | SCTP_DECR_ASOC_COUNT(); |
| 5984 | #endif |
| 5985 | if (from_inpcbfree == SCTP_NORMAL_PROC) { |
| 5986 | if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { |
| 5987 | /* If its NOT the inp_free calling us AND |
| 5988 | * sctp_close as been called, we |
| 5989 | * call back... |
| 5990 | */ |
| 5991 | SCTP_INP_RUNLOCK(inp); |
| 5992 | /* This will start the kill timer (if we are |
| 5993 | * the last one) since we hold an increment yet. But |
| 5994 | * this is the only safe way to do this |
| 5995 | * since otherwise if the socket closes |
| 5996 | * at the same time we are here we might |
| 5997 | * collide in the cleanup. |
| 5998 | */ |
| 5999 | sctp_inpcb_free(inp, |
| 6000 | SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE, |
| 6001 | SCTP_CALLED_DIRECTLY_NOCMPSET); |
| 6002 | SCTP_INP_DECR_REF(inp); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6003 | } else { |
| 6004 | /* The socket is still open. */ |
| 6005 | SCTP_INP_DECR_REF(inp); |
Michael Tuexen | c6c994c | 2020-05-18 21:50:12 +0200 | [diff] [blame] | 6006 | SCTP_INP_RUNLOCK(inp); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6007 | } |
| 6008 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6009 | /* destroyed the asoc */ |
| 6010 | #ifdef SCTP_LOG_CLOSING |
| 6011 | sctp_log_closing(inp, NULL, 11); |
| 6012 | #endif |
| 6013 | return (1); |
| 6014 | } |
| 6015 | |
| 6016 | |
| 6017 | |
| 6018 | /* |
| 6019 | * determine if a destination is "reachable" based upon the addresses bound |
| 6020 | * to the current endpoint (e.g. only v4 or v6 currently bound) |
| 6021 | */ |
| 6022 | /* |
| 6023 | * FIX: if we allow assoc-level bindx(), then this needs to be fixed to use |
| 6024 | * assoc level v4/v6 flags, as the assoc *may* not have the same address |
| 6025 | * types bound as its endpoint |
| 6026 | */ |
| 6027 | int |
| 6028 | sctp_destination_is_reachable(struct sctp_tcb *stcb, struct sockaddr *destaddr) |
| 6029 | { |
| 6030 | struct sctp_inpcb *inp; |
| 6031 | int answer; |
| 6032 | |
| 6033 | /* |
| 6034 | * No locks here, the TCB, in all cases is already locked and an |
| 6035 | * assoc is up. There is either a INP lock by the caller applied (in |
| 6036 | * asconf case when deleting an address) or NOT in the HB case, |
| 6037 | * however if HB then the INP increment is up and the INP will not |
| 6038 | * be removed (on top of the fact that we have a TCB lock). So we |
| 6039 | * only want to read the sctp_flags, which is either bound-all or |
| 6040 | * not.. no protection needed since once an assoc is up you can't be |
| 6041 | * changing your binding. |
| 6042 | */ |
| 6043 | inp = stcb->sctp_ep; |
| 6044 | if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { |
| 6045 | /* if bound all, destination is not restricted */ |
| 6046 | /* |
| 6047 | * RRS: Question during lock work: Is this correct? If you |
| 6048 | * are bound-all you still might need to obey the V4--V6 |
| 6049 | * flags??? IMO this bound-all stuff needs to be removed! |
| 6050 | */ |
| 6051 | return (1); |
| 6052 | } |
| 6053 | /* NOTE: all "scope" checks are done when local addresses are added */ |
| 6054 | switch (destaddr->sa_family) { |
tuexen | 310f1bc | 2012-07-15 11:20:56 +0000 | [diff] [blame] | 6055 | #ifdef INET6 |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6056 | case AF_INET6: |
| 6057 | #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)) |
| 6058 | answer = inp->inp_vflag & INP_IPV6; |
| 6059 | #else |
| 6060 | answer = inp->ip_inp.inp.inp_vflag & INP_IPV6; |
| 6061 | #endif |
| 6062 | break; |
tuexen | 310f1bc | 2012-07-15 11:20:56 +0000 | [diff] [blame] | 6063 | #endif |
| 6064 | #ifdef INET |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6065 | case AF_INET: |
| 6066 | #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)) |
| 6067 | answer = inp->inp_vflag & INP_IPV4; |
| 6068 | #else |
| 6069 | answer = inp->ip_inp.inp.inp_vflag & INP_IPV4; |
| 6070 | #endif |
tuexen | 4657054 | 2012-07-14 20:37:24 +0000 | [diff] [blame] | 6071 | break; |
tuexen | 310f1bc | 2012-07-15 11:20:56 +0000 | [diff] [blame] | 6072 | #endif |
tuexen | 5d9b974 | 2012-07-11 21:52:01 +0000 | [diff] [blame] | 6073 | #if defined(__Userspace__) |
| 6074 | case AF_CONN: |
| 6075 | answer = inp->ip_inp.inp.inp_vflag & INP_CONN; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6076 | break; |
tuexen | 5d9b974 | 2012-07-11 21:52:01 +0000 | [diff] [blame] | 6077 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6078 | default: |
| 6079 | /* invalid family, so it's unreachable */ |
| 6080 | answer = 0; |
| 6081 | break; |
| 6082 | } |
| 6083 | return (answer); |
| 6084 | } |
| 6085 | |
| 6086 | /* |
| 6087 | * update the inp_vflags on an endpoint |
| 6088 | */ |
| 6089 | static void |
| 6090 | sctp_update_ep_vflag(struct sctp_inpcb *inp) |
| 6091 | { |
| 6092 | struct sctp_laddr *laddr; |
| 6093 | |
| 6094 | /* first clear the flag */ |
| 6095 | #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)) |
| 6096 | inp->inp_vflag = 0; |
| 6097 | #else |
| 6098 | inp->ip_inp.inp.inp_vflag = 0; |
| 6099 | #endif |
| 6100 | /* set the flag based on addresses on the ep list */ |
| 6101 | LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { |
| 6102 | if (laddr->ifa == NULL) { |
| 6103 | SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n", |
Saúl Ibarra Corretgé | 8b04b41 | 2015-09-26 09:53:06 +0200 | [diff] [blame] | 6104 | __func__); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6105 | continue; |
| 6106 | } |
| 6107 | |
| 6108 | if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) { |
| 6109 | continue; |
| 6110 | } |
| 6111 | switch (laddr->ifa->address.sa.sa_family) { |
| 6112 | #ifdef INET6 |
| 6113 | case AF_INET6: |
| 6114 | #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)) |
| 6115 | inp->inp_vflag |= INP_IPV6; |
| 6116 | #else |
| 6117 | inp->ip_inp.inp.inp_vflag |= INP_IPV6; |
| 6118 | #endif |
| 6119 | break; |
| 6120 | #endif |
| 6121 | #ifdef INET |
| 6122 | case AF_INET: |
| 6123 | #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)) |
| 6124 | inp->inp_vflag |= INP_IPV4; |
| 6125 | #else |
| 6126 | inp->ip_inp.inp.inp_vflag |= INP_IPV4; |
| 6127 | #endif |
| 6128 | break; |
| 6129 | #endif |
tuexen | 5d9b974 | 2012-07-11 21:52:01 +0000 | [diff] [blame] | 6130 | #if defined(__Userspace__) |
| 6131 | case AF_CONN: |
| 6132 | inp->ip_inp.inp.inp_vflag |= INP_CONN; |
| 6133 | break; |
| 6134 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6135 | default: |
| 6136 | break; |
| 6137 | } |
| 6138 | } |
| 6139 | } |
| 6140 | |
| 6141 | /* |
| 6142 | * Add the address to the endpoint local address list There is nothing to be |
| 6143 | * done if we are bound to all addresses |
| 6144 | */ |
| 6145 | void |
| 6146 | sctp_add_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa, uint32_t action) |
| 6147 | { |
| 6148 | struct sctp_laddr *laddr; |
Michael Tuexen | d75dc0c | 2016-03-28 21:38:43 +0200 | [diff] [blame] | 6149 | struct sctp_tcb *stcb; |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 6150 | int fnd, error = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6151 | |
| 6152 | fnd = 0; |
| 6153 | |
| 6154 | if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { |
| 6155 | /* You are already bound to all. You have it already */ |
| 6156 | return; |
| 6157 | } |
| 6158 | #ifdef INET6 |
| 6159 | if (ifa->address.sa.sa_family == AF_INET6) { |
| 6160 | if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) { |
| 6161 | /* Can't bind a non-useable addr. */ |
| 6162 | return; |
| 6163 | } |
| 6164 | } |
| 6165 | #endif |
| 6166 | /* first, is it already present? */ |
| 6167 | LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { |
| 6168 | if (laddr->ifa == ifa) { |
| 6169 | fnd = 1; |
| 6170 | break; |
| 6171 | } |
| 6172 | } |
| 6173 | |
| 6174 | if (fnd == 0) { |
| 6175 | /* Not in the ep list */ |
| 6176 | error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, action); |
| 6177 | if (error != 0) |
| 6178 | return; |
| 6179 | inp->laddr_count++; |
| 6180 | /* update inp_vflag flags */ |
| 6181 | switch (ifa->address.sa.sa_family) { |
| 6182 | #ifdef INET6 |
| 6183 | case AF_INET6: |
| 6184 | #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)) |
| 6185 | inp->inp_vflag |= INP_IPV6; |
| 6186 | #else |
| 6187 | inp->ip_inp.inp.inp_vflag |= INP_IPV6; |
| 6188 | #endif |
| 6189 | break; |
| 6190 | #endif |
tuexen | 310f1bc | 2012-07-15 11:20:56 +0000 | [diff] [blame] | 6191 | #ifdef INET |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6192 | case AF_INET: |
| 6193 | #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)) |
| 6194 | inp->inp_vflag |= INP_IPV4; |
| 6195 | #else |
| 6196 | inp->ip_inp.inp.inp_vflag |= INP_IPV4; |
| 6197 | #endif |
| 6198 | break; |
| 6199 | #endif |
tuexen | 5d9b974 | 2012-07-11 21:52:01 +0000 | [diff] [blame] | 6200 | #if defined(__Userspace__) |
| 6201 | case AF_CONN: |
| 6202 | inp->ip_inp.inp.inp_vflag |= INP_CONN; |
| 6203 | break; |
| 6204 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6205 | default: |
| 6206 | break; |
| 6207 | } |
Michael Tuexen | d75dc0c | 2016-03-28 21:38:43 +0200 | [diff] [blame] | 6208 | LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { |
| 6209 | sctp_add_local_addr_restricted(stcb, ifa); |
| 6210 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6211 | } |
| 6212 | return; |
| 6213 | } |
| 6214 | |
| 6215 | |
| 6216 | /* |
| 6217 | * select a new (hopefully reachable) destination net (should only be used |
| 6218 | * when we deleted an ep addr that is the only usable source address to reach |
| 6219 | * the destination net) |
| 6220 | */ |
| 6221 | static void |
| 6222 | sctp_select_primary_destination(struct sctp_tcb *stcb) |
| 6223 | { |
| 6224 | struct sctp_nets *net; |
| 6225 | |
| 6226 | TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { |
| 6227 | /* for now, we'll just pick the first reachable one we find */ |
| 6228 | if (net->dest_state & SCTP_ADDR_UNCONFIRMED) |
| 6229 | continue; |
| 6230 | if (sctp_destination_is_reachable(stcb, |
| 6231 | (struct sockaddr *)&net->ro._l_addr)) { |
| 6232 | /* found a reachable destination */ |
| 6233 | stcb->asoc.primary_destination = net; |
| 6234 | } |
| 6235 | } |
| 6236 | /* I can't there from here! ...we're gonna die shortly... */ |
| 6237 | } |
| 6238 | |
| 6239 | |
| 6240 | /* |
Michael Tuexen | 24e6c73 | 2016-02-19 12:28:21 +0100 | [diff] [blame] | 6241 | * Delete the address from the endpoint local address list. There is nothing |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6242 | * to be done if we are bound to all addresses |
| 6243 | */ |
| 6244 | void |
| 6245 | sctp_del_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa) |
| 6246 | { |
| 6247 | struct sctp_laddr *laddr; |
| 6248 | int fnd; |
| 6249 | |
| 6250 | fnd = 0; |
| 6251 | if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { |
| 6252 | /* You are already bound to all. You have it already */ |
| 6253 | return; |
| 6254 | } |
| 6255 | LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { |
| 6256 | if (laddr->ifa == ifa) { |
| 6257 | fnd = 1; |
| 6258 | break; |
| 6259 | } |
| 6260 | } |
| 6261 | if (fnd && (inp->laddr_count < 2)) { |
| 6262 | /* can't delete unless there are at LEAST 2 addresses */ |
| 6263 | return; |
| 6264 | } |
| 6265 | if (fnd) { |
| 6266 | /* |
| 6267 | * clean up any use of this address go through our |
| 6268 | * associations and clear any last_used_address that match |
| 6269 | * this one for each assoc, see if a new primary_destination |
| 6270 | * is needed |
| 6271 | */ |
| 6272 | struct sctp_tcb *stcb; |
| 6273 | |
| 6274 | /* clean up "next_addr_touse" */ |
| 6275 | if (inp->next_addr_touse == laddr) |
| 6276 | /* delete this address */ |
| 6277 | inp->next_addr_touse = NULL; |
| 6278 | |
| 6279 | /* clean up "last_used_address" */ |
| 6280 | LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { |
| 6281 | struct sctp_nets *net; |
Michael Tuexen | 24e6c73 | 2016-02-19 12:28:21 +0100 | [diff] [blame] | 6282 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6283 | SCTP_TCB_LOCK(stcb); |
| 6284 | if (stcb->asoc.last_used_address == laddr) |
| 6285 | /* delete this address */ |
| 6286 | stcb->asoc.last_used_address = NULL; |
| 6287 | /* Now spin through all the nets and purge any ref to laddr */ |
| 6288 | TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { |
Michael Tuexen | 24e6c73 | 2016-02-19 12:28:21 +0100 | [diff] [blame] | 6289 | if (net->ro._s_addr == laddr->ifa) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6290 | /* Yep, purge src address selected */ |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 6291 | #if defined(__FreeBSD__) |
| 6292 | RO_NHFREE(&net->ro); |
| 6293 | #else |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6294 | sctp_rtentry_t *rt; |
| 6295 | |
| 6296 | /* delete this address if cached */ |
| 6297 | rt = net->ro.ro_rt; |
| 6298 | if (rt != NULL) { |
| 6299 | RTFREE(rt); |
| 6300 | net->ro.ro_rt = NULL; |
| 6301 | } |
Michael Tuexen | a051542 | 2020-05-18 00:47:04 +0200 | [diff] [blame] | 6302 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6303 | sctp_free_ifa(net->ro._s_addr); |
| 6304 | net->ro._s_addr = NULL; |
| 6305 | net->src_addr_selected = 0; |
| 6306 | } |
| 6307 | } |
| 6308 | SCTP_TCB_UNLOCK(stcb); |
| 6309 | } /* for each tcb */ |
| 6310 | /* remove it from the ep list */ |
| 6311 | sctp_remove_laddr(laddr); |
| 6312 | inp->laddr_count--; |
| 6313 | /* update inp_vflag flags */ |
| 6314 | sctp_update_ep_vflag(inp); |
| 6315 | } |
| 6316 | return; |
| 6317 | } |
| 6318 | |
| 6319 | /* |
| 6320 | * Add the address to the TCB local address restricted list. |
| 6321 | * This is a "pending" address list (eg. addresses waiting for an |
| 6322 | * ASCONF-ACK response) and cannot be used as a valid source address. |
| 6323 | */ |
| 6324 | void |
| 6325 | sctp_add_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa) |
| 6326 | { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6327 | struct sctp_laddr *laddr; |
| 6328 | struct sctpladdr *list; |
| 6329 | |
| 6330 | /* |
| 6331 | * Assumes TCB is locked.. and possibly the INP. May need to |
| 6332 | * confirm/fix that if we need it and is not the case. |
| 6333 | */ |
| 6334 | list = &stcb->asoc.sctp_restricted_addrs; |
| 6335 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6336 | #ifdef INET6 |
| 6337 | if (ifa->address.sa.sa_family == AF_INET6) { |
| 6338 | if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) { |
| 6339 | /* Can't bind a non-existent addr. */ |
| 6340 | return; |
| 6341 | } |
| 6342 | } |
| 6343 | #endif |
| 6344 | /* does the address already exist? */ |
| 6345 | LIST_FOREACH(laddr, list, sctp_nxt_addr) { |
| 6346 | if (laddr->ifa == ifa) { |
| 6347 | return; |
| 6348 | } |
| 6349 | } |
| 6350 | |
| 6351 | /* add to the list */ |
| 6352 | (void)sctp_insert_laddr(list, ifa, 0); |
| 6353 | return; |
| 6354 | } |
| 6355 | |
| 6356 | /* |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6357 | * Remove a local address from the TCB local address restricted list |
| 6358 | */ |
| 6359 | void |
| 6360 | sctp_del_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa) |
| 6361 | { |
| 6362 | struct sctp_inpcb *inp; |
| 6363 | struct sctp_laddr *laddr; |
| 6364 | |
| 6365 | /* |
| 6366 | * This is called by asconf work. It is assumed that a) The TCB is |
| 6367 | * locked and b) The INP is locked. This is true in as much as I can |
| 6368 | * trace through the entry asconf code where I did these locks. |
| 6369 | * Again, the ASCONF code is a bit different in that it does lock |
| 6370 | * the INP during its work often times. This must be since we don't |
| 6371 | * want other proc's looking up things while what they are looking |
| 6372 | * up is changing :-D |
| 6373 | */ |
| 6374 | |
| 6375 | inp = stcb->sctp_ep; |
| 6376 | /* if subset bound and don't allow ASCONF's, can't delete last */ |
| 6377 | if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) && |
| 6378 | sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF)) { |
| 6379 | if (stcb->sctp_ep->laddr_count < 2) { |
| 6380 | /* can't delete last address */ |
| 6381 | return; |
| 6382 | } |
| 6383 | } |
| 6384 | LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) { |
| 6385 | /* remove the address if it exists */ |
| 6386 | if (laddr->ifa == NULL) |
| 6387 | continue; |
| 6388 | if (laddr->ifa == ifa) { |
| 6389 | sctp_remove_laddr(laddr); |
| 6390 | return; |
| 6391 | } |
| 6392 | } |
| 6393 | |
| 6394 | /* address not found! */ |
| 6395 | return; |
| 6396 | } |
| 6397 | |
| 6398 | #if defined(__FreeBSD__) |
| 6399 | /* |
| 6400 | * Temporarily remove for __APPLE__ until we use the Tiger equivalents |
| 6401 | */ |
| 6402 | /* sysctl */ |
| 6403 | static int sctp_max_number_of_assoc = SCTP_MAX_NUM_OF_ASOC; |
| 6404 | static int sctp_scale_up_for_address = SCTP_SCALE_FOR_ADDR; |
| 6405 | #endif /* FreeBSD || APPLE */ |
| 6406 | |
| 6407 | |
| 6408 | |
| 6409 | #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP) |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 6410 | struct sctp_mcore_ctrl *sctp_mcore_workers = NULL; |
| 6411 | int *sctp_cpuarry = NULL; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6412 | void |
| 6413 | sctp_queue_to_mcore(struct mbuf *m, int off, int cpu_to_use) |
| 6414 | { |
| 6415 | /* Queue a packet to a processor for the specified core */ |
| 6416 | struct sctp_mcore_queue *qent; |
| 6417 | struct sctp_mcore_ctrl *wkq; |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 6418 | int need_wake = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6419 | if (sctp_mcore_workers == NULL) { |
| 6420 | /* Something went way bad during setup */ |
| 6421 | sctp_input_with_port(m, off, 0); |
| 6422 | return; |
| 6423 | } |
| 6424 | SCTP_MALLOC(qent, struct sctp_mcore_queue *, |
| 6425 | (sizeof(struct sctp_mcore_queue)), |
| 6426 | SCTP_M_MCORE); |
| 6427 | if (qent == NULL) { |
| 6428 | /* This is trouble */ |
| 6429 | sctp_input_with_port(m, off, 0); |
| 6430 | return; |
| 6431 | } |
| 6432 | #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 |
| 6433 | qent->vn = curvnet; |
| 6434 | #endif |
| 6435 | qent->m = m; |
| 6436 | qent->off = off; |
| 6437 | qent->v6 = 0; |
| 6438 | wkq = &sctp_mcore_workers[cpu_to_use]; |
| 6439 | SCTP_MCORE_QLOCK(wkq); |
| 6440 | |
| 6441 | TAILQ_INSERT_TAIL(&wkq->que, qent, next); |
| 6442 | if (wkq->running == 0) { |
| 6443 | need_wake = 1; |
| 6444 | } |
| 6445 | SCTP_MCORE_QUNLOCK(wkq); |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 6446 | if (need_wake) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6447 | wakeup(&wkq->running); |
| 6448 | } |
| 6449 | } |
| 6450 | |
| 6451 | static void |
| 6452 | sctp_mcore_thread(void *arg) |
| 6453 | { |
| 6454 | |
| 6455 | struct sctp_mcore_ctrl *wkq; |
| 6456 | struct sctp_mcore_queue *qent; |
| 6457 | |
| 6458 | wkq = (struct sctp_mcore_ctrl *)arg; |
| 6459 | struct mbuf *m; |
| 6460 | int off, v6; |
| 6461 | |
| 6462 | /* Wait for first tickle */ |
| 6463 | SCTP_MCORE_LOCK(wkq); |
| 6464 | wkq->running = 0; |
| 6465 | msleep(&wkq->running, |
| 6466 | &wkq->core_mtx, |
| 6467 | 0, "wait for pkt", 0); |
| 6468 | SCTP_MCORE_UNLOCK(wkq); |
| 6469 | |
| 6470 | /* Bind to our cpu */ |
| 6471 | thread_lock(curthread); |
| 6472 | sched_bind(curthread, wkq->cpuid); |
| 6473 | thread_unlock(curthread); |
| 6474 | |
| 6475 | /* Now lets start working */ |
| 6476 | SCTP_MCORE_LOCK(wkq); |
| 6477 | /* Now grab lock and go */ |
tuexen | 9784e9a | 2011-12-18 13:04:23 +0000 | [diff] [blame] | 6478 | for (;;) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6479 | SCTP_MCORE_QLOCK(wkq); |
| 6480 | skip_sleep: |
| 6481 | wkq->running = 1; |
| 6482 | qent = TAILQ_FIRST(&wkq->que); |
| 6483 | if (qent) { |
| 6484 | TAILQ_REMOVE(&wkq->que, qent, next); |
| 6485 | SCTP_MCORE_QUNLOCK(wkq); |
| 6486 | #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 |
| 6487 | CURVNET_SET(qent->vn); |
| 6488 | #endif |
| 6489 | m = qent->m; |
| 6490 | off = qent->off; |
| 6491 | v6 = qent->v6; |
| 6492 | SCTP_FREE(qent, SCTP_M_MCORE); |
| 6493 | if (v6 == 0) { |
| 6494 | sctp_input_with_port(m, off, 0); |
| 6495 | } else { |
tuexen | cb5fe8d | 2012-05-04 09:50:27 +0000 | [diff] [blame] | 6496 | SCTP_PRINTF("V6 not yet supported\n"); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6497 | sctp_m_freem(m); |
| 6498 | } |
| 6499 | #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 |
| 6500 | CURVNET_RESTORE(); |
| 6501 | #endif |
| 6502 | SCTP_MCORE_QLOCK(wkq); |
| 6503 | } |
| 6504 | wkq->running = 0; |
| 6505 | if (!TAILQ_EMPTY(&wkq->que)) { |
| 6506 | goto skip_sleep; |
| 6507 | } |
| 6508 | SCTP_MCORE_QUNLOCK(wkq); |
| 6509 | msleep(&wkq->running, |
| 6510 | &wkq->core_mtx, |
| 6511 | 0, "wait for pkt", 0); |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 6512 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6513 | } |
| 6514 | |
| 6515 | static void |
| 6516 | sctp_startup_mcore_threads(void) |
| 6517 | { |
| 6518 | int i, cpu; |
| 6519 | |
| 6520 | if (mp_ncpus == 1) |
| 6521 | return; |
| 6522 | |
| 6523 | if (sctp_mcore_workers != NULL) { |
| 6524 | /* Already been here in some previous |
| 6525 | * vnet? |
| 6526 | */ |
| 6527 | return; |
| 6528 | } |
| 6529 | SCTP_MALLOC(sctp_mcore_workers, struct sctp_mcore_ctrl *, |
| 6530 | ((mp_maxid+1) * sizeof(struct sctp_mcore_ctrl)), |
| 6531 | SCTP_M_MCORE); |
| 6532 | if (sctp_mcore_workers == NULL) { |
| 6533 | /* TSNH I hope */ |
| 6534 | return; |
| 6535 | } |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 6536 | memset(sctp_mcore_workers, 0 , ((mp_maxid+1) * |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6537 | sizeof(struct sctp_mcore_ctrl))); |
| 6538 | /* Init the structures */ |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 6539 | for (i = 0; i<=mp_maxid; i++) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6540 | TAILQ_INIT(&sctp_mcore_workers[i].que); |
| 6541 | SCTP_MCORE_LOCK_INIT(&sctp_mcore_workers[i]); |
| 6542 | SCTP_MCORE_QLOCK_INIT(&sctp_mcore_workers[i]); |
| 6543 | sctp_mcore_workers[i].cpuid = i; |
| 6544 | } |
| 6545 | if (sctp_cpuarry == NULL) { |
| 6546 | SCTP_MALLOC(sctp_cpuarry, int *, |
| 6547 | (mp_ncpus * sizeof(int)), |
| 6548 | SCTP_M_MCORE); |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 6549 | i = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6550 | CPU_FOREACH(cpu) { |
| 6551 | sctp_cpuarry[i] = cpu; |
| 6552 | i++; |
| 6553 | } |
| 6554 | } |
| 6555 | |
| 6556 | /* Now start them all */ |
| 6557 | CPU_FOREACH(cpu) { |
| 6558 | #if __FreeBSD_version <= 701000 |
| 6559 | (void)kthread_create(sctp_mcore_thread, |
| 6560 | (void *)&sctp_mcore_workers[cpu], |
| 6561 | &sctp_mcore_workers[cpu].thread_proc, |
| 6562 | RFPROC, |
| 6563 | SCTP_KTHREAD_PAGES, |
| 6564 | SCTP_MCORE_NAME); |
| 6565 | |
| 6566 | #else |
| 6567 | (void)kproc_create(sctp_mcore_thread, |
| 6568 | (void *)&sctp_mcore_workers[cpu], |
| 6569 | &sctp_mcore_workers[cpu].thread_proc, |
| 6570 | RFPROC, |
| 6571 | SCTP_KTHREAD_PAGES, |
| 6572 | SCTP_MCORE_NAME); |
| 6573 | #endif |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 6574 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6575 | } |
| 6576 | } |
| 6577 | #endif |
Michael Tuexen | be8e0eb | 2019-03-24 00:11:02 +0100 | [diff] [blame] | 6578 | #if defined(__FreeBSD__) && __FreeBSD_cc_version >= 1400000 |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6579 | static struct mbuf * |
| 6580 | sctp_netisr_hdlr(struct mbuf *m, uintptr_t source) |
| 6581 | { |
| 6582 | struct ip *ip; |
| 6583 | struct sctphdr *sh; |
| 6584 | int offset; |
| 6585 | uint32_t flowid, tag; |
tuexen | 76eb209 | 2012-04-01 16:35:28 +0000 | [diff] [blame] | 6586 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6587 | /* |
| 6588 | * No flow id built by lower layers fix it so we |
| 6589 | * create one. |
| 6590 | */ |
| 6591 | ip = mtod(m, struct ip *); |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 6592 | offset = (ip->ip_hl << 2) + sizeof(struct sctphdr); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6593 | if (SCTP_BUF_LEN(m) < offset) { |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 6594 | if ((m = m_pullup(m, offset)) == NULL) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6595 | SCTP_STAT_INCR(sctps_hdrops); |
| 6596 | return (NULL); |
| 6597 | } |
| 6598 | ip = mtod(m, struct ip *); |
| 6599 | } |
| 6600 | sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6601 | tag = htonl(sh->v_tag); |
| 6602 | flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port); |
| 6603 | m->m_pkthdr.flowid = flowid; |
Michael Tuexen | fdcf790 | 2016-08-06 14:39:31 +0200 | [diff] [blame] | 6604 | /* FIX ME */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6605 | m->m_flags |= M_FLOWID; |
| 6606 | return (m); |
| 6607 | } |
| 6608 | #endif |
| 6609 | |
| 6610 | void |
Michael Tuexen | 843cc3b | 2019-08-31 15:25:03 +0200 | [diff] [blame] | 6611 | #if defined(__Userspace__) |
José Luis Millán | f6aa4b0 | 2019-08-12 22:54:25 +0200 | [diff] [blame] | 6612 | sctp_pcb_init(int start_threads) |
Michael Tuexen | 843cc3b | 2019-08-31 15:25:03 +0200 | [diff] [blame] | 6613 | #else |
| 6614 | sctp_pcb_init(void) |
| 6615 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6616 | { |
| 6617 | /* |
| 6618 | * SCTP initialization for the PCB structures should be called by |
Michael Tuexen | 34488e7 | 2016-05-03 22:11:59 +0200 | [diff] [blame] | 6619 | * the sctp_init() function. |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6620 | */ |
| 6621 | int i; |
| 6622 | struct timeval tv; |
tuexen | 6019b07 | 2011-12-15 18:38:03 +0000 | [diff] [blame] | 6623 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6624 | if (SCTP_BASE_VAR(sctp_pcb_initialized) != 0) { |
| 6625 | /* error I was called twice */ |
| 6626 | return; |
| 6627 | } |
| 6628 | SCTP_BASE_VAR(sctp_pcb_initialized) = 1; |
| 6629 | |
Michael Tuexen | cb2878b | 2015-11-13 12:59:40 +0100 | [diff] [blame] | 6630 | #if defined(SCTP_PROCESS_LEVEL_LOCKS) |
| 6631 | #if !defined(__Userspace_os_Windows) |
| 6632 | pthread_mutexattr_init(&SCTP_BASE_VAR(mtx_attr)); |
| 6633 | #ifdef INVARIANTS |
| 6634 | pthread_mutexattr_settype(&SCTP_BASE_VAR(mtx_attr), PTHREAD_MUTEX_ERRORCHECK); |
| 6635 | #endif |
| 6636 | #endif |
| 6637 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6638 | #if defined(SCTP_LOCAL_TRACE_BUF) |
| 6639 | #if defined(__Windows__) |
| 6640 | if (SCTP_BASE_SYSCTL(sctp_log) != NULL) { |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 6641 | memset(SCTP_BASE_SYSCTL(sctp_log), 0, sizeof(struct sctp_log)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6642 | } |
| 6643 | #else |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 6644 | memset(&SCTP_BASE_SYSCTL(sctp_log), 0, sizeof(struct sctp_log)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6645 | #endif |
| 6646 | #endif |
| 6647 | #if defined(__FreeBSD__) && defined(SMP) && defined(SCTP_USE_PERCPU_STAT) |
| 6648 | SCTP_MALLOC(SCTP_BASE_STATS, struct sctpstat *, |
| 6649 | ((mp_maxid+1) * sizeof(struct sctpstat)), |
| 6650 | SCTP_M_MCORE); |
| 6651 | #endif |
| 6652 | (void)SCTP_GETTIME_TIMEVAL(&tv); |
| 6653 | #if defined(__FreeBSD__) && defined(SMP) && defined(SCTP_USE_PERCPU_STAT) |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 6654 | memset(SCTP_BASE_STATS, 0, sizeof(struct sctpstat) * (mp_maxid+1)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6655 | SCTP_BASE_STATS[PCPU_GET(cpuid)].sctps_discontinuitytime.tv_sec = (uint32_t)tv.tv_sec; |
| 6656 | SCTP_BASE_STATS[PCPU_GET(cpuid)].sctps_discontinuitytime.tv_usec = (uint32_t)tv.tv_usec; |
| 6657 | #else |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 6658 | memset(&SCTP_BASE_STATS, 0, sizeof(struct sctpstat)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6659 | SCTP_BASE_STAT(sctps_discontinuitytime).tv_sec = (uint32_t)tv.tv_sec; |
| 6660 | SCTP_BASE_STAT(sctps_discontinuitytime).tv_usec = (uint32_t)tv.tv_usec; |
| 6661 | #endif |
| 6662 | /* init the empty list of (All) Endpoints */ |
| 6663 | LIST_INIT(&SCTP_BASE_INFO(listhead)); |
| 6664 | #if defined(__APPLE__) |
| 6665 | LIST_INIT(&SCTP_BASE_INFO(inplisthead)); |
t00fcxen | bc766ab | 2014-01-03 19:29:31 +0000 | [diff] [blame] | 6666 | #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) || defined(APPLE_LION) || defined(APPLE_MOUNTAINLION) |
| 6667 | SCTP_BASE_INFO(sctbinfo).listhead = &SCTP_BASE_INFO(inplisthead); |
| 6668 | SCTP_BASE_INFO(sctbinfo).mtx_grp_attr = lck_grp_attr_alloc_init(); |
| 6669 | lck_grp_attr_setdefault(SCTP_BASE_INFO(sctbinfo).mtx_grp_attr); |
| 6670 | SCTP_BASE_INFO(sctbinfo).mtx_grp = lck_grp_alloc_init("sctppcb", SCTP_BASE_INFO(sctbinfo).mtx_grp_attr); |
| 6671 | SCTP_BASE_INFO(sctbinfo).mtx_attr = lck_attr_alloc_init(); |
| 6672 | lck_attr_setdefault(SCTP_BASE_INFO(sctbinfo).mtx_attr); |
| 6673 | #else |
| 6674 | SCTP_BASE_INFO(sctbinfo).ipi_listhead = &SCTP_BASE_INFO(inplisthead); |
| 6675 | SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr = lck_grp_attr_alloc_init(); |
| 6676 | lck_grp_attr_setdefault(SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr); |
| 6677 | SCTP_BASE_INFO(sctbinfo).ipi_lock_grp = lck_grp_alloc_init("sctppcb", SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr); |
| 6678 | SCTP_BASE_INFO(sctbinfo).ipi_lock_attr = lck_attr_alloc_init(); |
| 6679 | lck_attr_setdefault(SCTP_BASE_INFO(sctbinfo).ipi_lock_attr); |
| 6680 | #endif |
| 6681 | #if !defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) && !defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION) |
| 6682 | SCTP_BASE_INFO(sctbinfo).ipi_gc = sctp_gc; |
| 6683 | in_pcbinfo_attach(&SCTP_BASE_INFO(sctbinfo)); |
| 6684 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6685 | #endif |
| 6686 | |
| 6687 | |
| 6688 | /* init the hash table of endpoints */ |
| 6689 | #if defined(__FreeBSD__) |
| 6690 | #if defined(__FreeBSD_cc_version) && __FreeBSD_cc_version >= 440000 |
| 6691 | TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", &SCTP_BASE_SYSCTL(sctp_hashtblsize)); |
| 6692 | TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", &SCTP_BASE_SYSCTL(sctp_pcbtblsize)); |
| 6693 | TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", &SCTP_BASE_SYSCTL(sctp_chunkscale)); |
| 6694 | #else |
| 6695 | TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", SCTP_TCBHASHSIZE, |
| 6696 | SCTP_BASE_SYSCTL(sctp_hashtblsize)); |
| 6697 | TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", SCTP_PCBHASHSIZE, |
| 6698 | SCTP_BASE_SYSCTL(sctp_pcbtblsize)); |
| 6699 | TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", SCTP_CHUNKQUEUE_SCALE, |
| 6700 | SCTP_BASE_SYSCTL(sctp_chunkscale)); |
| 6701 | #endif |
| 6702 | #endif |
| 6703 | SCTP_BASE_INFO(sctp_asochash) = SCTP_HASH_INIT((SCTP_BASE_SYSCTL(sctp_hashtblsize) * 31), |
| 6704 | &SCTP_BASE_INFO(hashasocmark)); |
| 6705 | SCTP_BASE_INFO(sctp_ephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize), |
| 6706 | &SCTP_BASE_INFO(hashmark)); |
| 6707 | SCTP_BASE_INFO(sctp_tcpephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize), |
| 6708 | &SCTP_BASE_INFO(hashtcpmark)); |
| 6709 | SCTP_BASE_INFO(hashtblsize) = SCTP_BASE_SYSCTL(sctp_hashtblsize); |
| 6710 | |
| 6711 | |
| 6712 | SCTP_BASE_INFO(sctp_vrfhash) = SCTP_HASH_INIT(SCTP_SIZE_OF_VRF_HASH, |
| 6713 | &SCTP_BASE_INFO(hashvrfmark)); |
| 6714 | |
| 6715 | SCTP_BASE_INFO(vrf_ifn_hash) = SCTP_HASH_INIT(SCTP_VRF_IFN_HASH_SIZE, |
| 6716 | &SCTP_BASE_INFO(vrf_ifn_hashmark)); |
| 6717 | /* init the zones */ |
| 6718 | /* |
| 6719 | * FIX ME: Should check for NULL returns, but if it does fail we are |
| 6720 | * doomed to panic anyways... add later maybe. |
| 6721 | */ |
| 6722 | SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_ep), "sctp_ep", |
| 6723 | sizeof(struct sctp_inpcb), maxsockets); |
| 6724 | |
| 6725 | SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asoc), "sctp_asoc", |
| 6726 | sizeof(struct sctp_tcb), sctp_max_number_of_assoc); |
| 6727 | |
| 6728 | SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_laddr), "sctp_laddr", |
| 6729 | sizeof(struct sctp_laddr), |
| 6730 | (sctp_max_number_of_assoc * sctp_scale_up_for_address)); |
| 6731 | |
| 6732 | SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_net), "sctp_raddr", |
| 6733 | sizeof(struct sctp_nets), |
| 6734 | (sctp_max_number_of_assoc * sctp_scale_up_for_address)); |
| 6735 | |
| 6736 | SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_chunk), "sctp_chunk", |
| 6737 | sizeof(struct sctp_tmit_chunk), |
| 6738 | (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale))); |
| 6739 | |
| 6740 | SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_readq), "sctp_readq", |
| 6741 | sizeof(struct sctp_queued_to_read), |
| 6742 | (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale))); |
| 6743 | |
| 6744 | SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_strmoq), "sctp_stream_msg_out", |
| 6745 | sizeof(struct sctp_stream_queue_pending), |
| 6746 | (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale))); |
| 6747 | |
| 6748 | SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf), "sctp_asconf", |
| 6749 | sizeof(struct sctp_asconf), |
| 6750 | (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale))); |
| 6751 | |
| 6752 | SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf_ack), "sctp_asconf_ack", |
| 6753 | sizeof(struct sctp_asconf_ack), |
| 6754 | (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale))); |
| 6755 | |
tuexen | 9784e9a | 2011-12-18 13:04:23 +0000 | [diff] [blame] | 6756 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6757 | /* Master Lock INIT for info structure */ |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6758 | SCTP_INP_INFO_LOCK_INIT(); |
| 6759 | SCTP_STATLOG_INIT_LOCK(); |
| 6760 | |
| 6761 | SCTP_IPI_COUNT_INIT(); |
| 6762 | SCTP_IPI_ADDR_INIT(); |
| 6763 | #ifdef SCTP_PACKET_LOGGING |
| 6764 | SCTP_IP_PKTLOG_INIT(); |
| 6765 | #endif |
| 6766 | LIST_INIT(&SCTP_BASE_INFO(addr_wq)); |
| 6767 | |
| 6768 | SCTP_WQ_ADDR_INIT(); |
| 6769 | /* not sure if we need all the counts */ |
| 6770 | SCTP_BASE_INFO(ipi_count_ep) = 0; |
| 6771 | /* assoc/tcb zone info */ |
| 6772 | SCTP_BASE_INFO(ipi_count_asoc) = 0; |
| 6773 | /* local addrlist zone info */ |
| 6774 | SCTP_BASE_INFO(ipi_count_laddr) = 0; |
| 6775 | /* remote addrlist zone info */ |
| 6776 | SCTP_BASE_INFO(ipi_count_raddr) = 0; |
| 6777 | /* chunk info */ |
| 6778 | SCTP_BASE_INFO(ipi_count_chunk) = 0; |
| 6779 | |
| 6780 | /* socket queue zone info */ |
| 6781 | SCTP_BASE_INFO(ipi_count_readq) = 0; |
| 6782 | |
| 6783 | /* stream out queue cont */ |
| 6784 | SCTP_BASE_INFO(ipi_count_strmoq) = 0; |
| 6785 | |
| 6786 | SCTP_BASE_INFO(ipi_free_strmoq) = 0; |
| 6787 | SCTP_BASE_INFO(ipi_free_chunks) = 0; |
| 6788 | |
| 6789 | SCTP_OS_TIMER_INIT(&SCTP_BASE_INFO(addr_wq_timer.timer)); |
| 6790 | |
| 6791 | /* Init the TIMEWAIT list */ |
| 6792 | for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) { |
| 6793 | LIST_INIT(&SCTP_BASE_INFO(vtag_timewait)[i]); |
| 6794 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6795 | #if defined(SCTP_PROCESS_LEVEL_LOCKS) |
tuexen | 5154a00 | 2011-12-23 18:47:55 +0000 | [diff] [blame] | 6796 | #if defined(__Userspace_os_Windows) |
tuexen | 6019b07 | 2011-12-15 18:38:03 +0000 | [diff] [blame] | 6797 | InitializeConditionVariable(&sctp_it_ctl.iterator_wakeup); |
| 6798 | #else |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6799 | (void)pthread_cond_init(&sctp_it_ctl.iterator_wakeup, NULL); |
| 6800 | #endif |
tuexen | 6019b07 | 2011-12-15 18:38:03 +0000 | [diff] [blame] | 6801 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6802 | sctp_startup_iterator(); |
| 6803 | |
| 6804 | #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP) |
| 6805 | sctp_startup_mcore_threads(); |
| 6806 | #endif |
| 6807 | |
| 6808 | #ifndef __Panda__ |
| 6809 | /* |
| 6810 | * INIT the default VRF which for BSD is the only one, other O/S's |
| 6811 | * may have more. But initially they must start with one and then |
| 6812 | * add the VRF's as addresses are added. |
| 6813 | */ |
| 6814 | sctp_init_vrf_list(SCTP_DEFAULT_VRF); |
| 6815 | #endif |
Michael Tuexen | be8e0eb | 2019-03-24 00:11:02 +0100 | [diff] [blame] | 6816 | #if defined(__FreeBSD__) && __FreeBSD_cc_version >= 1400000 |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6817 | if (ip_register_flow_handler(sctp_netisr_hdlr, IPPROTO_SCTP)) { |
tuexen | cb5fe8d | 2012-05-04 09:50:27 +0000 | [diff] [blame] | 6818 | SCTP_PRINTF("***SCTP- Error can't register netisr handler***\n"); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6819 | } |
| 6820 | #endif |
| 6821 | #if defined(_SCTP_NEEDS_CALLOUT_) || defined(_USER_SCTP_NEEDS_CALLOUT_) |
| 6822 | /* allocate the lock for the callout/timer queue */ |
| 6823 | SCTP_TIMERQ_LOCK_INIT(); |
| 6824 | TAILQ_INIT(&SCTP_BASE_INFO(callqueue)); |
| 6825 | #endif |
| 6826 | #if defined(__Userspace__) |
Michael Tuexen | 2f6478e | 2017-03-02 09:57:46 +0100 | [diff] [blame] | 6827 | mbuf_initialize(NULL); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6828 | atomic_init(); |
tuexen | c417897 | 2012-07-15 20:40:54 +0000 | [diff] [blame] | 6829 | #if defined(INET) || defined(INET6) |
José Luis Millán | f6aa4b0 | 2019-08-12 22:54:25 +0200 | [diff] [blame] | 6830 | if (start_threads) |
| 6831 | recv_thread_init(); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6832 | #endif |
tuexen | c417897 | 2012-07-15 20:40:54 +0000 | [diff] [blame] | 6833 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6834 | } |
| 6835 | |
| 6836 | /* |
| 6837 | * Assumes that the SCTP_BASE_INFO() lock is NOT held. |
| 6838 | */ |
| 6839 | void |
| 6840 | sctp_pcb_finish(void) |
| 6841 | { |
| 6842 | struct sctp_vrflist *vrf_bucket; |
| 6843 | struct sctp_vrf *vrf, *nvrf; |
| 6844 | struct sctp_ifn *ifn, *nifn; |
| 6845 | struct sctp_ifa *ifa, *nifa; |
| 6846 | struct sctpvtaghead *chain; |
| 6847 | struct sctp_tagblock *twait_block, *prev_twait_block; |
| 6848 | struct sctp_laddr *wi, *nwi; |
| 6849 | int i; |
t00fcxen | c793228 | 2013-11-16 14:57:41 +0000 | [diff] [blame] | 6850 | struct sctp_iterator *it, *nit; |
Michael Tuexen | 20785d4 | 2015-11-13 12:48:35 +0100 | [diff] [blame] | 6851 | |
Michael Tuexen | 71a83fb | 2016-02-16 21:52:58 +0100 | [diff] [blame] | 6852 | if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) { |
| 6853 | SCTP_PRINTF("%s: race condition on teardown.\n", __func__); |
| 6854 | return; |
| 6855 | } |
| 6856 | SCTP_BASE_VAR(sctp_pcb_initialized) = 0; |
t00fcxen | 9232a65 | 2013-11-30 09:34:24 +0000 | [diff] [blame] | 6857 | #if !defined(__FreeBSD__) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6858 | /* Notify the iterator to exit. */ |
| 6859 | SCTP_IPI_ITERATOR_WQ_LOCK(); |
| 6860 | sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_MUST_EXIT; |
| 6861 | sctp_wakeup_iterator(); |
| 6862 | SCTP_IPI_ITERATOR_WQ_UNLOCK(); |
| 6863 | #endif |
| 6864 | #if defined(__APPLE__) |
t00fcxen | bc766ab | 2014-01-03 19:29:31 +0000 | [diff] [blame] | 6865 | #if !defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) && !defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION) |
| 6866 | in_pcbinfo_detach(&SCTP_BASE_INFO(sctbinfo)); |
| 6867 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6868 | SCTP_IPI_ITERATOR_WQ_LOCK(); |
| 6869 | do { |
| 6870 | msleep(&sctp_it_ctl.iterator_flags, |
| 6871 | sctp_it_ctl.ipi_iterator_wq_mtx, |
| 6872 | 0, "waiting_for_work", 0); |
| 6873 | } while ((sctp_it_ctl.iterator_flags & SCTP_ITERATOR_EXITED) == 0); |
| 6874 | thread_deallocate(sctp_it_ctl.thread_proc); |
| 6875 | SCTP_IPI_ITERATOR_WQ_UNLOCK(); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6876 | #endif |
| 6877 | #if defined(__Windows__) |
| 6878 | if (sctp_it_ctl.iterator_thread_obj != NULL) { |
| 6879 | NTSTATUS status = STATUS_SUCCESS; |
| 6880 | |
| 6881 | KeSetEvent(&sctp_it_ctl.iterator_wakeup[1], IO_NO_INCREMENT, FALSE); |
| 6882 | status = KeWaitForSingleObject(sctp_it_ctl.iterator_thread_obj, |
| 6883 | Executive, |
| 6884 | KernelMode, |
| 6885 | FALSE, |
| 6886 | NULL); |
| 6887 | ObDereferenceObject(sctp_it_ctl.iterator_thread_obj); |
| 6888 | } |
| 6889 | #endif |
t00fcxen | 92a5ac4 | 2013-11-16 12:58:54 +0000 | [diff] [blame] | 6890 | #if defined(__Userspace__) |
Michael Tuexen | 3212d54 | 2020-03-16 12:30:04 +0100 | [diff] [blame] | 6891 | if (SCTP_BASE_VAR(iterator_thread_started)) { |
t00fcxen | 92a5ac4 | 2013-11-16 12:58:54 +0000 | [diff] [blame] | 6892 | #if defined(__Userspace_os_Windows) |
| 6893 | WaitForSingleObject(sctp_it_ctl.thread_proc, INFINITE); |
| 6894 | CloseHandle(sctp_it_ctl.thread_proc); |
| 6895 | sctp_it_ctl.thread_proc = NULL; |
| 6896 | #else |
| 6897 | pthread_join(sctp_it_ctl.thread_proc, NULL); |
| 6898 | sctp_it_ctl.thread_proc = 0; |
| 6899 | #endif |
| 6900 | } |
| 6901 | #endif |
| 6902 | #if defined(SCTP_PROCESS_LEVEL_LOCKS) |
| 6903 | #if defined(__Userspace_os_Windows) |
| 6904 | DeleteConditionVariable(&sctp_it_ctl.iterator_wakeup); |
| 6905 | #else |
| 6906 | pthread_cond_destroy(&sctp_it_ctl.iterator_wakeup); |
Michael Tuexen | 89e15a7 | 2015-11-13 12:42:55 +0100 | [diff] [blame] | 6907 | pthread_mutexattr_destroy(&SCTP_BASE_VAR(mtx_attr)); |
t00fcxen | 92a5ac4 | 2013-11-16 12:58:54 +0000 | [diff] [blame] | 6908 | #endif |
| 6909 | #endif |
t00fcxen | 9232a65 | 2013-11-30 09:34:24 +0000 | [diff] [blame] | 6910 | /* In FreeBSD the iterator thread never exits |
| 6911 | * but we do clean up. |
| 6912 | * The only way FreeBSD reaches here is if we have VRF's |
| 6913 | * but we still add the ifdef to make it compile on old versions. |
| 6914 | */ |
Michael Tuexen | 71a83fb | 2016-02-16 21:52:58 +0100 | [diff] [blame] | 6915 | #if defined(__FreeBSD__) |
| 6916 | retry: |
| 6917 | #endif |
t00fcxen | 9232a65 | 2013-11-30 09:34:24 +0000 | [diff] [blame] | 6918 | SCTP_IPI_ITERATOR_WQ_LOCK(); |
Michael Tuexen | 71a83fb | 2016-02-16 21:52:58 +0100 | [diff] [blame] | 6919 | #if defined(__FreeBSD__) |
| 6920 | /* |
| 6921 | * sctp_iterator_worker() might be working on an it entry without |
| 6922 | * holding the lock. We won't find it on the list either and |
| 6923 | * continue and free/destroy it. While holding the lock, spin, to |
| 6924 | * avoid the race condition as sctp_iterator_worker() will have to |
| 6925 | * wait to re-aquire the lock. |
| 6926 | */ |
| 6927 | if (sctp_it_ctl.iterator_running != 0 || sctp_it_ctl.cur_it != NULL) { |
| 6928 | SCTP_IPI_ITERATOR_WQ_UNLOCK(); |
| 6929 | SCTP_PRINTF("%s: Iterator running while we held the lock. Retry. " |
| 6930 | "cur_it=%p\n", __func__, sctp_it_ctl.cur_it); |
| 6931 | DELAY(10); |
| 6932 | goto retry; |
| 6933 | } |
| 6934 | #endif |
t00fcxen | 9232a65 | 2013-11-30 09:34:24 +0000 | [diff] [blame] | 6935 | TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) { |
t00fcxen | 433c6e1 | 2013-11-30 09:45:11 +0000 | [diff] [blame] | 6936 | #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 |
t00fcxen | 9232a65 | 2013-11-30 09:34:24 +0000 | [diff] [blame] | 6937 | if (it->vn != curvnet) { |
| 6938 | continue; |
| 6939 | } |
| 6940 | #endif |
| 6941 | TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr); |
| 6942 | if (it->function_atend != NULL) { |
| 6943 | (*it->function_atend) (it->pointer, it->val); |
| 6944 | } |
| 6945 | SCTP_FREE(it,SCTP_M_ITER); |
| 6946 | } |
| 6947 | SCTP_IPI_ITERATOR_WQ_UNLOCK(); |
t00fcxen | 433c6e1 | 2013-11-30 09:45:11 +0000 | [diff] [blame] | 6948 | #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 |
t00fcxen | 9232a65 | 2013-11-30 09:34:24 +0000 | [diff] [blame] | 6949 | SCTP_ITERATOR_LOCK(); |
| 6950 | if ((sctp_it_ctl.cur_it) && |
| 6951 | (sctp_it_ctl.cur_it->vn == curvnet)) { |
| 6952 | sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_IT; |
| 6953 | } |
| 6954 | SCTP_ITERATOR_UNLOCK(); |
| 6955 | #endif |
t00fcxen | c793228 | 2013-11-16 14:57:41 +0000 | [diff] [blame] | 6956 | #if !defined(__FreeBSD__) |
t00fcxen | 92a5ac4 | 2013-11-16 12:58:54 +0000 | [diff] [blame] | 6957 | SCTP_IPI_ITERATOR_WQ_DESTROY(); |
t00fcxen | 8dd95b8 | 2014-07-11 20:41:28 +0000 | [diff] [blame] | 6958 | SCTP_ITERATOR_LOCK_DESTROY(); |
t00fcxen | c793228 | 2013-11-16 14:57:41 +0000 | [diff] [blame] | 6959 | #endif |
Michael Tuexen | 71a83fb | 2016-02-16 21:52:58 +0100 | [diff] [blame] | 6960 | SCTP_OS_TIMER_STOP_DRAIN(&SCTP_BASE_INFO(addr_wq_timer.timer)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6961 | SCTP_WQ_ADDR_LOCK(); |
| 6962 | LIST_FOREACH_SAFE(wi, &SCTP_BASE_INFO(addr_wq), sctp_nxt_addr, nwi) { |
| 6963 | LIST_REMOVE(wi, sctp_nxt_addr); |
| 6964 | SCTP_DECR_LADDR_COUNT(); |
t00fcxen | 9892b7f | 2014-09-06 20:06:39 +0000 | [diff] [blame] | 6965 | if (wi->action == SCTP_DEL_IP_ADDRESS) { |
| 6966 | SCTP_FREE(wi->ifa, SCTP_M_IFA); |
| 6967 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6968 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), wi); |
| 6969 | } |
| 6970 | SCTP_WQ_ADDR_UNLOCK(); |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 6971 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 6972 | /* |
| 6973 | * free the vrf/ifn/ifa lists and hashes (be sure address monitor |
| 6974 | * is destroyed first). |
| 6975 | */ |
| 6976 | vrf_bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(SCTP_DEFAULT_VRFID & SCTP_BASE_INFO(hashvrfmark))]; |
| 6977 | LIST_FOREACH_SAFE(vrf, vrf_bucket, next_vrf, nvrf) { |
| 6978 | LIST_FOREACH_SAFE(ifn, &vrf->ifnlist, next_ifn, nifn) { |
| 6979 | LIST_FOREACH_SAFE(ifa, &ifn->ifalist, next_ifa, nifa) { |
| 6980 | /* free the ifa */ |
| 6981 | LIST_REMOVE(ifa, next_bucket); |
| 6982 | LIST_REMOVE(ifa, next_ifa); |
| 6983 | SCTP_FREE(ifa, SCTP_M_IFA); |
| 6984 | } |
| 6985 | /* free the ifn */ |
| 6986 | LIST_REMOVE(ifn, next_bucket); |
| 6987 | LIST_REMOVE(ifn, next_ifn); |
| 6988 | SCTP_FREE(ifn, SCTP_M_IFN); |
| 6989 | } |
| 6990 | SCTP_HASH_FREE(vrf->vrf_addr_hash, vrf->vrf_addr_hashmark); |
| 6991 | /* free the vrf */ |
| 6992 | LIST_REMOVE(vrf, next_vrf); |
| 6993 | SCTP_FREE(vrf, SCTP_M_VRF); |
| 6994 | } |
| 6995 | /* free the vrf hashes */ |
| 6996 | SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_vrfhash), SCTP_BASE_INFO(hashvrfmark)); |
| 6997 | 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] | 6998 | |
| 6999 | /* free the TIMEWAIT list elements malloc'd in the function |
| 7000 | * sctp_add_vtag_to_timewait()... |
| 7001 | */ |
| 7002 | for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) { |
| 7003 | chain = &SCTP_BASE_INFO(vtag_timewait)[i]; |
| 7004 | if (!LIST_EMPTY(chain)) { |
| 7005 | prev_twait_block = NULL; |
| 7006 | LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) { |
| 7007 | if (prev_twait_block) { |
| 7008 | SCTP_FREE(prev_twait_block, SCTP_M_TIMW); |
| 7009 | } |
| 7010 | prev_twait_block = twait_block; |
| 7011 | } |
| 7012 | SCTP_FREE(prev_twait_block, SCTP_M_TIMW); |
| 7013 | } |
| 7014 | } |
| 7015 | |
| 7016 | /* free the locks and mutexes */ |
| 7017 | #if defined(__APPLE__) |
| 7018 | SCTP_TIMERQ_LOCK_DESTROY(); |
| 7019 | #endif |
| 7020 | #ifdef SCTP_PACKET_LOGGING |
| 7021 | SCTP_IP_PKTLOG_DESTROY(); |
| 7022 | #endif |
| 7023 | SCTP_IPI_ADDR_DESTROY(); |
| 7024 | #if defined(__APPLE__) |
| 7025 | SCTP_IPI_COUNT_DESTROY(); |
| 7026 | #endif |
| 7027 | SCTP_STATLOG_DESTROY(); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7028 | SCTP_INP_INFO_LOCK_DESTROY(); |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 7029 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7030 | SCTP_WQ_ADDR_DESTROY(); |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 7031 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7032 | #if defined(__APPLE__) |
t00fcxen | bc766ab | 2014-01-03 19:29:31 +0000 | [diff] [blame] | 7033 | #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) || defined(APPLE_LION) || defined(APPLE_MOUNTAINLION) |
| 7034 | lck_grp_attr_free(SCTP_BASE_INFO(sctbinfo).mtx_grp_attr); |
| 7035 | lck_grp_free(SCTP_BASE_INFO(sctbinfo).mtx_grp); |
| 7036 | lck_attr_free(SCTP_BASE_INFO(sctbinfo).mtx_attr); |
| 7037 | #else |
| 7038 | lck_grp_attr_free(SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr); |
| 7039 | lck_grp_free(SCTP_BASE_INFO(sctbinfo).ipi_lock_grp); |
| 7040 | lck_attr_free(SCTP_BASE_INFO(sctbinfo).ipi_lock_attr); |
| 7041 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7042 | #endif |
| 7043 | #if defined(__Userspace__) |
| 7044 | SCTP_TIMERQ_LOCK_DESTROY(); |
| 7045 | SCTP_ZONE_DESTROY(zone_mbuf); |
| 7046 | SCTP_ZONE_DESTROY(zone_clust); |
| 7047 | SCTP_ZONE_DESTROY(zone_ext_refcnt); |
| 7048 | #endif |
Michael Tuexen | 71a83fb | 2016-02-16 21:52:58 +0100 | [diff] [blame] | 7049 | /* Get rid of other stuff too. */ |
| 7050 | if (SCTP_BASE_INFO(sctp_asochash) != NULL) |
| 7051 | SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_asochash), SCTP_BASE_INFO(hashasocmark)); |
| 7052 | if (SCTP_BASE_INFO(sctp_ephash) != NULL) |
| 7053 | SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_ephash), SCTP_BASE_INFO(hashmark)); |
| 7054 | if (SCTP_BASE_INFO(sctp_tcpephash) != NULL) |
| 7055 | SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_tcpephash), SCTP_BASE_INFO(hashtcpmark)); |
| 7056 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7057 | #if defined(__Windows__) || defined(__FreeBSD__) || defined(__Userspace__) |
| 7058 | SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_ep)); |
| 7059 | SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asoc)); |
| 7060 | SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_laddr)); |
| 7061 | SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_net)); |
| 7062 | SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_chunk)); |
| 7063 | SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_readq)); |
| 7064 | SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_strmoq)); |
| 7065 | SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf)); |
| 7066 | SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf_ack)); |
| 7067 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7068 | #if defined(__FreeBSD__) && defined(SMP) && defined(SCTP_USE_PERCPU_STAT) |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 7069 | SCTP_FREE(SCTP_BASE_STATS, SCTP_M_MCORE); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7070 | #endif |
| 7071 | } |
| 7072 | |
| 7073 | |
| 7074 | int |
| 7075 | sctp_load_addresses_from_init(struct sctp_tcb *stcb, struct mbuf *m, |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 7076 | int offset, int limit, |
| 7077 | struct sockaddr *src, struct sockaddr *dst, |
Michael Tuexen | 923f735 | 2016-05-01 23:55:35 +0200 | [diff] [blame] | 7078 | struct sockaddr *altsa, uint16_t port) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7079 | { |
| 7080 | /* |
| 7081 | * grub through the INIT pulling addresses and loading them to the |
| 7082 | * nets structure in the asoc. The from address in the mbuf should |
| 7083 | * also be loaded (if it is not already). This routine can be called |
| 7084 | * with either INIT or INIT-ACK's as long as the m points to the IP |
| 7085 | * packet and the offset points to the beginning of the parameters. |
| 7086 | */ |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 7087 | struct sctp_inpcb *inp; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7088 | struct sctp_nets *net, *nnet, *net_tmp; |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 7089 | struct sctp_paramhdr *phdr, param_buf; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7090 | struct sctp_tcb *stcb_tmp; |
| 7091 | uint16_t ptype, plen; |
| 7092 | struct sockaddr *sa; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7093 | uint8_t random_store[SCTP_PARAM_BUFFER_SIZE]; |
| 7094 | struct sctp_auth_random *p_random = NULL; |
| 7095 | uint16_t random_len = 0; |
| 7096 | uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE]; |
| 7097 | struct sctp_auth_hmac_algo *hmacs = NULL; |
| 7098 | uint16_t hmacs_len = 0; |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 7099 | uint8_t saw_asconf = 0; |
| 7100 | uint8_t saw_asconf_ack = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7101 | uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE]; |
| 7102 | struct sctp_auth_chunk_list *chunks = NULL; |
| 7103 | uint16_t num_chunks = 0; |
| 7104 | sctp_key_t *new_key; |
| 7105 | uint32_t keylen; |
| 7106 | int got_random = 0, got_hmacs = 0, got_chklist = 0; |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7107 | uint8_t peer_supports_ecn; |
| 7108 | uint8_t peer_supports_prsctp; |
| 7109 | uint8_t peer_supports_auth; |
| 7110 | uint8_t peer_supports_asconf; |
| 7111 | uint8_t peer_supports_asconf_ack; |
| 7112 | uint8_t peer_supports_reconfig; |
| 7113 | uint8_t peer_supports_nrsack; |
| 7114 | uint8_t peer_supports_pktdrop; |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7115 | uint8_t peer_supports_idata; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7116 | #ifdef INET |
| 7117 | struct sockaddr_in sin; |
| 7118 | #endif |
| 7119 | #ifdef INET6 |
| 7120 | struct sockaddr_in6 sin6; |
| 7121 | #endif |
| 7122 | |
| 7123 | /* First get the destination address setup too. */ |
| 7124 | #ifdef INET |
| 7125 | memset(&sin, 0, sizeof(sin)); |
| 7126 | sin.sin_family = AF_INET; |
t00fcxen | 8fbaee3 | 2012-09-04 16:41:39 +0000 | [diff] [blame] | 7127 | #ifdef HAVE_SIN_LEN |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7128 | sin.sin_len = sizeof(sin); |
| 7129 | #endif |
| 7130 | sin.sin_port = stcb->rport; |
| 7131 | #endif |
| 7132 | #ifdef INET6 |
| 7133 | memset(&sin6, 0, sizeof(sin6)); |
| 7134 | sin6.sin6_family = AF_INET6; |
t00fcxen | 8fbaee3 | 2012-09-04 16:41:39 +0000 | [diff] [blame] | 7135 | #ifdef HAVE_SIN6_LEN |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7136 | sin6.sin6_len = sizeof(struct sockaddr_in6); |
| 7137 | #endif |
| 7138 | sin6.sin6_port = stcb->rport; |
| 7139 | #endif |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 7140 | if (altsa) { |
| 7141 | sa = altsa; |
| 7142 | } else { |
| 7143 | sa = src; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7144 | } |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7145 | peer_supports_idata = 0; |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7146 | peer_supports_ecn = 0; |
| 7147 | peer_supports_prsctp = 0; |
| 7148 | peer_supports_auth = 0; |
| 7149 | peer_supports_asconf = 0; |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7150 | peer_supports_reconfig = 0; |
| 7151 | peer_supports_nrsack = 0; |
| 7152 | peer_supports_pktdrop = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7153 | TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { |
| 7154 | /* mark all addresses that we have currently on the list */ |
| 7155 | net->dest_state |= SCTP_ADDR_NOT_IN_ASSOC; |
| 7156 | } |
| 7157 | /* does the source address already exist? if so skip it */ |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 7158 | inp = stcb->sctp_ep; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7159 | atomic_add_int(&stcb->asoc.refcnt, 1); |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 7160 | stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net_tmp, dst, stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7161 | atomic_add_int(&stcb->asoc.refcnt, -1); |
| 7162 | |
| 7163 | if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || inp == NULL) { |
| 7164 | /* we must add the source address */ |
| 7165 | /* no scope set here since we have a tcb already. */ |
| 7166 | switch (sa->sa_family) { |
| 7167 | #ifdef INET |
| 7168 | case AF_INET: |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 7169 | if (stcb->asoc.scope.ipv4_addr_legal) { |
Michael Tuexen | 923f735 | 2016-05-01 23:55:35 +0200 | [diff] [blame] | 7170 | 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] | 7171 | return (-1); |
| 7172 | } |
| 7173 | } |
| 7174 | break; |
| 7175 | #endif |
| 7176 | #ifdef INET6 |
| 7177 | case AF_INET6: |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 7178 | if (stcb->asoc.scope.ipv6_addr_legal) { |
Michael Tuexen | 923f735 | 2016-05-01 23:55:35 +0200 | [diff] [blame] | 7179 | 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] | 7180 | return (-2); |
| 7181 | } |
| 7182 | } |
| 7183 | break; |
| 7184 | #endif |
tuexen | 430ca6c | 2012-07-10 23:36:36 +0000 | [diff] [blame] | 7185 | #if defined(__Userspace__) |
| 7186 | case AF_CONN: |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 7187 | if (stcb->asoc.scope.conn_addr_legal) { |
Michael Tuexen | 923f735 | 2016-05-01 23:55:35 +0200 | [diff] [blame] | 7188 | 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] | 7189 | return (-2); |
| 7190 | } |
tuexen | 430ca6c | 2012-07-10 23:36:36 +0000 | [diff] [blame] | 7191 | } |
| 7192 | break; |
| 7193 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7194 | default: |
| 7195 | break; |
| 7196 | } |
| 7197 | } else { |
| 7198 | if (net_tmp != NULL && stcb_tmp == stcb) { |
| 7199 | net_tmp->dest_state &= ~SCTP_ADDR_NOT_IN_ASSOC; |
| 7200 | } else if (stcb_tmp != stcb) { |
| 7201 | /* It belongs to another association? */ |
| 7202 | if (stcb_tmp) |
| 7203 | SCTP_TCB_UNLOCK(stcb_tmp); |
| 7204 | return (-3); |
| 7205 | } |
| 7206 | } |
| 7207 | if (stcb->asoc.state == 0) { |
| 7208 | /* the assoc was freed? */ |
| 7209 | return (-4); |
| 7210 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7211 | /* now we must go through each of the params. */ |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 7212 | phdr = sctp_get_next_param(m, offset, ¶m_buf, sizeof(param_buf)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7213 | while (phdr) { |
| 7214 | ptype = ntohs(phdr->param_type); |
| 7215 | plen = ntohs(phdr->param_length); |
| 7216 | /* |
tuexen | cb5fe8d | 2012-05-04 09:50:27 +0000 | [diff] [blame] | 7217 | * SCTP_PRINTF("ptype => %0x, plen => %d\n", (uint32_t)ptype, |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7218 | * (int)plen); |
| 7219 | */ |
| 7220 | if (offset + plen > limit) { |
| 7221 | break; |
| 7222 | } |
Michael Tuexen | 790a7a2 | 2019-12-20 17:02:02 +0100 | [diff] [blame] | 7223 | if (plen < sizeof(struct sctp_paramhdr)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7224 | break; |
| 7225 | } |
| 7226 | #ifdef INET |
| 7227 | if (ptype == SCTP_IPV4_ADDRESS) { |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 7228 | if (stcb->asoc.scope.ipv4_addr_legal) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7229 | struct sctp_ipv4addr_param *p4, p4_buf; |
| 7230 | |
| 7231 | /* ok get the v4 address and check/add */ |
| 7232 | phdr = sctp_get_next_param(m, offset, |
| 7233 | (struct sctp_paramhdr *)&p4_buf, |
| 7234 | sizeof(p4_buf)); |
| 7235 | if (plen != sizeof(struct sctp_ipv4addr_param) || |
| 7236 | phdr == NULL) { |
| 7237 | return (-5); |
| 7238 | } |
| 7239 | p4 = (struct sctp_ipv4addr_param *)phdr; |
| 7240 | sin.sin_addr.s_addr = p4->addr; |
| 7241 | if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) { |
| 7242 | /* Skip multi-cast addresses */ |
| 7243 | goto next_param; |
| 7244 | } |
| 7245 | if ((sin.sin_addr.s_addr == INADDR_BROADCAST) || |
| 7246 | (sin.sin_addr.s_addr == INADDR_ANY)) { |
| 7247 | goto next_param; |
| 7248 | } |
| 7249 | sa = (struct sockaddr *)&sin; |
| 7250 | inp = stcb->sctp_ep; |
| 7251 | atomic_add_int(&stcb->asoc.refcnt, 1); |
| 7252 | stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net, |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 7253 | dst, stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7254 | atomic_add_int(&stcb->asoc.refcnt, -1); |
| 7255 | |
| 7256 | if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || |
| 7257 | inp == NULL) { |
| 7258 | /* we must add the source address */ |
| 7259 | /* |
| 7260 | * no scope set since we have a tcb |
| 7261 | * already |
| 7262 | */ |
| 7263 | |
| 7264 | /* |
| 7265 | * we must validate the state again |
| 7266 | * here |
| 7267 | */ |
| 7268 | add_it_now: |
| 7269 | if (stcb->asoc.state == 0) { |
| 7270 | /* the assoc was freed? */ |
| 7271 | return (-7); |
| 7272 | } |
Michael Tuexen | 923f735 | 2016-05-01 23:55:35 +0200 | [diff] [blame] | 7273 | 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] | 7274 | return (-8); |
| 7275 | } |
| 7276 | } else if (stcb_tmp == stcb) { |
| 7277 | if (stcb->asoc.state == 0) { |
| 7278 | /* the assoc was freed? */ |
| 7279 | return (-10); |
| 7280 | } |
| 7281 | if (net != NULL) { |
| 7282 | /* clear flag */ |
| 7283 | net->dest_state &= |
| 7284 | ~SCTP_ADDR_NOT_IN_ASSOC; |
| 7285 | } |
| 7286 | } else { |
| 7287 | /* |
| 7288 | * strange, address is in another |
| 7289 | * assoc? straighten out locks. |
| 7290 | */ |
| 7291 | if (stcb_tmp) { |
Michael Tuexen | 348a36c | 2018-08-13 16:24:47 +0200 | [diff] [blame] | 7292 | if (SCTP_GET_STATE(stcb_tmp) == SCTP_STATE_COOKIE_WAIT) { |
Michael Tüxen | ac14228 | 2015-07-28 00:42:03 +0200 | [diff] [blame] | 7293 | struct mbuf *op_err; |
| 7294 | char msg[SCTP_DIAG_INFO_LEN]; |
| 7295 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7296 | /* in setup state we abort this guy */ |
Michael Tuexen | edd369d | 2020-05-19 09:42:15 +0200 | [diff] [blame] | 7297 | SCTP_SNPRINTF(msg, sizeof(msg), |
| 7298 | "%s:%d at %s", __FILE__, __LINE__, __func__); |
Michael Tüxen | ac14228 | 2015-07-28 00:42:03 +0200 | [diff] [blame] | 7299 | op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), |
| 7300 | msg); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7301 | sctp_abort_an_association(stcb_tmp->sctp_ep, |
Michael Tüxen | ac14228 | 2015-07-28 00:42:03 +0200 | [diff] [blame] | 7302 | stcb_tmp, op_err, |
| 7303 | SCTP_SO_NOT_LOCKED); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7304 | goto add_it_now; |
| 7305 | } |
| 7306 | SCTP_TCB_UNLOCK(stcb_tmp); |
| 7307 | } |
| 7308 | |
| 7309 | if (stcb->asoc.state == 0) { |
| 7310 | /* the assoc was freed? */ |
| 7311 | return (-12); |
| 7312 | } |
| 7313 | return (-13); |
| 7314 | } |
| 7315 | } |
| 7316 | } else |
| 7317 | #endif |
| 7318 | #ifdef INET6 |
| 7319 | if (ptype == SCTP_IPV6_ADDRESS) { |
t00fcxen | d0ad16b | 2013-02-09 18:34:24 +0000 | [diff] [blame] | 7320 | if (stcb->asoc.scope.ipv6_addr_legal) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7321 | /* ok get the v6 address and check/add */ |
| 7322 | struct sctp_ipv6addr_param *p6, p6_buf; |
| 7323 | |
| 7324 | phdr = sctp_get_next_param(m, offset, |
| 7325 | (struct sctp_paramhdr *)&p6_buf, |
| 7326 | sizeof(p6_buf)); |
| 7327 | if (plen != sizeof(struct sctp_ipv6addr_param) || |
| 7328 | phdr == NULL) { |
| 7329 | return (-14); |
| 7330 | } |
| 7331 | p6 = (struct sctp_ipv6addr_param *)phdr; |
| 7332 | memcpy((caddr_t)&sin6.sin6_addr, p6->addr, |
| 7333 | sizeof(p6->addr)); |
| 7334 | if (IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) { |
| 7335 | /* Skip multi-cast addresses */ |
| 7336 | goto next_param; |
| 7337 | } |
| 7338 | if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) { |
| 7339 | /* Link local make no sense without scope */ |
| 7340 | goto next_param; |
| 7341 | } |
| 7342 | sa = (struct sockaddr *)&sin6; |
| 7343 | inp = stcb->sctp_ep; |
| 7344 | atomic_add_int(&stcb->asoc.refcnt, 1); |
| 7345 | stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net, |
tuexen | 664967a | 2012-06-28 16:24:36 +0000 | [diff] [blame] | 7346 | dst, stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7347 | atomic_add_int(&stcb->asoc.refcnt, -1); |
| 7348 | if (stcb_tmp == NULL && |
| 7349 | (inp == stcb->sctp_ep || inp == NULL)) { |
| 7350 | /* |
| 7351 | * we must validate the state again |
| 7352 | * here |
| 7353 | */ |
| 7354 | add_it_now6: |
| 7355 | if (stcb->asoc.state == 0) { |
| 7356 | /* the assoc was freed? */ |
| 7357 | return (-16); |
| 7358 | } |
| 7359 | /* |
| 7360 | * we must add the address, no scope |
| 7361 | * set |
| 7362 | */ |
Michael Tuexen | 923f735 | 2016-05-01 23:55:35 +0200 | [diff] [blame] | 7363 | 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] | 7364 | return (-17); |
| 7365 | } |
| 7366 | } else if (stcb_tmp == stcb) { |
| 7367 | /* |
| 7368 | * we must validate the state again |
| 7369 | * here |
| 7370 | */ |
| 7371 | if (stcb->asoc.state == 0) { |
| 7372 | /* the assoc was freed? */ |
| 7373 | return (-19); |
| 7374 | } |
| 7375 | if (net != NULL) { |
| 7376 | /* clear flag */ |
| 7377 | net->dest_state &= |
| 7378 | ~SCTP_ADDR_NOT_IN_ASSOC; |
| 7379 | } |
| 7380 | } else { |
| 7381 | /* |
| 7382 | * strange, address is in another |
| 7383 | * assoc? straighten out locks. |
| 7384 | */ |
Michael Tüxen | ac14228 | 2015-07-28 00:42:03 +0200 | [diff] [blame] | 7385 | if (stcb_tmp) { |
Michael Tuexen | 348a36c | 2018-08-13 16:24:47 +0200 | [diff] [blame] | 7386 | if (SCTP_GET_STATE(stcb_tmp) == SCTP_STATE_COOKIE_WAIT) { |
Michael Tüxen | ac14228 | 2015-07-28 00:42:03 +0200 | [diff] [blame] | 7387 | struct mbuf *op_err; |
| 7388 | char msg[SCTP_DIAG_INFO_LEN]; |
| 7389 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7390 | /* in setup state we abort this guy */ |
Michael Tuexen | edd369d | 2020-05-19 09:42:15 +0200 | [diff] [blame] | 7391 | SCTP_SNPRINTF(msg, sizeof(msg), |
| 7392 | "%s:%d at %s", __FILE__, __LINE__, __func__); |
Michael Tüxen | ac14228 | 2015-07-28 00:42:03 +0200 | [diff] [blame] | 7393 | op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), |
| 7394 | msg); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7395 | sctp_abort_an_association(stcb_tmp->sctp_ep, |
Michael Tüxen | ac14228 | 2015-07-28 00:42:03 +0200 | [diff] [blame] | 7396 | stcb_tmp, op_err, |
| 7397 | SCTP_SO_NOT_LOCKED); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7398 | goto add_it_now6; |
| 7399 | } |
Michael Tüxen | ac14228 | 2015-07-28 00:42:03 +0200 | [diff] [blame] | 7400 | SCTP_TCB_UNLOCK(stcb_tmp); |
| 7401 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7402 | if (stcb->asoc.state == 0) { |
| 7403 | /* the assoc was freed? */ |
| 7404 | return (-21); |
| 7405 | } |
| 7406 | return (-22); |
| 7407 | } |
| 7408 | } |
| 7409 | } else |
| 7410 | #endif |
| 7411 | if (ptype == SCTP_ECN_CAPABLE) { |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7412 | peer_supports_ecn = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7413 | } else if (ptype == SCTP_ULP_ADAPTATION) { |
| 7414 | if (stcb->asoc.state != SCTP_STATE_OPEN) { |
| 7415 | struct sctp_adaptation_layer_indication ai, *aip; |
| 7416 | |
| 7417 | phdr = sctp_get_next_param(m, offset, |
| 7418 | (struct sctp_paramhdr *)&ai, sizeof(ai)); |
| 7419 | aip = (struct sctp_adaptation_layer_indication *)phdr; |
| 7420 | if (aip) { |
| 7421 | stcb->asoc.peers_adaptation = ntohl(aip->indication); |
| 7422 | stcb->asoc.adaptation_needed = 1; |
| 7423 | } |
| 7424 | } |
| 7425 | } else if (ptype == SCTP_SET_PRIM_ADDR) { |
| 7426 | struct sctp_asconf_addr_param lstore, *fee; |
| 7427 | int lptype; |
| 7428 | struct sockaddr *lsa = NULL; |
| 7429 | #ifdef INET |
| 7430 | struct sctp_asconf_addrv4_param *fii; |
| 7431 | #endif |
| 7432 | |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7433 | if (stcb->asoc.asconf_supported == 0) { |
| 7434 | return (-100); |
| 7435 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7436 | if (plen > sizeof(lstore)) { |
| 7437 | return (-23); |
| 7438 | } |
Michael Tuexen | 790a7a2 | 2019-12-20 17:02:02 +0100 | [diff] [blame] | 7439 | if (plen < sizeof(struct sctp_asconf_addrv4_param)) { |
| 7440 | return (-101); |
| 7441 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7442 | phdr = sctp_get_next_param(m, offset, |
| 7443 | (struct sctp_paramhdr *)&lstore, |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 7444 | plen); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7445 | if (phdr == NULL) { |
| 7446 | return (-24); |
| 7447 | } |
| 7448 | fee = (struct sctp_asconf_addr_param *)phdr; |
| 7449 | lptype = ntohs(fee->addrp.ph.param_type); |
| 7450 | switch (lptype) { |
| 7451 | #ifdef INET |
| 7452 | case SCTP_IPV4_ADDRESS: |
| 7453 | if (plen != |
| 7454 | sizeof(struct sctp_asconf_addrv4_param)) { |
| 7455 | SCTP_PRINTF("Sizeof setprim in init/init ack not %d but %d - ignored\n", |
| 7456 | (int)sizeof(struct sctp_asconf_addrv4_param), |
| 7457 | plen); |
| 7458 | } else { |
| 7459 | fii = (struct sctp_asconf_addrv4_param *)fee; |
| 7460 | sin.sin_addr.s_addr = fii->addrp.addr; |
| 7461 | lsa = (struct sockaddr *)&sin; |
| 7462 | } |
| 7463 | break; |
| 7464 | #endif |
| 7465 | #ifdef INET6 |
| 7466 | case SCTP_IPV6_ADDRESS: |
| 7467 | if (plen != |
| 7468 | sizeof(struct sctp_asconf_addr_param)) { |
| 7469 | SCTP_PRINTF("Sizeof setprim (v6) in init/init ack not %d but %d - ignored\n", |
| 7470 | (int)sizeof(struct sctp_asconf_addr_param), |
| 7471 | plen); |
| 7472 | } else { |
| 7473 | memcpy(sin6.sin6_addr.s6_addr, |
| 7474 | fee->addrp.addr, |
| 7475 | sizeof(fee->addrp.addr)); |
| 7476 | lsa = (struct sockaddr *)&sin6; |
| 7477 | } |
| 7478 | break; |
| 7479 | #endif |
| 7480 | default: |
| 7481 | break; |
| 7482 | } |
| 7483 | if (lsa) { |
| 7484 | (void)sctp_set_primary_addr(stcb, sa, NULL); |
| 7485 | } |
| 7486 | } else if (ptype == SCTP_HAS_NAT_SUPPORT) { |
| 7487 | stcb->asoc.peer_supports_nat = 1; |
| 7488 | } else if (ptype == SCTP_PRSCTP_SUPPORTED) { |
| 7489 | /* Peer supports pr-sctp */ |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7490 | peer_supports_prsctp = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7491 | } else if (ptype == SCTP_SUPPORTED_CHUNK_EXT) { |
| 7492 | /* A supported extension chunk */ |
| 7493 | struct sctp_supported_chunk_types_param *pr_supported; |
| 7494 | uint8_t local_store[SCTP_PARAM_BUFFER_SIZE]; |
| 7495 | int num_ent, i; |
| 7496 | |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 7497 | if (plen > sizeof(local_store)) { |
| 7498 | return (-35); |
| 7499 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7500 | phdr = sctp_get_next_param(m, offset, |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 7501 | (struct sctp_paramhdr *)&local_store, plen); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7502 | if (phdr == NULL) { |
| 7503 | return (-25); |
| 7504 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7505 | pr_supported = (struct sctp_supported_chunk_types_param *)phdr; |
| 7506 | num_ent = plen - sizeof(struct sctp_paramhdr); |
| 7507 | for (i = 0; i < num_ent; i++) { |
| 7508 | switch (pr_supported->chunk_types[i]) { |
| 7509 | case SCTP_ASCONF: |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7510 | peer_supports_asconf = 1; |
t00fcxen | 10c9698 | 2014-12-17 20:37:01 +0000 | [diff] [blame] | 7511 | break; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7512 | case SCTP_ASCONF_ACK: |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7513 | peer_supports_asconf_ack = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7514 | break; |
| 7515 | case SCTP_FORWARD_CUM_TSN: |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7516 | peer_supports_prsctp = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7517 | break; |
| 7518 | case SCTP_PACKET_DROPPED: |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7519 | peer_supports_pktdrop = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7520 | break; |
| 7521 | case SCTP_NR_SELECTIVE_ACK: |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7522 | peer_supports_nrsack = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7523 | break; |
| 7524 | case SCTP_STREAM_RESET: |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7525 | peer_supports_reconfig = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7526 | break; |
| 7527 | case SCTP_AUTHENTICATION: |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7528 | peer_supports_auth = 1; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7529 | break; |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7530 | case SCTP_IDATA: |
| 7531 | peer_supports_idata = 1; |
| 7532 | break; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7533 | default: |
| 7534 | /* one I have not learned yet */ |
| 7535 | break; |
| 7536 | |
| 7537 | } |
| 7538 | } |
| 7539 | } else if (ptype == SCTP_RANDOM) { |
| 7540 | if (plen > sizeof(random_store)) |
| 7541 | break; |
| 7542 | if (got_random) { |
| 7543 | /* already processed a RANDOM */ |
| 7544 | goto next_param; |
| 7545 | } |
| 7546 | phdr = sctp_get_next_param(m, offset, |
| 7547 | (struct sctp_paramhdr *)random_store, |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 7548 | plen); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7549 | if (phdr == NULL) |
| 7550 | return (-26); |
| 7551 | p_random = (struct sctp_auth_random *)phdr; |
| 7552 | random_len = plen - sizeof(*p_random); |
| 7553 | /* enforce the random length */ |
| 7554 | if (random_len != SCTP_AUTH_RANDOM_SIZE_REQUIRED) { |
| 7555 | SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: invalid RANDOM len\n"); |
| 7556 | return (-27); |
| 7557 | } |
| 7558 | got_random = 1; |
| 7559 | } else if (ptype == SCTP_HMAC_LIST) { |
t00fcxen | 05d0bb9 | 2014-09-16 14:25:27 +0000 | [diff] [blame] | 7560 | uint16_t num_hmacs; |
| 7561 | uint16_t i; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7562 | |
| 7563 | if (plen > sizeof(hmacs_store)) |
| 7564 | break; |
| 7565 | if (got_hmacs) { |
| 7566 | /* already processed a HMAC list */ |
| 7567 | goto next_param; |
| 7568 | } |
| 7569 | phdr = sctp_get_next_param(m, offset, |
| 7570 | (struct sctp_paramhdr *)hmacs_store, |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 7571 | plen); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7572 | if (phdr == NULL) |
| 7573 | return (-28); |
| 7574 | hmacs = (struct sctp_auth_hmac_algo *)phdr; |
| 7575 | hmacs_len = plen - sizeof(*hmacs); |
| 7576 | num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]); |
| 7577 | /* validate the hmac list */ |
| 7578 | if (sctp_verify_hmac_param(hmacs, num_hmacs)) { |
| 7579 | return (-29); |
| 7580 | } |
| 7581 | if (stcb->asoc.peer_hmacs != NULL) |
| 7582 | sctp_free_hmaclist(stcb->asoc.peer_hmacs); |
| 7583 | stcb->asoc.peer_hmacs = sctp_alloc_hmaclist(num_hmacs); |
| 7584 | if (stcb->asoc.peer_hmacs != NULL) { |
| 7585 | for (i = 0; i < num_hmacs; i++) { |
| 7586 | (void)sctp_auth_add_hmacid(stcb->asoc.peer_hmacs, |
| 7587 | ntohs(hmacs->hmac_ids[i])); |
| 7588 | } |
| 7589 | } |
| 7590 | got_hmacs = 1; |
| 7591 | } else if (ptype == SCTP_CHUNK_LIST) { |
| 7592 | int i; |
| 7593 | |
| 7594 | if (plen > sizeof(chunks_store)) |
| 7595 | break; |
| 7596 | if (got_chklist) { |
| 7597 | /* already processed a Chunks list */ |
| 7598 | goto next_param; |
| 7599 | } |
| 7600 | phdr = sctp_get_next_param(m, offset, |
| 7601 | (struct sctp_paramhdr *)chunks_store, |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 7602 | plen); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7603 | if (phdr == NULL) |
| 7604 | return (-30); |
| 7605 | chunks = (struct sctp_auth_chunk_list *)phdr; |
| 7606 | num_chunks = plen - sizeof(*chunks); |
| 7607 | if (stcb->asoc.peer_auth_chunks != NULL) |
| 7608 | sctp_clear_chunklist(stcb->asoc.peer_auth_chunks); |
| 7609 | else |
| 7610 | stcb->asoc.peer_auth_chunks = sctp_alloc_chunklist(); |
| 7611 | for (i = 0; i < num_chunks; i++) { |
| 7612 | (void)sctp_auth_add_chunk(chunks->chunk_types[i], |
| 7613 | stcb->asoc.peer_auth_chunks); |
| 7614 | /* record asconf/asconf-ack if listed */ |
| 7615 | if (chunks->chunk_types[i] == SCTP_ASCONF) |
| 7616 | saw_asconf = 1; |
| 7617 | if (chunks->chunk_types[i] == SCTP_ASCONF_ACK) |
| 7618 | saw_asconf_ack = 1; |
| 7619 | |
| 7620 | } |
| 7621 | got_chklist = 1; |
| 7622 | } else if ((ptype == SCTP_HEARTBEAT_INFO) || |
| 7623 | (ptype == SCTP_STATE_COOKIE) || |
| 7624 | (ptype == SCTP_UNRECOG_PARAM) || |
| 7625 | (ptype == SCTP_COOKIE_PRESERVE) || |
| 7626 | (ptype == SCTP_SUPPORTED_ADDRTYPE) || |
| 7627 | (ptype == SCTP_ADD_IP_ADDRESS) || |
| 7628 | (ptype == SCTP_DEL_IP_ADDRESS) || |
| 7629 | (ptype == SCTP_ERROR_CAUSE_IND) || |
| 7630 | (ptype == SCTP_SUCCESS_REPORT)) { |
| 7631 | /* don't care */ ; |
| 7632 | } else { |
| 7633 | if ((ptype & 0x8000) == 0x0000) { |
| 7634 | /* |
| 7635 | * must stop processing the rest of the |
| 7636 | * param's. Any report bits were handled |
| 7637 | * with the call to |
| 7638 | * sctp_arethere_unrecognized_parameters() |
| 7639 | * when the INIT or INIT-ACK was first seen. |
| 7640 | */ |
| 7641 | break; |
| 7642 | } |
| 7643 | } |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 7644 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7645 | next_param: |
| 7646 | offset += SCTP_SIZE32(plen); |
| 7647 | if (offset >= limit) { |
| 7648 | break; |
| 7649 | } |
Michael Tuexen | 473225f | 2017-06-23 23:05:43 +0200 | [diff] [blame] | 7650 | phdr = sctp_get_next_param(m, offset, ¶m_buf, |
| 7651 | sizeof(param_buf)); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7652 | } |
| 7653 | /* Now check to see if we need to purge any addresses */ |
| 7654 | TAILQ_FOREACH_SAFE(net, &stcb->asoc.nets, sctp_next, nnet) { |
| 7655 | if ((net->dest_state & SCTP_ADDR_NOT_IN_ASSOC) == |
| 7656 | SCTP_ADDR_NOT_IN_ASSOC) { |
| 7657 | /* This address has been removed from the asoc */ |
| 7658 | /* remove and free it */ |
| 7659 | stcb->asoc.numnets--; |
| 7660 | TAILQ_REMOVE(&stcb->asoc.nets, net, sctp_next); |
| 7661 | sctp_free_remote_addr(net); |
| 7662 | if (net == stcb->asoc.primary_destination) { |
| 7663 | stcb->asoc.primary_destination = NULL; |
| 7664 | sctp_select_primary_destination(stcb); |
| 7665 | } |
| 7666 | } |
| 7667 | } |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7668 | if ((stcb->asoc.ecn_supported == 1) && |
| 7669 | (peer_supports_ecn == 0)) { |
| 7670 | stcb->asoc.ecn_supported = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7671 | } |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7672 | if ((stcb->asoc.prsctp_supported == 1) && |
| 7673 | (peer_supports_prsctp == 0)) { |
| 7674 | stcb->asoc.prsctp_supported = 0; |
| 7675 | } |
| 7676 | if ((stcb->asoc.auth_supported == 1) && |
| 7677 | ((peer_supports_auth == 0) || |
| 7678 | (got_random == 0) || (got_hmacs == 0))) { |
| 7679 | stcb->asoc.auth_supported = 0; |
| 7680 | } |
| 7681 | if ((stcb->asoc.asconf_supported == 1) && |
| 7682 | ((peer_supports_asconf == 0) || (peer_supports_asconf_ack == 0) || |
| 7683 | (stcb->asoc.auth_supported == 0) || |
| 7684 | (saw_asconf == 0) || (saw_asconf_ack == 0))) { |
| 7685 | stcb->asoc.asconf_supported = 0; |
| 7686 | } |
| 7687 | if ((stcb->asoc.reconfig_supported == 1) && |
| 7688 | (peer_supports_reconfig == 0)) { |
| 7689 | stcb->asoc.reconfig_supported = 0; |
| 7690 | } |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7691 | if ((stcb->asoc.idata_supported == 1) && |
| 7692 | (peer_supports_idata == 0)) { |
| 7693 | stcb->asoc.idata_supported = 0; |
| 7694 | } |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7695 | if ((stcb->asoc.nrsack_supported == 1) && |
| 7696 | (peer_supports_nrsack == 0)) { |
| 7697 | stcb->asoc.nrsack_supported = 0; |
| 7698 | } |
| 7699 | if ((stcb->asoc.pktdrop_supported == 1) && |
| 7700 | (peer_supports_pktdrop == 0)){ |
| 7701 | stcb->asoc.pktdrop_supported = 0; |
| 7702 | } |
| 7703 | /* validate authentication required parameters */ |
| 7704 | if ((peer_supports_auth == 0) && (got_chklist == 1)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7705 | /* peer does not support auth but sent a chunks list? */ |
| 7706 | return (-31); |
| 7707 | } |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7708 | if ((peer_supports_asconf == 1) && (peer_supports_auth == 0)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7709 | /* peer supports asconf but not auth? */ |
| 7710 | return (-32); |
t00fcxen | aac0f45 | 2014-08-12 12:51:45 +0000 | [diff] [blame] | 7711 | } else if ((peer_supports_asconf == 1) && |
| 7712 | (peer_supports_auth == 1) && |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7713 | ((saw_asconf == 0) || (saw_asconf_ack == 0))) { |
| 7714 | return (-33); |
| 7715 | } |
| 7716 | /* concatenate the full random key */ |
| 7717 | keylen = sizeof(*p_random) + random_len + sizeof(*hmacs) + hmacs_len; |
| 7718 | if (chunks != NULL) { |
| 7719 | keylen += sizeof(*chunks) + num_chunks; |
| 7720 | } |
| 7721 | new_key = sctp_alloc_key(keylen); |
| 7722 | if (new_key != NULL) { |
| 7723 | /* copy in the RANDOM */ |
| 7724 | if (p_random != NULL) { |
| 7725 | keylen = sizeof(*p_random) + random_len; |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 7726 | memcpy(new_key->key, p_random, keylen); |
Michael Tuexen | 8789a6d | 2018-06-02 18:33:47 +0200 | [diff] [blame] | 7727 | } else { |
| 7728 | keylen = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7729 | } |
| 7730 | /* append in the AUTH chunks */ |
| 7731 | if (chunks != NULL) { |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 7732 | memcpy(new_key->key + keylen, chunks, |
| 7733 | sizeof(*chunks) + num_chunks); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7734 | keylen += sizeof(*chunks) + num_chunks; |
| 7735 | } |
| 7736 | /* append in the HMACs */ |
| 7737 | if (hmacs != NULL) { |
Michael Tuexen | d085330 | 2017-07-19 17:01:31 +0200 | [diff] [blame] | 7738 | memcpy(new_key->key + keylen, hmacs, |
| 7739 | sizeof(*hmacs) + hmacs_len); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7740 | } |
| 7741 | } else { |
| 7742 | /* failed to get memory for the key */ |
| 7743 | return (-34); |
| 7744 | } |
| 7745 | if (stcb->asoc.authinfo.peer_random != NULL) |
| 7746 | sctp_free_key(stcb->asoc.authinfo.peer_random); |
| 7747 | stcb->asoc.authinfo.peer_random = new_key; |
| 7748 | sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid); |
| 7749 | sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid); |
| 7750 | |
| 7751 | return (0); |
| 7752 | } |
| 7753 | |
| 7754 | int |
| 7755 | sctp_set_primary_addr(struct sctp_tcb *stcb, struct sockaddr *sa, |
| 7756 | struct sctp_nets *net) |
| 7757 | { |
| 7758 | /* make sure the requested primary address exists in the assoc */ |
| 7759 | if (net == NULL && sa) |
| 7760 | net = sctp_findnet(stcb, sa); |
| 7761 | |
| 7762 | if (net == NULL) { |
| 7763 | /* didn't find the requested primary address! */ |
| 7764 | return (-1); |
| 7765 | } else { |
| 7766 | /* set the primary address */ |
| 7767 | if (net->dest_state & SCTP_ADDR_UNCONFIRMED) { |
| 7768 | /* Must be confirmed, so queue to set */ |
| 7769 | net->dest_state |= SCTP_ADDR_REQ_PRIMARY; |
| 7770 | return (0); |
| 7771 | } |
| 7772 | stcb->asoc.primary_destination = net; |
| 7773 | if (!(net->dest_state & SCTP_ADDR_PF) && (stcb->asoc.alternate)) { |
| 7774 | sctp_free_remote_addr(stcb->asoc.alternate); |
| 7775 | stcb->asoc.alternate = NULL; |
| 7776 | } |
| 7777 | net = TAILQ_FIRST(&stcb->asoc.nets); |
| 7778 | if (net != stcb->asoc.primary_destination) { |
| 7779 | /* first one on the list is NOT the primary |
| 7780 | * sctp_cmpaddr() is much more efficient if |
| 7781 | * the primary is the first on the list, make it |
| 7782 | * so. |
| 7783 | */ |
| 7784 | TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next); |
| 7785 | TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next); |
| 7786 | } |
| 7787 | return (0); |
| 7788 | } |
| 7789 | } |
| 7790 | |
| 7791 | int |
tuexen | 9784e9a | 2011-12-18 13:04:23 +0000 | [diff] [blame] | 7792 | 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] | 7793 | { |
| 7794 | /* |
| 7795 | * This function serves two purposes. It will see if a TAG can be |
| 7796 | * re-used and return 1 for yes it is ok and 0 for don't use that |
| 7797 | * tag. A secondary function it will do is purge out old tags that |
| 7798 | * can be removed. |
| 7799 | */ |
| 7800 | struct sctpvtaghead *chain; |
| 7801 | struct sctp_tagblock *twait_block; |
| 7802 | struct sctpasochead *head; |
| 7803 | struct sctp_tcb *stcb; |
| 7804 | int i; |
| 7805 | |
| 7806 | SCTP_INP_INFO_RLOCK(); |
| 7807 | head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag, |
| 7808 | SCTP_BASE_INFO(hashasocmark))]; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7809 | LIST_FOREACH(stcb, head, sctp_asocs) { |
| 7810 | /* We choose not to lock anything here. TCB's can't be |
| 7811 | * removed since we have the read lock, so they can't |
| 7812 | * be freed on us, same thing for the INP. I may |
| 7813 | * be wrong with this assumption, but we will go |
| 7814 | * with it for now :-) |
| 7815 | */ |
| 7816 | if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
| 7817 | continue; |
| 7818 | } |
| 7819 | if (stcb->asoc.my_vtag == tag) { |
| 7820 | /* candidate */ |
| 7821 | if (stcb->rport != rport) { |
| 7822 | continue; |
| 7823 | } |
| 7824 | if (stcb->sctp_ep->sctp_lport != lport) { |
| 7825 | continue; |
| 7826 | } |
| 7827 | /* Its a used tag set */ |
| 7828 | SCTP_INP_INFO_RUNLOCK(); |
| 7829 | return (0); |
| 7830 | } |
| 7831 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7832 | chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)]; |
| 7833 | /* Now what about timed wait ? */ |
t00fcxen | 14f8fbb | 2013-06-25 09:32:47 +0000 | [diff] [blame] | 7834 | LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7835 | /* |
| 7836 | * Block(s) are present, lets see if we have this tag in the |
| 7837 | * list |
| 7838 | */ |
t00fcxen | 14f8fbb | 2013-06-25 09:32:47 +0000 | [diff] [blame] | 7839 | for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) { |
| 7840 | if (twait_block->vtag_block[i].v_tag == 0) { |
| 7841 | /* not used */ |
| 7842 | continue; |
| 7843 | } else if ((long)twait_block->vtag_block[i].tv_sec_at_expire < |
| 7844 | now->tv_sec) { |
| 7845 | /* Audit expires this guy */ |
| 7846 | twait_block->vtag_block[i].tv_sec_at_expire = 0; |
| 7847 | twait_block->vtag_block[i].v_tag = 0; |
| 7848 | twait_block->vtag_block[i].lport = 0; |
| 7849 | twait_block->vtag_block[i].rport = 0; |
| 7850 | } else if ((twait_block->vtag_block[i].v_tag == tag) && |
| 7851 | (twait_block->vtag_block[i].lport == lport) && |
| 7852 | (twait_block->vtag_block[i].rport == rport)) { |
| 7853 | /* Bad tag, sorry :< */ |
| 7854 | SCTP_INP_INFO_RUNLOCK(); |
| 7855 | return (0); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7856 | } |
| 7857 | } |
| 7858 | } |
| 7859 | SCTP_INP_INFO_RUNLOCK(); |
| 7860 | return (1); |
| 7861 | } |
| 7862 | |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7863 | static void |
tuexen | 9784e9a | 2011-12-18 13:04:23 +0000 | [diff] [blame] | 7864 | sctp_drain_mbufs(struct sctp_tcb *stcb) |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7865 | { |
| 7866 | /* |
| 7867 | * We must hunt this association for MBUF's past the cumack (i.e. |
| 7868 | * out of order data that we can renege on). |
| 7869 | */ |
| 7870 | struct sctp_association *asoc; |
| 7871 | struct sctp_tmit_chunk *chk, *nchk; |
| 7872 | uint32_t cumulative_tsn_p1; |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7873 | struct sctp_queued_to_read *control, *ncontrol; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7874 | int cnt, strmat; |
| 7875 | uint32_t gap, i; |
tuexen | 63fc0bb | 2011-12-27 12:24:52 +0000 | [diff] [blame] | 7876 | int fnd = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7877 | |
| 7878 | /* We look for anything larger than the cum-ack + 1 */ |
| 7879 | |
| 7880 | asoc = &stcb->asoc; |
| 7881 | if (asoc->cumulative_tsn == asoc->highest_tsn_inside_map) { |
| 7882 | /* none we can reneg on. */ |
| 7883 | return; |
| 7884 | } |
| 7885 | SCTP_STAT_INCR(sctps_protocol_drains_done); |
| 7886 | cumulative_tsn_p1 = asoc->cumulative_tsn + 1; |
| 7887 | cnt = 0; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7888 | /* Ok that was fun, now we will drain all the inbound streams? */ |
| 7889 | for (strmat = 0; strmat < asoc->streamincnt; strmat++) { |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7890 | TAILQ_FOREACH_SAFE(control, &asoc->strmin[strmat].inqueue, next_instrm, ncontrol) { |
Michael Tuexen | fdcf790 | 2016-08-06 14:39:31 +0200 | [diff] [blame] | 7891 | #ifdef INVARIANTS |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7892 | if (control->on_strm_q != SCTP_ON_ORDERED ) { |
Michael Tuexen | fdcf790 | 2016-08-06 14:39:31 +0200 | [diff] [blame] | 7893 | panic("Huh control: %p on_q: %d -- not ordered?", |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7894 | control, control->on_strm_q); |
Michael Tuexen | fdcf790 | 2016-08-06 14:39:31 +0200 | [diff] [blame] | 7895 | } |
| 7896 | #endif |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7897 | if (SCTP_TSN_GT(control->sinfo_tsn, cumulative_tsn_p1)) { |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7898 | /* Yep it is above cum-ack */ |
| 7899 | cnt++; |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7900 | SCTP_CALC_TSN_TO_GAP(gap, control->sinfo_tsn, asoc->mapping_array_base_tsn); |
| 7901 | KASSERT(control->length > 0, ("control has zero length")); |
| 7902 | if (asoc->size_on_all_streams >= control->length) { |
| 7903 | asoc->size_on_all_streams -= control->length; |
| 7904 | } else { |
| 7905 | #ifdef INVARIANTS |
| 7906 | panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length); |
| 7907 | #else |
| 7908 | asoc->size_on_all_streams = 0; |
| 7909 | #endif |
| 7910 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7911 | sctp_ucount_decr(asoc->cnt_on_all_streams); |
| 7912 | SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap); |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7913 | if (control->on_read_q) { |
| 7914 | TAILQ_REMOVE(&stcb->sctp_ep->read_queue, control, next); |
| 7915 | control->on_read_q = 0; |
Michael Tuexen | fdcf790 | 2016-08-06 14:39:31 +0200 | [diff] [blame] | 7916 | } |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7917 | TAILQ_REMOVE(&asoc->strmin[strmat].inqueue, control, next_instrm); |
| 7918 | control->on_strm_q = 0; |
| 7919 | if (control->data) { |
| 7920 | sctp_m_freem(control->data); |
| 7921 | control->data = NULL; |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 7922 | } |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7923 | sctp_free_remote_addr(control->whoFrom); |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7924 | /* Now its reasm? */ |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7925 | TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) { |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7926 | cnt++; |
Michael Tuexen | 00657ac | 2016-12-07 21:53:26 +0100 | [diff] [blame] | 7927 | 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] | 7928 | KASSERT(chk->send_size > 0, ("chunk has zero length")); |
| 7929 | if (asoc->size_on_reasm_queue >= chk->send_size) { |
| 7930 | asoc->size_on_reasm_queue -= chk->send_size; |
| 7931 | } else { |
| 7932 | #ifdef INVARIANTS |
| 7933 | panic("size_on_reasm_queue = %u smaller than chunk length %u", asoc->size_on_reasm_queue, chk->send_size); |
| 7934 | #else |
| 7935 | asoc->size_on_reasm_queue = 0; |
| 7936 | #endif |
| 7937 | } |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7938 | sctp_ucount_decr(asoc->cnt_on_reasm_queue); |
| 7939 | SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap); |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7940 | TAILQ_REMOVE(&control->reasm, chk, sctp_next); |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7941 | if (chk->data) { |
| 7942 | sctp_m_freem(chk->data); |
| 7943 | chk->data = NULL; |
| 7944 | } |
| 7945 | sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); |
| 7946 | } |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7947 | sctp_free_a_readq(stcb, control); |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7948 | } |
| 7949 | } |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7950 | TAILQ_FOREACH_SAFE(control, &asoc->strmin[strmat].uno_inqueue, next_instrm, ncontrol) { |
Michael Tuexen | fdcf790 | 2016-08-06 14:39:31 +0200 | [diff] [blame] | 7951 | #ifdef INVARIANTS |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7952 | if (control->on_strm_q != SCTP_ON_UNORDERED ) { |
Michael Tuexen | fdcf790 | 2016-08-06 14:39:31 +0200 | [diff] [blame] | 7953 | panic("Huh control: %p on_q: %d -- not unordered?", |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7954 | control, control->on_strm_q); |
Michael Tuexen | fdcf790 | 2016-08-06 14:39:31 +0200 | [diff] [blame] | 7955 | } |
| 7956 | #endif |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7957 | if (SCTP_TSN_GT(control->sinfo_tsn, cumulative_tsn_p1)) { |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7958 | /* Yep it is above cum-ack */ |
| 7959 | cnt++; |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7960 | SCTP_CALC_TSN_TO_GAP(gap, control->sinfo_tsn, asoc->mapping_array_base_tsn); |
| 7961 | KASSERT(control->length > 0, ("control has zero length")); |
| 7962 | if (asoc->size_on_all_streams >= control->length) { |
| 7963 | asoc->size_on_all_streams -= control->length; |
| 7964 | } else { |
| 7965 | #ifdef INVARIANTS |
| 7966 | panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length); |
| 7967 | #else |
| 7968 | asoc->size_on_all_streams = 0; |
| 7969 | #endif |
| 7970 | } |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7971 | sctp_ucount_decr(asoc->cnt_on_all_streams); |
| 7972 | SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap); |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7973 | if (control->on_read_q) { |
| 7974 | TAILQ_REMOVE(&stcb->sctp_ep->read_queue, control, next); |
| 7975 | control->on_read_q = 0; |
Michael Tuexen | fdcf790 | 2016-08-06 14:39:31 +0200 | [diff] [blame] | 7976 | } |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7977 | TAILQ_REMOVE(&asoc->strmin[strmat].uno_inqueue, control, next_instrm); |
| 7978 | control->on_strm_q = 0; |
| 7979 | if (control->data) { |
| 7980 | sctp_m_freem(control->data); |
| 7981 | control->data = NULL; |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7982 | } |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7983 | sctp_free_remote_addr(control->whoFrom); |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7984 | /* Now its reasm? */ |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 7985 | TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) { |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7986 | cnt++; |
Michael Tuexen | 00657ac | 2016-12-07 21:53:26 +0100 | [diff] [blame] | 7987 | 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] | 7988 | KASSERT(chk->send_size > 0, ("chunk has zero length")); |
| 7989 | if (asoc->size_on_reasm_queue >= chk->send_size) { |
| 7990 | asoc->size_on_reasm_queue -= chk->send_size; |
| 7991 | } else { |
| 7992 | #ifdef INVARIANTS |
| 7993 | panic("size_on_reasm_queue = %u smaller than chunk length %u", asoc->size_on_reasm_queue, chk->send_size); |
| 7994 | #else |
| 7995 | asoc->size_on_reasm_queue = 0; |
| 7996 | #endif |
| 7997 | } |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 7998 | sctp_ucount_decr(asoc->cnt_on_reasm_queue); |
| 7999 | SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap); |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 8000 | TAILQ_REMOVE(&control->reasm, chk, sctp_next); |
Michael Tuexen | e500195 | 2016-04-17 19:25:27 +0200 | [diff] [blame] | 8001 | if (chk->data) { |
| 8002 | sctp_m_freem(chk->data); |
| 8003 | chk->data = NULL; |
| 8004 | } |
| 8005 | sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); |
| 8006 | } |
Michael Tuexen | be5e3e7 | 2017-07-19 14:44:48 +0200 | [diff] [blame] | 8007 | sctp_free_a_readq(stcb, control); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 8008 | } |
| 8009 | } |
| 8010 | } |
| 8011 | if (cnt) { |
| 8012 | /* We must back down to see what the new highest is */ |
| 8013 | for (i = asoc->highest_tsn_inside_map; SCTP_TSN_GE(i, asoc->mapping_array_base_tsn); i--) { |
| 8014 | SCTP_CALC_TSN_TO_GAP(gap, i, asoc->mapping_array_base_tsn); |
| 8015 | if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) { |
| 8016 | asoc->highest_tsn_inside_map = i; |
| 8017 | fnd = 1; |
| 8018 | break; |
| 8019 | } |
| 8020 | } |
| 8021 | if (!fnd) { |
| 8022 | asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn - 1; |
| 8023 | } |
| 8024 | |
| 8025 | /* |
| 8026 | * Question, should we go through the delivery queue? The only |
| 8027 | * reason things are on here is the app not reading OR a p-d-api up. |
| 8028 | * An attacker COULD send enough in to initiate the PD-API and then |
| 8029 | * send a bunch of stuff to other streams... these would wind up on |
| 8030 | * the delivery queue.. and then we would not get to them. But in |
| 8031 | * order to do this I then have to back-track and un-deliver |
| 8032 | * sequence numbers in streams.. el-yucko. I think for now we will |
| 8033 | * NOT look at the delivery queue and leave it to be something to |
| 8034 | * consider later. An alternative would be to abort the P-D-API with |
| 8035 | * a notification and then deliver the data.... Or another method |
| 8036 | * might be to keep track of how many times the situation occurs and |
| 8037 | * if we see a possible attack underway just abort the association. |
| 8038 | */ |
| 8039 | #ifdef SCTP_DEBUG |
| 8040 | SCTPDBG(SCTP_DEBUG_PCB1, "Freed %d chunks from reneg harvest\n", cnt); |
| 8041 | #endif |
| 8042 | /* |
| 8043 | * Now do we need to find a new |
| 8044 | * asoc->highest_tsn_inside_map? |
| 8045 | */ |
| 8046 | asoc->last_revoke_count = cnt; |
Michael Tuexen | 61f6683 | 2020-02-09 23:15:19 +0100 | [diff] [blame] | 8047 | sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL, |
Michael Tuexen | 21b55df | 2020-02-09 23:46:32 +0100 | [diff] [blame] | 8048 | SCTP_FROM_SCTP_PCB + SCTP_LOC_11); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 8049 | /*sa_ignore NO_NULL_CHK*/ |
| 8050 | sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED); |
| 8051 | 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] | 8052 | } |
| 8053 | /* |
| 8054 | * Another issue, in un-setting the TSN's in the mapping array we |
| 8055 | * DID NOT adjust the highest_tsn marker. This will cause one of two |
| 8056 | * things to occur. It may cause us to do extra work in checking for |
| 8057 | * our mapping array movement. More importantly it may cause us to |
| 8058 | * SACK every datagram. This may not be a bad thing though since we |
| 8059 | * will recover once we get our cum-ack above and all this stuff we |
| 8060 | * dumped recovered. |
| 8061 | */ |
| 8062 | } |
| 8063 | |
| 8064 | void |
| 8065 | sctp_drain() |
| 8066 | { |
| 8067 | /* |
| 8068 | * We must walk the PCB lists for ALL associations here. The system |
| 8069 | * is LOW on MBUF's and needs help. This is where reneging will |
| 8070 | * occur. We really hope this does NOT happen! |
| 8071 | */ |
| 8072 | #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 |
| 8073 | VNET_ITERATOR_DECL(vnet_iter); |
| 8074 | #else |
| 8075 | struct sctp_inpcb *inp; |
| 8076 | struct sctp_tcb *stcb; |
| 8077 | |
| 8078 | SCTP_STAT_INCR(sctps_protocol_drain_calls); |
| 8079 | if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) { |
| 8080 | return; |
| 8081 | } |
| 8082 | #endif |
| 8083 | #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 |
| 8084 | VNET_LIST_RLOCK_NOSLEEP(); |
| 8085 | VNET_FOREACH(vnet_iter) { |
| 8086 | CURVNET_SET(vnet_iter); |
| 8087 | struct sctp_inpcb *inp; |
| 8088 | struct sctp_tcb *stcb; |
| 8089 | #endif |
| 8090 | |
| 8091 | #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 |
| 8092 | SCTP_STAT_INCR(sctps_protocol_drain_calls); |
| 8093 | if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) { |
| 8094 | #ifdef VIMAGE |
| 8095 | continue; |
| 8096 | #else |
| 8097 | return; |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 8098 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 8099 | } |
| 8100 | #endif |
| 8101 | SCTP_INP_INFO_RLOCK(); |
| 8102 | LIST_FOREACH(inp, &SCTP_BASE_INFO(listhead), sctp_list) { |
| 8103 | /* For each endpoint */ |
| 8104 | SCTP_INP_RLOCK(inp); |
| 8105 | LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { |
| 8106 | /* For each association */ |
| 8107 | SCTP_TCB_LOCK(stcb); |
tuexen | 9784e9a | 2011-12-18 13:04:23 +0000 | [diff] [blame] | 8108 | sctp_drain_mbufs(stcb); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 8109 | SCTP_TCB_UNLOCK(stcb); |
| 8110 | } |
| 8111 | SCTP_INP_RUNLOCK(inp); |
| 8112 | } |
| 8113 | SCTP_INP_INFO_RUNLOCK(); |
| 8114 | #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 |
| 8115 | CURVNET_RESTORE(); |
| 8116 | } |
| 8117 | VNET_LIST_RUNLOCK_NOSLEEP(); |
| 8118 | #endif |
| 8119 | } |
| 8120 | |
| 8121 | /* |
| 8122 | * start a new iterator |
| 8123 | * iterates through all endpoints and associations based on the pcb_state |
| 8124 | * flags and asoc_state. "af" (mandatory) is executed for all matching |
| 8125 | * assocs and "ef" (optional) is executed when the iterator completes. |
| 8126 | * "inpf" (optional) is executed for each new endpoint as it is being |
| 8127 | * iterated through. inpe (optional) is called when the inp completes |
| 8128 | * its way through all the stcbs. |
| 8129 | */ |
| 8130 | int |
| 8131 | sctp_initiate_iterator(inp_func inpf, |
| 8132 | asoc_func af, |
| 8133 | inp_func inpe, |
| 8134 | uint32_t pcb_state, |
| 8135 | uint32_t pcb_features, |
| 8136 | uint32_t asoc_state, |
| 8137 | void *argp, |
| 8138 | uint32_t argi, |
| 8139 | end_func ef, |
| 8140 | struct sctp_inpcb *s_inp, |
| 8141 | uint8_t chunk_output_off) |
| 8142 | { |
| 8143 | struct sctp_iterator *it = NULL; |
| 8144 | |
| 8145 | if (af == NULL) { |
| 8146 | return (-1); |
| 8147 | } |
Michael Tuexen | 71a83fb | 2016-02-16 21:52:58 +0100 | [diff] [blame] | 8148 | if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) { |
| 8149 | SCTP_PRINTF("%s: abort on initialize being %d\n", __func__, |
| 8150 | SCTP_BASE_VAR(sctp_pcb_initialized)); |
| 8151 | return (-1); |
| 8152 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 8153 | SCTP_MALLOC(it, struct sctp_iterator *, sizeof(struct sctp_iterator), |
| 8154 | SCTP_M_ITER); |
| 8155 | if (it == NULL) { |
| 8156 | SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM); |
Michael Tuexen | 287284c | 2020-01-12 14:34:21 +0100 | [diff] [blame] | 8157 | return (-1); |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 8158 | } |
| 8159 | memset(it, 0, sizeof(*it)); |
| 8160 | it->function_assoc = af; |
| 8161 | it->function_inp = inpf; |
| 8162 | if (inpf) |
| 8163 | it->done_current_ep = 0; |
| 8164 | else |
| 8165 | it->done_current_ep = 1; |
| 8166 | it->function_atend = ef; |
| 8167 | it->pointer = argp; |
| 8168 | it->val = argi; |
| 8169 | it->pcb_flags = pcb_state; |
| 8170 | it->pcb_features = pcb_features; |
| 8171 | it->asoc_state = asoc_state; |
| 8172 | it->function_inp_end = inpe; |
| 8173 | it->no_chunk_output = chunk_output_off; |
| 8174 | #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 |
| 8175 | it->vn = curvnet; |
tuexen | 0963116 | 2012-04-18 11:23:02 +0000 | [diff] [blame] | 8176 | #endif |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 8177 | if (s_inp) { |
| 8178 | /* Assume lock is held here */ |
| 8179 | it->inp = s_inp; |
| 8180 | SCTP_INP_INCR_REF(it->inp); |
| 8181 | it->iterator_flags = SCTP_ITERATOR_DO_SINGLE_INP; |
| 8182 | } else { |
| 8183 | SCTP_INP_INFO_RLOCK(); |
| 8184 | it->inp = LIST_FIRST(&SCTP_BASE_INFO(listhead)); |
| 8185 | if (it->inp) { |
| 8186 | SCTP_INP_INCR_REF(it->inp); |
| 8187 | } |
| 8188 | SCTP_INP_INFO_RUNLOCK(); |
| 8189 | it->iterator_flags = SCTP_ITERATOR_DO_ALL_INP; |
| 8190 | |
| 8191 | } |
| 8192 | SCTP_IPI_ITERATOR_WQ_LOCK(); |
Michael Tuexen | 71a83fb | 2016-02-16 21:52:58 +0100 | [diff] [blame] | 8193 | if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) { |
| 8194 | SCTP_IPI_ITERATOR_WQ_UNLOCK(); |
| 8195 | SCTP_PRINTF("%s: rollback on initialize being %d it=%p\n", __func__, |
| 8196 | SCTP_BASE_VAR(sctp_pcb_initialized), it); |
| 8197 | SCTP_FREE(it, SCTP_M_ITER); |
| 8198 | return (-1); |
| 8199 | } |
tuexen | dd72923 | 2011-11-01 23:04:43 +0000 | [diff] [blame] | 8200 | TAILQ_INSERT_TAIL(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr); |
| 8201 | if (sctp_it_ctl.iterator_running == 0) { |
| 8202 | sctp_wakeup_iterator(); |
| 8203 | } |
| 8204 | SCTP_IPI_ITERATOR_WQ_UNLOCK(); |
| 8205 | /* sa_ignore MEMLEAK {memory is put on the tailq for the iterator} */ |
| 8206 | return (0); |
| 8207 | } |