2 * Copyright (c) 2001-2007, 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 vrfid)
135 struct sctp_vrf *vrf = NULL;
136 struct sctp_vrflist *bucket;
138 /* First allocate the VRF structure */
139 vrf = sctp_find_vrf(vrfid);
141 /* Already allocated */
144 SCTP_MALLOC(vrf, struct sctp_vrf *, sizeof(struct sctp_vrf),
149 panic("No memory for VRF:%d", vrfid);
154 memset(vrf, 0, sizeof(struct sctp_vrf));
156 LIST_INIT(&vrf->ifnlist);
157 vrf->total_ifa_count = 0;
158 /* Add it to the hash table */
159 bucket = &sctppcbinfo.sctp_vrfhash[(vrfid & sctppcbinfo.hashvrfmark)];
160 LIST_INSERT_HEAD(bucket, vrf, next_vrf);
166 sctp_find_ifn(struct sctp_vrf *vrf, void *ifn, uint32_t ifn_index)
168 struct sctp_ifn *sctp_ifnp;
171 * We assume the lock is held for the addresses if thats wrong
172 * problems could occur :-)
174 LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
175 if (sctp_ifnp->ifn_index == ifn_index) {
178 if (sctp_ifnp->ifn_p && ifn && (sctp_ifnp->ifn_p == ifn)) {
186 sctp_find_vrf(uint32_t vrfid)
188 struct sctp_vrflist *bucket;
189 struct sctp_vrf *liste;
191 bucket = &sctppcbinfo.sctp_vrfhash[(vrfid & sctppcbinfo.hashvrfmark)];
192 LIST_FOREACH(liste, bucket, next_vrf) {
193 if (vrfid == liste->vrf_id) {
201 sctp_free_ifa(struct sctp_ifa *sctp_ifap)
205 ret = atomic_fetchadd_int(&sctp_ifap->refcount, -1);
207 /* We zero'd the count */
208 SCTP_FREE(sctp_ifap);
213 sctp_add_addr_to_vrf(uint32_t vrfid, void *ifn, uint32_t ifn_index,
214 uint32_t ifn_type, const char *if_name,
215 void *ifa, struct sockaddr *addr, uint32_t ifa_flags)
217 struct sctp_vrf *vrf;
218 struct sctp_ifn *sctp_ifnp = NULL;
219 struct sctp_ifa *sctp_ifap = NULL;
221 /* How granular do we need the locks to be here? */
222 SCTP_IPI_ADDR_LOCK();
223 vrf = sctp_find_vrf(vrfid);
225 vrf = sctp_allocate_vrf(vrfid);
227 SCTP_IPI_ADDR_UNLOCK();
231 sctp_ifnp = sctp_find_ifn(vrf, ifn, ifn_index);
232 if (sctp_ifnp == NULL) {
234 * build one and add it, can't hold lock until after malloc
237 SCTP_IPI_ADDR_UNLOCK();
238 SCTP_MALLOC(sctp_ifnp, struct sctp_ifn *, sizeof(struct sctp_ifn), "SCTP_IFN");
239 if (sctp_ifnp == NULL) {
241 panic("No memory for IFN:%u", sctp_ifnp->ifn_index);
245 sctp_ifnp->ifn_index = ifn_index;
246 sctp_ifnp->ifn_p = ifn;
247 sctp_ifnp->ifn_type = ifn_type;
248 sctp_ifnp->ifa_count = 0;
249 sctp_ifnp->refcount = 0;
250 sctp_ifnp->vrf = vrf;
251 memcpy(sctp_ifnp->ifn_name, if_name, SCTP_IFNAMSIZ);
252 LIST_INIT(&sctp_ifnp->ifalist);
253 SCTP_IPI_ADDR_LOCK();
254 LIST_INSERT_HEAD(&vrf->ifnlist, sctp_ifnp, next_ifn);
256 sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, 1);
258 /* Hmm, it already exists? */
259 if ((sctp_ifap->ifn_p) &&
260 (sctp_ifap->ifn_p->ifn_index == ifn_index)) {
261 if (sctp_ifap->localifa_flags & SCTP_BEING_DELETED) {
262 /* easy to solve, just switch back to active */
263 sctp_ifap->localifa_flags = SCTP_ADDR_VALID;
264 sctp_ifap->ifn_p = sctp_ifnp;
266 SCTP_IPI_ADDR_UNLOCK();
269 goto exit_stage_left;
272 if (sctp_ifap->ifn_p) {
274 * The first IFN gets the address,
275 * duplicates are ignored.
277 goto exit_stage_left;
279 /* repair ifnp which was NULL ? */
280 sctp_ifap->localifa_flags = SCTP_ADDR_VALID;
281 sctp_ifap->ifn_p = sctp_ifnp;
282 atomic_add_int(&sctp_ifnp->refcount, 1);
284 goto exit_stage_left;
287 SCTP_IPI_ADDR_UNLOCK();
288 SCTP_MALLOC(sctp_ifap, struct sctp_ifa *, sizeof(struct sctp_ifa), "SCTP_IFA");
289 if (sctp_ifap == NULL) {
291 panic("No memory for IFA");
295 memset(sctp_ifap, 0, sizeof(struct sctp_ifa));
296 sctp_ifap->ifn_p = sctp_ifnp;
297 atomic_add_int(&sctp_ifnp->refcount, 1);
299 sctp_ifap->ifa = ifa;
300 memcpy(&sctp_ifap->address, addr, addr->sa_len);
301 sctp_ifap->localifa_flags = SCTP_ADDR_VALID | SCTP_ADDR_DEFER_USE;
302 sctp_ifap->flags = ifa_flags;
304 if (sctp_ifap->address.sa.sa_family == AF_INET) {
305 struct sockaddr_in *sin;
307 sin = (struct sockaddr_in *)&sctp_ifap->address.sin;
308 if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) ||
309 (IN4_ISLOOPBACK_ADDRESS(&sin->sin_addr))) {
310 sctp_ifap->src_is_loop = 1;
312 if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
313 sctp_ifap->src_is_priv = 1;
315 } else if (sctp_ifap->address.sa.sa_family == AF_INET6) {
316 /* ok to use deprecated addresses? */
317 struct sockaddr_in6 *sin6;
319 sin6 = (struct sockaddr_in6 *)&sctp_ifap->address.sin6;
320 if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) ||
321 (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))) {
322 sctp_ifap->src_is_loop = 1;
324 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
325 sctp_ifap->src_is_priv = 1;
328 if ((sctp_ifap->src_is_priv == 0) &&
329 (sctp_ifap->src_is_loop == 0)) {
330 sctp_ifap->src_is_glob = 1;
332 SCTP_IPI_ADDR_LOCK();
333 sctp_ifap->refcount = 1;
334 LIST_INSERT_HEAD(&sctp_ifnp->ifalist, sctp_ifap, next_ifa);
335 sctp_ifnp->ifa_count++;
336 vrf->total_ifa_count++;
337 SCTP_IPI_ADDR_UNLOCK();
342 sctp_del_addr_from_vrf(uint32_t vrfid, struct sockaddr *addr,
345 struct sctp_vrf *vrf;
346 struct sctp_ifa *sctp_ifap = NULL;
347 struct sctp_ifn *sctp_ifnp = NULL;
349 SCTP_IPI_ADDR_LOCK();
351 vrf = sctp_find_vrf(vrfid);
353 printf("Can't find vrfid:%d\n", vrfid);
356 sctp_ifnp = sctp_find_ifn(vrf, (void *)NULL, ifn_index);
357 if (sctp_ifnp == NULL) {
358 sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, 1);
360 sctp_ifap = sctp_find_ifa_in_ifn(sctp_ifnp, addr, 1);
364 sctp_ifap->localifa_flags &= SCTP_ADDR_VALID;
365 sctp_ifap->localifa_flags |= SCTP_BEING_DELETED;
366 sctp_ifnp->ifa_count--;
367 vrf->total_ifa_count--;
368 LIST_REMOVE(sctp_ifap, next_ifa);
369 atomic_add_int(&sctp_ifnp->refcount, -1);
371 printf("Del Addr-ifn:%d Could not find address:",
373 sctp_print_address(addr);
376 SCTP_IPI_ADDR_UNLOCK();
381 * Notes on locks for FreeBSD 5 and up. All association lookups that have a
382 * definte ep, the INP structure is assumed to be locked for reading. If we
383 * need to go find the INP (ususally when a **inp is passed) then we must
384 * lock the INFO structure first and if needed lock the INP too. Note that if
391 * Given a endpoint, look and find in its association list any association
392 * with the "to" address given. This can be a "from" address, too, for
393 * inbound packets. For outbound packets it is a true "to" address.
396 static struct sctp_tcb *
397 sctp_tcb_special_locate(struct sctp_inpcb **inp_p, struct sockaddr *from,
398 struct sockaddr *to, struct sctp_nets **netp)
400 /**** ASSUMSES THE CALLER holds the INP_INFO_RLOCK */
403 * Note for this module care must be taken when observing what to is
404 * for. In most of the rest of the code the TO field represents my
405 * peer and the FROM field represents my address. For this module it
406 * is reversed of that.
409 * If we support the TCP model, then we must now dig through to see
410 * if we can find our endpoint in the list of tcp ep's.
412 uint16_t lport, rport;
413 struct sctppcbhead *ephead;
414 struct sctp_inpcb *inp;
415 struct sctp_laddr *laddr;
416 struct sctp_tcb *stcb;
417 struct sctp_nets *net;
419 if ((to == NULL) || (from == NULL)) {
422 if (to->sa_family == AF_INET && from->sa_family == AF_INET) {
423 lport = ((struct sockaddr_in *)to)->sin_port;
424 rport = ((struct sockaddr_in *)from)->sin_port;
425 } else if (to->sa_family == AF_INET6 && from->sa_family == AF_INET6) {
426 lport = ((struct sockaddr_in6 *)to)->sin6_port;
427 rport = ((struct sockaddr_in6 *)from)->sin6_port;
431 ephead = &sctppcbinfo.sctp_tcpephash[SCTP_PCBHASH_ALLADDR(
432 (lport + rport), sctppcbinfo.hashtcpmark)];
434 * Ok now for each of the guys in this bucket we must look and see:
435 * - Does the remote port match. - Does there single association's
436 * addresses match this address (to). If so we update p_ep to point
437 * to this ep and return the tcb from it.
439 LIST_FOREACH(inp, ephead, sctp_hash) {
440 if (lport != inp->sctp_lport) {
444 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
445 SCTP_INP_RUNLOCK(inp);
448 /* check to see if the ep has one of the addresses */
449 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
450 /* We are NOT bound all, so look further */
453 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
455 if (laddr->ifa == NULL) {
457 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
458 printf("An ounce of prevention is worth a pound of cure\n");
463 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
465 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
466 printf("ifa being deleted\n");
471 if (laddr->ifa->address.sa.sa_family ==
473 /* see if it matches */
474 struct sockaddr_in *intf_addr, *sin;
476 intf_addr = &laddr->ifa->address.sin;
477 sin = (struct sockaddr_in *)to;
478 if (from->sa_family == AF_INET) {
479 if (sin->sin_addr.s_addr ==
480 intf_addr->sin_addr.s_addr) {
485 struct sockaddr_in6 *intf_addr6;
486 struct sockaddr_in6 *sin6;
488 sin6 = (struct sockaddr_in6 *)
490 intf_addr6 = &laddr->ifa->address.sin6;
492 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
493 &intf_addr6->sin6_addr)) {
501 /* This endpoint does not have this address */
502 SCTP_INP_RUNLOCK(inp);
507 * Ok if we hit here the ep has the address, does it hold
511 stcb = LIST_FIRST(&inp->sctp_asoc_list);
513 SCTP_INP_RUNLOCK(inp);
517 if (stcb->rport != rport) {
518 /* remote port does not match. */
519 SCTP_TCB_UNLOCK(stcb);
520 SCTP_INP_RUNLOCK(inp);
523 /* Does this TCB have a matching address? */
524 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
526 if (net->ro._l_addr.sa.sa_family != from->sa_family) {
527 /* not the same family, can't be a match */
530 if (from->sa_family == AF_INET) {
531 struct sockaddr_in *sin, *rsin;
533 sin = (struct sockaddr_in *)&net->ro._l_addr;
534 rsin = (struct sockaddr_in *)from;
535 if (sin->sin_addr.s_addr ==
536 rsin->sin_addr.s_addr) {
541 /* Update the endpoint pointer */
543 SCTP_INP_RUNLOCK(inp);
547 struct sockaddr_in6 *sin6, *rsin6;
549 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
550 rsin6 = (struct sockaddr_in6 *)from;
551 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
552 &rsin6->sin6_addr)) {
557 /* Update the endpoint pointer */
559 SCTP_INP_RUNLOCK(inp);
564 SCTP_TCB_UNLOCK(stcb);
565 SCTP_INP_RUNLOCK(inp);
573 * 1) If I return a NULL you must decrement any INP ref cnt. 2) If I find an
574 * stcb, both will be locked (locked_tcb and stcb) but decrement will be done
575 * (if locked == NULL). 3) Decrement happens on return ONLY if locked ==
580 sctp_findassociation_ep_addr(struct sctp_inpcb **inp_p, struct sockaddr *remote,
581 struct sctp_nets **netp, struct sockaddr *local, struct sctp_tcb *locked_tcb)
583 struct sctpasochead *head;
584 struct sctp_inpcb *inp;
585 struct sctp_tcb *stcb;
586 struct sctp_nets *net;
590 if (remote->sa_family == AF_INET) {
591 rport = (((struct sockaddr_in *)remote)->sin_port);
592 } else if (remote->sa_family == AF_INET6) {
593 rport = (((struct sockaddr_in6 *)remote)->sin6_port);
599 * UN-lock so we can do proper locking here this occurs when
600 * called from load_addresses_from_init.
602 SCTP_TCB_UNLOCK(locked_tcb);
604 SCTP_INP_INFO_RLOCK();
605 if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
607 * Now either this guy is our listener or it's the
608 * connector. If it is the one that issued the connect, then
609 * it's only chance is to be the first TCB in the list. If
610 * it is the acceptor, then do the special_lookup to hash
611 * and find the real inp.
613 if ((inp->sctp_socket) && (inp->sctp_socket->so_qlimit)) {
614 /* to is peer addr, from is my addr */
615 stcb = sctp_tcb_special_locate(inp_p, remote, local,
617 if ((stcb != NULL) && (locked_tcb == NULL)) {
618 /* we have a locked tcb, lower refcount */
620 SCTP_INP_DECR_REF(inp);
621 SCTP_INP_WUNLOCK(inp);
623 if ((locked_tcb != NULL) && (locked_tcb != stcb)) {
624 SCTP_INP_RLOCK(locked_tcb->sctp_ep);
625 SCTP_TCB_LOCK(locked_tcb);
626 SCTP_INP_RUNLOCK(locked_tcb->sctp_ep);
628 SCTP_INP_INFO_RUNLOCK();
632 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
635 stcb = LIST_FIRST(&inp->sctp_asoc_list);
640 if (stcb->rport != rport) {
641 /* remote port does not match. */
642 SCTP_TCB_UNLOCK(stcb);
645 /* now look at the list of remote addresses */
646 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
648 if (net == (TAILQ_NEXT(net, sctp_next))) {
649 panic("Corrupt net list");
652 if (net->ro._l_addr.sa.sa_family !=
654 /* not the same family */
657 if (remote->sa_family == AF_INET) {
658 struct sockaddr_in *sin, *rsin;
660 sin = (struct sockaddr_in *)
662 rsin = (struct sockaddr_in *)remote;
663 if (sin->sin_addr.s_addr ==
664 rsin->sin_addr.s_addr) {
669 if (locked_tcb == NULL) {
670 SCTP_INP_DECR_REF(inp);
671 } else if (locked_tcb != stcb) {
672 SCTP_TCB_LOCK(locked_tcb);
674 SCTP_INP_WUNLOCK(inp);
675 SCTP_INP_INFO_RUNLOCK();
678 } else if (remote->sa_family == AF_INET6) {
679 struct sockaddr_in6 *sin6, *rsin6;
681 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
682 rsin6 = (struct sockaddr_in6 *)remote;
683 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
684 &rsin6->sin6_addr)) {
689 if (locked_tcb == NULL) {
690 SCTP_INP_DECR_REF(inp);
691 } else if (locked_tcb != stcb) {
692 SCTP_TCB_LOCK(locked_tcb);
694 SCTP_INP_WUNLOCK(inp);
695 SCTP_INP_INFO_RUNLOCK();
700 SCTP_TCB_UNLOCK(stcb);
704 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
707 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(rport,
708 inp->sctp_hashmark)];
712 LIST_FOREACH(stcb, head, sctp_tcbhash) {
713 if (stcb->rport != rport) {
714 /* remote port does not match */
717 /* now look at the list of remote addresses */
719 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
721 if (net == (TAILQ_NEXT(net, sctp_next))) {
722 panic("Corrupt net list");
725 if (net->ro._l_addr.sa.sa_family !=
727 /* not the same family */
730 if (remote->sa_family == AF_INET) {
731 struct sockaddr_in *sin, *rsin;
733 sin = (struct sockaddr_in *)
735 rsin = (struct sockaddr_in *)remote;
736 if (sin->sin_addr.s_addr ==
737 rsin->sin_addr.s_addr) {
742 if (locked_tcb == NULL) {
743 SCTP_INP_DECR_REF(inp);
744 } else if (locked_tcb != stcb) {
745 SCTP_TCB_LOCK(locked_tcb);
747 SCTP_INP_WUNLOCK(inp);
748 SCTP_INP_INFO_RUNLOCK();
751 } else if (remote->sa_family == AF_INET6) {
752 struct sockaddr_in6 *sin6, *rsin6;
754 sin6 = (struct sockaddr_in6 *)
756 rsin6 = (struct sockaddr_in6 *)remote;
757 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
758 &rsin6->sin6_addr)) {
763 if (locked_tcb == NULL) {
764 SCTP_INP_DECR_REF(inp);
765 } else if (locked_tcb != stcb) {
766 SCTP_TCB_LOCK(locked_tcb);
768 SCTP_INP_WUNLOCK(inp);
769 SCTP_INP_INFO_RUNLOCK();
774 SCTP_TCB_UNLOCK(stcb);
778 /* clean up for returning null */
780 SCTP_TCB_LOCK(locked_tcb);
782 SCTP_INP_WUNLOCK(inp);
783 SCTP_INP_INFO_RUNLOCK();
789 * Find an association for a specific endpoint using the association id given
790 * out in the COMM_UP notification
794 sctp_findassociation_ep_asocid(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock)
797 * Use my the assoc_id to find a endpoint
799 struct sctpasochead *head;
800 struct sctp_tcb *stcb;
803 if (asoc_id == 0 || inp == NULL) {
806 SCTP_INP_INFO_RLOCK();
807 id = (uint32_t) asoc_id;
808 head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(id,
809 sctppcbinfo.hashasocmark)];
811 /* invalid id TSNH */
812 SCTP_INP_INFO_RUNLOCK();
815 LIST_FOREACH(stcb, head, sctp_asocs) {
816 SCTP_INP_RLOCK(stcb->sctp_ep);
817 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
818 SCTP_INP_RUNLOCK(stcb->sctp_ep);
819 SCTP_INP_INFO_RUNLOCK();
822 if (stcb->asoc.assoc_id == id) {
824 if (inp != stcb->sctp_ep) {
826 * some other guy has the same id active (id
829 SCTP_INP_RUNLOCK(stcb->sctp_ep);
835 SCTP_INP_RUNLOCK(stcb->sctp_ep);
836 SCTP_INP_INFO_RUNLOCK();
839 SCTP_INP_RUNLOCK(stcb->sctp_ep);
841 /* Ok if we missed here, lets try the restart hash */
842 head = &sctppcbinfo.sctp_restarthash[SCTP_PCBHASH_ASOC(id, sctppcbinfo.hashrestartmark)];
844 /* invalid id TSNH */
845 SCTP_INP_INFO_RUNLOCK();
848 LIST_FOREACH(stcb, head, sctp_tcbrestarhash) {
849 SCTP_INP_RLOCK(stcb->sctp_ep);
850 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
851 SCTP_INP_RUNLOCK(stcb->sctp_ep);
852 SCTP_INP_INFO_RUNLOCK();
856 SCTP_INP_RUNLOCK(stcb->sctp_ep);
857 if (stcb->asoc.assoc_id == id) {
859 if (inp != stcb->sctp_ep) {
861 * some other guy has the same id active (id
864 SCTP_TCB_UNLOCK(stcb);
867 SCTP_INP_INFO_RUNLOCK();
870 SCTP_TCB_UNLOCK(stcb);
872 SCTP_INP_INFO_RUNLOCK();
877 static struct sctp_inpcb *
878 sctp_endpoint_probe(struct sockaddr *nam, struct sctppcbhead *head,
879 uint16_t lport, uint32_t vrf_id)
881 struct sctp_inpcb *inp;
882 struct sockaddr_in *sin;
883 struct sockaddr_in6 *sin6;
884 struct sctp_laddr *laddr;
888 * Endpoing probe expects that the INP_INFO is locked.
890 if (nam->sa_family == AF_INET) {
891 sin = (struct sockaddr_in *)nam;
893 } else if (nam->sa_family == AF_INET6) {
894 sin6 = (struct sockaddr_in6 *)nam;
897 /* unsupported family */
902 LIST_FOREACH(inp, head, sctp_hash) {
904 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
905 SCTP_INP_RUNLOCK(inp);
908 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) &&
909 (inp->sctp_lport == lport)) {
911 if ((nam->sa_family == AF_INET) &&
912 (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
913 SCTP_IPV6_V6ONLY(inp)) {
914 /* IPv4 on a IPv6 socket with ONLY IPv6 set */
915 SCTP_INP_RUNLOCK(inp);
918 /* A V6 address and the endpoint is NOT bound V6 */
919 if (nam->sa_family == AF_INET6 &&
920 (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
921 SCTP_INP_RUNLOCK(inp);
924 /* does a VRF id match? */
926 if (inp->def_vrf_id == vrf_id)
929 SCTP_INP_RUNLOCK(inp);
934 SCTP_INP_RUNLOCK(inp);
937 if ((nam->sa_family == AF_INET) &&
938 (sin->sin_addr.s_addr == INADDR_ANY)) {
939 /* Can't hunt for one that has no address specified */
941 } else if ((nam->sa_family == AF_INET6) &&
942 (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))) {
943 /* Can't hunt for one that has no address specified */
947 * ok, not bound to all so see if we can find a EP bound to this
950 LIST_FOREACH(inp, head, sctp_hash) {
952 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
953 SCTP_INP_RUNLOCK(inp);
956 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)) {
957 SCTP_INP_RUNLOCK(inp);
961 * Ok this could be a likely candidate, look at all of its
964 if (inp->sctp_lport != lport) {
965 SCTP_INP_RUNLOCK(inp);
968 /* does a VRF id match? */
970 if (inp->def_vrf_id == vrf_id)
974 SCTP_INP_RUNLOCK(inp);
977 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
978 if (laddr->ifa == NULL) {
980 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
981 printf("An ounce of prevention is worth a pound of cure\n");
987 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
988 printf("Ok laddr->ifa:%p is possible, ",
992 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
994 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
995 printf("Huh IFA being deleted\n");
1000 if (laddr->ifa->address.sa.sa_family == nam->sa_family) {
1001 /* possible, see if it matches */
1002 struct sockaddr_in *intf_addr;
1004 intf_addr = &laddr->ifa->address.sin;
1005 if (nam->sa_family == AF_INET) {
1006 if (sin->sin_addr.s_addr ==
1007 intf_addr->sin_addr.s_addr) {
1008 SCTP_INP_RUNLOCK(inp);
1011 } else if (nam->sa_family == AF_INET6) {
1012 struct sockaddr_in6 *intf_addr6;
1014 intf_addr6 = &laddr->ifa->address.sin6;
1015 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
1016 &intf_addr6->sin6_addr)) {
1017 SCTP_INP_RUNLOCK(inp);
1023 SCTP_INP_RUNLOCK(inp);
1030 sctp_pcb_findep(struct sockaddr *nam, int find_tcp_pool, int have_lock, uint32_t vrf_id)
1033 * First we check the hash table to see if someone has this port
1034 * bound with just the port.
1036 struct sctp_inpcb *inp;
1037 struct sctppcbhead *head;
1038 struct sockaddr_in *sin;
1039 struct sockaddr_in6 *sin6;
1042 if (nam->sa_family == AF_INET) {
1043 sin = (struct sockaddr_in *)nam;
1044 lport = ((struct sockaddr_in *)nam)->sin_port;
1045 } else if (nam->sa_family == AF_INET6) {
1046 sin6 = (struct sockaddr_in6 *)nam;
1047 lport = ((struct sockaddr_in6 *)nam)->sin6_port;
1049 /* unsupported family */
1053 * I could cheat here and just cast to one of the types but we will
1054 * do it right. It also provides the check against an Unsupported
1057 /* Find the head of the ALLADDR chain */
1058 if (have_lock == 0) {
1059 SCTP_INP_INFO_RLOCK();
1062 head = &sctppcbinfo.sctp_ephash[SCTP_PCBHASH_ALLADDR(lport,
1063 sctppcbinfo.hashmark)];
1064 inp = sctp_endpoint_probe(nam, head, lport, vrf_id);
1067 * If the TCP model exists it could be that the main listening
1068 * endpoint is gone but there exists a connected socket for this guy
1069 * yet. If so we can return the first one that we find. This may NOT
1070 * be the correct one but the sctp_findassociation_ep_addr has
1071 * further code to look at all TCP models.
1073 if (inp == NULL && find_tcp_pool) {
1076 for (i = 0; i < sctppcbinfo.hashtblsize; i++) {
1078 * This is real gross, but we do NOT have a remote
1079 * port at this point depending on who is calling.
1080 * We must therefore look for ANY one that matches
1083 head = &sctppcbinfo.sctp_tcpephash[i];
1084 if (LIST_FIRST(head)) {
1085 inp = sctp_endpoint_probe(nam, head, lport, vrf_id);
1094 SCTP_INP_INCR_REF(inp);
1096 if (have_lock == 0) {
1097 SCTP_INP_INFO_RUNLOCK();
1103 * Find an association for an endpoint with the pointer to whom you want to
1104 * send to and the endpoint pointer. The address can be IPv4 or IPv6. We may
1105 * need to change the *to to some other struct like a mbuf...
1108 sctp_findassociation_addr_sa(struct sockaddr *to, struct sockaddr *from,
1109 struct sctp_inpcb **inp_p, struct sctp_nets **netp, int find_tcp_pool, uint32_t vrf_id)
1111 struct sctp_inpcb *inp;
1112 struct sctp_tcb *retval;
1114 SCTP_INP_INFO_RLOCK();
1115 if (find_tcp_pool) {
1116 if (inp_p != NULL) {
1117 retval = sctp_tcb_special_locate(inp_p, from, to, netp);
1119 retval = sctp_tcb_special_locate(&inp, from, to, netp);
1121 if (retval != NULL) {
1122 SCTP_INP_INFO_RUNLOCK();
1126 inp = sctp_pcb_findep(to, 0, 1, vrf_id);
1127 if (inp_p != NULL) {
1130 SCTP_INP_INFO_RUNLOCK();
1136 * ok, we have an endpoint, now lets find the assoc for it (if any)
1137 * we now place the source address or from in the to of the find
1138 * endpoint call. Since in reality this chain is used from the
1139 * inbound packet side.
1141 if (inp_p != NULL) {
1142 retval = sctp_findassociation_ep_addr(inp_p, from, netp, to, NULL);
1144 retval = sctp_findassociation_ep_addr(&inp, from, netp, to, NULL);
1151 * This routine will grub through the mbuf that is a INIT or INIT-ACK and
1152 * find all addresses that the sender has specified in any address list. Each
1153 * address will be used to lookup the TCB and see if one exits.
1155 static struct sctp_tcb *
1156 sctp_findassociation_special_addr(struct mbuf *m, int iphlen, int offset,
1157 struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp,
1158 struct sockaddr *dest)
1160 struct sockaddr_in sin4;
1161 struct sockaddr_in6 sin6;
1162 struct sctp_paramhdr *phdr, parm_buf;
1163 struct sctp_tcb *retval;
1164 uint32_t ptype, plen;
1166 memset(&sin4, 0, sizeof(sin4));
1167 memset(&sin6, 0, sizeof(sin6));
1168 sin4.sin_len = sizeof(sin4);
1169 sin4.sin_family = AF_INET;
1170 sin4.sin_port = sh->src_port;
1171 sin6.sin6_len = sizeof(sin6);
1172 sin6.sin6_family = AF_INET6;
1173 sin6.sin6_port = sh->src_port;
1176 offset += sizeof(struct sctp_init_chunk);
1178 phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
1179 while (phdr != NULL) {
1180 /* now we must see if we want the parameter */
1181 ptype = ntohs(phdr->param_type);
1182 plen = ntohs(phdr->param_length);
1186 if (ptype == SCTP_IPV4_ADDRESS &&
1187 plen == sizeof(struct sctp_ipv4addr_param)) {
1188 /* Get the rest of the address */
1189 struct sctp_ipv4addr_param ip4_parm, *p4;
1191 phdr = sctp_get_next_param(m, offset,
1192 (struct sctp_paramhdr *)&ip4_parm, plen);
1196 p4 = (struct sctp_ipv4addr_param *)phdr;
1197 memcpy(&sin4.sin_addr, &p4->addr, sizeof(p4->addr));
1199 retval = sctp_findassociation_ep_addr(inp_p,
1200 (struct sockaddr *)&sin4, netp, dest, NULL);
1201 if (retval != NULL) {
1204 } else if (ptype == SCTP_IPV6_ADDRESS &&
1205 plen == sizeof(struct sctp_ipv6addr_param)) {
1206 /* Get the rest of the address */
1207 struct sctp_ipv6addr_param ip6_parm, *p6;
1209 phdr = sctp_get_next_param(m, offset,
1210 (struct sctp_paramhdr *)&ip6_parm, plen);
1214 p6 = (struct sctp_ipv6addr_param *)phdr;
1215 memcpy(&sin6.sin6_addr, &p6->addr, sizeof(p6->addr));
1217 retval = sctp_findassociation_ep_addr(inp_p,
1218 (struct sockaddr *)&sin6, netp, dest, NULL);
1219 if (retval != NULL) {
1223 offset += SCTP_SIZE32(plen);
1224 phdr = sctp_get_next_param(m, offset, &parm_buf,
1231 static struct sctp_tcb *
1232 sctp_findassoc_by_vtag(struct sockaddr *from, uint32_t vtag,
1233 struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint16_t rport,
1234 uint16_t lport, int skip_src_check)
1237 * Use my vtag to hash. If we find it we then verify the source addr
1238 * is in the assoc. If all goes well we save a bit on rec of a
1241 struct sctpasochead *head;
1242 struct sctp_nets *net;
1243 struct sctp_tcb *stcb;
1247 SCTP_INP_INFO_RLOCK();
1248 head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(vtag,
1249 sctppcbinfo.hashasocmark)];
1252 SCTP_INP_INFO_RUNLOCK();
1255 LIST_FOREACH(stcb, head, sctp_asocs) {
1256 SCTP_INP_RLOCK(stcb->sctp_ep);
1257 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1258 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1259 SCTP_INP_INFO_RUNLOCK();
1262 SCTP_TCB_LOCK(stcb);
1263 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1264 if (stcb->asoc.my_vtag == vtag) {
1266 if (stcb->rport != rport) {
1268 * we could remove this if vtags are unique
1269 * across the system.
1271 SCTP_TCB_UNLOCK(stcb);
1274 if (stcb->sctp_ep->sctp_lport != lport) {
1276 * we could remove this if vtags are unique
1277 * across the system.
1279 SCTP_TCB_UNLOCK(stcb);
1282 if (skip_src_check) {
1283 *netp = NULL; /* unknown */
1284 *inp_p = stcb->sctp_ep;
1285 SCTP_INP_INFO_RUNLOCK();
1288 net = sctp_findnet(stcb, from);
1292 SCTP_STAT_INCR(sctps_vtagexpress);
1293 *inp_p = stcb->sctp_ep;
1294 SCTP_INP_INFO_RUNLOCK();
1298 * not him, this should only happen in rare
1299 * cases so I peg it.
1301 SCTP_STAT_INCR(sctps_vtagbogus);
1304 SCTP_TCB_UNLOCK(stcb);
1306 SCTP_INP_INFO_RUNLOCK();
1311 * Find an association with the pointer to the inbound IP packet. This can be
1312 * a IPv4 or IPv6 packet.
1315 sctp_findassociation_addr(struct mbuf *m, int iphlen, int offset,
1316 struct sctphdr *sh, struct sctp_chunkhdr *ch,
1317 struct sctp_inpcb **inp_p, struct sctp_nets **netp)
1321 struct sctp_tcb *retval;
1322 struct sockaddr_storage to_store, from_store;
1323 struct sockaddr *to = (struct sockaddr *)&to_store;
1324 struct sockaddr *from = (struct sockaddr *)&from_store;
1325 struct sctp_inpcb *inp;
1328 vrf_id = SCTP_DEFAULT_VRFID;
1329 iph = mtod(m, struct ip *);
1330 if (iph->ip_v == IPVERSION) {
1332 struct sockaddr_in *from4;
1334 from4 = (struct sockaddr_in *)&from_store;
1335 bzero(from4, sizeof(*from4));
1336 from4->sin_family = AF_INET;
1337 from4->sin_len = sizeof(struct sockaddr_in);
1338 from4->sin_addr.s_addr = iph->ip_src.s_addr;
1339 from4->sin_port = sh->src_port;
1340 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
1342 struct ip6_hdr *ip6;
1343 struct sockaddr_in6 *from6;
1345 ip6 = mtod(m, struct ip6_hdr *);
1346 from6 = (struct sockaddr_in6 *)&from_store;
1347 bzero(from6, sizeof(*from6));
1348 from6->sin6_family = AF_INET6;
1349 from6->sin6_len = sizeof(struct sockaddr_in6);
1350 from6->sin6_addr = ip6->ip6_src;
1351 from6->sin6_port = sh->src_port;
1352 /* Get the scopes in properly to the sin6 addr's */
1353 /* we probably don't need these operations */
1354 (void)sa6_recoverscope(from6);
1355 sa6_embedscope(from6, ip6_use_defzone);
1357 /* Currently not supported. */
1361 /* we only go down this path if vtag is non-zero */
1362 retval = sctp_findassoc_by_vtag(from, ntohl(sh->v_tag),
1363 inp_p, netp, sh->src_port, sh->dest_port, 0);
1368 if (iph->ip_v == IPVERSION) {
1370 struct sockaddr_in *to4;
1372 to4 = (struct sockaddr_in *)&to_store;
1373 bzero(to4, sizeof(*to4));
1374 to4->sin_family = AF_INET;
1375 to4->sin_len = sizeof(struct sockaddr_in);
1376 to4->sin_addr.s_addr = iph->ip_dst.s_addr;
1377 to4->sin_port = sh->dest_port;
1378 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
1380 struct ip6_hdr *ip6;
1381 struct sockaddr_in6 *to6;
1383 ip6 = mtod(m, struct ip6_hdr *);
1384 to6 = (struct sockaddr_in6 *)&to_store;
1385 bzero(to6, sizeof(*to6));
1386 to6->sin6_family = AF_INET6;
1387 to6->sin6_len = sizeof(struct sockaddr_in6);
1388 to6->sin6_addr = ip6->ip6_dst;
1389 to6->sin6_port = sh->dest_port;
1390 /* Get the scopes in properly to the sin6 addr's */
1391 /* we probably don't need these operations */
1392 (void)sa6_recoverscope(to6);
1393 sa6_embedscope(to6, ip6_use_defzone);
1397 * FIX FIX?, I think we only need to look in the TCP pool if its an
1398 * INIT or COOKIE-ECHO, We really don't need to find it that way if
1399 * its a INIT-ACK or COOKIE_ACK since these in bot one-2-one and
1400 * one-2-N would be in the main pool anyway.
1402 if ((ch->chunk_type != SCTP_INITIATION) &&
1403 (ch->chunk_type != SCTP_INITIATION_ACK) &&
1404 (ch->chunk_type != SCTP_COOKIE_ACK) &&
1405 (ch->chunk_type != SCTP_COOKIE_ECHO)) {
1406 /* Other chunk types go to the tcp pool. */
1410 retval = sctp_findassociation_addr_sa(to, from, inp_p, netp,
1411 find_tcp_pool, vrf_id);
1414 retval = sctp_findassociation_addr_sa(to, from, &inp, netp,
1415 find_tcp_pool, vrf_id);
1418 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1419 printf("retval:%p inp:%p\n", retval, inp);
1422 if (retval == NULL && inp) {
1423 /* Found a EP but not this address */
1424 if ((ch->chunk_type == SCTP_INITIATION) ||
1425 (ch->chunk_type == SCTP_INITIATION_ACK)) {
1427 * special hook, we do NOT return linp or an
1428 * association that is linked to an existing
1429 * association that is under the TCP pool (i.e. no
1430 * listener exists). The endpoint finding routine
1431 * will always find a listner before examining the
1434 if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
1440 retval = sctp_findassociation_special_addr(m, iphlen,
1441 offset, sh, &inp, netp, to);
1442 if (inp_p != NULL) {
1448 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1449 printf("retval is %p\n", retval);
1456 * lookup an association by an ASCONF lookup address.
1457 * if the lookup address is 0.0.0.0 or ::0, use the vtag to do the lookup
1460 sctp_findassociation_ep_asconf(struct mbuf *m, int iphlen, int offset,
1461 struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp)
1463 struct sctp_tcb *stcb;
1464 struct sockaddr_in *sin;
1465 struct sockaddr_in6 *sin6;
1466 struct sockaddr_storage local_store, remote_store;
1468 struct sctp_paramhdr parm_buf, *phdr;
1470 int zero_address = 0;
1473 memset(&local_store, 0, sizeof(local_store));
1474 memset(&remote_store, 0, sizeof(remote_store));
1476 /* First get the destination address setup too. */
1477 iph = mtod(m, struct ip *);
1478 if (iph->ip_v == IPVERSION) {
1480 sin = (struct sockaddr_in *)&local_store;
1481 sin->sin_family = AF_INET;
1482 sin->sin_len = sizeof(*sin);
1483 sin->sin_port = sh->dest_port;
1484 sin->sin_addr.s_addr = iph->ip_dst.s_addr;
1485 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
1487 struct ip6_hdr *ip6;
1489 ip6 = mtod(m, struct ip6_hdr *);
1490 sin6 = (struct sockaddr_in6 *)&local_store;
1491 sin6->sin6_family = AF_INET6;
1492 sin6->sin6_len = sizeof(*sin6);
1493 sin6->sin6_port = sh->dest_port;
1494 sin6->sin6_addr = ip6->ip6_dst;
1499 phdr = sctp_get_next_param(m, offset + sizeof(struct sctp_asconf_chunk),
1500 &parm_buf, sizeof(struct sctp_paramhdr));
1503 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
1504 printf("findassociation_ep_asconf: failed to get asconf lookup addr\n");
1506 #endif /* SCTP_DEBUG */
1509 ptype = (int)((uint32_t) ntohs(phdr->param_type));
1510 /* get the correlation address */
1511 if (ptype == SCTP_IPV6_ADDRESS) {
1512 /* ipv6 address param */
1513 struct sctp_ipv6addr_param *p6, p6_buf;
1515 if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv6addr_param)) {
1518 p6 = (struct sctp_ipv6addr_param *)sctp_get_next_param(m,
1519 offset + sizeof(struct sctp_asconf_chunk),
1520 &p6_buf.ph, sizeof(*p6));
1523 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
1524 printf("findassociation_ep_asconf: failed to get asconf v6 lookup addr\n");
1526 #endif /* SCTP_DEBUG */
1529 sin6 = (struct sockaddr_in6 *)&remote_store;
1530 sin6->sin6_family = AF_INET6;
1531 sin6->sin6_len = sizeof(*sin6);
1532 sin6->sin6_port = sh->src_port;
1533 memcpy(&sin6->sin6_addr, &p6->addr, sizeof(struct in6_addr));
1534 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
1536 } else if (ptype == SCTP_IPV4_ADDRESS) {
1537 /* ipv4 address param */
1538 struct sctp_ipv4addr_param *p4, p4_buf;
1540 if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv4addr_param)) {
1543 p4 = (struct sctp_ipv4addr_param *)sctp_get_next_param(m,
1544 offset + sizeof(struct sctp_asconf_chunk),
1545 &p4_buf.ph, sizeof(*p4));
1548 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
1549 printf("findassociation_ep_asconf: failed to get asconf v4 lookup addr\n");
1551 #endif /* SCTP_DEBUG */
1554 sin = (struct sockaddr_in *)&remote_store;
1555 sin->sin_family = AF_INET;
1556 sin->sin_len = sizeof(*sin);
1557 sin->sin_port = sh->src_port;
1558 memcpy(&sin->sin_addr, &p4->addr, sizeof(struct in_addr));
1559 if (sin->sin_addr.s_addr == INADDR_ANY)
1562 /* invalid address param type */
1567 stcb = sctp_findassoc_by_vtag(NULL, ntohl(sh->v_tag), inp_p,
1568 netp, sh->src_port, sh->dest_port, 1);
1570 * printf("findassociation_ep_asconf: zero lookup address
1571 * finds stcb 0x%x\n", (uint32_t)stcb);
1574 stcb = sctp_findassociation_ep_addr(inp_p,
1575 (struct sockaddr *)&remote_store, netp,
1576 (struct sockaddr *)&local_store, NULL);
1583 * allocate a sctp_inpcb and setup a temporary binding to a port/all
1584 * addresses. This way if we don't get a bind we by default pick a ephemeral
1585 * port with all addresses bound.
1588 sctp_inpcb_alloc(struct socket *so)
1591 * we get called when a new endpoint starts up. We need to allocate
1592 * the sctp_inpcb structure from the zone and init it. Mark it as
1593 * unbound and find a port that we can use as an ephemeral with
1594 * INADDR_ANY. If the user binds later no problem we can then add in
1595 * the specific addresses. And setup the default parameters for the
1599 struct sctp_inpcb *inp;
1602 struct timeval time;
1603 sctp_sharedkey_t *null_key;
1607 SCTP_INP_INFO_WLOCK();
1608 inp = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_ep, struct sctp_inpcb);
1610 printf("Out of SCTP-INPCB structures - no resources\n");
1611 SCTP_INP_INFO_WUNLOCK();
1615 bzero(inp, sizeof(*inp));
1617 /* bump generations */
1618 /* setup socket pointers */
1619 inp->sctp_socket = so;
1620 inp->ip_inp.inp.inp_socket = so;
1622 inp->partial_delivery_point = so->so_rcv.sb_hiwat >> SCTP_PARTIAL_DELIVERY_SHIFT;
1623 inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
1627 struct inpcbpolicy *pcb_sp = NULL;
1629 error = ipsec_init_pcbpolicy(so, &pcb_sp);
1630 /* Arrange to share the policy */
1631 inp->ip_inp.inp.inp_sp = pcb_sp;
1632 ((struct in6pcb *)(&inp->ip_inp.inp))->in6p_sp = pcb_sp;
1635 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
1636 SCTP_INP_INFO_WUNLOCK();
1640 SCTP_INCR_EP_COUNT();
1641 inp->ip_inp.inp.inp_ip_ttl = ip_defttl;
1642 SCTP_INP_INFO_WUNLOCK();
1644 so->so_pcb = (caddr_t)inp;
1646 if ((SCTP_SO_TYPE(so) == SOCK_DGRAM) ||
1647 (SCTP_SO_TYPE(so) == SOCK_SEQPACKET)) {
1648 /* UDP style socket */
1649 inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
1650 SCTP_PCB_FLAGS_UNBOUND);
1651 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
1652 /* Be sure it is NON-BLOCKING IO for UDP */
1653 /* SCTP_SET_SO_NBIO(so); */
1654 } else if (SCTP_SO_TYPE(so) == SOCK_STREAM) {
1655 /* TCP style socket */
1656 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
1657 SCTP_PCB_FLAGS_UNBOUND);
1658 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
1659 /* Be sure we have blocking IO by default */
1660 SCTP_CLEAR_SO_NBIO(so);
1663 * unsupported socket type (RAW, etc)- in case we missed it
1666 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
1667 return (EOPNOTSUPP);
1669 inp->sctp_tcbhash = SCTP_HASH_INIT(sctp_pcbtblsize,
1670 &inp->sctp_hashmark);
1671 if (inp->sctp_tcbhash == NULL) {
1672 printf("Out of SCTP-INPCB->hashinit - no resources\n");
1673 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
1676 inp->def_vrf_id = SCTP_DEFAULT_VRFID;
1678 SCTP_INP_INFO_WLOCK();
1679 SCTP_INP_LOCK_INIT(inp);
1680 SCTP_INP_READ_INIT(inp);
1681 SCTP_ASOC_CREATE_LOCK_INIT(inp);
1682 /* lock the new ep */
1683 SCTP_INP_WLOCK(inp);
1685 /* add it to the info area */
1686 LIST_INSERT_HEAD(&sctppcbinfo.listhead, inp, sctp_list);
1687 SCTP_INP_INFO_WUNLOCK();
1689 TAILQ_INIT(&inp->read_queue);
1690 LIST_INIT(&inp->sctp_addr_list);
1692 LIST_INIT(&inp->sctp_asoc_list);
1694 #ifdef SCTP_TRACK_FREED_ASOCS
1696 LIST_INIT(&inp->sctp_asoc_free_list);
1698 /* Init the timer structure for signature change */
1699 SCTP_OS_TIMER_INIT(&inp->sctp_ep.signature_change.timer);
1700 inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NEWCOOKIE;
1702 /* now init the actual endpoint default data */
1705 /* setup the base timeout information */
1706 m->sctp_timeoutticks[SCTP_TIMER_SEND] = SEC_TO_TICKS(SCTP_SEND_SEC); /* needed ? */
1707 m->sctp_timeoutticks[SCTP_TIMER_INIT] = SEC_TO_TICKS(SCTP_INIT_SEC); /* needed ? */
1708 m->sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sctp_delayed_sack_time_default);
1709 m->sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(sctp_heartbeat_interval_default);
1710 m->sctp_timeoutticks[SCTP_TIMER_PMTU] = SEC_TO_TICKS(sctp_pmtu_raise_time_default);
1711 m->sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] = SEC_TO_TICKS(sctp_shutdown_guard_time_default);
1712 m->sctp_timeoutticks[SCTP_TIMER_SIGNATURE] = SEC_TO_TICKS(sctp_secret_lifetime_default);
1713 /* all max/min max are in ms */
1714 m->sctp_maxrto = sctp_rto_max_default;
1715 m->sctp_minrto = sctp_rto_min_default;
1716 m->initial_rto = sctp_rto_initial_default;
1717 m->initial_init_rto_max = sctp_init_rto_max_default;
1718 m->sctp_sack_freq = sctp_sack_freq_default;
1720 m->max_open_streams_intome = MAX_SCTP_STREAMS;
1722 m->max_init_times = sctp_init_rtx_max_default;
1723 m->max_send_times = sctp_assoc_rtx_max_default;
1724 m->def_net_failure = sctp_path_rtx_max_default;
1725 m->sctp_sws_sender = SCTP_SWS_SENDER_DEF;
1726 m->sctp_sws_receiver = SCTP_SWS_RECEIVER_DEF;
1727 m->max_burst = sctp_max_burst_default;
1728 /* number of streams to pre-open on a association */
1729 m->pre_open_stream_count = sctp_nr_outgoing_streams_default;
1731 /* Add adaptation cookie */
1732 m->adaptation_layer_indicator = 0x504C5253;
1734 /* seed random number generator */
1735 m->random_counter = 1;
1736 m->store_at = SCTP_SIGNATURE_SIZE;
1737 SCTP_READ_RANDOM(m->random_numbers, sizeof(m->random_numbers));
1738 sctp_fill_random_store(m);
1740 /* Minimum cookie size */
1741 m->size_of_a_cookie = (sizeof(struct sctp_init_msg) * 2) +
1742 sizeof(struct sctp_state_cookie);
1743 m->size_of_a_cookie += SCTP_SIGNATURE_SIZE;
1745 /* Setup the initial secret */
1746 SCTP_GETTIME_TIMEVAL(&time);
1747 m->time_of_secret_change = time.tv_sec;
1749 for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
1750 m->secret_key[0][i] = sctp_select_initial_TSN(m);
1752 sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL);
1754 /* How long is a cookie good for ? */
1755 m->def_cookie_life = sctp_valid_cookie_life_default;
1757 * Initialize authentication parameters
1759 m->local_hmacs = sctp_default_supported_hmaclist();
1760 m->local_auth_chunks = sctp_alloc_chunklist();
1761 sctp_auth_set_default_chunks(m->local_auth_chunks);
1762 LIST_INIT(&m->shared_keys);
1763 /* add default NULL key as key id 0 */
1764 null_key = sctp_alloc_sharedkey();
1765 sctp_insert_sharedkey(&m->shared_keys, null_key);
1766 SCTP_INP_WUNLOCK(inp);
1767 #ifdef SCTP_LOG_CLOSING
1768 sctp_log_closing(inp, NULL, 12);
1775 sctp_move_pcb_and_assoc(struct sctp_inpcb *old_inp, struct sctp_inpcb *new_inp,
1776 struct sctp_tcb *stcb)
1778 struct sctp_nets *net;
1779 uint16_t lport, rport;
1780 struct sctppcbhead *head;
1781 struct sctp_laddr *laddr, *oladdr;
1783 SCTP_TCB_UNLOCK(stcb);
1784 SCTP_INP_INFO_WLOCK();
1785 SCTP_INP_WLOCK(old_inp);
1786 SCTP_INP_WLOCK(new_inp);
1787 SCTP_TCB_LOCK(stcb);
1789 new_inp->sctp_ep.time_of_secret_change =
1790 old_inp->sctp_ep.time_of_secret_change;
1791 memcpy(new_inp->sctp_ep.secret_key, old_inp->sctp_ep.secret_key,
1792 sizeof(old_inp->sctp_ep.secret_key));
1793 new_inp->sctp_ep.current_secret_number =
1794 old_inp->sctp_ep.current_secret_number;
1795 new_inp->sctp_ep.last_secret_number =
1796 old_inp->sctp_ep.last_secret_number;
1797 new_inp->sctp_ep.size_of_a_cookie = old_inp->sctp_ep.size_of_a_cookie;
1799 /* make it so new data pours into the new socket */
1800 stcb->sctp_socket = new_inp->sctp_socket;
1801 stcb->sctp_ep = new_inp;
1803 /* Copy the port across */
1804 lport = new_inp->sctp_lport = old_inp->sctp_lport;
1805 rport = stcb->rport;
1806 /* Pull the tcb from the old association */
1807 LIST_REMOVE(stcb, sctp_tcbhash);
1808 LIST_REMOVE(stcb, sctp_tcblist);
1810 /* Now insert the new_inp into the TCP connected hash */
1811 head = &sctppcbinfo.sctp_tcpephash[SCTP_PCBHASH_ALLADDR((lport + rport),
1812 sctppcbinfo.hashtcpmark)];
1814 LIST_INSERT_HEAD(head, new_inp, sctp_hash);
1815 /* Its safe to access */
1816 new_inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
1818 /* Now move the tcb into the endpoint list */
1819 LIST_INSERT_HEAD(&new_inp->sctp_asoc_list, stcb, sctp_tcblist);
1821 * Question, do we even need to worry about the ep-hash since we
1822 * only have one connection? Probably not :> so lets get rid of it
1823 * and not suck up any kernel memory in that.
1826 /* Ok. Let's restart timer. */
1827 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1828 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, new_inp,
1832 SCTP_INP_INFO_WUNLOCK();
1833 if (new_inp->sctp_tcbhash != NULL) {
1834 SCTP_HASH_FREE(new_inp->sctp_tcbhash, new_inp->sctp_hashmark);
1835 new_inp->sctp_tcbhash = NULL;
1837 if ((new_inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
1838 /* Subset bound, so copy in the laddr list from the old_inp */
1839 LIST_FOREACH(oladdr, &old_inp->sctp_addr_list, sctp_nxt_addr) {
1840 laddr = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_laddr, struct sctp_laddr);
1841 if (laddr == NULL) {
1843 * Gak, what can we do? This assoc is really
1844 * HOSED. We probably should send an abort
1848 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1849 printf("Association hosed in TCP model, out of laddr memory\n");
1851 #endif /* SCTP_DEBUG */
1854 SCTP_INCR_LADDR_COUNT();
1855 bzero(laddr, sizeof(*laddr));
1856 laddr->ifa = oladdr->ifa;
1857 LIST_INSERT_HEAD(&new_inp->sctp_addr_list, laddr,
1859 new_inp->laddr_count++;
1863 * Now any running timers need to be adjusted since we really don't
1864 * care if they are running or not just blast in the new_inp into
1868 stcb->asoc.hb_timer.ep = (void *)new_inp;
1869 stcb->asoc.dack_timer.ep = (void *)new_inp;
1870 stcb->asoc.asconf_timer.ep = (void *)new_inp;
1871 stcb->asoc.strreset_timer.ep = (void *)new_inp;
1872 stcb->asoc.shut_guard_timer.ep = (void *)new_inp;
1873 stcb->asoc.autoclose_timer.ep = (void *)new_inp;
1874 stcb->asoc.delayed_event_timer.ep = (void *)new_inp;
1875 /* now what about the nets? */
1876 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1877 net->pmtu_timer.ep = (void *)new_inp;
1878 net->rxt_timer.ep = (void *)new_inp;
1879 net->fr_timer.ep = (void *)new_inp;
1881 SCTP_INP_WUNLOCK(new_inp);
1882 SCTP_INP_WUNLOCK(old_inp);
1886 sctp_isport_inuse(struct sctp_inpcb *inp, uint16_t lport, uint32_t vrf_id)
1888 struct sctppcbhead *head;
1889 struct sctp_inpcb *t_inp;
1892 head = &sctppcbinfo.sctp_ephash[SCTP_PCBHASH_ALLADDR(lport,
1893 sctppcbinfo.hashmark)];
1895 LIST_FOREACH(t_inp, head, sctp_hash) {
1896 if (t_inp->sctp_lport != lport) {
1899 /* is it in the VRF in question */
1901 if (t_inp->def_vrf_id == vrf_id)
1906 /* This one is in use. */
1907 /* check the v6/v4 binding issue */
1908 if ((t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1909 SCTP_IPV6_V6ONLY(t_inp)) {
1910 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1911 /* collision in V6 space */
1914 /* inp is BOUND_V4 no conflict */
1917 } else if (t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1918 /* t_inp is bound v4 and v6, conflict always */
1921 /* t_inp is bound only V4 */
1922 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1923 SCTP_IPV6_V6ONLY(t_inp)) {
1927 /* else fall through to conflict */
1937 sctp_inpcb_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
1939 /* bind a ep to a socket address */
1940 struct sctppcbhead *head;
1941 struct sctp_inpcb *inp, *inp_tmp;
1942 struct inpcb *ip_inp;
1951 inp = (struct sctp_inpcb *)so->so_pcb;
1952 ip_inp = (struct inpcb *)so->so_pcb;
1954 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1956 printf("Bind called port:%d\n",
1957 ntohs(((struct sockaddr_in *)addr)->sin_port));
1959 sctp_print_address(addr);
1962 #endif /* SCTP_DEBUG */
1963 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
1964 /* already did a bind, subsequent binds NOT allowed ! */
1968 if (addr->sa_family == AF_INET) {
1969 struct sockaddr_in *sin;
1971 /* IPV6_V6ONLY socket? */
1972 if (SCTP_IPV6_V6ONLY(ip_inp)) {
1975 if (addr->sa_len != sizeof(*sin))
1978 sin = (struct sockaddr_in *)addr;
1979 lport = sin->sin_port;
1981 if (sin->sin_addr.s_addr != INADDR_ANY) {
1984 } else if (addr->sa_family == AF_INET6) {
1985 /* Only for pure IPv6 Address. (No IPv4 Mapped!) */
1986 struct sockaddr_in6 *sin6;
1988 sin6 = (struct sockaddr_in6 *)addr;
1990 if (addr->sa_len != sizeof(*sin6))
1993 lport = sin6->sin6_port;
1994 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1996 /* KAME hack: embed scopeid */
1997 if (sa6_embedscope(sin6, ip6_use_defzone) != 0)
2000 /* this must be cleared for ifa_ifwithaddr() */
2001 sin6->sin6_scope_id = 0;
2003 return (EAFNOSUPPORT);
2007 * Setup a vrf_id to be the default for the non-bind-all case.
2009 vrf_id = inp->def_vrf_id;
2011 SCTP_INP_INFO_WLOCK();
2012 SCTP_INP_WLOCK(inp);
2013 /* increase our count due to the unlock we do */
2014 SCTP_INP_INCR_REF(inp);
2017 * Did the caller specify a port? if so we must see if a ep
2018 * already has this one bound.
2020 /* got to be root to get at low ports */
2021 if (ntohs(lport) < IPPORT_RESERVED) {
2023 priv_check_cred(p->td_ucred,
2024 PRIV_NETINET_RESERVEDPORT,
2028 SCTP_INP_DECR_REF(inp);
2029 SCTP_INP_WUNLOCK(inp);
2030 SCTP_INP_INFO_WUNLOCK();
2035 SCTP_INP_DECR_REF(inp);
2036 SCTP_INP_WUNLOCK(inp);
2037 SCTP_INP_INFO_WUNLOCK();
2040 SCTP_INP_WUNLOCK(inp);
2042 vrf_id = inp->def_vrf_id;
2043 inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id);
2044 if (inp_tmp != NULL) {
2046 * lock guy returned and lower count note
2047 * that we are not bound so inp_tmp should
2048 * NEVER be inp. And it is this inp
2049 * (inp_tmp) that gets the reference bump,
2050 * so we must lower it.
2052 SCTP_INP_DECR_REF(inp_tmp);
2053 SCTP_INP_DECR_REF(inp);
2055 SCTP_INP_INFO_WUNLOCK();
2056 return (EADDRNOTAVAIL);
2059 inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id);
2060 if (inp_tmp != NULL) {
2062 * lock guy returned and lower count note
2063 * that we are not bound so inp_tmp should
2064 * NEVER be inp. And it is this inp
2065 * (inp_tmp) that gets the reference bump,
2066 * so we must lower it.
2068 SCTP_INP_DECR_REF(inp_tmp);
2069 SCTP_INP_DECR_REF(inp);
2071 SCTP_INP_INFO_WUNLOCK();
2072 return (EADDRNOTAVAIL);
2075 SCTP_INP_WLOCK(inp);
2077 /* verify that no lport is not used by a singleton */
2078 if (sctp_isport_inuse(inp, lport, vrf_id)) {
2079 /* Sorry someone already has this one bound */
2080 SCTP_INP_DECR_REF(inp);
2081 SCTP_INP_WUNLOCK(inp);
2082 SCTP_INP_INFO_WUNLOCK();
2083 return (EADDRNOTAVAIL);
2088 * get any port but lets make sure no one has any address
2089 * with this port bound
2093 * setup the inp to the top (I could use the union but this
2096 uint32_t port_guess;
2097 uint16_t port_attempt;
2102 port_guess = sctp_select_initial_TSN(&inp->sctp_ep);
2103 port_attempt = (port_guess & 0x0000ffff);
2104 if (port_attempt == 0) {
2107 if (port_attempt < IPPORT_RESERVED) {
2108 port_attempt += IPPORT_RESERVED;
2110 vrf_id = inp->def_vrf_id;
2111 if (sctp_isport_inuse(inp, htons(port_attempt), vrf_id) == 1) {
2112 /* got a port we can use */
2116 if (not_found == 1) {
2117 /* We can use this port */
2121 /* try upper half */
2123 port_attempt = ((port_guess >> 16) & 0x0000ffff);
2124 if (port_attempt == 0) {
2127 if (port_attempt < IPPORT_RESERVED) {
2128 port_attempt += IPPORT_RESERVED;
2130 vrf_id = inp->def_vrf_id;
2131 if (sctp_isport_inuse(inp, htons(port_attempt), vrf_id) == 1) {
2132 /* got a port we can use */
2136 if (not_found == 1) {
2137 /* We can use this port */
2141 /* try two half's added together */
2143 port_attempt = (((port_guess >> 16) & 0x0000ffff) +
2144 (port_guess & 0x0000ffff));
2145 if (port_attempt == 0) {
2146 /* get a new random number */
2149 if (port_attempt < IPPORT_RESERVED) {
2150 port_attempt += IPPORT_RESERVED;
2152 vrf_id = inp->def_vrf_id;
2153 if (sctp_isport_inuse(inp, htons(port_attempt), vrf_id) == 1) {
2154 /* got a port we can use */
2158 if (not_found == 1) {
2159 /* We can use this port */
2164 /* we don't get out of the loop until we have a port */
2165 lport = htons(port_attempt);
2167 SCTP_INP_DECR_REF(inp);
2168 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE |
2169 SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
2171 * this really should not happen. The guy did a non-blocking
2172 * bind and then did a close at the same time.
2174 SCTP_INP_WUNLOCK(inp);
2175 SCTP_INP_INFO_WUNLOCK();
2178 /* ok we look clear to give out this port, so lets setup the binding */
2180 /* binding to all addresses, so just set in the proper flags */
2181 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUNDALL;
2182 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
2183 /* set the automatic addr changes from kernel flag */
2184 if (sctp_auto_asconf == 0) {
2185 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
2187 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
2191 * bind specific, make sure flags is off and add a new
2192 * address structure to the sctp_addr_list inside the ep
2195 * We will need to allocate one and insert it at the head. The
2196 * socketopt call can just insert new addresses in there as
2197 * well. It will also have to do the embed scope kame hack
2198 * too (before adding).
2200 struct sctp_ifa *ifa;
2201 struct sockaddr_storage store_sa;
2203 memset(&store_sa, 0, sizeof(store_sa));
2204 if (addr->sa_family == AF_INET) {
2205 struct sockaddr_in *sin;
2207 sin = (struct sockaddr_in *)&store_sa;
2208 memcpy(sin, addr, sizeof(struct sockaddr_in));
2210 } else if (addr->sa_family == AF_INET6) {
2211 struct sockaddr_in6 *sin6;
2213 sin6 = (struct sockaddr_in6 *)&store_sa;
2214 memcpy(sin6, addr, sizeof(struct sockaddr_in6));
2215 sin6->sin6_port = 0;
2218 * first find the interface with the bound address need to
2219 * zero out the port to find the address! yuck! can't do
2220 * this earlier since need port for sctp_pcb_findep()
2222 ifa = sctp_find_ifa_by_addr((struct sockaddr *)&store_sa, vrf_id, 0);
2224 /* Can't find an interface with that address */
2225 SCTP_INP_WUNLOCK(inp);
2226 SCTP_INP_INFO_WUNLOCK();
2227 return (EADDRNOTAVAIL);
2229 if (addr->sa_family == AF_INET6) {
2230 /* GAK, more FIXME IFA lock? */
2231 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2232 /* Can't bind a non-existent addr. */
2233 SCTP_INP_WUNLOCK(inp);
2234 SCTP_INP_INFO_WUNLOCK();
2238 /* we're not bound all */
2239 inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUNDALL;
2240 /* set the automatic addr changes from kernel flag */
2241 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
2242 if (sctp_auto_asconf == 0) {
2243 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
2246 * allow bindx() to send ASCONF's for binding
2249 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
2252 /* add this address to the endpoint list */
2253 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, 0);
2255 SCTP_INP_WUNLOCK(inp);
2256 SCTP_INP_INFO_WUNLOCK();
2261 /* find the bucket */
2262 head = &sctppcbinfo.sctp_ephash[SCTP_PCBHASH_ALLADDR(lport,
2263 sctppcbinfo.hashmark)];
2264 /* put it in the bucket */
2265 LIST_INSERT_HEAD(head, inp, sctp_hash);
2267 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
2268 printf("Main hash to bind at head:%p, bound port:%d\n", head, ntohs(lport));
2271 /* set in the port */
2272 inp->sctp_lport = lport;
2274 /* turn off just the unbound flag */
2275 inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
2276 SCTP_INP_WUNLOCK(inp);
2277 SCTP_INP_INFO_WUNLOCK();
2283 sctp_iterator_inp_being_freed(struct sctp_inpcb *inp, struct sctp_inpcb *inp_next)
2285 struct sctp_iterator *it;
2288 * We enter with the only the ITERATOR_LOCK in place and a write
2289 * lock on the inp_info stuff.
2293 * Go through all iterators, we must do this since it is possible
2294 * that some iterator does NOT have the lock, but is waiting for it.
2295 * And the one that had the lock has either moved in the last
2296 * iteration or we just cleared it above. We need to find all of
2297 * those guys. The list of iterators should never be very big
2300 TAILQ_FOREACH(it, &sctppcbinfo.iteratorhead, sctp_nxt_itr) {
2301 if (it == inp->inp_starting_point_for_iterator)
2302 /* skip this guy, he's special */
2304 if (it->inp == inp) {
2306 * This is tricky and we DON'T lock the iterator.
2307 * Reason is he's running but waiting for me since
2308 * inp->inp_starting_point_for_iterator has the lock
2309 * on me (the guy above we skipped). This tells us
2310 * its is not running but waiting for
2311 * inp->inp_starting_point_for_iterator to be
2312 * released by the guy that does have our INP in a
2315 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
2319 /* set him up to do the next guy not me */
2325 it = inp->inp_starting_point_for_iterator;
2327 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
2336 /* release sctp_inpcb unbind the port */
2338 sctp_inpcb_free(struct sctp_inpcb *inp, int immediate, int from)
2341 * Here we free a endpoint. We must find it (if it is in the Hash
2342 * table) and remove it from there. Then we must also find it in the
2343 * overall list and remove it from there. After all removals are
2344 * complete then any timer has to be stopped. Then start the actual
2345 * freeing. a) Any local lists. b) Any associations. c) The hash of
2346 * all associations. d) finally the ep itself.
2349 struct sctp_inpcb *inp_save;
2350 struct sctp_tcb *asoc, *nasoc;
2351 struct sctp_laddr *laddr, *nladdr;
2352 struct inpcb *ip_pcb;
2355 struct sctp_queued_to_read *sq;
2358 sctp_sharedkey_t *shared_key;
2361 #ifdef SCTP_LOG_CLOSING
2362 sctp_log_closing(inp, NULL, 0);
2365 SCTP_ITERATOR_LOCK();
2366 so = inp->sctp_socket;
2367 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
2368 /* been here before.. eeks.. get out of here */
2369 printf("This conflict in free SHOULD not be happening!\n");
2370 SCTP_ITERATOR_UNLOCK();
2371 #ifdef SCTP_LOG_CLOSING
2372 sctp_log_closing(inp, NULL, 1);
2376 SCTP_ASOC_CREATE_LOCK(inp);
2377 SCTP_INP_INFO_WLOCK();
2379 SCTP_INP_WLOCK(inp);
2381 * First time through we have the socket lock, after that no more.
2385 * Once we are in we can remove the flag from = 1 is only
2386 * passed from the actual closing routines that are called
2387 * via the sockets layer.
2389 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CLOSE_IP;
2391 sctp_timer_stop(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL,
2392 SCTP_FROM_SCTP_PCB + SCTP_LOC_1);
2395 sctp_m_freem(inp->control);
2396 inp->control = NULL;
2399 sctp_m_freem(inp->pkt);
2403 ip_pcb = &inp->ip_inp.inp; /* we could just cast the main pointer
2404 * here but I will be nice :> (i.e.
2406 if (immediate == 0) {
2410 for ((asoc = LIST_FIRST(&inp->sctp_asoc_list)); asoc != NULL;
2412 nasoc = LIST_NEXT(asoc, sctp_tcblist);
2413 if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
2414 /* Skip guys being freed */
2415 asoc->sctp_socket = NULL;
2419 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_WAIT) ||
2420 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
2421 /* Just abandon things in the front states */
2422 if (asoc->asoc.total_output_queue_size == 0) {
2423 sctp_free_assoc(inp, asoc, SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_2);
2427 SCTP_TCB_LOCK(asoc);
2428 /* Disconnect the socket please */
2429 asoc->sctp_socket = NULL;
2430 asoc->asoc.state |= SCTP_STATE_CLOSED_SOCKET;
2431 if ((asoc->asoc.size_on_reasm_queue > 0) ||
2432 (asoc->asoc.control_pdapi) ||
2433 (asoc->asoc.size_on_all_streams > 0) ||
2434 (so && (so->so_rcv.sb_cc > 0))
2436 /* Left with Data unread */
2437 struct mbuf *op_err;
2439 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
2440 0, M_DONTWAIT, 1, MT_DATA);
2442 /* Fill in the user initiated abort */
2443 struct sctp_paramhdr *ph;
2446 SCTP_BUF_LEN(op_err) =
2447 sizeof(struct sctp_paramhdr) + sizeof(uint32_t);
2449 struct sctp_paramhdr *);
2450 ph->param_type = htons(
2451 SCTP_CAUSE_USER_INITIATED_ABT);
2452 ph->param_length = htons(SCTP_BUF_LEN(op_err));
2453 ippp = (uint32_t *) (ph + 1);
2454 *ippp = htonl(SCTP_FROM_SCTP_PCB + SCTP_LOC_3);
2456 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_3;
2457 sctp_send_abort_tcb(asoc, op_err);
2458 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
2459 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
2460 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
2461 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
2463 sctp_free_assoc(inp, asoc, SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_4);
2465 } else if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
2466 TAILQ_EMPTY(&asoc->asoc.sent_queue) &&
2467 (asoc->asoc.stream_queue_cnt == 0)
2469 if (asoc->asoc.locked_on_sending) {
2472 if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
2473 (SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
2475 * there is nothing queued to send,
2476 * so I send shutdown
2478 sctp_send_shutdown(asoc, asoc->asoc.primary_destination);
2479 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
2480 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
2481 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
2483 asoc->asoc.state = SCTP_STATE_SHUTDOWN_SENT;
2484 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, asoc->sctp_ep, asoc,
2485 asoc->asoc.primary_destination);
2486 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc,
2487 asoc->asoc.primary_destination);
2488 sctp_chunk_output(inp, asoc, SCTP_OUTPUT_FROM_SHUT_TMR);
2491 /* mark into shutdown pending */
2492 struct sctp_stream_queue_pending *sp;
2494 asoc->asoc.state |= SCTP_STATE_SHUTDOWN_PENDING;
2495 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc,
2496 asoc->asoc.primary_destination);
2497 if (asoc->asoc.locked_on_sending) {
2498 sp = TAILQ_LAST(&((asoc->asoc.locked_on_sending)->outqueue),
2501 printf("Error, sp is NULL, locked on sending is %p strm:%d\n",
2502 asoc->asoc.locked_on_sending,
2503 asoc->asoc.locked_on_sending->stream_no);
2505 if ((sp->length == 0) && (sp->msg_is_complete == 0))
2506 asoc->asoc.state |= SCTP_STATE_PARTIAL_MSG_LEFT;
2509 if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
2510 TAILQ_EMPTY(&asoc->asoc.sent_queue) &&
2511 (asoc->asoc.state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
2512 struct mbuf *op_err;
2515 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
2516 0, M_DONTWAIT, 1, MT_DATA);
2522 struct sctp_paramhdr *ph;
2525 SCTP_BUF_LEN(op_err) =
2526 (sizeof(struct sctp_paramhdr) +
2529 struct sctp_paramhdr *);
2530 ph->param_type = htons(
2531 SCTP_CAUSE_USER_INITIATED_ABT);
2532 ph->param_length = htons(SCTP_BUF_LEN(op_err));
2533 ippp = (uint32_t *) (ph + 1);
2534 *ippp = htonl(SCTP_FROM_SCTP_PCB + SCTP_LOC_5);
2536 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_5;
2537 sctp_send_abort_tcb(asoc, op_err);
2538 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
2539 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
2540 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
2541 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
2543 sctp_free_assoc(inp, asoc, SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_6);
2548 SCTP_TCB_UNLOCK(asoc);
2550 /* now is there some left in our SHUTDOWN state? */
2552 SCTP_INP_WUNLOCK(inp);
2553 SCTP_ASOC_CREATE_UNLOCK(inp);
2554 SCTP_INP_INFO_WUNLOCK();
2555 SCTP_ITERATOR_UNLOCK();
2556 #ifdef SCTP_LOG_CLOSING
2557 sctp_log_closing(inp, NULL, 2);
2562 inp->sctp_socket = NULL;
2563 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) !=
2564 SCTP_PCB_FLAGS_UNBOUND) {
2566 * ok, this guy has been bound. It's port is somewhere in
2567 * the sctppcbinfo hash table. Remove it!
2569 LIST_REMOVE(inp, sctp_hash);
2570 inp->sctp_flags |= SCTP_PCB_FLAGS_UNBOUND;
2573 * If there is a timer running to kill us, forget it, since it may
2574 * have a contest on the INP lock.. which would cause us to die ...
2577 for ((asoc = LIST_FIRST(&inp->sctp_asoc_list)); asoc != NULL;
2579 nasoc = LIST_NEXT(asoc, sctp_tcblist);
2580 if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
2584 /* Free associations that are NOT killing us */
2585 SCTP_TCB_LOCK(asoc);
2586 if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_COOKIE_WAIT) &&
2587 ((asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0)) {
2588 struct mbuf *op_err;
2591 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
2592 0, M_DONTWAIT, 1, MT_DATA);
2594 /* Fill in the user initiated abort */
2595 struct sctp_paramhdr *ph;
2597 SCTP_BUF_LEN(op_err) = (sizeof(struct sctp_paramhdr) +
2599 ph = mtod(op_err, struct sctp_paramhdr *);
2600 ph->param_type = htons(
2601 SCTP_CAUSE_USER_INITIATED_ABT);
2602 ph->param_length = htons(SCTP_BUF_LEN(op_err));
2603 ippp = (uint32_t *) (ph + 1);
2604 *ippp = htonl(SCTP_FROM_SCTP_PCB + SCTP_LOC_7);
2607 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_7;
2608 sctp_send_abort_tcb(asoc, op_err);
2609 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
2610 } else if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
2612 SCTP_TCB_UNLOCK(asoc);
2615 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
2616 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
2617 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
2619 sctp_free_assoc(inp, asoc, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_8);
2622 /* Ok we have someone out there that will kill us */
2623 SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
2624 SCTP_INP_WUNLOCK(inp);
2625 SCTP_ASOC_CREATE_UNLOCK(inp);
2626 SCTP_INP_INFO_WUNLOCK();
2627 SCTP_ITERATOR_UNLOCK();
2628 #ifdef SCTP_LOG_CLOSING
2629 sctp_log_closing(inp, NULL, 3);
2633 if ((inp->refcount) || (inp->sctp_flags & SCTP_PCB_FLAGS_CLOSE_IP)) {
2634 SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
2635 sctp_timer_start(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL);
2636 SCTP_INP_WUNLOCK(inp);
2637 SCTP_ASOC_CREATE_UNLOCK(inp);
2638 SCTP_INP_INFO_WUNLOCK();
2639 SCTP_ITERATOR_UNLOCK();
2640 #ifdef SCTP_LOG_CLOSING
2641 sctp_log_closing(inp, NULL, 4);
2645 SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
2646 inp->sctp_ep.signature_change.type = 0;
2647 inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_ALLGONE;
2649 #ifdef SCTP_LOG_CLOSING
2650 sctp_log_closing(inp, NULL, 5);
2653 SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
2654 inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NONE;
2655 /* Clear the read queue */
2656 while ((sq = TAILQ_FIRST(&inp->read_queue)) != NULL) {
2657 TAILQ_REMOVE(&inp->read_queue, sq, next);
2658 sctp_free_remote_addr(sq->whoFrom);
2660 so->so_rcv.sb_cc -= sq->length;
2662 sctp_m_freem(sq->data);
2666 * no need to free the net count, since at this point all
2669 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_readq, sq);
2670 SCTP_DECR_READQ_COUNT();
2672 /* Now the sctp_pcb things */
2674 * free each asoc if it is not already closed/free. we can't use the
2675 * macro here since le_next will get freed as part of the
2676 * sctp_free_assoc() call.
2681 ipsec4_delete_pcbpolicy(ip_pcb);
2684 /* Unlocks not needed since the socket is gone now */
2686 if (ip_pcb->inp_options) {
2687 (void)sctp_m_free(ip_pcb->inp_options);
2688 ip_pcb->inp_options = 0;
2690 if (ip_pcb->inp_moptions) {
2691 ip_freemoptions(ip_pcb->inp_moptions);
2692 ip_pcb->inp_moptions = 0;
2695 if (ip_pcb->inp_vflag & INP_IPV6) {
2696 struct in6pcb *in6p;
2698 in6p = (struct in6pcb *)inp;
2699 ip6_freepcbopts(in6p->in6p_outputopts);
2702 ip_pcb->inp_vflag = 0;
2703 /* free up authentication fields */
2704 if (inp->sctp_ep.local_auth_chunks != NULL)
2705 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2706 if (inp->sctp_ep.local_hmacs != NULL)
2707 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2709 shared_key = LIST_FIRST(&inp->sctp_ep.shared_keys);
2710 while (shared_key) {
2711 LIST_REMOVE(shared_key, next);
2712 sctp_free_sharedkey(shared_key);
2713 shared_key = LIST_FIRST(&inp->sctp_ep.shared_keys);
2716 inp_save = LIST_NEXT(inp, sctp_list);
2717 LIST_REMOVE(inp, sctp_list);
2719 /* fix any iterators only after out of the list */
2720 sctp_iterator_inp_being_freed(inp, inp_save);
2722 * if we have an address list the following will free the list of
2723 * ifaddr's that are set into this ep. Again macro limitations here,
2724 * since the LIST_FOREACH could be a bad idea.
2726 for ((laddr = LIST_FIRST(&inp->sctp_addr_list)); laddr != NULL;
2728 nladdr = LIST_NEXT(laddr, sctp_nxt_addr);
2729 sctp_remove_laddr(laddr);
2732 #ifdef SCTP_TRACK_FREED_ASOCS
2734 for ((asoc = LIST_FIRST(&inp->sctp_asoc_free_list)); asoc != NULL;
2736 nasoc = LIST_NEXT(asoc, sctp_tcblist);
2737 LIST_REMOVE(asoc, sctp_tcblist);
2738 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, asoc);
2739 SCTP_DECR_ASOC_COUNT();
2741 /* *** END TEMP CODE *** */
2743 /* Now lets see about freeing the EP hash table. */
2744 if (inp->sctp_tcbhash != NULL) {
2745 SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark);
2746 inp->sctp_tcbhash = NULL;
2748 /* Now we must put the ep memory back into the zone pool */
2749 SCTP_INP_LOCK_DESTROY(inp);
2750 SCTP_INP_READ_DESTROY(inp);
2751 SCTP_ASOC_CREATE_LOCK_DESTROY(inp);
2752 SCTP_INP_INFO_WUNLOCK();
2754 SCTP_ITERATOR_UNLOCK();
2756 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
2757 SCTP_DECR_EP_COUNT();
2763 sctp_findnet(struct sctp_tcb *stcb, struct sockaddr *addr)
2765 struct sctp_nets *net;
2767 /* locate the address */
2768 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2769 if (sctp_cmpaddr(addr, (struct sockaddr *)&net->ro._l_addr))
2777 * add's a remote endpoint address, done with the INIT/INIT-ACK as well as
2778 * when a ASCONF arrives that adds it. It will also initialize all the cwnd
2782 sctp_is_address_on_local_host(struct sockaddr *addr, uint32_t vrf_id)
2784 struct sctp_ifa *sctp_ifa;
2786 sctp_ifa = sctp_find_ifa_by_addr(addr, vrf_id, 0);
2795 sctp_set_initial_cc_param(struct sctp_tcb *stcb, struct sctp_nets *net)
2797 net->cwnd = min((net->mtu * 4), max((2 * net->mtu), SCTP_INITIAL_CWND));
2798 /* we always get at LEAST 2 MTU's */
2799 if (net->cwnd < (2 * net->mtu)) {
2800 net->cwnd = 2 * net->mtu;
2802 net->ssthresh = stcb->asoc.peers_rwnd;
2806 sctp_add_remote_addr(struct sctp_tcb *stcb, struct sockaddr *newaddr,
2807 int set_scope, int from)
2810 * The following is redundant to the same lines in the
2811 * sctp_aloc_assoc() but is needed since other's call the add
2814 struct sctp_nets *net, *netfirst;
2818 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
2819 printf("Adding an address (from:%d) to the peer: ", from);
2820 sctp_print_address(newaddr);
2824 netfirst = sctp_findnet(stcb, newaddr);
2827 * Lie and return ok, we don't want to make the association
2828 * go away for this behavior. It will happen in the TCP
2829 * model in a connected socket. It does not reach the hash
2830 * table until after the association is built so it can't be
2831 * found. Mark as reachable, since the initial creation will
2832 * have been cleared and the NOT_IN_ASSOC flag will have
2833 * been added... and we don't want to end up removing it
2836 if (netfirst->dest_state & SCTP_ADDR_UNCONFIRMED) {
2837 netfirst->dest_state = (SCTP_ADDR_REACHABLE |
2838 SCTP_ADDR_UNCONFIRMED);
2840 netfirst->dest_state = SCTP_ADDR_REACHABLE;
2846 if (newaddr->sa_family == AF_INET) {
2847 struct sockaddr_in *sin;
2849 sin = (struct sockaddr_in *)newaddr;
2850 if (sin->sin_addr.s_addr == 0) {
2851 /* Invalid address */
2854 /* zero out the bzero area */
2855 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
2857 /* assure len is set */
2858 sin->sin_len = sizeof(struct sockaddr_in);
2860 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
2861 stcb->ipv4_local_scope = 1;
2863 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
2864 stcb->asoc.ipv4_local_scope = 1;
2866 #endif /* SCTP_DONT_DO_PRIVADDR_SCOPE */
2868 /* Validate the address is in scope */
2869 if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) &&
2870 (stcb->asoc.ipv4_local_scope == 0)) {
2874 } else if (newaddr->sa_family == AF_INET6) {
2875 struct sockaddr_in6 *sin6;
2877 sin6 = (struct sockaddr_in6 *)newaddr;
2878 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2879 /* Invalid address */
2882 /* assure len is set */
2883 sin6->sin6_len = sizeof(struct sockaddr_in6);
2885 if (sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id)) {
2886 stcb->asoc.loopback_scope = 1;
2887 stcb->asoc.local_scope = 0;
2888 stcb->asoc.ipv4_local_scope = 1;
2889 stcb->asoc.site_scope = 1;
2890 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
2892 * If the new destination is a LINK_LOCAL we
2893 * must have common site scope. Don't set
2894 * the local scope since we may not share
2895 * all links, only loopback can do this.
2896 * Links on the local network would also be
2897 * on our private network for v4 too.
2899 stcb->asoc.ipv4_local_scope = 1;
2900 stcb->asoc.site_scope = 1;
2901 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
2903 * If the new destination is SITE_LOCAL then
2904 * we must have site scope in common.
2906 stcb->asoc.site_scope = 1;
2909 /* Validate the address is in scope */
2910 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) &&
2911 (stcb->asoc.loopback_scope == 0)) {
2913 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) &&
2914 (stcb->asoc.local_scope == 0)) {
2916 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) &&
2917 (stcb->asoc.site_scope == 0)) {
2922 /* not supported family type */
2925 net = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_net, struct sctp_nets);
2929 SCTP_INCR_RADDR_COUNT();
2930 bzero(net, sizeof(*net));
2931 SCTP_GETTIME_TIMEVAL(&net->start_time);
2932 memcpy(&net->ro._l_addr, newaddr, newaddr->sa_len);
2933 if (newaddr->sa_family == AF_INET) {
2934 ((struct sockaddr_in *)&net->ro._l_addr)->sin_port = stcb->rport;
2935 } else if (newaddr->sa_family == AF_INET6) {
2936 ((struct sockaddr_in6 *)&net->ro._l_addr)->sin6_port = stcb->rport;
2938 net->addr_is_local = sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id);
2939 if (net->addr_is_local && ((set_scope || (from == SCTP_ADDR_IS_CONFIRMED)))) {
2940 stcb->asoc.loopback_scope = 1;
2941 stcb->asoc.ipv4_local_scope = 1;
2942 stcb->asoc.local_scope = 0;
2943 stcb->asoc.site_scope = 1;
2946 net->failure_threshold = stcb->asoc.def_net_failure;
2947 if (addr_inscope == 0) {
2948 net->dest_state = (SCTP_ADDR_REACHABLE |
2949 SCTP_ADDR_OUT_OF_SCOPE);
2951 if (from == SCTP_ADDR_IS_CONFIRMED)
2952 /* SCTP_ADDR_IS_CONFIRMED is passed by connect_x */
2953 net->dest_state = SCTP_ADDR_REACHABLE;
2955 net->dest_state = SCTP_ADDR_REACHABLE |
2956 SCTP_ADDR_UNCONFIRMED;
2958 net->RTO = stcb->asoc.initial_rto;
2959 stcb->asoc.numnets++;
2960 *(&net->ref_count) = 1;
2961 net->tos_flowlabel = 0;
2963 if (newaddr->sa_family == AF_INET)
2964 net->tos_flowlabel = stcb->asoc.default_tos;
2967 if (newaddr->sa_family == AF_INET6)
2968 net->tos_flowlabel = stcb->asoc.default_flowlabel;
2970 /* Init the timer structure */
2971 SCTP_OS_TIMER_INIT(&net->rxt_timer.timer);
2972 SCTP_OS_TIMER_INIT(&net->fr_timer.timer);
2973 SCTP_OS_TIMER_INIT(&net->pmtu_timer.timer);
2975 /* Now generate a route for this guy */
2976 /* KAME hack: embed scopeid */
2977 if (newaddr->sa_family == AF_INET6) {
2978 struct sockaddr_in6 *sin6;
2980 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
2981 (void)sa6_embedscope(sin6, ip6_use_defzone);
2982 sin6->sin6_scope_id = 0;
2984 rtalloc_ign((struct route *)&net->ro, 0UL);
2985 if (newaddr->sa_family == AF_INET6) {
2986 struct sockaddr_in6 *sin6;
2988 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
2989 (void)sa6_recoverscope(sin6);
2991 if ((net->ro.ro_rt) &&
2992 (net->ro.ro_rt->rt_ifp)) {
2993 net->mtu = net->ro.ro_rt->rt_ifp->if_mtu;
2994 if (from == SCTP_ALLOC_ASOC) {
2995 stcb->asoc.smallest_mtu = net->mtu;
2997 /* start things off to match mtu of interface please. */
2998 net->ro.ro_rt->rt_rmx.rmx_mtu = net->ro.ro_rt->rt_ifp->if_mtu;
3000 net->mtu = stcb->asoc.smallest_mtu;
3003 if (stcb->asoc.smallest_mtu > net->mtu) {
3004 stcb->asoc.smallest_mtu = net->mtu;
3007 * We take the max of the burst limit times a MTU or the
3008 * INITIAL_CWND. We then limit this to 4 MTU's of sending.
3010 sctp_set_initial_cc_param(stcb, net);
3013 #if defined(SCTP_CWND_MONITOR) || defined(SCTP_CWND_LOGGING)
3014 sctp_log_cwnd(stcb, net, 0, SCTP_CWND_INITIALIZATION);
3018 * CMT: CUC algo - set find_pseudo_cumack to TRUE (1) at beginning
3019 * of assoc (2005/06/27, iyengar@cis.udel.edu)
3021 net->find_pseudo_cumack = 1;
3022 net->find_rtx_pseudo_cumack = 1;
3023 net->src_addr_selected = 0;
3024 netfirst = TAILQ_FIRST(&stcb->asoc.nets);
3025 if (net->ro.ro_rt == NULL) {
3026 /* Since we have no route put it at the back */
3027 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
3028 } else if (netfirst == NULL) {
3029 /* We are the first one in the pool. */
3030 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
3031 } else if (netfirst->ro.ro_rt == NULL) {
3033 * First one has NO route. Place this one ahead of the first
3036 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
3037 } else if (net->ro.ro_rt->rt_ifp != netfirst->ro.ro_rt->rt_ifp) {
3039 * This one has a different interface than the one at the
3040 * top of the list. Place it ahead.
3042 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
3045 * Ok we have the same interface as the first one. Move
3046 * forward until we find either a) one with a NULL route...
3047 * insert ahead of that b) one with a different ifp.. insert
3048 * after that. c) end of the list.. insert at the tail.
3050 struct sctp_nets *netlook;
3053 netlook = TAILQ_NEXT(netfirst, sctp_next);
3054 if (netlook == NULL) {
3055 /* End of the list */
3056 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net,
3059 } else if (netlook->ro.ro_rt == NULL) {
3060 /* next one has NO route */
3061 TAILQ_INSERT_BEFORE(netfirst, net, sctp_next);
3063 } else if (netlook->ro.ro_rt->rt_ifp !=
3064 net->ro.ro_rt->rt_ifp) {
3065 TAILQ_INSERT_AFTER(&stcb->asoc.nets, netlook,
3071 } while (netlook != NULL);
3074 /* got to have a primary set */
3075 if (stcb->asoc.primary_destination == 0) {
3076 stcb->asoc.primary_destination = net;
3077 } else if ((stcb->asoc.primary_destination->ro.ro_rt == NULL) &&
3079 ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) {
3080 /* No route to current primary adopt new primary */
3081 stcb->asoc.primary_destination = net;
3083 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb,
3085 /* Validate primary is first */
3086 net = TAILQ_FIRST(&stcb->asoc.nets);
3087 if ((net != stcb->asoc.primary_destination) &&
3088 (stcb->asoc.primary_destination)) {
3090 * first one on the list is NOT the primary sctp_cmpaddr()
3091 * is much more efficent if the primary is the first on the
3094 TAILQ_REMOVE(&stcb->asoc.nets,
3095 stcb->asoc.primary_destination, sctp_next);
3096 TAILQ_INSERT_HEAD(&stcb->asoc.nets,
3097 stcb->asoc.primary_destination, sctp_next);
3104 * allocate an association and add it to the endpoint. The caller must be
3105 * careful to add all additional addresses once they are know right away or
3106 * else the assoc will be may experience a blackout scenario.
3109 sctp_aloc_assoc(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
3110 int for_a_init, int *error, uint32_t override_tag, uint32_t vrf)
3112 struct sctp_tcb *stcb;
3113 struct sctp_association *asoc;
3114 struct sctpasochead *head;
3119 * Assumption made here: Caller has done a
3120 * sctp_findassociation_ep_addr(ep, addr's); to make sure the
3121 * address does not exist already.
3123 if (sctppcbinfo.ipi_count_asoc >= SCTP_MAX_NUM_OF_ASOC) {
3124 /* Hit max assoc, sorry no more */
3128 SCTP_INP_RLOCK(inp);
3129 if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
3131 * If its in the TCP pool, its NOT allowed to create an
3132 * association. The parent listener needs to call
3133 * sctp_aloc_assoc.. or the one-2-many socket. If a peeled
3134 * off, or connected one does this.. its an error.
3136 SCTP_INP_RUNLOCK(inp);
3141 if (sctp_debug_on & SCTP_DEBUG_PCB3) {
3142 printf("Allocate an association for peer:");
3144 sctp_print_address(firstaddr);
3148 ntohs(((struct sockaddr_in *)firstaddr)->sin_port));
3150 #endif /* SCTP_DEBUG */
3151 if (firstaddr->sa_family == AF_INET) {
3152 struct sockaddr_in *sin;
3154 sin = (struct sockaddr_in *)firstaddr;
3155 if ((sin->sin_port == 0) || (sin->sin_addr.s_addr == 0)) {
3156 /* Invalid address */
3157 SCTP_INP_RUNLOCK(inp);
3161 rport = sin->sin_port;
3162 } else if (firstaddr->sa_family == AF_INET6) {
3163 struct sockaddr_in6 *sin6;
3165 sin6 = (struct sockaddr_in6 *)firstaddr;
3166 if ((sin6->sin6_port == 0) ||
3167 (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))) {
3168 /* Invalid address */
3169 SCTP_INP_RUNLOCK(inp);
3173 rport = sin6->sin6_port;
3175 /* not supported family type */
3176 SCTP_INP_RUNLOCK(inp);
3180 SCTP_INP_RUNLOCK(inp);
3181 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
3183 * If you have not performed a bind, then we need to do the
3184 * ephemerial bind for you.
3186 if ((err = sctp_inpcb_bind(inp->sctp_socket,
3187 (struct sockaddr *)NULL,
3188 (struct thread *)NULL
3190 /* bind error, probably perm */
3195 stcb = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_asoc, struct sctp_tcb);
3197 /* out of memory? */
3201 SCTP_INCR_ASOC_COUNT();
3203 bzero(stcb, sizeof(*stcb));
3205 SCTP_TCB_LOCK_INIT(stcb);
3206 SCTP_TCB_SEND_LOCK_INIT(stcb);
3207 /* setup back pointer's */
3208 stcb->sctp_ep = inp;
3209 stcb->sctp_socket = inp->sctp_socket;
3210 if ((err = sctp_init_asoc(inp, asoc, for_a_init, override_tag, vrf))) {
3212 SCTP_TCB_LOCK_DESTROY(stcb);
3213 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
3214 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
3215 SCTP_DECR_ASOC_COUNT();
3220 stcb->rport = rport;
3221 SCTP_INP_INFO_WLOCK();
3222 SCTP_INP_WLOCK(inp);
3223 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
3224 /* inpcb freed while alloc going on */
3225 SCTP_TCB_LOCK_DESTROY(stcb);
3226 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
3227 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
3228 SCTP_INP_WUNLOCK(inp);
3229 SCTP_INP_INFO_WUNLOCK();
3230 SCTP_DECR_ASOC_COUNT();
3234 SCTP_TCB_LOCK(stcb);
3236 /* now that my_vtag is set, add it to the hash */
3237 head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
3238 sctppcbinfo.hashasocmark)];
3239 /* put it in the bucket in the vtag hash of assoc's for the system */
3240 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
3241 SCTP_INP_INFO_WUNLOCK();
3243 if ((err = sctp_add_remote_addr(stcb, firstaddr, SCTP_DO_SETSCOPE, SCTP_ALLOC_ASOC))) {
3244 /* failure.. memory error? */
3246 SCTP_FREE(asoc->strmout);
3247 if (asoc->mapping_array)
3248 SCTP_FREE(asoc->mapping_array);
3250 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
3251 SCTP_DECR_ASOC_COUNT();
3252 SCTP_TCB_LOCK_DESTROY(stcb);
3253 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
3257 /* Init all the timers */
3258 SCTP_OS_TIMER_INIT(&asoc->hb_timer.timer);
3259 SCTP_OS_TIMER_INIT(&asoc->dack_timer.timer);
3260 SCTP_OS_TIMER_INIT(&asoc->strreset_timer.timer);
3261 SCTP_OS_TIMER_INIT(&asoc->asconf_timer.timer);
3262 SCTP_OS_TIMER_INIT(&asoc->shut_guard_timer.timer);
3263 SCTP_OS_TIMER_INIT(&asoc->autoclose_timer.timer);
3264 SCTP_OS_TIMER_INIT(&asoc->delayed_event_timer.timer);
3266 LIST_INSERT_HEAD(&inp->sctp_asoc_list, stcb, sctp_tcblist);
3267 /* now file the port under the hash as well */
3268 if (inp->sctp_tcbhash != NULL) {
3269 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(stcb->rport,
3270 inp->sctp_hashmark)];
3271 LIST_INSERT_HEAD(head, stcb, sctp_tcbhash);
3273 SCTP_INP_WUNLOCK(inp);
3275 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
3276 printf("Association %p now allocated\n", stcb);
3284 sctp_remove_net(struct sctp_tcb *stcb, struct sctp_nets *net)
3286 struct sctp_association *asoc;
3290 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
3291 if (net == asoc->primary_destination) {
3293 struct sctp_nets *lnet;
3295 lnet = TAILQ_FIRST(&asoc->nets);
3296 /* Try to find a confirmed primary */
3297 asoc->primary_destination = sctp_find_alternate_net(stcb, lnet, 0);
3299 if (net == asoc->last_data_chunk_from) {
3301 asoc->last_data_chunk_from = TAILQ_FIRST(&asoc->nets);
3303 if (net == asoc->last_control_chunk_from) {
3305 asoc->last_control_chunk_from = NULL;
3307 sctp_free_remote_addr(net);
3311 * remove a remote endpoint address from an association, it will fail if the
3312 * address does not exist.
3315 sctp_del_remote_addr(struct sctp_tcb *stcb, struct sockaddr *remaddr)
3318 * Here we need to remove a remote address. This is quite simple, we
3319 * first find it in the list of address for the association
3320 * (tasoc->asoc.nets) and then if it is there, we do a LIST_REMOVE
3321 * on that item. Note we do not allow it to be removed if there are
3322 * no other addresses.
3324 struct sctp_association *asoc;
3325 struct sctp_nets *net, *net_tmp;
3329 /* locate the address */
3330 for (net = TAILQ_FIRST(&asoc->nets); net != NULL; net = net_tmp) {
3331 net_tmp = TAILQ_NEXT(net, sctp_next);
3332 if (net->ro._l_addr.sa.sa_family != remaddr->sa_family) {
3335 if (sctp_cmpaddr((struct sockaddr *)&net->ro._l_addr,
3337 /* we found the guy */
3338 if (asoc->numnets < 2) {
3339 /* Must have at LEAST two remote addresses */
3342 sctp_remove_net(stcb, net);
3353 sctp_add_vtag_to_timewait(struct sctp_inpcb *inp, uint32_t tag, uint32_t time)
3355 struct sctpvtaghead *chain;
3356 struct sctp_tagblock *twait_block;
3360 SCTP_GETTIME_TIMEVAL(&now);
3361 chain = &sctppcbinfo.vtag_timewait[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
3363 if (!SCTP_LIST_EMPTY(chain)) {
3364 /* Block(s) present, lets find space, and expire on the fly */
3365 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
3366 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
3367 if ((twait_block->vtag_block[i].v_tag == 0) &&
3369 twait_block->vtag_block[i].tv_sec_at_expire =
3371 twait_block->vtag_block[i].v_tag = tag;
3373 } else if ((twait_block->vtag_block[i].v_tag) &&
3374 ((long)twait_block->vtag_block[i].tv_sec_at_expire >
3376 /* Audit expires this guy */
3377 twait_block->vtag_block[i].tv_sec_at_expire = 0;
3378 twait_block->vtag_block[i].v_tag = 0;
3380 /* Reuse it for my new tag */
3381 twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec + SCTP_TIME_WAIT;
3382 twait_block->vtag_block[0].v_tag = tag;
3389 * We only do up to the block where we can
3390 * place our tag for audits
3396 /* Need to add a new block to chain */
3398 SCTP_MALLOC(twait_block, struct sctp_tagblock *,
3399 sizeof(struct sctp_tagblock), "TimeWait");
3400 if (twait_block == NULL) {
3403 memset(twait_block, 0, sizeof(struct sctp_tagblock));
3404 LIST_INSERT_HEAD(chain, twait_block, sctp_nxt_tagblock);
3405 twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec +
3407 twait_block->vtag_block[0].v_tag = tag;
3413 sctp_iterator_asoc_being_freed(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
3415 struct sctp_iterator *it;
3418 * Unlock the tcb lock we do this so we avoid a dead lock scenario
3419 * where the iterator is waiting on the TCB lock and the TCB lock is
3420 * waiting on the iterator lock.
3422 it = stcb->asoc.stcb_starting_point_for_iterator;
3426 if (it->inp != stcb->sctp_ep) {
3427 /* hmm, focused on the wrong one? */
3430 if (it->stcb != stcb) {
3433 it->stcb = LIST_NEXT(stcb, sctp_tcblist);
3434 if (it->stcb == NULL) {
3435 /* done with all asoc's in this assoc */
3436 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
3439 it->inp = LIST_NEXT(inp, sctp_list);
3445 * Free the association after un-hashing the remote port.
3448 sctp_free_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int from_inpcbfree, int from_location)
3451 struct sctp_association *asoc;
3452 struct sctp_nets *net, *prev;
3453 struct sctp_laddr *laddr;
3454 struct sctp_tmit_chunk *chk;
3455 struct sctp_asconf_addr *aparam;
3456 struct sctp_stream_reset_list *liste;
3457 struct sctp_queued_to_read *sq;
3458 struct sctp_stream_queue_pending *sp;
3459 sctp_sharedkey_t *shared_key;
3464 /* first, lets purge the entry from the hash table. */
3466 #ifdef SCTP_LOG_CLOSING
3467 sctp_log_closing(inp, stcb, 6);
3469 if (stcb->asoc.state == 0) {
3470 #ifdef SCTP_LOG_CLOSING
3471 sctp_log_closing(inp, NULL, 7);
3473 /* there is no asoc, really TSNH :-0 */
3477 if (stcb->freed_from_where == 0) {
3478 /* Only record the first place free happened from */
3479 stcb->freed_from_where = from_location;
3484 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3485 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
3486 /* nothing around */
3489 so = inp->sctp_socket;
3492 * We used timer based freeing if a reader or writer is in the way.
3493 * So we first check if we are actually being called from a timer,
3494 * if so we abort early if a reader or writer is still in the way.
3496 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) &&
3497 (from_inpcbfree == SCTP_NORMAL_PROC)) {
3499 * is it the timer driving us? if so are the reader/writers
3502 if (stcb->asoc.refcnt) {
3503 /* nope, reader or writer in the way */
3504 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
3505 /* no asoc destroyed */
3506 SCTP_TCB_UNLOCK(stcb);
3507 #ifdef SCTP_LOG_CLOSING
3508 sctp_log_closing(inp, stcb, 8);
3513 /* now clean up any other timers */
3514 SCTP_OS_TIMER_STOP(&asoc->hb_timer.timer);
3515 SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
3516 SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
3517 SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
3518 SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
3519 SCTP_OS_TIMER_STOP(&asoc->shut_guard_timer.timer);
3520 SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
3522 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3523 SCTP_OS_TIMER_STOP(&net->fr_timer.timer);
3524 SCTP_OS_TIMER_STOP(&net->rxt_timer.timer);
3525 SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
3527 /* Now the read queue needs to be cleaned up (only once) */
3529 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0) {
3530 SCTP_INP_READ_LOCK(inp);
3531 TAILQ_FOREACH(sq, &inp->read_queue, next) {
3532 if (sq->stcb == stcb) {
3533 sq->do_not_ref_stcb = 1;
3534 sq->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
3536 * If there is no end, there never will be
3539 if (sq->end_added == 0) {
3540 /* Held for PD-API clear that. */
3541 sq->pdapi_aborted = 1;
3542 sq->held_length = 0;
3543 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT)) {
3545 * Need to add a PD-API
3546 * aborted indication.
3547 * Setting the control_pdapi
3548 * assures that it will be
3549 * added right after this
3552 stcb->asoc.control_pdapi = sq;
3553 sctp_notify_partial_delivery_indication(stcb,
3554 SCTP_PARTIAL_DELIVERY_ABORTED, 1);
3555 stcb->asoc.control_pdapi = NULL;
3558 /* Add an end to wake them */
3563 SCTP_INP_READ_UNLOCK(inp);
3564 if (stcb->block_entry) {
3566 stcb->block_entry->error = ECONNRESET;
3567 stcb->block_entry = NULL;
3570 stcb->asoc.state |= SCTP_STATE_ABOUT_TO_BE_FREED;
3571 if ((from_inpcbfree != SCTP_PCBFREE_FORCE) && (stcb->asoc.refcnt)) {
3573 * reader or writer in the way, we have hopefully given him
3574 * something to chew on above.
3576 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
3577 SCTP_TCB_UNLOCK(stcb);
3579 SCTP_INP_RLOCK(inp);
3580 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3581 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
3582 /* nothing around */
3585 /* Wake any reader/writers */
3586 sctp_sorwakeup(inp, so);
3587 sctp_sowwakeup(inp, so);
3589 SCTP_INP_RUNLOCK(inp);
3592 #ifdef SCTP_LOG_CLOSING
3593 sctp_log_closing(inp, stcb, 9);
3595 /* no asoc destroyed */
3598 #ifdef SCTP_LOG_CLOSING
3599 sctp_log_closing(inp, stcb, 10);
3602 * When I reach here, no others want to kill the assoc yet.. and I
3603 * own the lock. Now its possible an abort comes in when I do the
3604 * lock exchange below to grab all the locks to do the final take
3605 * out. to prevent this we increment the count, which will start a
3606 * timer and blow out above thus assuring us that we hold exclusive
3607 * killing of the asoc. Note that after getting back the TCB lock we
3608 * will go ahead and increment the counter back up and stop any
3609 * timer a passing stranger may have started :-S
3611 if (from_inpcbfree == SCTP_NORMAL_PROC) {
3612 atomic_add_int(&stcb->asoc.refcnt, 1);
3614 SCTP_TCB_UNLOCK(stcb);
3616 SCTP_ITERATOR_LOCK();
3617 SCTP_INP_INFO_WLOCK();
3618 SCTP_INP_WLOCK(inp);
3619 SCTP_TCB_LOCK(stcb);
3621 /* Double check the GONE flag */
3622 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3623 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
3624 /* nothing around */
3627 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3628 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3630 * For TCP type we need special handling when we are
3631 * connected. We also include the peel'ed off ones to.
3633 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3634 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
3635 inp->sctp_flags |= SCTP_PCB_FLAGS_WAS_CONNECTED;
3638 if (so->so_rcv.sb_cc == 0) {
3639 so->so_state &= ~(SS_ISCONNECTING |
3640 SS_ISDISCONNECTING |
3645 sctp_sowwakeup(inp, so);
3646 sctp_sorwakeup(inp, so);
3647 wakeup(&so->so_timeo);
3652 * Make it invalid too, that way if its about to run it will abort
3655 sctp_iterator_asoc_being_freed(inp, stcb);
3656 /* re-increment the lock */
3657 if (from_inpcbfree == SCTP_NORMAL_PROC) {
3658 atomic_add_int(&stcb->asoc.refcnt, -1);
3661 if (inp->sctp_tcbhash) {
3662 LIST_REMOVE(stcb, sctp_tcbhash);
3664 if (stcb->asoc.in_restart_hash) {
3665 LIST_REMOVE(stcb, sctp_tcbrestarhash);
3667 /* Now lets remove it from the list of ALL associations in the EP */
3668 LIST_REMOVE(stcb, sctp_tcblist);
3669 if (from_inpcbfree == SCTP_NORMAL_PROC) {
3670 SCTP_INP_INCR_REF(inp);
3671 SCTP_INP_WUNLOCK(inp);
3672 SCTP_ITERATOR_UNLOCK();
3674 /* pull from vtag hash */
3675 LIST_REMOVE(stcb, sctp_asocs);
3676 sctp_add_vtag_to_timewait(inp, asoc->my_vtag, SCTP_TIME_WAIT);
3680 * Now restop the timers to be sure - this is paranoia at is finest!
3682 SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
3683 SCTP_OS_TIMER_STOP(&asoc->hb_timer.timer);
3684 SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
3685 SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
3686 SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
3687 SCTP_OS_TIMER_STOP(&asoc->shut_guard_timer.timer);
3688 SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
3689 SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
3691 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3692 SCTP_OS_TIMER_STOP(&net->fr_timer.timer);
3693 SCTP_OS_TIMER_STOP(&net->rxt_timer.timer);
3694 SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
3697 asoc->strreset_timer.type = SCTP_TIMER_TYPE_NONE;
3700 * The chunk lists and such SHOULD be empty but we check them just
3703 /* anything on the wheel needs to be removed */
3704 for (i = 0; i < asoc->streamoutcnt; i++) {
3705 struct sctp_stream_out *outs;
3707 outs = &asoc->strmout[i];
3708 /* now clean up any chunks here */
3709 sp = TAILQ_FIRST(&outs->outqueue);
3711 TAILQ_REMOVE(&outs->outqueue, sp, next);
3713 sctp_m_freem(sp->data);
3715 sp->tail_mbuf = NULL;
3717 sctp_free_remote_addr(sp->net);
3718 sctp_free_spbufspace(stcb, asoc, sp);
3719 /* Free the zone stuff */
3720 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_strmoq, sp);
3721 SCTP_DECR_STRMOQ_COUNT();
3722 sp = TAILQ_FIRST(&outs->outqueue);
3726 while ((sp = TAILQ_FIRST(&asoc->free_strmoq)) != NULL) {
3727 TAILQ_REMOVE(&asoc->free_strmoq, sp, next);
3729 sctp_m_freem(sp->data);
3731 sp->tail_mbuf = NULL;
3733 /* Free the zone stuff */
3734 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_strmoq, sp);
3735 SCTP_DECR_STRMOQ_COUNT();
3736 atomic_add_int(&sctppcbinfo.ipi_free_strmoq, -1);
3739 while ((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) {
3740 TAILQ_REMOVE(&asoc->resetHead, liste, next_resp);
3744 sq = TAILQ_FIRST(&asoc->pending_reply_queue);
3746 TAILQ_REMOVE(&asoc->pending_reply_queue, sq, next);
3748 sctp_m_freem(sq->data);
3751 sctp_free_remote_addr(sq->whoFrom);
3754 /* Free the ctl entry */
3755 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_readq, sq);
3756 SCTP_DECR_READQ_COUNT();
3757 sq = TAILQ_FIRST(&asoc->pending_reply_queue);
3760 chk = TAILQ_FIRST(&asoc->free_chunks);
3762 TAILQ_REMOVE(&asoc->free_chunks, chk, sctp_next);
3764 sctp_m_freem(chk->data);
3768 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3769 SCTP_DECR_CHK_COUNT();
3770 atomic_subtract_int(&sctppcbinfo.ipi_free_chunks, 1);
3771 asoc->free_chunk_cnt--;
3772 chk = TAILQ_FIRST(&asoc->free_chunks);
3774 /* pending send queue SHOULD be empty */
3775 if (!TAILQ_EMPTY(&asoc->send_queue)) {
3776 chk = TAILQ_FIRST(&asoc->send_queue);
3778 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
3780 sctp_m_freem(chk->data);
3784 sctp_free_remote_addr(chk->whoTo);
3785 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3786 SCTP_DECR_CHK_COUNT();
3787 chk = TAILQ_FIRST(&asoc->send_queue);
3792 printf("Freed %d from send_queue\n", ccnt);
3796 /* sent queue SHOULD be empty */
3797 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3798 chk = TAILQ_FIRST(&asoc->sent_queue);
3800 TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
3802 sctp_m_freem(chk->data);
3806 sctp_free_remote_addr(chk->whoTo);
3807 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3808 SCTP_DECR_CHK_COUNT();
3809 chk = TAILQ_FIRST(&asoc->sent_queue);
3814 printf("Freed %d from sent_queue\n", ccnt);
3818 /* control queue MAY not be empty */
3819 if (!TAILQ_EMPTY(&asoc->control_send_queue)) {
3820 chk = TAILQ_FIRST(&asoc->control_send_queue);
3822 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
3824 sctp_m_freem(chk->data);
3828 sctp_free_remote_addr(chk->whoTo);
3829 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3830 SCTP_DECR_CHK_COUNT();
3831 chk = TAILQ_FIRST(&asoc->control_send_queue);
3836 printf("Freed %d from ctrl_queue\n", ccnt);
3840 if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
3841 chk = TAILQ_FIRST(&asoc->reasmqueue);
3843 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
3845 sctp_m_freem(chk->data);
3848 sctp_free_remote_addr(chk->whoTo);
3850 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3851 SCTP_DECR_CHK_COUNT();
3852 chk = TAILQ_FIRST(&asoc->reasmqueue);
3857 printf("Freed %d from reasm_queue\n", ccnt);
3861 if (asoc->mapping_array) {
3862 SCTP_FREE(asoc->mapping_array);
3863 asoc->mapping_array = NULL;
3865 /* the stream outs */
3866 if (asoc->strmout) {
3867 SCTP_FREE(asoc->strmout);
3868 asoc->strmout = NULL;
3870 asoc->streamoutcnt = 0;
3872 struct sctp_queued_to_read *ctl;
3875 for (i = 0; i < asoc->streamincnt; i++) {
3876 if (!TAILQ_EMPTY(&asoc->strmin[i].inqueue)) {
3877 /* We have somethings on the streamin queue */
3878 ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue);
3880 TAILQ_REMOVE(&asoc->strmin[i].inqueue,
3882 sctp_free_remote_addr(ctl->whoFrom);
3884 sctp_m_freem(ctl->data);
3888 * We don't free the address here
3889 * since all the net's were freed
3892 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_readq, ctl);
3893 SCTP_DECR_READQ_COUNT();
3894 ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue);
3898 SCTP_FREE(asoc->strmin);
3899 asoc->strmin = NULL;
3901 asoc->streamincnt = 0;
3902 while (!TAILQ_EMPTY(&asoc->nets)) {
3903 net = TAILQ_FIRST(&asoc->nets);
3904 /* pull from list */
3905 if ((sctppcbinfo.ipi_count_raddr == 0) || (prev == net)) {
3907 panic("no net's left alloc'ed, or list points to itself");
3912 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
3913 sctp_free_remote_addr(net);
3916 while (!SCTP_LIST_EMPTY(&asoc->sctp_restricted_addrs)) {
3917 laddr = LIST_FIRST(&asoc->sctp_restricted_addrs);
3918 sctp_remove_laddr(laddr);
3921 /* pending asconf (address) parameters */
3922 while (!TAILQ_EMPTY(&asoc->asconf_queue)) {
3923 aparam = TAILQ_FIRST(&asoc->asconf_queue);
3924 TAILQ_REMOVE(&asoc->asconf_queue, aparam, next);
3927 if (asoc->last_asconf_ack_sent != NULL) {
3928 sctp_m_freem(asoc->last_asconf_ack_sent);
3929 asoc->last_asconf_ack_sent = NULL;
3931 /* clean up auth stuff */
3932 if (asoc->local_hmacs)
3933 sctp_free_hmaclist(asoc->local_hmacs);
3934 if (asoc->peer_hmacs)
3935 sctp_free_hmaclist(asoc->peer_hmacs);
3937 if (asoc->local_auth_chunks)
3938 sctp_free_chunklist(asoc->local_auth_chunks);
3939 if (asoc->peer_auth_chunks)
3940 sctp_free_chunklist(asoc->peer_auth_chunks);
3942 sctp_free_authinfo(&asoc->authinfo);
3944 shared_key = LIST_FIRST(&asoc->shared_keys);
3945 while (shared_key) {
3946 LIST_REMOVE(shared_key, next);
3947 sctp_free_sharedkey(shared_key);
3948 shared_key = LIST_FIRST(&asoc->shared_keys);
3951 /* Insert new items here :> */
3953 /* Get rid of LOCK */
3954 SCTP_TCB_LOCK_DESTROY(stcb);
3955 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
3956 if (from_inpcbfree == SCTP_NORMAL_PROC) {
3957 SCTP_INP_INFO_WUNLOCK();
3958 SCTP_INP_RLOCK(inp);
3960 #ifdef SCTP_TRACK_FREED_ASOCS
3961 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3962 /* now clean up the tasoc itself */
3963 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
3964 SCTP_DECR_ASOC_COUNT();
3966 LIST_INSERT_HEAD(&inp->sctp_asoc_free_list, stcb, sctp_tcblist);
3969 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
3970 SCTP_DECR_ASOC_COUNT();
3972 if (from_inpcbfree == SCTP_NORMAL_PROC) {
3973 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3975 * If its NOT the inp_free calling us AND sctp_close
3976 * as been called, we call back...
3978 SCTP_INP_RUNLOCK(inp);
3980 * This will start the kill timer (if we are the
3981 * lastone) since we hold an increment yet. But this
3982 * is the only safe way to do this since otherwise
3983 * if the socket closes at the same time we are here
3984 * we might collide in the cleanup.
3986 sctp_inpcb_free(inp, 0, 0);
3987 SCTP_INP_DECR_REF(inp);
3990 /* The socket is still open. */
3991 SCTP_INP_DECR_REF(inp);
3994 if (from_inpcbfree == SCTP_NORMAL_PROC) {
3995 SCTP_INP_RUNLOCK(inp);
3998 /* destroyed the asoc */
3999 #ifdef SCTP_LOG_CLOSING
4000 sctp_log_closing(inp, NULL, 11);
4008 * determine if a destination is "reachable" based upon the addresses bound
4009 * to the current endpoint (e.g. only v4 or v6 currently bound)
4012 * FIX: if we allow assoc-level bindx(), then this needs to be fixed to use
4013 * assoc level v4/v6 flags, as the assoc *may* not have the same address
4014 * types bound as its endpoint
4017 sctp_destination_is_reachable(struct sctp_tcb *stcb, struct sockaddr *destaddr)
4019 struct sctp_inpcb *inp;
4023 * No locks here, the TCB, in all cases is already locked and an
4024 * assoc is up. There is either a INP lock by the caller applied (in
4025 * asconf case when deleting an address) or NOT in the HB case,
4026 * however if HB then the INP increment is up and the INP will not
4027 * be removed (on top of the fact that we have a TCB lock). So we
4028 * only want to read the sctp_flags, which is either bound-all or
4029 * not.. no protection needed since once an assoc is up you can't be
4030 * changing your binding.
4032 inp = stcb->sctp_ep;
4033 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4034 /* if bound all, destination is not restricted */
4036 * RRS: Question during lock work: Is this correct? If you
4037 * are bound-all you still might need to obey the V4--V6
4038 * flags??? IMO this bound-all stuff needs to be removed!
4042 /* NOTE: all "scope" checks are done when local addresses are added */
4043 if (destaddr->sa_family == AF_INET6) {
4044 answer = inp->ip_inp.inp.inp_vflag & INP_IPV6;
4045 } else if (destaddr->sa_family == AF_INET) {
4046 answer = inp->ip_inp.inp.inp_vflag & INP_IPV4;
4048 /* invalid family, so it's unreachable */
4055 * update the inp_vflags on an endpoint
4058 sctp_update_ep_vflag(struct sctp_inpcb *inp)
4060 struct sctp_laddr *laddr;
4062 /* first clear the flag */
4063 inp->ip_inp.inp.inp_vflag = 0;
4064 /* set the flag based on addresses on the ep list */
4065 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4066 if (laddr->ifa == NULL) {
4068 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
4069 printf("An ounce of prevention is worth a pound of cure\n");
4071 #endif /* SCTP_DEBUG */
4074 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
4077 if (laddr->ifa->address.sa.sa_family == AF_INET6) {
4078 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
4079 } else if (laddr->ifa->address.sa.sa_family == AF_INET) {
4080 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
4086 * Add the address to the endpoint local address list There is nothing to be
4087 * done if we are bound to all addresses
4090 sctp_add_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa, uint32_t action)
4092 struct sctp_laddr *laddr;
4097 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4098 /* You are already bound to all. You have it already */
4101 if (ifa->address.sa.sa_family == AF_INET6) {
4102 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
4103 /* Can't bind a non-useable addr. */
4107 /* first, is it already present? */
4108 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4109 if (laddr->ifa == ifa) {
4116 /* Not in the ep list */
4117 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, action);
4121 /* update inp_vflag flags */
4122 if (ifa->address.sa.sa_family == AF_INET6) {
4123 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
4124 } else if (ifa->address.sa.sa_family == AF_INET) {
4125 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
4133 * select a new (hopefully reachable) destination net (should only be used
4134 * when we deleted an ep addr that is the only usable source address to reach
4135 * the destination net)
4138 sctp_select_primary_destination(struct sctp_tcb *stcb)
4140 struct sctp_nets *net;
4142 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4143 /* for now, we'll just pick the first reachable one we find */
4144 if (net->dest_state & SCTP_ADDR_UNCONFIRMED)
4146 if (sctp_destination_is_reachable(stcb,
4147 (struct sockaddr *)&net->ro._l_addr)) {
4148 /* found a reachable destination */
4149 stcb->asoc.primary_destination = net;
4152 /* I can't there from here! ...we're gonna die shortly... */
4157 * Delete the address from the endpoint local address list There is nothing
4158 * to be done if we are bound to all addresses
4161 sctp_del_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
4163 struct sctp_laddr *laddr;
4167 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4168 /* You are already bound to all. You have it already */
4171 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4172 if (laddr->ifa == ifa) {
4177 if (fnd && (inp->laddr_count < 2)) {
4178 /* can't delete unless there are at LEAST 2 addresses */
4183 * clean up any use of this address go through our
4184 * associations and clear any last_used_address that match
4185 * this one for each assoc, see if a new primary_destination
4188 struct sctp_tcb *stcb;
4190 /* clean up "next_addr_touse" */
4191 if (inp->next_addr_touse == laddr)
4192 /* delete this address */
4193 inp->next_addr_touse = NULL;
4195 /* clean up "last_used_address" */
4196 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
4197 struct sctp_nets *net;
4199 SCTP_TCB_LOCK(stcb);
4200 if (stcb->asoc.last_used_address == laddr)
4201 /* delete this address */
4202 stcb->asoc.last_used_address = NULL;
4204 * Now spin through all the nets and purge any ref
4207 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4208 if (net->ro._s_addr &&
4209 (net->ro._s_addr->ifa == laddr->ifa)) {
4210 /* Yep, purge src address selected */
4213 /* delete this address if cached */
4217 net->ro.ro_rt = NULL;
4219 sctp_free_ifa(net->ro._s_addr);
4220 net->ro._s_addr = NULL;
4221 net->src_addr_selected = 0;
4224 SCTP_TCB_UNLOCK(stcb);
4225 } /* for each tcb */
4226 /* remove it from the ep list */
4227 sctp_remove_laddr(laddr);
4229 /* update inp_vflag flags */
4230 sctp_update_ep_vflag(inp);
4236 * Add the addr to the TCB local address list For the BOUNDALL or dynamic
4237 * case, this is a "pending" address list (eg. addresses waiting for an
4238 * ASCONF-ACK response) For the subset binding, static case, this is a
4239 * "valid" address list
4242 sctp_add_local_addr_assoc(struct sctp_tcb *stcb, struct sctp_ifa *ifa, int restricted_list)
4244 struct sctp_inpcb *inp;
4245 struct sctp_laddr *laddr;
4246 struct sctpladdr *list;
4250 * Assumes TCB is locked.. and possibly the INP. May need to
4251 * confirm/fix that if we need it and is not the case.
4253 list = &stcb->asoc.sctp_restricted_addrs;
4255 inp = stcb->sctp_ep;
4256 if (ifa->address.sa.sa_family == AF_INET6) {
4257 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
4258 /* Can't bind a non-existent addr. */
4262 /* does the address already exist? */
4263 LIST_FOREACH(laddr, list, sctp_nxt_addr) {
4264 if (laddr->ifa == ifa) {
4269 /* add to the list */
4270 error = sctp_insert_laddr(list, ifa, 0);
4277 * insert an laddr entry with the given ifa for the desired list
4280 sctp_insert_laddr(struct sctpladdr *list, struct sctp_ifa *ifa, uint32_t act)
4282 struct sctp_laddr *laddr;
4284 laddr = SCTP_ZONE_GET(sctppcbinfo.ipi_zone_laddr, struct sctp_laddr);
4285 if (laddr == NULL) {
4286 /* out of memory? */
4289 SCTP_INCR_LADDR_COUNT();
4290 bzero(laddr, sizeof(*laddr));
4292 laddr->action = act;
4293 atomic_add_int(&ifa->refcount, 1);
4295 LIST_INSERT_HEAD(list, laddr, sctp_nxt_addr);
4301 * Remove an laddr entry from the local address list (on an assoc)
4304 sctp_remove_laddr(struct sctp_laddr *laddr)
4307 /* remove from the list */
4308 LIST_REMOVE(laddr, sctp_nxt_addr);
4309 sctp_free_ifa(laddr->ifa);
4310 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_laddr, laddr);
4311 SCTP_DECR_LADDR_COUNT();
4315 * Remove an address from the TCB local address list
4318 sctp_del_local_addr_assoc(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
4320 struct sctp_inpcb *inp;
4321 struct sctp_laddr *laddr;
4324 * This is called by asconf work. It is assumed that a) The TCB is
4325 * locked and b) The INP is locked. This is true in as much as I can
4326 * trace through the entry asconf code where I did these locks.
4327 * Again, the ASCONF code is a bit different in that it does lock
4328 * the INP during its work often times. This must be since we don't
4329 * want other proc's looking up things while what they are looking
4330 * up is changing :-D
4333 inp = stcb->sctp_ep;
4334 /* if subset bound and don't allow ASCONF's, can't delete last */
4335 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) &&
4336 (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF) == 0)) {
4337 if (stcb->asoc.numnets < 2) {
4338 /* can't delete last address */
4342 LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
4343 /* remove the address if it exists */
4344 if (laddr->ifa == NULL)
4346 if (laddr->ifa == ifa) {
4347 sctp_remove_laddr(laddr);
4352 /* address not found! */
4356 static char sctp_pcb_initialized = 0;
4359 * Temporarily remove for __APPLE__ until we use the Tiger equivalents
4362 static int sctp_max_number_of_assoc = SCTP_MAX_NUM_OF_ASOC;
4363 static int sctp_scale_up_for_address = SCTP_SCALE_FOR_ADDR;
4369 * SCTP initialization for the PCB structures should be called by
4370 * the sctp_init() funciton.
4374 if (sctp_pcb_initialized != 0) {
4375 /* error I was called twice */
4378 sctp_pcb_initialized = 1;
4380 bzero(&sctpstat, sizeof(struct sctpstat));
4381 SCTP_GETTIME_TIMEVAL(&sctpstat.sctps_discontinuitytime);
4382 /* init the empty list of (All) Endpoints */
4383 LIST_INIT(&sctppcbinfo.listhead);
4385 /* init the iterator head */
4386 TAILQ_INIT(&sctppcbinfo.iteratorhead);
4388 /* init the hash table of endpoints */
4389 TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", &sctp_hashtblsize);
4390 TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", &sctp_pcbtblsize);
4391 TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", &sctp_chunkscale);
4392 sctppcbinfo.sctp_asochash = SCTP_HASH_INIT((sctp_hashtblsize * 31),
4393 &sctppcbinfo.hashasocmark);
4394 sctppcbinfo.sctp_ephash = SCTP_HASH_INIT(sctp_hashtblsize,
4395 &sctppcbinfo.hashmark);
4396 sctppcbinfo.sctp_tcpephash = SCTP_HASH_INIT(sctp_hashtblsize,
4397 &sctppcbinfo.hashtcpmark);
4398 sctppcbinfo.hashtblsize = sctp_hashtblsize;
4400 /* init the small hash table we use to track restarted asoc's */
4401 sctppcbinfo.sctp_restarthash = SCTP_HASH_INIT(SCTP_STACK_VTAG_HASH_SIZE,
4402 &sctppcbinfo.hashrestartmark);
4405 sctppcbinfo.sctp_vrfhash = SCTP_HASH_INIT(SCTP_SIZE_OF_VRF_HASH,
4406 &sctppcbinfo.hashvrfmark);
4408 /* init the zones */
4410 * FIX ME: Should check for NULL returns, but if it does fail we are
4411 * doomed to panic anyways... add later maybe.
4413 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_ep, "sctp_ep",
4414 sizeof(struct sctp_inpcb), maxsockets);
4416 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_asoc, "sctp_asoc",
4417 sizeof(struct sctp_tcb), sctp_max_number_of_assoc);
4419 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_laddr, "sctp_laddr",
4420 sizeof(struct sctp_laddr),
4421 (sctp_max_number_of_assoc * sctp_scale_up_for_address));
4423 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_net, "sctp_raddr",
4424 sizeof(struct sctp_nets),
4425 (sctp_max_number_of_assoc * sctp_scale_up_for_address));
4427 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_chunk, "sctp_chunk",
4428 sizeof(struct sctp_tmit_chunk),
4429 (sctp_max_number_of_assoc * sctp_chunkscale));
4431 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_readq, "sctp_readq",
4432 sizeof(struct sctp_queued_to_read),
4433 (sctp_max_number_of_assoc * sctp_chunkscale));
4435 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_strmoq, "sctp_stream_msg_out",
4436 sizeof(struct sctp_stream_queue_pending),
4437 (sctp_max_number_of_assoc * sctp_chunkscale));
4439 /* Master Lock INIT for info structure */
4440 SCTP_INP_INFO_LOCK_INIT();
4441 SCTP_STATLOG_INIT_LOCK();
4442 SCTP_ITERATOR_LOCK_INIT();
4444 SCTP_IPI_COUNT_INIT();
4445 SCTP_IPI_ADDR_INIT();
4446 SCTP_IPI_ITERATOR_WQ_INIT();
4448 LIST_INIT(&sctppcbinfo.addr_wq);
4450 /* not sure if we need all the counts */
4451 sctppcbinfo.ipi_count_ep = 0;
4452 /* assoc/tcb zone info */
4453 sctppcbinfo.ipi_count_asoc = 0;
4454 /* local addrlist zone info */
4455 sctppcbinfo.ipi_count_laddr = 0;
4456 /* remote addrlist zone info */
4457 sctppcbinfo.ipi_count_raddr = 0;
4459 sctppcbinfo.ipi_count_chunk = 0;
4461 /* socket queue zone info */
4462 sctppcbinfo.ipi_count_readq = 0;
4464 /* stream out queue cont */
4465 sctppcbinfo.ipi_count_strmoq = 0;
4467 sctppcbinfo.ipi_free_strmoq = 0;
4468 sctppcbinfo.ipi_free_chunks = 0;
4470 SCTP_OS_TIMER_INIT(&sctppcbinfo.addr_wq_timer.timer);
4472 /* Init the TIMEWAIT list */
4473 for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) {
4474 LIST_INIT(&sctppcbinfo.vtag_timewait[i]);
4477 #if defined(SCTP_USE_THREAD_BASED_ITERATOR)
4478 sctppcbinfo.iterator_running = 0;
4479 sctp_startup_iterator();
4483 * INIT the default VRF which for BSD is the only one, other O/S's
4484 * may have more. But initially they must start with one and then
4485 * add the VRF's as addresses are added.
4487 sctp_init_vrf_list(SCTP_DEFAULT_VRF);
4493 sctp_load_addresses_from_init(struct sctp_tcb *stcb, struct mbuf *m,
4494 int iphlen, int offset, int limit, struct sctphdr *sh,
4495 struct sockaddr *altsa)
4498 * grub through the INIT pulling addresses and loading them to the
4499 * nets structure in the asoc. The from address in the mbuf should
4500 * also be loaded (if it is not already). This routine can be called
4501 * with either INIT or INIT-ACK's as long as the m points to the IP
4502 * packet and the offset points to the beginning of the parameters.
4504 struct sctp_inpcb *inp, *l_inp;
4505 struct sctp_nets *net, *net_tmp;
4507 struct sctp_paramhdr *phdr, parm_buf;
4508 struct sctp_tcb *stcb_tmp;
4509 uint16_t ptype, plen;
4510 struct sockaddr *sa;
4511 struct sockaddr_storage dest_store;
4512 struct sockaddr *local_sa = (struct sockaddr *)&dest_store;
4513 struct sockaddr_in sin;
4514 struct sockaddr_in6 sin6;
4515 uint8_t random_store[SCTP_PARAM_BUFFER_SIZE];
4516 struct sctp_auth_random *random = NULL;
4517 uint16_t random_len = 0;
4518 uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE];
4519 struct sctp_auth_hmac_algo *hmacs = NULL;
4520 uint16_t hmacs_len = 0;
4521 uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE];
4522 struct sctp_auth_chunk_list *chunks = NULL;
4523 uint16_t num_chunks = 0;
4524 sctp_key_t *new_key;
4526 int got_random = 0, got_hmacs = 0, got_chklist = 0;
4528 /* First get the destination address setup too. */
4529 memset(&sin, 0, sizeof(sin));
4530 memset(&sin6, 0, sizeof(sin6));
4532 sin.sin_family = AF_INET;
4533 sin.sin_len = sizeof(sin);
4534 sin.sin_port = stcb->rport;
4536 sin6.sin6_family = AF_INET6;
4537 sin6.sin6_len = sizeof(struct sockaddr_in6);
4538 sin6.sin6_port = stcb->rport;
4539 if (altsa == NULL) {
4540 iph = mtod(m, struct ip *);
4541 if (iph->ip_v == IPVERSION) {
4543 struct sockaddr_in *sin_2;
4545 sin_2 = (struct sockaddr_in *)(local_sa);
4546 memset(sin_2, 0, sizeof(sin));
4547 sin_2->sin_family = AF_INET;
4548 sin_2->sin_len = sizeof(sin);
4549 sin_2->sin_port = sh->dest_port;
4550 sin_2->sin_addr.s_addr = iph->ip_dst.s_addr;
4551 sin.sin_addr = iph->ip_src;
4552 sa = (struct sockaddr *)&sin;
4553 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
4555 struct ip6_hdr *ip6;
4556 struct sockaddr_in6 *sin6_2;
4558 ip6 = mtod(m, struct ip6_hdr *);
4559 sin6_2 = (struct sockaddr_in6 *)(local_sa);
4560 memset(sin6_2, 0, sizeof(sin6));
4561 sin6_2->sin6_family = AF_INET6;
4562 sin6_2->sin6_len = sizeof(struct sockaddr_in6);
4563 sin6_2->sin6_port = sh->dest_port;
4564 sin6.sin6_addr = ip6->ip6_src;
4565 sa = (struct sockaddr *)&sin6;
4571 * For cookies we use the src address NOT from the packet
4572 * but from the original INIT
4576 /* Turn off ECN until we get through all params */
4577 stcb->asoc.ecn_allowed = 0;
4578 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4579 /* mark all addresses that we have currently on the list */
4580 net->dest_state |= SCTP_ADDR_NOT_IN_ASSOC;
4582 /* does the source address already exist? if so skip it */
4583 l_inp = inp = stcb->sctp_ep;
4585 atomic_add_int(&stcb->asoc.refcnt, 1);
4586 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net_tmp, local_sa, stcb);
4587 atomic_add_int(&stcb->asoc.refcnt, -1);
4589 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || inp == NULL) {
4590 /* we must add the source address */
4591 /* no scope set here since we have a tcb already. */
4592 if ((sa->sa_family == AF_INET) &&
4593 (stcb->asoc.ipv4_addr_legal)) {
4594 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_2)) {
4597 } else if ((sa->sa_family == AF_INET6) &&
4598 (stcb->asoc.ipv6_addr_legal)) {
4599 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) {
4604 if (net_tmp != NULL && stcb_tmp == stcb) {
4605 net_tmp->dest_state &= ~SCTP_ADDR_NOT_IN_ASSOC;
4606 } else if (stcb_tmp != stcb) {
4607 /* It belongs to another association? */
4608 SCTP_TCB_UNLOCK(stcb_tmp);
4612 if (stcb->asoc.state == 0) {
4613 /* the assoc was freed? */
4616 /* now we must go through each of the params. */
4617 phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
4619 ptype = ntohs(phdr->param_type);
4620 plen = ntohs(phdr->param_length);
4622 * printf("ptype => %0x, plen => %d\n", (uint32_t)ptype,
4625 if (offset + plen > limit) {
4631 if (ptype == SCTP_IPV4_ADDRESS) {
4632 if (stcb->asoc.ipv4_addr_legal) {
4633 struct sctp_ipv4addr_param *p4, p4_buf;
4635 /* ok get the v4 address and check/add */
4636 phdr = sctp_get_next_param(m, offset,
4637 (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
4638 if (plen != sizeof(struct sctp_ipv4addr_param) ||
4642 p4 = (struct sctp_ipv4addr_param *)phdr;
4643 sin.sin_addr.s_addr = p4->addr;
4644 sa = (struct sockaddr *)&sin;
4645 inp = stcb->sctp_ep;
4646 atomic_add_int(&stcb->asoc.refcnt, 1);
4647 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
4649 atomic_add_int(&stcb->asoc.refcnt, -1);
4651 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) ||
4653 /* we must add the source address */
4655 * no scope set since we have a tcb
4660 * we must validate the state again
4663 if (stcb->asoc.state == 0) {
4664 /* the assoc was freed? */
4667 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_4)) {
4670 } else if (stcb_tmp == stcb) {
4671 if (stcb->asoc.state == 0) {
4672 /* the assoc was freed? */
4678 ~SCTP_ADDR_NOT_IN_ASSOC;
4682 * strange, address is in another
4683 * assoc? straighten out locks.
4685 if (stcb->asoc.state == 0) {
4686 /* the assoc was freed? */
4692 } else if (ptype == SCTP_IPV6_ADDRESS) {
4693 if (stcb->asoc.ipv6_addr_legal) {
4694 /* ok get the v6 address and check/add */
4695 struct sctp_ipv6addr_param *p6, p6_buf;
4697 phdr = sctp_get_next_param(m, offset,
4698 (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
4699 if (plen != sizeof(struct sctp_ipv6addr_param) ||
4703 p6 = (struct sctp_ipv6addr_param *)phdr;
4704 memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
4706 sa = (struct sockaddr *)&sin6;
4707 inp = stcb->sctp_ep;
4708 atomic_add_int(&stcb->asoc.refcnt, 1);
4709 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
4711 atomic_add_int(&stcb->asoc.refcnt, -1);
4712 if (stcb_tmp == NULL && (inp == stcb->sctp_ep ||
4715 * we must validate the state again
4718 if (stcb->asoc.state == 0) {
4719 /* the assoc was freed? */
4723 * we must add the address, no scope
4726 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_5)) {
4729 } else if (stcb_tmp == stcb) {
4731 * we must validate the state again
4734 if (stcb->asoc.state == 0) {
4735 /* the assoc was freed? */
4741 ~SCTP_ADDR_NOT_IN_ASSOC;
4745 * strange, address is in another
4746 * assoc? straighten out locks.
4748 if (stcb->asoc.state == 0) {
4749 /* the assoc was freed? */
4755 } else if (ptype == SCTP_ECN_CAPABLE) {
4756 stcb->asoc.ecn_allowed = 1;
4757 } else if (ptype == SCTP_ULP_ADAPTATION) {
4758 if (stcb->asoc.state != SCTP_STATE_OPEN) {
4759 struct sctp_adaptation_layer_indication ai,
4762 phdr = sctp_get_next_param(m, offset,
4763 (struct sctp_paramhdr *)&ai, sizeof(ai));
4764 aip = (struct sctp_adaptation_layer_indication *)phdr;
4765 sctp_ulp_notify(SCTP_NOTIFY_ADAPTATION_INDICATION,
4766 stcb, ntohl(aip->indication), NULL);
4768 } else if (ptype == SCTP_SET_PRIM_ADDR) {
4769 struct sctp_asconf_addr_param lstore, *fee;
4770 struct sctp_asconf_addrv4_param *fii;
4772 struct sockaddr *lsa = NULL;
4774 stcb->asoc.peer_supports_asconf = 1;
4775 if (plen > sizeof(lstore)) {
4778 phdr = sctp_get_next_param(m, offset,
4779 (struct sctp_paramhdr *)&lstore, plen);
4783 fee = (struct sctp_asconf_addr_param *)phdr;
4784 lptype = ntohs(fee->addrp.ph.param_type);
4785 if (lptype == SCTP_IPV4_ADDRESS) {
4787 sizeof(struct sctp_asconf_addrv4_param)) {
4788 printf("Sizeof setprim in init/init ack not %d but %d - ignored\n",
4789 (int)sizeof(struct sctp_asconf_addrv4_param),
4792 fii = (struct sctp_asconf_addrv4_param *)fee;
4793 sin.sin_addr.s_addr = fii->addrp.addr;
4794 lsa = (struct sockaddr *)&sin;
4796 } else if (lptype == SCTP_IPV6_ADDRESS) {
4798 sizeof(struct sctp_asconf_addr_param)) {
4799 printf("Sizeof setprim (v6) in init/init ack not %d but %d - ignored\n",
4800 (int)sizeof(struct sctp_asconf_addr_param),
4803 memcpy(sin6.sin6_addr.s6_addr,
4805 sizeof(fee->addrp.addr));
4806 lsa = (struct sockaddr *)&sin6;
4810 sctp_set_primary_addr(stcb, sa, NULL);
4812 } else if (ptype == SCTP_PRSCTP_SUPPORTED) {
4813 /* Peer supports pr-sctp */
4814 stcb->asoc.peer_supports_prsctp = 1;
4815 } else if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
4816 /* A supported extension chunk */
4817 struct sctp_supported_chunk_types_param *pr_supported;
4818 uint8_t local_store[SCTP_PARAM_BUFFER_SIZE];
4821 phdr = sctp_get_next_param(m, offset,
4822 (struct sctp_paramhdr *)&local_store, plen);
4826 stcb->asoc.peer_supports_asconf = 0;
4827 stcb->asoc.peer_supports_prsctp = 0;
4828 stcb->asoc.peer_supports_pktdrop = 0;
4829 stcb->asoc.peer_supports_strreset = 0;
4830 stcb->asoc.peer_supports_auth = 0;
4831 pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
4832 num_ent = plen - sizeof(struct sctp_paramhdr);
4833 for (i = 0; i < num_ent; i++) {
4834 switch (pr_supported->chunk_types[i]) {
4836 case SCTP_ASCONF_ACK:
4837 stcb->asoc.peer_supports_asconf = 1;
4839 case SCTP_FORWARD_CUM_TSN:
4840 stcb->asoc.peer_supports_prsctp = 1;
4842 case SCTP_PACKET_DROPPED:
4843 stcb->asoc.peer_supports_pktdrop = 1;
4845 case SCTP_STREAM_RESET:
4846 stcb->asoc.peer_supports_strreset = 1;
4848 case SCTP_AUTHENTICATION:
4849 stcb->asoc.peer_supports_auth = 1;
4852 /* one I have not learned yet */
4857 } else if (ptype == SCTP_ECN_NONCE_SUPPORTED) {
4858 /* Peer supports ECN-nonce */
4859 stcb->asoc.peer_supports_ecn_nonce = 1;
4860 stcb->asoc.ecn_nonce_allowed = 1;
4861 } else if (ptype == SCTP_RANDOM) {
4862 if (plen > sizeof(random_store))
4865 /* already processed a RANDOM */
4868 phdr = sctp_get_next_param(m, offset,
4869 (struct sctp_paramhdr *)random_store,
4873 random = (struct sctp_auth_random *)phdr;
4874 random_len = plen - sizeof(*random);
4875 /* enforce the random length */
4876 if (random_len != SCTP_AUTH_RANDOM_SIZE_REQUIRED) {
4878 if (sctp_debug_on & SCTP_DEBUG_AUTH1)
4879 printf("SCTP: invalid RANDOM len\n");
4884 } else if (ptype == SCTP_HMAC_LIST) {
4888 if (plen > sizeof(hmacs_store))
4891 /* already processed a HMAC list */
4894 phdr = sctp_get_next_param(m, offset,
4895 (struct sctp_paramhdr *)hmacs_store,
4899 hmacs = (struct sctp_auth_hmac_algo *)phdr;
4900 hmacs_len = plen - sizeof(*hmacs);
4901 num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]);
4902 /* validate the hmac list */
4903 if (sctp_verify_hmac_param(hmacs, num_hmacs)) {
4906 if (stcb->asoc.peer_hmacs != NULL)
4907 sctp_free_hmaclist(stcb->asoc.peer_hmacs);
4908 stcb->asoc.peer_hmacs = sctp_alloc_hmaclist(num_hmacs);
4909 if (stcb->asoc.peer_hmacs != NULL) {
4910 for (i = 0; i < num_hmacs; i++) {
4911 sctp_auth_add_hmacid(stcb->asoc.peer_hmacs,
4912 ntohs(hmacs->hmac_ids[i]));
4916 } else if (ptype == SCTP_CHUNK_LIST) {
4919 if (plen > sizeof(chunks_store))
4922 /* already processed a Chunks list */
4925 phdr = sctp_get_next_param(m, offset,
4926 (struct sctp_paramhdr *)chunks_store,
4930 chunks = (struct sctp_auth_chunk_list *)phdr;
4931 num_chunks = plen - sizeof(*chunks);
4932 if (stcb->asoc.peer_auth_chunks != NULL)
4933 sctp_clear_chunklist(stcb->asoc.peer_auth_chunks);
4935 stcb->asoc.peer_auth_chunks = sctp_alloc_chunklist();
4936 for (i = 0; i < num_chunks; i++) {
4937 sctp_auth_add_chunk(chunks->chunk_types[i],
4938 stcb->asoc.peer_auth_chunks);
4941 } else if ((ptype == SCTP_HEARTBEAT_INFO) ||
4942 (ptype == SCTP_STATE_COOKIE) ||
4943 (ptype == SCTP_UNRECOG_PARAM) ||
4944 (ptype == SCTP_COOKIE_PRESERVE) ||
4945 (ptype == SCTP_SUPPORTED_ADDRTYPE) ||
4946 (ptype == SCTP_ADD_IP_ADDRESS) ||
4947 (ptype == SCTP_DEL_IP_ADDRESS) ||
4948 (ptype == SCTP_ERROR_CAUSE_IND) ||
4949 (ptype == SCTP_SUCCESS_REPORT)) {
4952 if ((ptype & 0x8000) == 0x0000) {
4954 * must stop processing the rest of the
4955 * param's. Any report bits were handled
4957 * sctp_arethere_unrecognized_parameters()
4958 * when the INIT or INIT-ACK was first seen.
4964 offset += SCTP_SIZE32(plen);
4965 if (offset >= limit) {
4968 phdr = sctp_get_next_param(m, offset, &parm_buf,
4971 /* Now check to see if we need to purge any addresses */
4972 for (net = TAILQ_FIRST(&stcb->asoc.nets); net != NULL; net = net_tmp) {
4973 net_tmp = TAILQ_NEXT(net, sctp_next);
4974 if ((net->dest_state & SCTP_ADDR_NOT_IN_ASSOC) ==
4975 SCTP_ADDR_NOT_IN_ASSOC) {
4976 /* This address has been removed from the asoc */
4977 /* remove and free it */
4978 stcb->asoc.numnets--;
4979 TAILQ_REMOVE(&stcb->asoc.nets, net, sctp_next);
4980 sctp_free_remote_addr(net);
4981 if (net == stcb->asoc.primary_destination) {
4982 stcb->asoc.primary_destination = NULL;
4983 sctp_select_primary_destination(stcb);
4987 /* validate authentication required parameters */
4988 if (got_random && got_hmacs) {
4989 stcb->asoc.peer_supports_auth = 1;
4991 stcb->asoc.peer_supports_auth = 0;
4993 if (!sctp_asconf_auth_nochk && stcb->asoc.peer_supports_asconf &&
4994 !stcb->asoc.peer_supports_auth) {
4997 /* concatenate the full random key */
4998 #ifdef SCTP_AUTH_DRAFT_04
4999 keylen = random_len;
5000 new_key = sctp_alloc_key(keylen);
5001 if (new_key != NULL) {
5002 /* copy in the RANDOM */
5004 bcopy(random->random_data, new_key->key, random_len);
5007 keylen = sizeof(*random) + random_len + sizeof(*chunks) + num_chunks +
5008 sizeof(*hmacs) + hmacs_len;
5009 new_key = sctp_alloc_key(keylen);
5010 if (new_key != NULL) {
5011 /* copy in the RANDOM */
5012 if (random != NULL) {
5013 keylen = sizeof(*random) + random_len;
5014 bcopy(random, new_key->key, keylen);
5016 /* append in the AUTH chunks */
5017 if (chunks != NULL) {
5018 bcopy(chunks, new_key->key + keylen,
5019 sizeof(*chunks) + num_chunks);
5020 keylen += sizeof(*chunks) + num_chunks;
5022 /* append in the HMACs */
5023 if (hmacs != NULL) {
5024 bcopy(hmacs, new_key->key + keylen,
5025 sizeof(*hmacs) + hmacs_len);
5032 if (stcb->asoc.authinfo.peer_random != NULL)
5033 sctp_free_key(stcb->asoc.authinfo.peer_random);
5034 stcb->asoc.authinfo.peer_random = new_key;
5035 #ifdef SCTP_AUTH_DRAFT_04
5036 /* don't include the chunks and hmacs for draft -04 */
5037 stcb->asoc.authinfo.peer_random->keylen = random_len;
5039 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid);
5040 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid);
5046 sctp_set_primary_addr(struct sctp_tcb *stcb, struct sockaddr *sa,
5047 struct sctp_nets *net)
5049 /* make sure the requested primary address exists in the assoc */
5050 if (net == NULL && sa)
5051 net = sctp_findnet(stcb, sa);
5054 /* didn't find the requested primary address! */
5057 /* set the primary address */
5058 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
5059 /* Must be confirmed, so queue to set */
5060 net->dest_state |= SCTP_ADDR_REQ_PRIMARY;
5063 stcb->asoc.primary_destination = net;
5064 net->dest_state &= ~SCTP_ADDR_WAS_PRIMARY;
5065 net = TAILQ_FIRST(&stcb->asoc.nets);
5066 if (net != stcb->asoc.primary_destination) {
5068 * first one on the list is NOT the primary
5069 * sctp_cmpaddr() is much more efficent if the
5070 * primary is the first on the list, make it so.
5072 TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
5073 TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
5081 sctp_is_vtag_good(struct sctp_inpcb *inp, uint32_t tag, struct timeval *now)
5084 * This function serves two purposes. It will see if a TAG can be
5085 * re-used and return 1 for yes it is ok and 0 for don't use that
5086 * tag. A secondary function it will do is purge out old tags that
5089 struct sctpasochead *head;
5090 struct sctpvtaghead *chain;
5091 struct sctp_tagblock *twait_block;
5092 struct sctp_tcb *stcb;
5095 SCTP_INP_INFO_WLOCK();
5096 chain = &sctppcbinfo.vtag_timewait[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
5097 /* First is the vtag in use ? */
5099 head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(tag,
5100 sctppcbinfo.hashasocmark)];
5104 LIST_FOREACH(stcb, head, sctp_asocs) {
5106 if (stcb->asoc.my_vtag == tag) {
5108 * We should remove this if and return 0 always if
5109 * we want vtags unique across all endpoints. For
5110 * now within a endpoint is ok.
5112 if (inp == stcb->sctp_ep) {
5113 /* bad tag, in use */
5114 SCTP_INP_INFO_WUNLOCK();
5120 /* Now lets check the restart hash */
5121 head = &sctppcbinfo.sctp_restarthash[SCTP_PCBHASH_ASOC(tag,
5122 sctppcbinfo.hashrestartmark)];
5124 goto check_time_wait;
5126 LIST_FOREACH(stcb, head, sctp_tcbrestarhash) {
5127 if (stcb->asoc.assoc_id == tag) {
5129 if (inp == stcb->sctp_ep) {
5130 /* bad tag, in use */
5131 SCTP_INP_INFO_WUNLOCK();
5137 /* Now what about timed wait ? */
5138 if (!SCTP_LIST_EMPTY(chain)) {
5140 * Block(s) are present, lets see if we have this tag in the
5143 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
5144 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
5145 if (twait_block->vtag_block[i].v_tag == 0) {
5148 } else if ((long)twait_block->vtag_block[i].tv_sec_at_expire >
5150 /* Audit expires this guy */
5151 twait_block->vtag_block[i].tv_sec_at_expire = 0;
5152 twait_block->vtag_block[i].v_tag = 0;
5153 } else if (twait_block->vtag_block[i].v_tag ==
5155 /* Bad tag, sorry :< */
5156 SCTP_INP_INFO_WUNLOCK();
5162 /* Not found, ok to use the tag */
5163 SCTP_INP_INFO_WUNLOCK();
5168 static sctp_assoc_t reneged_asoc_ids[256];
5169 static uint8_t reneged_at = 0;
5173 sctp_drain_mbufs(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
5176 * We must hunt this association for MBUF's past the cumack (i.e.
5177 * out of order data that we can renege on).
5179 struct sctp_association *asoc;
5180 struct sctp_tmit_chunk *chk, *nchk;
5181 uint32_t cumulative_tsn_p1, tsn;
5182 struct sctp_queued_to_read *ctl, *nctl;
5183 int cnt, strmat, gap;
5185 /* We look for anything larger than the cum-ack + 1 */
5187 SCTP_STAT_INCR(sctps_protocol_drain_calls);
5188 if (sctp_do_drain == 0) {
5192 if (asoc->cumulative_tsn == asoc->highest_tsn_inside_map) {
5193 /* none we can reneg on. */
5196 SCTP_STAT_INCR(sctps_protocol_drains_done);
5197 cumulative_tsn_p1 = asoc->cumulative_tsn + 1;
5199 /* First look in the re-assembly queue */
5200 chk = TAILQ_FIRST(&asoc->reasmqueue);
5202 /* Get the next one */
5203 nchk = TAILQ_NEXT(chk, sctp_next);
5204 if (compare_with_wrap(chk->rec.data.TSN_seq,
5205 cumulative_tsn_p1, MAX_TSN)) {
5206 /* Yep it is above cum-ack */
5208 tsn = chk->rec.data.TSN_seq;
5209 if (tsn >= asoc->mapping_array_base_tsn) {
5210 gap = tsn - asoc->mapping_array_base_tsn;
5212 gap = (MAX_TSN - asoc->mapping_array_base_tsn) +
5215 asoc->size_on_reasm_queue = sctp_sbspace_sub(asoc->size_on_reasm_queue, chk->send_size);
5216 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
5217 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
5218 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
5220 sctp_m_freem(chk->data);
5223 sctp_free_remote_addr(chk->whoTo);
5224 sctp_free_a_chunk(stcb, chk);
5228 /* Ok that was fun, now we will drain all the inbound streams? */
5229 for (strmat = 0; strmat < asoc->streamincnt; strmat++) {
5230 ctl = TAILQ_FIRST(&asoc->strmin[strmat].inqueue);
5232 nctl = TAILQ_NEXT(ctl, next);
5233 if (compare_with_wrap(ctl->sinfo_tsn,
5234 cumulative_tsn_p1, MAX_TSN)) {
5235 /* Yep it is above cum-ack */
5237 tsn = ctl->sinfo_tsn;
5238 if (tsn >= asoc->mapping_array_base_tsn) {
5240 asoc->mapping_array_base_tsn;
5243 asoc->mapping_array_base_tsn) +
5246 asoc->size_on_all_streams = sctp_sbspace_sub(asoc->size_on_all_streams, ctl->length);
5247 sctp_ucount_decr(asoc->cnt_on_all_streams);
5249 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array,
5251 TAILQ_REMOVE(&asoc->strmin[strmat].inqueue,
5254 sctp_m_freem(ctl->data);
5257 sctp_free_remote_addr(ctl->whoFrom);
5258 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_readq, ctl);
5259 SCTP_DECR_READQ_COUNT();
5265 * Question, should we go through the delivery queue? The only
5266 * reason things are on here is the app not reading OR a p-d-api up.
5267 * An attacker COULD send enough in to initiate the PD-API and then
5268 * send a bunch of stuff to other streams... these would wind up on
5269 * the delivery queue.. and then we would not get to them. But in
5270 * order to do this I then have to back-track and un-deliver
5271 * sequence numbers in streams.. el-yucko. I think for now we will
5272 * NOT look at the delivery queue and leave it to be something to
5273 * consider later. An alternative would be to abort the P-D-API with
5274 * a notification and then deliver the data.... Or another method
5275 * might be to keep track of how many times the situation occurs and
5276 * if we see a possible attack underway just abort the association.
5279 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
5281 printf("Freed %d chunks from reneg harvest\n", cnt);
5284 #endif /* SCTP_DEBUG */
5287 * Now do we need to find a new
5288 * asoc->highest_tsn_inside_map?
5290 if (asoc->highest_tsn_inside_map >= asoc->mapping_array_base_tsn) {
5291 gap = asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn;
5293 gap = (MAX_TSN - asoc->mapping_array_base_tsn) +
5294 asoc->highest_tsn_inside_map + 1;
5296 if (gap >= (asoc->mapping_array_size << 3)) {
5298 * Something bad happened or cum-ack and high were
5299 * behind the base, but if so earlier checks should
5300 * have found NO data... wierd... we will start at
5301 * end of mapping array.
5303 printf("Gap was larger than array?? %d set to max:%d maparraymax:%x\n",
5305 (int)(asoc->mapping_array_size << 3),
5306 (int)asoc->highest_tsn_inside_map);
5307 gap = asoc->mapping_array_size << 3;
5310 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
5311 /* found the new highest */
5312 asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn + gap;
5318 /* Nothing left in map */
5319 memset(asoc->mapping_array, 0, asoc->mapping_array_size);
5320 asoc->mapping_array_base_tsn = asoc->cumulative_tsn + 1;
5321 asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
5323 asoc->last_revoke_count = cnt;
5324 SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
5325 sctp_send_sack(stcb);
5326 sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_DRAIN);
5327 reneged_asoc_ids[reneged_at] = sctp_get_associd(stcb);
5331 * Another issue, in un-setting the TSN's in the mapping array we
5332 * DID NOT adjust the higest_tsn marker. This will cause one of two
5333 * things to occur. It may cause us to do extra work in checking for
5334 * our mapping array movement. More importantly it may cause us to
5335 * SACK every datagram. This may not be a bad thing though since we
5336 * will recover once we get our cum-ack above and all this stuff we
5345 * We must walk the PCB lists for ALL associations here. The system
5346 * is LOW on MBUF's and needs help. This is where reneging will
5347 * occur. We really hope this does NOT happen!
5349 struct sctp_inpcb *inp;
5350 struct sctp_tcb *stcb;
5352 SCTP_INP_INFO_RLOCK();
5353 LIST_FOREACH(inp, &sctppcbinfo.listhead, sctp_list) {
5354 /* For each endpoint */
5355 SCTP_INP_RLOCK(inp);
5356 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5357 /* For each association */
5358 SCTP_TCB_LOCK(stcb);
5359 sctp_drain_mbufs(inp, stcb);
5360 SCTP_TCB_UNLOCK(stcb);
5362 SCTP_INP_RUNLOCK(inp);
5364 SCTP_INP_INFO_RUNLOCK();
5368 * start a new iterator
5369 * iterates through all endpoints and associations based on the pcb_state
5370 * flags and asoc_state. "af" (mandatory) is executed for all matching
5371 * assocs and "ef" (optional) is executed when the iterator completes.
5372 * "inpf" (optional) is executed for each new endpoint as it is being
5376 sctp_initiate_iterator(inp_func inpf,
5380 uint32_t pcb_features,
5381 uint32_t asoc_state,
5385 struct sctp_inpcb *s_inp,
5386 uint8_t chunk_output_off)
5388 struct sctp_iterator *it = NULL;
5393 SCTP_MALLOC(it, struct sctp_iterator *, sizeof(struct sctp_iterator),
5398 memset(it, 0, sizeof(*it));
5399 it->function_assoc = af;
5400 it->function_inp = inpf;
5402 it->done_current_ep = 0;
5404 it->done_current_ep = 1;
5405 it->function_atend = ef;
5408 it->pcb_flags = pcb_state;
5409 it->pcb_features = pcb_features;
5410 it->asoc_state = asoc_state;
5411 it->function_inp_end = inpe;
5412 it->no_chunk_output = chunk_output_off;
5415 it->iterator_flags = SCTP_ITERATOR_DO_SINGLE_INP;
5417 SCTP_INP_INFO_RLOCK();
5418 it->inp = LIST_FIRST(&sctppcbinfo.listhead);
5420 SCTP_INP_INFO_RUNLOCK();
5421 it->iterator_flags = SCTP_ITERATOR_DO_ALL_INP;
5424 SCTP_IPI_ITERATOR_WQ_LOCK();
5426 SCTP_INP_INCR_REF(it->inp);
5427 TAILQ_INSERT_TAIL(&sctppcbinfo.iteratorhead, it, sctp_nxt_itr);
5428 #if defined(SCTP_USE_THREAD_BASED_ITERATOR)
5429 if (sctppcbinfo.iterator_running == 0) {
5430 sctp_wakeup_iterator();
5432 SCTP_IPI_ITERATOR_WQ_UNLOCK();
5435 SCTP_INP_DECR_REF(it->inp);
5436 SCTP_IPI_ITERATOR_WQ_UNLOCK();
5437 /* Init the timer */
5438 SCTP_OS_TIMER_INIT(&it->tmr.timer);
5439 /* add to the list of all iterators */
5440 sctp_timer_start(SCTP_TIMER_TYPE_ITERATOR, (struct sctp_inpcb *)it,