2 * Copyright (c) 2001-2007, Cisco Systems, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * a) Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
10 * b) Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the distribution.
14 * c) Neither the name of Cisco Systems, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 * THE POSSIBILITY OF SUCH DAMAGE.
31 /* $KAME: sctp_pcb.c,v 1.38 2005/03/06 16:04:18 itojun Exp $ */
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <netinet/sctp_os.h>
38 #include <netinet/sctp_var.h>
39 #include <netinet/sctp_pcb.h>
40 #include <netinet/sctputil.h>
41 #include <netinet/sctp.h>
42 #include <netinet/sctp_header.h>
43 #include <netinet/sctp_asconf.h>
44 #include <netinet/sctp_output.h>
45 #include <netinet/sctp_timer.h>
49 uint32_t sctp_debug_on = 0;
51 #endif /* SCTP_DEBUG */
54 extern int sctp_pcbtblsize;
55 extern int sctp_hashtblsize;
56 extern int sctp_chunkscale;
58 struct sctp_epinfo sctppcbinfo;
60 /* FIX: we don't handle multiple link local scopes */
61 /* "scopeless" replacement IN6_ARE_ADDR_EQUAL */
63 SCTP6_ARE_ADDR_EQUAL(struct in6_addr *a, struct in6_addr *b)
65 struct in6_addr tmp_a, tmp_b;
67 /* use a copy of a and b */
70 in6_clearscope(&tmp_a);
71 in6_clearscope(&tmp_b);
72 return (IN6_ARE_ADDR_EQUAL(&tmp_a, &tmp_b));
77 sctp_fill_pcbinfo(struct sctp_pcbinfo *spcb)
80 * We really don't need to lock this, but I will just because it
83 SCTP_INP_INFO_RLOCK();
84 spcb->ep_count = sctppcbinfo.ipi_count_ep;
85 spcb->asoc_count = sctppcbinfo.ipi_count_asoc;
86 spcb->laddr_count = sctppcbinfo.ipi_count_laddr;
87 spcb->raddr_count = sctppcbinfo.ipi_count_raddr;
88 spcb->chk_count = sctppcbinfo.ipi_count_chunk;
89 spcb->readq_count = sctppcbinfo.ipi_count_readq;
90 spcb->stream_oque = sctppcbinfo.ipi_count_strmoq;
91 spcb->free_chunks = sctppcbinfo.ipi_free_chunks;
93 SCTP_INP_INFO_RUNLOCK();
98 * Notes on locks for FreeBSD 5 and up. All association lookups that have a
99 * definte ep, the INP structure is assumed to be locked for reading. If we
100 * need to go find the INP (ususally when a **inp is passed) then we must
101 * lock the INFO structure first and if needed lock the INP too. Note that if
108 * Given a endpoint, look and find in its association list any association
109 * with the "to" address given. This can be a "from" address, too, for
110 * inbound packets. For outbound packets it is a true "to" address.
113 static struct sctp_tcb *
114 sctp_tcb_special_locate(struct sctp_inpcb **inp_p, struct sockaddr *from,
115 struct sockaddr *to, struct sctp_nets **netp)
117 /**** ASSUMSES THE CALLER holds the INP_INFO_RLOCK */
120 * Note for this module care must be taken when observing what to is
121 * for. In most of the rest of the code the TO field represents my
122 * peer and the FROM field represents my address. For this module it
123 * is reversed of that.
126 * If we support the TCP model, then we must now dig through to see
127 * if we can find our endpoint in the list of tcp ep's.
129 uint16_t lport, rport;
130 struct sctppcbhead *ephead;
131 struct sctp_inpcb *inp;
132 struct sctp_laddr *laddr;
133 struct sctp_tcb *stcb;
134 struct sctp_nets *net;
136 if ((to == NULL) || (from == NULL)) {
139 if (to->sa_family == AF_INET && from->sa_family == AF_INET) {
140 lport = ((struct sockaddr_in *)to)->sin_port;
141 rport = ((struct sockaddr_in *)from)->sin_port;
142 } else if (to->sa_family == AF_INET6 && from->sa_family == AF_INET6) {
143 lport = ((struct sockaddr_in6 *)to)->sin6_port;
144 rport = ((struct sockaddr_in6 *)from)->sin6_port;
148 ephead = &sctppcbinfo.sctp_tcpephash[SCTP_PCBHASH_ALLADDR(
149 (lport + rport), sctppcbinfo.hashtcpmark)];
151 * Ok now for each of the guys in this bucket we must look and see:
152 * - Does the remote port match. - Does there single association's
153 * addresses match this address (to). If so we update p_ep to point
154 * to this ep and return the tcb from it.
156 LIST_FOREACH(inp, ephead, sctp_hash) {
157 if (lport != inp->sctp_lport) {
161 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
162 SCTP_INP_RUNLOCK(inp);
165 /* check to see if the ep has one of the addresses */
166 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
167 /* We are NOT bound all, so look further */
170 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
172 if (laddr->ifa == NULL) {
174 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
175 printf("An ounce of prevention is worth a pound of cure\n");
180 if (laddr->ifa->ifa_addr == NULL) {
182 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
183 printf("ifa with a NULL address\n");
188 if (laddr->ifa->ifa_addr->sa_family ==
190 /* see if it matches */
191 struct sockaddr_in *intf_addr, *sin;
193 intf_addr = (struct sockaddr_in *)
194 laddr->ifa->ifa_addr;
195 sin = (struct sockaddr_in *)to;
196 if (from->sa_family == AF_INET) {
197 if (sin->sin_addr.s_addr ==
198 intf_addr->sin_addr.s_addr) {
203 struct sockaddr_in6 *intf_addr6;
204 struct sockaddr_in6 *sin6;
206 sin6 = (struct sockaddr_in6 *)
208 intf_addr6 = (struct sockaddr_in6 *)
209 laddr->ifa->ifa_addr;
211 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
212 &intf_addr6->sin6_addr)) {
220 /* This endpoint does not have this address */
221 SCTP_INP_RUNLOCK(inp);
226 * Ok if we hit here the ep has the address, does it hold
230 stcb = LIST_FIRST(&inp->sctp_asoc_list);
232 SCTP_INP_RUNLOCK(inp);
236 if (stcb->rport != rport) {
237 /* remote port does not match. */
238 SCTP_TCB_UNLOCK(stcb);
239 SCTP_INP_RUNLOCK(inp);
242 /* Does this TCB have a matching address? */
243 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
245 if (net->ro._l_addr.sa.sa_family != from->sa_family) {
246 /* not the same family, can't be a match */
249 if (from->sa_family == AF_INET) {
250 struct sockaddr_in *sin, *rsin;
252 sin = (struct sockaddr_in *)&net->ro._l_addr;
253 rsin = (struct sockaddr_in *)from;
254 if (sin->sin_addr.s_addr ==
255 rsin->sin_addr.s_addr) {
260 /* Update the endpoint pointer */
262 SCTP_INP_RUNLOCK(inp);
266 struct sockaddr_in6 *sin6, *rsin6;
268 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
269 rsin6 = (struct sockaddr_in6 *)from;
270 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
271 &rsin6->sin6_addr)) {
276 /* Update the endpoint pointer */
278 SCTP_INP_RUNLOCK(inp);
283 SCTP_TCB_UNLOCK(stcb);
284 SCTP_INP_RUNLOCK(inp);
292 * 1) If I return a NULL you must decrement any INP ref cnt. 2) If I find an
293 * stcb, both will be locked (locked_tcb and stcb) but decrement will be done
294 * (if locked == NULL). 3) Decrement happens on return ONLY if locked ==
299 sctp_findassociation_ep_addr(struct sctp_inpcb **inp_p, struct sockaddr *remote,
300 struct sctp_nets **netp, struct sockaddr *local, struct sctp_tcb *locked_tcb)
302 struct sctpasochead *head;
303 struct sctp_inpcb *inp;
304 struct sctp_tcb *stcb;
305 struct sctp_nets *net;
309 if (remote->sa_family == AF_INET) {
310 rport = (((struct sockaddr_in *)remote)->sin_port);
311 } else if (remote->sa_family == AF_INET6) {
312 rport = (((struct sockaddr_in6 *)remote)->sin6_port);
318 * UN-lock so we can do proper locking here this occurs when
319 * called from load_addresses_from_init.
321 SCTP_TCB_UNLOCK(locked_tcb);
323 SCTP_INP_INFO_RLOCK();
324 if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
326 * Now either this guy is our listener or it's the
327 * connector. If it is the one that issued the connect, then
328 * it's only chance is to be the first TCB in the list. If
329 * it is the acceptor, then do the special_lookup to hash
330 * and find the real inp.
332 if ((inp->sctp_socket) && (inp->sctp_socket->so_qlimit)) {
333 /* to is peer addr, from is my addr */
334 stcb = sctp_tcb_special_locate(inp_p, remote, local,
336 if ((stcb != NULL) && (locked_tcb == NULL)) {
337 /* we have a locked tcb, lower refcount */
339 SCTP_INP_DECR_REF(inp);
340 SCTP_INP_WUNLOCK(inp);
342 if ((locked_tcb != NULL) && (locked_tcb != stcb)) {
343 SCTP_INP_RLOCK(locked_tcb->sctp_ep);
344 SCTP_TCB_LOCK(locked_tcb);
345 SCTP_INP_RUNLOCK(locked_tcb->sctp_ep);
347 SCTP_INP_INFO_RUNLOCK();
351 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
354 stcb = LIST_FIRST(&inp->sctp_asoc_list);
359 if (stcb->rport != rport) {
360 /* remote port does not match. */
361 SCTP_TCB_UNLOCK(stcb);
364 /* now look at the list of remote addresses */
365 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
367 if (net == (TAILQ_NEXT(net, sctp_next))) {
368 panic("Corrupt net list");
371 if (net->ro._l_addr.sa.sa_family !=
373 /* not the same family */
376 if (remote->sa_family == AF_INET) {
377 struct sockaddr_in *sin, *rsin;
379 sin = (struct sockaddr_in *)
381 rsin = (struct sockaddr_in *)remote;
382 if (sin->sin_addr.s_addr ==
383 rsin->sin_addr.s_addr) {
388 if (locked_tcb == NULL) {
389 SCTP_INP_DECR_REF(inp);
390 } else if (locked_tcb != stcb) {
391 SCTP_TCB_LOCK(locked_tcb);
393 SCTP_INP_WUNLOCK(inp);
394 SCTP_INP_INFO_RUNLOCK();
397 } else if (remote->sa_family == AF_INET6) {
398 struct sockaddr_in6 *sin6, *rsin6;
400 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
401 rsin6 = (struct sockaddr_in6 *)remote;
402 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
403 &rsin6->sin6_addr)) {
408 if (locked_tcb == NULL) {
409 SCTP_INP_DECR_REF(inp);
410 } else if (locked_tcb != stcb) {
411 SCTP_TCB_LOCK(locked_tcb);
413 SCTP_INP_WUNLOCK(inp);
414 SCTP_INP_INFO_RUNLOCK();
419 SCTP_TCB_UNLOCK(stcb);
423 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
426 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(rport,
427 inp->sctp_hashmark)];
431 LIST_FOREACH(stcb, head, sctp_tcbhash) {
432 if (stcb->rport != rport) {
433 /* remote port does not match */
436 /* now look at the list of remote addresses */
438 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
440 if (net == (TAILQ_NEXT(net, sctp_next))) {
441 panic("Corrupt net list");
444 if (net->ro._l_addr.sa.sa_family !=
446 /* not the same family */
449 if (remote->sa_family == AF_INET) {
450 struct sockaddr_in *sin, *rsin;
452 sin = (struct sockaddr_in *)
454 rsin = (struct sockaddr_in *)remote;
455 if (sin->sin_addr.s_addr ==
456 rsin->sin_addr.s_addr) {
461 if (locked_tcb == NULL) {
462 SCTP_INP_DECR_REF(inp);
463 } else if (locked_tcb != stcb) {
464 SCTP_TCB_LOCK(locked_tcb);
466 SCTP_INP_WUNLOCK(inp);
467 SCTP_INP_INFO_RUNLOCK();
470 } else if (remote->sa_family == AF_INET6) {
471 struct sockaddr_in6 *sin6, *rsin6;
473 sin6 = (struct sockaddr_in6 *)
475 rsin6 = (struct sockaddr_in6 *)remote;
476 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
477 &rsin6->sin6_addr)) {
482 if (locked_tcb == NULL) {
483 SCTP_INP_DECR_REF(inp);
484 } else if (locked_tcb != stcb) {
485 SCTP_TCB_LOCK(locked_tcb);
487 SCTP_INP_WUNLOCK(inp);
488 SCTP_INP_INFO_RUNLOCK();
493 SCTP_TCB_UNLOCK(stcb);
497 /* clean up for returning null */
499 SCTP_TCB_LOCK(locked_tcb);
501 SCTP_INP_WUNLOCK(inp);
502 SCTP_INP_INFO_RUNLOCK();
508 * Find an association for a specific endpoint using the association id given
509 * out in the COMM_UP notification
513 sctp_findassociation_ep_asocid(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock)
516 * Use my the assoc_id to find a endpoint
518 struct sctpasochead *head;
519 struct sctp_tcb *stcb;
522 if (asoc_id == 0 || inp == NULL) {
525 SCTP_INP_INFO_RLOCK();
526 id = (uint32_t) asoc_id;
527 head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(id,
528 sctppcbinfo.hashasocmark)];
530 /* invalid id TSNH */
531 SCTP_INP_INFO_RUNLOCK();
534 LIST_FOREACH(stcb, head, sctp_asocs) {
535 SCTP_INP_RLOCK(stcb->sctp_ep);
536 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
537 SCTP_INP_RUNLOCK(stcb->sctp_ep);
538 SCTP_INP_INFO_RUNLOCK();
541 if (stcb->asoc.assoc_id == id) {
543 if (inp != stcb->sctp_ep) {
545 * some other guy has the same id active (id
548 SCTP_INP_RUNLOCK(stcb->sctp_ep);
554 SCTP_INP_RUNLOCK(stcb->sctp_ep);
555 SCTP_INP_INFO_RUNLOCK();
558 SCTP_INP_RUNLOCK(stcb->sctp_ep);
560 /* Ok if we missed here, lets try the restart hash */
561 head = &sctppcbinfo.sctp_restarthash[SCTP_PCBHASH_ASOC(id, sctppcbinfo.hashrestartmark)];
563 /* invalid id TSNH */
564 SCTP_INP_INFO_RUNLOCK();
567 LIST_FOREACH(stcb, head, sctp_tcbrestarhash) {
568 SCTP_INP_RLOCK(stcb->sctp_ep);
569 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
570 SCTP_INP_RUNLOCK(stcb->sctp_ep);
571 SCTP_INP_INFO_RUNLOCK();
575 SCTP_INP_RUNLOCK(stcb->sctp_ep);
576 if (stcb->asoc.assoc_id == id) {
578 if (inp != stcb->sctp_ep) {
580 * some other guy has the same id active (id
583 SCTP_TCB_UNLOCK(stcb);
586 SCTP_INP_INFO_RUNLOCK();
589 SCTP_TCB_UNLOCK(stcb);
591 SCTP_INP_INFO_RUNLOCK();
596 static struct sctp_inpcb *
597 sctp_endpoint_probe(struct sockaddr *nam, struct sctppcbhead *head,
600 struct sctp_inpcb *inp;
601 struct sockaddr_in *sin;
602 struct sockaddr_in6 *sin6;
603 struct sctp_laddr *laddr;
606 * Endpoing probe expects that the INP_INFO is locked.
608 if (nam->sa_family == AF_INET) {
609 sin = (struct sockaddr_in *)nam;
611 } else if (nam->sa_family == AF_INET6) {
612 sin6 = (struct sockaddr_in6 *)nam;
615 /* unsupported family */
620 LIST_FOREACH(inp, head, sctp_hash) {
622 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
623 SCTP_INP_RUNLOCK(inp);
626 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) &&
627 (inp->sctp_lport == lport)) {
629 if ((nam->sa_family == AF_INET) &&
630 (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
631 SCTP_IPV6_V6ONLY(inp)) {
632 /* IPv4 on a IPv6 socket with ONLY IPv6 set */
633 SCTP_INP_RUNLOCK(inp);
636 /* A V6 address and the endpoint is NOT bound V6 */
637 if (nam->sa_family == AF_INET6 &&
638 (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
639 SCTP_INP_RUNLOCK(inp);
642 SCTP_INP_RUNLOCK(inp);
645 SCTP_INP_RUNLOCK(inp);
648 if ((nam->sa_family == AF_INET) &&
649 (sin->sin_addr.s_addr == INADDR_ANY)) {
650 /* Can't hunt for one that has no address specified */
652 } else if ((nam->sa_family == AF_INET6) &&
653 (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))) {
654 /* Can't hunt for one that has no address specified */
658 * ok, not bound to all so see if we can find a EP bound to this
661 LIST_FOREACH(inp, head, sctp_hash) {
663 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
664 SCTP_INP_RUNLOCK(inp);
667 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)) {
668 SCTP_INP_RUNLOCK(inp);
672 * Ok this could be a likely candidate, look at all of its
675 if (inp->sctp_lport != lport) {
676 SCTP_INP_RUNLOCK(inp);
679 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
680 if (laddr->ifa == NULL) {
682 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
683 printf("An ounce of prevention is worth a pound of cure\n");
689 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
690 printf("Ok laddr->ifa:%p is possible, ",
694 if (laddr->ifa->ifa_addr == NULL) {
696 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
697 printf("Huh IFA as an ifa_addr=NULL, ");
702 if (laddr->ifa->ifa_addr->sa_family == nam->sa_family) {
703 /* possible, see if it matches */
704 struct sockaddr_in *intf_addr;
706 intf_addr = (struct sockaddr_in *)
707 laddr->ifa->ifa_addr;
708 if (nam->sa_family == AF_INET) {
709 if (sin->sin_addr.s_addr ==
710 intf_addr->sin_addr.s_addr) {
711 SCTP_INP_RUNLOCK(inp);
714 } else if (nam->sa_family == AF_INET6) {
715 struct sockaddr_in6 *intf_addr6;
717 intf_addr6 = (struct sockaddr_in6 *)
718 laddr->ifa->ifa_addr;
719 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
720 &intf_addr6->sin6_addr)) {
721 SCTP_INP_RUNLOCK(inp);
727 SCTP_INP_RUNLOCK(inp);
734 sctp_pcb_findep(struct sockaddr *nam, int find_tcp_pool, int have_lock)
737 * First we check the hash table to see if someone has this port
738 * bound with just the port.
740 struct sctp_inpcb *inp;
741 struct sctppcbhead *head;
742 struct sockaddr_in *sin;
743 struct sockaddr_in6 *sin6;
746 if (nam->sa_family == AF_INET) {
747 sin = (struct sockaddr_in *)nam;
748 lport = ((struct sockaddr_in *)nam)->sin_port;
749 } else if (nam->sa_family == AF_INET6) {
750 sin6 = (struct sockaddr_in6 *)nam;
751 lport = ((struct sockaddr_in6 *)nam)->sin6_port;
753 /* unsupported family */
757 * I could cheat here and just cast to one of the types but we will
758 * do it right. It also provides the check against an Unsupported
761 /* Find the head of the ALLADDR chain */
762 if (have_lock == 0) {
763 SCTP_INP_INFO_RLOCK();
766 head = &sctppcbinfo.sctp_ephash[SCTP_PCBHASH_ALLADDR(lport,
767 sctppcbinfo.hashmark)];
768 inp = sctp_endpoint_probe(nam, head, lport);
771 * If the TCP model exists it could be that the main listening
772 * endpoint is gone but there exists a connected socket for this guy
773 * yet. If so we can return the first one that we find. This may NOT
774 * be the correct one but the sctp_findassociation_ep_addr has
775 * further code to look at all TCP models.
777 if (inp == NULL && find_tcp_pool) {
780 for (i = 0; i < sctppcbinfo.hashtblsize; i++) {
782 * This is real gross, but we do NOT have a remote
783 * port at this point depending on who is calling.
784 * We must therefore look for ANY one that matches
787 head = &sctppcbinfo.sctp_tcpephash[i];
788 if (LIST_FIRST(head)) {
789 inp = sctp_endpoint_probe(nam, head, lport);
798 SCTP_INP_INCR_REF(inp);
800 if (have_lock == 0) {
801 SCTP_INP_INFO_RUNLOCK();
807 * Find an association for an endpoint with the pointer to whom you want to
808 * send to and the endpoint pointer. The address can be IPv4 or IPv6. We may
809 * need to change the *to to some other struct like a mbuf...
812 sctp_findassociation_addr_sa(struct sockaddr *to, struct sockaddr *from,
813 struct sctp_inpcb **inp_p, struct sctp_nets **netp, int find_tcp_pool)
815 struct sctp_inpcb *inp;
816 struct sctp_tcb *retval;
818 SCTP_INP_INFO_RLOCK();
821 retval = sctp_tcb_special_locate(inp_p, from, to, netp);
823 retval = sctp_tcb_special_locate(&inp, from, to, netp);
825 if (retval != NULL) {
826 SCTP_INP_INFO_RUNLOCK();
830 inp = sctp_pcb_findep(to, 0, 1);
834 SCTP_INP_INFO_RUNLOCK();
840 * ok, we have an endpoint, now lets find the assoc for it (if any)
841 * we now place the source address or from in the to of the find
842 * endpoint call. Since in reality this chain is used from the
843 * inbound packet side.
846 retval = sctp_findassociation_ep_addr(inp_p, from, netp, to, NULL);
848 retval = sctp_findassociation_ep_addr(&inp, from, netp, to, NULL);
855 * This routine will grub through the mbuf that is a INIT or INIT-ACK and
856 * find all addresses that the sender has specified in any address list. Each
857 * address will be used to lookup the TCB and see if one exits.
859 static struct sctp_tcb *
860 sctp_findassociation_special_addr(struct mbuf *m, int iphlen, int offset,
861 struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp,
862 struct sockaddr *dest)
864 struct sockaddr_in sin4;
865 struct sockaddr_in6 sin6;
866 struct sctp_paramhdr *phdr, parm_buf;
867 struct sctp_tcb *retval;
868 uint32_t ptype, plen;
870 memset(&sin4, 0, sizeof(sin4));
871 memset(&sin6, 0, sizeof(sin6));
872 sin4.sin_len = sizeof(sin4);
873 sin4.sin_family = AF_INET;
874 sin4.sin_port = sh->src_port;
875 sin6.sin6_len = sizeof(sin6);
876 sin6.sin6_family = AF_INET6;
877 sin6.sin6_port = sh->src_port;
880 offset += sizeof(struct sctp_init_chunk);
882 phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
883 while (phdr != NULL) {
884 /* now we must see if we want the parameter */
885 ptype = ntohs(phdr->param_type);
886 plen = ntohs(phdr->param_length);
890 if (ptype == SCTP_IPV4_ADDRESS &&
891 plen == sizeof(struct sctp_ipv4addr_param)) {
892 /* Get the rest of the address */
893 struct sctp_ipv4addr_param ip4_parm, *p4;
895 phdr = sctp_get_next_param(m, offset,
896 (struct sctp_paramhdr *)&ip4_parm, plen);
900 p4 = (struct sctp_ipv4addr_param *)phdr;
901 memcpy(&sin4.sin_addr, &p4->addr, sizeof(p4->addr));
903 retval = sctp_findassociation_ep_addr(inp_p,
904 (struct sockaddr *)&sin4, netp, dest, NULL);
905 if (retval != NULL) {
908 } else if (ptype == SCTP_IPV6_ADDRESS &&
909 plen == sizeof(struct sctp_ipv6addr_param)) {
910 /* Get the rest of the address */
911 struct sctp_ipv6addr_param ip6_parm, *p6;
913 phdr = sctp_get_next_param(m, offset,
914 (struct sctp_paramhdr *)&ip6_parm, plen);
918 p6 = (struct sctp_ipv6addr_param *)phdr;
919 memcpy(&sin6.sin6_addr, &p6->addr, sizeof(p6->addr));
921 retval = sctp_findassociation_ep_addr(inp_p,
922 (struct sockaddr *)&sin6, netp, dest, NULL);
923 if (retval != NULL) {
927 offset += SCTP_SIZE32(plen);
928 phdr = sctp_get_next_param(m, offset, &parm_buf,
935 static struct sctp_tcb *
936 sctp_findassoc_by_vtag(struct sockaddr *from, uint32_t vtag,
937 struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint16_t rport,
938 uint16_t lport, int skip_src_check)
941 * Use my vtag to hash. If we find it we then verify the source addr
942 * is in the assoc. If all goes well we save a bit on rec of a
945 struct sctpasochead *head;
946 struct sctp_nets *net;
947 struct sctp_tcb *stcb;
951 SCTP_INP_INFO_RLOCK();
952 head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(vtag,
953 sctppcbinfo.hashasocmark)];
956 SCTP_INP_INFO_RUNLOCK();
959 LIST_FOREACH(stcb, head, sctp_asocs) {
960 SCTP_INP_RLOCK(stcb->sctp_ep);
961 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
962 SCTP_INP_RUNLOCK(stcb->sctp_ep);
963 SCTP_INP_INFO_RUNLOCK();
967 SCTP_INP_RUNLOCK(stcb->sctp_ep);
968 if (stcb->asoc.my_vtag == vtag) {
970 if (stcb->rport != rport) {
972 * we could remove this if vtags are unique
975 SCTP_TCB_UNLOCK(stcb);
978 if (stcb->sctp_ep->sctp_lport != lport) {
980 * we could remove this if vtags are unique
983 SCTP_TCB_UNLOCK(stcb);
986 if (skip_src_check) {
987 *netp = NULL; /* unknown */
988 *inp_p = stcb->sctp_ep;
989 SCTP_INP_INFO_RUNLOCK();
992 net = sctp_findnet(stcb, from);
996 SCTP_STAT_INCR(sctps_vtagexpress);
997 *inp_p = stcb->sctp_ep;
998 SCTP_INP_INFO_RUNLOCK();
1002 * not him, this should only happen in rare
1003 * cases so I peg it.
1005 SCTP_STAT_INCR(sctps_vtagbogus);
1008 SCTP_TCB_UNLOCK(stcb);
1010 SCTP_INP_INFO_RUNLOCK();
1015 * Find an association with the pointer to the inbound IP packet. This can be
1016 * a IPv4 or IPv6 packet.
1019 sctp_findassociation_addr(struct mbuf *m, int iphlen, int offset,
1020 struct sctphdr *sh, struct sctp_chunkhdr *ch,
1021 struct sctp_inpcb **inp_p, struct sctp_nets **netp)
1025 struct sctp_tcb *retval;
1026 struct sockaddr_storage to_store, from_store;
1027 struct sockaddr *to = (struct sockaddr *)&to_store;
1028 struct sockaddr *from = (struct sockaddr *)&from_store;
1029 struct sctp_inpcb *inp;
1032 iph = mtod(m, struct ip *);
1033 if (iph->ip_v == IPVERSION) {
1035 struct sockaddr_in *from4;
1037 from4 = (struct sockaddr_in *)&from_store;
1038 bzero(from4, sizeof(*from4));
1039 from4->sin_family = AF_INET;
1040 from4->sin_len = sizeof(struct sockaddr_in);
1041 from4->sin_addr.s_addr = iph->ip_src.s_addr;
1042 from4->sin_port = sh->src_port;
1043 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
1045 struct ip6_hdr *ip6;
1046 struct sockaddr_in6 *from6;
1048 ip6 = mtod(m, struct ip6_hdr *);
1049 from6 = (struct sockaddr_in6 *)&from_store;
1050 bzero(from6, sizeof(*from6));
1051 from6->sin6_family = AF_INET6;
1052 from6->sin6_len = sizeof(struct sockaddr_in6);
1053 from6->sin6_addr = ip6->ip6_src;
1054 from6->sin6_port = sh->src_port;
1055 /* Get the scopes in properly to the sin6 addr's */
1056 /* we probably don't need these operations */
1057 (void)sa6_recoverscope(from6);
1058 sa6_embedscope(from6, ip6_use_defzone);
1060 /* Currently not supported. */
1064 /* we only go down this path if vtag is non-zero */
1065 retval = sctp_findassoc_by_vtag(from, ntohl(sh->v_tag),
1066 inp_p, netp, sh->src_port, sh->dest_port, 0);
1071 if (iph->ip_v == IPVERSION) {
1073 struct sockaddr_in *to4;
1075 to4 = (struct sockaddr_in *)&to_store;
1076 bzero(to4, sizeof(*to4));
1077 to4->sin_family = AF_INET;
1078 to4->sin_len = sizeof(struct sockaddr_in);
1079 to4->sin_addr.s_addr = iph->ip_dst.s_addr;
1080 to4->sin_port = sh->dest_port;
1081 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
1083 struct ip6_hdr *ip6;
1084 struct sockaddr_in6 *to6;
1086 ip6 = mtod(m, struct ip6_hdr *);
1087 to6 = (struct sockaddr_in6 *)&to_store;
1088 bzero(to6, sizeof(*to6));
1089 to6->sin6_family = AF_INET6;
1090 to6->sin6_len = sizeof(struct sockaddr_in6);
1091 to6->sin6_addr = ip6->ip6_dst;
1092 to6->sin6_port = sh->dest_port;
1093 /* Get the scopes in properly to the sin6 addr's */
1094 /* we probably don't need these operations */
1095 (void)sa6_recoverscope(to6);
1096 sa6_embedscope(to6, ip6_use_defzone);
1100 * FIX FIX?, I think we only need to look in the TCP pool if its an
1101 * INIT or COOKIE-ECHO, We really don't need to find it that way if
1102 * its a INIT-ACK or COOKIE_ACK since these in bot one-2-one and
1103 * one-2-N would be in the main pool anyway.
1105 if ((ch->chunk_type != SCTP_INITIATION) &&
1106 (ch->chunk_type != SCTP_INITIATION_ACK) &&
1107 (ch->chunk_type != SCTP_COOKIE_ACK) &&
1108 (ch->chunk_type != SCTP_COOKIE_ECHO)) {
1109 /* Other chunk types go to the tcp pool. */
1113 retval = sctp_findassociation_addr_sa(to, from, inp_p, netp,
1117 retval = sctp_findassociation_addr_sa(to, from, &inp, netp,
1121 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1122 printf("retval:%p inp:%p\n", retval, inp);
1125 if (retval == NULL && inp) {
1126 /* Found a EP but not this address */
1127 if ((ch->chunk_type == SCTP_INITIATION) ||
1128 (ch->chunk_type == SCTP_INITIATION_ACK)) {
1130 * special hook, we do NOT return linp or an
1131 * association that is linked to an existing
1132 * association that is under the TCP pool (i.e. no
1133 * listener exists). The endpoint finding routine
1134 * will always find a listner before examining the
1137 if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
1143 retval = sctp_findassociation_special_addr(m, iphlen,
1144 offset, sh, &inp, netp, to);
1145 if (inp_p != NULL) {
1151 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1152 printf("retval is %p\n", retval);
1159 * lookup an association by an ASCONF lookup address.
1160 * if the lookup address is 0.0.0.0 or ::0, use the vtag to do the lookup
1163 sctp_findassociation_ep_asconf(struct mbuf *m, int iphlen, int offset,
1164 struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp)
1166 struct sctp_tcb *stcb;
1167 struct sockaddr_in *sin;
1168 struct sockaddr_in6 *sin6;
1169 struct sockaddr_storage local_store, remote_store;
1171 struct sctp_paramhdr parm_buf, *phdr;
1173 int zero_address = 0;
1176 memset(&local_store, 0, sizeof(local_store));
1177 memset(&remote_store, 0, sizeof(remote_store));
1179 /* First get the destination address setup too. */
1180 iph = mtod(m, struct ip *);
1181 if (iph->ip_v == IPVERSION) {
1183 sin = (struct sockaddr_in *)&local_store;
1184 sin->sin_family = AF_INET;
1185 sin->sin_len = sizeof(*sin);
1186 sin->sin_port = sh->dest_port;
1187 sin->sin_addr.s_addr = iph->ip_dst.s_addr;
1188 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
1190 struct ip6_hdr *ip6;
1192 ip6 = mtod(m, struct ip6_hdr *);
1193 sin6 = (struct sockaddr_in6 *)&local_store;
1194 sin6->sin6_family = AF_INET6;
1195 sin6->sin6_len = sizeof(*sin6);
1196 sin6->sin6_port = sh->dest_port;
1197 sin6->sin6_addr = ip6->ip6_dst;
1202 phdr = sctp_get_next_param(m, offset + sizeof(struct sctp_asconf_chunk),
1203 &parm_buf, sizeof(struct sctp_paramhdr));
1206 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
1207 printf("findassociation_ep_asconf: failed to get asconf lookup addr\n");
1209 #endif /* SCTP_DEBUG */
1212 ptype = (int)((uint32_t) ntohs(phdr->param_type));
1213 /* get the correlation address */
1214 if (ptype == SCTP_IPV6_ADDRESS) {
1215 /* ipv6 address param */
1216 struct sctp_ipv6addr_param *p6, p6_buf;
1218 if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv6addr_param)) {
1221 p6 = (struct sctp_ipv6addr_param *)sctp_get_next_param(m,
1222 offset + sizeof(struct sctp_asconf_chunk),
1223 &p6_buf.ph, sizeof(*p6));
1226 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
1227 printf("findassociation_ep_asconf: failed to get asconf v6 lookup addr\n");
1229 #endif /* SCTP_DEBUG */
1232 sin6 = (struct sockaddr_in6 *)&remote_store;
1233 sin6->sin6_family = AF_INET6;
1234 sin6->sin6_len = sizeof(*sin6);
1235 sin6->sin6_port = sh->src_port;
1236 memcpy(&sin6->sin6_addr, &p6->addr, sizeof(struct in6_addr));
1237 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
1239 } else if (ptype == SCTP_IPV4_ADDRESS) {
1240 /* ipv4 address param */
1241 struct sctp_ipv4addr_param *p4, p4_buf;
1243 if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv4addr_param)) {
1246 p4 = (struct sctp_ipv4addr_param *)sctp_get_next_param(m,
1247 offset + sizeof(struct sctp_asconf_chunk),
1248 &p4_buf.ph, sizeof(*p4));
1251 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
1252 printf("findassociation_ep_asconf: failed to get asconf v4 lookup addr\n");
1254 #endif /* SCTP_DEBUG */
1257 sin = (struct sockaddr_in *)&remote_store;
1258 sin->sin_family = AF_INET;
1259 sin->sin_len = sizeof(*sin);
1260 sin->sin_port = sh->src_port;
1261 memcpy(&sin->sin_addr, &p4->addr, sizeof(struct in_addr));
1262 if (sin->sin_addr.s_addr == INADDR_ANY)
1265 /* invalid address param type */
1270 stcb = sctp_findassoc_by_vtag(NULL, ntohl(sh->v_tag), inp_p,
1271 netp, sh->src_port, sh->dest_port, 1);
1273 * printf("findassociation_ep_asconf: zero lookup address
1274 * finds stcb 0x%x\n", (uint32_t)stcb);
1277 stcb = sctp_findassociation_ep_addr(inp_p,
1278 (struct sockaddr *)&remote_store, netp,
1279 (struct sockaddr *)&local_store, NULL);
1285 extern int sctp_max_burst_default;
1287 extern unsigned int sctp_delayed_sack_time_default;
1288 extern unsigned int sctp_heartbeat_interval_default;
1289 extern unsigned int sctp_pmtu_raise_time_default;
1290 extern unsigned int sctp_shutdown_guard_time_default;
1291 extern unsigned int sctp_secret_lifetime_default;
1293 extern unsigned int sctp_rto_max_default;
1294 extern unsigned int sctp_rto_min_default;
1295 extern unsigned int sctp_rto_initial_default;
1296 extern unsigned int sctp_init_rto_max_default;
1297 extern unsigned int sctp_valid_cookie_life_default;
1298 extern unsigned int sctp_init_rtx_max_default;
1299 extern unsigned int sctp_assoc_rtx_max_default;
1300 extern unsigned int sctp_path_rtx_max_default;
1301 extern unsigned int sctp_nr_outgoing_streams_default;
1304 * allocate a sctp_inpcb and setup a temporary binding to a port/all
1305 * addresses. This way if we don't get a bind we by default pick a ephemeral
1306 * port with all addresses bound.
1309 sctp_inpcb_alloc(struct socket *so)
1312 * we get called when a new endpoint starts up. We need to allocate
1313 * the sctp_inpcb structure from the zone and init it. Mark it as
1314 * unbound and find a port that we can use as an ephemeral with
1315 * INADDR_ANY. If the user binds later no problem we can then add in
1316 * the specific addresses. And setup the default parameters for the
1320 struct sctp_inpcb *inp;
1323 struct timeval time;
1324 sctp_sharedkey_t *null_key;
1328 SCTP_INP_INFO_WLOCK();
1329 inp = (struct sctp_inpcb *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_ep);
1331 printf("Out of SCTP-INPCB structures - no resources\n");
1332 SCTP_INP_INFO_WUNLOCK();
1336 bzero(inp, sizeof(*inp));
1338 /* bump generations */
1339 /* setup socket pointers */
1340 inp->sctp_socket = so;
1341 inp->ip_inp.inp.inp_socket = so;
1343 inp->partial_delivery_point = so->so_rcv.sb_hiwat >> SCTP_PARTIAL_DELIVERY_SHIFT;
1344 inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
1348 struct inpcbpolicy *pcb_sp = NULL;
1350 error = ipsec_init_pcbpolicy(so, &pcb_sp);
1351 /* Arrange to share the policy */
1352 inp->ip_inp.inp.inp_sp = pcb_sp;
1353 ((struct in6pcb *)(&inp->ip_inp.inp))->in6p_sp = pcb_sp;
1356 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
1357 SCTP_INP_INFO_WUNLOCK();
1361 SCTP_INCR_EP_COUNT();
1362 inp->ip_inp.inp.inp_ip_ttl = ip_defttl;
1363 SCTP_INP_INFO_WUNLOCK();
1365 so->so_pcb = (caddr_t)inp;
1367 if ((so->so_type == SOCK_DGRAM) ||
1368 (so->so_type == SOCK_SEQPACKET)) {
1369 /* UDP style socket */
1370 inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
1371 SCTP_PCB_FLAGS_UNBOUND);
1372 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
1373 /* Be sure it is NON-BLOCKING IO for UDP */
1374 /* so->so_state |= SS_NBIO; */
1375 } else if (so->so_type == SOCK_STREAM) {
1376 /* TCP style socket */
1377 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
1378 SCTP_PCB_FLAGS_UNBOUND);
1379 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
1380 /* Be sure we have blocking IO by default */
1381 so->so_state &= ~SS_NBIO;
1384 * unsupported socket type (RAW, etc)- in case we missed it
1387 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
1388 return (EOPNOTSUPP);
1390 inp->sctp_tcbhash = SCTP_HASH_INIT(sctp_pcbtblsize,
1391 &inp->sctp_hashmark);
1392 if (inp->sctp_tcbhash == NULL) {
1393 printf("Out of SCTP-INPCB->hashinit - no resources\n");
1394 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
1397 SCTP_INP_INFO_WLOCK();
1398 SCTP_INP_LOCK_INIT(inp);
1399 SCTP_INP_READ_INIT(inp);
1400 SCTP_ASOC_CREATE_LOCK_INIT(inp);
1401 /* lock the new ep */
1402 SCTP_INP_WLOCK(inp);
1404 /* add it to the info area */
1405 LIST_INSERT_HEAD(&sctppcbinfo.listhead, inp, sctp_list);
1406 SCTP_INP_INFO_WUNLOCK();
1408 TAILQ_INIT(&inp->read_queue);
1409 LIST_INIT(&inp->sctp_addr_list);
1410 LIST_INIT(&inp->sctp_asoc_list);
1412 #ifdef SCTP_TRACK_FREED_ASOCS
1414 LIST_INIT(&inp->sctp_asoc_free_list);
1416 /* Init the timer structure for signature change */
1417 SCTP_OS_TIMER_INIT(&inp->sctp_ep.signature_change.timer);
1418 inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NEWCOOKIE;
1420 /* now init the actual endpoint default data */
1423 /* setup the base timeout information */
1424 m->sctp_timeoutticks[SCTP_TIMER_SEND] = SEC_TO_TICKS(SCTP_SEND_SEC); /* needed ? */
1425 m->sctp_timeoutticks[SCTP_TIMER_INIT] = SEC_TO_TICKS(SCTP_INIT_SEC); /* needed ? */
1426 m->sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sctp_delayed_sack_time_default);
1427 m->sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(sctp_heartbeat_interval_default);
1428 m->sctp_timeoutticks[SCTP_TIMER_PMTU] = SEC_TO_TICKS(sctp_pmtu_raise_time_default);
1429 m->sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] = SEC_TO_TICKS(sctp_shutdown_guard_time_default);
1430 m->sctp_timeoutticks[SCTP_TIMER_SIGNATURE] = SEC_TO_TICKS(sctp_secret_lifetime_default);
1431 /* all max/min max are in ms */
1432 m->sctp_maxrto = sctp_rto_max_default;
1433 m->sctp_minrto = sctp_rto_min_default;
1434 m->initial_rto = sctp_rto_initial_default;
1435 m->initial_init_rto_max = sctp_init_rto_max_default;
1437 m->max_open_streams_intome = MAX_SCTP_STREAMS;
1439 m->max_init_times = sctp_init_rtx_max_default;
1440 m->max_send_times = sctp_assoc_rtx_max_default;
1441 m->def_net_failure = sctp_path_rtx_max_default;
1442 m->sctp_sws_sender = SCTP_SWS_SENDER_DEF;
1443 m->sctp_sws_receiver = SCTP_SWS_RECEIVER_DEF;
1444 m->max_burst = sctp_max_burst_default;
1445 /* number of streams to pre-open on a association */
1446 m->pre_open_stream_count = sctp_nr_outgoing_streams_default;
1448 /* Add adaptation cookie */
1449 m->adaptation_layer_indicator = 0x504C5253;
1451 /* seed random number generator */
1452 m->random_counter = 1;
1453 m->store_at = SCTP_SIGNATURE_SIZE;
1454 SCTP_READ_RANDOM(m->random_numbers, sizeof(m->random_numbers));
1455 sctp_fill_random_store(m);
1457 /* Minimum cookie size */
1458 m->size_of_a_cookie = (sizeof(struct sctp_init_msg) * 2) +
1459 sizeof(struct sctp_state_cookie);
1460 m->size_of_a_cookie += SCTP_SIGNATURE_SIZE;
1462 /* Setup the initial secret */
1463 SCTP_GETTIME_TIMEVAL(&time);
1464 m->time_of_secret_change = time.tv_sec;
1466 for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
1467 m->secret_key[0][i] = sctp_select_initial_TSN(m);
1469 sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL);
1471 /* How long is a cookie good for ? */
1472 m->def_cookie_life = sctp_valid_cookie_life_default;
1475 * Initialize authentication parameters
1477 m->local_hmacs = sctp_default_supported_hmaclist();
1478 m->local_auth_chunks = sctp_alloc_chunklist();
1479 sctp_auth_set_default_chunks(m->local_auth_chunks);
1480 LIST_INIT(&m->shared_keys);
1481 /* add default NULL key as key id 0 */
1482 null_key = sctp_alloc_sharedkey();
1483 sctp_insert_sharedkey(&m->shared_keys, null_key);
1484 SCTP_INP_WUNLOCK(inp);
1485 #ifdef SCTP_LOG_CLOSING
1486 sctp_log_closing(inp, NULL, 12);
1493 sctp_move_pcb_and_assoc(struct sctp_inpcb *old_inp, struct sctp_inpcb *new_inp,
1494 struct sctp_tcb *stcb)
1496 struct sctp_nets *net;
1497 uint16_t lport, rport;
1498 struct sctppcbhead *head;
1499 struct sctp_laddr *laddr, *oladdr;
1501 SCTP_TCB_UNLOCK(stcb);
1502 SCTP_INP_INFO_WLOCK();
1503 SCTP_INP_WLOCK(old_inp);
1504 SCTP_INP_WLOCK(new_inp);
1505 SCTP_TCB_LOCK(stcb);
1508 new_inp->sctp_ep.time_of_secret_change =
1509 old_inp->sctp_ep.time_of_secret_change;
1510 memcpy(new_inp->sctp_ep.secret_key, old_inp->sctp_ep.secret_key,
1511 sizeof(old_inp->sctp_ep.secret_key));
1512 new_inp->sctp_ep.current_secret_number =
1513 old_inp->sctp_ep.current_secret_number;
1514 new_inp->sctp_ep.last_secret_number =
1515 old_inp->sctp_ep.last_secret_number;
1516 new_inp->sctp_ep.size_of_a_cookie = old_inp->sctp_ep.size_of_a_cookie;
1518 /* make it so new data pours into the new socket */
1519 stcb->sctp_socket = new_inp->sctp_socket;
1520 stcb->sctp_ep = new_inp;
1522 /* Copy the port across */
1523 lport = new_inp->sctp_lport = old_inp->sctp_lport;
1524 rport = stcb->rport;
1525 /* Pull the tcb from the old association */
1526 LIST_REMOVE(stcb, sctp_tcbhash);
1527 LIST_REMOVE(stcb, sctp_tcblist);
1529 /* Now insert the new_inp into the TCP connected hash */
1530 head = &sctppcbinfo.sctp_tcpephash[SCTP_PCBHASH_ALLADDR((lport + rport),
1531 sctppcbinfo.hashtcpmark)];
1533 LIST_INSERT_HEAD(head, new_inp, sctp_hash);
1534 /* Its safe to access */
1535 new_inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
1537 /* Now move the tcb into the endpoint list */
1538 LIST_INSERT_HEAD(&new_inp->sctp_asoc_list, stcb, sctp_tcblist);
1540 * Question, do we even need to worry about the ep-hash since we
1541 * only have one connection? Probably not :> so lets get rid of it
1542 * and not suck up any kernel memory in that.
1545 /* Ok. Let's restart timer. */
1546 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1547 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, new_inp,
1551 SCTP_INP_INFO_WUNLOCK();
1552 if (new_inp->sctp_tcbhash != NULL) {
1553 SCTP_HASH_FREE(new_inp->sctp_tcbhash, new_inp->sctp_hashmark);
1554 new_inp->sctp_tcbhash = NULL;
1556 if ((new_inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
1557 /* Subset bound, so copy in the laddr list from the old_inp */
1558 LIST_FOREACH(oladdr, &old_inp->sctp_addr_list, sctp_nxt_addr) {
1559 laddr = (struct sctp_laddr *)SCTP_ZONE_GET(
1560 sctppcbinfo.ipi_zone_laddr);
1561 if (laddr == NULL) {
1563 * Gak, what can we do? This assoc is really
1564 * HOSED. We probably should send an abort
1568 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1569 printf("Association hosed in TCP model, out of laddr memory\n");
1571 #endif /* SCTP_DEBUG */
1574 SCTP_INCR_LADDR_COUNT();
1575 bzero(laddr, sizeof(*laddr));
1576 laddr->ifa = oladdr->ifa;
1577 LIST_INSERT_HEAD(&new_inp->sctp_addr_list, laddr,
1579 new_inp->laddr_count++;
1583 * Now any running timers need to be adjusted since we really don't
1584 * care if they are running or not just blast in the new_inp into
1588 stcb->asoc.hb_timer.ep = (void *)new_inp;
1589 stcb->asoc.dack_timer.ep = (void *)new_inp;
1590 stcb->asoc.asconf_timer.ep = (void *)new_inp;
1591 stcb->asoc.strreset_timer.ep = (void *)new_inp;
1592 stcb->asoc.shut_guard_timer.ep = (void *)new_inp;
1593 stcb->asoc.autoclose_timer.ep = (void *)new_inp;
1594 stcb->asoc.delayed_event_timer.ep = (void *)new_inp;
1595 /* now what about the nets? */
1596 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1597 net->pmtu_timer.ep = (void *)new_inp;
1598 net->rxt_timer.ep = (void *)new_inp;
1599 net->fr_timer.ep = (void *)new_inp;
1601 SCTP_INP_WUNLOCK(new_inp);
1602 SCTP_INP_WUNLOCK(old_inp);
1606 sctp_isport_inuse(struct sctp_inpcb *inp, uint16_t lport)
1608 struct sctppcbhead *head;
1609 struct sctp_inpcb *t_inp;
1611 head = &sctppcbinfo.sctp_ephash[SCTP_PCBHASH_ALLADDR(lport,
1612 sctppcbinfo.hashmark)];
1614 LIST_FOREACH(t_inp, head, sctp_hash) {
1615 if (t_inp->sctp_lport != lport) {
1618 /* This one is in use. */
1619 /* check the v6/v4 binding issue */
1620 if ((t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1621 SCTP_IPV6_V6ONLY(t_inp)) {
1622 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1623 /* collision in V6 space */
1626 /* inp is BOUND_V4 no conflict */
1629 } else if (t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1630 /* t_inp is bound v4 and v6, conflict always */
1633 /* t_inp is bound only V4 */
1634 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1635 SCTP_IPV6_V6ONLY(t_inp)) {
1639 /* else fall through to conflict */
1649 sctp_inpcb_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
1651 /* bind a ep to a socket address */
1652 struct sctppcbhead *head;
1653 struct sctp_inpcb *inp, *inp_tmp;
1654 struct inpcb *ip_inp;
1662 inp = (struct sctp_inpcb *)so->so_pcb;
1663 ip_inp = (struct inpcb *)so->so_pcb;
1665 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1667 printf("Bind called port:%d\n",
1668 ntohs(((struct sockaddr_in *)addr)->sin_port));
1670 sctp_print_address(addr);
1673 #endif /* SCTP_DEBUG */
1674 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
1675 /* already did a bind, subsequent binds NOT allowed ! */
1679 if (addr->sa_family == AF_INET) {
1680 struct sockaddr_in *sin;
1682 /* IPV6_V6ONLY socket? */
1683 if (SCTP_IPV6_V6ONLY(ip_inp)) {
1686 if (addr->sa_len != sizeof(*sin))
1689 sin = (struct sockaddr_in *)addr;
1690 lport = sin->sin_port;
1692 if (sin->sin_addr.s_addr != INADDR_ANY) {
1695 } else if (addr->sa_family == AF_INET6) {
1696 /* Only for pure IPv6 Address. (No IPv4 Mapped!) */
1697 struct sockaddr_in6 *sin6;
1699 sin6 = (struct sockaddr_in6 *)addr;
1701 if (addr->sa_len != sizeof(*sin6))
1704 lport = sin6->sin6_port;
1705 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1707 /* KAME hack: embed scopeid */
1708 if (sa6_embedscope(sin6, ip6_use_defzone) != 0)
1711 /* this must be cleared for ifa_ifwithaddr() */
1712 sin6->sin6_scope_id = 0;
1714 return (EAFNOSUPPORT);
1717 SCTP_INP_INFO_WLOCK();
1718 SCTP_INP_WLOCK(inp);
1719 /* increase our count due to the unlock we do */
1720 SCTP_INP_INCR_REF(inp);
1723 * Did the caller specify a port? if so we must see if a ep
1724 * already has this one bound.
1726 /* got to be root to get at low ports */
1727 if (ntohs(lport) < IPPORT_RESERVED) {
1730 PRIV_NETINET_RESERVEDPORT)
1732 SCTP_INP_DECR_REF(inp);
1733 SCTP_INP_WUNLOCK(inp);
1734 SCTP_INP_INFO_WUNLOCK();
1739 SCTP_INP_DECR_REF(inp);
1740 SCTP_INP_WUNLOCK(inp);
1741 SCTP_INP_INFO_WUNLOCK();
1744 SCTP_INP_WUNLOCK(inp);
1745 inp_tmp = sctp_pcb_findep(addr, 0, 1);
1746 if (inp_tmp != NULL) {
1748 * lock guy returned and lower count note that we
1749 * are not bound so inp_tmp should NEVER be inp. And
1750 * it is this inp (inp_tmp) that gets the reference
1751 * bump, so we must lower it.
1753 SCTP_INP_DECR_REF(inp_tmp);
1754 SCTP_INP_DECR_REF(inp);
1756 SCTP_INP_INFO_WUNLOCK();
1757 return (EADDRNOTAVAIL);
1759 SCTP_INP_WLOCK(inp);
1761 /* verify that no lport is not used by a singleton */
1762 if (sctp_isport_inuse(inp, lport)) {
1763 /* Sorry someone already has this one bound */
1764 SCTP_INP_DECR_REF(inp);
1765 SCTP_INP_WUNLOCK(inp);
1766 SCTP_INP_INFO_WUNLOCK();
1767 return (EADDRNOTAVAIL);
1772 * get any port but lets make sure no one has any address
1773 * with this port bound
1777 * setup the inp to the top (I could use the union but this
1780 uint32_t port_guess;
1781 uint16_t port_attempt;
1785 port_guess = sctp_select_initial_TSN(&inp->sctp_ep);
1786 port_attempt = (port_guess & 0x0000ffff);
1787 if (port_attempt == 0) {
1790 if (port_attempt < IPPORT_RESERVED) {
1791 port_attempt += IPPORT_RESERVED;
1793 if (sctp_isport_inuse(inp, htons(port_attempt)) == 0) {
1794 /* got a port we can use */
1798 /* try upper half */
1800 port_attempt = ((port_guess >> 16) & 0x0000ffff);
1801 if (port_attempt == 0) {
1804 if (port_attempt < IPPORT_RESERVED) {
1805 port_attempt += IPPORT_RESERVED;
1807 if (sctp_isport_inuse(inp, htons(port_attempt)) == 0) {
1808 /* got a port we can use */
1812 /* try two half's added together */
1814 port_attempt = (((port_guess >> 16) & 0x0000ffff) +
1815 (port_guess & 0x0000ffff));
1816 if (port_attempt == 0) {
1817 /* get a new random number */
1820 if (port_attempt < IPPORT_RESERVED) {
1821 port_attempt += IPPORT_RESERVED;
1823 if (sctp_isport_inuse(inp, htons(port_attempt)) == 0) {
1824 /* got a port we can use */
1829 /* we don't get out of the loop until we have a port */
1830 lport = htons(port_attempt);
1832 SCTP_INP_DECR_REF(inp);
1833 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE |
1834 SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
1836 * this really should not happen. The guy did a non-blocking
1837 * bind and then did a close at the same time.
1839 SCTP_INP_WUNLOCK(inp);
1840 SCTP_INP_INFO_WUNLOCK();
1843 /* ok we look clear to give out this port, so lets setup the binding */
1845 /* binding to all addresses, so just set in the proper flags */
1846 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUNDALL;
1847 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
1848 /* set the automatic addr changes from kernel flag */
1849 if (sctp_auto_asconf == 0) {
1850 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1852 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1856 * bind specific, make sure flags is off and add a new
1857 * address structure to the sctp_addr_list inside the ep
1860 * We will need to allocate one and insert it at the head. The
1861 * socketopt call can just insert new addresses in there as
1862 * well. It will also have to do the embed scope kame hack
1863 * too (before adding).
1866 struct sockaddr_storage store_sa;
1868 memset(&store_sa, 0, sizeof(store_sa));
1869 if (addr->sa_family == AF_INET) {
1870 struct sockaddr_in *sin;
1872 sin = (struct sockaddr_in *)&store_sa;
1873 memcpy(sin, addr, sizeof(struct sockaddr_in));
1875 } else if (addr->sa_family == AF_INET6) {
1876 struct sockaddr_in6 *sin6;
1878 sin6 = (struct sockaddr_in6 *)&store_sa;
1879 memcpy(sin6, addr, sizeof(struct sockaddr_in6));
1880 sin6->sin6_port = 0;
1883 * first find the interface with the bound address need to
1884 * zero out the port to find the address! yuck! can't do
1885 * this earlier since need port for sctp_pcb_findep()
1887 ifa = sctp_find_ifa_by_addr((struct sockaddr *)&store_sa);
1889 /* Can't find an interface with that address */
1890 SCTP_INP_WUNLOCK(inp);
1891 SCTP_INP_INFO_WUNLOCK();
1892 return (EADDRNOTAVAIL);
1894 if (addr->sa_family == AF_INET6) {
1895 struct in6_ifaddr *ifa6;
1897 ifa6 = (struct in6_ifaddr *)ifa;
1899 * allow binding of deprecated addresses as per RFC
1900 * 2462 and ipng discussion
1902 if (ifa6->ia6_flags & (IN6_IFF_DETACHED |
1904 IN6_IFF_NOTREADY)) {
1905 /* Can't bind a non-existent addr. */
1906 SCTP_INP_WUNLOCK(inp);
1907 SCTP_INP_INFO_WUNLOCK();
1911 /* we're not bound all */
1912 inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUNDALL;
1913 /* set the automatic addr changes from kernel flag */
1914 if (sctp_auto_asconf == 0) {
1915 sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1917 sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1919 /* allow bindx() to send ASCONF's for binding changes */
1920 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
1921 /* add this address to the endpoint list */
1922 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa);
1924 SCTP_INP_WUNLOCK(inp);
1925 SCTP_INP_INFO_WUNLOCK();
1930 /* find the bucket */
1931 head = &sctppcbinfo.sctp_ephash[SCTP_PCBHASH_ALLADDR(lport,
1932 sctppcbinfo.hashmark)];
1933 /* put it in the bucket */
1934 LIST_INSERT_HEAD(head, inp, sctp_hash);
1936 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1937 printf("Main hash to bind at head:%p, bound port:%d\n", head, ntohs(lport));
1940 /* set in the port */
1941 inp->sctp_lport = lport;
1943 /* turn off just the unbound flag */
1944 inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
1945 SCTP_INP_WUNLOCK(inp);
1946 SCTP_INP_INFO_WUNLOCK();
1952 sctp_iterator_inp_being_freed(struct sctp_inpcb *inp, struct sctp_inpcb *inp_next)
1954 struct sctp_iterator *it;
1957 * We enter with the only the ITERATOR_LOCK in place and a write
1958 * lock on the inp_info stuff.
1962 * Go through all iterators, we must do this since it is possible
1963 * that some iterator does NOT have the lock, but is waiting for it.
1964 * And the one that had the lock has either moved in the last
1965 * iteration or we just cleared it above. We need to find all of
1966 * those guys. The list of iterators should never be very big
1969 LIST_FOREACH(it, &sctppcbinfo.iteratorhead, sctp_nxt_itr) {
1970 if (it == inp->inp_starting_point_for_iterator)
1971 /* skip this guy, he's special */
1973 if (it->inp == inp) {
1975 * This is tricky and we DON'T lock the iterator.
1976 * Reason is he's running but waiting for me since
1977 * inp->inp_starting_point_for_iterator has the lock
1978 * on me (the guy above we skipped). This tells us
1979 * its is not running but waiting for
1980 * inp->inp_starting_point_for_iterator to be
1981 * released by the guy that does have our INP in a
1984 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1988 /* set him up to do the next guy not me */
1994 it = inp->inp_starting_point_for_iterator;
1996 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
2005 /* release sctp_inpcb unbind the port */
2007 sctp_inpcb_free(struct sctp_inpcb *inp, int immediate, int from)
2010 * Here we free a endpoint. We must find it (if it is in the Hash
2011 * table) and remove it from there. Then we must also find it in the
2012 * overall list and remove it from there. After all removals are
2013 * complete then any timer has to be stopped. Then start the actual
2014 * freeing. a) Any local lists. b) Any associations. c) The hash of
2015 * all associations. d) finally the ep itself.
2018 struct sctp_inpcb *inp_save;
2019 struct sctp_tcb *asoc, *nasoc;
2020 struct sctp_laddr *laddr, *nladdr;
2021 struct inpcb *ip_pcb;
2024 struct sctp_queued_to_read *sq;
2027 sctp_sharedkey_t *shared_key;
2030 #ifdef SCTP_LOG_CLOSING
2031 sctp_log_closing(inp, NULL, 0);
2034 SCTP_ITERATOR_LOCK();
2035 so = inp->sctp_socket;
2036 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
2037 /* been here before.. eeks.. get out of here */
2038 printf("This conflict in free SHOULD not be happening!\n");
2039 SCTP_ITERATOR_UNLOCK();
2040 #ifdef SCTP_LOG_CLOSING
2041 sctp_log_closing(inp, NULL, 1);
2045 SCTP_ASOC_CREATE_LOCK(inp);
2046 SCTP_INP_INFO_WLOCK();
2048 SCTP_INP_WLOCK(inp);
2050 * First time through we have the socket lock, after that no more.
2054 * Once we are in we can remove the flag from = 1 is only
2055 * passed from the actual closing routines that are called
2056 * via the sockets layer.
2058 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CLOSE_IP;
2060 sctp_timer_stop(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL,
2061 SCTP_FROM_SCTP_PCB + SCTP_LOC_1);
2064 sctp_m_freem(inp->control);
2065 inp->control = NULL;
2068 sctp_m_freem(inp->pkt);
2072 ip_pcb = &inp->ip_inp.inp; /* we could just cast the main pointer
2073 * here but I will be nice :> (i.e.
2075 if (immediate == 0) {
2079 for ((asoc = LIST_FIRST(&inp->sctp_asoc_list)); asoc != NULL;
2081 nasoc = LIST_NEXT(asoc, sctp_tcblist);
2082 if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
2083 /* Skip guys being freed */
2084 asoc->sctp_socket = NULL;
2088 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_WAIT) ||
2089 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
2090 /* Just abandon things in the front states */
2091 if (asoc->asoc.total_output_queue_size == 0) {
2092 sctp_free_assoc(inp, asoc, SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_2);
2096 SCTP_TCB_LOCK(asoc);
2097 /* Disconnect the socket please */
2098 asoc->sctp_socket = NULL;
2099 asoc->asoc.state |= SCTP_STATE_CLOSED_SOCKET;
2100 if ((asoc->asoc.size_on_reasm_queue > 0) ||
2101 (asoc->asoc.control_pdapi) ||
2102 (asoc->asoc.size_on_all_streams > 0) ||
2103 (so && (so->so_rcv.sb_cc > 0))
2105 /* Left with Data unread */
2106 struct mbuf *op_err;
2108 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
2109 0, M_DONTWAIT, 1, MT_DATA);
2111 /* Fill in the user initiated abort */
2112 struct sctp_paramhdr *ph;
2115 SCTP_BUF_LEN(op_err) =
2116 sizeof(struct sctp_paramhdr) + sizeof(uint32_t);
2118 struct sctp_paramhdr *);
2119 ph->param_type = htons(
2120 SCTP_CAUSE_USER_INITIATED_ABT);
2121 ph->param_length = htons(SCTP_BUF_LEN(op_err));
2122 ippp = (uint32_t *) (ph + 1);
2123 *ippp = htonl(SCTP_FROM_SCTP_PCB + SCTP_LOC_3);
2125 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_3;
2126 sctp_send_abort_tcb(asoc, op_err);
2127 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
2128 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
2129 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
2130 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
2132 sctp_free_assoc(inp, asoc, SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_4);
2134 } else if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
2135 TAILQ_EMPTY(&asoc->asoc.sent_queue) &&
2136 (asoc->asoc.stream_queue_cnt == 0)
2138 if (asoc->asoc.locked_on_sending) {
2141 if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
2142 (SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
2144 * there is nothing queued to send,
2145 * so I send shutdown
2147 sctp_send_shutdown(asoc, asoc->asoc.primary_destination);
2148 asoc->asoc.state = SCTP_STATE_SHUTDOWN_SENT;
2149 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
2150 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, asoc->sctp_ep, asoc,
2151 asoc->asoc.primary_destination);
2152 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc,
2153 asoc->asoc.primary_destination);
2154 sctp_chunk_output(inp, asoc, SCTP_OUTPUT_FROM_SHUT_TMR);
2157 /* mark into shutdown pending */
2158 struct sctp_stream_queue_pending *sp;
2160 asoc->asoc.state |= SCTP_STATE_SHUTDOWN_PENDING;
2161 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc,
2162 asoc->asoc.primary_destination);
2163 if (asoc->asoc.locked_on_sending) {
2164 sp = TAILQ_LAST(&((asoc->asoc.locked_on_sending)->outqueue),
2167 printf("Error, sp is NULL, locked on sending is %p strm:%d\n",
2168 asoc->asoc.locked_on_sending,
2169 asoc->asoc.locked_on_sending->stream_no);
2171 if ((sp->length == 0) && (sp->msg_is_complete == 0))
2172 asoc->asoc.state |= SCTP_STATE_PARTIAL_MSG_LEFT;
2175 if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
2176 TAILQ_EMPTY(&asoc->asoc.sent_queue) &&
2177 (asoc->asoc.state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
2178 struct mbuf *op_err;
2181 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
2182 0, M_DONTWAIT, 1, MT_DATA);
2188 struct sctp_paramhdr *ph;
2191 SCTP_BUF_LEN(op_err) =
2192 (sizeof(struct sctp_paramhdr) +
2195 struct sctp_paramhdr *);
2196 ph->param_type = htons(
2197 SCTP_CAUSE_USER_INITIATED_ABT);
2198 ph->param_length = htons(SCTP_BUF_LEN(op_err));
2199 ippp = (uint32_t *) (ph + 1);
2200 *ippp = htonl(SCTP_FROM_SCTP_PCB + SCTP_LOC_5);
2202 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_5;
2203 sctp_send_abort_tcb(asoc, op_err);
2204 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
2205 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
2206 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
2207 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
2209 sctp_free_assoc(inp, asoc, SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_6);
2214 SCTP_TCB_UNLOCK(asoc);
2216 /* now is there some left in our SHUTDOWN state? */
2218 SCTP_INP_WUNLOCK(inp);
2219 SCTP_ASOC_CREATE_UNLOCK(inp);
2220 SCTP_INP_INFO_WUNLOCK();
2221 SCTP_ITERATOR_UNLOCK();
2222 #ifdef SCTP_LOG_CLOSING
2223 sctp_log_closing(inp, NULL, 2);
2228 inp->sctp_socket = NULL;
2229 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) !=
2230 SCTP_PCB_FLAGS_UNBOUND) {
2232 * ok, this guy has been bound. It's port is somewhere in
2233 * the sctppcbinfo hash table. Remove it!
2235 LIST_REMOVE(inp, sctp_hash);
2236 inp->sctp_flags |= SCTP_PCB_FLAGS_UNBOUND;
2239 * If there is a timer running to kill us, forget it, since it may
2240 * have a contest on the INP lock.. which would cause us to die ...
2243 for ((asoc = LIST_FIRST(&inp->sctp_asoc_list)); asoc != NULL;
2245 nasoc = LIST_NEXT(asoc, sctp_tcblist);
2246 if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
2250 /* Free associations that are NOT killing us */
2251 SCTP_TCB_LOCK(asoc);
2252 if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_COOKIE_WAIT) &&
2253 ((asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0)) {
2254 struct mbuf *op_err;
2257 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
2258 0, M_DONTWAIT, 1, MT_DATA);
2260 /* Fill in the user initiated abort */
2261 struct sctp_paramhdr *ph;
2263 SCTP_BUF_LEN(op_err) = (sizeof(struct sctp_paramhdr) +
2265 ph = mtod(op_err, struct sctp_paramhdr *);
2266 ph->param_type = htons(
2267 SCTP_CAUSE_USER_INITIATED_ABT);
2268 ph->param_length = htons(SCTP_BUF_LEN(op_err));
2269 ippp = (uint32_t *) (ph + 1);
2270 *ippp = htonl(SCTP_FROM_SCTP_PCB + SCTP_LOC_7);
2273 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_7;
2274 sctp_send_abort_tcb(asoc, op_err);
2275 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
2276 } else if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
2278 SCTP_TCB_UNLOCK(asoc);
2281 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
2282 (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
2283 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
2285 sctp_free_assoc(inp, asoc, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_8);
2288 /* Ok we have someone out there that will kill us */
2289 SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
2290 SCTP_INP_WUNLOCK(inp);
2291 SCTP_ASOC_CREATE_UNLOCK(inp);
2292 SCTP_INP_INFO_WUNLOCK();
2293 SCTP_ITERATOR_UNLOCK();
2294 #ifdef SCTP_LOG_CLOSING
2295 sctp_log_closing(inp, NULL, 3);
2299 if ((inp->refcount) || (inp->sctp_flags & SCTP_PCB_FLAGS_CLOSE_IP)) {
2300 SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
2301 sctp_timer_start(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL);
2302 SCTP_INP_WUNLOCK(inp);
2303 SCTP_ASOC_CREATE_UNLOCK(inp);
2304 SCTP_INP_INFO_WUNLOCK();
2305 SCTP_ITERATOR_UNLOCK();
2306 #ifdef SCTP_LOG_CLOSING
2307 sctp_log_closing(inp, NULL, 4);
2311 SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
2312 inp->sctp_ep.signature_change.type = 0;
2313 inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_ALLGONE;
2315 #ifdef SCTP_LOG_CLOSING
2316 sctp_log_closing(inp, NULL, 5);
2319 SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
2320 inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NONE;
2321 /* Clear the read queue */
2322 while ((sq = TAILQ_FIRST(&inp->read_queue)) != NULL) {
2323 TAILQ_REMOVE(&inp->read_queue, sq, next);
2324 sctp_free_remote_addr(sq->whoFrom);
2326 so->so_rcv.sb_cc -= sq->length;
2328 sctp_m_freem(sq->data);
2332 * no need to free the net count, since at this point all
2335 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_readq, sq);
2336 SCTP_DECR_READQ_COUNT();
2338 /* Now the sctp_pcb things */
2340 * free each asoc if it is not already closed/free. we can't use the
2341 * macro here since le_next will get freed as part of the
2342 * sctp_free_assoc() call.
2347 ipsec4_delete_pcbpolicy(ip_pcb);
2350 /* Unlocks not needed since the socket is gone now */
2352 if (ip_pcb->inp_options) {
2353 (void)sctp_m_free(ip_pcb->inp_options);
2354 ip_pcb->inp_options = 0;
2356 if (ip_pcb->inp_moptions) {
2357 ip_freemoptions(ip_pcb->inp_moptions);
2358 ip_pcb->inp_moptions = 0;
2361 if (ip_pcb->inp_vflag & INP_IPV6) {
2362 struct in6pcb *in6p;
2364 in6p = (struct in6pcb *)inp;
2365 ip6_freepcbopts(in6p->in6p_outputopts);
2368 ip_pcb->inp_vflag = 0;
2369 /* free up authentication fields */
2370 if (inp->sctp_ep.local_auth_chunks != NULL)
2371 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2372 if (inp->sctp_ep.local_hmacs != NULL)
2373 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2375 shared_key = LIST_FIRST(&inp->sctp_ep.shared_keys);
2376 while (shared_key) {
2377 LIST_REMOVE(shared_key, next);
2378 sctp_free_sharedkey(shared_key);
2379 shared_key = LIST_FIRST(&inp->sctp_ep.shared_keys);
2382 inp_save = LIST_NEXT(inp, sctp_list);
2383 LIST_REMOVE(inp, sctp_list);
2385 /* fix any iterators only after out of the list */
2386 sctp_iterator_inp_being_freed(inp, inp_save);
2388 * if we have an address list the following will free the list of
2389 * ifaddr's that are set into this ep. Again macro limitations here,
2390 * since the LIST_FOREACH could be a bad idea.
2392 for ((laddr = LIST_FIRST(&inp->sctp_addr_list)); laddr != NULL;
2394 nladdr = LIST_NEXT(laddr, sctp_nxt_addr);
2395 LIST_REMOVE(laddr, sctp_nxt_addr);
2396 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_laddr, laddr);
2397 SCTP_DECR_LADDR_COUNT();
2400 #ifdef SCTP_TRACK_FREED_ASOCS
2402 for ((asoc = LIST_FIRST(&inp->sctp_asoc_free_list)); asoc != NULL;
2404 nasoc = LIST_NEXT(asoc, sctp_tcblist);
2405 LIST_REMOVE(asoc, sctp_tcblist);
2406 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, asoc);
2407 SCTP_DECR_ASOC_COUNT();
2409 /* *** END TEMP CODE *** */
2411 /* Now lets see about freeing the EP hash table. */
2412 if (inp->sctp_tcbhash != NULL) {
2413 SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark);
2414 inp->sctp_tcbhash = NULL;
2416 /* Now we must put the ep memory back into the zone pool */
2417 SCTP_INP_LOCK_DESTROY(inp);
2418 SCTP_INP_READ_DESTROY(inp);
2419 SCTP_ASOC_CREATE_LOCK_DESTROY(inp);
2420 SCTP_INP_INFO_WUNLOCK();
2422 SCTP_ITERATOR_UNLOCK();
2424 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
2425 SCTP_DECR_EP_COUNT();
2431 sctp_findnet(struct sctp_tcb *stcb, struct sockaddr *addr)
2433 struct sctp_nets *net;
2435 /* locate the address */
2436 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2437 if (sctp_cmpaddr(addr, (struct sockaddr *)&net->ro._l_addr))
2445 * add's a remote endpoint address, done with the INIT/INIT-ACK as well as
2446 * when a ASCONF arrives that adds it. It will also initialize all the cwnd
2450 sctp_is_address_on_local_host(struct sockaddr *addr)
2455 TAILQ_FOREACH(ifn, &ifnet, if_list) {
2456 TAILQ_FOREACH(ifa, &ifn->if_addrlist, ifa_list) {
2457 if (addr->sa_family == ifa->ifa_addr->sa_family) {
2459 if (addr->sa_family == AF_INET) {
2460 struct sockaddr_in *sin, *sin_c;
2462 sin = (struct sockaddr_in *)addr;
2463 sin_c = (struct sockaddr_in *)
2465 if (sin->sin_addr.s_addr ==
2466 sin_c->sin_addr.s_addr) {
2468 * we are on the same
2473 } else if (addr->sa_family == AF_INET6) {
2474 struct sockaddr_in6 *sin6, *sin_c6;
2476 sin6 = (struct sockaddr_in6 *)addr;
2477 sin_c6 = (struct sockaddr_in6 *)
2479 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
2480 &sin_c6->sin6_addr)) {
2482 * we are on the same
2495 sctp_add_remote_addr(struct sctp_tcb *stcb, struct sockaddr *newaddr,
2496 int set_scope, int from)
2499 * The following is redundant to the same lines in the
2500 * sctp_aloc_assoc() but is needed since other's call the add
2503 struct sctp_nets *net, *netfirst;
2507 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
2508 printf("Adding an address (from:%d) to the peer: ", from);
2509 sctp_print_address(newaddr);
2513 netfirst = sctp_findnet(stcb, newaddr);
2516 * Lie and return ok, we don't want to make the association
2517 * go away for this behavior. It will happen in the TCP
2518 * model in a connected socket. It does not reach the hash
2519 * table until after the association is built so it can't be
2520 * found. Mark as reachable, since the initial creation will
2521 * have been cleared and the NOT_IN_ASSOC flag will have
2522 * been added... and we don't want to end up removing it
2525 if (netfirst->dest_state & SCTP_ADDR_UNCONFIRMED) {
2526 netfirst->dest_state = (SCTP_ADDR_REACHABLE |
2527 SCTP_ADDR_UNCONFIRMED);
2529 netfirst->dest_state = SCTP_ADDR_REACHABLE;
2535 if (newaddr->sa_family == AF_INET) {
2536 struct sockaddr_in *sin;
2538 sin = (struct sockaddr_in *)newaddr;
2539 if (sin->sin_addr.s_addr == 0) {
2540 /* Invalid address */
2543 /* zero out the bzero area */
2544 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
2546 /* assure len is set */
2547 sin->sin_len = sizeof(struct sockaddr_in);
2549 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
2550 stcb->ipv4_local_scope = 1;
2552 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
2553 stcb->asoc.ipv4_local_scope = 1;
2555 #endif /* SCTP_DONT_DO_PRIVADDR_SCOPE */
2557 if (sctp_is_address_on_local_host(newaddr)) {
2558 stcb->asoc.loopback_scope = 1;
2559 stcb->asoc.ipv4_local_scope = 1;
2560 stcb->asoc.local_scope = 1;
2561 stcb->asoc.site_scope = 1;
2564 if (from == SCTP_ADDR_IS_CONFIRMED) {
2566 if (sctp_is_address_on_local_host(newaddr)) {
2567 stcb->asoc.loopback_scope = 1;
2568 stcb->asoc.ipv4_local_scope = 1;
2569 stcb->asoc.local_scope = 1;
2570 stcb->asoc.site_scope = 1;
2573 /* Validate the address is in scope */
2574 if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) &&
2575 (stcb->asoc.ipv4_local_scope == 0)) {
2579 } else if (newaddr->sa_family == AF_INET6) {
2580 struct sockaddr_in6 *sin6;
2582 sin6 = (struct sockaddr_in6 *)newaddr;
2583 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2584 /* Invalid address */
2587 /* assure len is set */
2588 sin6->sin6_len = sizeof(struct sockaddr_in6);
2590 if (sctp_is_address_on_local_host(newaddr)) {
2591 stcb->asoc.loopback_scope = 1;
2592 stcb->asoc.local_scope = 1;
2593 stcb->asoc.ipv4_local_scope = 1;
2594 stcb->asoc.site_scope = 1;
2595 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
2597 * If the new destination is a LINK_LOCAL we
2598 * must have common site scope. Don't set
2599 * the local scope since we may not share
2600 * all links, only loopback can do this.
2601 * Links on the local network would also be
2602 * on our private network for v4 too.
2604 stcb->asoc.ipv4_local_scope = 1;
2605 stcb->asoc.site_scope = 1;
2606 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
2608 * If the new destination is SITE_LOCAL then
2609 * we must have site scope in common.
2611 stcb->asoc.site_scope = 1;
2614 if (from == SCTP_ADDR_IS_CONFIRMED) {
2615 /* From connectx so we check for localhost. */
2616 if (sctp_is_address_on_local_host(newaddr)) {
2617 stcb->asoc.loopback_scope = 1;
2618 stcb->asoc.ipv4_local_scope = 1;
2619 stcb->asoc.local_scope = 1;
2620 stcb->asoc.site_scope = 1;
2623 /* Validate the address is in scope */
2624 if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) &&
2625 (stcb->asoc.loopback_scope == 0)) {
2627 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) &&
2628 (stcb->asoc.local_scope == 0)) {
2630 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) &&
2631 (stcb->asoc.site_scope == 0)) {
2636 /* not supported family type */
2639 net = (struct sctp_nets *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_net);
2643 SCTP_INCR_RADDR_COUNT();
2644 bzero(net, sizeof(*net));
2645 memcpy(&net->ro._l_addr, newaddr, newaddr->sa_len);
2646 if (newaddr->sa_family == AF_INET) {
2647 ((struct sockaddr_in *)&net->ro._l_addr)->sin_port = stcb->rport;
2648 } else if (newaddr->sa_family == AF_INET6) {
2649 ((struct sockaddr_in6 *)&net->ro._l_addr)->sin6_port = stcb->rport;
2651 net->addr_is_local = sctp_is_address_on_local_host(newaddr);
2652 net->failure_threshold = stcb->asoc.def_net_failure;
2653 if (addr_inscope == 0) {
2654 net->dest_state = (SCTP_ADDR_REACHABLE |
2655 SCTP_ADDR_OUT_OF_SCOPE);
2657 if (from == SCTP_ADDR_IS_CONFIRMED)
2658 /* SCTP_ADDR_IS_CONFIRMED is passed by connect_x */
2659 net->dest_state = SCTP_ADDR_REACHABLE;
2661 net->dest_state = SCTP_ADDR_REACHABLE |
2662 SCTP_ADDR_UNCONFIRMED;
2664 net->RTO = stcb->asoc.initial_rto;
2665 stcb->asoc.numnets++;
2666 *(&net->ref_count) = 1;
2667 net->tos_flowlabel = 0;
2669 if (newaddr->sa_family == AF_INET)
2670 net->tos_flowlabel = stcb->asoc.default_tos;
2673 if (newaddr->sa_family == AF_INET6)
2674 net->tos_flowlabel = stcb->asoc.default_flowlabel;
2676 /* Init the timer structure */
2677 SCTP_OS_TIMER_INIT(&net->rxt_timer.timer);
2678 SCTP_OS_TIMER_INIT(&net->fr_timer.timer);
2679 SCTP_OS_TIMER_INIT(&net->pmtu_timer.timer);
2681 /* Now generate a route for this guy */
2682 /* KAME hack: embed scopeid */
2683 if (newaddr->sa_family == AF_INET6) {
2684 struct sockaddr_in6 *sin6;
2686 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
2687 (void)sa6_embedscope(sin6, ip6_use_defzone);
2688 sin6->sin6_scope_id = 0;
2690 rtalloc_ign((struct route *)&net->ro, 0UL);
2691 if (newaddr->sa_family == AF_INET6) {
2692 struct sockaddr_in6 *sin6;
2694 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
2695 (void)sa6_recoverscope(sin6);
2697 if ((net->ro.ro_rt) &&
2698 (net->ro.ro_rt->rt_ifp)) {
2699 net->mtu = net->ro.ro_rt->rt_ifp->if_mtu;
2700 if (from == SCTP_ALLOC_ASOC) {
2701 stcb->asoc.smallest_mtu = net->mtu;
2703 /* start things off to match mtu of interface please. */
2704 net->ro.ro_rt->rt_rmx.rmx_mtu = net->ro.ro_rt->rt_ifp->if_mtu;
2706 net->mtu = stcb->asoc.smallest_mtu;
2709 if (stcb->asoc.smallest_mtu > net->mtu) {
2710 stcb->asoc.smallest_mtu = net->mtu;
2713 * We take the max of the burst limit times a MTU or the
2714 * INITIAL_CWND. We then limit this to 4 MTU's of sending.
2716 net->cwnd = min((net->mtu * 4), max((2 * net->mtu), SCTP_INITIAL_CWND));
2718 /* we always get at LEAST 2 MTU's */
2719 if (net->cwnd < (2 * net->mtu)) {
2720 net->cwnd = 2 * net->mtu;
2722 net->ssthresh = stcb->asoc.peers_rwnd;
2724 #if defined(SCTP_CWND_MONITOR) || defined(SCTP_CWND_LOGGING)
2725 sctp_log_cwnd(stcb, net, 0, SCTP_CWND_INITIALIZATION);
2729 * CMT: CUC algo - set find_pseudo_cumack to TRUE (1) at beginning
2730 * of assoc (2005/06/27, iyengar@cis.udel.edu)
2732 net->find_pseudo_cumack = 1;
2733 net->find_rtx_pseudo_cumack = 1;
2734 net->src_addr_selected = 0;
2735 netfirst = TAILQ_FIRST(&stcb->asoc.nets);
2736 if (net->ro.ro_rt == NULL) {
2737 /* Since we have no route put it at the back */
2738 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
2739 } else if (netfirst == NULL) {
2740 /* We are the first one in the pool. */
2741 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
2742 } else if (netfirst->ro.ro_rt == NULL) {
2744 * First one has NO route. Place this one ahead of the first
2747 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
2748 } else if (net->ro.ro_rt->rt_ifp != netfirst->ro.ro_rt->rt_ifp) {
2750 * This one has a different interface than the one at the
2751 * top of the list. Place it ahead.
2753 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
2756 * Ok we have the same interface as the first one. Move
2757 * forward until we find either a) one with a NULL route...
2758 * insert ahead of that b) one with a different ifp.. insert
2759 * after that. c) end of the list.. insert at the tail.
2761 struct sctp_nets *netlook;
2764 netlook = TAILQ_NEXT(netfirst, sctp_next);
2765 if (netlook == NULL) {
2766 /* End of the list */
2767 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net,
2770 } else if (netlook->ro.ro_rt == NULL) {
2771 /* next one has NO route */
2772 TAILQ_INSERT_BEFORE(netfirst, net, sctp_next);
2774 } else if (netlook->ro.ro_rt->rt_ifp !=
2775 net->ro.ro_rt->rt_ifp) {
2776 TAILQ_INSERT_AFTER(&stcb->asoc.nets, netlook,
2782 } while (netlook != NULL);
2785 /* got to have a primary set */
2786 if (stcb->asoc.primary_destination == 0) {
2787 stcb->asoc.primary_destination = net;
2788 } else if ((stcb->asoc.primary_destination->ro.ro_rt == NULL) &&
2790 ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) {
2791 /* No route to current primary adopt new primary */
2792 stcb->asoc.primary_destination = net;
2794 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb,
2796 /* Validate primary is first */
2797 net = TAILQ_FIRST(&stcb->asoc.nets);
2798 if ((net != stcb->asoc.primary_destination) &&
2799 (stcb->asoc.primary_destination)) {
2801 * first one on the list is NOT the primary sctp_cmpaddr()
2802 * is much more efficent if the primary is the first on the
2805 TAILQ_REMOVE(&stcb->asoc.nets,
2806 stcb->asoc.primary_destination, sctp_next);
2807 TAILQ_INSERT_HEAD(&stcb->asoc.nets,
2808 stcb->asoc.primary_destination, sctp_next);
2815 * allocate an association and add it to the endpoint. The caller must be
2816 * careful to add all additional addresses once they are know right away or
2817 * else the assoc will be may experience a blackout scenario.
2820 sctp_aloc_assoc(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
2821 int for_a_init, int *error, uint32_t override_tag)
2823 struct sctp_tcb *stcb;
2824 struct sctp_association *asoc;
2825 struct sctpasochead *head;
2830 * Assumption made here: Caller has done a
2831 * sctp_findassociation_ep_addr(ep, addr's); to make sure the
2832 * address does not exist already.
2834 if (sctppcbinfo.ipi_count_asoc >= SCTP_MAX_NUM_OF_ASOC) {
2835 /* Hit max assoc, sorry no more */
2839 SCTP_INP_RLOCK(inp);
2840 if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
2842 * If its in the TCP pool, its NOT allowed to create an
2843 * association. The parent listener needs to call
2844 * sctp_aloc_assoc.. or the one-2-many socket. If a peeled
2845 * off, or connected one does this.. its an error.
2847 SCTP_INP_RUNLOCK(inp);
2852 if (sctp_debug_on & SCTP_DEBUG_PCB3) {
2853 printf("Allocate an association for peer:");
2855 sctp_print_address(firstaddr);
2859 ntohs(((struct sockaddr_in *)firstaddr)->sin_port));
2861 #endif /* SCTP_DEBUG */
2862 if (firstaddr->sa_family == AF_INET) {
2863 struct sockaddr_in *sin;
2865 sin = (struct sockaddr_in *)firstaddr;
2866 if ((sin->sin_port == 0) || (sin->sin_addr.s_addr == 0)) {
2867 /* Invalid address */
2868 SCTP_INP_RUNLOCK(inp);
2872 rport = sin->sin_port;
2873 } else if (firstaddr->sa_family == AF_INET6) {
2874 struct sockaddr_in6 *sin6;
2876 sin6 = (struct sockaddr_in6 *)firstaddr;
2877 if ((sin6->sin6_port == 0) ||
2878 (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))) {
2879 /* Invalid address */
2880 SCTP_INP_RUNLOCK(inp);
2884 rport = sin6->sin6_port;
2886 /* not supported family type */
2887 SCTP_INP_RUNLOCK(inp);
2891 SCTP_INP_RUNLOCK(inp);
2892 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
2894 * If you have not performed a bind, then we need to do the
2895 * ephemerial bind for you.
2897 if ((err = sctp_inpcb_bind(inp->sctp_socket,
2898 (struct sockaddr *)NULL,
2899 (struct thread *)NULL
2901 /* bind error, probably perm */
2906 stcb = (struct sctp_tcb *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_asoc);
2908 /* out of memory? */
2912 SCTP_INCR_ASOC_COUNT();
2914 bzero(stcb, sizeof(*stcb));
2916 SCTP_TCB_LOCK_INIT(stcb);
2917 SCTP_TCB_SEND_LOCK_INIT(stcb);
2918 /* setup back pointer's */
2919 stcb->sctp_ep = inp;
2920 stcb->sctp_socket = inp->sctp_socket;
2921 if ((err = sctp_init_asoc(inp, asoc, for_a_init, override_tag))) {
2923 SCTP_TCB_LOCK_DESTROY(stcb);
2924 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
2925 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
2926 SCTP_DECR_ASOC_COUNT();
2931 stcb->rport = rport;
2932 SCTP_INP_INFO_WLOCK();
2933 SCTP_INP_WLOCK(inp);
2934 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
2935 /* inpcb freed while alloc going on */
2936 SCTP_TCB_LOCK_DESTROY(stcb);
2937 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
2938 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
2939 SCTP_INP_WUNLOCK(inp);
2940 SCTP_INP_INFO_WUNLOCK();
2941 SCTP_DECR_ASOC_COUNT();
2945 SCTP_TCB_LOCK(stcb);
2947 /* now that my_vtag is set, add it to the hash */
2948 head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
2949 sctppcbinfo.hashasocmark)];
2950 /* put it in the bucket in the vtag hash of assoc's for the system */
2951 LIST_INSERT_HEAD(head, stcb, sctp_asocs);
2952 SCTP_INP_INFO_WUNLOCK();
2954 if ((err = sctp_add_remote_addr(stcb, firstaddr, SCTP_DO_SETSCOPE, SCTP_ALLOC_ASOC))) {
2955 /* failure.. memory error? */
2957 SCTP_FREE(asoc->strmout);
2958 if (asoc->mapping_array)
2959 SCTP_FREE(asoc->mapping_array);
2961 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
2962 SCTP_DECR_ASOC_COUNT();
2963 SCTP_TCB_LOCK_DESTROY(stcb);
2964 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
2968 /* Init all the timers */
2969 SCTP_OS_TIMER_INIT(&asoc->hb_timer.timer);
2970 SCTP_OS_TIMER_INIT(&asoc->dack_timer.timer);
2971 SCTP_OS_TIMER_INIT(&asoc->strreset_timer.timer);
2972 SCTP_OS_TIMER_INIT(&asoc->asconf_timer.timer);
2973 SCTP_OS_TIMER_INIT(&asoc->shut_guard_timer.timer);
2974 SCTP_OS_TIMER_INIT(&asoc->autoclose_timer.timer);
2975 SCTP_OS_TIMER_INIT(&asoc->delayed_event_timer.timer);
2977 LIST_INSERT_HEAD(&inp->sctp_asoc_list, stcb, sctp_tcblist);
2978 /* now file the port under the hash as well */
2979 if (inp->sctp_tcbhash != NULL) {
2980 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(stcb->rport,
2981 inp->sctp_hashmark)];
2982 LIST_INSERT_HEAD(head, stcb, sctp_tcbhash);
2984 SCTP_INP_WUNLOCK(inp);
2986 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
2987 printf("Association %p now allocated\n", stcb);
2995 sctp_remove_net(struct sctp_tcb *stcb, struct sctp_nets *net)
2997 struct sctp_association *asoc;
3001 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
3002 sctp_free_remote_addr(net);
3003 if (net == asoc->primary_destination) {
3005 struct sctp_nets *lnet;
3007 lnet = TAILQ_FIRST(&asoc->nets);
3008 /* Try to find a confirmed primary */
3009 asoc->primary_destination = sctp_find_alternate_net(stcb, lnet,
3012 if (net == asoc->last_data_chunk_from) {
3014 asoc->last_data_chunk_from = TAILQ_FIRST(&asoc->nets);
3016 if (net == asoc->last_control_chunk_from) {
3018 asoc->last_control_chunk_from = NULL;
3020 /* if (net == asoc->asconf_last_sent_to) {*/
3022 /* asoc->asconf_last_sent_to = TAILQ_FIRST(&asoc->nets);*/
3027 * remove a remote endpoint address from an association, it will fail if the
3028 * address does not exist.
3031 sctp_del_remote_addr(struct sctp_tcb *stcb, struct sockaddr *remaddr)
3034 * Here we need to remove a remote address. This is quite simple, we
3035 * first find it in the list of address for the association
3036 * (tasoc->asoc.nets) and then if it is there, we do a LIST_REMOVE
3037 * on that item. Note we do not allow it to be removed if there are
3038 * no other addresses.
3040 struct sctp_association *asoc;
3041 struct sctp_nets *net, *net_tmp;
3045 /* locate the address */
3046 for (net = TAILQ_FIRST(&asoc->nets); net != NULL; net = net_tmp) {
3047 net_tmp = TAILQ_NEXT(net, sctp_next);
3048 if (net->ro._l_addr.sa.sa_family != remaddr->sa_family) {
3051 if (sctp_cmpaddr((struct sockaddr *)&net->ro._l_addr,
3053 /* we found the guy */
3054 if (asoc->numnets < 2) {
3055 /* Must have at LEAST two remote addresses */
3058 sctp_remove_net(stcb, net);
3069 sctp_add_vtag_to_timewait(struct sctp_inpcb *inp, uint32_t tag, uint32_t time)
3071 struct sctpvtaghead *chain;
3072 struct sctp_tagblock *twait_block;
3076 SCTP_GETTIME_TIMEVAL(&now);
3077 chain = &sctppcbinfo.vtag_timewait[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
3079 if (!LIST_EMPTY(chain)) {
3080 /* Block(s) present, lets find space, and expire on the fly */
3081 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
3082 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
3083 if ((twait_block->vtag_block[i].v_tag == 0) &&
3085 twait_block->vtag_block[i].tv_sec_at_expire =
3087 twait_block->vtag_block[i].v_tag = tag;
3089 } else if ((twait_block->vtag_block[i].v_tag) &&
3090 ((long)twait_block->vtag_block[i].tv_sec_at_expire >
3092 /* Audit expires this guy */
3093 twait_block->vtag_block[i].tv_sec_at_expire = 0;
3094 twait_block->vtag_block[i].v_tag = 0;
3096 /* Reuse it for my new tag */
3097 twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec + SCTP_TIME_WAIT;
3098 twait_block->vtag_block[0].v_tag = tag;
3105 * We only do up to the block where we can
3106 * place our tag for audits
3112 /* Need to add a new block to chain */
3114 SCTP_MALLOC(twait_block, struct sctp_tagblock *,
3115 sizeof(struct sctp_tagblock), "TimeWait");
3116 if (twait_block == NULL) {
3119 memset(twait_block, 0, sizeof(struct sctp_tagblock));
3120 LIST_INSERT_HEAD(chain, twait_block, sctp_nxt_tagblock);
3121 twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec +
3123 twait_block->vtag_block[0].v_tag = tag;
3129 sctp_iterator_asoc_being_freed(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
3131 struct sctp_iterator *it;
3134 * Unlock the tcb lock we do this so we avoid a dead lock scenario
3135 * where the iterator is waiting on the TCB lock and the TCB lock is
3136 * waiting on the iterator lock.
3138 it = stcb->asoc.stcb_starting_point_for_iterator;
3142 if (it->inp != stcb->sctp_ep) {
3143 /* hmm, focused on the wrong one? */
3146 if (it->stcb != stcb) {
3149 it->stcb = LIST_NEXT(stcb, sctp_tcblist);
3150 if (it->stcb == NULL) {
3151 /* done with all asoc's in this assoc */
3152 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
3155 it->inp = LIST_NEXT(inp, sctp_list);
3161 * Free the association after un-hashing the remote port.
3164 sctp_free_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int from_inpcbfree, int from_location)
3167 struct sctp_association *asoc;
3168 struct sctp_nets *net, *prev;
3169 struct sctp_laddr *laddr;
3170 struct sctp_tmit_chunk *chk;
3171 struct sctp_asconf_addr *aparam;
3172 struct sctp_stream_reset_list *liste;
3173 struct sctp_queued_to_read *sq;
3174 struct sctp_stream_queue_pending *sp;
3175 sctp_sharedkey_t *shared_key;
3180 /* first, lets purge the entry from the hash table. */
3182 #ifdef SCTP_LOG_CLOSING
3183 sctp_log_closing(inp, stcb, 6);
3185 if (stcb->asoc.state == 0) {
3186 #ifdef SCTP_LOG_CLOSING
3187 sctp_log_closing(inp, NULL, 7);
3189 /* there is no asoc, really TSNH :-0 */
3193 if (stcb->freed_from_where == 0) {
3194 /* Only record the first place free happened from */
3195 stcb->freed_from_where = from_location;
3200 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3201 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
3202 /* nothing around */
3205 so = inp->sctp_socket;
3208 * We used timer based freeing if a reader or writer is in the way.
3209 * So we first check if we are actually being called from a timer,
3210 * if so we abort early if a reader or writer is still in the way.
3212 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) &&
3213 (from_inpcbfree == SCTP_NORMAL_PROC)) {
3215 * is it the timer driving us? if so are the reader/writers
3218 if (stcb->asoc.refcnt) {
3219 /* nope, reader or writer in the way */
3220 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
3221 /* no asoc destroyed */
3222 SCTP_TCB_UNLOCK(stcb);
3223 #ifdef SCTP_LOG_CLOSING
3224 sctp_log_closing(inp, stcb, 8);
3229 /* now clean up any other timers */
3230 SCTP_OS_TIMER_STOP(&asoc->hb_timer.timer);
3231 SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
3232 SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
3233 SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
3234 SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
3235 SCTP_OS_TIMER_STOP(&asoc->shut_guard_timer.timer);
3236 SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
3238 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3239 SCTP_OS_TIMER_STOP(&net->fr_timer.timer);
3240 SCTP_OS_TIMER_STOP(&net->rxt_timer.timer);
3241 SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
3243 /* Now the read queue needs to be cleaned up (only once) */
3245 if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0) {
3246 SCTP_INP_READ_LOCK(inp);
3247 TAILQ_FOREACH(sq, &inp->read_queue, next) {
3248 if (sq->stcb == stcb) {
3249 sq->do_not_ref_stcb = 1;
3250 sq->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
3252 * If there is no end, there never will be
3255 if (sq->end_added == 0) {
3256 /* Held for PD-API clear that. */
3257 sq->pdapi_aborted = 1;
3258 sq->held_length = 0;
3259 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT)) {
3261 * Need to add a PD-API
3262 * aborted indication.
3263 * Setting the control_pdapi
3264 * assures that it will be
3265 * added right after this
3268 stcb->asoc.control_pdapi = sq;
3269 sctp_notify_partial_delivery_indication(stcb,
3270 SCTP_PARTIAL_DELIVERY_ABORTED, 1);
3271 stcb->asoc.control_pdapi = NULL;
3274 /* Add an end to wake them */
3279 SCTP_INP_READ_UNLOCK(inp);
3280 if (stcb->block_entry) {
3282 stcb->block_entry->error = ECONNRESET;
3283 stcb->block_entry = NULL;
3286 stcb->asoc.state |= SCTP_STATE_ABOUT_TO_BE_FREED;
3287 if ((from_inpcbfree != SCTP_PCBFREE_FORCE) && (stcb->asoc.refcnt)) {
3289 * reader or writer in the way, we have hopefully given him
3290 * something to chew on above.
3292 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
3293 SCTP_TCB_UNLOCK(stcb);
3295 SCTP_INP_RLOCK(inp);
3296 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3297 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
3298 /* nothing around */
3301 /* Wake any reader/writers */
3302 sctp_sorwakeup(inp, so);
3303 sctp_sowwakeup(inp, so);
3305 SCTP_INP_RUNLOCK(inp);
3308 #ifdef SCTP_LOG_CLOSING
3309 sctp_log_closing(inp, stcb, 9);
3311 /* no asoc destroyed */
3314 #ifdef SCTP_LOG_CLOSING
3315 sctp_log_closing(inp, stcb, 10);
3318 * When I reach here, no others want to kill the assoc yet.. and I
3319 * own the lock. Now its possible an abort comes in when I do the
3320 * lock exchange below to grab all the locks to do the final take
3321 * out. to prevent this we increment the count, which will start a
3322 * timer and blow out above thus assuring us that we hold exclusive
3323 * killing of the asoc. Note that after getting back the TCB lock we
3324 * will go ahead and increment the counter back up and stop any
3325 * timer a passing stranger may have started :-S
3327 if (from_inpcbfree == SCTP_NORMAL_PROC) {
3328 atomic_add_int(&stcb->asoc.refcnt, 1);
3330 SCTP_TCB_UNLOCK(stcb);
3332 SCTP_ITERATOR_LOCK();
3333 SCTP_INP_INFO_WLOCK();
3334 SCTP_INP_WLOCK(inp);
3335 SCTP_TCB_LOCK(stcb);
3337 /* Double check the GONE flag */
3338 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3339 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
3340 /* nothing around */
3343 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3344 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3346 * For TCP type we need special handling when we are
3347 * connected. We also include the peel'ed off ones to.
3349 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3350 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
3351 inp->sctp_flags |= SCTP_PCB_FLAGS_WAS_CONNECTED;
3354 if (so->so_rcv.sb_cc == 0) {
3355 so->so_state &= ~(SS_ISCONNECTING |
3356 SS_ISDISCONNECTING |
3361 sctp_sowwakeup(inp, so);
3362 sctp_sorwakeup(inp, so);
3363 wakeup(&so->so_timeo);
3368 * Make it invalid too, that way if its about to run it will abort
3371 sctp_iterator_asoc_being_freed(inp, stcb);
3372 /* re-increment the lock */
3373 if (from_inpcbfree == SCTP_NORMAL_PROC) {
3374 atomic_add_int(&stcb->asoc.refcnt, -1);
3377 if (inp->sctp_tcbhash) {
3378 LIST_REMOVE(stcb, sctp_tcbhash);
3380 if (stcb->asoc.in_restart_hash) {
3381 LIST_REMOVE(stcb, sctp_tcbrestarhash);
3383 /* Now lets remove it from the list of ALL associations in the EP */
3384 LIST_REMOVE(stcb, sctp_tcblist);
3385 if (from_inpcbfree == SCTP_NORMAL_PROC) {
3386 SCTP_INP_INCR_REF(inp);
3387 SCTP_INP_WUNLOCK(inp);
3388 SCTP_ITERATOR_UNLOCK();
3390 /* pull from vtag hash */
3391 LIST_REMOVE(stcb, sctp_asocs);
3392 sctp_add_vtag_to_timewait(inp, asoc->my_vtag, SCTP_TIME_WAIT);
3396 * Now restop the timers to be sure - this is paranoia at is finest!
3398 SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
3399 SCTP_OS_TIMER_STOP(&asoc->hb_timer.timer);
3400 SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
3401 SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
3402 SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
3403 SCTP_OS_TIMER_STOP(&asoc->shut_guard_timer.timer);
3404 SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
3405 SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
3407 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3408 SCTP_OS_TIMER_STOP(&net->fr_timer.timer);
3409 SCTP_OS_TIMER_STOP(&net->rxt_timer.timer);
3410 SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
3413 asoc->strreset_timer.type = SCTP_TIMER_TYPE_NONE;
3416 * The chunk lists and such SHOULD be empty but we check them just
3419 /* anything on the wheel needs to be removed */
3420 for (i = 0; i < asoc->streamoutcnt; i++) {
3421 struct sctp_stream_out *outs;
3423 outs = &asoc->strmout[i];
3424 /* now clean up any chunks here */
3425 sp = TAILQ_FIRST(&outs->outqueue);
3427 TAILQ_REMOVE(&outs->outqueue, sp, next);
3429 sctp_m_freem(sp->data);
3431 sp->tail_mbuf = NULL;
3433 sctp_free_remote_addr(sp->net);
3434 sctp_free_spbufspace(stcb, asoc, sp);
3435 /* Free the zone stuff */
3436 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_strmoq, sp);
3437 SCTP_DECR_STRMOQ_COUNT();
3438 sp = TAILQ_FIRST(&outs->outqueue);
3442 while ((sp = TAILQ_FIRST(&asoc->free_strmoq)) != NULL) {
3443 TAILQ_REMOVE(&asoc->free_strmoq, sp, next);
3445 sctp_m_freem(sp->data);
3447 sp->tail_mbuf = NULL;
3449 /* Free the zone stuff */
3450 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_strmoq, sp);
3451 SCTP_DECR_STRMOQ_COUNT();
3452 atomic_add_int(&sctppcbinfo.ipi_free_strmoq, -1);
3455 while ((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) {
3456 TAILQ_REMOVE(&asoc->resetHead, liste, next_resp);
3460 sq = TAILQ_FIRST(&asoc->pending_reply_queue);
3462 TAILQ_REMOVE(&asoc->pending_reply_queue, sq, next);
3464 sctp_m_freem(sq->data);
3467 sctp_free_remote_addr(sq->whoFrom);
3470 /* Free the ctl entry */
3471 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_readq, sq);
3472 SCTP_DECR_READQ_COUNT();
3473 sq = TAILQ_FIRST(&asoc->pending_reply_queue);
3476 chk = TAILQ_FIRST(&asoc->free_chunks);
3478 TAILQ_REMOVE(&asoc->free_chunks, chk, sctp_next);
3480 sctp_m_freem(chk->data);
3484 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3485 SCTP_DECR_CHK_COUNT();
3486 atomic_subtract_int(&sctppcbinfo.ipi_free_chunks, 1);
3487 asoc->free_chunk_cnt--;
3488 chk = TAILQ_FIRST(&asoc->free_chunks);
3490 /* pending send queue SHOULD be empty */
3491 if (!TAILQ_EMPTY(&asoc->send_queue)) {
3492 chk = TAILQ_FIRST(&asoc->send_queue);
3494 TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
3496 sctp_m_freem(chk->data);
3500 sctp_free_remote_addr(chk->whoTo);
3501 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3502 SCTP_DECR_CHK_COUNT();
3503 chk = TAILQ_FIRST(&asoc->send_queue);
3508 printf("Freed %d from send_queue\n", ccnt);
3512 /* sent queue SHOULD be empty */
3513 if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3514 chk = TAILQ_FIRST(&asoc->sent_queue);
3516 TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
3518 sctp_m_freem(chk->data);
3522 sctp_free_remote_addr(chk->whoTo);
3523 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3524 SCTP_DECR_CHK_COUNT();
3525 chk = TAILQ_FIRST(&asoc->sent_queue);
3530 printf("Freed %d from sent_queue\n", ccnt);
3534 /* control queue MAY not be empty */
3535 if (!TAILQ_EMPTY(&asoc->control_send_queue)) {
3536 chk = TAILQ_FIRST(&asoc->control_send_queue);
3538 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
3540 sctp_m_freem(chk->data);
3544 sctp_free_remote_addr(chk->whoTo);
3545 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3546 SCTP_DECR_CHK_COUNT();
3547 chk = TAILQ_FIRST(&asoc->control_send_queue);
3552 printf("Freed %d from ctrl_queue\n", ccnt);
3556 if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
3557 chk = TAILQ_FIRST(&asoc->reasmqueue);
3559 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
3561 sctp_m_freem(chk->data);
3564 sctp_free_remote_addr(chk->whoTo);
3566 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3567 SCTP_DECR_CHK_COUNT();
3568 chk = TAILQ_FIRST(&asoc->reasmqueue);
3573 printf("Freed %d from reasm_queue\n", ccnt);
3577 if (asoc->mapping_array) {
3578 SCTP_FREE(asoc->mapping_array);
3579 asoc->mapping_array = NULL;
3581 /* the stream outs */
3582 if (asoc->strmout) {
3583 SCTP_FREE(asoc->strmout);
3584 asoc->strmout = NULL;
3586 asoc->streamoutcnt = 0;
3588 struct sctp_queued_to_read *ctl;
3591 for (i = 0; i < asoc->streamincnt; i++) {
3592 if (!TAILQ_EMPTY(&asoc->strmin[i].inqueue)) {
3593 /* We have somethings on the streamin queue */
3594 ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue);
3596 TAILQ_REMOVE(&asoc->strmin[i].inqueue,
3598 sctp_free_remote_addr(ctl->whoFrom);
3600 sctp_m_freem(ctl->data);
3604 * We don't free the address here
3605 * since all the net's were freed
3608 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_readq, ctl);
3609 SCTP_DECR_READQ_COUNT();
3610 ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue);
3614 SCTP_FREE(asoc->strmin);
3615 asoc->strmin = NULL;
3617 asoc->streamincnt = 0;
3618 while (!TAILQ_EMPTY(&asoc->nets)) {
3619 net = TAILQ_FIRST(&asoc->nets);
3620 /* pull from list */
3621 if ((sctppcbinfo.ipi_count_raddr == 0) || (prev == net)) {
3623 panic("no net's left alloc'ed, or list points to itself");
3628 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
3629 sctp_free_remote_addr(net);
3632 /* local addresses, if any */
3633 while (!LIST_EMPTY(&asoc->sctp_local_addr_list)) {
3634 laddr = LIST_FIRST(&asoc->sctp_local_addr_list);
3635 LIST_REMOVE(laddr, sctp_nxt_addr);
3636 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_laddr, laddr);
3637 SCTP_DECR_LADDR_COUNT();
3639 /* pending asconf (address) parameters */
3640 while (!TAILQ_EMPTY(&asoc->asconf_queue)) {
3641 aparam = TAILQ_FIRST(&asoc->asconf_queue);
3642 TAILQ_REMOVE(&asoc->asconf_queue, aparam, next);
3645 if (asoc->last_asconf_ack_sent != NULL) {
3646 sctp_m_freem(asoc->last_asconf_ack_sent);
3647 asoc->last_asconf_ack_sent = NULL;
3649 /* clean up auth stuff */
3650 if (asoc->local_hmacs)
3651 sctp_free_hmaclist(asoc->local_hmacs);
3652 if (asoc->peer_hmacs)
3653 sctp_free_hmaclist(asoc->peer_hmacs);
3655 if (asoc->local_auth_chunks)
3656 sctp_free_chunklist(asoc->local_auth_chunks);
3657 if (asoc->peer_auth_chunks)
3658 sctp_free_chunklist(asoc->peer_auth_chunks);
3660 sctp_free_authinfo(&asoc->authinfo);
3662 shared_key = LIST_FIRST(&asoc->shared_keys);
3663 while (shared_key) {
3664 LIST_REMOVE(shared_key, next);
3665 sctp_free_sharedkey(shared_key);
3666 shared_key = LIST_FIRST(&asoc->shared_keys);
3669 /* Insert new items here :> */
3671 /* Get rid of LOCK */
3672 SCTP_TCB_LOCK_DESTROY(stcb);
3673 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
3674 if (from_inpcbfree == SCTP_NORMAL_PROC) {
3675 SCTP_INP_INFO_WUNLOCK();
3676 SCTP_INP_RLOCK(inp);
3678 #ifdef SCTP_TRACK_FREED_ASOCS
3679 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3680 /* now clean up the tasoc itself */
3681 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
3682 SCTP_DECR_ASOC_COUNT();
3684 LIST_INSERT_HEAD(&inp->sctp_asoc_free_list, stcb, sctp_tcblist);
3687 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
3688 SCTP_DECR_ASOC_COUNT();
3690 if (from_inpcbfree == SCTP_NORMAL_PROC) {
3691 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3693 * If its NOT the inp_free calling us AND sctp_close
3694 * as been called, we call back...
3696 SCTP_INP_RUNLOCK(inp);
3698 * This will start the kill timer (if we are the
3699 * lastone) since we hold an increment yet. But this
3700 * is the only safe way to do this since otherwise
3701 * if the socket closes at the same time we are here
3702 * we might collide in the cleanup.
3704 sctp_inpcb_free(inp, 0, 0);
3705 SCTP_INP_DECR_REF(inp);
3708 /* The socket is still open. */
3709 SCTP_INP_DECR_REF(inp);
3712 if (from_inpcbfree == SCTP_NORMAL_PROC) {
3713 SCTP_INP_RUNLOCK(inp);
3716 /* destroyed the asoc */
3717 #ifdef SCTP_LOG_CLOSING
3718 sctp_log_closing(inp, NULL, 11);
3726 * determine if a destination is "reachable" based upon the addresses bound
3727 * to the current endpoint (e.g. only v4 or v6 currently bound)
3730 * FIX: if we allow assoc-level bindx(), then this needs to be fixed to use
3731 * assoc level v4/v6 flags, as the assoc *may* not have the same address
3732 * types bound as its endpoint
3735 sctp_destination_is_reachable(struct sctp_tcb *stcb, struct sockaddr *destaddr)
3737 struct sctp_inpcb *inp;
3741 * No locks here, the TCB, in all cases is already locked and an
3742 * assoc is up. There is either a INP lock by the caller applied (in
3743 * asconf case when deleting an address) or NOT in the HB case,
3744 * however if HB then the INP increment is up and the INP will not
3745 * be removed (on top of the fact that we have a TCB lock). So we
3746 * only want to read the sctp_flags, which is either bound-all or
3747 * not.. no protection needed since once an assoc is up you can't be
3748 * changing your binding.
3750 inp = stcb->sctp_ep;
3751 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3752 /* if bound all, destination is not restricted */
3754 * RRS: Question during lock work: Is this correct? If you
3755 * are bound-all you still might need to obey the V4--V6
3756 * flags??? IMO this bound-all stuff needs to be removed!
3760 /* NOTE: all "scope" checks are done when local addresses are added */
3761 if (destaddr->sa_family == AF_INET6) {
3762 answer = inp->ip_inp.inp.inp_vflag & INP_IPV6;
3763 } else if (destaddr->sa_family == AF_INET) {
3764 answer = inp->ip_inp.inp.inp_vflag & INP_IPV4;
3766 /* invalid family, so it's unreachable */
3773 * update the inp_vflags on an endpoint
3776 sctp_update_ep_vflag(struct sctp_inpcb *inp)
3778 struct sctp_laddr *laddr;
3780 /* first clear the flag */
3781 inp->ip_inp.inp.inp_vflag = 0;
3782 /* set the flag based on addresses on the ep list */
3783 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3784 if (laddr->ifa == NULL) {
3786 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
3787 printf("An ounce of prevention is worth a pound of cure\n");
3789 #endif /* SCTP_DEBUG */
3792 if (laddr->ifa->ifa_addr) {
3795 if (laddr->ifa->ifa_addr->sa_family == AF_INET6) {
3796 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
3797 } else if (laddr->ifa->ifa_addr->sa_family == AF_INET) {
3798 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
3804 * Add the address to the endpoint local address list There is nothing to be
3805 * done if we are bound to all addresses
3808 sctp_add_local_addr_ep(struct sctp_inpcb *inp, struct ifaddr *ifa)
3810 struct sctp_laddr *laddr;
3815 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3816 /* You are already bound to all. You have it already */
3819 if (ifa->ifa_addr->sa_family == AF_INET6) {
3820 struct in6_ifaddr *ifa6;
3822 ifa6 = (struct in6_ifaddr *)ifa;
3823 if (ifa6->ia6_flags & (IN6_IFF_DETACHED |
3824 IN6_IFF_DEPRECATED | IN6_IFF_ANYCAST | IN6_IFF_NOTREADY))
3825 /* Can't bind a non-existent addr. */
3828 /* first, is it already present? */
3829 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3830 if (laddr->ifa == ifa) {
3836 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) && (fnd == 0)) {
3837 /* Not bound to all */
3838 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa);
3842 /* update inp_vflag flags */
3843 if (ifa->ifa_addr->sa_family == AF_INET6) {
3844 inp->ip_inp.inp.inp_vflag |= INP_IPV6;
3845 } else if (ifa->ifa_addr->sa_family == AF_INET) {
3846 inp->ip_inp.inp.inp_vflag |= INP_IPV4;
3854 * select a new (hopefully reachable) destination net (should only be used
3855 * when we deleted an ep addr that is the only usable source address to reach
3856 * the destination net)
3859 sctp_select_primary_destination(struct sctp_tcb *stcb)
3861 struct sctp_nets *net;
3863 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3864 /* for now, we'll just pick the first reachable one we find */
3865 if (net->dest_state & SCTP_ADDR_UNCONFIRMED)
3867 if (sctp_destination_is_reachable(stcb,
3868 (struct sockaddr *)&net->ro._l_addr)) {
3869 /* found a reachable destination */
3870 stcb->asoc.primary_destination = net;
3873 /* I can't there from here! ...we're gonna die shortly... */
3878 * Delete the address from the endpoint local address list There is nothing
3879 * to be done if we are bound to all addresses
3882 sctp_del_local_addr_ep(struct sctp_inpcb *inp, struct ifaddr *ifa)
3884 struct sctp_laddr *laddr;
3888 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3889 /* You are already bound to all. You have it already */
3892 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3893 if (laddr->ifa == ifa) {
3898 if (fnd && (inp->laddr_count < 2)) {
3899 /* can't delete unless there are at LEAST 2 addresses */
3902 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) && (fnd)) {
3904 * clean up any use of this address go through our
3905 * associations and clear any last_used_address that match
3906 * this one for each assoc, see if a new primary_destination
3909 struct sctp_tcb *stcb;
3911 /* clean up "next_addr_touse" */
3912 if (inp->next_addr_touse == laddr)
3913 /* delete this address */
3914 inp->next_addr_touse = NULL;
3916 /* clean up "last_used_address" */
3917 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
3918 if (stcb->asoc.last_used_address == laddr)
3919 /* delete this address */
3920 stcb->asoc.last_used_address = NULL;
3921 } /* for each tcb */
3923 /* remove it from the ep list */
3924 sctp_remove_laddr(laddr);
3926 /* update inp_vflag flags */
3927 sctp_update_ep_vflag(inp);
3928 /* select a new primary destination if needed */
3929 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
3931 * presume caller (sctp_asconf.c) already owns INP
3934 SCTP_TCB_LOCK(stcb);
3935 if (sctp_destination_is_reachable(stcb,
3936 (struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr) == 0) {
3937 sctp_select_primary_destination(stcb);
3939 SCTP_TCB_UNLOCK(stcb);
3940 } /* for each tcb */
3946 * Add the addr to the TCB local address list For the BOUNDALL or dynamic
3947 * case, this is a "pending" address list (eg. addresses waiting for an
3948 * ASCONF-ACK response) For the subset binding, static case, this is a
3949 * "valid" address list
3952 sctp_add_local_addr_assoc(struct sctp_tcb *stcb, struct ifaddr *ifa)
3954 struct sctp_inpcb *inp;
3955 struct sctp_laddr *laddr;
3959 * Assumes TCP is locked.. and possiblye the INP. May need to
3960 * confirm/fix that if we need it and is not the case.
3962 inp = stcb->sctp_ep;
3963 if (ifa->ifa_addr->sa_family == AF_INET6) {
3964 struct in6_ifaddr *ifa6;
3966 ifa6 = (struct in6_ifaddr *)ifa;
3967 if (ifa6->ia6_flags & (IN6_IFF_DETACHED |
3968 /* IN6_IFF_DEPRECATED | */
3971 /* Can't bind a non-existent addr. */
3974 /* does the address already exist? */
3975 LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list, sctp_nxt_addr) {
3976 if (laddr->ifa == ifa) {
3981 /* add to the list */
3982 error = sctp_insert_laddr(&stcb->asoc.sctp_local_addr_list, ifa);
3989 * insert an laddr entry with the given ifa for the desired list
3992 sctp_insert_laddr(struct sctpladdr *list, struct ifaddr *ifa)
3994 struct sctp_laddr *laddr;
3996 laddr = (struct sctp_laddr *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_laddr);
3997 if (laddr == NULL) {
3998 /* out of memory? */
4001 SCTP_INCR_LADDR_COUNT();
4002 bzero(laddr, sizeof(*laddr));
4005 LIST_INSERT_HEAD(list, laddr, sctp_nxt_addr);
4011 * Remove an laddr entry from the local address list (on an assoc)
4014 sctp_remove_laddr(struct sctp_laddr *laddr)
4017 /* remove from the list */
4018 LIST_REMOVE(laddr, sctp_nxt_addr);
4019 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_laddr, laddr);
4020 SCTP_DECR_LADDR_COUNT();
4024 * Remove an address from the TCB local address list
4027 sctp_del_local_addr_assoc(struct sctp_tcb *stcb, struct ifaddr *ifa)
4029 struct sctp_inpcb *inp;
4030 struct sctp_laddr *laddr;
4033 * This is called by asconf work. It is assumed that a) The TCB is
4034 * locked and b) The INP is locked. This is true in as much as I can
4035 * trace through the entry asconf code where I did these locks.
4036 * Again, the ASCONF code is a bit different in that it does lock
4037 * the INP during its work often times. This must be since we don't
4038 * want other proc's looking up things while what they are looking
4039 * up is changing :-D
4042 inp = stcb->sctp_ep;
4043 /* if subset bound and don't allow ASCONF's, can't delete last */
4044 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) &&
4045 (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF) == 0)) {
4046 if (stcb->asoc.numnets < 2) {
4047 /* can't delete last address */
4051 LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list, sctp_nxt_addr) {
4052 /* remove the address if it exists */
4053 if (laddr->ifa == NULL)
4055 if (laddr->ifa == ifa) {
4056 sctp_remove_laddr(laddr);
4061 /* address not found! */
4066 * Remove an address from the TCB local address list lookup using a sockaddr
4070 sctp_del_local_addr_assoc_sa(struct sctp_tcb *stcb, struct sockaddr *sa)
4072 struct sctp_inpcb *inp;
4073 struct sctp_laddr *laddr;
4074 struct sockaddr *l_sa;
4077 * This function I find does not seem to have a caller. As such we
4078 * NEED TO DELETE this code. If we do find a caller, the caller MUST
4079 * have locked the TCB at the least and probably the INP as well.
4081 inp = stcb->sctp_ep;
4082 /* if subset bound and don't allow ASCONF's, can't delete last */
4083 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) &&
4084 (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF) == 0)) {
4085 if (stcb->asoc.numnets < 2) {
4086 /* can't delete last address */
4090 LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list, sctp_nxt_addr) {
4091 /* make sure the address exists */
4092 if (laddr->ifa == NULL)
4094 if (laddr->ifa->ifa_addr == NULL)
4097 l_sa = laddr->ifa->ifa_addr;
4098 if (l_sa->sa_family == AF_INET6) {
4100 struct sockaddr_in6 *sin1, *sin2;
4102 sin1 = (struct sockaddr_in6 *)l_sa;
4103 sin2 = (struct sockaddr_in6 *)sa;
4104 if (memcmp(&sin1->sin6_addr, &sin2->sin6_addr,
4105 sizeof(struct in6_addr)) == 0) {
4107 sctp_remove_laddr(laddr);
4110 } else if (l_sa->sa_family == AF_INET) {
4112 struct sockaddr_in *sin1, *sin2;
4114 sin1 = (struct sockaddr_in *)l_sa;
4115 sin2 = (struct sockaddr_in *)sa;
4116 if (sin1->sin_addr.s_addr == sin2->sin_addr.s_addr) {
4118 sctp_remove_laddr(laddr);
4122 /* invalid family */
4126 /* address not found! */
4130 static char sctp_pcb_initialized = 0;
4133 * Temporarily remove for __APPLE__ until we use the Tiger equivalents
4136 static int sctp_max_number_of_assoc = SCTP_MAX_NUM_OF_ASOC;
4137 static int sctp_scale_up_for_address = SCTP_SCALE_FOR_ADDR;
4143 * SCTP initialization for the PCB structures should be called by
4144 * the sctp_init() funciton.
4148 if (sctp_pcb_initialized != 0) {
4149 /* error I was called twice */
4152 sctp_pcb_initialized = 1;
4154 bzero(&sctpstat, sizeof(struct sctpstat));
4156 /* init the empty list of (All) Endpoints */
4157 LIST_INIT(&sctppcbinfo.listhead);
4159 /* init the iterator head */
4160 LIST_INIT(&sctppcbinfo.iteratorhead);
4162 /* init the hash table of endpoints */
4163 TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", &sctp_hashtblsize);
4164 TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", &sctp_pcbtblsize);
4165 TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", &sctp_chunkscale);
4166 sctppcbinfo.sctp_asochash = SCTP_HASH_INIT((sctp_hashtblsize * 31),
4167 &sctppcbinfo.hashasocmark);
4168 sctppcbinfo.sctp_ephash = SCTP_HASH_INIT(sctp_hashtblsize,
4169 &sctppcbinfo.hashmark);
4170 sctppcbinfo.sctp_tcpephash = SCTP_HASH_INIT(sctp_hashtblsize,
4171 &sctppcbinfo.hashtcpmark);
4172 sctppcbinfo.hashtblsize = sctp_hashtblsize;
4174 /* init the small hash table we use to track restarted asoc's */
4175 sctppcbinfo.sctp_restarthash = SCTP_HASH_INIT(SCTP_STACK_VTAG_HASH_SIZE,
4176 &sctppcbinfo.hashrestartmark);
4178 /* init the zones */
4180 * FIX ME: Should check for NULL returns, but if it does fail we are
4181 * doomed to panic anyways... add later maybe.
4183 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_ep, "sctp_ep",
4184 sizeof(struct sctp_inpcb), maxsockets);
4186 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_asoc, "sctp_asoc",
4187 sizeof(struct sctp_tcb), sctp_max_number_of_assoc);
4189 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_laddr, "sctp_laddr",
4190 sizeof(struct sctp_laddr),
4191 (sctp_max_number_of_assoc * sctp_scale_up_for_address));
4193 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_net, "sctp_raddr",
4194 sizeof(struct sctp_nets),
4195 (sctp_max_number_of_assoc * sctp_scale_up_for_address));
4197 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_chunk, "sctp_chunk",
4198 sizeof(struct sctp_tmit_chunk),
4199 (sctp_max_number_of_assoc * sctp_chunkscale));
4201 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_readq, "sctp_readq",
4202 sizeof(struct sctp_queued_to_read),
4203 (sctp_max_number_of_assoc * sctp_chunkscale));
4205 SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_strmoq, "sctp_stream_msg_out",
4206 sizeof(struct sctp_stream_queue_pending),
4207 (sctp_max_number_of_assoc * sctp_chunkscale));
4209 /* Master Lock INIT for info structure */
4210 SCTP_INP_INFO_LOCK_INIT();
4211 SCTP_STATLOG_INIT_LOCK();
4212 SCTP_ITERATOR_LOCK_INIT();
4214 SCTP_IPI_COUNT_INIT();
4215 SCTP_IPI_ADDR_INIT();
4216 LIST_INIT(&sctppcbinfo.addr_wq);
4218 /* not sure if we need all the counts */
4219 sctppcbinfo.ipi_count_ep = 0;
4220 /* assoc/tcb zone info */
4221 sctppcbinfo.ipi_count_asoc = 0;
4222 /* local addrlist zone info */
4223 sctppcbinfo.ipi_count_laddr = 0;
4224 /* remote addrlist zone info */
4225 sctppcbinfo.ipi_count_raddr = 0;
4227 sctppcbinfo.ipi_count_chunk = 0;
4229 /* socket queue zone info */
4230 sctppcbinfo.ipi_count_readq = 0;
4232 /* stream out queue cont */
4233 sctppcbinfo.ipi_count_strmoq = 0;
4235 sctppcbinfo.ipi_free_strmoq = 0;
4236 sctppcbinfo.ipi_free_chunks = 0;
4238 SCTP_OS_TIMER_INIT(&sctppcbinfo.addr_wq_timer.timer);
4240 /* Init the TIMEWAIT list */
4241 for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) {
4242 LIST_INIT(&sctppcbinfo.vtag_timewait[i]);
4249 sctp_load_addresses_from_init(struct sctp_tcb *stcb, struct mbuf *m,
4250 int iphlen, int offset, int limit, struct sctphdr *sh,
4251 struct sockaddr *altsa)
4254 * grub through the INIT pulling addresses and loading them to the
4255 * nets structure in the asoc. The from address in the mbuf should
4256 * also be loaded (if it is not already). This routine can be called
4257 * with either INIT or INIT-ACK's as long as the m points to the IP
4258 * packet and the offset points to the beginning of the parameters.
4260 struct sctp_inpcb *inp, *l_inp;
4261 struct sctp_nets *net, *net_tmp;
4263 struct sctp_paramhdr *phdr, parm_buf;
4264 struct sctp_tcb *stcb_tmp;
4265 uint16_t ptype, plen;
4266 struct sockaddr *sa;
4267 struct sockaddr_storage dest_store;
4268 struct sockaddr *local_sa = (struct sockaddr *)&dest_store;
4269 struct sockaddr_in sin;
4270 struct sockaddr_in6 sin6;
4272 struct sctp_auth_random *random = NULL;
4273 uint16_t random_len = 0;
4274 struct sctp_auth_hmac_algo *hmacs = NULL;
4275 uint16_t hmacs_len = 0;
4276 struct sctp_auth_chunk_list *chunks = NULL;
4277 uint16_t num_chunks = 0;
4278 sctp_key_t *new_key;
4280 int got_random = 0, got_hmacs = 0, got_chklist = 0;
4282 /* First get the destination address setup too. */
4283 memset(&sin, 0, sizeof(sin));
4284 memset(&sin6, 0, sizeof(sin6));
4286 sin.sin_family = AF_INET;
4287 sin.sin_len = sizeof(sin);
4288 sin.sin_port = stcb->rport;
4290 sin6.sin6_family = AF_INET6;
4291 sin6.sin6_len = sizeof(struct sockaddr_in6);
4292 sin6.sin6_port = stcb->rport;
4293 if (altsa == NULL) {
4294 iph = mtod(m, struct ip *);
4295 if (iph->ip_v == IPVERSION) {
4297 struct sockaddr_in *sin_2;
4299 sin_2 = (struct sockaddr_in *)(local_sa);
4300 memset(sin_2, 0, sizeof(sin));
4301 sin_2->sin_family = AF_INET;
4302 sin_2->sin_len = sizeof(sin);
4303 sin_2->sin_port = sh->dest_port;
4304 sin_2->sin_addr.s_addr = iph->ip_dst.s_addr;
4305 sin.sin_addr = iph->ip_src;
4306 sa = (struct sockaddr *)&sin;
4307 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
4309 struct ip6_hdr *ip6;
4310 struct sockaddr_in6 *sin6_2;
4312 ip6 = mtod(m, struct ip6_hdr *);
4313 sin6_2 = (struct sockaddr_in6 *)(local_sa);
4314 memset(sin6_2, 0, sizeof(sin6));
4315 sin6_2->sin6_family = AF_INET6;
4316 sin6_2->sin6_len = sizeof(struct sockaddr_in6);
4317 sin6_2->sin6_port = sh->dest_port;
4318 sin6.sin6_addr = ip6->ip6_src;
4319 sa = (struct sockaddr *)&sin6;
4325 * For cookies we use the src address NOT from the packet
4326 * but from the original INIT
4330 /* Turn off ECN until we get through all params */
4331 stcb->asoc.ecn_allowed = 0;
4332 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4333 /* mark all addresses that we have currently on the list */
4334 net->dest_state |= SCTP_ADDR_NOT_IN_ASSOC;
4336 /* does the source address already exist? if so skip it */
4337 l_inp = inp = stcb->sctp_ep;
4339 atomic_add_int(&stcb->asoc.refcnt, 1);
4340 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net_tmp, local_sa, stcb);
4341 atomic_add_int(&stcb->asoc.refcnt, -1);
4343 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || inp == NULL) {
4344 /* we must add the source address */
4345 /* no scope set here since we have a tcb already. */
4346 if ((sa->sa_family == AF_INET) &&
4347 (stcb->asoc.ipv4_addr_legal)) {
4348 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_2)) {
4351 } else if ((sa->sa_family == AF_INET6) &&
4352 (stcb->asoc.ipv6_addr_legal)) {
4353 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) {
4358 if (net_tmp != NULL && stcb_tmp == stcb) {
4359 net_tmp->dest_state &= ~SCTP_ADDR_NOT_IN_ASSOC;
4360 } else if (stcb_tmp != stcb) {
4361 /* It belongs to another association? */
4362 SCTP_TCB_UNLOCK(stcb_tmp);
4366 if (stcb->asoc.state == 0) {
4367 /* the assoc was freed? */
4370 /* now we must go through each of the params. */
4371 phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
4373 ptype = ntohs(phdr->param_type);
4374 plen = ntohs(phdr->param_length);
4376 * printf("ptype => %0x, plen => %d\n", (uint32_t)ptype,
4379 if (offset + plen > limit) {
4385 if (ptype == SCTP_IPV4_ADDRESS) {
4386 if (stcb->asoc.ipv4_addr_legal) {
4387 struct sctp_ipv4addr_param *p4, p4_buf;
4389 /* ok get the v4 address and check/add */
4390 phdr = sctp_get_next_param(m, offset,
4391 (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
4392 if (plen != sizeof(struct sctp_ipv4addr_param) ||
4396 p4 = (struct sctp_ipv4addr_param *)phdr;
4397 sin.sin_addr.s_addr = p4->addr;
4398 sa = (struct sockaddr *)&sin;
4399 inp = stcb->sctp_ep;
4400 atomic_add_int(&stcb->asoc.refcnt, 1);
4401 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
4403 atomic_add_int(&stcb->asoc.refcnt, -1);
4405 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) ||
4407 /* we must add the source address */
4409 * no scope set since we have a tcb
4414 * we must validate the state again
4417 if (stcb->asoc.state == 0) {
4418 /* the assoc was freed? */
4421 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_4)) {
4424 } else if (stcb_tmp == stcb) {
4425 if (stcb->asoc.state == 0) {
4426 /* the assoc was freed? */
4432 ~SCTP_ADDR_NOT_IN_ASSOC;
4436 * strange, address is in another
4437 * assoc? straighten out locks.
4439 if (stcb->asoc.state == 0) {
4440 /* the assoc was freed? */
4446 } else if (ptype == SCTP_IPV6_ADDRESS) {
4447 if (stcb->asoc.ipv6_addr_legal) {
4448 /* ok get the v6 address and check/add */
4449 struct sctp_ipv6addr_param *p6, p6_buf;
4451 phdr = sctp_get_next_param(m, offset,
4452 (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
4453 if (plen != sizeof(struct sctp_ipv6addr_param) ||
4457 p6 = (struct sctp_ipv6addr_param *)phdr;
4458 memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
4460 sa = (struct sockaddr *)&sin6;
4461 inp = stcb->sctp_ep;
4462 atomic_add_int(&stcb->asoc.refcnt, 1);
4463 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
4465 atomic_add_int(&stcb->asoc.refcnt, -1);
4466 if (stcb_tmp == NULL && (inp == stcb->sctp_ep ||
4469 * we must validate the state again
4472 if (stcb->asoc.state == 0) {
4473 /* the assoc was freed? */
4477 * we must add the address, no scope
4480 if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_5)) {
4483 } else if (stcb_tmp == stcb) {
4485 * we must validate the state again
4488 if (stcb->asoc.state == 0) {
4489 /* the assoc was freed? */
4495 ~SCTP_ADDR_NOT_IN_ASSOC;
4499 * strange, address is in another
4500 * assoc? straighten out locks.
4502 if (stcb->asoc.state == 0) {
4503 /* the assoc was freed? */
4509 } else if (ptype == SCTP_ECN_CAPABLE) {
4510 stcb->asoc.ecn_allowed = 1;
4511 } else if (ptype == SCTP_ULP_ADAPTATION) {
4512 if (stcb->asoc.state != SCTP_STATE_OPEN) {
4513 struct sctp_adaptation_layer_indication ai,
4516 phdr = sctp_get_next_param(m, offset,
4517 (struct sctp_paramhdr *)&ai, sizeof(ai));
4518 aip = (struct sctp_adaptation_layer_indication *)phdr;
4519 sctp_ulp_notify(SCTP_NOTIFY_ADAPTATION_INDICATION,
4520 stcb, ntohl(aip->indication), NULL);
4522 } else if (ptype == SCTP_SET_PRIM_ADDR) {
4523 struct sctp_asconf_addr_param lstore, *fee;
4524 struct sctp_asconf_addrv4_param *fii;
4526 struct sockaddr *lsa = NULL;
4528 stcb->asoc.peer_supports_asconf = 1;
4529 if (plen > sizeof(lstore)) {
4532 phdr = sctp_get_next_param(m, offset,
4533 (struct sctp_paramhdr *)&lstore, plen);
4537 fee = (struct sctp_asconf_addr_param *)phdr;
4538 lptype = ntohs(fee->addrp.ph.param_type);
4539 if (lptype == SCTP_IPV4_ADDRESS) {
4541 sizeof(struct sctp_asconf_addrv4_param)) {
4542 printf("Sizeof setprim in init/init ack not %d but %d - ignored\n",
4543 (int)sizeof(struct sctp_asconf_addrv4_param),
4546 fii = (struct sctp_asconf_addrv4_param *)fee;
4547 sin.sin_addr.s_addr = fii->addrp.addr;
4548 lsa = (struct sockaddr *)&sin;
4550 } else if (lptype == SCTP_IPV6_ADDRESS) {
4552 sizeof(struct sctp_asconf_addr_param)) {
4553 printf("Sizeof setprim (v6) in init/init ack not %d but %d - ignored\n",
4554 (int)sizeof(struct sctp_asconf_addr_param),
4557 memcpy(sin6.sin6_addr.s6_addr,
4559 sizeof(fee->addrp.addr));
4560 lsa = (struct sockaddr *)&sin6;
4564 sctp_set_primary_addr(stcb, sa, NULL);
4566 } else if (ptype == SCTP_PRSCTP_SUPPORTED) {
4567 /* Peer supports pr-sctp */
4568 stcb->asoc.peer_supports_prsctp = 1;
4569 } else if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
4570 /* A supported extension chunk */
4571 struct sctp_supported_chunk_types_param *pr_supported;
4572 uint8_t local_store[128];
4575 phdr = sctp_get_next_param(m, offset,
4576 (struct sctp_paramhdr *)&local_store, plen);
4580 stcb->asoc.peer_supports_asconf = 0;
4581 stcb->asoc.peer_supports_prsctp = 0;
4582 stcb->asoc.peer_supports_pktdrop = 0;
4583 stcb->asoc.peer_supports_strreset = 0;
4584 stcb->asoc.peer_supports_auth = 0;
4585 pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
4586 num_ent = plen - sizeof(struct sctp_paramhdr);
4587 for (i = 0; i < num_ent; i++) {
4588 switch (pr_supported->chunk_types[i]) {
4590 case SCTP_ASCONF_ACK:
4591 stcb->asoc.peer_supports_asconf = 1;
4593 case SCTP_FORWARD_CUM_TSN:
4594 stcb->asoc.peer_supports_prsctp = 1;
4596 case SCTP_PACKET_DROPPED:
4597 stcb->asoc.peer_supports_pktdrop = 1;
4599 case SCTP_STREAM_RESET:
4600 stcb->asoc.peer_supports_strreset = 1;
4602 case SCTP_AUTHENTICATION:
4603 stcb->asoc.peer_supports_auth = 1;
4606 /* one I have not learned yet */
4611 } else if (ptype == SCTP_ECN_NONCE_SUPPORTED) {
4612 /* Peer supports ECN-nonce */
4613 stcb->asoc.peer_supports_ecn_nonce = 1;
4614 stcb->asoc.ecn_nonce_allowed = 1;
4615 } else if (ptype == SCTP_RANDOM) {
4616 if (plen > sizeof(store))
4619 /* already processed a RANDOM */
4622 phdr = sctp_get_next_param(m, offset,
4623 (struct sctp_paramhdr *)store,
4627 random = (struct sctp_auth_random *)phdr;
4628 random_len = plen - sizeof(*random);
4629 /* enforce the random length */
4630 if (random_len != SCTP_AUTH_RANDOM_SIZE_REQUIRED) {
4632 if (sctp_debug_on & SCTP_DEBUG_AUTH1)
4633 printf("SCTP: invalid RANDOM len\n");
4638 } else if (ptype == SCTP_HMAC_LIST) {
4642 if (plen > sizeof(store))
4645 /* already processed a HMAC list */
4648 phdr = sctp_get_next_param(m, offset,
4649 (struct sctp_paramhdr *)store,
4653 hmacs = (struct sctp_auth_hmac_algo *)phdr;
4654 hmacs_len = plen - sizeof(*hmacs);
4655 num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]);
4656 /* validate the hmac list */
4657 if (sctp_verify_hmac_param(hmacs, num_hmacs)) {
4660 if (stcb->asoc.peer_hmacs != NULL)
4661 sctp_free_hmaclist(stcb->asoc.peer_hmacs);
4662 stcb->asoc.peer_hmacs = sctp_alloc_hmaclist(num_hmacs);
4663 if (stcb->asoc.peer_hmacs != NULL) {
4664 for (i = 0; i < num_hmacs; i++) {
4665 sctp_auth_add_hmacid(stcb->asoc.peer_hmacs,
4666 ntohs(hmacs->hmac_ids[i]));
4670 } else if (ptype == SCTP_CHUNK_LIST) {
4673 if (plen > sizeof(store))
4676 /* already processed a Chunks list */
4679 phdr = sctp_get_next_param(m, offset,
4680 (struct sctp_paramhdr *)store,
4684 chunks = (struct sctp_auth_chunk_list *)phdr;
4685 num_chunks = plen - sizeof(*chunks);
4686 if (stcb->asoc.peer_auth_chunks != NULL)
4687 sctp_clear_chunklist(stcb->asoc.peer_auth_chunks);
4689 stcb->asoc.peer_auth_chunks = sctp_alloc_chunklist();
4690 for (i = 0; i < num_chunks; i++) {
4691 sctp_auth_add_chunk(chunks->chunk_types[i],
4692 stcb->asoc.peer_auth_chunks);
4695 } else if ((ptype == SCTP_HEARTBEAT_INFO) ||
4696 (ptype == SCTP_STATE_COOKIE) ||
4697 (ptype == SCTP_UNRECOG_PARAM) ||
4698 (ptype == SCTP_COOKIE_PRESERVE) ||
4699 (ptype == SCTP_SUPPORTED_ADDRTYPE) ||
4700 (ptype == SCTP_ADD_IP_ADDRESS) ||
4701 (ptype == SCTP_DEL_IP_ADDRESS) ||
4702 (ptype == SCTP_ERROR_CAUSE_IND) ||
4703 (ptype == SCTP_SUCCESS_REPORT)) {
4706 if ((ptype & 0x8000) == 0x0000) {
4708 * must stop processing the rest of the
4709 * param's. Any report bits were handled
4711 * sctp_arethere_unrecognized_parameters()
4712 * when the INIT or INIT-ACK was first seen.
4718 offset += SCTP_SIZE32(plen);
4719 if (offset >= limit) {
4722 phdr = sctp_get_next_param(m, offset, &parm_buf,
4725 /* Now check to see if we need to purge any addresses */
4726 for (net = TAILQ_FIRST(&stcb->asoc.nets); net != NULL; net = net_tmp) {
4727 net_tmp = TAILQ_NEXT(net, sctp_next);
4728 if ((net->dest_state & SCTP_ADDR_NOT_IN_ASSOC) ==
4729 SCTP_ADDR_NOT_IN_ASSOC) {
4730 /* This address has been removed from the asoc */
4731 /* remove and free it */
4732 stcb->asoc.numnets--;
4733 TAILQ_REMOVE(&stcb->asoc.nets, net, sctp_next);
4734 sctp_free_remote_addr(net);
4735 if (net == stcb->asoc.primary_destination) {
4736 stcb->asoc.primary_destination = NULL;
4737 sctp_select_primary_destination(stcb);
4741 /* validate authentication required parameters */
4742 if (got_random && got_hmacs) {
4743 stcb->asoc.peer_supports_auth = 1;
4745 stcb->asoc.peer_supports_auth = 0;
4747 if (!sctp_asconf_auth_nochk && stcb->asoc.peer_supports_asconf &&
4748 !stcb->asoc.peer_supports_auth) {
4751 /* concatenate the full random key */
4752 keylen = random_len + num_chunks + hmacs_len;
4753 new_key = sctp_alloc_key(keylen);
4754 if (new_key != NULL) {
4755 /* copy in the RANDOM */
4757 bcopy(random->random_data, new_key->key, random_len);
4758 /* append in the AUTH chunks */
4760 bcopy(chunks->chunk_types, new_key->key + random_len,
4762 /* append in the HMACs */
4764 bcopy(hmacs->hmac_ids, new_key->key + random_len + num_chunks,
4769 if (stcb->asoc.authinfo.peer_random != NULL)
4770 sctp_free_key(stcb->asoc.authinfo.peer_random);
4771 stcb->asoc.authinfo.peer_random = new_key;
4772 #ifdef SCTP_AUTH_DRAFT_04
4773 /* don't include the chunks and hmacs for draft -04 */
4774 stcb->asoc.authinfo.peer_random->keylen = random_len;
4776 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid);
4777 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid);
4783 sctp_set_primary_addr(struct sctp_tcb *stcb, struct sockaddr *sa,
4784 struct sctp_nets *net)
4786 /* make sure the requested primary address exists in the assoc */
4787 if (net == NULL && sa)
4788 net = sctp_findnet(stcb, sa);
4791 /* didn't find the requested primary address! */
4794 /* set the primary address */
4795 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
4796 /* Must be confirmed */
4799 stcb->asoc.primary_destination = net;
4800 net->dest_state &= ~SCTP_ADDR_WAS_PRIMARY;
4801 net = TAILQ_FIRST(&stcb->asoc.nets);
4802 if (net != stcb->asoc.primary_destination) {
4804 * first one on the list is NOT the primary
4805 * sctp_cmpaddr() is much more efficent if the
4806 * primary is the first on the list, make it so.
4808 TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
4809 TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
4817 sctp_is_vtag_good(struct sctp_inpcb *inp, uint32_t tag, struct timeval *now)
4820 * This function serves two purposes. It will see if a TAG can be
4821 * re-used and return 1 for yes it is ok and 0 for don't use that
4822 * tag. A secondary function it will do is purge out old tags that
4825 struct sctpasochead *head;
4826 struct sctpvtaghead *chain;
4827 struct sctp_tagblock *twait_block;
4828 struct sctp_tcb *stcb;
4831 SCTP_INP_INFO_WLOCK();
4832 chain = &sctppcbinfo.vtag_timewait[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
4833 /* First is the vtag in use ? */
4835 head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(tag,
4836 sctppcbinfo.hashasocmark)];
4840 LIST_FOREACH(stcb, head, sctp_asocs) {
4842 if (stcb->asoc.my_vtag == tag) {
4844 * We should remove this if and return 0 always if
4845 * we want vtags unique across all endpoints. For
4846 * now within a endpoint is ok.
4848 if (inp == stcb->sctp_ep) {
4849 /* bad tag, in use */
4850 SCTP_INP_INFO_WUNLOCK();
4856 /* Now lets check the restart hash */
4857 head = &sctppcbinfo.sctp_restarthash[SCTP_PCBHASH_ASOC(tag,
4858 sctppcbinfo.hashrestartmark)];
4860 goto check_time_wait;
4862 LIST_FOREACH(stcb, head, sctp_tcbrestarhash) {
4863 if (stcb->asoc.assoc_id == tag) {
4865 if (inp == stcb->sctp_ep) {
4866 /* bad tag, in use */
4867 SCTP_INP_INFO_WUNLOCK();
4873 /* Now what about timed wait ? */
4874 if (!LIST_EMPTY(chain)) {
4876 * Block(s) are present, lets see if we have this tag in the
4879 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
4880 for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
4881 if (twait_block->vtag_block[i].v_tag == 0) {
4884 } else if ((long)twait_block->vtag_block[i].tv_sec_at_expire >
4886 /* Audit expires this guy */
4887 twait_block->vtag_block[i].tv_sec_at_expire = 0;
4888 twait_block->vtag_block[i].v_tag = 0;
4889 } else if (twait_block->vtag_block[i].v_tag ==
4891 /* Bad tag, sorry :< */
4892 SCTP_INP_INFO_WUNLOCK();
4898 /* Not found, ok to use the tag */
4899 SCTP_INP_INFO_WUNLOCK();
4905 * Delete the address from the endpoint local address list Lookup using a
4906 * sockaddr address (ie. not an ifaddr)
4909 sctp_del_local_addr_ep_sa(struct sctp_inpcb *inp, struct sockaddr *sa)
4911 struct sctp_laddr *laddr;
4912 struct sockaddr *l_sa;
4916 * Here is another function I cannot find a caller for. As such we
4917 * SHOULD delete it if we have no users. If we find a user that user
4918 * MUST have the INP locked.
4922 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4923 /* You are already bound to all. You have it already */
4926 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4927 /* make sure the address exists */
4928 if (laddr->ifa == NULL)
4930 if (laddr->ifa->ifa_addr == NULL)
4933 l_sa = laddr->ifa->ifa_addr;
4934 if (l_sa->sa_family == AF_INET6) {
4936 struct sockaddr_in6 *sin1, *sin2;
4938 sin1 = (struct sockaddr_in6 *)l_sa;
4939 sin2 = (struct sockaddr_in6 *)sa;
4940 if (memcmp(&sin1->sin6_addr, &sin2->sin6_addr,
4941 sizeof(struct in6_addr)) == 0) {
4946 } else if (l_sa->sa_family == AF_INET) {
4948 struct sockaddr_in *sin1, *sin2;
4950 sin1 = (struct sockaddr_in *)l_sa;
4951 sin2 = (struct sockaddr_in *)sa;
4952 if (sin1->sin_addr.s_addr == sin2->sin_addr.s_addr) {
4958 /* invalid family */
4963 if (found && inp->laddr_count < 2) {
4964 /* can't delete unless there are at LEAST 2 addresses */
4967 if (found && (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
4969 * remove it from the ep list, this should NOT be done until
4970 * its really gone from the interface list and we won't be
4971 * receiving more of these. Probably right away. If we do
4972 * allow a removal of an address from an association
4973 * (sub-set bind) than this should NOT be called until the
4974 * all ASCONF come back from this association.
4976 sctp_remove_laddr(laddr);
4983 static sctp_assoc_t reneged_asoc_ids[256];
4984 static uint8_t reneged_at = 0;
4986 extern int sctp_do_drain;
4989 sctp_drain_mbufs(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
4992 * We must hunt this association for MBUF's past the cumack (i.e.
4993 * out of order data that we can renege on).
4995 struct sctp_association *asoc;
4996 struct sctp_tmit_chunk *chk, *nchk;
4997 uint32_t cumulative_tsn_p1, tsn;
4998 struct sctp_queued_to_read *ctl, *nctl;
4999 int cnt, strmat, gap;
5001 /* We look for anything larger than the cum-ack + 1 */
5003 SCTP_STAT_INCR(sctps_protocol_drain_calls);
5004 if (sctp_do_drain == 0) {
5008 if (asoc->cumulative_tsn == asoc->highest_tsn_inside_map) {
5009 /* none we can reneg on. */
5012 SCTP_STAT_INCR(sctps_protocol_drains_done);
5013 cumulative_tsn_p1 = asoc->cumulative_tsn + 1;
5015 /* First look in the re-assembly queue */
5016 chk = TAILQ_FIRST(&asoc->reasmqueue);
5018 /* Get the next one */
5019 nchk = TAILQ_NEXT(chk, sctp_next);
5020 if (compare_with_wrap(chk->rec.data.TSN_seq,
5021 cumulative_tsn_p1, MAX_TSN)) {
5022 /* Yep it is above cum-ack */
5024 tsn = chk->rec.data.TSN_seq;
5025 if (tsn >= asoc->mapping_array_base_tsn) {
5026 gap = tsn - asoc->mapping_array_base_tsn;
5028 gap = (MAX_TSN - asoc->mapping_array_base_tsn) +
5031 asoc->size_on_reasm_queue = sctp_sbspace_sub(asoc->size_on_reasm_queue, chk->send_size);
5032 sctp_ucount_decr(asoc->cnt_on_reasm_queue);
5033 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
5034 TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
5036 sctp_m_freem(chk->data);
5039 sctp_free_remote_addr(chk->whoTo);
5040 sctp_free_a_chunk(stcb, chk);
5044 /* Ok that was fun, now we will drain all the inbound streams? */
5045 for (strmat = 0; strmat < asoc->streamincnt; strmat++) {
5046 ctl = TAILQ_FIRST(&asoc->strmin[strmat].inqueue);
5048 nctl = TAILQ_NEXT(ctl, next);
5049 if (compare_with_wrap(ctl->sinfo_tsn,
5050 cumulative_tsn_p1, MAX_TSN)) {
5051 /* Yep it is above cum-ack */
5053 tsn = ctl->sinfo_tsn;
5054 if (tsn >= asoc->mapping_array_base_tsn) {
5056 asoc->mapping_array_base_tsn;
5059 asoc->mapping_array_base_tsn) +
5062 asoc->size_on_all_streams = sctp_sbspace_sub(asoc->size_on_all_streams, ctl->length);
5063 sctp_ucount_decr(asoc->cnt_on_all_streams);
5065 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array,
5067 TAILQ_REMOVE(&asoc->strmin[strmat].inqueue,
5070 sctp_m_freem(ctl->data);
5073 sctp_free_remote_addr(ctl->whoFrom);
5074 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_readq, ctl);
5075 SCTP_DECR_READQ_COUNT();
5081 * Question, should we go through the delivery queue? The only
5082 * reason things are on here is the app not reading OR a p-d-api up.
5083 * An attacker COULD send enough in to initiate the PD-API and then
5084 * send a bunch of stuff to other streams... these would wind up on
5085 * the delivery queue.. and then we would not get to them. But in
5086 * order to do this I then have to back-track and un-deliver
5087 * sequence numbers in streams.. el-yucko. I think for now we will
5088 * NOT look at the delivery queue and leave it to be something to
5089 * consider later. An alternative would be to abort the P-D-API with
5090 * a notification and then deliver the data.... Or another method
5091 * might be to keep track of how many times the situation occurs and
5092 * if we see a possible attack underway just abort the association.
5095 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
5097 printf("Freed %d chunks from reneg harvest\n", cnt);
5100 #endif /* SCTP_DEBUG */
5103 * Now do we need to find a new
5104 * asoc->highest_tsn_inside_map?
5106 if (asoc->highest_tsn_inside_map >= asoc->mapping_array_base_tsn) {
5107 gap = asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn;
5109 gap = (MAX_TSN - asoc->mapping_array_base_tsn) +
5110 asoc->highest_tsn_inside_map + 1;
5112 if (gap >= (asoc->mapping_array_size << 3)) {
5114 * Something bad happened or cum-ack and high were
5115 * behind the base, but if so earlier checks should
5116 * have found NO data... wierd... we will start at
5117 * end of mapping array.
5119 printf("Gap was larger than array?? %d set to max:%d maparraymax:%x\n",
5121 (int)(asoc->mapping_array_size << 3),
5122 (int)asoc->highest_tsn_inside_map);
5123 gap = asoc->mapping_array_size << 3;
5126 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
5127 /* found the new highest */
5128 asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn + gap;
5134 /* Nothing left in map */
5135 memset(asoc->mapping_array, 0, asoc->mapping_array_size);
5136 asoc->mapping_array_base_tsn = asoc->cumulative_tsn + 1;
5137 asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
5139 asoc->last_revoke_count = cnt;
5140 SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
5141 sctp_send_sack(stcb);
5142 reneged_asoc_ids[reneged_at] = sctp_get_associd(stcb);
5146 * Another issue, in un-setting the TSN's in the mapping array we
5147 * DID NOT adjust the higest_tsn marker. This will cause one of two
5148 * things to occur. It may cause us to do extra work in checking for
5149 * our mapping array movement. More importantly it may cause us to
5150 * SACK every datagram. This may not be a bad thing though since we
5151 * will recover once we get our cum-ack above and all this stuff we
5160 * We must walk the PCB lists for ALL associations here. The system
5161 * is LOW on MBUF's and needs help. This is where reneging will
5162 * occur. We really hope this does NOT happen!
5164 struct sctp_inpcb *inp;
5165 struct sctp_tcb *stcb;
5167 SCTP_INP_INFO_RLOCK();
5168 LIST_FOREACH(inp, &sctppcbinfo.listhead, sctp_list) {
5169 /* For each endpoint */
5170 SCTP_INP_RLOCK(inp);
5171 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5172 /* For each association */
5173 SCTP_TCB_LOCK(stcb);
5174 sctp_drain_mbufs(inp, stcb);
5175 SCTP_TCB_UNLOCK(stcb);
5177 SCTP_INP_RUNLOCK(inp);
5179 SCTP_INP_INFO_RUNLOCK();
5183 * start a new iterator
5184 * iterates through all endpoints and associations based on the pcb_state
5185 * flags and asoc_state. "af" (mandatory) is executed for all matching
5186 * assocs and "ef" (optional) is executed when the iterator completes.
5187 * "inpf" (optional) is executed for each new endpoint as it is being
5191 sctp_initiate_iterator(inp_func inpf, asoc_func af, uint32_t pcb_state,
5192 uint32_t pcb_features, uint32_t asoc_state, void *argp, uint32_t argi,
5193 end_func ef, struct sctp_inpcb *s_inp, uint8_t chunk_output_off)
5195 struct sctp_iterator *it = NULL;
5200 SCTP_MALLOC(it, struct sctp_iterator *, sizeof(struct sctp_iterator),
5205 memset(it, 0, sizeof(*it));
5206 it->function_assoc = af;
5207 it->function_inp = inpf;
5208 it->function_atend = ef;
5211 it->pcb_flags = pcb_state;
5212 it->pcb_features = pcb_features;
5213 it->asoc_state = asoc_state;
5214 it->no_chunk_output = chunk_output_off;
5217 it->iterator_flags = SCTP_ITERATOR_DO_SINGLE_INP;
5219 SCTP_INP_INFO_RLOCK();
5220 it->inp = LIST_FIRST(&sctppcbinfo.listhead);
5221 SCTP_INP_INFO_RUNLOCK();
5222 it->iterator_flags = SCTP_ITERATOR_DO_ALL_INP;
5225 /* Init the timer */
5226 SCTP_OS_TIMER_INIT(&it->tmr.timer);
5227 /* add to the list of all iterators */
5228 SCTP_INP_INFO_WLOCK();
5229 LIST_INSERT_HEAD(&sctppcbinfo.iteratorhead, it, sctp_nxt_itr);
5230 SCTP_INP_INFO_WUNLOCK();
5231 sctp_timer_start(SCTP_TIMER_TYPE_ITERATOR, (struct sctp_inpcb *)it,