2 * Copyright (c) 2001-2006, 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$");
35 #include "opt_ipsec.h"
36 #include "opt_compat.h"
37 #include "opt_inet6.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
45 #include <sys/domain.h>
46 #include <sys/protosw.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
51 #include <sys/kernel.h>
52 #include <sys/sysctl.h>
54 #include <sys/limits.h>
55 #include <machine/cpu.h>
58 #include <net/if_types.h>
59 #include <net/route.h>
60 #include <netinet/in.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/ip.h>
63 #include <netinet/in_pcb.h>
64 #include <netinet/in_var.h>
65 #include <netinet/ip_var.h>
68 #include <netinet/ip6.h>
69 #include <netinet6/ip6_var.h>
70 #include <netinet6/scope6_var.h>
71 #include <netinet6/in6_pcb.h>
75 #include <netinet6/ipsec.h>
76 #include <netkey/key.h>
79 #include <netinet/sctp_os.h>
80 #include <netinet/sctp_var.h>
81 #include <netinet/sctp_pcb.h>
82 #include <netinet/sctputil.h>
83 #include <netinet/sctp.h>
84 #include <netinet/sctp_header.h>
85 #include <netinet/sctp_asconf.h>
86 #include <netinet/sctp_output.h>
87 #include <netinet/sctp_timer.h>
91 uint32_t sctp_debug_on = 0;
93 #endif /* SCTP_DEBUG */
96 extern int sctp_pcbtblsize;
97 extern int sctp_hashtblsize;
98 extern int sctp_chunkscale;
100 struct sctp_epinfo sctppcbinfo;
102 /* FIX: we don't handle multiple link local scopes */
103 /* "scopeless" replacement IN6_ARE_ADDR_EQUAL */
105 SCTP6_ARE_ADDR_EQUAL(struct in6_addr *a, struct in6_addr *b)
107 struct in6_addr tmp_a, tmp_b;
109 /* use a copy of a and b */
112 in6_clearscope(&tmp_a);
113 in6_clearscope(&tmp_b);
114 return (IN6_ARE_ADDR_EQUAL(&tmp_a, &tmp_b));
119 sctp_fill_pcbinfo(struct sctp_pcbinfo *spcb)
122 * We really don't need to lock this, but I will just because it
125 SCTP_INP_INFO_RLOCK();
126 spcb->ep_count = sctppcbinfo.ipi_count_ep;
127 spcb->asoc_count = sctppcbinfo.ipi_count_asoc;
128 spcb->laddr_count = sctppcbinfo.ipi_count_laddr;
129 spcb->raddr_count = sctppcbinfo.ipi_count_raddr;
130 spcb->chk_count = sctppcbinfo.ipi_count_chunk;
131 spcb->readq_count = sctppcbinfo.ipi_count_readq;
132 spcb->stream_oque = sctppcbinfo.ipi_count_strmoq;
133 spcb->free_chunks = sctppcbinfo.ipi_free_chunks;
135 SCTP_INP_INFO_RUNLOCK();
140 * Notes on locks for FreeBSD 5 and up. All association lookups that have a
141 * definte ep, the INP structure is assumed to be locked for reading. If we
142 * need to go find the INP (ususally when a **inp is passed) then we must
143 * lock the INFO structure first and if needed lock the INP too. Note that if
150 * Given a endpoint, look and find in its association list any association
151 * with the "to" address given. This can be a "from" address, too, for
152 * inbound packets. For outbound packets it is a true "to" address.
155 static struct sctp_tcb *
156 sctp_tcb_special_locate(struct sctp_inpcb **inp_p, struct sockaddr *from,
157 struct sockaddr *to, struct sctp_nets **netp)
159 /**** ASSUMSES THE CALLER holds the INP_INFO_RLOCK */
162 * Note for this module care must be taken when observing what to is
163 * for. In most of the rest of the code the TO field represents my
164 * peer and the FROM field represents my address. For this module it
165 * is reversed of that.
168 * If we support the TCP model, then we must now dig through to see
169 * if we can find our endpoint in the list of tcp ep's.
171 uint16_t lport, rport;
172 struct sctppcbhead *ephead;
173 struct sctp_inpcb *inp;
174 struct sctp_laddr *laddr;
175 struct sctp_tcb *stcb;
176 struct sctp_nets *net;
178 if ((to == NULL) || (from == NULL)) {
181 if (to->sa_family == AF_INET && from->sa_family == AF_INET) {
182 lport = ((struct sockaddr_in *)to)->sin_port;
183 rport = ((struct sockaddr_in *)from)->sin_port;
184 } else if (to->sa_family == AF_INET6 && from->sa_family == AF_INET6) {
185 lport = ((struct sockaddr_in6 *)to)->sin6_port;
186 rport = ((struct sockaddr_in6 *)from)->sin6_port;
190 ephead = &sctppcbinfo.sctp_tcpephash[SCTP_PCBHASH_ALLADDR(
191 (lport + rport), sctppcbinfo.hashtcpmark)];
193 * Ok now for each of the guys in this bucket we must look and see:
194 * - Does the remote port match. - Does there single association's
195 * addresses match this address (to). If so we update p_ep to point
196 * to this ep and return the tcb from it.
198 LIST_FOREACH(inp, ephead, sctp_hash) {
199 if (lport != inp->sctp_lport) {
203 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
204 SCTP_INP_RUNLOCK(inp);
207 /* check to see if the ep has one of the addresses */
208 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
209 /* We are NOT bound all, so look further */
212 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
214 if (laddr->ifa == NULL) {
216 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
217 printf("An ounce of prevention is worth a pound of cure\n");
222 if (laddr->ifa->ifa_addr == NULL) {
224 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
225 printf("ifa with a NULL address\n");
230 if (laddr->ifa->ifa_addr->sa_family ==
232 /* see if it matches */
233 struct sockaddr_in *intf_addr, *sin;
235 intf_addr = (struct sockaddr_in *)
236 laddr->ifa->ifa_addr;
237 sin = (struct sockaddr_in *)to;
238 if (from->sa_family == AF_INET) {
239 if (sin->sin_addr.s_addr ==
240 intf_addr->sin_addr.s_addr) {
245 struct sockaddr_in6 *intf_addr6;
246 struct sockaddr_in6 *sin6;
248 sin6 = (struct sockaddr_in6 *)
250 intf_addr6 = (struct sockaddr_in6 *)
251 laddr->ifa->ifa_addr;
253 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
254 &intf_addr6->sin6_addr)) {
262 /* This endpoint does not have this address */
263 SCTP_INP_RUNLOCK(inp);
268 * Ok if we hit here the ep has the address, does it hold
272 stcb = LIST_FIRST(&inp->sctp_asoc_list);
274 SCTP_INP_RUNLOCK(inp);
278 if (stcb->rport != rport) {
279 /* remote port does not match. */
280 SCTP_TCB_UNLOCK(stcb);
281 SCTP_INP_RUNLOCK(inp);
284 /* Does this TCB have a matching address? */
285 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
287 if (net->ro._l_addr.sa.sa_family != from->sa_family) {
288 /* not the same family, can't be a match */
291 if (from->sa_family == AF_INET) {
292 struct sockaddr_in *sin, *rsin;
294 sin = (struct sockaddr_in *)&net->ro._l_addr;
295 rsin = (struct sockaddr_in *)from;
296 if (sin->sin_addr.s_addr ==
297 rsin->sin_addr.s_addr) {
302 /* Update the endpoint pointer */
304 SCTP_INP_RUNLOCK(inp);
308 struct sockaddr_in6 *sin6, *rsin6;
310 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
311 rsin6 = (struct sockaddr_in6 *)from;
312 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
313 &rsin6->sin6_addr)) {
318 /* Update the endpoint pointer */
320 SCTP_INP_RUNLOCK(inp);
325 SCTP_TCB_UNLOCK(stcb);
326 SCTP_INP_RUNLOCK(inp);
334 * 1) If I return a NULL you must decrement any INP ref cnt. 2) If I find an
335 * stcb, both will be locked (locked_tcb and stcb) but decrement will be done
336 * (if locked == NULL). 3) Decrement happens on return ONLY if locked ==
341 sctp_findassociation_ep_addr(struct sctp_inpcb **inp_p, struct sockaddr *remote,
342 struct sctp_nets **netp, struct sockaddr *local, struct sctp_tcb *locked_tcb)
344 struct sctpasochead *head;
345 struct sctp_inpcb *inp;
346 struct sctp_tcb *stcb;
347 struct sctp_nets *net;
351 if (remote->sa_family == AF_INET) {
352 rport = (((struct sockaddr_in *)remote)->sin_port);
353 } else if (remote->sa_family == AF_INET6) {
354 rport = (((struct sockaddr_in6 *)remote)->sin6_port);
360 * UN-lock so we can do proper locking here this occurs when
361 * called from load_addresses_from_init.
363 SCTP_TCB_UNLOCK(locked_tcb);
365 SCTP_INP_INFO_RLOCK();
366 if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
368 * Now either this guy is our listener or it's the
369 * connector. If it is the one that issued the connect, then
370 * it's only chance is to be the first TCB in the list. If
371 * it is the acceptor, then do the special_lookup to hash
372 * and find the real inp.
374 if ((inp->sctp_socket) && (inp->sctp_socket->so_qlimit)) {
375 /* to is peer addr, from is my addr */
376 stcb = sctp_tcb_special_locate(inp_p, remote, local,
378 if ((stcb != NULL) && (locked_tcb == NULL)) {
379 /* we have a locked tcb, lower refcount */
381 SCTP_INP_DECR_REF(inp);
382 SCTP_INP_WUNLOCK(inp);
384 if ((locked_tcb != NULL) && (locked_tcb != stcb)) {
385 SCTP_INP_RLOCK(locked_tcb->sctp_ep);
386 SCTP_TCB_LOCK(locked_tcb);
387 SCTP_INP_RUNLOCK(locked_tcb->sctp_ep);
389 SCTP_INP_INFO_RUNLOCK();
393 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
396 stcb = LIST_FIRST(&inp->sctp_asoc_list);
401 if (stcb->rport != rport) {
402 /* remote port does not match. */
403 SCTP_TCB_UNLOCK(stcb);
406 /* now look at the list of remote addresses */
407 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
409 if (net == (TAILQ_NEXT(net, sctp_next))) {
410 panic("Corrupt net list");
413 if (net->ro._l_addr.sa.sa_family !=
415 /* not the same family */
418 if (remote->sa_family == AF_INET) {
419 struct sockaddr_in *sin, *rsin;
421 sin = (struct sockaddr_in *)
423 rsin = (struct sockaddr_in *)remote;
424 if (sin->sin_addr.s_addr ==
425 rsin->sin_addr.s_addr) {
430 if (locked_tcb == NULL) {
431 SCTP_INP_DECR_REF(inp);
432 } else if (locked_tcb != stcb) {
433 SCTP_TCB_LOCK(locked_tcb);
435 SCTP_INP_WUNLOCK(inp);
436 SCTP_INP_INFO_RUNLOCK();
439 } else if (remote->sa_family == AF_INET6) {
440 struct sockaddr_in6 *sin6, *rsin6;
442 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
443 rsin6 = (struct sockaddr_in6 *)remote;
444 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
445 &rsin6->sin6_addr)) {
450 if (locked_tcb == NULL) {
451 SCTP_INP_DECR_REF(inp);
452 } else if (locked_tcb != stcb) {
453 SCTP_TCB_LOCK(locked_tcb);
455 SCTP_INP_WUNLOCK(inp);
456 SCTP_INP_INFO_RUNLOCK();
461 SCTP_TCB_UNLOCK(stcb);
465 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
468 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(rport,
469 inp->sctp_hashmark)];
473 LIST_FOREACH(stcb, head, sctp_tcbhash) {
474 if (stcb->rport != rport) {
475 /* remote port does not match */
478 /* now look at the list of remote addresses */
480 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
482 if (net == (TAILQ_NEXT(net, sctp_next))) {
483 panic("Corrupt net list");
486 if (net->ro._l_addr.sa.sa_family !=
488 /* not the same family */
491 if (remote->sa_family == AF_INET) {
492 struct sockaddr_in *sin, *rsin;
494 sin = (struct sockaddr_in *)
496 rsin = (struct sockaddr_in *)remote;
497 if (sin->sin_addr.s_addr ==
498 rsin->sin_addr.s_addr) {
503 if (locked_tcb == NULL) {
504 SCTP_INP_DECR_REF(inp);
505 } else if (locked_tcb != stcb) {
506 SCTP_TCB_LOCK(locked_tcb);
508 SCTP_INP_WUNLOCK(inp);
509 SCTP_INP_INFO_RUNLOCK();
512 } else if (remote->sa_family == AF_INET6) {
513 struct sockaddr_in6 *sin6, *rsin6;
515 sin6 = (struct sockaddr_in6 *)
517 rsin6 = (struct sockaddr_in6 *)remote;
518 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
519 &rsin6->sin6_addr)) {
524 if (locked_tcb == NULL) {
525 SCTP_INP_DECR_REF(inp);
526 } else if (locked_tcb != stcb) {
527 SCTP_TCB_LOCK(locked_tcb);
529 SCTP_INP_WUNLOCK(inp);
530 SCTP_INP_INFO_RUNLOCK();
535 SCTP_TCB_UNLOCK(stcb);
539 /* clean up for returning null */
541 SCTP_TCB_LOCK(locked_tcb);
543 SCTP_INP_WUNLOCK(inp);
544 SCTP_INP_INFO_RUNLOCK();
550 * Find an association for a specific endpoint using the association id given
551 * out in the COMM_UP notification
555 sctp_findassociation_ep_asocid(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock)
558 * Use my the assoc_id to find a endpoint
560 struct sctpasochead *head;
561 struct sctp_tcb *stcb;
564 if (asoc_id == 0 || inp == NULL) {
567 SCTP_INP_INFO_RLOCK();
568 id = (uint32_t) asoc_id;
569 head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(id,
570 sctppcbinfo.hashasocmark)];
572 /* invalid id TSNH */
573 SCTP_INP_INFO_RUNLOCK();
576 LIST_FOREACH(stcb, head, sctp_asocs) {
577 SCTP_INP_RLOCK(stcb->sctp_ep);
578 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
579 SCTP_INP_RUNLOCK(stcb->sctp_ep);
580 SCTP_INP_INFO_RUNLOCK();
583 if (stcb->asoc.assoc_id == id) {
585 if (inp != stcb->sctp_ep) {
587 * some other guy has the same id active (id
590 SCTP_INP_RUNLOCK(stcb->sctp_ep);
596 SCTP_INP_RUNLOCK(stcb->sctp_ep);
597 SCTP_INP_INFO_RUNLOCK();
600 SCTP_INP_RUNLOCK(stcb->sctp_ep);
602 /* Ok if we missed here, lets try the restart hash */
603 head = &sctppcbinfo.sctp_restarthash[SCTP_PCBHASH_ASOC(id, sctppcbinfo.hashrestartmark)];
605 /* invalid id TSNH */
606 SCTP_INP_INFO_RUNLOCK();
609 LIST_FOREACH(stcb, head, sctp_tcbrestarhash) {
610 SCTP_INP_RLOCK(stcb->sctp_ep);
611 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
612 SCTP_INP_RUNLOCK(stcb->sctp_ep);
613 SCTP_INP_INFO_RUNLOCK();
617 SCTP_INP_RUNLOCK(stcb->sctp_ep);
618 if (stcb->asoc.assoc_id == id) {
620 if (inp != stcb->sctp_ep) {
622 * some other guy has the same id active (id
625 SCTP_TCB_UNLOCK(stcb);
628 SCTP_INP_INFO_RUNLOCK();
631 SCTP_TCB_UNLOCK(stcb);
633 SCTP_INP_INFO_RUNLOCK();
638 static struct sctp_inpcb *
639 sctp_endpoint_probe(struct sockaddr *nam, struct sctppcbhead *head,
642 struct sctp_inpcb *inp;
643 struct sockaddr_in *sin;
644 struct sockaddr_in6 *sin6;
645 struct sctp_laddr *laddr;
648 * Endpoing probe expects that the INP_INFO is locked.
650 if (nam->sa_family == AF_INET) {
651 sin = (struct sockaddr_in *)nam;
653 } else if (nam->sa_family == AF_INET6) {
654 sin6 = (struct sockaddr_in6 *)nam;
657 /* unsupported family */
662 LIST_FOREACH(inp, head, sctp_hash) {
664 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
665 SCTP_INP_RUNLOCK(inp);
668 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) &&
669 (inp->sctp_lport == lport)) {
671 if ((nam->sa_family == AF_INET) &&
672 (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
673 SCTP_IPV6_V6ONLY(inp)) {
674 /* IPv4 on a IPv6 socket with ONLY IPv6 set */
675 SCTP_INP_RUNLOCK(inp);
678 /* A V6 address and the endpoint is NOT bound V6 */
679 if (nam->sa_family == AF_INET6 &&
680 (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
681 SCTP_INP_RUNLOCK(inp);
684 SCTP_INP_RUNLOCK(inp);
687 SCTP_INP_RUNLOCK(inp);
690 if ((nam->sa_family == AF_INET) &&
691 (sin->sin_addr.s_addr == INADDR_ANY)) {
692 /* Can't hunt for one that has no address specified */
694 } else if ((nam->sa_family == AF_INET6) &&
695 (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))) {
696 /* Can't hunt for one that has no address specified */
700 * ok, not bound to all so see if we can find a EP bound to this
703 LIST_FOREACH(inp, head, sctp_hash) {
705 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
706 SCTP_INP_RUNLOCK(inp);
709 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)) {
710 SCTP_INP_RUNLOCK(inp);
714 * Ok this could be a likely candidate, look at all of its
717 if (inp->sctp_lport != lport) {
718 SCTP_INP_RUNLOCK(inp);
721 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
722 if (laddr->ifa == NULL) {
724 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
725 printf("An ounce of prevention is worth a pound of cure\n");
731 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
732 printf("Ok laddr->ifa:%p is possible, ",
736 if (laddr->ifa->ifa_addr == NULL) {
738 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
739 printf("Huh IFA as an ifa_addr=NULL, ");
744 if (laddr->ifa->ifa_addr->sa_family == nam->sa_family) {
745 /* possible, see if it matches */
746 struct sockaddr_in *intf_addr;
748 intf_addr = (struct sockaddr_in *)
749 laddr->ifa->ifa_addr;
750 if (nam->sa_family == AF_INET) {
751 if (sin->sin_addr.s_addr ==
752 intf_addr->sin_addr.s_addr) {
753 SCTP_INP_RUNLOCK(inp);
756 } else if (nam->sa_family == AF_INET6) {
757 struct sockaddr_in6 *intf_addr6;
759 intf_addr6 = (struct sockaddr_in6 *)
760 laddr->ifa->ifa_addr;
761 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
762 &intf_addr6->sin6_addr)) {
763 SCTP_INP_RUNLOCK(inp);
769 SCTP_INP_RUNLOCK(inp);
776 sctp_pcb_findep(struct sockaddr *nam, int find_tcp_pool, int have_lock)
779 * First we check the hash table to see if someone has this port
780 * bound with just the port.
782 struct sctp_inpcb *inp;
783 struct sctppcbhead *head;
784 struct sockaddr_in *sin;
785 struct sockaddr_in6 *sin6;
788 if (nam->sa_family == AF_INET) {
789 sin = (struct sockaddr_in *)nam;
790 lport = ((struct sockaddr_in *)nam)->sin_port;
791 } else if (nam->sa_family == AF_INET6) {
792 sin6 = (struct sockaddr_in6 *)nam;
793 lport = ((struct sockaddr_in6 *)nam)->sin6_port;
795 /* unsupported family */
799 * I could cheat here and just cast to one of the types but we will
800 * do it right. It also provides the check against an Unsupported
803 /* Find the head of the ALLADDR chain */
804 if (have_lock == 0) {
805 SCTP_INP_INFO_RLOCK();
808 head = &sctppcbinfo.sctp_ephash[SCTP_PCBHASH_ALLADDR(lport,
809 sctppcbinfo.hashmark)];
810 inp = sctp_endpoint_probe(nam, head, lport);
813 * If the TCP model exists it could be that the main listening
814 * endpoint is gone but there exists a connected socket for this guy
815 * yet. If so we can return the first one that we find. This may NOT
816 * be the correct one but the sctp_findassociation_ep_addr has
817 * further code to look at all TCP models.
819 if (inp == NULL && find_tcp_pool) {
822 for (i = 0; i < sctppcbinfo.hashtblsize; i++) {
824 * This is real gross, but we do NOT have a remote
825 * port at this point depending on who is calling.
826 * We must therefore look for ANY one that matches
829 head = &sctppcbinfo.sctp_tcpephash[i];
830 if (LIST_FIRST(head)) {
831 inp = sctp_endpoint_probe(nam, head, lport);
840 SCTP_INP_INCR_REF(inp);
842 if (have_lock == 0) {
843 SCTP_INP_INFO_RUNLOCK();
849 * Find an association for an endpoint with the pointer to whom you want to
850 * send to and the endpoint pointer. The address can be IPv4 or IPv6. We may
851 * need to change the *to to some other struct like a mbuf...
854 sctp_findassociation_addr_sa(struct sockaddr *to, struct sockaddr *from,
855 struct sctp_inpcb **inp_p, struct sctp_nets **netp, int find_tcp_pool)
857 struct sctp_inpcb *inp;
858 struct sctp_tcb *retval;
860 SCTP_INP_INFO_RLOCK();
863 retval = sctp_tcb_special_locate(inp_p, from, to, netp);
865 retval = sctp_tcb_special_locate(&inp, from, to, netp);
867 if (retval != NULL) {
868 SCTP_INP_INFO_RUNLOCK();
872 inp = sctp_pcb_findep(to, 0, 1);
876 SCTP_INP_INFO_RUNLOCK();
882 * ok, we have an endpoint, now lets find the assoc for it (if any)
883 * we now place the source address or from in the to of the find
884 * endpoint call. Since in reality this chain is used from the
885 * inbound packet side.
888 retval = sctp_findassociation_ep_addr(inp_p, from, netp, to, NULL);
890 retval = sctp_findassociation_ep_addr(&inp, from, netp, to, NULL);
897 * This routine will grub through the mbuf that is a INIT or INIT-ACK and
898 * find all addresses that the sender has specified in any address list. Each
899 * address will be used to lookup the TCB and see if one exits.
901 static struct sctp_tcb *
902 sctp_findassociation_special_addr(struct mbuf *m, int iphlen, int offset,
903 struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp,
904 struct sockaddr *dest)
906 struct sockaddr_in sin4;
907 struct sockaddr_in6 sin6;
908 struct sctp_paramhdr *phdr, parm_buf;
909 struct sctp_tcb *retval;
910 uint32_t ptype, plen;
912 memset(&sin4, 0, sizeof(sin4));
913 memset(&sin6, 0, sizeof(sin6));
914 sin4.sin_len = sizeof(sin4);
915 sin4.sin_family = AF_INET;
916 sin4.sin_port = sh->src_port;
917 sin6.sin6_len = sizeof(sin6);
918 sin6.sin6_family = AF_INET6;
919 sin6.sin6_port = sh->src_port;
922 offset += sizeof(struct sctp_init_chunk);
924 phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
925 while (phdr != NULL) {
926 /* now we must see if we want the parameter */
927 ptype = ntohs(phdr->param_type);
928 plen = ntohs(phdr->param_length);
932 if (ptype == SCTP_IPV4_ADDRESS &&
933 plen == sizeof(struct sctp_ipv4addr_param)) {
934 /* Get the rest of the address */
935 struct sctp_ipv4addr_param ip4_parm, *p4;
937 phdr = sctp_get_next_param(m, offset,
938 (struct sctp_paramhdr *)&ip4_parm, plen);
942 p4 = (struct sctp_ipv4addr_param *)phdr;
943 memcpy(&sin4.sin_addr, &p4->addr, sizeof(p4->addr));
945 retval = sctp_findassociation_ep_addr(inp_p,
946 (struct sockaddr *)&sin4, netp, dest, NULL);
947 if (retval != NULL) {
950 } else if (ptype == SCTP_IPV6_ADDRESS &&
951 plen == sizeof(struct sctp_ipv6addr_param)) {
952 /* Get the rest of the address */
953 struct sctp_ipv6addr_param ip6_parm, *p6;
955 phdr = sctp_get_next_param(m, offset,
956 (struct sctp_paramhdr *)&ip6_parm, plen);
960 p6 = (struct sctp_ipv6addr_param *)phdr;
961 memcpy(&sin6.sin6_addr, &p6->addr, sizeof(p6->addr));
963 retval = sctp_findassociation_ep_addr(inp_p,
964 (struct sockaddr *)&sin6, netp, dest, NULL);
965 if (retval != NULL) {
969 offset += SCTP_SIZE32(plen);
970 phdr = sctp_get_next_param(m, offset, &parm_buf,
977 static struct sctp_tcb *
978 sctp_findassoc_by_vtag(struct sockaddr *from, uint32_t vtag,
979 struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint16_t rport,
980 uint16_t lport, int skip_src_check)
983 * Use my vtag to hash. If we find it we then verify the source addr
984 * is in the assoc. If all goes well we save a bit on rec of a
987 struct sctpasochead *head;
988 struct sctp_nets *net;
989 struct sctp_tcb *stcb;
993 SCTP_INP_INFO_RLOCK();
994 head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(vtag,
995 sctppcbinfo.hashasocmark)];
998 SCTP_INP_INFO_RUNLOCK();
1001 LIST_FOREACH(stcb, head, sctp_asocs) {
1002 SCTP_INP_RLOCK(stcb->sctp_ep);
1003 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1004 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1005 SCTP_INP_INFO_RUNLOCK();
1008 SCTP_TCB_LOCK(stcb);
1009 SCTP_INP_RUNLOCK(stcb->sctp_ep);
1010 if (stcb->asoc.my_vtag == vtag) {
1012 if (stcb->rport != rport) {
1014 * we could remove this if vtags are unique
1015 * across the system.
1017 SCTP_TCB_UNLOCK(stcb);
1020 if (stcb->sctp_ep->sctp_lport != lport) {
1022 * we could remove this if vtags are unique
1023 * across the system.
1025 SCTP_TCB_UNLOCK(stcb);
1028 if (skip_src_check) {
1029 *netp = NULL; /* unknown */
1030 *inp_p = stcb->sctp_ep;
1031 SCTP_INP_INFO_RUNLOCK();
1034 net = sctp_findnet(stcb, from);
1038 SCTP_STAT_INCR(sctps_vtagexpress);
1039 *inp_p = stcb->sctp_ep;
1040 SCTP_INP_INFO_RUNLOCK();
1044 * not him, this should only happen in rare
1045 * cases so I peg it.
1047 SCTP_STAT_INCR(sctps_vtagbogus);
1050 SCTP_TCB_UNLOCK(stcb);
1052 SCTP_INP_INFO_RUNLOCK();
1057 * Find an association with the pointer to the inbound IP packet. This can be
1058 * a IPv4 or IPv6 packet.
1061 sctp_findassociation_addr(struct mbuf *m, int iphlen, int offset,
1062 struct sctphdr *sh, struct sctp_chunkhdr *ch,
1063 struct sctp_inpcb **inp_p, struct sctp_nets **netp)
1067 struct sctp_tcb *retval;
1068 struct sockaddr_storage to_store, from_store;
1069 struct sockaddr *to = (struct sockaddr *)&to_store;
1070 struct sockaddr *from = (struct sockaddr *)&from_store;
1071 struct sctp_inpcb *inp;
1074 iph = mtod(m, struct ip *);
1075 if (iph->ip_v == IPVERSION) {
1077 struct sockaddr_in *from4;
1079 from4 = (struct sockaddr_in *)&from_store;
1080 bzero(from4, sizeof(*from4));
1081 from4->sin_family = AF_INET;
1082 from4->sin_len = sizeof(struct sockaddr_in);
1083 from4->sin_addr.s_addr = iph->ip_src.s_addr;
1084 from4->sin_port = sh->src_port;
1085 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
1087 struct ip6_hdr *ip6;
1088 struct sockaddr_in6 *from6;
1090 ip6 = mtod(m, struct ip6_hdr *);
1091 from6 = (struct sockaddr_in6 *)&from_store;
1092 bzero(from6, sizeof(*from6));
1093 from6->sin6_family = AF_INET6;
1094 from6->sin6_len = sizeof(struct sockaddr_in6);
1095 from6->sin6_addr = ip6->ip6_src;
1096 from6->sin6_port = sh->src_port;
1097 /* Get the scopes in properly to the sin6 addr's */
1098 /* we probably don't need these operations */
1099 (void)sa6_recoverscope(from6);
1100 sa6_embedscope(from6, ip6_use_defzone);
1102 /* Currently not supported. */
1106 /* we only go down this path if vtag is non-zero */
1107 retval = sctp_findassoc_by_vtag(from, ntohl(sh->v_tag),
1108 inp_p, netp, sh->src_port, sh->dest_port, 0);
1113 if (iph->ip_v == IPVERSION) {
1115 struct sockaddr_in *to4;
1117 to4 = (struct sockaddr_in *)&to_store;
1118 bzero(to4, sizeof(*to4));
1119 to4->sin_family = AF_INET;
1120 to4->sin_len = sizeof(struct sockaddr_in);
1121 to4->sin_addr.s_addr = iph->ip_dst.s_addr;
1122 to4->sin_port = sh->dest_port;
1123 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
1125 struct ip6_hdr *ip6;
1126 struct sockaddr_in6 *to6;
1128 ip6 = mtod(m, struct ip6_hdr *);
1129 to6 = (struct sockaddr_in6 *)&to_store;
1130 bzero(to6, sizeof(*to6));
1131 to6->sin6_family = AF_INET6;
1132 to6->sin6_len = sizeof(struct sockaddr_in6);
1133 to6->sin6_addr = ip6->ip6_dst;
1134 to6->sin6_port = sh->dest_port;
1135 /* Get the scopes in properly to the sin6 addr's */
1136 /* we probably don't need these operations */
1137 (void)sa6_recoverscope(to6);
1138 sa6_embedscope(to6, ip6_use_defzone);
1142 * FIX FIX?, I think we only need to look in the TCP pool if its an
1143 * INIT or COOKIE-ECHO, We really don't need to find it that way if
1144 * its a INIT-ACK or COOKIE_ACK since these in bot one-2-one and
1145 * one-2-N would be in the main pool anyway.
1147 if ((ch->chunk_type != SCTP_INITIATION) &&
1148 (ch->chunk_type != SCTP_INITIATION_ACK) &&
1149 (ch->chunk_type != SCTP_COOKIE_ACK) &&
1150 (ch->chunk_type != SCTP_COOKIE_ECHO)) {
1151 /* Other chunk types go to the tcp pool. */
1155 retval = sctp_findassociation_addr_sa(to, from, inp_p, netp,
1159 retval = sctp_findassociation_addr_sa(to, from, &inp, netp,
1163 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1164 printf("retval:%p inp:%p\n", retval, inp);
1167 if (retval == NULL && inp) {
1168 /* Found a EP but not this address */
1169 if ((ch->chunk_type == SCTP_INITIATION) ||
1170 (ch->chunk_type == SCTP_INITIATION_ACK)) {
1172 * special hook, we do NOT return linp or an
1173 * association that is linked to an existing
1174 * association that is under the TCP pool (i.e. no
1175 * listener exists). The endpoint finding routine
1176 * will always find a listner before examining the
1179 if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
1185 retval = sctp_findassociation_special_addr(m, iphlen,
1186 offset, sh, &inp, netp, to);
1187 if (inp_p != NULL) {
1193 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1194 printf("retval is %p\n", retval);
1201 * lookup an association by an ASCONF lookup address.
1202 * if the lookup address is 0.0.0.0 or ::0, use the vtag to do the lookup
1205 sctp_findassociation_ep_asconf(struct mbuf *m, int iphlen, int offset,
1206 struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp)
1208 struct sctp_tcb *stcb;
1209 struct sockaddr_in *sin;
1210 struct sockaddr_in6 *sin6;
1211 struct sockaddr_storage local_store, remote_store;
1213 struct sctp_paramhdr parm_buf, *phdr;
1215 int zero_address = 0;
1218 memset(&local_store, 0, sizeof(local_store));
1219 memset(&remote_store, 0, sizeof(remote_store));
1221 /* First get the destination address setup too. */
1222 iph = mtod(m, struct ip *);
1223 if (iph->ip_v == IPVERSION) {
1225 sin = (struct sockaddr_in *)&local_store;
1226 sin->sin_family = AF_INET;
1227 sin->sin_len = sizeof(*sin);
1228 sin->sin_port = sh->dest_port;
1229 sin->sin_addr.s_addr = iph->ip_dst.s_addr;
1230 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
1232 struct ip6_hdr *ip6;
1234 ip6 = mtod(m, struct ip6_hdr *);
1235 sin6 = (struct sockaddr_in6 *)&local_store;
1236 sin6->sin6_family = AF_INET6;
1237 sin6->sin6_len = sizeof(*sin6);
1238 sin6->sin6_port = sh->dest_port;
1239 sin6->sin6_addr = ip6->ip6_dst;
1244 phdr = sctp_get_next_param(m, offset + sizeof(struct sctp_asconf_chunk),
1245 &parm_buf, sizeof(struct sctp_paramhdr));
1248 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
1249 printf("findassociation_ep_asconf: failed to get asconf lookup addr\n");
1251 #endif /* SCTP_DEBUG */
1254 ptype = (int)((uint32_t) ntohs(phdr->param_type));
1255 /* get the correlation address */
1256 if (ptype == SCTP_IPV6_ADDRESS) {
1257 /* ipv6 address param */
1258 struct sctp_ipv6addr_param *p6, p6_buf;
1260 if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv6addr_param)) {
1263 p6 = (struct sctp_ipv6addr_param *)sctp_get_next_param(m,
1264 offset + sizeof(struct sctp_asconf_chunk),
1265 &p6_buf.ph, sizeof(*p6));
1268 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
1269 printf("findassociation_ep_asconf: failed to get asconf v6 lookup addr\n");
1271 #endif /* SCTP_DEBUG */
1274 sin6 = (struct sockaddr_in6 *)&remote_store;
1275 sin6->sin6_family = AF_INET6;
1276 sin6->sin6_len = sizeof(*sin6);
1277 sin6->sin6_port = sh->src_port;
1278 memcpy(&sin6->sin6_addr, &p6->addr, sizeof(struct in6_addr));
1279 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
1281 } else if (ptype == SCTP_IPV4_ADDRESS) {
1282 /* ipv4 address param */
1283 struct sctp_ipv4addr_param *p4, p4_buf;
1285 if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv4addr_param)) {
1288 p4 = (struct sctp_ipv4addr_param *)sctp_get_next_param(m,
1289 offset + sizeof(struct sctp_asconf_chunk),
1290 &p4_buf.ph, sizeof(*p4));
1293 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
1294 printf("findassociation_ep_asconf: failed to get asconf v4 lookup addr\n");
1296 #endif /* SCTP_DEBUG */
1299 sin = (struct sockaddr_in *)&remote_store;
1300 sin->sin_family = AF_INET;
1301 sin->sin_len = sizeof(*sin);
1302 sin->sin_port = sh->src_port;
1303 memcpy(&sin->sin_addr, &p4->addr, sizeof(struct in_addr));
1304 if (sin->sin_addr.s_addr == INADDR_ANY)
1307 /* invalid address param type */
1312 stcb = sctp_findassoc_by_vtag(NULL, ntohl(sh->v_tag), inp_p,
1313 netp, sh->src_port, sh->dest_port, 1);
1315 * printf("findassociation_ep_asconf: zero lookup address
1316 * finds stcb 0x%x\n", (uint32_t)stcb);
1319 stcb = sctp_findassociation_ep_addr(inp_p,
1320 (struct sockaddr *)&remote_store, netp,
1321 (struct sockaddr *)&local_store, NULL);
1327 extern int sctp_max_burst_default;
1329 extern unsigned int sctp_delayed_sack_time_default;
1330 extern unsigned int sctp_heartbeat_interval_default;
1331 extern unsigned int sctp_pmtu_raise_time_default;
1332 extern unsigned int sctp_shutdown_guard_time_default;
1333 extern unsigned int sctp_secret_lifetime_default;
1335 extern unsigned int sctp_rto_max_default;
1336 extern unsigned int sctp_rto_min_default;
1337 extern unsigned int sctp_rto_initial_default;
1338 extern unsigned int sctp_init_rto_max_default;
1339 extern unsigned int sctp_valid_cookie_life_default;
1340 extern unsigned int sctp_init_rtx_max_default;
1341 extern unsigned int sctp_assoc_rtx_max_default;
1342 extern unsigned int sctp_path_rtx_max_default;
1343 extern unsigned int sctp_nr_outgoing_streams_default;
1346 * allocate a sctp_inpcb and setup a temporary binding to a port/all
1347 * addresses. This way if we don't get a bind we by default pick a ephemeral
1348 * port with all addresses bound.
1351 sctp_inpcb_alloc(struct socket *so)
1354 * we get called when a new endpoint starts up. We need to allocate
1355 * the sctp_inpcb structure from the zone and init it. Mark it as
1356 * unbound and find a port that we can use as an ephemeral with
1357 * INADDR_ANY. If the user binds later no problem we can then add in
1358 * the specific addresses. And setup the default parameters for the
1362 struct sctp_inpcb *inp;
1365 struct timeval time;
1366 sctp_sharedkey_t *null_key;
1370 SCTP_INP_INFO_WLOCK();
1371 inp = (struct sctp_inpcb *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_ep);
1373 printf("Out of SCTP-INPCB structures - no resources\n");
1374 SCTP_INP_INFO_WUNLOCK();
1378 bzero(inp, sizeof(*inp));
1380 /* bump generations */
1381 /* setup socket pointers */
1382 inp->sctp_socket = so;
1383 inp->ip_inp.inp.inp_socket = so;
1385 inp->partial_delivery_point = so->so_rcv.sb_hiwat >> SCTP_PARTIAL_DELIVERY_SHIFT;
1386 inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
1390 struct inpcbpolicy *pcb_sp = NULL;
1392 error = ipsec_init_pcbpolicy(so, &pcb_sp);
1393 /* Arrange to share the policy */
1394 inp->ip_inp.inp.inp_sp = pcb_sp;
1395 ((struct in6pcb *)(&inp->ip_inp.inp))->in6p_sp = pcb_sp;
1398 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
1399 SCTP_INP_INFO_WUNLOCK();
1403 SCTP_INCR_EP_COUNT();
1404 inp->ip_inp.inp.inp_ip_ttl = ip_defttl;
1405 SCTP_INP_INFO_WUNLOCK();
1407 so->so_pcb = (caddr_t)inp;
1409 if ((so->so_type == SOCK_DGRAM) ||
1410 (so->so_type == SOCK_SEQPACKET)) {
1411 /* UDP style socket */
1412 inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
1413 SCTP_PCB_FLAGS_UNBOUND);
1414 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
1415 /* Be sure it is NON-BLOCKING IO for UDP */
1416 /* so->so_state |= SS_NBIO; */
1417 } else if (so->so_type == SOCK_STREAM) {
1418 /* TCP style socket */
1419 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
1420 SCTP_PCB_FLAGS_UNBOUND);
1421 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
1422 /* Be sure we have blocking IO by default */
1423 so->so_state &= ~SS_NBIO;
1426 * unsupported socket type (RAW, etc)- in case we missed it
1429 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
1430 return (EOPNOTSUPP);
1432 inp->sctp_tcbhash = SCTP_HASH_INIT(sctp_pcbtblsize,
1433 &inp->sctp_hashmark);
1434 if (inp->sctp_tcbhash == NULL) {
1435 printf("Out of SCTP-INPCB->hashinit - no resources\n");
1436 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
1439 SCTP_INP_INFO_WLOCK();
1440 SCTP_INP_LOCK_INIT(inp);
1441 SCTP_INP_READ_INIT(inp);
1442 SCTP_ASOC_CREATE_LOCK_INIT(inp);
1443 /* lock the new ep */
1444 SCTP_INP_WLOCK(inp);
1446 /* add it to the info area */
1447 LIST_INSERT_HEAD(&sctppcbinfo.listhead, inp, sctp_list);
1448 SCTP_INP_INFO_WUNLOCK();
1450 TAILQ_INIT(&inp->read_queue);
1451 LIST_INIT(&inp->sctp_addr_list);
1452 LIST_INIT(&inp->sctp_asoc_list);
1454 #ifdef SCTP_TRACK_FREED_ASOCS
1456 LIST_INIT(&inp->sctp_asoc_free_list);
1458 /* Init the timer structure for signature change */
1459 SCTP_OS_TIMER_INIT(&inp->sctp_ep.signature_change.timer);
1460 inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NEWCOOKIE;
1462 /* now init the actual endpoint default data */
1465 /* setup the base timeout information */
1466 m->sctp_timeoutticks[SCTP_TIMER_SEND] = SEC_TO_TICKS(SCTP_SEND_SEC); /* needed ? */
1467 m->sctp_timeoutticks[SCTP_TIMER_INIT] = SEC_TO_TICKS(SCTP_INIT_SEC); /* needed ? */
1468 m->sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sctp_delayed_sack_time_default);
1469 m->sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(sctp_heartbeat_interval_default);
1470 m->sctp_timeoutticks[SCTP_TIMER_PMTU] = SEC_TO_TICKS(sctp_pmtu_raise_time_default);
1471 m->sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] = SEC_TO_TICKS(sctp_shutdown_guard_time_default);
1472 m->sctp_timeoutticks[SCTP_TIMER_SIGNATURE] = SEC_TO_TICKS(sctp_secret_lifetime_default);
1473 /* all max/min max are in ms */
1474 m->sctp_maxrto = sctp_rto_max_default;
1475 m->sctp_minrto = sctp_rto_min_default;
1476 m->initial_rto = sctp_rto_initial_default;
1477 m->initial_init_rto_max = sctp_init_rto_max_default;
1479 m->max_open_streams_intome = MAX_SCTP_STREAMS;
1481 m->max_init_times = sctp_init_rtx_max_default;
1482 m->max_send_times = sctp_assoc_rtx_max_default;
1483 m->def_net_failure = sctp_path_rtx_max_default;
1484 m->sctp_sws_sender = SCTP_SWS_SENDER_DEF;
1485 m->sctp_sws_receiver = SCTP_SWS_RECEIVER_DEF;
1486 m->max_burst = sctp_max_burst_default;
1487 /* number of streams to pre-open on a association */
1488 m->pre_open_stream_count = sctp_nr_outgoing_streams_default;
1490 /* Add adaptation cookie */
1491 m->adaptation_layer_indicator = 0x504C5253;
1493 /* seed random number generator */
1494 m->random_counter = 1;
1495 m->store_at = SCTP_SIGNATURE_SIZE;
1496 SCTP_READ_RANDOM(m->random_numbers, sizeof(m->random_numbers));
1497 sctp_fill_random_store(m);
1499 /* Minimum cookie size */
1500 m->size_of_a_cookie = (sizeof(struct sctp_init_msg) * 2) +
1501 sizeof(struct sctp_state_cookie);
1502 m->size_of_a_cookie += SCTP_SIGNATURE_SIZE;
1504 /* Setup the initial secret */
1505 SCTP_GETTIME_TIMEVAL(&time);
1506 m->time_of_secret_change = time.tv_sec;
1508 for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
1509 m->secret_key[0][i] = sctp_select_initial_TSN(m);
1511 sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL);
1513 /* How long is a cookie good for ? */
1514 m->def_cookie_life = sctp_valid_cookie_life_default;
1517 * Initialize authentication parameters
1519 m->local_hmacs = sctp_default_supported_hmaclist();
1520 m->local_auth_chunks = sctp_alloc_chunklist();
1521 sctp_auth_set_default_chunks(m->local_auth_chunks);
1522 LIST_INIT(&m->shared_keys);
1523 /* add default NULL key as key id 0 */
1524 null_key = sctp_alloc_sharedkey();
1525 sctp_insert_sharedkey(&m->shared_keys, null_key);
1526 SCTP_INP_WUNLOCK(inp);
1527 #ifdef SCTP_LOG_CLOSING
1528 sctp_log_closing(inp, NULL, 12);
1535 sctp_move_pcb_and_assoc(struct sctp_inpcb *old_inp, struct sctp_inpcb *new_inp,
1536 struct sctp_tcb *stcb)
1538 struct sctp_nets *net;
1539 uint16_t lport, rport;
1540 struct sctppcbhead *head;
1541 struct sctp_laddr *laddr, *oladdr;
1543 SCTP_TCB_UNLOCK(stcb);
1544 SCTP_INP_INFO_WLOCK();
1545 SCTP_INP_WLOCK(old_inp);
1546 SCTP_INP_WLOCK(new_inp);
1547 SCTP_TCB_LOCK(stcb);
1549 new_inp->sctp_ep.time_of_secret_change =
1550 old_inp->sctp_ep.time_of_secret_change;
1551 memcpy(new_inp->sctp_ep.secret_key, old_inp->sctp_ep.secret_key,
1552 sizeof(old_inp->sctp_ep.secret_key));
1553 new_inp->sctp_ep.current_secret_number =
1554 old_inp->sctp_ep.current_secret_number;
1555 new_inp->sctp_ep.last_secret_number =
1556 old_inp->sctp_ep.last_secret_number;
1557 new_inp->sctp_ep.size_of_a_cookie = old_inp->sctp_ep.size_of_a_cookie;
1559 /* make it so new data pours into the new socket */
1560 stcb->sctp_socket = new_inp->sctp_socket;
1561 stcb->sctp_ep = new_inp;
1563 /* Copy the port across */
1564 lport = new_inp->sctp_lport = old_inp->sctp_lport;
1565 rport = stcb->rport;
1566 /* Pull the tcb from the old association */
1567 LIST_REMOVE(stcb, sctp_tcbhash);
1568 LIST_REMOVE(stcb, sctp_tcblist);
1570 /* Now insert the new_inp into the TCP connected hash */
1571 head = &sctppcbinfo.sctp_tcpephash[SCTP_PCBHASH_ALLADDR((lport + rport),
1572 sctppcbinfo.hashtcpmark)];
1574 LIST_INSERT_HEAD(head, new_inp, sctp_hash);
1576 /* Now move the tcb into the endpoint list */
1577 LIST_INSERT_HEAD(&new_inp->sctp_asoc_list, stcb, sctp_tcblist);
1579 * Question, do we even need to worry about the ep-hash since we
1580 * only have one connection? Probably not :> so lets get rid of it
1581 * and not suck up any kernel memory in that.
1584 /* Ok. Let's restart timer. */
1585 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1586 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, new_inp,
1590 SCTP_INP_INFO_WUNLOCK();
1591 if (new_inp->sctp_tcbhash != NULL) {
1592 SCTP_HASH_FREE(new_inp->sctp_tcbhash, new_inp->sctp_hashmark);
1593 new_inp->sctp_tcbhash = NULL;
1595 if ((new_inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
1596 /* Subset bound, so copy in the laddr list from the old_inp */
1597 LIST_FOREACH(oladdr, &old_inp->sctp_addr_list, sctp_nxt_addr) {
1598 laddr = (struct sctp_laddr *)SCTP_ZONE_GET(
1599 sctppcbinfo.ipi_zone_laddr);
1600 if (laddr == NULL) {
1602 * Gak, what can we do? This assoc is really
1603 * HOSED. We probably should send an abort
1607 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1608 printf("Association hosed in TCP model, out of laddr memory\n");
1610 #endif /* SCTP_DEBUG */
1613 SCTP_INCR_LADDR_COUNT();
1614 bzero(laddr, sizeof(*laddr));
1615 laddr->ifa = oladdr->ifa;
1616 LIST_INSERT_HEAD(&new_inp->sctp_addr_list, laddr,
1618 new_inp->laddr_count++;
1622 * Now any running timers need to be adjusted since we really don't
1623 * care if they are running or not just blast in the new_inp into
1627 stcb->asoc.hb_timer.ep = (void *)new_inp;
1628 stcb->asoc.dack_timer.ep = (void *)new_inp;
1629 stcb->asoc.asconf_timer.ep = (void *)new_inp;
1630 stcb->asoc.strreset_timer.ep = (void *)new_inp;
1631 stcb->asoc.shut_guard_timer.ep = (void *)new_inp;
1632 stcb->asoc.autoclose_timer.ep = (void *)new_inp;
1633 stcb->asoc.delayed_event_timer.ep = (void *)new_inp;
1634 /* now what about the nets? */
1635 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1636 net->pmtu_timer.ep = (void *)new_inp;
1637 net->rxt_timer.ep = (void *)new_inp;
1638 net->fr_timer.ep = (void *)new_inp;
1640 SCTP_INP_WUNLOCK(new_inp);
1641 SCTP_INP_WUNLOCK(old_inp);
1645 sctp_isport_inuse(struct sctp_inpcb *inp, uint16_t lport)
1647 struct sctppcbhead *head;
1648 struct sctp_inpcb *t_inp;
1650 head = &sctppcbinfo.sctp_ephash[SCTP_PCBHASH_ALLADDR(lport,
1651 sctppcbinfo.hashmark)];
1653 LIST_FOREACH(t_inp, head, sctp_hash) {
1654 if (t_inp->sctp_lport != lport) {
1657 /* This one is in use. */
1658 /* check the v6/v4 binding issue */
1659 if ((t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1660 SCTP_IPV6_V6ONLY(t_inp)) {
1661 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1662 /* collision in V6 space */
1665 /* inp is BOUND_V4 no conflict */
1668 } else if (t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1669 /* t_inp is bound v4 and v6, conflict always */
1672 /* t_inp is bound only V4 */
1673 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1674 SCTP_IPV6_V6ONLY(t_inp)) {
1678 /* else fall through to conflict */
1688 sctp_inpcb_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
1690 /* bind a ep to a socket address */
1691 struct sctppcbhead *head;
1692 struct sctp_inpcb *inp, *inp_tmp;
1693 struct inpcb *ip_inp;
1701 inp = (struct sctp_inpcb *)so->so_pcb;
1702 ip_inp = (struct inpcb *)so->so_pcb;
1704 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1706 printf("Bind called port:%d\n",
1707 ntohs(((struct sockaddr_in *)addr)->sin_port));
1709 sctp_print_address(addr);
1712 #endif /* SCTP_DEBUG */
1713 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
1714 /* already did a bind, subsequent binds NOT allowed ! */
1718 if (addr->sa_family == AF_INET) {
1719 struct sockaddr_in *sin;
1721 /* IPV6_V6ONLY socket? */
1722 if (SCTP_IPV6_V6ONLY(ip_inp)) {
1725 if (addr->sa_len != sizeof(*sin))
1728 sin = (struct sockaddr_in *)addr;
1729 lport = sin->sin_port;
1731 if (sin->sin_addr.s_addr != INADDR_ANY) {
1734 } else if (addr->sa_family == AF_INET6) {
1735 /* Only for pure IPv6 Address. (No IPv4 Mapped!) */
1736 struct sockaddr_in6 *sin6;
1738 sin6 = (struct sockaddr_in6 *)addr;
1740 if (addr->sa_len != sizeof(*sin6))
1743 lport = sin6->sin6_port;
1744 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1746 /* KAME hack: embed scopeid */
1747 if (sa6_embedscope(sin6, ip6_use_defzone) != 0)
1750 /* this must be cleared for ifa_ifwithaddr() */
1751 sin6->sin6_scope_id = 0;
1753 return (EAFNOSUPPORT);
1756 SCTP_INP_INFO_WLOCK();
1757 SCTP_INP_WLOCK(inp);
1758 /* increase our count due to the unlock we do */
1759 SCTP_INP_INCR_REF(inp);
1762 * Did the caller specify a port? if so we must see if a ep
1763 * already has this one bound.
1765 /* got to be root to get at low ports */
1766 if (ntohs(lport) < IPPORT_RESERVED) {
1769 PRIV_NETINET_RESERVEDPORT)
1771 SCTP_INP_DECR_REF(inp);
1772 SCTP_INP_WUNLOCK(inp);
1773 SCTP_INP_INFO_WUNLOCK();
1778 SCTP_INP_DECR_REF(inp);
1779 SCTP_INP_WUNLOCK(inp);
1780 SCTP_INP_INFO_WUNLOCK();
1783 SCTP_INP_WUNLOCK(inp);
1784 inp_tmp = sctp_pcb_findep(addr, 0, 1);
1785 if (inp_tmp != NULL) {
1787 * lock guy returned and lower count note that we
1788 * are not bound so inp_tmp should NEVER be inp. And
1789 * it is this inp (inp_tmp) that gets the reference
1790 * bump, so we must lower it.
1792 SCTP_INP_DECR_REF(inp_tmp);
1793 SCTP_INP_DECR_REF(inp);
1795 SCTP_INP_INFO_WUNLOCK();
1796 return (EADDRNOTAVAIL);
1798 SCTP_INP_WLOCK(inp);
1800 /* verify that no lport is not used by a singleton */
1801 if (sctp_isport_inuse(inp, lport)) {
1802 /* Sorry someone already has this one bound */
1803 SCTP_INP_DECR_REF(inp);
1804 SCTP_INP_WUNLOCK(inp);
1805 SCTP_INP_INFO_WUNLOCK();
1806 return (EADDRNOTAVAIL);
1811 * get any port but lets make sure no one has any address
1812 * with this port bound
1816 * setup the inp to the top (I could use the union but this
1819 uint32_t port_guess;
1820 uint16_t port_attempt;
1824 port_guess = sctp_select_initial_TSN(&inp->sctp_ep);
1825 port_attempt = (port_guess & 0x0000ffff);
1826 if (port_attempt == 0) {
1829 if (port_attempt < IPPORT_RESERVED) {
1830 port_attempt += IPPORT_RESERVED;
1832 if (sctp_isport_inuse(inp, htons(port_attempt)) == 0) {
1833 /* got a port we can use */
1837 /* try upper half */
1839 port_attempt = ((port_guess >> 16) & 0x0000ffff);
1840 if (port_attempt == 0) {
1843 if (port_attempt < IPPORT_RESERVED) {
1844 port_attempt += IPPORT_RESERVED;
1846 if (sctp_isport_inuse(inp, htons(port_attempt)) == 0) {
1847 /* got a port we can use */
1851 /* try two half's added together */
1853 port_attempt = (((port_guess >> 16) & 0x0000ffff) +
1854 (port_guess & 0x0000ffff));
1855 if (port_attempt == 0) {
1856 /* get a new random number */
1859 if (port_attempt < IPPORT_RESERVED) {
1860 port_attempt += IPPORT_RESERVED;
1862 if (sctp_isport_inuse(inp, htons(port_attempt)) == 0) {
1863 /* got a port we can use */
1868 /* we don't get out of the loop until we have a port */
1869 lport = htons(port_attempt);
1871 SCTP_INP_DECR_REF(inp);
1872 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE |
1873 SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
1875 * this really should not happen. The guy did a non-blocking
1876 * bind and then did a close at the same time.
1878 SCTP_INP_WUNLOCK(inp);
1879 SCTP_INP_INFO_WUNLOCK();
1882 /* ok we look clear to give out this port, so lets setup the binding */
1884 /* binding to all addresses, so just set in the proper flags */
1885 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUNDALL;
1886 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
1887 /* set the automatic addr changes from kernel flag */
1888 if (sctp_auto_asconf == 0) {
1889 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1891 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1895 * bind specific, make sure flags is off and add a new
1896 * address structure to the sctp_addr_list inside the ep
1899 * We will need to allocate one and insert it at the head. The
1900 * socketopt call can just insert new addresses in there as
1901 * well. It will also have to do the embed scope kame hack
1902 * too (before adding).
1905 struct sockaddr_storage store_sa;
1907 memset(&store_sa, 0, sizeof(store_sa));
1908 if (addr->sa_family == AF_INET) {
1909 struct sockaddr_in *sin;
1911 sin = (struct sockaddr_in *)&store_sa;
1912 memcpy(sin, addr, sizeof(struct sockaddr_in));
1914 } else if (addr->sa_family == AF_INET6) {
1915 struct sockaddr_in6 *sin6;
1917 sin6 = (struct sockaddr_in6 *)&store_sa;
1918 memcpy(sin6, addr, sizeof(struct sockaddr_in6));
1919 sin6->sin6_port = 0;
1922 * first find the interface with the bound address need to
1923 * zero out the port to find the address! yuck! can't do
1924 * this earlier since need port for sctp_pcb_findep()
1926 ifa = sctp_find_ifa_by_addr((struct sockaddr *)&store_sa);
1928 /* Can't find an interface with that address */
1929 SCTP_INP_WUNLOCK(inp);
1930 SCTP_INP_INFO_WUNLOCK();
1931 return (EADDRNOTAVAIL);
1933 if (addr->sa_family == AF_INET6) {
1934 struct in6_ifaddr *ifa6;
1936 ifa6 = (struct in6_ifaddr *)ifa;
1938 * allow binding of deprecated addresses as per RFC
1939 * 2462 and ipng discussion
1941 if (ifa6->ia6_flags & (IN6_IFF_DETACHED |
1943 IN6_IFF_NOTREADY)) {
1944 /* Can't bind a non-existent addr. */
1945 SCTP_INP_WUNLOCK(inp);
1946 SCTP_INP_INFO_WUNLOCK();
1950 /* we're not bound all */
1951 inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUNDALL;
1952 /* set the automatic addr changes from kernel flag */
1953 if (sctp_auto_asconf == 0) {
1954 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1956 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1958 /* allow bindx() to send ASCONF's for binding changes */
1959 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
1960 /* add this address to the endpoint list */
1961 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa);
1963 SCTP_INP_WUNLOCK(inp);
1964 SCTP_INP_INFO_WUNLOCK();
1969 /* find the bucket */
1970 head = &sctppcbinfo.sctp_ephash[SCTP_PCBHASH_ALLADDR(lport,
1971 sctppcbinfo.hashmark)];
1972 /* put it in the bucket */
1973 LIST_INSERT_HEAD(head, inp, sctp_hash);
1975 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1976 printf("Main hash to bind at head:%p, bound port:%d\n", head, ntohs(lport));
1979 /* set in the port */
1980 inp->sctp_lport = lport;
1982 /* turn off just the unbound flag */
1983 inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
1984 SCTP_INP_WUNLOCK(inp);
1985 SCTP_INP_INFO_WUNLOCK();
1991 sctp_iterator_inp_being_freed(struct sctp_inpcb *inp, struct sctp_inpcb *inp_next)
1993 struct sctp_iterator *it;
1996 * We enter with the only the ITERATOR_LOCK in place and a write
1997 * lock on the inp_info stuff.
2001 * Go through all iterators, we must do this since it is possible
2002 * that some iterator does NOT have the lock, but is waiting for it.
2003 * And the one that had the lock has either moved in the last
2004 * iteration or we just cleared it above. We need to find all of
2005 * those guys. The list of iterators should never be very big
2008 LIST_FOREACH(it, &sctppcbinfo.iteratorhead, sctp_nxt_itr) {
2009 if (it == inp->inp_starting_point_for_iterator)
2010 /* skip this guy, he's special */
2012 if (it->inp == inp) {
2014 * This is tricky and we DON'T lock the iterator.
2015 * Reason is he's running but waiting for me since
2016 * inp->inp_starting_point_for_iterator has the lock
2017 * on me (the guy above we skipped). This tells us
2018 * its is not running but waiting for
2019 * inp->inp_starting_point_for_iterator to be
2020 * released by the guy that does have our INP in a
2023 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
2027 /* set him up to do the next guy not me */
2033 it = inp->inp_starting_point_for_iterator;
2035 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
2044 /* release sctp_inpcb unbind the port */
2046 sctp_inpcb_free(struct sctp_inpcb *inp, int immediate, int from)
2049 * Here we free a endpoint. We must find it (if it is in the Hash
2050 * table) and remove it from there. Then we must also find it in the
2051 * overall list and remove it from there. After all removals are
2052 * complete then any timer has to be stopped. Then start the actual
2053 * freeing. a) Any local lists. b) Any associations. c) The hash of
2054 * all associations. d) finally the ep itself.
2057 struct sctp_inpcb *inp_save;
2058 struct sctp_tcb *asoc, *nasoc;
2059 struct sctp_laddr *laddr, *nladdr;
2060 struct inpcb *ip_pcb;
2063 struct sctp_queued_to_read *sq;
2066 sctp_sharedkey_t *shared_key;
2070 #ifdef SCTP_LOG_CLOSING
2071 sctp_log_closing(inp, NULL, 0);
2074 SCTP_ITERATOR_LOCK();
2075 so = inp->sctp_socket;
2076 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
2077 /* been here before.. eeks.. get out of here */
2079 printf("This conflict in free SHOULD not be happening!\n");
2080 SCTP_ITERATOR_UNLOCK();
2081 #ifdef SCTP_LOG_CLOSING
2082 sctp_log_closing(inp, NULL, 1);
2086 SCTP_ASOC_CREATE_LOCK(inp);
2087 SCTP_INP_INFO_WLOCK();
2089 SCTP_INP_WLOCK(inp);
2091 * First time through we have the socket lock, after that no more.
2095 * Once we are in we can remove the flag from = 1 is only
2096 * passed from the actual closing routines that are called
2097 * via the sockets layer.
2099 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CLOSE_IP;
2101 sctp_timer_stop(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL,
2102 SCTP_FROM_SCTP_PCB + SCTP_LOC_1);
2105 sctp_m_freem(inp->control);
2106 inp->control = NULL;
2109 sctp_m_freem(inp->pkt);
2113 ip_pcb = &inp->ip_inp.inp; /* we could just cast the main pointer
2114 * here but I will be nice :> (i.e.
2116 if (immediate == 0) {
2120 for ((asoc = LIST_FIRST(&inp->sctp_asoc_list)); asoc != NULL;
2122 nasoc = LIST_NEXT(asoc, sctp_tcblist);
2123 if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
2124 /* Skip guys being freed */
2125 asoc->sctp_socket = NULL;
2129 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_WAIT) ||
2130 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
2131 /* Just abandon things in the front states */
2132 if (asoc->asoc.total_output_queue_size == 0) {
2133 sctp_free_assoc(inp, asoc, SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_2);
2137 SCTP_TCB_LOCK(asoc);
2138 /* Disconnect the socket please */
2139 asoc->sctp_socket = NULL;
2140 asoc->asoc.state |= SCTP_STATE_CLOSED_SOCKET;
2141 if ((asoc->asoc.size_on_reasm_queue > 0) ||
2142 (asoc->asoc.control_pdapi) ||
2143 (asoc->asoc.size_on_all_streams > 0) ||
2144 (so && (so->so_rcv.sb_cc > 0))
2146 /* Left with Data unread */
2147 struct mbuf *op_err;
2149 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
2150 0, M_DONTWAIT, 1, MT_DATA);
2152 /* Fill in the user initiated abort */
2153 struct sctp_paramhdr *ph;
2156 SCTP_BUF_LEN(op_err) =
2157 sizeof(struct sctp_paramhdr) + sizeof(uint32_t);
2159 struct sctp_paramhdr *);
2160 ph->param_type = htons(
2161 SCTP_CAUSE_USER_INITIATED_ABT);
2162 ph->param_length = htons(SCTP_BUF_LEN(op_err));
2163 ippp = (uint32_t *) (ph + 1);
2164 *ippp = htonl(SCTP_FROM_SCTP_PCB + SCTP_LOC_3);
2166 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_3;
2167 sctp_send_abort_tcb(asoc, op_err);
2168 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
2169 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
2170 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
2171 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
2173 sctp_free_assoc(inp, asoc, SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_4);
2175 } else if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
2176 TAILQ_EMPTY(&asoc->asoc.sent_queue) &&
2177 (asoc->asoc.stream_queue_cnt == 0)
2179 if (asoc->asoc.locked_on_sending) {
2182 if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
2183 (SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
2185 * there is nothing queued to send,
2186 * so I send shutdown
2188 sctp_send_shutdown(asoc, asoc->asoc.primary_destination);
2189 asoc->asoc.state = SCTP_STATE_SHUTDOWN_SENT;
2190 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
2191 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, asoc->sctp_ep, asoc,
2192 asoc->asoc.primary_destination);
2193 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc,
2194 asoc->asoc.primary_destination);
2195 sctp_chunk_output(inp, asoc, SCTP_OUTPUT_FROM_SHUT_TMR);
2198 /* mark into shutdown pending */
2199 struct sctp_stream_queue_pending *sp;
2201 asoc->asoc.state |= SCTP_STATE_SHUTDOWN_PENDING;
2202 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc,
2203 asoc->asoc.primary_destination);
2204 if (asoc->asoc.locked_on_sending) {
2205 sp = TAILQ_LAST(&((asoc->asoc.locked_on_sending)->outqueue),
2208 printf("Error, sp is NULL, locked on sending is %p strm:%d\n",
2209 asoc->asoc.locked_on_sending,
2210 asoc->asoc.locked_on_sending->stream_no);
2212 if ((sp->length == 0) && (sp->msg_is_complete == 0))
2213 asoc->asoc.state |= SCTP_STATE_PARTIAL_MSG_LEFT;
2216 if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
2217 TAILQ_EMPTY(&asoc->asoc.sent_queue) &&
2218 (asoc->asoc.state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
2219 struct mbuf *op_err;
2222 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
2223 0, M_DONTWAIT, 1, MT_DATA);
2229 struct sctp_paramhdr *ph;
2232 SCTP_BUF_LEN(op_err) =
2233 (sizeof(struct sctp_paramhdr) +
2236 struct sctp_paramhdr *);
2237 ph->param_type = htons(
2238 SCTP_CAUSE_USER_INITIATED_ABT);
2239 ph->param_length = htons(SCTP_BUF_LEN(op_err));
2240 ippp = (uint32_t *) (ph + 1);
2241 *ippp = htonl(SCTP_FROM_SCTP_PCB + SCTP_LOC_5);
2243 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_5;
2244 sctp_send_abort_tcb(asoc, op_err);
2245 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
2246 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
2247 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
2248 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
2250 sctp_free_assoc(inp, asoc, SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_6);
2255 SCTP_TCB_UNLOCK(asoc);
2257 /* now is there some left in our SHUTDOWN state? */
2261 SCTP_INP_WUNLOCK(inp);
2262 SCTP_ASOC_CREATE_UNLOCK(inp);
2263 SCTP_INP_INFO_WUNLOCK();
2264 SCTP_ITERATOR_UNLOCK();
2265 #ifdef SCTP_LOG_CLOSING
2266 sctp_log_closing(inp, NULL, 2);
2271 inp->sctp_socket = NULL;
2272 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) !=
2273 SCTP_PCB_FLAGS_UNBOUND) {
2275 * ok, this guy has been bound. It's port is somewhere in
2276 * the sctppcbinfo hash table. Remove it!
2278 LIST_REMOVE(inp, sctp_hash);
2279 inp->sctp_flags |= SCTP_PCB_FLAGS_UNBOUND;
2282 * If there is a timer running to kill us, forget it, since it may
2283 * have a contest on the INP lock.. which would cause us to die ...
2286 for ((asoc = LIST_FIRST(&inp->sctp_asoc_list)); asoc != NULL;
2288 nasoc = LIST_NEXT(asoc, sctp_tcblist);
2289 if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
2293 /* Free associations that are NOT killing us */
2294 SCTP_TCB_LOCK(asoc);
2295 if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_COOKIE_WAIT) &&
2296 ((asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0)) {
2297 struct mbuf *op_err;
2300 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
2301 0, M_DONTWAIT, 1, MT_DATA);
2303 /* Fill in the user initiated abort */
2304 struct sctp_paramhdr *ph;
2306 SCTP_BUF_LEN(op_err) = (sizeof(struct sctp_paramhdr) +
2308 ph = mtod(op_err, struct sctp_paramhdr *);
2309 ph->param_type = htons(
2310 SCTP_CAUSE_USER_INITIATED_ABT);
2311 ph->param_length = htons(SCTP_BUF_LEN(op_err));
2312 ippp = (uint32_t *) (ph + 1);
2313 *ippp = htonl(SCTP_FROM_SCTP_PCB + SCTP_LOC_7);
2316 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_7;
2317 sctp_send_abort_tcb(asoc, op_err);
2318 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
2319 } else if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
2321 SCTP_TCB_UNLOCK(asoc);
2324 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
2325 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
2326 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
2328 sctp_free_assoc(inp, asoc, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_8);
2331 /* Ok we have someone out there that will kill us */
2332 SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
2333 SCTP_INP_WUNLOCK(inp);
2334 SCTP_ASOC_CREATE_UNLOCK(inp);
2335 SCTP_INP_INFO_WUNLOCK();
2336 SCTP_ITERATOR_UNLOCK();
2337 #ifdef SCTP_LOG_CLOSING
2338 sctp_log_closing(inp, NULL, 3);
2342 if ((inp->refcount) || (inp->sctp_flags & SCTP_PCB_FLAGS_CLOSE_IP)) {
2343 SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
2344 sctp_timer_start(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL);
2345 SCTP_INP_WUNLOCK(inp);
2346 SCTP_ASOC_CREATE_UNLOCK(inp);
2347 SCTP_INP_INFO_WUNLOCK();
2348 SCTP_ITERATOR_UNLOCK();
2349 #ifdef SCTP_LOG_CLOSING
2350 sctp_log_closing(inp, NULL, 4);
2354 SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
2355 inp->sctp_ep.signature_change.type = 0;
2356 inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_ALLGONE;
2358 #ifdef SCTP_LOG_CLOSING
2359 sctp_log_closing(inp, NULL, 5);
2362 SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
2363 inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NONE;
2364 /* Clear the read queue */
2365 while ((sq = TAILQ_FIRST(&inp->read_queue)) != NULL) {
2366 TAILQ_REMOVE(&inp->read_queue, sq, next);
2367 sctp_free_remote_addr(sq->whoFrom);
2369 so->so_rcv.sb_cc -= sq->length;
2371 sctp_m_freem(sq->data);
2375 * no need to free the net count, since at this point all
2378 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_readq, sq);
2379 SCTP_DECR_READQ_COUNT();
2381 /* Now the sctp_pcb things */
2383 * free each asoc if it is not already closed/free. we can't use the
2384 * macro here since le_next will get freed as part of the
2385 * sctp_free_assoc() call.
2390 ipsec4_delete_pcbpolicy(ip_pcb);
2393 /* Unlocks not needed since the socket is gone now */
2395 if (ip_pcb->inp_options) {
2396 (void)sctp_m_free(ip_pcb->inp_options);
2397 ip_pcb->inp_options = 0;
2399 if (ip_pcb->inp_moptions) {
2400 ip_freemoptions(ip_pcb->inp_moptions);
2401 ip_pcb->inp_moptions = 0;
2404 if (ip_pcb->inp_vflag & INP_IPV6) {
2405 struct in6pcb *in6p;
2407 in6p = (struct in6pcb *)inp;
2408 ip6_freepcbopts(in6p->in6p_outputopts);
2411 ip_pcb->inp_vflag = 0;
2412 /* free up authentication fields */
2413 if (inp->sctp_ep.local_auth_chunks != NULL)
2414 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2415 if (inp->sctp_ep.local_hmacs != NULL)
2416 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2418 shared_key = LIST_FIRST(&inp->sctp_ep.shared_keys);
2419 while (shared_key) {
2420 LIST_REMOVE(shared_key, next);
2421 sctp_free_sharedkey(shared_key);
2422 shared_key = LIST_FIRST(&inp->sctp_ep.shared_keys);
2425 inp_save = LIST_NEXT(inp, sctp_list);
2426 LIST_REMOVE(inp, sctp_list);
2428 /* fix any iterators only after out of the list */
2429 sctp_iterator_inp_being_freed(inp, inp_save);
2431 * if we have an address list the following will free the list of
2432 * ifaddr's that are set into this ep. Again macro limitations here,
2433 * since the LIST_FOREACH could be a bad idea.
2435 for ((laddr = LIST_FIRST(&inp->sctp_addr_list)); laddr != NULL;
2437 nladdr = LIST_NEXT(laddr, sctp_nxt_addr);
2438 LIST_REMOVE(laddr, sctp_nxt_addr);
2439 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_laddr, laddr);
2440 SCTP_DECR_LADDR_COUNT();
2443 #ifdef SCTP_TRACK_FREED_ASOCS
2445 for ((asoc = LIST_FIRST(&inp->sctp_asoc_free_list)); asoc != NULL;
2447 nasoc = LIST_NEXT(asoc, sctp_tcblist);
2448 LIST_REMOVE(asoc, sctp_tcblist);
2449 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, asoc);
2450 SCTP_DECR_ASOC_COUNT();
2452 /* *** END TEMP CODE *** */
2454 /* Now lets see about freeing the EP hash table. */
2455 if (inp->sctp_tcbhash != NULL) {
2456 SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark);
2457 inp->sctp_tcbhash = NULL;
2459 /* Now we must put the ep memory back into the zone pool */
2460 SCTP_INP_LOCK_DESTROY(inp);
2461 SCTP_INP_READ_DESTROY(inp);
2462 SCTP_ASOC_CREATE_LOCK_DESTROY(inp);
2463 SCTP_INP_INFO_WUNLOCK();
2465 SCTP_ITERATOR_UNLOCK();
2467 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
2468 SCTP_DECR_EP_COUNT();
2475 sctp_findnet(struct sctp_tcb *stcb, struct sockaddr *addr)
2477 struct sctp_nets *net;
2479 /* locate the address */
2480 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2481 if (sctp_cmpaddr(addr, (struct sockaddr *)&net->ro._l_addr))
2489 * add's a remote endpoint address, done with the INIT/INIT-ACK as well as
2490 * when a ASCONF arrives that adds it. It will also initialize all the cwnd
2494 sctp_is_address_on_local_host(struct sockaddr *addr)
2499 TAILQ_FOREACH(ifn, &ifnet, if_list) {
2500 TAILQ_FOREACH(ifa, &ifn->if_addrlist, ifa_list) {
2501 if (addr->sa_family == ifa->ifa_addr->sa_family) {
2503 if (addr->sa_family == AF_INET) {
2504 struct sockaddr_in *sin, *sin_c;
2506 sin = (struct sockaddr_in *)addr;
2507 sin_c = (struct sockaddr_in *)
2509 if (sin->sin_addr.s_addr ==
2510 sin_c->sin_addr.s_addr) {
2512 * we are on the same
2517 } else if (addr->sa_family == AF_INET6) {
2518 struct sockaddr_in6 *sin6, *sin_c6;
2520 sin6 = (struct sockaddr_in6 *)addr;
2521 sin_c6 = (struct sockaddr_in6 *)
2523 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
2524 &sin_c6->sin6_addr)) {
2526 * we are on the same
2539 sctp_add_remote_addr(struct sctp_tcb *stcb, struct sockaddr *newaddr,
2540 int set_scope, int from)
2543 * The following is redundant to the same lines in the
2544 * sctp_aloc_assoc() but is needed since other's call the add
2547 struct sctp_nets *net, *netfirst;
2551 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
2552 printf("Adding an address (from:%d) to the peer: ", from);
2553 sctp_print_address(newaddr);
2557 netfirst = sctp_findnet(stcb, newaddr);
2560 * Lie and return ok, we don't want to make the association
2561 * go away for this behavior. It will happen in the TCP
2562 * model in a connected socket. It does not reach the hash
2563 * table until after the association is built so it can't be
2564 * found. Mark as reachable, since the initial creation will
2565 * have been cleared and the NOT_IN_ASSOC flag will have
2566 * been added... and we don't want to end up removing it
2569 if (netfirst->dest_state & SCTP_ADDR_UNCONFIRMED) {
2570 netfirst->dest_state = (SCTP_ADDR_REACHABLE |
2571 SCTP_ADDR_UNCONFIRMED);
2573 netfirst->dest_state = SCTP_ADDR_REACHABLE;
2579 if (newaddr->sa_family == AF_INET) {
2580 struct sockaddr_in *sin;
2582 sin = (struct sockaddr_in *)newaddr;
2583 if (sin->sin_addr.s_addr == 0) {
2584 /* Invalid address */
2587 /* zero out the bzero area */
2588 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
2590 /* assure len is set */
2591 sin->sin_len = sizeof(struct sockaddr_in);
2593 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
2594 stcb->ipv4_local_scope = 1;
2596 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
2597 stcb->asoc.ipv4_local_scope = 1;
2599 #endif /* SCTP_DONT_DO_PRIVADDR_SCOPE */
2601 if (sctp_is_address_on_local_host(newaddr)) {
2602 stcb->asoc.loopback_scope = 1;
2603 stcb->asoc.ipv4_local_scope = 1;
2604 stcb->asoc.local_scope = 1;
2605 stcb->asoc.site_scope = 1;
2608 if (from == SCTP_ADDR_IS_CONFIRMED) {
2610 if (sctp_is_address_on_local_host(newaddr)) {
2611 stcb->asoc.loopback_scope = 1;
2612 stcb->asoc.ipv4_local_scope = 1;
2613 stcb->asoc.local_scope = 1;
2614 stcb->asoc.site_scope = 1;
2617 /* Validate the address is in scope */
2618 if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) &&
2619 (stcb->asoc.ipv4_local_scope == 0)) {
2623 } else if (newaddr->sa_family == AF_INET6) {
2624 struct sockaddr_in6 *sin6;
2626 sin6 = (struct sockaddr_in6 *)newaddr;
2627 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2628 /* Invalid address */
2631 /* assure len is set */
2632 sin6->sin6_len = sizeof(struct sockaddr_in6);
2634 if (sctp_is_address_on_local_host(newaddr)) {
2635 stcb->asoc.loopback_scope = 1;
2636 stcb->asoc.local_scope = 1;
2637 stcb->asoc.ipv4_local_scope = 1;
2638 stcb->asoc.site_scope = 1;
2639 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
2641 * If the new destination is a LINK_LOCAL we
2642 * must have common site scope. Don't set
2643 * the local scope since we may not share
2644 * all links, only loopback can do this.
2645 * Links on the local network would also be
2646 * on our private network for v4 too.
2648 stcb->asoc.ipv4_local_scope = 1;
2649 stcb->asoc.site_scope = 1;
2650 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
2652 * If the new destination is SITE_LOCAL then
2653 * we must have site scope in common.
2655 stcb->asoc.site_scope = 1;
2658 if (from == SCTP_ADDR_IS_CONFIRMED) {
2659 /* From connectx so we check for localhost. */
2660 if (sctp_is_address_on_local_host(newaddr)) {
2661 stcb->asoc.loopback_scope = 1;
2662 stcb->asoc.ipv4_local_scope = 1;
2663 stcb->asoc.local_scope = 1;
2664 stcb->asoc.site_scope = 1;
2667 /* Validate the address is in scope */
2668 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) &&
2669 (stcb->asoc.loopback_scope == 0)) {
2671 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) &&
2672 (stcb->asoc.local_scope == 0)) {
2674 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) &&
2675 (stcb->asoc.site_scope == 0)) {
2680 /* not supported family type */
2683 net = (struct sctp_nets *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_net);
2687 SCTP_INCR_RADDR_COUNT();
2688 bzero(net, sizeof(*net));
2689 memcpy(&net->ro._l_addr, newaddr, newaddr->sa_len);
2690 if (newaddr->sa_family == AF_INET) {
2691 ((struct sockaddr_in *)&net->ro._l_addr)->sin_port = stcb->rport;
2692 } else if (newaddr->sa_family == AF_INET6) {
2693 ((struct sockaddr_in6 *)&net->ro._l_addr)->sin6_port = stcb->rport;
2695 net->addr_is_local = sctp_is_address_on_local_host(newaddr);
2696 net->failure_threshold = stcb->asoc.def_net_failure;
2697 if (addr_inscope == 0) {
2698 net->dest_state = (SCTP_ADDR_REACHABLE |
2699 SCTP_ADDR_OUT_OF_SCOPE);
2701 if (from == SCTP_ADDR_IS_CONFIRMED)
2702 /* SCTP_ADDR_IS_CONFIRMED is passed by connect_x */
2703 net->dest_state = SCTP_ADDR_REACHABLE;
2705 net->dest_state = SCTP_ADDR_REACHABLE |
2706 SCTP_ADDR_UNCONFIRMED;
2708 net->RTO = stcb->asoc.initial_rto;
2709 stcb->asoc.numnets++;
2710 *(&net->ref_count) = 1;
2711 net->tos_flowlabel = 0;
2713 if (newaddr->sa_family == AF_INET)
2714 net->tos_flowlabel = stcb->asoc.default_tos;
2717 if (newaddr->sa_family == AF_INET6)
2718 net->tos_flowlabel = stcb->asoc.default_flowlabel;
2720 /* Init the timer structure */
2721 SCTP_OS_TIMER_INIT(&net->rxt_timer.timer);
2722 SCTP_OS_TIMER_INIT(&net->fr_timer.timer);
2723 SCTP_OS_TIMER_INIT(&net->pmtu_timer.timer);
2725 /* Now generate a route for this guy */
2726 /* KAME hack: embed scopeid */
2727 if (newaddr->sa_family == AF_INET6) {
2728 struct sockaddr_in6 *sin6;
2730 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
2731 (void)sa6_embedscope(sin6, ip6_use_defzone);
2732 sin6->sin6_scope_id = 0;
2734 rtalloc_ign((struct route *)&net->ro, 0UL);
2735 if (newaddr->sa_family == AF_INET6) {
2736 struct sockaddr_in6 *sin6;
2738 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
2739 (void)sa6_recoverscope(sin6);
2741 if ((net->ro.ro_rt) &&
2742 (net->ro.ro_rt->rt_ifp)) {
2743 net->mtu = net->ro.ro_rt->rt_ifp->if_mtu;
2744 if (from == SCTP_ALLOC_ASOC) {
2745 stcb->asoc.smallest_mtu = net->mtu;
2747 /* start things off to match mtu of interface please. */
2748 net->ro.ro_rt->rt_rmx.rmx_mtu = net->ro.ro_rt->rt_ifp->if_mtu;
2750 net->mtu = stcb->asoc.smallest_mtu;
2753 if (stcb->asoc.smallest_mtu > net->mtu) {
2754 stcb->asoc.smallest_mtu = net->mtu;
2757 * We take the max of the burst limit times a MTU or the
2758 * INITIAL_CWND. We then limit this to 4 MTU's of sending.
2760 net->cwnd = min((net->mtu * 4), max((2 * net->mtu), SCTP_INITIAL_CWND));
2762 /* we always get at LEAST 2 MTU's */
2763 if (net->cwnd < (2 * net->mtu)) {
2764 net->cwnd = 2 * net->mtu;
2766 net->ssthresh = stcb->asoc.peers_rwnd;
2768 #if defined(SCTP_CWND_MONITOR) || defined(SCTP_CWND_LOGGING)
2769 sctp_log_cwnd(stcb, net, 0, SCTP_CWND_INITIALIZATION);
2773 * CMT: CUC algo - set find_pseudo_cumack to TRUE (1) at beginning
2774 * of assoc (2005/06/27, iyengar@cis.udel.edu)
2776 net->find_pseudo_cumack = 1;
2777 net->find_rtx_pseudo_cumack = 1;
2778 net->src_addr_selected = 0;
2779 netfirst = TAILQ_FIRST(&stcb->asoc.nets);
2780 if (net->ro.ro_rt == NULL) {
2781 /* Since we have no route put it at the back */
2782 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
2783 } else if (netfirst == NULL) {
2784 /* We are the first one in the pool. */
2785 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
2786 } else if (netfirst->ro.ro_rt == NULL) {
2788 * First one has NO route. Place this one ahead of the first
2791 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
2792 } else if (net->ro.ro_rt->rt_ifp != netfirst->ro.ro_rt->rt_ifp) {
2794 * This one has a different interface than the one at the
2795 * top of the list. Place it ahead.
2797 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
2800 * Ok we have the same interface as the first one. Move
2801 * forward until we find either a) one with a NULL route...
2802 * insert ahead of that b) one with a different ifp.. insert
2803 * after that. c) end of the list.. insert at the tail.
2805 struct sctp_nets *netlook;
2808 netlook = TAILQ_NEXT(netfirst, sctp_next);
2809 if (netlook == NULL) {
2810 /* End of the list */
2811 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net,
2814 } else if (netlook->ro.ro_rt == NULL) {
2815 /* next one has NO route */
2816 TAILQ_INSERT_BEFORE(netfirst, net, sctp_next);
2818 } else if (netlook->ro.ro_rt->rt_ifp !=
2819 net->ro.ro_rt->rt_ifp) {
2820 TAILQ_INSERT_AFTER(&stcb->asoc.nets, netlook,
2826 } while (netlook != NULL);
2829 /* got to have a primary set */
2830 if (stcb->asoc.primary_destination == 0) {
2831 stcb->asoc.primary_destination = net;
2832 } else if ((stcb->asoc.primary_destination->ro.ro_rt == NULL) &&
2834 ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) {
2835 /* No route to current primary adopt new primary */
2836 stcb->asoc.primary_destination = net;
2838 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb,
2840 /* Validate primary is first */
2841 net = TAILQ_FIRST(&stcb->asoc.nets);
2842 if ((net != stcb->asoc.primary_destination) &&
2843 (stcb->asoc.primary_destination)) {
2845 * first one on the list is NOT the primary sctp_cmpaddr()
2846 * is much more efficent if the primary is the first on the
2849 TAILQ_REMOVE(&stcb->asoc.nets,
2850 stcb->asoc.primary_destination, sctp_next);
2851 TAILQ_INSERT_HEAD(&stcb->asoc.nets,
2852 stcb->asoc.primary_destination, sctp_next);
2859 * allocate an association and add it to the endpoint. The caller must be
2860 * careful to add all additional addresses once they are know right away or
2861 * else the assoc will be may experience a blackout scenario.
2864 sctp_aloc_assoc(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
2865 int for_a_init, int *error, uint32_t override_tag)
2867 struct sctp_tcb *stcb;
2868 struct sctp_association *asoc;
2869 struct sctpasochead *head;
2874 * Assumption made here: Caller has done a
2875 * sctp_findassociation_ep_addr(ep, addr's); to make sure the
2876 * address does not exist already.
2878 if (sctppcbinfo.ipi_count_asoc >= SCTP_MAX_NUM_OF_ASOC) {
2879 /* Hit max assoc, sorry no more */
2883 SCTP_INP_RLOCK(inp);
2884 if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
2886 * If its in the TCP pool, its NOT allowed to create an
2887 * association. The parent listener needs to call
2888 * sctp_aloc_assoc.. or the one-2-many socket. If a peeled
2889 * off, or connected one does this.. its an error.
2891 SCTP_INP_RUNLOCK(inp);
2896 if (sctp_debug_on & SCTP_DEBUG_PCB3) {
2897 printf("Allocate an association for peer:");
2899 sctp_print_address(firstaddr);
2903 ntohs(((struct sockaddr_in *)firstaddr)->sin_port));
2905 #endif /* SCTP_DEBUG */
2906 if (firstaddr->sa_family == AF_INET) {
2907 struct sockaddr_in *sin;
2909 sin = (struct sockaddr_in *)firstaddr;
2910 if ((sin->sin_port == 0) || (sin->sin_addr.s_addr == 0)) {
2911 /* Invalid address */
2912 SCTP_INP_RUNLOCK(inp);
2916 rport = sin->sin_port;
2917 } else if (firstaddr->sa_family == AF_INET6) {
2918 struct sockaddr_in6 *sin6;
2920 sin6 = (struct sockaddr_in6 *)firstaddr;
2921 if ((sin6->sin6_port == 0) ||
2922 (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))) {
2923 /* Invalid address */
2924 SCTP_INP_RUNLOCK(inp);
2928 rport = sin6->sin6_port;
2930 /* not supported family type */
2931 SCTP_INP_RUNLOCK(inp);
2935 SCTP_INP_RUNLOCK(inp);
2936 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
2938 * If you have not performed a bind, then we need to do the
2939 * ephemerial bind for you.
2941 if ((err = sctp_inpcb_bind(inp->sctp_socket,
2942 (struct sockaddr *)NULL,
2943 (struct thread *)NULL
2945 /* bind error, probably perm */
2950 stcb = (struct sctp_tcb *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_asoc);
2952 /* out of memory? */
2956 SCTP_INCR_ASOC_COUNT();
2958 bzero(stcb, sizeof(*stcb));
2960 SCTP_TCB_LOCK_INIT(stcb);
2961 SCTP_TCB_SEND_LOCK_INIT(stcb);
2962 /* setup back pointer's */
2963 stcb->sctp_ep = inp;
2964 stcb->sctp_socket = inp->sctp_socket;
2965 if ((err = sctp_init_asoc(inp, asoc, for_a_init, override_tag))) {
2967 SCTP_TCB_LOCK_DESTROY(stcb);
2968 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
2969 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
2970 SCTP_DECR_ASOC_COUNT();
2975 stcb->rport = rport;
2976 SCTP_INP_INFO_WLOCK();
2977 SCTP_INP_WLOCK(inp);
2978 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
2979 /* inpcb freed while alloc going on */
2980 SCTP_TCB_LOCK_DESTROY(stcb);
2981 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
2982 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
2983 SCTP_INP_WUNLOCK(inp);
2984 SCTP_INP_INFO_WUNLOCK();
2985 SCTP_DECR_ASOC_COUNT();
2989 SCTP_TCB_LOCK(stcb);
2991 /* now that my_vtag is set, add it to the hash */
2992 head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
2993 sctppcbinfo.hashasocmark)];
2994 /* put it in the bucket in the vtag hash of assoc's for the system */
2995 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
2996 SCTP_INP_INFO_WUNLOCK();
2998 if ((err = sctp_add_remote_addr(stcb, firstaddr, SCTP_DO_SETSCOPE, SCTP_ALLOC_ASOC))) {
2999 /* failure.. memory error? */
3001 SCTP_FREE(asoc->strmout);
3002 if (asoc->mapping_array)
3003 SCTP_FREE(asoc->mapping_array);
3005 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
3006 SCTP_DECR_ASOC_COUNT();
3007 SCTP_TCB_LOCK_DESTROY(stcb);
3008 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
3012 /* Init all the timers */
3013 SCTP_OS_TIMER_INIT(&asoc->hb_timer.timer);
3014 SCTP_OS_TIMER_INIT(&asoc->dack_timer.timer);
3015 SCTP_OS_TIMER_INIT(&asoc->strreset_timer.timer);
3016 SCTP_OS_TIMER_INIT(&asoc->asconf_timer.timer);
3017 SCTP_OS_TIMER_INIT(&asoc->shut_guard_timer.timer);
3018 SCTP_OS_TIMER_INIT(&asoc->autoclose_timer.timer);
3019 SCTP_OS_TIMER_INIT(&asoc->delayed_event_timer.timer);
3021 LIST_INSERT_HEAD(&inp->sctp_asoc_list, stcb, sctp_tcblist);
3022 /* now file the port under the hash as well */
3023 if (inp->sctp_tcbhash != NULL) {
3024 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(stcb->rport,
3025 inp->sctp_hashmark)];
3026 LIST_INSERT_HEAD(head, stcb, sctp_tcbhash);
3028 SCTP_INP_WUNLOCK(inp);
3030 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
3031 printf("Association %p now allocated\n", stcb);
3039 sctp_remove_net(struct sctp_tcb *stcb, struct sctp_nets *net)
3041 struct sctp_association *asoc;
3045 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
3046 sctp_free_remote_addr(net);
3047 if (net == asoc->primary_destination) {
3049 struct sctp_nets *lnet;
3051 lnet = TAILQ_FIRST(&asoc->nets);
3052 /* Try to find a confirmed primary */
3053 asoc->primary_destination = sctp_find_alternate_net(stcb, lnet,
3056 if (net == asoc->last_data_chunk_from) {
3058 asoc->last_data_chunk_from = TAILQ_FIRST(&asoc->nets);
3060 if (net == asoc->last_control_chunk_from) {
3062 asoc->last_control_chunk_from = NULL;
3064 /* if (net == asoc->asconf_last_sent_to) {*/
3066 /* asoc->asconf_last_sent_to = TAILQ_FIRST(&asoc->nets);*/
3071 * remove a remote endpoint address from an association, it will fail if the
3072 * address does not exist.
3075 sctp_del_remote_addr(struct sctp_tcb *stcb, struct sockaddr *remaddr)
3078 * Here we need to remove a remote address. This is quite simple, we
3079 * first find it in the list of address for the association
3080 * (tasoc->asoc.nets) and then if it is there, we do a LIST_REMOVE
3081 * on that item. Note we do not allow it to be removed if there are
3082 * no other addresses.
3084 struct sctp_association *asoc;
3085 struct sctp_nets *net, *net_tmp;
3089 /* locate the address */
3090 for (net = TAILQ_FIRST(&asoc->nets); net != NULL; net = net_tmp) {
3091 net_tmp = TAILQ_NEXT(net, sctp_next);
3092 if (net->ro._l_addr.sa.sa_family != remaddr->sa_family) {
3095 if (sctp_cmpaddr((struct sockaddr *)&net->ro._l_addr,
3097 /* we found the guy */
3098 if (asoc->numnets < 2) {
3099 /* Must have at LEAST two remote addresses */
3102 sctp_remove_net(stcb, net);
3113 sctp_add_vtag_to_timewait(struct sctp_inpcb *inp, uint32_t tag, uint32_t time)
3115 struct sctpvtaghead *chain;
3116 struct sctp_tagblock *twait_block;
3120 SCTP_GETTIME_TIMEVAL(&now);
3121 chain = &sctppcbinfo.vtag_timewait[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
3123 if (!LIST_EMPTY(chain)) {
3124 /* Block(s) present, lets find space, and expire on the fly */
3125 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
3126 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
3127 if ((twait_block->vtag_block[i].v_tag == 0) &&
3129 twait_block->vtag_block[i].tv_sec_at_expire =
3131 twait_block->vtag_block[i].v_tag = tag;
3133 } else if ((twait_block->vtag_block[i].v_tag) &&
3134 ((long)twait_block->vtag_block[i].tv_sec_at_expire >
3136 /* Audit expires this guy */
3137 twait_block->vtag_block[i].tv_sec_at_expire = 0;
3138 twait_block->vtag_block[i].v_tag = 0;
3140 /* Reuse it for my new tag */
3141 twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec + SCTP_TIME_WAIT;
3142 twait_block->vtag_block[0].v_tag = tag;
3149 * We only do up to the block where we can
3150 * place our tag for audits
3156 /* Need to add a new block to chain */
3158 SCTP_MALLOC(twait_block, struct sctp_tagblock *,
3159 sizeof(struct sctp_tagblock), "TimeWait");
3160 if (twait_block == NULL) {
3163 memset(twait_block, 0, sizeof(struct sctp_tagblock));
3164 LIST_INSERT_HEAD(chain, twait_block, sctp_nxt_tagblock);
3165 twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec +
3167 twait_block->vtag_block[0].v_tag = tag;
3173 sctp_iterator_asoc_being_freed(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
3175 struct sctp_iterator *it;
3178 * Unlock the tcb lock we do this so we avoid a dead lock scenario
3179 * where the iterator is waiting on the TCB lock and the TCB lock is
3180 * waiting on the iterator lock.
3182 it = stcb->asoc.stcb_starting_point_for_iterator;
3186 if (it->inp != stcb->sctp_ep) {
3187 /* hmm, focused on the wrong one? */
3190 if (it->stcb != stcb) {
3193 it->stcb = LIST_NEXT(stcb, sctp_tcblist);
3194 if (it->stcb == NULL) {
3195 /* done with all asoc's in this assoc */
3196 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
3199 it->inp = LIST_NEXT(inp, sctp_list);
3205 * Free the association after un-hashing the remote port.
3208 sctp_free_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int from_inpcbfree, int from_location)
3211 struct sctp_association *asoc;
3212 struct sctp_nets *net, *prev;
3213 struct sctp_laddr *laddr;
3214 struct sctp_tmit_chunk *chk;
3215 struct sctp_asconf_addr *aparam;
3216 struct sctp_stream_reset_list *liste;
3217 struct sctp_queued_to_read *sq;
3218 struct sctp_stream_queue_pending *sp;
3219 sctp_sharedkey_t *shared_key;
3224 /* first, lets purge the entry from the hash table. */
3227 #ifdef SCTP_LOG_CLOSING
3228 sctp_log_closing(inp, stcb, 6);
3230 if (stcb->asoc.state == 0) {
3231 #ifdef SCTP_LOG_CLOSING
3232 sctp_log_closing(inp, NULL, 7);
3235 /* there is no asoc, really TSNH :-0 */
3239 if (stcb->freed_from_where == 0) {
3240 /* Only record the first place free happened from */
3241 stcb->freed_from_where = from_location;
3246 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3247 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
3248 /* nothing around */
3251 so = inp->sctp_socket;
3254 * We used timer based freeing if a reader or writer is in the way.
3255 * So we first check if we are actually being called from a timer,
3256 * if so we abort early if a reader or writer is still in the way.
3258 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) &&
3259 (from_inpcbfree == SCTP_NORMAL_PROC)) {
3261 * is it the timer driving us? if so are the reader/writers
3264 if (stcb->asoc.refcnt) {
3265 /* nope, reader or writer in the way */
3266 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
3267 /* no asoc destroyed */
3268 SCTP_TCB_UNLOCK(stcb);
3270 #ifdef SCTP_LOG_CLOSING
3271 sctp_log_closing(inp, stcb, 8);
3276 /* now clean up any other timers */
3277 SCTP_OS_TIMER_STOP(&asoc->hb_timer.timer);
3278 SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
3279 SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
3280 SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
3281 SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
3282 SCTP_OS_TIMER_STOP(&asoc->shut_guard_timer.timer);
3283 SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
3285 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3286 SCTP_OS_TIMER_STOP(&net->fr_timer.timer);
3287 SCTP_OS_TIMER_STOP(&net->rxt_timer.timer);
3288 SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
3290 /* Now the read queue needs to be cleaned up (only once) */
3292 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0) {
3293 SCTP_INP_READ_LOCK(inp);
3294 TAILQ_FOREACH(sq, &inp->read_queue, next) {
3295 if (sq->stcb == stcb) {
3296 sq->do_not_ref_stcb = 1;
3297 sq->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
3299 * If there is no end, there never will be
3302 if (sq->end_added == 0) {
3303 /* Held for PD-API clear that. */
3304 sq->pdapi_aborted = 1;
3305 sq->held_length = 0;
3306 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT)) {
3308 * Need to add a PD-API
3309 * aborted indication.
3310 * Setting the control_pdapi
3311 * assures that it will be
3312 * added right after this
3315 stcb->asoc.control_pdapi = sq;
3316 sctp_notify_partial_delivery_indication(stcb,
3317 SCTP_PARTIAL_DELIVERY_ABORTED, 1);
3318 stcb->asoc.control_pdapi = NULL;
3321 /* Add an end to wake them */
3326 SCTP_INP_READ_UNLOCK(inp);
3327 if (stcb->block_entry) {
3329 stcb->block_entry->error = ECONNRESET;
3330 stcb->block_entry = NULL;
3333 stcb->asoc.state |= SCTP_STATE_ABOUT_TO_BE_FREED;
3334 if ((from_inpcbfree != SCTP_PCBFREE_FORCE) && (stcb->asoc.refcnt)) {
3336 * reader or writer in the way, we have hopefully given him
3337 * something to chew on above.
3339 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
3340 SCTP_TCB_UNLOCK(stcb);
3342 SCTP_INP_RLOCK(inp);
3343 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3344 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
3345 /* nothing around */
3348 /* Wake any reader/writers */
3349 sctp_sorwakeup(inp, so);
3350 sctp_sowwakeup(inp, so);
3352 SCTP_INP_RUNLOCK(inp);
3356 #ifdef SCTP_LOG_CLOSING
3357 sctp_log_closing(inp, stcb, 9);
3359 /* no asoc destroyed */
3362 #ifdef SCTP_LOG_CLOSING
3363 sctp_log_closing(inp, stcb, 10);
3366 * When I reach here, no others want to kill the assoc yet.. and I
3367 * own the lock. Now its possible an abort comes in when I do the
3368 * lock exchange below to grab all the locks to do the final take
3369 * out. to prevent this we increment the count, which will start a
3370 * timer and blow out above thus assuring us that we hold exclusive
3371 * killing of the asoc. Note that after getting back the TCB lock we
3372 * will go ahead and increment the counter back up and stop any
3373 * timer a passing stranger may have started :-S
3375 if (from_inpcbfree == SCTP_NORMAL_PROC) {
3376 atomic_add_int(&stcb->asoc.refcnt, 1);
3378 SCTP_TCB_UNLOCK(stcb);
3380 SCTP_ITERATOR_LOCK();
3381 SCTP_INP_INFO_WLOCK();
3382 SCTP_INP_WLOCK(inp);
3383 SCTP_TCB_LOCK(stcb);
3385 /* Double check the GONE flag */
3386 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3387 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
3388 /* nothing around */
3391 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3392 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3394 * For TCP type we need special handling when we are
3395 * connected. We also include the peel'ed off ones to.
3397 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3398 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
3399 inp->sctp_flags |= SCTP_PCB_FLAGS_WAS_CONNECTED;
3402 if (so->so_rcv.sb_cc == 0) {
3403 so->so_state &= ~(SS_ISCONNECTING |
3404 SS_ISDISCONNECTING |
3409 sctp_sowwakeup(inp, so);
3410 sctp_sorwakeup(inp, so);
3411 wakeup(&so->so_timeo);
3416 * Make it invalid too, that way if its about to run it will abort
3419 sctp_iterator_asoc_being_freed(inp, stcb);
3420 /* re-increment the lock */
3421 if (from_inpcbfree == SCTP_NORMAL_PROC) {
3422 atomic_add_int(&stcb->asoc.refcnt, -1);
3425 if (inp->sctp_tcbhash) {
3426 LIST_REMOVE(stcb, sctp_tcbhash);
3428 if (stcb->asoc.in_restart_hash) {
3429 LIST_REMOVE(stcb, sctp_tcbrestarhash);
3431 /* Now lets remove it from the list of ALL associations in the EP */
3432 LIST_REMOVE(stcb, sctp_tcblist);
3433 if (from_inpcbfree == SCTP_NORMAL_PROC) {
3434 SCTP_INP_INCR_REF(inp);
3435 SCTP_INP_WUNLOCK(inp);
3436 SCTP_ITERATOR_UNLOCK();
3438 /* pull from vtag hash */
3439 LIST_REMOVE(stcb, sctp_asocs);
3440 sctp_add_vtag_to_timewait(inp, asoc->my_vtag, SCTP_TIME_WAIT);
3444 * Now restop the timers to be sure - this is paranoia at is finest!
3446 SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
3447 SCTP_OS_TIMER_STOP(&asoc->hb_timer.timer);
3448 SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
3449 SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
3450 SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
3451 SCTP_OS_TIMER_STOP(&asoc->shut_guard_timer.timer);
3452 SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
3453 SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
3455 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3456 SCTP_OS_TIMER_STOP(&net->fr_timer.timer);
3457 SCTP_OS_TIMER_STOP(&net->rxt_timer.timer);
3458 SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
3461 asoc->strreset_timer.type = SCTP_TIMER_TYPE_NONE;
3464 * The chunk lists and such SHOULD be empty but we check them just
3467 /* anything on the wheel needs to be removed */
3468 for (i = 0; i < asoc->streamoutcnt; i++) {
3469 struct sctp_stream_out *outs;
3471 outs = &asoc->strmout[i];
3472 /* now clean up any chunks here */
3473 sp = TAILQ_FIRST(&outs->outqueue);
3475 TAILQ_REMOVE(&outs->outqueue, sp, next);
3477 sctp_m_freem(sp->data);
3479 sp->tail_mbuf = NULL;
3481 sctp_free_remote_addr(sp->net);
3482 sctp_free_spbufspace(stcb, asoc, sp);
3483 /* Free the zone stuff */
3484 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_strmoq, sp);
3485 SCTP_DECR_STRMOQ_COUNT();
3486 sp = TAILQ_FIRST(&outs->outqueue);
3490 while ((sp = TAILQ_FIRST(&asoc->free_strmoq)) != NULL) {
3491 TAILQ_REMOVE(&asoc->free_strmoq, sp, next);
3493 sctp_m_freem(sp->data);
3495 sp->tail_mbuf = NULL;
3497 /* Free the zone stuff */
3498 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_strmoq, sp);
3499 SCTP_DECR_STRMOQ_COUNT();
3500 atomic_add_int(&sctppcbinfo.ipi_free_strmoq, -1);
3503 while ((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) {
3504 TAILQ_REMOVE(&asoc->resetHead, liste, next_resp);
3508 sq = TAILQ_FIRST(&asoc->pending_reply_queue);
3510 TAILQ_REMOVE(&asoc->pending_reply_queue, sq, next);
3512 sctp_m_freem(sq->data);
3515 sctp_free_remote_addr(sq->whoFrom);
3518 /* Free the ctl entry */
3519 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_readq, sq);
3520 SCTP_DECR_READQ_COUNT();
3521 sq = TAILQ_FIRST(&asoc->pending_reply_queue);
3524 chk = TAILQ_FIRST(&asoc->free_chunks);
3526 TAILQ_REMOVE(&asoc->free_chunks, chk, sctp_next);
3528 sctp_m_freem(chk->data);
3532 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3533 SCTP_DECR_CHK_COUNT();
3534 atomic_subtract_int(&sctppcbinfo.ipi_free_chunks, 1);
3535 asoc->free_chunk_cnt--;
3536 chk = TAILQ_FIRST(&asoc->free_chunks);
3538 /* pending send queue SHOULD be empty */
3539 if (!TAILQ_EMPTY(&asoc->send_queue)) {
3540 chk = TAILQ_FIRST(&asoc->send_queue);
3542 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
3544 sctp_m_freem(chk->data);
3548 sctp_free_remote_addr(chk->whoTo);
3549 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3550 SCTP_DECR_CHK_COUNT();
3551 chk = TAILQ_FIRST(&asoc->send_queue);
3556 printf("Freed %d from send_queue\n", ccnt);
3560 /* sent queue SHOULD be empty */
3561 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3562 chk = TAILQ_FIRST(&asoc->sent_queue);
3564 TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
3566 sctp_m_freem(chk->data);
3570 sctp_free_remote_addr(chk->whoTo);
3571 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3572 SCTP_DECR_CHK_COUNT();
3573 chk = TAILQ_FIRST(&asoc->sent_queue);
3578 printf("Freed %d from sent_queue\n", ccnt);
3582 /* control queue MAY not be empty */
3583 if (!TAILQ_EMPTY(&asoc->control_send_queue)) {
3584 chk = TAILQ_FIRST(&asoc->control_send_queue);
3586 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
3588 sctp_m_freem(chk->data);
3592 sctp_free_remote_addr(chk->whoTo);
3593 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3594 SCTP_DECR_CHK_COUNT();
3595 chk = TAILQ_FIRST(&asoc->control_send_queue);
3600 printf("Freed %d from ctrl_queue\n", ccnt);
3604 if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
3605 chk = TAILQ_FIRST(&asoc->reasmqueue);
3607 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
3609 sctp_m_freem(chk->data);
3612 sctp_free_remote_addr(chk->whoTo);
3614 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3615 SCTP_DECR_CHK_COUNT();
3616 chk = TAILQ_FIRST(&asoc->reasmqueue);
3621 printf("Freed %d from reasm_queue\n", ccnt);
3625 if (asoc->mapping_array) {
3626 SCTP_FREE(asoc->mapping_array);
3627 asoc->mapping_array = NULL;
3629 /* the stream outs */
3630 if (asoc->strmout) {
3631 SCTP_FREE(asoc->strmout);
3632 asoc->strmout = NULL;
3634 asoc->streamoutcnt = 0;
3636 struct sctp_queued_to_read *ctl;
3639 for (i = 0; i < asoc->streamincnt; i++) {
3640 if (!TAILQ_EMPTY(&asoc->strmin[i].inqueue)) {
3641 /* We have somethings on the streamin queue */
3642 ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue);
3644 TAILQ_REMOVE(&asoc->strmin[i].inqueue,
3646 sctp_free_remote_addr(ctl->whoFrom);
3648 sctp_m_freem(ctl->data);
3652 * We don't free the address here
3653 * since all the net's were freed
3656 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_readq, ctl);
3657 SCTP_DECR_READQ_COUNT();
3658 ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue);
3662 SCTP_FREE(asoc->strmin);
3663 asoc->strmin = NULL;
3665 asoc->streamincnt = 0;
3666 while (!TAILQ_EMPTY(&asoc->nets)) {
3667 net = TAILQ_FIRST(&asoc->nets);
3668 /* pull from list */
3669 if ((sctppcbinfo.ipi_count_raddr == 0) || (prev == net)) {
3671 panic("no net's left alloc'ed, or list points to itself");
3676 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
3677 sctp_free_remote_addr(net);
3680 /* local addresses, if any */
3681 while (!LIST_EMPTY(&asoc->sctp_local_addr_list)) {
3682 laddr = LIST_FIRST(&asoc->sctp_local_addr_list);
3683 LIST_REMOVE(laddr, sctp_nxt_addr);
3684 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_laddr, laddr);
3685 SCTP_DECR_LADDR_COUNT();
3687 /* pending asconf (address) parameters */
3688 while (!TAILQ_EMPTY(&asoc->asconf_queue)) {
3689 aparam = TAILQ_FIRST(&asoc->asconf_queue);
3690 TAILQ_REMOVE(&asoc->asconf_queue, aparam, next);
3693 if (asoc->last_asconf_ack_sent != NULL) {
3694 sctp_m_freem(asoc->last_asconf_ack_sent);
3695 asoc->last_asconf_ack_sent = NULL;
3697 /* clean up auth stuff */
3698 if (asoc->local_hmacs)
3699 sctp_free_hmaclist(asoc->local_hmacs);
3700 if (asoc->peer_hmacs)
3701 sctp_free_hmaclist(asoc->peer_hmacs);
3703 if (asoc->local_auth_chunks)
3704 sctp_free_chunklist(asoc->local_auth_chunks);
3705 if (asoc->peer_auth_chunks)
3706 sctp_free_chunklist(asoc->peer_auth_chunks);
3708 sctp_free_authinfo(&asoc->authinfo);
3710 shared_key = LIST_FIRST(&asoc->shared_keys);
3711 while (shared_key) {
3712 LIST_REMOVE(shared_key, next);
3713 sctp_free_sharedkey(shared_key);
3714 shared_key = LIST_FIRST(&asoc->shared_keys);
3717 /* Insert new items here :> */
3719 /* Get rid of LOCK */
3720 SCTP_TCB_LOCK_DESTROY(stcb);
3721 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
3722 if (from_inpcbfree == SCTP_NORMAL_PROC) {
3723 SCTP_INP_INFO_WUNLOCK();
3724 SCTP_INP_RLOCK(inp);
3726 #ifdef SCTP_TRACK_FREED_ASOCS
3727 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3728 /* now clean up the tasoc itself */
3729 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
3730 SCTP_DECR_ASOC_COUNT();
3732 LIST_INSERT_HEAD(&inp->sctp_asoc_free_list, stcb, sctp_tcblist);
3735 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
3736 SCTP_DECR_ASOC_COUNT();
3738 if (from_inpcbfree == SCTP_NORMAL_PROC) {
3739 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3741 * If its NOT the inp_free calling us AND sctp_close
3742 * as been called, we call back...
3744 SCTP_INP_RUNLOCK(inp);
3746 * This will start the kill timer (if we are the
3747 * lastone) since we hold an increment yet. But this
3748 * is the only safe way to do this since otherwise
3749 * if the socket closes at the same time we are here
3750 * we might collide in the cleanup.
3752 sctp_inpcb_free(inp, 0, 0);
3753 SCTP_INP_DECR_REF(inp);
3756 /* The socket is still open. */
3757 SCTP_INP_DECR_REF(inp);
3760 if (from_inpcbfree == SCTP_NORMAL_PROC) {
3761 SCTP_INP_RUNLOCK(inp);
3765 /* destroyed the asoc */
3766 #ifdef SCTP_LOG_CLOSING
3767 sctp_log_closing(inp, NULL, 11);
3775 * determine if a destination is "reachable" based upon the addresses bound
3776 * to the current endpoint (e.g. only v4 or v6 currently bound)
3779 * FIX: if we allow assoc-level bindx(), then this needs to be fixed to use
3780 * assoc level v4/v6 flags, as the assoc *may* not have the same address
3781 * types bound as its endpoint
3784 sctp_destination_is_reachable(struct sctp_tcb *stcb, struct sockaddr *destaddr)
3786 struct sctp_inpcb *inp;
3790 * No locks here, the TCB, in all cases is already locked and an
3791 * assoc is up. There is either a INP lock by the caller applied (in
3792 * asconf case when deleting an address) or NOT in the HB case,
3793 * however if HB then the INP increment is up and the INP will not
3794 * be removed (on top of the fact that we have a TCB lock). So we
3795 * only want to read the sctp_flags, which is either bound-all or
3796 * not.. no protection needed since once an assoc is up you can't be
3797 * changing your binding.
3799 inp = stcb->sctp_ep;
3800 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3801 /* if bound all, destination is not restricted */
3803 * RRS: Question during lock work: Is this correct? If you
3804 * are bound-all you still might need to obey the V4--V6
3805 * flags??? IMO this bound-all stuff needs to be removed!
3809 /* NOTE: all "scope" checks are done when local addresses are added */
3810 if (destaddr->sa_family == AF_INET6) {
3811 answer = inp->ip_inp.inp.inp_vflag & INP_IPV6;
3812 } else if (destaddr->sa_family == AF_INET) {
3813 answer = inp->ip_inp.inp.inp_vflag & INP_IPV4;
3815 /* invalid family, so it's unreachable */
3822 * update the inp_vflags on an endpoint
3825 sctp_update_ep_vflag(struct sctp_inpcb *inp)
3827 struct sctp_laddr *laddr;
3829 /* first clear the flag */
3830 inp->ip_inp.inp.inp_vflag = 0;
3831 /* set the flag based on addresses on the ep list */
3832 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3833 if (laddr->ifa == NULL) {
3835 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
3836 printf("An ounce of prevention is worth a pound of cure\n");
3838 #endif /* SCTP_DEBUG */
3841 if (laddr->ifa->ifa_addr) {
3844 if (laddr->ifa->ifa_addr->sa_family == AF_INET6) {
3845 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
3846 } else if (laddr->ifa->ifa_addr->sa_family == AF_INET) {
3847 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
3853 * Add the address to the endpoint local address list There is nothing to be
3854 * done if we are bound to all addresses
3857 sctp_add_local_addr_ep(struct sctp_inpcb *inp, struct ifaddr *ifa)
3859 struct sctp_laddr *laddr;
3864 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3865 /* You are already bound to all. You have it already */
3868 if (ifa->ifa_addr->sa_family == AF_INET6) {
3869 struct in6_ifaddr *ifa6;
3871 ifa6 = (struct in6_ifaddr *)ifa;
3872 if (ifa6->ia6_flags & (IN6_IFF_DETACHED |
3873 IN6_IFF_DEPRECATED | IN6_IFF_ANYCAST | IN6_IFF_NOTREADY))
3874 /* Can't bind a non-existent addr. */
3877 /* first, is it already present? */
3878 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3879 if (laddr->ifa == ifa) {
3885 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) && (fnd == 0)) {
3886 /* Not bound to all */
3887 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa);
3891 /* update inp_vflag flags */
3892 if (ifa->ifa_addr->sa_family == AF_INET6) {
3893 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
3894 } else if (ifa->ifa_addr->sa_family == AF_INET) {
3895 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
3903 * select a new (hopefully reachable) destination net (should only be used
3904 * when we deleted an ep addr that is the only usable source address to reach
3905 * the destination net)
3908 sctp_select_primary_destination(struct sctp_tcb *stcb)
3910 struct sctp_nets *net;
3912 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3913 /* for now, we'll just pick the first reachable one we find */
3914 if (net->dest_state & SCTP_ADDR_UNCONFIRMED)
3916 if (sctp_destination_is_reachable(stcb,
3917 (struct sockaddr *)&net->ro._l_addr)) {
3918 /* found a reachable destination */
3919 stcb->asoc.primary_destination = net;
3922 /* I can't there from here! ...we're gonna die shortly... */
3927 * Delete the address from the endpoint local address list There is nothing
3928 * to be done if we are bound to all addresses
3931 sctp_del_local_addr_ep(struct sctp_inpcb *inp, struct ifaddr *ifa)
3933 struct sctp_laddr *laddr;
3937 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3938 /* You are already bound to all. You have it already */
3941 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3942 if (laddr->ifa == ifa) {
3947 if (fnd && (inp->laddr_count < 2)) {
3948 /* can't delete unless there are at LEAST 2 addresses */
3951 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) && (fnd)) {
3953 * clean up any use of this address go through our
3954 * associations and clear any last_used_address that match
3955 * this one for each assoc, see if a new primary_destination
3958 struct sctp_tcb *stcb;
3960 /* clean up "next_addr_touse" */
3961 if (inp->next_addr_touse == laddr)
3962 /* delete this address */
3963 inp->next_addr_touse = NULL;
3965 /* clean up "last_used_address" */
3966 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
3967 if (stcb->asoc.last_used_address == laddr)
3968 /* delete this address */
3969 stcb->asoc.last_used_address = NULL;
3970 } /* for each tcb */
3972 /* remove it from the ep list */
3973 sctp_remove_laddr(laddr);
3975 /* update inp_vflag flags */
3976 sctp_update_ep_vflag(inp);
3977 /* select a new primary destination if needed */
3978 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
3980 * presume caller (sctp_asconf.c) already owns INP
3983 SCTP_TCB_LOCK(stcb);
3984 if (sctp_destination_is_reachable(stcb,
3985 (struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr) == 0) {
3986 sctp_select_primary_destination(stcb);
3988 SCTP_TCB_UNLOCK(stcb);
3989 } /* for each tcb */
3995 * Add the addr to the TCB local address list For the BOUNDALL or dynamic
3996 * case, this is a "pending" address list (eg. addresses waiting for an
3997 * ASCONF-ACK response) For the subset binding, static case, this is a
3998 * "valid" address list
4001 sctp_add_local_addr_assoc(struct sctp_tcb *stcb, struct ifaddr *ifa)
4003 struct sctp_inpcb *inp;
4004 struct sctp_laddr *laddr;
4008 * Assumes TCP is locked.. and possiblye the INP. May need to
4009 * confirm/fix that if we need it and is not the case.
4011 inp = stcb->sctp_ep;
4012 if (ifa->ifa_addr->sa_family == AF_INET6) {
4013 struct in6_ifaddr *ifa6;
4015 ifa6 = (struct in6_ifaddr *)ifa;
4016 if (ifa6->ia6_flags & (IN6_IFF_DETACHED |
4017 /* IN6_IFF_DEPRECATED | */
4020 /* Can't bind a non-existent addr. */
4023 /* does the address already exist? */
4024 LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list, sctp_nxt_addr) {
4025 if (laddr->ifa == ifa) {
4030 /* add to the list */
4031 error = sctp_insert_laddr(&stcb->asoc.sctp_local_addr_list, ifa);
4038 * insert an laddr entry with the given ifa for the desired list
4041 sctp_insert_laddr(struct sctpladdr *list, struct ifaddr *ifa)
4043 struct sctp_laddr *laddr;
4048 laddr = (struct sctp_laddr *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_laddr);
4049 if (laddr == NULL) {
4050 /* out of memory? */
4054 SCTP_INCR_LADDR_COUNT();
4055 bzero(laddr, sizeof(*laddr));
4058 LIST_INSERT_HEAD(list, laddr, sctp_nxt_addr);
4065 * Remove an laddr entry from the local address list (on an assoc)
4068 sctp_remove_laddr(struct sctp_laddr *laddr)
4073 /* remove from the list */
4074 LIST_REMOVE(laddr, sctp_nxt_addr);
4075 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_laddr, laddr);
4076 SCTP_DECR_LADDR_COUNT();
4081 * Remove an address from the TCB local address list
4084 sctp_del_local_addr_assoc(struct sctp_tcb *stcb, struct ifaddr *ifa)
4086 struct sctp_inpcb *inp;
4087 struct sctp_laddr *laddr;
4090 * This is called by asconf work. It is assumed that a) The TCB is
4091 * locked and b) The INP is locked. This is true in as much as I can
4092 * trace through the entry asconf code where I did these locks.
4093 * Again, the ASCONF code is a bit different in that it does lock
4094 * the INP during its work often times. This must be since we don't
4095 * want other proc's looking up things while what they are looking
4096 * up is changing :-D
4099 inp = stcb->sctp_ep;
4100 /* if subset bound and don't allow ASCONF's, can't delete last */
4101 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) &&
4102 (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF) == 0)) {
4103 if (stcb->asoc.numnets < 2) {
4104 /* can't delete last address */
4108 LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list, sctp_nxt_addr) {
4109 /* remove the address if it exists */
4110 if (laddr->ifa == NULL)
4112 if (laddr->ifa == ifa) {
4113 sctp_remove_laddr(laddr);
4118 /* address not found! */
4123 * Remove an address from the TCB local address list lookup using a sockaddr
4127 sctp_del_local_addr_assoc_sa(struct sctp_tcb *stcb, struct sockaddr *sa)
4129 struct sctp_inpcb *inp;
4130 struct sctp_laddr *laddr;
4131 struct sockaddr *l_sa;
4134 * This function I find does not seem to have a caller. As such we
4135 * NEED TO DELETE this code. If we do find a caller, the caller MUST
4136 * have locked the TCB at the least and probably the INP as well.
4138 inp = stcb->sctp_ep;
4139 /* if subset bound and don't allow ASCONF's, can't delete last */
4140 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) &&
4141 (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF) == 0)) {
4142 if (stcb->asoc.numnets < 2) {
4143 /* can't delete last address */
4147 LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list, sctp_nxt_addr) {
4148 /* make sure the address exists */
4149 if (laddr->ifa == NULL)
4151 if (laddr->ifa->ifa_addr == NULL)
4154 l_sa = laddr->ifa->ifa_addr;
4155 if (l_sa->sa_family == AF_INET6) {
4157 struct sockaddr_in6 *sin1, *sin2;
4159 sin1 = (struct sockaddr_in6 *)l_sa;
4160 sin2 = (struct sockaddr_in6 *)sa;
4161 if (memcmp(&sin1->sin6_addr, &sin2->sin6_addr,
4162 sizeof(struct in6_addr)) == 0) {
4164 sctp_remove_laddr(laddr);
4167 } else if (l_sa->sa_family == AF_INET) {
4169 struct sockaddr_in *sin1, *sin2;
4171 sin1 = (struct sockaddr_in *)l_sa;
4172 sin2 = (struct sockaddr_in *)sa;
4173 if (sin1->sin_addr.s_addr == sin2->sin_addr.s_addr) {
4175 sctp_remove_laddr(laddr);
4179 /* invalid family */
4183 /* address not found! */
4187 static char sctp_pcb_initialized = 0;
4190 * Temporarily remove for __APPLE__ until we use the Tiger equivalents
4193 static int sctp_max_number_of_assoc = SCTP_MAX_NUM_OF_ASOC;
4194 static int sctp_scale_up_for_address = SCTP_SCALE_FOR_ADDR;
4200 * SCTP initialization for the PCB structures should be called by
4201 * the sctp_init() funciton.
4205 if (sctp_pcb_initialized != 0) {
4206 /* error I was called twice */
4209 sctp_pcb_initialized = 1;
4211 bzero(&sctpstat, sizeof(struct sctpstat));
4213 /* init the empty list of (All) Endpoints */
4214 LIST_INIT(&sctppcbinfo.listhead);
4216 /* init the iterator head */
4217 LIST_INIT(&sctppcbinfo.iteratorhead);
4219 /* init the hash table of endpoints */
4220 TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", &sctp_hashtblsize);
4221 TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", &sctp_pcbtblsize);
4222 TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", &sctp_chunkscale);
4223 sctppcbinfo.sctp_asochash = SCTP_HASH_INIT((sctp_hashtblsize * 31),
4224 &sctppcbinfo.hashasocmark);
4225 sctppcbinfo.sctp_ephash = SCTP_HASH_INIT(sctp_hashtblsize,
4226 &sctppcbinfo.hashmark);
4227 sctppcbinfo.sctp_tcpephash = SCTP_HASH_INIT(sctp_hashtblsize,
4228 &sctppcbinfo.hashtcpmark);
4229 sctppcbinfo.hashtblsize = sctp_hashtblsize;
4231 /* init the small hash table we use to track restarted asoc's */
4232 sctppcbinfo.sctp_restarthash = SCTP_HASH_INIT(SCTP_STACK_VTAG_HASH_SIZE,
4233 &sctppcbinfo.hashrestartmark);
4235 /* init the zones */
4237 * FIX ME: Should check for NULL returns, but if it does fail we are
4238 * doomed to panic anyways... add later maybe.
4240 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_ep, "sctp_ep",
4241 sizeof(struct sctp_inpcb), maxsockets);
4243 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_asoc, "sctp_asoc",
4244 sizeof(struct sctp_tcb), sctp_max_number_of_assoc);
4246 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_laddr, "sctp_laddr",
4247 sizeof(struct sctp_laddr),
4248 (sctp_max_number_of_assoc * sctp_scale_up_for_address));
4250 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_net, "sctp_raddr",
4251 sizeof(struct sctp_nets),
4252 (sctp_max_number_of_assoc * sctp_scale_up_for_address));
4254 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_chunk, "sctp_chunk",
4255 sizeof(struct sctp_tmit_chunk),
4256 (sctp_max_number_of_assoc * sctp_chunkscale));
4258 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_readq, "sctp_readq",
4259 sizeof(struct sctp_queued_to_read),
4260 (sctp_max_number_of_assoc * sctp_chunkscale));
4262 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_strmoq, "sctp_stream_msg_out",
4263 sizeof(struct sctp_stream_queue_pending),
4264 (sctp_max_number_of_assoc * sctp_chunkscale));
4266 /* Master Lock INIT for info structure */
4267 SCTP_INP_INFO_LOCK_INIT();
4268 SCTP_STATLOG_INIT_LOCK();
4269 SCTP_ITERATOR_LOCK_INIT();
4271 SCTP_IPI_COUNT_INIT();
4272 SCTP_IPI_ADDR_INIT();
4273 LIST_INIT(&sctppcbinfo.addr_wq);
4275 /* not sure if we need all the counts */
4276 sctppcbinfo.ipi_count_ep = 0;
4277 /* assoc/tcb zone info */
4278 sctppcbinfo.ipi_count_asoc = 0;
4279 /* local addrlist zone info */
4280 sctppcbinfo.ipi_count_laddr = 0;
4281 /* remote addrlist zone info */
4282 sctppcbinfo.ipi_count_raddr = 0;
4284 sctppcbinfo.ipi_count_chunk = 0;
4286 /* socket queue zone info */
4287 sctppcbinfo.ipi_count_readq = 0;
4289 /* stream out queue cont */
4290 sctppcbinfo.ipi_count_strmoq = 0;
4292 sctppcbinfo.ipi_free_strmoq = 0;
4293 sctppcbinfo.ipi_free_chunks = 0;
4295 SCTP_OS_TIMER_INIT(&sctppcbinfo.addr_wq_timer.timer);
4297 /* Init the TIMEWAIT list */
4298 for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) {
4299 LIST_INIT(&sctppcbinfo.vtag_timewait[i]);
4306 sctp_load_addresses_from_init(struct sctp_tcb *stcb, struct mbuf *m,
4307 int iphlen, int offset, int limit, struct sctphdr *sh,
4308 struct sockaddr *altsa)
4311 * grub through the INIT pulling addresses and loading them to the
4312 * nets structure in the asoc. The from address in the mbuf should
4313 * also be loaded (if it is not already). This routine can be called
4314 * with either INIT or INIT-ACK's as long as the m points to the IP
4315 * packet and the offset points to the beginning of the parameters.
4317 struct sctp_inpcb *inp, *l_inp;
4318 struct sctp_nets *net, *net_tmp;
4320 struct sctp_paramhdr *phdr, parm_buf;
4321 struct sctp_tcb *stcb_tmp;
4322 uint16_t ptype, plen;
4323 struct sockaddr *sa;
4324 struct sockaddr_storage dest_store;
4325 struct sockaddr *local_sa = (struct sockaddr *)&dest_store;
4326 struct sockaddr_in sin;
4327 struct sockaddr_in6 sin6;
4329 struct sctp_auth_random *random = NULL;
4330 uint16_t random_len = 0;
4331 struct sctp_auth_hmac_algo *hmacs = NULL;
4332 uint16_t hmacs_len = 0;
4333 struct sctp_auth_chunk_list *chunks = NULL;
4334 uint16_t num_chunks = 0;
4335 sctp_key_t *new_key;
4337 int got_random = 0, got_hmacs = 0, got_chklist = 0;
4339 /* First get the destination address setup too. */
4340 memset(&sin, 0, sizeof(sin));
4341 memset(&sin6, 0, sizeof(sin6));
4343 sin.sin_family = AF_INET;
4344 sin.sin_len = sizeof(sin);
4345 sin.sin_port = stcb->rport;
4347 sin6.sin6_family = AF_INET6;
4348 sin6.sin6_len = sizeof(struct sockaddr_in6);
4349 sin6.sin6_port = stcb->rport;
4350 if (altsa == NULL) {
4351 iph = mtod(m, struct ip *);
4352 if (iph->ip_v == IPVERSION) {
4354 struct sockaddr_in *sin_2;
4356 sin_2 = (struct sockaddr_in *)(local_sa);
4357 memset(sin_2, 0, sizeof(sin));
4358 sin_2->sin_family = AF_INET;
4359 sin_2->sin_len = sizeof(sin);
4360 sin_2->sin_port = sh->dest_port;
4361 sin_2->sin_addr.s_addr = iph->ip_dst.s_addr;
4362 sin.sin_addr = iph->ip_src;
4363 sa = (struct sockaddr *)&sin;
4364 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
4366 struct ip6_hdr *ip6;
4367 struct sockaddr_in6 *sin6_2;
4369 ip6 = mtod(m, struct ip6_hdr *);
4370 sin6_2 = (struct sockaddr_in6 *)(local_sa);
4371 memset(sin6_2, 0, sizeof(sin6));
4372 sin6_2->sin6_family = AF_INET6;
4373 sin6_2->sin6_len = sizeof(struct sockaddr_in6);
4374 sin6_2->sin6_port = sh->dest_port;
4375 sin6.sin6_addr = ip6->ip6_src;
4376 sa = (struct sockaddr *)&sin6;
4382 * For cookies we use the src address NOT from the packet
4383 * but from the original INIT
4387 /* Turn off ECN until we get through all params */
4388 stcb->asoc.ecn_allowed = 0;
4389 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4390 /* mark all addresses that we have currently on the list */
4391 net->dest_state |= SCTP_ADDR_NOT_IN_ASSOC;
4393 /* does the source address already exist? if so skip it */
4394 l_inp = inp = stcb->sctp_ep;
4396 atomic_add_int(&stcb->asoc.refcnt, 1);
4397 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net_tmp, local_sa, stcb);
4398 atomic_add_int(&stcb->asoc.refcnt, -1);
4400 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || inp == NULL) {
4401 /* we must add the source address */
4402 /* no scope set here since we have a tcb already. */
4403 if ((sa->sa_family == AF_INET) &&
4404 (stcb->asoc.ipv4_addr_legal)) {
4405 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_2)) {
4408 } else if ((sa->sa_family == AF_INET6) &&
4409 (stcb->asoc.ipv6_addr_legal)) {
4410 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) {
4415 if (net_tmp != NULL && stcb_tmp == stcb) {
4416 net_tmp->dest_state &= ~SCTP_ADDR_NOT_IN_ASSOC;
4417 } else if (stcb_tmp != stcb) {
4418 /* It belongs to another association? */
4419 SCTP_TCB_UNLOCK(stcb_tmp);
4423 if (stcb->asoc.state == 0) {
4424 /* the assoc was freed? */
4427 /* now we must go through each of the params. */
4428 phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
4430 ptype = ntohs(phdr->param_type);
4431 plen = ntohs(phdr->param_length);
4433 * printf("ptype => %0x, plen => %d\n", (uint32_t)ptype,
4436 if (offset + plen > limit) {
4442 if (ptype == SCTP_IPV4_ADDRESS) {
4443 if (stcb->asoc.ipv4_addr_legal) {
4444 struct sctp_ipv4addr_param *p4, p4_buf;
4446 /* ok get the v4 address and check/add */
4447 phdr = sctp_get_next_param(m, offset,
4448 (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
4449 if (plen != sizeof(struct sctp_ipv4addr_param) ||
4453 p4 = (struct sctp_ipv4addr_param *)phdr;
4454 sin.sin_addr.s_addr = p4->addr;
4455 sa = (struct sockaddr *)&sin;
4456 inp = stcb->sctp_ep;
4457 atomic_add_int(&stcb->asoc.refcnt, 1);
4458 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
4460 atomic_add_int(&stcb->asoc.refcnt, -1);
4462 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) ||
4464 /* we must add the source address */
4466 * no scope set since we have a tcb
4471 * we must validate the state again
4474 if (stcb->asoc.state == 0) {
4475 /* the assoc was freed? */
4478 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_4)) {
4481 } else if (stcb_tmp == stcb) {
4482 if (stcb->asoc.state == 0) {
4483 /* the assoc was freed? */
4489 ~SCTP_ADDR_NOT_IN_ASSOC;
4493 * strange, address is in another
4494 * assoc? straighten out locks.
4496 if (stcb->asoc.state == 0) {
4497 /* the assoc was freed? */
4503 } else if (ptype == SCTP_IPV6_ADDRESS) {
4504 if (stcb->asoc.ipv6_addr_legal) {
4505 /* ok get the v6 address and check/add */
4506 struct sctp_ipv6addr_param *p6, p6_buf;
4508 phdr = sctp_get_next_param(m, offset,
4509 (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
4510 if (plen != sizeof(struct sctp_ipv6addr_param) ||
4514 p6 = (struct sctp_ipv6addr_param *)phdr;
4515 memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
4517 sa = (struct sockaddr *)&sin6;
4518 inp = stcb->sctp_ep;
4519 atomic_add_int(&stcb->asoc.refcnt, 1);
4520 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
4522 atomic_add_int(&stcb->asoc.refcnt, -1);
4523 if (stcb_tmp == NULL && (inp == stcb->sctp_ep ||
4526 * we must validate the state again
4529 if (stcb->asoc.state == 0) {
4530 /* the assoc was freed? */
4534 * we must add the address, no scope
4537 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_5)) {
4540 } else if (stcb_tmp == stcb) {
4542 * we must validate the state again
4545 if (stcb->asoc.state == 0) {
4546 /* the assoc was freed? */
4552 ~SCTP_ADDR_NOT_IN_ASSOC;
4556 * strange, address is in another
4557 * assoc? straighten out locks.
4559 if (stcb->asoc.state == 0) {
4560 /* the assoc was freed? */
4566 } else if (ptype == SCTP_ECN_CAPABLE) {
4567 stcb->asoc.ecn_allowed = 1;
4568 } else if (ptype == SCTP_ULP_ADAPTATION) {
4569 if (stcb->asoc.state != SCTP_STATE_OPEN) {
4570 struct sctp_adaptation_layer_indication ai,
4573 phdr = sctp_get_next_param(m, offset,
4574 (struct sctp_paramhdr *)&ai, sizeof(ai));
4575 aip = (struct sctp_adaptation_layer_indication *)phdr;
4576 sctp_ulp_notify(SCTP_NOTIFY_ADAPTATION_INDICATION,
4577 stcb, ntohl(aip->indication), NULL);
4579 } else if (ptype == SCTP_SET_PRIM_ADDR) {
4580 struct sctp_asconf_addr_param lstore, *fee;
4581 struct sctp_asconf_addrv4_param *fii;
4583 struct sockaddr *lsa = NULL;
4585 stcb->asoc.peer_supports_asconf = 1;
4586 if (plen > sizeof(lstore)) {
4589 phdr = sctp_get_next_param(m, offset,
4590 (struct sctp_paramhdr *)&lstore, plen);
4594 fee = (struct sctp_asconf_addr_param *)phdr;
4595 lptype = ntohs(fee->addrp.ph.param_type);
4596 if (lptype == SCTP_IPV4_ADDRESS) {
4598 sizeof(struct sctp_asconf_addrv4_param)) {
4599 printf("Sizeof setprim in init/init ack not %d but %d - ignored\n",
4600 (int)sizeof(struct sctp_asconf_addrv4_param),
4603 fii = (struct sctp_asconf_addrv4_param *)fee;
4604 sin.sin_addr.s_addr = fii->addrp.addr;
4605 lsa = (struct sockaddr *)&sin;
4607 } else if (lptype == SCTP_IPV6_ADDRESS) {
4609 sizeof(struct sctp_asconf_addr_param)) {
4610 printf("Sizeof setprim (v6) in init/init ack not %d but %d - ignored\n",
4611 (int)sizeof(struct sctp_asconf_addr_param),
4614 memcpy(sin6.sin6_addr.s6_addr,
4616 sizeof(fee->addrp.addr));
4617 lsa = (struct sockaddr *)&sin6;
4621 sctp_set_primary_addr(stcb, sa, NULL);
4623 } else if (ptype == SCTP_PRSCTP_SUPPORTED) {
4624 /* Peer supports pr-sctp */
4625 stcb->asoc.peer_supports_prsctp = 1;
4626 } else if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
4627 /* A supported extension chunk */
4628 struct sctp_supported_chunk_types_param *pr_supported;
4629 uint8_t local_store[128];
4632 phdr = sctp_get_next_param(m, offset,
4633 (struct sctp_paramhdr *)&local_store, plen);
4637 stcb->asoc.peer_supports_asconf = 0;
4638 stcb->asoc.peer_supports_prsctp = 0;
4639 stcb->asoc.peer_supports_pktdrop = 0;
4640 stcb->asoc.peer_supports_strreset = 0;
4641 stcb->asoc.peer_supports_auth = 0;
4642 pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
4643 num_ent = plen - sizeof(struct sctp_paramhdr);
4644 for (i = 0; i < num_ent; i++) {
4645 switch (pr_supported->chunk_types[i]) {
4647 case SCTP_ASCONF_ACK:
4648 stcb->asoc.peer_supports_asconf = 1;
4650 case SCTP_FORWARD_CUM_TSN:
4651 stcb->asoc.peer_supports_prsctp = 1;
4653 case SCTP_PACKET_DROPPED:
4654 stcb->asoc.peer_supports_pktdrop = 1;
4656 case SCTP_STREAM_RESET:
4657 stcb->asoc.peer_supports_strreset = 1;
4659 case SCTP_AUTHENTICATION:
4660 stcb->asoc.peer_supports_auth = 1;
4663 /* one I have not learned yet */
4668 } else if (ptype == SCTP_ECN_NONCE_SUPPORTED) {
4669 /* Peer supports ECN-nonce */
4670 stcb->asoc.peer_supports_ecn_nonce = 1;
4671 stcb->asoc.ecn_nonce_allowed = 1;
4672 } else if (ptype == SCTP_RANDOM) {
4673 if (plen > sizeof(store))
4676 /* already processed a RANDOM */
4679 phdr = sctp_get_next_param(m, offset,
4680 (struct sctp_paramhdr *)store,
4684 random = (struct sctp_auth_random *)phdr;
4685 random_len = plen - sizeof(*random);
4686 /* enforce the random length */
4687 if (random_len != SCTP_AUTH_RANDOM_SIZE_REQUIRED) {
4689 if (sctp_debug_on & SCTP_DEBUG_AUTH1)
4690 printf("SCTP: invalid RANDOM len\n");
4695 } else if (ptype == SCTP_HMAC_LIST) {
4699 if (plen > sizeof(store))
4702 /* already processed a HMAC list */
4705 phdr = sctp_get_next_param(m, offset,
4706 (struct sctp_paramhdr *)store,
4710 hmacs = (struct sctp_auth_hmac_algo *)phdr;
4711 hmacs_len = plen - sizeof(*hmacs);
4712 num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]);
4713 /* validate the hmac list */
4714 if (sctp_verify_hmac_param(hmacs, num_hmacs)) {
4717 if (stcb->asoc.peer_hmacs != NULL)
4718 sctp_free_hmaclist(stcb->asoc.peer_hmacs);
4719 stcb->asoc.peer_hmacs = sctp_alloc_hmaclist(num_hmacs);
4720 if (stcb->asoc.peer_hmacs != NULL) {
4721 for (i = 0; i < num_hmacs; i++) {
4722 sctp_auth_add_hmacid(stcb->asoc.peer_hmacs,
4723 ntohs(hmacs->hmac_ids[i]));
4727 } else if (ptype == SCTP_CHUNK_LIST) {
4730 if (plen > sizeof(store))
4733 /* already processed a Chunks list */
4736 phdr = sctp_get_next_param(m, offset,
4737 (struct sctp_paramhdr *)store,
4741 chunks = (struct sctp_auth_chunk_list *)phdr;
4742 num_chunks = plen - sizeof(*chunks);
4743 if (stcb->asoc.peer_auth_chunks != NULL)
4744 sctp_clear_chunklist(stcb->asoc.peer_auth_chunks);
4746 stcb->asoc.peer_auth_chunks = sctp_alloc_chunklist();
4747 for (i = 0; i < num_chunks; i++) {
4748 sctp_auth_add_chunk(chunks->chunk_types[i],
4749 stcb->asoc.peer_auth_chunks);
4752 } else if ((ptype == SCTP_HEARTBEAT_INFO) ||
4753 (ptype == SCTP_STATE_COOKIE) ||
4754 (ptype == SCTP_UNRECOG_PARAM) ||
4755 (ptype == SCTP_COOKIE_PRESERVE) ||
4756 (ptype == SCTP_SUPPORTED_ADDRTYPE) ||
4757 (ptype == SCTP_ADD_IP_ADDRESS) ||
4758 (ptype == SCTP_DEL_IP_ADDRESS) ||
4759 (ptype == SCTP_ERROR_CAUSE_IND) ||
4760 (ptype == SCTP_SUCCESS_REPORT)) {
4763 if ((ptype & 0x8000) == 0x0000) {
4765 * must stop processing the rest of the
4766 * param's. Any report bits were handled
4768 * sctp_arethere_unrecognized_parameters()
4769 * when the INIT or INIT-ACK was first seen.
4775 offset += SCTP_SIZE32(plen);
4776 if (offset >= limit) {
4779 phdr = sctp_get_next_param(m, offset, &parm_buf,
4782 /* Now check to see if we need to purge any addresses */
4783 for (net = TAILQ_FIRST(&stcb->asoc.nets); net != NULL; net = net_tmp) {
4784 net_tmp = TAILQ_NEXT(net, sctp_next);
4785 if ((net->dest_state & SCTP_ADDR_NOT_IN_ASSOC) ==
4786 SCTP_ADDR_NOT_IN_ASSOC) {
4787 /* This address has been removed from the asoc */
4788 /* remove and free it */
4789 stcb->asoc.numnets--;
4790 TAILQ_REMOVE(&stcb->asoc.nets, net, sctp_next);
4791 sctp_free_remote_addr(net);
4792 if (net == stcb->asoc.primary_destination) {
4793 stcb->asoc.primary_destination = NULL;
4794 sctp_select_primary_destination(stcb);
4798 /* validate authentication required parameters */
4799 if (got_random && got_hmacs) {
4800 stcb->asoc.peer_supports_auth = 1;
4802 stcb->asoc.peer_supports_auth = 0;
4804 if (!sctp_asconf_auth_nochk && stcb->asoc.peer_supports_asconf &&
4805 !stcb->asoc.peer_supports_auth) {
4808 /* concatenate the full random key */
4809 keylen = random_len + num_chunks + hmacs_len;
4810 new_key = sctp_alloc_key(keylen);
4811 if (new_key != NULL) {
4812 /* copy in the RANDOM */
4814 bcopy(random->random_data, new_key->key, random_len);
4815 /* append in the AUTH chunks */
4817 bcopy(chunks->chunk_types, new_key->key + random_len,
4819 /* append in the HMACs */
4821 bcopy(hmacs->hmac_ids, new_key->key + random_len + num_chunks,
4826 if (stcb->asoc.authinfo.peer_random != NULL)
4827 sctp_free_key(stcb->asoc.authinfo.peer_random);
4828 stcb->asoc.authinfo.peer_random = new_key;
4829 #ifdef SCTP_AUTH_DRAFT_04
4830 /* don't include the chunks and hmacs for draft -04 */
4831 stcb->asoc.authinfo.peer_random->keylen = random_len;
4833 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid);
4834 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid);
4840 sctp_set_primary_addr(struct sctp_tcb *stcb, struct sockaddr *sa,
4841 struct sctp_nets *net)
4843 /* make sure the requested primary address exists in the assoc */
4844 if (net == NULL && sa)
4845 net = sctp_findnet(stcb, sa);
4848 /* didn't find the requested primary address! */
4851 /* set the primary address */
4852 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
4853 /* Must be confirmed */
4856 stcb->asoc.primary_destination = net;
4857 net->dest_state &= ~SCTP_ADDR_WAS_PRIMARY;
4858 net = TAILQ_FIRST(&stcb->asoc.nets);
4859 if (net != stcb->asoc.primary_destination) {
4861 * first one on the list is NOT the primary
4862 * sctp_cmpaddr() is much more efficent if the
4863 * primary is the first on the list, make it so.
4865 TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
4866 TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
4874 sctp_is_vtag_good(struct sctp_inpcb *inp, uint32_t tag, struct timeval *now)
4877 * This function serves two purposes. It will see if a TAG can be
4878 * re-used and return 1 for yes it is ok and 0 for don't use that
4879 * tag. A secondary function it will do is purge out old tags that
4882 struct sctpasochead *head;
4883 struct sctpvtaghead *chain;
4884 struct sctp_tagblock *twait_block;
4885 struct sctp_tcb *stcb;
4888 SCTP_INP_INFO_WLOCK();
4889 chain = &sctppcbinfo.vtag_timewait[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
4890 /* First is the vtag in use ? */
4892 head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(tag,
4893 sctppcbinfo.hashasocmark)];
4897 LIST_FOREACH(stcb, head, sctp_asocs) {
4899 if (stcb->asoc.my_vtag == tag) {
4901 * We should remove this if and return 0 always if
4902 * we want vtags unique across all endpoints. For
4903 * now within a endpoint is ok.
4905 if (inp == stcb->sctp_ep) {
4906 /* bad tag, in use */
4907 SCTP_INP_INFO_WUNLOCK();
4913 /* Now lets check the restart hash */
4914 head = &sctppcbinfo.sctp_restarthash[SCTP_PCBHASH_ASOC(tag,
4915 sctppcbinfo.hashrestartmark)];
4917 goto check_time_wait;
4919 LIST_FOREACH(stcb, head, sctp_tcbrestarhash) {
4920 if (stcb->asoc.assoc_id == tag) {
4922 if (inp == stcb->sctp_ep) {
4923 /* bad tag, in use */
4924 SCTP_INP_INFO_WUNLOCK();
4930 /* Now what about timed wait ? */
4931 if (!LIST_EMPTY(chain)) {
4933 * Block(s) are present, lets see if we have this tag in the
4936 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
4937 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
4938 if (twait_block->vtag_block[i].v_tag == 0) {
4941 } else if ((long)twait_block->vtag_block[i].tv_sec_at_expire >
4943 /* Audit expires this guy */
4944 twait_block->vtag_block[i].tv_sec_at_expire = 0;
4945 twait_block->vtag_block[i].v_tag = 0;
4946 } else if (twait_block->vtag_block[i].v_tag ==
4948 /* Bad tag, sorry :< */
4949 SCTP_INP_INFO_WUNLOCK();
4955 /* Not found, ok to use the tag */
4956 SCTP_INP_INFO_WUNLOCK();
4962 * Delete the address from the endpoint local address list Lookup using a
4963 * sockaddr address (ie. not an ifaddr)
4966 sctp_del_local_addr_ep_sa(struct sctp_inpcb *inp, struct sockaddr *sa)
4968 struct sctp_laddr *laddr;
4969 struct sockaddr *l_sa;
4973 * Here is another function I cannot find a caller for. As such we
4974 * SHOULD delete it if we have no users. If we find a user that user
4975 * MUST have the INP locked.
4979 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4980 /* You are already bound to all. You have it already */
4983 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4984 /* make sure the address exists */
4985 if (laddr->ifa == NULL)
4987 if (laddr->ifa->ifa_addr == NULL)
4990 l_sa = laddr->ifa->ifa_addr;
4991 if (l_sa->sa_family == AF_INET6) {
4993 struct sockaddr_in6 *sin1, *sin2;
4995 sin1 = (struct sockaddr_in6 *)l_sa;
4996 sin2 = (struct sockaddr_in6 *)sa;
4997 if (memcmp(&sin1->sin6_addr, &sin2->sin6_addr,
4998 sizeof(struct in6_addr)) == 0) {
5003 } else if (l_sa->sa_family == AF_INET) {
5005 struct sockaddr_in *sin1, *sin2;
5007 sin1 = (struct sockaddr_in *)l_sa;
5008 sin2 = (struct sockaddr_in *)sa;
5009 if (sin1->sin_addr.s_addr == sin2->sin_addr.s_addr) {
5015 /* invalid family */
5020 if (found && inp->laddr_count < 2) {
5021 /* can't delete unless there are at LEAST 2 addresses */
5024 if (found && (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
5026 * remove it from the ep list, this should NOT be done until
5027 * its really gone from the interface list and we won't be
5028 * receiving more of these. Probably right away. If we do
5029 * allow a removal of an address from an association
5030 * (sub-set bind) than this should NOT be called until the
5031 * all ASCONF come back from this association.
5033 sctp_remove_laddr(laddr);
5040 static sctp_assoc_t reneged_asoc_ids[256];
5041 static uint8_t reneged_at = 0;
5043 extern int sctp_do_drain;
5046 sctp_drain_mbufs(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
5049 * We must hunt this association for MBUF's past the cumack (i.e.
5050 * out of order data that we can renege on).
5052 struct sctp_association *asoc;
5053 struct sctp_tmit_chunk *chk, *nchk;
5054 uint32_t cumulative_tsn_p1, tsn;
5055 struct sctp_queued_to_read *ctl, *nctl;
5056 int cnt, strmat, gap;
5058 /* We look for anything larger than the cum-ack + 1 */
5060 SCTP_STAT_INCR(sctps_protocol_drain_calls);
5061 if (sctp_do_drain == 0) {
5065 if (asoc->cumulative_tsn == asoc->highest_tsn_inside_map) {
5066 /* none we can reneg on. */
5069 SCTP_STAT_INCR(sctps_protocol_drains_done);
5070 cumulative_tsn_p1 = asoc->cumulative_tsn + 1;
5072 /* First look in the re-assembly queue */
5073 chk = TAILQ_FIRST(&asoc->reasmqueue);
5075 /* Get the next one */
5076 nchk = TAILQ_NEXT(chk, sctp_next);
5077 if (compare_with_wrap(chk->rec.data.TSN_seq,
5078 cumulative_tsn_p1, MAX_TSN)) {
5079 /* Yep it is above cum-ack */
5081 tsn = chk->rec.data.TSN_seq;
5082 if (tsn >= asoc->mapping_array_base_tsn) {
5083 gap = tsn - asoc->mapping_array_base_tsn;
5085 gap = (MAX_TSN - asoc->mapping_array_base_tsn) +
5088 asoc->size_on_reasm_queue = sctp_sbspace_sub(asoc->size_on_reasm_queue, chk->send_size);
5089 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
5090 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
5091 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
5093 sctp_m_freem(chk->data);
5096 sctp_free_remote_addr(chk->whoTo);
5097 sctp_free_a_chunk(stcb, chk);
5101 /* Ok that was fun, now we will drain all the inbound streams? */
5102 for (strmat = 0; strmat < asoc->streamincnt; strmat++) {
5103 ctl = TAILQ_FIRST(&asoc->strmin[strmat].inqueue);
5105 nctl = TAILQ_NEXT(ctl, next);
5106 if (compare_with_wrap(ctl->sinfo_tsn,
5107 cumulative_tsn_p1, MAX_TSN)) {
5108 /* Yep it is above cum-ack */
5110 tsn = ctl->sinfo_tsn;
5111 if (tsn >= asoc->mapping_array_base_tsn) {
5113 asoc->mapping_array_base_tsn;
5116 asoc->mapping_array_base_tsn) +
5119 asoc->size_on_all_streams = sctp_sbspace_sub(asoc->size_on_all_streams, ctl->length);
5120 sctp_ucount_decr(asoc->cnt_on_all_streams);
5122 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array,
5124 TAILQ_REMOVE(&asoc->strmin[strmat].inqueue,
5127 sctp_m_freem(ctl->data);
5130 sctp_free_remote_addr(ctl->whoFrom);
5131 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_readq, ctl);
5132 SCTP_DECR_READQ_COUNT();
5138 * Question, should we go through the delivery queue? The only
5139 * reason things are on here is the app not reading OR a p-d-api up.
5140 * An attacker COULD send enough in to initiate the PD-API and then
5141 * send a bunch of stuff to other streams... these would wind up on
5142 * the delivery queue.. and then we would not get to them. But in
5143 * order to do this I then have to back-track and un-deliver
5144 * sequence numbers in streams.. el-yucko. I think for now we will
5145 * NOT look at the delivery queue and leave it to be something to
5146 * consider later. An alternative would be to abort the P-D-API with
5147 * a notification and then deliver the data.... Or another method
5148 * might be to keep track of how many times the situation occurs and
5149 * if we see a possible attack underway just abort the association.
5152 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
5154 printf("Freed %d chunks from reneg harvest\n", cnt);
5157 #endif /* SCTP_DEBUG */
5160 * Now do we need to find a new
5161 * asoc->highest_tsn_inside_map?
5163 if (asoc->highest_tsn_inside_map >= asoc->mapping_array_base_tsn) {
5164 gap = asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn;
5166 gap = (MAX_TSN - asoc->mapping_array_base_tsn) +
5167 asoc->highest_tsn_inside_map + 1;
5169 if (gap >= (asoc->mapping_array_size << 3)) {
5171 * Something bad happened or cum-ack and high were
5172 * behind the base, but if so earlier checks should
5173 * have found NO data... wierd... we will start at
5174 * end of mapping array.
5176 printf("Gap was larger than array?? %d set to max:%d maparraymax:%x\n",
5178 (int)(asoc->mapping_array_size << 3),
5179 (int)asoc->highest_tsn_inside_map);
5180 gap = asoc->mapping_array_size << 3;
5183 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
5184 /* found the new highest */
5185 asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn + gap;
5191 /* Nothing left in map */
5192 memset(asoc->mapping_array, 0, asoc->mapping_array_size);
5193 asoc->mapping_array_base_tsn = asoc->cumulative_tsn + 1;
5194 asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
5196 asoc->last_revoke_count = cnt;
5197 SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
5198 sctp_send_sack(stcb);
5199 reneged_asoc_ids[reneged_at] = sctp_get_associd(stcb);
5203 * Another issue, in un-setting the TSN's in the mapping array we
5204 * DID NOT adjust the higest_tsn marker. This will cause one of two
5205 * things to occur. It may cause us to do extra work in checking for
5206 * our mapping array movement. More importantly it may cause us to
5207 * SACK every datagram. This may not be a bad thing though since we
5208 * will recover once we get our cum-ack above and all this stuff we
5217 * We must walk the PCB lists for ALL associations here. The system
5218 * is LOW on MBUF's and needs help. This is where reneging will
5219 * occur. We really hope this does NOT happen!
5221 struct sctp_inpcb *inp;
5222 struct sctp_tcb *stcb;
5224 SCTP_INP_INFO_RLOCK();
5225 LIST_FOREACH(inp, &sctppcbinfo.listhead, sctp_list) {
5226 /* For each endpoint */
5227 SCTP_INP_RLOCK(inp);
5228 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5229 /* For each association */
5230 SCTP_TCB_LOCK(stcb);
5231 sctp_drain_mbufs(inp, stcb);
5232 SCTP_TCB_UNLOCK(stcb);
5234 SCTP_INP_RUNLOCK(inp);
5236 SCTP_INP_INFO_RUNLOCK();
5240 * start a new iterator
5241 * iterates through all endpoints and associations based on the pcb_state
5242 * flags and asoc_state. "af" (mandatory) is executed for all matching
5243 * assocs and "ef" (optional) is executed when the iterator completes.
5244 * "inpf" (optional) is executed for each new endpoint as it is being
5248 sctp_initiate_iterator(inp_func inpf, asoc_func af, uint32_t pcb_state,
5249 uint32_t pcb_features, uint32_t asoc_state, void *argp, uint32_t argi,
5250 end_func ef, struct sctp_inpcb *s_inp, uint8_t chunk_output_off)
5252 struct sctp_iterator *it = NULL;
5258 SCTP_MALLOC(it, struct sctp_iterator *, sizeof(struct sctp_iterator),
5263 memset(it, 0, sizeof(*it));
5264 it->function_assoc = af;
5265 it->function_inp = inpf;
5266 it->function_atend = ef;
5269 it->pcb_flags = pcb_state;
5270 it->pcb_features = pcb_features;
5271 it->asoc_state = asoc_state;
5272 it->no_chunk_output = chunk_output_off;
5275 it->iterator_flags = SCTP_ITERATOR_DO_SINGLE_INP;
5277 SCTP_INP_INFO_RLOCK();
5278 it->inp = LIST_FIRST(&sctppcbinfo.listhead);
5279 SCTP_INP_INFO_RUNLOCK();
5280 it->iterator_flags = SCTP_ITERATOR_DO_ALL_INP;
5283 /* Init the timer */
5284 SCTP_OS_TIMER_INIT(&it->tmr.timer);
5285 /* add to the list of all iterators */
5286 SCTP_INP_INFO_WLOCK();
5287 LIST_INSERT_HEAD(&sctppcbinfo.iteratorhead, it, sctp_nxt_itr);
5288 SCTP_INP_INFO_WUNLOCK();
5290 sctp_timer_start(SCTP_TIMER_TYPE_ITERATOR, (struct sctp_inpcb *)it,