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;
159 /* now also setup table ids */
160 SCTP_INIT_VRF_TABLEID(vrf);
161 /* Init the HASH of addresses */
162 vrf->vrf_addr_hash = SCTP_HASH_INIT(SCTP_VRF_ADDR_HASH_SIZE,
163 &vrf->vrf_addr_hashmark);
164 if (vrf->vrf_addr_hash == NULL) {
167 panic("No memory for VRF:%d", vrf_id);
169 SCTP_FREE(vrf, SCTP_M_VRF);
172 /* Add it to the hash table */
173 bucket = &sctppcbinfo.sctp_vrfhash[(vrf_id & sctppcbinfo.hashvrfmark)];
174 LIST_INSERT_HEAD(bucket, vrf, next_vrf);
175 atomic_add_int(&sctppcbinfo.ipi_count_vrfs, 1);
181 sctp_find_ifn(void *ifn, uint32_t ifn_index)
183 struct sctp_ifn *sctp_ifnp;
184 struct sctp_ifnlist *hash_ifn_head;
187 * We assume the lock is held for the addresses if thats wrong
188 * problems could occur :-)
190 hash_ifn_head = &sctppcbinfo.vrf_ifn_hash[(ifn_index & sctppcbinfo.vrf_ifn_hashmark)];
191 LIST_FOREACH(sctp_ifnp, hash_ifn_head, next_bucket) {
192 if (sctp_ifnp->ifn_index == ifn_index) {
195 if (sctp_ifnp->ifn_p && ifn && (sctp_ifnp->ifn_p == ifn)) {
205 sctp_find_vrf(uint32_t vrf_id)
207 struct sctp_vrflist *bucket;
208 struct sctp_vrf *liste;
210 bucket = &sctppcbinfo.sctp_vrfhash[(vrf_id & sctppcbinfo.hashvrfmark)];
211 LIST_FOREACH(liste, bucket, next_vrf) {
212 if (vrf_id == liste->vrf_id) {
220 sctp_free_vrf(struct sctp_vrf *vrf)
224 ret = atomic_fetchadd_int(&vrf->refcount, -1);
226 /* We zero'd the count */
227 LIST_REMOVE(vrf, next_vrf);
228 SCTP_FREE(vrf, SCTP_M_VRF);
229 atomic_subtract_int(&sctppcbinfo.ipi_count_vrfs, 1);
234 sctp_free_ifn(struct sctp_ifn *sctp_ifnp)
238 ret = atomic_fetchadd_int(&sctp_ifnp->refcount, -1);
240 /* We zero'd the count */
241 if (sctp_ifnp->vrf) {
242 sctp_free_vrf(sctp_ifnp->vrf);
244 SCTP_FREE(sctp_ifnp, SCTP_M_IFN);
245 atomic_subtract_int(&sctppcbinfo.ipi_count_ifns, 1);
250 sctp_update_ifn_mtu(uint32_t ifn_index, uint32_t mtu)
252 struct sctp_ifn *sctp_ifnp;
254 sctp_ifnp = sctp_find_ifn((void *)NULL, ifn_index);
255 if (sctp_ifnp != NULL) {
256 sctp_ifnp->ifn_mtu = mtu;
262 sctp_free_ifa(struct sctp_ifa *sctp_ifap)
266 ret = atomic_fetchadd_int(&sctp_ifap->refcount, -1);
268 /* We zero'd the count */
269 if (sctp_ifap->ifn_p) {
270 sctp_free_ifn(sctp_ifap->ifn_p);
272 SCTP_FREE(sctp_ifap, SCTP_M_IFA);
273 atomic_subtract_int(&sctppcbinfo.ipi_count_ifas, 1);
278 sctp_delete_ifn(struct sctp_ifn *sctp_ifnp, int hold_addr_lock)
280 struct sctp_ifn *found;
282 found = sctp_find_ifn(sctp_ifnp->ifn_p, sctp_ifnp->ifn_index);
284 /* Not in the list.. sorry */
287 if (hold_addr_lock == 0)
288 SCTP_IPI_ADDR_LOCK();
289 LIST_REMOVE(sctp_ifnp, next_bucket);
290 LIST_REMOVE(sctp_ifnp, next_ifn);
291 SCTP_DEREGISTER_INTERFACE(sctp_ifnp->ifn_index,
292 sctp_ifnp->registered_af);
293 if (hold_addr_lock == 0)
294 SCTP_IPI_ADDR_UNLOCK();
295 /* Take away the reference, and possibly free it */
296 sctp_free_ifn(sctp_ifnp);
301 sctp_add_addr_to_vrf(uint32_t vrf_id, void *ifn, uint32_t ifn_index,
302 uint32_t ifn_type, const char *if_name,
303 void *ifa, struct sockaddr *addr, uint32_t ifa_flags,
306 struct sctp_vrf *vrf;
307 struct sctp_ifn *sctp_ifnp = NULL;
308 struct sctp_ifa *sctp_ifap = NULL;
309 struct sctp_ifalist *hash_addr_head;
310 struct sctp_ifnlist *hash_ifn_head;
311 uint32_t hash_of_addr;
314 /* How granular do we need the locks to be here? */
315 SCTP_IPI_ADDR_LOCK();
316 sctp_ifnp = sctp_find_ifn(ifn, ifn_index);
318 vrf = sctp_ifnp->vrf;
320 vrf = sctp_find_vrf(vrf_id);
322 vrf = sctp_allocate_vrf(vrf_id);
324 SCTP_IPI_ADDR_UNLOCK();
329 if (sctp_ifnp == NULL) {
331 * build one and add it, can't hold lock until after malloc
334 SCTP_IPI_ADDR_UNLOCK();
335 SCTP_MALLOC(sctp_ifnp, struct sctp_ifn *, sizeof(struct sctp_ifn), SCTP_M_IFN);
336 if (sctp_ifnp == NULL) {
338 panic("No memory for IFN:%u", sctp_ifnp->ifn_index);
342 memset(sctp_ifnp, 0, sizeof(struct sctp_ifn));
343 sctp_ifnp->ifn_index = ifn_index;
344 sctp_ifnp->ifn_p = ifn;
345 sctp_ifnp->ifn_type = ifn_type;
346 sctp_ifnp->refcount = 1;
347 sctp_ifnp->vrf = vrf;
349 atomic_add_int(&vrf->refcount, 1);
350 sctp_ifnp->ifn_mtu = SCTP_GATHER_MTU_FROM_IFN_INFO(ifn, ifn_index, addr->sa_family);
351 if (if_name != NULL) {
352 memcpy(sctp_ifnp->ifn_name, if_name, SCTP_IFNAMSIZ);
354 memcpy(sctp_ifnp->ifn_name, "unknown", min(7, SCTP_IFNAMSIZ));
356 hash_ifn_head = &sctppcbinfo.vrf_ifn_hash[(ifn_index & sctppcbinfo.vrf_ifn_hashmark)];
357 LIST_INIT(&sctp_ifnp->ifalist);
358 SCTP_IPI_ADDR_LOCK();
359 LIST_INSERT_HEAD(hash_ifn_head, sctp_ifnp, next_bucket);
360 LIST_INSERT_HEAD(&vrf->ifnlist, sctp_ifnp, next_ifn);
361 atomic_add_int(&sctppcbinfo.ipi_count_ifns, 1);
364 sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, 1);
366 /* Hmm, it already exists? */
367 if ((sctp_ifap->ifn_p) &&
368 (sctp_ifap->ifn_p->ifn_index == ifn_index)) {
370 /* Remove the created one that we don't want */
371 sctp_delete_ifn(sctp_ifap->ifn_p, 1);
373 if (sctp_ifap->localifa_flags & SCTP_BEING_DELETED) {
374 /* easy to solve, just switch back to active */
375 sctp_ifap->localifa_flags = SCTP_ADDR_VALID;
376 sctp_ifap->ifn_p = sctp_ifnp;
377 atomic_add_int(&sctp_ifap->ifn_p->refcount, 1);
379 SCTP_IPI_ADDR_UNLOCK();
382 goto exit_stage_left;
385 if (sctp_ifap->ifn_p) {
387 * The first IFN gets the address,
388 * duplicates are ignored.
392 * Remove the created one that we
395 sctp_delete_ifn(sctp_ifap->ifn_p, 1);
397 goto exit_stage_left;
399 /* repair ifnp which was NULL ? */
400 sctp_ifap->localifa_flags = SCTP_ADDR_VALID;
401 sctp_ifap->ifn_p = sctp_ifnp;
402 atomic_add_int(&sctp_ifap->ifn_p->refcount, 1);
404 goto exit_stage_left;
407 SCTP_IPI_ADDR_UNLOCK();
408 SCTP_MALLOC(sctp_ifap, struct sctp_ifa *, sizeof(struct sctp_ifa), SCTP_M_IFA);
409 if (sctp_ifap == NULL) {
411 panic("No memory for IFA");
415 memset(sctp_ifap, 0, sizeof(struct sctp_ifa));
416 sctp_ifap->ifn_p = sctp_ifnp;
417 atomic_add_int(&sctp_ifnp->refcount, 1);
418 sctp_ifap->vrf_id = vrf_id;
419 sctp_ifap->ifa = ifa;
420 memcpy(&sctp_ifap->address, addr, addr->sa_len);
421 sctp_ifap->localifa_flags = SCTP_ADDR_VALID | SCTP_ADDR_DEFER_USE;
422 sctp_ifap->flags = ifa_flags;
424 if (sctp_ifap->address.sa.sa_family == AF_INET) {
425 struct sockaddr_in *sin;
427 sin = (struct sockaddr_in *)&sctp_ifap->address.sin;
428 if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) ||
429 (IN4_ISLOOPBACK_ADDRESS(&sin->sin_addr))) {
430 sctp_ifap->src_is_loop = 1;
432 if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
433 sctp_ifap->src_is_priv = 1;
437 new_ifn_af = AF_INET;
438 } else if (sctp_ifap->address.sa.sa_family == AF_INET6) {
439 /* ok to use deprecated addresses? */
440 struct sockaddr_in6 *sin6;
442 sin6 = (struct sockaddr_in6 *)&sctp_ifap->address.sin6;
443 if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) ||
444 (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))) {
445 sctp_ifap->src_is_loop = 1;
447 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
448 sctp_ifap->src_is_priv = 1;
452 new_ifn_af = AF_INET6;
456 hash_of_addr = sctp_get_ifa_hash_val(&sctp_ifap->address.sa);
458 if ((sctp_ifap->src_is_priv == 0) &&
459 (sctp_ifap->src_is_loop == 0)) {
460 sctp_ifap->src_is_glob = 1;
462 SCTP_IPI_ADDR_LOCK();
463 hash_addr_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)];
464 LIST_INSERT_HEAD(hash_addr_head, sctp_ifap, next_bucket);
465 sctp_ifap->refcount = 1;
466 LIST_INSERT_HEAD(&sctp_ifnp->ifalist, sctp_ifap, next_ifa);
467 sctp_ifnp->ifa_count++;
468 vrf->total_ifa_count++;
469 atomic_add_int(&sctppcbinfo.ipi_count_ifas, 1);
471 SCTP_REGISTER_INTERFACE(ifn_index, new_ifn_af);
472 sctp_ifnp->registered_af = new_ifn_af;
474 SCTP_IPI_ADDR_UNLOCK();
477 * Bump up the refcount so that when the timer completes it
478 * will drop back down.
480 struct sctp_laddr *wi;
482 atomic_add_int(&sctp_ifap->refcount, 1);
483 wi = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_laddr, struct sctp_laddr);
486 * Gak, what can we do? We have lost an address
487 * change can you say HOSED?
489 SCTPDBG(SCTP_DEBUG_PCB1, "Lost and address change ???\n");
490 /* Opps, must decrement the count */
491 sctp_del_addr_from_vrf(vrf_id, addr, ifn_index);
494 SCTP_INCR_LADDR_COUNT();
495 bzero(wi, sizeof(*wi));
496 (void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
498 wi->action = SCTP_ADD_IP_ADDRESS;
499 SCTP_IPI_ITERATOR_WQ_LOCK();
501 * Should this really be a tailq? As it is we will process
502 * the newest first :-0
504 LIST_INSERT_HEAD(&sctppcbinfo.addr_wq, wi, sctp_nxt_addr);
505 SCTP_IPI_ITERATOR_WQ_UNLOCK();
506 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
507 (struct sctp_inpcb *)NULL,
508 (struct sctp_tcb *)NULL,
509 (struct sctp_nets *)NULL);
511 /* it's ready for use */
512 sctp_ifap->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
518 sctp_del_addr_from_vrf(uint32_t vrf_id, struct sockaddr *addr,
521 struct sctp_vrf *vrf;
522 struct sctp_ifa *sctp_ifap = NULL;
524 SCTP_IPI_ADDR_LOCK();
526 vrf = sctp_find_vrf(vrf_id);
528 SCTP_PRINTF("Can't find vrf_id:%d\n", vrf_id);
531 sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, 1);
533 sctp_ifap->localifa_flags &= SCTP_ADDR_VALID;
534 sctp_ifap->localifa_flags |= SCTP_BEING_DELETED;
535 vrf->total_ifa_count--;
536 LIST_REMOVE(sctp_ifap, next_bucket);
537 LIST_REMOVE(sctp_ifap, next_ifa);
538 if (sctp_ifap->ifn_p) {
539 sctp_ifap->ifn_p->ifa_count--;
540 if (sctp_ifap->address.sa.sa_family == AF_INET6)
541 sctp_ifap->ifn_p->num_v6--;
542 else if (sctp_ifap->address.sa.sa_family == AF_INET)
543 sctp_ifap->ifn_p->num_v4--;
544 if (SCTP_LIST_EMPTY(&sctp_ifap->ifn_p->ifalist)) {
545 sctp_delete_ifn(sctp_ifap->ifn_p, 1);
547 if ((sctp_ifap->ifn_p->num_v6 == 0) &&
548 (sctp_ifap->ifn_p->registered_af == AF_INET6)) {
549 SCTP_DEREGISTER_INTERFACE(ifn_index,
551 SCTP_REGISTER_INTERFACE(ifn_index,
553 sctp_ifap->ifn_p->registered_af = AF_INET;
554 } else if ((sctp_ifap->ifn_p->num_v4 == 0) &&
555 (sctp_ifap->ifn_p->registered_af == AF_INET)) {
556 SCTP_DEREGISTER_INTERFACE(ifn_index,
558 SCTP_REGISTER_INTERFACE(ifn_index,
560 sctp_ifap->ifn_p->registered_af = AF_INET6;
563 sctp_free_ifn(sctp_ifap->ifn_p);
564 sctp_ifap->ifn_p = NULL;
569 SCTPDBG(SCTP_DEBUG_PCB1, "Del Addr-ifn:%d Could not find address:",
571 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr);
576 SCTP_IPI_ADDR_UNLOCK();
578 struct sctp_laddr *wi;
580 wi = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_laddr, struct sctp_laddr);
583 * Gak, what can we do? We have lost an address
584 * change can you say HOSED?
586 SCTPDBG(SCTP_DEBUG_PCB1, "Lost and address change ???\n");
588 /* Opps, must decrement the count */
589 sctp_free_ifa(sctp_ifap);
592 SCTP_INCR_LADDR_COUNT();
593 bzero(wi, sizeof(*wi));
594 (void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
596 wi->action = SCTP_DEL_IP_ADDRESS;
597 SCTP_IPI_ITERATOR_WQ_LOCK();
599 * Should this really be a tailq? As it is we will process
600 * the newest first :-0
602 LIST_INSERT_HEAD(&sctppcbinfo.addr_wq, wi, sctp_nxt_addr);
603 SCTP_IPI_ITERATOR_WQ_UNLOCK();
605 sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
606 (struct sctp_inpcb *)NULL,
607 (struct sctp_tcb *)NULL,
608 (struct sctp_nets *)NULL);
615 static struct sctp_tcb *
616 sctp_tcb_special_locate(struct sctp_inpcb **inp_p, struct sockaddr *from,
617 struct sockaddr *to, struct sctp_nets **netp, uint32_t vrf_id)
619 /**** ASSUMSES THE CALLER holds the INP_INFO_RLOCK */
621 * If we support the TCP model, then we must now dig through to see
622 * if we can find our endpoint in the list of tcp ep's.
624 uint16_t lport, rport;
625 struct sctppcbhead *ephead;
626 struct sctp_inpcb *inp;
627 struct sctp_laddr *laddr;
628 struct sctp_tcb *stcb;
629 struct sctp_nets *net;
631 if ((to == NULL) || (from == NULL)) {
634 if (to->sa_family == AF_INET && from->sa_family == AF_INET) {
635 lport = ((struct sockaddr_in *)to)->sin_port;
636 rport = ((struct sockaddr_in *)from)->sin_port;
637 } else if (to->sa_family == AF_INET6 && from->sa_family == AF_INET6) {
638 lport = ((struct sockaddr_in6 *)to)->sin6_port;
639 rport = ((struct sockaddr_in6 *)from)->sin6_port;
643 ephead = &sctppcbinfo.sctp_tcpephash[SCTP_PCBHASH_ALLADDR(
644 (lport + rport), sctppcbinfo.hashtcpmark)];
646 * Ok now for each of the guys in this bucket we must look and see:
647 * - Does the remote port match. - Does there single association's
648 * addresses match this address (to). If so we update p_ep to point
649 * to this ep and return the tcb from it.
651 LIST_FOREACH(inp, ephead, sctp_hash) {
653 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
654 SCTP_INP_RUNLOCK(inp);
657 if (lport != inp->sctp_lport) {
658 SCTP_INP_RUNLOCK(inp);
661 if (inp->def_vrf_id != vrf_id) {
662 SCTP_INP_RUNLOCK(inp);
665 /* check to see if the ep has one of the addresses */
666 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
667 /* We are NOT bound all, so look further */
670 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
672 if (laddr->ifa == NULL) {
673 SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n", __FUNCTION__);
676 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
677 SCTPDBG(SCTP_DEBUG_PCB1, "ifa being deleted\n");
680 if (laddr->ifa->address.sa.sa_family ==
682 /* see if it matches */
683 struct sockaddr_in *intf_addr, *sin;
685 intf_addr = &laddr->ifa->address.sin;
686 sin = (struct sockaddr_in *)to;
687 if (from->sa_family == AF_INET) {
688 if (sin->sin_addr.s_addr ==
689 intf_addr->sin_addr.s_addr) {
694 struct sockaddr_in6 *intf_addr6;
695 struct sockaddr_in6 *sin6;
697 sin6 = (struct sockaddr_in6 *)
699 intf_addr6 = &laddr->ifa->address.sin6;
701 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
702 &intf_addr6->sin6_addr)) {
710 /* This endpoint does not have this address */
711 SCTP_INP_RUNLOCK(inp);
716 * Ok if we hit here the ep has the address, does it hold
720 stcb = LIST_FIRST(&inp->sctp_asoc_list);
722 SCTP_INP_RUNLOCK(inp);
726 if (stcb->rport != rport) {
727 /* remote port does not match. */
728 SCTP_TCB_UNLOCK(stcb);
729 SCTP_INP_RUNLOCK(inp);
732 /* Does this TCB have a matching address? */
733 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
735 if (net->ro._l_addr.sa.sa_family != from->sa_family) {
736 /* not the same family, can't be a match */
739 if (from->sa_family == AF_INET) {
740 struct sockaddr_in *sin, *rsin;
742 sin = (struct sockaddr_in *)&net->ro._l_addr;
743 rsin = (struct sockaddr_in *)from;
744 if (sin->sin_addr.s_addr ==
745 rsin->sin_addr.s_addr) {
750 /* Update the endpoint pointer */
752 SCTP_INP_RUNLOCK(inp);
756 struct sockaddr_in6 *sin6, *rsin6;
758 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
759 rsin6 = (struct sockaddr_in6 *)from;
760 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
761 &rsin6->sin6_addr)) {
766 /* Update the endpoint pointer */
768 SCTP_INP_RUNLOCK(inp);
773 SCTP_TCB_UNLOCK(stcb);
774 SCTP_INP_RUNLOCK(inp);
782 * 1) If I return a NULL you must decrement any INP ref cnt. 2) If I find an
783 * stcb, both will be locked (locked_tcb and stcb) but decrement will be done
784 * (if locked == NULL). 3) Decrement happens on return ONLY if locked ==
789 sctp_findassociation_ep_addr(struct sctp_inpcb **inp_p, struct sockaddr *remote,
790 struct sctp_nets **netp, struct sockaddr *local, struct sctp_tcb *locked_tcb)
792 struct sctpasochead *head;
793 struct sctp_inpcb *inp;
794 struct sctp_tcb *stcb = NULL;
795 struct sctp_nets *net;
799 if (remote->sa_family == AF_INET) {
800 rport = (((struct sockaddr_in *)remote)->sin_port);
801 } else if (remote->sa_family == AF_INET6) {
802 rport = (((struct sockaddr_in6 *)remote)->sin6_port);
808 * UN-lock so we can do proper locking here this occurs when
809 * called from load_addresses_from_init.
811 SCTP_TCB_UNLOCK(locked_tcb);
813 SCTP_INP_INFO_RLOCK();
814 if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
816 * Now either this guy is our listener or it's the
817 * connector. If it is the one that issued the connect, then
818 * it's only chance is to be the first TCB in the list. If
819 * it is the acceptor, then do the special_lookup to hash
820 * and find the real inp.
822 if ((inp->sctp_socket) && (inp->sctp_socket->so_qlimit)) {
823 /* to is peer addr, from is my addr */
824 stcb = sctp_tcb_special_locate(inp_p, remote, local,
825 netp, inp->def_vrf_id);
826 if ((stcb != NULL) && (locked_tcb == NULL)) {
827 /* we have a locked tcb, lower refcount */
829 SCTP_INP_DECR_REF(inp);
830 SCTP_INP_WUNLOCK(inp);
832 if ((locked_tcb != NULL) && (locked_tcb != stcb)) {
833 SCTP_INP_RLOCK(locked_tcb->sctp_ep);
834 SCTP_TCB_LOCK(locked_tcb);
835 SCTP_INP_RUNLOCK(locked_tcb->sctp_ep);
837 SCTP_INP_INFO_RUNLOCK();
841 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
844 stcb = LIST_FIRST(&inp->sctp_asoc_list);
849 if (stcb->rport != rport) {
850 /* remote port does not match. */
851 SCTP_TCB_UNLOCK(stcb);
854 /* now look at the list of remote addresses */
855 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
857 if (net == (TAILQ_NEXT(net, sctp_next))) {
858 panic("Corrupt net list");
861 if (net->ro._l_addr.sa.sa_family !=
863 /* not the same family */
866 if (remote->sa_family == AF_INET) {
867 struct sockaddr_in *sin, *rsin;
869 sin = (struct sockaddr_in *)
871 rsin = (struct sockaddr_in *)remote;
872 if (sin->sin_addr.s_addr ==
873 rsin->sin_addr.s_addr) {
878 if (locked_tcb == NULL) {
879 SCTP_INP_DECR_REF(inp);
880 } else if (locked_tcb != stcb) {
881 SCTP_TCB_LOCK(locked_tcb);
883 SCTP_INP_WUNLOCK(inp);
884 SCTP_INP_INFO_RUNLOCK();
887 } else if (remote->sa_family == AF_INET6) {
888 struct sockaddr_in6 *sin6, *rsin6;
890 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
891 rsin6 = (struct sockaddr_in6 *)remote;
892 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
893 &rsin6->sin6_addr)) {
898 if (locked_tcb == NULL) {
899 SCTP_INP_DECR_REF(inp);
900 } else if (locked_tcb != stcb) {
901 SCTP_TCB_LOCK(locked_tcb);
903 SCTP_INP_WUNLOCK(inp);
904 SCTP_INP_INFO_RUNLOCK();
909 SCTP_TCB_UNLOCK(stcb);
913 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
916 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(rport,
917 inp->sctp_hashmark)];
921 LIST_FOREACH(stcb, head, sctp_tcbhash) {
922 if (stcb->rport != rport) {
923 /* remote port does not match */
926 /* now look at the list of remote addresses */
928 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
930 if (net == (TAILQ_NEXT(net, sctp_next))) {
931 panic("Corrupt net list");
934 if (net->ro._l_addr.sa.sa_family !=
936 /* not the same family */
939 if (remote->sa_family == AF_INET) {
940 struct sockaddr_in *sin, *rsin;
942 sin = (struct sockaddr_in *)
944 rsin = (struct sockaddr_in *)remote;
945 if (sin->sin_addr.s_addr ==
946 rsin->sin_addr.s_addr) {
951 if (locked_tcb == NULL) {
952 SCTP_INP_DECR_REF(inp);
953 } else if (locked_tcb != stcb) {
954 SCTP_TCB_LOCK(locked_tcb);
956 SCTP_INP_WUNLOCK(inp);
957 SCTP_INP_INFO_RUNLOCK();
960 } else if (remote->sa_family == AF_INET6) {
961 struct sockaddr_in6 *sin6, *rsin6;
963 sin6 = (struct sockaddr_in6 *)
965 rsin6 = (struct sockaddr_in6 *)remote;
966 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
967 &rsin6->sin6_addr)) {
972 if (locked_tcb == NULL) {
973 SCTP_INP_DECR_REF(inp);
974 } else if (locked_tcb != stcb) {
975 SCTP_TCB_LOCK(locked_tcb);
977 SCTP_INP_WUNLOCK(inp);
978 SCTP_INP_INFO_RUNLOCK();
983 SCTP_TCB_UNLOCK(stcb);
987 /* clean up for returning null */
989 SCTP_TCB_LOCK(locked_tcb);
991 SCTP_INP_WUNLOCK(inp);
992 SCTP_INP_INFO_RUNLOCK();
998 * Find an association for a specific endpoint using the association id given
999 * out in the COMM_UP notification
1003 sctp_findassociation_ep_asocid(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock)
1006 * Use my the assoc_id to find a endpoint
1008 struct sctpasochead *head;
1009 struct sctp_tcb *stcb;
1012 if (asoc_id == 0 || inp == NULL) {
1015 SCTP_INP_INFO_RLOCK();
1016 id = (uint32_t) asoc_id;
1017 head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(id,
1018 sctppcbinfo.hashasocmark)];
1020 /* invalid id TSNH */
1021 SCTP_INP_INFO_RUNLOCK();
1024 LIST_FOREACH(stcb, head, sctp_asocs) {
1025 SCTP_INP_RLOCK(stcb->sctp_ep);
1026 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1027 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1028 SCTP_INP_INFO_RUNLOCK();
1031 if (stcb->asoc.assoc_id == id) {
1033 if (inp != stcb->sctp_ep) {
1035 * some other guy has the same id active (id
1038 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1042 SCTP_TCB_LOCK(stcb);
1044 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1045 SCTP_INP_INFO_RUNLOCK();
1048 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1050 /* Ok if we missed here, lets try the restart hash */
1051 head = &sctppcbinfo.sctp_restarthash[SCTP_PCBHASH_ASOC(id, sctppcbinfo.hashrestartmark)];
1053 /* invalid id TSNH */
1054 SCTP_INP_INFO_RUNLOCK();
1057 LIST_FOREACH(stcb, head, sctp_tcbrestarhash) {
1058 SCTP_INP_RLOCK(stcb->sctp_ep);
1059 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1060 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1064 SCTP_TCB_LOCK(stcb);
1066 if (stcb->asoc.assoc_id == id) {
1068 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1069 if (inp != stcb->sctp_ep) {
1071 * some other guy has the same id active (id
1075 SCTP_TCB_UNLOCK(stcb);
1079 SCTP_INP_INFO_RUNLOCK();
1082 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1085 SCTP_TCB_UNLOCK(stcb);
1088 SCTP_INP_INFO_RUNLOCK();
1093 static struct sctp_inpcb *
1094 sctp_endpoint_probe(struct sockaddr *nam, struct sctppcbhead *head,
1095 uint16_t lport, uint32_t vrf_id)
1097 struct sctp_inpcb *inp;
1098 struct sockaddr_in *sin;
1099 struct sockaddr_in6 *sin6;
1100 struct sctp_laddr *laddr;
1104 * Endpoing probe expects that the INP_INFO is locked.
1106 if (nam->sa_family == AF_INET) {
1107 sin = (struct sockaddr_in *)nam;
1109 } else if (nam->sa_family == AF_INET6) {
1110 sin6 = (struct sockaddr_in6 *)nam;
1113 /* unsupported family */
1118 LIST_FOREACH(inp, head, sctp_hash) {
1119 SCTP_INP_RLOCK(inp);
1120 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1121 SCTP_INP_RUNLOCK(inp);
1124 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) &&
1125 (inp->sctp_lport == lport)) {
1127 if ((nam->sa_family == AF_INET) &&
1128 (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1129 SCTP_IPV6_V6ONLY(inp)) {
1130 /* IPv4 on a IPv6 socket with ONLY IPv6 set */
1131 SCTP_INP_RUNLOCK(inp);
1134 /* A V6 address and the endpoint is NOT bound V6 */
1135 if (nam->sa_family == AF_INET6 &&
1136 (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
1137 SCTP_INP_RUNLOCK(inp);
1140 /* does a VRF id match? */
1142 if (inp->def_vrf_id == vrf_id)
1145 SCTP_INP_RUNLOCK(inp);
1150 SCTP_INP_RUNLOCK(inp);
1153 if ((nam->sa_family == AF_INET) &&
1154 (sin->sin_addr.s_addr == INADDR_ANY)) {
1155 /* Can't hunt for one that has no address specified */
1157 } else if ((nam->sa_family == AF_INET6) &&
1158 (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))) {
1159 /* Can't hunt for one that has no address specified */
1163 * ok, not bound to all so see if we can find a EP bound to this
1166 LIST_FOREACH(inp, head, sctp_hash) {
1167 SCTP_INP_RLOCK(inp);
1168 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1169 SCTP_INP_RUNLOCK(inp);
1172 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)) {
1173 SCTP_INP_RUNLOCK(inp);
1177 * Ok this could be a likely candidate, look at all of its
1180 if (inp->sctp_lport != lport) {
1181 SCTP_INP_RUNLOCK(inp);
1184 /* does a VRF id match? */
1186 if (inp->def_vrf_id == vrf_id)
1190 SCTP_INP_RUNLOCK(inp);
1193 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1194 if (laddr->ifa == NULL) {
1195 SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n",
1199 SCTPDBG(SCTP_DEBUG_PCB1, "Ok laddr->ifa:%p is possible, ",
1201 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
1202 SCTPDBG(SCTP_DEBUG_PCB1, "Huh IFA being deleted\n");
1205 if (laddr->ifa->address.sa.sa_family == nam->sa_family) {
1206 /* possible, see if it matches */
1207 struct sockaddr_in *intf_addr;
1209 intf_addr = &laddr->ifa->address.sin;
1210 if (nam->sa_family == AF_INET) {
1211 if (sin->sin_addr.s_addr ==
1212 intf_addr->sin_addr.s_addr) {
1213 SCTP_INP_RUNLOCK(inp);
1216 } else if (nam->sa_family == AF_INET6) {
1217 struct sockaddr_in6 *intf_addr6;
1219 intf_addr6 = &laddr->ifa->address.sin6;
1220 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
1221 &intf_addr6->sin6_addr)) {
1222 SCTP_INP_RUNLOCK(inp);
1228 SCTP_INP_RUNLOCK(inp);
1235 sctp_pcb_findep(struct sockaddr *nam, int find_tcp_pool, int have_lock,
1239 * First we check the hash table to see if someone has this port
1240 * bound with just the port.
1242 struct sctp_inpcb *inp;
1243 struct sctppcbhead *head;
1244 struct sockaddr_in *sin;
1245 struct sockaddr_in6 *sin6;
1248 if (nam->sa_family == AF_INET) {
1249 sin = (struct sockaddr_in *)nam;
1250 lport = ((struct sockaddr_in *)nam)->sin_port;
1251 } else if (nam->sa_family == AF_INET6) {
1252 sin6 = (struct sockaddr_in6 *)nam;
1253 lport = ((struct sockaddr_in6 *)nam)->sin6_port;
1255 /* unsupported family */
1259 * I could cheat here and just cast to one of the types but we will
1260 * do it right. It also provides the check against an Unsupported
1263 /* Find the head of the ALLADDR chain */
1264 if (have_lock == 0) {
1265 SCTP_INP_INFO_RLOCK();
1268 head = &sctppcbinfo.sctp_ephash[SCTP_PCBHASH_ALLADDR(lport,
1269 sctppcbinfo.hashmark)];
1270 inp = sctp_endpoint_probe(nam, head, lport, vrf_id);
1273 * If the TCP model exists it could be that the main listening
1274 * endpoint is gone but there exists a connected socket for this guy
1275 * yet. If so we can return the first one that we find. This may NOT
1276 * be the correct one but the sctp_findassociation_ep_addr has
1277 * further code to look at all TCP models.
1279 if (inp == NULL && find_tcp_pool) {
1282 for (i = 0; i < sctppcbinfo.hashtblsize; i++) {
1284 * This is real gross, but we do NOT have a remote
1285 * port at this point depending on who is calling.
1286 * We must therefore look for ANY one that matches
1289 head = &sctppcbinfo.sctp_tcpephash[i];
1290 if (LIST_FIRST(head)) {
1291 inp = sctp_endpoint_probe(nam, head, lport, vrf_id);
1300 SCTP_INP_INCR_REF(inp);
1302 if (have_lock == 0) {
1303 SCTP_INP_INFO_RUNLOCK();
1309 * Find an association for an endpoint with the pointer to whom you want to
1310 * send to and the endpoint pointer. The address can be IPv4 or IPv6. We may
1311 * need to change the *to to some other struct like a mbuf...
1314 sctp_findassociation_addr_sa(struct sockaddr *to, struct sockaddr *from,
1315 struct sctp_inpcb **inp_p, struct sctp_nets **netp, int find_tcp_pool,
1318 struct sctp_inpcb *inp = NULL;
1319 struct sctp_tcb *retval;
1321 SCTP_INP_INFO_RLOCK();
1322 if (find_tcp_pool) {
1323 if (inp_p != NULL) {
1324 retval = sctp_tcb_special_locate(inp_p, from, to, netp,
1327 retval = sctp_tcb_special_locate(&inp, from, to, netp,
1330 if (retval != NULL) {
1331 SCTP_INP_INFO_RUNLOCK();
1335 inp = sctp_pcb_findep(to, 0, 1, vrf_id);
1336 if (inp_p != NULL) {
1339 SCTP_INP_INFO_RUNLOCK();
1345 * ok, we have an endpoint, now lets find the assoc for it (if any)
1346 * we now place the source address or from in the to of the find
1347 * endpoint call. Since in reality this chain is used from the
1348 * inbound packet side.
1350 if (inp_p != NULL) {
1351 retval = sctp_findassociation_ep_addr(inp_p, from, netp, to,
1354 retval = sctp_findassociation_ep_addr(&inp, from, netp, to,
1362 * This routine will grub through the mbuf that is a INIT or INIT-ACK and
1363 * find all addresses that the sender has specified in any address list. Each
1364 * address will be used to lookup the TCB and see if one exits.
1366 static struct sctp_tcb *
1367 sctp_findassociation_special_addr(struct mbuf *m, int iphlen, int offset,
1368 struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp,
1369 struct sockaddr *dest)
1371 struct sockaddr_in sin4;
1372 struct sockaddr_in6 sin6;
1373 struct sctp_paramhdr *phdr, parm_buf;
1374 struct sctp_tcb *retval;
1375 uint32_t ptype, plen;
1377 memset(&sin4, 0, sizeof(sin4));
1378 memset(&sin6, 0, sizeof(sin6));
1379 sin4.sin_len = sizeof(sin4);
1380 sin4.sin_family = AF_INET;
1381 sin4.sin_port = sh->src_port;
1382 sin6.sin6_len = sizeof(sin6);
1383 sin6.sin6_family = AF_INET6;
1384 sin6.sin6_port = sh->src_port;
1387 offset += sizeof(struct sctp_init_chunk);
1389 phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
1390 while (phdr != NULL) {
1391 /* now we must see if we want the parameter */
1392 ptype = ntohs(phdr->param_type);
1393 plen = ntohs(phdr->param_length);
1397 if (ptype == SCTP_IPV4_ADDRESS &&
1398 plen == sizeof(struct sctp_ipv4addr_param)) {
1399 /* Get the rest of the address */
1400 struct sctp_ipv4addr_param ip4_parm, *p4;
1402 phdr = sctp_get_next_param(m, offset,
1403 (struct sctp_paramhdr *)&ip4_parm, min(plen, sizeof(ip4_parm)));
1407 p4 = (struct sctp_ipv4addr_param *)phdr;
1408 memcpy(&sin4.sin_addr, &p4->addr, sizeof(p4->addr));
1410 retval = sctp_findassociation_ep_addr(inp_p,
1411 (struct sockaddr *)&sin4, netp, dest, NULL);
1412 if (retval != NULL) {
1415 } else if (ptype == SCTP_IPV6_ADDRESS &&
1416 plen == sizeof(struct sctp_ipv6addr_param)) {
1417 /* Get the rest of the address */
1418 struct sctp_ipv6addr_param ip6_parm, *p6;
1420 phdr = sctp_get_next_param(m, offset,
1421 (struct sctp_paramhdr *)&ip6_parm, min(plen, sizeof(ip6_parm)));
1425 p6 = (struct sctp_ipv6addr_param *)phdr;
1426 memcpy(&sin6.sin6_addr, &p6->addr, sizeof(p6->addr));
1428 retval = sctp_findassociation_ep_addr(inp_p,
1429 (struct sockaddr *)&sin6, netp, dest, NULL);
1430 if (retval != NULL) {
1434 offset += SCTP_SIZE32(plen);
1435 phdr = sctp_get_next_param(m, offset, &parm_buf,
1442 static struct sctp_tcb *
1443 sctp_findassoc_by_vtag(struct sockaddr *from, uint32_t vtag,
1444 struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint16_t rport,
1445 uint16_t lport, int skip_src_check)
1448 * Use my vtag to hash. If we find it we then verify the source addr
1449 * is in the assoc. If all goes well we save a bit on rec of a
1452 struct sctpasochead *head;
1453 struct sctp_nets *net;
1454 struct sctp_tcb *stcb;
1458 SCTP_INP_INFO_RLOCK();
1459 head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(vtag,
1460 sctppcbinfo.hashasocmark)];
1463 SCTP_INP_INFO_RUNLOCK();
1466 LIST_FOREACH(stcb, head, sctp_asocs) {
1467 SCTP_INP_RLOCK(stcb->sctp_ep);
1468 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1469 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1472 SCTP_TCB_LOCK(stcb);
1473 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1474 if (stcb->asoc.my_vtag == vtag) {
1476 if (stcb->rport != rport) {
1478 * we could remove this if vtags are unique
1479 * across the system.
1481 SCTP_TCB_UNLOCK(stcb);
1484 if (stcb->sctp_ep->sctp_lport != lport) {
1486 * we could remove this if vtags are unique
1487 * across the system.
1489 SCTP_TCB_UNLOCK(stcb);
1492 if (skip_src_check) {
1493 *netp = NULL; /* unknown */
1495 *inp_p = stcb->sctp_ep;
1496 SCTP_INP_INFO_RUNLOCK();
1499 net = sctp_findnet(stcb, from);
1503 SCTP_STAT_INCR(sctps_vtagexpress);
1504 *inp_p = stcb->sctp_ep;
1505 SCTP_INP_INFO_RUNLOCK();
1509 * not him, this should only happen in rare
1510 * cases so I peg it.
1512 SCTP_STAT_INCR(sctps_vtagbogus);
1515 SCTP_TCB_UNLOCK(stcb);
1517 SCTP_INP_INFO_RUNLOCK();
1522 * Find an association with the pointer to the inbound IP packet. This can be
1523 * a IPv4 or IPv6 packet.
1526 sctp_findassociation_addr(struct mbuf *m, int iphlen, int offset,
1527 struct sctphdr *sh, struct sctp_chunkhdr *ch,
1528 struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id)
1532 struct sctp_tcb *retval;
1533 struct sockaddr_storage to_store, from_store;
1534 struct sockaddr *to = (struct sockaddr *)&to_store;
1535 struct sockaddr *from = (struct sockaddr *)&from_store;
1536 struct sctp_inpcb *inp;
1538 iph = mtod(m, struct ip *);
1539 if (iph->ip_v == IPVERSION) {
1541 struct sockaddr_in *from4;
1543 from4 = (struct sockaddr_in *)&from_store;
1544 bzero(from4, sizeof(*from4));
1545 from4->sin_family = AF_INET;
1546 from4->sin_len = sizeof(struct sockaddr_in);
1547 from4->sin_addr.s_addr = iph->ip_src.s_addr;
1548 from4->sin_port = sh->src_port;
1549 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
1551 struct ip6_hdr *ip6;
1552 struct sockaddr_in6 *from6;
1554 ip6 = mtod(m, struct ip6_hdr *);
1555 from6 = (struct sockaddr_in6 *)&from_store;
1556 bzero(from6, sizeof(*from6));
1557 from6->sin6_family = AF_INET6;
1558 from6->sin6_len = sizeof(struct sockaddr_in6);
1559 from6->sin6_addr = ip6->ip6_src;
1560 from6->sin6_port = sh->src_port;
1561 /* Get the scopes in properly to the sin6 addr's */
1562 /* we probably don't need these operations */
1563 (void)sa6_recoverscope(from6);
1564 sa6_embedscope(from6, ip6_use_defzone);
1566 /* Currently not supported. */
1570 /* we only go down this path if vtag is non-zero */
1571 retval = sctp_findassoc_by_vtag(from, ntohl(sh->v_tag),
1572 inp_p, netp, sh->src_port, sh->dest_port, 0);
1577 if (iph->ip_v == IPVERSION) {
1579 struct sockaddr_in *to4;
1581 to4 = (struct sockaddr_in *)&to_store;
1582 bzero(to4, sizeof(*to4));
1583 to4->sin_family = AF_INET;
1584 to4->sin_len = sizeof(struct sockaddr_in);
1585 to4->sin_addr.s_addr = iph->ip_dst.s_addr;
1586 to4->sin_port = sh->dest_port;
1587 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
1589 struct ip6_hdr *ip6;
1590 struct sockaddr_in6 *to6;
1592 ip6 = mtod(m, struct ip6_hdr *);
1593 to6 = (struct sockaddr_in6 *)&to_store;
1594 bzero(to6, sizeof(*to6));
1595 to6->sin6_family = AF_INET6;
1596 to6->sin6_len = sizeof(struct sockaddr_in6);
1597 to6->sin6_addr = ip6->ip6_dst;
1598 to6->sin6_port = sh->dest_port;
1599 /* Get the scopes in properly to the sin6 addr's */
1600 /* we probably don't need these operations */
1601 (void)sa6_recoverscope(to6);
1602 sa6_embedscope(to6, ip6_use_defzone);
1605 if ((ch->chunk_type != SCTP_INITIATION) &&
1606 (ch->chunk_type != SCTP_INITIATION_ACK) &&
1607 (ch->chunk_type != SCTP_COOKIE_ACK) &&
1608 (ch->chunk_type != SCTP_COOKIE_ECHO)) {
1609 /* Other chunk types go to the tcp pool. */
1613 retval = sctp_findassociation_addr_sa(to, from, inp_p, netp,
1614 find_tcp_pool, vrf_id);
1617 retval = sctp_findassociation_addr_sa(to, from, &inp, netp,
1618 find_tcp_pool, vrf_id);
1620 SCTPDBG(SCTP_DEBUG_PCB1, "retval:%p inp:%p\n", retval, inp);
1621 if (retval == NULL && inp) {
1622 /* Found a EP but not this address */
1623 if ((ch->chunk_type == SCTP_INITIATION) ||
1624 (ch->chunk_type == SCTP_INITIATION_ACK)) {
1626 * special hook, we do NOT return linp or an
1627 * association that is linked to an existing
1628 * association that is under the TCP pool (i.e. no
1629 * listener exists). The endpoint finding routine
1630 * will always find a listner before examining the
1633 if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
1639 retval = sctp_findassociation_special_addr(m, iphlen,
1640 offset, sh, &inp, netp, to);
1641 if (inp_p != NULL) {
1646 SCTPDBG(SCTP_DEBUG_PCB1, "retval is %p\n", retval);
1651 * lookup an association by an ASCONF lookup address.
1652 * if the lookup address is 0.0.0.0 or ::0, use the vtag to do the lookup
1655 sctp_findassociation_ep_asconf(struct mbuf *m, int iphlen, int offset,
1656 struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp)
1658 struct sctp_tcb *stcb;
1659 struct sockaddr_in *sin;
1660 struct sockaddr_in6 *sin6;
1661 struct sockaddr_storage local_store, remote_store;
1663 struct sctp_paramhdr parm_buf, *phdr;
1665 int zero_address = 0;
1668 memset(&local_store, 0, sizeof(local_store));
1669 memset(&remote_store, 0, sizeof(remote_store));
1671 /* First get the destination address setup too. */
1672 iph = mtod(m, struct ip *);
1673 if (iph->ip_v == IPVERSION) {
1675 sin = (struct sockaddr_in *)&local_store;
1676 sin->sin_family = AF_INET;
1677 sin->sin_len = sizeof(*sin);
1678 sin->sin_port = sh->dest_port;
1679 sin->sin_addr.s_addr = iph->ip_dst.s_addr;
1680 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
1682 struct ip6_hdr *ip6;
1684 ip6 = mtod(m, struct ip6_hdr *);
1685 sin6 = (struct sockaddr_in6 *)&local_store;
1686 sin6->sin6_family = AF_INET6;
1687 sin6->sin6_len = sizeof(*sin6);
1688 sin6->sin6_port = sh->dest_port;
1689 sin6->sin6_addr = ip6->ip6_dst;
1694 phdr = sctp_get_next_param(m, offset + sizeof(struct sctp_asconf_chunk),
1695 &parm_buf, sizeof(struct sctp_paramhdr));
1697 SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf lookup addr\n",
1701 ptype = (int)((uint32_t) ntohs(phdr->param_type));
1702 /* get the correlation address */
1703 if (ptype == SCTP_IPV6_ADDRESS) {
1704 /* ipv6 address param */
1705 struct sctp_ipv6addr_param *p6, p6_buf;
1707 if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv6addr_param)) {
1710 p6 = (struct sctp_ipv6addr_param *)sctp_get_next_param(m,
1711 offset + sizeof(struct sctp_asconf_chunk),
1712 &p6_buf.ph, sizeof(*p6));
1714 SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v6 lookup addr\n",
1718 sin6 = (struct sockaddr_in6 *)&remote_store;
1719 sin6->sin6_family = AF_INET6;
1720 sin6->sin6_len = sizeof(*sin6);
1721 sin6->sin6_port = sh->src_port;
1722 memcpy(&sin6->sin6_addr, &p6->addr, sizeof(struct in6_addr));
1723 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
1725 } else if (ptype == SCTP_IPV4_ADDRESS) {
1726 /* ipv4 address param */
1727 struct sctp_ipv4addr_param *p4, p4_buf;
1729 if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv4addr_param)) {
1732 p4 = (struct sctp_ipv4addr_param *)sctp_get_next_param(m,
1733 offset + sizeof(struct sctp_asconf_chunk),
1734 &p4_buf.ph, sizeof(*p4));
1736 SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v4 lookup addr\n",
1740 sin = (struct sockaddr_in *)&remote_store;
1741 sin->sin_family = AF_INET;
1742 sin->sin_len = sizeof(*sin);
1743 sin->sin_port = sh->src_port;
1744 memcpy(&sin->sin_addr, &p4->addr, sizeof(struct in_addr));
1745 if (sin->sin_addr.s_addr == INADDR_ANY)
1748 /* invalid address param type */
1753 stcb = sctp_findassoc_by_vtag(NULL, ntohl(sh->v_tag), inp_p,
1754 netp, sh->src_port, sh->dest_port, 1);
1756 * printf("findassociation_ep_asconf: zero lookup address
1757 * finds stcb 0x%x\n", (uint32_t)stcb);
1760 stcb = sctp_findassociation_ep_addr(inp_p,
1761 (struct sockaddr *)&remote_store, netp,
1762 (struct sockaddr *)&local_store, NULL);
1769 * allocate a sctp_inpcb and setup a temporary binding to a port/all
1770 * addresses. This way if we don't get a bind we by default pick a ephemeral
1771 * port with all addresses bound.
1774 sctp_inpcb_alloc(struct socket *so, uint32_t vrf_id)
1777 * we get called when a new endpoint starts up. We need to allocate
1778 * the sctp_inpcb structure from the zone and init it. Mark it as
1779 * unbound and find a port that we can use as an ephemeral with
1780 * INADDR_ANY. If the user binds later no problem we can then add in
1781 * the specific addresses. And setup the default parameters for the
1785 struct sctp_inpcb *inp;
1787 struct timeval time;
1788 sctp_sharedkey_t *null_key;
1792 SCTP_INP_INFO_WLOCK();
1793 inp = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_ep, struct sctp_inpcb);
1795 SCTP_PRINTF("Out of SCTP-INPCB structures - no resources\n");
1796 SCTP_INP_INFO_WUNLOCK();
1800 bzero(inp, sizeof(*inp));
1802 /* bump generations */
1803 /* setup socket pointers */
1804 inp->sctp_socket = so;
1805 inp->ip_inp.inp.inp_socket = so;
1807 inp->partial_delivery_point = SCTP_SB_LIMIT_RCV(so) >> SCTP_PARTIAL_DELIVERY_SHIFT;
1808 inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
1812 struct inpcbpolicy *pcb_sp = NULL;
1814 error = ipsec_init_policy(so, &pcb_sp);
1815 /* Arrange to share the policy */
1816 inp->ip_inp.inp.inp_sp = pcb_sp;
1817 ((struct in6pcb *)(&inp->ip_inp.inp))->in6p_sp = pcb_sp;
1820 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
1821 SCTP_INP_INFO_WUNLOCK();
1825 SCTP_INCR_EP_COUNT();
1826 inp->ip_inp.inp.inp_ip_ttl = ip_defttl;
1827 SCTP_INP_INFO_WUNLOCK();
1829 so->so_pcb = (caddr_t)inp;
1831 if ((SCTP_SO_TYPE(so) == SOCK_DGRAM) ||
1832 (SCTP_SO_TYPE(so) == SOCK_SEQPACKET)) {
1833 /* UDP style socket */
1834 inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
1835 SCTP_PCB_FLAGS_UNBOUND);
1836 /* Be sure it is NON-BLOCKING IO for UDP */
1837 /* SCTP_SET_SO_NBIO(so); */
1838 } else if (SCTP_SO_TYPE(so) == SOCK_STREAM) {
1839 /* TCP style socket */
1840 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
1841 SCTP_PCB_FLAGS_UNBOUND);
1842 /* Be sure we have blocking IO by default */
1843 SCTP_CLEAR_SO_NBIO(so);
1846 * unsupported socket type (RAW, etc)- in case we missed it
1849 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
1850 return (EOPNOTSUPP);
1852 sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
1854 inp->sctp_tcbhash = SCTP_HASH_INIT(sctp_pcbtblsize,
1855 &inp->sctp_hashmark);
1856 if (inp->sctp_tcbhash == NULL) {
1857 SCTP_PRINTF("Out of SCTP-INPCB->hashinit - no resources\n");
1858 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
1861 inp->def_vrf_id = vrf_id;
1863 SCTP_INP_INFO_WLOCK();
1864 SCTP_INP_LOCK_INIT(inp);
1865 INP_LOCK_INIT(&inp->ip_inp.inp, "inp", "sctpinp");
1866 SCTP_INP_READ_INIT(inp);
1867 SCTP_ASOC_CREATE_LOCK_INIT(inp);
1868 /* lock the new ep */
1869 SCTP_INP_WLOCK(inp);
1871 /* add it to the info area */
1872 LIST_INSERT_HEAD(&sctppcbinfo.listhead, inp, sctp_list);
1873 SCTP_INP_INFO_WUNLOCK();
1875 TAILQ_INIT(&inp->read_queue);
1876 LIST_INIT(&inp->sctp_addr_list);
1878 LIST_INIT(&inp->sctp_asoc_list);
1880 #ifdef SCTP_TRACK_FREED_ASOCS
1882 LIST_INIT(&inp->sctp_asoc_free_list);
1884 /* Init the timer structure for signature change */
1885 SCTP_OS_TIMER_INIT(&inp->sctp_ep.signature_change.timer);
1886 inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NEWCOOKIE;
1888 /* now init the actual endpoint default data */
1891 /* setup the base timeout information */
1892 m->sctp_timeoutticks[SCTP_TIMER_SEND] = SEC_TO_TICKS(SCTP_SEND_SEC); /* needed ? */
1893 m->sctp_timeoutticks[SCTP_TIMER_INIT] = SEC_TO_TICKS(SCTP_INIT_SEC); /* needed ? */
1894 m->sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sctp_delayed_sack_time_default);
1895 m->sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(sctp_heartbeat_interval_default);
1896 m->sctp_timeoutticks[SCTP_TIMER_PMTU] = SEC_TO_TICKS(sctp_pmtu_raise_time_default);
1897 m->sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] = SEC_TO_TICKS(sctp_shutdown_guard_time_default);
1898 m->sctp_timeoutticks[SCTP_TIMER_SIGNATURE] = SEC_TO_TICKS(sctp_secret_lifetime_default);
1899 /* all max/min max are in ms */
1900 m->sctp_maxrto = sctp_rto_max_default;
1901 m->sctp_minrto = sctp_rto_min_default;
1902 m->initial_rto = sctp_rto_initial_default;
1903 m->initial_init_rto_max = sctp_init_rto_max_default;
1904 m->sctp_sack_freq = sctp_sack_freq_default;
1906 m->max_open_streams_intome = MAX_SCTP_STREAMS;
1908 m->max_init_times = sctp_init_rtx_max_default;
1909 m->max_send_times = sctp_assoc_rtx_max_default;
1910 m->def_net_failure = sctp_path_rtx_max_default;
1911 m->sctp_sws_sender = SCTP_SWS_SENDER_DEF;
1912 m->sctp_sws_receiver = SCTP_SWS_RECEIVER_DEF;
1913 m->max_burst = sctp_max_burst_default;
1914 if ((sctp_default_cc_module >= SCTP_CC_RFC2581) &&
1915 (sctp_default_cc_module <= SCTP_CC_HTCP)) {
1916 m->sctp_default_cc_module = sctp_default_cc_module;
1918 /* sysctl done with invalid value, set to 2581 */
1919 m->sctp_default_cc_module = SCTP_CC_RFC2581;
1921 /* number of streams to pre-open on a association */
1922 m->pre_open_stream_count = sctp_nr_outgoing_streams_default;
1924 /* Add adaptation cookie */
1925 m->adaptation_layer_indicator = 0x504C5253;
1927 /* seed random number generator */
1928 m->random_counter = 1;
1929 m->store_at = SCTP_SIGNATURE_SIZE;
1930 SCTP_READ_RANDOM(m->random_numbers, sizeof(m->random_numbers));
1931 sctp_fill_random_store(m);
1933 /* Minimum cookie size */
1934 m->size_of_a_cookie = (sizeof(struct sctp_init_msg) * 2) +
1935 sizeof(struct sctp_state_cookie);
1936 m->size_of_a_cookie += SCTP_SIGNATURE_SIZE;
1938 /* Setup the initial secret */
1939 (void)SCTP_GETTIME_TIMEVAL(&time);
1940 m->time_of_secret_change = time.tv_sec;
1942 for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
1943 m->secret_key[0][i] = sctp_select_initial_TSN(m);
1945 sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL);
1947 /* How long is a cookie good for ? */
1948 m->def_cookie_life = MSEC_TO_TICKS(sctp_valid_cookie_life_default);
1950 * Initialize authentication parameters
1952 m->local_hmacs = sctp_default_supported_hmaclist();
1953 m->local_auth_chunks = sctp_alloc_chunklist();
1954 sctp_auth_set_default_chunks(m->local_auth_chunks);
1955 LIST_INIT(&m->shared_keys);
1956 /* add default NULL key as key id 0 */
1957 null_key = sctp_alloc_sharedkey();
1958 sctp_insert_sharedkey(&m->shared_keys, null_key);
1959 SCTP_INP_WUNLOCK(inp);
1960 #ifdef SCTP_LOG_CLOSING
1961 sctp_log_closing(inp, NULL, 12);
1968 sctp_move_pcb_and_assoc(struct sctp_inpcb *old_inp, struct sctp_inpcb *new_inp,
1969 struct sctp_tcb *stcb)
1971 struct sctp_nets *net;
1972 uint16_t lport, rport;
1973 struct sctppcbhead *head;
1974 struct sctp_laddr *laddr, *oladdr;
1976 SCTP_TCB_UNLOCK(stcb);
1977 SCTP_INP_INFO_WLOCK();
1978 SCTP_INP_WLOCK(old_inp);
1979 SCTP_INP_WLOCK(new_inp);
1980 SCTP_TCB_LOCK(stcb);
1982 new_inp->sctp_ep.time_of_secret_change =
1983 old_inp->sctp_ep.time_of_secret_change;
1984 memcpy(new_inp->sctp_ep.secret_key, old_inp->sctp_ep.secret_key,
1985 sizeof(old_inp->sctp_ep.secret_key));
1986 new_inp->sctp_ep.current_secret_number =
1987 old_inp->sctp_ep.current_secret_number;
1988 new_inp->sctp_ep.last_secret_number =
1989 old_inp->sctp_ep.last_secret_number;
1990 new_inp->sctp_ep.size_of_a_cookie = old_inp->sctp_ep.size_of_a_cookie;
1992 /* make it so new data pours into the new socket */
1993 stcb->sctp_socket = new_inp->sctp_socket;
1994 stcb->sctp_ep = new_inp;
1996 /* Copy the port across */
1997 lport = new_inp->sctp_lport = old_inp->sctp_lport;
1998 rport = stcb->rport;
1999 /* Pull the tcb from the old association */
2000 LIST_REMOVE(stcb, sctp_tcbhash);
2001 LIST_REMOVE(stcb, sctp_tcblist);
2003 /* Now insert the new_inp into the TCP connected hash */
2004 head = &sctppcbinfo.sctp_tcpephash[SCTP_PCBHASH_ALLADDR((lport + rport),
2005 sctppcbinfo.hashtcpmark)];
2007 LIST_INSERT_HEAD(head, new_inp, sctp_hash);
2008 /* Its safe to access */
2009 new_inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
2011 /* Now move the tcb into the endpoint list */
2012 LIST_INSERT_HEAD(&new_inp->sctp_asoc_list, stcb, sctp_tcblist);
2014 * Question, do we even need to worry about the ep-hash since we
2015 * only have one connection? Probably not :> so lets get rid of it
2016 * and not suck up any kernel memory in that.
2019 /* Ok. Let's restart timer. */
2020 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2021 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, new_inp,
2025 SCTP_INP_INFO_WUNLOCK();
2026 if (new_inp->sctp_tcbhash != NULL) {
2027 SCTP_HASH_FREE(new_inp->sctp_tcbhash, new_inp->sctp_hashmark);
2028 new_inp->sctp_tcbhash = NULL;
2030 if ((new_inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
2031 /* Subset bound, so copy in the laddr list from the old_inp */
2032 LIST_FOREACH(oladdr, &old_inp->sctp_addr_list, sctp_nxt_addr) {
2033 laddr = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_laddr, struct sctp_laddr);
2034 if (laddr == NULL) {
2036 * Gak, what can we do? This assoc is really
2037 * HOSED. We probably should send an abort
2040 SCTPDBG(SCTP_DEBUG_PCB1, "Association hosed in TCP model, out of laddr memory\n");
2043 SCTP_INCR_LADDR_COUNT();
2044 bzero(laddr, sizeof(*laddr));
2045 (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time);
2046 laddr->ifa = oladdr->ifa;
2047 atomic_add_int(&laddr->ifa->refcount, 1);
2048 LIST_INSERT_HEAD(&new_inp->sctp_addr_list, laddr,
2050 new_inp->laddr_count++;
2054 * Now any running timers need to be adjusted since we really don't
2055 * care if they are running or not just blast in the new_inp into
2059 stcb->asoc.hb_timer.ep = (void *)new_inp;
2060 stcb->asoc.dack_timer.ep = (void *)new_inp;
2061 stcb->asoc.asconf_timer.ep = (void *)new_inp;
2062 stcb->asoc.strreset_timer.ep = (void *)new_inp;
2063 stcb->asoc.shut_guard_timer.ep = (void *)new_inp;
2064 stcb->asoc.autoclose_timer.ep = (void *)new_inp;
2065 stcb->asoc.delayed_event_timer.ep = (void *)new_inp;
2066 /* now what about the nets? */
2067 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2068 net->pmtu_timer.ep = (void *)new_inp;
2069 net->rxt_timer.ep = (void *)new_inp;
2070 net->fr_timer.ep = (void *)new_inp;
2072 SCTP_INP_WUNLOCK(new_inp);
2073 SCTP_INP_WUNLOCK(old_inp);
2077 sctp_isport_inuse(struct sctp_inpcb *inp, uint16_t lport, uint32_t vrf_id)
2079 struct sctppcbhead *head;
2080 struct sctp_inpcb *t_inp;
2083 head = &sctppcbinfo.sctp_ephash[SCTP_PCBHASH_ALLADDR(lport,
2084 sctppcbinfo.hashmark)];
2085 LIST_FOREACH(t_inp, head, sctp_hash) {
2086 if (t_inp->sctp_lport != lport) {
2089 /* is it in the VRF in question */
2091 if (t_inp->def_vrf_id == vrf_id)
2096 /* This one is in use. */
2097 /* check the v6/v4 binding issue */
2098 if ((t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
2099 SCTP_IPV6_V6ONLY(t_inp)) {
2100 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2101 /* collision in V6 space */
2104 /* inp is BOUND_V4 no conflict */
2107 } else if (t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2108 /* t_inp is bound v4 and v6, conflict always */
2111 /* t_inp is bound only V4 */
2112 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
2113 SCTP_IPV6_V6ONLY(inp)) {
2117 /* else fall through to conflict */
2126 /* sctp_ifap is used to bypass normal local address validation checks */
2128 sctp_inpcb_bind(struct socket *so, struct sockaddr *addr,
2129 struct sctp_ifa *sctp_ifap, struct thread *p)
2131 /* bind a ep to a socket address */
2132 struct sctppcbhead *head;
2133 struct sctp_inpcb *inp, *inp_tmp;
2134 struct inpcb *ip_inp;
2144 inp = (struct sctp_inpcb *)so->so_pcb;
2145 ip_inp = (struct inpcb *)so->so_pcb;
2148 SCTPDBG(SCTP_DEBUG_PCB1, "Bind called port:%d\n",
2149 ntohs(((struct sockaddr_in *)addr)->sin_port));
2150 SCTPDBG(SCTP_DEBUG_PCB1, "Addr :");
2151 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr);
2154 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
2155 /* already did a bind, subsequent binds NOT allowed ! */
2160 panic("null proc/thread");
2162 if (p && jailed(p->td_ucred)) {
2166 if (addr->sa_family == AF_INET) {
2167 struct sockaddr_in *sin;
2169 /* IPV6_V6ONLY socket? */
2170 if (SCTP_IPV6_V6ONLY(ip_inp)) {
2173 if (addr->sa_len != sizeof(*sin))
2176 sin = (struct sockaddr_in *)addr;
2177 lport = sin->sin_port;
2180 * For INADDR_ANY and LOOPBACK the
2181 * prison_ip() call will tranmute the ip
2182 * address to the proper valie.
2184 if (prison_ip(p->td_ucred, 0, &sin->sin_addr.s_addr))
2187 if (sin->sin_addr.s_addr != INADDR_ANY) {
2190 } else if (addr->sa_family == AF_INET6) {
2191 /* Only for pure IPv6 Address. (No IPv4 Mapped!) */
2192 struct sockaddr_in6 *sin6;
2194 sin6 = (struct sockaddr_in6 *)addr;
2196 if (addr->sa_len != sizeof(*sin6))
2199 lport = sin6->sin6_port;
2201 * Jail checks for IPv6 should go HERE! i.e. add the
2202 * prison_ip() equivilant in this postion to
2203 * transmute the addresses to the proper one jailed.
2205 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2207 /* KAME hack: embed scopeid */
2208 if (sa6_embedscope(sin6, ip6_use_defzone) != 0)
2211 /* this must be cleared for ifa_ifwithaddr() */
2212 sin6->sin6_scope_id = 0;
2214 return (EAFNOSUPPORT);
2217 /* Setup a vrf_id to be the default for the non-bind-all case. */
2218 vrf_id = inp->def_vrf_id;
2220 SCTP_INP_INFO_WLOCK();
2221 SCTP_INP_WLOCK(inp);
2222 /* increase our count due to the unlock we do */
2223 SCTP_INP_INCR_REF(inp);
2226 * Did the caller specify a port? if so we must see if a ep
2227 * already has this one bound.
2229 /* got to be root to get at low ports */
2230 if (ntohs(lport) < IPPORT_RESERVED) {
2232 priv_check(p, PRIV_NETINET_RESERVEDPORT)
2234 SCTP_INP_DECR_REF(inp);
2235 SCTP_INP_WUNLOCK(inp);
2236 SCTP_INP_INFO_WUNLOCK();
2241 SCTP_INP_DECR_REF(inp);
2242 SCTP_INP_WUNLOCK(inp);
2243 SCTP_INP_INFO_WUNLOCK();
2246 SCTP_INP_WUNLOCK(inp);
2248 vrf_id = inp->def_vrf_id;
2249 inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id);
2250 if (inp_tmp != NULL) {
2252 * lock guy returned and lower count note
2253 * that we are not bound so inp_tmp should
2254 * NEVER be inp. And it is this inp
2255 * (inp_tmp) that gets the reference bump,
2256 * so we must lower it.
2258 SCTP_INP_DECR_REF(inp_tmp);
2259 SCTP_INP_DECR_REF(inp);
2261 SCTP_INP_INFO_WUNLOCK();
2262 return (EADDRINUSE);
2265 inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id);
2266 if (inp_tmp != NULL) {
2268 * lock guy returned and lower count note
2269 * that we are not bound so inp_tmp should
2270 * NEVER be inp. And it is this inp
2271 * (inp_tmp) that gets the reference bump,
2272 * so we must lower it.
2274 SCTP_INP_DECR_REF(inp_tmp);
2275 SCTP_INP_DECR_REF(inp);
2277 SCTP_INP_INFO_WUNLOCK();
2278 return (EADDRINUSE);
2281 SCTP_INP_WLOCK(inp);
2283 /* verify that no lport is not used by a singleton */
2284 if (sctp_isport_inuse(inp, lport, vrf_id)) {
2285 /* Sorry someone already has this one bound */
2286 SCTP_INP_DECR_REF(inp);
2287 SCTP_INP_WUNLOCK(inp);
2288 SCTP_INP_INFO_WUNLOCK();
2289 return (EADDRINUSE);
2293 uint16_t first, last, candidate;
2297 if (ip_inp->inp_flags & INP_HIGHPORT) {
2298 first = ipport_hifirstauto;
2299 last = ipport_hilastauto;
2300 } else if (ip_inp->inp_flags & INP_LOWPORT) {
2302 priv_check(p, PRIV_NETINET_RESERVEDPORT)
2304 SCTP_INP_DECR_REF(inp);
2305 SCTP_INP_WUNLOCK(inp);
2306 SCTP_INP_INFO_WUNLOCK();
2309 first = ipport_lowfirstauto;
2310 last = ipport_lowlastauto;
2312 first = ipport_firstauto;
2313 last = ipport_lastauto;
2322 count = last - first + 1; /* number of candidates */
2323 candidate = first + sctp_select_initial_TSN(&inp->sctp_ep) % (count);
2327 if (sctp_isport_inuse(inp, htons(candidate), inp->def_vrf_id) == 0) {
2332 SCTP_INP_DECR_REF(inp);
2333 SCTP_INP_WUNLOCK(inp);
2334 SCTP_INP_INFO_WUNLOCK();
2335 return (EADDRINUSE);
2337 if (candidate == last)
2340 candidate = candidate + 1;
2343 lport = htons(candidate);
2345 SCTP_INP_DECR_REF(inp);
2346 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE |
2347 SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
2349 * this really should not happen. The guy did a non-blocking
2350 * bind and then did a close at the same time.
2352 SCTP_INP_WUNLOCK(inp);
2353 SCTP_INP_INFO_WUNLOCK();
2356 /* ok we look clear to give out this port, so lets setup the binding */
2358 /* binding to all addresses, so just set in the proper flags */
2359 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUNDALL;
2360 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
2361 /* set the automatic addr changes from kernel flag */
2362 if (sctp_auto_asconf == 0) {
2363 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
2365 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
2369 * bind specific, make sure flags is off and add a new
2370 * address structure to the sctp_addr_list inside the ep
2373 * We will need to allocate one and insert it at the head. The
2374 * socketopt call can just insert new addresses in there as
2375 * well. It will also have to do the embed scope kame hack
2376 * too (before adding).
2378 struct sctp_ifa *ifa;
2379 struct sockaddr_storage store_sa;
2381 memset(&store_sa, 0, sizeof(store_sa));
2382 if (addr->sa_family == AF_INET) {
2383 struct sockaddr_in *sin;
2385 sin = (struct sockaddr_in *)&store_sa;
2386 memcpy(sin, addr, sizeof(struct sockaddr_in));
2388 } else if (addr->sa_family == AF_INET6) {
2389 struct sockaddr_in6 *sin6;
2391 sin6 = (struct sockaddr_in6 *)&store_sa;
2392 memcpy(sin6, addr, sizeof(struct sockaddr_in6));
2393 sin6->sin6_port = 0;
2396 * first find the interface with the bound address need to
2397 * zero out the port to find the address! yuck! can't do
2398 * this earlier since need port for sctp_pcb_findep()
2400 if (sctp_ifap != NULL)
2404 * Note for BSD we hit here always other O/S's will
2405 * pass things in via the sctp_ifap argument
2408 ifa = sctp_find_ifa_by_addr((struct sockaddr *)&store_sa,
2412 /* Can't find an interface with that address */
2413 SCTP_INP_WUNLOCK(inp);
2414 SCTP_INP_INFO_WUNLOCK();
2415 return (EADDRNOTAVAIL);
2417 if (addr->sa_family == AF_INET6) {
2418 /* GAK, more FIXME IFA lock? */
2419 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2420 /* Can't bind a non-existent addr. */
2421 SCTP_INP_WUNLOCK(inp);
2422 SCTP_INP_INFO_WUNLOCK();
2426 /* we're not bound all */
2427 inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUNDALL;
2428 /* allow bindx() to send ASCONF's for binding changes */
2429 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
2430 /* clear automatic addr changes from kernel flag */
2431 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
2433 /* add this address to the endpoint list */
2434 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, 0);
2436 SCTP_INP_WUNLOCK(inp);
2437 SCTP_INP_INFO_WUNLOCK();
2442 /* find the bucket */
2443 head = &sctppcbinfo.sctp_ephash[SCTP_PCBHASH_ALLADDR(lport,
2444 sctppcbinfo.hashmark)];
2445 /* put it in the bucket */
2446 LIST_INSERT_HEAD(head, inp, sctp_hash);
2447 SCTPDBG(SCTP_DEBUG_PCB1, "Main hash to bind at head:%p, bound port:%d\n",
2448 head, ntohs(lport));
2449 /* set in the port */
2450 inp->sctp_lport = lport;
2452 /* turn off just the unbound flag */
2453 inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
2454 SCTP_INP_WUNLOCK(inp);
2455 SCTP_INP_INFO_WUNLOCK();
2461 sctp_iterator_inp_being_freed(struct sctp_inpcb *inp, struct sctp_inpcb *inp_next)
2463 struct sctp_iterator *it;
2466 * We enter with the only the ITERATOR_LOCK in place and a write
2467 * lock on the inp_info stuff.
2471 * Go through all iterators, we must do this since it is possible
2472 * that some iterator does NOT have the lock, but is waiting for it.
2473 * And the one that had the lock has either moved in the last
2474 * iteration or we just cleared it above. We need to find all of
2475 * those guys. The list of iterators should never be very big
2478 TAILQ_FOREACH(it, &sctppcbinfo.iteratorhead, sctp_nxt_itr) {
2479 if (it == inp->inp_starting_point_for_iterator)
2480 /* skip this guy, he's special */
2482 if (it->inp == inp) {
2484 * This is tricky and we DON'T lock the iterator.
2485 * Reason is he's running but waiting for me since
2486 * inp->inp_starting_point_for_iterator has the lock
2487 * on me (the guy above we skipped). This tells us
2488 * its is not running but waiting for
2489 * inp->inp_starting_point_for_iterator to be
2490 * released by the guy that does have our INP in a
2493 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
2497 /* set him up to do the next guy not me */
2503 it = inp->inp_starting_point_for_iterator;
2505 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
2514 /* release sctp_inpcb unbind the port */
2516 sctp_inpcb_free(struct sctp_inpcb *inp, int immediate, int from)
2519 * Here we free a endpoint. We must find it (if it is in the Hash
2520 * table) and remove it from there. Then we must also find it in the
2521 * overall list and remove it from there. After all removals are
2522 * complete then any timer has to be stopped. Then start the actual
2523 * freeing. a) Any local lists. b) Any associations. c) The hash of
2524 * all associations. d) finally the ep itself.
2527 struct sctp_inpcb *inp_save;
2528 struct sctp_tcb *asoc, *nasoc;
2529 struct sctp_laddr *laddr, *nladdr;
2530 struct inpcb *ip_pcb;
2533 struct sctp_queued_to_read *sq;
2537 sctp_sharedkey_t *shared_key;
2540 #ifdef SCTP_LOG_CLOSING
2541 sctp_log_closing(inp, NULL, 0);
2544 SCTP_ITERATOR_LOCK();
2545 so = inp->sctp_socket;
2546 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
2547 /* been here before.. eeks.. get out of here */
2548 SCTP_PRINTF("This conflict in free SHOULD not be happening! from %d, imm %d\n", from, immediate);
2549 SCTP_ITERATOR_UNLOCK();
2550 #ifdef SCTP_LOG_CLOSING
2551 sctp_log_closing(inp, NULL, 1);
2555 SCTP_ASOC_CREATE_LOCK(inp);
2556 SCTP_INP_INFO_WLOCK();
2558 SCTP_INP_WLOCK(inp);
2559 /* First time through we have the socket lock, after that no more. */
2560 if (from == SCTP_CALLED_AFTER_CMPSET_OFCLOSE) {
2562 * Once we are in we can remove the flag from = 1 is only
2563 * passed from the actual closing routines that are called
2564 * via the sockets layer.
2566 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CLOSE_IP;
2568 sctp_timer_stop(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL,
2569 SCTP_FROM_SCTP_PCB + SCTP_LOC_1);
2572 sctp_m_freem(inp->control);
2573 inp->control = NULL;
2576 sctp_m_freem(inp->pkt);
2580 ip_pcb = &inp->ip_inp.inp; /* we could just cast the main pointer
2581 * here but I will be nice :> (i.e.
2583 if (immediate == SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE) {
2587 for ((asoc = LIST_FIRST(&inp->sctp_asoc_list)); asoc != NULL;
2589 nasoc = LIST_NEXT(asoc, sctp_tcblist);
2590 if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
2591 /* Skip guys being freed */
2592 asoc->sctp_socket = NULL;
2596 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_WAIT) ||
2597 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
2599 * If we have data in queue, we don't want
2600 * to just free since the app may have done,
2601 * send()/close or connect/send/close. And
2602 * it wants the data to get across first.
2604 if (asoc->asoc.total_output_queue_size == 0) {
2606 * Just abandon things in the front
2609 sctp_free_assoc(inp, asoc, SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_2);
2613 SCTP_TCB_LOCK(asoc);
2614 /* Disconnect the socket please */
2615 asoc->sctp_socket = NULL;
2616 asoc->asoc.state |= SCTP_STATE_CLOSED_SOCKET;
2617 if ((asoc->asoc.size_on_reasm_queue > 0) ||
2618 (asoc->asoc.control_pdapi) ||
2619 (asoc->asoc.size_on_all_streams > 0) ||
2620 (so && (so->so_rcv.sb_cc > 0))
2622 /* Left with Data unread */
2623 struct mbuf *op_err;
2625 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
2626 0, M_DONTWAIT, 1, MT_DATA);
2628 /* Fill in the user initiated abort */
2629 struct sctp_paramhdr *ph;
2632 SCTP_BUF_LEN(op_err) =
2633 sizeof(struct sctp_paramhdr) + sizeof(uint32_t);
2635 struct sctp_paramhdr *);
2636 ph->param_type = htons(
2637 SCTP_CAUSE_USER_INITIATED_ABT);
2638 ph->param_length = htons(SCTP_BUF_LEN(op_err));
2639 ippp = (uint32_t *) (ph + 1);
2640 *ippp = htonl(SCTP_FROM_SCTP_PCB + SCTP_LOC_3);
2642 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_3;
2643 sctp_send_abort_tcb(asoc, op_err);
2644 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
2645 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
2646 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
2647 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
2649 sctp_free_assoc(inp, asoc, SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_4);
2651 } else if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
2652 TAILQ_EMPTY(&asoc->asoc.sent_queue) &&
2653 (asoc->asoc.stream_queue_cnt == 0)
2655 if (asoc->asoc.locked_on_sending) {
2658 if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
2659 (SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
2661 * there is nothing queued to send,
2662 * so I send shutdown
2664 sctp_send_shutdown(asoc, asoc->asoc.primary_destination);
2665 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
2666 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
2667 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
2669 asoc->asoc.state = SCTP_STATE_SHUTDOWN_SENT;
2670 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, asoc->sctp_ep, asoc,
2671 asoc->asoc.primary_destination);
2672 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc,
2673 asoc->asoc.primary_destination);
2674 sctp_chunk_output(inp, asoc, SCTP_OUTPUT_FROM_SHUT_TMR);
2677 /* mark into shutdown pending */
2678 struct sctp_stream_queue_pending *sp;
2680 asoc->asoc.state |= SCTP_STATE_SHUTDOWN_PENDING;
2681 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc,
2682 asoc->asoc.primary_destination);
2683 if (asoc->asoc.locked_on_sending) {
2684 sp = TAILQ_LAST(&((asoc->asoc.locked_on_sending)->outqueue),
2687 SCTP_PRINTF("Error, sp is NULL, locked on sending is %p strm:%d\n",
2688 asoc->asoc.locked_on_sending,
2689 asoc->asoc.locked_on_sending->stream_no);
2691 if ((sp->length == 0) && (sp->msg_is_complete == 0))
2692 asoc->asoc.state |= SCTP_STATE_PARTIAL_MSG_LEFT;
2695 if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
2696 TAILQ_EMPTY(&asoc->asoc.sent_queue) &&
2697 (asoc->asoc.state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
2698 struct mbuf *op_err;
2701 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
2702 0, M_DONTWAIT, 1, MT_DATA);
2708 struct sctp_paramhdr *ph;
2711 SCTP_BUF_LEN(op_err) =
2712 (sizeof(struct sctp_paramhdr) +
2715 struct sctp_paramhdr *);
2716 ph->param_type = htons(
2717 SCTP_CAUSE_USER_INITIATED_ABT);
2718 ph->param_length = htons(SCTP_BUF_LEN(op_err));
2719 ippp = (uint32_t *) (ph + 1);
2720 *ippp = htonl(SCTP_FROM_SCTP_PCB + SCTP_LOC_5);
2722 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_5;
2723 sctp_send_abort_tcb(asoc, op_err);
2724 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
2725 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
2726 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
2727 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
2729 sctp_free_assoc(inp, asoc, SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_6);
2734 SCTP_TCB_UNLOCK(asoc);
2736 /* now is there some left in our SHUTDOWN state? */
2738 SCTP_INP_WUNLOCK(inp);
2739 SCTP_ASOC_CREATE_UNLOCK(inp);
2740 SCTP_INP_INFO_WUNLOCK();
2741 SCTP_ITERATOR_UNLOCK();
2742 #ifdef SCTP_LOG_CLOSING
2743 sctp_log_closing(inp, NULL, 2);
2748 inp->sctp_socket = NULL;
2749 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) !=
2750 SCTP_PCB_FLAGS_UNBOUND) {
2752 * ok, this guy has been bound. It's port is somewhere in
2753 * the sctppcbinfo hash table. Remove it!
2755 LIST_REMOVE(inp, sctp_hash);
2756 inp->sctp_flags |= SCTP_PCB_FLAGS_UNBOUND;
2759 * If there is a timer running to kill us, forget it, since it may
2760 * have a contest on the INP lock.. which would cause us to die ...
2763 for ((asoc = LIST_FIRST(&inp->sctp_asoc_list)); asoc != NULL;
2765 nasoc = LIST_NEXT(asoc, sctp_tcblist);
2766 if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
2770 /* Free associations that are NOT killing us */
2771 SCTP_TCB_LOCK(asoc);
2772 if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_COOKIE_WAIT) &&
2773 ((asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0)) {
2774 struct mbuf *op_err;
2777 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
2778 0, M_DONTWAIT, 1, MT_DATA);
2780 /* Fill in the user initiated abort */
2781 struct sctp_paramhdr *ph;
2783 SCTP_BUF_LEN(op_err) = (sizeof(struct sctp_paramhdr) +
2785 ph = mtod(op_err, struct sctp_paramhdr *);
2786 ph->param_type = htons(
2787 SCTP_CAUSE_USER_INITIATED_ABT);
2788 ph->param_length = htons(SCTP_BUF_LEN(op_err));
2789 ippp = (uint32_t *) (ph + 1);
2790 *ippp = htonl(SCTP_FROM_SCTP_PCB + SCTP_LOC_7);
2793 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_7;
2794 sctp_send_abort_tcb(asoc, op_err);
2795 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
2796 } else if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
2798 SCTP_TCB_UNLOCK(asoc);
2801 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
2802 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
2803 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
2805 sctp_free_assoc(inp, asoc, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_8);
2808 /* Ok we have someone out there that will kill us */
2809 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
2810 SCTP_INP_WUNLOCK(inp);
2811 SCTP_ASOC_CREATE_UNLOCK(inp);
2812 SCTP_INP_INFO_WUNLOCK();
2813 SCTP_ITERATOR_UNLOCK();
2814 #ifdef SCTP_LOG_CLOSING
2815 sctp_log_closing(inp, NULL, 3);
2819 if ((inp->refcount) || (inp->sctp_flags & SCTP_PCB_FLAGS_CLOSE_IP)) {
2820 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
2821 sctp_timer_start(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL);
2822 SCTP_INP_WUNLOCK(inp);
2823 SCTP_ASOC_CREATE_UNLOCK(inp);
2824 SCTP_INP_INFO_WUNLOCK();
2825 SCTP_ITERATOR_UNLOCK();
2826 #ifdef SCTP_LOG_CLOSING
2827 sctp_log_closing(inp, NULL, 4);
2831 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
2832 inp->sctp_ep.signature_change.type = 0;
2833 inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_ALLGONE;
2835 #ifdef SCTP_LOG_CLOSING
2836 sctp_log_closing(inp, NULL, 5);
2839 (void)SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
2840 inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NONE;
2841 /* Clear the read queue */
2842 /* sa_ignore FREED_MEMORY */
2843 while ((sq = TAILQ_FIRST(&inp->read_queue)) != NULL) {
2844 /* Its only abandoned if it had data left */
2846 SCTP_STAT_INCR(sctps_left_abandon);
2848 TAILQ_REMOVE(&inp->read_queue, sq, next);
2849 sctp_free_remote_addr(sq->whoFrom);
2851 so->so_rcv.sb_cc -= sq->length;
2853 sctp_m_freem(sq->data);
2857 * no need to free the net count, since at this point all
2860 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_readq, sq);
2861 SCTP_DECR_READQ_COUNT();
2863 /* Now the sctp_pcb things */
2865 * free each asoc if it is not already closed/free. we can't use the
2866 * macro here since le_next will get freed as part of the
2867 * sctp_free_assoc() call.
2872 ipsec4_delete_pcbpolicy(ip_pcb);
2875 /* Unlocks not needed since the socket is gone now */
2877 if (ip_pcb->inp_options) {
2878 (void)sctp_m_free(ip_pcb->inp_options);
2879 ip_pcb->inp_options = 0;
2881 if (ip_pcb->inp_moptions) {
2882 inp_freemoptions(ip_pcb->inp_moptions);
2883 ip_pcb->inp_moptions = 0;
2886 if (ip_pcb->inp_vflag & INP_IPV6) {
2887 struct in6pcb *in6p;
2889 in6p = (struct in6pcb *)inp;
2890 ip6_freepcbopts(in6p->in6p_outputopts);
2893 ip_pcb->inp_vflag = 0;
2894 /* free up authentication fields */
2895 if (inp->sctp_ep.local_auth_chunks != NULL)
2896 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2897 if (inp->sctp_ep.local_hmacs != NULL)
2898 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2900 shared_key = LIST_FIRST(&inp->sctp_ep.shared_keys);
2901 while (shared_key) {
2902 LIST_REMOVE(shared_key, next);
2903 sctp_free_sharedkey(shared_key);
2904 /* sa_ignore FREED_MEMORY */
2905 shared_key = LIST_FIRST(&inp->sctp_ep.shared_keys);
2908 inp_save = LIST_NEXT(inp, sctp_list);
2909 LIST_REMOVE(inp, sctp_list);
2911 /* fix any iterators only after out of the list */
2912 sctp_iterator_inp_being_freed(inp, inp_save);
2914 * if we have an address list the following will free the list of
2915 * ifaddr's that are set into this ep. Again macro limitations here,
2916 * since the LIST_FOREACH could be a bad idea.
2918 for ((laddr = LIST_FIRST(&inp->sctp_addr_list)); laddr != NULL;
2920 nladdr = LIST_NEXT(laddr, sctp_nxt_addr);
2921 sctp_remove_laddr(laddr);
2924 #ifdef SCTP_TRACK_FREED_ASOCS
2926 for ((asoc = LIST_FIRST(&inp->sctp_asoc_free_list)); asoc != NULL;
2928 nasoc = LIST_NEXT(asoc, sctp_tcblist);
2929 LIST_REMOVE(asoc, sctp_tcblist);
2930 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, asoc);
2931 SCTP_DECR_ASOC_COUNT();
2933 /* *** END TEMP CODE *** */
2935 /* Now lets see about freeing the EP hash table. */
2936 if (inp->sctp_tcbhash != NULL) {
2937 SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark);
2938 inp->sctp_tcbhash = NULL;
2940 /* Now we must put the ep memory back into the zone pool */
2941 INP_LOCK_DESTROY(&inp->ip_inp.inp);
2942 SCTP_INP_LOCK_DESTROY(inp);
2943 SCTP_INP_READ_DESTROY(inp);
2944 SCTP_ASOC_CREATE_LOCK_DESTROY(inp);
2945 SCTP_INP_INFO_WUNLOCK();
2947 SCTP_ITERATOR_UNLOCK();
2949 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
2950 SCTP_DECR_EP_COUNT();
2956 sctp_findnet(struct sctp_tcb *stcb, struct sockaddr *addr)
2958 struct sctp_nets *net;
2960 /* locate the address */
2961 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2962 if (sctp_cmpaddr(addr, (struct sockaddr *)&net->ro._l_addr))
2970 sctp_is_address_on_local_host(struct sockaddr *addr, uint32_t vrf_id)
2972 struct sctp_ifa *sctp_ifa;
2974 sctp_ifa = sctp_find_ifa_by_addr(addr, vrf_id, 0);
2983 * add's a remote endpoint address, done with the INIT/INIT-ACK as well as
2984 * when a ASCONF arrives that adds it. It will also initialize all the cwnd
2988 sctp_add_remote_addr(struct sctp_tcb *stcb, struct sockaddr *newaddr,
2989 int set_scope, int from)
2992 * The following is redundant to the same lines in the
2993 * sctp_aloc_assoc() but is needed since other's call the add
2996 struct sctp_nets *net, *netfirst;
2999 SCTPDBG(SCTP_DEBUG_PCB1, "Adding an address (from:%d) to the peer: ",
3001 SCTPDBG_ADDR(SCTP_DEBUG_PCB1, newaddr);
3003 netfirst = sctp_findnet(stcb, newaddr);
3006 * Lie and return ok, we don't want to make the association
3007 * go away for this behavior. It will happen in the TCP
3008 * model in a connected socket. It does not reach the hash
3009 * table until after the association is built so it can't be
3010 * found. Mark as reachable, since the initial creation will
3011 * have been cleared and the NOT_IN_ASSOC flag will have
3012 * been added... and we don't want to end up removing it
3015 if (netfirst->dest_state & SCTP_ADDR_UNCONFIRMED) {
3016 netfirst->dest_state = (SCTP_ADDR_REACHABLE |
3017 SCTP_ADDR_UNCONFIRMED);
3019 netfirst->dest_state = SCTP_ADDR_REACHABLE;
3025 if (newaddr->sa_family == AF_INET) {
3026 struct sockaddr_in *sin;
3028 sin = (struct sockaddr_in *)newaddr;
3029 if (sin->sin_addr.s_addr == 0) {
3030 /* Invalid address */
3033 /* zero out the bzero area */
3034 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
3036 /* assure len is set */
3037 sin->sin_len = sizeof(struct sockaddr_in);
3039 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
3040 stcb->ipv4_local_scope = 1;
3042 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
3043 stcb->asoc.ipv4_local_scope = 1;
3045 #endif /* SCTP_DONT_DO_PRIVADDR_SCOPE */
3047 /* Validate the address is in scope */
3048 if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) &&
3049 (stcb->asoc.ipv4_local_scope == 0)) {
3053 } else if (newaddr->sa_family == AF_INET6) {
3054 struct sockaddr_in6 *sin6;
3056 sin6 = (struct sockaddr_in6 *)newaddr;
3057 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3058 /* Invalid address */
3061 /* assure len is set */
3062 sin6->sin6_len = sizeof(struct sockaddr_in6);
3064 if (sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id)) {
3065 stcb->asoc.loopback_scope = 1;
3066 stcb->asoc.local_scope = 0;
3067 stcb->asoc.ipv4_local_scope = 1;
3068 stcb->asoc.site_scope = 1;
3069 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
3071 * If the new destination is a LINK_LOCAL we
3072 * must have common site scope. Don't set
3073 * the local scope since we may not share
3074 * all links, only loopback can do this.
3075 * Links on the local network would also be
3076 * on our private network for v4 too.
3078 stcb->asoc.ipv4_local_scope = 1;
3079 stcb->asoc.site_scope = 1;
3080 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
3082 * If the new destination is SITE_LOCAL then
3083 * we must have site scope in common.
3085 stcb->asoc.site_scope = 1;
3088 /* Validate the address is in scope */
3089 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) &&
3090 (stcb->asoc.loopback_scope == 0)) {
3092 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) &&
3093 (stcb->asoc.local_scope == 0)) {
3095 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) &&
3096 (stcb->asoc.site_scope == 0)) {
3101 /* not supported family type */
3104 net = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_net, struct sctp_nets);
3108 SCTP_INCR_RADDR_COUNT();
3109 bzero(net, sizeof(*net));
3110 (void)SCTP_GETTIME_TIMEVAL(&net->start_time);
3111 memcpy(&net->ro._l_addr, newaddr, newaddr->sa_len);
3112 if (newaddr->sa_family == AF_INET) {
3113 ((struct sockaddr_in *)&net->ro._l_addr)->sin_port = stcb->rport;
3114 } else if (newaddr->sa_family == AF_INET6) {
3115 ((struct sockaddr_in6 *)&net->ro._l_addr)->sin6_port = stcb->rport;
3117 net->addr_is_local = sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id);
3118 if (net->addr_is_local && ((set_scope || (from == SCTP_ADDR_IS_CONFIRMED)))) {
3119 stcb->asoc.loopback_scope = 1;
3120 stcb->asoc.ipv4_local_scope = 1;
3121 stcb->asoc.local_scope = 0;
3122 stcb->asoc.site_scope = 1;
3125 net->failure_threshold = stcb->asoc.def_net_failure;
3126 if (addr_inscope == 0) {
3127 net->dest_state = (SCTP_ADDR_REACHABLE |
3128 SCTP_ADDR_OUT_OF_SCOPE);
3130 if (from == SCTP_ADDR_IS_CONFIRMED)
3131 /* SCTP_ADDR_IS_CONFIRMED is passed by connect_x */
3132 net->dest_state = SCTP_ADDR_REACHABLE;
3134 net->dest_state = SCTP_ADDR_REACHABLE |
3135 SCTP_ADDR_UNCONFIRMED;
3138 * We set this to 0, the timer code knows that this means its an
3142 net->RTO_measured = 0;
3143 stcb->asoc.numnets++;
3144 *(&net->ref_count) = 1;
3145 net->tos_flowlabel = 0;
3147 if (newaddr->sa_family == AF_INET)
3148 net->tos_flowlabel = stcb->asoc.default_tos;
3151 if (newaddr->sa_family == AF_INET6)
3152 net->tos_flowlabel = stcb->asoc.default_flowlabel;
3154 /* Init the timer structure */
3155 SCTP_OS_TIMER_INIT(&net->rxt_timer.timer);
3156 SCTP_OS_TIMER_INIT(&net->fr_timer.timer);
3157 SCTP_OS_TIMER_INIT(&net->pmtu_timer.timer);
3159 /* Now generate a route for this guy */
3160 /* KAME hack: embed scopeid */
3161 if (newaddr->sa_family == AF_INET6) {
3162 struct sockaddr_in6 *sin6;
3164 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3165 (void)sa6_embedscope(sin6, ip6_use_defzone);
3166 sin6->sin6_scope_id = 0;
3168 SCTP_RTALLOC((sctp_route_t *) & net->ro, stcb->asoc.vrf_id);
3170 if (newaddr->sa_family == AF_INET6) {
3171 struct sockaddr_in6 *sin6;
3173 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3174 (void)sa6_recoverscope(sin6);
3176 if (SCTP_ROUTE_HAS_VALID_IFN(&net->ro)) {
3177 /* Get source address */
3178 net->ro._s_addr = sctp_source_address_selection(stcb->sctp_ep,
3180 (sctp_route_t *) & net->ro,
3184 /* Now get the interface MTU */
3185 if (net->ro._s_addr && net->ro._s_addr->ifn_p) {
3186 net->mtu = SCTP_GATHER_MTU_FROM_INTFC(net->ro._s_addr->ifn_p);
3190 #ifdef SCTP_PRINT_FOR_B_AND_M
3191 SCTP_PRINTF("We have found an interface mtu of %d\n", net->mtu);
3193 if (net->mtu == 0) {
3195 net->mtu = SCTP_DEFAULT_MTU;
3199 rmtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, net->ro.ro_rt);
3200 #ifdef SCTP_PRINT_FOR_B_AND_M
3201 SCTP_PRINTF("The route mtu is %d\n", rmtu);
3205 * Start things off to match mtu of
3208 SCTP_SET_MTU_OF_ROUTE(&net->ro._l_addr.sa,
3209 net->ro.ro_rt, net->mtu);
3212 * we take the route mtu over the interface,
3213 * since the route may be leading out the
3214 * loopback, or a different interface.
3219 if (from == SCTP_ALLOC_ASOC) {
3220 #ifdef SCTP_PRINT_FOR_B_AND_M
3221 SCTP_PRINTF("New assoc sets mtu to :%d\n", net->mtu);
3223 stcb->asoc.smallest_mtu = net->mtu;
3226 net->mtu = stcb->asoc.smallest_mtu;
3228 if (stcb->asoc.smallest_mtu > net->mtu) {
3229 #ifdef SCTP_PRINT_FOR_B_AND_M
3230 SCTP_PRINTF("new address mtu:%d smaller than smallest:%d\n",
3231 net->mtu, stcb->asoc.smallest_mtu);
3233 stcb->asoc.smallest_mtu = net->mtu;
3235 /* JRS - Use the congestion control given in the CC module */
3236 stcb->asoc.cc_functions.sctp_set_initial_cc_param(stcb, net);
3239 * CMT: CUC algo - set find_pseudo_cumack to TRUE (1) at beginning
3240 * of assoc (2005/06/27, iyengar@cis.udel.edu)
3242 net->find_pseudo_cumack = 1;
3243 net->find_rtx_pseudo_cumack = 1;
3244 net->src_addr_selected = 0;
3245 netfirst = TAILQ_FIRST(&stcb->asoc.nets);
3246 if (net->ro.ro_rt == NULL) {
3247 /* Since we have no route put it at the back */
3248 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
3249 } else if (netfirst == NULL) {
3250 /* We are the first one in the pool. */
3251 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
3252 } else if (netfirst->ro.ro_rt == NULL) {
3254 * First one has NO route. Place this one ahead of the first
3257 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
3258 } else if (net->ro.ro_rt->rt_ifp != netfirst->ro.ro_rt->rt_ifp) {
3260 * This one has a different interface than the one at the
3261 * top of the list. Place it ahead.
3263 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
3266 * Ok we have the same interface as the first one. Move
3267 * forward until we find either a) one with a NULL route...
3268 * insert ahead of that b) one with a different ifp.. insert
3269 * after that. c) end of the list.. insert at the tail.
3271 struct sctp_nets *netlook;
3274 netlook = TAILQ_NEXT(netfirst, sctp_next);
3275 if (netlook == NULL) {
3276 /* End of the list */
3277 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
3279 } else if (netlook->ro.ro_rt == NULL) {
3280 /* next one has NO route */
3281 TAILQ_INSERT_BEFORE(netfirst, net, sctp_next);
3283 } else if (netlook->ro.ro_rt->rt_ifp != net->ro.ro_rt->rt_ifp) {
3284 TAILQ_INSERT_AFTER(&stcb->asoc.nets, netlook,
3290 } while (netlook != NULL);
3293 /* got to have a primary set */
3294 if (stcb->asoc.primary_destination == 0) {
3295 stcb->asoc.primary_destination = net;
3296 } else if ((stcb->asoc.primary_destination->ro.ro_rt == NULL) &&
3298 ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) {
3299 /* No route to current primary adopt new primary */
3300 stcb->asoc.primary_destination = net;
3302 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb,
3304 /* Validate primary is first */
3305 net = TAILQ_FIRST(&stcb->asoc.nets);
3306 if ((net != stcb->asoc.primary_destination) &&
3307 (stcb->asoc.primary_destination)) {
3309 * first one on the list is NOT the primary sctp_cmpaddr()
3310 * is much more efficent if the primary is the first on the
3313 TAILQ_REMOVE(&stcb->asoc.nets,
3314 stcb->asoc.primary_destination, sctp_next);
3315 TAILQ_INSERT_HEAD(&stcb->asoc.nets,
3316 stcb->asoc.primary_destination, sctp_next);
3323 * allocate an association and add it to the endpoint. The caller must be
3324 * careful to add all additional addresses once they are know right away or
3325 * else the assoc will be may experience a blackout scenario.
3328 sctp_aloc_assoc(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
3329 int for_a_init, int *error, uint32_t override_tag, uint32_t vrf_id,
3333 /* note the p argument is only valid in unbound sockets */
3335 struct sctp_tcb *stcb;
3336 struct sctp_association *asoc;
3337 struct sctpasochead *head;
3342 * Assumption made here: Caller has done a
3343 * sctp_findassociation_ep_addr(ep, addr's); to make sure the
3344 * address does not exist already.
3346 if (sctppcbinfo.ipi_count_asoc >= SCTP_MAX_NUM_OF_ASOC) {
3347 /* Hit max assoc, sorry no more */
3351 if (firstaddr == NULL) {
3355 SCTP_INP_RLOCK(inp);
3356 if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
3358 * If its in the TCP pool, its NOT allowed to create an
3359 * association. The parent listener needs to call
3360 * sctp_aloc_assoc.. or the one-2-many socket. If a peeled
3361 * off, or connected one does this.. its an error.
3363 SCTP_INP_RUNLOCK(inp);
3367 SCTPDBG(SCTP_DEBUG_PCB3, "Allocate an association for peer:");
3370 SCTPDBG_ADDR(SCTP_DEBUG_PCB3, firstaddr);
3371 SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
3372 ntohs(((struct sockaddr_in *)firstaddr)->sin_port));
3374 SCTPDBG(SCTP_DEBUG_PCB3, "None\n");
3376 #endif /* SCTP_DEBUG */
3377 if (firstaddr->sa_family == AF_INET) {
3378 struct sockaddr_in *sin;
3380 sin = (struct sockaddr_in *)firstaddr;
3381 if ((sin->sin_port == 0) || (sin->sin_addr.s_addr == 0)) {
3382 /* Invalid address */
3383 SCTP_INP_RUNLOCK(inp);
3387 rport = sin->sin_port;
3388 } else if (firstaddr->sa_family == AF_INET6) {
3389 struct sockaddr_in6 *sin6;
3391 sin6 = (struct sockaddr_in6 *)firstaddr;
3392 if ((sin6->sin6_port == 0) ||
3393 (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))) {
3394 /* Invalid address */
3395 SCTP_INP_RUNLOCK(inp);
3399 rport = sin6->sin6_port;
3401 /* not supported family type */
3402 SCTP_INP_RUNLOCK(inp);
3406 SCTP_INP_RUNLOCK(inp);
3407 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
3409 * If you have not performed a bind, then we need to do the
3410 * ephemerial bind for you.
3412 if ((err = sctp_inpcb_bind(inp->sctp_socket,
3413 (struct sockaddr *)NULL,
3414 (struct sctp_ifa *)NULL,
3417 /* bind error, probably perm */
3422 stcb = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_asoc, struct sctp_tcb);
3424 /* out of memory? */
3428 SCTP_INCR_ASOC_COUNT();
3430 bzero(stcb, sizeof(*stcb));
3432 SCTP_TCB_LOCK_INIT(stcb);
3433 SCTP_TCB_SEND_LOCK_INIT(stcb);
3434 /* setup back pointer's */
3435 stcb->sctp_ep = inp;
3436 stcb->sctp_socket = inp->sctp_socket;
3437 if ((err = sctp_init_asoc(inp, stcb, for_a_init, override_tag, vrf_id))) {
3439 SCTP_TCB_LOCK_DESTROY(stcb);
3440 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
3441 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
3442 SCTP_DECR_ASOC_COUNT();
3447 stcb->rport = rport;
3448 SCTP_INP_INFO_WLOCK();
3449 SCTP_INP_WLOCK(inp);
3450 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
3451 /* inpcb freed while alloc going on */
3452 SCTP_TCB_LOCK_DESTROY(stcb);
3453 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
3454 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
3455 SCTP_INP_WUNLOCK(inp);
3456 SCTP_INP_INFO_WUNLOCK();
3457 SCTP_DECR_ASOC_COUNT();
3461 SCTP_TCB_LOCK(stcb);
3463 /* now that my_vtag is set, add it to the hash */
3464 head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
3465 sctppcbinfo.hashasocmark)];
3466 /* put it in the bucket in the vtag hash of assoc's for the system */
3467 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
3468 SCTP_INP_INFO_WUNLOCK();
3470 if ((err = sctp_add_remote_addr(stcb, firstaddr, SCTP_DO_SETSCOPE, SCTP_ALLOC_ASOC))) {
3471 /* failure.. memory error? */
3472 if (asoc->strmout) {
3473 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
3474 asoc->strmout = NULL;
3476 if (asoc->mapping_array) {
3477 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
3478 asoc->mapping_array = NULL;
3480 SCTP_DECR_ASOC_COUNT();
3481 SCTP_TCB_LOCK_DESTROY(stcb);
3482 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
3483 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
3484 SCTP_INP_WUNLOCK(inp);
3488 /* Init all the timers */
3489 SCTP_OS_TIMER_INIT(&asoc->hb_timer.timer);
3490 SCTP_OS_TIMER_INIT(&asoc->dack_timer.timer);
3491 SCTP_OS_TIMER_INIT(&asoc->strreset_timer.timer);
3492 SCTP_OS_TIMER_INIT(&asoc->asconf_timer.timer);
3493 SCTP_OS_TIMER_INIT(&asoc->shut_guard_timer.timer);
3494 SCTP_OS_TIMER_INIT(&asoc->autoclose_timer.timer);
3495 SCTP_OS_TIMER_INIT(&asoc->delayed_event_timer.timer);
3497 LIST_INSERT_HEAD(&inp->sctp_asoc_list, stcb, sctp_tcblist);
3498 /* now file the port under the hash as well */
3499 if (inp->sctp_tcbhash != NULL) {
3500 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(stcb->rport,
3501 inp->sctp_hashmark)];
3502 LIST_INSERT_HEAD(head, stcb, sctp_tcbhash);
3504 SCTP_INP_WUNLOCK(inp);
3505 SCTPDBG(SCTP_DEBUG_PCB1, "Association %p now allocated\n", stcb);
3511 sctp_remove_net(struct sctp_tcb *stcb, struct sctp_nets *net)
3513 struct sctp_association *asoc;
3517 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
3518 if (net == asoc->primary_destination) {
3520 struct sctp_nets *lnet;
3522 lnet = TAILQ_FIRST(&asoc->nets);
3523 /* Try to find a confirmed primary */
3524 asoc->primary_destination = sctp_find_alternate_net(stcb, lnet, 0);
3526 if (net == asoc->last_data_chunk_from) {
3528 asoc->last_data_chunk_from = TAILQ_FIRST(&asoc->nets);
3530 if (net == asoc->last_control_chunk_from) {
3532 asoc->last_control_chunk_from = NULL;
3534 sctp_free_remote_addr(net);
3538 * remove a remote endpoint address from an association, it will fail if the
3539 * address does not exist.
3542 sctp_del_remote_addr(struct sctp_tcb *stcb, struct sockaddr *remaddr)
3545 * Here we need to remove a remote address. This is quite simple, we
3546 * first find it in the list of address for the association
3547 * (tasoc->asoc.nets) and then if it is there, we do a LIST_REMOVE
3548 * on that item. Note we do not allow it to be removed if there are
3549 * no other addresses.
3551 struct sctp_association *asoc;
3552 struct sctp_nets *net, *net_tmp;
3556 /* locate the address */
3557 for (net = TAILQ_FIRST(&asoc->nets); net != NULL; net = net_tmp) {
3558 net_tmp = TAILQ_NEXT(net, sctp_next);
3559 if (net->ro._l_addr.sa.sa_family != remaddr->sa_family) {
3562 if (sctp_cmpaddr((struct sockaddr *)&net->ro._l_addr,
3564 /* we found the guy */
3565 if (asoc->numnets < 2) {
3566 /* Must have at LEAST two remote addresses */
3569 sctp_remove_net(stcb, net);
3580 sctp_add_vtag_to_timewait(struct sctp_inpcb *inp, uint32_t tag, uint32_t time)
3582 struct sctpvtaghead *chain;
3583 struct sctp_tagblock *twait_block;
3587 (void)SCTP_GETTIME_TIMEVAL(&now);
3588 chain = &sctppcbinfo.vtag_timewait[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
3590 if (!SCTP_LIST_EMPTY(chain)) {
3591 /* Block(s) present, lets find space, and expire on the fly */
3592 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
3593 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
3594 if ((twait_block->vtag_block[i].v_tag == 0) &&
3596 twait_block->vtag_block[i].tv_sec_at_expire =
3598 twait_block->vtag_block[i].v_tag = tag;
3600 } else if ((twait_block->vtag_block[i].v_tag) &&
3601 ((long)twait_block->vtag_block[i].tv_sec_at_expire >
3603 /* Audit expires this guy */
3604 twait_block->vtag_block[i].tv_sec_at_expire = 0;
3605 twait_block->vtag_block[i].v_tag = 0;
3607 /* Reuse it for my new tag */
3608 twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec + SCTP_TIME_WAIT;
3609 twait_block->vtag_block[0].v_tag = tag;
3616 * We only do up to the block where we can
3617 * place our tag for audits
3623 /* Need to add a new block to chain */
3625 SCTP_MALLOC(twait_block, struct sctp_tagblock *,
3626 sizeof(struct sctp_tagblock), SCTP_M_TIMW);
3627 if (twait_block == NULL) {
3630 memset(twait_block, 0, sizeof(struct sctp_tagblock));
3631 LIST_INSERT_HEAD(chain, twait_block, sctp_nxt_tagblock);
3632 twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec +
3634 twait_block->vtag_block[0].v_tag = tag;
3640 sctp_iterator_asoc_being_freed(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
3642 struct sctp_iterator *it;
3645 * Unlock the tcb lock we do this so we avoid a dead lock scenario
3646 * where the iterator is waiting on the TCB lock and the TCB lock is
3647 * waiting on the iterator lock.
3649 it = stcb->asoc.stcb_starting_point_for_iterator;
3653 if (it->inp != stcb->sctp_ep) {
3654 /* hmm, focused on the wrong one? */
3657 if (it->stcb != stcb) {
3660 it->stcb = LIST_NEXT(stcb, sctp_tcblist);
3661 if (it->stcb == NULL) {
3662 /* done with all asoc's in this assoc */
3663 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
3666 it->inp = LIST_NEXT(inp, sctp_list);
3673 * Free the association after un-hashing the remote port.
3676 sctp_free_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int from_inpcbfree, int from_location)
3679 struct sctp_association *asoc;
3680 struct sctp_nets *net, *prev;
3681 struct sctp_laddr *laddr;
3682 struct sctp_tmit_chunk *chk;
3683 struct sctp_asconf_addr *aparam;
3684 struct sctp_stream_reset_list *liste;
3685 struct sctp_queued_to_read *sq;
3686 struct sctp_stream_queue_pending *sp;
3687 sctp_sharedkey_t *shared_key;
3692 /* first, lets purge the entry from the hash table. */
3694 #ifdef SCTP_LOG_CLOSING
3695 sctp_log_closing(inp, stcb, 6);
3697 if (stcb->asoc.state == 0) {
3698 #ifdef SCTP_LOG_CLOSING
3699 sctp_log_closing(inp, NULL, 7);
3701 /* there is no asoc, really TSNH :-0 */
3705 if (stcb->freed_from_where == 0) {
3706 /* Only record the first place free happened from */
3707 stcb->freed_from_where = from_location;
3712 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3713 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
3714 /* nothing around */
3717 so = inp->sctp_socket;
3720 * We used timer based freeing if a reader or writer is in the way.
3721 * So we first check if we are actually being called from a timer,
3722 * if so we abort early if a reader or writer is still in the way.
3724 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) &&
3725 (from_inpcbfree == SCTP_NORMAL_PROC)) {
3727 * is it the timer driving us? if so are the reader/writers
3730 if (stcb->asoc.refcnt) {
3731 /* nope, reader or writer in the way */
3732 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
3733 /* no asoc destroyed */
3734 SCTP_TCB_UNLOCK(stcb);
3735 #ifdef SCTP_LOG_CLOSING
3736 sctp_log_closing(inp, stcb, 8);
3741 /* now clean up any other timers */
3742 (void)SCTP_OS_TIMER_STOP(&asoc->hb_timer.timer);
3743 asoc->hb_timer.self = NULL;
3744 (void)SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
3745 asoc->dack_timer.self = NULL;
3746 (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
3748 * For stream reset we don't blast this unless
3749 * it is a str-reset timer, it might be the
3750 * free-asoc timer which we DON'T want to
3753 if (asoc->strreset_timer.type == SCTP_TIMER_TYPE_STRRESET)
3754 asoc->strreset_timer.self = NULL;
3755 (void)SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
3756 asoc->asconf_timer.self = NULL;
3757 (void)SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
3758 asoc->autoclose_timer.self = NULL;
3759 (void)SCTP_OS_TIMER_STOP(&asoc->shut_guard_timer.timer);
3760 asoc->shut_guard_timer.self = NULL;
3761 (void)SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
3762 asoc->delayed_event_timer.self = NULL;
3763 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3764 (void)SCTP_OS_TIMER_STOP(&net->fr_timer.timer);
3765 net->fr_timer.self = NULL;
3766 (void)SCTP_OS_TIMER_STOP(&net->rxt_timer.timer);
3767 net->rxt_timer.self = NULL;
3768 (void)SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
3769 net->pmtu_timer.self = NULL;
3771 /* Now the read queue needs to be cleaned up (only once) */
3773 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0) {
3774 stcb->asoc.state |= SCTP_STATE_ABOUT_TO_BE_FREED;
3775 SCTP_INP_READ_LOCK(inp);
3776 TAILQ_FOREACH(sq, &inp->read_queue, next) {
3777 if (sq->stcb == stcb) {
3778 sq->do_not_ref_stcb = 1;
3779 sq->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
3781 * If there is no end, there never will be
3784 if (sq->end_added == 0) {
3785 /* Held for PD-API clear that. */
3786 sq->pdapi_aborted = 1;
3787 sq->held_length = 0;
3788 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT)) {
3790 * Need to add a PD-API
3791 * aborted indication.
3792 * Setting the control_pdapi
3793 * assures that it will be
3794 * added right after this
3799 stcb->asoc.control_pdapi = sq;
3800 strseq = (sq->sinfo_stream << 16) | sq->sinfo_ssn;
3801 sctp_notify_partial_delivery_indication(stcb,
3802 SCTP_PARTIAL_DELIVERY_ABORTED, 1, strseq);
3803 stcb->asoc.control_pdapi = NULL;
3806 /* Add an end to wake them */
3811 SCTP_INP_READ_UNLOCK(inp);
3812 if (stcb->block_entry) {
3814 stcb->block_entry->error = ECONNRESET;
3815 stcb->block_entry = NULL;
3818 if ((from_inpcbfree != SCTP_PCBFREE_FORCE) && (stcb->asoc.refcnt)) {
3820 * reader or writer in the way, we have hopefully given him
3821 * something to chew on above.
3823 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
3824 SCTP_TCB_UNLOCK(stcb);
3825 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3826 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
3827 /* nothing around */
3830 /* Wake any reader/writers */
3831 sctp_sorwakeup(inp, so);
3832 sctp_sowwakeup(inp, so);
3834 #ifdef SCTP_LOG_CLOSING
3835 sctp_log_closing(inp, stcb, 9);
3837 /* no asoc destroyed */
3840 #ifdef SCTP_LOG_CLOSING
3841 sctp_log_closing(inp, stcb, 10);
3844 * When I reach here, no others want to kill the assoc yet.. and I
3845 * own the lock. Now its possible an abort comes in when I do the
3846 * lock exchange below to grab all the locks to do the final take
3847 * out. to prevent this we increment the count, which will start a
3848 * timer and blow out above thus assuring us that we hold exclusive
3849 * killing of the asoc. Note that after getting back the TCB lock we
3850 * will go ahead and increment the counter back up and stop any
3851 * timer a passing stranger may have started :-S
3853 if (from_inpcbfree == SCTP_NORMAL_PROC) {
3854 atomic_add_int(&stcb->asoc.refcnt, 1);
3856 SCTP_TCB_UNLOCK(stcb);
3858 SCTP_ITERATOR_LOCK();
3859 SCTP_INP_INFO_WLOCK();
3860 SCTP_INP_WLOCK(inp);
3861 SCTP_TCB_LOCK(stcb);
3863 /* Double check the GONE flag */
3864 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3865 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
3866 /* nothing around */
3869 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3870 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3872 * For TCP type we need special handling when we are
3873 * connected. We also include the peel'ed off ones to.
3875 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3876 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
3877 inp->sctp_flags |= SCTP_PCB_FLAGS_WAS_CONNECTED;
3880 if (so->so_rcv.sb_cc == 0) {
3881 so->so_state &= ~(SS_ISCONNECTING |
3882 SS_ISDISCONNECTING |
3887 sctp_sowwakeup(inp, so);
3888 sctp_sorwakeup(inp, so);
3894 * Make it invalid too, that way if its about to run it will abort
3897 sctp_iterator_asoc_being_freed(inp, stcb);
3898 /* re-increment the lock */
3899 if (from_inpcbfree == SCTP_NORMAL_PROC) {
3900 atomic_add_int(&stcb->asoc.refcnt, -1);
3903 if (inp->sctp_tcbhash) {
3904 LIST_REMOVE(stcb, sctp_tcbhash);
3906 if (stcb->asoc.in_restart_hash) {
3907 LIST_REMOVE(stcb, sctp_tcbrestarhash);
3909 /* Now lets remove it from the list of ALL associations in the EP */
3910 LIST_REMOVE(stcb, sctp_tcblist);
3911 if (from_inpcbfree == SCTP_NORMAL_PROC) {
3912 SCTP_INP_INCR_REF(inp);
3913 SCTP_INP_WUNLOCK(inp);
3914 SCTP_ITERATOR_UNLOCK();
3916 /* pull from vtag hash */
3917 LIST_REMOVE(stcb, sctp_asocs);
3918 sctp_add_vtag_to_timewait(inp, asoc->my_vtag, SCTP_TIME_WAIT);
3922 * Now restop the timers to be sure - this is paranoia at is finest!
3924 (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
3925 (void)SCTP_OS_TIMER_STOP(&asoc->hb_timer.timer);
3926 (void)SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
3927 (void)SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
3928 (void)SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
3929 (void)SCTP_OS_TIMER_STOP(&asoc->shut_guard_timer.timer);
3930 (void)SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
3931 (void)SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
3933 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3934 (void)SCTP_OS_TIMER_STOP(&net->fr_timer.timer);
3935 (void)SCTP_OS_TIMER_STOP(&net->rxt_timer.timer);
3936 (void)SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
3939 asoc->strreset_timer.type = SCTP_TIMER_TYPE_NONE;
3942 * The chunk lists and such SHOULD be empty but we check them just
3945 /* anything on the wheel needs to be removed */
3946 for (i = 0; i < asoc->streamoutcnt; i++) {
3947 struct sctp_stream_out *outs;
3949 outs = &asoc->strmout[i];
3950 /* now clean up any chunks here */
3951 sp = TAILQ_FIRST(&outs->outqueue);
3953 TAILQ_REMOVE(&outs->outqueue, sp, next);
3955 sctp_m_freem(sp->data);
3957 sp->tail_mbuf = NULL;
3959 sctp_free_remote_addr(sp->net);
3960 sctp_free_spbufspace(stcb, asoc, sp);
3961 /* Free the zone stuff */
3962 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_strmoq, sp);
3963 SCTP_DECR_STRMOQ_COUNT();
3964 /* sa_ignore FREED_MEMORY */
3965 sp = TAILQ_FIRST(&outs->outqueue);
3969 /* sa_ignore FREED_MEMORY */
3970 while ((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) {
3971 TAILQ_REMOVE(&asoc->resetHead, liste, next_resp);
3972 SCTP_FREE(liste, SCTP_M_STRESET);
3975 sq = TAILQ_FIRST(&asoc->pending_reply_queue);
3977 TAILQ_REMOVE(&asoc->pending_reply_queue, sq, next);
3979 sctp_m_freem(sq->data);
3982 sctp_free_remote_addr(sq->whoFrom);
3985 /* Free the ctl entry */
3986 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_readq, sq);
3987 SCTP_DECR_READQ_COUNT();
3988 /* sa_ignore FREED_MEMORY */
3989 sq = TAILQ_FIRST(&asoc->pending_reply_queue);
3992 chk = TAILQ_FIRST(&asoc->free_chunks);
3994 TAILQ_REMOVE(&asoc->free_chunks, chk, sctp_next);
3996 sctp_m_freem(chk->data);
4000 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
4001 SCTP_DECR_CHK_COUNT();
4002 atomic_subtract_int(&sctppcbinfo.ipi_free_chunks, 1);
4003 asoc->free_chunk_cnt--;
4004 /* sa_ignore FREED_MEMORY */
4005 chk = TAILQ_FIRST(&asoc->free_chunks);
4007 /* pending send queue SHOULD be empty */
4008 if (!TAILQ_EMPTY(&asoc->send_queue)) {
4009 chk = TAILQ_FIRST(&asoc->send_queue);
4011 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
4013 sctp_m_freem(chk->data);
4017 sctp_free_remote_addr(chk->whoTo);
4018 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
4019 SCTP_DECR_CHK_COUNT();
4020 /* sa_ignore FREED_MEMORY */
4021 chk = TAILQ_FIRST(&asoc->send_queue);
4026 printf("Freed %d from send_queue\n", ccnt);
4030 /* sent queue SHOULD be empty */
4031 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
4032 chk = TAILQ_FIRST(&asoc->sent_queue);
4034 TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
4036 sctp_m_freem(chk->data);
4040 sctp_free_remote_addr(chk->whoTo);
4041 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
4042 SCTP_DECR_CHK_COUNT();
4043 /* sa_ignore FREED_MEMORY */
4044 chk = TAILQ_FIRST(&asoc->sent_queue);
4049 printf("Freed %d from sent_queue\n", ccnt);
4053 /* control queue MAY not be empty */
4054 if (!TAILQ_EMPTY(&asoc->control_send_queue)) {
4055 chk = TAILQ_FIRST(&asoc->control_send_queue);
4057 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
4059 sctp_m_freem(chk->data);
4063 sctp_free_remote_addr(chk->whoTo);
4064 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
4065 SCTP_DECR_CHK_COUNT();
4066 /* sa_ignore FREED_MEMORY */
4067 chk = TAILQ_FIRST(&asoc->control_send_queue);
4072 printf("Freed %d from ctrl_queue\n", ccnt);
4076 if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
4077 chk = TAILQ_FIRST(&asoc->reasmqueue);
4079 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
4081 sctp_m_freem(chk->data);
4084 sctp_free_remote_addr(chk->whoTo);
4086 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
4087 SCTP_DECR_CHK_COUNT();
4088 /* sa_ignore FREED_MEMORY */
4089 chk = TAILQ_FIRST(&asoc->reasmqueue);
4094 printf("Freed %d from reasm_queue\n", ccnt);
4098 if (asoc->mapping_array) {
4099 SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
4100 asoc->mapping_array = NULL;
4102 /* the stream outs */
4103 if (asoc->strmout) {
4104 SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
4105 asoc->strmout = NULL;
4107 asoc->streamoutcnt = 0;
4109 struct sctp_queued_to_read *ctl;
4111 for (i = 0; i < asoc->streamincnt; i++) {
4112 if (!TAILQ_EMPTY(&asoc->strmin[i].inqueue)) {
4113 /* We have somethings on the streamin queue */
4114 ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue);
4116 TAILQ_REMOVE(&asoc->strmin[i].inqueue,
4118 sctp_free_remote_addr(ctl->whoFrom);
4120 sctp_m_freem(ctl->data);
4124 * We don't free the address here
4125 * since all the net's were freed
4128 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_readq, ctl);
4129 SCTP_DECR_READQ_COUNT();
4130 ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue);
4134 SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
4135 asoc->strmin = NULL;
4137 asoc->streamincnt = 0;
4138 while (!TAILQ_EMPTY(&asoc->nets)) {
4139 /* sa_ignore FREED_MEMORY */
4140 net = TAILQ_FIRST(&asoc->nets);
4141 /* pull from list */
4142 if ((sctppcbinfo.ipi_count_raddr == 0) || (prev == net)) {
4144 panic("no net's left alloc'ed, or list points to itself");
4149 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
4150 sctp_free_remote_addr(net);
4153 while (!SCTP_LIST_EMPTY(&asoc->sctp_restricted_addrs)) {
4154 /* sa_ignore FREED_MEMORY */
4155 laddr = LIST_FIRST(&asoc->sctp_restricted_addrs);
4156 sctp_remove_laddr(laddr);
4159 /* pending asconf (address) parameters */
4160 while (!TAILQ_EMPTY(&asoc->asconf_queue)) {
4161 /* sa_ignore FREED_MEMORY */
4162 aparam = TAILQ_FIRST(&asoc->asconf_queue);
4163 TAILQ_REMOVE(&asoc->asconf_queue, aparam, next);
4164 SCTP_FREE(aparam, SCTP_M_ASC_ADDR);
4166 if (asoc->last_asconf_ack_sent != NULL) {
4167 sctp_m_freem(asoc->last_asconf_ack_sent);
4168 asoc->last_asconf_ack_sent = NULL;
4170 /* clean up auth stuff */
4171 if (asoc->local_hmacs)
4172 sctp_free_hmaclist(asoc->local_hmacs);
4173 if (asoc->peer_hmacs)
4174 sctp_free_hmaclist(asoc->peer_hmacs);
4176 if (asoc->local_auth_chunks)
4177 sctp_free_chunklist(asoc->local_auth_chunks);
4178 if (asoc->peer_auth_chunks)
4179 sctp_free_chunklist(asoc->peer_auth_chunks);
4181 sctp_free_authinfo(&asoc->authinfo);
4183 shared_key = LIST_FIRST(&asoc->shared_keys);
4184 while (shared_key) {
4185 LIST_REMOVE(shared_key, next);
4186 sctp_free_sharedkey(shared_key);
4187 /* sa_ignore FREED_MEMORY */
4188 shared_key = LIST_FIRST(&asoc->shared_keys);
4191 /* Insert new items here :> */
4193 /* Get rid of LOCK */
4194 SCTP_TCB_LOCK_DESTROY(stcb);
4195 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
4196 if (from_inpcbfree == SCTP_NORMAL_PROC) {
4197 SCTP_INP_INFO_WUNLOCK();
4198 SCTP_INP_RLOCK(inp);
4200 #ifdef SCTP_TRACK_FREED_ASOCS
4201 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4202 /* now clean up the tasoc itself */
4203 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
4204 SCTP_DECR_ASOC_COUNT();
4206 LIST_INSERT_HEAD(&inp->sctp_asoc_free_list, stcb, sctp_tcblist);
4209 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
4210 SCTP_DECR_ASOC_COUNT();
4212 if (from_inpcbfree == SCTP_NORMAL_PROC) {
4213 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4215 * If its NOT the inp_free calling us AND sctp_close
4216 * as been called, we call back...
4218 SCTP_INP_RUNLOCK(inp);
4220 * This will start the kill timer (if we are the
4221 * lastone) since we hold an increment yet. But this
4222 * is the only safe way to do this since otherwise
4223 * if the socket closes at the same time we are here
4224 * we might collide in the cleanup.
4226 sctp_inpcb_free(inp,
4227 SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
4228 SCTP_CALLED_DIRECTLY_NOCMPSET);
4229 SCTP_INP_DECR_REF(inp);
4232 /* The socket is still open. */
4233 SCTP_INP_DECR_REF(inp);
4236 if (from_inpcbfree == SCTP_NORMAL_PROC) {
4237 SCTP_INP_RUNLOCK(inp);
4240 /* destroyed the asoc */
4241 #ifdef SCTP_LOG_CLOSING
4242 sctp_log_closing(inp, NULL, 11);
4250 * determine if a destination is "reachable" based upon the addresses bound
4251 * to the current endpoint (e.g. only v4 or v6 currently bound)
4254 * FIX: if we allow assoc-level bindx(), then this needs to be fixed to use
4255 * assoc level v4/v6 flags, as the assoc *may* not have the same address
4256 * types bound as its endpoint
4259 sctp_destination_is_reachable(struct sctp_tcb *stcb, struct sockaddr *destaddr)
4261 struct sctp_inpcb *inp;
4265 * No locks here, the TCB, in all cases is already locked and an
4266 * assoc is up. There is either a INP lock by the caller applied (in
4267 * asconf case when deleting an address) or NOT in the HB case,
4268 * however if HB then the INP increment is up and the INP will not
4269 * be removed (on top of the fact that we have a TCB lock). So we
4270 * only want to read the sctp_flags, which is either bound-all or
4271 * not.. no protection needed since once an assoc is up you can't be
4272 * changing your binding.
4274 inp = stcb->sctp_ep;
4275 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4276 /* if bound all, destination is not restricted */
4278 * RRS: Question during lock work: Is this correct? If you
4279 * are bound-all you still might need to obey the V4--V6
4280 * flags??? IMO this bound-all stuff needs to be removed!
4284 /* NOTE: all "scope" checks are done when local addresses are added */
4285 if (destaddr->sa_family == AF_INET6) {
4286 answer = inp->ip_inp.inp.inp_vflag & INP_IPV6;
4287 } else if (destaddr->sa_family == AF_INET) {
4288 answer = inp->ip_inp.inp.inp_vflag & INP_IPV4;
4290 /* invalid family, so it's unreachable */
4297 * update the inp_vflags on an endpoint
4300 sctp_update_ep_vflag(struct sctp_inpcb *inp)
4302 struct sctp_laddr *laddr;
4304 /* first clear the flag */
4305 inp->ip_inp.inp.inp_vflag = 0;
4306 /* set the flag based on addresses on the ep list */
4307 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4308 if (laddr->ifa == NULL) {
4309 SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n",
4313 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
4316 if (laddr->ifa->address.sa.sa_family == AF_INET6) {
4317 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
4318 } else if (laddr->ifa->address.sa.sa_family == AF_INET) {
4319 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
4325 * Add the address to the endpoint local address list There is nothing to be
4326 * done if we are bound to all addresses
4329 sctp_add_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa, uint32_t action)
4331 struct sctp_laddr *laddr;
4336 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4337 /* You are already bound to all. You have it already */
4340 if (ifa->address.sa.sa_family == AF_INET6) {
4341 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
4342 /* Can't bind a non-useable addr. */
4346 /* first, is it already present? */
4347 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4348 if (laddr->ifa == ifa) {
4355 /* Not in the ep list */
4356 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, action);
4360 /* update inp_vflag flags */
4361 if (ifa->address.sa.sa_family == AF_INET6) {
4362 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
4363 } else if (ifa->address.sa.sa_family == AF_INET) {
4364 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
4372 * select a new (hopefully reachable) destination net (should only be used
4373 * when we deleted an ep addr that is the only usable source address to reach
4374 * the destination net)
4377 sctp_select_primary_destination(struct sctp_tcb *stcb)
4379 struct sctp_nets *net;
4381 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4382 /* for now, we'll just pick the first reachable one we find */
4383 if (net->dest_state & SCTP_ADDR_UNCONFIRMED)
4385 if (sctp_destination_is_reachable(stcb,
4386 (struct sockaddr *)&net->ro._l_addr)) {
4387 /* found a reachable destination */
4388 stcb->asoc.primary_destination = net;
4391 /* I can't there from here! ...we're gonna die shortly... */
4396 * Delete the address from the endpoint local address list There is nothing
4397 * to be done if we are bound to all addresses
4400 sctp_del_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
4402 struct sctp_laddr *laddr;
4406 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4407 /* You are already bound to all. You have it already */
4410 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4411 if (laddr->ifa == ifa) {
4416 if (fnd && (inp->laddr_count < 2)) {
4417 /* can't delete unless there are at LEAST 2 addresses */
4422 * clean up any use of this address go through our
4423 * associations and clear any last_used_address that match
4424 * this one for each assoc, see if a new primary_destination
4427 struct sctp_tcb *stcb;
4429 /* clean up "next_addr_touse" */
4430 if (inp->next_addr_touse == laddr)
4431 /* delete this address */
4432 inp->next_addr_touse = NULL;
4434 /* clean up "last_used_address" */
4435 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4436 struct sctp_nets *net;
4438 SCTP_TCB_LOCK(stcb);
4439 if (stcb->asoc.last_used_address == laddr)
4440 /* delete this address */
4441 stcb->asoc.last_used_address = NULL;
4443 * Now spin through all the nets and purge any ref
4446 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4447 if (net->ro._s_addr &&
4448 (net->ro._s_addr->ifa == laddr->ifa)) {
4449 /* Yep, purge src address selected */
4452 /* delete this address if cached */
4456 net->ro.ro_rt = NULL;
4458 sctp_free_ifa(net->ro._s_addr);
4459 net->ro._s_addr = NULL;
4460 net->src_addr_selected = 0;
4463 SCTP_TCB_UNLOCK(stcb);
4464 } /* for each tcb */
4465 /* remove it from the ep list */
4466 sctp_remove_laddr(laddr);
4468 /* update inp_vflag flags */
4469 sctp_update_ep_vflag(inp);
4475 * Add the address to the TCB local address restricted list.
4476 * This is a "pending" address list (eg. addresses waiting for an
4477 * ASCONF-ACK response) and cannot be used as a valid source address.
4480 sctp_add_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
4482 struct sctp_inpcb *inp;
4483 struct sctp_laddr *laddr;
4484 struct sctpladdr *list;
4487 * Assumes TCB is locked.. and possibly the INP. May need to
4488 * confirm/fix that if we need it and is not the case.
4490 list = &stcb->asoc.sctp_restricted_addrs;
4492 inp = stcb->sctp_ep;
4493 if (ifa->address.sa.sa_family == AF_INET6) {
4494 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
4495 /* Can't bind a non-existent addr. */
4499 /* does the address already exist? */
4500 LIST_FOREACH(laddr, list, sctp_nxt_addr) {
4501 if (laddr->ifa == ifa) {
4506 /* add to the list */
4507 (void)sctp_insert_laddr(list, ifa, 0);
4512 * insert an laddr entry with the given ifa for the desired list
4515 sctp_insert_laddr(struct sctpladdr *list, struct sctp_ifa *ifa, uint32_t act)
4517 struct sctp_laddr *laddr;
4519 laddr = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_laddr, struct sctp_laddr);
4520 if (laddr == NULL) {
4521 /* out of memory? */
4524 SCTP_INCR_LADDR_COUNT();
4525 bzero(laddr, sizeof(*laddr));
4526 (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time);
4528 laddr->action = act;
4529 atomic_add_int(&ifa->refcount, 1);
4531 LIST_INSERT_HEAD(list, laddr, sctp_nxt_addr);
4537 * Remove an laddr entry from the local address list (on an assoc)
4540 sctp_remove_laddr(struct sctp_laddr *laddr)
4543 /* remove from the list */
4544 LIST_REMOVE(laddr, sctp_nxt_addr);
4545 sctp_free_ifa(laddr->ifa);
4546 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_laddr, laddr);
4547 SCTP_DECR_LADDR_COUNT();
4551 * Remove a local address from the TCB local address restricted list
4554 sctp_del_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
4556 struct sctp_inpcb *inp;
4557 struct sctp_laddr *laddr;
4560 * This is called by asconf work. It is assumed that a) The TCB is
4561 * locked and b) The INP is locked. This is true in as much as I can
4562 * trace through the entry asconf code where I did these locks.
4563 * Again, the ASCONF code is a bit different in that it does lock
4564 * the INP during its work often times. This must be since we don't
4565 * want other proc's looking up things while what they are looking
4566 * up is changing :-D
4569 inp = stcb->sctp_ep;
4570 /* if subset bound and don't allow ASCONF's, can't delete last */
4571 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) &&
4572 sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF)) {
4573 if (stcb->sctp_ep->laddr_count < 2) {
4574 /* can't delete last address */
4578 LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
4579 /* remove the address if it exists */
4580 if (laddr->ifa == NULL)
4582 if (laddr->ifa == ifa) {
4583 sctp_remove_laddr(laddr);
4588 /* address not found! */
4592 static char sctp_pcb_initialized = 0;
4595 * Temporarily remove for __APPLE__ until we use the Tiger equivalents
4598 static int sctp_max_number_of_assoc = SCTP_MAX_NUM_OF_ASOC;
4599 static int sctp_scale_up_for_address = SCTP_SCALE_FOR_ADDR;
4605 * SCTP initialization for the PCB structures should be called by
4606 * the sctp_init() funciton.
4610 if (sctp_pcb_initialized != 0) {
4611 /* error I was called twice */
4614 sctp_pcb_initialized = 1;
4616 bzero(&sctpstat, sizeof(struct sctpstat));
4617 (void)SCTP_GETTIME_TIMEVAL(&sctpstat.sctps_discontinuitytime);
4618 /* init the empty list of (All) Endpoints */
4619 LIST_INIT(&sctppcbinfo.listhead);
4621 /* init the iterator head */
4622 TAILQ_INIT(&sctppcbinfo.iteratorhead);
4624 /* init the hash table of endpoints */
4625 TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", &sctp_hashtblsize);
4626 TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", &sctp_pcbtblsize);
4627 TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", &sctp_chunkscale);
4628 sctppcbinfo.sctp_asochash = SCTP_HASH_INIT((sctp_hashtblsize * 31),
4629 &sctppcbinfo.hashasocmark);
4630 sctppcbinfo.sctp_ephash = SCTP_HASH_INIT(sctp_hashtblsize,
4631 &sctppcbinfo.hashmark);
4632 sctppcbinfo.sctp_tcpephash = SCTP_HASH_INIT(sctp_hashtblsize,
4633 &sctppcbinfo.hashtcpmark);
4634 sctppcbinfo.hashtblsize = sctp_hashtblsize;
4636 /* init the small hash table we use to track restarted asoc's */
4637 sctppcbinfo.sctp_restarthash = SCTP_HASH_INIT(SCTP_STACK_VTAG_HASH_SIZE,
4638 &sctppcbinfo.hashrestartmark);
4641 sctppcbinfo.sctp_vrfhash = SCTP_HASH_INIT(SCTP_SIZE_OF_VRF_HASH,
4642 &sctppcbinfo.hashvrfmark);
4644 sctppcbinfo.vrf_ifn_hash = SCTP_HASH_INIT(SCTP_VRF_IFN_HASH_SIZE,
4645 &sctppcbinfo.vrf_ifn_hashmark);
4647 /* init the zones */
4649 * FIX ME: Should check for NULL returns, but if it does fail we are
4650 * doomed to panic anyways... add later maybe.
4652 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_ep, "sctp_ep",
4653 sizeof(struct sctp_inpcb), maxsockets);
4655 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_asoc, "sctp_asoc",
4656 sizeof(struct sctp_tcb), sctp_max_number_of_assoc);
4658 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_laddr, "sctp_laddr",
4659 sizeof(struct sctp_laddr),
4660 (sctp_max_number_of_assoc * sctp_scale_up_for_address));
4662 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_net, "sctp_raddr",
4663 sizeof(struct sctp_nets),
4664 (sctp_max_number_of_assoc * sctp_scale_up_for_address));
4666 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_chunk, "sctp_chunk",
4667 sizeof(struct sctp_tmit_chunk),
4668 (sctp_max_number_of_assoc * sctp_chunkscale));
4670 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_readq, "sctp_readq",
4671 sizeof(struct sctp_queued_to_read),
4672 (sctp_max_number_of_assoc * sctp_chunkscale));
4674 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_strmoq, "sctp_stream_msg_out",
4675 sizeof(struct sctp_stream_queue_pending),
4676 (sctp_max_number_of_assoc * sctp_chunkscale));
4678 /* Master Lock INIT for info structure */
4679 SCTP_INP_INFO_LOCK_INIT();
4680 SCTP_STATLOG_INIT_LOCK();
4681 SCTP_ITERATOR_LOCK_INIT();
4683 SCTP_IPI_COUNT_INIT();
4684 SCTP_IPI_ADDR_INIT();
4685 SCTP_IPI_ITERATOR_WQ_INIT();
4686 #ifdef SCTP_PACKET_LOGGING
4687 SCTP_IP_PKTLOG_INIT();
4689 LIST_INIT(&sctppcbinfo.addr_wq);
4691 /* not sure if we need all the counts */
4692 sctppcbinfo.ipi_count_ep = 0;
4693 /* assoc/tcb zone info */
4694 sctppcbinfo.ipi_count_asoc = 0;
4695 /* local addrlist zone info */
4696 sctppcbinfo.ipi_count_laddr = 0;
4697 /* remote addrlist zone info */
4698 sctppcbinfo.ipi_count_raddr = 0;
4700 sctppcbinfo.ipi_count_chunk = 0;
4702 /* socket queue zone info */
4703 sctppcbinfo.ipi_count_readq = 0;
4705 /* stream out queue cont */
4706 sctppcbinfo.ipi_count_strmoq = 0;
4708 sctppcbinfo.ipi_free_strmoq = 0;
4709 sctppcbinfo.ipi_free_chunks = 0;
4711 SCTP_OS_TIMER_INIT(&sctppcbinfo.addr_wq_timer.timer);
4713 /* Init the TIMEWAIT list */
4714 for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE_A; i++) {
4715 LIST_INIT(&sctppcbinfo.vtag_timewait[i]);
4718 #if defined(SCTP_USE_THREAD_BASED_ITERATOR)
4719 sctppcbinfo.iterator_running = 0;
4720 sctp_startup_iterator();
4724 * INIT the default VRF which for BSD is the only one, other O/S's
4725 * may have more. But initially they must start with one and then
4726 * add the VRF's as addresses are added.
4728 sctp_init_vrf_list(SCTP_DEFAULT_VRF);
4734 sctp_load_addresses_from_init(struct sctp_tcb *stcb, struct mbuf *m,
4735 int iphlen, int offset, int limit, struct sctphdr *sh,
4736 struct sockaddr *altsa)
4739 * grub through the INIT pulling addresses and loading them to the
4740 * nets structure in the asoc. The from address in the mbuf should
4741 * also be loaded (if it is not already). This routine can be called
4742 * with either INIT or INIT-ACK's as long as the m points to the IP
4743 * packet and the offset points to the beginning of the parameters.
4745 struct sctp_inpcb *inp, *l_inp;
4746 struct sctp_nets *net, *net_tmp;
4748 struct sctp_paramhdr *phdr, parm_buf;
4749 struct sctp_tcb *stcb_tmp;
4750 uint16_t ptype, plen;
4751 struct sockaddr *sa;
4752 struct sockaddr_storage dest_store;
4753 struct sockaddr *local_sa = (struct sockaddr *)&dest_store;
4754 struct sockaddr_in sin;
4755 struct sockaddr_in6 sin6;
4756 uint8_t random_store[SCTP_PARAM_BUFFER_SIZE];
4757 struct sctp_auth_random *p_random = NULL;
4758 uint16_t random_len = 0;
4759 uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE];
4760 struct sctp_auth_hmac_algo *hmacs = NULL;
4761 uint16_t hmacs_len = 0;
4762 uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE];
4763 struct sctp_auth_chunk_list *chunks = NULL;
4764 uint16_t num_chunks = 0;
4765 sctp_key_t *new_key;
4767 int got_random = 0, got_hmacs = 0, got_chklist = 0;
4769 /* First get the destination address setup too. */
4770 memset(&sin, 0, sizeof(sin));
4771 memset(&sin6, 0, sizeof(sin6));
4773 sin.sin_family = AF_INET;
4774 sin.sin_len = sizeof(sin);
4775 sin.sin_port = stcb->rport;
4777 sin6.sin6_family = AF_INET6;
4778 sin6.sin6_len = sizeof(struct sockaddr_in6);
4779 sin6.sin6_port = stcb->rport;
4780 if (altsa == NULL) {
4781 iph = mtod(m, struct ip *);
4782 if (iph->ip_v == IPVERSION) {
4784 struct sockaddr_in *sin_2;
4786 sin_2 = (struct sockaddr_in *)(local_sa);
4787 memset(sin_2, 0, sizeof(sin));
4788 sin_2->sin_family = AF_INET;
4789 sin_2->sin_len = sizeof(sin);
4790 sin_2->sin_port = sh->dest_port;
4791 sin_2->sin_addr.s_addr = iph->ip_dst.s_addr;
4792 sin.sin_addr = iph->ip_src;
4793 sa = (struct sockaddr *)&sin;
4794 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
4796 struct ip6_hdr *ip6;
4797 struct sockaddr_in6 *sin6_2;
4799 ip6 = mtod(m, struct ip6_hdr *);
4800 sin6_2 = (struct sockaddr_in6 *)(local_sa);
4801 memset(sin6_2, 0, sizeof(sin6));
4802 sin6_2->sin6_family = AF_INET6;
4803 sin6_2->sin6_len = sizeof(struct sockaddr_in6);
4804 sin6_2->sin6_port = sh->dest_port;
4805 sin6.sin6_addr = ip6->ip6_src;
4806 sa = (struct sockaddr *)&sin6;
4812 * For cookies we use the src address NOT from the packet
4813 * but from the original INIT
4817 /* Turn off ECN until we get through all params */
4818 stcb->asoc.ecn_allowed = 0;
4819 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4820 /* mark all addresses that we have currently on the list */
4821 net->dest_state |= SCTP_ADDR_NOT_IN_ASSOC;
4823 /* does the source address already exist? if so skip it */
4824 l_inp = inp = stcb->sctp_ep;
4826 atomic_add_int(&stcb->asoc.refcnt, 1);
4827 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net_tmp, local_sa, stcb);
4828 atomic_add_int(&stcb->asoc.refcnt, -1);
4830 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || inp == NULL) {
4831 /* we must add the source address */
4832 /* no scope set here since we have a tcb already. */
4833 if ((sa->sa_family == AF_INET) &&
4834 (stcb->asoc.ipv4_addr_legal)) {
4835 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_2)) {
4838 } else if ((sa->sa_family == AF_INET6) &&
4839 (stcb->asoc.ipv6_addr_legal)) {
4840 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) {
4845 if (net_tmp != NULL && stcb_tmp == stcb) {
4846 net_tmp->dest_state &= ~SCTP_ADDR_NOT_IN_ASSOC;
4847 } else if (stcb_tmp != stcb) {
4848 /* It belongs to another association? */
4850 SCTP_TCB_UNLOCK(stcb_tmp);
4854 if (stcb->asoc.state == 0) {
4855 /* the assoc was freed? */
4859 * peer must explicitly turn this on. This may have been initialized
4860 * to be "on" in order to allow local addr changes while INIT's are
4863 stcb->asoc.peer_supports_asconf = 0;
4864 /* now we must go through each of the params. */
4865 phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
4867 ptype = ntohs(phdr->param_type);
4868 plen = ntohs(phdr->param_length);
4870 * printf("ptype => %0x, plen => %d\n", (uint32_t)ptype,
4873 if (offset + plen > limit) {
4879 if (ptype == SCTP_IPV4_ADDRESS) {
4880 if (stcb->asoc.ipv4_addr_legal) {
4881 struct sctp_ipv4addr_param *p4, p4_buf;
4883 /* ok get the v4 address and check/add */
4884 phdr = sctp_get_next_param(m, offset,
4885 (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
4886 if (plen != sizeof(struct sctp_ipv4addr_param) ||
4890 p4 = (struct sctp_ipv4addr_param *)phdr;
4891 sin.sin_addr.s_addr = p4->addr;
4892 if (IN_MULTICAST(sin.sin_addr.s_addr)) {
4893 /* Skip multi-cast addresses */
4896 if ((sin.sin_addr.s_addr == INADDR_BROADCAST) ||
4897 (sin.sin_addr.s_addr == INADDR_ANY)) {
4900 sa = (struct sockaddr *)&sin;
4901 inp = stcb->sctp_ep;
4902 atomic_add_int(&stcb->asoc.refcnt, 1);
4903 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
4905 atomic_add_int(&stcb->asoc.refcnt, -1);
4907 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) ||
4909 /* we must add the source address */
4911 * no scope set since we have a tcb
4916 * we must validate the state again
4919 if (stcb->asoc.state == 0) {
4920 /* the assoc was freed? */
4923 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_4)) {
4926 } else if (stcb_tmp == stcb) {
4927 if (stcb->asoc.state == 0) {
4928 /* the assoc was freed? */
4934 ~SCTP_ADDR_NOT_IN_ASSOC;
4938 * strange, address is in another
4939 * assoc? straighten out locks.
4942 SCTP_TCB_UNLOCK(stcb_tmp);
4944 if (stcb->asoc.state == 0) {
4945 /* the assoc was freed? */
4951 } else if (ptype == SCTP_IPV6_ADDRESS) {
4952 if (stcb->asoc.ipv6_addr_legal) {
4953 /* ok get the v6 address and check/add */
4954 struct sctp_ipv6addr_param *p6, p6_buf;
4956 phdr = sctp_get_next_param(m, offset,
4957 (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
4958 if (plen != sizeof(struct sctp_ipv6addr_param) ||
4962 p6 = (struct sctp_ipv6addr_param *)phdr;
4963 memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
4965 if (IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) {
4966 /* Skip multi-cast addresses */
4969 if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
4971 * Link local make no sense without
4976 sa = (struct sockaddr *)&sin6;
4977 inp = stcb->sctp_ep;
4978 atomic_add_int(&stcb->asoc.refcnt, 1);
4979 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
4981 atomic_add_int(&stcb->asoc.refcnt, -1);
4982 if (stcb_tmp == NULL && (inp == stcb->sctp_ep ||
4985 * we must validate the state again
4988 if (stcb->asoc.state == 0) {
4989 /* the assoc was freed? */
4993 * we must add the address, no scope
4996 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_5)) {
4999 } else if (stcb_tmp == stcb) {
5001 * we must validate the state again
5004 if (stcb->asoc.state == 0) {
5005 /* the assoc was freed? */
5011 ~SCTP_ADDR_NOT_IN_ASSOC;
5015 * strange, address is in another
5016 * assoc? straighten out locks.
5019 SCTP_TCB_UNLOCK(stcb_tmp);
5021 if (stcb->asoc.state == 0) {
5022 /* the assoc was freed? */
5028 } else if (ptype == SCTP_ECN_CAPABLE) {
5029 stcb->asoc.ecn_allowed = 1;
5030 } else if (ptype == SCTP_ULP_ADAPTATION) {
5031 if (stcb->asoc.state != SCTP_STATE_OPEN) {
5032 struct sctp_adaptation_layer_indication ai,
5035 phdr = sctp_get_next_param(m, offset,
5036 (struct sctp_paramhdr *)&ai, sizeof(ai));
5037 aip = (struct sctp_adaptation_layer_indication *)phdr;
5039 sctp_ulp_notify(SCTP_NOTIFY_ADAPTATION_INDICATION,
5040 stcb, ntohl(aip->indication), NULL);
5043 } else if (ptype == SCTP_SET_PRIM_ADDR) {
5044 struct sctp_asconf_addr_param lstore, *fee;
5045 struct sctp_asconf_addrv4_param *fii;
5047 struct sockaddr *lsa = NULL;
5049 stcb->asoc.peer_supports_asconf = 1;
5050 if (plen > sizeof(lstore)) {
5053 phdr = sctp_get_next_param(m, offset,
5054 (struct sctp_paramhdr *)&lstore, min(plen, sizeof(lstore)));
5058 fee = (struct sctp_asconf_addr_param *)phdr;
5059 lptype = ntohs(fee->addrp.ph.param_type);
5060 if (lptype == SCTP_IPV4_ADDRESS) {
5062 sizeof(struct sctp_asconf_addrv4_param)) {
5063 SCTP_PRINTF("Sizeof setprim in init/init ack not %d but %d - ignored\n",
5064 (int)sizeof(struct sctp_asconf_addrv4_param),
5067 fii = (struct sctp_asconf_addrv4_param *)fee;
5068 sin.sin_addr.s_addr = fii->addrp.addr;
5069 lsa = (struct sockaddr *)&sin;
5071 } else if (lptype == SCTP_IPV6_ADDRESS) {
5073 sizeof(struct sctp_asconf_addr_param)) {
5074 SCTP_PRINTF("Sizeof setprim (v6) in init/init ack not %d but %d - ignored\n",
5075 (int)sizeof(struct sctp_asconf_addr_param),
5078 memcpy(sin6.sin6_addr.s6_addr,
5080 sizeof(fee->addrp.addr));
5081 lsa = (struct sockaddr *)&sin6;
5085 (void)sctp_set_primary_addr(stcb, sa, NULL);
5087 } else if (ptype == SCTP_PRSCTP_SUPPORTED) {
5088 /* Peer supports pr-sctp */
5089 stcb->asoc.peer_supports_prsctp = 1;
5090 } else if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
5091 /* A supported extension chunk */
5092 struct sctp_supported_chunk_types_param *pr_supported;
5093 uint8_t local_store[SCTP_PARAM_BUFFER_SIZE];
5096 phdr = sctp_get_next_param(m, offset,
5097 (struct sctp_paramhdr *)&local_store, min(sizeof(local_store), plen));
5101 stcb->asoc.peer_supports_asconf = 0;
5102 stcb->asoc.peer_supports_prsctp = 0;
5103 stcb->asoc.peer_supports_pktdrop = 0;
5104 stcb->asoc.peer_supports_strreset = 0;
5105 stcb->asoc.peer_supports_auth = 0;
5106 pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
5107 num_ent = plen - sizeof(struct sctp_paramhdr);
5108 for (i = 0; i < num_ent; i++) {
5109 switch (pr_supported->chunk_types[i]) {
5111 case SCTP_ASCONF_ACK:
5112 stcb->asoc.peer_supports_asconf = 1;
5114 case SCTP_FORWARD_CUM_TSN:
5115 stcb->asoc.peer_supports_prsctp = 1;
5117 case SCTP_PACKET_DROPPED:
5118 stcb->asoc.peer_supports_pktdrop = 1;
5120 case SCTP_STREAM_RESET:
5121 stcb->asoc.peer_supports_strreset = 1;
5123 case SCTP_AUTHENTICATION:
5124 stcb->asoc.peer_supports_auth = 1;
5127 /* one I have not learned yet */
5132 } else if (ptype == SCTP_ECN_NONCE_SUPPORTED) {
5133 /* Peer supports ECN-nonce */
5134 stcb->asoc.peer_supports_ecn_nonce = 1;
5135 stcb->asoc.ecn_nonce_allowed = 1;
5136 } else if (ptype == SCTP_RANDOM) {
5137 if (plen > sizeof(random_store))
5140 /* already processed a RANDOM */
5143 phdr = sctp_get_next_param(m, offset,
5144 (struct sctp_paramhdr *)random_store,
5145 min(sizeof(random_store), plen));
5148 p_random = (struct sctp_auth_random *)phdr;
5149 random_len = plen - sizeof(*p_random);
5150 /* enforce the random length */
5151 if (random_len != SCTP_AUTH_RANDOM_SIZE_REQUIRED) {
5152 SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: invalid RANDOM len\n");
5156 } else if (ptype == SCTP_HMAC_LIST) {
5160 if (plen > sizeof(hmacs_store))
5163 /* already processed a HMAC list */
5166 phdr = sctp_get_next_param(m, offset,
5167 (struct sctp_paramhdr *)hmacs_store,
5168 min(plen, sizeof(hmacs_store)));
5171 hmacs = (struct sctp_auth_hmac_algo *)phdr;
5172 hmacs_len = plen - sizeof(*hmacs);
5173 num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]);
5174 /* validate the hmac list */
5175 if (sctp_verify_hmac_param(hmacs, num_hmacs)) {
5178 if (stcb->asoc.peer_hmacs != NULL)
5179 sctp_free_hmaclist(stcb->asoc.peer_hmacs);
5180 stcb->asoc.peer_hmacs = sctp_alloc_hmaclist(num_hmacs);
5181 if (stcb->asoc.peer_hmacs != NULL) {
5182 for (i = 0; i < num_hmacs; i++) {
5183 (void)sctp_auth_add_hmacid(stcb->asoc.peer_hmacs,
5184 ntohs(hmacs->hmac_ids[i]));
5188 } else if (ptype == SCTP_CHUNK_LIST) {
5191 if (plen > sizeof(chunks_store))
5194 /* already processed a Chunks list */
5197 phdr = sctp_get_next_param(m, offset,
5198 (struct sctp_paramhdr *)chunks_store,
5199 min(plen, sizeof(chunks_store)));
5202 chunks = (struct sctp_auth_chunk_list *)phdr;
5203 num_chunks = plen - sizeof(*chunks);
5204 if (stcb->asoc.peer_auth_chunks != NULL)
5205 sctp_clear_chunklist(stcb->asoc.peer_auth_chunks);
5207 stcb->asoc.peer_auth_chunks = sctp_alloc_chunklist();
5208 for (i = 0; i < num_chunks; i++) {
5209 (void)sctp_auth_add_chunk(chunks->chunk_types[i],
5210 stcb->asoc.peer_auth_chunks);
5213 } else if ((ptype == SCTP_HEARTBEAT_INFO) ||
5214 (ptype == SCTP_STATE_COOKIE) ||
5215 (ptype == SCTP_UNRECOG_PARAM) ||
5216 (ptype == SCTP_COOKIE_PRESERVE) ||
5217 (ptype == SCTP_SUPPORTED_ADDRTYPE) ||
5218 (ptype == SCTP_ADD_IP_ADDRESS) ||
5219 (ptype == SCTP_DEL_IP_ADDRESS) ||
5220 (ptype == SCTP_ERROR_CAUSE_IND) ||
5221 (ptype == SCTP_SUCCESS_REPORT)) {
5224 if ((ptype & 0x8000) == 0x0000) {
5226 * must stop processing the rest of the
5227 * param's. Any report bits were handled
5229 * sctp_arethere_unrecognized_parameters()
5230 * when the INIT or INIT-ACK was first seen.
5236 offset += SCTP_SIZE32(plen);
5237 if (offset >= limit) {
5240 phdr = sctp_get_next_param(m, offset, &parm_buf,
5243 /* Now check to see if we need to purge any addresses */
5244 for (net = TAILQ_FIRST(&stcb->asoc.nets); net != NULL; net = net_tmp) {
5245 net_tmp = TAILQ_NEXT(net, sctp_next);
5246 if ((net->dest_state & SCTP_ADDR_NOT_IN_ASSOC) ==
5247 SCTP_ADDR_NOT_IN_ASSOC) {
5248 /* This address has been removed from the asoc */
5249 /* remove and free it */
5250 stcb->asoc.numnets--;
5251 TAILQ_REMOVE(&stcb->asoc.nets, net, sctp_next);
5252 sctp_free_remote_addr(net);
5253 if (net == stcb->asoc.primary_destination) {
5254 stcb->asoc.primary_destination = NULL;
5255 sctp_select_primary_destination(stcb);
5259 /* validate authentication required parameters */
5260 if (got_random && got_hmacs) {
5261 stcb->asoc.peer_supports_auth = 1;
5263 stcb->asoc.peer_supports_auth = 0;
5265 if (!stcb->asoc.peer_supports_auth && got_chklist) {
5266 /* peer does not support auth but sent a chunks list? */
5269 if (!sctp_asconf_auth_nochk && stcb->asoc.peer_supports_asconf &&
5270 !stcb->asoc.peer_supports_auth) {
5271 /* peer supports asconf but not auth? */
5274 /* concatenate the full random key */
5275 #ifdef SCTP_AUTH_DRAFT_04
5276 keylen = random_len;
5277 new_key = sctp_alloc_key(keylen);
5278 if (new_key != NULL) {
5279 /* copy in the RANDOM */
5280 if (p_random != NULL)
5281 bcopy(p_random->random_data, new_key->key, random_len);
5284 keylen = sizeof(*p_random) + random_len + sizeof(*chunks) + num_chunks +
5285 sizeof(*hmacs) + hmacs_len;
5286 new_key = sctp_alloc_key(keylen);
5287 if (new_key != NULL) {
5288 /* copy in the RANDOM */
5289 if (p_random != NULL) {
5290 keylen = sizeof(*p_random) + random_len;
5291 bcopy(p_random, new_key->key, keylen);
5293 /* append in the AUTH chunks */
5294 if (chunks != NULL) {
5295 bcopy(chunks, new_key->key + keylen,
5296 sizeof(*chunks) + num_chunks);
5297 keylen += sizeof(*chunks) + num_chunks;
5299 /* append in the HMACs */
5300 if (hmacs != NULL) {
5301 bcopy(hmacs, new_key->key + keylen,
5302 sizeof(*hmacs) + hmacs_len);
5307 /* failed to get memory for the key */
5310 if (stcb->asoc.authinfo.peer_random != NULL)
5311 sctp_free_key(stcb->asoc.authinfo.peer_random);
5312 stcb->asoc.authinfo.peer_random = new_key;
5313 #ifdef SCTP_AUTH_DRAFT_04
5314 /* don't include the chunks and hmacs for draft -04 */
5315 stcb->asoc.authinfo.peer_random->keylen = random_len;
5317 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid);
5318 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid);
5324 sctp_set_primary_addr(struct sctp_tcb *stcb, struct sockaddr *sa,
5325 struct sctp_nets *net)
5327 /* make sure the requested primary address exists in the assoc */
5328 if (net == NULL && sa)
5329 net = sctp_findnet(stcb, sa);
5332 /* didn't find the requested primary address! */
5335 /* set the primary address */
5336 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
5337 /* Must be confirmed, so queue to set */
5338 net->dest_state |= SCTP_ADDR_REQ_PRIMARY;
5341 stcb->asoc.primary_destination = net;
5342 net->dest_state &= ~SCTP_ADDR_WAS_PRIMARY;
5343 net = TAILQ_FIRST(&stcb->asoc.nets);
5344 if (net != stcb->asoc.primary_destination) {
5346 * first one on the list is NOT the primary
5347 * sctp_cmpaddr() is much more efficent if the
5348 * primary is the first on the list, make it so.
5350 TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
5351 TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
5359 sctp_is_vtag_good(struct sctp_inpcb *inp, uint32_t tag, struct timeval *now)
5362 * This function serves two purposes. It will see if a TAG can be
5363 * re-used and return 1 for yes it is ok and 0 for don't use that
5364 * tag. A secondary function it will do is purge out old tags that
5367 struct sctpasochead *head;
5368 struct sctpvtaghead *chain;
5369 struct sctp_tagblock *twait_block;
5370 struct sctp_tcb *stcb;
5373 SCTP_INP_INFO_WLOCK();
5374 chain = &sctppcbinfo.vtag_timewait[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
5375 /* First is the vtag in use ? */
5377 head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(tag,
5378 sctppcbinfo.hashasocmark)];
5382 LIST_FOREACH(stcb, head, sctp_asocs) {
5384 if (stcb->asoc.my_vtag == tag) {
5386 * We should remove this if and return 0 always if
5387 * we want vtags unique across all endpoints. For
5388 * now within a endpoint is ok.
5390 if (inp == stcb->sctp_ep) {
5391 /* bad tag, in use */
5392 SCTP_INP_INFO_WUNLOCK();
5398 /* Now lets check the restart hash */
5399 head = &sctppcbinfo.sctp_restarthash[SCTP_PCBHASH_ASOC(tag,
5400 sctppcbinfo.hashrestartmark)];
5402 goto check_time_wait;
5404 LIST_FOREACH(stcb, head, sctp_tcbrestarhash) {
5405 if (stcb->asoc.assoc_id == tag) {
5407 if (inp == stcb->sctp_ep) {
5408 /* bad tag, in use */
5409 SCTP_INP_INFO_WUNLOCK();
5415 /* Now what about timed wait ? */
5416 if (!SCTP_LIST_EMPTY(chain)) {
5418 * Block(s) are present, lets see if we have this tag in the
5421 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
5422 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
5423 if (twait_block->vtag_block[i].v_tag == 0) {
5426 } else if ((long)twait_block->vtag_block[i].tv_sec_at_expire >
5428 /* Audit expires this guy */
5429 twait_block->vtag_block[i].tv_sec_at_expire = 0;
5430 twait_block->vtag_block[i].v_tag = 0;
5431 } else if (twait_block->vtag_block[i].v_tag ==
5433 /* Bad tag, sorry :< */
5434 SCTP_INP_INFO_WUNLOCK();
5440 /* Not found, ok to use the tag */
5441 SCTP_INP_INFO_WUNLOCK();
5446 static sctp_assoc_t reneged_asoc_ids[256];
5447 static uint8_t reneged_at = 0;
5451 sctp_drain_mbufs(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
5454 * We must hunt this association for MBUF's past the cumack (i.e.
5455 * out of order data that we can renege on).
5457 struct sctp_association *asoc;
5458 struct sctp_tmit_chunk *chk, *nchk;
5459 uint32_t cumulative_tsn_p1, tsn;
5460 struct sctp_queued_to_read *ctl, *nctl;
5461 int cnt, strmat, gap;
5463 /* We look for anything larger than the cum-ack + 1 */
5465 SCTP_STAT_INCR(sctps_protocol_drain_calls);
5466 if (sctp_do_drain == 0) {
5470 if (asoc->cumulative_tsn == asoc->highest_tsn_inside_map) {
5471 /* none we can reneg on. */
5474 SCTP_STAT_INCR(sctps_protocol_drains_done);
5475 cumulative_tsn_p1 = asoc->cumulative_tsn + 1;
5477 /* First look in the re-assembly queue */
5478 chk = TAILQ_FIRST(&asoc->reasmqueue);
5480 /* Get the next one */
5481 nchk = TAILQ_NEXT(chk, sctp_next);
5482 if (compare_with_wrap(chk->rec.data.TSN_seq,
5483 cumulative_tsn_p1, MAX_TSN)) {
5484 /* Yep it is above cum-ack */
5486 tsn = chk->rec.data.TSN_seq;
5487 if (tsn >= asoc->mapping_array_base_tsn) {
5488 gap = tsn - asoc->mapping_array_base_tsn;
5490 gap = (MAX_TSN - asoc->mapping_array_base_tsn) +
5493 asoc->size_on_reasm_queue = sctp_sbspace_sub(asoc->size_on_reasm_queue, chk->send_size);
5494 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
5495 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
5496 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
5498 sctp_m_freem(chk->data);
5501 sctp_free_a_chunk(stcb, chk);
5505 /* Ok that was fun, now we will drain all the inbound streams? */
5506 for (strmat = 0; strmat < asoc->streamincnt; strmat++) {
5507 ctl = TAILQ_FIRST(&asoc->strmin[strmat].inqueue);
5509 nctl = TAILQ_NEXT(ctl, next);
5510 if (compare_with_wrap(ctl->sinfo_tsn,
5511 cumulative_tsn_p1, MAX_TSN)) {
5512 /* Yep it is above cum-ack */
5514 tsn = ctl->sinfo_tsn;
5515 if (tsn >= asoc->mapping_array_base_tsn) {
5517 asoc->mapping_array_base_tsn;
5520 asoc->mapping_array_base_tsn) +
5523 asoc->size_on_all_streams = sctp_sbspace_sub(asoc->size_on_all_streams, ctl->length);
5524 sctp_ucount_decr(asoc->cnt_on_all_streams);
5526 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array,
5528 TAILQ_REMOVE(&asoc->strmin[strmat].inqueue,
5531 sctp_m_freem(ctl->data);
5534 sctp_free_remote_addr(ctl->whoFrom);
5535 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_readq, ctl);
5536 SCTP_DECR_READQ_COUNT();
5542 * Question, should we go through the delivery queue? The only
5543 * reason things are on here is the app not reading OR a p-d-api up.
5544 * An attacker COULD send enough in to initiate the PD-API and then
5545 * send a bunch of stuff to other streams... these would wind up on
5546 * the delivery queue.. and then we would not get to them. But in
5547 * order to do this I then have to back-track and un-deliver
5548 * sequence numbers in streams.. el-yucko. I think for now we will
5549 * NOT look at the delivery queue and leave it to be something to
5550 * consider later. An alternative would be to abort the P-D-API with
5551 * a notification and then deliver the data.... Or another method
5552 * might be to keep track of how many times the situation occurs and
5553 * if we see a possible attack underway just abort the association.
5557 SCTPDBG(SCTP_DEBUG_PCB1, "Freed %d chunks from reneg harvest\n", cnt);
5562 * Now do we need to find a new
5563 * asoc->highest_tsn_inside_map?
5565 if (asoc->highest_tsn_inside_map >= asoc->mapping_array_base_tsn) {
5566 gap = asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn;
5568 gap = (MAX_TSN - asoc->mapping_array_base_tsn) +
5569 asoc->highest_tsn_inside_map + 1;
5571 if (gap >= (asoc->mapping_array_size << 3)) {
5573 * Something bad happened or cum-ack and high were
5574 * behind the base, but if so earlier checks should
5575 * have found NO data... wierd... we will start at
5576 * end of mapping array.
5578 SCTP_PRINTF("Gap was larger than array?? %d set to max:%d maparraymax:%x\n",
5580 (int)(asoc->mapping_array_size << 3),
5581 (int)asoc->highest_tsn_inside_map);
5582 gap = asoc->mapping_array_size << 3;
5585 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
5586 /* found the new highest */
5587 asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn + gap;
5593 /* Nothing left in map */
5594 memset(asoc->mapping_array, 0, asoc->mapping_array_size);
5595 asoc->mapping_array_base_tsn = asoc->cumulative_tsn + 1;
5596 asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
5598 asoc->last_revoke_count = cnt;
5599 (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
5600 sctp_send_sack(stcb);
5601 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_DRAIN);
5602 reneged_asoc_ids[reneged_at] = sctp_get_associd(stcb);
5606 * Another issue, in un-setting the TSN's in the mapping array we
5607 * DID NOT adjust the higest_tsn marker. This will cause one of two
5608 * things to occur. It may cause us to do extra work in checking for
5609 * our mapping array movement. More importantly it may cause us to
5610 * SACK every datagram. This may not be a bad thing though since we
5611 * will recover once we get our cum-ack above and all this stuff we
5620 * We must walk the PCB lists for ALL associations here. The system
5621 * is LOW on MBUF's and needs help. This is where reneging will
5622 * occur. We really hope this does NOT happen!
5624 struct sctp_inpcb *inp;
5625 struct sctp_tcb *stcb;
5627 SCTP_INP_INFO_RLOCK();
5628 LIST_FOREACH(inp, &sctppcbinfo.listhead, sctp_list) {
5629 /* For each endpoint */
5630 SCTP_INP_RLOCK(inp);
5631 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5632 /* For each association */
5633 SCTP_TCB_LOCK(stcb);
5634 sctp_drain_mbufs(inp, stcb);
5635 SCTP_TCB_UNLOCK(stcb);
5637 SCTP_INP_RUNLOCK(inp);
5639 SCTP_INP_INFO_RUNLOCK();
5643 * start a new iterator
5644 * iterates through all endpoints and associations based on the pcb_state
5645 * flags and asoc_state. "af" (mandatory) is executed for all matching
5646 * assocs and "ef" (optional) is executed when the iterator completes.
5647 * "inpf" (optional) is executed for each new endpoint as it is being
5648 * iterated through. inpe (optional) is called when the inp completes
5649 * its way through all the stcbs.
5652 sctp_initiate_iterator(inp_func inpf,
5656 uint32_t pcb_features,
5657 uint32_t asoc_state,
5661 struct sctp_inpcb *s_inp,
5662 uint8_t chunk_output_off)
5664 struct sctp_iterator *it = NULL;
5669 SCTP_MALLOC(it, struct sctp_iterator *, sizeof(struct sctp_iterator),
5674 memset(it, 0, sizeof(*it));
5675 it->function_assoc = af;
5676 it->function_inp = inpf;
5678 it->done_current_ep = 0;
5680 it->done_current_ep = 1;
5681 it->function_atend = ef;
5684 it->pcb_flags = pcb_state;
5685 it->pcb_features = pcb_features;
5686 it->asoc_state = asoc_state;
5687 it->function_inp_end = inpe;
5688 it->no_chunk_output = chunk_output_off;
5691 it->iterator_flags = SCTP_ITERATOR_DO_SINGLE_INP;
5693 SCTP_INP_INFO_RLOCK();
5694 it->inp = LIST_FIRST(&sctppcbinfo.listhead);
5696 SCTP_INP_INFO_RUNLOCK();
5697 it->iterator_flags = SCTP_ITERATOR_DO_ALL_INP;
5700 SCTP_IPI_ITERATOR_WQ_LOCK();
5702 SCTP_INP_INCR_REF(it->inp);
5704 TAILQ_INSERT_TAIL(&sctppcbinfo.iteratorhead, it, sctp_nxt_itr);
5705 #if defined(SCTP_USE_THREAD_BASED_ITERATOR)
5706 if (sctppcbinfo.iterator_running == 0) {
5707 sctp_wakeup_iterator();
5709 SCTP_IPI_ITERATOR_WQ_UNLOCK();
5712 SCTP_INP_DECR_REF(it->inp);
5713 SCTP_IPI_ITERATOR_WQ_UNLOCK();
5714 /* Init the timer */
5715 SCTP_OS_TIMER_INIT(&it->tmr.timer);
5716 /* add to the list of all iterators */
5717 sctp_timer_start(SCTP_TIMER_TYPE_ITERATOR, (struct sctp_inpcb *)it,
5720 /* sa_ignore MEMLEAK {memory is put on the tailq for the iterator} */