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>
50 struct sctp_epinfo sctppcbinfo;
52 /* FIX: we don't handle multiple link local scopes */
53 /* "scopeless" replacement IN6_ARE_ADDR_EQUAL */
56 SCTP6_ARE_ADDR_EQUAL(struct in6_addr *a, struct in6_addr *b)
58 struct in6_addr tmp_a, tmp_b;
60 /* use a copy of a and b */
63 in6_clearscope(&tmp_a);
64 in6_clearscope(&tmp_b);
65 return (IN6_ARE_ADDR_EQUAL(&tmp_a, &tmp_b));
71 sctp_fill_pcbinfo(struct sctp_pcbinfo *spcb)
74 * We really don't need to lock this, but I will just because it
77 SCTP_INP_INFO_RLOCK();
78 spcb->ep_count = sctppcbinfo.ipi_count_ep;
79 spcb->asoc_count = sctppcbinfo.ipi_count_asoc;
80 spcb->laddr_count = sctppcbinfo.ipi_count_laddr;
81 spcb->raddr_count = sctppcbinfo.ipi_count_raddr;
82 spcb->chk_count = sctppcbinfo.ipi_count_chunk;
83 spcb->readq_count = sctppcbinfo.ipi_count_readq;
84 spcb->stream_oque = sctppcbinfo.ipi_count_strmoq;
85 spcb->free_chunks = sctppcbinfo.ipi_free_chunks;
87 SCTP_INP_INFO_RUNLOCK();
91 * Addresses are added to VRF's (Virtual Router's). For BSD we
92 * have only the default VRF 0. We maintain a hash list of
93 * VRF's. Each VRF has its own list of sctp_ifn's. Each of
94 * these has a list of addresses. When we add a new address
95 * to a VRF we lookup the ifn/ifn_index, if the ifn does
96 * not exist we create it and add it to the list of IFN's
97 * within the VRF. Once we have the sctp_ifn, we add the
98 * address to the list. So we look something like:
101 * vrf-> ifn-> ifn -> ifn
103 * ... +--ifa-> ifa -> ifa
106 * We keep these seperate lists since the SCTP subsystem will
107 * point to these from its source address selection nets structure.
108 * When an address is deleted it does not happen right away on
109 * the SCTP side, it gets scheduled. What we do when a
110 * delete happens is immediately remove the address from
111 * the master list and decrement the refcount. As our
112 * addip iterator works through and frees the src address
113 * selection pointing to the sctp_ifa, eventually the refcount
114 * will reach 0 and we will delete it. Note that it is assumed
115 * that any locking on system level ifn/ifa is done at the
116 * caller of these functions and these routines will only
117 * lock the SCTP structures as they add or delete things.
119 * Other notes on VRF concepts.
120 * - An endpoint can be in multiple VRF's
121 * - An association lives within a VRF and only one VRF.
122 * - Any incoming packet we can deduce the VRF for by
123 * looking at the mbuf/pak inbound (for BSD its VRF=0 :D)
124 * - Any downward send call or connect call must supply the
125 * VRF via ancillary data or via some sort of set default
126 * VRF socket option call (again for BSD no brainer since
127 * the VRF is always 0).
128 * - An endpoint may add multiple VRF's to it.
129 * - Listening sockets can accept associations in any
130 * of the VRF's they are in but the assoc will end up
131 * in only one VRF (gotten from the packet or connect/send).
136 sctp_allocate_vrf(int vrf_id)
138 struct sctp_vrf *vrf = NULL;
139 struct sctp_vrflist *bucket;
141 /* First allocate the VRF structure */
142 vrf = sctp_find_vrf(vrf_id);
144 /* Already allocated */
147 SCTP_MALLOC(vrf, struct sctp_vrf *, sizeof(struct sctp_vrf),
152 panic("No memory for VRF:%d", vrf_id);
157 memset(vrf, 0, sizeof(struct sctp_vrf));
158 vrf->vrf_id = vrf_id;
159 LIST_INIT(&vrf->ifnlist);
160 vrf->total_ifa_count = 0;
162 /* now also setup table ids */
163 SCTP_INIT_VRF_TABLEID(vrf);
164 /* Init the HASH of addresses */
165 vrf->vrf_addr_hash = SCTP_HASH_INIT(SCTP_VRF_ADDR_HASH_SIZE,
166 &vrf->vrf_addr_hashmark);
167 if (vrf->vrf_addr_hash == NULL) {
170 panic("No memory for VRF:%d", vrf_id);
172 SCTP_FREE(vrf, SCTP_M_VRF);
175 /* Add it to the hash table */
176 bucket = &sctppcbinfo.sctp_vrfhash[(vrf_id & sctppcbinfo.hashvrfmark)];
177 LIST_INSERT_HEAD(bucket, vrf, next_vrf);
178 atomic_add_int(&sctppcbinfo.ipi_count_vrfs, 1);
184 sctp_find_ifn(void *ifn, uint32_t ifn_index)
186 struct sctp_ifn *sctp_ifnp;
187 struct sctp_ifnlist *hash_ifn_head;
190 * We assume the lock is held for the addresses if thats wrong
191 * problems could occur :-)
193 hash_ifn_head = &sctppcbinfo.vrf_ifn_hash[(ifn_index & sctppcbinfo.vrf_ifn_hashmark)];
194 LIST_FOREACH(sctp_ifnp, hash_ifn_head, next_bucket) {
195 if (sctp_ifnp->ifn_index == ifn_index) {
198 if (sctp_ifnp->ifn_p && ifn && (sctp_ifnp->ifn_p == ifn)) {
208 sctp_find_vrf(uint32_t vrf_id)
210 struct sctp_vrflist *bucket;
211 struct sctp_vrf *liste;
213 bucket = &sctppcbinfo.sctp_vrfhash[(vrf_id & sctppcbinfo.hashvrfmark)];
214 LIST_FOREACH(liste, bucket, next_vrf) {
215 if (vrf_id == liste->vrf_id) {
223 sctp_free_vrf(struct sctp_vrf *vrf)
227 ret = atomic_fetchadd_int(&vrf->refcount, -1);
229 if (vrf->vrf_addr_hash) {
230 SCTP_HASH_FREE(vrf->vrf_addr_hash, vrf->vrf_addr_hashmark);
231 vrf->vrf_addr_hash = NULL;
233 /* We zero'd the count */
234 LIST_REMOVE(vrf, next_vrf);
235 SCTP_FREE(vrf, SCTP_M_VRF);
236 atomic_subtract_int(&sctppcbinfo.ipi_count_vrfs, 1);
241 sctp_free_ifn(struct sctp_ifn *sctp_ifnp)
245 ret = atomic_fetchadd_int(&sctp_ifnp->refcount, -1);
247 /* We zero'd the count */
248 if (sctp_ifnp->vrf) {
249 sctp_free_vrf(sctp_ifnp->vrf);
251 SCTP_FREE(sctp_ifnp, SCTP_M_IFN);
252 atomic_subtract_int(&sctppcbinfo.ipi_count_ifns, 1);
257 sctp_update_ifn_mtu(uint32_t ifn_index, uint32_t mtu)
259 struct sctp_ifn *sctp_ifnp;
261 sctp_ifnp = sctp_find_ifn((void *)NULL, ifn_index);
262 if (sctp_ifnp != NULL) {
263 sctp_ifnp->ifn_mtu = mtu;
269 sctp_free_ifa(struct sctp_ifa *sctp_ifap)
273 ret = atomic_fetchadd_int(&sctp_ifap->refcount, -1);
275 /* We zero'd the count */
276 if (sctp_ifap->ifn_p) {
277 sctp_free_ifn(sctp_ifap->ifn_p);
279 SCTP_FREE(sctp_ifap, SCTP_M_IFA);
280 atomic_subtract_int(&sctppcbinfo.ipi_count_ifas, 1);
285 sctp_delete_ifn(struct sctp_ifn *sctp_ifnp, int hold_addr_lock)
287 struct sctp_ifn *found;
289 found = sctp_find_ifn(sctp_ifnp->ifn_p, sctp_ifnp->ifn_index);
291 /* Not in the list.. sorry */
294 if (hold_addr_lock == 0)
295 SCTP_IPI_ADDR_WLOCK();
296 LIST_REMOVE(sctp_ifnp, next_bucket);
297 LIST_REMOVE(sctp_ifnp, next_ifn);
298 SCTP_DEREGISTER_INTERFACE(sctp_ifnp->ifn_index,
299 sctp_ifnp->registered_af);
300 if (hold_addr_lock == 0)
301 SCTP_IPI_ADDR_WUNLOCK();
302 /* Take away the reference, and possibly free it */
303 sctp_free_ifn(sctp_ifnp);
307 sctp_mark_ifa_addr_down(uint32_t vrf_id, struct sockaddr *addr,
308 const char *if_name, uint32_t ifn_index)
310 struct sctp_vrf *vrf;
311 struct sctp_ifa *sctp_ifap = NULL;
313 SCTP_IPI_ADDR_RLOCK();
314 vrf = sctp_find_vrf(vrf_id);
316 SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id);
320 sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
321 if (sctp_ifap == NULL) {
322 SCTPDBG(SCTP_DEBUG_PCB4, "Can't find sctp_ifap for address\n");
325 if (sctp_ifap->ifn_p == NULL) {
326 SCTPDBG(SCTP_DEBUG_PCB4, "IFA has no IFN - can't mark unuseable\n");
332 len1 = strlen(if_name);
333 len2 = strlen(sctp_ifap->ifn_p->ifn_name);
335 SCTPDBG(SCTP_DEBUG_PCB4, "IFN of ifa names different lenght %d vs %d - ignored\n",
339 if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, len1) != 0) {
340 SCTPDBG(SCTP_DEBUG_PCB4, "IFN %s of IFA not the same as %s\n",
341 sctp_ifap->ifn_p->ifn_name,
346 if (sctp_ifap->ifn_p->ifn_index != ifn_index) {
347 SCTPDBG(SCTP_DEBUG_PCB4, "IFA owned by ifn_index:%d down command for ifn_index:%d - ignored\n",
348 sctp_ifap->ifn_p->ifn_index, ifn_index);
353 sctp_ifap->localifa_flags &= (~SCTP_ADDR_VALID);
354 sctp_ifap->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE;
356 SCTP_IPI_ADDR_RUNLOCK();
360 sctp_mark_ifa_addr_up(uint32_t vrf_id, struct sockaddr *addr,
361 const char *if_name, uint32_t ifn_index)
363 struct sctp_vrf *vrf;
364 struct sctp_ifa *sctp_ifap = NULL;
366 SCTP_IPI_ADDR_RLOCK();
367 vrf = sctp_find_vrf(vrf_id);
369 SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id);
373 sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
374 if (sctp_ifap == NULL) {
375 SCTPDBG(SCTP_DEBUG_PCB4, "Can't find sctp_ifap for address\n");
378 if (sctp_ifap->ifn_p == NULL) {
379 SCTPDBG(SCTP_DEBUG_PCB4, "IFA has no IFN - can't mark unuseable\n");
385 len1 = strlen(if_name);
386 len2 = strlen(sctp_ifap->ifn_p->ifn_name);
388 SCTPDBG(SCTP_DEBUG_PCB4, "IFN of ifa names different lenght %d vs %d - ignored\n",
392 if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, len1) != 0) {
393 SCTPDBG(SCTP_DEBUG_PCB4, "IFN %s of IFA not the same as %s\n",
394 sctp_ifap->ifn_p->ifn_name,
399 if (sctp_ifap->ifn_p->ifn_index != ifn_index) {
400 SCTPDBG(SCTP_DEBUG_PCB4, "IFA owned by ifn_index:%d down command for ifn_index:%d - ignored\n",
401 sctp_ifap->ifn_p->ifn_index, ifn_index);
406 sctp_ifap->localifa_flags &= (~SCTP_ADDR_IFA_UNUSEABLE);
407 sctp_ifap->localifa_flags |= SCTP_ADDR_VALID;
409 SCTP_IPI_ADDR_RUNLOCK();
413 * Add an ifa to an ifn.
414 * Register the interface as necessary.
415 * NOTE: ADDR write lock MUST be held.
418 sctp_add_ifa_to_ifn(struct sctp_ifn *sctp_ifnp, struct sctp_ifa *sctp_ifap)
422 LIST_INSERT_HEAD(&sctp_ifnp->ifalist, sctp_ifap, next_ifa);
423 sctp_ifap->ifn_p = sctp_ifnp;
424 atomic_add_int(&sctp_ifap->ifn_p->refcount, 1);
425 /* update address counts */
426 sctp_ifnp->ifa_count++;
427 ifa_af = sctp_ifap->address.sa.sa_family;
428 if (ifa_af == AF_INET)
432 if (sctp_ifnp->ifa_count == 1) {
433 /* register the new interface */
434 SCTP_REGISTER_INTERFACE(sctp_ifnp->ifn_index, ifa_af);
435 sctp_ifnp->registered_af = ifa_af;
440 * Remove an ifa from its ifn.
441 * If no more addresses exist, remove the ifn too. Otherwise, re-register
442 * the interface based on the remaining address families left.
443 * NOTE: ADDR write lock MUST be held.
446 sctp_remove_ifa_from_ifn(struct sctp_ifa *sctp_ifap)
450 LIST_REMOVE(sctp_ifap, next_ifa);
451 if (sctp_ifap->ifn_p) {
452 /* update address counts */
453 sctp_ifap->ifn_p->ifa_count--;
454 if (sctp_ifap->address.sa.sa_family == AF_INET6)
455 sctp_ifap->ifn_p->num_v6--;
456 else if (sctp_ifap->address.sa.sa_family == AF_INET)
457 sctp_ifap->ifn_p->num_v4--;
459 ifn_index = sctp_ifap->ifn_p->ifn_index;
460 if (SCTP_LIST_EMPTY(&sctp_ifap->ifn_p->ifalist)) {
461 /* remove the ifn, possibly freeing it */
462 sctp_delete_ifn(sctp_ifap->ifn_p, SCTP_ADDR_LOCKED);
464 /* re-register address family type, if needed */
465 if ((sctp_ifap->ifn_p->num_v6 == 0) &&
466 (sctp_ifap->ifn_p->registered_af == AF_INET6)) {
467 SCTP_DEREGISTER_INTERFACE(ifn_index, AF_INET6);
468 SCTP_REGISTER_INTERFACE(ifn_index, AF_INET);
469 sctp_ifap->ifn_p->registered_af = AF_INET;
470 } else if ((sctp_ifap->ifn_p->num_v4 == 0) &&
471 (sctp_ifap->ifn_p->registered_af == AF_INET)) {
472 SCTP_DEREGISTER_INTERFACE(ifn_index, AF_INET);
473 SCTP_REGISTER_INTERFACE(ifn_index, AF_INET6);
474 sctp_ifap->ifn_p->registered_af = AF_INET6;
476 /* free the ifn refcount */
477 sctp_free_ifn(sctp_ifap->ifn_p);
479 sctp_ifap->ifn_p = NULL;
484 sctp_add_addr_to_vrf(uint32_t vrf_id, void *ifn, uint32_t ifn_index,
485 uint32_t ifn_type, const char *if_name, void *ifa,
486 struct sockaddr *addr, uint32_t ifa_flags,
489 struct sctp_vrf *vrf;
490 struct sctp_ifn *sctp_ifnp = NULL;
491 struct sctp_ifa *sctp_ifap = NULL;
492 struct sctp_ifalist *hash_addr_head;
493 struct sctp_ifnlist *hash_ifn_head;
494 uint32_t hash_of_addr;
498 SCTPDBG(SCTP_DEBUG_PCB4, "vrf_id 0x%x: adding address: ", vrf_id);
499 SCTPDBG_ADDR(SCTP_DEBUG_PCB4, addr);
501 SCTP_IPI_ADDR_WLOCK();
502 sctp_ifnp = sctp_find_ifn(ifn, ifn_index);
504 vrf = sctp_ifnp->vrf;
506 vrf = sctp_find_vrf(vrf_id);
508 vrf = sctp_allocate_vrf(vrf_id);
510 SCTP_IPI_ADDR_WUNLOCK();
515 if (sctp_ifnp == NULL) {
517 * build one and add it, can't hold lock until after malloc
520 SCTP_IPI_ADDR_WUNLOCK();
521 SCTP_MALLOC(sctp_ifnp, struct sctp_ifn *,
522 sizeof(struct sctp_ifn), SCTP_M_IFN);
523 if (sctp_ifnp == NULL) {
525 panic("No memory for IFN:%u", sctp_ifnp->ifn_index);
529 memset(sctp_ifnp, 0, sizeof(struct sctp_ifn));
530 sctp_ifnp->ifn_index = ifn_index;
531 sctp_ifnp->ifn_p = ifn;
532 sctp_ifnp->ifn_type = ifn_type;
533 sctp_ifnp->refcount = 0;
534 sctp_ifnp->vrf = vrf;
535 atomic_add_int(&vrf->refcount, 1);
536 sctp_ifnp->ifn_mtu = SCTP_GATHER_MTU_FROM_IFN_INFO(ifn, ifn_index, addr->sa_family);
537 if (if_name != NULL) {
538 memcpy(sctp_ifnp->ifn_name, if_name, SCTP_IFNAMSIZ);
540 memcpy(sctp_ifnp->ifn_name, "unknown", min(7, SCTP_IFNAMSIZ));
542 hash_ifn_head = &sctppcbinfo.vrf_ifn_hash[(ifn_index & sctppcbinfo.vrf_ifn_hashmark)];
543 LIST_INIT(&sctp_ifnp->ifalist);
544 SCTP_IPI_ADDR_WLOCK();
545 LIST_INSERT_HEAD(hash_ifn_head, sctp_ifnp, next_bucket);
546 LIST_INSERT_HEAD(&vrf->ifnlist, sctp_ifnp, next_ifn);
547 atomic_add_int(&sctppcbinfo.ipi_count_ifns, 1);
550 sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
552 /* Hmm, it already exists? */
553 if ((sctp_ifap->ifn_p) &&
554 (sctp_ifap->ifn_p->ifn_index == ifn_index)) {
555 SCTPDBG(SCTP_DEBUG_PCB4, "Using existing ifn %s (0x%x) for ifa %p\n",
556 sctp_ifap->ifn_p->ifn_name, ifn_index,
559 /* Remove the created one that we don't want */
560 sctp_delete_ifn(sctp_ifnp, SCTP_ADDR_LOCKED);
562 if (sctp_ifap->localifa_flags & SCTP_BEING_DELETED) {
563 /* easy to solve, just switch back to active */
564 SCTPDBG(SCTP_DEBUG_PCB4, "Clearing deleted ifa flag\n");
565 sctp_ifap->localifa_flags = SCTP_ADDR_VALID;
566 sctp_ifap->ifn_p = sctp_ifnp;
567 atomic_add_int(&sctp_ifap->ifn_p->refcount, 1);
570 SCTP_IPI_ADDR_WUNLOCK();
573 if (sctp_ifap->ifn_p) {
575 * The last IFN gets the address, removee
578 SCTPDBG(SCTP_DEBUG_PCB4, "Moving ifa %p from %s (0x%x) to %s (0x%x)\n",
579 sctp_ifap, sctp_ifap->ifn_p->ifn_name,
580 sctp_ifap->ifn_p->ifn_index, if_name,
582 /* remove the address from the old ifn */
583 sctp_remove_ifa_from_ifn(sctp_ifap);
584 /* move the address over to the new ifn */
585 sctp_add_ifa_to_ifn(sctp_ifnp, sctp_ifap);
586 goto exit_stage_left;
588 /* repair ifnp which was NULL ? */
589 sctp_ifap->localifa_flags = SCTP_ADDR_VALID;
590 SCTPDBG(SCTP_DEBUG_PCB4, "Repairing ifn %p for ifa %p\n",
591 sctp_ifnp, sctp_ifap);
592 sctp_add_ifa_to_ifn(sctp_ifnp, sctp_ifap);
594 goto exit_stage_left;
597 SCTP_IPI_ADDR_WUNLOCK();
598 SCTP_MALLOC(sctp_ifap, struct sctp_ifa *, sizeof(struct sctp_ifa), SCTP_M_IFA);
599 if (sctp_ifap == NULL) {
601 panic("No memory for IFA");
605 memset(sctp_ifap, 0, sizeof(struct sctp_ifa));
606 sctp_ifap->ifn_p = sctp_ifnp;
607 atomic_add_int(&sctp_ifnp->refcount, 1);
608 sctp_ifap->vrf_id = vrf_id;
609 sctp_ifap->ifa = ifa;
610 memcpy(&sctp_ifap->address, addr, addr->sa_len);
611 sctp_ifap->localifa_flags = SCTP_ADDR_VALID | SCTP_ADDR_DEFER_USE;
612 sctp_ifap->flags = ifa_flags;
614 switch (sctp_ifap->address.sa.sa_family) {
617 struct sockaddr_in *sin;
619 sin = (struct sockaddr_in *)&sctp_ifap->address.sin;
620 if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) ||
621 (IN4_ISLOOPBACK_ADDRESS(&sin->sin_addr))) {
622 sctp_ifap->src_is_loop = 1;
624 if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
625 sctp_ifap->src_is_priv = 1;
629 new_ifn_af = AF_INET;
635 /* ok to use deprecated addresses? */
636 struct sockaddr_in6 *sin6;
638 sin6 = (struct sockaddr_in6 *)&sctp_ifap->address.sin6;
639 if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) ||
640 (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))) {
641 sctp_ifap->src_is_loop = 1;
643 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
644 sctp_ifap->src_is_priv = 1;
648 new_ifn_af = AF_INET6;
656 hash_of_addr = sctp_get_ifa_hash_val(&sctp_ifap->address.sa);
658 if ((sctp_ifap->src_is_priv == 0) &&
659 (sctp_ifap->src_is_loop == 0)) {
660 sctp_ifap->src_is_glob = 1;
662 SCTP_IPI_ADDR_WLOCK();
663 hash_addr_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)];
664 LIST_INSERT_HEAD(hash_addr_head, sctp_ifap, next_bucket);
665 sctp_ifap->refcount = 1;
666 LIST_INSERT_HEAD(&sctp_ifnp->ifalist, sctp_ifap, next_ifa);
667 sctp_ifnp->ifa_count++;
668 vrf->total_ifa_count++;
669 atomic_add_int(&sctppcbinfo.ipi_count_ifas, 1);
671 SCTP_REGISTER_INTERFACE(ifn_index, new_ifn_af);
672 sctp_ifnp->registered_af = new_ifn_af;
674 SCTP_IPI_ADDR_WUNLOCK();
677 * Bump up the refcount so that when the timer completes it
678 * will drop back down.
680 struct sctp_laddr *wi;
682 atomic_add_int(&sctp_ifap->refcount, 1);
683 wi = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_laddr, struct sctp_laddr);
686 * Gak, what can we do? We have lost an address
687 * change can you say HOSED?
689 SCTPDBG(SCTP_DEBUG_PCB4, "Lost an address change?\n");
690 /* Opps, must decrement the count */
691 sctp_del_addr_from_vrf(vrf_id, addr, ifn_index,
695 SCTP_INCR_LADDR_COUNT();
696 bzero(wi, sizeof(*wi));
697 (void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
699 wi->action = SCTP_ADD_IP_ADDRESS;
700 SCTP_IPI_ITERATOR_WQ_LOCK();
702 * Should this really be a tailq? As it is we will process
703 * the newest first :-0
705 LIST_INSERT_HEAD(&sctppcbinfo.addr_wq, wi, sctp_nxt_addr);
706 SCTP_IPI_ITERATOR_WQ_UNLOCK();
707 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
708 (struct sctp_inpcb *)NULL,
709 (struct sctp_tcb *)NULL,
710 (struct sctp_nets *)NULL);
712 /* it's ready for use */
713 sctp_ifap->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
719 sctp_del_addr_from_vrf(uint32_t vrf_id, struct sockaddr *addr,
720 uint32_t ifn_index, const char *if_name)
722 struct sctp_vrf *vrf;
723 struct sctp_ifa *sctp_ifap = NULL;
725 SCTP_IPI_ADDR_WLOCK();
726 vrf = sctp_find_vrf(vrf_id);
728 SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id);
732 SCTPDBG(SCTP_DEBUG_PCB4, "vrf_id 0x%x: deleting address:", vrf_id);
733 SCTPDBG_ADDR(SCTP_DEBUG_PCB4, addr);
735 sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
737 /* Validate the delete */
738 if (sctp_ifap->ifn_p) {
742 * The name has priority over the ifn_index
743 * if its given. We do this especially for
744 * panda who might recycle indexes fast.
749 len1 = min(SCTP_IFNAMSIZ, strlen(if_name));
750 len2 = min(SCTP_IFNAMSIZ, strlen(sctp_ifap->ifn_p->ifn_name));
751 if (len1 && len2 && (len1 == len2)) {
752 /* we can compare them */
753 if (strncmp(if_name, sctp_ifap->ifn_p->ifn_name, len1) == 0) {
755 * They match its a correct
763 /* last ditch check ifn_index */
764 if (ifn_index == sctp_ifap->ifn_p->ifn_index) {
769 SCTPDBG(SCTP_DEBUG_PCB4, "ifn:%d ifname:%s does not match addresses\n",
770 ifn_index, ((if_name == NULL) ? "NULL" : if_name));
771 SCTPDBG(SCTP_DEBUG_PCB4, "ifn:%d ifname:%s - ignoring delete\n",
772 sctp_ifap->ifn_p->ifn_index, sctp_ifap->ifn_p->ifn_name);
773 SCTP_IPI_ADDR_WUNLOCK();
777 SCTPDBG(SCTP_DEBUG_PCB4, "Deleting ifa %p\n", sctp_ifap);
778 sctp_ifap->localifa_flags &= SCTP_ADDR_VALID;
779 sctp_ifap->localifa_flags |= SCTP_BEING_DELETED;
780 vrf->total_ifa_count--;
781 LIST_REMOVE(sctp_ifap, next_bucket);
782 sctp_remove_ifa_from_ifn(sctp_ifap);
786 SCTPDBG(SCTP_DEBUG_PCB4, "Del Addr-ifn:%d Could not find address:",
788 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr);
793 SCTP_IPI_ADDR_WUNLOCK();
795 struct sctp_laddr *wi;
797 wi = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_laddr, struct sctp_laddr);
800 * Gak, what can we do? We have lost an address
801 * change can you say HOSED?
803 SCTPDBG(SCTP_DEBUG_PCB4, "Lost an address change?\n");
805 /* Oops, must decrement the count */
806 sctp_free_ifa(sctp_ifap);
809 SCTP_INCR_LADDR_COUNT();
810 bzero(wi, sizeof(*wi));
811 (void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
813 wi->action = SCTP_DEL_IP_ADDRESS;
814 SCTP_IPI_ITERATOR_WQ_LOCK();
816 * Should this really be a tailq? As it is we will process
817 * the newest first :-0
819 LIST_INSERT_HEAD(&sctppcbinfo.addr_wq, wi, sctp_nxt_addr);
820 SCTP_IPI_ITERATOR_WQ_UNLOCK();
822 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
823 (struct sctp_inpcb *)NULL,
824 (struct sctp_tcb *)NULL,
825 (struct sctp_nets *)NULL);
831 static struct sctp_tcb *
832 sctp_tcb_special_locate(struct sctp_inpcb **inp_p, struct sockaddr *from,
833 struct sockaddr *to, struct sctp_nets **netp, uint32_t vrf_id)
835 /**** ASSUMES THE CALLER holds the INP_INFO_RLOCK */
837 * If we support the TCP model, then we must now dig through to see
838 * if we can find our endpoint in the list of tcp ep's.
840 uint16_t lport, rport;
841 struct sctppcbhead *ephead;
842 struct sctp_inpcb *inp;
843 struct sctp_laddr *laddr;
844 struct sctp_tcb *stcb;
845 struct sctp_nets *net;
847 if ((to == NULL) || (from == NULL)) {
850 if (to->sa_family == AF_INET && from->sa_family == AF_INET) {
851 lport = ((struct sockaddr_in *)to)->sin_port;
852 rport = ((struct sockaddr_in *)from)->sin_port;
853 } else if (to->sa_family == AF_INET6 && from->sa_family == AF_INET6) {
854 lport = ((struct sockaddr_in6 *)to)->sin6_port;
855 rport = ((struct sockaddr_in6 *)from)->sin6_port;
859 ephead = &sctppcbinfo.sctp_tcpephash[SCTP_PCBHASH_ALLADDR(
860 (lport + rport), sctppcbinfo.hashtcpmark)];
862 * Ok now for each of the guys in this bucket we must look and see:
863 * - Does the remote port match. - Does there single association's
864 * addresses match this address (to). If so we update p_ep to point
865 * to this ep and return the tcb from it.
867 LIST_FOREACH(inp, ephead, sctp_hash) {
869 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
870 SCTP_INP_RUNLOCK(inp);
873 if (lport != inp->sctp_lport) {
874 SCTP_INP_RUNLOCK(inp);
877 if (inp->def_vrf_id != vrf_id) {
878 SCTP_INP_RUNLOCK(inp);
881 /* check to see if the ep has one of the addresses */
882 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
883 /* We are NOT bound all, so look further */
886 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
888 if (laddr->ifa == NULL) {
889 SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n", __FUNCTION__);
892 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
893 SCTPDBG(SCTP_DEBUG_PCB1, "ifa being deleted\n");
896 if (laddr->ifa->address.sa.sa_family ==
898 /* see if it matches */
899 struct sockaddr_in *intf_addr, *sin;
901 intf_addr = &laddr->ifa->address.sin;
902 sin = (struct sockaddr_in *)to;
903 if (from->sa_family == AF_INET) {
904 if (sin->sin_addr.s_addr ==
905 intf_addr->sin_addr.s_addr) {
911 if (from->sa_family == AF_INET6) {
912 struct sockaddr_in6 *intf_addr6;
913 struct sockaddr_in6 *sin6;
915 sin6 = (struct sockaddr_in6 *)
917 intf_addr6 = &laddr->ifa->address.sin6;
919 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
920 &intf_addr6->sin6_addr)) {
929 /* This endpoint does not have this address */
930 SCTP_INP_RUNLOCK(inp);
935 * Ok if we hit here the ep has the address, does it hold
939 stcb = LIST_FIRST(&inp->sctp_asoc_list);
941 SCTP_INP_RUNLOCK(inp);
945 if (stcb->rport != rport) {
946 /* remote port does not match. */
947 SCTP_TCB_UNLOCK(stcb);
948 SCTP_INP_RUNLOCK(inp);
951 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
952 SCTP_TCB_UNLOCK(stcb);
953 SCTP_INP_RUNLOCK(inp);
956 /* Does this TCB have a matching address? */
957 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
959 if (net->ro._l_addr.sa.sa_family != from->sa_family) {
960 /* not the same family, can't be a match */
963 switch (from->sa_family) {
966 struct sockaddr_in *sin, *rsin;
968 sin = (struct sockaddr_in *)&net->ro._l_addr;
969 rsin = (struct sockaddr_in *)from;
970 if (sin->sin_addr.s_addr ==
971 rsin->sin_addr.s_addr) {
977 * Update the endpoint
981 SCTP_INP_RUNLOCK(inp);
989 struct sockaddr_in6 *sin6, *rsin6;
991 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
992 rsin6 = (struct sockaddr_in6 *)from;
993 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
994 &rsin6->sin6_addr)) {
1000 * Update the endpoint
1004 SCTP_INP_RUNLOCK(inp);
1015 SCTP_TCB_UNLOCK(stcb);
1016 SCTP_INP_RUNLOCK(inp);
1024 * 1) If I return a NULL you must decrement any INP ref cnt. 2) If I find an
1025 * stcb, both will be locked (locked_tcb and stcb) but decrement will be done
1026 * (if locked == NULL). 3) Decrement happens on return ONLY if locked ==
1031 sctp_findassociation_ep_addr(struct sctp_inpcb **inp_p, struct sockaddr *remote,
1032 struct sctp_nets **netp, struct sockaddr *local, struct sctp_tcb *locked_tcb)
1034 struct sctpasochead *head;
1035 struct sctp_inpcb *inp;
1036 struct sctp_tcb *stcb = NULL;
1037 struct sctp_nets *net;
1041 if (remote->sa_family == AF_INET) {
1042 rport = (((struct sockaddr_in *)remote)->sin_port);
1043 } else if (remote->sa_family == AF_INET6) {
1044 rport = (((struct sockaddr_in6 *)remote)->sin6_port);
1050 * UN-lock so we can do proper locking here this occurs when
1051 * called from load_addresses_from_init.
1053 atomic_add_int(&locked_tcb->asoc.refcnt, 1);
1054 SCTP_TCB_UNLOCK(locked_tcb);
1056 SCTP_INP_INFO_RLOCK();
1057 if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1059 * Now either this guy is our listener or it's the
1060 * connector. If it is the one that issued the connect, then
1061 * it's only chance is to be the first TCB in the list. If
1062 * it is the acceptor, then do the special_lookup to hash
1063 * and find the real inp.
1065 if ((inp->sctp_socket) && (inp->sctp_socket->so_qlimit)) {
1066 /* to is peer addr, from is my addr */
1067 stcb = sctp_tcb_special_locate(inp_p, remote, local,
1068 netp, inp->def_vrf_id);
1069 if ((stcb != NULL) && (locked_tcb == NULL)) {
1070 /* we have a locked tcb, lower refcount */
1071 SCTP_INP_DECR_REF(inp);
1073 if ((locked_tcb != NULL) && (locked_tcb != stcb)) {
1074 SCTP_INP_RLOCK(locked_tcb->sctp_ep);
1075 SCTP_TCB_LOCK(locked_tcb);
1076 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1077 SCTP_INP_RUNLOCK(locked_tcb->sctp_ep);
1079 SCTP_INP_INFO_RUNLOCK();
1082 SCTP_INP_WLOCK(inp);
1083 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1086 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1090 SCTP_TCB_LOCK(stcb);
1092 if (stcb->rport != rport) {
1093 /* remote port does not match. */
1094 SCTP_TCB_UNLOCK(stcb);
1097 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
1098 SCTP_TCB_UNLOCK(stcb);
1101 /* now look at the list of remote addresses */
1102 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1104 if (net == (TAILQ_NEXT(net, sctp_next))) {
1105 panic("Corrupt net list");
1108 if (net->ro._l_addr.sa.sa_family !=
1109 remote->sa_family) {
1110 /* not the same family */
1113 switch (remote->sa_family) {
1116 struct sockaddr_in *sin,
1119 sin = (struct sockaddr_in *)
1121 rsin = (struct sockaddr_in *)remote;
1122 if (sin->sin_addr.s_addr ==
1123 rsin->sin_addr.s_addr) {
1128 if (locked_tcb == NULL) {
1129 SCTP_INP_DECR_REF(inp);
1130 } else if (locked_tcb != stcb) {
1131 SCTP_TCB_LOCK(locked_tcb);
1134 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1136 SCTP_INP_WUNLOCK(inp);
1137 SCTP_INP_INFO_RUNLOCK();
1145 struct sockaddr_in6 *sin6,
1148 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1149 rsin6 = (struct sockaddr_in6 *)remote;
1150 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
1151 &rsin6->sin6_addr)) {
1156 if (locked_tcb == NULL) {
1157 SCTP_INP_DECR_REF(inp);
1158 } else if (locked_tcb != stcb) {
1159 SCTP_TCB_LOCK(locked_tcb);
1162 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1164 SCTP_INP_WUNLOCK(inp);
1165 SCTP_INP_INFO_RUNLOCK();
1176 SCTP_TCB_UNLOCK(stcb);
1179 SCTP_INP_WLOCK(inp);
1180 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1183 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(rport,
1184 inp->sctp_hashmark)];
1188 LIST_FOREACH(stcb, head, sctp_tcbhash) {
1189 if (stcb->rport != rport) {
1190 /* remote port does not match */
1193 SCTP_TCB_LOCK(stcb);
1194 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
1195 SCTP_TCB_UNLOCK(stcb);
1198 /* now look at the list of remote addresses */
1199 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1201 if (net == (TAILQ_NEXT(net, sctp_next))) {
1202 panic("Corrupt net list");
1205 if (net->ro._l_addr.sa.sa_family !=
1206 remote->sa_family) {
1207 /* not the same family */
1210 switch (remote->sa_family) {
1213 struct sockaddr_in *sin,
1216 sin = (struct sockaddr_in *)
1218 rsin = (struct sockaddr_in *)remote;
1219 if (sin->sin_addr.s_addr ==
1220 rsin->sin_addr.s_addr) {
1225 if (locked_tcb == NULL) {
1226 SCTP_INP_DECR_REF(inp);
1227 } else if (locked_tcb != stcb) {
1228 SCTP_TCB_LOCK(locked_tcb);
1231 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1233 SCTP_INP_WUNLOCK(inp);
1234 SCTP_INP_INFO_RUNLOCK();
1242 struct sockaddr_in6 *sin6,
1245 sin6 = (struct sockaddr_in6 *)
1247 rsin6 = (struct sockaddr_in6 *)remote;
1248 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
1249 &rsin6->sin6_addr)) {
1254 if (locked_tcb == NULL) {
1255 SCTP_INP_DECR_REF(inp);
1256 } else if (locked_tcb != stcb) {
1257 SCTP_TCB_LOCK(locked_tcb);
1260 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1262 SCTP_INP_WUNLOCK(inp);
1263 SCTP_INP_INFO_RUNLOCK();
1274 SCTP_TCB_UNLOCK(stcb);
1278 /* clean up for returning null */
1280 SCTP_TCB_LOCK(locked_tcb);
1281 atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1283 SCTP_INP_WUNLOCK(inp);
1284 SCTP_INP_INFO_RUNLOCK();
1290 * Find an association for a specific endpoint using the association id given
1291 * out in the COMM_UP notification
1295 sctp_findassociation_ep_asocid(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock)
1298 * Use my the assoc_id to find a endpoint
1300 struct sctpasochead *head;
1301 struct sctp_tcb *stcb;
1304 if (asoc_id == 0 || inp == NULL) {
1307 SCTP_INP_INFO_RLOCK();
1308 id = (uint32_t) asoc_id;
1309 head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(id,
1310 sctppcbinfo.hashasocmark)];
1312 /* invalid id TSNH */
1313 SCTP_INP_INFO_RUNLOCK();
1316 LIST_FOREACH(stcb, head, sctp_asocs) {
1317 SCTP_INP_RLOCK(stcb->sctp_ep);
1318 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1319 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1320 SCTP_INP_INFO_RUNLOCK();
1323 if (stcb->asoc.assoc_id == id) {
1325 if (inp != stcb->sctp_ep) {
1327 * some other guy has the same id active (id
1330 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1333 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
1337 SCTP_TCB_LOCK(stcb);
1339 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1340 SCTP_INP_INFO_RUNLOCK();
1343 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1345 /* Ok if we missed here, lets try the restart hash */
1346 head = &sctppcbinfo.sctp_restarthash[SCTP_PCBHASH_ASOC(id, sctppcbinfo.hashrestartmark)];
1348 /* invalid id TSNH */
1349 SCTP_INP_INFO_RUNLOCK();
1352 LIST_FOREACH(stcb, head, sctp_tcbrestarhash) {
1353 SCTP_INP_RLOCK(stcb->sctp_ep);
1354 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1355 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1358 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
1362 SCTP_TCB_LOCK(stcb);
1364 if (stcb->asoc.assoc_id == id) {
1366 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1367 if (inp != stcb->sctp_ep) {
1369 * some other guy has the same id active (id
1373 SCTP_TCB_UNLOCK(stcb);
1377 SCTP_INP_INFO_RUNLOCK();
1380 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1383 SCTP_TCB_UNLOCK(stcb);
1386 SCTP_INP_INFO_RUNLOCK();
1391 static struct sctp_inpcb *
1392 sctp_endpoint_probe(struct sockaddr *nam, struct sctppcbhead *head,
1393 uint16_t lport, uint32_t vrf_id)
1395 struct sctp_inpcb *inp;
1396 struct sockaddr_in *sin;
1399 struct sockaddr_in6 *sin6;
1402 struct sctp_laddr *laddr;
1405 struct sockaddr_in6 *intf_addr6;
1412 * Endpoing probe expects that the INP_INFO is locked.
1418 switch (nam->sa_family) {
1420 sin = (struct sockaddr_in *)nam;
1424 sin6 = (struct sockaddr_in6 *)nam;
1428 /* unsupported family */
1433 LIST_FOREACH(inp, head, sctp_hash) {
1434 SCTP_INP_RLOCK(inp);
1435 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1436 SCTP_INP_RUNLOCK(inp);
1439 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) &&
1440 (inp->sctp_lport == lport)) {
1442 if ((nam->sa_family == AF_INET) &&
1443 (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1444 SCTP_IPV6_V6ONLY(inp)) {
1445 /* IPv4 on a IPv6 socket with ONLY IPv6 set */
1446 SCTP_INP_RUNLOCK(inp);
1449 /* A V6 address and the endpoint is NOT bound V6 */
1450 if (nam->sa_family == AF_INET6 &&
1451 (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
1452 SCTP_INP_RUNLOCK(inp);
1455 /* does a VRF id match? */
1457 if (inp->def_vrf_id == vrf_id)
1460 SCTP_INP_RUNLOCK(inp);
1465 SCTP_INP_RUNLOCK(inp);
1468 if ((nam->sa_family == AF_INET) &&
1469 (sin->sin_addr.s_addr == INADDR_ANY)) {
1470 /* Can't hunt for one that has no address specified */
1474 if ((nam->sa_family == AF_INET6) &&
1475 (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))) {
1476 /* Can't hunt for one that has no address specified */
1481 * ok, not bound to all so see if we can find a EP bound to this
1484 LIST_FOREACH(inp, head, sctp_hash) {
1485 SCTP_INP_RLOCK(inp);
1486 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1487 SCTP_INP_RUNLOCK(inp);
1490 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)) {
1491 SCTP_INP_RUNLOCK(inp);
1495 * Ok this could be a likely candidate, look at all of its
1498 if (inp->sctp_lport != lport) {
1499 SCTP_INP_RUNLOCK(inp);
1502 /* does a VRF id match? */
1504 if (inp->def_vrf_id == vrf_id)
1508 SCTP_INP_RUNLOCK(inp);
1511 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1512 if (laddr->ifa == NULL) {
1513 SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n",
1517 SCTPDBG(SCTP_DEBUG_PCB1, "Ok laddr->ifa:%p is possible, ",
1519 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
1520 SCTPDBG(SCTP_DEBUG_PCB1, "Huh IFA being deleted\n");
1523 if (laddr->ifa->address.sa.sa_family == nam->sa_family) {
1524 /* possible, see if it matches */
1525 struct sockaddr_in *intf_addr;
1527 intf_addr = &laddr->ifa->address.sin;
1528 switch (nam->sa_family) {
1530 if (sin->sin_addr.s_addr ==
1531 intf_addr->sin_addr.s_addr) {
1532 SCTP_INP_RUNLOCK(inp);
1538 intf_addr6 = &laddr->ifa->address.sin6;
1539 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
1540 &intf_addr6->sin6_addr)) {
1541 SCTP_INP_RUNLOCK(inp);
1549 SCTP_INP_RUNLOCK(inp);
1555 sctp_pcb_findep(struct sockaddr *nam, int find_tcp_pool, int have_lock,
1559 * First we check the hash table to see if someone has this port
1560 * bound with just the port.
1562 struct sctp_inpcb *inp;
1563 struct sctppcbhead *head;
1564 struct sockaddr_in *sin;
1565 struct sockaddr_in6 *sin6;
1568 if (nam->sa_family == AF_INET) {
1569 sin = (struct sockaddr_in *)nam;
1570 lport = ((struct sockaddr_in *)nam)->sin_port;
1571 } else if (nam->sa_family == AF_INET6) {
1572 sin6 = (struct sockaddr_in6 *)nam;
1573 lport = ((struct sockaddr_in6 *)nam)->sin6_port;
1575 /* unsupported family */
1579 * I could cheat here and just cast to one of the types but we will
1580 * do it right. It also provides the check against an Unsupported
1583 /* Find the head of the ALLADDR chain */
1584 if (have_lock == 0) {
1585 SCTP_INP_INFO_RLOCK();
1587 head = &sctppcbinfo.sctp_ephash[SCTP_PCBHASH_ALLADDR(lport,
1588 sctppcbinfo.hashmark)];
1589 inp = sctp_endpoint_probe(nam, head, lport, vrf_id);
1592 * If the TCP model exists it could be that the main listening
1593 * endpoint is gone but there exists a connected socket for this guy
1594 * yet. If so we can return the first one that we find. This may NOT
1595 * be the correct one but the sctp_findassociation_ep_addr has
1596 * further code to look at all TCP models.
1598 if (inp == NULL && find_tcp_pool) {
1601 for (i = 0; i < sctppcbinfo.hashtblsize; i++) {
1603 * This is real gross, but we do NOT have a remote
1604 * port at this point depending on who is calling.
1605 * We must therefore look for ANY one that matches
1608 head = &sctppcbinfo.sctp_tcpephash[i];
1609 if (LIST_FIRST(head)) {
1610 inp = sctp_endpoint_probe(nam, head, lport, vrf_id);
1619 SCTP_INP_INCR_REF(inp);
1621 if (have_lock == 0) {
1622 SCTP_INP_INFO_RUNLOCK();
1628 * Find an association for an endpoint with the pointer to whom you want to
1629 * send to and the endpoint pointer. The address can be IPv4 or IPv6. We may
1630 * need to change the *to to some other struct like a mbuf...
1633 sctp_findassociation_addr_sa(struct sockaddr *to, struct sockaddr *from,
1634 struct sctp_inpcb **inp_p, struct sctp_nets **netp, int find_tcp_pool,
1637 struct sctp_inpcb *inp = NULL;
1638 struct sctp_tcb *retval;
1640 SCTP_INP_INFO_RLOCK();
1641 if (find_tcp_pool) {
1642 if (inp_p != NULL) {
1643 retval = sctp_tcb_special_locate(inp_p, from, to, netp,
1646 retval = sctp_tcb_special_locate(&inp, from, to, netp,
1649 if (retval != NULL) {
1650 SCTP_INP_INFO_RUNLOCK();
1654 inp = sctp_pcb_findep(to, 0, 1, vrf_id);
1655 if (inp_p != NULL) {
1658 SCTP_INP_INFO_RUNLOCK();
1664 * ok, we have an endpoint, now lets find the assoc for it (if any)
1665 * we now place the source address or from in the to of the find
1666 * endpoint call. Since in reality this chain is used from the
1667 * inbound packet side.
1669 if (inp_p != NULL) {
1670 retval = sctp_findassociation_ep_addr(inp_p, from, netp, to,
1673 retval = sctp_findassociation_ep_addr(&inp, from, netp, to,
1681 * This routine will grub through the mbuf that is a INIT or INIT-ACK and
1682 * find all addresses that the sender has specified in any address list. Each
1683 * address will be used to lookup the TCB and see if one exits.
1685 static struct sctp_tcb *
1686 sctp_findassociation_special_addr(struct mbuf *m, int iphlen, int offset,
1687 struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp,
1688 struct sockaddr *dest)
1690 struct sockaddr_in sin4;
1691 struct sockaddr_in6 sin6;
1692 struct sctp_paramhdr *phdr, parm_buf;
1693 struct sctp_tcb *retval;
1694 uint32_t ptype, plen;
1696 memset(&sin4, 0, sizeof(sin4));
1697 memset(&sin6, 0, sizeof(sin6));
1698 sin4.sin_len = sizeof(sin4);
1699 sin4.sin_family = AF_INET;
1700 sin4.sin_port = sh->src_port;
1701 sin6.sin6_len = sizeof(sin6);
1702 sin6.sin6_family = AF_INET6;
1703 sin6.sin6_port = sh->src_port;
1706 offset += sizeof(struct sctp_init_chunk);
1708 phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
1709 while (phdr != NULL) {
1710 /* now we must see if we want the parameter */
1711 ptype = ntohs(phdr->param_type);
1712 plen = ntohs(phdr->param_length);
1716 if (ptype == SCTP_IPV4_ADDRESS &&
1717 plen == sizeof(struct sctp_ipv4addr_param)) {
1718 /* Get the rest of the address */
1719 struct sctp_ipv4addr_param ip4_parm, *p4;
1721 phdr = sctp_get_next_param(m, offset,
1722 (struct sctp_paramhdr *)&ip4_parm, min(plen, sizeof(ip4_parm)));
1726 p4 = (struct sctp_ipv4addr_param *)phdr;
1727 memcpy(&sin4.sin_addr, &p4->addr, sizeof(p4->addr));
1729 retval = sctp_findassociation_ep_addr(inp_p,
1730 (struct sockaddr *)&sin4, netp, dest, NULL);
1731 if (retval != NULL) {
1734 } else if (ptype == SCTP_IPV6_ADDRESS &&
1735 plen == sizeof(struct sctp_ipv6addr_param)) {
1736 /* Get the rest of the address */
1737 struct sctp_ipv6addr_param ip6_parm, *p6;
1739 phdr = sctp_get_next_param(m, offset,
1740 (struct sctp_paramhdr *)&ip6_parm, min(plen, sizeof(ip6_parm)));
1744 p6 = (struct sctp_ipv6addr_param *)phdr;
1745 memcpy(&sin6.sin6_addr, &p6->addr, sizeof(p6->addr));
1747 retval = sctp_findassociation_ep_addr(inp_p,
1748 (struct sockaddr *)&sin6, netp, dest, NULL);
1749 if (retval != NULL) {
1753 offset += SCTP_SIZE32(plen);
1754 phdr = sctp_get_next_param(m, offset, &parm_buf,
1761 static struct sctp_tcb *
1762 sctp_findassoc_by_vtag(struct sockaddr *from, uint32_t vtag,
1763 struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint16_t rport,
1764 uint16_t lport, int skip_src_check)
1767 * Use my vtag to hash. If we find it we then verify the source addr
1768 * is in the assoc. If all goes well we save a bit on rec of a
1771 struct sctpasochead *head;
1772 struct sctp_nets *net;
1773 struct sctp_tcb *stcb;
1777 SCTP_INP_INFO_RLOCK();
1778 head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(vtag,
1779 sctppcbinfo.hashasocmark)];
1782 SCTP_INP_INFO_RUNLOCK();
1785 LIST_FOREACH(stcb, head, sctp_asocs) {
1786 SCTP_INP_RLOCK(stcb->sctp_ep);
1787 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1788 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1791 SCTP_TCB_LOCK(stcb);
1792 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1793 if (stcb->asoc.my_vtag == vtag) {
1795 if (stcb->rport != rport) {
1797 * we could remove this if vtags are unique
1798 * across the system.
1800 SCTP_TCB_UNLOCK(stcb);
1803 if (stcb->sctp_ep->sctp_lport != lport) {
1805 * we could remove this if vtags are unique
1806 * across the system.
1808 SCTP_TCB_UNLOCK(stcb);
1811 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
1812 SCTP_TCB_UNLOCK(stcb);
1815 if (skip_src_check) {
1816 *netp = NULL; /* unknown */
1818 *inp_p = stcb->sctp_ep;
1819 SCTP_INP_INFO_RUNLOCK();
1822 net = sctp_findnet(stcb, from);
1826 SCTP_STAT_INCR(sctps_vtagexpress);
1827 *inp_p = stcb->sctp_ep;
1828 SCTP_INP_INFO_RUNLOCK();
1832 * not him, this should only happen in rare
1833 * cases so I peg it.
1835 SCTP_STAT_INCR(sctps_vtagbogus);
1838 SCTP_TCB_UNLOCK(stcb);
1840 SCTP_INP_INFO_RUNLOCK();
1845 * Find an association with the pointer to the inbound IP packet. This can be
1846 * a IPv4 or IPv6 packet.
1849 sctp_findassociation_addr(struct mbuf *m, int iphlen, int offset,
1850 struct sctphdr *sh, struct sctp_chunkhdr *ch,
1851 struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id)
1855 struct sctp_tcb *retval;
1856 struct sockaddr_storage to_store, from_store;
1857 struct sockaddr *to = (struct sockaddr *)&to_store;
1858 struct sockaddr *from = (struct sockaddr *)&from_store;
1859 struct sctp_inpcb *inp;
1861 iph = mtod(m, struct ip *);
1862 switch (iph->ip_v) {
1866 struct sockaddr_in *from4;
1868 from4 = (struct sockaddr_in *)&from_store;
1869 bzero(from4, sizeof(*from4));
1870 from4->sin_family = AF_INET;
1871 from4->sin_len = sizeof(struct sockaddr_in);
1872 from4->sin_addr.s_addr = iph->ip_src.s_addr;
1873 from4->sin_port = sh->src_port;
1877 case IPV6_VERSION >> 4:
1880 struct ip6_hdr *ip6;
1881 struct sockaddr_in6 *from6;
1883 ip6 = mtod(m, struct ip6_hdr *);
1884 from6 = (struct sockaddr_in6 *)&from_store;
1885 bzero(from6, sizeof(*from6));
1886 from6->sin6_family = AF_INET6;
1887 from6->sin6_len = sizeof(struct sockaddr_in6);
1888 from6->sin6_addr = ip6->ip6_src;
1889 from6->sin6_port = sh->src_port;
1890 /* Get the scopes in properly to the sin6 addr's */
1891 /* we probably don't need these operations */
1892 (void)sa6_recoverscope(from6);
1893 sa6_embedscope(from6, ip6_use_defzone);
1898 /* Currently not supported. */
1902 /* we only go down this path if vtag is non-zero */
1903 retval = sctp_findassoc_by_vtag(from, ntohl(sh->v_tag),
1904 inp_p, netp, sh->src_port, sh->dest_port, 0);
1909 switch (iph->ip_v) {
1913 struct sockaddr_in *to4;
1915 to4 = (struct sockaddr_in *)&to_store;
1916 bzero(to4, sizeof(*to4));
1917 to4->sin_family = AF_INET;
1918 to4->sin_len = sizeof(struct sockaddr_in);
1919 to4->sin_addr.s_addr = iph->ip_dst.s_addr;
1920 to4->sin_port = sh->dest_port;
1924 case IPV6_VERSION >> 4:
1927 struct ip6_hdr *ip6;
1928 struct sockaddr_in6 *to6;
1930 ip6 = mtod(m, struct ip6_hdr *);
1931 to6 = (struct sockaddr_in6 *)&to_store;
1932 bzero(to6, sizeof(*to6));
1933 to6->sin6_family = AF_INET6;
1934 to6->sin6_len = sizeof(struct sockaddr_in6);
1935 to6->sin6_addr = ip6->ip6_dst;
1936 to6->sin6_port = sh->dest_port;
1937 /* Get the scopes in properly to the sin6 addr's */
1938 /* we probably don't need these operations */
1939 (void)sa6_recoverscope(to6);
1940 sa6_embedscope(to6, ip6_use_defzone);
1949 if ((ch->chunk_type != SCTP_INITIATION) &&
1950 (ch->chunk_type != SCTP_INITIATION_ACK) &&
1951 (ch->chunk_type != SCTP_COOKIE_ACK) &&
1952 (ch->chunk_type != SCTP_COOKIE_ECHO)) {
1953 /* Other chunk types go to the tcp pool. */
1957 retval = sctp_findassociation_addr_sa(to, from, inp_p, netp,
1958 find_tcp_pool, vrf_id);
1961 retval = sctp_findassociation_addr_sa(to, from, &inp, netp,
1962 find_tcp_pool, vrf_id);
1964 SCTPDBG(SCTP_DEBUG_PCB1, "retval:%p inp:%p\n", retval, inp);
1965 if (retval == NULL && inp) {
1966 /* Found a EP but not this address */
1967 if ((ch->chunk_type == SCTP_INITIATION) ||
1968 (ch->chunk_type == SCTP_INITIATION_ACK)) {
1970 * special hook, we do NOT return linp or an
1971 * association that is linked to an existing
1972 * association that is under the TCP pool (i.e. no
1973 * listener exists). The endpoint finding routine
1974 * will always find a listner before examining the
1977 if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
1983 retval = sctp_findassociation_special_addr(m, iphlen,
1984 offset, sh, &inp, netp, to);
1985 if (inp_p != NULL) {
1990 SCTPDBG(SCTP_DEBUG_PCB1, "retval is %p\n", retval);
1995 * lookup an association by an ASCONF lookup address.
1996 * if the lookup address is 0.0.0.0 or ::0, use the vtag to do the lookup
1999 sctp_findassociation_ep_asconf(struct mbuf *m, int iphlen, int offset,
2000 struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp)
2002 struct sctp_tcb *stcb;
2003 struct sockaddr_in *sin;
2006 struct sockaddr_in6 *sin6;
2009 struct sockaddr_storage local_store, remote_store;
2013 struct ip6_hdr *ip6;
2016 struct sctp_paramhdr parm_buf, *phdr;
2018 int zero_address = 0;
2021 memset(&local_store, 0, sizeof(local_store));
2022 memset(&remote_store, 0, sizeof(remote_store));
2024 /* First get the destination address setup too. */
2025 iph = mtod(m, struct ip *);
2026 switch (iph->ip_v) {
2029 sin = (struct sockaddr_in *)&local_store;
2030 sin->sin_family = AF_INET;
2031 sin->sin_len = sizeof(*sin);
2032 sin->sin_port = sh->dest_port;
2033 sin->sin_addr.s_addr = iph->ip_dst.s_addr;
2036 case IPV6_VERSION >> 4:
2038 ip6 = mtod(m, struct ip6_hdr *);
2039 sin6 = (struct sockaddr_in6 *)&local_store;
2040 sin6->sin6_family = AF_INET6;
2041 sin6->sin6_len = sizeof(*sin6);
2042 sin6->sin6_port = sh->dest_port;
2043 sin6->sin6_addr = ip6->ip6_dst;
2050 phdr = sctp_get_next_param(m, offset + sizeof(struct sctp_asconf_chunk),
2051 &parm_buf, sizeof(struct sctp_paramhdr));
2053 SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf lookup addr\n",
2057 ptype = (int)((uint32_t) ntohs(phdr->param_type));
2058 /* get the correlation address */
2061 case SCTP_IPV6_ADDRESS:
2063 /* ipv6 address param */
2064 struct sctp_ipv6addr_param *p6, p6_buf;
2066 if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv6addr_param)) {
2069 p6 = (struct sctp_ipv6addr_param *)sctp_get_next_param(m,
2070 offset + sizeof(struct sctp_asconf_chunk),
2071 &p6_buf.ph, sizeof(*p6));
2073 SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v6 lookup addr\n",
2077 sin6 = (struct sockaddr_in6 *)&remote_store;
2078 sin6->sin6_family = AF_INET6;
2079 sin6->sin6_len = sizeof(*sin6);
2080 sin6->sin6_port = sh->src_port;
2081 memcpy(&sin6->sin6_addr, &p6->addr, sizeof(struct in6_addr));
2082 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
2087 case SCTP_IPV4_ADDRESS:
2089 /* ipv4 address param */
2090 struct sctp_ipv4addr_param *p4, p4_buf;
2092 if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv4addr_param)) {
2095 p4 = (struct sctp_ipv4addr_param *)sctp_get_next_param(m,
2096 offset + sizeof(struct sctp_asconf_chunk),
2097 &p4_buf.ph, sizeof(*p4));
2099 SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v4 lookup addr\n",
2103 sin = (struct sockaddr_in *)&remote_store;
2104 sin->sin_family = AF_INET;
2105 sin->sin_len = sizeof(*sin);
2106 sin->sin_port = sh->src_port;
2107 memcpy(&sin->sin_addr, &p4->addr, sizeof(struct in_addr));
2108 if (sin->sin_addr.s_addr == INADDR_ANY)
2113 /* invalid address param type */
2118 stcb = sctp_findassoc_by_vtag(NULL, ntohl(sh->v_tag), inp_p,
2119 netp, sh->src_port, sh->dest_port, 1);
2121 * printf("findassociation_ep_asconf: zero lookup address
2122 * finds stcb 0x%x\n", (uint32_t)stcb);
2125 stcb = sctp_findassociation_ep_addr(inp_p,
2126 (struct sockaddr *)&remote_store, netp,
2127 (struct sockaddr *)&local_store, NULL);
2134 * allocate a sctp_inpcb and setup a temporary binding to a port/all
2135 * addresses. This way if we don't get a bind we by default pick a ephemeral
2136 * port with all addresses bound.
2139 sctp_inpcb_alloc(struct socket *so, uint32_t vrf_id)
2142 * we get called when a new endpoint starts up. We need to allocate
2143 * the sctp_inpcb structure from the zone and init it. Mark it as
2144 * unbound and find a port that we can use as an ephemeral with
2145 * INADDR_ANY. If the user binds later no problem we can then add in
2146 * the specific addresses. And setup the default parameters for the
2150 struct sctp_inpcb *inp;
2152 struct timeval time;
2153 sctp_sharedkey_t *null_key;
2157 SCTP_INP_INFO_WLOCK();
2158 inp = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_ep, struct sctp_inpcb);
2160 SCTP_PRINTF("Out of SCTP-INPCB structures - no resources\n");
2161 SCTP_INP_INFO_WUNLOCK();
2162 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
2166 bzero(inp, sizeof(*inp));
2168 /* bump generations */
2169 /* setup socket pointers */
2170 inp->sctp_socket = so;
2171 inp->ip_inp.inp.inp_socket = so;
2173 inp->partial_delivery_point = SCTP_SB_LIMIT_RCV(so) >> SCTP_PARTIAL_DELIVERY_SHIFT;
2174 inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
2178 struct inpcbpolicy *pcb_sp = NULL;
2180 error = ipsec_init_policy(so, &pcb_sp);
2181 /* Arrange to share the policy */
2182 inp->ip_inp.inp.inp_sp = pcb_sp;
2183 ((struct in6pcb *)(&inp->ip_inp.inp))->in6p_sp = pcb_sp;
2186 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
2187 SCTP_INP_INFO_WUNLOCK();
2191 SCTP_INCR_EP_COUNT();
2192 inp->ip_inp.inp.inp_ip_ttl = ip_defttl;
2193 SCTP_INP_INFO_WUNLOCK();
2195 so->so_pcb = (caddr_t)inp;
2197 if ((SCTP_SO_TYPE(so) == SOCK_DGRAM) ||
2198 (SCTP_SO_TYPE(so) == SOCK_SEQPACKET)) {
2199 /* UDP style socket */
2200 inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
2201 SCTP_PCB_FLAGS_UNBOUND);
2202 /* Be sure it is NON-BLOCKING IO for UDP */
2203 /* SCTP_SET_SO_NBIO(so); */
2204 } else if (SCTP_SO_TYPE(so) == SOCK_STREAM) {
2205 /* TCP style socket */
2206 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2207 SCTP_PCB_FLAGS_UNBOUND);
2208 /* Be sure we have blocking IO by default */
2209 SCTP_CLEAR_SO_NBIO(so);
2212 * unsupported socket type (RAW, etc)- in case we missed it
2215 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EOPNOTSUPP);
2216 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
2217 return (EOPNOTSUPP);
2219 if (sctp_default_frag_interleave == SCTP_FRAG_LEVEL_1) {
2220 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2221 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2222 } else if (sctp_default_frag_interleave == SCTP_FRAG_LEVEL_2) {
2223 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2224 sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2225 } else if (sctp_default_frag_interleave == SCTP_FRAG_LEVEL_0) {
2226 sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2227 sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2229 inp->sctp_tcbhash = SCTP_HASH_INIT(sctp_pcbtblsize,
2230 &inp->sctp_hashmark);
2231 if (inp->sctp_tcbhash == NULL) {
2232 SCTP_PRINTF("Out of SCTP-INPCB->hashinit - no resources\n");
2233 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
2234 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
2237 inp->def_vrf_id = vrf_id;
2239 SCTP_INP_INFO_WLOCK();
2240 SCTP_INP_LOCK_INIT(inp);
2241 INP_LOCK_INIT(&inp->ip_inp.inp, "inp", "sctpinp");
2242 SCTP_INP_READ_INIT(inp);
2243 SCTP_ASOC_CREATE_LOCK_INIT(inp);
2244 /* lock the new ep */
2245 SCTP_INP_WLOCK(inp);
2247 /* add it to the info area */
2248 LIST_INSERT_HEAD(&sctppcbinfo.listhead, inp, sctp_list);
2249 SCTP_INP_INFO_WUNLOCK();
2251 TAILQ_INIT(&inp->read_queue);
2252 LIST_INIT(&inp->sctp_addr_list);
2254 LIST_INIT(&inp->sctp_asoc_list);
2256 #ifdef SCTP_TRACK_FREED_ASOCS
2258 LIST_INIT(&inp->sctp_asoc_free_list);
2260 /* Init the timer structure for signature change */
2261 SCTP_OS_TIMER_INIT(&inp->sctp_ep.signature_change.timer);
2262 inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NEWCOOKIE;
2264 /* now init the actual endpoint default data */
2267 /* setup the base timeout information */
2268 m->sctp_timeoutticks[SCTP_TIMER_SEND] = SEC_TO_TICKS(SCTP_SEND_SEC); /* needed ? */
2269 m->sctp_timeoutticks[SCTP_TIMER_INIT] = SEC_TO_TICKS(SCTP_INIT_SEC); /* needed ? */
2270 m->sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sctp_delayed_sack_time_default);
2271 m->sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(sctp_heartbeat_interval_default);
2272 m->sctp_timeoutticks[SCTP_TIMER_PMTU] = SEC_TO_TICKS(sctp_pmtu_raise_time_default);
2273 m->sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] = SEC_TO_TICKS(sctp_shutdown_guard_time_default);
2274 m->sctp_timeoutticks[SCTP_TIMER_SIGNATURE] = SEC_TO_TICKS(sctp_secret_lifetime_default);
2275 /* all max/min max are in ms */
2276 m->sctp_maxrto = sctp_rto_max_default;
2277 m->sctp_minrto = sctp_rto_min_default;
2278 m->initial_rto = sctp_rto_initial_default;
2279 m->initial_init_rto_max = sctp_init_rto_max_default;
2280 m->sctp_sack_freq = sctp_sack_freq_default;
2282 m->max_open_streams_intome = MAX_SCTP_STREAMS;
2284 m->max_init_times = sctp_init_rtx_max_default;
2285 m->max_send_times = sctp_assoc_rtx_max_default;
2286 m->def_net_failure = sctp_path_rtx_max_default;
2287 m->sctp_sws_sender = SCTP_SWS_SENDER_DEF;
2288 m->sctp_sws_receiver = SCTP_SWS_RECEIVER_DEF;
2289 m->max_burst = sctp_max_burst_default;
2290 if ((sctp_default_cc_module >= SCTP_CC_RFC2581) &&
2291 (sctp_default_cc_module <= SCTP_CC_HTCP)) {
2292 m->sctp_default_cc_module = sctp_default_cc_module;
2294 /* sysctl done with invalid value, set to 2581 */
2295 m->sctp_default_cc_module = SCTP_CC_RFC2581;
2297 /* number of streams to pre-open on a association */
2298 m->pre_open_stream_count = sctp_nr_outgoing_streams_default;
2300 /* Add adaptation cookie */
2301 m->adaptation_layer_indicator = 0x504C5253;
2303 /* seed random number generator */
2304 m->random_counter = 1;
2305 m->store_at = SCTP_SIGNATURE_SIZE;
2306 SCTP_READ_RANDOM(m->random_numbers, sizeof(m->random_numbers));
2307 sctp_fill_random_store(m);
2309 /* Minimum cookie size */
2310 m->size_of_a_cookie = (sizeof(struct sctp_init_msg) * 2) +
2311 sizeof(struct sctp_state_cookie);
2312 m->size_of_a_cookie += SCTP_SIGNATURE_SIZE;
2314 /* Setup the initial secret */
2315 (void)SCTP_GETTIME_TIMEVAL(&time);
2316 m->time_of_secret_change = time.tv_sec;
2318 for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
2319 m->secret_key[0][i] = sctp_select_initial_TSN(m);
2321 sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL);
2323 /* How long is a cookie good for ? */
2324 m->def_cookie_life = MSEC_TO_TICKS(sctp_valid_cookie_life_default);
2326 * Initialize authentication parameters
2328 m->local_hmacs = sctp_default_supported_hmaclist();
2329 m->local_auth_chunks = sctp_alloc_chunklist();
2330 sctp_auth_set_default_chunks(m->local_auth_chunks);
2331 LIST_INIT(&m->shared_keys);
2332 /* add default NULL key as key id 0 */
2333 null_key = sctp_alloc_sharedkey();
2334 sctp_insert_sharedkey(&m->shared_keys, null_key);
2335 SCTP_INP_WUNLOCK(inp);
2336 #ifdef SCTP_LOG_CLOSING
2337 sctp_log_closing(inp, NULL, 12);
2344 sctp_move_pcb_and_assoc(struct sctp_inpcb *old_inp, struct sctp_inpcb *new_inp,
2345 struct sctp_tcb *stcb)
2347 struct sctp_nets *net;
2348 uint16_t lport, rport;
2349 struct sctppcbhead *head;
2350 struct sctp_laddr *laddr, *oladdr;
2352 atomic_add_int(&stcb->asoc.refcnt, 1);
2353 SCTP_TCB_UNLOCK(stcb);
2354 SCTP_INP_INFO_WLOCK();
2355 SCTP_INP_WLOCK(old_inp);
2356 SCTP_INP_WLOCK(new_inp);
2357 SCTP_TCB_LOCK(stcb);
2358 atomic_subtract_int(&stcb->asoc.refcnt, 1);
2360 new_inp->sctp_ep.time_of_secret_change =
2361 old_inp->sctp_ep.time_of_secret_change;
2362 memcpy(new_inp->sctp_ep.secret_key, old_inp->sctp_ep.secret_key,
2363 sizeof(old_inp->sctp_ep.secret_key));
2364 new_inp->sctp_ep.current_secret_number =
2365 old_inp->sctp_ep.current_secret_number;
2366 new_inp->sctp_ep.last_secret_number =
2367 old_inp->sctp_ep.last_secret_number;
2368 new_inp->sctp_ep.size_of_a_cookie = old_inp->sctp_ep.size_of_a_cookie;
2370 /* make it so new data pours into the new socket */
2371 stcb->sctp_socket = new_inp->sctp_socket;
2372 stcb->sctp_ep = new_inp;
2374 /* Copy the port across */
2375 lport = new_inp->sctp_lport = old_inp->sctp_lport;
2376 rport = stcb->rport;
2377 /* Pull the tcb from the old association */
2378 LIST_REMOVE(stcb, sctp_tcbhash);
2379 LIST_REMOVE(stcb, sctp_tcblist);
2381 /* Now insert the new_inp into the TCP connected hash */
2382 head = &sctppcbinfo.sctp_tcpephash[SCTP_PCBHASH_ALLADDR((lport + rport),
2383 sctppcbinfo.hashtcpmark)];
2385 LIST_INSERT_HEAD(head, new_inp, sctp_hash);
2386 /* Its safe to access */
2387 new_inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
2389 /* Now move the tcb into the endpoint list */
2390 LIST_INSERT_HEAD(&new_inp->sctp_asoc_list, stcb, sctp_tcblist);
2392 * Question, do we even need to worry about the ep-hash since we
2393 * only have one connection? Probably not :> so lets get rid of it
2394 * and not suck up any kernel memory in that.
2397 /* Ok. Let's restart timer. */
2398 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2399 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, new_inp,
2403 SCTP_INP_INFO_WUNLOCK();
2404 if (new_inp->sctp_tcbhash != NULL) {
2405 SCTP_HASH_FREE(new_inp->sctp_tcbhash, new_inp->sctp_hashmark);
2406 new_inp->sctp_tcbhash = NULL;
2408 if ((new_inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
2409 /* Subset bound, so copy in the laddr list from the old_inp */
2410 LIST_FOREACH(oladdr, &old_inp->sctp_addr_list, sctp_nxt_addr) {
2411 laddr = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_laddr, struct sctp_laddr);
2412 if (laddr == NULL) {
2414 * Gak, what can we do? This assoc is really
2415 * HOSED. We probably should send an abort
2418 SCTPDBG(SCTP_DEBUG_PCB1, "Association hosed in TCP model, out of laddr memory\n");
2421 SCTP_INCR_LADDR_COUNT();
2422 bzero(laddr, sizeof(*laddr));
2423 (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time);
2424 laddr->ifa = oladdr->ifa;
2425 atomic_add_int(&laddr->ifa->refcount, 1);
2426 LIST_INSERT_HEAD(&new_inp->sctp_addr_list, laddr,
2428 new_inp->laddr_count++;
2432 * Now any running timers need to be adjusted since we really don't
2433 * care if they are running or not just blast in the new_inp into
2437 stcb->asoc.hb_timer.ep = (void *)new_inp;
2438 stcb->asoc.dack_timer.ep = (void *)new_inp;
2439 stcb->asoc.asconf_timer.ep = (void *)new_inp;
2440 stcb->asoc.strreset_timer.ep = (void *)new_inp;
2441 stcb->asoc.shut_guard_timer.ep = (void *)new_inp;
2442 stcb->asoc.autoclose_timer.ep = (void *)new_inp;
2443 stcb->asoc.delayed_event_timer.ep = (void *)new_inp;
2444 stcb->asoc.delete_prim_timer.ep = (void *)new_inp;
2445 /* now what about the nets? */
2446 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2447 net->pmtu_timer.ep = (void *)new_inp;
2448 net->rxt_timer.ep = (void *)new_inp;
2449 net->fr_timer.ep = (void *)new_inp;
2451 SCTP_INP_WUNLOCK(new_inp);
2452 SCTP_INP_WUNLOCK(old_inp);
2456 sctp_isport_inuse(struct sctp_inpcb *inp, uint16_t lport, uint32_t vrf_id)
2458 struct sctppcbhead *head;
2459 struct sctp_inpcb *t_inp;
2462 head = &sctppcbinfo.sctp_ephash[SCTP_PCBHASH_ALLADDR(lport,
2463 sctppcbinfo.hashmark)];
2464 LIST_FOREACH(t_inp, head, sctp_hash) {
2465 if (t_inp->sctp_lport != lport) {
2468 /* is it in the VRF in question */
2470 if (t_inp->def_vrf_id == vrf_id)
2475 /* This one is in use. */
2476 /* check the v6/v4 binding issue */
2477 if ((t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
2478 SCTP_IPV6_V6ONLY(t_inp)) {
2479 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2480 /* collision in V6 space */
2483 /* inp is BOUND_V4 no conflict */
2486 } else if (t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2487 /* t_inp is bound v4 and v6, conflict always */
2490 /* t_inp is bound only V4 */
2491 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
2492 SCTP_IPV6_V6ONLY(inp)) {
2496 /* else fall through to conflict */
2505 /* sctp_ifap is used to bypass normal local address validation checks */
2507 sctp_inpcb_bind(struct socket *so, struct sockaddr *addr,
2508 struct sctp_ifa *sctp_ifap, struct thread *p)
2510 /* bind a ep to a socket address */
2511 struct sctppcbhead *head;
2512 struct sctp_inpcb *inp, *inp_tmp;
2513 struct inpcb *ip_inp;
2523 inp = (struct sctp_inpcb *)so->so_pcb;
2524 ip_inp = (struct inpcb *)so->so_pcb;
2527 SCTPDBG(SCTP_DEBUG_PCB1, "Bind called port:%d\n",
2528 ntohs(((struct sockaddr_in *)addr)->sin_port));
2529 SCTPDBG(SCTP_DEBUG_PCB1, "Addr :");
2530 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr);
2533 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
2534 /* already did a bind, subsequent binds NOT allowed ! */
2535 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
2540 panic("null proc/thread");
2542 if (p && jailed(p->td_ucred)) {
2546 switch (addr->sa_family) {
2549 struct sockaddr_in *sin;
2551 /* IPV6_V6ONLY socket? */
2552 if (SCTP_IPV6_V6ONLY(ip_inp)) {
2553 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
2556 if (addr->sa_len != sizeof(*sin)) {
2557 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
2560 sin = (struct sockaddr_in *)addr;
2561 lport = sin->sin_port;
2564 * For INADDR_ANY and LOOPBACK the
2565 * prison_ip() call will transmute
2566 * the ip address to the proper
2567 * value (i.e. the IP address owned
2570 if (prison_ip(p->td_ucred, 0, &sin->sin_addr.s_addr)) {
2571 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
2575 if (sin->sin_addr.s_addr != INADDR_ANY) {
2584 * Only for pure IPv6 Address. (No IPv4
2587 struct sockaddr_in6 *sin6;
2589 sin6 = (struct sockaddr_in6 *)addr;
2591 if (addr->sa_len != sizeof(*sin6)) {
2592 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
2595 lport = sin6->sin6_port;
2597 * Jail checks for IPv6 should go HERE! i.e.
2598 * add the prison_ip() equivilant in this
2599 * postion to transmute the addresses to the
2600 * proper one jailed.
2602 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2604 /* KAME hack: embed scopeid */
2605 if (sa6_embedscope(sin6, ip6_use_defzone) != 0) {
2606 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
2610 /* this must be cleared for ifa_ifwithaddr() */
2611 sin6->sin6_scope_id = 0;
2616 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EAFNOSUPPORT);
2617 return (EAFNOSUPPORT);
2620 SCTP_INP_INFO_WLOCK();
2621 SCTP_INP_WLOCK(inp);
2622 /* Setup a vrf_id to be the default for the non-bind-all case. */
2623 vrf_id = inp->def_vrf_id;
2625 /* increase our count due to the unlock we do */
2626 SCTP_INP_INCR_REF(inp);
2629 * Did the caller specify a port? if so we must see if a ep
2630 * already has this one bound.
2632 /* got to be root to get at low ports */
2633 if (ntohs(lport) < IPPORT_RESERVED) {
2635 priv_check(p, PRIV_NETINET_RESERVEDPORT)
2637 SCTP_INP_DECR_REF(inp);
2638 SCTP_INP_WUNLOCK(inp);
2639 SCTP_INP_INFO_WUNLOCK();
2644 SCTP_INP_DECR_REF(inp);
2645 SCTP_INP_WUNLOCK(inp);
2646 SCTP_INP_INFO_WUNLOCK();
2647 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
2650 SCTP_INP_WUNLOCK(inp);
2652 vrf_id = inp->def_vrf_id;
2653 inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id);
2654 if (inp_tmp != NULL) {
2656 * lock guy returned and lower count note
2657 * that we are not bound so inp_tmp should
2658 * NEVER be inp. And it is this inp
2659 * (inp_tmp) that gets the reference bump,
2660 * so we must lower it.
2662 SCTP_INP_DECR_REF(inp_tmp);
2663 SCTP_INP_DECR_REF(inp);
2665 SCTP_INP_INFO_WUNLOCK();
2666 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE);
2667 return (EADDRINUSE);
2670 inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id);
2671 if (inp_tmp != NULL) {
2673 * lock guy returned and lower count note
2674 * that we are not bound so inp_tmp should
2675 * NEVER be inp. And it is this inp
2676 * (inp_tmp) that gets the reference bump,
2677 * so we must lower it.
2679 SCTP_INP_DECR_REF(inp_tmp);
2680 SCTP_INP_DECR_REF(inp);
2682 SCTP_INP_INFO_WUNLOCK();
2683 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE);
2684 return (EADDRINUSE);
2687 SCTP_INP_WLOCK(inp);
2689 /* verify that no lport is not used by a singleton */
2690 if (sctp_isport_inuse(inp, lport, vrf_id)) {
2691 /* Sorry someone already has this one bound */
2692 SCTP_INP_DECR_REF(inp);
2693 SCTP_INP_WUNLOCK(inp);
2694 SCTP_INP_INFO_WUNLOCK();
2695 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE);
2696 return (EADDRINUSE);
2700 uint16_t first, last, candidate;
2704 if (ip_inp->inp_flags & INP_HIGHPORT) {
2705 first = ipport_hifirstauto;
2706 last = ipport_hilastauto;
2707 } else if (ip_inp->inp_flags & INP_LOWPORT) {
2709 priv_check(p, PRIV_NETINET_RESERVEDPORT)
2711 SCTP_INP_DECR_REF(inp);
2712 SCTP_INP_WUNLOCK(inp);
2713 SCTP_INP_INFO_WUNLOCK();
2714 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
2717 first = ipport_lowfirstauto;
2718 last = ipport_lowlastauto;
2720 first = ipport_firstauto;
2721 last = ipport_lastauto;
2730 count = last - first + 1; /* number of candidates */
2731 candidate = first + sctp_select_initial_TSN(&inp->sctp_ep) % (count);
2735 if (sctp_isport_inuse(inp, htons(candidate), inp->def_vrf_id) == 0) {
2740 SCTP_INP_DECR_REF(inp);
2741 SCTP_INP_WUNLOCK(inp);
2742 SCTP_INP_INFO_WUNLOCK();
2743 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRINUSE);
2744 return (EADDRINUSE);
2746 if (candidate == last)
2749 candidate = candidate + 1;
2752 lport = htons(candidate);
2754 SCTP_INP_DECR_REF(inp);
2755 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE |
2756 SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
2758 * this really should not happen. The guy did a non-blocking
2759 * bind and then did a close at the same time.
2761 SCTP_INP_WUNLOCK(inp);
2762 SCTP_INP_INFO_WUNLOCK();
2763 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
2766 /* ok we look clear to give out this port, so lets setup the binding */
2768 /* binding to all addresses, so just set in the proper flags */
2769 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUNDALL;
2770 /* set the automatic addr changes from kernel flag */
2771 if (sctp_auto_asconf == 0) {
2772 sctp_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF);
2773 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
2775 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
2776 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
2779 * set the automatic mobility_base from kernel flag (by
2782 if (sctp_mobility_base == 0) {
2783 sctp_mobility_feature_off(inp, SCTP_MOBILITY_BASE);
2784 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
2786 sctp_mobility_feature_on(inp, SCTP_MOBILITY_BASE);
2787 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
2790 * set the automatic mobility_fasthandoff from kernel flag
2793 if (sctp_mobility_fasthandoff == 0) {
2794 sctp_mobility_feature_off(inp, SCTP_MOBILITY_FASTHANDOFF);
2795 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
2797 sctp_mobility_feature_on(inp, SCTP_MOBILITY_FASTHANDOFF);
2798 sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
2802 * bind specific, make sure flags is off and add a new
2803 * address structure to the sctp_addr_list inside the ep
2806 * We will need to allocate one and insert it at the head. The
2807 * socketopt call can just insert new addresses in there as
2808 * well. It will also have to do the embed scope kame hack
2809 * too (before adding).
2811 struct sctp_ifa *ifa;
2812 struct sockaddr_storage store_sa;
2814 memset(&store_sa, 0, sizeof(store_sa));
2815 if (addr->sa_family == AF_INET) {
2816 struct sockaddr_in *sin;
2818 sin = (struct sockaddr_in *)&store_sa;
2819 memcpy(sin, addr, sizeof(struct sockaddr_in));
2821 } else if (addr->sa_family == AF_INET6) {
2822 struct sockaddr_in6 *sin6;
2824 sin6 = (struct sockaddr_in6 *)&store_sa;
2825 memcpy(sin6, addr, sizeof(struct sockaddr_in6));
2826 sin6->sin6_port = 0;
2829 * first find the interface with the bound address need to
2830 * zero out the port to find the address! yuck! can't do
2831 * this earlier since need port for sctp_pcb_findep()
2833 if (sctp_ifap != NULL)
2837 * Note for BSD we hit here always other O/S's will
2838 * pass things in via the sctp_ifap argument
2841 ifa = sctp_find_ifa_by_addr((struct sockaddr *)&store_sa,
2842 vrf_id, SCTP_ADDR_NOT_LOCKED);
2845 /* Can't find an interface with that address */
2846 SCTP_INP_WUNLOCK(inp);
2847 SCTP_INP_INFO_WUNLOCK();
2848 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EADDRNOTAVAIL);
2849 return (EADDRNOTAVAIL);
2851 if (addr->sa_family == AF_INET6) {
2852 /* GAK, more FIXME IFA lock? */
2853 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2854 /* Can't bind a non-existent addr. */
2855 SCTP_INP_WUNLOCK(inp);
2856 SCTP_INP_INFO_WUNLOCK();
2857 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
2861 /* we're not bound all */
2862 inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUNDALL;
2863 /* allow bindx() to send ASCONF's for binding changes */
2864 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
2865 /* clear automatic addr changes from kernel flag */
2866 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
2868 /* add this address to the endpoint list */
2869 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, 0);
2871 SCTP_INP_WUNLOCK(inp);
2872 SCTP_INP_INFO_WUNLOCK();
2877 /* find the bucket */
2878 head = &sctppcbinfo.sctp_ephash[SCTP_PCBHASH_ALLADDR(lport,
2879 sctppcbinfo.hashmark)];
2880 /* put it in the bucket */
2881 LIST_INSERT_HEAD(head, inp, sctp_hash);
2882 SCTPDBG(SCTP_DEBUG_PCB1, "Main hash to bind at head:%p, bound port:%d\n",
2883 head, ntohs(lport));
2884 /* set in the port */
2885 inp->sctp_lport = lport;
2887 /* turn off just the unbound flag */
2888 inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
2889 SCTP_INP_WUNLOCK(inp);
2890 SCTP_INP_INFO_WUNLOCK();
2896 sctp_iterator_inp_being_freed(struct sctp_inpcb *inp, struct sctp_inpcb *inp_next)
2898 struct sctp_iterator *it;
2901 * We enter with the only the ITERATOR_LOCK in place and a write
2902 * lock on the inp_info stuff.
2906 * Go through all iterators, we must do this since it is possible
2907 * that some iterator does NOT have the lock, but is waiting for it.
2908 * And the one that had the lock has either moved in the last
2909 * iteration or we just cleared it above. We need to find all of
2910 * those guys. The list of iterators should never be very big
2913 TAILQ_FOREACH(it, &sctppcbinfo.iteratorhead, sctp_nxt_itr) {
2914 if (it == inp->inp_starting_point_for_iterator)
2915 /* skip this guy, he's special */
2917 if (it->inp == inp) {
2919 * This is tricky and we DON'T lock the iterator.
2920 * Reason is he's running but waiting for me since
2921 * inp->inp_starting_point_for_iterator has the lock
2922 * on me (the guy above we skipped). This tells us
2923 * its is not running but waiting for
2924 * inp->inp_starting_point_for_iterator to be
2925 * released by the guy that does have our INP in a
2928 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
2932 /* set him up to do the next guy not me */
2938 it = inp->inp_starting_point_for_iterator;
2940 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
2949 /* release sctp_inpcb unbind the port */
2951 sctp_inpcb_free(struct sctp_inpcb *inp, int immediate, int from)
2954 * Here we free a endpoint. We must find it (if it is in the Hash
2955 * table) and remove it from there. Then we must also find it in the
2956 * overall list and remove it from there. After all removals are
2957 * complete then any timer has to be stopped. Then start the actual
2958 * freeing. a) Any local lists. b) Any associations. c) The hash of
2959 * all associations. d) finally the ep itself.
2962 struct sctp_inpcb *inp_save;
2963 struct sctp_tcb *asoc, *nasoc;
2964 struct sctp_laddr *laddr, *nladdr;
2965 struct inpcb *ip_pcb;
2968 struct sctp_queued_to_read *sq;
2972 sctp_sharedkey_t *shared_key;
2975 #ifdef SCTP_LOG_CLOSING
2976 sctp_log_closing(inp, NULL, 0);
2978 SCTP_ITERATOR_LOCK();
2979 so = inp->sctp_socket;
2980 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
2981 /* been here before.. eeks.. get out of here */
2982 SCTP_PRINTF("This conflict in free SHOULD not be happening! from %d, imm %d\n", from, immediate);
2983 SCTP_ITERATOR_UNLOCK();
2984 #ifdef SCTP_LOG_CLOSING
2985 sctp_log_closing(inp, NULL, 1);
2989 SCTP_ASOC_CREATE_LOCK(inp);
2990 SCTP_INP_INFO_WLOCK();
2992 SCTP_INP_WLOCK(inp);
2993 /* First time through we have the socket lock, after that no more. */
2994 if (from == SCTP_CALLED_AFTER_CMPSET_OFCLOSE) {
2996 * Once we are in we can remove the flag from = 1 is only
2997 * passed from the actual closing routines that are called
2998 * via the sockets layer.
3000 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CLOSE_IP;
3001 /* socket is gone, so no more wakeups allowed */
3002 inp->sctp_flags |= SCTP_PCB_FLAGS_DONT_WAKE;
3003 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
3004 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
3006 sctp_timer_stop(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL,
3007 SCTP_FROM_SCTP_PCB + SCTP_LOC_1);
3010 sctp_m_freem(inp->control);
3011 inp->control = NULL;
3014 sctp_m_freem(inp->pkt);
3018 ip_pcb = &inp->ip_inp.inp; /* we could just cast the main pointer
3019 * here but I will be nice :> (i.e.
3021 if (immediate == SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE) {
3025 for ((asoc = LIST_FIRST(&inp->sctp_asoc_list)); asoc != NULL;
3027 SCTP_TCB_LOCK(asoc);
3028 nasoc = LIST_NEXT(asoc, sctp_tcblist);
3029 if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3030 /* Skip guys being freed */
3031 /* asoc->sctp_socket = NULL; FIXME MT */
3033 SCTP_TCB_UNLOCK(asoc);
3036 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_WAIT) ||
3037 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
3039 * If we have data in queue, we don't want
3040 * to just free since the app may have done,
3041 * send()/close or connect/send/close. And
3042 * it wants the data to get across first.
3044 /* Just abandon things in the front states */
3045 if (sctp_free_assoc(inp, asoc, SCTP_PCBFREE_NOFORCE,
3046 SCTP_FROM_SCTP_PCB + SCTP_LOC_2) == 0) {
3051 /* Disconnect the socket please */
3052 asoc->sctp_socket = NULL;
3053 asoc->asoc.state |= SCTP_STATE_CLOSED_SOCKET;
3054 if ((asoc->asoc.size_on_reasm_queue > 0) ||
3055 (asoc->asoc.control_pdapi) ||
3056 (asoc->asoc.size_on_all_streams > 0) ||
3057 (so && (so->so_rcv.sb_cc > 0))
3059 /* Left with Data unread */
3060 struct mbuf *op_err;
3062 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
3063 0, M_DONTWAIT, 1, MT_DATA);
3065 /* Fill in the user initiated abort */
3066 struct sctp_paramhdr *ph;
3069 SCTP_BUF_LEN(op_err) =
3070 sizeof(struct sctp_paramhdr) + sizeof(uint32_t);
3072 struct sctp_paramhdr *);
3073 ph->param_type = htons(
3074 SCTP_CAUSE_USER_INITIATED_ABT);
3075 ph->param_length = htons(SCTP_BUF_LEN(op_err));
3076 ippp = (uint32_t *) (ph + 1);
3077 *ippp = htonl(SCTP_FROM_SCTP_PCB + SCTP_LOC_3);
3079 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_3;
3080 #if defined(SCTP_PANIC_ON_ABORT)
3081 panic("inpcb_free does an abort");
3083 sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED);
3084 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3085 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
3086 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3087 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3089 if (sctp_free_assoc(inp, asoc,
3090 SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_4) == 0) {
3094 } else if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
3095 TAILQ_EMPTY(&asoc->asoc.sent_queue) &&
3096 (asoc->asoc.stream_queue_cnt == 0)
3098 if (asoc->asoc.locked_on_sending) {
3101 if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
3102 (SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
3104 * there is nothing queued to send,
3105 * so I send shutdown
3107 sctp_send_shutdown(asoc, asoc->asoc.primary_destination);
3108 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
3109 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3110 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3112 SCTP_SET_STATE(&asoc->asoc, SCTP_STATE_SHUTDOWN_SENT);
3113 SCTP_CLEAR_SUBSTATE(&asoc->asoc, SCTP_STATE_SHUTDOWN_PENDING);
3114 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, asoc->sctp_ep, asoc,
3115 asoc->asoc.primary_destination);
3116 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc,
3117 asoc->asoc.primary_destination);
3118 sctp_chunk_output(inp, asoc, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_LOCKED);
3121 /* mark into shutdown pending */
3122 struct sctp_stream_queue_pending *sp;
3124 asoc->asoc.state |= SCTP_STATE_SHUTDOWN_PENDING;
3125 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc,
3126 asoc->asoc.primary_destination);
3127 if (asoc->asoc.locked_on_sending) {
3128 sp = TAILQ_LAST(&((asoc->asoc.locked_on_sending)->outqueue),
3131 SCTP_PRINTF("Error, sp is NULL, locked on sending is %p strm:%d\n",
3132 asoc->asoc.locked_on_sending,
3133 asoc->asoc.locked_on_sending->stream_no);
3135 if ((sp->length == 0) && (sp->msg_is_complete == 0))
3136 asoc->asoc.state |= SCTP_STATE_PARTIAL_MSG_LEFT;
3139 if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
3140 TAILQ_EMPTY(&asoc->asoc.sent_queue) &&
3141 (asoc->asoc.state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
3142 struct mbuf *op_err;
3145 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
3146 0, M_DONTWAIT, 1, MT_DATA);
3152 struct sctp_paramhdr *ph;
3155 SCTP_BUF_LEN(op_err) =
3156 (sizeof(struct sctp_paramhdr) +
3159 struct sctp_paramhdr *);
3160 ph->param_type = htons(
3161 SCTP_CAUSE_USER_INITIATED_ABT);
3162 ph->param_length = htons(SCTP_BUF_LEN(op_err));
3163 ippp = (uint32_t *) (ph + 1);
3164 *ippp = htonl(SCTP_FROM_SCTP_PCB + SCTP_LOC_5);
3166 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_5;
3167 #if defined(SCTP_PANIC_ON_ABORT)
3168 panic("inpcb_free does an abort");
3171 sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED);
3172 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3173 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
3174 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3175 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3177 if (sctp_free_assoc(inp, asoc,
3178 SCTP_PCBFREE_NOFORCE,
3179 SCTP_FROM_SCTP_PCB + SCTP_LOC_6) == 0) {
3184 sctp_chunk_output(inp, asoc, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
3188 SCTP_TCB_UNLOCK(asoc);
3190 /* now is there some left in our SHUTDOWN state? */
3192 SCTP_INP_WUNLOCK(inp);
3193 SCTP_ASOC_CREATE_UNLOCK(inp);
3194 SCTP_INP_INFO_WUNLOCK();
3195 SCTP_ITERATOR_UNLOCK();
3196 #ifdef SCTP_LOG_CLOSING
3197 sctp_log_closing(inp, NULL, 2);
3202 inp->sctp_socket = NULL;
3203 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) !=
3204 SCTP_PCB_FLAGS_UNBOUND) {
3206 * ok, this guy has been bound. It's port is somewhere in
3207 * the sctppcbinfo hash table. Remove it!
3209 LIST_REMOVE(inp, sctp_hash);
3210 inp->sctp_flags |= SCTP_PCB_FLAGS_UNBOUND;
3213 * If there is a timer running to kill us, forget it, since it may
3214 * have a contest on the INP lock.. which would cause us to die ...
3217 for ((asoc = LIST_FIRST(&inp->sctp_asoc_list)); asoc != NULL;
3219 nasoc = LIST_NEXT(asoc, sctp_tcblist);
3220 if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3224 /* Free associations that are NOT killing us */
3225 SCTP_TCB_LOCK(asoc);
3226 if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_COOKIE_WAIT) &&
3227 ((asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0)) {
3228 struct mbuf *op_err;
3231 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
3232 0, M_DONTWAIT, 1, MT_DATA);
3234 /* Fill in the user initiated abort */
3235 struct sctp_paramhdr *ph;
3237 SCTP_BUF_LEN(op_err) = (sizeof(struct sctp_paramhdr) +
3239 ph = mtod(op_err, struct sctp_paramhdr *);
3240 ph->param_type = htons(
3241 SCTP_CAUSE_USER_INITIATED_ABT);
3242 ph->param_length = htons(SCTP_BUF_LEN(op_err));
3243 ippp = (uint32_t *) (ph + 1);
3244 *ippp = htonl(SCTP_FROM_SCTP_PCB + SCTP_LOC_7);
3247 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_7;
3248 #if defined(SCTP_PANIC_ON_ABORT)
3249 panic("inpcb_free does an abort");
3251 sctp_send_abort_tcb(asoc, op_err, SCTP_SO_LOCKED);
3252 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3253 } else if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3255 SCTP_TCB_UNLOCK(asoc);
3258 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
3259 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3260 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3262 if (sctp_free_assoc(inp, asoc, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_8) == 0) {
3267 /* Ok we have someone out there that will kill us */
3268 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
3269 SCTP_INP_WUNLOCK(inp);
3270 SCTP_ASOC_CREATE_UNLOCK(inp);
3271 SCTP_INP_INFO_WUNLOCK();
3272 SCTP_ITERATOR_UNLOCK();
3273 #ifdef SCTP_LOG_CLOSING
3274 sctp_log_closing(inp, NULL, 3);
3278 if ((inp->refcount) || (inp->sctp_flags & SCTP_PCB_FLAGS_CLOSE_IP)) {
3279 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
3280 sctp_timer_start(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL);
3281 SCTP_INP_WUNLOCK(inp);
3282 SCTP_ASOC_CREATE_UNLOCK(inp);
3283 SCTP_INP_INFO_WUNLOCK();
3284 SCTP_ITERATOR_UNLOCK();
3285 #ifdef SCTP_LOG_CLOSING
3286 sctp_log_closing(inp, NULL, 4);
3290 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
3291 inp->sctp_ep.signature_change.type = 0;
3292 inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_ALLGONE;
3294 #ifdef SCTP_LOG_CLOSING
3295 sctp_log_closing(inp, NULL, 5);
3298 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
3299 inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NONE;
3300 /* Clear the read queue */
3301 /* sa_ignore FREED_MEMORY */
3302 while ((sq = TAILQ_FIRST(&inp->read_queue)) != NULL) {
3303 /* Its only abandoned if it had data left */
3305 SCTP_STAT_INCR(sctps_left_abandon);
3307 TAILQ_REMOVE(&inp->read_queue, sq, next);
3308 sctp_free_remote_addr(sq->whoFrom);
3310 so->so_rcv.sb_cc -= sq->length;
3312 sctp_m_freem(sq->data);
3316 * no need to free the net count, since at this point all
3319 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_readq, sq);
3320 SCTP_DECR_READQ_COUNT();
3322 /* Now the sctp_pcb things */
3324 * free each asoc if it is not already closed/free. we can't use the
3325 * macro here since le_next will get freed as part of the
3326 * sctp_free_assoc() call.
3331 ipsec4_delete_pcbpolicy(ip_pcb);
3334 /* Unlocks not needed since the socket is gone now */
3336 if (ip_pcb->inp_options) {
3337 (void)sctp_m_free(ip_pcb->inp_options);
3338 ip_pcb->inp_options = 0;
3340 if (ip_pcb->inp_moptions) {
3341 inp_freemoptions(ip_pcb->inp_moptions);
3342 ip_pcb->inp_moptions = 0;
3345 if (ip_pcb->inp_vflag & INP_IPV6) {
3346 struct in6pcb *in6p;
3348 in6p = (struct in6pcb *)inp;
3349 ip6_freepcbopts(in6p->in6p_outputopts);
3352 ip_pcb->inp_vflag = 0;
3353 /* free up authentication fields */
3354 if (inp->sctp_ep.local_auth_chunks != NULL)
3355 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
3356 if (inp->sctp_ep.local_hmacs != NULL)
3357 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
3359 shared_key = LIST_FIRST(&inp->sctp_ep.shared_keys);
3360 while (shared_key) {
3361 LIST_REMOVE(shared_key, next);
3362 sctp_free_sharedkey(shared_key);
3363 /* sa_ignore FREED_MEMORY */
3364 shared_key = LIST_FIRST(&inp->sctp_ep.shared_keys);
3367 inp_save = LIST_NEXT(inp, sctp_list);
3368 LIST_REMOVE(inp, sctp_list);
3370 /* fix any iterators only after out of the list */
3371 sctp_iterator_inp_being_freed(inp, inp_save);
3373 * if we have an address list the following will free the list of
3374 * ifaddr's that are set into this ep. Again macro limitations here,
3375 * since the LIST_FOREACH could be a bad idea.
3377 for ((laddr = LIST_FIRST(&inp->sctp_addr_list)); laddr != NULL;
3379 nladdr = LIST_NEXT(laddr, sctp_nxt_addr);
3380 sctp_remove_laddr(laddr);
3383 #ifdef SCTP_TRACK_FREED_ASOCS
3385 for ((asoc = LIST_FIRST(&inp->sctp_asoc_free_list)); asoc != NULL;
3387 nasoc = LIST_NEXT(asoc, sctp_tcblist);
3388 LIST_REMOVE(asoc, sctp_tcblist);
3389 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, asoc);
3390 SCTP_DECR_ASOC_COUNT();
3392 /* *** END TEMP CODE *** */
3394 /* Now lets see about freeing the EP hash table. */
3395 if (inp->sctp_tcbhash != NULL) {
3396 SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark);
3397 inp->sctp_tcbhash = NULL;
3399 /* Now we must put the ep memory back into the zone pool */
3400 INP_LOCK_DESTROY(&inp->ip_inp.inp);
3401 SCTP_INP_LOCK_DESTROY(inp);
3402 SCTP_INP_READ_DESTROY(inp);
3403 SCTP_ASOC_CREATE_LOCK_DESTROY(inp);
3404 SCTP_INP_INFO_WUNLOCK();
3405 SCTP_ITERATOR_UNLOCK();
3406 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
3407 SCTP_DECR_EP_COUNT();
3412 sctp_findnet(struct sctp_tcb *stcb, struct sockaddr *addr)
3414 struct sctp_nets *net;
3416 /* locate the address */
3417 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3418 if (sctp_cmpaddr(addr, (struct sockaddr *)&net->ro._l_addr))
3426 sctp_is_address_on_local_host(struct sockaddr *addr, uint32_t vrf_id)
3428 struct sctp_ifa *sctp_ifa;
3430 sctp_ifa = sctp_find_ifa_by_addr(addr, vrf_id, SCTP_ADDR_NOT_LOCKED);
3439 * add's a remote endpoint address, done with the INIT/INIT-ACK as well as
3440 * when a ASCONF arrives that adds it. It will also initialize all the cwnd
3444 sctp_add_remote_addr(struct sctp_tcb *stcb, struct sockaddr *newaddr,
3445 int set_scope, int from)
3448 * The following is redundant to the same lines in the
3449 * sctp_aloc_assoc() but is needed since other's call the add
3452 struct sctp_nets *net, *netfirst;
3455 SCTPDBG(SCTP_DEBUG_PCB1, "Adding an address (from:%d) to the peer: ",
3457 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, newaddr);
3459 netfirst = sctp_findnet(stcb, newaddr);
3462 * Lie and return ok, we don't want to make the association
3463 * go away for this behavior. It will happen in the TCP
3464 * model in a connected socket. It does not reach the hash
3465 * table until after the association is built so it can't be
3466 * found. Mark as reachable, since the initial creation will
3467 * have been cleared and the NOT_IN_ASSOC flag will have
3468 * been added... and we don't want to end up removing it
3471 if (netfirst->dest_state & SCTP_ADDR_UNCONFIRMED) {
3472 netfirst->dest_state = (SCTP_ADDR_REACHABLE |
3473 SCTP_ADDR_UNCONFIRMED);
3475 netfirst->dest_state = SCTP_ADDR_REACHABLE;
3481 if (newaddr->sa_family == AF_INET) {
3482 struct sockaddr_in *sin;
3484 sin = (struct sockaddr_in *)newaddr;
3485 if (sin->sin_addr.s_addr == 0) {
3486 /* Invalid address */
3489 /* zero out the bzero area */
3490 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
3492 /* assure len is set */
3493 sin->sin_len = sizeof(struct sockaddr_in);
3495 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
3496 stcb->ipv4_local_scope = 1;
3498 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
3499 stcb->asoc.ipv4_local_scope = 1;
3501 #endif /* SCTP_DONT_DO_PRIVADDR_SCOPE */
3503 /* Validate the address is in scope */
3504 if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) &&
3505 (stcb->asoc.ipv4_local_scope == 0)) {
3509 } else if (newaddr->sa_family == AF_INET6) {
3510 struct sockaddr_in6 *sin6;
3512 sin6 = (struct sockaddr_in6 *)newaddr;
3513 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3514 /* Invalid address */
3517 /* assure len is set */
3518 sin6->sin6_len = sizeof(struct sockaddr_in6);
3520 if (sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id)) {
3521 stcb->asoc.loopback_scope = 1;
3522 stcb->asoc.local_scope = 0;
3523 stcb->asoc.ipv4_local_scope = 1;
3524 stcb->asoc.site_scope = 1;
3525 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
3527 * If the new destination is a LINK_LOCAL we
3528 * must have common site scope. Don't set
3529 * the local scope since we may not share
3530 * all links, only loopback can do this.
3531 * Links on the local network would also be
3532 * on our private network for v4 too.
3534 stcb->asoc.ipv4_local_scope = 1;
3535 stcb->asoc.site_scope = 1;
3536 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
3538 * If the new destination is SITE_LOCAL then
3539 * we must have site scope in common.
3541 stcb->asoc.site_scope = 1;
3544 /* Validate the address is in scope */
3545 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) &&
3546 (stcb->asoc.loopback_scope == 0)) {
3548 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) &&
3549 (stcb->asoc.local_scope == 0)) {
3551 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) &&
3552 (stcb->asoc.site_scope == 0)) {
3557 /* not supported family type */
3560 net = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_net, struct sctp_nets);
3564 SCTP_INCR_RADDR_COUNT();
3565 bzero(net, sizeof(*net));
3566 (void)SCTP_GETTIME_TIMEVAL(&net->start_time);
3567 memcpy(&net->ro._l_addr, newaddr, newaddr->sa_len);
3568 if (newaddr->sa_family == AF_INET) {
3569 ((struct sockaddr_in *)&net->ro._l_addr)->sin_port = stcb->rport;
3570 } else if (newaddr->sa_family == AF_INET6) {
3571 ((struct sockaddr_in6 *)&net->ro._l_addr)->sin6_port = stcb->rport;
3573 net->addr_is_local = sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id);
3574 if (net->addr_is_local && ((set_scope || (from == SCTP_ADDR_IS_CONFIRMED)))) {
3575 stcb->asoc.loopback_scope = 1;
3576 stcb->asoc.ipv4_local_scope = 1;
3577 stcb->asoc.local_scope = 0;
3578 stcb->asoc.site_scope = 1;
3581 net->failure_threshold = stcb->asoc.def_net_failure;
3582 if (addr_inscope == 0) {
3583 net->dest_state = (SCTP_ADDR_REACHABLE |
3584 SCTP_ADDR_OUT_OF_SCOPE);
3586 if (from == SCTP_ADDR_IS_CONFIRMED)
3587 /* SCTP_ADDR_IS_CONFIRMED is passed by connect_x */
3588 net->dest_state = SCTP_ADDR_REACHABLE;
3590 net->dest_state = SCTP_ADDR_REACHABLE |
3591 SCTP_ADDR_UNCONFIRMED;
3594 * We set this to 0, the timer code knows that this means its an
3598 net->RTO_measured = 0;
3599 stcb->asoc.numnets++;
3600 *(&net->ref_count) = 1;
3601 net->tos_flowlabel = 0;
3603 if (newaddr->sa_family == AF_INET)
3604 net->tos_flowlabel = stcb->asoc.default_tos;
3607 if (newaddr->sa_family == AF_INET6)
3608 net->tos_flowlabel = stcb->asoc.default_flowlabel;
3610 /* Init the timer structure */
3611 SCTP_OS_TIMER_INIT(&net->rxt_timer.timer);
3612 SCTP_OS_TIMER_INIT(&net->fr_timer.timer);
3613 SCTP_OS_TIMER_INIT(&net->pmtu_timer.timer);
3615 /* Now generate a route for this guy */
3617 /* KAME hack: embed scopeid */
3618 if (newaddr->sa_family == AF_INET6) {
3619 struct sockaddr_in6 *sin6;
3621 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3622 (void)sa6_embedscope(sin6, ip6_use_defzone);
3623 sin6->sin6_scope_id = 0;
3626 SCTP_RTALLOC((sctp_route_t *) & net->ro, stcb->asoc.vrf_id);
3629 if (newaddr->sa_family == AF_INET6) {
3630 struct sockaddr_in6 *sin6;
3632 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3633 (void)sa6_recoverscope(sin6);
3636 if (SCTP_ROUTE_HAS_VALID_IFN(&net->ro)) {
3637 /* Get source address */
3638 net->ro._s_addr = sctp_source_address_selection(stcb->sctp_ep,
3640 (sctp_route_t *) & net->ro,
3644 /* Now get the interface MTU */
3645 if (net->ro._s_addr && net->ro._s_addr->ifn_p) {
3646 net->mtu = SCTP_GATHER_MTU_FROM_INTFC(net->ro._s_addr->ifn_p);
3650 #ifdef SCTP_PRINT_FOR_B_AND_M
3651 SCTP_PRINTF("We have found an interface mtu of %d\n", net->mtu);
3653 if (net->mtu == 0) {
3655 net->mtu = SCTP_DEFAULT_MTU;
3659 rmtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, net->ro.ro_rt);
3660 #ifdef SCTP_PRINT_FOR_B_AND_M
3661 SCTP_PRINTF("The route mtu is %d\n", rmtu);
3665 * Start things off to match mtu of
3668 SCTP_SET_MTU_OF_ROUTE(&net->ro._l_addr.sa,
3669 net->ro.ro_rt, net->mtu);
3672 * we take the route mtu over the interface,
3673 * since the route may be leading out the
3674 * loopback, or a different interface.
3679 if (from == SCTP_ALLOC_ASOC) {
3680 #ifdef SCTP_PRINT_FOR_B_AND_M
3681 SCTP_PRINTF("New assoc sets mtu to :%d\n", net->mtu);
3683 stcb->asoc.smallest_mtu = net->mtu;
3686 net->mtu = stcb->asoc.smallest_mtu;
3688 if (stcb->asoc.smallest_mtu > net->mtu) {
3689 #ifdef SCTP_PRINT_FOR_B_AND_M
3690 SCTP_PRINTF("new address mtu:%d smaller than smallest:%d\n",
3691 net->mtu, stcb->asoc.smallest_mtu);
3693 stcb->asoc.smallest_mtu = net->mtu;
3695 /* JRS - Use the congestion control given in the CC module */
3696 stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
3699 * CMT: CUC algo - set find_pseudo_cumack to TRUE (1) at beginning
3700 * of assoc (2005/06/27, iyengar@cis.udel.edu)
3702 net->find_pseudo_cumack = 1;
3703 net->find_rtx_pseudo_cumack = 1;
3704 net->src_addr_selected = 0;
3705 netfirst = TAILQ_FIRST(&stcb->asoc.nets);
3706 if (net->ro.ro_rt == NULL) {
3707 /* Since we have no route put it at the back */
3708 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
3709 } else if (netfirst == NULL) {
3710 /* We are the first one in the pool. */
3711 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
3712 } else if (netfirst->ro.ro_rt == NULL) {
3714 * First one has NO route. Place this one ahead of the first
3717 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
3718 } else if (net->ro.ro_rt->rt_ifp != netfirst->ro.ro_rt->rt_ifp) {
3720 * This one has a different interface than the one at the
3721 * top of the list. Place it ahead.
3723 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
3726 * Ok we have the same interface as the first one. Move
3727 * forward until we find either a) one with a NULL route...
3728 * insert ahead of that b) one with a different ifp.. insert
3729 * after that. c) end of the list.. insert at the tail.
3731 struct sctp_nets *netlook;
3734 netlook = TAILQ_NEXT(netfirst, sctp_next);
3735 if (netlook == NULL) {
3736 /* End of the list */
3737 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
3739 } else if (netlook->ro.ro_rt == NULL) {
3740 /* next one has NO route */
3741 TAILQ_INSERT_BEFORE(netfirst, net, sctp_next);
3743 } else if (netlook->ro.ro_rt->rt_ifp != net->ro.ro_rt->rt_ifp) {
3744 TAILQ_INSERT_AFTER(&stcb->asoc.nets, netlook,
3750 } while (netlook != NULL);
3753 /* got to have a primary set */
3754 if (stcb->asoc.primary_destination == 0) {
3755 stcb->asoc.primary_destination = net;
3756 } else if ((stcb->asoc.primary_destination->ro.ro_rt == NULL) &&
3758 ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) {
3759 /* No route to current primary adopt new primary */
3760 stcb->asoc.primary_destination = net;
3762 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb,
3764 /* Validate primary is first */
3765 net = TAILQ_FIRST(&stcb->asoc.nets);
3766 if ((net != stcb->asoc.primary_destination) &&
3767 (stcb->asoc.primary_destination)) {
3769 * first one on the list is NOT the primary sctp_cmpaddr()
3770 * is much more efficent if the primary is the first on the
3773 TAILQ_REMOVE(&stcb->asoc.nets,
3774 stcb->asoc.primary_destination, sctp_next);
3775 TAILQ_INSERT_HEAD(&stcb->asoc.nets,
3776 stcb->asoc.primary_destination, sctp_next);
3783 * allocate an association and add it to the endpoint. The caller must be
3784 * careful to add all additional addresses once they are know right away or
3785 * else the assoc will be may experience a blackout scenario.
3788 sctp_aloc_assoc(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
3789 int for_a_init, int *error, uint32_t override_tag, uint32_t vrf_id,
3793 /* note the p argument is only valid in unbound sockets */
3795 struct sctp_tcb *stcb;
3796 struct sctp_association *asoc;
3797 struct sctpasochead *head;
3802 * Assumption made here: Caller has done a
3803 * sctp_findassociation_ep_addr(ep, addr's); to make sure the
3804 * address does not exist already.
3806 if (sctppcbinfo.ipi_count_asoc >= SCTP_MAX_NUM_OF_ASOC) {
3807 /* Hit max assoc, sorry no more */
3808 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
3812 if (firstaddr == NULL) {
3813 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3817 SCTP_INP_RLOCK(inp);
3818 if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
3820 * If its in the TCP pool, its NOT allowed to create an
3821 * association. The parent listener needs to call
3822 * sctp_aloc_assoc.. or the one-2-many socket. If a peeled
3823 * off, or connected one does this.. its an error.
3825 SCTP_INP_RUNLOCK(inp);
3826 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3830 SCTPDBG(SCTP_DEBUG_PCB3, "Allocate an association for peer:");
3833 SCTPDBG_ADDR(SCTP_DEBUG_PCB3, firstaddr);
3834 SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
3835 ntohs(((struct sockaddr_in *)firstaddr)->sin_port));
3837 SCTPDBG(SCTP_DEBUG_PCB3, "None\n");
3839 #endif /* SCTP_DEBUG */
3840 if (firstaddr->sa_family == AF_INET) {
3841 struct sockaddr_in *sin;
3843 sin = (struct sockaddr_in *)firstaddr;
3844 if ((sin->sin_port == 0) || (sin->sin_addr.s_addr == 0)) {
3845 /* Invalid address */
3846 SCTP_INP_RUNLOCK(inp);
3847 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3851 rport = sin->sin_port;
3852 } else if (firstaddr->sa_family == AF_INET6) {
3853 struct sockaddr_in6 *sin6;
3855 sin6 = (struct sockaddr_in6 *)firstaddr;
3856 if ((sin6->sin6_port == 0) ||
3857 (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))) {
3858 /* Invalid address */
3859 SCTP_INP_RUNLOCK(inp);
3860 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3864 rport = sin6->sin6_port;
3866 /* not supported family type */
3867 SCTP_INP_RUNLOCK(inp);
3868 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3872 SCTP_INP_RUNLOCK(inp);
3873 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
3875 * If you have not performed a bind, then we need to do the
3876 * ephemerial bind for you.
3878 if ((err = sctp_inpcb_bind(inp->sctp_socket,
3879 (struct sockaddr *)NULL,
3880 (struct sctp_ifa *)NULL,
3883 /* bind error, probably perm */
3888 stcb = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_asoc, struct sctp_tcb);
3890 /* out of memory? */
3891 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
3895 SCTP_INCR_ASOC_COUNT();
3897 bzero(stcb, sizeof(*stcb));
3899 SCTP_TCB_LOCK_INIT(stcb);
3900 SCTP_TCB_SEND_LOCK_INIT(stcb);
3901 /* setup back pointer's */
3902 stcb->sctp_ep = inp;
3903 stcb->sctp_socket = inp->sctp_socket;
3904 if ((err = sctp_init_asoc(inp, stcb, for_a_init, override_tag, vrf_id))) {
3906 SCTP_TCB_LOCK_DESTROY(stcb);
3907 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
3908 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
3909 SCTP_DECR_ASOC_COUNT();
3914 stcb->rport = rport;
3915 SCTP_INP_INFO_WLOCK();
3916 SCTP_INP_WLOCK(inp);
3917 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
3918 /* inpcb freed while alloc going on */
3919 SCTP_TCB_LOCK_DESTROY(stcb);
3920 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
3921 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
3922 SCTP_INP_WUNLOCK(inp);
3923 SCTP_INP_INFO_WUNLOCK();
3924 SCTP_DECR_ASOC_COUNT();
3925 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3929 SCTP_TCB_LOCK(stcb);
3931 /* now that my_vtag is set, add it to the hash */
3932 head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
3933 sctppcbinfo.hashasocmark)];
3934 /* put it in the bucket in the vtag hash of assoc's for the system */
3935 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
3936 sctp_delete_from_timewait(stcb->asoc.my_vtag);
3938 SCTP_INP_INFO_WUNLOCK();
3940 if ((err = sctp_add_remote_addr(stcb, firstaddr, SCTP_DO_SETSCOPE, SCTP_ALLOC_ASOC))) {
3941 /* failure.. memory error? */
3942 if (asoc->strmout) {
3943 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
3944 asoc->strmout = NULL;
3946 if (asoc->mapping_array) {
3947 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
3948 asoc->mapping_array = NULL;
3950 SCTP_DECR_ASOC_COUNT();
3951 SCTP_TCB_LOCK_DESTROY(stcb);
3952 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
3953 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
3954 SCTP_INP_WUNLOCK(inp);
3955 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
3959 /* Init all the timers */
3960 SCTP_OS_TIMER_INIT(&asoc->hb_timer.timer);
3961 SCTP_OS_TIMER_INIT(&asoc->dack_timer.timer);
3962 SCTP_OS_TIMER_INIT(&asoc->strreset_timer.timer);
3963 SCTP_OS_TIMER_INIT(&asoc->asconf_timer.timer);
3964 SCTP_OS_TIMER_INIT(&asoc->shut_guard_timer.timer);
3965 SCTP_OS_TIMER_INIT(&asoc->autoclose_timer.timer);
3966 SCTP_OS_TIMER_INIT(&asoc->delayed_event_timer.timer);
3967 SCTP_OS_TIMER_INIT(&asoc->delete_prim_timer.timer);
3969 LIST_INSERT_HEAD(&inp->sctp_asoc_list, stcb, sctp_tcblist);
3970 /* now file the port under the hash as well */
3971 if (inp->sctp_tcbhash != NULL) {
3972 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(stcb->rport,
3973 inp->sctp_hashmark)];
3974 LIST_INSERT_HEAD(head, stcb, sctp_tcbhash);
3976 SCTP_INP_WUNLOCK(inp);
3977 SCTPDBG(SCTP_DEBUG_PCB1, "Association %p now allocated\n", stcb);
3983 sctp_remove_net(struct sctp_tcb *stcb, struct sctp_nets *net)
3985 struct sctp_association *asoc;
3989 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
3990 if (net == asoc->primary_destination) {
3992 struct sctp_nets *lnet;
3994 lnet = TAILQ_FIRST(&asoc->nets);
3996 * Mobility adaptation Ideally, if deleted destination is
3997 * the primary, it becomes a fast retransmission trigger by
3998 * the subsequent SET PRIMARY. (by micchie)
4000 if (sctp_is_mobility_feature_on(stcb->sctp_ep,
4001 SCTP_MOBILITY_BASE) ||
4002 sctp_is_mobility_feature_on(stcb->sctp_ep,
4003 SCTP_MOBILITY_FASTHANDOFF)) {
4004 SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: primary dst is deleting\n");
4005 if (asoc->deleted_primary != NULL) {
4006 SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: deleted primary may be already stored\n");
4009 asoc->deleted_primary = net;
4010 atomic_add_int(&net->ref_count, 1);
4011 memset(&net->lastsa, 0, sizeof(net->lastsa));
4012 memset(&net->lastsv, 0, sizeof(net->lastsv));
4013 sctp_mobility_feature_on(stcb->sctp_ep,
4014 SCTP_MOBILITY_PRIM_DELETED);
4015 sctp_timer_start(SCTP_TIMER_TYPE_PRIM_DELETED,
4016 stcb->sctp_ep, stcb, NULL);
4019 /* Try to find a confirmed primary */
4020 asoc->primary_destination = sctp_find_alternate_net(stcb, lnet, 0);
4022 if (net == asoc->last_data_chunk_from) {
4024 asoc->last_data_chunk_from = TAILQ_FIRST(&asoc->nets);
4026 if (net == asoc->last_control_chunk_from) {
4028 asoc->last_control_chunk_from = NULL;
4030 sctp_free_remote_addr(net);
4034 * remove a remote endpoint address from an association, it will fail if the
4035 * address does not exist.
4038 sctp_del_remote_addr(struct sctp_tcb *stcb, struct sockaddr *remaddr)
4041 * Here we need to remove a remote address. This is quite simple, we
4042 * first find it in the list of address for the association
4043 * (tasoc->asoc.nets) and then if it is there, we do a LIST_REMOVE
4044 * on that item. Note we do not allow it to be removed if there are
4045 * no other addresses.
4047 struct sctp_association *asoc;
4048 struct sctp_nets *net, *net_tmp;
4052 /* locate the address */
4053 for (net = TAILQ_FIRST(&asoc->nets); net != NULL; net = net_tmp) {
4054 net_tmp = TAILQ_NEXT(net, sctp_next);
4055 if (net->ro._l_addr.sa.sa_family != remaddr->sa_family) {
4058 if (sctp_cmpaddr((struct sockaddr *)&net->ro._l_addr,
4060 /* we found the guy */
4061 if (asoc->numnets < 2) {
4062 /* Must have at LEAST two remote addresses */
4065 sctp_remove_net(stcb, net);
4075 sctp_delete_from_timewait(uint32_t tag)
4077 struct sctpvtaghead *chain;
4078 struct sctp_tagblock *twait_block;
4082 chain = &sctppcbinfo.vtag_timewait[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
4083 if (!SCTP_LIST_EMPTY(chain)) {
4084 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
4085 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
4086 if (twait_block->vtag_block[i].v_tag == tag) {
4087 twait_block->vtag_block[i].tv_sec_at_expire = 0;
4088 twait_block->vtag_block[i].v_tag = 0;
4100 sctp_is_in_timewait(uint32_t tag)
4102 struct sctpvtaghead *chain;
4103 struct sctp_tagblock *twait_block;
4107 chain = &sctppcbinfo.vtag_timewait[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
4108 if (!SCTP_LIST_EMPTY(chain)) {
4109 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
4110 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
4111 if (twait_block->vtag_block[i].v_tag == tag) {
4125 sctp_add_vtag_to_timewait(uint32_t tag, uint32_t time)
4127 struct sctpvtaghead *chain;
4128 struct sctp_tagblock *twait_block;
4132 (void)SCTP_GETTIME_TIMEVAL(&now);
4133 chain = &sctppcbinfo.vtag_timewait[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
4135 if (!SCTP_LIST_EMPTY(chain)) {
4136 /* Block(s) present, lets find space, and expire on the fly */
4137 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
4138 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
4139 if ((twait_block->vtag_block[i].v_tag == 0) &&
4141 twait_block->vtag_block[i].tv_sec_at_expire =
4143 twait_block->vtag_block[i].v_tag = tag;
4145 } else if ((twait_block->vtag_block[i].v_tag) &&
4146 ((long)twait_block->vtag_block[i].tv_sec_at_expire < now.tv_sec)) {
4147 /* Audit expires this guy */
4148 twait_block->vtag_block[i].tv_sec_at_expire = 0;
4149 twait_block->vtag_block[i].v_tag = 0;
4151 /* Reuse it for my new tag */
4152 twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec + time;
4153 twait_block->vtag_block[0].v_tag = tag;
4160 * We only do up to the block where we can
4161 * place our tag for audits
4167 /* Need to add a new block to chain */
4169 SCTP_MALLOC(twait_block, struct sctp_tagblock *,
4170 sizeof(struct sctp_tagblock), SCTP_M_TIMW);
4171 if (twait_block == NULL) {
4174 memset(twait_block, 0, sizeof(struct sctp_tagblock));
4175 LIST_INSERT_HEAD(chain, twait_block, sctp_nxt_tagblock);
4176 twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec + time;
4177 twait_block->vtag_block[0].v_tag = tag;
4183 sctp_iterator_asoc_being_freed(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
4185 struct sctp_iterator *it;
4188 * Unlock the tcb lock we do this so we avoid a dead lock scenario
4189 * where the iterator is waiting on the TCB lock and the TCB lock is
4190 * waiting on the iterator lock.
4192 it = stcb->asoc.stcb_starting_point_for_iterator;
4196 if (it->inp != stcb->sctp_ep) {
4197 /* hmm, focused on the wrong one? */
4200 if (it->stcb != stcb) {
4203 it->stcb = LIST_NEXT(stcb, sctp_tcblist);
4204 if (it->stcb == NULL) {
4205 /* done with all asoc's in this assoc */
4206 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
4209 it->inp = LIST_NEXT(inp, sctp_list);
4216 * Free the association after un-hashing the remote port. This
4217 * function ALWAYS returns holding NO LOCK on the stcb. It DOES
4218 * expect that the input to this function IS a locked TCB.
4219 * It will return 0, if it did NOT destroy the association (instead
4220 * it unlocks it. It will return NON-zero if it either destroyed the
4221 * association OR the association is already destroyed.
4224 sctp_free_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int from_inpcbfree, int from_location)
4227 struct sctp_association *asoc;
4228 struct sctp_nets *net, *prev;
4229 struct sctp_laddr *laddr;
4230 struct sctp_tmit_chunk *chk;
4231 struct sctp_asconf_addr *aparam;
4232 struct sctp_asconf_ack *aack;
4233 struct sctp_stream_reset_list *liste;
4234 struct sctp_queued_to_read *sq;
4235 struct sctp_stream_queue_pending *sp;
4236 sctp_sharedkey_t *shared_key;
4241 /* first, lets purge the entry from the hash table. */
4243 #ifdef SCTP_LOG_CLOSING
4244 sctp_log_closing(inp, stcb, 6);
4246 if (stcb->asoc.state == 0) {
4247 #ifdef SCTP_LOG_CLOSING
4248 sctp_log_closing(inp, NULL, 7);
4250 /* there is no asoc, really TSNH :-0 */
4254 if (stcb->freed_from_where == 0) {
4255 /* Only record the first place free happened from */
4256 stcb->freed_from_where = from_location;
4261 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4262 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
4263 /* nothing around */
4266 so = inp->sctp_socket;
4269 * We used timer based freeing if a reader or writer is in the way.
4270 * So we first check if we are actually being called from a timer,
4271 * if so we abort early if a reader or writer is still in the way.
4273 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) &&
4274 (from_inpcbfree == SCTP_NORMAL_PROC)) {
4276 * is it the timer driving us? if so are the reader/writers
4279 if (stcb->asoc.refcnt) {
4280 /* nope, reader or writer in the way */
4281 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
4282 /* no asoc destroyed */
4283 SCTP_TCB_UNLOCK(stcb);
4284 #ifdef SCTP_LOG_CLOSING
4285 sctp_log_closing(inp, stcb, 8);
4290 /* now clean up any other timers */
4291 (void)SCTP_OS_TIMER_STOP(&asoc->hb_timer.timer);
4292 asoc->hb_timer.self = NULL;
4293 (void)SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
4294 asoc->dack_timer.self = NULL;
4295 (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
4297 * For stream reset we don't blast this unless
4298 * it is a str-reset timer, it might be the
4299 * free-asoc timer which we DON'T want to
4302 if (asoc->strreset_timer.type == SCTP_TIMER_TYPE_STRRESET)
4303 asoc->strreset_timer.self = NULL;
4304 (void)SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
4305 asoc->asconf_timer.self = NULL;
4306 (void)SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
4307 asoc->autoclose_timer.self = NULL;
4308 (void)SCTP_OS_TIMER_STOP(&asoc->shut_guard_timer.timer);
4309 asoc->shut_guard_timer.self = NULL;
4310 (void)SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
4311 asoc->delayed_event_timer.self = NULL;
4312 /* Mobility adaptation */
4313 (void)SCTP_OS_TIMER_STOP(&asoc->delete_prim_timer.timer);
4314 asoc->delete_prim_timer.self = NULL;
4315 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4316 (void)SCTP_OS_TIMER_STOP(&net->fr_timer.timer);
4317 net->fr_timer.self = NULL;
4318 (void)SCTP_OS_TIMER_STOP(&net->rxt_timer.timer);
4319 net->rxt_timer.self = NULL;
4320 (void)SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
4321 net->pmtu_timer.self = NULL;
4323 /* Now the read queue needs to be cleaned up (only once) */
4325 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0) {
4326 stcb->asoc.state |= SCTP_STATE_ABOUT_TO_BE_FREED;
4327 SCTP_INP_READ_LOCK(inp);
4328 TAILQ_FOREACH(sq, &inp->read_queue, next) {
4329 if (sq->stcb == stcb) {
4330 sq->do_not_ref_stcb = 1;
4331 sq->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
4333 * If there is no end, there never will be
4336 if (sq->end_added == 0) {
4337 /* Held for PD-API clear that. */
4338 sq->pdapi_aborted = 1;
4339 sq->held_length = 0;
4340 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT) && (so != NULL)) {
4342 * Need to add a PD-API
4343 * aborted indication.
4344 * Setting the control_pdapi
4345 * assures that it will be
4346 * added right after this
4351 stcb->asoc.control_pdapi = sq;
4352 strseq = (sq->sinfo_stream << 16) | sq->sinfo_ssn;
4353 sctp_notify_partial_delivery_indication(stcb,
4354 SCTP_PARTIAL_DELIVERY_ABORTED, 1, strseq);
4355 stcb->asoc.control_pdapi = NULL;
4358 /* Add an end to wake them */
4363 SCTP_INP_READ_UNLOCK(inp);
4364 if (stcb->block_entry) {
4366 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PCB, ECONNRESET);
4367 stcb->block_entry->error = ECONNRESET;
4368 stcb->block_entry = NULL;
4371 if (stcb->asoc.refcnt) {
4373 * reader or writer in the way, we have hopefully given him
4374 * something to chew on above.
4376 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
4377 SCTP_TCB_UNLOCK(stcb);
4378 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4379 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
4380 /* nothing around */
4383 /* Wake any reader/writers */
4384 sctp_sorwakeup(inp, so);
4385 sctp_sowwakeup(inp, so);
4387 #ifdef SCTP_LOG_CLOSING
4388 sctp_log_closing(inp, stcb, 9);
4390 /* no asoc destroyed */
4393 #ifdef SCTP_LOG_CLOSING
4394 sctp_log_closing(inp, stcb, 10);
4397 * When I reach here, no others want to kill the assoc yet.. and I
4398 * own the lock. Now its possible an abort comes in when I do the
4399 * lock exchange below to grab all the locks to do the final take
4400 * out. to prevent this we increment the count, which will start a
4401 * timer and blow out above thus assuring us that we hold exclusive
4402 * killing of the asoc. Note that after getting back the TCB lock we
4403 * will go ahead and increment the counter back up and stop any
4404 * timer a passing stranger may have started :-S
4406 if (from_inpcbfree == SCTP_NORMAL_PROC) {
4407 atomic_add_int(&stcb->asoc.refcnt, 1);
4409 SCTP_TCB_UNLOCK(stcb);
4411 SCTP_ITERATOR_LOCK();
4412 SCTP_INP_INFO_WLOCK();
4413 SCTP_INP_WLOCK(inp);
4414 SCTP_TCB_LOCK(stcb);
4416 /* Double check the GONE flag */
4417 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4418 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
4419 /* nothing around */
4422 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4423 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
4425 * For TCP type we need special handling when we are
4426 * connected. We also include the peel'ed off ones to.
4428 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4429 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
4430 inp->sctp_flags |= SCTP_PCB_FLAGS_WAS_CONNECTED;
4433 if (so->so_rcv.sb_cc == 0) {
4434 so->so_state &= ~(SS_ISCONNECTING |
4435 SS_ISDISCONNECTING |
4441 sctp_sowwakeup(inp, so);
4442 sctp_sorwakeup(inp, so);
4448 * Make it invalid too, that way if its about to run it will abort
4451 sctp_iterator_asoc_being_freed(inp, stcb);
4452 /* re-increment the lock */
4453 if (from_inpcbfree == SCTP_NORMAL_PROC) {
4454 atomic_add_int(&stcb->asoc.refcnt, -1);
4457 if (inp->sctp_tcbhash) {
4458 LIST_REMOVE(stcb, sctp_tcbhash);
4460 if (stcb->asoc.in_restart_hash) {
4461 LIST_REMOVE(stcb, sctp_tcbrestarhash);
4463 /* Now lets remove it from the list of ALL associations in the EP */
4464 LIST_REMOVE(stcb, sctp_tcblist);
4465 if (from_inpcbfree == SCTP_NORMAL_PROC) {
4466 SCTP_INP_INCR_REF(inp);
4467 SCTP_INP_WUNLOCK(inp);
4468 SCTP_ITERATOR_UNLOCK();
4470 /* pull from vtag hash */
4471 LIST_REMOVE(stcb, sctp_asocs);
4472 sctp_add_vtag_to_timewait(asoc->my_vtag, SCTP_TIME_WAIT);
4475 * Now restop the timers to be sure - this is paranoia at is finest!
4477 (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
4478 (void)SCTP_OS_TIMER_STOP(&asoc->hb_timer.timer);
4479 (void)SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
4480 (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
4481 (void)SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
4482 (void)SCTP_OS_TIMER_STOP(&asoc->shut_guard_timer.timer);
4483 (void)SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
4484 (void)SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
4485 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4486 (void)SCTP_OS_TIMER_STOP(&net->fr_timer.timer);
4487 (void)SCTP_OS_TIMER_STOP(&net->rxt_timer.timer);
4488 (void)SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
4491 asoc->strreset_timer.type = SCTP_TIMER_TYPE_NONE;
4494 * The chunk lists and such SHOULD be empty but we check them just
4497 /* anything on the wheel needs to be removed */
4498 for (i = 0; i < asoc->streamoutcnt; i++) {
4499 struct sctp_stream_out *outs;
4501 outs = &asoc->strmout[i];
4502 /* now clean up any chunks here */
4503 sp = TAILQ_FIRST(&outs->outqueue);
4505 TAILQ_REMOVE(&outs->outqueue, sp, next);
4507 sctp_m_freem(sp->data);
4509 sp->tail_mbuf = NULL;
4511 sctp_free_remote_addr(sp->net);
4512 sctp_free_spbufspace(stcb, asoc, sp);
4513 /* Free the zone stuff */
4514 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_strmoq, sp);
4515 SCTP_DECR_STRMOQ_COUNT();
4516 /* sa_ignore FREED_MEMORY */
4517 sp = TAILQ_FIRST(&outs->outqueue);
4521 /* sa_ignore FREED_MEMORY */
4522 while ((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) {
4523 TAILQ_REMOVE(&asoc->resetHead, liste, next_resp);
4524 SCTP_FREE(liste, SCTP_M_STRESET);
4527 sq = TAILQ_FIRST(&asoc->pending_reply_queue);
4529 TAILQ_REMOVE(&asoc->pending_reply_queue, sq, next);
4531 sctp_m_freem(sq->data);
4534 sctp_free_remote_addr(sq->whoFrom);
4537 /* Free the ctl entry */
4538 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_readq, sq);
4539 SCTP_DECR_READQ_COUNT();
4540 /* sa_ignore FREED_MEMORY */
4541 sq = TAILQ_FIRST(&asoc->pending_reply_queue);
4544 chk = TAILQ_FIRST(&asoc->free_chunks);
4546 TAILQ_REMOVE(&asoc->free_chunks, chk, sctp_next);
4548 sctp_m_freem(chk->data);
4552 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
4553 SCTP_DECR_CHK_COUNT();
4554 atomic_subtract_int(&sctppcbinfo.ipi_free_chunks, 1);
4555 asoc->free_chunk_cnt--;
4556 /* sa_ignore FREED_MEMORY */
4557 chk = TAILQ_FIRST(&asoc->free_chunks);
4559 /* pending send queue SHOULD be empty */
4560 if (!TAILQ_EMPTY(&asoc->send_queue)) {
4561 chk = TAILQ_FIRST(&asoc->send_queue);
4563 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
4565 sctp_m_freem(chk->data);
4569 sctp_free_remote_addr(chk->whoTo);
4570 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
4571 SCTP_DECR_CHK_COUNT();
4572 /* sa_ignore FREED_MEMORY */
4573 chk = TAILQ_FIRST(&asoc->send_queue);
4578 printf("Freed %d from send_queue\n", ccnt);
4582 /* sent queue SHOULD be empty */
4583 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
4584 chk = TAILQ_FIRST(&asoc->sent_queue);
4586 TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
4588 sctp_m_freem(chk->data);
4592 sctp_free_remote_addr(chk->whoTo);
4593 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
4594 SCTP_DECR_CHK_COUNT();
4595 /* sa_ignore FREED_MEMORY */
4596 chk = TAILQ_FIRST(&asoc->sent_queue);
4601 printf("Freed %d from sent_queue\n", ccnt);
4605 /* control queue MAY not be empty */
4606 if (!TAILQ_EMPTY(&asoc->control_send_queue)) {
4607 chk = TAILQ_FIRST(&asoc->control_send_queue);
4609 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
4611 sctp_m_freem(chk->data);
4615 sctp_free_remote_addr(chk->whoTo);
4616 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
4617 SCTP_DECR_CHK_COUNT();
4618 /* sa_ignore FREED_MEMORY */
4619 chk = TAILQ_FIRST(&asoc->control_send_queue);
4624 printf("Freed %d from ctrl_queue\n", ccnt);
4628 if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
4629 chk = TAILQ_FIRST(&asoc->reasmqueue);
4631 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
4633 sctp_m_freem(chk->data);
4636 sctp_free_remote_addr(chk->whoTo);
4638 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
4639 SCTP_DECR_CHK_COUNT();
4640 /* sa_ignore FREED_MEMORY */
4641 chk = TAILQ_FIRST(&asoc->reasmqueue);
4646 printf("Freed %d from reasm_queue\n", ccnt);
4650 if (asoc->mapping_array) {
4651 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
4652 asoc->mapping_array = NULL;
4654 /* the stream outs */
4655 if (asoc->strmout) {
4656 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
4657 asoc->strmout = NULL;
4659 asoc->streamoutcnt = 0;
4661 struct sctp_queued_to_read *ctl;
4663 for (i = 0; i < asoc->streamincnt; i++) {
4664 if (!TAILQ_EMPTY(&asoc->strmin[i].inqueue)) {
4665 /* We have somethings on the streamin queue */
4666 ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue);
4668 TAILQ_REMOVE(&asoc->strmin[i].inqueue,
4670 sctp_free_remote_addr(ctl->whoFrom);
4672 sctp_m_freem(ctl->data);
4676 * We don't free the address here
4677 * since all the net's were freed
4680 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_readq, ctl);
4681 SCTP_DECR_READQ_COUNT();
4682 ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue);
4686 SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
4687 asoc->strmin = NULL;
4689 asoc->streamincnt = 0;
4690 while (!TAILQ_EMPTY(&asoc->nets)) {
4691 /* sa_ignore FREED_MEMORY */
4692 net = TAILQ_FIRST(&asoc->nets);
4693 /* pull from list */
4694 if ((sctppcbinfo.ipi_count_raddr == 0) || (prev == net)) {
4696 panic("no net's left alloc'ed, or list points to itself");
4701 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
4702 sctp_free_remote_addr(net);
4705 while (!SCTP_LIST_EMPTY(&asoc->sctp_restricted_addrs)) {
4706 /* sa_ignore FREED_MEMORY */
4707 laddr = LIST_FIRST(&asoc->sctp_restricted_addrs);
4708 sctp_remove_laddr(laddr);
4711 /* pending asconf (address) parameters */
4712 while (!TAILQ_EMPTY(&asoc->asconf_queue)) {
4713 /* sa_ignore FREED_MEMORY */
4714 aparam = TAILQ_FIRST(&asoc->asconf_queue);
4715 TAILQ_REMOVE(&asoc->asconf_queue, aparam, next);
4716 SCTP_FREE(aparam, SCTP_M_ASC_ADDR);
4718 while (!TAILQ_EMPTY(&asoc->asconf_ack_sent)) {
4719 /* sa_ignore FREED_MEMORY */
4720 aack = TAILQ_FIRST(&asoc->asconf_ack_sent);
4721 TAILQ_REMOVE(&asoc->asconf_ack_sent, aack, next);
4722 if (aack->data != NULL) {
4723 sctp_m_freem(aack->data);
4725 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asconf_ack, aack);
4727 /* clean up auth stuff */
4728 if (asoc->local_hmacs)
4729 sctp_free_hmaclist(asoc->local_hmacs);
4730 if (asoc->peer_hmacs)
4731 sctp_free_hmaclist(asoc->peer_hmacs);
4733 if (asoc->local_auth_chunks)
4734 sctp_free_chunklist(asoc->local_auth_chunks);
4735 if (asoc->peer_auth_chunks)
4736 sctp_free_chunklist(asoc->peer_auth_chunks);
4738 sctp_free_authinfo(&asoc->authinfo);
4740 shared_key = LIST_FIRST(&asoc->shared_keys);
4741 while (shared_key) {
4742 LIST_REMOVE(shared_key, next);
4743 sctp_free_sharedkey(shared_key);
4744 /* sa_ignore FREED_MEMORY */
4745 shared_key = LIST_FIRST(&asoc->shared_keys);
4748 /* Insert new items here :> */
4750 /* Get rid of LOCK */
4751 SCTP_TCB_LOCK_DESTROY(stcb);
4752 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
4753 if (from_inpcbfree == SCTP_NORMAL_PROC) {
4754 SCTP_INP_INFO_WUNLOCK();
4755 SCTP_INP_RLOCK(inp);
4757 #ifdef SCTP_TRACK_FREED_ASOCS
4758 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4759 /* now clean up the tasoc itself */
4760 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
4761 SCTP_DECR_ASOC_COUNT();
4763 LIST_INSERT_HEAD(&inp->sctp_asoc_free_list, stcb, sctp_tcblist);
4766 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
4767 SCTP_DECR_ASOC_COUNT();
4769 if (from_inpcbfree == SCTP_NORMAL_PROC) {
4770 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4772 * If its NOT the inp_free calling us AND sctp_close
4773 * as been called, we call back...
4775 SCTP_INP_RUNLOCK(inp);
4777 * This will start the kill timer (if we are the
4778 * lastone) since we hold an increment yet. But this
4779 * is the only safe way to do this since otherwise
4780 * if the socket closes at the same time we are here
4781 * we might collide in the cleanup.
4783 sctp_inpcb_free(inp,
4784 SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
4785 SCTP_CALLED_DIRECTLY_NOCMPSET);
4786 SCTP_INP_DECR_REF(inp);
4789 /* The socket is still open. */
4790 SCTP_INP_DECR_REF(inp);
4793 if (from_inpcbfree == SCTP_NORMAL_PROC) {
4794 SCTP_INP_RUNLOCK(inp);
4797 /* destroyed the asoc */
4798 #ifdef SCTP_LOG_CLOSING
4799 sctp_log_closing(inp, NULL, 11);
4807 * determine if a destination is "reachable" based upon the addresses bound
4808 * to the current endpoint (e.g. only v4 or v6 currently bound)
4811 * FIX: if we allow assoc-level bindx(), then this needs to be fixed to use
4812 * assoc level v4/v6 flags, as the assoc *may* not have the same address
4813 * types bound as its endpoint
4816 sctp_destination_is_reachable(struct sctp_tcb *stcb, struct sockaddr *destaddr)
4818 struct sctp_inpcb *inp;
4822 * No locks here, the TCB, in all cases is already locked and an
4823 * assoc is up. There is either a INP lock by the caller applied (in
4824 * asconf case when deleting an address) or NOT in the HB case,
4825 * however if HB then the INP increment is up and the INP will not
4826 * be removed (on top of the fact that we have a TCB lock). So we
4827 * only want to read the sctp_flags, which is either bound-all or
4828 * not.. no protection needed since once an assoc is up you can't be
4829 * changing your binding.
4831 inp = stcb->sctp_ep;
4832 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4833 /* if bound all, destination is not restricted */
4835 * RRS: Question during lock work: Is this correct? If you
4836 * are bound-all you still might need to obey the V4--V6
4837 * flags??? IMO this bound-all stuff needs to be removed!
4841 /* NOTE: all "scope" checks are done when local addresses are added */
4842 if (destaddr->sa_family == AF_INET6) {
4843 answer = inp->ip_inp.inp.inp_vflag & INP_IPV6;
4844 } else if (destaddr->sa_family == AF_INET) {
4845 answer = inp->ip_inp.inp.inp_vflag & INP_IPV4;
4847 /* invalid family, so it's unreachable */
4854 * update the inp_vflags on an endpoint
4857 sctp_update_ep_vflag(struct sctp_inpcb *inp)
4859 struct sctp_laddr *laddr;
4861 /* first clear the flag */
4862 inp->ip_inp.inp.inp_vflag = 0;
4863 /* set the flag based on addresses on the ep list */
4864 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4865 if (laddr->ifa == NULL) {
4866 SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n",
4870 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
4873 if (laddr->ifa->address.sa.sa_family == AF_INET6) {
4874 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
4875 } else if (laddr->ifa->address.sa.sa_family == AF_INET) {
4876 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
4882 * Add the address to the endpoint local address list There is nothing to be
4883 * done if we are bound to all addresses
4886 sctp_add_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa, uint32_t action)
4888 struct sctp_laddr *laddr;
4893 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4894 /* You are already bound to all. You have it already */
4897 if (ifa->address.sa.sa_family == AF_INET6) {
4898 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
4899 /* Can't bind a non-useable addr. */
4903 /* first, is it already present? */
4904 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4905 if (laddr->ifa == ifa) {
4912 /* Not in the ep list */
4913 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, action);
4917 /* update inp_vflag flags */
4918 if (ifa->address.sa.sa_family == AF_INET6) {
4919 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
4920 } else if (ifa->address.sa.sa_family == AF_INET) {
4921 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
4929 * select a new (hopefully reachable) destination net (should only be used
4930 * when we deleted an ep addr that is the only usable source address to reach
4931 * the destination net)
4934 sctp_select_primary_destination(struct sctp_tcb *stcb)
4936 struct sctp_nets *net;
4938 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4939 /* for now, we'll just pick the first reachable one we find */
4940 if (net->dest_state & SCTP_ADDR_UNCONFIRMED)
4942 if (sctp_destination_is_reachable(stcb,
4943 (struct sockaddr *)&net->ro._l_addr)) {
4944 /* found a reachable destination */
4945 stcb->asoc.primary_destination = net;
4948 /* I can't there from here! ...we're gonna die shortly... */
4953 * Delete the address from the endpoint local address list There is nothing
4954 * to be done if we are bound to all addresses
4957 sctp_del_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
4959 struct sctp_laddr *laddr;
4963 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4964 /* You are already bound to all. You have it already */
4967 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4968 if (laddr->ifa == ifa) {
4973 if (fnd && (inp->laddr_count < 2)) {
4974 /* can't delete unless there are at LEAST 2 addresses */
4979 * clean up any use of this address go through our
4980 * associations and clear any last_used_address that match
4981 * this one for each assoc, see if a new primary_destination
4984 struct sctp_tcb *stcb;
4986 /* clean up "next_addr_touse" */
4987 if (inp->next_addr_touse == laddr)
4988 /* delete this address */
4989 inp->next_addr_touse = NULL;
4991 /* clean up "last_used_address" */
4992 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4993 struct sctp_nets *net;
4995 SCTP_TCB_LOCK(stcb);
4996 if (stcb->asoc.last_used_address == laddr)
4997 /* delete this address */
4998 stcb->asoc.last_used_address = NULL;
5000 * Now spin through all the nets and purge any ref
5003 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5004 if (net->ro._s_addr &&
5005 (net->ro._s_addr->ifa == laddr->ifa)) {
5006 /* Yep, purge src address selected */
5009 /* delete this address if cached */
5013 net->ro.ro_rt = NULL;
5015 sctp_free_ifa(net->ro._s_addr);
5016 net->ro._s_addr = NULL;
5017 net->src_addr_selected = 0;
5020 SCTP_TCB_UNLOCK(stcb);
5021 } /* for each tcb */
5022 /* remove it from the ep list */
5023 sctp_remove_laddr(laddr);
5025 /* update inp_vflag flags */
5026 sctp_update_ep_vflag(inp);
5032 * Add the address to the TCB local address restricted list.
5033 * This is a "pending" address list (eg. addresses waiting for an
5034 * ASCONF-ACK response) and cannot be used as a valid source address.
5037 sctp_add_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
5039 struct sctp_inpcb *inp;
5040 struct sctp_laddr *laddr;
5041 struct sctpladdr *list;
5044 * Assumes TCB is locked.. and possibly the INP. May need to
5045 * confirm/fix that if we need it and is not the case.
5047 list = &stcb->asoc.sctp_restricted_addrs;
5049 inp = stcb->sctp_ep;
5050 if (ifa->address.sa.sa_family == AF_INET6) {
5051 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
5052 /* Can't bind a non-existent addr. */
5056 /* does the address already exist? */
5057 LIST_FOREACH(laddr, list, sctp_nxt_addr) {
5058 if (laddr->ifa == ifa) {
5063 /* add to the list */
5064 (void)sctp_insert_laddr(list, ifa, 0);
5069 * insert an laddr entry with the given ifa for the desired list
5072 sctp_insert_laddr(struct sctpladdr *list, struct sctp_ifa *ifa, uint32_t act)
5074 struct sctp_laddr *laddr;
5076 laddr = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_laddr, struct sctp_laddr);
5077 if (laddr == NULL) {
5078 /* out of memory? */
5079 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
5082 SCTP_INCR_LADDR_COUNT();
5083 bzero(laddr, sizeof(*laddr));
5084 (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time);
5086 laddr->action = act;
5087 atomic_add_int(&ifa->refcount, 1);
5089 LIST_INSERT_HEAD(list, laddr, sctp_nxt_addr);
5095 * Remove an laddr entry from the local address list (on an assoc)
5098 sctp_remove_laddr(struct sctp_laddr *laddr)
5101 /* remove from the list */
5102 LIST_REMOVE(laddr, sctp_nxt_addr);
5103 sctp_free_ifa(laddr->ifa);
5104 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_laddr, laddr);
5105 SCTP_DECR_LADDR_COUNT();
5109 * Remove a local address from the TCB local address restricted list
5112 sctp_del_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
5114 struct sctp_inpcb *inp;
5115 struct sctp_laddr *laddr;
5118 * This is called by asconf work. It is assumed that a) The TCB is
5119 * locked and b) The INP is locked. This is true in as much as I can
5120 * trace through the entry asconf code where I did these locks.
5121 * Again, the ASCONF code is a bit different in that it does lock
5122 * the INP during its work often times. This must be since we don't
5123 * want other proc's looking up things while what they are looking
5124 * up is changing :-D
5127 inp = stcb->sctp_ep;
5128 /* if subset bound and don't allow ASCONF's, can't delete last */
5129 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) &&
5130 sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF)) {
5131 if (stcb->sctp_ep->laddr_count < 2) {
5132 /* can't delete last address */
5136 LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
5137 /* remove the address if it exists */
5138 if (laddr->ifa == NULL)
5140 if (laddr->ifa == ifa) {
5141 sctp_remove_laddr(laddr);
5146 /* address not found! */
5150 static char sctp_pcb_initialized = 0;
5153 * Temporarily remove for __APPLE__ until we use the Tiger equivalents
5156 static int sctp_max_number_of_assoc = SCTP_MAX_NUM_OF_ASOC;
5157 static int sctp_scale_up_for_address = SCTP_SCALE_FOR_ADDR;
5163 * SCTP initialization for the PCB structures should be called by
5164 * the sctp_init() funciton.
5169 if (sctp_pcb_initialized != 0) {
5170 /* error I was called twice */
5173 sctp_pcb_initialized = 1;
5175 bzero(&sctpstat, sizeof(struct sctpstat));
5176 #if defined(SCTP_LOCAL_TRACE_BUF)
5177 bzero(&sctp_log, sizeof(struct sctp_log));
5179 (void)SCTP_GETTIME_TIMEVAL(&tv);
5180 sctpstat.sctps_discontinuitytime.tv_sec = (uint32_t) tv.tv_sec;
5181 sctpstat.sctps_discontinuitytime.tv_usec = (uint32_t) tv.tv_usec;
5182 /* init the empty list of (All) Endpoints */
5183 LIST_INIT(&sctppcbinfo.listhead);
5185 /* init the iterator head */
5186 TAILQ_INIT(&sctppcbinfo.iteratorhead);
5188 /* init the hash table of endpoints */
5189 TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", &sctp_hashtblsize);
5190 TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", &sctp_pcbtblsize);
5191 TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", &sctp_chunkscale);
5192 sctppcbinfo.sctp_asochash = SCTP_HASH_INIT((sctp_hashtblsize * 31),
5193 &sctppcbinfo.hashasocmark);
5194 sctppcbinfo.sctp_ephash = SCTP_HASH_INIT(sctp_hashtblsize,
5195 &sctppcbinfo.hashmark);
5196 sctppcbinfo.sctp_tcpephash = SCTP_HASH_INIT(sctp_hashtblsize,
5197 &sctppcbinfo.hashtcpmark);
5198 sctppcbinfo.hashtblsize = sctp_hashtblsize;
5200 /* init the small hash table we use to track restarted asoc's */
5201 sctppcbinfo.sctp_restarthash = SCTP_HASH_INIT(SCTP_STACK_VTAG_HASH_SIZE,
5202 &sctppcbinfo.hashrestartmark);
5205 sctppcbinfo.sctp_vrfhash = SCTP_HASH_INIT(SCTP_SIZE_OF_VRF_HASH,
5206 &sctppcbinfo.hashvrfmark);
5208 sctppcbinfo.vrf_ifn_hash = SCTP_HASH_INIT(SCTP_VRF_IFN_HASH_SIZE,
5209 &sctppcbinfo.vrf_ifn_hashmark);
5211 /* init the zones */
5213 * FIX ME: Should check for NULL returns, but if it does fail we are
5214 * doomed to panic anyways... add later maybe.
5216 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_ep, "sctp_ep",
5217 sizeof(struct sctp_inpcb), maxsockets);
5219 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_asoc, "sctp_asoc",
5220 sizeof(struct sctp_tcb), sctp_max_number_of_assoc);
5222 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_laddr, "sctp_laddr",
5223 sizeof(struct sctp_laddr),
5224 (sctp_max_number_of_assoc * sctp_scale_up_for_address));
5226 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_net, "sctp_raddr",
5227 sizeof(struct sctp_nets),
5228 (sctp_max_number_of_assoc * sctp_scale_up_for_address));
5230 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_chunk, "sctp_chunk",
5231 sizeof(struct sctp_tmit_chunk),
5232 (sctp_max_number_of_assoc * sctp_chunkscale));
5234 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_readq, "sctp_readq",
5235 sizeof(struct sctp_queued_to_read),
5236 (sctp_max_number_of_assoc * sctp_chunkscale));
5238 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_strmoq, "sctp_stream_msg_out",
5239 sizeof(struct sctp_stream_queue_pending),
5240 (sctp_max_number_of_assoc * sctp_chunkscale));
5242 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_asconf_ack, "sctp_asconf_ack",
5243 sizeof(struct sctp_asconf_ack),
5244 (sctp_max_number_of_assoc * sctp_chunkscale));
5247 /* Master Lock INIT for info structure */
5248 SCTP_INP_INFO_LOCK_INIT();
5249 SCTP_STATLOG_INIT_LOCK();
5250 SCTP_ITERATOR_LOCK_INIT();
5252 SCTP_IPI_COUNT_INIT();
5253 SCTP_IPI_ADDR_INIT();
5254 SCTP_IPI_ITERATOR_WQ_INIT();
5255 #ifdef SCTP_PACKET_LOGGING
5256 SCTP_IP_PKTLOG_INIT();
5258 LIST_INIT(&sctppcbinfo.addr_wq);
5260 /* not sure if we need all the counts */
5261 sctppcbinfo.ipi_count_ep = 0;
5262 /* assoc/tcb zone info */
5263 sctppcbinfo.ipi_count_asoc = 0;
5264 /* local addrlist zone info */
5265 sctppcbinfo.ipi_count_laddr = 0;
5266 /* remote addrlist zone info */
5267 sctppcbinfo.ipi_count_raddr = 0;
5269 sctppcbinfo.ipi_count_chunk = 0;
5271 /* socket queue zone info */
5272 sctppcbinfo.ipi_count_readq = 0;
5274 /* stream out queue cont */
5275 sctppcbinfo.ipi_count_strmoq = 0;
5277 sctppcbinfo.ipi_free_strmoq = 0;
5278 sctppcbinfo.ipi_free_chunks = 0;
5280 SCTP_OS_TIMER_INIT(&sctppcbinfo.addr_wq_timer.timer);
5282 /* Init the TIMEWAIT list */
5283 for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE_A; i++) {
5284 LIST_INIT(&sctppcbinfo.vtag_timewait[i]);
5287 #if defined(SCTP_USE_THREAD_BASED_ITERATOR)
5288 sctppcbinfo.iterator_running = 0;
5289 sctp_startup_iterator();
5293 * INIT the default VRF which for BSD is the only one, other O/S's
5294 * may have more. But initially they must start with one and then
5295 * add the VRF's as addresses are added.
5297 sctp_init_vrf_list(SCTP_DEFAULT_VRF);
5303 sctp_load_addresses_from_init(struct sctp_tcb *stcb, struct mbuf *m,
5304 int iphlen, int offset, int limit, struct sctphdr *sh,
5305 struct sockaddr *altsa)
5308 * grub through the INIT pulling addresses and loading them to the
5309 * nets structure in the asoc. The from address in the mbuf should
5310 * also be loaded (if it is not already). This routine can be called
5311 * with either INIT or INIT-ACK's as long as the m points to the IP
5312 * packet and the offset points to the beginning of the parameters.
5314 struct sctp_inpcb *inp, *l_inp;
5315 struct sctp_nets *net, *net_tmp;
5317 struct sctp_paramhdr *phdr, parm_buf;
5318 struct sctp_tcb *stcb_tmp;
5319 uint16_t ptype, plen;
5320 struct sockaddr *sa;
5321 struct sockaddr_storage dest_store;
5322 struct sockaddr *local_sa = (struct sockaddr *)&dest_store;
5323 struct sockaddr_in sin;
5324 struct sockaddr_in6 sin6;
5325 uint8_t random_store[SCTP_PARAM_BUFFER_SIZE];
5326 struct sctp_auth_random *p_random = NULL;
5327 uint16_t random_len = 0;
5328 uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE];
5329 struct sctp_auth_hmac_algo *hmacs = NULL;
5330 uint16_t hmacs_len = 0;
5331 uint8_t saw_asconf = 0;
5332 uint8_t saw_asconf_ack = 0;
5333 uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE];
5334 struct sctp_auth_chunk_list *chunks = NULL;
5335 uint16_t num_chunks = 0;
5336 sctp_key_t *new_key;
5338 int got_random = 0, got_hmacs = 0, got_chklist = 0;
5340 /* First get the destination address setup too. */
5341 memset(&sin, 0, sizeof(sin));
5342 memset(&sin6, 0, sizeof(sin6));
5344 sin.sin_family = AF_INET;
5345 sin.sin_len = sizeof(sin);
5346 sin.sin_port = stcb->rport;
5348 sin6.sin6_family = AF_INET6;
5349 sin6.sin6_len = sizeof(struct sockaddr_in6);
5350 sin6.sin6_port = stcb->rport;
5351 if (altsa == NULL) {
5352 iph = mtod(m, struct ip *);
5353 switch (iph->ip_v) {
5357 struct sockaddr_in *sin_2;
5359 sin_2 = (struct sockaddr_in *)(local_sa);
5360 memset(sin_2, 0, sizeof(sin));
5361 sin_2->sin_family = AF_INET;
5362 sin_2->sin_len = sizeof(sin);
5363 sin_2->sin_port = sh->dest_port;
5364 sin_2->sin_addr.s_addr = iph->ip_dst.s_addr;
5365 sin.sin_addr = iph->ip_src;
5366 sa = (struct sockaddr *)&sin;
5370 case IPV6_VERSION >> 4:
5373 struct ip6_hdr *ip6;
5374 struct sockaddr_in6 *sin6_2;
5376 ip6 = mtod(m, struct ip6_hdr *);
5377 sin6_2 = (struct sockaddr_in6 *)(local_sa);
5378 memset(sin6_2, 0, sizeof(sin6));
5379 sin6_2->sin6_family = AF_INET6;
5380 sin6_2->sin6_len = sizeof(struct sockaddr_in6);
5381 sin6_2->sin6_port = sh->dest_port;
5382 sin6.sin6_addr = ip6->ip6_src;
5383 sa = (struct sockaddr *)&sin6;
5393 * For cookies we use the src address NOT from the packet
5394 * but from the original INIT
5398 /* Turn off ECN until we get through all params */
5399 stcb->asoc.ecn_allowed = 0;
5400 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
5401 /* mark all addresses that we have currently on the list */
5402 net->dest_state |= SCTP_ADDR_NOT_IN_ASSOC;
5404 /* does the source address already exist? if so skip it */
5405 l_inp = inp = stcb->sctp_ep;
5407 atomic_add_int(&stcb->asoc.refcnt, 1);
5408 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net_tmp, local_sa, stcb);
5409 atomic_add_int(&stcb->asoc.refcnt, -1);
5411 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || inp == NULL) {
5412 /* we must add the source address */
5413 /* no scope set here since we have a tcb already. */
5414 if ((sa->sa_family == AF_INET) &&
5415 (stcb->asoc.ipv4_addr_legal)) {
5416 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_2)) {
5419 } else if ((sa->sa_family == AF_INET6) &&
5420 (stcb->asoc.ipv6_addr_legal)) {
5421 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) {
5426 if (net_tmp != NULL && stcb_tmp == stcb) {
5427 net_tmp->dest_state &= ~SCTP_ADDR_NOT_IN_ASSOC;
5428 } else if (stcb_tmp != stcb) {
5429 /* It belongs to another association? */
5431 SCTP_TCB_UNLOCK(stcb_tmp);
5435 if (stcb->asoc.state == 0) {
5436 /* the assoc was freed? */
5440 * peer must explicitly turn this on. This may have been initialized
5441 * to be "on" in order to allow local addr changes while INIT's are
5444 stcb->asoc.peer_supports_asconf = 0;
5445 /* now we must go through each of the params. */
5446 phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
5448 ptype = ntohs(phdr->param_type);
5449 plen = ntohs(phdr->param_length);
5451 * printf("ptype => %0x, plen => %d\n", (uint32_t)ptype,
5454 if (offset + plen > limit) {
5460 if (ptype == SCTP_IPV4_ADDRESS) {
5461 if (stcb->asoc.ipv4_addr_legal) {
5462 struct sctp_ipv4addr_param *p4, p4_buf;
5464 /* ok get the v4 address and check/add */
5465 phdr = sctp_get_next_param(m, offset,
5466 (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
5467 if (plen != sizeof(struct sctp_ipv4addr_param) ||
5471 p4 = (struct sctp_ipv4addr_param *)phdr;
5472 sin.sin_addr.s_addr = p4->addr;
5473 if (IN_MULTICAST(sin.sin_addr.s_addr)) {
5474 /* Skip multi-cast addresses */
5477 if ((sin.sin_addr.s_addr == INADDR_BROADCAST) ||
5478 (sin.sin_addr.s_addr == INADDR_ANY)) {
5481 sa = (struct sockaddr *)&sin;
5482 inp = stcb->sctp_ep;
5483 atomic_add_int(&stcb->asoc.refcnt, 1);
5484 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
5486 atomic_add_int(&stcb->asoc.refcnt, -1);
5488 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) ||
5490 /* we must add the source address */
5492 * no scope set since we have a tcb
5497 * we must validate the state again
5500 if (stcb->asoc.state == 0) {
5501 /* the assoc was freed? */
5504 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_4)) {
5507 } else if (stcb_tmp == stcb) {
5508 if (stcb->asoc.state == 0) {
5509 /* the assoc was freed? */
5515 ~SCTP_ADDR_NOT_IN_ASSOC;
5519 * strange, address is in another
5520 * assoc? straighten out locks.
5523 SCTP_TCB_UNLOCK(stcb_tmp);
5525 if (stcb->asoc.state == 0) {
5526 /* the assoc was freed? */
5532 } else if (ptype == SCTP_IPV6_ADDRESS) {
5533 if (stcb->asoc.ipv6_addr_legal) {
5534 /* ok get the v6 address and check/add */
5535 struct sctp_ipv6addr_param *p6, p6_buf;
5537 phdr = sctp_get_next_param(m, offset,
5538 (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
5539 if (plen != sizeof(struct sctp_ipv6addr_param) ||
5543 p6 = (struct sctp_ipv6addr_param *)phdr;
5544 memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
5546 if (IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) {
5547 /* Skip multi-cast addresses */
5550 if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
5552 * Link local make no sense without
5557 sa = (struct sockaddr *)&sin6;
5558 inp = stcb->sctp_ep;
5559 atomic_add_int(&stcb->asoc.refcnt, 1);
5560 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
5562 atomic_add_int(&stcb->asoc.refcnt, -1);
5563 if (stcb_tmp == NULL && (inp == stcb->sctp_ep ||
5566 * we must validate the state again
5569 if (stcb->asoc.state == 0) {
5570 /* the assoc was freed? */
5574 * we must add the address, no scope
5577 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_5)) {
5580 } else if (stcb_tmp == stcb) {
5582 * we must validate the state again
5585 if (stcb->asoc.state == 0) {
5586 /* the assoc was freed? */
5592 ~SCTP_ADDR_NOT_IN_ASSOC;
5596 * strange, address is in another
5597 * assoc? straighten out locks.
5600 SCTP_TCB_UNLOCK(stcb_tmp);
5602 if (stcb->asoc.state == 0) {
5603 /* the assoc was freed? */
5609 } else if (ptype == SCTP_ECN_CAPABLE) {
5610 stcb->asoc.ecn_allowed = 1;
5611 } else if (ptype == SCTP_ULP_ADAPTATION) {
5612 if (stcb->asoc.state != SCTP_STATE_OPEN) {
5613 struct sctp_adaptation_layer_indication ai,
5616 phdr = sctp_get_next_param(m, offset,
5617 (struct sctp_paramhdr *)&ai, sizeof(ai));
5618 aip = (struct sctp_adaptation_layer_indication *)phdr;
5620 stcb->asoc.peers_adaptation = ntohl(aip->indication);
5621 stcb->asoc.adaptation_needed = 1;
5624 } else if (ptype == SCTP_SET_PRIM_ADDR) {
5625 struct sctp_asconf_addr_param lstore, *fee;
5626 struct sctp_asconf_addrv4_param *fii;
5628 struct sockaddr *lsa = NULL;
5630 stcb->asoc.peer_supports_asconf = 1;
5631 if (plen > sizeof(lstore)) {
5634 phdr = sctp_get_next_param(m, offset,
5635 (struct sctp_paramhdr *)&lstore, min(plen, sizeof(lstore)));
5639 fee = (struct sctp_asconf_addr_param *)phdr;
5640 lptype = ntohs(fee->addrp.ph.param_type);
5641 if (lptype == SCTP_IPV4_ADDRESS) {
5643 sizeof(struct sctp_asconf_addrv4_param)) {
5644 SCTP_PRINTF("Sizeof setprim in init/init ack not %d but %d - ignored\n",
5645 (int)sizeof(struct sctp_asconf_addrv4_param),
5648 fii = (struct sctp_asconf_addrv4_param *)fee;
5649 sin.sin_addr.s_addr = fii->addrp.addr;
5650 lsa = (struct sockaddr *)&sin;
5652 } else if (lptype == SCTP_IPV6_ADDRESS) {
5654 sizeof(struct sctp_asconf_addr_param)) {
5655 SCTP_PRINTF("Sizeof setprim (v6) in init/init ack not %d but %d - ignored\n",
5656 (int)sizeof(struct sctp_asconf_addr_param),
5659 memcpy(sin6.sin6_addr.s6_addr,
5661 sizeof(fee->addrp.addr));
5662 lsa = (struct sockaddr *)&sin6;
5666 (void)sctp_set_primary_addr(stcb, sa, NULL);
5668 } else if (ptype == SCTP_PRSCTP_SUPPORTED) {
5669 /* Peer supports pr-sctp */
5670 stcb->asoc.peer_supports_prsctp = 1;
5671 } else if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
5672 /* A supported extension chunk */
5673 struct sctp_supported_chunk_types_param *pr_supported;
5674 uint8_t local_store[SCTP_PARAM_BUFFER_SIZE];
5677 phdr = sctp_get_next_param(m, offset,
5678 (struct sctp_paramhdr *)&local_store, min(sizeof(local_store), plen));
5682 stcb->asoc.peer_supports_asconf = 0;
5683 stcb->asoc.peer_supports_prsctp = 0;
5684 stcb->asoc.peer_supports_pktdrop = 0;
5685 stcb->asoc.peer_supports_strreset = 0;
5686 stcb->asoc.peer_supports_auth = 0;
5687 pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
5688 num_ent = plen - sizeof(struct sctp_paramhdr);
5689 for (i = 0; i < num_ent; i++) {
5690 switch (pr_supported->chunk_types[i]) {
5692 case SCTP_ASCONF_ACK:
5693 stcb->asoc.peer_supports_asconf = 1;
5695 case SCTP_FORWARD_CUM_TSN:
5696 stcb->asoc.peer_supports_prsctp = 1;
5698 case SCTP_PACKET_DROPPED:
5699 stcb->asoc.peer_supports_pktdrop = 1;
5701 case SCTP_STREAM_RESET:
5702 stcb->asoc.peer_supports_strreset = 1;
5704 case SCTP_AUTHENTICATION:
5705 stcb->asoc.peer_supports_auth = 1;
5708 /* one I have not learned yet */
5713 } else if (ptype == SCTP_ECN_NONCE_SUPPORTED) {
5714 /* Peer supports ECN-nonce */
5715 stcb->asoc.peer_supports_ecn_nonce = 1;
5716 stcb->asoc.ecn_nonce_allowed = 1;
5717 } else if (ptype == SCTP_RANDOM) {
5718 if (plen > sizeof(random_store))
5721 /* already processed a RANDOM */
5724 phdr = sctp_get_next_param(m, offset,
5725 (struct sctp_paramhdr *)random_store,
5726 min(sizeof(random_store), plen));
5729 p_random = (struct sctp_auth_random *)phdr;
5730 random_len = plen - sizeof(*p_random);
5731 /* enforce the random length */
5732 if (random_len != SCTP_AUTH_RANDOM_SIZE_REQUIRED) {
5733 SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: invalid RANDOM len\n");
5737 } else if (ptype == SCTP_HMAC_LIST) {
5741 if (plen > sizeof(hmacs_store))
5744 /* already processed a HMAC list */
5747 phdr = sctp_get_next_param(m, offset,
5748 (struct sctp_paramhdr *)hmacs_store,
5749 min(plen, sizeof(hmacs_store)));
5752 hmacs = (struct sctp_auth_hmac_algo *)phdr;
5753 hmacs_len = plen - sizeof(*hmacs);
5754 num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]);
5755 /* validate the hmac list */
5756 if (sctp_verify_hmac_param(hmacs, num_hmacs)) {
5759 if (stcb->asoc.peer_hmacs != NULL)
5760 sctp_free_hmaclist(stcb->asoc.peer_hmacs);
5761 stcb->asoc.peer_hmacs = sctp_alloc_hmaclist(num_hmacs);
5762 if (stcb->asoc.peer_hmacs != NULL) {
5763 for (i = 0; i < num_hmacs; i++) {
5764 (void)sctp_auth_add_hmacid(stcb->asoc.peer_hmacs,
5765 ntohs(hmacs->hmac_ids[i]));
5769 } else if (ptype == SCTP_CHUNK_LIST) {
5772 if (plen > sizeof(chunks_store))
5775 /* already processed a Chunks list */
5778 phdr = sctp_get_next_param(m, offset,
5779 (struct sctp_paramhdr *)chunks_store,
5780 min(plen, sizeof(chunks_store)));
5783 chunks = (struct sctp_auth_chunk_list *)phdr;
5784 num_chunks = plen - sizeof(*chunks);
5785 if (stcb->asoc.peer_auth_chunks != NULL)
5786 sctp_clear_chunklist(stcb->asoc.peer_auth_chunks);
5788 stcb->asoc.peer_auth_chunks = sctp_alloc_chunklist();
5789 for (i = 0; i < num_chunks; i++) {
5790 (void)sctp_auth_add_chunk(chunks->chunk_types[i],
5791 stcb->asoc.peer_auth_chunks);
5792 /* record asconf/asconf-ack if listed */
5793 if (chunks->chunk_types[i] == SCTP_ASCONF)
5795 if (chunks->chunk_types[i] == SCTP_ASCONF_ACK)
5800 } else if ((ptype == SCTP_HEARTBEAT_INFO) ||
5801 (ptype == SCTP_STATE_COOKIE) ||
5802 (ptype == SCTP_UNRECOG_PARAM) ||
5803 (ptype == SCTP_COOKIE_PRESERVE) ||
5804 (ptype == SCTP_SUPPORTED_ADDRTYPE) ||
5805 (ptype == SCTP_ADD_IP_ADDRESS) ||
5806 (ptype == SCTP_DEL_IP_ADDRESS) ||
5807 (ptype == SCTP_ERROR_CAUSE_IND) ||
5808 (ptype == SCTP_SUCCESS_REPORT)) {
5811 if ((ptype & 0x8000) == 0x0000) {
5813 * must stop processing the rest of the
5814 * param's. Any report bits were handled
5816 * sctp_arethere_unrecognized_parameters()
5817 * when the INIT or INIT-ACK was first seen.
5823 offset += SCTP_SIZE32(plen);
5824 if (offset >= limit) {
5827 phdr = sctp_get_next_param(m, offset, &parm_buf,
5830 /* Now check to see if we need to purge any addresses */
5831 for (net = TAILQ_FIRST(&stcb->asoc.nets); net != NULL; net = net_tmp) {
5832 net_tmp = TAILQ_NEXT(net, sctp_next);
5833 if ((net->dest_state & SCTP_ADDR_NOT_IN_ASSOC) ==
5834 SCTP_ADDR_NOT_IN_ASSOC) {
5835 /* This address has been removed from the asoc */
5836 /* remove and free it */
5837 stcb->asoc.numnets--;
5838 TAILQ_REMOVE(&stcb->asoc.nets, net, sctp_next);
5839 sctp_free_remote_addr(net);
5840 if (net == stcb->asoc.primary_destination) {
5841 stcb->asoc.primary_destination = NULL;
5842 sctp_select_primary_destination(stcb);
5846 /* validate authentication required parameters */
5847 if (got_random && got_hmacs) {
5848 stcb->asoc.peer_supports_auth = 1;
5850 stcb->asoc.peer_supports_auth = 0;
5852 if (!stcb->asoc.peer_supports_auth && got_chklist) {
5853 /* peer does not support auth but sent a chunks list? */
5856 if (!sctp_asconf_auth_nochk && stcb->asoc.peer_supports_asconf &&
5857 !stcb->asoc.peer_supports_auth) {
5858 /* peer supports asconf but not auth? */
5860 } else if ((stcb->asoc.peer_supports_asconf) && (stcb->asoc.peer_supports_auth) &&
5861 ((saw_asconf == 0) || (saw_asconf_ack == 0))) {
5864 /* concatenate the full random key */
5865 #ifdef SCTP_AUTH_DRAFT_04
5866 keylen = random_len;
5867 new_key = sctp_alloc_key(keylen);
5868 if (new_key != NULL) {
5869 /* copy in the RANDOM */
5870 if (p_random != NULL)
5871 bcopy(p_random->random_data, new_key->key, random_len);
5874 keylen = sizeof(*p_random) + random_len + sizeof(*chunks) + num_chunks +
5875 sizeof(*hmacs) + hmacs_len;
5876 new_key = sctp_alloc_key(keylen);
5877 if (new_key != NULL) {
5878 /* copy in the RANDOM */
5879 if (p_random != NULL) {
5880 keylen = sizeof(*p_random) + random_len;
5881 bcopy(p_random, new_key->key, keylen);
5883 /* append in the AUTH chunks */
5884 if (chunks != NULL) {
5885 bcopy(chunks, new_key->key + keylen,
5886 sizeof(*chunks) + num_chunks);
5887 keylen += sizeof(*chunks) + num_chunks;
5889 /* append in the HMACs */
5890 if (hmacs != NULL) {
5891 bcopy(hmacs, new_key->key + keylen,
5892 sizeof(*hmacs) + hmacs_len);
5897 /* failed to get memory for the key */
5900 if (stcb->asoc.authinfo.peer_random != NULL)
5901 sctp_free_key(stcb->asoc.authinfo.peer_random);
5902 stcb->asoc.authinfo.peer_random = new_key;
5903 #ifdef SCTP_AUTH_DRAFT_04
5904 /* don't include the chunks and hmacs for draft -04 */
5905 stcb->asoc.authinfo.peer_random->keylen = random_len;
5907 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid);
5908 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid);
5914 sctp_set_primary_addr(struct sctp_tcb *stcb, struct sockaddr *sa,
5915 struct sctp_nets *net)
5917 /* make sure the requested primary address exists in the assoc */
5918 if (net == NULL && sa)
5919 net = sctp_findnet(stcb, sa);
5922 /* didn't find the requested primary address! */
5925 /* set the primary address */
5926 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
5927 /* Must be confirmed, so queue to set */
5928 net->dest_state |= SCTP_ADDR_REQ_PRIMARY;
5931 stcb->asoc.primary_destination = net;
5932 net->dest_state &= ~SCTP_ADDR_WAS_PRIMARY;
5933 net = TAILQ_FIRST(&stcb->asoc.nets);
5934 if (net != stcb->asoc.primary_destination) {
5936 * first one on the list is NOT the primary
5937 * sctp_cmpaddr() is much more efficent if the
5938 * primary is the first on the list, make it so.
5940 TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
5941 TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
5948 sctp_is_vtag_good(struct sctp_inpcb *inp, uint32_t tag, struct timeval *now, int save_in_twait)
5951 * This function serves two purposes. It will see if a TAG can be
5952 * re-used and return 1 for yes it is ok and 0 for don't use that
5953 * tag. A secondary function it will do is purge out old tags that
5956 struct sctpasochead *head;
5957 struct sctpvtaghead *chain;
5958 struct sctp_tagblock *twait_block;
5959 struct sctp_tcb *stcb;
5962 SCTP_INP_INFO_WLOCK();
5963 chain = &sctppcbinfo.vtag_timewait[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
5964 /* First is the vtag in use ? */
5966 head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(tag,
5967 sctppcbinfo.hashasocmark)];
5971 LIST_FOREACH(stcb, head, sctp_asocs) {
5973 if (stcb->asoc.my_vtag == tag) {
5975 * We should remove this if and return 0 always if
5976 * we want vtags unique across all endpoints. For
5977 * now within a endpoint is ok.
5979 if (inp == stcb->sctp_ep) {
5980 /* bad tag, in use */
5981 SCTP_INP_INFO_WUNLOCK();
5987 /* Now lets check the restart hash */
5988 head = &sctppcbinfo.sctp_restarthash[SCTP_PCBHASH_ASOC(tag,
5989 sctppcbinfo.hashrestartmark)];
5991 goto check_time_wait;
5993 LIST_FOREACH(stcb, head, sctp_tcbrestarhash) {
5994 if (stcb->asoc.assoc_id == tag) {
5996 if (inp == stcb->sctp_ep) {
5997 /* bad tag, in use */
5998 SCTP_INP_INFO_WUNLOCK();
6004 /* Now what about timed wait ? */
6005 if (!SCTP_LIST_EMPTY(chain)) {
6007 * Block(s) are present, lets see if we have this tag in the
6010 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
6011 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
6012 if (twait_block->vtag_block[i].v_tag == 0) {
6015 } else if ((long)twait_block->vtag_block[i].tv_sec_at_expire <
6017 /* Audit expires this guy */
6018 twait_block->vtag_block[i].tv_sec_at_expire = 0;
6019 twait_block->vtag_block[i].v_tag = 0;
6020 } else if (twait_block->vtag_block[i].v_tag ==
6022 /* Bad tag, sorry :< */
6023 SCTP_INP_INFO_WUNLOCK();
6030 * Not found, ok to use the tag, add it to the time wait hash
6031 * as well this will prevent two sucessive cookies from getting
6032 * the same tag or two inits sent quickly on multi-processors.
6033 * We only keep the tag for the life of a cookie and when we
6034 * add this tag to the assoc hash we need to purge it from
6038 sctp_add_vtag_to_timewait(tag, TICKS_TO_SEC(inp->sctp_ep.def_cookie_life));
6039 SCTP_INP_INFO_WUNLOCK();
6044 static sctp_assoc_t reneged_asoc_ids[256];
6045 static uint8_t reneged_at = 0;
6049 sctp_drain_mbufs(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
6052 * We must hunt this association for MBUF's past the cumack (i.e.
6053 * out of order data that we can renege on).
6055 struct sctp_association *asoc;
6056 struct sctp_tmit_chunk *chk, *nchk;
6057 uint32_t cumulative_tsn_p1, tsn;
6058 struct sctp_queued_to_read *ctl, *nctl;
6059 int cnt, strmat, gap;
6061 /* We look for anything larger than the cum-ack + 1 */
6063 SCTP_STAT_INCR(sctps_protocol_drain_calls);
6064 if (sctp_do_drain == 0) {
6068 if (asoc->cumulative_tsn == asoc->highest_tsn_inside_map) {
6069 /* none we can reneg on. */
6072 SCTP_STAT_INCR(sctps_protocol_drains_done);
6073 cumulative_tsn_p1 = asoc->cumulative_tsn + 1;
6075 /* First look in the re-assembly queue */
6076 chk = TAILQ_FIRST(&asoc->reasmqueue);
6078 /* Get the next one */
6079 nchk = TAILQ_NEXT(chk, sctp_next);
6080 if (compare_with_wrap(chk->rec.data.TSN_seq,
6081 cumulative_tsn_p1, MAX_TSN)) {
6082 /* Yep it is above cum-ack */
6084 tsn = chk->rec.data.TSN_seq;
6085 if (tsn >= asoc->mapping_array_base_tsn) {
6086 gap = tsn - asoc->mapping_array_base_tsn;
6088 gap = (MAX_TSN - asoc->mapping_array_base_tsn) +
6091 asoc->size_on_reasm_queue = sctp_sbspace_sub(asoc->size_on_reasm_queue, chk->send_size);
6092 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
6093 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
6094 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
6096 sctp_m_freem(chk->data);
6099 sctp_free_a_chunk(stcb, chk);
6103 /* Ok that was fun, now we will drain all the inbound streams? */
6104 for (strmat = 0; strmat < asoc->streamincnt; strmat++) {
6105 ctl = TAILQ_FIRST(&asoc->strmin[strmat].inqueue);
6107 nctl = TAILQ_NEXT(ctl, next);
6108 if (compare_with_wrap(ctl->sinfo_tsn,
6109 cumulative_tsn_p1, MAX_TSN)) {
6110 /* Yep it is above cum-ack */
6112 tsn = ctl->sinfo_tsn;
6113 if (tsn >= asoc->mapping_array_base_tsn) {
6115 asoc->mapping_array_base_tsn;
6118 asoc->mapping_array_base_tsn) +
6121 asoc->size_on_all_streams = sctp_sbspace_sub(asoc->size_on_all_streams, ctl->length);
6122 sctp_ucount_decr(asoc->cnt_on_all_streams);
6124 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array,
6126 TAILQ_REMOVE(&asoc->strmin[strmat].inqueue,
6129 sctp_m_freem(ctl->data);
6132 sctp_free_remote_addr(ctl->whoFrom);
6133 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_readq, ctl);
6134 SCTP_DECR_READQ_COUNT();
6140 * Question, should we go through the delivery queue? The only
6141 * reason things are on here is the app not reading OR a p-d-api up.
6142 * An attacker COULD send enough in to initiate the PD-API and then
6143 * send a bunch of stuff to other streams... these would wind up on
6144 * the delivery queue.. and then we would not get to them. But in
6145 * order to do this I then have to back-track and un-deliver
6146 * sequence numbers in streams.. el-yucko. I think for now we will
6147 * NOT look at the delivery queue and leave it to be something to
6148 * consider later. An alternative would be to abort the P-D-API with
6149 * a notification and then deliver the data.... Or another method
6150 * might be to keep track of how many times the situation occurs and
6151 * if we see a possible attack underway just abort the association.
6155 SCTPDBG(SCTP_DEBUG_PCB1, "Freed %d chunks from reneg harvest\n", cnt);
6160 * Now do we need to find a new
6161 * asoc->highest_tsn_inside_map?
6163 if (asoc->highest_tsn_inside_map >= asoc->mapping_array_base_tsn) {
6164 gap = asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn;
6166 gap = (MAX_TSN - asoc->mapping_array_base_tsn) +
6167 asoc->highest_tsn_inside_map + 1;
6169 if (gap >= (asoc->mapping_array_size << 3)) {
6171 * Something bad happened or cum-ack and high were
6172 * behind the base, but if so earlier checks should
6173 * have found NO data... wierd... we will start at
6174 * end of mapping array.
6176 SCTP_PRINTF("Gap was larger than array?? %d set to max:%d maparraymax:%x\n",
6178 (int)(asoc->mapping_array_size << 3),
6179 (int)asoc->highest_tsn_inside_map);
6180 gap = asoc->mapping_array_size << 3;
6183 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
6184 /* found the new highest */
6185 asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn + gap;
6191 /* Nothing left in map */
6192 memset(asoc->mapping_array, 0, asoc->mapping_array_size);
6193 asoc->mapping_array_base_tsn = asoc->cumulative_tsn + 1;
6194 asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
6196 asoc->last_revoke_count = cnt;
6197 (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
6198 /* sa_ignore NO_NULL_CHK */
6199 sctp_send_sack(stcb);
6200 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_DRAIN, SCTP_SO_NOT_LOCKED);
6201 reneged_asoc_ids[reneged_at] = sctp_get_associd(stcb);
6205 * Another issue, in un-setting the TSN's in the mapping array we
6206 * DID NOT adjust the higest_tsn marker. This will cause one of two
6207 * things to occur. It may cause us to do extra work in checking for
6208 * our mapping array movement. More importantly it may cause us to
6209 * SACK every datagram. This may not be a bad thing though since we
6210 * will recover once we get our cum-ack above and all this stuff we
6219 * We must walk the PCB lists for ALL associations here. The system
6220 * is LOW on MBUF's and needs help. This is where reneging will
6221 * occur. We really hope this does NOT happen!
6223 struct sctp_inpcb *inp;
6224 struct sctp_tcb *stcb;
6226 SCTP_INP_INFO_RLOCK();
6227 LIST_FOREACH(inp, &sctppcbinfo.listhead, sctp_list) {
6228 /* For each endpoint */
6229 SCTP_INP_RLOCK(inp);
6230 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6231 /* For each association */
6232 SCTP_TCB_LOCK(stcb);
6233 sctp_drain_mbufs(inp, stcb);
6234 SCTP_TCB_UNLOCK(stcb);
6236 SCTP_INP_RUNLOCK(inp);
6238 SCTP_INP_INFO_RUNLOCK();
6242 * start a new iterator
6243 * iterates through all endpoints and associations based on the pcb_state
6244 * flags and asoc_state. "af" (mandatory) is executed for all matching
6245 * assocs and "ef" (optional) is executed when the iterator completes.
6246 * "inpf" (optional) is executed for each new endpoint as it is being
6247 * iterated through. inpe (optional) is called when the inp completes
6248 * its way through all the stcbs.
6251 sctp_initiate_iterator(inp_func inpf,
6255 uint32_t pcb_features,
6256 uint32_t asoc_state,
6260 struct sctp_inpcb *s_inp,
6261 uint8_t chunk_output_off)
6263 struct sctp_iterator *it = NULL;
6268 SCTP_MALLOC(it, struct sctp_iterator *, sizeof(struct sctp_iterator),
6271 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
6274 memset(it, 0, sizeof(*it));
6275 it->function_assoc = af;
6276 it->function_inp = inpf;
6278 it->done_current_ep = 0;
6280 it->done_current_ep = 1;
6281 it->function_atend = ef;
6284 it->pcb_flags = pcb_state;
6285 it->pcb_features = pcb_features;
6286 it->asoc_state = asoc_state;
6287 it->function_inp_end = inpe;
6288 it->no_chunk_output = chunk_output_off;
6291 it->iterator_flags = SCTP_ITERATOR_DO_SINGLE_INP;
6293 SCTP_INP_INFO_RLOCK();
6294 it->inp = LIST_FIRST(&sctppcbinfo.listhead);
6296 SCTP_INP_INFO_RUNLOCK();
6297 it->iterator_flags = SCTP_ITERATOR_DO_ALL_INP;
6300 SCTP_IPI_ITERATOR_WQ_LOCK();
6302 SCTP_INP_INCR_REF(it->inp);
6304 TAILQ_INSERT_TAIL(&sctppcbinfo.iteratorhead, it, sctp_nxt_itr);
6305 #if defined(SCTP_USE_THREAD_BASED_ITERATOR)
6306 if (sctppcbinfo.iterator_running == 0) {
6307 sctp_wakeup_iterator();
6309 SCTP_IPI_ITERATOR_WQ_UNLOCK();
6312 SCTP_INP_DECR_REF(it->inp);
6313 SCTP_IPI_ITERATOR_WQ_UNLOCK();
6314 /* Init the timer */
6315 SCTP_OS_TIMER_INIT(&it->tmr.timer);
6316 /* add to the list of all iterators */
6317 sctp_timer_start(SCTP_TIMER_TYPE_ITERATOR, (struct sctp_inpcb *)it,
6320 /* sa_ignore MEMLEAK {memory is put on the tailq for the iterator} */