2 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2008-2011, by Randall Stewart. All rights reserved.
4 * Copyright (c) 2008-2011, by Michael Tuexen. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * a) Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
12 * b) Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the distribution.
16 * c) Neither the name of Cisco Systems, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
33 /* $KAME: sctp_pcb.c,v 1.38 2005/03/06 16:04:18 itojun Exp $ */
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <netinet/sctp_os.h>
40 #include <netinet/sctp_var.h>
41 #include <netinet/sctp_sysctl.h>
42 #include <netinet/sctp_pcb.h>
43 #include <netinet/sctputil.h>
44 #include <netinet/sctp.h>
45 #include <netinet/sctp_header.h>
46 #include <netinet/sctp_asconf.h>
47 #include <netinet/sctp_output.h>
48 #include <netinet/sctp_timer.h>
49 #include <netinet/sctp_bsd_addr.h>
50 #include <netinet/sctp_dtrace_define.h>
51 #include <netinet/udp.h>
52 #include <sys/sched.h>
54 #include <sys/unistd.h>
57 VNET_DEFINE(struct sctp_base_info, system_base_info);
59 /* FIX: we don't handle multiple link local scopes */
60 /* "scopeless" replacement IN6_ARE_ADDR_EQUAL */
63 SCTP6_ARE_ADDR_EQUAL(struct sockaddr_in6 *a, struct sockaddr_in6 *b)
65 struct sockaddr_in6 tmp_a, tmp_b;
67 memcpy(&tmp_a, a, sizeof(struct sockaddr_in6));
68 if (sa6_embedscope(&tmp_a, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
71 memcpy(&tmp_b, b, sizeof(struct sockaddr_in6));
72 if (sa6_embedscope(&tmp_b, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
75 return (IN6_ARE_ADDR_EQUAL(&tmp_a.sin6_addr, &tmp_b.sin6_addr));
81 sctp_fill_pcbinfo(struct sctp_pcbinfo *spcb)
84 * We really don't need to lock this, but I will just because it
87 SCTP_INP_INFO_RLOCK();
88 spcb->ep_count = SCTP_BASE_INFO(ipi_count_ep);
89 spcb->asoc_count = SCTP_BASE_INFO(ipi_count_asoc);
90 spcb->laddr_count = SCTP_BASE_INFO(ipi_count_laddr);
91 spcb->raddr_count = SCTP_BASE_INFO(ipi_count_raddr);
92 spcb->chk_count = SCTP_BASE_INFO(ipi_count_chunk);
93 spcb->readq_count = SCTP_BASE_INFO(ipi_count_readq);
94 spcb->stream_oque = SCTP_BASE_INFO(ipi_count_strmoq);
95 spcb->free_chunks = SCTP_BASE_INFO(ipi_free_chunks);
97 SCTP_INP_INFO_RUNLOCK();
101 * Addresses are added to VRF's (Virtual Router's). For BSD we
102 * have only the default VRF 0. We maintain a hash list of
103 * VRF's. Each VRF has its own list of sctp_ifn's. Each of
104 * these has a list of addresses. When we add a new address
105 * to a VRF we lookup the ifn/ifn_index, if the ifn does
106 * not exist we create it and add it to the list of IFN's
107 * within the VRF. Once we have the sctp_ifn, we add the
108 * address to the list. So we look something like:
111 * vrf-> ifn-> ifn -> ifn
113 * ... +--ifa-> ifa -> ifa
116 * We keep these separate lists since the SCTP subsystem will
117 * point to these from its source address selection nets structure.
118 * When an address is deleted it does not happen right away on
119 * the SCTP side, it gets scheduled. What we do when a
120 * delete happens is immediately remove the address from
121 * the master list and decrement the refcount. As our
122 * addip iterator works through and frees the src address
123 * selection pointing to the sctp_ifa, eventually the refcount
124 * will reach 0 and we will delete it. Note that it is assumed
125 * that any locking on system level ifn/ifa is done at the
126 * caller of these functions and these routines will only
127 * lock the SCTP structures as they add or delete things.
129 * Other notes on VRF concepts.
130 * - An endpoint can be in multiple VRF's
131 * - An association lives within a VRF and only one VRF.
132 * - Any incoming packet we can deduce the VRF for by
133 * looking at the mbuf/pak inbound (for BSD its VRF=0 :D)
134 * - Any downward send call or connect call must supply the
135 * VRF via ancillary data or via some sort of set default
136 * VRF socket option call (again for BSD no brainer since
137 * the VRF is always 0).
138 * - An endpoint may add multiple VRF's to it.
139 * - Listening sockets can accept associations in any
140 * of the VRF's they are in but the assoc will end up
141 * in only one VRF (gotten from the packet or connect/send).
146 sctp_allocate_vrf(int vrf_id)
148 struct sctp_vrf *vrf = NULL;
149 struct sctp_vrflist *bucket;
151 /* First allocate the VRF structure */
152 vrf = sctp_find_vrf(vrf_id);
154 /* Already allocated */
157 SCTP_MALLOC(vrf, struct sctp_vrf *, sizeof(struct sctp_vrf),
162 panic("No memory for VRF:%d", vrf_id);
167 memset(vrf, 0, sizeof(struct sctp_vrf));
168 vrf->vrf_id = vrf_id;
169 LIST_INIT(&vrf->ifnlist);
170 vrf->total_ifa_count = 0;
172 /* now also setup table ids */
173 SCTP_INIT_VRF_TABLEID(vrf);
174 /* Init the HASH of addresses */
175 vrf->vrf_addr_hash = SCTP_HASH_INIT(SCTP_VRF_ADDR_HASH_SIZE,
176 &vrf->vrf_addr_hashmark);
177 if (vrf->vrf_addr_hash == NULL) {
180 panic("No memory for VRF:%d", vrf_id);
182 SCTP_FREE(vrf, SCTP_M_VRF);
185 /* Add it to the hash table */
186 bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(vrf_id & SCTP_BASE_INFO(hashvrfmark))];
187 LIST_INSERT_HEAD(bucket, vrf, next_vrf);
188 atomic_add_int(&SCTP_BASE_INFO(ipi_count_vrfs), 1);
194 sctp_find_ifn(void *ifn, uint32_t ifn_index)
196 struct sctp_ifn *sctp_ifnp;
197 struct sctp_ifnlist *hash_ifn_head;
200 * We assume the lock is held for the addresses if that's wrong
201 * problems could occur :-)
203 hash_ifn_head = &SCTP_BASE_INFO(vrf_ifn_hash)[(ifn_index & SCTP_BASE_INFO(vrf_ifn_hashmark))];
204 LIST_FOREACH(sctp_ifnp, hash_ifn_head, next_bucket) {
205 if (sctp_ifnp->ifn_index == ifn_index) {
208 if (sctp_ifnp->ifn_p && ifn && (sctp_ifnp->ifn_p == ifn)) {
218 sctp_find_vrf(uint32_t vrf_id)
220 struct sctp_vrflist *bucket;
221 struct sctp_vrf *liste;
223 bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(vrf_id & SCTP_BASE_INFO(hashvrfmark))];
224 LIST_FOREACH(liste, bucket, next_vrf) {
225 if (vrf_id == liste->vrf_id) {
233 sctp_free_vrf(struct sctp_vrf *vrf)
235 if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&vrf->refcount)) {
236 if (vrf->vrf_addr_hash) {
237 SCTP_HASH_FREE(vrf->vrf_addr_hash, vrf->vrf_addr_hashmark);
238 vrf->vrf_addr_hash = NULL;
240 /* We zero'd the count */
241 LIST_REMOVE(vrf, next_vrf);
242 SCTP_FREE(vrf, SCTP_M_VRF);
243 atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_vrfs), 1);
248 sctp_free_ifn(struct sctp_ifn *sctp_ifnp)
250 if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&sctp_ifnp->refcount)) {
251 /* We zero'd the count */
252 if (sctp_ifnp->vrf) {
253 sctp_free_vrf(sctp_ifnp->vrf);
255 SCTP_FREE(sctp_ifnp, SCTP_M_IFN);
256 atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_ifns), 1);
261 sctp_update_ifn_mtu(uint32_t ifn_index, uint32_t mtu)
263 struct sctp_ifn *sctp_ifnp;
265 sctp_ifnp = sctp_find_ifn((void *)NULL, ifn_index);
266 if (sctp_ifnp != NULL) {
267 sctp_ifnp->ifn_mtu = mtu;
273 sctp_free_ifa(struct sctp_ifa *sctp_ifap)
275 if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&sctp_ifap->refcount)) {
276 /* We zero'd the count */
277 if (sctp_ifap->ifn_p) {
278 sctp_free_ifn(sctp_ifap->ifn_p);
280 SCTP_FREE(sctp_ifap, SCTP_M_IFA);
281 atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_ifas), 1);
286 sctp_delete_ifn(struct sctp_ifn *sctp_ifnp, int hold_addr_lock)
288 struct sctp_ifn *found;
290 found = sctp_find_ifn(sctp_ifnp->ifn_p, sctp_ifnp->ifn_index);
292 /* Not in the list.. sorry */
295 if (hold_addr_lock == 0)
296 SCTP_IPI_ADDR_WLOCK();
297 LIST_REMOVE(sctp_ifnp, next_bucket);
298 LIST_REMOVE(sctp_ifnp, next_ifn);
299 SCTP_DEREGISTER_INTERFACE(sctp_ifnp->ifn_index,
300 sctp_ifnp->registered_af);
301 if (hold_addr_lock == 0)
302 SCTP_IPI_ADDR_WUNLOCK();
303 /* Take away the reference, and possibly free it */
304 sctp_free_ifn(sctp_ifnp);
308 sctp_mark_ifa_addr_down(uint32_t vrf_id, struct sockaddr *addr,
309 const char *if_name, uint32_t ifn_index)
311 struct sctp_vrf *vrf;
312 struct sctp_ifa *sctp_ifap = NULL;
314 SCTP_IPI_ADDR_RLOCK();
315 vrf = sctp_find_vrf(vrf_id);
317 SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id);
321 sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
322 if (sctp_ifap == NULL) {
323 SCTPDBG(SCTP_DEBUG_PCB4, "Can't find sctp_ifap for address\n");
326 if (sctp_ifap->ifn_p == NULL) {
327 SCTPDBG(SCTP_DEBUG_PCB4, "IFA has no IFN - can't mark unuseable\n");
333 len1 = strlen(if_name);
334 len2 = strlen(sctp_ifap->ifn_p->ifn_name);
336 SCTPDBG(SCTP_DEBUG_PCB4, "IFN of ifa names different length %d vs %d - ignored\n",
340 if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, len1) != 0) {
341 SCTPDBG(SCTP_DEBUG_PCB4, "IFN %s of IFA not the same as %s\n",
342 sctp_ifap->ifn_p->ifn_name,
347 if (sctp_ifap->ifn_p->ifn_index != ifn_index) {
348 SCTPDBG(SCTP_DEBUG_PCB4, "IFA owned by ifn_index:%d down command for ifn_index:%d - ignored\n",
349 sctp_ifap->ifn_p->ifn_index, ifn_index);
354 sctp_ifap->localifa_flags &= (~SCTP_ADDR_VALID);
355 sctp_ifap->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE;
357 SCTP_IPI_ADDR_RUNLOCK();
361 sctp_mark_ifa_addr_up(uint32_t vrf_id, struct sockaddr *addr,
362 const char *if_name, uint32_t ifn_index)
364 struct sctp_vrf *vrf;
365 struct sctp_ifa *sctp_ifap = NULL;
367 SCTP_IPI_ADDR_RLOCK();
368 vrf = sctp_find_vrf(vrf_id);
370 SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id);
374 sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
375 if (sctp_ifap == NULL) {
376 SCTPDBG(SCTP_DEBUG_PCB4, "Can't find sctp_ifap for address\n");
379 if (sctp_ifap->ifn_p == NULL) {
380 SCTPDBG(SCTP_DEBUG_PCB4, "IFA has no IFN - can't mark unuseable\n");
386 len1 = strlen(if_name);
387 len2 = strlen(sctp_ifap->ifn_p->ifn_name);
389 SCTPDBG(SCTP_DEBUG_PCB4, "IFN of ifa names different length %d vs %d - ignored\n",
393 if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, len1) != 0) {
394 SCTPDBG(SCTP_DEBUG_PCB4, "IFN %s of IFA not the same as %s\n",
395 sctp_ifap->ifn_p->ifn_name,
400 if (sctp_ifap->ifn_p->ifn_index != ifn_index) {
401 SCTPDBG(SCTP_DEBUG_PCB4, "IFA owned by ifn_index:%d down command for ifn_index:%d - ignored\n",
402 sctp_ifap->ifn_p->ifn_index, ifn_index);
407 sctp_ifap->localifa_flags &= (~SCTP_ADDR_IFA_UNUSEABLE);
408 sctp_ifap->localifa_flags |= SCTP_ADDR_VALID;
410 SCTP_IPI_ADDR_RUNLOCK();
414 * Add an ifa to an ifn.
415 * Register the interface as necessary.
416 * NOTE: ADDR write lock MUST be held.
419 sctp_add_ifa_to_ifn(struct sctp_ifn *sctp_ifnp, struct sctp_ifa *sctp_ifap)
423 LIST_INSERT_HEAD(&sctp_ifnp->ifalist, sctp_ifap, next_ifa);
424 sctp_ifap->ifn_p = sctp_ifnp;
425 atomic_add_int(&sctp_ifap->ifn_p->refcount, 1);
426 /* update address counts */
427 sctp_ifnp->ifa_count++;
428 ifa_af = sctp_ifap->address.sa.sa_family;
443 if (sctp_ifnp->ifa_count == 1) {
444 /* register the new interface */
445 SCTP_REGISTER_INTERFACE(sctp_ifnp->ifn_index, ifa_af);
446 sctp_ifnp->registered_af = ifa_af;
451 * Remove an ifa from its ifn.
452 * If no more addresses exist, remove the ifn too. Otherwise, re-register
453 * the interface based on the remaining address families left.
454 * NOTE: ADDR write lock MUST be held.
457 sctp_remove_ifa_from_ifn(struct sctp_ifa *sctp_ifap)
461 LIST_REMOVE(sctp_ifap, next_ifa);
462 if (sctp_ifap->ifn_p) {
463 /* update address counts */
464 sctp_ifap->ifn_p->ifa_count--;
465 switch (sctp_ifap->address.sa.sa_family) {
468 sctp_ifap->ifn_p->num_v4--;
473 sctp_ifap->ifn_p->num_v6--;
480 ifn_index = sctp_ifap->ifn_p->ifn_index;
481 if (LIST_EMPTY(&sctp_ifap->ifn_p->ifalist)) {
482 /* remove the ifn, possibly freeing it */
483 sctp_delete_ifn(sctp_ifap->ifn_p, SCTP_ADDR_LOCKED);
485 /* re-register address family type, if needed */
486 if ((sctp_ifap->ifn_p->num_v6 == 0) &&
487 (sctp_ifap->ifn_p->registered_af == AF_INET6)) {
488 SCTP_DEREGISTER_INTERFACE(ifn_index, AF_INET6);
489 SCTP_REGISTER_INTERFACE(ifn_index, AF_INET);
490 sctp_ifap->ifn_p->registered_af = AF_INET;
491 } else if ((sctp_ifap->ifn_p->num_v4 == 0) &&
492 (sctp_ifap->ifn_p->registered_af == AF_INET)) {
493 SCTP_DEREGISTER_INTERFACE(ifn_index, AF_INET);
494 SCTP_REGISTER_INTERFACE(ifn_index, AF_INET6);
495 sctp_ifap->ifn_p->registered_af = AF_INET6;
497 /* free the ifn refcount */
498 sctp_free_ifn(sctp_ifap->ifn_p);
500 sctp_ifap->ifn_p = NULL;
505 sctp_add_addr_to_vrf(uint32_t vrf_id, void *ifn, uint32_t ifn_index,
506 uint32_t ifn_type, const char *if_name, void *ifa,
507 struct sockaddr *addr, uint32_t ifa_flags,
510 struct sctp_vrf *vrf;
511 struct sctp_ifn *sctp_ifnp = NULL;
512 struct sctp_ifa *sctp_ifap = NULL;
513 struct sctp_ifalist *hash_addr_head;
514 struct sctp_ifnlist *hash_ifn_head;
515 uint32_t hash_of_addr;
519 SCTPDBG(SCTP_DEBUG_PCB4, "vrf_id 0x%x: adding address: ", vrf_id);
520 SCTPDBG_ADDR(SCTP_DEBUG_PCB4, addr);
522 SCTP_IPI_ADDR_WLOCK();
523 sctp_ifnp = sctp_find_ifn(ifn, ifn_index);
525 vrf = sctp_ifnp->vrf;
527 vrf = sctp_find_vrf(vrf_id);
529 vrf = sctp_allocate_vrf(vrf_id);
531 SCTP_IPI_ADDR_WUNLOCK();
536 if (sctp_ifnp == NULL) {
538 * build one and add it, can't hold lock until after malloc
541 SCTP_IPI_ADDR_WUNLOCK();
542 SCTP_MALLOC(sctp_ifnp, struct sctp_ifn *,
543 sizeof(struct sctp_ifn), SCTP_M_IFN);
544 if (sctp_ifnp == NULL) {
546 panic("No memory for IFN");
550 memset(sctp_ifnp, 0, sizeof(struct sctp_ifn));
551 sctp_ifnp->ifn_index = ifn_index;
552 sctp_ifnp->ifn_p = ifn;
553 sctp_ifnp->ifn_type = ifn_type;
554 sctp_ifnp->refcount = 0;
555 sctp_ifnp->vrf = vrf;
556 atomic_add_int(&vrf->refcount, 1);
557 sctp_ifnp->ifn_mtu = SCTP_GATHER_MTU_FROM_IFN_INFO(ifn, ifn_index, addr->sa_family);
558 if (if_name != NULL) {
559 memcpy(sctp_ifnp->ifn_name, if_name, SCTP_IFNAMSIZ);
561 memcpy(sctp_ifnp->ifn_name, "unknown", min(7, SCTP_IFNAMSIZ));
563 hash_ifn_head = &SCTP_BASE_INFO(vrf_ifn_hash)[(ifn_index & SCTP_BASE_INFO(vrf_ifn_hashmark))];
564 LIST_INIT(&sctp_ifnp->ifalist);
565 SCTP_IPI_ADDR_WLOCK();
566 LIST_INSERT_HEAD(hash_ifn_head, sctp_ifnp, next_bucket);
567 LIST_INSERT_HEAD(&vrf->ifnlist, sctp_ifnp, next_ifn);
568 atomic_add_int(&SCTP_BASE_INFO(ipi_count_ifns), 1);
571 sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
573 /* Hmm, it already exists? */
574 if ((sctp_ifap->ifn_p) &&
575 (sctp_ifap->ifn_p->ifn_index == ifn_index)) {
576 SCTPDBG(SCTP_DEBUG_PCB4, "Using existing ifn %s (0x%x) for ifa %p\n",
577 sctp_ifap->ifn_p->ifn_name, ifn_index,
580 /* Remove the created one that we don't want */
581 sctp_delete_ifn(sctp_ifnp, SCTP_ADDR_LOCKED);
583 if (sctp_ifap->localifa_flags & SCTP_BEING_DELETED) {
584 /* easy to solve, just switch back to active */
585 SCTPDBG(SCTP_DEBUG_PCB4, "Clearing deleted ifa flag\n");
586 sctp_ifap->localifa_flags = SCTP_ADDR_VALID;
587 sctp_ifap->ifn_p = sctp_ifnp;
588 atomic_add_int(&sctp_ifap->ifn_p->refcount, 1);
591 SCTP_IPI_ADDR_WUNLOCK();
594 if (sctp_ifap->ifn_p) {
596 * The last IFN gets the address, remove the
599 SCTPDBG(SCTP_DEBUG_PCB4, "Moving ifa %p from %s (0x%x) to %s (0x%x)\n",
600 sctp_ifap, sctp_ifap->ifn_p->ifn_name,
601 sctp_ifap->ifn_p->ifn_index, if_name,
603 /* remove the address from the old ifn */
604 sctp_remove_ifa_from_ifn(sctp_ifap);
605 /* move the address over to the new ifn */
606 sctp_add_ifa_to_ifn(sctp_ifnp, sctp_ifap);
607 goto exit_stage_left;
609 /* repair ifnp which was NULL ? */
610 sctp_ifap->localifa_flags = SCTP_ADDR_VALID;
611 SCTPDBG(SCTP_DEBUG_PCB4, "Repairing ifn %p for ifa %p\n",
612 sctp_ifnp, sctp_ifap);
613 sctp_add_ifa_to_ifn(sctp_ifnp, sctp_ifap);
615 goto exit_stage_left;
618 SCTP_IPI_ADDR_WUNLOCK();
619 SCTP_MALLOC(sctp_ifap, struct sctp_ifa *, sizeof(struct sctp_ifa), SCTP_M_IFA);
620 if (sctp_ifap == NULL) {
622 panic("No memory for IFA");
626 memset(sctp_ifap, 0, sizeof(struct sctp_ifa));
627 sctp_ifap->ifn_p = sctp_ifnp;
628 atomic_add_int(&sctp_ifnp->refcount, 1);
629 sctp_ifap->vrf_id = vrf_id;
630 sctp_ifap->ifa = ifa;
631 memcpy(&sctp_ifap->address, addr, addr->sa_len);
632 sctp_ifap->localifa_flags = SCTP_ADDR_VALID | SCTP_ADDR_DEFER_USE;
633 sctp_ifap->flags = ifa_flags;
635 switch (sctp_ifap->address.sa.sa_family) {
639 struct sockaddr_in *sin;
641 sin = (struct sockaddr_in *)&sctp_ifap->address.sin;
642 if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) ||
643 (IN4_ISLOOPBACK_ADDRESS(&sin->sin_addr))) {
644 sctp_ifap->src_is_loop = 1;
646 if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
647 sctp_ifap->src_is_priv = 1;
651 new_ifn_af = AF_INET;
658 /* ok to use deprecated addresses? */
659 struct sockaddr_in6 *sin6;
661 sin6 = (struct sockaddr_in6 *)&sctp_ifap->address.sin6;
662 if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) ||
663 (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))) {
664 sctp_ifap->src_is_loop = 1;
666 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
667 sctp_ifap->src_is_priv = 1;
671 new_ifn_af = AF_INET6;
679 hash_of_addr = sctp_get_ifa_hash_val(&sctp_ifap->address.sa);
681 if ((sctp_ifap->src_is_priv == 0) &&
682 (sctp_ifap->src_is_loop == 0)) {
683 sctp_ifap->src_is_glob = 1;
685 SCTP_IPI_ADDR_WLOCK();
686 hash_addr_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)];
687 LIST_INSERT_HEAD(hash_addr_head, sctp_ifap, next_bucket);
688 sctp_ifap->refcount = 1;
689 LIST_INSERT_HEAD(&sctp_ifnp->ifalist, sctp_ifap, next_ifa);
690 sctp_ifnp->ifa_count++;
691 vrf->total_ifa_count++;
692 atomic_add_int(&SCTP_BASE_INFO(ipi_count_ifas), 1);
694 SCTP_REGISTER_INTERFACE(ifn_index, new_ifn_af);
695 sctp_ifnp->registered_af = new_ifn_af;
697 SCTP_IPI_ADDR_WUNLOCK();
700 * Bump up the refcount so that when the timer completes it
701 * will drop back down.
703 struct sctp_laddr *wi;
705 atomic_add_int(&sctp_ifap->refcount, 1);
706 wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
709 * Gak, what can we do? We have lost an address
710 * change can you say HOSED?
712 SCTPDBG(SCTP_DEBUG_PCB4, "Lost an address change?\n");
713 /* Opps, must decrement the count */
714 sctp_del_addr_from_vrf(vrf_id, addr, ifn_index,
718 SCTP_INCR_LADDR_COUNT();
719 bzero(wi, sizeof(*wi));
720 (void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
722 wi->action = SCTP_ADD_IP_ADDRESS;
725 LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
726 SCTP_WQ_ADDR_UNLOCK();
728 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
729 (struct sctp_inpcb *)NULL,
730 (struct sctp_tcb *)NULL,
731 (struct sctp_nets *)NULL);
733 /* it's ready for use */
734 sctp_ifap->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
740 sctp_del_addr_from_vrf(uint32_t vrf_id, struct sockaddr *addr,
741 uint32_t ifn_index, const char *if_name)
743 struct sctp_vrf *vrf;
744 struct sctp_ifa *sctp_ifap = NULL;
746 SCTP_IPI_ADDR_WLOCK();
747 vrf = sctp_find_vrf(vrf_id);
749 SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id);
753 SCTPDBG(SCTP_DEBUG_PCB4, "vrf_id 0x%x: deleting address:", vrf_id);
754 SCTPDBG_ADDR(SCTP_DEBUG_PCB4, addr);
756 sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
758 /* Validate the delete */
759 if (sctp_ifap->ifn_p) {
763 * The name has priority over the ifn_index
764 * if its given. We do this especially for
765 * panda who might recycle indexes fast.
770 len1 = min(SCTP_IFNAMSIZ, strlen(if_name));
771 len2 = min(SCTP_IFNAMSIZ, strlen(sctp_ifap->ifn_p->ifn_name));
772 if (len1 && len2 && (len1 == len2)) {
773 /* we can compare them */
774 if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, len1) == 0) {
776 * They match its a correct
784 /* last ditch check ifn_index */
785 if (ifn_index == sctp_ifap->ifn_p->ifn_index) {
790 SCTPDBG(SCTP_DEBUG_PCB4, "ifn:%d ifname:%s does not match addresses\n",
791 ifn_index, ((if_name == NULL) ? "NULL" : if_name));
792 SCTPDBG(SCTP_DEBUG_PCB4, "ifn:%d ifname:%s - ignoring delete\n",
793 sctp_ifap->ifn_p->ifn_index, sctp_ifap->ifn_p->ifn_name);
794 SCTP_IPI_ADDR_WUNLOCK();
798 SCTPDBG(SCTP_DEBUG_PCB4, "Deleting ifa %p\n", sctp_ifap);
799 sctp_ifap->localifa_flags &= SCTP_ADDR_VALID;
800 sctp_ifap->localifa_flags |= SCTP_BEING_DELETED;
801 vrf->total_ifa_count--;
802 LIST_REMOVE(sctp_ifap, next_bucket);
803 sctp_remove_ifa_from_ifn(sctp_ifap);
807 SCTPDBG(SCTP_DEBUG_PCB4, "Del Addr-ifn:%d Could not find address:",
809 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr);
814 SCTP_IPI_ADDR_WUNLOCK();
816 struct sctp_laddr *wi;
818 wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
821 * Gak, what can we do? We have lost an address
822 * change can you say HOSED?
824 SCTPDBG(SCTP_DEBUG_PCB4, "Lost an address change?\n");
826 /* Oops, must decrement the count */
827 sctp_free_ifa(sctp_ifap);
830 SCTP_INCR_LADDR_COUNT();
831 bzero(wi, sizeof(*wi));
832 (void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
834 wi->action = SCTP_DEL_IP_ADDRESS;
837 * Should this really be a tailq? As it is we will process
838 * the newest first :-0
840 LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
841 SCTP_WQ_ADDR_UNLOCK();
843 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
844 (struct sctp_inpcb *)NULL,
845 (struct sctp_tcb *)NULL,
846 (struct sctp_nets *)NULL);
852 static struct sctp_tcb *
853 sctp_tcb_special_locate(struct sctp_inpcb **inp_p, struct sockaddr *from,
854 struct sockaddr *to, struct sctp_nets **netp, uint32_t vrf_id)
856 /**** ASSUMES THE CALLER holds the INP_INFO_RLOCK */
858 * If we support the TCP model, then we must now dig through to see
859 * if we can find our endpoint in the list of tcp ep's.
861 uint16_t lport, rport;
862 struct sctppcbhead *ephead;
863 struct sctp_inpcb *inp;
864 struct sctp_laddr *laddr;
865 struct sctp_tcb *stcb;
866 struct sctp_nets *net;
868 if ((to == NULL) || (from == NULL)) {
871 switch (to->sa_family) {
874 if (from->sa_family == AF_INET) {
875 lport = ((struct sockaddr_in *)to)->sin_port;
876 rport = ((struct sockaddr_in *)from)->sin_port;
884 if (from->sa_family == AF_INET6) {
885 lport = ((struct sockaddr_in6 *)to)->sin6_port;
886 rport = ((struct sockaddr_in6 *)from)->sin6_port;
895 ephead = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR((lport | rport), SCTP_BASE_INFO(hashtcpmark))];
897 * Ok now for each of the guys in this bucket we must look and see:
898 * - Does the remote port match. - Does there single association's
899 * addresses match this address (to). If so we update p_ep to point
900 * to this ep and return the tcb from it.
902 LIST_FOREACH(inp, ephead, sctp_hash) {
904 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
905 SCTP_INP_RUNLOCK(inp);
908 if (lport != inp->sctp_lport) {
909 SCTP_INP_RUNLOCK(inp);
912 if (inp->def_vrf_id != vrf_id) {
913 SCTP_INP_RUNLOCK(inp);
916 /* check to see if the ep has one of the addresses */
917 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
918 /* We are NOT bound all, so look further */
921 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
923 if (laddr->ifa == NULL) {
924 SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n", __FUNCTION__);
927 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
928 SCTPDBG(SCTP_DEBUG_PCB1, "ifa being deleted\n");
931 if (laddr->ifa->address.sa.sa_family ==
933 /* see if it matches */
936 if (from->sa_family == AF_INET) {
937 struct sockaddr_in *intf_addr,
940 intf_addr = &laddr->ifa->address.sin;
941 sin = (struct sockaddr_in *)to;
942 if (sin->sin_addr.s_addr ==
943 intf_addr->sin_addr.s_addr) {
950 if (from->sa_family == AF_INET6) {
951 struct sockaddr_in6 *intf_addr6;
952 struct sockaddr_in6 *sin6;
954 sin6 = (struct sockaddr_in6 *)
956 intf_addr6 = &laddr->ifa->address.sin6;
958 if (SCTP6_ARE_ADDR_EQUAL(sin6,
968 /* This endpoint does not have this address */
969 SCTP_INP_RUNLOCK(inp);
974 * Ok if we hit here the ep has the address, does it hold
978 stcb = LIST_FIRST(&inp->sctp_asoc_list);
980 SCTP_INP_RUNLOCK(inp);
984 if (stcb->rport != rport) {
985 /* remote port does not match. */
986 SCTP_TCB_UNLOCK(stcb);
987 SCTP_INP_RUNLOCK(inp);
990 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
991 SCTP_TCB_UNLOCK(stcb);
992 SCTP_INP_RUNLOCK(inp);
995 /* Does this TCB have a matching address? */
996 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
998 if (net->ro._l_addr.sa.sa_family != from->sa_family) {
999 /* not the same family, can't be a match */
1002 switch (from->sa_family) {
1006 struct sockaddr_in *sin, *rsin;
1008 sin = (struct sockaddr_in *)&net->ro._l_addr;
1009 rsin = (struct sockaddr_in *)from;
1010 if (sin->sin_addr.s_addr ==
1011 rsin->sin_addr.s_addr) {
1017 * Update the endpoint
1021 SCTP_INP_RUNLOCK(inp);
1030 struct sockaddr_in6 *sin6, *rsin6;
1032 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1033 rsin6 = (struct sockaddr_in6 *)from;
1034 if (SCTP6_ARE_ADDR_EQUAL(sin6,
1041 * Update the endpoint
1045 SCTP_INP_RUNLOCK(inp);
1056 SCTP_TCB_UNLOCK(stcb);
1057 SCTP_INP_RUNLOCK(inp);
1063 sctp_does_stcb_own_this_addr(struct sctp_tcb *stcb, struct sockaddr *to)
1065 int loopback_scope, ipv4_local_scope, local_scope, site_scope;
1066 int ipv4_addr_legal, ipv6_addr_legal;
1067 struct sctp_vrf *vrf;
1068 struct sctp_ifn *sctp_ifn;
1069 struct sctp_ifa *sctp_ifa;
1071 loopback_scope = stcb->asoc.loopback_scope;
1072 ipv4_local_scope = stcb->asoc.ipv4_local_scope;
1073 local_scope = stcb->asoc.local_scope;
1074 site_scope = stcb->asoc.site_scope;
1075 ipv4_addr_legal = ipv6_addr_legal = 0;
1076 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1077 ipv6_addr_legal = 1;
1078 if (SCTP_IPV6_V6ONLY(stcb->sctp_ep) == 0) {
1079 ipv4_addr_legal = 1;
1082 ipv4_addr_legal = 1;
1085 SCTP_IPI_ADDR_RLOCK();
1086 vrf = sctp_find_vrf(stcb->asoc.vrf_id);
1088 /* no vrf, no addresses */
1089 SCTP_IPI_ADDR_RUNLOCK();
1092 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1093 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
1094 if ((loopback_scope == 0) &&
1095 SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
1098 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
1099 if (sctp_is_addr_restricted(stcb, sctp_ifa))
1101 switch (sctp_ifa->address.sa.sa_family) {
1104 if (ipv4_addr_legal) {
1105 struct sockaddr_in *sin,
1108 sin = &sctp_ifa->address.sin;
1109 rsin = (struct sockaddr_in *)to;
1110 if ((ipv4_local_scope == 0) &&
1111 IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
1114 if (sin->sin_addr.s_addr == rsin->sin_addr.s_addr) {
1115 SCTP_IPI_ADDR_RUNLOCK();
1123 if (ipv6_addr_legal) {
1124 struct sockaddr_in6 *sin6,
1127 sin6 = &sctp_ifa->address.sin6;
1128 rsin6 = (struct sockaddr_in6 *)to;
1129 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1130 if (local_scope == 0)
1132 if (sin6->sin6_scope_id == 0) {
1133 if (sa6_recoverscope(sin6) != 0)
1137 if ((site_scope == 0) &&
1138 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1141 if (SCTP6_ARE_ADDR_EQUAL(sin6, rsin6)) {
1142 SCTP_IPI_ADDR_RUNLOCK();
1155 struct sctp_laddr *laddr;
1157 LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) {
1158 if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
1161 if (laddr->ifa->address.sa.sa_family != to->sa_family) {
1164 switch (to->sa_family) {
1168 struct sockaddr_in *sin, *rsin;
1170 sin = (struct sockaddr_in *)&laddr->ifa->address.sin;
1171 rsin = (struct sockaddr_in *)to;
1172 if (sin->sin_addr.s_addr == rsin->sin_addr.s_addr) {
1173 SCTP_IPI_ADDR_RUNLOCK();
1182 struct sockaddr_in6 *sin6, *rsin6;
1184 sin6 = (struct sockaddr_in6 *)&laddr->ifa->address.sin6;
1185 rsin6 = (struct sockaddr_in6 *)to;
1186 if (SCTP6_ARE_ADDR_EQUAL(sin6, rsin6)) {
1187 SCTP_IPI_ADDR_RUNLOCK();
1201 SCTP_IPI_ADDR_RUNLOCK();
1208 * 1) If I return a NULL you must decrement any INP ref cnt. 2) If I find an
1209 * stcb, both will be locked (locked_tcb and stcb) but decrement will be done
1210 * (if locked == NULL). 3) Decrement happens on return ONLY if locked ==
1215 sctp_findassociation_ep_addr(struct sctp_inpcb **inp_p, struct sockaddr *remote,
1216 struct sctp_nets **netp, struct sockaddr *local, struct sctp_tcb *locked_tcb)
1218 struct sctpasochead *head;
1219 struct sctp_inpcb *inp;
1220 struct sctp_tcb *stcb = NULL;
1221 struct sctp_nets *net;
1225 if (remote->sa_family == AF_INET) {
1226 rport = (((struct sockaddr_in *)remote)->sin_port);
1227 } else if (remote->sa_family == AF_INET6) {
1228 rport = (((struct sockaddr_in6 *)remote)->sin6_port);
1234 * UN-lock so we can do proper locking here this occurs when
1235 * called from load_addresses_from_init.
1237 atomic_add_int(&locked_tcb->asoc.refcnt, 1);
1238 SCTP_TCB_UNLOCK(locked_tcb);
1240 SCTP_INP_INFO_RLOCK();
1241 if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1243 * Now either this guy is our listener or it's the
1244 * connector. If it is the one that issued the connect, then
1245 * it's only chance is to be the first TCB in the list. If
1246 * it is the acceptor, then do the special_lookup to hash
1247 * and find the real inp.
1249 if ((inp->sctp_socket) && (inp->sctp_socket->so_qlimit)) {
1250 /* to is peer addr, from is my addr */
1251 stcb = sctp_tcb_special_locate(inp_p, remote, local,
1252 netp, inp->def_vrf_id);
1253 if ((stcb != NULL) && (locked_tcb == NULL)) {
1254 /* we have a locked tcb, lower refcount */
1255 SCTP_INP_DECR_REF(inp);
1257 if ((locked_tcb != NULL) && (locked_tcb != stcb)) {
1258 SCTP_INP_RLOCK(locked_tcb->sctp_ep);
1259 SCTP_TCB_LOCK(locked_tcb);
1260 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1261 SCTP_INP_RUNLOCK(locked_tcb->sctp_ep);
1263 SCTP_INP_INFO_RUNLOCK();
1266 SCTP_INP_WLOCK(inp);
1267 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1270 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1274 SCTP_TCB_LOCK(stcb);
1276 if (stcb->rport != rport) {
1277 /* remote port does not match. */
1278 SCTP_TCB_UNLOCK(stcb);
1281 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
1282 SCTP_TCB_UNLOCK(stcb);
1285 if (local && !sctp_does_stcb_own_this_addr(stcb, local)) {
1286 SCTP_TCB_UNLOCK(stcb);
1289 /* now look at the list of remote addresses */
1290 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1292 if (net == (TAILQ_NEXT(net, sctp_next))) {
1293 panic("Corrupt net list");
1296 if (net->ro._l_addr.sa.sa_family !=
1297 remote->sa_family) {
1298 /* not the same family */
1301 switch (remote->sa_family) {
1305 struct sockaddr_in *sin,
1308 sin = (struct sockaddr_in *)
1310 rsin = (struct sockaddr_in *)remote;
1311 if (sin->sin_addr.s_addr ==
1312 rsin->sin_addr.s_addr) {
1317 if (locked_tcb == NULL) {
1318 SCTP_INP_DECR_REF(inp);
1319 } else if (locked_tcb != stcb) {
1320 SCTP_TCB_LOCK(locked_tcb);
1323 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1325 SCTP_INP_WUNLOCK(inp);
1326 SCTP_INP_INFO_RUNLOCK();
1335 struct sockaddr_in6 *sin6,
1338 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1339 rsin6 = (struct sockaddr_in6 *)remote;
1340 if (SCTP6_ARE_ADDR_EQUAL(sin6,
1346 if (locked_tcb == NULL) {
1347 SCTP_INP_DECR_REF(inp);
1348 } else if (locked_tcb != stcb) {
1349 SCTP_TCB_LOCK(locked_tcb);
1352 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1354 SCTP_INP_WUNLOCK(inp);
1355 SCTP_INP_INFO_RUNLOCK();
1366 SCTP_TCB_UNLOCK(stcb);
1369 SCTP_INP_WLOCK(inp);
1370 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1373 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(rport,
1374 inp->sctp_hashmark)];
1378 LIST_FOREACH(stcb, head, sctp_tcbhash) {
1379 if (stcb->rport != rport) {
1380 /* remote port does not match */
1383 SCTP_TCB_LOCK(stcb);
1384 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
1385 SCTP_TCB_UNLOCK(stcb);
1388 if (local && !sctp_does_stcb_own_this_addr(stcb, local)) {
1389 SCTP_TCB_UNLOCK(stcb);
1392 /* now look at the list of remote addresses */
1393 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1395 if (net == (TAILQ_NEXT(net, sctp_next))) {
1396 panic("Corrupt net list");
1399 if (net->ro._l_addr.sa.sa_family !=
1400 remote->sa_family) {
1401 /* not the same family */
1404 switch (remote->sa_family) {
1408 struct sockaddr_in *sin,
1411 sin = (struct sockaddr_in *)
1413 rsin = (struct sockaddr_in *)remote;
1414 if (sin->sin_addr.s_addr ==
1415 rsin->sin_addr.s_addr) {
1420 if (locked_tcb == NULL) {
1421 SCTP_INP_DECR_REF(inp);
1422 } else if (locked_tcb != stcb) {
1423 SCTP_TCB_LOCK(locked_tcb);
1426 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1428 SCTP_INP_WUNLOCK(inp);
1429 SCTP_INP_INFO_RUNLOCK();
1438 struct sockaddr_in6 *sin6,
1441 sin6 = (struct sockaddr_in6 *)
1443 rsin6 = (struct sockaddr_in6 *)remote;
1444 if (SCTP6_ARE_ADDR_EQUAL(sin6,
1450 if (locked_tcb == NULL) {
1451 SCTP_INP_DECR_REF(inp);
1452 } else if (locked_tcb != stcb) {
1453 SCTP_TCB_LOCK(locked_tcb);
1456 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1458 SCTP_INP_WUNLOCK(inp);
1459 SCTP_INP_INFO_RUNLOCK();
1470 SCTP_TCB_UNLOCK(stcb);
1474 /* clean up for returning null */
1476 SCTP_TCB_LOCK(locked_tcb);
1477 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1479 SCTP_INP_WUNLOCK(inp);
1480 SCTP_INP_INFO_RUNLOCK();
1486 * Find an association for a specific endpoint using the association id given
1487 * out in the COMM_UP notification
1491 sctp_findasoc_ep_asocid_locked(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock)
1494 * Use my the assoc_id to find a endpoint
1496 struct sctpasochead *head;
1497 struct sctp_tcb *stcb;
1501 SCTP_PRINTF("TSNH ep_associd\n");
1504 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1505 SCTP_PRINTF("TSNH ep_associd0\n");
1508 id = (uint32_t) asoc_id;
1509 head = &inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(id, inp->hashasocidmark)];
1511 /* invalid id TSNH */
1512 SCTP_PRINTF("TSNH ep_associd1\n");
1515 LIST_FOREACH(stcb, head, sctp_tcbasocidhash) {
1516 if (stcb->asoc.assoc_id == id) {
1517 if (inp != stcb->sctp_ep) {
1519 * some other guy has the same id active (id
1522 SCTP_PRINTF("TSNH ep_associd2\n");
1525 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
1529 SCTP_TCB_LOCK(stcb);
1539 sctp_findassociation_ep_asocid(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock)
1541 struct sctp_tcb *stcb;
1543 SCTP_INP_RLOCK(inp);
1544 stcb = sctp_findasoc_ep_asocid_locked(inp, asoc_id, want_lock);
1545 SCTP_INP_RUNLOCK(inp);
1550 static struct sctp_inpcb *
1551 sctp_endpoint_probe(struct sockaddr *nam, struct sctppcbhead *head,
1552 uint16_t lport, uint32_t vrf_id)
1554 struct sctp_inpcb *inp;
1555 struct sctp_laddr *laddr;
1558 struct sockaddr_in *sin;
1562 struct sockaddr_in6 *sin6;
1563 struct sockaddr_in6 *intf_addr6;
1570 * Endpoint probe expects that the INP_INFO is locked.
1578 switch (nam->sa_family) {
1581 sin = (struct sockaddr_in *)nam;
1586 sin6 = (struct sockaddr_in6 *)nam;
1590 /* unsupported family */
1597 LIST_FOREACH(inp, head, sctp_hash) {
1598 SCTP_INP_RLOCK(inp);
1599 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1600 SCTP_INP_RUNLOCK(inp);
1603 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) &&
1604 (inp->sctp_lport == lport)) {
1607 if ((nam->sa_family == AF_INET) &&
1608 (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1609 SCTP_IPV6_V6ONLY(inp)) {
1610 /* IPv4 on a IPv6 socket with ONLY IPv6 set */
1611 SCTP_INP_RUNLOCK(inp);
1616 /* A V6 address and the endpoint is NOT bound V6 */
1617 if (nam->sa_family == AF_INET6 &&
1618 (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
1619 SCTP_INP_RUNLOCK(inp);
1623 /* does a VRF id match? */
1625 if (inp->def_vrf_id == vrf_id)
1628 SCTP_INP_RUNLOCK(inp);
1633 SCTP_INP_RUNLOCK(inp);
1635 switch (nam->sa_family) {
1638 if (sin->sin_addr.s_addr == INADDR_ANY) {
1639 /* Can't hunt for one that has no address specified */
1646 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1647 /* Can't hunt for one that has no address specified */
1656 * ok, not bound to all so see if we can find a EP bound to this
1659 LIST_FOREACH(inp, head, sctp_hash) {
1660 SCTP_INP_RLOCK(inp);
1661 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1662 SCTP_INP_RUNLOCK(inp);
1665 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)) {
1666 SCTP_INP_RUNLOCK(inp);
1670 * Ok this could be a likely candidate, look at all of its
1673 if (inp->sctp_lport != lport) {
1674 SCTP_INP_RUNLOCK(inp);
1677 /* does a VRF id match? */
1679 if (inp->def_vrf_id == vrf_id)
1683 SCTP_INP_RUNLOCK(inp);
1686 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1687 if (laddr->ifa == NULL) {
1688 SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n",
1692 SCTPDBG(SCTP_DEBUG_PCB1, "Ok laddr->ifa:%p is possible, ",
1694 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
1695 SCTPDBG(SCTP_DEBUG_PCB1, "Huh IFA being deleted\n");
1698 if (laddr->ifa->address.sa.sa_family == nam->sa_family) {
1699 /* possible, see if it matches */
1700 switch (nam->sa_family) {
1703 if (sin->sin_addr.s_addr ==
1704 laddr->ifa->address.sin.sin_addr.s_addr) {
1705 SCTP_INP_RUNLOCK(inp);
1712 intf_addr6 = &laddr->ifa->address.sin6;
1713 if (SCTP6_ARE_ADDR_EQUAL(sin6,
1715 SCTP_INP_RUNLOCK(inp);
1723 SCTP_INP_RUNLOCK(inp);
1729 static struct sctp_inpcb *
1730 sctp_isport_inuse(struct sctp_inpcb *inp, uint16_t lport, uint32_t vrf_id)
1732 struct sctppcbhead *head;
1733 struct sctp_inpcb *t_inp;
1736 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport,
1737 SCTP_BASE_INFO(hashmark))];
1738 LIST_FOREACH(t_inp, head, sctp_hash) {
1739 if (t_inp->sctp_lport != lport) {
1742 /* is it in the VRF in question */
1744 if (t_inp->def_vrf_id == vrf_id)
1749 /* This one is in use. */
1750 /* check the v6/v4 binding issue */
1751 if ((t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1752 SCTP_IPV6_V6ONLY(t_inp)) {
1753 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1754 /* collision in V6 space */
1757 /* inp is BOUND_V4 no conflict */
1760 } else if (t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1761 /* t_inp is bound v4 and v6, conflict always */
1764 /* t_inp is bound only V4 */
1765 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1766 SCTP_IPV6_V6ONLY(inp)) {
1770 /* else fall through to conflict */
1779 sctp_swap_inpcb_for_listen(struct sctp_inpcb *inp)
1781 /* For 1-2-1 with port reuse */
1782 struct sctppcbhead *head;
1783 struct sctp_inpcb *tinp;
1785 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) {
1786 /* only works with port reuse on */
1789 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) {
1792 SCTP_INP_RUNLOCK(inp);
1793 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(inp->sctp_lport,
1794 SCTP_BASE_INFO(hashmark))];
1795 /* Kick out all non-listeners to the TCP hash */
1796 LIST_FOREACH(tinp, head, sctp_hash) {
1797 if (tinp->sctp_lport != inp->sctp_lport) {
1800 if (tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1803 if (tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
1806 if (tinp->sctp_socket->so_qlimit) {
1809 SCTP_INP_WLOCK(tinp);
1810 LIST_REMOVE(tinp, sctp_hash);
1811 head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR(tinp->sctp_lport, SCTP_BASE_INFO(hashtcpmark))];
1812 tinp->sctp_flags |= SCTP_PCB_FLAGS_IN_TCPPOOL;
1813 LIST_INSERT_HEAD(head, tinp, sctp_hash);
1814 SCTP_INP_WUNLOCK(tinp);
1816 SCTP_INP_WLOCK(inp);
1817 /* Pull from where he was */
1818 LIST_REMOVE(inp, sctp_hash);
1819 inp->sctp_flags &= ~SCTP_PCB_FLAGS_IN_TCPPOOL;
1820 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(inp->sctp_lport, SCTP_BASE_INFO(hashmark))];
1821 LIST_INSERT_HEAD(head, inp, sctp_hash);
1822 SCTP_INP_WUNLOCK(inp);
1823 SCTP_INP_RLOCK(inp);
1829 sctp_pcb_findep(struct sockaddr *nam, int find_tcp_pool, int have_lock,
1833 * First we check the hash table to see if someone has this port
1834 * bound with just the port.
1836 struct sctp_inpcb *inp;
1837 struct sctppcbhead *head;
1842 struct sockaddr_in *sin;
1846 struct sockaddr_in6 *sin6;
1850 switch (nam->sa_family) {
1853 sin = (struct sockaddr_in *)nam;
1854 lport = ((struct sockaddr_in *)nam)->sin_port;
1859 sin6 = (struct sockaddr_in6 *)nam;
1860 lport = ((struct sockaddr_in6 *)nam)->sin6_port;
1867 * I could cheat here and just cast to one of the types but we will
1868 * do it right. It also provides the check against an Unsupported
1871 /* Find the head of the ALLADDR chain */
1872 if (have_lock == 0) {
1873 SCTP_INP_INFO_RLOCK();
1875 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport,
1876 SCTP_BASE_INFO(hashmark))];
1877 inp = sctp_endpoint_probe(nam, head, lport, vrf_id);
1880 * If the TCP model exists it could be that the main listening
1881 * endpoint is gone but there still exists a connected socket for
1882 * this guy. If so we can return the first one that we find. This
1883 * may NOT be the correct one so the caller should be wary on the
1884 * returned INP. Currently the only caller that sets find_tcp_pool
1885 * is in bindx where we are verifying that a user CAN bind the
1886 * address. He either has bound it already, or someone else has, or
1887 * its open to bind, so this is good enough.
1889 if (inp == NULL && find_tcp_pool) {
1890 for (i = 0; i < SCTP_BASE_INFO(hashtcpmark) + 1; i++) {
1891 head = &SCTP_BASE_INFO(sctp_tcpephash)[i];
1892 inp = sctp_endpoint_probe(nam, head, lport, vrf_id);
1899 SCTP_INP_INCR_REF(inp);
1901 if (have_lock == 0) {
1902 SCTP_INP_INFO_RUNLOCK();
1908 * Find an association for an endpoint with the pointer to whom you want to
1909 * send to and the endpoint pointer. The address can be IPv4 or IPv6. We may
1910 * need to change the *to to some other struct like a mbuf...
1913 sctp_findassociation_addr_sa(struct sockaddr *to, struct sockaddr *from,
1914 struct sctp_inpcb **inp_p, struct sctp_nets **netp, int find_tcp_pool,
1917 struct sctp_inpcb *inp = NULL;
1918 struct sctp_tcb *retval;
1920 SCTP_INP_INFO_RLOCK();
1921 if (find_tcp_pool) {
1922 if (inp_p != NULL) {
1923 retval = sctp_tcb_special_locate(inp_p, from, to, netp,
1926 retval = sctp_tcb_special_locate(&inp, from, to, netp,
1929 if (retval != NULL) {
1930 SCTP_INP_INFO_RUNLOCK();
1934 inp = sctp_pcb_findep(to, 0, 1, vrf_id);
1935 if (inp_p != NULL) {
1938 SCTP_INP_INFO_RUNLOCK();
1944 * ok, we have an endpoint, now lets find the assoc for it (if any)
1945 * we now place the source address or from in the to of the find
1946 * endpoint call. Since in reality this chain is used from the
1947 * inbound packet side.
1949 if (inp_p != NULL) {
1950 retval = sctp_findassociation_ep_addr(inp_p, from, netp, to,
1953 retval = sctp_findassociation_ep_addr(&inp, from, netp, to,
1961 * This routine will grub through the mbuf that is a INIT or INIT-ACK and
1962 * find all addresses that the sender has specified in any address list. Each
1963 * address will be used to lookup the TCB and see if one exits.
1965 static struct sctp_tcb *
1966 sctp_findassociation_special_addr(struct mbuf *m, int iphlen, int offset,
1967 struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp,
1968 struct sockaddr *dest)
1970 struct sctp_paramhdr *phdr, parm_buf;
1971 struct sctp_tcb *retval;
1972 uint32_t ptype, plen;
1975 struct sockaddr_in sin4;
1979 struct sockaddr_in6 sin6;
1984 memset(&sin4, 0, sizeof(sin4));
1985 sin4.sin_len = sizeof(sin4);
1986 sin4.sin_family = AF_INET;
1987 sin4.sin_port = sh->src_port;
1990 memset(&sin6, 0, sizeof(sin6));
1991 sin6.sin6_len = sizeof(sin6);
1992 sin6.sin6_family = AF_INET6;
1993 sin6.sin6_port = sh->src_port;
1997 offset += sizeof(struct sctp_init_chunk);
1999 phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
2000 while (phdr != NULL) {
2001 /* now we must see if we want the parameter */
2002 ptype = ntohs(phdr->param_type);
2003 plen = ntohs(phdr->param_length);
2008 if (ptype == SCTP_IPV4_ADDRESS &&
2009 plen == sizeof(struct sctp_ipv4addr_param)) {
2010 /* Get the rest of the address */
2011 struct sctp_ipv4addr_param ip4_parm, *p4;
2013 phdr = sctp_get_next_param(m, offset,
2014 (struct sctp_paramhdr *)&ip4_parm, min(plen, sizeof(ip4_parm)));
2018 p4 = (struct sctp_ipv4addr_param *)phdr;
2019 memcpy(&sin4.sin_addr, &p4->addr, sizeof(p4->addr));
2021 retval = sctp_findassociation_ep_addr(inp_p,
2022 (struct sockaddr *)&sin4, netp, dest, NULL);
2023 if (retval != NULL) {
2029 if (ptype == SCTP_IPV6_ADDRESS &&
2030 plen == sizeof(struct sctp_ipv6addr_param)) {
2031 /* Get the rest of the address */
2032 struct sctp_ipv6addr_param ip6_parm, *p6;
2034 phdr = sctp_get_next_param(m, offset,
2035 (struct sctp_paramhdr *)&ip6_parm, min(plen, sizeof(ip6_parm)));
2039 p6 = (struct sctp_ipv6addr_param *)phdr;
2040 memcpy(&sin6.sin6_addr, &p6->addr, sizeof(p6->addr));
2042 retval = sctp_findassociation_ep_addr(inp_p,
2043 (struct sockaddr *)&sin6, netp, dest, NULL);
2044 if (retval != NULL) {
2049 offset += SCTP_SIZE32(plen);
2050 phdr = sctp_get_next_param(m, offset, &parm_buf,
2056 static struct sctp_tcb *
2057 sctp_findassoc_by_vtag(struct sockaddr *from, struct sockaddr *to, uint32_t vtag,
2058 struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint16_t rport,
2059 uint16_t lport, int skip_src_check, uint32_t vrf_id, uint32_t remote_tag)
2062 * Use my vtag to hash. If we find it we then verify the source addr
2063 * is in the assoc. If all goes well we save a bit on rec of a
2066 struct sctpasochead *head;
2067 struct sctp_nets *net;
2068 struct sctp_tcb *stcb;
2072 SCTP_INP_INFO_RLOCK();
2073 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(vtag,
2074 SCTP_BASE_INFO(hashasocmark))];
2077 SCTP_INP_INFO_RUNLOCK();
2080 LIST_FOREACH(stcb, head, sctp_asocs) {
2081 SCTP_INP_RLOCK(stcb->sctp_ep);
2082 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
2083 SCTP_INP_RUNLOCK(stcb->sctp_ep);
2086 SCTP_TCB_LOCK(stcb);
2087 SCTP_INP_RUNLOCK(stcb->sctp_ep);
2088 if (stcb->asoc.my_vtag == vtag) {
2090 if (stcb->rport != rport) {
2091 SCTP_TCB_UNLOCK(stcb);
2094 if (stcb->sctp_ep->sctp_lport != lport) {
2095 SCTP_TCB_UNLOCK(stcb);
2098 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
2099 SCTP_TCB_UNLOCK(stcb);
2102 /* RRS:Need toaddr check here */
2103 if (sctp_does_stcb_own_this_addr(stcb, to) == 0) {
2104 /* Endpoint does not own this address */
2105 SCTP_TCB_UNLOCK(stcb);
2110 * If we have both vtags that's all we match
2113 if (stcb->asoc.peer_vtag == remote_tag) {
2115 * If both tags match we consider it
2116 * conclusive and check NO
2117 * source/destination addresses
2122 if (skip_src_check) {
2125 net = sctp_findnet(stcb, from);
2127 *netp = NULL; /* unknown */
2130 *inp_p = stcb->sctp_ep;
2131 SCTP_INP_INFO_RUNLOCK();
2134 net = sctp_findnet(stcb, from);
2138 SCTP_STAT_INCR(sctps_vtagexpress);
2139 *inp_p = stcb->sctp_ep;
2140 SCTP_INP_INFO_RUNLOCK();
2144 * not him, this should only happen in rare
2145 * cases so I peg it.
2147 SCTP_STAT_INCR(sctps_vtagbogus);
2150 SCTP_TCB_UNLOCK(stcb);
2152 SCTP_INP_INFO_RUNLOCK();
2157 * Find an association with the pointer to the inbound IP packet. This can be
2158 * a IPv4 or IPv6 packet.
2161 sctp_findassociation_addr(struct mbuf *m, int iphlen, int offset,
2162 struct sctphdr *sh, struct sctp_chunkhdr *ch,
2163 struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id)
2167 struct sctp_tcb *retval;
2168 struct sockaddr_storage to_store, from_store;
2169 struct sockaddr *to = (struct sockaddr *)&to_store;
2170 struct sockaddr *from = (struct sockaddr *)&from_store;
2171 struct sctp_inpcb *inp;
2173 iph = mtod(m, struct ip *);
2174 switch (iph->ip_v) {
2179 struct sockaddr_in *from4;
2181 from4 = (struct sockaddr_in *)&from_store;
2182 bzero(from4, sizeof(*from4));
2183 from4->sin_family = AF_INET;
2184 from4->sin_len = sizeof(struct sockaddr_in);
2185 from4->sin_addr.s_addr = iph->ip_src.s_addr;
2186 from4->sin_port = sh->src_port;
2191 case IPV6_VERSION >> 4:
2194 struct ip6_hdr *ip6;
2195 struct sockaddr_in6 *from6;
2197 ip6 = mtod(m, struct ip6_hdr *);
2198 from6 = (struct sockaddr_in6 *)&from_store;
2199 bzero(from6, sizeof(*from6));
2200 from6->sin6_family = AF_INET6;
2201 from6->sin6_len = sizeof(struct sockaddr_in6);
2202 from6->sin6_addr = ip6->ip6_src;
2203 from6->sin6_port = sh->src_port;
2204 /* Get the scopes in properly to the sin6 addr's */
2205 /* we probably don't need these operations */
2206 (void)sa6_recoverscope(from6);
2207 sa6_embedscope(from6, MODULE_GLOBAL(ip6_use_defzone));
2212 /* Currently not supported. */
2217 switch (iph->ip_v) {
2222 struct sockaddr_in *to4;
2224 to4 = (struct sockaddr_in *)&to_store;
2225 bzero(to4, sizeof(*to4));
2226 to4->sin_family = AF_INET;
2227 to4->sin_len = sizeof(struct sockaddr_in);
2228 to4->sin_addr.s_addr = iph->ip_dst.s_addr;
2229 to4->sin_port = sh->dest_port;
2234 case IPV6_VERSION >> 4:
2237 struct ip6_hdr *ip6;
2238 struct sockaddr_in6 *to6;
2240 ip6 = mtod(m, struct ip6_hdr *);
2241 to6 = (struct sockaddr_in6 *)&to_store;
2242 bzero(to6, sizeof(*to6));
2243 to6->sin6_family = AF_INET6;
2244 to6->sin6_len = sizeof(struct sockaddr_in6);
2245 to6->sin6_addr = ip6->ip6_dst;
2246 to6->sin6_port = sh->dest_port;
2247 /* Get the scopes in properly to the sin6 addr's */
2248 /* we probably don't need these operations */
2249 (void)sa6_recoverscope(to6);
2250 sa6_embedscope(to6, MODULE_GLOBAL(ip6_use_defzone));
2259 /* we only go down this path if vtag is non-zero */
2260 retval = sctp_findassoc_by_vtag(from, to, ntohl(sh->v_tag),
2261 inp_p, netp, sh->src_port, sh->dest_port, 0, vrf_id, 0);
2267 if ((ch->chunk_type != SCTP_INITIATION) &&
2268 (ch->chunk_type != SCTP_INITIATION_ACK) &&
2269 (ch->chunk_type != SCTP_COOKIE_ACK) &&
2270 (ch->chunk_type != SCTP_COOKIE_ECHO)) {
2271 /* Other chunk types go to the tcp pool. */
2275 retval = sctp_findassociation_addr_sa(to, from, inp_p, netp,
2276 find_tcp_pool, vrf_id);
2279 retval = sctp_findassociation_addr_sa(to, from, &inp, netp,
2280 find_tcp_pool, vrf_id);
2282 SCTPDBG(SCTP_DEBUG_PCB1, "retval:%p inp:%p\n", retval, inp);
2283 if (retval == NULL && inp) {
2284 /* Found a EP but not this address */
2285 if ((ch->chunk_type == SCTP_INITIATION) ||
2286 (ch->chunk_type == SCTP_INITIATION_ACK)) {
2288 * special hook, we do NOT return linp or an
2289 * association that is linked to an existing
2290 * association that is under the TCP pool (i.e. no
2291 * listener exists). The endpoint finding routine
2292 * will always find a listener before examining the
2295 if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
2301 retval = sctp_findassociation_special_addr(m, iphlen,
2302 offset, sh, &inp, netp, to);
2303 if (inp_p != NULL) {
2308 SCTPDBG(SCTP_DEBUG_PCB1, "retval is %p\n", retval);
2313 * lookup an association by an ASCONF lookup address.
2314 * if the lookup address is 0.0.0.0 or ::0, use the vtag to do the lookup
2317 sctp_findassociation_ep_asconf(struct mbuf *m, int iphlen, int offset,
2318 struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id)
2320 struct sctp_tcb *stcb;
2321 struct sockaddr_storage local_store, remote_store;
2322 struct sockaddr *to;
2324 struct sctp_paramhdr parm_buf, *phdr;
2326 int zero_address = 0;
2329 struct sockaddr_in *sin;
2333 struct ip6_hdr *ip6;
2334 struct sockaddr_in6 *sin6;
2338 memset(&local_store, 0, sizeof(local_store));
2339 memset(&remote_store, 0, sizeof(remote_store));
2340 to = (struct sockaddr *)&local_store;
2341 /* First get the destination address setup too. */
2342 iph = mtod(m, struct ip *);
2343 switch (iph->ip_v) {
2347 sin = (struct sockaddr_in *)&local_store;
2348 sin->sin_family = AF_INET;
2349 sin->sin_len = sizeof(*sin);
2350 sin->sin_port = sh->dest_port;
2351 sin->sin_addr.s_addr = iph->ip_dst.s_addr;
2355 case IPV6_VERSION >> 4:
2357 ip6 = mtod(m, struct ip6_hdr *);
2358 sin6 = (struct sockaddr_in6 *)&local_store;
2359 sin6->sin6_family = AF_INET6;
2360 sin6->sin6_len = sizeof(*sin6);
2361 sin6->sin6_port = sh->dest_port;
2362 sin6->sin6_addr = ip6->ip6_dst;
2369 phdr = sctp_get_next_param(m, offset + sizeof(struct sctp_asconf_chunk),
2370 &parm_buf, sizeof(struct sctp_paramhdr));
2372 SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf lookup addr\n",
2376 ptype = (int)((uint32_t) ntohs(phdr->param_type));
2377 /* get the correlation address */
2380 case SCTP_IPV6_ADDRESS:
2382 /* ipv6 address param */
2383 struct sctp_ipv6addr_param *p6, p6_buf;
2385 if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv6addr_param)) {
2388 p6 = (struct sctp_ipv6addr_param *)sctp_get_next_param(m,
2389 offset + sizeof(struct sctp_asconf_chunk),
2390 &p6_buf.ph, sizeof(*p6));
2392 SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v6 lookup addr\n",
2396 sin6 = (struct sockaddr_in6 *)&remote_store;
2397 sin6->sin6_family = AF_INET6;
2398 sin6->sin6_len = sizeof(*sin6);
2399 sin6->sin6_port = sh->src_port;
2400 memcpy(&sin6->sin6_addr, &p6->addr, sizeof(struct in6_addr));
2401 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
2407 case SCTP_IPV4_ADDRESS:
2409 /* ipv4 address param */
2410 struct sctp_ipv4addr_param *p4, p4_buf;
2412 if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv4addr_param)) {
2415 p4 = (struct sctp_ipv4addr_param *)sctp_get_next_param(m,
2416 offset + sizeof(struct sctp_asconf_chunk),
2417 &p4_buf.ph, sizeof(*p4));
2419 SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v4 lookup addr\n",
2423 sin = (struct sockaddr_in *)&remote_store;
2424 sin->sin_family = AF_INET;
2425 sin->sin_len = sizeof(*sin);
2426 sin->sin_port = sh->src_port;
2427 memcpy(&sin->sin_addr, &p4->addr, sizeof(struct in_addr));
2428 if (sin->sin_addr.s_addr == INADDR_ANY)
2434 /* invalid address param type */
2439 stcb = sctp_findassoc_by_vtag(NULL, to, ntohl(sh->v_tag), inp_p,
2440 netp, sh->src_port, sh->dest_port, 1, vrf_id, 0);
2442 * printf("findassociation_ep_asconf: zero lookup address
2443 * finds stcb 0x%x\n", (uint32_t)stcb);
2446 stcb = sctp_findassociation_ep_addr(inp_p,
2447 (struct sockaddr *)&remote_store, netp,
2455 * allocate a sctp_inpcb and setup a temporary binding to a port/all
2456 * addresses. This way if we don't get a bind we by default pick a ephemeral
2457 * port with all addresses bound.
2460 sctp_inpcb_alloc(struct socket *so, uint32_t vrf_id)
2463 * we get called when a new endpoint starts up. We need to allocate
2464 * the sctp_inpcb structure from the zone and init it. Mark it as
2465 * unbound and find a port that we can use as an ephemeral with
2466 * INADDR_ANY. If the user binds later no problem we can then add in
2467 * the specific addresses. And setup the default parameters for the
2471 struct sctp_inpcb *inp;
2473 struct timeval time;
2474 sctp_sharedkey_t *null_key;
2478 SCTP_INP_INFO_WLOCK();
2479 inp = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_ep), struct sctp_inpcb);
2481 SCTP_PRINTF("Out of SCTP-INPCB structures - no resources\n");
2482 SCTP_INP_INFO_WUNLOCK();
2483 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
2487 bzero(inp, sizeof(*inp));
2489 /* bump generations */
2490 /* setup socket pointers */
2491 inp->sctp_socket = so;
2492 inp->ip_inp.inp.inp_socket = so;
2493 inp->sctp_associd_counter = 1;
2494 inp->partial_delivery_point = SCTP_SB_LIMIT_RCV(so) >> SCTP_PARTIAL_DELIVERY_SHIFT;
2495 inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
2496 inp->sctp_cmt_on_off = SCTP_BASE_SYSCTL(sctp_cmt_on_off);
2497 inp->sctp_ecn_enable = SCTP_BASE_SYSCTL(sctp_ecn_enable);
2498 /* init the small hash table we use to track asocid <-> tcb */
2499 inp->sctp_asocidhash = SCTP_HASH_INIT(SCTP_STACK_VTAG_HASH_SIZE, &inp->hashasocidmark);
2500 if (inp->sctp_asocidhash == NULL) {
2501 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2502 SCTP_INP_INFO_WUNLOCK();
2507 struct inpcbpolicy *pcb_sp = NULL;
2509 error = ipsec_init_policy(so, &pcb_sp);
2510 /* Arrange to share the policy */
2511 inp->ip_inp.inp.inp_sp = pcb_sp;
2512 ((struct in6pcb *)(&inp->ip_inp.inp))->in6p_sp = pcb_sp;
2515 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2516 SCTP_INP_INFO_WUNLOCK();
2520 SCTP_INCR_EP_COUNT();
2521 inp->ip_inp.inp.inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
2522 SCTP_INP_INFO_WUNLOCK();
2524 so->so_pcb = (caddr_t)inp;
2526 if ((SCTP_SO_TYPE(so) == SOCK_DGRAM) ||
2527 (SCTP_SO_TYPE(so) == SOCK_SEQPACKET)) {
2528 /* UDP style socket */
2529 inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
2530 SCTP_PCB_FLAGS_UNBOUND);
2531 /* Be sure it is NON-BLOCKING IO for UDP */
2532 /* SCTP_SET_SO_NBIO(so); */
2533 } else if (SCTP_SO_TYPE(so) == SOCK_STREAM) {
2534 /* TCP style socket */
2535 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2536 SCTP_PCB_FLAGS_UNBOUND);
2537 /* Be sure we have blocking IO by default */
2538 SCTP_CLEAR_SO_NBIO(so);
2541 * unsupported socket type (RAW, etc)- in case we missed it
2544 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EOPNOTSUPP);
2546 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2547 return (EOPNOTSUPP);
2549 if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_1) {
2550 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2551 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2552 } else if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_2) {
2553 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2554 sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2555 } else if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_0) {
2556 sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2557 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2559 inp->sctp_tcbhash = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_pcbtblsize),
2560 &inp->sctp_hashmark);
2561 if (inp->sctp_tcbhash == NULL) {
2562 SCTP_PRINTF("Out of SCTP-INPCB->hashinit - no resources\n");
2563 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
2565 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2568 inp->def_vrf_id = vrf_id;
2570 SCTP_INP_INFO_WLOCK();
2571 SCTP_INP_LOCK_INIT(inp);
2572 INP_LOCK_INIT(&inp->ip_inp.inp, "inp", "sctpinp");
2573 SCTP_INP_READ_INIT(inp);
2574 SCTP_ASOC_CREATE_LOCK_INIT(inp);
2575 /* lock the new ep */
2576 SCTP_INP_WLOCK(inp);
2578 /* add it to the info area */
2579 LIST_INSERT_HEAD(&SCTP_BASE_INFO(listhead), inp, sctp_list);
2580 SCTP_INP_INFO_WUNLOCK();
2582 TAILQ_INIT(&inp->read_queue);
2583 LIST_INIT(&inp->sctp_addr_list);
2585 LIST_INIT(&inp->sctp_asoc_list);
2587 #ifdef SCTP_TRACK_FREED_ASOCS
2589 LIST_INIT(&inp->sctp_asoc_free_list);
2591 /* Init the timer structure for signature change */
2592 SCTP_OS_TIMER_INIT(&inp->sctp_ep.signature_change.timer);
2593 inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NEWCOOKIE;
2595 /* now init the actual endpoint default data */
2598 /* setup the base timeout information */
2599 m->sctp_timeoutticks[SCTP_TIMER_SEND] = SEC_TO_TICKS(SCTP_SEND_SEC); /* needed ? */
2600 m->sctp_timeoutticks[SCTP_TIMER_INIT] = SEC_TO_TICKS(SCTP_INIT_SEC); /* needed ? */
2601 m->sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_delayed_sack_time_default));
2602 m->sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_heartbeat_interval_default));
2603 m->sctp_timeoutticks[SCTP_TIMER_PMTU] = SEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_pmtu_raise_time_default));
2604 m->sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] = SEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_shutdown_guard_time_default));
2605 m->sctp_timeoutticks[SCTP_TIMER_SIGNATURE] = SEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_secret_lifetime_default));
2606 /* all max/min max are in ms */
2607 m->sctp_maxrto = SCTP_BASE_SYSCTL(sctp_rto_max_default);
2608 m->sctp_minrto = SCTP_BASE_SYSCTL(sctp_rto_min_default);
2609 m->initial_rto = SCTP_BASE_SYSCTL(sctp_rto_initial_default);
2610 m->initial_init_rto_max = SCTP_BASE_SYSCTL(sctp_init_rto_max_default);
2611 m->sctp_sack_freq = SCTP_BASE_SYSCTL(sctp_sack_freq_default);
2613 m->max_open_streams_intome = MAX_SCTP_STREAMS;
2615 m->max_init_times = SCTP_BASE_SYSCTL(sctp_init_rtx_max_default);
2616 m->max_send_times = SCTP_BASE_SYSCTL(sctp_assoc_rtx_max_default);
2617 m->def_net_failure = SCTP_BASE_SYSCTL(sctp_path_rtx_max_default);
2618 m->sctp_sws_sender = SCTP_SWS_SENDER_DEF;
2619 m->sctp_sws_receiver = SCTP_SWS_RECEIVER_DEF;
2620 m->max_burst = SCTP_BASE_SYSCTL(sctp_max_burst_default);
2621 m->fr_max_burst = SCTP_BASE_SYSCTL(sctp_fr_max_burst_default);
2623 m->sctp_default_cc_module = SCTP_BASE_SYSCTL(sctp_default_cc_module);
2624 m->sctp_default_ss_module = SCTP_BASE_SYSCTL(sctp_default_ss_module);
2625 /* number of streams to pre-open on a association */
2626 m->pre_open_stream_count = SCTP_BASE_SYSCTL(sctp_nr_outgoing_streams_default);
2628 /* Add adaptation cookie */
2629 m->adaptation_layer_indicator = 0x504C5253;
2631 /* seed random number generator */
2632 m->random_counter = 1;
2633 m->store_at = SCTP_SIGNATURE_SIZE;
2634 SCTP_READ_RANDOM(m->random_numbers, sizeof(m->random_numbers));
2635 sctp_fill_random_store(m);
2637 /* Minimum cookie size */
2638 m->size_of_a_cookie = (sizeof(struct sctp_init_msg) * 2) +
2639 sizeof(struct sctp_state_cookie);
2640 m->size_of_a_cookie += SCTP_SIGNATURE_SIZE;
2642 /* Setup the initial secret */
2643 (void)SCTP_GETTIME_TIMEVAL(&time);
2644 m->time_of_secret_change = time.tv_sec;
2646 for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
2647 m->secret_key[0][i] = sctp_select_initial_TSN(m);
2649 sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL);
2651 /* How long is a cookie good for ? */
2652 m->def_cookie_life = MSEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_valid_cookie_life_default));
2654 * Initialize authentication parameters
2656 m->local_hmacs = sctp_default_supported_hmaclist();
2657 m->local_auth_chunks = sctp_alloc_chunklist();
2658 sctp_auth_set_default_chunks(m->local_auth_chunks);
2659 LIST_INIT(&m->shared_keys);
2660 /* add default NULL key as key id 0 */
2661 null_key = sctp_alloc_sharedkey();
2662 sctp_insert_sharedkey(&m->shared_keys, null_key);
2663 SCTP_INP_WUNLOCK(inp);
2664 #ifdef SCTP_LOG_CLOSING
2665 sctp_log_closing(inp, NULL, 12);
2672 sctp_move_pcb_and_assoc(struct sctp_inpcb *old_inp, struct sctp_inpcb *new_inp,
2673 struct sctp_tcb *stcb)
2675 struct sctp_nets *net;
2676 uint16_t lport, rport;
2677 struct sctppcbhead *head;
2678 struct sctp_laddr *laddr, *oladdr;
2680 atomic_add_int(&stcb->asoc.refcnt, 1);
2681 SCTP_TCB_UNLOCK(stcb);
2682 SCTP_INP_INFO_WLOCK();
2683 SCTP_INP_WLOCK(old_inp);
2684 SCTP_INP_WLOCK(new_inp);
2685 SCTP_TCB_LOCK(stcb);
2686 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2688 new_inp->sctp_ep.time_of_secret_change =
2689 old_inp->sctp_ep.time_of_secret_change;
2690 memcpy(new_inp->sctp_ep.secret_key, old_inp->sctp_ep.secret_key,
2691 sizeof(old_inp->sctp_ep.secret_key));
2692 new_inp->sctp_ep.current_secret_number =
2693 old_inp->sctp_ep.current_secret_number;
2694 new_inp->sctp_ep.last_secret_number =
2695 old_inp->sctp_ep.last_secret_number;
2696 new_inp->sctp_ep.size_of_a_cookie = old_inp->sctp_ep.size_of_a_cookie;
2698 /* make it so new data pours into the new socket */
2699 stcb->sctp_socket = new_inp->sctp_socket;
2700 stcb->sctp_ep = new_inp;
2702 /* Copy the port across */
2703 lport = new_inp->sctp_lport = old_inp->sctp_lport;
2704 rport = stcb->rport;
2705 /* Pull the tcb from the old association */
2706 LIST_REMOVE(stcb, sctp_tcbhash);
2707 LIST_REMOVE(stcb, sctp_tcblist);
2708 if (stcb->asoc.in_asocid_hash) {
2709 LIST_REMOVE(stcb, sctp_tcbasocidhash);
2711 /* Now insert the new_inp into the TCP connected hash */
2712 head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR((lport | rport), SCTP_BASE_INFO(hashtcpmark))];
2714 LIST_INSERT_HEAD(head, new_inp, sctp_hash);
2715 /* Its safe to access */
2716 new_inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
2718 /* Now move the tcb into the endpoint list */
2719 LIST_INSERT_HEAD(&new_inp->sctp_asoc_list, stcb, sctp_tcblist);
2721 * Question, do we even need to worry about the ep-hash since we
2722 * only have one connection? Probably not :> so lets get rid of it
2723 * and not suck up any kernel memory in that.
2725 if (stcb->asoc.in_asocid_hash) {
2726 struct sctpasochead *lhd;
2728 lhd = &new_inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(stcb->asoc.assoc_id,
2729 new_inp->hashasocidmark)];
2730 LIST_INSERT_HEAD(lhd, stcb, sctp_tcbasocidhash);
2732 /* Ok. Let's restart timer. */
2733 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2734 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, new_inp,
2738 SCTP_INP_INFO_WUNLOCK();
2739 if (new_inp->sctp_tcbhash != NULL) {
2740 SCTP_HASH_FREE(new_inp->sctp_tcbhash, new_inp->sctp_hashmark);
2741 new_inp->sctp_tcbhash = NULL;
2743 if ((new_inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
2744 /* Subset bound, so copy in the laddr list from the old_inp */
2745 LIST_FOREACH(oladdr, &old_inp->sctp_addr_list, sctp_nxt_addr) {
2746 laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
2747 if (laddr == NULL) {
2749 * Gak, what can we do? This assoc is really
2750 * HOSED. We probably should send an abort
2753 SCTPDBG(SCTP_DEBUG_PCB1, "Association hosed in TCP model, out of laddr memory\n");
2756 SCTP_INCR_LADDR_COUNT();
2757 bzero(laddr, sizeof(*laddr));
2758 (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time);
2759 laddr->ifa = oladdr->ifa;
2760 atomic_add_int(&laddr->ifa->refcount, 1);
2761 LIST_INSERT_HEAD(&new_inp->sctp_addr_list, laddr,
2763 new_inp->laddr_count++;
2767 * Now any running timers need to be adjusted since we really don't
2768 * care if they are running or not just blast in the new_inp into
2772 stcb->asoc.hb_timer.ep = (void *)new_inp;
2773 stcb->asoc.dack_timer.ep = (void *)new_inp;
2774 stcb->asoc.asconf_timer.ep = (void *)new_inp;
2775 stcb->asoc.strreset_timer.ep = (void *)new_inp;
2776 stcb->asoc.shut_guard_timer.ep = (void *)new_inp;
2777 stcb->asoc.autoclose_timer.ep = (void *)new_inp;
2778 stcb->asoc.delayed_event_timer.ep = (void *)new_inp;
2779 stcb->asoc.delete_prim_timer.ep = (void *)new_inp;
2780 /* now what about the nets? */
2781 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2782 net->pmtu_timer.ep = (void *)new_inp;
2783 net->rxt_timer.ep = (void *)new_inp;
2784 net->fr_timer.ep = (void *)new_inp;
2786 SCTP_INP_WUNLOCK(new_inp);
2787 SCTP_INP_WUNLOCK(old_inp);
2793 /* sctp_ifap is used to bypass normal local address validation checks */
2795 sctp_inpcb_bind(struct socket *so, struct sockaddr *addr,
2796 struct sctp_ifa *sctp_ifap, struct thread *p)
2798 /* bind a ep to a socket address */
2799 struct sctppcbhead *head;
2800 struct sctp_inpcb *inp, *inp_tmp;
2801 struct inpcb *ip_inp;
2802 int port_reuse_active = 0;
2811 inp = (struct sctp_inpcb *)so->so_pcb;
2812 ip_inp = (struct inpcb *)so->so_pcb;
2815 SCTPDBG(SCTP_DEBUG_PCB1, "Bind called port:%d\n",
2816 ntohs(((struct sockaddr_in *)addr)->sin_port));
2817 SCTPDBG(SCTP_DEBUG_PCB1, "Addr :");
2818 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr);
2821 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
2822 /* already did a bind, subsequent binds NOT allowed ! */
2823 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
2828 panic("null proc/thread");
2831 switch (addr->sa_family) {
2835 struct sockaddr_in *sin;
2837 /* IPV6_V6ONLY socket? */
2838 if (SCTP_IPV6_V6ONLY(ip_inp)) {
2839 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
2842 if (addr->sa_len != sizeof(*sin)) {
2843 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
2846 sin = (struct sockaddr_in *)addr;
2847 lport = sin->sin_port;
2849 * For LOOPBACK the prison_local_ip4() call
2850 * will transmute the ip address to the
2853 if (p && (error = prison_local_ip4(p->td_ucred, &sin->sin_addr)) != 0) {
2854 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
2857 if (sin->sin_addr.s_addr != INADDR_ANY) {
2867 * Only for pure IPv6 Address. (No IPv4
2870 struct sockaddr_in6 *sin6;
2872 sin6 = (struct sockaddr_in6 *)addr;
2874 if (addr->sa_len != sizeof(*sin6)) {
2875 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
2878 lport = sin6->sin6_port;
2881 * For LOOPBACK the prison_local_ip6() call
2882 * will transmute the ipv6 address to the
2885 if (p && (error = prison_local_ip6(p->td_ucred, &sin6->sin6_addr,
2886 (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
2887 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
2890 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2892 /* KAME hack: embed scopeid */
2893 if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
2894 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
2898 /* this must be cleared for ifa_ifwithaddr() */
2899 sin6->sin6_scope_id = 0;
2904 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EAFNOSUPPORT);
2905 return (EAFNOSUPPORT);
2908 SCTP_INP_INFO_WLOCK();
2909 SCTP_INP_WLOCK(inp);
2910 /* Setup a vrf_id to be the default for the non-bind-all case. */
2911 vrf_id = inp->def_vrf_id;
2913 /* increase our count due to the unlock we do */
2914 SCTP_INP_INCR_REF(inp);
2917 * Did the caller specify a port? if so we must see if a ep
2918 * already has this one bound.
2920 /* got to be root to get at low ports */
2921 if (ntohs(lport) < IPPORT_RESERVED) {
2923 priv_check(p, PRIV_NETINET_RESERVEDPORT)
2925 SCTP_INP_DECR_REF(inp);
2926 SCTP_INP_WUNLOCK(inp);
2927 SCTP_INP_INFO_WUNLOCK();
2932 SCTP_INP_DECR_REF(inp);
2933 SCTP_INP_WUNLOCK(inp);
2934 SCTP_INP_INFO_WUNLOCK();
2935 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
2938 SCTP_INP_WUNLOCK(inp);
2940 vrf_id = inp->def_vrf_id;
2941 inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id);
2942 if (inp_tmp != NULL) {
2944 * lock guy returned and lower count note
2945 * that we are not bound so inp_tmp should
2946 * NEVER be inp. And it is this inp
2947 * (inp_tmp) that gets the reference bump,
2948 * so we must lower it.
2950 SCTP_INP_DECR_REF(inp_tmp);
2952 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
2953 (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
2955 * Ok, must be one-2-one and
2956 * allowing port re-use
2958 port_reuse_active = 1;
2959 goto continue_anyway;
2961 SCTP_INP_DECR_REF(inp);
2962 SCTP_INP_INFO_WUNLOCK();
2963 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE);
2964 return (EADDRINUSE);
2967 inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id);
2968 if (inp_tmp != NULL) {
2970 * lock guy returned and lower count note
2971 * that we are not bound so inp_tmp should
2972 * NEVER be inp. And it is this inp
2973 * (inp_tmp) that gets the reference bump,
2974 * so we must lower it.
2976 SCTP_INP_DECR_REF(inp_tmp);
2978 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
2979 (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
2981 * Ok, must be one-2-one and
2982 * allowing port re-use
2984 port_reuse_active = 1;
2985 goto continue_anyway;
2987 SCTP_INP_DECR_REF(inp);
2988 SCTP_INP_INFO_WUNLOCK();
2989 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE);
2990 return (EADDRINUSE);
2994 SCTP_INP_WLOCK(inp);
2996 /* verify that no lport is not used by a singleton */
2997 if ((port_reuse_active == 0) &&
2998 (inp_tmp = sctp_isport_inuse(inp, lport, vrf_id))
3000 /* Sorry someone already has this one bound */
3001 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
3002 (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
3003 port_reuse_active = 1;
3005 SCTP_INP_DECR_REF(inp);
3006 SCTP_INP_WUNLOCK(inp);
3007 SCTP_INP_INFO_WUNLOCK();
3008 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE);
3009 return (EADDRINUSE);
3014 uint16_t first, last, candidate;
3018 if (ip_inp->inp_flags & INP_HIGHPORT) {
3019 first = MODULE_GLOBAL(ipport_hifirstauto);
3020 last = MODULE_GLOBAL(ipport_hilastauto);
3021 } else if (ip_inp->inp_flags & INP_LOWPORT) {
3023 priv_check(p, PRIV_NETINET_RESERVEDPORT)
3025 SCTP_INP_DECR_REF(inp);
3026 SCTP_INP_WUNLOCK(inp);
3027 SCTP_INP_INFO_WUNLOCK();
3028 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3031 first = MODULE_GLOBAL(ipport_lowfirstauto);
3032 last = MODULE_GLOBAL(ipport_lowlastauto);
3034 first = MODULE_GLOBAL(ipport_firstauto);
3035 last = MODULE_GLOBAL(ipport_lastauto);
3044 count = last - first + 1; /* number of candidates */
3045 candidate = first + sctp_select_initial_TSN(&inp->sctp_ep) % (count);
3049 if (sctp_isport_inuse(inp, htons(candidate), inp->def_vrf_id) == NULL) {
3054 SCTP_INP_DECR_REF(inp);
3055 SCTP_INP_WUNLOCK(inp);
3056 SCTP_INP_INFO_WUNLOCK();
3057 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE);
3058 return (EADDRINUSE);
3060 if (candidate == last)
3063 candidate = candidate + 1;
3066 lport = htons(candidate);
3068 SCTP_INP_DECR_REF(inp);
3069 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE |
3070 SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
3072 * this really should not happen. The guy did a non-blocking
3073 * bind and then did a close at the same time.
3075 SCTP_INP_WUNLOCK(inp);
3076 SCTP_INP_INFO_WUNLOCK();
3077 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3080 /* ok we look clear to give out this port, so lets setup the binding */
3082 /* binding to all addresses, so just set in the proper flags */
3083 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUNDALL;
3084 /* set the automatic addr changes from kernel flag */
3085 if (SCTP_BASE_SYSCTL(sctp_auto_asconf) == 0) {
3086 sctp_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF);
3087 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
3089 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
3090 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
3092 if (SCTP_BASE_SYSCTL(sctp_multiple_asconfs) == 0) {
3093 sctp_feature_off(inp, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS);
3095 sctp_feature_on(inp, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS);
3098 * set the automatic mobility_base from kernel flag (by
3101 if (SCTP_BASE_SYSCTL(sctp_mobility_base) == 0) {
3102 sctp_mobility_feature_off(inp, SCTP_MOBILITY_BASE);
3103 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
3105 sctp_mobility_feature_on(inp, SCTP_MOBILITY_BASE);
3106 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
3109 * set the automatic mobility_fasthandoff from kernel flag
3112 if (SCTP_BASE_SYSCTL(sctp_mobility_fasthandoff) == 0) {
3113 sctp_mobility_feature_off(inp, SCTP_MOBILITY_FASTHANDOFF);
3114 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
3116 sctp_mobility_feature_on(inp, SCTP_MOBILITY_FASTHANDOFF);
3117 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
3121 * bind specific, make sure flags is off and add a new
3122 * address structure to the sctp_addr_list inside the ep
3125 * We will need to allocate one and insert it at the head. The
3126 * socketopt call can just insert new addresses in there as
3127 * well. It will also have to do the embed scope kame hack
3128 * too (before adding).
3130 struct sctp_ifa *ifa;
3131 struct sockaddr_storage store_sa;
3133 memset(&store_sa, 0, sizeof(store_sa));
3134 switch (addr->sa_family) {
3137 struct sockaddr_in *sin;
3139 sin = (struct sockaddr_in *)&store_sa;
3140 memcpy(sin, addr, sizeof(struct sockaddr_in));
3146 struct sockaddr_in6 *sin6;
3148 sin6 = (struct sockaddr_in6 *)&store_sa;
3149 memcpy(sin6, addr, sizeof(struct sockaddr_in6));
3150 sin6->sin6_port = 0;
3157 * first find the interface with the bound address need to
3158 * zero out the port to find the address! yuck! can't do
3159 * this earlier since need port for sctp_pcb_findep()
3161 if (sctp_ifap != NULL)
3165 * Note for BSD we hit here always other O/S's will
3166 * pass things in via the sctp_ifap argument
3169 ifa = sctp_find_ifa_by_addr((struct sockaddr *)&store_sa,
3170 vrf_id, SCTP_ADDR_NOT_LOCKED);
3173 /* Can't find an interface with that address */
3174 SCTP_INP_WUNLOCK(inp);
3175 SCTP_INP_INFO_WUNLOCK();
3176 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRNOTAVAIL);
3177 return (EADDRNOTAVAIL);
3180 if (addr->sa_family == AF_INET6) {
3181 /* GAK, more FIXME IFA lock? */
3182 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
3183 /* Can't bind a non-existent addr. */
3184 SCTP_INP_WUNLOCK(inp);
3185 SCTP_INP_INFO_WUNLOCK();
3186 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3191 /* we're not bound all */
3192 inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUNDALL;
3193 /* allow bindx() to send ASCONF's for binding changes */
3194 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
3195 /* clear automatic addr changes from kernel flag */
3196 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
3198 /* add this address to the endpoint list */
3199 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, 0);
3201 SCTP_INP_WUNLOCK(inp);
3202 SCTP_INP_INFO_WUNLOCK();
3207 /* find the bucket */
3208 if (port_reuse_active) {
3209 /* Put it into tcp 1-2-1 hash */
3210 head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashtcpmark))];
3211 inp->sctp_flags |= SCTP_PCB_FLAGS_IN_TCPPOOL;
3213 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashmark))];
3215 /* put it in the bucket */
3216 LIST_INSERT_HEAD(head, inp, sctp_hash);
3217 SCTPDBG(SCTP_DEBUG_PCB1, "Main hash to bind at head:%p, bound port:%d - in tcp_pool=%d\n",
3218 head, ntohs(lport), port_reuse_active);
3219 /* set in the port */
3220 inp->sctp_lport = lport;
3222 /* turn off just the unbound flag */
3223 inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
3224 SCTP_INP_WUNLOCK(inp);
3225 SCTP_INP_INFO_WUNLOCK();
3231 sctp_iterator_inp_being_freed(struct sctp_inpcb *inp)
3233 struct sctp_iterator *it, *nit;
3236 * We enter with the only the ITERATOR_LOCK in place and a write
3237 * lock on the inp_info stuff.
3239 it = sctp_it_ctl.cur_it;
3240 if (it && (it->vn != curvnet)) {
3241 /* Its not looking at our VNET */
3244 if (it && (it->inp == inp)) {
3246 * This is tricky and we hold the iterator lock, but when it
3247 * returns and gets the lock (when we release it) the
3248 * iterator will try to operate on inp. We need to stop that
3249 * from happening. But of course the iterator has a
3250 * reference on the stcb and inp. We can mark it and it will
3253 * If its a single iterator situation, we set the end iterator
3254 * flag. Otherwise we set the iterator to go to the next
3258 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
3259 sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_IT;
3261 sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_INP;
3265 * Now go through and remove any single reference to our inp that
3266 * may be still pending on the list
3268 SCTP_IPI_ITERATOR_WQ_LOCK();
3269 TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) {
3270 if (it->vn != curvnet) {
3273 if (it->inp == inp) {
3274 /* This one points to me is it inp specific? */
3275 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
3276 /* Remove and free this one */
3277 TAILQ_REMOVE(&sctp_it_ctl.iteratorhead,
3279 if (it->function_atend != NULL) {
3280 (*it->function_atend) (it->pointer, it->val);
3282 SCTP_FREE(it, SCTP_M_ITER);
3284 it->inp = LIST_NEXT(it->inp, sctp_list);
3286 SCTP_INP_INCR_REF(it->inp);
3290 * When its put in the refcnt is incremented so decr
3293 SCTP_INP_DECR_REF(inp);
3296 SCTP_IPI_ITERATOR_WQ_UNLOCK();
3299 /* release sctp_inpcb unbind the port */
3301 sctp_inpcb_free(struct sctp_inpcb *inp, int immediate, int from)
3304 * Here we free a endpoint. We must find it (if it is in the Hash
3305 * table) and remove it from there. Then we must also find it in the
3306 * overall list and remove it from there. After all removals are
3307 * complete then any timer has to be stopped. Then start the actual
3308 * freeing. a) Any local lists. b) Any associations. c) The hash of
3309 * all associations. d) finally the ep itself.
3312 struct sctp_tcb *asoc, *nasoc;
3313 struct sctp_laddr *laddr, *nladdr;
3314 struct inpcb *ip_pcb;
3316 int being_refed = 0;
3317 struct sctp_queued_to_read *sq, *nsq;
3319 sctp_sharedkey_t *shared_key, *nshared_key;
3322 #ifdef SCTP_LOG_CLOSING
3323 sctp_log_closing(inp, NULL, 0);
3325 SCTP_ITERATOR_LOCK();
3326 /* mark any iterators on the list or being processed */
3327 sctp_iterator_inp_being_freed(inp);
3328 SCTP_ITERATOR_UNLOCK();
3329 so = inp->sctp_socket;
3330 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
3331 /* been here before.. eeks.. get out of here */
3332 SCTP_PRINTF("This conflict in free SHOULD not be happening! from %d, imm %d\n", from, immediate);
3333 #ifdef SCTP_LOG_CLOSING
3334 sctp_log_closing(inp, NULL, 1);
3338 SCTP_ASOC_CREATE_LOCK(inp);
3339 SCTP_INP_INFO_WLOCK();
3341 SCTP_INP_WLOCK(inp);
3342 if (from == SCTP_CALLED_AFTER_CMPSET_OFCLOSE) {
3343 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CLOSE_IP;
3344 /* socket is gone, so no more wakeups allowed */
3345 inp->sctp_flags |= SCTP_PCB_FLAGS_DONT_WAKE;
3346 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
3347 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
3350 /* First time through we have the socket lock, after that no more. */
3351 sctp_timer_stop(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL,
3352 SCTP_FROM_SCTP_PCB + SCTP_LOC_1);
3355 sctp_m_freem(inp->control);
3356 inp->control = NULL;
3359 sctp_m_freem(inp->pkt);
3363 ip_pcb = &inp->ip_inp.inp; /* we could just cast the main pointer
3364 * here but I will be nice :> (i.e.
3366 if (immediate == SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE) {
3370 LIST_FOREACH_SAFE(asoc, &inp->sctp_asoc_list, sctp_tcblist, nasoc) {
3371 SCTP_TCB_LOCK(asoc);
3372 if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3373 /* Skip guys being freed */
3375 if (asoc->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE) {
3377 * Special case - we did not start a
3378 * kill timer on the asoc due to it
3379 * was not closed. So go ahead and
3382 asoc->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE;
3383 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, asoc, NULL);
3385 SCTP_TCB_UNLOCK(asoc);
3388 if (((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_WAIT) ||
3389 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_ECHOED)) &&
3390 (asoc->asoc.total_output_queue_size == 0)) {
3392 * If we have data in queue, we don't want
3393 * to just free since the app may have done,
3394 * send()/close or connect/send/close. And
3395 * it wants the data to get across first.
3397 /* Just abandon things in the front states */
3398 if (sctp_free_assoc(inp, asoc, SCTP_PCBFREE_NOFORCE,
3399 SCTP_FROM_SCTP_PCB + SCTP_LOC_2) == 0) {
3404 /* Disconnect the socket please */
3405 asoc->sctp_socket = NULL;
3406 asoc->asoc.state |= SCTP_STATE_CLOSED_SOCKET;
3407 if ((asoc->asoc.size_on_reasm_queue > 0) ||
3408 (asoc->asoc.control_pdapi) ||
3409 (asoc->asoc.size_on_all_streams > 0) ||
3410 (so && (so->so_rcv.sb_cc > 0))
3412 /* Left with Data unread */
3413 struct mbuf *op_err;
3415 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
3416 0, M_DONTWAIT, 1, MT_DATA);
3418 /* Fill in the user initiated abort */
3419 struct sctp_paramhdr *ph;
3422 SCTP_BUF_LEN(op_err) =
3423 sizeof(struct sctp_paramhdr) + sizeof(uint32_t);
3425 struct sctp_paramhdr *);
3426 ph->param_type = htons(
3427 SCTP_CAUSE_USER_INITIATED_ABT);
3428 ph->param_length = htons(SCTP_BUF_LEN(op_err));
3429 ippp = (uint32_t *) (ph + 1);
3430 *ippp = htonl(SCTP_FROM_SCTP_PCB + SCTP_LOC_3);
3432 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_3;
3433 #if defined(SCTP_PANIC_ON_ABORT)
3434 panic("inpcb_free does an abort");
3436 sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED);
3437 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3438 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
3439 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3440 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3442 if (sctp_free_assoc(inp, asoc,
3443 SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_4) == 0) {
3447 } else if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
3448 TAILQ_EMPTY(&asoc->asoc.sent_queue) &&
3449 (asoc->asoc.stream_queue_cnt == 0)
3451 if (asoc->asoc.locked_on_sending) {
3454 if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
3455 (SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
3457 * there is nothing queued to send,
3458 * so I send shutdown
3460 sctp_send_shutdown(asoc, asoc->asoc.primary_destination);
3461 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
3462 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3463 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3465 SCTP_SET_STATE(&asoc->asoc, SCTP_STATE_SHUTDOWN_SENT);
3466 SCTP_CLEAR_SUBSTATE(&asoc->asoc, SCTP_STATE_SHUTDOWN_PENDING);
3467 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, asoc->sctp_ep, asoc,
3468 asoc->asoc.primary_destination);
3469 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc,
3470 asoc->asoc.primary_destination);
3471 sctp_chunk_output(inp, asoc, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_LOCKED);
3474 /* mark into shutdown pending */
3475 struct sctp_stream_queue_pending *sp;
3477 asoc->asoc.state |= SCTP_STATE_SHUTDOWN_PENDING;
3478 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc,
3479 asoc->asoc.primary_destination);
3480 if (asoc->asoc.locked_on_sending) {
3481 sp = TAILQ_LAST(&((asoc->asoc.locked_on_sending)->outqueue),
3484 SCTP_PRINTF("Error, sp is NULL, locked on sending is %p strm:%d\n",
3485 asoc->asoc.locked_on_sending,
3486 asoc->asoc.locked_on_sending->stream_no);
3488 if ((sp->length == 0) && (sp->msg_is_complete == 0))
3489 asoc->asoc.state |= SCTP_STATE_PARTIAL_MSG_LEFT;
3492 if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
3493 TAILQ_EMPTY(&asoc->asoc.sent_queue) &&
3494 (asoc->asoc.state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
3495 struct mbuf *op_err;
3498 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
3499 0, M_DONTWAIT, 1, MT_DATA);
3505 struct sctp_paramhdr *ph;
3508 SCTP_BUF_LEN(op_err) =
3509 (sizeof(struct sctp_paramhdr) +
3512 struct sctp_paramhdr *);
3513 ph->param_type = htons(
3514 SCTP_CAUSE_USER_INITIATED_ABT);
3515 ph->param_length = htons(SCTP_BUF_LEN(op_err));
3516 ippp = (uint32_t *) (ph + 1);
3517 *ippp = htonl(SCTP_FROM_SCTP_PCB + SCTP_LOC_5);
3519 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_5;
3520 #if defined(SCTP_PANIC_ON_ABORT)
3521 panic("inpcb_free does an abort");
3524 sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED);
3525 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3526 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
3527 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3528 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3530 if (sctp_free_assoc(inp, asoc,
3531 SCTP_PCBFREE_NOFORCE,
3532 SCTP_FROM_SCTP_PCB + SCTP_LOC_6) == 0) {
3537 sctp_chunk_output(inp, asoc, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
3541 SCTP_TCB_UNLOCK(asoc);
3543 /* now is there some left in our SHUTDOWN state? */
3545 #ifdef SCTP_LOG_CLOSING
3546 sctp_log_closing(inp, NULL, 2);
3548 inp->sctp_socket = NULL;
3549 SCTP_INP_WUNLOCK(inp);
3550 SCTP_ASOC_CREATE_UNLOCK(inp);
3551 SCTP_INP_INFO_WUNLOCK();
3555 inp->sctp_socket = NULL;
3556 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) !=
3557 SCTP_PCB_FLAGS_UNBOUND) {
3559 * ok, this guy has been bound. It's port is somewhere in
3560 * the SCTP_BASE_INFO(hash table). Remove it!
3562 LIST_REMOVE(inp, sctp_hash);
3563 inp->sctp_flags |= SCTP_PCB_FLAGS_UNBOUND;
3566 * If there is a timer running to kill us, forget it, since it may
3567 * have a contest on the INP lock.. which would cause us to die ...
3570 LIST_FOREACH_SAFE(asoc, &inp->sctp_asoc_list, sctp_tcblist, nasoc) {
3571 SCTP_TCB_LOCK(asoc);
3572 if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3573 if (asoc->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE) {
3574 asoc->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE;
3575 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, asoc, NULL);
3578 SCTP_TCB_UNLOCK(asoc);
3581 /* Free associations that are NOT killing us */
3582 if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_COOKIE_WAIT) &&
3583 ((asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0)) {
3584 struct mbuf *op_err;
3587 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
3588 0, M_DONTWAIT, 1, MT_DATA);
3590 /* Fill in the user initiated abort */
3591 struct sctp_paramhdr *ph;
3593 SCTP_BUF_LEN(op_err) = (sizeof(struct sctp_paramhdr) +
3595 ph = mtod(op_err, struct sctp_paramhdr *);
3596 ph->param_type = htons(
3597 SCTP_CAUSE_USER_INITIATED_ABT);
3598 ph->param_length = htons(SCTP_BUF_LEN(op_err));
3599 ippp = (uint32_t *) (ph + 1);
3600 *ippp = htonl(SCTP_FROM_SCTP_PCB + SCTP_LOC_7);
3603 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_7;
3604 #if defined(SCTP_PANIC_ON_ABORT)
3605 panic("inpcb_free does an abort");
3607 sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED);
3608 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3609 } else if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3611 SCTP_TCB_UNLOCK(asoc);
3614 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
3615 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3616 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3618 if (sctp_free_assoc(inp, asoc, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_8) == 0) {
3623 /* Ok we have someone out there that will kill us */
3624 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
3625 #ifdef SCTP_LOG_CLOSING
3626 sctp_log_closing(inp, NULL, 3);
3628 SCTP_INP_WUNLOCK(inp);
3629 SCTP_ASOC_CREATE_UNLOCK(inp);
3630 SCTP_INP_INFO_WUNLOCK();
3633 if (SCTP_INP_LOCK_CONTENDED(inp))
3635 if (SCTP_INP_READ_CONTENDED(inp))
3637 if (SCTP_ASOC_CREATE_LOCK_CONTENDED(inp))
3640 if ((inp->refcount) ||
3642 (inp->sctp_flags & SCTP_PCB_FLAGS_CLOSE_IP)) {
3643 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
3644 #ifdef SCTP_LOG_CLOSING
3645 sctp_log_closing(inp, NULL, 4);
3647 sctp_timer_start(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL);
3648 SCTP_INP_WUNLOCK(inp);
3649 SCTP_ASOC_CREATE_UNLOCK(inp);
3650 SCTP_INP_INFO_WUNLOCK();
3653 inp->sctp_ep.signature_change.type = 0;
3654 inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_ALLGONE;
3656 * Remove it from the list .. last thing we need a lock for.
3658 LIST_REMOVE(inp, sctp_list);
3659 SCTP_INP_WUNLOCK(inp);
3660 SCTP_ASOC_CREATE_UNLOCK(inp);
3661 SCTP_INP_INFO_WUNLOCK();
3663 * Now we release all locks. Since this INP cannot be found anymore
3664 * except possibly by the kill timer that might be running. We call
3665 * the drain function here. It should hit the case were it sees the
3666 * ACTIVE flag cleared and exit out freeing us to proceed and
3667 * destroy everything.
3669 if (from != SCTP_CALLED_FROM_INPKILL_TIMER) {
3670 (void)SCTP_OS_TIMER_STOP_DRAIN(&inp->sctp_ep.signature_change.timer);
3672 /* Probably un-needed */
3673 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
3676 #ifdef SCTP_LOG_CLOSING
3677 sctp_log_closing(inp, NULL, 5);
3681 if ((inp->sctp_asocidhash) != NULL) {
3682 SCTP_HASH_FREE(inp->sctp_asocidhash, inp->hashasocidmark);
3683 inp->sctp_asocidhash = NULL;
3685 /* sa_ignore FREED_MEMORY */
3686 TAILQ_FOREACH_SAFE(sq, &inp->read_queue, next, nsq) {
3687 /* Its only abandoned if it had data left */
3689 SCTP_STAT_INCR(sctps_left_abandon);
3691 TAILQ_REMOVE(&inp->read_queue, sq, next);
3692 sctp_free_remote_addr(sq->whoFrom);
3694 so->so_rcv.sb_cc -= sq->length;
3696 sctp_m_freem(sq->data);
3700 * no need to free the net count, since at this point all
3703 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), sq);
3704 SCTP_DECR_READQ_COUNT();
3706 /* Now the sctp_pcb things */
3708 * free each asoc if it is not already closed/free. we can't use the
3709 * macro here since le_next will get freed as part of the
3710 * sctp_free_assoc() call.
3715 ipsec_delete_pcbpolicy(ip_pcb);
3718 /* Unlocks not needed since the socket is gone now */
3720 if (ip_pcb->inp_options) {
3721 (void)sctp_m_free(ip_pcb->inp_options);
3722 ip_pcb->inp_options = 0;
3725 if (ip_pcb->inp_moptions) {
3726 inp_freemoptions(ip_pcb->inp_moptions);
3727 ip_pcb->inp_moptions = 0;
3732 if (ip_pcb->inp_vflag & INP_IPV6) {
3733 struct in6pcb *in6p;
3735 in6p = (struct in6pcb *)inp;
3736 ip6_freepcbopts(in6p->in6p_outputopts);
3739 ip_pcb->inp_vflag = 0;
3740 /* free up authentication fields */
3741 if (inp->sctp_ep.local_auth_chunks != NULL)
3742 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
3743 if (inp->sctp_ep.local_hmacs != NULL)
3744 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
3746 LIST_FOREACH_SAFE(shared_key, &inp->sctp_ep.shared_keys, next, nshared_key) {
3747 LIST_REMOVE(shared_key, next);
3748 sctp_free_sharedkey(shared_key);
3749 /* sa_ignore FREED_MEMORY */
3753 * if we have an address list the following will free the list of
3754 * ifaddr's that are set into this ep. Again macro limitations here,
3755 * since the LIST_FOREACH could be a bad idea.
3757 LIST_FOREACH_SAFE(laddr, &inp->sctp_addr_list, sctp_nxt_addr, nladdr) {
3758 sctp_remove_laddr(laddr);
3761 #ifdef SCTP_TRACK_FREED_ASOCS
3763 LIST_FOREACH_SAFE(asoc, &inp->sctp_asoc_free_list, sctp_tcblist, nasoc) {
3764 LIST_REMOVE(asoc, sctp_tcblist);
3765 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), asoc);
3766 SCTP_DECR_ASOC_COUNT();
3768 /* *** END TEMP CODE *** */
3770 /* Now lets see about freeing the EP hash table. */
3771 if (inp->sctp_tcbhash != NULL) {
3772 SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark);
3773 inp->sctp_tcbhash = NULL;
3775 /* Now we must put the ep memory back into the zone pool */
3776 INP_LOCK_DESTROY(&inp->ip_inp.inp);
3777 SCTP_INP_LOCK_DESTROY(inp);
3778 SCTP_INP_READ_DESTROY(inp);
3779 SCTP_ASOC_CREATE_LOCK_DESTROY(inp);
3780 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
3781 SCTP_DECR_EP_COUNT();
3786 sctp_findnet(struct sctp_tcb *stcb, struct sockaddr *addr)
3788 struct sctp_nets *net;
3790 /* locate the address */
3791 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3792 if (sctp_cmpaddr(addr, (struct sockaddr *)&net->ro._l_addr))
3800 sctp_is_address_on_local_host(struct sockaddr *addr, uint32_t vrf_id)
3802 struct sctp_ifa *sctp_ifa;
3804 sctp_ifa = sctp_find_ifa_by_addr(addr, vrf_id, SCTP_ADDR_NOT_LOCKED);
3813 * add's a remote endpoint address, done with the INIT/INIT-ACK as well as
3814 * when a ASCONF arrives that adds it. It will also initialize all the cwnd
3818 sctp_add_remote_addr(struct sctp_tcb *stcb, struct sockaddr *newaddr,
3819 int set_scope, int from)
3822 * The following is redundant to the same lines in the
3823 * sctp_aloc_assoc() but is needed since others call the add address
3826 struct sctp_nets *net, *netfirst;
3829 SCTPDBG(SCTP_DEBUG_PCB1, "Adding an address (from:%d) to the peer: ",
3831 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, newaddr);
3833 netfirst = sctp_findnet(stcb, newaddr);
3836 * Lie and return ok, we don't want to make the association
3837 * go away for this behavior. It will happen in the TCP
3838 * model in a connected socket. It does not reach the hash
3839 * table until after the association is built so it can't be
3840 * found. Mark as reachable, since the initial creation will
3841 * have been cleared and the NOT_IN_ASSOC flag will have
3842 * been added... and we don't want to end up removing it
3845 if (netfirst->dest_state & SCTP_ADDR_UNCONFIRMED) {
3846 netfirst->dest_state = (SCTP_ADDR_REACHABLE |
3847 SCTP_ADDR_UNCONFIRMED);
3849 netfirst->dest_state = SCTP_ADDR_REACHABLE;
3855 switch (newaddr->sa_family) {
3859 struct sockaddr_in *sin;
3861 sin = (struct sockaddr_in *)newaddr;
3862 if (sin->sin_addr.s_addr == 0) {
3863 /* Invalid address */
3866 /* zero out the bzero area */
3867 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
3869 /* assure len is set */
3870 sin->sin_len = sizeof(struct sockaddr_in);
3872 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
3873 stcb->ipv4_local_scope = 1;
3875 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
3876 stcb->asoc.ipv4_local_scope = 1;
3878 #endif /* SCTP_DONT_DO_PRIVADDR_SCOPE */
3880 /* Validate the address is in scope */
3881 if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) &&
3882 (stcb->asoc.ipv4_local_scope == 0)) {
3892 struct sockaddr_in6 *sin6;
3894 sin6 = (struct sockaddr_in6 *)newaddr;
3895 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3896 /* Invalid address */
3899 /* assure len is set */
3900 sin6->sin6_len = sizeof(struct sockaddr_in6);
3902 if (sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id)) {
3903 stcb->asoc.loopback_scope = 1;
3904 stcb->asoc.local_scope = 0;
3905 stcb->asoc.ipv4_local_scope = 1;
3906 stcb->asoc.site_scope = 1;
3907 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
3909 * If the new destination is a
3910 * LINK_LOCAL we must have common
3911 * site scope. Don't set the local
3912 * scope since we may not share all
3913 * links, only loopback can do this.
3914 * Links on the local network would
3915 * also be on our private network
3918 stcb->asoc.ipv4_local_scope = 1;
3919 stcb->asoc.site_scope = 1;
3920 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
3922 * If the new destination is
3923 * SITE_LOCAL then we must have site
3926 stcb->asoc.site_scope = 1;
3929 /* Validate the address is in scope */
3930 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) &&
3931 (stcb->asoc.loopback_scope == 0)) {
3933 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) &&
3934 (stcb->asoc.local_scope == 0)) {
3936 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) &&
3937 (stcb->asoc.site_scope == 0)) {
3945 /* not supported family type */
3948 net = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_net), struct sctp_nets);
3952 SCTP_INCR_RADDR_COUNT();
3953 bzero(net, sizeof(*net));
3954 (void)SCTP_GETTIME_TIMEVAL(&net->start_time);
3955 memcpy(&net->ro._l_addr, newaddr, newaddr->sa_len);
3956 switch (newaddr->sa_family) {
3959 ((struct sockaddr_in *)&net->ro._l_addr)->sin_port = stcb->rport;
3964 ((struct sockaddr_in6 *)&net->ro._l_addr)->sin6_port = stcb->rport;
3970 net->addr_is_local = sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id);
3971 if (net->addr_is_local && ((set_scope || (from == SCTP_ADDR_IS_CONFIRMED)))) {
3972 stcb->asoc.loopback_scope = 1;
3973 stcb->asoc.ipv4_local_scope = 1;
3974 stcb->asoc.local_scope = 0;
3975 stcb->asoc.site_scope = 1;
3978 net->failure_threshold = stcb->asoc.def_net_failure;
3979 if (addr_inscope == 0) {
3980 net->dest_state = (SCTP_ADDR_REACHABLE |
3981 SCTP_ADDR_OUT_OF_SCOPE);
3983 if (from == SCTP_ADDR_IS_CONFIRMED)
3984 /* SCTP_ADDR_IS_CONFIRMED is passed by connect_x */
3985 net->dest_state = SCTP_ADDR_REACHABLE;
3987 net->dest_state = SCTP_ADDR_REACHABLE |
3988 SCTP_ADDR_UNCONFIRMED;
3991 * We set this to 0, the timer code knows that this means its an
3994 net->rto_needed = 1;
3996 net->RTO_measured = 0;
3997 stcb->asoc.numnets++;
3998 *(&net->ref_count) = 1;
3999 net->cwr_window_tsn = net->last_cwr_tsn = stcb->asoc.sending_seq - 1;
4000 net->tos_flowlabel = 0;
4001 if (SCTP_BASE_SYSCTL(sctp_udp_tunneling_for_client_enable)) {
4002 net->port = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
4007 if (newaddr->sa_family == AF_INET)
4008 net->tos_flowlabel = stcb->asoc.default_tos;
4011 if (newaddr->sa_family == AF_INET6)
4012 net->tos_flowlabel = stcb->asoc.default_flowlabel;
4014 /* Init the timer structure */
4015 SCTP_OS_TIMER_INIT(&net->rxt_timer.timer);
4016 SCTP_OS_TIMER_INIT(&net->fr_timer.timer);
4017 SCTP_OS_TIMER_INIT(&net->pmtu_timer.timer);
4019 /* Now generate a route for this guy */
4021 /* KAME hack: embed scopeid */
4022 if (newaddr->sa_family == AF_INET6) {
4023 struct sockaddr_in6 *sin6;
4025 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
4026 (void)sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
4027 sin6->sin6_scope_id = 0;
4030 SCTP_RTALLOC((sctp_route_t *) & net->ro, stcb->asoc.vrf_id);
4032 if (SCTP_ROUTE_HAS_VALID_IFN(&net->ro)) {
4033 /* Get source address */
4034 net->ro._s_addr = sctp_source_address_selection(stcb->sctp_ep,
4036 (sctp_route_t *) & net->ro,
4040 /* Now get the interface MTU */
4041 if (net->ro._s_addr && net->ro._s_addr->ifn_p) {
4042 net->mtu = SCTP_GATHER_MTU_FROM_INTFC(net->ro._s_addr->ifn_p);
4046 if (net->mtu == 0) {
4048 net->mtu = SCTP_DEFAULT_MTU;
4052 rmtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, net->ro.ro_rt);
4055 * Start things off to match mtu of
4058 SCTP_SET_MTU_OF_ROUTE(&net->ro._l_addr.sa,
4059 net->ro.ro_rt, net->mtu);
4062 * we take the route mtu over the interface,
4063 * since the route may be leading out the
4064 * loopback, or a different interface.
4069 if (from == SCTP_ALLOC_ASOC) {
4070 stcb->asoc.smallest_mtu = net->mtu;
4073 net->mtu = stcb->asoc.smallest_mtu;
4076 if (newaddr->sa_family == AF_INET6) {
4077 struct sockaddr_in6 *sin6;
4079 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
4080 (void)sa6_recoverscope(sin6);
4084 net->mtu -= sizeof(struct udphdr);
4086 if (stcb->asoc.smallest_mtu > net->mtu) {
4087 stcb->asoc.smallest_mtu = net->mtu;
4089 /* JRS - Use the congestion control given in the CC module */
4090 if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL)
4091 (*stcb->asoc.cc_functions.sctp_set_initial_cc_param) (stcb, net);
4094 * CMT: CUC algo - set find_pseudo_cumack to TRUE (1) at beginning
4095 * of assoc (2005/06/27, iyengar@cis.udel.edu)
4097 net->find_pseudo_cumack = 1;
4098 net->find_rtx_pseudo_cumack = 1;
4099 net->src_addr_selected = 0;
4100 /* Choose an initial flowid. */
4101 net->flowid = stcb->asoc.my_vtag ^
4102 ntohs(stcb->rport) ^
4103 ntohs(stcb->sctp_ep->sctp_lport);
4107 netfirst = TAILQ_FIRST(&stcb->asoc.nets);
4108 if (net->ro.ro_rt == NULL) {
4109 /* Since we have no route put it at the back */
4110 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
4111 } else if (netfirst == NULL) {
4112 /* We are the first one in the pool. */
4113 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
4114 } else if (netfirst->ro.ro_rt == NULL) {
4116 * First one has NO route. Place this one ahead of the first
4119 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
4120 } else if (net->ro.ro_rt->rt_ifp != netfirst->ro.ro_rt->rt_ifp) {
4122 * This one has a different interface than the one at the
4123 * top of the list. Place it ahead.
4125 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
4128 * Ok we have the same interface as the first one. Move
4129 * forward until we find either a) one with a NULL route...
4130 * insert ahead of that b) one with a different ifp.. insert
4131 * after that. c) end of the list.. insert at the tail.
4133 struct sctp_nets *netlook;
4136 netlook = TAILQ_NEXT(netfirst, sctp_next);
4137 if (netlook == NULL) {
4138 /* End of the list */
4139 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
4141 } else if (netlook->ro.ro_rt == NULL) {
4142 /* next one has NO route */
4143 TAILQ_INSERT_BEFORE(netfirst, net, sctp_next);
4145 } else if (netlook->ro.ro_rt->rt_ifp != net->ro.ro_rt->rt_ifp) {
4146 TAILQ_INSERT_AFTER(&stcb->asoc.nets, netlook,
4152 } while (netlook != NULL);
4155 /* got to have a primary set */
4156 if (stcb->asoc.primary_destination == 0) {
4157 stcb->asoc.primary_destination = net;
4158 } else if ((stcb->asoc.primary_destination->ro.ro_rt == NULL) &&
4160 ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) {
4161 /* No route to current primary adopt new primary */
4162 stcb->asoc.primary_destination = net;
4164 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb,
4166 /* Validate primary is first */
4167 net = TAILQ_FIRST(&stcb->asoc.nets);
4168 if ((net != stcb->asoc.primary_destination) &&
4169 (stcb->asoc.primary_destination)) {
4171 * first one on the list is NOT the primary sctp_cmpaddr()
4172 * is much more efficient if the primary is the first on the
4175 TAILQ_REMOVE(&stcb->asoc.nets,
4176 stcb->asoc.primary_destination, sctp_next);
4177 TAILQ_INSERT_HEAD(&stcb->asoc.nets,
4178 stcb->asoc.primary_destination, sctp_next);
4185 sctp_aloc_a_assoc_id(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
4188 struct sctpasochead *head;
4189 struct sctp_tcb *lstcb;
4191 SCTP_INP_WLOCK(inp);
4193 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
4195 SCTP_INP_WUNLOCK(inp);
4199 * We don't allow assoc id to be one of SCTP_FUTURE_ASSOC,
4200 * SCTP_CURRENT_ASSOC and SCTP_ALL_ASSOC.
4202 if (inp->sctp_associd_counter <= SCTP_ALL_ASSOC) {
4203 inp->sctp_associd_counter = SCTP_ALL_ASSOC + 1;
4205 id = inp->sctp_associd_counter;
4206 inp->sctp_associd_counter++;
4207 lstcb = sctp_findasoc_ep_asocid_locked(inp, (sctp_assoc_t) id, 0);
4211 head = &inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(id, inp->hashasocidmark)];
4212 LIST_INSERT_HEAD(head, stcb, sctp_tcbasocidhash);
4213 stcb->asoc.in_asocid_hash = 1;
4214 SCTP_INP_WUNLOCK(inp);
4219 * allocate an association and add it to the endpoint. The caller must be
4220 * careful to add all additional addresses once they are know right away or
4221 * else the assoc will be may experience a blackout scenario.
4224 sctp_aloc_assoc(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
4225 int *error, uint32_t override_tag, uint32_t vrf_id,
4229 /* note the p argument is only valid in unbound sockets */
4231 struct sctp_tcb *stcb;
4232 struct sctp_association *asoc;
4233 struct sctpasochead *head;
4238 * Assumption made here: Caller has done a
4239 * sctp_findassociation_ep_addr(ep, addr's); to make sure the
4240 * address does not exist already.
4242 if (SCTP_BASE_INFO(ipi_count_asoc) >= SCTP_MAX_NUM_OF_ASOC) {
4243 /* Hit max assoc, sorry no more */
4244 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
4248 if (firstaddr == NULL) {
4249 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4253 SCTP_INP_RLOCK(inp);
4254 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
4255 ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) ||
4256 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
4258 * If its in the TCP pool, its NOT allowed to create an
4259 * association. The parent listener needs to call
4260 * sctp_aloc_assoc.. or the one-2-many socket. If a peeled
4261 * off, or connected one does this.. its an error.
4263 SCTP_INP_RUNLOCK(inp);
4264 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4268 SCTPDBG(SCTP_DEBUG_PCB3, "Allocate an association for peer:");
4271 SCTPDBG_ADDR(SCTP_DEBUG_PCB3, firstaddr);
4272 switch (firstaddr->sa_family) {
4275 SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
4276 ntohs(((struct sockaddr_in *)firstaddr)->sin_port));
4281 SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
4282 ntohs(((struct sockaddr_in6 *)firstaddr)->sin6_port));
4289 SCTPDBG(SCTP_DEBUG_PCB3, "None\n");
4291 #endif /* SCTP_DEBUG */
4292 switch (firstaddr->sa_family) {
4296 struct sockaddr_in *sin;
4298 sin = (struct sockaddr_in *)firstaddr;
4299 if ((sin->sin_port == 0) || (sin->sin_addr.s_addr == 0)) {
4300 /* Invalid address */
4301 SCTP_INP_RUNLOCK(inp);
4302 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4306 rport = sin->sin_port;
4313 struct sockaddr_in6 *sin6;
4315 sin6 = (struct sockaddr_in6 *)firstaddr;
4316 if ((sin6->sin6_port == 0) ||
4317 (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))) {
4318 /* Invalid address */
4319 SCTP_INP_RUNLOCK(inp);
4320 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4324 rport = sin6->sin6_port;
4329 /* not supported family type */
4330 SCTP_INP_RUNLOCK(inp);
4331 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4335 SCTP_INP_RUNLOCK(inp);
4336 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
4338 * If you have not performed a bind, then we need to do the
4339 * ephemeral bind for you.
4341 if ((err = sctp_inpcb_bind(inp->sctp_socket,
4342 (struct sockaddr *)NULL,
4343 (struct sctp_ifa *)NULL,
4346 /* bind error, probably perm */
4351 stcb = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_asoc), struct sctp_tcb);
4353 /* out of memory? */
4354 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
4358 SCTP_INCR_ASOC_COUNT();
4360 bzero(stcb, sizeof(*stcb));
4363 asoc->assoc_id = sctp_aloc_a_assoc_id(inp, stcb);
4364 SCTP_TCB_LOCK_INIT(stcb);
4365 SCTP_TCB_SEND_LOCK_INIT(stcb);
4366 stcb->rport = rport;
4367 /* setup back pointer's */
4368 stcb->sctp_ep = inp;
4369 stcb->sctp_socket = inp->sctp_socket;
4370 if ((err = sctp_init_asoc(inp, stcb, override_tag, vrf_id))) {
4372 SCTP_TCB_LOCK_DESTROY(stcb);
4373 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
4374 LIST_REMOVE(stcb, sctp_tcbasocidhash);
4375 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
4376 SCTP_DECR_ASOC_COUNT();
4381 SCTP_INP_INFO_WLOCK();
4382 SCTP_INP_WLOCK(inp);
4383 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4384 /* inpcb freed while alloc going on */
4385 SCTP_TCB_LOCK_DESTROY(stcb);
4386 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
4387 LIST_REMOVE(stcb, sctp_tcbasocidhash);
4388 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
4389 SCTP_INP_WUNLOCK(inp);
4390 SCTP_INP_INFO_WUNLOCK();
4391 SCTP_DECR_ASOC_COUNT();
4392 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4396 SCTP_TCB_LOCK(stcb);
4398 /* now that my_vtag is set, add it to the hash */
4399 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
4400 /* put it in the bucket in the vtag hash of assoc's for the system */
4401 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
4402 SCTP_INP_INFO_WUNLOCK();
4404 if ((err = sctp_add_remote_addr(stcb, firstaddr, SCTP_DO_SETSCOPE, SCTP_ALLOC_ASOC))) {
4405 /* failure.. memory error? */
4406 if (asoc->strmout) {
4407 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
4408 asoc->strmout = NULL;
4410 if (asoc->mapping_array) {
4411 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
4412 asoc->mapping_array = NULL;
4414 if (asoc->nr_mapping_array) {
4415 SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
4416 asoc->nr_mapping_array = NULL;
4418 SCTP_DECR_ASOC_COUNT();
4419 SCTP_TCB_LOCK_DESTROY(stcb);
4420 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
4421 LIST_REMOVE(stcb, sctp_tcbasocidhash);
4422 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
4423 SCTP_INP_WUNLOCK(inp);
4424 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
4428 /* Init all the timers */
4429 SCTP_OS_TIMER_INIT(&asoc->hb_timer.timer);
4430 SCTP_OS_TIMER_INIT(&asoc->dack_timer.timer);
4431 SCTP_OS_TIMER_INIT(&asoc->strreset_timer.timer);
4432 SCTP_OS_TIMER_INIT(&asoc->asconf_timer.timer);
4433 SCTP_OS_TIMER_INIT(&asoc->shut_guard_timer.timer);
4434 SCTP_OS_TIMER_INIT(&asoc->autoclose_timer.timer);
4435 SCTP_OS_TIMER_INIT(&asoc->delayed_event_timer.timer);
4436 SCTP_OS_TIMER_INIT(&asoc->delete_prim_timer.timer);
4438 LIST_INSERT_HEAD(&inp->sctp_asoc_list, stcb, sctp_tcblist);
4439 /* now file the port under the hash as well */
4440 if (inp->sctp_tcbhash != NULL) {
4441 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(stcb->rport,
4442 inp->sctp_hashmark)];
4443 LIST_INSERT_HEAD(head, stcb, sctp_tcbhash);
4445 SCTP_INP_WUNLOCK(inp);
4446 SCTPDBG(SCTP_DEBUG_PCB1, "Association %p now allocated\n", stcb);
4452 sctp_remove_net(struct sctp_tcb *stcb, struct sctp_nets *net)
4454 struct sctp_association *asoc;
4458 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
4459 if (net == asoc->primary_destination) {
4461 struct sctp_nets *lnet;
4463 lnet = TAILQ_FIRST(&asoc->nets);
4465 * Mobility adaptation Ideally, if deleted destination is
4466 * the primary, it becomes a fast retransmission trigger by
4467 * the subsequent SET PRIMARY. (by micchie)
4469 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
4470 SCTP_MOBILITY_BASE) ||
4471 sctp_is_mobility_feature_on(stcb->sctp_ep,
4472 SCTP_MOBILITY_FASTHANDOFF)) {
4473 SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: primary dst is deleting\n");
4474 if (asoc->deleted_primary != NULL) {
4475 SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: deleted primary may be already stored\n");
4478 asoc->deleted_primary = net;
4479 atomic_add_int(&net->ref_count, 1);
4480 memset(&net->lastsa, 0, sizeof(net->lastsa));
4481 memset(&net->lastsv, 0, sizeof(net->lastsv));
4482 sctp_mobility_feature_on(stcb->sctp_ep,
4483 SCTP_MOBILITY_PRIM_DELETED);
4484 sctp_timer_start(SCTP_TIMER_TYPE_PRIM_DELETED,
4485 stcb->sctp_ep, stcb, NULL);
4488 /* Try to find a confirmed primary */
4489 asoc->primary_destination = sctp_find_alternate_net(stcb, lnet, 0);
4491 if (net == asoc->last_data_chunk_from) {
4493 asoc->last_data_chunk_from = TAILQ_FIRST(&asoc->nets);
4495 if (net == asoc->last_control_chunk_from) {
4497 asoc->last_control_chunk_from = NULL;
4499 sctp_free_remote_addr(net);
4503 * remove a remote endpoint address from an association, it will fail if the
4504 * address does not exist.
4507 sctp_del_remote_addr(struct sctp_tcb *stcb, struct sockaddr *remaddr)
4510 * Here we need to remove a remote address. This is quite simple, we
4511 * first find it in the list of address for the association
4512 * (tasoc->asoc.nets) and then if it is there, we do a LIST_REMOVE
4513 * on that item. Note we do not allow it to be removed if there are
4514 * no other addresses.
4516 struct sctp_association *asoc;
4517 struct sctp_nets *net, *nnet;
4521 /* locate the address */
4522 TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) {
4523 if (net->ro._l_addr.sa.sa_family != remaddr->sa_family) {
4526 if (sctp_cmpaddr((struct sockaddr *)&net->ro._l_addr,
4528 /* we found the guy */
4529 if (asoc->numnets < 2) {
4530 /* Must have at LEAST two remote addresses */
4533 sctp_remove_net(stcb, net);
4543 sctp_delete_from_timewait(uint32_t tag, uint16_t lport, uint16_t rport)
4545 struct sctpvtaghead *chain;
4546 struct sctp_tagblock *twait_block;
4550 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
4551 if (!LIST_EMPTY(chain)) {
4552 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
4553 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
4554 if ((twait_block->vtag_block[i].v_tag == tag) &&
4555 (twait_block->vtag_block[i].lport == lport) &&
4556 (twait_block->vtag_block[i].rport == rport)) {
4557 twait_block->vtag_block[i].tv_sec_at_expire = 0;
4558 twait_block->vtag_block[i].v_tag = 0;
4559 twait_block->vtag_block[i].lport = 0;
4560 twait_block->vtag_block[i].rport = 0;
4572 sctp_is_in_timewait(uint32_t tag, uint16_t lport, uint16_t rport)
4574 struct sctpvtaghead *chain;
4575 struct sctp_tagblock *twait_block;
4579 SCTP_INP_INFO_WLOCK();
4580 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
4581 if (!LIST_EMPTY(chain)) {
4582 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
4583 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
4584 if ((twait_block->vtag_block[i].v_tag == tag) &&
4585 (twait_block->vtag_block[i].lport == lport) &&
4586 (twait_block->vtag_block[i].rport == rport)) {
4595 SCTP_INP_INFO_WUNLOCK();
4601 sctp_add_vtag_to_timewait(uint32_t tag, uint32_t time, uint16_t lport, uint16_t rport)
4603 struct sctpvtaghead *chain;
4604 struct sctp_tagblock *twait_block;
4612 (void)SCTP_GETTIME_TIMEVAL(&now);
4613 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
4615 if (!LIST_EMPTY(chain)) {
4616 /* Block(s) present, lets find space, and expire on the fly */
4617 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
4618 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
4619 if ((twait_block->vtag_block[i].v_tag == 0) &&
4621 twait_block->vtag_block[i].tv_sec_at_expire =
4623 twait_block->vtag_block[i].v_tag = tag;
4624 twait_block->vtag_block[i].lport = lport;
4625 twait_block->vtag_block[i].rport = rport;
4627 } else if ((twait_block->vtag_block[i].v_tag) &&
4628 ((long)twait_block->vtag_block[i].tv_sec_at_expire < now.tv_sec)) {
4629 /* Audit expires this guy */
4630 twait_block->vtag_block[i].tv_sec_at_expire = 0;
4631 twait_block->vtag_block[i].v_tag = 0;
4632 twait_block->vtag_block[i].lport = 0;
4633 twait_block->vtag_block[i].rport = 0;
4635 /* Reuse it for my new tag */
4636 twait_block->vtag_block[i].tv_sec_at_expire = now.tv_sec + time;
4637 twait_block->vtag_block[i].v_tag = tag;
4638 twait_block->vtag_block[i].lport = lport;
4639 twait_block->vtag_block[i].rport = rport;
4646 * We only do up to the block where we can
4647 * place our tag for audits
4653 /* Need to add a new block to chain */
4655 SCTP_MALLOC(twait_block, struct sctp_tagblock *,
4656 sizeof(struct sctp_tagblock), SCTP_M_TIMW);
4657 if (twait_block == NULL) {
4659 panic("Can not alloc tagblock");
4663 memset(twait_block, 0, sizeof(struct sctp_tagblock));
4664 LIST_INSERT_HEAD(chain, twait_block, sctp_nxt_tagblock);
4665 twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec + time;
4666 twait_block->vtag_block[0].v_tag = tag;
4667 twait_block->vtag_block[0].lport = lport;
4668 twait_block->vtag_block[0].rport = rport;
4675 * Free the association after un-hashing the remote port. This
4676 * function ALWAYS returns holding NO LOCK on the stcb. It DOES
4677 * expect that the input to this function IS a locked TCB.
4678 * It will return 0, if it did NOT destroy the association (instead
4679 * it unlocks it. It will return NON-zero if it either destroyed the
4680 * association OR the association is already destroyed.
4683 sctp_free_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int from_inpcbfree, int from_location)
4686 struct sctp_association *asoc;
4687 struct sctp_nets *net, *nnet;
4688 struct sctp_laddr *laddr, *naddr;
4689 struct sctp_tmit_chunk *chk, *nchk;
4690 struct sctp_asconf_addr *aparam, *naparam;
4691 struct sctp_asconf_ack *aack, *naack;
4692 struct sctp_stream_reset_list *strrst, *nstrrst;
4693 struct sctp_queued_to_read *sq, *nsq;
4694 struct sctp_stream_queue_pending *sp, *nsp;
4695 sctp_sharedkey_t *shared_key, *nshared_key;
4698 /* first, lets purge the entry from the hash table. */
4700 #ifdef SCTP_LOG_CLOSING
4701 sctp_log_closing(inp, stcb, 6);
4703 if (stcb->asoc.state == 0) {
4704 #ifdef SCTP_LOG_CLOSING
4705 sctp_log_closing(inp, NULL, 7);
4707 /* there is no asoc, really TSNH :-0 */
4711 if (stcb->freed_from_where == 0) {
4712 /* Only record the first place free happened from */
4713 stcb->freed_from_where = from_location;
4718 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4719 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
4720 /* nothing around */
4723 so = inp->sctp_socket;
4726 * We used timer based freeing if a reader or writer is in the way.
4727 * So we first check if we are actually being called from a timer,
4728 * if so we abort early if a reader or writer is still in the way.
4730 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) &&
4731 (from_inpcbfree == SCTP_NORMAL_PROC)) {
4733 * is it the timer driving us? if so are the reader/writers
4736 if (stcb->asoc.refcnt) {
4737 /* nope, reader or writer in the way */
4738 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
4739 /* no asoc destroyed */
4740 SCTP_TCB_UNLOCK(stcb);
4741 #ifdef SCTP_LOG_CLOSING
4742 sctp_log_closing(inp, stcb, 8);
4747 /* now clean up any other timers */
4748 (void)SCTP_OS_TIMER_STOP(&asoc->hb_timer.timer);
4749 asoc->hb_timer.self = NULL;
4750 (void)SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
4751 asoc->dack_timer.self = NULL;
4752 (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
4754 * For stream reset we don't blast this unless
4755 * it is a str-reset timer, it might be the
4756 * free-asoc timer which we DON'T want to
4759 if (asoc->strreset_timer.type == SCTP_TIMER_TYPE_STRRESET)
4760 asoc->strreset_timer.self = NULL;
4761 (void)SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
4762 asoc->asconf_timer.self = NULL;
4763 (void)SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
4764 asoc->autoclose_timer.self = NULL;
4765 (void)SCTP_OS_TIMER_STOP(&asoc->shut_guard_timer.timer);
4766 asoc->shut_guard_timer.self = NULL;
4767 (void)SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
4768 asoc->delayed_event_timer.self = NULL;
4769 /* Mobility adaptation */
4770 (void)SCTP_OS_TIMER_STOP(&asoc->delete_prim_timer.timer);
4771 asoc->delete_prim_timer.self = NULL;
4772 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4773 (void)SCTP_OS_TIMER_STOP(&net->fr_timer.timer);
4774 net->fr_timer.self = NULL;
4775 (void)SCTP_OS_TIMER_STOP(&net->rxt_timer.timer);
4776 net->rxt_timer.self = NULL;
4777 (void)SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
4778 net->pmtu_timer.self = NULL;
4780 /* Now the read queue needs to be cleaned up (only once) */
4781 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0) {
4782 stcb->asoc.state |= SCTP_STATE_ABOUT_TO_BE_FREED;
4783 SCTP_INP_READ_LOCK(inp);
4784 TAILQ_FOREACH(sq, &inp->read_queue, next) {
4785 if (sq->stcb == stcb) {
4786 sq->do_not_ref_stcb = 1;
4787 sq->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
4789 * If there is no end, there never will be
4792 if (sq->end_added == 0) {
4793 /* Held for PD-API clear that. */
4794 sq->pdapi_aborted = 1;
4795 sq->held_length = 0;
4796 if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT) && (so != NULL)) {
4798 * Need to add a PD-API
4799 * aborted indication.
4800 * Setting the control_pdapi
4801 * assures that it will be
4802 * added right after this
4807 stcb->asoc.control_pdapi = sq;
4808 strseq = (sq->sinfo_stream << 16) | sq->sinfo_ssn;
4809 sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
4811 SCTP_PARTIAL_DELIVERY_ABORTED,
4814 stcb->asoc.control_pdapi = NULL;
4817 /* Add an end to wake them */
4821 SCTP_INP_READ_UNLOCK(inp);
4822 if (stcb->block_entry) {
4823 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PCB, ECONNRESET);
4824 stcb->block_entry->error = ECONNRESET;
4825 stcb->block_entry = NULL;
4828 if ((stcb->asoc.refcnt) || (stcb->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE)) {
4830 * Someone holds a reference OR the socket is unaccepted
4833 if ((stcb->asoc.refcnt) ||
4834 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4835 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
4836 stcb->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE;
4837 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
4839 SCTP_TCB_UNLOCK(stcb);
4840 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4841 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
4842 /* nothing around */
4845 /* Wake any reader/writers */
4846 sctp_sorwakeup(inp, so);
4847 sctp_sowwakeup(inp, so);
4849 #ifdef SCTP_LOG_CLOSING
4850 sctp_log_closing(inp, stcb, 9);
4852 /* no asoc destroyed */
4855 #ifdef SCTP_LOG_CLOSING
4856 sctp_log_closing(inp, stcb, 10);
4859 * When I reach here, no others want to kill the assoc yet.. and I
4860 * own the lock. Now its possible an abort comes in when I do the
4861 * lock exchange below to grab all the locks to do the final take
4862 * out. to prevent this we increment the count, which will start a
4863 * timer and blow out above thus assuring us that we hold exclusive
4864 * killing of the asoc. Note that after getting back the TCB lock we
4865 * will go ahead and increment the counter back up and stop any
4866 * timer a passing stranger may have started :-S
4868 if (from_inpcbfree == SCTP_NORMAL_PROC) {
4869 atomic_add_int(&stcb->asoc.refcnt, 1);
4871 SCTP_TCB_UNLOCK(stcb);
4872 SCTP_INP_INFO_WLOCK();
4873 SCTP_INP_WLOCK(inp);
4874 SCTP_TCB_LOCK(stcb);
4876 /* Double check the GONE flag */
4877 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4878 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
4879 /* nothing around */
4882 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4883 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
4885 * For TCP type we need special handling when we are
4886 * connected. We also include the peel'ed off ones to.
4888 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4889 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
4890 inp->sctp_flags |= SCTP_PCB_FLAGS_WAS_CONNECTED;
4893 if (so->so_rcv.sb_cc == 0) {
4894 so->so_state &= ~(SS_ISCONNECTING |
4895 SS_ISDISCONNECTING |
4899 socantrcvmore_locked(so);
4900 sctp_sowwakeup(inp, so);
4901 sctp_sorwakeup(inp, so);
4907 * Make it invalid too, that way if its about to run it will abort
4910 /* re-increment the lock */
4911 if (from_inpcbfree == SCTP_NORMAL_PROC) {
4912 atomic_add_int(&stcb->asoc.refcnt, -1);
4914 if (stcb->asoc.refcnt) {
4915 stcb->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE;
4916 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
4917 if (from_inpcbfree == SCTP_NORMAL_PROC) {
4918 SCTP_INP_INFO_WUNLOCK();
4919 SCTP_INP_WUNLOCK(inp);
4921 SCTP_TCB_UNLOCK(stcb);
4925 if (inp->sctp_tcbhash) {
4926 LIST_REMOVE(stcb, sctp_tcbhash);
4928 if (stcb->asoc.in_asocid_hash) {
4929 LIST_REMOVE(stcb, sctp_tcbasocidhash);
4931 /* Now lets remove it from the list of ALL associations in the EP */
4932 LIST_REMOVE(stcb, sctp_tcblist);
4933 if (from_inpcbfree == SCTP_NORMAL_PROC) {
4934 SCTP_INP_INCR_REF(inp);
4935 SCTP_INP_WUNLOCK(inp);
4937 /* pull from vtag hash */
4938 LIST_REMOVE(stcb, sctp_asocs);
4939 sctp_add_vtag_to_timewait(asoc->my_vtag, SCTP_BASE_SYSCTL(sctp_vtag_time_wait),
4940 inp->sctp_lport, stcb->rport);
4943 * Now restop the timers to be sure this is paranoia at is finest!
4945 (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
4946 (void)SCTP_OS_TIMER_STOP(&asoc->hb_timer.timer);
4947 (void)SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
4948 (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
4949 (void)SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
4950 (void)SCTP_OS_TIMER_STOP(&asoc->shut_guard_timer.timer);
4951 (void)SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
4952 (void)SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
4953 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4954 (void)SCTP_OS_TIMER_STOP(&net->fr_timer.timer);
4955 (void)SCTP_OS_TIMER_STOP(&net->rxt_timer.timer);
4956 (void)SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
4959 asoc->strreset_timer.type = SCTP_TIMER_TYPE_NONE;
4961 * The chunk lists and such SHOULD be empty but we check them just
4964 /* anything on the wheel needs to be removed */
4965 for (i = 0; i < asoc->streamoutcnt; i++) {
4966 struct sctp_stream_out *outs;
4968 outs = &asoc->strmout[i];
4969 /* now clean up any chunks here */
4970 TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
4971 TAILQ_REMOVE(&outs->outqueue, sp, next);
4974 /* Still an open socket - report */
4975 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb,
4976 SCTP_NOTIFY_DATAGRAM_UNSENT,
4977 (void *)sp, SCTP_SO_LOCKED);
4980 sctp_m_freem(sp->data);
4982 sp->tail_mbuf = NULL;
4986 sctp_free_remote_addr(sp->net);
4989 sctp_free_spbufspace(stcb, asoc, sp);
4990 if (sp->holds_key_ref)
4991 sctp_auth_key_release(stcb, sp->auth_keyid, SCTP_SO_LOCKED);
4992 /* Free the zone stuff */
4993 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_strmoq), sp);
4994 SCTP_DECR_STRMOQ_COUNT();
4995 /* sa_ignore FREED_MEMORY */
4998 /* sa_ignore FREED_MEMORY */
4999 TAILQ_FOREACH_SAFE(strrst, &asoc->resetHead, next_resp, nstrrst) {
5000 TAILQ_REMOVE(&asoc->resetHead, strrst, next_resp);
5001 SCTP_FREE(strrst, SCTP_M_STRESET);
5003 TAILQ_FOREACH_SAFE(sq, &asoc->pending_reply_queue, next, nsq) {
5004 TAILQ_REMOVE(&asoc->pending_reply_queue, sq, next);
5006 sctp_m_freem(sq->data);
5009 sctp_free_remote_addr(sq->whoFrom);
5012 /* Free the ctl entry */
5013 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), sq);
5014 SCTP_DECR_READQ_COUNT();
5015 /* sa_ignore FREED_MEMORY */
5017 TAILQ_FOREACH_SAFE(chk, &asoc->free_chunks, sctp_next, nchk) {
5018 TAILQ_REMOVE(&asoc->free_chunks, chk, sctp_next);
5020 sctp_m_freem(chk->data);
5023 if (chk->holds_key_ref)
5024 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5025 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5026 SCTP_DECR_CHK_COUNT();
5027 atomic_subtract_int(&SCTP_BASE_INFO(ipi_free_chunks), 1);
5028 asoc->free_chunk_cnt--;
5029 /* sa_ignore FREED_MEMORY */
5031 /* pending send queue SHOULD be empty */
5032 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
5033 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
5036 /* Still a socket? */
5037 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb,
5038 SCTP_NOTIFY_DATAGRAM_UNSENT, chk, SCTP_SO_LOCKED);
5041 sctp_m_freem(chk->data);
5045 if (chk->holds_key_ref)
5046 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5048 sctp_free_remote_addr(chk->whoTo);
5051 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5052 SCTP_DECR_CHK_COUNT();
5053 /* sa_ignore FREED_MEMORY */
5055 /* sent queue SHOULD be empty */
5056 TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) {
5057 TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
5060 /* Still a socket? */
5061 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb,
5062 SCTP_NOTIFY_DATAGRAM_SENT, chk, SCTP_SO_LOCKED);
5065 sctp_m_freem(chk->data);
5069 if (chk->holds_key_ref)
5070 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5071 sctp_free_remote_addr(chk->whoTo);
5072 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5073 SCTP_DECR_CHK_COUNT();
5074 /* sa_ignore FREED_MEMORY */
5076 /* control queue MAY not be empty */
5077 TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
5078 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
5080 sctp_m_freem(chk->data);
5083 if (chk->holds_key_ref)
5084 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5085 sctp_free_remote_addr(chk->whoTo);
5086 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5087 SCTP_DECR_CHK_COUNT();
5088 /* sa_ignore FREED_MEMORY */
5090 /* ASCONF queue MAY not be empty */
5091 TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
5092 TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
5094 sctp_m_freem(chk->data);
5097 if (chk->holds_key_ref)
5098 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5099 sctp_free_remote_addr(chk->whoTo);
5100 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5101 SCTP_DECR_CHK_COUNT();
5102 /* sa_ignore FREED_MEMORY */
5104 TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
5105 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
5107 sctp_m_freem(chk->data);
5110 if (chk->holds_key_ref)
5111 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5112 sctp_free_remote_addr(chk->whoTo);
5113 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5114 SCTP_DECR_CHK_COUNT();
5115 /* sa_ignore FREED_MEMORY */
5118 if (asoc->mapping_array) {
5119 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
5120 asoc->mapping_array = NULL;
5122 if (asoc->nr_mapping_array) {
5123 SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
5124 asoc->nr_mapping_array = NULL;
5126 /* the stream outs */
5127 if (asoc->strmout) {
5128 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
5129 asoc->strmout = NULL;
5131 asoc->strm_realoutsize = asoc->streamoutcnt = 0;
5133 struct sctp_queued_to_read *ctl, *nctl;
5135 for (i = 0; i < asoc->streamincnt; i++) {
5136 TAILQ_FOREACH_SAFE(ctl, &asoc->strmin[i].inqueue, next, nctl) {
5137 TAILQ_REMOVE(&asoc->strmin[i].inqueue, ctl, next);
5138 sctp_free_remote_addr(ctl->whoFrom);
5140 sctp_m_freem(ctl->data);
5144 * We don't free the address here since all
5145 * the net's were freed above.
5147 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), ctl);
5148 SCTP_DECR_READQ_COUNT();
5151 SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
5152 asoc->strmin = NULL;
5154 asoc->streamincnt = 0;
5155 TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) {
5157 if (SCTP_BASE_INFO(ipi_count_raddr) == 0) {
5158 panic("no net's left alloc'ed, or list points to itself");
5161 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
5162 sctp_free_remote_addr(net);
5164 LIST_FOREACH_SAFE(laddr, &asoc->sctp_restricted_addrs, sctp_nxt_addr, naddr) {
5165 /* sa_ignore FREED_MEMORY */
5166 sctp_remove_laddr(laddr);
5169 /* pending asconf (address) parameters */
5170 TAILQ_FOREACH_SAFE(aparam, &asoc->asconf_queue, next, naparam) {
5171 /* sa_ignore FREED_MEMORY */
5172 TAILQ_REMOVE(&asoc->asconf_queue, aparam, next);
5173 SCTP_FREE(aparam, SCTP_M_ASC_ADDR);
5175 TAILQ_FOREACH_SAFE(aack, &asoc->asconf_ack_sent, next, naack) {
5176 /* sa_ignore FREED_MEMORY */
5177 TAILQ_REMOVE(&asoc->asconf_ack_sent, aack, next);
5178 if (aack->data != NULL) {
5179 sctp_m_freem(aack->data);
5181 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asconf_ack), aack);
5183 /* clean up auth stuff */
5184 if (asoc->local_hmacs)
5185 sctp_free_hmaclist(asoc->local_hmacs);
5186 if (asoc->peer_hmacs)
5187 sctp_free_hmaclist(asoc->peer_hmacs);
5189 if (asoc->local_auth_chunks)
5190 sctp_free_chunklist(asoc->local_auth_chunks);
5191 if (asoc->peer_auth_chunks)
5192 sctp_free_chunklist(asoc->peer_auth_chunks);
5194 sctp_free_authinfo(&asoc->authinfo);
5196 LIST_FOREACH_SAFE(shared_key, &asoc->shared_keys, next, nshared_key) {
5197 LIST_REMOVE(shared_key, next);
5198 sctp_free_sharedkey(shared_key);
5199 /* sa_ignore FREED_MEMORY */
5202 /* Insert new items here :> */
5204 /* Get rid of LOCK */
5205 SCTP_TCB_LOCK_DESTROY(stcb);
5206 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
5207 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5208 SCTP_INP_INFO_WUNLOCK();
5209 SCTP_INP_RLOCK(inp);
5211 #ifdef SCTP_TRACK_FREED_ASOCS
5212 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5213 /* now clean up the tasoc itself */
5214 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
5215 SCTP_DECR_ASOC_COUNT();
5217 LIST_INSERT_HEAD(&inp->sctp_asoc_free_list, stcb, sctp_tcblist);
5220 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
5221 SCTP_DECR_ASOC_COUNT();
5223 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5224 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5226 * If its NOT the inp_free calling us AND sctp_close
5227 * as been called, we call back...
5229 SCTP_INP_RUNLOCK(inp);
5231 * This will start the kill timer (if we are the
5232 * last one) since we hold an increment yet. But
5233 * this is the only safe way to do this since
5234 * otherwise if the socket closes at the same time
5235 * we are here we might collide in the cleanup.
5237 sctp_inpcb_free(inp,
5238 SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
5239 SCTP_CALLED_DIRECTLY_NOCMPSET);
5240 SCTP_INP_DECR_REF(inp);
5243 /* The socket is still open. */
5244 SCTP_INP_DECR_REF(inp);
5247 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5248 SCTP_INP_RUNLOCK(inp);
5251 /* destroyed the asoc */
5252 #ifdef SCTP_LOG_CLOSING
5253 sctp_log_closing(inp, NULL, 11);
5261 * determine if a destination is "reachable" based upon the addresses bound
5262 * to the current endpoint (e.g. only v4 or v6 currently bound)
5265 * FIX: if we allow assoc-level bindx(), then this needs to be fixed to use
5266 * assoc level v4/v6 flags, as the assoc *may* not have the same address
5267 * types bound as its endpoint
5270 sctp_destination_is_reachable(struct sctp_tcb *stcb, struct sockaddr *destaddr)
5272 struct sctp_inpcb *inp;
5276 * No locks here, the TCB, in all cases is already locked and an
5277 * assoc is up. There is either a INP lock by the caller applied (in
5278 * asconf case when deleting an address) or NOT in the HB case,
5279 * however if HB then the INP increment is up and the INP will not
5280 * be removed (on top of the fact that we have a TCB lock). So we
5281 * only want to read the sctp_flags, which is either bound-all or
5282 * not.. no protection needed since once an assoc is up you can't be
5283 * changing your binding.
5285 inp = stcb->sctp_ep;
5286 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
5287 /* if bound all, destination is not restricted */
5289 * RRS: Question during lock work: Is this correct? If you
5290 * are bound-all you still might need to obey the V4--V6
5291 * flags??? IMO this bound-all stuff needs to be removed!
5295 /* NOTE: all "scope" checks are done when local addresses are added */
5296 switch (destaddr->sa_family) {
5298 answer = inp->ip_inp.inp.inp_vflag & INP_IPV6;
5301 answer = inp->ip_inp.inp.inp_vflag & INP_IPV4;
5304 /* invalid family, so it's unreachable */
5312 * update the inp_vflags on an endpoint
5315 sctp_update_ep_vflag(struct sctp_inpcb *inp)
5317 struct sctp_laddr *laddr;
5319 /* first clear the flag */
5320 inp->ip_inp.inp.inp_vflag = 0;
5321 /* set the flag based on addresses on the ep list */
5322 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
5323 if (laddr->ifa == NULL) {
5324 SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n",
5328 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
5331 switch (laddr->ifa->address.sa.sa_family) {
5334 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
5339 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
5349 * Add the address to the endpoint local address list There is nothing to be
5350 * done if we are bound to all addresses
5353 sctp_add_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa, uint32_t action)
5355 struct sctp_laddr *laddr;
5360 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
5361 /* You are already bound to all. You have it already */
5365 if (ifa->address.sa.sa_family == AF_INET6) {
5366 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
5367 /* Can't bind a non-useable addr. */
5372 /* first, is it already present? */
5373 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
5374 if (laddr->ifa == ifa) {
5381 /* Not in the ep list */
5382 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, action);
5386 /* update inp_vflag flags */
5387 switch (ifa->address.sa.sa_family) {
5390 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
5395 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
5407 * select a new (hopefully reachable) destination net (should only be used
5408 * when we deleted an ep addr that is the only usable source address to reach
5409 * the destination net)
5412 sctp_select_primary_destination(struct sctp_tcb *stcb)
5414 struct sctp_nets *net;
5416 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5417 /* for now, we'll just pick the first reachable one we find */
5418 if (net->dest_state & SCTP_ADDR_UNCONFIRMED)
5420 if (sctp_destination_is_reachable(stcb,
5421 (struct sockaddr *)&net->ro._l_addr)) {
5422 /* found a reachable destination */
5423 stcb->asoc.primary_destination = net;
5426 /* I can't there from here! ...we're gonna die shortly... */
5431 * Delete the address from the endpoint local address list There is nothing
5432 * to be done if we are bound to all addresses
5435 sctp_del_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
5437 struct sctp_laddr *laddr;
5441 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
5442 /* You are already bound to all. You have it already */
5445 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
5446 if (laddr->ifa == ifa) {
5451 if (fnd && (inp->laddr_count < 2)) {
5452 /* can't delete unless there are at LEAST 2 addresses */
5457 * clean up any use of this address go through our
5458 * associations and clear any last_used_address that match
5459 * this one for each assoc, see if a new primary_destination
5462 struct sctp_tcb *stcb;
5464 /* clean up "next_addr_touse" */
5465 if (inp->next_addr_touse == laddr)
5466 /* delete this address */
5467 inp->next_addr_touse = NULL;
5469 /* clean up "last_used_address" */
5470 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5471 struct sctp_nets *net;
5473 SCTP_TCB_LOCK(stcb);
5474 if (stcb->asoc.last_used_address == laddr)
5475 /* delete this address */
5476 stcb->asoc.last_used_address = NULL;
5478 * Now spin through all the nets and purge any ref
5481 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5482 if (net->ro._s_addr &&
5483 (net->ro._s_addr->ifa == laddr->ifa)) {
5484 /* Yep, purge src address selected */
5487 /* delete this address if cached */
5491 net->ro.ro_rt = NULL;
5493 sctp_free_ifa(net->ro._s_addr);
5494 net->ro._s_addr = NULL;
5495 net->src_addr_selected = 0;
5498 SCTP_TCB_UNLOCK(stcb);
5499 } /* for each tcb */
5500 /* remove it from the ep list */
5501 sctp_remove_laddr(laddr);
5503 /* update inp_vflag flags */
5504 sctp_update_ep_vflag(inp);
5510 * Add the address to the TCB local address restricted list.
5511 * This is a "pending" address list (eg. addresses waiting for an
5512 * ASCONF-ACK response) and cannot be used as a valid source address.
5515 sctp_add_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
5517 struct sctp_inpcb *inp;
5518 struct sctp_laddr *laddr;
5519 struct sctpladdr *list;
5522 * Assumes TCB is locked.. and possibly the INP. May need to
5523 * confirm/fix that if we need it and is not the case.
5525 list = &stcb->asoc.sctp_restricted_addrs;
5527 inp = stcb->sctp_ep;
5529 if (ifa->address.sa.sa_family == AF_INET6) {
5530 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
5531 /* Can't bind a non-existent addr. */
5536 /* does the address already exist? */
5537 LIST_FOREACH(laddr, list, sctp_nxt_addr) {
5538 if (laddr->ifa == ifa) {
5543 /* add to the list */
5544 (void)sctp_insert_laddr(list, ifa, 0);
5549 * insert an laddr entry with the given ifa for the desired list
5552 sctp_insert_laddr(struct sctpladdr *list, struct sctp_ifa *ifa, uint32_t act)
5554 struct sctp_laddr *laddr;
5556 laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
5557 if (laddr == NULL) {
5558 /* out of memory? */
5559 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
5562 SCTP_INCR_LADDR_COUNT();
5563 bzero(laddr, sizeof(*laddr));
5564 (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time);
5566 laddr->action = act;
5567 atomic_add_int(&ifa->refcount, 1);
5569 LIST_INSERT_HEAD(list, laddr, sctp_nxt_addr);
5575 * Remove an laddr entry from the local address list (on an assoc)
5578 sctp_remove_laddr(struct sctp_laddr *laddr)
5581 /* remove from the list */
5582 LIST_REMOVE(laddr, sctp_nxt_addr);
5583 sctp_free_ifa(laddr->ifa);
5584 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), laddr);
5585 SCTP_DECR_LADDR_COUNT();
5589 * Remove a local address from the TCB local address restricted list
5592 sctp_del_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
5594 struct sctp_inpcb *inp;
5595 struct sctp_laddr *laddr;
5598 * This is called by asconf work. It is assumed that a) The TCB is
5599 * locked and b) The INP is locked. This is true in as much as I can
5600 * trace through the entry asconf code where I did these locks.
5601 * Again, the ASCONF code is a bit different in that it does lock
5602 * the INP during its work often times. This must be since we don't
5603 * want other proc's looking up things while what they are looking
5604 * up is changing :-D
5607 inp = stcb->sctp_ep;
5608 /* if subset bound and don't allow ASCONF's, can't delete last */
5609 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) &&
5610 sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF)) {
5611 if (stcb->sctp_ep->laddr_count < 2) {
5612 /* can't delete last address */
5616 LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
5617 /* remove the address if it exists */
5618 if (laddr->ifa == NULL)
5620 if (laddr->ifa == ifa) {
5621 sctp_remove_laddr(laddr);
5626 /* address not found! */
5631 * Temporarily remove for __APPLE__ until we use the Tiger equivalents
5634 static int sctp_max_number_of_assoc = SCTP_MAX_NUM_OF_ASOC;
5635 static int sctp_scale_up_for_address = SCTP_SCALE_FOR_ADDR;
5639 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
5640 struct sctp_mcore_ctrl *sctp_mcore_workers = NULL;
5641 int *sctp_cpuarry = NULL;
5643 sctp_queue_to_mcore(struct mbuf *m, int off, int cpu_to_use)
5645 /* Queue a packet to a processor for the specified core */
5646 struct sctp_mcore_queue *qent;
5647 struct sctp_mcore_ctrl *wkq;
5650 if (sctp_mcore_workers == NULL) {
5651 /* Something went way bad during setup */
5652 sctp_input_with_port(m, off, 0);
5655 SCTP_MALLOC(qent, struct sctp_mcore_queue *,
5656 (sizeof(struct sctp_mcore_queue)),
5659 /* This is trouble */
5660 sctp_input_with_port(m, off, 0);
5667 wkq = &sctp_mcore_workers[cpu_to_use];
5668 SCTP_MCORE_QLOCK(wkq);
5670 TAILQ_INSERT_TAIL(&wkq->que, qent, next);
5671 if (wkq->running == 0) {
5674 SCTP_MCORE_QUNLOCK(wkq);
5676 wakeup(&wkq->running);
5681 sctp_mcore_thread(void *arg)
5684 struct sctp_mcore_ctrl *wkq;
5685 struct sctp_mcore_queue *qent;
5687 wkq = (struct sctp_mcore_ctrl *)arg;
5691 /* Wait for first tickle */
5692 SCTP_MCORE_LOCK(wkq);
5694 msleep(&wkq->running,
5696 0, "wait for pkt", 0);
5697 SCTP_MCORE_UNLOCK(wkq);
5699 /* Bind to our cpu */
5700 thread_lock(curthread);
5701 sched_bind(curthread, wkq->cpuid);
5702 thread_unlock(curthread);
5704 /* Now lets start working */
5705 SCTP_MCORE_LOCK(wkq);
5706 /* Now grab lock and go */
5708 SCTP_MCORE_QLOCK(wkq);
5711 qent = TAILQ_FIRST(&wkq->que);
5713 TAILQ_REMOVE(&wkq->que, qent, next);
5714 SCTP_MCORE_QUNLOCK(wkq);
5715 CURVNET_SET(qent->vn);
5719 SCTP_FREE(qent, SCTP_M_MCORE);
5721 sctp_input_with_port(m, off, 0);
5723 printf("V6 not yet supported\n");
5727 SCTP_MCORE_QLOCK(wkq);
5730 if (!TAILQ_EMPTY(&wkq->que)) {
5733 SCTP_MCORE_QUNLOCK(wkq);
5734 msleep(&wkq->running,
5736 0, "wait for pkt", 0);
5741 sctp_startup_mcore_threads(void)
5748 if (sctp_mcore_workers != NULL) {
5750 * Already been here in some previous vnet?
5754 SCTP_MALLOC(sctp_mcore_workers, struct sctp_mcore_ctrl *,
5755 ((mp_maxid + 1) * sizeof(struct sctp_mcore_ctrl)),
5757 if (sctp_mcore_workers == NULL) {
5761 memset(sctp_mcore_workers, 0, ((mp_maxid + 1) *
5762 sizeof(struct sctp_mcore_ctrl)));
5763 /* Init the structures */
5764 for (i = 0; i <= mp_maxid; i++) {
5765 TAILQ_INIT(&sctp_mcore_workers[i].que);
5766 SCTP_MCORE_LOCK_INIT(&sctp_mcore_workers[i]);
5767 SCTP_MCORE_QLOCK_INIT(&sctp_mcore_workers[i]);
5768 sctp_mcore_workers[i].cpuid = i;
5770 if (sctp_cpuarry == NULL) {
5771 SCTP_MALLOC(sctp_cpuarry, int *,
5772 (mp_ncpus * sizeof(int)),
5776 sctp_cpuarry[i] = cpu;
5780 /* Now start them all */
5782 (void)kproc_create(sctp_mcore_thread,
5783 (void *)&sctp_mcore_workers[cpu],
5784 &sctp_mcore_workers[cpu].thread_proc,
5798 * SCTP initialization for the PCB structures should be called by
5799 * the sctp_init() funciton.
5804 if (SCTP_BASE_VAR(sctp_pcb_initialized) != 0) {
5805 /* error I was called twice */
5808 SCTP_BASE_VAR(sctp_pcb_initialized) = 1;
5810 #if defined(SCTP_LOCAL_TRACE_BUF)
5811 bzero(&SCTP_BASE_SYSCTL(sctp_log), sizeof(struct sctp_log));
5813 #if defined(__FreeBSD__) && defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
5814 SCTP_MALLOC(SCTP_BASE_STATS, struct sctpstat *,
5815 ((mp_maxid + 1) * sizeof(struct sctpstat)),
5818 (void)SCTP_GETTIME_TIMEVAL(&tv);
5819 #if defined(__FreeBSD__) && defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
5820 bzero(SCTP_BASE_STATS, (sizeof(struct sctpstat) * (mp_maxid + 1)));
5821 SCTP_BASE_STATS[PCPU_GET(cpuid)].sctps_discontinuitytime.tv_sec = (uint32_t) tv.tv_sec;
5822 SCTP_BASE_STATS[PCPU_GET(cpuid)].sctps_discontinuitytime.tv_usec = (uint32_t) tv.tv_usec;
5824 bzero(&SCTP_BASE_STATS, sizeof(struct sctpstat));
5825 SCTP_BASE_STAT(sctps_discontinuitytime).tv_sec = (uint32_t) tv.tv_sec;
5826 SCTP_BASE_STAT(sctps_discontinuitytime).tv_usec = (uint32_t) tv.tv_usec;
5828 /* init the empty list of (All) Endpoints */
5829 LIST_INIT(&SCTP_BASE_INFO(listhead));
5832 /* init the hash table of endpoints */
5833 TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", &SCTP_BASE_SYSCTL(sctp_hashtblsize));
5834 TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", &SCTP_BASE_SYSCTL(sctp_pcbtblsize));
5835 TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", &SCTP_BASE_SYSCTL(sctp_chunkscale));
5836 SCTP_BASE_INFO(sctp_asochash) = SCTP_HASH_INIT((SCTP_BASE_SYSCTL(sctp_hashtblsize) * 31),
5837 &SCTP_BASE_INFO(hashasocmark));
5838 SCTP_BASE_INFO(sctp_ephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize),
5839 &SCTP_BASE_INFO(hashmark));
5840 SCTP_BASE_INFO(sctp_tcpephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize),
5841 &SCTP_BASE_INFO(hashtcpmark));
5842 SCTP_BASE_INFO(hashtblsize) = SCTP_BASE_SYSCTL(sctp_hashtblsize);
5845 SCTP_BASE_INFO(sctp_vrfhash) = SCTP_HASH_INIT(SCTP_SIZE_OF_VRF_HASH,
5846 &SCTP_BASE_INFO(hashvrfmark));
5848 SCTP_BASE_INFO(vrf_ifn_hash) = SCTP_HASH_INIT(SCTP_VRF_IFN_HASH_SIZE,
5849 &SCTP_BASE_INFO(vrf_ifn_hashmark));
5850 /* init the zones */
5852 * FIX ME: Should check for NULL returns, but if it does fail we are
5853 * doomed to panic anyways... add later maybe.
5855 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_ep), "sctp_ep",
5856 sizeof(struct sctp_inpcb), maxsockets);
5858 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asoc), "sctp_asoc",
5859 sizeof(struct sctp_tcb), sctp_max_number_of_assoc);
5861 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_laddr), "sctp_laddr",
5862 sizeof(struct sctp_laddr),
5863 (sctp_max_number_of_assoc * sctp_scale_up_for_address));
5865 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_net), "sctp_raddr",
5866 sizeof(struct sctp_nets),
5867 (sctp_max_number_of_assoc * sctp_scale_up_for_address));
5869 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_chunk), "sctp_chunk",
5870 sizeof(struct sctp_tmit_chunk),
5871 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
5873 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_readq), "sctp_readq",
5874 sizeof(struct sctp_queued_to_read),
5875 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
5877 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_strmoq), "sctp_stream_msg_out",
5878 sizeof(struct sctp_stream_queue_pending),
5879 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
5881 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf), "sctp_asconf",
5882 sizeof(struct sctp_asconf),
5883 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
5885 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf_ack), "sctp_asconf_ack",
5886 sizeof(struct sctp_asconf_ack),
5887 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
5890 /* Master Lock INIT for info structure */
5891 SCTP_INP_INFO_LOCK_INIT();
5892 SCTP_STATLOG_INIT_LOCK();
5894 SCTP_IPI_COUNT_INIT();
5895 SCTP_IPI_ADDR_INIT();
5896 #ifdef SCTP_PACKET_LOGGING
5897 SCTP_IP_PKTLOG_INIT();
5899 LIST_INIT(&SCTP_BASE_INFO(addr_wq));
5901 SCTP_WQ_ADDR_INIT();
5902 /* not sure if we need all the counts */
5903 SCTP_BASE_INFO(ipi_count_ep) = 0;
5904 /* assoc/tcb zone info */
5905 SCTP_BASE_INFO(ipi_count_asoc) = 0;
5906 /* local addrlist zone info */
5907 SCTP_BASE_INFO(ipi_count_laddr) = 0;
5908 /* remote addrlist zone info */
5909 SCTP_BASE_INFO(ipi_count_raddr) = 0;
5911 SCTP_BASE_INFO(ipi_count_chunk) = 0;
5913 /* socket queue zone info */
5914 SCTP_BASE_INFO(ipi_count_readq) = 0;
5916 /* stream out queue cont */
5917 SCTP_BASE_INFO(ipi_count_strmoq) = 0;
5919 SCTP_BASE_INFO(ipi_free_strmoq) = 0;
5920 SCTP_BASE_INFO(ipi_free_chunks) = 0;
5922 SCTP_OS_TIMER_INIT(&SCTP_BASE_INFO(addr_wq_timer.timer));
5924 /* Init the TIMEWAIT list */
5925 for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) {
5926 LIST_INIT(&SCTP_BASE_INFO(vtag_timewait)[i]);
5929 sctp_startup_iterator();
5931 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
5932 sctp_startup_mcore_threads();
5936 * INIT the default VRF which for BSD is the only one, other O/S's
5937 * may have more. But initially they must start with one and then
5938 * add the VRF's as addresses are added.
5940 sctp_init_vrf_list(SCTP_DEFAULT_VRF);
5944 * Assumes that the SCTP_BASE_INFO() lock is NOT held.
5947 sctp_pcb_finish(void)
5949 struct sctp_vrflist *vrf_bucket;
5950 struct sctp_vrf *vrf, *nvrf;
5951 struct sctp_ifn *ifn, *nifn;
5952 struct sctp_ifa *ifa, *nifa;
5953 struct sctpvtaghead *chain;
5954 struct sctp_tagblock *twait_block, *prev_twait_block;
5955 struct sctp_laddr *wi, *nwi;
5959 * Free BSD the it thread never exits but we do clean up. The only
5960 * way freebsd reaches here if we have VRF's but we still add the
5961 * ifdef to make it compile on old versions.
5964 struct sctp_iterator *it, *nit;
5966 SCTP_IPI_ITERATOR_WQ_LOCK();
5967 TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) {
5968 if (it->vn != curvnet) {
5971 TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
5972 if (it->function_atend != NULL) {
5973 (*it->function_atend) (it->pointer, it->val);
5975 SCTP_FREE(it, SCTP_M_ITER);
5977 SCTP_IPI_ITERATOR_WQ_UNLOCK();
5978 SCTP_ITERATOR_LOCK();
5979 if ((sctp_it_ctl.cur_it) &&
5980 (sctp_it_ctl.cur_it->vn == curvnet)) {
5981 sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_IT;
5983 SCTP_ITERATOR_UNLOCK();
5986 SCTP_OS_TIMER_STOP(&SCTP_BASE_INFO(addr_wq_timer.timer));
5987 SCTP_WQ_ADDR_LOCK();
5988 LIST_FOREACH_SAFE(wi, &SCTP_BASE_INFO(addr_wq), sctp_nxt_addr, nwi) {
5989 LIST_REMOVE(wi, sctp_nxt_addr);
5990 SCTP_DECR_LADDR_COUNT();
5991 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), wi);
5993 SCTP_WQ_ADDR_UNLOCK();
5996 * free the vrf/ifn/ifa lists and hashes (be sure address monitor is
5999 vrf_bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(SCTP_DEFAULT_VRFID & SCTP_BASE_INFO(hashvrfmark))];
6000 LIST_FOREACH_SAFE(vrf, vrf_bucket, next_vrf, nvrf) {
6001 LIST_FOREACH_SAFE(ifn, &vrf->ifnlist, next_ifn, nifn) {
6002 LIST_FOREACH_SAFE(ifa, &ifn->ifalist, next_ifa, nifa) {
6004 LIST_REMOVE(ifa, next_bucket);
6005 LIST_REMOVE(ifa, next_ifa);
6006 SCTP_FREE(ifa, SCTP_M_IFA);
6009 LIST_REMOVE(ifn, next_bucket);
6010 LIST_REMOVE(ifn, next_ifn);
6011 SCTP_FREE(ifn, SCTP_M_IFN);
6013 SCTP_HASH_FREE(vrf->vrf_addr_hash, vrf->vrf_addr_hashmark);
6015 LIST_REMOVE(vrf, next_vrf);
6016 SCTP_FREE(vrf, SCTP_M_VRF);
6018 /* free the vrf hashes */
6019 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_vrfhash), SCTP_BASE_INFO(hashvrfmark));
6020 SCTP_HASH_FREE(SCTP_BASE_INFO(vrf_ifn_hash), SCTP_BASE_INFO(vrf_ifn_hashmark));
6023 * free the TIMEWAIT list elements malloc'd in the function
6024 * sctp_add_vtag_to_timewait()...
6026 for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) {
6027 chain = &SCTP_BASE_INFO(vtag_timewait)[i];
6028 if (!LIST_EMPTY(chain)) {
6029 prev_twait_block = NULL;
6030 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
6031 if (prev_twait_block) {
6032 SCTP_FREE(prev_twait_block, SCTP_M_TIMW);
6034 prev_twait_block = twait_block;
6036 SCTP_FREE(prev_twait_block, SCTP_M_TIMW);
6040 /* free the locks and mutexes */
6041 #ifdef SCTP_PACKET_LOGGING
6042 SCTP_IP_PKTLOG_DESTROY();
6044 SCTP_IPI_ADDR_DESTROY();
6045 SCTP_STATLOG_DESTROY();
6046 SCTP_INP_INFO_LOCK_DESTROY();
6048 SCTP_WQ_ADDR_DESTROY();
6050 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_ep));
6051 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asoc));
6052 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_laddr));
6053 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_net));
6054 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_chunk));
6055 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_readq));
6056 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_strmoq));
6057 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf));
6058 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf_ack));
6059 /* Get rid of other stuff to */
6060 if (SCTP_BASE_INFO(sctp_asochash) != NULL)
6061 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_asochash), SCTP_BASE_INFO(hashasocmark));
6062 if (SCTP_BASE_INFO(sctp_ephash) != NULL)
6063 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_ephash), SCTP_BASE_INFO(hashmark));
6064 if (SCTP_BASE_INFO(sctp_tcpephash) != NULL)
6065 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_tcpephash), SCTP_BASE_INFO(hashtcpmark));
6066 #if defined(__FreeBSD__) && defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
6067 SCTP_FREE(SCTP_BASE_STATS, SCTP_M_MCORE);
6073 sctp_load_addresses_from_init(struct sctp_tcb *stcb, struct mbuf *m,
6074 int iphlen, int offset, int limit, struct sctphdr *sh,
6075 struct sockaddr *altsa)
6078 * grub through the INIT pulling addresses and loading them to the
6079 * nets structure in the asoc. The from address in the mbuf should
6080 * also be loaded (if it is not already). This routine can be called
6081 * with either INIT or INIT-ACK's as long as the m points to the IP
6082 * packet and the offset points to the beginning of the parameters.
6084 struct sctp_inpcb *inp, *l_inp;
6085 struct sctp_nets *net, *nnet, *net_tmp;
6087 struct sctp_paramhdr *phdr, parm_buf;
6088 struct sctp_tcb *stcb_tmp;
6089 uint16_t ptype, plen;
6090 struct sockaddr *sa;
6091 struct sockaddr_storage dest_store;
6092 struct sockaddr *local_sa = (struct sockaddr *)&dest_store;
6093 uint8_t random_store[SCTP_PARAM_BUFFER_SIZE];
6094 struct sctp_auth_random *p_random = NULL;
6095 uint16_t random_len = 0;
6096 uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE];
6097 struct sctp_auth_hmac_algo *hmacs = NULL;
6098 uint16_t hmacs_len = 0;
6099 uint8_t saw_asconf = 0;
6100 uint8_t saw_asconf_ack = 0;
6101 uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE];
6102 struct sctp_auth_chunk_list *chunks = NULL;
6103 uint16_t num_chunks = 0;
6104 sctp_key_t *new_key;
6106 int got_random = 0, got_hmacs = 0, got_chklist = 0;
6107 uint8_t ecn_allowed;
6110 struct sockaddr_in sin;
6114 struct sockaddr_in6 sin6;
6118 /* First get the destination address setup too. */
6120 memset(&sin, 0, sizeof(sin));
6121 sin.sin_family = AF_INET;
6122 sin.sin_len = sizeof(sin);
6123 sin.sin_port = stcb->rport;
6126 memset(&sin6, 0, sizeof(sin6));
6127 sin6.sin6_family = AF_INET6;
6128 sin6.sin6_len = sizeof(struct sockaddr_in6);
6129 sin6.sin6_port = stcb->rport;
6131 if (altsa == NULL) {
6132 iph = mtod(m, struct ip *);
6133 switch (iph->ip_v) {
6138 struct sockaddr_in *sin_2;
6140 sin_2 = (struct sockaddr_in *)(local_sa);
6141 memset(sin_2, 0, sizeof(sin));
6142 sin_2->sin_family = AF_INET;
6143 sin_2->sin_len = sizeof(sin);
6144 sin_2->sin_port = sh->dest_port;
6145 sin_2->sin_addr.s_addr = iph->ip_dst.s_addr;
6146 sin.sin_addr = iph->ip_src;
6147 sa = (struct sockaddr *)&sin;
6152 case IPV6_VERSION >> 4:
6155 struct ip6_hdr *ip6;
6156 struct sockaddr_in6 *sin6_2;
6158 ip6 = mtod(m, struct ip6_hdr *);
6159 sin6_2 = (struct sockaddr_in6 *)(local_sa);
6160 memset(sin6_2, 0, sizeof(sin6));
6161 sin6_2->sin6_family = AF_INET6;
6162 sin6_2->sin6_len = sizeof(struct sockaddr_in6);
6163 sin6_2->sin6_port = sh->dest_port;
6164 sin6.sin6_addr = ip6->ip6_src;
6165 sa = (struct sockaddr *)&sin6;
6175 * For cookies we use the src address NOT from the packet
6176 * but from the original INIT
6180 /* Turn off ECN until we get through all params */
6182 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6183 /* mark all addresses that we have currently on the list */
6184 net->dest_state |= SCTP_ADDR_NOT_IN_ASSOC;
6186 /* does the source address already exist? if so skip it */
6187 l_inp = inp = stcb->sctp_ep;
6189 atomic_add_int(&stcb->asoc.refcnt, 1);
6190 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net_tmp, local_sa, stcb);
6191 atomic_add_int(&stcb->asoc.refcnt, -1);
6193 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || inp == NULL) {
6194 /* we must add the source address */
6195 /* no scope set here since we have a tcb already. */
6196 switch (sa->sa_family) {
6199 if (stcb->asoc.ipv4_addr_legal) {
6200 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_2)) {
6208 if (stcb->asoc.ipv6_addr_legal) {
6209 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) {
6219 if (net_tmp != NULL && stcb_tmp == stcb) {
6220 net_tmp->dest_state &= ~SCTP_ADDR_NOT_IN_ASSOC;
6221 } else if (stcb_tmp != stcb) {
6222 /* It belongs to another association? */
6224 SCTP_TCB_UNLOCK(stcb_tmp);
6228 if (stcb->asoc.state == 0) {
6229 /* the assoc was freed? */
6233 * peer must explicitly turn this on. This may have been initialized
6234 * to be "on" in order to allow local addr changes while INIT's are
6237 stcb->asoc.peer_supports_asconf = 0;
6238 /* now we must go through each of the params. */
6239 phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
6241 ptype = ntohs(phdr->param_type);
6242 plen = ntohs(phdr->param_length);
6244 * printf("ptype => %0x, plen => %d\n", (uint32_t)ptype,
6247 if (offset + plen > limit) {
6254 if (ptype == SCTP_IPV4_ADDRESS) {
6255 if (stcb->asoc.ipv4_addr_legal) {
6256 struct sctp_ipv4addr_param *p4, p4_buf;
6258 /* ok get the v4 address and check/add */
6259 phdr = sctp_get_next_param(m, offset,
6260 (struct sctp_paramhdr *)&p4_buf,
6262 if (plen != sizeof(struct sctp_ipv4addr_param) ||
6266 p4 = (struct sctp_ipv4addr_param *)phdr;
6267 sin.sin_addr.s_addr = p4->addr;
6268 if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
6269 /* Skip multi-cast addresses */
6272 if ((sin.sin_addr.s_addr == INADDR_BROADCAST) ||
6273 (sin.sin_addr.s_addr == INADDR_ANY)) {
6276 sa = (struct sockaddr *)&sin;
6277 inp = stcb->sctp_ep;
6278 atomic_add_int(&stcb->asoc.refcnt, 1);
6279 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
6281 atomic_add_int(&stcb->asoc.refcnt, -1);
6283 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) ||
6285 /* we must add the source address */
6287 * no scope set since we have a tcb
6292 * we must validate the state again
6296 if (stcb->asoc.state == 0) {
6297 /* the assoc was freed? */
6300 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_4)) {
6303 } else if (stcb_tmp == stcb) {
6304 if (stcb->asoc.state == 0) {
6305 /* the assoc was freed? */
6311 ~SCTP_ADDR_NOT_IN_ASSOC;
6315 * strange, address is in another
6316 * assoc? straighten out locks.
6319 if (SCTP_GET_STATE(&stcb_tmp->asoc) & SCTP_STATE_COOKIE_WAIT) {
6324 sctp_abort_an_association(stcb_tmp->sctp_ep,
6325 stcb_tmp, 1, NULL, 0);
6328 SCTP_TCB_UNLOCK(stcb_tmp);
6330 if (stcb->asoc.state == 0) {
6331 /* the assoc was freed? */
6340 if (ptype == SCTP_IPV6_ADDRESS) {
6341 if (stcb->asoc.ipv6_addr_legal) {
6342 /* ok get the v6 address and check/add */
6343 struct sctp_ipv6addr_param *p6, p6_buf;
6345 phdr = sctp_get_next_param(m, offset,
6346 (struct sctp_paramhdr *)&p6_buf,
6348 if (plen != sizeof(struct sctp_ipv6addr_param) ||
6352 p6 = (struct sctp_ipv6addr_param *)phdr;
6353 memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
6355 if (IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) {
6356 /* Skip multi-cast addresses */
6359 if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
6361 * Link local make no sense without
6366 sa = (struct sockaddr *)&sin6;
6367 inp = stcb->sctp_ep;
6368 atomic_add_int(&stcb->asoc.refcnt, 1);
6369 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
6371 atomic_add_int(&stcb->asoc.refcnt, -1);
6372 if (stcb_tmp == NULL &&
6373 (inp == stcb->sctp_ep || inp == NULL)) {
6375 * we must validate the state again
6379 if (stcb->asoc.state == 0) {
6380 /* the assoc was freed? */
6384 * we must add the address, no scope
6387 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_5)) {
6390 } else if (stcb_tmp == stcb) {
6392 * we must validate the state again
6395 if (stcb->asoc.state == 0) {
6396 /* the assoc was freed? */
6402 ~SCTP_ADDR_NOT_IN_ASSOC;
6406 * strange, address is in another
6407 * assoc? straighten out locks.
6410 if (SCTP_GET_STATE(&stcb_tmp->asoc) & SCTP_STATE_COOKIE_WAIT) {
6415 sctp_abort_an_association(stcb_tmp->sctp_ep,
6416 stcb_tmp, 1, NULL, 0);
6419 SCTP_TCB_UNLOCK(stcb_tmp);
6421 if (stcb->asoc.state == 0) {
6422 /* the assoc was freed? */
6430 if (ptype == SCTP_ECN_CAPABLE) {
6432 } else if (ptype == SCTP_ULP_ADAPTATION) {
6433 if (stcb->asoc.state != SCTP_STATE_OPEN) {
6434 struct sctp_adaptation_layer_indication ai,
6437 phdr = sctp_get_next_param(m, offset,
6438 (struct sctp_paramhdr *)&ai, sizeof(ai));
6439 aip = (struct sctp_adaptation_layer_indication *)phdr;
6441 stcb->asoc.peers_adaptation = ntohl(aip->indication);
6442 stcb->asoc.adaptation_needed = 1;
6445 } else if (ptype == SCTP_SET_PRIM_ADDR) {
6446 struct sctp_asconf_addr_param lstore, *fee;
6448 struct sockaddr *lsa = NULL;
6451 struct sctp_asconf_addrv4_param *fii;
6455 stcb->asoc.peer_supports_asconf = 1;
6456 if (plen > sizeof(lstore)) {
6459 phdr = sctp_get_next_param(m, offset,
6460 (struct sctp_paramhdr *)&lstore,
6461 min(plen, sizeof(lstore)));
6465 fee = (struct sctp_asconf_addr_param *)phdr;
6466 lptype = ntohs(fee->addrp.ph.param_type);
6469 case SCTP_IPV4_ADDRESS:
6471 sizeof(struct sctp_asconf_addrv4_param)) {
6472 SCTP_PRINTF("Sizeof setprim in init/init ack not %d but %d - ignored\n",
6473 (int)sizeof(struct sctp_asconf_addrv4_param),
6476 fii = (struct sctp_asconf_addrv4_param *)fee;
6477 sin.sin_addr.s_addr = fii->addrp.addr;
6478 lsa = (struct sockaddr *)&sin;
6483 case SCTP_IPV6_ADDRESS:
6485 sizeof(struct sctp_asconf_addr_param)) {
6486 SCTP_PRINTF("Sizeof setprim (v6) in init/init ack not %d but %d - ignored\n",
6487 (int)sizeof(struct sctp_asconf_addr_param),
6490 memcpy(sin6.sin6_addr.s6_addr,
6492 sizeof(fee->addrp.addr));
6493 lsa = (struct sockaddr *)&sin6;
6501 (void)sctp_set_primary_addr(stcb, sa, NULL);
6503 } else if (ptype == SCTP_HAS_NAT_SUPPORT) {
6504 stcb->asoc.peer_supports_nat = 1;
6505 } else if (ptype == SCTP_PRSCTP_SUPPORTED) {
6506 /* Peer supports pr-sctp */
6507 stcb->asoc.peer_supports_prsctp = 1;
6508 } else if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
6509 /* A supported extension chunk */
6510 struct sctp_supported_chunk_types_param *pr_supported;
6511 uint8_t local_store[SCTP_PARAM_BUFFER_SIZE];
6514 phdr = sctp_get_next_param(m, offset,
6515 (struct sctp_paramhdr *)&local_store, min(sizeof(local_store), plen));
6519 stcb->asoc.peer_supports_asconf = 0;
6520 stcb->asoc.peer_supports_prsctp = 0;
6521 stcb->asoc.peer_supports_pktdrop = 0;
6522 stcb->asoc.peer_supports_strreset = 0;
6523 stcb->asoc.peer_supports_nr_sack = 0;
6524 stcb->asoc.peer_supports_auth = 0;
6525 pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
6526 num_ent = plen - sizeof(struct sctp_paramhdr);
6527 for (i = 0; i < num_ent; i++) {
6528 switch (pr_supported->chunk_types[i]) {
6530 case SCTP_ASCONF_ACK:
6531 stcb->asoc.peer_supports_asconf = 1;
6533 case SCTP_FORWARD_CUM_TSN:
6534 stcb->asoc.peer_supports_prsctp = 1;
6536 case SCTP_PACKET_DROPPED:
6537 stcb->asoc.peer_supports_pktdrop = 1;
6539 case SCTP_NR_SELECTIVE_ACK:
6540 stcb->asoc.peer_supports_nr_sack = 1;
6542 case SCTP_STREAM_RESET:
6543 stcb->asoc.peer_supports_strreset = 1;
6545 case SCTP_AUTHENTICATION:
6546 stcb->asoc.peer_supports_auth = 1;
6549 /* one I have not learned yet */
6554 } else if (ptype == SCTP_RANDOM) {
6555 if (plen > sizeof(random_store))
6558 /* already processed a RANDOM */
6561 phdr = sctp_get_next_param(m, offset,
6562 (struct sctp_paramhdr *)random_store,
6563 min(sizeof(random_store), plen));
6566 p_random = (struct sctp_auth_random *)phdr;
6567 random_len = plen - sizeof(*p_random);
6568 /* enforce the random length */
6569 if (random_len != SCTP_AUTH_RANDOM_SIZE_REQUIRED) {
6570 SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: invalid RANDOM len\n");
6574 } else if (ptype == SCTP_HMAC_LIST) {
6578 if (plen > sizeof(hmacs_store))
6581 /* already processed a HMAC list */
6584 phdr = sctp_get_next_param(m, offset,
6585 (struct sctp_paramhdr *)hmacs_store,
6586 min(plen, sizeof(hmacs_store)));
6589 hmacs = (struct sctp_auth_hmac_algo *)phdr;
6590 hmacs_len = plen - sizeof(*hmacs);
6591 num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]);
6592 /* validate the hmac list */
6593 if (sctp_verify_hmac_param(hmacs, num_hmacs)) {
6596 if (stcb->asoc.peer_hmacs != NULL)
6597 sctp_free_hmaclist(stcb->asoc.peer_hmacs);
6598 stcb->asoc.peer_hmacs = sctp_alloc_hmaclist(num_hmacs);
6599 if (stcb->asoc.peer_hmacs != NULL) {
6600 for (i = 0; i < num_hmacs; i++) {
6601 (void)sctp_auth_add_hmacid(stcb->asoc.peer_hmacs,
6602 ntohs(hmacs->hmac_ids[i]));
6606 } else if (ptype == SCTP_CHUNK_LIST) {
6609 if (plen > sizeof(chunks_store))
6612 /* already processed a Chunks list */
6615 phdr = sctp_get_next_param(m, offset,
6616 (struct sctp_paramhdr *)chunks_store,
6617 min(plen, sizeof(chunks_store)));
6620 chunks = (struct sctp_auth_chunk_list *)phdr;
6621 num_chunks = plen - sizeof(*chunks);
6622 if (stcb->asoc.peer_auth_chunks != NULL)
6623 sctp_clear_chunklist(stcb->asoc.peer_auth_chunks);
6625 stcb->asoc.peer_auth_chunks = sctp_alloc_chunklist();
6626 for (i = 0; i < num_chunks; i++) {
6627 (void)sctp_auth_add_chunk(chunks->chunk_types[i],
6628 stcb->asoc.peer_auth_chunks);
6629 /* record asconf/asconf-ack if listed */
6630 if (chunks->chunk_types[i] == SCTP_ASCONF)
6632 if (chunks->chunk_types[i] == SCTP_ASCONF_ACK)
6637 } else if ((ptype == SCTP_HEARTBEAT_INFO) ||
6638 (ptype == SCTP_STATE_COOKIE) ||
6639 (ptype == SCTP_UNRECOG_PARAM) ||
6640 (ptype == SCTP_COOKIE_PRESERVE) ||
6641 (ptype == SCTP_SUPPORTED_ADDRTYPE) ||
6642 (ptype == SCTP_ADD_IP_ADDRESS) ||
6643 (ptype == SCTP_DEL_IP_ADDRESS) ||
6644 (ptype == SCTP_ERROR_CAUSE_IND) ||
6645 (ptype == SCTP_SUCCESS_REPORT)) {
6648 if ((ptype & 0x8000) == 0x0000) {
6650 * must stop processing the rest of the
6651 * param's. Any report bits were handled
6653 * sctp_arethere_unrecognized_parameters()
6654 * when the INIT or INIT-ACK was first seen.
6661 offset += SCTP_SIZE32(plen);
6662 if (offset >= limit) {
6665 phdr = sctp_get_next_param(m, offset, &parm_buf,
6668 /* Now check to see if we need to purge any addresses */
6669 TAILQ_FOREACH_SAFE(net, &stcb->asoc.nets, sctp_next, nnet) {
6670 if ((net->dest_state & SCTP_ADDR_NOT_IN_ASSOC) ==
6671 SCTP_ADDR_NOT_IN_ASSOC) {
6672 /* This address has been removed from the asoc */
6673 /* remove and free it */
6674 stcb->asoc.numnets--;
6675 TAILQ_REMOVE(&stcb->asoc.nets, net, sctp_next);
6676 sctp_free_remote_addr(net);
6677 if (net == stcb->asoc.primary_destination) {
6678 stcb->asoc.primary_destination = NULL;
6679 sctp_select_primary_destination(stcb);
6683 if (ecn_allowed == 0) {
6684 stcb->asoc.ecn_allowed = 0;
6686 /* validate authentication required parameters */
6687 if (got_random && got_hmacs) {
6688 stcb->asoc.peer_supports_auth = 1;
6690 stcb->asoc.peer_supports_auth = 0;
6692 if (!stcb->asoc.peer_supports_auth && got_chklist) {
6693 /* peer does not support auth but sent a chunks list? */
6696 if (!SCTP_BASE_SYSCTL(sctp_asconf_auth_nochk) && stcb->asoc.peer_supports_asconf &&
6697 !stcb->asoc.peer_supports_auth) {
6698 /* peer supports asconf but not auth? */
6700 } else if ((stcb->asoc.peer_supports_asconf) && (stcb->asoc.peer_supports_auth) &&
6701 ((saw_asconf == 0) || (saw_asconf_ack == 0))) {
6704 /* concatenate the full random key */
6705 keylen = sizeof(*p_random) + random_len + sizeof(*hmacs) + hmacs_len;
6706 if (chunks != NULL) {
6707 keylen += sizeof(*chunks) + num_chunks;
6709 new_key = sctp_alloc_key(keylen);
6710 if (new_key != NULL) {
6711 /* copy in the RANDOM */
6712 if (p_random != NULL) {
6713 keylen = sizeof(*p_random) + random_len;
6714 bcopy(p_random, new_key->key, keylen);
6716 /* append in the AUTH chunks */
6717 if (chunks != NULL) {
6718 bcopy(chunks, new_key->key + keylen,
6719 sizeof(*chunks) + num_chunks);
6720 keylen += sizeof(*chunks) + num_chunks;
6722 /* append in the HMACs */
6723 if (hmacs != NULL) {
6724 bcopy(hmacs, new_key->key + keylen,
6725 sizeof(*hmacs) + hmacs_len);
6728 /* failed to get memory for the key */
6731 if (stcb->asoc.authinfo.peer_random != NULL)
6732 sctp_free_key(stcb->asoc.authinfo.peer_random);
6733 stcb->asoc.authinfo.peer_random = new_key;
6734 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid);
6735 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid);
6741 sctp_set_primary_addr(struct sctp_tcb *stcb, struct sockaddr *sa,
6742 struct sctp_nets *net)
6744 /* make sure the requested primary address exists in the assoc */
6745 if (net == NULL && sa)
6746 net = sctp_findnet(stcb, sa);
6749 /* didn't find the requested primary address! */
6752 /* set the primary address */
6753 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
6754 /* Must be confirmed, so queue to set */
6755 net->dest_state |= SCTP_ADDR_REQ_PRIMARY;
6758 stcb->asoc.primary_destination = net;
6759 net->dest_state &= ~SCTP_ADDR_WAS_PRIMARY;
6760 net = TAILQ_FIRST(&stcb->asoc.nets);
6761 if (net != stcb->asoc.primary_destination) {
6763 * first one on the list is NOT the primary
6764 * sctp_cmpaddr() is much more efficient if the
6765 * primary is the first on the list, make it so.
6767 TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
6768 TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
6775 sctp_is_vtag_good(struct sctp_inpcb *inp, uint32_t tag, uint16_t lport, uint16_t rport, struct timeval *now, int save_in_twait)
6778 * This function serves two purposes. It will see if a TAG can be
6779 * re-used and return 1 for yes it is ok and 0 for don't use that
6780 * tag. A secondary function it will do is purge out old tags that
6783 struct sctpvtaghead *chain;
6784 struct sctp_tagblock *twait_block;
6785 struct sctpasochead *head;
6786 struct sctp_tcb *stcb;
6789 SCTP_INP_INFO_RLOCK();
6790 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
6791 SCTP_BASE_INFO(hashasocmark))];
6794 goto skip_vtag_check;
6796 LIST_FOREACH(stcb, head, sctp_asocs) {
6798 * We choose not to lock anything here. TCB's can't be
6799 * removed since we have the read lock, so they can't be
6800 * freed on us, same thing for the INP. I may be wrong with
6801 * this assumption, but we will go with it for now :-)
6803 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
6806 if (stcb->asoc.my_vtag == tag) {
6808 if (stcb->rport != rport) {
6811 if (stcb->sctp_ep->sctp_lport != lport) {
6814 /* Its a used tag set */
6815 SCTP_INP_INFO_RUNLOCK();
6821 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
6822 /* Now what about timed wait ? */
6823 if (!LIST_EMPTY(chain)) {
6825 * Block(s) are present, lets see if we have this tag in the
6828 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
6829 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
6830 if (twait_block->vtag_block[i].v_tag == 0) {
6833 } else if ((long)twait_block->vtag_block[i].tv_sec_at_expire <
6835 /* Audit expires this guy */
6836 twait_block->vtag_block[i].tv_sec_at_expire = 0;
6837 twait_block->vtag_block[i].v_tag = 0;
6838 twait_block->vtag_block[i].lport = 0;
6839 twait_block->vtag_block[i].rport = 0;
6840 } else if ((twait_block->vtag_block[i].v_tag == tag) &&
6841 (twait_block->vtag_block[i].lport == lport) &&
6842 (twait_block->vtag_block[i].rport == rport)) {
6843 /* Bad tag, sorry :< */
6844 SCTP_INP_INFO_RUNLOCK();
6850 SCTP_INP_INFO_RUNLOCK();
6855 static sctp_assoc_t reneged_asoc_ids[256];
6856 static uint8_t reneged_at = 0;
6860 sctp_drain_mbufs(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
6863 * We must hunt this association for MBUF's past the cumack (i.e.
6864 * out of order data that we can renege on).
6866 struct sctp_association *asoc;
6867 struct sctp_tmit_chunk *chk, *nchk;
6868 uint32_t cumulative_tsn_p1;
6869 struct sctp_queued_to_read *ctl, *nctl;
6874 /* We look for anything larger than the cum-ack + 1 */
6877 if (asoc->cumulative_tsn == asoc->highest_tsn_inside_map) {
6878 /* none we can reneg on. */
6881 SCTP_STAT_INCR(sctps_protocol_drains_done);
6882 cumulative_tsn_p1 = asoc->cumulative_tsn + 1;
6884 /* First look in the re-assembly queue */
6885 TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
6886 if (SCTP_TSN_GT(chk->rec.data.TSN_seq, cumulative_tsn_p1)) {
6887 /* Yep it is above cum-ack */
6889 SCTP_CALC_TSN_TO_GAP(gap, chk->rec.data.TSN_seq, asoc->mapping_array_base_tsn);
6890 asoc->size_on_reasm_queue = sctp_sbspace_sub(asoc->size_on_reasm_queue, chk->send_size);
6891 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
6892 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
6893 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
6895 sctp_m_freem(chk->data);
6898 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
6901 /* Ok that was fun, now we will drain all the inbound streams? */
6902 for (strmat = 0; strmat < asoc->streamincnt; strmat++) {
6903 TAILQ_FOREACH_SAFE(ctl, &asoc->strmin[strmat].inqueue, next, nctl) {
6904 if (SCTP_TSN_GT(ctl->sinfo_tsn, cumulative_tsn_p1)) {
6905 /* Yep it is above cum-ack */
6907 SCTP_CALC_TSN_TO_GAP(gap, ctl->sinfo_tsn, asoc->mapping_array_base_tsn);
6908 asoc->size_on_all_streams = sctp_sbspace_sub(asoc->size_on_all_streams, ctl->length);
6909 sctp_ucount_decr(asoc->cnt_on_all_streams);
6910 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
6911 TAILQ_REMOVE(&asoc->strmin[strmat].inqueue, ctl, next);
6913 sctp_m_freem(ctl->data);
6916 sctp_free_remote_addr(ctl->whoFrom);
6917 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), ctl);
6918 SCTP_DECR_READQ_COUNT();
6923 /* We must back down to see what the new highest is */
6924 for (i = asoc->highest_tsn_inside_map; SCTP_TSN_GE(i, asoc->mapping_array_base_tsn); i--) {
6925 SCTP_CALC_TSN_TO_GAP(gap, i, asoc->mapping_array_base_tsn);
6926 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
6927 asoc->highest_tsn_inside_map = i;
6933 asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn - 1;
6936 * Question, should we go through the delivery queue? The
6937 * only reason things are on here is the app not reading OR
6938 * a p-d-api up. An attacker COULD send enough in to
6939 * initiate the PD-API and then send a bunch of stuff to
6940 * other streams... these would wind up on the delivery
6941 * queue.. and then we would not get to them. But in order
6942 * to do this I then have to back-track and un-deliver
6943 * sequence numbers in streams.. el-yucko. I think for now
6944 * we will NOT look at the delivery queue and leave it to be
6945 * something to consider later. An alternative would be to
6946 * abort the P-D-API with a notification and then deliver
6947 * the data.... Or another method might be to keep track of
6948 * how many times the situation occurs and if we see a
6949 * possible attack underway just abort the association.
6952 SCTPDBG(SCTP_DEBUG_PCB1, "Freed %d chunks from reneg harvest\n", cnt);
6955 * Now do we need to find a new
6956 * asoc->highest_tsn_inside_map?
6958 asoc->last_revoke_count = cnt;
6959 (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
6960 /* sa_ignore NO_NULL_CHK */
6961 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
6962 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_DRAIN, SCTP_SO_NOT_LOCKED);
6963 reneged_asoc_ids[reneged_at] = sctp_get_associd(stcb);
6967 * Another issue, in un-setting the TSN's in the mapping array we
6968 * DID NOT adjust the highest_tsn marker. This will cause one of
6969 * two things to occur. It may cause us to do extra work in checking
6970 * for our mapping array movement. More importantly it may cause us
6971 * to SACK every datagram. This may not be a bad thing though since
6972 * we will recover once we get our cum-ack above and all this stuff
6973 * we dumped recovered.
6981 * We must walk the PCB lists for ALL associations here. The system
6982 * is LOW on MBUF's and needs help. This is where reneging will
6983 * occur. We really hope this does NOT happen!
6985 VNET_ITERATOR_DECL(vnet_iter);
6986 VNET_LIST_RLOCK_NOSLEEP();
6987 VNET_FOREACH(vnet_iter) {
6988 CURVNET_SET(vnet_iter);
6989 struct sctp_inpcb *inp;
6990 struct sctp_tcb *stcb;
6992 SCTP_STAT_INCR(sctps_protocol_drain_calls);
6993 if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
7000 SCTP_INP_INFO_RLOCK();
7001 LIST_FOREACH(inp, &SCTP_BASE_INFO(listhead), sctp_list) {
7002 /* For each endpoint */
7003 SCTP_INP_RLOCK(inp);
7004 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
7005 /* For each association */
7006 SCTP_TCB_LOCK(stcb);
7007 sctp_drain_mbufs(inp, stcb);
7008 SCTP_TCB_UNLOCK(stcb);
7010 SCTP_INP_RUNLOCK(inp);
7012 SCTP_INP_INFO_RUNLOCK();
7015 VNET_LIST_RUNLOCK_NOSLEEP();
7019 * start a new iterator
7020 * iterates through all endpoints and associations based on the pcb_state
7021 * flags and asoc_state. "af" (mandatory) is executed for all matching
7022 * assocs and "ef" (optional) is executed when the iterator completes.
7023 * "inpf" (optional) is executed for each new endpoint as it is being
7024 * iterated through. inpe (optional) is called when the inp completes
7025 * its way through all the stcbs.
7028 sctp_initiate_iterator(inp_func inpf,
7032 uint32_t pcb_features,
7033 uint32_t asoc_state,
7037 struct sctp_inpcb *s_inp,
7038 uint8_t chunk_output_off)
7040 struct sctp_iterator *it = NULL;
7045 SCTP_MALLOC(it, struct sctp_iterator *, sizeof(struct sctp_iterator),
7048 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
7051 memset(it, 0, sizeof(*it));
7052 it->function_assoc = af;
7053 it->function_inp = inpf;
7055 it->done_current_ep = 0;
7057 it->done_current_ep = 1;
7058 it->function_atend = ef;
7061 it->pcb_flags = pcb_state;
7062 it->pcb_features = pcb_features;
7063 it->asoc_state = asoc_state;
7064 it->function_inp_end = inpe;
7065 it->no_chunk_output = chunk_output_off;
7068 /* Assume lock is held here */
7070 SCTP_INP_INCR_REF(it->inp);
7071 it->iterator_flags = SCTP_ITERATOR_DO_SINGLE_INP;
7073 SCTP_INP_INFO_RLOCK();
7074 it->inp = LIST_FIRST(&SCTP_BASE_INFO(listhead));
7076 SCTP_INP_INCR_REF(it->inp);
7078 SCTP_INP_INFO_RUNLOCK();
7079 it->iterator_flags = SCTP_ITERATOR_DO_ALL_INP;
7082 SCTP_IPI_ITERATOR_WQ_LOCK();
7084 TAILQ_INSERT_TAIL(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
7085 if (sctp_it_ctl.iterator_running == 0) {
7086 sctp_wakeup_iterator();
7088 SCTP_IPI_ITERATOR_WQ_UNLOCK();
7089 /* sa_ignore MEMLEAK {memory is put on the tailq for the iterator} */