2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
5 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * a) Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
14 * b) Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the distribution.
18 * c) Neither the name of Cisco Systems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
35 #if defined(__FreeBSD__) && !defined(__Userspace__)
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD: head/sys/netinet/sctp_pcb.c 363456 2020-07-23 19:43:49Z tuexen $");
40 #include <netinet/sctp_os.h>
41 #if defined(__FreeBSD__) && !defined(__Userspace__)
44 #include <netinet/sctp_var.h>
45 #include <netinet/sctp_sysctl.h>
46 #include <netinet/sctp_pcb.h>
47 #include <netinet/sctputil.h>
48 #include <netinet/sctp.h>
49 #include <netinet/sctp_header.h>
50 #include <netinet/sctp_asconf.h>
51 #include <netinet/sctp_output.h>
52 #include <netinet/sctp_timer.h>
53 #include <netinet/sctp_bsd_addr.h>
54 #if defined(INET) || defined(INET6)
56 #include <netinet/udp.h>
60 #if defined(__Userspace__)
61 #include "user_ip6_var.h"
63 #include <netinet6/ip6_var.h>
66 #if defined(__FreeBSD__) && !defined(__Userspace__)
67 #include <sys/sched.h>
69 #include <sys/unistd.h>
71 #if defined(__Userspace__)
72 #include <user_socketvar.h>
73 #include <user_atomic.h>
79 #if !defined(__FreeBSD__) || defined(__Userspace__)
80 struct sctp_base_info system_base_info;
83 /* FIX: we don't handle multiple link local scopes */
84 /* "scopeless" replacement IN6_ARE_ADDR_EQUAL */
87 SCTP6_ARE_ADDR_EQUAL(struct sockaddr_in6 *a, struct sockaddr_in6 *b)
89 #ifdef SCTP_EMBEDDED_V6_SCOPE
90 #if defined(__APPLE__) && !defined(__Userspace__)
91 struct in6_addr tmp_a, tmp_b;
94 #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
95 if (in6_embedscope(&tmp_a, a, NULL, NULL) != 0) {
97 if (in6_embedscope(&tmp_a, a, NULL, NULL, NULL) != 0) {
101 tmp_b = b->sin6_addr;
102 #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
103 if (in6_embedscope(&tmp_b, b, NULL, NULL) != 0) {
105 if (in6_embedscope(&tmp_b, b, NULL, NULL, NULL) != 0) {
109 return (IN6_ARE_ADDR_EQUAL(&tmp_a, &tmp_b));
110 #elif defined(SCTP_KAME)
111 struct sockaddr_in6 tmp_a, tmp_b;
113 memcpy(&tmp_a, a, sizeof(struct sockaddr_in6));
114 if (sa6_embedscope(&tmp_a, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
117 memcpy(&tmp_b, b, sizeof(struct sockaddr_in6));
118 if (sa6_embedscope(&tmp_b, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
121 return (IN6_ARE_ADDR_EQUAL(&tmp_a.sin6_addr, &tmp_b.sin6_addr));
123 struct in6_addr tmp_a, tmp_b;
125 tmp_a = a->sin6_addr;
126 if (in6_embedscope(&tmp_a, a) != 0) {
129 tmp_b = b->sin6_addr;
130 if (in6_embedscope(&tmp_b, b) != 0) {
133 return (IN6_ARE_ADDR_EQUAL(&tmp_a, &tmp_b));
136 return (IN6_ARE_ADDR_EQUAL(&(a->sin6_addr), &(b->sin6_addr)));
137 #endif /* SCTP_EMBEDDED_V6_SCOPE */
142 sctp_fill_pcbinfo(struct sctp_pcbinfo *spcb)
145 * We really don't need to lock this, but I will just because it
148 SCTP_INP_INFO_RLOCK();
149 spcb->ep_count = SCTP_BASE_INFO(ipi_count_ep);
150 spcb->asoc_count = SCTP_BASE_INFO(ipi_count_asoc);
151 spcb->laddr_count = SCTP_BASE_INFO(ipi_count_laddr);
152 spcb->raddr_count = SCTP_BASE_INFO(ipi_count_raddr);
153 spcb->chk_count = SCTP_BASE_INFO(ipi_count_chunk);
154 spcb->readq_count = SCTP_BASE_INFO(ipi_count_readq);
155 spcb->stream_oque = SCTP_BASE_INFO(ipi_count_strmoq);
156 spcb->free_chunks = SCTP_BASE_INFO(ipi_free_chunks);
157 SCTP_INP_INFO_RUNLOCK();
161 * Addresses are added to VRF's (Virtual Router's). For BSD we
162 * have only the default VRF 0. We maintain a hash list of
163 * VRF's. Each VRF has its own list of sctp_ifn's. Each of
164 * these has a list of addresses. When we add a new address
165 * to a VRF we lookup the ifn/ifn_index, if the ifn does
166 * not exist we create it and add it to the list of IFN's
167 * within the VRF. Once we have the sctp_ifn, we add the
168 * address to the list. So we look something like:
171 * vrf-> ifn-> ifn -> ifn
173 * ... +--ifa-> ifa -> ifa
176 * We keep these separate lists since the SCTP subsystem will
177 * point to these from its source address selection nets structure.
178 * When an address is deleted it does not happen right away on
179 * the SCTP side, it gets scheduled. What we do when a
180 * delete happens is immediately remove the address from
181 * the master list and decrement the refcount. As our
182 * addip iterator works through and frees the src address
183 * selection pointing to the sctp_ifa, eventually the refcount
184 * will reach 0 and we will delete it. Note that it is assumed
185 * that any locking on system level ifn/ifa is done at the
186 * caller of these functions and these routines will only
187 * lock the SCTP structures as they add or delete things.
189 * Other notes on VRF concepts.
190 * - An endpoint can be in multiple VRF's
191 * - An association lives within a VRF and only one VRF.
192 * - Any incoming packet we can deduce the VRF for by
193 * looking at the mbuf/pak inbound (for BSD its VRF=0 :D)
194 * - Any downward send call or connect call must supply the
195 * VRF via ancillary data or via some sort of set default
196 * VRF socket option call (again for BSD no brainer since
197 * the VRF is always 0).
198 * - An endpoint may add multiple VRF's to it.
199 * - Listening sockets can accept associations in any
200 * of the VRF's they are in but the assoc will end up
201 * in only one VRF (gotten from the packet or connect/send).
206 sctp_allocate_vrf(int vrf_id)
208 struct sctp_vrf *vrf = NULL;
209 struct sctp_vrflist *bucket;
211 /* First allocate the VRF structure */
212 vrf = sctp_find_vrf(vrf_id);
214 /* Already allocated */
217 SCTP_MALLOC(vrf, struct sctp_vrf *, sizeof(struct sctp_vrf),
222 panic("No memory for VRF:%d", vrf_id);
227 memset(vrf, 0, sizeof(struct sctp_vrf));
228 vrf->vrf_id = vrf_id;
229 LIST_INIT(&vrf->ifnlist);
230 vrf->total_ifa_count = 0;
232 /* now also setup table ids */
233 SCTP_INIT_VRF_TABLEID(vrf);
234 /* Init the HASH of addresses */
235 vrf->vrf_addr_hash = SCTP_HASH_INIT(SCTP_VRF_ADDR_HASH_SIZE,
236 &vrf->vrf_addr_hashmark);
237 if (vrf->vrf_addr_hash == NULL) {
240 panic("No memory for VRF:%d", vrf_id);
242 SCTP_FREE(vrf, SCTP_M_VRF);
246 /* Add it to the hash table */
247 bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(vrf_id & SCTP_BASE_INFO(hashvrfmark))];
248 LIST_INSERT_HEAD(bucket, vrf, next_vrf);
249 atomic_add_int(&SCTP_BASE_INFO(ipi_count_vrfs), 1);
255 sctp_find_ifn(void *ifn, uint32_t ifn_index)
257 struct sctp_ifn *sctp_ifnp;
258 struct sctp_ifnlist *hash_ifn_head;
260 /* We assume the lock is held for the addresses
261 * if that's wrong problems could occur :-)
263 SCTP_IPI_ADDR_LOCK_ASSERT();
264 hash_ifn_head = &SCTP_BASE_INFO(vrf_ifn_hash)[(ifn_index & SCTP_BASE_INFO(vrf_ifn_hashmark))];
265 LIST_FOREACH(sctp_ifnp, hash_ifn_head, next_bucket) {
266 if (sctp_ifnp->ifn_index == ifn_index) {
269 if (sctp_ifnp->ifn_p && ifn && (sctp_ifnp->ifn_p == ifn)) {
278 sctp_find_vrf(uint32_t vrf_id)
280 struct sctp_vrflist *bucket;
281 struct sctp_vrf *liste;
283 bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(vrf_id & SCTP_BASE_INFO(hashvrfmark))];
284 LIST_FOREACH(liste, bucket, next_vrf) {
285 if (vrf_id == liste->vrf_id) {
294 sctp_free_vrf(struct sctp_vrf *vrf)
296 if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&vrf->refcount)) {
297 if (vrf->vrf_addr_hash) {
298 SCTP_HASH_FREE(vrf->vrf_addr_hash, vrf->vrf_addr_hashmark);
299 vrf->vrf_addr_hash = NULL;
301 /* We zero'd the count */
302 LIST_REMOVE(vrf, next_vrf);
303 SCTP_FREE(vrf, SCTP_M_VRF);
304 atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_vrfs), 1);
310 sctp_free_ifn(struct sctp_ifn *sctp_ifnp)
312 if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&sctp_ifnp->refcount)) {
313 /* We zero'd the count */
314 if (sctp_ifnp->vrf) {
315 sctp_free_vrf(sctp_ifnp->vrf);
317 SCTP_FREE(sctp_ifnp, SCTP_M_IFN);
318 atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_ifns), 1);
324 sctp_update_ifn_mtu(uint32_t ifn_index, uint32_t mtu)
326 struct sctp_ifn *sctp_ifnp;
328 sctp_ifnp = sctp_find_ifn((void *)NULL, ifn_index);
329 if (sctp_ifnp != NULL) {
330 sctp_ifnp->ifn_mtu = mtu;
336 sctp_free_ifa(struct sctp_ifa *sctp_ifap)
338 if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&sctp_ifap->refcount)) {
339 /* We zero'd the count */
340 if (sctp_ifap->ifn_p) {
341 sctp_free_ifn(sctp_ifap->ifn_p);
343 SCTP_FREE(sctp_ifap, SCTP_M_IFA);
344 atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_ifas), 1);
350 sctp_delete_ifn(struct sctp_ifn *sctp_ifnp, int hold_addr_lock)
352 struct sctp_ifn *found;
354 found = sctp_find_ifn(sctp_ifnp->ifn_p, sctp_ifnp->ifn_index);
356 /* Not in the list.. sorry */
359 if (hold_addr_lock == 0) {
360 SCTP_IPI_ADDR_WLOCK();
362 SCTP_IPI_ADDR_WLOCK_ASSERT();
364 LIST_REMOVE(sctp_ifnp, next_bucket);
365 LIST_REMOVE(sctp_ifnp, next_ifn);
366 if (hold_addr_lock == 0) {
367 SCTP_IPI_ADDR_WUNLOCK();
369 /* Take away the reference, and possibly free it */
370 sctp_free_ifn(sctp_ifnp);
375 sctp_mark_ifa_addr_down(uint32_t vrf_id, struct sockaddr *addr,
376 const char *if_name, uint32_t ifn_index)
378 struct sctp_vrf *vrf;
379 struct sctp_ifa *sctp_ifap;
381 SCTP_IPI_ADDR_RLOCK();
382 vrf = sctp_find_vrf(vrf_id);
384 SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id);
388 sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
389 if (sctp_ifap == NULL) {
390 SCTPDBG(SCTP_DEBUG_PCB4, "Can't find sctp_ifap for address\n");
393 if (sctp_ifap->ifn_p == NULL) {
394 SCTPDBG(SCTP_DEBUG_PCB4, "IFA has no IFN - can't mark unusable\n");
398 if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, SCTP_IFNAMSIZ) != 0) {
399 SCTPDBG(SCTP_DEBUG_PCB4, "IFN %s of IFA not the same as %s\n",
400 sctp_ifap->ifn_p->ifn_name, if_name);
404 if (sctp_ifap->ifn_p->ifn_index != ifn_index) {
405 SCTPDBG(SCTP_DEBUG_PCB4, "IFA owned by ifn_index:%d down command for ifn_index:%d - ignored\n",
406 sctp_ifap->ifn_p->ifn_index, ifn_index);
411 sctp_ifap->localifa_flags &= (~SCTP_ADDR_VALID);
412 sctp_ifap->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE;
414 SCTP_IPI_ADDR_RUNLOCK();
419 sctp_mark_ifa_addr_up(uint32_t vrf_id, struct sockaddr *addr,
420 const char *if_name, uint32_t ifn_index)
422 struct sctp_vrf *vrf;
423 struct sctp_ifa *sctp_ifap;
425 SCTP_IPI_ADDR_RLOCK();
426 vrf = sctp_find_vrf(vrf_id);
428 SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id);
432 sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
433 if (sctp_ifap == NULL) {
434 SCTPDBG(SCTP_DEBUG_PCB4, "Can't find sctp_ifap for address\n");
437 if (sctp_ifap->ifn_p == NULL) {
438 SCTPDBG(SCTP_DEBUG_PCB4, "IFA has no IFN - can't mark unusable\n");
442 if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, SCTP_IFNAMSIZ) != 0) {
443 SCTPDBG(SCTP_DEBUG_PCB4, "IFN %s of IFA not the same as %s\n",
444 sctp_ifap->ifn_p->ifn_name, if_name);
448 if (sctp_ifap->ifn_p->ifn_index != ifn_index) {
449 SCTPDBG(SCTP_DEBUG_PCB4, "IFA owned by ifn_index:%d down command for ifn_index:%d - ignored\n",
450 sctp_ifap->ifn_p->ifn_index, ifn_index);
455 sctp_ifap->localifa_flags &= (~SCTP_ADDR_IFA_UNUSEABLE);
456 sctp_ifap->localifa_flags |= SCTP_ADDR_VALID;
458 SCTP_IPI_ADDR_RUNLOCK();
463 * Add an ifa to an ifn.
464 * Register the interface as necessary.
465 * NOTE: ADDR write lock MUST be held.
468 sctp_add_ifa_to_ifn(struct sctp_ifn *sctp_ifnp, struct sctp_ifa *sctp_ifap)
472 LIST_INSERT_HEAD(&sctp_ifnp->ifalist, sctp_ifap, next_ifa);
473 sctp_ifap->ifn_p = sctp_ifnp;
474 atomic_add_int(&sctp_ifap->ifn_p->refcount, 1);
475 /* update address counts */
476 sctp_ifnp->ifa_count++;
477 ifa_af = sctp_ifap->address.sa.sa_family;
492 if (sctp_ifnp->ifa_count == 1) {
493 /* register the new interface */
494 sctp_ifnp->registered_af = ifa_af;
500 * Remove an ifa from its ifn.
501 * If no more addresses exist, remove the ifn too. Otherwise, re-register
502 * the interface based on the remaining address families left.
503 * NOTE: ADDR write lock MUST be held.
506 sctp_remove_ifa_from_ifn(struct sctp_ifa *sctp_ifap)
508 LIST_REMOVE(sctp_ifap, next_ifa);
509 if (sctp_ifap->ifn_p) {
510 /* update address counts */
511 sctp_ifap->ifn_p->ifa_count--;
512 switch (sctp_ifap->address.sa.sa_family) {
515 sctp_ifap->ifn_p->num_v4--;
520 sctp_ifap->ifn_p->num_v6--;
527 if (LIST_EMPTY(&sctp_ifap->ifn_p->ifalist)) {
528 /* remove the ifn, possibly freeing it */
529 sctp_delete_ifn(sctp_ifap->ifn_p, SCTP_ADDR_LOCKED);
531 /* re-register address family type, if needed */
532 if ((sctp_ifap->ifn_p->num_v6 == 0) &&
533 (sctp_ifap->ifn_p->registered_af == AF_INET6)) {
534 sctp_ifap->ifn_p->registered_af = AF_INET;
535 } else if ((sctp_ifap->ifn_p->num_v4 == 0) &&
536 (sctp_ifap->ifn_p->registered_af == AF_INET)) {
537 sctp_ifap->ifn_p->registered_af = AF_INET6;
539 /* free the ifn refcount */
540 sctp_free_ifn(sctp_ifap->ifn_p);
542 sctp_ifap->ifn_p = NULL;
548 sctp_add_addr_to_vrf(uint32_t vrf_id, void *ifn, uint32_t ifn_index,
549 uint32_t ifn_type, const char *if_name, void *ifa,
550 struct sockaddr *addr, uint32_t ifa_flags,
553 struct sctp_vrf *vrf;
554 struct sctp_ifn *sctp_ifnp, *new_sctp_ifnp;
555 struct sctp_ifa *sctp_ifap, *new_sctp_ifap;
556 struct sctp_ifalist *hash_addr_head;
557 struct sctp_ifnlist *hash_ifn_head;
558 uint32_t hash_of_addr;
562 SCTPDBG(SCTP_DEBUG_PCB4, "vrf_id 0x%x: adding address: ", vrf_id);
563 SCTPDBG_ADDR(SCTP_DEBUG_PCB4, addr);
565 SCTP_MALLOC(new_sctp_ifnp, struct sctp_ifn *,
566 sizeof(struct sctp_ifn), SCTP_M_IFN);
567 if (new_sctp_ifnp == NULL) {
569 panic("No memory for IFN");
573 SCTP_MALLOC(new_sctp_ifap, struct sctp_ifa *, sizeof(struct sctp_ifa), SCTP_M_IFA);
574 if (new_sctp_ifap == NULL) {
576 panic("No memory for IFA");
578 SCTP_FREE(new_sctp_ifnp, SCTP_M_IFN);
582 SCTP_IPI_ADDR_WLOCK();
583 sctp_ifnp = sctp_find_ifn(ifn, ifn_index);
585 vrf = sctp_ifnp->vrf;
587 vrf = sctp_find_vrf(vrf_id);
589 vrf = sctp_allocate_vrf(vrf_id);
591 SCTP_IPI_ADDR_WUNLOCK();
592 SCTP_FREE(new_sctp_ifnp, SCTP_M_IFN);
593 SCTP_FREE(new_sctp_ifap, SCTP_M_IFA);
598 if (sctp_ifnp == NULL) {
599 /* build one and add it, can't hold lock
600 * until after malloc done though.
602 sctp_ifnp = new_sctp_ifnp;
603 new_sctp_ifnp = NULL;
604 memset(sctp_ifnp, 0, sizeof(struct sctp_ifn));
605 sctp_ifnp->ifn_index = ifn_index;
606 sctp_ifnp->ifn_p = ifn;
607 sctp_ifnp->ifn_type = ifn_type;
608 sctp_ifnp->refcount = 0;
609 sctp_ifnp->vrf = vrf;
610 atomic_add_int(&vrf->refcount, 1);
611 sctp_ifnp->ifn_mtu = SCTP_GATHER_MTU_FROM_IFN_INFO(ifn, ifn_index, addr->sa_family);
612 if (if_name != NULL) {
613 SCTP_SNPRINTF(sctp_ifnp->ifn_name, SCTP_IFNAMSIZ, "%s", if_name);
615 SCTP_SNPRINTF(sctp_ifnp->ifn_name, SCTP_IFNAMSIZ, "%s", "unknown");
617 hash_ifn_head = &SCTP_BASE_INFO(vrf_ifn_hash)[(ifn_index & SCTP_BASE_INFO(vrf_ifn_hashmark))];
618 LIST_INIT(&sctp_ifnp->ifalist);
619 LIST_INSERT_HEAD(hash_ifn_head, sctp_ifnp, next_bucket);
620 LIST_INSERT_HEAD(&vrf->ifnlist, sctp_ifnp, next_ifn);
621 atomic_add_int(&SCTP_BASE_INFO(ipi_count_ifns), 1);
624 sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
626 /* Hmm, it already exists? */
627 if ((sctp_ifap->ifn_p) &&
628 (sctp_ifap->ifn_p->ifn_index == ifn_index)) {
629 SCTPDBG(SCTP_DEBUG_PCB4, "Using existing ifn %s (0x%x) for ifa %p\n",
630 sctp_ifap->ifn_p->ifn_name, ifn_index,
633 /* Remove the created one that we don't want */
634 sctp_delete_ifn(sctp_ifnp, SCTP_ADDR_LOCKED);
636 if (sctp_ifap->localifa_flags & SCTP_BEING_DELETED) {
637 /* easy to solve, just switch back to active */
638 SCTPDBG(SCTP_DEBUG_PCB4, "Clearing deleted ifa flag\n");
639 sctp_ifap->localifa_flags = SCTP_ADDR_VALID;
640 sctp_ifap->ifn_p = sctp_ifnp;
641 atomic_add_int(&sctp_ifap->ifn_p->refcount, 1);
644 SCTP_IPI_ADDR_WUNLOCK();
645 if (new_sctp_ifnp != NULL) {
646 SCTP_FREE(new_sctp_ifnp, SCTP_M_IFN);
648 SCTP_FREE(new_sctp_ifap, SCTP_M_IFA);
651 if (sctp_ifap->ifn_p) {
653 * The last IFN gets the address, remove the
656 SCTPDBG(SCTP_DEBUG_PCB4, "Moving ifa %p from %s (0x%x) to %s (0x%x)\n",
657 (void *)sctp_ifap, sctp_ifap->ifn_p->ifn_name,
658 sctp_ifap->ifn_p->ifn_index, if_name,
660 /* remove the address from the old ifn */
661 sctp_remove_ifa_from_ifn(sctp_ifap);
662 /* move the address over to the new ifn */
663 sctp_add_ifa_to_ifn(sctp_ifnp, sctp_ifap);
664 goto exit_stage_left;
666 /* repair ifnp which was NULL ? */
667 sctp_ifap->localifa_flags = SCTP_ADDR_VALID;
668 SCTPDBG(SCTP_DEBUG_PCB4, "Repairing ifn %p for ifa %p\n",
669 (void *)sctp_ifnp, (void *)sctp_ifap);
670 sctp_add_ifa_to_ifn(sctp_ifnp, sctp_ifap);
672 goto exit_stage_left;
675 sctp_ifap = new_sctp_ifap;
676 memset(sctp_ifap, 0, sizeof(struct sctp_ifa));
677 sctp_ifap->ifn_p = sctp_ifnp;
678 atomic_add_int(&sctp_ifnp->refcount, 1);
679 sctp_ifap->vrf_id = vrf_id;
680 sctp_ifap->ifa = ifa;
682 memcpy(&sctp_ifap->address, addr, addr->sa_len);
684 switch (addr->sa_family) {
687 memcpy(&sctp_ifap->address, addr, sizeof(struct sockaddr_in));
692 memcpy(&sctp_ifap->address, addr, sizeof(struct sockaddr_in6));
695 #if defined(__Userspace__)
697 memcpy(&sctp_ifap->address, addr, sizeof(struct sockaddr_conn));
705 sctp_ifap->localifa_flags = SCTP_ADDR_VALID | SCTP_ADDR_DEFER_USE;
706 sctp_ifap->flags = ifa_flags;
708 switch (sctp_ifap->address.sa.sa_family) {
712 struct sockaddr_in *sin;
714 sin = &sctp_ifap->address.sin;
715 if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) ||
716 (IN4_ISLOOPBACK_ADDRESS(&sin->sin_addr))) {
717 sctp_ifap->src_is_loop = 1;
719 if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
720 sctp_ifap->src_is_priv = 1;
724 new_ifn_af = AF_INET;
731 /* ok to use deprecated addresses? */
732 struct sockaddr_in6 *sin6;
734 sin6 = &sctp_ifap->address.sin6;
735 if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) ||
736 (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))) {
737 sctp_ifap->src_is_loop = 1;
739 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
740 sctp_ifap->src_is_priv = 1;
744 new_ifn_af = AF_INET6;
748 #if defined(__Userspace__)
751 new_ifn_af = AF_CONN;
758 hash_of_addr = sctp_get_ifa_hash_val(&sctp_ifap->address.sa);
760 if ((sctp_ifap->src_is_priv == 0) &&
761 (sctp_ifap->src_is_loop == 0)) {
762 sctp_ifap->src_is_glob = 1;
764 hash_addr_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)];
765 LIST_INSERT_HEAD(hash_addr_head, sctp_ifap, next_bucket);
766 sctp_ifap->refcount = 1;
767 LIST_INSERT_HEAD(&sctp_ifnp->ifalist, sctp_ifap, next_ifa);
768 sctp_ifnp->ifa_count++;
769 vrf->total_ifa_count++;
770 atomic_add_int(&SCTP_BASE_INFO(ipi_count_ifas), 1);
772 sctp_ifnp->registered_af = new_ifn_af;
774 SCTP_IPI_ADDR_WUNLOCK();
775 if (new_sctp_ifnp != NULL) {
776 SCTP_FREE(new_sctp_ifnp, SCTP_M_IFN);
780 /* Bump up the refcount so that when the timer
781 * completes it will drop back down.
783 struct sctp_laddr *wi;
785 atomic_add_int(&sctp_ifap->refcount, 1);
786 wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
789 * Gak, what can we do? We have lost an address
790 * change can you say HOSED?
792 SCTPDBG(SCTP_DEBUG_PCB4, "Lost an address change?\n");
793 /* Opps, must decrement the count */
794 sctp_del_addr_from_vrf(vrf_id, addr, ifn_index,
798 SCTP_INCR_LADDR_COUNT();
799 memset(wi, 0, sizeof(*wi));
800 (void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
802 wi->action = SCTP_ADD_IP_ADDRESS;
805 LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
806 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
807 (struct sctp_inpcb *)NULL,
808 (struct sctp_tcb *)NULL,
809 (struct sctp_nets *)NULL);
810 SCTP_WQ_ADDR_UNLOCK();
812 /* it's ready for use */
813 sctp_ifap->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
819 sctp_del_addr_from_vrf(uint32_t vrf_id, struct sockaddr *addr,
820 uint32_t ifn_index, const char *if_name)
822 struct sctp_vrf *vrf;
823 struct sctp_ifa *sctp_ifap = NULL;
825 SCTP_IPI_ADDR_WLOCK();
826 vrf = sctp_find_vrf(vrf_id);
828 SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id);
833 SCTPDBG(SCTP_DEBUG_PCB4, "vrf_id 0x%x: deleting address:", vrf_id);
834 SCTPDBG_ADDR(SCTP_DEBUG_PCB4, addr);
836 sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
838 /* Validate the delete */
839 if (sctp_ifap->ifn_p) {
842 * The name has priority over the ifn_index
846 if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, SCTP_IFNAMSIZ) == 0) {
847 /* They match its a correct delete */
852 /* last ditch check ifn_index */
853 if (ifn_index == sctp_ifap->ifn_p->ifn_index) {
858 SCTPDBG(SCTP_DEBUG_PCB4, "ifn:%d ifname:%s does not match addresses\n",
859 ifn_index, ((if_name == NULL) ? "NULL" : if_name));
860 SCTPDBG(SCTP_DEBUG_PCB4, "ifn:%d ifname:%s - ignoring delete\n",
861 sctp_ifap->ifn_p->ifn_index, sctp_ifap->ifn_p->ifn_name);
862 SCTP_IPI_ADDR_WUNLOCK();
866 SCTPDBG(SCTP_DEBUG_PCB4, "Deleting ifa %p\n", (void *)sctp_ifap);
867 sctp_ifap->localifa_flags &= SCTP_ADDR_VALID;
869 * We don't set the flag. This means that the structure will
870 * hang around in EP's that have bound specific to it until
871 * they close. This gives us TCP like behavior if someone
872 * removes an address (or for that matter adds it right back).
874 /* sctp_ifap->localifa_flags |= SCTP_BEING_DELETED; */
875 vrf->total_ifa_count--;
876 LIST_REMOVE(sctp_ifap, next_bucket);
877 sctp_remove_ifa_from_ifn(sctp_ifap);
881 SCTPDBG(SCTP_DEBUG_PCB4, "Del Addr-ifn:%d Could not find address:",
883 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr);
888 SCTP_IPI_ADDR_WUNLOCK();
890 struct sctp_laddr *wi;
892 wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
895 * Gak, what can we do? We have lost an address
896 * change can you say HOSED?
898 SCTPDBG(SCTP_DEBUG_PCB4, "Lost an address change?\n");
900 /* Oops, must decrement the count */
901 sctp_free_ifa(sctp_ifap);
904 SCTP_INCR_LADDR_COUNT();
905 memset(wi, 0, sizeof(*wi));
906 (void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
908 wi->action = SCTP_DEL_IP_ADDRESS;
911 * Should this really be a tailq? As it is we will process the
914 LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
915 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
916 (struct sctp_inpcb *)NULL,
917 (struct sctp_tcb *)NULL,
918 (struct sctp_nets *)NULL);
919 SCTP_WQ_ADDR_UNLOCK();
926 sctp_does_stcb_own_this_addr(struct sctp_tcb *stcb, struct sockaddr *to)
930 int ipv4_local_scope, ipv4_addr_legal;
933 int local_scope, site_scope, ipv6_addr_legal;
935 #if defined(__Userspace__)
938 struct sctp_vrf *vrf;
939 struct sctp_ifn *sctp_ifn;
940 struct sctp_ifa *sctp_ifa;
942 loopback_scope = stcb->asoc.scope.loopback_scope;
944 ipv4_local_scope = stcb->asoc.scope.ipv4_local_scope;
945 ipv4_addr_legal = stcb->asoc.scope.ipv4_addr_legal;
948 local_scope = stcb->asoc.scope.local_scope;
949 site_scope = stcb->asoc.scope.site_scope;
950 ipv6_addr_legal = stcb->asoc.scope.ipv6_addr_legal;
952 #if defined(__Userspace__)
953 conn_addr_legal = stcb->asoc.scope.conn_addr_legal;
956 SCTP_IPI_ADDR_RLOCK();
957 vrf = sctp_find_vrf(stcb->asoc.vrf_id);
959 /* no vrf, no addresses */
960 SCTP_IPI_ADDR_RUNLOCK();
964 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
965 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
966 if ((loopback_scope == 0) &&
967 SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
970 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
971 if (sctp_is_addr_restricted(stcb, sctp_ifa) &&
972 (!sctp_is_addr_pending(stcb, sctp_ifa))) {
973 /* We allow pending addresses, where we
974 * have sent an asconf-add to be considered
979 if (sctp_ifa->address.sa.sa_family != to->sa_family) {
982 switch (sctp_ifa->address.sa.sa_family) {
985 if (ipv4_addr_legal) {
986 struct sockaddr_in *sin, *rsin;
988 sin = &sctp_ifa->address.sin;
989 rsin = (struct sockaddr_in *)to;
990 if ((ipv4_local_scope == 0) &&
991 IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
994 #if defined(__FreeBSD__) && !defined(__Userspace__)
995 if (prison_check_ip4(stcb->sctp_ep->ip_inp.inp.inp_cred,
996 &sin->sin_addr) != 0) {
1000 if (sin->sin_addr.s_addr == rsin->sin_addr.s_addr) {
1001 SCTP_IPI_ADDR_RUNLOCK();
1009 if (ipv6_addr_legal) {
1010 struct sockaddr_in6 *sin6, *rsin6;
1011 #if defined(SCTP_EMBEDDED_V6_SCOPE) && !defined(SCTP_KAME)
1012 struct sockaddr_in6 lsa6;
1014 sin6 = &sctp_ifa->address.sin6;
1015 rsin6 = (struct sockaddr_in6 *)to;
1016 #if defined(__FreeBSD__) && !defined(__Userspace__)
1017 if (prison_check_ip6(stcb->sctp_ep->ip_inp.inp.inp_cred,
1018 &sin6->sin6_addr) != 0) {
1022 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1023 if (local_scope == 0)
1025 #if defined(SCTP_EMBEDDED_V6_SCOPE)
1026 if (sin6->sin6_scope_id == 0) {
1028 if (sa6_recoverscope(sin6) != 0)
1032 if (in6_recoverscope(&lsa6,
1037 #endif /* SCTP_KAME */
1039 #endif /* SCTP_EMBEDDED_V6_SCOPE */
1041 if ((site_scope == 0) &&
1042 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1045 if (SCTP6_ARE_ADDR_EQUAL(sin6, rsin6)) {
1046 SCTP_IPI_ADDR_RUNLOCK();
1052 #if defined(__Userspace__)
1054 if (conn_addr_legal) {
1055 struct sockaddr_conn *sconn, *rsconn;
1057 sconn = &sctp_ifa->address.sconn;
1058 rsconn = (struct sockaddr_conn *)to;
1059 if (sconn->sconn_addr == rsconn->sconn_addr) {
1060 SCTP_IPI_ADDR_RUNLOCK();
1073 struct sctp_laddr *laddr;
1075 LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) {
1076 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
1077 SCTPDBG(SCTP_DEBUG_PCB1, "ifa being deleted\n");
1080 if (sctp_is_addr_restricted(stcb, laddr->ifa) &&
1081 (!sctp_is_addr_pending(stcb, laddr->ifa))) {
1082 /* We allow pending addresses, where we
1083 * have sent an asconf-add to be considered
1088 if (laddr->ifa->address.sa.sa_family != to->sa_family) {
1091 switch (to->sa_family) {
1095 struct sockaddr_in *sin, *rsin;
1097 sin = &laddr->ifa->address.sin;
1098 rsin = (struct sockaddr_in *)to;
1099 if (sin->sin_addr.s_addr == rsin->sin_addr.s_addr) {
1100 SCTP_IPI_ADDR_RUNLOCK();
1109 struct sockaddr_in6 *sin6, *rsin6;
1111 sin6 = &laddr->ifa->address.sin6;
1112 rsin6 = (struct sockaddr_in6 *)to;
1113 if (SCTP6_ARE_ADDR_EQUAL(sin6, rsin6)) {
1114 SCTP_IPI_ADDR_RUNLOCK();
1121 #if defined(__Userspace__)
1124 struct sockaddr_conn *sconn, *rsconn;
1126 sconn = &laddr->ifa->address.sconn;
1127 rsconn = (struct sockaddr_conn *)to;
1128 if (sconn->sconn_addr == rsconn->sconn_addr) {
1129 SCTP_IPI_ADDR_RUNLOCK();
1142 SCTP_IPI_ADDR_RUNLOCK();
1147 static struct sctp_tcb *
1148 sctp_tcb_special_locate(struct sctp_inpcb **inp_p, struct sockaddr *from,
1149 struct sockaddr *to, struct sctp_nets **netp, uint32_t vrf_id)
1151 /**** ASSUMES THE CALLER holds the INP_INFO_RLOCK */
1153 * If we support the TCP model, then we must now dig through to see
1154 * if we can find our endpoint in the list of tcp ep's.
1156 uint16_t lport, rport;
1157 struct sctppcbhead *ephead;
1158 struct sctp_inpcb *inp;
1159 struct sctp_laddr *laddr;
1160 struct sctp_tcb *stcb;
1161 struct sctp_nets *net;
1166 if ((to == NULL) || (from == NULL)) {
1170 switch (to->sa_family) {
1173 if (from->sa_family == AF_INET) {
1174 lport = ((struct sockaddr_in *)to)->sin_port;
1175 rport = ((struct sockaddr_in *)from)->sin_port;
1183 if (from->sa_family == AF_INET6) {
1184 lport = ((struct sockaddr_in6 *)to)->sin6_port;
1185 rport = ((struct sockaddr_in6 *)from)->sin6_port;
1191 #if defined(__Userspace__)
1193 if (from->sa_family == AF_CONN) {
1194 lport = ((struct sockaddr_conn *)to)->sconn_port;
1195 rport = ((struct sockaddr_conn *)from)->sconn_port;
1204 ephead = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR((lport | rport), SCTP_BASE_INFO(hashtcpmark))];
1206 * Ok now for each of the guys in this bucket we must look and see:
1207 * - Does the remote port match. - Does there single association's
1208 * addresses match this address (to). If so we update p_ep to point
1209 * to this ep and return the tcb from it.
1211 LIST_FOREACH(inp, ephead, sctp_hash) {
1212 SCTP_INP_RLOCK(inp);
1213 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1214 SCTP_INP_RUNLOCK(inp);
1217 if (lport != inp->sctp_lport) {
1218 SCTP_INP_RUNLOCK(inp);
1221 #if defined(__FreeBSD__) && !defined(__Userspace__)
1222 switch (to->sa_family) {
1226 struct sockaddr_in *sin;
1228 sin = (struct sockaddr_in *)to;
1229 if (prison_check_ip4(inp->ip_inp.inp.inp_cred,
1230 &sin->sin_addr) != 0) {
1231 SCTP_INP_RUNLOCK(inp);
1240 struct sockaddr_in6 *sin6;
1242 sin6 = (struct sockaddr_in6 *)to;
1243 if (prison_check_ip6(inp->ip_inp.inp.inp_cred,
1244 &sin6->sin6_addr) != 0) {
1245 SCTP_INP_RUNLOCK(inp);
1252 SCTP_INP_RUNLOCK(inp);
1258 for (i = 0; i < inp->num_vrfs; i++) {
1259 if (inp->m_vrf_ids[i] == vrf_id) {
1265 SCTP_INP_RUNLOCK(inp);
1269 if (inp->def_vrf_id != vrf_id) {
1270 SCTP_INP_RUNLOCK(inp);
1274 /* check to see if the ep has one of the addresses */
1275 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
1276 /* We are NOT bound all, so look further */
1279 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1281 if (laddr->ifa == NULL) {
1282 SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n", __func__);
1285 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
1286 SCTPDBG(SCTP_DEBUG_PCB1, "ifa being deleted\n");
1289 if (laddr->ifa->address.sa.sa_family ==
1291 /* see if it matches */
1293 if (from->sa_family == AF_INET) {
1294 struct sockaddr_in *intf_addr, *sin;
1296 intf_addr = &laddr->ifa->address.sin;
1297 sin = (struct sockaddr_in *)to;
1298 if (sin->sin_addr.s_addr ==
1299 intf_addr->sin_addr.s_addr) {
1306 if (from->sa_family == AF_INET6) {
1307 struct sockaddr_in6 *intf_addr6;
1308 struct sockaddr_in6 *sin6;
1310 sin6 = (struct sockaddr_in6 *)
1312 intf_addr6 = &laddr->ifa->address.sin6;
1314 if (SCTP6_ARE_ADDR_EQUAL(sin6,
1321 #if defined(__Userspace__)
1322 if (from->sa_family == AF_CONN) {
1323 struct sockaddr_conn *intf_addr, *sconn;
1325 intf_addr = &laddr->ifa->address.sconn;
1326 sconn = (struct sockaddr_conn *)to;
1327 if (sconn->sconn_addr ==
1328 intf_addr->sconn_addr) {
1337 /* This endpoint does not have this address */
1338 SCTP_INP_RUNLOCK(inp);
1343 * Ok if we hit here the ep has the address, does it hold
1346 /* XXX: Why don't we TAILQ_FOREACH through sctp_asoc_list? */
1347 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1349 SCTP_INP_RUNLOCK(inp);
1352 SCTP_TCB_LOCK(stcb);
1353 if (!sctp_does_stcb_own_this_addr(stcb, to)) {
1354 SCTP_TCB_UNLOCK(stcb);
1355 SCTP_INP_RUNLOCK(inp);
1358 if (stcb->rport != rport) {
1359 /* remote port does not match. */
1360 SCTP_TCB_UNLOCK(stcb);
1361 SCTP_INP_RUNLOCK(inp);
1364 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
1365 SCTP_TCB_UNLOCK(stcb);
1366 SCTP_INP_RUNLOCK(inp);
1369 if (!sctp_does_stcb_own_this_addr(stcb, to)) {
1370 SCTP_TCB_UNLOCK(stcb);
1371 SCTP_INP_RUNLOCK(inp);
1374 /* Does this TCB have a matching address? */
1375 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1377 if (net->ro._l_addr.sa.sa_family != from->sa_family) {
1378 /* not the same family, can't be a match */
1381 switch (from->sa_family) {
1385 struct sockaddr_in *sin, *rsin;
1387 sin = (struct sockaddr_in *)&net->ro._l_addr;
1388 rsin = (struct sockaddr_in *)from;
1389 if (sin->sin_addr.s_addr ==
1390 rsin->sin_addr.s_addr) {
1395 /* Update the endpoint pointer */
1397 SCTP_INP_RUNLOCK(inp);
1406 struct sockaddr_in6 *sin6, *rsin6;
1408 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1409 rsin6 = (struct sockaddr_in6 *)from;
1410 if (SCTP6_ARE_ADDR_EQUAL(sin6,
1416 /* Update the endpoint pointer */
1418 SCTP_INP_RUNLOCK(inp);
1424 #if defined(__Userspace__)
1427 struct sockaddr_conn *sconn, *rsconn;
1429 sconn = (struct sockaddr_conn *)&net->ro._l_addr;
1430 rsconn = (struct sockaddr_conn *)from;
1431 if (sconn->sconn_addr == rsconn->sconn_addr) {
1436 /* Update the endpoint pointer */
1438 SCTP_INP_RUNLOCK(inp);
1449 SCTP_TCB_UNLOCK(stcb);
1450 SCTP_INP_RUNLOCK(inp);
1459 * 1) If I return a NULL you must decrement any INP ref cnt. 2) If I find an
1460 * stcb, both will be locked (locked_tcb and stcb) but decrement will be done
1461 * (if locked == NULL). 3) Decrement happens on return ONLY if locked ==
1466 sctp_findassociation_ep_addr(struct sctp_inpcb **inp_p, struct sockaddr *remote,
1467 struct sctp_nets **netp, struct sockaddr *local, struct sctp_tcb *locked_tcb)
1469 struct sctpasochead *head;
1470 struct sctp_inpcb *inp;
1471 struct sctp_tcb *stcb = NULL;
1472 struct sctp_nets *net;
1476 switch (remote->sa_family) {
1479 rport = (((struct sockaddr_in *)remote)->sin_port);
1484 rport = (((struct sockaddr_in6 *)remote)->sin6_port);
1487 #if defined(__Userspace__)
1489 rport = (((struct sockaddr_conn *)remote)->sconn_port);
1497 * UN-lock so we can do proper locking here this occurs when
1498 * called from load_addresses_from_init.
1500 atomic_add_int(&locked_tcb->asoc.refcnt, 1);
1501 SCTP_TCB_UNLOCK(locked_tcb);
1503 SCTP_INP_INFO_RLOCK();
1504 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1505 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1507 * Now either this guy is our listener or it's the
1508 * connector. If it is the one that issued the connect, then
1509 * it's only chance is to be the first TCB in the list. If
1510 * it is the acceptor, then do the special_lookup to hash
1511 * and find the real inp.
1513 if ((inp->sctp_socket) && SCTP_IS_LISTENING(inp)) {
1514 /* to is peer addr, from is my addr */
1516 stcb = sctp_tcb_special_locate(inp_p, remote, local,
1517 netp, inp->def_vrf_id);
1518 if ((stcb != NULL) && (locked_tcb == NULL)) {
1519 /* we have a locked tcb, lower refcount */
1520 SCTP_INP_DECR_REF(inp);
1522 if ((locked_tcb != NULL) && (locked_tcb != stcb)) {
1523 SCTP_INP_RLOCK(locked_tcb->sctp_ep);
1524 SCTP_TCB_LOCK(locked_tcb);
1525 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1526 SCTP_INP_RUNLOCK(locked_tcb->sctp_ep);
1530 * MVRF is tricky, we must look in every VRF
1535 for (i = 0; i < inp->num_vrfs; i++) {
1536 stcb = sctp_tcb_special_locate(inp_p, remote, local,
1537 netp, inp->m_vrf_ids[i]);
1538 if ((stcb != NULL) && (locked_tcb == NULL)) {
1539 /* we have a locked tcb, lower refcount */
1540 SCTP_INP_DECR_REF(inp);
1543 if ((locked_tcb != NULL) && (locked_tcb != stcb)) {
1544 SCTP_INP_RLOCK(locked_tcb->sctp_ep);
1545 SCTP_TCB_LOCK(locked_tcb);
1546 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1547 SCTP_INP_RUNLOCK(locked_tcb->sctp_ep);
1552 SCTP_INP_INFO_RUNLOCK();
1555 SCTP_INP_WLOCK(inp);
1556 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1559 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1563 SCTP_TCB_LOCK(stcb);
1565 if (stcb->rport != rport) {
1566 /* remote port does not match. */
1567 SCTP_TCB_UNLOCK(stcb);
1570 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
1571 SCTP_TCB_UNLOCK(stcb);
1574 if (local && !sctp_does_stcb_own_this_addr(stcb, local)) {
1575 SCTP_TCB_UNLOCK(stcb);
1578 /* now look at the list of remote addresses */
1579 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1581 if (net == (TAILQ_NEXT(net, sctp_next))) {
1582 panic("Corrupt net list");
1585 if (net->ro._l_addr.sa.sa_family !=
1586 remote->sa_family) {
1587 /* not the same family */
1590 switch (remote->sa_family) {
1594 struct sockaddr_in *sin, *rsin;
1596 sin = (struct sockaddr_in *)
1598 rsin = (struct sockaddr_in *)remote;
1599 if (sin->sin_addr.s_addr ==
1600 rsin->sin_addr.s_addr) {
1605 if (locked_tcb == NULL) {
1606 SCTP_INP_DECR_REF(inp);
1607 } else if (locked_tcb != stcb) {
1608 SCTP_TCB_LOCK(locked_tcb);
1611 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1614 SCTP_INP_WUNLOCK(inp);
1615 SCTP_INP_INFO_RUNLOCK();
1624 struct sockaddr_in6 *sin6, *rsin6;
1626 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1627 rsin6 = (struct sockaddr_in6 *)remote;
1628 if (SCTP6_ARE_ADDR_EQUAL(sin6,
1634 if (locked_tcb == NULL) {
1635 SCTP_INP_DECR_REF(inp);
1636 } else if (locked_tcb != stcb) {
1637 SCTP_TCB_LOCK(locked_tcb);
1640 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1642 SCTP_INP_WUNLOCK(inp);
1643 SCTP_INP_INFO_RUNLOCK();
1649 #if defined(__Userspace__)
1652 struct sockaddr_conn *sconn, *rsconn;
1654 sconn = (struct sockaddr_conn *)&net->ro._l_addr;
1655 rsconn = (struct sockaddr_conn *)remote;
1656 if (sconn->sconn_addr == rsconn->sconn_addr) {
1661 if (locked_tcb == NULL) {
1662 SCTP_INP_DECR_REF(inp);
1663 } else if (locked_tcb != stcb) {
1664 SCTP_TCB_LOCK(locked_tcb);
1667 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1669 SCTP_INP_WUNLOCK(inp);
1670 SCTP_INP_INFO_RUNLOCK();
1681 SCTP_TCB_UNLOCK(stcb);
1684 SCTP_INP_WLOCK(inp);
1685 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1688 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(rport,
1689 inp->sctp_hashmark)];
1690 LIST_FOREACH(stcb, head, sctp_tcbhash) {
1691 if (stcb->rport != rport) {
1692 /* remote port does not match */
1695 SCTP_TCB_LOCK(stcb);
1696 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
1697 SCTP_TCB_UNLOCK(stcb);
1700 if (local && !sctp_does_stcb_own_this_addr(stcb, local)) {
1701 SCTP_TCB_UNLOCK(stcb);
1704 /* now look at the list of remote addresses */
1705 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1707 if (net == (TAILQ_NEXT(net, sctp_next))) {
1708 panic("Corrupt net list");
1711 if (net->ro._l_addr.sa.sa_family !=
1712 remote->sa_family) {
1713 /* not the same family */
1716 switch (remote->sa_family) {
1720 struct sockaddr_in *sin, *rsin;
1722 sin = (struct sockaddr_in *)
1724 rsin = (struct sockaddr_in *)remote;
1725 if (sin->sin_addr.s_addr ==
1726 rsin->sin_addr.s_addr) {
1731 if (locked_tcb == NULL) {
1732 SCTP_INP_DECR_REF(inp);
1733 } else if (locked_tcb != stcb) {
1734 SCTP_TCB_LOCK(locked_tcb);
1737 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1739 SCTP_INP_WUNLOCK(inp);
1740 SCTP_INP_INFO_RUNLOCK();
1749 struct sockaddr_in6 *sin6, *rsin6;
1751 sin6 = (struct sockaddr_in6 *)
1753 rsin6 = (struct sockaddr_in6 *)remote;
1754 if (SCTP6_ARE_ADDR_EQUAL(sin6,
1760 if (locked_tcb == NULL) {
1761 SCTP_INP_DECR_REF(inp);
1762 } else if (locked_tcb != stcb) {
1763 SCTP_TCB_LOCK(locked_tcb);
1766 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1768 SCTP_INP_WUNLOCK(inp);
1769 SCTP_INP_INFO_RUNLOCK();
1775 #if defined(__Userspace__)
1778 struct sockaddr_conn *sconn, *rsconn;
1780 sconn = (struct sockaddr_conn *)&net->ro._l_addr;
1781 rsconn = (struct sockaddr_conn *)remote;
1782 if (sconn->sconn_addr == rsconn->sconn_addr) {
1787 if (locked_tcb == NULL) {
1788 SCTP_INP_DECR_REF(inp);
1789 } else if (locked_tcb != stcb) {
1790 SCTP_TCB_LOCK(locked_tcb);
1793 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1795 SCTP_INP_WUNLOCK(inp);
1796 SCTP_INP_INFO_RUNLOCK();
1807 SCTP_TCB_UNLOCK(stcb);
1811 /* clean up for returning null */
1813 SCTP_TCB_LOCK(locked_tcb);
1814 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1816 SCTP_INP_WUNLOCK(inp);
1817 SCTP_INP_INFO_RUNLOCK();
1824 * Find an association for a specific endpoint using the association id given
1825 * out in the COMM_UP notification
1828 sctp_findasoc_ep_asocid_locked(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock)
1831 * Use my the assoc_id to find a endpoint
1833 struct sctpasochead *head;
1834 struct sctp_tcb *stcb;
1838 SCTP_PRINTF("TSNH ep_associd\n");
1841 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1842 SCTP_PRINTF("TSNH ep_associd0\n");
1845 id = (uint32_t)asoc_id;
1846 head = &inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(id, inp->hashasocidmark)];
1848 /* invalid id TSNH */
1849 SCTP_PRINTF("TSNH ep_associd1\n");
1852 LIST_FOREACH(stcb, head, sctp_tcbasocidhash) {
1853 if (stcb->asoc.assoc_id == id) {
1854 if (inp != stcb->sctp_ep) {
1856 * some other guy has the same id active (id
1859 SCTP_PRINTF("TSNH ep_associd2\n");
1862 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
1866 SCTP_TCB_LOCK(stcb);
1876 sctp_findassociation_ep_asocid(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock)
1878 struct sctp_tcb *stcb;
1880 SCTP_INP_RLOCK(inp);
1881 stcb = sctp_findasoc_ep_asocid_locked(inp, asoc_id, want_lock);
1882 SCTP_INP_RUNLOCK(inp);
1888 * Endpoint probe expects that the INP_INFO is locked.
1890 static struct sctp_inpcb *
1891 sctp_endpoint_probe(struct sockaddr *nam, struct sctppcbhead *head,
1892 uint16_t lport, uint32_t vrf_id)
1894 struct sctp_inpcb *inp;
1895 struct sctp_laddr *laddr;
1897 struct sockaddr_in *sin;
1900 struct sockaddr_in6 *sin6;
1901 struct sockaddr_in6 *intf_addr6;
1903 #if defined(__Userspace__)
1904 struct sockaddr_conn *sconn;
1917 #if defined(__Userspace__)
1920 switch (nam->sa_family) {
1923 sin = (struct sockaddr_in *)nam;
1928 sin6 = (struct sockaddr_in6 *)nam;
1931 #if defined(__Userspace__)
1933 sconn = (struct sockaddr_conn *)nam;
1937 /* unsupported family */
1944 LIST_FOREACH(inp, head, sctp_hash) {
1945 SCTP_INP_RLOCK(inp);
1946 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1947 SCTP_INP_RUNLOCK(inp);
1950 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) &&
1951 (inp->sctp_lport == lport)) {
1953 switch (nam->sa_family) {
1956 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1957 SCTP_IPV6_V6ONLY(inp)) {
1958 /* IPv4 on a IPv6 socket with ONLY IPv6 set */
1959 SCTP_INP_RUNLOCK(inp);
1962 #if defined(__FreeBSD__) && !defined(__Userspace__)
1963 if (prison_check_ip4(inp->ip_inp.inp.inp_cred,
1964 &sin->sin_addr) != 0) {
1965 SCTP_INP_RUNLOCK(inp);
1973 /* A V6 address and the endpoint is NOT bound V6 */
1974 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
1975 SCTP_INP_RUNLOCK(inp);
1978 #if defined(__FreeBSD__) && !defined(__Userspace__)
1979 if (prison_check_ip6(inp->ip_inp.inp.inp_cred,
1980 &sin6->sin6_addr) != 0) {
1981 SCTP_INP_RUNLOCK(inp);
1990 /* does a VRF id match? */
1993 for (i = 0; i < inp->num_vrfs; i++) {
1994 if (inp->m_vrf_ids[i] == vrf_id) {
2000 if (inp->def_vrf_id == vrf_id)
2004 SCTP_INP_RUNLOCK(inp);
2009 SCTP_INP_RUNLOCK(inp);
2011 switch (nam->sa_family) {
2014 if (sin->sin_addr.s_addr == INADDR_ANY) {
2015 /* Can't hunt for one that has no address specified */
2022 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2023 /* Can't hunt for one that has no address specified */
2028 #if defined(__Userspace__)
2030 if (sconn->sconn_addr == NULL) {
2039 * ok, not bound to all so see if we can find a EP bound to this
2042 LIST_FOREACH(inp, head, sctp_hash) {
2043 SCTP_INP_RLOCK(inp);
2044 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
2045 SCTP_INP_RUNLOCK(inp);
2048 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)) {
2049 SCTP_INP_RUNLOCK(inp);
2053 * Ok this could be a likely candidate, look at all of its
2056 if (inp->sctp_lport != lport) {
2057 SCTP_INP_RUNLOCK(inp);
2060 /* does a VRF id match? */
2063 for (i = 0; i < inp->num_vrfs; i++) {
2064 if (inp->m_vrf_ids[i] == vrf_id) {
2070 if (inp->def_vrf_id == vrf_id)
2075 SCTP_INP_RUNLOCK(inp);
2078 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2079 if (laddr->ifa == NULL) {
2080 SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n",
2084 SCTPDBG(SCTP_DEBUG_PCB1, "Ok laddr->ifa:%p is possible, ",
2085 (void *)laddr->ifa);
2086 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
2087 SCTPDBG(SCTP_DEBUG_PCB1, "Huh IFA being deleted\n");
2090 if (laddr->ifa->address.sa.sa_family == nam->sa_family) {
2091 /* possible, see if it matches */
2092 switch (nam->sa_family) {
2095 #if defined(__APPLE__) && !defined(__Userspace__)
2101 if (sin->sin_addr.s_addr ==
2102 laddr->ifa->address.sin.sin_addr.s_addr) {
2103 SCTP_INP_RUNLOCK(inp);
2110 intf_addr6 = &laddr->ifa->address.sin6;
2111 if (SCTP6_ARE_ADDR_EQUAL(sin6,
2113 SCTP_INP_RUNLOCK(inp);
2118 #if defined(__Userspace__)
2120 if (sconn->sconn_addr == laddr->ifa->address.sconn.sconn_addr) {
2121 SCTP_INP_RUNLOCK(inp);
2129 SCTP_INP_RUNLOCK(inp);
2135 static struct sctp_inpcb *
2136 sctp_isport_inuse(struct sctp_inpcb *inp, uint16_t lport, uint32_t vrf_id)
2138 struct sctppcbhead *head;
2139 struct sctp_inpcb *t_inp;
2145 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport,
2146 SCTP_BASE_INFO(hashmark))];
2147 LIST_FOREACH(t_inp, head, sctp_hash) {
2148 if (t_inp->sctp_lport != lport) {
2151 /* is it in the VRF in question */
2154 for (i = 0; i < inp->num_vrfs; i++) {
2155 if (t_inp->m_vrf_ids[i] == vrf_id) {
2161 if (t_inp->def_vrf_id == vrf_id)
2167 /* This one is in use. */
2168 /* check the v6/v4 binding issue */
2169 if ((t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
2170 SCTP_IPV6_V6ONLY(t_inp)) {
2171 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2172 /* collision in V6 space */
2175 /* inp is BOUND_V4 no conflict */
2178 } else if (t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2179 /* t_inp is bound v4 and v6, conflict always */
2182 /* t_inp is bound only V4 */
2183 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
2184 SCTP_IPV6_V6ONLY(inp)) {
2188 /* else fall through to conflict */
2197 sctp_swap_inpcb_for_listen(struct sctp_inpcb *inp)
2199 /* For 1-2-1 with port reuse */
2200 struct sctppcbhead *head;
2201 struct sctp_inpcb *tinp, *ninp;
2203 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) {
2204 /* only works with port reuse on */
2207 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) {
2210 SCTP_INP_RUNLOCK(inp);
2211 SCTP_INP_INFO_WLOCK();
2212 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(inp->sctp_lport,
2213 SCTP_BASE_INFO(hashmark))];
2214 /* Kick out all non-listeners to the TCP hash */
2215 LIST_FOREACH_SAFE(tinp, head, sctp_hash, ninp) {
2216 if (tinp->sctp_lport != inp->sctp_lport) {
2219 if (tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
2222 if (tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
2225 if (SCTP_IS_LISTENING(tinp)) {
2228 SCTP_INP_WLOCK(tinp);
2229 LIST_REMOVE(tinp, sctp_hash);
2230 head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR(tinp->sctp_lport, SCTP_BASE_INFO(hashtcpmark))];
2231 tinp->sctp_flags |= SCTP_PCB_FLAGS_IN_TCPPOOL;
2232 LIST_INSERT_HEAD(head, tinp, sctp_hash);
2233 SCTP_INP_WUNLOCK(tinp);
2235 SCTP_INP_WLOCK(inp);
2236 /* Pull from where he was */
2237 LIST_REMOVE(inp, sctp_hash);
2238 inp->sctp_flags &= ~SCTP_PCB_FLAGS_IN_TCPPOOL;
2239 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(inp->sctp_lport, SCTP_BASE_INFO(hashmark))];
2240 LIST_INSERT_HEAD(head, inp, sctp_hash);
2241 SCTP_INP_WUNLOCK(inp);
2242 SCTP_INP_RLOCK(inp);
2243 SCTP_INP_INFO_WUNLOCK();
2249 sctp_pcb_findep(struct sockaddr *nam, int find_tcp_pool, int have_lock,
2253 * First we check the hash table to see if someone has this port
2254 * bound with just the port.
2256 struct sctp_inpcb *inp;
2257 struct sctppcbhead *head;
2261 struct sockaddr_in *sin;
2264 struct sockaddr_in6 *sin6;
2266 #if defined(__Userspace__)
2267 struct sockaddr_conn *sconn;
2270 switch (nam->sa_family) {
2273 sin = (struct sockaddr_in *)nam;
2274 lport = sin->sin_port;
2279 sin6 = (struct sockaddr_in6 *)nam;
2280 lport = sin6->sin6_port;
2283 #if defined(__Userspace__)
2285 sconn = (struct sockaddr_conn *)nam;
2286 lport = sconn->sconn_port;
2293 * I could cheat here and just cast to one of the types but we will
2294 * do it right. It also provides the check against an Unsupported
2297 /* Find the head of the ALLADDR chain */
2298 if (have_lock == 0) {
2299 SCTP_INP_INFO_RLOCK();
2301 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport,
2302 SCTP_BASE_INFO(hashmark))];
2303 inp = sctp_endpoint_probe(nam, head, lport, vrf_id);
2306 * If the TCP model exists it could be that the main listening
2307 * endpoint is gone but there still exists a connected socket for this
2308 * guy. If so we can return the first one that we find. This may NOT
2309 * be the correct one so the caller should be wary on the returned INP.
2310 * Currently the only caller that sets find_tcp_pool is in bindx where
2311 * we are verifying that a user CAN bind the address. He either
2312 * has bound it already, or someone else has, or its open to bind,
2313 * so this is good enough.
2315 if (inp == NULL && find_tcp_pool) {
2316 for (i = 0; i < SCTP_BASE_INFO(hashtcpmark) + 1; i++) {
2317 head = &SCTP_BASE_INFO(sctp_tcpephash)[i];
2318 inp = sctp_endpoint_probe(nam, head, lport, vrf_id);
2325 SCTP_INP_INCR_REF(inp);
2327 if (have_lock == 0) {
2328 SCTP_INP_INFO_RUNLOCK();
2335 * Find an association for an endpoint with the pointer to whom you want to
2336 * send to and the endpoint pointer. The address can be IPv4 or IPv6. We may
2337 * need to change the *to to some other struct like a mbuf...
2340 sctp_findassociation_addr_sa(struct sockaddr *from, struct sockaddr *to,
2341 struct sctp_inpcb **inp_p, struct sctp_nets **netp, int find_tcp_pool,
2344 struct sctp_inpcb *inp = NULL;
2345 struct sctp_tcb *stcb;
2347 SCTP_INP_INFO_RLOCK();
2348 if (find_tcp_pool) {
2349 if (inp_p != NULL) {
2350 stcb = sctp_tcb_special_locate(inp_p, from, to, netp,
2353 stcb = sctp_tcb_special_locate(&inp, from, to, netp,
2357 SCTP_INP_INFO_RUNLOCK();
2361 inp = sctp_pcb_findep(to, 0, 1, vrf_id);
2362 if (inp_p != NULL) {
2365 SCTP_INP_INFO_RUNLOCK();
2370 * ok, we have an endpoint, now lets find the assoc for it (if any)
2371 * we now place the source address or from in the to of the find
2372 * endpoint call. Since in reality this chain is used from the
2373 * inbound packet side.
2375 if (inp_p != NULL) {
2376 stcb = sctp_findassociation_ep_addr(inp_p, from, netp, to,
2379 stcb = sctp_findassociation_ep_addr(&inp, from, netp, to,
2387 * This routine will grub through the mbuf that is a INIT or INIT-ACK and
2388 * find all addresses that the sender has specified in any address list. Each
2389 * address will be used to lookup the TCB and see if one exits.
2391 static struct sctp_tcb *
2392 sctp_findassociation_special_addr(struct mbuf *m, int offset,
2393 struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp,
2394 struct sockaddr *dst)
2396 struct sctp_paramhdr *phdr, param_buf;
2397 #if defined(INET) || defined(INET6)
2398 struct sctp_tcb *stcb;
2403 struct sockaddr_in sin4;
2406 struct sockaddr_in6 sin6;
2410 memset(&sin4, 0, sizeof(sin4));
2412 sin4.sin_len = sizeof(sin4);
2414 sin4.sin_family = AF_INET;
2415 sin4.sin_port = sh->src_port;
2418 memset(&sin6, 0, sizeof(sin6));
2419 #ifdef HAVE_SIN6_LEN
2420 sin6.sin6_len = sizeof(sin6);
2422 sin6.sin6_family = AF_INET6;
2423 sin6.sin6_port = sh->src_port;
2426 offset += sizeof(struct sctp_init_chunk);
2428 phdr = sctp_get_next_param(m, offset, ¶m_buf, sizeof(param_buf));
2429 while (phdr != NULL) {
2430 /* now we must see if we want the parameter */
2431 #if defined(INET) || defined(INET6)
2432 ptype = ntohs(phdr->param_type);
2434 plen = ntohs(phdr->param_length);
2439 if (ptype == SCTP_IPV4_ADDRESS &&
2440 plen == sizeof(struct sctp_ipv4addr_param)) {
2441 /* Get the rest of the address */
2442 struct sctp_ipv4addr_param ip4_param, *p4;
2444 phdr = sctp_get_next_param(m, offset,
2445 (struct sctp_paramhdr *)&ip4_param, sizeof(ip4_param));
2449 p4 = (struct sctp_ipv4addr_param *)phdr;
2450 memcpy(&sin4.sin_addr, &p4->addr, sizeof(p4->addr));
2452 stcb = sctp_findassociation_ep_addr(inp_p,
2453 (struct sockaddr *)&sin4, netp, dst, NULL);
2460 if (ptype == SCTP_IPV6_ADDRESS &&
2461 plen == sizeof(struct sctp_ipv6addr_param)) {
2462 /* Get the rest of the address */
2463 struct sctp_ipv6addr_param ip6_param, *p6;
2465 phdr = sctp_get_next_param(m, offset,
2466 (struct sctp_paramhdr *)&ip6_param, sizeof(ip6_param));
2470 p6 = (struct sctp_ipv6addr_param *)phdr;
2471 memcpy(&sin6.sin6_addr, &p6->addr, sizeof(p6->addr));
2473 stcb = sctp_findassociation_ep_addr(inp_p,
2474 (struct sockaddr *)&sin6, netp, dst, NULL);
2480 offset += SCTP_SIZE32(plen);
2481 phdr = sctp_get_next_param(m, offset, ¶m_buf,
2487 static struct sctp_tcb *
2488 sctp_findassoc_by_vtag(struct sockaddr *from, struct sockaddr *to, uint32_t vtag,
2489 struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint16_t rport,
2490 uint16_t lport, int skip_src_check, uint32_t vrf_id, uint32_t remote_tag)
2493 * Use my vtag to hash. If we find it we then verify the source addr
2494 * is in the assoc. If all goes well we save a bit on rec of a
2497 struct sctpasochead *head;
2498 struct sctp_nets *net;
2499 struct sctp_tcb *stcb;
2504 SCTP_INP_INFO_RLOCK();
2505 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(vtag,
2506 SCTP_BASE_INFO(hashasocmark))];
2507 LIST_FOREACH(stcb, head, sctp_asocs) {
2508 SCTP_INP_RLOCK(stcb->sctp_ep);
2509 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
2510 SCTP_INP_RUNLOCK(stcb->sctp_ep);
2514 for (i = 0; i < stcb->sctp_ep->num_vrfs; i++) {
2515 if (stcb->sctp_ep->m_vrf_ids[i] == vrf_id) {
2519 if (i == stcb->sctp_ep->num_vrfs) {
2520 SCTP_INP_RUNLOCK(inp);
2524 if (stcb->sctp_ep->def_vrf_id != vrf_id) {
2525 SCTP_INP_RUNLOCK(stcb->sctp_ep);
2529 SCTP_TCB_LOCK(stcb);
2530 SCTP_INP_RUNLOCK(stcb->sctp_ep);
2531 if (stcb->asoc.my_vtag == vtag) {
2533 if (stcb->rport != rport) {
2534 SCTP_TCB_UNLOCK(stcb);
2537 if (stcb->sctp_ep->sctp_lport != lport) {
2538 SCTP_TCB_UNLOCK(stcb);
2541 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
2542 SCTP_TCB_UNLOCK(stcb);
2545 /* RRS:Need toaddr check here */
2546 if (sctp_does_stcb_own_this_addr(stcb, to) == 0) {
2547 /* Endpoint does not own this address */
2548 SCTP_TCB_UNLOCK(stcb);
2552 /* If we have both vtags that's all we match on */
2553 if (stcb->asoc.peer_vtag == remote_tag) {
2554 /* If both tags match we consider it conclusive
2555 * and check NO source/destination addresses
2560 if (skip_src_check) {
2563 *netp = sctp_findnet(stcb, from);
2565 *netp = NULL; /* unknown */
2568 *inp_p = stcb->sctp_ep;
2569 SCTP_INP_INFO_RUNLOCK();
2572 net = sctp_findnet(stcb, from);
2576 SCTP_STAT_INCR(sctps_vtagexpress);
2577 *inp_p = stcb->sctp_ep;
2578 SCTP_INP_INFO_RUNLOCK();
2582 * not him, this should only happen in rare
2583 * cases so I peg it.
2585 SCTP_STAT_INCR(sctps_vtagbogus);
2588 SCTP_TCB_UNLOCK(stcb);
2590 SCTP_INP_INFO_RUNLOCK();
2596 * Find an association with the pointer to the inbound IP packet. This can be
2597 * a IPv4 or IPv6 packet.
2600 sctp_findassociation_addr(struct mbuf *m, int offset,
2601 struct sockaddr *src, struct sockaddr *dst,
2602 struct sctphdr *sh, struct sctp_chunkhdr *ch,
2603 struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id)
2605 struct sctp_tcb *stcb;
2606 struct sctp_inpcb *inp;
2609 /* we only go down this path if vtag is non-zero */
2610 stcb = sctp_findassoc_by_vtag(src, dst, ntohl(sh->v_tag),
2611 inp_p, netp, sh->src_port, sh->dest_port, 0, vrf_id, 0);
2618 stcb = sctp_findassociation_addr_sa(src, dst, inp_p, netp,
2622 stcb = sctp_findassociation_addr_sa(src, dst, &inp, netp,
2625 SCTPDBG(SCTP_DEBUG_PCB1, "stcb:%p inp:%p\n", (void *)stcb, (void *)inp);
2626 if (stcb == NULL && inp) {
2627 /* Found a EP but not this address */
2628 if ((ch->chunk_type == SCTP_INITIATION) ||
2629 (ch->chunk_type == SCTP_INITIATION_ACK)) {
2631 * special hook, we do NOT return linp or an
2632 * association that is linked to an existing
2633 * association that is under the TCP pool (i.e. no
2634 * listener exists). The endpoint finding routine
2635 * will always find a listener before examining the
2638 if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
2644 stcb = sctp_findassociation_special_addr(m,
2645 offset, sh, &inp, netp, dst);
2646 if (inp_p != NULL) {
2651 SCTPDBG(SCTP_DEBUG_PCB1, "stcb is %p\n", (void *)stcb);
2656 * lookup an association by an ASCONF lookup address.
2657 * if the lookup address is 0.0.0.0 or ::0, use the vtag to do the lookup
2660 sctp_findassociation_ep_asconf(struct mbuf *m, int offset,
2661 struct sockaddr *dst, struct sctphdr *sh,
2662 struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id)
2664 struct sctp_tcb *stcb;
2665 union sctp_sockstore remote_store;
2666 struct sctp_paramhdr param_buf, *phdr;
2668 int zero_address = 0;
2670 struct sockaddr_in *sin;
2673 struct sockaddr_in6 *sin6;
2676 memset(&remote_store, 0, sizeof(remote_store));
2677 phdr = sctp_get_next_param(m, offset + sizeof(struct sctp_asconf_chunk),
2678 ¶m_buf, sizeof(struct sctp_paramhdr));
2680 SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf lookup addr\n",
2684 ptype = (int)((uint32_t) ntohs(phdr->param_type));
2685 /* get the correlation address */
2688 case SCTP_IPV6_ADDRESS:
2690 /* ipv6 address param */
2691 struct sctp_ipv6addr_param *p6, p6_buf;
2693 if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv6addr_param)) {
2696 p6 = (struct sctp_ipv6addr_param *)sctp_get_next_param(m,
2697 offset + sizeof(struct sctp_asconf_chunk),
2698 &p6_buf.ph, sizeof(p6_buf));
2700 SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v6 lookup addr\n",
2704 sin6 = &remote_store.sin6;
2705 sin6->sin6_family = AF_INET6;
2706 #ifdef HAVE_SIN6_LEN
2707 sin6->sin6_len = sizeof(*sin6);
2709 sin6->sin6_port = sh->src_port;
2710 memcpy(&sin6->sin6_addr, &p6->addr, sizeof(struct in6_addr));
2711 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
2717 case SCTP_IPV4_ADDRESS:
2719 /* ipv4 address param */
2720 struct sctp_ipv4addr_param *p4, p4_buf;
2722 if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv4addr_param)) {
2725 p4 = (struct sctp_ipv4addr_param *)sctp_get_next_param(m,
2726 offset + sizeof(struct sctp_asconf_chunk),
2727 &p4_buf.ph, sizeof(p4_buf));
2729 SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v4 lookup addr\n",
2733 sin = &remote_store.sin;
2734 sin->sin_family = AF_INET;
2736 sin->sin_len = sizeof(*sin);
2738 sin->sin_port = sh->src_port;
2739 memcpy(&sin->sin_addr, &p4->addr, sizeof(struct in_addr));
2740 if (sin->sin_addr.s_addr == INADDR_ANY)
2746 /* invalid address param type */
2751 stcb = sctp_findassoc_by_vtag(NULL, dst, ntohl(sh->v_tag), inp_p,
2752 netp, sh->src_port, sh->dest_port, 1, vrf_id, 0);
2754 SCTP_INP_DECR_REF(*inp_p);
2757 stcb = sctp_findassociation_ep_addr(inp_p,
2758 &remote_store.sa, netp,
2766 * allocate a sctp_inpcb and setup a temporary binding to a port/all
2767 * addresses. This way if we don't get a bind we by default pick a ephemeral
2768 * port with all addresses bound.
2771 sctp_inpcb_alloc(struct socket *so, uint32_t vrf_id)
2774 * we get called when a new endpoint starts up. We need to allocate
2775 * the sctp_inpcb structure from the zone and init it. Mark it as
2776 * unbound and find a port that we can use as an ephemeral with
2777 * INADDR_ANY. If the user binds later no problem we can then add in
2778 * the specific addresses. And setup the default parameters for the
2782 struct sctp_inpcb *inp;
2784 struct timeval time;
2785 sctp_sharedkey_t *null_key;
2789 SCTP_INP_INFO_WLOCK();
2790 inp = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_ep), struct sctp_inpcb);
2792 SCTP_PRINTF("Out of SCTP-INPCB structures - no resources\n");
2793 SCTP_INP_INFO_WUNLOCK();
2794 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
2798 memset(inp, 0, sizeof(*inp));
2800 /* bump generations */
2801 #if defined(__APPLE__) && !defined(__Userspace__)
2802 inp->ip_inp.inp.inp_state = INPCB_STATE_INUSE;
2804 /* setup socket pointers */
2805 inp->sctp_socket = so;
2806 inp->ip_inp.inp.inp_socket = so;
2807 #if defined(__FreeBSD__) && !defined(__Userspace__)
2808 inp->ip_inp.inp.inp_cred = crhold(so->so_cred);
2811 #if !defined(__Userspace__) && !defined(_WIN32)
2812 if (INP_SOCKAF(so) == AF_INET6) {
2813 if (MODULE_GLOBAL(ip6_auto_flowlabel)) {
2814 inp->ip_inp.inp.inp_flags |= IN6P_AUTOFLOWLABEL;
2816 if (MODULE_GLOBAL(ip6_v6only)) {
2817 inp->ip_inp.inp.inp_flags |= IN6P_IPV6_V6ONLY;
2822 inp->sctp_associd_counter = 1;
2823 inp->partial_delivery_point = SCTP_SB_LIMIT_RCV(so) >> SCTP_PARTIAL_DELIVERY_SHIFT;
2824 inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
2826 inp->sctp_cmt_on_off = SCTP_BASE_SYSCTL(sctp_cmt_on_off);
2827 inp->ecn_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_ecn_enable);
2828 inp->prsctp_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_pr_enable);
2829 inp->auth_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_auth_enable);
2830 inp->asconf_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_asconf_enable);
2831 inp->reconfig_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_reconfig_enable);
2832 inp->nrsack_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_nrsack_enable);
2833 inp->pktdrop_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_pktdrop_enable);
2834 inp->idata_supported = 0;
2836 #if defined(__FreeBSD__) && !defined(__Userspace__)
2837 inp->fibnum = so->so_fibnum;
2841 #if defined(__Userspace__)
2842 inp->ulp_info = NULL;
2843 inp->recv_callback = NULL;
2844 inp->send_callback = NULL;
2845 inp->send_sb_threshold = 0;
2847 /* init the small hash table we use to track asocid <-> tcb */
2848 inp->sctp_asocidhash = SCTP_HASH_INIT(SCTP_STACK_VTAG_HASH_SIZE, &inp->hashasocidmark);
2849 if (inp->sctp_asocidhash == NULL) {
2850 #if defined(__FreeBSD__) && !defined(__Userspace__)
2851 crfree(inp->ip_inp.inp.inp_cred);
2853 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2854 SCTP_INP_INFO_WUNLOCK();
2857 SCTP_INCR_EP_COUNT();
2858 inp->ip_inp.inp.inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
2859 SCTP_INP_INFO_WUNLOCK();
2861 so->so_pcb = (caddr_t)inp;
2863 if (SCTP_SO_TYPE(so) == SOCK_SEQPACKET) {
2864 /* UDP style socket */
2865 inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
2866 SCTP_PCB_FLAGS_UNBOUND);
2867 /* Be sure it is NON-BLOCKING IO for UDP */
2868 /* SCTP_SET_SO_NBIO(so); */
2869 } else if (SCTP_SO_TYPE(so) == SOCK_STREAM) {
2870 /* TCP style socket */
2871 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2872 SCTP_PCB_FLAGS_UNBOUND);
2873 /* Be sure we have blocking IO by default */
2875 SCTP_CLEAR_SO_NBIO(so);
2879 * unsupported socket type (RAW, etc)- in case we missed it
2882 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EOPNOTSUPP);
2884 #if defined(__FreeBSD__) && !defined(__Userspace__)
2885 crfree(inp->ip_inp.inp.inp_cred);
2887 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2888 return (EOPNOTSUPP);
2890 if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_1) {
2891 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2892 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2893 } else if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_2) {
2894 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2895 sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2896 } else if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_0) {
2897 sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2898 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2900 inp->sctp_tcbhash = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_pcbtblsize),
2901 &inp->sctp_hashmark);
2902 if (inp->sctp_tcbhash == NULL) {
2903 SCTP_PRINTF("Out of SCTP-INPCB->hashinit - no resources\n");
2904 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
2906 #if defined(__FreeBSD__) && !defined(__Userspace__)
2907 crfree(inp->ip_inp.inp.inp_cred);
2909 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2913 inp->vrf_size = SCTP_DEFAULT_VRF_SIZE;
2914 SCTP_MALLOC(inp->m_vrf_ids, uint32_t *,
2915 (sizeof(uint32_t) * inp->vrf_size), SCTP_M_MVRF);
2916 if (inp->m_vrf_ids == NULL) {
2917 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
2919 SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark);
2920 #if defined(__FreeBSD__) && !defined(__Userspace__)
2921 crfree(inp->ip_inp.inp.inp_cred);
2923 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2926 inp->m_vrf_ids[0] = vrf_id;
2929 inp->def_vrf_id = vrf_id;
2931 #if defined(__APPLE__) && !defined(__Userspace__)
2932 #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
2933 inp->ip_inp.inp.inpcb_mtx = lck_mtx_alloc_init(SCTP_BASE_INFO(sctbinfo).mtx_grp, SCTP_BASE_INFO(sctbinfo).mtx_attr);
2934 if (inp->ip_inp.inp.inpcb_mtx == NULL) {
2935 SCTP_PRINTF("in_pcballoc: can't alloc mutex! so=%p\n", (void *)so);
2937 SCTP_FREE(inp->m_vrf_ids, SCTP_M_MVRF);
2939 SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark);
2941 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2942 SCTP_UNLOCK_EXC(SCTP_BASE_INFO(sctbinfo).ipi_lock);
2943 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
2946 #elif defined(APPLE_LION) || defined(APPLE_MOUNTAINLION)
2947 lck_mtx_init(&inp->ip_inp.inp.inpcb_mtx, SCTP_BASE_INFO(sctbinfo).mtx_grp, SCTP_BASE_INFO(sctbinfo).mtx_attr);
2949 lck_mtx_init(&inp->ip_inp.inp.inpcb_mtx, SCTP_BASE_INFO(sctbinfo).ipi_lock_grp, SCTP_BASE_INFO(sctbinfo).ipi_lock_attr);
2952 SCTP_INP_INFO_WLOCK();
2953 SCTP_INP_LOCK_INIT(inp);
2954 #if defined(__FreeBSD__) && !defined(__Userspace__)
2955 INP_LOCK_INIT(&inp->ip_inp.inp, "inp", "sctpinp");
2957 SCTP_INP_READ_INIT(inp);
2958 SCTP_ASOC_CREATE_LOCK_INIT(inp);
2959 /* lock the new ep */
2960 SCTP_INP_WLOCK(inp);
2962 /* add it to the info area */
2963 LIST_INSERT_HEAD(&SCTP_BASE_INFO(listhead), inp, sctp_list);
2964 #if defined(__APPLE__) && !defined(__Userspace__)
2965 inp->ip_inp.inp.inp_pcbinfo = &SCTP_BASE_INFO(sctbinfo);
2966 #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) || defined(APPLE_LION) || defined(APPLE_MOUNTAINLION)
2967 LIST_INSERT_HEAD(SCTP_BASE_INFO(sctbinfo).listhead, &inp->ip_inp.inp, inp_list);
2969 LIST_INSERT_HEAD(SCTP_BASE_INFO(sctbinfo).ipi_listhead, &inp->ip_inp.inp, inp_list);
2972 SCTP_INP_INFO_WUNLOCK();
2974 TAILQ_INIT(&inp->read_queue);
2975 LIST_INIT(&inp->sctp_addr_list);
2977 LIST_INIT(&inp->sctp_asoc_list);
2979 #ifdef SCTP_TRACK_FREED_ASOCS
2981 LIST_INIT(&inp->sctp_asoc_free_list);
2983 /* Init the timer structure for signature change */
2984 SCTP_OS_TIMER_INIT(&inp->sctp_ep.signature_change.timer);
2985 inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NEWCOOKIE;
2987 /* now init the actual endpoint default data */
2990 /* setup the base timeout information */
2991 m->sctp_timeoutticks[SCTP_TIMER_SEND] = sctp_secs_to_ticks(SCTP_SEND_SEC); /* needed ? */
2992 m->sctp_timeoutticks[SCTP_TIMER_INIT] = sctp_secs_to_ticks(SCTP_INIT_SEC); /* needed ? */
2993 m->sctp_timeoutticks[SCTP_TIMER_RECV] = sctp_msecs_to_ticks(SCTP_BASE_SYSCTL(sctp_delayed_sack_time_default));
2994 m->sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = sctp_msecs_to_ticks(SCTP_BASE_SYSCTL(sctp_heartbeat_interval_default));
2995 m->sctp_timeoutticks[SCTP_TIMER_PMTU] = sctp_secs_to_ticks(SCTP_BASE_SYSCTL(sctp_pmtu_raise_time_default));
2996 m->sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] = sctp_secs_to_ticks(SCTP_BASE_SYSCTL(sctp_shutdown_guard_time_default));
2997 m->sctp_timeoutticks[SCTP_TIMER_SIGNATURE] = sctp_secs_to_ticks(SCTP_BASE_SYSCTL(sctp_secret_lifetime_default));
2998 /* all max/min max are in ms */
2999 m->sctp_maxrto = SCTP_BASE_SYSCTL(sctp_rto_max_default);
3000 m->sctp_minrto = SCTP_BASE_SYSCTL(sctp_rto_min_default);
3001 m->initial_rto = SCTP_BASE_SYSCTL(sctp_rto_initial_default);
3002 m->initial_init_rto_max = SCTP_BASE_SYSCTL(sctp_init_rto_max_default);
3003 m->sctp_sack_freq = SCTP_BASE_SYSCTL(sctp_sack_freq_default);
3004 m->max_init_times = SCTP_BASE_SYSCTL(sctp_init_rtx_max_default);
3005 m->max_send_times = SCTP_BASE_SYSCTL(sctp_assoc_rtx_max_default);
3006 m->def_net_failure = SCTP_BASE_SYSCTL(sctp_path_rtx_max_default);
3007 m->def_net_pf_threshold = SCTP_BASE_SYSCTL(sctp_path_pf_threshold);
3008 m->sctp_sws_sender = SCTP_SWS_SENDER_DEF;
3009 m->sctp_sws_receiver = SCTP_SWS_RECEIVER_DEF;
3010 m->max_burst = SCTP_BASE_SYSCTL(sctp_max_burst_default);
3011 m->fr_max_burst = SCTP_BASE_SYSCTL(sctp_fr_max_burst_default);
3013 m->sctp_default_cc_module = SCTP_BASE_SYSCTL(sctp_default_cc_module);
3014 m->sctp_default_ss_module = SCTP_BASE_SYSCTL(sctp_default_ss_module);
3015 m->max_open_streams_intome = SCTP_BASE_SYSCTL(sctp_nr_incoming_streams_default);
3016 /* number of streams to pre-open on a association */
3017 m->pre_open_stream_count = SCTP_BASE_SYSCTL(sctp_nr_outgoing_streams_default);
3020 /* Add adaptation cookie */
3021 m->adaptation_layer_indicator = 0;
3022 m->adaptation_layer_indicator_provided = 0;
3024 /* seed random number generator */
3025 m->random_counter = 1;
3026 m->store_at = SCTP_SIGNATURE_SIZE;
3027 SCTP_READ_RANDOM(m->random_numbers, sizeof(m->random_numbers));
3028 sctp_fill_random_store(m);
3030 /* Minimum cookie size */
3031 m->size_of_a_cookie = (sizeof(struct sctp_init_msg) * 2) +
3032 sizeof(struct sctp_state_cookie);
3033 m->size_of_a_cookie += SCTP_SIGNATURE_SIZE;
3035 /* Setup the initial secret */
3036 (void)SCTP_GETTIME_TIMEVAL(&time);
3037 m->time_of_secret_change = time.tv_sec;
3039 for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
3040 m->secret_key[0][i] = sctp_select_initial_TSN(m);
3042 sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL);
3044 /* How long is a cookie good for ? */
3045 m->def_cookie_life = sctp_msecs_to_ticks(SCTP_BASE_SYSCTL(sctp_valid_cookie_life_default));
3047 * Initialize authentication parameters
3049 m->local_hmacs = sctp_default_supported_hmaclist();
3050 m->local_auth_chunks = sctp_alloc_chunklist();
3051 if (inp->asconf_supported) {
3052 sctp_auth_add_chunk(SCTP_ASCONF, m->local_auth_chunks);
3053 sctp_auth_add_chunk(SCTP_ASCONF_ACK, m->local_auth_chunks);
3055 m->default_dscp = 0;
3057 m->default_flowlabel = 0;
3059 m->port = 0; /* encapsulation disabled by default */
3060 LIST_INIT(&m->shared_keys);
3061 /* add default NULL key as key id 0 */
3062 null_key = sctp_alloc_sharedkey();
3063 sctp_insert_sharedkey(&m->shared_keys, null_key);
3064 SCTP_INP_WUNLOCK(inp);
3065 #ifdef SCTP_LOG_CLOSING
3066 sctp_log_closing(inp, NULL, 12);
3073 sctp_move_pcb_and_assoc(struct sctp_inpcb *old_inp, struct sctp_inpcb *new_inp,
3074 struct sctp_tcb *stcb)
3076 struct sctp_nets *net;
3077 uint16_t lport, rport;
3078 struct sctppcbhead *head;
3079 struct sctp_laddr *laddr, *oladdr;
3081 atomic_add_int(&stcb->asoc.refcnt, 1);
3082 SCTP_TCB_UNLOCK(stcb);
3083 SCTP_INP_INFO_WLOCK();
3084 SCTP_INP_WLOCK(old_inp);
3085 SCTP_INP_WLOCK(new_inp);
3086 SCTP_TCB_LOCK(stcb);
3087 atomic_subtract_int(&stcb->asoc.refcnt, 1);
3089 new_inp->sctp_ep.time_of_secret_change =
3090 old_inp->sctp_ep.time_of_secret_change;
3091 memcpy(new_inp->sctp_ep.secret_key, old_inp->sctp_ep.secret_key,
3092 sizeof(old_inp->sctp_ep.secret_key));
3093 new_inp->sctp_ep.current_secret_number =
3094 old_inp->sctp_ep.current_secret_number;
3095 new_inp->sctp_ep.last_secret_number =
3096 old_inp->sctp_ep.last_secret_number;
3097 new_inp->sctp_ep.size_of_a_cookie = old_inp->sctp_ep.size_of_a_cookie;
3099 /* make it so new data pours into the new socket */
3100 stcb->sctp_socket = new_inp->sctp_socket;
3101 stcb->sctp_ep = new_inp;
3103 /* Copy the port across */
3104 lport = new_inp->sctp_lport = old_inp->sctp_lport;
3105 rport = stcb->rport;
3106 /* Pull the tcb from the old association */
3107 LIST_REMOVE(stcb, sctp_tcbhash);
3108 LIST_REMOVE(stcb, sctp_tcblist);
3109 if (stcb->asoc.in_asocid_hash) {
3110 LIST_REMOVE(stcb, sctp_tcbasocidhash);
3112 /* Now insert the new_inp into the TCP connected hash */
3113 head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR((lport | rport), SCTP_BASE_INFO(hashtcpmark))];
3115 LIST_INSERT_HEAD(head, new_inp, sctp_hash);
3116 /* Its safe to access */
3117 new_inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
3119 /* Now move the tcb into the endpoint list */
3120 LIST_INSERT_HEAD(&new_inp->sctp_asoc_list, stcb, sctp_tcblist);
3122 * Question, do we even need to worry about the ep-hash since we
3123 * only have one connection? Probably not :> so lets get rid of it
3124 * and not suck up any kernel memory in that.
3126 if (stcb->asoc.in_asocid_hash) {
3127 struct sctpasochead *lhd;
3128 lhd = &new_inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(stcb->asoc.assoc_id,
3129 new_inp->hashasocidmark)];
3130 LIST_INSERT_HEAD(lhd, stcb, sctp_tcbasocidhash);
3132 /* Ok. Let's restart timer. */
3133 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3134 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, new_inp,
3138 SCTP_INP_INFO_WUNLOCK();
3139 if (new_inp->sctp_tcbhash != NULL) {
3140 SCTP_HASH_FREE(new_inp->sctp_tcbhash, new_inp->sctp_hashmark);
3141 new_inp->sctp_tcbhash = NULL;
3143 if ((new_inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
3144 /* Subset bound, so copy in the laddr list from the old_inp */
3145 LIST_FOREACH(oladdr, &old_inp->sctp_addr_list, sctp_nxt_addr) {
3146 laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
3147 if (laddr == NULL) {
3149 * Gak, what can we do? This assoc is really
3150 * HOSED. We probably should send an abort
3153 SCTPDBG(SCTP_DEBUG_PCB1, "Association hosed in TCP model, out of laddr memory\n");
3156 SCTP_INCR_LADDR_COUNT();
3157 memset(laddr, 0, sizeof(*laddr));
3158 (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time);
3159 laddr->ifa = oladdr->ifa;
3160 atomic_add_int(&laddr->ifa->refcount, 1);
3161 LIST_INSERT_HEAD(&new_inp->sctp_addr_list, laddr,
3163 new_inp->laddr_count++;
3164 if (oladdr == stcb->asoc.last_used_address) {
3165 stcb->asoc.last_used_address = laddr;
3169 /* Now any running timers need to be adjusted. */
3170 if (stcb->asoc.dack_timer.ep == old_inp) {
3171 SCTP_INP_DECR_REF(old_inp);
3172 stcb->asoc.dack_timer.ep = new_inp;
3173 SCTP_INP_INCR_REF(new_inp);
3175 if (stcb->asoc.asconf_timer.ep == old_inp) {
3176 SCTP_INP_DECR_REF(old_inp);
3177 stcb->asoc.asconf_timer.ep = new_inp;
3178 SCTP_INP_INCR_REF(new_inp);
3180 if (stcb->asoc.strreset_timer.ep == old_inp) {
3181 SCTP_INP_DECR_REF(old_inp);
3182 stcb->asoc.strreset_timer.ep = new_inp;
3183 SCTP_INP_INCR_REF(new_inp);
3185 if (stcb->asoc.shut_guard_timer.ep == old_inp) {
3186 SCTP_INP_DECR_REF(old_inp);
3187 stcb->asoc.shut_guard_timer.ep = new_inp;
3188 SCTP_INP_INCR_REF(new_inp);
3190 if (stcb->asoc.autoclose_timer.ep == old_inp) {
3191 SCTP_INP_DECR_REF(old_inp);
3192 stcb->asoc.autoclose_timer.ep = new_inp;
3193 SCTP_INP_INCR_REF(new_inp);
3195 if (stcb->asoc.delete_prim_timer.ep == old_inp) {
3196 SCTP_INP_DECR_REF(old_inp);
3197 stcb->asoc.delete_prim_timer.ep = new_inp;
3198 SCTP_INP_INCR_REF(new_inp);
3200 /* now what about the nets? */
3201 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3202 if (net->pmtu_timer.ep == old_inp) {
3203 SCTP_INP_DECR_REF(old_inp);
3204 net->pmtu_timer.ep = new_inp;
3205 SCTP_INP_INCR_REF(new_inp);
3207 if (net->hb_timer.ep == old_inp) {
3208 SCTP_INP_DECR_REF(old_inp);
3209 net->hb_timer.ep = new_inp;
3210 SCTP_INP_INCR_REF(new_inp);
3212 if (net->rxt_timer.ep == old_inp) {
3213 SCTP_INP_DECR_REF(old_inp);
3214 net->rxt_timer.ep = new_inp;
3215 SCTP_INP_INCR_REF(new_inp);
3218 SCTP_INP_WUNLOCK(new_inp);
3219 SCTP_INP_WUNLOCK(old_inp);
3223 * insert an laddr entry with the given ifa for the desired list
3226 sctp_insert_laddr(struct sctpladdr *list, struct sctp_ifa *ifa, uint32_t act)
3228 struct sctp_laddr *laddr;
3230 laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
3231 if (laddr == NULL) {
3232 /* out of memory? */
3233 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3236 SCTP_INCR_LADDR_COUNT();
3237 memset(laddr, 0, sizeof(*laddr));
3238 (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time);
3240 laddr->action = act;
3241 atomic_add_int(&ifa->refcount, 1);
3243 LIST_INSERT_HEAD(list, laddr, sctp_nxt_addr);
3249 * Remove an laddr entry from the local address list (on an assoc)
3252 sctp_remove_laddr(struct sctp_laddr *laddr)
3255 /* remove from the list */
3256 LIST_REMOVE(laddr, sctp_nxt_addr);
3257 sctp_free_ifa(laddr->ifa);
3258 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), laddr);
3259 SCTP_DECR_LADDR_COUNT();
3262 #if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Userspace__))
3264 * Don't know why, but without this there is an unknown reference when
3265 * compiling NetBSD... hmm
3267 extern void in6_sin6_2_sin(struct sockaddr_in *, struct sockaddr_in6 *sin6);
3271 /* sctp_ifap is used to bypass normal local address validation checks */
3273 #if defined(__FreeBSD__) && !defined(__Userspace__)
3274 sctp_inpcb_bind(struct socket *so, struct sockaddr *addr,
3275 struct sctp_ifa *sctp_ifap, struct thread *p)
3276 #elif defined(_WIN32) && !defined(__Userspace__)
3277 sctp_inpcb_bind(struct socket *so, struct sockaddr *addr,
3278 struct sctp_ifa *sctp_ifap, PKTHREAD p)
3280 sctp_inpcb_bind(struct socket *so, struct sockaddr *addr,
3281 struct sctp_ifa *sctp_ifap, struct proc *p)
3284 /* bind a ep to a socket address */
3285 struct sctppcbhead *head;
3286 struct sctp_inpcb *inp, *inp_tmp;
3287 #if (defined(__FreeBSD__) || defined(__APPLE__)) && !defined(__Userspace__)
3288 struct inpcb *ip_inp;
3290 int port_reuse_active = 0;
3301 inp = (struct sctp_inpcb *)so->so_pcb;
3302 #if (defined(__FreeBSD__) || defined(__APPLE__)) && !defined(__Userspace__)
3303 ip_inp = (struct inpcb *)so->so_pcb;
3307 SCTPDBG(SCTP_DEBUG_PCB1, "Bind called port: %d\n",
3308 ntohs(((struct sockaddr_in *)addr)->sin_port));
3309 SCTPDBG(SCTP_DEBUG_PCB1, "Addr: ");
3310 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr);
3313 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
3314 /* already did a bind, subsequent binds NOT allowed ! */
3315 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3318 #if defined(__FreeBSD__) && !defined(__Userspace__)
3321 panic("null proc/thread");
3325 switch (addr->sa_family) {
3329 struct sockaddr_in *sin;
3331 /* IPV6_V6ONLY socket? */
3332 if (SCTP_IPV6_V6ONLY(inp)) {
3333 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3337 if (addr->sa_len != sizeof(*sin)) {
3338 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3343 sin = (struct sockaddr_in *)addr;
3344 lport = sin->sin_port;
3345 #if defined(__FreeBSD__) && !defined(__Userspace__)
3347 * For LOOPBACK the prison_local_ip4() call will transmute the ip address
3348 * to the proper value.
3350 if (p && (error = prison_local_ip4(p->td_ucred, &sin->sin_addr)) != 0) {
3351 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3355 if (sin->sin_addr.s_addr != INADDR_ANY) {
3364 /* Only for pure IPv6 Address. (No IPv4 Mapped!) */
3365 struct sockaddr_in6 *sin6;
3367 sin6 = (struct sockaddr_in6 *)addr;
3370 if (addr->sa_len != sizeof(*sin6)) {
3371 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3375 lport = sin6->sin6_port;
3376 #if defined(__FreeBSD__) && !defined(__Userspace__)
3378 * For LOOPBACK the prison_local_ip6() call will transmute the ipv6 address
3379 * to the proper value.
3381 if (p && (error = prison_local_ip6(p->td_ucred, &sin6->sin6_addr,
3382 (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
3383 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3387 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3389 #ifdef SCTP_EMBEDDED_V6_SCOPE
3390 /* KAME hack: embed scopeid */
3391 #if defined(SCTP_KAME)
3392 if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
3393 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3396 #elif defined(__APPLE__) && !defined(__Userspace__)
3397 #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
3398 if (in6_embedscope(&sin6->sin6_addr, sin6, ip_inp, NULL) != 0) {
3400 if (in6_embedscope(&sin6->sin6_addr, sin6, ip_inp, NULL, NULL) != 0) {
3402 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3405 #elif defined(__FreeBSD__) && !defined(__Userspace__)
3406 error = scope6_check_id(sin6, MODULE_GLOBAL(ip6_use_defzone));
3408 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3412 if (in6_embedscope(&sin6->sin6_addr, sin6) != 0) {
3413 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3417 #endif /* SCTP_EMBEDDED_V6_SCOPE */
3419 #ifndef SCOPEDROUTING
3420 /* this must be cleared for ifa_ifwithaddr() */
3421 sin6->sin6_scope_id = 0;
3422 #endif /* SCOPEDROUTING */
3426 #if defined(__Userspace__)
3429 struct sockaddr_conn *sconn;
3432 if (addr->sa_len != sizeof(struct sockaddr_conn)) {
3433 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3437 sconn = (struct sockaddr_conn *)addr;
3438 lport = sconn->sconn_port;
3439 if (sconn->sconn_addr != NULL) {
3446 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EAFNOSUPPORT);
3447 return (EAFNOSUPPORT);
3450 SCTP_INP_INFO_WLOCK();
3451 SCTP_INP_WLOCK(inp);
3452 /* Setup a vrf_id to be the default for the non-bind-all case. */
3453 vrf_id = inp->def_vrf_id;
3455 /* increase our count due to the unlock we do */
3456 SCTP_INP_INCR_REF(inp);
3459 * Did the caller specify a port? if so we must see if an ep
3460 * already has this one bound.
3462 /* got to be root to get at low ports */
3463 #if !(defined(_WIN32) && !defined(__Userspace__))
3464 if (ntohs(lport) < IPPORT_RESERVED) {
3465 if ((p != NULL) && ((error =
3466 #if defined(__FreeBSD__) && !defined(__Userspace__)
3467 priv_check(p, PRIV_NETINET_RESERVEDPORT)
3468 #elif defined(__APPLE__) && !defined(__Userspace__)
3469 suser(p->p_ucred, &p->p_acflag)
3470 #elif defined(__Userspace__) /* must be true to use raw socket */
3476 SCTP_INP_DECR_REF(inp);
3477 SCTP_INP_WUNLOCK(inp);
3478 SCTP_INP_INFO_WUNLOCK();
3483 SCTP_INP_WUNLOCK(inp);
3486 for (i = 0; i < inp->num_vrfs; i++) {
3487 vrf_id = inp->m_vrf_ids[i];
3489 vrf_id = inp->def_vrf_id;
3491 inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id);
3492 if (inp_tmp != NULL) {
3494 * lock guy returned and lower count
3495 * note that we are not bound so
3496 * inp_tmp should NEVER be inp. And
3497 * it is this inp (inp_tmp) that gets
3498 * the reference bump, so we must
3501 SCTP_INP_DECR_REF(inp_tmp);
3503 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
3504 (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
3505 /* Ok, must be one-2-one and allowing port re-use */
3506 port_reuse_active = 1;
3507 goto continue_anyway;
3509 SCTP_INP_DECR_REF(inp);
3510 SCTP_INP_INFO_WUNLOCK();
3511 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE);
3512 return (EADDRINUSE);
3518 inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id);
3519 if (inp_tmp != NULL) {
3521 * lock guy returned and lower count note
3522 * that we are not bound so inp_tmp should
3523 * NEVER be inp. And it is this inp (inp_tmp)
3524 * that gets the reference bump, so we must
3527 SCTP_INP_DECR_REF(inp_tmp);
3529 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
3530 (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
3531 /* Ok, must be one-2-one and allowing port re-use */
3532 port_reuse_active = 1;
3533 goto continue_anyway;
3535 SCTP_INP_DECR_REF(inp);
3536 SCTP_INP_INFO_WUNLOCK();
3537 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE);
3538 return (EADDRINUSE);
3542 SCTP_INP_WLOCK(inp);
3544 /* verify that no lport is not used by a singleton */
3545 if ((port_reuse_active == 0) &&
3546 (inp_tmp = sctp_isport_inuse(inp, lport, vrf_id))) {
3547 /* Sorry someone already has this one bound */
3548 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
3549 (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
3550 port_reuse_active = 1;
3552 SCTP_INP_DECR_REF(inp);
3553 SCTP_INP_WUNLOCK(inp);
3554 SCTP_INP_INFO_WUNLOCK();
3555 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE);
3556 return (EADDRINUSE);
3561 uint16_t first, last, candidate;
3565 #if defined(_WIN32) && !defined(__Userspace__)
3569 #if defined(__Userspace__)
3570 /* TODO ensure uid is 0, etc... */
3571 #elif (defined(__FreeBSD__) || defined(__APPLE__)) && !defined(__Userspace__)
3572 if (ip_inp->inp_flags & INP_HIGHPORT) {
3573 first = MODULE_GLOBAL(ipport_hifirstauto);
3574 last = MODULE_GLOBAL(ipport_hilastauto);
3575 } else if (ip_inp->inp_flags & INP_LOWPORT) {
3577 #if defined(__FreeBSD__)
3578 priv_check(p, PRIV_NETINET_RESERVEDPORT)
3579 #elif defined(__APPLE__)
3580 suser(p->p_ucred, &p->p_acflag)
3585 SCTP_INP_DECR_REF(inp);
3586 SCTP_INP_WUNLOCK(inp);
3587 SCTP_INP_INFO_WUNLOCK();
3588 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3591 first = MODULE_GLOBAL(ipport_lowfirstauto);
3592 last = MODULE_GLOBAL(ipport_lowlastauto);
3595 first = MODULE_GLOBAL(ipport_firstauto);
3596 last = MODULE_GLOBAL(ipport_lastauto);
3597 #if (defined(__FreeBSD__) || defined(__APPLE__)) && !defined(__Userspace__)
3608 count = last - first + 1; /* number of candidates */
3609 candidate = first + sctp_select_initial_TSN(&inp->sctp_ep) % (count);
3614 for (i = 0; i < inp->num_vrfs; i++) {
3615 if (sctp_isport_inuse(inp, htons(candidate), inp->m_vrf_ids[i]) != NULL) {
3619 if (i == inp->num_vrfs) {
3623 if (sctp_isport_inuse(inp, htons(candidate), inp->def_vrf_id) == NULL) {
3629 SCTP_INP_DECR_REF(inp);
3630 SCTP_INP_WUNLOCK(inp);
3631 SCTP_INP_INFO_WUNLOCK();
3632 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE);
3633 return (EADDRINUSE);
3635 if (candidate == last)
3638 candidate = candidate + 1;
3641 lport = htons(candidate);
3643 SCTP_INP_DECR_REF(inp);
3644 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE |
3645 SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
3647 * this really should not happen. The guy did a non-blocking
3648 * bind and then did a close at the same time.
3650 SCTP_INP_WUNLOCK(inp);
3651 SCTP_INP_INFO_WUNLOCK();
3652 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3655 /* ok we look clear to give out this port, so lets setup the binding */
3657 /* binding to all addresses, so just set in the proper flags */
3658 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUNDALL;
3659 /* set the automatic addr changes from kernel flag */
3660 if (SCTP_BASE_SYSCTL(sctp_auto_asconf) == 0) {
3661 sctp_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF);
3662 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
3664 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
3665 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
3667 if (SCTP_BASE_SYSCTL(sctp_multiple_asconfs) == 0) {
3668 sctp_feature_off(inp, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS);
3670 sctp_feature_on(inp, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS);
3672 /* set the automatic mobility_base from kernel
3675 if (SCTP_BASE_SYSCTL(sctp_mobility_base) == 0) {
3676 sctp_mobility_feature_off(inp, SCTP_MOBILITY_BASE);
3677 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
3679 sctp_mobility_feature_on(inp, SCTP_MOBILITY_BASE);
3680 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
3682 /* set the automatic mobility_fasthandoff from kernel
3685 if (SCTP_BASE_SYSCTL(sctp_mobility_fasthandoff) == 0) {
3686 sctp_mobility_feature_off(inp, SCTP_MOBILITY_FASTHANDOFF);
3687 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
3689 sctp_mobility_feature_on(inp, SCTP_MOBILITY_FASTHANDOFF);
3690 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
3694 * bind specific, make sure flags is off and add a new
3695 * address structure to the sctp_addr_list inside the ep
3698 * We will need to allocate one and insert it at the head. The
3699 * socketopt call can just insert new addresses in there as
3700 * well. It will also have to do the embed scope kame hack
3701 * too (before adding).
3703 struct sctp_ifa *ifa;
3704 union sctp_sockstore store;
3706 memset(&store, 0, sizeof(store));
3707 switch (addr->sa_family) {
3710 memcpy(&store.sin, addr, sizeof(struct sockaddr_in));
3711 store.sin.sin_port = 0;
3716 memcpy(&store.sin6, addr, sizeof(struct sockaddr_in6));
3717 store.sin6.sin6_port = 0;
3720 #if defined(__Userspace__)
3722 memcpy(&store.sconn, addr, sizeof(struct sockaddr_conn));
3723 store.sconn.sconn_port = 0;
3730 * first find the interface with the bound address need to
3731 * zero out the port to find the address! yuck! can't do
3732 * this earlier since need port for sctp_pcb_findep()
3734 if (sctp_ifap != NULL) {
3737 /* Note for BSD we hit here always other
3738 * O/S's will pass things in via the
3739 * sctp_ifap argument.
3741 ifa = sctp_find_ifa_by_addr(&store.sa,
3742 vrf_id, SCTP_ADDR_NOT_LOCKED);
3745 /* Can't find an interface with that address */
3746 SCTP_INP_WUNLOCK(inp);
3747 SCTP_INP_INFO_WUNLOCK();
3748 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRNOTAVAIL);
3749 return (EADDRNOTAVAIL);
3752 if (addr->sa_family == AF_INET6) {
3753 /* GAK, more FIXME IFA lock? */
3754 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
3755 /* Can't bind a non-existent addr. */
3756 SCTP_INP_WUNLOCK(inp);
3757 SCTP_INP_INFO_WUNLOCK();
3758 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3763 /* we're not bound all */
3764 inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUNDALL;
3765 /* allow bindx() to send ASCONF's for binding changes */
3766 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
3767 /* clear automatic addr changes from kernel flag */
3768 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
3770 /* add this address to the endpoint list */
3771 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, 0);
3773 SCTP_INP_WUNLOCK(inp);
3774 SCTP_INP_INFO_WUNLOCK();
3779 /* find the bucket */
3780 if (port_reuse_active) {
3781 /* Put it into tcp 1-2-1 hash */
3782 head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashtcpmark))];
3783 inp->sctp_flags |= SCTP_PCB_FLAGS_IN_TCPPOOL;
3785 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashmark))];
3787 /* put it in the bucket */
3788 LIST_INSERT_HEAD(head, inp, sctp_hash);
3789 SCTPDBG(SCTP_DEBUG_PCB1, "Main hash to bind at head:%p, bound port:%d - in tcp_pool=%d\n",
3790 (void *)head, ntohs(lport), port_reuse_active);
3791 /* set in the port */
3792 inp->sctp_lport = lport;
3794 /* turn off just the unbound flag */
3795 inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
3796 SCTP_INP_WUNLOCK(inp);
3797 SCTP_INP_INFO_WUNLOCK();
3803 sctp_iterator_inp_being_freed(struct sctp_inpcb *inp)
3805 struct sctp_iterator *it, *nit;
3808 * We enter with the only the ITERATOR_LOCK in place and a write
3809 * lock on the inp_info stuff.
3811 it = sctp_it_ctl.cur_it;
3812 #if defined(__FreeBSD__) && !defined(__Userspace__)
3813 if (it && (it->vn != curvnet)) {
3814 /* Its not looking at our VNET */
3818 if (it && (it->inp == inp)) {
3820 * This is tricky and we hold the iterator lock,
3821 * but when it returns and gets the lock (when we
3822 * release it) the iterator will try to operate on
3823 * inp. We need to stop that from happening. But
3824 * of course the iterator has a reference on the
3825 * stcb and inp. We can mark it and it will stop.
3827 * If its a single iterator situation, we
3828 * set the end iterator flag. Otherwise
3829 * we set the iterator to go to the next inp.
3832 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
3833 sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_IT;
3835 sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_INP;
3838 /* Now go through and remove any single reference to
3839 * our inp that may be still pending on the list
3841 SCTP_IPI_ITERATOR_WQ_LOCK();
3842 TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) {
3843 #if defined(__FreeBSD__) && !defined(__Userspace__)
3844 if (it->vn != curvnet) {
3848 if (it->inp == inp) {
3849 /* This one points to me is it inp specific? */
3850 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
3851 /* Remove and free this one */
3852 TAILQ_REMOVE(&sctp_it_ctl.iteratorhead,
3854 if (it->function_atend != NULL) {
3855 (*it->function_atend) (it->pointer, it->val);
3857 SCTP_FREE(it, SCTP_M_ITER);
3859 it->inp = LIST_NEXT(it->inp, sctp_list);
3861 SCTP_INP_INCR_REF(it->inp);
3864 /* When its put in the refcnt is incremented so decr it */
3865 SCTP_INP_DECR_REF(inp);
3868 SCTP_IPI_ITERATOR_WQ_UNLOCK();
3871 /* release sctp_inpcb unbind the port */
3873 sctp_inpcb_free(struct sctp_inpcb *inp, int immediate, int from)
3876 * Here we free a endpoint. We must find it (if it is in the Hash
3877 * table) and remove it from there. Then we must also find it in the
3878 * overall list and remove it from there. After all removals are
3879 * complete then any timer has to be stopped. Then start the actual
3880 * freeing. a) Any local lists. b) Any associations. c) The hash of
3881 * all associations. d) finally the ep itself.
3883 struct sctp_tcb *asoc, *nasoc;
3884 struct sctp_laddr *laddr, *nladdr;
3885 struct inpcb *ip_pcb;
3887 int being_refed = 0;
3888 struct sctp_queued_to_read *sq, *nsq;
3889 #if !defined(__Userspace__)
3890 #if !defined(__FreeBSD__)
3895 sctp_sharedkey_t *shared_key, *nshared_key;
3898 #if defined(__APPLE__) && !defined(__Userspace__)
3899 sctp_lock_assert(SCTP_INP_SO(inp));
3901 #ifdef SCTP_LOG_CLOSING
3902 sctp_log_closing(inp, NULL, 0);
3904 SCTP_ITERATOR_LOCK();
3905 /* mark any iterators on the list or being processed */
3906 sctp_iterator_inp_being_freed(inp);
3907 SCTP_ITERATOR_UNLOCK();
3908 so = inp->sctp_socket;
3909 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
3910 /* been here before.. eeks.. get out of here */
3911 SCTP_PRINTF("This conflict in free SHOULD not be happening! from %d, imm %d\n", from, immediate);
3912 #ifdef SCTP_LOG_CLOSING
3913 sctp_log_closing(inp, NULL, 1);
3917 SCTP_ASOC_CREATE_LOCK(inp);
3918 SCTP_INP_INFO_WLOCK();
3920 SCTP_INP_WLOCK(inp);
3921 if (from == SCTP_CALLED_AFTER_CMPSET_OFCLOSE) {
3922 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CLOSE_IP;
3923 /* socket is gone, so no more wakeups allowed */
3924 inp->sctp_flags |= SCTP_PCB_FLAGS_DONT_WAKE;
3925 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
3926 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
3929 /* First time through we have the socket lock, after that no more. */
3930 sctp_timer_stop(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL,
3931 SCTP_FROM_SCTP_PCB + SCTP_LOC_1);
3934 sctp_m_freem(inp->control);
3935 inp->control = NULL;
3938 sctp_m_freem(inp->pkt);
3941 ip_pcb = &inp->ip_inp.inp; /* we could just cast the main pointer
3942 * here but I will be nice :> (i.e.
3944 if (immediate == SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE) {
3948 LIST_FOREACH_SAFE(asoc, &inp->sctp_asoc_list, sctp_tcblist, nasoc) {
3949 SCTP_TCB_LOCK(asoc);
3950 if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3951 /* Skip guys being freed */
3953 if (asoc->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE) {
3955 * Special case - we did not start a kill
3956 * timer on the asoc due to it was not
3957 * closed. So go ahead and start it now.
3959 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_IN_ACCEPT_QUEUE);
3960 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, asoc, NULL);
3962 SCTP_TCB_UNLOCK(asoc);
3965 if (((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
3966 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) &&
3967 (asoc->asoc.total_output_queue_size == 0)) {
3968 /* If we have data in queue, we don't want to just
3969 * free since the app may have done, send()/close
3970 * or connect/send/close. And it wants the data
3971 * to get across first.
3973 /* Just abandon things in the front states */
3974 if (sctp_free_assoc(inp, asoc, SCTP_PCBFREE_NOFORCE,
3975 SCTP_FROM_SCTP_PCB + SCTP_LOC_2) == 0) {
3980 /* Disconnect the socket please */
3981 asoc->sctp_socket = NULL;
3982 SCTP_ADD_SUBSTATE(asoc, SCTP_STATE_CLOSED_SOCKET);
3983 if ((asoc->asoc.size_on_reasm_queue > 0) ||
3984 (asoc->asoc.control_pdapi) ||
3985 (asoc->asoc.size_on_all_streams > 0) ||
3986 (so && (so->so_rcv.sb_cc > 0))) {
3987 /* Left with Data unread */
3988 struct mbuf *op_err;
3990 op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
3991 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_3;
3992 sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED);
3993 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3994 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
3995 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3996 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3998 if (sctp_free_assoc(inp, asoc,
3999 SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_4) == 0) {
4003 } else if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
4004 TAILQ_EMPTY(&asoc->asoc.sent_queue) &&
4005 (asoc->asoc.stream_queue_cnt == 0)) {
4006 if ((*asoc->asoc.ss_functions.sctp_ss_is_user_msgs_incomplete)(asoc, &asoc->asoc)) {
4009 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
4010 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
4011 struct sctp_nets *netp;
4014 * there is nothing queued to send,
4015 * so I send shutdown
4017 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4018 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4019 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4021 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
4022 sctp_stop_timers_for_shutdown(asoc);
4023 if (asoc->asoc.alternate) {
4024 netp = asoc->asoc.alternate;
4026 netp = asoc->asoc.primary_destination;
4028 sctp_send_shutdown(asoc, netp);
4029 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, asoc->sctp_ep, asoc,
4031 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc, NULL);
4032 sctp_chunk_output(inp, asoc, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_LOCKED);
4035 /* mark into shutdown pending */
4036 SCTP_ADD_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
4037 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc, NULL);
4038 if ((*asoc->asoc.ss_functions.sctp_ss_is_user_msgs_incomplete)(asoc, &asoc->asoc)) {
4039 SCTP_ADD_SUBSTATE(asoc, SCTP_STATE_PARTIAL_MSG_LEFT);
4041 if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
4042 TAILQ_EMPTY(&asoc->asoc.sent_queue) &&
4043 (asoc->asoc.state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
4044 struct mbuf *op_err;
4046 op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
4047 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_5;
4048 sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED);
4049 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
4050 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4051 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4052 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4054 if (sctp_free_assoc(inp, asoc,
4055 SCTP_PCBFREE_NOFORCE,
4056 SCTP_FROM_SCTP_PCB + SCTP_LOC_6) == 0) {
4061 sctp_chunk_output(inp, asoc, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
4065 SCTP_TCB_UNLOCK(asoc);
4067 /* now is there some left in our SHUTDOWN state? */
4069 #ifdef SCTP_LOG_CLOSING
4070 sctp_log_closing(inp, NULL, 2);
4072 inp->sctp_socket = NULL;
4073 SCTP_INP_WUNLOCK(inp);
4074 SCTP_ASOC_CREATE_UNLOCK(inp);
4075 SCTP_INP_INFO_WUNLOCK();
4079 inp->sctp_socket = NULL;
4080 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) !=
4081 SCTP_PCB_FLAGS_UNBOUND) {
4083 * ok, this guy has been bound. It's port is
4084 * somewhere in the SCTP_BASE_INFO(hash table). Remove
4087 LIST_REMOVE(inp, sctp_hash);
4088 inp->sctp_flags |= SCTP_PCB_FLAGS_UNBOUND;
4091 /* If there is a timer running to kill us,
4092 * forget it, since it may have a contest
4093 * on the INP lock.. which would cause us
4097 LIST_FOREACH_SAFE(asoc, &inp->sctp_asoc_list, sctp_tcblist, nasoc) {
4098 SCTP_TCB_LOCK(asoc);
4099 if (immediate != SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE) {
4100 /* Disconnect the socket please */
4101 asoc->sctp_socket = NULL;
4102 SCTP_ADD_SUBSTATE(asoc, SCTP_STATE_CLOSED_SOCKET);
4104 if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
4105 if (asoc->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE) {
4106 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_IN_ACCEPT_QUEUE);
4107 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, asoc, NULL);
4110 SCTP_TCB_UNLOCK(asoc);
4113 /* Free associations that are NOT killing us */
4114 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
4115 ((asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0)) {
4116 struct mbuf *op_err;
4118 op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
4119 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_7;
4120 sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED);
4121 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
4122 } else if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
4124 SCTP_TCB_UNLOCK(asoc);
4127 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) ||
4128 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4129 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4131 if (sctp_free_assoc(inp, asoc, SCTP_PCBFREE_FORCE,
4132 SCTP_FROM_SCTP_PCB + SCTP_LOC_8) == 0) {
4137 /* Ok we have someone out there that will kill us */
4138 #ifdef SCTP_LOG_CLOSING
4139 sctp_log_closing(inp, NULL, 3);
4141 SCTP_INP_WUNLOCK(inp);
4142 SCTP_ASOC_CREATE_UNLOCK(inp);
4143 SCTP_INP_INFO_WUNLOCK();
4146 if (SCTP_INP_LOCK_CONTENDED(inp))
4148 if (SCTP_INP_READ_CONTENDED(inp))
4150 if (SCTP_ASOC_CREATE_LOCK_CONTENDED(inp))
4152 /* NOTE: 0 refcount also means no timers are referencing us. */
4153 if ((inp->refcount) ||
4155 (inp->sctp_flags & SCTP_PCB_FLAGS_CLOSE_IP)) {
4156 #ifdef SCTP_LOG_CLOSING
4157 sctp_log_closing(inp, NULL, 4);
4159 sctp_timer_start(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL);
4160 SCTP_INP_WUNLOCK(inp);
4161 SCTP_ASOC_CREATE_UNLOCK(inp);
4162 SCTP_INP_INFO_WUNLOCK();
4165 inp->sctp_ep.signature_change.type = 0;
4166 inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_ALLGONE;
4167 /* Remove it from the list .. last thing we need a
4170 LIST_REMOVE(inp, sctp_list);
4171 SCTP_INP_WUNLOCK(inp);
4172 SCTP_ASOC_CREATE_UNLOCK(inp);
4173 SCTP_INP_INFO_WUNLOCK();
4175 #ifdef SCTP_LOG_CLOSING
4176 sctp_log_closing(inp, NULL, 5);
4179 #if !(defined(_WIN32) || defined(__Userspace__))
4180 #if !(defined(__FreeBSD__) && !defined(__Userspace__))
4181 rt = ip_pcb->inp_route.ro_rt;
4185 if ((inp->sctp_asocidhash) != NULL) {
4186 SCTP_HASH_FREE(inp->sctp_asocidhash, inp->hashasocidmark);
4187 inp->sctp_asocidhash = NULL;
4189 /*sa_ignore FREED_MEMORY*/
4190 TAILQ_FOREACH_SAFE(sq, &inp->read_queue, next, nsq) {
4191 /* Its only abandoned if it had data left */
4193 SCTP_STAT_INCR(sctps_left_abandon);
4195 TAILQ_REMOVE(&inp->read_queue, sq, next);
4196 sctp_free_remote_addr(sq->whoFrom);
4198 so->so_rcv.sb_cc -= sq->length;
4200 sctp_m_freem(sq->data);
4204 * no need to free the net count, since at this point all
4207 sctp_free_a_readq(NULL, sq);
4209 /* Now the sctp_pcb things */
4211 * free each asoc if it is not already closed/free. we can't use the
4212 * macro here since le_next will get freed as part of the
4213 * sctp_free_assoc() call.
4215 if (ip_pcb->inp_options) {
4216 (void)sctp_m_free(ip_pcb->inp_options);
4217 ip_pcb->inp_options = 0;
4220 #if !(defined(_WIN32) || defined(__Userspace__))
4221 #if !defined(__FreeBSD__)
4224 ip_pcb->inp_route.ro_rt = 0;
4230 #if !(defined(_WIN32) || defined(__Userspace__))
4231 #if (defined(__FreeBSD__) || defined(__APPLE__) && !defined(__Userspace__))
4232 if (ip_pcb->inp_vflag & INP_IPV6) {
4234 if (inp->inp_vflag & INP_IPV6) {
4236 ip6_freepcbopts(ip_pcb->in6p_outputopts);
4240 ip_pcb->inp_vflag = 0;
4241 /* free up authentication fields */
4242 if (inp->sctp_ep.local_auth_chunks != NULL)
4243 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
4244 if (inp->sctp_ep.local_hmacs != NULL)
4245 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
4247 LIST_FOREACH_SAFE(shared_key, &inp->sctp_ep.shared_keys, next, nshared_key) {
4248 LIST_REMOVE(shared_key, next);
4249 sctp_free_sharedkey(shared_key);
4250 /*sa_ignore FREED_MEMORY*/
4253 #if defined(__APPLE__) && !defined(__Userspace__)
4254 inp->ip_inp.inp.inp_state = INPCB_STATE_DEAD;
4255 if (in_pcb_checkstate(&inp->ip_inp.inp, WNT_STOPUSING, 1) != WNT_STOPUSING) {
4257 panic("sctp_inpcb_free inp = %p couldn't set to STOPUSING\n", (void *)inp);
4259 SCTP_PRINTF("sctp_inpcb_free inp = %p couldn't set to STOPUSING\n", (void *)inp);
4262 inp->ip_inp.inp.inp_socket->so_flags |= SOF_PCBCLEARING;
4265 * if we have an address list the following will free the list of
4266 * ifaddr's that are set into this ep. Again macro limitations here,
4267 * since the LIST_FOREACH could be a bad idea.
4269 LIST_FOREACH_SAFE(laddr, &inp->sctp_addr_list, sctp_nxt_addr, nladdr) {
4270 sctp_remove_laddr(laddr);
4273 #ifdef SCTP_TRACK_FREED_ASOCS
4275 LIST_FOREACH_SAFE(asoc, &inp->sctp_asoc_free_list, sctp_tcblist, nasoc) {
4276 LIST_REMOVE(asoc, sctp_tcblist);
4277 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), asoc);
4278 SCTP_DECR_ASOC_COUNT();
4280 /* *** END TEMP CODE ****/
4283 SCTP_FREE(inp->m_vrf_ids, SCTP_M_MVRF);
4285 /* Now lets see about freeing the EP hash table. */
4286 if (inp->sctp_tcbhash != NULL) {
4287 SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark);
4288 inp->sctp_tcbhash = NULL;
4290 /* Now we must put the ep memory back into the zone pool */
4291 #if defined(__FreeBSD__) && !defined(__Userspace__)
4292 crfree(inp->ip_inp.inp.inp_cred);
4293 INP_LOCK_DESTROY(&inp->ip_inp.inp);
4295 SCTP_INP_LOCK_DESTROY(inp);
4296 SCTP_INP_READ_DESTROY(inp);
4297 SCTP_ASOC_CREATE_LOCK_DESTROY(inp);
4298 #if !(defined(__APPLE__) && !defined(__Userspace__))
4299 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
4300 SCTP_DECR_EP_COUNT();
4302 /* For Tiger, we will do this later... */
4308 sctp_findnet(struct sctp_tcb *stcb, struct sockaddr *addr)
4310 struct sctp_nets *net;
4311 /* locate the address */
4312 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4313 if (sctp_cmpaddr(addr, (struct sockaddr *)&net->ro._l_addr))
4321 sctp_is_address_on_local_host(struct sockaddr *addr, uint32_t vrf_id)
4323 struct sctp_ifa *sctp_ifa;
4324 sctp_ifa = sctp_find_ifa_by_addr(addr, vrf_id, SCTP_ADDR_NOT_LOCKED);
4333 * add's a remote endpoint address, done with the INIT/INIT-ACK as well as
4334 * when a ASCONF arrives that adds it. It will also initialize all the cwnd
4338 sctp_add_remote_addr(struct sctp_tcb *stcb, struct sockaddr *newaddr,
4339 struct sctp_nets **netp, uint16_t port, int set_scope, int from)
4342 * The following is redundant to the same lines in the
4343 * sctp_aloc_assoc() but is needed since others call the add
4346 struct sctp_nets *net, *netfirst;
4349 SCTPDBG(SCTP_DEBUG_PCB1, "Adding an address (from:%d) to the peer: ",
4351 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, newaddr);
4353 netfirst = sctp_findnet(stcb, newaddr);
4356 * Lie and return ok, we don't want to make the association
4357 * go away for this behavior. It will happen in the TCP
4358 * model in a connected socket. It does not reach the hash
4359 * table until after the association is built so it can't be
4360 * found. Mark as reachable, since the initial creation will
4361 * have been cleared and the NOT_IN_ASSOC flag will have
4362 * been added... and we don't want to end up removing it
4365 if (netfirst->dest_state & SCTP_ADDR_UNCONFIRMED) {
4366 netfirst->dest_state = (SCTP_ADDR_REACHABLE |
4367 SCTP_ADDR_UNCONFIRMED);
4369 netfirst->dest_state = SCTP_ADDR_REACHABLE;
4375 switch (newaddr->sa_family) {
4379 struct sockaddr_in *sin;
4381 sin = (struct sockaddr_in *)newaddr;
4382 if (sin->sin_addr.s_addr == 0) {
4383 /* Invalid address */
4386 /* zero out the zero area */
4387 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
4389 /* assure len is set */
4391 sin->sin_len = sizeof(struct sockaddr_in);
4394 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
4395 stcb->asoc.scope.ipv4_local_scope = 1;
4398 /* Validate the address is in scope */
4399 if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) &&
4400 (stcb->asoc.scope.ipv4_local_scope == 0)) {
4410 struct sockaddr_in6 *sin6;
4412 sin6 = (struct sockaddr_in6 *)newaddr;
4413 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
4414 /* Invalid address */
4417 /* assure len is set */
4418 #ifdef HAVE_SIN6_LEN
4419 sin6->sin6_len = sizeof(struct sockaddr_in6);
4422 if (sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id)) {
4423 stcb->asoc.scope.loopback_scope = 1;
4424 stcb->asoc.scope.local_scope = 0;
4425 stcb->asoc.scope.ipv4_local_scope = 1;
4426 stcb->asoc.scope.site_scope = 1;
4427 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
4429 * If the new destination is a LINK_LOCAL we
4430 * must have common site scope. Don't set
4431 * the local scope since we may not share
4432 * all links, only loopback can do this.
4433 * Links on the local network would also be
4434 * on our private network for v4 too.
4436 stcb->asoc.scope.ipv4_local_scope = 1;
4437 stcb->asoc.scope.site_scope = 1;
4438 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
4440 * If the new destination is SITE_LOCAL then
4441 * we must have site scope in common.
4443 stcb->asoc.scope.site_scope = 1;
4446 /* Validate the address is in scope */
4447 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) &&
4448 (stcb->asoc.scope.loopback_scope == 0)) {
4450 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) &&
4451 (stcb->asoc.scope.local_scope == 0)) {
4453 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) &&
4454 (stcb->asoc.scope.site_scope == 0)) {
4461 #if defined(__Userspace__)
4464 struct sockaddr_conn *sconn;
4466 sconn = (struct sockaddr_conn *)newaddr;
4467 if (sconn->sconn_addr == NULL) {
4468 /* Invalid address */
4471 #ifdef HAVE_SCONN_LEN
4472 sconn->sconn_len = sizeof(struct sockaddr_conn);
4478 /* not supported family type */
4481 net = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_net), struct sctp_nets);
4485 SCTP_INCR_RADDR_COUNT();
4486 memset(net, 0, sizeof(struct sctp_nets));
4487 (void)SCTP_GETTIME_TIMEVAL(&net->start_time);
4489 memcpy(&net->ro._l_addr, newaddr, newaddr->sa_len);
4491 switch (newaddr->sa_family) {
4495 memcpy(&net->ro._l_addr, newaddr, sizeof(struct sockaddr_in));
4497 ((struct sockaddr_in *)&net->ro._l_addr)->sin_port = stcb->rport;
4503 memcpy(&net->ro._l_addr, newaddr, sizeof(struct sockaddr_in6));
4505 ((struct sockaddr_in6 *)&net->ro._l_addr)->sin6_port = stcb->rport;
4508 #if defined(__Userspace__)
4511 memcpy(&net->ro._l_addr, newaddr, sizeof(struct sockaddr_conn));
4513 ((struct sockaddr_conn *)&net->ro._l_addr)->sconn_port = stcb->rport;
4519 net->addr_is_local = sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id);
4520 if (net->addr_is_local && ((set_scope || (from == SCTP_ADDR_IS_CONFIRMED)))) {
4521 stcb->asoc.scope.loopback_scope = 1;
4522 stcb->asoc.scope.ipv4_local_scope = 1;
4523 stcb->asoc.scope.local_scope = 0;
4524 stcb->asoc.scope.site_scope = 1;
4527 net->failure_threshold = stcb->asoc.def_net_failure;
4528 net->pf_threshold = stcb->asoc.def_net_pf_threshold;
4529 if (addr_inscope == 0) {
4530 net->dest_state = (SCTP_ADDR_REACHABLE |
4531 SCTP_ADDR_OUT_OF_SCOPE);
4533 if (from == SCTP_ADDR_IS_CONFIRMED)
4534 /* SCTP_ADDR_IS_CONFIRMED is passed by connect_x */
4535 net->dest_state = SCTP_ADDR_REACHABLE;
4537 net->dest_state = SCTP_ADDR_REACHABLE |
4538 SCTP_ADDR_UNCONFIRMED;
4540 /* We set this to 0, the timer code knows that
4541 * this means its an initial value
4543 net->rto_needed = 1;
4545 net->RTO_measured = 0;
4546 stcb->asoc.numnets++;
4548 net->cwr_window_tsn = net->last_cwr_tsn = stcb->asoc.sending_seq - 1;
4550 net->dscp = stcb->asoc.default_dscp;
4552 net->flowlabel = stcb->asoc.default_flowlabel;
4554 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
4555 net->dest_state |= SCTP_ADDR_NOHB;
4557 net->dest_state &= ~SCTP_ADDR_NOHB;
4559 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD)) {
4560 net->dest_state |= SCTP_ADDR_NO_PMTUD;
4562 net->dest_state &= ~SCTP_ADDR_NO_PMTUD;
4564 net->heart_beat_delay = stcb->asoc.heart_beat_delay;
4565 /* Init the timer structure */
4566 SCTP_OS_TIMER_INIT(&net->rxt_timer.timer);
4567 SCTP_OS_TIMER_INIT(&net->pmtu_timer.timer);
4568 SCTP_OS_TIMER_INIT(&net->hb_timer.timer);
4570 /* Now generate a route for this guy */
4572 #ifdef SCTP_EMBEDDED_V6_SCOPE
4573 /* KAME hack: embed scopeid */
4574 if (newaddr->sa_family == AF_INET6) {
4575 struct sockaddr_in6 *sin6;
4577 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
4578 #if defined(__APPLE__) && !defined(__Userspace__)
4579 #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
4580 (void)in6_embedscope(&sin6->sin6_addr, sin6, &stcb->sctp_ep->ip_inp.inp, NULL);
4582 (void)in6_embedscope(&sin6->sin6_addr, sin6, &stcb->sctp_ep->ip_inp.inp, NULL, NULL);
4584 #elif defined(SCTP_KAME)
4585 (void)sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
4587 (void)in6_embedscope(&sin6->sin6_addr, sin6);
4589 #ifndef SCOPEDROUTING
4590 sin6->sin6_scope_id = 0;
4593 #endif /* SCTP_EMBEDDED_V6_SCOPE */
4595 SCTP_RTALLOC((sctp_route_t *)&net->ro,
4597 stcb->sctp_ep->fibnum);
4599 net->src_addr_selected = 0;
4600 #if !defined(__Userspace__)
4601 if (SCTP_ROUTE_HAS_VALID_IFN(&net->ro)) {
4602 /* Get source address */
4603 net->ro._s_addr = sctp_source_address_selection(stcb->sctp_ep,
4605 (sctp_route_t *)&net->ro,
4609 if (stcb->asoc.default_mtu > 0) {
4610 net->mtu = stcb->asoc.default_mtu;
4611 switch (net->ro._l_addr.sa.sa_family) {
4614 net->mtu += SCTP_MIN_V4_OVERHEAD;
4619 net->mtu += SCTP_MIN_OVERHEAD;
4622 #if defined(__Userspace__)
4624 net->mtu += sizeof(struct sctphdr);
4630 #if defined(INET) || defined(INET6)
4632 net->mtu += (uint32_t)sizeof(struct udphdr);
4635 } else if (net->ro._s_addr != NULL) {
4636 uint32_t imtu, rmtu, hcmtu;
4638 net->src_addr_selected = 1;
4639 /* Now get the interface MTU */
4640 if (net->ro._s_addr->ifn_p != NULL) {
4641 imtu = SCTP_GATHER_MTU_FROM_INTFC(net->ro._s_addr->ifn_p);
4645 #if defined(__FreeBSD__) && !defined(__Userspace__)
4646 rmtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, net->ro.ro_nh);
4647 hcmtu = sctp_hc_get_mtu(&net->ro._l_addr, stcb->sctp_ep->fibnum);
4649 rmtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, net->ro.ro_rt);
4652 net->mtu = sctp_min_mtu(hcmtu, rmtu, imtu);
4653 #if defined(__FreeBSD__) && !defined(__Userspace__)
4656 /* Start things off to match mtu of interface please. */
4657 SCTP_SET_MTU_OF_ROUTE(&net->ro._l_addr.sa,
4658 net->ro.ro_rt, net->mtu);
4664 if (net->mtu == 0) {
4665 if (stcb->asoc.default_mtu > 0) {
4666 net->mtu = stcb->asoc.default_mtu;
4667 switch (net->ro._l_addr.sa.sa_family) {
4670 net->mtu += SCTP_MIN_V4_OVERHEAD;
4675 net->mtu += SCTP_MIN_OVERHEAD;
4678 #if defined(__Userspace__)
4680 net->mtu += sizeof(struct sctphdr);
4686 #if defined(INET) || defined(INET6)
4688 net->mtu += (uint32_t)sizeof(struct udphdr);
4692 switch (newaddr->sa_family) {
4695 net->mtu = SCTP_DEFAULT_MTU;
4703 #if defined(__Userspace__)
4713 #if defined(INET) || defined(INET6)
4715 net->mtu -= (uint32_t)sizeof(struct udphdr);
4718 if (from == SCTP_ALLOC_ASOC) {
4719 stcb->asoc.smallest_mtu = net->mtu;
4721 if (stcb->asoc.smallest_mtu > net->mtu) {
4722 sctp_pathmtu_adjustment(stcb, net->mtu);
4725 #ifdef SCTP_EMBEDDED_V6_SCOPE
4726 if (newaddr->sa_family == AF_INET6) {
4727 struct sockaddr_in6 *sin6;
4729 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
4731 (void)sa6_recoverscope(sin6);
4733 (void)in6_recoverscope(sin6, &sin6->sin6_addr, NULL);
4734 #endif /* SCTP_KAME */
4736 #endif /* SCTP_EMBEDDED_V6_SCOPE */
4739 /* JRS - Use the congestion control given in the CC module */
4740 if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL)
4741 (*stcb->asoc.cc_functions.sctp_set_initial_cc_param)(stcb, net);
4744 * CMT: CUC algo - set find_pseudo_cumack to TRUE (1) at beginning
4745 * of assoc (2005/06/27, iyengar@cis.udel.edu)
4747 net->find_pseudo_cumack = 1;
4748 net->find_rtx_pseudo_cumack = 1;
4749 #if defined(__FreeBSD__) && !defined(__Userspace__)
4750 /* Choose an initial flowid. */
4751 net->flowid = stcb->asoc.my_vtag ^
4752 ntohs(stcb->rport) ^
4753 ntohs(stcb->sctp_ep->sctp_lport);
4754 net->flowtype = M_HASHTYPE_OPAQUE_HASH;
4759 netfirst = TAILQ_FIRST(&stcb->asoc.nets);
4760 #if defined(__FreeBSD__) && !defined(__Userspace__)
4761 if (net->ro.ro_nh == NULL) {
4763 if (net->ro.ro_rt == NULL) {
4765 /* Since we have no route put it at the back */
4766 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
4767 } else if (netfirst == NULL) {
4768 /* We are the first one in the pool. */
4769 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
4770 #if defined(__FreeBSD__) && !defined(__Userspace__)
4771 } else if (netfirst->ro.ro_nh == NULL) {
4773 } else if (netfirst->ro.ro_rt == NULL) {
4776 * First one has NO route. Place this one ahead of the first
4779 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
4780 #if defined(__FreeBSD__) && !defined(__Userspace__)
4781 } else if (net->ro.ro_nh->nh_ifp != netfirst->ro.ro_nh->nh_ifp) {
4783 } else if (net->ro.ro_rt->rt_ifp != netfirst->ro.ro_rt->rt_ifp) {
4786 * This one has a different interface than the one at the
4787 * top of the list. Place it ahead.
4789 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
4792 * Ok we have the same interface as the first one. Move
4793 * forward until we find either a) one with a NULL route...
4794 * insert ahead of that b) one with a different ifp.. insert
4795 * after that. c) end of the list.. insert at the tail.
4797 struct sctp_nets *netlook;
4800 netlook = TAILQ_NEXT(netfirst, sctp_next);
4801 if (netlook == NULL) {
4802 /* End of the list */
4803 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
4805 #if defined(__FreeBSD__) && !defined(__Userspace__)
4806 } else if (netlook->ro.ro_nh == NULL) {
4808 } else if (netlook->ro.ro_rt == NULL) {
4810 /* next one has NO route */
4811 TAILQ_INSERT_BEFORE(netfirst, net, sctp_next);
4813 #if defined(__FreeBSD__) && !defined(__Userspace__)
4814 } else if (netlook->ro.ro_nh->nh_ifp != net->ro.ro_nh->nh_ifp) {
4816 } else if (netlook->ro.ro_rt->rt_ifp != net->ro.ro_rt->rt_ifp) {
4818 TAILQ_INSERT_AFTER(&stcb->asoc.nets, netlook,
4824 } while (netlook != NULL);
4827 /* got to have a primary set */
4828 if (stcb->asoc.primary_destination == 0) {
4829 stcb->asoc.primary_destination = net;
4830 #if defined(__FreeBSD__) && !defined(__Userspace__)
4831 } else if ((stcb->asoc.primary_destination->ro.ro_nh == NULL) &&
4834 } else if ((stcb->asoc.primary_destination->ro.ro_rt == NULL) &&
4837 ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) {
4838 /* No route to current primary adopt new primary */
4839 stcb->asoc.primary_destination = net;
4841 /* Validate primary is first */
4842 net = TAILQ_FIRST(&stcb->asoc.nets);
4843 if ((net != stcb->asoc.primary_destination) &&
4844 (stcb->asoc.primary_destination)) {
4845 /* first one on the list is NOT the primary
4846 * sctp_cmpaddr() is much more efficient if
4847 * the primary is the first on the list, make it
4850 TAILQ_REMOVE(&stcb->asoc.nets,
4851 stcb->asoc.primary_destination, sctp_next);
4852 TAILQ_INSERT_HEAD(&stcb->asoc.nets,
4853 stcb->asoc.primary_destination, sctp_next);
4860 sctp_aloc_a_assoc_id(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
4863 struct sctpasochead *head;
4864 struct sctp_tcb *lstcb;
4867 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
4872 * We don't allow assoc id to be one of SCTP_FUTURE_ASSOC,
4873 * SCTP_CURRENT_ASSOC and SCTP_ALL_ASSOC.
4875 if (inp->sctp_associd_counter <= SCTP_ALL_ASSOC) {
4876 inp->sctp_associd_counter = SCTP_ALL_ASSOC + 1;
4878 id = inp->sctp_associd_counter;
4879 inp->sctp_associd_counter++;
4880 lstcb = sctp_findasoc_ep_asocid_locked(inp, (sctp_assoc_t)id, 0);
4884 head = &inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(id, inp->hashasocidmark)];
4885 LIST_INSERT_HEAD(head, stcb, sctp_tcbasocidhash);
4886 stcb->asoc.in_asocid_hash = 1;
4891 * allocate an association and add it to the endpoint. The caller must be
4892 * careful to add all additional addresses once they are know right away or
4893 * else the assoc will be may experience a blackout scenario.
4896 sctp_aloc_assoc(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
4897 int *error, uint32_t override_tag, uint32_t vrf_id,
4898 uint16_t o_streams, uint16_t port,
4899 #if defined(__FreeBSD__) && !defined(__Userspace__)
4901 #elif defined(_WIN32) && !defined(__Userspace__)
4904 #if defined(__Userspace__)
4905 /* __Userspace__ NULL proc is going to be passed here. See sctp_lower_sosend */
4909 int initialize_auth_params)
4911 /* note the p argument is only valid in unbound sockets */
4913 struct sctp_tcb *stcb;
4914 struct sctp_association *asoc;
4915 struct sctpasochead *head;
4920 * Assumption made here: Caller has done a
4921 * sctp_findassociation_ep_addr(ep, addr's); to make sure the
4922 * address does not exist already.
4924 if (SCTP_BASE_INFO(ipi_count_asoc) >= SCTP_MAX_NUM_OF_ASOC) {
4925 /* Hit max assoc, sorry no more */
4926 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
4930 if (firstaddr == NULL) {
4931 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4935 SCTP_INP_RLOCK(inp);
4936 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
4937 ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) ||
4938 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
4940 * If its in the TCP pool, its NOT allowed to create an
4941 * association. The parent listener needs to call
4942 * sctp_aloc_assoc.. or the one-2-many socket. If a peeled
4943 * off, or connected one does this.. its an error.
4945 SCTP_INP_RUNLOCK(inp);
4946 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4950 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4951 (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) {
4952 if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) ||
4953 (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED)) {
4954 SCTP_INP_RUNLOCK(inp);
4955 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4960 SCTPDBG(SCTP_DEBUG_PCB3, "Allocate an association for peer:");
4963 SCTPDBG_ADDR(SCTP_DEBUG_PCB3, firstaddr);
4964 switch (firstaddr->sa_family) {
4967 SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
4968 ntohs(((struct sockaddr_in *)firstaddr)->sin_port));
4973 SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
4974 ntohs(((struct sockaddr_in6 *)firstaddr)->sin6_port));
4977 #if defined(__Userspace__)
4979 SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
4980 ntohs(((struct sockaddr_conn *)firstaddr)->sconn_port));
4987 SCTPDBG(SCTP_DEBUG_PCB3,"None\n");
4989 #endif /* SCTP_DEBUG */
4990 switch (firstaddr->sa_family) {
4994 struct sockaddr_in *sin;
4996 sin = (struct sockaddr_in *)firstaddr;
4997 if ((ntohs(sin->sin_port) == 0) ||
4998 (sin->sin_addr.s_addr == INADDR_ANY) ||
4999 (sin->sin_addr.s_addr == INADDR_BROADCAST) ||
5000 IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
5001 /* Invalid address */
5002 SCTP_INP_RUNLOCK(inp);
5003 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
5007 rport = sin->sin_port;
5014 struct sockaddr_in6 *sin6;
5016 sin6 = (struct sockaddr_in6 *)firstaddr;
5017 if ((ntohs(sin6->sin6_port) == 0) ||
5018 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
5019 IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
5020 /* Invalid address */
5021 SCTP_INP_RUNLOCK(inp);
5022 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
5026 rport = sin6->sin6_port;
5030 #if defined(__Userspace__)
5033 struct sockaddr_conn *sconn;
5035 sconn = (struct sockaddr_conn *)firstaddr;
5036 if ((ntohs(sconn->sconn_port) == 0) ||
5037 (sconn->sconn_addr == NULL)) {
5038 /* Invalid address */
5039 SCTP_INP_RUNLOCK(inp);
5040 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
5044 rport = sconn->sconn_port;
5049 /* not supported family type */
5050 SCTP_INP_RUNLOCK(inp);
5051 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
5055 SCTP_INP_RUNLOCK(inp);
5056 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
5058 * If you have not performed a bind, then we need to do the
5059 * ephemeral bind for you.
5061 if ((err = sctp_inpcb_bind(inp->sctp_socket, NULL, NULL, p))) {
5062 /* bind error, probably perm */
5067 stcb = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_asoc), struct sctp_tcb);
5069 /* out of memory? */
5070 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
5074 SCTP_INCR_ASOC_COUNT();
5076 memset(stcb, 0, sizeof(*stcb));
5079 SCTP_TCB_LOCK_INIT(stcb);
5080 SCTP_TCB_SEND_LOCK_INIT(stcb);
5081 stcb->rport = rport;
5082 /* setup back pointer's */
5083 stcb->sctp_ep = inp;
5084 stcb->sctp_socket = inp->sctp_socket;
5085 if ((err = sctp_init_asoc(inp, stcb, override_tag, vrf_id, o_streams))) {
5087 SCTP_TCB_LOCK_DESTROY(stcb);
5088 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
5089 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
5090 SCTP_DECR_ASOC_COUNT();
5095 SCTP_INP_INFO_WLOCK();
5096 SCTP_INP_WLOCK(inp);
5097 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
5098 /* inpcb freed while alloc going on */
5099 SCTP_TCB_LOCK_DESTROY(stcb);
5100 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
5101 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
5102 SCTP_INP_WUNLOCK(inp);
5103 SCTP_INP_INFO_WUNLOCK();
5104 SCTP_DECR_ASOC_COUNT();
5105 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
5109 SCTP_TCB_LOCK(stcb);
5111 asoc->assoc_id = sctp_aloc_a_assoc_id(inp, stcb);
5112 /* now that my_vtag is set, add it to the hash */
5113 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
5114 /* put it in the bucket in the vtag hash of assoc's for the system */
5115 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
5116 SCTP_INP_INFO_WUNLOCK();
5118 if ((err = sctp_add_remote_addr(stcb, firstaddr, NULL, port, SCTP_DO_SETSCOPE, SCTP_ALLOC_ASOC))) {
5119 /* failure.. memory error? */
5120 if (asoc->strmout) {
5121 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
5122 asoc->strmout = NULL;
5124 if (asoc->mapping_array) {
5125 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
5126 asoc->mapping_array = NULL;
5128 if (asoc->nr_mapping_array) {
5129 SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
5130 asoc->nr_mapping_array = NULL;
5132 SCTP_DECR_ASOC_COUNT();
5133 SCTP_TCB_UNLOCK(stcb);
5134 SCTP_TCB_LOCK_DESTROY(stcb);
5135 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
5136 LIST_REMOVE(stcb, sctp_tcbasocidhash);
5137 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
5138 SCTP_INP_WUNLOCK(inp);
5139 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
5143 /* Init all the timers */
5144 SCTP_OS_TIMER_INIT(&asoc->dack_timer.timer);
5145 SCTP_OS_TIMER_INIT(&asoc->strreset_timer.timer);
5146 SCTP_OS_TIMER_INIT(&asoc->asconf_timer.timer);
5147 SCTP_OS_TIMER_INIT(&asoc->shut_guard_timer.timer);
5148 SCTP_OS_TIMER_INIT(&asoc->autoclose_timer.timer);
5149 SCTP_OS_TIMER_INIT(&asoc->delete_prim_timer.timer);
5151 LIST_INSERT_HEAD(&inp->sctp_asoc_list, stcb, sctp_tcblist);
5152 /* now file the port under the hash as well */
5153 if (inp->sctp_tcbhash != NULL) {
5154 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(stcb->rport,
5155 inp->sctp_hashmark)];
5156 LIST_INSERT_HEAD(head, stcb, sctp_tcbhash);
5158 if (initialize_auth_params == SCTP_INITIALIZE_AUTH_PARAMS) {
5159 sctp_initialize_auth_params(inp, stcb);
5161 SCTP_INP_WUNLOCK(inp);
5162 SCTPDBG(SCTP_DEBUG_PCB1, "Association %p now allocated\n", (void *)stcb);
5168 sctp_remove_net(struct sctp_tcb *stcb, struct sctp_nets *net)
5170 struct sctp_inpcb *inp;
5171 struct sctp_association *asoc;
5173 inp = stcb->sctp_ep;
5176 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
5177 if (net == asoc->primary_destination) {
5179 struct sctp_nets *lnet;
5181 lnet = TAILQ_FIRST(&asoc->nets);
5182 /* Mobility adaptation
5183 Ideally, if deleted destination is the primary, it becomes
5184 a fast retransmission trigger by the subsequent SET PRIMARY.
5187 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
5188 SCTP_MOBILITY_BASE) ||
5189 sctp_is_mobility_feature_on(stcb->sctp_ep,
5190 SCTP_MOBILITY_FASTHANDOFF)) {
5191 SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: primary dst is deleting\n");
5192 if (asoc->deleted_primary != NULL) {
5193 SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: deleted primary may be already stored\n");
5196 asoc->deleted_primary = net;
5197 atomic_add_int(&net->ref_count, 1);
5198 memset(&net->lastsa, 0, sizeof(net->lastsa));
5199 memset(&net->lastsv, 0, sizeof(net->lastsv));
5200 sctp_mobility_feature_on(stcb->sctp_ep,
5201 SCTP_MOBILITY_PRIM_DELETED);
5202 sctp_timer_start(SCTP_TIMER_TYPE_PRIM_DELETED,
5203 stcb->sctp_ep, stcb, NULL);
5206 /* Try to find a confirmed primary */
5207 asoc->primary_destination = sctp_find_alternate_net(stcb, lnet, 0);
5209 if (net == asoc->last_data_chunk_from) {
5211 asoc->last_data_chunk_from = TAILQ_FIRST(&asoc->nets);
5213 if (net == asoc->last_control_chunk_from) {
5215 asoc->last_control_chunk_from = NULL;
5217 if (net == stcb->asoc.alternate) {
5218 sctp_free_remote_addr(stcb->asoc.alternate);
5219 stcb->asoc.alternate = NULL;
5221 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
5222 SCTP_FROM_SCTP_PCB + SCTP_LOC_9);
5223 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
5224 SCTP_FROM_SCTP_PCB + SCTP_LOC_10);
5225 net->dest_state |= SCTP_ADDR_BEING_DELETED;
5226 sctp_free_remote_addr(net);
5230 * remove a remote endpoint address from an association, it will fail if the
5231 * address does not exist.
5234 sctp_del_remote_addr(struct sctp_tcb *stcb, struct sockaddr *remaddr)
5237 * Here we need to remove a remote address. This is quite simple, we
5238 * first find it in the list of address for the association
5239 * (tasoc->asoc.nets) and then if it is there, we do a LIST_REMOVE
5240 * on that item. Note we do not allow it to be removed if there are
5241 * no other addresses.
5243 struct sctp_association *asoc;
5244 struct sctp_nets *net, *nnet;
5248 /* locate the address */
5249 TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) {
5250 if (net->ro._l_addr.sa.sa_family != remaddr->sa_family) {
5253 if (sctp_cmpaddr((struct sockaddr *)&net->ro._l_addr,
5255 /* we found the guy */
5256 if (asoc->numnets < 2) {
5257 /* Must have at LEAST two remote addresses */
5260 sctp_remove_net(stcb, net);
5270 sctp_delete_from_timewait(uint32_t tag, uint16_t lport, uint16_t rport)
5272 struct sctpvtaghead *chain;
5273 struct sctp_tagblock *twait_block;
5277 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
5278 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
5279 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
5280 if ((twait_block->vtag_block[i].v_tag == tag) &&
5281 (twait_block->vtag_block[i].lport == lport) &&
5282 (twait_block->vtag_block[i].rport == rport)) {
5283 twait_block->vtag_block[i].tv_sec_at_expire = 0;
5284 twait_block->vtag_block[i].v_tag = 0;
5285 twait_block->vtag_block[i].lport = 0;
5286 twait_block->vtag_block[i].rport = 0;
5297 sctp_is_in_timewait(uint32_t tag, uint16_t lport, uint16_t rport)
5299 struct sctpvtaghead *chain;
5300 struct sctp_tagblock *twait_block;
5304 SCTP_INP_INFO_WLOCK();
5305 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
5306 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
5307 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
5308 if ((twait_block->vtag_block[i].v_tag == tag) &&
5309 (twait_block->vtag_block[i].lport == lport) &&
5310 (twait_block->vtag_block[i].rport == rport)) {
5318 SCTP_INP_INFO_WUNLOCK();
5324 sctp_add_vtag_to_timewait(uint32_t tag, uint32_t time, uint16_t lport, uint16_t rport)
5326 struct sctpvtaghead *chain;
5327 struct sctp_tagblock *twait_block;
5335 (void)SCTP_GETTIME_TIMEVAL(&now);
5336 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
5338 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
5339 /* Block(s) present, lets find space, and expire on the fly */
5340 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
5341 if ((twait_block->vtag_block[i].v_tag == 0) &&
5343 twait_block->vtag_block[i].tv_sec_at_expire =
5345 twait_block->vtag_block[i].v_tag = tag;
5346 twait_block->vtag_block[i].lport = lport;
5347 twait_block->vtag_block[i].rport = rport;
5349 } else if ((twait_block->vtag_block[i].v_tag) &&
5350 ((long)twait_block->vtag_block[i].tv_sec_at_expire < now.tv_sec)) {
5351 /* Audit expires this guy */
5352 twait_block->vtag_block[i].tv_sec_at_expire = 0;
5353 twait_block->vtag_block[i].v_tag = 0;
5354 twait_block->vtag_block[i].lport = 0;
5355 twait_block->vtag_block[i].rport = 0;
5357 /* Reuse it for my new tag */
5358 twait_block->vtag_block[i].tv_sec_at_expire = now.tv_sec + time;
5359 twait_block->vtag_block[i].v_tag = tag;
5360 twait_block->vtag_block[i].lport = lport;
5361 twait_block->vtag_block[i].rport = rport;
5368 * We only do up to the block where we can
5369 * place our tag for audits
5374 /* Need to add a new block to chain */
5376 SCTP_MALLOC(twait_block, struct sctp_tagblock *,
5377 sizeof(struct sctp_tagblock), SCTP_M_TIMW);
5378 if (twait_block == NULL) {
5381 memset(twait_block, 0, sizeof(struct sctp_tagblock));
5382 LIST_INSERT_HEAD(chain, twait_block, sctp_nxt_tagblock);
5383 twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec + time;
5384 twait_block->vtag_block[0].v_tag = tag;
5385 twait_block->vtag_block[0].lport = lport;
5386 twait_block->vtag_block[0].rport = rport;
5391 sctp_clean_up_stream(struct sctp_tcb *stcb, struct sctp_readhead *rh)
5393 struct sctp_tmit_chunk *chk, *nchk;
5394 struct sctp_queued_to_read *control, *ncontrol;
5396 TAILQ_FOREACH_SAFE(control, rh, next_instrm, ncontrol) {
5397 TAILQ_REMOVE(rh, control, next_instrm);
5398 control->on_strm_q = 0;
5399 if (control->on_read_q == 0) {
5400 sctp_free_remote_addr(control->whoFrom);
5401 if (control->data) {
5402 sctp_m_freem(control->data);
5403 control->data = NULL;
5406 /* Reassembly free? */
5407 TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) {
5408 TAILQ_REMOVE(&control->reasm, chk, sctp_next);
5410 sctp_m_freem(chk->data);
5413 if (chk->holds_key_ref)
5414 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5415 sctp_free_remote_addr(chk->whoTo);
5416 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5417 SCTP_DECR_CHK_COUNT();
5418 /*sa_ignore FREED_MEMORY*/
5421 * We don't free the address here
5422 * since all the net's were freed
5425 if (control->on_read_q == 0) {
5426 sctp_free_a_readq(stcb, control);
5432 * Free the association after un-hashing the remote port. This
5433 * function ALWAYS returns holding NO LOCK on the stcb. It DOES
5434 * expect that the input to this function IS a locked TCB.
5435 * It will return 0, if it did NOT destroy the association (instead
5436 * it unlocks it. It will return NON-zero if it either destroyed the
5437 * association OR the association is already destroyed.
5440 sctp_free_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int from_inpcbfree, int from_location)
5443 struct sctp_association *asoc;
5444 struct sctp_nets *net, *nnet;
5445 struct sctp_laddr *laddr, *naddr;
5446 struct sctp_tmit_chunk *chk, *nchk;
5447 struct sctp_asconf_addr *aparam, *naparam;
5448 struct sctp_asconf_ack *aack, *naack;
5449 struct sctp_stream_reset_list *strrst, *nstrrst;
5450 struct sctp_queued_to_read *sq, *nsq;
5451 struct sctp_stream_queue_pending *sp, *nsp;
5452 sctp_sharedkey_t *shared_key, *nshared_key;
5455 /* first, lets purge the entry from the hash table. */
5456 #if defined(__APPLE__) && !defined(__Userspace__)
5457 sctp_lock_assert(SCTP_INP_SO(inp));
5460 #ifdef SCTP_LOG_CLOSING
5461 sctp_log_closing(inp, stcb, 6);
5463 if (stcb->asoc.state == 0) {
5464 #ifdef SCTP_LOG_CLOSING
5465 sctp_log_closing(inp, NULL, 7);
5467 /* there is no asoc, really TSNH :-0 */
5470 if (stcb->asoc.alternate) {
5471 sctp_free_remote_addr(stcb->asoc.alternate);
5472 stcb->asoc.alternate = NULL;
5474 #if !(defined(__APPLE__) && !defined(__Userspace__))
5476 if (stcb->freed_from_where == 0) {
5477 /* Only record the first place free happened from */
5478 stcb->freed_from_where = from_location;
5484 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
5485 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
5486 /* nothing around */
5489 so = inp->sctp_socket;
5492 * We used timer based freeing if a reader or writer is in the way.
5493 * So we first check if we are actually being called from a timer,
5494 * if so we abort early if a reader or writer is still in the way.
5496 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) &&
5497 (from_inpcbfree == SCTP_NORMAL_PROC)) {
5499 * is it the timer driving us? if so are the reader/writers
5502 if (stcb->asoc.refcnt) {
5503 /* nope, reader or writer in the way */
5504 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
5505 /* no asoc destroyed */
5506 SCTP_TCB_UNLOCK(stcb);
5507 #ifdef SCTP_LOG_CLOSING
5508 sctp_log_closing(inp, stcb, 8);
5513 /* Now clean up any other timers */
5514 sctp_stop_association_timers(stcb, false);
5515 /* Now the read queue needs to be cleaned up (only once) */
5516 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0) {
5517 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_ABOUT_TO_BE_FREED);
5518 SCTP_INP_READ_LOCK(inp);
5519 TAILQ_FOREACH(sq, &inp->read_queue, next) {
5520 if (sq->stcb == stcb) {
5521 sq->do_not_ref_stcb = 1;
5522 sq->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
5523 /* If there is no end, there never
5526 if (sq->end_added == 0) {
5527 /* Held for PD-API clear that. */
5528 sq->pdapi_aborted = 1;
5529 sq->held_length = 0;
5530 if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT) && (so != NULL)) {
5532 * Need to add a PD-API aborted indication.
5533 * Setting the control_pdapi assures that it will
5534 * be added right after this msg.
5537 stcb->asoc.control_pdapi = sq;
5538 strseq = (sq->sinfo_stream << 16) | (sq->mid & 0x0000ffff);
5539 sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
5541 SCTP_PARTIAL_DELIVERY_ABORTED,
5544 stcb->asoc.control_pdapi = NULL;
5547 /* Add an end to wake them */
5551 SCTP_INP_READ_UNLOCK(inp);
5552 if (stcb->block_entry) {
5553 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PCB, ECONNRESET);
5554 stcb->block_entry->error = ECONNRESET;
5555 stcb->block_entry = NULL;
5558 if ((stcb->asoc.refcnt) || (stcb->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE)) {
5559 /* Someone holds a reference OR the socket is unaccepted yet.
5561 if ((stcb->asoc.refcnt) ||
5562 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
5563 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
5564 SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE);
5565 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
5567 SCTP_TCB_UNLOCK(stcb);
5568 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
5569 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
5570 /* nothing around */
5573 /* Wake any reader/writers */
5574 sctp_sorwakeup(inp, so);
5575 sctp_sowwakeup(inp, so);
5578 #ifdef SCTP_LOG_CLOSING
5579 sctp_log_closing(inp, stcb, 9);
5581 /* no asoc destroyed */
5584 #ifdef SCTP_LOG_CLOSING
5585 sctp_log_closing(inp, stcb, 10);
5587 /* When I reach here, no others want
5588 * to kill the assoc yet.. and I own
5589 * the lock. Now its possible an abort
5590 * comes in when I do the lock exchange
5591 * below to grab all the locks to do
5592 * the final take out. to prevent this
5593 * we increment the count, which will
5594 * start a timer and blow out above thus
5595 * assuring us that we hold exclusive
5596 * killing of the asoc. Note that
5597 * after getting back the TCB lock
5598 * we will go ahead and increment the
5599 * counter back up and stop any timer
5600 * a passing stranger may have started :-S
5602 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5603 atomic_add_int(&stcb->asoc.refcnt, 1);
5605 SCTP_TCB_UNLOCK(stcb);
5606 SCTP_INP_INFO_WLOCK();
5607 SCTP_INP_WLOCK(inp);
5608 SCTP_TCB_LOCK(stcb);
5610 /* Double check the GONE flag */
5611 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
5612 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
5613 /* nothing around */
5616 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5617 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
5619 * For TCP type we need special handling when we are
5620 * connected. We also include the peel'ed off ones to.
5622 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
5623 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
5624 inp->sctp_flags |= SCTP_PCB_FLAGS_WAS_CONNECTED;
5626 SOCKBUF_LOCK(&so->so_rcv);
5627 so->so_state &= ~(SS_ISCONNECTING |
5628 SS_ISDISCONNECTING |
5631 so->so_state |= SS_ISDISCONNECTED;
5632 #if defined(__APPLE__) && !defined(__Userspace__)
5635 socantrcvmore_locked(so);
5638 sctp_sowwakeup(inp, so);
5639 sctp_sorwakeup(inp, so);
5645 /* Make it invalid too, that way if its
5646 * about to run it will abort and return.
5648 /* re-increment the lock */
5649 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5650 atomic_add_int(&stcb->asoc.refcnt, -1);
5652 if (stcb->asoc.refcnt) {
5653 SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE);
5654 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
5655 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5656 SCTP_INP_INFO_WUNLOCK();
5657 SCTP_INP_WUNLOCK(inp);
5659 SCTP_TCB_UNLOCK(stcb);
5663 if (inp->sctp_tcbhash) {
5664 LIST_REMOVE(stcb, sctp_tcbhash);
5666 if (stcb->asoc.in_asocid_hash) {
5667 LIST_REMOVE(stcb, sctp_tcbasocidhash);
5669 /* Now lets remove it from the list of ALL associations in the EP */
5670 LIST_REMOVE(stcb, sctp_tcblist);
5671 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5672 SCTP_INP_INCR_REF(inp);
5673 SCTP_INP_WUNLOCK(inp);
5675 /* pull from vtag hash */
5676 LIST_REMOVE(stcb, sctp_asocs);
5677 sctp_add_vtag_to_timewait(asoc->my_vtag, SCTP_BASE_SYSCTL(sctp_vtag_time_wait),
5678 inp->sctp_lport, stcb->rport);
5680 /* Now restop the timers to be sure
5681 * this is paranoia at is finest!
5683 sctp_stop_association_timers(stcb, true);
5686 * The chunk lists and such SHOULD be empty but we check them just
5689 /* anything on the wheel needs to be removed */
5690 SCTP_TCB_SEND_LOCK(stcb);
5691 for (i = 0; i < asoc->streamoutcnt; i++) {
5692 struct sctp_stream_out *outs;
5694 outs = &asoc->strmout[i];
5695 /* now clean up any chunks here */
5696 TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
5697 atomic_subtract_int(&asoc->stream_queue_cnt, 1);
5698 TAILQ_REMOVE(&outs->outqueue, sp, next);
5699 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, outs, sp, 1);
5700 sctp_free_spbufspace(stcb, asoc, sp);
5703 /* Still an open socket - report */
5704 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb,
5705 0, (void *)sp, SCTP_SO_LOCKED);
5708 sctp_m_freem(sp->data);
5710 sp->tail_mbuf = NULL;
5715 sctp_free_remote_addr(sp->net);
5718 sctp_free_a_strmoq(stcb, sp, SCTP_SO_LOCKED);
5721 SCTP_TCB_SEND_UNLOCK(stcb);
5722 /*sa_ignore FREED_MEMORY*/
5723 TAILQ_FOREACH_SAFE(strrst, &asoc->resetHead, next_resp, nstrrst) {
5724 TAILQ_REMOVE(&asoc->resetHead, strrst, next_resp);
5725 SCTP_FREE(strrst, SCTP_M_STRESET);
5727 TAILQ_FOREACH_SAFE(sq, &asoc->pending_reply_queue, next, nsq) {
5728 TAILQ_REMOVE(&asoc->pending_reply_queue, sq, next);
5730 sctp_m_freem(sq->data);
5733 sctp_free_remote_addr(sq->whoFrom);
5736 /* Free the ctl entry */
5737 sctp_free_a_readq(stcb, sq);
5738 /*sa_ignore FREED_MEMORY*/
5740 TAILQ_FOREACH_SAFE(chk, &asoc->free_chunks, sctp_next, nchk) {
5741 TAILQ_REMOVE(&asoc->free_chunks, chk, sctp_next);
5743 sctp_m_freem(chk->data);
5746 if (chk->holds_key_ref)
5747 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5748 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5749 SCTP_DECR_CHK_COUNT();
5750 atomic_subtract_int(&SCTP_BASE_INFO(ipi_free_chunks), 1);
5751 asoc->free_chunk_cnt--;
5752 /*sa_ignore FREED_MEMORY*/
5754 /* pending send queue SHOULD be empty */
5755 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
5756 if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
5757 asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
5760 panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
5763 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
5766 /* Still a socket? */
5767 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
5768 0, chk, SCTP_SO_LOCKED);
5771 sctp_m_freem(chk->data);
5775 if (chk->holds_key_ref)
5776 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5778 sctp_free_remote_addr(chk->whoTo);
5781 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5782 SCTP_DECR_CHK_COUNT();
5783 /*sa_ignore FREED_MEMORY*/
5785 /* sent queue SHOULD be empty */
5786 TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) {
5787 if (chk->sent != SCTP_DATAGRAM_NR_ACKED) {
5788 if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
5789 asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
5792 panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
5796 TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
5799 /* Still a socket? */
5800 sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb,
5801 0, chk, SCTP_SO_LOCKED);
5804 sctp_m_freem(chk->data);
5808 if (chk->holds_key_ref)
5809 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5810 sctp_free_remote_addr(chk->whoTo);
5811 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5812 SCTP_DECR_CHK_COUNT();
5813 /*sa_ignore FREED_MEMORY*/
5816 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
5817 if (stcb->asoc.strmout[i].chunks_on_queues > 0) {
5818 panic("%u chunks left for stream %u.", stcb->asoc.strmout[i].chunks_on_queues, i);
5822 /* control queue MAY not be empty */
5823 TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
5824 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
5826 sctp_m_freem(chk->data);
5829 if (chk->holds_key_ref)
5830 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5831 sctp_free_remote_addr(chk->whoTo);
5832 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5833 SCTP_DECR_CHK_COUNT();
5834 /*sa_ignore FREED_MEMORY*/
5836 /* ASCONF queue MAY not be empty */
5837 TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
5838 TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
5840 sctp_m_freem(chk->data);
5843 if (chk->holds_key_ref)
5844 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5845 sctp_free_remote_addr(chk->whoTo);
5846 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5847 SCTP_DECR_CHK_COUNT();
5848 /*sa_ignore FREED_MEMORY*/
5850 if (asoc->mapping_array) {
5851 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
5852 asoc->mapping_array = NULL;
5854 if (asoc->nr_mapping_array) {
5855 SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
5856 asoc->nr_mapping_array = NULL;
5858 /* the stream outs */
5859 if (asoc->strmout) {
5860 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
5861 asoc->strmout = NULL;
5863 asoc->strm_realoutsize = asoc->streamoutcnt = 0;
5865 for (i = 0; i < asoc->streamincnt; i++) {
5866 sctp_clean_up_stream(stcb, &asoc->strmin[i].inqueue);
5867 sctp_clean_up_stream(stcb, &asoc->strmin[i].uno_inqueue);
5869 SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
5870 asoc->strmin = NULL;
5872 asoc->streamincnt = 0;
5873 TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) {
5875 if (SCTP_BASE_INFO(ipi_count_raddr) == 0) {
5876 panic("no net's left alloc'ed, or list points to itself");
5879 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
5880 sctp_free_remote_addr(net);
5882 LIST_FOREACH_SAFE(laddr, &asoc->sctp_restricted_addrs, sctp_nxt_addr, naddr) {
5883 /*sa_ignore FREED_MEMORY*/
5884 sctp_remove_laddr(laddr);
5887 /* pending asconf (address) parameters */
5888 TAILQ_FOREACH_SAFE(aparam, &asoc->asconf_queue, next, naparam) {
5889 /*sa_ignore FREED_MEMORY*/
5890 TAILQ_REMOVE(&asoc->asconf_queue, aparam, next);
5891 SCTP_FREE(aparam,SCTP_M_ASC_ADDR);
5893 TAILQ_FOREACH_SAFE(aack, &asoc->asconf_ack_sent, next, naack) {
5894 /*sa_ignore FREED_MEMORY*/
5895 TAILQ_REMOVE(&asoc->asconf_ack_sent, aack, next);
5896 if (aack->data != NULL) {
5897 sctp_m_freem(aack->data);
5899 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asconf_ack), aack);
5901 /* clean up auth stuff */
5902 if (asoc->local_hmacs)
5903 sctp_free_hmaclist(asoc->local_hmacs);
5904 if (asoc->peer_hmacs)
5905 sctp_free_hmaclist(asoc->peer_hmacs);
5907 if (asoc->local_auth_chunks)
5908 sctp_free_chunklist(asoc->local_auth_chunks);
5909 if (asoc->peer_auth_chunks)
5910 sctp_free_chunklist(asoc->peer_auth_chunks);
5912 sctp_free_authinfo(&asoc->authinfo);
5914 LIST_FOREACH_SAFE(shared_key, &asoc->shared_keys, next, nshared_key) {
5915 LIST_REMOVE(shared_key, next);
5916 sctp_free_sharedkey(shared_key);
5917 /*sa_ignore FREED_MEMORY*/
5920 /* Insert new items here :> */
5922 /* Get rid of LOCK */
5923 SCTP_TCB_UNLOCK(stcb);
5924 SCTP_TCB_LOCK_DESTROY(stcb);
5925 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
5926 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5927 SCTP_INP_INFO_WUNLOCK();
5928 SCTP_INP_RLOCK(inp);
5930 #if defined(__APPLE__) && !defined(__Userspace__)
5932 stcb->freed_from_where = from_location;
5934 #ifdef SCTP_TRACK_FREED_ASOCS
5935 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5936 /* now clean up the tasoc itself */
5937 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
5938 SCTP_DECR_ASOC_COUNT();
5940 LIST_INSERT_HEAD(&inp->sctp_asoc_free_list, stcb, sctp_tcblist);
5943 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
5944 SCTP_DECR_ASOC_COUNT();
5946 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5947 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5948 /* If its NOT the inp_free calling us AND
5949 * sctp_close as been called, we
5952 SCTP_INP_RUNLOCK(inp);
5953 /* This will start the kill timer (if we are
5954 * the last one) since we hold an increment yet. But
5955 * this is the only safe way to do this
5956 * since otherwise if the socket closes
5957 * at the same time we are here we might
5958 * collide in the cleanup.
5960 sctp_inpcb_free(inp,
5961 SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
5962 SCTP_CALLED_DIRECTLY_NOCMPSET);
5963 SCTP_INP_DECR_REF(inp);
5965 /* The socket is still open. */
5966 SCTP_INP_DECR_REF(inp);
5967 SCTP_INP_RUNLOCK(inp);
5970 /* destroyed the asoc */
5971 #ifdef SCTP_LOG_CLOSING
5972 sctp_log_closing(inp, NULL, 11);
5980 * determine if a destination is "reachable" based upon the addresses bound
5981 * to the current endpoint (e.g. only v4 or v6 currently bound)
5984 * FIX: if we allow assoc-level bindx(), then this needs to be fixed to use
5985 * assoc level v4/v6 flags, as the assoc *may* not have the same address
5986 * types bound as its endpoint
5989 sctp_destination_is_reachable(struct sctp_tcb *stcb, struct sockaddr *destaddr)
5991 struct sctp_inpcb *inp;
5995 * No locks here, the TCB, in all cases is already locked and an
5996 * assoc is up. There is either a INP lock by the caller applied (in
5997 * asconf case when deleting an address) or NOT in the HB case,
5998 * however if HB then the INP increment is up and the INP will not
5999 * be removed (on top of the fact that we have a TCB lock). So we
6000 * only want to read the sctp_flags, which is either bound-all or
6001 * not.. no protection needed since once an assoc is up you can't be
6002 * changing your binding.
6004 inp = stcb->sctp_ep;
6005 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6006 /* if bound all, destination is not restricted */
6008 * RRS: Question during lock work: Is this correct? If you
6009 * are bound-all you still might need to obey the V4--V6
6010 * flags??? IMO this bound-all stuff needs to be removed!
6014 /* NOTE: all "scope" checks are done when local addresses are added */
6015 switch (destaddr->sa_family) {
6018 answer = inp->ip_inp.inp.inp_vflag & INP_IPV6;
6023 answer = inp->ip_inp.inp.inp_vflag & INP_IPV4;
6026 #if defined(__Userspace__)
6028 answer = inp->ip_inp.inp.inp_vflag & INP_CONN;
6032 /* invalid family, so it's unreachable */
6040 * update the inp_vflags on an endpoint
6043 sctp_update_ep_vflag(struct sctp_inpcb *inp)
6045 struct sctp_laddr *laddr;
6047 /* first clear the flag */
6048 inp->ip_inp.inp.inp_vflag = 0;
6049 /* set the flag based on addresses on the ep list */
6050 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
6051 if (laddr->ifa == NULL) {
6052 SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n",
6057 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
6060 switch (laddr->ifa->address.sa.sa_family) {
6063 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
6068 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
6071 #if defined(__Userspace__)
6073 inp->ip_inp.inp.inp_vflag |= INP_CONN;
6083 * Add the address to the endpoint local address list There is nothing to be
6084 * done if we are bound to all addresses
6087 sctp_add_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa, uint32_t action)
6089 struct sctp_laddr *laddr;
6090 struct sctp_tcb *stcb;
6095 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6096 /* You are already bound to all. You have it already */
6100 if (ifa->address.sa.sa_family == AF_INET6) {
6101 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
6102 /* Can't bind a non-useable addr. */
6107 /* first, is it already present? */
6108 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
6109 if (laddr->ifa == ifa) {
6116 /* Not in the ep list */
6117 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, action);
6121 /* update inp_vflag flags */
6122 switch (ifa->address.sa.sa_family) {
6125 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
6130 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
6133 #if defined(__Userspace__)
6135 inp->ip_inp.inp.inp_vflag |= INP_CONN;
6141 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6142 sctp_add_local_addr_restricted(stcb, ifa);
6150 * select a new (hopefully reachable) destination net (should only be used
6151 * when we deleted an ep addr that is the only usable source address to reach
6152 * the destination net)
6155 sctp_select_primary_destination(struct sctp_tcb *stcb)
6157 struct sctp_nets *net;
6159 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6160 /* for now, we'll just pick the first reachable one we find */
6161 if (net->dest_state & SCTP_ADDR_UNCONFIRMED)
6163 if (sctp_destination_is_reachable(stcb,
6164 (struct sockaddr *)&net->ro._l_addr)) {
6165 /* found a reachable destination */
6166 stcb->asoc.primary_destination = net;
6169 /* I can't there from here! ...we're gonna die shortly... */
6174 * Delete the address from the endpoint local address list. There is nothing
6175 * to be done if we are bound to all addresses
6178 sctp_del_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
6180 struct sctp_laddr *laddr;
6184 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6185 /* You are already bound to all. You have it already */
6188 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
6189 if (laddr->ifa == ifa) {
6194 if (fnd && (inp->laddr_count < 2)) {
6195 /* can't delete unless there are at LEAST 2 addresses */
6200 * clean up any use of this address go through our
6201 * associations and clear any last_used_address that match
6202 * this one for each assoc, see if a new primary_destination
6205 struct sctp_tcb *stcb;
6207 /* clean up "next_addr_touse" */
6208 if (inp->next_addr_touse == laddr)
6209 /* delete this address */
6210 inp->next_addr_touse = NULL;
6212 /* clean up "last_used_address" */
6213 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6214 struct sctp_nets *net;
6216 SCTP_TCB_LOCK(stcb);
6217 if (stcb->asoc.last_used_address == laddr)
6218 /* delete this address */
6219 stcb->asoc.last_used_address = NULL;
6220 /* Now spin through all the nets and purge any ref to laddr */
6221 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6222 if (net->ro._s_addr == laddr->ifa) {
6223 /* Yep, purge src address selected */
6224 #if defined(__FreeBSD__) && !defined(__Userspace__)
6225 RO_NHFREE(&net->ro);
6229 /* delete this address if cached */
6233 net->ro.ro_rt = NULL;
6236 sctp_free_ifa(net->ro._s_addr);
6237 net->ro._s_addr = NULL;
6238 net->src_addr_selected = 0;
6241 SCTP_TCB_UNLOCK(stcb);
6242 } /* for each tcb */
6243 /* remove it from the ep list */
6244 sctp_remove_laddr(laddr);
6246 /* update inp_vflag flags */
6247 sctp_update_ep_vflag(inp);
6253 * Add the address to the TCB local address restricted list.
6254 * This is a "pending" address list (eg. addresses waiting for an
6255 * ASCONF-ACK response) and cannot be used as a valid source address.
6258 sctp_add_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
6260 struct sctp_laddr *laddr;
6261 struct sctpladdr *list;
6264 * Assumes TCB is locked.. and possibly the INP. May need to
6265 * confirm/fix that if we need it and is not the case.
6267 list = &stcb->asoc.sctp_restricted_addrs;
6270 if (ifa->address.sa.sa_family == AF_INET6) {
6271 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
6272 /* Can't bind a non-existent addr. */
6277 /* does the address already exist? */
6278 LIST_FOREACH(laddr, list, sctp_nxt_addr) {
6279 if (laddr->ifa == ifa) {
6284 /* add to the list */
6285 (void)sctp_insert_laddr(list, ifa, 0);
6290 * Remove a local address from the TCB local address restricted list
6293 sctp_del_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
6295 struct sctp_inpcb *inp;
6296 struct sctp_laddr *laddr;
6299 * This is called by asconf work. It is assumed that a) The TCB is
6300 * locked and b) The INP is locked. This is true in as much as I can
6301 * trace through the entry asconf code where I did these locks.
6302 * Again, the ASCONF code is a bit different in that it does lock
6303 * the INP during its work often times. This must be since we don't
6304 * want other proc's looking up things while what they are looking
6305 * up is changing :-D
6308 inp = stcb->sctp_ep;
6309 /* if subset bound and don't allow ASCONF's, can't delete last */
6310 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) &&
6311 sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF)) {
6312 if (stcb->sctp_ep->laddr_count < 2) {
6313 /* can't delete last address */
6317 LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
6318 /* remove the address if it exists */
6319 if (laddr->ifa == NULL)
6321 if (laddr->ifa == ifa) {
6322 sctp_remove_laddr(laddr);
6327 /* address not found! */
6331 #if defined(__FreeBSD__) && !defined(__Userspace__)
6333 static int sctp_max_number_of_assoc = SCTP_MAX_NUM_OF_ASOC;
6334 static int sctp_scale_up_for_address = SCTP_SCALE_FOR_ADDR;
6337 #if defined(__FreeBSD__) && !defined(__Userspace__)
6338 #if defined(SCTP_MCORE_INPUT) && defined(SMP)
6339 struct sctp_mcore_ctrl *sctp_mcore_workers = NULL;
6340 int *sctp_cpuarry = NULL;
6343 sctp_queue_to_mcore(struct mbuf *m, int off, int cpu_to_use)
6345 /* Queue a packet to a processor for the specified core */
6346 struct sctp_mcore_queue *qent;
6347 struct sctp_mcore_ctrl *wkq;
6350 if (sctp_mcore_workers == NULL) {
6351 /* Something went way bad during setup */
6352 sctp_input_with_port(m, off, 0);
6355 SCTP_MALLOC(qent, struct sctp_mcore_queue *,
6356 (sizeof(struct sctp_mcore_queue)),
6359 /* This is trouble */
6360 sctp_input_with_port(m, off, 0);
6367 wkq = &sctp_mcore_workers[cpu_to_use];
6368 SCTP_MCORE_QLOCK(wkq);
6370 TAILQ_INSERT_TAIL(&wkq->que, qent, next);
6371 if (wkq->running == 0) {
6374 SCTP_MCORE_QUNLOCK(wkq);
6376 wakeup(&wkq->running);
6381 sctp_mcore_thread(void *arg)
6384 struct sctp_mcore_ctrl *wkq;
6385 struct sctp_mcore_queue *qent;
6387 wkq = (struct sctp_mcore_ctrl *)arg;
6391 /* Wait for first tickle */
6392 SCTP_MCORE_LOCK(wkq);
6394 msleep(&wkq->running,
6396 0, "wait for pkt", 0);
6397 SCTP_MCORE_UNLOCK(wkq);
6399 /* Bind to our cpu */
6400 thread_lock(curthread);
6401 sched_bind(curthread, wkq->cpuid);
6402 thread_unlock(curthread);
6404 /* Now lets start working */
6405 SCTP_MCORE_LOCK(wkq);
6406 /* Now grab lock and go */
6408 SCTP_MCORE_QLOCK(wkq);
6411 qent = TAILQ_FIRST(&wkq->que);
6413 TAILQ_REMOVE(&wkq->que, qent, next);
6414 SCTP_MCORE_QUNLOCK(wkq);
6415 CURVNET_SET(qent->vn);
6419 SCTP_FREE(qent, SCTP_M_MCORE);
6421 sctp_input_with_port(m, off, 0);
6423 SCTP_PRINTF("V6 not yet supported\n");
6427 SCTP_MCORE_QLOCK(wkq);
6430 if (!TAILQ_EMPTY(&wkq->que)) {
6433 SCTP_MCORE_QUNLOCK(wkq);
6434 msleep(&wkq->running,
6436 0, "wait for pkt", 0);
6441 sctp_startup_mcore_threads(void)
6448 if (sctp_mcore_workers != NULL) {
6449 /* Already been here in some previous
6454 SCTP_MALLOC(sctp_mcore_workers, struct sctp_mcore_ctrl *,
6455 ((mp_maxid+1) * sizeof(struct sctp_mcore_ctrl)),
6457 if (sctp_mcore_workers == NULL) {
6461 memset(sctp_mcore_workers, 0 , ((mp_maxid+1) *
6462 sizeof(struct sctp_mcore_ctrl)));
6463 /* Init the structures */
6464 for (i = 0; i<=mp_maxid; i++) {
6465 TAILQ_INIT(&sctp_mcore_workers[i].que);
6466 SCTP_MCORE_LOCK_INIT(&sctp_mcore_workers[i]);
6467 SCTP_MCORE_QLOCK_INIT(&sctp_mcore_workers[i]);
6468 sctp_mcore_workers[i].cpuid = i;
6470 if (sctp_cpuarry == NULL) {
6471 SCTP_MALLOC(sctp_cpuarry, int *,
6472 (mp_ncpus * sizeof(int)),
6476 sctp_cpuarry[i] = cpu;
6480 /* Now start them all */
6482 (void)kproc_create(sctp_mcore_thread,
6483 (void *)&sctp_mcore_workers[cpu],
6484 &sctp_mcore_workers[cpu].thread_proc,
6493 #if defined(__FreeBSD__) && !defined(__Userspace__)
6494 #if defined(SCTP_NOT_YET)
6495 static struct mbuf *
6496 sctp_netisr_hdlr(struct mbuf *m, uintptr_t source)
6501 uint32_t flowid, tag;
6504 * No flow id built by lower layers fix it so we
6507 ip = mtod(m, struct ip *);
6508 offset = (ip->ip_hl << 2) + sizeof(struct sctphdr);
6509 if (SCTP_BUF_LEN(m) < offset) {
6510 if ((m = m_pullup(m, offset)) == NULL) {
6511 SCTP_STAT_INCR(sctps_hdrops);
6514 ip = mtod(m, struct ip *);
6516 sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2));
6517 tag = htonl(sh->v_tag);
6518 flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port);
6519 m->m_pkthdr.flowid = flowid;
6521 m->m_flags |= M_FLOWID;
6528 #if defined(__Userspace__)
6529 sctp_pcb_init(int start_threads)
6535 * SCTP initialization for the PCB structures should be called by
6536 * the sctp_init() function.
6541 if (SCTP_BASE_VAR(sctp_pcb_initialized) != 0) {
6542 /* error I was called twice */
6545 SCTP_BASE_VAR(sctp_pcb_initialized) = 1;
6547 #if defined(SCTP_PROCESS_LEVEL_LOCKS)
6548 #if !defined(_WIN32)
6549 pthread_mutexattr_init(&SCTP_BASE_VAR(mtx_attr));
6551 pthread_mutexattr_settype(&SCTP_BASE_VAR(mtx_attr), PTHREAD_MUTEX_ERRORCHECK);
6555 #if defined(SCTP_LOCAL_TRACE_BUF)
6556 #if defined(_WIN32) && !defined(__Userspace__)
6557 if (SCTP_BASE_SYSCTL(sctp_log) != NULL) {
6558 memset(SCTP_BASE_SYSCTL(sctp_log), 0, sizeof(struct sctp_log));
6561 memset(&SCTP_BASE_SYSCTL(sctp_log), 0, sizeof(struct sctp_log));
6564 #if defined(__FreeBSD__) && !defined(__Userspace__)
6565 #if defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
6566 SCTP_MALLOC(SCTP_BASE_STATS, struct sctpstat *,
6567 ((mp_maxid+1) * sizeof(struct sctpstat)),
6571 (void)SCTP_GETTIME_TIMEVAL(&tv);
6572 #if defined(__FreeBSD__) && !defined(__Userspace__)
6573 #if defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
6574 memset(SCTP_BASE_STATS, 0, sizeof(struct sctpstat) * (mp_maxid+1));
6575 SCTP_BASE_STATS[PCPU_GET(cpuid)].sctps_discontinuitytime.tv_sec = (uint32_t)tv.tv_sec;
6576 SCTP_BASE_STATS[PCPU_GET(cpuid)].sctps_discontinuitytime.tv_usec = (uint32_t)tv.tv_usec;
6578 memset(&SCTP_BASE_STATS, 0, sizeof(struct sctpstat));
6579 SCTP_BASE_STAT(sctps_discontinuitytime).tv_sec = (uint32_t)tv.tv_sec;
6580 SCTP_BASE_STAT(sctps_discontinuitytime).tv_usec = (uint32_t)tv.tv_usec;
6583 memset(&SCTP_BASE_STATS, 0, sizeof(struct sctpstat));
6584 SCTP_BASE_STAT(sctps_discontinuitytime).tv_sec = (uint32_t)tv.tv_sec;
6585 SCTP_BASE_STAT(sctps_discontinuitytime).tv_usec = (uint32_t)tv.tv_usec;
6587 /* init the empty list of (All) Endpoints */
6588 LIST_INIT(&SCTP_BASE_INFO(listhead));
6589 #if defined(__APPLE__) && !defined(__Userspace__)
6590 LIST_INIT(&SCTP_BASE_INFO(inplisthead));
6591 #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) || defined(APPLE_LION) || defined(APPLE_MOUNTAINLION)
6592 SCTP_BASE_INFO(sctbinfo).listhead = &SCTP_BASE_INFO(inplisthead);
6593 SCTP_BASE_INFO(sctbinfo).mtx_grp_attr = lck_grp_attr_alloc_init();
6594 lck_grp_attr_setdefault(SCTP_BASE_INFO(sctbinfo).mtx_grp_attr);
6595 SCTP_BASE_INFO(sctbinfo).mtx_grp = lck_grp_alloc_init("sctppcb", SCTP_BASE_INFO(sctbinfo).mtx_grp_attr);
6596 SCTP_BASE_INFO(sctbinfo).mtx_attr = lck_attr_alloc_init();
6597 lck_attr_setdefault(SCTP_BASE_INFO(sctbinfo).mtx_attr);
6599 SCTP_BASE_INFO(sctbinfo).ipi_listhead = &SCTP_BASE_INFO(inplisthead);
6600 SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr = lck_grp_attr_alloc_init();
6601 lck_grp_attr_setdefault(SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr);
6602 SCTP_BASE_INFO(sctbinfo).ipi_lock_grp = lck_grp_alloc_init("sctppcb", SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr);
6603 SCTP_BASE_INFO(sctbinfo).ipi_lock_attr = lck_attr_alloc_init();
6604 lck_attr_setdefault(SCTP_BASE_INFO(sctbinfo).ipi_lock_attr);
6606 #if !defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) && !defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION)
6607 SCTP_BASE_INFO(sctbinfo).ipi_gc = sctp_gc;
6608 in_pcbinfo_attach(&SCTP_BASE_INFO(sctbinfo));
6613 /* init the hash table of endpoints */
6614 #if defined(__FreeBSD__) && !defined(__Userspace__)
6615 TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", &SCTP_BASE_SYSCTL(sctp_hashtblsize));
6616 TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", &SCTP_BASE_SYSCTL(sctp_pcbtblsize));
6617 TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", &SCTP_BASE_SYSCTL(sctp_chunkscale));
6619 SCTP_BASE_INFO(sctp_asochash) = SCTP_HASH_INIT((SCTP_BASE_SYSCTL(sctp_hashtblsize) * 31),
6620 &SCTP_BASE_INFO(hashasocmark));
6621 SCTP_BASE_INFO(sctp_ephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize),
6622 &SCTP_BASE_INFO(hashmark));
6623 SCTP_BASE_INFO(sctp_tcpephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize),
6624 &SCTP_BASE_INFO(hashtcpmark));
6625 SCTP_BASE_INFO(hashtblsize) = SCTP_BASE_SYSCTL(sctp_hashtblsize);
6628 SCTP_BASE_INFO(sctp_vrfhash) = SCTP_HASH_INIT(SCTP_SIZE_OF_VRF_HASH,
6629 &SCTP_BASE_INFO(hashvrfmark));
6631 SCTP_BASE_INFO(vrf_ifn_hash) = SCTP_HASH_INIT(SCTP_VRF_IFN_HASH_SIZE,
6632 &SCTP_BASE_INFO(vrf_ifn_hashmark));
6633 /* init the zones */
6635 * FIX ME: Should check for NULL returns, but if it does fail we are
6636 * doomed to panic anyways... add later maybe.
6638 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_ep), "sctp_ep",
6639 sizeof(struct sctp_inpcb), maxsockets);
6641 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asoc), "sctp_asoc",
6642 sizeof(struct sctp_tcb), sctp_max_number_of_assoc);
6644 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_laddr), "sctp_laddr",
6645 sizeof(struct sctp_laddr),
6646 (sctp_max_number_of_assoc * sctp_scale_up_for_address));
6648 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_net), "sctp_raddr",
6649 sizeof(struct sctp_nets),
6650 (sctp_max_number_of_assoc * sctp_scale_up_for_address));
6652 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_chunk), "sctp_chunk",
6653 sizeof(struct sctp_tmit_chunk),
6654 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
6656 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_readq), "sctp_readq",
6657 sizeof(struct sctp_queued_to_read),
6658 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
6660 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_strmoq), "sctp_stream_msg_out",
6661 sizeof(struct sctp_stream_queue_pending),
6662 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
6664 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf), "sctp_asconf",
6665 sizeof(struct sctp_asconf),
6666 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
6668 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf_ack), "sctp_asconf_ack",
6669 sizeof(struct sctp_asconf_ack),
6670 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
6673 /* Master Lock INIT for info structure */
6674 SCTP_INP_INFO_LOCK_INIT();
6675 SCTP_STATLOG_INIT_LOCK();
6677 SCTP_IPI_COUNT_INIT();
6678 SCTP_IPI_ADDR_INIT();
6679 #ifdef SCTP_PACKET_LOGGING
6680 SCTP_IP_PKTLOG_INIT();
6682 LIST_INIT(&SCTP_BASE_INFO(addr_wq));
6684 SCTP_WQ_ADDR_INIT();
6685 /* not sure if we need all the counts */
6686 SCTP_BASE_INFO(ipi_count_ep) = 0;
6687 /* assoc/tcb zone info */
6688 SCTP_BASE_INFO(ipi_count_asoc) = 0;
6689 /* local addrlist zone info */
6690 SCTP_BASE_INFO(ipi_count_laddr) = 0;
6691 /* remote addrlist zone info */
6692 SCTP_BASE_INFO(ipi_count_raddr) = 0;
6694 SCTP_BASE_INFO(ipi_count_chunk) = 0;
6696 /* socket queue zone info */
6697 SCTP_BASE_INFO(ipi_count_readq) = 0;
6699 /* stream out queue cont */
6700 SCTP_BASE_INFO(ipi_count_strmoq) = 0;
6702 SCTP_BASE_INFO(ipi_free_strmoq) = 0;
6703 SCTP_BASE_INFO(ipi_free_chunks) = 0;
6705 SCTP_OS_TIMER_INIT(&SCTP_BASE_INFO(addr_wq_timer.timer));
6707 /* Init the TIMEWAIT list */
6708 for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) {
6709 LIST_INIT(&SCTP_BASE_INFO(vtag_timewait)[i]);
6711 #if defined(SCTP_PROCESS_LEVEL_LOCKS)
6713 InitializeConditionVariable(&sctp_it_ctl.iterator_wakeup);
6715 (void)pthread_cond_init(&sctp_it_ctl.iterator_wakeup, NULL);
6718 sctp_startup_iterator();
6720 #if defined(__FreeBSD__) && !defined(__Userspace__)
6721 #if defined(SCTP_MCORE_INPUT) && defined(SMP)
6722 sctp_startup_mcore_threads();
6727 * INIT the default VRF which for BSD is the only one, other O/S's
6728 * may have more. But initially they must start with one and then
6729 * add the VRF's as addresses are added.
6731 sctp_init_vrf_list(SCTP_DEFAULT_VRF);
6732 #if defined(__FreeBSD__) && !defined(__Userspace__) && defined(SCTP_NOT_YET)
6733 if (ip_register_flow_handler(sctp_netisr_hdlr, IPPROTO_SCTP)) {
6734 SCTP_PRINTF("***SCTP- Error can't register netisr handler***\n");
6737 #if defined(_SCTP_NEEDS_CALLOUT_) || defined(_USER_SCTP_NEEDS_CALLOUT_)
6738 /* allocate the lock for the callout/timer queue */
6739 SCTP_TIMERQ_LOCK_INIT();
6740 TAILQ_INIT(&SCTP_BASE_INFO(callqueue));
6742 #if defined(__Userspace__)
6743 mbuf_initialize(NULL);
6745 #if defined(INET) || defined(INET6)
6753 * Assumes that the SCTP_BASE_INFO() lock is NOT held.
6756 sctp_pcb_finish(void)
6758 struct sctp_vrflist *vrf_bucket;
6759 struct sctp_vrf *vrf, *nvrf;
6760 struct sctp_ifn *ifn, *nifn;
6761 struct sctp_ifa *ifa, *nifa;
6762 struct sctpvtaghead *chain;
6763 struct sctp_tagblock *twait_block, *prev_twait_block;
6764 struct sctp_laddr *wi, *nwi;
6766 struct sctp_iterator *it, *nit;
6768 if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
6769 SCTP_PRINTF("%s: race condition on teardown.\n", __func__);
6772 SCTP_BASE_VAR(sctp_pcb_initialized) = 0;
6773 #if !(defined(__FreeBSD__) && !defined(__Userspace__))
6774 /* Notify the iterator to exit. */
6775 SCTP_IPI_ITERATOR_WQ_LOCK();
6776 sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_MUST_EXIT;
6777 sctp_wakeup_iterator();
6778 SCTP_IPI_ITERATOR_WQ_UNLOCK();
6780 #if defined(__APPLE__) && !defined(__Userspace__)
6781 #if !defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) && !defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION)
6782 in_pcbinfo_detach(&SCTP_BASE_INFO(sctbinfo));
6784 SCTP_IPI_ITERATOR_WQ_LOCK();
6786 msleep(&sctp_it_ctl.iterator_flags,
6787 sctp_it_ctl.ipi_iterator_wq_mtx,
6788 0, "waiting_for_work", 0);
6789 } while ((sctp_it_ctl.iterator_flags & SCTP_ITERATOR_EXITED) == 0);
6790 thread_deallocate(sctp_it_ctl.thread_proc);
6791 SCTP_IPI_ITERATOR_WQ_UNLOCK();
6793 #if defined(_WIN32) && !defined(__Userspace__)
6794 if (sctp_it_ctl.iterator_thread_obj != NULL) {
6795 NTSTATUS status = STATUS_SUCCESS;
6797 KeSetEvent(&sctp_it_ctl.iterator_wakeup[1], IO_NO_INCREMENT, FALSE);
6798 status = KeWaitForSingleObject(sctp_it_ctl.iterator_thread_obj,
6803 ObDereferenceObject(sctp_it_ctl.iterator_thread_obj);
6806 #if defined(__Userspace__)
6807 if (SCTP_BASE_VAR(iterator_thread_started)) {
6809 WaitForSingleObject(sctp_it_ctl.thread_proc, INFINITE);
6810 CloseHandle(sctp_it_ctl.thread_proc);
6811 sctp_it_ctl.thread_proc = NULL;
6813 pthread_join(sctp_it_ctl.thread_proc, NULL);
6814 sctp_it_ctl.thread_proc = 0;
6818 #if defined(SCTP_PROCESS_LEVEL_LOCKS)
6820 DeleteConditionVariable(&sctp_it_ctl.iterator_wakeup);
6822 pthread_cond_destroy(&sctp_it_ctl.iterator_wakeup);
6823 pthread_mutexattr_destroy(&SCTP_BASE_VAR(mtx_attr));
6826 /* In FreeBSD the iterator thread never exits
6827 * but we do clean up.
6828 * The only way FreeBSD reaches here is if we have VRF's
6829 * but we still add the ifdef to make it compile on old versions.
6831 #if defined(__FreeBSD__) && !defined(__Userspace__)
6834 SCTP_IPI_ITERATOR_WQ_LOCK();
6835 #if defined(__FreeBSD__) && !defined(__Userspace__)
6837 * sctp_iterator_worker() might be working on an it entry without
6838 * holding the lock. We won't find it on the list either and
6839 * continue and free/destroy it. While holding the lock, spin, to
6840 * avoid the race condition as sctp_iterator_worker() will have to
6841 * wait to re-acquire the lock.
6843 if (sctp_it_ctl.iterator_running != 0 || sctp_it_ctl.cur_it != NULL) {
6844 SCTP_IPI_ITERATOR_WQ_UNLOCK();
6845 SCTP_PRINTF("%s: Iterator running while we held the lock. Retry. "
6846 "cur_it=%p\n", __func__, sctp_it_ctl.cur_it);
6851 TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) {
6852 #if defined(__FreeBSD__) && !defined(__Userspace__)
6853 if (it->vn != curvnet) {
6857 TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
6858 if (it->function_atend != NULL) {
6859 (*it->function_atend) (it->pointer, it->val);
6861 SCTP_FREE(it,SCTP_M_ITER);
6863 SCTP_IPI_ITERATOR_WQ_UNLOCK();
6864 #if defined(__FreeBSD__) && !defined(__Userspace__)
6865 SCTP_ITERATOR_LOCK();
6866 if ((sctp_it_ctl.cur_it) &&
6867 (sctp_it_ctl.cur_it->vn == curvnet)) {
6868 sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_IT;
6870 SCTP_ITERATOR_UNLOCK();
6872 #if !(defined(__FreeBSD__) && !defined(__Userspace__))
6873 SCTP_IPI_ITERATOR_WQ_DESTROY();
6874 SCTP_ITERATOR_LOCK_DESTROY();
6876 SCTP_OS_TIMER_STOP_DRAIN(&SCTP_BASE_INFO(addr_wq_timer.timer));
6877 SCTP_WQ_ADDR_LOCK();
6878 LIST_FOREACH_SAFE(wi, &SCTP_BASE_INFO(addr_wq), sctp_nxt_addr, nwi) {
6879 LIST_REMOVE(wi, sctp_nxt_addr);
6880 SCTP_DECR_LADDR_COUNT();
6881 if (wi->action == SCTP_DEL_IP_ADDRESS) {
6882 SCTP_FREE(wi->ifa, SCTP_M_IFA);
6884 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), wi);
6886 SCTP_WQ_ADDR_UNLOCK();
6889 * free the vrf/ifn/ifa lists and hashes (be sure address monitor
6890 * is destroyed first).
6892 SCTP_IPI_ADDR_WLOCK();
6893 vrf_bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(SCTP_DEFAULT_VRFID & SCTP_BASE_INFO(hashvrfmark))];
6894 LIST_FOREACH_SAFE(vrf, vrf_bucket, next_vrf, nvrf) {
6895 LIST_FOREACH_SAFE(ifn, &vrf->ifnlist, next_ifn, nifn) {
6896 LIST_FOREACH_SAFE(ifa, &ifn->ifalist, next_ifa, nifa) {
6898 LIST_REMOVE(ifa, next_bucket);
6899 LIST_REMOVE(ifa, next_ifa);
6900 SCTP_FREE(ifa, SCTP_M_IFA);
6903 LIST_REMOVE(ifn, next_bucket);
6904 LIST_REMOVE(ifn, next_ifn);
6905 SCTP_FREE(ifn, SCTP_M_IFN);
6907 SCTP_HASH_FREE(vrf->vrf_addr_hash, vrf->vrf_addr_hashmark);
6909 LIST_REMOVE(vrf, next_vrf);
6910 SCTP_FREE(vrf, SCTP_M_VRF);
6912 SCTP_IPI_ADDR_WUNLOCK();
6913 /* free the vrf hashes */
6914 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_vrfhash), SCTP_BASE_INFO(hashvrfmark));
6915 SCTP_HASH_FREE(SCTP_BASE_INFO(vrf_ifn_hash), SCTP_BASE_INFO(vrf_ifn_hashmark));
6917 /* free the TIMEWAIT list elements malloc'd in the function
6918 * sctp_add_vtag_to_timewait()...
6920 for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) {
6921 chain = &SCTP_BASE_INFO(vtag_timewait)[i];
6922 if (!LIST_EMPTY(chain)) {
6923 prev_twait_block = NULL;
6924 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
6925 if (prev_twait_block) {
6926 SCTP_FREE(prev_twait_block, SCTP_M_TIMW);
6928 prev_twait_block = twait_block;
6930 SCTP_FREE(prev_twait_block, SCTP_M_TIMW);
6934 /* free the locks and mutexes */
6935 #if defined(__APPLE__) && !defined(__Userspace__)
6936 SCTP_TIMERQ_LOCK_DESTROY();
6938 #ifdef SCTP_PACKET_LOGGING
6939 SCTP_IP_PKTLOG_DESTROY();
6941 SCTP_IPI_ADDR_DESTROY();
6942 #if defined(__APPLE__) && !defined(__Userspace__)
6943 SCTP_IPI_COUNT_DESTROY();
6945 SCTP_STATLOG_DESTROY();
6946 SCTP_INP_INFO_LOCK_DESTROY();
6948 SCTP_WQ_ADDR_DESTROY();
6950 #if defined(__APPLE__) && !defined(__Userspace__)
6951 #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) || defined(APPLE_LION) || defined(APPLE_MOUNTAINLION)
6952 lck_grp_attr_free(SCTP_BASE_INFO(sctbinfo).mtx_grp_attr);
6953 lck_grp_free(SCTP_BASE_INFO(sctbinfo).mtx_grp);
6954 lck_attr_free(SCTP_BASE_INFO(sctbinfo).mtx_attr);
6956 lck_grp_attr_free(SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr);
6957 lck_grp_free(SCTP_BASE_INFO(sctbinfo).ipi_lock_grp);
6958 lck_attr_free(SCTP_BASE_INFO(sctbinfo).ipi_lock_attr);
6961 #if defined(__Userspace__)
6962 SCTP_TIMERQ_LOCK_DESTROY();
6963 SCTP_ZONE_DESTROY(zone_mbuf);
6964 SCTP_ZONE_DESTROY(zone_clust);
6965 SCTP_ZONE_DESTROY(zone_ext_refcnt);
6967 /* Get rid of other stuff too. */
6968 if (SCTP_BASE_INFO(sctp_asochash) != NULL)
6969 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_asochash), SCTP_BASE_INFO(hashasocmark));
6970 if (SCTP_BASE_INFO(sctp_ephash) != NULL)
6971 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_ephash), SCTP_BASE_INFO(hashmark));
6972 if (SCTP_BASE_INFO(sctp_tcpephash) != NULL)
6973 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_tcpephash), SCTP_BASE_INFO(hashtcpmark));
6975 #if defined(_WIN32) || defined(__FreeBSD__) || defined(__Userspace__)
6976 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_ep));
6977 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asoc));
6978 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_laddr));
6979 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_net));
6980 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_chunk));
6981 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_readq));
6982 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_strmoq));
6983 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf));
6984 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf_ack));
6986 #if defined(__FreeBSD__) && !defined(__Userspace__)
6987 #if defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
6988 SCTP_FREE(SCTP_BASE_STATS, SCTP_M_MCORE);
6995 sctp_load_addresses_from_init(struct sctp_tcb *stcb, struct mbuf *m,
6996 int offset, int limit,
6997 struct sockaddr *src, struct sockaddr *dst,
6998 struct sockaddr *altsa, uint16_t port)
7001 * grub through the INIT pulling addresses and loading them to the
7002 * nets structure in the asoc. The from address in the mbuf should
7003 * also be loaded (if it is not already). This routine can be called
7004 * with either INIT or INIT-ACK's as long as the m points to the IP
7005 * packet and the offset points to the beginning of the parameters.
7007 struct sctp_inpcb *inp;
7008 struct sctp_nets *net, *nnet, *net_tmp;
7009 struct sctp_paramhdr *phdr, param_buf;
7010 struct sctp_tcb *stcb_tmp;
7011 uint16_t ptype, plen;
7012 struct sockaddr *sa;
7013 uint8_t random_store[SCTP_PARAM_BUFFER_SIZE];
7014 struct sctp_auth_random *p_random = NULL;
7015 uint16_t random_len = 0;
7016 uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE];
7017 struct sctp_auth_hmac_algo *hmacs = NULL;
7018 uint16_t hmacs_len = 0;
7019 uint8_t saw_asconf = 0;
7020 uint8_t saw_asconf_ack = 0;
7021 uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE];
7022 struct sctp_auth_chunk_list *chunks = NULL;
7023 uint16_t num_chunks = 0;
7024 sctp_key_t *new_key;
7026 int got_random = 0, got_hmacs = 0, got_chklist = 0;
7027 uint8_t peer_supports_ecn;
7028 uint8_t peer_supports_prsctp;
7029 uint8_t peer_supports_auth;
7030 uint8_t peer_supports_asconf;
7031 uint8_t peer_supports_asconf_ack;
7032 uint8_t peer_supports_reconfig;
7033 uint8_t peer_supports_nrsack;
7034 uint8_t peer_supports_pktdrop;
7035 uint8_t peer_supports_idata;
7037 struct sockaddr_in sin;
7040 struct sockaddr_in6 sin6;
7043 /* First get the destination address setup too. */
7045 memset(&sin, 0, sizeof(sin));
7046 sin.sin_family = AF_INET;
7048 sin.sin_len = sizeof(sin);
7050 sin.sin_port = stcb->rport;
7053 memset(&sin6, 0, sizeof(sin6));
7054 sin6.sin6_family = AF_INET6;
7055 #ifdef HAVE_SIN6_LEN
7056 sin6.sin6_len = sizeof(struct sockaddr_in6);
7058 sin6.sin6_port = stcb->rport;
7065 peer_supports_idata = 0;
7066 peer_supports_ecn = 0;
7067 peer_supports_prsctp = 0;
7068 peer_supports_auth = 0;
7069 peer_supports_asconf = 0;
7070 peer_supports_reconfig = 0;
7071 peer_supports_nrsack = 0;
7072 peer_supports_pktdrop = 0;
7073 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
7074 /* mark all addresses that we have currently on the list */
7075 net->dest_state |= SCTP_ADDR_NOT_IN_ASSOC;
7077 /* does the source address already exist? if so skip it */
7078 inp = stcb->sctp_ep;
7079 atomic_add_int(&stcb->asoc.refcnt, 1);
7080 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net_tmp, dst, stcb);
7081 atomic_add_int(&stcb->asoc.refcnt, -1);
7083 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || inp == NULL) {
7084 /* we must add the source address */
7085 /* no scope set here since we have a tcb already. */
7086 switch (sa->sa_family) {
7089 if (stcb->asoc.scope.ipv4_addr_legal) {
7090 if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_2)) {
7098 if (stcb->asoc.scope.ipv6_addr_legal) {
7099 if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) {
7105 #if defined(__Userspace__)
7107 if (stcb->asoc.scope.conn_addr_legal) {
7108 if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) {
7118 if (net_tmp != NULL && stcb_tmp == stcb) {
7119 net_tmp->dest_state &= ~SCTP_ADDR_NOT_IN_ASSOC;
7120 } else if (stcb_tmp != stcb) {
7121 /* It belongs to another association? */
7123 SCTP_TCB_UNLOCK(stcb_tmp);
7127 if (stcb->asoc.state == 0) {
7128 /* the assoc was freed? */
7131 /* now we must go through each of the params. */
7132 phdr = sctp_get_next_param(m, offset, ¶m_buf, sizeof(param_buf));
7134 ptype = ntohs(phdr->param_type);
7135 plen = ntohs(phdr->param_length);
7137 * SCTP_PRINTF("ptype => %0x, plen => %d\n", (uint32_t)ptype,
7140 if (offset + plen > limit) {
7143 if (plen < sizeof(struct sctp_paramhdr)) {
7147 if (ptype == SCTP_IPV4_ADDRESS) {
7148 if (stcb->asoc.scope.ipv4_addr_legal) {
7149 struct sctp_ipv4addr_param *p4, p4_buf;
7151 /* ok get the v4 address and check/add */
7152 phdr = sctp_get_next_param(m, offset,
7153 (struct sctp_paramhdr *)&p4_buf,
7155 if (plen != sizeof(struct sctp_ipv4addr_param) ||
7159 p4 = (struct sctp_ipv4addr_param *)phdr;
7160 sin.sin_addr.s_addr = p4->addr;
7161 if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
7162 /* Skip multi-cast addresses */
7165 if ((sin.sin_addr.s_addr == INADDR_BROADCAST) ||
7166 (sin.sin_addr.s_addr == INADDR_ANY)) {
7169 sa = (struct sockaddr *)&sin;
7170 inp = stcb->sctp_ep;
7171 atomic_add_int(&stcb->asoc.refcnt, 1);
7172 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
7174 atomic_add_int(&stcb->asoc.refcnt, -1);
7176 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) ||
7178 /* we must add the source address */
7180 * no scope set since we have a tcb
7185 * we must validate the state again
7189 if (stcb->asoc.state == 0) {
7190 /* the assoc was freed? */
7193 if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_4)) {
7196 } else if (stcb_tmp == stcb) {
7197 if (stcb->asoc.state == 0) {
7198 /* the assoc was freed? */
7204 ~SCTP_ADDR_NOT_IN_ASSOC;
7208 * strange, address is in another
7209 * assoc? straighten out locks.
7212 if (SCTP_GET_STATE(stcb_tmp) == SCTP_STATE_COOKIE_WAIT) {
7213 struct mbuf *op_err;
7214 char msg[SCTP_DIAG_INFO_LEN];
7216 /* in setup state we abort this guy */
7217 SCTP_SNPRINTF(msg, sizeof(msg),
7218 "%s:%d at %s", __FILE__, __LINE__, __func__);
7219 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
7221 sctp_abort_an_association(stcb_tmp->sctp_ep,
7223 SCTP_SO_NOT_LOCKED);
7226 SCTP_TCB_UNLOCK(stcb_tmp);
7229 if (stcb->asoc.state == 0) {
7230 /* the assoc was freed? */
7239 if (ptype == SCTP_IPV6_ADDRESS) {
7240 if (stcb->asoc.scope.ipv6_addr_legal) {
7241 /* ok get the v6 address and check/add */
7242 struct sctp_ipv6addr_param *p6, p6_buf;
7244 phdr = sctp_get_next_param(m, offset,
7245 (struct sctp_paramhdr *)&p6_buf,
7247 if (plen != sizeof(struct sctp_ipv6addr_param) ||
7251 p6 = (struct sctp_ipv6addr_param *)phdr;
7252 memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
7254 if (IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) {
7255 /* Skip multi-cast addresses */
7258 if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
7259 /* Link local make no sense without scope */
7262 sa = (struct sockaddr *)&sin6;
7263 inp = stcb->sctp_ep;
7264 atomic_add_int(&stcb->asoc.refcnt, 1);
7265 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
7267 atomic_add_int(&stcb->asoc.refcnt, -1);
7268 if (stcb_tmp == NULL &&
7269 (inp == stcb->sctp_ep || inp == NULL)) {
7271 * we must validate the state again
7275 if (stcb->asoc.state == 0) {
7276 /* the assoc was freed? */
7280 * we must add the address, no scope
7283 if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_5)) {
7286 } else if (stcb_tmp == stcb) {
7288 * we must validate the state again
7291 if (stcb->asoc.state == 0) {
7292 /* the assoc was freed? */
7298 ~SCTP_ADDR_NOT_IN_ASSOC;
7302 * strange, address is in another
7303 * assoc? straighten out locks.
7306 if (SCTP_GET_STATE(stcb_tmp) == SCTP_STATE_COOKIE_WAIT) {
7307 struct mbuf *op_err;
7308 char msg[SCTP_DIAG_INFO_LEN];
7310 /* in setup state we abort this guy */
7311 SCTP_SNPRINTF(msg, sizeof(msg),
7312 "%s:%d at %s", __FILE__, __LINE__, __func__);
7313 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
7315 sctp_abort_an_association(stcb_tmp->sctp_ep,
7317 SCTP_SO_NOT_LOCKED);
7320 SCTP_TCB_UNLOCK(stcb_tmp);
7322 if (stcb->asoc.state == 0) {
7323 /* the assoc was freed? */
7331 if (ptype == SCTP_ECN_CAPABLE) {
7332 peer_supports_ecn = 1;
7333 } else if (ptype == SCTP_ULP_ADAPTATION) {
7334 if (stcb->asoc.state != SCTP_STATE_OPEN) {
7335 struct sctp_adaptation_layer_indication ai, *aip;
7337 phdr = sctp_get_next_param(m, offset,
7338 (struct sctp_paramhdr *)&ai, sizeof(ai));
7339 aip = (struct sctp_adaptation_layer_indication *)phdr;
7341 stcb->asoc.peers_adaptation = ntohl(aip->indication);
7342 stcb->asoc.adaptation_needed = 1;
7345 } else if (ptype == SCTP_SET_PRIM_ADDR) {
7346 struct sctp_asconf_addr_param lstore, *fee;
7348 struct sockaddr *lsa = NULL;
7350 struct sctp_asconf_addrv4_param *fii;
7353 if (stcb->asoc.asconf_supported == 0) {
7356 if (plen > sizeof(lstore)) {
7359 if (plen < sizeof(struct sctp_asconf_addrv4_param)) {
7362 phdr = sctp_get_next_param(m, offset,
7363 (struct sctp_paramhdr *)&lstore,
7368 fee = (struct sctp_asconf_addr_param *)phdr;
7369 lptype = ntohs(fee->addrp.ph.param_type);
7372 case SCTP_IPV4_ADDRESS:
7374 sizeof(struct sctp_asconf_addrv4_param)) {
7375 SCTP_PRINTF("Sizeof setprim in init/init ack not %d but %d - ignored\n",
7376 (int)sizeof(struct sctp_asconf_addrv4_param),
7379 fii = (struct sctp_asconf_addrv4_param *)fee;
7380 sin.sin_addr.s_addr = fii->addrp.addr;
7381 lsa = (struct sockaddr *)&sin;
7386 case SCTP_IPV6_ADDRESS:
7388 sizeof(struct sctp_asconf_addr_param)) {
7389 SCTP_PRINTF("Sizeof setprim (v6) in init/init ack not %d but %d - ignored\n",
7390 (int)sizeof(struct sctp_asconf_addr_param),
7393 memcpy(sin6.sin6_addr.s6_addr,
7395 sizeof(fee->addrp.addr));
7396 lsa = (struct sockaddr *)&sin6;
7404 (void)sctp_set_primary_addr(stcb, sa, NULL);
7406 } else if (ptype == SCTP_HAS_NAT_SUPPORT) {
7407 stcb->asoc.peer_supports_nat = 1;
7408 } else if (ptype == SCTP_PRSCTP_SUPPORTED) {
7409 /* Peer supports pr-sctp */
7410 peer_supports_prsctp = 1;
7411 } else if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
7412 /* A supported extension chunk */
7413 struct sctp_supported_chunk_types_param *pr_supported;
7414 uint8_t local_store[SCTP_PARAM_BUFFER_SIZE];
7417 if (plen > sizeof(local_store)) {
7420 phdr = sctp_get_next_param(m, offset,
7421 (struct sctp_paramhdr *)&local_store, plen);
7425 pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
7426 num_ent = plen - sizeof(struct sctp_paramhdr);
7427 for (i = 0; i < num_ent; i++) {
7428 switch (pr_supported->chunk_types[i]) {
7430 peer_supports_asconf = 1;
7432 case SCTP_ASCONF_ACK:
7433 peer_supports_asconf_ack = 1;
7435 case SCTP_FORWARD_CUM_TSN:
7436 peer_supports_prsctp = 1;
7438 case SCTP_PACKET_DROPPED:
7439 peer_supports_pktdrop = 1;
7441 case SCTP_NR_SELECTIVE_ACK:
7442 peer_supports_nrsack = 1;
7444 case SCTP_STREAM_RESET:
7445 peer_supports_reconfig = 1;
7447 case SCTP_AUTHENTICATION:
7448 peer_supports_auth = 1;
7451 peer_supports_idata = 1;
7454 /* one I have not learned yet */
7459 } else if (ptype == SCTP_RANDOM) {
7460 if (plen > sizeof(random_store))
7463 /* already processed a RANDOM */
7466 phdr = sctp_get_next_param(m, offset,
7467 (struct sctp_paramhdr *)random_store,
7471 p_random = (struct sctp_auth_random *)phdr;
7472 random_len = plen - sizeof(*p_random);
7473 /* enforce the random length */
7474 if (random_len != SCTP_AUTH_RANDOM_SIZE_REQUIRED) {
7475 SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: invalid RANDOM len\n");
7479 } else if (ptype == SCTP_HMAC_LIST) {
7483 if (plen > sizeof(hmacs_store))
7486 /* already processed a HMAC list */
7489 phdr = sctp_get_next_param(m, offset,
7490 (struct sctp_paramhdr *)hmacs_store,
7494 hmacs = (struct sctp_auth_hmac_algo *)phdr;
7495 hmacs_len = plen - sizeof(*hmacs);
7496 num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]);
7497 /* validate the hmac list */
7498 if (sctp_verify_hmac_param(hmacs, num_hmacs)) {
7501 if (stcb->asoc.peer_hmacs != NULL)
7502 sctp_free_hmaclist(stcb->asoc.peer_hmacs);
7503 stcb->asoc.peer_hmacs = sctp_alloc_hmaclist(num_hmacs);
7504 if (stcb->asoc.peer_hmacs != NULL) {
7505 for (i = 0; i < num_hmacs; i++) {
7506 (void)sctp_auth_add_hmacid(stcb->asoc.peer_hmacs,
7507 ntohs(hmacs->hmac_ids[i]));
7511 } else if (ptype == SCTP_CHUNK_LIST) {
7514 if (plen > sizeof(chunks_store))
7517 /* already processed a Chunks list */
7520 phdr = sctp_get_next_param(m, offset,
7521 (struct sctp_paramhdr *)chunks_store,
7525 chunks = (struct sctp_auth_chunk_list *)phdr;
7526 num_chunks = plen - sizeof(*chunks);
7527 if (stcb->asoc.peer_auth_chunks != NULL)
7528 sctp_clear_chunklist(stcb->asoc.peer_auth_chunks);
7530 stcb->asoc.peer_auth_chunks = sctp_alloc_chunklist();
7531 for (i = 0; i < num_chunks; i++) {
7532 (void)sctp_auth_add_chunk(chunks->chunk_types[i],
7533 stcb->asoc.peer_auth_chunks);
7534 /* record asconf/asconf-ack if listed */
7535 if (chunks->chunk_types[i] == SCTP_ASCONF)
7537 if (chunks->chunk_types[i] == SCTP_ASCONF_ACK)
7542 } else if ((ptype == SCTP_HEARTBEAT_INFO) ||
7543 (ptype == SCTP_STATE_COOKIE) ||
7544 (ptype == SCTP_UNRECOG_PARAM) ||
7545 (ptype == SCTP_COOKIE_PRESERVE) ||
7546 (ptype == SCTP_SUPPORTED_ADDRTYPE) ||
7547 (ptype == SCTP_ADD_IP_ADDRESS) ||
7548 (ptype == SCTP_DEL_IP_ADDRESS) ||
7549 (ptype == SCTP_ERROR_CAUSE_IND) ||
7550 (ptype == SCTP_SUCCESS_REPORT)) {
7553 if ((ptype & 0x8000) == 0x0000) {
7555 * must stop processing the rest of the
7556 * param's. Any report bits were handled
7558 * sctp_arethere_unrecognized_parameters()
7559 * when the INIT or INIT-ACK was first seen.
7566 offset += SCTP_SIZE32(plen);
7567 if (offset >= limit) {
7570 phdr = sctp_get_next_param(m, offset, ¶m_buf,
7573 /* Now check to see if we need to purge any addresses */
7574 TAILQ_FOREACH_SAFE(net, &stcb->asoc.nets, sctp_next, nnet) {
7575 if ((net->dest_state & SCTP_ADDR_NOT_IN_ASSOC) ==
7576 SCTP_ADDR_NOT_IN_ASSOC) {
7577 /* This address has been removed from the asoc */
7578 /* remove and free it */
7579 stcb->asoc.numnets--;
7580 TAILQ_REMOVE(&stcb->asoc.nets, net, sctp_next);
7581 sctp_free_remote_addr(net);
7582 if (net == stcb->asoc.primary_destination) {
7583 stcb->asoc.primary_destination = NULL;
7584 sctp_select_primary_destination(stcb);
7588 if ((stcb->asoc.ecn_supported == 1) &&
7589 (peer_supports_ecn == 0)) {
7590 stcb->asoc.ecn_supported = 0;
7592 if ((stcb->asoc.prsctp_supported == 1) &&
7593 (peer_supports_prsctp == 0)) {
7594 stcb->asoc.prsctp_supported = 0;
7596 if ((stcb->asoc.auth_supported == 1) &&
7597 ((peer_supports_auth == 0) ||
7598 (got_random == 0) || (got_hmacs == 0))) {
7599 stcb->asoc.auth_supported = 0;
7601 if ((stcb->asoc.asconf_supported == 1) &&
7602 ((peer_supports_asconf == 0) || (peer_supports_asconf_ack == 0) ||
7603 (stcb->asoc.auth_supported == 0) ||
7604 (saw_asconf == 0) || (saw_asconf_ack == 0))) {
7605 stcb->asoc.asconf_supported = 0;
7607 if ((stcb->asoc.reconfig_supported == 1) &&
7608 (peer_supports_reconfig == 0)) {
7609 stcb->asoc.reconfig_supported = 0;
7611 if ((stcb->asoc.idata_supported == 1) &&
7612 (peer_supports_idata == 0)) {
7613 stcb->asoc.idata_supported = 0;
7615 if ((stcb->asoc.nrsack_supported == 1) &&
7616 (peer_supports_nrsack == 0)) {
7617 stcb->asoc.nrsack_supported = 0;
7619 if ((stcb->asoc.pktdrop_supported == 1) &&
7620 (peer_supports_pktdrop == 0)){
7621 stcb->asoc.pktdrop_supported = 0;
7623 /* validate authentication required parameters */
7624 if ((peer_supports_auth == 0) && (got_chklist == 1)) {
7625 /* peer does not support auth but sent a chunks list? */
7628 if ((peer_supports_asconf == 1) && (peer_supports_auth == 0)) {
7629 /* peer supports asconf but not auth? */
7631 } else if ((peer_supports_asconf == 1) &&
7632 (peer_supports_auth == 1) &&
7633 ((saw_asconf == 0) || (saw_asconf_ack == 0))) {
7636 /* concatenate the full random key */
7637 keylen = sizeof(*p_random) + random_len + sizeof(*hmacs) + hmacs_len;
7638 if (chunks != NULL) {
7639 keylen += sizeof(*chunks) + num_chunks;
7641 new_key = sctp_alloc_key(keylen);
7642 if (new_key != NULL) {
7643 /* copy in the RANDOM */
7644 if (p_random != NULL) {
7645 keylen = sizeof(*p_random) + random_len;
7646 memcpy(new_key->key, p_random, keylen);
7650 /* append in the AUTH chunks */
7651 if (chunks != NULL) {
7652 memcpy(new_key->key + keylen, chunks,
7653 sizeof(*chunks) + num_chunks);
7654 keylen += sizeof(*chunks) + num_chunks;
7656 /* append in the HMACs */
7657 if (hmacs != NULL) {
7658 memcpy(new_key->key + keylen, hmacs,
7659 sizeof(*hmacs) + hmacs_len);
7662 /* failed to get memory for the key */
7665 if (stcb->asoc.authinfo.peer_random != NULL)
7666 sctp_free_key(stcb->asoc.authinfo.peer_random);
7667 stcb->asoc.authinfo.peer_random = new_key;
7668 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid);
7669 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid);
7675 sctp_set_primary_addr(struct sctp_tcb *stcb, struct sockaddr *sa,
7676 struct sctp_nets *net)
7678 /* make sure the requested primary address exists in the assoc */
7679 if (net == NULL && sa)
7680 net = sctp_findnet(stcb, sa);
7683 /* didn't find the requested primary address! */
7686 /* set the primary address */
7687 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
7688 /* Must be confirmed, so queue to set */
7689 net->dest_state |= SCTP_ADDR_REQ_PRIMARY;
7692 stcb->asoc.primary_destination = net;
7693 if (!(net->dest_state & SCTP_ADDR_PF) && (stcb->asoc.alternate)) {
7694 sctp_free_remote_addr(stcb->asoc.alternate);
7695 stcb->asoc.alternate = NULL;
7697 net = TAILQ_FIRST(&stcb->asoc.nets);
7698 if (net != stcb->asoc.primary_destination) {
7699 /* first one on the list is NOT the primary
7700 * sctp_cmpaddr() is much more efficient if
7701 * the primary is the first on the list, make it
7704 TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
7705 TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
7712 sctp_is_vtag_good(uint32_t tag, uint16_t lport, uint16_t rport, struct timeval *now)
7715 * This function serves two purposes. It will see if a TAG can be
7716 * re-used and return 1 for yes it is ok and 0 for don't use that
7717 * tag. A secondary function it will do is purge out old tags that
7720 struct sctpvtaghead *chain;
7721 struct sctp_tagblock *twait_block;
7722 struct sctpasochead *head;
7723 struct sctp_tcb *stcb;
7726 SCTP_INP_INFO_RLOCK();
7727 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
7728 SCTP_BASE_INFO(hashasocmark))];
7729 LIST_FOREACH(stcb, head, sctp_asocs) {
7730 /* We choose not to lock anything here. TCB's can't be
7731 * removed since we have the read lock, so they can't
7732 * be freed on us, same thing for the INP. I may
7733 * be wrong with this assumption, but we will go
7734 * with it for now :-)
7736 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
7739 if (stcb->asoc.my_vtag == tag) {
7741 if (stcb->rport != rport) {
7744 if (stcb->sctp_ep->sctp_lport != lport) {
7747 /* Its a used tag set */
7748 SCTP_INP_INFO_RUNLOCK();
7752 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
7753 /* Now what about timed wait ? */
7754 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
7756 * Block(s) are present, lets see if we have this tag in the
7759 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
7760 if (twait_block->vtag_block[i].v_tag == 0) {
7763 } else if ((long)twait_block->vtag_block[i].tv_sec_at_expire <
7765 /* Audit expires this guy */
7766 twait_block->vtag_block[i].tv_sec_at_expire = 0;
7767 twait_block->vtag_block[i].v_tag = 0;
7768 twait_block->vtag_block[i].lport = 0;
7769 twait_block->vtag_block[i].rport = 0;
7770 } else if ((twait_block->vtag_block[i].v_tag == tag) &&
7771 (twait_block->vtag_block[i].lport == lport) &&
7772 (twait_block->vtag_block[i].rport == rport)) {
7773 /* Bad tag, sorry :< */
7774 SCTP_INP_INFO_RUNLOCK();
7779 SCTP_INP_INFO_RUNLOCK();
7784 sctp_drain_mbufs(struct sctp_tcb *stcb)
7787 * We must hunt this association for MBUF's past the cumack (i.e.
7788 * out of order data that we can renege on).
7790 struct sctp_association *asoc;
7791 struct sctp_tmit_chunk *chk, *nchk;
7792 uint32_t cumulative_tsn_p1;
7793 struct sctp_queued_to_read *control, *ncontrol;
7798 /* We look for anything larger than the cum-ack + 1 */
7801 if (asoc->cumulative_tsn == asoc->highest_tsn_inside_map) {
7802 /* none we can reneg on. */
7805 SCTP_STAT_INCR(sctps_protocol_drains_done);
7806 cumulative_tsn_p1 = asoc->cumulative_tsn + 1;
7808 /* Ok that was fun, now we will drain all the inbound streams? */
7809 for (strmat = 0; strmat < asoc->streamincnt; strmat++) {
7810 TAILQ_FOREACH_SAFE(control, &asoc->strmin[strmat].inqueue, next_instrm, ncontrol) {
7812 if (control->on_strm_q != SCTP_ON_ORDERED ) {
7813 panic("Huh control: %p on_q: %d -- not ordered?",
7814 control, control->on_strm_q);
7817 if (SCTP_TSN_GT(control->sinfo_tsn, cumulative_tsn_p1)) {
7818 /* Yep it is above cum-ack */
7820 SCTP_CALC_TSN_TO_GAP(gap, control->sinfo_tsn, asoc->mapping_array_base_tsn);
7821 KASSERT(control->length > 0, ("control has zero length"));
7822 if (asoc->size_on_all_streams >= control->length) {
7823 asoc->size_on_all_streams -= control->length;
7826 panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length);
7828 asoc->size_on_all_streams = 0;
7831 sctp_ucount_decr(asoc->cnt_on_all_streams);
7832 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
7833 if (control->on_read_q) {
7834 TAILQ_REMOVE(&stcb->sctp_ep->read_queue, control, next);
7835 control->on_read_q = 0;
7837 TAILQ_REMOVE(&asoc->strmin[strmat].inqueue, control, next_instrm);
7838 control->on_strm_q = 0;
7839 if (control->data) {
7840 sctp_m_freem(control->data);
7841 control->data = NULL;
7843 sctp_free_remote_addr(control->whoFrom);
7844 /* Now its reasm? */
7845 TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) {
7847 SCTP_CALC_TSN_TO_GAP(gap, chk->rec.data.tsn, asoc->mapping_array_base_tsn);
7848 KASSERT(chk->send_size > 0, ("chunk has zero length"));
7849 if (asoc->size_on_reasm_queue >= chk->send_size) {
7850 asoc->size_on_reasm_queue -= chk->send_size;
7853 panic("size_on_reasm_queue = %u smaller than chunk length %u", asoc->size_on_reasm_queue, chk->send_size);
7855 asoc->size_on_reasm_queue = 0;
7858 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
7859 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
7860 TAILQ_REMOVE(&control->reasm, chk, sctp_next);
7862 sctp_m_freem(chk->data);
7865 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
7867 sctp_free_a_readq(stcb, control);
7870 TAILQ_FOREACH_SAFE(control, &asoc->strmin[strmat].uno_inqueue, next_instrm, ncontrol) {
7872 if (control->on_strm_q != SCTP_ON_UNORDERED ) {
7873 panic("Huh control: %p on_q: %d -- not unordered?",
7874 control, control->on_strm_q);
7877 if (SCTP_TSN_GT(control->sinfo_tsn, cumulative_tsn_p1)) {
7878 /* Yep it is above cum-ack */
7880 SCTP_CALC_TSN_TO_GAP(gap, control->sinfo_tsn, asoc->mapping_array_base_tsn);
7881 KASSERT(control->length > 0, ("control has zero length"));
7882 if (asoc->size_on_all_streams >= control->length) {
7883 asoc->size_on_all_streams -= control->length;
7886 panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length);
7888 asoc->size_on_all_streams = 0;
7891 sctp_ucount_decr(asoc->cnt_on_all_streams);
7892 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
7893 if (control->on_read_q) {
7894 TAILQ_REMOVE(&stcb->sctp_ep->read_queue, control, next);
7895 control->on_read_q = 0;
7897 TAILQ_REMOVE(&asoc->strmin[strmat].uno_inqueue, control, next_instrm);
7898 control->on_strm_q = 0;
7899 if (control->data) {
7900 sctp_m_freem(control->data);
7901 control->data = NULL;
7903 sctp_free_remote_addr(control->whoFrom);
7904 /* Now its reasm? */
7905 TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) {
7907 SCTP_CALC_TSN_TO_GAP(gap, chk->rec.data.tsn, asoc->mapping_array_base_tsn);
7908 KASSERT(chk->send_size > 0, ("chunk has zero length"));
7909 if (asoc->size_on_reasm_queue >= chk->send_size) {
7910 asoc->size_on_reasm_queue -= chk->send_size;
7913 panic("size_on_reasm_queue = %u smaller than chunk length %u", asoc->size_on_reasm_queue, chk->send_size);
7915 asoc->size_on_reasm_queue = 0;
7918 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
7919 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
7920 TAILQ_REMOVE(&control->reasm, chk, sctp_next);
7922 sctp_m_freem(chk->data);
7925 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
7927 sctp_free_a_readq(stcb, control);
7932 /* We must back down to see what the new highest is */
7933 for (i = asoc->highest_tsn_inside_map; SCTP_TSN_GE(i, asoc->mapping_array_base_tsn); i--) {
7934 SCTP_CALC_TSN_TO_GAP(gap, i, asoc->mapping_array_base_tsn);
7935 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
7936 asoc->highest_tsn_inside_map = i;
7942 asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn - 1;
7946 * Question, should we go through the delivery queue? The only
7947 * reason things are on here is the app not reading OR a p-d-api up.
7948 * An attacker COULD send enough in to initiate the PD-API and then
7949 * send a bunch of stuff to other streams... these would wind up on
7950 * the delivery queue.. and then we would not get to them. But in
7951 * order to do this I then have to back-track and un-deliver
7952 * sequence numbers in streams.. el-yucko. I think for now we will
7953 * NOT look at the delivery queue and leave it to be something to
7954 * consider later. An alternative would be to abort the P-D-API with
7955 * a notification and then deliver the data.... Or another method
7956 * might be to keep track of how many times the situation occurs and
7957 * if we see a possible attack underway just abort the association.
7960 SCTPDBG(SCTP_DEBUG_PCB1, "Freed %d chunks from reneg harvest\n", cnt);
7963 * Now do we need to find a new
7964 * asoc->highest_tsn_inside_map?
7966 asoc->last_revoke_count = cnt;
7967 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL,
7968 SCTP_FROM_SCTP_PCB + SCTP_LOC_11);
7969 /*sa_ignore NO_NULL_CHK*/
7970 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
7971 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_DRAIN, SCTP_SO_NOT_LOCKED);
7974 * Another issue, in un-setting the TSN's in the mapping array we
7975 * DID NOT adjust the highest_tsn marker. This will cause one of two
7976 * things to occur. It may cause us to do extra work in checking for
7977 * our mapping array movement. More importantly it may cause us to
7978 * SACK every datagram. This may not be a bad thing though since we
7979 * will recover once we get our cum-ack above and all this stuff we
7988 * We must walk the PCB lists for ALL associations here. The system
7989 * is LOW on MBUF's and needs help. This is where reneging will
7990 * occur. We really hope this does NOT happen!
7992 #if defined(__FreeBSD__) && !defined(__Userspace__)
7993 VNET_ITERATOR_DECL(vnet_iter);
7995 struct sctp_inpcb *inp;
7996 struct sctp_tcb *stcb;
7998 SCTP_STAT_INCR(sctps_protocol_drain_calls);
7999 if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
8003 #if defined(__FreeBSD__) && !defined(__Userspace__)
8004 VNET_LIST_RLOCK_NOSLEEP();
8005 VNET_FOREACH(vnet_iter) {
8006 CURVNET_SET(vnet_iter);
8007 struct sctp_inpcb *inp;
8008 struct sctp_tcb *stcb;
8011 #if defined(__FreeBSD__) && !defined(__Userspace__)
8012 SCTP_STAT_INCR(sctps_protocol_drain_calls);
8013 if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
8021 SCTP_INP_INFO_RLOCK();
8022 LIST_FOREACH(inp, &SCTP_BASE_INFO(listhead), sctp_list) {
8023 /* For each endpoint */
8024 SCTP_INP_RLOCK(inp);
8025 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
8026 /* For each association */
8027 SCTP_TCB_LOCK(stcb);
8028 sctp_drain_mbufs(stcb);
8029 SCTP_TCB_UNLOCK(stcb);
8031 SCTP_INP_RUNLOCK(inp);
8033 SCTP_INP_INFO_RUNLOCK();
8034 #if defined(__FreeBSD__) && !defined(__Userspace__)
8037 VNET_LIST_RUNLOCK_NOSLEEP();
8042 * start a new iterator
8043 * iterates through all endpoints and associations based on the pcb_state
8044 * flags and asoc_state. "af" (mandatory) is executed for all matching
8045 * assocs and "ef" (optional) is executed when the iterator completes.
8046 * "inpf" (optional) is executed for each new endpoint as it is being
8047 * iterated through. inpe (optional) is called when the inp completes
8048 * its way through all the stcbs.
8051 sctp_initiate_iterator(inp_func inpf,
8055 uint32_t pcb_features,
8056 uint32_t asoc_state,
8060 struct sctp_inpcb *s_inp,
8061 uint8_t chunk_output_off)
8063 struct sctp_iterator *it = NULL;
8068 if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
8069 SCTP_PRINTF("%s: abort on initialize being %d\n", __func__,
8070 SCTP_BASE_VAR(sctp_pcb_initialized));
8073 SCTP_MALLOC(it, struct sctp_iterator *, sizeof(struct sctp_iterator),
8076 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
8079 memset(it, 0, sizeof(*it));
8080 it->function_assoc = af;
8081 it->function_inp = inpf;
8083 it->done_current_ep = 0;
8085 it->done_current_ep = 1;
8086 it->function_atend = ef;
8089 it->pcb_flags = pcb_state;
8090 it->pcb_features = pcb_features;
8091 it->asoc_state = asoc_state;
8092 it->function_inp_end = inpe;
8093 it->no_chunk_output = chunk_output_off;
8094 #if defined(__FreeBSD__) && !defined(__Userspace__)
8098 /* Assume lock is held here */
8100 SCTP_INP_INCR_REF(it->inp);
8101 it->iterator_flags = SCTP_ITERATOR_DO_SINGLE_INP;
8103 SCTP_INP_INFO_RLOCK();
8104 it->inp = LIST_FIRST(&SCTP_BASE_INFO(listhead));
8106 SCTP_INP_INCR_REF(it->inp);
8108 SCTP_INP_INFO_RUNLOCK();
8109 it->iterator_flags = SCTP_ITERATOR_DO_ALL_INP;
8112 SCTP_IPI_ITERATOR_WQ_LOCK();
8113 if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
8114 SCTP_IPI_ITERATOR_WQ_UNLOCK();
8115 SCTP_PRINTF("%s: rollback on initialize being %d it=%p\n", __func__,
8116 SCTP_BASE_VAR(sctp_pcb_initialized), it);
8117 SCTP_FREE(it, SCTP_M_ITER);
8120 TAILQ_INSERT_TAIL(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
8121 if (sctp_it_ctl.iterator_running == 0) {
8122 sctp_wakeup_iterator();
8124 SCTP_IPI_ITERATOR_WQ_UNLOCK();
8125 /* sa_ignore MEMLEAK {memory is put on the tailq for the iterator} */