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 */
55 SCTP6_ARE_ADDR_EQUAL(struct in6_addr *a, struct in6_addr *b)
57 struct in6_addr tmp_a, tmp_b;
59 /* use a copy of a and b */
62 in6_clearscope(&tmp_a);
63 in6_clearscope(&tmp_b);
64 return (IN6_ARE_ADDR_EQUAL(&tmp_a, &tmp_b));
68 sctp_fill_pcbinfo(struct sctp_pcbinfo *spcb)
71 * We really don't need to lock this, but I will just because it
74 SCTP_INP_INFO_RLOCK();
75 spcb->ep_count = sctppcbinfo.ipi_count_ep;
76 spcb->asoc_count = sctppcbinfo.ipi_count_asoc;
77 spcb->laddr_count = sctppcbinfo.ipi_count_laddr;
78 spcb->raddr_count = sctppcbinfo.ipi_count_raddr;
79 spcb->chk_count = sctppcbinfo.ipi_count_chunk;
80 spcb->readq_count = sctppcbinfo.ipi_count_readq;
81 spcb->stream_oque = sctppcbinfo.ipi_count_strmoq;
82 spcb->free_chunks = sctppcbinfo.ipi_free_chunks;
84 SCTP_INP_INFO_RUNLOCK();
88 * Addresses are added to VRF's (Virtual Router's). For BSD we
89 * have only the default VRF 0. We maintain a hash list of
90 * VRF's. Each VRF has its own list of sctp_ifn's. Each of
91 * these has a list of addresses. When we add a new address
92 * to a VRF we lookup the ifn/ifn_index, if the ifn does
93 * not exist we create it and add it to the list of IFN's
94 * within the VRF. Once we have the sctp_ifn, we add the
95 * address to the list. So we look something like:
98 * vrf-> ifn-> ifn -> ifn
100 * ... +--ifa-> ifa -> ifa
103 * We keep these seperate lists since the SCTP subsystem will
104 * point to these from its source address selection nets structure.
105 * When an address is deleted it does not happen right away on
106 * the SCTP side, it gets scheduled. What we do when a
107 * delete happens is immediately remove the address from
108 * the master list and decrement the refcount. As our
109 * addip iterator works through and frees the src address
110 * selection pointing to the sctp_ifa, eventually the refcount
111 * will reach 0 and we will delete it. Note that it is assumed
112 * that any locking on system level ifn/ifa is done at the
113 * caller of these functions and these routines will only
114 * lock the SCTP structures as they add or delete things.
116 * Other notes on VRF concepts.
117 * - An endpoint can be in multiple VRF's
118 * - An association lives within a VRF and only one VRF.
119 * - Any incoming packet we can deduce the VRF for by
120 * looking at the mbuf/pak inbound (for BSD its VRF=0 :D)
121 * - Any downward send call or connect call must supply the
122 * VRF via ancillary data or via some sort of set default
123 * VRF socket option call (again for BSD no brainer since
124 * the VRF is always 0).
125 * - An endpoint may add multiple VRF's to it.
126 * - Listening sockets can accept associations in any
127 * of the VRF's they are in but the assoc will end up
128 * in only one VRF (gotten from the packet or connect/send).
133 sctp_allocate_vrf(int vrf_id)
135 struct sctp_vrf *vrf = NULL;
136 struct sctp_vrflist *bucket;
138 /* First allocate the VRF structure */
139 vrf = sctp_find_vrf(vrf_id);
141 /* Already allocated */
144 SCTP_MALLOC(vrf, struct sctp_vrf *, sizeof(struct sctp_vrf),
149 panic("No memory for VRF:%d", vrf_id);
154 memset(vrf, 0, sizeof(struct sctp_vrf));
155 vrf->vrf_id = vrf_id;
156 LIST_INIT(&vrf->ifnlist);
157 vrf->total_ifa_count = 0;
158 /* Init the HASH of addresses */
159 vrf->vrf_addr_hash = SCTP_HASH_INIT(SCTP_VRF_ADDR_HASH_SIZE,
160 &vrf->vrf_addr_hashmark);
161 if (vrf->vrf_addr_hash == NULL) {
164 panic("No memory for VRF:%d", vrf_id);
169 vrf->vrf_ifn_hash = SCTP_HASH_INIT(SCTP_VRF_IFN_HASH_SIZE,
170 &vrf->vrf_ifn_hashmark);
171 if (vrf->vrf_ifn_hash == NULL) {
174 panic("No memory for VRF:%d", vrf_id);
176 SCTP_HASH_FREE(vrf->vrf_addr_hash, vrf->vrf_addr_hashmark);
180 /* Add it to the hash table */
181 bucket = &sctppcbinfo.sctp_vrfhash[(vrf_id & sctppcbinfo.hashvrfmark)];
182 LIST_INSERT_HEAD(bucket, vrf, next_vrf);
183 atomic_add_int(&sctppcbinfo.ipi_count_vrfs, 1);
189 sctp_find_ifn(struct sctp_vrf *vrf, void *ifn, uint32_t ifn_index)
191 struct sctp_ifn *sctp_ifnp;
192 struct sctp_ifnlist *hash_ifn_head;
195 * We assume the lock is held for the addresses if thats wrong
196 * problems could occur :-)
198 hash_ifn_head = &vrf->vrf_ifn_hash[(ifn_index & vrf->vrf_ifn_hashmark)];
199 LIST_FOREACH(sctp_ifnp, hash_ifn_head, next_bucket) {
200 if (sctp_ifnp->ifn_index == ifn_index) {
203 if (sctp_ifnp->ifn_p && ifn && (sctp_ifnp->ifn_p == ifn)) {
213 sctp_find_vrf(uint32_t vrf_id)
215 struct sctp_vrflist *bucket;
216 struct sctp_vrf *liste;
218 bucket = &sctppcbinfo.sctp_vrfhash[(vrf_id & sctppcbinfo.hashvrfmark)];
219 LIST_FOREACH(liste, bucket, next_vrf) {
220 if (vrf_id == liste->vrf_id) {
229 sctp_free_ifn(struct sctp_ifn *sctp_ifnp)
233 ret = atomic_fetchadd_int(&sctp_ifnp->refcount, -1);
235 /* We zero'd the count */
236 SCTP_FREE(sctp_ifnp);
237 atomic_subtract_int(&sctppcbinfo.ipi_count_ifns, 1);
242 sctp_update_ifn_mtu(uint32_t vrf_id, uint32_t ifn_index, uint32_t mtu)
244 struct sctp_ifn *sctp_ifnp;
245 struct sctp_vrf *vrf;
247 vrf = sctp_find_vrf(vrf_id);
250 sctp_ifnp = sctp_find_ifn(vrf, (void *)NULL, ifn_index);
251 if (sctp_ifnp != NULL) {
252 sctp_ifnp->ifn_mtu = mtu;
258 sctp_free_ifa(struct sctp_ifa *sctp_ifap)
262 ret = atomic_fetchadd_int(&sctp_ifap->refcount, -1);
264 /* We zero'd the count */
265 SCTP_FREE(sctp_ifap);
266 atomic_subtract_int(&sctppcbinfo.ipi_count_ifas, 1);
271 sctp_delete_ifn(struct sctp_ifn *sctp_ifnp, int hold_addr_lock)
273 struct sctp_ifn *found;
275 found = sctp_find_ifn(sctp_ifnp->vrf, sctp_ifnp->ifn_p, sctp_ifnp->ifn_index);
277 /* Not in the list.. sorry */
280 if (hold_addr_lock == 0)
281 SCTP_IPI_ADDR_LOCK();
282 LIST_REMOVE(sctp_ifnp, next_bucket);
283 LIST_REMOVE(sctp_ifnp, next_ifn);
284 if (hold_addr_lock == 0)
285 SCTP_IPI_ADDR_UNLOCK();
286 /* Take away the reference, and possibly free it */
287 sctp_free_ifn(sctp_ifnp);
292 sctp_add_addr_to_vrf(uint32_t vrf_id, void *ifn, uint32_t ifn_index,
293 uint32_t ifn_type, const char *if_name,
294 void *ifa, struct sockaddr *addr, uint32_t ifa_flags,
297 struct sctp_vrf *vrf;
298 struct sctp_ifn *sctp_ifnp = NULL;
299 struct sctp_ifa *sctp_ifap = NULL;
300 struct sctp_ifalist *hash_addr_head;
301 struct sctp_ifnlist *hash_ifn_head;
302 uint32_t hash_of_addr;
304 /* How granular do we need the locks to be here? */
305 SCTP_IPI_ADDR_LOCK();
306 vrf = sctp_find_vrf(vrf_id);
308 vrf = sctp_allocate_vrf(vrf_id);
310 SCTP_IPI_ADDR_UNLOCK();
314 sctp_ifnp = sctp_find_ifn(vrf, ifn, ifn_index);
315 if (sctp_ifnp == NULL) {
317 * build one and add it, can't hold lock until after malloc
320 SCTP_IPI_ADDR_UNLOCK();
321 SCTP_MALLOC(sctp_ifnp, struct sctp_ifn *, sizeof(struct sctp_ifn), "SCTP_IFN");
322 if (sctp_ifnp == NULL) {
324 panic("No memory for IFN:%u", sctp_ifnp->ifn_index);
328 sctp_ifnp->ifn_index = ifn_index;
329 sctp_ifnp->ifn_p = ifn;
330 sctp_ifnp->ifn_type = ifn_type;
331 sctp_ifnp->ifa_count = 0;
332 sctp_ifnp->refcount = 1;
333 sctp_ifnp->vrf = vrf;
334 sctp_ifnp->ifn_mtu = SCTP_GATHER_MTU_FROM_IFN_INFO(ifn, ifn_index);
335 if (if_name != NULL) {
336 memcpy(sctp_ifnp->ifn_name, if_name, SCTP_IFNAMSIZ);
338 memcpy(sctp_ifnp->ifn_name, "unknown", min(7, SCTP_IFNAMSIZ));
340 hash_ifn_head = &vrf->vrf_ifn_hash[(ifn_index & vrf->vrf_ifn_hashmark)];
341 LIST_INIT(&sctp_ifnp->ifalist);
342 SCTP_IPI_ADDR_LOCK();
343 LIST_INSERT_HEAD(hash_ifn_head, sctp_ifnp, next_bucket);
344 LIST_INSERT_HEAD(&vrf->ifnlist, sctp_ifnp, next_ifn);
345 atomic_add_int(&sctppcbinfo.ipi_count_ifns, 1);
347 sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, 1);
349 /* Hmm, it already exists? */
350 if ((sctp_ifap->ifn_p) &&
351 (sctp_ifap->ifn_p->ifn_index == ifn_index)) {
352 if (sctp_ifap->localifa_flags & SCTP_BEING_DELETED) {
353 /* easy to solve, just switch back to active */
354 sctp_ifap->localifa_flags = SCTP_ADDR_VALID;
355 sctp_ifap->ifn_p = sctp_ifnp;
357 SCTP_IPI_ADDR_UNLOCK();
360 goto exit_stage_left;
363 if (sctp_ifap->ifn_p) {
365 * The first IFN gets the address,
366 * duplicates are ignored.
368 goto exit_stage_left;
370 /* repair ifnp which was NULL ? */
371 sctp_ifap->localifa_flags = SCTP_ADDR_VALID;
372 sctp_ifap->ifn_p = sctp_ifnp;
373 atomic_add_int(&sctp_ifap->ifn_p->refcount, 1);
375 goto exit_stage_left;
378 SCTP_IPI_ADDR_UNLOCK();
379 SCTP_MALLOC(sctp_ifap, struct sctp_ifa *, sizeof(struct sctp_ifa), "SCTP_IFA");
380 if (sctp_ifap == NULL) {
382 panic("No memory for IFA");
386 memset(sctp_ifap, 0, sizeof(struct sctp_ifa));
387 sctp_ifap->ifn_p = sctp_ifnp;
388 atomic_add_int(&sctp_ifnp->refcount, 1);
390 sctp_ifap->ifa = ifa;
391 memcpy(&sctp_ifap->address, addr, addr->sa_len);
392 sctp_ifap->localifa_flags = SCTP_ADDR_VALID | SCTP_ADDR_DEFER_USE;
393 sctp_ifap->flags = ifa_flags;
395 if (sctp_ifap->address.sa.sa_family == AF_INET) {
396 struct sockaddr_in *sin;
398 sin = (struct sockaddr_in *)&sctp_ifap->address.sin;
399 if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) ||
400 (IN4_ISLOOPBACK_ADDRESS(&sin->sin_addr))) {
401 sctp_ifap->src_is_loop = 1;
403 if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
404 sctp_ifap->src_is_priv = 1;
406 } else if (sctp_ifap->address.sa.sa_family == AF_INET6) {
407 /* ok to use deprecated addresses? */
408 struct sockaddr_in6 *sin6;
410 sin6 = (struct sockaddr_in6 *)&sctp_ifap->address.sin6;
411 if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) ||
412 (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))) {
413 sctp_ifap->src_is_loop = 1;
415 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
416 sctp_ifap->src_is_priv = 1;
419 hash_of_addr = sctp_get_ifa_hash_val(&sctp_ifap->address.sa);
421 if ((sctp_ifap->src_is_priv == 0) &&
422 (sctp_ifap->src_is_loop == 0)) {
423 sctp_ifap->src_is_glob = 1;
425 SCTP_IPI_ADDR_LOCK();
426 hash_addr_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)];
427 LIST_INSERT_HEAD(hash_addr_head, sctp_ifap, next_bucket);
428 sctp_ifap->refcount = 1;
429 LIST_INSERT_HEAD(&sctp_ifnp->ifalist, sctp_ifap, next_ifa);
430 sctp_ifnp->ifa_count++;
431 vrf->total_ifa_count++;
432 atomic_add_int(&sctppcbinfo.ipi_count_ifas, 1);
433 SCTP_IPI_ADDR_UNLOCK();
436 * Bump up the refcount so that when the timer completes it
437 * will drop back down.
439 struct sctp_laddr *wi;
441 atomic_add_int(&sctp_ifap->refcount, 1);
442 wi = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_laddr, struct sctp_laddr);
445 * Gak, what can we do? We have lost an address
446 * change can you say HOSED?
448 SCTPDBG(SCTP_DEBUG_PCB1, "Lost and address change ???\n");
449 /* Opps, must decrement the count */
450 sctp_del_addr_from_vrf(vrf_id, addr, ifn_index);
453 SCTP_INCR_LADDR_COUNT();
454 bzero(wi, sizeof(*wi));
456 wi->action = SCTP_ADD_IP_ADDRESS;
457 SCTP_IPI_ITERATOR_WQ_LOCK();
459 * Should this really be a tailq? As it is we will process
460 * the newest first :-0
462 LIST_INSERT_HEAD(&sctppcbinfo.addr_wq, wi, sctp_nxt_addr);
463 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
464 (struct sctp_inpcb *)NULL,
465 (struct sctp_tcb *)NULL,
466 (struct sctp_nets *)NULL);
467 SCTP_IPI_ITERATOR_WQ_UNLOCK();
469 /* it's ready for use */
470 sctp_ifap->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
476 sctp_del_addr_from_vrf(uint32_t vrf_id, struct sockaddr *addr,
479 struct sctp_vrf *vrf;
480 struct sctp_ifa *sctp_ifap = NULL;
482 SCTP_IPI_ADDR_LOCK();
484 vrf = sctp_find_vrf(vrf_id);
486 SCTP_PRINTF("Can't find vrf_id:%d\n", vrf_id);
489 sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, 1);
491 sctp_ifap->localifa_flags &= SCTP_ADDR_VALID;
492 sctp_ifap->localifa_flags |= SCTP_BEING_DELETED;
493 vrf->total_ifa_count--;
494 LIST_REMOVE(sctp_ifap, next_bucket);
495 LIST_REMOVE(sctp_ifap, next_ifa);
496 if (sctp_ifap->ifn_p) {
497 sctp_ifap->ifn_p->ifa_count--;
498 if (SCTP_LIST_EMPTY(&sctp_ifap->ifn_p->ifalist)) {
499 sctp_delete_ifn(sctp_ifap->ifn_p, 1);
501 sctp_free_ifn(sctp_ifap->ifn_p);
502 sctp_ifap->ifn_p = NULL;
507 SCTPDBG(SCTP_DEBUG_PCB1, "Del Addr-ifn:%d Could not find address:",
509 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr);
514 SCTP_IPI_ADDR_UNLOCK();
516 struct sctp_laddr *wi;
518 wi = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_laddr, struct sctp_laddr);
521 * Gak, what can we do? We have lost an address
522 * change can you say HOSED?
524 SCTPDBG(SCTP_DEBUG_PCB1, "Lost and address change ???\n");
526 /* Opps, must decrement the count */
527 sctp_free_ifa(sctp_ifap);
530 SCTP_INCR_LADDR_COUNT();
531 bzero(wi, sizeof(*wi));
533 wi->action = SCTP_DEL_IP_ADDRESS;
534 SCTP_IPI_ITERATOR_WQ_LOCK();
536 * Should this really be a tailq? As it is we will process
537 * the newest first :-0
539 LIST_INSERT_HEAD(&sctppcbinfo.addr_wq, wi, sctp_nxt_addr);
540 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
541 (struct sctp_inpcb *)NULL,
542 (struct sctp_tcb *)NULL,
543 (struct sctp_nets *)NULL);
544 SCTP_IPI_ITERATOR_WQ_UNLOCK();
551 static struct sctp_tcb *
552 sctp_tcb_special_locate(struct sctp_inpcb **inp_p, struct sockaddr *from,
553 struct sockaddr *to, struct sctp_nets **netp, uint32_t vrf_id)
555 /**** ASSUMSES THE CALLER holds the INP_INFO_RLOCK */
557 * If we support the TCP model, then we must now dig through to see
558 * if we can find our endpoint in the list of tcp ep's.
560 uint16_t lport, rport;
561 struct sctppcbhead *ephead;
562 struct sctp_inpcb *inp;
563 struct sctp_laddr *laddr;
564 struct sctp_tcb *stcb;
565 struct sctp_nets *net;
567 if ((to == NULL) || (from == NULL)) {
570 if (to->sa_family == AF_INET && from->sa_family == AF_INET) {
571 lport = ((struct sockaddr_in *)to)->sin_port;
572 rport = ((struct sockaddr_in *)from)->sin_port;
573 } else if (to->sa_family == AF_INET6 && from->sa_family == AF_INET6) {
574 lport = ((struct sockaddr_in6 *)to)->sin6_port;
575 rport = ((struct sockaddr_in6 *)from)->sin6_port;
579 ephead = &sctppcbinfo.sctp_tcpephash[SCTP_PCBHASH_ALLADDR(
580 (lport + rport), sctppcbinfo.hashtcpmark)];
582 * Ok now for each of the guys in this bucket we must look and see:
583 * - Does the remote port match. - Does there single association's
584 * addresses match this address (to). If so we update p_ep to point
585 * to this ep and return the tcb from it.
587 LIST_FOREACH(inp, ephead, sctp_hash) {
589 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
590 SCTP_INP_RUNLOCK(inp);
593 if (lport != inp->sctp_lport) {
594 SCTP_INP_RUNLOCK(inp);
597 if (inp->def_vrf_id != vrf_id) {
598 SCTP_INP_RUNLOCK(inp);
601 /* check to see if the ep has one of the addresses */
602 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
603 /* We are NOT bound all, so look further */
606 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
608 if (laddr->ifa == NULL) {
609 SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n", __FUNCTION__);
612 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
613 SCTPDBG(SCTP_DEBUG_PCB1, "ifa being deleted\n");
616 if (laddr->ifa->address.sa.sa_family ==
618 /* see if it matches */
619 struct sockaddr_in *intf_addr, *sin;
621 intf_addr = &laddr->ifa->address.sin;
622 sin = (struct sockaddr_in *)to;
623 if (from->sa_family == AF_INET) {
624 if (sin->sin_addr.s_addr ==
625 intf_addr->sin_addr.s_addr) {
630 struct sockaddr_in6 *intf_addr6;
631 struct sockaddr_in6 *sin6;
633 sin6 = (struct sockaddr_in6 *)
635 intf_addr6 = &laddr->ifa->address.sin6;
637 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
638 &intf_addr6->sin6_addr)) {
646 /* This endpoint does not have this address */
647 SCTP_INP_RUNLOCK(inp);
652 * Ok if we hit here the ep has the address, does it hold
656 stcb = LIST_FIRST(&inp->sctp_asoc_list);
658 SCTP_INP_RUNLOCK(inp);
662 if (stcb->rport != rport) {
663 /* remote port does not match. */
664 SCTP_TCB_UNLOCK(stcb);
665 SCTP_INP_RUNLOCK(inp);
668 /* Does this TCB have a matching address? */
669 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
671 if (net->ro._l_addr.sa.sa_family != from->sa_family) {
672 /* not the same family, can't be a match */
675 if (from->sa_family == AF_INET) {
676 struct sockaddr_in *sin, *rsin;
678 sin = (struct sockaddr_in *)&net->ro._l_addr;
679 rsin = (struct sockaddr_in *)from;
680 if (sin->sin_addr.s_addr ==
681 rsin->sin_addr.s_addr) {
686 /* Update the endpoint pointer */
688 SCTP_INP_RUNLOCK(inp);
692 struct sockaddr_in6 *sin6, *rsin6;
694 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
695 rsin6 = (struct sockaddr_in6 *)from;
696 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
697 &rsin6->sin6_addr)) {
702 /* Update the endpoint pointer */
704 SCTP_INP_RUNLOCK(inp);
709 SCTP_TCB_UNLOCK(stcb);
710 SCTP_INP_RUNLOCK(inp);
718 * 1) If I return a NULL you must decrement any INP ref cnt. 2) If I find an
719 * stcb, both will be locked (locked_tcb and stcb) but decrement will be done
720 * (if locked == NULL). 3) Decrement happens on return ONLY if locked ==
725 sctp_findassociation_ep_addr(struct sctp_inpcb **inp_p, struct sockaddr *remote,
726 struct sctp_nets **netp, struct sockaddr *local, struct sctp_tcb *locked_tcb)
728 struct sctpasochead *head;
729 struct sctp_inpcb *inp;
730 struct sctp_tcb *stcb = NULL;
731 struct sctp_nets *net;
735 if (remote->sa_family == AF_INET) {
736 rport = (((struct sockaddr_in *)remote)->sin_port);
737 } else if (remote->sa_family == AF_INET6) {
738 rport = (((struct sockaddr_in6 *)remote)->sin6_port);
744 * UN-lock so we can do proper locking here this occurs when
745 * called from load_addresses_from_init.
747 SCTP_TCB_UNLOCK(locked_tcb);
749 SCTP_INP_INFO_RLOCK();
750 if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
752 * Now either this guy is our listener or it's the
753 * connector. If it is the one that issued the connect, then
754 * it's only chance is to be the first TCB in the list. If
755 * it is the acceptor, then do the special_lookup to hash
756 * and find the real inp.
758 if ((inp->sctp_socket) && (inp->sctp_socket->so_qlimit)) {
759 /* to is peer addr, from is my addr */
760 stcb = sctp_tcb_special_locate(inp_p, remote, local,
761 netp, inp->def_vrf_id);
762 if ((stcb != NULL) && (locked_tcb == NULL)) {
763 /* we have a locked tcb, lower refcount */
765 SCTP_INP_DECR_REF(inp);
766 SCTP_INP_WUNLOCK(inp);
768 if ((locked_tcb != NULL) && (locked_tcb != stcb)) {
769 SCTP_INP_RLOCK(locked_tcb->sctp_ep);
770 SCTP_TCB_LOCK(locked_tcb);
771 SCTP_INP_RUNLOCK(locked_tcb->sctp_ep);
773 SCTP_INP_INFO_RUNLOCK();
777 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
780 stcb = LIST_FIRST(&inp->sctp_asoc_list);
785 if (stcb->rport != rport) {
786 /* remote port does not match. */
787 SCTP_TCB_UNLOCK(stcb);
790 /* now look at the list of remote addresses */
791 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
793 if (net == (TAILQ_NEXT(net, sctp_next))) {
794 panic("Corrupt net list");
797 if (net->ro._l_addr.sa.sa_family !=
799 /* not the same family */
802 if (remote->sa_family == AF_INET) {
803 struct sockaddr_in *sin, *rsin;
805 sin = (struct sockaddr_in *)
807 rsin = (struct sockaddr_in *)remote;
808 if (sin->sin_addr.s_addr ==
809 rsin->sin_addr.s_addr) {
814 if (locked_tcb == NULL) {
815 SCTP_INP_DECR_REF(inp);
816 } else if (locked_tcb != stcb) {
817 SCTP_TCB_LOCK(locked_tcb);
819 SCTP_INP_WUNLOCK(inp);
820 SCTP_INP_INFO_RUNLOCK();
823 } else if (remote->sa_family == AF_INET6) {
824 struct sockaddr_in6 *sin6, *rsin6;
826 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
827 rsin6 = (struct sockaddr_in6 *)remote;
828 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
829 &rsin6->sin6_addr)) {
834 if (locked_tcb == NULL) {
835 SCTP_INP_DECR_REF(inp);
836 } else if (locked_tcb != stcb) {
837 SCTP_TCB_LOCK(locked_tcb);
839 SCTP_INP_WUNLOCK(inp);
840 SCTP_INP_INFO_RUNLOCK();
845 SCTP_TCB_UNLOCK(stcb);
849 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
852 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(rport,
853 inp->sctp_hashmark)];
857 LIST_FOREACH(stcb, head, sctp_tcbhash) {
858 if (stcb->rport != rport) {
859 /* remote port does not match */
862 /* now look at the list of remote addresses */
864 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
866 if (net == (TAILQ_NEXT(net, sctp_next))) {
867 panic("Corrupt net list");
870 if (net->ro._l_addr.sa.sa_family !=
872 /* not the same family */
875 if (remote->sa_family == AF_INET) {
876 struct sockaddr_in *sin, *rsin;
878 sin = (struct sockaddr_in *)
880 rsin = (struct sockaddr_in *)remote;
881 if (sin->sin_addr.s_addr ==
882 rsin->sin_addr.s_addr) {
887 if (locked_tcb == NULL) {
888 SCTP_INP_DECR_REF(inp);
889 } else if (locked_tcb != stcb) {
890 SCTP_TCB_LOCK(locked_tcb);
892 SCTP_INP_WUNLOCK(inp);
893 SCTP_INP_INFO_RUNLOCK();
896 } else if (remote->sa_family == AF_INET6) {
897 struct sockaddr_in6 *sin6, *rsin6;
899 sin6 = (struct sockaddr_in6 *)
901 rsin6 = (struct sockaddr_in6 *)remote;
902 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
903 &rsin6->sin6_addr)) {
908 if (locked_tcb == NULL) {
909 SCTP_INP_DECR_REF(inp);
910 } else if (locked_tcb != stcb) {
911 SCTP_TCB_LOCK(locked_tcb);
913 SCTP_INP_WUNLOCK(inp);
914 SCTP_INP_INFO_RUNLOCK();
919 SCTP_TCB_UNLOCK(stcb);
923 /* clean up for returning null */
925 SCTP_TCB_LOCK(locked_tcb);
927 SCTP_INP_WUNLOCK(inp);
928 SCTP_INP_INFO_RUNLOCK();
934 * Find an association for a specific endpoint using the association id given
935 * out in the COMM_UP notification
939 sctp_findassociation_ep_asocid(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock)
942 * Use my the assoc_id to find a endpoint
944 struct sctpasochead *head;
945 struct sctp_tcb *stcb;
948 if (asoc_id == 0 || inp == NULL) {
951 SCTP_INP_INFO_RLOCK();
952 id = (uint32_t) asoc_id;
953 head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(id,
954 sctppcbinfo.hashasocmark)];
956 /* invalid id TSNH */
957 SCTP_INP_INFO_RUNLOCK();
960 LIST_FOREACH(stcb, head, sctp_asocs) {
961 SCTP_INP_RLOCK(stcb->sctp_ep);
962 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
963 SCTP_INP_RUNLOCK(stcb->sctp_ep);
964 SCTP_INP_INFO_RUNLOCK();
967 if (stcb->asoc.assoc_id == id) {
969 if (inp != stcb->sctp_ep) {
971 * some other guy has the same id active (id
974 SCTP_INP_RUNLOCK(stcb->sctp_ep);
980 SCTP_INP_RUNLOCK(stcb->sctp_ep);
981 SCTP_INP_INFO_RUNLOCK();
984 SCTP_INP_RUNLOCK(stcb->sctp_ep);
986 /* Ok if we missed here, lets try the restart hash */
987 head = &sctppcbinfo.sctp_restarthash[SCTP_PCBHASH_ASOC(id, sctppcbinfo.hashrestartmark)];
989 /* invalid id TSNH */
990 SCTP_INP_INFO_RUNLOCK();
993 LIST_FOREACH(stcb, head, sctp_tcbrestarhash) {
994 SCTP_INP_RLOCK(stcb->sctp_ep);
995 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
996 SCTP_INP_RUNLOCK(stcb->sctp_ep);
997 SCTP_INP_INFO_RUNLOCK();
1000 SCTP_TCB_LOCK(stcb);
1001 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1002 if (stcb->asoc.assoc_id == id) {
1004 if (inp != stcb->sctp_ep) {
1006 * some other guy has the same id active (id
1009 SCTP_TCB_UNLOCK(stcb);
1012 SCTP_INP_INFO_RUNLOCK();
1015 SCTP_TCB_UNLOCK(stcb);
1017 SCTP_INP_INFO_RUNLOCK();
1022 static struct sctp_inpcb *
1023 sctp_endpoint_probe(struct sockaddr *nam, struct sctppcbhead *head,
1024 uint16_t lport, uint32_t vrf_id)
1026 struct sctp_inpcb *inp;
1027 struct sockaddr_in *sin;
1028 struct sockaddr_in6 *sin6;
1029 struct sctp_laddr *laddr;
1033 * Endpoing probe expects that the INP_INFO is locked.
1035 if (nam->sa_family == AF_INET) {
1036 sin = (struct sockaddr_in *)nam;
1038 } else if (nam->sa_family == AF_INET6) {
1039 sin6 = (struct sockaddr_in6 *)nam;
1042 /* unsupported family */
1047 LIST_FOREACH(inp, head, sctp_hash) {
1048 SCTP_INP_RLOCK(inp);
1049 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1050 SCTP_INP_RUNLOCK(inp);
1053 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) &&
1054 (inp->sctp_lport == lport)) {
1056 if ((nam->sa_family == AF_INET) &&
1057 (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1058 SCTP_IPV6_V6ONLY(inp)) {
1059 /* IPv4 on a IPv6 socket with ONLY IPv6 set */
1060 SCTP_INP_RUNLOCK(inp);
1063 /* A V6 address and the endpoint is NOT bound V6 */
1064 if (nam->sa_family == AF_INET6 &&
1065 (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
1066 SCTP_INP_RUNLOCK(inp);
1069 /* does a VRF id match? */
1071 if (inp->def_vrf_id == vrf_id)
1074 SCTP_INP_RUNLOCK(inp);
1079 SCTP_INP_RUNLOCK(inp);
1082 if ((nam->sa_family == AF_INET) &&
1083 (sin->sin_addr.s_addr == INADDR_ANY)) {
1084 /* Can't hunt for one that has no address specified */
1086 } else if ((nam->sa_family == AF_INET6) &&
1087 (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))) {
1088 /* Can't hunt for one that has no address specified */
1092 * ok, not bound to all so see if we can find a EP bound to this
1095 LIST_FOREACH(inp, head, sctp_hash) {
1096 SCTP_INP_RLOCK(inp);
1097 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1098 SCTP_INP_RUNLOCK(inp);
1101 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)) {
1102 SCTP_INP_RUNLOCK(inp);
1106 * Ok this could be a likely candidate, look at all of its
1109 if (inp->sctp_lport != lport) {
1110 SCTP_INP_RUNLOCK(inp);
1113 /* does a VRF id match? */
1115 if (inp->def_vrf_id == vrf_id)
1119 SCTP_INP_RUNLOCK(inp);
1122 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1123 if (laddr->ifa == NULL) {
1124 SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n",
1128 SCTPDBG(SCTP_DEBUG_PCB1, "Ok laddr->ifa:%p is possible, ",
1130 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
1131 SCTPDBG(SCTP_DEBUG_PCB1, "Huh IFA being deleted\n");
1134 if (laddr->ifa->address.sa.sa_family == nam->sa_family) {
1135 /* possible, see if it matches */
1136 struct sockaddr_in *intf_addr;
1138 intf_addr = &laddr->ifa->address.sin;
1139 if (nam->sa_family == AF_INET) {
1140 if (sin->sin_addr.s_addr ==
1141 intf_addr->sin_addr.s_addr) {
1142 SCTP_INP_RUNLOCK(inp);
1145 } else if (nam->sa_family == AF_INET6) {
1146 struct sockaddr_in6 *intf_addr6;
1148 intf_addr6 = &laddr->ifa->address.sin6;
1149 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
1150 &intf_addr6->sin6_addr)) {
1151 SCTP_INP_RUNLOCK(inp);
1157 SCTP_INP_RUNLOCK(inp);
1164 sctp_pcb_findep(struct sockaddr *nam, int find_tcp_pool, int have_lock,
1168 * First we check the hash table to see if someone has this port
1169 * bound with just the port.
1171 struct sctp_inpcb *inp;
1172 struct sctppcbhead *head;
1173 struct sockaddr_in *sin;
1174 struct sockaddr_in6 *sin6;
1177 if (nam->sa_family == AF_INET) {
1178 sin = (struct sockaddr_in *)nam;
1179 lport = ((struct sockaddr_in *)nam)->sin_port;
1180 } else if (nam->sa_family == AF_INET6) {
1181 sin6 = (struct sockaddr_in6 *)nam;
1182 lport = ((struct sockaddr_in6 *)nam)->sin6_port;
1184 /* unsupported family */
1188 * I could cheat here and just cast to one of the types but we will
1189 * do it right. It also provides the check against an Unsupported
1192 /* Find the head of the ALLADDR chain */
1193 if (have_lock == 0) {
1194 SCTP_INP_INFO_RLOCK();
1197 head = &sctppcbinfo.sctp_ephash[SCTP_PCBHASH_ALLADDR(lport,
1198 sctppcbinfo.hashmark)];
1199 inp = sctp_endpoint_probe(nam, head, lport, vrf_id);
1202 * If the TCP model exists it could be that the main listening
1203 * endpoint is gone but there exists a connected socket for this guy
1204 * yet. If so we can return the first one that we find. This may NOT
1205 * be the correct one but the sctp_findassociation_ep_addr has
1206 * further code to look at all TCP models.
1208 if (inp == NULL && find_tcp_pool) {
1211 for (i = 0; i < sctppcbinfo.hashtblsize; i++) {
1213 * This is real gross, but we do NOT have a remote
1214 * port at this point depending on who is calling.
1215 * We must therefore look for ANY one that matches
1218 head = &sctppcbinfo.sctp_tcpephash[i];
1219 if (LIST_FIRST(head)) {
1220 inp = sctp_endpoint_probe(nam, head, lport, vrf_id);
1229 SCTP_INP_INCR_REF(inp);
1231 if (have_lock == 0) {
1232 SCTP_INP_INFO_RUNLOCK();
1238 * Find an association for an endpoint with the pointer to whom you want to
1239 * send to and the endpoint pointer. The address can be IPv4 or IPv6. We may
1240 * need to change the *to to some other struct like a mbuf...
1243 sctp_findassociation_addr_sa(struct sockaddr *to, struct sockaddr *from,
1244 struct sctp_inpcb **inp_p, struct sctp_nets **netp, int find_tcp_pool, uint32_t vrf_id)
1246 struct sctp_inpcb *inp = NULL;
1247 struct sctp_tcb *retval;
1249 SCTP_INP_INFO_RLOCK();
1250 if (find_tcp_pool) {
1251 if (inp_p != NULL) {
1252 retval = sctp_tcb_special_locate(inp_p, from, to, netp, vrf_id);
1254 retval = sctp_tcb_special_locate(&inp, from, to, netp, vrf_id);
1256 if (retval != NULL) {
1257 SCTP_INP_INFO_RUNLOCK();
1261 inp = sctp_pcb_findep(to, 0, 1, vrf_id);
1262 if (inp_p != NULL) {
1265 SCTP_INP_INFO_RUNLOCK();
1271 * ok, we have an endpoint, now lets find the assoc for it (if any)
1272 * we now place the source address or from in the to of the find
1273 * endpoint call. Since in reality this chain is used from the
1274 * inbound packet side.
1276 if (inp_p != NULL) {
1277 retval = sctp_findassociation_ep_addr(inp_p, from, netp, to, NULL);
1279 retval = sctp_findassociation_ep_addr(&inp, from, netp, to, NULL);
1286 * This routine will grub through the mbuf that is a INIT or INIT-ACK and
1287 * find all addresses that the sender has specified in any address list. Each
1288 * address will be used to lookup the TCB and see if one exits.
1290 static struct sctp_tcb *
1291 sctp_findassociation_special_addr(struct mbuf *m, int iphlen, int offset,
1292 struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp,
1293 struct sockaddr *dest)
1295 struct sockaddr_in sin4;
1296 struct sockaddr_in6 sin6;
1297 struct sctp_paramhdr *phdr, parm_buf;
1298 struct sctp_tcb *retval;
1299 uint32_t ptype, plen;
1301 memset(&sin4, 0, sizeof(sin4));
1302 memset(&sin6, 0, sizeof(sin6));
1303 sin4.sin_len = sizeof(sin4);
1304 sin4.sin_family = AF_INET;
1305 sin4.sin_port = sh->src_port;
1306 sin6.sin6_len = sizeof(sin6);
1307 sin6.sin6_family = AF_INET6;
1308 sin6.sin6_port = sh->src_port;
1311 offset += sizeof(struct sctp_init_chunk);
1313 phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
1314 while (phdr != NULL) {
1315 /* now we must see if we want the parameter */
1316 ptype = ntohs(phdr->param_type);
1317 plen = ntohs(phdr->param_length);
1321 if (ptype == SCTP_IPV4_ADDRESS &&
1322 plen == sizeof(struct sctp_ipv4addr_param)) {
1323 /* Get the rest of the address */
1324 struct sctp_ipv4addr_param ip4_parm, *p4;
1326 phdr = sctp_get_next_param(m, offset,
1327 (struct sctp_paramhdr *)&ip4_parm, min(plen, sizeof(ip4_parm)));
1331 p4 = (struct sctp_ipv4addr_param *)phdr;
1332 memcpy(&sin4.sin_addr, &p4->addr, sizeof(p4->addr));
1334 retval = sctp_findassociation_ep_addr(inp_p,
1335 (struct sockaddr *)&sin4, netp, dest, NULL);
1336 if (retval != NULL) {
1339 } else if (ptype == SCTP_IPV6_ADDRESS &&
1340 plen == sizeof(struct sctp_ipv6addr_param)) {
1341 /* Get the rest of the address */
1342 struct sctp_ipv6addr_param ip6_parm, *p6;
1344 phdr = sctp_get_next_param(m, offset,
1345 (struct sctp_paramhdr *)&ip6_parm, min(plen, sizeof(ip6_parm)));
1349 p6 = (struct sctp_ipv6addr_param *)phdr;
1350 memcpy(&sin6.sin6_addr, &p6->addr, sizeof(p6->addr));
1352 retval = sctp_findassociation_ep_addr(inp_p,
1353 (struct sockaddr *)&sin6, netp, dest, NULL);
1354 if (retval != NULL) {
1358 offset += SCTP_SIZE32(plen);
1359 phdr = sctp_get_next_param(m, offset, &parm_buf,
1366 static struct sctp_tcb *
1367 sctp_findassoc_by_vtag(struct sockaddr *from, uint32_t vtag,
1368 struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint16_t rport,
1369 uint16_t lport, int skip_src_check)
1372 * Use my vtag to hash. If we find it we then verify the source addr
1373 * is in the assoc. If all goes well we save a bit on rec of a
1376 struct sctpasochead *head;
1377 struct sctp_nets *net;
1378 struct sctp_tcb *stcb;
1382 SCTP_INP_INFO_RLOCK();
1383 head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(vtag,
1384 sctppcbinfo.hashasocmark)];
1387 SCTP_INP_INFO_RUNLOCK();
1390 LIST_FOREACH(stcb, head, sctp_asocs) {
1391 SCTP_INP_RLOCK(stcb->sctp_ep);
1392 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1393 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1396 SCTP_TCB_LOCK(stcb);
1397 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1398 if (stcb->asoc.my_vtag == vtag) {
1400 if (stcb->rport != rport) {
1402 * we could remove this if vtags are unique
1403 * across the system.
1405 SCTP_TCB_UNLOCK(stcb);
1408 if (stcb->sctp_ep->sctp_lport != lport) {
1410 * we could remove this if vtags are unique
1411 * across the system.
1413 SCTP_TCB_UNLOCK(stcb);
1416 if (skip_src_check) {
1417 *netp = NULL; /* unknown */
1419 *inp_p = stcb->sctp_ep;
1420 SCTP_INP_INFO_RUNLOCK();
1423 net = sctp_findnet(stcb, from);
1427 SCTP_STAT_INCR(sctps_vtagexpress);
1428 *inp_p = stcb->sctp_ep;
1429 SCTP_INP_INFO_RUNLOCK();
1433 * not him, this should only happen in rare
1434 * cases so I peg it.
1436 SCTP_STAT_INCR(sctps_vtagbogus);
1439 SCTP_TCB_UNLOCK(stcb);
1441 SCTP_INP_INFO_RUNLOCK();
1446 * Find an association with the pointer to the inbound IP packet. This can be
1447 * a IPv4 or IPv6 packet.
1450 sctp_findassociation_addr(struct mbuf *m, int iphlen, int offset,
1451 struct sctphdr *sh, struct sctp_chunkhdr *ch,
1452 struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id)
1456 struct sctp_tcb *retval;
1457 struct sockaddr_storage to_store, from_store;
1458 struct sockaddr *to = (struct sockaddr *)&to_store;
1459 struct sockaddr *from = (struct sockaddr *)&from_store;
1460 struct sctp_inpcb *inp;
1462 iph = mtod(m, struct ip *);
1463 if (iph->ip_v == IPVERSION) {
1465 struct sockaddr_in *from4;
1467 from4 = (struct sockaddr_in *)&from_store;
1468 bzero(from4, sizeof(*from4));
1469 from4->sin_family = AF_INET;
1470 from4->sin_len = sizeof(struct sockaddr_in);
1471 from4->sin_addr.s_addr = iph->ip_src.s_addr;
1472 from4->sin_port = sh->src_port;
1473 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
1475 struct ip6_hdr *ip6;
1476 struct sockaddr_in6 *from6;
1478 ip6 = mtod(m, struct ip6_hdr *);
1479 from6 = (struct sockaddr_in6 *)&from_store;
1480 bzero(from6, sizeof(*from6));
1481 from6->sin6_family = AF_INET6;
1482 from6->sin6_len = sizeof(struct sockaddr_in6);
1483 from6->sin6_addr = ip6->ip6_src;
1484 from6->sin6_port = sh->src_port;
1485 /* Get the scopes in properly to the sin6 addr's */
1486 /* we probably don't need these operations */
1487 (void)sa6_recoverscope(from6);
1488 sa6_embedscope(from6, ip6_use_defzone);
1490 /* Currently not supported. */
1494 /* we only go down this path if vtag is non-zero */
1495 retval = sctp_findassoc_by_vtag(from, ntohl(sh->v_tag),
1496 inp_p, netp, sh->src_port, sh->dest_port, 0);
1501 if (iph->ip_v == IPVERSION) {
1503 struct sockaddr_in *to4;
1505 to4 = (struct sockaddr_in *)&to_store;
1506 bzero(to4, sizeof(*to4));
1507 to4->sin_family = AF_INET;
1508 to4->sin_len = sizeof(struct sockaddr_in);
1509 to4->sin_addr.s_addr = iph->ip_dst.s_addr;
1510 to4->sin_port = sh->dest_port;
1511 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
1513 struct ip6_hdr *ip6;
1514 struct sockaddr_in6 *to6;
1516 ip6 = mtod(m, struct ip6_hdr *);
1517 to6 = (struct sockaddr_in6 *)&to_store;
1518 bzero(to6, sizeof(*to6));
1519 to6->sin6_family = AF_INET6;
1520 to6->sin6_len = sizeof(struct sockaddr_in6);
1521 to6->sin6_addr = ip6->ip6_dst;
1522 to6->sin6_port = sh->dest_port;
1523 /* Get the scopes in properly to the sin6 addr's */
1524 /* we probably don't need these operations */
1525 (void)sa6_recoverscope(to6);
1526 sa6_embedscope(to6, ip6_use_defzone);
1529 if ((ch->chunk_type != SCTP_INITIATION) &&
1530 (ch->chunk_type != SCTP_INITIATION_ACK) &&
1531 (ch->chunk_type != SCTP_COOKIE_ACK) &&
1532 (ch->chunk_type != SCTP_COOKIE_ECHO)) {
1533 /* Other chunk types go to the tcp pool. */
1537 retval = sctp_findassociation_addr_sa(to, from, inp_p, netp,
1538 find_tcp_pool, vrf_id);
1541 retval = sctp_findassociation_addr_sa(to, from, &inp, netp,
1542 find_tcp_pool, vrf_id);
1544 SCTPDBG(SCTP_DEBUG_PCB1, "retval:%p inp:%p\n", retval, inp);
1545 if (retval == NULL && inp) {
1546 /* Found a EP but not this address */
1547 if ((ch->chunk_type == SCTP_INITIATION) ||
1548 (ch->chunk_type == SCTP_INITIATION_ACK)) {
1550 * special hook, we do NOT return linp or an
1551 * association that is linked to an existing
1552 * association that is under the TCP pool (i.e. no
1553 * listener exists). The endpoint finding routine
1554 * will always find a listner before examining the
1557 if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
1563 retval = sctp_findassociation_special_addr(m, iphlen,
1564 offset, sh, &inp, netp, to);
1565 if (inp_p != NULL) {
1570 SCTPDBG(SCTP_DEBUG_PCB1, "retval is %p\n", retval);
1575 * lookup an association by an ASCONF lookup address.
1576 * if the lookup address is 0.0.0.0 or ::0, use the vtag to do the lookup
1579 sctp_findassociation_ep_asconf(struct mbuf *m, int iphlen, int offset,
1580 struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp)
1582 struct sctp_tcb *stcb;
1583 struct sockaddr_in *sin;
1584 struct sockaddr_in6 *sin6;
1585 struct sockaddr_storage local_store, remote_store;
1587 struct sctp_paramhdr parm_buf, *phdr;
1589 int zero_address = 0;
1592 memset(&local_store, 0, sizeof(local_store));
1593 memset(&remote_store, 0, sizeof(remote_store));
1595 /* First get the destination address setup too. */
1596 iph = mtod(m, struct ip *);
1597 if (iph->ip_v == IPVERSION) {
1599 sin = (struct sockaddr_in *)&local_store;
1600 sin->sin_family = AF_INET;
1601 sin->sin_len = sizeof(*sin);
1602 sin->sin_port = sh->dest_port;
1603 sin->sin_addr.s_addr = iph->ip_dst.s_addr;
1604 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
1606 struct ip6_hdr *ip6;
1608 ip6 = mtod(m, struct ip6_hdr *);
1609 sin6 = (struct sockaddr_in6 *)&local_store;
1610 sin6->sin6_family = AF_INET6;
1611 sin6->sin6_len = sizeof(*sin6);
1612 sin6->sin6_port = sh->dest_port;
1613 sin6->sin6_addr = ip6->ip6_dst;
1618 phdr = sctp_get_next_param(m, offset + sizeof(struct sctp_asconf_chunk),
1619 &parm_buf, sizeof(struct sctp_paramhdr));
1621 SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf lookup addr\n",
1625 ptype = (int)((uint32_t) ntohs(phdr->param_type));
1626 /* get the correlation address */
1627 if (ptype == SCTP_IPV6_ADDRESS) {
1628 /* ipv6 address param */
1629 struct sctp_ipv6addr_param *p6, p6_buf;
1631 if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv6addr_param)) {
1634 p6 = (struct sctp_ipv6addr_param *)sctp_get_next_param(m,
1635 offset + sizeof(struct sctp_asconf_chunk),
1636 &p6_buf.ph, sizeof(*p6));
1638 SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v6 lookup addr\n",
1642 sin6 = (struct sockaddr_in6 *)&remote_store;
1643 sin6->sin6_family = AF_INET6;
1644 sin6->sin6_len = sizeof(*sin6);
1645 sin6->sin6_port = sh->src_port;
1646 memcpy(&sin6->sin6_addr, &p6->addr, sizeof(struct in6_addr));
1647 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
1649 } else if (ptype == SCTP_IPV4_ADDRESS) {
1650 /* ipv4 address param */
1651 struct sctp_ipv4addr_param *p4, p4_buf;
1653 if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv4addr_param)) {
1656 p4 = (struct sctp_ipv4addr_param *)sctp_get_next_param(m,
1657 offset + sizeof(struct sctp_asconf_chunk),
1658 &p4_buf.ph, sizeof(*p4));
1660 SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v4 lookup addr\n",
1664 sin = (struct sockaddr_in *)&remote_store;
1665 sin->sin_family = AF_INET;
1666 sin->sin_len = sizeof(*sin);
1667 sin->sin_port = sh->src_port;
1668 memcpy(&sin->sin_addr, &p4->addr, sizeof(struct in_addr));
1669 if (sin->sin_addr.s_addr == INADDR_ANY)
1672 /* invalid address param type */
1677 stcb = sctp_findassoc_by_vtag(NULL, ntohl(sh->v_tag), inp_p,
1678 netp, sh->src_port, sh->dest_port, 1);
1680 * printf("findassociation_ep_asconf: zero lookup address
1681 * finds stcb 0x%x\n", (uint32_t)stcb);
1684 stcb = sctp_findassociation_ep_addr(inp_p,
1685 (struct sockaddr *)&remote_store, netp,
1686 (struct sockaddr *)&local_store, NULL);
1693 * allocate a sctp_inpcb and setup a temporary binding to a port/all
1694 * addresses. This way if we don't get a bind we by default pick a ephemeral
1695 * port with all addresses bound.
1698 sctp_inpcb_alloc(struct socket *so)
1701 * we get called when a new endpoint starts up. We need to allocate
1702 * the sctp_inpcb structure from the zone and init it. Mark it as
1703 * unbound and find a port that we can use as an ephemeral with
1704 * INADDR_ANY. If the user binds later no problem we can then add in
1705 * the specific addresses. And setup the default parameters for the
1709 struct sctp_inpcb *inp;
1711 struct timeval time;
1712 sctp_sharedkey_t *null_key;
1716 SCTP_INP_INFO_WLOCK();
1717 inp = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_ep, struct sctp_inpcb);
1719 SCTP_PRINTF("Out of SCTP-INPCB structures - no resources\n");
1720 SCTP_INP_INFO_WUNLOCK();
1724 bzero(inp, sizeof(*inp));
1726 /* bump generations */
1727 /* setup socket pointers */
1728 inp->sctp_socket = so;
1729 inp->ip_inp.inp.inp_socket = so;
1731 inp->partial_delivery_point = SCTP_SB_LIMIT_RCV(so) >> SCTP_PARTIAL_DELIVERY_SHIFT;
1732 inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
1736 struct inpcbpolicy *pcb_sp = NULL;
1738 error = ipsec_init_pcbpolicy(so, &pcb_sp);
1739 /* Arrange to share the policy */
1740 inp->ip_inp.inp.inp_sp = pcb_sp;
1741 ((struct in6pcb *)(&inp->ip_inp.inp))->in6p_sp = pcb_sp;
1744 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
1745 SCTP_INP_INFO_WUNLOCK();
1749 SCTP_INCR_EP_COUNT();
1750 inp->ip_inp.inp.inp_ip_ttl = ip_defttl;
1751 SCTP_INP_INFO_WUNLOCK();
1753 so->so_pcb = (caddr_t)inp;
1755 if ((SCTP_SO_TYPE(so) == SOCK_DGRAM) ||
1756 (SCTP_SO_TYPE(so) == SOCK_SEQPACKET)) {
1757 /* UDP style socket */
1758 inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
1759 SCTP_PCB_FLAGS_UNBOUND);
1760 /* Be sure it is NON-BLOCKING IO for UDP */
1761 /* SCTP_SET_SO_NBIO(so); */
1762 } else if (SCTP_SO_TYPE(so) == SOCK_STREAM) {
1763 /* TCP style socket */
1764 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
1765 SCTP_PCB_FLAGS_UNBOUND);
1766 /* Be sure we have blocking IO by default */
1767 SCTP_CLEAR_SO_NBIO(so);
1770 * unsupported socket type (RAW, etc)- in case we missed it
1773 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
1774 return (EOPNOTSUPP);
1776 inp->sctp_tcbhash = SCTP_HASH_INIT(sctp_pcbtblsize,
1777 &inp->sctp_hashmark);
1778 if (inp->sctp_tcbhash == NULL) {
1779 SCTP_PRINTF("Out of SCTP-INPCB->hashinit - no resources\n");
1780 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
1783 inp->def_vrf_id = SCTP_DEFAULT_VRFID;
1784 inp->def_table_id = SCTP_DEFAULT_TABLEID;
1786 SCTP_INP_INFO_WLOCK();
1787 SCTP_INP_LOCK_INIT(inp);
1788 SCTP_INP_READ_INIT(inp);
1789 SCTP_ASOC_CREATE_LOCK_INIT(inp);
1790 /* lock the new ep */
1791 SCTP_INP_WLOCK(inp);
1793 /* add it to the info area */
1794 LIST_INSERT_HEAD(&sctppcbinfo.listhead, inp, sctp_list);
1795 SCTP_INP_INFO_WUNLOCK();
1797 TAILQ_INIT(&inp->read_queue);
1798 LIST_INIT(&inp->sctp_addr_list);
1800 LIST_INIT(&inp->sctp_asoc_list);
1802 #ifdef SCTP_TRACK_FREED_ASOCS
1804 LIST_INIT(&inp->sctp_asoc_free_list);
1806 /* Init the timer structure for signature change */
1807 SCTP_OS_TIMER_INIT(&inp->sctp_ep.signature_change.timer);
1808 inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NEWCOOKIE;
1810 /* now init the actual endpoint default data */
1813 /* setup the base timeout information */
1814 m->sctp_timeoutticks[SCTP_TIMER_SEND] = SEC_TO_TICKS(SCTP_SEND_SEC); /* needed ? */
1815 m->sctp_timeoutticks[SCTP_TIMER_INIT] = SEC_TO_TICKS(SCTP_INIT_SEC); /* needed ? */
1816 m->sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sctp_delayed_sack_time_default);
1817 m->sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(sctp_heartbeat_interval_default);
1818 m->sctp_timeoutticks[SCTP_TIMER_PMTU] = SEC_TO_TICKS(sctp_pmtu_raise_time_default);
1819 m->sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] = SEC_TO_TICKS(sctp_shutdown_guard_time_default);
1820 m->sctp_timeoutticks[SCTP_TIMER_SIGNATURE] = SEC_TO_TICKS(sctp_secret_lifetime_default);
1821 /* all max/min max are in ms */
1822 m->sctp_maxrto = sctp_rto_max_default;
1823 m->sctp_minrto = sctp_rto_min_default;
1824 m->initial_rto = sctp_rto_initial_default;
1825 m->initial_init_rto_max = sctp_init_rto_max_default;
1826 m->sctp_sack_freq = sctp_sack_freq_default;
1828 m->max_open_streams_intome = MAX_SCTP_STREAMS;
1830 m->max_init_times = sctp_init_rtx_max_default;
1831 m->max_send_times = sctp_assoc_rtx_max_default;
1832 m->def_net_failure = sctp_path_rtx_max_default;
1833 m->sctp_sws_sender = SCTP_SWS_SENDER_DEF;
1834 m->sctp_sws_receiver = SCTP_SWS_RECEIVER_DEF;
1835 m->max_burst = sctp_max_burst_default;
1836 /* number of streams to pre-open on a association */
1837 m->pre_open_stream_count = sctp_nr_outgoing_streams_default;
1839 /* Add adaptation cookie */
1840 m->adaptation_layer_indicator = 0x504C5253;
1842 /* seed random number generator */
1843 m->random_counter = 1;
1844 m->store_at = SCTP_SIGNATURE_SIZE;
1845 SCTP_READ_RANDOM(m->random_numbers, sizeof(m->random_numbers));
1846 sctp_fill_random_store(m);
1848 /* Minimum cookie size */
1849 m->size_of_a_cookie = (sizeof(struct sctp_init_msg) * 2) +
1850 sizeof(struct sctp_state_cookie);
1851 m->size_of_a_cookie += SCTP_SIGNATURE_SIZE;
1853 /* Setup the initial secret */
1854 (void)SCTP_GETTIME_TIMEVAL(&time);
1855 m->time_of_secret_change = time.tv_sec;
1857 for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
1858 m->secret_key[0][i] = sctp_select_initial_TSN(m);
1860 sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL);
1862 /* How long is a cookie good for ? */
1863 m->def_cookie_life = MSEC_TO_TICKS(sctp_valid_cookie_life_default);
1865 * Initialize authentication parameters
1867 m->local_hmacs = sctp_default_supported_hmaclist();
1868 m->local_auth_chunks = sctp_alloc_chunklist();
1869 sctp_auth_set_default_chunks(m->local_auth_chunks);
1870 LIST_INIT(&m->shared_keys);
1871 /* add default NULL key as key id 0 */
1872 null_key = sctp_alloc_sharedkey();
1873 sctp_insert_sharedkey(&m->shared_keys, null_key);
1874 SCTP_INP_WUNLOCK(inp);
1875 #ifdef SCTP_LOG_CLOSING
1876 sctp_log_closing(inp, NULL, 12);
1883 sctp_move_pcb_and_assoc(struct sctp_inpcb *old_inp, struct sctp_inpcb *new_inp,
1884 struct sctp_tcb *stcb)
1886 struct sctp_nets *net;
1887 uint16_t lport, rport;
1888 struct sctppcbhead *head;
1889 struct sctp_laddr *laddr, *oladdr;
1891 SCTP_TCB_UNLOCK(stcb);
1892 SCTP_INP_INFO_WLOCK();
1893 SCTP_INP_WLOCK(old_inp);
1894 SCTP_INP_WLOCK(new_inp);
1895 SCTP_TCB_LOCK(stcb);
1897 new_inp->sctp_ep.time_of_secret_change =
1898 old_inp->sctp_ep.time_of_secret_change;
1899 memcpy(new_inp->sctp_ep.secret_key, old_inp->sctp_ep.secret_key,
1900 sizeof(old_inp->sctp_ep.secret_key));
1901 new_inp->sctp_ep.current_secret_number =
1902 old_inp->sctp_ep.current_secret_number;
1903 new_inp->sctp_ep.last_secret_number =
1904 old_inp->sctp_ep.last_secret_number;
1905 new_inp->sctp_ep.size_of_a_cookie = old_inp->sctp_ep.size_of_a_cookie;
1907 /* make it so new data pours into the new socket */
1908 stcb->sctp_socket = new_inp->sctp_socket;
1909 stcb->sctp_ep = new_inp;
1911 /* Copy the port across */
1912 lport = new_inp->sctp_lport = old_inp->sctp_lport;
1913 rport = stcb->rport;
1914 /* Pull the tcb from the old association */
1915 LIST_REMOVE(stcb, sctp_tcbhash);
1916 LIST_REMOVE(stcb, sctp_tcblist);
1918 /* Now insert the new_inp into the TCP connected hash */
1919 head = &sctppcbinfo.sctp_tcpephash[SCTP_PCBHASH_ALLADDR((lport + rport),
1920 sctppcbinfo.hashtcpmark)];
1922 LIST_INSERT_HEAD(head, new_inp, sctp_hash);
1923 /* Its safe to access */
1924 new_inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
1926 /* Now move the tcb into the endpoint list */
1927 LIST_INSERT_HEAD(&new_inp->sctp_asoc_list, stcb, sctp_tcblist);
1929 * Question, do we even need to worry about the ep-hash since we
1930 * only have one connection? Probably not :> so lets get rid of it
1931 * and not suck up any kernel memory in that.
1934 /* Ok. Let's restart timer. */
1935 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1936 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, new_inp,
1940 SCTP_INP_INFO_WUNLOCK();
1941 if (new_inp->sctp_tcbhash != NULL) {
1942 SCTP_HASH_FREE(new_inp->sctp_tcbhash, new_inp->sctp_hashmark);
1943 new_inp->sctp_tcbhash = NULL;
1945 if ((new_inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
1946 /* Subset bound, so copy in the laddr list from the old_inp */
1947 LIST_FOREACH(oladdr, &old_inp->sctp_addr_list, sctp_nxt_addr) {
1948 laddr = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_laddr, struct sctp_laddr);
1949 if (laddr == NULL) {
1951 * Gak, what can we do? This assoc is really
1952 * HOSED. We probably should send an abort
1955 SCTPDBG(SCTP_DEBUG_PCB1, "Association hosed in TCP model, out of laddr memory\n");
1958 SCTP_INCR_LADDR_COUNT();
1959 bzero(laddr, sizeof(*laddr));
1960 laddr->ifa = oladdr->ifa;
1961 atomic_add_int(&laddr->ifa->refcount, 1);
1962 LIST_INSERT_HEAD(&new_inp->sctp_addr_list, laddr,
1964 new_inp->laddr_count++;
1968 * Now any running timers need to be adjusted since we really don't
1969 * care if they are running or not just blast in the new_inp into
1973 stcb->asoc.hb_timer.ep = (void *)new_inp;
1974 stcb->asoc.dack_timer.ep = (void *)new_inp;
1975 stcb->asoc.asconf_timer.ep = (void *)new_inp;
1976 stcb->asoc.strreset_timer.ep = (void *)new_inp;
1977 stcb->asoc.shut_guard_timer.ep = (void *)new_inp;
1978 stcb->asoc.autoclose_timer.ep = (void *)new_inp;
1979 stcb->asoc.delayed_event_timer.ep = (void *)new_inp;
1980 /* now what about the nets? */
1981 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1982 net->pmtu_timer.ep = (void *)new_inp;
1983 net->rxt_timer.ep = (void *)new_inp;
1984 net->fr_timer.ep = (void *)new_inp;
1986 SCTP_INP_WUNLOCK(new_inp);
1987 SCTP_INP_WUNLOCK(old_inp);
1991 sctp_isport_inuse(struct sctp_inpcb *inp, uint16_t lport, uint32_t vrf_id)
1993 struct sctppcbhead *head;
1994 struct sctp_inpcb *t_inp;
1997 head = &sctppcbinfo.sctp_ephash[SCTP_PCBHASH_ALLADDR(lport,
1998 sctppcbinfo.hashmark)];
1999 LIST_FOREACH(t_inp, head, sctp_hash) {
2000 if (t_inp->sctp_lport != lport) {
2003 /* is it in the VRF in question */
2005 if (t_inp->def_vrf_id == vrf_id)
2010 /* This one is in use. */
2011 /* check the v6/v4 binding issue */
2012 if ((t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
2013 SCTP_IPV6_V6ONLY(t_inp)) {
2014 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2015 /* collision in V6 space */
2018 /* inp is BOUND_V4 no conflict */
2021 } else if (t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2022 /* t_inp is bound v4 and v6, conflict always */
2025 /* t_inp is bound only V4 */
2026 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
2027 SCTP_IPV6_V6ONLY(inp)) {
2031 /* else fall through to conflict */
2041 sctp_inpcb_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
2043 /* bind a ep to a socket address */
2044 struct sctppcbhead *head;
2045 struct sctp_inpcb *inp, *inp_tmp;
2046 struct inpcb *ip_inp;
2055 inp = (struct sctp_inpcb *)so->so_pcb;
2056 ip_inp = (struct inpcb *)so->so_pcb;
2059 SCTPDBG(SCTP_DEBUG_PCB1, "Bind called port:%d\n",
2060 ntohs(((struct sockaddr_in *)addr)->sin_port));
2061 SCTPDBG(SCTP_DEBUG_PCB1, "Addr :");
2062 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr);
2065 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
2066 /* already did a bind, subsequent binds NOT allowed ! */
2070 if (addr->sa_family == AF_INET) {
2071 struct sockaddr_in *sin;
2073 /* IPV6_V6ONLY socket? */
2074 if (SCTP_IPV6_V6ONLY(ip_inp)) {
2077 if (addr->sa_len != sizeof(*sin))
2080 sin = (struct sockaddr_in *)addr;
2081 lport = sin->sin_port;
2083 if (sin->sin_addr.s_addr != INADDR_ANY) {
2086 } else if (addr->sa_family == AF_INET6) {
2087 /* Only for pure IPv6 Address. (No IPv4 Mapped!) */
2088 struct sockaddr_in6 *sin6;
2090 sin6 = (struct sockaddr_in6 *)addr;
2092 if (addr->sa_len != sizeof(*sin6))
2095 lport = sin6->sin6_port;
2096 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2098 /* KAME hack: embed scopeid */
2099 if (sa6_embedscope(sin6, ip6_use_defzone) != 0)
2102 /* this must be cleared for ifa_ifwithaddr() */
2103 sin6->sin6_scope_id = 0;
2105 return (EAFNOSUPPORT);
2108 /* Setup a vrf_id to be the default for the non-bind-all case. */
2109 vrf_id = inp->def_vrf_id;
2111 SCTP_INP_INFO_WLOCK();
2112 SCTP_INP_WLOCK(inp);
2113 /* increase our count due to the unlock we do */
2114 SCTP_INP_INCR_REF(inp);
2117 * Did the caller specify a port? if so we must see if a ep
2118 * already has this one bound.
2120 /* got to be root to get at low ports */
2121 if (ntohs(lport) < IPPORT_RESERVED) {
2123 priv_check_cred(p->td_ucred,
2124 PRIV_NETINET_RESERVEDPORT,
2128 SCTP_INP_DECR_REF(inp);
2129 SCTP_INP_WUNLOCK(inp);
2130 SCTP_INP_INFO_WUNLOCK();
2135 SCTP_INP_DECR_REF(inp);
2136 SCTP_INP_WUNLOCK(inp);
2137 SCTP_INP_INFO_WUNLOCK();
2140 SCTP_INP_WUNLOCK(inp);
2142 vrf_id = inp->def_vrf_id;
2143 inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id);
2144 if (inp_tmp != NULL) {
2146 * lock guy returned and lower count note
2147 * that we are not bound so inp_tmp should
2148 * NEVER be inp. And it is this inp
2149 * (inp_tmp) that gets the reference bump,
2150 * so we must lower it.
2152 SCTP_INP_DECR_REF(inp_tmp);
2153 SCTP_INP_DECR_REF(inp);
2155 SCTP_INP_INFO_WUNLOCK();
2156 return (EADDRNOTAVAIL);
2159 inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id);
2160 if (inp_tmp != NULL) {
2162 * lock guy returned and lower count note
2163 * that we are not bound so inp_tmp should
2164 * NEVER be inp. And it is this inp
2165 * (inp_tmp) that gets the reference bump,
2166 * so we must lower it.
2168 SCTP_INP_DECR_REF(inp_tmp);
2169 SCTP_INP_DECR_REF(inp);
2171 SCTP_INP_INFO_WUNLOCK();
2172 return (EADDRNOTAVAIL);
2175 SCTP_INP_WLOCK(inp);
2177 /* verify that no lport is not used by a singleton */
2178 if (sctp_isport_inuse(inp, lport, vrf_id)) {
2179 /* Sorry someone already has this one bound */
2180 SCTP_INP_DECR_REF(inp);
2181 SCTP_INP_WUNLOCK(inp);
2182 SCTP_INP_INFO_WUNLOCK();
2183 return (EADDRNOTAVAIL);
2188 * get any port but lets make sure no one has any address
2189 * with this port bound
2193 * setup the inp to the top (I could use the union but this
2196 uint32_t port_guess;
2197 uint16_t port_attempt;
2202 port_guess = sctp_select_initial_TSN(&inp->sctp_ep);
2203 port_attempt = (port_guess & 0x0000ffff);
2204 if (port_attempt == 0) {
2207 if (port_attempt < IPPORT_RESERVED) {
2208 port_attempt += IPPORT_RESERVED;
2211 vrf_id = inp->def_vrf_id;
2212 if (sctp_isport_inuse(inp, htons(port_attempt),
2214 /* got a port we can use */
2217 if (not_found == 1) {
2218 /* We can use this port */
2222 /* try upper half */
2224 port_attempt = ((port_guess >> 16) & 0x0000ffff);
2226 if (port_attempt == 0) {
2229 if (port_attempt < IPPORT_RESERVED) {
2230 port_attempt += IPPORT_RESERVED;
2233 vrf_id = inp->def_vrf_id;
2234 if (sctp_isport_inuse(inp, htons(port_attempt),
2236 /* got a port we can use */
2239 if (not_found == 1) {
2240 /* We can use this port */
2244 /* try two half's added together */
2246 port_attempt = (((port_guess >> 16) & 0x0000ffff) +
2247 (port_guess & 0x0000ffff));
2248 if (port_attempt == 0) {
2249 /* get a new random number */
2252 if (port_attempt < IPPORT_RESERVED) {
2253 port_attempt += IPPORT_RESERVED;
2256 vrf_id = inp->def_vrf_id;
2257 if (sctp_isport_inuse(inp, htons(port_attempt), vrf_id) == 1) {
2258 /* got a port we can use */
2261 if (not_found == 1) {
2262 /* We can use this port */
2267 /* we don't get out of the loop until we have a port */
2268 lport = htons(port_attempt);
2270 SCTP_INP_DECR_REF(inp);
2271 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE |
2272 SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
2274 * this really should not happen. The guy did a non-blocking
2275 * bind and then did a close at the same time.
2277 SCTP_INP_WUNLOCK(inp);
2278 SCTP_INP_INFO_WUNLOCK();
2281 /* ok we look clear to give out this port, so lets setup the binding */
2283 /* binding to all addresses, so just set in the proper flags */
2284 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUNDALL;
2285 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
2286 /* set the automatic addr changes from kernel flag */
2287 if (sctp_auto_asconf == 0) {
2288 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
2290 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
2294 * bind specific, make sure flags is off and add a new
2295 * address structure to the sctp_addr_list inside the ep
2298 * We will need to allocate one and insert it at the head. The
2299 * socketopt call can just insert new addresses in there as
2300 * well. It will also have to do the embed scope kame hack
2301 * too (before adding).
2303 struct sctp_ifa *ifa;
2304 struct sockaddr_storage store_sa;
2306 memset(&store_sa, 0, sizeof(store_sa));
2307 if (addr->sa_family == AF_INET) {
2308 struct sockaddr_in *sin;
2310 sin = (struct sockaddr_in *)&store_sa;
2311 memcpy(sin, addr, sizeof(struct sockaddr_in));
2313 } else if (addr->sa_family == AF_INET6) {
2314 struct sockaddr_in6 *sin6;
2316 sin6 = (struct sockaddr_in6 *)&store_sa;
2317 memcpy(sin6, addr, sizeof(struct sockaddr_in6));
2318 sin6->sin6_port = 0;
2321 * first find the interface with the bound address need to
2322 * zero out the port to find the address! yuck! can't do
2323 * this earlier since need port for sctp_pcb_findep()
2325 ifa = sctp_find_ifa_by_addr((struct sockaddr *)&store_sa,
2328 /* Can't find an interface with that address */
2329 SCTP_INP_WUNLOCK(inp);
2330 SCTP_INP_INFO_WUNLOCK();
2331 return (EADDRNOTAVAIL);
2333 if (addr->sa_family == AF_INET6) {
2334 /* GAK, more FIXME IFA lock? */
2335 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2336 /* Can't bind a non-existent addr. */
2337 SCTP_INP_WUNLOCK(inp);
2338 SCTP_INP_INFO_WUNLOCK();
2342 /* we're not bound all */
2343 inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUNDALL;
2344 /* allow bindx() to send ASCONF's for binding changes */
2345 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
2346 /* set the automatic addr changes from kernel flag */
2347 if (sctp_auto_asconf == 0) {
2348 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
2350 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
2353 /* add this address to the endpoint list */
2354 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, 0);
2356 SCTP_INP_WUNLOCK(inp);
2357 SCTP_INP_INFO_WUNLOCK();
2362 /* find the bucket */
2363 head = &sctppcbinfo.sctp_ephash[SCTP_PCBHASH_ALLADDR(lport,
2364 sctppcbinfo.hashmark)];
2365 /* put it in the bucket */
2366 LIST_INSERT_HEAD(head, inp, sctp_hash);
2367 SCTPDBG(SCTP_DEBUG_PCB1, "Main hash to bind at head:%p, bound port:%d\n",
2368 head, ntohs(lport));
2369 /* set in the port */
2370 inp->sctp_lport = lport;
2372 /* turn off just the unbound flag */
2373 inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
2374 SCTP_INP_WUNLOCK(inp);
2375 SCTP_INP_INFO_WUNLOCK();
2381 sctp_iterator_inp_being_freed(struct sctp_inpcb *inp, struct sctp_inpcb *inp_next)
2383 struct sctp_iterator *it;
2386 * We enter with the only the ITERATOR_LOCK in place and a write
2387 * lock on the inp_info stuff.
2391 * Go through all iterators, we must do this since it is possible
2392 * that some iterator does NOT have the lock, but is waiting for it.
2393 * And the one that had the lock has either moved in the last
2394 * iteration or we just cleared it above. We need to find all of
2395 * those guys. The list of iterators should never be very big
2398 TAILQ_FOREACH(it, &sctppcbinfo.iteratorhead, sctp_nxt_itr) {
2399 if (it == inp->inp_starting_point_for_iterator)
2400 /* skip this guy, he's special */
2402 if (it->inp == inp) {
2404 * This is tricky and we DON'T lock the iterator.
2405 * Reason is he's running but waiting for me since
2406 * inp->inp_starting_point_for_iterator has the lock
2407 * on me (the guy above we skipped). This tells us
2408 * its is not running but waiting for
2409 * inp->inp_starting_point_for_iterator to be
2410 * released by the guy that does have our INP in a
2413 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
2417 /* set him up to do the next guy not me */
2423 it = inp->inp_starting_point_for_iterator;
2425 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
2434 /* release sctp_inpcb unbind the port */
2436 sctp_inpcb_free(struct sctp_inpcb *inp, int immediate, int from)
2439 * Here we free a endpoint. We must find it (if it is in the Hash
2440 * table) and remove it from there. Then we must also find it in the
2441 * overall list and remove it from there. After all removals are
2442 * complete then any timer has to be stopped. Then start the actual
2443 * freeing. a) Any local lists. b) Any associations. c) The hash of
2444 * all associations. d) finally the ep itself.
2447 struct sctp_inpcb *inp_save;
2448 struct sctp_tcb *asoc, *nasoc;
2449 struct sctp_laddr *laddr, *nladdr;
2450 struct inpcb *ip_pcb;
2453 struct sctp_queued_to_read *sq;
2457 sctp_sharedkey_t *shared_key;
2460 #ifdef SCTP_LOG_CLOSING
2461 sctp_log_closing(inp, NULL, 0);
2464 SCTP_ITERATOR_LOCK();
2465 so = inp->sctp_socket;
2466 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
2467 /* been here before.. eeks.. get out of here */
2468 SCTP_PRINTF("This conflict in free SHOULD not be happening!\n");
2469 SCTP_ITERATOR_UNLOCK();
2470 #ifdef SCTP_LOG_CLOSING
2471 sctp_log_closing(inp, NULL, 1);
2475 SCTP_ASOC_CREATE_LOCK(inp);
2476 SCTP_INP_INFO_WLOCK();
2478 SCTP_INP_WLOCK(inp);
2479 /* First time through we have the socket lock, after that no more. */
2480 if (from == SCTP_CALLED_AFTER_CMPSET_OFCLOSE) {
2482 * Once we are in we can remove the flag from = 1 is only
2483 * passed from the actual closing routines that are called
2484 * via the sockets layer.
2486 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CLOSE_IP;
2488 sctp_timer_stop(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL,
2489 SCTP_FROM_SCTP_PCB + SCTP_LOC_1);
2492 sctp_m_freem(inp->control);
2493 inp->control = NULL;
2496 sctp_m_freem(inp->pkt);
2500 ip_pcb = &inp->ip_inp.inp; /* we could just cast the main pointer
2501 * here but I will be nice :> (i.e.
2503 if (immediate == SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE) {
2507 for ((asoc = LIST_FIRST(&inp->sctp_asoc_list)); asoc != NULL;
2509 nasoc = LIST_NEXT(asoc, sctp_tcblist);
2510 if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
2511 /* Skip guys being freed */
2512 asoc->sctp_socket = NULL;
2516 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_WAIT) ||
2517 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
2518 /* Just abandon things in the front states */
2519 if (asoc->asoc.total_output_queue_size == 0) {
2520 sctp_free_assoc(inp, asoc, SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_2);
2524 SCTP_TCB_LOCK(asoc);
2525 /* Disconnect the socket please */
2526 asoc->sctp_socket = NULL;
2527 asoc->asoc.state |= SCTP_STATE_CLOSED_SOCKET;
2528 if ((asoc->asoc.size_on_reasm_queue > 0) ||
2529 (asoc->asoc.control_pdapi) ||
2530 (asoc->asoc.size_on_all_streams > 0) ||
2531 (so && (so->so_rcv.sb_cc > 0))
2533 /* Left with Data unread */
2534 struct mbuf *op_err;
2536 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
2537 0, M_DONTWAIT, 1, MT_DATA);
2539 /* Fill in the user initiated abort */
2540 struct sctp_paramhdr *ph;
2543 SCTP_BUF_LEN(op_err) =
2544 sizeof(struct sctp_paramhdr) + sizeof(uint32_t);
2546 struct sctp_paramhdr *);
2547 ph->param_type = htons(
2548 SCTP_CAUSE_USER_INITIATED_ABT);
2549 ph->param_length = htons(SCTP_BUF_LEN(op_err));
2550 ippp = (uint32_t *) (ph + 1);
2551 *ippp = htonl(SCTP_FROM_SCTP_PCB + SCTP_LOC_3);
2553 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_3;
2554 sctp_send_abort_tcb(asoc, op_err);
2555 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
2556 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
2557 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
2558 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
2560 sctp_free_assoc(inp, asoc, SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_4);
2562 } else if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
2563 TAILQ_EMPTY(&asoc->asoc.sent_queue) &&
2564 (asoc->asoc.stream_queue_cnt == 0)
2566 if (asoc->asoc.locked_on_sending) {
2569 if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
2570 (SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
2572 * there is nothing queued to send,
2573 * so I send shutdown
2575 sctp_send_shutdown(asoc, asoc->asoc.primary_destination);
2576 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
2577 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
2578 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
2580 asoc->asoc.state = SCTP_STATE_SHUTDOWN_SENT;
2581 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, asoc->sctp_ep, asoc,
2582 asoc->asoc.primary_destination);
2583 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc,
2584 asoc->asoc.primary_destination);
2585 sctp_chunk_output(inp, asoc, SCTP_OUTPUT_FROM_SHUT_TMR);
2588 /* mark into shutdown pending */
2589 struct sctp_stream_queue_pending *sp;
2591 asoc->asoc.state |= SCTP_STATE_SHUTDOWN_PENDING;
2592 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc,
2593 asoc->asoc.primary_destination);
2594 if (asoc->asoc.locked_on_sending) {
2595 sp = TAILQ_LAST(&((asoc->asoc.locked_on_sending)->outqueue),
2598 SCTP_PRINTF("Error, sp is NULL, locked on sending is %p strm:%d\n",
2599 asoc->asoc.locked_on_sending,
2600 asoc->asoc.locked_on_sending->stream_no);
2602 if ((sp->length == 0) && (sp->msg_is_complete == 0))
2603 asoc->asoc.state |= SCTP_STATE_PARTIAL_MSG_LEFT;
2606 if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
2607 TAILQ_EMPTY(&asoc->asoc.sent_queue) &&
2608 (asoc->asoc.state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
2609 struct mbuf *op_err;
2612 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
2613 0, M_DONTWAIT, 1, MT_DATA);
2619 struct sctp_paramhdr *ph;
2622 SCTP_BUF_LEN(op_err) =
2623 (sizeof(struct sctp_paramhdr) +
2626 struct sctp_paramhdr *);
2627 ph->param_type = htons(
2628 SCTP_CAUSE_USER_INITIATED_ABT);
2629 ph->param_length = htons(SCTP_BUF_LEN(op_err));
2630 ippp = (uint32_t *) (ph + 1);
2631 *ippp = htonl(SCTP_FROM_SCTP_PCB + SCTP_LOC_5);
2633 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_5;
2634 sctp_send_abort_tcb(asoc, op_err);
2635 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
2636 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
2637 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
2638 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
2640 sctp_free_assoc(inp, asoc, SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_6);
2645 SCTP_TCB_UNLOCK(asoc);
2647 /* now is there some left in our SHUTDOWN state? */
2649 SCTP_INP_WUNLOCK(inp);
2650 SCTP_ASOC_CREATE_UNLOCK(inp);
2651 SCTP_INP_INFO_WUNLOCK();
2652 SCTP_ITERATOR_UNLOCK();
2653 #ifdef SCTP_LOG_CLOSING
2654 sctp_log_closing(inp, NULL, 2);
2659 inp->sctp_socket = NULL;
2660 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) !=
2661 SCTP_PCB_FLAGS_UNBOUND) {
2663 * ok, this guy has been bound. It's port is somewhere in
2664 * the sctppcbinfo hash table. Remove it!
2666 LIST_REMOVE(inp, sctp_hash);
2667 inp->sctp_flags |= SCTP_PCB_FLAGS_UNBOUND;
2670 * If there is a timer running to kill us, forget it, since it may
2671 * have a contest on the INP lock.. which would cause us to die ...
2674 for ((asoc = LIST_FIRST(&inp->sctp_asoc_list)); asoc != NULL;
2676 nasoc = LIST_NEXT(asoc, sctp_tcblist);
2677 if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
2681 /* Free associations that are NOT killing us */
2682 SCTP_TCB_LOCK(asoc);
2683 if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_COOKIE_WAIT) &&
2684 ((asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0)) {
2685 struct mbuf *op_err;
2688 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
2689 0, M_DONTWAIT, 1, MT_DATA);
2691 /* Fill in the user initiated abort */
2692 struct sctp_paramhdr *ph;
2694 SCTP_BUF_LEN(op_err) = (sizeof(struct sctp_paramhdr) +
2696 ph = mtod(op_err, struct sctp_paramhdr *);
2697 ph->param_type = htons(
2698 SCTP_CAUSE_USER_INITIATED_ABT);
2699 ph->param_length = htons(SCTP_BUF_LEN(op_err));
2700 ippp = (uint32_t *) (ph + 1);
2701 *ippp = htonl(SCTP_FROM_SCTP_PCB + SCTP_LOC_7);
2704 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_7;
2705 sctp_send_abort_tcb(asoc, op_err);
2706 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
2707 } else if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
2709 SCTP_TCB_UNLOCK(asoc);
2712 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
2713 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
2714 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
2716 sctp_free_assoc(inp, asoc, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_8);
2719 /* Ok we have someone out there that will kill us */
2720 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
2721 SCTP_INP_WUNLOCK(inp);
2722 SCTP_ASOC_CREATE_UNLOCK(inp);
2723 SCTP_INP_INFO_WUNLOCK();
2724 SCTP_ITERATOR_UNLOCK();
2725 #ifdef SCTP_LOG_CLOSING
2726 sctp_log_closing(inp, NULL, 3);
2730 if ((inp->refcount) || (inp->sctp_flags & SCTP_PCB_FLAGS_CLOSE_IP)) {
2731 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
2732 sctp_timer_start(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL);
2733 SCTP_INP_WUNLOCK(inp);
2734 SCTP_ASOC_CREATE_UNLOCK(inp);
2735 SCTP_INP_INFO_WUNLOCK();
2736 SCTP_ITERATOR_UNLOCK();
2737 #ifdef SCTP_LOG_CLOSING
2738 sctp_log_closing(inp, NULL, 4);
2742 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
2743 inp->sctp_ep.signature_change.type = 0;
2744 inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_ALLGONE;
2746 #ifdef SCTP_LOG_CLOSING
2747 sctp_log_closing(inp, NULL, 5);
2750 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
2751 inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NONE;
2752 /* Clear the read queue */
2753 /* sa_ignore FREED_MEMORY */
2754 while ((sq = TAILQ_FIRST(&inp->read_queue)) != NULL) {
2755 /* Its only abandoned if it had data left */
2757 SCTP_STAT_INCR(sctps_left_abandon);
2759 TAILQ_REMOVE(&inp->read_queue, sq, next);
2760 sctp_free_remote_addr(sq->whoFrom);
2762 so->so_rcv.sb_cc -= sq->length;
2764 sctp_m_freem(sq->data);
2768 * no need to free the net count, since at this point all
2771 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_readq, sq);
2772 SCTP_DECR_READQ_COUNT();
2774 /* Now the sctp_pcb things */
2776 * free each asoc if it is not already closed/free. we can't use the
2777 * macro here since le_next will get freed as part of the
2778 * sctp_free_assoc() call.
2783 ipsec4_delete_pcbpolicy(ip_pcb);
2786 /* Unlocks not needed since the socket is gone now */
2788 if (ip_pcb->inp_options) {
2789 (void)sctp_m_free(ip_pcb->inp_options);
2790 ip_pcb->inp_options = 0;
2792 if (ip_pcb->inp_moptions) {
2793 ip_freemoptions(ip_pcb->inp_moptions);
2794 ip_pcb->inp_moptions = 0;
2797 if (ip_pcb->inp_vflag & INP_IPV6) {
2798 struct in6pcb *in6p;
2800 in6p = (struct in6pcb *)inp;
2801 ip6_freepcbopts(in6p->in6p_outputopts);
2804 ip_pcb->inp_vflag = 0;
2805 /* free up authentication fields */
2806 if (inp->sctp_ep.local_auth_chunks != NULL)
2807 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2808 if (inp->sctp_ep.local_hmacs != NULL)
2809 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2811 shared_key = LIST_FIRST(&inp->sctp_ep.shared_keys);
2812 while (shared_key) {
2813 LIST_REMOVE(shared_key, next);
2814 sctp_free_sharedkey(shared_key);
2815 /* sa_ignore FREED_MEMORY */
2816 shared_key = LIST_FIRST(&inp->sctp_ep.shared_keys);
2819 inp_save = LIST_NEXT(inp, sctp_list);
2820 LIST_REMOVE(inp, sctp_list);
2822 /* fix any iterators only after out of the list */
2823 sctp_iterator_inp_being_freed(inp, inp_save);
2825 * if we have an address list the following will free the list of
2826 * ifaddr's that are set into this ep. Again macro limitations here,
2827 * since the LIST_FOREACH could be a bad idea.
2829 for ((laddr = LIST_FIRST(&inp->sctp_addr_list)); laddr != NULL;
2831 nladdr = LIST_NEXT(laddr, sctp_nxt_addr);
2832 sctp_remove_laddr(laddr);
2835 #ifdef SCTP_TRACK_FREED_ASOCS
2837 for ((asoc = LIST_FIRST(&inp->sctp_asoc_free_list)); asoc != NULL;
2839 nasoc = LIST_NEXT(asoc, sctp_tcblist);
2840 LIST_REMOVE(asoc, sctp_tcblist);
2841 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, asoc);
2842 SCTP_DECR_ASOC_COUNT();
2844 /* *** END TEMP CODE *** */
2846 /* Now lets see about freeing the EP hash table. */
2847 if (inp->sctp_tcbhash != NULL) {
2848 SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark);
2849 inp->sctp_tcbhash = NULL;
2851 /* Now we must put the ep memory back into the zone pool */
2852 SCTP_INP_LOCK_DESTROY(inp);
2853 SCTP_INP_READ_DESTROY(inp);
2854 SCTP_ASOC_CREATE_LOCK_DESTROY(inp);
2855 SCTP_INP_INFO_WUNLOCK();
2857 SCTP_ITERATOR_UNLOCK();
2859 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
2860 SCTP_DECR_EP_COUNT();
2866 sctp_findnet(struct sctp_tcb *stcb, struct sockaddr *addr)
2868 struct sctp_nets *net;
2870 /* locate the address */
2871 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2872 if (sctp_cmpaddr(addr, (struct sockaddr *)&net->ro._l_addr))
2880 sctp_is_address_on_local_host(struct sockaddr *addr, uint32_t vrf_id)
2882 struct sctp_ifa *sctp_ifa;
2884 sctp_ifa = sctp_find_ifa_by_addr(addr, vrf_id, 0);
2893 sctp_set_initial_cc_param(struct sctp_tcb *stcb, struct sctp_nets *net)
2895 net->cwnd = min((net->mtu * 4), max((2 * net->mtu), SCTP_INITIAL_CWND));
2896 /* we always get at LEAST 2 MTU's */
2897 if (net->cwnd < (2 * net->mtu)) {
2898 net->cwnd = 2 * net->mtu;
2900 net->ssthresh = stcb->asoc.peers_rwnd;
2906 * add's a remote endpoint address, done with the INIT/INIT-ACK as well as
2907 * when a ASCONF arrives that adds it. It will also initialize all the cwnd
2911 sctp_add_remote_addr(struct sctp_tcb *stcb, struct sockaddr *newaddr,
2912 int set_scope, int from)
2915 * The following is redundant to the same lines in the
2916 * sctp_aloc_assoc() but is needed since other's call the add
2919 struct sctp_nets *net, *netfirst;
2922 SCTPDBG(SCTP_DEBUG_PCB1, "Adding an address (from:%d) to the peer: ",
2924 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, newaddr);
2926 netfirst = sctp_findnet(stcb, newaddr);
2929 * Lie and return ok, we don't want to make the association
2930 * go away for this behavior. It will happen in the TCP
2931 * model in a connected socket. It does not reach the hash
2932 * table until after the association is built so it can't be
2933 * found. Mark as reachable, since the initial creation will
2934 * have been cleared and the NOT_IN_ASSOC flag will have
2935 * been added... and we don't want to end up removing it
2938 if (netfirst->dest_state & SCTP_ADDR_UNCONFIRMED) {
2939 netfirst->dest_state = (SCTP_ADDR_REACHABLE |
2940 SCTP_ADDR_UNCONFIRMED);
2942 netfirst->dest_state = SCTP_ADDR_REACHABLE;
2948 if (newaddr->sa_family == AF_INET) {
2949 struct sockaddr_in *sin;
2951 sin = (struct sockaddr_in *)newaddr;
2952 if (sin->sin_addr.s_addr == 0) {
2953 /* Invalid address */
2956 /* zero out the bzero area */
2957 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
2959 /* assure len is set */
2960 sin->sin_len = sizeof(struct sockaddr_in);
2962 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
2963 stcb->ipv4_local_scope = 1;
2965 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
2966 stcb->asoc.ipv4_local_scope = 1;
2968 #endif /* SCTP_DONT_DO_PRIVADDR_SCOPE */
2970 /* Validate the address is in scope */
2971 if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) &&
2972 (stcb->asoc.ipv4_local_scope == 0)) {
2976 } else if (newaddr->sa_family == AF_INET6) {
2977 struct sockaddr_in6 *sin6;
2979 sin6 = (struct sockaddr_in6 *)newaddr;
2980 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2981 /* Invalid address */
2984 /* assure len is set */
2985 sin6->sin6_len = sizeof(struct sockaddr_in6);
2987 if (sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id)) {
2988 stcb->asoc.loopback_scope = 1;
2989 stcb->asoc.local_scope = 0;
2990 stcb->asoc.ipv4_local_scope = 1;
2991 stcb->asoc.site_scope = 1;
2992 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
2994 * If the new destination is a LINK_LOCAL we
2995 * must have common site scope. Don't set
2996 * the local scope since we may not share
2997 * all links, only loopback can do this.
2998 * Links on the local network would also be
2999 * on our private network for v4 too.
3001 stcb->asoc.ipv4_local_scope = 1;
3002 stcb->asoc.site_scope = 1;
3003 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
3005 * If the new destination is SITE_LOCAL then
3006 * we must have site scope in common.
3008 stcb->asoc.site_scope = 1;
3011 /* Validate the address is in scope */
3012 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) &&
3013 (stcb->asoc.loopback_scope == 0)) {
3015 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) &&
3016 (stcb->asoc.local_scope == 0)) {
3018 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) &&
3019 (stcb->asoc.site_scope == 0)) {
3024 /* not supported family type */
3027 net = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_net, struct sctp_nets);
3031 SCTP_INCR_RADDR_COUNT();
3032 bzero(net, sizeof(*net));
3033 (void)SCTP_GETTIME_TIMEVAL(&net->start_time);
3034 memcpy(&net->ro._l_addr, newaddr, newaddr->sa_len);
3035 if (newaddr->sa_family == AF_INET) {
3036 ((struct sockaddr_in *)&net->ro._l_addr)->sin_port = stcb->rport;
3037 } else if (newaddr->sa_family == AF_INET6) {
3038 ((struct sockaddr_in6 *)&net->ro._l_addr)->sin6_port = stcb->rport;
3040 net->addr_is_local = sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id);
3041 if (net->addr_is_local && ((set_scope || (from == SCTP_ADDR_IS_CONFIRMED)))) {
3042 stcb->asoc.loopback_scope = 1;
3043 stcb->asoc.ipv4_local_scope = 1;
3044 stcb->asoc.local_scope = 0;
3045 stcb->asoc.site_scope = 1;
3048 net->failure_threshold = stcb->asoc.def_net_failure;
3049 if (addr_inscope == 0) {
3050 net->dest_state = (SCTP_ADDR_REACHABLE |
3051 SCTP_ADDR_OUT_OF_SCOPE);
3053 if (from == SCTP_ADDR_IS_CONFIRMED)
3054 /* SCTP_ADDR_IS_CONFIRMED is passed by connect_x */
3055 net->dest_state = SCTP_ADDR_REACHABLE;
3057 net->dest_state = SCTP_ADDR_REACHABLE |
3058 SCTP_ADDR_UNCONFIRMED;
3060 net->RTO = stcb->asoc.initial_rto;
3061 stcb->asoc.numnets++;
3062 *(&net->ref_count) = 1;
3063 net->tos_flowlabel = 0;
3065 if (newaddr->sa_family == AF_INET)
3066 net->tos_flowlabel = stcb->asoc.default_tos;
3069 if (newaddr->sa_family == AF_INET6)
3070 net->tos_flowlabel = stcb->asoc.default_flowlabel;
3072 /* Init the timer structure */
3073 SCTP_OS_TIMER_INIT(&net->rxt_timer.timer);
3074 SCTP_OS_TIMER_INIT(&net->fr_timer.timer);
3075 SCTP_OS_TIMER_INIT(&net->pmtu_timer.timer);
3077 /* Now generate a route for this guy */
3078 /* KAME hack: embed scopeid */
3079 if (newaddr->sa_family == AF_INET6) {
3080 struct sockaddr_in6 *sin6;
3082 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3083 (void)sa6_embedscope(sin6, ip6_use_defzone);
3084 sin6->sin6_scope_id = 0;
3086 SCTP_RTALLOC((sctp_route_t *) & net->ro, stcb->asoc.vrf_id,
3087 stcb->asoc.table_id);
3089 if (newaddr->sa_family == AF_INET6) {
3090 struct sockaddr_in6 *sin6;
3092 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3093 (void)sa6_recoverscope(sin6);
3095 if (SCTP_ROUTE_HAS_VALID_IFN(&net->ro)) {
3096 /* Get source address */
3097 net->ro._s_addr = sctp_source_address_selection(stcb->sctp_ep,
3099 (sctp_route_t *) & net->ro,
3103 /* Now get the interface MTU */
3104 if (net->ro._s_addr && net->ro._s_addr->ifn_p) {
3105 net->mtu = SCTP_GATHER_MTU_FROM_INTFC(net->ro._s_addr->ifn_p);
3109 #ifdef SCTP_PRINT_FOR_B_AND_M
3110 SCTP_PRINTF("We have found an interface mtu of %d\n", net->mtu);
3112 if (net->mtu == 0) {
3114 net->mtu = SCTP_DEFAULT_MTU;
3118 rmtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, net->ro.ro_rt);
3119 #ifdef SCTP_PRINT_FOR_B_AND_M
3120 SCTP_PRINTF("The route mtu is %d\n", rmtu);
3124 * Start things off to match mtu of
3127 SCTP_SET_MTU_OF_ROUTE(&net->ro._l_addr.sa,
3128 net->ro.ro_rt, net->mtu);
3131 * we take the route mtu over the interface,
3132 * since the route may be leading out the
3133 * loopback, or a different interface.
3138 if (from == SCTP_ALLOC_ASOC) {
3139 #ifdef SCTP_PRINT_FOR_B_AND_M
3140 SCTP_PRINTF("New assoc sets mtu to :%d\n", net->mtu);
3142 stcb->asoc.smallest_mtu = net->mtu;
3145 net->mtu = stcb->asoc.smallest_mtu;
3147 if (stcb->asoc.smallest_mtu > net->mtu) {
3148 #ifdef SCTP_PRINT_FOR_B_AND_M
3149 SCTP_PRINTF("new address mtu:%d smaller than smallest:%d\n",
3150 net->mtu, stcb->asoc.smallest_mtu);
3152 stcb->asoc.smallest_mtu = net->mtu;
3155 * We take the max of the burst limit times a MTU or the
3156 * INITIAL_CWND. We then limit this to 4 MTU's of sending.
3158 sctp_set_initial_cc_param(stcb, net);
3161 #if defined(SCTP_CWND_MONITOR) || defined(SCTP_CWND_LOGGING)
3162 sctp_log_cwnd(stcb, net, 0, SCTP_CWND_INITIALIZATION);
3166 * CMT: CUC algo - set find_pseudo_cumack to TRUE (1) at beginning
3167 * of assoc (2005/06/27, iyengar@cis.udel.edu)
3169 net->find_pseudo_cumack = 1;
3170 net->find_rtx_pseudo_cumack = 1;
3171 net->src_addr_selected = 0;
3172 netfirst = TAILQ_FIRST(&stcb->asoc.nets);
3173 if (net->ro.ro_rt == NULL) {
3174 /* Since we have no route put it at the back */
3175 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
3176 } else if (netfirst == NULL) {
3177 /* We are the first one in the pool. */
3178 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
3179 } else if (netfirst->ro.ro_rt == NULL) {
3181 * First one has NO route. Place this one ahead of the first
3184 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
3185 } else if (net->ro.ro_rt->rt_ifp != netfirst->ro.ro_rt->rt_ifp) {
3187 * This one has a different interface than the one at the
3188 * top of the list. Place it ahead.
3190 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
3193 * Ok we have the same interface as the first one. Move
3194 * forward until we find either a) one with a NULL route...
3195 * insert ahead of that b) one with a different ifp.. insert
3196 * after that. c) end of the list.. insert at the tail.
3198 struct sctp_nets *netlook;
3201 netlook = TAILQ_NEXT(netfirst, sctp_next);
3202 if (netlook == NULL) {
3203 /* End of the list */
3204 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
3206 } else if (netlook->ro.ro_rt == NULL) {
3207 /* next one has NO route */
3208 TAILQ_INSERT_BEFORE(netfirst, net, sctp_next);
3210 } else if (netlook->ro.ro_rt->rt_ifp != net->ro.ro_rt->rt_ifp) {
3211 TAILQ_INSERT_AFTER(&stcb->asoc.nets, netlook,
3217 } while (netlook != NULL);
3220 /* got to have a primary set */
3221 if (stcb->asoc.primary_destination == 0) {
3222 stcb->asoc.primary_destination = net;
3223 } else if ((stcb->asoc.primary_destination->ro.ro_rt == NULL) &&
3225 ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) {
3226 /* No route to current primary adopt new primary */
3227 stcb->asoc.primary_destination = net;
3229 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb,
3231 /* Validate primary is first */
3232 net = TAILQ_FIRST(&stcb->asoc.nets);
3233 if ((net != stcb->asoc.primary_destination) &&
3234 (stcb->asoc.primary_destination)) {
3236 * first one on the list is NOT the primary sctp_cmpaddr()
3237 * is much more efficent if the primary is the first on the
3240 TAILQ_REMOVE(&stcb->asoc.nets,
3241 stcb->asoc.primary_destination, sctp_next);
3242 TAILQ_INSERT_HEAD(&stcb->asoc.nets,
3243 stcb->asoc.primary_destination, sctp_next);
3250 * allocate an association and add it to the endpoint. The caller must be
3251 * careful to add all additional addresses once they are know right away or
3252 * else the assoc will be may experience a blackout scenario.
3255 sctp_aloc_assoc(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
3256 int for_a_init, int *error, uint32_t override_tag, uint32_t vrf_id)
3258 struct sctp_tcb *stcb;
3259 struct sctp_association *asoc;
3260 struct sctpasochead *head;
3265 * Assumption made here: Caller has done a
3266 * sctp_findassociation_ep_addr(ep, addr's); to make sure the
3267 * address does not exist already.
3269 if (sctppcbinfo.ipi_count_asoc >= SCTP_MAX_NUM_OF_ASOC) {
3270 /* Hit max assoc, sorry no more */
3274 if (firstaddr == NULL) {
3278 SCTP_INP_RLOCK(inp);
3279 if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
3281 * If its in the TCP pool, its NOT allowed to create an
3282 * association. The parent listener needs to call
3283 * sctp_aloc_assoc.. or the one-2-many socket. If a peeled
3284 * off, or connected one does this.. its an error.
3286 SCTP_INP_RUNLOCK(inp);
3290 SCTPDBG(SCTP_DEBUG_PCB3, "Allocate an association for peer:");
3293 SCTPDBG_ADDR(SCTP_DEBUG_PCB3, firstaddr);
3294 SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
3295 ntohs(((struct sockaddr_in *)firstaddr)->sin_port));
3297 SCTPDBG(SCTP_DEBUG_PCB3, "None\n");
3299 #endif /* SCTP_DEBUG */
3300 if (firstaddr->sa_family == AF_INET) {
3301 struct sockaddr_in *sin;
3303 sin = (struct sockaddr_in *)firstaddr;
3304 if ((sin->sin_port == 0) || (sin->sin_addr.s_addr == 0)) {
3305 /* Invalid address */
3306 SCTP_INP_RUNLOCK(inp);
3310 rport = sin->sin_port;
3311 } else if (firstaddr->sa_family == AF_INET6) {
3312 struct sockaddr_in6 *sin6;
3314 sin6 = (struct sockaddr_in6 *)firstaddr;
3315 if ((sin6->sin6_port == 0) ||
3316 (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))) {
3317 /* Invalid address */
3318 SCTP_INP_RUNLOCK(inp);
3322 rport = sin6->sin6_port;
3324 /* not supported family type */
3325 SCTP_INP_RUNLOCK(inp);
3329 SCTP_INP_RUNLOCK(inp);
3330 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
3332 * If you have not performed a bind, then we need to do the
3333 * ephemerial bind for you.
3335 if ((err = sctp_inpcb_bind(inp->sctp_socket,
3336 (struct sockaddr *)NULL,
3337 (struct thread *)NULL
3339 /* bind error, probably perm */
3344 stcb = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_asoc, struct sctp_tcb);
3346 /* out of memory? */
3350 SCTP_INCR_ASOC_COUNT();
3352 bzero(stcb, sizeof(*stcb));
3354 SCTP_TCB_LOCK_INIT(stcb);
3355 SCTP_TCB_SEND_LOCK_INIT(stcb);
3356 /* setup back pointer's */
3357 stcb->sctp_ep = inp;
3358 stcb->sctp_socket = inp->sctp_socket;
3359 if ((err = sctp_init_asoc(inp, asoc, for_a_init, override_tag, vrf_id))) {
3361 SCTP_TCB_LOCK_DESTROY(stcb);
3362 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
3363 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
3364 SCTP_DECR_ASOC_COUNT();
3369 stcb->rport = rport;
3370 SCTP_INP_INFO_WLOCK();
3371 SCTP_INP_WLOCK(inp);
3372 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
3373 /* inpcb freed while alloc going on */
3374 SCTP_TCB_LOCK_DESTROY(stcb);
3375 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
3376 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
3377 SCTP_INP_WUNLOCK(inp);
3378 SCTP_INP_INFO_WUNLOCK();
3379 SCTP_DECR_ASOC_COUNT();
3383 SCTP_TCB_LOCK(stcb);
3385 /* now that my_vtag is set, add it to the hash */
3386 head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
3387 sctppcbinfo.hashasocmark)];
3388 /* put it in the bucket in the vtag hash of assoc's for the system */
3389 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
3390 SCTP_INP_INFO_WUNLOCK();
3392 if ((err = sctp_add_remote_addr(stcb, firstaddr, SCTP_DO_SETSCOPE, SCTP_ALLOC_ASOC))) {
3393 /* failure.. memory error? */
3395 SCTP_FREE(asoc->strmout);
3396 if (asoc->mapping_array)
3397 SCTP_FREE(asoc->mapping_array);
3399 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
3400 SCTP_DECR_ASOC_COUNT();
3401 SCTP_TCB_LOCK_DESTROY(stcb);
3402 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
3403 SCTP_INP_WUNLOCK(inp);
3407 /* Init all the timers */
3408 SCTP_OS_TIMER_INIT(&asoc->hb_timer.timer);
3409 SCTP_OS_TIMER_INIT(&asoc->dack_timer.timer);
3410 SCTP_OS_TIMER_INIT(&asoc->strreset_timer.timer);
3411 SCTP_OS_TIMER_INIT(&asoc->asconf_timer.timer);
3412 SCTP_OS_TIMER_INIT(&asoc->shut_guard_timer.timer);
3413 SCTP_OS_TIMER_INIT(&asoc->autoclose_timer.timer);
3414 SCTP_OS_TIMER_INIT(&asoc->delayed_event_timer.timer);
3416 LIST_INSERT_HEAD(&inp->sctp_asoc_list, stcb, sctp_tcblist);
3417 /* now file the port under the hash as well */
3418 if (inp->sctp_tcbhash != NULL) {
3419 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(stcb->rport,
3420 inp->sctp_hashmark)];
3421 LIST_INSERT_HEAD(head, stcb, sctp_tcbhash);
3423 SCTP_INP_WUNLOCK(inp);
3424 SCTPDBG(SCTP_DEBUG_PCB1, "Association %p now allocated\n", stcb);
3430 sctp_remove_net(struct sctp_tcb *stcb, struct sctp_nets *net)
3432 struct sctp_association *asoc;
3436 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
3437 if (net == asoc->primary_destination) {
3439 struct sctp_nets *lnet;
3441 lnet = TAILQ_FIRST(&asoc->nets);
3442 /* Try to find a confirmed primary */
3443 asoc->primary_destination = sctp_find_alternate_net(stcb, lnet, 0);
3445 if (net == asoc->last_data_chunk_from) {
3447 asoc->last_data_chunk_from = TAILQ_FIRST(&asoc->nets);
3449 if (net == asoc->last_control_chunk_from) {
3451 asoc->last_control_chunk_from = NULL;
3453 sctp_free_remote_addr(net);
3457 * remove a remote endpoint address from an association, it will fail if the
3458 * address does not exist.
3461 sctp_del_remote_addr(struct sctp_tcb *stcb, struct sockaddr *remaddr)
3464 * Here we need to remove a remote address. This is quite simple, we
3465 * first find it in the list of address for the association
3466 * (tasoc->asoc.nets) and then if it is there, we do a LIST_REMOVE
3467 * on that item. Note we do not allow it to be removed if there are
3468 * no other addresses.
3470 struct sctp_association *asoc;
3471 struct sctp_nets *net, *net_tmp;
3475 /* locate the address */
3476 for (net = TAILQ_FIRST(&asoc->nets); net != NULL; net = net_tmp) {
3477 net_tmp = TAILQ_NEXT(net, sctp_next);
3478 if (net->ro._l_addr.sa.sa_family != remaddr->sa_family) {
3481 if (sctp_cmpaddr((struct sockaddr *)&net->ro._l_addr,
3483 /* we found the guy */
3484 if (asoc->numnets < 2) {
3485 /* Must have at LEAST two remote addresses */
3488 sctp_remove_net(stcb, net);
3499 sctp_add_vtag_to_timewait(struct sctp_inpcb *inp, uint32_t tag, uint32_t time)
3501 struct sctpvtaghead *chain;
3502 struct sctp_tagblock *twait_block;
3506 (void)SCTP_GETTIME_TIMEVAL(&now);
3507 chain = &sctppcbinfo.vtag_timewait[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
3509 if (!SCTP_LIST_EMPTY(chain)) {
3510 /* Block(s) present, lets find space, and expire on the fly */
3511 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
3512 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
3513 if ((twait_block->vtag_block[i].v_tag == 0) &&
3515 twait_block->vtag_block[i].tv_sec_at_expire =
3517 twait_block->vtag_block[i].v_tag = tag;
3519 } else if ((twait_block->vtag_block[i].v_tag) &&
3520 ((long)twait_block->vtag_block[i].tv_sec_at_expire >
3522 /* Audit expires this guy */
3523 twait_block->vtag_block[i].tv_sec_at_expire = 0;
3524 twait_block->vtag_block[i].v_tag = 0;
3526 /* Reuse it for my new tag */
3527 twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec + SCTP_TIME_WAIT;
3528 twait_block->vtag_block[0].v_tag = tag;
3535 * We only do up to the block where we can
3536 * place our tag for audits
3542 /* Need to add a new block to chain */
3544 SCTP_MALLOC(twait_block, struct sctp_tagblock *,
3545 sizeof(struct sctp_tagblock), "TimeWait");
3546 if (twait_block == NULL) {
3549 memset(twait_block, 0, sizeof(struct sctp_tagblock));
3550 LIST_INSERT_HEAD(chain, twait_block, sctp_nxt_tagblock);
3551 twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec +
3553 twait_block->vtag_block[0].v_tag = tag;
3559 sctp_iterator_asoc_being_freed(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
3561 struct sctp_iterator *it;
3564 * Unlock the tcb lock we do this so we avoid a dead lock scenario
3565 * where the iterator is waiting on the TCB lock and the TCB lock is
3566 * waiting on the iterator lock.
3568 it = stcb->asoc.stcb_starting_point_for_iterator;
3572 if (it->inp != stcb->sctp_ep) {
3573 /* hmm, focused on the wrong one? */
3576 if (it->stcb != stcb) {
3579 it->stcb = LIST_NEXT(stcb, sctp_tcblist);
3580 if (it->stcb == NULL) {
3581 /* done with all asoc's in this assoc */
3582 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
3585 it->inp = LIST_NEXT(inp, sctp_list);
3592 * Free the association after un-hashing the remote port.
3595 sctp_free_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int from_inpcbfree, int from_location)
3598 struct sctp_association *asoc;
3599 struct sctp_nets *net, *prev;
3600 struct sctp_laddr *laddr;
3601 struct sctp_tmit_chunk *chk;
3602 struct sctp_asconf_addr *aparam;
3603 struct sctp_stream_reset_list *liste;
3604 struct sctp_queued_to_read *sq;
3605 struct sctp_stream_queue_pending *sp;
3606 sctp_sharedkey_t *shared_key;
3611 /* first, lets purge the entry from the hash table. */
3613 #ifdef SCTP_LOG_CLOSING
3614 sctp_log_closing(inp, stcb, 6);
3616 if (stcb->asoc.state == 0) {
3617 #ifdef SCTP_LOG_CLOSING
3618 sctp_log_closing(inp, NULL, 7);
3620 /* there is no asoc, really TSNH :-0 */
3624 if (stcb->freed_from_where == 0) {
3625 /* Only record the first place free happened from */
3626 stcb->freed_from_where = from_location;
3631 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3632 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
3633 /* nothing around */
3636 so = inp->sctp_socket;
3639 * We used timer based freeing if a reader or writer is in the way.
3640 * So we first check if we are actually being called from a timer,
3641 * if so we abort early if a reader or writer is still in the way.
3643 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) &&
3644 (from_inpcbfree == SCTP_NORMAL_PROC)) {
3646 * is it the timer driving us? if so are the reader/writers
3649 if (stcb->asoc.refcnt) {
3650 /* nope, reader or writer in the way */
3651 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
3652 /* no asoc destroyed */
3653 SCTP_TCB_UNLOCK(stcb);
3654 #ifdef SCTP_LOG_CLOSING
3655 sctp_log_closing(inp, stcb, 8);
3660 /* now clean up any other timers */
3661 (void)SCTP_OS_TIMER_STOP(&asoc->hb_timer.timer);
3662 asoc->hb_timer.self = NULL;
3663 (void)SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
3664 asoc->dack_timer.self = NULL;
3665 (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
3667 * For stream reset we don't blast this unless
3668 * it is a str-reset timer, it might be the
3669 * free-asoc timer which we DON'T want to
3672 if (asoc->strreset_timer.type == SCTP_TIMER_TYPE_STRRESET)
3673 asoc->strreset_timer.self = NULL;
3674 (void)SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
3675 asoc->asconf_timer.self = NULL;
3676 (void)SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
3677 asoc->autoclose_timer.self = NULL;
3678 (void)SCTP_OS_TIMER_STOP(&asoc->shut_guard_timer.timer);
3679 asoc->shut_guard_timer.self = NULL;
3680 (void)SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
3681 asoc->delayed_event_timer.self = NULL;
3682 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3683 (void)SCTP_OS_TIMER_STOP(&net->fr_timer.timer);
3684 net->fr_timer.self = NULL;
3685 (void)SCTP_OS_TIMER_STOP(&net->rxt_timer.timer);
3686 net->rxt_timer.self = NULL;
3687 (void)SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
3688 net->pmtu_timer.self = NULL;
3690 /* Now the read queue needs to be cleaned up (only once) */
3692 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0) {
3693 stcb->asoc.state |= SCTP_STATE_ABOUT_TO_BE_FREED;
3694 SCTP_INP_READ_LOCK(inp);
3695 TAILQ_FOREACH(sq, &inp->read_queue, next) {
3696 if (sq->stcb == stcb) {
3697 sq->do_not_ref_stcb = 1;
3698 sq->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
3700 * If there is no end, there never will be
3703 if (sq->end_added == 0) {
3704 /* Held for PD-API clear that. */
3705 sq->pdapi_aborted = 1;
3706 sq->held_length = 0;
3707 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT)) {
3709 * Need to add a PD-API
3710 * aborted indication.
3711 * Setting the control_pdapi
3712 * assures that it will be
3713 * added right after this
3718 stcb->asoc.control_pdapi = sq;
3719 strseq = (sq->sinfo_stream << 16) | sq->sinfo_ssn;
3720 sctp_notify_partial_delivery_indication(stcb,
3721 SCTP_PARTIAL_DELIVERY_ABORTED, 1, strseq);
3722 stcb->asoc.control_pdapi = NULL;
3725 /* Add an end to wake them */
3730 SCTP_INP_READ_UNLOCK(inp);
3731 if (stcb->block_entry) {
3733 stcb->block_entry->error = ECONNRESET;
3734 stcb->block_entry = NULL;
3737 if ((from_inpcbfree != SCTP_PCBFREE_FORCE) && (stcb->asoc.refcnt)) {
3739 * reader or writer in the way, we have hopefully given him
3740 * something to chew on above.
3742 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
3743 SCTP_TCB_UNLOCK(stcb);
3745 SCTP_INP_RLOCK(inp);
3746 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3747 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
3748 /* nothing around */
3751 /* Wake any reader/writers */
3752 sctp_sorwakeup(inp, so);
3753 sctp_sowwakeup(inp, so);
3755 SCTP_INP_RUNLOCK(inp);
3758 #ifdef SCTP_LOG_CLOSING
3759 sctp_log_closing(inp, stcb, 9);
3761 /* no asoc destroyed */
3764 #ifdef SCTP_LOG_CLOSING
3765 sctp_log_closing(inp, stcb, 10);
3768 * When I reach here, no others want to kill the assoc yet.. and I
3769 * own the lock. Now its possible an abort comes in when I do the
3770 * lock exchange below to grab all the locks to do the final take
3771 * out. to prevent this we increment the count, which will start a
3772 * timer and blow out above thus assuring us that we hold exclusive
3773 * killing of the asoc. Note that after getting back the TCB lock we
3774 * will go ahead and increment the counter back up and stop any
3775 * timer a passing stranger may have started :-S
3777 if (from_inpcbfree == SCTP_NORMAL_PROC) {
3778 atomic_add_int(&stcb->asoc.refcnt, 1);
3780 SCTP_TCB_UNLOCK(stcb);
3782 SCTP_ITERATOR_LOCK();
3783 SCTP_INP_INFO_WLOCK();
3784 SCTP_INP_WLOCK(inp);
3785 SCTP_TCB_LOCK(stcb);
3787 /* Double check the GONE flag */
3788 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3789 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
3790 /* nothing around */
3793 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3794 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3796 * For TCP type we need special handling when we are
3797 * connected. We also include the peel'ed off ones to.
3799 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3800 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
3801 inp->sctp_flags |= SCTP_PCB_FLAGS_WAS_CONNECTED;
3804 if (so->so_rcv.sb_cc == 0) {
3805 so->so_state &= ~(SS_ISCONNECTING |
3806 SS_ISDISCONNECTING |
3811 sctp_sowwakeup(inp, so);
3812 sctp_sorwakeup(inp, so);
3818 * Make it invalid too, that way if its about to run it will abort
3821 sctp_iterator_asoc_being_freed(inp, stcb);
3822 /* re-increment the lock */
3823 if (from_inpcbfree == SCTP_NORMAL_PROC) {
3824 atomic_add_int(&stcb->asoc.refcnt, -1);
3827 if (inp->sctp_tcbhash) {
3828 LIST_REMOVE(stcb, sctp_tcbhash);
3830 if (stcb->asoc.in_restart_hash) {
3831 LIST_REMOVE(stcb, sctp_tcbrestarhash);
3833 /* Now lets remove it from the list of ALL associations in the EP */
3834 LIST_REMOVE(stcb, sctp_tcblist);
3835 if (from_inpcbfree == SCTP_NORMAL_PROC) {
3836 SCTP_INP_INCR_REF(inp);
3837 SCTP_INP_WUNLOCK(inp);
3838 SCTP_ITERATOR_UNLOCK();
3840 /* pull from vtag hash */
3841 LIST_REMOVE(stcb, sctp_asocs);
3842 sctp_add_vtag_to_timewait(inp, asoc->my_vtag, SCTP_TIME_WAIT);
3846 * Now restop the timers to be sure - this is paranoia at is finest!
3848 (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
3849 (void)SCTP_OS_TIMER_STOP(&asoc->hb_timer.timer);
3850 (void)SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
3851 (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
3852 (void)SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
3853 (void)SCTP_OS_TIMER_STOP(&asoc->shut_guard_timer.timer);
3854 (void)SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
3855 (void)SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
3857 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3858 (void)SCTP_OS_TIMER_STOP(&net->fr_timer.timer);
3859 (void)SCTP_OS_TIMER_STOP(&net->rxt_timer.timer);
3860 (void)SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
3863 asoc->strreset_timer.type = SCTP_TIMER_TYPE_NONE;
3866 * The chunk lists and such SHOULD be empty but we check them just
3869 /* anything on the wheel needs to be removed */
3870 for (i = 0; i < asoc->streamoutcnt; i++) {
3871 struct sctp_stream_out *outs;
3873 outs = &asoc->strmout[i];
3874 /* now clean up any chunks here */
3875 sp = TAILQ_FIRST(&outs->outqueue);
3877 TAILQ_REMOVE(&outs->outqueue, sp, next);
3879 sctp_m_freem(sp->data);
3881 sp->tail_mbuf = NULL;
3883 sctp_free_remote_addr(sp->net);
3884 sctp_free_spbufspace(stcb, asoc, sp);
3885 /* Free the zone stuff */
3886 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_strmoq, sp);
3887 SCTP_DECR_STRMOQ_COUNT();
3888 /* sa_ignore FREED_MEMORY */
3889 sp = TAILQ_FIRST(&outs->outqueue);
3893 /* sa_ignore FREED_MEMORY */
3894 while ((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) {
3895 TAILQ_REMOVE(&asoc->resetHead, liste, next_resp);
3899 sq = TAILQ_FIRST(&asoc->pending_reply_queue);
3901 TAILQ_REMOVE(&asoc->pending_reply_queue, sq, next);
3903 sctp_m_freem(sq->data);
3906 sctp_free_remote_addr(sq->whoFrom);
3909 /* Free the ctl entry */
3910 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_readq, sq);
3911 SCTP_DECR_READQ_COUNT();
3912 /* sa_ignore FREED_MEMORY */
3913 sq = TAILQ_FIRST(&asoc->pending_reply_queue);
3916 chk = TAILQ_FIRST(&asoc->free_chunks);
3918 TAILQ_REMOVE(&asoc->free_chunks, chk, sctp_next);
3920 sctp_m_freem(chk->data);
3924 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3925 SCTP_DECR_CHK_COUNT();
3926 atomic_subtract_int(&sctppcbinfo.ipi_free_chunks, 1);
3927 asoc->free_chunk_cnt--;
3928 /* sa_ignore FREED_MEMORY */
3929 chk = TAILQ_FIRST(&asoc->free_chunks);
3931 /* pending send queue SHOULD be empty */
3932 if (!TAILQ_EMPTY(&asoc->send_queue)) {
3933 chk = TAILQ_FIRST(&asoc->send_queue);
3935 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
3937 sctp_m_freem(chk->data);
3941 sctp_free_remote_addr(chk->whoTo);
3942 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3943 SCTP_DECR_CHK_COUNT();
3944 /* sa_ignore FREED_MEMORY */
3945 chk = TAILQ_FIRST(&asoc->send_queue);
3950 printf("Freed %d from send_queue\n", ccnt);
3954 /* sent queue SHOULD be empty */
3955 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3956 chk = TAILQ_FIRST(&asoc->sent_queue);
3958 TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
3960 sctp_m_freem(chk->data);
3964 sctp_free_remote_addr(chk->whoTo);
3965 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3966 SCTP_DECR_CHK_COUNT();
3967 /* sa_ignore FREED_MEMORY */
3968 chk = TAILQ_FIRST(&asoc->sent_queue);
3973 printf("Freed %d from sent_queue\n", ccnt);
3977 /* control queue MAY not be empty */
3978 if (!TAILQ_EMPTY(&asoc->control_send_queue)) {
3979 chk = TAILQ_FIRST(&asoc->control_send_queue);
3981 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
3983 sctp_m_freem(chk->data);
3987 sctp_free_remote_addr(chk->whoTo);
3988 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3989 SCTP_DECR_CHK_COUNT();
3990 /* sa_ignore FREED_MEMORY */
3991 chk = TAILQ_FIRST(&asoc->control_send_queue);
3996 printf("Freed %d from ctrl_queue\n", ccnt);
4000 if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
4001 chk = TAILQ_FIRST(&asoc->reasmqueue);
4003 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
4005 sctp_m_freem(chk->data);
4008 sctp_free_remote_addr(chk->whoTo);
4010 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
4011 SCTP_DECR_CHK_COUNT();
4012 /* sa_ignore FREED_MEMORY */
4013 chk = TAILQ_FIRST(&asoc->reasmqueue);
4018 printf("Freed %d from reasm_queue\n", ccnt);
4022 if (asoc->mapping_array) {
4023 SCTP_FREE(asoc->mapping_array);
4024 asoc->mapping_array = NULL;
4026 /* the stream outs */
4027 if (asoc->strmout) {
4028 SCTP_FREE(asoc->strmout);
4029 asoc->strmout = NULL;
4031 asoc->streamoutcnt = 0;
4033 struct sctp_queued_to_read *ctl;
4035 for (i = 0; i < asoc->streamincnt; i++) {
4036 if (!TAILQ_EMPTY(&asoc->strmin[i].inqueue)) {
4037 /* We have somethings on the streamin queue */
4038 ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue);
4040 TAILQ_REMOVE(&asoc->strmin[i].inqueue,
4042 sctp_free_remote_addr(ctl->whoFrom);
4044 sctp_m_freem(ctl->data);
4048 * We don't free the address here
4049 * since all the net's were freed
4052 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_readq, ctl);
4053 SCTP_DECR_READQ_COUNT();
4054 ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue);
4058 SCTP_FREE(asoc->strmin);
4059 asoc->strmin = NULL;
4061 asoc->streamincnt = 0;
4062 while (!TAILQ_EMPTY(&asoc->nets)) {
4063 /* sa_ignore FREED_MEMORY */
4064 net = TAILQ_FIRST(&asoc->nets);
4065 /* pull from list */
4066 if ((sctppcbinfo.ipi_count_raddr == 0) || (prev == net)) {
4068 panic("no net's left alloc'ed, or list points to itself");
4073 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
4074 sctp_free_remote_addr(net);
4077 while (!SCTP_LIST_EMPTY(&asoc->sctp_restricted_addrs)) {
4078 /* sa_ignore FREED_MEMORY */
4079 laddr = LIST_FIRST(&asoc->sctp_restricted_addrs);
4080 sctp_remove_laddr(laddr);
4083 /* pending asconf (address) parameters */
4084 while (!TAILQ_EMPTY(&asoc->asconf_queue)) {
4085 /* sa_ignore FREED_MEMORY */
4086 aparam = TAILQ_FIRST(&asoc->asconf_queue);
4087 TAILQ_REMOVE(&asoc->asconf_queue, aparam, next);
4090 if (asoc->last_asconf_ack_sent != NULL) {
4091 sctp_m_freem(asoc->last_asconf_ack_sent);
4092 asoc->last_asconf_ack_sent = NULL;
4094 /* clean up auth stuff */
4095 if (asoc->local_hmacs)
4096 sctp_free_hmaclist(asoc->local_hmacs);
4097 if (asoc->peer_hmacs)
4098 sctp_free_hmaclist(asoc->peer_hmacs);
4100 if (asoc->local_auth_chunks)
4101 sctp_free_chunklist(asoc->local_auth_chunks);
4102 if (asoc->peer_auth_chunks)
4103 sctp_free_chunklist(asoc->peer_auth_chunks);
4105 sctp_free_authinfo(&asoc->authinfo);
4107 shared_key = LIST_FIRST(&asoc->shared_keys);
4108 while (shared_key) {
4109 LIST_REMOVE(shared_key, next);
4110 sctp_free_sharedkey(shared_key);
4111 /* sa_ignore FREED_MEMORY */
4112 shared_key = LIST_FIRST(&asoc->shared_keys);
4115 /* Insert new items here :> */
4117 /* Get rid of LOCK */
4118 SCTP_TCB_LOCK_DESTROY(stcb);
4119 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
4120 if (from_inpcbfree == SCTP_NORMAL_PROC) {
4121 SCTP_INP_INFO_WUNLOCK();
4122 SCTP_INP_RLOCK(inp);
4124 #ifdef SCTP_TRACK_FREED_ASOCS
4125 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4126 /* now clean up the tasoc itself */
4127 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
4128 SCTP_DECR_ASOC_COUNT();
4130 LIST_INSERT_HEAD(&inp->sctp_asoc_free_list, stcb, sctp_tcblist);
4133 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
4134 SCTP_DECR_ASOC_COUNT();
4136 if (from_inpcbfree == SCTP_NORMAL_PROC) {
4137 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4139 * If its NOT the inp_free calling us AND sctp_close
4140 * as been called, we call back...
4142 SCTP_INP_RUNLOCK(inp);
4144 * This will start the kill timer (if we are the
4145 * lastone) since we hold an increment yet. But this
4146 * is the only safe way to do this since otherwise
4147 * if the socket closes at the same time we are here
4148 * we might collide in the cleanup.
4150 sctp_inpcb_free(inp,
4151 SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
4152 SCTP_CALLED_DIRECTLY_NOCMPSET);
4153 SCTP_INP_DECR_REF(inp);
4156 /* The socket is still open. */
4157 SCTP_INP_DECR_REF(inp);
4160 if (from_inpcbfree == SCTP_NORMAL_PROC) {
4161 SCTP_INP_RUNLOCK(inp);
4164 /* destroyed the asoc */
4165 #ifdef SCTP_LOG_CLOSING
4166 sctp_log_closing(inp, NULL, 11);
4174 * determine if a destination is "reachable" based upon the addresses bound
4175 * to the current endpoint (e.g. only v4 or v6 currently bound)
4178 * FIX: if we allow assoc-level bindx(), then this needs to be fixed to use
4179 * assoc level v4/v6 flags, as the assoc *may* not have the same address
4180 * types bound as its endpoint
4183 sctp_destination_is_reachable(struct sctp_tcb *stcb, struct sockaddr *destaddr)
4185 struct sctp_inpcb *inp;
4189 * No locks here, the TCB, in all cases is already locked and an
4190 * assoc is up. There is either a INP lock by the caller applied (in
4191 * asconf case when deleting an address) or NOT in the HB case,
4192 * however if HB then the INP increment is up and the INP will not
4193 * be removed (on top of the fact that we have a TCB lock). So we
4194 * only want to read the sctp_flags, which is either bound-all or
4195 * not.. no protection needed since once an assoc is up you can't be
4196 * changing your binding.
4198 inp = stcb->sctp_ep;
4199 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4200 /* if bound all, destination is not restricted */
4202 * RRS: Question during lock work: Is this correct? If you
4203 * are bound-all you still might need to obey the V4--V6
4204 * flags??? IMO this bound-all stuff needs to be removed!
4208 /* NOTE: all "scope" checks are done when local addresses are added */
4209 if (destaddr->sa_family == AF_INET6) {
4210 answer = inp->ip_inp.inp.inp_vflag & INP_IPV6;
4211 } else if (destaddr->sa_family == AF_INET) {
4212 answer = inp->ip_inp.inp.inp_vflag & INP_IPV4;
4214 /* invalid family, so it's unreachable */
4221 * update the inp_vflags on an endpoint
4224 sctp_update_ep_vflag(struct sctp_inpcb *inp)
4226 struct sctp_laddr *laddr;
4228 /* first clear the flag */
4229 inp->ip_inp.inp.inp_vflag = 0;
4230 /* set the flag based on addresses on the ep list */
4231 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4232 if (laddr->ifa == NULL) {
4233 SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n",
4237 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
4240 if (laddr->ifa->address.sa.sa_family == AF_INET6) {
4241 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
4242 } else if (laddr->ifa->address.sa.sa_family == AF_INET) {
4243 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
4249 * Add the address to the endpoint local address list There is nothing to be
4250 * done if we are bound to all addresses
4253 sctp_add_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa, uint32_t action)
4255 struct sctp_laddr *laddr;
4260 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4261 /* You are already bound to all. You have it already */
4264 if (ifa->address.sa.sa_family == AF_INET6) {
4265 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
4266 /* Can't bind a non-useable addr. */
4270 /* first, is it already present? */
4271 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4272 if (laddr->ifa == ifa) {
4279 /* Not in the ep list */
4280 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, action);
4284 /* update inp_vflag flags */
4285 if (ifa->address.sa.sa_family == AF_INET6) {
4286 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
4287 } else if (ifa->address.sa.sa_family == AF_INET) {
4288 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
4296 * select a new (hopefully reachable) destination net (should only be used
4297 * when we deleted an ep addr that is the only usable source address to reach
4298 * the destination net)
4301 sctp_select_primary_destination(struct sctp_tcb *stcb)
4303 struct sctp_nets *net;
4305 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4306 /* for now, we'll just pick the first reachable one we find */
4307 if (net->dest_state & SCTP_ADDR_UNCONFIRMED)
4309 if (sctp_destination_is_reachable(stcb,
4310 (struct sockaddr *)&net->ro._l_addr)) {
4311 /* found a reachable destination */
4312 stcb->asoc.primary_destination = net;
4315 /* I can't there from here! ...we're gonna die shortly... */
4320 * Delete the address from the endpoint local address list There is nothing
4321 * to be done if we are bound to all addresses
4324 sctp_del_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
4326 struct sctp_laddr *laddr;
4330 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4331 /* You are already bound to all. You have it already */
4334 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4335 if (laddr->ifa == ifa) {
4340 if (fnd && (inp->laddr_count < 2)) {
4341 /* can't delete unless there are at LEAST 2 addresses */
4346 * clean up any use of this address go through our
4347 * associations and clear any last_used_address that match
4348 * this one for each assoc, see if a new primary_destination
4351 struct sctp_tcb *stcb;
4353 /* clean up "next_addr_touse" */
4354 if (inp->next_addr_touse == laddr)
4355 /* delete this address */
4356 inp->next_addr_touse = NULL;
4358 /* clean up "last_used_address" */
4359 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4360 struct sctp_nets *net;
4362 SCTP_TCB_LOCK(stcb);
4363 if (stcb->asoc.last_used_address == laddr)
4364 /* delete this address */
4365 stcb->asoc.last_used_address = NULL;
4367 * Now spin through all the nets and purge any ref
4370 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4371 if (net->ro._s_addr &&
4372 (net->ro._s_addr->ifa == laddr->ifa)) {
4373 /* Yep, purge src address selected */
4376 /* delete this address if cached */
4380 net->ro.ro_rt = NULL;
4382 sctp_free_ifa(net->ro._s_addr);
4383 net->ro._s_addr = NULL;
4384 net->src_addr_selected = 0;
4387 SCTP_TCB_UNLOCK(stcb);
4388 } /* for each tcb */
4389 /* remove it from the ep list */
4390 sctp_remove_laddr(laddr);
4392 /* update inp_vflag flags */
4393 sctp_update_ep_vflag(inp);
4399 * Add the addr to the TCB local address list For the BOUNDALL or dynamic
4400 * case, this is a "pending" address list (eg. addresses waiting for an
4401 * ASCONF-ACK response) For the subset binding, static case, this is a
4402 * "valid" address list
4405 sctp_add_local_addr_assoc(struct sctp_tcb *stcb, struct sctp_ifa *ifa, int restricted_list)
4407 struct sctp_inpcb *inp;
4408 struct sctp_laddr *laddr;
4409 struct sctpladdr *list;
4412 * Assumes TCB is locked.. and possibly the INP. May need to
4413 * confirm/fix that if we need it and is not the case.
4415 list = &stcb->asoc.sctp_restricted_addrs;
4417 inp = stcb->sctp_ep;
4418 if (ifa->address.sa.sa_family == AF_INET6) {
4419 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
4420 /* Can't bind a non-existent addr. */
4424 /* does the address already exist? */
4425 LIST_FOREACH(laddr, list, sctp_nxt_addr) {
4426 if (laddr->ifa == ifa) {
4431 /* add to the list */
4432 (void)sctp_insert_laddr(list, ifa, 0);
4437 * insert an laddr entry with the given ifa for the desired list
4440 sctp_insert_laddr(struct sctpladdr *list, struct sctp_ifa *ifa, uint32_t act)
4442 struct sctp_laddr *laddr;
4444 laddr = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_laddr, struct sctp_laddr);
4445 if (laddr == NULL) {
4446 /* out of memory? */
4449 SCTP_INCR_LADDR_COUNT();
4450 bzero(laddr, sizeof(*laddr));
4452 laddr->action = act;
4453 atomic_add_int(&ifa->refcount, 1);
4455 LIST_INSERT_HEAD(list, laddr, sctp_nxt_addr);
4461 * Remove an laddr entry from the local address list (on an assoc)
4464 sctp_remove_laddr(struct sctp_laddr *laddr)
4467 /* remove from the list */
4468 LIST_REMOVE(laddr, sctp_nxt_addr);
4469 sctp_free_ifa(laddr->ifa);
4470 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_laddr, laddr);
4471 SCTP_DECR_LADDR_COUNT();
4475 * Remove an address from the TCB local address list
4478 sctp_del_local_addr_assoc(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
4480 struct sctp_inpcb *inp;
4481 struct sctp_laddr *laddr;
4484 * This is called by asconf work. It is assumed that a) The TCB is
4485 * locked and b) The INP is locked. This is true in as much as I can
4486 * trace through the entry asconf code where I did these locks.
4487 * Again, the ASCONF code is a bit different in that it does lock
4488 * the INP during its work often times. This must be since we don't
4489 * want other proc's looking up things while what they are looking
4490 * up is changing :-D
4493 inp = stcb->sctp_ep;
4494 /* if subset bound and don't allow ASCONF's, can't delete last */
4495 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) &&
4496 (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF) == 0)) {
4497 if (stcb->asoc.numnets < 2) {
4498 /* can't delete last address */
4502 LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
4503 /* remove the address if it exists */
4504 if (laddr->ifa == NULL)
4506 if (laddr->ifa == ifa) {
4507 sctp_remove_laddr(laddr);
4512 /* address not found! */
4516 static char sctp_pcb_initialized = 0;
4519 * Temporarily remove for __APPLE__ until we use the Tiger equivalents
4522 static int sctp_max_number_of_assoc = SCTP_MAX_NUM_OF_ASOC;
4523 static int sctp_scale_up_for_address = SCTP_SCALE_FOR_ADDR;
4529 * SCTP initialization for the PCB structures should be called by
4530 * the sctp_init() funciton.
4534 if (sctp_pcb_initialized != 0) {
4535 /* error I was called twice */
4538 sctp_pcb_initialized = 1;
4540 bzero(&sctpstat, sizeof(struct sctpstat));
4541 (void)SCTP_GETTIME_TIMEVAL(&sctpstat.sctps_discontinuitytime);
4542 /* init the empty list of (All) Endpoints */
4543 LIST_INIT(&sctppcbinfo.listhead);
4545 /* init the iterator head */
4546 TAILQ_INIT(&sctppcbinfo.iteratorhead);
4548 /* init the hash table of endpoints */
4549 TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", &sctp_hashtblsize);
4550 TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", &sctp_pcbtblsize);
4551 TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", &sctp_chunkscale);
4552 sctppcbinfo.sctp_asochash = SCTP_HASH_INIT((sctp_hashtblsize * 31),
4553 &sctppcbinfo.hashasocmark);
4554 sctppcbinfo.sctp_ephash = SCTP_HASH_INIT(sctp_hashtblsize,
4555 &sctppcbinfo.hashmark);
4556 sctppcbinfo.sctp_tcpephash = SCTP_HASH_INIT(sctp_hashtblsize,
4557 &sctppcbinfo.hashtcpmark);
4558 sctppcbinfo.hashtblsize = sctp_hashtblsize;
4560 /* init the small hash table we use to track restarted asoc's */
4561 sctppcbinfo.sctp_restarthash = SCTP_HASH_INIT(SCTP_STACK_VTAG_HASH_SIZE,
4562 &sctppcbinfo.hashrestartmark);
4565 sctppcbinfo.sctp_vrfhash = SCTP_HASH_INIT(SCTP_SIZE_OF_VRF_HASH,
4566 &sctppcbinfo.hashvrfmark);
4568 /* init the zones */
4570 * FIX ME: Should check for NULL returns, but if it does fail we are
4571 * doomed to panic anyways... add later maybe.
4573 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_ep, "sctp_ep",
4574 sizeof(struct sctp_inpcb), maxsockets);
4576 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_asoc, "sctp_asoc",
4577 sizeof(struct sctp_tcb), sctp_max_number_of_assoc);
4579 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_laddr, "sctp_laddr",
4580 sizeof(struct sctp_laddr),
4581 (sctp_max_number_of_assoc * sctp_scale_up_for_address));
4583 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_net, "sctp_raddr",
4584 sizeof(struct sctp_nets),
4585 (sctp_max_number_of_assoc * sctp_scale_up_for_address));
4587 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_chunk, "sctp_chunk",
4588 sizeof(struct sctp_tmit_chunk),
4589 (sctp_max_number_of_assoc * sctp_chunkscale));
4591 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_readq, "sctp_readq",
4592 sizeof(struct sctp_queued_to_read),
4593 (sctp_max_number_of_assoc * sctp_chunkscale));
4595 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_strmoq, "sctp_stream_msg_out",
4596 sizeof(struct sctp_stream_queue_pending),
4597 (sctp_max_number_of_assoc * sctp_chunkscale));
4599 /* Master Lock INIT for info structure */
4600 SCTP_INP_INFO_LOCK_INIT();
4601 SCTP_STATLOG_INIT_LOCK();
4602 SCTP_ITERATOR_LOCK_INIT();
4604 SCTP_IPI_COUNT_INIT();
4605 SCTP_IPI_ADDR_INIT();
4606 SCTP_IPI_ITERATOR_WQ_INIT();
4608 LIST_INIT(&sctppcbinfo.addr_wq);
4610 /* not sure if we need all the counts */
4611 sctppcbinfo.ipi_count_ep = 0;
4612 /* assoc/tcb zone info */
4613 sctppcbinfo.ipi_count_asoc = 0;
4614 /* local addrlist zone info */
4615 sctppcbinfo.ipi_count_laddr = 0;
4616 /* remote addrlist zone info */
4617 sctppcbinfo.ipi_count_raddr = 0;
4619 sctppcbinfo.ipi_count_chunk = 0;
4621 /* socket queue zone info */
4622 sctppcbinfo.ipi_count_readq = 0;
4624 /* stream out queue cont */
4625 sctppcbinfo.ipi_count_strmoq = 0;
4627 sctppcbinfo.ipi_free_strmoq = 0;
4628 sctppcbinfo.ipi_free_chunks = 0;
4630 SCTP_OS_TIMER_INIT(&sctppcbinfo.addr_wq_timer.timer);
4632 /* Init the TIMEWAIT list */
4633 for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) {
4634 LIST_INIT(&sctppcbinfo.vtag_timewait[i]);
4637 #if defined(SCTP_USE_THREAD_BASED_ITERATOR)
4638 sctppcbinfo.iterator_running = 0;
4639 sctp_startup_iterator();
4643 * INIT the default VRF which for BSD is the only one, other O/S's
4644 * may have more. But initially they must start with one and then
4645 * add the VRF's as addresses are added.
4647 sctp_init_vrf_list(SCTP_DEFAULT_VRF);
4653 sctp_load_addresses_from_init(struct sctp_tcb *stcb, struct mbuf *m,
4654 int iphlen, int offset, int limit, struct sctphdr *sh,
4655 struct sockaddr *altsa)
4658 * grub through the INIT pulling addresses and loading them to the
4659 * nets structure in the asoc. The from address in the mbuf should
4660 * also be loaded (if it is not already). This routine can be called
4661 * with either INIT or INIT-ACK's as long as the m points to the IP
4662 * packet and the offset points to the beginning of the parameters.
4664 struct sctp_inpcb *inp, *l_inp;
4665 struct sctp_nets *net, *net_tmp;
4667 struct sctp_paramhdr *phdr, parm_buf;
4668 struct sctp_tcb *stcb_tmp;
4669 uint16_t ptype, plen;
4670 struct sockaddr *sa;
4671 struct sockaddr_storage dest_store;
4672 struct sockaddr *local_sa = (struct sockaddr *)&dest_store;
4673 struct sockaddr_in sin;
4674 struct sockaddr_in6 sin6;
4675 uint8_t random_store[SCTP_PARAM_BUFFER_SIZE];
4676 struct sctp_auth_random *p_random = NULL;
4677 uint16_t random_len = 0;
4678 uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE];
4679 struct sctp_auth_hmac_algo *hmacs = NULL;
4680 uint16_t hmacs_len = 0;
4681 uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE];
4682 struct sctp_auth_chunk_list *chunks = NULL;
4683 uint16_t num_chunks = 0;
4684 sctp_key_t *new_key;
4686 int got_random = 0, got_hmacs = 0, got_chklist = 0;
4688 /* First get the destination address setup too. */
4689 memset(&sin, 0, sizeof(sin));
4690 memset(&sin6, 0, sizeof(sin6));
4692 sin.sin_family = AF_INET;
4693 sin.sin_len = sizeof(sin);
4694 sin.sin_port = stcb->rport;
4696 sin6.sin6_family = AF_INET6;
4697 sin6.sin6_len = sizeof(struct sockaddr_in6);
4698 sin6.sin6_port = stcb->rport;
4699 if (altsa == NULL) {
4700 iph = mtod(m, struct ip *);
4701 if (iph->ip_v == IPVERSION) {
4703 struct sockaddr_in *sin_2;
4705 sin_2 = (struct sockaddr_in *)(local_sa);
4706 memset(sin_2, 0, sizeof(sin));
4707 sin_2->sin_family = AF_INET;
4708 sin_2->sin_len = sizeof(sin);
4709 sin_2->sin_port = sh->dest_port;
4710 sin_2->sin_addr.s_addr = iph->ip_dst.s_addr;
4711 sin.sin_addr = iph->ip_src;
4712 sa = (struct sockaddr *)&sin;
4713 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
4715 struct ip6_hdr *ip6;
4716 struct sockaddr_in6 *sin6_2;
4718 ip6 = mtod(m, struct ip6_hdr *);
4719 sin6_2 = (struct sockaddr_in6 *)(local_sa);
4720 memset(sin6_2, 0, sizeof(sin6));
4721 sin6_2->sin6_family = AF_INET6;
4722 sin6_2->sin6_len = sizeof(struct sockaddr_in6);
4723 sin6_2->sin6_port = sh->dest_port;
4724 sin6.sin6_addr = ip6->ip6_src;
4725 sa = (struct sockaddr *)&sin6;
4731 * For cookies we use the src address NOT from the packet
4732 * but from the original INIT
4736 /* Turn off ECN until we get through all params */
4737 stcb->asoc.ecn_allowed = 0;
4738 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4739 /* mark all addresses that we have currently on the list */
4740 net->dest_state |= SCTP_ADDR_NOT_IN_ASSOC;
4742 /* does the source address already exist? if so skip it */
4743 l_inp = inp = stcb->sctp_ep;
4745 atomic_add_int(&stcb->asoc.refcnt, 1);
4746 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net_tmp, local_sa, stcb);
4747 atomic_add_int(&stcb->asoc.refcnt, -1);
4749 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || inp == NULL) {
4750 /* we must add the source address */
4751 /* no scope set here since we have a tcb already. */
4752 if ((sa->sa_family == AF_INET) &&
4753 (stcb->asoc.ipv4_addr_legal)) {
4754 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_2)) {
4757 } else if ((sa->sa_family == AF_INET6) &&
4758 (stcb->asoc.ipv6_addr_legal)) {
4759 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) {
4764 if (net_tmp != NULL && stcb_tmp == stcb) {
4765 net_tmp->dest_state &= ~SCTP_ADDR_NOT_IN_ASSOC;
4766 } else if (stcb_tmp != stcb) {
4767 /* It belongs to another association? */
4768 SCTP_TCB_UNLOCK(stcb_tmp);
4772 if (stcb->asoc.state == 0) {
4773 /* the assoc was freed? */
4776 /* now we must go through each of the params. */
4777 phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
4779 ptype = ntohs(phdr->param_type);
4780 plen = ntohs(phdr->param_length);
4782 * printf("ptype => %0x, plen => %d\n", (uint32_t)ptype,
4785 if (offset + plen > limit) {
4791 if (ptype == SCTP_IPV4_ADDRESS) {
4792 if (stcb->asoc.ipv4_addr_legal) {
4793 struct sctp_ipv4addr_param *p4, p4_buf;
4795 /* ok get the v4 address and check/add */
4796 phdr = sctp_get_next_param(m, offset,
4797 (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
4798 if (plen != sizeof(struct sctp_ipv4addr_param) ||
4802 p4 = (struct sctp_ipv4addr_param *)phdr;
4803 sin.sin_addr.s_addr = p4->addr;
4804 if (IN_MULTICAST(sin.sin_addr.s_addr)) {
4805 /* Skip multi-cast addresses */
4808 if ((sin.sin_addr.s_addr == INADDR_BROADCAST) ||
4809 (sin.sin_addr.s_addr == INADDR_ANY)) {
4812 sa = (struct sockaddr *)&sin;
4813 inp = stcb->sctp_ep;
4814 atomic_add_int(&stcb->asoc.refcnt, 1);
4815 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
4817 atomic_add_int(&stcb->asoc.refcnt, -1);
4819 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) ||
4821 /* we must add the source address */
4823 * no scope set since we have a tcb
4828 * we must validate the state again
4831 if (stcb->asoc.state == 0) {
4832 /* the assoc was freed? */
4835 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_4)) {
4838 } else if (stcb_tmp == stcb) {
4839 if (stcb->asoc.state == 0) {
4840 /* the assoc was freed? */
4846 ~SCTP_ADDR_NOT_IN_ASSOC;
4850 * strange, address is in another
4851 * assoc? straighten out locks.
4853 if (stcb->asoc.state == 0) {
4854 /* the assoc was freed? */
4860 } else if (ptype == SCTP_IPV6_ADDRESS) {
4861 if (stcb->asoc.ipv6_addr_legal) {
4862 /* ok get the v6 address and check/add */
4863 struct sctp_ipv6addr_param *p6, p6_buf;
4865 phdr = sctp_get_next_param(m, offset,
4866 (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
4867 if (plen != sizeof(struct sctp_ipv6addr_param) ||
4871 p6 = (struct sctp_ipv6addr_param *)phdr;
4872 memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
4874 if (IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) {
4875 /* Skip multi-cast addresses */
4878 if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
4880 * Link local make no sense without
4885 sa = (struct sockaddr *)&sin6;
4886 inp = stcb->sctp_ep;
4887 atomic_add_int(&stcb->asoc.refcnt, 1);
4888 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
4890 atomic_add_int(&stcb->asoc.refcnt, -1);
4891 if (stcb_tmp == NULL && (inp == stcb->sctp_ep ||
4894 * we must validate the state again
4897 if (stcb->asoc.state == 0) {
4898 /* the assoc was freed? */
4902 * we must add the address, no scope
4905 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_5)) {
4908 } else if (stcb_tmp == stcb) {
4910 * we must validate the state again
4913 if (stcb->asoc.state == 0) {
4914 /* the assoc was freed? */
4920 ~SCTP_ADDR_NOT_IN_ASSOC;
4924 * strange, address is in another
4925 * assoc? straighten out locks.
4927 if (stcb->asoc.state == 0) {
4928 /* the assoc was freed? */
4934 } else if (ptype == SCTP_ECN_CAPABLE) {
4935 stcb->asoc.ecn_allowed = 1;
4936 } else if (ptype == SCTP_ULP_ADAPTATION) {
4937 if (stcb->asoc.state != SCTP_STATE_OPEN) {
4938 struct sctp_adaptation_layer_indication ai,
4941 phdr = sctp_get_next_param(m, offset,
4942 (struct sctp_paramhdr *)&ai, sizeof(ai));
4943 aip = (struct sctp_adaptation_layer_indication *)phdr;
4945 sctp_ulp_notify(SCTP_NOTIFY_ADAPTATION_INDICATION,
4946 stcb, ntohl(aip->indication), NULL);
4949 } else if (ptype == SCTP_SET_PRIM_ADDR) {
4950 struct sctp_asconf_addr_param lstore, *fee;
4951 struct sctp_asconf_addrv4_param *fii;
4953 struct sockaddr *lsa = NULL;
4955 stcb->asoc.peer_supports_asconf = 1;
4956 if (plen > sizeof(lstore)) {
4959 phdr = sctp_get_next_param(m, offset,
4960 (struct sctp_paramhdr *)&lstore, min(plen, sizeof(lstore)));
4964 fee = (struct sctp_asconf_addr_param *)phdr;
4965 lptype = ntohs(fee->addrp.ph.param_type);
4966 if (lptype == SCTP_IPV4_ADDRESS) {
4968 sizeof(struct sctp_asconf_addrv4_param)) {
4969 SCTP_PRINTF("Sizeof setprim in init/init ack not %d but %d - ignored\n",
4970 (int)sizeof(struct sctp_asconf_addrv4_param),
4973 fii = (struct sctp_asconf_addrv4_param *)fee;
4974 sin.sin_addr.s_addr = fii->addrp.addr;
4975 lsa = (struct sockaddr *)&sin;
4977 } else if (lptype == SCTP_IPV6_ADDRESS) {
4979 sizeof(struct sctp_asconf_addr_param)) {
4980 SCTP_PRINTF("Sizeof setprim (v6) in init/init ack not %d but %d - ignored\n",
4981 (int)sizeof(struct sctp_asconf_addr_param),
4984 memcpy(sin6.sin6_addr.s6_addr,
4986 sizeof(fee->addrp.addr));
4987 lsa = (struct sockaddr *)&sin6;
4991 (void)sctp_set_primary_addr(stcb, sa, NULL);
4993 } else if (ptype == SCTP_PRSCTP_SUPPORTED) {
4994 /* Peer supports pr-sctp */
4995 stcb->asoc.peer_supports_prsctp = 1;
4996 } else if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
4997 /* A supported extension chunk */
4998 struct sctp_supported_chunk_types_param *pr_supported;
4999 uint8_t local_store[SCTP_PARAM_BUFFER_SIZE];
5002 phdr = sctp_get_next_param(m, offset,
5003 (struct sctp_paramhdr *)&local_store, min(sizeof(local_store), plen));
5007 stcb->asoc.peer_supports_asconf = 0;
5008 stcb->asoc.peer_supports_prsctp = 0;
5009 stcb->asoc.peer_supports_pktdrop = 0;
5010 stcb->asoc.peer_supports_strreset = 0;
5011 stcb->asoc.peer_supports_auth = 0;
5012 pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
5013 num_ent = plen - sizeof(struct sctp_paramhdr);
5014 for (i = 0; i < num_ent; i++) {
5015 switch (pr_supported->chunk_types[i]) {
5017 case SCTP_ASCONF_ACK:
5018 stcb->asoc.peer_supports_asconf = 1;
5020 case SCTP_FORWARD_CUM_TSN:
5021 stcb->asoc.peer_supports_prsctp = 1;
5023 case SCTP_PACKET_DROPPED:
5024 stcb->asoc.peer_supports_pktdrop = 1;
5026 case SCTP_STREAM_RESET:
5027 stcb->asoc.peer_supports_strreset = 1;
5029 case SCTP_AUTHENTICATION:
5030 stcb->asoc.peer_supports_auth = 1;
5033 /* one I have not learned yet */
5038 } else if (ptype == SCTP_ECN_NONCE_SUPPORTED) {
5039 /* Peer supports ECN-nonce */
5040 stcb->asoc.peer_supports_ecn_nonce = 1;
5041 stcb->asoc.ecn_nonce_allowed = 1;
5042 } else if (ptype == SCTP_RANDOM) {
5043 if (plen > sizeof(random_store))
5046 /* already processed a RANDOM */
5049 phdr = sctp_get_next_param(m, offset,
5050 (struct sctp_paramhdr *)random_store,
5051 min(sizeof(random_store), plen));
5054 p_random = (struct sctp_auth_random *)phdr;
5055 random_len = plen - sizeof(*p_random);
5056 /* enforce the random length */
5057 if (random_len != SCTP_AUTH_RANDOM_SIZE_REQUIRED) {
5058 SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: invalid RANDOM len\n");
5062 } else if (ptype == SCTP_HMAC_LIST) {
5066 if (plen > sizeof(hmacs_store))
5069 /* already processed a HMAC list */
5072 phdr = sctp_get_next_param(m, offset,
5073 (struct sctp_paramhdr *)hmacs_store,
5074 min(plen, sizeof(hmacs_store)));
5077 hmacs = (struct sctp_auth_hmac_algo *)phdr;
5078 hmacs_len = plen - sizeof(*hmacs);
5079 num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]);
5080 /* validate the hmac list */
5081 if (sctp_verify_hmac_param(hmacs, num_hmacs)) {
5084 if (stcb->asoc.peer_hmacs != NULL)
5085 sctp_free_hmaclist(stcb->asoc.peer_hmacs);
5086 stcb->asoc.peer_hmacs = sctp_alloc_hmaclist(num_hmacs);
5087 if (stcb->asoc.peer_hmacs != NULL) {
5088 for (i = 0; i < num_hmacs; i++) {
5089 (void)sctp_auth_add_hmacid(stcb->asoc.peer_hmacs,
5090 ntohs(hmacs->hmac_ids[i]));
5094 } else if (ptype == SCTP_CHUNK_LIST) {
5097 if (plen > sizeof(chunks_store))
5100 /* already processed a Chunks list */
5103 phdr = sctp_get_next_param(m, offset,
5104 (struct sctp_paramhdr *)chunks_store,
5105 min(plen, sizeof(chunks_store)));
5108 chunks = (struct sctp_auth_chunk_list *)phdr;
5109 num_chunks = plen - sizeof(*chunks);
5110 if (stcb->asoc.peer_auth_chunks != NULL)
5111 sctp_clear_chunklist(stcb->asoc.peer_auth_chunks);
5113 stcb->asoc.peer_auth_chunks = sctp_alloc_chunklist();
5114 for (i = 0; i < num_chunks; i++) {
5115 (void)sctp_auth_add_chunk(chunks->chunk_types[i],
5116 stcb->asoc.peer_auth_chunks);
5119 } else if ((ptype == SCTP_HEARTBEAT_INFO) ||
5120 (ptype == SCTP_STATE_COOKIE) ||
5121 (ptype == SCTP_UNRECOG_PARAM) ||
5122 (ptype == SCTP_COOKIE_PRESERVE) ||
5123 (ptype == SCTP_SUPPORTED_ADDRTYPE) ||
5124 (ptype == SCTP_ADD_IP_ADDRESS) ||
5125 (ptype == SCTP_DEL_IP_ADDRESS) ||
5126 (ptype == SCTP_ERROR_CAUSE_IND) ||
5127 (ptype == SCTP_SUCCESS_REPORT)) {
5130 if ((ptype & 0x8000) == 0x0000) {
5132 * must stop processing the rest of the
5133 * param's. Any report bits were handled
5135 * sctp_arethere_unrecognized_parameters()
5136 * when the INIT or INIT-ACK was first seen.
5142 offset += SCTP_SIZE32(plen);
5143 if (offset >= limit) {
5146 phdr = sctp_get_next_param(m, offset, &parm_buf,
5149 /* Now check to see if we need to purge any addresses */
5150 for (net = TAILQ_FIRST(&stcb->asoc.nets); net != NULL; net = net_tmp) {
5151 net_tmp = TAILQ_NEXT(net, sctp_next);
5152 if ((net->dest_state & SCTP_ADDR_NOT_IN_ASSOC) ==
5153 SCTP_ADDR_NOT_IN_ASSOC) {
5154 /* This address has been removed from the asoc */
5155 /* remove and free it */
5156 stcb->asoc.numnets--;
5157 TAILQ_REMOVE(&stcb->asoc.nets, net, sctp_next);
5158 sctp_free_remote_addr(net);
5159 if (net == stcb->asoc.primary_destination) {
5160 stcb->asoc.primary_destination = NULL;
5161 sctp_select_primary_destination(stcb);
5165 /* validate authentication required parameters */
5166 if (got_random && got_hmacs) {
5167 stcb->asoc.peer_supports_auth = 1;
5169 stcb->asoc.peer_supports_auth = 0;
5171 if (!stcb->asoc.peer_supports_auth && got_chklist) {
5172 /* peer does not support auth but sent a chunks list? */
5175 if (!sctp_asconf_auth_nochk && stcb->asoc.peer_supports_asconf &&
5176 !stcb->asoc.peer_supports_auth) {
5177 /* peer supports asconf but not auth? */
5180 /* concatenate the full random key */
5181 #ifdef SCTP_AUTH_DRAFT_04
5182 keylen = random_len;
5183 new_key = sctp_alloc_key(keylen);
5184 if (new_key != NULL) {
5185 /* copy in the RANDOM */
5186 if (p_random != NULL)
5187 bcopy(p_random->random_data, new_key->key, random_len);
5190 keylen = sizeof(*p_random) + random_len + sizeof(*chunks) + num_chunks +
5191 sizeof(*hmacs) + hmacs_len;
5192 new_key = sctp_alloc_key(keylen);
5193 if (new_key != NULL) {
5194 /* copy in the RANDOM */
5195 if (p_random != NULL) {
5196 keylen = sizeof(*p_random) + random_len;
5197 bcopy(p_random, new_key->key, keylen);
5199 /* append in the AUTH chunks */
5200 if (chunks != NULL) {
5201 bcopy(chunks, new_key->key + keylen,
5202 sizeof(*chunks) + num_chunks);
5203 keylen += sizeof(*chunks) + num_chunks;
5205 /* append in the HMACs */
5206 if (hmacs != NULL) {
5207 bcopy(hmacs, new_key->key + keylen,
5208 sizeof(*hmacs) + hmacs_len);
5213 /* failed to get memory for the key */
5216 if (stcb->asoc.authinfo.peer_random != NULL)
5217 sctp_free_key(stcb->asoc.authinfo.peer_random);
5218 stcb->asoc.authinfo.peer_random = new_key;
5219 #ifdef SCTP_AUTH_DRAFT_04
5220 /* don't include the chunks and hmacs for draft -04 */
5221 stcb->asoc.authinfo.peer_random->keylen = random_len;
5223 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid);
5224 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid);
5230 sctp_set_primary_addr(struct sctp_tcb *stcb, struct sockaddr *sa,
5231 struct sctp_nets *net)
5233 /* make sure the requested primary address exists in the assoc */
5234 if (net == NULL && sa)
5235 net = sctp_findnet(stcb, sa);
5238 /* didn't find the requested primary address! */
5241 /* set the primary address */
5242 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
5243 /* Must be confirmed, so queue to set */
5244 net->dest_state |= SCTP_ADDR_REQ_PRIMARY;
5247 stcb->asoc.primary_destination = net;
5248 net->dest_state &= ~SCTP_ADDR_WAS_PRIMARY;
5249 net = TAILQ_FIRST(&stcb->asoc.nets);
5250 if (net != stcb->asoc.primary_destination) {
5252 * first one on the list is NOT the primary
5253 * sctp_cmpaddr() is much more efficent if the
5254 * primary is the first on the list, make it so.
5256 TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
5257 TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
5265 sctp_is_vtag_good(struct sctp_inpcb *inp, uint32_t tag, struct timeval *now)
5268 * This function serves two purposes. It will see if a TAG can be
5269 * re-used and return 1 for yes it is ok and 0 for don't use that
5270 * tag. A secondary function it will do is purge out old tags that
5273 struct sctpasochead *head;
5274 struct sctpvtaghead *chain;
5275 struct sctp_tagblock *twait_block;
5276 struct sctp_tcb *stcb;
5279 SCTP_INP_INFO_WLOCK();
5280 chain = &sctppcbinfo.vtag_timewait[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
5281 /* First is the vtag in use ? */
5283 head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(tag,
5284 sctppcbinfo.hashasocmark)];
5288 LIST_FOREACH(stcb, head, sctp_asocs) {
5290 if (stcb->asoc.my_vtag == tag) {
5292 * We should remove this if and return 0 always if
5293 * we want vtags unique across all endpoints. For
5294 * now within a endpoint is ok.
5296 if (inp == stcb->sctp_ep) {
5297 /* bad tag, in use */
5298 SCTP_INP_INFO_WUNLOCK();
5304 /* Now lets check the restart hash */
5305 head = &sctppcbinfo.sctp_restarthash[SCTP_PCBHASH_ASOC(tag,
5306 sctppcbinfo.hashrestartmark)];
5308 goto check_time_wait;
5310 LIST_FOREACH(stcb, head, sctp_tcbrestarhash) {
5311 if (stcb->asoc.assoc_id == tag) {
5313 if (inp == stcb->sctp_ep) {
5314 /* bad tag, in use */
5315 SCTP_INP_INFO_WUNLOCK();
5321 /* Now what about timed wait ? */
5322 if (!SCTP_LIST_EMPTY(chain)) {
5324 * Block(s) are present, lets see if we have this tag in the
5327 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
5328 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
5329 if (twait_block->vtag_block[i].v_tag == 0) {
5332 } else if ((long)twait_block->vtag_block[i].tv_sec_at_expire >
5334 /* Audit expires this guy */
5335 twait_block->vtag_block[i].tv_sec_at_expire = 0;
5336 twait_block->vtag_block[i].v_tag = 0;
5337 } else if (twait_block->vtag_block[i].v_tag ==
5339 /* Bad tag, sorry :< */
5340 SCTP_INP_INFO_WUNLOCK();
5346 /* Not found, ok to use the tag */
5347 SCTP_INP_INFO_WUNLOCK();
5352 static sctp_assoc_t reneged_asoc_ids[256];
5353 static uint8_t reneged_at = 0;
5357 sctp_drain_mbufs(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
5360 * We must hunt this association for MBUF's past the cumack (i.e.
5361 * out of order data that we can renege on).
5363 struct sctp_association *asoc;
5364 struct sctp_tmit_chunk *chk, *nchk;
5365 uint32_t cumulative_tsn_p1, tsn;
5366 struct sctp_queued_to_read *ctl, *nctl;
5367 int cnt, strmat, gap;
5369 /* We look for anything larger than the cum-ack + 1 */
5371 SCTP_STAT_INCR(sctps_protocol_drain_calls);
5372 if (sctp_do_drain == 0) {
5376 if (asoc->cumulative_tsn == asoc->highest_tsn_inside_map) {
5377 /* none we can reneg on. */
5380 SCTP_STAT_INCR(sctps_protocol_drains_done);
5381 cumulative_tsn_p1 = asoc->cumulative_tsn + 1;
5383 /* First look in the re-assembly queue */
5384 chk = TAILQ_FIRST(&asoc->reasmqueue);
5386 /* Get the next one */
5387 nchk = TAILQ_NEXT(chk, sctp_next);
5388 if (compare_with_wrap(chk->rec.data.TSN_seq,
5389 cumulative_tsn_p1, MAX_TSN)) {
5390 /* Yep it is above cum-ack */
5392 tsn = chk->rec.data.TSN_seq;
5393 if (tsn >= asoc->mapping_array_base_tsn) {
5394 gap = tsn - asoc->mapping_array_base_tsn;
5396 gap = (MAX_TSN - asoc->mapping_array_base_tsn) +
5399 asoc->size_on_reasm_queue = sctp_sbspace_sub(asoc->size_on_reasm_queue, chk->send_size);
5400 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
5401 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
5402 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
5404 sctp_m_freem(chk->data);
5407 sctp_free_remote_addr(chk->whoTo);
5408 sctp_free_a_chunk(stcb, chk);
5412 /* Ok that was fun, now we will drain all the inbound streams? */
5413 for (strmat = 0; strmat < asoc->streamincnt; strmat++) {
5414 ctl = TAILQ_FIRST(&asoc->strmin[strmat].inqueue);
5416 nctl = TAILQ_NEXT(ctl, next);
5417 if (compare_with_wrap(ctl->sinfo_tsn,
5418 cumulative_tsn_p1, MAX_TSN)) {
5419 /* Yep it is above cum-ack */
5421 tsn = ctl->sinfo_tsn;
5422 if (tsn >= asoc->mapping_array_base_tsn) {
5424 asoc->mapping_array_base_tsn;
5427 asoc->mapping_array_base_tsn) +
5430 asoc->size_on_all_streams = sctp_sbspace_sub(asoc->size_on_all_streams, ctl->length);
5431 sctp_ucount_decr(asoc->cnt_on_all_streams);
5433 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array,
5435 TAILQ_REMOVE(&asoc->strmin[strmat].inqueue,
5438 sctp_m_freem(ctl->data);
5441 sctp_free_remote_addr(ctl->whoFrom);
5442 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_readq, ctl);
5443 SCTP_DECR_READQ_COUNT();
5449 * Question, should we go through the delivery queue? The only
5450 * reason things are on here is the app not reading OR a p-d-api up.
5451 * An attacker COULD send enough in to initiate the PD-API and then
5452 * send a bunch of stuff to other streams... these would wind up on
5453 * the delivery queue.. and then we would not get to them. But in
5454 * order to do this I then have to back-track and un-deliver
5455 * sequence numbers in streams.. el-yucko. I think for now we will
5456 * NOT look at the delivery queue and leave it to be something to
5457 * consider later. An alternative would be to abort the P-D-API with
5458 * a notification and then deliver the data.... Or another method
5459 * might be to keep track of how many times the situation occurs and
5460 * if we see a possible attack underway just abort the association.
5464 SCTPDBG(SCTP_DEBUG_PCB1, "Freed %d chunks from reneg harvest\n", cnt);
5469 * Now do we need to find a new
5470 * asoc->highest_tsn_inside_map?
5472 if (asoc->highest_tsn_inside_map >= asoc->mapping_array_base_tsn) {
5473 gap = asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn;
5475 gap = (MAX_TSN - asoc->mapping_array_base_tsn) +
5476 asoc->highest_tsn_inside_map + 1;
5478 if (gap >= (asoc->mapping_array_size << 3)) {
5480 * Something bad happened or cum-ack and high were
5481 * behind the base, but if so earlier checks should
5482 * have found NO data... wierd... we will start at
5483 * end of mapping array.
5485 SCTP_PRINTF("Gap was larger than array?? %d set to max:%d maparraymax:%x\n",
5487 (int)(asoc->mapping_array_size << 3),
5488 (int)asoc->highest_tsn_inside_map);
5489 gap = asoc->mapping_array_size << 3;
5492 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
5493 /* found the new highest */
5494 asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn + gap;
5500 /* Nothing left in map */
5501 memset(asoc->mapping_array, 0, asoc->mapping_array_size);
5502 asoc->mapping_array_base_tsn = asoc->cumulative_tsn + 1;
5503 asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
5505 asoc->last_revoke_count = cnt;
5506 (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
5507 sctp_send_sack(stcb);
5508 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_DRAIN);
5509 reneged_asoc_ids[reneged_at] = sctp_get_associd(stcb);
5513 * Another issue, in un-setting the TSN's in the mapping array we
5514 * DID NOT adjust the higest_tsn marker. This will cause one of two
5515 * things to occur. It may cause us to do extra work in checking for
5516 * our mapping array movement. More importantly it may cause us to
5517 * SACK every datagram. This may not be a bad thing though since we
5518 * will recover once we get our cum-ack above and all this stuff we
5527 * We must walk the PCB lists for ALL associations here. The system
5528 * is LOW on MBUF's and needs help. This is where reneging will
5529 * occur. We really hope this does NOT happen!
5531 struct sctp_inpcb *inp;
5532 struct sctp_tcb *stcb;
5534 SCTP_INP_INFO_RLOCK();
5535 LIST_FOREACH(inp, &sctppcbinfo.listhead, sctp_list) {
5536 /* For each endpoint */
5537 SCTP_INP_RLOCK(inp);
5538 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5539 /* For each association */
5540 SCTP_TCB_LOCK(stcb);
5541 sctp_drain_mbufs(inp, stcb);
5542 SCTP_TCB_UNLOCK(stcb);
5544 SCTP_INP_RUNLOCK(inp);
5546 SCTP_INP_INFO_RUNLOCK();
5550 * start a new iterator
5551 * iterates through all endpoints and associations based on the pcb_state
5552 * flags and asoc_state. "af" (mandatory) is executed for all matching
5553 * assocs and "ef" (optional) is executed when the iterator completes.
5554 * "inpf" (optional) is executed for each new endpoint as it is being
5555 * iterated through. inpe (optional) is called when the inp completes
5556 * its way through all the stcbs.
5559 sctp_initiate_iterator(inp_func inpf,
5563 uint32_t pcb_features,
5564 uint32_t asoc_state,
5568 struct sctp_inpcb *s_inp,
5569 uint8_t chunk_output_off)
5571 struct sctp_iterator *it = NULL;
5576 SCTP_MALLOC(it, struct sctp_iterator *, sizeof(struct sctp_iterator),
5581 memset(it, 0, sizeof(*it));
5582 it->function_assoc = af;
5583 it->function_inp = inpf;
5585 it->done_current_ep = 0;
5587 it->done_current_ep = 1;
5588 it->function_atend = ef;
5591 it->pcb_flags = pcb_state;
5592 it->pcb_features = pcb_features;
5593 it->asoc_state = asoc_state;
5594 it->function_inp_end = inpe;
5595 it->no_chunk_output = chunk_output_off;
5598 it->iterator_flags = SCTP_ITERATOR_DO_SINGLE_INP;
5600 SCTP_INP_INFO_RLOCK();
5601 it->inp = LIST_FIRST(&sctppcbinfo.listhead);
5603 SCTP_INP_INFO_RUNLOCK();
5604 it->iterator_flags = SCTP_ITERATOR_DO_ALL_INP;
5607 SCTP_IPI_ITERATOR_WQ_LOCK();
5609 SCTP_INP_INCR_REF(it->inp);
5611 TAILQ_INSERT_TAIL(&sctppcbinfo.iteratorhead, it, sctp_nxt_itr);
5612 #if defined(SCTP_USE_THREAD_BASED_ITERATOR)
5613 if (sctppcbinfo.iterator_running == 0) {
5614 sctp_wakeup_iterator();
5616 SCTP_IPI_ITERATOR_WQ_UNLOCK();
5619 SCTP_INP_DECR_REF(it->inp);
5620 SCTP_IPI_ITERATOR_WQ_UNLOCK();
5621 /* Init the timer */
5622 SCTP_OS_TIMER_INIT(&it->tmr.timer);
5623 /* add to the list of all iterators */
5624 sctp_timer_start(SCTP_TIMER_TYPE_ITERATOR, (struct sctp_inpcb *)it,
5627 /* sa_ignore MEMLEAK {memory is put on the tailq for the iterator} */