Thu, 15 Jan 2015 15:59:08 +0100
Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /*- |
michael@0 | 2 | * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved. |
michael@0 | 3 | * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved. |
michael@0 | 4 | * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved. |
michael@0 | 5 | * |
michael@0 | 6 | * Redistribution and use in source and binary forms, with or without |
michael@0 | 7 | * modification, are permitted provided that the following conditions are met: |
michael@0 | 8 | * |
michael@0 | 9 | * a) Redistributions of source code must retain the above copyright notice, |
michael@0 | 10 | * this list of conditions and the following disclaimer. |
michael@0 | 11 | * |
michael@0 | 12 | * b) Redistributions in binary form must reproduce the above copyright |
michael@0 | 13 | * notice, this list of conditions and the following disclaimer in |
michael@0 | 14 | * the documentation and/or other materials provided with the distribution. |
michael@0 | 15 | * |
michael@0 | 16 | * c) Neither the name of Cisco Systems, Inc. nor the names of its |
michael@0 | 17 | * contributors may be used to endorse or promote products derived |
michael@0 | 18 | * from this software without specific prior written permission. |
michael@0 | 19 | * |
michael@0 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
michael@0 | 22 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
michael@0 | 23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
michael@0 | 24 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
michael@0 | 25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
michael@0 | 26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
michael@0 | 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
michael@0 | 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
michael@0 | 29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
michael@0 | 30 | * THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 31 | */ |
michael@0 | 32 | |
michael@0 | 33 | #ifdef __FreeBSD__ |
michael@0 | 34 | #include <sys/cdefs.h> |
michael@0 | 35 | __FBSDID("$FreeBSD: head/sys/netinet/sctp_pcb.c 258765 2013-11-30 12:51:19Z tuexen $"); |
michael@0 | 36 | #endif |
michael@0 | 37 | |
michael@0 | 38 | #include <netinet/sctp_os.h> |
michael@0 | 39 | #ifdef __FreeBSD__ |
michael@0 | 40 | #include <sys/proc.h> |
michael@0 | 41 | #endif |
michael@0 | 42 | #include <netinet/sctp_var.h> |
michael@0 | 43 | #include <netinet/sctp_sysctl.h> |
michael@0 | 44 | #include <netinet/sctp_pcb.h> |
michael@0 | 45 | #include <netinet/sctputil.h> |
michael@0 | 46 | #include <netinet/sctp.h> |
michael@0 | 47 | #include <netinet/sctp_header.h> |
michael@0 | 48 | #include <netinet/sctp_asconf.h> |
michael@0 | 49 | #include <netinet/sctp_output.h> |
michael@0 | 50 | #include <netinet/sctp_timer.h> |
michael@0 | 51 | #include <netinet/sctp_bsd_addr.h> |
michael@0 | 52 | #if defined(__FreeBSD__) && __FreeBSD_version >= 803000 |
michael@0 | 53 | #include <netinet/sctp_dtrace_define.h> |
michael@0 | 54 | #endif |
michael@0 | 55 | #if !defined(__Userspace_os_Windows) |
michael@0 | 56 | #include <netinet/udp.h> |
michael@0 | 57 | #endif |
michael@0 | 58 | #ifdef INET6 |
michael@0 | 59 | #if defined(__Userspace__) |
michael@0 | 60 | #include "user_ip6_var.h" |
michael@0 | 61 | #else |
michael@0 | 62 | #include <netinet6/ip6_var.h> |
michael@0 | 63 | #endif |
michael@0 | 64 | #endif |
michael@0 | 65 | #if defined(__FreeBSD__) |
michael@0 | 66 | #include <sys/sched.h> |
michael@0 | 67 | #include <sys/smp.h> |
michael@0 | 68 | #include <sys/unistd.h> |
michael@0 | 69 | #endif |
michael@0 | 70 | #if defined(__Userspace__) |
michael@0 | 71 | #include <user_socketvar.h> |
michael@0 | 72 | #endif |
michael@0 | 73 | |
michael@0 | 74 | #if defined(__APPLE__) |
michael@0 | 75 | #define APPLE_FILE_NO 4 |
michael@0 | 76 | #endif |
michael@0 | 77 | |
michael@0 | 78 | #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 |
michael@0 | 79 | VNET_DEFINE(struct sctp_base_info, system_base_info); |
michael@0 | 80 | #else |
michael@0 | 81 | struct sctp_base_info system_base_info; |
michael@0 | 82 | #endif |
michael@0 | 83 | |
michael@0 | 84 | #if defined(__Userspace__) |
michael@0 | 85 | #if defined(INET) || defined(INET6) |
michael@0 | 86 | struct ifaddrs *g_interfaces; |
michael@0 | 87 | #endif |
michael@0 | 88 | #endif |
michael@0 | 89 | /* FIX: we don't handle multiple link local scopes */ |
michael@0 | 90 | /* "scopeless" replacement IN6_ARE_ADDR_EQUAL */ |
michael@0 | 91 | #ifdef INET6 |
michael@0 | 92 | int |
michael@0 | 93 | SCTP6_ARE_ADDR_EQUAL(struct sockaddr_in6 *a, struct sockaddr_in6 *b) |
michael@0 | 94 | { |
michael@0 | 95 | #ifdef SCTP_EMBEDDED_V6_SCOPE |
michael@0 | 96 | #if defined(__APPLE__) |
michael@0 | 97 | struct in6_addr tmp_a, tmp_b; |
michael@0 | 98 | |
michael@0 | 99 | tmp_a = a->sin6_addr; |
michael@0 | 100 | #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) |
michael@0 | 101 | if (in6_embedscope(&tmp_a, a, NULL, NULL) != 0) { |
michael@0 | 102 | #else |
michael@0 | 103 | if (in6_embedscope(&tmp_a, a, NULL, NULL, NULL) != 0) { |
michael@0 | 104 | #endif |
michael@0 | 105 | return (0); |
michael@0 | 106 | } |
michael@0 | 107 | tmp_b = b->sin6_addr; |
michael@0 | 108 | #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) |
michael@0 | 109 | if (in6_embedscope(&tmp_b, b, NULL, NULL) != 0) { |
michael@0 | 110 | #else |
michael@0 | 111 | if (in6_embedscope(&tmp_b, b, NULL, NULL, NULL) != 0) { |
michael@0 | 112 | #endif |
michael@0 | 113 | return (0); |
michael@0 | 114 | } |
michael@0 | 115 | return (IN6_ARE_ADDR_EQUAL(&tmp_a, &tmp_b)); |
michael@0 | 116 | #elif defined(SCTP_KAME) |
michael@0 | 117 | struct sockaddr_in6 tmp_a, tmp_b; |
michael@0 | 118 | |
michael@0 | 119 | memcpy(&tmp_a, a, sizeof(struct sockaddr_in6)); |
michael@0 | 120 | if (sa6_embedscope(&tmp_a, MODULE_GLOBAL(ip6_use_defzone)) != 0) { |
michael@0 | 121 | return (0); |
michael@0 | 122 | } |
michael@0 | 123 | memcpy(&tmp_b, b, sizeof(struct sockaddr_in6)); |
michael@0 | 124 | if (sa6_embedscope(&tmp_b, MODULE_GLOBAL(ip6_use_defzone)) != 0) { |
michael@0 | 125 | return (0); |
michael@0 | 126 | } |
michael@0 | 127 | return (IN6_ARE_ADDR_EQUAL(&tmp_a.sin6_addr, &tmp_b.sin6_addr)); |
michael@0 | 128 | #else |
michael@0 | 129 | struct in6_addr tmp_a, tmp_b; |
michael@0 | 130 | |
michael@0 | 131 | tmp_a = a->sin6_addr; |
michael@0 | 132 | if (in6_embedscope(&tmp_a, a) != 0) { |
michael@0 | 133 | return (0); |
michael@0 | 134 | } |
michael@0 | 135 | tmp_b = b->sin6_addr; |
michael@0 | 136 | if (in6_embedscope(&tmp_b, b) != 0) { |
michael@0 | 137 | return (0); |
michael@0 | 138 | } |
michael@0 | 139 | return (IN6_ARE_ADDR_EQUAL(&tmp_a, &tmp_b)); |
michael@0 | 140 | #endif |
michael@0 | 141 | #else |
michael@0 | 142 | return (IN6_ARE_ADDR_EQUAL(&(a->sin6_addr), &(b->sin6_addr))); |
michael@0 | 143 | #endif /* SCTP_EMBEDDED_V6_SCOPE */ |
michael@0 | 144 | } |
michael@0 | 145 | #endif |
michael@0 | 146 | |
michael@0 | 147 | void |
michael@0 | 148 | sctp_fill_pcbinfo(struct sctp_pcbinfo *spcb) |
michael@0 | 149 | { |
michael@0 | 150 | /* |
michael@0 | 151 | * We really don't need to lock this, but I will just because it |
michael@0 | 152 | * does not hurt. |
michael@0 | 153 | */ |
michael@0 | 154 | SCTP_INP_INFO_RLOCK(); |
michael@0 | 155 | spcb->ep_count = SCTP_BASE_INFO(ipi_count_ep); |
michael@0 | 156 | spcb->asoc_count = SCTP_BASE_INFO(ipi_count_asoc); |
michael@0 | 157 | spcb->laddr_count = SCTP_BASE_INFO(ipi_count_laddr); |
michael@0 | 158 | spcb->raddr_count = SCTP_BASE_INFO(ipi_count_raddr); |
michael@0 | 159 | spcb->chk_count = SCTP_BASE_INFO(ipi_count_chunk); |
michael@0 | 160 | spcb->readq_count = SCTP_BASE_INFO(ipi_count_readq); |
michael@0 | 161 | spcb->stream_oque = SCTP_BASE_INFO(ipi_count_strmoq); |
michael@0 | 162 | spcb->free_chunks = SCTP_BASE_INFO(ipi_free_chunks); |
michael@0 | 163 | SCTP_INP_INFO_RUNLOCK(); |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | /*- |
michael@0 | 167 | * Addresses are added to VRF's (Virtual Router's). For BSD we |
michael@0 | 168 | * have only the default VRF 0. We maintain a hash list of |
michael@0 | 169 | * VRF's. Each VRF has its own list of sctp_ifn's. Each of |
michael@0 | 170 | * these has a list of addresses. When we add a new address |
michael@0 | 171 | * to a VRF we lookup the ifn/ifn_index, if the ifn does |
michael@0 | 172 | * not exist we create it and add it to the list of IFN's |
michael@0 | 173 | * within the VRF. Once we have the sctp_ifn, we add the |
michael@0 | 174 | * address to the list. So we look something like: |
michael@0 | 175 | * |
michael@0 | 176 | * hash-vrf-table |
michael@0 | 177 | * vrf-> ifn-> ifn -> ifn |
michael@0 | 178 | * vrf | |
michael@0 | 179 | * ... +--ifa-> ifa -> ifa |
michael@0 | 180 | * vrf |
michael@0 | 181 | * |
michael@0 | 182 | * We keep these separate lists since the SCTP subsystem will |
michael@0 | 183 | * point to these from its source address selection nets structure. |
michael@0 | 184 | * When an address is deleted it does not happen right away on |
michael@0 | 185 | * the SCTP side, it gets scheduled. What we do when a |
michael@0 | 186 | * delete happens is immediately remove the address from |
michael@0 | 187 | * the master list and decrement the refcount. As our |
michael@0 | 188 | * addip iterator works through and frees the src address |
michael@0 | 189 | * selection pointing to the sctp_ifa, eventually the refcount |
michael@0 | 190 | * will reach 0 and we will delete it. Note that it is assumed |
michael@0 | 191 | * that any locking on system level ifn/ifa is done at the |
michael@0 | 192 | * caller of these functions and these routines will only |
michael@0 | 193 | * lock the SCTP structures as they add or delete things. |
michael@0 | 194 | * |
michael@0 | 195 | * Other notes on VRF concepts. |
michael@0 | 196 | * - An endpoint can be in multiple VRF's |
michael@0 | 197 | * - An association lives within a VRF and only one VRF. |
michael@0 | 198 | * - Any incoming packet we can deduce the VRF for by |
michael@0 | 199 | * looking at the mbuf/pak inbound (for BSD its VRF=0 :D) |
michael@0 | 200 | * - Any downward send call or connect call must supply the |
michael@0 | 201 | * VRF via ancillary data or via some sort of set default |
michael@0 | 202 | * VRF socket option call (again for BSD no brainer since |
michael@0 | 203 | * the VRF is always 0). |
michael@0 | 204 | * - An endpoint may add multiple VRF's to it. |
michael@0 | 205 | * - Listening sockets can accept associations in any |
michael@0 | 206 | * of the VRF's they are in but the assoc will end up |
michael@0 | 207 | * in only one VRF (gotten from the packet or connect/send). |
michael@0 | 208 | * |
michael@0 | 209 | */ |
michael@0 | 210 | |
michael@0 | 211 | struct sctp_vrf * |
michael@0 | 212 | sctp_allocate_vrf(int vrf_id) |
michael@0 | 213 | { |
michael@0 | 214 | struct sctp_vrf *vrf = NULL; |
michael@0 | 215 | struct sctp_vrflist *bucket; |
michael@0 | 216 | |
michael@0 | 217 | /* First allocate the VRF structure */ |
michael@0 | 218 | vrf = sctp_find_vrf(vrf_id); |
michael@0 | 219 | if (vrf) { |
michael@0 | 220 | /* Already allocated */ |
michael@0 | 221 | return (vrf); |
michael@0 | 222 | } |
michael@0 | 223 | SCTP_MALLOC(vrf, struct sctp_vrf *, sizeof(struct sctp_vrf), |
michael@0 | 224 | SCTP_M_VRF); |
michael@0 | 225 | if (vrf == NULL) { |
michael@0 | 226 | /* No memory */ |
michael@0 | 227 | #ifdef INVARIANTS |
michael@0 | 228 | panic("No memory for VRF:%d", vrf_id); |
michael@0 | 229 | #endif |
michael@0 | 230 | return (NULL); |
michael@0 | 231 | } |
michael@0 | 232 | /* setup the VRF */ |
michael@0 | 233 | memset(vrf, 0, sizeof(struct sctp_vrf)); |
michael@0 | 234 | vrf->vrf_id = vrf_id; |
michael@0 | 235 | LIST_INIT(&vrf->ifnlist); |
michael@0 | 236 | vrf->total_ifa_count = 0; |
michael@0 | 237 | vrf->refcount = 0; |
michael@0 | 238 | /* now also setup table ids */ |
michael@0 | 239 | SCTP_INIT_VRF_TABLEID(vrf); |
michael@0 | 240 | /* Init the HASH of addresses */ |
michael@0 | 241 | vrf->vrf_addr_hash = SCTP_HASH_INIT(SCTP_VRF_ADDR_HASH_SIZE, |
michael@0 | 242 | &vrf->vrf_addr_hashmark); |
michael@0 | 243 | if (vrf->vrf_addr_hash == NULL) { |
michael@0 | 244 | /* No memory */ |
michael@0 | 245 | #ifdef INVARIANTS |
michael@0 | 246 | panic("No memory for VRF:%d", vrf_id); |
michael@0 | 247 | #endif |
michael@0 | 248 | SCTP_FREE(vrf, SCTP_M_VRF); |
michael@0 | 249 | return (NULL); |
michael@0 | 250 | } |
michael@0 | 251 | |
michael@0 | 252 | /* Add it to the hash table */ |
michael@0 | 253 | bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(vrf_id & SCTP_BASE_INFO(hashvrfmark))]; |
michael@0 | 254 | LIST_INSERT_HEAD(bucket, vrf, next_vrf); |
michael@0 | 255 | atomic_add_int(&SCTP_BASE_INFO(ipi_count_vrfs), 1); |
michael@0 | 256 | return (vrf); |
michael@0 | 257 | } |
michael@0 | 258 | |
michael@0 | 259 | |
michael@0 | 260 | struct sctp_ifn * |
michael@0 | 261 | sctp_find_ifn(void *ifn, uint32_t ifn_index) |
michael@0 | 262 | { |
michael@0 | 263 | struct sctp_ifn *sctp_ifnp; |
michael@0 | 264 | struct sctp_ifnlist *hash_ifn_head; |
michael@0 | 265 | |
michael@0 | 266 | /* We assume the lock is held for the addresses |
michael@0 | 267 | * if that's wrong problems could occur :-) |
michael@0 | 268 | */ |
michael@0 | 269 | hash_ifn_head = &SCTP_BASE_INFO(vrf_ifn_hash)[(ifn_index & SCTP_BASE_INFO(vrf_ifn_hashmark))]; |
michael@0 | 270 | LIST_FOREACH(sctp_ifnp, hash_ifn_head, next_bucket) { |
michael@0 | 271 | if (sctp_ifnp->ifn_index == ifn_index) { |
michael@0 | 272 | return (sctp_ifnp); |
michael@0 | 273 | } |
michael@0 | 274 | if (sctp_ifnp->ifn_p && ifn && (sctp_ifnp->ifn_p == ifn)) { |
michael@0 | 275 | return (sctp_ifnp); |
michael@0 | 276 | } |
michael@0 | 277 | } |
michael@0 | 278 | return (NULL); |
michael@0 | 279 | } |
michael@0 | 280 | |
michael@0 | 281 | |
michael@0 | 282 | struct sctp_vrf * |
michael@0 | 283 | sctp_find_vrf(uint32_t vrf_id) |
michael@0 | 284 | { |
michael@0 | 285 | struct sctp_vrflist *bucket; |
michael@0 | 286 | struct sctp_vrf *liste; |
michael@0 | 287 | |
michael@0 | 288 | bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(vrf_id & SCTP_BASE_INFO(hashvrfmark))]; |
michael@0 | 289 | LIST_FOREACH(liste, bucket, next_vrf) { |
michael@0 | 290 | if (vrf_id == liste->vrf_id) { |
michael@0 | 291 | return (liste); |
michael@0 | 292 | } |
michael@0 | 293 | } |
michael@0 | 294 | return (NULL); |
michael@0 | 295 | } |
michael@0 | 296 | |
michael@0 | 297 | |
michael@0 | 298 | void |
michael@0 | 299 | sctp_free_vrf(struct sctp_vrf *vrf) |
michael@0 | 300 | { |
michael@0 | 301 | if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&vrf->refcount)) { |
michael@0 | 302 | if (vrf->vrf_addr_hash) { |
michael@0 | 303 | SCTP_HASH_FREE(vrf->vrf_addr_hash, vrf->vrf_addr_hashmark); |
michael@0 | 304 | vrf->vrf_addr_hash = NULL; |
michael@0 | 305 | } |
michael@0 | 306 | /* We zero'd the count */ |
michael@0 | 307 | LIST_REMOVE(vrf, next_vrf); |
michael@0 | 308 | SCTP_FREE(vrf, SCTP_M_VRF); |
michael@0 | 309 | atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_vrfs), 1); |
michael@0 | 310 | } |
michael@0 | 311 | } |
michael@0 | 312 | |
michael@0 | 313 | |
michael@0 | 314 | void |
michael@0 | 315 | sctp_free_ifn(struct sctp_ifn *sctp_ifnp) |
michael@0 | 316 | { |
michael@0 | 317 | if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&sctp_ifnp->refcount)) { |
michael@0 | 318 | /* We zero'd the count */ |
michael@0 | 319 | if (sctp_ifnp->vrf) { |
michael@0 | 320 | sctp_free_vrf(sctp_ifnp->vrf); |
michael@0 | 321 | } |
michael@0 | 322 | SCTP_FREE(sctp_ifnp, SCTP_M_IFN); |
michael@0 | 323 | atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_ifns), 1); |
michael@0 | 324 | } |
michael@0 | 325 | } |
michael@0 | 326 | |
michael@0 | 327 | |
michael@0 | 328 | void |
michael@0 | 329 | sctp_update_ifn_mtu(uint32_t ifn_index, uint32_t mtu) |
michael@0 | 330 | { |
michael@0 | 331 | struct sctp_ifn *sctp_ifnp; |
michael@0 | 332 | |
michael@0 | 333 | sctp_ifnp = sctp_find_ifn((void *)NULL, ifn_index); |
michael@0 | 334 | if (sctp_ifnp != NULL) { |
michael@0 | 335 | sctp_ifnp->ifn_mtu = mtu; |
michael@0 | 336 | } |
michael@0 | 337 | } |
michael@0 | 338 | |
michael@0 | 339 | |
michael@0 | 340 | void |
michael@0 | 341 | sctp_free_ifa(struct sctp_ifa *sctp_ifap) |
michael@0 | 342 | { |
michael@0 | 343 | if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&sctp_ifap->refcount)) { |
michael@0 | 344 | /* We zero'd the count */ |
michael@0 | 345 | if (sctp_ifap->ifn_p) { |
michael@0 | 346 | sctp_free_ifn(sctp_ifap->ifn_p); |
michael@0 | 347 | } |
michael@0 | 348 | SCTP_FREE(sctp_ifap, SCTP_M_IFA); |
michael@0 | 349 | atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_ifas), 1); |
michael@0 | 350 | } |
michael@0 | 351 | } |
michael@0 | 352 | |
michael@0 | 353 | |
michael@0 | 354 | static void |
michael@0 | 355 | sctp_delete_ifn(struct sctp_ifn *sctp_ifnp, int hold_addr_lock) |
michael@0 | 356 | { |
michael@0 | 357 | struct sctp_ifn *found; |
michael@0 | 358 | |
michael@0 | 359 | found = sctp_find_ifn(sctp_ifnp->ifn_p, sctp_ifnp->ifn_index); |
michael@0 | 360 | if (found == NULL) { |
michael@0 | 361 | /* Not in the list.. sorry */ |
michael@0 | 362 | return; |
michael@0 | 363 | } |
michael@0 | 364 | if (hold_addr_lock == 0) |
michael@0 | 365 | SCTP_IPI_ADDR_WLOCK(); |
michael@0 | 366 | LIST_REMOVE(sctp_ifnp, next_bucket); |
michael@0 | 367 | LIST_REMOVE(sctp_ifnp, next_ifn); |
michael@0 | 368 | SCTP_DEREGISTER_INTERFACE(sctp_ifnp->ifn_index, |
michael@0 | 369 | sctp_ifnp->registered_af); |
michael@0 | 370 | if (hold_addr_lock == 0) |
michael@0 | 371 | SCTP_IPI_ADDR_WUNLOCK(); |
michael@0 | 372 | /* Take away the reference, and possibly free it */ |
michael@0 | 373 | sctp_free_ifn(sctp_ifnp); |
michael@0 | 374 | } |
michael@0 | 375 | |
michael@0 | 376 | |
michael@0 | 377 | void |
michael@0 | 378 | sctp_mark_ifa_addr_down(uint32_t vrf_id, struct sockaddr *addr, |
michael@0 | 379 | const char *if_name, uint32_t ifn_index) |
michael@0 | 380 | { |
michael@0 | 381 | struct sctp_vrf *vrf; |
michael@0 | 382 | struct sctp_ifa *sctp_ifap; |
michael@0 | 383 | |
michael@0 | 384 | SCTP_IPI_ADDR_RLOCK(); |
michael@0 | 385 | vrf = sctp_find_vrf(vrf_id); |
michael@0 | 386 | if (vrf == NULL) { |
michael@0 | 387 | SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id); |
michael@0 | 388 | goto out; |
michael@0 | 389 | |
michael@0 | 390 | } |
michael@0 | 391 | sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED); |
michael@0 | 392 | if (sctp_ifap == NULL) { |
michael@0 | 393 | SCTPDBG(SCTP_DEBUG_PCB4, "Can't find sctp_ifap for address\n"); |
michael@0 | 394 | goto out; |
michael@0 | 395 | } |
michael@0 | 396 | if (sctp_ifap->ifn_p == NULL) { |
michael@0 | 397 | SCTPDBG(SCTP_DEBUG_PCB4, "IFA has no IFN - can't mark unuseable\n"); |
michael@0 | 398 | goto out; |
michael@0 | 399 | } |
michael@0 | 400 | if (if_name) { |
michael@0 | 401 | if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, SCTP_IFNAMSIZ) != 0) { |
michael@0 | 402 | SCTPDBG(SCTP_DEBUG_PCB4, "IFN %s of IFA not the same as %s\n", |
michael@0 | 403 | sctp_ifap->ifn_p->ifn_name, if_name); |
michael@0 | 404 | goto out; |
michael@0 | 405 | } |
michael@0 | 406 | } else { |
michael@0 | 407 | if (sctp_ifap->ifn_p->ifn_index != ifn_index) { |
michael@0 | 408 | SCTPDBG(SCTP_DEBUG_PCB4, "IFA owned by ifn_index:%d down command for ifn_index:%d - ignored\n", |
michael@0 | 409 | sctp_ifap->ifn_p->ifn_index, ifn_index); |
michael@0 | 410 | goto out; |
michael@0 | 411 | } |
michael@0 | 412 | } |
michael@0 | 413 | |
michael@0 | 414 | sctp_ifap->localifa_flags &= (~SCTP_ADDR_VALID); |
michael@0 | 415 | sctp_ifap->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE; |
michael@0 | 416 | out: |
michael@0 | 417 | SCTP_IPI_ADDR_RUNLOCK(); |
michael@0 | 418 | } |
michael@0 | 419 | |
michael@0 | 420 | |
michael@0 | 421 | void |
michael@0 | 422 | sctp_mark_ifa_addr_up(uint32_t vrf_id, struct sockaddr *addr, |
michael@0 | 423 | const char *if_name, uint32_t ifn_index) |
michael@0 | 424 | { |
michael@0 | 425 | struct sctp_vrf *vrf; |
michael@0 | 426 | struct sctp_ifa *sctp_ifap; |
michael@0 | 427 | |
michael@0 | 428 | SCTP_IPI_ADDR_RLOCK(); |
michael@0 | 429 | vrf = sctp_find_vrf(vrf_id); |
michael@0 | 430 | if (vrf == NULL) { |
michael@0 | 431 | SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id); |
michael@0 | 432 | goto out; |
michael@0 | 433 | |
michael@0 | 434 | } |
michael@0 | 435 | sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED); |
michael@0 | 436 | if (sctp_ifap == NULL) { |
michael@0 | 437 | SCTPDBG(SCTP_DEBUG_PCB4, "Can't find sctp_ifap for address\n"); |
michael@0 | 438 | goto out; |
michael@0 | 439 | } |
michael@0 | 440 | if (sctp_ifap->ifn_p == NULL) { |
michael@0 | 441 | SCTPDBG(SCTP_DEBUG_PCB4, "IFA has no IFN - can't mark unuseable\n"); |
michael@0 | 442 | goto out; |
michael@0 | 443 | } |
michael@0 | 444 | if (if_name) { |
michael@0 | 445 | if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, SCTP_IFNAMSIZ) != 0) { |
michael@0 | 446 | SCTPDBG(SCTP_DEBUG_PCB4, "IFN %s of IFA not the same as %s\n", |
michael@0 | 447 | sctp_ifap->ifn_p->ifn_name, if_name); |
michael@0 | 448 | goto out; |
michael@0 | 449 | } |
michael@0 | 450 | } else { |
michael@0 | 451 | if (sctp_ifap->ifn_p->ifn_index != ifn_index) { |
michael@0 | 452 | SCTPDBG(SCTP_DEBUG_PCB4, "IFA owned by ifn_index:%d down command for ifn_index:%d - ignored\n", |
michael@0 | 453 | sctp_ifap->ifn_p->ifn_index, ifn_index); |
michael@0 | 454 | goto out; |
michael@0 | 455 | } |
michael@0 | 456 | } |
michael@0 | 457 | |
michael@0 | 458 | sctp_ifap->localifa_flags &= (~SCTP_ADDR_IFA_UNUSEABLE); |
michael@0 | 459 | sctp_ifap->localifa_flags |= SCTP_ADDR_VALID; |
michael@0 | 460 | out: |
michael@0 | 461 | SCTP_IPI_ADDR_RUNLOCK(); |
michael@0 | 462 | } |
michael@0 | 463 | |
michael@0 | 464 | |
michael@0 | 465 | /*- |
michael@0 | 466 | * Add an ifa to an ifn. |
michael@0 | 467 | * Register the interface as necessary. |
michael@0 | 468 | * NOTE: ADDR write lock MUST be held. |
michael@0 | 469 | */ |
michael@0 | 470 | static void |
michael@0 | 471 | sctp_add_ifa_to_ifn(struct sctp_ifn *sctp_ifnp, struct sctp_ifa *sctp_ifap) |
michael@0 | 472 | { |
michael@0 | 473 | int ifa_af; |
michael@0 | 474 | |
michael@0 | 475 | LIST_INSERT_HEAD(&sctp_ifnp->ifalist, sctp_ifap, next_ifa); |
michael@0 | 476 | sctp_ifap->ifn_p = sctp_ifnp; |
michael@0 | 477 | atomic_add_int(&sctp_ifap->ifn_p->refcount, 1); |
michael@0 | 478 | /* update address counts */ |
michael@0 | 479 | sctp_ifnp->ifa_count++; |
michael@0 | 480 | ifa_af = sctp_ifap->address.sa.sa_family; |
michael@0 | 481 | switch (ifa_af) { |
michael@0 | 482 | #ifdef INET |
michael@0 | 483 | case AF_INET: |
michael@0 | 484 | sctp_ifnp->num_v4++; |
michael@0 | 485 | break; |
michael@0 | 486 | #endif |
michael@0 | 487 | #ifdef INET6 |
michael@0 | 488 | case AF_INET6: |
michael@0 | 489 | sctp_ifnp->num_v6++; |
michael@0 | 490 | break; |
michael@0 | 491 | #endif |
michael@0 | 492 | default: |
michael@0 | 493 | break; |
michael@0 | 494 | } |
michael@0 | 495 | if (sctp_ifnp->ifa_count == 1) { |
michael@0 | 496 | /* register the new interface */ |
michael@0 | 497 | SCTP_REGISTER_INTERFACE(sctp_ifnp->ifn_index, ifa_af); |
michael@0 | 498 | sctp_ifnp->registered_af = ifa_af; |
michael@0 | 499 | } |
michael@0 | 500 | } |
michael@0 | 501 | |
michael@0 | 502 | |
michael@0 | 503 | /*- |
michael@0 | 504 | * Remove an ifa from its ifn. |
michael@0 | 505 | * If no more addresses exist, remove the ifn too. Otherwise, re-register |
michael@0 | 506 | * the interface based on the remaining address families left. |
michael@0 | 507 | * NOTE: ADDR write lock MUST be held. |
michael@0 | 508 | */ |
michael@0 | 509 | static void |
michael@0 | 510 | sctp_remove_ifa_from_ifn(struct sctp_ifa *sctp_ifap) |
michael@0 | 511 | { |
michael@0 | 512 | LIST_REMOVE(sctp_ifap, next_ifa); |
michael@0 | 513 | if (sctp_ifap->ifn_p) { |
michael@0 | 514 | /* update address counts */ |
michael@0 | 515 | sctp_ifap->ifn_p->ifa_count--; |
michael@0 | 516 | switch (sctp_ifap->address.sa.sa_family) { |
michael@0 | 517 | #ifdef INET |
michael@0 | 518 | case AF_INET: |
michael@0 | 519 | sctp_ifap->ifn_p->num_v4--; |
michael@0 | 520 | break; |
michael@0 | 521 | #endif |
michael@0 | 522 | #ifdef INET6 |
michael@0 | 523 | case AF_INET6: |
michael@0 | 524 | sctp_ifap->ifn_p->num_v6--; |
michael@0 | 525 | break; |
michael@0 | 526 | #endif |
michael@0 | 527 | default: |
michael@0 | 528 | break; |
michael@0 | 529 | } |
michael@0 | 530 | |
michael@0 | 531 | if (LIST_EMPTY(&sctp_ifap->ifn_p->ifalist)) { |
michael@0 | 532 | /* remove the ifn, possibly freeing it */ |
michael@0 | 533 | sctp_delete_ifn(sctp_ifap->ifn_p, SCTP_ADDR_LOCKED); |
michael@0 | 534 | } else { |
michael@0 | 535 | /* re-register address family type, if needed */ |
michael@0 | 536 | if ((sctp_ifap->ifn_p->num_v6 == 0) && |
michael@0 | 537 | (sctp_ifap->ifn_p->registered_af == AF_INET6)) { |
michael@0 | 538 | SCTP_DEREGISTER_INTERFACE(sctp_ifap->ifn_p->ifn_index, AF_INET6); |
michael@0 | 539 | SCTP_REGISTER_INTERFACE(sctp_ifap->ifn_p->ifn_index, AF_INET); |
michael@0 | 540 | sctp_ifap->ifn_p->registered_af = AF_INET; |
michael@0 | 541 | } else if ((sctp_ifap->ifn_p->num_v4 == 0) && |
michael@0 | 542 | (sctp_ifap->ifn_p->registered_af == AF_INET)) { |
michael@0 | 543 | SCTP_DEREGISTER_INTERFACE(sctp_ifap->ifn_p->ifn_index, AF_INET); |
michael@0 | 544 | SCTP_REGISTER_INTERFACE(sctp_ifap->ifn_p->ifn_index, AF_INET6); |
michael@0 | 545 | sctp_ifap->ifn_p->registered_af = AF_INET6; |
michael@0 | 546 | } |
michael@0 | 547 | /* free the ifn refcount */ |
michael@0 | 548 | sctp_free_ifn(sctp_ifap->ifn_p); |
michael@0 | 549 | } |
michael@0 | 550 | sctp_ifap->ifn_p = NULL; |
michael@0 | 551 | } |
michael@0 | 552 | } |
michael@0 | 553 | |
michael@0 | 554 | |
michael@0 | 555 | struct sctp_ifa * |
michael@0 | 556 | sctp_add_addr_to_vrf(uint32_t vrf_id, void *ifn, uint32_t ifn_index, |
michael@0 | 557 | uint32_t ifn_type, const char *if_name, void *ifa, |
michael@0 | 558 | struct sockaddr *addr, uint32_t ifa_flags, |
michael@0 | 559 | int dynamic_add) |
michael@0 | 560 | { |
michael@0 | 561 | struct sctp_vrf *vrf; |
michael@0 | 562 | struct sctp_ifn *sctp_ifnp = NULL; |
michael@0 | 563 | struct sctp_ifa *sctp_ifap = NULL; |
michael@0 | 564 | struct sctp_ifalist *hash_addr_head; |
michael@0 | 565 | struct sctp_ifnlist *hash_ifn_head; |
michael@0 | 566 | uint32_t hash_of_addr; |
michael@0 | 567 | int new_ifn_af = 0; |
michael@0 | 568 | |
michael@0 | 569 | #ifdef SCTP_DEBUG |
michael@0 | 570 | SCTPDBG(SCTP_DEBUG_PCB4, "vrf_id 0x%x: adding address: ", vrf_id); |
michael@0 | 571 | SCTPDBG_ADDR(SCTP_DEBUG_PCB4, addr); |
michael@0 | 572 | #endif |
michael@0 | 573 | SCTP_IPI_ADDR_WLOCK(); |
michael@0 | 574 | sctp_ifnp = sctp_find_ifn(ifn, ifn_index); |
michael@0 | 575 | if (sctp_ifnp) { |
michael@0 | 576 | vrf = sctp_ifnp->vrf; |
michael@0 | 577 | } else { |
michael@0 | 578 | vrf = sctp_find_vrf(vrf_id); |
michael@0 | 579 | if (vrf == NULL) { |
michael@0 | 580 | vrf = sctp_allocate_vrf(vrf_id); |
michael@0 | 581 | if (vrf == NULL) { |
michael@0 | 582 | SCTP_IPI_ADDR_WUNLOCK(); |
michael@0 | 583 | return (NULL); |
michael@0 | 584 | } |
michael@0 | 585 | } |
michael@0 | 586 | } |
michael@0 | 587 | if (sctp_ifnp == NULL) { |
michael@0 | 588 | /* build one and add it, can't hold lock |
michael@0 | 589 | * until after malloc done though. |
michael@0 | 590 | */ |
michael@0 | 591 | SCTP_IPI_ADDR_WUNLOCK(); |
michael@0 | 592 | SCTP_MALLOC(sctp_ifnp, struct sctp_ifn *, |
michael@0 | 593 | sizeof(struct sctp_ifn), SCTP_M_IFN); |
michael@0 | 594 | if (sctp_ifnp == NULL) { |
michael@0 | 595 | #ifdef INVARIANTS |
michael@0 | 596 | panic("No memory for IFN"); |
michael@0 | 597 | #endif |
michael@0 | 598 | return (NULL); |
michael@0 | 599 | } |
michael@0 | 600 | memset(sctp_ifnp, 0, sizeof(struct sctp_ifn)); |
michael@0 | 601 | sctp_ifnp->ifn_index = ifn_index; |
michael@0 | 602 | sctp_ifnp->ifn_p = ifn; |
michael@0 | 603 | sctp_ifnp->ifn_type = ifn_type; |
michael@0 | 604 | sctp_ifnp->refcount = 0; |
michael@0 | 605 | sctp_ifnp->vrf = vrf; |
michael@0 | 606 | atomic_add_int(&vrf->refcount, 1); |
michael@0 | 607 | sctp_ifnp->ifn_mtu = SCTP_GATHER_MTU_FROM_IFN_INFO(ifn, ifn_index, addr->sa_family); |
michael@0 | 608 | if (if_name != NULL) { |
michael@0 | 609 | snprintf(sctp_ifnp->ifn_name, SCTP_IFNAMSIZ, "%s", if_name); |
michael@0 | 610 | } else { |
michael@0 | 611 | snprintf(sctp_ifnp->ifn_name, SCTP_IFNAMSIZ, "%s", "unknown"); |
michael@0 | 612 | } |
michael@0 | 613 | hash_ifn_head = &SCTP_BASE_INFO(vrf_ifn_hash)[(ifn_index & SCTP_BASE_INFO(vrf_ifn_hashmark))]; |
michael@0 | 614 | LIST_INIT(&sctp_ifnp->ifalist); |
michael@0 | 615 | SCTP_IPI_ADDR_WLOCK(); |
michael@0 | 616 | LIST_INSERT_HEAD(hash_ifn_head, sctp_ifnp, next_bucket); |
michael@0 | 617 | LIST_INSERT_HEAD(&vrf->ifnlist, sctp_ifnp, next_ifn); |
michael@0 | 618 | atomic_add_int(&SCTP_BASE_INFO(ipi_count_ifns), 1); |
michael@0 | 619 | new_ifn_af = 1; |
michael@0 | 620 | } |
michael@0 | 621 | sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED); |
michael@0 | 622 | if (sctp_ifap) { |
michael@0 | 623 | /* Hmm, it already exists? */ |
michael@0 | 624 | if ((sctp_ifap->ifn_p) && |
michael@0 | 625 | (sctp_ifap->ifn_p->ifn_index == ifn_index)) { |
michael@0 | 626 | SCTPDBG(SCTP_DEBUG_PCB4, "Using existing ifn %s (0x%x) for ifa %p\n", |
michael@0 | 627 | sctp_ifap->ifn_p->ifn_name, ifn_index, |
michael@0 | 628 | (void *)sctp_ifap); |
michael@0 | 629 | if (new_ifn_af) { |
michael@0 | 630 | /* Remove the created one that we don't want */ |
michael@0 | 631 | sctp_delete_ifn(sctp_ifnp, SCTP_ADDR_LOCKED); |
michael@0 | 632 | } |
michael@0 | 633 | if (sctp_ifap->localifa_flags & SCTP_BEING_DELETED) { |
michael@0 | 634 | /* easy to solve, just switch back to active */ |
michael@0 | 635 | SCTPDBG(SCTP_DEBUG_PCB4, "Clearing deleted ifa flag\n"); |
michael@0 | 636 | sctp_ifap->localifa_flags = SCTP_ADDR_VALID; |
michael@0 | 637 | sctp_ifap->ifn_p = sctp_ifnp; |
michael@0 | 638 | atomic_add_int(&sctp_ifap->ifn_p->refcount, 1); |
michael@0 | 639 | } |
michael@0 | 640 | exit_stage_left: |
michael@0 | 641 | SCTP_IPI_ADDR_WUNLOCK(); |
michael@0 | 642 | return (sctp_ifap); |
michael@0 | 643 | } else { |
michael@0 | 644 | if (sctp_ifap->ifn_p) { |
michael@0 | 645 | /* |
michael@0 | 646 | * The last IFN gets the address, remove the |
michael@0 | 647 | * old one |
michael@0 | 648 | */ |
michael@0 | 649 | SCTPDBG(SCTP_DEBUG_PCB4, "Moving ifa %p from %s (0x%x) to %s (0x%x)\n", |
michael@0 | 650 | (void *)sctp_ifap, sctp_ifap->ifn_p->ifn_name, |
michael@0 | 651 | sctp_ifap->ifn_p->ifn_index, if_name, |
michael@0 | 652 | ifn_index); |
michael@0 | 653 | /* remove the address from the old ifn */ |
michael@0 | 654 | sctp_remove_ifa_from_ifn(sctp_ifap); |
michael@0 | 655 | /* move the address over to the new ifn */ |
michael@0 | 656 | sctp_add_ifa_to_ifn(sctp_ifnp, sctp_ifap); |
michael@0 | 657 | goto exit_stage_left; |
michael@0 | 658 | } else { |
michael@0 | 659 | /* repair ifnp which was NULL ? */ |
michael@0 | 660 | sctp_ifap->localifa_flags = SCTP_ADDR_VALID; |
michael@0 | 661 | SCTPDBG(SCTP_DEBUG_PCB4, "Repairing ifn %p for ifa %p\n", |
michael@0 | 662 | (void *)sctp_ifnp, (void *)sctp_ifap); |
michael@0 | 663 | sctp_add_ifa_to_ifn(sctp_ifnp, sctp_ifap); |
michael@0 | 664 | } |
michael@0 | 665 | goto exit_stage_left; |
michael@0 | 666 | } |
michael@0 | 667 | } |
michael@0 | 668 | SCTP_IPI_ADDR_WUNLOCK(); |
michael@0 | 669 | SCTP_MALLOC(sctp_ifap, struct sctp_ifa *, sizeof(struct sctp_ifa), SCTP_M_IFA); |
michael@0 | 670 | if (sctp_ifap == NULL) { |
michael@0 | 671 | #ifdef INVARIANTS |
michael@0 | 672 | panic("No memory for IFA"); |
michael@0 | 673 | #endif |
michael@0 | 674 | return (NULL); |
michael@0 | 675 | } |
michael@0 | 676 | memset(sctp_ifap, 0, sizeof(struct sctp_ifa)); |
michael@0 | 677 | sctp_ifap->ifn_p = sctp_ifnp; |
michael@0 | 678 | atomic_add_int(&sctp_ifnp->refcount, 1); |
michael@0 | 679 | sctp_ifap->vrf_id = vrf_id; |
michael@0 | 680 | sctp_ifap->ifa = ifa; |
michael@0 | 681 | #ifdef HAVE_SA_LEN |
michael@0 | 682 | memcpy(&sctp_ifap->address, addr, addr->sa_len); |
michael@0 | 683 | #else |
michael@0 | 684 | switch (addr->sa_family) { |
michael@0 | 685 | #ifdef INET |
michael@0 | 686 | case AF_INET: |
michael@0 | 687 | memcpy(&sctp_ifap->address, addr, sizeof(struct sockaddr_in)); |
michael@0 | 688 | break; |
michael@0 | 689 | #endif |
michael@0 | 690 | #ifdef INET6 |
michael@0 | 691 | case AF_INET6: |
michael@0 | 692 | memcpy(&sctp_ifap->address, addr, sizeof(struct sockaddr_in6)); |
michael@0 | 693 | break; |
michael@0 | 694 | #endif |
michael@0 | 695 | #if defined(__Userspace__) |
michael@0 | 696 | case AF_CONN: |
michael@0 | 697 | memcpy(&sctp_ifap->address, addr, sizeof(struct sockaddr_conn)); |
michael@0 | 698 | break; |
michael@0 | 699 | #endif |
michael@0 | 700 | default: |
michael@0 | 701 | /* TSNH */ |
michael@0 | 702 | break; |
michael@0 | 703 | } |
michael@0 | 704 | #endif |
michael@0 | 705 | sctp_ifap->localifa_flags = SCTP_ADDR_VALID | SCTP_ADDR_DEFER_USE; |
michael@0 | 706 | sctp_ifap->flags = ifa_flags; |
michael@0 | 707 | /* Set scope */ |
michael@0 | 708 | switch (sctp_ifap->address.sa.sa_family) { |
michael@0 | 709 | #ifdef INET |
michael@0 | 710 | case AF_INET: |
michael@0 | 711 | { |
michael@0 | 712 | struct sockaddr_in *sin; |
michael@0 | 713 | |
michael@0 | 714 | sin = (struct sockaddr_in *)&sctp_ifap->address.sin; |
michael@0 | 715 | if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) || |
michael@0 | 716 | (IN4_ISLOOPBACK_ADDRESS(&sin->sin_addr))) { |
michael@0 | 717 | sctp_ifap->src_is_loop = 1; |
michael@0 | 718 | } |
michael@0 | 719 | if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) { |
michael@0 | 720 | sctp_ifap->src_is_priv = 1; |
michael@0 | 721 | } |
michael@0 | 722 | sctp_ifnp->num_v4++; |
michael@0 | 723 | if (new_ifn_af) |
michael@0 | 724 | new_ifn_af = AF_INET; |
michael@0 | 725 | break; |
michael@0 | 726 | } |
michael@0 | 727 | #endif |
michael@0 | 728 | #ifdef INET6 |
michael@0 | 729 | case AF_INET6: |
michael@0 | 730 | { |
michael@0 | 731 | /* ok to use deprecated addresses? */ |
michael@0 | 732 | struct sockaddr_in6 *sin6; |
michael@0 | 733 | |
michael@0 | 734 | sin6 = (struct sockaddr_in6 *)&sctp_ifap->address.sin6; |
michael@0 | 735 | if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) || |
michael@0 | 736 | (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))) { |
michael@0 | 737 | sctp_ifap->src_is_loop = 1; |
michael@0 | 738 | } |
michael@0 | 739 | if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { |
michael@0 | 740 | sctp_ifap->src_is_priv = 1; |
michael@0 | 741 | } |
michael@0 | 742 | sctp_ifnp->num_v6++; |
michael@0 | 743 | if (new_ifn_af) |
michael@0 | 744 | new_ifn_af = AF_INET6; |
michael@0 | 745 | break; |
michael@0 | 746 | } |
michael@0 | 747 | #endif |
michael@0 | 748 | #if defined(__Userspace__) |
michael@0 | 749 | case AF_CONN: |
michael@0 | 750 | if (new_ifn_af) |
michael@0 | 751 | new_ifn_af = AF_CONN; |
michael@0 | 752 | break; |
michael@0 | 753 | #endif |
michael@0 | 754 | default: |
michael@0 | 755 | new_ifn_af = 0; |
michael@0 | 756 | break; |
michael@0 | 757 | } |
michael@0 | 758 | hash_of_addr = sctp_get_ifa_hash_val(&sctp_ifap->address.sa); |
michael@0 | 759 | |
michael@0 | 760 | if ((sctp_ifap->src_is_priv == 0) && |
michael@0 | 761 | (sctp_ifap->src_is_loop == 0)) { |
michael@0 | 762 | sctp_ifap->src_is_glob = 1; |
michael@0 | 763 | } |
michael@0 | 764 | SCTP_IPI_ADDR_WLOCK(); |
michael@0 | 765 | hash_addr_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)]; |
michael@0 | 766 | LIST_INSERT_HEAD(hash_addr_head, sctp_ifap, next_bucket); |
michael@0 | 767 | sctp_ifap->refcount = 1; |
michael@0 | 768 | LIST_INSERT_HEAD(&sctp_ifnp->ifalist, sctp_ifap, next_ifa); |
michael@0 | 769 | sctp_ifnp->ifa_count++; |
michael@0 | 770 | vrf->total_ifa_count++; |
michael@0 | 771 | atomic_add_int(&SCTP_BASE_INFO(ipi_count_ifas), 1); |
michael@0 | 772 | if (new_ifn_af) { |
michael@0 | 773 | SCTP_REGISTER_INTERFACE(ifn_index, new_ifn_af); |
michael@0 | 774 | sctp_ifnp->registered_af = new_ifn_af; |
michael@0 | 775 | } |
michael@0 | 776 | SCTP_IPI_ADDR_WUNLOCK(); |
michael@0 | 777 | if (dynamic_add) { |
michael@0 | 778 | /* Bump up the refcount so that when the timer |
michael@0 | 779 | * completes it will drop back down. |
michael@0 | 780 | */ |
michael@0 | 781 | struct sctp_laddr *wi; |
michael@0 | 782 | |
michael@0 | 783 | atomic_add_int(&sctp_ifap->refcount, 1); |
michael@0 | 784 | wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr); |
michael@0 | 785 | if (wi == NULL) { |
michael@0 | 786 | /* |
michael@0 | 787 | * Gak, what can we do? We have lost an address |
michael@0 | 788 | * change can you say HOSED? |
michael@0 | 789 | */ |
michael@0 | 790 | SCTPDBG(SCTP_DEBUG_PCB4, "Lost an address change?\n"); |
michael@0 | 791 | /* Opps, must decrement the count */ |
michael@0 | 792 | sctp_del_addr_from_vrf(vrf_id, addr, ifn_index, |
michael@0 | 793 | if_name); |
michael@0 | 794 | return (NULL); |
michael@0 | 795 | } |
michael@0 | 796 | SCTP_INCR_LADDR_COUNT(); |
michael@0 | 797 | bzero(wi, sizeof(*wi)); |
michael@0 | 798 | (void)SCTP_GETTIME_TIMEVAL(&wi->start_time); |
michael@0 | 799 | wi->ifa = sctp_ifap; |
michael@0 | 800 | wi->action = SCTP_ADD_IP_ADDRESS; |
michael@0 | 801 | |
michael@0 | 802 | SCTP_WQ_ADDR_LOCK(); |
michael@0 | 803 | LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr); |
michael@0 | 804 | SCTP_WQ_ADDR_UNLOCK(); |
michael@0 | 805 | |
michael@0 | 806 | sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ, |
michael@0 | 807 | (struct sctp_inpcb *)NULL, |
michael@0 | 808 | (struct sctp_tcb *)NULL, |
michael@0 | 809 | (struct sctp_nets *)NULL); |
michael@0 | 810 | } else { |
michael@0 | 811 | /* it's ready for use */ |
michael@0 | 812 | sctp_ifap->localifa_flags &= ~SCTP_ADDR_DEFER_USE; |
michael@0 | 813 | } |
michael@0 | 814 | return (sctp_ifap); |
michael@0 | 815 | } |
michael@0 | 816 | |
michael@0 | 817 | void |
michael@0 | 818 | sctp_del_addr_from_vrf(uint32_t vrf_id, struct sockaddr *addr, |
michael@0 | 819 | uint32_t ifn_index, const char *if_name) |
michael@0 | 820 | { |
michael@0 | 821 | struct sctp_vrf *vrf; |
michael@0 | 822 | struct sctp_ifa *sctp_ifap = NULL; |
michael@0 | 823 | |
michael@0 | 824 | SCTP_IPI_ADDR_WLOCK(); |
michael@0 | 825 | vrf = sctp_find_vrf(vrf_id); |
michael@0 | 826 | if (vrf == NULL) { |
michael@0 | 827 | SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id); |
michael@0 | 828 | goto out_now; |
michael@0 | 829 | } |
michael@0 | 830 | |
michael@0 | 831 | #ifdef SCTP_DEBUG |
michael@0 | 832 | SCTPDBG(SCTP_DEBUG_PCB4, "vrf_id 0x%x: deleting address:", vrf_id); |
michael@0 | 833 | SCTPDBG_ADDR(SCTP_DEBUG_PCB4, addr); |
michael@0 | 834 | #endif |
michael@0 | 835 | sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED); |
michael@0 | 836 | if (sctp_ifap) { |
michael@0 | 837 | /* Validate the delete */ |
michael@0 | 838 | if (sctp_ifap->ifn_p) { |
michael@0 | 839 | int valid = 0; |
michael@0 | 840 | /*- |
michael@0 | 841 | * The name has priority over the ifn_index |
michael@0 | 842 | * if its given. We do this especially for |
michael@0 | 843 | * panda who might recycle indexes fast. |
michael@0 | 844 | */ |
michael@0 | 845 | if (if_name) { |
michael@0 | 846 | if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, SCTP_IFNAMSIZ) == 0) { |
michael@0 | 847 | /* They match its a correct delete */ |
michael@0 | 848 | valid = 1; |
michael@0 | 849 | } |
michael@0 | 850 | } |
michael@0 | 851 | if (!valid) { |
michael@0 | 852 | /* last ditch check ifn_index */ |
michael@0 | 853 | if (ifn_index == sctp_ifap->ifn_p->ifn_index) { |
michael@0 | 854 | valid = 1; |
michael@0 | 855 | } |
michael@0 | 856 | } |
michael@0 | 857 | if (!valid) { |
michael@0 | 858 | SCTPDBG(SCTP_DEBUG_PCB4, "ifn:%d ifname:%s does not match addresses\n", |
michael@0 | 859 | ifn_index, ((if_name == NULL) ? "NULL" : if_name)); |
michael@0 | 860 | SCTPDBG(SCTP_DEBUG_PCB4, "ifn:%d ifname:%s - ignoring delete\n", |
michael@0 | 861 | sctp_ifap->ifn_p->ifn_index, sctp_ifap->ifn_p->ifn_name); |
michael@0 | 862 | SCTP_IPI_ADDR_WUNLOCK(); |
michael@0 | 863 | return; |
michael@0 | 864 | } |
michael@0 | 865 | } |
michael@0 | 866 | SCTPDBG(SCTP_DEBUG_PCB4, "Deleting ifa %p\n", (void *)sctp_ifap); |
michael@0 | 867 | sctp_ifap->localifa_flags &= SCTP_ADDR_VALID; |
michael@0 | 868 | sctp_ifap->localifa_flags |= SCTP_BEING_DELETED; |
michael@0 | 869 | vrf->total_ifa_count--; |
michael@0 | 870 | LIST_REMOVE(sctp_ifap, next_bucket); |
michael@0 | 871 | sctp_remove_ifa_from_ifn(sctp_ifap); |
michael@0 | 872 | } |
michael@0 | 873 | #ifdef SCTP_DEBUG |
michael@0 | 874 | else { |
michael@0 | 875 | SCTPDBG(SCTP_DEBUG_PCB4, "Del Addr-ifn:%d Could not find address:", |
michael@0 | 876 | ifn_index); |
michael@0 | 877 | SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr); |
michael@0 | 878 | } |
michael@0 | 879 | #endif |
michael@0 | 880 | |
michael@0 | 881 | out_now: |
michael@0 | 882 | SCTP_IPI_ADDR_WUNLOCK(); |
michael@0 | 883 | if (sctp_ifap) { |
michael@0 | 884 | struct sctp_laddr *wi; |
michael@0 | 885 | |
michael@0 | 886 | wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr); |
michael@0 | 887 | if (wi == NULL) { |
michael@0 | 888 | /* |
michael@0 | 889 | * Gak, what can we do? We have lost an address |
michael@0 | 890 | * change can you say HOSED? |
michael@0 | 891 | */ |
michael@0 | 892 | SCTPDBG(SCTP_DEBUG_PCB4, "Lost an address change?\n"); |
michael@0 | 893 | |
michael@0 | 894 | /* Oops, must decrement the count */ |
michael@0 | 895 | sctp_free_ifa(sctp_ifap); |
michael@0 | 896 | return; |
michael@0 | 897 | } |
michael@0 | 898 | SCTP_INCR_LADDR_COUNT(); |
michael@0 | 899 | bzero(wi, sizeof(*wi)); |
michael@0 | 900 | (void)SCTP_GETTIME_TIMEVAL(&wi->start_time); |
michael@0 | 901 | wi->ifa = sctp_ifap; |
michael@0 | 902 | wi->action = SCTP_DEL_IP_ADDRESS; |
michael@0 | 903 | SCTP_WQ_ADDR_LOCK(); |
michael@0 | 904 | /* |
michael@0 | 905 | * Should this really be a tailq? As it is we will process the |
michael@0 | 906 | * newest first :-0 |
michael@0 | 907 | */ |
michael@0 | 908 | LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr); |
michael@0 | 909 | SCTP_WQ_ADDR_UNLOCK(); |
michael@0 | 910 | |
michael@0 | 911 | sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ, |
michael@0 | 912 | (struct sctp_inpcb *)NULL, |
michael@0 | 913 | (struct sctp_tcb *)NULL, |
michael@0 | 914 | (struct sctp_nets *)NULL); |
michael@0 | 915 | } |
michael@0 | 916 | return; |
michael@0 | 917 | } |
michael@0 | 918 | |
michael@0 | 919 | |
michael@0 | 920 | static int |
michael@0 | 921 | sctp_does_stcb_own_this_addr(struct sctp_tcb *stcb, struct sockaddr *to) |
michael@0 | 922 | { |
michael@0 | 923 | int loopback_scope; |
michael@0 | 924 | #if defined(INET) |
michael@0 | 925 | int ipv4_local_scope, ipv4_addr_legal; |
michael@0 | 926 | #endif |
michael@0 | 927 | #if defined(INET6) |
michael@0 | 928 | int local_scope, site_scope, ipv6_addr_legal; |
michael@0 | 929 | #endif |
michael@0 | 930 | #if defined(__Userspace__) |
michael@0 | 931 | int conn_addr_legal; |
michael@0 | 932 | #endif |
michael@0 | 933 | struct sctp_vrf *vrf; |
michael@0 | 934 | struct sctp_ifn *sctp_ifn; |
michael@0 | 935 | struct sctp_ifa *sctp_ifa; |
michael@0 | 936 | |
michael@0 | 937 | loopback_scope = stcb->asoc.scope.loopback_scope; |
michael@0 | 938 | #if defined(INET) |
michael@0 | 939 | ipv4_local_scope = stcb->asoc.scope.ipv4_local_scope; |
michael@0 | 940 | ipv4_addr_legal = stcb->asoc.scope.ipv4_addr_legal; |
michael@0 | 941 | #endif |
michael@0 | 942 | #if defined(INET6) |
michael@0 | 943 | local_scope = stcb->asoc.scope.local_scope; |
michael@0 | 944 | site_scope = stcb->asoc.scope.site_scope; |
michael@0 | 945 | ipv6_addr_legal = stcb->asoc.scope.ipv6_addr_legal; |
michael@0 | 946 | #endif |
michael@0 | 947 | #if defined(__Userspace__) |
michael@0 | 948 | conn_addr_legal = stcb->asoc.scope.conn_addr_legal; |
michael@0 | 949 | #endif |
michael@0 | 950 | |
michael@0 | 951 | SCTP_IPI_ADDR_RLOCK(); |
michael@0 | 952 | vrf = sctp_find_vrf(stcb->asoc.vrf_id); |
michael@0 | 953 | if (vrf == NULL) { |
michael@0 | 954 | /* no vrf, no addresses */ |
michael@0 | 955 | SCTP_IPI_ADDR_RUNLOCK(); |
michael@0 | 956 | return (0); |
michael@0 | 957 | } |
michael@0 | 958 | |
michael@0 | 959 | if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { |
michael@0 | 960 | LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) { |
michael@0 | 961 | if ((loopback_scope == 0) && |
michael@0 | 962 | SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) { |
michael@0 | 963 | continue; |
michael@0 | 964 | } |
michael@0 | 965 | LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) { |
michael@0 | 966 | if (sctp_is_addr_restricted(stcb, sctp_ifa) && |
michael@0 | 967 | (!sctp_is_addr_pending(stcb, sctp_ifa))) { |
michael@0 | 968 | /* We allow pending addresses, where we |
michael@0 | 969 | * have sent an asconf-add to be considered |
michael@0 | 970 | * valid. |
michael@0 | 971 | */ |
michael@0 | 972 | continue; |
michael@0 | 973 | } |
michael@0 | 974 | if (sctp_ifa->address.sa.sa_family != to->sa_family) { |
michael@0 | 975 | continue; |
michael@0 | 976 | } |
michael@0 | 977 | switch (sctp_ifa->address.sa.sa_family) { |
michael@0 | 978 | #ifdef INET |
michael@0 | 979 | case AF_INET: |
michael@0 | 980 | if (ipv4_addr_legal) { |
michael@0 | 981 | struct sockaddr_in *sin, *rsin; |
michael@0 | 982 | |
michael@0 | 983 | sin = &sctp_ifa->address.sin; |
michael@0 | 984 | rsin = (struct sockaddr_in *)to; |
michael@0 | 985 | if ((ipv4_local_scope == 0) && |
michael@0 | 986 | IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) { |
michael@0 | 987 | continue; |
michael@0 | 988 | } |
michael@0 | 989 | if (sin->sin_addr.s_addr == rsin->sin_addr.s_addr) { |
michael@0 | 990 | SCTP_IPI_ADDR_RUNLOCK(); |
michael@0 | 991 | return (1); |
michael@0 | 992 | } |
michael@0 | 993 | } |
michael@0 | 994 | break; |
michael@0 | 995 | #endif |
michael@0 | 996 | #ifdef INET6 |
michael@0 | 997 | case AF_INET6: |
michael@0 | 998 | if (ipv6_addr_legal) { |
michael@0 | 999 | struct sockaddr_in6 *sin6, *rsin6; |
michael@0 | 1000 | #if defined(SCTP_EMBEDDED_V6_SCOPE) && !defined(SCTP_KAME) |
michael@0 | 1001 | struct sockaddr_in6 lsa6; |
michael@0 | 1002 | #endif |
michael@0 | 1003 | sin6 = &sctp_ifa->address.sin6; |
michael@0 | 1004 | rsin6 = (struct sockaddr_in6 *)to; |
michael@0 | 1005 | if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { |
michael@0 | 1006 | if (local_scope == 0) |
michael@0 | 1007 | continue; |
michael@0 | 1008 | #if defined(SCTP_EMBEDDED_V6_SCOPE) |
michael@0 | 1009 | if (sin6->sin6_scope_id == 0) { |
michael@0 | 1010 | #ifdef SCTP_KAME |
michael@0 | 1011 | if (sa6_recoverscope(sin6) != 0) |
michael@0 | 1012 | continue; |
michael@0 | 1013 | #else |
michael@0 | 1014 | lsa6 = *sin6; |
michael@0 | 1015 | if (in6_recoverscope(&lsa6, |
michael@0 | 1016 | &lsa6.sin6_addr, |
michael@0 | 1017 | NULL)) |
michael@0 | 1018 | continue; |
michael@0 | 1019 | sin6 = &lsa6; |
michael@0 | 1020 | #endif /* SCTP_KAME */ |
michael@0 | 1021 | } |
michael@0 | 1022 | #endif /* SCTP_EMBEDDED_V6_SCOPE */ |
michael@0 | 1023 | } |
michael@0 | 1024 | if ((site_scope == 0) && |
michael@0 | 1025 | (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) { |
michael@0 | 1026 | continue; |
michael@0 | 1027 | } |
michael@0 | 1028 | if (SCTP6_ARE_ADDR_EQUAL(sin6, rsin6)) { |
michael@0 | 1029 | SCTP_IPI_ADDR_RUNLOCK(); |
michael@0 | 1030 | return (1); |
michael@0 | 1031 | } |
michael@0 | 1032 | } |
michael@0 | 1033 | break; |
michael@0 | 1034 | #endif |
michael@0 | 1035 | #if defined(__Userspace__) |
michael@0 | 1036 | case AF_CONN: |
michael@0 | 1037 | if (conn_addr_legal) { |
michael@0 | 1038 | struct sockaddr_conn *sconn, *rsconn; |
michael@0 | 1039 | |
michael@0 | 1040 | sconn = &sctp_ifa->address.sconn; |
michael@0 | 1041 | rsconn = (struct sockaddr_conn *)to; |
michael@0 | 1042 | if (sconn->sconn_addr == rsconn->sconn_addr) { |
michael@0 | 1043 | SCTP_IPI_ADDR_RUNLOCK(); |
michael@0 | 1044 | return (1); |
michael@0 | 1045 | } |
michael@0 | 1046 | } |
michael@0 | 1047 | break; |
michael@0 | 1048 | #endif |
michael@0 | 1049 | default: |
michael@0 | 1050 | /* TSNH */ |
michael@0 | 1051 | break; |
michael@0 | 1052 | } |
michael@0 | 1053 | } |
michael@0 | 1054 | } |
michael@0 | 1055 | } else { |
michael@0 | 1056 | struct sctp_laddr *laddr; |
michael@0 | 1057 | |
michael@0 | 1058 | LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) { |
michael@0 | 1059 | if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) { |
michael@0 | 1060 | SCTPDBG(SCTP_DEBUG_PCB1, "ifa being deleted\n"); |
michael@0 | 1061 | continue; |
michael@0 | 1062 | } |
michael@0 | 1063 | if (sctp_is_addr_restricted(stcb, laddr->ifa) && |
michael@0 | 1064 | (!sctp_is_addr_pending(stcb, laddr->ifa))) { |
michael@0 | 1065 | /* We allow pending addresses, where we |
michael@0 | 1066 | * have sent an asconf-add to be considered |
michael@0 | 1067 | * valid. |
michael@0 | 1068 | */ |
michael@0 | 1069 | continue; |
michael@0 | 1070 | } |
michael@0 | 1071 | if (laddr->ifa->address.sa.sa_family != to->sa_family) { |
michael@0 | 1072 | continue; |
michael@0 | 1073 | } |
michael@0 | 1074 | switch (to->sa_family) { |
michael@0 | 1075 | #ifdef INET |
michael@0 | 1076 | case AF_INET: |
michael@0 | 1077 | { |
michael@0 | 1078 | struct sockaddr_in *sin, *rsin; |
michael@0 | 1079 | |
michael@0 | 1080 | sin = (struct sockaddr_in *)&laddr->ifa->address.sin; |
michael@0 | 1081 | rsin = (struct sockaddr_in *)to; |
michael@0 | 1082 | if (sin->sin_addr.s_addr == rsin->sin_addr.s_addr) { |
michael@0 | 1083 | SCTP_IPI_ADDR_RUNLOCK(); |
michael@0 | 1084 | return (1); |
michael@0 | 1085 | } |
michael@0 | 1086 | break; |
michael@0 | 1087 | } |
michael@0 | 1088 | #endif |
michael@0 | 1089 | #ifdef INET6 |
michael@0 | 1090 | case AF_INET6: |
michael@0 | 1091 | { |
michael@0 | 1092 | struct sockaddr_in6 *sin6, *rsin6; |
michael@0 | 1093 | |
michael@0 | 1094 | sin6 = (struct sockaddr_in6 *)&laddr->ifa->address.sin6; |
michael@0 | 1095 | rsin6 = (struct sockaddr_in6 *)to; |
michael@0 | 1096 | if (SCTP6_ARE_ADDR_EQUAL(sin6, rsin6)) { |
michael@0 | 1097 | SCTP_IPI_ADDR_RUNLOCK(); |
michael@0 | 1098 | return (1); |
michael@0 | 1099 | } |
michael@0 | 1100 | break; |
michael@0 | 1101 | } |
michael@0 | 1102 | |
michael@0 | 1103 | #endif |
michael@0 | 1104 | #if defined(__Userspace__) |
michael@0 | 1105 | case AF_CONN: |
michael@0 | 1106 | { |
michael@0 | 1107 | struct sockaddr_conn *sconn, *rsconn; |
michael@0 | 1108 | |
michael@0 | 1109 | sconn = (struct sockaddr_conn *)&laddr->ifa->address.sconn; |
michael@0 | 1110 | rsconn = (struct sockaddr_conn *)to; |
michael@0 | 1111 | if (sconn->sconn_addr == rsconn->sconn_addr) { |
michael@0 | 1112 | SCTP_IPI_ADDR_RUNLOCK(); |
michael@0 | 1113 | return (1); |
michael@0 | 1114 | } |
michael@0 | 1115 | break; |
michael@0 | 1116 | } |
michael@0 | 1117 | #endif |
michael@0 | 1118 | default: |
michael@0 | 1119 | /* TSNH */ |
michael@0 | 1120 | break; |
michael@0 | 1121 | } |
michael@0 | 1122 | |
michael@0 | 1123 | } |
michael@0 | 1124 | } |
michael@0 | 1125 | SCTP_IPI_ADDR_RUNLOCK(); |
michael@0 | 1126 | return (0); |
michael@0 | 1127 | } |
michael@0 | 1128 | |
michael@0 | 1129 | |
michael@0 | 1130 | static struct sctp_tcb * |
michael@0 | 1131 | sctp_tcb_special_locate(struct sctp_inpcb **inp_p, struct sockaddr *from, |
michael@0 | 1132 | struct sockaddr *to, struct sctp_nets **netp, uint32_t vrf_id) |
michael@0 | 1133 | { |
michael@0 | 1134 | /**** ASSUMES THE CALLER holds the INP_INFO_RLOCK */ |
michael@0 | 1135 | /* |
michael@0 | 1136 | * If we support the TCP model, then we must now dig through to see |
michael@0 | 1137 | * if we can find our endpoint in the list of tcp ep's. |
michael@0 | 1138 | */ |
michael@0 | 1139 | uint16_t lport, rport; |
michael@0 | 1140 | struct sctppcbhead *ephead; |
michael@0 | 1141 | struct sctp_inpcb *inp; |
michael@0 | 1142 | struct sctp_laddr *laddr; |
michael@0 | 1143 | struct sctp_tcb *stcb; |
michael@0 | 1144 | struct sctp_nets *net; |
michael@0 | 1145 | #ifdef SCTP_MVRF |
michael@0 | 1146 | int fnd, i; |
michael@0 | 1147 | #endif |
michael@0 | 1148 | |
michael@0 | 1149 | if ((to == NULL) || (from == NULL)) { |
michael@0 | 1150 | return (NULL); |
michael@0 | 1151 | } |
michael@0 | 1152 | |
michael@0 | 1153 | switch (to->sa_family) { |
michael@0 | 1154 | #ifdef INET |
michael@0 | 1155 | case AF_INET: |
michael@0 | 1156 | if (from->sa_family == AF_INET) { |
michael@0 | 1157 | lport = ((struct sockaddr_in *)to)->sin_port; |
michael@0 | 1158 | rport = ((struct sockaddr_in *)from)->sin_port; |
michael@0 | 1159 | } else { |
michael@0 | 1160 | return (NULL); |
michael@0 | 1161 | } |
michael@0 | 1162 | break; |
michael@0 | 1163 | #endif |
michael@0 | 1164 | #ifdef INET6 |
michael@0 | 1165 | case AF_INET6: |
michael@0 | 1166 | if (from->sa_family == AF_INET6) { |
michael@0 | 1167 | lport = ((struct sockaddr_in6 *)to)->sin6_port; |
michael@0 | 1168 | rport = ((struct sockaddr_in6 *)from)->sin6_port; |
michael@0 | 1169 | } else { |
michael@0 | 1170 | return (NULL); |
michael@0 | 1171 | } |
michael@0 | 1172 | break; |
michael@0 | 1173 | #endif |
michael@0 | 1174 | #if defined(__Userspace__) |
michael@0 | 1175 | case AF_CONN: |
michael@0 | 1176 | if (from->sa_family == AF_CONN) { |
michael@0 | 1177 | lport = ((struct sockaddr_conn *)to)->sconn_port; |
michael@0 | 1178 | rport = ((struct sockaddr_conn *)from)->sconn_port; |
michael@0 | 1179 | } else { |
michael@0 | 1180 | return (NULL); |
michael@0 | 1181 | } |
michael@0 | 1182 | break; |
michael@0 | 1183 | #endif |
michael@0 | 1184 | default: |
michael@0 | 1185 | return (NULL); |
michael@0 | 1186 | } |
michael@0 | 1187 | ephead = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR((lport | rport), SCTP_BASE_INFO(hashtcpmark))]; |
michael@0 | 1188 | /* |
michael@0 | 1189 | * Ok now for each of the guys in this bucket we must look and see: |
michael@0 | 1190 | * - Does the remote port match. - Does there single association's |
michael@0 | 1191 | * addresses match this address (to). If so we update p_ep to point |
michael@0 | 1192 | * to this ep and return the tcb from it. |
michael@0 | 1193 | */ |
michael@0 | 1194 | LIST_FOREACH(inp, ephead, sctp_hash) { |
michael@0 | 1195 | SCTP_INP_RLOCK(inp); |
michael@0 | 1196 | if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
michael@0 | 1197 | SCTP_INP_RUNLOCK(inp); |
michael@0 | 1198 | continue; |
michael@0 | 1199 | } |
michael@0 | 1200 | if (lport != inp->sctp_lport) { |
michael@0 | 1201 | SCTP_INP_RUNLOCK(inp); |
michael@0 | 1202 | continue; |
michael@0 | 1203 | } |
michael@0 | 1204 | #ifdef SCTP_MVRF |
michael@0 | 1205 | fnd = 0; |
michael@0 | 1206 | for (i = 0; i < inp->num_vrfs; i++) { |
michael@0 | 1207 | if (inp->m_vrf_ids[i] == vrf_id) { |
michael@0 | 1208 | fnd = 1; |
michael@0 | 1209 | break; |
michael@0 | 1210 | } |
michael@0 | 1211 | } |
michael@0 | 1212 | if (fnd == 0) { |
michael@0 | 1213 | SCTP_INP_RUNLOCK(inp); |
michael@0 | 1214 | continue; |
michael@0 | 1215 | } |
michael@0 | 1216 | #else |
michael@0 | 1217 | if (inp->def_vrf_id != vrf_id) { |
michael@0 | 1218 | SCTP_INP_RUNLOCK(inp); |
michael@0 | 1219 | continue; |
michael@0 | 1220 | } |
michael@0 | 1221 | #endif |
michael@0 | 1222 | /* check to see if the ep has one of the addresses */ |
michael@0 | 1223 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) { |
michael@0 | 1224 | /* We are NOT bound all, so look further */ |
michael@0 | 1225 | int match = 0; |
michael@0 | 1226 | |
michael@0 | 1227 | LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { |
michael@0 | 1228 | |
michael@0 | 1229 | if (laddr->ifa == NULL) { |
michael@0 | 1230 | SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n", __FUNCTION__); |
michael@0 | 1231 | continue; |
michael@0 | 1232 | } |
michael@0 | 1233 | if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) { |
michael@0 | 1234 | SCTPDBG(SCTP_DEBUG_PCB1, "ifa being deleted\n"); |
michael@0 | 1235 | continue; |
michael@0 | 1236 | } |
michael@0 | 1237 | if (laddr->ifa->address.sa.sa_family == |
michael@0 | 1238 | to->sa_family) { |
michael@0 | 1239 | /* see if it matches */ |
michael@0 | 1240 | #ifdef INET |
michael@0 | 1241 | if (from->sa_family == AF_INET) { |
michael@0 | 1242 | struct sockaddr_in *intf_addr, *sin; |
michael@0 | 1243 | |
michael@0 | 1244 | intf_addr = &laddr->ifa->address.sin; |
michael@0 | 1245 | sin = (struct sockaddr_in *)to; |
michael@0 | 1246 | if (sin->sin_addr.s_addr == |
michael@0 | 1247 | intf_addr->sin_addr.s_addr) { |
michael@0 | 1248 | match = 1; |
michael@0 | 1249 | break; |
michael@0 | 1250 | } |
michael@0 | 1251 | } |
michael@0 | 1252 | #endif |
michael@0 | 1253 | #ifdef INET6 |
michael@0 | 1254 | if (from->sa_family == AF_INET6) { |
michael@0 | 1255 | struct sockaddr_in6 *intf_addr6; |
michael@0 | 1256 | struct sockaddr_in6 *sin6; |
michael@0 | 1257 | |
michael@0 | 1258 | sin6 = (struct sockaddr_in6 *) |
michael@0 | 1259 | to; |
michael@0 | 1260 | intf_addr6 = &laddr->ifa->address.sin6; |
michael@0 | 1261 | |
michael@0 | 1262 | if (SCTP6_ARE_ADDR_EQUAL(sin6, |
michael@0 | 1263 | intf_addr6)) { |
michael@0 | 1264 | match = 1; |
michael@0 | 1265 | break; |
michael@0 | 1266 | } |
michael@0 | 1267 | } |
michael@0 | 1268 | #endif |
michael@0 | 1269 | #if defined(__Userspace__) |
michael@0 | 1270 | if (from->sa_family == AF_CONN) { |
michael@0 | 1271 | struct sockaddr_conn *intf_addr, *sconn; |
michael@0 | 1272 | |
michael@0 | 1273 | intf_addr = &laddr->ifa->address.sconn; |
michael@0 | 1274 | sconn = (struct sockaddr_conn *)to; |
michael@0 | 1275 | if (sconn->sconn_addr == |
michael@0 | 1276 | intf_addr->sconn_addr) { |
michael@0 | 1277 | match = 1; |
michael@0 | 1278 | break; |
michael@0 | 1279 | } |
michael@0 | 1280 | } |
michael@0 | 1281 | #endif |
michael@0 | 1282 | } |
michael@0 | 1283 | } |
michael@0 | 1284 | if (match == 0) { |
michael@0 | 1285 | /* This endpoint does not have this address */ |
michael@0 | 1286 | SCTP_INP_RUNLOCK(inp); |
michael@0 | 1287 | continue; |
michael@0 | 1288 | } |
michael@0 | 1289 | } |
michael@0 | 1290 | /* |
michael@0 | 1291 | * Ok if we hit here the ep has the address, does it hold |
michael@0 | 1292 | * the tcb? |
michael@0 | 1293 | */ |
michael@0 | 1294 | /* XXX: Why don't we TAILQ_FOREACH through sctp_asoc_list? */ |
michael@0 | 1295 | stcb = LIST_FIRST(&inp->sctp_asoc_list); |
michael@0 | 1296 | if (stcb == NULL) { |
michael@0 | 1297 | SCTP_INP_RUNLOCK(inp); |
michael@0 | 1298 | continue; |
michael@0 | 1299 | } |
michael@0 | 1300 | SCTP_TCB_LOCK(stcb); |
michael@0 | 1301 | if (!sctp_does_stcb_own_this_addr(stcb, to)) { |
michael@0 | 1302 | SCTP_TCB_UNLOCK(stcb); |
michael@0 | 1303 | SCTP_INP_RUNLOCK(inp); |
michael@0 | 1304 | continue; |
michael@0 | 1305 | } |
michael@0 | 1306 | if (stcb->rport != rport) { |
michael@0 | 1307 | /* remote port does not match. */ |
michael@0 | 1308 | SCTP_TCB_UNLOCK(stcb); |
michael@0 | 1309 | SCTP_INP_RUNLOCK(inp); |
michael@0 | 1310 | continue; |
michael@0 | 1311 | } |
michael@0 | 1312 | if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { |
michael@0 | 1313 | SCTP_TCB_UNLOCK(stcb); |
michael@0 | 1314 | SCTP_INP_RUNLOCK(inp); |
michael@0 | 1315 | continue; |
michael@0 | 1316 | } |
michael@0 | 1317 | if (!sctp_does_stcb_own_this_addr(stcb, to)) { |
michael@0 | 1318 | SCTP_TCB_UNLOCK(stcb); |
michael@0 | 1319 | SCTP_INP_RUNLOCK(inp); |
michael@0 | 1320 | continue; |
michael@0 | 1321 | } |
michael@0 | 1322 | /* Does this TCB have a matching address? */ |
michael@0 | 1323 | TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { |
michael@0 | 1324 | |
michael@0 | 1325 | if (net->ro._l_addr.sa.sa_family != from->sa_family) { |
michael@0 | 1326 | /* not the same family, can't be a match */ |
michael@0 | 1327 | continue; |
michael@0 | 1328 | } |
michael@0 | 1329 | switch (from->sa_family) { |
michael@0 | 1330 | #ifdef INET |
michael@0 | 1331 | case AF_INET: |
michael@0 | 1332 | { |
michael@0 | 1333 | struct sockaddr_in *sin, *rsin; |
michael@0 | 1334 | |
michael@0 | 1335 | sin = (struct sockaddr_in *)&net->ro._l_addr; |
michael@0 | 1336 | rsin = (struct sockaddr_in *)from; |
michael@0 | 1337 | if (sin->sin_addr.s_addr == |
michael@0 | 1338 | rsin->sin_addr.s_addr) { |
michael@0 | 1339 | /* found it */ |
michael@0 | 1340 | if (netp != NULL) { |
michael@0 | 1341 | *netp = net; |
michael@0 | 1342 | } |
michael@0 | 1343 | /* Update the endpoint pointer */ |
michael@0 | 1344 | *inp_p = inp; |
michael@0 | 1345 | SCTP_INP_RUNLOCK(inp); |
michael@0 | 1346 | return (stcb); |
michael@0 | 1347 | } |
michael@0 | 1348 | break; |
michael@0 | 1349 | } |
michael@0 | 1350 | #endif |
michael@0 | 1351 | #ifdef INET6 |
michael@0 | 1352 | case AF_INET6: |
michael@0 | 1353 | { |
michael@0 | 1354 | struct sockaddr_in6 *sin6, *rsin6; |
michael@0 | 1355 | |
michael@0 | 1356 | sin6 = (struct sockaddr_in6 *)&net->ro._l_addr; |
michael@0 | 1357 | rsin6 = (struct sockaddr_in6 *)from; |
michael@0 | 1358 | if (SCTP6_ARE_ADDR_EQUAL(sin6, |
michael@0 | 1359 | rsin6)) { |
michael@0 | 1360 | /* found it */ |
michael@0 | 1361 | if (netp != NULL) { |
michael@0 | 1362 | *netp = net; |
michael@0 | 1363 | } |
michael@0 | 1364 | /* Update the endpoint pointer */ |
michael@0 | 1365 | *inp_p = inp; |
michael@0 | 1366 | SCTP_INP_RUNLOCK(inp); |
michael@0 | 1367 | return (stcb); |
michael@0 | 1368 | } |
michael@0 | 1369 | break; |
michael@0 | 1370 | } |
michael@0 | 1371 | #endif |
michael@0 | 1372 | #if defined(__Userspace__) |
michael@0 | 1373 | case AF_CONN: |
michael@0 | 1374 | { |
michael@0 | 1375 | struct sockaddr_conn *sconn, *rsconn; |
michael@0 | 1376 | |
michael@0 | 1377 | sconn = (struct sockaddr_conn *)&net->ro._l_addr; |
michael@0 | 1378 | rsconn = (struct sockaddr_conn *)from; |
michael@0 | 1379 | if (sconn->sconn_addr == rsconn->sconn_addr) { |
michael@0 | 1380 | /* found it */ |
michael@0 | 1381 | if (netp != NULL) { |
michael@0 | 1382 | *netp = net; |
michael@0 | 1383 | } |
michael@0 | 1384 | /* Update the endpoint pointer */ |
michael@0 | 1385 | *inp_p = inp; |
michael@0 | 1386 | SCTP_INP_RUNLOCK(inp); |
michael@0 | 1387 | return (stcb); |
michael@0 | 1388 | } |
michael@0 | 1389 | break; |
michael@0 | 1390 | } |
michael@0 | 1391 | #endif |
michael@0 | 1392 | default: |
michael@0 | 1393 | /* TSNH */ |
michael@0 | 1394 | break; |
michael@0 | 1395 | } |
michael@0 | 1396 | } |
michael@0 | 1397 | SCTP_TCB_UNLOCK(stcb); |
michael@0 | 1398 | SCTP_INP_RUNLOCK(inp); |
michael@0 | 1399 | } |
michael@0 | 1400 | return (NULL); |
michael@0 | 1401 | } |
michael@0 | 1402 | |
michael@0 | 1403 | |
michael@0 | 1404 | /* |
michael@0 | 1405 | * rules for use |
michael@0 | 1406 | * |
michael@0 | 1407 | * 1) If I return a NULL you must decrement any INP ref cnt. 2) If I find an |
michael@0 | 1408 | * stcb, both will be locked (locked_tcb and stcb) but decrement will be done |
michael@0 | 1409 | * (if locked == NULL). 3) Decrement happens on return ONLY if locked == |
michael@0 | 1410 | * NULL. |
michael@0 | 1411 | */ |
michael@0 | 1412 | |
michael@0 | 1413 | struct sctp_tcb * |
michael@0 | 1414 | sctp_findassociation_ep_addr(struct sctp_inpcb **inp_p, struct sockaddr *remote, |
michael@0 | 1415 | struct sctp_nets **netp, struct sockaddr *local, struct sctp_tcb *locked_tcb) |
michael@0 | 1416 | { |
michael@0 | 1417 | struct sctpasochead *head; |
michael@0 | 1418 | struct sctp_inpcb *inp; |
michael@0 | 1419 | struct sctp_tcb *stcb = NULL; |
michael@0 | 1420 | struct sctp_nets *net; |
michael@0 | 1421 | uint16_t rport; |
michael@0 | 1422 | |
michael@0 | 1423 | inp = *inp_p; |
michael@0 | 1424 | switch (remote->sa_family) { |
michael@0 | 1425 | #ifdef INET |
michael@0 | 1426 | case AF_INET: |
michael@0 | 1427 | rport = (((struct sockaddr_in *)remote)->sin_port); |
michael@0 | 1428 | break; |
michael@0 | 1429 | #endif |
michael@0 | 1430 | #ifdef INET6 |
michael@0 | 1431 | case AF_INET6: |
michael@0 | 1432 | rport = (((struct sockaddr_in6 *)remote)->sin6_port); |
michael@0 | 1433 | break; |
michael@0 | 1434 | #endif |
michael@0 | 1435 | #if defined(__Userspace__) |
michael@0 | 1436 | case AF_CONN: |
michael@0 | 1437 | rport = (((struct sockaddr_in6 *)remote)->sin6_port); |
michael@0 | 1438 | break; |
michael@0 | 1439 | #endif |
michael@0 | 1440 | default: |
michael@0 | 1441 | return (NULL); |
michael@0 | 1442 | } |
michael@0 | 1443 | if (locked_tcb) { |
michael@0 | 1444 | /* |
michael@0 | 1445 | * UN-lock so we can do proper locking here this occurs when |
michael@0 | 1446 | * called from load_addresses_from_init. |
michael@0 | 1447 | */ |
michael@0 | 1448 | atomic_add_int(&locked_tcb->asoc.refcnt, 1); |
michael@0 | 1449 | SCTP_TCB_UNLOCK(locked_tcb); |
michael@0 | 1450 | } |
michael@0 | 1451 | SCTP_INP_INFO_RLOCK(); |
michael@0 | 1452 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || |
michael@0 | 1453 | (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { |
michael@0 | 1454 | /*- |
michael@0 | 1455 | * Now either this guy is our listener or it's the |
michael@0 | 1456 | * connector. If it is the one that issued the connect, then |
michael@0 | 1457 | * it's only chance is to be the first TCB in the list. If |
michael@0 | 1458 | * it is the acceptor, then do the special_lookup to hash |
michael@0 | 1459 | * and find the real inp. |
michael@0 | 1460 | */ |
michael@0 | 1461 | if ((inp->sctp_socket) && (inp->sctp_socket->so_qlimit)) { |
michael@0 | 1462 | /* to is peer addr, from is my addr */ |
michael@0 | 1463 | #ifndef SCTP_MVRF |
michael@0 | 1464 | stcb = sctp_tcb_special_locate(inp_p, remote, local, |
michael@0 | 1465 | netp, inp->def_vrf_id); |
michael@0 | 1466 | if ((stcb != NULL) && (locked_tcb == NULL)) { |
michael@0 | 1467 | /* we have a locked tcb, lower refcount */ |
michael@0 | 1468 | SCTP_INP_DECR_REF(inp); |
michael@0 | 1469 | } |
michael@0 | 1470 | if ((locked_tcb != NULL) && (locked_tcb != stcb)) { |
michael@0 | 1471 | SCTP_INP_RLOCK(locked_tcb->sctp_ep); |
michael@0 | 1472 | SCTP_TCB_LOCK(locked_tcb); |
michael@0 | 1473 | atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); |
michael@0 | 1474 | SCTP_INP_RUNLOCK(locked_tcb->sctp_ep); |
michael@0 | 1475 | } |
michael@0 | 1476 | #else |
michael@0 | 1477 | /*- |
michael@0 | 1478 | * MVRF is tricky, we must look in every VRF |
michael@0 | 1479 | * the endpoint has. |
michael@0 | 1480 | */ |
michael@0 | 1481 | int i; |
michael@0 | 1482 | |
michael@0 | 1483 | for (i = 0; i < inp->num_vrfs; i++) { |
michael@0 | 1484 | stcb = sctp_tcb_special_locate(inp_p, remote, local, |
michael@0 | 1485 | netp, inp->m_vrf_ids[i]); |
michael@0 | 1486 | if ((stcb != NULL) && (locked_tcb == NULL)) { |
michael@0 | 1487 | /* we have a locked tcb, lower refcount */ |
michael@0 | 1488 | SCTP_INP_DECR_REF(inp); |
michael@0 | 1489 | break; |
michael@0 | 1490 | } |
michael@0 | 1491 | if ((locked_tcb != NULL) && (locked_tcb != stcb)) { |
michael@0 | 1492 | SCTP_INP_RLOCK(locked_tcb->sctp_ep); |
michael@0 | 1493 | SCTP_TCB_LOCK(locked_tcb); |
michael@0 | 1494 | atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); |
michael@0 | 1495 | SCTP_INP_RUNLOCK(locked_tcb->sctp_ep); |
michael@0 | 1496 | break; |
michael@0 | 1497 | } |
michael@0 | 1498 | } |
michael@0 | 1499 | #endif |
michael@0 | 1500 | SCTP_INP_INFO_RUNLOCK(); |
michael@0 | 1501 | return (stcb); |
michael@0 | 1502 | } else { |
michael@0 | 1503 | SCTP_INP_WLOCK(inp); |
michael@0 | 1504 | if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
michael@0 | 1505 | goto null_return; |
michael@0 | 1506 | } |
michael@0 | 1507 | stcb = LIST_FIRST(&inp->sctp_asoc_list); |
michael@0 | 1508 | if (stcb == NULL) { |
michael@0 | 1509 | goto null_return; |
michael@0 | 1510 | } |
michael@0 | 1511 | SCTP_TCB_LOCK(stcb); |
michael@0 | 1512 | |
michael@0 | 1513 | if (stcb->rport != rport) { |
michael@0 | 1514 | /* remote port does not match. */ |
michael@0 | 1515 | SCTP_TCB_UNLOCK(stcb); |
michael@0 | 1516 | goto null_return; |
michael@0 | 1517 | } |
michael@0 | 1518 | if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { |
michael@0 | 1519 | SCTP_TCB_UNLOCK(stcb); |
michael@0 | 1520 | goto null_return; |
michael@0 | 1521 | } |
michael@0 | 1522 | if (local && !sctp_does_stcb_own_this_addr(stcb, local)) { |
michael@0 | 1523 | SCTP_TCB_UNLOCK(stcb); |
michael@0 | 1524 | goto null_return; |
michael@0 | 1525 | } |
michael@0 | 1526 | /* now look at the list of remote addresses */ |
michael@0 | 1527 | TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { |
michael@0 | 1528 | #ifdef INVARIANTS |
michael@0 | 1529 | if (net == (TAILQ_NEXT(net, sctp_next))) { |
michael@0 | 1530 | panic("Corrupt net list"); |
michael@0 | 1531 | } |
michael@0 | 1532 | #endif |
michael@0 | 1533 | if (net->ro._l_addr.sa.sa_family != |
michael@0 | 1534 | remote->sa_family) { |
michael@0 | 1535 | /* not the same family */ |
michael@0 | 1536 | continue; |
michael@0 | 1537 | } |
michael@0 | 1538 | switch (remote->sa_family) { |
michael@0 | 1539 | #ifdef INET |
michael@0 | 1540 | case AF_INET: |
michael@0 | 1541 | { |
michael@0 | 1542 | struct sockaddr_in *sin, *rsin; |
michael@0 | 1543 | |
michael@0 | 1544 | sin = (struct sockaddr_in *) |
michael@0 | 1545 | &net->ro._l_addr; |
michael@0 | 1546 | rsin = (struct sockaddr_in *)remote; |
michael@0 | 1547 | if (sin->sin_addr.s_addr == |
michael@0 | 1548 | rsin->sin_addr.s_addr) { |
michael@0 | 1549 | /* found it */ |
michael@0 | 1550 | if (netp != NULL) { |
michael@0 | 1551 | *netp = net; |
michael@0 | 1552 | } |
michael@0 | 1553 | if (locked_tcb == NULL) { |
michael@0 | 1554 | SCTP_INP_DECR_REF(inp); |
michael@0 | 1555 | } else if (locked_tcb != stcb) { |
michael@0 | 1556 | SCTP_TCB_LOCK(locked_tcb); |
michael@0 | 1557 | } |
michael@0 | 1558 | if (locked_tcb) { |
michael@0 | 1559 | atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); |
michael@0 | 1560 | } |
michael@0 | 1561 | |
michael@0 | 1562 | SCTP_INP_WUNLOCK(inp); |
michael@0 | 1563 | SCTP_INP_INFO_RUNLOCK(); |
michael@0 | 1564 | return (stcb); |
michael@0 | 1565 | } |
michael@0 | 1566 | break; |
michael@0 | 1567 | } |
michael@0 | 1568 | #endif |
michael@0 | 1569 | #ifdef INET6 |
michael@0 | 1570 | case AF_INET6: |
michael@0 | 1571 | { |
michael@0 | 1572 | struct sockaddr_in6 *sin6, *rsin6; |
michael@0 | 1573 | |
michael@0 | 1574 | sin6 = (struct sockaddr_in6 *)&net->ro._l_addr; |
michael@0 | 1575 | rsin6 = (struct sockaddr_in6 *)remote; |
michael@0 | 1576 | if (SCTP6_ARE_ADDR_EQUAL(sin6, |
michael@0 | 1577 | rsin6)) { |
michael@0 | 1578 | /* found it */ |
michael@0 | 1579 | if (netp != NULL) { |
michael@0 | 1580 | *netp = net; |
michael@0 | 1581 | } |
michael@0 | 1582 | if (locked_tcb == NULL) { |
michael@0 | 1583 | SCTP_INP_DECR_REF(inp); |
michael@0 | 1584 | } else if (locked_tcb != stcb) { |
michael@0 | 1585 | SCTP_TCB_LOCK(locked_tcb); |
michael@0 | 1586 | } |
michael@0 | 1587 | if (locked_tcb) { |
michael@0 | 1588 | atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); |
michael@0 | 1589 | } |
michael@0 | 1590 | SCTP_INP_WUNLOCK(inp); |
michael@0 | 1591 | SCTP_INP_INFO_RUNLOCK(); |
michael@0 | 1592 | return (stcb); |
michael@0 | 1593 | } |
michael@0 | 1594 | break; |
michael@0 | 1595 | } |
michael@0 | 1596 | #endif |
michael@0 | 1597 | #if defined(__Userspace__) |
michael@0 | 1598 | case AF_CONN: |
michael@0 | 1599 | { |
michael@0 | 1600 | struct sockaddr_conn *sconn, *rsconn; |
michael@0 | 1601 | |
michael@0 | 1602 | sconn = (struct sockaddr_conn *)&net->ro._l_addr; |
michael@0 | 1603 | rsconn = (struct sockaddr_conn *)remote; |
michael@0 | 1604 | if (sconn->sconn_addr == rsconn->sconn_addr) { |
michael@0 | 1605 | /* found it */ |
michael@0 | 1606 | if (netp != NULL) { |
michael@0 | 1607 | *netp = net; |
michael@0 | 1608 | } |
michael@0 | 1609 | if (locked_tcb == NULL) { |
michael@0 | 1610 | SCTP_INP_DECR_REF(inp); |
michael@0 | 1611 | } else if (locked_tcb != stcb) { |
michael@0 | 1612 | SCTP_TCB_LOCK(locked_tcb); |
michael@0 | 1613 | } |
michael@0 | 1614 | if (locked_tcb) { |
michael@0 | 1615 | atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); |
michael@0 | 1616 | } |
michael@0 | 1617 | SCTP_INP_WUNLOCK(inp); |
michael@0 | 1618 | SCTP_INP_INFO_RUNLOCK(); |
michael@0 | 1619 | return (stcb); |
michael@0 | 1620 | } |
michael@0 | 1621 | break; |
michael@0 | 1622 | } |
michael@0 | 1623 | #endif |
michael@0 | 1624 | default: |
michael@0 | 1625 | /* TSNH */ |
michael@0 | 1626 | break; |
michael@0 | 1627 | } |
michael@0 | 1628 | } |
michael@0 | 1629 | SCTP_TCB_UNLOCK(stcb); |
michael@0 | 1630 | } |
michael@0 | 1631 | } else { |
michael@0 | 1632 | SCTP_INP_WLOCK(inp); |
michael@0 | 1633 | if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
michael@0 | 1634 | goto null_return; |
michael@0 | 1635 | } |
michael@0 | 1636 | head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(rport, |
michael@0 | 1637 | inp->sctp_hashmark)]; |
michael@0 | 1638 | if (head == NULL) { |
michael@0 | 1639 | goto null_return; |
michael@0 | 1640 | } |
michael@0 | 1641 | LIST_FOREACH(stcb, head, sctp_tcbhash) { |
michael@0 | 1642 | if (stcb->rport != rport) { |
michael@0 | 1643 | /* remote port does not match */ |
michael@0 | 1644 | continue; |
michael@0 | 1645 | } |
michael@0 | 1646 | SCTP_TCB_LOCK(stcb); |
michael@0 | 1647 | if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { |
michael@0 | 1648 | SCTP_TCB_UNLOCK(stcb); |
michael@0 | 1649 | continue; |
michael@0 | 1650 | } |
michael@0 | 1651 | if (local && !sctp_does_stcb_own_this_addr(stcb, local)) { |
michael@0 | 1652 | SCTP_TCB_UNLOCK(stcb); |
michael@0 | 1653 | continue; |
michael@0 | 1654 | } |
michael@0 | 1655 | /* now look at the list of remote addresses */ |
michael@0 | 1656 | TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { |
michael@0 | 1657 | #ifdef INVARIANTS |
michael@0 | 1658 | if (net == (TAILQ_NEXT(net, sctp_next))) { |
michael@0 | 1659 | panic("Corrupt net list"); |
michael@0 | 1660 | } |
michael@0 | 1661 | #endif |
michael@0 | 1662 | if (net->ro._l_addr.sa.sa_family != |
michael@0 | 1663 | remote->sa_family) { |
michael@0 | 1664 | /* not the same family */ |
michael@0 | 1665 | continue; |
michael@0 | 1666 | } |
michael@0 | 1667 | switch (remote->sa_family) { |
michael@0 | 1668 | #ifdef INET |
michael@0 | 1669 | case AF_INET: |
michael@0 | 1670 | { |
michael@0 | 1671 | struct sockaddr_in *sin, *rsin; |
michael@0 | 1672 | |
michael@0 | 1673 | sin = (struct sockaddr_in *) |
michael@0 | 1674 | &net->ro._l_addr; |
michael@0 | 1675 | rsin = (struct sockaddr_in *)remote; |
michael@0 | 1676 | if (sin->sin_addr.s_addr == |
michael@0 | 1677 | rsin->sin_addr.s_addr) { |
michael@0 | 1678 | /* found it */ |
michael@0 | 1679 | if (netp != NULL) { |
michael@0 | 1680 | *netp = net; |
michael@0 | 1681 | } |
michael@0 | 1682 | if (locked_tcb == NULL) { |
michael@0 | 1683 | SCTP_INP_DECR_REF(inp); |
michael@0 | 1684 | } else if (locked_tcb != stcb) { |
michael@0 | 1685 | SCTP_TCB_LOCK(locked_tcb); |
michael@0 | 1686 | } |
michael@0 | 1687 | if (locked_tcb) { |
michael@0 | 1688 | atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); |
michael@0 | 1689 | } |
michael@0 | 1690 | SCTP_INP_WUNLOCK(inp); |
michael@0 | 1691 | SCTP_INP_INFO_RUNLOCK(); |
michael@0 | 1692 | return (stcb); |
michael@0 | 1693 | } |
michael@0 | 1694 | break; |
michael@0 | 1695 | } |
michael@0 | 1696 | #endif |
michael@0 | 1697 | #ifdef INET6 |
michael@0 | 1698 | case AF_INET6: |
michael@0 | 1699 | { |
michael@0 | 1700 | struct sockaddr_in6 *sin6, *rsin6; |
michael@0 | 1701 | |
michael@0 | 1702 | sin6 = (struct sockaddr_in6 *) |
michael@0 | 1703 | &net->ro._l_addr; |
michael@0 | 1704 | rsin6 = (struct sockaddr_in6 *)remote; |
michael@0 | 1705 | if (SCTP6_ARE_ADDR_EQUAL(sin6, |
michael@0 | 1706 | rsin6)) { |
michael@0 | 1707 | /* found it */ |
michael@0 | 1708 | if (netp != NULL) { |
michael@0 | 1709 | *netp = net; |
michael@0 | 1710 | } |
michael@0 | 1711 | if (locked_tcb == NULL) { |
michael@0 | 1712 | SCTP_INP_DECR_REF(inp); |
michael@0 | 1713 | } else if (locked_tcb != stcb) { |
michael@0 | 1714 | SCTP_TCB_LOCK(locked_tcb); |
michael@0 | 1715 | } |
michael@0 | 1716 | if (locked_tcb) { |
michael@0 | 1717 | atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); |
michael@0 | 1718 | } |
michael@0 | 1719 | SCTP_INP_WUNLOCK(inp); |
michael@0 | 1720 | SCTP_INP_INFO_RUNLOCK(); |
michael@0 | 1721 | return (stcb); |
michael@0 | 1722 | } |
michael@0 | 1723 | break; |
michael@0 | 1724 | } |
michael@0 | 1725 | #endif |
michael@0 | 1726 | #if defined(__Userspace__) |
michael@0 | 1727 | case AF_CONN: |
michael@0 | 1728 | { |
michael@0 | 1729 | struct sockaddr_conn *sconn, *rsconn; |
michael@0 | 1730 | |
michael@0 | 1731 | sconn = (struct sockaddr_conn *)&net->ro._l_addr; |
michael@0 | 1732 | rsconn = (struct sockaddr_conn *)remote; |
michael@0 | 1733 | if (sconn->sconn_addr == rsconn->sconn_addr) { |
michael@0 | 1734 | /* found it */ |
michael@0 | 1735 | if (netp != NULL) { |
michael@0 | 1736 | *netp = net; |
michael@0 | 1737 | } |
michael@0 | 1738 | if (locked_tcb == NULL) { |
michael@0 | 1739 | SCTP_INP_DECR_REF(inp); |
michael@0 | 1740 | } else if (locked_tcb != stcb) { |
michael@0 | 1741 | SCTP_TCB_LOCK(locked_tcb); |
michael@0 | 1742 | } |
michael@0 | 1743 | if (locked_tcb) { |
michael@0 | 1744 | atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); |
michael@0 | 1745 | } |
michael@0 | 1746 | SCTP_INP_WUNLOCK(inp); |
michael@0 | 1747 | SCTP_INP_INFO_RUNLOCK(); |
michael@0 | 1748 | return (stcb); |
michael@0 | 1749 | } |
michael@0 | 1750 | break; |
michael@0 | 1751 | } |
michael@0 | 1752 | #endif |
michael@0 | 1753 | default: |
michael@0 | 1754 | /* TSNH */ |
michael@0 | 1755 | break; |
michael@0 | 1756 | } |
michael@0 | 1757 | } |
michael@0 | 1758 | SCTP_TCB_UNLOCK(stcb); |
michael@0 | 1759 | } |
michael@0 | 1760 | } |
michael@0 | 1761 | null_return: |
michael@0 | 1762 | /* clean up for returning null */ |
michael@0 | 1763 | if (locked_tcb) { |
michael@0 | 1764 | SCTP_TCB_LOCK(locked_tcb); |
michael@0 | 1765 | atomic_subtract_int(&locked_tcb->asoc.refcnt, 1); |
michael@0 | 1766 | } |
michael@0 | 1767 | SCTP_INP_WUNLOCK(inp); |
michael@0 | 1768 | SCTP_INP_INFO_RUNLOCK(); |
michael@0 | 1769 | /* not found */ |
michael@0 | 1770 | return (NULL); |
michael@0 | 1771 | } |
michael@0 | 1772 | |
michael@0 | 1773 | |
michael@0 | 1774 | /* |
michael@0 | 1775 | * Find an association for a specific endpoint using the association id given |
michael@0 | 1776 | * out in the COMM_UP notification |
michael@0 | 1777 | */ |
michael@0 | 1778 | struct sctp_tcb * |
michael@0 | 1779 | sctp_findasoc_ep_asocid_locked(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock) |
michael@0 | 1780 | { |
michael@0 | 1781 | /* |
michael@0 | 1782 | * Use my the assoc_id to find a endpoint |
michael@0 | 1783 | */ |
michael@0 | 1784 | struct sctpasochead *head; |
michael@0 | 1785 | struct sctp_tcb *stcb; |
michael@0 | 1786 | uint32_t id; |
michael@0 | 1787 | |
michael@0 | 1788 | if (inp == NULL) { |
michael@0 | 1789 | SCTP_PRINTF("TSNH ep_associd\n"); |
michael@0 | 1790 | return (NULL); |
michael@0 | 1791 | } |
michael@0 | 1792 | if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
michael@0 | 1793 | SCTP_PRINTF("TSNH ep_associd0\n"); |
michael@0 | 1794 | return (NULL); |
michael@0 | 1795 | } |
michael@0 | 1796 | id = (uint32_t)asoc_id; |
michael@0 | 1797 | head = &inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(id, inp->hashasocidmark)]; |
michael@0 | 1798 | if (head == NULL) { |
michael@0 | 1799 | /* invalid id TSNH */ |
michael@0 | 1800 | SCTP_PRINTF("TSNH ep_associd1\n"); |
michael@0 | 1801 | return (NULL); |
michael@0 | 1802 | } |
michael@0 | 1803 | LIST_FOREACH(stcb, head, sctp_tcbasocidhash) { |
michael@0 | 1804 | if (stcb->asoc.assoc_id == id) { |
michael@0 | 1805 | if (inp != stcb->sctp_ep) { |
michael@0 | 1806 | /* |
michael@0 | 1807 | * some other guy has the same id active (id |
michael@0 | 1808 | * collision ??). |
michael@0 | 1809 | */ |
michael@0 | 1810 | SCTP_PRINTF("TSNH ep_associd2\n"); |
michael@0 | 1811 | continue; |
michael@0 | 1812 | } |
michael@0 | 1813 | if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { |
michael@0 | 1814 | continue; |
michael@0 | 1815 | } |
michael@0 | 1816 | if (want_lock) { |
michael@0 | 1817 | SCTP_TCB_LOCK(stcb); |
michael@0 | 1818 | } |
michael@0 | 1819 | return (stcb); |
michael@0 | 1820 | } |
michael@0 | 1821 | } |
michael@0 | 1822 | return (NULL); |
michael@0 | 1823 | } |
michael@0 | 1824 | |
michael@0 | 1825 | |
michael@0 | 1826 | struct sctp_tcb * |
michael@0 | 1827 | sctp_findassociation_ep_asocid(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock) |
michael@0 | 1828 | { |
michael@0 | 1829 | struct sctp_tcb *stcb; |
michael@0 | 1830 | |
michael@0 | 1831 | SCTP_INP_RLOCK(inp); |
michael@0 | 1832 | stcb = sctp_findasoc_ep_asocid_locked(inp, asoc_id, want_lock); |
michael@0 | 1833 | SCTP_INP_RUNLOCK(inp); |
michael@0 | 1834 | return (stcb); |
michael@0 | 1835 | } |
michael@0 | 1836 | |
michael@0 | 1837 | |
michael@0 | 1838 | /* |
michael@0 | 1839 | * Endpoint probe expects that the INP_INFO is locked. |
michael@0 | 1840 | */ |
michael@0 | 1841 | static struct sctp_inpcb * |
michael@0 | 1842 | sctp_endpoint_probe(struct sockaddr *nam, struct sctppcbhead *head, |
michael@0 | 1843 | uint16_t lport, uint32_t vrf_id) |
michael@0 | 1844 | { |
michael@0 | 1845 | struct sctp_inpcb *inp; |
michael@0 | 1846 | struct sctp_laddr *laddr; |
michael@0 | 1847 | #ifdef INET |
michael@0 | 1848 | struct sockaddr_in *sin; |
michael@0 | 1849 | #endif |
michael@0 | 1850 | #ifdef INET6 |
michael@0 | 1851 | struct sockaddr_in6 *sin6; |
michael@0 | 1852 | struct sockaddr_in6 *intf_addr6; |
michael@0 | 1853 | #endif |
michael@0 | 1854 | #if defined(__Userspace__) |
michael@0 | 1855 | struct sockaddr_conn *sconn; |
michael@0 | 1856 | #endif |
michael@0 | 1857 | #ifdef SCTP_MVRF |
michael@0 | 1858 | int i; |
michael@0 | 1859 | #endif |
michael@0 | 1860 | int fnd; |
michael@0 | 1861 | |
michael@0 | 1862 | #ifdef INET |
michael@0 | 1863 | sin = NULL; |
michael@0 | 1864 | #endif |
michael@0 | 1865 | #ifdef INET6 |
michael@0 | 1866 | sin6 = NULL; |
michael@0 | 1867 | #endif |
michael@0 | 1868 | #if defined(__Userspace__) |
michael@0 | 1869 | sconn = NULL; |
michael@0 | 1870 | #endif |
michael@0 | 1871 | switch (nam->sa_family) { |
michael@0 | 1872 | #ifdef INET |
michael@0 | 1873 | case AF_INET: |
michael@0 | 1874 | sin = (struct sockaddr_in *)nam; |
michael@0 | 1875 | break; |
michael@0 | 1876 | #endif |
michael@0 | 1877 | #ifdef INET6 |
michael@0 | 1878 | case AF_INET6: |
michael@0 | 1879 | sin6 = (struct sockaddr_in6 *)nam; |
michael@0 | 1880 | break; |
michael@0 | 1881 | #endif |
michael@0 | 1882 | #if defined(__Userspace__) |
michael@0 | 1883 | case AF_CONN: |
michael@0 | 1884 | sconn = (struct sockaddr_conn *)nam; |
michael@0 | 1885 | break; |
michael@0 | 1886 | #endif |
michael@0 | 1887 | default: |
michael@0 | 1888 | /* unsupported family */ |
michael@0 | 1889 | return (NULL); |
michael@0 | 1890 | } |
michael@0 | 1891 | |
michael@0 | 1892 | if (head == NULL) |
michael@0 | 1893 | return (NULL); |
michael@0 | 1894 | |
michael@0 | 1895 | LIST_FOREACH(inp, head, sctp_hash) { |
michael@0 | 1896 | SCTP_INP_RLOCK(inp); |
michael@0 | 1897 | if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
michael@0 | 1898 | SCTP_INP_RUNLOCK(inp); |
michael@0 | 1899 | continue; |
michael@0 | 1900 | } |
michael@0 | 1901 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) && |
michael@0 | 1902 | (inp->sctp_lport == lport)) { |
michael@0 | 1903 | /* got it */ |
michael@0 | 1904 | #ifdef INET |
michael@0 | 1905 | if ((nam->sa_family == AF_INET) && |
michael@0 | 1906 | (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) && |
michael@0 | 1907 | SCTP_IPV6_V6ONLY(inp)) { |
michael@0 | 1908 | /* IPv4 on a IPv6 socket with ONLY IPv6 set */ |
michael@0 | 1909 | SCTP_INP_RUNLOCK(inp); |
michael@0 | 1910 | continue; |
michael@0 | 1911 | } |
michael@0 | 1912 | #endif |
michael@0 | 1913 | #ifdef INET6 |
michael@0 | 1914 | /* A V6 address and the endpoint is NOT bound V6 */ |
michael@0 | 1915 | if (nam->sa_family == AF_INET6 && |
michael@0 | 1916 | (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) { |
michael@0 | 1917 | SCTP_INP_RUNLOCK(inp); |
michael@0 | 1918 | continue; |
michael@0 | 1919 | } |
michael@0 | 1920 | #endif |
michael@0 | 1921 | /* does a VRF id match? */ |
michael@0 | 1922 | fnd = 0; |
michael@0 | 1923 | #ifdef SCTP_MVRF |
michael@0 | 1924 | for (i = 0; i < inp->num_vrfs; i++) { |
michael@0 | 1925 | if (inp->m_vrf_ids[i] == vrf_id) { |
michael@0 | 1926 | fnd = 1; |
michael@0 | 1927 | break; |
michael@0 | 1928 | } |
michael@0 | 1929 | } |
michael@0 | 1930 | #else |
michael@0 | 1931 | if (inp->def_vrf_id == vrf_id) |
michael@0 | 1932 | fnd = 1; |
michael@0 | 1933 | #endif |
michael@0 | 1934 | |
michael@0 | 1935 | SCTP_INP_RUNLOCK(inp); |
michael@0 | 1936 | if (!fnd) |
michael@0 | 1937 | continue; |
michael@0 | 1938 | return (inp); |
michael@0 | 1939 | } |
michael@0 | 1940 | SCTP_INP_RUNLOCK(inp); |
michael@0 | 1941 | } |
michael@0 | 1942 | switch (nam->sa_family) { |
michael@0 | 1943 | #ifdef INET |
michael@0 | 1944 | case AF_INET: |
michael@0 | 1945 | if (sin->sin_addr.s_addr == INADDR_ANY) { |
michael@0 | 1946 | /* Can't hunt for one that has no address specified */ |
michael@0 | 1947 | return (NULL); |
michael@0 | 1948 | } |
michael@0 | 1949 | break; |
michael@0 | 1950 | #endif |
michael@0 | 1951 | #ifdef INET6 |
michael@0 | 1952 | case AF_INET6: |
michael@0 | 1953 | if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { |
michael@0 | 1954 | /* Can't hunt for one that has no address specified */ |
michael@0 | 1955 | return (NULL); |
michael@0 | 1956 | } |
michael@0 | 1957 | break; |
michael@0 | 1958 | #endif |
michael@0 | 1959 | #if defined(__Userspace__) |
michael@0 | 1960 | case AF_CONN: |
michael@0 | 1961 | if (sconn->sconn_addr == NULL) { |
michael@0 | 1962 | return (NULL); |
michael@0 | 1963 | } |
michael@0 | 1964 | break; |
michael@0 | 1965 | #endif |
michael@0 | 1966 | default: |
michael@0 | 1967 | break; |
michael@0 | 1968 | } |
michael@0 | 1969 | /* |
michael@0 | 1970 | * ok, not bound to all so see if we can find a EP bound to this |
michael@0 | 1971 | * address. |
michael@0 | 1972 | */ |
michael@0 | 1973 | LIST_FOREACH(inp, head, sctp_hash) { |
michael@0 | 1974 | SCTP_INP_RLOCK(inp); |
michael@0 | 1975 | if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
michael@0 | 1976 | SCTP_INP_RUNLOCK(inp); |
michael@0 | 1977 | continue; |
michael@0 | 1978 | } |
michael@0 | 1979 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)) { |
michael@0 | 1980 | SCTP_INP_RUNLOCK(inp); |
michael@0 | 1981 | continue; |
michael@0 | 1982 | } |
michael@0 | 1983 | /* |
michael@0 | 1984 | * Ok this could be a likely candidate, look at all of its |
michael@0 | 1985 | * addresses |
michael@0 | 1986 | */ |
michael@0 | 1987 | if (inp->sctp_lport != lport) { |
michael@0 | 1988 | SCTP_INP_RUNLOCK(inp); |
michael@0 | 1989 | continue; |
michael@0 | 1990 | } |
michael@0 | 1991 | /* does a VRF id match? */ |
michael@0 | 1992 | fnd = 0; |
michael@0 | 1993 | #ifdef SCTP_MVRF |
michael@0 | 1994 | for (i = 0; i < inp->num_vrfs; i++) { |
michael@0 | 1995 | if (inp->m_vrf_ids[i] == vrf_id) { |
michael@0 | 1996 | fnd = 1; |
michael@0 | 1997 | break; |
michael@0 | 1998 | } |
michael@0 | 1999 | } |
michael@0 | 2000 | #else |
michael@0 | 2001 | if (inp->def_vrf_id == vrf_id) |
michael@0 | 2002 | fnd = 1; |
michael@0 | 2003 | |
michael@0 | 2004 | #endif |
michael@0 | 2005 | if (!fnd) { |
michael@0 | 2006 | SCTP_INP_RUNLOCK(inp); |
michael@0 | 2007 | continue; |
michael@0 | 2008 | } |
michael@0 | 2009 | LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { |
michael@0 | 2010 | if (laddr->ifa == NULL) { |
michael@0 | 2011 | SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n", |
michael@0 | 2012 | __FUNCTION__); |
michael@0 | 2013 | continue; |
michael@0 | 2014 | } |
michael@0 | 2015 | SCTPDBG(SCTP_DEBUG_PCB1, "Ok laddr->ifa:%p is possible, ", |
michael@0 | 2016 | (void *)laddr->ifa); |
michael@0 | 2017 | if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) { |
michael@0 | 2018 | SCTPDBG(SCTP_DEBUG_PCB1, "Huh IFA being deleted\n"); |
michael@0 | 2019 | continue; |
michael@0 | 2020 | } |
michael@0 | 2021 | if (laddr->ifa->address.sa.sa_family == nam->sa_family) { |
michael@0 | 2022 | /* possible, see if it matches */ |
michael@0 | 2023 | switch (nam->sa_family) { |
michael@0 | 2024 | #ifdef INET |
michael@0 | 2025 | case AF_INET: |
michael@0 | 2026 | #if defined(__APPLE__) |
michael@0 | 2027 | if (sin == NULL) { |
michael@0 | 2028 | /* TSNH */ |
michael@0 | 2029 | break; |
michael@0 | 2030 | } |
michael@0 | 2031 | #endif |
michael@0 | 2032 | if (sin->sin_addr.s_addr == |
michael@0 | 2033 | laddr->ifa->address.sin.sin_addr.s_addr) { |
michael@0 | 2034 | SCTP_INP_RUNLOCK(inp); |
michael@0 | 2035 | return (inp); |
michael@0 | 2036 | } |
michael@0 | 2037 | break; |
michael@0 | 2038 | #endif |
michael@0 | 2039 | #ifdef INET6 |
michael@0 | 2040 | case AF_INET6: |
michael@0 | 2041 | intf_addr6 = &laddr->ifa->address.sin6; |
michael@0 | 2042 | if (SCTP6_ARE_ADDR_EQUAL(sin6, |
michael@0 | 2043 | intf_addr6)) { |
michael@0 | 2044 | SCTP_INP_RUNLOCK(inp); |
michael@0 | 2045 | return (inp); |
michael@0 | 2046 | } |
michael@0 | 2047 | break; |
michael@0 | 2048 | #endif |
michael@0 | 2049 | #if defined(__Userspace__) |
michael@0 | 2050 | case AF_CONN: |
michael@0 | 2051 | if (sconn->sconn_addr == laddr->ifa->address.sconn.sconn_addr) { |
michael@0 | 2052 | SCTP_INP_RUNLOCK(inp); |
michael@0 | 2053 | return (inp); |
michael@0 | 2054 | } |
michael@0 | 2055 | break; |
michael@0 | 2056 | #endif |
michael@0 | 2057 | } |
michael@0 | 2058 | } |
michael@0 | 2059 | } |
michael@0 | 2060 | SCTP_INP_RUNLOCK(inp); |
michael@0 | 2061 | } |
michael@0 | 2062 | return (NULL); |
michael@0 | 2063 | } |
michael@0 | 2064 | |
michael@0 | 2065 | |
michael@0 | 2066 | static struct sctp_inpcb * |
michael@0 | 2067 | sctp_isport_inuse(struct sctp_inpcb *inp, uint16_t lport, uint32_t vrf_id) |
michael@0 | 2068 | { |
michael@0 | 2069 | struct sctppcbhead *head; |
michael@0 | 2070 | struct sctp_inpcb *t_inp; |
michael@0 | 2071 | #ifdef SCTP_MVRF |
michael@0 | 2072 | int i; |
michael@0 | 2073 | #endif |
michael@0 | 2074 | int fnd; |
michael@0 | 2075 | |
michael@0 | 2076 | head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport, |
michael@0 | 2077 | SCTP_BASE_INFO(hashmark))]; |
michael@0 | 2078 | LIST_FOREACH(t_inp, head, sctp_hash) { |
michael@0 | 2079 | if (t_inp->sctp_lport != lport) { |
michael@0 | 2080 | continue; |
michael@0 | 2081 | } |
michael@0 | 2082 | /* is it in the VRF in question */ |
michael@0 | 2083 | fnd = 0; |
michael@0 | 2084 | #ifdef SCTP_MVRF |
michael@0 | 2085 | for (i = 0; i < inp->num_vrfs; i++) { |
michael@0 | 2086 | if (t_inp->m_vrf_ids[i] == vrf_id) { |
michael@0 | 2087 | fnd = 1; |
michael@0 | 2088 | break; |
michael@0 | 2089 | } |
michael@0 | 2090 | } |
michael@0 | 2091 | #else |
michael@0 | 2092 | if (t_inp->def_vrf_id == vrf_id) |
michael@0 | 2093 | fnd = 1; |
michael@0 | 2094 | #endif |
michael@0 | 2095 | if (!fnd) |
michael@0 | 2096 | continue; |
michael@0 | 2097 | |
michael@0 | 2098 | /* This one is in use. */ |
michael@0 | 2099 | /* check the v6/v4 binding issue */ |
michael@0 | 2100 | if ((t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) && |
michael@0 | 2101 | SCTP_IPV6_V6ONLY(t_inp)) { |
michael@0 | 2102 | if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { |
michael@0 | 2103 | /* collision in V6 space */ |
michael@0 | 2104 | return (t_inp); |
michael@0 | 2105 | } else { |
michael@0 | 2106 | /* inp is BOUND_V4 no conflict */ |
michael@0 | 2107 | continue; |
michael@0 | 2108 | } |
michael@0 | 2109 | } else if (t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { |
michael@0 | 2110 | /* t_inp is bound v4 and v6, conflict always */ |
michael@0 | 2111 | return (t_inp); |
michael@0 | 2112 | } else { |
michael@0 | 2113 | /* t_inp is bound only V4 */ |
michael@0 | 2114 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) && |
michael@0 | 2115 | SCTP_IPV6_V6ONLY(inp)) { |
michael@0 | 2116 | /* no conflict */ |
michael@0 | 2117 | continue; |
michael@0 | 2118 | } |
michael@0 | 2119 | /* else fall through to conflict */ |
michael@0 | 2120 | } |
michael@0 | 2121 | return (t_inp); |
michael@0 | 2122 | } |
michael@0 | 2123 | return (NULL); |
michael@0 | 2124 | } |
michael@0 | 2125 | |
michael@0 | 2126 | |
michael@0 | 2127 | int |
michael@0 | 2128 | sctp_swap_inpcb_for_listen(struct sctp_inpcb *inp) |
michael@0 | 2129 | { |
michael@0 | 2130 | /* For 1-2-1 with port reuse */ |
michael@0 | 2131 | struct sctppcbhead *head; |
michael@0 | 2132 | struct sctp_inpcb *tinp; |
michael@0 | 2133 | |
michael@0 | 2134 | if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) { |
michael@0 | 2135 | /* only works with port reuse on */ |
michael@0 | 2136 | return (-1); |
michael@0 | 2137 | } |
michael@0 | 2138 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) { |
michael@0 | 2139 | return (0); |
michael@0 | 2140 | } |
michael@0 | 2141 | SCTP_INP_RUNLOCK(inp); |
michael@0 | 2142 | head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(inp->sctp_lport, |
michael@0 | 2143 | SCTP_BASE_INFO(hashmark))]; |
michael@0 | 2144 | /* Kick out all non-listeners to the TCP hash */ |
michael@0 | 2145 | LIST_FOREACH(tinp, head, sctp_hash) { |
michael@0 | 2146 | if (tinp->sctp_lport != inp->sctp_lport) { |
michael@0 | 2147 | continue; |
michael@0 | 2148 | } |
michael@0 | 2149 | if (tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
michael@0 | 2150 | continue; |
michael@0 | 2151 | } |
michael@0 | 2152 | if (tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { |
michael@0 | 2153 | continue; |
michael@0 | 2154 | } |
michael@0 | 2155 | if (tinp->sctp_socket->so_qlimit) { |
michael@0 | 2156 | continue; |
michael@0 | 2157 | } |
michael@0 | 2158 | SCTP_INP_WLOCK(tinp); |
michael@0 | 2159 | LIST_REMOVE(tinp, sctp_hash); |
michael@0 | 2160 | head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR(tinp->sctp_lport, SCTP_BASE_INFO(hashtcpmark))]; |
michael@0 | 2161 | tinp->sctp_flags |= SCTP_PCB_FLAGS_IN_TCPPOOL; |
michael@0 | 2162 | LIST_INSERT_HEAD(head, tinp, sctp_hash); |
michael@0 | 2163 | SCTP_INP_WUNLOCK(tinp); |
michael@0 | 2164 | } |
michael@0 | 2165 | SCTP_INP_WLOCK(inp); |
michael@0 | 2166 | /* Pull from where he was */ |
michael@0 | 2167 | LIST_REMOVE(inp, sctp_hash); |
michael@0 | 2168 | inp->sctp_flags &= ~SCTP_PCB_FLAGS_IN_TCPPOOL; |
michael@0 | 2169 | head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(inp->sctp_lport, SCTP_BASE_INFO(hashmark))]; |
michael@0 | 2170 | LIST_INSERT_HEAD(head, inp, sctp_hash); |
michael@0 | 2171 | SCTP_INP_WUNLOCK(inp); |
michael@0 | 2172 | SCTP_INP_RLOCK(inp); |
michael@0 | 2173 | return (0); |
michael@0 | 2174 | } |
michael@0 | 2175 | |
michael@0 | 2176 | |
michael@0 | 2177 | struct sctp_inpcb * |
michael@0 | 2178 | sctp_pcb_findep(struct sockaddr *nam, int find_tcp_pool, int have_lock, |
michael@0 | 2179 | uint32_t vrf_id) |
michael@0 | 2180 | { |
michael@0 | 2181 | /* |
michael@0 | 2182 | * First we check the hash table to see if someone has this port |
michael@0 | 2183 | * bound with just the port. |
michael@0 | 2184 | */ |
michael@0 | 2185 | struct sctp_inpcb *inp; |
michael@0 | 2186 | struct sctppcbhead *head; |
michael@0 | 2187 | int lport; |
michael@0 | 2188 | unsigned int i; |
michael@0 | 2189 | #ifdef INET |
michael@0 | 2190 | struct sockaddr_in *sin; |
michael@0 | 2191 | #endif |
michael@0 | 2192 | #ifdef INET6 |
michael@0 | 2193 | struct sockaddr_in6 *sin6; |
michael@0 | 2194 | #endif |
michael@0 | 2195 | #if defined(__Userspace__) |
michael@0 | 2196 | struct sockaddr_conn *sconn; |
michael@0 | 2197 | #endif |
michael@0 | 2198 | |
michael@0 | 2199 | switch (nam->sa_family) { |
michael@0 | 2200 | #ifdef INET |
michael@0 | 2201 | case AF_INET: |
michael@0 | 2202 | sin = (struct sockaddr_in *)nam; |
michael@0 | 2203 | lport = sin->sin_port; |
michael@0 | 2204 | break; |
michael@0 | 2205 | #endif |
michael@0 | 2206 | #ifdef INET6 |
michael@0 | 2207 | case AF_INET6: |
michael@0 | 2208 | sin6 = (struct sockaddr_in6 *)nam; |
michael@0 | 2209 | lport = sin6->sin6_port; |
michael@0 | 2210 | break; |
michael@0 | 2211 | #endif |
michael@0 | 2212 | #if defined(__Userspace__) |
michael@0 | 2213 | case AF_CONN: |
michael@0 | 2214 | sconn = (struct sockaddr_conn *)nam; |
michael@0 | 2215 | lport = sconn->sconn_port; |
michael@0 | 2216 | break; |
michael@0 | 2217 | #endif |
michael@0 | 2218 | default: |
michael@0 | 2219 | return (NULL); |
michael@0 | 2220 | } |
michael@0 | 2221 | /* |
michael@0 | 2222 | * I could cheat here and just cast to one of the types but we will |
michael@0 | 2223 | * do it right. It also provides the check against an Unsupported |
michael@0 | 2224 | * type too. |
michael@0 | 2225 | */ |
michael@0 | 2226 | /* Find the head of the ALLADDR chain */ |
michael@0 | 2227 | if (have_lock == 0) { |
michael@0 | 2228 | SCTP_INP_INFO_RLOCK(); |
michael@0 | 2229 | } |
michael@0 | 2230 | head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport, |
michael@0 | 2231 | SCTP_BASE_INFO(hashmark))]; |
michael@0 | 2232 | inp = sctp_endpoint_probe(nam, head, lport, vrf_id); |
michael@0 | 2233 | |
michael@0 | 2234 | /* |
michael@0 | 2235 | * If the TCP model exists it could be that the main listening |
michael@0 | 2236 | * endpoint is gone but there still exists a connected socket for this |
michael@0 | 2237 | * guy. If so we can return the first one that we find. This may NOT |
michael@0 | 2238 | * be the correct one so the caller should be wary on the returned INP. |
michael@0 | 2239 | * Currently the only caller that sets find_tcp_pool is in bindx where |
michael@0 | 2240 | * we are verifying that a user CAN bind the address. He either |
michael@0 | 2241 | * has bound it already, or someone else has, or its open to bind, |
michael@0 | 2242 | * so this is good enough. |
michael@0 | 2243 | */ |
michael@0 | 2244 | if (inp == NULL && find_tcp_pool) { |
michael@0 | 2245 | for (i = 0; i < SCTP_BASE_INFO(hashtcpmark) + 1; i++) { |
michael@0 | 2246 | head = &SCTP_BASE_INFO(sctp_tcpephash)[i]; |
michael@0 | 2247 | inp = sctp_endpoint_probe(nam, head, lport, vrf_id); |
michael@0 | 2248 | if (inp) { |
michael@0 | 2249 | break; |
michael@0 | 2250 | } |
michael@0 | 2251 | } |
michael@0 | 2252 | } |
michael@0 | 2253 | if (inp) { |
michael@0 | 2254 | SCTP_INP_INCR_REF(inp); |
michael@0 | 2255 | } |
michael@0 | 2256 | if (have_lock == 0) { |
michael@0 | 2257 | SCTP_INP_INFO_RUNLOCK(); |
michael@0 | 2258 | } |
michael@0 | 2259 | return (inp); |
michael@0 | 2260 | } |
michael@0 | 2261 | |
michael@0 | 2262 | |
michael@0 | 2263 | /* |
michael@0 | 2264 | * Find an association for an endpoint with the pointer to whom you want to |
michael@0 | 2265 | * send to and the endpoint pointer. The address can be IPv4 or IPv6. We may |
michael@0 | 2266 | * need to change the *to to some other struct like a mbuf... |
michael@0 | 2267 | */ |
michael@0 | 2268 | struct sctp_tcb * |
michael@0 | 2269 | sctp_findassociation_addr_sa(struct sockaddr *from, struct sockaddr *to, |
michael@0 | 2270 | struct sctp_inpcb **inp_p, struct sctp_nets **netp, int find_tcp_pool, |
michael@0 | 2271 | uint32_t vrf_id) |
michael@0 | 2272 | { |
michael@0 | 2273 | struct sctp_inpcb *inp = NULL; |
michael@0 | 2274 | struct sctp_tcb *stcb; |
michael@0 | 2275 | |
michael@0 | 2276 | SCTP_INP_INFO_RLOCK(); |
michael@0 | 2277 | if (find_tcp_pool) { |
michael@0 | 2278 | if (inp_p != NULL) { |
michael@0 | 2279 | stcb = sctp_tcb_special_locate(inp_p, from, to, netp, |
michael@0 | 2280 | vrf_id); |
michael@0 | 2281 | } else { |
michael@0 | 2282 | stcb = sctp_tcb_special_locate(&inp, from, to, netp, |
michael@0 | 2283 | vrf_id); |
michael@0 | 2284 | } |
michael@0 | 2285 | if (stcb != NULL) { |
michael@0 | 2286 | SCTP_INP_INFO_RUNLOCK(); |
michael@0 | 2287 | return (stcb); |
michael@0 | 2288 | } |
michael@0 | 2289 | } |
michael@0 | 2290 | inp = sctp_pcb_findep(to, 0, 1, vrf_id); |
michael@0 | 2291 | if (inp_p != NULL) { |
michael@0 | 2292 | *inp_p = inp; |
michael@0 | 2293 | } |
michael@0 | 2294 | SCTP_INP_INFO_RUNLOCK(); |
michael@0 | 2295 | if (inp == NULL) { |
michael@0 | 2296 | return (NULL); |
michael@0 | 2297 | } |
michael@0 | 2298 | /* |
michael@0 | 2299 | * ok, we have an endpoint, now lets find the assoc for it (if any) |
michael@0 | 2300 | * we now place the source address or from in the to of the find |
michael@0 | 2301 | * endpoint call. Since in reality this chain is used from the |
michael@0 | 2302 | * inbound packet side. |
michael@0 | 2303 | */ |
michael@0 | 2304 | if (inp_p != NULL) { |
michael@0 | 2305 | stcb = sctp_findassociation_ep_addr(inp_p, from, netp, to, |
michael@0 | 2306 | NULL); |
michael@0 | 2307 | } else { |
michael@0 | 2308 | stcb = sctp_findassociation_ep_addr(&inp, from, netp, to, |
michael@0 | 2309 | NULL); |
michael@0 | 2310 | } |
michael@0 | 2311 | return (stcb); |
michael@0 | 2312 | } |
michael@0 | 2313 | |
michael@0 | 2314 | |
michael@0 | 2315 | /* |
michael@0 | 2316 | * This routine will grub through the mbuf that is a INIT or INIT-ACK and |
michael@0 | 2317 | * find all addresses that the sender has specified in any address list. Each |
michael@0 | 2318 | * address will be used to lookup the TCB and see if one exits. |
michael@0 | 2319 | */ |
michael@0 | 2320 | static struct sctp_tcb * |
michael@0 | 2321 | sctp_findassociation_special_addr(struct mbuf *m, int offset, |
michael@0 | 2322 | struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp, |
michael@0 | 2323 | struct sockaddr *dst) |
michael@0 | 2324 | { |
michael@0 | 2325 | struct sctp_paramhdr *phdr, parm_buf; |
michael@0 | 2326 | #if defined(INET) || defined(INET6) |
michael@0 | 2327 | struct sctp_tcb *stcb; |
michael@0 | 2328 | uint16_t ptype; |
michael@0 | 2329 | #endif |
michael@0 | 2330 | uint16_t plen; |
michael@0 | 2331 | #ifdef INET |
michael@0 | 2332 | struct sockaddr_in sin4; |
michael@0 | 2333 | #endif |
michael@0 | 2334 | #ifdef INET6 |
michael@0 | 2335 | struct sockaddr_in6 sin6; |
michael@0 | 2336 | #endif |
michael@0 | 2337 | |
michael@0 | 2338 | #ifdef INET |
michael@0 | 2339 | memset(&sin4, 0, sizeof(sin4)); |
michael@0 | 2340 | #ifdef HAVE_SIN_LEN |
michael@0 | 2341 | sin4.sin_len = sizeof(sin4); |
michael@0 | 2342 | #endif |
michael@0 | 2343 | sin4.sin_family = AF_INET; |
michael@0 | 2344 | sin4.sin_port = sh->src_port; |
michael@0 | 2345 | #endif |
michael@0 | 2346 | #ifdef INET6 |
michael@0 | 2347 | memset(&sin6, 0, sizeof(sin6)); |
michael@0 | 2348 | #ifdef HAVE_SIN6_LEN |
michael@0 | 2349 | sin6.sin6_len = sizeof(sin6); |
michael@0 | 2350 | #endif |
michael@0 | 2351 | sin6.sin6_family = AF_INET6; |
michael@0 | 2352 | sin6.sin6_port = sh->src_port; |
michael@0 | 2353 | #endif |
michael@0 | 2354 | |
michael@0 | 2355 | offset += sizeof(struct sctp_init_chunk); |
michael@0 | 2356 | |
michael@0 | 2357 | phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf)); |
michael@0 | 2358 | while (phdr != NULL) { |
michael@0 | 2359 | /* now we must see if we want the parameter */ |
michael@0 | 2360 | #if defined(INET) || defined(INET6) |
michael@0 | 2361 | ptype = ntohs(phdr->param_type); |
michael@0 | 2362 | #endif |
michael@0 | 2363 | plen = ntohs(phdr->param_length); |
michael@0 | 2364 | if (plen == 0) { |
michael@0 | 2365 | break; |
michael@0 | 2366 | } |
michael@0 | 2367 | #ifdef INET |
michael@0 | 2368 | if (ptype == SCTP_IPV4_ADDRESS && |
michael@0 | 2369 | plen == sizeof(struct sctp_ipv4addr_param)) { |
michael@0 | 2370 | /* Get the rest of the address */ |
michael@0 | 2371 | struct sctp_ipv4addr_param ip4_parm, *p4; |
michael@0 | 2372 | |
michael@0 | 2373 | phdr = sctp_get_next_param(m, offset, |
michael@0 | 2374 | (struct sctp_paramhdr *)&ip4_parm, min(plen, sizeof(ip4_parm))); |
michael@0 | 2375 | if (phdr == NULL) { |
michael@0 | 2376 | return (NULL); |
michael@0 | 2377 | } |
michael@0 | 2378 | p4 = (struct sctp_ipv4addr_param *)phdr; |
michael@0 | 2379 | memcpy(&sin4.sin_addr, &p4->addr, sizeof(p4->addr)); |
michael@0 | 2380 | /* look it up */ |
michael@0 | 2381 | stcb = sctp_findassociation_ep_addr(inp_p, |
michael@0 | 2382 | (struct sockaddr *)&sin4, netp, dst, NULL); |
michael@0 | 2383 | if (stcb != NULL) { |
michael@0 | 2384 | return (stcb); |
michael@0 | 2385 | } |
michael@0 | 2386 | } |
michael@0 | 2387 | #endif |
michael@0 | 2388 | #ifdef INET6 |
michael@0 | 2389 | if (ptype == SCTP_IPV6_ADDRESS && |
michael@0 | 2390 | plen == sizeof(struct sctp_ipv6addr_param)) { |
michael@0 | 2391 | /* Get the rest of the address */ |
michael@0 | 2392 | struct sctp_ipv6addr_param ip6_parm, *p6; |
michael@0 | 2393 | |
michael@0 | 2394 | phdr = sctp_get_next_param(m, offset, |
michael@0 | 2395 | (struct sctp_paramhdr *)&ip6_parm, min(plen,sizeof(ip6_parm))); |
michael@0 | 2396 | if (phdr == NULL) { |
michael@0 | 2397 | return (NULL); |
michael@0 | 2398 | } |
michael@0 | 2399 | p6 = (struct sctp_ipv6addr_param *)phdr; |
michael@0 | 2400 | memcpy(&sin6.sin6_addr, &p6->addr, sizeof(p6->addr)); |
michael@0 | 2401 | /* look it up */ |
michael@0 | 2402 | stcb = sctp_findassociation_ep_addr(inp_p, |
michael@0 | 2403 | (struct sockaddr *)&sin6, netp, dst, NULL); |
michael@0 | 2404 | if (stcb != NULL) { |
michael@0 | 2405 | return (stcb); |
michael@0 | 2406 | } |
michael@0 | 2407 | } |
michael@0 | 2408 | #endif |
michael@0 | 2409 | offset += SCTP_SIZE32(plen); |
michael@0 | 2410 | phdr = sctp_get_next_param(m, offset, &parm_buf, |
michael@0 | 2411 | sizeof(parm_buf)); |
michael@0 | 2412 | } |
michael@0 | 2413 | return (NULL); |
michael@0 | 2414 | } |
michael@0 | 2415 | |
michael@0 | 2416 | static struct sctp_tcb * |
michael@0 | 2417 | sctp_findassoc_by_vtag(struct sockaddr *from, struct sockaddr *to, uint32_t vtag, |
michael@0 | 2418 | struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint16_t rport, |
michael@0 | 2419 | uint16_t lport, int skip_src_check, uint32_t vrf_id, uint32_t remote_tag) |
michael@0 | 2420 | { |
michael@0 | 2421 | /* |
michael@0 | 2422 | * Use my vtag to hash. If we find it we then verify the source addr |
michael@0 | 2423 | * is in the assoc. If all goes well we save a bit on rec of a |
michael@0 | 2424 | * packet. |
michael@0 | 2425 | */ |
michael@0 | 2426 | struct sctpasochead *head; |
michael@0 | 2427 | struct sctp_nets *net; |
michael@0 | 2428 | struct sctp_tcb *stcb; |
michael@0 | 2429 | #ifdef SCTP_MVRF |
michael@0 | 2430 | unsigned int i; |
michael@0 | 2431 | #endif |
michael@0 | 2432 | |
michael@0 | 2433 | SCTP_INP_INFO_RLOCK(); |
michael@0 | 2434 | head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(vtag, |
michael@0 | 2435 | SCTP_BASE_INFO(hashasocmark))]; |
michael@0 | 2436 | if (head == NULL) { |
michael@0 | 2437 | /* invalid vtag */ |
michael@0 | 2438 | SCTP_INP_INFO_RUNLOCK(); |
michael@0 | 2439 | return (NULL); |
michael@0 | 2440 | } |
michael@0 | 2441 | LIST_FOREACH(stcb, head, sctp_asocs) { |
michael@0 | 2442 | SCTP_INP_RLOCK(stcb->sctp_ep); |
michael@0 | 2443 | if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
michael@0 | 2444 | SCTP_INP_RUNLOCK(stcb->sctp_ep); |
michael@0 | 2445 | continue; |
michael@0 | 2446 | } |
michael@0 | 2447 | #ifdef SCTP_MVRF |
michael@0 | 2448 | for (i = 0; i < stcb->sctp_ep->num_vrfs; i++) { |
michael@0 | 2449 | if (stcb->sctp_ep->m_vrf_ids[i] == vrf_id) { |
michael@0 | 2450 | break; |
michael@0 | 2451 | } |
michael@0 | 2452 | } |
michael@0 | 2453 | if (i == stcb->sctp_ep->num_vrfs) { |
michael@0 | 2454 | SCTP_INP_RUNLOCK(inp); |
michael@0 | 2455 | continue; |
michael@0 | 2456 | } |
michael@0 | 2457 | #else |
michael@0 | 2458 | if (stcb->sctp_ep->def_vrf_id != vrf_id) { |
michael@0 | 2459 | SCTP_INP_RUNLOCK(stcb->sctp_ep); |
michael@0 | 2460 | continue; |
michael@0 | 2461 | } |
michael@0 | 2462 | #endif |
michael@0 | 2463 | SCTP_TCB_LOCK(stcb); |
michael@0 | 2464 | SCTP_INP_RUNLOCK(stcb->sctp_ep); |
michael@0 | 2465 | if (stcb->asoc.my_vtag == vtag) { |
michael@0 | 2466 | /* candidate */ |
michael@0 | 2467 | if (stcb->rport != rport) { |
michael@0 | 2468 | SCTP_TCB_UNLOCK(stcb); |
michael@0 | 2469 | continue; |
michael@0 | 2470 | } |
michael@0 | 2471 | if (stcb->sctp_ep->sctp_lport != lport) { |
michael@0 | 2472 | SCTP_TCB_UNLOCK(stcb); |
michael@0 | 2473 | continue; |
michael@0 | 2474 | } |
michael@0 | 2475 | if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { |
michael@0 | 2476 | SCTP_TCB_UNLOCK(stcb); |
michael@0 | 2477 | continue; |
michael@0 | 2478 | } |
michael@0 | 2479 | /* RRS:Need toaddr check here */ |
michael@0 | 2480 | if (sctp_does_stcb_own_this_addr(stcb, to) == 0) { |
michael@0 | 2481 | /* Endpoint does not own this address */ |
michael@0 | 2482 | SCTP_TCB_UNLOCK(stcb); |
michael@0 | 2483 | continue; |
michael@0 | 2484 | } |
michael@0 | 2485 | if (remote_tag) { |
michael@0 | 2486 | /* If we have both vtags that's all we match on */ |
michael@0 | 2487 | if (stcb->asoc.peer_vtag == remote_tag) { |
michael@0 | 2488 | /* If both tags match we consider it conclusive |
michael@0 | 2489 | * and check NO source/destination addresses |
michael@0 | 2490 | */ |
michael@0 | 2491 | goto conclusive; |
michael@0 | 2492 | } |
michael@0 | 2493 | } |
michael@0 | 2494 | if (skip_src_check) { |
michael@0 | 2495 | conclusive: |
michael@0 | 2496 | if (from) { |
michael@0 | 2497 | *netp = sctp_findnet(stcb, from); |
michael@0 | 2498 | } else { |
michael@0 | 2499 | *netp = NULL; /* unknown */ |
michael@0 | 2500 | } |
michael@0 | 2501 | if (inp_p) |
michael@0 | 2502 | *inp_p = stcb->sctp_ep; |
michael@0 | 2503 | SCTP_INP_INFO_RUNLOCK(); |
michael@0 | 2504 | return (stcb); |
michael@0 | 2505 | } |
michael@0 | 2506 | net = sctp_findnet(stcb, from); |
michael@0 | 2507 | if (net) { |
michael@0 | 2508 | /* yep its him. */ |
michael@0 | 2509 | *netp = net; |
michael@0 | 2510 | SCTP_STAT_INCR(sctps_vtagexpress); |
michael@0 | 2511 | *inp_p = stcb->sctp_ep; |
michael@0 | 2512 | SCTP_INP_INFO_RUNLOCK(); |
michael@0 | 2513 | return (stcb); |
michael@0 | 2514 | } else { |
michael@0 | 2515 | /* |
michael@0 | 2516 | * not him, this should only happen in rare |
michael@0 | 2517 | * cases so I peg it. |
michael@0 | 2518 | */ |
michael@0 | 2519 | SCTP_STAT_INCR(sctps_vtagbogus); |
michael@0 | 2520 | } |
michael@0 | 2521 | } |
michael@0 | 2522 | SCTP_TCB_UNLOCK(stcb); |
michael@0 | 2523 | } |
michael@0 | 2524 | SCTP_INP_INFO_RUNLOCK(); |
michael@0 | 2525 | return (NULL); |
michael@0 | 2526 | } |
michael@0 | 2527 | |
michael@0 | 2528 | |
michael@0 | 2529 | /* |
michael@0 | 2530 | * Find an association with the pointer to the inbound IP packet. This can be |
michael@0 | 2531 | * a IPv4 or IPv6 packet. |
michael@0 | 2532 | */ |
michael@0 | 2533 | struct sctp_tcb * |
michael@0 | 2534 | sctp_findassociation_addr(struct mbuf *m, int offset, |
michael@0 | 2535 | struct sockaddr *src, struct sockaddr *dst, |
michael@0 | 2536 | struct sctphdr *sh, struct sctp_chunkhdr *ch, |
michael@0 | 2537 | struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id) |
michael@0 | 2538 | { |
michael@0 | 2539 | int find_tcp_pool; |
michael@0 | 2540 | struct sctp_tcb *stcb; |
michael@0 | 2541 | struct sctp_inpcb *inp; |
michael@0 | 2542 | |
michael@0 | 2543 | if (sh->v_tag) { |
michael@0 | 2544 | /* we only go down this path if vtag is non-zero */ |
michael@0 | 2545 | stcb = sctp_findassoc_by_vtag(src, dst, ntohl(sh->v_tag), |
michael@0 | 2546 | inp_p, netp, sh->src_port, sh->dest_port, 0, vrf_id, 0); |
michael@0 | 2547 | if (stcb) { |
michael@0 | 2548 | return (stcb); |
michael@0 | 2549 | } |
michael@0 | 2550 | } |
michael@0 | 2551 | |
michael@0 | 2552 | find_tcp_pool = 0; |
michael@0 | 2553 | if ((ch->chunk_type != SCTP_INITIATION) && |
michael@0 | 2554 | (ch->chunk_type != SCTP_INITIATION_ACK) && |
michael@0 | 2555 | (ch->chunk_type != SCTP_COOKIE_ACK) && |
michael@0 | 2556 | (ch->chunk_type != SCTP_COOKIE_ECHO)) { |
michael@0 | 2557 | /* Other chunk types go to the tcp pool. */ |
michael@0 | 2558 | find_tcp_pool = 1; |
michael@0 | 2559 | } |
michael@0 | 2560 | if (inp_p) { |
michael@0 | 2561 | stcb = sctp_findassociation_addr_sa(src, dst, inp_p, netp, |
michael@0 | 2562 | find_tcp_pool, vrf_id); |
michael@0 | 2563 | inp = *inp_p; |
michael@0 | 2564 | } else { |
michael@0 | 2565 | stcb = sctp_findassociation_addr_sa(src, dst, &inp, netp, |
michael@0 | 2566 | find_tcp_pool, vrf_id); |
michael@0 | 2567 | } |
michael@0 | 2568 | SCTPDBG(SCTP_DEBUG_PCB1, "stcb:%p inp:%p\n", (void *)stcb, (void *)inp); |
michael@0 | 2569 | if (stcb == NULL && inp) { |
michael@0 | 2570 | /* Found a EP but not this address */ |
michael@0 | 2571 | if ((ch->chunk_type == SCTP_INITIATION) || |
michael@0 | 2572 | (ch->chunk_type == SCTP_INITIATION_ACK)) { |
michael@0 | 2573 | /*- |
michael@0 | 2574 | * special hook, we do NOT return linp or an |
michael@0 | 2575 | * association that is linked to an existing |
michael@0 | 2576 | * association that is under the TCP pool (i.e. no |
michael@0 | 2577 | * listener exists). The endpoint finding routine |
michael@0 | 2578 | * will always find a listener before examining the |
michael@0 | 2579 | * TCP pool. |
michael@0 | 2580 | */ |
michael@0 | 2581 | if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) { |
michael@0 | 2582 | if (inp_p) { |
michael@0 | 2583 | *inp_p = NULL; |
michael@0 | 2584 | } |
michael@0 | 2585 | return (NULL); |
michael@0 | 2586 | } |
michael@0 | 2587 | stcb = sctp_findassociation_special_addr(m, |
michael@0 | 2588 | offset, sh, &inp, netp, dst); |
michael@0 | 2589 | if (inp_p != NULL) { |
michael@0 | 2590 | *inp_p = inp; |
michael@0 | 2591 | } |
michael@0 | 2592 | } |
michael@0 | 2593 | } |
michael@0 | 2594 | SCTPDBG(SCTP_DEBUG_PCB1, "stcb is %p\n", (void *)stcb); |
michael@0 | 2595 | return (stcb); |
michael@0 | 2596 | } |
michael@0 | 2597 | |
michael@0 | 2598 | /* |
michael@0 | 2599 | * lookup an association by an ASCONF lookup address. |
michael@0 | 2600 | * if the lookup address is 0.0.0.0 or ::0, use the vtag to do the lookup |
michael@0 | 2601 | */ |
michael@0 | 2602 | struct sctp_tcb * |
michael@0 | 2603 | sctp_findassociation_ep_asconf(struct mbuf *m, int offset, |
michael@0 | 2604 | struct sockaddr *dst, struct sctphdr *sh, |
michael@0 | 2605 | struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id) |
michael@0 | 2606 | { |
michael@0 | 2607 | struct sctp_tcb *stcb; |
michael@0 | 2608 | struct sockaddr_storage remote_store; |
michael@0 | 2609 | struct sctp_paramhdr parm_buf, *phdr; |
michael@0 | 2610 | int ptype; |
michael@0 | 2611 | int zero_address = 0; |
michael@0 | 2612 | #ifdef INET |
michael@0 | 2613 | struct sockaddr_in *sin; |
michael@0 | 2614 | #endif |
michael@0 | 2615 | #ifdef INET6 |
michael@0 | 2616 | struct sockaddr_in6 *sin6; |
michael@0 | 2617 | #endif |
michael@0 | 2618 | |
michael@0 | 2619 | memset(&remote_store, 0, sizeof(remote_store)); |
michael@0 | 2620 | phdr = sctp_get_next_param(m, offset + sizeof(struct sctp_asconf_chunk), |
michael@0 | 2621 | &parm_buf, sizeof(struct sctp_paramhdr)); |
michael@0 | 2622 | if (phdr == NULL) { |
michael@0 | 2623 | SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf lookup addr\n", |
michael@0 | 2624 | __FUNCTION__); |
michael@0 | 2625 | return NULL; |
michael@0 | 2626 | } |
michael@0 | 2627 | ptype = (int)((uint32_t) ntohs(phdr->param_type)); |
michael@0 | 2628 | /* get the correlation address */ |
michael@0 | 2629 | switch (ptype) { |
michael@0 | 2630 | #ifdef INET6 |
michael@0 | 2631 | case SCTP_IPV6_ADDRESS: |
michael@0 | 2632 | { |
michael@0 | 2633 | /* ipv6 address param */ |
michael@0 | 2634 | struct sctp_ipv6addr_param *p6, p6_buf; |
michael@0 | 2635 | |
michael@0 | 2636 | if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv6addr_param)) { |
michael@0 | 2637 | return NULL; |
michael@0 | 2638 | } |
michael@0 | 2639 | p6 = (struct sctp_ipv6addr_param *)sctp_get_next_param(m, |
michael@0 | 2640 | offset + sizeof(struct sctp_asconf_chunk), |
michael@0 | 2641 | &p6_buf.ph, sizeof(*p6)); |
michael@0 | 2642 | if (p6 == NULL) { |
michael@0 | 2643 | SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v6 lookup addr\n", |
michael@0 | 2644 | __FUNCTION__); |
michael@0 | 2645 | return (NULL); |
michael@0 | 2646 | } |
michael@0 | 2647 | sin6 = (struct sockaddr_in6 *)&remote_store; |
michael@0 | 2648 | sin6->sin6_family = AF_INET6; |
michael@0 | 2649 | #ifdef HAVE_SIN6_LEN |
michael@0 | 2650 | sin6->sin6_len = sizeof(*sin6); |
michael@0 | 2651 | #endif |
michael@0 | 2652 | sin6->sin6_port = sh->src_port; |
michael@0 | 2653 | memcpy(&sin6->sin6_addr, &p6->addr, sizeof(struct in6_addr)); |
michael@0 | 2654 | if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) |
michael@0 | 2655 | zero_address = 1; |
michael@0 | 2656 | break; |
michael@0 | 2657 | } |
michael@0 | 2658 | #endif |
michael@0 | 2659 | #ifdef INET |
michael@0 | 2660 | case SCTP_IPV4_ADDRESS: |
michael@0 | 2661 | { |
michael@0 | 2662 | /* ipv4 address param */ |
michael@0 | 2663 | struct sctp_ipv4addr_param *p4, p4_buf; |
michael@0 | 2664 | |
michael@0 | 2665 | if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv4addr_param)) { |
michael@0 | 2666 | return NULL; |
michael@0 | 2667 | } |
michael@0 | 2668 | p4 = (struct sctp_ipv4addr_param *)sctp_get_next_param(m, |
michael@0 | 2669 | offset + sizeof(struct sctp_asconf_chunk), |
michael@0 | 2670 | &p4_buf.ph, sizeof(*p4)); |
michael@0 | 2671 | if (p4 == NULL) { |
michael@0 | 2672 | SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v4 lookup addr\n", |
michael@0 | 2673 | __FUNCTION__); |
michael@0 | 2674 | return (NULL); |
michael@0 | 2675 | } |
michael@0 | 2676 | sin = (struct sockaddr_in *)&remote_store; |
michael@0 | 2677 | sin->sin_family = AF_INET; |
michael@0 | 2678 | #ifdef HAVE_SIN_LEN |
michael@0 | 2679 | sin->sin_len = sizeof(*sin); |
michael@0 | 2680 | #endif |
michael@0 | 2681 | sin->sin_port = sh->src_port; |
michael@0 | 2682 | memcpy(&sin->sin_addr, &p4->addr, sizeof(struct in_addr)); |
michael@0 | 2683 | if (sin->sin_addr.s_addr == INADDR_ANY) |
michael@0 | 2684 | zero_address = 1; |
michael@0 | 2685 | break; |
michael@0 | 2686 | } |
michael@0 | 2687 | #endif |
michael@0 | 2688 | default: |
michael@0 | 2689 | /* invalid address param type */ |
michael@0 | 2690 | return NULL; |
michael@0 | 2691 | } |
michael@0 | 2692 | |
michael@0 | 2693 | if (zero_address) { |
michael@0 | 2694 | stcb = sctp_findassoc_by_vtag(NULL, dst, ntohl(sh->v_tag), inp_p, |
michael@0 | 2695 | netp, sh->src_port, sh->dest_port, 1, vrf_id, 0); |
michael@0 | 2696 | if (stcb != NULL) { |
michael@0 | 2697 | SCTP_INP_DECR_REF(*inp_p); |
michael@0 | 2698 | } |
michael@0 | 2699 | } else { |
michael@0 | 2700 | stcb = sctp_findassociation_ep_addr(inp_p, |
michael@0 | 2701 | (struct sockaddr *)&remote_store, netp, |
michael@0 | 2702 | dst, NULL); |
michael@0 | 2703 | } |
michael@0 | 2704 | return (stcb); |
michael@0 | 2705 | } |
michael@0 | 2706 | |
michael@0 | 2707 | |
michael@0 | 2708 | /* |
michael@0 | 2709 | * allocate a sctp_inpcb and setup a temporary binding to a port/all |
michael@0 | 2710 | * addresses. This way if we don't get a bind we by default pick a ephemeral |
michael@0 | 2711 | * port with all addresses bound. |
michael@0 | 2712 | */ |
michael@0 | 2713 | int |
michael@0 | 2714 | sctp_inpcb_alloc(struct socket *so, uint32_t vrf_id) |
michael@0 | 2715 | { |
michael@0 | 2716 | /* |
michael@0 | 2717 | * we get called when a new endpoint starts up. We need to allocate |
michael@0 | 2718 | * the sctp_inpcb structure from the zone and init it. Mark it as |
michael@0 | 2719 | * unbound and find a port that we can use as an ephemeral with |
michael@0 | 2720 | * INADDR_ANY. If the user binds later no problem we can then add in |
michael@0 | 2721 | * the specific addresses. And setup the default parameters for the |
michael@0 | 2722 | * EP. |
michael@0 | 2723 | */ |
michael@0 | 2724 | int i, error; |
michael@0 | 2725 | struct sctp_inpcb *inp; |
michael@0 | 2726 | struct sctp_pcb *m; |
michael@0 | 2727 | struct timeval time; |
michael@0 | 2728 | sctp_sharedkey_t *null_key; |
michael@0 | 2729 | |
michael@0 | 2730 | error = 0; |
michael@0 | 2731 | |
michael@0 | 2732 | SCTP_INP_INFO_WLOCK(); |
michael@0 | 2733 | inp = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_ep), struct sctp_inpcb); |
michael@0 | 2734 | if (inp == NULL) { |
michael@0 | 2735 | SCTP_PRINTF("Out of SCTP-INPCB structures - no resources\n"); |
michael@0 | 2736 | SCTP_INP_INFO_WUNLOCK(); |
michael@0 | 2737 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS); |
michael@0 | 2738 | return (ENOBUFS); |
michael@0 | 2739 | } |
michael@0 | 2740 | /* zap it */ |
michael@0 | 2741 | bzero(inp, sizeof(*inp)); |
michael@0 | 2742 | |
michael@0 | 2743 | /* bump generations */ |
michael@0 | 2744 | #if defined(__APPLE__) |
michael@0 | 2745 | inp->ip_inp.inp.inp_state = INPCB_STATE_INUSE; |
michael@0 | 2746 | #endif |
michael@0 | 2747 | /* setup socket pointers */ |
michael@0 | 2748 | inp->sctp_socket = so; |
michael@0 | 2749 | inp->ip_inp.inp.inp_socket = so; |
michael@0 | 2750 | #ifdef INET6 |
michael@0 | 2751 | #if !defined(__Userspace__) && !defined(__Windows__) |
michael@0 | 2752 | if (INP_SOCKAF(so) == AF_INET6) { |
michael@0 | 2753 | if (MODULE_GLOBAL(ip6_auto_flowlabel)) { |
michael@0 | 2754 | inp->ip_inp.inp.inp_flags |= IN6P_AUTOFLOWLABEL; |
michael@0 | 2755 | } |
michael@0 | 2756 | if (MODULE_GLOBAL(ip6_v6only)) { |
michael@0 | 2757 | inp->ip_inp.inp.inp_flags |= IN6P_IPV6_V6ONLY; |
michael@0 | 2758 | } |
michael@0 | 2759 | } |
michael@0 | 2760 | #endif |
michael@0 | 2761 | #endif |
michael@0 | 2762 | inp->sctp_associd_counter = 1; |
michael@0 | 2763 | inp->partial_delivery_point = SCTP_SB_LIMIT_RCV(so) >> SCTP_PARTIAL_DELIVERY_SHIFT; |
michael@0 | 2764 | inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT; |
michael@0 | 2765 | inp->sctp_cmt_on_off = SCTP_BASE_SYSCTL(sctp_cmt_on_off); |
michael@0 | 2766 | inp->sctp_ecn_enable = SCTP_BASE_SYSCTL(sctp_ecn_enable); |
michael@0 | 2767 | #if defined(__Userspace__) |
michael@0 | 2768 | inp->ulp_info = NULL; |
michael@0 | 2769 | inp->recv_callback = NULL; |
michael@0 | 2770 | inp->send_callback = NULL; |
michael@0 | 2771 | inp->send_sb_threshold = 0; |
michael@0 | 2772 | #endif |
michael@0 | 2773 | /* init the small hash table we use to track asocid <-> tcb */ |
michael@0 | 2774 | inp->sctp_asocidhash = SCTP_HASH_INIT(SCTP_STACK_VTAG_HASH_SIZE, &inp->hashasocidmark); |
michael@0 | 2775 | if (inp->sctp_asocidhash == NULL) { |
michael@0 | 2776 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp); |
michael@0 | 2777 | SCTP_INP_INFO_WUNLOCK(); |
michael@0 | 2778 | return (ENOBUFS); |
michael@0 | 2779 | } |
michael@0 | 2780 | #ifdef IPSEC |
michael@0 | 2781 | #if !(defined(__APPLE__)) |
michael@0 | 2782 | { |
michael@0 | 2783 | struct inpcbpolicy *pcb_sp = NULL; |
michael@0 | 2784 | |
michael@0 | 2785 | error = ipsec_init_policy(so, &pcb_sp); |
michael@0 | 2786 | /* Arrange to share the policy */ |
michael@0 | 2787 | inp->ip_inp.inp.inp_sp = pcb_sp; |
michael@0 | 2788 | ((struct in6pcb *)(&inp->ip_inp.inp))->in6p_sp = pcb_sp; |
michael@0 | 2789 | } |
michael@0 | 2790 | #else |
michael@0 | 2791 | /* not sure what to do for openbsd here */ |
michael@0 | 2792 | error = 0; |
michael@0 | 2793 | #endif |
michael@0 | 2794 | if (error != 0) { |
michael@0 | 2795 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp); |
michael@0 | 2796 | SCTP_INP_INFO_WUNLOCK(); |
michael@0 | 2797 | return error; |
michael@0 | 2798 | } |
michael@0 | 2799 | #endif /* IPSEC */ |
michael@0 | 2800 | SCTP_INCR_EP_COUNT(); |
michael@0 | 2801 | inp->ip_inp.inp.inp_ip_ttl = MODULE_GLOBAL(ip_defttl); |
michael@0 | 2802 | SCTP_INP_INFO_WUNLOCK(); |
michael@0 | 2803 | |
michael@0 | 2804 | so->so_pcb = (caddr_t)inp; |
michael@0 | 2805 | |
michael@0 | 2806 | #if defined(__FreeBSD__) && __FreeBSD_version < 803000 |
michael@0 | 2807 | if ((SCTP_SO_TYPE(so) == SOCK_DGRAM) || |
michael@0 | 2808 | (SCTP_SO_TYPE(so) == SOCK_SEQPACKET)) { |
michael@0 | 2809 | #else |
michael@0 | 2810 | if (SCTP_SO_TYPE(so) == SOCK_SEQPACKET) { |
michael@0 | 2811 | #endif |
michael@0 | 2812 | /* UDP style socket */ |
michael@0 | 2813 | inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE | |
michael@0 | 2814 | SCTP_PCB_FLAGS_UNBOUND); |
michael@0 | 2815 | /* Be sure it is NON-BLOCKING IO for UDP */ |
michael@0 | 2816 | /* SCTP_SET_SO_NBIO(so); */ |
michael@0 | 2817 | } else if (SCTP_SO_TYPE(so) == SOCK_STREAM) { |
michael@0 | 2818 | /* TCP style socket */ |
michael@0 | 2819 | inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE | |
michael@0 | 2820 | SCTP_PCB_FLAGS_UNBOUND); |
michael@0 | 2821 | /* Be sure we have blocking IO by default */ |
michael@0 | 2822 | SCTP_CLEAR_SO_NBIO(so); |
michael@0 | 2823 | #if defined(__Panda__) |
michael@0 | 2824 | } else if (SCTP_SO_TYPE(so) == SOCK_FASTSEQPACKET) { |
michael@0 | 2825 | inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE | |
michael@0 | 2826 | SCTP_PCB_FLAGS_UNBOUND); |
michael@0 | 2827 | sctp_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE); |
michael@0 | 2828 | } else if (SCTP_SO_TYPE(so) == SOCK_FASTSTREAM) { |
michael@0 | 2829 | inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE | |
michael@0 | 2830 | SCTP_PCB_FLAGS_UNBOUND); |
michael@0 | 2831 | sctp_feature_on(inp, SCTP_PCB_FLAGS_ZERO_COPY_ACTIVE); |
michael@0 | 2832 | #endif |
michael@0 | 2833 | } else { |
michael@0 | 2834 | /* |
michael@0 | 2835 | * unsupported socket type (RAW, etc)- in case we missed it |
michael@0 | 2836 | * in protosw |
michael@0 | 2837 | */ |
michael@0 | 2838 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EOPNOTSUPP); |
michael@0 | 2839 | so->so_pcb = NULL; |
michael@0 | 2840 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp); |
michael@0 | 2841 | return (EOPNOTSUPP); |
michael@0 | 2842 | } |
michael@0 | 2843 | if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_1) { |
michael@0 | 2844 | sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE); |
michael@0 | 2845 | sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS); |
michael@0 | 2846 | } else if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_2) { |
michael@0 | 2847 | sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE); |
michael@0 | 2848 | sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS); |
michael@0 | 2849 | } else if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_0) { |
michael@0 | 2850 | sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE); |
michael@0 | 2851 | sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS); |
michael@0 | 2852 | } |
michael@0 | 2853 | inp->sctp_tcbhash = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_pcbtblsize), |
michael@0 | 2854 | &inp->sctp_hashmark); |
michael@0 | 2855 | if (inp->sctp_tcbhash == NULL) { |
michael@0 | 2856 | SCTP_PRINTF("Out of SCTP-INPCB->hashinit - no resources\n"); |
michael@0 | 2857 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS); |
michael@0 | 2858 | so->so_pcb = NULL; |
michael@0 | 2859 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp); |
michael@0 | 2860 | return (ENOBUFS); |
michael@0 | 2861 | } |
michael@0 | 2862 | #ifdef SCTP_MVRF |
michael@0 | 2863 | inp->vrf_size = SCTP_DEFAULT_VRF_SIZE; |
michael@0 | 2864 | SCTP_MALLOC(inp->m_vrf_ids, uint32_t *, |
michael@0 | 2865 | (sizeof(uint32_t) * inp->vrf_size), SCTP_M_MVRF); |
michael@0 | 2866 | if (inp->m_vrf_ids == NULL) { |
michael@0 | 2867 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS); |
michael@0 | 2868 | so->so_pcb = NULL; |
michael@0 | 2869 | SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark); |
michael@0 | 2870 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp); |
michael@0 | 2871 | return (ENOBUFS); |
michael@0 | 2872 | } |
michael@0 | 2873 | inp->m_vrf_ids[0] = vrf_id; |
michael@0 | 2874 | inp->num_vrfs = 1; |
michael@0 | 2875 | #endif |
michael@0 | 2876 | inp->def_vrf_id = vrf_id; |
michael@0 | 2877 | |
michael@0 | 2878 | #if defined(__APPLE__) |
michael@0 | 2879 | #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) |
michael@0 | 2880 | inp->ip_inp.inp.inpcb_mtx = lck_mtx_alloc_init(SCTP_BASE_INFO(sctbinfo).mtx_grp, SCTP_BASE_INFO(sctbinfo).mtx_attr); |
michael@0 | 2881 | if (inp->ip_inp.inp.inpcb_mtx == NULL) { |
michael@0 | 2882 | SCTP_PRINTF("in_pcballoc: can't alloc mutex! so=%p\n", (void *)so); |
michael@0 | 2883 | #ifdef SCTP_MVRF |
michael@0 | 2884 | SCTP_FREE(inp->m_vrf_ids, SCTP_M_MVRF); |
michael@0 | 2885 | #endif |
michael@0 | 2886 | SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark); |
michael@0 | 2887 | so->so_pcb = NULL; |
michael@0 | 2888 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp); |
michael@0 | 2889 | SCTP_UNLOCK_EXC(SCTP_BASE_INFO(sctbinfo).ipi_lock); |
michael@0 | 2890 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM); |
michael@0 | 2891 | return (ENOMEM); |
michael@0 | 2892 | } |
michael@0 | 2893 | #elif defined(APPLE_LION) || defined(APPLE_MOUNTAINLION) |
michael@0 | 2894 | lck_mtx_init(&inp->ip_inp.inp.inpcb_mtx, SCTP_BASE_INFO(sctbinfo).mtx_grp, SCTP_BASE_INFO(sctbinfo).mtx_attr); |
michael@0 | 2895 | #else |
michael@0 | 2896 | lck_mtx_init(&inp->ip_inp.inp.inpcb_mtx, SCTP_BASE_INFO(sctbinfo).ipi_lock_grp, SCTP_BASE_INFO(sctbinfo).ipi_lock_attr); |
michael@0 | 2897 | #endif |
michael@0 | 2898 | #endif |
michael@0 | 2899 | SCTP_INP_INFO_WLOCK(); |
michael@0 | 2900 | SCTP_INP_LOCK_INIT(inp); |
michael@0 | 2901 | #if defined(__FreeBSD__) |
michael@0 | 2902 | INP_LOCK_INIT(&inp->ip_inp.inp, "inp", "sctpinp"); |
michael@0 | 2903 | #endif |
michael@0 | 2904 | SCTP_INP_READ_INIT(inp); |
michael@0 | 2905 | SCTP_ASOC_CREATE_LOCK_INIT(inp); |
michael@0 | 2906 | /* lock the new ep */ |
michael@0 | 2907 | SCTP_INP_WLOCK(inp); |
michael@0 | 2908 | |
michael@0 | 2909 | /* add it to the info area */ |
michael@0 | 2910 | LIST_INSERT_HEAD(&SCTP_BASE_INFO(listhead), inp, sctp_list); |
michael@0 | 2911 | #if defined(__APPLE__) |
michael@0 | 2912 | inp->ip_inp.inp.inp_pcbinfo = &SCTP_BASE_INFO(sctbinfo); |
michael@0 | 2913 | #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) || defined(APPLE_LION) || defined(APPLE_MOUNTAINLION) |
michael@0 | 2914 | LIST_INSERT_HEAD(SCTP_BASE_INFO(sctbinfo).listhead, &inp->ip_inp.inp, inp_list); |
michael@0 | 2915 | #else |
michael@0 | 2916 | LIST_INSERT_HEAD(SCTP_BASE_INFO(sctbinfo).ipi_listhead, &inp->ip_inp.inp, inp_list); |
michael@0 | 2917 | #endif |
michael@0 | 2918 | #endif |
michael@0 | 2919 | SCTP_INP_INFO_WUNLOCK(); |
michael@0 | 2920 | |
michael@0 | 2921 | TAILQ_INIT(&inp->read_queue); |
michael@0 | 2922 | LIST_INIT(&inp->sctp_addr_list); |
michael@0 | 2923 | |
michael@0 | 2924 | LIST_INIT(&inp->sctp_asoc_list); |
michael@0 | 2925 | |
michael@0 | 2926 | #ifdef SCTP_TRACK_FREED_ASOCS |
michael@0 | 2927 | /* TEMP CODE */ |
michael@0 | 2928 | LIST_INIT(&inp->sctp_asoc_free_list); |
michael@0 | 2929 | #endif |
michael@0 | 2930 | /* Init the timer structure for signature change */ |
michael@0 | 2931 | SCTP_OS_TIMER_INIT(&inp->sctp_ep.signature_change.timer); |
michael@0 | 2932 | inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NEWCOOKIE; |
michael@0 | 2933 | |
michael@0 | 2934 | /* now init the actual endpoint default data */ |
michael@0 | 2935 | m = &inp->sctp_ep; |
michael@0 | 2936 | |
michael@0 | 2937 | /* setup the base timeout information */ |
michael@0 | 2938 | m->sctp_timeoutticks[SCTP_TIMER_SEND] = SEC_TO_TICKS(SCTP_SEND_SEC); /* needed ? */ |
michael@0 | 2939 | m->sctp_timeoutticks[SCTP_TIMER_INIT] = SEC_TO_TICKS(SCTP_INIT_SEC); /* needed ? */ |
michael@0 | 2940 | m->sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_delayed_sack_time_default)); |
michael@0 | 2941 | m->sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_heartbeat_interval_default)); |
michael@0 | 2942 | m->sctp_timeoutticks[SCTP_TIMER_PMTU] = SEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_pmtu_raise_time_default)); |
michael@0 | 2943 | m->sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] = SEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_shutdown_guard_time_default)); |
michael@0 | 2944 | m->sctp_timeoutticks[SCTP_TIMER_SIGNATURE] = SEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_secret_lifetime_default)); |
michael@0 | 2945 | /* all max/min max are in ms */ |
michael@0 | 2946 | m->sctp_maxrto = SCTP_BASE_SYSCTL(sctp_rto_max_default); |
michael@0 | 2947 | m->sctp_minrto = SCTP_BASE_SYSCTL(sctp_rto_min_default); |
michael@0 | 2948 | m->initial_rto = SCTP_BASE_SYSCTL(sctp_rto_initial_default); |
michael@0 | 2949 | m->initial_init_rto_max = SCTP_BASE_SYSCTL(sctp_init_rto_max_default); |
michael@0 | 2950 | m->sctp_sack_freq = SCTP_BASE_SYSCTL(sctp_sack_freq_default); |
michael@0 | 2951 | m->max_init_times = SCTP_BASE_SYSCTL(sctp_init_rtx_max_default); |
michael@0 | 2952 | m->max_send_times = SCTP_BASE_SYSCTL(sctp_assoc_rtx_max_default); |
michael@0 | 2953 | m->def_net_failure = SCTP_BASE_SYSCTL(sctp_path_rtx_max_default); |
michael@0 | 2954 | m->def_net_pf_threshold = SCTP_BASE_SYSCTL(sctp_path_pf_threshold); |
michael@0 | 2955 | m->sctp_sws_sender = SCTP_SWS_SENDER_DEF; |
michael@0 | 2956 | m->sctp_sws_receiver = SCTP_SWS_RECEIVER_DEF; |
michael@0 | 2957 | m->max_burst = SCTP_BASE_SYSCTL(sctp_max_burst_default); |
michael@0 | 2958 | m->fr_max_burst = SCTP_BASE_SYSCTL(sctp_fr_max_burst_default); |
michael@0 | 2959 | |
michael@0 | 2960 | m->sctp_default_cc_module = SCTP_BASE_SYSCTL(sctp_default_cc_module); |
michael@0 | 2961 | m->sctp_default_ss_module = SCTP_BASE_SYSCTL(sctp_default_ss_module); |
michael@0 | 2962 | m->max_open_streams_intome = SCTP_BASE_SYSCTL(sctp_nr_incoming_streams_default); |
michael@0 | 2963 | /* number of streams to pre-open on a association */ |
michael@0 | 2964 | m->pre_open_stream_count = SCTP_BASE_SYSCTL(sctp_nr_outgoing_streams_default); |
michael@0 | 2965 | |
michael@0 | 2966 | /* Add adaptation cookie */ |
michael@0 | 2967 | m->adaptation_layer_indicator = 0; |
michael@0 | 2968 | m->adaptation_layer_indicator_provided = 0; |
michael@0 | 2969 | |
michael@0 | 2970 | /* seed random number generator */ |
michael@0 | 2971 | m->random_counter = 1; |
michael@0 | 2972 | m->store_at = SCTP_SIGNATURE_SIZE; |
michael@0 | 2973 | SCTP_READ_RANDOM(m->random_numbers, sizeof(m->random_numbers)); |
michael@0 | 2974 | sctp_fill_random_store(m); |
michael@0 | 2975 | |
michael@0 | 2976 | /* Minimum cookie size */ |
michael@0 | 2977 | m->size_of_a_cookie = (sizeof(struct sctp_init_msg) * 2) + |
michael@0 | 2978 | sizeof(struct sctp_state_cookie); |
michael@0 | 2979 | m->size_of_a_cookie += SCTP_SIGNATURE_SIZE; |
michael@0 | 2980 | |
michael@0 | 2981 | /* Setup the initial secret */ |
michael@0 | 2982 | (void)SCTP_GETTIME_TIMEVAL(&time); |
michael@0 | 2983 | m->time_of_secret_change = time.tv_sec; |
michael@0 | 2984 | |
michael@0 | 2985 | for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) { |
michael@0 | 2986 | m->secret_key[0][i] = sctp_select_initial_TSN(m); |
michael@0 | 2987 | } |
michael@0 | 2988 | sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL); |
michael@0 | 2989 | |
michael@0 | 2990 | /* How long is a cookie good for ? */ |
michael@0 | 2991 | m->def_cookie_life = MSEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_valid_cookie_life_default)); |
michael@0 | 2992 | /* |
michael@0 | 2993 | * Initialize authentication parameters |
michael@0 | 2994 | */ |
michael@0 | 2995 | m->local_hmacs = sctp_default_supported_hmaclist(); |
michael@0 | 2996 | m->local_auth_chunks = sctp_alloc_chunklist(); |
michael@0 | 2997 | m->default_dscp = 0; |
michael@0 | 2998 | #ifdef INET6 |
michael@0 | 2999 | m->default_flowlabel = 0; |
michael@0 | 3000 | #endif |
michael@0 | 3001 | m->port = 0; /* encapsulation disabled by default */ |
michael@0 | 3002 | sctp_auth_set_default_chunks(m->local_auth_chunks); |
michael@0 | 3003 | LIST_INIT(&m->shared_keys); |
michael@0 | 3004 | /* add default NULL key as key id 0 */ |
michael@0 | 3005 | null_key = sctp_alloc_sharedkey(); |
michael@0 | 3006 | sctp_insert_sharedkey(&m->shared_keys, null_key); |
michael@0 | 3007 | SCTP_INP_WUNLOCK(inp); |
michael@0 | 3008 | #ifdef SCTP_LOG_CLOSING |
michael@0 | 3009 | sctp_log_closing(inp, NULL, 12); |
michael@0 | 3010 | #endif |
michael@0 | 3011 | return (error); |
michael@0 | 3012 | } |
michael@0 | 3013 | |
michael@0 | 3014 | |
michael@0 | 3015 | void |
michael@0 | 3016 | sctp_move_pcb_and_assoc(struct sctp_inpcb *old_inp, struct sctp_inpcb *new_inp, |
michael@0 | 3017 | struct sctp_tcb *stcb) |
michael@0 | 3018 | { |
michael@0 | 3019 | struct sctp_nets *net; |
michael@0 | 3020 | uint16_t lport, rport; |
michael@0 | 3021 | struct sctppcbhead *head; |
michael@0 | 3022 | struct sctp_laddr *laddr, *oladdr; |
michael@0 | 3023 | |
michael@0 | 3024 | atomic_add_int(&stcb->asoc.refcnt, 1); |
michael@0 | 3025 | SCTP_TCB_UNLOCK(stcb); |
michael@0 | 3026 | SCTP_INP_INFO_WLOCK(); |
michael@0 | 3027 | SCTP_INP_WLOCK(old_inp); |
michael@0 | 3028 | SCTP_INP_WLOCK(new_inp); |
michael@0 | 3029 | SCTP_TCB_LOCK(stcb); |
michael@0 | 3030 | atomic_subtract_int(&stcb->asoc.refcnt, 1); |
michael@0 | 3031 | |
michael@0 | 3032 | new_inp->sctp_ep.time_of_secret_change = |
michael@0 | 3033 | old_inp->sctp_ep.time_of_secret_change; |
michael@0 | 3034 | memcpy(new_inp->sctp_ep.secret_key, old_inp->sctp_ep.secret_key, |
michael@0 | 3035 | sizeof(old_inp->sctp_ep.secret_key)); |
michael@0 | 3036 | new_inp->sctp_ep.current_secret_number = |
michael@0 | 3037 | old_inp->sctp_ep.current_secret_number; |
michael@0 | 3038 | new_inp->sctp_ep.last_secret_number = |
michael@0 | 3039 | old_inp->sctp_ep.last_secret_number; |
michael@0 | 3040 | new_inp->sctp_ep.size_of_a_cookie = old_inp->sctp_ep.size_of_a_cookie; |
michael@0 | 3041 | |
michael@0 | 3042 | /* make it so new data pours into the new socket */ |
michael@0 | 3043 | stcb->sctp_socket = new_inp->sctp_socket; |
michael@0 | 3044 | stcb->sctp_ep = new_inp; |
michael@0 | 3045 | |
michael@0 | 3046 | /* Copy the port across */ |
michael@0 | 3047 | lport = new_inp->sctp_lport = old_inp->sctp_lport; |
michael@0 | 3048 | rport = stcb->rport; |
michael@0 | 3049 | /* Pull the tcb from the old association */ |
michael@0 | 3050 | LIST_REMOVE(stcb, sctp_tcbhash); |
michael@0 | 3051 | LIST_REMOVE(stcb, sctp_tcblist); |
michael@0 | 3052 | if (stcb->asoc.in_asocid_hash) { |
michael@0 | 3053 | LIST_REMOVE(stcb, sctp_tcbasocidhash); |
michael@0 | 3054 | } |
michael@0 | 3055 | /* Now insert the new_inp into the TCP connected hash */ |
michael@0 | 3056 | head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR((lport | rport), SCTP_BASE_INFO(hashtcpmark))]; |
michael@0 | 3057 | |
michael@0 | 3058 | LIST_INSERT_HEAD(head, new_inp, sctp_hash); |
michael@0 | 3059 | /* Its safe to access */ |
michael@0 | 3060 | new_inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND; |
michael@0 | 3061 | |
michael@0 | 3062 | /* Now move the tcb into the endpoint list */ |
michael@0 | 3063 | LIST_INSERT_HEAD(&new_inp->sctp_asoc_list, stcb, sctp_tcblist); |
michael@0 | 3064 | /* |
michael@0 | 3065 | * Question, do we even need to worry about the ep-hash since we |
michael@0 | 3066 | * only have one connection? Probably not :> so lets get rid of it |
michael@0 | 3067 | * and not suck up any kernel memory in that. |
michael@0 | 3068 | */ |
michael@0 | 3069 | if (stcb->asoc.in_asocid_hash) { |
michael@0 | 3070 | struct sctpasochead *lhd; |
michael@0 | 3071 | lhd = &new_inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(stcb->asoc.assoc_id, |
michael@0 | 3072 | new_inp->hashasocidmark)]; |
michael@0 | 3073 | LIST_INSERT_HEAD(lhd, stcb, sctp_tcbasocidhash); |
michael@0 | 3074 | } |
michael@0 | 3075 | /* Ok. Let's restart timer. */ |
michael@0 | 3076 | TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { |
michael@0 | 3077 | sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, new_inp, |
michael@0 | 3078 | stcb, net); |
michael@0 | 3079 | } |
michael@0 | 3080 | |
michael@0 | 3081 | SCTP_INP_INFO_WUNLOCK(); |
michael@0 | 3082 | if (new_inp->sctp_tcbhash != NULL) { |
michael@0 | 3083 | SCTP_HASH_FREE(new_inp->sctp_tcbhash, new_inp->sctp_hashmark); |
michael@0 | 3084 | new_inp->sctp_tcbhash = NULL; |
michael@0 | 3085 | } |
michael@0 | 3086 | if ((new_inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) { |
michael@0 | 3087 | /* Subset bound, so copy in the laddr list from the old_inp */ |
michael@0 | 3088 | LIST_FOREACH(oladdr, &old_inp->sctp_addr_list, sctp_nxt_addr) { |
michael@0 | 3089 | laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr); |
michael@0 | 3090 | if (laddr == NULL) { |
michael@0 | 3091 | /* |
michael@0 | 3092 | * Gak, what can we do? This assoc is really |
michael@0 | 3093 | * HOSED. We probably should send an abort |
michael@0 | 3094 | * here. |
michael@0 | 3095 | */ |
michael@0 | 3096 | SCTPDBG(SCTP_DEBUG_PCB1, "Association hosed in TCP model, out of laddr memory\n"); |
michael@0 | 3097 | continue; |
michael@0 | 3098 | } |
michael@0 | 3099 | SCTP_INCR_LADDR_COUNT(); |
michael@0 | 3100 | bzero(laddr, sizeof(*laddr)); |
michael@0 | 3101 | (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time); |
michael@0 | 3102 | laddr->ifa = oladdr->ifa; |
michael@0 | 3103 | atomic_add_int(&laddr->ifa->refcount, 1); |
michael@0 | 3104 | LIST_INSERT_HEAD(&new_inp->sctp_addr_list, laddr, |
michael@0 | 3105 | sctp_nxt_addr); |
michael@0 | 3106 | new_inp->laddr_count++; |
michael@0 | 3107 | if (oladdr == stcb->asoc.last_used_address) { |
michael@0 | 3108 | stcb->asoc.last_used_address = laddr; |
michael@0 | 3109 | } |
michael@0 | 3110 | } |
michael@0 | 3111 | } |
michael@0 | 3112 | /* Now any running timers need to be adjusted |
michael@0 | 3113 | * since we really don't care if they are running |
michael@0 | 3114 | * or not just blast in the new_inp into all of |
michael@0 | 3115 | * them. |
michael@0 | 3116 | */ |
michael@0 | 3117 | |
michael@0 | 3118 | stcb->asoc.dack_timer.ep = (void *)new_inp; |
michael@0 | 3119 | stcb->asoc.asconf_timer.ep = (void *)new_inp; |
michael@0 | 3120 | stcb->asoc.strreset_timer.ep = (void *)new_inp; |
michael@0 | 3121 | stcb->asoc.shut_guard_timer.ep = (void *)new_inp; |
michael@0 | 3122 | stcb->asoc.autoclose_timer.ep = (void *)new_inp; |
michael@0 | 3123 | stcb->asoc.delayed_event_timer.ep = (void *)new_inp; |
michael@0 | 3124 | stcb->asoc.delete_prim_timer.ep = (void *)new_inp; |
michael@0 | 3125 | /* now what about the nets? */ |
michael@0 | 3126 | TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { |
michael@0 | 3127 | net->pmtu_timer.ep = (void *)new_inp; |
michael@0 | 3128 | net->hb_timer.ep = (void *)new_inp; |
michael@0 | 3129 | net->rxt_timer.ep = (void *)new_inp; |
michael@0 | 3130 | } |
michael@0 | 3131 | SCTP_INP_WUNLOCK(new_inp); |
michael@0 | 3132 | SCTP_INP_WUNLOCK(old_inp); |
michael@0 | 3133 | } |
michael@0 | 3134 | |
michael@0 | 3135 | |
michael@0 | 3136 | #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Userspace__)) |
michael@0 | 3137 | /* |
michael@0 | 3138 | * Don't know why, but without this there is an unknown reference when |
michael@0 | 3139 | * compiling NetBSD... hmm |
michael@0 | 3140 | */ |
michael@0 | 3141 | extern void in6_sin6_2_sin(struct sockaddr_in *, struct sockaddr_in6 *sin6); |
michael@0 | 3142 | #endif |
michael@0 | 3143 | |
michael@0 | 3144 | |
michael@0 | 3145 | /* sctp_ifap is used to bypass normal local address validation checks */ |
michael@0 | 3146 | int |
michael@0 | 3147 | #if defined(__FreeBSD__) && __FreeBSD_version >= 500000 |
michael@0 | 3148 | sctp_inpcb_bind(struct socket *so, struct sockaddr *addr, |
michael@0 | 3149 | struct sctp_ifa *sctp_ifap, struct thread *p) |
michael@0 | 3150 | #elif defined(__Windows__) |
michael@0 | 3151 | sctp_inpcb_bind(struct socket *so, struct sockaddr *addr, |
michael@0 | 3152 | struct sctp_ifa *sctp_ifap, PKTHREAD p) |
michael@0 | 3153 | #else |
michael@0 | 3154 | sctp_inpcb_bind(struct socket *so, struct sockaddr *addr, |
michael@0 | 3155 | struct sctp_ifa *sctp_ifap, struct proc *p) |
michael@0 | 3156 | #endif |
michael@0 | 3157 | { |
michael@0 | 3158 | /* bind a ep to a socket address */ |
michael@0 | 3159 | struct sctppcbhead *head; |
michael@0 | 3160 | struct sctp_inpcb *inp, *inp_tmp; |
michael@0 | 3161 | #if defined(INET) || (defined(INET6) && defined(__APPLE__)) || defined(__FreeBSD__) || defined(__APPLE__) |
michael@0 | 3162 | struct inpcb *ip_inp; |
michael@0 | 3163 | #endif |
michael@0 | 3164 | int port_reuse_active = 0; |
michael@0 | 3165 | int bindall; |
michael@0 | 3166 | #ifdef SCTP_MVRF |
michael@0 | 3167 | int i; |
michael@0 | 3168 | #endif |
michael@0 | 3169 | uint16_t lport; |
michael@0 | 3170 | int error; |
michael@0 | 3171 | uint32_t vrf_id; |
michael@0 | 3172 | |
michael@0 | 3173 | lport = 0; |
michael@0 | 3174 | error = 0; |
michael@0 | 3175 | bindall = 1; |
michael@0 | 3176 | inp = (struct sctp_inpcb *)so->so_pcb; |
michael@0 | 3177 | #if defined(INET) || (defined(INET6) && defined(__APPLE__)) || defined(__FreeBSD__) || defined(__APPLE__) |
michael@0 | 3178 | ip_inp = (struct inpcb *)so->so_pcb; |
michael@0 | 3179 | #endif |
michael@0 | 3180 | #ifdef SCTP_DEBUG |
michael@0 | 3181 | if (addr) { |
michael@0 | 3182 | SCTPDBG(SCTP_DEBUG_PCB1, "Bind called port: %d\n", |
michael@0 | 3183 | ntohs(((struct sockaddr_in *)addr)->sin_port)); |
michael@0 | 3184 | SCTPDBG(SCTP_DEBUG_PCB1, "Addr: "); |
michael@0 | 3185 | SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr); |
michael@0 | 3186 | } |
michael@0 | 3187 | #endif |
michael@0 | 3188 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) { |
michael@0 | 3189 | /* already did a bind, subsequent binds NOT allowed ! */ |
michael@0 | 3190 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
michael@0 | 3191 | return (EINVAL); |
michael@0 | 3192 | } |
michael@0 | 3193 | #if defined(__FreeBSD__) && __FreeBSD_version >= 500000 |
michael@0 | 3194 | #ifdef INVARIANTS |
michael@0 | 3195 | if (p == NULL) |
michael@0 | 3196 | panic("null proc/thread"); |
michael@0 | 3197 | #endif |
michael@0 | 3198 | #endif |
michael@0 | 3199 | if (addr != NULL) { |
michael@0 | 3200 | switch (addr->sa_family) { |
michael@0 | 3201 | #ifdef INET |
michael@0 | 3202 | case AF_INET: |
michael@0 | 3203 | { |
michael@0 | 3204 | struct sockaddr_in *sin; |
michael@0 | 3205 | |
michael@0 | 3206 | /* IPV6_V6ONLY socket? */ |
michael@0 | 3207 | if (SCTP_IPV6_V6ONLY(ip_inp)) { |
michael@0 | 3208 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
michael@0 | 3209 | return (EINVAL); |
michael@0 | 3210 | } |
michael@0 | 3211 | #ifdef HAVE_SA_LEN |
michael@0 | 3212 | if (addr->sa_len != sizeof(*sin)) { |
michael@0 | 3213 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
michael@0 | 3214 | return (EINVAL); |
michael@0 | 3215 | } |
michael@0 | 3216 | #endif |
michael@0 | 3217 | |
michael@0 | 3218 | sin = (struct sockaddr_in *)addr; |
michael@0 | 3219 | lport = sin->sin_port; |
michael@0 | 3220 | #if defined(__FreeBSD__) && __FreeBSD_version >= 800000 |
michael@0 | 3221 | /* |
michael@0 | 3222 | * For LOOPBACK the prison_local_ip4() call will transmute the ip address |
michael@0 | 3223 | * to the proper value. |
michael@0 | 3224 | */ |
michael@0 | 3225 | if (p && (error = prison_local_ip4(p->td_ucred, &sin->sin_addr)) != 0) { |
michael@0 | 3226 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); |
michael@0 | 3227 | return (error); |
michael@0 | 3228 | } |
michael@0 | 3229 | #endif |
michael@0 | 3230 | if (sin->sin_addr.s_addr != INADDR_ANY) { |
michael@0 | 3231 | bindall = 0; |
michael@0 | 3232 | } |
michael@0 | 3233 | break; |
michael@0 | 3234 | } |
michael@0 | 3235 | #endif |
michael@0 | 3236 | #ifdef INET6 |
michael@0 | 3237 | case AF_INET6: |
michael@0 | 3238 | { |
michael@0 | 3239 | /* Only for pure IPv6 Address. (No IPv4 Mapped!) */ |
michael@0 | 3240 | struct sockaddr_in6 *sin6; |
michael@0 | 3241 | |
michael@0 | 3242 | sin6 = (struct sockaddr_in6 *)addr; |
michael@0 | 3243 | |
michael@0 | 3244 | #ifdef HAVE_SA_LEN |
michael@0 | 3245 | if (addr->sa_len != sizeof(*sin6)) { |
michael@0 | 3246 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
michael@0 | 3247 | return (EINVAL); |
michael@0 | 3248 | } |
michael@0 | 3249 | #endif |
michael@0 | 3250 | lport = sin6->sin6_port; |
michael@0 | 3251 | #if defined(__FreeBSD__) && __FreeBSD_version >= 800000 |
michael@0 | 3252 | /* |
michael@0 | 3253 | * For LOOPBACK the prison_local_ip6() call will transmute the ipv6 address |
michael@0 | 3254 | * to the proper value. |
michael@0 | 3255 | */ |
michael@0 | 3256 | if (p && (error = prison_local_ip6(p->td_ucred, &sin6->sin6_addr, |
michael@0 | 3257 | (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) { |
michael@0 | 3258 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); |
michael@0 | 3259 | return (error); |
michael@0 | 3260 | } |
michael@0 | 3261 | #endif |
michael@0 | 3262 | if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { |
michael@0 | 3263 | bindall = 0; |
michael@0 | 3264 | #ifdef SCTP_EMBEDDED_V6_SCOPE |
michael@0 | 3265 | /* KAME hack: embed scopeid */ |
michael@0 | 3266 | #if defined(SCTP_KAME) |
michael@0 | 3267 | if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) { |
michael@0 | 3268 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
michael@0 | 3269 | return (EINVAL); |
michael@0 | 3270 | } |
michael@0 | 3271 | #elif defined(__APPLE__) |
michael@0 | 3272 | #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) |
michael@0 | 3273 | if (in6_embedscope(&sin6->sin6_addr, sin6, ip_inp, NULL) != 0) { |
michael@0 | 3274 | #else |
michael@0 | 3275 | if (in6_embedscope(&sin6->sin6_addr, sin6, ip_inp, NULL, NULL) != 0) { |
michael@0 | 3276 | #endif |
michael@0 | 3277 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
michael@0 | 3278 | return (EINVAL); |
michael@0 | 3279 | } |
michael@0 | 3280 | #elif defined(__FreeBSD__) |
michael@0 | 3281 | error = scope6_check_id(sin6, MODULE_GLOBAL(ip6_use_defzone)); |
michael@0 | 3282 | if (error != 0) { |
michael@0 | 3283 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); |
michael@0 | 3284 | return (error); |
michael@0 | 3285 | } |
michael@0 | 3286 | #else |
michael@0 | 3287 | if (in6_embedscope(&sin6->sin6_addr, sin6) != 0) { |
michael@0 | 3288 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
michael@0 | 3289 | return (EINVAL); |
michael@0 | 3290 | } |
michael@0 | 3291 | #endif |
michael@0 | 3292 | #endif /* SCTP_EMBEDDED_V6_SCOPE */ |
michael@0 | 3293 | } |
michael@0 | 3294 | #ifndef SCOPEDROUTING |
michael@0 | 3295 | /* this must be cleared for ifa_ifwithaddr() */ |
michael@0 | 3296 | sin6->sin6_scope_id = 0; |
michael@0 | 3297 | #endif /* SCOPEDROUTING */ |
michael@0 | 3298 | break; |
michael@0 | 3299 | } |
michael@0 | 3300 | #endif |
michael@0 | 3301 | #if defined(__Userspace__) |
michael@0 | 3302 | case AF_CONN: |
michael@0 | 3303 | { |
michael@0 | 3304 | struct sockaddr_conn *sconn; |
michael@0 | 3305 | |
michael@0 | 3306 | #ifdef HAVE_SA_LEN |
michael@0 | 3307 | if (addr->sa_len != sizeof(struct sockaddr_conn)) { |
michael@0 | 3308 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
michael@0 | 3309 | return (EINVAL); |
michael@0 | 3310 | } |
michael@0 | 3311 | #endif |
michael@0 | 3312 | sconn = (struct sockaddr_conn *)addr; |
michael@0 | 3313 | lport = sconn->sconn_port; |
michael@0 | 3314 | if (sconn->sconn_addr != NULL) { |
michael@0 | 3315 | bindall = 0; |
michael@0 | 3316 | } |
michael@0 | 3317 | break; |
michael@0 | 3318 | } |
michael@0 | 3319 | #endif |
michael@0 | 3320 | default: |
michael@0 | 3321 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EAFNOSUPPORT); |
michael@0 | 3322 | return (EAFNOSUPPORT); |
michael@0 | 3323 | } |
michael@0 | 3324 | } |
michael@0 | 3325 | SCTP_INP_INFO_WLOCK(); |
michael@0 | 3326 | SCTP_INP_WLOCK(inp); |
michael@0 | 3327 | /* Setup a vrf_id to be the default for the non-bind-all case. */ |
michael@0 | 3328 | vrf_id = inp->def_vrf_id; |
michael@0 | 3329 | |
michael@0 | 3330 | /* increase our count due to the unlock we do */ |
michael@0 | 3331 | SCTP_INP_INCR_REF(inp); |
michael@0 | 3332 | if (lport) { |
michael@0 | 3333 | /* |
michael@0 | 3334 | * Did the caller specify a port? if so we must see if an ep |
michael@0 | 3335 | * already has this one bound. |
michael@0 | 3336 | */ |
michael@0 | 3337 | /* got to be root to get at low ports */ |
michael@0 | 3338 | #if !defined(__Windows__) |
michael@0 | 3339 | if (ntohs(lport) < IPPORT_RESERVED) { |
michael@0 | 3340 | if (p && (error = |
michael@0 | 3341 | #ifdef __FreeBSD__ |
michael@0 | 3342 | #if __FreeBSD_version > 602000 |
michael@0 | 3343 | priv_check(p, PRIV_NETINET_RESERVEDPORT) |
michael@0 | 3344 | #elif __FreeBSD_version >= 500000 |
michael@0 | 3345 | suser_cred(p->td_ucred, 0) |
michael@0 | 3346 | #else |
michael@0 | 3347 | suser(p) |
michael@0 | 3348 | #endif |
michael@0 | 3349 | #elif defined(__APPLE__) |
michael@0 | 3350 | suser(p->p_ucred, &p->p_acflag) |
michael@0 | 3351 | #elif defined(__Userspace__) /* must be true to use raw socket */ |
michael@0 | 3352 | 1 |
michael@0 | 3353 | #else |
michael@0 | 3354 | suser(p, 0) |
michael@0 | 3355 | #endif |
michael@0 | 3356 | )) { |
michael@0 | 3357 | SCTP_INP_DECR_REF(inp); |
michael@0 | 3358 | SCTP_INP_WUNLOCK(inp); |
michael@0 | 3359 | SCTP_INP_INFO_WUNLOCK(); |
michael@0 | 3360 | return (error); |
michael@0 | 3361 | } |
michael@0 | 3362 | #if defined(__Panda__) |
michael@0 | 3363 | if (!SCTP_IS_PRIVILEDGED(so)) { |
michael@0 | 3364 | SCTP_INP_DECR_REF(inp); |
michael@0 | 3365 | SCTP_INP_WUNLOCK(inp); |
michael@0 | 3366 | SCTP_INP_INFO_WUNLOCK(); |
michael@0 | 3367 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EACCES); |
michael@0 | 3368 | return (EACCES); |
michael@0 | 3369 | } |
michael@0 | 3370 | #endif |
michael@0 | 3371 | } |
michael@0 | 3372 | #if !defined(__Panda__) && !defined(__Userspace__) |
michael@0 | 3373 | if (p == NULL) { |
michael@0 | 3374 | SCTP_INP_DECR_REF(inp); |
michael@0 | 3375 | SCTP_INP_WUNLOCK(inp); |
michael@0 | 3376 | SCTP_INP_INFO_WUNLOCK(); |
michael@0 | 3377 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); |
michael@0 | 3378 | return (error); |
michael@0 | 3379 | } |
michael@0 | 3380 | #endif |
michael@0 | 3381 | #endif /* __Windows__ */ |
michael@0 | 3382 | SCTP_INP_WUNLOCK(inp); |
michael@0 | 3383 | if (bindall) { |
michael@0 | 3384 | #ifdef SCTP_MVRF |
michael@0 | 3385 | for (i = 0; i < inp->num_vrfs; i++) { |
michael@0 | 3386 | vrf_id = inp->m_vrf_ids[i]; |
michael@0 | 3387 | #else |
michael@0 | 3388 | vrf_id = inp->def_vrf_id; |
michael@0 | 3389 | #endif |
michael@0 | 3390 | inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id); |
michael@0 | 3391 | if (inp_tmp != NULL) { |
michael@0 | 3392 | /* |
michael@0 | 3393 | * lock guy returned and lower count |
michael@0 | 3394 | * note that we are not bound so |
michael@0 | 3395 | * inp_tmp should NEVER be inp. And |
michael@0 | 3396 | * it is this inp (inp_tmp) that gets |
michael@0 | 3397 | * the reference bump, so we must |
michael@0 | 3398 | * lower it. |
michael@0 | 3399 | */ |
michael@0 | 3400 | SCTP_INP_DECR_REF(inp_tmp); |
michael@0 | 3401 | /* unlock info */ |
michael@0 | 3402 | if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) && |
michael@0 | 3403 | (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) { |
michael@0 | 3404 | /* Ok, must be one-2-one and allowing port re-use */ |
michael@0 | 3405 | port_reuse_active = 1; |
michael@0 | 3406 | goto continue_anyway; |
michael@0 | 3407 | } |
michael@0 | 3408 | SCTP_INP_DECR_REF(inp); |
michael@0 | 3409 | SCTP_INP_INFO_WUNLOCK(); |
michael@0 | 3410 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE); |
michael@0 | 3411 | return (EADDRINUSE); |
michael@0 | 3412 | } |
michael@0 | 3413 | #ifdef SCTP_MVRF |
michael@0 | 3414 | } |
michael@0 | 3415 | #endif |
michael@0 | 3416 | } else { |
michael@0 | 3417 | inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id); |
michael@0 | 3418 | if (inp_tmp != NULL) { |
michael@0 | 3419 | /* |
michael@0 | 3420 | * lock guy returned and lower count note |
michael@0 | 3421 | * that we are not bound so inp_tmp should |
michael@0 | 3422 | * NEVER be inp. And it is this inp (inp_tmp) |
michael@0 | 3423 | * that gets the reference bump, so we must |
michael@0 | 3424 | * lower it. |
michael@0 | 3425 | */ |
michael@0 | 3426 | SCTP_INP_DECR_REF(inp_tmp); |
michael@0 | 3427 | /* unlock info */ |
michael@0 | 3428 | if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) && |
michael@0 | 3429 | (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) { |
michael@0 | 3430 | /* Ok, must be one-2-one and allowing port re-use */ |
michael@0 | 3431 | port_reuse_active = 1; |
michael@0 | 3432 | goto continue_anyway; |
michael@0 | 3433 | } |
michael@0 | 3434 | SCTP_INP_DECR_REF(inp); |
michael@0 | 3435 | SCTP_INP_INFO_WUNLOCK(); |
michael@0 | 3436 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE); |
michael@0 | 3437 | return (EADDRINUSE); |
michael@0 | 3438 | } |
michael@0 | 3439 | } |
michael@0 | 3440 | continue_anyway: |
michael@0 | 3441 | SCTP_INP_WLOCK(inp); |
michael@0 | 3442 | if (bindall) { |
michael@0 | 3443 | /* verify that no lport is not used by a singleton */ |
michael@0 | 3444 | if ((port_reuse_active == 0) && |
michael@0 | 3445 | (inp_tmp = sctp_isport_inuse(inp, lport, vrf_id))) { |
michael@0 | 3446 | /* Sorry someone already has this one bound */ |
michael@0 | 3447 | if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) && |
michael@0 | 3448 | (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) { |
michael@0 | 3449 | port_reuse_active = 1; |
michael@0 | 3450 | } else { |
michael@0 | 3451 | SCTP_INP_DECR_REF(inp); |
michael@0 | 3452 | SCTP_INP_WUNLOCK(inp); |
michael@0 | 3453 | SCTP_INP_INFO_WUNLOCK(); |
michael@0 | 3454 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE); |
michael@0 | 3455 | return (EADDRINUSE); |
michael@0 | 3456 | } |
michael@0 | 3457 | } |
michael@0 | 3458 | } |
michael@0 | 3459 | } else { |
michael@0 | 3460 | uint16_t first, last, candidate; |
michael@0 | 3461 | uint16_t count; |
michael@0 | 3462 | int done; |
michael@0 | 3463 | |
michael@0 | 3464 | #if defined(__Windows__) |
michael@0 | 3465 | first = 1; |
michael@0 | 3466 | last = 0xffff; |
michael@0 | 3467 | #else |
michael@0 | 3468 | #if defined(__Userspace__) |
michael@0 | 3469 | /* TODO ensure uid is 0, etc... */ |
michael@0 | 3470 | #elif defined(__FreeBSD__) || defined(__APPLE__) |
michael@0 | 3471 | if (ip_inp->inp_flags & INP_HIGHPORT) { |
michael@0 | 3472 | first = MODULE_GLOBAL(ipport_hifirstauto); |
michael@0 | 3473 | last = MODULE_GLOBAL(ipport_hilastauto); |
michael@0 | 3474 | } else if (ip_inp->inp_flags & INP_LOWPORT) { |
michael@0 | 3475 | if (p && (error = |
michael@0 | 3476 | #ifdef __FreeBSD__ |
michael@0 | 3477 | #if __FreeBSD_version > 602000 |
michael@0 | 3478 | priv_check(p, PRIV_NETINET_RESERVEDPORT) |
michael@0 | 3479 | #elif __FreeBSD_version >= 500000 |
michael@0 | 3480 | suser_cred(p->td_ucred, 0) |
michael@0 | 3481 | #else |
michael@0 | 3482 | suser(p) |
michael@0 | 3483 | #endif |
michael@0 | 3484 | #elif defined(__APPLE__) |
michael@0 | 3485 | suser(p->p_ucred, &p->p_acflag) |
michael@0 | 3486 | #else |
michael@0 | 3487 | suser(p, 0) |
michael@0 | 3488 | #endif |
michael@0 | 3489 | )) { |
michael@0 | 3490 | SCTP_INP_DECR_REF(inp); |
michael@0 | 3491 | SCTP_INP_WUNLOCK(inp); |
michael@0 | 3492 | SCTP_INP_INFO_WUNLOCK(); |
michael@0 | 3493 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error); |
michael@0 | 3494 | return (error); |
michael@0 | 3495 | } |
michael@0 | 3496 | first = MODULE_GLOBAL(ipport_lowfirstauto); |
michael@0 | 3497 | last = MODULE_GLOBAL(ipport_lowlastauto); |
michael@0 | 3498 | } else { |
michael@0 | 3499 | #endif |
michael@0 | 3500 | first = MODULE_GLOBAL(ipport_firstauto); |
michael@0 | 3501 | last = MODULE_GLOBAL(ipport_lastauto); |
michael@0 | 3502 | #if defined(__FreeBSD__) || defined(__APPLE__) |
michael@0 | 3503 | } |
michael@0 | 3504 | #endif |
michael@0 | 3505 | #endif /* __Windows__ */ |
michael@0 | 3506 | if (first > last) { |
michael@0 | 3507 | uint16_t temp; |
michael@0 | 3508 | |
michael@0 | 3509 | temp = first; |
michael@0 | 3510 | first = last; |
michael@0 | 3511 | last = temp; |
michael@0 | 3512 | } |
michael@0 | 3513 | count = last - first + 1; /* number of candidates */ |
michael@0 | 3514 | candidate = first + sctp_select_initial_TSN(&inp->sctp_ep) % (count); |
michael@0 | 3515 | |
michael@0 | 3516 | done = 0; |
michael@0 | 3517 | while (!done) { |
michael@0 | 3518 | #ifdef SCTP_MVRF |
michael@0 | 3519 | for (i = 0; i < inp->num_vrfs; i++) { |
michael@0 | 3520 | if (sctp_isport_inuse(inp, htons(candidate), inp->m_vrf_ids[i]) != NULL) { |
michael@0 | 3521 | break; |
michael@0 | 3522 | } |
michael@0 | 3523 | } |
michael@0 | 3524 | if (i == inp->num_vrfs) { |
michael@0 | 3525 | done = 1; |
michael@0 | 3526 | } |
michael@0 | 3527 | #else |
michael@0 | 3528 | if (sctp_isport_inuse(inp, htons(candidate), inp->def_vrf_id) == NULL) { |
michael@0 | 3529 | done = 1; |
michael@0 | 3530 | } |
michael@0 | 3531 | #endif |
michael@0 | 3532 | if (!done) { |
michael@0 | 3533 | if (--count == 0) { |
michael@0 | 3534 | SCTP_INP_DECR_REF(inp); |
michael@0 | 3535 | SCTP_INP_WUNLOCK(inp); |
michael@0 | 3536 | SCTP_INP_INFO_WUNLOCK(); |
michael@0 | 3537 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE); |
michael@0 | 3538 | return (EADDRINUSE); |
michael@0 | 3539 | } |
michael@0 | 3540 | if (candidate == last) |
michael@0 | 3541 | candidate = first; |
michael@0 | 3542 | else |
michael@0 | 3543 | candidate = candidate + 1; |
michael@0 | 3544 | } |
michael@0 | 3545 | } |
michael@0 | 3546 | lport = htons(candidate); |
michael@0 | 3547 | } |
michael@0 | 3548 | SCTP_INP_DECR_REF(inp); |
michael@0 | 3549 | if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | |
michael@0 | 3550 | SCTP_PCB_FLAGS_SOCKET_ALLGONE)) { |
michael@0 | 3551 | /* |
michael@0 | 3552 | * this really should not happen. The guy did a non-blocking |
michael@0 | 3553 | * bind and then did a close at the same time. |
michael@0 | 3554 | */ |
michael@0 | 3555 | SCTP_INP_WUNLOCK(inp); |
michael@0 | 3556 | SCTP_INP_INFO_WUNLOCK(); |
michael@0 | 3557 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
michael@0 | 3558 | return (EINVAL); |
michael@0 | 3559 | } |
michael@0 | 3560 | /* ok we look clear to give out this port, so lets setup the binding */ |
michael@0 | 3561 | if (bindall) { |
michael@0 | 3562 | /* binding to all addresses, so just set in the proper flags */ |
michael@0 | 3563 | inp->sctp_flags |= SCTP_PCB_FLAGS_BOUNDALL; |
michael@0 | 3564 | /* set the automatic addr changes from kernel flag */ |
michael@0 | 3565 | if (SCTP_BASE_SYSCTL(sctp_auto_asconf) == 0) { |
michael@0 | 3566 | sctp_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF); |
michael@0 | 3567 | sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF); |
michael@0 | 3568 | } else { |
michael@0 | 3569 | sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF); |
michael@0 | 3570 | sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF); |
michael@0 | 3571 | } |
michael@0 | 3572 | if (SCTP_BASE_SYSCTL(sctp_multiple_asconfs) == 0) { |
michael@0 | 3573 | sctp_feature_off(inp, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS); |
michael@0 | 3574 | } else { |
michael@0 | 3575 | sctp_feature_on(inp, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS); |
michael@0 | 3576 | } |
michael@0 | 3577 | /* set the automatic mobility_base from kernel |
michael@0 | 3578 | flag (by micchie) |
michael@0 | 3579 | */ |
michael@0 | 3580 | if (SCTP_BASE_SYSCTL(sctp_mobility_base) == 0) { |
michael@0 | 3581 | sctp_mobility_feature_off(inp, SCTP_MOBILITY_BASE); |
michael@0 | 3582 | sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED); |
michael@0 | 3583 | } else { |
michael@0 | 3584 | sctp_mobility_feature_on(inp, SCTP_MOBILITY_BASE); |
michael@0 | 3585 | sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED); |
michael@0 | 3586 | } |
michael@0 | 3587 | /* set the automatic mobility_fasthandoff from kernel |
michael@0 | 3588 | flag (by micchie) |
michael@0 | 3589 | */ |
michael@0 | 3590 | if (SCTP_BASE_SYSCTL(sctp_mobility_fasthandoff) == 0) { |
michael@0 | 3591 | sctp_mobility_feature_off(inp, SCTP_MOBILITY_FASTHANDOFF); |
michael@0 | 3592 | sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED); |
michael@0 | 3593 | } else { |
michael@0 | 3594 | sctp_mobility_feature_on(inp, SCTP_MOBILITY_FASTHANDOFF); |
michael@0 | 3595 | sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED); |
michael@0 | 3596 | } |
michael@0 | 3597 | } else { |
michael@0 | 3598 | /* |
michael@0 | 3599 | * bind specific, make sure flags is off and add a new |
michael@0 | 3600 | * address structure to the sctp_addr_list inside the ep |
michael@0 | 3601 | * structure. |
michael@0 | 3602 | * |
michael@0 | 3603 | * We will need to allocate one and insert it at the head. The |
michael@0 | 3604 | * socketopt call can just insert new addresses in there as |
michael@0 | 3605 | * well. It will also have to do the embed scope kame hack |
michael@0 | 3606 | * too (before adding). |
michael@0 | 3607 | */ |
michael@0 | 3608 | struct sctp_ifa *ifa; |
michael@0 | 3609 | struct sockaddr_storage store_sa; |
michael@0 | 3610 | |
michael@0 | 3611 | memset(&store_sa, 0, sizeof(store_sa)); |
michael@0 | 3612 | switch (addr->sa_family) { |
michael@0 | 3613 | #ifdef INET |
michael@0 | 3614 | case AF_INET: |
michael@0 | 3615 | { |
michael@0 | 3616 | struct sockaddr_in *sin; |
michael@0 | 3617 | |
michael@0 | 3618 | sin = (struct sockaddr_in *)&store_sa; |
michael@0 | 3619 | memcpy(sin, addr, sizeof(struct sockaddr_in)); |
michael@0 | 3620 | sin->sin_port = 0; |
michael@0 | 3621 | break; |
michael@0 | 3622 | } |
michael@0 | 3623 | #endif |
michael@0 | 3624 | #ifdef INET6 |
michael@0 | 3625 | case AF_INET6: |
michael@0 | 3626 | { |
michael@0 | 3627 | struct sockaddr_in6 *sin6; |
michael@0 | 3628 | |
michael@0 | 3629 | sin6 = (struct sockaddr_in6 *)&store_sa; |
michael@0 | 3630 | memcpy(sin6, addr, sizeof(struct sockaddr_in6)); |
michael@0 | 3631 | sin6->sin6_port = 0; |
michael@0 | 3632 | break; |
michael@0 | 3633 | } |
michael@0 | 3634 | #endif |
michael@0 | 3635 | #if defined(__Userspace__) |
michael@0 | 3636 | case AF_CONN: |
michael@0 | 3637 | { |
michael@0 | 3638 | struct sockaddr_conn *sconn; |
michael@0 | 3639 | |
michael@0 | 3640 | sconn = (struct sockaddr_conn *)&store_sa; |
michael@0 | 3641 | memcpy(sconn, addr, sizeof(struct sockaddr_conn)); |
michael@0 | 3642 | sconn->sconn_port = 0; |
michael@0 | 3643 | break; |
michael@0 | 3644 | } |
michael@0 | 3645 | #endif |
michael@0 | 3646 | default: |
michael@0 | 3647 | break; |
michael@0 | 3648 | } |
michael@0 | 3649 | /* |
michael@0 | 3650 | * first find the interface with the bound address need to |
michael@0 | 3651 | * zero out the port to find the address! yuck! can't do |
michael@0 | 3652 | * this earlier since need port for sctp_pcb_findep() |
michael@0 | 3653 | */ |
michael@0 | 3654 | if (sctp_ifap != NULL) { |
michael@0 | 3655 | ifa = sctp_ifap; |
michael@0 | 3656 | } else { |
michael@0 | 3657 | /* Note for BSD we hit here always other |
michael@0 | 3658 | * O/S's will pass things in via the |
michael@0 | 3659 | * sctp_ifap argument (Panda). |
michael@0 | 3660 | */ |
michael@0 | 3661 | ifa = sctp_find_ifa_by_addr((struct sockaddr *)&store_sa, |
michael@0 | 3662 | vrf_id, SCTP_ADDR_NOT_LOCKED); |
michael@0 | 3663 | } |
michael@0 | 3664 | if (ifa == NULL) { |
michael@0 | 3665 | /* Can't find an interface with that address */ |
michael@0 | 3666 | SCTP_INP_WUNLOCK(inp); |
michael@0 | 3667 | SCTP_INP_INFO_WUNLOCK(); |
michael@0 | 3668 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRNOTAVAIL); |
michael@0 | 3669 | return (EADDRNOTAVAIL); |
michael@0 | 3670 | } |
michael@0 | 3671 | #ifdef INET6 |
michael@0 | 3672 | if (addr->sa_family == AF_INET6) { |
michael@0 | 3673 | /* GAK, more FIXME IFA lock? */ |
michael@0 | 3674 | if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) { |
michael@0 | 3675 | /* Can't bind a non-existent addr. */ |
michael@0 | 3676 | SCTP_INP_WUNLOCK(inp); |
michael@0 | 3677 | SCTP_INP_INFO_WUNLOCK(); |
michael@0 | 3678 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
michael@0 | 3679 | return (EINVAL); |
michael@0 | 3680 | } |
michael@0 | 3681 | } |
michael@0 | 3682 | #endif |
michael@0 | 3683 | /* we're not bound all */ |
michael@0 | 3684 | inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUNDALL; |
michael@0 | 3685 | /* allow bindx() to send ASCONF's for binding changes */ |
michael@0 | 3686 | sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF); |
michael@0 | 3687 | /* clear automatic addr changes from kernel flag */ |
michael@0 | 3688 | sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF); |
michael@0 | 3689 | |
michael@0 | 3690 | /* add this address to the endpoint list */ |
michael@0 | 3691 | error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, 0); |
michael@0 | 3692 | if (error != 0) { |
michael@0 | 3693 | SCTP_INP_WUNLOCK(inp); |
michael@0 | 3694 | SCTP_INP_INFO_WUNLOCK(); |
michael@0 | 3695 | return (error); |
michael@0 | 3696 | } |
michael@0 | 3697 | inp->laddr_count++; |
michael@0 | 3698 | } |
michael@0 | 3699 | /* find the bucket */ |
michael@0 | 3700 | if (port_reuse_active) { |
michael@0 | 3701 | /* Put it into tcp 1-2-1 hash */ |
michael@0 | 3702 | head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashtcpmark))]; |
michael@0 | 3703 | inp->sctp_flags |= SCTP_PCB_FLAGS_IN_TCPPOOL; |
michael@0 | 3704 | } else { |
michael@0 | 3705 | head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashmark))]; |
michael@0 | 3706 | } |
michael@0 | 3707 | /* put it in the bucket */ |
michael@0 | 3708 | LIST_INSERT_HEAD(head, inp, sctp_hash); |
michael@0 | 3709 | SCTPDBG(SCTP_DEBUG_PCB1, "Main hash to bind at head:%p, bound port:%d - in tcp_pool=%d\n", |
michael@0 | 3710 | (void *)head, ntohs(lport), port_reuse_active); |
michael@0 | 3711 | /* set in the port */ |
michael@0 | 3712 | inp->sctp_lport = lport; |
michael@0 | 3713 | |
michael@0 | 3714 | /* turn off just the unbound flag */ |
michael@0 | 3715 | inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND; |
michael@0 | 3716 | SCTP_INP_WUNLOCK(inp); |
michael@0 | 3717 | SCTP_INP_INFO_WUNLOCK(); |
michael@0 | 3718 | return (0); |
michael@0 | 3719 | } |
michael@0 | 3720 | |
michael@0 | 3721 | |
michael@0 | 3722 | static void |
michael@0 | 3723 | sctp_iterator_inp_being_freed(struct sctp_inpcb *inp) |
michael@0 | 3724 | { |
michael@0 | 3725 | struct sctp_iterator *it, *nit; |
michael@0 | 3726 | |
michael@0 | 3727 | /* |
michael@0 | 3728 | * We enter with the only the ITERATOR_LOCK in place and a write |
michael@0 | 3729 | * lock on the inp_info stuff. |
michael@0 | 3730 | */ |
michael@0 | 3731 | it = sctp_it_ctl.cur_it; |
michael@0 | 3732 | #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 |
michael@0 | 3733 | if (it && (it->vn != curvnet)) { |
michael@0 | 3734 | /* Its not looking at our VNET */ |
michael@0 | 3735 | return; |
michael@0 | 3736 | } |
michael@0 | 3737 | #endif |
michael@0 | 3738 | if (it && (it->inp == inp)) { |
michael@0 | 3739 | /* |
michael@0 | 3740 | * This is tricky and we hold the iterator lock, |
michael@0 | 3741 | * but when it returns and gets the lock (when we |
michael@0 | 3742 | * release it) the iterator will try to operate on |
michael@0 | 3743 | * inp. We need to stop that from happening. But |
michael@0 | 3744 | * of course the iterator has a reference on the |
michael@0 | 3745 | * stcb and inp. We can mark it and it will stop. |
michael@0 | 3746 | * |
michael@0 | 3747 | * If its a single iterator situation, we |
michael@0 | 3748 | * set the end iterator flag. Otherwise |
michael@0 | 3749 | * we set the iterator to go to the next inp. |
michael@0 | 3750 | * |
michael@0 | 3751 | */ |
michael@0 | 3752 | if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) { |
michael@0 | 3753 | sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_IT; |
michael@0 | 3754 | } else { |
michael@0 | 3755 | sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_INP; |
michael@0 | 3756 | } |
michael@0 | 3757 | } |
michael@0 | 3758 | /* Now go through and remove any single reference to |
michael@0 | 3759 | * our inp that may be still pending on the list |
michael@0 | 3760 | */ |
michael@0 | 3761 | SCTP_IPI_ITERATOR_WQ_LOCK(); |
michael@0 | 3762 | TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) { |
michael@0 | 3763 | #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 |
michael@0 | 3764 | if (it->vn != curvnet) { |
michael@0 | 3765 | continue; |
michael@0 | 3766 | } |
michael@0 | 3767 | #endif |
michael@0 | 3768 | if (it->inp == inp) { |
michael@0 | 3769 | /* This one points to me is it inp specific? */ |
michael@0 | 3770 | if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) { |
michael@0 | 3771 | /* Remove and free this one */ |
michael@0 | 3772 | TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, |
michael@0 | 3773 | it, sctp_nxt_itr); |
michael@0 | 3774 | if (it->function_atend != NULL) { |
michael@0 | 3775 | (*it->function_atend) (it->pointer, it->val); |
michael@0 | 3776 | } |
michael@0 | 3777 | SCTP_FREE(it, SCTP_M_ITER); |
michael@0 | 3778 | } else { |
michael@0 | 3779 | it->inp = LIST_NEXT(it->inp, sctp_list); |
michael@0 | 3780 | if (it->inp) { |
michael@0 | 3781 | SCTP_INP_INCR_REF(it->inp); |
michael@0 | 3782 | } |
michael@0 | 3783 | } |
michael@0 | 3784 | /* When its put in the refcnt is incremented so decr it */ |
michael@0 | 3785 | SCTP_INP_DECR_REF(inp); |
michael@0 | 3786 | } |
michael@0 | 3787 | } |
michael@0 | 3788 | SCTP_IPI_ITERATOR_WQ_UNLOCK(); |
michael@0 | 3789 | } |
michael@0 | 3790 | |
michael@0 | 3791 | /* release sctp_inpcb unbind the port */ |
michael@0 | 3792 | void |
michael@0 | 3793 | sctp_inpcb_free(struct sctp_inpcb *inp, int immediate, int from) |
michael@0 | 3794 | { |
michael@0 | 3795 | /* |
michael@0 | 3796 | * Here we free a endpoint. We must find it (if it is in the Hash |
michael@0 | 3797 | * table) and remove it from there. Then we must also find it in the |
michael@0 | 3798 | * overall list and remove it from there. After all removals are |
michael@0 | 3799 | * complete then any timer has to be stopped. Then start the actual |
michael@0 | 3800 | * freeing. a) Any local lists. b) Any associations. c) The hash of |
michael@0 | 3801 | * all associations. d) finally the ep itself. |
michael@0 | 3802 | */ |
michael@0 | 3803 | struct sctp_tcb *asoc, *nasoc; |
michael@0 | 3804 | struct sctp_laddr *laddr, *nladdr; |
michael@0 | 3805 | struct inpcb *ip_pcb; |
michael@0 | 3806 | struct socket *so; |
michael@0 | 3807 | int being_refed = 0; |
michael@0 | 3808 | struct sctp_queued_to_read *sq, *nsq; |
michael@0 | 3809 | #if !defined(__Panda__) && !defined(__Userspace__) |
michael@0 | 3810 | #if !defined(__FreeBSD__) || __FreeBSD_version < 500000 |
michael@0 | 3811 | sctp_rtentry_t *rt; |
michael@0 | 3812 | #endif |
michael@0 | 3813 | #endif |
michael@0 | 3814 | int cnt; |
michael@0 | 3815 | sctp_sharedkey_t *shared_key, *nshared_key; |
michael@0 | 3816 | |
michael@0 | 3817 | |
michael@0 | 3818 | #if defined(__APPLE__) |
michael@0 | 3819 | sctp_lock_assert(SCTP_INP_SO(inp)); |
michael@0 | 3820 | #endif |
michael@0 | 3821 | #ifdef SCTP_LOG_CLOSING |
michael@0 | 3822 | sctp_log_closing(inp, NULL, 0); |
michael@0 | 3823 | #endif |
michael@0 | 3824 | SCTP_ITERATOR_LOCK(); |
michael@0 | 3825 | /* mark any iterators on the list or being processed */ |
michael@0 | 3826 | sctp_iterator_inp_being_freed(inp); |
michael@0 | 3827 | SCTP_ITERATOR_UNLOCK(); |
michael@0 | 3828 | so = inp->sctp_socket; |
michael@0 | 3829 | if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
michael@0 | 3830 | /* been here before.. eeks.. get out of here */ |
michael@0 | 3831 | SCTP_PRINTF("This conflict in free SHOULD not be happening! from %d, imm %d\n", from, immediate); |
michael@0 | 3832 | #ifdef SCTP_LOG_CLOSING |
michael@0 | 3833 | sctp_log_closing(inp, NULL, 1); |
michael@0 | 3834 | #endif |
michael@0 | 3835 | return; |
michael@0 | 3836 | } |
michael@0 | 3837 | SCTP_ASOC_CREATE_LOCK(inp); |
michael@0 | 3838 | SCTP_INP_INFO_WLOCK(); |
michael@0 | 3839 | |
michael@0 | 3840 | SCTP_INP_WLOCK(inp); |
michael@0 | 3841 | if (from == SCTP_CALLED_AFTER_CMPSET_OFCLOSE) { |
michael@0 | 3842 | inp->sctp_flags &= ~SCTP_PCB_FLAGS_CLOSE_IP; |
michael@0 | 3843 | /* socket is gone, so no more wakeups allowed */ |
michael@0 | 3844 | inp->sctp_flags |= SCTP_PCB_FLAGS_DONT_WAKE; |
michael@0 | 3845 | inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT; |
michael@0 | 3846 | inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT; |
michael@0 | 3847 | |
michael@0 | 3848 | } |
michael@0 | 3849 | /* First time through we have the socket lock, after that no more. */ |
michael@0 | 3850 | sctp_timer_stop(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL, |
michael@0 | 3851 | SCTP_FROM_SCTP_PCB+SCTP_LOC_1); |
michael@0 | 3852 | |
michael@0 | 3853 | if (inp->control) { |
michael@0 | 3854 | sctp_m_freem(inp->control); |
michael@0 | 3855 | inp->control = NULL; |
michael@0 | 3856 | } |
michael@0 | 3857 | if (inp->pkt) { |
michael@0 | 3858 | sctp_m_freem(inp->pkt); |
michael@0 | 3859 | inp->pkt = NULL; |
michael@0 | 3860 | } |
michael@0 | 3861 | ip_pcb = &inp->ip_inp.inp; /* we could just cast the main pointer |
michael@0 | 3862 | * here but I will be nice :> (i.e. |
michael@0 | 3863 | * ip_pcb = ep;) */ |
michael@0 | 3864 | if (immediate == SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE) { |
michael@0 | 3865 | int cnt_in_sd; |
michael@0 | 3866 | |
michael@0 | 3867 | cnt_in_sd = 0; |
michael@0 | 3868 | LIST_FOREACH_SAFE(asoc, &inp->sctp_asoc_list, sctp_tcblist, nasoc) { |
michael@0 | 3869 | SCTP_TCB_LOCK(asoc); |
michael@0 | 3870 | if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { |
michael@0 | 3871 | /* Skip guys being freed */ |
michael@0 | 3872 | cnt_in_sd++; |
michael@0 | 3873 | if (asoc->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE) { |
michael@0 | 3874 | /* |
michael@0 | 3875 | * Special case - we did not start a kill |
michael@0 | 3876 | * timer on the asoc due to it was not |
michael@0 | 3877 | * closed. So go ahead and start it now. |
michael@0 | 3878 | */ |
michael@0 | 3879 | asoc->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE; |
michael@0 | 3880 | sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, asoc, NULL); |
michael@0 | 3881 | } |
michael@0 | 3882 | SCTP_TCB_UNLOCK(asoc); |
michael@0 | 3883 | continue; |
michael@0 | 3884 | } |
michael@0 | 3885 | if (((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_WAIT) || |
michael@0 | 3886 | (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_ECHOED)) && |
michael@0 | 3887 | (asoc->asoc.total_output_queue_size == 0)) { |
michael@0 | 3888 | /* If we have data in queue, we don't want to just |
michael@0 | 3889 | * free since the app may have done, send()/close |
michael@0 | 3890 | * or connect/send/close. And it wants the data |
michael@0 | 3891 | * to get across first. |
michael@0 | 3892 | */ |
michael@0 | 3893 | /* Just abandon things in the front states */ |
michael@0 | 3894 | if (sctp_free_assoc(inp, asoc, SCTP_PCBFREE_NOFORCE, |
michael@0 | 3895 | SCTP_FROM_SCTP_PCB+SCTP_LOC_2) == 0) { |
michael@0 | 3896 | cnt_in_sd++; |
michael@0 | 3897 | } |
michael@0 | 3898 | continue; |
michael@0 | 3899 | } |
michael@0 | 3900 | /* Disconnect the socket please */ |
michael@0 | 3901 | asoc->sctp_socket = NULL; |
michael@0 | 3902 | asoc->asoc.state |= SCTP_STATE_CLOSED_SOCKET; |
michael@0 | 3903 | if ((asoc->asoc.size_on_reasm_queue > 0) || |
michael@0 | 3904 | (asoc->asoc.control_pdapi) || |
michael@0 | 3905 | (asoc->asoc.size_on_all_streams > 0) || |
michael@0 | 3906 | (so && (so->so_rcv.sb_cc > 0))) { |
michael@0 | 3907 | /* Left with Data unread */ |
michael@0 | 3908 | struct mbuf *op_err; |
michael@0 | 3909 | |
michael@0 | 3910 | op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), |
michael@0 | 3911 | 0, M_NOWAIT, 1, MT_DATA); |
michael@0 | 3912 | if (op_err) { |
michael@0 | 3913 | /* Fill in the user initiated abort */ |
michael@0 | 3914 | struct sctp_paramhdr *ph; |
michael@0 | 3915 | |
michael@0 | 3916 | SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr); |
michael@0 | 3917 | ph = mtod(op_err, struct sctp_paramhdr *); |
michael@0 | 3918 | ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT); |
michael@0 | 3919 | ph->param_length = htons(SCTP_BUF_LEN(op_err)); |
michael@0 | 3920 | } |
michael@0 | 3921 | asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB+SCTP_LOC_3; |
michael@0 | 3922 | sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED); |
michael@0 | 3923 | SCTP_STAT_INCR_COUNTER32(sctps_aborted); |
michael@0 | 3924 | if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) || |
michael@0 | 3925 | (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { |
michael@0 | 3926 | SCTP_STAT_DECR_GAUGE32(sctps_currestab); |
michael@0 | 3927 | } |
michael@0 | 3928 | if (sctp_free_assoc(inp, asoc, |
michael@0 | 3929 | SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB+SCTP_LOC_4) == 0) { |
michael@0 | 3930 | cnt_in_sd++; |
michael@0 | 3931 | } |
michael@0 | 3932 | continue; |
michael@0 | 3933 | } else if (TAILQ_EMPTY(&asoc->asoc.send_queue) && |
michael@0 | 3934 | TAILQ_EMPTY(&asoc->asoc.sent_queue) && |
michael@0 | 3935 | (asoc->asoc.stream_queue_cnt == 0)) { |
michael@0 | 3936 | if (asoc->asoc.locked_on_sending) { |
michael@0 | 3937 | goto abort_anyway; |
michael@0 | 3938 | } |
michael@0 | 3939 | if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_SENT) && |
michael@0 | 3940 | (SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) { |
michael@0 | 3941 | struct sctp_nets *netp; |
michael@0 | 3942 | |
michael@0 | 3943 | /* |
michael@0 | 3944 | * there is nothing queued to send, |
michael@0 | 3945 | * so I send shutdown |
michael@0 | 3946 | */ |
michael@0 | 3947 | if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) || |
michael@0 | 3948 | (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { |
michael@0 | 3949 | SCTP_STAT_DECR_GAUGE32(sctps_currestab); |
michael@0 | 3950 | } |
michael@0 | 3951 | SCTP_SET_STATE(&asoc->asoc, SCTP_STATE_SHUTDOWN_SENT); |
michael@0 | 3952 | SCTP_CLEAR_SUBSTATE(&asoc->asoc, SCTP_STATE_SHUTDOWN_PENDING); |
michael@0 | 3953 | sctp_stop_timers_for_shutdown(asoc); |
michael@0 | 3954 | if (asoc->asoc.alternate) { |
michael@0 | 3955 | netp = asoc->asoc.alternate; |
michael@0 | 3956 | } else { |
michael@0 | 3957 | netp = asoc->asoc.primary_destination; |
michael@0 | 3958 | } |
michael@0 | 3959 | sctp_send_shutdown(asoc, netp); |
michael@0 | 3960 | sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, asoc->sctp_ep, asoc, |
michael@0 | 3961 | netp); |
michael@0 | 3962 | sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc, |
michael@0 | 3963 | asoc->asoc.primary_destination); |
michael@0 | 3964 | sctp_chunk_output(inp, asoc, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_LOCKED); |
michael@0 | 3965 | } |
michael@0 | 3966 | } else { |
michael@0 | 3967 | /* mark into shutdown pending */ |
michael@0 | 3968 | struct sctp_stream_queue_pending *sp; |
michael@0 | 3969 | |
michael@0 | 3970 | asoc->asoc.state |= SCTP_STATE_SHUTDOWN_PENDING; |
michael@0 | 3971 | sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc, |
michael@0 | 3972 | asoc->asoc.primary_destination); |
michael@0 | 3973 | if (asoc->asoc.locked_on_sending) { |
michael@0 | 3974 | sp = TAILQ_LAST(&((asoc->asoc.locked_on_sending)->outqueue), |
michael@0 | 3975 | sctp_streamhead); |
michael@0 | 3976 | if (sp == NULL) { |
michael@0 | 3977 | SCTP_PRINTF("Error, sp is NULL, locked on sending is %p strm:%d\n", |
michael@0 | 3978 | (void *)asoc->asoc.locked_on_sending, |
michael@0 | 3979 | asoc->asoc.locked_on_sending->stream_no); |
michael@0 | 3980 | } else { |
michael@0 | 3981 | if ((sp->length == 0) && (sp->msg_is_complete == 0)) |
michael@0 | 3982 | asoc->asoc.state |= SCTP_STATE_PARTIAL_MSG_LEFT; |
michael@0 | 3983 | } |
michael@0 | 3984 | } |
michael@0 | 3985 | if (TAILQ_EMPTY(&asoc->asoc.send_queue) && |
michael@0 | 3986 | TAILQ_EMPTY(&asoc->asoc.sent_queue) && |
michael@0 | 3987 | (asoc->asoc.state & SCTP_STATE_PARTIAL_MSG_LEFT)) { |
michael@0 | 3988 | struct mbuf *op_err; |
michael@0 | 3989 | abort_anyway: |
michael@0 | 3990 | op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), |
michael@0 | 3991 | 0, M_NOWAIT, 1, MT_DATA); |
michael@0 | 3992 | if (op_err) { |
michael@0 | 3993 | /* Fill in the user initiated abort */ |
michael@0 | 3994 | struct sctp_paramhdr *ph; |
michael@0 | 3995 | |
michael@0 | 3996 | SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr); |
michael@0 | 3997 | ph = mtod(op_err, struct sctp_paramhdr *); |
michael@0 | 3998 | ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT); |
michael@0 | 3999 | ph->param_length = htons(SCTP_BUF_LEN(op_err)); |
michael@0 | 4000 | } |
michael@0 | 4001 | asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB+SCTP_LOC_5; |
michael@0 | 4002 | sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED); |
michael@0 | 4003 | SCTP_STAT_INCR_COUNTER32(sctps_aborted); |
michael@0 | 4004 | if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) || |
michael@0 | 4005 | (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { |
michael@0 | 4006 | SCTP_STAT_DECR_GAUGE32(sctps_currestab); |
michael@0 | 4007 | } |
michael@0 | 4008 | if (sctp_free_assoc(inp, asoc, |
michael@0 | 4009 | SCTP_PCBFREE_NOFORCE, |
michael@0 | 4010 | SCTP_FROM_SCTP_PCB+SCTP_LOC_6) == 0) { |
michael@0 | 4011 | cnt_in_sd++; |
michael@0 | 4012 | } |
michael@0 | 4013 | continue; |
michael@0 | 4014 | } else { |
michael@0 | 4015 | sctp_chunk_output(inp, asoc, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED); |
michael@0 | 4016 | } |
michael@0 | 4017 | } |
michael@0 | 4018 | cnt_in_sd++; |
michael@0 | 4019 | SCTP_TCB_UNLOCK(asoc); |
michael@0 | 4020 | } |
michael@0 | 4021 | /* now is there some left in our SHUTDOWN state? */ |
michael@0 | 4022 | if (cnt_in_sd) { |
michael@0 | 4023 | #ifdef SCTP_LOG_CLOSING |
michael@0 | 4024 | sctp_log_closing(inp, NULL, 2); |
michael@0 | 4025 | #endif |
michael@0 | 4026 | inp->sctp_socket = NULL; |
michael@0 | 4027 | SCTP_INP_WUNLOCK(inp); |
michael@0 | 4028 | SCTP_ASOC_CREATE_UNLOCK(inp); |
michael@0 | 4029 | SCTP_INP_INFO_WUNLOCK(); |
michael@0 | 4030 | return; |
michael@0 | 4031 | } |
michael@0 | 4032 | } |
michael@0 | 4033 | inp->sctp_socket = NULL; |
michael@0 | 4034 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) != |
michael@0 | 4035 | SCTP_PCB_FLAGS_UNBOUND) { |
michael@0 | 4036 | /* |
michael@0 | 4037 | * ok, this guy has been bound. It's port is |
michael@0 | 4038 | * somewhere in the SCTP_BASE_INFO(hash table). Remove |
michael@0 | 4039 | * it! |
michael@0 | 4040 | */ |
michael@0 | 4041 | LIST_REMOVE(inp, sctp_hash); |
michael@0 | 4042 | inp->sctp_flags |= SCTP_PCB_FLAGS_UNBOUND; |
michael@0 | 4043 | } |
michael@0 | 4044 | |
michael@0 | 4045 | /* If there is a timer running to kill us, |
michael@0 | 4046 | * forget it, since it may have a contest |
michael@0 | 4047 | * on the INP lock.. which would cause us |
michael@0 | 4048 | * to die ... |
michael@0 | 4049 | */ |
michael@0 | 4050 | cnt = 0; |
michael@0 | 4051 | LIST_FOREACH_SAFE(asoc, &inp->sctp_asoc_list, sctp_tcblist, nasoc) { |
michael@0 | 4052 | SCTP_TCB_LOCK(asoc); |
michael@0 | 4053 | if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { |
michael@0 | 4054 | if (asoc->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE) { |
michael@0 | 4055 | asoc->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE; |
michael@0 | 4056 | sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, asoc, NULL); |
michael@0 | 4057 | } |
michael@0 | 4058 | cnt++; |
michael@0 | 4059 | SCTP_TCB_UNLOCK(asoc); |
michael@0 | 4060 | continue; |
michael@0 | 4061 | } |
michael@0 | 4062 | /* Free associations that are NOT killing us */ |
michael@0 | 4063 | if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_COOKIE_WAIT) && |
michael@0 | 4064 | ((asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0)) { |
michael@0 | 4065 | struct mbuf *op_err; |
michael@0 | 4066 | |
michael@0 | 4067 | op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), |
michael@0 | 4068 | 0, M_NOWAIT, 1, MT_DATA); |
michael@0 | 4069 | if (op_err) { |
michael@0 | 4070 | /* Fill in the user initiated abort */ |
michael@0 | 4071 | struct sctp_paramhdr *ph; |
michael@0 | 4072 | |
michael@0 | 4073 | SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr); |
michael@0 | 4074 | ph = mtod(op_err, struct sctp_paramhdr *); |
michael@0 | 4075 | ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT); |
michael@0 | 4076 | ph->param_length = htons(SCTP_BUF_LEN(op_err)); |
michael@0 | 4077 | } |
michael@0 | 4078 | asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB+SCTP_LOC_7; |
michael@0 | 4079 | sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED); |
michael@0 | 4080 | SCTP_STAT_INCR_COUNTER32(sctps_aborted); |
michael@0 | 4081 | } else if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { |
michael@0 | 4082 | cnt++; |
michael@0 | 4083 | SCTP_TCB_UNLOCK(asoc); |
michael@0 | 4084 | continue; |
michael@0 | 4085 | } |
michael@0 | 4086 | if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) || |
michael@0 | 4087 | (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) { |
michael@0 | 4088 | SCTP_STAT_DECR_GAUGE32(sctps_currestab); |
michael@0 | 4089 | } |
michael@0 | 4090 | if (sctp_free_assoc(inp, asoc, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_PCB+SCTP_LOC_8) == 0) { |
michael@0 | 4091 | cnt++; |
michael@0 | 4092 | } |
michael@0 | 4093 | } |
michael@0 | 4094 | if (cnt) { |
michael@0 | 4095 | /* Ok we have someone out there that will kill us */ |
michael@0 | 4096 | (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer); |
michael@0 | 4097 | #ifdef SCTP_LOG_CLOSING |
michael@0 | 4098 | sctp_log_closing(inp, NULL, 3); |
michael@0 | 4099 | #endif |
michael@0 | 4100 | SCTP_INP_WUNLOCK(inp); |
michael@0 | 4101 | SCTP_ASOC_CREATE_UNLOCK(inp); |
michael@0 | 4102 | SCTP_INP_INFO_WUNLOCK(); |
michael@0 | 4103 | return; |
michael@0 | 4104 | } |
michael@0 | 4105 | if (SCTP_INP_LOCK_CONTENDED(inp)) |
michael@0 | 4106 | being_refed++; |
michael@0 | 4107 | if (SCTP_INP_READ_CONTENDED(inp)) |
michael@0 | 4108 | being_refed++; |
michael@0 | 4109 | if (SCTP_ASOC_CREATE_LOCK_CONTENDED(inp)) |
michael@0 | 4110 | being_refed++; |
michael@0 | 4111 | |
michael@0 | 4112 | if ((inp->refcount) || |
michael@0 | 4113 | (being_refed) || |
michael@0 | 4114 | (inp->sctp_flags & SCTP_PCB_FLAGS_CLOSE_IP)) { |
michael@0 | 4115 | (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer); |
michael@0 | 4116 | #ifdef SCTP_LOG_CLOSING |
michael@0 | 4117 | sctp_log_closing(inp, NULL, 4); |
michael@0 | 4118 | #endif |
michael@0 | 4119 | sctp_timer_start(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL); |
michael@0 | 4120 | SCTP_INP_WUNLOCK(inp); |
michael@0 | 4121 | SCTP_ASOC_CREATE_UNLOCK(inp); |
michael@0 | 4122 | SCTP_INP_INFO_WUNLOCK(); |
michael@0 | 4123 | return; |
michael@0 | 4124 | } |
michael@0 | 4125 | inp->sctp_ep.signature_change.type = 0; |
michael@0 | 4126 | inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_ALLGONE; |
michael@0 | 4127 | /* Remove it from the list .. last thing we need a |
michael@0 | 4128 | * lock for. |
michael@0 | 4129 | */ |
michael@0 | 4130 | LIST_REMOVE(inp, sctp_list); |
michael@0 | 4131 | SCTP_INP_WUNLOCK(inp); |
michael@0 | 4132 | SCTP_ASOC_CREATE_UNLOCK(inp); |
michael@0 | 4133 | SCTP_INP_INFO_WUNLOCK(); |
michael@0 | 4134 | /* Now we release all locks. Since this INP |
michael@0 | 4135 | * cannot be found anymore except possibly by the |
michael@0 | 4136 | * kill timer that might be running. We call |
michael@0 | 4137 | * the drain function here. It should hit the case |
michael@0 | 4138 | * were it sees the ACTIVE flag cleared and exit |
michael@0 | 4139 | * out freeing us to proceed and destroy everything. |
michael@0 | 4140 | */ |
michael@0 | 4141 | if (from != SCTP_CALLED_FROM_INPKILL_TIMER) { |
michael@0 | 4142 | (void)SCTP_OS_TIMER_STOP_DRAIN(&inp->sctp_ep.signature_change.timer); |
michael@0 | 4143 | } else { |
michael@0 | 4144 | /* Probably un-needed */ |
michael@0 | 4145 | (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer); |
michael@0 | 4146 | } |
michael@0 | 4147 | |
michael@0 | 4148 | #ifdef SCTP_LOG_CLOSING |
michael@0 | 4149 | sctp_log_closing(inp, NULL, 5); |
michael@0 | 4150 | #endif |
michael@0 | 4151 | |
michael@0 | 4152 | #if !(defined(__Panda__) || defined(__Windows__) || defined(__Userspace__)) |
michael@0 | 4153 | #if !defined(__FreeBSD__) || __FreeBSD_version < 500000 |
michael@0 | 4154 | rt = ip_pcb->inp_route.ro_rt; |
michael@0 | 4155 | #endif |
michael@0 | 4156 | #endif |
michael@0 | 4157 | |
michael@0 | 4158 | #if defined(__Panda__) |
michael@0 | 4159 | if (inp->pak_to_read) { |
michael@0 | 4160 | (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.zero_copy_timer.timer); |
michael@0 | 4161 | SCTP_RELEASE_PKT(inp->pak_to_read); |
michael@0 | 4162 | inp->pak_to_read = NULL; |
michael@0 | 4163 | } |
michael@0 | 4164 | if (inp->pak_to_read_sendq) { |
michael@0 | 4165 | (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.zero_copy_sendq_timer.timer); |
michael@0 | 4166 | SCTP_RELEASE_PKT(inp->pak_to_read_sendq); |
michael@0 | 4167 | inp->pak_to_read_sendq = NULL; |
michael@0 | 4168 | } |
michael@0 | 4169 | #endif |
michael@0 | 4170 | if ((inp->sctp_asocidhash) != NULL) { |
michael@0 | 4171 | SCTP_HASH_FREE(inp->sctp_asocidhash, inp->hashasocidmark); |
michael@0 | 4172 | inp->sctp_asocidhash = NULL; |
michael@0 | 4173 | } |
michael@0 | 4174 | /*sa_ignore FREED_MEMORY*/ |
michael@0 | 4175 | TAILQ_FOREACH_SAFE(sq, &inp->read_queue, next, nsq) { |
michael@0 | 4176 | /* Its only abandoned if it had data left */ |
michael@0 | 4177 | if (sq->length) |
michael@0 | 4178 | SCTP_STAT_INCR(sctps_left_abandon); |
michael@0 | 4179 | |
michael@0 | 4180 | TAILQ_REMOVE(&inp->read_queue, sq, next); |
michael@0 | 4181 | sctp_free_remote_addr(sq->whoFrom); |
michael@0 | 4182 | if (so) |
michael@0 | 4183 | so->so_rcv.sb_cc -= sq->length; |
michael@0 | 4184 | if (sq->data) { |
michael@0 | 4185 | sctp_m_freem(sq->data); |
michael@0 | 4186 | sq->data = NULL; |
michael@0 | 4187 | } |
michael@0 | 4188 | /* |
michael@0 | 4189 | * no need to free the net count, since at this point all |
michael@0 | 4190 | * assoc's are gone. |
michael@0 | 4191 | */ |
michael@0 | 4192 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), sq); |
michael@0 | 4193 | SCTP_DECR_READQ_COUNT(); |
michael@0 | 4194 | } |
michael@0 | 4195 | /* Now the sctp_pcb things */ |
michael@0 | 4196 | /* |
michael@0 | 4197 | * free each asoc if it is not already closed/free. we can't use the |
michael@0 | 4198 | * macro here since le_next will get freed as part of the |
michael@0 | 4199 | * sctp_free_assoc() call. |
michael@0 | 4200 | */ |
michael@0 | 4201 | if (so) { |
michael@0 | 4202 | #ifdef IPSEC |
michael@0 | 4203 | ipsec_delete_pcbpolicy(ip_pcb); |
michael@0 | 4204 | #endif /* IPSEC */ |
michael@0 | 4205 | |
michael@0 | 4206 | /* Unlocks not needed since the socket is gone now */ |
michael@0 | 4207 | } |
michael@0 | 4208 | #ifndef __Panda__ |
michael@0 | 4209 | if (ip_pcb->inp_options) { |
michael@0 | 4210 | (void)sctp_m_free(ip_pcb->inp_options); |
michael@0 | 4211 | ip_pcb->inp_options = 0; |
michael@0 | 4212 | } |
michael@0 | 4213 | #endif |
michael@0 | 4214 | |
michael@0 | 4215 | #if !(defined(__Panda__) || defined(__Windows__) || defined(__Userspace__)) |
michael@0 | 4216 | #if !defined(__FreeBSD__) || __FreeBSD_version < 500000 |
michael@0 | 4217 | if (rt) { |
michael@0 | 4218 | RTFREE(rt); |
michael@0 | 4219 | ip_pcb->inp_route.ro_rt = 0; |
michael@0 | 4220 | } |
michael@0 | 4221 | #endif |
michael@0 | 4222 | #if defined(__FreeBSD__) && __FreeBSD_version < 803000 |
michael@0 | 4223 | #ifdef INET |
michael@0 | 4224 | if (ip_pcb->inp_moptions) { |
michael@0 | 4225 | inp_freemoptions(ip_pcb->inp_moptions); |
michael@0 | 4226 | ip_pcb->inp_moptions = 0; |
michael@0 | 4227 | } |
michael@0 | 4228 | #endif |
michael@0 | 4229 | #endif |
michael@0 | 4230 | #endif |
michael@0 | 4231 | |
michael@0 | 4232 | #ifdef INET6 |
michael@0 | 4233 | #if !(defined(__Panda__) || defined(__Windows__) || defined(__Userspace__)) |
michael@0 | 4234 | #if defined(__FreeBSD__) || defined(__APPLE__) |
michael@0 | 4235 | if (ip_pcb->inp_vflag & INP_IPV6) { |
michael@0 | 4236 | #else |
michael@0 | 4237 | if (inp->inp_vflag & INP_IPV6) { |
michael@0 | 4238 | #endif |
michael@0 | 4239 | struct in6pcb *in6p; |
michael@0 | 4240 | |
michael@0 | 4241 | in6p = (struct in6pcb *)inp; |
michael@0 | 4242 | ip6_freepcbopts(in6p->in6p_outputopts); |
michael@0 | 4243 | } |
michael@0 | 4244 | #endif |
michael@0 | 4245 | #endif /* INET6 */ |
michael@0 | 4246 | #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)) |
michael@0 | 4247 | inp->inp_vflag = 0; |
michael@0 | 4248 | #else |
michael@0 | 4249 | ip_pcb->inp_vflag = 0; |
michael@0 | 4250 | #endif |
michael@0 | 4251 | /* free up authentication fields */ |
michael@0 | 4252 | if (inp->sctp_ep.local_auth_chunks != NULL) |
michael@0 | 4253 | sctp_free_chunklist(inp->sctp_ep.local_auth_chunks); |
michael@0 | 4254 | if (inp->sctp_ep.local_hmacs != NULL) |
michael@0 | 4255 | sctp_free_hmaclist(inp->sctp_ep.local_hmacs); |
michael@0 | 4256 | |
michael@0 | 4257 | LIST_FOREACH_SAFE(shared_key, &inp->sctp_ep.shared_keys, next, nshared_key) { |
michael@0 | 4258 | LIST_REMOVE(shared_key, next); |
michael@0 | 4259 | sctp_free_sharedkey(shared_key); |
michael@0 | 4260 | /*sa_ignore FREED_MEMORY*/ |
michael@0 | 4261 | } |
michael@0 | 4262 | |
michael@0 | 4263 | #if defined(__APPLE__) |
michael@0 | 4264 | inp->ip_inp.inp.inp_state = INPCB_STATE_DEAD; |
michael@0 | 4265 | if (in_pcb_checkstate(&inp->ip_inp.inp, WNT_STOPUSING, 1) != WNT_STOPUSING) { |
michael@0 | 4266 | #ifdef INVARIANTS |
michael@0 | 4267 | panic("sctp_inpcb_free inp = %p couldn't set to STOPUSING\n", (void *)inp); |
michael@0 | 4268 | #else |
michael@0 | 4269 | SCTP_PRINTF("sctp_inpcb_free inp = %p couldn't set to STOPUSING\n", (void *)inp); |
michael@0 | 4270 | #endif |
michael@0 | 4271 | } |
michael@0 | 4272 | inp->ip_inp.inp.inp_socket->so_flags |= SOF_PCBCLEARING; |
michael@0 | 4273 | #endif |
michael@0 | 4274 | /* |
michael@0 | 4275 | * if we have an address list the following will free the list of |
michael@0 | 4276 | * ifaddr's that are set into this ep. Again macro limitations here, |
michael@0 | 4277 | * since the LIST_FOREACH could be a bad idea. |
michael@0 | 4278 | */ |
michael@0 | 4279 | LIST_FOREACH_SAFE(laddr, &inp->sctp_addr_list, sctp_nxt_addr, nladdr) { |
michael@0 | 4280 | sctp_remove_laddr(laddr); |
michael@0 | 4281 | } |
michael@0 | 4282 | |
michael@0 | 4283 | #ifdef SCTP_TRACK_FREED_ASOCS |
michael@0 | 4284 | /* TEMP CODE */ |
michael@0 | 4285 | LIST_FOREACH_SAFE(asoc, &inp->sctp_asoc_free_list, sctp_tcblist, nasoc) { |
michael@0 | 4286 | LIST_REMOVE(asoc, sctp_tcblist); |
michael@0 | 4287 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), asoc); |
michael@0 | 4288 | SCTP_DECR_ASOC_COUNT(); |
michael@0 | 4289 | } |
michael@0 | 4290 | /* *** END TEMP CODE ****/ |
michael@0 | 4291 | #endif |
michael@0 | 4292 | #ifdef SCTP_MVRF |
michael@0 | 4293 | SCTP_FREE(inp->m_vrf_ids, SCTP_M_MVRF); |
michael@0 | 4294 | #endif |
michael@0 | 4295 | /* Now lets see about freeing the EP hash table. */ |
michael@0 | 4296 | if (inp->sctp_tcbhash != NULL) { |
michael@0 | 4297 | SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark); |
michael@0 | 4298 | inp->sctp_tcbhash = NULL; |
michael@0 | 4299 | } |
michael@0 | 4300 | /* Now we must put the ep memory back into the zone pool */ |
michael@0 | 4301 | #if defined(__FreeBSD__) |
michael@0 | 4302 | INP_LOCK_DESTROY(&inp->ip_inp.inp); |
michael@0 | 4303 | #endif |
michael@0 | 4304 | SCTP_INP_LOCK_DESTROY(inp); |
michael@0 | 4305 | SCTP_INP_READ_DESTROY(inp); |
michael@0 | 4306 | SCTP_ASOC_CREATE_LOCK_DESTROY(inp); |
michael@0 | 4307 | #if !defined(__APPLE__) |
michael@0 | 4308 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp); |
michael@0 | 4309 | SCTP_DECR_EP_COUNT(); |
michael@0 | 4310 | #else |
michael@0 | 4311 | /* For Tiger, we will do this later... */ |
michael@0 | 4312 | #endif |
michael@0 | 4313 | } |
michael@0 | 4314 | |
michael@0 | 4315 | |
michael@0 | 4316 | struct sctp_nets * |
michael@0 | 4317 | sctp_findnet(struct sctp_tcb *stcb, struct sockaddr *addr) |
michael@0 | 4318 | { |
michael@0 | 4319 | struct sctp_nets *net; |
michael@0 | 4320 | /* locate the address */ |
michael@0 | 4321 | TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { |
michael@0 | 4322 | if (sctp_cmpaddr(addr, (struct sockaddr *)&net->ro._l_addr)) |
michael@0 | 4323 | return (net); |
michael@0 | 4324 | } |
michael@0 | 4325 | return (NULL); |
michael@0 | 4326 | } |
michael@0 | 4327 | |
michael@0 | 4328 | |
michael@0 | 4329 | int |
michael@0 | 4330 | sctp_is_address_on_local_host(struct sockaddr *addr, uint32_t vrf_id) |
michael@0 | 4331 | { |
michael@0 | 4332 | #ifdef __Panda__ |
michael@0 | 4333 | return (0); |
michael@0 | 4334 | #else |
michael@0 | 4335 | struct sctp_ifa *sctp_ifa; |
michael@0 | 4336 | sctp_ifa = sctp_find_ifa_by_addr(addr, vrf_id, SCTP_ADDR_NOT_LOCKED); |
michael@0 | 4337 | if (sctp_ifa) { |
michael@0 | 4338 | return (1); |
michael@0 | 4339 | } else { |
michael@0 | 4340 | return (0); |
michael@0 | 4341 | } |
michael@0 | 4342 | #endif |
michael@0 | 4343 | } |
michael@0 | 4344 | |
michael@0 | 4345 | /* |
michael@0 | 4346 | * add's a remote endpoint address, done with the INIT/INIT-ACK as well as |
michael@0 | 4347 | * when a ASCONF arrives that adds it. It will also initialize all the cwnd |
michael@0 | 4348 | * stats of stuff. |
michael@0 | 4349 | */ |
michael@0 | 4350 | int |
michael@0 | 4351 | sctp_add_remote_addr(struct sctp_tcb *stcb, struct sockaddr *newaddr, |
michael@0 | 4352 | struct sctp_nets **netp, int set_scope, int from) |
michael@0 | 4353 | { |
michael@0 | 4354 | /* |
michael@0 | 4355 | * The following is redundant to the same lines in the |
michael@0 | 4356 | * sctp_aloc_assoc() but is needed since others call the add |
michael@0 | 4357 | * address function |
michael@0 | 4358 | */ |
michael@0 | 4359 | struct sctp_nets *net, *netfirst; |
michael@0 | 4360 | int addr_inscope; |
michael@0 | 4361 | |
michael@0 | 4362 | SCTPDBG(SCTP_DEBUG_PCB1, "Adding an address (from:%d) to the peer: ", |
michael@0 | 4363 | from); |
michael@0 | 4364 | SCTPDBG_ADDR(SCTP_DEBUG_PCB1, newaddr); |
michael@0 | 4365 | |
michael@0 | 4366 | netfirst = sctp_findnet(stcb, newaddr); |
michael@0 | 4367 | if (netfirst) { |
michael@0 | 4368 | /* |
michael@0 | 4369 | * Lie and return ok, we don't want to make the association |
michael@0 | 4370 | * go away for this behavior. It will happen in the TCP |
michael@0 | 4371 | * model in a connected socket. It does not reach the hash |
michael@0 | 4372 | * table until after the association is built so it can't be |
michael@0 | 4373 | * found. Mark as reachable, since the initial creation will |
michael@0 | 4374 | * have been cleared and the NOT_IN_ASSOC flag will have |
michael@0 | 4375 | * been added... and we don't want to end up removing it |
michael@0 | 4376 | * back out. |
michael@0 | 4377 | */ |
michael@0 | 4378 | if (netfirst->dest_state & SCTP_ADDR_UNCONFIRMED) { |
michael@0 | 4379 | netfirst->dest_state = (SCTP_ADDR_REACHABLE | |
michael@0 | 4380 | SCTP_ADDR_UNCONFIRMED); |
michael@0 | 4381 | } else { |
michael@0 | 4382 | netfirst->dest_state = SCTP_ADDR_REACHABLE; |
michael@0 | 4383 | } |
michael@0 | 4384 | |
michael@0 | 4385 | return (0); |
michael@0 | 4386 | } |
michael@0 | 4387 | addr_inscope = 1; |
michael@0 | 4388 | switch (newaddr->sa_family) { |
michael@0 | 4389 | #ifdef INET |
michael@0 | 4390 | case AF_INET: |
michael@0 | 4391 | { |
michael@0 | 4392 | struct sockaddr_in *sin; |
michael@0 | 4393 | |
michael@0 | 4394 | sin = (struct sockaddr_in *)newaddr; |
michael@0 | 4395 | if (sin->sin_addr.s_addr == 0) { |
michael@0 | 4396 | /* Invalid address */ |
michael@0 | 4397 | return (-1); |
michael@0 | 4398 | } |
michael@0 | 4399 | /* zero out the bzero area */ |
michael@0 | 4400 | memset(&sin->sin_zero, 0, sizeof(sin->sin_zero)); |
michael@0 | 4401 | |
michael@0 | 4402 | /* assure len is set */ |
michael@0 | 4403 | #ifdef HAVE_SIN_LEN |
michael@0 | 4404 | sin->sin_len = sizeof(struct sockaddr_in); |
michael@0 | 4405 | #endif |
michael@0 | 4406 | if (set_scope) { |
michael@0 | 4407 | #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE |
michael@0 | 4408 | stcb->asoc.scope.ipv4_local_scope = 1; |
michael@0 | 4409 | #else |
michael@0 | 4410 | if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) { |
michael@0 | 4411 | stcb->asoc.scope.ipv4_local_scope = 1; |
michael@0 | 4412 | } |
michael@0 | 4413 | #endif /* SCTP_DONT_DO_PRIVADDR_SCOPE */ |
michael@0 | 4414 | } else { |
michael@0 | 4415 | /* Validate the address is in scope */ |
michael@0 | 4416 | if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) && |
michael@0 | 4417 | (stcb->asoc.scope.ipv4_local_scope == 0)) { |
michael@0 | 4418 | addr_inscope = 0; |
michael@0 | 4419 | } |
michael@0 | 4420 | } |
michael@0 | 4421 | break; |
michael@0 | 4422 | } |
michael@0 | 4423 | #endif |
michael@0 | 4424 | #ifdef INET6 |
michael@0 | 4425 | case AF_INET6: |
michael@0 | 4426 | { |
michael@0 | 4427 | struct sockaddr_in6 *sin6; |
michael@0 | 4428 | |
michael@0 | 4429 | sin6 = (struct sockaddr_in6 *)newaddr; |
michael@0 | 4430 | if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { |
michael@0 | 4431 | /* Invalid address */ |
michael@0 | 4432 | return (-1); |
michael@0 | 4433 | } |
michael@0 | 4434 | /* assure len is set */ |
michael@0 | 4435 | #ifdef HAVE_SIN6_LEN |
michael@0 | 4436 | sin6->sin6_len = sizeof(struct sockaddr_in6); |
michael@0 | 4437 | #endif |
michael@0 | 4438 | if (set_scope) { |
michael@0 | 4439 | if (sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id)) { |
michael@0 | 4440 | stcb->asoc.scope.loopback_scope = 1; |
michael@0 | 4441 | stcb->asoc.scope.local_scope = 0; |
michael@0 | 4442 | stcb->asoc.scope.ipv4_local_scope = 1; |
michael@0 | 4443 | stcb->asoc.scope.site_scope = 1; |
michael@0 | 4444 | } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { |
michael@0 | 4445 | /* |
michael@0 | 4446 | * If the new destination is a LINK_LOCAL we |
michael@0 | 4447 | * must have common site scope. Don't set |
michael@0 | 4448 | * the local scope since we may not share |
michael@0 | 4449 | * all links, only loopback can do this. |
michael@0 | 4450 | * Links on the local network would also be |
michael@0 | 4451 | * on our private network for v4 too. |
michael@0 | 4452 | */ |
michael@0 | 4453 | stcb->asoc.scope.ipv4_local_scope = 1; |
michael@0 | 4454 | stcb->asoc.scope.site_scope = 1; |
michael@0 | 4455 | } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) { |
michael@0 | 4456 | /* |
michael@0 | 4457 | * If the new destination is SITE_LOCAL then |
michael@0 | 4458 | * we must have site scope in common. |
michael@0 | 4459 | */ |
michael@0 | 4460 | stcb->asoc.scope.site_scope = 1; |
michael@0 | 4461 | } |
michael@0 | 4462 | } else { |
michael@0 | 4463 | /* Validate the address is in scope */ |
michael@0 | 4464 | if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) && |
michael@0 | 4465 | (stcb->asoc.scope.loopback_scope == 0)) { |
michael@0 | 4466 | addr_inscope = 0; |
michael@0 | 4467 | } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) && |
michael@0 | 4468 | (stcb->asoc.scope.local_scope == 0)) { |
michael@0 | 4469 | addr_inscope = 0; |
michael@0 | 4470 | } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) && |
michael@0 | 4471 | (stcb->asoc.scope.site_scope == 0)) { |
michael@0 | 4472 | addr_inscope = 0; |
michael@0 | 4473 | } |
michael@0 | 4474 | } |
michael@0 | 4475 | break; |
michael@0 | 4476 | } |
michael@0 | 4477 | #endif |
michael@0 | 4478 | #if defined(__Userspace__) |
michael@0 | 4479 | case AF_CONN: |
michael@0 | 4480 | { |
michael@0 | 4481 | struct sockaddr_conn *sconn; |
michael@0 | 4482 | |
michael@0 | 4483 | sconn = (struct sockaddr_conn *)newaddr; |
michael@0 | 4484 | if (sconn->sconn_addr == NULL) { |
michael@0 | 4485 | /* Invalid address */ |
michael@0 | 4486 | return (-1); |
michael@0 | 4487 | } |
michael@0 | 4488 | #ifdef HAVE_SCONN_LEN |
michael@0 | 4489 | sconn->sconn_len = sizeof(struct sockaddr_conn); |
michael@0 | 4490 | #endif |
michael@0 | 4491 | break; |
michael@0 | 4492 | } |
michael@0 | 4493 | #endif |
michael@0 | 4494 | default: |
michael@0 | 4495 | /* not supported family type */ |
michael@0 | 4496 | return (-1); |
michael@0 | 4497 | } |
michael@0 | 4498 | net = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_net), struct sctp_nets); |
michael@0 | 4499 | if (net == NULL) { |
michael@0 | 4500 | return (-1); |
michael@0 | 4501 | } |
michael@0 | 4502 | SCTP_INCR_RADDR_COUNT(); |
michael@0 | 4503 | bzero(net, sizeof(struct sctp_nets)); |
michael@0 | 4504 | (void)SCTP_GETTIME_TIMEVAL(&net->start_time); |
michael@0 | 4505 | #ifdef HAVE_SA_LEN |
michael@0 | 4506 | memcpy(&net->ro._l_addr, newaddr, newaddr->sa_len); |
michael@0 | 4507 | #endif |
michael@0 | 4508 | switch (newaddr->sa_family) { |
michael@0 | 4509 | #ifdef INET |
michael@0 | 4510 | case AF_INET: |
michael@0 | 4511 | #ifndef HAVE_SA_LEN |
michael@0 | 4512 | memcpy(&net->ro._l_addr, newaddr, sizeof(struct sockaddr_in)); |
michael@0 | 4513 | #endif |
michael@0 | 4514 | ((struct sockaddr_in *)&net->ro._l_addr)->sin_port = stcb->rport; |
michael@0 | 4515 | break; |
michael@0 | 4516 | #endif |
michael@0 | 4517 | #ifdef INET6 |
michael@0 | 4518 | case AF_INET6: |
michael@0 | 4519 | #ifndef HAVE_SA_LEN |
michael@0 | 4520 | memcpy(&net->ro._l_addr, newaddr, sizeof(struct sockaddr_in6)); |
michael@0 | 4521 | #endif |
michael@0 | 4522 | ((struct sockaddr_in6 *)&net->ro._l_addr)->sin6_port = stcb->rport; |
michael@0 | 4523 | break; |
michael@0 | 4524 | #endif |
michael@0 | 4525 | #if defined(__Userspace__) |
michael@0 | 4526 | case AF_CONN: |
michael@0 | 4527 | #ifndef HAVE_SA_LEN |
michael@0 | 4528 | memcpy(&net->ro._l_addr, newaddr, sizeof(struct sockaddr_conn)); |
michael@0 | 4529 | #endif |
michael@0 | 4530 | ((struct sockaddr_conn *)&net->ro._l_addr)->sconn_port = stcb->rport; |
michael@0 | 4531 | break; |
michael@0 | 4532 | #endif |
michael@0 | 4533 | default: |
michael@0 | 4534 | break; |
michael@0 | 4535 | } |
michael@0 | 4536 | net->addr_is_local = sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id); |
michael@0 | 4537 | if (net->addr_is_local && ((set_scope || (from == SCTP_ADDR_IS_CONFIRMED)))) { |
michael@0 | 4538 | stcb->asoc.scope.loopback_scope = 1; |
michael@0 | 4539 | stcb->asoc.scope.ipv4_local_scope = 1; |
michael@0 | 4540 | stcb->asoc.scope.local_scope = 0; |
michael@0 | 4541 | stcb->asoc.scope.site_scope = 1; |
michael@0 | 4542 | addr_inscope = 1; |
michael@0 | 4543 | } |
michael@0 | 4544 | net->failure_threshold = stcb->asoc.def_net_failure; |
michael@0 | 4545 | net->pf_threshold = stcb->asoc.def_net_pf_threshold; |
michael@0 | 4546 | if (addr_inscope == 0) { |
michael@0 | 4547 | net->dest_state = (SCTP_ADDR_REACHABLE | |
michael@0 | 4548 | SCTP_ADDR_OUT_OF_SCOPE); |
michael@0 | 4549 | } else { |
michael@0 | 4550 | if (from == SCTP_ADDR_IS_CONFIRMED) |
michael@0 | 4551 | /* SCTP_ADDR_IS_CONFIRMED is passed by connect_x */ |
michael@0 | 4552 | net->dest_state = SCTP_ADDR_REACHABLE; |
michael@0 | 4553 | else |
michael@0 | 4554 | net->dest_state = SCTP_ADDR_REACHABLE | |
michael@0 | 4555 | SCTP_ADDR_UNCONFIRMED; |
michael@0 | 4556 | } |
michael@0 | 4557 | /* We set this to 0, the timer code knows that |
michael@0 | 4558 | * this means its an initial value |
michael@0 | 4559 | */ |
michael@0 | 4560 | net->rto_needed = 1; |
michael@0 | 4561 | net->RTO = 0; |
michael@0 | 4562 | net->RTO_measured = 0; |
michael@0 | 4563 | stcb->asoc.numnets++; |
michael@0 | 4564 | net->ref_count = 1; |
michael@0 | 4565 | net->cwr_window_tsn = net->last_cwr_tsn = stcb->asoc.sending_seq - 1; |
michael@0 | 4566 | net->port = stcb->asoc.port; |
michael@0 | 4567 | net->dscp = stcb->asoc.default_dscp; |
michael@0 | 4568 | #ifdef INET6 |
michael@0 | 4569 | net->flowlabel = stcb->asoc.default_flowlabel; |
michael@0 | 4570 | #endif |
michael@0 | 4571 | if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) { |
michael@0 | 4572 | net->dest_state |= SCTP_ADDR_NOHB; |
michael@0 | 4573 | } else { |
michael@0 | 4574 | net->dest_state &= ~SCTP_ADDR_NOHB; |
michael@0 | 4575 | } |
michael@0 | 4576 | if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD)) { |
michael@0 | 4577 | net->dest_state |= SCTP_ADDR_NO_PMTUD; |
michael@0 | 4578 | } else { |
michael@0 | 4579 | net->dest_state &= ~SCTP_ADDR_NO_PMTUD; |
michael@0 | 4580 | } |
michael@0 | 4581 | net->heart_beat_delay = stcb->asoc.heart_beat_delay; |
michael@0 | 4582 | /* Init the timer structure */ |
michael@0 | 4583 | SCTP_OS_TIMER_INIT(&net->rxt_timer.timer); |
michael@0 | 4584 | SCTP_OS_TIMER_INIT(&net->pmtu_timer.timer); |
michael@0 | 4585 | SCTP_OS_TIMER_INIT(&net->hb_timer.timer); |
michael@0 | 4586 | |
michael@0 | 4587 | /* Now generate a route for this guy */ |
michael@0 | 4588 | #ifdef INET6 |
michael@0 | 4589 | #ifdef SCTP_EMBEDDED_V6_SCOPE |
michael@0 | 4590 | /* KAME hack: embed scopeid */ |
michael@0 | 4591 | if (newaddr->sa_family == AF_INET6) { |
michael@0 | 4592 | struct sockaddr_in6 *sin6; |
michael@0 | 4593 | |
michael@0 | 4594 | sin6 = (struct sockaddr_in6 *)&net->ro._l_addr; |
michael@0 | 4595 | #if defined(__APPLE__) |
michael@0 | 4596 | #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) |
michael@0 | 4597 | (void)in6_embedscope(&sin6->sin6_addr, sin6, &stcb->sctp_ep->ip_inp.inp, NULL); |
michael@0 | 4598 | #else |
michael@0 | 4599 | (void)in6_embedscope(&sin6->sin6_addr, sin6, &stcb->sctp_ep->ip_inp.inp, NULL, NULL); |
michael@0 | 4600 | #endif |
michael@0 | 4601 | #elif defined(SCTP_KAME) |
michael@0 | 4602 | (void)sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)); |
michael@0 | 4603 | #else |
michael@0 | 4604 | (void)in6_embedscope(&sin6->sin6_addr, sin6); |
michael@0 | 4605 | #endif |
michael@0 | 4606 | #ifndef SCOPEDROUTING |
michael@0 | 4607 | sin6->sin6_scope_id = 0; |
michael@0 | 4608 | #endif |
michael@0 | 4609 | } |
michael@0 | 4610 | #endif /* SCTP_EMBEDDED_V6_SCOPE */ |
michael@0 | 4611 | #endif |
michael@0 | 4612 | SCTP_RTALLOC((sctp_route_t *)&net->ro, stcb->asoc.vrf_id); |
michael@0 | 4613 | |
michael@0 | 4614 | #if !defined(__Userspace__) |
michael@0 | 4615 | if (SCTP_ROUTE_HAS_VALID_IFN(&net->ro)) { |
michael@0 | 4616 | /* Get source address */ |
michael@0 | 4617 | net->ro._s_addr = sctp_source_address_selection(stcb->sctp_ep, |
michael@0 | 4618 | stcb, |
michael@0 | 4619 | (sctp_route_t *)&net->ro, |
michael@0 | 4620 | net, |
michael@0 | 4621 | 0, |
michael@0 | 4622 | stcb->asoc.vrf_id); |
michael@0 | 4623 | /* Now get the interface MTU */ |
michael@0 | 4624 | if (net->ro._s_addr && net->ro._s_addr->ifn_p) { |
michael@0 | 4625 | net->mtu = SCTP_GATHER_MTU_FROM_INTFC(net->ro._s_addr->ifn_p); |
michael@0 | 4626 | } |
michael@0 | 4627 | if (net->mtu > 0) { |
michael@0 | 4628 | uint32_t rmtu; |
michael@0 | 4629 | |
michael@0 | 4630 | rmtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, net->ro.ro_rt); |
michael@0 | 4631 | if (rmtu == 0) { |
michael@0 | 4632 | /* Start things off to match mtu of interface please. */ |
michael@0 | 4633 | SCTP_SET_MTU_OF_ROUTE(&net->ro._l_addr.sa, |
michael@0 | 4634 | net->ro.ro_rt, net->mtu); |
michael@0 | 4635 | } else { |
michael@0 | 4636 | /* we take the route mtu over the interface, since |
michael@0 | 4637 | * the route may be leading out the loopback, or |
michael@0 | 4638 | * a different interface. |
michael@0 | 4639 | */ |
michael@0 | 4640 | net->mtu = rmtu; |
michael@0 | 4641 | } |
michael@0 | 4642 | } |
michael@0 | 4643 | } |
michael@0 | 4644 | #endif |
michael@0 | 4645 | if (net->mtu == 0) { |
michael@0 | 4646 | switch (newaddr->sa_family) { |
michael@0 | 4647 | #ifdef INET |
michael@0 | 4648 | case AF_INET: |
michael@0 | 4649 | net->mtu = SCTP_DEFAULT_MTU; |
michael@0 | 4650 | break; |
michael@0 | 4651 | #endif |
michael@0 | 4652 | #ifdef INET6 |
michael@0 | 4653 | case AF_INET6: |
michael@0 | 4654 | net->mtu = 1280; |
michael@0 | 4655 | break; |
michael@0 | 4656 | #endif |
michael@0 | 4657 | #if defined(__Userspace__) |
michael@0 | 4658 | case AF_CONN: |
michael@0 | 4659 | net->mtu = 1280; |
michael@0 | 4660 | break; |
michael@0 | 4661 | #endif |
michael@0 | 4662 | default: |
michael@0 | 4663 | break; |
michael@0 | 4664 | } |
michael@0 | 4665 | } |
michael@0 | 4666 | if (net->port) { |
michael@0 | 4667 | net->mtu -= (uint32_t)sizeof(struct udphdr); |
michael@0 | 4668 | } |
michael@0 | 4669 | if (from == SCTP_ALLOC_ASOC) { |
michael@0 | 4670 | stcb->asoc.smallest_mtu = net->mtu; |
michael@0 | 4671 | } |
michael@0 | 4672 | if (stcb->asoc.smallest_mtu > net->mtu) { |
michael@0 | 4673 | stcb->asoc.smallest_mtu = net->mtu; |
michael@0 | 4674 | } |
michael@0 | 4675 | #ifdef INET6 |
michael@0 | 4676 | #ifdef SCTP_EMBEDDED_V6_SCOPE |
michael@0 | 4677 | if (newaddr->sa_family == AF_INET6) { |
michael@0 | 4678 | struct sockaddr_in6 *sin6; |
michael@0 | 4679 | |
michael@0 | 4680 | sin6 = (struct sockaddr_in6 *)&net->ro._l_addr; |
michael@0 | 4681 | #ifdef SCTP_KAME |
michael@0 | 4682 | (void)sa6_recoverscope(sin6); |
michael@0 | 4683 | #else |
michael@0 | 4684 | (void)in6_recoverscope(sin6, &sin6->sin6_addr, NULL); |
michael@0 | 4685 | #endif /* SCTP_KAME */ |
michael@0 | 4686 | } |
michael@0 | 4687 | #endif /* SCTP_EMBEDDED_V6_SCOPE */ |
michael@0 | 4688 | #endif |
michael@0 | 4689 | |
michael@0 | 4690 | /* JRS - Use the congestion control given in the CC module */ |
michael@0 | 4691 | if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL) |
michael@0 | 4692 | (*stcb->asoc.cc_functions.sctp_set_initial_cc_param)(stcb, net); |
michael@0 | 4693 | |
michael@0 | 4694 | /* |
michael@0 | 4695 | * CMT: CUC algo - set find_pseudo_cumack to TRUE (1) at beginning |
michael@0 | 4696 | * of assoc (2005/06/27, iyengar@cis.udel.edu) |
michael@0 | 4697 | */ |
michael@0 | 4698 | net->find_pseudo_cumack = 1; |
michael@0 | 4699 | net->find_rtx_pseudo_cumack = 1; |
michael@0 | 4700 | net->src_addr_selected = 0; |
michael@0 | 4701 | #if defined(__FreeBSD__) |
michael@0 | 4702 | /* Choose an initial flowid. */ |
michael@0 | 4703 | net->flowid = stcb->asoc.my_vtag ^ |
michael@0 | 4704 | ntohs(stcb->rport) ^ |
michael@0 | 4705 | ntohs(stcb->sctp_ep->sctp_lport); |
michael@0 | 4706 | #ifdef INVARIANTS |
michael@0 | 4707 | net->flowidset = 1; |
michael@0 | 4708 | #endif |
michael@0 | 4709 | #endif |
michael@0 | 4710 | if (netp) { |
michael@0 | 4711 | *netp = net; |
michael@0 | 4712 | } |
michael@0 | 4713 | netfirst = TAILQ_FIRST(&stcb->asoc.nets); |
michael@0 | 4714 | if (net->ro.ro_rt == NULL) { |
michael@0 | 4715 | /* Since we have no route put it at the back */ |
michael@0 | 4716 | TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next); |
michael@0 | 4717 | } else if (netfirst == NULL) { |
michael@0 | 4718 | /* We are the first one in the pool. */ |
michael@0 | 4719 | TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next); |
michael@0 | 4720 | } else if (netfirst->ro.ro_rt == NULL) { |
michael@0 | 4721 | /* |
michael@0 | 4722 | * First one has NO route. Place this one ahead of the first |
michael@0 | 4723 | * one. |
michael@0 | 4724 | */ |
michael@0 | 4725 | TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next); |
michael@0 | 4726 | #ifndef __Panda__ |
michael@0 | 4727 | } else if (net->ro.ro_rt->rt_ifp != netfirst->ro.ro_rt->rt_ifp) { |
michael@0 | 4728 | /* |
michael@0 | 4729 | * This one has a different interface than the one at the |
michael@0 | 4730 | * top of the list. Place it ahead. |
michael@0 | 4731 | */ |
michael@0 | 4732 | TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next); |
michael@0 | 4733 | #endif |
michael@0 | 4734 | } else { |
michael@0 | 4735 | /* |
michael@0 | 4736 | * Ok we have the same interface as the first one. Move |
michael@0 | 4737 | * forward until we find either a) one with a NULL route... |
michael@0 | 4738 | * insert ahead of that b) one with a different ifp.. insert |
michael@0 | 4739 | * after that. c) end of the list.. insert at the tail. |
michael@0 | 4740 | */ |
michael@0 | 4741 | struct sctp_nets *netlook; |
michael@0 | 4742 | |
michael@0 | 4743 | do { |
michael@0 | 4744 | netlook = TAILQ_NEXT(netfirst, sctp_next); |
michael@0 | 4745 | if (netlook == NULL) { |
michael@0 | 4746 | /* End of the list */ |
michael@0 | 4747 | TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next); |
michael@0 | 4748 | break; |
michael@0 | 4749 | } else if (netlook->ro.ro_rt == NULL) { |
michael@0 | 4750 | /* next one has NO route */ |
michael@0 | 4751 | TAILQ_INSERT_BEFORE(netfirst, net, sctp_next); |
michael@0 | 4752 | break; |
michael@0 | 4753 | } |
michael@0 | 4754 | #ifndef __Panda__ |
michael@0 | 4755 | else if (netlook->ro.ro_rt->rt_ifp != net->ro.ro_rt->rt_ifp) |
michael@0 | 4756 | #else |
michael@0 | 4757 | else |
michael@0 | 4758 | #endif |
michael@0 | 4759 | { |
michael@0 | 4760 | TAILQ_INSERT_AFTER(&stcb->asoc.nets, netlook, |
michael@0 | 4761 | net, sctp_next); |
michael@0 | 4762 | break; |
michael@0 | 4763 | } |
michael@0 | 4764 | #ifndef __Panda__ |
michael@0 | 4765 | /* Shift forward */ |
michael@0 | 4766 | netfirst = netlook; |
michael@0 | 4767 | #endif |
michael@0 | 4768 | } while (netlook != NULL); |
michael@0 | 4769 | } |
michael@0 | 4770 | |
michael@0 | 4771 | /* got to have a primary set */ |
michael@0 | 4772 | if (stcb->asoc.primary_destination == 0) { |
michael@0 | 4773 | stcb->asoc.primary_destination = net; |
michael@0 | 4774 | } else if ((stcb->asoc.primary_destination->ro.ro_rt == NULL) && |
michael@0 | 4775 | (net->ro.ro_rt) && |
michael@0 | 4776 | ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) { |
michael@0 | 4777 | /* No route to current primary adopt new primary */ |
michael@0 | 4778 | stcb->asoc.primary_destination = net; |
michael@0 | 4779 | } |
michael@0 | 4780 | /* Validate primary is first */ |
michael@0 | 4781 | net = TAILQ_FIRST(&stcb->asoc.nets); |
michael@0 | 4782 | if ((net != stcb->asoc.primary_destination) && |
michael@0 | 4783 | (stcb->asoc.primary_destination)) { |
michael@0 | 4784 | /* first one on the list is NOT the primary |
michael@0 | 4785 | * sctp_cmpaddr() is much more efficient if |
michael@0 | 4786 | * the primary is the first on the list, make it |
michael@0 | 4787 | * so. |
michael@0 | 4788 | */ |
michael@0 | 4789 | TAILQ_REMOVE(&stcb->asoc.nets, |
michael@0 | 4790 | stcb->asoc.primary_destination, sctp_next); |
michael@0 | 4791 | TAILQ_INSERT_HEAD(&stcb->asoc.nets, |
michael@0 | 4792 | stcb->asoc.primary_destination, sctp_next); |
michael@0 | 4793 | } |
michael@0 | 4794 | return (0); |
michael@0 | 4795 | } |
michael@0 | 4796 | |
michael@0 | 4797 | |
michael@0 | 4798 | static uint32_t |
michael@0 | 4799 | sctp_aloc_a_assoc_id(struct sctp_inpcb *inp, struct sctp_tcb *stcb) |
michael@0 | 4800 | { |
michael@0 | 4801 | uint32_t id; |
michael@0 | 4802 | struct sctpasochead *head; |
michael@0 | 4803 | struct sctp_tcb *lstcb; |
michael@0 | 4804 | |
michael@0 | 4805 | SCTP_INP_WLOCK(inp); |
michael@0 | 4806 | try_again: |
michael@0 | 4807 | if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
michael@0 | 4808 | /* TSNH */ |
michael@0 | 4809 | SCTP_INP_WUNLOCK(inp); |
michael@0 | 4810 | return (0); |
michael@0 | 4811 | } |
michael@0 | 4812 | /* |
michael@0 | 4813 | * We don't allow assoc id to be one of SCTP_FUTURE_ASSOC, |
michael@0 | 4814 | * SCTP_CURRENT_ASSOC and SCTP_ALL_ASSOC. |
michael@0 | 4815 | */ |
michael@0 | 4816 | if (inp->sctp_associd_counter <= SCTP_ALL_ASSOC) { |
michael@0 | 4817 | inp->sctp_associd_counter = SCTP_ALL_ASSOC + 1; |
michael@0 | 4818 | } |
michael@0 | 4819 | id = inp->sctp_associd_counter; |
michael@0 | 4820 | inp->sctp_associd_counter++; |
michael@0 | 4821 | lstcb = sctp_findasoc_ep_asocid_locked(inp, (sctp_assoc_t)id, 0); |
michael@0 | 4822 | if (lstcb) { |
michael@0 | 4823 | goto try_again; |
michael@0 | 4824 | } |
michael@0 | 4825 | head = &inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(id, inp->hashasocidmark)]; |
michael@0 | 4826 | LIST_INSERT_HEAD(head, stcb, sctp_tcbasocidhash); |
michael@0 | 4827 | stcb->asoc.in_asocid_hash = 1; |
michael@0 | 4828 | SCTP_INP_WUNLOCK(inp); |
michael@0 | 4829 | return id; |
michael@0 | 4830 | } |
michael@0 | 4831 | |
michael@0 | 4832 | /* |
michael@0 | 4833 | * allocate an association and add it to the endpoint. The caller must be |
michael@0 | 4834 | * careful to add all additional addresses once they are know right away or |
michael@0 | 4835 | * else the assoc will be may experience a blackout scenario. |
michael@0 | 4836 | */ |
michael@0 | 4837 | struct sctp_tcb * |
michael@0 | 4838 | sctp_aloc_assoc(struct sctp_inpcb *inp, struct sockaddr *firstaddr, |
michael@0 | 4839 | int *error, uint32_t override_tag, uint32_t vrf_id, |
michael@0 | 4840 | #if defined(__FreeBSD__) && __FreeBSD_version >= 500000 |
michael@0 | 4841 | struct thread *p |
michael@0 | 4842 | #elif defined(__Windows__) |
michael@0 | 4843 | PKTHREAD p |
michael@0 | 4844 | #else |
michael@0 | 4845 | #if defined(__Userspace__) |
michael@0 | 4846 | /* __Userspace__ NULL proc is going to be passed here. See sctp_lower_sosend */ |
michael@0 | 4847 | #endif |
michael@0 | 4848 | struct proc *p |
michael@0 | 4849 | #endif |
michael@0 | 4850 | ) |
michael@0 | 4851 | { |
michael@0 | 4852 | /* note the p argument is only valid in unbound sockets */ |
michael@0 | 4853 | |
michael@0 | 4854 | struct sctp_tcb *stcb; |
michael@0 | 4855 | struct sctp_association *asoc; |
michael@0 | 4856 | struct sctpasochead *head; |
michael@0 | 4857 | uint16_t rport; |
michael@0 | 4858 | int err; |
michael@0 | 4859 | |
michael@0 | 4860 | /* |
michael@0 | 4861 | * Assumption made here: Caller has done a |
michael@0 | 4862 | * sctp_findassociation_ep_addr(ep, addr's); to make sure the |
michael@0 | 4863 | * address does not exist already. |
michael@0 | 4864 | */ |
michael@0 | 4865 | if (SCTP_BASE_INFO(ipi_count_asoc) >= SCTP_MAX_NUM_OF_ASOC) { |
michael@0 | 4866 | /* Hit max assoc, sorry no more */ |
michael@0 | 4867 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS); |
michael@0 | 4868 | *error = ENOBUFS; |
michael@0 | 4869 | return (NULL); |
michael@0 | 4870 | } |
michael@0 | 4871 | if (firstaddr == NULL) { |
michael@0 | 4872 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
michael@0 | 4873 | *error = EINVAL; |
michael@0 | 4874 | return (NULL); |
michael@0 | 4875 | } |
michael@0 | 4876 | SCTP_INP_RLOCK(inp); |
michael@0 | 4877 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) && |
michael@0 | 4878 | ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) || |
michael@0 | 4879 | (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) { |
michael@0 | 4880 | /* |
michael@0 | 4881 | * If its in the TCP pool, its NOT allowed to create an |
michael@0 | 4882 | * association. The parent listener needs to call |
michael@0 | 4883 | * sctp_aloc_assoc.. or the one-2-many socket. If a peeled |
michael@0 | 4884 | * off, or connected one does this.. its an error. |
michael@0 | 4885 | */ |
michael@0 | 4886 | SCTP_INP_RUNLOCK(inp); |
michael@0 | 4887 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
michael@0 | 4888 | *error = EINVAL; |
michael@0 | 4889 | return (NULL); |
michael@0 | 4890 | } |
michael@0 | 4891 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) || |
michael@0 | 4892 | (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) { |
michael@0 | 4893 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) || |
michael@0 | 4894 | (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED)) { |
michael@0 | 4895 | SCTP_INP_RUNLOCK(inp); |
michael@0 | 4896 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
michael@0 | 4897 | *error = EINVAL; |
michael@0 | 4898 | return (NULL); |
michael@0 | 4899 | } |
michael@0 | 4900 | } |
michael@0 | 4901 | SCTPDBG(SCTP_DEBUG_PCB3, "Allocate an association for peer:"); |
michael@0 | 4902 | #ifdef SCTP_DEBUG |
michael@0 | 4903 | if (firstaddr) { |
michael@0 | 4904 | SCTPDBG_ADDR(SCTP_DEBUG_PCB3, firstaddr); |
michael@0 | 4905 | switch (firstaddr->sa_family) { |
michael@0 | 4906 | #ifdef INET |
michael@0 | 4907 | case AF_INET: |
michael@0 | 4908 | SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n", |
michael@0 | 4909 | ntohs(((struct sockaddr_in *)firstaddr)->sin_port)); |
michael@0 | 4910 | break; |
michael@0 | 4911 | #endif |
michael@0 | 4912 | #ifdef INET6 |
michael@0 | 4913 | case AF_INET6: |
michael@0 | 4914 | SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n", |
michael@0 | 4915 | ntohs(((struct sockaddr_in6 *)firstaddr)->sin6_port)); |
michael@0 | 4916 | break; |
michael@0 | 4917 | #endif |
michael@0 | 4918 | #if defined(__Userspace__) |
michael@0 | 4919 | case AF_CONN: |
michael@0 | 4920 | SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n", |
michael@0 | 4921 | ntohs(((struct sockaddr_conn *)firstaddr)->sconn_port)); |
michael@0 | 4922 | break; |
michael@0 | 4923 | #endif |
michael@0 | 4924 | default: |
michael@0 | 4925 | break; |
michael@0 | 4926 | } |
michael@0 | 4927 | } else { |
michael@0 | 4928 | SCTPDBG(SCTP_DEBUG_PCB3,"None\n"); |
michael@0 | 4929 | } |
michael@0 | 4930 | #endif /* SCTP_DEBUG */ |
michael@0 | 4931 | switch (firstaddr->sa_family) { |
michael@0 | 4932 | #ifdef INET |
michael@0 | 4933 | case AF_INET: |
michael@0 | 4934 | { |
michael@0 | 4935 | struct sockaddr_in *sin; |
michael@0 | 4936 | |
michael@0 | 4937 | sin = (struct sockaddr_in *)firstaddr; |
michael@0 | 4938 | if ((ntohs(sin->sin_port) == 0) || |
michael@0 | 4939 | (sin->sin_addr.s_addr == INADDR_ANY) || |
michael@0 | 4940 | (sin->sin_addr.s_addr == INADDR_BROADCAST) || |
michael@0 | 4941 | IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) { |
michael@0 | 4942 | /* Invalid address */ |
michael@0 | 4943 | SCTP_INP_RUNLOCK(inp); |
michael@0 | 4944 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
michael@0 | 4945 | *error = EINVAL; |
michael@0 | 4946 | return (NULL); |
michael@0 | 4947 | } |
michael@0 | 4948 | rport = sin->sin_port; |
michael@0 | 4949 | break; |
michael@0 | 4950 | } |
michael@0 | 4951 | #endif |
michael@0 | 4952 | #ifdef INET6 |
michael@0 | 4953 | case AF_INET6: |
michael@0 | 4954 | { |
michael@0 | 4955 | struct sockaddr_in6 *sin6; |
michael@0 | 4956 | |
michael@0 | 4957 | sin6 = (struct sockaddr_in6 *)firstaddr; |
michael@0 | 4958 | if ((ntohs(sin6->sin6_port) == 0) || |
michael@0 | 4959 | IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) || |
michael@0 | 4960 | IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { |
michael@0 | 4961 | /* Invalid address */ |
michael@0 | 4962 | SCTP_INP_RUNLOCK(inp); |
michael@0 | 4963 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
michael@0 | 4964 | *error = EINVAL; |
michael@0 | 4965 | return (NULL); |
michael@0 | 4966 | } |
michael@0 | 4967 | rport = sin6->sin6_port; |
michael@0 | 4968 | break; |
michael@0 | 4969 | } |
michael@0 | 4970 | #endif |
michael@0 | 4971 | #if defined(__Userspace__) |
michael@0 | 4972 | case AF_CONN: |
michael@0 | 4973 | { |
michael@0 | 4974 | struct sockaddr_conn *sconn; |
michael@0 | 4975 | |
michael@0 | 4976 | sconn = (struct sockaddr_conn *)firstaddr; |
michael@0 | 4977 | if ((ntohs(sconn->sconn_port) == 0) || |
michael@0 | 4978 | (sconn->sconn_addr == NULL)) { |
michael@0 | 4979 | /* Invalid address */ |
michael@0 | 4980 | SCTP_INP_RUNLOCK(inp); |
michael@0 | 4981 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
michael@0 | 4982 | *error = EINVAL; |
michael@0 | 4983 | return (NULL); |
michael@0 | 4984 | } |
michael@0 | 4985 | rport = sconn->sconn_port; |
michael@0 | 4986 | break; |
michael@0 | 4987 | } |
michael@0 | 4988 | #endif |
michael@0 | 4989 | default: |
michael@0 | 4990 | /* not supported family type */ |
michael@0 | 4991 | SCTP_INP_RUNLOCK(inp); |
michael@0 | 4992 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
michael@0 | 4993 | *error = EINVAL; |
michael@0 | 4994 | return (NULL); |
michael@0 | 4995 | } |
michael@0 | 4996 | SCTP_INP_RUNLOCK(inp); |
michael@0 | 4997 | if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) { |
michael@0 | 4998 | /* |
michael@0 | 4999 | * If you have not performed a bind, then we need to do the |
michael@0 | 5000 | * ephemeral bind for you. |
michael@0 | 5001 | */ |
michael@0 | 5002 | if ((err = sctp_inpcb_bind(inp->sctp_socket, |
michael@0 | 5003 | (struct sockaddr *)NULL, |
michael@0 | 5004 | (struct sctp_ifa *)NULL, |
michael@0 | 5005 | #ifndef __Panda__ |
michael@0 | 5006 | p |
michael@0 | 5007 | #else |
michael@0 | 5008 | (struct proc *)NULL |
michael@0 | 5009 | #endif |
michael@0 | 5010 | ))) { |
michael@0 | 5011 | /* bind error, probably perm */ |
michael@0 | 5012 | *error = err; |
michael@0 | 5013 | return (NULL); |
michael@0 | 5014 | } |
michael@0 | 5015 | } |
michael@0 | 5016 | stcb = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_asoc), struct sctp_tcb); |
michael@0 | 5017 | if (stcb == NULL) { |
michael@0 | 5018 | /* out of memory? */ |
michael@0 | 5019 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM); |
michael@0 | 5020 | *error = ENOMEM; |
michael@0 | 5021 | return (NULL); |
michael@0 | 5022 | } |
michael@0 | 5023 | SCTP_INCR_ASOC_COUNT(); |
michael@0 | 5024 | |
michael@0 | 5025 | bzero(stcb, sizeof(*stcb)); |
michael@0 | 5026 | asoc = &stcb->asoc; |
michael@0 | 5027 | |
michael@0 | 5028 | asoc->assoc_id = sctp_aloc_a_assoc_id(inp, stcb); |
michael@0 | 5029 | SCTP_TCB_LOCK_INIT(stcb); |
michael@0 | 5030 | SCTP_TCB_SEND_LOCK_INIT(stcb); |
michael@0 | 5031 | stcb->rport = rport; |
michael@0 | 5032 | /* setup back pointer's */ |
michael@0 | 5033 | stcb->sctp_ep = inp; |
michael@0 | 5034 | stcb->sctp_socket = inp->sctp_socket; |
michael@0 | 5035 | if ((err = sctp_init_asoc(inp, stcb, override_tag, vrf_id))) { |
michael@0 | 5036 | /* failed */ |
michael@0 | 5037 | SCTP_TCB_LOCK_DESTROY(stcb); |
michael@0 | 5038 | SCTP_TCB_SEND_LOCK_DESTROY(stcb); |
michael@0 | 5039 | LIST_REMOVE(stcb, sctp_tcbasocidhash); |
michael@0 | 5040 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb); |
michael@0 | 5041 | SCTP_DECR_ASOC_COUNT(); |
michael@0 | 5042 | *error = err; |
michael@0 | 5043 | return (NULL); |
michael@0 | 5044 | } |
michael@0 | 5045 | /* and the port */ |
michael@0 | 5046 | SCTP_INP_INFO_WLOCK(); |
michael@0 | 5047 | SCTP_INP_WLOCK(inp); |
michael@0 | 5048 | if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_SOCKET_ALLGONE)) { |
michael@0 | 5049 | /* inpcb freed while alloc going on */ |
michael@0 | 5050 | SCTP_TCB_LOCK_DESTROY(stcb); |
michael@0 | 5051 | SCTP_TCB_SEND_LOCK_DESTROY(stcb); |
michael@0 | 5052 | LIST_REMOVE(stcb, sctp_tcbasocidhash); |
michael@0 | 5053 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb); |
michael@0 | 5054 | SCTP_INP_WUNLOCK(inp); |
michael@0 | 5055 | SCTP_INP_INFO_WUNLOCK(); |
michael@0 | 5056 | SCTP_DECR_ASOC_COUNT(); |
michael@0 | 5057 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
michael@0 | 5058 | *error = EINVAL; |
michael@0 | 5059 | return (NULL); |
michael@0 | 5060 | } |
michael@0 | 5061 | SCTP_TCB_LOCK(stcb); |
michael@0 | 5062 | |
michael@0 | 5063 | /* now that my_vtag is set, add it to the hash */ |
michael@0 | 5064 | head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))]; |
michael@0 | 5065 | /* put it in the bucket in the vtag hash of assoc's for the system */ |
michael@0 | 5066 | LIST_INSERT_HEAD(head, stcb, sctp_asocs); |
michael@0 | 5067 | SCTP_INP_INFO_WUNLOCK(); |
michael@0 | 5068 | |
michael@0 | 5069 | if ((err = sctp_add_remote_addr(stcb, firstaddr, NULL, SCTP_DO_SETSCOPE, SCTP_ALLOC_ASOC))) { |
michael@0 | 5070 | /* failure.. memory error? */ |
michael@0 | 5071 | if (asoc->strmout) { |
michael@0 | 5072 | SCTP_FREE(asoc->strmout, SCTP_M_STRMO); |
michael@0 | 5073 | asoc->strmout = NULL; |
michael@0 | 5074 | } |
michael@0 | 5075 | if (asoc->mapping_array) { |
michael@0 | 5076 | SCTP_FREE(asoc->mapping_array, SCTP_M_MAP); |
michael@0 | 5077 | asoc->mapping_array = NULL; |
michael@0 | 5078 | } |
michael@0 | 5079 | if (asoc->nr_mapping_array) { |
michael@0 | 5080 | SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP); |
michael@0 | 5081 | asoc->nr_mapping_array = NULL; |
michael@0 | 5082 | } |
michael@0 | 5083 | SCTP_DECR_ASOC_COUNT(); |
michael@0 | 5084 | SCTP_TCB_UNLOCK(stcb); |
michael@0 | 5085 | SCTP_TCB_LOCK_DESTROY(stcb); |
michael@0 | 5086 | SCTP_TCB_SEND_LOCK_DESTROY(stcb); |
michael@0 | 5087 | LIST_REMOVE(stcb, sctp_tcbasocidhash); |
michael@0 | 5088 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb); |
michael@0 | 5089 | SCTP_INP_WUNLOCK(inp); |
michael@0 | 5090 | SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS); |
michael@0 | 5091 | *error = ENOBUFS; |
michael@0 | 5092 | return (NULL); |
michael@0 | 5093 | } |
michael@0 | 5094 | /* Init all the timers */ |
michael@0 | 5095 | SCTP_OS_TIMER_INIT(&asoc->dack_timer.timer); |
michael@0 | 5096 | SCTP_OS_TIMER_INIT(&asoc->strreset_timer.timer); |
michael@0 | 5097 | SCTP_OS_TIMER_INIT(&asoc->asconf_timer.timer); |
michael@0 | 5098 | SCTP_OS_TIMER_INIT(&asoc->shut_guard_timer.timer); |
michael@0 | 5099 | SCTP_OS_TIMER_INIT(&asoc->autoclose_timer.timer); |
michael@0 | 5100 | SCTP_OS_TIMER_INIT(&asoc->delayed_event_timer.timer); |
michael@0 | 5101 | SCTP_OS_TIMER_INIT(&asoc->delete_prim_timer.timer); |
michael@0 | 5102 | |
michael@0 | 5103 | LIST_INSERT_HEAD(&inp->sctp_asoc_list, stcb, sctp_tcblist); |
michael@0 | 5104 | /* now file the port under the hash as well */ |
michael@0 | 5105 | if (inp->sctp_tcbhash != NULL) { |
michael@0 | 5106 | head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(stcb->rport, |
michael@0 | 5107 | inp->sctp_hashmark)]; |
michael@0 | 5108 | LIST_INSERT_HEAD(head, stcb, sctp_tcbhash); |
michael@0 | 5109 | } |
michael@0 | 5110 | SCTP_INP_WUNLOCK(inp); |
michael@0 | 5111 | SCTPDBG(SCTP_DEBUG_PCB1, "Association %p now allocated\n", (void *)stcb); |
michael@0 | 5112 | return (stcb); |
michael@0 | 5113 | } |
michael@0 | 5114 | |
michael@0 | 5115 | |
michael@0 | 5116 | void |
michael@0 | 5117 | sctp_remove_net(struct sctp_tcb *stcb, struct sctp_nets *net) |
michael@0 | 5118 | { |
michael@0 | 5119 | struct sctp_association *asoc; |
michael@0 | 5120 | |
michael@0 | 5121 | asoc = &stcb->asoc; |
michael@0 | 5122 | asoc->numnets--; |
michael@0 | 5123 | TAILQ_REMOVE(&asoc->nets, net, sctp_next); |
michael@0 | 5124 | if (net == asoc->primary_destination) { |
michael@0 | 5125 | /* Reset primary */ |
michael@0 | 5126 | struct sctp_nets *lnet; |
michael@0 | 5127 | |
michael@0 | 5128 | lnet = TAILQ_FIRST(&asoc->nets); |
michael@0 | 5129 | /* Mobility adaptation |
michael@0 | 5130 | Ideally, if deleted destination is the primary, it becomes |
michael@0 | 5131 | a fast retransmission trigger by the subsequent SET PRIMARY. |
michael@0 | 5132 | (by micchie) |
michael@0 | 5133 | */ |
michael@0 | 5134 | if (sctp_is_mobility_feature_on(stcb->sctp_ep, |
michael@0 | 5135 | SCTP_MOBILITY_BASE) || |
michael@0 | 5136 | sctp_is_mobility_feature_on(stcb->sctp_ep, |
michael@0 | 5137 | SCTP_MOBILITY_FASTHANDOFF)) { |
michael@0 | 5138 | SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: primary dst is deleting\n"); |
michael@0 | 5139 | if (asoc->deleted_primary != NULL) { |
michael@0 | 5140 | SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: deleted primary may be already stored\n"); |
michael@0 | 5141 | goto out; |
michael@0 | 5142 | } |
michael@0 | 5143 | asoc->deleted_primary = net; |
michael@0 | 5144 | atomic_add_int(&net->ref_count, 1); |
michael@0 | 5145 | memset(&net->lastsa, 0, sizeof(net->lastsa)); |
michael@0 | 5146 | memset(&net->lastsv, 0, sizeof(net->lastsv)); |
michael@0 | 5147 | sctp_mobility_feature_on(stcb->sctp_ep, |
michael@0 | 5148 | SCTP_MOBILITY_PRIM_DELETED); |
michael@0 | 5149 | sctp_timer_start(SCTP_TIMER_TYPE_PRIM_DELETED, |
michael@0 | 5150 | stcb->sctp_ep, stcb, NULL); |
michael@0 | 5151 | } |
michael@0 | 5152 | out: |
michael@0 | 5153 | /* Try to find a confirmed primary */ |
michael@0 | 5154 | asoc->primary_destination = sctp_find_alternate_net(stcb, lnet, 0); |
michael@0 | 5155 | } |
michael@0 | 5156 | if (net == asoc->last_data_chunk_from) { |
michael@0 | 5157 | /* Reset primary */ |
michael@0 | 5158 | asoc->last_data_chunk_from = TAILQ_FIRST(&asoc->nets); |
michael@0 | 5159 | } |
michael@0 | 5160 | if (net == asoc->last_control_chunk_from) { |
michael@0 | 5161 | /* Clear net */ |
michael@0 | 5162 | asoc->last_control_chunk_from = NULL; |
michael@0 | 5163 | } |
michael@0 | 5164 | if (net == stcb->asoc.alternate) { |
michael@0 | 5165 | sctp_free_remote_addr(stcb->asoc.alternate); |
michael@0 | 5166 | stcb->asoc.alternate = NULL; |
michael@0 | 5167 | } |
michael@0 | 5168 | sctp_free_remote_addr(net); |
michael@0 | 5169 | } |
michael@0 | 5170 | |
michael@0 | 5171 | /* |
michael@0 | 5172 | * remove a remote endpoint address from an association, it will fail if the |
michael@0 | 5173 | * address does not exist. |
michael@0 | 5174 | */ |
michael@0 | 5175 | int |
michael@0 | 5176 | sctp_del_remote_addr(struct sctp_tcb *stcb, struct sockaddr *remaddr) |
michael@0 | 5177 | { |
michael@0 | 5178 | /* |
michael@0 | 5179 | * Here we need to remove a remote address. This is quite simple, we |
michael@0 | 5180 | * first find it in the list of address for the association |
michael@0 | 5181 | * (tasoc->asoc.nets) and then if it is there, we do a LIST_REMOVE |
michael@0 | 5182 | * on that item. Note we do not allow it to be removed if there are |
michael@0 | 5183 | * no other addresses. |
michael@0 | 5184 | */ |
michael@0 | 5185 | struct sctp_association *asoc; |
michael@0 | 5186 | struct sctp_nets *net, *nnet; |
michael@0 | 5187 | |
michael@0 | 5188 | asoc = &stcb->asoc; |
michael@0 | 5189 | |
michael@0 | 5190 | /* locate the address */ |
michael@0 | 5191 | TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) { |
michael@0 | 5192 | if (net->ro._l_addr.sa.sa_family != remaddr->sa_family) { |
michael@0 | 5193 | continue; |
michael@0 | 5194 | } |
michael@0 | 5195 | if (sctp_cmpaddr((struct sockaddr *)&net->ro._l_addr, |
michael@0 | 5196 | remaddr)) { |
michael@0 | 5197 | /* we found the guy */ |
michael@0 | 5198 | if (asoc->numnets < 2) { |
michael@0 | 5199 | /* Must have at LEAST two remote addresses */ |
michael@0 | 5200 | return (-1); |
michael@0 | 5201 | } else { |
michael@0 | 5202 | sctp_remove_net(stcb, net); |
michael@0 | 5203 | return (0); |
michael@0 | 5204 | } |
michael@0 | 5205 | } |
michael@0 | 5206 | } |
michael@0 | 5207 | /* not found. */ |
michael@0 | 5208 | return (-2); |
michael@0 | 5209 | } |
michael@0 | 5210 | |
michael@0 | 5211 | void |
michael@0 | 5212 | sctp_delete_from_timewait(uint32_t tag, uint16_t lport, uint16_t rport) |
michael@0 | 5213 | { |
michael@0 | 5214 | struct sctpvtaghead *chain; |
michael@0 | 5215 | struct sctp_tagblock *twait_block; |
michael@0 | 5216 | int found = 0; |
michael@0 | 5217 | int i; |
michael@0 | 5218 | |
michael@0 | 5219 | chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)]; |
michael@0 | 5220 | LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) { |
michael@0 | 5221 | for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) { |
michael@0 | 5222 | if ((twait_block->vtag_block[i].v_tag == tag) && |
michael@0 | 5223 | (twait_block->vtag_block[i].lport == lport) && |
michael@0 | 5224 | (twait_block->vtag_block[i].rport == rport)) { |
michael@0 | 5225 | twait_block->vtag_block[i].tv_sec_at_expire = 0; |
michael@0 | 5226 | twait_block->vtag_block[i].v_tag = 0; |
michael@0 | 5227 | twait_block->vtag_block[i].lport = 0; |
michael@0 | 5228 | twait_block->vtag_block[i].rport = 0; |
michael@0 | 5229 | found = 1; |
michael@0 | 5230 | break; |
michael@0 | 5231 | } |
michael@0 | 5232 | } |
michael@0 | 5233 | if (found) |
michael@0 | 5234 | break; |
michael@0 | 5235 | } |
michael@0 | 5236 | } |
michael@0 | 5237 | |
michael@0 | 5238 | int |
michael@0 | 5239 | sctp_is_in_timewait(uint32_t tag, uint16_t lport, uint16_t rport) |
michael@0 | 5240 | { |
michael@0 | 5241 | struct sctpvtaghead *chain; |
michael@0 | 5242 | struct sctp_tagblock *twait_block; |
michael@0 | 5243 | int found = 0; |
michael@0 | 5244 | int i; |
michael@0 | 5245 | |
michael@0 | 5246 | SCTP_INP_INFO_WLOCK(); |
michael@0 | 5247 | chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)]; |
michael@0 | 5248 | LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) { |
michael@0 | 5249 | for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) { |
michael@0 | 5250 | if ((twait_block->vtag_block[i].v_tag == tag) && |
michael@0 | 5251 | (twait_block->vtag_block[i].lport == lport) && |
michael@0 | 5252 | (twait_block->vtag_block[i].rport == rport)) { |
michael@0 | 5253 | found = 1; |
michael@0 | 5254 | break; |
michael@0 | 5255 | } |
michael@0 | 5256 | } |
michael@0 | 5257 | if (found) |
michael@0 | 5258 | break; |
michael@0 | 5259 | } |
michael@0 | 5260 | SCTP_INP_INFO_WUNLOCK(); |
michael@0 | 5261 | return (found); |
michael@0 | 5262 | } |
michael@0 | 5263 | |
michael@0 | 5264 | |
michael@0 | 5265 | void |
michael@0 | 5266 | sctp_add_vtag_to_timewait(uint32_t tag, uint32_t time, uint16_t lport, uint16_t rport) |
michael@0 | 5267 | { |
michael@0 | 5268 | struct sctpvtaghead *chain; |
michael@0 | 5269 | struct sctp_tagblock *twait_block; |
michael@0 | 5270 | struct timeval now; |
michael@0 | 5271 | int set, i; |
michael@0 | 5272 | |
michael@0 | 5273 | if (time == 0) { |
michael@0 | 5274 | /* Its disabled */ |
michael@0 | 5275 | return; |
michael@0 | 5276 | } |
michael@0 | 5277 | (void)SCTP_GETTIME_TIMEVAL(&now); |
michael@0 | 5278 | chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)]; |
michael@0 | 5279 | set = 0; |
michael@0 | 5280 | LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) { |
michael@0 | 5281 | /* Block(s) present, lets find space, and expire on the fly */ |
michael@0 | 5282 | for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) { |
michael@0 | 5283 | if ((twait_block->vtag_block[i].v_tag == 0) && |
michael@0 | 5284 | !set) { |
michael@0 | 5285 | twait_block->vtag_block[i].tv_sec_at_expire = |
michael@0 | 5286 | now.tv_sec + time; |
michael@0 | 5287 | twait_block->vtag_block[i].v_tag = tag; |
michael@0 | 5288 | twait_block->vtag_block[i].lport = lport; |
michael@0 | 5289 | twait_block->vtag_block[i].rport = rport; |
michael@0 | 5290 | set = 1; |
michael@0 | 5291 | } else if ((twait_block->vtag_block[i].v_tag) && |
michael@0 | 5292 | ((long)twait_block->vtag_block[i].tv_sec_at_expire < now.tv_sec)) { |
michael@0 | 5293 | /* Audit expires this guy */ |
michael@0 | 5294 | twait_block->vtag_block[i].tv_sec_at_expire = 0; |
michael@0 | 5295 | twait_block->vtag_block[i].v_tag = 0; |
michael@0 | 5296 | twait_block->vtag_block[i].lport = 0; |
michael@0 | 5297 | twait_block->vtag_block[i].rport = 0; |
michael@0 | 5298 | if (set == 0) { |
michael@0 | 5299 | /* Reuse it for my new tag */ |
michael@0 | 5300 | twait_block->vtag_block[i].tv_sec_at_expire = now.tv_sec + time; |
michael@0 | 5301 | twait_block->vtag_block[i].v_tag = tag; |
michael@0 | 5302 | twait_block->vtag_block[i].lport = lport; |
michael@0 | 5303 | twait_block->vtag_block[i].rport = rport; |
michael@0 | 5304 | set = 1; |
michael@0 | 5305 | } |
michael@0 | 5306 | } |
michael@0 | 5307 | } |
michael@0 | 5308 | if (set) { |
michael@0 | 5309 | /* |
michael@0 | 5310 | * We only do up to the block where we can |
michael@0 | 5311 | * place our tag for audits |
michael@0 | 5312 | */ |
michael@0 | 5313 | break; |
michael@0 | 5314 | } |
michael@0 | 5315 | } |
michael@0 | 5316 | /* Need to add a new block to chain */ |
michael@0 | 5317 | if (!set) { |
michael@0 | 5318 | SCTP_MALLOC(twait_block, struct sctp_tagblock *, |
michael@0 | 5319 | sizeof(struct sctp_tagblock), SCTP_M_TIMW); |
michael@0 | 5320 | if (twait_block == NULL) { |
michael@0 | 5321 | #ifdef INVARIANTS |
michael@0 | 5322 | panic("Can not alloc tagblock"); |
michael@0 | 5323 | #endif |
michael@0 | 5324 | return; |
michael@0 | 5325 | } |
michael@0 | 5326 | memset(twait_block, 0, sizeof(struct sctp_tagblock)); |
michael@0 | 5327 | LIST_INSERT_HEAD(chain, twait_block, sctp_nxt_tagblock); |
michael@0 | 5328 | twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec + time; |
michael@0 | 5329 | twait_block->vtag_block[0].v_tag = tag; |
michael@0 | 5330 | twait_block->vtag_block[0].lport = lport; |
michael@0 | 5331 | twait_block->vtag_block[0].rport = rport; |
michael@0 | 5332 | } |
michael@0 | 5333 | } |
michael@0 | 5334 | |
michael@0 | 5335 | |
michael@0 | 5336 | #ifdef __Panda__ |
michael@0 | 5337 | void panda_wakeup_socket(struct socket *so); |
michael@0 | 5338 | #endif |
michael@0 | 5339 | |
michael@0 | 5340 | /*- |
michael@0 | 5341 | * Free the association after un-hashing the remote port. This |
michael@0 | 5342 | * function ALWAYS returns holding NO LOCK on the stcb. It DOES |
michael@0 | 5343 | * expect that the input to this function IS a locked TCB. |
michael@0 | 5344 | * It will return 0, if it did NOT destroy the association (instead |
michael@0 | 5345 | * it unlocks it. It will return NON-zero if it either destroyed the |
michael@0 | 5346 | * association OR the association is already destroyed. |
michael@0 | 5347 | */ |
michael@0 | 5348 | int |
michael@0 | 5349 | sctp_free_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int from_inpcbfree, int from_location) |
michael@0 | 5350 | { |
michael@0 | 5351 | int i; |
michael@0 | 5352 | struct sctp_association *asoc; |
michael@0 | 5353 | struct sctp_nets *net, *nnet; |
michael@0 | 5354 | struct sctp_laddr *laddr, *naddr; |
michael@0 | 5355 | struct sctp_tmit_chunk *chk, *nchk; |
michael@0 | 5356 | struct sctp_asconf_addr *aparam, *naparam; |
michael@0 | 5357 | struct sctp_asconf_ack *aack, *naack; |
michael@0 | 5358 | struct sctp_stream_reset_list *strrst, *nstrrst; |
michael@0 | 5359 | struct sctp_queued_to_read *sq, *nsq; |
michael@0 | 5360 | struct sctp_stream_queue_pending *sp, *nsp; |
michael@0 | 5361 | sctp_sharedkey_t *shared_key, *nshared_key; |
michael@0 | 5362 | struct socket *so; |
michael@0 | 5363 | |
michael@0 | 5364 | /* first, lets purge the entry from the hash table. */ |
michael@0 | 5365 | #if defined(__APPLE__) |
michael@0 | 5366 | sctp_lock_assert(SCTP_INP_SO(inp)); |
michael@0 | 5367 | #endif |
michael@0 | 5368 | |
michael@0 | 5369 | #ifdef SCTP_LOG_CLOSING |
michael@0 | 5370 | sctp_log_closing(inp, stcb, 6); |
michael@0 | 5371 | #endif |
michael@0 | 5372 | if (stcb->asoc.state == 0) { |
michael@0 | 5373 | #ifdef SCTP_LOG_CLOSING |
michael@0 | 5374 | sctp_log_closing(inp, NULL, 7); |
michael@0 | 5375 | #endif |
michael@0 | 5376 | /* there is no asoc, really TSNH :-0 */ |
michael@0 | 5377 | return (1); |
michael@0 | 5378 | } |
michael@0 | 5379 | if (stcb->asoc.alternate) { |
michael@0 | 5380 | sctp_free_remote_addr(stcb->asoc.alternate); |
michael@0 | 5381 | stcb->asoc.alternate = NULL; |
michael@0 | 5382 | } |
michael@0 | 5383 | #if !defined(__APPLE__) /* TEMP: moved to below */ |
michael@0 | 5384 | /* TEMP CODE */ |
michael@0 | 5385 | if (stcb->freed_from_where == 0) { |
michael@0 | 5386 | /* Only record the first place free happened from */ |
michael@0 | 5387 | stcb->freed_from_where = from_location; |
michael@0 | 5388 | } |
michael@0 | 5389 | /* TEMP CODE */ |
michael@0 | 5390 | #endif |
michael@0 | 5391 | |
michael@0 | 5392 | asoc = &stcb->asoc; |
michael@0 | 5393 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || |
michael@0 | 5394 | (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) |
michael@0 | 5395 | /* nothing around */ |
michael@0 | 5396 | so = NULL; |
michael@0 | 5397 | else |
michael@0 | 5398 | so = inp->sctp_socket; |
michael@0 | 5399 | |
michael@0 | 5400 | /* |
michael@0 | 5401 | * We used timer based freeing if a reader or writer is in the way. |
michael@0 | 5402 | * So we first check if we are actually being called from a timer, |
michael@0 | 5403 | * if so we abort early if a reader or writer is still in the way. |
michael@0 | 5404 | */ |
michael@0 | 5405 | if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) && |
michael@0 | 5406 | (from_inpcbfree == SCTP_NORMAL_PROC)) { |
michael@0 | 5407 | /* |
michael@0 | 5408 | * is it the timer driving us? if so are the reader/writers |
michael@0 | 5409 | * gone? |
michael@0 | 5410 | */ |
michael@0 | 5411 | if (stcb->asoc.refcnt) { |
michael@0 | 5412 | /* nope, reader or writer in the way */ |
michael@0 | 5413 | sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL); |
michael@0 | 5414 | /* no asoc destroyed */ |
michael@0 | 5415 | SCTP_TCB_UNLOCK(stcb); |
michael@0 | 5416 | #ifdef SCTP_LOG_CLOSING |
michael@0 | 5417 | sctp_log_closing(inp, stcb, 8); |
michael@0 | 5418 | #endif |
michael@0 | 5419 | return (0); |
michael@0 | 5420 | } |
michael@0 | 5421 | } |
michael@0 | 5422 | /* now clean up any other timers */ |
michael@0 | 5423 | (void)SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer); |
michael@0 | 5424 | asoc->dack_timer.self = NULL; |
michael@0 | 5425 | (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer); |
michael@0 | 5426 | /*- |
michael@0 | 5427 | * For stream reset we don't blast this unless |
michael@0 | 5428 | * it is a str-reset timer, it might be the |
michael@0 | 5429 | * free-asoc timer which we DON'T want to |
michael@0 | 5430 | * disturb. |
michael@0 | 5431 | */ |
michael@0 | 5432 | if (asoc->strreset_timer.type == SCTP_TIMER_TYPE_STRRESET) |
michael@0 | 5433 | asoc->strreset_timer.self = NULL; |
michael@0 | 5434 | (void)SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer); |
michael@0 | 5435 | asoc->asconf_timer.self = NULL; |
michael@0 | 5436 | (void)SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer); |
michael@0 | 5437 | asoc->autoclose_timer.self = NULL; |
michael@0 | 5438 | (void)SCTP_OS_TIMER_STOP(&asoc->shut_guard_timer.timer); |
michael@0 | 5439 | asoc->shut_guard_timer.self = NULL; |
michael@0 | 5440 | (void)SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer); |
michael@0 | 5441 | asoc->delayed_event_timer.self = NULL; |
michael@0 | 5442 | /* Mobility adaptation */ |
michael@0 | 5443 | (void)SCTP_OS_TIMER_STOP(&asoc->delete_prim_timer.timer); |
michael@0 | 5444 | asoc->delete_prim_timer.self = NULL; |
michael@0 | 5445 | TAILQ_FOREACH(net, &asoc->nets, sctp_next) { |
michael@0 | 5446 | (void)SCTP_OS_TIMER_STOP(&net->rxt_timer.timer); |
michael@0 | 5447 | net->rxt_timer.self = NULL; |
michael@0 | 5448 | (void)SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer); |
michael@0 | 5449 | net->pmtu_timer.self = NULL; |
michael@0 | 5450 | (void)SCTP_OS_TIMER_STOP(&net->hb_timer.timer); |
michael@0 | 5451 | net->hb_timer.self = NULL; |
michael@0 | 5452 | } |
michael@0 | 5453 | /* Now the read queue needs to be cleaned up (only once) */ |
michael@0 | 5454 | if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0) { |
michael@0 | 5455 | stcb->asoc.state |= SCTP_STATE_ABOUT_TO_BE_FREED; |
michael@0 | 5456 | SCTP_INP_READ_LOCK(inp); |
michael@0 | 5457 | TAILQ_FOREACH(sq, &inp->read_queue, next) { |
michael@0 | 5458 | if (sq->stcb == stcb) { |
michael@0 | 5459 | sq->do_not_ref_stcb = 1; |
michael@0 | 5460 | sq->sinfo_cumtsn = stcb->asoc.cumulative_tsn; |
michael@0 | 5461 | /* If there is no end, there never |
michael@0 | 5462 | * will be now. |
michael@0 | 5463 | */ |
michael@0 | 5464 | if (sq->end_added == 0) { |
michael@0 | 5465 | /* Held for PD-API clear that. */ |
michael@0 | 5466 | sq->pdapi_aborted = 1; |
michael@0 | 5467 | sq->held_length = 0; |
michael@0 | 5468 | if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT) && (so != NULL)) { |
michael@0 | 5469 | /* |
michael@0 | 5470 | * Need to add a PD-API aborted indication. |
michael@0 | 5471 | * Setting the control_pdapi assures that it will |
michael@0 | 5472 | * be added right after this msg. |
michael@0 | 5473 | */ |
michael@0 | 5474 | uint32_t strseq; |
michael@0 | 5475 | stcb->asoc.control_pdapi = sq; |
michael@0 | 5476 | strseq = (sq->sinfo_stream << 16) | sq->sinfo_ssn; |
michael@0 | 5477 | sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION, |
michael@0 | 5478 | stcb, |
michael@0 | 5479 | SCTP_PARTIAL_DELIVERY_ABORTED, |
michael@0 | 5480 | (void *)&strseq, |
michael@0 | 5481 | SCTP_SO_LOCKED); |
michael@0 | 5482 | stcb->asoc.control_pdapi = NULL; |
michael@0 | 5483 | } |
michael@0 | 5484 | } |
michael@0 | 5485 | /* Add an end to wake them */ |
michael@0 | 5486 | sq->end_added = 1; |
michael@0 | 5487 | } |
michael@0 | 5488 | } |
michael@0 | 5489 | SCTP_INP_READ_UNLOCK(inp); |
michael@0 | 5490 | if (stcb->block_entry) { |
michael@0 | 5491 | SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PCB, ECONNRESET); |
michael@0 | 5492 | stcb->block_entry->error = ECONNRESET; |
michael@0 | 5493 | stcb->block_entry = NULL; |
michael@0 | 5494 | } |
michael@0 | 5495 | } |
michael@0 | 5496 | if ((stcb->asoc.refcnt) || (stcb->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE)) { |
michael@0 | 5497 | /* Someone holds a reference OR the socket is unaccepted yet. |
michael@0 | 5498 | */ |
michael@0 | 5499 | if ((stcb->asoc.refcnt) || |
michael@0 | 5500 | (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || |
michael@0 | 5501 | (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) { |
michael@0 | 5502 | stcb->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE; |
michael@0 | 5503 | sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL); |
michael@0 | 5504 | } |
michael@0 | 5505 | SCTP_TCB_UNLOCK(stcb); |
michael@0 | 5506 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || |
michael@0 | 5507 | (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) |
michael@0 | 5508 | /* nothing around */ |
michael@0 | 5509 | so = NULL; |
michael@0 | 5510 | if (so) { |
michael@0 | 5511 | /* Wake any reader/writers */ |
michael@0 | 5512 | sctp_sorwakeup(inp, so); |
michael@0 | 5513 | sctp_sowwakeup(inp, so); |
michael@0 | 5514 | } |
michael@0 | 5515 | |
michael@0 | 5516 | #ifdef SCTP_LOG_CLOSING |
michael@0 | 5517 | sctp_log_closing(inp, stcb, 9); |
michael@0 | 5518 | #endif |
michael@0 | 5519 | /* no asoc destroyed */ |
michael@0 | 5520 | return (0); |
michael@0 | 5521 | } |
michael@0 | 5522 | #ifdef SCTP_LOG_CLOSING |
michael@0 | 5523 | sctp_log_closing(inp, stcb, 10); |
michael@0 | 5524 | #endif |
michael@0 | 5525 | /* When I reach here, no others want |
michael@0 | 5526 | * to kill the assoc yet.. and I own |
michael@0 | 5527 | * the lock. Now its possible an abort |
michael@0 | 5528 | * comes in when I do the lock exchange |
michael@0 | 5529 | * below to grab all the locks to do |
michael@0 | 5530 | * the final take out. to prevent this |
michael@0 | 5531 | * we increment the count, which will |
michael@0 | 5532 | * start a timer and blow out above thus |
michael@0 | 5533 | * assuring us that we hold exclusive |
michael@0 | 5534 | * killing of the asoc. Note that |
michael@0 | 5535 | * after getting back the TCB lock |
michael@0 | 5536 | * we will go ahead and increment the |
michael@0 | 5537 | * counter back up and stop any timer |
michael@0 | 5538 | * a passing stranger may have started :-S |
michael@0 | 5539 | */ |
michael@0 | 5540 | if (from_inpcbfree == SCTP_NORMAL_PROC) { |
michael@0 | 5541 | atomic_add_int(&stcb->asoc.refcnt, 1); |
michael@0 | 5542 | |
michael@0 | 5543 | SCTP_TCB_UNLOCK(stcb); |
michael@0 | 5544 | SCTP_INP_INFO_WLOCK(); |
michael@0 | 5545 | SCTP_INP_WLOCK(inp); |
michael@0 | 5546 | SCTP_TCB_LOCK(stcb); |
michael@0 | 5547 | } |
michael@0 | 5548 | /* Double check the GONE flag */ |
michael@0 | 5549 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || |
michael@0 | 5550 | (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) |
michael@0 | 5551 | /* nothing around */ |
michael@0 | 5552 | so = NULL; |
michael@0 | 5553 | |
michael@0 | 5554 | if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || |
michael@0 | 5555 | (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) { |
michael@0 | 5556 | /* |
michael@0 | 5557 | * For TCP type we need special handling when we are |
michael@0 | 5558 | * connected. We also include the peel'ed off ones to. |
michael@0 | 5559 | */ |
michael@0 | 5560 | if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) { |
michael@0 | 5561 | inp->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED; |
michael@0 | 5562 | inp->sctp_flags |= SCTP_PCB_FLAGS_WAS_CONNECTED; |
michael@0 | 5563 | if (so) { |
michael@0 | 5564 | SOCK_LOCK(so); |
michael@0 | 5565 | if (so->so_rcv.sb_cc == 0) { |
michael@0 | 5566 | so->so_state &= ~(SS_ISCONNECTING | |
michael@0 | 5567 | SS_ISDISCONNECTING | |
michael@0 | 5568 | SS_ISCONFIRMING | |
michael@0 | 5569 | SS_ISCONNECTED); |
michael@0 | 5570 | } |
michael@0 | 5571 | #if defined(__APPLE__) |
michael@0 | 5572 | socantrcvmore(so); |
michael@0 | 5573 | #else |
michael@0 | 5574 | socantrcvmore_locked(so); |
michael@0 | 5575 | #endif |
michael@0 | 5576 | sctp_sowwakeup(inp, so); |
michael@0 | 5577 | sctp_sorwakeup(inp, so); |
michael@0 | 5578 | SCTP_SOWAKEUP(so); |
michael@0 | 5579 | } |
michael@0 | 5580 | } |
michael@0 | 5581 | } |
michael@0 | 5582 | |
michael@0 | 5583 | /* Make it invalid too, that way if its |
michael@0 | 5584 | * about to run it will abort and return. |
michael@0 | 5585 | */ |
michael@0 | 5586 | /* re-increment the lock */ |
michael@0 | 5587 | if (from_inpcbfree == SCTP_NORMAL_PROC) { |
michael@0 | 5588 | atomic_add_int(&stcb->asoc.refcnt, -1); |
michael@0 | 5589 | } |
michael@0 | 5590 | if (stcb->asoc.refcnt) { |
michael@0 | 5591 | stcb->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE; |
michael@0 | 5592 | sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL); |
michael@0 | 5593 | if (from_inpcbfree == SCTP_NORMAL_PROC) { |
michael@0 | 5594 | SCTP_INP_INFO_WUNLOCK(); |
michael@0 | 5595 | SCTP_INP_WUNLOCK(inp); |
michael@0 | 5596 | } |
michael@0 | 5597 | SCTP_TCB_UNLOCK(stcb); |
michael@0 | 5598 | return (0); |
michael@0 | 5599 | } |
michael@0 | 5600 | asoc->state = 0; |
michael@0 | 5601 | if (inp->sctp_tcbhash) { |
michael@0 | 5602 | LIST_REMOVE(stcb, sctp_tcbhash); |
michael@0 | 5603 | } |
michael@0 | 5604 | if (stcb->asoc.in_asocid_hash) { |
michael@0 | 5605 | LIST_REMOVE(stcb, sctp_tcbasocidhash); |
michael@0 | 5606 | } |
michael@0 | 5607 | /* Now lets remove it from the list of ALL associations in the EP */ |
michael@0 | 5608 | LIST_REMOVE(stcb, sctp_tcblist); |
michael@0 | 5609 | if (from_inpcbfree == SCTP_NORMAL_PROC) { |
michael@0 | 5610 | SCTP_INP_INCR_REF(inp); |
michael@0 | 5611 | SCTP_INP_WUNLOCK(inp); |
michael@0 | 5612 | } |
michael@0 | 5613 | /* pull from vtag hash */ |
michael@0 | 5614 | LIST_REMOVE(stcb, sctp_asocs); |
michael@0 | 5615 | sctp_add_vtag_to_timewait(asoc->my_vtag, SCTP_BASE_SYSCTL(sctp_vtag_time_wait), |
michael@0 | 5616 | inp->sctp_lport, stcb->rport); |
michael@0 | 5617 | |
michael@0 | 5618 | /* Now restop the timers to be sure |
michael@0 | 5619 | * this is paranoia at is finest! |
michael@0 | 5620 | */ |
michael@0 | 5621 | (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer); |
michael@0 | 5622 | (void)SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer); |
michael@0 | 5623 | (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer); |
michael@0 | 5624 | (void)SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer); |
michael@0 | 5625 | (void)SCTP_OS_TIMER_STOP(&asoc->shut_guard_timer.timer); |
michael@0 | 5626 | (void)SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer); |
michael@0 | 5627 | (void)SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer); |
michael@0 | 5628 | TAILQ_FOREACH(net, &asoc->nets, sctp_next) { |
michael@0 | 5629 | (void)SCTP_OS_TIMER_STOP(&net->rxt_timer.timer); |
michael@0 | 5630 | (void)SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer); |
michael@0 | 5631 | (void)SCTP_OS_TIMER_STOP(&net->hb_timer.timer); |
michael@0 | 5632 | } |
michael@0 | 5633 | |
michael@0 | 5634 | asoc->strreset_timer.type = SCTP_TIMER_TYPE_NONE; |
michael@0 | 5635 | /* |
michael@0 | 5636 | * The chunk lists and such SHOULD be empty but we check them just |
michael@0 | 5637 | * in case. |
michael@0 | 5638 | */ |
michael@0 | 5639 | /* anything on the wheel needs to be removed */ |
michael@0 | 5640 | for (i = 0; i < asoc->streamoutcnt; i++) { |
michael@0 | 5641 | struct sctp_stream_out *outs; |
michael@0 | 5642 | |
michael@0 | 5643 | outs = &asoc->strmout[i]; |
michael@0 | 5644 | /* now clean up any chunks here */ |
michael@0 | 5645 | TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) { |
michael@0 | 5646 | TAILQ_REMOVE(&outs->outqueue, sp, next); |
michael@0 | 5647 | sctp_free_spbufspace(stcb, asoc, sp); |
michael@0 | 5648 | if (sp->data) { |
michael@0 | 5649 | if (so) { |
michael@0 | 5650 | /* Still an open socket - report */ |
michael@0 | 5651 | sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb, |
michael@0 | 5652 | 0, (void *)sp, SCTP_SO_LOCKED); |
michael@0 | 5653 | } |
michael@0 | 5654 | if (sp->data) { |
michael@0 | 5655 | sctp_m_freem(sp->data); |
michael@0 | 5656 | sp->data = NULL; |
michael@0 | 5657 | sp->tail_mbuf = NULL; |
michael@0 | 5658 | sp->length = 0; |
michael@0 | 5659 | } |
michael@0 | 5660 | } |
michael@0 | 5661 | if (sp->net) { |
michael@0 | 5662 | sctp_free_remote_addr(sp->net); |
michael@0 | 5663 | sp->net = NULL; |
michael@0 | 5664 | } |
michael@0 | 5665 | sctp_free_a_strmoq(stcb, sp, SCTP_SO_LOCKED); |
michael@0 | 5666 | } |
michael@0 | 5667 | } |
michael@0 | 5668 | /*sa_ignore FREED_MEMORY*/ |
michael@0 | 5669 | TAILQ_FOREACH_SAFE(strrst, &asoc->resetHead, next_resp, nstrrst) { |
michael@0 | 5670 | TAILQ_REMOVE(&asoc->resetHead, strrst, next_resp); |
michael@0 | 5671 | SCTP_FREE(strrst, SCTP_M_STRESET); |
michael@0 | 5672 | } |
michael@0 | 5673 | TAILQ_FOREACH_SAFE(sq, &asoc->pending_reply_queue, next, nsq) { |
michael@0 | 5674 | TAILQ_REMOVE(&asoc->pending_reply_queue, sq, next); |
michael@0 | 5675 | if (sq->data) { |
michael@0 | 5676 | sctp_m_freem(sq->data); |
michael@0 | 5677 | sq->data = NULL; |
michael@0 | 5678 | } |
michael@0 | 5679 | sctp_free_remote_addr(sq->whoFrom); |
michael@0 | 5680 | sq->whoFrom = NULL; |
michael@0 | 5681 | sq->stcb = NULL; |
michael@0 | 5682 | /* Free the ctl entry */ |
michael@0 | 5683 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), sq); |
michael@0 | 5684 | SCTP_DECR_READQ_COUNT(); |
michael@0 | 5685 | /*sa_ignore FREED_MEMORY*/ |
michael@0 | 5686 | } |
michael@0 | 5687 | TAILQ_FOREACH_SAFE(chk, &asoc->free_chunks, sctp_next, nchk) { |
michael@0 | 5688 | TAILQ_REMOVE(&asoc->free_chunks, chk, sctp_next); |
michael@0 | 5689 | if (chk->data) { |
michael@0 | 5690 | sctp_m_freem(chk->data); |
michael@0 | 5691 | chk->data = NULL; |
michael@0 | 5692 | } |
michael@0 | 5693 | if (chk->holds_key_ref) |
michael@0 | 5694 | sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); |
michael@0 | 5695 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); |
michael@0 | 5696 | SCTP_DECR_CHK_COUNT(); |
michael@0 | 5697 | atomic_subtract_int(&SCTP_BASE_INFO(ipi_free_chunks), 1); |
michael@0 | 5698 | asoc->free_chunk_cnt--; |
michael@0 | 5699 | /*sa_ignore FREED_MEMORY*/ |
michael@0 | 5700 | } |
michael@0 | 5701 | /* pending send queue SHOULD be empty */ |
michael@0 | 5702 | TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) { |
michael@0 | 5703 | if (asoc->strmout[chk->rec.data.stream_number].chunks_on_queues > 0) { |
michael@0 | 5704 | asoc->strmout[chk->rec.data.stream_number].chunks_on_queues--; |
michael@0 | 5705 | #ifdef INVARIANTS |
michael@0 | 5706 | } else { |
michael@0 | 5707 | panic("No chunks on the queues for sid %u.", chk->rec.data.stream_number); |
michael@0 | 5708 | #endif |
michael@0 | 5709 | } |
michael@0 | 5710 | TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next); |
michael@0 | 5711 | if (chk->data) { |
michael@0 | 5712 | if (so) { |
michael@0 | 5713 | /* Still a socket? */ |
michael@0 | 5714 | sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, |
michael@0 | 5715 | 0, chk, SCTP_SO_LOCKED); |
michael@0 | 5716 | } |
michael@0 | 5717 | if (chk->data) { |
michael@0 | 5718 | sctp_m_freem(chk->data); |
michael@0 | 5719 | chk->data = NULL; |
michael@0 | 5720 | } |
michael@0 | 5721 | } |
michael@0 | 5722 | if (chk->holds_key_ref) |
michael@0 | 5723 | sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); |
michael@0 | 5724 | if (chk->whoTo) { |
michael@0 | 5725 | sctp_free_remote_addr(chk->whoTo); |
michael@0 | 5726 | chk->whoTo = NULL; |
michael@0 | 5727 | } |
michael@0 | 5728 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); |
michael@0 | 5729 | SCTP_DECR_CHK_COUNT(); |
michael@0 | 5730 | /*sa_ignore FREED_MEMORY*/ |
michael@0 | 5731 | } |
michael@0 | 5732 | /* sent queue SHOULD be empty */ |
michael@0 | 5733 | TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) { |
michael@0 | 5734 | if (chk->sent != SCTP_DATAGRAM_NR_ACKED) { |
michael@0 | 5735 | if (asoc->strmout[chk->rec.data.stream_number].chunks_on_queues > 0) { |
michael@0 | 5736 | asoc->strmout[chk->rec.data.stream_number].chunks_on_queues--; |
michael@0 | 5737 | #ifdef INVARIANTS |
michael@0 | 5738 | } else { |
michael@0 | 5739 | panic("No chunks on the queues for sid %u.", chk->rec.data.stream_number); |
michael@0 | 5740 | #endif |
michael@0 | 5741 | } |
michael@0 | 5742 | } |
michael@0 | 5743 | TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next); |
michael@0 | 5744 | if (chk->data) { |
michael@0 | 5745 | if (so) { |
michael@0 | 5746 | /* Still a socket? */ |
michael@0 | 5747 | sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, |
michael@0 | 5748 | 0, chk, SCTP_SO_LOCKED); |
michael@0 | 5749 | } |
michael@0 | 5750 | if (chk->data) { |
michael@0 | 5751 | sctp_m_freem(chk->data); |
michael@0 | 5752 | chk->data = NULL; |
michael@0 | 5753 | } |
michael@0 | 5754 | } |
michael@0 | 5755 | if (chk->holds_key_ref) |
michael@0 | 5756 | sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); |
michael@0 | 5757 | sctp_free_remote_addr(chk->whoTo); |
michael@0 | 5758 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); |
michael@0 | 5759 | SCTP_DECR_CHK_COUNT(); |
michael@0 | 5760 | /*sa_ignore FREED_MEMORY*/ |
michael@0 | 5761 | } |
michael@0 | 5762 | #ifdef INVARIANTS |
michael@0 | 5763 | for (i = 0; i < stcb->asoc.streamoutcnt; i++) { |
michael@0 | 5764 | if (stcb->asoc.strmout[i].chunks_on_queues > 0) { |
michael@0 | 5765 | panic("%u chunks left for stream %u.", stcb->asoc.strmout[i].chunks_on_queues, i); |
michael@0 | 5766 | } |
michael@0 | 5767 | } |
michael@0 | 5768 | #endif |
michael@0 | 5769 | /* control queue MAY not be empty */ |
michael@0 | 5770 | TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) { |
michael@0 | 5771 | TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next); |
michael@0 | 5772 | if (chk->data) { |
michael@0 | 5773 | sctp_m_freem(chk->data); |
michael@0 | 5774 | chk->data = NULL; |
michael@0 | 5775 | } |
michael@0 | 5776 | if (chk->holds_key_ref) |
michael@0 | 5777 | sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); |
michael@0 | 5778 | sctp_free_remote_addr(chk->whoTo); |
michael@0 | 5779 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); |
michael@0 | 5780 | SCTP_DECR_CHK_COUNT(); |
michael@0 | 5781 | /*sa_ignore FREED_MEMORY*/ |
michael@0 | 5782 | } |
michael@0 | 5783 | /* ASCONF queue MAY not be empty */ |
michael@0 | 5784 | TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) { |
michael@0 | 5785 | TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next); |
michael@0 | 5786 | if (chk->data) { |
michael@0 | 5787 | sctp_m_freem(chk->data); |
michael@0 | 5788 | chk->data = NULL; |
michael@0 | 5789 | } |
michael@0 | 5790 | if (chk->holds_key_ref) |
michael@0 | 5791 | sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); |
michael@0 | 5792 | sctp_free_remote_addr(chk->whoTo); |
michael@0 | 5793 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); |
michael@0 | 5794 | SCTP_DECR_CHK_COUNT(); |
michael@0 | 5795 | /*sa_ignore FREED_MEMORY*/ |
michael@0 | 5796 | } |
michael@0 | 5797 | TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) { |
michael@0 | 5798 | TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next); |
michael@0 | 5799 | if (chk->data) { |
michael@0 | 5800 | sctp_m_freem(chk->data); |
michael@0 | 5801 | chk->data = NULL; |
michael@0 | 5802 | } |
michael@0 | 5803 | if (chk->holds_key_ref) |
michael@0 | 5804 | sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED); |
michael@0 | 5805 | sctp_free_remote_addr(chk->whoTo); |
michael@0 | 5806 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk); |
michael@0 | 5807 | SCTP_DECR_CHK_COUNT(); |
michael@0 | 5808 | /*sa_ignore FREED_MEMORY*/ |
michael@0 | 5809 | } |
michael@0 | 5810 | |
michael@0 | 5811 | if (asoc->mapping_array) { |
michael@0 | 5812 | SCTP_FREE(asoc->mapping_array, SCTP_M_MAP); |
michael@0 | 5813 | asoc->mapping_array = NULL; |
michael@0 | 5814 | } |
michael@0 | 5815 | if (asoc->nr_mapping_array) { |
michael@0 | 5816 | SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP); |
michael@0 | 5817 | asoc->nr_mapping_array = NULL; |
michael@0 | 5818 | } |
michael@0 | 5819 | /* the stream outs */ |
michael@0 | 5820 | if (asoc->strmout) { |
michael@0 | 5821 | SCTP_FREE(asoc->strmout, SCTP_M_STRMO); |
michael@0 | 5822 | asoc->strmout = NULL; |
michael@0 | 5823 | } |
michael@0 | 5824 | asoc->strm_realoutsize = asoc->streamoutcnt = 0; |
michael@0 | 5825 | if (asoc->strmin) { |
michael@0 | 5826 | struct sctp_queued_to_read *ctl, *nctl; |
michael@0 | 5827 | |
michael@0 | 5828 | for (i = 0; i < asoc->streamincnt; i++) { |
michael@0 | 5829 | TAILQ_FOREACH_SAFE(ctl, &asoc->strmin[i].inqueue, next, nctl) { |
michael@0 | 5830 | TAILQ_REMOVE(&asoc->strmin[i].inqueue, ctl, next); |
michael@0 | 5831 | sctp_free_remote_addr(ctl->whoFrom); |
michael@0 | 5832 | if (ctl->data) { |
michael@0 | 5833 | sctp_m_freem(ctl->data); |
michael@0 | 5834 | ctl->data = NULL; |
michael@0 | 5835 | } |
michael@0 | 5836 | /* |
michael@0 | 5837 | * We don't free the address here |
michael@0 | 5838 | * since all the net's were freed |
michael@0 | 5839 | * above. |
michael@0 | 5840 | */ |
michael@0 | 5841 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), ctl); |
michael@0 | 5842 | SCTP_DECR_READQ_COUNT(); |
michael@0 | 5843 | } |
michael@0 | 5844 | } |
michael@0 | 5845 | SCTP_FREE(asoc->strmin, SCTP_M_STRMI); |
michael@0 | 5846 | asoc->strmin = NULL; |
michael@0 | 5847 | } |
michael@0 | 5848 | asoc->streamincnt = 0; |
michael@0 | 5849 | TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) { |
michael@0 | 5850 | #ifdef INVARIANTS |
michael@0 | 5851 | if (SCTP_BASE_INFO(ipi_count_raddr) == 0) { |
michael@0 | 5852 | panic("no net's left alloc'ed, or list points to itself"); |
michael@0 | 5853 | } |
michael@0 | 5854 | #endif |
michael@0 | 5855 | TAILQ_REMOVE(&asoc->nets, net, sctp_next); |
michael@0 | 5856 | sctp_free_remote_addr(net); |
michael@0 | 5857 | } |
michael@0 | 5858 | LIST_FOREACH_SAFE(laddr, &asoc->sctp_restricted_addrs, sctp_nxt_addr, naddr) { |
michael@0 | 5859 | /*sa_ignore FREED_MEMORY*/ |
michael@0 | 5860 | sctp_remove_laddr(laddr); |
michael@0 | 5861 | } |
michael@0 | 5862 | |
michael@0 | 5863 | /* pending asconf (address) parameters */ |
michael@0 | 5864 | TAILQ_FOREACH_SAFE(aparam, &asoc->asconf_queue, next, naparam) { |
michael@0 | 5865 | /*sa_ignore FREED_MEMORY*/ |
michael@0 | 5866 | TAILQ_REMOVE(&asoc->asconf_queue, aparam, next); |
michael@0 | 5867 | SCTP_FREE(aparam,SCTP_M_ASC_ADDR); |
michael@0 | 5868 | } |
michael@0 | 5869 | TAILQ_FOREACH_SAFE(aack, &asoc->asconf_ack_sent, next, naack) { |
michael@0 | 5870 | /*sa_ignore FREED_MEMORY*/ |
michael@0 | 5871 | TAILQ_REMOVE(&asoc->asconf_ack_sent, aack, next); |
michael@0 | 5872 | if (aack->data != NULL) { |
michael@0 | 5873 | sctp_m_freem(aack->data); |
michael@0 | 5874 | } |
michael@0 | 5875 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asconf_ack), aack); |
michael@0 | 5876 | } |
michael@0 | 5877 | /* clean up auth stuff */ |
michael@0 | 5878 | if (asoc->local_hmacs) |
michael@0 | 5879 | sctp_free_hmaclist(asoc->local_hmacs); |
michael@0 | 5880 | if (asoc->peer_hmacs) |
michael@0 | 5881 | sctp_free_hmaclist(asoc->peer_hmacs); |
michael@0 | 5882 | |
michael@0 | 5883 | if (asoc->local_auth_chunks) |
michael@0 | 5884 | sctp_free_chunklist(asoc->local_auth_chunks); |
michael@0 | 5885 | if (asoc->peer_auth_chunks) |
michael@0 | 5886 | sctp_free_chunklist(asoc->peer_auth_chunks); |
michael@0 | 5887 | |
michael@0 | 5888 | sctp_free_authinfo(&asoc->authinfo); |
michael@0 | 5889 | |
michael@0 | 5890 | LIST_FOREACH_SAFE(shared_key, &asoc->shared_keys, next, nshared_key) { |
michael@0 | 5891 | LIST_REMOVE(shared_key, next); |
michael@0 | 5892 | sctp_free_sharedkey(shared_key); |
michael@0 | 5893 | /*sa_ignore FREED_MEMORY*/ |
michael@0 | 5894 | } |
michael@0 | 5895 | |
michael@0 | 5896 | /* Insert new items here :> */ |
michael@0 | 5897 | |
michael@0 | 5898 | /* Get rid of LOCK */ |
michael@0 | 5899 | SCTP_TCB_UNLOCK(stcb); |
michael@0 | 5900 | SCTP_TCB_LOCK_DESTROY(stcb); |
michael@0 | 5901 | SCTP_TCB_SEND_LOCK_DESTROY(stcb); |
michael@0 | 5902 | if (from_inpcbfree == SCTP_NORMAL_PROC) { |
michael@0 | 5903 | SCTP_INP_INFO_WUNLOCK(); |
michael@0 | 5904 | SCTP_INP_RLOCK(inp); |
michael@0 | 5905 | } |
michael@0 | 5906 | #if defined(__APPLE__) /* TEMP CODE */ |
michael@0 | 5907 | stcb->freed_from_where = from_location; |
michael@0 | 5908 | #endif |
michael@0 | 5909 | #ifdef SCTP_TRACK_FREED_ASOCS |
michael@0 | 5910 | if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { |
michael@0 | 5911 | /* now clean up the tasoc itself */ |
michael@0 | 5912 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb); |
michael@0 | 5913 | SCTP_DECR_ASOC_COUNT(); |
michael@0 | 5914 | } else { |
michael@0 | 5915 | LIST_INSERT_HEAD(&inp->sctp_asoc_free_list, stcb, sctp_tcblist); |
michael@0 | 5916 | } |
michael@0 | 5917 | #else |
michael@0 | 5918 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb); |
michael@0 | 5919 | SCTP_DECR_ASOC_COUNT(); |
michael@0 | 5920 | #endif |
michael@0 | 5921 | if (from_inpcbfree == SCTP_NORMAL_PROC) { |
michael@0 | 5922 | if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) { |
michael@0 | 5923 | /* If its NOT the inp_free calling us AND |
michael@0 | 5924 | * sctp_close as been called, we |
michael@0 | 5925 | * call back... |
michael@0 | 5926 | */ |
michael@0 | 5927 | SCTP_INP_RUNLOCK(inp); |
michael@0 | 5928 | /* This will start the kill timer (if we are |
michael@0 | 5929 | * the last one) since we hold an increment yet. But |
michael@0 | 5930 | * this is the only safe way to do this |
michael@0 | 5931 | * since otherwise if the socket closes |
michael@0 | 5932 | * at the same time we are here we might |
michael@0 | 5933 | * collide in the cleanup. |
michael@0 | 5934 | */ |
michael@0 | 5935 | sctp_inpcb_free(inp, |
michael@0 | 5936 | SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE, |
michael@0 | 5937 | SCTP_CALLED_DIRECTLY_NOCMPSET); |
michael@0 | 5938 | SCTP_INP_DECR_REF(inp); |
michael@0 | 5939 | goto out_of; |
michael@0 | 5940 | } else { |
michael@0 | 5941 | /* The socket is still open. */ |
michael@0 | 5942 | SCTP_INP_DECR_REF(inp); |
michael@0 | 5943 | } |
michael@0 | 5944 | } |
michael@0 | 5945 | if (from_inpcbfree == SCTP_NORMAL_PROC) { |
michael@0 | 5946 | SCTP_INP_RUNLOCK(inp); |
michael@0 | 5947 | } |
michael@0 | 5948 | out_of: |
michael@0 | 5949 | /* destroyed the asoc */ |
michael@0 | 5950 | #ifdef SCTP_LOG_CLOSING |
michael@0 | 5951 | sctp_log_closing(inp, NULL, 11); |
michael@0 | 5952 | #endif |
michael@0 | 5953 | return (1); |
michael@0 | 5954 | } |
michael@0 | 5955 | |
michael@0 | 5956 | |
michael@0 | 5957 | |
michael@0 | 5958 | /* |
michael@0 | 5959 | * determine if a destination is "reachable" based upon the addresses bound |
michael@0 | 5960 | * to the current endpoint (e.g. only v4 or v6 currently bound) |
michael@0 | 5961 | */ |
michael@0 | 5962 | /* |
michael@0 | 5963 | * FIX: if we allow assoc-level bindx(), then this needs to be fixed to use |
michael@0 | 5964 | * assoc level v4/v6 flags, as the assoc *may* not have the same address |
michael@0 | 5965 | * types bound as its endpoint |
michael@0 | 5966 | */ |
michael@0 | 5967 | int |
michael@0 | 5968 | sctp_destination_is_reachable(struct sctp_tcb *stcb, struct sockaddr *destaddr) |
michael@0 | 5969 | { |
michael@0 | 5970 | struct sctp_inpcb *inp; |
michael@0 | 5971 | int answer; |
michael@0 | 5972 | |
michael@0 | 5973 | /* |
michael@0 | 5974 | * No locks here, the TCB, in all cases is already locked and an |
michael@0 | 5975 | * assoc is up. There is either a INP lock by the caller applied (in |
michael@0 | 5976 | * asconf case when deleting an address) or NOT in the HB case, |
michael@0 | 5977 | * however if HB then the INP increment is up and the INP will not |
michael@0 | 5978 | * be removed (on top of the fact that we have a TCB lock). So we |
michael@0 | 5979 | * only want to read the sctp_flags, which is either bound-all or |
michael@0 | 5980 | * not.. no protection needed since once an assoc is up you can't be |
michael@0 | 5981 | * changing your binding. |
michael@0 | 5982 | */ |
michael@0 | 5983 | inp = stcb->sctp_ep; |
michael@0 | 5984 | if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { |
michael@0 | 5985 | /* if bound all, destination is not restricted */ |
michael@0 | 5986 | /* |
michael@0 | 5987 | * RRS: Question during lock work: Is this correct? If you |
michael@0 | 5988 | * are bound-all you still might need to obey the V4--V6 |
michael@0 | 5989 | * flags??? IMO this bound-all stuff needs to be removed! |
michael@0 | 5990 | */ |
michael@0 | 5991 | return (1); |
michael@0 | 5992 | } |
michael@0 | 5993 | /* NOTE: all "scope" checks are done when local addresses are added */ |
michael@0 | 5994 | switch (destaddr->sa_family) { |
michael@0 | 5995 | #ifdef INET6 |
michael@0 | 5996 | case AF_INET6: |
michael@0 | 5997 | #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)) |
michael@0 | 5998 | answer = inp->inp_vflag & INP_IPV6; |
michael@0 | 5999 | #else |
michael@0 | 6000 | answer = inp->ip_inp.inp.inp_vflag & INP_IPV6; |
michael@0 | 6001 | #endif |
michael@0 | 6002 | break; |
michael@0 | 6003 | #endif |
michael@0 | 6004 | #ifdef INET |
michael@0 | 6005 | case AF_INET: |
michael@0 | 6006 | #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)) |
michael@0 | 6007 | answer = inp->inp_vflag & INP_IPV4; |
michael@0 | 6008 | #else |
michael@0 | 6009 | answer = inp->ip_inp.inp.inp_vflag & INP_IPV4; |
michael@0 | 6010 | #endif |
michael@0 | 6011 | break; |
michael@0 | 6012 | #endif |
michael@0 | 6013 | #if defined(__Userspace__) |
michael@0 | 6014 | case AF_CONN: |
michael@0 | 6015 | answer = inp->ip_inp.inp.inp_vflag & INP_CONN; |
michael@0 | 6016 | break; |
michael@0 | 6017 | #endif |
michael@0 | 6018 | default: |
michael@0 | 6019 | /* invalid family, so it's unreachable */ |
michael@0 | 6020 | answer = 0; |
michael@0 | 6021 | break; |
michael@0 | 6022 | } |
michael@0 | 6023 | return (answer); |
michael@0 | 6024 | } |
michael@0 | 6025 | |
michael@0 | 6026 | /* |
michael@0 | 6027 | * update the inp_vflags on an endpoint |
michael@0 | 6028 | */ |
michael@0 | 6029 | static void |
michael@0 | 6030 | sctp_update_ep_vflag(struct sctp_inpcb *inp) |
michael@0 | 6031 | { |
michael@0 | 6032 | struct sctp_laddr *laddr; |
michael@0 | 6033 | |
michael@0 | 6034 | /* first clear the flag */ |
michael@0 | 6035 | #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)) |
michael@0 | 6036 | inp->inp_vflag = 0; |
michael@0 | 6037 | #else |
michael@0 | 6038 | inp->ip_inp.inp.inp_vflag = 0; |
michael@0 | 6039 | #endif |
michael@0 | 6040 | /* set the flag based on addresses on the ep list */ |
michael@0 | 6041 | LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { |
michael@0 | 6042 | if (laddr->ifa == NULL) { |
michael@0 | 6043 | SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n", |
michael@0 | 6044 | __FUNCTION__); |
michael@0 | 6045 | continue; |
michael@0 | 6046 | } |
michael@0 | 6047 | |
michael@0 | 6048 | if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) { |
michael@0 | 6049 | continue; |
michael@0 | 6050 | } |
michael@0 | 6051 | switch (laddr->ifa->address.sa.sa_family) { |
michael@0 | 6052 | #ifdef INET6 |
michael@0 | 6053 | case AF_INET6: |
michael@0 | 6054 | #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)) |
michael@0 | 6055 | inp->inp_vflag |= INP_IPV6; |
michael@0 | 6056 | #else |
michael@0 | 6057 | inp->ip_inp.inp.inp_vflag |= INP_IPV6; |
michael@0 | 6058 | #endif |
michael@0 | 6059 | break; |
michael@0 | 6060 | #endif |
michael@0 | 6061 | #ifdef INET |
michael@0 | 6062 | case AF_INET: |
michael@0 | 6063 | #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)) |
michael@0 | 6064 | inp->inp_vflag |= INP_IPV4; |
michael@0 | 6065 | #else |
michael@0 | 6066 | inp->ip_inp.inp.inp_vflag |= INP_IPV4; |
michael@0 | 6067 | #endif |
michael@0 | 6068 | break; |
michael@0 | 6069 | #endif |
michael@0 | 6070 | #if defined(__Userspace__) |
michael@0 | 6071 | case AF_CONN: |
michael@0 | 6072 | inp->ip_inp.inp.inp_vflag |= INP_CONN; |
michael@0 | 6073 | break; |
michael@0 | 6074 | #endif |
michael@0 | 6075 | default: |
michael@0 | 6076 | break; |
michael@0 | 6077 | } |
michael@0 | 6078 | } |
michael@0 | 6079 | } |
michael@0 | 6080 | |
michael@0 | 6081 | /* |
michael@0 | 6082 | * Add the address to the endpoint local address list There is nothing to be |
michael@0 | 6083 | * done if we are bound to all addresses |
michael@0 | 6084 | */ |
michael@0 | 6085 | void |
michael@0 | 6086 | sctp_add_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa, uint32_t action) |
michael@0 | 6087 | { |
michael@0 | 6088 | struct sctp_laddr *laddr; |
michael@0 | 6089 | int fnd, error = 0; |
michael@0 | 6090 | |
michael@0 | 6091 | fnd = 0; |
michael@0 | 6092 | |
michael@0 | 6093 | if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { |
michael@0 | 6094 | /* You are already bound to all. You have it already */ |
michael@0 | 6095 | return; |
michael@0 | 6096 | } |
michael@0 | 6097 | #ifdef INET6 |
michael@0 | 6098 | if (ifa->address.sa.sa_family == AF_INET6) { |
michael@0 | 6099 | if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) { |
michael@0 | 6100 | /* Can't bind a non-useable addr. */ |
michael@0 | 6101 | return; |
michael@0 | 6102 | } |
michael@0 | 6103 | } |
michael@0 | 6104 | #endif |
michael@0 | 6105 | /* first, is it already present? */ |
michael@0 | 6106 | LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { |
michael@0 | 6107 | if (laddr->ifa == ifa) { |
michael@0 | 6108 | fnd = 1; |
michael@0 | 6109 | break; |
michael@0 | 6110 | } |
michael@0 | 6111 | } |
michael@0 | 6112 | |
michael@0 | 6113 | if (fnd == 0) { |
michael@0 | 6114 | /* Not in the ep list */ |
michael@0 | 6115 | error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, action); |
michael@0 | 6116 | if (error != 0) |
michael@0 | 6117 | return; |
michael@0 | 6118 | inp->laddr_count++; |
michael@0 | 6119 | /* update inp_vflag flags */ |
michael@0 | 6120 | switch (ifa->address.sa.sa_family) { |
michael@0 | 6121 | #ifdef INET6 |
michael@0 | 6122 | case AF_INET6: |
michael@0 | 6123 | #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)) |
michael@0 | 6124 | inp->inp_vflag |= INP_IPV6; |
michael@0 | 6125 | #else |
michael@0 | 6126 | inp->ip_inp.inp.inp_vflag |= INP_IPV6; |
michael@0 | 6127 | #endif |
michael@0 | 6128 | break; |
michael@0 | 6129 | #endif |
michael@0 | 6130 | #ifdef INET |
michael@0 | 6131 | case AF_INET: |
michael@0 | 6132 | #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Windows__) || defined(__Userspace__)) |
michael@0 | 6133 | inp->inp_vflag |= INP_IPV4; |
michael@0 | 6134 | #else |
michael@0 | 6135 | inp->ip_inp.inp.inp_vflag |= INP_IPV4; |
michael@0 | 6136 | #endif |
michael@0 | 6137 | break; |
michael@0 | 6138 | #endif |
michael@0 | 6139 | #if defined(__Userspace__) |
michael@0 | 6140 | case AF_CONN: |
michael@0 | 6141 | inp->ip_inp.inp.inp_vflag |= INP_CONN; |
michael@0 | 6142 | break; |
michael@0 | 6143 | #endif |
michael@0 | 6144 | default: |
michael@0 | 6145 | break; |
michael@0 | 6146 | } |
michael@0 | 6147 | } |
michael@0 | 6148 | return; |
michael@0 | 6149 | } |
michael@0 | 6150 | |
michael@0 | 6151 | |
michael@0 | 6152 | /* |
michael@0 | 6153 | * select a new (hopefully reachable) destination net (should only be used |
michael@0 | 6154 | * when we deleted an ep addr that is the only usable source address to reach |
michael@0 | 6155 | * the destination net) |
michael@0 | 6156 | */ |
michael@0 | 6157 | static void |
michael@0 | 6158 | sctp_select_primary_destination(struct sctp_tcb *stcb) |
michael@0 | 6159 | { |
michael@0 | 6160 | struct sctp_nets *net; |
michael@0 | 6161 | |
michael@0 | 6162 | TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { |
michael@0 | 6163 | /* for now, we'll just pick the first reachable one we find */ |
michael@0 | 6164 | if (net->dest_state & SCTP_ADDR_UNCONFIRMED) |
michael@0 | 6165 | continue; |
michael@0 | 6166 | if (sctp_destination_is_reachable(stcb, |
michael@0 | 6167 | (struct sockaddr *)&net->ro._l_addr)) { |
michael@0 | 6168 | /* found a reachable destination */ |
michael@0 | 6169 | stcb->asoc.primary_destination = net; |
michael@0 | 6170 | } |
michael@0 | 6171 | } |
michael@0 | 6172 | /* I can't there from here! ...we're gonna die shortly... */ |
michael@0 | 6173 | } |
michael@0 | 6174 | |
michael@0 | 6175 | |
michael@0 | 6176 | /* |
michael@0 | 6177 | * Delete the address from the endpoint local address list There is nothing |
michael@0 | 6178 | * to be done if we are bound to all addresses |
michael@0 | 6179 | */ |
michael@0 | 6180 | void |
michael@0 | 6181 | sctp_del_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa) |
michael@0 | 6182 | { |
michael@0 | 6183 | struct sctp_laddr *laddr; |
michael@0 | 6184 | int fnd; |
michael@0 | 6185 | |
michael@0 | 6186 | fnd = 0; |
michael@0 | 6187 | if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) { |
michael@0 | 6188 | /* You are already bound to all. You have it already */ |
michael@0 | 6189 | return; |
michael@0 | 6190 | } |
michael@0 | 6191 | LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) { |
michael@0 | 6192 | if (laddr->ifa == ifa) { |
michael@0 | 6193 | fnd = 1; |
michael@0 | 6194 | break; |
michael@0 | 6195 | } |
michael@0 | 6196 | } |
michael@0 | 6197 | if (fnd && (inp->laddr_count < 2)) { |
michael@0 | 6198 | /* can't delete unless there are at LEAST 2 addresses */ |
michael@0 | 6199 | return; |
michael@0 | 6200 | } |
michael@0 | 6201 | if (fnd) { |
michael@0 | 6202 | /* |
michael@0 | 6203 | * clean up any use of this address go through our |
michael@0 | 6204 | * associations and clear any last_used_address that match |
michael@0 | 6205 | * this one for each assoc, see if a new primary_destination |
michael@0 | 6206 | * is needed |
michael@0 | 6207 | */ |
michael@0 | 6208 | struct sctp_tcb *stcb; |
michael@0 | 6209 | |
michael@0 | 6210 | /* clean up "next_addr_touse" */ |
michael@0 | 6211 | if (inp->next_addr_touse == laddr) |
michael@0 | 6212 | /* delete this address */ |
michael@0 | 6213 | inp->next_addr_touse = NULL; |
michael@0 | 6214 | |
michael@0 | 6215 | /* clean up "last_used_address" */ |
michael@0 | 6216 | LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { |
michael@0 | 6217 | struct sctp_nets *net; |
michael@0 | 6218 | SCTP_TCB_LOCK(stcb); |
michael@0 | 6219 | if (stcb->asoc.last_used_address == laddr) |
michael@0 | 6220 | /* delete this address */ |
michael@0 | 6221 | stcb->asoc.last_used_address = NULL; |
michael@0 | 6222 | /* Now spin through all the nets and purge any ref to laddr */ |
michael@0 | 6223 | TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { |
michael@0 | 6224 | if (net->ro._s_addr && |
michael@0 | 6225 | (net->ro._s_addr->ifa == laddr->ifa)) { |
michael@0 | 6226 | /* Yep, purge src address selected */ |
michael@0 | 6227 | sctp_rtentry_t *rt; |
michael@0 | 6228 | |
michael@0 | 6229 | /* delete this address if cached */ |
michael@0 | 6230 | rt = net->ro.ro_rt; |
michael@0 | 6231 | if (rt != NULL) { |
michael@0 | 6232 | RTFREE(rt); |
michael@0 | 6233 | net->ro.ro_rt = NULL; |
michael@0 | 6234 | } |
michael@0 | 6235 | sctp_free_ifa(net->ro._s_addr); |
michael@0 | 6236 | net->ro._s_addr = NULL; |
michael@0 | 6237 | net->src_addr_selected = 0; |
michael@0 | 6238 | } |
michael@0 | 6239 | } |
michael@0 | 6240 | SCTP_TCB_UNLOCK(stcb); |
michael@0 | 6241 | } /* for each tcb */ |
michael@0 | 6242 | /* remove it from the ep list */ |
michael@0 | 6243 | sctp_remove_laddr(laddr); |
michael@0 | 6244 | inp->laddr_count--; |
michael@0 | 6245 | /* update inp_vflag flags */ |
michael@0 | 6246 | sctp_update_ep_vflag(inp); |
michael@0 | 6247 | } |
michael@0 | 6248 | return; |
michael@0 | 6249 | } |
michael@0 | 6250 | |
michael@0 | 6251 | /* |
michael@0 | 6252 | * Add the address to the TCB local address restricted list. |
michael@0 | 6253 | * This is a "pending" address list (eg. addresses waiting for an |
michael@0 | 6254 | * ASCONF-ACK response) and cannot be used as a valid source address. |
michael@0 | 6255 | */ |
michael@0 | 6256 | void |
michael@0 | 6257 | sctp_add_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa) |
michael@0 | 6258 | { |
michael@0 | 6259 | struct sctp_laddr *laddr; |
michael@0 | 6260 | struct sctpladdr *list; |
michael@0 | 6261 | |
michael@0 | 6262 | /* |
michael@0 | 6263 | * Assumes TCB is locked.. and possibly the INP. May need to |
michael@0 | 6264 | * confirm/fix that if we need it and is not the case. |
michael@0 | 6265 | */ |
michael@0 | 6266 | list = &stcb->asoc.sctp_restricted_addrs; |
michael@0 | 6267 | |
michael@0 | 6268 | #ifdef INET6 |
michael@0 | 6269 | if (ifa->address.sa.sa_family == AF_INET6) { |
michael@0 | 6270 | if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) { |
michael@0 | 6271 | /* Can't bind a non-existent addr. */ |
michael@0 | 6272 | return; |
michael@0 | 6273 | } |
michael@0 | 6274 | } |
michael@0 | 6275 | #endif |
michael@0 | 6276 | /* does the address already exist? */ |
michael@0 | 6277 | LIST_FOREACH(laddr, list, sctp_nxt_addr) { |
michael@0 | 6278 | if (laddr->ifa == ifa) { |
michael@0 | 6279 | return; |
michael@0 | 6280 | } |
michael@0 | 6281 | } |
michael@0 | 6282 | |
michael@0 | 6283 | /* add to the list */ |
michael@0 | 6284 | (void)sctp_insert_laddr(list, ifa, 0); |
michael@0 | 6285 | return; |
michael@0 | 6286 | } |
michael@0 | 6287 | |
michael@0 | 6288 | /* |
michael@0 | 6289 | * insert an laddr entry with the given ifa for the desired list |
michael@0 | 6290 | */ |
michael@0 | 6291 | int |
michael@0 | 6292 | sctp_insert_laddr(struct sctpladdr *list, struct sctp_ifa *ifa, uint32_t act) |
michael@0 | 6293 | { |
michael@0 | 6294 | struct sctp_laddr *laddr; |
michael@0 | 6295 | |
michael@0 | 6296 | laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr); |
michael@0 | 6297 | if (laddr == NULL) { |
michael@0 | 6298 | /* out of memory? */ |
michael@0 | 6299 | SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL); |
michael@0 | 6300 | return (EINVAL); |
michael@0 | 6301 | } |
michael@0 | 6302 | SCTP_INCR_LADDR_COUNT(); |
michael@0 | 6303 | bzero(laddr, sizeof(*laddr)); |
michael@0 | 6304 | (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time); |
michael@0 | 6305 | laddr->ifa = ifa; |
michael@0 | 6306 | laddr->action = act; |
michael@0 | 6307 | atomic_add_int(&ifa->refcount, 1); |
michael@0 | 6308 | /* insert it */ |
michael@0 | 6309 | LIST_INSERT_HEAD(list, laddr, sctp_nxt_addr); |
michael@0 | 6310 | |
michael@0 | 6311 | return (0); |
michael@0 | 6312 | } |
michael@0 | 6313 | |
michael@0 | 6314 | /* |
michael@0 | 6315 | * Remove an laddr entry from the local address list (on an assoc) |
michael@0 | 6316 | */ |
michael@0 | 6317 | void |
michael@0 | 6318 | sctp_remove_laddr(struct sctp_laddr *laddr) |
michael@0 | 6319 | { |
michael@0 | 6320 | |
michael@0 | 6321 | /* remove from the list */ |
michael@0 | 6322 | LIST_REMOVE(laddr, sctp_nxt_addr); |
michael@0 | 6323 | sctp_free_ifa(laddr->ifa); |
michael@0 | 6324 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), laddr); |
michael@0 | 6325 | SCTP_DECR_LADDR_COUNT(); |
michael@0 | 6326 | } |
michael@0 | 6327 | |
michael@0 | 6328 | /* |
michael@0 | 6329 | * Remove a local address from the TCB local address restricted list |
michael@0 | 6330 | */ |
michael@0 | 6331 | void |
michael@0 | 6332 | sctp_del_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa) |
michael@0 | 6333 | { |
michael@0 | 6334 | struct sctp_inpcb *inp; |
michael@0 | 6335 | struct sctp_laddr *laddr; |
michael@0 | 6336 | |
michael@0 | 6337 | /* |
michael@0 | 6338 | * This is called by asconf work. It is assumed that a) The TCB is |
michael@0 | 6339 | * locked and b) The INP is locked. This is true in as much as I can |
michael@0 | 6340 | * trace through the entry asconf code where I did these locks. |
michael@0 | 6341 | * Again, the ASCONF code is a bit different in that it does lock |
michael@0 | 6342 | * the INP during its work often times. This must be since we don't |
michael@0 | 6343 | * want other proc's looking up things while what they are looking |
michael@0 | 6344 | * up is changing :-D |
michael@0 | 6345 | */ |
michael@0 | 6346 | |
michael@0 | 6347 | inp = stcb->sctp_ep; |
michael@0 | 6348 | /* if subset bound and don't allow ASCONF's, can't delete last */ |
michael@0 | 6349 | if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) && |
michael@0 | 6350 | sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF)) { |
michael@0 | 6351 | if (stcb->sctp_ep->laddr_count < 2) { |
michael@0 | 6352 | /* can't delete last address */ |
michael@0 | 6353 | return; |
michael@0 | 6354 | } |
michael@0 | 6355 | } |
michael@0 | 6356 | LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) { |
michael@0 | 6357 | /* remove the address if it exists */ |
michael@0 | 6358 | if (laddr->ifa == NULL) |
michael@0 | 6359 | continue; |
michael@0 | 6360 | if (laddr->ifa == ifa) { |
michael@0 | 6361 | sctp_remove_laddr(laddr); |
michael@0 | 6362 | return; |
michael@0 | 6363 | } |
michael@0 | 6364 | } |
michael@0 | 6365 | |
michael@0 | 6366 | /* address not found! */ |
michael@0 | 6367 | return; |
michael@0 | 6368 | } |
michael@0 | 6369 | |
michael@0 | 6370 | #if defined(__FreeBSD__) |
michael@0 | 6371 | /* |
michael@0 | 6372 | * Temporarily remove for __APPLE__ until we use the Tiger equivalents |
michael@0 | 6373 | */ |
michael@0 | 6374 | /* sysctl */ |
michael@0 | 6375 | static int sctp_max_number_of_assoc = SCTP_MAX_NUM_OF_ASOC; |
michael@0 | 6376 | static int sctp_scale_up_for_address = SCTP_SCALE_FOR_ADDR; |
michael@0 | 6377 | #endif /* FreeBSD || APPLE */ |
michael@0 | 6378 | |
michael@0 | 6379 | |
michael@0 | 6380 | |
michael@0 | 6381 | #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP) |
michael@0 | 6382 | struct sctp_mcore_ctrl *sctp_mcore_workers = NULL; |
michael@0 | 6383 | int *sctp_cpuarry = NULL; |
michael@0 | 6384 | void |
michael@0 | 6385 | sctp_queue_to_mcore(struct mbuf *m, int off, int cpu_to_use) |
michael@0 | 6386 | { |
michael@0 | 6387 | /* Queue a packet to a processor for the specified core */ |
michael@0 | 6388 | struct sctp_mcore_queue *qent; |
michael@0 | 6389 | struct sctp_mcore_ctrl *wkq; |
michael@0 | 6390 | int need_wake = 0; |
michael@0 | 6391 | if (sctp_mcore_workers == NULL) { |
michael@0 | 6392 | /* Something went way bad during setup */ |
michael@0 | 6393 | sctp_input_with_port(m, off, 0); |
michael@0 | 6394 | return; |
michael@0 | 6395 | } |
michael@0 | 6396 | SCTP_MALLOC(qent, struct sctp_mcore_queue *, |
michael@0 | 6397 | (sizeof(struct sctp_mcore_queue)), |
michael@0 | 6398 | SCTP_M_MCORE); |
michael@0 | 6399 | if (qent == NULL) { |
michael@0 | 6400 | /* This is trouble */ |
michael@0 | 6401 | sctp_input_with_port(m, off, 0); |
michael@0 | 6402 | return; |
michael@0 | 6403 | } |
michael@0 | 6404 | #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 |
michael@0 | 6405 | qent->vn = curvnet; |
michael@0 | 6406 | #endif |
michael@0 | 6407 | qent->m = m; |
michael@0 | 6408 | qent->off = off; |
michael@0 | 6409 | qent->v6 = 0; |
michael@0 | 6410 | wkq = &sctp_mcore_workers[cpu_to_use]; |
michael@0 | 6411 | SCTP_MCORE_QLOCK(wkq); |
michael@0 | 6412 | |
michael@0 | 6413 | TAILQ_INSERT_TAIL(&wkq->que, qent, next); |
michael@0 | 6414 | if (wkq->running == 0) { |
michael@0 | 6415 | need_wake = 1; |
michael@0 | 6416 | } |
michael@0 | 6417 | SCTP_MCORE_QUNLOCK(wkq); |
michael@0 | 6418 | if (need_wake) { |
michael@0 | 6419 | wakeup(&wkq->running); |
michael@0 | 6420 | } |
michael@0 | 6421 | } |
michael@0 | 6422 | |
michael@0 | 6423 | static void |
michael@0 | 6424 | sctp_mcore_thread(void *arg) |
michael@0 | 6425 | { |
michael@0 | 6426 | |
michael@0 | 6427 | struct sctp_mcore_ctrl *wkq; |
michael@0 | 6428 | struct sctp_mcore_queue *qent; |
michael@0 | 6429 | |
michael@0 | 6430 | wkq = (struct sctp_mcore_ctrl *)arg; |
michael@0 | 6431 | struct mbuf *m; |
michael@0 | 6432 | int off, v6; |
michael@0 | 6433 | |
michael@0 | 6434 | /* Wait for first tickle */ |
michael@0 | 6435 | SCTP_MCORE_LOCK(wkq); |
michael@0 | 6436 | wkq->running = 0; |
michael@0 | 6437 | msleep(&wkq->running, |
michael@0 | 6438 | &wkq->core_mtx, |
michael@0 | 6439 | 0, "wait for pkt", 0); |
michael@0 | 6440 | SCTP_MCORE_UNLOCK(wkq); |
michael@0 | 6441 | |
michael@0 | 6442 | /* Bind to our cpu */ |
michael@0 | 6443 | thread_lock(curthread); |
michael@0 | 6444 | sched_bind(curthread, wkq->cpuid); |
michael@0 | 6445 | thread_unlock(curthread); |
michael@0 | 6446 | |
michael@0 | 6447 | /* Now lets start working */ |
michael@0 | 6448 | SCTP_MCORE_LOCK(wkq); |
michael@0 | 6449 | /* Now grab lock and go */ |
michael@0 | 6450 | for (;;) { |
michael@0 | 6451 | SCTP_MCORE_QLOCK(wkq); |
michael@0 | 6452 | skip_sleep: |
michael@0 | 6453 | wkq->running = 1; |
michael@0 | 6454 | qent = TAILQ_FIRST(&wkq->que); |
michael@0 | 6455 | if (qent) { |
michael@0 | 6456 | TAILQ_REMOVE(&wkq->que, qent, next); |
michael@0 | 6457 | SCTP_MCORE_QUNLOCK(wkq); |
michael@0 | 6458 | #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 |
michael@0 | 6459 | CURVNET_SET(qent->vn); |
michael@0 | 6460 | #endif |
michael@0 | 6461 | m = qent->m; |
michael@0 | 6462 | off = qent->off; |
michael@0 | 6463 | v6 = qent->v6; |
michael@0 | 6464 | SCTP_FREE(qent, SCTP_M_MCORE); |
michael@0 | 6465 | if (v6 == 0) { |
michael@0 | 6466 | sctp_input_with_port(m, off, 0); |
michael@0 | 6467 | } else { |
michael@0 | 6468 | SCTP_PRINTF("V6 not yet supported\n"); |
michael@0 | 6469 | sctp_m_freem(m); |
michael@0 | 6470 | } |
michael@0 | 6471 | #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 |
michael@0 | 6472 | CURVNET_RESTORE(); |
michael@0 | 6473 | #endif |
michael@0 | 6474 | SCTP_MCORE_QLOCK(wkq); |
michael@0 | 6475 | } |
michael@0 | 6476 | wkq->running = 0; |
michael@0 | 6477 | if (!TAILQ_EMPTY(&wkq->que)) { |
michael@0 | 6478 | goto skip_sleep; |
michael@0 | 6479 | } |
michael@0 | 6480 | SCTP_MCORE_QUNLOCK(wkq); |
michael@0 | 6481 | msleep(&wkq->running, |
michael@0 | 6482 | &wkq->core_mtx, |
michael@0 | 6483 | 0, "wait for pkt", 0); |
michael@0 | 6484 | } |
michael@0 | 6485 | } |
michael@0 | 6486 | |
michael@0 | 6487 | static void |
michael@0 | 6488 | sctp_startup_mcore_threads(void) |
michael@0 | 6489 | { |
michael@0 | 6490 | int i, cpu; |
michael@0 | 6491 | |
michael@0 | 6492 | if (mp_ncpus == 1) |
michael@0 | 6493 | return; |
michael@0 | 6494 | |
michael@0 | 6495 | if (sctp_mcore_workers != NULL) { |
michael@0 | 6496 | /* Already been here in some previous |
michael@0 | 6497 | * vnet? |
michael@0 | 6498 | */ |
michael@0 | 6499 | return; |
michael@0 | 6500 | } |
michael@0 | 6501 | SCTP_MALLOC(sctp_mcore_workers, struct sctp_mcore_ctrl *, |
michael@0 | 6502 | ((mp_maxid+1) * sizeof(struct sctp_mcore_ctrl)), |
michael@0 | 6503 | SCTP_M_MCORE); |
michael@0 | 6504 | if (sctp_mcore_workers == NULL) { |
michael@0 | 6505 | /* TSNH I hope */ |
michael@0 | 6506 | return; |
michael@0 | 6507 | } |
michael@0 | 6508 | memset(sctp_mcore_workers, 0 , ((mp_maxid+1) * |
michael@0 | 6509 | sizeof(struct sctp_mcore_ctrl))); |
michael@0 | 6510 | /* Init the structures */ |
michael@0 | 6511 | for (i = 0; i<=mp_maxid; i++) { |
michael@0 | 6512 | TAILQ_INIT(&sctp_mcore_workers[i].que); |
michael@0 | 6513 | SCTP_MCORE_LOCK_INIT(&sctp_mcore_workers[i]); |
michael@0 | 6514 | SCTP_MCORE_QLOCK_INIT(&sctp_mcore_workers[i]); |
michael@0 | 6515 | sctp_mcore_workers[i].cpuid = i; |
michael@0 | 6516 | } |
michael@0 | 6517 | if (sctp_cpuarry == NULL) { |
michael@0 | 6518 | SCTP_MALLOC(sctp_cpuarry, int *, |
michael@0 | 6519 | (mp_ncpus * sizeof(int)), |
michael@0 | 6520 | SCTP_M_MCORE); |
michael@0 | 6521 | i = 0; |
michael@0 | 6522 | CPU_FOREACH(cpu) { |
michael@0 | 6523 | sctp_cpuarry[i] = cpu; |
michael@0 | 6524 | i++; |
michael@0 | 6525 | } |
michael@0 | 6526 | } |
michael@0 | 6527 | |
michael@0 | 6528 | /* Now start them all */ |
michael@0 | 6529 | CPU_FOREACH(cpu) { |
michael@0 | 6530 | #if __FreeBSD_version <= 701000 |
michael@0 | 6531 | (void)kthread_create(sctp_mcore_thread, |
michael@0 | 6532 | (void *)&sctp_mcore_workers[cpu], |
michael@0 | 6533 | &sctp_mcore_workers[cpu].thread_proc, |
michael@0 | 6534 | RFPROC, |
michael@0 | 6535 | SCTP_KTHREAD_PAGES, |
michael@0 | 6536 | SCTP_MCORE_NAME); |
michael@0 | 6537 | |
michael@0 | 6538 | #else |
michael@0 | 6539 | (void)kproc_create(sctp_mcore_thread, |
michael@0 | 6540 | (void *)&sctp_mcore_workers[cpu], |
michael@0 | 6541 | &sctp_mcore_workers[cpu].thread_proc, |
michael@0 | 6542 | RFPROC, |
michael@0 | 6543 | SCTP_KTHREAD_PAGES, |
michael@0 | 6544 | SCTP_MCORE_NAME); |
michael@0 | 6545 | #endif |
michael@0 | 6546 | |
michael@0 | 6547 | } |
michael@0 | 6548 | } |
michael@0 | 6549 | #endif |
michael@0 | 6550 | #if defined(__FreeBSD__) && __FreeBSD_cc_version >= 1100000 |
michael@0 | 6551 | static struct mbuf * |
michael@0 | 6552 | sctp_netisr_hdlr(struct mbuf *m, uintptr_t source) |
michael@0 | 6553 | { |
michael@0 | 6554 | struct ip *ip; |
michael@0 | 6555 | struct sctphdr *sh; |
michael@0 | 6556 | int offset; |
michael@0 | 6557 | uint32_t flowid, tag; |
michael@0 | 6558 | |
michael@0 | 6559 | /* |
michael@0 | 6560 | * No flow id built by lower layers fix it so we |
michael@0 | 6561 | * create one. |
michael@0 | 6562 | */ |
michael@0 | 6563 | ip = mtod(m, struct ip *); |
michael@0 | 6564 | offset = (ip->ip_hl << 2) + sizeof(struct sctphdr); |
michael@0 | 6565 | if (SCTP_BUF_LEN(m) < offset) { |
michael@0 | 6566 | if ((m = m_pullup(m, offset)) == NULL) { |
michael@0 | 6567 | SCTP_STAT_INCR(sctps_hdrops); |
michael@0 | 6568 | return (NULL); |
michael@0 | 6569 | } |
michael@0 | 6570 | ip = mtod(m, struct ip *); |
michael@0 | 6571 | } |
michael@0 | 6572 | sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2)); |
michael@0 | 6573 | tag = htonl(sh->v_tag); |
michael@0 | 6574 | flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port); |
michael@0 | 6575 | m->m_pkthdr.flowid = flowid; |
michael@0 | 6576 | m->m_flags |= M_FLOWID; |
michael@0 | 6577 | return (m); |
michael@0 | 6578 | } |
michael@0 | 6579 | #endif |
michael@0 | 6580 | |
michael@0 | 6581 | void |
michael@0 | 6582 | sctp_pcb_init() |
michael@0 | 6583 | { |
michael@0 | 6584 | /* |
michael@0 | 6585 | * SCTP initialization for the PCB structures should be called by |
michael@0 | 6586 | * the sctp_init() funciton. |
michael@0 | 6587 | */ |
michael@0 | 6588 | int i; |
michael@0 | 6589 | struct timeval tv; |
michael@0 | 6590 | |
michael@0 | 6591 | if (SCTP_BASE_VAR(sctp_pcb_initialized) != 0) { |
michael@0 | 6592 | /* error I was called twice */ |
michael@0 | 6593 | return; |
michael@0 | 6594 | } |
michael@0 | 6595 | SCTP_BASE_VAR(sctp_pcb_initialized) = 1; |
michael@0 | 6596 | |
michael@0 | 6597 | #if defined(SCTP_LOCAL_TRACE_BUF) |
michael@0 | 6598 | #if defined(__Windows__) |
michael@0 | 6599 | if (SCTP_BASE_SYSCTL(sctp_log) != NULL) { |
michael@0 | 6600 | bzero(SCTP_BASE_SYSCTL(sctp_log), sizeof(struct sctp_log)); |
michael@0 | 6601 | } |
michael@0 | 6602 | #else |
michael@0 | 6603 | bzero(&SCTP_BASE_SYSCTL(sctp_log), sizeof(struct sctp_log)); |
michael@0 | 6604 | #endif |
michael@0 | 6605 | #endif |
michael@0 | 6606 | #if defined(__FreeBSD__) && defined(SMP) && defined(SCTP_USE_PERCPU_STAT) |
michael@0 | 6607 | SCTP_MALLOC(SCTP_BASE_STATS, struct sctpstat *, |
michael@0 | 6608 | ((mp_maxid+1) * sizeof(struct sctpstat)), |
michael@0 | 6609 | SCTP_M_MCORE); |
michael@0 | 6610 | #endif |
michael@0 | 6611 | (void)SCTP_GETTIME_TIMEVAL(&tv); |
michael@0 | 6612 | #if defined(__FreeBSD__) && defined(SMP) && defined(SCTP_USE_PERCPU_STAT) |
michael@0 | 6613 | bzero(SCTP_BASE_STATS, (sizeof(struct sctpstat) * (mp_maxid+1))); |
michael@0 | 6614 | SCTP_BASE_STATS[PCPU_GET(cpuid)].sctps_discontinuitytime.tv_sec = (uint32_t)tv.tv_sec; |
michael@0 | 6615 | SCTP_BASE_STATS[PCPU_GET(cpuid)].sctps_discontinuitytime.tv_usec = (uint32_t)tv.tv_usec; |
michael@0 | 6616 | #else |
michael@0 | 6617 | bzero(&SCTP_BASE_STATS, sizeof(struct sctpstat)); |
michael@0 | 6618 | SCTP_BASE_STAT(sctps_discontinuitytime).tv_sec = (uint32_t)tv.tv_sec; |
michael@0 | 6619 | SCTP_BASE_STAT(sctps_discontinuitytime).tv_usec = (uint32_t)tv.tv_usec; |
michael@0 | 6620 | #endif |
michael@0 | 6621 | /* init the empty list of (All) Endpoints */ |
michael@0 | 6622 | LIST_INIT(&SCTP_BASE_INFO(listhead)); |
michael@0 | 6623 | #if defined(__APPLE__) |
michael@0 | 6624 | LIST_INIT(&SCTP_BASE_INFO(inplisthead)); |
michael@0 | 6625 | #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) || defined(APPLE_LION) || defined(APPLE_MOUNTAINLION) |
michael@0 | 6626 | SCTP_BASE_INFO(sctbinfo).listhead = &SCTP_BASE_INFO(inplisthead); |
michael@0 | 6627 | SCTP_BASE_INFO(sctbinfo).mtx_grp_attr = lck_grp_attr_alloc_init(); |
michael@0 | 6628 | lck_grp_attr_setdefault(SCTP_BASE_INFO(sctbinfo).mtx_grp_attr); |
michael@0 | 6629 | SCTP_BASE_INFO(sctbinfo).mtx_grp = lck_grp_alloc_init("sctppcb", SCTP_BASE_INFO(sctbinfo).mtx_grp_attr); |
michael@0 | 6630 | SCTP_BASE_INFO(sctbinfo).mtx_attr = lck_attr_alloc_init(); |
michael@0 | 6631 | lck_attr_setdefault(SCTP_BASE_INFO(sctbinfo).mtx_attr); |
michael@0 | 6632 | #else |
michael@0 | 6633 | SCTP_BASE_INFO(sctbinfo).ipi_listhead = &SCTP_BASE_INFO(inplisthead); |
michael@0 | 6634 | SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr = lck_grp_attr_alloc_init(); |
michael@0 | 6635 | lck_grp_attr_setdefault(SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr); |
michael@0 | 6636 | SCTP_BASE_INFO(sctbinfo).ipi_lock_grp = lck_grp_alloc_init("sctppcb", SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr); |
michael@0 | 6637 | SCTP_BASE_INFO(sctbinfo).ipi_lock_attr = lck_attr_alloc_init(); |
michael@0 | 6638 | lck_attr_setdefault(SCTP_BASE_INFO(sctbinfo).ipi_lock_attr); |
michael@0 | 6639 | #endif |
michael@0 | 6640 | #if !defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) && !defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION) |
michael@0 | 6641 | SCTP_BASE_INFO(sctbinfo).ipi_gc = sctp_gc; |
michael@0 | 6642 | in_pcbinfo_attach(&SCTP_BASE_INFO(sctbinfo)); |
michael@0 | 6643 | #endif |
michael@0 | 6644 | #endif |
michael@0 | 6645 | |
michael@0 | 6646 | |
michael@0 | 6647 | /* init the hash table of endpoints */ |
michael@0 | 6648 | #if defined(__FreeBSD__) |
michael@0 | 6649 | #if defined(__FreeBSD_cc_version) && __FreeBSD_cc_version >= 440000 |
michael@0 | 6650 | TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", &SCTP_BASE_SYSCTL(sctp_hashtblsize)); |
michael@0 | 6651 | TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", &SCTP_BASE_SYSCTL(sctp_pcbtblsize)); |
michael@0 | 6652 | TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", &SCTP_BASE_SYSCTL(sctp_chunkscale)); |
michael@0 | 6653 | #else |
michael@0 | 6654 | TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", SCTP_TCBHASHSIZE, |
michael@0 | 6655 | SCTP_BASE_SYSCTL(sctp_hashtblsize)); |
michael@0 | 6656 | TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", SCTP_PCBHASHSIZE, |
michael@0 | 6657 | SCTP_BASE_SYSCTL(sctp_pcbtblsize)); |
michael@0 | 6658 | TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", SCTP_CHUNKQUEUE_SCALE, |
michael@0 | 6659 | SCTP_BASE_SYSCTL(sctp_chunkscale)); |
michael@0 | 6660 | #endif |
michael@0 | 6661 | #endif |
michael@0 | 6662 | SCTP_BASE_INFO(sctp_asochash) = SCTP_HASH_INIT((SCTP_BASE_SYSCTL(sctp_hashtblsize) * 31), |
michael@0 | 6663 | &SCTP_BASE_INFO(hashasocmark)); |
michael@0 | 6664 | SCTP_BASE_INFO(sctp_ephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize), |
michael@0 | 6665 | &SCTP_BASE_INFO(hashmark)); |
michael@0 | 6666 | SCTP_BASE_INFO(sctp_tcpephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize), |
michael@0 | 6667 | &SCTP_BASE_INFO(hashtcpmark)); |
michael@0 | 6668 | SCTP_BASE_INFO(hashtblsize) = SCTP_BASE_SYSCTL(sctp_hashtblsize); |
michael@0 | 6669 | |
michael@0 | 6670 | |
michael@0 | 6671 | SCTP_BASE_INFO(sctp_vrfhash) = SCTP_HASH_INIT(SCTP_SIZE_OF_VRF_HASH, |
michael@0 | 6672 | &SCTP_BASE_INFO(hashvrfmark)); |
michael@0 | 6673 | |
michael@0 | 6674 | SCTP_BASE_INFO(vrf_ifn_hash) = SCTP_HASH_INIT(SCTP_VRF_IFN_HASH_SIZE, |
michael@0 | 6675 | &SCTP_BASE_INFO(vrf_ifn_hashmark)); |
michael@0 | 6676 | /* init the zones */ |
michael@0 | 6677 | /* |
michael@0 | 6678 | * FIX ME: Should check for NULL returns, but if it does fail we are |
michael@0 | 6679 | * doomed to panic anyways... add later maybe. |
michael@0 | 6680 | */ |
michael@0 | 6681 | SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_ep), "sctp_ep", |
michael@0 | 6682 | sizeof(struct sctp_inpcb), maxsockets); |
michael@0 | 6683 | |
michael@0 | 6684 | SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asoc), "sctp_asoc", |
michael@0 | 6685 | sizeof(struct sctp_tcb), sctp_max_number_of_assoc); |
michael@0 | 6686 | |
michael@0 | 6687 | SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_laddr), "sctp_laddr", |
michael@0 | 6688 | sizeof(struct sctp_laddr), |
michael@0 | 6689 | (sctp_max_number_of_assoc * sctp_scale_up_for_address)); |
michael@0 | 6690 | |
michael@0 | 6691 | SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_net), "sctp_raddr", |
michael@0 | 6692 | sizeof(struct sctp_nets), |
michael@0 | 6693 | (sctp_max_number_of_assoc * sctp_scale_up_for_address)); |
michael@0 | 6694 | |
michael@0 | 6695 | SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_chunk), "sctp_chunk", |
michael@0 | 6696 | sizeof(struct sctp_tmit_chunk), |
michael@0 | 6697 | (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale))); |
michael@0 | 6698 | |
michael@0 | 6699 | SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_readq), "sctp_readq", |
michael@0 | 6700 | sizeof(struct sctp_queued_to_read), |
michael@0 | 6701 | (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale))); |
michael@0 | 6702 | |
michael@0 | 6703 | SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_strmoq), "sctp_stream_msg_out", |
michael@0 | 6704 | sizeof(struct sctp_stream_queue_pending), |
michael@0 | 6705 | (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale))); |
michael@0 | 6706 | |
michael@0 | 6707 | SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf), "sctp_asconf", |
michael@0 | 6708 | sizeof(struct sctp_asconf), |
michael@0 | 6709 | (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale))); |
michael@0 | 6710 | |
michael@0 | 6711 | SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf_ack), "sctp_asconf_ack", |
michael@0 | 6712 | sizeof(struct sctp_asconf_ack), |
michael@0 | 6713 | (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale))); |
michael@0 | 6714 | |
michael@0 | 6715 | |
michael@0 | 6716 | /* Master Lock INIT for info structure */ |
michael@0 | 6717 | SCTP_INP_INFO_LOCK_INIT(); |
michael@0 | 6718 | SCTP_STATLOG_INIT_LOCK(); |
michael@0 | 6719 | |
michael@0 | 6720 | SCTP_IPI_COUNT_INIT(); |
michael@0 | 6721 | SCTP_IPI_ADDR_INIT(); |
michael@0 | 6722 | #ifdef SCTP_PACKET_LOGGING |
michael@0 | 6723 | SCTP_IP_PKTLOG_INIT(); |
michael@0 | 6724 | #endif |
michael@0 | 6725 | LIST_INIT(&SCTP_BASE_INFO(addr_wq)); |
michael@0 | 6726 | |
michael@0 | 6727 | SCTP_WQ_ADDR_INIT(); |
michael@0 | 6728 | /* not sure if we need all the counts */ |
michael@0 | 6729 | SCTP_BASE_INFO(ipi_count_ep) = 0; |
michael@0 | 6730 | /* assoc/tcb zone info */ |
michael@0 | 6731 | SCTP_BASE_INFO(ipi_count_asoc) = 0; |
michael@0 | 6732 | /* local addrlist zone info */ |
michael@0 | 6733 | SCTP_BASE_INFO(ipi_count_laddr) = 0; |
michael@0 | 6734 | /* remote addrlist zone info */ |
michael@0 | 6735 | SCTP_BASE_INFO(ipi_count_raddr) = 0; |
michael@0 | 6736 | /* chunk info */ |
michael@0 | 6737 | SCTP_BASE_INFO(ipi_count_chunk) = 0; |
michael@0 | 6738 | |
michael@0 | 6739 | /* socket queue zone info */ |
michael@0 | 6740 | SCTP_BASE_INFO(ipi_count_readq) = 0; |
michael@0 | 6741 | |
michael@0 | 6742 | /* stream out queue cont */ |
michael@0 | 6743 | SCTP_BASE_INFO(ipi_count_strmoq) = 0; |
michael@0 | 6744 | |
michael@0 | 6745 | SCTP_BASE_INFO(ipi_free_strmoq) = 0; |
michael@0 | 6746 | SCTP_BASE_INFO(ipi_free_chunks) = 0; |
michael@0 | 6747 | |
michael@0 | 6748 | SCTP_OS_TIMER_INIT(&SCTP_BASE_INFO(addr_wq_timer.timer)); |
michael@0 | 6749 | |
michael@0 | 6750 | /* Init the TIMEWAIT list */ |
michael@0 | 6751 | for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) { |
michael@0 | 6752 | LIST_INIT(&SCTP_BASE_INFO(vtag_timewait)[i]); |
michael@0 | 6753 | } |
michael@0 | 6754 | #if defined(SCTP_PROCESS_LEVEL_LOCKS) |
michael@0 | 6755 | #if defined(__Userspace_os_Windows) |
michael@0 | 6756 | InitializeConditionVariable(&sctp_it_ctl.iterator_wakeup); |
michael@0 | 6757 | #else |
michael@0 | 6758 | (void)pthread_cond_init(&sctp_it_ctl.iterator_wakeup, NULL); |
michael@0 | 6759 | #endif |
michael@0 | 6760 | #endif |
michael@0 | 6761 | sctp_startup_iterator(); |
michael@0 | 6762 | |
michael@0 | 6763 | #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP) |
michael@0 | 6764 | sctp_startup_mcore_threads(); |
michael@0 | 6765 | #endif |
michael@0 | 6766 | |
michael@0 | 6767 | #ifndef __Panda__ |
michael@0 | 6768 | /* |
michael@0 | 6769 | * INIT the default VRF which for BSD is the only one, other O/S's |
michael@0 | 6770 | * may have more. But initially they must start with one and then |
michael@0 | 6771 | * add the VRF's as addresses are added. |
michael@0 | 6772 | */ |
michael@0 | 6773 | sctp_init_vrf_list(SCTP_DEFAULT_VRF); |
michael@0 | 6774 | #endif |
michael@0 | 6775 | #if defined(__FreeBSD__) && __FreeBSD_cc_version >= 1100000 |
michael@0 | 6776 | if (ip_register_flow_handler(sctp_netisr_hdlr, IPPROTO_SCTP)) { |
michael@0 | 6777 | SCTP_PRINTF("***SCTP- Error can't register netisr handler***\n"); |
michael@0 | 6778 | } |
michael@0 | 6779 | #endif |
michael@0 | 6780 | #if defined(_SCTP_NEEDS_CALLOUT_) || defined(_USER_SCTP_NEEDS_CALLOUT_) |
michael@0 | 6781 | /* allocate the lock for the callout/timer queue */ |
michael@0 | 6782 | SCTP_TIMERQ_LOCK_INIT(); |
michael@0 | 6783 | TAILQ_INIT(&SCTP_BASE_INFO(callqueue)); |
michael@0 | 6784 | #endif |
michael@0 | 6785 | #if defined(__Userspace__) |
michael@0 | 6786 | mbuf_init(NULL); |
michael@0 | 6787 | atomic_init(); |
michael@0 | 6788 | #if defined(INET) || defined(INET6) |
michael@0 | 6789 | recv_thread_init(); |
michael@0 | 6790 | #endif |
michael@0 | 6791 | #endif |
michael@0 | 6792 | } |
michael@0 | 6793 | |
michael@0 | 6794 | /* |
michael@0 | 6795 | * Assumes that the SCTP_BASE_INFO() lock is NOT held. |
michael@0 | 6796 | */ |
michael@0 | 6797 | void |
michael@0 | 6798 | sctp_pcb_finish(void) |
michael@0 | 6799 | { |
michael@0 | 6800 | struct sctp_vrflist *vrf_bucket; |
michael@0 | 6801 | struct sctp_vrf *vrf, *nvrf; |
michael@0 | 6802 | struct sctp_ifn *ifn, *nifn; |
michael@0 | 6803 | struct sctp_ifa *ifa, *nifa; |
michael@0 | 6804 | struct sctpvtaghead *chain; |
michael@0 | 6805 | struct sctp_tagblock *twait_block, *prev_twait_block; |
michael@0 | 6806 | struct sctp_laddr *wi, *nwi; |
michael@0 | 6807 | int i; |
michael@0 | 6808 | struct sctp_iterator *it, *nit; |
michael@0 | 6809 | |
michael@0 | 6810 | #if !defined(__FreeBSD__) |
michael@0 | 6811 | /* Notify the iterator to exit. */ |
michael@0 | 6812 | SCTP_IPI_ITERATOR_WQ_LOCK(); |
michael@0 | 6813 | sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_MUST_EXIT; |
michael@0 | 6814 | sctp_wakeup_iterator(); |
michael@0 | 6815 | SCTP_IPI_ITERATOR_WQ_UNLOCK(); |
michael@0 | 6816 | #endif |
michael@0 | 6817 | #if defined(__APPLE__) |
michael@0 | 6818 | #if !defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) && !defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION) |
michael@0 | 6819 | in_pcbinfo_detach(&SCTP_BASE_INFO(sctbinfo)); |
michael@0 | 6820 | #endif |
michael@0 | 6821 | SCTP_IPI_ITERATOR_WQ_LOCK(); |
michael@0 | 6822 | do { |
michael@0 | 6823 | msleep(&sctp_it_ctl.iterator_flags, |
michael@0 | 6824 | sctp_it_ctl.ipi_iterator_wq_mtx, |
michael@0 | 6825 | 0, "waiting_for_work", 0); |
michael@0 | 6826 | } while ((sctp_it_ctl.iterator_flags & SCTP_ITERATOR_EXITED) == 0); |
michael@0 | 6827 | thread_deallocate(sctp_it_ctl.thread_proc); |
michael@0 | 6828 | SCTP_IPI_ITERATOR_WQ_UNLOCK(); |
michael@0 | 6829 | #endif |
michael@0 | 6830 | #if defined(__Windows__) |
michael@0 | 6831 | if (sctp_it_ctl.iterator_thread_obj != NULL) { |
michael@0 | 6832 | NTSTATUS status = STATUS_SUCCESS; |
michael@0 | 6833 | |
michael@0 | 6834 | KeSetEvent(&sctp_it_ctl.iterator_wakeup[1], IO_NO_INCREMENT, FALSE); |
michael@0 | 6835 | status = KeWaitForSingleObject(sctp_it_ctl.iterator_thread_obj, |
michael@0 | 6836 | Executive, |
michael@0 | 6837 | KernelMode, |
michael@0 | 6838 | FALSE, |
michael@0 | 6839 | NULL); |
michael@0 | 6840 | ObDereferenceObject(sctp_it_ctl.iterator_thread_obj); |
michael@0 | 6841 | } |
michael@0 | 6842 | #endif |
michael@0 | 6843 | #if defined(__Userspace__) |
michael@0 | 6844 | if (sctp_it_ctl.thread_proc) { |
michael@0 | 6845 | #if defined(__Userspace_os_Windows) |
michael@0 | 6846 | WaitForSingleObject(sctp_it_ctl.thread_proc, INFINITE); |
michael@0 | 6847 | CloseHandle(sctp_it_ctl.thread_proc); |
michael@0 | 6848 | sctp_it_ctl.thread_proc = NULL; |
michael@0 | 6849 | #else |
michael@0 | 6850 | pthread_join(sctp_it_ctl.thread_proc, NULL); |
michael@0 | 6851 | sctp_it_ctl.thread_proc = 0; |
michael@0 | 6852 | #endif |
michael@0 | 6853 | } |
michael@0 | 6854 | #endif |
michael@0 | 6855 | #if defined(SCTP_PROCESS_LEVEL_LOCKS) |
michael@0 | 6856 | #if defined(__Userspace_os_Windows) |
michael@0 | 6857 | DeleteConditionVariable(&sctp_it_ctl.iterator_wakeup); |
michael@0 | 6858 | #else |
michael@0 | 6859 | pthread_cond_destroy(&sctp_it_ctl.iterator_wakeup); |
michael@0 | 6860 | #endif |
michael@0 | 6861 | #endif |
michael@0 | 6862 | /* In FreeBSD the iterator thread never exits |
michael@0 | 6863 | * but we do clean up. |
michael@0 | 6864 | * The only way FreeBSD reaches here is if we have VRF's |
michael@0 | 6865 | * but we still add the ifdef to make it compile on old versions. |
michael@0 | 6866 | */ |
michael@0 | 6867 | SCTP_IPI_ITERATOR_WQ_LOCK(); |
michael@0 | 6868 | TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) { |
michael@0 | 6869 | #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 |
michael@0 | 6870 | if (it->vn != curvnet) { |
michael@0 | 6871 | continue; |
michael@0 | 6872 | } |
michael@0 | 6873 | #endif |
michael@0 | 6874 | TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr); |
michael@0 | 6875 | if (it->function_atend != NULL) { |
michael@0 | 6876 | (*it->function_atend) (it->pointer, it->val); |
michael@0 | 6877 | } |
michael@0 | 6878 | SCTP_FREE(it,SCTP_M_ITER); |
michael@0 | 6879 | } |
michael@0 | 6880 | SCTP_IPI_ITERATOR_WQ_UNLOCK(); |
michael@0 | 6881 | #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 |
michael@0 | 6882 | SCTP_ITERATOR_LOCK(); |
michael@0 | 6883 | if ((sctp_it_ctl.cur_it) && |
michael@0 | 6884 | (sctp_it_ctl.cur_it->vn == curvnet)) { |
michael@0 | 6885 | sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_IT; |
michael@0 | 6886 | } |
michael@0 | 6887 | SCTP_ITERATOR_UNLOCK(); |
michael@0 | 6888 | #endif |
michael@0 | 6889 | #if !defined(__FreeBSD__) |
michael@0 | 6890 | SCTP_IPI_ITERATOR_WQ_DESTROY(); |
michael@0 | 6891 | SCTP_ITERATOR_LOCK_DESTROY(); |
michael@0 | 6892 | #endif |
michael@0 | 6893 | SCTP_OS_TIMER_STOP(&SCTP_BASE_INFO(addr_wq_timer.timer)); |
michael@0 | 6894 | SCTP_WQ_ADDR_LOCK(); |
michael@0 | 6895 | LIST_FOREACH_SAFE(wi, &SCTP_BASE_INFO(addr_wq), sctp_nxt_addr, nwi) { |
michael@0 | 6896 | LIST_REMOVE(wi, sctp_nxt_addr); |
michael@0 | 6897 | SCTP_DECR_LADDR_COUNT(); |
michael@0 | 6898 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), wi); |
michael@0 | 6899 | } |
michael@0 | 6900 | SCTP_WQ_ADDR_UNLOCK(); |
michael@0 | 6901 | |
michael@0 | 6902 | /* |
michael@0 | 6903 | * free the vrf/ifn/ifa lists and hashes (be sure address monitor |
michael@0 | 6904 | * is destroyed first). |
michael@0 | 6905 | */ |
michael@0 | 6906 | vrf_bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(SCTP_DEFAULT_VRFID & SCTP_BASE_INFO(hashvrfmark))]; |
michael@0 | 6907 | LIST_FOREACH_SAFE(vrf, vrf_bucket, next_vrf, nvrf) { |
michael@0 | 6908 | LIST_FOREACH_SAFE(ifn, &vrf->ifnlist, next_ifn, nifn) { |
michael@0 | 6909 | LIST_FOREACH_SAFE(ifa, &ifn->ifalist, next_ifa, nifa) { |
michael@0 | 6910 | /* free the ifa */ |
michael@0 | 6911 | LIST_REMOVE(ifa, next_bucket); |
michael@0 | 6912 | LIST_REMOVE(ifa, next_ifa); |
michael@0 | 6913 | SCTP_FREE(ifa, SCTP_M_IFA); |
michael@0 | 6914 | } |
michael@0 | 6915 | /* free the ifn */ |
michael@0 | 6916 | LIST_REMOVE(ifn, next_bucket); |
michael@0 | 6917 | LIST_REMOVE(ifn, next_ifn); |
michael@0 | 6918 | SCTP_FREE(ifn, SCTP_M_IFN); |
michael@0 | 6919 | } |
michael@0 | 6920 | SCTP_HASH_FREE(vrf->vrf_addr_hash, vrf->vrf_addr_hashmark); |
michael@0 | 6921 | /* free the vrf */ |
michael@0 | 6922 | LIST_REMOVE(vrf, next_vrf); |
michael@0 | 6923 | SCTP_FREE(vrf, SCTP_M_VRF); |
michael@0 | 6924 | } |
michael@0 | 6925 | /* free the vrf hashes */ |
michael@0 | 6926 | SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_vrfhash), SCTP_BASE_INFO(hashvrfmark)); |
michael@0 | 6927 | SCTP_HASH_FREE(SCTP_BASE_INFO(vrf_ifn_hash), SCTP_BASE_INFO(vrf_ifn_hashmark)); |
michael@0 | 6928 | #if defined(__Userspace__) && !defined(__Userspace_os_Windows) |
michael@0 | 6929 | /* free memory allocated by getifaddrs call */ |
michael@0 | 6930 | #if defined(INET) || defined(INET6) |
michael@0 | 6931 | freeifaddrs(g_interfaces); |
michael@0 | 6932 | #endif |
michael@0 | 6933 | #endif |
michael@0 | 6934 | |
michael@0 | 6935 | /* free the TIMEWAIT list elements malloc'd in the function |
michael@0 | 6936 | * sctp_add_vtag_to_timewait()... |
michael@0 | 6937 | */ |
michael@0 | 6938 | for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) { |
michael@0 | 6939 | chain = &SCTP_BASE_INFO(vtag_timewait)[i]; |
michael@0 | 6940 | if (!LIST_EMPTY(chain)) { |
michael@0 | 6941 | prev_twait_block = NULL; |
michael@0 | 6942 | LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) { |
michael@0 | 6943 | if (prev_twait_block) { |
michael@0 | 6944 | SCTP_FREE(prev_twait_block, SCTP_M_TIMW); |
michael@0 | 6945 | } |
michael@0 | 6946 | prev_twait_block = twait_block; |
michael@0 | 6947 | } |
michael@0 | 6948 | SCTP_FREE(prev_twait_block, SCTP_M_TIMW); |
michael@0 | 6949 | } |
michael@0 | 6950 | } |
michael@0 | 6951 | |
michael@0 | 6952 | /* free the locks and mutexes */ |
michael@0 | 6953 | #if defined(__APPLE__) |
michael@0 | 6954 | SCTP_TIMERQ_LOCK_DESTROY(); |
michael@0 | 6955 | #endif |
michael@0 | 6956 | #ifdef SCTP_PACKET_LOGGING |
michael@0 | 6957 | SCTP_IP_PKTLOG_DESTROY(); |
michael@0 | 6958 | #endif |
michael@0 | 6959 | SCTP_IPI_ADDR_DESTROY(); |
michael@0 | 6960 | #if defined(__APPLE__) |
michael@0 | 6961 | SCTP_IPI_COUNT_DESTROY(); |
michael@0 | 6962 | #endif |
michael@0 | 6963 | SCTP_STATLOG_DESTROY(); |
michael@0 | 6964 | SCTP_INP_INFO_LOCK_DESTROY(); |
michael@0 | 6965 | |
michael@0 | 6966 | SCTP_WQ_ADDR_DESTROY(); |
michael@0 | 6967 | |
michael@0 | 6968 | #if defined(__APPLE__) |
michael@0 | 6969 | #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) || defined(APPLE_LION) || defined(APPLE_MOUNTAINLION) |
michael@0 | 6970 | lck_grp_attr_free(SCTP_BASE_INFO(sctbinfo).mtx_grp_attr); |
michael@0 | 6971 | lck_grp_free(SCTP_BASE_INFO(sctbinfo).mtx_grp); |
michael@0 | 6972 | lck_attr_free(SCTP_BASE_INFO(sctbinfo).mtx_attr); |
michael@0 | 6973 | #else |
michael@0 | 6974 | lck_grp_attr_free(SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr); |
michael@0 | 6975 | lck_grp_free(SCTP_BASE_INFO(sctbinfo).ipi_lock_grp); |
michael@0 | 6976 | lck_attr_free(SCTP_BASE_INFO(sctbinfo).ipi_lock_attr); |
michael@0 | 6977 | #endif |
michael@0 | 6978 | #endif |
michael@0 | 6979 | #if defined(__Userspace__) |
michael@0 | 6980 | SCTP_TIMERQ_LOCK_DESTROY(); |
michael@0 | 6981 | SCTP_ZONE_DESTROY(zone_mbuf); |
michael@0 | 6982 | SCTP_ZONE_DESTROY(zone_clust); |
michael@0 | 6983 | SCTP_ZONE_DESTROY(zone_ext_refcnt); |
michael@0 | 6984 | #endif |
michael@0 | 6985 | #if defined(__Windows__) || defined(__FreeBSD__) || defined(__Userspace__) |
michael@0 | 6986 | SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_ep)); |
michael@0 | 6987 | SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asoc)); |
michael@0 | 6988 | SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_laddr)); |
michael@0 | 6989 | SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_net)); |
michael@0 | 6990 | SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_chunk)); |
michael@0 | 6991 | SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_readq)); |
michael@0 | 6992 | SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_strmoq)); |
michael@0 | 6993 | SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf)); |
michael@0 | 6994 | SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf_ack)); |
michael@0 | 6995 | #endif |
michael@0 | 6996 | /* Get rid of other stuff to */ |
michael@0 | 6997 | if (SCTP_BASE_INFO(sctp_asochash) != NULL) |
michael@0 | 6998 | SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_asochash), SCTP_BASE_INFO(hashasocmark)); |
michael@0 | 6999 | if (SCTP_BASE_INFO(sctp_ephash) != NULL) |
michael@0 | 7000 | SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_ephash), SCTP_BASE_INFO(hashmark)); |
michael@0 | 7001 | if (SCTP_BASE_INFO(sctp_tcpephash) != NULL) |
michael@0 | 7002 | SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_tcpephash), SCTP_BASE_INFO(hashtcpmark)); |
michael@0 | 7003 | #if defined(__FreeBSD__) && defined(SMP) && defined(SCTP_USE_PERCPU_STAT) |
michael@0 | 7004 | SCTP_FREE(SCTP_BASE_STATS, SCTP_M_MCORE); |
michael@0 | 7005 | #endif |
michael@0 | 7006 | } |
michael@0 | 7007 | |
michael@0 | 7008 | |
michael@0 | 7009 | int |
michael@0 | 7010 | sctp_load_addresses_from_init(struct sctp_tcb *stcb, struct mbuf *m, |
michael@0 | 7011 | int offset, int limit, |
michael@0 | 7012 | struct sockaddr *src, struct sockaddr *dst, |
michael@0 | 7013 | struct sockaddr *altsa) |
michael@0 | 7014 | { |
michael@0 | 7015 | /* |
michael@0 | 7016 | * grub through the INIT pulling addresses and loading them to the |
michael@0 | 7017 | * nets structure in the asoc. The from address in the mbuf should |
michael@0 | 7018 | * also be loaded (if it is not already). This routine can be called |
michael@0 | 7019 | * with either INIT or INIT-ACK's as long as the m points to the IP |
michael@0 | 7020 | * packet and the offset points to the beginning of the parameters. |
michael@0 | 7021 | */ |
michael@0 | 7022 | struct sctp_inpcb *inp; |
michael@0 | 7023 | struct sctp_nets *net, *nnet, *net_tmp; |
michael@0 | 7024 | struct sctp_paramhdr *phdr, parm_buf; |
michael@0 | 7025 | struct sctp_tcb *stcb_tmp; |
michael@0 | 7026 | uint16_t ptype, plen; |
michael@0 | 7027 | struct sockaddr *sa; |
michael@0 | 7028 | uint8_t random_store[SCTP_PARAM_BUFFER_SIZE]; |
michael@0 | 7029 | struct sctp_auth_random *p_random = NULL; |
michael@0 | 7030 | uint16_t random_len = 0; |
michael@0 | 7031 | uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE]; |
michael@0 | 7032 | struct sctp_auth_hmac_algo *hmacs = NULL; |
michael@0 | 7033 | uint16_t hmacs_len = 0; |
michael@0 | 7034 | uint8_t saw_asconf = 0; |
michael@0 | 7035 | uint8_t saw_asconf_ack = 0; |
michael@0 | 7036 | uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE]; |
michael@0 | 7037 | struct sctp_auth_chunk_list *chunks = NULL; |
michael@0 | 7038 | uint16_t num_chunks = 0; |
michael@0 | 7039 | sctp_key_t *new_key; |
michael@0 | 7040 | uint32_t keylen; |
michael@0 | 7041 | int got_random = 0, got_hmacs = 0, got_chklist = 0; |
michael@0 | 7042 | uint8_t ecn_allowed; |
michael@0 | 7043 | #ifdef INET |
michael@0 | 7044 | struct sockaddr_in sin; |
michael@0 | 7045 | #endif |
michael@0 | 7046 | #ifdef INET6 |
michael@0 | 7047 | struct sockaddr_in6 sin6; |
michael@0 | 7048 | #endif |
michael@0 | 7049 | |
michael@0 | 7050 | /* First get the destination address setup too. */ |
michael@0 | 7051 | #ifdef INET |
michael@0 | 7052 | memset(&sin, 0, sizeof(sin)); |
michael@0 | 7053 | sin.sin_family = AF_INET; |
michael@0 | 7054 | #ifdef HAVE_SIN_LEN |
michael@0 | 7055 | sin.sin_len = sizeof(sin); |
michael@0 | 7056 | #endif |
michael@0 | 7057 | sin.sin_port = stcb->rport; |
michael@0 | 7058 | #endif |
michael@0 | 7059 | #ifdef INET6 |
michael@0 | 7060 | memset(&sin6, 0, sizeof(sin6)); |
michael@0 | 7061 | sin6.sin6_family = AF_INET6; |
michael@0 | 7062 | #ifdef HAVE_SIN6_LEN |
michael@0 | 7063 | sin6.sin6_len = sizeof(struct sockaddr_in6); |
michael@0 | 7064 | #endif |
michael@0 | 7065 | sin6.sin6_port = stcb->rport; |
michael@0 | 7066 | #endif |
michael@0 | 7067 | if (altsa) { |
michael@0 | 7068 | sa = altsa; |
michael@0 | 7069 | } else { |
michael@0 | 7070 | sa = src; |
michael@0 | 7071 | } |
michael@0 | 7072 | /* Turn off ECN until we get through all params */ |
michael@0 | 7073 | ecn_allowed = 0; |
michael@0 | 7074 | TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) { |
michael@0 | 7075 | /* mark all addresses that we have currently on the list */ |
michael@0 | 7076 | net->dest_state |= SCTP_ADDR_NOT_IN_ASSOC; |
michael@0 | 7077 | } |
michael@0 | 7078 | /* does the source address already exist? if so skip it */ |
michael@0 | 7079 | inp = stcb->sctp_ep; |
michael@0 | 7080 | atomic_add_int(&stcb->asoc.refcnt, 1); |
michael@0 | 7081 | stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net_tmp, dst, stcb); |
michael@0 | 7082 | atomic_add_int(&stcb->asoc.refcnt, -1); |
michael@0 | 7083 | |
michael@0 | 7084 | if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || inp == NULL) { |
michael@0 | 7085 | /* we must add the source address */ |
michael@0 | 7086 | /* no scope set here since we have a tcb already. */ |
michael@0 | 7087 | switch (sa->sa_family) { |
michael@0 | 7088 | #ifdef INET |
michael@0 | 7089 | case AF_INET: |
michael@0 | 7090 | if (stcb->asoc.scope.ipv4_addr_legal) { |
michael@0 | 7091 | if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_2)) { |
michael@0 | 7092 | return (-1); |
michael@0 | 7093 | } |
michael@0 | 7094 | } |
michael@0 | 7095 | break; |
michael@0 | 7096 | #endif |
michael@0 | 7097 | #ifdef INET6 |
michael@0 | 7098 | case AF_INET6: |
michael@0 | 7099 | if (stcb->asoc.scope.ipv6_addr_legal) { |
michael@0 | 7100 | if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) { |
michael@0 | 7101 | return (-2); |
michael@0 | 7102 | } |
michael@0 | 7103 | } |
michael@0 | 7104 | break; |
michael@0 | 7105 | #endif |
michael@0 | 7106 | #if defined(__Userspace__) |
michael@0 | 7107 | case AF_CONN: |
michael@0 | 7108 | if (stcb->asoc.scope.conn_addr_legal) { |
michael@0 | 7109 | if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) { |
michael@0 | 7110 | return (-2); |
michael@0 | 7111 | } |
michael@0 | 7112 | } |
michael@0 | 7113 | break; |
michael@0 | 7114 | #endif |
michael@0 | 7115 | default: |
michael@0 | 7116 | break; |
michael@0 | 7117 | } |
michael@0 | 7118 | } else { |
michael@0 | 7119 | if (net_tmp != NULL && stcb_tmp == stcb) { |
michael@0 | 7120 | net_tmp->dest_state &= ~SCTP_ADDR_NOT_IN_ASSOC; |
michael@0 | 7121 | } else if (stcb_tmp != stcb) { |
michael@0 | 7122 | /* It belongs to another association? */ |
michael@0 | 7123 | if (stcb_tmp) |
michael@0 | 7124 | SCTP_TCB_UNLOCK(stcb_tmp); |
michael@0 | 7125 | return (-3); |
michael@0 | 7126 | } |
michael@0 | 7127 | } |
michael@0 | 7128 | if (stcb->asoc.state == 0) { |
michael@0 | 7129 | /* the assoc was freed? */ |
michael@0 | 7130 | return (-4); |
michael@0 | 7131 | } |
michael@0 | 7132 | /* |
michael@0 | 7133 | * peer must explicitly turn this on. This may have been initialized |
michael@0 | 7134 | * to be "on" in order to allow local addr changes while INIT's are |
michael@0 | 7135 | * in flight. |
michael@0 | 7136 | */ |
michael@0 | 7137 | stcb->asoc.peer_supports_asconf = 0; |
michael@0 | 7138 | /* now we must go through each of the params. */ |
michael@0 | 7139 | phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf)); |
michael@0 | 7140 | while (phdr) { |
michael@0 | 7141 | ptype = ntohs(phdr->param_type); |
michael@0 | 7142 | plen = ntohs(phdr->param_length); |
michael@0 | 7143 | /* |
michael@0 | 7144 | * SCTP_PRINTF("ptype => %0x, plen => %d\n", (uint32_t)ptype, |
michael@0 | 7145 | * (int)plen); |
michael@0 | 7146 | */ |
michael@0 | 7147 | if (offset + plen > limit) { |
michael@0 | 7148 | break; |
michael@0 | 7149 | } |
michael@0 | 7150 | if (plen == 0) { |
michael@0 | 7151 | break; |
michael@0 | 7152 | } |
michael@0 | 7153 | #ifdef INET |
michael@0 | 7154 | if (ptype == SCTP_IPV4_ADDRESS) { |
michael@0 | 7155 | if (stcb->asoc.scope.ipv4_addr_legal) { |
michael@0 | 7156 | struct sctp_ipv4addr_param *p4, p4_buf; |
michael@0 | 7157 | |
michael@0 | 7158 | /* ok get the v4 address and check/add */ |
michael@0 | 7159 | phdr = sctp_get_next_param(m, offset, |
michael@0 | 7160 | (struct sctp_paramhdr *)&p4_buf, |
michael@0 | 7161 | sizeof(p4_buf)); |
michael@0 | 7162 | if (plen != sizeof(struct sctp_ipv4addr_param) || |
michael@0 | 7163 | phdr == NULL) { |
michael@0 | 7164 | return (-5); |
michael@0 | 7165 | } |
michael@0 | 7166 | p4 = (struct sctp_ipv4addr_param *)phdr; |
michael@0 | 7167 | sin.sin_addr.s_addr = p4->addr; |
michael@0 | 7168 | if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) { |
michael@0 | 7169 | /* Skip multi-cast addresses */ |
michael@0 | 7170 | goto next_param; |
michael@0 | 7171 | } |
michael@0 | 7172 | if ((sin.sin_addr.s_addr == INADDR_BROADCAST) || |
michael@0 | 7173 | (sin.sin_addr.s_addr == INADDR_ANY)) { |
michael@0 | 7174 | goto next_param; |
michael@0 | 7175 | } |
michael@0 | 7176 | sa = (struct sockaddr *)&sin; |
michael@0 | 7177 | inp = stcb->sctp_ep; |
michael@0 | 7178 | atomic_add_int(&stcb->asoc.refcnt, 1); |
michael@0 | 7179 | stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net, |
michael@0 | 7180 | dst, stcb); |
michael@0 | 7181 | atomic_add_int(&stcb->asoc.refcnt, -1); |
michael@0 | 7182 | |
michael@0 | 7183 | if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || |
michael@0 | 7184 | inp == NULL) { |
michael@0 | 7185 | /* we must add the source address */ |
michael@0 | 7186 | /* |
michael@0 | 7187 | * no scope set since we have a tcb |
michael@0 | 7188 | * already |
michael@0 | 7189 | */ |
michael@0 | 7190 | |
michael@0 | 7191 | /* |
michael@0 | 7192 | * we must validate the state again |
michael@0 | 7193 | * here |
michael@0 | 7194 | */ |
michael@0 | 7195 | add_it_now: |
michael@0 | 7196 | if (stcb->asoc.state == 0) { |
michael@0 | 7197 | /* the assoc was freed? */ |
michael@0 | 7198 | return (-7); |
michael@0 | 7199 | } |
michael@0 | 7200 | if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_4)) { |
michael@0 | 7201 | return (-8); |
michael@0 | 7202 | } |
michael@0 | 7203 | } else if (stcb_tmp == stcb) { |
michael@0 | 7204 | if (stcb->asoc.state == 0) { |
michael@0 | 7205 | /* the assoc was freed? */ |
michael@0 | 7206 | return (-10); |
michael@0 | 7207 | } |
michael@0 | 7208 | if (net != NULL) { |
michael@0 | 7209 | /* clear flag */ |
michael@0 | 7210 | net->dest_state &= |
michael@0 | 7211 | ~SCTP_ADDR_NOT_IN_ASSOC; |
michael@0 | 7212 | } |
michael@0 | 7213 | } else { |
michael@0 | 7214 | /* |
michael@0 | 7215 | * strange, address is in another |
michael@0 | 7216 | * assoc? straighten out locks. |
michael@0 | 7217 | */ |
michael@0 | 7218 | if (stcb_tmp) { |
michael@0 | 7219 | if (SCTP_GET_STATE(&stcb_tmp->asoc) & SCTP_STATE_COOKIE_WAIT) { |
michael@0 | 7220 | /* in setup state we abort this guy */ |
michael@0 | 7221 | sctp_abort_an_association(stcb_tmp->sctp_ep, |
michael@0 | 7222 | stcb_tmp, NULL, SCTP_SO_NOT_LOCKED); |
michael@0 | 7223 | goto add_it_now; |
michael@0 | 7224 | } |
michael@0 | 7225 | SCTP_TCB_UNLOCK(stcb_tmp); |
michael@0 | 7226 | } |
michael@0 | 7227 | |
michael@0 | 7228 | if (stcb->asoc.state == 0) { |
michael@0 | 7229 | /* the assoc was freed? */ |
michael@0 | 7230 | return (-12); |
michael@0 | 7231 | } |
michael@0 | 7232 | return (-13); |
michael@0 | 7233 | } |
michael@0 | 7234 | } |
michael@0 | 7235 | } else |
michael@0 | 7236 | #endif |
michael@0 | 7237 | #ifdef INET6 |
michael@0 | 7238 | if (ptype == SCTP_IPV6_ADDRESS) { |
michael@0 | 7239 | if (stcb->asoc.scope.ipv6_addr_legal) { |
michael@0 | 7240 | /* ok get the v6 address and check/add */ |
michael@0 | 7241 | struct sctp_ipv6addr_param *p6, p6_buf; |
michael@0 | 7242 | |
michael@0 | 7243 | phdr = sctp_get_next_param(m, offset, |
michael@0 | 7244 | (struct sctp_paramhdr *)&p6_buf, |
michael@0 | 7245 | sizeof(p6_buf)); |
michael@0 | 7246 | if (plen != sizeof(struct sctp_ipv6addr_param) || |
michael@0 | 7247 | phdr == NULL) { |
michael@0 | 7248 | return (-14); |
michael@0 | 7249 | } |
michael@0 | 7250 | p6 = (struct sctp_ipv6addr_param *)phdr; |
michael@0 | 7251 | memcpy((caddr_t)&sin6.sin6_addr, p6->addr, |
michael@0 | 7252 | sizeof(p6->addr)); |
michael@0 | 7253 | if (IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) { |
michael@0 | 7254 | /* Skip multi-cast addresses */ |
michael@0 | 7255 | goto next_param; |
michael@0 | 7256 | } |
michael@0 | 7257 | if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) { |
michael@0 | 7258 | /* Link local make no sense without scope */ |
michael@0 | 7259 | goto next_param; |
michael@0 | 7260 | } |
michael@0 | 7261 | sa = (struct sockaddr *)&sin6; |
michael@0 | 7262 | inp = stcb->sctp_ep; |
michael@0 | 7263 | atomic_add_int(&stcb->asoc.refcnt, 1); |
michael@0 | 7264 | stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net, |
michael@0 | 7265 | dst, stcb); |
michael@0 | 7266 | atomic_add_int(&stcb->asoc.refcnt, -1); |
michael@0 | 7267 | if (stcb_tmp == NULL && |
michael@0 | 7268 | (inp == stcb->sctp_ep || inp == NULL)) { |
michael@0 | 7269 | /* |
michael@0 | 7270 | * we must validate the state again |
michael@0 | 7271 | * here |
michael@0 | 7272 | */ |
michael@0 | 7273 | add_it_now6: |
michael@0 | 7274 | if (stcb->asoc.state == 0) { |
michael@0 | 7275 | /* the assoc was freed? */ |
michael@0 | 7276 | return (-16); |
michael@0 | 7277 | } |
michael@0 | 7278 | /* |
michael@0 | 7279 | * we must add the address, no scope |
michael@0 | 7280 | * set |
michael@0 | 7281 | */ |
michael@0 | 7282 | if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_5)) { |
michael@0 | 7283 | return (-17); |
michael@0 | 7284 | } |
michael@0 | 7285 | } else if (stcb_tmp == stcb) { |
michael@0 | 7286 | /* |
michael@0 | 7287 | * we must validate the state again |
michael@0 | 7288 | * here |
michael@0 | 7289 | */ |
michael@0 | 7290 | if (stcb->asoc.state == 0) { |
michael@0 | 7291 | /* the assoc was freed? */ |
michael@0 | 7292 | return (-19); |
michael@0 | 7293 | } |
michael@0 | 7294 | if (net != NULL) { |
michael@0 | 7295 | /* clear flag */ |
michael@0 | 7296 | net->dest_state &= |
michael@0 | 7297 | ~SCTP_ADDR_NOT_IN_ASSOC; |
michael@0 | 7298 | } |
michael@0 | 7299 | } else { |
michael@0 | 7300 | /* |
michael@0 | 7301 | * strange, address is in another |
michael@0 | 7302 | * assoc? straighten out locks. |
michael@0 | 7303 | */ |
michael@0 | 7304 | if (stcb_tmp) |
michael@0 | 7305 | if (SCTP_GET_STATE(&stcb_tmp->asoc) & SCTP_STATE_COOKIE_WAIT) { |
michael@0 | 7306 | /* in setup state we abort this guy */ |
michael@0 | 7307 | sctp_abort_an_association(stcb_tmp->sctp_ep, |
michael@0 | 7308 | stcb_tmp, NULL, SCTP_SO_NOT_LOCKED); |
michael@0 | 7309 | goto add_it_now6; |
michael@0 | 7310 | } |
michael@0 | 7311 | SCTP_TCB_UNLOCK(stcb_tmp); |
michael@0 | 7312 | |
michael@0 | 7313 | if (stcb->asoc.state == 0) { |
michael@0 | 7314 | /* the assoc was freed? */ |
michael@0 | 7315 | return (-21); |
michael@0 | 7316 | } |
michael@0 | 7317 | return (-22); |
michael@0 | 7318 | } |
michael@0 | 7319 | } |
michael@0 | 7320 | } else |
michael@0 | 7321 | #endif |
michael@0 | 7322 | if (ptype == SCTP_ECN_CAPABLE) { |
michael@0 | 7323 | ecn_allowed = 1; |
michael@0 | 7324 | } else if (ptype == SCTP_ULP_ADAPTATION) { |
michael@0 | 7325 | if (stcb->asoc.state != SCTP_STATE_OPEN) { |
michael@0 | 7326 | struct sctp_adaptation_layer_indication ai, *aip; |
michael@0 | 7327 | |
michael@0 | 7328 | phdr = sctp_get_next_param(m, offset, |
michael@0 | 7329 | (struct sctp_paramhdr *)&ai, sizeof(ai)); |
michael@0 | 7330 | aip = (struct sctp_adaptation_layer_indication *)phdr; |
michael@0 | 7331 | if (aip) { |
michael@0 | 7332 | stcb->asoc.peers_adaptation = ntohl(aip->indication); |
michael@0 | 7333 | stcb->asoc.adaptation_needed = 1; |
michael@0 | 7334 | } |
michael@0 | 7335 | } |
michael@0 | 7336 | } else if (ptype == SCTP_SET_PRIM_ADDR) { |
michael@0 | 7337 | struct sctp_asconf_addr_param lstore, *fee; |
michael@0 | 7338 | int lptype; |
michael@0 | 7339 | struct sockaddr *lsa = NULL; |
michael@0 | 7340 | #ifdef INET |
michael@0 | 7341 | struct sctp_asconf_addrv4_param *fii; |
michael@0 | 7342 | #endif |
michael@0 | 7343 | |
michael@0 | 7344 | stcb->asoc.peer_supports_asconf = 1; |
michael@0 | 7345 | if (plen > sizeof(lstore)) { |
michael@0 | 7346 | return (-23); |
michael@0 | 7347 | } |
michael@0 | 7348 | phdr = sctp_get_next_param(m, offset, |
michael@0 | 7349 | (struct sctp_paramhdr *)&lstore, |
michael@0 | 7350 | min(plen,sizeof(lstore))); |
michael@0 | 7351 | if (phdr == NULL) { |
michael@0 | 7352 | return (-24); |
michael@0 | 7353 | } |
michael@0 | 7354 | fee = (struct sctp_asconf_addr_param *)phdr; |
michael@0 | 7355 | lptype = ntohs(fee->addrp.ph.param_type); |
michael@0 | 7356 | switch (lptype) { |
michael@0 | 7357 | #ifdef INET |
michael@0 | 7358 | case SCTP_IPV4_ADDRESS: |
michael@0 | 7359 | if (plen != |
michael@0 | 7360 | sizeof(struct sctp_asconf_addrv4_param)) { |
michael@0 | 7361 | SCTP_PRINTF("Sizeof setprim in init/init ack not %d but %d - ignored\n", |
michael@0 | 7362 | (int)sizeof(struct sctp_asconf_addrv4_param), |
michael@0 | 7363 | plen); |
michael@0 | 7364 | } else { |
michael@0 | 7365 | fii = (struct sctp_asconf_addrv4_param *)fee; |
michael@0 | 7366 | sin.sin_addr.s_addr = fii->addrp.addr; |
michael@0 | 7367 | lsa = (struct sockaddr *)&sin; |
michael@0 | 7368 | } |
michael@0 | 7369 | break; |
michael@0 | 7370 | #endif |
michael@0 | 7371 | #ifdef INET6 |
michael@0 | 7372 | case SCTP_IPV6_ADDRESS: |
michael@0 | 7373 | if (plen != |
michael@0 | 7374 | sizeof(struct sctp_asconf_addr_param)) { |
michael@0 | 7375 | SCTP_PRINTF("Sizeof setprim (v6) in init/init ack not %d but %d - ignored\n", |
michael@0 | 7376 | (int)sizeof(struct sctp_asconf_addr_param), |
michael@0 | 7377 | plen); |
michael@0 | 7378 | } else { |
michael@0 | 7379 | memcpy(sin6.sin6_addr.s6_addr, |
michael@0 | 7380 | fee->addrp.addr, |
michael@0 | 7381 | sizeof(fee->addrp.addr)); |
michael@0 | 7382 | lsa = (struct sockaddr *)&sin6; |
michael@0 | 7383 | } |
michael@0 | 7384 | break; |
michael@0 | 7385 | #endif |
michael@0 | 7386 | default: |
michael@0 | 7387 | break; |
michael@0 | 7388 | } |
michael@0 | 7389 | if (lsa) { |
michael@0 | 7390 | (void)sctp_set_primary_addr(stcb, sa, NULL); |
michael@0 | 7391 | } |
michael@0 | 7392 | } else if (ptype == SCTP_HAS_NAT_SUPPORT) { |
michael@0 | 7393 | stcb->asoc.peer_supports_nat = 1; |
michael@0 | 7394 | } else if (ptype == SCTP_PRSCTP_SUPPORTED) { |
michael@0 | 7395 | /* Peer supports pr-sctp */ |
michael@0 | 7396 | stcb->asoc.peer_supports_prsctp = 1; |
michael@0 | 7397 | } else if (ptype == SCTP_SUPPORTED_CHUNK_EXT) { |
michael@0 | 7398 | /* A supported extension chunk */ |
michael@0 | 7399 | struct sctp_supported_chunk_types_param *pr_supported; |
michael@0 | 7400 | uint8_t local_store[SCTP_PARAM_BUFFER_SIZE]; |
michael@0 | 7401 | int num_ent, i; |
michael@0 | 7402 | |
michael@0 | 7403 | phdr = sctp_get_next_param(m, offset, |
michael@0 | 7404 | (struct sctp_paramhdr *)&local_store, min(sizeof(local_store),plen)); |
michael@0 | 7405 | if (phdr == NULL) { |
michael@0 | 7406 | return (-25); |
michael@0 | 7407 | } |
michael@0 | 7408 | stcb->asoc.peer_supports_asconf = 0; |
michael@0 | 7409 | stcb->asoc.peer_supports_prsctp = 0; |
michael@0 | 7410 | stcb->asoc.peer_supports_pktdrop = 0; |
michael@0 | 7411 | stcb->asoc.peer_supports_strreset = 0; |
michael@0 | 7412 | stcb->asoc.peer_supports_nr_sack = 0; |
michael@0 | 7413 | stcb->asoc.peer_supports_auth = 0; |
michael@0 | 7414 | pr_supported = (struct sctp_supported_chunk_types_param *)phdr; |
michael@0 | 7415 | num_ent = plen - sizeof(struct sctp_paramhdr); |
michael@0 | 7416 | for (i = 0; i < num_ent; i++) { |
michael@0 | 7417 | switch (pr_supported->chunk_types[i]) { |
michael@0 | 7418 | case SCTP_ASCONF: |
michael@0 | 7419 | case SCTP_ASCONF_ACK: |
michael@0 | 7420 | stcb->asoc.peer_supports_asconf = 1; |
michael@0 | 7421 | break; |
michael@0 | 7422 | case SCTP_FORWARD_CUM_TSN: |
michael@0 | 7423 | stcb->asoc.peer_supports_prsctp = 1; |
michael@0 | 7424 | break; |
michael@0 | 7425 | case SCTP_PACKET_DROPPED: |
michael@0 | 7426 | stcb->asoc.peer_supports_pktdrop = 1; |
michael@0 | 7427 | break; |
michael@0 | 7428 | case SCTP_NR_SELECTIVE_ACK: |
michael@0 | 7429 | stcb->asoc.peer_supports_nr_sack = 1; |
michael@0 | 7430 | break; |
michael@0 | 7431 | case SCTP_STREAM_RESET: |
michael@0 | 7432 | stcb->asoc.peer_supports_strreset = 1; |
michael@0 | 7433 | break; |
michael@0 | 7434 | case SCTP_AUTHENTICATION: |
michael@0 | 7435 | stcb->asoc.peer_supports_auth = 1; |
michael@0 | 7436 | break; |
michael@0 | 7437 | default: |
michael@0 | 7438 | /* one I have not learned yet */ |
michael@0 | 7439 | break; |
michael@0 | 7440 | |
michael@0 | 7441 | } |
michael@0 | 7442 | } |
michael@0 | 7443 | } else if (ptype == SCTP_RANDOM) { |
michael@0 | 7444 | if (plen > sizeof(random_store)) |
michael@0 | 7445 | break; |
michael@0 | 7446 | if (got_random) { |
michael@0 | 7447 | /* already processed a RANDOM */ |
michael@0 | 7448 | goto next_param; |
michael@0 | 7449 | } |
michael@0 | 7450 | phdr = sctp_get_next_param(m, offset, |
michael@0 | 7451 | (struct sctp_paramhdr *)random_store, |
michael@0 | 7452 | min(sizeof(random_store),plen)); |
michael@0 | 7453 | if (phdr == NULL) |
michael@0 | 7454 | return (-26); |
michael@0 | 7455 | p_random = (struct sctp_auth_random *)phdr; |
michael@0 | 7456 | random_len = plen - sizeof(*p_random); |
michael@0 | 7457 | /* enforce the random length */ |
michael@0 | 7458 | if (random_len != SCTP_AUTH_RANDOM_SIZE_REQUIRED) { |
michael@0 | 7459 | SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: invalid RANDOM len\n"); |
michael@0 | 7460 | return (-27); |
michael@0 | 7461 | } |
michael@0 | 7462 | got_random = 1; |
michael@0 | 7463 | } else if (ptype == SCTP_HMAC_LIST) { |
michael@0 | 7464 | int num_hmacs; |
michael@0 | 7465 | int i; |
michael@0 | 7466 | |
michael@0 | 7467 | if (plen > sizeof(hmacs_store)) |
michael@0 | 7468 | break; |
michael@0 | 7469 | if (got_hmacs) { |
michael@0 | 7470 | /* already processed a HMAC list */ |
michael@0 | 7471 | goto next_param; |
michael@0 | 7472 | } |
michael@0 | 7473 | phdr = sctp_get_next_param(m, offset, |
michael@0 | 7474 | (struct sctp_paramhdr *)hmacs_store, |
michael@0 | 7475 | min(plen,sizeof(hmacs_store))); |
michael@0 | 7476 | if (phdr == NULL) |
michael@0 | 7477 | return (-28); |
michael@0 | 7478 | hmacs = (struct sctp_auth_hmac_algo *)phdr; |
michael@0 | 7479 | hmacs_len = plen - sizeof(*hmacs); |
michael@0 | 7480 | num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]); |
michael@0 | 7481 | /* validate the hmac list */ |
michael@0 | 7482 | if (sctp_verify_hmac_param(hmacs, num_hmacs)) { |
michael@0 | 7483 | return (-29); |
michael@0 | 7484 | } |
michael@0 | 7485 | if (stcb->asoc.peer_hmacs != NULL) |
michael@0 | 7486 | sctp_free_hmaclist(stcb->asoc.peer_hmacs); |
michael@0 | 7487 | stcb->asoc.peer_hmacs = sctp_alloc_hmaclist(num_hmacs); |
michael@0 | 7488 | if (stcb->asoc.peer_hmacs != NULL) { |
michael@0 | 7489 | for (i = 0; i < num_hmacs; i++) { |
michael@0 | 7490 | (void)sctp_auth_add_hmacid(stcb->asoc.peer_hmacs, |
michael@0 | 7491 | ntohs(hmacs->hmac_ids[i])); |
michael@0 | 7492 | } |
michael@0 | 7493 | } |
michael@0 | 7494 | got_hmacs = 1; |
michael@0 | 7495 | } else if (ptype == SCTP_CHUNK_LIST) { |
michael@0 | 7496 | int i; |
michael@0 | 7497 | |
michael@0 | 7498 | if (plen > sizeof(chunks_store)) |
michael@0 | 7499 | break; |
michael@0 | 7500 | if (got_chklist) { |
michael@0 | 7501 | /* already processed a Chunks list */ |
michael@0 | 7502 | goto next_param; |
michael@0 | 7503 | } |
michael@0 | 7504 | phdr = sctp_get_next_param(m, offset, |
michael@0 | 7505 | (struct sctp_paramhdr *)chunks_store, |
michael@0 | 7506 | min(plen,sizeof(chunks_store))); |
michael@0 | 7507 | if (phdr == NULL) |
michael@0 | 7508 | return (-30); |
michael@0 | 7509 | chunks = (struct sctp_auth_chunk_list *)phdr; |
michael@0 | 7510 | num_chunks = plen - sizeof(*chunks); |
michael@0 | 7511 | if (stcb->asoc.peer_auth_chunks != NULL) |
michael@0 | 7512 | sctp_clear_chunklist(stcb->asoc.peer_auth_chunks); |
michael@0 | 7513 | else |
michael@0 | 7514 | stcb->asoc.peer_auth_chunks = sctp_alloc_chunklist(); |
michael@0 | 7515 | for (i = 0; i < num_chunks; i++) { |
michael@0 | 7516 | (void)sctp_auth_add_chunk(chunks->chunk_types[i], |
michael@0 | 7517 | stcb->asoc.peer_auth_chunks); |
michael@0 | 7518 | /* record asconf/asconf-ack if listed */ |
michael@0 | 7519 | if (chunks->chunk_types[i] == SCTP_ASCONF) |
michael@0 | 7520 | saw_asconf = 1; |
michael@0 | 7521 | if (chunks->chunk_types[i] == SCTP_ASCONF_ACK) |
michael@0 | 7522 | saw_asconf_ack = 1; |
michael@0 | 7523 | |
michael@0 | 7524 | } |
michael@0 | 7525 | got_chklist = 1; |
michael@0 | 7526 | } else if ((ptype == SCTP_HEARTBEAT_INFO) || |
michael@0 | 7527 | (ptype == SCTP_STATE_COOKIE) || |
michael@0 | 7528 | (ptype == SCTP_UNRECOG_PARAM) || |
michael@0 | 7529 | (ptype == SCTP_COOKIE_PRESERVE) || |
michael@0 | 7530 | (ptype == SCTP_SUPPORTED_ADDRTYPE) || |
michael@0 | 7531 | (ptype == SCTP_ADD_IP_ADDRESS) || |
michael@0 | 7532 | (ptype == SCTP_DEL_IP_ADDRESS) || |
michael@0 | 7533 | (ptype == SCTP_ERROR_CAUSE_IND) || |
michael@0 | 7534 | (ptype == SCTP_SUCCESS_REPORT)) { |
michael@0 | 7535 | /* don't care */ ; |
michael@0 | 7536 | } else { |
michael@0 | 7537 | if ((ptype & 0x8000) == 0x0000) { |
michael@0 | 7538 | /* |
michael@0 | 7539 | * must stop processing the rest of the |
michael@0 | 7540 | * param's. Any report bits were handled |
michael@0 | 7541 | * with the call to |
michael@0 | 7542 | * sctp_arethere_unrecognized_parameters() |
michael@0 | 7543 | * when the INIT or INIT-ACK was first seen. |
michael@0 | 7544 | */ |
michael@0 | 7545 | break; |
michael@0 | 7546 | } |
michael@0 | 7547 | } |
michael@0 | 7548 | |
michael@0 | 7549 | next_param: |
michael@0 | 7550 | offset += SCTP_SIZE32(plen); |
michael@0 | 7551 | if (offset >= limit) { |
michael@0 | 7552 | break; |
michael@0 | 7553 | } |
michael@0 | 7554 | phdr = sctp_get_next_param(m, offset, &parm_buf, |
michael@0 | 7555 | sizeof(parm_buf)); |
michael@0 | 7556 | } |
michael@0 | 7557 | /* Now check to see if we need to purge any addresses */ |
michael@0 | 7558 | TAILQ_FOREACH_SAFE(net, &stcb->asoc.nets, sctp_next, nnet) { |
michael@0 | 7559 | if ((net->dest_state & SCTP_ADDR_NOT_IN_ASSOC) == |
michael@0 | 7560 | SCTP_ADDR_NOT_IN_ASSOC) { |
michael@0 | 7561 | /* This address has been removed from the asoc */ |
michael@0 | 7562 | /* remove and free it */ |
michael@0 | 7563 | stcb->asoc.numnets--; |
michael@0 | 7564 | TAILQ_REMOVE(&stcb->asoc.nets, net, sctp_next); |
michael@0 | 7565 | sctp_free_remote_addr(net); |
michael@0 | 7566 | if (net == stcb->asoc.primary_destination) { |
michael@0 | 7567 | stcb->asoc.primary_destination = NULL; |
michael@0 | 7568 | sctp_select_primary_destination(stcb); |
michael@0 | 7569 | } |
michael@0 | 7570 | } |
michael@0 | 7571 | } |
michael@0 | 7572 | if (ecn_allowed == 0) { |
michael@0 | 7573 | stcb->asoc.ecn_allowed = 0; |
michael@0 | 7574 | } |
michael@0 | 7575 | /* validate authentication required parameters */ |
michael@0 | 7576 | if (got_random && got_hmacs) { |
michael@0 | 7577 | stcb->asoc.peer_supports_auth = 1; |
michael@0 | 7578 | } else { |
michael@0 | 7579 | stcb->asoc.peer_supports_auth = 0; |
michael@0 | 7580 | } |
michael@0 | 7581 | if (!stcb->asoc.peer_supports_auth && got_chklist) { |
michael@0 | 7582 | /* peer does not support auth but sent a chunks list? */ |
michael@0 | 7583 | return (-31); |
michael@0 | 7584 | } |
michael@0 | 7585 | if (!SCTP_BASE_SYSCTL(sctp_asconf_auth_nochk) && stcb->asoc.peer_supports_asconf && |
michael@0 | 7586 | !stcb->asoc.peer_supports_auth) { |
michael@0 | 7587 | /* peer supports asconf but not auth? */ |
michael@0 | 7588 | return (-32); |
michael@0 | 7589 | } else if ((stcb->asoc.peer_supports_asconf) && (stcb->asoc.peer_supports_auth) && |
michael@0 | 7590 | ((saw_asconf == 0) || (saw_asconf_ack == 0))) { |
michael@0 | 7591 | return (-33); |
michael@0 | 7592 | } |
michael@0 | 7593 | /* concatenate the full random key */ |
michael@0 | 7594 | keylen = sizeof(*p_random) + random_len + sizeof(*hmacs) + hmacs_len; |
michael@0 | 7595 | if (chunks != NULL) { |
michael@0 | 7596 | keylen += sizeof(*chunks) + num_chunks; |
michael@0 | 7597 | } |
michael@0 | 7598 | new_key = sctp_alloc_key(keylen); |
michael@0 | 7599 | if (new_key != NULL) { |
michael@0 | 7600 | /* copy in the RANDOM */ |
michael@0 | 7601 | if (p_random != NULL) { |
michael@0 | 7602 | keylen = sizeof(*p_random) + random_len; |
michael@0 | 7603 | bcopy(p_random, new_key->key, keylen); |
michael@0 | 7604 | } |
michael@0 | 7605 | /* append in the AUTH chunks */ |
michael@0 | 7606 | if (chunks != NULL) { |
michael@0 | 7607 | bcopy(chunks, new_key->key + keylen, |
michael@0 | 7608 | sizeof(*chunks) + num_chunks); |
michael@0 | 7609 | keylen += sizeof(*chunks) + num_chunks; |
michael@0 | 7610 | } |
michael@0 | 7611 | /* append in the HMACs */ |
michael@0 | 7612 | if (hmacs != NULL) { |
michael@0 | 7613 | bcopy(hmacs, new_key->key + keylen, |
michael@0 | 7614 | sizeof(*hmacs) + hmacs_len); |
michael@0 | 7615 | } |
michael@0 | 7616 | } else { |
michael@0 | 7617 | /* failed to get memory for the key */ |
michael@0 | 7618 | return (-34); |
michael@0 | 7619 | } |
michael@0 | 7620 | if (stcb->asoc.authinfo.peer_random != NULL) |
michael@0 | 7621 | sctp_free_key(stcb->asoc.authinfo.peer_random); |
michael@0 | 7622 | stcb->asoc.authinfo.peer_random = new_key; |
michael@0 | 7623 | sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid); |
michael@0 | 7624 | sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid); |
michael@0 | 7625 | |
michael@0 | 7626 | return (0); |
michael@0 | 7627 | } |
michael@0 | 7628 | |
michael@0 | 7629 | int |
michael@0 | 7630 | sctp_set_primary_addr(struct sctp_tcb *stcb, struct sockaddr *sa, |
michael@0 | 7631 | struct sctp_nets *net) |
michael@0 | 7632 | { |
michael@0 | 7633 | /* make sure the requested primary address exists in the assoc */ |
michael@0 | 7634 | if (net == NULL && sa) |
michael@0 | 7635 | net = sctp_findnet(stcb, sa); |
michael@0 | 7636 | |
michael@0 | 7637 | if (net == NULL) { |
michael@0 | 7638 | /* didn't find the requested primary address! */ |
michael@0 | 7639 | return (-1); |
michael@0 | 7640 | } else { |
michael@0 | 7641 | /* set the primary address */ |
michael@0 | 7642 | if (net->dest_state & SCTP_ADDR_UNCONFIRMED) { |
michael@0 | 7643 | /* Must be confirmed, so queue to set */ |
michael@0 | 7644 | net->dest_state |= SCTP_ADDR_REQ_PRIMARY; |
michael@0 | 7645 | return (0); |
michael@0 | 7646 | } |
michael@0 | 7647 | stcb->asoc.primary_destination = net; |
michael@0 | 7648 | if (!(net->dest_state & SCTP_ADDR_PF) && (stcb->asoc.alternate)) { |
michael@0 | 7649 | sctp_free_remote_addr(stcb->asoc.alternate); |
michael@0 | 7650 | stcb->asoc.alternate = NULL; |
michael@0 | 7651 | } |
michael@0 | 7652 | net = TAILQ_FIRST(&stcb->asoc.nets); |
michael@0 | 7653 | if (net != stcb->asoc.primary_destination) { |
michael@0 | 7654 | /* first one on the list is NOT the primary |
michael@0 | 7655 | * sctp_cmpaddr() is much more efficient if |
michael@0 | 7656 | * the primary is the first on the list, make it |
michael@0 | 7657 | * so. |
michael@0 | 7658 | */ |
michael@0 | 7659 | TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next); |
michael@0 | 7660 | TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next); |
michael@0 | 7661 | } |
michael@0 | 7662 | return (0); |
michael@0 | 7663 | } |
michael@0 | 7664 | } |
michael@0 | 7665 | |
michael@0 | 7666 | int |
michael@0 | 7667 | sctp_is_vtag_good(uint32_t tag, uint16_t lport, uint16_t rport, struct timeval *now) |
michael@0 | 7668 | { |
michael@0 | 7669 | /* |
michael@0 | 7670 | * This function serves two purposes. It will see if a TAG can be |
michael@0 | 7671 | * re-used and return 1 for yes it is ok and 0 for don't use that |
michael@0 | 7672 | * tag. A secondary function it will do is purge out old tags that |
michael@0 | 7673 | * can be removed. |
michael@0 | 7674 | */ |
michael@0 | 7675 | struct sctpvtaghead *chain; |
michael@0 | 7676 | struct sctp_tagblock *twait_block; |
michael@0 | 7677 | struct sctpasochead *head; |
michael@0 | 7678 | struct sctp_tcb *stcb; |
michael@0 | 7679 | int i; |
michael@0 | 7680 | |
michael@0 | 7681 | SCTP_INP_INFO_RLOCK(); |
michael@0 | 7682 | head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag, |
michael@0 | 7683 | SCTP_BASE_INFO(hashasocmark))]; |
michael@0 | 7684 | if (head == NULL) { |
michael@0 | 7685 | /* invalid vtag */ |
michael@0 | 7686 | goto skip_vtag_check; |
michael@0 | 7687 | } |
michael@0 | 7688 | LIST_FOREACH(stcb, head, sctp_asocs) { |
michael@0 | 7689 | /* We choose not to lock anything here. TCB's can't be |
michael@0 | 7690 | * removed since we have the read lock, so they can't |
michael@0 | 7691 | * be freed on us, same thing for the INP. I may |
michael@0 | 7692 | * be wrong with this assumption, but we will go |
michael@0 | 7693 | * with it for now :-) |
michael@0 | 7694 | */ |
michael@0 | 7695 | if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) { |
michael@0 | 7696 | continue; |
michael@0 | 7697 | } |
michael@0 | 7698 | if (stcb->asoc.my_vtag == tag) { |
michael@0 | 7699 | /* candidate */ |
michael@0 | 7700 | if (stcb->rport != rport) { |
michael@0 | 7701 | continue; |
michael@0 | 7702 | } |
michael@0 | 7703 | if (stcb->sctp_ep->sctp_lport != lport) { |
michael@0 | 7704 | continue; |
michael@0 | 7705 | } |
michael@0 | 7706 | /* Its a used tag set */ |
michael@0 | 7707 | SCTP_INP_INFO_RUNLOCK(); |
michael@0 | 7708 | return (0); |
michael@0 | 7709 | } |
michael@0 | 7710 | } |
michael@0 | 7711 | skip_vtag_check: |
michael@0 | 7712 | |
michael@0 | 7713 | chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)]; |
michael@0 | 7714 | /* Now what about timed wait ? */ |
michael@0 | 7715 | LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) { |
michael@0 | 7716 | /* |
michael@0 | 7717 | * Block(s) are present, lets see if we have this tag in the |
michael@0 | 7718 | * list |
michael@0 | 7719 | */ |
michael@0 | 7720 | for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) { |
michael@0 | 7721 | if (twait_block->vtag_block[i].v_tag == 0) { |
michael@0 | 7722 | /* not used */ |
michael@0 | 7723 | continue; |
michael@0 | 7724 | } else if ((long)twait_block->vtag_block[i].tv_sec_at_expire < |
michael@0 | 7725 | now->tv_sec) { |
michael@0 | 7726 | /* Audit expires this guy */ |
michael@0 | 7727 | twait_block->vtag_block[i].tv_sec_at_expire = 0; |
michael@0 | 7728 | twait_block->vtag_block[i].v_tag = 0; |
michael@0 | 7729 | twait_block->vtag_block[i].lport = 0; |
michael@0 | 7730 | twait_block->vtag_block[i].rport = 0; |
michael@0 | 7731 | } else if ((twait_block->vtag_block[i].v_tag == tag) && |
michael@0 | 7732 | (twait_block->vtag_block[i].lport == lport) && |
michael@0 | 7733 | (twait_block->vtag_block[i].rport == rport)) { |
michael@0 | 7734 | /* Bad tag, sorry :< */ |
michael@0 | 7735 | SCTP_INP_INFO_RUNLOCK(); |
michael@0 | 7736 | return (0); |
michael@0 | 7737 | } |
michael@0 | 7738 | } |
michael@0 | 7739 | } |
michael@0 | 7740 | SCTP_INP_INFO_RUNLOCK(); |
michael@0 | 7741 | return (1); |
michael@0 | 7742 | } |
michael@0 | 7743 | |
michael@0 | 7744 | static void |
michael@0 | 7745 | sctp_drain_mbufs(struct sctp_tcb *stcb) |
michael@0 | 7746 | { |
michael@0 | 7747 | /* |
michael@0 | 7748 | * We must hunt this association for MBUF's past the cumack (i.e. |
michael@0 | 7749 | * out of order data that we can renege on). |
michael@0 | 7750 | */ |
michael@0 | 7751 | struct sctp_association *asoc; |
michael@0 | 7752 | struct sctp_tmit_chunk *chk, *nchk; |
michael@0 | 7753 | uint32_t cumulative_tsn_p1; |
michael@0 | 7754 | struct sctp_queued_to_read *ctl, *nctl; |
michael@0 | 7755 | int cnt, strmat; |
michael@0 | 7756 | uint32_t gap, i; |
michael@0 | 7757 | int fnd = 0; |
michael@0 | 7758 | |
michael@0 | 7759 | /* We look for anything larger than the cum-ack + 1 */ |
michael@0 | 7760 | |
michael@0 | 7761 | asoc = &stcb->asoc; |
michael@0 | 7762 | if (asoc->cumulative_tsn == asoc->highest_tsn_inside_map) { |
michael@0 | 7763 | /* none we can reneg on. */ |
michael@0 | 7764 | return; |
michael@0 | 7765 | } |
michael@0 | 7766 | SCTP_STAT_INCR(sctps_protocol_drains_done); |
michael@0 | 7767 | cumulative_tsn_p1 = asoc->cumulative_tsn + 1; |
michael@0 | 7768 | cnt = 0; |
michael@0 | 7769 | /* First look in the re-assembly queue */ |
michael@0 | 7770 | TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) { |
michael@0 | 7771 | if (SCTP_TSN_GT(chk->rec.data.TSN_seq, cumulative_tsn_p1)) { |
michael@0 | 7772 | /* Yep it is above cum-ack */ |
michael@0 | 7773 | cnt++; |
michael@0 | 7774 | SCTP_CALC_TSN_TO_GAP(gap, chk->rec.data.TSN_seq, asoc->mapping_array_base_tsn); |
michael@0 | 7775 | asoc->size_on_reasm_queue = sctp_sbspace_sub(asoc->size_on_reasm_queue, chk->send_size); |
michael@0 | 7776 | sctp_ucount_decr(asoc->cnt_on_reasm_queue); |
michael@0 | 7777 | SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap); |
michael@0 | 7778 | TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next); |
michael@0 | 7779 | if (chk->data) { |
michael@0 | 7780 | sctp_m_freem(chk->data); |
michael@0 | 7781 | chk->data = NULL; |
michael@0 | 7782 | } |
michael@0 | 7783 | sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED); |
michael@0 | 7784 | } |
michael@0 | 7785 | } |
michael@0 | 7786 | /* Ok that was fun, now we will drain all the inbound streams? */ |
michael@0 | 7787 | for (strmat = 0; strmat < asoc->streamincnt; strmat++) { |
michael@0 | 7788 | TAILQ_FOREACH_SAFE(ctl, &asoc->strmin[strmat].inqueue, next, nctl) { |
michael@0 | 7789 | if (SCTP_TSN_GT(ctl->sinfo_tsn, cumulative_tsn_p1)) { |
michael@0 | 7790 | /* Yep it is above cum-ack */ |
michael@0 | 7791 | cnt++; |
michael@0 | 7792 | SCTP_CALC_TSN_TO_GAP(gap, ctl->sinfo_tsn, asoc->mapping_array_base_tsn); |
michael@0 | 7793 | asoc->size_on_all_streams = sctp_sbspace_sub(asoc->size_on_all_streams, ctl->length); |
michael@0 | 7794 | sctp_ucount_decr(asoc->cnt_on_all_streams); |
michael@0 | 7795 | SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap); |
michael@0 | 7796 | TAILQ_REMOVE(&asoc->strmin[strmat].inqueue, ctl, next); |
michael@0 | 7797 | if (ctl->data) { |
michael@0 | 7798 | sctp_m_freem(ctl->data); |
michael@0 | 7799 | ctl->data = NULL; |
michael@0 | 7800 | } |
michael@0 | 7801 | sctp_free_remote_addr(ctl->whoFrom); |
michael@0 | 7802 | SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), ctl); |
michael@0 | 7803 | SCTP_DECR_READQ_COUNT(); |
michael@0 | 7804 | } |
michael@0 | 7805 | } |
michael@0 | 7806 | } |
michael@0 | 7807 | if (cnt) { |
michael@0 | 7808 | /* We must back down to see what the new highest is */ |
michael@0 | 7809 | for (i = asoc->highest_tsn_inside_map; SCTP_TSN_GE(i, asoc->mapping_array_base_tsn); i--) { |
michael@0 | 7810 | SCTP_CALC_TSN_TO_GAP(gap, i, asoc->mapping_array_base_tsn); |
michael@0 | 7811 | if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) { |
michael@0 | 7812 | asoc->highest_tsn_inside_map = i; |
michael@0 | 7813 | fnd = 1; |
michael@0 | 7814 | break; |
michael@0 | 7815 | } |
michael@0 | 7816 | } |
michael@0 | 7817 | if (!fnd) { |
michael@0 | 7818 | asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn - 1; |
michael@0 | 7819 | } |
michael@0 | 7820 | |
michael@0 | 7821 | /* |
michael@0 | 7822 | * Question, should we go through the delivery queue? The only |
michael@0 | 7823 | * reason things are on here is the app not reading OR a p-d-api up. |
michael@0 | 7824 | * An attacker COULD send enough in to initiate the PD-API and then |
michael@0 | 7825 | * send a bunch of stuff to other streams... these would wind up on |
michael@0 | 7826 | * the delivery queue.. and then we would not get to them. But in |
michael@0 | 7827 | * order to do this I then have to back-track and un-deliver |
michael@0 | 7828 | * sequence numbers in streams.. el-yucko. I think for now we will |
michael@0 | 7829 | * NOT look at the delivery queue and leave it to be something to |
michael@0 | 7830 | * consider later. An alternative would be to abort the P-D-API with |
michael@0 | 7831 | * a notification and then deliver the data.... Or another method |
michael@0 | 7832 | * might be to keep track of how many times the situation occurs and |
michael@0 | 7833 | * if we see a possible attack underway just abort the association. |
michael@0 | 7834 | */ |
michael@0 | 7835 | #ifdef SCTP_DEBUG |
michael@0 | 7836 | SCTPDBG(SCTP_DEBUG_PCB1, "Freed %d chunks from reneg harvest\n", cnt); |
michael@0 | 7837 | #endif |
michael@0 | 7838 | /* |
michael@0 | 7839 | * Now do we need to find a new |
michael@0 | 7840 | * asoc->highest_tsn_inside_map? |
michael@0 | 7841 | */ |
michael@0 | 7842 | asoc->last_revoke_count = cnt; |
michael@0 | 7843 | (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer); |
michael@0 | 7844 | /*sa_ignore NO_NULL_CHK*/ |
michael@0 | 7845 | sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED); |
michael@0 | 7846 | sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_DRAIN, SCTP_SO_NOT_LOCKED); |
michael@0 | 7847 | } |
michael@0 | 7848 | /* |
michael@0 | 7849 | * Another issue, in un-setting the TSN's in the mapping array we |
michael@0 | 7850 | * DID NOT adjust the highest_tsn marker. This will cause one of two |
michael@0 | 7851 | * things to occur. It may cause us to do extra work in checking for |
michael@0 | 7852 | * our mapping array movement. More importantly it may cause us to |
michael@0 | 7853 | * SACK every datagram. This may not be a bad thing though since we |
michael@0 | 7854 | * will recover once we get our cum-ack above and all this stuff we |
michael@0 | 7855 | * dumped recovered. |
michael@0 | 7856 | */ |
michael@0 | 7857 | } |
michael@0 | 7858 | |
michael@0 | 7859 | void |
michael@0 | 7860 | sctp_drain() |
michael@0 | 7861 | { |
michael@0 | 7862 | /* |
michael@0 | 7863 | * We must walk the PCB lists for ALL associations here. The system |
michael@0 | 7864 | * is LOW on MBUF's and needs help. This is where reneging will |
michael@0 | 7865 | * occur. We really hope this does NOT happen! |
michael@0 | 7866 | */ |
michael@0 | 7867 | #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 |
michael@0 | 7868 | VNET_ITERATOR_DECL(vnet_iter); |
michael@0 | 7869 | #else |
michael@0 | 7870 | struct sctp_inpcb *inp; |
michael@0 | 7871 | struct sctp_tcb *stcb; |
michael@0 | 7872 | |
michael@0 | 7873 | SCTP_STAT_INCR(sctps_protocol_drain_calls); |
michael@0 | 7874 | if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) { |
michael@0 | 7875 | return; |
michael@0 | 7876 | } |
michael@0 | 7877 | #endif |
michael@0 | 7878 | #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 |
michael@0 | 7879 | VNET_LIST_RLOCK_NOSLEEP(); |
michael@0 | 7880 | VNET_FOREACH(vnet_iter) { |
michael@0 | 7881 | CURVNET_SET(vnet_iter); |
michael@0 | 7882 | struct sctp_inpcb *inp; |
michael@0 | 7883 | struct sctp_tcb *stcb; |
michael@0 | 7884 | #endif |
michael@0 | 7885 | |
michael@0 | 7886 | #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 |
michael@0 | 7887 | SCTP_STAT_INCR(sctps_protocol_drain_calls); |
michael@0 | 7888 | if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) { |
michael@0 | 7889 | #ifdef VIMAGE |
michael@0 | 7890 | continue; |
michael@0 | 7891 | #else |
michael@0 | 7892 | return; |
michael@0 | 7893 | #endif |
michael@0 | 7894 | } |
michael@0 | 7895 | #endif |
michael@0 | 7896 | SCTP_INP_INFO_RLOCK(); |
michael@0 | 7897 | LIST_FOREACH(inp, &SCTP_BASE_INFO(listhead), sctp_list) { |
michael@0 | 7898 | /* For each endpoint */ |
michael@0 | 7899 | SCTP_INP_RLOCK(inp); |
michael@0 | 7900 | LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { |
michael@0 | 7901 | /* For each association */ |
michael@0 | 7902 | SCTP_TCB_LOCK(stcb); |
michael@0 | 7903 | sctp_drain_mbufs(stcb); |
michael@0 | 7904 | SCTP_TCB_UNLOCK(stcb); |
michael@0 | 7905 | } |
michael@0 | 7906 | SCTP_INP_RUNLOCK(inp); |
michael@0 | 7907 | } |
michael@0 | 7908 | SCTP_INP_INFO_RUNLOCK(); |
michael@0 | 7909 | #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 |
michael@0 | 7910 | CURVNET_RESTORE(); |
michael@0 | 7911 | } |
michael@0 | 7912 | VNET_LIST_RUNLOCK_NOSLEEP(); |
michael@0 | 7913 | #endif |
michael@0 | 7914 | } |
michael@0 | 7915 | |
michael@0 | 7916 | /* |
michael@0 | 7917 | * start a new iterator |
michael@0 | 7918 | * iterates through all endpoints and associations based on the pcb_state |
michael@0 | 7919 | * flags and asoc_state. "af" (mandatory) is executed for all matching |
michael@0 | 7920 | * assocs and "ef" (optional) is executed when the iterator completes. |
michael@0 | 7921 | * "inpf" (optional) is executed for each new endpoint as it is being |
michael@0 | 7922 | * iterated through. inpe (optional) is called when the inp completes |
michael@0 | 7923 | * its way through all the stcbs. |
michael@0 | 7924 | */ |
michael@0 | 7925 | int |
michael@0 | 7926 | sctp_initiate_iterator(inp_func inpf, |
michael@0 | 7927 | asoc_func af, |
michael@0 | 7928 | inp_func inpe, |
michael@0 | 7929 | uint32_t pcb_state, |
michael@0 | 7930 | uint32_t pcb_features, |
michael@0 | 7931 | uint32_t asoc_state, |
michael@0 | 7932 | void *argp, |
michael@0 | 7933 | uint32_t argi, |
michael@0 | 7934 | end_func ef, |
michael@0 | 7935 | struct sctp_inpcb *s_inp, |
michael@0 | 7936 | uint8_t chunk_output_off) |
michael@0 | 7937 | { |
michael@0 | 7938 | struct sctp_iterator *it = NULL; |
michael@0 | 7939 | |
michael@0 | 7940 | if (af == NULL) { |
michael@0 | 7941 | return (-1); |
michael@0 | 7942 | } |
michael@0 | 7943 | SCTP_MALLOC(it, struct sctp_iterator *, sizeof(struct sctp_iterator), |
michael@0 | 7944 | SCTP_M_ITER); |
michael@0 | 7945 | if (it == NULL) { |
michael@0 | 7946 | SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM); |
michael@0 | 7947 | return (ENOMEM); |
michael@0 | 7948 | } |
michael@0 | 7949 | memset(it, 0, sizeof(*it)); |
michael@0 | 7950 | it->function_assoc = af; |
michael@0 | 7951 | it->function_inp = inpf; |
michael@0 | 7952 | if (inpf) |
michael@0 | 7953 | it->done_current_ep = 0; |
michael@0 | 7954 | else |
michael@0 | 7955 | it->done_current_ep = 1; |
michael@0 | 7956 | it->function_atend = ef; |
michael@0 | 7957 | it->pointer = argp; |
michael@0 | 7958 | it->val = argi; |
michael@0 | 7959 | it->pcb_flags = pcb_state; |
michael@0 | 7960 | it->pcb_features = pcb_features; |
michael@0 | 7961 | it->asoc_state = asoc_state; |
michael@0 | 7962 | it->function_inp_end = inpe; |
michael@0 | 7963 | it->no_chunk_output = chunk_output_off; |
michael@0 | 7964 | #if defined(__FreeBSD__) && __FreeBSD_version >= 801000 |
michael@0 | 7965 | it->vn = curvnet; |
michael@0 | 7966 | #endif |
michael@0 | 7967 | if (s_inp) { |
michael@0 | 7968 | /* Assume lock is held here */ |
michael@0 | 7969 | it->inp = s_inp; |
michael@0 | 7970 | SCTP_INP_INCR_REF(it->inp); |
michael@0 | 7971 | it->iterator_flags = SCTP_ITERATOR_DO_SINGLE_INP; |
michael@0 | 7972 | } else { |
michael@0 | 7973 | SCTP_INP_INFO_RLOCK(); |
michael@0 | 7974 | it->inp = LIST_FIRST(&SCTP_BASE_INFO(listhead)); |
michael@0 | 7975 | if (it->inp) { |
michael@0 | 7976 | SCTP_INP_INCR_REF(it->inp); |
michael@0 | 7977 | } |
michael@0 | 7978 | SCTP_INP_INFO_RUNLOCK(); |
michael@0 | 7979 | it->iterator_flags = SCTP_ITERATOR_DO_ALL_INP; |
michael@0 | 7980 | |
michael@0 | 7981 | } |
michael@0 | 7982 | SCTP_IPI_ITERATOR_WQ_LOCK(); |
michael@0 | 7983 | |
michael@0 | 7984 | TAILQ_INSERT_TAIL(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr); |
michael@0 | 7985 | if (sctp_it_ctl.iterator_running == 0) { |
michael@0 | 7986 | sctp_wakeup_iterator(); |
michael@0 | 7987 | } |
michael@0 | 7988 | SCTP_IPI_ITERATOR_WQ_UNLOCK(); |
michael@0 | 7989 | /* sa_ignore MEMLEAK {memory is put on the tailq for the iterator} */ |
michael@0 | 7990 | return (0); |
michael@0 | 7991 | } |