2 * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * a) Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
10 * b) Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the distribution.
14 * c) Neither the name of Cisco Systems, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 * THE POSSIBILITY OF SUCH DAMAGE.
31 /* $KAME: sctp_pcb.c,v 1.38 2005/03/06 16:04:18 itojun Exp $ */
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/udp.h>
51 struct sctp_base_info system_base_info;
53 /* FIX: we don't handle multiple link local scopes */
54 /* "scopeless" replacement IN6_ARE_ADDR_EQUAL */
57 SCTP6_ARE_ADDR_EQUAL(struct sockaddr_in6 *a, struct sockaddr_in6 *b)
59 struct sockaddr_in6 tmp_a, tmp_b;
61 memcpy(&tmp_a, a, sizeof(struct sockaddr_in6));
62 if (sa6_embedscope(&tmp_a, MODULE_GLOBAL(MOD_INET6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone))) != 0) {
65 memcpy(&tmp_b, b, sizeof(struct sockaddr_in6));
66 if (sa6_embedscope(&tmp_b, MODULE_GLOBAL(MOD_INET6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone))) != 0) {
69 return (IN6_ARE_ADDR_EQUAL(&tmp_a.sin6_addr, &tmp_b.sin6_addr));
75 sctp_fill_pcbinfo(struct sctp_pcbinfo *spcb)
78 * We really don't need to lock this, but I will just because it
81 SCTP_INP_INFO_RLOCK();
82 spcb->ep_count = SCTP_BASE_INFO(ipi_count_ep);
83 spcb->asoc_count = SCTP_BASE_INFO(ipi_count_asoc);
84 spcb->laddr_count = SCTP_BASE_INFO(ipi_count_laddr);
85 spcb->raddr_count = SCTP_BASE_INFO(ipi_count_raddr);
86 spcb->chk_count = SCTP_BASE_INFO(ipi_count_chunk);
87 spcb->readq_count = SCTP_BASE_INFO(ipi_count_readq);
88 spcb->stream_oque = SCTP_BASE_INFO(ipi_count_strmoq);
89 spcb->free_chunks = SCTP_BASE_INFO(ipi_free_chunks);
91 SCTP_INP_INFO_RUNLOCK();
95 * Addresses are added to VRF's (Virtual Router's). For BSD we
96 * have only the default VRF 0. We maintain a hash list of
97 * VRF's. Each VRF has its own list of sctp_ifn's. Each of
98 * these has a list of addresses. When we add a new address
99 * to a VRF we lookup the ifn/ifn_index, if the ifn does
100 * not exist we create it and add it to the list of IFN's
101 * within the VRF. Once we have the sctp_ifn, we add the
102 * address to the list. So we look something like:
105 * vrf-> ifn-> ifn -> ifn
107 * ... +--ifa-> ifa -> ifa
110 * We keep these seperate lists since the SCTP subsystem will
111 * point to these from its source address selection nets structure.
112 * When an address is deleted it does not happen right away on
113 * the SCTP side, it gets scheduled. What we do when a
114 * delete happens is immediately remove the address from
115 * the master list and decrement the refcount. As our
116 * addip iterator works through and frees the src address
117 * selection pointing to the sctp_ifa, eventually the refcount
118 * will reach 0 and we will delete it. Note that it is assumed
119 * that any locking on system level ifn/ifa is done at the
120 * caller of these functions and these routines will only
121 * lock the SCTP structures as they add or delete things.
123 * Other notes on VRF concepts.
124 * - An endpoint can be in multiple VRF's
125 * - An association lives within a VRF and only one VRF.
126 * - Any incoming packet we can deduce the VRF for by
127 * looking at the mbuf/pak inbound (for BSD its VRF=0 :D)
128 * - Any downward send call or connect call must supply the
129 * VRF via ancillary data or via some sort of set default
130 * VRF socket option call (again for BSD no brainer since
131 * the VRF is always 0).
132 * - An endpoint may add multiple VRF's to it.
133 * - Listening sockets can accept associations in any
134 * of the VRF's they are in but the assoc will end up
135 * in only one VRF (gotten from the packet or connect/send).
140 sctp_allocate_vrf(int vrf_id)
142 struct sctp_vrf *vrf = NULL;
143 struct sctp_vrflist *bucket;
145 /* First allocate the VRF structure */
146 vrf = sctp_find_vrf(vrf_id);
148 /* Already allocated */
151 SCTP_MALLOC(vrf, struct sctp_vrf *, sizeof(struct sctp_vrf),
156 panic("No memory for VRF:%d", vrf_id);
161 memset(vrf, 0, sizeof(struct sctp_vrf));
162 vrf->vrf_id = vrf_id;
163 LIST_INIT(&vrf->ifnlist);
164 vrf->total_ifa_count = 0;
166 /* now also setup table ids */
167 SCTP_INIT_VRF_TABLEID(vrf);
168 /* Init the HASH of addresses */
169 vrf->vrf_addr_hash = SCTP_HASH_INIT(SCTP_VRF_ADDR_HASH_SIZE,
170 &vrf->vrf_addr_hashmark);
171 if (vrf->vrf_addr_hash == NULL) {
174 panic("No memory for VRF:%d", vrf_id);
176 SCTP_FREE(vrf, SCTP_M_VRF);
179 /* Add it to the hash table */
180 bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(vrf_id & SCTP_BASE_INFO(hashvrfmark))];
181 LIST_INSERT_HEAD(bucket, vrf, next_vrf);
182 atomic_add_int(&SCTP_BASE_INFO(ipi_count_vrfs), 1);
188 sctp_find_ifn(void *ifn, uint32_t ifn_index)
190 struct sctp_ifn *sctp_ifnp;
191 struct sctp_ifnlist *hash_ifn_head;
194 * We assume the lock is held for the addresses if thats wrong
195 * problems could occur :-)
197 hash_ifn_head = &SCTP_BASE_INFO(vrf_ifn_hash)[(ifn_index & SCTP_BASE_INFO(vrf_ifn_hashmark))];
198 LIST_FOREACH(sctp_ifnp, hash_ifn_head, next_bucket) {
199 if (sctp_ifnp->ifn_index == ifn_index) {
202 if (sctp_ifnp->ifn_p && ifn && (sctp_ifnp->ifn_p == ifn)) {
212 sctp_find_vrf(uint32_t vrf_id)
214 struct sctp_vrflist *bucket;
215 struct sctp_vrf *liste;
217 bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(vrf_id & SCTP_BASE_INFO(hashvrfmark))];
218 LIST_FOREACH(liste, bucket, next_vrf) {
219 if (vrf_id == liste->vrf_id) {
227 sctp_free_vrf(struct sctp_vrf *vrf)
231 ret = atomic_fetchadd_int(&vrf->refcount, -1);
233 if (vrf->vrf_addr_hash) {
234 SCTP_HASH_FREE(vrf->vrf_addr_hash, vrf->vrf_addr_hashmark);
235 vrf->vrf_addr_hash = NULL;
237 /* We zero'd the count */
238 LIST_REMOVE(vrf, next_vrf);
239 SCTP_FREE(vrf, SCTP_M_VRF);
240 atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_vrfs), 1);
245 sctp_free_ifn(struct sctp_ifn *sctp_ifnp)
249 ret = atomic_fetchadd_int(&sctp_ifnp->refcount, -1);
251 /* We zero'd the count */
252 if (sctp_ifnp->vrf) {
253 sctp_free_vrf(sctp_ifnp->vrf);
255 SCTP_FREE(sctp_ifnp, SCTP_M_IFN);
256 atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_ifns), 1);
261 sctp_update_ifn_mtu(uint32_t ifn_index, uint32_t mtu)
263 struct sctp_ifn *sctp_ifnp;
265 sctp_ifnp = sctp_find_ifn((void *)NULL, ifn_index);
266 if (sctp_ifnp != NULL) {
267 sctp_ifnp->ifn_mtu = mtu;
273 sctp_free_ifa(struct sctp_ifa *sctp_ifap)
277 ret = atomic_fetchadd_int(&sctp_ifap->refcount, -1);
279 /* We zero'd the count */
280 if (sctp_ifap->ifn_p) {
281 sctp_free_ifn(sctp_ifap->ifn_p);
283 SCTP_FREE(sctp_ifap, SCTP_M_IFA);
284 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);
311 sctp_mark_ifa_addr_down(uint32_t vrf_id, struct sockaddr *addr,
312 const char *if_name, uint32_t ifn_index)
314 struct sctp_vrf *vrf;
315 struct sctp_ifa *sctp_ifap = NULL;
317 SCTP_IPI_ADDR_RLOCK();
318 vrf = sctp_find_vrf(vrf_id);
320 SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id);
324 sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
325 if (sctp_ifap == NULL) {
326 SCTPDBG(SCTP_DEBUG_PCB4, "Can't find sctp_ifap for address\n");
329 if (sctp_ifap->ifn_p == NULL) {
330 SCTPDBG(SCTP_DEBUG_PCB4, "IFA has no IFN - can't mark unuseable\n");
336 len1 = strlen(if_name);
337 len2 = strlen(sctp_ifap->ifn_p->ifn_name);
339 SCTPDBG(SCTP_DEBUG_PCB4, "IFN of ifa names different lenght %d vs %d - ignored\n",
343 if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, len1) != 0) {
344 SCTPDBG(SCTP_DEBUG_PCB4, "IFN %s of IFA not the same as %s\n",
345 sctp_ifap->ifn_p->ifn_name,
350 if (sctp_ifap->ifn_p->ifn_index != ifn_index) {
351 SCTPDBG(SCTP_DEBUG_PCB4, "IFA owned by ifn_index:%d down command for ifn_index:%d - ignored\n",
352 sctp_ifap->ifn_p->ifn_index, ifn_index);
357 sctp_ifap->localifa_flags &= (~SCTP_ADDR_VALID);
358 sctp_ifap->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE;
360 SCTP_IPI_ADDR_RUNLOCK();
364 sctp_mark_ifa_addr_up(uint32_t vrf_id, struct sockaddr *addr,
365 const char *if_name, uint32_t ifn_index)
367 struct sctp_vrf *vrf;
368 struct sctp_ifa *sctp_ifap = NULL;
370 SCTP_IPI_ADDR_RLOCK();
371 vrf = sctp_find_vrf(vrf_id);
373 SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id);
377 sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
378 if (sctp_ifap == NULL) {
379 SCTPDBG(SCTP_DEBUG_PCB4, "Can't find sctp_ifap for address\n");
382 if (sctp_ifap->ifn_p == NULL) {
383 SCTPDBG(SCTP_DEBUG_PCB4, "IFA has no IFN - can't mark unuseable\n");
389 len1 = strlen(if_name);
390 len2 = strlen(sctp_ifap->ifn_p->ifn_name);
392 SCTPDBG(SCTP_DEBUG_PCB4, "IFN of ifa names different lenght %d vs %d - ignored\n",
396 if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, len1) != 0) {
397 SCTPDBG(SCTP_DEBUG_PCB4, "IFN %s of IFA not the same as %s\n",
398 sctp_ifap->ifn_p->ifn_name,
403 if (sctp_ifap->ifn_p->ifn_index != ifn_index) {
404 SCTPDBG(SCTP_DEBUG_PCB4, "IFA owned by ifn_index:%d down command for ifn_index:%d - ignored\n",
405 sctp_ifap->ifn_p->ifn_index, ifn_index);
410 sctp_ifap->localifa_flags &= (~SCTP_ADDR_IFA_UNUSEABLE);
411 sctp_ifap->localifa_flags |= SCTP_ADDR_VALID;
413 SCTP_IPI_ADDR_RUNLOCK();
417 * Add an ifa to an ifn.
418 * Register the interface as necessary.
419 * NOTE: ADDR write lock MUST be held.
422 sctp_add_ifa_to_ifn(struct sctp_ifn *sctp_ifnp, struct sctp_ifa *sctp_ifap)
426 LIST_INSERT_HEAD(&sctp_ifnp->ifalist, sctp_ifap, next_ifa);
427 sctp_ifap->ifn_p = sctp_ifnp;
428 atomic_add_int(&sctp_ifap->ifn_p->refcount, 1);
429 /* update address counts */
430 sctp_ifnp->ifa_count++;
431 ifa_af = sctp_ifap->address.sa.sa_family;
432 if (ifa_af == AF_INET)
436 if (sctp_ifnp->ifa_count == 1) {
437 /* register the new interface */
438 SCTP_REGISTER_INTERFACE(sctp_ifnp->ifn_index, ifa_af);
439 sctp_ifnp->registered_af = ifa_af;
444 * Remove an ifa from its ifn.
445 * If no more addresses exist, remove the ifn too. Otherwise, re-register
446 * the interface based on the remaining address families left.
447 * NOTE: ADDR write lock MUST be held.
450 sctp_remove_ifa_from_ifn(struct sctp_ifa *sctp_ifap)
454 LIST_REMOVE(sctp_ifap, next_ifa);
455 if (sctp_ifap->ifn_p) {
456 /* update address counts */
457 sctp_ifap->ifn_p->ifa_count--;
458 if (sctp_ifap->address.sa.sa_family == AF_INET6)
459 sctp_ifap->ifn_p->num_v6--;
460 else if (sctp_ifap->address.sa.sa_family == AF_INET)
461 sctp_ifap->ifn_p->num_v4--;
463 ifn_index = sctp_ifap->ifn_p->ifn_index;
464 if (SCTP_LIST_EMPTY(&sctp_ifap->ifn_p->ifalist)) {
465 /* remove the ifn, possibly freeing it */
466 sctp_delete_ifn(sctp_ifap->ifn_p, SCTP_ADDR_LOCKED);
468 /* re-register address family type, if needed */
469 if ((sctp_ifap->ifn_p->num_v6 == 0) &&
470 (sctp_ifap->ifn_p->registered_af == AF_INET6)) {
471 SCTP_DEREGISTER_INTERFACE(ifn_index, AF_INET6);
472 SCTP_REGISTER_INTERFACE(ifn_index, AF_INET);
473 sctp_ifap->ifn_p->registered_af = AF_INET;
474 } else if ((sctp_ifap->ifn_p->num_v4 == 0) &&
475 (sctp_ifap->ifn_p->registered_af == AF_INET)) {
476 SCTP_DEREGISTER_INTERFACE(ifn_index, AF_INET);
477 SCTP_REGISTER_INTERFACE(ifn_index, AF_INET6);
478 sctp_ifap->ifn_p->registered_af = AF_INET6;
480 /* free the ifn refcount */
481 sctp_free_ifn(sctp_ifap->ifn_p);
483 sctp_ifap->ifn_p = NULL;
488 sctp_add_addr_to_vrf(uint32_t vrf_id, void *ifn, uint32_t ifn_index,
489 uint32_t ifn_type, const char *if_name, void *ifa,
490 struct sockaddr *addr, uint32_t ifa_flags,
493 struct sctp_vrf *vrf;
494 struct sctp_ifn *sctp_ifnp = NULL;
495 struct sctp_ifa *sctp_ifap = NULL;
496 struct sctp_ifalist *hash_addr_head;
497 struct sctp_ifnlist *hash_ifn_head;
498 uint32_t hash_of_addr;
502 SCTPDBG(SCTP_DEBUG_PCB4, "vrf_id 0x%x: adding address: ", vrf_id);
503 SCTPDBG_ADDR(SCTP_DEBUG_PCB4, addr);
505 SCTP_IPI_ADDR_WLOCK();
506 sctp_ifnp = sctp_find_ifn(ifn, ifn_index);
508 vrf = sctp_ifnp->vrf;
510 vrf = sctp_find_vrf(vrf_id);
512 vrf = sctp_allocate_vrf(vrf_id);
514 SCTP_IPI_ADDR_WUNLOCK();
519 if (sctp_ifnp == NULL) {
521 * build one and add it, can't hold lock until after malloc
524 SCTP_IPI_ADDR_WUNLOCK();
525 SCTP_MALLOC(sctp_ifnp, struct sctp_ifn *,
526 sizeof(struct sctp_ifn), SCTP_M_IFN);
527 if (sctp_ifnp == NULL) {
529 panic("No memory for IFN:%u", sctp_ifnp->ifn_index);
533 memset(sctp_ifnp, 0, sizeof(struct sctp_ifn));
534 sctp_ifnp->ifn_index = ifn_index;
535 sctp_ifnp->ifn_p = ifn;
536 sctp_ifnp->ifn_type = ifn_type;
537 sctp_ifnp->refcount = 0;
538 sctp_ifnp->vrf = vrf;
539 atomic_add_int(&vrf->refcount, 1);
540 sctp_ifnp->ifn_mtu = SCTP_GATHER_MTU_FROM_IFN_INFO(ifn, ifn_index, addr->sa_family);
541 if (if_name != NULL) {
542 memcpy(sctp_ifnp->ifn_name, if_name, SCTP_IFNAMSIZ);
544 memcpy(sctp_ifnp->ifn_name, "unknown", min(7, SCTP_IFNAMSIZ));
546 hash_ifn_head = &SCTP_BASE_INFO(vrf_ifn_hash)[(ifn_index & SCTP_BASE_INFO(vrf_ifn_hashmark))];
547 LIST_INIT(&sctp_ifnp->ifalist);
548 SCTP_IPI_ADDR_WLOCK();
549 LIST_INSERT_HEAD(hash_ifn_head, sctp_ifnp, next_bucket);
550 LIST_INSERT_HEAD(&vrf->ifnlist, sctp_ifnp, next_ifn);
551 atomic_add_int(&SCTP_BASE_INFO(ipi_count_ifns), 1);
554 sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
556 /* Hmm, it already exists? */
557 if ((sctp_ifap->ifn_p) &&
558 (sctp_ifap->ifn_p->ifn_index == ifn_index)) {
559 SCTPDBG(SCTP_DEBUG_PCB4, "Using existing ifn %s (0x%x) for ifa %p\n",
560 sctp_ifap->ifn_p->ifn_name, ifn_index,
563 /* Remove the created one that we don't want */
564 sctp_delete_ifn(sctp_ifnp, SCTP_ADDR_LOCKED);
566 if (sctp_ifap->localifa_flags & SCTP_BEING_DELETED) {
567 /* easy to solve, just switch back to active */
568 SCTPDBG(SCTP_DEBUG_PCB4, "Clearing deleted ifa flag\n");
569 sctp_ifap->localifa_flags = SCTP_ADDR_VALID;
570 sctp_ifap->ifn_p = sctp_ifnp;
571 atomic_add_int(&sctp_ifap->ifn_p->refcount, 1);
574 SCTP_IPI_ADDR_WUNLOCK();
577 if (sctp_ifap->ifn_p) {
579 * The last IFN gets the address, removee
582 SCTPDBG(SCTP_DEBUG_PCB4, "Moving ifa %p from %s (0x%x) to %s (0x%x)\n",
583 sctp_ifap, sctp_ifap->ifn_p->ifn_name,
584 sctp_ifap->ifn_p->ifn_index, if_name,
586 /* remove the address from the old ifn */
587 sctp_remove_ifa_from_ifn(sctp_ifap);
588 /* move the address over to the new ifn */
589 sctp_add_ifa_to_ifn(sctp_ifnp, sctp_ifap);
590 goto exit_stage_left;
592 /* repair ifnp which was NULL ? */
593 sctp_ifap->localifa_flags = SCTP_ADDR_VALID;
594 SCTPDBG(SCTP_DEBUG_PCB4, "Repairing ifn %p for ifa %p\n",
595 sctp_ifnp, sctp_ifap);
596 sctp_add_ifa_to_ifn(sctp_ifnp, sctp_ifap);
598 goto exit_stage_left;
601 SCTP_IPI_ADDR_WUNLOCK();
602 SCTP_MALLOC(sctp_ifap, struct sctp_ifa *, sizeof(struct sctp_ifa), SCTP_M_IFA);
603 if (sctp_ifap == NULL) {
605 panic("No memory for IFA");
609 memset(sctp_ifap, 0, sizeof(struct sctp_ifa));
610 sctp_ifap->ifn_p = sctp_ifnp;
611 atomic_add_int(&sctp_ifnp->refcount, 1);
612 sctp_ifap->vrf_id = vrf_id;
613 sctp_ifap->ifa = ifa;
614 memcpy(&sctp_ifap->address, addr, addr->sa_len);
615 sctp_ifap->localifa_flags = SCTP_ADDR_VALID | SCTP_ADDR_DEFER_USE;
616 sctp_ifap->flags = ifa_flags;
618 switch (sctp_ifap->address.sa.sa_family) {
621 struct sockaddr_in *sin;
623 sin = (struct sockaddr_in *)&sctp_ifap->address.sin;
624 if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) ||
625 (IN4_ISLOOPBACK_ADDRESS(&sin->sin_addr))) {
626 sctp_ifap->src_is_loop = 1;
628 if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
629 sctp_ifap->src_is_priv = 1;
633 new_ifn_af = AF_INET;
639 /* ok to use deprecated addresses? */
640 struct sockaddr_in6 *sin6;
642 sin6 = (struct sockaddr_in6 *)&sctp_ifap->address.sin6;
643 if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) ||
644 (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))) {
645 sctp_ifap->src_is_loop = 1;
647 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
648 sctp_ifap->src_is_priv = 1;
652 new_ifn_af = AF_INET6;
660 hash_of_addr = sctp_get_ifa_hash_val(&sctp_ifap->address.sa);
662 if ((sctp_ifap->src_is_priv == 0) &&
663 (sctp_ifap->src_is_loop == 0)) {
664 sctp_ifap->src_is_glob = 1;
666 SCTP_IPI_ADDR_WLOCK();
667 hash_addr_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)];
668 LIST_INSERT_HEAD(hash_addr_head, sctp_ifap, next_bucket);
669 sctp_ifap->refcount = 1;
670 LIST_INSERT_HEAD(&sctp_ifnp->ifalist, sctp_ifap, next_ifa);
671 sctp_ifnp->ifa_count++;
672 vrf->total_ifa_count++;
673 atomic_add_int(&SCTP_BASE_INFO(ipi_count_ifas), 1);
675 SCTP_REGISTER_INTERFACE(ifn_index, new_ifn_af);
676 sctp_ifnp->registered_af = new_ifn_af;
678 SCTP_IPI_ADDR_WUNLOCK();
681 * Bump up the refcount so that when the timer completes it
682 * will drop back down.
684 struct sctp_laddr *wi;
686 atomic_add_int(&sctp_ifap->refcount, 1);
687 wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
690 * Gak, what can we do? We have lost an address
691 * change can you say HOSED?
693 SCTPDBG(SCTP_DEBUG_PCB4, "Lost an address change?\n");
694 /* Opps, must decrement the count */
695 sctp_del_addr_from_vrf(vrf_id, addr, ifn_index,
699 SCTP_INCR_LADDR_COUNT();
700 bzero(wi, sizeof(*wi));
701 (void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
703 wi->action = SCTP_ADD_IP_ADDRESS;
704 SCTP_IPI_ITERATOR_WQ_LOCK();
706 * Should this really be a tailq? As it is we will process
707 * the newest first :-0
709 LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
710 SCTP_IPI_ITERATOR_WQ_UNLOCK();
711 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
712 (struct sctp_inpcb *)NULL,
713 (struct sctp_tcb *)NULL,
714 (struct sctp_nets *)NULL);
716 /* it's ready for use */
717 sctp_ifap->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
723 sctp_del_addr_from_vrf(uint32_t vrf_id, struct sockaddr *addr,
724 uint32_t ifn_index, const char *if_name)
726 struct sctp_vrf *vrf;
727 struct sctp_ifa *sctp_ifap = NULL;
729 SCTP_IPI_ADDR_WLOCK();
730 vrf = sctp_find_vrf(vrf_id);
732 SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id);
736 SCTPDBG(SCTP_DEBUG_PCB4, "vrf_id 0x%x: deleting address:", vrf_id);
737 SCTPDBG_ADDR(SCTP_DEBUG_PCB4, addr);
739 sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
741 /* Validate the delete */
742 if (sctp_ifap->ifn_p) {
746 * The name has priority over the ifn_index
747 * if its given. We do this especially for
748 * panda who might recycle indexes fast.
753 len1 = min(SCTP_IFNAMSIZ, strlen(if_name));
754 len2 = min(SCTP_IFNAMSIZ, strlen(sctp_ifap->ifn_p->ifn_name));
755 if (len1 && len2 && (len1 == len2)) {
756 /* we can compare them */
757 if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, len1) == 0) {
759 * They match its a correct
767 /* last ditch check ifn_index */
768 if (ifn_index == sctp_ifap->ifn_p->ifn_index) {
773 SCTPDBG(SCTP_DEBUG_PCB4, "ifn:%d ifname:%s does not match addresses\n",
774 ifn_index, ((if_name == NULL) ? "NULL" : if_name));
775 SCTPDBG(SCTP_DEBUG_PCB4, "ifn:%d ifname:%s - ignoring delete\n",
776 sctp_ifap->ifn_p->ifn_index, sctp_ifap->ifn_p->ifn_name);
777 SCTP_IPI_ADDR_WUNLOCK();
781 SCTPDBG(SCTP_DEBUG_PCB4, "Deleting ifa %p\n", sctp_ifap);
782 sctp_ifap->localifa_flags &= SCTP_ADDR_VALID;
783 sctp_ifap->localifa_flags |= SCTP_BEING_DELETED;
784 vrf->total_ifa_count--;
785 LIST_REMOVE(sctp_ifap, next_bucket);
786 sctp_remove_ifa_from_ifn(sctp_ifap);
790 SCTPDBG(SCTP_DEBUG_PCB4, "Del Addr-ifn:%d Could not find address:",
792 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr);
797 SCTP_IPI_ADDR_WUNLOCK();
799 struct sctp_laddr *wi;
801 wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
804 * Gak, what can we do? We have lost an address
805 * change can you say HOSED?
807 SCTPDBG(SCTP_DEBUG_PCB4, "Lost an address change?\n");
809 /* Oops, must decrement the count */
810 sctp_free_ifa(sctp_ifap);
813 SCTP_INCR_LADDR_COUNT();
814 bzero(wi, sizeof(*wi));
815 (void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
817 wi->action = SCTP_DEL_IP_ADDRESS;
818 SCTP_IPI_ITERATOR_WQ_LOCK();
820 * Should this really be a tailq? As it is we will process
821 * the newest first :-0
823 LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
824 SCTP_IPI_ITERATOR_WQ_UNLOCK();
826 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
827 (struct sctp_inpcb *)NULL,
828 (struct sctp_tcb *)NULL,
829 (struct sctp_nets *)NULL);
835 static struct sctp_tcb *
836 sctp_tcb_special_locate(struct sctp_inpcb **inp_p, struct sockaddr *from,
837 struct sockaddr *to, struct sctp_nets **netp, uint32_t vrf_id)
839 /**** ASSUMES THE CALLER holds the INP_INFO_RLOCK */
841 * If we support the TCP model, then we must now dig through to see
842 * if we can find our endpoint in the list of tcp ep's.
844 uint16_t lport, rport;
845 struct sctppcbhead *ephead;
846 struct sctp_inpcb *inp;
847 struct sctp_laddr *laddr;
848 struct sctp_tcb *stcb;
849 struct sctp_nets *net;
851 if ((to == NULL) || (from == NULL)) {
854 if (to->sa_family == AF_INET && from->sa_family == AF_INET) {
855 lport = ((struct sockaddr_in *)to)->sin_port;
856 rport = ((struct sockaddr_in *)from)->sin_port;
857 } else if (to->sa_family == AF_INET6 && from->sa_family == AF_INET6) {
858 lport = ((struct sockaddr_in6 *)to)->sin6_port;
859 rport = ((struct sockaddr_in6 *)from)->sin6_port;
863 ephead = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR((lport), SCTP_BASE_INFO(hashtcpmark))];
865 * Ok now for each of the guys in this bucket we must look and see:
866 * - Does the remote port match. - Does there single association's
867 * addresses match this address (to). If so we update p_ep to point
868 * to this ep and return the tcb from it.
870 LIST_FOREACH(inp, ephead, sctp_hash) {
872 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
873 SCTP_INP_RUNLOCK(inp);
876 if (lport != inp->sctp_lport) {
877 SCTP_INP_RUNLOCK(inp);
880 if (inp->def_vrf_id != vrf_id) {
881 SCTP_INP_RUNLOCK(inp);
884 /* check to see if the ep has one of the addresses */
885 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
886 /* We are NOT bound all, so look further */
889 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
891 if (laddr->ifa == NULL) {
892 SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n", __FUNCTION__);
895 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
896 SCTPDBG(SCTP_DEBUG_PCB1, "ifa being deleted\n");
899 if (laddr->ifa->address.sa.sa_family ==
901 /* see if it matches */
902 struct sockaddr_in *intf_addr, *sin;
904 intf_addr = &laddr->ifa->address.sin;
905 sin = (struct sockaddr_in *)to;
906 if (from->sa_family == AF_INET) {
907 if (sin->sin_addr.s_addr ==
908 intf_addr->sin_addr.s_addr) {
914 if (from->sa_family == AF_INET6) {
915 struct sockaddr_in6 *intf_addr6;
916 struct sockaddr_in6 *sin6;
918 sin6 = (struct sockaddr_in6 *)
920 intf_addr6 = &laddr->ifa->address.sin6;
922 if (SCTP6_ARE_ADDR_EQUAL(sin6,
932 /* This endpoint does not have this address */
933 SCTP_INP_RUNLOCK(inp);
938 * Ok if we hit here the ep has the address, does it hold
942 stcb = LIST_FIRST(&inp->sctp_asoc_list);
944 SCTP_INP_RUNLOCK(inp);
948 if (stcb->rport != rport) {
949 /* remote port does not match. */
950 SCTP_TCB_UNLOCK(stcb);
951 SCTP_INP_RUNLOCK(inp);
954 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
955 SCTP_TCB_UNLOCK(stcb);
956 SCTP_INP_RUNLOCK(inp);
959 /* Does this TCB have a matching address? */
960 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
962 if (net->ro._l_addr.sa.sa_family != from->sa_family) {
963 /* not the same family, can't be a match */
966 switch (from->sa_family) {
969 struct sockaddr_in *sin, *rsin;
971 sin = (struct sockaddr_in *)&net->ro._l_addr;
972 rsin = (struct sockaddr_in *)from;
973 if (sin->sin_addr.s_addr ==
974 rsin->sin_addr.s_addr) {
980 * Update the endpoint
984 SCTP_INP_RUNLOCK(inp);
992 struct sockaddr_in6 *sin6, *rsin6;
994 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
995 rsin6 = (struct sockaddr_in6 *)from;
996 if (SCTP6_ARE_ADDR_EQUAL(sin6,
1003 * Update the endpoint
1007 SCTP_INP_RUNLOCK(inp);
1018 SCTP_TCB_UNLOCK(stcb);
1019 SCTP_INP_RUNLOCK(inp);
1027 * 1) If I return a NULL you must decrement any INP ref cnt. 2) If I find an
1028 * stcb, both will be locked (locked_tcb and stcb) but decrement will be done
1029 * (if locked == NULL). 3) Decrement happens on return ONLY if locked ==
1034 sctp_findassociation_ep_addr(struct sctp_inpcb **inp_p, struct sockaddr *remote,
1035 struct sctp_nets **netp, struct sockaddr *local, struct sctp_tcb *locked_tcb)
1037 struct sctpasochead *head;
1038 struct sctp_inpcb *inp;
1039 struct sctp_tcb *stcb = NULL;
1040 struct sctp_nets *net;
1044 if (remote->sa_family == AF_INET) {
1045 rport = (((struct sockaddr_in *)remote)->sin_port);
1046 } else if (remote->sa_family == AF_INET6) {
1047 rport = (((struct sockaddr_in6 *)remote)->sin6_port);
1053 * UN-lock so we can do proper locking here this occurs when
1054 * called from load_addresses_from_init.
1056 atomic_add_int(&locked_tcb->asoc.refcnt, 1);
1057 SCTP_TCB_UNLOCK(locked_tcb);
1059 SCTP_INP_INFO_RLOCK();
1060 if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1062 * Now either this guy is our listener or it's the
1063 * connector. If it is the one that issued the connect, then
1064 * it's only chance is to be the first TCB in the list. If
1065 * it is the acceptor, then do the special_lookup to hash
1066 * and find the real inp.
1068 if ((inp->sctp_socket) && (inp->sctp_socket->so_qlimit)) {
1069 /* to is peer addr, from is my addr */
1070 stcb = sctp_tcb_special_locate(inp_p, remote, local,
1071 netp, inp->def_vrf_id);
1072 if ((stcb != NULL) && (locked_tcb == NULL)) {
1073 /* we have a locked tcb, lower refcount */
1074 SCTP_INP_DECR_REF(inp);
1076 if ((locked_tcb != NULL) && (locked_tcb != stcb)) {
1077 SCTP_INP_RLOCK(locked_tcb->sctp_ep);
1078 SCTP_TCB_LOCK(locked_tcb);
1079 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1080 SCTP_INP_RUNLOCK(locked_tcb->sctp_ep);
1082 SCTP_INP_INFO_RUNLOCK();
1085 SCTP_INP_WLOCK(inp);
1086 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1089 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1093 SCTP_TCB_LOCK(stcb);
1095 if (stcb->rport != rport) {
1096 /* remote port does not match. */
1097 SCTP_TCB_UNLOCK(stcb);
1100 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
1101 SCTP_TCB_UNLOCK(stcb);
1104 /* now look at the list of remote addresses */
1105 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1107 if (net == (TAILQ_NEXT(net, sctp_next))) {
1108 panic("Corrupt net list");
1111 if (net->ro._l_addr.sa.sa_family !=
1112 remote->sa_family) {
1113 /* not the same family */
1116 switch (remote->sa_family) {
1119 struct sockaddr_in *sin,
1122 sin = (struct sockaddr_in *)
1124 rsin = (struct sockaddr_in *)remote;
1125 if (sin->sin_addr.s_addr ==
1126 rsin->sin_addr.s_addr) {
1131 if (locked_tcb == NULL) {
1132 SCTP_INP_DECR_REF(inp);
1133 } else if (locked_tcb != stcb) {
1134 SCTP_TCB_LOCK(locked_tcb);
1137 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1139 SCTP_INP_WUNLOCK(inp);
1140 SCTP_INP_INFO_RUNLOCK();
1148 struct sockaddr_in6 *sin6,
1151 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1152 rsin6 = (struct sockaddr_in6 *)remote;
1153 if (SCTP6_ARE_ADDR_EQUAL(sin6,
1159 if (locked_tcb == NULL) {
1160 SCTP_INP_DECR_REF(inp);
1161 } else if (locked_tcb != stcb) {
1162 SCTP_TCB_LOCK(locked_tcb);
1165 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1167 SCTP_INP_WUNLOCK(inp);
1168 SCTP_INP_INFO_RUNLOCK();
1179 SCTP_TCB_UNLOCK(stcb);
1182 SCTP_INP_WLOCK(inp);
1183 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1186 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(rport,
1187 inp->sctp_hashmark)];
1191 LIST_FOREACH(stcb, head, sctp_tcbhash) {
1192 if (stcb->rport != rport) {
1193 /* remote port does not match */
1196 SCTP_TCB_LOCK(stcb);
1197 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
1198 SCTP_TCB_UNLOCK(stcb);
1201 /* now look at the list of remote addresses */
1202 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1204 if (net == (TAILQ_NEXT(net, sctp_next))) {
1205 panic("Corrupt net list");
1208 if (net->ro._l_addr.sa.sa_family !=
1209 remote->sa_family) {
1210 /* not the same family */
1213 switch (remote->sa_family) {
1216 struct sockaddr_in *sin,
1219 sin = (struct sockaddr_in *)
1221 rsin = (struct sockaddr_in *)remote;
1222 if (sin->sin_addr.s_addr ==
1223 rsin->sin_addr.s_addr) {
1228 if (locked_tcb == NULL) {
1229 SCTP_INP_DECR_REF(inp);
1230 } else if (locked_tcb != stcb) {
1231 SCTP_TCB_LOCK(locked_tcb);
1234 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1236 SCTP_INP_WUNLOCK(inp);
1237 SCTP_INP_INFO_RUNLOCK();
1245 struct sockaddr_in6 *sin6,
1248 sin6 = (struct sockaddr_in6 *)
1250 rsin6 = (struct sockaddr_in6 *)remote;
1251 if (SCTP6_ARE_ADDR_EQUAL(sin6,
1257 if (locked_tcb == NULL) {
1258 SCTP_INP_DECR_REF(inp);
1259 } else if (locked_tcb != stcb) {
1260 SCTP_TCB_LOCK(locked_tcb);
1263 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1265 SCTP_INP_WUNLOCK(inp);
1266 SCTP_INP_INFO_RUNLOCK();
1277 SCTP_TCB_UNLOCK(stcb);
1281 /* clean up for returning null */
1283 SCTP_TCB_LOCK(locked_tcb);
1284 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1286 SCTP_INP_WUNLOCK(inp);
1287 SCTP_INP_INFO_RUNLOCK();
1293 * Find an association for a specific endpoint using the association id given
1294 * out in the COMM_UP notification
1298 sctp_findassociation_ep_asocid(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock)
1301 * Use my the assoc_id to find a endpoint
1303 struct sctpasochead *head;
1304 struct sctp_tcb *stcb;
1307 if (asoc_id == 0 || inp == NULL) {
1310 SCTP_INP_INFO_RLOCK();
1311 id = (uint32_t) asoc_id;
1312 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(id,
1313 SCTP_BASE_INFO(hashasocmark))];
1315 /* invalid id TSNH */
1316 SCTP_INP_INFO_RUNLOCK();
1319 LIST_FOREACH(stcb, head, sctp_asocs) {
1320 SCTP_INP_RLOCK(stcb->sctp_ep);
1321 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1322 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1323 SCTP_INP_INFO_RUNLOCK();
1326 if (stcb->asoc.assoc_id == id) {
1328 if (inp != stcb->sctp_ep) {
1330 * some other guy has the same id active (id
1333 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1336 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
1340 SCTP_TCB_LOCK(stcb);
1342 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1343 SCTP_INP_INFO_RUNLOCK();
1346 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1348 /* Ok if we missed here, lets try the restart hash */
1349 head = &SCTP_BASE_INFO(sctp_restarthash)[SCTP_PCBHASH_ASOC(id, SCTP_BASE_INFO(hashrestartmark))];
1351 /* invalid id TSNH */
1352 SCTP_INP_INFO_RUNLOCK();
1355 LIST_FOREACH(stcb, head, sctp_tcbrestarhash) {
1356 SCTP_INP_RLOCK(stcb->sctp_ep);
1357 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1358 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1361 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
1365 SCTP_TCB_LOCK(stcb);
1367 if (stcb->asoc.assoc_id == id) {
1369 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1370 if (inp != stcb->sctp_ep) {
1372 * some other guy has the same id active (id
1376 SCTP_TCB_UNLOCK(stcb);
1380 SCTP_INP_INFO_RUNLOCK();
1383 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1386 SCTP_TCB_UNLOCK(stcb);
1389 SCTP_INP_INFO_RUNLOCK();
1394 static struct sctp_inpcb *
1395 sctp_endpoint_probe(struct sockaddr *nam, struct sctppcbhead *head,
1396 uint16_t lport, uint32_t vrf_id)
1398 struct sctp_inpcb *inp;
1399 struct sockaddr_in *sin;
1402 struct sockaddr_in6 *sin6;
1405 struct sctp_laddr *laddr;
1408 struct sockaddr_in6 *intf_addr6;
1415 * Endpoing probe expects that the INP_INFO is locked.
1421 switch (nam->sa_family) {
1423 sin = (struct sockaddr_in *)nam;
1427 sin6 = (struct sockaddr_in6 *)nam;
1431 /* unsupported family */
1437 LIST_FOREACH(inp, head, sctp_hash) {
1438 SCTP_INP_RLOCK(inp);
1439 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1440 SCTP_INP_RUNLOCK(inp);
1443 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) &&
1444 (inp->sctp_lport == lport)) {
1446 if ((nam->sa_family == AF_INET) &&
1447 (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1448 SCTP_IPV6_V6ONLY(inp)) {
1449 /* IPv4 on a IPv6 socket with ONLY IPv6 set */
1450 SCTP_INP_RUNLOCK(inp);
1453 /* A V6 address and the endpoint is NOT bound V6 */
1454 if (nam->sa_family == AF_INET6 &&
1455 (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
1456 SCTP_INP_RUNLOCK(inp);
1459 /* does a VRF id match? */
1461 if (inp->def_vrf_id == vrf_id)
1464 SCTP_INP_RUNLOCK(inp);
1469 SCTP_INP_RUNLOCK(inp);
1471 if ((nam->sa_family == AF_INET) &&
1472 (sin->sin_addr.s_addr == INADDR_ANY)) {
1473 /* Can't hunt for one that has no address specified */
1477 if ((nam->sa_family == AF_INET6) &&
1478 (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))) {
1479 /* Can't hunt for one that has no address specified */
1484 * ok, not bound to all so see if we can find a EP bound to this
1487 LIST_FOREACH(inp, head, sctp_hash) {
1488 SCTP_INP_RLOCK(inp);
1489 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1490 SCTP_INP_RUNLOCK(inp);
1493 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)) {
1494 SCTP_INP_RUNLOCK(inp);
1498 * Ok this could be a likely candidate, look at all of its
1501 if (inp->sctp_lport != lport) {
1502 SCTP_INP_RUNLOCK(inp);
1505 /* does a VRF id match? */
1507 if (inp->def_vrf_id == vrf_id)
1511 SCTP_INP_RUNLOCK(inp);
1514 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1515 if (laddr->ifa == NULL) {
1516 SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n",
1520 SCTPDBG(SCTP_DEBUG_PCB1, "Ok laddr->ifa:%p is possible, ",
1522 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
1523 SCTPDBG(SCTP_DEBUG_PCB1, "Huh IFA being deleted\n");
1526 if (laddr->ifa->address.sa.sa_family == nam->sa_family) {
1527 /* possible, see if it matches */
1528 struct sockaddr_in *intf_addr;
1530 intf_addr = &laddr->ifa->address.sin;
1531 switch (nam->sa_family) {
1533 if (sin->sin_addr.s_addr ==
1534 intf_addr->sin_addr.s_addr) {
1535 SCTP_INP_RUNLOCK(inp);
1541 intf_addr6 = &laddr->ifa->address.sin6;
1542 if (SCTP6_ARE_ADDR_EQUAL(sin6,
1544 SCTP_INP_RUNLOCK(inp);
1552 SCTP_INP_RUNLOCK(inp);
1558 static struct sctp_inpcb *
1559 sctp_isport_inuse(struct sctp_inpcb *inp, uint16_t lport, uint32_t vrf_id)
1561 struct sctppcbhead *head;
1562 struct sctp_inpcb *t_inp;
1565 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport,
1566 SCTP_BASE_INFO(hashmark))];
1567 LIST_FOREACH(t_inp, head, sctp_hash) {
1568 if (t_inp->sctp_lport != lport) {
1571 /* is it in the VRF in question */
1573 if (t_inp->def_vrf_id == vrf_id)
1578 /* This one is in use. */
1579 /* check the v6/v4 binding issue */
1580 if ((t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1581 SCTP_IPV6_V6ONLY(t_inp)) {
1582 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1583 /* collision in V6 space */
1586 /* inp is BOUND_V4 no conflict */
1589 } else if (t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1590 /* t_inp is bound v4 and v6, conflict always */
1593 /* t_inp is bound only V4 */
1594 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1595 SCTP_IPV6_V6ONLY(inp)) {
1599 /* else fall through to conflict */
1608 sctp_swap_inpcb_for_listen(struct sctp_inpcb *inp)
1610 /* For 1-2-1 with port reuse */
1611 struct sctppcbhead *head;
1612 struct sctp_inpcb *tinp;
1614 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) {
1615 /* only works with port reuse on */
1618 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) {
1621 SCTP_INP_RUNLOCK(inp);
1622 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(inp->sctp_lport,
1623 SCTP_BASE_INFO(hashmark))];
1624 /* Kick out all non-listeners to the TCP hash */
1625 LIST_FOREACH(tinp, head, sctp_hash) {
1626 if (tinp->sctp_lport != inp->sctp_lport) {
1629 if (tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1632 if (tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
1635 if (tinp->sctp_socket->so_qlimit) {
1638 SCTP_INP_WLOCK(tinp);
1639 LIST_REMOVE(tinp, sctp_hash);
1640 head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR(tinp->sctp_lport, SCTP_BASE_INFO(hashtcpmark))];
1641 tinp->sctp_flags |= SCTP_PCB_FLAGS_IN_TCPPOOL;
1642 LIST_INSERT_HEAD(head, tinp, sctp_hash);
1643 SCTP_INP_WUNLOCK(tinp);
1645 SCTP_INP_WLOCK(inp);
1646 /* Pull from where he was */
1647 LIST_REMOVE(inp, sctp_hash);
1648 inp->sctp_flags &= ~SCTP_PCB_FLAGS_IN_TCPPOOL;
1649 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(inp->sctp_lport, SCTP_BASE_INFO(hashmark))];
1650 LIST_INSERT_HEAD(head, inp, sctp_hash);
1651 SCTP_INP_WUNLOCK(inp);
1652 SCTP_INP_RLOCK(inp);
1658 sctp_pcb_findep(struct sockaddr *nam, int find_tcp_pool, int have_lock,
1662 * First we check the hash table to see if someone has this port
1663 * bound with just the port.
1665 struct sctp_inpcb *inp;
1666 struct sctppcbhead *head;
1667 struct sockaddr_in *sin;
1668 struct sockaddr_in6 *sin6;
1671 if (nam->sa_family == AF_INET) {
1672 sin = (struct sockaddr_in *)nam;
1673 lport = ((struct sockaddr_in *)nam)->sin_port;
1674 } else if (nam->sa_family == AF_INET6) {
1675 sin6 = (struct sockaddr_in6 *)nam;
1676 lport = ((struct sockaddr_in6 *)nam)->sin6_port;
1678 /* unsupported family */
1682 * I could cheat here and just cast to one of the types but we will
1683 * do it right. It also provides the check against an Unsupported
1686 /* Find the head of the ALLADDR chain */
1687 if (have_lock == 0) {
1688 SCTP_INP_INFO_RLOCK();
1690 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport,
1691 SCTP_BASE_INFO(hashmark))];
1692 inp = sctp_endpoint_probe(nam, head, lport, vrf_id);
1695 * If the TCP model exists it could be that the main listening
1696 * endpoint is gone but there exists a connected socket for this guy
1697 * yet. If so we can return the first one that we find. This may NOT
1698 * be the correct one but the sctp_findassociation_ep_addr has
1699 * further code to look at all TCP models.
1701 if (inp == NULL && find_tcp_pool) {
1702 head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashtcpmark))];
1703 inp = sctp_endpoint_probe(nam, head, lport, vrf_id);
1706 SCTP_INP_INCR_REF(inp);
1708 if (have_lock == 0) {
1709 SCTP_INP_INFO_RUNLOCK();
1715 * Find an association for an endpoint with the pointer to whom you want to
1716 * send to and the endpoint pointer. The address can be IPv4 or IPv6. We may
1717 * need to change the *to to some other struct like a mbuf...
1720 sctp_findassociation_addr_sa(struct sockaddr *to, struct sockaddr *from,
1721 struct sctp_inpcb **inp_p, struct sctp_nets **netp, int find_tcp_pool,
1724 struct sctp_inpcb *inp = NULL;
1725 struct sctp_tcb *retval;
1727 SCTP_INP_INFO_RLOCK();
1728 if (find_tcp_pool) {
1729 if (inp_p != NULL) {
1730 retval = sctp_tcb_special_locate(inp_p, from, to, netp,
1733 retval = sctp_tcb_special_locate(&inp, from, to, netp,
1736 if (retval != NULL) {
1737 SCTP_INP_INFO_RUNLOCK();
1741 inp = sctp_pcb_findep(to, 0, 1, vrf_id);
1742 if (inp_p != NULL) {
1745 SCTP_INP_INFO_RUNLOCK();
1751 * ok, we have an endpoint, now lets find the assoc for it (if any)
1752 * we now place the source address or from in the to of the find
1753 * endpoint call. Since in reality this chain is used from the
1754 * inbound packet side.
1756 if (inp_p != NULL) {
1757 retval = sctp_findassociation_ep_addr(inp_p, from, netp, to,
1760 retval = sctp_findassociation_ep_addr(&inp, from, netp, to,
1768 * This routine will grub through the mbuf that is a INIT or INIT-ACK and
1769 * find all addresses that the sender has specified in any address list. Each
1770 * address will be used to lookup the TCB and see if one exits.
1772 static struct sctp_tcb *
1773 sctp_findassociation_special_addr(struct mbuf *m, int iphlen, int offset,
1774 struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp,
1775 struct sockaddr *dest)
1777 struct sockaddr_in sin4;
1778 struct sockaddr_in6 sin6;
1779 struct sctp_paramhdr *phdr, parm_buf;
1780 struct sctp_tcb *retval;
1781 uint32_t ptype, plen;
1783 memset(&sin4, 0, sizeof(sin4));
1784 memset(&sin6, 0, sizeof(sin6));
1785 sin4.sin_len = sizeof(sin4);
1786 sin4.sin_family = AF_INET;
1787 sin4.sin_port = sh->src_port;
1788 sin6.sin6_len = sizeof(sin6);
1789 sin6.sin6_family = AF_INET6;
1790 sin6.sin6_port = sh->src_port;
1793 offset += sizeof(struct sctp_init_chunk);
1795 phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
1796 while (phdr != NULL) {
1797 /* now we must see if we want the parameter */
1798 ptype = ntohs(phdr->param_type);
1799 plen = ntohs(phdr->param_length);
1803 if (ptype == SCTP_IPV4_ADDRESS &&
1804 plen == sizeof(struct sctp_ipv4addr_param)) {
1805 /* Get the rest of the address */
1806 struct sctp_ipv4addr_param ip4_parm, *p4;
1808 phdr = sctp_get_next_param(m, offset,
1809 (struct sctp_paramhdr *)&ip4_parm, min(plen, sizeof(ip4_parm)));
1813 p4 = (struct sctp_ipv4addr_param *)phdr;
1814 memcpy(&sin4.sin_addr, &p4->addr, sizeof(p4->addr));
1816 retval = sctp_findassociation_ep_addr(inp_p,
1817 (struct sockaddr *)&sin4, netp, dest, NULL);
1818 if (retval != NULL) {
1821 } else if (ptype == SCTP_IPV6_ADDRESS &&
1822 plen == sizeof(struct sctp_ipv6addr_param)) {
1823 /* Get the rest of the address */
1824 struct sctp_ipv6addr_param ip6_parm, *p6;
1826 phdr = sctp_get_next_param(m, offset,
1827 (struct sctp_paramhdr *)&ip6_parm, min(plen, sizeof(ip6_parm)));
1831 p6 = (struct sctp_ipv6addr_param *)phdr;
1832 memcpy(&sin6.sin6_addr, &p6->addr, sizeof(p6->addr));
1834 retval = sctp_findassociation_ep_addr(inp_p,
1835 (struct sockaddr *)&sin6, netp, dest, NULL);
1836 if (retval != NULL) {
1840 offset += SCTP_SIZE32(plen);
1841 phdr = sctp_get_next_param(m, offset, &parm_buf,
1848 static struct sctp_tcb *
1849 sctp_findassoc_by_vtag(struct sockaddr *from, uint32_t vtag,
1850 struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint16_t rport,
1851 uint16_t lport, int skip_src_check)
1854 * Use my vtag to hash. If we find it we then verify the source addr
1855 * is in the assoc. If all goes well we save a bit on rec of a
1858 struct sctpasochead *head;
1859 struct sctp_nets *net;
1860 struct sctp_tcb *stcb;
1864 SCTP_INP_INFO_RLOCK();
1865 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(vtag,
1866 SCTP_BASE_INFO(hashasocmark))];
1869 SCTP_INP_INFO_RUNLOCK();
1872 LIST_FOREACH(stcb, head, sctp_asocs) {
1873 SCTP_INP_RLOCK(stcb->sctp_ep);
1874 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1875 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1878 SCTP_TCB_LOCK(stcb);
1879 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1880 if (stcb->asoc.my_vtag == vtag) {
1882 if (stcb->rport != rport) {
1884 * we could remove this if vtags are unique
1885 * across the system.
1887 SCTP_TCB_UNLOCK(stcb);
1890 if (stcb->sctp_ep->sctp_lport != lport) {
1892 * we could remove this if vtags are unique
1893 * across the system.
1895 SCTP_TCB_UNLOCK(stcb);
1898 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
1899 SCTP_TCB_UNLOCK(stcb);
1902 if (skip_src_check) {
1903 *netp = NULL; /* unknown */
1905 *inp_p = stcb->sctp_ep;
1906 SCTP_INP_INFO_RUNLOCK();
1909 net = sctp_findnet(stcb, from);
1913 SCTP_STAT_INCR(sctps_vtagexpress);
1914 *inp_p = stcb->sctp_ep;
1915 SCTP_INP_INFO_RUNLOCK();
1919 * not him, this should only happen in rare
1920 * cases so I peg it.
1922 SCTP_STAT_INCR(sctps_vtagbogus);
1925 SCTP_TCB_UNLOCK(stcb);
1927 SCTP_INP_INFO_RUNLOCK();
1932 * Find an association with the pointer to the inbound IP packet. This can be
1933 * a IPv4 or IPv6 packet.
1936 sctp_findassociation_addr(struct mbuf *m, int iphlen, int offset,
1937 struct sctphdr *sh, struct sctp_chunkhdr *ch,
1938 struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id)
1942 struct sctp_tcb *retval;
1943 struct sockaddr_storage to_store, from_store;
1944 struct sockaddr *to = (struct sockaddr *)&to_store;
1945 struct sockaddr *from = (struct sockaddr *)&from_store;
1946 struct sctp_inpcb *inp;
1948 iph = mtod(m, struct ip *);
1949 switch (iph->ip_v) {
1953 struct sockaddr_in *from4;
1955 from4 = (struct sockaddr_in *)&from_store;
1956 bzero(from4, sizeof(*from4));
1957 from4->sin_family = AF_INET;
1958 from4->sin_len = sizeof(struct sockaddr_in);
1959 from4->sin_addr.s_addr = iph->ip_src.s_addr;
1960 from4->sin_port = sh->src_port;
1964 case IPV6_VERSION >> 4:
1967 struct ip6_hdr *ip6;
1968 struct sockaddr_in6 *from6;
1970 ip6 = mtod(m, struct ip6_hdr *);
1971 from6 = (struct sockaddr_in6 *)&from_store;
1972 bzero(from6, sizeof(*from6));
1973 from6->sin6_family = AF_INET6;
1974 from6->sin6_len = sizeof(struct sockaddr_in6);
1975 from6->sin6_addr = ip6->ip6_src;
1976 from6->sin6_port = sh->src_port;
1977 /* Get the scopes in properly to the sin6 addr's */
1978 /* we probably don't need these operations */
1979 (void)sa6_recoverscope(from6);
1980 sa6_embedscope(from6, MODULE_GLOBAL(MOD_INET6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone)));
1985 /* Currently not supported. */
1989 /* we only go down this path if vtag is non-zero */
1990 retval = sctp_findassoc_by_vtag(from, ntohl(sh->v_tag),
1991 inp_p, netp, sh->src_port, sh->dest_port, 0);
1996 switch (iph->ip_v) {
2000 struct sockaddr_in *to4;
2002 to4 = (struct sockaddr_in *)&to_store;
2003 bzero(to4, sizeof(*to4));
2004 to4->sin_family = AF_INET;
2005 to4->sin_len = sizeof(struct sockaddr_in);
2006 to4->sin_addr.s_addr = iph->ip_dst.s_addr;
2007 to4->sin_port = sh->dest_port;
2011 case IPV6_VERSION >> 4:
2014 struct ip6_hdr *ip6;
2015 struct sockaddr_in6 *to6;
2017 ip6 = mtod(m, struct ip6_hdr *);
2018 to6 = (struct sockaddr_in6 *)&to_store;
2019 bzero(to6, sizeof(*to6));
2020 to6->sin6_family = AF_INET6;
2021 to6->sin6_len = sizeof(struct sockaddr_in6);
2022 to6->sin6_addr = ip6->ip6_dst;
2023 to6->sin6_port = sh->dest_port;
2024 /* Get the scopes in properly to the sin6 addr's */
2025 /* we probably don't need these operations */
2026 (void)sa6_recoverscope(to6);
2027 sa6_embedscope(to6, MODULE_GLOBAL(MOD_INET6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone)));
2036 if ((ch->chunk_type != SCTP_INITIATION) &&
2037 (ch->chunk_type != SCTP_INITIATION_ACK) &&
2038 (ch->chunk_type != SCTP_COOKIE_ACK) &&
2039 (ch->chunk_type != SCTP_COOKIE_ECHO)) {
2040 /* Other chunk types go to the tcp pool. */
2044 retval = sctp_findassociation_addr_sa(to, from, inp_p, netp,
2045 find_tcp_pool, vrf_id);
2048 retval = sctp_findassociation_addr_sa(to, from, &inp, netp,
2049 find_tcp_pool, vrf_id);
2051 SCTPDBG(SCTP_DEBUG_PCB1, "retval:%p inp:%p\n", retval, inp);
2052 if (retval == NULL && inp) {
2053 /* Found a EP but not this address */
2054 if ((ch->chunk_type == SCTP_INITIATION) ||
2055 (ch->chunk_type == SCTP_INITIATION_ACK)) {
2057 * special hook, we do NOT return linp or an
2058 * association that is linked to an existing
2059 * association that is under the TCP pool (i.e. no
2060 * listener exists). The endpoint finding routine
2061 * will always find a listner before examining the
2064 if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
2070 retval = sctp_findassociation_special_addr(m, iphlen,
2071 offset, sh, &inp, netp, to);
2072 if (inp_p != NULL) {
2077 SCTPDBG(SCTP_DEBUG_PCB1, "retval is %p\n", retval);
2082 * lookup an association by an ASCONF lookup address.
2083 * if the lookup address is 0.0.0.0 or ::0, use the vtag to do the lookup
2086 sctp_findassociation_ep_asconf(struct mbuf *m, int iphlen, int offset,
2087 struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp)
2089 struct sctp_tcb *stcb;
2090 struct sockaddr_in *sin;
2093 struct sockaddr_in6 *sin6;
2096 struct sockaddr_storage local_store, remote_store;
2100 struct ip6_hdr *ip6;
2103 struct sctp_paramhdr parm_buf, *phdr;
2105 int zero_address = 0;
2108 memset(&local_store, 0, sizeof(local_store));
2109 memset(&remote_store, 0, sizeof(remote_store));
2111 /* First get the destination address setup too. */
2112 iph = mtod(m, struct ip *);
2113 switch (iph->ip_v) {
2116 sin = (struct sockaddr_in *)&local_store;
2117 sin->sin_family = AF_INET;
2118 sin->sin_len = sizeof(*sin);
2119 sin->sin_port = sh->dest_port;
2120 sin->sin_addr.s_addr = iph->ip_dst.s_addr;
2123 case IPV6_VERSION >> 4:
2125 ip6 = mtod(m, struct ip6_hdr *);
2126 sin6 = (struct sockaddr_in6 *)&local_store;
2127 sin6->sin6_family = AF_INET6;
2128 sin6->sin6_len = sizeof(*sin6);
2129 sin6->sin6_port = sh->dest_port;
2130 sin6->sin6_addr = ip6->ip6_dst;
2137 phdr = sctp_get_next_param(m, offset + sizeof(struct sctp_asconf_chunk),
2138 &parm_buf, sizeof(struct sctp_paramhdr));
2140 SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf lookup addr\n",
2144 ptype = (int)((uint32_t) ntohs(phdr->param_type));
2145 /* get the correlation address */
2148 case SCTP_IPV6_ADDRESS:
2150 /* ipv6 address param */
2151 struct sctp_ipv6addr_param *p6, p6_buf;
2153 if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv6addr_param)) {
2156 p6 = (struct sctp_ipv6addr_param *)sctp_get_next_param(m,
2157 offset + sizeof(struct sctp_asconf_chunk),
2158 &p6_buf.ph, sizeof(*p6));
2160 SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v6 lookup addr\n",
2164 sin6 = (struct sockaddr_in6 *)&remote_store;
2165 sin6->sin6_family = AF_INET6;
2166 sin6->sin6_len = sizeof(*sin6);
2167 sin6->sin6_port = sh->src_port;
2168 memcpy(&sin6->sin6_addr, &p6->addr, sizeof(struct in6_addr));
2169 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
2174 case SCTP_IPV4_ADDRESS:
2176 /* ipv4 address param */
2177 struct sctp_ipv4addr_param *p4, p4_buf;
2179 if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv4addr_param)) {
2182 p4 = (struct sctp_ipv4addr_param *)sctp_get_next_param(m,
2183 offset + sizeof(struct sctp_asconf_chunk),
2184 &p4_buf.ph, sizeof(*p4));
2186 SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v4 lookup addr\n",
2190 sin = (struct sockaddr_in *)&remote_store;
2191 sin->sin_family = AF_INET;
2192 sin->sin_len = sizeof(*sin);
2193 sin->sin_port = sh->src_port;
2194 memcpy(&sin->sin_addr, &p4->addr, sizeof(struct in_addr));
2195 if (sin->sin_addr.s_addr == INADDR_ANY)
2200 /* invalid address param type */
2205 stcb = sctp_findassoc_by_vtag(NULL, ntohl(sh->v_tag), inp_p,
2206 netp, sh->src_port, sh->dest_port, 1);
2208 * printf("findassociation_ep_asconf: zero lookup address
2209 * finds stcb 0x%x\n", (uint32_t)stcb);
2212 stcb = sctp_findassociation_ep_addr(inp_p,
2213 (struct sockaddr *)&remote_store, netp,
2214 (struct sockaddr *)&local_store, NULL);
2221 * allocate a sctp_inpcb and setup a temporary binding to a port/all
2222 * addresses. This way if we don't get a bind we by default pick a ephemeral
2223 * port with all addresses bound.
2226 sctp_inpcb_alloc(struct socket *so, uint32_t vrf_id)
2229 * we get called when a new endpoint starts up. We need to allocate
2230 * the sctp_inpcb structure from the zone and init it. Mark it as
2231 * unbound and find a port that we can use as an ephemeral with
2232 * INADDR_ANY. If the user binds later no problem we can then add in
2233 * the specific addresses. And setup the default parameters for the
2237 struct sctp_inpcb *inp;
2239 struct timeval time;
2240 sctp_sharedkey_t *null_key;
2244 SCTP_INP_INFO_WLOCK();
2245 inp = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_ep), struct sctp_inpcb);
2247 SCTP_PRINTF("Out of SCTP-INPCB structures - no resources\n");
2248 SCTP_INP_INFO_WUNLOCK();
2249 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
2253 bzero(inp, sizeof(*inp));
2255 /* bump generations */
2256 /* setup socket pointers */
2257 inp->sctp_socket = so;
2258 inp->ip_inp.inp.inp_socket = so;
2260 inp->partial_delivery_point = SCTP_SB_LIMIT_RCV(so) >> SCTP_PARTIAL_DELIVERY_SHIFT;
2261 inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
2265 struct inpcbpolicy *pcb_sp = NULL;
2267 error = ipsec_init_policy(so, &pcb_sp);
2268 /* Arrange to share the policy */
2269 inp->ip_inp.inp.inp_sp = pcb_sp;
2270 ((struct in6pcb *)(&inp->ip_inp.inp))->in6p_sp = pcb_sp;
2273 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2274 SCTP_INP_INFO_WUNLOCK();
2278 SCTP_INCR_EP_COUNT();
2279 inp->ip_inp.inp.inp_ip_ttl = MODULE_GLOBAL(MOD_INET, ip_defttl);
2280 SCTP_INP_INFO_WUNLOCK();
2282 so->so_pcb = (caddr_t)inp;
2284 if ((SCTP_SO_TYPE(so) == SOCK_DGRAM) ||
2285 (SCTP_SO_TYPE(so) == SOCK_SEQPACKET)) {
2286 /* UDP style socket */
2287 inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
2288 SCTP_PCB_FLAGS_UNBOUND);
2289 /* Be sure it is NON-BLOCKING IO for UDP */
2290 /* SCTP_SET_SO_NBIO(so); */
2291 } else if (SCTP_SO_TYPE(so) == SOCK_STREAM) {
2292 /* TCP style socket */
2293 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2294 SCTP_PCB_FLAGS_UNBOUND);
2295 /* Be sure we have blocking IO by default */
2296 SCTP_CLEAR_SO_NBIO(so);
2299 * unsupported socket type (RAW, etc)- in case we missed it
2302 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EOPNOTSUPP);
2303 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2304 return (EOPNOTSUPP);
2306 if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_1) {
2307 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2308 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2309 } else if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_2) {
2310 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2311 sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2312 } else if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_0) {
2313 sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2314 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2316 inp->sctp_tcbhash = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_pcbtblsize),
2317 &inp->sctp_hashmark);
2318 if (inp->sctp_tcbhash == NULL) {
2319 SCTP_PRINTF("Out of SCTP-INPCB->hashinit - no resources\n");
2320 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
2321 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2324 inp->def_vrf_id = vrf_id;
2326 SCTP_INP_INFO_WLOCK();
2327 SCTP_INP_LOCK_INIT(inp);
2328 INP_LOCK_INIT(&inp->ip_inp.inp, "inp", "sctpinp");
2329 SCTP_INP_READ_INIT(inp);
2330 SCTP_ASOC_CREATE_LOCK_INIT(inp);
2331 /* lock the new ep */
2332 SCTP_INP_WLOCK(inp);
2334 /* add it to the info area */
2335 LIST_INSERT_HEAD(&SCTP_BASE_INFO(listhead), inp, sctp_list);
2336 SCTP_INP_INFO_WUNLOCK();
2338 TAILQ_INIT(&inp->read_queue);
2339 LIST_INIT(&inp->sctp_addr_list);
2341 LIST_INIT(&inp->sctp_asoc_list);
2343 #ifdef SCTP_TRACK_FREED_ASOCS
2345 LIST_INIT(&inp->sctp_asoc_free_list);
2347 /* Init the timer structure for signature change */
2348 SCTP_OS_TIMER_INIT(&inp->sctp_ep.signature_change.timer);
2349 inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NEWCOOKIE;
2351 /* now init the actual endpoint default data */
2354 /* setup the base timeout information */
2355 m->sctp_timeoutticks[SCTP_TIMER_SEND] = SEC_TO_TICKS(SCTP_SEND_SEC); /* needed ? */
2356 m->sctp_timeoutticks[SCTP_TIMER_INIT] = SEC_TO_TICKS(SCTP_INIT_SEC); /* needed ? */
2357 m->sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_delayed_sack_time_default));
2358 m->sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_heartbeat_interval_default));
2359 m->sctp_timeoutticks[SCTP_TIMER_PMTU] = SEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_pmtu_raise_time_default));
2360 m->sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] = SEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_shutdown_guard_time_default));
2361 m->sctp_timeoutticks[SCTP_TIMER_SIGNATURE] = SEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_secret_lifetime_default));
2362 /* all max/min max are in ms */
2363 m->sctp_maxrto = SCTP_BASE_SYSCTL(sctp_rto_max_default);
2364 m->sctp_minrto = SCTP_BASE_SYSCTL(sctp_rto_min_default);
2365 m->initial_rto = SCTP_BASE_SYSCTL(sctp_rto_initial_default);
2366 m->initial_init_rto_max = SCTP_BASE_SYSCTL(sctp_init_rto_max_default);
2367 m->sctp_sack_freq = SCTP_BASE_SYSCTL(sctp_sack_freq_default);
2369 m->max_open_streams_intome = MAX_SCTP_STREAMS;
2371 m->max_init_times = SCTP_BASE_SYSCTL(sctp_init_rtx_max_default);
2372 m->max_send_times = SCTP_BASE_SYSCTL(sctp_assoc_rtx_max_default);
2373 m->def_net_failure = SCTP_BASE_SYSCTL(sctp_path_rtx_max_default);
2374 m->sctp_sws_sender = SCTP_SWS_SENDER_DEF;
2375 m->sctp_sws_receiver = SCTP_SWS_RECEIVER_DEF;
2376 m->max_burst = SCTP_BASE_SYSCTL(sctp_max_burst_default);
2377 if ((SCTP_BASE_SYSCTL(sctp_default_cc_module) >= SCTP_CC_RFC2581) &&
2378 (SCTP_BASE_SYSCTL(sctp_default_cc_module) <= SCTP_CC_HTCP)) {
2379 m->sctp_default_cc_module = SCTP_BASE_SYSCTL(sctp_default_cc_module);
2381 /* sysctl done with invalid value, set to 2581 */
2382 m->sctp_default_cc_module = SCTP_CC_RFC2581;
2384 /* number of streams to pre-open on a association */
2385 m->pre_open_stream_count = SCTP_BASE_SYSCTL(sctp_nr_outgoing_streams_default);
2387 /* Add adaptation cookie */
2388 m->adaptation_layer_indicator = 0x504C5253;
2390 /* seed random number generator */
2391 m->random_counter = 1;
2392 m->store_at = SCTP_SIGNATURE_SIZE;
2393 SCTP_READ_RANDOM(m->random_numbers, sizeof(m->random_numbers));
2394 sctp_fill_random_store(m);
2396 /* Minimum cookie size */
2397 m->size_of_a_cookie = (sizeof(struct sctp_init_msg) * 2) +
2398 sizeof(struct sctp_state_cookie);
2399 m->size_of_a_cookie += SCTP_SIGNATURE_SIZE;
2401 /* Setup the initial secret */
2402 (void)SCTP_GETTIME_TIMEVAL(&time);
2403 m->time_of_secret_change = time.tv_sec;
2405 for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
2406 m->secret_key[0][i] = sctp_select_initial_TSN(m);
2408 sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL);
2410 /* How long is a cookie good for ? */
2411 m->def_cookie_life = MSEC_TO_TICKS(SCTP_BASE_SYSCTL(sctp_valid_cookie_life_default));
2413 * Initialize authentication parameters
2415 m->local_hmacs = sctp_default_supported_hmaclist();
2416 m->local_auth_chunks = sctp_alloc_chunklist();
2417 sctp_auth_set_default_chunks(m->local_auth_chunks);
2418 LIST_INIT(&m->shared_keys);
2419 /* add default NULL key as key id 0 */
2420 null_key = sctp_alloc_sharedkey();
2421 sctp_insert_sharedkey(&m->shared_keys, null_key);
2422 SCTP_INP_WUNLOCK(inp);
2423 #ifdef SCTP_LOG_CLOSING
2424 sctp_log_closing(inp, NULL, 12);
2431 sctp_move_pcb_and_assoc(struct sctp_inpcb *old_inp, struct sctp_inpcb *new_inp,
2432 struct sctp_tcb *stcb)
2434 struct sctp_nets *net;
2435 uint16_t lport, rport;
2436 struct sctppcbhead *head;
2437 struct sctp_laddr *laddr, *oladdr;
2439 atomic_add_int(&stcb->asoc.refcnt, 1);
2440 SCTP_TCB_UNLOCK(stcb);
2441 SCTP_INP_INFO_WLOCK();
2442 SCTP_INP_WLOCK(old_inp);
2443 SCTP_INP_WLOCK(new_inp);
2444 SCTP_TCB_LOCK(stcb);
2445 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2447 new_inp->sctp_ep.time_of_secret_change =
2448 old_inp->sctp_ep.time_of_secret_change;
2449 memcpy(new_inp->sctp_ep.secret_key, old_inp->sctp_ep.secret_key,
2450 sizeof(old_inp->sctp_ep.secret_key));
2451 new_inp->sctp_ep.current_secret_number =
2452 old_inp->sctp_ep.current_secret_number;
2453 new_inp->sctp_ep.last_secret_number =
2454 old_inp->sctp_ep.last_secret_number;
2455 new_inp->sctp_ep.size_of_a_cookie = old_inp->sctp_ep.size_of_a_cookie;
2457 /* make it so new data pours into the new socket */
2458 stcb->sctp_socket = new_inp->sctp_socket;
2459 stcb->sctp_ep = new_inp;
2461 /* Copy the port across */
2462 lport = new_inp->sctp_lport = old_inp->sctp_lport;
2463 rport = stcb->rport;
2464 /* Pull the tcb from the old association */
2465 LIST_REMOVE(stcb, sctp_tcbhash);
2466 LIST_REMOVE(stcb, sctp_tcblist);
2468 /* Now insert the new_inp into the TCP connected hash */
2469 head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR((lport),
2470 SCTP_BASE_INFO(hashtcpmark))];
2472 LIST_INSERT_HEAD(head, new_inp, sctp_hash);
2473 /* Its safe to access */
2474 new_inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
2476 /* Now move the tcb into the endpoint list */
2477 LIST_INSERT_HEAD(&new_inp->sctp_asoc_list, stcb, sctp_tcblist);
2479 * Question, do we even need to worry about the ep-hash since we
2480 * only have one connection? Probably not :> so lets get rid of it
2481 * and not suck up any kernel memory in that.
2484 /* Ok. Let's restart timer. */
2485 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2486 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, new_inp,
2490 SCTP_INP_INFO_WUNLOCK();
2491 if (new_inp->sctp_tcbhash != NULL) {
2492 SCTP_HASH_FREE(new_inp->sctp_tcbhash, new_inp->sctp_hashmark);
2493 new_inp->sctp_tcbhash = NULL;
2495 if ((new_inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
2496 /* Subset bound, so copy in the laddr list from the old_inp */
2497 LIST_FOREACH(oladdr, &old_inp->sctp_addr_list, sctp_nxt_addr) {
2498 laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
2499 if (laddr == NULL) {
2501 * Gak, what can we do? This assoc is really
2502 * HOSED. We probably should send an abort
2505 SCTPDBG(SCTP_DEBUG_PCB1, "Association hosed in TCP model, out of laddr memory\n");
2508 SCTP_INCR_LADDR_COUNT();
2509 bzero(laddr, sizeof(*laddr));
2510 (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time);
2511 laddr->ifa = oladdr->ifa;
2512 atomic_add_int(&laddr->ifa->refcount, 1);
2513 LIST_INSERT_HEAD(&new_inp->sctp_addr_list, laddr,
2515 new_inp->laddr_count++;
2519 * Now any running timers need to be adjusted since we really don't
2520 * care if they are running or not just blast in the new_inp into
2524 stcb->asoc.hb_timer.ep = (void *)new_inp;
2525 stcb->asoc.dack_timer.ep = (void *)new_inp;
2526 stcb->asoc.asconf_timer.ep = (void *)new_inp;
2527 stcb->asoc.strreset_timer.ep = (void *)new_inp;
2528 stcb->asoc.shut_guard_timer.ep = (void *)new_inp;
2529 stcb->asoc.autoclose_timer.ep = (void *)new_inp;
2530 stcb->asoc.delayed_event_timer.ep = (void *)new_inp;
2531 stcb->asoc.delete_prim_timer.ep = (void *)new_inp;
2532 /* now what about the nets? */
2533 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2534 net->pmtu_timer.ep = (void *)new_inp;
2535 net->rxt_timer.ep = (void *)new_inp;
2536 net->fr_timer.ep = (void *)new_inp;
2538 SCTP_INP_WUNLOCK(new_inp);
2539 SCTP_INP_WUNLOCK(old_inp);
2545 /* sctp_ifap is used to bypass normal local address validation checks */
2547 sctp_inpcb_bind(struct socket *so, struct sockaddr *addr,
2548 struct sctp_ifa *sctp_ifap, struct thread *p)
2550 /* bind a ep to a socket address */
2551 struct sctppcbhead *head;
2552 struct sctp_inpcb *inp, *inp_tmp;
2553 struct inpcb *ip_inp;
2554 int port_reuse_active = 0;
2564 inp = (struct sctp_inpcb *)so->so_pcb;
2565 ip_inp = (struct inpcb *)so->so_pcb;
2568 SCTPDBG(SCTP_DEBUG_PCB1, "Bind called port:%d\n",
2569 ntohs(((struct sockaddr_in *)addr)->sin_port));
2570 SCTPDBG(SCTP_DEBUG_PCB1, "Addr :");
2571 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr);
2574 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
2575 /* already did a bind, subsequent binds NOT allowed ! */
2576 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
2581 panic("null proc/thread");
2583 if (p && jailed(p->td_ucred)) {
2587 switch (addr->sa_family) {
2590 struct sockaddr_in *sin;
2592 /* IPV6_V6ONLY socket? */
2593 if (SCTP_IPV6_V6ONLY(ip_inp)) {
2594 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
2597 if (addr->sa_len != sizeof(*sin)) {
2598 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
2601 sin = (struct sockaddr_in *)addr;
2602 lport = sin->sin_port;
2605 * For INADDR_ANY and LOOPBACK the
2606 * prison_ip() call will transmute
2607 * the ip address to the proper
2608 * value (i.e. the IP address owned
2611 if (prison_ip(p->td_ucred, 0, &sin->sin_addr.s_addr)) {
2612 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
2616 if (sin->sin_addr.s_addr != INADDR_ANY) {
2625 * Only for pure IPv6 Address. (No IPv4
2628 struct sockaddr_in6 *sin6;
2630 sin6 = (struct sockaddr_in6 *)addr;
2632 if (addr->sa_len != sizeof(*sin6)) {
2633 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
2636 lport = sin6->sin6_port;
2638 * Jail checks for IPv6 should go HERE! i.e.
2639 * add the prison_ip() equivilant in this
2640 * postion to transmute the addresses to the
2641 * proper one jailed.
2643 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2645 /* KAME hack: embed scopeid */
2646 if (sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone)) != 0) {
2647 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
2651 /* this must be cleared for ifa_ifwithaddr() */
2652 sin6->sin6_scope_id = 0;
2657 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EAFNOSUPPORT);
2658 return (EAFNOSUPPORT);
2661 SCTP_INP_INFO_WLOCK();
2662 SCTP_INP_WLOCK(inp);
2663 /* Setup a vrf_id to be the default for the non-bind-all case. */
2664 vrf_id = inp->def_vrf_id;
2666 /* increase our count due to the unlock we do */
2667 SCTP_INP_INCR_REF(inp);
2670 * Did the caller specify a port? if so we must see if a ep
2671 * already has this one bound.
2673 /* got to be root to get at low ports */
2674 if (ntohs(lport) < IPPORT_RESERVED) {
2676 priv_check(p, PRIV_NETINET_RESERVEDPORT)
2678 SCTP_INP_DECR_REF(inp);
2679 SCTP_INP_WUNLOCK(inp);
2680 SCTP_INP_INFO_WUNLOCK();
2685 SCTP_INP_DECR_REF(inp);
2686 SCTP_INP_WUNLOCK(inp);
2687 SCTP_INP_INFO_WUNLOCK();
2688 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
2691 SCTP_INP_WUNLOCK(inp);
2693 vrf_id = inp->def_vrf_id;
2694 inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id);
2695 if (inp_tmp != NULL) {
2697 * lock guy returned and lower count note
2698 * that we are not bound so inp_tmp should
2699 * NEVER be inp. And it is this inp
2700 * (inp_tmp) that gets the reference bump,
2701 * so we must lower it.
2703 SCTP_INP_DECR_REF(inp_tmp);
2705 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
2706 (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
2708 * Ok, must be one-2-one and
2709 * allowing port re-use
2711 port_reuse_active = 1;
2712 goto continue_anyway;
2714 SCTP_INP_DECR_REF(inp);
2715 SCTP_INP_INFO_WUNLOCK();
2716 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE);
2717 return (EADDRINUSE);
2720 inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id);
2721 if (inp_tmp != NULL) {
2723 * lock guy returned and lower count note
2724 * that we are not bound so inp_tmp should
2725 * NEVER be inp. And it is this inp
2726 * (inp_tmp) that gets the reference bump,
2727 * so we must lower it.
2729 SCTP_INP_DECR_REF(inp_tmp);
2731 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
2732 (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
2734 * Ok, must be one-2-one and
2735 * allowing port re-use
2737 port_reuse_active = 1;
2738 goto continue_anyway;
2740 SCTP_INP_DECR_REF(inp);
2741 SCTP_INP_INFO_WUNLOCK();
2742 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE);
2743 return (EADDRINUSE);
2747 SCTP_INP_WLOCK(inp);
2749 /* verify that no lport is not used by a singleton */
2750 if ((port_reuse_active == 0) &&
2751 (inp_tmp = sctp_isport_inuse(inp, lport, vrf_id))
2753 /* Sorry someone already has this one bound */
2754 if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
2755 (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
2756 port_reuse_active = 1;
2758 SCTP_INP_DECR_REF(inp);
2759 SCTP_INP_WUNLOCK(inp);
2760 SCTP_INP_INFO_WUNLOCK();
2761 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE);
2762 return (EADDRINUSE);
2767 uint16_t first, last, candidate;
2771 if (ip_inp->inp_flags & INP_HIGHPORT) {
2772 first = MODULE_GLOBAL(MOD_INET, ipport_hifirstauto);
2773 last = MODULE_GLOBAL(MOD_INET, ipport_hilastauto);
2774 } else if (ip_inp->inp_flags & INP_LOWPORT) {
2776 priv_check(p, PRIV_NETINET_RESERVEDPORT)
2778 SCTP_INP_DECR_REF(inp);
2779 SCTP_INP_WUNLOCK(inp);
2780 SCTP_INP_INFO_WUNLOCK();
2781 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
2784 first = MODULE_GLOBAL(MOD_INET, ipport_lowfirstauto);
2785 last = MODULE_GLOBAL(MOD_INET, ipport_lowlastauto);
2787 first = MODULE_GLOBAL(MOD_INET, ipport_firstauto);
2788 last = MODULE_GLOBAL(MOD_INET, ipport_lastauto);
2797 count = last - first + 1; /* number of candidates */
2798 candidate = first + sctp_select_initial_TSN(&inp->sctp_ep) % (count);
2802 if (sctp_isport_inuse(inp, htons(candidate), inp->def_vrf_id) == NULL) {
2807 SCTP_INP_DECR_REF(inp);
2808 SCTP_INP_WUNLOCK(inp);
2809 SCTP_INP_INFO_WUNLOCK();
2810 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE);
2811 return (EADDRINUSE);
2813 if (candidate == last)
2816 candidate = candidate + 1;
2819 lport = htons(candidate);
2821 SCTP_INP_DECR_REF(inp);
2822 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE |
2823 SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
2825 * this really should not happen. The guy did a non-blocking
2826 * bind and then did a close at the same time.
2828 SCTP_INP_WUNLOCK(inp);
2829 SCTP_INP_INFO_WUNLOCK();
2830 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
2833 /* ok we look clear to give out this port, so lets setup the binding */
2835 /* binding to all addresses, so just set in the proper flags */
2836 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUNDALL;
2837 /* set the automatic addr changes from kernel flag */
2838 if (SCTP_BASE_SYSCTL(sctp_auto_asconf) == 0) {
2839 sctp_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF);
2840 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
2842 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
2843 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
2845 if (SCTP_BASE_SYSCTL(sctp_multiple_asconfs) == 0) {
2846 sctp_feature_off(inp, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS);
2848 sctp_feature_on(inp, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS);
2851 * set the automatic mobility_base from kernel flag (by
2854 if (SCTP_BASE_SYSCTL(sctp_mobility_base) == 0) {
2855 sctp_mobility_feature_off(inp, SCTP_MOBILITY_BASE);
2856 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
2858 sctp_mobility_feature_on(inp, SCTP_MOBILITY_BASE);
2859 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
2862 * set the automatic mobility_fasthandoff from kernel flag
2865 if (SCTP_BASE_SYSCTL(sctp_mobility_fasthandoff) == 0) {
2866 sctp_mobility_feature_off(inp, SCTP_MOBILITY_FASTHANDOFF);
2867 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
2869 sctp_mobility_feature_on(inp, SCTP_MOBILITY_FASTHANDOFF);
2870 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
2874 * bind specific, make sure flags is off and add a new
2875 * address structure to the sctp_addr_list inside the ep
2878 * We will need to allocate one and insert it at the head. The
2879 * socketopt call can just insert new addresses in there as
2880 * well. It will also have to do the embed scope kame hack
2881 * too (before adding).
2883 struct sctp_ifa *ifa;
2884 struct sockaddr_storage store_sa;
2886 memset(&store_sa, 0, sizeof(store_sa));
2887 if (addr->sa_family == AF_INET) {
2888 struct sockaddr_in *sin;
2890 sin = (struct sockaddr_in *)&store_sa;
2891 memcpy(sin, addr, sizeof(struct sockaddr_in));
2893 } else if (addr->sa_family == AF_INET6) {
2894 struct sockaddr_in6 *sin6;
2896 sin6 = (struct sockaddr_in6 *)&store_sa;
2897 memcpy(sin6, addr, sizeof(struct sockaddr_in6));
2898 sin6->sin6_port = 0;
2901 * first find the interface with the bound address need to
2902 * zero out the port to find the address! yuck! can't do
2903 * this earlier since need port for sctp_pcb_findep()
2905 if (sctp_ifap != NULL)
2909 * Note for BSD we hit here always other O/S's will
2910 * pass things in via the sctp_ifap argument
2913 ifa = sctp_find_ifa_by_addr((struct sockaddr *)&store_sa,
2914 vrf_id, SCTP_ADDR_NOT_LOCKED);
2917 /* Can't find an interface with that address */
2918 SCTP_INP_WUNLOCK(inp);
2919 SCTP_INP_INFO_WUNLOCK();
2920 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRNOTAVAIL);
2921 return (EADDRNOTAVAIL);
2923 if (addr->sa_family == AF_INET6) {
2924 /* GAK, more FIXME IFA lock? */
2925 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2926 /* Can't bind a non-existent addr. */
2927 SCTP_INP_WUNLOCK(inp);
2928 SCTP_INP_INFO_WUNLOCK();
2929 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
2933 /* we're not bound all */
2934 inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUNDALL;
2935 /* allow bindx() to send ASCONF's for binding changes */
2936 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
2937 /* clear automatic addr changes from kernel flag */
2938 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
2940 /* add this address to the endpoint list */
2941 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, 0);
2943 SCTP_INP_WUNLOCK(inp);
2944 SCTP_INP_INFO_WUNLOCK();
2949 /* find the bucket */
2950 if (port_reuse_active) {
2951 /* Put it into tcp 1-2-1 hash */
2952 head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR((lport),
2953 SCTP_BASE_INFO(hashtcpmark))];
2954 inp->sctp_flags |= SCTP_PCB_FLAGS_IN_TCPPOOL;
2956 head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport,
2957 SCTP_BASE_INFO(hashmark))];
2959 /* put it in the bucket */
2960 LIST_INSERT_HEAD(head, inp, sctp_hash);
2961 SCTPDBG(SCTP_DEBUG_PCB1, "Main hash to bind at head:%p, bound port:%d - in tcp_pool=%d\n",
2962 head, ntohs(lport), port_reuse_active);
2963 /* set in the port */
2964 inp->sctp_lport = lport;
2966 /* turn off just the unbound flag */
2967 inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
2968 SCTP_INP_WUNLOCK(inp);
2969 SCTP_INP_INFO_WUNLOCK();
2975 sctp_iterator_inp_being_freed(struct sctp_inpcb *inp, struct sctp_inpcb *inp_next)
2977 struct sctp_iterator *it;
2980 * We enter with the only the ITERATOR_LOCK in place and a write
2981 * lock on the inp_info stuff.
2985 * Go through all iterators, we must do this since it is possible
2986 * that some iterator does NOT have the lock, but is waiting for it.
2987 * And the one that had the lock has either moved in the last
2988 * iteration or we just cleared it above. We need to find all of
2989 * those guys. The list of iterators should never be very big
2992 TAILQ_FOREACH(it, &SCTP_BASE_INFO(iteratorhead), sctp_nxt_itr) {
2993 if (it == inp->inp_starting_point_for_iterator)
2994 /* skip this guy, he's special */
2996 if (it->inp == inp) {
2998 * This is tricky and we DON'T lock the iterator.
2999 * Reason is he's running but waiting for me since
3000 * inp->inp_starting_point_for_iterator has the lock
3001 * on me (the guy above we skipped). This tells us
3002 * its is not running but waiting for
3003 * inp->inp_starting_point_for_iterator to be
3004 * released by the guy that does have our INP in a
3007 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
3011 /* set him up to do the next guy not me */
3017 it = inp->inp_starting_point_for_iterator;
3019 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
3028 /* release sctp_inpcb unbind the port */
3030 sctp_inpcb_free(struct sctp_inpcb *inp, int immediate, int from)
3033 * Here we free a endpoint. We must find it (if it is in the Hash
3034 * table) and remove it from there. Then we must also find it in the
3035 * overall list and remove it from there. After all removals are
3036 * complete then any timer has to be stopped. Then start the actual
3037 * freeing. a) Any local lists. b) Any associations. c) The hash of
3038 * all associations. d) finally the ep itself.
3041 struct sctp_inpcb *inp_save;
3042 struct sctp_tcb *asoc, *nasoc;
3043 struct sctp_laddr *laddr, *nladdr;
3044 struct inpcb *ip_pcb;
3047 struct sctp_queued_to_read *sq;
3051 sctp_sharedkey_t *shared_key;
3054 #ifdef SCTP_LOG_CLOSING
3055 sctp_log_closing(inp, NULL, 0);
3057 SCTP_ITERATOR_LOCK();
3058 so = inp->sctp_socket;
3059 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
3060 /* been here before.. eeks.. get out of here */
3061 SCTP_PRINTF("This conflict in free SHOULD not be happening! from %d, imm %d\n", from, immediate);
3062 SCTP_ITERATOR_UNLOCK();
3063 #ifdef SCTP_LOG_CLOSING
3064 sctp_log_closing(inp, NULL, 1);
3068 SCTP_ASOC_CREATE_LOCK(inp);
3069 SCTP_INP_INFO_WLOCK();
3071 SCTP_INP_WLOCK(inp);
3072 /* First time through we have the socket lock, after that no more. */
3073 if (from == SCTP_CALLED_AFTER_CMPSET_OFCLOSE) {
3075 * Once we are in we can remove the flag from = 1 is only
3076 * passed from the actual closing routines that are called
3077 * via the sockets layer.
3079 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CLOSE_IP;
3080 /* socket is gone, so no more wakeups allowed */
3081 inp->sctp_flags |= SCTP_PCB_FLAGS_DONT_WAKE;
3082 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
3083 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
3085 sctp_timer_stop(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL,
3086 SCTP_FROM_SCTP_PCB + SCTP_LOC_1);
3089 sctp_m_freem(inp->control);
3090 inp->control = NULL;
3093 sctp_m_freem(inp->pkt);
3097 ip_pcb = &inp->ip_inp.inp; /* we could just cast the main pointer
3098 * here but I will be nice :> (i.e.
3100 if (immediate == SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE) {
3104 for ((asoc = LIST_FIRST(&inp->sctp_asoc_list)); asoc != NULL;
3106 SCTP_TCB_LOCK(asoc);
3107 nasoc = LIST_NEXT(asoc, sctp_tcblist);
3108 if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3109 /* Skip guys being freed */
3110 /* asoc->sctp_socket = NULL; FIXME MT */
3112 SCTP_TCB_UNLOCK(asoc);
3115 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_WAIT) ||
3116 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
3118 * If we have data in queue, we don't want
3119 * to just free since the app may have done,
3120 * send()/close or connect/send/close. And
3121 * it wants the data to get across first.
3123 /* Just abandon things in the front states */
3124 if (sctp_free_assoc(inp, asoc, SCTP_PCBFREE_NOFORCE,
3125 SCTP_FROM_SCTP_PCB + SCTP_LOC_2) == 0) {
3130 /* Disconnect the socket please */
3131 asoc->sctp_socket = NULL;
3132 asoc->asoc.state |= SCTP_STATE_CLOSED_SOCKET;
3133 if ((asoc->asoc.size_on_reasm_queue > 0) ||
3134 (asoc->asoc.control_pdapi) ||
3135 (asoc->asoc.size_on_all_streams > 0) ||
3136 (so && (so->so_rcv.sb_cc > 0))
3138 /* Left with Data unread */
3139 struct mbuf *op_err;
3141 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
3142 0, M_DONTWAIT, 1, MT_DATA);
3144 /* Fill in the user initiated abort */
3145 struct sctp_paramhdr *ph;
3148 SCTP_BUF_LEN(op_err) =
3149 sizeof(struct sctp_paramhdr) + sizeof(uint32_t);
3151 struct sctp_paramhdr *);
3152 ph->param_type = htons(
3153 SCTP_CAUSE_USER_INITIATED_ABT);
3154 ph->param_length = htons(SCTP_BUF_LEN(op_err));
3155 ippp = (uint32_t *) (ph + 1);
3156 *ippp = htonl(SCTP_FROM_SCTP_PCB + SCTP_LOC_3);
3158 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_3;
3159 #if defined(SCTP_PANIC_ON_ABORT)
3160 panic("inpcb_free does an abort");
3162 sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED);
3163 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3164 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
3165 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3166 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3168 if (sctp_free_assoc(inp, asoc,
3169 SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_4) == 0) {
3173 } else if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
3174 TAILQ_EMPTY(&asoc->asoc.sent_queue) &&
3175 (asoc->asoc.stream_queue_cnt == 0)
3177 if (asoc->asoc.locked_on_sending) {
3180 if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
3181 (SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
3183 * there is nothing queued to send,
3184 * so I send shutdown
3186 sctp_send_shutdown(asoc, asoc->asoc.primary_destination);
3187 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
3188 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3189 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3191 SCTP_SET_STATE(&asoc->asoc, SCTP_STATE_SHUTDOWN_SENT);
3192 SCTP_CLEAR_SUBSTATE(&asoc->asoc, SCTP_STATE_SHUTDOWN_PENDING);
3193 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, asoc->sctp_ep, asoc,
3194 asoc->asoc.primary_destination);
3195 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc,
3196 asoc->asoc.primary_destination);
3197 sctp_chunk_output(inp, asoc, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_LOCKED);
3200 /* mark into shutdown pending */
3201 struct sctp_stream_queue_pending *sp;
3203 asoc->asoc.state |= SCTP_STATE_SHUTDOWN_PENDING;
3204 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc,
3205 asoc->asoc.primary_destination);
3206 if (asoc->asoc.locked_on_sending) {
3207 sp = TAILQ_LAST(&((asoc->asoc.locked_on_sending)->outqueue),
3210 SCTP_PRINTF("Error, sp is NULL, locked on sending is %p strm:%d\n",
3211 asoc->asoc.locked_on_sending,
3212 asoc->asoc.locked_on_sending->stream_no);
3214 if ((sp->length == 0) && (sp->msg_is_complete == 0))
3215 asoc->asoc.state |= SCTP_STATE_PARTIAL_MSG_LEFT;
3218 if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
3219 TAILQ_EMPTY(&asoc->asoc.sent_queue) &&
3220 (asoc->asoc.state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
3221 struct mbuf *op_err;
3224 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
3225 0, M_DONTWAIT, 1, MT_DATA);
3231 struct sctp_paramhdr *ph;
3234 SCTP_BUF_LEN(op_err) =
3235 (sizeof(struct sctp_paramhdr) +
3238 struct sctp_paramhdr *);
3239 ph->param_type = htons(
3240 SCTP_CAUSE_USER_INITIATED_ABT);
3241 ph->param_length = htons(SCTP_BUF_LEN(op_err));
3242 ippp = (uint32_t *) (ph + 1);
3243 *ippp = htonl(SCTP_FROM_SCTP_PCB + SCTP_LOC_5);
3245 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_5;
3246 #if defined(SCTP_PANIC_ON_ABORT)
3247 panic("inpcb_free does an abort");
3250 sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED);
3251 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3252 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
3253 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3254 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3256 if (sctp_free_assoc(inp, asoc,
3257 SCTP_PCBFREE_NOFORCE,
3258 SCTP_FROM_SCTP_PCB + SCTP_LOC_6) == 0) {
3263 sctp_chunk_output(inp, asoc, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
3267 SCTP_TCB_UNLOCK(asoc);
3269 /* now is there some left in our SHUTDOWN state? */
3271 SCTP_INP_WUNLOCK(inp);
3272 SCTP_ASOC_CREATE_UNLOCK(inp);
3273 SCTP_INP_INFO_WUNLOCK();
3274 SCTP_ITERATOR_UNLOCK();
3275 #ifdef SCTP_LOG_CLOSING
3276 sctp_log_closing(inp, NULL, 2);
3281 inp->sctp_socket = NULL;
3282 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) !=
3283 SCTP_PCB_FLAGS_UNBOUND) {
3285 * ok, this guy has been bound. It's port is somewhere in
3286 * the SCTP_BASE_INFO(hash table). Remove it!
3288 LIST_REMOVE(inp, sctp_hash);
3289 inp->sctp_flags |= SCTP_PCB_FLAGS_UNBOUND;
3292 * If there is a timer running to kill us, forget it, since it may
3293 * have a contest on the INP lock.. which would cause us to die ...
3296 for ((asoc = LIST_FIRST(&inp->sctp_asoc_list)); asoc != NULL;
3298 nasoc = LIST_NEXT(asoc, sctp_tcblist);
3299 if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3303 /* Free associations that are NOT killing us */
3304 SCTP_TCB_LOCK(asoc);
3305 if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_COOKIE_WAIT) &&
3306 ((asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0)) {
3307 struct mbuf *op_err;
3310 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
3311 0, M_DONTWAIT, 1, MT_DATA);
3313 /* Fill in the user initiated abort */
3314 struct sctp_paramhdr *ph;
3316 SCTP_BUF_LEN(op_err) = (sizeof(struct sctp_paramhdr) +
3318 ph = mtod(op_err, struct sctp_paramhdr *);
3319 ph->param_type = htons(
3320 SCTP_CAUSE_USER_INITIATED_ABT);
3321 ph->param_length = htons(SCTP_BUF_LEN(op_err));
3322 ippp = (uint32_t *) (ph + 1);
3323 *ippp = htonl(SCTP_FROM_SCTP_PCB + SCTP_LOC_7);
3326 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_7;
3327 #if defined(SCTP_PANIC_ON_ABORT)
3328 panic("inpcb_free does an abort");
3330 sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED);
3331 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3332 } else if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3334 SCTP_TCB_UNLOCK(asoc);
3337 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
3338 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3339 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3341 if (sctp_free_assoc(inp, asoc, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_8) == 0) {
3346 /* Ok we have someone out there that will kill us */
3347 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
3348 SCTP_INP_WUNLOCK(inp);
3349 SCTP_ASOC_CREATE_UNLOCK(inp);
3350 SCTP_INP_INFO_WUNLOCK();
3351 SCTP_ITERATOR_UNLOCK();
3352 #ifdef SCTP_LOG_CLOSING
3353 sctp_log_closing(inp, NULL, 3);
3357 if ((inp->refcount) || (inp->sctp_flags & SCTP_PCB_FLAGS_CLOSE_IP)) {
3358 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
3359 sctp_timer_start(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL);
3360 SCTP_INP_WUNLOCK(inp);
3361 SCTP_ASOC_CREATE_UNLOCK(inp);
3362 SCTP_INP_INFO_WUNLOCK();
3363 SCTP_ITERATOR_UNLOCK();
3364 #ifdef SCTP_LOG_CLOSING
3365 sctp_log_closing(inp, NULL, 4);
3369 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
3370 inp->sctp_ep.signature_change.type = 0;
3371 inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_ALLGONE;
3373 #ifdef SCTP_LOG_CLOSING
3374 sctp_log_closing(inp, NULL, 5);
3377 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
3378 inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NONE;
3379 /* Clear the read queue */
3380 /* sa_ignore FREED_MEMORY */
3381 while ((sq = TAILQ_FIRST(&inp->read_queue)) != NULL) {
3382 /* Its only abandoned if it had data left */
3384 SCTP_STAT_INCR(sctps_left_abandon);
3386 TAILQ_REMOVE(&inp->read_queue, sq, next);
3387 sctp_free_remote_addr(sq->whoFrom);
3389 so->so_rcv.sb_cc -= sq->length;
3391 sctp_m_freem(sq->data);
3395 * no need to free the net count, since at this point all
3398 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), sq);
3399 SCTP_DECR_READQ_COUNT();
3401 /* Now the sctp_pcb things */
3403 * free each asoc if it is not already closed/free. we can't use the
3404 * macro here since le_next will get freed as part of the
3405 * sctp_free_assoc() call.
3410 ipsec4_delete_pcbpolicy(ip_pcb);
3413 /* Unlocks not needed since the socket is gone now */
3415 if (ip_pcb->inp_options) {
3416 (void)sctp_m_free(ip_pcb->inp_options);
3417 ip_pcb->inp_options = 0;
3419 if (ip_pcb->inp_moptions) {
3420 inp_freemoptions(ip_pcb->inp_moptions);
3421 ip_pcb->inp_moptions = 0;
3424 if (ip_pcb->inp_vflag & INP_IPV6) {
3425 struct in6pcb *in6p;
3427 in6p = (struct in6pcb *)inp;
3428 ip6_freepcbopts(in6p->in6p_outputopts);
3431 ip_pcb->inp_vflag = 0;
3432 /* free up authentication fields */
3433 if (inp->sctp_ep.local_auth_chunks != NULL)
3434 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
3435 if (inp->sctp_ep.local_hmacs != NULL)
3436 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
3438 shared_key = LIST_FIRST(&inp->sctp_ep.shared_keys);
3439 while (shared_key) {
3440 LIST_REMOVE(shared_key, next);
3441 sctp_free_sharedkey(shared_key);
3442 /* sa_ignore FREED_MEMORY */
3443 shared_key = LIST_FIRST(&inp->sctp_ep.shared_keys);
3446 inp_save = LIST_NEXT(inp, sctp_list);
3447 LIST_REMOVE(inp, sctp_list);
3449 /* fix any iterators only after out of the list */
3450 sctp_iterator_inp_being_freed(inp, inp_save);
3452 * if we have an address list the following will free the list of
3453 * ifaddr's that are set into this ep. Again macro limitations here,
3454 * since the LIST_FOREACH could be a bad idea.
3456 for ((laddr = LIST_FIRST(&inp->sctp_addr_list)); laddr != NULL;
3458 nladdr = LIST_NEXT(laddr, sctp_nxt_addr);
3459 sctp_remove_laddr(laddr);
3462 #ifdef SCTP_TRACK_FREED_ASOCS
3464 for ((asoc = LIST_FIRST(&inp->sctp_asoc_free_list)); asoc != NULL;
3466 nasoc = LIST_NEXT(asoc, sctp_tcblist);
3467 LIST_REMOVE(asoc, sctp_tcblist);
3468 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), asoc);
3469 SCTP_DECR_ASOC_COUNT();
3471 /* *** END TEMP CODE *** */
3473 /* Now lets see about freeing the EP hash table. */
3474 if (inp->sctp_tcbhash != NULL) {
3475 SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark);
3476 inp->sctp_tcbhash = NULL;
3478 /* Now we must put the ep memory back into the zone pool */
3479 INP_LOCK_DESTROY(&inp->ip_inp.inp);
3480 SCTP_INP_LOCK_DESTROY(inp);
3481 SCTP_INP_READ_DESTROY(inp);
3482 SCTP_ASOC_CREATE_LOCK_DESTROY(inp);
3483 SCTP_INP_INFO_WUNLOCK();
3484 SCTP_ITERATOR_UNLOCK();
3485 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
3486 SCTP_DECR_EP_COUNT();
3491 sctp_findnet(struct sctp_tcb *stcb, struct sockaddr *addr)
3493 struct sctp_nets *net;
3495 /* locate the address */
3496 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3497 if (sctp_cmpaddr(addr, (struct sockaddr *)&net->ro._l_addr))
3505 sctp_is_address_on_local_host(struct sockaddr *addr, uint32_t vrf_id)
3507 struct sctp_ifa *sctp_ifa;
3509 sctp_ifa = sctp_find_ifa_by_addr(addr, vrf_id, SCTP_ADDR_NOT_LOCKED);
3518 * add's a remote endpoint address, done with the INIT/INIT-ACK as well as
3519 * when a ASCONF arrives that adds it. It will also initialize all the cwnd
3523 sctp_add_remote_addr(struct sctp_tcb *stcb, struct sockaddr *newaddr,
3524 int set_scope, int from)
3527 * The following is redundant to the same lines in the
3528 * sctp_aloc_assoc() but is needed since other's call the add
3531 struct sctp_nets *net, *netfirst;
3534 SCTPDBG(SCTP_DEBUG_PCB1, "Adding an address (from:%d) to the peer: ",
3536 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, newaddr);
3538 netfirst = sctp_findnet(stcb, newaddr);
3541 * Lie and return ok, we don't want to make the association
3542 * go away for this behavior. It will happen in the TCP
3543 * model in a connected socket. It does not reach the hash
3544 * table until after the association is built so it can't be
3545 * found. Mark as reachable, since the initial creation will
3546 * have been cleared and the NOT_IN_ASSOC flag will have
3547 * been added... and we don't want to end up removing it
3550 if (netfirst->dest_state & SCTP_ADDR_UNCONFIRMED) {
3551 netfirst->dest_state = (SCTP_ADDR_REACHABLE |
3552 SCTP_ADDR_UNCONFIRMED);
3554 netfirst->dest_state = SCTP_ADDR_REACHABLE;
3560 if (newaddr->sa_family == AF_INET) {
3561 struct sockaddr_in *sin;
3563 sin = (struct sockaddr_in *)newaddr;
3564 if (sin->sin_addr.s_addr == 0) {
3565 /* Invalid address */
3568 /* zero out the bzero area */
3569 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
3571 /* assure len is set */
3572 sin->sin_len = sizeof(struct sockaddr_in);
3574 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
3575 stcb->ipv4_local_scope = 1;
3577 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
3578 stcb->asoc.ipv4_local_scope = 1;
3580 #endif /* SCTP_DONT_DO_PRIVADDR_SCOPE */
3582 /* Validate the address is in scope */
3583 if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) &&
3584 (stcb->asoc.ipv4_local_scope == 0)) {
3589 } else if (newaddr->sa_family == AF_INET6) {
3590 struct sockaddr_in6 *sin6;
3592 sin6 = (struct sockaddr_in6 *)newaddr;
3593 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3594 /* Invalid address */
3597 /* assure len is set */
3598 sin6->sin6_len = sizeof(struct sockaddr_in6);
3600 if (sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id)) {
3601 stcb->asoc.loopback_scope = 1;
3602 stcb->asoc.local_scope = 0;
3603 stcb->asoc.ipv4_local_scope = 1;
3604 stcb->asoc.site_scope = 1;
3605 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
3607 * If the new destination is a LINK_LOCAL we
3608 * must have common site scope. Don't set
3609 * the local scope since we may not share
3610 * all links, only loopback can do this.
3611 * Links on the local network would also be
3612 * on our private network for v4 too.
3614 stcb->asoc.ipv4_local_scope = 1;
3615 stcb->asoc.site_scope = 1;
3616 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
3618 * If the new destination is SITE_LOCAL then
3619 * we must have site scope in common.
3621 stcb->asoc.site_scope = 1;
3624 /* Validate the address is in scope */
3625 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) &&
3626 (stcb->asoc.loopback_scope == 0)) {
3628 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) &&
3629 (stcb->asoc.local_scope == 0)) {
3631 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) &&
3632 (stcb->asoc.site_scope == 0)) {
3638 /* not supported family type */
3641 net = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_net), struct sctp_nets);
3645 SCTP_INCR_RADDR_COUNT();
3646 bzero(net, sizeof(*net));
3647 (void)SCTP_GETTIME_TIMEVAL(&net->start_time);
3648 memcpy(&net->ro._l_addr, newaddr, newaddr->sa_len);
3649 if (newaddr->sa_family == AF_INET) {
3650 ((struct sockaddr_in *)&net->ro._l_addr)->sin_port = stcb->rport;
3651 } else if (newaddr->sa_family == AF_INET6) {
3652 ((struct sockaddr_in6 *)&net->ro._l_addr)->sin6_port = stcb->rport;
3654 net->addr_is_local = sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id);
3655 if (net->addr_is_local && ((set_scope || (from == SCTP_ADDR_IS_CONFIRMED)))) {
3656 stcb->asoc.loopback_scope = 1;
3657 stcb->asoc.ipv4_local_scope = 1;
3658 stcb->asoc.local_scope = 0;
3659 stcb->asoc.site_scope = 1;
3662 net->failure_threshold = stcb->asoc.def_net_failure;
3663 if (addr_inscope == 0) {
3664 net->dest_state = (SCTP_ADDR_REACHABLE |
3665 SCTP_ADDR_OUT_OF_SCOPE);
3667 if (from == SCTP_ADDR_IS_CONFIRMED)
3668 /* SCTP_ADDR_IS_CONFIRMED is passed by connect_x */
3669 net->dest_state = SCTP_ADDR_REACHABLE;
3671 net->dest_state = SCTP_ADDR_REACHABLE |
3672 SCTP_ADDR_UNCONFIRMED;
3675 * We set this to 0, the timer code knows that this means its an
3679 net->RTO_measured = 0;
3680 stcb->asoc.numnets++;
3681 *(&net->ref_count) = 1;
3682 net->tos_flowlabel = 0;
3683 if (SCTP_BASE_SYSCTL(sctp_udp_tunneling_for_client_enable)) {
3684 net->port = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
3689 if (newaddr->sa_family == AF_INET)
3690 net->tos_flowlabel = stcb->asoc.default_tos;
3693 if (newaddr->sa_family == AF_INET6)
3694 net->tos_flowlabel = stcb->asoc.default_flowlabel;
3696 /* Init the timer structure */
3697 SCTP_OS_TIMER_INIT(&net->rxt_timer.timer);
3698 SCTP_OS_TIMER_INIT(&net->fr_timer.timer);
3699 SCTP_OS_TIMER_INIT(&net->pmtu_timer.timer);
3701 /* Now generate a route for this guy */
3703 /* KAME hack: embed scopeid */
3704 if (newaddr->sa_family == AF_INET6) {
3705 struct sockaddr_in6 *sin6;
3707 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3708 (void)sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone));
3709 sin6->sin6_scope_id = 0;
3712 SCTP_RTALLOC((sctp_route_t *) & net->ro, stcb->asoc.vrf_id);
3714 if (SCTP_ROUTE_HAS_VALID_IFN(&net->ro)) {
3715 /* Get source address */
3716 net->ro._s_addr = sctp_source_address_selection(stcb->sctp_ep,
3718 (sctp_route_t *) & net->ro,
3722 /* Now get the interface MTU */
3723 if (net->ro._s_addr && net->ro._s_addr->ifn_p) {
3724 net->mtu = SCTP_GATHER_MTU_FROM_INTFC(net->ro._s_addr->ifn_p);
3728 #ifdef SCTP_PRINT_FOR_B_AND_M
3729 SCTP_PRINTF("We have found an interface mtu of %d\n", net->mtu);
3731 if (net->mtu == 0) {
3733 net->mtu = SCTP_DEFAULT_MTU;
3737 rmtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, net->ro.ro_rt);
3738 #ifdef SCTP_PRINT_FOR_B_AND_M
3739 SCTP_PRINTF("The route mtu is %d\n", rmtu);
3743 * Start things off to match mtu of
3746 SCTP_SET_MTU_OF_ROUTE(&net->ro._l_addr.sa,
3747 net->ro.ro_rt, net->mtu);
3750 * we take the route mtu over the interface,
3751 * since the route may be leading out the
3752 * loopback, or a different interface.
3757 if (from == SCTP_ALLOC_ASOC) {
3758 #ifdef SCTP_PRINT_FOR_B_AND_M
3759 SCTP_PRINTF("New assoc sets mtu to :%d\n", net->mtu);
3761 stcb->asoc.smallest_mtu = net->mtu;
3764 net->mtu = stcb->asoc.smallest_mtu;
3767 if (newaddr->sa_family == AF_INET6) {
3768 struct sockaddr_in6 *sin6;
3770 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3771 (void)sa6_recoverscope(sin6);
3775 net->mtu -= sizeof(struct udphdr);
3777 if (stcb->asoc.smallest_mtu > net->mtu) {
3778 #ifdef SCTP_PRINT_FOR_B_AND_M
3779 SCTP_PRINTF("new address mtu:%d smaller than smallest:%d\n",
3780 net->mtu, stcb->asoc.smallest_mtu);
3782 stcb->asoc.smallest_mtu = net->mtu;
3784 /* JRS - Use the congestion control given in the CC module */
3785 stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
3788 * CMT: CUC algo - set find_pseudo_cumack to TRUE (1) at beginning
3789 * of assoc (2005/06/27, iyengar@cis.udel.edu)
3791 net->find_pseudo_cumack = 1;
3792 net->find_rtx_pseudo_cumack = 1;
3793 net->src_addr_selected = 0;
3794 netfirst = TAILQ_FIRST(&stcb->asoc.nets);
3795 if (net->ro.ro_rt == NULL) {
3796 /* Since we have no route put it at the back */
3797 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
3798 } else if (netfirst == NULL) {
3799 /* We are the first one in the pool. */
3800 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
3801 } else if (netfirst->ro.ro_rt == NULL) {
3803 * First one has NO route. Place this one ahead of the first
3806 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
3807 } else if (net->ro.ro_rt->rt_ifp != netfirst->ro.ro_rt->rt_ifp) {
3809 * This one has a different interface than the one at the
3810 * top of the list. Place it ahead.
3812 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
3815 * Ok we have the same interface as the first one. Move
3816 * forward until we find either a) one with a NULL route...
3817 * insert ahead of that b) one with a different ifp.. insert
3818 * after that. c) end of the list.. insert at the tail.
3820 struct sctp_nets *netlook;
3823 netlook = TAILQ_NEXT(netfirst, sctp_next);
3824 if (netlook == NULL) {
3825 /* End of the list */
3826 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
3828 } else if (netlook->ro.ro_rt == NULL) {
3829 /* next one has NO route */
3830 TAILQ_INSERT_BEFORE(netfirst, net, sctp_next);
3832 } else if (netlook->ro.ro_rt->rt_ifp != net->ro.ro_rt->rt_ifp) {
3833 TAILQ_INSERT_AFTER(&stcb->asoc.nets, netlook,
3839 } while (netlook != NULL);
3842 /* got to have a primary set */
3843 if (stcb->asoc.primary_destination == 0) {
3844 stcb->asoc.primary_destination = net;
3845 } else if ((stcb->asoc.primary_destination->ro.ro_rt == NULL) &&
3847 ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) {
3848 /* No route to current primary adopt new primary */
3849 stcb->asoc.primary_destination = net;
3851 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb,
3853 /* Validate primary is first */
3854 net = TAILQ_FIRST(&stcb->asoc.nets);
3855 if ((net != stcb->asoc.primary_destination) &&
3856 (stcb->asoc.primary_destination)) {
3858 * first one on the list is NOT the primary sctp_cmpaddr()
3859 * is much more efficent if the primary is the first on the
3862 TAILQ_REMOVE(&stcb->asoc.nets,
3863 stcb->asoc.primary_destination, sctp_next);
3864 TAILQ_INSERT_HEAD(&stcb->asoc.nets,
3865 stcb->asoc.primary_destination, sctp_next);
3872 * allocate an association and add it to the endpoint. The caller must be
3873 * careful to add all additional addresses once they are know right away or
3874 * else the assoc will be may experience a blackout scenario.
3877 sctp_aloc_assoc(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
3878 int for_a_init, int *error, uint32_t override_tag, uint32_t vrf_id,
3882 /* note the p argument is only valid in unbound sockets */
3884 struct sctp_tcb *stcb;
3885 struct sctp_association *asoc;
3886 struct sctpasochead *head;
3891 * Assumption made here: Caller has done a
3892 * sctp_findassociation_ep_addr(ep, addr's); to make sure the
3893 * address does not exist already.
3895 if (SCTP_BASE_INFO(ipi_count_asoc) >= SCTP_MAX_NUM_OF_ASOC) {
3896 /* Hit max assoc, sorry no more */
3897 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
3901 if (firstaddr == NULL) {
3902 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3906 SCTP_INP_RLOCK(inp);
3907 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
3908 ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) ||
3909 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
3911 * If its in the TCP pool, its NOT allowed to create an
3912 * association. The parent listener needs to call
3913 * sctp_aloc_assoc.. or the one-2-many socket. If a peeled
3914 * off, or connected one does this.. its an error.
3916 SCTP_INP_RUNLOCK(inp);
3917 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3921 SCTPDBG(SCTP_DEBUG_PCB3, "Allocate an association for peer:");
3924 SCTPDBG_ADDR(SCTP_DEBUG_PCB3, firstaddr);
3925 SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
3926 ntohs(((struct sockaddr_in *)firstaddr)->sin_port));
3928 SCTPDBG(SCTP_DEBUG_PCB3, "None\n");
3930 #endif /* SCTP_DEBUG */
3931 if (firstaddr->sa_family == AF_INET) {
3932 struct sockaddr_in *sin;
3934 sin = (struct sockaddr_in *)firstaddr;
3935 if ((sin->sin_port == 0) || (sin->sin_addr.s_addr == 0)) {
3936 /* Invalid address */
3937 SCTP_INP_RUNLOCK(inp);
3938 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3942 rport = sin->sin_port;
3943 } else if (firstaddr->sa_family == AF_INET6) {
3944 struct sockaddr_in6 *sin6;
3946 sin6 = (struct sockaddr_in6 *)firstaddr;
3947 if ((sin6->sin6_port == 0) ||
3948 (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))) {
3949 /* Invalid address */
3950 SCTP_INP_RUNLOCK(inp);
3951 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3955 rport = sin6->sin6_port;
3957 /* not supported family type */
3958 SCTP_INP_RUNLOCK(inp);
3959 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3963 SCTP_INP_RUNLOCK(inp);
3964 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
3966 * If you have not performed a bind, then we need to do the
3967 * ephemerial bind for you.
3969 if ((err = sctp_inpcb_bind(inp->sctp_socket,
3970 (struct sockaddr *)NULL,
3971 (struct sctp_ifa *)NULL,
3974 /* bind error, probably perm */
3979 stcb = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_asoc), struct sctp_tcb);
3981 /* out of memory? */
3982 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
3986 SCTP_INCR_ASOC_COUNT();
3988 bzero(stcb, sizeof(*stcb));
3990 SCTP_TCB_LOCK_INIT(stcb);
3991 SCTP_TCB_SEND_LOCK_INIT(stcb);
3992 /* setup back pointer's */
3993 stcb->sctp_ep = inp;
3994 stcb->sctp_socket = inp->sctp_socket;
3995 if ((err = sctp_init_asoc(inp, stcb, for_a_init, override_tag, vrf_id))) {
3997 SCTP_TCB_LOCK_DESTROY(stcb);
3998 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
3999 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
4000 SCTP_DECR_ASOC_COUNT();
4005 stcb->rport = rport;
4006 SCTP_INP_INFO_WLOCK();
4007 SCTP_INP_WLOCK(inp);
4008 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4009 /* inpcb freed while alloc going on */
4010 SCTP_TCB_LOCK_DESTROY(stcb);
4011 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
4012 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
4013 SCTP_INP_WUNLOCK(inp);
4014 SCTP_INP_INFO_WUNLOCK();
4015 SCTP_DECR_ASOC_COUNT();
4016 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4020 SCTP_TCB_LOCK(stcb);
4022 /* now that my_vtag is set, add it to the hash */
4023 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
4024 SCTP_BASE_INFO(hashasocmark))];
4025 /* put it in the bucket in the vtag hash of assoc's for the system */
4026 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
4027 sctp_delete_from_timewait(stcb->asoc.my_vtag);
4029 SCTP_INP_INFO_WUNLOCK();
4031 if ((err = sctp_add_remote_addr(stcb, firstaddr, SCTP_DO_SETSCOPE, SCTP_ALLOC_ASOC))) {
4032 /* failure.. memory error? */
4033 if (asoc->strmout) {
4034 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
4035 asoc->strmout = NULL;
4037 if (asoc->mapping_array) {
4038 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
4039 asoc->mapping_array = NULL;
4041 SCTP_DECR_ASOC_COUNT();
4042 SCTP_TCB_LOCK_DESTROY(stcb);
4043 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
4044 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
4045 SCTP_INP_WUNLOCK(inp);
4046 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
4050 /* Init all the timers */
4051 SCTP_OS_TIMER_INIT(&asoc->hb_timer.timer);
4052 SCTP_OS_TIMER_INIT(&asoc->dack_timer.timer);
4053 SCTP_OS_TIMER_INIT(&asoc->strreset_timer.timer);
4054 SCTP_OS_TIMER_INIT(&asoc->asconf_timer.timer);
4055 SCTP_OS_TIMER_INIT(&asoc->shut_guard_timer.timer);
4056 SCTP_OS_TIMER_INIT(&asoc->autoclose_timer.timer);
4057 SCTP_OS_TIMER_INIT(&asoc->delayed_event_timer.timer);
4058 SCTP_OS_TIMER_INIT(&asoc->delete_prim_timer.timer);
4060 LIST_INSERT_HEAD(&inp->sctp_asoc_list, stcb, sctp_tcblist);
4061 /* now file the port under the hash as well */
4062 if (inp->sctp_tcbhash != NULL) {
4063 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(stcb->rport,
4064 inp->sctp_hashmark)];
4065 LIST_INSERT_HEAD(head, stcb, sctp_tcbhash);
4067 SCTP_INP_WUNLOCK(inp);
4068 SCTPDBG(SCTP_DEBUG_PCB1, "Association %p now allocated\n", stcb);
4074 sctp_remove_net(struct sctp_tcb *stcb, struct sctp_nets *net)
4076 struct sctp_association *asoc;
4080 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
4081 if (net == asoc->primary_destination) {
4083 struct sctp_nets *lnet;
4085 lnet = TAILQ_FIRST(&asoc->nets);
4087 * Mobility adaptation Ideally, if deleted destination is
4088 * the primary, it becomes a fast retransmission trigger by
4089 * the subsequent SET PRIMARY. (by micchie)
4091 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
4092 SCTP_MOBILITY_BASE) ||
4093 sctp_is_mobility_feature_on(stcb->sctp_ep,
4094 SCTP_MOBILITY_FASTHANDOFF)) {
4095 SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: primary dst is deleting\n");
4096 if (asoc->deleted_primary != NULL) {
4097 SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: deleted primary may be already stored\n");
4100 asoc->deleted_primary = net;
4101 atomic_add_int(&net->ref_count, 1);
4102 memset(&net->lastsa, 0, sizeof(net->lastsa));
4103 memset(&net->lastsv, 0, sizeof(net->lastsv));
4104 sctp_mobility_feature_on(stcb->sctp_ep,
4105 SCTP_MOBILITY_PRIM_DELETED);
4106 sctp_timer_start(SCTP_TIMER_TYPE_PRIM_DELETED,
4107 stcb->sctp_ep, stcb, NULL);
4110 /* Try to find a confirmed primary */
4111 asoc->primary_destination = sctp_find_alternate_net(stcb, lnet, 0);
4113 if (net == asoc->last_data_chunk_from) {
4115 asoc->last_data_chunk_from = TAILQ_FIRST(&asoc->nets);
4117 if (net == asoc->last_control_chunk_from) {
4119 asoc->last_control_chunk_from = NULL;
4121 sctp_free_remote_addr(net);
4125 * remove a remote endpoint address from an association, it will fail if the
4126 * address does not exist.
4129 sctp_del_remote_addr(struct sctp_tcb *stcb, struct sockaddr *remaddr)
4132 * Here we need to remove a remote address. This is quite simple, we
4133 * first find it in the list of address for the association
4134 * (tasoc->asoc.nets) and then if it is there, we do a LIST_REMOVE
4135 * on that item. Note we do not allow it to be removed if there are
4136 * no other addresses.
4138 struct sctp_association *asoc;
4139 struct sctp_nets *net, *net_tmp;
4143 /* locate the address */
4144 for (net = TAILQ_FIRST(&asoc->nets); net != NULL; net = net_tmp) {
4145 net_tmp = TAILQ_NEXT(net, sctp_next);
4146 if (net->ro._l_addr.sa.sa_family != remaddr->sa_family) {
4149 if (sctp_cmpaddr((struct sockaddr *)&net->ro._l_addr,
4151 /* we found the guy */
4152 if (asoc->numnets < 2) {
4153 /* Must have at LEAST two remote addresses */
4156 sctp_remove_net(stcb, net);
4166 sctp_delete_from_timewait(uint32_t tag)
4168 struct sctpvtaghead *chain;
4169 struct sctp_tagblock *twait_block;
4173 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
4174 if (!SCTP_LIST_EMPTY(chain)) {
4175 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
4176 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
4177 if (twait_block->vtag_block[i].v_tag == tag) {
4178 twait_block->vtag_block[i].tv_sec_at_expire = 0;
4179 twait_block->vtag_block[i].v_tag = 0;
4191 sctp_is_in_timewait(uint32_t tag)
4193 struct sctpvtaghead *chain;
4194 struct sctp_tagblock *twait_block;
4198 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
4199 if (!SCTP_LIST_EMPTY(chain)) {
4200 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
4201 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
4202 if (twait_block->vtag_block[i].v_tag == tag) {
4216 sctp_add_vtag_to_timewait(uint32_t tag, uint32_t time)
4218 struct sctpvtaghead *chain;
4219 struct sctp_tagblock *twait_block;
4223 (void)SCTP_GETTIME_TIMEVAL(&now);
4224 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
4226 if (!SCTP_LIST_EMPTY(chain)) {
4227 /* Block(s) present, lets find space, and expire on the fly */
4228 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
4229 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
4230 if ((twait_block->vtag_block[i].v_tag == 0) &&
4232 twait_block->vtag_block[i].tv_sec_at_expire =
4234 twait_block->vtag_block[i].v_tag = tag;
4236 } else if ((twait_block->vtag_block[i].v_tag) &&
4237 ((long)twait_block->vtag_block[i].tv_sec_at_expire < now.tv_sec)) {
4238 /* Audit expires this guy */
4239 twait_block->vtag_block[i].tv_sec_at_expire = 0;
4240 twait_block->vtag_block[i].v_tag = 0;
4242 /* Reuse it for my new tag */
4243 twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec + time;
4244 twait_block->vtag_block[0].v_tag = tag;
4251 * We only do up to the block where we can
4252 * place our tag for audits
4258 /* Need to add a new block to chain */
4260 SCTP_MALLOC(twait_block, struct sctp_tagblock *,
4261 sizeof(struct sctp_tagblock), SCTP_M_TIMW);
4262 if (twait_block == NULL) {
4265 memset(twait_block, 0, sizeof(struct sctp_tagblock));
4266 LIST_INSERT_HEAD(chain, twait_block, sctp_nxt_tagblock);
4267 twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec + time;
4268 twait_block->vtag_block[0].v_tag = tag;
4274 sctp_iterator_asoc_being_freed(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
4276 struct sctp_iterator *it;
4279 * Unlock the tcb lock we do this so we avoid a dead lock scenario
4280 * where the iterator is waiting on the TCB lock and the TCB lock is
4281 * waiting on the iterator lock.
4283 it = stcb->asoc.stcb_starting_point_for_iterator;
4287 if (it->inp != stcb->sctp_ep) {
4288 /* hmm, focused on the wrong one? */
4291 if (it->stcb != stcb) {
4294 it->stcb = LIST_NEXT(stcb, sctp_tcblist);
4295 if (it->stcb == NULL) {
4296 /* done with all asoc's in this assoc */
4297 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
4300 it->inp = LIST_NEXT(inp, sctp_list);
4307 * Free the association after un-hashing the remote port. This
4308 * function ALWAYS returns holding NO LOCK on the stcb. It DOES
4309 * expect that the input to this function IS a locked TCB.
4310 * It will return 0, if it did NOT destroy the association (instead
4311 * it unlocks it. It will return NON-zero if it either destroyed the
4312 * association OR the association is already destroyed.
4315 sctp_free_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int from_inpcbfree, int from_location)
4318 struct sctp_association *asoc;
4319 struct sctp_nets *net, *prev;
4320 struct sctp_laddr *laddr;
4321 struct sctp_tmit_chunk *chk;
4322 struct sctp_asconf_addr *aparam;
4323 struct sctp_asconf_ack *aack;
4324 struct sctp_stream_reset_list *liste;
4325 struct sctp_queued_to_read *sq;
4326 struct sctp_stream_queue_pending *sp;
4327 sctp_sharedkey_t *shared_key;
4332 /* first, lets purge the entry from the hash table. */
4334 #ifdef SCTP_LOG_CLOSING
4335 sctp_log_closing(inp, stcb, 6);
4337 if (stcb->asoc.state == 0) {
4338 #ifdef SCTP_LOG_CLOSING
4339 sctp_log_closing(inp, NULL, 7);
4341 /* there is no asoc, really TSNH :-0 */
4345 if (stcb->freed_from_where == 0) {
4346 /* Only record the first place free happened from */
4347 stcb->freed_from_where = from_location;
4352 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4353 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
4354 /* nothing around */
4357 so = inp->sctp_socket;
4360 * We used timer based freeing if a reader or writer is in the way.
4361 * So we first check if we are actually being called from a timer,
4362 * if so we abort early if a reader or writer is still in the way.
4364 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) &&
4365 (from_inpcbfree == SCTP_NORMAL_PROC)) {
4367 * is it the timer driving us? if so are the reader/writers
4370 if (stcb->asoc.refcnt) {
4371 /* nope, reader or writer in the way */
4372 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
4373 /* no asoc destroyed */
4374 SCTP_TCB_UNLOCK(stcb);
4375 #ifdef SCTP_LOG_CLOSING
4376 sctp_log_closing(inp, stcb, 8);
4381 /* now clean up any other timers */
4382 (void)SCTP_OS_TIMER_STOP(&asoc->hb_timer.timer);
4383 asoc->hb_timer.self = NULL;
4384 (void)SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
4385 asoc->dack_timer.self = NULL;
4386 (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
4388 * For stream reset we don't blast this unless
4389 * it is a str-reset timer, it might be the
4390 * free-asoc timer which we DON'T want to
4393 if (asoc->strreset_timer.type == SCTP_TIMER_TYPE_STRRESET)
4394 asoc->strreset_timer.self = NULL;
4395 (void)SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
4396 asoc->asconf_timer.self = NULL;
4397 (void)SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
4398 asoc->autoclose_timer.self = NULL;
4399 (void)SCTP_OS_TIMER_STOP(&asoc->shut_guard_timer.timer);
4400 asoc->shut_guard_timer.self = NULL;
4401 (void)SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
4402 asoc->delayed_event_timer.self = NULL;
4403 /* Mobility adaptation */
4404 (void)SCTP_OS_TIMER_STOP(&asoc->delete_prim_timer.timer);
4405 asoc->delete_prim_timer.self = NULL;
4406 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4407 (void)SCTP_OS_TIMER_STOP(&net->fr_timer.timer);
4408 net->fr_timer.self = NULL;
4409 (void)SCTP_OS_TIMER_STOP(&net->rxt_timer.timer);
4410 net->rxt_timer.self = NULL;
4411 (void)SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
4412 net->pmtu_timer.self = NULL;
4414 /* Now the read queue needs to be cleaned up (only once) */
4416 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0) {
4417 stcb->asoc.state |= SCTP_STATE_ABOUT_TO_BE_FREED;
4418 SCTP_INP_READ_LOCK(inp);
4419 TAILQ_FOREACH(sq, &inp->read_queue, next) {
4420 if (sq->stcb == stcb) {
4421 sq->do_not_ref_stcb = 1;
4422 sq->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
4424 * If there is no end, there never will be
4427 if (sq->end_added == 0) {
4428 /* Held for PD-API clear that. */
4429 sq->pdapi_aborted = 1;
4430 sq->held_length = 0;
4431 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT) && (so != NULL)) {
4433 * Need to add a PD-API
4434 * aborted indication.
4435 * Setting the control_pdapi
4436 * assures that it will be
4437 * added right after this
4442 stcb->asoc.control_pdapi = sq;
4443 strseq = (sq->sinfo_stream << 16) | sq->sinfo_ssn;
4444 sctp_notify_partial_delivery_indication(stcb,
4445 SCTP_PARTIAL_DELIVERY_ABORTED, 1, strseq);
4446 stcb->asoc.control_pdapi = NULL;
4449 /* Add an end to wake them */
4454 SCTP_INP_READ_UNLOCK(inp);
4455 if (stcb->block_entry) {
4457 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PCB, ECONNRESET);
4458 stcb->block_entry->error = ECONNRESET;
4459 stcb->block_entry = NULL;
4462 if (stcb->asoc.refcnt) {
4464 * reader or writer in the way, we have hopefully given him
4465 * something to chew on above.
4467 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
4468 SCTP_TCB_UNLOCK(stcb);
4469 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4470 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
4471 /* nothing around */
4474 /* Wake any reader/writers */
4475 sctp_sorwakeup(inp, so);
4476 sctp_sowwakeup(inp, so);
4478 #ifdef SCTP_LOG_CLOSING
4479 sctp_log_closing(inp, stcb, 9);
4481 /* no asoc destroyed */
4484 #ifdef SCTP_LOG_CLOSING
4485 sctp_log_closing(inp, stcb, 10);
4488 * When I reach here, no others want to kill the assoc yet.. and I
4489 * own the lock. Now its possible an abort comes in when I do the
4490 * lock exchange below to grab all the locks to do the final take
4491 * out. to prevent this we increment the count, which will start a
4492 * timer and blow out above thus assuring us that we hold exclusive
4493 * killing of the asoc. Note that after getting back the TCB lock we
4494 * will go ahead and increment the counter back up and stop any
4495 * timer a passing stranger may have started :-S
4497 if (from_inpcbfree == SCTP_NORMAL_PROC) {
4498 atomic_add_int(&stcb->asoc.refcnt, 1);
4500 SCTP_TCB_UNLOCK(stcb);
4502 SCTP_ITERATOR_LOCK();
4503 SCTP_INP_INFO_WLOCK();
4504 SCTP_INP_WLOCK(inp);
4505 SCTP_TCB_LOCK(stcb);
4507 /* Double check the GONE flag */
4508 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4509 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
4510 /* nothing around */
4513 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4514 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
4516 * For TCP type we need special handling when we are
4517 * connected. We also include the peel'ed off ones to.
4519 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4520 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
4521 inp->sctp_flags |= SCTP_PCB_FLAGS_WAS_CONNECTED;
4524 if (so->so_rcv.sb_cc == 0) {
4525 so->so_state &= ~(SS_ISCONNECTING |
4526 SS_ISDISCONNECTING |
4532 sctp_sowwakeup(inp, so);
4533 sctp_sorwakeup(inp, so);
4539 * Make it invalid too, that way if its about to run it will abort
4542 sctp_iterator_asoc_being_freed(inp, stcb);
4543 /* re-increment the lock */
4544 if (from_inpcbfree == SCTP_NORMAL_PROC) {
4545 atomic_add_int(&stcb->asoc.refcnt, -1);
4548 if (inp->sctp_tcbhash) {
4549 LIST_REMOVE(stcb, sctp_tcbhash);
4551 if (stcb->asoc.in_restart_hash) {
4552 LIST_REMOVE(stcb, sctp_tcbrestarhash);
4554 /* Now lets remove it from the list of ALL associations in the EP */
4555 LIST_REMOVE(stcb, sctp_tcblist);
4556 if (from_inpcbfree == SCTP_NORMAL_PROC) {
4557 SCTP_INP_INCR_REF(inp);
4558 SCTP_INP_WUNLOCK(inp);
4559 SCTP_ITERATOR_UNLOCK();
4561 /* pull from vtag hash */
4562 LIST_REMOVE(stcb, sctp_asocs);
4563 sctp_add_vtag_to_timewait(asoc->my_vtag, SCTP_TIME_WAIT);
4566 * Now restop the timers to be sure - this is paranoia at is finest!
4568 (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
4569 (void)SCTP_OS_TIMER_STOP(&asoc->hb_timer.timer);
4570 (void)SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
4571 (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
4572 (void)SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
4573 (void)SCTP_OS_TIMER_STOP(&asoc->shut_guard_timer.timer);
4574 (void)SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
4575 (void)SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
4576 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4577 (void)SCTP_OS_TIMER_STOP(&net->fr_timer.timer);
4578 (void)SCTP_OS_TIMER_STOP(&net->rxt_timer.timer);
4579 (void)SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
4582 asoc->strreset_timer.type = SCTP_TIMER_TYPE_NONE;
4585 * The chunk lists and such SHOULD be empty but we check them just
4588 /* anything on the wheel needs to be removed */
4589 for (i = 0; i < asoc->streamoutcnt; i++) {
4590 struct sctp_stream_out *outs;
4592 outs = &asoc->strmout[i];
4593 /* now clean up any chunks here */
4594 sp = TAILQ_FIRST(&outs->outqueue);
4596 TAILQ_REMOVE(&outs->outqueue, sp, next);
4598 sctp_m_freem(sp->data);
4600 sp->tail_mbuf = NULL;
4602 sctp_free_remote_addr(sp->net);
4603 sctp_free_spbufspace(stcb, asoc, sp);
4604 /* Free the zone stuff */
4605 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_strmoq), sp);
4606 SCTP_DECR_STRMOQ_COUNT();
4607 /* sa_ignore FREED_MEMORY */
4608 sp = TAILQ_FIRST(&outs->outqueue);
4612 /* sa_ignore FREED_MEMORY */
4613 while ((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) {
4614 TAILQ_REMOVE(&asoc->resetHead, liste, next_resp);
4615 SCTP_FREE(liste, SCTP_M_STRESET);
4618 sq = TAILQ_FIRST(&asoc->pending_reply_queue);
4620 TAILQ_REMOVE(&asoc->pending_reply_queue, sq, next);
4622 sctp_m_freem(sq->data);
4625 sctp_free_remote_addr(sq->whoFrom);
4628 /* Free the ctl entry */
4629 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), sq);
4630 SCTP_DECR_READQ_COUNT();
4631 /* sa_ignore FREED_MEMORY */
4632 sq = TAILQ_FIRST(&asoc->pending_reply_queue);
4635 chk = TAILQ_FIRST(&asoc->free_chunks);
4637 TAILQ_REMOVE(&asoc->free_chunks, chk, sctp_next);
4639 sctp_m_freem(chk->data);
4643 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
4644 SCTP_DECR_CHK_COUNT();
4645 atomic_subtract_int(&SCTP_BASE_INFO(ipi_free_chunks), 1);
4646 asoc->free_chunk_cnt--;
4647 /* sa_ignore FREED_MEMORY */
4648 chk = TAILQ_FIRST(&asoc->free_chunks);
4650 /* pending send queue SHOULD be empty */
4651 if (!TAILQ_EMPTY(&asoc->send_queue)) {
4652 chk = TAILQ_FIRST(&asoc->send_queue);
4654 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
4656 sctp_m_freem(chk->data);
4660 sctp_free_remote_addr(chk->whoTo);
4661 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
4662 SCTP_DECR_CHK_COUNT();
4663 /* sa_ignore FREED_MEMORY */
4664 chk = TAILQ_FIRST(&asoc->send_queue);
4669 printf("Freed %d from send_queue\n", ccnt);
4673 /* sent queue SHOULD be empty */
4674 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
4675 chk = TAILQ_FIRST(&asoc->sent_queue);
4677 TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
4679 sctp_m_freem(chk->data);
4683 sctp_free_remote_addr(chk->whoTo);
4684 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
4685 SCTP_DECR_CHK_COUNT();
4686 /* sa_ignore FREED_MEMORY */
4687 chk = TAILQ_FIRST(&asoc->sent_queue);
4692 printf("Freed %d from sent_queue\n", ccnt);
4696 /* control queue MAY not be empty */
4697 if (!TAILQ_EMPTY(&asoc->control_send_queue)) {
4698 chk = TAILQ_FIRST(&asoc->control_send_queue);
4700 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
4702 sctp_m_freem(chk->data);
4706 sctp_free_remote_addr(chk->whoTo);
4707 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
4708 SCTP_DECR_CHK_COUNT();
4709 /* sa_ignore FREED_MEMORY */
4710 chk = TAILQ_FIRST(&asoc->control_send_queue);
4715 printf("Freed %d from ctrl_queue\n", ccnt);
4720 /* ASCONF queue MAY not be empty */
4721 if (!TAILQ_EMPTY(&asoc->asconf_send_queue)) {
4722 chk = TAILQ_FIRST(&asoc->asconf_send_queue);
4724 TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
4726 sctp_m_freem(chk->data);
4730 sctp_free_remote_addr(chk->whoTo);
4731 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
4732 SCTP_DECR_CHK_COUNT();
4733 /* sa_ignore FREED_MEMORY */
4734 chk = TAILQ_FIRST(&asoc->asconf_send_queue);
4739 printf("Freed %d from asconf_queue\n", ccnt);
4743 if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
4744 chk = TAILQ_FIRST(&asoc->reasmqueue);
4746 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
4748 sctp_m_freem(chk->data);
4751 sctp_free_remote_addr(chk->whoTo);
4753 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
4754 SCTP_DECR_CHK_COUNT();
4755 /* sa_ignore FREED_MEMORY */
4756 chk = TAILQ_FIRST(&asoc->reasmqueue);
4761 printf("Freed %d from reasm_queue\n", ccnt);
4765 if (asoc->mapping_array) {
4766 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
4767 asoc->mapping_array = NULL;
4769 /* the stream outs */
4770 if (asoc->strmout) {
4771 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
4772 asoc->strmout = NULL;
4774 asoc->streamoutcnt = 0;
4776 struct sctp_queued_to_read *ctl;
4778 for (i = 0; i < asoc->streamincnt; i++) {
4779 if (!TAILQ_EMPTY(&asoc->strmin[i].inqueue)) {
4780 /* We have somethings on the streamin queue */
4781 ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue);
4783 TAILQ_REMOVE(&asoc->strmin[i].inqueue,
4785 sctp_free_remote_addr(ctl->whoFrom);
4787 sctp_m_freem(ctl->data);
4791 * We don't free the address here
4792 * since all the net's were freed
4795 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), ctl);
4796 SCTP_DECR_READQ_COUNT();
4797 ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue);
4801 SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
4802 asoc->strmin = NULL;
4804 asoc->streamincnt = 0;
4805 while (!TAILQ_EMPTY(&asoc->nets)) {
4806 /* sa_ignore FREED_MEMORY */
4807 net = TAILQ_FIRST(&asoc->nets);
4808 /* pull from list */
4809 if ((SCTP_BASE_INFO(ipi_count_raddr) == 0) || (prev == net)) {
4811 panic("no net's left alloc'ed, or list points to itself");
4816 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
4817 sctp_free_remote_addr(net);
4820 while (!SCTP_LIST_EMPTY(&asoc->sctp_restricted_addrs)) {
4821 /* sa_ignore FREED_MEMORY */
4822 laddr = LIST_FIRST(&asoc->sctp_restricted_addrs);
4823 sctp_remove_laddr(laddr);
4826 /* pending asconf (address) parameters */
4827 while (!TAILQ_EMPTY(&asoc->asconf_queue)) {
4828 /* sa_ignore FREED_MEMORY */
4829 aparam = TAILQ_FIRST(&asoc->asconf_queue);
4830 TAILQ_REMOVE(&asoc->asconf_queue, aparam, next);
4831 SCTP_FREE(aparam, SCTP_M_ASC_ADDR);
4833 while (!TAILQ_EMPTY(&asoc->asconf_ack_sent)) {
4834 /* sa_ignore FREED_MEMORY */
4835 aack = TAILQ_FIRST(&asoc->asconf_ack_sent);
4836 TAILQ_REMOVE(&asoc->asconf_ack_sent, aack, next);
4837 if (aack->data != NULL) {
4838 sctp_m_freem(aack->data);
4840 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asconf_ack), aack);
4842 /* clean up auth stuff */
4843 if (asoc->local_hmacs)
4844 sctp_free_hmaclist(asoc->local_hmacs);
4845 if (asoc->peer_hmacs)
4846 sctp_free_hmaclist(asoc->peer_hmacs);
4848 if (asoc->local_auth_chunks)
4849 sctp_free_chunklist(asoc->local_auth_chunks);
4850 if (asoc->peer_auth_chunks)
4851 sctp_free_chunklist(asoc->peer_auth_chunks);
4853 sctp_free_authinfo(&asoc->authinfo);
4855 shared_key = LIST_FIRST(&asoc->shared_keys);
4856 while (shared_key) {
4857 LIST_REMOVE(shared_key, next);
4858 sctp_free_sharedkey(shared_key);
4859 /* sa_ignore FREED_MEMORY */
4860 shared_key = LIST_FIRST(&asoc->shared_keys);
4863 /* Insert new items here :> */
4865 /* Get rid of LOCK */
4866 SCTP_TCB_LOCK_DESTROY(stcb);
4867 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
4868 if (from_inpcbfree == SCTP_NORMAL_PROC) {
4869 SCTP_INP_INFO_WUNLOCK();
4870 SCTP_INP_RLOCK(inp);
4872 #ifdef SCTP_TRACK_FREED_ASOCS
4873 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4874 /* now clean up the tasoc itself */
4875 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
4876 SCTP_DECR_ASOC_COUNT();
4878 LIST_INSERT_HEAD(&inp->sctp_asoc_free_list, stcb, sctp_tcblist);
4881 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
4882 SCTP_DECR_ASOC_COUNT();
4884 if (from_inpcbfree == SCTP_NORMAL_PROC) {
4885 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4887 * If its NOT the inp_free calling us AND sctp_close
4888 * as been called, we call back...
4890 SCTP_INP_RUNLOCK(inp);
4892 * This will start the kill timer (if we are the
4893 * lastone) since we hold an increment yet. But this
4894 * is the only safe way to do this since otherwise
4895 * if the socket closes at the same time we are here
4896 * we might collide in the cleanup.
4898 sctp_inpcb_free(inp,
4899 SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
4900 SCTP_CALLED_DIRECTLY_NOCMPSET);
4901 SCTP_INP_DECR_REF(inp);
4904 /* The socket is still open. */
4905 SCTP_INP_DECR_REF(inp);
4908 if (from_inpcbfree == SCTP_NORMAL_PROC) {
4909 SCTP_INP_RUNLOCK(inp);
4912 /* destroyed the asoc */
4913 #ifdef SCTP_LOG_CLOSING
4914 sctp_log_closing(inp, NULL, 11);
4922 * determine if a destination is "reachable" based upon the addresses bound
4923 * to the current endpoint (e.g. only v4 or v6 currently bound)
4926 * FIX: if we allow assoc-level bindx(), then this needs to be fixed to use
4927 * assoc level v4/v6 flags, as the assoc *may* not have the same address
4928 * types bound as its endpoint
4931 sctp_destination_is_reachable(struct sctp_tcb *stcb, struct sockaddr *destaddr)
4933 struct sctp_inpcb *inp;
4937 * No locks here, the TCB, in all cases is already locked and an
4938 * assoc is up. There is either a INP lock by the caller applied (in
4939 * asconf case when deleting an address) or NOT in the HB case,
4940 * however if HB then the INP increment is up and the INP will not
4941 * be removed (on top of the fact that we have a TCB lock). So we
4942 * only want to read the sctp_flags, which is either bound-all or
4943 * not.. no protection needed since once an assoc is up you can't be
4944 * changing your binding.
4946 inp = stcb->sctp_ep;
4947 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4948 /* if bound all, destination is not restricted */
4950 * RRS: Question during lock work: Is this correct? If you
4951 * are bound-all you still might need to obey the V4--V6
4952 * flags??? IMO this bound-all stuff needs to be removed!
4956 /* NOTE: all "scope" checks are done when local addresses are added */
4957 if (destaddr->sa_family == AF_INET6) {
4958 answer = inp->ip_inp.inp.inp_vflag & INP_IPV6;
4959 } else if (destaddr->sa_family == AF_INET) {
4960 answer = inp->ip_inp.inp.inp_vflag & INP_IPV4;
4962 /* invalid family, so it's unreachable */
4969 * update the inp_vflags on an endpoint
4972 sctp_update_ep_vflag(struct sctp_inpcb *inp)
4974 struct sctp_laddr *laddr;
4976 /* first clear the flag */
4977 inp->ip_inp.inp.inp_vflag = 0;
4978 /* set the flag based on addresses on the ep list */
4979 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4980 if (laddr->ifa == NULL) {
4981 SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n",
4985 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
4988 if (laddr->ifa->address.sa.sa_family == AF_INET6) {
4989 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
4990 } else if (laddr->ifa->address.sa.sa_family == AF_INET) {
4991 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
4997 * Add the address to the endpoint local address list There is nothing to be
4998 * done if we are bound to all addresses
5001 sctp_add_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa, uint32_t action)
5003 struct sctp_laddr *laddr;
5008 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
5009 /* You are already bound to all. You have it already */
5012 if (ifa->address.sa.sa_family == AF_INET6) {
5013 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
5014 /* Can't bind a non-useable addr. */
5018 /* first, is it already present? */
5019 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
5020 if (laddr->ifa == ifa) {
5027 /* Not in the ep list */
5028 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, action);
5032 /* update inp_vflag flags */
5033 if (ifa->address.sa.sa_family == AF_INET6) {
5034 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
5035 } else if (ifa->address.sa.sa_family == AF_INET) {
5036 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
5044 * select a new (hopefully reachable) destination net (should only be used
5045 * when we deleted an ep addr that is the only usable source address to reach
5046 * the destination net)
5049 sctp_select_primary_destination(struct sctp_tcb *stcb)
5051 struct sctp_nets *net;
5053 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5054 /* for now, we'll just pick the first reachable one we find */
5055 if (net->dest_state & SCTP_ADDR_UNCONFIRMED)
5057 if (sctp_destination_is_reachable(stcb,
5058 (struct sockaddr *)&net->ro._l_addr)) {
5059 /* found a reachable destination */
5060 stcb->asoc.primary_destination = net;
5063 /* I can't there from here! ...we're gonna die shortly... */
5068 * Delete the address from the endpoint local address list There is nothing
5069 * to be done if we are bound to all addresses
5072 sctp_del_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
5074 struct sctp_laddr *laddr;
5078 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
5079 /* You are already bound to all. You have it already */
5082 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
5083 if (laddr->ifa == ifa) {
5088 if (fnd && (inp->laddr_count < 2)) {
5089 /* can't delete unless there are at LEAST 2 addresses */
5094 * clean up any use of this address go through our
5095 * associations and clear any last_used_address that match
5096 * this one for each assoc, see if a new primary_destination
5099 struct sctp_tcb *stcb;
5101 /* clean up "next_addr_touse" */
5102 if (inp->next_addr_touse == laddr)
5103 /* delete this address */
5104 inp->next_addr_touse = NULL;
5106 /* clean up "last_used_address" */
5107 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5108 struct sctp_nets *net;
5110 SCTP_TCB_LOCK(stcb);
5111 if (stcb->asoc.last_used_address == laddr)
5112 /* delete this address */
5113 stcb->asoc.last_used_address = NULL;
5115 * Now spin through all the nets and purge any ref
5118 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5119 if (net->ro._s_addr &&
5120 (net->ro._s_addr->ifa == laddr->ifa)) {
5121 /* Yep, purge src address selected */
5124 /* delete this address if cached */
5128 net->ro.ro_rt = NULL;
5130 sctp_free_ifa(net->ro._s_addr);
5131 net->ro._s_addr = NULL;
5132 net->src_addr_selected = 0;
5135 SCTP_TCB_UNLOCK(stcb);
5136 } /* for each tcb */
5137 /* remove it from the ep list */
5138 sctp_remove_laddr(laddr);
5140 /* update inp_vflag flags */
5141 sctp_update_ep_vflag(inp);
5147 * Add the address to the TCB local address restricted list.
5148 * This is a "pending" address list (eg. addresses waiting for an
5149 * ASCONF-ACK response) and cannot be used as a valid source address.
5152 sctp_add_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
5154 struct sctp_inpcb *inp;
5155 struct sctp_laddr *laddr;
5156 struct sctpladdr *list;
5159 * Assumes TCB is locked.. and possibly the INP. May need to
5160 * confirm/fix that if we need it and is not the case.
5162 list = &stcb->asoc.sctp_restricted_addrs;
5164 inp = stcb->sctp_ep;
5165 if (ifa->address.sa.sa_family == AF_INET6) {
5166 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
5167 /* Can't bind a non-existent addr. */
5171 /* does the address already exist? */
5172 LIST_FOREACH(laddr, list, sctp_nxt_addr) {
5173 if (laddr->ifa == ifa) {
5178 /* add to the list */
5179 (void)sctp_insert_laddr(list, ifa, 0);
5184 * insert an laddr entry with the given ifa for the desired list
5187 sctp_insert_laddr(struct sctpladdr *list, struct sctp_ifa *ifa, uint32_t act)
5189 struct sctp_laddr *laddr;
5191 laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
5192 if (laddr == NULL) {
5193 /* out of memory? */
5194 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
5197 SCTP_INCR_LADDR_COUNT();
5198 bzero(laddr, sizeof(*laddr));
5199 (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time);
5201 laddr->action = act;
5202 atomic_add_int(&ifa->refcount, 1);
5204 LIST_INSERT_HEAD(list, laddr, sctp_nxt_addr);
5210 * Remove an laddr entry from the local address list (on an assoc)
5213 sctp_remove_laddr(struct sctp_laddr *laddr)
5216 /* remove from the list */
5217 LIST_REMOVE(laddr, sctp_nxt_addr);
5218 sctp_free_ifa(laddr->ifa);
5219 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), laddr);
5220 SCTP_DECR_LADDR_COUNT();
5224 * Remove a local address from the TCB local address restricted list
5227 sctp_del_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
5229 struct sctp_inpcb *inp;
5230 struct sctp_laddr *laddr;
5233 * This is called by asconf work. It is assumed that a) The TCB is
5234 * locked and b) The INP is locked. This is true in as much as I can
5235 * trace through the entry asconf code where I did these locks.
5236 * Again, the ASCONF code is a bit different in that it does lock
5237 * the INP during its work often times. This must be since we don't
5238 * want other proc's looking up things while what they are looking
5239 * up is changing :-D
5242 inp = stcb->sctp_ep;
5243 /* if subset bound and don't allow ASCONF's, can't delete last */
5244 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) &&
5245 sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF)) {
5246 if (stcb->sctp_ep->laddr_count < 2) {
5247 /* can't delete last address */
5251 LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
5252 /* remove the address if it exists */
5253 if (laddr->ifa == NULL)
5255 if (laddr->ifa == ifa) {
5256 sctp_remove_laddr(laddr);
5261 /* address not found! */
5266 * Temporarily remove for __APPLE__ until we use the Tiger equivalents
5269 static int sctp_max_number_of_assoc = SCTP_MAX_NUM_OF_ASOC;
5270 static int sctp_scale_up_for_address = SCTP_SCALE_FOR_ADDR;
5276 * SCTP initialization for the PCB structures should be called by
5277 * the sctp_init() funciton.
5282 if (SCTP_BASE_VAR(sctp_pcb_initialized) != 0) {
5283 /* error I was called twice */
5286 SCTP_BASE_VAR(sctp_pcb_initialized) = 1;
5288 #if defined(SCTP_LOCAL_TRACE_BUF)
5289 bzero(&SCTP_BASE_SYSCTL(sctp_log), sizeof(struct sctp_log));
5291 (void)SCTP_GETTIME_TIMEVAL(&tv);
5292 SCTP_BASE_STAT(sctps_discontinuitytime).tv_sec = (uint32_t) tv.tv_sec;
5293 SCTP_BASE_STAT(sctps_discontinuitytime).tv_usec = (uint32_t) tv.tv_usec;
5294 /* init the empty list of (All) Endpoints */
5295 LIST_INIT(&SCTP_BASE_INFO(listhead));
5297 /* init the iterator head */
5298 TAILQ_INIT(&SCTP_BASE_INFO(iteratorhead));
5300 /* init the hash table of endpoints */
5301 TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", &SCTP_BASE_SYSCTL(sctp_hashtblsize));
5302 TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", &SCTP_BASE_SYSCTL(sctp_pcbtblsize));
5303 TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", &SCTP_BASE_SYSCTL(sctp_chunkscale));
5304 SCTP_BASE_INFO(sctp_asochash) = SCTP_HASH_INIT((SCTP_BASE_SYSCTL(sctp_hashtblsize) * 31),
5305 &SCTP_BASE_INFO(hashasocmark));
5306 SCTP_BASE_INFO(sctp_ephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize),
5307 &SCTP_BASE_INFO(hashmark));
5308 SCTP_BASE_INFO(sctp_tcpephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize),
5309 &SCTP_BASE_INFO(hashtcpmark));
5310 SCTP_BASE_INFO(hashtblsize) = SCTP_BASE_SYSCTL(sctp_hashtblsize);
5312 /* init the small hash table we use to track restarted asoc's */
5313 SCTP_BASE_INFO(sctp_restarthash) = SCTP_HASH_INIT(SCTP_STACK_VTAG_HASH_SIZE,
5314 &SCTP_BASE_INFO(hashrestartmark));
5317 SCTP_BASE_INFO(sctp_vrfhash) = SCTP_HASH_INIT(SCTP_SIZE_OF_VRF_HASH,
5318 &SCTP_BASE_INFO(hashvrfmark));
5320 SCTP_BASE_INFO(vrf_ifn_hash) = SCTP_HASH_INIT(SCTP_VRF_IFN_HASH_SIZE,
5321 &SCTP_BASE_INFO(vrf_ifn_hashmark));
5323 /* init the zones */
5325 * FIX ME: Should check for NULL returns, but if it does fail we are
5326 * doomed to panic anyways... add later maybe.
5328 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_ep), "sctp_ep",
5329 sizeof(struct sctp_inpcb), maxsockets);
5331 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asoc), "sctp_asoc",
5332 sizeof(struct sctp_tcb), sctp_max_number_of_assoc);
5334 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_laddr), "sctp_laddr",
5335 sizeof(struct sctp_laddr),
5336 (sctp_max_number_of_assoc * sctp_scale_up_for_address));
5338 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_net), "sctp_raddr",
5339 sizeof(struct sctp_nets),
5340 (sctp_max_number_of_assoc * sctp_scale_up_for_address));
5342 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_chunk), "sctp_chunk",
5343 sizeof(struct sctp_tmit_chunk),
5344 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
5346 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_readq), "sctp_readq",
5347 sizeof(struct sctp_queued_to_read),
5348 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
5350 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_strmoq), "sctp_stream_msg_out",
5351 sizeof(struct sctp_stream_queue_pending),
5352 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
5354 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf), "sctp_asconf",
5355 sizeof(struct sctp_asconf),
5356 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
5358 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf_ack), "sctp_asconf_ack",
5359 sizeof(struct sctp_asconf_ack),
5360 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
5363 /* Master Lock INIT for info structure */
5364 SCTP_INP_INFO_LOCK_INIT();
5365 SCTP_STATLOG_INIT_LOCK();
5366 SCTP_ITERATOR_LOCK_INIT();
5368 SCTP_IPI_COUNT_INIT();
5369 SCTP_IPI_ADDR_INIT();
5370 SCTP_IPI_ITERATOR_WQ_INIT();
5371 #ifdef SCTP_PACKET_LOGGING
5372 SCTP_IP_PKTLOG_INIT();
5374 LIST_INIT(&SCTP_BASE_INFO(addr_wq));
5376 /* not sure if we need all the counts */
5377 SCTP_BASE_INFO(ipi_count_ep) = 0;
5378 /* assoc/tcb zone info */
5379 SCTP_BASE_INFO(ipi_count_asoc) = 0;
5380 /* local addrlist zone info */
5381 SCTP_BASE_INFO(ipi_count_laddr) = 0;
5382 /* remote addrlist zone info */
5383 SCTP_BASE_INFO(ipi_count_raddr) = 0;
5385 SCTP_BASE_INFO(ipi_count_chunk) = 0;
5387 /* socket queue zone info */
5388 SCTP_BASE_INFO(ipi_count_readq) = 0;
5390 /* stream out queue cont */
5391 SCTP_BASE_INFO(ipi_count_strmoq) = 0;
5393 SCTP_BASE_INFO(ipi_free_strmoq) = 0;
5394 SCTP_BASE_INFO(ipi_free_chunks) = 0;
5396 SCTP_OS_TIMER_INIT(&SCTP_BASE_INFO(addr_wq_timer.timer));
5398 /* Init the TIMEWAIT list */
5399 for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE_A; i++) {
5400 LIST_INIT(&SCTP_BASE_INFO(vtag_timewait[i]));
5403 #if defined(SCTP_USE_THREAD_BASED_ITERATOR)
5404 SCTP_BASE_INFO(iterator_running) = 0;
5405 SCTP_BASE_INFO(threads_must_exit) = 0;
5406 sctp_startup_iterator();
5410 * INIT the default VRF which for BSD is the only one, other O/S's
5411 * may have more. But initially they must start with one and then
5412 * add the VRF's as addresses are added.
5414 sctp_init_vrf_list(SCTP_DEFAULT_VRF);
5419 * Assumes that the SCTP_BASE_INFO() lock is NOT held.
5422 sctp_pcb_finish(void)
5424 struct sctp_vrflist *vrf_bucket;
5425 struct sctp_vrf *vrf;
5426 struct sctp_ifn *ifn;
5427 struct sctp_ifa *ifa;
5428 struct sctpvtaghead *chain;
5429 struct sctp_tagblock *twait_block, *prev_twait_block;
5433 * free the vrf/ifn/ifa lists and hashes (be sure address monitor is
5436 vrf_bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(SCTP_DEFAULT_VRFID & SCTP_BASE_INFO(hashvrfmark))];
5437 vrf = LIST_FIRST(vrf_bucket);
5439 ifn = LIST_FIRST(&vrf->ifnlist);
5441 ifa = LIST_FIRST(&ifn->ifalist);
5444 LIST_REMOVE(ifa, next_bucket);
5445 LIST_REMOVE(ifa, next_ifa);
5446 SCTP_FREE(ifa, SCTP_M_IFA);
5447 ifa = LIST_FIRST(&ifn->ifalist);
5450 LIST_REMOVE(ifn, next_bucket);
5451 LIST_REMOVE(ifn, next_ifn);
5452 SCTP_FREE(ifn, SCTP_M_IFN);
5453 ifn = LIST_FIRST(&vrf->ifnlist);
5455 SCTP_HASH_FREE(vrf->vrf_addr_hash, vrf->vrf_addr_hashmark);
5457 LIST_REMOVE(vrf, next_vrf);
5458 SCTP_FREE(vrf, SCTP_M_VRF);
5459 vrf = LIST_FIRST(vrf_bucket);
5461 /* free the vrf hashes */
5462 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_vrfhash), SCTP_BASE_INFO(hashvrfmark));
5463 SCTP_HASH_FREE(SCTP_BASE_INFO(vrf_ifn_hash), SCTP_BASE_INFO(vrf_ifn_hashmark));
5466 * free the TIMEWAIT list elements malloc'd in the function
5467 * sctp_add_vtag_to_timewait()...
5469 for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE_A; i++) {
5470 chain = &SCTP_BASE_INFO(vtag_timewait)[i];
5471 if (!SCTP_LIST_EMPTY(chain)) {
5472 prev_twait_block = NULL;
5473 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
5474 if (prev_twait_block) {
5475 SCTP_FREE(prev_twait_block, SCTP_M_TIMW);
5477 prev_twait_block = twait_block;
5479 SCTP_FREE(prev_twait_block, SCTP_M_TIMW);
5483 /* free the locks and mutexes */
5484 #ifdef SCTP_PACKET_LOGGING
5485 SCTP_IP_PKTLOG_DESTROY();
5488 SCTP_IPI_ITERATOR_WQ_DESTROY();
5489 SCTP_IPI_ADDR_DESTROY();
5490 SCTP_ITERATOR_LOCK_DESTROY();
5491 SCTP_STATLOG_DESTROY();
5492 SCTP_INP_INFO_LOCK_DESTROY();
5494 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_ep));
5495 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asoc));
5496 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_laddr));
5497 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_net));
5498 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_chunk));
5499 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_readq));
5500 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_strmoq));
5501 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf));
5502 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf_ack));
5503 /* Get rid of other stuff to */
5504 if (SCTP_BASE_INFO(sctp_asochash) != NULL)
5505 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_asochash), SCTP_BASE_INFO(hashasocmark));
5506 if (SCTP_BASE_INFO(sctp_ephash) != NULL)
5507 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_ephash), SCTP_BASE_INFO(hashmark));
5508 if (SCTP_BASE_INFO(sctp_tcpephash) != NULL)
5509 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_tcpephash), SCTP_BASE_INFO(hashtcpmark));
5510 if (SCTP_BASE_INFO(sctp_restarthash) != NULL)
5511 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_restarthash), SCTP_BASE_INFO(hashrestartmark));
5517 sctp_load_addresses_from_init(struct sctp_tcb *stcb, struct mbuf *m,
5518 int iphlen, int offset, int limit, struct sctphdr *sh,
5519 struct sockaddr *altsa)
5522 * grub through the INIT pulling addresses and loading them to the
5523 * nets structure in the asoc. The from address in the mbuf should
5524 * also be loaded (if it is not already). This routine can be called
5525 * with either INIT or INIT-ACK's as long as the m points to the IP
5526 * packet and the offset points to the beginning of the parameters.
5528 struct sctp_inpcb *inp, *l_inp;
5529 struct sctp_nets *net, *net_tmp;
5531 struct sctp_paramhdr *phdr, parm_buf;
5532 struct sctp_tcb *stcb_tmp;
5533 uint16_t ptype, plen;
5534 struct sockaddr *sa;
5535 struct sockaddr_storage dest_store;
5536 struct sockaddr *local_sa = (struct sockaddr *)&dest_store;
5537 struct sockaddr_in sin;
5538 struct sockaddr_in6 sin6;
5539 uint8_t random_store[SCTP_PARAM_BUFFER_SIZE];
5540 struct sctp_auth_random *p_random = NULL;
5541 uint16_t random_len = 0;
5542 uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE];
5543 struct sctp_auth_hmac_algo *hmacs = NULL;
5544 uint16_t hmacs_len = 0;
5545 uint8_t saw_asconf = 0;
5546 uint8_t saw_asconf_ack = 0;
5547 uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE];
5548 struct sctp_auth_chunk_list *chunks = NULL;
5549 uint16_t num_chunks = 0;
5550 sctp_key_t *new_key;
5552 int got_random = 0, got_hmacs = 0, got_chklist = 0;
5554 /* First get the destination address setup too. */
5555 memset(&sin, 0, sizeof(sin));
5556 memset(&sin6, 0, sizeof(sin6));
5558 sin.sin_family = AF_INET;
5559 sin.sin_len = sizeof(sin);
5560 sin.sin_port = stcb->rport;
5562 sin6.sin6_family = AF_INET6;
5563 sin6.sin6_len = sizeof(struct sockaddr_in6);
5564 sin6.sin6_port = stcb->rport;
5565 if (altsa == NULL) {
5566 iph = mtod(m, struct ip *);
5567 switch (iph->ip_v) {
5571 struct sockaddr_in *sin_2;
5573 sin_2 = (struct sockaddr_in *)(local_sa);
5574 memset(sin_2, 0, sizeof(sin));
5575 sin_2->sin_family = AF_INET;
5576 sin_2->sin_len = sizeof(sin);
5577 sin_2->sin_port = sh->dest_port;
5578 sin_2->sin_addr.s_addr = iph->ip_dst.s_addr;
5579 sin.sin_addr = iph->ip_src;
5580 sa = (struct sockaddr *)&sin;
5584 case IPV6_VERSION >> 4:
5587 struct ip6_hdr *ip6;
5588 struct sockaddr_in6 *sin6_2;
5590 ip6 = mtod(m, struct ip6_hdr *);
5591 sin6_2 = (struct sockaddr_in6 *)(local_sa);
5592 memset(sin6_2, 0, sizeof(sin6));
5593 sin6_2->sin6_family = AF_INET6;
5594 sin6_2->sin6_len = sizeof(struct sockaddr_in6);
5595 sin6_2->sin6_port = sh->dest_port;
5596 sin6.sin6_addr = ip6->ip6_src;
5597 sa = (struct sockaddr *)&sin6;
5607 * For cookies we use the src address NOT from the packet
5608 * but from the original INIT
5612 /* Turn off ECN until we get through all params */
5613 stcb->asoc.ecn_allowed = 0;
5614 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5615 /* mark all addresses that we have currently on the list */
5616 net->dest_state |= SCTP_ADDR_NOT_IN_ASSOC;
5618 /* does the source address already exist? if so skip it */
5619 l_inp = inp = stcb->sctp_ep;
5621 atomic_add_int(&stcb->asoc.refcnt, 1);
5622 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net_tmp, local_sa, stcb);
5623 atomic_add_int(&stcb->asoc.refcnt, -1);
5625 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || inp == NULL) {
5626 /* we must add the source address */
5627 /* no scope set here since we have a tcb already. */
5628 if ((sa->sa_family == AF_INET) &&
5629 (stcb->asoc.ipv4_addr_legal)) {
5630 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_2)) {
5633 } else if ((sa->sa_family == AF_INET6) &&
5634 (stcb->asoc.ipv6_addr_legal)) {
5635 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) {
5640 if (net_tmp != NULL && stcb_tmp == stcb) {
5641 net_tmp->dest_state &= ~SCTP_ADDR_NOT_IN_ASSOC;
5642 } else if (stcb_tmp != stcb) {
5643 /* It belongs to another association? */
5645 SCTP_TCB_UNLOCK(stcb_tmp);
5649 if (stcb->asoc.state == 0) {
5650 /* the assoc was freed? */
5654 * peer must explicitly turn this on. This may have been initialized
5655 * to be "on" in order to allow local addr changes while INIT's are
5658 stcb->asoc.peer_supports_asconf = 0;
5659 /* now we must go through each of the params. */
5660 phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
5662 ptype = ntohs(phdr->param_type);
5663 plen = ntohs(phdr->param_length);
5665 * printf("ptype => %0x, plen => %d\n", (uint32_t)ptype,
5668 if (offset + plen > limit) {
5674 if (ptype == SCTP_IPV4_ADDRESS) {
5675 if (stcb->asoc.ipv4_addr_legal) {
5676 struct sctp_ipv4addr_param *p4, p4_buf;
5678 /* ok get the v4 address and check/add */
5679 phdr = sctp_get_next_param(m, offset,
5680 (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
5681 if (plen != sizeof(struct sctp_ipv4addr_param) ||
5685 p4 = (struct sctp_ipv4addr_param *)phdr;
5686 sin.sin_addr.s_addr = p4->addr;
5687 if (IN_MULTICAST(sin.sin_addr.s_addr)) {
5688 /* Skip multi-cast addresses */
5691 if ((sin.sin_addr.s_addr == INADDR_BROADCAST) ||
5692 (sin.sin_addr.s_addr == INADDR_ANY)) {
5695 sa = (struct sockaddr *)&sin;
5696 inp = stcb->sctp_ep;
5697 atomic_add_int(&stcb->asoc.refcnt, 1);
5698 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
5700 atomic_add_int(&stcb->asoc.refcnt, -1);
5702 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) ||
5704 /* we must add the source address */
5706 * no scope set since we have a tcb
5711 * we must validate the state again
5715 if (stcb->asoc.state == 0) {
5716 /* the assoc was freed? */
5719 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_4)) {
5722 } else if (stcb_tmp == stcb) {
5723 if (stcb->asoc.state == 0) {
5724 /* the assoc was freed? */
5730 ~SCTP_ADDR_NOT_IN_ASSOC;
5734 * strange, address is in another
5735 * assoc? straighten out locks.
5738 if (SCTP_GET_STATE(&stcb_tmp->asoc) & SCTP_STATE_COOKIE_WAIT) {
5743 sctp_abort_an_association(stcb_tmp->sctp_ep,
5744 stcb_tmp, 1, NULL, 0);
5747 SCTP_TCB_UNLOCK(stcb_tmp);
5749 if (stcb->asoc.state == 0) {
5750 /* the assoc was freed? */
5756 } else if (ptype == SCTP_IPV6_ADDRESS) {
5757 if (stcb->asoc.ipv6_addr_legal) {
5758 /* ok get the v6 address and check/add */
5759 struct sctp_ipv6addr_param *p6, p6_buf;
5761 phdr = sctp_get_next_param(m, offset,
5762 (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
5763 if (plen != sizeof(struct sctp_ipv6addr_param) ||
5767 p6 = (struct sctp_ipv6addr_param *)phdr;
5768 memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
5770 if (IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) {
5771 /* Skip multi-cast addresses */
5774 if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
5776 * Link local make no sense without
5781 sa = (struct sockaddr *)&sin6;
5782 inp = stcb->sctp_ep;
5783 atomic_add_int(&stcb->asoc.refcnt, 1);
5784 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
5786 atomic_add_int(&stcb->asoc.refcnt, -1);
5787 if (stcb_tmp == NULL && (inp == stcb->sctp_ep ||
5790 * we must validate the state again
5794 if (stcb->asoc.state == 0) {
5795 /* the assoc was freed? */
5799 * we must add the address, no scope
5802 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_5)) {
5805 } else if (stcb_tmp == stcb) {
5807 * we must validate the state again
5810 if (stcb->asoc.state == 0) {
5811 /* the assoc was freed? */
5817 ~SCTP_ADDR_NOT_IN_ASSOC;
5821 * strange, address is in another
5822 * assoc? straighten out locks.
5825 if (SCTP_GET_STATE(&stcb_tmp->asoc) & SCTP_STATE_COOKIE_WAIT) {
5830 sctp_abort_an_association(stcb_tmp->sctp_ep,
5831 stcb_tmp, 1, NULL, 0);
5834 SCTP_TCB_UNLOCK(stcb_tmp);
5836 if (stcb->asoc.state == 0) {
5837 /* the assoc was freed? */
5843 } else if (ptype == SCTP_ECN_CAPABLE) {
5844 stcb->asoc.ecn_allowed = 1;
5845 } else if (ptype == SCTP_ULP_ADAPTATION) {
5846 if (stcb->asoc.state != SCTP_STATE_OPEN) {
5847 struct sctp_adaptation_layer_indication ai,
5850 phdr = sctp_get_next_param(m, offset,
5851 (struct sctp_paramhdr *)&ai, sizeof(ai));
5852 aip = (struct sctp_adaptation_layer_indication *)phdr;
5854 stcb->asoc.peers_adaptation = ntohl(aip->indication);
5855 stcb->asoc.adaptation_needed = 1;
5858 } else if (ptype == SCTP_SET_PRIM_ADDR) {
5859 struct sctp_asconf_addr_param lstore, *fee;
5860 struct sctp_asconf_addrv4_param *fii;
5862 struct sockaddr *lsa = NULL;
5864 stcb->asoc.peer_supports_asconf = 1;
5865 if (plen > sizeof(lstore)) {
5868 phdr = sctp_get_next_param(m, offset,
5869 (struct sctp_paramhdr *)&lstore, min(plen, sizeof(lstore)));
5873 fee = (struct sctp_asconf_addr_param *)phdr;
5874 lptype = ntohs(fee->addrp.ph.param_type);
5875 if (lptype == SCTP_IPV4_ADDRESS) {
5877 sizeof(struct sctp_asconf_addrv4_param)) {
5878 SCTP_PRINTF("Sizeof setprim in init/init ack not %d but %d - ignored\n",
5879 (int)sizeof(struct sctp_asconf_addrv4_param),
5882 fii = (struct sctp_asconf_addrv4_param *)fee;
5883 sin.sin_addr.s_addr = fii->addrp.addr;
5884 lsa = (struct sockaddr *)&sin;
5886 } else if (lptype == SCTP_IPV6_ADDRESS) {
5888 sizeof(struct sctp_asconf_addr_param)) {
5889 SCTP_PRINTF("Sizeof setprim (v6) in init/init ack not %d but %d - ignored\n",
5890 (int)sizeof(struct sctp_asconf_addr_param),
5893 memcpy(sin6.sin6_addr.s6_addr,
5895 sizeof(fee->addrp.addr));
5896 lsa = (struct sockaddr *)&sin6;
5900 (void)sctp_set_primary_addr(stcb, sa, NULL);
5902 } else if (ptype == SCTP_PRSCTP_SUPPORTED) {
5903 /* Peer supports pr-sctp */
5904 stcb->asoc.peer_supports_prsctp = 1;
5905 } else if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
5906 /* A supported extension chunk */
5907 struct sctp_supported_chunk_types_param *pr_supported;
5908 uint8_t local_store[SCTP_PARAM_BUFFER_SIZE];
5911 phdr = sctp_get_next_param(m, offset,
5912 (struct sctp_paramhdr *)&local_store, min(sizeof(local_store), plen));
5916 stcb->asoc.peer_supports_asconf = 0;
5917 stcb->asoc.peer_supports_prsctp = 0;
5918 stcb->asoc.peer_supports_pktdrop = 0;
5919 stcb->asoc.peer_supports_strreset = 0;
5920 stcb->asoc.peer_supports_auth = 0;
5921 pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
5922 num_ent = plen - sizeof(struct sctp_paramhdr);
5923 for (i = 0; i < num_ent; i++) {
5924 switch (pr_supported->chunk_types[i]) {
5926 case SCTP_ASCONF_ACK:
5927 stcb->asoc.peer_supports_asconf = 1;
5929 case SCTP_FORWARD_CUM_TSN:
5930 stcb->asoc.peer_supports_prsctp = 1;
5932 case SCTP_PACKET_DROPPED:
5933 stcb->asoc.peer_supports_pktdrop = 1;
5935 case SCTP_STREAM_RESET:
5936 stcb->asoc.peer_supports_strreset = 1;
5938 case SCTP_AUTHENTICATION:
5939 stcb->asoc.peer_supports_auth = 1;
5942 /* one I have not learned yet */
5947 } else if (ptype == SCTP_ECN_NONCE_SUPPORTED) {
5948 /* Peer supports ECN-nonce */
5949 stcb->asoc.peer_supports_ecn_nonce = 1;
5950 stcb->asoc.ecn_nonce_allowed = 1;
5951 } else if (ptype == SCTP_RANDOM) {
5952 if (plen > sizeof(random_store))
5955 /* already processed a RANDOM */
5958 phdr = sctp_get_next_param(m, offset,
5959 (struct sctp_paramhdr *)random_store,
5960 min(sizeof(random_store), plen));
5963 p_random = (struct sctp_auth_random *)phdr;
5964 random_len = plen - sizeof(*p_random);
5965 /* enforce the random length */
5966 if (random_len != SCTP_AUTH_RANDOM_SIZE_REQUIRED) {
5967 SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: invalid RANDOM len\n");
5971 } else if (ptype == SCTP_HMAC_LIST) {
5975 if (plen > sizeof(hmacs_store))
5978 /* already processed a HMAC list */
5981 phdr = sctp_get_next_param(m, offset,
5982 (struct sctp_paramhdr *)hmacs_store,
5983 min(plen, sizeof(hmacs_store)));
5986 hmacs = (struct sctp_auth_hmac_algo *)phdr;
5987 hmacs_len = plen - sizeof(*hmacs);
5988 num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]);
5989 /* validate the hmac list */
5990 if (sctp_verify_hmac_param(hmacs, num_hmacs)) {
5993 if (stcb->asoc.peer_hmacs != NULL)
5994 sctp_free_hmaclist(stcb->asoc.peer_hmacs);
5995 stcb->asoc.peer_hmacs = sctp_alloc_hmaclist(num_hmacs);
5996 if (stcb->asoc.peer_hmacs != NULL) {
5997 for (i = 0; i < num_hmacs; i++) {
5998 (void)sctp_auth_add_hmacid(stcb->asoc.peer_hmacs,
5999 ntohs(hmacs->hmac_ids[i]));
6003 } else if (ptype == SCTP_CHUNK_LIST) {
6006 if (plen > sizeof(chunks_store))
6009 /* already processed a Chunks list */
6012 phdr = sctp_get_next_param(m, offset,
6013 (struct sctp_paramhdr *)chunks_store,
6014 min(plen, sizeof(chunks_store)));
6017 chunks = (struct sctp_auth_chunk_list *)phdr;
6018 num_chunks = plen - sizeof(*chunks);
6019 if (stcb->asoc.peer_auth_chunks != NULL)
6020 sctp_clear_chunklist(stcb->asoc.peer_auth_chunks);
6022 stcb->asoc.peer_auth_chunks = sctp_alloc_chunklist();
6023 for (i = 0; i < num_chunks; i++) {
6024 (void)sctp_auth_add_chunk(chunks->chunk_types[i],
6025 stcb->asoc.peer_auth_chunks);
6026 /* record asconf/asconf-ack if listed */
6027 if (chunks->chunk_types[i] == SCTP_ASCONF)
6029 if (chunks->chunk_types[i] == SCTP_ASCONF_ACK)
6034 } else if ((ptype == SCTP_HEARTBEAT_INFO) ||
6035 (ptype == SCTP_STATE_COOKIE) ||
6036 (ptype == SCTP_UNRECOG_PARAM) ||
6037 (ptype == SCTP_COOKIE_PRESERVE) ||
6038 (ptype == SCTP_SUPPORTED_ADDRTYPE) ||
6039 (ptype == SCTP_ADD_IP_ADDRESS) ||
6040 (ptype == SCTP_DEL_IP_ADDRESS) ||
6041 (ptype == SCTP_ERROR_CAUSE_IND) ||
6042 (ptype == SCTP_SUCCESS_REPORT)) {
6045 if ((ptype & 0x8000) == 0x0000) {
6047 * must stop processing the rest of the
6048 * param's. Any report bits were handled
6050 * sctp_arethere_unrecognized_parameters()
6051 * when the INIT or INIT-ACK was first seen.
6057 offset += SCTP_SIZE32(plen);
6058 if (offset >= limit) {
6061 phdr = sctp_get_next_param(m, offset, &parm_buf,
6064 /* Now check to see if we need to purge any addresses */
6065 for (net = TAILQ_FIRST(&stcb->asoc.nets); net != NULL; net = net_tmp) {
6066 net_tmp = TAILQ_NEXT(net, sctp_next);
6067 if ((net->dest_state & SCTP_ADDR_NOT_IN_ASSOC) ==
6068 SCTP_ADDR_NOT_IN_ASSOC) {
6069 /* This address has been removed from the asoc */
6070 /* remove and free it */
6071 stcb->asoc.numnets--;
6072 TAILQ_REMOVE(&stcb->asoc.nets, net, sctp_next);
6073 sctp_free_remote_addr(net);
6074 if (net == stcb->asoc.primary_destination) {
6075 stcb->asoc.primary_destination = NULL;
6076 sctp_select_primary_destination(stcb);
6080 /* validate authentication required parameters */
6081 if (got_random && got_hmacs) {
6082 stcb->asoc.peer_supports_auth = 1;
6084 stcb->asoc.peer_supports_auth = 0;
6086 if (!stcb->asoc.peer_supports_auth && got_chklist) {
6087 /* peer does not support auth but sent a chunks list? */
6090 if (!SCTP_BASE_SYSCTL(sctp_asconf_auth_nochk) && stcb->asoc.peer_supports_asconf &&
6091 !stcb->asoc.peer_supports_auth) {
6092 /* peer supports asconf but not auth? */
6094 } else if ((stcb->asoc.peer_supports_asconf) && (stcb->asoc.peer_supports_auth) &&
6095 ((saw_asconf == 0) || (saw_asconf_ack == 0))) {
6098 /* concatenate the full random key */
6099 #ifdef SCTP_AUTH_DRAFT_04
6100 keylen = random_len;
6101 new_key = sctp_alloc_key(keylen);
6102 if (new_key != NULL) {
6103 /* copy in the RANDOM */
6104 if (p_random != NULL)
6105 bcopy(p_random->random_data, new_key->key, random_len);
6108 keylen = sizeof(*p_random) + random_len + sizeof(*chunks) + num_chunks +
6109 sizeof(*hmacs) + hmacs_len;
6110 new_key = sctp_alloc_key(keylen);
6111 if (new_key != NULL) {
6112 /* copy in the RANDOM */
6113 if (p_random != NULL) {
6114 keylen = sizeof(*p_random) + random_len;
6115 bcopy(p_random, new_key->key, keylen);
6117 /* append in the AUTH chunks */
6118 if (chunks != NULL) {
6119 bcopy(chunks, new_key->key + keylen,
6120 sizeof(*chunks) + num_chunks);
6121 keylen += sizeof(*chunks) + num_chunks;
6123 /* append in the HMACs */
6124 if (hmacs != NULL) {
6125 bcopy(hmacs, new_key->key + keylen,
6126 sizeof(*hmacs) + hmacs_len);
6131 /* failed to get memory for the key */
6134 if (stcb->asoc.authinfo.peer_random != NULL)
6135 sctp_free_key(stcb->asoc.authinfo.peer_random);
6136 stcb->asoc.authinfo.peer_random = new_key;
6137 #ifdef SCTP_AUTH_DRAFT_04
6138 /* don't include the chunks and hmacs for draft -04 */
6139 stcb->asoc.authinfo.peer_random->keylen = random_len;
6141 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid);
6142 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid);
6148 sctp_set_primary_addr(struct sctp_tcb *stcb, struct sockaddr *sa,
6149 struct sctp_nets *net)
6151 /* make sure the requested primary address exists in the assoc */
6152 if (net == NULL && sa)
6153 net = sctp_findnet(stcb, sa);
6156 /* didn't find the requested primary address! */
6159 /* set the primary address */
6160 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
6161 /* Must be confirmed, so queue to set */
6162 net->dest_state |= SCTP_ADDR_REQ_PRIMARY;
6165 stcb->asoc.primary_destination = net;
6166 net->dest_state &= ~SCTP_ADDR_WAS_PRIMARY;
6167 net = TAILQ_FIRST(&stcb->asoc.nets);
6168 if (net != stcb->asoc.primary_destination) {
6170 * first one on the list is NOT the primary
6171 * sctp_cmpaddr() is much more efficent if the
6172 * primary is the first on the list, make it so.
6174 TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
6175 TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
6182 sctp_is_vtag_good(struct sctp_inpcb *inp, uint32_t tag, struct timeval *now, int save_in_twait)
6185 * This function serves two purposes. It will see if a TAG can be
6186 * re-used and return 1 for yes it is ok and 0 for don't use that
6187 * tag. A secondary function it will do is purge out old tags that
6190 struct sctpasochead *head;
6191 struct sctpvtaghead *chain;
6192 struct sctp_tagblock *twait_block;
6193 struct sctp_tcb *stcb;
6196 SCTP_INP_INFO_WLOCK();
6197 chain = &SCTP_BASE_INFO(vtag_timewait[(tag % SCTP_STACK_VTAG_HASH_SIZE))];
6198 /* First is the vtag in use ? */
6200 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
6201 SCTP_BASE_INFO(hashasocmark))];
6205 LIST_FOREACH(stcb, head, sctp_asocs) {
6207 if (stcb->asoc.my_vtag == tag) {
6209 * We should remove this if and return 0 always if
6210 * we want vtags unique across all endpoints. For
6211 * now within a endpoint is ok.
6213 if (inp == stcb->sctp_ep) {
6214 /* bad tag, in use */
6215 SCTP_INP_INFO_WUNLOCK();
6221 /* Now lets check the restart hash */
6222 head = &SCTP_BASE_INFO(sctp_restarthash)[SCTP_PCBHASH_ASOC(tag,
6223 SCTP_BASE_INFO(hashrestartmark))];
6225 goto check_time_wait;
6227 LIST_FOREACH(stcb, head, sctp_tcbrestarhash) {
6228 if (stcb->asoc.assoc_id == tag) {
6230 if (inp == stcb->sctp_ep) {
6231 /* bad tag, in use */
6232 SCTP_INP_INFO_WUNLOCK();
6238 /* Now what about timed wait ? */
6239 if (!SCTP_LIST_EMPTY(chain)) {
6241 * Block(s) are present, lets see if we have this tag in the
6244 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
6245 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
6246 if (twait_block->vtag_block[i].v_tag == 0) {
6249 } else if ((long)twait_block->vtag_block[i].tv_sec_at_expire <
6251 /* Audit expires this guy */
6252 twait_block->vtag_block[i].tv_sec_at_expire = 0;
6253 twait_block->vtag_block[i].v_tag = 0;
6254 } else if (twait_block->vtag_block[i].v_tag ==
6256 /* Bad tag, sorry :< */
6257 SCTP_INP_INFO_WUNLOCK();
6264 * Not found, ok to use the tag, add it to the time wait hash
6265 * as well this will prevent two sucessive cookies from getting
6266 * the same tag or two inits sent quickly on multi-processors.
6267 * We only keep the tag for the life of a cookie and when we
6268 * add this tag to the assoc hash we need to purge it from
6272 sctp_add_vtag_to_timewait(tag, TICKS_TO_SEC(inp->sctp_ep.def_cookie_life));
6273 SCTP_INP_INFO_WUNLOCK();
6278 static sctp_assoc_t reneged_asoc_ids[256];
6279 static uint8_t reneged_at = 0;
6283 sctp_drain_mbufs(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
6286 * We must hunt this association for MBUF's past the cumack (i.e.
6287 * out of order data that we can renege on).
6289 struct sctp_association *asoc;
6290 struct sctp_tmit_chunk *chk, *nchk;
6291 uint32_t cumulative_tsn_p1, tsn;
6292 struct sctp_queued_to_read *ctl, *nctl;
6293 int cnt, strmat, gap;
6295 /* We look for anything larger than the cum-ack + 1 */
6297 SCTP_STAT_INCR(sctps_protocol_drain_calls);
6298 if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
6302 if (asoc->cumulative_tsn == asoc->highest_tsn_inside_map) {
6303 /* none we can reneg on. */
6306 SCTP_STAT_INCR(sctps_protocol_drains_done);
6307 cumulative_tsn_p1 = asoc->cumulative_tsn + 1;
6309 /* First look in the re-assembly queue */
6310 chk = TAILQ_FIRST(&asoc->reasmqueue);
6312 /* Get the next one */
6313 nchk = TAILQ_NEXT(chk, sctp_next);
6314 if (compare_with_wrap(chk->rec.data.TSN_seq,
6315 cumulative_tsn_p1, MAX_TSN)) {
6316 /* Yep it is above cum-ack */
6318 tsn = chk->rec.data.TSN_seq;
6319 if (tsn >= asoc->mapping_array_base_tsn) {
6320 gap = tsn - asoc->mapping_array_base_tsn;
6322 gap = (MAX_TSN - asoc->mapping_array_base_tsn) +
6325 asoc->size_on_reasm_queue = sctp_sbspace_sub(asoc->size_on_reasm_queue, chk->send_size);
6326 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
6327 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
6328 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
6330 sctp_m_freem(chk->data);
6333 sctp_free_a_chunk(stcb, chk);
6337 /* Ok that was fun, now we will drain all the inbound streams? */
6338 for (strmat = 0; strmat < asoc->streamincnt; strmat++) {
6339 ctl = TAILQ_FIRST(&asoc->strmin[strmat].inqueue);
6341 nctl = TAILQ_NEXT(ctl, next);
6342 if (compare_with_wrap(ctl->sinfo_tsn,
6343 cumulative_tsn_p1, MAX_TSN)) {
6344 /* Yep it is above cum-ack */
6346 tsn = ctl->sinfo_tsn;
6347 if (tsn >= asoc->mapping_array_base_tsn) {
6349 asoc->mapping_array_base_tsn;
6352 asoc->mapping_array_base_tsn) +
6355 asoc->size_on_all_streams = sctp_sbspace_sub(asoc->size_on_all_streams, ctl->length);
6356 sctp_ucount_decr(asoc->cnt_on_all_streams);
6358 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array,
6360 TAILQ_REMOVE(&asoc->strmin[strmat].inqueue,
6363 sctp_m_freem(ctl->data);
6366 sctp_free_remote_addr(ctl->whoFrom);
6367 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), ctl);
6368 SCTP_DECR_READQ_COUNT();
6374 * Question, should we go through the delivery queue? The only
6375 * reason things are on here is the app not reading OR a p-d-api up.
6376 * An attacker COULD send enough in to initiate the PD-API and then
6377 * send a bunch of stuff to other streams... these would wind up on
6378 * the delivery queue.. and then we would not get to them. But in
6379 * order to do this I then have to back-track and un-deliver
6380 * sequence numbers in streams.. el-yucko. I think for now we will
6381 * NOT look at the delivery queue and leave it to be something to
6382 * consider later. An alternative would be to abort the P-D-API with
6383 * a notification and then deliver the data.... Or another method
6384 * might be to keep track of how many times the situation occurs and
6385 * if we see a possible attack underway just abort the association.
6389 SCTPDBG(SCTP_DEBUG_PCB1, "Freed %d chunks from reneg harvest\n", cnt);
6394 * Now do we need to find a new
6395 * asoc->highest_tsn_inside_map?
6397 if (asoc->highest_tsn_inside_map >= asoc->mapping_array_base_tsn) {
6398 gap = asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn;
6400 gap = (MAX_TSN - asoc->mapping_array_base_tsn) +
6401 asoc->highest_tsn_inside_map + 1;
6403 if (gap >= (asoc->mapping_array_size << 3)) {
6405 * Something bad happened or cum-ack and high were
6406 * behind the base, but if so earlier checks should
6407 * have found NO data... wierd... we will start at
6408 * end of mapping array.
6410 SCTP_PRINTF("Gap was larger than array?? %d set to max:%d maparraymax:%x\n",
6412 (int)(asoc->mapping_array_size << 3),
6413 (int)asoc->highest_tsn_inside_map);
6414 gap = asoc->mapping_array_size << 3;
6417 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
6418 /* found the new highest */
6419 asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn + gap;
6420 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
6421 sctp_log_map(0, 8, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
6428 /* Nothing left in map */
6429 memset(asoc->mapping_array, 0, asoc->mapping_array_size);
6430 asoc->mapping_array_base_tsn = asoc->cumulative_tsn + 1;
6431 asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
6432 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
6433 sctp_log_map(0, 9, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
6436 asoc->last_revoke_count = cnt;
6437 (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
6438 /* sa_ignore NO_NULL_CHK */
6439 sctp_send_sack(stcb);
6440 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_DRAIN, SCTP_SO_NOT_LOCKED);
6441 reneged_asoc_ids[reneged_at] = sctp_get_associd(stcb);
6445 * Another issue, in un-setting the TSN's in the mapping array we
6446 * DID NOT adjust the higest_tsn marker. This will cause one of two
6447 * things to occur. It may cause us to do extra work in checking for
6448 * our mapping array movement. More importantly it may cause us to
6449 * SACK every datagram. This may not be a bad thing though since we
6450 * will recover once we get our cum-ack above and all this stuff we
6459 * We must walk the PCB lists for ALL associations here. The system
6460 * is LOW on MBUF's and needs help. This is where reneging will
6461 * occur. We really hope this does NOT happen!
6463 struct sctp_inpcb *inp;
6464 struct sctp_tcb *stcb;
6466 SCTP_INP_INFO_RLOCK();
6467 LIST_FOREACH(inp, &SCTP_BASE_INFO(listhead), sctp_list) {
6468 /* For each endpoint */
6469 SCTP_INP_RLOCK(inp);
6470 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6471 /* For each association */
6472 SCTP_TCB_LOCK(stcb);
6473 sctp_drain_mbufs(inp, stcb);
6474 SCTP_TCB_UNLOCK(stcb);
6476 SCTP_INP_RUNLOCK(inp);
6478 SCTP_INP_INFO_RUNLOCK();
6482 * start a new iterator
6483 * iterates through all endpoints and associations based on the pcb_state
6484 * flags and asoc_state. "af" (mandatory) is executed for all matching
6485 * assocs and "ef" (optional) is executed when the iterator completes.
6486 * "inpf" (optional) is executed for each new endpoint as it is being
6487 * iterated through. inpe (optional) is called when the inp completes
6488 * its way through all the stcbs.
6491 sctp_initiate_iterator(inp_func inpf,
6495 uint32_t pcb_features,
6496 uint32_t asoc_state,
6500 struct sctp_inpcb *s_inp,
6501 uint8_t chunk_output_off)
6503 struct sctp_iterator *it = NULL;
6508 SCTP_MALLOC(it, struct sctp_iterator *, sizeof(struct sctp_iterator),
6511 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
6514 memset(it, 0, sizeof(*it));
6515 it->function_assoc = af;
6516 it->function_inp = inpf;
6518 it->done_current_ep = 0;
6520 it->done_current_ep = 1;
6521 it->function_atend = ef;
6524 it->pcb_flags = pcb_state;
6525 it->pcb_features = pcb_features;
6526 it->asoc_state = asoc_state;
6527 it->function_inp_end = inpe;
6528 it->no_chunk_output = chunk_output_off;
6531 it->iterator_flags = SCTP_ITERATOR_DO_SINGLE_INP;
6533 SCTP_INP_INFO_RLOCK();
6534 it->inp = LIST_FIRST(&SCTP_BASE_INFO(listhead));
6536 SCTP_INP_INFO_RUNLOCK();
6537 it->iterator_flags = SCTP_ITERATOR_DO_ALL_INP;
6540 SCTP_IPI_ITERATOR_WQ_LOCK();
6542 SCTP_INP_INCR_REF(it->inp);
6544 TAILQ_INSERT_TAIL(&SCTP_BASE_INFO(iteratorhead), it, sctp_nxt_itr);
6545 #if defined(SCTP_USE_THREAD_BASED_ITERATOR)
6546 if (SCTP_BASE_INFO(iterator_running) == 0) {
6547 sctp_wakeup_iterator();
6549 SCTP_IPI_ITERATOR_WQ_UNLOCK();
6552 SCTP_INP_DECR_REF(it->inp);
6553 SCTP_IPI_ITERATOR_WQ_UNLOCK();
6554 /* Init the timer */
6555 SCTP_OS_TIMER_INIT(&it->tmr.timer);
6556 /* add to the list of all iterators */
6557 sctp_timer_start(SCTP_TIMER_TYPE_ITERATOR, (struct sctp_inpcb *)it,
6560 /* sa_ignore MEMLEAK {memory is put on the tailq for the iterator} */