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);
3278 inp->sctp_socket = NULL;
3282 inp->sctp_socket = NULL;
3283 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) !=
3284 SCTP_PCB_FLAGS_UNBOUND) {
3286 * ok, this guy has been bound. It's port is somewhere in
3287 * the SCTP_BASE_INFO(hash table). Remove it!
3289 LIST_REMOVE(inp, sctp_hash);
3290 inp->sctp_flags |= SCTP_PCB_FLAGS_UNBOUND;
3293 * If there is a timer running to kill us, forget it, since it may
3294 * have a contest on the INP lock.. which would cause us to die ...
3297 for ((asoc = LIST_FIRST(&inp->sctp_asoc_list)); asoc != NULL;
3299 nasoc = LIST_NEXT(asoc, sctp_tcblist);
3300 if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3304 /* Free associations that are NOT killing us */
3305 SCTP_TCB_LOCK(asoc);
3306 if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_COOKIE_WAIT) &&
3307 ((asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0)) {
3308 struct mbuf *op_err;
3311 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
3312 0, M_DONTWAIT, 1, MT_DATA);
3314 /* Fill in the user initiated abort */
3315 struct sctp_paramhdr *ph;
3317 SCTP_BUF_LEN(op_err) = (sizeof(struct sctp_paramhdr) +
3319 ph = mtod(op_err, struct sctp_paramhdr *);
3320 ph->param_type = htons(
3321 SCTP_CAUSE_USER_INITIATED_ABT);
3322 ph->param_length = htons(SCTP_BUF_LEN(op_err));
3323 ippp = (uint32_t *) (ph + 1);
3324 *ippp = htonl(SCTP_FROM_SCTP_PCB + SCTP_LOC_7);
3327 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_7;
3328 #if defined(SCTP_PANIC_ON_ABORT)
3329 panic("inpcb_free does an abort");
3331 sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED);
3332 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3333 } else if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3335 SCTP_TCB_UNLOCK(asoc);
3338 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
3339 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3340 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3342 if (sctp_free_assoc(inp, asoc, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_8) == 0) {
3347 /* Ok we have someone out there that will kill us */
3348 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
3349 SCTP_INP_WUNLOCK(inp);
3350 SCTP_ASOC_CREATE_UNLOCK(inp);
3351 SCTP_INP_INFO_WUNLOCK();
3352 SCTP_ITERATOR_UNLOCK();
3353 #ifdef SCTP_LOG_CLOSING
3354 sctp_log_closing(inp, NULL, 3);
3358 if ((inp->refcount) || (inp->sctp_flags & SCTP_PCB_FLAGS_CLOSE_IP)) {
3359 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
3360 sctp_timer_start(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL);
3361 SCTP_INP_WUNLOCK(inp);
3362 SCTP_ASOC_CREATE_UNLOCK(inp);
3363 SCTP_INP_INFO_WUNLOCK();
3364 SCTP_ITERATOR_UNLOCK();
3365 #ifdef SCTP_LOG_CLOSING
3366 sctp_log_closing(inp, NULL, 4);
3370 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
3371 inp->sctp_ep.signature_change.type = 0;
3372 inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_ALLGONE;
3374 #ifdef SCTP_LOG_CLOSING
3375 sctp_log_closing(inp, NULL, 5);
3378 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
3379 inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NONE;
3380 /* Clear the read queue */
3381 /* sa_ignore FREED_MEMORY */
3382 while ((sq = TAILQ_FIRST(&inp->read_queue)) != NULL) {
3383 /* Its only abandoned if it had data left */
3385 SCTP_STAT_INCR(sctps_left_abandon);
3387 TAILQ_REMOVE(&inp->read_queue, sq, next);
3388 sctp_free_remote_addr(sq->whoFrom);
3390 so->so_rcv.sb_cc -= sq->length;
3392 sctp_m_freem(sq->data);
3396 * no need to free the net count, since at this point all
3399 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), sq);
3400 SCTP_DECR_READQ_COUNT();
3402 /* Now the sctp_pcb things */
3404 * free each asoc if it is not already closed/free. we can't use the
3405 * macro here since le_next will get freed as part of the
3406 * sctp_free_assoc() call.
3411 ipsec4_delete_pcbpolicy(ip_pcb);
3414 /* Unlocks not needed since the socket is gone now */
3416 if (ip_pcb->inp_options) {
3417 (void)sctp_m_free(ip_pcb->inp_options);
3418 ip_pcb->inp_options = 0;
3420 if (ip_pcb->inp_moptions) {
3421 inp_freemoptions(ip_pcb->inp_moptions);
3422 ip_pcb->inp_moptions = 0;
3425 if (ip_pcb->inp_vflag & INP_IPV6) {
3426 struct in6pcb *in6p;
3428 in6p = (struct in6pcb *)inp;
3429 ip6_freepcbopts(in6p->in6p_outputopts);
3432 ip_pcb->inp_vflag = 0;
3433 /* free up authentication fields */
3434 if (inp->sctp_ep.local_auth_chunks != NULL)
3435 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
3436 if (inp->sctp_ep.local_hmacs != NULL)
3437 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
3439 shared_key = LIST_FIRST(&inp->sctp_ep.shared_keys);
3440 while (shared_key) {
3441 LIST_REMOVE(shared_key, next);
3442 sctp_free_sharedkey(shared_key);
3443 /* sa_ignore FREED_MEMORY */
3444 shared_key = LIST_FIRST(&inp->sctp_ep.shared_keys);
3447 inp_save = LIST_NEXT(inp, sctp_list);
3448 LIST_REMOVE(inp, sctp_list);
3450 /* fix any iterators only after out of the list */
3451 sctp_iterator_inp_being_freed(inp, inp_save);
3453 * if we have an address list the following will free the list of
3454 * ifaddr's that are set into this ep. Again macro limitations here,
3455 * since the LIST_FOREACH could be a bad idea.
3457 for ((laddr = LIST_FIRST(&inp->sctp_addr_list)); laddr != NULL;
3459 nladdr = LIST_NEXT(laddr, sctp_nxt_addr);
3460 sctp_remove_laddr(laddr);
3463 #ifdef SCTP_TRACK_FREED_ASOCS
3465 for ((asoc = LIST_FIRST(&inp->sctp_asoc_free_list)); asoc != NULL;
3467 nasoc = LIST_NEXT(asoc, sctp_tcblist);
3468 LIST_REMOVE(asoc, sctp_tcblist);
3469 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), asoc);
3470 SCTP_DECR_ASOC_COUNT();
3472 /* *** END TEMP CODE *** */
3474 /* Now lets see about freeing the EP hash table. */
3475 if (inp->sctp_tcbhash != NULL) {
3476 SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark);
3477 inp->sctp_tcbhash = NULL;
3479 /* Now we must put the ep memory back into the zone pool */
3480 INP_LOCK_DESTROY(&inp->ip_inp.inp);
3481 SCTP_INP_LOCK_DESTROY(inp);
3482 SCTP_INP_READ_DESTROY(inp);
3483 SCTP_ASOC_CREATE_LOCK_DESTROY(inp);
3484 SCTP_INP_INFO_WUNLOCK();
3485 SCTP_ITERATOR_UNLOCK();
3486 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
3487 SCTP_DECR_EP_COUNT();
3492 sctp_findnet(struct sctp_tcb *stcb, struct sockaddr *addr)
3494 struct sctp_nets *net;
3496 /* locate the address */
3497 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3498 if (sctp_cmpaddr(addr, (struct sockaddr *)&net->ro._l_addr))
3506 sctp_is_address_on_local_host(struct sockaddr *addr, uint32_t vrf_id)
3508 struct sctp_ifa *sctp_ifa;
3510 sctp_ifa = sctp_find_ifa_by_addr(addr, vrf_id, SCTP_ADDR_NOT_LOCKED);
3519 * add's a remote endpoint address, done with the INIT/INIT-ACK as well as
3520 * when a ASCONF arrives that adds it. It will also initialize all the cwnd
3524 sctp_add_remote_addr(struct sctp_tcb *stcb, struct sockaddr *newaddr,
3525 int set_scope, int from)
3528 * The following is redundant to the same lines in the
3529 * sctp_aloc_assoc() but is needed since other's call the add
3532 struct sctp_nets *net, *netfirst;
3535 SCTPDBG(SCTP_DEBUG_PCB1, "Adding an address (from:%d) to the peer: ",
3537 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, newaddr);
3539 netfirst = sctp_findnet(stcb, newaddr);
3542 * Lie and return ok, we don't want to make the association
3543 * go away for this behavior. It will happen in the TCP
3544 * model in a connected socket. It does not reach the hash
3545 * table until after the association is built so it can't be
3546 * found. Mark as reachable, since the initial creation will
3547 * have been cleared and the NOT_IN_ASSOC flag will have
3548 * been added... and we don't want to end up removing it
3551 if (netfirst->dest_state & SCTP_ADDR_UNCONFIRMED) {
3552 netfirst->dest_state = (SCTP_ADDR_REACHABLE |
3553 SCTP_ADDR_UNCONFIRMED);
3555 netfirst->dest_state = SCTP_ADDR_REACHABLE;
3561 if (newaddr->sa_family == AF_INET) {
3562 struct sockaddr_in *sin;
3564 sin = (struct sockaddr_in *)newaddr;
3565 if (sin->sin_addr.s_addr == 0) {
3566 /* Invalid address */
3569 /* zero out the bzero area */
3570 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
3572 /* assure len is set */
3573 sin->sin_len = sizeof(struct sockaddr_in);
3575 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
3576 stcb->ipv4_local_scope = 1;
3578 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
3579 stcb->asoc.ipv4_local_scope = 1;
3581 #endif /* SCTP_DONT_DO_PRIVADDR_SCOPE */
3583 /* Validate the address is in scope */
3584 if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) &&
3585 (stcb->asoc.ipv4_local_scope == 0)) {
3590 } else if (newaddr->sa_family == AF_INET6) {
3591 struct sockaddr_in6 *sin6;
3593 sin6 = (struct sockaddr_in6 *)newaddr;
3594 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3595 /* Invalid address */
3598 /* assure len is set */
3599 sin6->sin6_len = sizeof(struct sockaddr_in6);
3601 if (sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id)) {
3602 stcb->asoc.loopback_scope = 1;
3603 stcb->asoc.local_scope = 0;
3604 stcb->asoc.ipv4_local_scope = 1;
3605 stcb->asoc.site_scope = 1;
3606 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
3608 * If the new destination is a LINK_LOCAL we
3609 * must have common site scope. Don't set
3610 * the local scope since we may not share
3611 * all links, only loopback can do this.
3612 * Links on the local network would also be
3613 * on our private network for v4 too.
3615 stcb->asoc.ipv4_local_scope = 1;
3616 stcb->asoc.site_scope = 1;
3617 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
3619 * If the new destination is SITE_LOCAL then
3620 * we must have site scope in common.
3622 stcb->asoc.site_scope = 1;
3625 /* Validate the address is in scope */
3626 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) &&
3627 (stcb->asoc.loopback_scope == 0)) {
3629 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) &&
3630 (stcb->asoc.local_scope == 0)) {
3632 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) &&
3633 (stcb->asoc.site_scope == 0)) {
3639 /* not supported family type */
3642 net = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_net), struct sctp_nets);
3646 SCTP_INCR_RADDR_COUNT();
3647 bzero(net, sizeof(*net));
3648 (void)SCTP_GETTIME_TIMEVAL(&net->start_time);
3649 memcpy(&net->ro._l_addr, newaddr, newaddr->sa_len);
3650 if (newaddr->sa_family == AF_INET) {
3651 ((struct sockaddr_in *)&net->ro._l_addr)->sin_port = stcb->rport;
3652 } else if (newaddr->sa_family == AF_INET6) {
3653 ((struct sockaddr_in6 *)&net->ro._l_addr)->sin6_port = stcb->rport;
3655 net->addr_is_local = sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id);
3656 if (net->addr_is_local && ((set_scope || (from == SCTP_ADDR_IS_CONFIRMED)))) {
3657 stcb->asoc.loopback_scope = 1;
3658 stcb->asoc.ipv4_local_scope = 1;
3659 stcb->asoc.local_scope = 0;
3660 stcb->asoc.site_scope = 1;
3663 net->failure_threshold = stcb->asoc.def_net_failure;
3664 if (addr_inscope == 0) {
3665 net->dest_state = (SCTP_ADDR_REACHABLE |
3666 SCTP_ADDR_OUT_OF_SCOPE);
3668 if (from == SCTP_ADDR_IS_CONFIRMED)
3669 /* SCTP_ADDR_IS_CONFIRMED is passed by connect_x */
3670 net->dest_state = SCTP_ADDR_REACHABLE;
3672 net->dest_state = SCTP_ADDR_REACHABLE |
3673 SCTP_ADDR_UNCONFIRMED;
3676 * We set this to 0, the timer code knows that this means its an
3680 net->RTO_measured = 0;
3681 stcb->asoc.numnets++;
3682 *(&net->ref_count) = 1;
3683 net->tos_flowlabel = 0;
3684 if (SCTP_BASE_SYSCTL(sctp_udp_tunneling_for_client_enable)) {
3685 net->port = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
3690 if (newaddr->sa_family == AF_INET)
3691 net->tos_flowlabel = stcb->asoc.default_tos;
3694 if (newaddr->sa_family == AF_INET6)
3695 net->tos_flowlabel = stcb->asoc.default_flowlabel;
3697 /* Init the timer structure */
3698 SCTP_OS_TIMER_INIT(&net->rxt_timer.timer);
3699 SCTP_OS_TIMER_INIT(&net->fr_timer.timer);
3700 SCTP_OS_TIMER_INIT(&net->pmtu_timer.timer);
3702 /* Now generate a route for this guy */
3704 /* KAME hack: embed scopeid */
3705 if (newaddr->sa_family == AF_INET6) {
3706 struct sockaddr_in6 *sin6;
3708 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3709 (void)sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone));
3710 sin6->sin6_scope_id = 0;
3713 SCTP_RTALLOC((sctp_route_t *) & net->ro, stcb->asoc.vrf_id);
3715 if (SCTP_ROUTE_HAS_VALID_IFN(&net->ro)) {
3716 /* Get source address */
3717 net->ro._s_addr = sctp_source_address_selection(stcb->sctp_ep,
3719 (sctp_route_t *) & net->ro,
3723 /* Now get the interface MTU */
3724 if (net->ro._s_addr && net->ro._s_addr->ifn_p) {
3725 net->mtu = SCTP_GATHER_MTU_FROM_INTFC(net->ro._s_addr->ifn_p);
3729 #ifdef SCTP_PRINT_FOR_B_AND_M
3730 SCTP_PRINTF("We have found an interface mtu of %d\n", net->mtu);
3732 if (net->mtu == 0) {
3734 net->mtu = SCTP_DEFAULT_MTU;
3738 rmtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, net->ro.ro_rt);
3739 #ifdef SCTP_PRINT_FOR_B_AND_M
3740 SCTP_PRINTF("The route mtu is %d\n", rmtu);
3744 * Start things off to match mtu of
3747 SCTP_SET_MTU_OF_ROUTE(&net->ro._l_addr.sa,
3748 net->ro.ro_rt, net->mtu);
3751 * we take the route mtu over the interface,
3752 * since the route may be leading out the
3753 * loopback, or a different interface.
3758 if (from == SCTP_ALLOC_ASOC) {
3759 #ifdef SCTP_PRINT_FOR_B_AND_M
3760 SCTP_PRINTF("New assoc sets mtu to :%d\n", net->mtu);
3762 stcb->asoc.smallest_mtu = net->mtu;
3765 net->mtu = stcb->asoc.smallest_mtu;
3768 if (newaddr->sa_family == AF_INET6) {
3769 struct sockaddr_in6 *sin6;
3771 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3772 (void)sa6_recoverscope(sin6);
3776 net->mtu -= sizeof(struct udphdr);
3778 if (stcb->asoc.smallest_mtu > net->mtu) {
3779 #ifdef SCTP_PRINT_FOR_B_AND_M
3780 SCTP_PRINTF("new address mtu:%d smaller than smallest:%d\n",
3781 net->mtu, stcb->asoc.smallest_mtu);
3783 stcb->asoc.smallest_mtu = net->mtu;
3785 /* JRS - Use the congestion control given in the CC module */
3786 stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
3789 * CMT: CUC algo - set find_pseudo_cumack to TRUE (1) at beginning
3790 * of assoc (2005/06/27, iyengar@cis.udel.edu)
3792 net->find_pseudo_cumack = 1;
3793 net->find_rtx_pseudo_cumack = 1;
3794 net->src_addr_selected = 0;
3795 netfirst = TAILQ_FIRST(&stcb->asoc.nets);
3796 if (net->ro.ro_rt == NULL) {
3797 /* Since we have no route put it at the back */
3798 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
3799 } else if (netfirst == NULL) {
3800 /* We are the first one in the pool. */
3801 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
3802 } else if (netfirst->ro.ro_rt == NULL) {
3804 * First one has NO route. Place this one ahead of the first
3807 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
3808 } else if (net->ro.ro_rt->rt_ifp != netfirst->ro.ro_rt->rt_ifp) {
3810 * This one has a different interface than the one at the
3811 * top of the list. Place it ahead.
3813 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
3816 * Ok we have the same interface as the first one. Move
3817 * forward until we find either a) one with a NULL route...
3818 * insert ahead of that b) one with a different ifp.. insert
3819 * after that. c) end of the list.. insert at the tail.
3821 struct sctp_nets *netlook;
3824 netlook = TAILQ_NEXT(netfirst, sctp_next);
3825 if (netlook == NULL) {
3826 /* End of the list */
3827 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
3829 } else if (netlook->ro.ro_rt == NULL) {
3830 /* next one has NO route */
3831 TAILQ_INSERT_BEFORE(netfirst, net, sctp_next);
3833 } else if (netlook->ro.ro_rt->rt_ifp != net->ro.ro_rt->rt_ifp) {
3834 TAILQ_INSERT_AFTER(&stcb->asoc.nets, netlook,
3840 } while (netlook != NULL);
3843 /* got to have a primary set */
3844 if (stcb->asoc.primary_destination == 0) {
3845 stcb->asoc.primary_destination = net;
3846 } else if ((stcb->asoc.primary_destination->ro.ro_rt == NULL) &&
3848 ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) {
3849 /* No route to current primary adopt new primary */
3850 stcb->asoc.primary_destination = net;
3852 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb,
3854 /* Validate primary is first */
3855 net = TAILQ_FIRST(&stcb->asoc.nets);
3856 if ((net != stcb->asoc.primary_destination) &&
3857 (stcb->asoc.primary_destination)) {
3859 * first one on the list is NOT the primary sctp_cmpaddr()
3860 * is much more efficent if the primary is the first on the
3863 TAILQ_REMOVE(&stcb->asoc.nets,
3864 stcb->asoc.primary_destination, sctp_next);
3865 TAILQ_INSERT_HEAD(&stcb->asoc.nets,
3866 stcb->asoc.primary_destination, sctp_next);
3873 * allocate an association and add it to the endpoint. The caller must be
3874 * careful to add all additional addresses once they are know right away or
3875 * else the assoc will be may experience a blackout scenario.
3878 sctp_aloc_assoc(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
3879 int for_a_init, int *error, uint32_t override_tag, uint32_t vrf_id,
3883 /* note the p argument is only valid in unbound sockets */
3885 struct sctp_tcb *stcb;
3886 struct sctp_association *asoc;
3887 struct sctpasochead *head;
3892 * Assumption made here: Caller has done a
3893 * sctp_findassociation_ep_addr(ep, addr's); to make sure the
3894 * address does not exist already.
3896 if (SCTP_BASE_INFO(ipi_count_asoc) >= SCTP_MAX_NUM_OF_ASOC) {
3897 /* Hit max assoc, sorry no more */
3898 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
3902 if (firstaddr == NULL) {
3903 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3907 SCTP_INP_RLOCK(inp);
3908 if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
3909 ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) ||
3910 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
3912 * If its in the TCP pool, its NOT allowed to create an
3913 * association. The parent listener needs to call
3914 * sctp_aloc_assoc.. or the one-2-many socket. If a peeled
3915 * off, or connected one does this.. its an error.
3917 SCTP_INP_RUNLOCK(inp);
3918 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3922 SCTPDBG(SCTP_DEBUG_PCB3, "Allocate an association for peer:");
3925 SCTPDBG_ADDR(SCTP_DEBUG_PCB3, firstaddr);
3926 SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
3927 ntohs(((struct sockaddr_in *)firstaddr)->sin_port));
3929 SCTPDBG(SCTP_DEBUG_PCB3, "None\n");
3931 #endif /* SCTP_DEBUG */
3932 if (firstaddr->sa_family == AF_INET) {
3933 struct sockaddr_in *sin;
3935 sin = (struct sockaddr_in *)firstaddr;
3936 if ((sin->sin_port == 0) || (sin->sin_addr.s_addr == 0)) {
3937 /* Invalid address */
3938 SCTP_INP_RUNLOCK(inp);
3939 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3943 rport = sin->sin_port;
3944 } else if (firstaddr->sa_family == AF_INET6) {
3945 struct sockaddr_in6 *sin6;
3947 sin6 = (struct sockaddr_in6 *)firstaddr;
3948 if ((sin6->sin6_port == 0) ||
3949 (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))) {
3950 /* Invalid address */
3951 SCTP_INP_RUNLOCK(inp);
3952 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3956 rport = sin6->sin6_port;
3958 /* not supported family type */
3959 SCTP_INP_RUNLOCK(inp);
3960 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3964 SCTP_INP_RUNLOCK(inp);
3965 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
3967 * If you have not performed a bind, then we need to do the
3968 * ephemerial bind for you.
3970 if ((err = sctp_inpcb_bind(inp->sctp_socket,
3971 (struct sockaddr *)NULL,
3972 (struct sctp_ifa *)NULL,
3975 /* bind error, probably perm */
3980 stcb = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_asoc), struct sctp_tcb);
3982 /* out of memory? */
3983 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
3987 SCTP_INCR_ASOC_COUNT();
3989 bzero(stcb, sizeof(*stcb));
3991 SCTP_TCB_LOCK_INIT(stcb);
3992 SCTP_TCB_SEND_LOCK_INIT(stcb);
3993 /* setup back pointer's */
3994 stcb->sctp_ep = inp;
3995 stcb->sctp_socket = inp->sctp_socket;
3996 if ((err = sctp_init_asoc(inp, stcb, for_a_init, override_tag, vrf_id))) {
3998 SCTP_TCB_LOCK_DESTROY(stcb);
3999 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
4000 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
4001 SCTP_DECR_ASOC_COUNT();
4006 stcb->rport = rport;
4007 SCTP_INP_INFO_WLOCK();
4008 SCTP_INP_WLOCK(inp);
4009 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4010 /* inpcb freed while alloc going on */
4011 SCTP_TCB_LOCK_DESTROY(stcb);
4012 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
4013 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
4014 SCTP_INP_WUNLOCK(inp);
4015 SCTP_INP_INFO_WUNLOCK();
4016 SCTP_DECR_ASOC_COUNT();
4017 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4021 SCTP_TCB_LOCK(stcb);
4023 /* now that my_vtag is set, add it to the hash */
4024 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
4025 SCTP_BASE_INFO(hashasocmark))];
4026 /* put it in the bucket in the vtag hash of assoc's for the system */
4027 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
4028 sctp_delete_from_timewait(stcb->asoc.my_vtag);
4030 SCTP_INP_INFO_WUNLOCK();
4032 if ((err = sctp_add_remote_addr(stcb, firstaddr, SCTP_DO_SETSCOPE, SCTP_ALLOC_ASOC))) {
4033 /* failure.. memory error? */
4034 if (asoc->strmout) {
4035 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
4036 asoc->strmout = NULL;
4038 if (asoc->mapping_array) {
4039 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
4040 asoc->mapping_array = NULL;
4042 SCTP_DECR_ASOC_COUNT();
4043 SCTP_TCB_LOCK_DESTROY(stcb);
4044 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
4045 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
4046 SCTP_INP_WUNLOCK(inp);
4047 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
4051 /* Init all the timers */
4052 SCTP_OS_TIMER_INIT(&asoc->hb_timer.timer);
4053 SCTP_OS_TIMER_INIT(&asoc->dack_timer.timer);
4054 SCTP_OS_TIMER_INIT(&asoc->strreset_timer.timer);
4055 SCTP_OS_TIMER_INIT(&asoc->asconf_timer.timer);
4056 SCTP_OS_TIMER_INIT(&asoc->shut_guard_timer.timer);
4057 SCTP_OS_TIMER_INIT(&asoc->autoclose_timer.timer);
4058 SCTP_OS_TIMER_INIT(&asoc->delayed_event_timer.timer);
4059 SCTP_OS_TIMER_INIT(&asoc->delete_prim_timer.timer);
4061 LIST_INSERT_HEAD(&inp->sctp_asoc_list, stcb, sctp_tcblist);
4062 /* now file the port under the hash as well */
4063 if (inp->sctp_tcbhash != NULL) {
4064 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(stcb->rport,
4065 inp->sctp_hashmark)];
4066 LIST_INSERT_HEAD(head, stcb, sctp_tcbhash);
4068 SCTP_INP_WUNLOCK(inp);
4069 SCTPDBG(SCTP_DEBUG_PCB1, "Association %p now allocated\n", stcb);
4075 sctp_remove_net(struct sctp_tcb *stcb, struct sctp_nets *net)
4077 struct sctp_association *asoc;
4081 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
4082 if (net == asoc->primary_destination) {
4084 struct sctp_nets *lnet;
4086 lnet = TAILQ_FIRST(&asoc->nets);
4088 * Mobility adaptation Ideally, if deleted destination is
4089 * the primary, it becomes a fast retransmission trigger by
4090 * the subsequent SET PRIMARY. (by micchie)
4092 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
4093 SCTP_MOBILITY_BASE) ||
4094 sctp_is_mobility_feature_on(stcb->sctp_ep,
4095 SCTP_MOBILITY_FASTHANDOFF)) {
4096 SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: primary dst is deleting\n");
4097 if (asoc->deleted_primary != NULL) {
4098 SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: deleted primary may be already stored\n");
4101 asoc->deleted_primary = net;
4102 atomic_add_int(&net->ref_count, 1);
4103 memset(&net->lastsa, 0, sizeof(net->lastsa));
4104 memset(&net->lastsv, 0, sizeof(net->lastsv));
4105 sctp_mobility_feature_on(stcb->sctp_ep,
4106 SCTP_MOBILITY_PRIM_DELETED);
4107 sctp_timer_start(SCTP_TIMER_TYPE_PRIM_DELETED,
4108 stcb->sctp_ep, stcb, NULL);
4111 /* Try to find a confirmed primary */
4112 asoc->primary_destination = sctp_find_alternate_net(stcb, lnet, 0);
4114 if (net == asoc->last_data_chunk_from) {
4116 asoc->last_data_chunk_from = TAILQ_FIRST(&asoc->nets);
4118 if (net == asoc->last_control_chunk_from) {
4120 asoc->last_control_chunk_from = NULL;
4122 sctp_free_remote_addr(net);
4126 * remove a remote endpoint address from an association, it will fail if the
4127 * address does not exist.
4130 sctp_del_remote_addr(struct sctp_tcb *stcb, struct sockaddr *remaddr)
4133 * Here we need to remove a remote address. This is quite simple, we
4134 * first find it in the list of address for the association
4135 * (tasoc->asoc.nets) and then if it is there, we do a LIST_REMOVE
4136 * on that item. Note we do not allow it to be removed if there are
4137 * no other addresses.
4139 struct sctp_association *asoc;
4140 struct sctp_nets *net, *net_tmp;
4144 /* locate the address */
4145 for (net = TAILQ_FIRST(&asoc->nets); net != NULL; net = net_tmp) {
4146 net_tmp = TAILQ_NEXT(net, sctp_next);
4147 if (net->ro._l_addr.sa.sa_family != remaddr->sa_family) {
4150 if (sctp_cmpaddr((struct sockaddr *)&net->ro._l_addr,
4152 /* we found the guy */
4153 if (asoc->numnets < 2) {
4154 /* Must have at LEAST two remote addresses */
4157 sctp_remove_net(stcb, net);
4167 sctp_delete_from_timewait(uint32_t tag)
4169 struct sctpvtaghead *chain;
4170 struct sctp_tagblock *twait_block;
4174 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
4175 if (!SCTP_LIST_EMPTY(chain)) {
4176 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
4177 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
4178 if (twait_block->vtag_block[i].v_tag == tag) {
4179 twait_block->vtag_block[i].tv_sec_at_expire = 0;
4180 twait_block->vtag_block[i].v_tag = 0;
4192 sctp_is_in_timewait(uint32_t tag)
4194 struct sctpvtaghead *chain;
4195 struct sctp_tagblock *twait_block;
4199 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
4200 if (!SCTP_LIST_EMPTY(chain)) {
4201 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
4202 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
4203 if (twait_block->vtag_block[i].v_tag == tag) {
4217 sctp_add_vtag_to_timewait(uint32_t tag, uint32_t time)
4219 struct sctpvtaghead *chain;
4220 struct sctp_tagblock *twait_block;
4224 (void)SCTP_GETTIME_TIMEVAL(&now);
4225 chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
4227 if (!SCTP_LIST_EMPTY(chain)) {
4228 /* Block(s) present, lets find space, and expire on the fly */
4229 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
4230 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
4231 if ((twait_block->vtag_block[i].v_tag == 0) &&
4233 twait_block->vtag_block[i].tv_sec_at_expire =
4235 twait_block->vtag_block[i].v_tag = tag;
4237 } else if ((twait_block->vtag_block[i].v_tag) &&
4238 ((long)twait_block->vtag_block[i].tv_sec_at_expire < now.tv_sec)) {
4239 /* Audit expires this guy */
4240 twait_block->vtag_block[i].tv_sec_at_expire = 0;
4241 twait_block->vtag_block[i].v_tag = 0;
4243 /* Reuse it for my new tag */
4244 twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec + time;
4245 twait_block->vtag_block[0].v_tag = tag;
4252 * We only do up to the block where we can
4253 * place our tag for audits
4259 /* Need to add a new block to chain */
4261 SCTP_MALLOC(twait_block, struct sctp_tagblock *,
4262 sizeof(struct sctp_tagblock), SCTP_M_TIMW);
4263 if (twait_block == NULL) {
4266 memset(twait_block, 0, sizeof(struct sctp_tagblock));
4267 LIST_INSERT_HEAD(chain, twait_block, sctp_nxt_tagblock);
4268 twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec + time;
4269 twait_block->vtag_block[0].v_tag = tag;
4275 sctp_iterator_asoc_being_freed(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
4277 struct sctp_iterator *it;
4280 * Unlock the tcb lock we do this so we avoid a dead lock scenario
4281 * where the iterator is waiting on the TCB lock and the TCB lock is
4282 * waiting on the iterator lock.
4284 it = stcb->asoc.stcb_starting_point_for_iterator;
4288 if (it->inp != stcb->sctp_ep) {
4289 /* hmm, focused on the wrong one? */
4292 if (it->stcb != stcb) {
4295 it->stcb = LIST_NEXT(stcb, sctp_tcblist);
4296 if (it->stcb == NULL) {
4297 /* done with all asoc's in this assoc */
4298 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
4301 it->inp = LIST_NEXT(inp, sctp_list);
4308 * Free the association after un-hashing the remote port. This
4309 * function ALWAYS returns holding NO LOCK on the stcb. It DOES
4310 * expect that the input to this function IS a locked TCB.
4311 * It will return 0, if it did NOT destroy the association (instead
4312 * it unlocks it. It will return NON-zero if it either destroyed the
4313 * association OR the association is already destroyed.
4316 sctp_free_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int from_inpcbfree, int from_location)
4319 struct sctp_association *asoc;
4320 struct sctp_nets *net, *prev;
4321 struct sctp_laddr *laddr;
4322 struct sctp_tmit_chunk *chk;
4323 struct sctp_asconf_addr *aparam;
4324 struct sctp_asconf_ack *aack;
4325 struct sctp_stream_reset_list *liste;
4326 struct sctp_queued_to_read *sq;
4327 struct sctp_stream_queue_pending *sp;
4328 sctp_sharedkey_t *shared_key;
4333 /* first, lets purge the entry from the hash table. */
4335 #ifdef SCTP_LOG_CLOSING
4336 sctp_log_closing(inp, stcb, 6);
4338 if (stcb->asoc.state == 0) {
4339 #ifdef SCTP_LOG_CLOSING
4340 sctp_log_closing(inp, NULL, 7);
4342 /* there is no asoc, really TSNH :-0 */
4346 if (stcb->freed_from_where == 0) {
4347 /* Only record the first place free happened from */
4348 stcb->freed_from_where = from_location;
4353 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4354 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
4355 /* nothing around */
4358 so = inp->sctp_socket;
4361 * We used timer based freeing if a reader or writer is in the way.
4362 * So we first check if we are actually being called from a timer,
4363 * if so we abort early if a reader or writer is still in the way.
4365 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) &&
4366 (from_inpcbfree == SCTP_NORMAL_PROC)) {
4368 * is it the timer driving us? if so are the reader/writers
4371 if (stcb->asoc.refcnt) {
4372 /* nope, reader or writer in the way */
4373 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
4374 /* no asoc destroyed */
4375 SCTP_TCB_UNLOCK(stcb);
4376 #ifdef SCTP_LOG_CLOSING
4377 sctp_log_closing(inp, stcb, 8);
4382 /* now clean up any other timers */
4383 (void)SCTP_OS_TIMER_STOP(&asoc->hb_timer.timer);
4384 asoc->hb_timer.self = NULL;
4385 (void)SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
4386 asoc->dack_timer.self = NULL;
4387 (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
4389 * For stream reset we don't blast this unless
4390 * it is a str-reset timer, it might be the
4391 * free-asoc timer which we DON'T want to
4394 if (asoc->strreset_timer.type == SCTP_TIMER_TYPE_STRRESET)
4395 asoc->strreset_timer.self = NULL;
4396 (void)SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
4397 asoc->asconf_timer.self = NULL;
4398 (void)SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
4399 asoc->autoclose_timer.self = NULL;
4400 (void)SCTP_OS_TIMER_STOP(&asoc->shut_guard_timer.timer);
4401 asoc->shut_guard_timer.self = NULL;
4402 (void)SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
4403 asoc->delayed_event_timer.self = NULL;
4404 /* Mobility adaptation */
4405 (void)SCTP_OS_TIMER_STOP(&asoc->delete_prim_timer.timer);
4406 asoc->delete_prim_timer.self = NULL;
4407 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4408 (void)SCTP_OS_TIMER_STOP(&net->fr_timer.timer);
4409 net->fr_timer.self = NULL;
4410 (void)SCTP_OS_TIMER_STOP(&net->rxt_timer.timer);
4411 net->rxt_timer.self = NULL;
4412 (void)SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
4413 net->pmtu_timer.self = NULL;
4415 /* Now the read queue needs to be cleaned up (only once) */
4417 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0) {
4418 stcb->asoc.state |= SCTP_STATE_ABOUT_TO_BE_FREED;
4419 SCTP_INP_READ_LOCK(inp);
4420 TAILQ_FOREACH(sq, &inp->read_queue, next) {
4421 if (sq->stcb == stcb) {
4422 sq->do_not_ref_stcb = 1;
4423 sq->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
4425 * If there is no end, there never will be
4428 if (sq->end_added == 0) {
4429 /* Held for PD-API clear that. */
4430 sq->pdapi_aborted = 1;
4431 sq->held_length = 0;
4432 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT) && (so != NULL)) {
4434 * Need to add a PD-API
4435 * aborted indication.
4436 * Setting the control_pdapi
4437 * assures that it will be
4438 * added right after this
4443 stcb->asoc.control_pdapi = sq;
4444 strseq = (sq->sinfo_stream << 16) | sq->sinfo_ssn;
4445 sctp_notify_partial_delivery_indication(stcb,
4446 SCTP_PARTIAL_DELIVERY_ABORTED, 1, strseq);
4447 stcb->asoc.control_pdapi = NULL;
4450 /* Add an end to wake them */
4455 SCTP_INP_READ_UNLOCK(inp);
4456 if (stcb->block_entry) {
4458 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PCB, ECONNRESET);
4459 stcb->block_entry->error = ECONNRESET;
4460 stcb->block_entry = NULL;
4463 if (stcb->asoc.refcnt) {
4465 * reader or writer in the way, we have hopefully given him
4466 * something to chew on above.
4468 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
4469 SCTP_TCB_UNLOCK(stcb);
4470 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4471 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
4472 /* nothing around */
4475 /* Wake any reader/writers */
4476 sctp_sorwakeup(inp, so);
4477 sctp_sowwakeup(inp, so);
4479 #ifdef SCTP_LOG_CLOSING
4480 sctp_log_closing(inp, stcb, 9);
4482 /* no asoc destroyed */
4485 #ifdef SCTP_LOG_CLOSING
4486 sctp_log_closing(inp, stcb, 10);
4489 * When I reach here, no others want to kill the assoc yet.. and I
4490 * own the lock. Now its possible an abort comes in when I do the
4491 * lock exchange below to grab all the locks to do the final take
4492 * out. to prevent this we increment the count, which will start a
4493 * timer and blow out above thus assuring us that we hold exclusive
4494 * killing of the asoc. Note that after getting back the TCB lock we
4495 * will go ahead and increment the counter back up and stop any
4496 * timer a passing stranger may have started :-S
4498 if (from_inpcbfree == SCTP_NORMAL_PROC) {
4499 atomic_add_int(&stcb->asoc.refcnt, 1);
4501 SCTP_TCB_UNLOCK(stcb);
4503 SCTP_ITERATOR_LOCK();
4504 SCTP_INP_INFO_WLOCK();
4505 SCTP_INP_WLOCK(inp);
4506 SCTP_TCB_LOCK(stcb);
4508 /* Double check the GONE flag */
4509 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4510 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
4511 /* nothing around */
4514 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4515 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
4517 * For TCP type we need special handling when we are
4518 * connected. We also include the peel'ed off ones to.
4520 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4521 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
4522 inp->sctp_flags |= SCTP_PCB_FLAGS_WAS_CONNECTED;
4525 if (so->so_rcv.sb_cc == 0) {
4526 so->so_state &= ~(SS_ISCONNECTING |
4527 SS_ISDISCONNECTING |
4533 sctp_sowwakeup(inp, so);
4534 sctp_sorwakeup(inp, so);
4540 * Make it invalid too, that way if its about to run it will abort
4543 sctp_iterator_asoc_being_freed(inp, stcb);
4544 /* re-increment the lock */
4545 if (from_inpcbfree == SCTP_NORMAL_PROC) {
4546 atomic_add_int(&stcb->asoc.refcnt, -1);
4549 if (inp->sctp_tcbhash) {
4550 LIST_REMOVE(stcb, sctp_tcbhash);
4552 if (stcb->asoc.in_restart_hash) {
4553 LIST_REMOVE(stcb, sctp_tcbrestarhash);
4555 /* Now lets remove it from the list of ALL associations in the EP */
4556 LIST_REMOVE(stcb, sctp_tcblist);
4557 if (from_inpcbfree == SCTP_NORMAL_PROC) {
4558 SCTP_INP_INCR_REF(inp);
4559 SCTP_INP_WUNLOCK(inp);
4560 SCTP_ITERATOR_UNLOCK();
4562 /* pull from vtag hash */
4563 LIST_REMOVE(stcb, sctp_asocs);
4564 sctp_add_vtag_to_timewait(asoc->my_vtag, SCTP_TIME_WAIT);
4567 * Now restop the timers to be sure - this is paranoia at is finest!
4569 (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
4570 (void)SCTP_OS_TIMER_STOP(&asoc->hb_timer.timer);
4571 (void)SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
4572 (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
4573 (void)SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
4574 (void)SCTP_OS_TIMER_STOP(&asoc->shut_guard_timer.timer);
4575 (void)SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
4576 (void)SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
4577 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4578 (void)SCTP_OS_TIMER_STOP(&net->fr_timer.timer);
4579 (void)SCTP_OS_TIMER_STOP(&net->rxt_timer.timer);
4580 (void)SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
4583 asoc->strreset_timer.type = SCTP_TIMER_TYPE_NONE;
4586 * The chunk lists and such SHOULD be empty but we check them just
4589 /* anything on the wheel needs to be removed */
4590 for (i = 0; i < asoc->streamoutcnt; i++) {
4591 struct sctp_stream_out *outs;
4593 outs = &asoc->strmout[i];
4594 /* now clean up any chunks here */
4595 sp = TAILQ_FIRST(&outs->outqueue);
4597 TAILQ_REMOVE(&outs->outqueue, sp, next);
4599 sctp_m_freem(sp->data);
4601 sp->tail_mbuf = NULL;
4603 sctp_free_remote_addr(sp->net);
4604 sctp_free_spbufspace(stcb, asoc, sp);
4605 /* Free the zone stuff */
4606 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_strmoq), sp);
4607 SCTP_DECR_STRMOQ_COUNT();
4608 /* sa_ignore FREED_MEMORY */
4609 sp = TAILQ_FIRST(&outs->outqueue);
4613 /* sa_ignore FREED_MEMORY */
4614 while ((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) {
4615 TAILQ_REMOVE(&asoc->resetHead, liste, next_resp);
4616 SCTP_FREE(liste, SCTP_M_STRESET);
4619 sq = TAILQ_FIRST(&asoc->pending_reply_queue);
4621 TAILQ_REMOVE(&asoc->pending_reply_queue, sq, next);
4623 sctp_m_freem(sq->data);
4626 sctp_free_remote_addr(sq->whoFrom);
4629 /* Free the ctl entry */
4630 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), sq);
4631 SCTP_DECR_READQ_COUNT();
4632 /* sa_ignore FREED_MEMORY */
4633 sq = TAILQ_FIRST(&asoc->pending_reply_queue);
4636 chk = TAILQ_FIRST(&asoc->free_chunks);
4638 TAILQ_REMOVE(&asoc->free_chunks, chk, sctp_next);
4640 sctp_m_freem(chk->data);
4644 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
4645 SCTP_DECR_CHK_COUNT();
4646 atomic_subtract_int(&SCTP_BASE_INFO(ipi_free_chunks), 1);
4647 asoc->free_chunk_cnt--;
4648 /* sa_ignore FREED_MEMORY */
4649 chk = TAILQ_FIRST(&asoc->free_chunks);
4651 /* pending send queue SHOULD be empty */
4652 if (!TAILQ_EMPTY(&asoc->send_queue)) {
4653 chk = TAILQ_FIRST(&asoc->send_queue);
4655 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
4657 sctp_m_freem(chk->data);
4661 sctp_free_remote_addr(chk->whoTo);
4662 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
4663 SCTP_DECR_CHK_COUNT();
4664 /* sa_ignore FREED_MEMORY */
4665 chk = TAILQ_FIRST(&asoc->send_queue);
4670 printf("Freed %d from send_queue\n", ccnt);
4674 /* sent queue SHOULD be empty */
4675 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
4676 chk = TAILQ_FIRST(&asoc->sent_queue);
4678 TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
4680 sctp_m_freem(chk->data);
4684 sctp_free_remote_addr(chk->whoTo);
4685 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
4686 SCTP_DECR_CHK_COUNT();
4687 /* sa_ignore FREED_MEMORY */
4688 chk = TAILQ_FIRST(&asoc->sent_queue);
4693 printf("Freed %d from sent_queue\n", ccnt);
4697 /* control queue MAY not be empty */
4698 if (!TAILQ_EMPTY(&asoc->control_send_queue)) {
4699 chk = TAILQ_FIRST(&asoc->control_send_queue);
4701 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
4703 sctp_m_freem(chk->data);
4707 sctp_free_remote_addr(chk->whoTo);
4708 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
4709 SCTP_DECR_CHK_COUNT();
4710 /* sa_ignore FREED_MEMORY */
4711 chk = TAILQ_FIRST(&asoc->control_send_queue);
4716 printf("Freed %d from ctrl_queue\n", ccnt);
4721 /* ASCONF queue MAY not be empty */
4722 if (!TAILQ_EMPTY(&asoc->asconf_send_queue)) {
4723 chk = TAILQ_FIRST(&asoc->asconf_send_queue);
4725 TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
4727 sctp_m_freem(chk->data);
4731 sctp_free_remote_addr(chk->whoTo);
4732 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
4733 SCTP_DECR_CHK_COUNT();
4734 /* sa_ignore FREED_MEMORY */
4735 chk = TAILQ_FIRST(&asoc->asconf_send_queue);
4740 printf("Freed %d from asconf_queue\n", ccnt);
4744 if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
4745 chk = TAILQ_FIRST(&asoc->reasmqueue);
4747 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
4749 sctp_m_freem(chk->data);
4752 sctp_free_remote_addr(chk->whoTo);
4754 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
4755 SCTP_DECR_CHK_COUNT();
4756 /* sa_ignore FREED_MEMORY */
4757 chk = TAILQ_FIRST(&asoc->reasmqueue);
4762 printf("Freed %d from reasm_queue\n", ccnt);
4766 if (asoc->mapping_array) {
4767 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
4768 asoc->mapping_array = NULL;
4770 /* the stream outs */
4771 if (asoc->strmout) {
4772 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
4773 asoc->strmout = NULL;
4775 asoc->streamoutcnt = 0;
4777 struct sctp_queued_to_read *ctl;
4779 for (i = 0; i < asoc->streamincnt; i++) {
4780 if (!TAILQ_EMPTY(&asoc->strmin[i].inqueue)) {
4781 /* We have somethings on the streamin queue */
4782 ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue);
4784 TAILQ_REMOVE(&asoc->strmin[i].inqueue,
4786 sctp_free_remote_addr(ctl->whoFrom);
4788 sctp_m_freem(ctl->data);
4792 * We don't free the address here
4793 * since all the net's were freed
4796 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), ctl);
4797 SCTP_DECR_READQ_COUNT();
4798 ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue);
4802 SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
4803 asoc->strmin = NULL;
4805 asoc->streamincnt = 0;
4806 while (!TAILQ_EMPTY(&asoc->nets)) {
4807 /* sa_ignore FREED_MEMORY */
4808 net = TAILQ_FIRST(&asoc->nets);
4809 /* pull from list */
4810 if ((SCTP_BASE_INFO(ipi_count_raddr) == 0) || (prev == net)) {
4812 panic("no net's left alloc'ed, or list points to itself");
4817 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
4818 sctp_free_remote_addr(net);
4821 while (!SCTP_LIST_EMPTY(&asoc->sctp_restricted_addrs)) {
4822 /* sa_ignore FREED_MEMORY */
4823 laddr = LIST_FIRST(&asoc->sctp_restricted_addrs);
4824 sctp_remove_laddr(laddr);
4827 /* pending asconf (address) parameters */
4828 while (!TAILQ_EMPTY(&asoc->asconf_queue)) {
4829 /* sa_ignore FREED_MEMORY */
4830 aparam = TAILQ_FIRST(&asoc->asconf_queue);
4831 TAILQ_REMOVE(&asoc->asconf_queue, aparam, next);
4832 SCTP_FREE(aparam, SCTP_M_ASC_ADDR);
4834 while (!TAILQ_EMPTY(&asoc->asconf_ack_sent)) {
4835 /* sa_ignore FREED_MEMORY */
4836 aack = TAILQ_FIRST(&asoc->asconf_ack_sent);
4837 TAILQ_REMOVE(&asoc->asconf_ack_sent, aack, next);
4838 if (aack->data != NULL) {
4839 sctp_m_freem(aack->data);
4841 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asconf_ack), aack);
4843 /* clean up auth stuff */
4844 if (asoc->local_hmacs)
4845 sctp_free_hmaclist(asoc->local_hmacs);
4846 if (asoc->peer_hmacs)
4847 sctp_free_hmaclist(asoc->peer_hmacs);
4849 if (asoc->local_auth_chunks)
4850 sctp_free_chunklist(asoc->local_auth_chunks);
4851 if (asoc->peer_auth_chunks)
4852 sctp_free_chunklist(asoc->peer_auth_chunks);
4854 sctp_free_authinfo(&asoc->authinfo);
4856 shared_key = LIST_FIRST(&asoc->shared_keys);
4857 while (shared_key) {
4858 LIST_REMOVE(shared_key, next);
4859 sctp_free_sharedkey(shared_key);
4860 /* sa_ignore FREED_MEMORY */
4861 shared_key = LIST_FIRST(&asoc->shared_keys);
4864 /* Insert new items here :> */
4866 /* Get rid of LOCK */
4867 SCTP_TCB_LOCK_DESTROY(stcb);
4868 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
4869 if (from_inpcbfree == SCTP_NORMAL_PROC) {
4870 SCTP_INP_INFO_WUNLOCK();
4871 SCTP_INP_RLOCK(inp);
4873 #ifdef SCTP_TRACK_FREED_ASOCS
4874 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4875 /* now clean up the tasoc itself */
4876 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
4877 SCTP_DECR_ASOC_COUNT();
4879 LIST_INSERT_HEAD(&inp->sctp_asoc_free_list, stcb, sctp_tcblist);
4882 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
4883 SCTP_DECR_ASOC_COUNT();
4885 if (from_inpcbfree == SCTP_NORMAL_PROC) {
4886 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4888 * If its NOT the inp_free calling us AND sctp_close
4889 * as been called, we call back...
4891 SCTP_INP_RUNLOCK(inp);
4893 * This will start the kill timer (if we are the
4894 * lastone) since we hold an increment yet. But this
4895 * is the only safe way to do this since otherwise
4896 * if the socket closes at the same time we are here
4897 * we might collide in the cleanup.
4899 sctp_inpcb_free(inp,
4900 SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
4901 SCTP_CALLED_DIRECTLY_NOCMPSET);
4902 SCTP_INP_DECR_REF(inp);
4905 /* The socket is still open. */
4906 SCTP_INP_DECR_REF(inp);
4909 if (from_inpcbfree == SCTP_NORMAL_PROC) {
4910 SCTP_INP_RUNLOCK(inp);
4913 /* destroyed the asoc */
4914 #ifdef SCTP_LOG_CLOSING
4915 sctp_log_closing(inp, NULL, 11);
4923 * determine if a destination is "reachable" based upon the addresses bound
4924 * to the current endpoint (e.g. only v4 or v6 currently bound)
4927 * FIX: if we allow assoc-level bindx(), then this needs to be fixed to use
4928 * assoc level v4/v6 flags, as the assoc *may* not have the same address
4929 * types bound as its endpoint
4932 sctp_destination_is_reachable(struct sctp_tcb *stcb, struct sockaddr *destaddr)
4934 struct sctp_inpcb *inp;
4938 * No locks here, the TCB, in all cases is already locked and an
4939 * assoc is up. There is either a INP lock by the caller applied (in
4940 * asconf case when deleting an address) or NOT in the HB case,
4941 * however if HB then the INP increment is up and the INP will not
4942 * be removed (on top of the fact that we have a TCB lock). So we
4943 * only want to read the sctp_flags, which is either bound-all or
4944 * not.. no protection needed since once an assoc is up you can't be
4945 * changing your binding.
4947 inp = stcb->sctp_ep;
4948 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4949 /* if bound all, destination is not restricted */
4951 * RRS: Question during lock work: Is this correct? If you
4952 * are bound-all you still might need to obey the V4--V6
4953 * flags??? IMO this bound-all stuff needs to be removed!
4957 /* NOTE: all "scope" checks are done when local addresses are added */
4958 if (destaddr->sa_family == AF_INET6) {
4959 answer = inp->ip_inp.inp.inp_vflag & INP_IPV6;
4960 } else if (destaddr->sa_family == AF_INET) {
4961 answer = inp->ip_inp.inp.inp_vflag & INP_IPV4;
4963 /* invalid family, so it's unreachable */
4970 * update the inp_vflags on an endpoint
4973 sctp_update_ep_vflag(struct sctp_inpcb *inp)
4975 struct sctp_laddr *laddr;
4977 /* first clear the flag */
4978 inp->ip_inp.inp.inp_vflag = 0;
4979 /* set the flag based on addresses on the ep list */
4980 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4981 if (laddr->ifa == NULL) {
4982 SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n",
4986 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
4989 if (laddr->ifa->address.sa.sa_family == AF_INET6) {
4990 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
4991 } else if (laddr->ifa->address.sa.sa_family == AF_INET) {
4992 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
4998 * Add the address to the endpoint local address list There is nothing to be
4999 * done if we are bound to all addresses
5002 sctp_add_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa, uint32_t action)
5004 struct sctp_laddr *laddr;
5009 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
5010 /* You are already bound to all. You have it already */
5013 if (ifa->address.sa.sa_family == AF_INET6) {
5014 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
5015 /* Can't bind a non-useable addr. */
5019 /* first, is it already present? */
5020 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
5021 if (laddr->ifa == ifa) {
5028 /* Not in the ep list */
5029 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, action);
5033 /* update inp_vflag flags */
5034 if (ifa->address.sa.sa_family == AF_INET6) {
5035 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
5036 } else if (ifa->address.sa.sa_family == AF_INET) {
5037 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
5045 * select a new (hopefully reachable) destination net (should only be used
5046 * when we deleted an ep addr that is the only usable source address to reach
5047 * the destination net)
5050 sctp_select_primary_destination(struct sctp_tcb *stcb)
5052 struct sctp_nets *net;
5054 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5055 /* for now, we'll just pick the first reachable one we find */
5056 if (net->dest_state & SCTP_ADDR_UNCONFIRMED)
5058 if (sctp_destination_is_reachable(stcb,
5059 (struct sockaddr *)&net->ro._l_addr)) {
5060 /* found a reachable destination */
5061 stcb->asoc.primary_destination = net;
5064 /* I can't there from here! ...we're gonna die shortly... */
5069 * Delete the address from the endpoint local address list There is nothing
5070 * to be done if we are bound to all addresses
5073 sctp_del_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
5075 struct sctp_laddr *laddr;
5079 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
5080 /* You are already bound to all. You have it already */
5083 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
5084 if (laddr->ifa == ifa) {
5089 if (fnd && (inp->laddr_count < 2)) {
5090 /* can't delete unless there are at LEAST 2 addresses */
5095 * clean up any use of this address go through our
5096 * associations and clear any last_used_address that match
5097 * this one for each assoc, see if a new primary_destination
5100 struct sctp_tcb *stcb;
5102 /* clean up "next_addr_touse" */
5103 if (inp->next_addr_touse == laddr)
5104 /* delete this address */
5105 inp->next_addr_touse = NULL;
5107 /* clean up "last_used_address" */
5108 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5109 struct sctp_nets *net;
5111 SCTP_TCB_LOCK(stcb);
5112 if (stcb->asoc.last_used_address == laddr)
5113 /* delete this address */
5114 stcb->asoc.last_used_address = NULL;
5116 * Now spin through all the nets and purge any ref
5119 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5120 if (net->ro._s_addr &&
5121 (net->ro._s_addr->ifa == laddr->ifa)) {
5122 /* Yep, purge src address selected */
5125 /* delete this address if cached */
5129 net->ro.ro_rt = NULL;
5131 sctp_free_ifa(net->ro._s_addr);
5132 net->ro._s_addr = NULL;
5133 net->src_addr_selected = 0;
5136 SCTP_TCB_UNLOCK(stcb);
5137 } /* for each tcb */
5138 /* remove it from the ep list */
5139 sctp_remove_laddr(laddr);
5141 /* update inp_vflag flags */
5142 sctp_update_ep_vflag(inp);
5148 * Add the address to the TCB local address restricted list.
5149 * This is a "pending" address list (eg. addresses waiting for an
5150 * ASCONF-ACK response) and cannot be used as a valid source address.
5153 sctp_add_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
5155 struct sctp_inpcb *inp;
5156 struct sctp_laddr *laddr;
5157 struct sctpladdr *list;
5160 * Assumes TCB is locked.. and possibly the INP. May need to
5161 * confirm/fix that if we need it and is not the case.
5163 list = &stcb->asoc.sctp_restricted_addrs;
5165 inp = stcb->sctp_ep;
5166 if (ifa->address.sa.sa_family == AF_INET6) {
5167 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
5168 /* Can't bind a non-existent addr. */
5172 /* does the address already exist? */
5173 LIST_FOREACH(laddr, list, sctp_nxt_addr) {
5174 if (laddr->ifa == ifa) {
5179 /* add to the list */
5180 (void)sctp_insert_laddr(list, ifa, 0);
5185 * insert an laddr entry with the given ifa for the desired list
5188 sctp_insert_laddr(struct sctpladdr *list, struct sctp_ifa *ifa, uint32_t act)
5190 struct sctp_laddr *laddr;
5192 laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
5193 if (laddr == NULL) {
5194 /* out of memory? */
5195 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
5198 SCTP_INCR_LADDR_COUNT();
5199 bzero(laddr, sizeof(*laddr));
5200 (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time);
5202 laddr->action = act;
5203 atomic_add_int(&ifa->refcount, 1);
5205 LIST_INSERT_HEAD(list, laddr, sctp_nxt_addr);
5211 * Remove an laddr entry from the local address list (on an assoc)
5214 sctp_remove_laddr(struct sctp_laddr *laddr)
5217 /* remove from the list */
5218 LIST_REMOVE(laddr, sctp_nxt_addr);
5219 sctp_free_ifa(laddr->ifa);
5220 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), laddr);
5221 SCTP_DECR_LADDR_COUNT();
5225 * Remove a local address from the TCB local address restricted list
5228 sctp_del_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
5230 struct sctp_inpcb *inp;
5231 struct sctp_laddr *laddr;
5234 * This is called by asconf work. It is assumed that a) The TCB is
5235 * locked and b) The INP is locked. This is true in as much as I can
5236 * trace through the entry asconf code where I did these locks.
5237 * Again, the ASCONF code is a bit different in that it does lock
5238 * the INP during its work often times. This must be since we don't
5239 * want other proc's looking up things while what they are looking
5240 * up is changing :-D
5243 inp = stcb->sctp_ep;
5244 /* if subset bound and don't allow ASCONF's, can't delete last */
5245 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) &&
5246 sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF)) {
5247 if (stcb->sctp_ep->laddr_count < 2) {
5248 /* can't delete last address */
5252 LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
5253 /* remove the address if it exists */
5254 if (laddr->ifa == NULL)
5256 if (laddr->ifa == ifa) {
5257 sctp_remove_laddr(laddr);
5262 /* address not found! */
5267 * Temporarily remove for __APPLE__ until we use the Tiger equivalents
5270 static int sctp_max_number_of_assoc = SCTP_MAX_NUM_OF_ASOC;
5271 static int sctp_scale_up_for_address = SCTP_SCALE_FOR_ADDR;
5277 * SCTP initialization for the PCB structures should be called by
5278 * the sctp_init() funciton.
5283 if (SCTP_BASE_VAR(sctp_pcb_initialized) != 0) {
5284 /* error I was called twice */
5287 SCTP_BASE_VAR(sctp_pcb_initialized) = 1;
5289 #if defined(SCTP_LOCAL_TRACE_BUF)
5290 bzero(&SCTP_BASE_SYSCTL(sctp_log), sizeof(struct sctp_log));
5292 (void)SCTP_GETTIME_TIMEVAL(&tv);
5293 SCTP_BASE_STAT(sctps_discontinuitytime).tv_sec = (uint32_t) tv.tv_sec;
5294 SCTP_BASE_STAT(sctps_discontinuitytime).tv_usec = (uint32_t) tv.tv_usec;
5295 /* init the empty list of (All) Endpoints */
5296 LIST_INIT(&SCTP_BASE_INFO(listhead));
5298 /* init the iterator head */
5299 TAILQ_INIT(&SCTP_BASE_INFO(iteratorhead));
5301 /* init the hash table of endpoints */
5302 TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", &SCTP_BASE_SYSCTL(sctp_hashtblsize));
5303 TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", &SCTP_BASE_SYSCTL(sctp_pcbtblsize));
5304 TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", &SCTP_BASE_SYSCTL(sctp_chunkscale));
5305 SCTP_BASE_INFO(sctp_asochash) = SCTP_HASH_INIT((SCTP_BASE_SYSCTL(sctp_hashtblsize) * 31),
5306 &SCTP_BASE_INFO(hashasocmark));
5307 SCTP_BASE_INFO(sctp_ephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize),
5308 &SCTP_BASE_INFO(hashmark));
5309 SCTP_BASE_INFO(sctp_tcpephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize),
5310 &SCTP_BASE_INFO(hashtcpmark));
5311 SCTP_BASE_INFO(hashtblsize) = SCTP_BASE_SYSCTL(sctp_hashtblsize);
5313 /* init the small hash table we use to track restarted asoc's */
5314 SCTP_BASE_INFO(sctp_restarthash) = SCTP_HASH_INIT(SCTP_STACK_VTAG_HASH_SIZE,
5315 &SCTP_BASE_INFO(hashrestartmark));
5318 SCTP_BASE_INFO(sctp_vrfhash) = SCTP_HASH_INIT(SCTP_SIZE_OF_VRF_HASH,
5319 &SCTP_BASE_INFO(hashvrfmark));
5321 SCTP_BASE_INFO(vrf_ifn_hash) = SCTP_HASH_INIT(SCTP_VRF_IFN_HASH_SIZE,
5322 &SCTP_BASE_INFO(vrf_ifn_hashmark));
5324 /* init the zones */
5326 * FIX ME: Should check for NULL returns, but if it does fail we are
5327 * doomed to panic anyways... add later maybe.
5329 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_ep), "sctp_ep",
5330 sizeof(struct sctp_inpcb), maxsockets);
5332 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asoc), "sctp_asoc",
5333 sizeof(struct sctp_tcb), sctp_max_number_of_assoc);
5335 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_laddr), "sctp_laddr",
5336 sizeof(struct sctp_laddr),
5337 (sctp_max_number_of_assoc * sctp_scale_up_for_address));
5339 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_net), "sctp_raddr",
5340 sizeof(struct sctp_nets),
5341 (sctp_max_number_of_assoc * sctp_scale_up_for_address));
5343 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_chunk), "sctp_chunk",
5344 sizeof(struct sctp_tmit_chunk),
5345 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
5347 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_readq), "sctp_readq",
5348 sizeof(struct sctp_queued_to_read),
5349 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
5351 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_strmoq), "sctp_stream_msg_out",
5352 sizeof(struct sctp_stream_queue_pending),
5353 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
5355 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf), "sctp_asconf",
5356 sizeof(struct sctp_asconf),
5357 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
5359 SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf_ack), "sctp_asconf_ack",
5360 sizeof(struct sctp_asconf_ack),
5361 (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
5364 /* Master Lock INIT for info structure */
5365 SCTP_INP_INFO_LOCK_INIT();
5366 SCTP_STATLOG_INIT_LOCK();
5367 SCTP_ITERATOR_LOCK_INIT();
5369 SCTP_IPI_COUNT_INIT();
5370 SCTP_IPI_ADDR_INIT();
5371 SCTP_IPI_ITERATOR_WQ_INIT();
5372 #ifdef SCTP_PACKET_LOGGING
5373 SCTP_IP_PKTLOG_INIT();
5375 LIST_INIT(&SCTP_BASE_INFO(addr_wq));
5377 /* not sure if we need all the counts */
5378 SCTP_BASE_INFO(ipi_count_ep) = 0;
5379 /* assoc/tcb zone info */
5380 SCTP_BASE_INFO(ipi_count_asoc) = 0;
5381 /* local addrlist zone info */
5382 SCTP_BASE_INFO(ipi_count_laddr) = 0;
5383 /* remote addrlist zone info */
5384 SCTP_BASE_INFO(ipi_count_raddr) = 0;
5386 SCTP_BASE_INFO(ipi_count_chunk) = 0;
5388 /* socket queue zone info */
5389 SCTP_BASE_INFO(ipi_count_readq) = 0;
5391 /* stream out queue cont */
5392 SCTP_BASE_INFO(ipi_count_strmoq) = 0;
5394 SCTP_BASE_INFO(ipi_free_strmoq) = 0;
5395 SCTP_BASE_INFO(ipi_free_chunks) = 0;
5397 SCTP_OS_TIMER_INIT(&SCTP_BASE_INFO(addr_wq_timer.timer));
5399 /* Init the TIMEWAIT list */
5400 for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE_A; i++) {
5401 LIST_INIT(&SCTP_BASE_INFO(vtag_timewait[i]));
5404 #if defined(SCTP_USE_THREAD_BASED_ITERATOR)
5405 SCTP_BASE_INFO(iterator_running) = 0;
5406 SCTP_BASE_INFO(threads_must_exit) = 0;
5407 sctp_startup_iterator();
5411 * INIT the default VRF which for BSD is the only one, other O/S's
5412 * may have more. But initially they must start with one and then
5413 * add the VRF's as addresses are added.
5415 sctp_init_vrf_list(SCTP_DEFAULT_VRF);
5420 * Assumes that the SCTP_BASE_INFO() lock is NOT held.
5423 sctp_pcb_finish(void)
5425 struct sctp_vrflist *vrf_bucket;
5426 struct sctp_vrf *vrf;
5427 struct sctp_ifn *ifn;
5428 struct sctp_ifa *ifa;
5429 struct sctpvtaghead *chain;
5430 struct sctp_tagblock *twait_block, *prev_twait_block;
5434 * free the vrf/ifn/ifa lists and hashes (be sure address monitor is
5437 vrf_bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(SCTP_DEFAULT_VRFID & SCTP_BASE_INFO(hashvrfmark))];
5438 vrf = LIST_FIRST(vrf_bucket);
5440 ifn = LIST_FIRST(&vrf->ifnlist);
5442 ifa = LIST_FIRST(&ifn->ifalist);
5445 LIST_REMOVE(ifa, next_bucket);
5446 LIST_REMOVE(ifa, next_ifa);
5447 SCTP_FREE(ifa, SCTP_M_IFA);
5448 ifa = LIST_FIRST(&ifn->ifalist);
5451 LIST_REMOVE(ifn, next_bucket);
5452 LIST_REMOVE(ifn, next_ifn);
5453 SCTP_FREE(ifn, SCTP_M_IFN);
5454 ifn = LIST_FIRST(&vrf->ifnlist);
5456 SCTP_HASH_FREE(vrf->vrf_addr_hash, vrf->vrf_addr_hashmark);
5458 LIST_REMOVE(vrf, next_vrf);
5459 SCTP_FREE(vrf, SCTP_M_VRF);
5460 vrf = LIST_FIRST(vrf_bucket);
5462 /* free the vrf hashes */
5463 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_vrfhash), SCTP_BASE_INFO(hashvrfmark));
5464 SCTP_HASH_FREE(SCTP_BASE_INFO(vrf_ifn_hash), SCTP_BASE_INFO(vrf_ifn_hashmark));
5467 * free the TIMEWAIT list elements malloc'd in the function
5468 * sctp_add_vtag_to_timewait()...
5470 for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE_A; i++) {
5471 chain = &SCTP_BASE_INFO(vtag_timewait)[i];
5472 if (!SCTP_LIST_EMPTY(chain)) {
5473 prev_twait_block = NULL;
5474 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
5475 if (prev_twait_block) {
5476 SCTP_FREE(prev_twait_block, SCTP_M_TIMW);
5478 prev_twait_block = twait_block;
5480 SCTP_FREE(prev_twait_block, SCTP_M_TIMW);
5484 /* free the locks and mutexes */
5485 #ifdef SCTP_PACKET_LOGGING
5486 SCTP_IP_PKTLOG_DESTROY();
5489 SCTP_IPI_ITERATOR_WQ_DESTROY();
5490 SCTP_IPI_ADDR_DESTROY();
5491 SCTP_ITERATOR_LOCK_DESTROY();
5492 SCTP_STATLOG_DESTROY();
5493 SCTP_INP_INFO_LOCK_DESTROY();
5495 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_ep));
5496 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asoc));
5497 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_laddr));
5498 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_net));
5499 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_chunk));
5500 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_readq));
5501 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_strmoq));
5502 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf));
5503 SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf_ack));
5504 /* Get rid of other stuff to */
5505 if (SCTP_BASE_INFO(sctp_asochash) != NULL)
5506 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_asochash), SCTP_BASE_INFO(hashasocmark));
5507 if (SCTP_BASE_INFO(sctp_ephash) != NULL)
5508 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_ephash), SCTP_BASE_INFO(hashmark));
5509 if (SCTP_BASE_INFO(sctp_tcpephash) != NULL)
5510 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_tcpephash), SCTP_BASE_INFO(hashtcpmark));
5511 if (SCTP_BASE_INFO(sctp_restarthash) != NULL)
5512 SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_restarthash), SCTP_BASE_INFO(hashrestartmark));
5518 sctp_load_addresses_from_init(struct sctp_tcb *stcb, struct mbuf *m,
5519 int iphlen, int offset, int limit, struct sctphdr *sh,
5520 struct sockaddr *altsa)
5523 * grub through the INIT pulling addresses and loading them to the
5524 * nets structure in the asoc. The from address in the mbuf should
5525 * also be loaded (if it is not already). This routine can be called
5526 * with either INIT or INIT-ACK's as long as the m points to the IP
5527 * packet and the offset points to the beginning of the parameters.
5529 struct sctp_inpcb *inp, *l_inp;
5530 struct sctp_nets *net, *net_tmp;
5532 struct sctp_paramhdr *phdr, parm_buf;
5533 struct sctp_tcb *stcb_tmp;
5534 uint16_t ptype, plen;
5535 struct sockaddr *sa;
5536 struct sockaddr_storage dest_store;
5537 struct sockaddr *local_sa = (struct sockaddr *)&dest_store;
5538 struct sockaddr_in sin;
5539 struct sockaddr_in6 sin6;
5540 uint8_t random_store[SCTP_PARAM_BUFFER_SIZE];
5541 struct sctp_auth_random *p_random = NULL;
5542 uint16_t random_len = 0;
5543 uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE];
5544 struct sctp_auth_hmac_algo *hmacs = NULL;
5545 uint16_t hmacs_len = 0;
5546 uint8_t saw_asconf = 0;
5547 uint8_t saw_asconf_ack = 0;
5548 uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE];
5549 struct sctp_auth_chunk_list *chunks = NULL;
5550 uint16_t num_chunks = 0;
5551 sctp_key_t *new_key;
5553 int got_random = 0, got_hmacs = 0, got_chklist = 0;
5555 /* First get the destination address setup too. */
5556 memset(&sin, 0, sizeof(sin));
5557 memset(&sin6, 0, sizeof(sin6));
5559 sin.sin_family = AF_INET;
5560 sin.sin_len = sizeof(sin);
5561 sin.sin_port = stcb->rport;
5563 sin6.sin6_family = AF_INET6;
5564 sin6.sin6_len = sizeof(struct sockaddr_in6);
5565 sin6.sin6_port = stcb->rport;
5566 if (altsa == NULL) {
5567 iph = mtod(m, struct ip *);
5568 switch (iph->ip_v) {
5572 struct sockaddr_in *sin_2;
5574 sin_2 = (struct sockaddr_in *)(local_sa);
5575 memset(sin_2, 0, sizeof(sin));
5576 sin_2->sin_family = AF_INET;
5577 sin_2->sin_len = sizeof(sin);
5578 sin_2->sin_port = sh->dest_port;
5579 sin_2->sin_addr.s_addr = iph->ip_dst.s_addr;
5580 sin.sin_addr = iph->ip_src;
5581 sa = (struct sockaddr *)&sin;
5585 case IPV6_VERSION >> 4:
5588 struct ip6_hdr *ip6;
5589 struct sockaddr_in6 *sin6_2;
5591 ip6 = mtod(m, struct ip6_hdr *);
5592 sin6_2 = (struct sockaddr_in6 *)(local_sa);
5593 memset(sin6_2, 0, sizeof(sin6));
5594 sin6_2->sin6_family = AF_INET6;
5595 sin6_2->sin6_len = sizeof(struct sockaddr_in6);
5596 sin6_2->sin6_port = sh->dest_port;
5597 sin6.sin6_addr = ip6->ip6_src;
5598 sa = (struct sockaddr *)&sin6;
5608 * For cookies we use the src address NOT from the packet
5609 * but from the original INIT
5613 /* Turn off ECN until we get through all params */
5614 stcb->asoc.ecn_allowed = 0;
5615 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5616 /* mark all addresses that we have currently on the list */
5617 net->dest_state |= SCTP_ADDR_NOT_IN_ASSOC;
5619 /* does the source address already exist? if so skip it */
5620 l_inp = inp = stcb->sctp_ep;
5622 atomic_add_int(&stcb->asoc.refcnt, 1);
5623 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net_tmp, local_sa, stcb);
5624 atomic_add_int(&stcb->asoc.refcnt, -1);
5626 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || inp == NULL) {
5627 /* we must add the source address */
5628 /* no scope set here since we have a tcb already. */
5629 if ((sa->sa_family == AF_INET) &&
5630 (stcb->asoc.ipv4_addr_legal)) {
5631 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_2)) {
5634 } else if ((sa->sa_family == AF_INET6) &&
5635 (stcb->asoc.ipv6_addr_legal)) {
5636 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) {
5641 if (net_tmp != NULL && stcb_tmp == stcb) {
5642 net_tmp->dest_state &= ~SCTP_ADDR_NOT_IN_ASSOC;
5643 } else if (stcb_tmp != stcb) {
5644 /* It belongs to another association? */
5646 SCTP_TCB_UNLOCK(stcb_tmp);
5650 if (stcb->asoc.state == 0) {
5651 /* the assoc was freed? */
5655 * peer must explicitly turn this on. This may have been initialized
5656 * to be "on" in order to allow local addr changes while INIT's are
5659 stcb->asoc.peer_supports_asconf = 0;
5660 /* now we must go through each of the params. */
5661 phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
5663 ptype = ntohs(phdr->param_type);
5664 plen = ntohs(phdr->param_length);
5666 * printf("ptype => %0x, plen => %d\n", (uint32_t)ptype,
5669 if (offset + plen > limit) {
5675 if (ptype == SCTP_IPV4_ADDRESS) {
5676 if (stcb->asoc.ipv4_addr_legal) {
5677 struct sctp_ipv4addr_param *p4, p4_buf;
5679 /* ok get the v4 address and check/add */
5680 phdr = sctp_get_next_param(m, offset,
5681 (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
5682 if (plen != sizeof(struct sctp_ipv4addr_param) ||
5686 p4 = (struct sctp_ipv4addr_param *)phdr;
5687 sin.sin_addr.s_addr = p4->addr;
5688 if (IN_MULTICAST(sin.sin_addr.s_addr)) {
5689 /* Skip multi-cast addresses */
5692 if ((sin.sin_addr.s_addr == INADDR_BROADCAST) ||
5693 (sin.sin_addr.s_addr == INADDR_ANY)) {
5696 sa = (struct sockaddr *)&sin;
5697 inp = stcb->sctp_ep;
5698 atomic_add_int(&stcb->asoc.refcnt, 1);
5699 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
5701 atomic_add_int(&stcb->asoc.refcnt, -1);
5703 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) ||
5705 /* we must add the source address */
5707 * no scope set since we have a tcb
5712 * we must validate the state again
5716 if (stcb->asoc.state == 0) {
5717 /* the assoc was freed? */
5720 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_4)) {
5723 } else if (stcb_tmp == stcb) {
5724 if (stcb->asoc.state == 0) {
5725 /* the assoc was freed? */
5731 ~SCTP_ADDR_NOT_IN_ASSOC;
5735 * strange, address is in another
5736 * assoc? straighten out locks.
5739 if (SCTP_GET_STATE(&stcb_tmp->asoc) & SCTP_STATE_COOKIE_WAIT) {
5744 sctp_abort_an_association(stcb_tmp->sctp_ep,
5745 stcb_tmp, 1, NULL, 0);
5748 SCTP_TCB_UNLOCK(stcb_tmp);
5750 if (stcb->asoc.state == 0) {
5751 /* the assoc was freed? */
5757 } else if (ptype == SCTP_IPV6_ADDRESS) {
5758 if (stcb->asoc.ipv6_addr_legal) {
5759 /* ok get the v6 address and check/add */
5760 struct sctp_ipv6addr_param *p6, p6_buf;
5762 phdr = sctp_get_next_param(m, offset,
5763 (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
5764 if (plen != sizeof(struct sctp_ipv6addr_param) ||
5768 p6 = (struct sctp_ipv6addr_param *)phdr;
5769 memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
5771 if (IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) {
5772 /* Skip multi-cast addresses */
5775 if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
5777 * Link local make no sense without
5782 sa = (struct sockaddr *)&sin6;
5783 inp = stcb->sctp_ep;
5784 atomic_add_int(&stcb->asoc.refcnt, 1);
5785 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
5787 atomic_add_int(&stcb->asoc.refcnt, -1);
5788 if (stcb_tmp == NULL && (inp == stcb->sctp_ep ||
5791 * we must validate the state again
5795 if (stcb->asoc.state == 0) {
5796 /* the assoc was freed? */
5800 * we must add the address, no scope
5803 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_5)) {
5806 } else if (stcb_tmp == stcb) {
5808 * we must validate the state again
5811 if (stcb->asoc.state == 0) {
5812 /* the assoc was freed? */
5818 ~SCTP_ADDR_NOT_IN_ASSOC;
5822 * strange, address is in another
5823 * assoc? straighten out locks.
5826 if (SCTP_GET_STATE(&stcb_tmp->asoc) & SCTP_STATE_COOKIE_WAIT) {
5831 sctp_abort_an_association(stcb_tmp->sctp_ep,
5832 stcb_tmp, 1, NULL, 0);
5835 SCTP_TCB_UNLOCK(stcb_tmp);
5837 if (stcb->asoc.state == 0) {
5838 /* the assoc was freed? */
5844 } else if (ptype == SCTP_ECN_CAPABLE) {
5845 stcb->asoc.ecn_allowed = 1;
5846 } else if (ptype == SCTP_ULP_ADAPTATION) {
5847 if (stcb->asoc.state != SCTP_STATE_OPEN) {
5848 struct sctp_adaptation_layer_indication ai,
5851 phdr = sctp_get_next_param(m, offset,
5852 (struct sctp_paramhdr *)&ai, sizeof(ai));
5853 aip = (struct sctp_adaptation_layer_indication *)phdr;
5855 stcb->asoc.peers_adaptation = ntohl(aip->indication);
5856 stcb->asoc.adaptation_needed = 1;
5859 } else if (ptype == SCTP_SET_PRIM_ADDR) {
5860 struct sctp_asconf_addr_param lstore, *fee;
5861 struct sctp_asconf_addrv4_param *fii;
5863 struct sockaddr *lsa = NULL;
5865 stcb->asoc.peer_supports_asconf = 1;
5866 if (plen > sizeof(lstore)) {
5869 phdr = sctp_get_next_param(m, offset,
5870 (struct sctp_paramhdr *)&lstore, min(plen, sizeof(lstore)));
5874 fee = (struct sctp_asconf_addr_param *)phdr;
5875 lptype = ntohs(fee->addrp.ph.param_type);
5876 if (lptype == SCTP_IPV4_ADDRESS) {
5878 sizeof(struct sctp_asconf_addrv4_param)) {
5879 SCTP_PRINTF("Sizeof setprim in init/init ack not %d but %d - ignored\n",
5880 (int)sizeof(struct sctp_asconf_addrv4_param),
5883 fii = (struct sctp_asconf_addrv4_param *)fee;
5884 sin.sin_addr.s_addr = fii->addrp.addr;
5885 lsa = (struct sockaddr *)&sin;
5887 } else if (lptype == SCTP_IPV6_ADDRESS) {
5889 sizeof(struct sctp_asconf_addr_param)) {
5890 SCTP_PRINTF("Sizeof setprim (v6) in init/init ack not %d but %d - ignored\n",
5891 (int)sizeof(struct sctp_asconf_addr_param),
5894 memcpy(sin6.sin6_addr.s6_addr,
5896 sizeof(fee->addrp.addr));
5897 lsa = (struct sockaddr *)&sin6;
5901 (void)sctp_set_primary_addr(stcb, sa, NULL);
5903 } else if (ptype == SCTP_PRSCTP_SUPPORTED) {
5904 /* Peer supports pr-sctp */
5905 stcb->asoc.peer_supports_prsctp = 1;
5906 } else if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
5907 /* A supported extension chunk */
5908 struct sctp_supported_chunk_types_param *pr_supported;
5909 uint8_t local_store[SCTP_PARAM_BUFFER_SIZE];
5912 phdr = sctp_get_next_param(m, offset,
5913 (struct sctp_paramhdr *)&local_store, min(sizeof(local_store), plen));
5917 stcb->asoc.peer_supports_asconf = 0;
5918 stcb->asoc.peer_supports_prsctp = 0;
5919 stcb->asoc.peer_supports_pktdrop = 0;
5920 stcb->asoc.peer_supports_strreset = 0;
5921 stcb->asoc.peer_supports_auth = 0;
5922 pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
5923 num_ent = plen - sizeof(struct sctp_paramhdr);
5924 for (i = 0; i < num_ent; i++) {
5925 switch (pr_supported->chunk_types[i]) {
5927 case SCTP_ASCONF_ACK:
5928 stcb->asoc.peer_supports_asconf = 1;
5930 case SCTP_FORWARD_CUM_TSN:
5931 stcb->asoc.peer_supports_prsctp = 1;
5933 case SCTP_PACKET_DROPPED:
5934 stcb->asoc.peer_supports_pktdrop = 1;
5936 case SCTP_STREAM_RESET:
5937 stcb->asoc.peer_supports_strreset = 1;
5939 case SCTP_AUTHENTICATION:
5940 stcb->asoc.peer_supports_auth = 1;
5943 /* one I have not learned yet */
5948 } else if (ptype == SCTP_ECN_NONCE_SUPPORTED) {
5949 /* Peer supports ECN-nonce */
5950 stcb->asoc.peer_supports_ecn_nonce = 1;
5951 stcb->asoc.ecn_nonce_allowed = 1;
5952 } else if (ptype == SCTP_RANDOM) {
5953 if (plen > sizeof(random_store))
5956 /* already processed a RANDOM */
5959 phdr = sctp_get_next_param(m, offset,
5960 (struct sctp_paramhdr *)random_store,
5961 min(sizeof(random_store), plen));
5964 p_random = (struct sctp_auth_random *)phdr;
5965 random_len = plen - sizeof(*p_random);
5966 /* enforce the random length */
5967 if (random_len != SCTP_AUTH_RANDOM_SIZE_REQUIRED) {
5968 SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: invalid RANDOM len\n");
5972 } else if (ptype == SCTP_HMAC_LIST) {
5976 if (plen > sizeof(hmacs_store))
5979 /* already processed a HMAC list */
5982 phdr = sctp_get_next_param(m, offset,
5983 (struct sctp_paramhdr *)hmacs_store,
5984 min(plen, sizeof(hmacs_store)));
5987 hmacs = (struct sctp_auth_hmac_algo *)phdr;
5988 hmacs_len = plen - sizeof(*hmacs);
5989 num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]);
5990 /* validate the hmac list */
5991 if (sctp_verify_hmac_param(hmacs, num_hmacs)) {
5994 if (stcb->asoc.peer_hmacs != NULL)
5995 sctp_free_hmaclist(stcb->asoc.peer_hmacs);
5996 stcb->asoc.peer_hmacs = sctp_alloc_hmaclist(num_hmacs);
5997 if (stcb->asoc.peer_hmacs != NULL) {
5998 for (i = 0; i < num_hmacs; i++) {
5999 (void)sctp_auth_add_hmacid(stcb->asoc.peer_hmacs,
6000 ntohs(hmacs->hmac_ids[i]));
6004 } else if (ptype == SCTP_CHUNK_LIST) {
6007 if (plen > sizeof(chunks_store))
6010 /* already processed a Chunks list */
6013 phdr = sctp_get_next_param(m, offset,
6014 (struct sctp_paramhdr *)chunks_store,
6015 min(plen, sizeof(chunks_store)));
6018 chunks = (struct sctp_auth_chunk_list *)phdr;
6019 num_chunks = plen - sizeof(*chunks);
6020 if (stcb->asoc.peer_auth_chunks != NULL)
6021 sctp_clear_chunklist(stcb->asoc.peer_auth_chunks);
6023 stcb->asoc.peer_auth_chunks = sctp_alloc_chunklist();
6024 for (i = 0; i < num_chunks; i++) {
6025 (void)sctp_auth_add_chunk(chunks->chunk_types[i],
6026 stcb->asoc.peer_auth_chunks);
6027 /* record asconf/asconf-ack if listed */
6028 if (chunks->chunk_types[i] == SCTP_ASCONF)
6030 if (chunks->chunk_types[i] == SCTP_ASCONF_ACK)
6035 } else if ((ptype == SCTP_HEARTBEAT_INFO) ||
6036 (ptype == SCTP_STATE_COOKIE) ||
6037 (ptype == SCTP_UNRECOG_PARAM) ||
6038 (ptype == SCTP_COOKIE_PRESERVE) ||
6039 (ptype == SCTP_SUPPORTED_ADDRTYPE) ||
6040 (ptype == SCTP_ADD_IP_ADDRESS) ||
6041 (ptype == SCTP_DEL_IP_ADDRESS) ||
6042 (ptype == SCTP_ERROR_CAUSE_IND) ||
6043 (ptype == SCTP_SUCCESS_REPORT)) {
6046 if ((ptype & 0x8000) == 0x0000) {
6048 * must stop processing the rest of the
6049 * param's. Any report bits were handled
6051 * sctp_arethere_unrecognized_parameters()
6052 * when the INIT or INIT-ACK was first seen.
6058 offset += SCTP_SIZE32(plen);
6059 if (offset >= limit) {
6062 phdr = sctp_get_next_param(m, offset, &parm_buf,
6065 /* Now check to see if we need to purge any addresses */
6066 for (net = TAILQ_FIRST(&stcb->asoc.nets); net != NULL; net = net_tmp) {
6067 net_tmp = TAILQ_NEXT(net, sctp_next);
6068 if ((net->dest_state & SCTP_ADDR_NOT_IN_ASSOC) ==
6069 SCTP_ADDR_NOT_IN_ASSOC) {
6070 /* This address has been removed from the asoc */
6071 /* remove and free it */
6072 stcb->asoc.numnets--;
6073 TAILQ_REMOVE(&stcb->asoc.nets, net, sctp_next);
6074 sctp_free_remote_addr(net);
6075 if (net == stcb->asoc.primary_destination) {
6076 stcb->asoc.primary_destination = NULL;
6077 sctp_select_primary_destination(stcb);
6081 /* validate authentication required parameters */
6082 if (got_random && got_hmacs) {
6083 stcb->asoc.peer_supports_auth = 1;
6085 stcb->asoc.peer_supports_auth = 0;
6087 if (!stcb->asoc.peer_supports_auth && got_chklist) {
6088 /* peer does not support auth but sent a chunks list? */
6091 if (!SCTP_BASE_SYSCTL(sctp_asconf_auth_nochk) && stcb->asoc.peer_supports_asconf &&
6092 !stcb->asoc.peer_supports_auth) {
6093 /* peer supports asconf but not auth? */
6095 } else if ((stcb->asoc.peer_supports_asconf) && (stcb->asoc.peer_supports_auth) &&
6096 ((saw_asconf == 0) || (saw_asconf_ack == 0))) {
6099 /* concatenate the full random key */
6100 #ifdef SCTP_AUTH_DRAFT_04
6101 keylen = random_len;
6102 new_key = sctp_alloc_key(keylen);
6103 if (new_key != NULL) {
6104 /* copy in the RANDOM */
6105 if (p_random != NULL)
6106 bcopy(p_random->random_data, new_key->key, random_len);
6109 keylen = sizeof(*p_random) + random_len + sizeof(*chunks) + num_chunks +
6110 sizeof(*hmacs) + hmacs_len;
6111 new_key = sctp_alloc_key(keylen);
6112 if (new_key != NULL) {
6113 /* copy in the RANDOM */
6114 if (p_random != NULL) {
6115 keylen = sizeof(*p_random) + random_len;
6116 bcopy(p_random, new_key->key, keylen);
6118 /* append in the AUTH chunks */
6119 if (chunks != NULL) {
6120 bcopy(chunks, new_key->key + keylen,
6121 sizeof(*chunks) + num_chunks);
6122 keylen += sizeof(*chunks) + num_chunks;
6124 /* append in the HMACs */
6125 if (hmacs != NULL) {
6126 bcopy(hmacs, new_key->key + keylen,
6127 sizeof(*hmacs) + hmacs_len);
6132 /* failed to get memory for the key */
6135 if (stcb->asoc.authinfo.peer_random != NULL)
6136 sctp_free_key(stcb->asoc.authinfo.peer_random);
6137 stcb->asoc.authinfo.peer_random = new_key;
6138 #ifdef SCTP_AUTH_DRAFT_04
6139 /* don't include the chunks and hmacs for draft -04 */
6140 stcb->asoc.authinfo.peer_random->keylen = random_len;
6142 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid);
6143 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid);
6149 sctp_set_primary_addr(struct sctp_tcb *stcb, struct sockaddr *sa,
6150 struct sctp_nets *net)
6152 /* make sure the requested primary address exists in the assoc */
6153 if (net == NULL && sa)
6154 net = sctp_findnet(stcb, sa);
6157 /* didn't find the requested primary address! */
6160 /* set the primary address */
6161 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
6162 /* Must be confirmed, so queue to set */
6163 net->dest_state |= SCTP_ADDR_REQ_PRIMARY;
6166 stcb->asoc.primary_destination = net;
6167 net->dest_state &= ~SCTP_ADDR_WAS_PRIMARY;
6168 net = TAILQ_FIRST(&stcb->asoc.nets);
6169 if (net != stcb->asoc.primary_destination) {
6171 * first one on the list is NOT the primary
6172 * sctp_cmpaddr() is much more efficent if the
6173 * primary is the first on the list, make it so.
6175 TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
6176 TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
6183 sctp_is_vtag_good(struct sctp_inpcb *inp, uint32_t tag, struct timeval *now, int save_in_twait)
6186 * This function serves two purposes. It will see if a TAG can be
6187 * re-used and return 1 for yes it is ok and 0 for don't use that
6188 * tag. A secondary function it will do is purge out old tags that
6191 struct sctpasochead *head;
6192 struct sctpvtaghead *chain;
6193 struct sctp_tagblock *twait_block;
6194 struct sctp_tcb *stcb;
6197 SCTP_INP_INFO_WLOCK();
6198 chain = &SCTP_BASE_INFO(vtag_timewait[(tag % SCTP_STACK_VTAG_HASH_SIZE))];
6199 /* First is the vtag in use ? */
6201 head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag,
6202 SCTP_BASE_INFO(hashasocmark))];
6206 LIST_FOREACH(stcb, head, sctp_asocs) {
6208 if (stcb->asoc.my_vtag == tag) {
6210 * We should remove this if and return 0 always if
6211 * we want vtags unique across all endpoints. For
6212 * now within a endpoint is ok.
6214 if (inp == stcb->sctp_ep) {
6215 /* bad tag, in use */
6216 SCTP_INP_INFO_WUNLOCK();
6222 /* Now lets check the restart hash */
6223 head = &SCTP_BASE_INFO(sctp_restarthash)[SCTP_PCBHASH_ASOC(tag,
6224 SCTP_BASE_INFO(hashrestartmark))];
6226 goto check_time_wait;
6228 LIST_FOREACH(stcb, head, sctp_tcbrestarhash) {
6229 if (stcb->asoc.assoc_id == tag) {
6231 if (inp == stcb->sctp_ep) {
6232 /* bad tag, in use */
6233 SCTP_INP_INFO_WUNLOCK();
6239 /* Now what about timed wait ? */
6240 if (!SCTP_LIST_EMPTY(chain)) {
6242 * Block(s) are present, lets see if we have this tag in the
6245 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
6246 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
6247 if (twait_block->vtag_block[i].v_tag == 0) {
6250 } else if ((long)twait_block->vtag_block[i].tv_sec_at_expire <
6252 /* Audit expires this guy */
6253 twait_block->vtag_block[i].tv_sec_at_expire = 0;
6254 twait_block->vtag_block[i].v_tag = 0;
6255 } else if (twait_block->vtag_block[i].v_tag ==
6257 /* Bad tag, sorry :< */
6258 SCTP_INP_INFO_WUNLOCK();
6265 * Not found, ok to use the tag, add it to the time wait hash
6266 * as well this will prevent two sucessive cookies from getting
6267 * the same tag or two inits sent quickly on multi-processors.
6268 * We only keep the tag for the life of a cookie and when we
6269 * add this tag to the assoc hash we need to purge it from
6273 sctp_add_vtag_to_timewait(tag, TICKS_TO_SEC(inp->sctp_ep.def_cookie_life));
6274 SCTP_INP_INFO_WUNLOCK();
6279 static sctp_assoc_t reneged_asoc_ids[256];
6280 static uint8_t reneged_at = 0;
6284 sctp_drain_mbufs(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
6287 * We must hunt this association for MBUF's past the cumack (i.e.
6288 * out of order data that we can renege on).
6290 struct sctp_association *asoc;
6291 struct sctp_tmit_chunk *chk, *nchk;
6292 uint32_t cumulative_tsn_p1, tsn;
6293 struct sctp_queued_to_read *ctl, *nctl;
6294 int cnt, strmat, gap;
6296 /* We look for anything larger than the cum-ack + 1 */
6298 SCTP_STAT_INCR(sctps_protocol_drain_calls);
6299 if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
6303 if (asoc->cumulative_tsn == asoc->highest_tsn_inside_map) {
6304 /* none we can reneg on. */
6307 SCTP_STAT_INCR(sctps_protocol_drains_done);
6308 cumulative_tsn_p1 = asoc->cumulative_tsn + 1;
6310 /* First look in the re-assembly queue */
6311 chk = TAILQ_FIRST(&asoc->reasmqueue);
6313 /* Get the next one */
6314 nchk = TAILQ_NEXT(chk, sctp_next);
6315 if (compare_with_wrap(chk->rec.data.TSN_seq,
6316 cumulative_tsn_p1, MAX_TSN)) {
6317 /* Yep it is above cum-ack */
6319 tsn = chk->rec.data.TSN_seq;
6320 if (tsn >= asoc->mapping_array_base_tsn) {
6321 gap = tsn - asoc->mapping_array_base_tsn;
6323 gap = (MAX_TSN - asoc->mapping_array_base_tsn) +
6326 asoc->size_on_reasm_queue = sctp_sbspace_sub(asoc->size_on_reasm_queue, chk->send_size);
6327 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
6328 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
6329 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
6331 sctp_m_freem(chk->data);
6334 sctp_free_a_chunk(stcb, chk);
6338 /* Ok that was fun, now we will drain all the inbound streams? */
6339 for (strmat = 0; strmat < asoc->streamincnt; strmat++) {
6340 ctl = TAILQ_FIRST(&asoc->strmin[strmat].inqueue);
6342 nctl = TAILQ_NEXT(ctl, next);
6343 if (compare_with_wrap(ctl->sinfo_tsn,
6344 cumulative_tsn_p1, MAX_TSN)) {
6345 /* Yep it is above cum-ack */
6347 tsn = ctl->sinfo_tsn;
6348 if (tsn >= asoc->mapping_array_base_tsn) {
6350 asoc->mapping_array_base_tsn;
6353 asoc->mapping_array_base_tsn) +
6356 asoc->size_on_all_streams = sctp_sbspace_sub(asoc->size_on_all_streams, ctl->length);
6357 sctp_ucount_decr(asoc->cnt_on_all_streams);
6359 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array,
6361 TAILQ_REMOVE(&asoc->strmin[strmat].inqueue,
6364 sctp_m_freem(ctl->data);
6367 sctp_free_remote_addr(ctl->whoFrom);
6368 SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_readq), ctl);
6369 SCTP_DECR_READQ_COUNT();
6375 * Question, should we go through the delivery queue? The only
6376 * reason things are on here is the app not reading OR a p-d-api up.
6377 * An attacker COULD send enough in to initiate the PD-API and then
6378 * send a bunch of stuff to other streams... these would wind up on
6379 * the delivery queue.. and then we would not get to them. But in
6380 * order to do this I then have to back-track and un-deliver
6381 * sequence numbers in streams.. el-yucko. I think for now we will
6382 * NOT look at the delivery queue and leave it to be something to
6383 * consider later. An alternative would be to abort the P-D-API with
6384 * a notification and then deliver the data.... Or another method
6385 * might be to keep track of how many times the situation occurs and
6386 * if we see a possible attack underway just abort the association.
6390 SCTPDBG(SCTP_DEBUG_PCB1, "Freed %d chunks from reneg harvest\n", cnt);
6395 * Now do we need to find a new
6396 * asoc->highest_tsn_inside_map?
6398 if (asoc->highest_tsn_inside_map >= asoc->mapping_array_base_tsn) {
6399 gap = asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn;
6401 gap = (MAX_TSN - asoc->mapping_array_base_tsn) +
6402 asoc->highest_tsn_inside_map + 1;
6404 if (gap >= (asoc->mapping_array_size << 3)) {
6406 * Something bad happened or cum-ack and high were
6407 * behind the base, but if so earlier checks should
6408 * have found NO data... wierd... we will start at
6409 * end of mapping array.
6411 SCTP_PRINTF("Gap was larger than array?? %d set to max:%d maparraymax:%x\n",
6413 (int)(asoc->mapping_array_size << 3),
6414 (int)asoc->highest_tsn_inside_map);
6415 gap = asoc->mapping_array_size << 3;
6418 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
6419 /* found the new highest */
6420 asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn + gap;
6421 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
6422 sctp_log_map(0, 8, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
6429 /* Nothing left in map */
6430 memset(asoc->mapping_array, 0, asoc->mapping_array_size);
6431 asoc->mapping_array_base_tsn = asoc->cumulative_tsn + 1;
6432 asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
6433 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MAP_LOGGING_ENABLE) {
6434 sctp_log_map(0, 9, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
6437 asoc->last_revoke_count = cnt;
6438 (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
6439 /* sa_ignore NO_NULL_CHK */
6440 sctp_send_sack(stcb);
6441 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_DRAIN, SCTP_SO_NOT_LOCKED);
6442 reneged_asoc_ids[reneged_at] = sctp_get_associd(stcb);
6446 * Another issue, in un-setting the TSN's in the mapping array we
6447 * DID NOT adjust the higest_tsn marker. This will cause one of two
6448 * things to occur. It may cause us to do extra work in checking for
6449 * our mapping array movement. More importantly it may cause us to
6450 * SACK every datagram. This may not be a bad thing though since we
6451 * will recover once we get our cum-ack above and all this stuff we
6460 * We must walk the PCB lists for ALL associations here. The system
6461 * is LOW on MBUF's and needs help. This is where reneging will
6462 * occur. We really hope this does NOT happen!
6464 struct sctp_inpcb *inp;
6465 struct sctp_tcb *stcb;
6467 SCTP_INP_INFO_RLOCK();
6468 LIST_FOREACH(inp, &SCTP_BASE_INFO(listhead), sctp_list) {
6469 /* For each endpoint */
6470 SCTP_INP_RLOCK(inp);
6471 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6472 /* For each association */
6473 SCTP_TCB_LOCK(stcb);
6474 sctp_drain_mbufs(inp, stcb);
6475 SCTP_TCB_UNLOCK(stcb);
6477 SCTP_INP_RUNLOCK(inp);
6479 SCTP_INP_INFO_RUNLOCK();
6483 * start a new iterator
6484 * iterates through all endpoints and associations based on the pcb_state
6485 * flags and asoc_state. "af" (mandatory) is executed for all matching
6486 * assocs and "ef" (optional) is executed when the iterator completes.
6487 * "inpf" (optional) is executed for each new endpoint as it is being
6488 * iterated through. inpe (optional) is called when the inp completes
6489 * its way through all the stcbs.
6492 sctp_initiate_iterator(inp_func inpf,
6496 uint32_t pcb_features,
6497 uint32_t asoc_state,
6501 struct sctp_inpcb *s_inp,
6502 uint8_t chunk_output_off)
6504 struct sctp_iterator *it = NULL;
6509 SCTP_MALLOC(it, struct sctp_iterator *, sizeof(struct sctp_iterator),
6512 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
6515 memset(it, 0, sizeof(*it));
6516 it->function_assoc = af;
6517 it->function_inp = inpf;
6519 it->done_current_ep = 0;
6521 it->done_current_ep = 1;
6522 it->function_atend = ef;
6525 it->pcb_flags = pcb_state;
6526 it->pcb_features = pcb_features;
6527 it->asoc_state = asoc_state;
6528 it->function_inp_end = inpe;
6529 it->no_chunk_output = chunk_output_off;
6532 it->iterator_flags = SCTP_ITERATOR_DO_SINGLE_INP;
6534 SCTP_INP_INFO_RLOCK();
6535 it->inp = LIST_FIRST(&SCTP_BASE_INFO(listhead));
6537 SCTP_INP_INFO_RUNLOCK();
6538 it->iterator_flags = SCTP_ITERATOR_DO_ALL_INP;
6541 SCTP_IPI_ITERATOR_WQ_LOCK();
6543 SCTP_INP_INCR_REF(it->inp);
6545 TAILQ_INSERT_TAIL(&SCTP_BASE_INFO(iteratorhead), it, sctp_nxt_itr);
6546 #if defined(SCTP_USE_THREAD_BASED_ITERATOR)
6547 if (SCTP_BASE_INFO(iterator_running) == 0) {
6548 sctp_wakeup_iterator();
6550 SCTP_IPI_ITERATOR_WQ_UNLOCK();
6553 SCTP_INP_DECR_REF(it->inp);
6554 SCTP_IPI_ITERATOR_WQ_UNLOCK();
6555 /* Init the timer */
6556 SCTP_OS_TIMER_INIT(&it->tmr.timer);
6557 /* add to the list of all iterators */
6558 sctp_timer_start(SCTP_TIMER_TYPE_ITERATOR, (struct sctp_inpcb *)it,
6561 /* sa_ignore MEMLEAK {memory is put on the tailq for the iterator} */