2 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4 * Copyright (c) 2008-2012, 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 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <netinet/sctp_os.h>
38 #include <netinet/sctp_var.h>
39 #include <netinet/sctp_sysctl.h>
40 #include <netinet/sctp_pcb.h>
41 #include <netinet/sctputil.h>
42 #include <netinet/sctp.h>
43 #include <netinet/sctp_header.h>
44 #include <netinet/sctp_asconf.h>
45 #include <netinet/sctp_output.h>
46 #include <netinet/sctp_timer.h>
47 #include <netinet/sctp_bsd_addr.h>
48 #include <netinet/sctp_dtrace_define.h>
49 #include <netinet/udp.h>
51 #include <netinet6/ip6_var.h>
53 #include <sys/sched.h>
55 #include <sys/unistd.h>
58 VNET_DEFINE(struct sctp_base_info, system_base_info);
60 /* FIX: we don't handle multiple link local scopes */
61 /* "scopeless" replacement IN6_ARE_ADDR_EQUAL */
64 SCTP6_ARE_ADDR_EQUAL(struct sockaddr_in6 *a, struct sockaddr_in6 *b)
66 struct sockaddr_in6 tmp_a, tmp_b;
68 memcpy(&tmp_a, a, sizeof(struct sockaddr_in6));
69 if (sa6_embedscope(&tmp_a, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
72 memcpy(&tmp_b, b, sizeof(struct sockaddr_in6));
73 if (sa6_embedscope(&tmp_b, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
76 return (IN6_ARE_ADDR_EQUAL(&tmp_a.sin6_addr, &tmp_b.sin6_addr));
82 sctp_fill_pcbinfo(struct sctp_pcbinfo *spcb)
85 * We really don't need to lock this, but I will just because it
88 SCTP_INP_INFO_RLOCK();
89 spcb->ep_count = SCTP_BASE_INFO(ipi_count_ep);
90 spcb->asoc_count = SCTP_BASE_INFO(ipi_count_asoc);
91 spcb->laddr_count = SCTP_BASE_INFO(ipi_count_laddr);
92 spcb->raddr_count = SCTP_BASE_INFO(ipi_count_raddr);
93 spcb->chk_count = SCTP_BASE_INFO(ipi_count_chunk);
94 spcb->readq_count = SCTP_BASE_INFO(ipi_count_readq);
95 spcb->stream_oque = SCTP_BASE_INFO(ipi_count_strmoq);
96 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)) {
217 sctp_find_vrf(uint32_t vrf_id)
219 struct sctp_vrflist *bucket;
220 struct sctp_vrf *liste;
222 bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(vrf_id & SCTP_BASE_INFO(hashvrfmark))];
223 LIST_FOREACH(liste, bucket, next_vrf) {
224 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);
249 sctp_free_ifn(struct sctp_ifn *sctp_ifnp)
251 if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&sctp_ifnp->refcount)) {
252 /* We zero'd the count */
253 if (sctp_ifnp->vrf) {
254 sctp_free_vrf(sctp_ifnp->vrf);
256 SCTP_FREE(sctp_ifnp, SCTP_M_IFN);
257 atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_ifns), 1);
263 sctp_update_ifn_mtu(uint32_t ifn_index, uint32_t mtu)
265 struct sctp_ifn *sctp_ifnp;
267 sctp_ifnp = sctp_find_ifn((void *)NULL, ifn_index);
268 if (sctp_ifnp != NULL) {
269 sctp_ifnp->ifn_mtu = mtu;
275 sctp_free_ifa(struct sctp_ifa *sctp_ifap)
277 if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&sctp_ifap->refcount)) {
278 /* We zero'd the count */
279 if (sctp_ifap->ifn_p) {
280 sctp_free_ifn(sctp_ifap->ifn_p);
282 SCTP_FREE(sctp_ifap, SCTP_M_IFA);
283 atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_ifas), 1);
289 sctp_delete_ifn(struct sctp_ifn *sctp_ifnp, int hold_addr_lock)
291 struct sctp_ifn *found;
293 found = sctp_find_ifn(sctp_ifnp->ifn_p, sctp_ifnp->ifn_index);
295 /* Not in the list.. sorry */
298 if (hold_addr_lock == 0)
299 SCTP_IPI_ADDR_WLOCK();
300 LIST_REMOVE(sctp_ifnp, next_bucket);
301 LIST_REMOVE(sctp_ifnp, next_ifn);
302 SCTP_DEREGISTER_INTERFACE(sctp_ifnp->ifn_index,
303 sctp_ifnp->registered_af);
304 if (hold_addr_lock == 0)
305 SCTP_IPI_ADDR_WUNLOCK();
306 /* Take away the reference, and possibly free it */
307 sctp_free_ifn(sctp_ifnp);
312 sctp_mark_ifa_addr_down(uint32_t vrf_id, struct sockaddr *addr,
313 const char *if_name, uint32_t ifn_index)
315 struct sctp_vrf *vrf;
316 struct sctp_ifa *sctp_ifap;
318 SCTP_IPI_ADDR_RLOCK();
319 vrf = sctp_find_vrf(vrf_id);
321 SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id);
325 sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
326 if (sctp_ifap == NULL) {
327 SCTPDBG(SCTP_DEBUG_PCB4, "Can't find sctp_ifap for address\n");
330 if (sctp_ifap->ifn_p == NULL) {
331 SCTPDBG(SCTP_DEBUG_PCB4, "IFA has no IFN - can't mark unuseable\n");
335 if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, SCTP_IFNAMSIZ) != 0) {
336 SCTPDBG(SCTP_DEBUG_PCB4, "IFN %s of IFA not the same as %s\n",
337 sctp_ifap->ifn_p->ifn_name, if_name);
341 if (sctp_ifap->ifn_p->ifn_index != ifn_index) {
342 SCTPDBG(SCTP_DEBUG_PCB4, "IFA owned by ifn_index:%d down command for ifn_index:%d - ignored\n",
343 sctp_ifap->ifn_p->ifn_index, ifn_index);
348 sctp_ifap->localifa_flags &= (~SCTP_ADDR_VALID);
349 sctp_ifap->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE;
351 SCTP_IPI_ADDR_RUNLOCK();
356 sctp_mark_ifa_addr_up(uint32_t vrf_id, struct sockaddr *addr,
357 const char *if_name, uint32_t ifn_index)
359 struct sctp_vrf *vrf;
360 struct sctp_ifa *sctp_ifap;
362 SCTP_IPI_ADDR_RLOCK();
363 vrf = sctp_find_vrf(vrf_id);
365 SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id);
369 sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
370 if (sctp_ifap == NULL) {
371 SCTPDBG(SCTP_DEBUG_PCB4, "Can't find sctp_ifap for address\n");
374 if (sctp_ifap->ifn_p == NULL) {
375 SCTPDBG(SCTP_DEBUG_PCB4, "IFA has no IFN - can't mark unuseable\n");
379 if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, SCTP_IFNAMSIZ) != 0) {
380 SCTPDBG(SCTP_DEBUG_PCB4, "IFN %s of IFA not the same as %s\n",
381 sctp_ifap->ifn_p->ifn_name, if_name);
385 if (sctp_ifap->ifn_p->ifn_index != ifn_index) {
386 SCTPDBG(SCTP_DEBUG_PCB4, "IFA owned by ifn_index:%d down command for ifn_index:%d - ignored\n",
387 sctp_ifap->ifn_p->ifn_index, ifn_index);
392 sctp_ifap->localifa_flags &= (~SCTP_ADDR_IFA_UNUSEABLE);
393 sctp_ifap->localifa_flags |= SCTP_ADDR_VALID;
395 SCTP_IPI_ADDR_RUNLOCK();
400 * Add an ifa to an ifn.
401 * Register the interface as necessary.
402 * NOTE: ADDR write lock MUST be held.
405 sctp_add_ifa_to_ifn(struct sctp_ifn *sctp_ifnp, struct sctp_ifa *sctp_ifap)
409 LIST_INSERT_HEAD(&sctp_ifnp->ifalist, sctp_ifap, next_ifa);
410 sctp_ifap->ifn_p = sctp_ifnp;
411 atomic_add_int(&sctp_ifap->ifn_p->refcount, 1);
412 /* update address counts */
413 sctp_ifnp->ifa_count++;
414 ifa_af = sctp_ifap->address.sa.sa_family;
429 if (sctp_ifnp->ifa_count == 1) {
430 /* register the new interface */
431 SCTP_REGISTER_INTERFACE(sctp_ifnp->ifn_index, ifa_af);
432 sctp_ifnp->registered_af = ifa_af;
438 * Remove an ifa from its ifn.
439 * If no more addresses exist, remove the ifn too. Otherwise, re-register
440 * the interface based on the remaining address families left.
441 * NOTE: ADDR write lock MUST be held.
444 sctp_remove_ifa_from_ifn(struct sctp_ifa *sctp_ifap)
446 LIST_REMOVE(sctp_ifap, next_ifa);
447 if (sctp_ifap->ifn_p) {
448 /* update address counts */
449 sctp_ifap->ifn_p->ifa_count--;
450 switch (sctp_ifap->address.sa.sa_family) {
453 sctp_ifap->ifn_p->num_v4--;
458 sctp_ifap->ifn_p->num_v6--;
465 if (LIST_EMPTY(&sctp_ifap->ifn_p->ifalist)) {
466 /* remove the ifn, possibly freeing it */
467 sctp_delete_ifn(sctp_ifap->ifn_p, SCTP_ADDR_LOCKED);
469 /* re-register address family type, if needed */
470 if ((sctp_ifap->ifn_p->num_v6 == 0) &&
471 (sctp_ifap->ifn_p->registered_af == AF_INET6)) {
472 SCTP_DEREGISTER_INTERFACE(sctp_ifap->ifn_p->ifn_index, AF_INET6);
473 SCTP_REGISTER_INTERFACE(sctp_ifap->ifn_p->ifn_index, AF_INET);
474 sctp_ifap->ifn_p->registered_af = AF_INET;
475 } else if ((sctp_ifap->ifn_p->num_v4 == 0) &&
476 (sctp_ifap->ifn_p->registered_af == AF_INET)) {
477 SCTP_DEREGISTER_INTERFACE(sctp_ifap->ifn_p->ifn_index, AF_INET);
478 SCTP_REGISTER_INTERFACE(sctp_ifap->ifn_p->ifn_index, AF_INET6);
479 sctp_ifap->ifn_p->registered_af = AF_INET6;
481 /* free the ifn refcount */
482 sctp_free_ifn(sctp_ifap->ifn_p);
484 sctp_ifap->ifn_p = NULL;
490 sctp_add_addr_to_vrf(uint32_t vrf_id, void *ifn, uint32_t ifn_index,
491 uint32_t ifn_type, const char *if_name, void *ifa,
492 struct sockaddr *addr, uint32_t ifa_flags,
495 struct sctp_vrf *vrf;
496 struct sctp_ifn *sctp_ifnp = NULL;
497 struct sctp_ifa *sctp_ifap = NULL;
498 struct sctp_ifalist *hash_addr_head;
499 struct sctp_ifnlist *hash_ifn_head;
500 uint32_t hash_of_addr;
504 SCTPDBG(SCTP_DEBUG_PCB4, "vrf_id 0x%x: adding address: ", vrf_id);
505 SCTPDBG_ADDR(SCTP_DEBUG_PCB4, addr);
507 SCTP_IPI_ADDR_WLOCK();
508 sctp_ifnp = sctp_find_ifn(ifn, ifn_index);
510 vrf = sctp_ifnp->vrf;
512 vrf = sctp_find_vrf(vrf_id);
514 vrf = sctp_allocate_vrf(vrf_id);
516 SCTP_IPI_ADDR_WUNLOCK();
521 if (sctp_ifnp == NULL) {
523 * build one and add it, can't hold lock until after malloc
526 SCTP_IPI_ADDR_WUNLOCK();
527 SCTP_MALLOC(sctp_ifnp, struct sctp_ifn *,
528 sizeof(struct sctp_ifn), SCTP_M_IFN);
529 if (sctp_ifnp == NULL) {
531 panic("No memory for IFN");
535 memset(sctp_ifnp, 0, sizeof(struct sctp_ifn));
536 sctp_ifnp->ifn_index = ifn_index;
537 sctp_ifnp->ifn_p = ifn;
538 sctp_ifnp->ifn_type = ifn_type;
539 sctp_ifnp->refcount = 0;
540 sctp_ifnp->vrf = vrf;
541 atomic_add_int(&vrf->refcount, 1);
542 sctp_ifnp->ifn_mtu = SCTP_GATHER_MTU_FROM_IFN_INFO(ifn, ifn_index, addr->sa_family);
543 if (if_name != NULL) {
544 snprintf(sctp_ifnp->ifn_name, SCTP_IFNAMSIZ, "%s", if_name);
546 snprintf(sctp_ifnp->ifn_name, SCTP_IFNAMSIZ, "%s", "unknown");
548 hash_ifn_head = &SCTP_BASE_INFO(vrf_ifn_hash)[(ifn_index & SCTP_BASE_INFO(vrf_ifn_hashmark))];
549 LIST_INIT(&sctp_ifnp->ifalist);
550 SCTP_IPI_ADDR_WLOCK();
551 LIST_INSERT_HEAD(hash_ifn_head, sctp_ifnp, next_bucket);
552 LIST_INSERT_HEAD(&vrf->ifnlist, sctp_ifnp, next_ifn);
553 atomic_add_int(&SCTP_BASE_INFO(ipi_count_ifns), 1);
556 sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
558 /* Hmm, it already exists? */
559 if ((sctp_ifap->ifn_p) &&
560 (sctp_ifap->ifn_p->ifn_index == ifn_index)) {
561 SCTPDBG(SCTP_DEBUG_PCB4, "Using existing ifn %s (0x%x) for ifa %p\n",
562 sctp_ifap->ifn_p->ifn_name, ifn_index,
565 /* Remove the created one that we don't want */
566 sctp_delete_ifn(sctp_ifnp, SCTP_ADDR_LOCKED);
568 if (sctp_ifap->localifa_flags & SCTP_BEING_DELETED) {
569 /* easy to solve, just switch back to active */
570 SCTPDBG(SCTP_DEBUG_PCB4, "Clearing deleted ifa flag\n");
571 sctp_ifap->localifa_flags = SCTP_ADDR_VALID;
572 sctp_ifap->ifn_p = sctp_ifnp;
573 atomic_add_int(&sctp_ifap->ifn_p->refcount, 1);
576 SCTP_IPI_ADDR_WUNLOCK();
579 if (sctp_ifap->ifn_p) {
581 * The last IFN gets the address, remove the
584 SCTPDBG(SCTP_DEBUG_PCB4, "Moving ifa %p from %s (0x%x) to %s (0x%x)\n",
585 (void *)sctp_ifap, sctp_ifap->ifn_p->ifn_name,
586 sctp_ifap->ifn_p->ifn_index, if_name,
588 /* remove the address from the old ifn */
589 sctp_remove_ifa_from_ifn(sctp_ifap);
590 /* move the address over to the new ifn */
591 sctp_add_ifa_to_ifn(sctp_ifnp, sctp_ifap);
592 goto exit_stage_left;
594 /* repair ifnp which was NULL ? */
595 sctp_ifap->localifa_flags = SCTP_ADDR_VALID;
596 SCTPDBG(SCTP_DEBUG_PCB4, "Repairing ifn %p for ifa %p\n",
597 (void *)sctp_ifnp, (void *)sctp_ifap);
598 sctp_add_ifa_to_ifn(sctp_ifnp, sctp_ifap);
600 goto exit_stage_left;
603 SCTP_IPI_ADDR_WUNLOCK();
604 SCTP_MALLOC(sctp_ifap, struct sctp_ifa *, sizeof(struct sctp_ifa), SCTP_M_IFA);
605 if (sctp_ifap == NULL) {
607 panic("No memory for IFA");
611 memset(sctp_ifap, 0, sizeof(struct sctp_ifa));
612 sctp_ifap->ifn_p = sctp_ifnp;
613 atomic_add_int(&sctp_ifnp->refcount, 1);
614 sctp_ifap->vrf_id = vrf_id;
615 sctp_ifap->ifa = ifa;
616 memcpy(&sctp_ifap->address, addr, addr->sa_len);
617 sctp_ifap->localifa_flags = SCTP_ADDR_VALID | SCTP_ADDR_DEFER_USE;
618 sctp_ifap->flags = ifa_flags;
620 switch (sctp_ifap->address.sa.sa_family) {
624 struct sockaddr_in *sin;
626 sin = (struct sockaddr_in *)&sctp_ifap->address.sin;
627 if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) ||
628 (IN4_ISLOOPBACK_ADDRESS(&sin->sin_addr))) {
629 sctp_ifap->src_is_loop = 1;
631 if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
632 sctp_ifap->src_is_priv = 1;
636 new_ifn_af = AF_INET;
643 /* ok to use deprecated addresses? */
644 struct sockaddr_in6 *sin6;
646 sin6 = (struct sockaddr_in6 *)&sctp_ifap->address.sin6;
647 if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) ||
648 (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))) {
649 sctp_ifap->src_is_loop = 1;
651 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
652 sctp_ifap->src_is_priv = 1;
656 new_ifn_af = AF_INET6;
664 hash_of_addr = sctp_get_ifa_hash_val(&sctp_ifap->address.sa);
666 if ((sctp_ifap->src_is_priv == 0) &&
667 (sctp_ifap->src_is_loop == 0)) {
668 sctp_ifap->src_is_glob = 1;
670 SCTP_IPI_ADDR_WLOCK();
671 hash_addr_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)];
672 LIST_INSERT_HEAD(hash_addr_head, sctp_ifap, next_bucket);
673 sctp_ifap->refcount = 1;
674 LIST_INSERT_HEAD(&sctp_ifnp->ifalist, sctp_ifap, next_ifa);
675 sctp_ifnp->ifa_count++;
676 vrf->total_ifa_count++;
677 atomic_add_int(&SCTP_BASE_INFO(ipi_count_ifas), 1);
679 SCTP_REGISTER_INTERFACE(ifn_index, new_ifn_af);
680 sctp_ifnp->registered_af = new_ifn_af;
682 SCTP_IPI_ADDR_WUNLOCK();
685 * Bump up the refcount so that when the timer completes it
686 * will drop back down.
688 struct sctp_laddr *wi;
690 atomic_add_int(&sctp_ifap->refcount, 1);
691 wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
694 * Gak, what can we do? We have lost an address
695 * change can you say HOSED?
697 SCTPDBG(SCTP_DEBUG_PCB4, "Lost an address change?\n");
698 /* Opps, must decrement the count */
699 sctp_del_addr_from_vrf(vrf_id, addr, ifn_index,
703 SCTP_INCR_LADDR_COUNT();
704 bzero(wi, sizeof(*wi));
705 (void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
707 wi->action = SCTP_ADD_IP_ADDRESS;
710 LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
711 SCTP_WQ_ADDR_UNLOCK();
713 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
714 (struct sctp_inpcb *)NULL,
715 (struct sctp_tcb *)NULL,
716 (struct sctp_nets *)NULL);
718 /* it's ready for use */
719 sctp_ifap->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
725 sctp_del_addr_from_vrf(uint32_t vrf_id, struct sockaddr *addr,
726 uint32_t ifn_index, const char *if_name)
728 struct sctp_vrf *vrf;
729 struct sctp_ifa *sctp_ifap = NULL;
731 SCTP_IPI_ADDR_WLOCK();
732 vrf = sctp_find_vrf(vrf_id);
734 SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id);
738 SCTPDBG(SCTP_DEBUG_PCB4, "vrf_id 0x%x: deleting address:", vrf_id);
739 SCTPDBG_ADDR(SCTP_DEBUG_PCB4, addr);
741 sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
743 /* Validate the delete */
744 if (sctp_ifap->ifn_p) {
748 * The name has priority over the ifn_index
749 * if its given. We do this especially for
750 * panda who might recycle indexes fast.
753 if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, SCTP_IFNAMSIZ) == 0) {
754 /* They match its a correct delete */
759 /* last ditch check ifn_index */
760 if (ifn_index == sctp_ifap->ifn_p->ifn_index) {
765 SCTPDBG(SCTP_DEBUG_PCB4, "ifn:%d ifname:%s does not match addresses\n",
766 ifn_index, ((if_name == NULL) ? "NULL" : if_name));
767 SCTPDBG(SCTP_DEBUG_PCB4, "ifn:%d ifname:%s - ignoring delete\n",
768 sctp_ifap->ifn_p->ifn_index, sctp_ifap->ifn_p->ifn_name);
769 SCTP_IPI_ADDR_WUNLOCK();
773 SCTPDBG(SCTP_DEBUG_PCB4, "Deleting ifa %p\n", (void *)sctp_ifap);
774 sctp_ifap->localifa_flags &= SCTP_ADDR_VALID;
775 sctp_ifap->localifa_flags |= SCTP_BEING_DELETED;
776 vrf->total_ifa_count--;
777 LIST_REMOVE(sctp_ifap, next_bucket);
778 sctp_remove_ifa_from_ifn(sctp_ifap);
782 SCTPDBG(SCTP_DEBUG_PCB4, "Del Addr-ifn:%d Could not find address:",
784 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr);
789 SCTP_IPI_ADDR_WUNLOCK();
791 struct sctp_laddr *wi;
793 wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
796 * Gak, what can we do? We have lost an address
797 * change can you say HOSED?
799 SCTPDBG(SCTP_DEBUG_PCB4, "Lost an address change?\n");
801 /* Oops, must decrement the count */
802 sctp_free_ifa(sctp_ifap);
805 SCTP_INCR_LADDR_COUNT();
806 bzero(wi, sizeof(*wi));
807 (void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
809 wi->action = SCTP_DEL_IP_ADDRESS;
812 * Should this really be a tailq? As it is we will process
813 * the newest first :-0
815 LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
816 SCTP_WQ_ADDR_UNLOCK();
818 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
819 (struct sctp_inpcb *)NULL,
820 (struct sctp_tcb *)NULL,
821 (struct sctp_nets *)NULL);
828 sctp_does_stcb_own_this_addr(struct sctp_tcb *stcb, struct sockaddr *to)
833 int ipv4_local_scope, ipv4_addr_legal;
837 int local_scope, site_scope, ipv6_addr_legal;
840 struct sctp_vrf *vrf;
841 struct sctp_ifn *sctp_ifn;
842 struct sctp_ifa *sctp_ifa;
844 loopback_scope = stcb->asoc.scope.loopback_scope;
846 ipv4_local_scope = stcb->asoc.scope.ipv4_local_scope;
847 ipv4_addr_legal = stcb->asoc.scope.ipv4_addr_legal;
850 local_scope = stcb->asoc.scope.local_scope;
851 site_scope = stcb->asoc.scope.site_scope;
852 ipv6_addr_legal = stcb->asoc.scope.ipv6_addr_legal;
855 SCTP_IPI_ADDR_RLOCK();
856 vrf = sctp_find_vrf(stcb->asoc.vrf_id);
858 /* no vrf, no addresses */
859 SCTP_IPI_ADDR_RUNLOCK();
862 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
863 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
864 if ((loopback_scope == 0) &&
865 SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
868 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
869 if (sctp_is_addr_restricted(stcb, sctp_ifa) &&
870 (!sctp_is_addr_pending(stcb, sctp_ifa))) {
872 * We allow pending addresses, where
873 * we have sent an asconf-add to be
878 if (sctp_ifa->address.sa.sa_family != to->sa_family) {
881 switch (sctp_ifa->address.sa.sa_family) {
884 if (ipv4_addr_legal) {
885 struct sockaddr_in *sin,
888 sin = &sctp_ifa->address.sin;
889 rsin = (struct sockaddr_in *)to;
890 if ((ipv4_local_scope == 0) &&
891 IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
894 if (sin->sin_addr.s_addr == rsin->sin_addr.s_addr) {
895 SCTP_IPI_ADDR_RUNLOCK();
903 if (ipv6_addr_legal) {
904 struct sockaddr_in6 *sin6,
907 sin6 = &sctp_ifa->address.sin6;
908 rsin6 = (struct sockaddr_in6 *)to;
909 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
910 if (local_scope == 0)
912 if (sin6->sin6_scope_id == 0) {
913 if (sa6_recoverscope(sin6) != 0)
917 if ((site_scope == 0) &&
918 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
921 if (SCTP6_ARE_ADDR_EQUAL(sin6, rsin6)) {
922 SCTP_IPI_ADDR_RUNLOCK();
935 struct sctp_laddr *laddr;
937 LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) {
938 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
939 SCTPDBG(SCTP_DEBUG_PCB1, "ifa being deleted\n");
942 if (sctp_is_addr_restricted(stcb, laddr->ifa) &&
943 (!sctp_is_addr_pending(stcb, laddr->ifa))) {
945 * We allow pending addresses, where we have
946 * sent an asconf-add to be considered
951 if (laddr->ifa->address.sa.sa_family != to->sa_family) {
954 switch (to->sa_family) {
958 struct sockaddr_in *sin, *rsin;
960 sin = (struct sockaddr_in *)&laddr->ifa->address.sin;
961 rsin = (struct sockaddr_in *)to;
962 if (sin->sin_addr.s_addr == rsin->sin_addr.s_addr) {
963 SCTP_IPI_ADDR_RUNLOCK();
972 struct sockaddr_in6 *sin6, *rsin6;
974 sin6 = (struct sockaddr_in6 *)&laddr->ifa->address.sin6;
975 rsin6 = (struct sockaddr_in6 *)to;
976 if (SCTP6_ARE_ADDR_EQUAL(sin6, rsin6)) {
977 SCTP_IPI_ADDR_RUNLOCK();
991 SCTP_IPI_ADDR_RUNLOCK();
996 static struct sctp_tcb *
997 sctp_tcb_special_locate(struct sctp_inpcb **inp_p, struct sockaddr *from,
998 struct sockaddr *to, struct sctp_nets **netp, uint32_t vrf_id)
1000 /**** ASSUMES THE CALLER holds the INP_INFO_RLOCK */
1002 * If we support the TCP model, then we must now dig through to see
1003 * if we can find our endpoint in the list of tcp ep's.
1005 uint16_t lport, rport;
1006 struct sctppcbhead *ephead;
1007 struct sctp_inpcb *inp;
1008 struct sctp_laddr *laddr;
1009 struct sctp_tcb *stcb;
1010 struct sctp_nets *net;
1012 if ((to == NULL) || (from == NULL)) {
1015 switch (to->sa_family) {
1018 if (from->sa_family == AF_INET) {
1019 lport = ((struct sockaddr_in *)to)->sin_port;
1020 rport = ((struct sockaddr_in *)from)->sin_port;
1028 if (from->sa_family == AF_INET6) {
1029 lport = ((struct sockaddr_in6 *)to)->sin6_port;
1030 rport = ((struct sockaddr_in6 *)from)->sin6_port;
1039 ephead = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR((lport | rport), SCTP_BASE_INFO(hashtcpmark))];
1041 * Ok now for each of the guys in this bucket we must look and see:
1042 * - Does the remote port match. - Does there single association's
1043 * addresses match this address (to). If so we update p_ep to point
1044 * to this ep and return the tcb from it.
1046 LIST_FOREACH(inp, ephead, sctp_hash) {
1047 SCTP_INP_RLOCK(inp);
1048 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1049 SCTP_INP_RUNLOCK(inp);
1052 if (lport != inp->sctp_lport) {
1053 SCTP_INP_RUNLOCK(inp);
1056 if (inp->def_vrf_id != vrf_id) {
1057 SCTP_INP_RUNLOCK(inp);
1060 /* check to see if the ep has one of the addresses */
1061 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
1062 /* We are NOT bound all, so look further */
1065 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1067 if (laddr->ifa == NULL) {
1068 SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n", __FUNCTION__);
1071 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
1072 SCTPDBG(SCTP_DEBUG_PCB1, "ifa being deleted\n");
1075 if (laddr->ifa->address.sa.sa_family ==
1077 /* see if it matches */
1079 if (from->sa_family == AF_INET) {
1080 struct sockaddr_in *intf_addr,
1083 intf_addr = &laddr->ifa->address.sin;
1084 sin = (struct sockaddr_in *)to;
1085 if (sin->sin_addr.s_addr ==
1086 intf_addr->sin_addr.s_addr) {
1093 if (from->sa_family == AF_INET6) {
1094 struct sockaddr_in6 *intf_addr6;
1095 struct sockaddr_in6 *sin6;
1097 sin6 = (struct sockaddr_in6 *)
1099 intf_addr6 = &laddr->ifa->address.sin6;
1101 if (SCTP6_ARE_ADDR_EQUAL(sin6,
1111 /* This endpoint does not have this address */
1112 SCTP_INP_RUNLOCK(inp);
1117 * Ok if we hit here the ep has the address, does it hold
1120 /* XXX: Why don't we TAILQ_FOREACH through sctp_asoc_list? */
1121 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1123 SCTP_INP_RUNLOCK(inp);
1126 SCTP_TCB_LOCK(stcb);
1127 if (!sctp_does_stcb_own_this_addr(stcb, to)) {
1128 SCTP_TCB_UNLOCK(stcb);
1129 SCTP_INP_RUNLOCK(inp);
1132 if (stcb->rport != rport) {
1133 /* remote port does not match. */
1134 SCTP_TCB_UNLOCK(stcb);
1135 SCTP_INP_RUNLOCK(inp);
1138 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
1139 SCTP_TCB_UNLOCK(stcb);
1140 SCTP_INP_RUNLOCK(inp);
1143 if (!sctp_does_stcb_own_this_addr(stcb, to)) {
1144 SCTP_TCB_UNLOCK(stcb);
1145 SCTP_INP_RUNLOCK(inp);
1148 /* Does this TCB have a matching address? */
1149 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1151 if (net->ro._l_addr.sa.sa_family != from->sa_family) {
1152 /* not the same family, can't be a match */
1155 switch (from->sa_family) {
1159 struct sockaddr_in *sin, *rsin;
1161 sin = (struct sockaddr_in *)&net->ro._l_addr;
1162 rsin = (struct sockaddr_in *)from;
1163 if (sin->sin_addr.s_addr ==
1164 rsin->sin_addr.s_addr) {
1170 * Update the endpoint
1174 SCTP_INP_RUNLOCK(inp);
1183 struct sockaddr_in6 *sin6, *rsin6;
1185 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1186 rsin6 = (struct sockaddr_in6 *)from;
1187 if (SCTP6_ARE_ADDR_EQUAL(sin6,
1194 * Update the endpoint
1198 SCTP_INP_RUNLOCK(inp);
1209 SCTP_TCB_UNLOCK(stcb);
1210 SCTP_INP_RUNLOCK(inp);
1219 * 1) If I return a NULL you must decrement any INP ref cnt. 2) If I find an
1220 * stcb, both will be locked (locked_tcb and stcb) but decrement will be done
1221 * (if locked == NULL). 3) Decrement happens on return ONLY if locked ==
1226 sctp_findassociation_ep_addr(struct sctp_inpcb **inp_p, struct sockaddr *remote,
1227 struct sctp_nets **netp, struct sockaddr *local, struct sctp_tcb *locked_tcb)
1229 struct sctpasochead *head;
1230 struct sctp_inpcb *inp;
1231 struct sctp_tcb *stcb = NULL;
1232 struct sctp_nets *net;
1236 switch (remote->sa_family) {
1239 rport = (((struct sockaddr_in *)remote)->sin_port);
1244 rport = (((struct sockaddr_in6 *)remote)->sin6_port);
1252 * UN-lock so we can do proper locking here this occurs when
1253 * called from load_addresses_from_init.
1255 atomic_add_int(&locked_tcb->asoc.refcnt, 1);
1256 SCTP_TCB_UNLOCK(locked_tcb);
1258 SCTP_INP_INFO_RLOCK();
1259 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1260 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1262 * Now either this guy is our listener or it's the
1263 * connector. If it is the one that issued the connect, then
1264 * it's only chance is to be the first TCB in the list. If
1265 * it is the acceptor, then do the special_lookup to hash
1266 * and find the real inp.
1268 if ((inp->sctp_socket) && (inp->sctp_socket->so_qlimit)) {
1269 /* to is peer addr, from is my addr */
1270 stcb = sctp_tcb_special_locate(inp_p, remote, local,
1271 netp, inp->def_vrf_id);
1272 if ((stcb != NULL) && (locked_tcb == NULL)) {
1273 /* we have a locked tcb, lower refcount */
1274 SCTP_INP_DECR_REF(inp);
1276 if ((locked_tcb != NULL) && (locked_tcb != stcb)) {
1277 SCTP_INP_RLOCK(locked_tcb->sctp_ep);
1278 SCTP_TCB_LOCK(locked_tcb);
1279 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1280 SCTP_INP_RUNLOCK(locked_tcb->sctp_ep);
1282 SCTP_INP_INFO_RUNLOCK();
1285 SCTP_INP_WLOCK(inp);
1286 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1289 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1293 SCTP_TCB_LOCK(stcb);
1295 if (stcb->rport != rport) {
1296 /* remote port does not match. */
1297 SCTP_TCB_UNLOCK(stcb);
1300 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
1301 SCTP_TCB_UNLOCK(stcb);
1304 if (local && !sctp_does_stcb_own_this_addr(stcb, local)) {
1305 SCTP_TCB_UNLOCK(stcb);
1308 /* now look at the list of remote addresses */
1309 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1311 if (net == (TAILQ_NEXT(net, sctp_next))) {
1312 panic("Corrupt net list");
1315 if (net->ro._l_addr.sa.sa_family !=
1316 remote->sa_family) {
1317 /* not the same family */
1320 switch (remote->sa_family) {
1324 struct sockaddr_in *sin,
1327 sin = (struct sockaddr_in *)
1329 rsin = (struct sockaddr_in *)remote;
1330 if (sin->sin_addr.s_addr ==
1331 rsin->sin_addr.s_addr) {
1336 if (locked_tcb == NULL) {
1337 SCTP_INP_DECR_REF(inp);
1338 } else if (locked_tcb != stcb) {
1339 SCTP_TCB_LOCK(locked_tcb);
1342 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1344 SCTP_INP_WUNLOCK(inp);
1345 SCTP_INP_INFO_RUNLOCK();
1354 struct sockaddr_in6 *sin6,
1357 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1358 rsin6 = (struct sockaddr_in6 *)remote;
1359 if (SCTP6_ARE_ADDR_EQUAL(sin6,
1365 if (locked_tcb == NULL) {
1366 SCTP_INP_DECR_REF(inp);
1367 } else if (locked_tcb != stcb) {
1368 SCTP_TCB_LOCK(locked_tcb);
1371 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1373 SCTP_INP_WUNLOCK(inp);
1374 SCTP_INP_INFO_RUNLOCK();
1385 SCTP_TCB_UNLOCK(stcb);
1388 SCTP_INP_WLOCK(inp);
1389 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1392 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(rport,
1393 inp->sctp_hashmark)];
1397 LIST_FOREACH(stcb, head, sctp_tcbhash) {
1398 if (stcb->rport != rport) {
1399 /* remote port does not match */
1402 SCTP_TCB_LOCK(stcb);
1403 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
1404 SCTP_TCB_UNLOCK(stcb);
1407 if (local && !sctp_does_stcb_own_this_addr(stcb, local)) {
1408 SCTP_TCB_UNLOCK(stcb);
1411 /* now look at the list of remote addresses */
1412 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1414 if (net == (TAILQ_NEXT(net, sctp_next))) {
1415 panic("Corrupt net list");
1418 if (net->ro._l_addr.sa.sa_family !=
1419 remote->sa_family) {
1420 /* not the same family */
1423 switch (remote->sa_family) {
1427 struct sockaddr_in *sin,
1430 sin = (struct sockaddr_in *)
1432 rsin = (struct sockaddr_in *)remote;
1433 if (sin->sin_addr.s_addr ==
1434 rsin->sin_addr.s_addr) {
1439 if (locked_tcb == NULL) {
1440 SCTP_INP_DECR_REF(inp);
1441 } else if (locked_tcb != stcb) {
1442 SCTP_TCB_LOCK(locked_tcb);
1445 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1447 SCTP_INP_WUNLOCK(inp);
1448 SCTP_INP_INFO_RUNLOCK();
1457 struct sockaddr_in6 *sin6,
1460 sin6 = (struct sockaddr_in6 *)
1462 rsin6 = (struct sockaddr_in6 *)remote;
1463 if (SCTP6_ARE_ADDR_EQUAL(sin6,
1469 if (locked_tcb == NULL) {
1470 SCTP_INP_DECR_REF(inp);
1471 } else if (locked_tcb != stcb) {
1472 SCTP_TCB_LOCK(locked_tcb);
1475 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1477 SCTP_INP_WUNLOCK(inp);
1478 SCTP_INP_INFO_RUNLOCK();
1489 SCTP_TCB_UNLOCK(stcb);
1493 /* clean up for returning null */
1495 SCTP_TCB_LOCK(locked_tcb);
1496 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1498 SCTP_INP_WUNLOCK(inp);
1499 SCTP_INP_INFO_RUNLOCK();
1506 * Find an association for a specific endpoint using the association id given
1507 * out in the COMM_UP notification
1510 sctp_findasoc_ep_asocid_locked(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock)
1513 * Use my the assoc_id to find a endpoint
1515 struct sctpasochead *head;
1516 struct sctp_tcb *stcb;
1520 SCTP_PRINTF("TSNH ep_associd\n");
1523 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1524 SCTP_PRINTF("TSNH ep_associd0\n");
1527 id = (uint32_t) asoc_id;
1528 head = &inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(id, inp->hashasocidmark)];
1530 /* invalid id TSNH */
1531 SCTP_PRINTF("TSNH ep_associd1\n");
1534 LIST_FOREACH(stcb, head, sctp_tcbasocidhash) {
1535 if (stcb->asoc.assoc_id == id) {
1536 if (inp != stcb->sctp_ep) {
1538 * some other guy has the same id active (id
1541 SCTP_PRINTF("TSNH ep_associd2\n");
1544 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
1548 SCTP_TCB_LOCK(stcb);
1558 sctp_findassociation_ep_asocid(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock)
1560 struct sctp_tcb *stcb;
1562 SCTP_INP_RLOCK(inp);
1563 stcb = sctp_findasoc_ep_asocid_locked(inp, asoc_id, want_lock);
1564 SCTP_INP_RUNLOCK(inp);
1570 * Endpoint probe expects that the INP_INFO is locked.
1572 static struct sctp_inpcb *
1573 sctp_endpoint_probe(struct sockaddr *nam, struct sctppcbhead *head,
1574 uint16_t lport, uint32_t vrf_id)
1576 struct sctp_inpcb *inp;
1577 struct sctp_laddr *laddr;
1580 struct sockaddr_in *sin;
1584 struct sockaddr_in6 *sin6;
1585 struct sockaddr_in6 *intf_addr6;
1596 switch (nam->sa_family) {
1599 sin = (struct sockaddr_in *)nam;
1604 sin6 = (struct sockaddr_in6 *)nam;
1608 /* unsupported family */
1615 LIST_FOREACH(inp, head, sctp_hash) {
1616 SCTP_INP_RLOCK(inp);
1617 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1618 SCTP_INP_RUNLOCK(inp);
1621 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) &&
1622 (inp->sctp_lport == lport)) {
1625 if ((nam->sa_family == AF_INET) &&
1626 (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1627 SCTP_IPV6_V6ONLY(inp)) {
1628 /* IPv4 on a IPv6 socket with ONLY IPv6 set */
1629 SCTP_INP_RUNLOCK(inp);
1634 /* A V6 address and the endpoint is NOT bound V6 */
1635 if (nam->sa_family == AF_INET6 &&
1636 (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
1637 SCTP_INP_RUNLOCK(inp);
1641 /* does a VRF id match? */
1643 if (inp->def_vrf_id == vrf_id)
1646 SCTP_INP_RUNLOCK(inp);
1651 SCTP_INP_RUNLOCK(inp);
1653 switch (nam->sa_family) {
1656 if (sin->sin_addr.s_addr == INADDR_ANY) {
1657 /* Can't hunt for one that has no address specified */
1664 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1665 /* Can't hunt for one that has no address specified */
1674 * ok, not bound to all so see if we can find a EP bound to this
1677 LIST_FOREACH(inp, head, sctp_hash) {
1678 SCTP_INP_RLOCK(inp);
1679 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1680 SCTP_INP_RUNLOCK(inp);
1683 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)) {
1684 SCTP_INP_RUNLOCK(inp);
1688 * Ok this could be a likely candidate, look at all of its
1691 if (inp->sctp_lport != lport) {
1692 SCTP_INP_RUNLOCK(inp);
1695 /* does a VRF id match? */
1697 if (inp->def_vrf_id == vrf_id)
1701 SCTP_INP_RUNLOCK(inp);
1704 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1705 if (laddr->ifa == NULL) {
1706 SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n",
1710 SCTPDBG(SCTP_DEBUG_PCB1, "Ok laddr->ifa:%p is possible, ",
1711 (void *)laddr->ifa);
1712 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
1713 SCTPDBG(SCTP_DEBUG_PCB1, "Huh IFA being deleted\n");
1716 if (laddr->ifa->address.sa.sa_family == nam->sa_family) {
1717 /* possible, see if it matches */
1718 switch (nam->sa_family) {
1721 if (sin->sin_addr.s_addr ==
1722 laddr->ifa->address.sin.sin_addr.s_addr) {
1723 SCTP_INP_RUNLOCK(inp);
1730 intf_addr6 = &laddr->ifa->address.sin6;
1731 if (SCTP6_ARE_ADDR_EQUAL(sin6,
1733 SCTP_INP_RUNLOCK(inp);
1741 SCTP_INP_RUNLOCK(inp);
1747 static struct sctp_inpcb *
1748 sctp_isport_inuse(struct sctp_inpcb *inp, uint16_t lport, uint32_t vrf_id)
1750 struct sctppcbhead *head;
1751 struct sctp_inpcb *t_inp;
1754 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport,
1755 SCTP_BASE_INFO(hashmark))];
1756 LIST_FOREACH(t_inp, head, sctp_hash) {
1757 if (t_inp->sctp_lport != lport) {
1760 /* is it in the VRF in question */
1762 if (t_inp->def_vrf_id == vrf_id)
1767 /* This one is in use. */
1768 /* check the v6/v4 binding issue */
1769 if ((t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1770 SCTP_IPV6_V6ONLY(t_inp)) {
1771 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1772 /* collision in V6 space */
1775 /* inp is BOUND_V4 no conflict */
1778 } else if (t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1779 /* t_inp is bound v4 and v6, conflict always */
1782 /* t_inp is bound only V4 */
1783 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1784 SCTP_IPV6_V6ONLY(inp)) {
1788 /* else fall through to conflict */
1797 sctp_swap_inpcb_for_listen(struct sctp_inpcb *inp)
1799 /* For 1-2-1 with port reuse */
1800 struct sctppcbhead *head;
1801 struct sctp_inpcb *tinp;
1803 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) {
1804 /* only works with port reuse on */
1807 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) {
1810 SCTP_INP_RUNLOCK(inp);
1811 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(inp->sctp_lport,
1812 SCTP_BASE_INFO(hashmark))];
1813 /* Kick out all non-listeners to the TCP hash */
1814 LIST_FOREACH(tinp, head, sctp_hash) {
1815 if (tinp->sctp_lport != inp->sctp_lport) {
1818 if (tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1821 if (tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
1824 if (tinp->sctp_socket->so_qlimit) {
1827 SCTP_INP_WLOCK(tinp);
1828 LIST_REMOVE(tinp, sctp_hash);
1829 head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR(tinp->sctp_lport, SCTP_BASE_INFO(hashtcpmark))];
1830 tinp->sctp_flags |= SCTP_PCB_FLAGS_IN_TCPPOOL;
1831 LIST_INSERT_HEAD(head, tinp, sctp_hash);
1832 SCTP_INP_WUNLOCK(tinp);
1834 SCTP_INP_WLOCK(inp);
1835 /* Pull from where he was */
1836 LIST_REMOVE(inp, sctp_hash);
1837 inp->sctp_flags &= ~SCTP_PCB_FLAGS_IN_TCPPOOL;
1838 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(inp->sctp_lport, SCTP_BASE_INFO(hashmark))];
1839 LIST_INSERT_HEAD(head, inp, sctp_hash);
1840 SCTP_INP_WUNLOCK(inp);
1841 SCTP_INP_RLOCK(inp);
1847 sctp_pcb_findep(struct sockaddr *nam, int find_tcp_pool, int have_lock,
1851 * First we check the hash table to see if someone has this port
1852 * bound with just the port.
1854 struct sctp_inpcb *inp;
1855 struct sctppcbhead *head;
1860 struct sockaddr_in *sin;
1864 struct sockaddr_in6 *sin6;
1868 switch (nam->sa_family) {
1871 sin = (struct sockaddr_in *)nam;
1872 lport = sin->sin_port;
1877 sin6 = (struct sockaddr_in6 *)nam;
1878 lport = sin6->sin6_port;
1885 * I could cheat here and just cast to one of the types but we will
1886 * do it right. It also provides the check against an Unsupported
1889 /* Find the head of the ALLADDR chain */
1890 if (have_lock == 0) {
1891 SCTP_INP_INFO_RLOCK();
1893 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport,
1894 SCTP_BASE_INFO(hashmark))];
1895 inp = sctp_endpoint_probe(nam, head, lport, vrf_id);
1898 * If the TCP model exists it could be that the main listening
1899 * endpoint is gone but there still exists a connected socket for
1900 * this guy. If so we can return the first one that we find. This
1901 * may NOT be the correct one so the caller should be wary on the
1902 * returned INP. Currently the only caller that sets find_tcp_pool
1903 * is in bindx where we are verifying that a user CAN bind the
1904 * address. He either has bound it already, or someone else has, or
1905 * its open to bind, so this is good enough.
1907 if (inp == NULL && find_tcp_pool) {
1908 for (i = 0; i < SCTP_BASE_INFO(hashtcpmark) + 1; i++) {
1909 head = &SCTP_BASE_INFO(sctp_tcpephash)[i];
1910 inp = sctp_endpoint_probe(nam, head, lport, vrf_id);
1917 SCTP_INP_INCR_REF(inp);
1919 if (have_lock == 0) {
1920 SCTP_INP_INFO_RUNLOCK();
1927 * Find an association for an endpoint with the pointer to whom you want to
1928 * send to and the endpoint pointer. The address can be IPv4 or IPv6. We may
1929 * need to change the *to to some other struct like a mbuf...
1932 sctp_findassociation_addr_sa(struct sockaddr *from, struct sockaddr *to,
1933 struct sctp_inpcb **inp_p, struct sctp_nets **netp, int find_tcp_pool,
1936 struct sctp_inpcb *inp = NULL;
1937 struct sctp_tcb *stcb;
1939 SCTP_INP_INFO_RLOCK();
1940 if (find_tcp_pool) {
1941 if (inp_p != NULL) {
1942 stcb = sctp_tcb_special_locate(inp_p, from, to, netp,
1945 stcb = sctp_tcb_special_locate(&inp, from, to, netp,
1949 SCTP_INP_INFO_RUNLOCK();
1953 inp = sctp_pcb_findep(to, 0, 1, vrf_id);
1954 if (inp_p != NULL) {
1957 SCTP_INP_INFO_RUNLOCK();
1962 * ok, we have an endpoint, now lets find the assoc for it (if any)
1963 * we now place the source address or from in the to of the find
1964 * endpoint call. Since in reality this chain is used from the
1965 * inbound packet side.
1967 if (inp_p != NULL) {
1968 stcb = sctp_findassociation_ep_addr(inp_p, from, netp, to,
1971 stcb = sctp_findassociation_ep_addr(&inp, from, netp, to,
1979 * This routine will grub through the mbuf that is a INIT or INIT-ACK and
1980 * find all addresses that the sender has specified in any address list. Each
1981 * address will be used to lookup the TCB and see if one exits.
1983 static struct sctp_tcb *
1984 sctp_findassociation_special_addr(struct mbuf *m, int offset,
1985 struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp,
1986 struct sockaddr *dst)
1988 struct sctp_paramhdr *phdr, parm_buf;
1990 #if defined(INET) || defined(INET6)
1991 struct sctp_tcb *stcb;
1998 struct sockaddr_in sin4;
2002 struct sockaddr_in6 sin6;
2007 memset(&sin4, 0, sizeof(sin4));
2008 sin4.sin_len = sizeof(sin4);
2009 sin4.sin_family = AF_INET;
2010 sin4.sin_port = sh->src_port;
2013 memset(&sin6, 0, sizeof(sin6));
2014 sin6.sin6_len = sizeof(sin6);
2015 sin6.sin6_family = AF_INET6;
2016 sin6.sin6_port = sh->src_port;
2019 offset += sizeof(struct sctp_init_chunk);
2021 phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
2022 while (phdr != NULL) {
2023 /* now we must see if we want the parameter */
2024 #if defined(INET) || defined(INET6)
2025 ptype = ntohs(phdr->param_type);
2027 plen = ntohs(phdr->param_length);
2032 if (ptype == SCTP_IPV4_ADDRESS &&
2033 plen == sizeof(struct sctp_ipv4addr_param)) {
2034 /* Get the rest of the address */
2035 struct sctp_ipv4addr_param ip4_parm, *p4;
2037 phdr = sctp_get_next_param(m, offset,
2038 (struct sctp_paramhdr *)&ip4_parm, min(plen, sizeof(ip4_parm)));
2042 p4 = (struct sctp_ipv4addr_param *)phdr;
2043 memcpy(&sin4.sin_addr, &p4->addr, sizeof(p4->addr));
2045 stcb = sctp_findassociation_ep_addr(inp_p,
2046 (struct sockaddr *)&sin4, netp, dst, NULL);
2053 if (ptype == SCTP_IPV6_ADDRESS &&
2054 plen == sizeof(struct sctp_ipv6addr_param)) {
2055 /* Get the rest of the address */
2056 struct sctp_ipv6addr_param ip6_parm, *p6;
2058 phdr = sctp_get_next_param(m, offset,
2059 (struct sctp_paramhdr *)&ip6_parm, min(plen, sizeof(ip6_parm)));
2063 p6 = (struct sctp_ipv6addr_param *)phdr;
2064 memcpy(&sin6.sin6_addr, &p6->addr, sizeof(p6->addr));
2066 stcb = sctp_findassociation_ep_addr(inp_p,
2067 (struct sockaddr *)&sin6, netp, dst, NULL);
2073 offset += SCTP_SIZE32(plen);
2074 phdr = sctp_get_next_param(m, offset, &parm_buf,
2080 static struct sctp_tcb *
2081 sctp_findassoc_by_vtag(struct sockaddr *from, struct sockaddr *to, uint32_t vtag,
2082 struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint16_t rport,
2083 uint16_t lport, int skip_src_check, uint32_t vrf_id, uint32_t remote_tag)
2086 * Use my vtag to hash. If we find it we then verify the source addr
2087 * is in the assoc. If all goes well we save a bit on rec of a
2090 struct sctpasochead *head;
2091 struct sctp_nets *net;
2092 struct sctp_tcb *stcb;
2094 SCTP_INP_INFO_RLOCK();
2095 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(vtag,
2096 SCTP_BASE_INFO(hashasocmark))];
2099 SCTP_INP_INFO_RUNLOCK();
2102 LIST_FOREACH(stcb, head, sctp_asocs) {
2103 SCTP_INP_RLOCK(stcb->sctp_ep);
2104 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
2105 SCTP_INP_RUNLOCK(stcb->sctp_ep);
2108 if (stcb->sctp_ep->def_vrf_id != vrf_id) {
2109 SCTP_INP_RUNLOCK(stcb->sctp_ep);
2112 SCTP_TCB_LOCK(stcb);
2113 SCTP_INP_RUNLOCK(stcb->sctp_ep);
2114 if (stcb->asoc.my_vtag == vtag) {
2116 if (stcb->rport != rport) {
2117 SCTP_TCB_UNLOCK(stcb);
2120 if (stcb->sctp_ep->sctp_lport != lport) {
2121 SCTP_TCB_UNLOCK(stcb);
2124 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
2125 SCTP_TCB_UNLOCK(stcb);
2128 /* RRS:Need toaddr check here */
2129 if (sctp_does_stcb_own_this_addr(stcb, to) == 0) {
2130 /* Endpoint does not own this address */
2131 SCTP_TCB_UNLOCK(stcb);
2136 * If we have both vtags that's all we match
2139 if (stcb->asoc.peer_vtag == remote_tag) {
2141 * If both tags match we consider it
2142 * conclusive and check NO
2143 * source/destination addresses
2148 if (skip_src_check) {
2151 *netp = sctp_findnet(stcb, from);
2153 *netp = NULL; /* unknown */
2156 *inp_p = stcb->sctp_ep;
2157 SCTP_INP_INFO_RUNLOCK();
2160 net = sctp_findnet(stcb, from);
2164 SCTP_STAT_INCR(sctps_vtagexpress);
2165 *inp_p = stcb->sctp_ep;
2166 SCTP_INP_INFO_RUNLOCK();
2170 * not him, this should only happen in rare
2171 * cases so I peg it.
2173 SCTP_STAT_INCR(sctps_vtagbogus);
2176 SCTP_TCB_UNLOCK(stcb);
2178 SCTP_INP_INFO_RUNLOCK();
2184 * Find an association with the pointer to the inbound IP packet. This can be
2185 * a IPv4 or IPv6 packet.
2188 sctp_findassociation_addr(struct mbuf *m, int offset,
2189 struct sockaddr *src, struct sockaddr *dst,
2190 struct sctphdr *sh, struct sctp_chunkhdr *ch,
2191 struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id)
2194 struct sctp_tcb *stcb;
2195 struct sctp_inpcb *inp;
2198 /* we only go down this path if vtag is non-zero */
2199 stcb = sctp_findassoc_by_vtag(src, dst, ntohl(sh->v_tag),
2200 inp_p, netp, sh->src_port, sh->dest_port, 0, vrf_id, 0);
2206 if ((ch->chunk_type != SCTP_INITIATION) &&
2207 (ch->chunk_type != SCTP_INITIATION_ACK) &&
2208 (ch->chunk_type != SCTP_COOKIE_ACK) &&
2209 (ch->chunk_type != SCTP_COOKIE_ECHO)) {
2210 /* Other chunk types go to the tcp pool. */
2214 stcb = sctp_findassociation_addr_sa(src, dst, inp_p, netp,
2215 find_tcp_pool, vrf_id);
2218 stcb = sctp_findassociation_addr_sa(src, dst, &inp, netp,
2219 find_tcp_pool, vrf_id);
2221 SCTPDBG(SCTP_DEBUG_PCB1, "stcb:%p inp:%p\n", (void *)stcb, (void *)inp);
2222 if (stcb == NULL && inp) {
2223 /* Found a EP but not this address */
2224 if ((ch->chunk_type == SCTP_INITIATION) ||
2225 (ch->chunk_type == SCTP_INITIATION_ACK)) {
2227 * special hook, we do NOT return linp or an
2228 * association that is linked to an existing
2229 * association that is under the TCP pool (i.e. no
2230 * listener exists). The endpoint finding routine
2231 * will always find a listener before examining the
2234 if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
2240 stcb = sctp_findassociation_special_addr(m,
2241 offset, sh, &inp, netp, dst);
2242 if (inp_p != NULL) {
2247 SCTPDBG(SCTP_DEBUG_PCB1, "stcb is %p\n", (void *)stcb);
2252 * lookup an association by an ASCONF lookup address.
2253 * if the lookup address is 0.0.0.0 or ::0, use the vtag to do the lookup
2256 sctp_findassociation_ep_asconf(struct mbuf *m, int offset,
2257 struct sockaddr *dst, struct sctphdr *sh,
2258 struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id)
2260 struct sctp_tcb *stcb;
2261 struct sockaddr_storage remote_store;
2262 struct sctp_paramhdr parm_buf, *phdr;
2264 int zero_address = 0;
2267 struct sockaddr_in *sin;
2271 struct sockaddr_in6 *sin6;
2275 memset(&remote_store, 0, sizeof(remote_store));
2276 phdr = sctp_get_next_param(m, offset + sizeof(struct sctp_asconf_chunk),
2277 &parm_buf, sizeof(struct sctp_paramhdr));
2279 SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf lookup addr\n",
2283 ptype = (int)((uint32_t) ntohs(phdr->param_type));
2284 /* get the correlation address */
2287 case SCTP_IPV6_ADDRESS:
2289 /* ipv6 address param */
2290 struct sctp_ipv6addr_param *p6, p6_buf;
2292 if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv6addr_param)) {
2295 p6 = (struct sctp_ipv6addr_param *)sctp_get_next_param(m,
2296 offset + sizeof(struct sctp_asconf_chunk),
2297 &p6_buf.ph, sizeof(*p6));
2299 SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v6 lookup addr\n",
2303 sin6 = (struct sockaddr_in6 *)&remote_store;
2304 sin6->sin6_family = AF_INET6;
2305 sin6->sin6_len = sizeof(*sin6);
2306 sin6->sin6_port = sh->src_port;
2307 memcpy(&sin6->sin6_addr, &p6->addr, sizeof(struct in6_addr));
2308 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
2314 case SCTP_IPV4_ADDRESS:
2316 /* ipv4 address param */
2317 struct sctp_ipv4addr_param *p4, p4_buf;
2319 if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv4addr_param)) {
2322 p4 = (struct sctp_ipv4addr_param *)sctp_get_next_param(m,
2323 offset + sizeof(struct sctp_asconf_chunk),
2324 &p4_buf.ph, sizeof(*p4));
2326 SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v4 lookup addr\n",
2330 sin = (struct sockaddr_in *)&remote_store;
2331 sin->sin_family = AF_INET;
2332 sin->sin_len = sizeof(*sin);
2333 sin->sin_port = sh->src_port;
2334 memcpy(&sin->sin_addr, &p4->addr, sizeof(struct in_addr));
2335 if (sin->sin_addr.s_addr == INADDR_ANY)
2341 /* invalid address param type */
2346 stcb = sctp_findassoc_by_vtag(NULL, dst, ntohl(sh->v_tag), inp_p,
2347 netp, sh->src_port, sh->dest_port, 1, vrf_id, 0);
2349 SCTP_INP_DECR_REF(*inp_p);
2352 stcb = sctp_findassociation_ep_addr(inp_p,
2353 (struct sockaddr *)&remote_store, netp,
2361 * allocate a sctp_inpcb and setup a temporary binding to a port/all
2362 * addresses. This way if we don't get a bind we by default pick a ephemeral
2363 * port with all addresses bound.
2366 sctp_inpcb_alloc(struct socket *so, uint32_t vrf_id)
2369 * we get called when a new endpoint starts up. We need to allocate
2370 * the sctp_inpcb structure from the zone and init it. Mark it as
2371 * unbound and find a port that we can use as an ephemeral with
2372 * INADDR_ANY. If the user binds later no problem we can then add in
2373 * the specific addresses. And setup the default parameters for the
2377 struct sctp_inpcb *inp;
2379 struct timeval time;
2380 sctp_sharedkey_t *null_key;
2384 SCTP_INP_INFO_WLOCK();
2385 inp = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_ep), struct sctp_inpcb);
2387 SCTP_PRINTF("Out of SCTP-INPCB structures - no resources\n");
2388 SCTP_INP_INFO_WUNLOCK();
2389 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
2393 bzero(inp, sizeof(*inp));
2395 /* bump generations */
2396 /* setup socket pointers */
2397 inp->sctp_socket = so;
2398 inp->ip_inp.inp.inp_socket = so;
2400 if (INP_SOCKAF(so) == AF_INET6) {
2401 if (MODULE_GLOBAL(ip6_auto_flowlabel)) {
2402 inp->ip_inp.inp.inp_flags |= IN6P_AUTOFLOWLABEL;
2404 if (MODULE_GLOBAL(ip6_v6only)) {
2405 inp->ip_inp.inp.inp_flags |= IN6P_IPV6_V6ONLY;
2409 inp->sctp_associd_counter = 1;
2410 inp->partial_delivery_point = SCTP_SB_LIMIT_RCV(so) >> SCTP_PARTIAL_DELIVERY_SHIFT;
2411 inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
2412 inp->sctp_cmt_on_off = SCTP_BASE_SYSCTL(sctp_cmt_on_off);
2413 inp->sctp_ecn_enable = SCTP_BASE_SYSCTL(sctp_ecn_enable);
2414 /* init the small hash table we use to track asocid <-> tcb */
2415 inp->sctp_asocidhash = SCTP_HASH_INIT(SCTP_STACK_VTAG_HASH_SIZE, &inp->hashasocidmark);
2416 if (inp->sctp_asocidhash == NULL) {
2417 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2418 SCTP_INP_INFO_WUNLOCK();
2423 struct inpcbpolicy *pcb_sp = NULL;
2425 error = ipsec_init_policy(so, &pcb_sp);
2426 /* Arrange to share the policy */
2427 inp->ip_inp.inp.inp_sp = pcb_sp;
2428 ((struct in6pcb *)(&inp->ip_inp.inp))->in6p_sp = pcb_sp;
2431 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2432 SCTP_INP_INFO_WUNLOCK();
2436 SCTP_INCR_EP_COUNT();
2437 inp->ip_inp.inp.inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
2438 SCTP_INP_INFO_WUNLOCK();
2440 so->so_pcb = (caddr_t)inp;
2442 if (SCTP_SO_TYPE(so) == SOCK_SEQPACKET) {
2443 /* UDP style socket */
2444 inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
2445 SCTP_PCB_FLAGS_UNBOUND);
2446 /* Be sure it is NON-BLOCKING IO for UDP */
2447 /* SCTP_SET_SO_NBIO(so); */
2448 } else if (SCTP_SO_TYPE(so) == SOCK_STREAM) {
2449 /* TCP style socket */
2450 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2451 SCTP_PCB_FLAGS_UNBOUND);
2452 /* Be sure we have blocking IO by default */
2453 SCTP_CLEAR_SO_NBIO(so);
2456 * unsupported socket type (RAW, etc)- in case we missed it
2459 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EOPNOTSUPP);
2461 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2462 return (EOPNOTSUPP);
2464 if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_1) {
2465 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2466 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2467 } else if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_2) {
2468 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2469 sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2470 } else if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_0) {
2471 sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2472 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2474 inp->sctp_tcbhash = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_pcbtblsize),
2475 &inp->sctp_hashmark);
2476 if (inp->sctp_tcbhash == NULL) {
2477 SCTP_PRINTF("Out of SCTP-INPCB->hashinit - no resources\n");
2478 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
2480 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2483 inp->def_vrf_id = vrf_id;
2485 SCTP_INP_INFO_WLOCK();
2486 SCTP_INP_LOCK_INIT(inp);
2487 INP_LOCK_INIT(&inp->ip_inp.inp, "inp", "sctpinp");
2488 SCTP_INP_READ_INIT(inp);
2489 SCTP_ASOC_CREATE_LOCK_INIT(inp);
2490 /* lock the new ep */
2491 SCTP_INP_WLOCK(inp);
2493 /* add it to the info area */
2494 LIST_INSERT_HEAD(&SCTP_BASE_INFO(listhead), inp, sctp_list);
2495 SCTP_INP_INFO_WUNLOCK();
2497 TAILQ_INIT(&inp->read_queue);
2498 LIST_INIT(&inp->sctp_addr_list);
2500 LIST_INIT(&inp->sctp_asoc_list);
2502 #ifdef SCTP_TRACK_FREED_ASOCS
2504 LIST_INIT(&inp->sctp_asoc_free_list);
2506 /* Init the timer structure for signature change */
2507 SCTP_OS_TIMER_INIT(&inp->sctp_ep.signature_change.timer);
2508 inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NEWCOOKIE;
2510 /* now init the actual endpoint default data */
2513 /* setup the base timeout information */
2514 m->sctp_timeoutticks[SCTP_TIMER_SEND] = SEC_TO_TICKS(SCTP_SEND_SEC); /* needed ? */
2515 m->sctp_timeoutticks[SCTP_TIMER_INIT] = SEC_TO_TICKS(SCTP_INIT_SEC); /* needed ? */
2516 m->sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_delayed_sack_time_default));
2517 m->sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_heartbeat_interval_default));
2518 m->sctp_timeoutticks[SCTP_TIMER_PMTU] = SEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_pmtu_raise_time_default));
2519 m->sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] = SEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_shutdown_guard_time_default));
2520 m->sctp_timeoutticks[SCTP_TIMER_SIGNATURE] = SEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_secret_lifetime_default));
2521 /* all max/min max are in ms */
2522 m->sctp_maxrto = SCTP_BASE_SYSCTL(sctp_rto_max_default);
2523 m->sctp_minrto = SCTP_BASE_SYSCTL(sctp_rto_min_default);
2524 m->initial_rto = SCTP_BASE_SYSCTL(sctp_rto_initial_default);
2525 m->initial_init_rto_max = SCTP_BASE_SYSCTL(sctp_init_rto_max_default);
2526 m->sctp_sack_freq = SCTP_BASE_SYSCTL(sctp_sack_freq_default);
2527 m->max_init_times = SCTP_BASE_SYSCTL(sctp_init_rtx_max_default);
2528 m->max_send_times = SCTP_BASE_SYSCTL(sctp_assoc_rtx_max_default);
2529 m->def_net_failure = SCTP_BASE_SYSCTL(sctp_path_rtx_max_default);
2530 m->def_net_pf_threshold = SCTP_BASE_SYSCTL(sctp_path_pf_threshold);
2531 m->sctp_sws_sender = SCTP_SWS_SENDER_DEF;
2532 m->sctp_sws_receiver = SCTP_SWS_RECEIVER_DEF;
2533 m->max_burst = SCTP_BASE_SYSCTL(sctp_max_burst_default);
2534 m->fr_max_burst = SCTP_BASE_SYSCTL(sctp_fr_max_burst_default);
2536 m->sctp_default_cc_module = SCTP_BASE_SYSCTL(sctp_default_cc_module);
2537 m->sctp_default_ss_module = SCTP_BASE_SYSCTL(sctp_default_ss_module);
2538 m->max_open_streams_intome = SCTP_BASE_SYSCTL(sctp_nr_incoming_streams_default);
2539 /* number of streams to pre-open on a association */
2540 m->pre_open_stream_count = SCTP_BASE_SYSCTL(sctp_nr_outgoing_streams_default);
2542 /* Add adaptation cookie */
2543 m->adaptation_layer_indicator = 0;
2544 m->adaptation_layer_indicator_provided = 0;
2546 /* seed random number generator */
2547 m->random_counter = 1;
2548 m->store_at = SCTP_SIGNATURE_SIZE;
2549 SCTP_READ_RANDOM(m->random_numbers, sizeof(m->random_numbers));
2550 sctp_fill_random_store(m);
2552 /* Minimum cookie size */
2553 m->size_of_a_cookie = (sizeof(struct sctp_init_msg) * 2) +
2554 sizeof(struct sctp_state_cookie);
2555 m->size_of_a_cookie += SCTP_SIGNATURE_SIZE;
2557 /* Setup the initial secret */
2558 (void)SCTP_GETTIME_TIMEVAL(&time);
2559 m->time_of_secret_change = time.tv_sec;
2561 for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
2562 m->secret_key[0][i] = sctp_select_initial_TSN(m);
2564 sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL);
2566 /* How long is a cookie good for ? */
2567 m->def_cookie_life = MSEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_valid_cookie_life_default));
2569 * Initialize authentication parameters
2571 m->local_hmacs = sctp_default_supported_hmaclist();
2572 m->local_auth_chunks = sctp_alloc_chunklist();
2573 m->default_dscp = 0;
2575 m->default_flowlabel = 0;
2577 m->port = 0; /* encapsulation disabled by default */
2578 sctp_auth_set_default_chunks(m->local_auth_chunks);
2579 LIST_INIT(&m->shared_keys);
2580 /* add default NULL key as key id 0 */
2581 null_key = sctp_alloc_sharedkey();
2582 sctp_insert_sharedkey(&m->shared_keys, null_key);
2583 SCTP_INP_WUNLOCK(inp);
2584 #ifdef SCTP_LOG_CLOSING
2585 sctp_log_closing(inp, NULL, 12);
2592 sctp_move_pcb_and_assoc(struct sctp_inpcb *old_inp, struct sctp_inpcb *new_inp,
2593 struct sctp_tcb *stcb)
2595 struct sctp_nets *net;
2596 uint16_t lport, rport;
2597 struct sctppcbhead *head;
2598 struct sctp_laddr *laddr, *oladdr;
2600 atomic_add_int(&stcb->asoc.refcnt, 1);
2601 SCTP_TCB_UNLOCK(stcb);
2602 SCTP_INP_INFO_WLOCK();
2603 SCTP_INP_WLOCK(old_inp);
2604 SCTP_INP_WLOCK(new_inp);
2605 SCTP_TCB_LOCK(stcb);
2606 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2608 new_inp->sctp_ep.time_of_secret_change =
2609 old_inp->sctp_ep.time_of_secret_change;
2610 memcpy(new_inp->sctp_ep.secret_key, old_inp->sctp_ep.secret_key,
2611 sizeof(old_inp->sctp_ep.secret_key));
2612 new_inp->sctp_ep.current_secret_number =
2613 old_inp->sctp_ep.current_secret_number;
2614 new_inp->sctp_ep.last_secret_number =
2615 old_inp->sctp_ep.last_secret_number;
2616 new_inp->sctp_ep.size_of_a_cookie = old_inp->sctp_ep.size_of_a_cookie;
2618 /* make it so new data pours into the new socket */
2619 stcb->sctp_socket = new_inp->sctp_socket;
2620 stcb->sctp_ep = new_inp;
2622 /* Copy the port across */
2623 lport = new_inp->sctp_lport = old_inp->sctp_lport;
2624 rport = stcb->rport;
2625 /* Pull the tcb from the old association */
2626 LIST_REMOVE(stcb, sctp_tcbhash);
2627 LIST_REMOVE(stcb, sctp_tcblist);
2628 if (stcb->asoc.in_asocid_hash) {
2629 LIST_REMOVE(stcb, sctp_tcbasocidhash);
2631 /* Now insert the new_inp into the TCP connected hash */
2632 head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR((lport | rport), SCTP_BASE_INFO(hashtcpmark))];
2634 LIST_INSERT_HEAD(head, new_inp, sctp_hash);
2635 /* Its safe to access */
2636 new_inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
2638 /* Now move the tcb into the endpoint list */
2639 LIST_INSERT_HEAD(&new_inp->sctp_asoc_list, stcb, sctp_tcblist);
2641 * Question, do we even need to worry about the ep-hash since we
2642 * only have one connection? Probably not :> so lets get rid of it
2643 * and not suck up any kernel memory in that.
2645 if (stcb->asoc.in_asocid_hash) {
2646 struct sctpasochead *lhd;
2648 lhd = &new_inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(stcb->asoc.assoc_id,
2649 new_inp->hashasocidmark)];
2650 LIST_INSERT_HEAD(lhd, stcb, sctp_tcbasocidhash);
2652 /* Ok. Let's restart timer. */
2653 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2654 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, new_inp,
2658 SCTP_INP_INFO_WUNLOCK();
2659 if (new_inp->sctp_tcbhash != NULL) {
2660 SCTP_HASH_FREE(new_inp->sctp_tcbhash, new_inp->sctp_hashmark);
2661 new_inp->sctp_tcbhash = NULL;
2663 if ((new_inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
2664 /* Subset bound, so copy in the laddr list from the old_inp */
2665 LIST_FOREACH(oladdr, &old_inp->sctp_addr_list, sctp_nxt_addr) {
2666 laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
2667 if (laddr == NULL) {
2669 * Gak, what can we do? This assoc is really
2670 * HOSED. We probably should send an abort
2673 SCTPDBG(SCTP_DEBUG_PCB1, "Association hosed in TCP model, out of laddr memory\n");
2676 SCTP_INCR_LADDR_COUNT();
2677 bzero(laddr, sizeof(*laddr));
2678 (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time);
2679 laddr->ifa = oladdr->ifa;
2680 atomic_add_int(&laddr->ifa->refcount, 1);
2681 LIST_INSERT_HEAD(&new_inp->sctp_addr_list, laddr,
2683 new_inp->laddr_count++;
2684 if (oladdr == stcb->asoc.last_used_address) {
2685 stcb->asoc.last_used_address = laddr;
2690 * Now any running timers need to be adjusted since we really don't
2691 * care if they are running or not just blast in the new_inp into
2695 stcb->asoc.dack_timer.ep = (void *)new_inp;
2696 stcb->asoc.asconf_timer.ep = (void *)new_inp;
2697 stcb->asoc.strreset_timer.ep = (void *)new_inp;
2698 stcb->asoc.shut_guard_timer.ep = (void *)new_inp;
2699 stcb->asoc.autoclose_timer.ep = (void *)new_inp;
2700 stcb->asoc.delayed_event_timer.ep = (void *)new_inp;
2701 stcb->asoc.delete_prim_timer.ep = (void *)new_inp;
2702 /* now what about the nets? */
2703 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2704 net->pmtu_timer.ep = (void *)new_inp;
2705 net->hb_timer.ep = (void *)new_inp;
2706 net->rxt_timer.ep = (void *)new_inp;
2708 SCTP_INP_WUNLOCK(new_inp);
2709 SCTP_INP_WUNLOCK(old_inp);
2715 /* sctp_ifap is used to bypass normal local address validation checks */
2717 sctp_inpcb_bind(struct socket *so, struct sockaddr *addr,
2718 struct sctp_ifa *sctp_ifap, struct thread *p)
2720 /* bind a ep to a socket address */
2721 struct sctppcbhead *head;
2722 struct sctp_inpcb *inp, *inp_tmp;
2723 struct inpcb *ip_inp;
2724 int port_reuse_active = 0;
2733 inp = (struct sctp_inpcb *)so->so_pcb;
2734 ip_inp = (struct inpcb *)so->so_pcb;
2737 SCTPDBG(SCTP_DEBUG_PCB1, "Bind called port: %d\n",
2738 ntohs(((struct sockaddr_in *)addr)->sin_port));
2739 SCTPDBG(SCTP_DEBUG_PCB1, "Addr: ");
2740 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr);
2743 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
2744 /* already did a bind, subsequent binds NOT allowed ! */
2745 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
2750 panic("null proc/thread");
2753 switch (addr->sa_family) {
2757 struct sockaddr_in *sin;
2759 /* IPV6_V6ONLY socket? */
2760 if (SCTP_IPV6_V6ONLY(ip_inp)) {
2761 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
2764 if (addr->sa_len != sizeof(*sin)) {
2765 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
2768 sin = (struct sockaddr_in *)addr;
2769 lport = sin->sin_port;
2771 * For LOOPBACK the prison_local_ip4() call
2772 * will transmute the ip address to the
2775 if (p && (error = prison_local_ip4(p->td_ucred, &sin->sin_addr)) != 0) {
2776 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
2779 if (sin->sin_addr.s_addr != INADDR_ANY) {
2789 * Only for pure IPv6 Address. (No IPv4
2792 struct sockaddr_in6 *sin6;
2794 sin6 = (struct sockaddr_in6 *)addr;
2796 if (addr->sa_len != sizeof(*sin6)) {
2797 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
2800 lport = sin6->sin6_port;
2802 * For LOOPBACK the prison_local_ip6() call
2803 * will transmute the ipv6 address to the
2806 if (p && (error = prison_local_ip6(p->td_ucred, &sin6->sin6_addr,
2807 (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
2808 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
2811 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2813 /* KAME hack: embed scopeid */
2814 if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
2815 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
2819 /* this must be cleared for ifa_ifwithaddr() */
2820 sin6->sin6_scope_id = 0;
2825 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EAFNOSUPPORT);
2826 return (EAFNOSUPPORT);
2829 SCTP_INP_INFO_WLOCK();
2830 SCTP_INP_WLOCK(inp);
2831 /* Setup a vrf_id to be the default for the non-bind-all case. */
2832 vrf_id = inp->def_vrf_id;
2834 /* increase our count due to the unlock we do */
2835 SCTP_INP_INCR_REF(inp);
2838 * Did the caller specify a port? if so we must see if an ep
2839 * already has this one bound.
2841 /* got to be root to get at low ports */
2842 if (ntohs(lport) < IPPORT_RESERVED) {
2844 priv_check(p, PRIV_NETINET_RESERVEDPORT)
2846 SCTP_INP_DECR_REF(inp);
2847 SCTP_INP_WUNLOCK(inp);
2848 SCTP_INP_INFO_WUNLOCK();
2853 SCTP_INP_DECR_REF(inp);
2854 SCTP_INP_WUNLOCK(inp);
2855 SCTP_INP_INFO_WUNLOCK();
2856 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
2859 SCTP_INP_WUNLOCK(inp);
2861 vrf_id = inp->def_vrf_id;
2862 inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id);
2863 if (inp_tmp != NULL) {
2865 * lock guy returned and lower count note
2866 * that we are not bound so inp_tmp should
2867 * NEVER be inp. And it is this inp
2868 * (inp_tmp) that gets the reference bump,
2869 * so we must lower it.
2871 SCTP_INP_DECR_REF(inp_tmp);
2873 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
2874 (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
2876 * Ok, must be one-2-one and
2877 * allowing port re-use
2879 port_reuse_active = 1;
2880 goto continue_anyway;
2882 SCTP_INP_DECR_REF(inp);
2883 SCTP_INP_INFO_WUNLOCK();
2884 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE);
2885 return (EADDRINUSE);
2888 inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id);
2889 if (inp_tmp != NULL) {
2891 * lock guy returned and lower count note
2892 * that we are not bound so inp_tmp should
2893 * NEVER be inp. And it is this inp
2894 * (inp_tmp) that gets the reference bump,
2895 * so we must lower it.
2897 SCTP_INP_DECR_REF(inp_tmp);
2899 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
2900 (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
2902 * Ok, must be one-2-one and
2903 * allowing port re-use
2905 port_reuse_active = 1;
2906 goto continue_anyway;
2908 SCTP_INP_DECR_REF(inp);
2909 SCTP_INP_INFO_WUNLOCK();
2910 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE);
2911 return (EADDRINUSE);
2915 SCTP_INP_WLOCK(inp);
2917 /* verify that no lport is not used by a singleton */
2918 if ((port_reuse_active == 0) &&
2919 (inp_tmp = sctp_isport_inuse(inp, lport, vrf_id))) {
2920 /* Sorry someone already has this one bound */
2921 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
2922 (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
2923 port_reuse_active = 1;
2925 SCTP_INP_DECR_REF(inp);
2926 SCTP_INP_WUNLOCK(inp);
2927 SCTP_INP_INFO_WUNLOCK();
2928 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE);
2929 return (EADDRINUSE);
2934 uint16_t first, last, candidate;
2938 if (ip_inp->inp_flags & INP_HIGHPORT) {
2939 first = MODULE_GLOBAL(ipport_hifirstauto);
2940 last = MODULE_GLOBAL(ipport_hilastauto);
2941 } else if (ip_inp->inp_flags & INP_LOWPORT) {
2943 priv_check(p, PRIV_NETINET_RESERVEDPORT)
2945 SCTP_INP_DECR_REF(inp);
2946 SCTP_INP_WUNLOCK(inp);
2947 SCTP_INP_INFO_WUNLOCK();
2948 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
2951 first = MODULE_GLOBAL(ipport_lowfirstauto);
2952 last = MODULE_GLOBAL(ipport_lowlastauto);
2954 first = MODULE_GLOBAL(ipport_firstauto);
2955 last = MODULE_GLOBAL(ipport_lastauto);
2964 count = last - first + 1; /* number of candidates */
2965 candidate = first + sctp_select_initial_TSN(&inp->sctp_ep) % (count);
2969 if (sctp_isport_inuse(inp, htons(candidate), inp->def_vrf_id) == NULL) {
2974 SCTP_INP_DECR_REF(inp);
2975 SCTP_INP_WUNLOCK(inp);
2976 SCTP_INP_INFO_WUNLOCK();
2977 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE);
2978 return (EADDRINUSE);
2980 if (candidate == last)
2983 candidate = candidate + 1;
2986 lport = htons(candidate);
2988 SCTP_INP_DECR_REF(inp);
2989 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE |
2990 SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
2992 * this really should not happen. The guy did a non-blocking
2993 * bind and then did a close at the same time.
2995 SCTP_INP_WUNLOCK(inp);
2996 SCTP_INP_INFO_WUNLOCK();
2997 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3000 /* ok we look clear to give out this port, so lets setup the binding */
3002 /* binding to all addresses, so just set in the proper flags */
3003 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUNDALL;
3004 /* set the automatic addr changes from kernel flag */
3005 if (SCTP_BASE_SYSCTL(sctp_auto_asconf) == 0) {
3006 sctp_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF);
3007 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
3009 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
3010 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
3012 if (SCTP_BASE_SYSCTL(sctp_multiple_asconfs) == 0) {
3013 sctp_feature_off(inp, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS);
3015 sctp_feature_on(inp, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS);
3018 * set the automatic mobility_base from kernel flag (by
3021 if (SCTP_BASE_SYSCTL(sctp_mobility_base) == 0) {
3022 sctp_mobility_feature_off(inp, SCTP_MOBILITY_BASE);
3023 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
3025 sctp_mobility_feature_on(inp, SCTP_MOBILITY_BASE);
3026 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
3029 * set the automatic mobility_fasthandoff from kernel flag
3032 if (SCTP_BASE_SYSCTL(sctp_mobility_fasthandoff) == 0) {
3033 sctp_mobility_feature_off(inp, SCTP_MOBILITY_FASTHANDOFF);
3034 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
3036 sctp_mobility_feature_on(inp, SCTP_MOBILITY_FASTHANDOFF);
3037 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
3041 * bind specific, make sure flags is off and add a new
3042 * address structure to the sctp_addr_list inside the ep
3045 * We will need to allocate one and insert it at the head. The
3046 * socketopt call can just insert new addresses in there as
3047 * well. It will also have to do the embed scope kame hack
3048 * too (before adding).
3050 struct sctp_ifa *ifa;
3051 struct sockaddr_storage store_sa;
3053 memset(&store_sa, 0, sizeof(store_sa));
3054 switch (addr->sa_family) {
3058 struct sockaddr_in *sin;
3060 sin = (struct sockaddr_in *)&store_sa;
3061 memcpy(sin, addr, sizeof(struct sockaddr_in));
3069 struct sockaddr_in6 *sin6;
3071 sin6 = (struct sockaddr_in6 *)&store_sa;
3072 memcpy(sin6, addr, sizeof(struct sockaddr_in6));
3073 sin6->sin6_port = 0;
3081 * first find the interface with the bound address need to
3082 * zero out the port to find the address! yuck! can't do
3083 * this earlier since need port for sctp_pcb_findep()
3085 if (sctp_ifap != NULL) {
3089 * Note for BSD we hit here always other O/S's will
3090 * pass things in via the sctp_ifap argument
3093 ifa = sctp_find_ifa_by_addr((struct sockaddr *)&store_sa,
3094 vrf_id, SCTP_ADDR_NOT_LOCKED);
3097 /* Can't find an interface with that address */
3098 SCTP_INP_WUNLOCK(inp);
3099 SCTP_INP_INFO_WUNLOCK();
3100 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRNOTAVAIL);
3101 return (EADDRNOTAVAIL);
3104 if (addr->sa_family == AF_INET6) {
3105 /* GAK, more FIXME IFA lock? */
3106 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
3107 /* Can't bind a non-existent addr. */
3108 SCTP_INP_WUNLOCK(inp);
3109 SCTP_INP_INFO_WUNLOCK();
3110 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3115 /* we're not bound all */
3116 inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUNDALL;
3117 /* allow bindx() to send ASCONF's for binding changes */
3118 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
3119 /* clear automatic addr changes from kernel flag */
3120 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
3122 /* add this address to the endpoint list */
3123 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, 0);
3125 SCTP_INP_WUNLOCK(inp);
3126 SCTP_INP_INFO_WUNLOCK();
3131 /* find the bucket */
3132 if (port_reuse_active) {
3133 /* Put it into tcp 1-2-1 hash */
3134 head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashtcpmark))];
3135 inp->sctp_flags |= SCTP_PCB_FLAGS_IN_TCPPOOL;
3137 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashmark))];
3139 /* put it in the bucket */
3140 LIST_INSERT_HEAD(head, inp, sctp_hash);
3141 SCTPDBG(SCTP_DEBUG_PCB1, "Main hash to bind at head:%p, bound port:%d - in tcp_pool=%d\n",
3142 (void *)head, ntohs(lport), port_reuse_active);
3143 /* set in the port */
3144 inp->sctp_lport = lport;
3146 /* turn off just the unbound flag */
3147 inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
3148 SCTP_INP_WUNLOCK(inp);
3149 SCTP_INP_INFO_WUNLOCK();
3155 sctp_iterator_inp_being_freed(struct sctp_inpcb *inp)
3157 struct sctp_iterator *it, *nit;
3160 * We enter with the only the ITERATOR_LOCK in place and a write
3161 * lock on the inp_info stuff.
3163 it = sctp_it_ctl.cur_it;
3164 if (it && (it->vn != curvnet)) {
3165 /* Its not looking at our VNET */
3168 if (it && (it->inp == inp)) {
3170 * This is tricky and we hold the iterator lock, but when it
3171 * returns and gets the lock (when we release it) the
3172 * iterator will try to operate on inp. We need to stop that
3173 * from happening. But of course the iterator has a
3174 * reference on the stcb and inp. We can mark it and it will
3177 * If its a single iterator situation, we set the end iterator
3178 * flag. Otherwise we set the iterator to go to the next
3182 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
3183 sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_IT;
3185 sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_INP;
3189 * Now go through and remove any single reference to our inp that
3190 * may be still pending on the list
3192 SCTP_IPI_ITERATOR_WQ_LOCK();
3193 TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) {
3194 if (it->vn != curvnet) {
3197 if (it->inp == inp) {
3198 /* This one points to me is it inp specific? */
3199 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
3200 /* Remove and free this one */
3201 TAILQ_REMOVE(&sctp_it_ctl.iteratorhead,
3203 if (it->function_atend != NULL) {
3204 (*it->function_atend) (it->pointer, it->val);
3206 SCTP_FREE(it, SCTP_M_ITER);
3208 it->inp = LIST_NEXT(it->inp, sctp_list);
3210 SCTP_INP_INCR_REF(it->inp);
3214 * When its put in the refcnt is incremented so decr
3217 SCTP_INP_DECR_REF(inp);
3220 SCTP_IPI_ITERATOR_WQ_UNLOCK();
3223 /* release sctp_inpcb unbind the port */
3225 sctp_inpcb_free(struct sctp_inpcb *inp, int immediate, int from)
3228 * Here we free a endpoint. We must find it (if it is in the Hash
3229 * table) and remove it from there. Then we must also find it in the
3230 * overall list and remove it from there. After all removals are
3231 * complete then any timer has to be stopped. Then start the actual
3232 * freeing. a) Any local lists. b) Any associations. c) The hash of
3233 * all associations. d) finally the ep itself.
3235 struct sctp_tcb *asoc, *nasoc;
3236 struct sctp_laddr *laddr, *nladdr;
3237 struct inpcb *ip_pcb;
3239 int being_refed = 0;
3240 struct sctp_queued_to_read *sq, *nsq;
3242 sctp_sharedkey_t *shared_key, *nshared_key;
3245 #ifdef SCTP_LOG_CLOSING
3246 sctp_log_closing(inp, NULL, 0);
3248 SCTP_ITERATOR_LOCK();
3249 /* mark any iterators on the list or being processed */
3250 sctp_iterator_inp_being_freed(inp);
3251 SCTP_ITERATOR_UNLOCK();
3252 so = inp->sctp_socket;
3253 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
3254 /* been here before.. eeks.. get out of here */
3255 SCTP_PRINTF("This conflict in free SHOULD not be happening! from %d, imm %d\n", from, immediate);
3256 #ifdef SCTP_LOG_CLOSING
3257 sctp_log_closing(inp, NULL, 1);
3261 SCTP_ASOC_CREATE_LOCK(inp);
3262 SCTP_INP_INFO_WLOCK();
3264 SCTP_INP_WLOCK(inp);
3265 if (from == SCTP_CALLED_AFTER_CMPSET_OFCLOSE) {
3266 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CLOSE_IP;
3267 /* socket is gone, so no more wakeups allowed */
3268 inp->sctp_flags |= SCTP_PCB_FLAGS_DONT_WAKE;
3269 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
3270 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
3273 /* First time through we have the socket lock, after that no more. */
3274 sctp_timer_stop(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL,
3275 SCTP_FROM_SCTP_PCB + SCTP_LOC_1);
3278 sctp_m_freem(inp->control);
3279 inp->control = NULL;
3282 sctp_m_freem(inp->pkt);
3285 ip_pcb = &inp->ip_inp.inp; /* we could just cast the main pointer
3286 * here but I will be nice :> (i.e.
3288 if (immediate == SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE) {
3292 LIST_FOREACH_SAFE(asoc, &inp->sctp_asoc_list, sctp_tcblist, nasoc) {
3293 SCTP_TCB_LOCK(asoc);
3294 if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3295 /* Skip guys being freed */
3297 if (asoc->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE) {
3299 * Special case - we did not start a
3300 * kill timer on the asoc due to it
3301 * was not closed. So go ahead and
3304 asoc->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE;
3305 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, asoc, NULL);
3307 SCTP_TCB_UNLOCK(asoc);
3310 if (((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_WAIT) ||
3311 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_ECHOED)) &&
3312 (asoc->asoc.total_output_queue_size == 0)) {
3314 * If we have data in queue, we don't want
3315 * to just free since the app may have done,
3316 * send()/close or connect/send/close. And
3317 * it wants the data to get across first.
3319 /* Just abandon things in the front states */
3320 if (sctp_free_assoc(inp, asoc, SCTP_PCBFREE_NOFORCE,
3321 SCTP_FROM_SCTP_PCB + SCTP_LOC_2) == 0) {
3326 /* Disconnect the socket please */
3327 asoc->sctp_socket = NULL;
3328 asoc->asoc.state |= SCTP_STATE_CLOSED_SOCKET;
3329 if ((asoc->asoc.size_on_reasm_queue > 0) ||
3330 (asoc->asoc.control_pdapi) ||
3331 (asoc->asoc.size_on_all_streams > 0) ||
3332 (so && (so->so_rcv.sb_cc > 0))) {
3333 /* Left with Data unread */
3334 struct mbuf *op_err;
3336 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
3337 0, M_NOWAIT, 1, MT_DATA);
3339 /* Fill in the user initiated abort */
3340 struct sctp_paramhdr *ph;
3342 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr);
3343 ph = mtod(op_err, struct sctp_paramhdr *);
3344 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
3345 ph->param_length = htons(SCTP_BUF_LEN(op_err));
3347 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_3;
3348 sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED);
3349 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3350 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
3351 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3352 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3354 if (sctp_free_assoc(inp, asoc,
3355 SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_4) == 0) {
3359 } else if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
3360 TAILQ_EMPTY(&asoc->asoc.sent_queue) &&
3361 (asoc->asoc.stream_queue_cnt == 0)) {
3362 if (asoc->asoc.locked_on_sending) {
3365 if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
3366 (SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
3367 struct sctp_nets *netp;
3370 * there is nothing queued to send,
3371 * so I send shutdown
3373 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
3374 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3375 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3377 SCTP_SET_STATE(&asoc->asoc, SCTP_STATE_SHUTDOWN_SENT);
3378 SCTP_CLEAR_SUBSTATE(&asoc->asoc, SCTP_STATE_SHUTDOWN_PENDING);
3379 sctp_stop_timers_for_shutdown(asoc);
3380 if (asoc->asoc.alternate) {
3381 netp = asoc->asoc.alternate;
3383 netp = asoc->asoc.primary_destination;
3385 sctp_send_shutdown(asoc, netp);
3386 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, asoc->sctp_ep, asoc,
3388 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc,
3389 asoc->asoc.primary_destination);
3390 sctp_chunk_output(inp, asoc, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_LOCKED);
3393 /* mark into shutdown pending */
3394 struct sctp_stream_queue_pending *sp;
3396 asoc->asoc.state |= SCTP_STATE_SHUTDOWN_PENDING;
3397 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc,
3398 asoc->asoc.primary_destination);
3399 if (asoc->asoc.locked_on_sending) {
3400 sp = TAILQ_LAST(&((asoc->asoc.locked_on_sending)->outqueue),
3403 SCTP_PRINTF("Error, sp is NULL, locked on sending is %p strm:%d\n",
3404 (void *)asoc->asoc.locked_on_sending,
3405 asoc->asoc.locked_on_sending->stream_no);
3407 if ((sp->length == 0) && (sp->msg_is_complete == 0))
3408 asoc->asoc.state |= SCTP_STATE_PARTIAL_MSG_LEFT;
3411 if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
3412 TAILQ_EMPTY(&asoc->asoc.sent_queue) &&
3413 (asoc->asoc.state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
3414 struct mbuf *op_err;
3417 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
3418 0, M_NOWAIT, 1, MT_DATA);
3424 struct sctp_paramhdr *ph;
3426 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr);
3427 ph = mtod(op_err, struct sctp_paramhdr *);
3428 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
3429 ph->param_length = htons(SCTP_BUF_LEN(op_err));
3431 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_5;
3432 sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED);
3433 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3434 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
3435 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3436 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3438 if (sctp_free_assoc(inp, asoc,
3439 SCTP_PCBFREE_NOFORCE,
3440 SCTP_FROM_SCTP_PCB + SCTP_LOC_6) == 0) {
3445 sctp_chunk_output(inp, asoc, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
3449 SCTP_TCB_UNLOCK(asoc);
3451 /* now is there some left in our SHUTDOWN state? */
3453 #ifdef SCTP_LOG_CLOSING
3454 sctp_log_closing(inp, NULL, 2);
3456 inp->sctp_socket = NULL;
3457 SCTP_INP_WUNLOCK(inp);
3458 SCTP_ASOC_CREATE_UNLOCK(inp);
3459 SCTP_INP_INFO_WUNLOCK();
3463 inp->sctp_socket = NULL;
3464 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) !=
3465 SCTP_PCB_FLAGS_UNBOUND) {
3467 * ok, this guy has been bound. It's port is somewhere in
3468 * the SCTP_BASE_INFO(hash table). Remove it!
3470 LIST_REMOVE(inp, sctp_hash);
3471 inp->sctp_flags |= SCTP_PCB_FLAGS_UNBOUND;
3474 * If there is a timer running to kill us, forget it, since it may
3475 * have a contest on the INP lock.. which would cause us to die ...
3478 LIST_FOREACH_SAFE(asoc, &inp->sctp_asoc_list, sctp_tcblist, nasoc) {
3479 SCTP_TCB_LOCK(asoc);
3480 if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3481 if (asoc->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE) {
3482 asoc->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE;
3483 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, asoc, NULL);
3486 SCTP_TCB_UNLOCK(asoc);
3489 /* Free associations that are NOT killing us */
3490 if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_COOKIE_WAIT) &&
3491 ((asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0)) {
3492 struct mbuf *op_err;
3494 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
3495 0, M_NOWAIT, 1, MT_DATA);
3497 /* Fill in the user initiated abort */
3498 struct sctp_paramhdr *ph;
3500 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_paramhdr);
3501 ph = mtod(op_err, struct sctp_paramhdr *);
3502 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
3503 ph->param_length = htons(SCTP_BUF_LEN(op_err));
3505 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_7;
3506 sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED);
3507 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3508 } else if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3510 SCTP_TCB_UNLOCK(asoc);
3513 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
3514 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3515 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3517 if (sctp_free_assoc(inp, asoc, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_8) == 0) {
3522 /* Ok we have someone out there that will kill us */
3523 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
3524 #ifdef SCTP_LOG_CLOSING
3525 sctp_log_closing(inp, NULL, 3);
3527 SCTP_INP_WUNLOCK(inp);
3528 SCTP_ASOC_CREATE_UNLOCK(inp);
3529 SCTP_INP_INFO_WUNLOCK();
3532 if (SCTP_INP_LOCK_CONTENDED(inp))
3534 if (SCTP_INP_READ_CONTENDED(inp))
3536 if (SCTP_ASOC_CREATE_LOCK_CONTENDED(inp))
3539 if ((inp->refcount) ||
3541 (inp->sctp_flags & SCTP_PCB_FLAGS_CLOSE_IP)) {
3542 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
3543 #ifdef SCTP_LOG_CLOSING
3544 sctp_log_closing(inp, NULL, 4);
3546 sctp_timer_start(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL);
3547 SCTP_INP_WUNLOCK(inp);
3548 SCTP_ASOC_CREATE_UNLOCK(inp);
3549 SCTP_INP_INFO_WUNLOCK();
3552 inp->sctp_ep.signature_change.type = 0;
3553 inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_ALLGONE;
3555 * Remove it from the list .. last thing we need a lock for.
3557 LIST_REMOVE(inp, sctp_list);
3558 SCTP_INP_WUNLOCK(inp);
3559 SCTP_ASOC_CREATE_UNLOCK(inp);
3560 SCTP_INP_INFO_WUNLOCK();
3562 * Now we release all locks. Since this INP cannot be found anymore
3563 * except possibly by the kill timer that might be running. We call
3564 * the drain function here. It should hit the case were it sees the
3565 * ACTIVE flag cleared and exit out freeing us to proceed and
3566 * destroy everything.
3568 if (from != SCTP_CALLED_FROM_INPKILL_TIMER) {
3569 (void)SCTP_OS_TIMER_STOP_DRAIN(&inp->sctp_ep.signature_change.timer);
3571 /* Probably un-needed */
3572 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
3575 #ifdef SCTP_LOG_CLOSING
3576 sctp_log_closing(inp, NULL, 5);
3580 if ((inp->sctp_asocidhash) != NULL) {
3581 SCTP_HASH_FREE(inp->sctp_asocidhash, inp->hashasocidmark);
3582 inp->sctp_asocidhash = NULL;
3584 /* sa_ignore FREED_MEMORY */
3585 TAILQ_FOREACH_SAFE(sq, &inp->read_queue, next, nsq) {
3586 /* Its only abandoned if it had data left */
3588 SCTP_STAT_INCR(sctps_left_abandon);
3590 TAILQ_REMOVE(&inp->read_queue, sq, next);
3591 sctp_free_remote_addr(sq->whoFrom);
3593 so->so_rcv.sb_cc -= sq->length;
3595 sctp_m_freem(sq->data);
3599 * no need to free the net count, since at this point all
3602 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), sq);
3603 SCTP_DECR_READQ_COUNT();
3605 /* Now the sctp_pcb things */
3607 * free each asoc if it is not already closed/free. we can't use the
3608 * macro here since le_next will get freed as part of the
3609 * sctp_free_assoc() call.
3613 ipsec_delete_pcbpolicy(ip_pcb);
3616 /* Unlocks not needed since the socket is gone now */
3618 if (ip_pcb->inp_options) {
3619 (void)sctp_m_free(ip_pcb->inp_options);
3620 ip_pcb->inp_options = 0;
3623 if (ip_pcb->inp_vflag & INP_IPV6) {
3624 struct in6pcb *in6p;
3626 in6p = (struct in6pcb *)inp;
3627 ip6_freepcbopts(in6p->in6p_outputopts);
3630 ip_pcb->inp_vflag = 0;
3631 /* free up authentication fields */
3632 if (inp->sctp_ep.local_auth_chunks != NULL)
3633 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
3634 if (inp->sctp_ep.local_hmacs != NULL)
3635 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
3637 LIST_FOREACH_SAFE(shared_key, &inp->sctp_ep.shared_keys, next, nshared_key) {
3638 LIST_REMOVE(shared_key, next);
3639 sctp_free_sharedkey(shared_key);
3640 /* sa_ignore FREED_MEMORY */
3644 * if we have an address list the following will free the list of
3645 * ifaddr's that are set into this ep. Again macro limitations here,
3646 * since the LIST_FOREACH could be a bad idea.
3648 LIST_FOREACH_SAFE(laddr, &inp->sctp_addr_list, sctp_nxt_addr, nladdr) {
3649 sctp_remove_laddr(laddr);
3652 #ifdef SCTP_TRACK_FREED_ASOCS
3654 LIST_FOREACH_SAFE(asoc, &inp->sctp_asoc_free_list, sctp_tcblist, nasoc) {
3655 LIST_REMOVE(asoc, sctp_tcblist);
3656 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), asoc);
3657 SCTP_DECR_ASOC_COUNT();
3659 /* *** END TEMP CODE *** */
3661 /* Now lets see about freeing the EP hash table. */
3662 if (inp->sctp_tcbhash != NULL) {
3663 SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark);
3664 inp->sctp_tcbhash = NULL;
3666 /* Now we must put the ep memory back into the zone pool */
3667 INP_LOCK_DESTROY(&inp->ip_inp.inp);
3668 SCTP_INP_LOCK_DESTROY(inp);
3669 SCTP_INP_READ_DESTROY(inp);
3670 SCTP_ASOC_CREATE_LOCK_DESTROY(inp);
3671 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
3672 SCTP_DECR_EP_COUNT();
3677 sctp_findnet(struct sctp_tcb *stcb, struct sockaddr *addr)
3679 struct sctp_nets *net;
3681 /* locate the address */
3682 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3683 if (sctp_cmpaddr(addr, (struct sockaddr *)&net->ro._l_addr))
3691 sctp_is_address_on_local_host(struct sockaddr *addr, uint32_t vrf_id)
3693 struct sctp_ifa *sctp_ifa;
3695 sctp_ifa = sctp_find_ifa_by_addr(addr, vrf_id, SCTP_ADDR_NOT_LOCKED);
3704 * add's a remote endpoint address, done with the INIT/INIT-ACK as well as
3705 * when a ASCONF arrives that adds it. It will also initialize all the cwnd
3709 sctp_add_remote_addr(struct sctp_tcb *stcb, struct sockaddr *newaddr,
3710 struct sctp_nets **netp, int set_scope, int from)
3713 * The following is redundant to the same lines in the
3714 * sctp_aloc_assoc() but is needed since others call the add address
3717 struct sctp_nets *net, *netfirst;
3720 SCTPDBG(SCTP_DEBUG_PCB1, "Adding an address (from:%d) to the peer: ",
3722 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, newaddr);
3724 netfirst = sctp_findnet(stcb, newaddr);
3727 * Lie and return ok, we don't want to make the association
3728 * go away for this behavior. It will happen in the TCP
3729 * model in a connected socket. It does not reach the hash
3730 * table until after the association is built so it can't be
3731 * found. Mark as reachable, since the initial creation will
3732 * have been cleared and the NOT_IN_ASSOC flag will have
3733 * been added... and we don't want to end up removing it
3736 if (netfirst->dest_state & SCTP_ADDR_UNCONFIRMED) {
3737 netfirst->dest_state = (SCTP_ADDR_REACHABLE |
3738 SCTP_ADDR_UNCONFIRMED);
3740 netfirst->dest_state = SCTP_ADDR_REACHABLE;
3746 switch (newaddr->sa_family) {
3750 struct sockaddr_in *sin;
3752 sin = (struct sockaddr_in *)newaddr;
3753 if (sin->sin_addr.s_addr == 0) {
3754 /* Invalid address */
3757 /* zero out the bzero area */
3758 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
3760 /* assure len is set */
3761 sin->sin_len = sizeof(struct sockaddr_in);
3763 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
3764 stcb->asoc.scope.ipv4_local_scope = 1;
3766 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
3767 stcb->asoc.scope.ipv4_local_scope = 1;
3769 #endif /* SCTP_DONT_DO_PRIVADDR_SCOPE */
3771 /* Validate the address is in scope */
3772 if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) &&
3773 (stcb->asoc.scope.ipv4_local_scope == 0)) {
3783 struct sockaddr_in6 *sin6;
3785 sin6 = (struct sockaddr_in6 *)newaddr;
3786 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3787 /* Invalid address */
3790 /* assure len is set */
3791 sin6->sin6_len = sizeof(struct sockaddr_in6);
3793 if (sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id)) {
3794 stcb->asoc.scope.loopback_scope = 1;
3795 stcb->asoc.scope.local_scope = 0;
3796 stcb->asoc.scope.ipv4_local_scope = 1;
3797 stcb->asoc.scope.site_scope = 1;
3798 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
3800 * If the new destination is a
3801 * LINK_LOCAL we must have common
3802 * site scope. Don't set the local
3803 * scope since we may not share all
3804 * links, only loopback can do this.
3805 * Links on the local network would
3806 * also be on our private network
3809 stcb->asoc.scope.ipv4_local_scope = 1;
3810 stcb->asoc.scope.site_scope = 1;
3811 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
3813 * If the new destination is
3814 * SITE_LOCAL then we must have site
3817 stcb->asoc.scope.site_scope = 1;
3820 /* Validate the address is in scope */
3821 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) &&
3822 (stcb->asoc.scope.loopback_scope == 0)) {
3824 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) &&
3825 (stcb->asoc.scope.local_scope == 0)) {
3827 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) &&
3828 (stcb->asoc.scope.site_scope == 0)) {
3836 /* not supported family type */
3839 net = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_net), struct sctp_nets);
3843 SCTP_INCR_RADDR_COUNT();
3844 bzero(net, sizeof(struct sctp_nets));
3845 (void)SCTP_GETTIME_TIMEVAL(&net->start_time);
3846 memcpy(&net->ro._l_addr, newaddr, newaddr->sa_len);
3847 switch (newaddr->sa_family) {
3850 ((struct sockaddr_in *)&net->ro._l_addr)->sin_port = stcb->rport;
3855 ((struct sockaddr_in6 *)&net->ro._l_addr)->sin6_port = stcb->rport;
3861 net->addr_is_local = sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id);
3862 if (net->addr_is_local && ((set_scope || (from == SCTP_ADDR_IS_CONFIRMED)))) {
3863 stcb->asoc.scope.loopback_scope = 1;
3864 stcb->asoc.scope.ipv4_local_scope = 1;
3865 stcb->asoc.scope.local_scope = 0;
3866 stcb->asoc.scope.site_scope = 1;
3869 net->failure_threshold = stcb->asoc.def_net_failure;
3870 net->pf_threshold = stcb->asoc.def_net_pf_threshold;
3871 if (addr_inscope == 0) {
3872 net->dest_state = (SCTP_ADDR_REACHABLE |
3873 SCTP_ADDR_OUT_OF_SCOPE);
3875 if (from == SCTP_ADDR_IS_CONFIRMED)
3876 /* SCTP_ADDR_IS_CONFIRMED is passed by connect_x */
3877 net->dest_state = SCTP_ADDR_REACHABLE;
3879 net->dest_state = SCTP_ADDR_REACHABLE |
3880 SCTP_ADDR_UNCONFIRMED;
3883 * We set this to 0, the timer code knows that this means its an
3886 net->rto_needed = 1;
3888 net->RTO_measured = 0;
3889 stcb->asoc.numnets++;
3891 net->cwr_window_tsn = net->last_cwr_tsn = stcb->asoc.sending_seq - 1;
3892 net->port = stcb->asoc.port;
3893 net->dscp = stcb->asoc.default_dscp;
3895 net->flowlabel = stcb->asoc.default_flowlabel;
3897 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
3898 net->dest_state |= SCTP_ADDR_NOHB;
3900 net->dest_state &= ~SCTP_ADDR_NOHB;
3902 if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD)) {
3903 net->dest_state |= SCTP_ADDR_NO_PMTUD;
3905 net->dest_state &= ~SCTP_ADDR_NO_PMTUD;
3907 net->heart_beat_delay = stcb->asoc.heart_beat_delay;
3908 /* Init the timer structure */
3909 SCTP_OS_TIMER_INIT(&net->rxt_timer.timer);
3910 SCTP_OS_TIMER_INIT(&net->pmtu_timer.timer);
3911 SCTP_OS_TIMER_INIT(&net->hb_timer.timer);
3913 /* Now generate a route for this guy */
3915 /* KAME hack: embed scopeid */
3916 if (newaddr->sa_family == AF_INET6) {
3917 struct sockaddr_in6 *sin6;
3919 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3920 (void)sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
3921 sin6->sin6_scope_id = 0;
3924 SCTP_RTALLOC((sctp_route_t *) & net->ro, stcb->asoc.vrf_id);
3926 if (SCTP_ROUTE_HAS_VALID_IFN(&net->ro)) {
3927 /* Get source address */
3928 net->ro._s_addr = sctp_source_address_selection(stcb->sctp_ep,
3930 (sctp_route_t *) & net->ro,
3934 /* Now get the interface MTU */
3935 if (net->ro._s_addr && net->ro._s_addr->ifn_p) {
3936 net->mtu = SCTP_GATHER_MTU_FROM_INTFC(net->ro._s_addr->ifn_p);
3941 rmtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, net->ro.ro_rt);
3944 * Start things off to match mtu of
3947 SCTP_SET_MTU_OF_ROUTE(&net->ro._l_addr.sa,
3948 net->ro.ro_rt, net->mtu);
3951 * we take the route mtu over the interface,
3952 * since the route may be leading out the
3953 * loopback, or a different interface.
3959 if (net->mtu == 0) {
3960 switch (newaddr->sa_family) {
3963 net->mtu = SCTP_DEFAULT_MTU;
3976 net->mtu -= (uint32_t) sizeof(struct udphdr);
3978 if (from == SCTP_ALLOC_ASOC) {
3979 stcb->asoc.smallest_mtu = net->mtu;
3981 if (stcb->asoc.smallest_mtu > net->mtu) {
3982 stcb->asoc.smallest_mtu = net->mtu;
3985 if (newaddr->sa_family == AF_INET6) {
3986 struct sockaddr_in6 *sin6;
3988 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3989 (void)sa6_recoverscope(sin6);
3993 /* JRS - Use the congestion control given in the CC module */
3994 if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL)
3995 (*stcb->asoc.cc_functions.sctp_set_initial_cc_param) (stcb, net);
3998 * CMT: CUC algo - set find_pseudo_cumack to TRUE (1) at beginning
3999 * of assoc (2005/06/27, iyengar@cis.udel.edu)
4001 net->find_pseudo_cumack = 1;
4002 net->find_rtx_pseudo_cumack = 1;
4003 net->src_addr_selected = 0;
4004 /* Choose an initial flowid. */
4005 net->flowid = stcb->asoc.my_vtag ^
4006 ntohs(stcb->rport) ^
4007 ntohs(stcb->sctp_ep->sctp_lport);
4014 netfirst = TAILQ_FIRST(&stcb->asoc.nets);
4015 if (net->ro.ro_rt == NULL) {
4016 /* Since we have no route put it at the back */
4017 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
4018 } else if (netfirst == NULL) {
4019 /* We are the first one in the pool. */
4020 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
4021 } else if (netfirst->ro.ro_rt == NULL) {
4023 * First one has NO route. Place this one ahead of the first
4026 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
4027 } else if (net->ro.ro_rt->rt_ifp != netfirst->ro.ro_rt->rt_ifp) {
4029 * This one has a different interface than the one at the
4030 * top of the list. Place it ahead.
4032 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
4035 * Ok we have the same interface as the first one. Move
4036 * forward until we find either a) one with a NULL route...
4037 * insert ahead of that b) one with a different ifp.. insert
4038 * after that. c) end of the list.. insert at the tail.
4040 struct sctp_nets *netlook;
4043 netlook = TAILQ_NEXT(netfirst, sctp_next);
4044 if (netlook == NULL) {
4045 /* End of the list */
4046 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
4048 } else if (netlook->ro.ro_rt == NULL) {
4049 /* next one has NO route */
4050 TAILQ_INSERT_BEFORE(netfirst, net, sctp_next);
4052 } else if (netlook->ro.ro_rt->rt_ifp != net->ro.ro_rt->rt_ifp) {
4053 TAILQ_INSERT_AFTER(&stcb->asoc.nets, netlook,
4059 } while (netlook != NULL);
4062 /* got to have a primary set */
4063 if (stcb->asoc.primary_destination == 0) {
4064 stcb->asoc.primary_destination = net;
4065 } else if ((stcb->asoc.primary_destination->ro.ro_rt == NULL) &&
4067 ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) {
4068 /* No route to current primary adopt new primary */
4069 stcb->asoc.primary_destination = net;
4071 /* Validate primary is first */
4072 net = TAILQ_FIRST(&stcb->asoc.nets);
4073 if ((net != stcb->asoc.primary_destination) &&
4074 (stcb->asoc.primary_destination)) {
4076 * first one on the list is NOT the primary sctp_cmpaddr()
4077 * is much more efficient if the primary is the first on the
4080 TAILQ_REMOVE(&stcb->asoc.nets,
4081 stcb->asoc.primary_destination, sctp_next);
4082 TAILQ_INSERT_HEAD(&stcb->asoc.nets,
4083 stcb->asoc.primary_destination, sctp_next);
4090 sctp_aloc_a_assoc_id(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
4093 struct sctpasochead *head;
4094 struct sctp_tcb *lstcb;
4096 SCTP_INP_WLOCK(inp);
4098 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
4100 SCTP_INP_WUNLOCK(inp);
4104 * We don't allow assoc id to be one of SCTP_FUTURE_ASSOC,
4105 * SCTP_CURRENT_ASSOC and SCTP_ALL_ASSOC.
4107 if (inp->sctp_associd_counter <= SCTP_ALL_ASSOC) {
4108 inp->sctp_associd_counter = SCTP_ALL_ASSOC + 1;
4110 id = inp->sctp_associd_counter;
4111 inp->sctp_associd_counter++;
4112 lstcb = sctp_findasoc_ep_asocid_locked(inp, (sctp_assoc_t) id, 0);
4116 head = &inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(id, inp->hashasocidmark)];
4117 LIST_INSERT_HEAD(head, stcb, sctp_tcbasocidhash);
4118 stcb->asoc.in_asocid_hash = 1;
4119 SCTP_INP_WUNLOCK(inp);
4124 * allocate an association and add it to the endpoint. The caller must be
4125 * careful to add all additional addresses once they are know right away or
4126 * else the assoc will be may experience a blackout scenario.
4129 sctp_aloc_assoc(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
4130 int *error, uint32_t override_tag, uint32_t vrf_id,
4134 /* note the p argument is only valid in unbound sockets */
4136 struct sctp_tcb *stcb;
4137 struct sctp_association *asoc;
4138 struct sctpasochead *head;
4143 * Assumption made here: Caller has done a
4144 * sctp_findassociation_ep_addr(ep, addr's); to make sure the
4145 * address does not exist already.
4147 if (SCTP_BASE_INFO(ipi_count_asoc) >= SCTP_MAX_NUM_OF_ASOC) {
4148 /* Hit max assoc, sorry no more */
4149 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
4153 if (firstaddr == NULL) {
4154 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4158 SCTP_INP_RLOCK(inp);
4159 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
4160 ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) ||
4161 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
4163 * If its in the TCP pool, its NOT allowed to create an
4164 * association. The parent listener needs to call
4165 * sctp_aloc_assoc.. or the one-2-many socket. If a peeled
4166 * off, or connected one does this.. its an error.
4168 SCTP_INP_RUNLOCK(inp);
4169 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4173 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4174 (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) {
4175 if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) ||
4176 (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED)) {
4177 SCTP_INP_RUNLOCK(inp);
4178 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4183 SCTPDBG(SCTP_DEBUG_PCB3, "Allocate an association for peer:");
4186 SCTPDBG_ADDR(SCTP_DEBUG_PCB3, firstaddr);
4187 switch (firstaddr->sa_family) {
4190 SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
4191 ntohs(((struct sockaddr_in *)firstaddr)->sin_port));
4196 SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
4197 ntohs(((struct sockaddr_in6 *)firstaddr)->sin6_port));
4204 SCTPDBG(SCTP_DEBUG_PCB3, "None\n");
4206 #endif /* SCTP_DEBUG */
4207 switch (firstaddr->sa_family) {
4211 struct sockaddr_in *sin;
4213 sin = (struct sockaddr_in *)firstaddr;
4214 if ((ntohs(sin->sin_port) == 0) ||
4215 (sin->sin_addr.s_addr == INADDR_ANY) ||
4216 (sin->sin_addr.s_addr == INADDR_BROADCAST) ||
4217 IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
4218 /* Invalid address */
4219 SCTP_INP_RUNLOCK(inp);
4220 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4224 rport = sin->sin_port;
4231 struct sockaddr_in6 *sin6;
4233 sin6 = (struct sockaddr_in6 *)firstaddr;
4234 if ((ntohs(sin6->sin6_port) == 0) ||
4235 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
4236 IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
4237 /* Invalid address */
4238 SCTP_INP_RUNLOCK(inp);
4239 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4243 rport = sin6->sin6_port;
4248 /* not supported family type */
4249 SCTP_INP_RUNLOCK(inp);
4250 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4254 SCTP_INP_RUNLOCK(inp);
4255 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
4257 * If you have not performed a bind, then we need to do the
4258 * ephemeral bind for you.
4260 if ((err = sctp_inpcb_bind(inp->sctp_socket,
4261 (struct sockaddr *)NULL,
4262 (struct sctp_ifa *)NULL,
4265 /* bind error, probably perm */
4270 stcb = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_asoc), struct sctp_tcb);
4272 /* out of memory? */
4273 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
4277 SCTP_INCR_ASOC_COUNT();
4279 bzero(stcb, sizeof(*stcb));
4282 asoc->assoc_id = sctp_aloc_a_assoc_id(inp, stcb);
4283 SCTP_TCB_LOCK_INIT(stcb);
4284 SCTP_TCB_SEND_LOCK_INIT(stcb);
4285 stcb->rport = rport;
4286 /* setup back pointer's */
4287 stcb->sctp_ep = inp;
4288 stcb->sctp_socket = inp->sctp_socket;
4289 if ((err = sctp_init_asoc(inp, stcb, override_tag, vrf_id))) {
4291 SCTP_TCB_LOCK_DESTROY(stcb);
4292 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
4293 LIST_REMOVE(stcb, sctp_tcbasocidhash);
4294 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
4295 SCTP_DECR_ASOC_COUNT();
4300 SCTP_INP_INFO_WLOCK();
4301 SCTP_INP_WLOCK(inp);
4302 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4303 /* inpcb freed while alloc going on */
4304 SCTP_TCB_LOCK_DESTROY(stcb);
4305 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
4306 LIST_REMOVE(stcb, sctp_tcbasocidhash);
4307 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
4308 SCTP_INP_WUNLOCK(inp);
4309 SCTP_INP_INFO_WUNLOCK();
4310 SCTP_DECR_ASOC_COUNT();
4311 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4315 SCTP_TCB_LOCK(stcb);
4317 /* now that my_vtag is set, add it to the hash */
4318 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
4319 /* put it in the bucket in the vtag hash of assoc's for the system */
4320 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
4321 SCTP_INP_INFO_WUNLOCK();
4323 if ((err = sctp_add_remote_addr(stcb, firstaddr, NULL, SCTP_DO_SETSCOPE, SCTP_ALLOC_ASOC))) {
4324 /* failure.. memory error? */
4325 if (asoc->strmout) {
4326 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
4327 asoc->strmout = NULL;
4329 if (asoc->mapping_array) {
4330 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
4331 asoc->mapping_array = NULL;
4333 if (asoc->nr_mapping_array) {
4334 SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
4335 asoc->nr_mapping_array = NULL;
4337 SCTP_DECR_ASOC_COUNT();
4338 SCTP_TCB_UNLOCK(stcb);
4339 SCTP_TCB_LOCK_DESTROY(stcb);
4340 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
4341 LIST_REMOVE(stcb, sctp_tcbasocidhash);
4342 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
4343 SCTP_INP_WUNLOCK(inp);
4344 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
4348 /* Init all the timers */
4349 SCTP_OS_TIMER_INIT(&asoc->dack_timer.timer);
4350 SCTP_OS_TIMER_INIT(&asoc->strreset_timer.timer);
4351 SCTP_OS_TIMER_INIT(&asoc->asconf_timer.timer);
4352 SCTP_OS_TIMER_INIT(&asoc->shut_guard_timer.timer);
4353 SCTP_OS_TIMER_INIT(&asoc->autoclose_timer.timer);
4354 SCTP_OS_TIMER_INIT(&asoc->delayed_event_timer.timer);
4355 SCTP_OS_TIMER_INIT(&asoc->delete_prim_timer.timer);
4357 LIST_INSERT_HEAD(&inp->sctp_asoc_list, stcb, sctp_tcblist);
4358 /* now file the port under the hash as well */
4359 if (inp->sctp_tcbhash != NULL) {
4360 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(stcb->rport,
4361 inp->sctp_hashmark)];
4362 LIST_INSERT_HEAD(head, stcb, sctp_tcbhash);
4364 SCTP_INP_WUNLOCK(inp);
4365 SCTPDBG(SCTP_DEBUG_PCB1, "Association %p now allocated\n", (void *)stcb);
4371 sctp_remove_net(struct sctp_tcb *stcb, struct sctp_nets *net)
4373 struct sctp_association *asoc;
4377 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
4378 if (net == asoc->primary_destination) {
4380 struct sctp_nets *lnet;
4382 lnet = TAILQ_FIRST(&asoc->nets);
4384 * Mobility adaptation Ideally, if deleted destination is
4385 * the primary, it becomes a fast retransmission trigger by
4386 * the subsequent SET PRIMARY. (by micchie)
4388 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
4389 SCTP_MOBILITY_BASE) ||
4390 sctp_is_mobility_feature_on(stcb->sctp_ep,
4391 SCTP_MOBILITY_FASTHANDOFF)) {
4392 SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: primary dst is deleting\n");
4393 if (asoc->deleted_primary != NULL) {
4394 SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: deleted primary may be already stored\n");
4397 asoc->deleted_primary = net;
4398 atomic_add_int(&net->ref_count, 1);
4399 memset(&net->lastsa, 0, sizeof(net->lastsa));
4400 memset(&net->lastsv, 0, sizeof(net->lastsv));
4401 sctp_mobility_feature_on(stcb->sctp_ep,
4402 SCTP_MOBILITY_PRIM_DELETED);
4403 sctp_timer_start(SCTP_TIMER_TYPE_PRIM_DELETED,
4404 stcb->sctp_ep, stcb, NULL);
4407 /* Try to find a confirmed primary */
4408 asoc->primary_destination = sctp_find_alternate_net(stcb, lnet, 0);
4410 if (net == asoc->last_data_chunk_from) {
4412 asoc->last_data_chunk_from = TAILQ_FIRST(&asoc->nets);
4414 if (net == asoc->last_control_chunk_from) {
4416 asoc->last_control_chunk_from = NULL;
4418 if (net == stcb->asoc.alternate) {
4419 sctp_free_remote_addr(stcb->asoc.alternate);
4420 stcb->asoc.alternate = NULL;
4422 sctp_free_remote_addr(net);
4426 * remove a remote endpoint address from an association, it will fail if the
4427 * address does not exist.
4430 sctp_del_remote_addr(struct sctp_tcb *stcb, struct sockaddr *remaddr)
4433 * Here we need to remove a remote address. This is quite simple, we
4434 * first find it in the list of address for the association
4435 * (tasoc->asoc.nets) and then if it is there, we do a LIST_REMOVE
4436 * on that item. Note we do not allow it to be removed if there are
4437 * no other addresses.
4439 struct sctp_association *asoc;
4440 struct sctp_nets *net, *nnet;
4444 /* locate the address */
4445 TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) {
4446 if (net->ro._l_addr.sa.sa_family != remaddr->sa_family) {
4449 if (sctp_cmpaddr((struct sockaddr *)&net->ro._l_addr,
4451 /* we found the guy */
4452 if (asoc->numnets < 2) {
4453 /* Must have at LEAST two remote addresses */
4456 sctp_remove_net(stcb, net);
4466 sctp_delete_from_timewait(uint32_t tag, uint16_t lport, uint16_t rport)
4468 struct sctpvtaghead *chain;
4469 struct sctp_tagblock *twait_block;
4473 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
4474 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
4475 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
4476 if ((twait_block->vtag_block[i].v_tag == tag) &&
4477 (twait_block->vtag_block[i].lport == lport) &&
4478 (twait_block->vtag_block[i].rport == rport)) {
4479 twait_block->vtag_block[i].tv_sec_at_expire = 0;
4480 twait_block->vtag_block[i].v_tag = 0;
4481 twait_block->vtag_block[i].lport = 0;
4482 twait_block->vtag_block[i].rport = 0;
4493 sctp_is_in_timewait(uint32_t tag, uint16_t lport, uint16_t rport)
4495 struct sctpvtaghead *chain;
4496 struct sctp_tagblock *twait_block;
4500 SCTP_INP_INFO_WLOCK();
4501 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
4502 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
4503 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
4504 if ((twait_block->vtag_block[i].v_tag == tag) &&
4505 (twait_block->vtag_block[i].lport == lport) &&
4506 (twait_block->vtag_block[i].rport == rport)) {
4514 SCTP_INP_INFO_WUNLOCK();
4520 sctp_add_vtag_to_timewait(uint32_t tag, uint32_t time, uint16_t lport, uint16_t rport)
4522 struct sctpvtaghead *chain;
4523 struct sctp_tagblock *twait_block;
4531 (void)SCTP_GETTIME_TIMEVAL(&now);
4532 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
4534 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
4535 /* Block(s) present, lets find space, and expire on the fly */
4536 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
4537 if ((twait_block->vtag_block[i].v_tag == 0) &&
4539 twait_block->vtag_block[i].tv_sec_at_expire =
4541 twait_block->vtag_block[i].v_tag = tag;
4542 twait_block->vtag_block[i].lport = lport;
4543 twait_block->vtag_block[i].rport = rport;
4545 } else if ((twait_block->vtag_block[i].v_tag) &&
4546 ((long)twait_block->vtag_block[i].tv_sec_at_expire < now.tv_sec)) {
4547 /* Audit expires this guy */
4548 twait_block->vtag_block[i].tv_sec_at_expire = 0;
4549 twait_block->vtag_block[i].v_tag = 0;
4550 twait_block->vtag_block[i].lport = 0;
4551 twait_block->vtag_block[i].rport = 0;
4553 /* Reuse it for my new tag */
4554 twait_block->vtag_block[i].tv_sec_at_expire = now.tv_sec + time;
4555 twait_block->vtag_block[i].v_tag = tag;
4556 twait_block->vtag_block[i].lport = lport;
4557 twait_block->vtag_block[i].rport = rport;
4564 * We only do up to the block where we can place our
4570 /* Need to add a new block to chain */
4572 SCTP_MALLOC(twait_block, struct sctp_tagblock *,
4573 sizeof(struct sctp_tagblock), SCTP_M_TIMW);
4574 if (twait_block == NULL) {
4576 panic("Can not alloc tagblock");
4580 memset(twait_block, 0, sizeof(struct sctp_tagblock));
4581 LIST_INSERT_HEAD(chain, twait_block, sctp_nxt_tagblock);
4582 twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec + time;
4583 twait_block->vtag_block[0].v_tag = tag;
4584 twait_block->vtag_block[0].lport = lport;
4585 twait_block->vtag_block[0].rport = rport;
4592 * Free the association after un-hashing the remote port. This
4593 * function ALWAYS returns holding NO LOCK on the stcb. It DOES
4594 * expect that the input to this function IS a locked TCB.
4595 * It will return 0, if it did NOT destroy the association (instead
4596 * it unlocks it. It will return NON-zero if it either destroyed the
4597 * association OR the association is already destroyed.
4600 sctp_free_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int from_inpcbfree, int from_location)
4603 struct sctp_association *asoc;
4604 struct sctp_nets *net, *nnet;
4605 struct sctp_laddr *laddr, *naddr;
4606 struct sctp_tmit_chunk *chk, *nchk;
4607 struct sctp_asconf_addr *aparam, *naparam;
4608 struct sctp_asconf_ack *aack, *naack;
4609 struct sctp_stream_reset_list *strrst, *nstrrst;
4610 struct sctp_queued_to_read *sq, *nsq;
4611 struct sctp_stream_queue_pending *sp, *nsp;
4612 sctp_sharedkey_t *shared_key, *nshared_key;
4615 /* first, lets purge the entry from the hash table. */
4617 #ifdef SCTP_LOG_CLOSING
4618 sctp_log_closing(inp, stcb, 6);
4620 if (stcb->asoc.state == 0) {
4621 #ifdef SCTP_LOG_CLOSING
4622 sctp_log_closing(inp, NULL, 7);
4624 /* there is no asoc, really TSNH :-0 */
4627 if (stcb->asoc.alternate) {
4628 sctp_free_remote_addr(stcb->asoc.alternate);
4629 stcb->asoc.alternate = NULL;
4632 if (stcb->freed_from_where == 0) {
4633 /* Only record the first place free happened from */
4634 stcb->freed_from_where = from_location;
4639 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4640 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
4641 /* nothing around */
4644 so = inp->sctp_socket;
4647 * We used timer based freeing if a reader or writer is in the way.
4648 * So we first check if we are actually being called from a timer,
4649 * if so we abort early if a reader or writer is still in the way.
4651 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) &&
4652 (from_inpcbfree == SCTP_NORMAL_PROC)) {
4654 * is it the timer driving us? if so are the reader/writers
4657 if (stcb->asoc.refcnt) {
4658 /* nope, reader or writer in the way */
4659 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
4660 /* no asoc destroyed */
4661 SCTP_TCB_UNLOCK(stcb);
4662 #ifdef SCTP_LOG_CLOSING
4663 sctp_log_closing(inp, stcb, 8);
4668 /* now clean up any other timers */
4669 (void)SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
4670 asoc->dack_timer.self = NULL;
4671 (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
4673 * For stream reset we don't blast this unless
4674 * it is a str-reset timer, it might be the
4675 * free-asoc timer which we DON'T want to
4678 if (asoc->strreset_timer.type == SCTP_TIMER_TYPE_STRRESET)
4679 asoc->strreset_timer.self = NULL;
4680 (void)SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
4681 asoc->asconf_timer.self = NULL;
4682 (void)SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
4683 asoc->autoclose_timer.self = NULL;
4684 (void)SCTP_OS_TIMER_STOP(&asoc->shut_guard_timer.timer);
4685 asoc->shut_guard_timer.self = NULL;
4686 (void)SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
4687 asoc->delayed_event_timer.self = NULL;
4688 /* Mobility adaptation */
4689 (void)SCTP_OS_TIMER_STOP(&asoc->delete_prim_timer.timer);
4690 asoc->delete_prim_timer.self = NULL;
4691 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4692 (void)SCTP_OS_TIMER_STOP(&net->rxt_timer.timer);
4693 net->rxt_timer.self = NULL;
4694 (void)SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
4695 net->pmtu_timer.self = NULL;
4696 (void)SCTP_OS_TIMER_STOP(&net->hb_timer.timer);
4697 net->hb_timer.self = NULL;
4699 /* Now the read queue needs to be cleaned up (only once) */
4700 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0) {
4701 stcb->asoc.state |= SCTP_STATE_ABOUT_TO_BE_FREED;
4702 SCTP_INP_READ_LOCK(inp);
4703 TAILQ_FOREACH(sq, &inp->read_queue, next) {
4704 if (sq->stcb == stcb) {
4705 sq->do_not_ref_stcb = 1;
4706 sq->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
4708 * If there is no end, there never will be
4711 if (sq->end_added == 0) {
4712 /* Held for PD-API clear that. */
4713 sq->pdapi_aborted = 1;
4714 sq->held_length = 0;
4715 if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT) && (so != NULL)) {
4717 * Need to add a PD-API
4718 * aborted indication.
4719 * Setting the control_pdapi
4720 * assures that it will be
4721 * added right after this
4726 stcb->asoc.control_pdapi = sq;
4727 strseq = (sq->sinfo_stream << 16) | sq->sinfo_ssn;
4728 sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
4730 SCTP_PARTIAL_DELIVERY_ABORTED,
4733 stcb->asoc.control_pdapi = NULL;
4736 /* Add an end to wake them */
4740 SCTP_INP_READ_UNLOCK(inp);
4741 if (stcb->block_entry) {
4742 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PCB, ECONNRESET);
4743 stcb->block_entry->error = ECONNRESET;
4744 stcb->block_entry = NULL;
4747 if ((stcb->asoc.refcnt) || (stcb->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE)) {
4749 * Someone holds a reference OR the socket is unaccepted
4752 if ((stcb->asoc.refcnt) ||
4753 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4754 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
4755 stcb->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE;
4756 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
4758 SCTP_TCB_UNLOCK(stcb);
4759 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4760 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
4761 /* nothing around */
4764 /* Wake any reader/writers */
4765 sctp_sorwakeup(inp, so);
4766 sctp_sowwakeup(inp, so);
4768 #ifdef SCTP_LOG_CLOSING
4769 sctp_log_closing(inp, stcb, 9);
4771 /* no asoc destroyed */
4774 #ifdef SCTP_LOG_CLOSING
4775 sctp_log_closing(inp, stcb, 10);
4778 * When I reach here, no others want to kill the assoc yet.. and I
4779 * own the lock. Now its possible an abort comes in when I do the
4780 * lock exchange below to grab all the locks to do the final take
4781 * out. to prevent this we increment the count, which will start a
4782 * timer and blow out above thus assuring us that we hold exclusive
4783 * killing of the asoc. Note that after getting back the TCB lock we
4784 * will go ahead and increment the counter back up and stop any
4785 * timer a passing stranger may have started :-S
4787 if (from_inpcbfree == SCTP_NORMAL_PROC) {
4788 atomic_add_int(&stcb->asoc.refcnt, 1);
4790 SCTP_TCB_UNLOCK(stcb);
4791 SCTP_INP_INFO_WLOCK();
4792 SCTP_INP_WLOCK(inp);
4793 SCTP_TCB_LOCK(stcb);
4795 /* Double check the GONE flag */
4796 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4797 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
4798 /* nothing around */
4801 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4802 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
4804 * For TCP type we need special handling when we are
4805 * connected. We also include the peel'ed off ones to.
4807 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4808 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
4809 inp->sctp_flags |= SCTP_PCB_FLAGS_WAS_CONNECTED;
4812 if (so->so_rcv.sb_cc == 0) {
4813 so->so_state &= ~(SS_ISCONNECTING |
4814 SS_ISDISCONNECTING |
4818 socantrcvmore_locked(so);
4819 sctp_sowwakeup(inp, so);
4820 sctp_sorwakeup(inp, so);
4826 * Make it invalid too, that way if its about to run it will abort
4829 /* re-increment the lock */
4830 if (from_inpcbfree == SCTP_NORMAL_PROC) {
4831 atomic_add_int(&stcb->asoc.refcnt, -1);
4833 if (stcb->asoc.refcnt) {
4834 stcb->asoc.state &= ~SCTP_STATE_IN_ACCEPT_QUEUE;
4835 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
4836 if (from_inpcbfree == SCTP_NORMAL_PROC) {
4837 SCTP_INP_INFO_WUNLOCK();
4838 SCTP_INP_WUNLOCK(inp);
4840 SCTP_TCB_UNLOCK(stcb);
4844 if (inp->sctp_tcbhash) {
4845 LIST_REMOVE(stcb, sctp_tcbhash);
4847 if (stcb->asoc.in_asocid_hash) {
4848 LIST_REMOVE(stcb, sctp_tcbasocidhash);
4850 /* Now lets remove it from the list of ALL associations in the EP */
4851 LIST_REMOVE(stcb, sctp_tcblist);
4852 if (from_inpcbfree == SCTP_NORMAL_PROC) {
4853 SCTP_INP_INCR_REF(inp);
4854 SCTP_INP_WUNLOCK(inp);
4856 /* pull from vtag hash */
4857 LIST_REMOVE(stcb, sctp_asocs);
4858 sctp_add_vtag_to_timewait(asoc->my_vtag, SCTP_BASE_SYSCTL(sctp_vtag_time_wait),
4859 inp->sctp_lport, stcb->rport);
4862 * Now restop the timers to be sure this is paranoia at is finest!
4864 (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
4865 (void)SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
4866 (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
4867 (void)SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
4868 (void)SCTP_OS_TIMER_STOP(&asoc->shut_guard_timer.timer);
4869 (void)SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
4870 (void)SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
4871 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4872 (void)SCTP_OS_TIMER_STOP(&net->rxt_timer.timer);
4873 (void)SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
4874 (void)SCTP_OS_TIMER_STOP(&net->hb_timer.timer);
4877 asoc->strreset_timer.type = SCTP_TIMER_TYPE_NONE;
4879 * The chunk lists and such SHOULD be empty but we check them just
4882 /* anything on the wheel needs to be removed */
4883 for (i = 0; i < asoc->streamoutcnt; i++) {
4884 struct sctp_stream_out *outs;
4886 outs = &asoc->strmout[i];
4887 /* now clean up any chunks here */
4888 TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
4889 TAILQ_REMOVE(&outs->outqueue, sp, next);
4890 sctp_free_spbufspace(stcb, asoc, sp);
4893 /* Still an open socket - report */
4894 sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb,
4895 0, (void *)sp, SCTP_SO_LOCKED);
4898 sctp_m_freem(sp->data);
4900 sp->tail_mbuf = NULL;
4905 sctp_free_remote_addr(sp->net);
4908 sctp_free_a_strmoq(stcb, sp, SCTP_SO_LOCKED);
4911 /* sa_ignore FREED_MEMORY */
4912 TAILQ_FOREACH_SAFE(strrst, &asoc->resetHead, next_resp, nstrrst) {
4913 TAILQ_REMOVE(&asoc->resetHead, strrst, next_resp);
4914 SCTP_FREE(strrst, SCTP_M_STRESET);
4916 TAILQ_FOREACH_SAFE(sq, &asoc->pending_reply_queue, next, nsq) {
4917 TAILQ_REMOVE(&asoc->pending_reply_queue, sq, next);
4919 sctp_m_freem(sq->data);
4922 sctp_free_remote_addr(sq->whoFrom);
4925 /* Free the ctl entry */
4926 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), sq);
4927 SCTP_DECR_READQ_COUNT();
4928 /* sa_ignore FREED_MEMORY */
4930 TAILQ_FOREACH_SAFE(chk, &asoc->free_chunks, sctp_next, nchk) {
4931 TAILQ_REMOVE(&asoc->free_chunks, chk, sctp_next);
4933 sctp_m_freem(chk->data);
4936 if (chk->holds_key_ref)
4937 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
4938 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
4939 SCTP_DECR_CHK_COUNT();
4940 atomic_subtract_int(&SCTP_BASE_INFO(ipi_free_chunks), 1);
4941 asoc->free_chunk_cnt--;
4942 /* sa_ignore FREED_MEMORY */
4944 /* pending send queue SHOULD be empty */
4945 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
4946 if (asoc->strmout[chk->rec.data.stream_number].chunks_on_queues > 0) {
4947 asoc->strmout[chk->rec.data.stream_number].chunks_on_queues--;
4950 panic("No chunks on the queues for sid %u.", chk->rec.data.stream_number);
4953 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
4956 /* Still a socket? */
4957 sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
4958 0, chk, SCTP_SO_LOCKED);
4961 sctp_m_freem(chk->data);
4965 if (chk->holds_key_ref)
4966 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
4968 sctp_free_remote_addr(chk->whoTo);
4971 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
4972 SCTP_DECR_CHK_COUNT();
4973 /* sa_ignore FREED_MEMORY */
4975 /* sent queue SHOULD be empty */
4976 TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) {
4977 if (chk->sent != SCTP_DATAGRAM_NR_ACKED) {
4978 if (asoc->strmout[chk->rec.data.stream_number].chunks_on_queues > 0) {
4979 asoc->strmout[chk->rec.data.stream_number].chunks_on_queues--;
4982 panic("No chunks on the queues for sid %u.", chk->rec.data.stream_number);
4986 TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
4989 /* Still a socket? */
4990 sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb,
4991 0, chk, SCTP_SO_LOCKED);
4994 sctp_m_freem(chk->data);
4998 if (chk->holds_key_ref)
4999 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5000 sctp_free_remote_addr(chk->whoTo);
5001 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5002 SCTP_DECR_CHK_COUNT();
5003 /* sa_ignore FREED_MEMORY */
5006 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
5007 if (stcb->asoc.strmout[i].chunks_on_queues > 0) {
5008 panic("%u chunks left for stream %u.", stcb->asoc.strmout[i].chunks_on_queues, i);
5012 /* control queue MAY not be empty */
5013 TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
5014 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
5016 sctp_m_freem(chk->data);
5019 if (chk->holds_key_ref)
5020 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5021 sctp_free_remote_addr(chk->whoTo);
5022 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5023 SCTP_DECR_CHK_COUNT();
5024 /* sa_ignore FREED_MEMORY */
5026 /* ASCONF queue MAY not be empty */
5027 TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
5028 TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
5030 sctp_m_freem(chk->data);
5033 if (chk->holds_key_ref)
5034 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5035 sctp_free_remote_addr(chk->whoTo);
5036 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5037 SCTP_DECR_CHK_COUNT();
5038 /* sa_ignore FREED_MEMORY */
5040 TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
5041 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
5043 sctp_m_freem(chk->data);
5046 if (chk->holds_key_ref)
5047 sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5048 sctp_free_remote_addr(chk->whoTo);
5049 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5050 SCTP_DECR_CHK_COUNT();
5051 /* sa_ignore FREED_MEMORY */
5054 if (asoc->mapping_array) {
5055 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
5056 asoc->mapping_array = NULL;
5058 if (asoc->nr_mapping_array) {
5059 SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
5060 asoc->nr_mapping_array = NULL;
5062 /* the stream outs */
5063 if (asoc->strmout) {
5064 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
5065 asoc->strmout = NULL;
5067 asoc->strm_realoutsize = asoc->streamoutcnt = 0;
5069 struct sctp_queued_to_read *ctl, *nctl;
5071 for (i = 0; i < asoc->streamincnt; i++) {
5072 TAILQ_FOREACH_SAFE(ctl, &asoc->strmin[i].inqueue, next, nctl) {
5073 TAILQ_REMOVE(&asoc->strmin[i].inqueue, ctl, next);
5074 sctp_free_remote_addr(ctl->whoFrom);
5076 sctp_m_freem(ctl->data);
5080 * We don't free the address here since all
5081 * the net's were freed above.
5083 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), ctl);
5084 SCTP_DECR_READQ_COUNT();
5087 SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
5088 asoc->strmin = NULL;
5090 asoc->streamincnt = 0;
5091 TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) {
5093 if (SCTP_BASE_INFO(ipi_count_raddr) == 0) {
5094 panic("no net's left alloc'ed, or list points to itself");
5097 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
5098 sctp_free_remote_addr(net);
5100 LIST_FOREACH_SAFE(laddr, &asoc->sctp_restricted_addrs, sctp_nxt_addr, naddr) {
5101 /* sa_ignore FREED_MEMORY */
5102 sctp_remove_laddr(laddr);
5105 /* pending asconf (address) parameters */
5106 TAILQ_FOREACH_SAFE(aparam, &asoc->asconf_queue, next, naparam) {
5107 /* sa_ignore FREED_MEMORY */
5108 TAILQ_REMOVE(&asoc->asconf_queue, aparam, next);
5109 SCTP_FREE(aparam, SCTP_M_ASC_ADDR);
5111 TAILQ_FOREACH_SAFE(aack, &asoc->asconf_ack_sent, next, naack) {
5112 /* sa_ignore FREED_MEMORY */
5113 TAILQ_REMOVE(&asoc->asconf_ack_sent, aack, next);
5114 if (aack->data != NULL) {
5115 sctp_m_freem(aack->data);
5117 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asconf_ack), aack);
5119 /* clean up auth stuff */
5120 if (asoc->local_hmacs)
5121 sctp_free_hmaclist(asoc->local_hmacs);
5122 if (asoc->peer_hmacs)
5123 sctp_free_hmaclist(asoc->peer_hmacs);
5125 if (asoc->local_auth_chunks)
5126 sctp_free_chunklist(asoc->local_auth_chunks);
5127 if (asoc->peer_auth_chunks)
5128 sctp_free_chunklist(asoc->peer_auth_chunks);
5130 sctp_free_authinfo(&asoc->authinfo);
5132 LIST_FOREACH_SAFE(shared_key, &asoc->shared_keys, next, nshared_key) {
5133 LIST_REMOVE(shared_key, next);
5134 sctp_free_sharedkey(shared_key);
5135 /* sa_ignore FREED_MEMORY */
5138 /* Insert new items here :> */
5140 /* Get rid of LOCK */
5141 SCTP_TCB_UNLOCK(stcb);
5142 SCTP_TCB_LOCK_DESTROY(stcb);
5143 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
5144 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5145 SCTP_INP_INFO_WUNLOCK();
5146 SCTP_INP_RLOCK(inp);
5148 #ifdef SCTP_TRACK_FREED_ASOCS
5149 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5150 /* now clean up the tasoc itself */
5151 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
5152 SCTP_DECR_ASOC_COUNT();
5154 LIST_INSERT_HEAD(&inp->sctp_asoc_free_list, stcb, sctp_tcblist);
5157 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
5158 SCTP_DECR_ASOC_COUNT();
5160 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5161 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5163 * If its NOT the inp_free calling us AND sctp_close
5164 * as been called, we call back...
5166 SCTP_INP_RUNLOCK(inp);
5168 * This will start the kill timer (if we are the
5169 * last one) since we hold an increment yet. But
5170 * this is the only safe way to do this since
5171 * otherwise if the socket closes at the same time
5172 * we are here we might collide in the cleanup.
5174 sctp_inpcb_free(inp,
5175 SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
5176 SCTP_CALLED_DIRECTLY_NOCMPSET);
5177 SCTP_INP_DECR_REF(inp);
5180 /* The socket is still open. */
5181 SCTP_INP_DECR_REF(inp);
5184 if (from_inpcbfree == SCTP_NORMAL_PROC) {
5185 SCTP_INP_RUNLOCK(inp);
5188 /* destroyed the asoc */
5189 #ifdef SCTP_LOG_CLOSING
5190 sctp_log_closing(inp, NULL, 11);
5198 * determine if a destination is "reachable" based upon the addresses bound
5199 * to the current endpoint (e.g. only v4 or v6 currently bound)
5202 * FIX: if we allow assoc-level bindx(), then this needs to be fixed to use
5203 * assoc level v4/v6 flags, as the assoc *may* not have the same address
5204 * types bound as its endpoint
5207 sctp_destination_is_reachable(struct sctp_tcb *stcb, struct sockaddr *destaddr)
5209 struct sctp_inpcb *inp;
5213 * No locks here, the TCB, in all cases is already locked and an
5214 * assoc is up. There is either a INP lock by the caller applied (in
5215 * asconf case when deleting an address) or NOT in the HB case,
5216 * however if HB then the INP increment is up and the INP will not
5217 * be removed (on top of the fact that we have a TCB lock). So we
5218 * only want to read the sctp_flags, which is either bound-all or
5219 * not.. no protection needed since once an assoc is up you can't be
5220 * changing your binding.
5222 inp = stcb->sctp_ep;
5223 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
5224 /* if bound all, destination is not restricted */
5226 * RRS: Question during lock work: Is this correct? If you
5227 * are bound-all you still might need to obey the V4--V6
5228 * flags??? IMO this bound-all stuff needs to be removed!
5232 /* NOTE: all "scope" checks are done when local addresses are added */
5233 switch (destaddr->sa_family) {
5236 answer = inp->ip_inp.inp.inp_vflag & INP_IPV6;
5241 answer = inp->ip_inp.inp.inp_vflag & INP_IPV4;
5245 /* invalid family, so it's unreachable */
5253 * update the inp_vflags on an endpoint
5256 sctp_update_ep_vflag(struct sctp_inpcb *inp)
5258 struct sctp_laddr *laddr;
5260 /* first clear the flag */
5261 inp->ip_inp.inp.inp_vflag = 0;
5262 /* set the flag based on addresses on the ep list */
5263 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
5264 if (laddr->ifa == NULL) {
5265 SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n",
5269 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
5272 switch (laddr->ifa->address.sa.sa_family) {
5275 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
5280 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
5290 * Add the address to the endpoint local address list There is nothing to be
5291 * done if we are bound to all addresses
5294 sctp_add_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa, uint32_t action)
5296 struct sctp_laddr *laddr;
5301 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
5302 /* You are already bound to all. You have it already */
5306 if (ifa->address.sa.sa_family == AF_INET6) {
5307 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
5308 /* Can't bind a non-useable addr. */
5313 /* first, is it already present? */
5314 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
5315 if (laddr->ifa == ifa) {
5322 /* Not in the ep list */
5323 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, action);
5327 /* update inp_vflag flags */
5328 switch (ifa->address.sa.sa_family) {
5331 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
5336 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
5348 * select a new (hopefully reachable) destination net (should only be used
5349 * when we deleted an ep addr that is the only usable source address to reach
5350 * the destination net)
5353 sctp_select_primary_destination(struct sctp_tcb *stcb)
5355 struct sctp_nets *net;
5357 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5358 /* for now, we'll just pick the first reachable one we find */
5359 if (net->dest_state & SCTP_ADDR_UNCONFIRMED)
5361 if (sctp_destination_is_reachable(stcb,
5362 (struct sockaddr *)&net->ro._l_addr)) {
5363 /* found a reachable destination */
5364 stcb->asoc.primary_destination = net;
5367 /* I can't there from here! ...we're gonna die shortly... */
5372 * Delete the address from the endpoint local address list There is nothing
5373 * to be done if we are bound to all addresses
5376 sctp_del_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
5378 struct sctp_laddr *laddr;
5382 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
5383 /* You are already bound to all. You have it already */
5386 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
5387 if (laddr->ifa == ifa) {
5392 if (fnd && (inp->laddr_count < 2)) {
5393 /* can't delete unless there are at LEAST 2 addresses */
5398 * clean up any use of this address go through our
5399 * associations and clear any last_used_address that match
5400 * this one for each assoc, see if a new primary_destination
5403 struct sctp_tcb *stcb;
5405 /* clean up "next_addr_touse" */
5406 if (inp->next_addr_touse == laddr)
5407 /* delete this address */
5408 inp->next_addr_touse = NULL;
5410 /* clean up "last_used_address" */
5411 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5412 struct sctp_nets *net;
5414 SCTP_TCB_LOCK(stcb);
5415 if (stcb->asoc.last_used_address == laddr)
5416 /* delete this address */
5417 stcb->asoc.last_used_address = NULL;
5419 * Now spin through all the nets and purge any ref
5422 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5423 if (net->ro._s_addr &&
5424 (net->ro._s_addr->ifa == laddr->ifa)) {
5425 /* Yep, purge src address selected */
5428 /* delete this address if cached */
5432 net->ro.ro_rt = NULL;
5434 sctp_free_ifa(net->ro._s_addr);
5435 net->ro._s_addr = NULL;
5436 net->src_addr_selected = 0;
5439 SCTP_TCB_UNLOCK(stcb);
5440 } /* for each tcb */
5441 /* remove it from the ep list */
5442 sctp_remove_laddr(laddr);
5444 /* update inp_vflag flags */
5445 sctp_update_ep_vflag(inp);
5451 * Add the address to the TCB local address restricted list.
5452 * This is a "pending" address list (eg. addresses waiting for an
5453 * ASCONF-ACK response) and cannot be used as a valid source address.
5456 sctp_add_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
5458 struct sctp_laddr *laddr;
5459 struct sctpladdr *list;
5462 * Assumes TCB is locked.. and possibly the INP. May need to
5463 * confirm/fix that if we need it and is not the case.
5465 list = &stcb->asoc.sctp_restricted_addrs;
5468 if (ifa->address.sa.sa_family == AF_INET6) {
5469 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
5470 /* Can't bind a non-existent addr. */
5475 /* does the address already exist? */
5476 LIST_FOREACH(laddr, list, sctp_nxt_addr) {
5477 if (laddr->ifa == ifa) {
5482 /* add to the list */
5483 (void)sctp_insert_laddr(list, ifa, 0);
5488 * insert an laddr entry with the given ifa for the desired list
5491 sctp_insert_laddr(struct sctpladdr *list, struct sctp_ifa *ifa, uint32_t act)
5493 struct sctp_laddr *laddr;
5495 laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
5496 if (laddr == NULL) {
5497 /* out of memory? */
5498 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
5501 SCTP_INCR_LADDR_COUNT();
5502 bzero(laddr, sizeof(*laddr));
5503 (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time);
5505 laddr->action = act;
5506 atomic_add_int(&ifa->refcount, 1);
5508 LIST_INSERT_HEAD(list, laddr, sctp_nxt_addr);
5514 * Remove an laddr entry from the local address list (on an assoc)
5517 sctp_remove_laddr(struct sctp_laddr *laddr)
5520 /* remove from the list */
5521 LIST_REMOVE(laddr, sctp_nxt_addr);
5522 sctp_free_ifa(laddr->ifa);
5523 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), laddr);
5524 SCTP_DECR_LADDR_COUNT();
5528 * Remove a local address from the TCB local address restricted list
5531 sctp_del_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
5533 struct sctp_inpcb *inp;
5534 struct sctp_laddr *laddr;
5537 * This is called by asconf work. It is assumed that a) The TCB is
5538 * locked and b) The INP is locked. This is true in as much as I can
5539 * trace through the entry asconf code where I did these locks.
5540 * Again, the ASCONF code is a bit different in that it does lock
5541 * the INP during its work often times. This must be since we don't
5542 * want other proc's looking up things while what they are looking
5543 * up is changing :-D
5546 inp = stcb->sctp_ep;
5547 /* if subset bound and don't allow ASCONF's, can't delete last */
5548 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) &&
5549 sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF)) {
5550 if (stcb->sctp_ep->laddr_count < 2) {
5551 /* can't delete last address */
5555 LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
5556 /* remove the address if it exists */
5557 if (laddr->ifa == NULL)
5559 if (laddr->ifa == ifa) {
5560 sctp_remove_laddr(laddr);
5565 /* address not found! */
5570 * Temporarily remove for __APPLE__ until we use the Tiger equivalents
5573 static int sctp_max_number_of_assoc = SCTP_MAX_NUM_OF_ASOC;
5574 static int sctp_scale_up_for_address = SCTP_SCALE_FOR_ADDR;
5578 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
5579 struct sctp_mcore_ctrl *sctp_mcore_workers = NULL;
5580 int *sctp_cpuarry = NULL;
5582 sctp_queue_to_mcore(struct mbuf *m, int off, int cpu_to_use)
5584 /* Queue a packet to a processor for the specified core */
5585 struct sctp_mcore_queue *qent;
5586 struct sctp_mcore_ctrl *wkq;
5589 if (sctp_mcore_workers == NULL) {
5590 /* Something went way bad during setup */
5591 sctp_input_with_port(m, off, 0);
5594 SCTP_MALLOC(qent, struct sctp_mcore_queue *,
5595 (sizeof(struct sctp_mcore_queue)),
5598 /* This is trouble */
5599 sctp_input_with_port(m, off, 0);
5606 wkq = &sctp_mcore_workers[cpu_to_use];
5607 SCTP_MCORE_QLOCK(wkq);
5609 TAILQ_INSERT_TAIL(&wkq->que, qent, next);
5610 if (wkq->running == 0) {
5613 SCTP_MCORE_QUNLOCK(wkq);
5615 wakeup(&wkq->running);
5620 sctp_mcore_thread(void *arg)
5623 struct sctp_mcore_ctrl *wkq;
5624 struct sctp_mcore_queue *qent;
5626 wkq = (struct sctp_mcore_ctrl *)arg;
5630 /* Wait for first tickle */
5631 SCTP_MCORE_LOCK(wkq);
5633 msleep(&wkq->running,
5635 0, "wait for pkt", 0);
5636 SCTP_MCORE_UNLOCK(wkq);
5638 /* Bind to our cpu */
5639 thread_lock(curthread);
5640 sched_bind(curthread, wkq->cpuid);
5641 thread_unlock(curthread);
5643 /* Now lets start working */
5644 SCTP_MCORE_LOCK(wkq);
5645 /* Now grab lock and go */
5647 SCTP_MCORE_QLOCK(wkq);
5650 qent = TAILQ_FIRST(&wkq->que);
5652 TAILQ_REMOVE(&wkq->que, qent, next);
5653 SCTP_MCORE_QUNLOCK(wkq);
5654 CURVNET_SET(qent->vn);
5658 SCTP_FREE(qent, SCTP_M_MCORE);
5660 sctp_input_with_port(m, off, 0);
5662 SCTP_PRINTF("V6 not yet supported\n");
5666 SCTP_MCORE_QLOCK(wkq);
5669 if (!TAILQ_EMPTY(&wkq->que)) {
5672 SCTP_MCORE_QUNLOCK(wkq);
5673 msleep(&wkq->running,
5675 0, "wait for pkt", 0);
5680 sctp_startup_mcore_threads(void)
5687 if (sctp_mcore_workers != NULL) {
5689 * Already been here in some previous vnet?
5693 SCTP_MALLOC(sctp_mcore_workers, struct sctp_mcore_ctrl *,
5694 ((mp_maxid + 1) * sizeof(struct sctp_mcore_ctrl)),
5696 if (sctp_mcore_workers == NULL) {
5700 memset(sctp_mcore_workers, 0, ((mp_maxid + 1) *
5701 sizeof(struct sctp_mcore_ctrl)));
5702 /* Init the structures */
5703 for (i = 0; i <= mp_maxid; i++) {
5704 TAILQ_INIT(&sctp_mcore_workers[i].que);
5705 SCTP_MCORE_LOCK_INIT(&sctp_mcore_workers[i]);
5706 SCTP_MCORE_QLOCK_INIT(&sctp_mcore_workers[i]);
5707 sctp_mcore_workers[i].cpuid = i;
5709 if (sctp_cpuarry == NULL) {
5710 SCTP_MALLOC(sctp_cpuarry, int *,
5711 (mp_ncpus * sizeof(int)),
5715 sctp_cpuarry[i] = cpu;
5719 /* Now start them all */
5721 (void)kproc_create(sctp_mcore_thread,
5722 (void *)&sctp_mcore_workers[cpu],
5723 &sctp_mcore_workers[cpu].thread_proc,
5737 * SCTP initialization for the PCB structures should be called by
5738 * the sctp_init() funciton.
5743 if (SCTP_BASE_VAR(sctp_pcb_initialized) != 0) {
5744 /* error I was called twice */
5747 SCTP_BASE_VAR(sctp_pcb_initialized) = 1;
5749 #if defined(SCTP_LOCAL_TRACE_BUF)
5750 bzero(&SCTP_BASE_SYSCTL(sctp_log), sizeof(struct sctp_log));
5752 #if defined(__FreeBSD__) && defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
5753 SCTP_MALLOC(SCTP_BASE_STATS, struct sctpstat *,
5754 ((mp_maxid + 1) * sizeof(struct sctpstat)),
5757 (void)SCTP_GETTIME_TIMEVAL(&tv);
5758 #if defined(__FreeBSD__) && defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
5759 bzero(SCTP_BASE_STATS, (sizeof(struct sctpstat) * (mp_maxid + 1)));
5760 SCTP_BASE_STATS[PCPU_GET(cpuid)].sctps_discontinuitytime.tv_sec = (uint32_t) tv.tv_sec;
5761 SCTP_BASE_STATS[PCPU_GET(cpuid)].sctps_discontinuitytime.tv_usec = (uint32_t) tv.tv_usec;
5763 bzero(&SCTP_BASE_STATS, sizeof(struct sctpstat));
5764 SCTP_BASE_STAT(sctps_discontinuitytime).tv_sec = (uint32_t) tv.tv_sec;
5765 SCTP_BASE_STAT(sctps_discontinuitytime).tv_usec = (uint32_t) tv.tv_usec;
5767 /* init the empty list of (All) Endpoints */
5768 LIST_INIT(&SCTP_BASE_INFO(listhead));
5771 /* init the hash table of endpoints */
5772 TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", &SCTP_BASE_SYSCTL(sctp_hashtblsize));
5773 TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", &SCTP_BASE_SYSCTL(sctp_pcbtblsize));
5774 TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", &SCTP_BASE_SYSCTL(sctp_chunkscale));
5775 SCTP_BASE_INFO(sctp_asochash) = SCTP_HASH_INIT((SCTP_BASE_SYSCTL(sctp_hashtblsize) * 31),
5776 &SCTP_BASE_INFO(hashasocmark));
5777 SCTP_BASE_INFO(sctp_ephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize),
5778 &SCTP_BASE_INFO(hashmark));
5779 SCTP_BASE_INFO(sctp_tcpephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize),
5780 &SCTP_BASE_INFO(hashtcpmark));
5781 SCTP_BASE_INFO(hashtblsize) = SCTP_BASE_SYSCTL(sctp_hashtblsize);
5784 SCTP_BASE_INFO(sctp_vrfhash) = SCTP_HASH_INIT(SCTP_SIZE_OF_VRF_HASH,
5785 &SCTP_BASE_INFO(hashvrfmark));
5787 SCTP_BASE_INFO(vrf_ifn_hash) = SCTP_HASH_INIT(SCTP_VRF_IFN_HASH_SIZE,
5788 &SCTP_BASE_INFO(vrf_ifn_hashmark));
5789 /* init the zones */
5791 * FIX ME: Should check for NULL returns, but if it does fail we are
5792 * doomed to panic anyways... add later maybe.
5794 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_ep), "sctp_ep",
5795 sizeof(struct sctp_inpcb), maxsockets);
5797 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asoc), "sctp_asoc",
5798 sizeof(struct sctp_tcb), sctp_max_number_of_assoc);
5800 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_laddr), "sctp_laddr",
5801 sizeof(struct sctp_laddr),
5802 (sctp_max_number_of_assoc * sctp_scale_up_for_address));
5804 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_net), "sctp_raddr",
5805 sizeof(struct sctp_nets),
5806 (sctp_max_number_of_assoc * sctp_scale_up_for_address));
5808 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_chunk), "sctp_chunk",
5809 sizeof(struct sctp_tmit_chunk),
5810 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
5812 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_readq), "sctp_readq",
5813 sizeof(struct sctp_queued_to_read),
5814 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
5816 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_strmoq), "sctp_stream_msg_out",
5817 sizeof(struct sctp_stream_queue_pending),
5818 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
5820 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf), "sctp_asconf",
5821 sizeof(struct sctp_asconf),
5822 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
5824 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf_ack), "sctp_asconf_ack",
5825 sizeof(struct sctp_asconf_ack),
5826 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
5829 /* Master Lock INIT for info structure */
5830 SCTP_INP_INFO_LOCK_INIT();
5831 SCTP_STATLOG_INIT_LOCK();
5833 SCTP_IPI_COUNT_INIT();
5834 SCTP_IPI_ADDR_INIT();
5835 #ifdef SCTP_PACKET_LOGGING
5836 SCTP_IP_PKTLOG_INIT();
5838 LIST_INIT(&SCTP_BASE_INFO(addr_wq));
5840 SCTP_WQ_ADDR_INIT();
5841 /* not sure if we need all the counts */
5842 SCTP_BASE_INFO(ipi_count_ep) = 0;
5843 /* assoc/tcb zone info */
5844 SCTP_BASE_INFO(ipi_count_asoc) = 0;
5845 /* local addrlist zone info */
5846 SCTP_BASE_INFO(ipi_count_laddr) = 0;
5847 /* remote addrlist zone info */
5848 SCTP_BASE_INFO(ipi_count_raddr) = 0;
5850 SCTP_BASE_INFO(ipi_count_chunk) = 0;
5852 /* socket queue zone info */
5853 SCTP_BASE_INFO(ipi_count_readq) = 0;
5855 /* stream out queue cont */
5856 SCTP_BASE_INFO(ipi_count_strmoq) = 0;
5858 SCTP_BASE_INFO(ipi_free_strmoq) = 0;
5859 SCTP_BASE_INFO(ipi_free_chunks) = 0;
5861 SCTP_OS_TIMER_INIT(&SCTP_BASE_INFO(addr_wq_timer.timer));
5863 /* Init the TIMEWAIT list */
5864 for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) {
5865 LIST_INIT(&SCTP_BASE_INFO(vtag_timewait)[i]);
5867 sctp_startup_iterator();
5869 #if defined(__FreeBSD__) && defined(SCTP_MCORE_INPUT) && defined(SMP)
5870 sctp_startup_mcore_threads();
5874 * INIT the default VRF which for BSD is the only one, other O/S's
5875 * may have more. But initially they must start with one and then
5876 * add the VRF's as addresses are added.
5878 sctp_init_vrf_list(SCTP_DEFAULT_VRF);
5882 * Assumes that the SCTP_BASE_INFO() lock is NOT held.
5885 sctp_pcb_finish(void)
5887 struct sctp_vrflist *vrf_bucket;
5888 struct sctp_vrf *vrf, *nvrf;
5889 struct sctp_ifn *ifn, *nifn;
5890 struct sctp_ifa *ifa, *nifa;
5891 struct sctpvtaghead *chain;
5892 struct sctp_tagblock *twait_block, *prev_twait_block;
5893 struct sctp_laddr *wi, *nwi;
5895 struct sctp_iterator *it, *nit;
5898 * In FreeBSD the iterator thread never exits but we do clean up.
5899 * The only way FreeBSD reaches here is if we have VRF's but we
5900 * still add the ifdef to make it compile on old versions.
5902 SCTP_IPI_ITERATOR_WQ_LOCK();
5903 TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) {
5904 if (it->vn != curvnet) {
5907 TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
5908 if (it->function_atend != NULL) {
5909 (*it->function_atend) (it->pointer, it->val);
5911 SCTP_FREE(it, SCTP_M_ITER);
5913 SCTP_IPI_ITERATOR_WQ_UNLOCK();
5914 SCTP_ITERATOR_LOCK();
5915 if ((sctp_it_ctl.cur_it) &&
5916 (sctp_it_ctl.cur_it->vn == curvnet)) {
5917 sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_IT;
5919 SCTP_ITERATOR_UNLOCK();
5920 SCTP_OS_TIMER_STOP(&SCTP_BASE_INFO(addr_wq_timer.timer));
5921 SCTP_WQ_ADDR_LOCK();
5922 LIST_FOREACH_SAFE(wi, &SCTP_BASE_INFO(addr_wq), sctp_nxt_addr, nwi) {
5923 LIST_REMOVE(wi, sctp_nxt_addr);
5924 SCTP_DECR_LADDR_COUNT();
5925 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), wi);
5927 SCTP_WQ_ADDR_UNLOCK();
5930 * free the vrf/ifn/ifa lists and hashes (be sure address monitor is
5933 vrf_bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(SCTP_DEFAULT_VRFID & SCTP_BASE_INFO(hashvrfmark))];
5934 LIST_FOREACH_SAFE(vrf, vrf_bucket, next_vrf, nvrf) {
5935 LIST_FOREACH_SAFE(ifn, &vrf->ifnlist, next_ifn, nifn) {
5936 LIST_FOREACH_SAFE(ifa, &ifn->ifalist, next_ifa, nifa) {
5938 LIST_REMOVE(ifa, next_bucket);
5939 LIST_REMOVE(ifa, next_ifa);
5940 SCTP_FREE(ifa, SCTP_M_IFA);
5943 LIST_REMOVE(ifn, next_bucket);
5944 LIST_REMOVE(ifn, next_ifn);
5945 SCTP_FREE(ifn, SCTP_M_IFN);
5947 SCTP_HASH_FREE(vrf->vrf_addr_hash, vrf->vrf_addr_hashmark);
5949 LIST_REMOVE(vrf, next_vrf);
5950 SCTP_FREE(vrf, SCTP_M_VRF);
5952 /* free the vrf hashes */
5953 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_vrfhash), SCTP_BASE_INFO(hashvrfmark));
5954 SCTP_HASH_FREE(SCTP_BASE_INFO(vrf_ifn_hash), SCTP_BASE_INFO(vrf_ifn_hashmark));
5957 * free the TIMEWAIT list elements malloc'd in the function
5958 * sctp_add_vtag_to_timewait()...
5960 for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) {
5961 chain = &SCTP_BASE_INFO(vtag_timewait)[i];
5962 if (!LIST_EMPTY(chain)) {
5963 prev_twait_block = NULL;
5964 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
5965 if (prev_twait_block) {
5966 SCTP_FREE(prev_twait_block, SCTP_M_TIMW);
5968 prev_twait_block = twait_block;
5970 SCTP_FREE(prev_twait_block, SCTP_M_TIMW);
5974 /* free the locks and mutexes */
5975 #ifdef SCTP_PACKET_LOGGING
5976 SCTP_IP_PKTLOG_DESTROY();
5978 SCTP_IPI_ADDR_DESTROY();
5979 SCTP_STATLOG_DESTROY();
5980 SCTP_INP_INFO_LOCK_DESTROY();
5982 SCTP_WQ_ADDR_DESTROY();
5984 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_ep));
5985 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asoc));
5986 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_laddr));
5987 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_net));
5988 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_chunk));
5989 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_readq));
5990 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_strmoq));
5991 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf));
5992 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf_ack));
5993 /* Get rid of other stuff to */
5994 if (SCTP_BASE_INFO(sctp_asochash) != NULL)
5995 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_asochash), SCTP_BASE_INFO(hashasocmark));
5996 if (SCTP_BASE_INFO(sctp_ephash) != NULL)
5997 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_ephash), SCTP_BASE_INFO(hashmark));
5998 if (SCTP_BASE_INFO(sctp_tcpephash) != NULL)
5999 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_tcpephash), SCTP_BASE_INFO(hashtcpmark));
6000 #if defined(__FreeBSD__) && defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
6001 SCTP_FREE(SCTP_BASE_STATS, SCTP_M_MCORE);
6007 sctp_load_addresses_from_init(struct sctp_tcb *stcb, struct mbuf *m,
6008 int offset, int limit,
6009 struct sockaddr *src, struct sockaddr *dst,
6010 struct sockaddr *altsa)
6013 * grub through the INIT pulling addresses and loading them to the
6014 * nets structure in the asoc. The from address in the mbuf should
6015 * also be loaded (if it is not already). This routine can be called
6016 * with either INIT or INIT-ACK's as long as the m points to the IP
6017 * packet and the offset points to the beginning of the parameters.
6019 struct sctp_inpcb *inp;
6020 struct sctp_nets *net, *nnet, *net_tmp;
6021 struct sctp_paramhdr *phdr, parm_buf;
6022 struct sctp_tcb *stcb_tmp;
6023 uint16_t ptype, plen;
6024 struct sockaddr *sa;
6025 uint8_t random_store[SCTP_PARAM_BUFFER_SIZE];
6026 struct sctp_auth_random *p_random = NULL;
6027 uint16_t random_len = 0;
6028 uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE];
6029 struct sctp_auth_hmac_algo *hmacs = NULL;
6030 uint16_t hmacs_len = 0;
6031 uint8_t saw_asconf = 0;
6032 uint8_t saw_asconf_ack = 0;
6033 uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE];
6034 struct sctp_auth_chunk_list *chunks = NULL;
6035 uint16_t num_chunks = 0;
6036 sctp_key_t *new_key;
6038 int got_random = 0, got_hmacs = 0, got_chklist = 0;
6039 uint8_t ecn_allowed;
6042 struct sockaddr_in sin;
6046 struct sockaddr_in6 sin6;
6050 /* First get the destination address setup too. */
6052 memset(&sin, 0, sizeof(sin));
6053 sin.sin_family = AF_INET;
6054 sin.sin_len = sizeof(sin);
6055 sin.sin_port = stcb->rport;
6058 memset(&sin6, 0, sizeof(sin6));
6059 sin6.sin6_family = AF_INET6;
6060 sin6.sin6_len = sizeof(struct sockaddr_in6);
6061 sin6.sin6_port = stcb->rport;
6068 /* Turn off ECN until we get through all params */
6070 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6071 /* mark all addresses that we have currently on the list */
6072 net->dest_state |= SCTP_ADDR_NOT_IN_ASSOC;
6074 /* does the source address already exist? if so skip it */
6075 inp = stcb->sctp_ep;
6076 atomic_add_int(&stcb->asoc.refcnt, 1);
6077 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net_tmp, dst, stcb);
6078 atomic_add_int(&stcb->asoc.refcnt, -1);
6080 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || inp == NULL) {
6081 /* we must add the source address */
6082 /* no scope set here since we have a tcb already. */
6083 switch (sa->sa_family) {
6086 if (stcb->asoc.scope.ipv4_addr_legal) {
6087 if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_2)) {
6095 if (stcb->asoc.scope.ipv6_addr_legal) {
6096 if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) {
6106 if (net_tmp != NULL && stcb_tmp == stcb) {
6107 net_tmp->dest_state &= ~SCTP_ADDR_NOT_IN_ASSOC;
6108 } else if (stcb_tmp != stcb) {
6109 /* It belongs to another association? */
6111 SCTP_TCB_UNLOCK(stcb_tmp);
6115 if (stcb->asoc.state == 0) {
6116 /* the assoc was freed? */
6120 * peer must explicitly turn this on. This may have been initialized
6121 * to be "on" in order to allow local addr changes while INIT's are
6124 stcb->asoc.peer_supports_asconf = 0;
6125 /* now we must go through each of the params. */
6126 phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
6128 ptype = ntohs(phdr->param_type);
6129 plen = ntohs(phdr->param_length);
6131 * SCTP_PRINTF("ptype => %0x, plen => %d\n",
6132 * (uint32_t)ptype, (int)plen);
6134 if (offset + plen > limit) {
6141 if (ptype == SCTP_IPV4_ADDRESS) {
6142 if (stcb->asoc.scope.ipv4_addr_legal) {
6143 struct sctp_ipv4addr_param *p4, p4_buf;
6145 /* ok get the v4 address and check/add */
6146 phdr = sctp_get_next_param(m, offset,
6147 (struct sctp_paramhdr *)&p4_buf,
6149 if (plen != sizeof(struct sctp_ipv4addr_param) ||
6153 p4 = (struct sctp_ipv4addr_param *)phdr;
6154 sin.sin_addr.s_addr = p4->addr;
6155 if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
6156 /* Skip multi-cast addresses */
6159 if ((sin.sin_addr.s_addr == INADDR_BROADCAST) ||
6160 (sin.sin_addr.s_addr == INADDR_ANY)) {
6163 sa = (struct sockaddr *)&sin;
6164 inp = stcb->sctp_ep;
6165 atomic_add_int(&stcb->asoc.refcnt, 1);
6166 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
6168 atomic_add_int(&stcb->asoc.refcnt, -1);
6170 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) ||
6172 /* we must add the source address */
6174 * no scope set since we have a tcb
6179 * we must validate the state again
6183 if (stcb->asoc.state == 0) {
6184 /* the assoc was freed? */
6187 if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_4)) {
6190 } else if (stcb_tmp == stcb) {
6191 if (stcb->asoc.state == 0) {
6192 /* the assoc was freed? */
6198 ~SCTP_ADDR_NOT_IN_ASSOC;
6202 * strange, address is in another
6203 * assoc? straighten out locks.
6206 if (SCTP_GET_STATE(&stcb_tmp->asoc) & SCTP_STATE_COOKIE_WAIT) {
6211 sctp_abort_an_association(stcb_tmp->sctp_ep,
6212 stcb_tmp, NULL, SCTP_SO_NOT_LOCKED);
6215 SCTP_TCB_UNLOCK(stcb_tmp);
6217 if (stcb->asoc.state == 0) {
6218 /* the assoc was freed? */
6227 if (ptype == SCTP_IPV6_ADDRESS) {
6228 if (stcb->asoc.scope.ipv6_addr_legal) {
6229 /* ok get the v6 address and check/add */
6230 struct sctp_ipv6addr_param *p6, p6_buf;
6232 phdr = sctp_get_next_param(m, offset,
6233 (struct sctp_paramhdr *)&p6_buf,
6235 if (plen != sizeof(struct sctp_ipv6addr_param) ||
6239 p6 = (struct sctp_ipv6addr_param *)phdr;
6240 memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
6242 if (IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) {
6243 /* Skip multi-cast addresses */
6246 if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
6248 * Link local make no sense without
6253 sa = (struct sockaddr *)&sin6;
6254 inp = stcb->sctp_ep;
6255 atomic_add_int(&stcb->asoc.refcnt, 1);
6256 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
6258 atomic_add_int(&stcb->asoc.refcnt, -1);
6259 if (stcb_tmp == NULL &&
6260 (inp == stcb->sctp_ep || inp == NULL)) {
6262 * we must validate the state again
6266 if (stcb->asoc.state == 0) {
6267 /* the assoc was freed? */
6271 * we must add the address, no scope
6274 if (sctp_add_remote_addr(stcb, sa, NULL, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_5)) {
6277 } else if (stcb_tmp == stcb) {
6279 * we must validate the state again
6282 if (stcb->asoc.state == 0) {
6283 /* the assoc was freed? */
6289 ~SCTP_ADDR_NOT_IN_ASSOC;
6293 * strange, address is in another
6294 * assoc? straighten out locks.
6297 if (SCTP_GET_STATE(&stcb_tmp->asoc) & SCTP_STATE_COOKIE_WAIT) {
6302 sctp_abort_an_association(stcb_tmp->sctp_ep,
6303 stcb_tmp, NULL, SCTP_SO_NOT_LOCKED);
6306 SCTP_TCB_UNLOCK(stcb_tmp);
6308 if (stcb->asoc.state == 0) {
6309 /* the assoc was freed? */
6317 if (ptype == SCTP_ECN_CAPABLE) {
6319 } else if (ptype == SCTP_ULP_ADAPTATION) {
6320 if (stcb->asoc.state != SCTP_STATE_OPEN) {
6321 struct sctp_adaptation_layer_indication ai,
6324 phdr = sctp_get_next_param(m, offset,
6325 (struct sctp_paramhdr *)&ai, sizeof(ai));
6326 aip = (struct sctp_adaptation_layer_indication *)phdr;
6328 stcb->asoc.peers_adaptation = ntohl(aip->indication);
6329 stcb->asoc.adaptation_needed = 1;
6332 } else if (ptype == SCTP_SET_PRIM_ADDR) {
6333 struct sctp_asconf_addr_param lstore, *fee;
6335 struct sockaddr *lsa = NULL;
6338 struct sctp_asconf_addrv4_param *fii;
6342 stcb->asoc.peer_supports_asconf = 1;
6343 if (plen > sizeof(lstore)) {
6346 phdr = sctp_get_next_param(m, offset,
6347 (struct sctp_paramhdr *)&lstore,
6348 min(plen, sizeof(lstore)));
6352 fee = (struct sctp_asconf_addr_param *)phdr;
6353 lptype = ntohs(fee->addrp.ph.param_type);
6356 case SCTP_IPV4_ADDRESS:
6358 sizeof(struct sctp_asconf_addrv4_param)) {
6359 SCTP_PRINTF("Sizeof setprim in init/init ack not %d but %d - ignored\n",
6360 (int)sizeof(struct sctp_asconf_addrv4_param),
6363 fii = (struct sctp_asconf_addrv4_param *)fee;
6364 sin.sin_addr.s_addr = fii->addrp.addr;
6365 lsa = (struct sockaddr *)&sin;
6370 case SCTP_IPV6_ADDRESS:
6372 sizeof(struct sctp_asconf_addr_param)) {
6373 SCTP_PRINTF("Sizeof setprim (v6) in init/init ack not %d but %d - ignored\n",
6374 (int)sizeof(struct sctp_asconf_addr_param),
6377 memcpy(sin6.sin6_addr.s6_addr,
6379 sizeof(fee->addrp.addr));
6380 lsa = (struct sockaddr *)&sin6;
6388 (void)sctp_set_primary_addr(stcb, sa, NULL);
6390 } else if (ptype == SCTP_HAS_NAT_SUPPORT) {
6391 stcb->asoc.peer_supports_nat = 1;
6392 } else if (ptype == SCTP_PRSCTP_SUPPORTED) {
6393 /* Peer supports pr-sctp */
6394 stcb->asoc.peer_supports_prsctp = 1;
6395 } else if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
6396 /* A supported extension chunk */
6397 struct sctp_supported_chunk_types_param *pr_supported;
6398 uint8_t local_store[SCTP_PARAM_BUFFER_SIZE];
6401 phdr = sctp_get_next_param(m, offset,
6402 (struct sctp_paramhdr *)&local_store, min(sizeof(local_store), plen));
6406 stcb->asoc.peer_supports_asconf = 0;
6407 stcb->asoc.peer_supports_prsctp = 0;
6408 stcb->asoc.peer_supports_pktdrop = 0;
6409 stcb->asoc.peer_supports_strreset = 0;
6410 stcb->asoc.peer_supports_nr_sack = 0;
6411 stcb->asoc.peer_supports_auth = 0;
6412 pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
6413 num_ent = plen - sizeof(struct sctp_paramhdr);
6414 for (i = 0; i < num_ent; i++) {
6415 switch (pr_supported->chunk_types[i]) {
6417 case SCTP_ASCONF_ACK:
6418 stcb->asoc.peer_supports_asconf = 1;
6420 case SCTP_FORWARD_CUM_TSN:
6421 stcb->asoc.peer_supports_prsctp = 1;
6423 case SCTP_PACKET_DROPPED:
6424 stcb->asoc.peer_supports_pktdrop = 1;
6426 case SCTP_NR_SELECTIVE_ACK:
6427 stcb->asoc.peer_supports_nr_sack = 1;
6429 case SCTP_STREAM_RESET:
6430 stcb->asoc.peer_supports_strreset = 1;
6432 case SCTP_AUTHENTICATION:
6433 stcb->asoc.peer_supports_auth = 1;
6436 /* one I have not learned yet */
6441 } else if (ptype == SCTP_RANDOM) {
6442 if (plen > sizeof(random_store))
6445 /* already processed a RANDOM */
6448 phdr = sctp_get_next_param(m, offset,
6449 (struct sctp_paramhdr *)random_store,
6450 min(sizeof(random_store), plen));
6453 p_random = (struct sctp_auth_random *)phdr;
6454 random_len = plen - sizeof(*p_random);
6455 /* enforce the random length */
6456 if (random_len != SCTP_AUTH_RANDOM_SIZE_REQUIRED) {
6457 SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: invalid RANDOM len\n");
6461 } else if (ptype == SCTP_HMAC_LIST) {
6465 if (plen > sizeof(hmacs_store))
6468 /* already processed a HMAC list */
6471 phdr = sctp_get_next_param(m, offset,
6472 (struct sctp_paramhdr *)hmacs_store,
6473 min(plen, sizeof(hmacs_store)));
6476 hmacs = (struct sctp_auth_hmac_algo *)phdr;
6477 hmacs_len = plen - sizeof(*hmacs);
6478 num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]);
6479 /* validate the hmac list */
6480 if (sctp_verify_hmac_param(hmacs, num_hmacs)) {
6483 if (stcb->asoc.peer_hmacs != NULL)
6484 sctp_free_hmaclist(stcb->asoc.peer_hmacs);
6485 stcb->asoc.peer_hmacs = sctp_alloc_hmaclist(num_hmacs);
6486 if (stcb->asoc.peer_hmacs != NULL) {
6487 for (i = 0; i < num_hmacs; i++) {
6488 (void)sctp_auth_add_hmacid(stcb->asoc.peer_hmacs,
6489 ntohs(hmacs->hmac_ids[i]));
6493 } else if (ptype == SCTP_CHUNK_LIST) {
6496 if (plen > sizeof(chunks_store))
6499 /* already processed a Chunks list */
6502 phdr = sctp_get_next_param(m, offset,
6503 (struct sctp_paramhdr *)chunks_store,
6504 min(plen, sizeof(chunks_store)));
6507 chunks = (struct sctp_auth_chunk_list *)phdr;
6508 num_chunks = plen - sizeof(*chunks);
6509 if (stcb->asoc.peer_auth_chunks != NULL)
6510 sctp_clear_chunklist(stcb->asoc.peer_auth_chunks);
6512 stcb->asoc.peer_auth_chunks = sctp_alloc_chunklist();
6513 for (i = 0; i < num_chunks; i++) {
6514 (void)sctp_auth_add_chunk(chunks->chunk_types[i],
6515 stcb->asoc.peer_auth_chunks);
6516 /* record asconf/asconf-ack if listed */
6517 if (chunks->chunk_types[i] == SCTP_ASCONF)
6519 if (chunks->chunk_types[i] == SCTP_ASCONF_ACK)
6524 } else if ((ptype == SCTP_HEARTBEAT_INFO) ||
6525 (ptype == SCTP_STATE_COOKIE) ||
6526 (ptype == SCTP_UNRECOG_PARAM) ||
6527 (ptype == SCTP_COOKIE_PRESERVE) ||
6528 (ptype == SCTP_SUPPORTED_ADDRTYPE) ||
6529 (ptype == SCTP_ADD_IP_ADDRESS) ||
6530 (ptype == SCTP_DEL_IP_ADDRESS) ||
6531 (ptype == SCTP_ERROR_CAUSE_IND) ||
6532 (ptype == SCTP_SUCCESS_REPORT)) {
6535 if ((ptype & 0x8000) == 0x0000) {
6537 * must stop processing the rest of the
6538 * param's. Any report bits were handled
6540 * sctp_arethere_unrecognized_parameters()
6541 * when the INIT or INIT-ACK was first seen.
6548 offset += SCTP_SIZE32(plen);
6549 if (offset >= limit) {
6552 phdr = sctp_get_next_param(m, offset, &parm_buf,
6555 /* Now check to see if we need to purge any addresses */
6556 TAILQ_FOREACH_SAFE(net, &stcb->asoc.nets, sctp_next, nnet) {
6557 if ((net->dest_state & SCTP_ADDR_NOT_IN_ASSOC) ==
6558 SCTP_ADDR_NOT_IN_ASSOC) {
6559 /* This address has been removed from the asoc */
6560 /* remove and free it */
6561 stcb->asoc.numnets--;
6562 TAILQ_REMOVE(&stcb->asoc.nets, net, sctp_next);
6563 sctp_free_remote_addr(net);
6564 if (net == stcb->asoc.primary_destination) {
6565 stcb->asoc.primary_destination = NULL;
6566 sctp_select_primary_destination(stcb);
6570 if (ecn_allowed == 0) {
6571 stcb->asoc.ecn_allowed = 0;
6573 /* validate authentication required parameters */
6574 if (got_random && got_hmacs) {
6575 stcb->asoc.peer_supports_auth = 1;
6577 stcb->asoc.peer_supports_auth = 0;
6579 if (!stcb->asoc.peer_supports_auth && got_chklist) {
6580 /* peer does not support auth but sent a chunks list? */
6583 if (!SCTP_BASE_SYSCTL(sctp_asconf_auth_nochk) && stcb->asoc.peer_supports_asconf &&
6584 !stcb->asoc.peer_supports_auth) {
6585 /* peer supports asconf but not auth? */
6587 } else if ((stcb->asoc.peer_supports_asconf) && (stcb->asoc.peer_supports_auth) &&
6588 ((saw_asconf == 0) || (saw_asconf_ack == 0))) {
6591 /* concatenate the full random key */
6592 keylen = sizeof(*p_random) + random_len + sizeof(*hmacs) + hmacs_len;
6593 if (chunks != NULL) {
6594 keylen += sizeof(*chunks) + num_chunks;
6596 new_key = sctp_alloc_key(keylen);
6597 if (new_key != NULL) {
6598 /* copy in the RANDOM */
6599 if (p_random != NULL) {
6600 keylen = sizeof(*p_random) + random_len;
6601 bcopy(p_random, new_key->key, keylen);
6603 /* append in the AUTH chunks */
6604 if (chunks != NULL) {
6605 bcopy(chunks, new_key->key + keylen,
6606 sizeof(*chunks) + num_chunks);
6607 keylen += sizeof(*chunks) + num_chunks;
6609 /* append in the HMACs */
6610 if (hmacs != NULL) {
6611 bcopy(hmacs, new_key->key + keylen,
6612 sizeof(*hmacs) + hmacs_len);
6615 /* failed to get memory for the key */
6618 if (stcb->asoc.authinfo.peer_random != NULL)
6619 sctp_free_key(stcb->asoc.authinfo.peer_random);
6620 stcb->asoc.authinfo.peer_random = new_key;
6621 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid);
6622 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid);
6628 sctp_set_primary_addr(struct sctp_tcb *stcb, struct sockaddr *sa,
6629 struct sctp_nets *net)
6631 /* make sure the requested primary address exists in the assoc */
6632 if (net == NULL && sa)
6633 net = sctp_findnet(stcb, sa);
6636 /* didn't find the requested primary address! */
6639 /* set the primary address */
6640 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
6641 /* Must be confirmed, so queue to set */
6642 net->dest_state |= SCTP_ADDR_REQ_PRIMARY;
6645 stcb->asoc.primary_destination = net;
6646 if (!(net->dest_state & SCTP_ADDR_PF) && (stcb->asoc.alternate)) {
6647 sctp_free_remote_addr(stcb->asoc.alternate);
6648 stcb->asoc.alternate = NULL;
6650 net = TAILQ_FIRST(&stcb->asoc.nets);
6651 if (net != stcb->asoc.primary_destination) {
6653 * first one on the list is NOT the primary
6654 * sctp_cmpaddr() is much more efficient if the
6655 * primary is the first on the list, make it so.
6657 TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
6658 TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
6665 sctp_is_vtag_good(uint32_t tag, uint16_t lport, uint16_t rport, struct timeval *now)
6668 * This function serves two purposes. It will see if a TAG can be
6669 * re-used and return 1 for yes it is ok and 0 for don't use that
6670 * tag. A secondary function it will do is purge out old tags that
6673 struct sctpvtaghead *chain;
6674 struct sctp_tagblock *twait_block;
6675 struct sctpasochead *head;
6676 struct sctp_tcb *stcb;
6679 SCTP_INP_INFO_RLOCK();
6680 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
6681 SCTP_BASE_INFO(hashasocmark))];
6684 goto skip_vtag_check;
6686 LIST_FOREACH(stcb, head, sctp_asocs) {
6688 * We choose not to lock anything here. TCB's can't be
6689 * removed since we have the read lock, so they can't be
6690 * freed on us, same thing for the INP. I may be wrong with
6691 * this assumption, but we will go with it for now :-)
6693 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
6696 if (stcb->asoc.my_vtag == tag) {
6698 if (stcb->rport != rport) {
6701 if (stcb->sctp_ep->sctp_lport != lport) {
6704 /* Its a used tag set */
6705 SCTP_INP_INFO_RUNLOCK();
6711 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
6712 /* Now what about timed wait ? */
6713 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
6715 * Block(s) are present, lets see if we have this tag in the
6718 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
6719 if (twait_block->vtag_block[i].v_tag == 0) {
6722 } else if ((long)twait_block->vtag_block[i].tv_sec_at_expire <
6724 /* Audit expires this guy */
6725 twait_block->vtag_block[i].tv_sec_at_expire = 0;
6726 twait_block->vtag_block[i].v_tag = 0;
6727 twait_block->vtag_block[i].lport = 0;
6728 twait_block->vtag_block[i].rport = 0;
6729 } else if ((twait_block->vtag_block[i].v_tag == tag) &&
6730 (twait_block->vtag_block[i].lport == lport) &&
6731 (twait_block->vtag_block[i].rport == rport)) {
6732 /* Bad tag, sorry :< */
6733 SCTP_INP_INFO_RUNLOCK();
6738 SCTP_INP_INFO_RUNLOCK();
6743 sctp_drain_mbufs(struct sctp_tcb *stcb)
6746 * We must hunt this association for MBUF's past the cumack (i.e.
6747 * out of order data that we can renege on).
6749 struct sctp_association *asoc;
6750 struct sctp_tmit_chunk *chk, *nchk;
6751 uint32_t cumulative_tsn_p1;
6752 struct sctp_queued_to_read *ctl, *nctl;
6757 /* We look for anything larger than the cum-ack + 1 */
6760 if (asoc->cumulative_tsn == asoc->highest_tsn_inside_map) {
6761 /* none we can reneg on. */
6764 SCTP_STAT_INCR(sctps_protocol_drains_done);
6765 cumulative_tsn_p1 = asoc->cumulative_tsn + 1;
6767 /* First look in the re-assembly queue */
6768 TAILQ_FOREACH_SAFE(chk, &asoc->reasmqueue, sctp_next, nchk) {
6769 if (SCTP_TSN_GT(chk->rec.data.TSN_seq, cumulative_tsn_p1)) {
6770 /* Yep it is above cum-ack */
6772 SCTP_CALC_TSN_TO_GAP(gap, chk->rec.data.TSN_seq, asoc->mapping_array_base_tsn);
6773 asoc->size_on_reasm_queue = sctp_sbspace_sub(asoc->size_on_reasm_queue, chk->send_size);
6774 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
6775 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
6776 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
6778 sctp_m_freem(chk->data);
6781 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
6784 /* Ok that was fun, now we will drain all the inbound streams? */
6785 for (strmat = 0; strmat < asoc->streamincnt; strmat++) {
6786 TAILQ_FOREACH_SAFE(ctl, &asoc->strmin[strmat].inqueue, next, nctl) {
6787 if (SCTP_TSN_GT(ctl->sinfo_tsn, cumulative_tsn_p1)) {
6788 /* Yep it is above cum-ack */
6790 SCTP_CALC_TSN_TO_GAP(gap, ctl->sinfo_tsn, asoc->mapping_array_base_tsn);
6791 asoc->size_on_all_streams = sctp_sbspace_sub(asoc->size_on_all_streams, ctl->length);
6792 sctp_ucount_decr(asoc->cnt_on_all_streams);
6793 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
6794 TAILQ_REMOVE(&asoc->strmin[strmat].inqueue, ctl, next);
6796 sctp_m_freem(ctl->data);
6799 sctp_free_remote_addr(ctl->whoFrom);
6800 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), ctl);
6801 SCTP_DECR_READQ_COUNT();
6806 /* We must back down to see what the new highest is */
6807 for (i = asoc->highest_tsn_inside_map; SCTP_TSN_GE(i, asoc->mapping_array_base_tsn); i--) {
6808 SCTP_CALC_TSN_TO_GAP(gap, i, asoc->mapping_array_base_tsn);
6809 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
6810 asoc->highest_tsn_inside_map = i;
6816 asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn - 1;
6819 * Question, should we go through the delivery queue? The
6820 * only reason things are on here is the app not reading OR
6821 * a p-d-api up. An attacker COULD send enough in to
6822 * initiate the PD-API and then send a bunch of stuff to
6823 * other streams... these would wind up on the delivery
6824 * queue.. and then we would not get to them. But in order
6825 * to do this I then have to back-track and un-deliver
6826 * sequence numbers in streams.. el-yucko. I think for now
6827 * we will NOT look at the delivery queue and leave it to be
6828 * something to consider later. An alternative would be to
6829 * abort the P-D-API with a notification and then deliver
6830 * the data.... Or another method might be to keep track of
6831 * how many times the situation occurs and if we see a
6832 * possible attack underway just abort the association.
6835 SCTPDBG(SCTP_DEBUG_PCB1, "Freed %d chunks from reneg harvest\n", cnt);
6838 * Now do we need to find a new
6839 * asoc->highest_tsn_inside_map?
6841 asoc->last_revoke_count = cnt;
6842 (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
6843 /* sa_ignore NO_NULL_CHK */
6844 sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
6845 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_DRAIN, SCTP_SO_NOT_LOCKED);
6848 * Another issue, in un-setting the TSN's in the mapping array we
6849 * DID NOT adjust the highest_tsn marker. This will cause one of
6850 * two things to occur. It may cause us to do extra work in checking
6851 * for our mapping array movement. More importantly it may cause us
6852 * to SACK every datagram. This may not be a bad thing though since
6853 * we will recover once we get our cum-ack above and all this stuff
6854 * we dumped recovered.
6862 * We must walk the PCB lists for ALL associations here. The system
6863 * is LOW on MBUF's and needs help. This is where reneging will
6864 * occur. We really hope this does NOT happen!
6866 VNET_ITERATOR_DECL(vnet_iter);
6867 VNET_LIST_RLOCK_NOSLEEP();
6868 VNET_FOREACH(vnet_iter) {
6869 CURVNET_SET(vnet_iter);
6870 struct sctp_inpcb *inp;
6871 struct sctp_tcb *stcb;
6873 SCTP_STAT_INCR(sctps_protocol_drain_calls);
6874 if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
6881 SCTP_INP_INFO_RLOCK();
6882 LIST_FOREACH(inp, &SCTP_BASE_INFO(listhead), sctp_list) {
6883 /* For each endpoint */
6884 SCTP_INP_RLOCK(inp);
6885 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6886 /* For each association */
6887 SCTP_TCB_LOCK(stcb);
6888 sctp_drain_mbufs(stcb);
6889 SCTP_TCB_UNLOCK(stcb);
6891 SCTP_INP_RUNLOCK(inp);
6893 SCTP_INP_INFO_RUNLOCK();
6896 VNET_LIST_RUNLOCK_NOSLEEP();
6900 * start a new iterator
6901 * iterates through all endpoints and associations based on the pcb_state
6902 * flags and asoc_state. "af" (mandatory) is executed for all matching
6903 * assocs and "ef" (optional) is executed when the iterator completes.
6904 * "inpf" (optional) is executed for each new endpoint as it is being
6905 * iterated through. inpe (optional) is called when the inp completes
6906 * its way through all the stcbs.
6909 sctp_initiate_iterator(inp_func inpf,
6913 uint32_t pcb_features,
6914 uint32_t asoc_state,
6918 struct sctp_inpcb *s_inp,
6919 uint8_t chunk_output_off)
6921 struct sctp_iterator *it = NULL;
6926 SCTP_MALLOC(it, struct sctp_iterator *, sizeof(struct sctp_iterator),
6929 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
6932 memset(it, 0, sizeof(*it));
6933 it->function_assoc = af;
6934 it->function_inp = inpf;
6936 it->done_current_ep = 0;
6938 it->done_current_ep = 1;
6939 it->function_atend = ef;
6942 it->pcb_flags = pcb_state;
6943 it->pcb_features = pcb_features;
6944 it->asoc_state = asoc_state;
6945 it->function_inp_end = inpe;
6946 it->no_chunk_output = chunk_output_off;
6949 /* Assume lock is held here */
6951 SCTP_INP_INCR_REF(it->inp);
6952 it->iterator_flags = SCTP_ITERATOR_DO_SINGLE_INP;
6954 SCTP_INP_INFO_RLOCK();
6955 it->inp = LIST_FIRST(&SCTP_BASE_INFO(listhead));
6957 SCTP_INP_INCR_REF(it->inp);
6959 SCTP_INP_INFO_RUNLOCK();
6960 it->iterator_flags = SCTP_ITERATOR_DO_ALL_INP;
6963 SCTP_IPI_ITERATOR_WQ_LOCK();
6965 TAILQ_INSERT_TAIL(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
6966 if (sctp_it_ctl.iterator_running == 0) {
6967 sctp_wakeup_iterator();
6969 SCTP_IPI_ITERATOR_WQ_UNLOCK();
6970 /* sa_ignore MEMLEAK {memory is put on the tailq for the iterator} */