1 /* $KAME: sctp_output.c,v 1.46 2005/03/06 16:04:17 itojun Exp $ */
4 * Copyright (C) 2002, 2003, 2004 Cisco Systems Inc,
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #if !(defined(__OpenBSD__) || defined (__APPLE__))
33 #include "opt_ipsec.h"
35 #if defined(__FreeBSD__) || defined(__DragonFly__)
36 #include "opt_compat.h"
37 #include "opt_inet6.h"
40 #if defined(__NetBSD__)
45 #elif !defined(__OpenBSD__)
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/malloc.h>
53 #include <sys/domain.h>
55 #include <sys/protosw.h>
56 #include <sys/socket.h>
57 #include <sys/socketvar.h>
59 #include <sys/kernel.h>
60 #include <sys/sysctl.h>
61 #include <sys/resourcevar.h>
64 #include <sys/domain.h>
66 #include <sys/thread2.h>
67 #include <sys/socketvar2.h>
69 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
70 #include <sys/limits.h>
72 #include <machine/limits.h>
74 #include <machine/cpu.h>
77 #include <net/if_types.h>
79 #if defined(__FreeBSD__) || defined(__DragonFly__)
80 #include <net/if_var.h>
83 #include <net/route.h>
85 #include <netinet/in.h>
86 #include <netinet/in_systm.h>
87 #include <netinet/ip.h>
88 #include <netinet/in_pcb.h>
89 #include <netinet/in_var.h>
90 #include <netinet/ip_var.h>
93 #include <netinet/ip6.h>
94 #include <netinet6/ip6_var.h>
95 #include <netinet6/scope6_var.h>
96 #include <netinet6/nd6.h>
98 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
99 #include <netinet6/in6_pcb.h>
100 #elif defined(__OpenBSD__)
101 #include <netinet/in_pcb.h>
104 #include <netinet/icmp6.h>
108 #include <net/net_osdep.h>
110 #if defined(HAVE_NRL_INPCB) || defined(__FreeBSD__) || defined(__DragonFly__)
116 #include <netinet/sctp_pcb.h>
120 #include <netinet6/ipsec.h>
121 #include <netproto/key/key.h>
127 #include <netinet/sctp_var.h>
128 #include <netinet/sctp_header.h>
129 #include <netinet/sctputil.h>
130 #include <netinet/sctp_pcb.h>
131 #include <netinet/sctp_output.h>
132 #include <netinet/sctp_uio.h>
133 #include <netinet/sctputil.h>
134 #include <netinet/sctp_hashdriver.h>
135 #include <netinet/sctp_timer.h>
136 #include <netinet/sctp_asconf.h>
137 #include <netinet/sctp_indata.h>
140 extern uint32_t sctp_debug_on;
143 extern int sctp_peer_chunk_oh;
146 sctp_find_cmsg(int c_type, void *data, struct mbuf *control, int cpsize)
151 tlen = control->m_len;
154 * Independent of how many mbufs, find the c_type inside the control
155 * structure and copy out the data.
158 if ((tlen-at) < (int)CMSG_ALIGN(sizeof(cmh))) {
159 /* not enough room for one more we are done. */
162 m_copydata(control, at, sizeof(cmh), (caddr_t)&cmh);
163 if ((cmh.cmsg_len + at) > tlen) {
165 * this is real messed up since there is not enough
166 * data here to cover the cmsg header. We are done.
170 if ((cmh.cmsg_level == IPPROTO_SCTP) &&
171 (c_type == cmh.cmsg_type)) {
172 /* found the one we want, copy it out */
173 at += CMSG_ALIGN(sizeof(struct cmsghdr));
174 if ((int)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < cpsize) {
176 * space of cmsg_len after header not
181 m_copydata(control, at, cpsize, data);
184 at += CMSG_ALIGN(cmh.cmsg_len);
185 if (cmh.cmsg_len == 0) {
195 sctp_add_addr_to_mbuf(struct mbuf *m, struct ifaddr *ifa)
197 struct sctp_paramhdr *parmh;
200 if (ifa->ifa_addr->sa_family == AF_INET) {
201 len = sizeof(struct sctp_ipv4addr_param);
202 } else if (ifa->ifa_addr->sa_family == AF_INET6) {
203 len = sizeof(struct sctp_ipv6addr_param);
209 if (M_TRAILINGSPACE(m) >= len) {
210 /* easy side we just drop it on the end */
211 parmh = (struct sctp_paramhdr *)(m->m_data + m->m_len);
214 /* Need more space */
216 while (mret->m_next != NULL) {
219 MGET(mret->m_next, MB_DONTWAIT, MT_DATA);
220 if (mret->m_next == NULL) {
221 /* We are hosed, can't add more addresses */
225 parmh = mtod(mret, struct sctp_paramhdr *);
227 /* now add the parameter */
228 if (ifa->ifa_addr->sa_family == AF_INET) {
229 struct sctp_ipv4addr_param *ipv4p;
230 struct sockaddr_in *sin;
231 sin = (struct sockaddr_in *)ifa->ifa_addr;
232 ipv4p = (struct sctp_ipv4addr_param *)parmh;
233 parmh->param_type = htons(SCTP_IPV4_ADDRESS);
234 parmh->param_length = htons(len);
235 ipv4p->addr = sin->sin_addr.s_addr;
237 } else if (ifa->ifa_addr->sa_family == AF_INET6) {
238 struct sctp_ipv6addr_param *ipv6p;
239 struct sockaddr_in6 *sin6;
240 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
241 ipv6p = (struct sctp_ipv6addr_param *)parmh;
242 parmh->param_type = htons(SCTP_IPV6_ADDRESS);
243 parmh->param_length = htons(len);
244 memcpy(ipv6p->addr, &sin6->sin6_addr,
245 sizeof(ipv6p->addr));
246 /* clear embedded scope in the address */
247 in6_clearscope((struct in6_addr *)ipv6p->addr);
258 sctp_add_cookie(struct sctp_inpcb *inp, struct mbuf *init, int init_offset,
259 struct mbuf *initack, int initack_offset, struct sctp_state_cookie *stc_in)
261 struct mbuf *copy_init, *copy_initack, *m_at, *sig, *mret;
262 struct sctp_state_cookie *stc;
263 struct sctp_paramhdr *ph;
270 MGET(mret, MB_DONTWAIT, MT_DATA);
274 copy_init = sctp_m_copym(init, init_offset, M_COPYALL, MB_DONTWAIT);
275 if (copy_init == NULL) {
279 copy_initack = sctp_m_copym(initack, initack_offset, M_COPYALL,
281 if (copy_initack == NULL) {
283 sctp_m_freem(copy_init);
286 /* easy side we just drop it on the end */
287 ph = mtod(mret, struct sctp_paramhdr *);
288 mret->m_len = sizeof(struct sctp_state_cookie) +
289 sizeof(struct sctp_paramhdr);
290 stc = (struct sctp_state_cookie *)((caddr_t)ph +
291 sizeof(struct sctp_paramhdr));
292 ph->param_type = htons(SCTP_STATE_COOKIE);
293 ph->param_length = 0; /* fill in at the end */
294 /* Fill in the stc cookie data */
297 /* tack the INIT and then the INIT-ACK onto the chain */
300 for (m_at = mret; m_at; m_at = m_at->m_next) {
301 cookie_sz += m_at->m_len;
302 if (m_at->m_next == NULL) {
303 m_at->m_next = copy_init;
308 for (m_at = copy_init; m_at; m_at = m_at->m_next) {
309 cookie_sz += m_at->m_len;
310 if (m_at->m_next == NULL) {
311 m_at->m_next = copy_initack;
316 for (m_at = copy_initack; m_at; m_at = m_at->m_next) {
317 cookie_sz += m_at->m_len;
318 if (m_at->m_next == NULL) {
322 MGET(sig, MB_DONTWAIT, MT_DATA);
326 sctp_m_freem(copy_init);
327 sctp_m_freem(copy_initack);
333 signature = (uint8_t *)(mtod(sig, caddr_t) + sig_offset);
334 /* Time to sign the cookie */
335 sctp_hash_digest_m((char *)inp->sctp_ep.secret_key[
336 (int)(inp->sctp_ep.current_secret_number)],
337 SCTP_SECRET_SIZE, mret, sizeof(struct sctp_paramhdr), signature);
338 sig->m_len += SCTP_SIGNATURE_SIZE;
339 cookie_sz += SCTP_SIGNATURE_SIZE;
341 ph->param_length = htons(cookie_sz);
346 static struct sockaddr_in *
347 sctp_is_v4_ifa_addr_prefered (struct ifaddr *ifa, uint8_t loopscope, uint8_t ipv4_scope, uint8_t *sin_loop, uint8_t *sin_local)
349 struct sockaddr_in *sin;
351 * Here we determine if its a prefered address. A
352 * prefered address means it is the same scope or
353 * higher scope then the destination.
354 * L = loopback, P = private, G = global
355 * -----------------------------------------
356 * src | dest | result
357 *-----------------------------------------
359 *-----------------------------------------
361 *-----------------------------------------
363 *-----------------------------------------
365 *-----------------------------------------
367 *-----------------------------------------
369 *-----------------------------------------
371 *-----------------------------------------
373 *-----------------------------------------
375 *-----------------------------------------
378 if (ifa->ifa_addr->sa_family != AF_INET) {
382 /* Ok the address may be ok */
383 sin = (struct sockaddr_in *)ifa->ifa_addr;
384 if (sin->sin_addr.s_addr == 0) {
387 *sin_local = *sin_loop = 0;
388 if ((ifa->ifa_ifp->if_type == IFT_LOOP) ||
389 (IN4_ISLOOPBACK_ADDRESS(&sin->sin_addr))) {
393 if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
396 if (!loopscope && *sin_loop) {
397 /* Its a loopback address and we don't have loop scope */
400 if (!ipv4_scope && *sin_local) {
401 /* Its a private address, and we don't have private address scope */
404 if (((ipv4_scope == 0) && (loopscope == 0)) && (*sin_local)) {
405 /* its a global src and a private dest */
408 /* its a prefered address */
412 static struct sockaddr_in *
413 sctp_is_v4_ifa_addr_acceptable (struct ifaddr *ifa, uint8_t loopscope, uint8_t ipv4_scope, uint8_t *sin_loop, uint8_t *sin_local)
415 struct sockaddr_in *sin;
417 * Here we determine if its a acceptable address. A
418 * acceptable address means it is the same scope or
419 * higher scope but we can allow for NAT which means
420 * its ok to have a global dest and a private src.
422 * L = loopback, P = private, G = global
423 * -----------------------------------------
424 * src | dest | result
425 *-----------------------------------------
427 *-----------------------------------------
429 *-----------------------------------------
431 *-----------------------------------------
433 *-----------------------------------------
435 *-----------------------------------------
436 * G | P | yes - probably this won't work.
437 *-----------------------------------------
439 *-----------------------------------------
441 *-----------------------------------------
443 *-----------------------------------------
446 if (ifa->ifa_addr->sa_family != AF_INET) {
450 /* Ok the address may be ok */
451 sin = (struct sockaddr_in *)ifa->ifa_addr;
452 if (sin->sin_addr.s_addr == 0) {
455 *sin_local = *sin_loop = 0;
456 if ((ifa->ifa_ifp->if_type == IFT_LOOP) ||
457 (IN4_ISLOOPBACK_ADDRESS(&sin->sin_addr))) {
461 if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
464 if (!loopscope && *sin_loop) {
465 /* Its a loopback address and we don't have loop scope */
468 /* its an acceptable address */
473 * This treats the address list on the ep as a restricted list
474 * (negative list). If a the passed address is listed, then
475 * the address is NOT allowed on the association.
478 sctp_is_addr_restricted(struct sctp_tcb *stcb, struct sockaddr *addr)
480 struct sctp_laddr *laddr;
485 /* There are no restrictions, no TCB :-) */
489 LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list, sctp_nxt_addr) {
492 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
493 kprintf("There are %d addresses on the restricted list\n", cnt);
497 LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list, sctp_nxt_addr) {
498 if (laddr->ifa == NULL) {
500 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
501 kprintf("Help I have fallen and I can't get up!\n");
507 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
509 kprintf("Restricted address[%d]:", cnt);
510 sctp_print_address(laddr->ifa->ifa_addr);
513 if (sctp_cmpaddr(addr, laddr->ifa->ifa_addr) == 1) {
514 /* Yes it is on the list */
522 sctp_is_addr_in_ep(struct sctp_inpcb *inp, struct ifaddr *ifa)
524 struct sctp_laddr *laddr;
528 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
529 if (laddr->ifa == NULL) {
531 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
532 kprintf("Help I have fallen and I can't get up!\n");
537 if (laddr->ifa->ifa_addr == NULL)
539 if (laddr->ifa == ifa)
542 if (laddr->ifa->ifa_addr->sa_family != ifa->ifa_addr->sa_family) {
543 /* skip non compatible address comparison */
546 if (sctp_cmpaddr(ifa->ifa_addr, laddr->ifa->ifa_addr) == 1) {
547 /* Yes it is restricted */
556 static struct in_addr
557 sctp_choose_v4_boundspecific_inp(struct sctp_inpcb *inp,
563 struct sctp_laddr *laddr;
564 struct sockaddr_in *sin;
566 uint8_t sin_loop, sin_local;
568 /* first question, is the ifn we will emit on
569 * in our list, if so, we want that one.
573 struct ifaddr_container *ifac;
575 /* is a prefered one on the interface we route out? */
576 TAILQ_FOREACH(ifac, &ifn->if_addrheads[mycpuid], ifa_link) {
577 struct ifaddr *ifa = ifac->ifa;
579 sin = sctp_is_v4_ifa_addr_prefered (ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
582 if (sctp_is_addr_in_ep(inp, ifa)) {
583 return (sin->sin_addr);
586 /* is an acceptable one on the interface we route out? */
587 TAILQ_FOREACH(ifac, &ifn->if_addrheads[mycpuid], ifa_link) {
588 struct ifaddr *ifa = ifac->ifa;
590 sin = sctp_is_v4_ifa_addr_acceptable (ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
593 if (sctp_is_addr_in_ep(inp, ifa)) {
594 return (sin->sin_addr);
598 /* ok, what about a prefered address in the inp */
599 for (laddr = LIST_FIRST(&inp->sctp_addr_list);
600 laddr && (laddr != inp->next_addr_touse);
601 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
602 if (laddr->ifa == NULL) {
603 /* address has been removed */
606 sin = sctp_is_v4_ifa_addr_prefered (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
609 return (sin->sin_addr);
612 /* ok, what about an acceptable address in the inp */
613 for (laddr = LIST_FIRST(&inp->sctp_addr_list);
614 laddr && (laddr != inp->next_addr_touse);
615 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
616 if (laddr->ifa == NULL) {
617 /* address has been removed */
620 sin = sctp_is_v4_ifa_addr_acceptable (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
623 return (sin->sin_addr);
627 /* no address bound can be a source for the destination we are in trouble */
629 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
630 kprintf("Src address selection for EP, no acceptable src address found for address\n");
633 memset(&ans, 0, sizeof(ans));
639 static struct in_addr
640 sctp_choose_v4_boundspecific_stcb(struct sctp_inpcb *inp,
641 struct sctp_tcb *stcb,
642 struct sctp_nets *net,
646 int non_asoc_addr_ok)
649 * Here we have two cases, bound all asconf
650 * allowed. bound all asconf not allowed.
653 struct sctp_laddr *laddr, *starting_point;
656 uint8_t sin_loop, sin_local, start_at_beginning=0;
657 struct sockaddr_in *sin;
659 /* first question, is the ifn we will emit on
660 * in our list, if so, we want that one.
664 if (inp->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) {
666 * Here we use the list of addresses on the endpoint. Then
667 * the addresses listed on the "restricted" list is just that,
668 * address that have not been added and can't be used (unless
669 * the non_asoc_addr_ok is set).
672 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
673 kprintf("Have a STCB - asconf allowed, not bound all have a netgative list\n");
676 /* first question, is the ifn we will emit on
677 * in our list, if so, we want that one.
680 struct ifaddr_container *ifac;
682 /* first try for an prefered address on the ep */
683 TAILQ_FOREACH(ifac, &ifn->if_addrheads[mycpuid], ifa_link) {
684 struct ifaddr *ifa = ifac->ifa;
686 if (sctp_is_addr_in_ep(inp, ifa)) {
687 sin = sctp_is_v4_ifa_addr_prefered (ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
690 if ((non_asoc_addr_ok == 0) &&
691 (sctp_is_addr_restricted(stcb, (struct sockaddr *)sin))) {
692 /* on the no-no list */
695 return (sin->sin_addr);
698 /* next try for an acceptable address on the ep */
699 TAILQ_FOREACH(ifac, &ifn->if_addrheads[mycpuid], ifa_link) {
700 struct ifaddr *ifa = ifac->ifa;
702 if (sctp_is_addr_in_ep(inp, ifa)) {
703 sin = sctp_is_v4_ifa_addr_acceptable (ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
706 if ((non_asoc_addr_ok == 0) &&
707 (sctp_is_addr_restricted(stcb, (struct sockaddr *)sin))) {
708 /* on the no-no list */
711 return (sin->sin_addr);
716 /* if we can't find one like that then we must
717 * look at all addresses bound to pick one at
718 * first prefereable then secondly acceptable.
720 starting_point = stcb->asoc.last_used_address;
722 if (stcb->asoc.last_used_address == NULL) {
723 start_at_beginning=1;
724 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
726 /* search beginning with the last used address */
727 for (laddr = stcb->asoc.last_used_address; laddr;
728 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
729 if (laddr->ifa == NULL) {
730 /* address has been removed */
733 sin = sctp_is_v4_ifa_addr_prefered (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
736 if ((non_asoc_addr_ok == 0) &&
737 (sctp_is_addr_restricted(stcb, (struct sockaddr *)sin))) {
738 /* on the no-no list */
741 return (sin->sin_addr);
744 if (start_at_beginning == 0) {
745 stcb->asoc.last_used_address = NULL;
746 goto sctpv4_from_the_top;
748 /* now try for any higher scope than the destination */
749 stcb->asoc.last_used_address = starting_point;
750 start_at_beginning = 0;
751 sctpv4_from_the_top2:
752 if (stcb->asoc.last_used_address == NULL) {
753 start_at_beginning=1;
754 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
756 /* search beginning with the last used address */
757 for (laddr = stcb->asoc.last_used_address; laddr;
758 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
759 if (laddr->ifa == NULL) {
760 /* address has been removed */
763 sin = sctp_is_v4_ifa_addr_acceptable (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
766 if ((non_asoc_addr_ok == 0) &&
767 (sctp_is_addr_restricted(stcb, (struct sockaddr *)sin))) {
768 /* on the no-no list */
771 return (sin->sin_addr);
773 if (start_at_beginning == 0) {
774 stcb->asoc.last_used_address = NULL;
775 goto sctpv4_from_the_top2;
779 * Here we have an address list on the association, thats the
780 * only valid source addresses that we can use.
783 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
784 kprintf("Have a STCB - no asconf allowed, not bound all have a positive list\n");
787 /* First look at all addresses for one that is on
788 * the interface we route out
790 LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
792 if (laddr->ifa == NULL) {
793 /* address has been removed */
796 sin = sctp_is_v4_ifa_addr_prefered (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
799 /* first question, is laddr->ifa an address associated with the emit interface */
801 struct ifaddr_container *ifac;
803 TAILQ_FOREACH(ifac, &ifn->if_addrheads[mycpuid], ifa_link) {
804 struct ifaddr *ifa = ifac->ifa;
806 if (laddr->ifa == ifa) {
807 sin = (struct sockaddr_in *)laddr->ifa->ifa_addr;
808 return (sin->sin_addr);
810 if (sctp_cmpaddr(ifa->ifa_addr, laddr->ifa->ifa_addr) == 1) {
811 sin = (struct sockaddr_in *)laddr->ifa->ifa_addr;
812 return (sin->sin_addr);
817 /* what about an acceptable one on the interface? */
818 LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
820 if (laddr->ifa == NULL) {
821 /* address has been removed */
824 sin = sctp_is_v4_ifa_addr_acceptable (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
827 /* first question, is laddr->ifa an address associated with the emit interface */
829 struct ifaddr_container *ifac;
831 TAILQ_FOREACH(ifac, &ifn->if_addrheads[mycpuid], ifa_link) {
832 struct ifaddr *ifa = ifac->ifa;
834 if (laddr->ifa == ifa) {
835 sin = (struct sockaddr_in *)laddr->ifa->ifa_addr;
836 return (sin->sin_addr);
838 if (sctp_cmpaddr(ifa->ifa_addr, laddr->ifa->ifa_addr) == 1) {
839 sin = (struct sockaddr_in *)laddr->ifa->ifa_addr;
840 return (sin->sin_addr);
845 /* ok, next one that is preferable in general */
846 LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
848 if (laddr->ifa == NULL) {
849 /* address has been removed */
852 sin = sctp_is_v4_ifa_addr_prefered (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
855 return (sin->sin_addr);
858 /* last, what about one that is acceptable */
859 LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
861 if (laddr->ifa == NULL) {
862 /* address has been removed */
865 sin = sctp_is_v4_ifa_addr_acceptable (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
868 return (sin->sin_addr);
871 memset(&ans, 0, sizeof(ans));
875 static struct sockaddr_in *
876 sctp_select_v4_nth_prefered_addr_from_ifn_boundall (struct ifnet *ifn, struct sctp_tcb *stcb, int non_asoc_addr_ok,
877 uint8_t loopscope, uint8_t ipv4_scope, int cur_addr_num)
879 struct ifaddr_container *ifac;
880 struct sockaddr_in *sin;
881 uint8_t sin_loop, sin_local;
882 int num_eligible_addr = 0;
884 TAILQ_FOREACH(ifac, &ifn->if_addrheads[mycpuid], ifa_link) {
885 struct ifaddr *ifa = ifac->ifa;
887 sin = sctp_is_v4_ifa_addr_prefered (ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
891 if ((non_asoc_addr_ok == 0) && sctp_is_addr_restricted(stcb, (struct sockaddr *)sin)) {
892 /* It is restricted for some reason.. probably
898 if (cur_addr_num == num_eligible_addr) {
907 sctp_count_v4_num_prefered_boundall (struct ifnet *ifn, struct sctp_tcb *stcb, int non_asoc_addr_ok,
908 uint8_t loopscope, uint8_t ipv4_scope, uint8_t *sin_loop, uint8_t *sin_local)
910 struct ifaddr_container *ifac;
911 struct sockaddr_in *sin;
912 int num_eligible_addr = 0;
914 TAILQ_FOREACH(ifac, &ifn->if_addrheads[mycpuid], ifa_link) {
915 struct ifaddr *ifa = ifac->ifa;
917 sin = sctp_is_v4_ifa_addr_prefered (ifa, loopscope, ipv4_scope, sin_loop, sin_local);
921 if ((non_asoc_addr_ok == 0) && sctp_is_addr_restricted(stcb, (struct sockaddr *)sin)) {
922 /* It is restricted for some reason.. probably
930 return (num_eligible_addr);
934 static struct in_addr
935 sctp_choose_v4_boundall(struct sctp_inpcb *inp,
936 struct sctp_tcb *stcb,
937 struct sctp_nets *net,
941 int non_asoc_addr_ok)
943 int cur_addr_num=0, num_prefered=0;
944 uint8_t sin_loop, sin_local;
946 struct sockaddr_in *sin;
948 struct ifaddr_container *ifac;
950 * For v4 we can use (in boundall) any address in the association. If
951 * non_asoc_addr_ok is set we can use any address (at least in theory).
952 * So we look for prefered addresses first. If we find one, we use it.
953 * Otherwise we next try to get an address on the interface, which we
954 * should be able to do (unless non_asoc_addr_ok is false and we are
955 * routed out that way). In these cases where we can't use the address
956 * of the interface we go through all the ifn's looking for an address
957 * we can use and fill that in. Punting means we send back address
958 * 0, which will probably cause problems actually since then IP will
959 * fill in the address of the route ifn, which means we probably already
960 * rejected it.. i.e. here comes an abort :-<.
964 cur_addr_num = net->indx_of_eligible_next_to_use;
967 goto bound_all_v4_plan_c;
969 num_prefered = sctp_count_v4_num_prefered_boundall (ifn, stcb, non_asoc_addr_ok, loopscope, ipv4_scope, &sin_loop, &sin_local);
971 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
972 kprintf("Found %d preferred source addresses\n", num_prefered);
975 if (num_prefered == 0) {
976 /* no eligible addresses, we must use some other
977 * interface address if we can find one.
979 goto bound_all_v4_plan_b;
981 /* Ok we have num_eligible_addr set with how many we can use,
982 * this may vary from call to call due to addresses being deprecated etc..
984 if (cur_addr_num >= num_prefered) {
987 /* select the nth address from the list (where cur_addr_num is the nth) and
988 * 0 is the first one, 1 is the second one etc...
991 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
992 kprintf("cur_addr_num:%d\n", cur_addr_num);
995 sin = sctp_select_v4_nth_prefered_addr_from_ifn_boundall (ifn, stcb, non_asoc_addr_ok, loopscope,
996 ipv4_scope, cur_addr_num);
998 /* if sin is NULL something changed??, plan_a now */
1000 return (sin->sin_addr);
1004 * plan_b: Look at the interface that we emit on
1005 * and see if we can find an acceptable address.
1007 bound_all_v4_plan_b:
1008 TAILQ_FOREACH(ifac, &ifn->if_addrheads[mycpuid], ifa_link) {
1009 struct ifaddr *ifa = ifac->ifa;
1011 sin = sctp_is_v4_ifa_addr_acceptable (ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
1015 if ((non_asoc_addr_ok == 0) && sctp_is_addr_restricted(stcb, (struct sockaddr *)sin)) {
1016 /* It is restricted for some reason.. probably
1022 return (sin->sin_addr);
1025 * plan_c: Look at all interfaces and find a prefered
1026 * address. If we reache here we are in trouble I think.
1028 bound_all_v4_plan_c:
1029 for (ifn = TAILQ_FIRST(&ifnet);
1030 ifn && (ifn != inp->next_ifn_touse);
1031 ifn=TAILQ_NEXT(ifn, if_list)) {
1032 if (loopscope == 0 && ifn->if_type == IFT_LOOP) {
1033 /* wrong base scope */
1036 if (ifn == rt->rt_ifp)
1037 /* already looked at this guy */
1039 num_prefered = sctp_count_v4_num_prefered_boundall (ifn, stcb, non_asoc_addr_ok,
1040 loopscope, ipv4_scope, &sin_loop, &sin_local);
1042 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1043 kprintf("Found ifn:%p %d preferred source addresses\n", ifn, num_prefered);
1046 if (num_prefered == 0) {
1048 * None on this interface.
1052 /* Ok we have num_eligible_addr set with how many we can use,
1053 * this may vary from call to call due to addresses being deprecated etc..
1055 if (cur_addr_num >= num_prefered) {
1058 sin = sctp_select_v4_nth_prefered_addr_from_ifn_boundall (ifn, stcb, non_asoc_addr_ok, loopscope,
1059 ipv4_scope, cur_addr_num);
1062 return (sin->sin_addr);
1067 * plan_d: We are in deep trouble. No prefered address on
1068 * any interface. And the emit interface does not
1069 * even have an acceptable address. Take anything
1070 * we can get! If this does not work we are
1071 * probably going to emit a packet that will
1072 * illicit an ABORT, falling through.
1075 for (ifn = TAILQ_FIRST(&ifnet);
1076 ifn && (ifn != inp->next_ifn_touse);
1077 ifn=TAILQ_NEXT(ifn, if_list)) {
1078 if (loopscope == 0 && ifn->if_type == IFT_LOOP) {
1079 /* wrong base scope */
1082 if (ifn == rt->rt_ifp)
1083 /* already looked at this guy */
1086 TAILQ_FOREACH(ifac, &ifn->if_addrheads[mycpuid], ifa_link) {
1087 struct ifaddr *ifa = ifac->ifa;
1089 sin = sctp_is_v4_ifa_addr_acceptable (ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
1093 if ((non_asoc_addr_ok == 0) && sctp_is_addr_restricted(stcb, (struct sockaddr *)sin)) {
1094 /* It is restricted for some reason.. probably
1100 return (sin->sin_addr);
1104 * Ok we can find NO address to source from that is
1105 * not on our negative list. It is either the special
1106 * ASCONF case where we are sourceing from a intf that
1107 * has been ifconfig'd to a different address (i.e.
1108 * it holds a ADD/DEL/SET-PRIM and the proper lookup
1109 * address. OR we are hosed, and this baby is going
1110 * to abort the association.
1112 if (non_asoc_addr_ok) {
1113 return (((struct sockaddr_in *)(rt->rt_ifa->ifa_addr))->sin_addr);
1115 memset(&ans, 0, sizeof(ans));
1122 /* tcb may be NULL */
1124 sctp_ipv4_source_address_selection(struct sctp_inpcb *inp,
1125 struct sctp_tcb *stcb, struct route *ro, struct sctp_nets *net,
1126 int non_asoc_addr_ok)
1129 struct sockaddr_in *to = (struct sockaddr_in *)&ro->ro_dst;
1130 uint8_t ipv4_scope, loopscope;
1133 * - Find the route if needed, cache if I can.
1134 * - Look at interface address in route, Is it
1135 * in the bound list. If so we have the best source.
1136 * - If not we must rotate amongst the addresses.
1140 * Do we need to pay attention to scope. We can have
1141 * a private address or a global address we are sourcing
1142 * or sending to. So if we draw it out
1143 * source * dest * result
1144 * ------------------------------------------
1145 * a Private * Global * NAT?
1146 * ------------------------------------------
1147 * b Private * Private * No problem
1148 * ------------------------------------------
1149 * c Global * Private * Huh, How will this work?
1150 * ------------------------------------------
1151 * d Global * Global * No Problem
1152 * ------------------------------------------
1154 * And then we add to that what happens if there are multiple
1155 * addresses assigned to an interface. Remember the ifa on a
1156 * ifn is a linked list of addresses. So one interface can
1157 * have more than one IPv4 address. What happens if we
1158 * have both a private and a global address? Do we then
1159 * use context of destination to sort out which one is
1160 * best? And what about NAT's sending P->G may get you
1161 * a NAT translation, or should you select the G thats
1162 * on the interface in preference.
1166 * - count the number of addresses on the interface.
1167 * - if its one, no problem except case <c>. For <a>
1168 * we will assume a NAT out there.
1169 * - if there are more than one, then we need to worry
1170 * about scope P or G. We should prefer G -> G and
1171 * P -> P if possible. Then as a secondary fall back
1172 * to mixed types G->P being a last ditch one.
1173 * - The above all works for bound all, but bound
1174 * specific we need to use the same concept but instead
1175 * only consider the bound addresses. If the bound set
1176 * is NOT assigned to the interface then we must use
1177 * rotation amongst them.
1179 * Notes: For v4, we can always punt and let ip_output
1180 * decide by sending back a source of 0.0.0.0
1183 if (ro->ro_rt == NULL) {
1185 * Need a route to cache.
1188 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
1189 rtalloc_ign(ro, 0UL);
1194 if (ro->ro_rt == NULL) {
1195 /* No route to host .. punt */
1196 memset(&ans, 0, sizeof(ans));
1199 /* Setup our scopes */
1201 ipv4_scope = stcb->asoc.ipv4_local_scope;
1202 loopscope = stcb->asoc.loopback_scope;
1204 /* Scope based on outbound address */
1205 if ((IN4_ISPRIVATE_ADDRESS(&to->sin_addr))) {
1208 } else if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
1217 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1218 kprintf("Scope setup loop:%d ipv4_scope:%d\n",
1219 loopscope, ipv4_scope);
1222 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1224 * When bound to all if the address list is set
1225 * it is a negative list. Addresses being added
1228 return (sctp_choose_v4_boundall(inp, stcb, net, ro->ro_rt,
1229 ipv4_scope, loopscope, non_asoc_addr_ok));
1232 * Three possiblities here:
1234 * a) stcb is NULL, which means we operate only from
1235 * the list of addresses (ifa's) bound to the assoc and
1236 * we care not about the list.
1237 * b) stcb is NOT-NULL, which means we have an assoc structure and
1238 * auto-asconf is on. This means that the list of addresses is
1239 * a NOT list. We use the list from the inp, but any listed address
1240 * in our list is NOT yet added. However if the non_asoc_addr_ok is
1241 * set we CAN use an address NOT available (i.e. being added). Its
1243 * c) stcb is NOT-NULL, which means we have an assoc structure and
1244 * auto-asconf is off. This means that the list of addresses is
1245 * the ONLY addresses I can use.. its positive.
1247 * Note we collapse b & c into the same function just like in
1248 * the v6 address selection.
1251 return (sctp_choose_v4_boundspecific_stcb(inp, stcb, net,
1252 ro->ro_rt, ipv4_scope, loopscope, non_asoc_addr_ok));
1254 return (sctp_choose_v4_boundspecific_inp(inp, ro->ro_rt,
1255 ipv4_scope, loopscope));
1257 /* this should not be reached */
1258 memset(&ans, 0, sizeof(ans));
1264 static struct sockaddr_in6 *
1265 sctp_is_v6_ifa_addr_acceptable (struct ifaddr *ifa, int loopscope, int loc_scope, int *sin_loop, int *sin_local)
1267 struct in6_ifaddr *ifa6;
1268 struct sockaddr_in6 *sin6;
1270 if (ifa->ifa_addr->sa_family != AF_INET6) {
1274 ifa6 = (struct in6_ifaddr *)ifa;
1275 /* ok to use deprecated addresses? */
1276 if (!ip6_use_deprecated) {
1277 if (IFA6_IS_DEPRECATED(ifa6)) {
1278 /* can't use this type */
1282 /* are we ok, with the current state of this address? */
1283 if (ifa6->ia6_flags &
1284 (IN6_IFF_DETACHED | IN6_IFF_NOTREADY | IN6_IFF_ANYCAST)) {
1285 /* Can't use these types */
1288 /* Ok the address may be ok */
1289 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
1290 *sin_local = *sin_loop = 0;
1291 if ((ifa->ifa_ifp->if_type == IFT_LOOP) ||
1292 (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))) {
1295 if (!loopscope && *sin_loop) {
1296 /* Its a loopback address and we don't have loop scope */
1299 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1300 /* we skip unspecifed addresses */
1304 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1307 if (!loc_scope && *sin_local) {
1308 /* Its a link local address, and we don't have link local scope */
1315 static struct sockaddr_in6 *
1316 sctp_choose_v6_boundspecific_stcb(struct sctp_inpcb *inp,
1317 struct sctp_tcb *stcb,
1318 struct sctp_nets *net,
1322 int non_asoc_addr_ok)
1325 * Each endpoint has a list of local addresses associated
1326 * with it. The address list is either a "negative list" i.e.
1327 * those addresses that are NOT allowed to be used as a source OR
1328 * a "postive list" i.e. those addresses that CAN be used.
1330 * Its a negative list if asconf is allowed. What we do
1331 * in this case is use the ep address list BUT we have
1332 * to cross check it against the negative list.
1334 * In the case where NO asconf is allowed, we have just
1335 * a straight association level list that we must use to
1336 * find a source address.
1338 struct sctp_laddr *laddr, *starting_point;
1339 struct sockaddr_in6 *sin6;
1340 int sin_loop, sin_local;
1341 int start_at_beginning=0;
1345 if (inp->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) {
1347 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1348 kprintf("Have a STCB - asconf allowed, not bound all have a netgative list\n");
1351 /* first question, is the ifn we will emit on
1352 * in our list, if so, we want that one.
1355 struct ifaddr_container *ifac;
1357 TAILQ_FOREACH(ifac, &ifn->if_addrheads[mycpuid], ifa_link) {
1358 struct ifaddr *ifa = ifac->ifa;
1360 if (sctp_is_addr_in_ep(inp, ifa)) {
1361 sin6 = sctp_is_v6_ifa_addr_acceptable (ifa, loopscope, loc_scope, &sin_loop, &sin_local);
1364 if ((non_asoc_addr_ok == 0) &&
1365 (sctp_is_addr_restricted(stcb, (struct sockaddr *)sin6))) {
1366 /* on the no-no list */
1373 starting_point = stcb->asoc.last_used_address;
1374 /* First try for matching scope */
1376 if (stcb->asoc.last_used_address == NULL) {
1377 start_at_beginning=1;
1378 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
1380 /* search beginning with the last used address */
1381 for (laddr = stcb->asoc.last_used_address; laddr;
1382 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
1383 if (laddr->ifa == NULL) {
1384 /* address has been removed */
1387 sin6 = sctp_is_v6_ifa_addr_acceptable (laddr->ifa, loopscope, loc_scope, &sin_loop, &sin_local);
1390 if ((non_asoc_addr_ok == 0) && (sctp_is_addr_restricted(stcb, (struct sockaddr *)sin6))) {
1391 /* on the no-no list */
1394 /* is it of matching scope ? */
1395 if ((loopscope == 0) &&
1399 /* all of global scope we are ok with it */
1402 if (loopscope && sin_loop)
1403 /* both on the loopback, thats ok */
1405 if (loc_scope && sin_local)
1406 /* both local scope */
1410 if (start_at_beginning == 0) {
1411 stcb->asoc.last_used_address = NULL;
1412 goto sctp_from_the_top;
1414 /* now try for any higher scope than the destination */
1415 stcb->asoc.last_used_address = starting_point;
1416 start_at_beginning = 0;
1418 if (stcb->asoc.last_used_address == NULL) {
1419 start_at_beginning=1;
1420 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
1422 /* search beginning with the last used address */
1423 for (laddr = stcb->asoc.last_used_address; laddr;
1424 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
1425 if (laddr->ifa == NULL) {
1426 /* address has been removed */
1429 sin6 = sctp_is_v6_ifa_addr_acceptable (laddr->ifa, loopscope, loc_scope, &sin_loop, &sin_local);
1432 if ((non_asoc_addr_ok == 0) && (sctp_is_addr_restricted(stcb, (struct sockaddr *)sin6))) {
1433 /* on the no-no list */
1438 if (start_at_beginning == 0) {
1439 stcb->asoc.last_used_address = NULL;
1440 goto sctp_from_the_top2;
1444 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1445 kprintf("Have a STCB - no asconf allowed, not bound all have a positive list\n");
1448 /* First try for interface output match */
1449 LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
1451 if (laddr->ifa == NULL) {
1452 /* address has been removed */
1455 sin6 = sctp_is_v6_ifa_addr_acceptable (laddr->ifa, loopscope, loc_scope, &sin_loop, &sin_local);
1458 /* first question, is laddr->ifa an address associated with the emit interface */
1460 struct ifaddr_container *ifac;
1462 TAILQ_FOREACH(ifac, &ifn->if_addrheads[mycpuid], ifa_link) {
1463 struct ifaddr *ifa = ifac->ifa;
1465 if (laddr->ifa == ifa) {
1466 sin6 = (struct sockaddr_in6 *)laddr->ifa->ifa_addr;
1469 if (sctp_cmpaddr(ifa->ifa_addr, laddr->ifa->ifa_addr) == 1) {
1470 sin6 = (struct sockaddr_in6 *)laddr->ifa->ifa_addr;
1476 /* Next try for matching scope */
1477 LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
1479 if (laddr->ifa == NULL) {
1480 /* address has been removed */
1483 sin6 = sctp_is_v6_ifa_addr_acceptable (laddr->ifa, loopscope, loc_scope, &sin_loop, &sin_local);
1487 if ((loopscope == 0) &&
1491 /* all of global scope we are ok with it */
1494 if (loopscope && sin_loop)
1495 /* both on the loopback, thats ok */
1497 if (loc_scope && sin_local)
1498 /* both local scope */
1501 /* ok, now try for a higher scope in the source address */
1502 /* First try for matching scope */
1503 LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
1505 if (laddr->ifa == NULL) {
1506 /* address has been removed */
1509 sin6 = sctp_is_v6_ifa_addr_acceptable (laddr->ifa, loopscope, loc_scope, &sin_loop, &sin_local);
1518 static struct sockaddr_in6 *
1519 sctp_choose_v6_boundspecific_inp(struct sctp_inpcb *inp,
1525 * Here we are bound specific and have only
1526 * an inp. We must find an address that is bound
1527 * that we can give out as a src address. We
1528 * prefer two addresses of same scope if we can
1529 * find them that way.
1531 struct sctp_laddr *laddr;
1532 struct sockaddr_in6 *sin6;
1534 int sin_loop, sin_local;
1536 /* first question, is the ifn we will emit on
1537 * in our list, if so, we want that one.
1542 struct ifaddr_container *ifac;
1544 TAILQ_FOREACH(ifac, &ifn->if_addrheads[mycpuid], ifa_link) {
1545 struct ifaddr *ifa = ifac->ifa;
1547 sin6 = sctp_is_v6_ifa_addr_acceptable (ifa, loopscope, loc_scope, &sin_loop, &sin_local);
1550 if (sctp_is_addr_in_ep(inp, ifa)) {
1555 for (laddr = LIST_FIRST(&inp->sctp_addr_list);
1556 laddr && (laddr != inp->next_addr_touse);
1557 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
1558 if (laddr->ifa == NULL) {
1559 /* address has been removed */
1562 sin6 = sctp_is_v6_ifa_addr_acceptable (laddr->ifa, loopscope, loc_scope, &sin_loop, &sin_local);
1566 if ((loopscope == 0) &&
1570 /* all of global scope we are ok with it */
1573 if (loopscope && sin_loop)
1574 /* both on the loopback, thats ok */
1576 if (loc_scope && sin_local)
1577 /* both local scope */
1581 /* if we reach here, we could not find two addresses
1582 * of the same scope to give out. Lets look for any higher level
1583 * scope for a source address.
1585 for (laddr = LIST_FIRST(&inp->sctp_addr_list);
1586 laddr && (laddr != inp->next_addr_touse);
1587 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
1588 if (laddr->ifa == NULL) {
1589 /* address has been removed */
1592 sin6 = sctp_is_v6_ifa_addr_acceptable (laddr->ifa, loopscope, loc_scope, &sin_loop, &sin_local);
1597 /* no address bound can be a source for the destination */
1599 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1600 kprintf("Src address selection for EP, no acceptable src address found for address\n");
1607 static struct sockaddr_in6 *
1608 sctp_select_v6_nth_addr_from_ifn_boundall (struct ifnet *ifn, struct sctp_tcb *stcb, int non_asoc_addr_ok, uint8_t loopscope,
1609 uint8_t loc_scope, int cur_addr_num, int match_scope)
1611 struct ifaddr_container *ifac;
1612 struct sockaddr_in6 *sin6;
1613 int sin_loop, sin_local;
1614 int num_eligible_addr = 0;
1616 TAILQ_FOREACH(ifac, &ifn->if_addrheads[mycpuid], ifa_link) {
1617 struct ifaddr *ifa = ifac->ifa;
1619 sin6 = sctp_is_v6_ifa_addr_acceptable (ifa, loopscope, loc_scope, &sin_loop, &sin_local);
1623 if ((non_asoc_addr_ok == 0) && sctp_is_addr_restricted(stcb, (struct sockaddr *)sin6)) {
1624 /* It is restricted for some reason.. probably
1631 /* Here we are asked to match scope if possible */
1632 if (loopscope && sin_loop)
1633 /* src and destination are loopback scope */
1635 if (loc_scope && sin_local)
1636 /* src and destination are local scope */
1638 if ((loopscope == 0) &&
1642 /* src and destination are global scope */
1647 if (num_eligible_addr == cur_addr_num) {
1651 num_eligible_addr++;
1658 sctp_count_v6_num_eligible_boundall (struct ifnet *ifn, struct sctp_tcb *stcb,
1659 int non_asoc_addr_ok, uint8_t loopscope, uint8_t loc_scope)
1661 struct ifaddr_container *ifac;
1662 struct sockaddr_in6 *sin6;
1663 int num_eligible_addr = 0;
1664 int sin_loop, sin_local;
1666 TAILQ_FOREACH(ifac, &ifn->if_addrheads[mycpuid], ifa_link) {
1667 struct ifaddr *ifa = ifac->ifa;
1669 sin6 = sctp_is_v6_ifa_addr_acceptable (ifa, loopscope, loc_scope, &sin_loop, &sin_local);
1673 if ((non_asoc_addr_ok == 0) && sctp_is_addr_restricted(stcb, (struct sockaddr *)sin6)) {
1674 /* It is restricted for some reason.. probably
1680 num_eligible_addr++;
1682 return (num_eligible_addr);
1686 static struct sockaddr_in6 *
1687 sctp_choose_v6_boundall(struct sctp_inpcb *inp,
1688 struct sctp_tcb *stcb,
1689 struct sctp_nets *net,
1693 int non_asoc_addr_ok)
1695 /* Ok, we are bound all SO any address
1696 * is ok to use as long as it is NOT in the negative
1699 int num_eligible_addr;
1701 int started_at_beginning=0;
1702 int match_scope_prefered;
1703 /* first question is, how many eligible addresses are
1704 * there for the destination ifn that we are using that
1705 * are within the proper scope?
1708 struct sockaddr_in6 *sin6;
1712 cur_addr_num = net->indx_of_eligible_next_to_use;
1714 if (cur_addr_num == 0) {
1715 match_scope_prefered = 1;
1717 match_scope_prefered = 0;
1719 num_eligible_addr = sctp_count_v6_num_eligible_boundall (ifn, stcb, non_asoc_addr_ok, loopscope, loc_scope);
1721 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1722 kprintf("Found %d eligible source addresses\n", num_eligible_addr);
1725 if (num_eligible_addr == 0) {
1726 /* no eligible addresses, we must use some other
1727 * interface address if we can find one.
1729 goto bound_all_v6_plan_b;
1731 /* Ok we have num_eligible_addr set with how many we can use,
1732 * this may vary from call to call due to addresses being deprecated etc..
1734 if (cur_addr_num >= num_eligible_addr) {
1737 /* select the nth address from the list (where cur_addr_num is the nth) and
1738 * 0 is the first one, 1 is the second one etc...
1741 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1742 kprintf("cur_addr_num:%d match_scope_prefered:%d select it\n",
1743 cur_addr_num, match_scope_prefered);
1746 sin6 = sctp_select_v6_nth_addr_from_ifn_boundall (ifn, stcb, non_asoc_addr_ok, loopscope,
1747 loc_scope, cur_addr_num, match_scope_prefered);
1748 if (match_scope_prefered && (sin6 == NULL)) {
1749 /* retry without the preference for matching scope */
1751 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1752 kprintf("retry with no match_scope_prefered\n");
1755 sin6 = sctp_select_v6_nth_addr_from_ifn_boundall (ifn, stcb, non_asoc_addr_ok, loopscope,
1756 loc_scope, cur_addr_num, 0);
1760 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1761 kprintf("Selected address %d ifn:%p for the route\n", cur_addr_num, ifn);
1765 /* store so we get the next one */
1766 if (cur_addr_num < 255)
1767 net->indx_of_eligible_next_to_use = cur_addr_num + 1;
1769 net->indx_of_eligible_next_to_use = 0;
1773 num_eligible_addr = 0;
1774 bound_all_v6_plan_b:
1775 /* ok, if we reach here we either fell through
1776 * due to something changing during an interupt (unlikely)
1777 * or we have NO eligible source addresses for the ifn
1778 * of the route (most likely). We must look at all the other
1779 * interfaces EXCEPT rt->rt_ifp and do the same game.
1782 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1783 kprintf("bound-all Plan B\n");
1786 if (inp->next_ifn_touse == NULL) {
1787 started_at_beginning=1;
1788 inp->next_ifn_touse = TAILQ_FIRST(&ifnet);
1790 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1791 kprintf("Start at first IFN:%p\n", inp->next_ifn_touse);
1795 inp->next_ifn_touse = TAILQ_NEXT(inp->next_ifn_touse, if_list);
1797 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1798 kprintf("Resume at IFN:%p\n", inp->next_ifn_touse);
1801 if (inp->next_ifn_touse == NULL) {
1803 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1804 kprintf("IFN Resets\n");
1807 started_at_beginning=1;
1808 inp->next_ifn_touse = TAILQ_FIRST(&ifnet);
1811 for (ifn = inp->next_ifn_touse; ifn;
1812 ifn = TAILQ_NEXT(ifn, if_list)) {
1813 if (loopscope == 0 && ifn->if_type == IFT_LOOP) {
1814 /* wrong base scope */
1817 if (loc_scope && (ifn->if_index != loc_scope)) {
1818 /* by definition the scope (from to->sin6_scopeid)
1819 * must match that of the interface. If not then
1820 * we could pick a wrong scope for the address.
1821 * Ususally we don't hit plan-b since the route
1822 * handles this. However we can hit plan-b when
1823 * we send to local-host so the route is the
1824 * loopback interface, but the destination is a
1829 if (ifn == rt->rt_ifp) {
1830 /* already looked at this guy */
1833 /* Address rotation will only work when we are not
1834 * rotating sourced interfaces and are using the interface
1835 * of the route. We would need to have a per interface index
1836 * in order to do proper rotation.
1838 num_eligible_addr = sctp_count_v6_num_eligible_boundall (ifn, stcb, non_asoc_addr_ok, loopscope, loc_scope);
1840 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1841 kprintf("IFN:%p has %d eligible\n", ifn, num_eligible_addr);
1844 if (num_eligible_addr == 0) {
1845 /* none we can use */
1848 /* Ok we have num_eligible_addr set with how many we can use,
1849 * this may vary from call to call due to addresses being deprecated etc..
1851 inp->next_ifn_touse = ifn;
1853 /* select the first one we can find with perference for matching scope.
1855 sin6 = sctp_select_v6_nth_addr_from_ifn_boundall (ifn, stcb, non_asoc_addr_ok, loopscope, loc_scope, 0, 1);
1857 /* can't find one with matching scope how about a source with higher
1860 sin6 = sctp_select_v6_nth_addr_from_ifn_boundall (ifn, stcb, non_asoc_addr_ok, loopscope, loc_scope, 0, 0);
1862 /* Hmm, can't find one in the interface now */
1866 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1867 kprintf("Selected the %d'th address of ifn:%p\n",
1874 if (started_at_beginning == 0) {
1875 /* we have not been through all of them yet, force
1876 * us to go through them all.
1879 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1880 kprintf("Force a recycle\n");
1883 inp->next_ifn_touse = NULL;
1884 goto bound_all_v6_plan_b;
1890 /* stcb and net may be NULL */
1892 sctp_ipv6_source_address_selection(struct sctp_inpcb *inp,
1893 struct sctp_tcb *stcb, struct route *ro, struct sctp_nets *net,
1894 int non_asoc_addr_ok)
1896 struct in6_addr ans;
1897 struct sockaddr_in6 *rt_addr;
1898 uint8_t loc_scope, loopscope;
1899 struct sockaddr_in6 *to = (struct sockaddr_in6 *)&ro->ro_dst;
1902 * This routine is tricky standard v6 src address
1903 * selection cannot take into account what we have
1904 * bound etc, so we can't use it.
1906 * Instead here is what we must do:
1907 * 1) Make sure we have a route, if we
1908 * don't have a route we can never reach the peer.
1909 * 2) Once we have a route, determine the scope of the
1910 * route. Link local, loopback or global.
1911 * 3) Next we divide into three types. Either we
1912 * are bound all.. which means we want to use
1913 * one of the addresses of the interface we are
1915 * 4a) We have not stcb, which means we are using the
1916 * specific addresses bound on an inp, in this
1917 * case we are similar to the stcb case (4b below)
1918 * accept the list is always a positive list.<or>
1919 * 4b) We are bound specific with a stcb, which means we have a
1920 * list of bound addresses and we must see if the
1921 * ifn of the route is actually one of the bound addresses.
1922 * If not, then we must rotate addresses amongst properly
1923 * scoped bound addresses, if so we use the address
1925 * 5) Always, no matter which path we take through the above
1926 * we must be sure the source address we use is allowed to
1927 * be used. I.e. IN6_IFF_DETACHED, IN6_IFF_NOTREADY, and IN6_IFF_ANYCAST
1928 * addresses cannot be used.
1929 * 6) Addresses that are deprecated MAY be used
1930 * if (!ip6_use_deprecated) {
1931 * if (IFA6_IS_DEPRECATED(ifa6)) {
1937 /*** 1> determine route, if not already done */
1938 if (ro->ro_rt == NULL) {
1940 * Need a route to cache.
1942 #ifndef SCOPEDROUTING
1944 scope_save = to->sin6_scope_id;
1945 to->sin6_scope_id = 0;
1948 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
1949 rtalloc_ign(ro, 0UL);
1953 #ifndef SCOPEDROUTING
1954 to->sin6_scope_id = scope_save;
1957 if (ro->ro_rt == NULL) {
1959 * no route to host. this packet is going no-where.
1960 * We probably should make sure we arrange to send back
1964 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1965 kprintf("No route to host, this packet cannot be sent!\n");
1968 memset(&ans, 0, sizeof(ans));
1972 /*** 2a> determine scope for outbound address/route */
1973 loc_scope = loopscope = 0;
1975 * We base our scope on the outbound packet scope and route,
1976 * NOT the TCB (if there is one). This way in local scope we will only
1977 * use a local scope src address when we send to a local address.
1980 if (IN6_IS_ADDR_LOOPBACK(&to->sin6_addr)) {
1981 /* If the route goes to the loopback address OR
1982 * the address is a loopback address, we are loopback
1986 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1987 kprintf("Loopback scope is set\n");
1993 /* mark it as local */
1994 net->addr_is_local = 1;
1997 } else if (IN6_IS_ADDR_LINKLOCAL(&to->sin6_addr)) {
1999 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
2000 kprintf("Link local scope is set, id:%d\n", to->sin6_scope_id);
2003 if (to->sin6_scope_id)
2004 loc_scope = to->sin6_scope_id;
2011 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
2012 kprintf("Global scope is set\n");
2017 /* now, depending on which way we are bound we call the appropriate
2018 * routine to do steps 3-6
2021 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
2022 kprintf("Destination address:");
2023 sctp_print_address((struct sockaddr *)to);
2027 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2029 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
2030 kprintf("Calling bound-all src addr selection for v6\n");
2033 rt_addr = sctp_choose_v6_boundall(inp, stcb, net, ro->ro_rt, loc_scope, loopscope, non_asoc_addr_ok);
2036 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
2037 kprintf("Calling bound-specific src addr selection for v6\n");
2041 rt_addr = sctp_choose_v6_boundspecific_stcb(inp, stcb, net, ro->ro_rt, loc_scope, loopscope, non_asoc_addr_ok);
2043 /* we can't have a non-asoc address since we have no association */
2044 rt_addr = sctp_choose_v6_boundspecific_inp(inp, ro->ro_rt, loc_scope, loopscope);
2046 if (rt_addr == NULL) {
2047 /* no suitable address? */
2048 struct in6_addr in6;
2050 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
2051 kprintf("V6 packet will reach dead-end no suitable src address\n");
2054 memset(&in6, 0, sizeof(in6));
2058 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
2059 kprintf("Source address selected is:");
2060 sctp_print_address((struct sockaddr *)rt_addr);
2063 return (rt_addr->sin6_addr);
2067 sctp_get_ect(struct sctp_tcb *stcb,
2068 struct sctp_tmit_chunk *chk)
2070 uint8_t this_random;
2076 if (sctp_ecn_nonce == 0)
2077 /* no nonce, always return ECT0 */
2078 return (SCTP_ECT0_BIT);
2080 if (stcb->asoc.peer_supports_ecn_nonce == 0) {
2081 /* Peer does NOT support it, so we send a ECT0 only */
2082 return (SCTP_ECT0_BIT);
2086 return (SCTP_ECT0_BIT);
2088 if (((stcb->asoc.hb_random_idx == 3) &&
2089 (stcb->asoc.hb_ect_randombit > 7)) ||
2090 (stcb->asoc.hb_random_idx > 3)) {
2092 rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
2093 memcpy(stcb->asoc.hb_random_values, &rndval,
2094 sizeof(stcb->asoc.hb_random_values));
2095 this_random = stcb->asoc.hb_random_values[0];
2096 stcb->asoc.hb_random_idx = 0;
2097 stcb->asoc.hb_ect_randombit = 0;
2099 if (stcb->asoc.hb_ect_randombit > 7) {
2100 stcb->asoc.hb_ect_randombit = 0;
2101 stcb->asoc.hb_random_idx++;
2103 this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
2105 if ((this_random >> stcb->asoc.hb_ect_randombit) & 0x01) {
2107 /* ECN Nonce stuff */
2108 chk->rec.data.ect_nonce = SCTP_ECT1_BIT;
2109 stcb->asoc.hb_ect_randombit++;
2110 return (SCTP_ECT1_BIT);
2112 stcb->asoc.hb_ect_randombit++;
2113 return (SCTP_ECT0_BIT);
2117 extern int sctp_no_csum_on_loopback;
2120 sctp_lowlevel_chunk_output(struct sctp_inpcb *inp,
2121 struct sctp_tcb *stcb, /* may be NULL */
2122 struct sctp_nets *net,
2123 struct sockaddr *to,
2125 int nofragment_flag,
2127 struct sctp_tmit_chunk *chk,
2129 /* nofragment_flag to tell if IP_DF should be set (IPv4 only) */
2132 * Given a mbuf chain (via m_next) that holds a packet header
2133 * WITH a SCTPHDR but no IP header, endpoint inp and sa structure.
2134 * - calculate SCTP checksum and fill in
2135 * - prepend a IP address header
2136 * - if boundall use INADDR_ANY
2137 * - if boundspecific do source address selection
2138 * - set fragmentation option for ipV4
2139 * - On return from IP output, check/adjust mtu size
2140 * - of output interface and smallest_mtu size as well.
2142 struct sctphdr *sctphdr;
2146 unsigned int have_mtu;
2149 if ((net) && (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)) {
2153 if ((m->m_flags & M_PKTHDR) == 0) {
2155 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
2156 kprintf("Software error: sctp_lowlevel_chunk_output() called with non pkthdr!\n");
2162 /* Calculate the csum and fill in the length of the packet */
2163 sctphdr = mtod(m, struct sctphdr *);
2165 if (sctp_no_csum_on_loopback &&
2167 (stcb->asoc.loopback_scope)) {
2168 sctphdr->checksum = 0;
2169 m->m_pkthdr.len = sctp_calculate_len(m);
2171 sctphdr->checksum = 0;
2172 csum = sctp_calculate_sum(m, &m->m_pkthdr.len, 0);
2173 sctphdr->checksum = csum;
2175 if (to->sa_family == AF_INET) {
2177 struct route iproute;
2178 M_PREPEND(m, sizeof(struct ip), MB_DONTWAIT);
2180 /* failed to prepend data, give up */
2183 ip = mtod(m, struct ip *);
2184 ip->ip_v = IPVERSION;
2185 ip->ip_hl = (sizeof(struct ip) >> 2);
2186 if (nofragment_flag) {
2187 #if defined(WITH_CONVERT_IP_OFF) || defined(__FreeBSD__) || defined(__DragonFly__)
2188 #if defined( __OpenBSD__) || defined(__NetBSD__)
2189 /* OpenBSD has WITH_CONVERT_IP_OFF defined?? */
2190 ip->ip_off = htons(IP_DF);
2195 ip->ip_off = htons(IP_DF);
2200 /* FreeBSD and Apple have RANDOM_IP_ID switch */
2201 #if defined(RANDOM_IP_ID) || defined(__NetBSD__) || defined(__OpenBSD__)
2202 ip->ip_id = htons(ip_randomid());
2204 ip->ip_id = htons(ip_id++);
2207 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
2208 ip->ip_ttl = inp->ip_inp.inp.inp_ip_ttl;
2210 ip->ip_ttl = inp->inp_ip_ttl;
2212 #if defined(__OpenBSD__) || defined(__NetBSD__)
2213 ip->ip_len = htons(m->m_pkthdr.len);
2215 ip->ip_len = m->m_pkthdr.len;
2218 if ((stcb->asoc.ecn_allowed) && ecn_ok) {
2220 #if defined(__FreeBSD__) || defined (__APPLE__) || defined(__DragonFly__)
2221 ip->ip_tos = (u_char)((inp->ip_inp.inp.inp_ip_tos & 0x000000fc) |
2222 sctp_get_ect(stcb, chk));
2223 #elif defined(__NetBSD__)
2224 ip->ip_tos = (u_char)((inp->ip_inp.inp.inp_ip.ip_tos & 0x000000fc) |
2225 sctp_get_ect(stcb, chk));
2227 ip->ip_tos = (u_char)((inp->inp_ip_tos & 0x000000fc) |
2228 sctp_get_ect(stcb, chk));
2232 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
2233 ip->ip_tos = inp->ip_inp.inp.inp_ip_tos;
2234 #elif defined(__NetBSD__)
2235 ip->ip_tos = inp->ip_inp.inp.inp_ip.ip_tos;
2237 ip->ip_tos = inp->inp_ip_tos;
2241 /* no association at all */
2242 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
2243 ip->ip_tos = inp->ip_inp.inp.inp_ip_tos;
2245 ip->ip_tos = inp->inp_ip_tos;
2248 ip->ip_p = IPPROTO_SCTP;
2252 memset(&iproute, 0, sizeof(iproute));
2253 memcpy(&ro->ro_dst, to, to->sa_len);
2255 ro = (struct route *)&net->ro;
2257 /* Now the address selection part */
2258 ip->ip_dst.s_addr = ((struct sockaddr_in *)to)->sin_addr.s_addr;
2260 /* call the routine to select the src address */
2262 if (net->src_addr_selected == 0) {
2263 /* Cache the source address */
2264 ((struct sockaddr_in *)&net->ro._s_addr)->sin_addr = sctp_ipv4_source_address_selection(inp,
2266 ro, net, out_of_asoc_ok);
2268 net->src_addr_selected = 1;
2270 ip->ip_src = ((struct sockaddr_in *)&net->ro._s_addr)->sin_addr;
2272 ip->ip_src = sctp_ipv4_source_address_selection(inp,
2273 stcb, ro, net, out_of_asoc_ok);
2276 * If source address selection fails and we find no route then
2277 * the ip_ouput should fail as well with a NO_ROUTE_TO_HOST
2278 * type error. We probably should catch that somewhere and
2279 * abort the association right away (assuming this is an INIT
2282 if ((ro->ro_rt == NULL)) {
2284 * src addr selection failed to find a route (or valid
2285 * source addr), so we can't get there from here!
2288 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
2289 kprintf("low_level_output: dropped v4 packet- no valid source addr\n");
2290 kprintf("Destination was %x\n", (u_int)(ntohl(ip->ip_dst.s_addr)));
2292 #endif /* SCTP_DEBUG */
2294 if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb)
2295 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
2297 SCTP_FAILED_THRESHOLD,
2299 net->dest_state &= ~SCTP_ADDR_REACHABLE;
2300 net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
2302 if (net == stcb->asoc.primary_destination) {
2303 /* need a new primary */
2304 struct sctp_nets *alt;
2305 alt = sctp_find_alternate_net(stcb, net);
2307 if (sctp_set_primary_addr(stcb,
2310 net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
2311 net->src_addr_selected = 0;
2318 return (EHOSTUNREACH);
2320 have_mtu = ro->ro_rt->rt_ifp->if_mtu;
2323 o_flgs = (IP_RAWOUTPUT | (inp->sctp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)));
2325 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
2326 kprintf("Calling ipv4 output routine from low level src addr:%x\n",
2327 (u_int)(ntohl(ip->ip_src.s_addr)));
2328 kprintf("Destination is %x\n", (u_int)(ntohl(ip->ip_dst.s_addr)));
2329 kprintf("RTP route is %p through\n", ro->ro_rt);
2332 if ((have_mtu) && (net) && (have_mtu > net->mtu)) {
2333 ro->ro_rt->rt_ifp->if_mtu = net->mtu;
2335 ret = ip_output(m, inp->ip_inp.inp.inp_options,
2336 ro, o_flgs, inp->ip_inp.inp.inp_moptions
2337 #if defined(__OpenBSD__) || (defined(__FreeBSD__) && __FreeBSD_version >= 480000) \
2338 || defined(__DragonFly__)
2341 #if defined(__NetBSD__)
2342 ,(struct socket *)inp->sctp_socket
2346 if ((ro->ro_rt) && (have_mtu) && (net) && (have_mtu > net->mtu)) {
2347 ro->ro_rt->rt_ifp->if_mtu = have_mtu;
2349 sctp_pegs[SCTP_DATAGRAMS_SENT]++;
2351 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
2352 kprintf("Ip output returns %d\n", ret);
2356 /* free tempy routes */
2360 /* PMTU check versus smallest asoc MTU goes here */
2361 if (ro->ro_rt != NULL) {
2362 if (ro->ro_rt->rt_rmx.rmx_mtu &&
2363 (stcb->asoc.smallest_mtu > ro->ro_rt->rt_rmx.rmx_mtu)) {
2364 sctp_mtu_size_reset(inp, &stcb->asoc,
2365 ro->ro_rt->rt_rmx.rmx_mtu);
2368 /* route was freed */
2369 net->src_addr_selected = 0;
2375 else if (to->sa_family == AF_INET6) {
2376 struct ip6_hdr *ip6h;
2377 #ifdef NEW_STRUCT_ROUTE
2378 struct route ip6route;
2380 struct route_in6 ip6route;
2384 uint16_t flowBottom;
2385 u_char tosBottom, tosTop;
2386 struct sockaddr_in6 *sin6, tmp, *lsa6, lsa6_tmp;
2387 struct sockaddr_in6 lsa6_storage;
2390 u_short prev_port=0;
2392 M_PREPEND(m, sizeof(struct ip6_hdr), MB_DONTWAIT);
2394 /* failed to prepend data, give up */
2397 ip6h = mtod(m, struct ip6_hdr *);
2400 * We assume here that inp_flow is in host byte order within
2403 flowBottom = ((struct in6pcb *)inp)->in6p_flowinfo & 0x0000ffff;
2404 flowTop = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x000f0000) >> 16);
2406 tosTop = (((((struct in6pcb *)inp)->in6p_flowinfo & 0xf0) >> 4) | IPV6_VERSION);
2408 /* protect *sin6 from overwrite */
2409 sin6 = (struct sockaddr_in6 *)to;
2413 /* KAME hack: embed scopeid */
2414 #if defined(SCTP_BASE_FREEBSD) || defined(__APPLE__) || defined(__DragonFly__)
2415 if (in6_embedscope(&sin6->sin6_addr, sin6, NULL, NULL) != 0)
2417 if (in6_embedscope(&sin6->sin6_addr, sin6) != 0)
2421 memset(&ip6route, 0, sizeof(ip6route));
2422 ro = (struct route *)&ip6route;
2423 memcpy(&ro->ro_dst, sin6, sin6->sin6_len);
2425 ro = (struct route *)&net->ro;
2428 if ((stcb->asoc.ecn_allowed) && ecn_ok) {
2430 tosBottom = (((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) | sctp_get_ect(stcb, chk)) << 4);
2433 tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
2436 /* we could get no asoc if it is a O-O-T-B packet */
2437 tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
2439 ip6h->ip6_flow = htonl(((tosTop << 24) | ((tosBottom|flowTop) << 16) | flowBottom));
2440 ip6h->ip6_nxt = IPPROTO_SCTP;
2441 ip6h->ip6_plen = m->m_pkthdr.len;
2442 ip6h->ip6_dst = sin6->sin6_addr;
2445 * Add SRC address selection here:
2446 * we can only reuse to a limited degree the kame src-addr-sel,
2447 * since we can try their selection but it may not be bound.
2449 bzero(&lsa6_tmp, sizeof(lsa6_tmp));
2450 lsa6_tmp.sin6_family = AF_INET6;
2451 lsa6_tmp.sin6_len = sizeof(lsa6_tmp);
2454 if (net->src_addr_selected == 0) {
2455 /* Cache the source address */
2456 ((struct sockaddr_in6 *)&net->ro._s_addr)->sin6_addr = sctp_ipv6_source_address_selection(inp,
2457 stcb, ro, net, out_of_asoc_ok);
2460 net->src_addr_selected = 1;
2462 lsa6->sin6_addr = ((struct sockaddr_in6 *)&net->ro._s_addr)->sin6_addr;
2464 lsa6->sin6_addr = sctp_ipv6_source_address_selection(
2465 inp, stcb, ro, net, out_of_asoc_ok);
2467 lsa6->sin6_port = inp->sctp_lport;
2469 if ((ro->ro_rt == NULL)) {
2471 * src addr selection failed to find a route (or valid
2472 * source addr), so we can't get there from here!
2475 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
2476 kprintf("low_level_output: dropped v6 pkt- no valid source addr\n");
2481 if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb)
2482 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
2484 SCTP_FAILED_THRESHOLD,
2486 net->dest_state &= ~SCTP_ADDR_REACHABLE;
2487 net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
2489 if (net == stcb->asoc.primary_destination) {
2490 /* need a new primary */
2491 struct sctp_nets *alt;
2492 alt = sctp_find_alternate_net(stcb, net);
2494 if (sctp_set_primary_addr(stcb,
2497 net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
2498 net->src_addr_selected = 0;
2504 return (EHOSTUNREACH);
2507 #ifndef SCOPEDROUTING
2509 * XXX: sa6 may not have a valid sin6_scope_id in
2510 * the non-SCOPEDROUTING case.
2512 bzero(&lsa6_storage, sizeof(lsa6_storage));
2513 lsa6_storage.sin6_family = AF_INET6;
2514 lsa6_storage.sin6_len = sizeof(lsa6_storage);
2515 if ((error = in6_recoverscope(&lsa6_storage, &lsa6->sin6_addr,
2521 lsa6_storage.sin6_addr = lsa6->sin6_addr;
2522 lsa6_storage.sin6_port = inp->sctp_lport;
2523 lsa6 = &lsa6_storage;
2524 #endif /* SCOPEDROUTING */
2525 ip6h->ip6_src = lsa6->sin6_addr;
2528 * We set the hop limit now since there is a good chance that
2529 * our ro pointer is now filled
2531 ip6h->ip6_hlim = in6_selecthlim((struct in6pcb *)&inp->ip_inp.inp,
2533 (ro->ro_rt ? (ro->ro_rt->rt_ifp) : (NULL)) :
2536 ifp = ro->ro_rt->rt_ifp;
2538 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
2539 /* Copy to be sure something bad is not happening */
2540 sin6->sin6_addr = ip6h->ip6_dst;
2541 lsa6->sin6_addr = ip6h->ip6_src;
2543 kprintf("Calling ipv6 output routine from low level\n");
2545 sctp_print_address((struct sockaddr *)lsa6);
2547 sctp_print_address((struct sockaddr *)sin6);
2549 #endif /* SCTP_DEBUG */
2551 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
2552 /* preserve the port and scope for link local send */
2553 prev_scope = sin6->sin6_scope_id;
2554 prev_port = sin6->sin6_port;
2556 ret = ip6_output(m, ((struct in6pcb *)inp)->in6p_outputopts,
2557 #ifdef NEW_STRUCT_ROUTE
2560 (struct route_in6 *)ro,
2563 ((struct in6pcb *)inp)->in6p_moptions,
2564 #if defined(__NetBSD__)
2565 (struct socket *)inp->sctp_socket,
2568 #if (defined(__FreeBSD__) && __FreeBSD_version >= 480000) || defined(__DragonFly__)
2573 /* for link local this must be done */
2574 sin6->sin6_scope_id = prev_scope;
2575 sin6->sin6_port = prev_port;
2578 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
2579 kprintf("return from send is %d\n", ret);
2581 #endif /* SCTP_DEBUG_OUTPUT */
2582 sctp_pegs[SCTP_DATAGRAMS_SENT]++;
2584 /* Now if we had a temp route free it */
2589 /* PMTU check versus smallest asoc MTU goes here */
2590 if (ro->ro_rt == NULL) {
2591 /* Route was freed */
2592 net->src_addr_selected = 0;
2594 if (ro->ro_rt != NULL) {
2595 if (ro->ro_rt->rt_rmx.rmx_mtu &&
2596 (stcb->asoc.smallest_mtu > ro->ro_rt->rt_rmx.rmx_mtu)) {
2597 sctp_mtu_size_reset(inp,
2599 ro->ro_rt->rt_rmx.rmx_mtu);
2602 #if (defined(SCTP_BASE_FREEBSD) && __FreeBSD_version < 500000) || defined(__APPLE__)
2603 #define ND_IFINFO(ifp) (&nd_ifinfo[ifp->if_index])
2604 #endif /* SCTP_BASE_FREEBSD */
2605 if (ND_IFINFO(ifp)->linkmtu &&
2606 (stcb->asoc.smallest_mtu > ND_IFINFO(ifp)->linkmtu)) {
2607 sctp_mtu_size_reset(inp,
2609 ND_IFINFO(ifp)->linkmtu);
2618 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
2619 kprintf("Unknown protocol (TSNH) type %d\n",
2629 sctp_is_address_in_scope(struct ifaddr *ifa,
2630 int ipv4_addr_legal,
2631 int ipv6_addr_legal,
2633 int ipv4_local_scope,
2637 if ((loopback_scope == 0) &&
2639 (ifa->ifa_ifp->if_type == IFT_LOOP)) {
2640 /* skip loopback if not in scope *
2644 if ((ifa->ifa_addr->sa_family == AF_INET) && ipv4_addr_legal) {
2645 struct sockaddr_in *sin;
2646 sin = (struct sockaddr_in *)ifa->ifa_addr;
2647 if (sin->sin_addr.s_addr == 0) {
2648 /* not in scope , unspecified */
2651 if ((ipv4_local_scope == 0) &&
2652 (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
2653 /* private address not in scope */
2656 } else if ((ifa->ifa_addr->sa_family == AF_INET6) && ipv6_addr_legal) {
2657 struct sockaddr_in6 *sin6;
2658 struct in6_ifaddr *ifa6;
2660 ifa6 = (struct in6_ifaddr *)ifa;
2661 /* ok to use deprecated addresses? */
2662 if (!ip6_use_deprecated) {
2663 if (ifa6->ia6_flags &
2664 IN6_IFF_DEPRECATED) {
2668 if (ifa6->ia6_flags &
2671 IN6_IFF_NOTREADY)) {
2674 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
2675 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2676 /* skip unspecifed addresses */
2679 if (/*(local_scope == 0) && */
2680 (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) {
2683 if ((site_scope == 0) &&
2684 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
2695 sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
2697 struct mbuf *m, *m_at, *m_last;
2698 struct sctp_nets *net;
2699 struct sctp_init_msg *initm;
2700 struct sctp_supported_addr_param *sup_addr;
2701 struct sctp_ecn_supported_param *ecn;
2702 struct sctp_prsctp_supported_param *prsctp;
2703 struct sctp_ecn_nonce_supported_param *ecn_nonce;
2704 struct sctp_supported_chunk_types_param *pr_supported;
2708 /* INIT's always go to the primary (and usually ONLY address) */
2710 net = stcb->asoc.primary_destination;
2712 net = TAILQ_FIRST(&stcb->asoc.nets);
2717 /* we confirm any address we send an INIT to */
2718 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2719 sctp_set_primary_addr(stcb, NULL, net);
2721 /* we confirm any address we send an INIT to */
2722 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2725 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
2726 kprintf("Sending INIT to ");
2727 sctp_print_address ((struct sockaddr *)&net->ro._l_addr);
2730 if (((struct sockaddr *)&(net->ro._l_addr))->sa_family == AF_INET6) {
2731 /* special hook, if we are sending to link local
2732 * it will not show up in our private address count.
2734 struct sockaddr_in6 *sin6l;
2735 sin6l = &net->ro._l_addr.sin6;
2736 if (IN6_IS_ADDR_LINKLOCAL(&sin6l->sin6_addr))
2739 if (callout_pending(&net->rxt_timer.timer)) {
2740 /* This case should not happen */
2743 /* start the INIT timer */
2744 if (sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net)) {
2745 /* we are hosed since I can't start the INIT timer? */
2748 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
2750 /* No memory, INIT timer will re-attempt. */
2753 /* make it into a M_EXT */
2754 MCLGET(m, MB_DONTWAIT);
2755 if ((m->m_flags & M_EXT) != M_EXT) {
2756 /* Failed to get cluster buffer */
2760 m->m_data += SCTP_MIN_OVERHEAD;
2761 m->m_len = sizeof(struct sctp_init_msg);
2762 /* Now lets put the SCTP header in place */
2763 initm = mtod(m, struct sctp_init_msg *);
2764 initm->sh.src_port = inp->sctp_lport;
2765 initm->sh.dest_port = stcb->rport;
2766 initm->sh.v_tag = 0;
2767 initm->sh.checksum = 0; /* calculate later */
2768 /* now the chunk header */
2769 initm->msg.ch.chunk_type = SCTP_INITIATION;
2770 initm->msg.ch.chunk_flags = 0;
2771 /* fill in later from mbuf we build */
2772 initm->msg.ch.chunk_length = 0;
2773 /* place in my tag */
2774 initm->msg.init.initiate_tag = htonl(stcb->asoc.my_vtag);
2775 /* set up some of the credits. */
2776 initm->msg.init.a_rwnd = htonl(max(inp->sctp_socket->so_rcv.ssb_hiwat,
2777 SCTP_MINIMAL_RWND));
2779 initm->msg.init.num_outbound_streams = htons(stcb->asoc.pre_open_streams);
2780 initm->msg.init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams);
2781 initm->msg.init.initial_tsn = htonl(stcb->asoc.init_seq_number);
2782 /* now the address restriction */
2783 sup_addr = (struct sctp_supported_addr_param *)((caddr_t)initm +
2785 sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE);
2786 /* we support 2 types IPv6/IPv4 */
2787 sup_addr->ph.param_length = htons(sizeof(*sup_addr) +
2789 sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
2790 sup_addr->addr_type[1] = htons(SCTP_IPV6_ADDRESS);
2791 m->m_len += sizeof(*sup_addr) + sizeof(uint16_t);
2793 /* if (inp->sctp_flags & SCTP_PCB_FLAGS_ADAPTIONEVNT) {*/
2794 if (inp->sctp_ep.adaption_layer_indicator) {
2795 struct sctp_adaption_layer_indication *ali;
2796 ali = (struct sctp_adaption_layer_indication *)(
2797 (caddr_t)sup_addr + sizeof(*sup_addr) + sizeof(uint16_t));
2798 ali->ph.param_type = htons(SCTP_ULP_ADAPTION);
2799 ali->ph.param_length = htons(sizeof(*ali));
2800 ali->indication = ntohl(inp->sctp_ep.adaption_layer_indicator);
2801 m->m_len += sizeof(*ali);
2802 ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali +
2805 ecn = (struct sctp_ecn_supported_param *)((caddr_t)sup_addr +
2806 sizeof(*sup_addr) + sizeof(uint16_t));
2809 /* now any cookie time extensions */
2810 if (stcb->asoc.cookie_preserve_req) {
2811 struct sctp_cookie_perserve_param *cookie_preserve;
2812 cookie_preserve = (struct sctp_cookie_perserve_param *)(ecn);
2813 cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE);
2814 cookie_preserve->ph.param_length = htons(
2815 sizeof(*cookie_preserve));
2816 cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req);
2817 m->m_len += sizeof(*cookie_preserve);
2818 ecn = (struct sctp_ecn_supported_param *)(
2819 (caddr_t)cookie_preserve + sizeof(*cookie_preserve));
2820 stcb->asoc.cookie_preserve_req = 0;
2824 if (sctp_ecn == 1) {
2825 ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
2826 ecn->ph.param_length = htons(sizeof(*ecn));
2827 m->m_len += sizeof(*ecn);
2828 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
2831 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
2833 /* And now tell the peer we do pr-sctp */
2834 prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
2835 prsctp->ph.param_length = htons(sizeof(*prsctp));
2836 m->m_len += sizeof(*prsctp);
2839 /* And now tell the peer we do all the extensions */
2840 pr_supported = (struct sctp_supported_chunk_types_param *)((caddr_t)prsctp +
2843 pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
2844 pr_supported->ph.param_length = htons(sizeof(*pr_supported) + SCTP_EXT_COUNT);
2845 pr_supported->chunk_types[0] = SCTP_ASCONF;
2846 pr_supported->chunk_types[1] = SCTP_ASCONF_ACK;
2847 pr_supported->chunk_types[2] = SCTP_FORWARD_CUM_TSN;
2848 pr_supported->chunk_types[3] = SCTP_PACKET_DROPPED;
2849 pr_supported->chunk_types[4] = SCTP_STREAM_RESET;
2850 pr_supported->chunk_types[5] = 0; /* pad */
2851 pr_supported->chunk_types[6] = 0; /* pad */
2852 pr_supported->chunk_types[7] = 0; /* pad */
2854 m->m_len += (sizeof(*pr_supported) + SCTP_EXT_COUNT + SCTP_PAD_EXT_COUNT);
2855 /* ECN nonce: And now tell the peer we support ECN nonce */
2857 if (sctp_ecn_nonce) {
2858 ecn_nonce = (struct sctp_ecn_nonce_supported_param *)((caddr_t)pr_supported +
2859 sizeof(*pr_supported) + SCTP_EXT_COUNT + SCTP_PAD_EXT_COUNT);
2860 ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
2861 ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
2862 m->m_len += sizeof(*ecn_nonce);
2866 /* now the addresses */
2867 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2872 TAILQ_FOREACH(ifn, &ifnet, if_list) {
2873 struct ifaddr_container *ifac;
2875 if ((stcb->asoc.loopback_scope == 0) &&
2876 (ifn->if_type == IFT_LOOP)) {
2878 * Skip loopback devices if loopback_scope
2883 TAILQ_FOREACH(ifac, &ifn->if_addrheads[mycpuid], ifa_link) {
2884 struct ifaddr *ifa = ifac->ifa;
2886 if (sctp_is_address_in_scope(ifa,
2887 stcb->asoc.ipv4_addr_legal,
2888 stcb->asoc.ipv6_addr_legal,
2889 stcb->asoc.loopback_scope,
2890 stcb->asoc.ipv4_local_scope,
2891 stcb->asoc.local_scope,
2892 stcb->asoc.site_scope) == 0) {
2899 TAILQ_FOREACH(ifn, &ifnet, if_list) {
2900 struct ifaddr_container *ifac;
2902 if ((stcb->asoc.loopback_scope == 0) &&
2903 (ifn->if_type == IFT_LOOP)) {
2905 * Skip loopback devices if loopback_scope
2910 TAILQ_FOREACH(ifac, &ifn->if_addrheads[mycpuid], ifa_link) {
2911 struct ifaddr *ifa = ifac->ifa;
2913 if (sctp_is_address_in_scope(ifa,
2914 stcb->asoc.ipv4_addr_legal,
2915 stcb->asoc.ipv6_addr_legal,
2916 stcb->asoc.loopback_scope,
2917 stcb->asoc.ipv4_local_scope,
2918 stcb->asoc.local_scope,
2919 stcb->asoc.site_scope) == 0) {
2922 m_at = sctp_add_addr_to_mbuf(m_at, ifa);
2927 struct sctp_laddr *laddr;
2930 /* First, how many ? */
2931 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2932 if (laddr->ifa == NULL) {
2935 if (laddr->ifa->ifa_addr == NULL)
2937 if (sctp_is_address_in_scope(laddr->ifa,
2938 stcb->asoc.ipv4_addr_legal,
2939 stcb->asoc.ipv6_addr_legal,
2940 stcb->asoc.loopback_scope,
2941 stcb->asoc.ipv4_local_scope,
2942 stcb->asoc.local_scope,
2943 stcb->asoc.site_scope) == 0) {
2948 /* To get through a NAT we only list addresses if
2949 * we have more than one. That way if you just
2950 * bind a single address we let the source of the init
2951 * dictate our address.
2954 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2955 if (laddr->ifa == NULL) {
2958 if (laddr->ifa->ifa_addr == NULL) {
2962 if (sctp_is_address_in_scope(laddr->ifa,
2963 stcb->asoc.ipv4_addr_legal,
2964 stcb->asoc.ipv6_addr_legal,
2965 stcb->asoc.loopback_scope,
2966 stcb->asoc.ipv4_local_scope,
2967 stcb->asoc.local_scope,
2968 stcb->asoc.site_scope) == 0) {
2971 m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa);
2975 /* calulate the size and update pkt header and chunk header */
2976 m->m_pkthdr.len = 0;
2977 for (m_at = m; m_at; m_at = m_at->m_next) {
2978 if (m_at->m_next == NULL)
2980 m->m_pkthdr.len += m_at->m_len;
2982 initm->msg.ch.chunk_length = htons((m->m_pkthdr.len -
2983 sizeof(struct sctphdr)));
2984 /* We pass 0 here to NOT set IP_DF if its IPv4, we
2985 * ignore the return here since the timer will drive
2989 /* I don't expect this to execute but we will be safe here */
2990 padval = m->m_pkthdr.len % 4;
2991 if ((padval) && (m_last)) {
2992 /* The compiler worries that m_last may not be
2993 * set even though I think it is impossible :->
2994 * however we add m_last here just in case.
2997 ret = sctp_add_pad_tombuf(m_last, (4-padval));
2999 /* Houston we have a problem, no space */
3003 m->m_pkthdr.len += padval;
3006 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
3007 kprintf("Calling lowlevel output stcb:%p net:%p\n", stcb, net);
3010 ret = sctp_lowlevel_chunk_output(inp, stcb, net,
3011 (struct sockaddr *)&net->ro._l_addr, m, 0, 0, NULL, 0);
3013 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
3014 kprintf("Low level output returns %d\n", ret);
3017 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
3018 SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
3022 sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt,
3023 int param_offset, int *abort_processing, struct sctp_chunkhdr *cp)
3025 /* Given a mbuf containing an INIT or INIT-ACK
3026 * with the param_offset being equal to the
3027 * beginning of the params i.e. (iphlen + sizeof(struct sctp_init_msg)
3028 * parse through the parameters to the end of the mbuf verifying
3029 * that all parameters are known.
3031 * For unknown parameters build and return a mbuf with
3032 * UNRECOGNIZED_PARAMETER errors. If the flags indicate
3033 * to stop processing this chunk stop, and set *abort_processing
3036 * By having param_offset be pre-set to where parameters begin
3037 * it is hoped that this routine may be reused in the future
3040 struct sctp_paramhdr *phdr, params;
3042 struct mbuf *mat, *op_err;
3044 int at, limit, pad_needed;
3045 uint16_t ptype, plen;
3048 *abort_processing = 0;
3051 limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk);
3053 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
3054 kprintf("Limit is %d bytes\n", limit);
3060 phdr = sctp_get_next_param(mat, at, ¶ms, sizeof(params));
3061 while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) {
3062 ptype = ntohs(phdr->param_type);
3063 plen = ntohs(phdr->param_length);
3064 limit -= SCTP_SIZE32(plen);
3065 if (plen < sizeof(struct sctp_paramhdr)) {
3067 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
3068 kprintf("sctp_output.c:Impossible length in parameter < %d\n", plen);
3071 *abort_processing = 1;
3074 /* All parameters for all chunks that we
3075 * know/understand are listed here. We process
3076 * them other places and make appropriate
3077 * stop actions per the upper bits. However
3078 * this is the generic routine processor's can
3079 * call to get back an operr.. to either incorporate (init-ack)
3082 if ((ptype == SCTP_HEARTBEAT_INFO) ||
3083 (ptype == SCTP_IPV4_ADDRESS) ||
3084 (ptype == SCTP_IPV6_ADDRESS) ||
3085 (ptype == SCTP_STATE_COOKIE) ||
3086 (ptype == SCTP_UNRECOG_PARAM) ||
3087 (ptype == SCTP_COOKIE_PRESERVE) ||
3088 (ptype == SCTP_SUPPORTED_ADDRTYPE) ||
3089 (ptype == SCTP_PRSCTP_SUPPORTED) ||
3090 (ptype == SCTP_ADD_IP_ADDRESS) ||
3091 (ptype == SCTP_DEL_IP_ADDRESS) ||
3092 (ptype == SCTP_ECN_CAPABLE) ||
3093 (ptype == SCTP_ULP_ADAPTION) ||
3094 (ptype == SCTP_ERROR_CAUSE_IND) ||
3095 (ptype == SCTP_SET_PRIM_ADDR) ||
3096 (ptype == SCTP_SUCCESS_REPORT) ||
3097 (ptype == SCTP_ULP_ADAPTION) ||
3098 (ptype == SCTP_SUPPORTED_CHUNK_EXT) ||
3099 (ptype == SCTP_ECN_NONCE_SUPPORTED)
3102 at += SCTP_SIZE32(plen);
3103 } else if (ptype == SCTP_HOSTNAME_ADDRESS) {
3104 /* We can NOT handle HOST NAME addresses!! */
3106 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
3107 kprintf("Can't handle hostname addresses.. abort processing\n");
3110 *abort_processing = 1;
3111 if (op_err == NULL) {
3112 /* Ok need to try to get a mbuf */
3113 MGETHDR(op_err, MB_DONTWAIT, MT_DATA);
3116 op_err->m_pkthdr.len = 0;
3117 /* pre-reserve space for ip and sctp header and chunk hdr*/
3118 op_err->m_data += sizeof(struct ip6_hdr);
3119 op_err->m_data += sizeof(struct sctphdr);
3120 op_err->m_data += sizeof(struct sctp_chunkhdr);
3124 /* If we have space */
3125 struct sctp_paramhdr s;
3128 pad_needed = 4 - (err_at % 4);
3129 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
3130 err_at += pad_needed;
3132 s.param_type = htons(SCTP_CAUSE_UNRESOLV_ADDR);
3133 s.param_length = htons(sizeof(s) + plen);
3134 m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
3135 err_at += sizeof(s);
3136 phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, plen);
3138 sctp_m_freem(op_err);
3139 /* we are out of memory but we
3140 * still need to have a look at what to
3141 * do (the system is in trouble though).
3145 m_copyback(op_err, err_at, plen, (caddr_t)phdr);
3150 /* we do not recognize the parameter
3151 * figure out what we do.
3154 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
3155 kprintf("Got parameter type %x - unknown\n",
3159 if ((ptype & 0x4000) == 0x4000) {
3160 /* Report bit is set?? */
3162 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
3163 kprintf("Report bit is set\n");
3166 if (op_err == NULL) {
3167 /* Ok need to try to get an mbuf */
3168 MGETHDR(op_err, MB_DONTWAIT, MT_DATA);
3171 op_err->m_pkthdr.len = 0;
3172 op_err->m_data += sizeof(struct ip6_hdr);
3173 op_err->m_data += sizeof(struct sctphdr);
3174 op_err->m_data += sizeof(struct sctp_chunkhdr);
3178 /* If we have space */
3179 struct sctp_paramhdr s;
3182 pad_needed = 4 - (err_at % 4);
3183 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
3184 err_at += pad_needed;
3186 s.param_type = htons(SCTP_UNRECOG_PARAM);
3187 s.param_length = htons(sizeof(s) + plen);
3188 m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
3189 err_at += sizeof(s);
3190 if (plen > sizeof(tempbuf)) {
3191 plen = sizeof(tempbuf);
3193 phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, plen);
3195 sctp_m_freem(op_err);
3196 /* we are out of memory but we
3197 * still need to have a look at what to
3198 * do (the system is in trouble though).
3200 goto more_processing;
3202 m_copyback(op_err, err_at, plen, (caddr_t)phdr);
3207 if ((ptype & 0x8000) == 0x0000) {
3209 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
3210 kprintf("Abort bit is now setting1\n");
3215 /* skip this chunk and continue processing */
3216 at += SCTP_SIZE32(plen);
3220 phdr = sctp_get_next_param(mat, at, ¶ms, sizeof(params));
3226 sctp_are_there_new_addresses(struct sctp_association *asoc,
3227 struct mbuf *in_initpkt, int iphlen, int offset)
3230 * Given a INIT packet, look through the packet to verify that
3231 * there are NO new addresses. As we go through the parameters
3232 * add reports of any un-understood parameters that require an
3233 * error. Also we must return (1) to drop the packet if we see
3234 * a un-understood parameter that tells us to drop the chunk.
3236 struct sockaddr_in sin4, *sa4;
3237 struct sockaddr_in6 sin6, *sa6;
3238 struct sockaddr *sa_touse;
3239 struct sockaddr *sa;
3240 struct sctp_paramhdr *phdr, params;
3243 uint16_t ptype, plen;
3246 struct sctp_nets *net;
3248 memset(&sin4, 0, sizeof(sin4));
3249 memset(&sin6, 0, sizeof(sin6));
3250 sin4.sin_family = AF_INET;
3251 sin4.sin_len = sizeof(sin4);
3252 sin6.sin6_family = AF_INET6;
3253 sin6.sin6_len = sizeof(sin6);
3256 /* First what about the src address of the pkt ? */
3257 iph = mtod(in_initpkt, struct ip *);
3258 if (iph->ip_v == IPVERSION) {
3259 /* source addr is IPv4 */
3260 sin4.sin_addr = iph->ip_src;
3261 sa_touse = (struct sockaddr *)&sin4;
3262 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
3263 /* source addr is IPv6 */
3264 struct ip6_hdr *ip6h;
3265 ip6h = mtod(in_initpkt, struct ip6_hdr *);
3266 sin6.sin6_addr = ip6h->ip6_src;
3267 sa_touse = (struct sockaddr *)&sin6;
3273 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3274 sa = (struct sockaddr *)&net->ro._l_addr;
3275 if (sa->sa_family == sa_touse->sa_family) {
3276 if (sa->sa_family == AF_INET) {
3277 sa4 = (struct sockaddr_in *)sa;
3278 if (sa4->sin_addr.s_addr ==
3279 sin4.sin_addr.s_addr) {
3283 } else if (sa->sa_family == AF_INET6) {
3284 sa6 = (struct sockaddr_in6 *)sa;
3285 if (SCTP6_ARE_ADDR_EQUAL(&sa6->sin6_addr,
3294 /* New address added! no need to look futher. */
3297 /* Ok so far lets munge through the rest of the packet */
3301 offset += sizeof(struct sctp_init_chunk);
3302 phdr = sctp_get_next_param(mat, offset, ¶ms, sizeof(params));
3304 ptype = ntohs(phdr->param_type);
3305 plen = ntohs(phdr->param_length);
3306 if (ptype == SCTP_IPV4_ADDRESS) {
3307 struct sctp_ipv4addr_param *p4, p4_buf;
3309 phdr = sctp_get_next_param(mat, offset,
3310 (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
3311 if (plen != sizeof(struct sctp_ipv4addr_param) ||
3315 p4 = (struct sctp_ipv4addr_param *)phdr;
3316 sin4.sin_addr.s_addr = p4->addr;
3317 sa_touse = (struct sockaddr *)&sin4;
3318 } else if (ptype == SCTP_IPV6_ADDRESS) {
3319 struct sctp_ipv6addr_param *p6, p6_buf;
3321 phdr = sctp_get_next_param(mat, offset,
3322 (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
3323 if (plen != sizeof(struct sctp_ipv6addr_param) ||
3327 p6 = (struct sctp_ipv6addr_param *)phdr;
3328 memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
3330 sa_touse = (struct sockaddr *)&sin4;
3334 /* ok, sa_touse points to one to check */
3336 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3337 sa = (struct sockaddr *)&net->ro._l_addr;
3338 if (sa->sa_family != sa_touse->sa_family) {
3341 if (sa->sa_family == AF_INET) {
3342 sa4 = (struct sockaddr_in *)sa;
3343 if (sa4->sin_addr.s_addr ==
3344 sin4.sin_addr.s_addr) {
3348 } else if (sa->sa_family == AF_INET6) {
3349 sa6 = (struct sockaddr_in6 *)sa;
3350 if (SCTP6_ARE_ADDR_EQUAL(
3351 &sa6->sin6_addr, &sin6.sin6_addr)) {
3358 /* New addr added! no need to look further */
3362 offset += SCTP_SIZE32(plen);
3363 phdr = sctp_get_next_param(mat, offset, ¶ms, sizeof(params));
3369 * Given a MBUF chain that was sent into us containing an
3370 * INIT. Build a INIT-ACK with COOKIE and send back.
3371 * We assume that the in_initpkt has done a pullup to
3372 * include IPv6/4header, SCTP header and initial part of
3373 * INIT message (i.e. the struct sctp_init_msg).
3376 sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
3377 struct mbuf *init_pkt, int iphlen, int offset, struct sctphdr *sh,
3378 struct sctp_init_chunk *init_chk)
3380 struct sctp_association *asoc;
3381 struct mbuf *m, *m_at, *m_tmp, *m_cookie, *op_err, *m_last;
3382 struct sctp_init_msg *initackm_out;
3383 struct sctp_ecn_supported_param *ecn;
3384 struct sctp_prsctp_supported_param *prsctp;
3385 struct sctp_ecn_nonce_supported_param *ecn_nonce;
3386 struct sctp_supported_chunk_types_param *pr_supported;
3387 struct sockaddr_storage store;
3388 struct sockaddr_in *sin;
3389 struct sockaddr_in6 *sin6;
3392 struct ip6_hdr *ip6;
3393 struct sockaddr *to;
3394 struct sctp_state_cookie stc;
3395 struct sctp_nets *net=NULL;
3397 uint16_t his_limit, i_want;
3398 int abort_flag, padval, sz_of;
3406 if ((asoc != NULL) &&
3407 (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
3408 (sctp_are_there_new_addresses(asoc, init_pkt, iphlen, offset))) {
3409 /* new addresses, out of here in non-cookie-wait states */
3411 * Send a ABORT, we don't add the new address error clause though
3412 * we even set the T bit and copy in the 0 tag.. this looks no
3413 * different than if no listner was present.
3415 sctp_send_abort(init_pkt, iphlen, sh, 0, NULL);
3419 op_err = sctp_arethere_unrecognized_parameters(init_pkt,
3420 (offset+sizeof(struct sctp_init_chunk)),
3421 &abort_flag, (struct sctp_chunkhdr *)init_chk);
3423 sctp_send_abort(init_pkt, iphlen, sh, init_chk->init.initiate_tag, op_err);
3426 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
3428 /* No memory, INIT timer will re-attempt. */
3430 sctp_m_freem(op_err);
3433 MCLGET(m, MB_DONTWAIT);
3434 if ((m->m_flags & M_EXT) != M_EXT) {
3435 /* Failed to get cluster buffer */
3437 sctp_m_freem(op_err);
3441 m->m_data += SCTP_MIN_OVERHEAD;
3442 m->m_pkthdr.rcvif = 0;
3443 m->m_len = sizeof(struct sctp_init_msg);
3445 /* the time I built cookie */
3446 SCTP_GETTIME_TIMEVAL(&stc.time_entered);
3448 /* populate any tie tags */
3450 /* unlock before tag selections */
3451 SCTP_TCB_UNLOCK(stcb);
3452 if (asoc->my_vtag_nonce == 0)
3453 asoc->my_vtag_nonce = sctp_select_a_tag(inp);
3454 stc.tie_tag_my_vtag = asoc->my_vtag_nonce;
3456 if (asoc->peer_vtag_nonce == 0)
3457 asoc->peer_vtag_nonce = sctp_select_a_tag(inp);
3458 stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce;
3460 stc.cookie_life = asoc->cookie_life;
3461 net = asoc->primary_destination;
3462 /* now we must relock */
3463 SCTP_INP_RLOCK(inp);
3464 /* we may be in trouble here if the inp got freed
3465 * most likely this set of tests will protect
3466 * us but there is a chance not.
3468 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
3470 sctp_m_freem(op_err);
3472 sctp_send_abort(init_pkt, iphlen, sh, 0, NULL);
3475 SCTP_TCB_LOCK(stcb);
3476 SCTP_INP_RUNLOCK(stcb->sctp_ep);
3478 stc.tie_tag_my_vtag = 0;
3479 stc.tie_tag_peer_vtag = 0;
3480 /* life I will award this cookie */
3481 stc.cookie_life = inp->sctp_ep.def_cookie_life;
3484 /* copy in the ports for later check */
3485 stc.myport = sh->dest_port;
3486 stc.peerport = sh->src_port;
3489 * If we wanted to honor cookie life extentions, we would add
3490 * to stc.cookie_life. For now we should NOT honor any extension
3492 stc.site_scope = stc.local_scope = stc.loopback_scope = 0;
3493 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3494 struct inpcb *in_inp;
3495 /* Its a V6 socket */
3496 in_inp = (struct inpcb *)inp;
3497 stc.ipv6_addr_legal = 1;
3498 /* Now look at the binding flag to see if V4 will be legal */
3500 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
3501 (in_inp->inp_flags & IN6P_IPV6_V6ONLY)
3502 #elif defined(__OpenBSD__)
3503 (0) /* For openbsd we do dual bind only */
3505 (((struct in6pcb *)in_inp)->in6p_flags & IN6P_IPV6_V6ONLY)
3508 stc.ipv4_addr_legal = 1;
3510 /* V4 addresses are NOT legal on the association */
3511 stc.ipv4_addr_legal = 0;
3514 /* Its a V4 socket, no - V6 */
3515 stc.ipv4_addr_legal = 1;
3516 stc.ipv6_addr_legal = 0;
3519 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
3524 /* now for scope setup */
3525 memset((caddr_t)&store, 0, sizeof(store));
3526 sin = (struct sockaddr_in *)&store;
3527 sin6 = (struct sockaddr_in6 *)&store;
3529 to = (struct sockaddr *)&store;
3530 iph = mtod(init_pkt, struct ip *);
3531 if (iph->ip_v == IPVERSION) {
3532 struct in_addr addr;
3533 struct route iproute;
3535 sin->sin_family = AF_INET;
3536 sin->sin_len = sizeof(struct sockaddr_in);
3537 sin->sin_port = sh->src_port;
3538 sin->sin_addr = iph->ip_src;
3539 /* lookup address */
3540 stc.address[0] = sin->sin_addr.s_addr;
3544 stc.addr_type = SCTP_IPV4_ADDRESS;
3545 /* local from address */
3546 memset(&iproute, 0, sizeof(iproute));
3548 memcpy(&ro->ro_dst, sin, sizeof(*sin));
3549 addr = sctp_ipv4_source_address_selection(inp, NULL,
3554 stc.laddress[0] = addr.s_addr;
3555 stc.laddress[1] = 0;
3556 stc.laddress[2] = 0;
3557 stc.laddress[3] = 0;
3558 stc.laddr_type = SCTP_IPV4_ADDRESS;
3559 /* scope_id is only for v6 */
3561 #ifndef SCTP_DONT_DO_PRIVADDR_SCOPE
3562 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
3567 #endif /* SCTP_DONT_DO_PRIVADDR_SCOPE */
3568 /* Must use the address in this case */
3569 if (sctp_is_address_on_local_host((struct sockaddr *)sin)) {
3570 stc.loopback_scope = 1;
3573 stc.local_scope = 1;
3575 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
3576 struct in6_addr addr;
3577 #ifdef NEW_STRUCT_ROUTE
3578 struct route iproute6;
3580 struct route_in6 iproute6;
3582 ip6 = mtod(init_pkt, struct ip6_hdr *);
3583 sin6->sin6_family = AF_INET6;
3584 sin6->sin6_len = sizeof(struct sockaddr_in6);
3585 sin6->sin6_port = sh->src_port;
3586 sin6->sin6_addr = ip6->ip6_src;
3587 /* lookup address */
3588 memcpy(&stc.address, &sin6->sin6_addr,
3589 sizeof(struct in6_addr));
3590 sin6->sin6_scope_id = 0;
3591 stc.addr_type = SCTP_IPV6_ADDRESS;
3593 if (sctp_is_address_on_local_host((struct sockaddr *)sin6)) {
3594 stc.loopback_scope = 1;
3595 stc.local_scope = 1;
3598 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
3600 * If the new destination is a LINK_LOCAL
3601 * we must have common both site and local
3602 * scope. Don't set local scope though since
3603 * we must depend on the source to be added
3604 * implicitly. We cannot assure just because
3605 * we share one link that all links are common.
3607 stc.local_scope = 0;
3610 /* we start counting for the private
3611 * address stuff at 1. since the link
3612 * local we source from won't show
3613 * up in our scoped cou8nt.
3616 /* pull out the scope_id from incoming pkt */
3617 in6_recoverscope(sin6, &ip6->ip6_src,
3618 init_pkt->m_pkthdr.rcvif);
3619 #if defined(SCTP_BASE_FREEBSD) || defined(__APPLE__) || defined(__DragonFly__)
3620 in6_embedscope(&sin6->sin6_addr, sin6, NULL,
3623 in6_embedscope(&sin6->sin6_addr, sin6);
3625 stc.scope_id = sin6->sin6_scope_id;
3626 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
3628 * If the new destination is SITE_LOCAL
3629 * then we must have site scope in common.
3633 /* local from address */
3634 memset(&iproute6, 0, sizeof(iproute6));
3635 ro = (struct route *)&iproute6;
3636 memcpy(&ro->ro_dst, sin6, sizeof(*sin6));
3637 addr = sctp_ipv6_source_address_selection(inp, NULL,
3642 memcpy(&stc.laddress, &addr, sizeof(struct in6_addr));
3643 stc.laddr_type = SCTP_IPV6_ADDRESS;
3646 /* set the scope per the existing tcb */
3647 struct sctp_nets *lnet;
3649 stc.loopback_scope = asoc->loopback_scope;
3650 stc.ipv4_scope = asoc->ipv4_local_scope;
3651 stc.site_scope = asoc->site_scope;
3652 stc.local_scope = asoc->local_scope;
3653 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
3654 if (lnet->ro._l_addr.sin6.sin6_family == AF_INET6) {
3655 if (IN6_IS_ADDR_LINKLOCAL(&lnet->ro._l_addr.sin6.sin6_addr)) {
3656 /* if we have a LL address, start counting
3664 /* use the net pointer */
3665 to = (struct sockaddr *)&net->ro._l_addr;
3666 if (to->sa_family == AF_INET) {
3667 sin = (struct sockaddr_in *)to;
3668 stc.address[0] = sin->sin_addr.s_addr;
3672 stc.addr_type = SCTP_IPV4_ADDRESS;
3673 if (net->src_addr_selected == 0) {
3674 /* strange case here, the INIT
3675 * should have did the selection.
3677 net->ro._s_addr.sin.sin_addr =
3678 sctp_ipv4_source_address_selection(inp,
3679 stcb, (struct route *)&net->ro, net, 0);
3680 net->src_addr_selected = 1;
3684 stc.laddress[0] = net->ro._s_addr.sin.sin_addr.s_addr;
3685 stc.laddress[1] = 0;
3686 stc.laddress[2] = 0;
3687 stc.laddress[3] = 0;
3688 stc.laddr_type = SCTP_IPV4_ADDRESS;
3689 } else if (to->sa_family == AF_INET6) {
3690 sin6 = (struct sockaddr_in6 *)to;
3691 memcpy(&stc.address, &sin6->sin6_addr,
3692 sizeof(struct in6_addr));
3693 stc.addr_type = SCTP_IPV6_ADDRESS;
3694 if (net->src_addr_selected == 0) {
3695 /* strange case here, the INIT
3696 * should have did the selection.
3698 net->ro._s_addr.sin6.sin6_addr =
3699 sctp_ipv6_source_address_selection(inp,
3700 stcb, (struct route *)&net->ro, net, 0);
3701 net->src_addr_selected = 1;
3703 memcpy(&stc.laddress, &net->ro._l_addr.sin6.sin6_addr,
3704 sizeof(struct in6_addr));
3705 stc.laddr_type = SCTP_IPV6_ADDRESS;
3708 /* Now lets put the SCTP header in place */
3709 initackm_out = mtod(m, struct sctp_init_msg *);
3710 initackm_out->sh.src_port = inp->sctp_lport;
3711 initackm_out->sh.dest_port = sh->src_port;
3712 initackm_out->sh.v_tag = init_chk->init.initiate_tag;
3713 /* Save it off for quick ref */
3714 stc.peers_vtag = init_chk->init.initiate_tag;
3715 initackm_out->sh.checksum = 0; /* calculate later */
3717 strncpy(stc.identification, SCTP_VERSION_STRING,
3718 min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification)));
3719 /* now the chunk header */
3720 initackm_out->msg.ch.chunk_type = SCTP_INITIATION_ACK;
3721 initackm_out->msg.ch.chunk_flags = 0;
3722 /* fill in later from mbuf we build */
3723 initackm_out->msg.ch.chunk_length = 0;
3724 /* place in my tag */
3725 if ((asoc != NULL) &&
3726 ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
3727 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED))) {
3728 /* re-use the v-tags and init-seq here */
3729 initackm_out->msg.init.initiate_tag = htonl(asoc->my_vtag);
3730 initackm_out->msg.init.initial_tsn = htonl(asoc->init_seq_number);
3732 initackm_out->msg.init.initiate_tag = htonl(sctp_select_a_tag(inp));
3733 /* get a TSN to use too */
3734 initackm_out->msg.init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
3736 /* save away my tag to */
3737 stc.my_vtag = initackm_out->msg.init.initiate_tag;
3739 /* set up some of the credits. */
3740 initackm_out->msg.init.a_rwnd = htonl(max(inp->sctp_socket->so_rcv.ssb_hiwat, SCTP_MINIMAL_RWND));
3741 /* set what I want */
3742 his_limit = ntohs(init_chk->init.num_inbound_streams);
3743 /* choose what I want */
3745 if (asoc->streamoutcnt > inp->sctp_ep.pre_open_stream_count) {
3746 i_want = asoc->streamoutcnt;
3748 i_want = inp->sctp_ep.pre_open_stream_count;
3751 i_want = inp->sctp_ep.pre_open_stream_count;
3753 if (his_limit < i_want) {
3754 /* I Want more :< */
3755 initackm_out->msg.init.num_outbound_streams = init_chk->init.num_inbound_streams;
3757 /* I can have what I want :> */
3758 initackm_out->msg.init.num_outbound_streams = htons(i_want);
3760 /* tell him his limt. */
3761 initackm_out->msg.init.num_inbound_streams =
3762 htons(inp->sctp_ep.max_open_streams_intome);
3763 /* setup the ECN pointer */
3765 /* if (inp->sctp_flags & SCTP_PCB_FLAGS_ADAPTIONEVNT) {*/
3766 if (inp->sctp_ep.adaption_layer_indicator) {
3767 struct sctp_adaption_layer_indication *ali;
3768 ali = (struct sctp_adaption_layer_indication *)(
3769 (caddr_t)initackm_out + sizeof(*initackm_out));
3770 ali->ph.param_type = htons(SCTP_ULP_ADAPTION);
3771 ali->ph.param_length = htons(sizeof(*ali));
3772 ali->indication = ntohl(inp->sctp_ep.adaption_layer_indicator);
3773 m->m_len += sizeof(*ali);
3774 ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali +
3777 ecn = (struct sctp_ecn_supported_param*)(
3778 (caddr_t)initackm_out + sizeof(*initackm_out));
3782 if (sctp_ecn == 1) {
3783 ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
3784 ecn->ph.param_length = htons(sizeof(*ecn));
3785 m->m_len += sizeof(*ecn);
3787 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
3790 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
3792 /* And now tell the peer we do pr-sctp */
3793 prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
3794 prsctp->ph.param_length = htons(sizeof(*prsctp));
3795 m->m_len += sizeof(*prsctp);
3798 /* And now tell the peer we do all the extensions */
3799 pr_supported = (struct sctp_supported_chunk_types_param *)((caddr_t)prsctp +
3802 pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
3803 pr_supported->ph.param_length = htons(sizeof(*pr_supported) + SCTP_EXT_COUNT);
3804 pr_supported->chunk_types[0] = SCTP_ASCONF;
3805 pr_supported->chunk_types[1] = SCTP_ASCONF_ACK;
3806 pr_supported->chunk_types[2] = SCTP_FORWARD_CUM_TSN;
3807 pr_supported->chunk_types[3] = SCTP_PACKET_DROPPED;
3808 pr_supported->chunk_types[4] = SCTP_STREAM_RESET;
3809 pr_supported->chunk_types[5] = 0; /* pad */
3810 pr_supported->chunk_types[6] = 0; /* pad */
3811 pr_supported->chunk_types[7] = 0; /* pad */
3813 m->m_len += (sizeof(*pr_supported) + SCTP_EXT_COUNT + SCTP_PAD_EXT_COUNT);
3814 if (sctp_ecn_nonce) {
3815 /* ECN nonce: And now tell the peer we support ECN nonce */
3816 ecn_nonce = (struct sctp_ecn_nonce_supported_param *)((caddr_t)pr_supported +
3817 sizeof(*pr_supported) + SCTP_EXT_COUNT + SCTP_PAD_EXT_COUNT);
3818 ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
3819 ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
3820 m->m_len += sizeof(*ecn_nonce);
3824 /* now the addresses */
3825 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3827 int cnt = cnt_inits_to;
3829 TAILQ_FOREACH(ifn, &ifnet, if_list) {
3830 struct ifaddr_container *ifac;
3832 if ((stc.loopback_scope == 0) &&
3833 (ifn->if_type == IFT_LOOP)) {
3835 * Skip loopback devices if loopback_scope
3840 TAILQ_FOREACH(ifac, &ifn->if_addrheads[mycpuid], ifa_link) {
3841 struct ifaddr *ifa = ifac->ifa;
3843 if (sctp_is_address_in_scope(ifa,
3844 stc.ipv4_addr_legal, stc.ipv6_addr_legal,
3845 stc.loopback_scope, stc.ipv4_scope,
3846 stc.local_scope, stc.site_scope) == 0) {
3853 TAILQ_FOREACH(ifn, &ifnet, if_list) {
3854 struct ifaddr_container *ifac;
3856 if ((stc.loopback_scope == 0) &&
3857 (ifn->if_type == IFT_LOOP)) {
3859 * Skip loopback devices if
3860 * loopback_scope not set
3864 TAILQ_FOREACH(ifac, &ifn->if_addrheads[mycpuid], ifa_link) {
3865 struct ifaddr *ifa = ifac->ifa;
3867 if (sctp_is_address_in_scope(ifa,
3868 stc.ipv4_addr_legal,
3869 stc.ipv6_addr_legal,
3870 stc.loopback_scope, stc.ipv4_scope,
3871 stc.local_scope, stc.site_scope) == 0) {
3874 m_at = sctp_add_addr_to_mbuf(m_at, ifa);
3879 struct sctp_laddr *laddr;
3882 /* First, how many ? */
3883 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3884 if (laddr->ifa == NULL) {
3887 if (laddr->ifa->ifa_addr == NULL)
3889 if (sctp_is_address_in_scope(laddr->ifa,
3890 stc.ipv4_addr_legal, stc.ipv6_addr_legal,
3891 stc.loopback_scope, stc.ipv4_scope,
3892 stc.local_scope, stc.site_scope) == 0) {
3897 /* If we bind a single address only we won't list
3898 * any. This way you can get through a NAT
3901 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3902 if (laddr->ifa == NULL) {
3904 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
3905 kprintf("Help I have fallen and I can't get up!\n");
3910 if (laddr->ifa->ifa_addr == NULL)
3912 if (sctp_is_address_in_scope(laddr->ifa,
3913 stc.ipv4_addr_legal, stc.ipv6_addr_legal,
3914 stc.loopback_scope, stc.ipv4_scope,
3915 stc.local_scope, stc.site_scope) == 0) {
3918 m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa);
3923 /* tack on the operational error if present */
3925 if (op_err->m_pkthdr.len % 4) {
3926 /* must add a pad to the param */
3929 padlen = 4 - (op_err->m_pkthdr.len % 4);
3930 m_copyback(op_err, op_err->m_pkthdr.len, padlen, (caddr_t)&cpthis);
3932 while (m_at->m_next != NULL) {
3933 m_at = m_at->m_next;
3935 m_at->m_next = op_err;
3936 while (m_at->m_next != NULL) {
3937 m_at = m_at->m_next;
3940 /* Get total size of init packet */
3941 sz_of = SCTP_SIZE32(ntohs(init_chk->ch.chunk_length));
3942 /* pre-calulate the size and update pkt header and chunk header */
3943 m->m_pkthdr.len = 0;
3944 for (m_tmp = m; m_tmp; m_tmp = m_tmp->m_next) {
3945 m->m_pkthdr.len += m_tmp->m_len;
3946 if (m_tmp->m_next == NULL) {
3947 /* m_tmp should now point to last one */
3952 * Figure now the size of the cookie. We know the size of the
3953 * INIT-ACK. The Cookie is going to be the size of INIT, INIT-ACK,
3954 * COOKIE-STRUCTURE and SIGNATURE.
3958 * take our earlier INIT calc and add in the sz we just calculated
3959 * minus the size of the sctphdr (its not included in chunk size
3962 /* add once for the INIT-ACK */
3963 sz_of += (m->m_pkthdr.len - sizeof(struct sctphdr));
3965 /* add a second time for the INIT-ACK in the cookie */
3966 sz_of += (m->m_pkthdr.len - sizeof(struct sctphdr));
3968 /* Now add the cookie header and cookie message struct */
3969 sz_of += sizeof(struct sctp_state_cookie_param);
3970 /* ...and add the size of our signature */
3971 sz_of += SCTP_SIGNATURE_SIZE;
3972 initackm_out->msg.ch.chunk_length = htons(sz_of);
3974 /* Now we must build a cookie */
3975 m_cookie = sctp_add_cookie(inp, init_pkt, offset, m,
3976 sizeof(struct sctphdr), &stc);
3977 if (m_cookie == NULL) {
3978 /* memory problem */
3982 /* Now append the cookie to the end and update the space/size */
3983 m_tmp->m_next = m_cookie;
3986 * We pass 0 here to NOT set IP_DF if its IPv4, we ignore the
3987 * return here since the timer will drive a retranmission.
3989 padval = m->m_pkthdr.len % 4;
3990 if ((padval) && (m_last)) {
3991 /* see my previous comments on m_last */
3993 ret = sctp_add_pad_tombuf(m_last, (4-padval));
3995 /* Houston we have a problem, no space */
3999 m->m_pkthdr.len += padval;
4001 sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, 0, NULL, 0);
4006 sctp_insert_on_wheel(struct sctp_association *asoc,
4007 struct sctp_stream_out *strq)
4009 struct sctp_stream_out *stre, *strn;
4010 stre = TAILQ_FIRST(&asoc->out_wheel);
4012 /* only one on wheel */
4013 TAILQ_INSERT_HEAD(&asoc->out_wheel, strq, next_spoke);
4016 for (; stre; stre = strn) {
4017 strn = TAILQ_NEXT(stre, next_spoke);
4018 if (stre->stream_no > strq->stream_no) {
4019 TAILQ_INSERT_BEFORE(stre, strq, next_spoke);
4021 } else if (stre->stream_no == strq->stream_no) {
4022 /* huh, should not happen */
4024 } else if (strn == NULL) {
4025 /* next one is null */
4026 TAILQ_INSERT_AFTER(&asoc->out_wheel, stre, strq,
4033 sctp_remove_from_wheel(struct sctp_association *asoc,
4034 struct sctp_stream_out *strq)
4036 /* take off and then setup so we know it is not on the wheel */
4037 TAILQ_REMOVE(&asoc->out_wheel, strq, next_spoke);
4038 strq->next_spoke.tqe_next = NULL;
4039 strq->next_spoke.tqe_prev = NULL;
4044 sctp_prune_prsctp(struct sctp_tcb *stcb,
4045 struct sctp_association *asoc,
4046 struct sctp_sndrcvinfo *srcv,
4051 struct sctp_tmit_chunk *chk, *nchk;
4052 if ((asoc->peer_supports_prsctp) && (asoc->sent_queue_cnt_removeable > 0)) {
4053 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
4055 * Look for chunks marked with the PR_SCTP
4056 * flag AND the buffer space flag. If the one
4057 * being sent is equal or greater priority then
4058 * purge the old one and free some space.
4060 if ((chk->flags & (SCTP_PR_SCTP_ENABLED |
4061 SCTP_PR_SCTP_BUFFER)) ==
4062 (SCTP_PR_SCTP_ENABLED|SCTP_PR_SCTP_BUFFER)) {
4064 * This one is PR-SCTP AND buffer space
4067 if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
4068 /* Lower numbers equates to
4069 * higher priority so if the
4070 * one we are looking at has a
4071 * larger or equal priority we
4072 * want to drop the data and
4073 * NOT retransmit it.
4082 if (chk->sent > SCTP_DATAGRAM_UNSENT)
4083 cause = SCTP_RESPONSE_TO_USER_REQ|SCTP_NOTIFY_DATAGRAM_SENT;
4085 cause = SCTP_RESPONSE_TO_USER_REQ|SCTP_NOTIFY_DATAGRAM_UNSENT;
4086 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
4089 freed_spc += ret_spc;
4090 if (freed_spc >= dataout) {
4093 } /* if chunk was present */
4094 } /* if of sufficent priority */
4095 } /* if chunk has enabled */
4096 } /* tailqforeach */
4098 chk = TAILQ_FIRST(&asoc->send_queue);
4100 nchk = TAILQ_NEXT(chk, sctp_next);
4101 /* Here we must move to the sent queue and mark */
4102 if ((chk->flags & (SCTP_PR_SCTP_ENABLED |
4103 SCTP_PR_SCTP_BUFFER)) ==
4104 (SCTP_PR_SCTP_ENABLED|SCTP_PR_SCTP_BUFFER)) {
4105 if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
4112 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
4113 SCTP_RESPONSE_TO_USER_REQ|SCTP_NOTIFY_DATAGRAM_UNSENT,
4116 freed_spc += ret_spc;
4117 if (freed_spc >= dataout) {
4120 } /* end if chk->data */
4121 } /* end if right class */
4122 } /* end if chk pr-sctp */
4124 } /* end while (chk) */
4125 } /* if enabled in asoc */
4129 sctp_prepare_chunk(struct sctp_tmit_chunk *template,
4130 struct sctp_tcb *stcb,
4131 struct sctp_sndrcvinfo *srcv,
4132 struct sctp_stream_out *strq,
4133 struct sctp_nets *net)
4135 bzero(template, sizeof(struct sctp_tmit_chunk));
4136 template->sent = SCTP_DATAGRAM_UNSENT;
4137 if ((stcb->asoc.peer_supports_prsctp) &&
4138 (srcv->sinfo_flags & (MSG_PR_SCTP_TTL|MSG_PR_SCTP_BUF)) &&
4139 (srcv->sinfo_timetolive > 0)
4142 * Peer supports PR-SCTP
4143 * The flags is set against this send for PR-SCTP
4144 * And timetolive is a postive value, zero is reserved
4145 * to mean a reliable send for both buffer/time
4148 if (srcv->sinfo_flags & MSG_PR_SCTP_BUF) {
4150 * Time to live is a priority stored in tv_sec
4151 * when doing the buffer drop thing.
4153 template->rec.data.timetodrop.tv_sec = srcv->sinfo_timetolive;
4157 SCTP_GETTIME_TIMEVAL(&template->rec.data.timetodrop);
4158 tv.tv_sec = srcv->sinfo_timetolive / 1000;
4159 tv.tv_usec = (srcv->sinfo_timetolive * 1000) % 1000000;
4161 timeradd(&template->rec.data.timetodrop, &tv,
4162 &template->rec.data.timetodrop);
4164 timevaladd(&template->rec.data.timetodrop, &tv);
4168 if ((srcv->sinfo_flags & MSG_UNORDERED) == 0) {
4169 template->rec.data.stream_seq = strq->next_sequence_sent;
4171 template->rec.data.stream_seq = 0;
4173 template->rec.data.TSN_seq = 0; /* not yet assigned */
4175 template->rec.data.stream_number = srcv->sinfo_stream;
4176 template->rec.data.payloadtype = srcv->sinfo_ppid;
4177 template->rec.data.context = srcv->sinfo_context;
4178 template->rec.data.doing_fast_retransmit = 0;
4179 template->rec.data.ect_nonce = 0; /* ECN Nonce */
4181 if (srcv->sinfo_flags & MSG_ADDR_OVER) {
4182 template->whoTo = net;
4184 if (stcb->asoc.primary_destination)
4185 template->whoTo = stcb->asoc.primary_destination;
4188 template->whoTo = net;
4191 /* the actual chunk flags */
4192 if (srcv->sinfo_flags & MSG_UNORDERED) {
4193 template->rec.data.rcv_flags = SCTP_DATA_UNORDERED;
4195 template->rec.data.rcv_flags = 0;
4197 /* no flags yet, FRAGMENT_OK goes here */
4198 template->flags = 0;
4200 if (stcb->asoc.peer_supports_prsctp) {
4201 if (srcv->sinfo_timetolive > 0) {
4203 * We only set the flag if timetolive (or
4204 * priority) was set to a positive number.
4205 * Zero is reserved specifically to be
4206 * EXCLUDED and sent reliable.
4208 if (srcv->sinfo_flags & MSG_PR_SCTP_TTL) {
4209 template->flags |= SCTP_PR_SCTP_ENABLED;
4211 if (srcv->sinfo_flags & MSG_PR_SCTP_BUF) {
4212 template->flags |= SCTP_PR_SCTP_BUFFER;
4216 template->asoc = &stcb->asoc;
4221 sctp_get_frag_point(struct sctp_tcb *stcb,
4222 struct sctp_association *asoc)
4226 /* For endpoints that have both 6 and 4 addresses
4227 * we must reserver room for the 6 ip header, for
4228 * those that are only dealing with V4 we use
4229 * a larger frag point.
4231 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
4232 ovh = SCTP_MED_OVERHEAD;
4234 ovh = SCTP_MED_V4_OVERHEAD;
4237 if (stcb->sctp_ep->sctp_frag_point > asoc->smallest_mtu)
4238 siz = asoc->smallest_mtu - ovh;
4240 siz = (stcb->sctp_ep->sctp_frag_point - ovh);
4242 if (siz > (MCLBYTES-sizeof(struct sctp_data_chunk))) { */
4243 /* A data chunk MUST fit in a cluster */
4244 /* siz = (MCLBYTES - sizeof(struct sctp_data_chunk));*/
4248 /* make it an even word boundary please */
4253 extern unsigned int sctp_max_chunks_on_queue;
4255 #define SBLOCKWAIT(f) (((f)&MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
4258 sctp_msg_append(struct sctp_tcb *stcb,
4259 struct sctp_nets *net,
4261 struct sctp_sndrcvinfo *srcv,
4265 struct sctp_association *asoc;
4266 struct sctp_stream_out *strq;
4267 struct sctp_tmit_chunk *chk;
4268 struct sctpchunk_listhead tmp;
4269 struct sctp_tmit_chunk template;
4270 struct mbuf *n, *mnext;
4272 unsigned int dataout, siz;
4277 if ((stcb == NULL) || (net == NULL) || (m == NULL) || (srcv == NULL)) {
4278 /* Software fault, you blew it on the call */
4280 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
4281 kprintf("software error in sctp_msg_append:1\n");
4282 kprintf("stcb:%p net:%p m:%p srcv:%p\n",
4283 stcb, net, m, srcv);
4290 so = stcb->sctp_socket;
4292 if (srcv->sinfo_flags & MSG_ABORT) {
4293 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
4294 (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_ECHOED)) {
4295 /* It has to be up before we abort */
4296 /* how big is the user initiated abort? */
4297 if ((m->m_flags & M_PKTHDR) && (m->m_pkthdr.len)) {
4298 dataout = m->m_pkthdr.len;
4302 for (n = m; n; n = n->m_next) {
4303 dataout += n->m_len;
4306 M_PREPEND(m, sizeof(struct sctp_paramhdr), MB_DONTWAIT);
4308 struct sctp_paramhdr *ph;
4309 m->m_len = sizeof(struct sctp_paramhdr) + dataout;
4310 ph = mtod(m, struct sctp_paramhdr *);
4311 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
4312 ph->param_length = htons(m->m_len);
4314 sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, m);
4317 /* Only free if we don't send an abort */
4322 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
4323 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
4324 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
4325 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
4326 /* got data while shutting down */
4331 if (srcv->sinfo_stream >= asoc->streamoutcnt) {
4332 /* Invalid stream number */
4336 if (asoc->strmout == NULL) {
4337 /* huh? software error */
4339 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
4340 kprintf("software error in sctp_msg_append:2\n");
4346 strq = &asoc->strmout[srcv->sinfo_stream];
4347 /* how big is it ? */
4348 if ((m->m_flags & M_PKTHDR) && (m->m_pkthdr.len)) {
4349 dataout = m->m_pkthdr.len;
4353 for (n = m; n; n = n->m_next) {
4354 dataout += n->m_len;
4358 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
4359 kprintf("Attempt to send out %d bytes\n",
4364 /* lock the socket buf */
4365 SOCKBUF_LOCK(&so->so_snd);
4366 error = ssb_lock(&so->so_snd, SBLOCKWAIT(flags));
4370 if (dataout > so->so_snd.ssb_hiwat) {
4371 /* It will NEVER fit */
4375 if ((srcv->sinfo_flags & MSG_EOF) &&
4376 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
4381 if ((so->so_snd.ssb_hiwat <
4382 (dataout + asoc->total_output_queue_size)) ||
4383 (asoc->chunks_on_out_queue > sctp_max_chunks_on_queue) ||
4384 (asoc->total_output_mbuf_queue_size >
4385 so->so_snd.ssb_mbmax)
4387 /* XXX Buffer space hunt for data to skip */
4388 if (asoc->peer_supports_prsctp) {
4389 sctp_prune_prsctp(stcb, asoc, srcv, dataout);
4391 while ((so->so_snd.ssb_hiwat <
4392 (dataout + asoc->total_output_queue_size)) ||
4393 (asoc->chunks_on_out_queue > sctp_max_chunks_on_queue) ||
4394 (asoc->total_output_mbuf_queue_size >
4395 so->so_snd.ssb_mbmax)) {
4396 struct sctp_inpcb *inp;
4397 /* Now did we free up enough room? */
4398 if (flags & (MSG_FNONBLOCKING|MSG_DONTWAIT)) {
4399 /* Non-blocking io in place */
4400 error = EWOULDBLOCK;
4404 * We store off a pointer to the endpoint.
4405 * Since on return from this we must check to
4406 * see if an so_error is set. If so we may have
4407 * been reset and our stcb destroyed. Returning
4408 * an error will cause the correct error return
4409 * through and fix this all.
4411 inp = stcb->sctp_ep;
4413 * Not sure how else to do this since
4414 * the level we suspended at is not
4415 * known deep down where we are. I will
4416 * drop to spl0() so that others can
4420 inp->sctp_tcb_at_block = (void *)stcb;
4421 inp->error_on_block = 0;
4422 ssb_unlock(&so->so_snd);
4423 error = ssb_wait(&so->so_snd);
4425 * XXX: This is ugly but I have
4426 * recreated most of what goes on to
4427 * block in the sb. UGHH
4428 * May want to add the bit about being
4429 * no longer connected.. but this then
4430 * further dooms the UDP model NOT to
4433 inp->sctp_tcb_at_block = 0;
4434 if (inp->error_on_block)
4435 error = inp->error_on_block;
4437 error = so->so_error;
4441 error = ssb_lock(&so->so_snd, M_WAITOK);
4444 /* Otherwise we cycle back and recheck
4447 #if defined(__FreeBSD__) && __FreeBSD_version >= 502115
4448 if (so->so_rcv.sb_state & SBS_CANTSENDMORE) {
4450 if (so->so_state & SS_CANTSENDMORE) {
4456 error = so->so_error;
4461 /* If we have a packet header fix it if it was broke */
4462 if (m->m_flags & M_PKTHDR) {
4463 m->m_pkthdr.len = dataout;
4465 /* use the smallest one, user set value or
4466 * smallest mtu of the asoc
4468 siz = sctp_get_frag_point(stcb, asoc);
4469 SOCKBUF_UNLOCK(&so->so_snd);
4470 if ((dataout) && (dataout <= siz)) {
4472 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
4475 SOCKBUF_LOCK(&so->so_snd);
4478 sctp_prepare_chunk(chk, stcb, srcv, strq, net);
4479 chk->whoTo->ref_count++;
4480 chk->rec.data.rcv_flags |= SCTP_DATA_NOT_FRAG;
4482 /* no flags yet, FRAGMENT_OK goes here */
4483 sctppcbinfo.ipi_count_chunk++;
4484 sctppcbinfo.ipi_gencnt_chunk++;
4485 asoc->chunks_on_out_queue++;
4488 /* Total in the MSIZE */
4489 for (mm = chk->data; mm; mm = mm->m_next) {
4491 if (mm->m_flags & M_EXT) {
4492 mbcnt += chk->data->m_ext.ext_size;
4495 /* fix up the send_size if it is not present */
4496 chk->send_size = dataout;
4497 chk->book_size = chk->send_size;
4499 /* ok, we are commited */
4500 if ((srcv->sinfo_flags & MSG_UNORDERED) == 0) {
4501 /* bump the ssn if we are unordered. */
4502 strq->next_sequence_sent++;
4504 chk->data->m_nextpkt = 0;
4505 asoc->stream_queue_cnt++;
4506 TAILQ_INSERT_TAIL(&strq->outqueue, chk, sctp_next);
4507 /* now check if this stream is on the wheel */
4508 if ((strq->next_spoke.tqe_next == NULL) &&
4509 (strq->next_spoke.tqe_prev == NULL)) {
4510 /* Insert it on the wheel since it is not
4513 sctp_insert_on_wheel(asoc, strq);
4515 } else if ((dataout) && (dataout > siz)) {
4517 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NO_FRAGMENT) &&
4520 SOCKBUF_LOCK(&so->so_snd);
4523 /* setup the template */
4524 sctp_prepare_chunk(&template, stcb, srcv, strq, net);
4527 while (dataout > siz) {
4529 * We can wait since this is called from the user
4532 n->m_nextpkt = m_split(n, siz, MB_WAIT);
4533 if (n->m_nextpkt == NULL) {
4535 SOCKBUF_LOCK(&so->so_snd);
4542 * ok, now we have a chain on m where m->m_nextpkt points to
4543 * the next chunk and m/m->m_next chain is the piece to send.
4544 * We must go through the chains and thread them on to
4545 * sctp_tmit_chunk chains and place them all on the stream
4546 * queue, breaking the m->m_nextpkt pointers as we go.
4552 * first go through and allocate a sctp_tmit chunk
4553 * for each chunk piece
4555 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
4558 * ok we must spin through and dump anything
4559 * we have allocated and then jump to the
4562 chk = TAILQ_FIRST(&tmp);
4564 TAILQ_REMOVE(&tmp, chk, sctp_next);
4565 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
4566 sctppcbinfo.ipi_count_chunk--;
4567 asoc->chunks_on_out_queue--;
4568 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
4569 panic("Chunk count is negative");
4571 sctppcbinfo.ipi_gencnt_chunk++;
4572 chk = TAILQ_FIRST(&tmp);
4575 SOCKBUF_LOCK(&so->so_snd);
4578 sctppcbinfo.ipi_count_chunk++;
4579 asoc->chunks_on_out_queue++;
4581 sctppcbinfo.ipi_gencnt_chunk++;
4583 chk->whoTo->ref_count++;
4585 /* Total in the MSIZE */
4587 for (mm = chk->data; mm; mm = mm->m_next) {
4589 if (mm->m_flags & M_EXT) {
4590 mbcnt_e += chk->data->m_ext.ext_size;
4593 /* now fix the chk->send_size */
4594 if (chk->data->m_flags & M_PKTHDR) {
4595 chk->send_size = chk->data->m_pkthdr.len;
4599 for (nn = chk->data; nn; nn = nn->m_next) {
4600 chk->send_size += nn->m_len;
4603 chk->book_size = chk->send_size;
4604 chk->mbcnt = mbcnt_e;
4606 if (chk->flags & SCTP_PR_SCTP_BUFFER) {
4607 asoc->sent_queue_cnt_removeable++;
4610 TAILQ_INSERT_TAIL(&tmp, chk, sctp_next);
4613 /* now that we have enough space for all de-couple the
4614 * chain of mbufs by going through our temp array
4615 * and breaking the pointers.
4617 /* ok, we are commited */
4618 if ((srcv->sinfo_flags & MSG_UNORDERED) == 0) {
4619 /* bump the ssn if we are unordered. */
4620 strq->next_sequence_sent++;
4622 /* Mark the first/last flags. This will
4623 * result int a 3 for a single item on the list
4625 chk = TAILQ_FIRST(&tmp);
4626 chk->rec.data.rcv_flags |= SCTP_DATA_FIRST_FRAG;
4627 chk = TAILQ_LAST(&tmp, sctpchunk_listhead);
4628 chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
4629 /* now break any chains on the queue and
4630 * move it to the streams actual queue.
4632 chk = TAILQ_FIRST(&tmp);
4634 chk->data->m_nextpkt = 0;
4635 TAILQ_REMOVE(&tmp, chk, sctp_next);
4636 asoc->stream_queue_cnt++;
4637 TAILQ_INSERT_TAIL(&strq->outqueue, chk, sctp_next);
4638 chk = TAILQ_FIRST(&tmp);
4640 /* now check if this stream is on the wheel */
4641 if ((strq->next_spoke.tqe_next == NULL) &&
4642 (strq->next_spoke.tqe_prev == NULL)) {
4643 /* Insert it on the wheel since it is not
4646 sctp_insert_on_wheel(asoc, strq);
4649 SOCKBUF_LOCK(&so->so_snd);
4650 /* has a SHUTDOWN been (also) requested by the user on this asoc? */
4653 if ((srcv->sinfo_flags & MSG_EOF) &&
4654 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
4656 int some_on_streamwheel = 0;
4658 if (!TAILQ_EMPTY(&asoc->out_wheel)) {
4659 /* Check to see if some data queued */
4660 struct sctp_stream_out *outs;
4661 TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) {
4662 if (!TAILQ_EMPTY(&outs->outqueue)) {
4663 some_on_streamwheel = 1;
4669 if (TAILQ_EMPTY(&asoc->send_queue) &&
4670 TAILQ_EMPTY(&asoc->sent_queue) &&
4671 (some_on_streamwheel == 0)) {
4672 /* there is nothing queued to send, so I'm done... */
4673 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
4674 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
4675 /* only send SHUTDOWN the first time through */
4677 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
4678 kprintf("%s:%d sends a shutdown\n",
4684 sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
4685 asoc->state = SCTP_STATE_SHUTDOWN_SENT;
4686 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
4687 asoc->primary_destination);
4688 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
4689 asoc->primary_destination);
4693 * we still got (or just got) data to send, so set
4697 * XXX sockets draft says that MSG_EOF should be sent
4698 * with no data. currently, we will allow user data
4699 * to be sent first and move to SHUTDOWN-PENDING
4701 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
4704 #ifdef SCTP_MBCNT_LOGGING
4705 sctp_log_mbcnt(SCTP_LOG_MBCNT_INCREASE,
4706 asoc->total_output_queue_size,
4708 asoc->total_output_mbuf_queue_size,
4711 asoc->total_output_queue_size += dataout;
4712 asoc->total_output_mbuf_queue_size += mbcnt;
4713 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4714 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
4715 so->so_snd.ssb_cc += dataout;
4716 so->so_snd.ssb_mbcnt += mbcnt;
4720 if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
4721 kprintf("++total out:%d total_mbuf_out:%d\n",
4722 (int)asoc->total_output_queue_size,
4723 (int)asoc->total_output_mbuf_queue_size);
4728 ssb_unlock(&so->so_snd);
4730 SOCKBUF_UNLOCK(&so->so_snd);
4732 if (m && m->m_nextpkt) {
4735 mnext = n->m_nextpkt;
4736 n->m_nextpkt = NULL;
4746 static struct mbuf *
4747 sctp_copy_mbufchain(struct mbuf *clonechain,
4748 struct mbuf *outchain)
4750 struct mbuf *appendchain;
4751 #if defined(__FreeBSD__) || defined(__NetBSD__)
4752 /* Supposedly m_copypacket is an optimization, use it if we can */
4753 if (clonechain->m_flags & M_PKTHDR) {
4754 appendchain = m_copypacket(clonechain, MB_DONTWAIT);
4755 sctp_pegs[SCTP_CACHED_SRC]++;
4757 appendchain = m_copy(clonechain, 0, M_COPYALL);
4758 #elif defined(__APPLE__)
4759 appendchain = sctp_m_copym(clonechain, 0, M_COPYALL, MB_DONTWAIT);
4761 appendchain = m_copy(clonechain, 0, M_COPYALL);
4764 if (appendchain == NULL) {
4767 sctp_m_freem(outchain);
4771 /* tack on to the end */
4775 if (m->m_next == NULL) {
4776 m->m_next = appendchain;
4781 if (outchain->m_flags & M_PKTHDR) {
4787 append_tot += t->m_len;
4790 outchain->m_pkthdr.len += append_tot;
4794 return (appendchain);
4799 sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr, u_int32_t val)
4801 struct sctp_copy_all *ca;
4805 ca = (struct sctp_copy_all *)ptr;
4806 if (ca->m == NULL) {
4809 if (ca->inp != inp) {
4813 m = sctp_copy_mbufchain(ca->m, NULL);
4815 /* can't copy so we are done */
4819 ret = sctp_msg_append(stcb, stcb->asoc.primary_destination, m,
4820 &ca->sndrcv, MSG_FNONBLOCKING);
4829 sctp_sendall_completes(void *ptr, u_int32_t val)
4831 struct sctp_copy_all *ca;
4832 ca = (struct sctp_copy_all *)ptr;
4833 /* Do a notify here?
4834 * Kacheong suggests that the notify
4835 * be done at the send time.. so you would
4836 * push up a notification if any send failed.
4837 * Don't know if this is feasable since the
4838 * only failures we have is "memory" related and
4839 * if you cannot get an mbuf to send the data
4840 * you surely can't get an mbuf to send up
4841 * to notify the user you can't send the data :->
4844 /* now free everything */
4850 #define MC_ALIGN(m, len) do { \
4851 (m)->m_data += (MCLBYTES - (len)) & ~(sizeof(long) - 1); \
4856 static struct mbuf *
4857 sctp_copy_out_all(struct uio *uio, int len)
4859 struct mbuf *ret, *at;
4860 int left, willcpy, cancpy, error;
4862 MGETHDR(ret, MB_WAIT, MT_HEADER);
4869 ret->m_pkthdr.len = len;
4870 MCLGET(ret, MB_WAIT);
4874 if ((ret->m_flags & M_EXT) == 0) {
4878 cancpy = M_TRAILINGSPACE(ret);
4879 willcpy = min(cancpy, left);
4882 /* Align data to the end */
4883 MC_ALIGN(at, willcpy);
4884 error = uiomove(mtod(at, caddr_t), willcpy, uio);
4890 at->m_len = willcpy;
4891 at->m_nextpkt = at->m_next = 0;
4894 MGET(at->m_next, MB_WAIT, MT_DATA);
4895 if (at->m_next == NULL) {
4900 MCLGET(at, MB_WAIT);
4904 if ((at->m_flags & M_EXT) == 0) {
4907 cancpy = M_TRAILINGSPACE(at);
4908 willcpy = min(cancpy, left);
4915 sctp_sendall (struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m, struct sctp_sndrcvinfo *srcv)
4918 struct sctp_copy_all *ca;
4919 ca = kmalloc(sizeof(struct sctp_copy_all), M_PCB, MB_WAIT);
4924 memset (ca, 0, sizeof(struct sctp_copy_all));
4928 /* take off the sendall flag, it would
4929 * be bad if we failed to do this :-0
4931 ca->sndrcv.sinfo_flags &= ~MSG_SENDALL;
4933 /* get length and mbuf chain */
4935 ca->sndlen = uio->uio_resid;
4936 ca->m = sctp_copy_out_all(uio, ca->sndlen);
4937 if (ca->m == NULL) {
4942 if ((m->m_flags & M_PKTHDR) == 0) {
4947 ca->sndlen += m->m_len;
4951 ca->sndlen = m->m_pkthdr.len;
4956 ret = sctp_initiate_iterator(sctp_sendall_iterator, SCTP_PCB_ANY_FLAGS, SCTP_ASOC_ANY_STATE,
4957 (void *)ca, 0, sctp_sendall_completes, inp);
4960 kprintf("Failed to initate iterator to takeover associations\n");
4971 sctp_toss_old_cookies(struct sctp_association *asoc)
4973 struct sctp_tmit_chunk *chk, *nchk;
4974 chk = TAILQ_FIRST(&asoc->control_send_queue);
4976 nchk = TAILQ_NEXT(chk, sctp_next);
4977 if (chk->rec.chunk_id == SCTP_COOKIE_ECHO) {
4978 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
4980 sctp_m_freem(chk->data);
4983 asoc->ctrl_queue_cnt--;
4985 sctp_free_remote_addr(chk->whoTo);
4986 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
4987 sctppcbinfo.ipi_count_chunk--;
4988 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
4989 panic("Chunk count is negative");
4991 sctppcbinfo.ipi_gencnt_chunk++;
4998 sctp_toss_old_asconf(struct sctp_tcb *stcb)
5000 struct sctp_association *asoc;
5001 struct sctp_tmit_chunk *chk, *chk_tmp;
5004 for (chk = TAILQ_FIRST(&asoc->control_send_queue); chk != NULL;
5007 chk_tmp = TAILQ_NEXT(chk, sctp_next);
5008 /* find SCTP_ASCONF chunk in queue (only one ever in queue) */
5009 if (chk->rec.chunk_id == SCTP_ASCONF) {
5010 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
5012 sctp_m_freem(chk->data);
5015 asoc->ctrl_queue_cnt--;
5017 sctp_free_remote_addr(chk->whoTo);
5018 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
5019 sctppcbinfo.ipi_count_chunk--;
5020 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
5021 panic("Chunk count is negative");
5023 sctppcbinfo.ipi_gencnt_chunk++;
5030 sctp_clean_up_datalist(struct sctp_tcb *stcb,
5031 struct sctp_association *asoc,
5032 struct sctp_tmit_chunk **data_list,
5034 struct sctp_nets *net)
5037 for (i = 0; i < bundle_at; i++) {
5038 /* off of the send queue */
5040 /* Any chunk NOT 0 you zap the time
5041 * chunk 0 gets zapped or set based on
5042 * if a RTO measurment is needed.
5044 data_list[i]->do_rtt = 0;
5047 data_list[i]->sent_rcv_time = net->last_sent_time;
5048 TAILQ_REMOVE(&asoc->send_queue,
5051 /* on to the sent queue */
5052 TAILQ_INSERT_TAIL(&asoc->sent_queue,
5055 /* This does not lower until the cum-ack passes it */
5056 asoc->sent_queue_cnt++;
5057 asoc->send_queue_cnt--;
5058 if ((asoc->peers_rwnd <= 0) &&
5059 (asoc->total_flight == 0) &&
5061 /* Mark the chunk as being a window probe */
5063 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
5064 kprintf("WINDOW PROBE SET\n");
5067 sctp_pegs[SCTP_WINDOW_PROBES]++;
5068 data_list[i]->rec.data.state_flags |= SCTP_WINDOW_PROBE;
5070 data_list[i]->rec.data.state_flags &= ~SCTP_WINDOW_PROBE;
5072 #ifdef SCTP_AUDITING_ENABLED
5073 sctp_audit_log(0xC2, 3);
5075 data_list[i]->sent = SCTP_DATAGRAM_SENT;
5076 data_list[i]->snd_count = 1;
5077 net->flight_size += data_list[i]->book_size;
5078 asoc->total_flight += data_list[i]->book_size;
5079 asoc->total_flight_count++;
5080 #ifdef SCTP_LOG_RWND
5081 sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
5082 asoc->peers_rwnd , data_list[i]->send_size, sctp_peer_chunk_oh);
5084 asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
5085 (u_int32_t)(data_list[i]->send_size + sctp_peer_chunk_oh));
5086 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
5087 /* SWS sender side engages */
5088 asoc->peers_rwnd = 0;
5094 sctp_clean_up_ctl(struct sctp_association *asoc)
5096 struct sctp_tmit_chunk *chk, *nchk;
5097 for (chk = TAILQ_FIRST(&asoc->control_send_queue);
5099 nchk = TAILQ_NEXT(chk, sctp_next);
5100 if ((chk->rec.chunk_id == SCTP_SELECTIVE_ACK) ||
5101 (chk->rec.chunk_id == SCTP_HEARTBEAT_REQUEST) ||
5102 (chk->rec.chunk_id == SCTP_HEARTBEAT_ACK) ||
5103 (chk->rec.chunk_id == SCTP_SHUTDOWN) ||
5104 (chk->rec.chunk_id == SCTP_SHUTDOWN_ACK) ||
5105 (chk->rec.chunk_id == SCTP_OPERATION_ERROR) ||
5106 (chk->rec.chunk_id == SCTP_PACKET_DROPPED) ||
5107 (chk->rec.chunk_id == SCTP_COOKIE_ACK) ||
5108 (chk->rec.chunk_id == SCTP_ECN_CWR) ||
5109 (chk->rec.chunk_id == SCTP_ASCONF_ACK)) {
5110 /* Stray chunks must be cleaned up */
5112 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
5114 sctp_m_freem(chk->data);
5117 asoc->ctrl_queue_cnt--;
5118 sctp_free_remote_addr(chk->whoTo);
5119 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
5120 sctppcbinfo.ipi_count_chunk--;
5121 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
5122 panic("Chunk count is negative");
5124 sctppcbinfo.ipi_gencnt_chunk++;
5125 } else if (chk->rec.chunk_id == SCTP_STREAM_RESET) {
5126 struct sctp_stream_reset_req *strreq;
5127 /* special handling, we must look into the param */
5128 strreq = mtod(chk->data, struct sctp_stream_reset_req *);
5129 if (strreq->sr_req.ph.param_type == ntohs(SCTP_STR_RESET_RESPONSE)) {
5130 goto clean_up_anyway;
5137 sctp_move_to_outqueue(struct sctp_tcb *stcb,
5138 struct sctp_stream_out *strq)
5140 /* Move from the stream to the send_queue keeping track of the total */
5141 struct sctp_association *asoc;
5145 struct sctp_tmit_chunk *chk, *nchk;
5146 struct sctp_data_chunk *dchkh;
5147 struct sctpchunk_listhead tmp;
5152 chk = TAILQ_FIRST(&strq->outqueue);
5154 nchk = TAILQ_NEXT(chk, sctp_next);
5155 /* now put in the chunk header */
5157 M_PREPEND(chk->data, sizeof(struct sctp_data_chunk), MB_DONTWAIT);
5158 if (chk->data == NULL) {
5163 if (orig != chk->data) {
5164 /* A new mbuf was added, account for it */
5165 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5166 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
5167 stcb->sctp_socket->so_snd.ssb_mbcnt += MSIZE;
5169 #ifdef SCTP_MBCNT_LOGGING
5170 sctp_log_mbcnt(SCTP_LOG_MBCNT_INCREASE,
5171 asoc->total_output_queue_size,
5173 asoc->total_output_mbuf_queue_size,
5176 stcb->asoc.total_output_mbuf_queue_size += MSIZE;
5177 chk->mbcnt += MSIZE;
5179 chk->send_size += sizeof(struct sctp_data_chunk);
5180 /* This should NOT have to do anything, but
5181 * I would rather be cautious
5183 if (!failed && ((size_t)chk->data->m_len < sizeof(struct sctp_data_chunk))) {
5184 m_pullup(chk->data, sizeof(struct sctp_data_chunk));
5185 if (chk->data == NULL) {
5190 dchkh = mtod(chk->data, struct sctp_data_chunk *);
5191 dchkh->ch.chunk_length = htons(chk->send_size);
5192 /* Chunks must be padded to even word boundary */
5193 padval = chk->send_size % 4;
5195 /* For fragmented messages this should not
5196 * run except possibly on the last chunk
5198 if (sctp_pad_lastmbuf(chk->data, (4 - padval))) {
5199 /* we are in big big trouble no mbufs :< */
5203 chk->send_size += (4 - padval);
5205 /* pull from stream queue */
5206 TAILQ_REMOVE(&strq->outqueue, chk, sctp_next);
5207 asoc->stream_queue_cnt--;
5208 TAILQ_INSERT_TAIL(&tmp, chk, sctp_next);
5209 /* add it in to the size of moved chunks */
5210 if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
5211 /* we pull only one message */
5217 /* Gak, we just lost the user message */
5218 chk = TAILQ_FIRST(&tmp);
5220 nchk = TAILQ_NEXT(chk, sctp_next);
5221 TAILQ_REMOVE(&tmp, chk, sctp_next);
5223 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb,
5224 (SCTP_NOTIFY_DATAGRAM_UNSENT|SCTP_INTERNAL_ERROR),
5228 sctp_m_freem(chk->data);
5232 sctp_free_remote_addr(chk->whoTo);
5235 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
5236 sctppcbinfo.ipi_count_chunk--;
5237 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
5238 panic("Chunk count is negative");
5240 sctppcbinfo.ipi_gencnt_chunk++;
5245 /* now pull them off of temp wheel */
5246 chk = TAILQ_FIRST(&tmp);
5248 nchk = TAILQ_NEXT(chk, sctp_next);
5249 /* insert on send_queue */
5250 TAILQ_REMOVE(&tmp, chk, sctp_next);
5251 TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
5252 asoc->send_queue_cnt++;
5254 chk->rec.data.TSN_seq = asoc->sending_seq++;
5256 dchkh = mtod(chk->data, struct sctp_data_chunk *);
5257 /* Put the rest of the things in place now. Size
5258 * was done earlier in previous loop prior to
5261 dchkh->ch.chunk_type = SCTP_DATA;
5262 dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
5263 dchkh->dp.tsn = htonl(chk->rec.data.TSN_seq);
5264 dchkh->dp.stream_id = htons(strq->stream_no);
5265 dchkh->dp.stream_sequence = htons(chk->rec.data.stream_seq);
5266 dchkh->dp.protocol_id = chk->rec.data.payloadtype;
5267 /* total count moved */
5268 tot_moved += chk->send_size;
5275 sctp_fill_outqueue(struct sctp_tcb *stcb,
5276 struct sctp_nets *net)
5278 struct sctp_association *asoc;
5279 struct sctp_tmit_chunk *chk;
5280 struct sctp_stream_out *strq, *strqn;
5281 int mtu_fromwheel, goal_mtu;
5282 unsigned int moved, seenend, cnt_mvd=0;
5285 /* Attempt to move at least 1 MTU's worth
5286 * onto the wheel for each destination address
5288 goal_mtu = net->cwnd - net->flight_size;
5289 if ((unsigned int)goal_mtu < net->mtu) {
5290 goal_mtu = net->mtu;
5292 if (sctp_pegs[SCTP_MOVED_MTU] < (unsigned int)goal_mtu) {
5293 sctp_pegs[SCTP_MOVED_MTU] = goal_mtu;
5295 seenend = moved = mtu_fromwheel = 0;
5296 if (asoc->last_out_stream == NULL) {
5297 strq = asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
5298 if (asoc->last_out_stream == NULL) {
5299 /* huh nothing on the wheel, TSNH */
5304 strq = TAILQ_NEXT(asoc->last_out_stream, next_spoke);
5307 asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
5309 while (mtu_fromwheel < goal_mtu) {
5313 strq = TAILQ_FIRST(&asoc->out_wheel);
5314 } else if ((moved == 0) && (seenend)) {
5315 /* none left on the wheel */
5316 sctp_pegs[SCTP_MOVED_NLEF]++;
5320 * clear the flags and rotate back through
5325 strq = TAILQ_FIRST(&asoc->out_wheel);
5331 strqn = TAILQ_NEXT(strq, next_spoke);
5332 if ((chk = TAILQ_FIRST(&strq->outqueue)) == NULL) {
5333 /* none left on this queue, prune a spoke? */
5334 sctp_remove_from_wheel(asoc, strq);
5335 if (strq == asoc->last_out_stream) {
5336 /* the last one we used went off the wheel */
5337 asoc->last_out_stream = NULL;
5342 if (chk->whoTo != net) {
5343 /* Skip this stream, first one on stream
5344 * does not head to our current destination.
5349 mtu_fromwheel += sctp_move_to_outqueue(stcb, strq);
5352 asoc->last_out_stream = strq;
5355 sctp_pegs[SCTP_MOVED_MAX]++;
5357 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5358 kprintf("Ok we moved %d chunks to send queue\n",
5362 if (sctp_pegs[SCTP_MOVED_QMAX] < cnt_mvd) {
5363 sctp_pegs[SCTP_MOVED_QMAX] = cnt_mvd;
5368 sctp_fix_ecn_echo(struct sctp_association *asoc)
5370 struct sctp_tmit_chunk *chk;
5371 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
5372 if (chk->rec.chunk_id == SCTP_ECN_ECHO) {
5373 chk->sent = SCTP_DATAGRAM_UNSENT;
5379 sctp_move_to_an_alt(struct sctp_tcb *stcb,
5380 struct sctp_association *asoc,
5381 struct sctp_nets *net)
5383 struct sctp_tmit_chunk *chk;
5384 struct sctp_nets *a_net;
5385 a_net = sctp_find_alternate_net(stcb, net);
5386 if ((a_net != net) &&
5387 ((a_net->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE)) {
5389 * We only proceed if a valid alternate is found that is
5390 * not this one and is reachable. Here we must move all
5391 * chunks queued in the send queue off of the destination
5392 * address to our alternate.
5394 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
5395 if (chk->whoTo == net) {
5396 /* Move the chunk to our alternate */
5397 sctp_free_remote_addr(chk->whoTo);
5405 static int sctp_from_user_send=0;
5408 sctp_med_chunk_output(struct sctp_inpcb *inp,
5409 struct sctp_tcb *stcb,
5410 struct sctp_association *asoc,
5413 int control_only, int *cwnd_full, int from_where,
5414 struct timeval *now, int *now_filled)
5417 * Ok this is the generic chunk service queue.
5418 * we must do the following:
5419 * - Service the stream queue that is next, moving any message
5420 * (note I must get a complete message i.e. FIRST/MIDDLE and
5421 * LAST to the out queue in one pass) and assigning TSN's
5422 * - Check to see if the cwnd/rwnd allows any output, if so we
5423 * go ahead and fomulate and send the low level chunks. Making
5424 * sure to combine any control in the control chunk queue also.
5426 struct sctp_nets *net;
5427 struct mbuf *outchain;
5428 struct sctp_tmit_chunk *chk, *nchk;
5429 struct sctphdr *shdr;
5430 /* temp arrays for unlinking */
5431 struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
5432 int no_fragmentflg, error;
5433 int one_chunk, hbflag;
5434 int asconf, cookie, no_out_cnt;
5435 int bundle_at, ctl_cnt, no_data_chunks, cwnd_full_ind;
5436 unsigned int mtu, r_mtu, omtu;
5439 ctl_cnt = no_out_cnt = asconf = cookie = 0;
5441 * First lets prime the pump. For each destination, if there
5442 * is room in the flight size, attempt to pull an MTU's worth
5443 * out of the stream queues into the general send_queue
5445 #ifdef SCTP_AUDITING_ENABLED
5446 sctp_audit_log(0xC2, 2);
5449 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5450 kprintf("***********************\n");
5459 /* Nothing to possible to send? */
5460 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
5461 TAILQ_EMPTY(&asoc->send_queue) &&
5462 TAILQ_EMPTY(&asoc->out_wheel)) {
5464 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5465 kprintf("All wheels empty\n");
5470 if (asoc->peers_rwnd <= 0) {
5471 /* No room in peers rwnd */
5474 if (asoc->total_flight > 0) {
5475 /* we are allowed one chunk in flight */
5477 sctp_pegs[SCTP_RWND_BLOCKED]++;
5481 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5482 kprintf("Ok we have done the fillup no_data_chunk=%d tf=%d prw:%d\n",
5484 (int)asoc->total_flight, (int)asoc->peers_rwnd);
5487 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5489 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5490 kprintf("net:%p fs:%d cwnd:%d\n",
5491 net, net->flight_size, net->cwnd);
5494 if (net->flight_size >= net->cwnd) {
5495 /* skip this network, no room */
5498 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5499 kprintf("Ok skip fillup->fs:%d > cwnd:%d\n",
5504 sctp_pegs[SCTP_CWND_NOFILL]++;
5508 * spin through the stream queues moving one message and
5509 * assign TSN's as appropriate.
5511 sctp_fill_outqueue(stcb, net);
5513 *cwnd_full = cwnd_full_ind;
5514 /* now service each destination and send out what we can for it */
5516 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5518 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
5521 kprintf("We have %d chunks on the send_queue\n", chk_cnt);
5523 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
5526 kprintf("We have %d chunks on the sent_queue\n", chk_cnt);
5527 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
5530 kprintf("We have %d chunks on the control_queue\n", chk_cnt);
5533 /* If we have data to send, and DSACK is running, stop it
5534 * and build a SACK to dump on to bundle with output. This
5535 * actually MAY make it so the bundling does not occur if
5536 * the SACK is big but I think this is ok because basic SACK
5537 * space is pre-reserved in our fragmentation size choice.
5539 if ((TAILQ_FIRST(&asoc->send_queue) != NULL) &&
5540 (no_data_chunks == 0)) {
5541 /* We will be sending something */
5542 if (callout_pending(&stcb->asoc.dack_timer.timer)) {
5543 /* Yep a callout is pending */
5544 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
5547 sctp_send_sack(stcb);
5550 /* Nothing to send? */
5551 if ((TAILQ_FIRST(&asoc->control_send_queue) == NULL) &&
5552 (TAILQ_FIRST(&asoc->send_queue) == NULL)) {
5555 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5556 /* how much can we send? */
5557 if (net->ref_count < 2) {
5558 /* Ref-count of 1 so we cannot have data or control
5559 * queued to this address. Skip it.
5563 ctl_cnt = bundle_at = 0;
5568 if ((net->ro.ro_rt) && (net->ro.ro_rt->rt_ifp)) {
5569 /* if we have a route and an ifp
5570 * check to see if we have room to
5574 ifp = net->ro.ro_rt->rt_ifp;
5575 if ((ifp->if_snd.ifq_len + 2) >= ifp->if_snd.ifq_maxlen) {
5576 sctp_pegs[SCTP_IFP_QUEUE_FULL]++;
5577 #ifdef SCTP_LOG_MAXBURST
5578 sctp_log_maxburst(net, ifp->if_snd.ifq_len, ifp->if_snd.ifq_maxlen, SCTP_MAX_IFP_APPLIED);
5583 if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
5584 mtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
5586 mtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
5588 if (mtu > asoc->peers_rwnd) {
5589 if (asoc->total_flight > 0) {
5590 /* We have a packet in flight somewhere */
5591 r_mtu = asoc->peers_rwnd;
5593 /* We are always allowed to send one MTU out */
5601 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5602 kprintf("Ok r_mtu is %d mtu is %d for this net:%p one_chunk:%d\n",
5603 r_mtu, mtu, net, one_chunk);
5606 /************************/
5607 /* Control transmission */
5608 /************************/
5609 /* Now first lets go through the control queue */
5610 for (chk = TAILQ_FIRST(&asoc->control_send_queue);
5612 nchk = TAILQ_NEXT(chk, sctp_next);
5613 if (chk->whoTo != net) {
5615 * No, not sent to the network we are
5620 if (chk->data == NULL) {
5623 if ((chk->data->m_flags & M_PKTHDR) == 0) {
5625 * NOTE: the chk queue MUST have the PKTHDR
5626 * flag set on it with a total in the
5627 * m_pkthdr.len field!! else the chunk will
5632 if (chk->sent != SCTP_DATAGRAM_UNSENT) {
5634 * It must be unsent. Cookies and ASCONF's
5635 * hang around but there timers will force
5636 * when marked for resend.
5640 /* Here we do NOT factor the r_mtu */
5641 if ((chk->data->m_pkthdr.len < (int)mtu) ||
5642 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
5644 * We probably should glom the mbuf chain from
5645 * the chk->data for control but the problem
5646 * is it becomes yet one more level of
5647 * tracking to do if for some reason output
5648 * fails. Then I have got to reconstruct the
5649 * merged control chain.. el yucko.. for now
5650 * we take the easy way and do the copy
5652 outchain = sctp_copy_mbufchain(chk->data,
5654 if (outchain == NULL) {
5657 /* update our MTU size */
5658 mtu -= chk->data->m_pkthdr.len;
5662 /* Do clear IP_DF ? */
5663 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
5666 /* Mark things to be removed, if needed */
5667 if ((chk->rec.chunk_id == SCTP_SELECTIVE_ACK) ||
5668 (chk->rec.chunk_id == SCTP_HEARTBEAT_REQUEST) ||
5669 (chk->rec.chunk_id == SCTP_HEARTBEAT_ACK) ||
5670 (chk->rec.chunk_id == SCTP_SHUTDOWN) ||
5671 (chk->rec.chunk_id == SCTP_SHUTDOWN_ACK) ||
5672 (chk->rec.chunk_id == SCTP_OPERATION_ERROR) ||
5673 (chk->rec.chunk_id == SCTP_COOKIE_ACK) ||
5674 (chk->rec.chunk_id == SCTP_ECN_CWR) ||
5675 (chk->rec.chunk_id == SCTP_PACKET_DROPPED) ||
5676 (chk->rec.chunk_id == SCTP_ASCONF_ACK)) {
5678 if (chk->rec.chunk_id == SCTP_HEARTBEAT_REQUEST)
5680 /* remove these chunks at the end */
5681 if (chk->rec.chunk_id == SCTP_SELECTIVE_ACK) {
5682 /* turn off the timer */
5683 if (callout_pending(&stcb->asoc.dack_timer.timer)) {
5684 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
5691 * Other chunks, since they have
5692 * timers running (i.e. COOKIE or
5693 * ASCONF) we just "trust" that it
5694 * gets sent or retransmitted.
5697 if (chk->rec.chunk_id == SCTP_COOKIE_ECHO) {
5700 } else if (chk->rec.chunk_id == SCTP_ASCONF) {
5702 * set hb flag since we can use
5708 chk->sent = SCTP_DATAGRAM_SENT;
5713 * Ok we are out of room but we can
5714 * output without effecting the flight
5715 * size since this little guy is a
5716 * control only packet.
5719 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
5723 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
5726 if (outchain->m_len == 0) {
5728 * Special case for when you
5729 * get a 0 len mbuf at the
5730 * head due to the lack of a
5731 * MHDR at the beginning.
5733 outchain->m_len = sizeof(struct sctphdr);
5735 M_PREPEND(outchain, sizeof(struct sctphdr), MB_DONTWAIT);
5736 if (outchain == NULL) {
5739 goto error_out_again;
5742 shdr = mtod(outchain, struct sctphdr *);
5743 shdr->src_port = inp->sctp_lport;
5744 shdr->dest_port = stcb->rport;
5745 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
5748 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
5749 (struct sockaddr *)&net->ro._l_addr,
5751 no_fragmentflg, 0, NULL, asconf))) {
5752 if (error == ENOBUFS) {
5753 asoc->ifp_had_enobuf = 1;
5755 sctp_pegs[SCTP_DATA_OUT_ERR]++;
5756 if (from_where == 0) {
5757 sctp_pegs[SCTP_ERROUT_FRM_USR]++;
5761 if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
5762 kprintf("Gak got ctrl error %d\n", error);
5765 /* error, could not output */
5768 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5769 kprintf("Update HB anyway\n");
5772 if (*now_filled == 0) {
5773 SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
5775 *now = net->last_sent_time;
5777 net->last_sent_time = *now;
5781 if (error == EHOSTUNREACH) {
5784 * unreachable during
5788 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5789 kprintf("Moving data to an alterante\n");
5792 sctp_move_to_an_alt(stcb, asoc, net);
5794 sctp_clean_up_ctl (asoc);
5797 asoc->ifp_had_enobuf = 0;
5798 /* Only HB or ASCONF advances time */
5800 if (*now_filled == 0) {
5801 SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
5803 *now = net->last_sent_time;
5805 net->last_sent_time = *now;
5810 * increase the number we sent, if a
5811 * cookie is sent we don't tell them
5815 *num_out += ctl_cnt;
5816 /* recalc a clean slate and setup */
5817 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5818 mtu = (net->mtu - SCTP_MIN_OVERHEAD);
5820 mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
5826 /*********************/
5827 /* Data transmission */
5828 /*********************/
5829 /* now lets add any data within the MTU constraints */
5830 if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
5831 omtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
5833 omtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
5837 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5838 kprintf("Now to data transmission\n");
5842 if (((asoc->state & SCTP_STATE_OPEN) == SCTP_STATE_OPEN) ||
5844 for (chk = TAILQ_FIRST(&asoc->send_queue); chk; chk = nchk) {
5845 if (no_data_chunks) {
5846 /* let only control go out */
5848 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5849 kprintf("Either nothing to send or we are full\n");
5854 if (net->flight_size >= net->cwnd) {
5855 /* skip this net, no room for data */
5857 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5858 kprintf("fs:%d > cwnd:%d\n",
5859 net->flight_size, net->cwnd);
5862 sctp_pegs[SCTP_CWND_BLOCKED]++;
5866 nchk = TAILQ_NEXT(chk, sctp_next);
5867 if (chk->whoTo != net) {
5868 /* No, not sent to this net */
5870 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5871 kprintf("chk->whoTo:%p not %p\n",
5879 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5880 kprintf("Can we pick up a chunk?\n");
5883 if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
5884 /* strange, we have a chunk that is to bit
5885 * for its destination and yet no fragment ok flag.
5886 * Something went wrong when the PMTU changed...we did
5887 * not mark this chunk for some reason?? I will
5888 * fix it here by letting IP fragment it for now and
5889 * printing a warning. This really should not happen ...
5891 /*#ifdef SCTP_DEBUG*/
5892 kprintf("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
5893 chk->send_size, mtu);
5895 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
5898 if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
5899 ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
5900 /* ok we will add this one */
5902 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5903 kprintf("Picking up the chunk\n");
5906 outchain = sctp_copy_mbufchain(chk->data, outchain);
5907 if (outchain == NULL) {
5909 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5910 kprintf("Gakk no memory\n");
5913 if (!callout_pending(&net->rxt_timer.timer)) {
5914 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
5918 /* upate our MTU size */
5919 /* Do clear IP_DF ? */
5920 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
5923 mtu -= chk->send_size;
5924 r_mtu -= chk->send_size;
5925 data_list[bundle_at++] = chk;
5926 if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
5934 if ((r_mtu <= 0) || one_chunk) {
5940 * Must be sent in order of the TSN's
5944 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5945 kprintf("ok no more chk:%d > mtu:%d || < r_mtu:%d\n",
5946 chk->send_size, mtu, r_mtu);
5953 } /* if asoc.state OPEN */
5954 /* Is there something to send for this destination? */
5956 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5957 kprintf("ok now is chain assembled? %p\n",
5963 /* We may need to start a control timer or two */
5965 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
5969 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
5972 /* must start a send timer if data is being sent */
5973 if (bundle_at && (!callout_pending(&net->rxt_timer.timer))) {
5974 /* no timer running on this destination
5978 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5979 kprintf("ok lets start a send timer .. we will transmit %p\n",
5983 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
5985 /* Now send it, if there is anything to send :> */
5986 if ((outchain->m_flags & M_PKTHDR) == 0) {
5989 MGETHDR(t, MB_DONTWAIT, MT_HEADER);
5991 sctp_m_freem(outchain);
5994 t->m_next = outchain;
5995 t->m_pkthdr.len = 0;
5996 t->m_pkthdr.rcvif = 0;
6001 outchain->m_pkthdr.len += t->m_len;
6005 if (outchain->m_len == 0) {
6006 /* Special case for when you get a 0 len
6007 * mbuf at the head due to the lack
6008 * of a MHDR at the beginning.
6010 MH_ALIGN(outchain, sizeof(struct sctphdr));
6011 outchain->m_len = sizeof(struct sctphdr);
6013 M_PREPEND(outchain, sizeof(struct sctphdr), MB_DONTWAIT);
6014 if (outchain == NULL) {
6020 shdr = mtod(outchain, struct sctphdr *);
6021 shdr->src_port = inp->sctp_lport;
6022 shdr->dest_port = stcb->rport;
6023 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
6025 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
6026 (struct sockaddr *)&net->ro._l_addr,
6028 no_fragmentflg, bundle_at, data_list[0], asconf))) {
6029 /* error, we could not output */
6030 if (error == ENOBUFS) {
6031 asoc->ifp_had_enobuf = 1;
6033 sctp_pegs[SCTP_DATA_OUT_ERR]++;
6034 if (from_where == 0) {
6035 sctp_pegs[SCTP_ERROUT_FRM_USR]++;
6040 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
6041 kprintf("Gak send error %d\n", error);
6046 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
6047 kprintf("Update HB time anyway\n");
6050 if (*now_filled == 0) {
6051 SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
6053 *now = net->last_sent_time;
6055 net->last_sent_time = *now;
6059 if (error == EHOSTUNREACH) {
6061 * Destination went unreachable during
6065 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
6066 kprintf("Calling the movement routine\n");
6069 sctp_move_to_an_alt(stcb, asoc, net);
6071 sctp_clean_up_ctl (asoc);
6074 asoc->ifp_had_enobuf = 0;
6076 if (bundle_at || hbflag) {
6077 /* For data/asconf and hb set time */
6078 if (*now_filled == 0) {
6079 SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
6081 *now = net->last_sent_time;
6083 net->last_sent_time = *now;
6088 *num_out += (ctl_cnt + bundle_at);
6091 if (!net->rto_pending) {
6092 /* setup for a RTO measurement */
6093 net->rto_pending = 1;
6094 data_list[0]->do_rtt = 1;
6096 data_list[0]->do_rtt = 0;
6098 sctp_pegs[SCTP_PEG_TSNS_SENT] += bundle_at;
6099 sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
6106 /* At the end there should be no NON timed
6107 * chunks hanging on this queue.
6109 if ((*num_out == 0) && (*reason_code == 0)) {
6112 sctp_clean_up_ctl (asoc);
6117 sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
6119 /* Prepend a OPERATIONAL_ERROR chunk header
6120 * and put on the end of the control chunk queue.
6122 /* Sender had better have gotten a MGETHDR or else
6123 * the control chunk will be forever skipped
6125 struct sctp_chunkhdr *hdr;
6126 struct sctp_tmit_chunk *chk;
6129 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
6132 sctp_m_freem(op_err);
6135 sctppcbinfo.ipi_count_chunk++;
6136 sctppcbinfo.ipi_gencnt_chunk++;
6137 M_PREPEND(op_err, sizeof(struct sctp_chunkhdr), MB_DONTWAIT);
6138 if (op_err == NULL) {
6139 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
6140 sctppcbinfo.ipi_count_chunk--;
6141 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
6142 panic("Chunk count is negative");
6144 sctppcbinfo.ipi_gencnt_chunk++;
6149 while (mat != NULL) {
6150 chk->send_size += mat->m_len;
6153 chk->rec.chunk_id = SCTP_OPERATION_ERROR;
6154 chk->sent = SCTP_DATAGRAM_UNSENT;
6157 chk->asoc = &stcb->asoc;
6159 chk->whoTo = chk->asoc->primary_destination;
6160 chk->whoTo->ref_count++;
6161 hdr = mtod(op_err, struct sctp_chunkhdr *);
6162 hdr->chunk_type = SCTP_OPERATION_ERROR;
6163 hdr->chunk_flags = 0;
6164 hdr->chunk_length = htons(chk->send_size);
6165 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue,
6168 chk->asoc->ctrl_queue_cnt++;
6172 sctp_send_cookie_echo(struct mbuf *m,
6174 struct sctp_tcb *stcb,
6175 struct sctp_nets *net)
6178 * pull out the cookie and put it at the front of the control
6182 struct mbuf *cookie, *mat;
6183 struct sctp_paramhdr parm, *phdr;
6184 struct sctp_chunkhdr *hdr;
6185 struct sctp_tmit_chunk *chk;
6186 uint16_t ptype, plen;
6187 /* First find the cookie in the param area */
6189 at = offset + sizeof(struct sctp_init_chunk);
6192 phdr = sctp_get_next_param(m, at, &parm, sizeof(parm));
6196 ptype = ntohs(phdr->param_type);
6197 plen = ntohs(phdr->param_length);
6198 if (ptype == SCTP_STATE_COOKIE) {
6200 /* found the cookie */
6201 if ((pad = (plen % 4))) {
6204 cookie = sctp_m_copym(m, at, plen, MB_DONTWAIT);
6205 if (cookie == NULL) {
6211 at += SCTP_SIZE32(plen);
6213 if (cookie == NULL) {
6214 /* Did not find the cookie */
6217 /* ok, we got the cookie lets change it into a cookie echo chunk */
6219 /* first the change from param to cookie */
6220 hdr = mtod(cookie, struct sctp_chunkhdr *);
6221 hdr->chunk_type = SCTP_COOKIE_ECHO;
6222 hdr->chunk_flags = 0;
6223 /* now we MUST have a PKTHDR on it */
6224 if ((cookie->m_flags & M_PKTHDR) != M_PKTHDR) {
6225 /* we hope this happens rarely */
6226 MGETHDR(mat, MB_DONTWAIT, MT_HEADER);
6228 sctp_m_freem(cookie);
6232 mat->m_pkthdr.rcvif = 0;
6233 mat->m_next = cookie;
6236 cookie->m_pkthdr.len = plen;
6237 /* get the chunk stuff now and place it in the FRONT of the queue */
6238 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
6241 sctp_m_freem(cookie);
6244 sctppcbinfo.ipi_count_chunk++;
6245 sctppcbinfo.ipi_gencnt_chunk++;
6246 chk->send_size = cookie->m_pkthdr.len;
6247 chk->rec.chunk_id = SCTP_COOKIE_ECHO;
6248 chk->sent = SCTP_DATAGRAM_UNSENT;
6251 chk->asoc = &stcb->asoc;
6253 chk->whoTo = chk->asoc->primary_destination;
6254 chk->whoTo->ref_count++;
6255 TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
6256 chk->asoc->ctrl_queue_cnt++;
6261 sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
6265 struct sctp_nets *net)
6267 /* take a HB request and make it into a
6268 * HB ack and send it.
6270 struct mbuf *outchain;
6271 struct sctp_chunkhdr *chdr;
6272 struct sctp_tmit_chunk *chk;
6276 /* must have a net pointer */
6279 outchain = sctp_m_copym(m, offset, chk_length, MB_DONTWAIT);
6280 if (outchain == NULL) {
6281 /* gak out of memory */
6284 chdr = mtod(outchain, struct sctp_chunkhdr *);
6285 chdr->chunk_type = SCTP_HEARTBEAT_ACK;
6286 chdr->chunk_flags = 0;
6287 if ((outchain->m_flags & M_PKTHDR) != M_PKTHDR) {
6288 /* should not happen but we are cautious. */
6290 MGETHDR(tmp, MB_DONTWAIT, MT_HEADER);
6295 tmp->m_pkthdr.rcvif = 0;
6296 tmp->m_next = outchain;
6299 outchain->m_pkthdr.len = chk_length;
6300 if (chk_length % 4) {
6304 padlen = 4 - (outchain->m_pkthdr.len % 4);
6305 m_copyback(outchain, outchain->m_pkthdr.len, padlen, (caddr_t)&cpthis);
6307 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
6310 sctp_m_freem(outchain);
6313 sctppcbinfo.ipi_count_chunk++;
6314 sctppcbinfo.ipi_gencnt_chunk++;
6316 chk->send_size = chk_length;
6317 chk->rec.chunk_id = SCTP_HEARTBEAT_ACK;
6318 chk->sent = SCTP_DATAGRAM_UNSENT;
6321 chk->asoc = &stcb->asoc;
6322 chk->data = outchain;
6324 chk->whoTo->ref_count++;
6325 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
6326 chk->asoc->ctrl_queue_cnt++;
6330 sctp_send_cookie_ack(struct sctp_tcb *stcb) {
6331 /* formulate and queue a cookie-ack back to sender */
6332 struct mbuf *cookie_ack;
6333 struct sctp_chunkhdr *hdr;
6334 struct sctp_tmit_chunk *chk;
6337 MGETHDR(cookie_ack, MB_DONTWAIT, MT_HEADER);
6338 if (cookie_ack == NULL) {
6342 cookie_ack->m_data += SCTP_MIN_OVERHEAD;
6343 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
6346 sctp_m_freem(cookie_ack);
6349 sctppcbinfo.ipi_count_chunk++;
6350 sctppcbinfo.ipi_gencnt_chunk++;
6352 chk->send_size = sizeof(struct sctp_chunkhdr);
6353 chk->rec.chunk_id = SCTP_COOKIE_ACK;
6354 chk->sent = SCTP_DATAGRAM_UNSENT;
6357 chk->asoc = &stcb->asoc;
6358 chk->data = cookie_ack;
6359 if (chk->asoc->last_control_chunk_from != NULL) {
6360 chk->whoTo = chk->asoc->last_control_chunk_from;
6362 chk->whoTo = chk->asoc->primary_destination;
6364 chk->whoTo->ref_count++;
6365 hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
6366 hdr->chunk_type = SCTP_COOKIE_ACK;
6367 hdr->chunk_flags = 0;
6368 hdr->chunk_length = htons(chk->send_size);
6369 cookie_ack->m_pkthdr.len = cookie_ack->m_len = chk->send_size;
6370 cookie_ack->m_pkthdr.rcvif = 0;
6371 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
6372 chk->asoc->ctrl_queue_cnt++;
6378 sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
6380 /* formulate and queue a SHUTDOWN-ACK back to the sender */
6381 struct mbuf *m_shutdown_ack;
6382 struct sctp_shutdown_ack_chunk *ack_cp;
6383 struct sctp_tmit_chunk *chk;
6385 m_shutdown_ack = NULL;
6386 MGETHDR(m_shutdown_ack, MB_DONTWAIT, MT_HEADER);
6387 if (m_shutdown_ack == NULL) {
6391 m_shutdown_ack->m_data += SCTP_MIN_OVERHEAD;
6392 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
6395 sctp_m_freem(m_shutdown_ack);
6398 sctppcbinfo.ipi_count_chunk++;
6399 sctppcbinfo.ipi_gencnt_chunk++;
6401 chk->send_size = sizeof(struct sctp_chunkhdr);
6402 chk->rec.chunk_id = SCTP_SHUTDOWN_ACK;
6403 chk->sent = SCTP_DATAGRAM_UNSENT;
6406 chk->asoc = &stcb->asoc;
6407 chk->data = m_shutdown_ack;
6411 ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
6412 ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
6413 ack_cp->ch.chunk_flags = 0;
6414 ack_cp->ch.chunk_length = htons(chk->send_size);
6415 m_shutdown_ack->m_pkthdr.len = m_shutdown_ack->m_len = chk->send_size;
6416 m_shutdown_ack->m_pkthdr.rcvif = 0;
6417 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
6418 chk->asoc->ctrl_queue_cnt++;
6423 sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
6425 /* formulate and queue a SHUTDOWN to the sender */
6426 struct mbuf *m_shutdown;
6427 struct sctp_shutdown_chunk *shutdown_cp;
6428 struct sctp_tmit_chunk *chk;
6431 MGETHDR(m_shutdown, MB_DONTWAIT, MT_HEADER);
6432 if (m_shutdown == NULL) {
6436 m_shutdown->m_data += SCTP_MIN_OVERHEAD;
6437 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
6440 sctp_m_freem(m_shutdown);
6443 sctppcbinfo.ipi_count_chunk++;
6444 sctppcbinfo.ipi_gencnt_chunk++;
6446 chk->send_size = sizeof(struct sctp_shutdown_chunk);
6447 chk->rec.chunk_id = SCTP_SHUTDOWN;
6448 chk->sent = SCTP_DATAGRAM_UNSENT;
6451 chk->asoc = &stcb->asoc;
6452 chk->data = m_shutdown;
6456 shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
6457 shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
6458 shutdown_cp->ch.chunk_flags = 0;
6459 shutdown_cp->ch.chunk_length = htons(chk->send_size);
6460 shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
6461 m_shutdown->m_pkthdr.len = m_shutdown->m_len = chk->send_size;
6462 m_shutdown->m_pkthdr.rcvif = 0;
6463 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
6464 chk->asoc->ctrl_queue_cnt++;
6466 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6467 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
6468 stcb->sctp_ep->sctp_socket->so_snd.ssb_cc = 0;
6469 soisdisconnecting(stcb->sctp_ep->sctp_socket);
6475 sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net)
6478 * formulate and queue an ASCONF to the peer
6479 * ASCONF parameters should be queued on the assoc queue
6481 struct sctp_tmit_chunk *chk;
6482 struct mbuf *m_asconf;
6483 struct sctp_asconf_chunk *acp;
6486 /* compose an ASCONF chunk, maximum length is PMTU */
6487 m_asconf = sctp_compose_asconf(stcb);
6488 if (m_asconf == NULL) {
6491 acp = mtod(m_asconf, struct sctp_asconf_chunk *);
6492 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
6495 sctp_m_freem(m_asconf);
6498 sctppcbinfo.ipi_count_chunk++;
6499 sctppcbinfo.ipi_gencnt_chunk++;
6501 chk->data = m_asconf;
6502 chk->send_size = m_asconf->m_pkthdr.len;
6503 chk->rec.chunk_id = SCTP_ASCONF;
6504 chk->sent = SCTP_DATAGRAM_UNSENT;
6507 chk->asoc = &stcb->asoc;
6508 chk->whoTo = chk->asoc->primary_destination;
6509 chk->whoTo->ref_count++;
6510 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
6511 chk->asoc->ctrl_queue_cnt++;
6516 sctp_send_asconf_ack(struct sctp_tcb *stcb, uint32_t retrans)
6519 * formulate and queue a asconf-ack back to sender
6520 * the asconf-ack must be stored in the tcb
6522 struct sctp_tmit_chunk *chk;
6525 /* is there a asconf-ack mbuf chain to send? */
6526 if (stcb->asoc.last_asconf_ack_sent == NULL) {
6530 /* copy the asconf_ack */
6531 #if defined(__FreeBSD__) || defined(__NetBSD__)
6532 /* Supposedly the m_copypacket is a optimzation,
6535 if (stcb->asoc.last_asconf_ack_sent->m_flags & M_PKTHDR) {
6536 m_ack = m_copypacket(stcb->asoc.last_asconf_ack_sent, MB_DONTWAIT);
6537 sctp_pegs[SCTP_CACHED_SRC]++;
6539 m_ack = m_copy(stcb->asoc.last_asconf_ack_sent, 0, M_COPYALL);
6541 m_ack = m_copy(stcb->asoc.last_asconf_ack_sent, 0, M_COPYALL);
6543 if (m_ack == NULL) {
6544 /* couldn't copy it */
6548 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
6552 sctp_m_freem(m_ack);
6555 sctppcbinfo.ipi_count_chunk++;
6556 sctppcbinfo.ipi_gencnt_chunk++;
6558 /* figure out where it goes to */
6560 /* we're doing a retransmission */
6561 if (stcb->asoc.used_alt_asconfack > 2) {
6562 /* tried alternate nets already, go back */
6565 /* need to try and alternate net */
6566 chk->whoTo = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from);
6567 stcb->asoc.used_alt_asconfack++;
6569 if (chk->whoTo == NULL) {
6571 if (stcb->asoc.last_control_chunk_from == NULL)
6572 chk->whoTo = stcb->asoc.primary_destination;
6574 chk->whoTo = stcb->asoc.last_control_chunk_from;
6575 stcb->asoc.used_alt_asconfack = 0;
6579 if (stcb->asoc.last_control_chunk_from == NULL)
6580 chk->whoTo = stcb->asoc.primary_destination;
6582 chk->whoTo = stcb->asoc.last_control_chunk_from;
6583 stcb->asoc.used_alt_asconfack = 0;
6586 chk->send_size = m_ack->m_pkthdr.len;
6587 chk->rec.chunk_id = SCTP_ASCONF_ACK;
6588 chk->sent = SCTP_DATAGRAM_UNSENT;
6591 chk->asoc = &stcb->asoc;
6592 chk->whoTo->ref_count++;
6593 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
6594 chk->asoc->ctrl_queue_cnt++;
6600 sctp_chunk_retransmission(struct sctp_inpcb *inp,
6601 struct sctp_tcb *stcb,
6602 struct sctp_association *asoc,
6603 int *cnt_out, struct timeval *now, int *now_filled)
6606 * send out one MTU of retransmission.
6607 * If fast_retransmit is happening we ignore the cwnd.
6608 * Otherwise we obey the cwnd and rwnd.
6609 * For a Cookie or Asconf in the control chunk queue we retransmit
6610 * them by themselves.
6612 * For data chunks we will pick out the lowest TSN's in the
6613 * sent_queue marked for resend and bundle them all together
6614 * (up to a MTU of destination). The address to send to should
6615 * have been selected/changed where the retransmission was
6616 * marked (i.e. in FR or t3-timeout routines).
6618 struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
6619 struct sctp_tmit_chunk *chk, *fwd;
6621 struct sctphdr *shdr;
6623 struct sctp_nets *net;
6624 int no_fragmentflg, bundle_at, cnt_thru;
6626 int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
6628 tmr_started = ctl_cnt = bundle_at = error = 0;
6635 #ifdef SCTP_AUDITING_ENABLED
6636 sctp_audit_log(0xC3, 1);
6638 if (TAILQ_EMPTY(&asoc->sent_queue)) {
6640 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
6641 kprintf("SCTP hits empty queue with cnt set to %d?\n",
6642 asoc->sent_queue_retran_cnt);
6645 asoc->sent_queue_cnt = 0;
6646 asoc->sent_queue_cnt_removeable = 0;
6648 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
6649 if (chk->sent != SCTP_DATAGRAM_RESEND) {
6650 /* we only worry about things marked for resend */
6653 if ((chk->rec.chunk_id == SCTP_COOKIE_ECHO) ||
6654 (chk->rec.chunk_id == SCTP_ASCONF) ||
6655 (chk->rec.chunk_id == SCTP_STREAM_RESET) ||
6656 (chk->rec.chunk_id == SCTP_FORWARD_CUM_TSN)) {
6657 if (chk->rec.chunk_id == SCTP_STREAM_RESET) {
6658 /* For stream reset we only retran the request
6661 struct sctp_stream_reset_req *strreq;
6662 strreq = mtod(chk->data, struct sctp_stream_reset_req *);
6663 if (strreq->sr_req.ph.param_type != ntohs(SCTP_STR_RESET_REQUEST)) {
6668 if (chk->rec.chunk_id == SCTP_ASCONF) {
6672 if (chk->rec.chunk_id == SCTP_FORWARD_CUM_TSN) {
6676 m = sctp_copy_mbufchain(chk->data, m);
6682 /* do we have control chunks to retransmit? */
6684 /* Start a timer no matter if we suceed or fail */
6685 if (chk->rec.chunk_id == SCTP_COOKIE_ECHO) {
6686 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
6687 } else if (chk->rec.chunk_id == SCTP_ASCONF)
6688 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
6690 if (m->m_len == 0) {
6691 /* Special case for when you get a 0 len
6692 * mbuf at the head due to the lack
6693 * of a MHDR at the beginning.
6695 m->m_len = sizeof(struct sctphdr);
6697 M_PREPEND(m, sizeof(struct sctphdr), MB_DONTWAIT);
6702 shdr = mtod(m, struct sctphdr *);
6703 shdr->src_port = inp->sctp_lport;
6704 shdr->dest_port = stcb->rport;
6705 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
6707 chk->snd_count++; /* update our count */
6709 if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
6710 (struct sockaddr *)&chk->whoTo->ro._l_addr, m,
6711 no_fragmentflg, 0, NULL, asconf))) {
6712 sctp_pegs[SCTP_DATA_OUT_ERR]++;
6716 *We don't want to mark the net->sent time here since this
6717 * we use this for HB and retrans cannot measure RTT
6719 /* SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time);*/
6721 chk->sent = SCTP_DATAGRAM_SENT;
6722 asoc->sent_queue_retran_cnt--;
6723 if (asoc->sent_queue_retran_cnt < 0) {
6724 asoc->sent_queue_retran_cnt = 0;
6729 /* Clean up the fwd-tsn list */
6730 sctp_clean_up_ctl (asoc);
6734 /* Ok, it is just data retransmission we need to do or
6735 * that and a fwd-tsn with it all.
6737 if (TAILQ_EMPTY(&asoc->sent_queue)) {
6741 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
6742 kprintf("Normal chunk retransmission cnt:%d\n",
6743 asoc->sent_queue_retran_cnt);
6746 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) ||
6747 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT)) {
6748 /* not yet open, resend the cookie and that is it */
6753 #ifdef SCTP_AUDITING_ENABLED
6754 sctp_auditing(20, inp, stcb, NULL);
6756 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
6757 if (chk->sent != SCTP_DATAGRAM_RESEND) {
6758 /* No, not sent to this net or not ready for rtx */
6762 /* pick up the net */
6764 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
6765 mtu = (net->mtu - SCTP_MIN_OVERHEAD);
6767 mtu = net->mtu- SCTP_MIN_V4_OVERHEAD;
6770 if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
6771 /* No room in peers rwnd */
6773 tsn = asoc->last_acked_seq + 1;
6774 if (tsn == chk->rec.data.TSN_seq) {
6775 /* we make a special exception for this case.
6776 * The peer has no rwnd but is missing the
6777 * lowest chunk.. which is probably what is
6778 * holding up the rwnd.
6780 goto one_chunk_around;
6783 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
6784 kprintf("blocked-peers_rwnd:%d tf:%d\n",
6785 (int)asoc->peers_rwnd,
6786 (int)asoc->total_flight);
6789 sctp_pegs[SCTP_RWND_BLOCKED]++;
6793 if (asoc->peers_rwnd < mtu) {
6796 #ifdef SCTP_AUDITING_ENABLED
6797 sctp_audit_log(0xC3, 2);
6801 net->fast_retran_ip = 0;
6802 if (chk->rec.data.doing_fast_retransmit == 0) {
6803 /* if no FR in progress skip destination that
6804 * have flight_size > cwnd.
6806 if (net->flight_size >= net->cwnd) {
6807 sctp_pegs[SCTP_CWND_BLOCKED]++;
6811 /* Mark the destination net to have FR recovery
6814 net->fast_retran_ip = 1;
6817 if ((chk->send_size <= mtu) || (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
6818 /* ok we will add this one */
6819 m = sctp_copy_mbufchain(chk->data, m);
6823 /* upate our MTU size */
6824 /* Do clear IP_DF ? */
6825 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
6828 mtu -= chk->send_size;
6829 data_list[bundle_at++] = chk;
6830 if (one_chunk && (asoc->total_flight <= 0)) {
6831 sctp_pegs[SCTP_WINDOW_PROBES]++;
6832 chk->rec.data.state_flags |= SCTP_WINDOW_PROBE;
6835 if (one_chunk == 0) {
6836 /* now are there anymore forward from chk to pick up?*/
6837 fwd = TAILQ_NEXT(chk, sctp_next);
6839 if (fwd->sent != SCTP_DATAGRAM_RESEND) {
6840 /* Nope, not for retran */
6841 fwd = TAILQ_NEXT(fwd, sctp_next);
6844 if (fwd->whoTo != net) {
6845 /* Nope, not the net in question */
6846 fwd = TAILQ_NEXT(fwd, sctp_next);
6849 if (fwd->send_size <= mtu) {
6850 m = sctp_copy_mbufchain(fwd->data, m);
6854 /* upate our MTU size */
6855 /* Do clear IP_DF ? */
6856 if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
6859 mtu -= fwd->send_size;
6860 data_list[bundle_at++] = fwd;
6861 if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
6864 fwd = TAILQ_NEXT(fwd, sctp_next);
6866 /* can't fit so we are done */
6871 /* Is there something to send for this destination? */
6873 /* No matter if we fail/or suceed we should
6874 * start a timer. A failure is like a lost
6877 if (!callout_pending(&net->rxt_timer.timer)) {
6878 /* no timer running on this destination
6881 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
6884 if (m->m_len == 0) {
6885 /* Special case for when you get a 0 len
6886 * mbuf at the head due to the lack
6887 * of a MHDR at the beginning.
6889 m->m_len = sizeof(struct sctphdr);
6891 M_PREPEND(m, sizeof(struct sctphdr), MB_DONTWAIT);
6896 shdr = mtod(m, struct sctphdr *);
6897 shdr->src_port = inp->sctp_lport;
6898 shdr->dest_port = stcb->rport;
6899 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
6902 /* Now lets send it, if there is anything to send :> */
6903 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
6904 (struct sockaddr *)&net->ro._l_addr,
6906 no_fragmentflg, 0, NULL, asconf))) {
6907 /* error, we could not output */
6908 sctp_pegs[SCTP_DATA_OUT_ERR]++;
6913 * We don't want to mark the net->sent time here since
6914 * this we use this for HB and retrans cannot measure
6917 /* SCTP_GETTIME_TIMEVAL(&net->last_sent_time);*/
6919 /* For auto-close */
6921 if (*now_filled == 0) {
6922 SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
6923 *now = asoc->time_last_sent;
6926 asoc->time_last_sent = *now;
6928 *cnt_out += bundle_at;
6929 #ifdef SCTP_AUDITING_ENABLED
6930 sctp_audit_log(0xC4, bundle_at);
6932 for (i = 0; i < bundle_at; i++) {
6933 sctp_pegs[SCTP_RETRANTSN_SENT]++;
6934 data_list[i]->sent = SCTP_DATAGRAM_SENT;
6935 data_list[i]->snd_count++;
6936 asoc->sent_queue_retran_cnt--;
6937 /* record the time */
6938 data_list[i]->sent_rcv_time = asoc->time_last_sent;
6939 if (asoc->sent_queue_retran_cnt < 0) {
6940 asoc->sent_queue_retran_cnt = 0;
6942 net->flight_size += data_list[i]->book_size;
6943 asoc->total_flight += data_list[i]->book_size;
6944 asoc->total_flight_count++;
6946 #ifdef SCTP_LOG_RWND
6947 sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
6948 asoc->peers_rwnd , data_list[i]->send_size, sctp_peer_chunk_oh);
6950 asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
6951 (u_int32_t)(data_list[i]->send_size + sctp_peer_chunk_oh));
6952 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
6953 /* SWS sender side engages */
6954 asoc->peers_rwnd = 0;
6958 (data_list[i]->rec.data.doing_fast_retransmit)) {
6959 sctp_pegs[SCTP_FAST_RETRAN]++;
6960 if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
6961 (tmr_started == 0)) {
6963 * ok we just fast-retrans'd
6964 * the lowest TSN, i.e the
6965 * first on the list. In this
6966 * case we want to give some
6967 * more time to get a SACK
6968 * back without a t3-expiring.
6970 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
6971 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
6975 #ifdef SCTP_AUDITING_ENABLED
6976 sctp_auditing(21, inp, stcb, NULL);
6982 if (asoc->sent_queue_retran_cnt <= 0) {
6983 /* all done we have no more to retran */
6984 asoc->sent_queue_retran_cnt = 0;
6988 /* No more room in rwnd */
6991 /* stop the for loop here. we sent out a packet */
6999 sctp_timer_validation(struct sctp_inpcb *inp,
7000 struct sctp_tcb *stcb,
7001 struct sctp_association *asoc,
7004 struct sctp_nets *net;
7005 /* Validate that a timer is running somewhere */
7006 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7007 if (callout_pending(&net->rxt_timer.timer)) {
7008 /* Here is a timer */
7012 /* Gak, we did not have a timer somewhere */
7014 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
7015 kprintf("Deadlock avoided starting timer on a dest at retran\n");
7018 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
7023 sctp_chunk_output(struct sctp_inpcb *inp,
7024 struct sctp_tcb *stcb,
7027 /* Ok this is the generic chunk service queue.
7028 * we must do the following:
7029 * - See if there are retransmits pending, if so we
7030 * must do these first and return.
7031 * - Service the stream queue that is next,
7032 * moving any message (note I must get a complete
7033 * message i.e. FIRST/MIDDLE and LAST to the out
7034 * queue in one pass) and assigning TSN's
7035 * - Check to see if the cwnd/rwnd allows any output, if
7036 * so we go ahead and fomulate and send the low level
7037 * chunks. Making sure to combine any control in the
7038 * control chunk queue also.
7040 struct sctp_association *asoc;
7041 struct sctp_nets *net;
7042 int error, num_out, tot_out, ret, reason_code, burst_cnt, burst_limit;
7050 sctp_pegs[SCTP_CALLS_TO_CO]++;
7052 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
7053 kprintf("in co - retran count:%d\n", asoc->sent_queue_retran_cnt);
7056 while (asoc->sent_queue_retran_cnt) {
7057 /* Ok, it is retransmission time only, we send out only ONE
7058 * packet with a single call off to the retran code.
7060 ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled);
7062 /* Can't send anymore */
7064 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7065 kprintf("retransmission ret:%d -- full\n", ret);
7069 * now lets push out control by calling med-level
7070 * output once. this assures that we WILL send HB's
7073 sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
7074 &cwnd_full, from_where,
7077 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7078 kprintf("Control send outputs:%d@full\n", num_out);
7081 #ifdef SCTP_AUDITING_ENABLED
7082 sctp_auditing(8, inp, stcb, NULL);
7084 return (sctp_timer_validation(inp, stcb, asoc, ret));
7088 * The count was off.. retran is not happening so do
7089 * the normal retransmission.
7092 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7093 kprintf("Done with retrans, none left fill up window\n");
7096 #ifdef SCTP_AUDITING_ENABLED
7097 sctp_auditing(9, inp, stcb, NULL);
7101 if (from_where == 1) {
7102 /* Only one transmission allowed out of a timeout */
7104 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7105 kprintf("Only one packet allowed out\n");
7108 #ifdef SCTP_AUDITING_ENABLED
7109 sctp_auditing(10, inp, stcb, NULL);
7111 /* Push out any control */
7112 sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, &cwnd_full, from_where,
7116 if ((num_out == 0) && (ret == 0)) {
7117 /* No more retrans to send */
7121 #ifdef SCTP_AUDITING_ENABLED
7122 sctp_auditing(12, inp, stcb, NULL);
7124 /* Check for bad destinations, if they exist move chunks around. */
7125 burst_limit = asoc->max_burst;
7126 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7127 if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) ==
7128 SCTP_ADDR_NOT_REACHABLE) {
7130 * if possible move things off of this address
7131 * we still may send below due to the dormant state
7132 * but we try to find an alternate address to send
7133 * to and if we have one we move all queued data on
7134 * the out wheel to this alternate address.
7136 sctp_move_to_an_alt(stcb, asoc, net);
7139 if ((asoc->sat_network) || (net->addr_is_local)) {
7140 burst_limit = asoc->max_burst * SCTP_SAT_NETWORK_BURST_INCR;
7144 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
7145 kprintf("examined net:%p burst limit:%d\n", net, asoc->max_burst);
7149 #ifdef SCTP_USE_ALLMAN_BURST
7150 if ((net->flight_size+(burst_limit*net->mtu)) < net->cwnd) {
7151 if (net->ssthresh < net->cwnd)
7152 net->ssthresh = net->cwnd;
7153 net->cwnd = (net->flight_size+(burst_limit*net->mtu));
7154 #ifdef SCTP_LOG_MAXBURST
7155 sctp_log_maxburst(net, 0, burst_limit, SCTP_MAX_BURST_APPLIED);
7157 sctp_pegs[SCTP_MAX_BURST_APL]++;
7159 net->fast_retran_ip = 0;
7164 /* Fill up what we can to the destination */
7169 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
7170 kprintf("Burst count:%d - call m-c-o\n", burst_cnt);
7173 error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
7174 &reason_code, 0, &cwnd_full, from_where,
7178 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7179 kprintf("Error %d was returned from med-c-op\n", error);
7182 #ifdef SCTP_LOG_MAXBURST
7183 sctp_log_maxburst(asoc->primary_destination, error , burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
7188 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
7189 kprintf("m-c-o put out %d\n", num_out);
7195 #ifndef SCTP_USE_ALLMAN_BURST
7196 && (burst_cnt < burst_limit)
7199 #ifndef SCTP_USE_ALLMAN_BURST
7200 if (burst_cnt >= burst_limit) {
7201 sctp_pegs[SCTP_MAX_BURST_APL]++;
7202 asoc->burst_limit_applied = 1;
7203 #ifdef SCTP_LOG_MAXBURST
7204 sctp_log_maxburst(asoc->primary_destination, 0 , burst_cnt, SCTP_MAX_BURST_APPLIED);
7207 asoc->burst_limit_applied = 0;
7212 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7213 kprintf("Ok, we have put out %d chunks\n", tot_out);
7217 sctp_pegs[SCTP_CO_NODATASNT]++;
7218 if (asoc->stream_queue_cnt > 0) {
7219 sctp_pegs[SCTP_SOS_NOSNT]++;
7221 sctp_pegs[SCTP_NOS_NOSNT]++;
7223 if (asoc->send_queue_cnt > 0) {
7224 sctp_pegs[SCTP_SOSE_NOSNT]++;
7226 sctp_pegs[SCTP_NOSE_NOSNT]++;
7229 /* Now we need to clean up the control chunk chain if
7230 * a ECNE is on it. It must be marked as UNSENT again
7231 * so next call will continue to send it until
7232 * such time that we get a CWR, to remove it.
7234 sctp_fix_ecn_echo(asoc);
7240 sctp_output(struct sctp_inpcb *inp, struct mbuf *m, struct sockaddr *addr,
7241 struct mbuf *control, struct thread *p, int flags)
7243 struct inpcb *ip_inp;
7244 struct sctp_inpcb *t_inp;
7245 struct sctp_tcb *stcb;
7246 struct sctp_nets *net;
7247 struct sctp_association *asoc;
7248 int create_lock_applied = 0;
7249 int queue_only, error = 0;
7250 struct sctp_sndrcvinfo srcv;
7252 int use_rcvinfo = 0;
7254 /* struct route ro;*/
7258 ip_inp = (struct inpcb *)inp;
7264 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7265 kprintf("USR Send BEGINS\n");
7269 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
7270 (inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING)) {
7271 /* The listner can NOT send */
7273 sctppcbinfo.mbuf_track--;
7274 sctp_m_freem(control);
7281 /* Can't allow a V6 address on a non-v6 socket */
7283 SCTP_ASOC_CREATE_LOCK(inp);
7284 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
7285 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
7286 /* Should I really unlock ? */
7287 SCTP_ASOC_CREATE_UNLOCK(inp);
7289 sctppcbinfo.mbuf_track--;
7290 sctp_m_freem(control);
7297 create_lock_applied = 1;
7298 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
7299 (addr->sa_family == AF_INET6)) {
7300 SCTP_ASOC_CREATE_UNLOCK(inp);
7302 sctppcbinfo.mbuf_track--;
7303 sctp_m_freem(control);
7312 sctppcbinfo.mbuf_track++;
7313 if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&srcv, control,
7315 if (srcv.sinfo_flags & MSG_SENDALL) {
7317 sctppcbinfo.mbuf_track--;
7318 sctp_m_freem(control);
7320 if (create_lock_applied) {
7321 SCTP_ASOC_CREATE_UNLOCK(inp);
7322 create_lock_applied = 0;
7324 return (sctp_sendall(inp, NULL, m, &srcv));
7326 if (srcv.sinfo_assoc_id) {
7327 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
7328 SCTP_INP_RLOCK(inp);
7329 stcb = LIST_FIRST(&inp->sctp_asoc_list);
7331 SCTP_TCB_LOCK(stcb);
7332 SCTP_INP_RUNLOCK(inp);
7335 if (create_lock_applied) {
7336 SCTP_ASOC_CREATE_UNLOCK(inp);
7337 create_lock_applied = 0;
7339 sctppcbinfo.mbuf_track--;
7340 sctp_m_freem(control);
7345 net = stcb->asoc.primary_destination;
7347 stcb = sctp_findassociation_ep_asocid(inp, srcv.sinfo_assoc_id);
7350 * Question: Should I error here if the
7352 * assoc_id is no longer valid?
7353 * i.e. I can't find it?
7357 /* Must locate the net structure */
7359 net = sctp_findnet(stcb, addr);
7362 net = stcb->asoc.primary_destination;
7368 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
7369 SCTP_INP_RLOCK(inp);
7370 stcb = LIST_FIRST(&inp->sctp_asoc_list);
7372 SCTP_TCB_LOCK(stcb);
7373 SCTP_INP_RUNLOCK(inp);
7376 if (create_lock_applied) {
7377 SCTP_ASOC_CREATE_UNLOCK(inp);
7378 create_lock_applied = 0;
7381 sctppcbinfo.mbuf_track--;
7382 sctp_m_freem(control);
7389 net = stcb->asoc.primary_destination;
7391 net = sctp_findnet(stcb, addr);
7393 net = stcb->asoc.primary_destination;
7398 SCTP_INP_WLOCK(inp);
7399 SCTP_INP_INCR_REF(inp);
7400 SCTP_INP_WUNLOCK(inp);
7401 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
7403 SCTP_INP_WLOCK(inp);
7404 SCTP_INP_DECR_REF(inp);
7405 SCTP_INP_WUNLOCK(inp);
7410 if ((stcb == NULL) &&
7411 (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) {
7413 sctppcbinfo.mbuf_track--;
7414 sctp_m_freem(control);
7417 if (create_lock_applied) {
7418 SCTP_ASOC_CREATE_UNLOCK(inp);
7419 create_lock_applied = 0;
7424 } else if ((stcb == NULL) &&
7427 sctppcbinfo.mbuf_track--;
7428 sctp_m_freem(control);
7431 if (create_lock_applied) {
7432 SCTP_ASOC_CREATE_UNLOCK(inp);
7433 create_lock_applied = 0;
7438 } else if (stcb == NULL) {
7439 /* UDP mode, we must go ahead and start the INIT process */
7440 if ((use_rcvinfo) && (srcv.sinfo_flags & MSG_ABORT)) {
7441 /* Strange user to do this */
7443 sctppcbinfo.mbuf_track--;
7444 sctp_m_freem(control);
7447 if (create_lock_applied) {
7448 SCTP_ASOC_CREATE_UNLOCK(inp);
7449 create_lock_applied = 0;
7455 stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0);
7458 sctppcbinfo.mbuf_track--;
7459 sctp_m_freem(control);
7462 if (create_lock_applied) {
7463 SCTP_ASOC_CREATE_UNLOCK(inp);
7464 create_lock_applied = 0;
7470 if (create_lock_applied) {
7471 SCTP_ASOC_CREATE_UNLOCK(inp);
7472 create_lock_applied = 0;
7474 kprintf("Huh-1, create lock should have been applied!\n");
7478 asoc->state = SCTP_STATE_COOKIE_WAIT;
7479 SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
7481 /* see if a init structure exists in cmsg headers */
7482 struct sctp_initmsg initm;
7484 if (sctp_find_cmsg(SCTP_INIT, (void *)&initm, control,
7486 /* we have an INIT override of the default */
7487 if (initm.sinit_max_attempts)
7488 asoc->max_init_times = initm.sinit_max_attempts;
7489 if (initm.sinit_num_ostreams)
7490 asoc->pre_open_streams = initm.sinit_num_ostreams;
7491 if (initm.sinit_max_instreams)
7492 asoc->max_inbound_streams = initm.sinit_max_instreams;
7493 if (initm.sinit_max_init_timeo)
7494 asoc->initial_init_rto_max = initm.sinit_max_init_timeo;
7496 if (asoc->streamoutcnt < asoc->pre_open_streams) {
7497 /* Default is NOT correct */
7499 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7500 kprintf("Ok, defout:%d pre_open:%d\n",
7501 asoc->streamoutcnt, asoc->pre_open_streams);
7504 kfree(asoc->strmout, M_PCB);
7505 asoc->strmout = NULL;
7506 asoc->streamoutcnt = asoc->pre_open_streams;
7507 asoc->strmout = kmalloc(asoc->streamoutcnt * sizeof(struct sctp_stream_out),
7509 for (i = 0; i < asoc->streamoutcnt; i++) {
7511 * inbound side must be set to 0xffff,
7512 * also NOTE when we get the INIT-ACK
7513 * back (for INIT sender) we MUST
7514 * reduce the count (streamoutcnt) but
7515 * first check if we sent to any of the
7516 * upper streams that were dropped (if
7517 * some were). Those that were dropped
7518 * must be notified to the upper layer
7519 * as failed to send.
7521 asoc->strmout[i].next_sequence_sent = 0x0;
7522 TAILQ_INIT(&asoc->strmout[i].outqueue);
7523 asoc->strmout[i].stream_no = i;
7524 asoc->strmout[i].next_spoke.tqe_next = 0;
7525 asoc->strmout[i].next_spoke.tqe_prev = 0;
7529 sctp_send_initiate(inp, stcb);
7531 * we may want to dig in after this call and adjust the MTU
7532 * value. It defaulted to 1500 (constant) but the ro structure
7533 * may now have an update and thus we may need to change it
7534 * BEFORE we append the message.
7536 net = stcb->asoc.primary_destination;
7538 if (create_lock_applied) {
7539 SCTP_ASOC_CREATE_UNLOCK(inp);
7540 create_lock_applied = 0;
7543 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
7544 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
7547 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
7548 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
7549 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
7550 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
7552 sctppcbinfo.mbuf_track--;
7553 sctp_m_freem(control);
7556 if ((use_rcvinfo) &&
7557 (srcv.sinfo_flags & MSG_ABORT)) {
7558 sctp_msg_append(stcb, net, m, &srcv, flags);
7566 SCTP_TCB_UNLOCK(stcb);
7570 if (create_lock_applied) {
7571 /* we should never hit here with the create lock applied
7574 SCTP_ASOC_CREATE_UNLOCK(inp);
7575 create_lock_applied = 0;
7579 if (use_rcvinfo == 0) {
7580 srcv = stcb->asoc.def_send;
7584 if (sctp_debug_on & SCTP_DEBUG_OUTPUT5) {
7585 kprintf("stream:%d\n", srcv.sinfo_stream);
7586 kprintf("flags:%x\n", (u_int)srcv.sinfo_flags);
7587 kprintf("ppid:%d\n", srcv.sinfo_ppid);
7588 kprintf("context:%d\n", srcv.sinfo_context);
7593 sctppcbinfo.mbuf_track--;
7594 sctp_m_freem(control);
7597 if (net && ((srcv.sinfo_flags & MSG_ADDR_OVER))) {
7598 /* we take the override or the unconfirmed */
7601 net = stcb->asoc.primary_destination;
7603 if ((error = sctp_msg_append(stcb, net, m, &srcv, flags))) {
7604 SCTP_TCB_UNLOCK(stcb);
7608 if (net->flight_size > net->cwnd) {
7609 sctp_pegs[SCTP_SENDTO_FULL_CWND]++;
7611 } else if (asoc->ifp_had_enobuf) {
7612 sctp_pegs[SCTP_QUEONLY_BURSTLMT]++;
7615 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
7616 ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) * sizeof(struct sctp_data_chunk)) +
7619 if (((inp->sctp_flags & SCTP_PCB_FLAGS_NODELAY) == 0) &&
7620 (stcb->asoc.total_flight > 0) &&
7621 (un_sent < (int)stcb->asoc.smallest_mtu)
7624 /* Ok, Nagle is set on and we have
7625 * data outstanding. Don't send anything
7626 * and let the SACK drive out the data.
7628 sctp_pegs[SCTP_NAGLE_NOQ]++;
7631 sctp_pegs[SCTP_NAGLE_OFF]++;
7634 if ((queue_only == 0) && stcb->asoc.peers_rwnd) {
7635 /* we can attempt to send too.*/
7637 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7638 kprintf("USR Send calls sctp_chunk_output\n");
7641 #ifdef SCTP_AUDITING_ENABLED
7642 sctp_audit_log(0xC0, 1);
7643 sctp_auditing(6, inp, stcb, net);
7645 sctp_pegs[SCTP_OUTPUT_FRM_SND]++;
7646 sctp_chunk_output(inp, stcb, 0);
7647 #ifdef SCTP_AUDITING_ENABLED
7648 sctp_audit_log(0xC0, 2);
7649 sctp_auditing(7, inp, stcb, net);
7654 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7655 kprintf("USR Send complete qo:%d prw:%d\n", queue_only, stcb->asoc.peers_rwnd);
7658 SCTP_TCB_UNLOCK(stcb);
7664 send_forward_tsn(struct sctp_tcb *stcb,
7665 struct sctp_association *asoc)
7667 struct sctp_tmit_chunk *chk;
7668 struct sctp_forward_tsn_chunk *fwdtsn;
7670 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7671 if (chk->rec.chunk_id == SCTP_FORWARD_CUM_TSN) {
7672 /* mark it to unsent */
7673 chk->sent = SCTP_DATAGRAM_UNSENT;
7675 /* Do we correct its output location? */
7676 if (chk->whoTo != asoc->primary_destination) {
7677 sctp_free_remote_addr(chk->whoTo);
7678 chk->whoTo = asoc->primary_destination;
7679 chk->whoTo->ref_count++;
7681 goto sctp_fill_in_rest;
7684 /* Ok if we reach here we must build one */
7685 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
7689 sctppcbinfo.ipi_count_chunk++;
7690 sctppcbinfo.ipi_gencnt_chunk++;
7691 chk->rec.chunk_id = SCTP_FORWARD_CUM_TSN;
7693 MGETHDR(chk->data, MB_DONTWAIT, MT_DATA);
7694 if (chk->data == NULL) {
7695 chk->whoTo->ref_count--;
7696 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
7697 sctppcbinfo.ipi_count_chunk--;
7698 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
7699 panic("Chunk count is negative");
7701 sctppcbinfo.ipi_gencnt_chunk++;
7704 chk->data->m_data += SCTP_MIN_OVERHEAD;
7705 chk->sent = SCTP_DATAGRAM_UNSENT;
7707 chk->whoTo = asoc->primary_destination;
7708 chk->whoTo->ref_count++;
7709 TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
7710 asoc->ctrl_queue_cnt++;
7712 /* Here we go through and fill out the part that
7713 * deals with stream/seq of the ones we skip.
7715 chk->data->m_pkthdr.len = chk->data->m_len = 0;
7717 struct sctp_tmit_chunk *at, *tp1, *last;
7718 struct sctp_strseq *strseq;
7719 unsigned int cnt_of_space, i, ovh;
7720 unsigned int space_needed;
7721 unsigned int cnt_of_skipped = 0;
7722 TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
7723 if (at->sent != SCTP_FORWARD_TSN_SKIP) {
7724 /* no more to look at */
7727 if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
7728 /* We don't report these */
7733 space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
7734 (cnt_of_skipped * sizeof(struct sctp_strseq)));
7735 if ((M_TRAILINGSPACE(chk->data) < (int)space_needed) &&
7736 ((chk->data->m_flags & M_EXT) == 0)) {
7737 /* Need a M_EXT, get one and move
7738 * fwdtsn to data area.
7740 MCLGET(chk->data, MB_DONTWAIT);
7742 cnt_of_space = M_TRAILINGSPACE(chk->data);
7744 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7745 ovh = SCTP_MIN_OVERHEAD;
7747 ovh = SCTP_MIN_V4_OVERHEAD;
7749 if (cnt_of_space > (asoc->smallest_mtu-ovh)) {
7750 /* trim to a mtu size */
7751 cnt_of_space = asoc->smallest_mtu - ovh;
7753 if (cnt_of_space < space_needed) {
7754 /* ok we must trim down the chunk by lowering
7755 * the advance peer ack point.
7757 cnt_of_skipped = (cnt_of_space-
7758 ((sizeof(struct sctp_forward_tsn_chunk))/
7759 sizeof(struct sctp_strseq)));
7760 /* Go through and find the TSN that
7761 * will be the one we report.
7763 at = TAILQ_FIRST(&asoc->sent_queue);
7764 for (i = 0; i < cnt_of_skipped; i++) {
7765 tp1 = TAILQ_NEXT(at, sctp_next);
7769 /* last now points to last one I can report, update peer ack point */
7770 asoc->advanced_peer_ack_point = last->rec.data.TSN_seq;
7771 space_needed -= (cnt_of_skipped * sizeof(struct sctp_strseq));
7773 chk->send_size = space_needed;
7774 /* Setup the chunk */
7775 fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
7776 fwdtsn->ch.chunk_length = htons(chk->send_size);
7777 fwdtsn->ch.chunk_flags = 0;
7778 fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
7779 fwdtsn->new_cumulative_tsn = htonl(asoc->advanced_peer_ack_point);
7780 chk->send_size = (sizeof(struct sctp_forward_tsn_chunk) +
7781 (cnt_of_skipped * sizeof(struct sctp_strseq)));
7782 chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
7784 /* Move pointer to after the fwdtsn and transfer to
7785 * the strseq pointer.
7787 strseq = (struct sctp_strseq *)fwdtsn;
7789 * Now populate the strseq list. This is done blindly
7790 * without pulling out duplicate stream info. This is
7791 * inefficent but won't harm the process since the peer
7792 * will look at these in sequence and will thus release
7793 * anything. It could mean we exceed the PMTU and chop
7794 * off some that we could have included.. but this is
7795 * unlikely (aka 1432/4 would mean 300+ stream seq's would
7796 * have to be reported in one FWD-TSN. With a bit of work
7797 * we can later FIX this to optimize and pull out duplcates..
7798 * but it does add more overhead. So for now... not!
7800 at = TAILQ_FIRST(&asoc->sent_queue);
7801 for (i = 0; i < cnt_of_skipped; i++) {
7802 tp1 = TAILQ_NEXT(at, sctp_next);
7803 if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
7804 /* We don't report these */
7809 strseq->stream = ntohs(at->rec.data.stream_number);
7810 strseq->sequence = ntohs(at->rec.data.stream_seq);
7820 sctp_send_sack(struct sctp_tcb *stcb)
7823 * Queue up a SACK in the control queue. We must first check to
7824 * see if a SACK is somehow on the control queue. If so, we will
7825 * take and and remove the old one.
7827 struct sctp_association *asoc;
7828 struct sctp_tmit_chunk *chk, *a_chk;
7829 struct sctp_sack_chunk *sack;
7830 struct sctp_gap_ack_block *gap_descriptor;
7833 unsigned int i, maxi, seeing_ones, m_size;
7834 unsigned int num_gap_blocks, space;
7840 if (asoc->last_data_chunk_from == NULL) {
7841 /* Hmm we never received anything */
7844 sctp_set_rwnd(stcb, asoc);
7845 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7846 if (chk->rec.chunk_id == SCTP_SELECTIVE_ACK) {
7847 /* Hmm, found a sack already on queue, remove it */
7848 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
7849 asoc->ctrl_queue_cnt++;
7852 sctp_m_freem(a_chk->data);
7854 sctp_free_remote_addr(a_chk->whoTo);
7855 a_chk->whoTo = NULL;
7859 if (a_chk == NULL) {
7860 a_chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
7861 if (a_chk == NULL) {
7862 /* No memory so we drop the idea, and set a timer */
7863 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
7864 stcb->sctp_ep, stcb, NULL);
7865 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
7866 stcb->sctp_ep, stcb, NULL);
7869 sctppcbinfo.ipi_count_chunk++;
7870 sctppcbinfo.ipi_gencnt_chunk++;
7871 a_chk->rec.chunk_id = SCTP_SELECTIVE_ACK;
7874 a_chk->snd_count = 0;
7875 a_chk->send_size = 0; /* fill in later */
7876 a_chk->sent = SCTP_DATAGRAM_UNSENT;
7877 m_size = (asoc->mapping_array_size << 3);
7879 if ((asoc->numduptsns) ||
7880 (asoc->last_data_chunk_from->dest_state & SCTP_ADDR_NOT_REACHABLE)
7882 /* Ok, we have some duplicates or the destination for the
7883 * sack is unreachable, lets see if we can select an alternate
7884 * than asoc->last_data_chunk_from
7886 if ((!(asoc->last_data_chunk_from->dest_state &
7887 SCTP_ADDR_NOT_REACHABLE)) &&
7888 (asoc->used_alt_onsack > 2)) {
7889 /* We used an alt last time, don't this time */
7890 a_chk->whoTo = NULL;
7892 asoc->used_alt_onsack++;
7893 a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from);
7895 if (a_chk->whoTo == NULL) {
7896 /* Nope, no alternate */
7897 a_chk->whoTo = asoc->last_data_chunk_from;
7898 asoc->used_alt_onsack = 0;
7901 /* No duplicates so we use the last
7902 * place we received data from.
7905 if (asoc->last_data_chunk_from == NULL) {
7906 kprintf("Huh, last_data_chunk_from is null when we want to sack??\n");
7909 asoc->used_alt_onsack = 0;
7910 a_chk->whoTo = asoc->last_data_chunk_from;
7913 a_chk->whoTo->ref_count++;
7915 /* Ok now lets formulate a MBUF with our sack */
7916 MGETHDR(a_chk->data, MB_DONTWAIT, MT_DATA);
7917 if ((a_chk->data == NULL) ||
7918 (a_chk->whoTo == NULL)) {
7919 /* rats, no mbuf memory */
7921 /* was a problem with the destination */
7922 sctp_m_freem(a_chk->data);
7925 a_chk->whoTo->ref_count--;
7926 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, a_chk);
7927 sctppcbinfo.ipi_count_chunk--;
7928 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
7929 panic("Chunk count is negative");
7931 sctppcbinfo.ipi_gencnt_chunk++;
7932 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
7933 stcb->sctp_ep, stcb, NULL);
7934 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
7935 stcb->sctp_ep, stcb, NULL);
7938 /* First count the number of gap ack blocks we need */
7939 if (asoc->highest_tsn_inside_map == asoc->cumulative_tsn) {
7940 /* We know if there are none above the cum-ack we
7941 * have everything with NO gaps
7945 /* Ok we must count how many gaps we
7949 if (asoc->highest_tsn_inside_map >= asoc->mapping_array_base_tsn) {
7950 maxi = (asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn);
7952 maxi = (asoc->highest_tsn_inside_map + (MAX_TSN - asoc->mapping_array_base_tsn) + 1);
7954 if (maxi > m_size) {
7955 /* impossible but who knows, someone is playing with us :> */
7957 kprintf("GAK maxi:%d > m_size:%d came out higher than allowed htsn:%u base:%u cumack:%u\n",
7960 asoc->highest_tsn_inside_map,
7961 asoc->mapping_array_base_tsn,
7962 asoc->cumulative_tsn
7968 if (asoc->cumulative_tsn >= asoc->mapping_array_base_tsn) {
7969 start = (asoc->cumulative_tsn - asoc->mapping_array_base_tsn);
7971 /* Set it so we start at 0 */
7974 /* Ok move start up one to look at the NEXT past the cum-ack */
7976 for (i = start; i <= maxi; i++) {
7978 /* while seeing ones I must
7979 * transition back to 0 before
7980 * finding the next gap and
7981 * counting the segment.
7983 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, i) == 0) {
7987 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, i)) {
7994 if (num_gap_blocks == 0) {
7996 * Traveled all of the bits and NO one,
7999 if (compare_with_wrap(asoc->cumulative_tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
8000 asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
8001 #ifdef SCTP_MAP_LOGGING
8002 sctp_log_map(0, 4, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
8008 /* Now calculate the space needed */
8009 space = (sizeof(struct sctp_sack_chunk) +
8010 (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
8011 (asoc->numduptsns * sizeof(int32_t))
8013 if (space > (asoc->smallest_mtu-SCTP_MAX_OVERHEAD)) {
8014 /* Reduce the size of the sack to fit */
8016 calc = (asoc->smallest_mtu - SCTP_MAX_OVERHEAD);
8017 calc -= sizeof(struct sctp_gap_ack_block);
8018 fit = calc/sizeof(struct sctp_gap_ack_block);
8019 if (fit > (int)num_gap_blocks) {
8020 /* discard some dups */
8021 asoc->numduptsns = (fit - num_gap_blocks);
8023 /* discard all dups and some gaps */
8024 num_gap_blocks = fit;
8025 asoc->numduptsns = 0;
8028 space = (sizeof(struct sctp_sack_chunk) +
8029 (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
8030 (asoc->numduptsns * sizeof(int32_t))
8035 if ((space+SCTP_MIN_OVERHEAD) > MHLEN) {
8036 /* We need a cluster */
8037 MCLGET(a_chk->data, MB_DONTWAIT);
8038 if ((a_chk->data->m_flags & M_EXT) != M_EXT) {
8039 /* can't get a cluster
8040 * give up and try later.
8043 sctp_m_freem(a_chk->data);
8045 a_chk->whoTo->ref_count--;
8046 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, a_chk);
8047 sctppcbinfo.ipi_count_chunk--;
8048 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
8049 panic("Chunk count is negative");
8051 sctppcbinfo.ipi_gencnt_chunk++;
8052 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
8053 stcb->sctp_ep, stcb, NULL);
8054 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
8055 stcb->sctp_ep, stcb, NULL);
8060 /* ok, lets go through and fill it in */
8061 a_chk->data->m_data += SCTP_MIN_OVERHEAD;
8062 sack = mtod(a_chk->data, struct sctp_sack_chunk *);
8063 sack->ch.chunk_type = SCTP_SELECTIVE_ACK;
8064 sack->ch.chunk_flags = asoc->receiver_nonce_sum & SCTP_SACK_NONCE_SUM;
8065 sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
8066 sack->sack.a_rwnd = htonl(asoc->my_rwnd);
8067 asoc->my_last_reported_rwnd = asoc->my_rwnd;
8068 sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
8069 sack->sack.num_dup_tsns = htons(asoc->numduptsns);
8071 a_chk->send_size = (sizeof(struct sctp_sack_chunk) +
8072 (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
8073 (asoc->numduptsns * sizeof(int32_t)));
8074 a_chk->data->m_pkthdr.len = a_chk->data->m_len = a_chk->send_size;
8075 sack->ch.chunk_length = htons(a_chk->send_size);
8077 gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)sack + sizeof(struct sctp_sack_chunk));
8079 for (i = start; i <= maxi; i++) {
8080 if (num_gap_blocks == 0) {
8084 /* while seeing Ones I must
8085 * transition back to 0 before
8086 * finding the next gap
8088 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, i) == 0) {
8089 gap_descriptor->end = htons(((uint16_t)(i-start)));
8095 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, i)) {
8096 gap_descriptor->start = htons(((uint16_t)(i+1-start)));
8097 /* advance struct to next pointer */
8102 if (num_gap_blocks) {
8103 /* special case where the array is all 1's
8104 * to the end of the array.
8106 gap_descriptor->end = htons(((uint16_t)((i-start))));
8109 /* now we must add any dups we are going to report. */
8110 if (asoc->numduptsns) {
8111 dup = (uint32_t *)gap_descriptor;
8112 for (i = 0; i < asoc->numduptsns; i++) {
8113 *dup = htonl(asoc->dup_tsns[i]);
8116 asoc->numduptsns = 0;
8118 /* now that the chunk is prepared queue it to the control
8121 TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
8122 asoc->ctrl_queue_cnt++;
8123 sctp_pegs[SCTP_PEG_SACKS_SENT]++;
8128 sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr)
8130 struct mbuf *m_abort;
8131 struct sctp_abort_msg *abort_m;
8134 MGETHDR(m_abort, MB_DONTWAIT, MT_HEADER);
8135 if (m_abort == NULL) {
8139 m_abort->m_data += SCTP_MIN_OVERHEAD;
8140 abort_m = mtod(m_abort, struct sctp_abort_msg *);
8141 m_abort->m_len = sizeof(struct sctp_abort_msg);
8142 m_abort->m_next = operr;
8152 abort_m->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
8153 abort_m->msg.ch.chunk_flags = 0;
8154 abort_m->msg.ch.chunk_length = htons(sizeof(struct sctp_abort_chunk) +
8156 abort_m->sh.src_port = stcb->sctp_ep->sctp_lport;
8157 abort_m->sh.dest_port = stcb->rport;
8158 abort_m->sh.v_tag = htonl(stcb->asoc.peer_vtag);
8159 abort_m->sh.checksum = 0;
8160 m_abort->m_pkthdr.len = m_abort->m_len + sz;
8161 m_abort->m_pkthdr.rcvif = 0;
8162 sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb,
8163 stcb->asoc.primary_destination,
8164 (struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr,
8165 m_abort, 1, 0, NULL, 0);
8169 sctp_send_shutdown_complete(struct sctp_tcb *stcb,
8170 struct sctp_nets *net)
8173 /* formulate and SEND a SHUTDOWN-COMPLETE */
8174 struct mbuf *m_shutdown_comp;
8175 struct sctp_shutdown_complete_msg *comp_cp;
8177 m_shutdown_comp = NULL;
8178 MGETHDR(m_shutdown_comp, MB_DONTWAIT, MT_HEADER);
8179 if (m_shutdown_comp == NULL) {
8183 m_shutdown_comp->m_data += sizeof(struct ip6_hdr);
8184 comp_cp = mtod(m_shutdown_comp, struct sctp_shutdown_complete_msg *);
8185 comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
8186 comp_cp->shut_cmp.ch.chunk_flags = 0;
8187 comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
8188 comp_cp->sh.src_port = stcb->sctp_ep->sctp_lport;
8189 comp_cp->sh.dest_port = stcb->rport;
8190 comp_cp->sh.v_tag = htonl(stcb->asoc.peer_vtag);
8191 comp_cp->sh.checksum = 0;
8193 m_shutdown_comp->m_pkthdr.len = m_shutdown_comp->m_len = sizeof(struct sctp_shutdown_complete_msg);
8194 m_shutdown_comp->m_pkthdr.rcvif = 0;
8195 sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
8196 (struct sockaddr *)&net->ro._l_addr, m_shutdown_comp,
8198 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
8199 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
8200 stcb->sctp_ep->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
8201 stcb->sctp_ep->sctp_socket->so_snd.ssb_cc = 0;
8202 soisdisconnected(stcb->sctp_ep->sctp_socket);
8208 sctp_send_shutdown_complete2(struct mbuf *m, int iphlen, struct sctphdr *sh)
8210 /* formulate and SEND a SHUTDOWN-COMPLETE */
8212 struct ip *iph, *iph_out;
8213 struct ip6_hdr *ip6, *ip6_out;
8215 struct sctp_shutdown_complete_msg *comp_cp;
8217 MGETHDR(mout, MB_DONTWAIT, MT_HEADER);
8222 iph = mtod(m, struct ip *);
8226 if (iph->ip_v == IPVERSION) {
8227 mout->m_len = sizeof(struct ip) +
8228 sizeof(struct sctp_shutdown_complete_msg);
8229 mout->m_next = NULL;
8230 iph_out = mtod(mout, struct ip *);
8232 /* Fill in the IP header for the ABORT */
8233 iph_out->ip_v = IPVERSION;
8234 iph_out->ip_hl = (sizeof(struct ip)/4);
8235 iph_out->ip_tos = (u_char)0;
8237 iph_out->ip_off = 0;
8238 iph_out->ip_ttl = MAXTTL;
8239 iph_out->ip_p = IPPROTO_SCTP;
8240 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
8241 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
8243 /* let IP layer calculate this */
8244 iph_out->ip_sum = 0;
8245 offset_out += sizeof(*iph_out);
8246 comp_cp = (struct sctp_shutdown_complete_msg *)(
8247 (caddr_t)iph_out + offset_out);
8248 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
8249 ip6 = (struct ip6_hdr *)iph;
8250 mout->m_len = sizeof(struct ip6_hdr) +
8251 sizeof(struct sctp_shutdown_complete_msg);
8252 mout->m_next = NULL;
8253 ip6_out = mtod(mout, struct ip6_hdr *);
8255 /* Fill in the IPv6 header for the ABORT */
8256 ip6_out->ip6_flow = ip6->ip6_flow;
8257 ip6_out->ip6_hlim = ip6_defhlim;
8258 ip6_out->ip6_nxt = IPPROTO_SCTP;
8259 ip6_out->ip6_src = ip6->ip6_dst;
8260 ip6_out->ip6_dst = ip6->ip6_src;
8261 ip6_out->ip6_plen = mout->m_len;
8262 offset_out += sizeof(*ip6_out);
8263 comp_cp = (struct sctp_shutdown_complete_msg *)(
8264 (caddr_t)ip6_out + offset_out);
8266 /* Currently not supported. */
8270 /* Now copy in and fill in the ABORT tags etc. */
8271 comp_cp->sh.src_port = sh->dest_port;
8272 comp_cp->sh.dest_port = sh->src_port;
8273 comp_cp->sh.checksum = 0;
8274 comp_cp->sh.v_tag = sh->v_tag;
8275 comp_cp->shut_cmp.ch.chunk_flags = SCTP_HAD_NO_TCB;
8276 comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
8277 comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
8279 mout->m_pkthdr.len = mout->m_len;
8281 if ((sctp_no_csum_on_loopback) &&
8282 (m->m_pkthdr.rcvif) &&
8283 (m->m_pkthdr.rcvif->if_type == IFT_LOOP)) {
8284 comp_cp->sh.checksum = 0;
8286 comp_cp->sh.checksum = sctp_calculate_sum(mout, NULL, offset_out);
8289 /* zap the rcvif, it should be null */
8290 mout->m_pkthdr.rcvif = 0;
8291 /* zap the stack pointer to the route */
8292 if (iph_out != NULL) {
8295 bzero(&ro, sizeof ro);
8297 if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
8298 kprintf("sctp_shutdown_complete2 calling ip_output:\n");
8299 sctp_print_address_pkt(iph_out, &comp_cp->sh);
8302 /* set IPv4 length */
8303 #if defined(__FreeBSD__)
8304 iph_out->ip_len = mout->m_pkthdr.len;
8306 iph_out->ip_len = htons(mout->m_pkthdr.len);
8309 ip_output(mout, 0, &ro, IP_RAWOUTPUT, NULL
8310 #if defined(__OpenBSD__) || (defined(__FreeBSD__) && __FreeBSD_version >= 480000) \
8311 || defined(__NetBSD__) || defined(__DragonFly__)
8315 /* Free the route if we got one back */
8318 } else if (ip6_out != NULL) {
8319 #ifdef NEW_STRUCT_ROUTE
8322 struct route_in6 ro;
8325 bzero(&ro, sizeof(ro));
8327 if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
8328 kprintf("sctp_shutdown_complete2 calling ip6_output:\n");
8329 sctp_print_address_pkt((struct ip *)ip6_out,
8333 ip6_output(mout, NULL, &ro, 0, NULL, NULL
8334 #if defined(__NetBSD__)
8337 #if (defined(__FreeBSD__) && __FreeBSD_version >= 480000) || defined(__DragonFly__)
8341 /* Free the route if we got one back */
8345 sctp_pegs[SCTP_DATAGRAMS_SENT]++;
8349 static struct sctp_nets *
8350 sctp_select_hb_destination(struct sctp_tcb *stcb, struct timeval *now)
8352 struct sctp_nets *net, *hnet;
8353 int ms_goneby, highest_ms, state_overide=0;
8355 SCTP_GETTIME_TIMEVAL(now);
8358 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
8360 ((net->dest_state & SCTP_ADDR_NOHB) && ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) ||
8361 (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)
8363 /* Skip this guy from consideration if HB is off AND its confirmed*/
8365 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
8366 kprintf("Skipping net:%p state:%d nohb/out-of-scope\n",
8367 net, net->dest_state);
8372 if (sctp_destination_is_reachable(stcb, (struct sockaddr *)&net->ro._l_addr) == 0) {
8373 /* skip this dest net from consideration */
8375 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
8376 kprintf("Skipping net:%p reachable NOT\n",
8382 if (net->last_sent_time.tv_sec) {
8383 /* Sent to so we subtract */
8384 ms_goneby = (now->tv_sec - net->last_sent_time.tv_sec) * 1000;
8386 /* Never been sent to */
8387 ms_goneby = 0x7fffffff;
8389 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
8390 kprintf("net:%p ms_goneby:%d\n",
8394 /* When the address state is unconfirmed but still considered reachable, we
8395 * HB at a higher rate. Once it goes confirmed OR reaches the "unreachable"
8396 * state, thenw we cut it back to HB at a more normal pace.
8398 if ((net->dest_state & (SCTP_ADDR_UNCONFIRMED|SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED) {
8404 if ((((unsigned int)ms_goneby >= net->RTO) || (state_overide)) &&
8405 (ms_goneby > highest_ms)) {
8406 highest_ms = ms_goneby;
8409 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
8410 kprintf("net:%p is the new high\n",
8417 ((hnet->dest_state & (SCTP_ADDR_UNCONFIRMED|SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED)) {
8423 if (highest_ms && (((unsigned int)highest_ms >= hnet->RTO) || state_overide)) {
8424 /* Found the one with longest delay bounds
8425 * OR it is unconfirmed and still not marked
8429 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
8430 kprintf("net:%p is the hb winner -",
8433 sctp_print_address((struct sockaddr *)&hnet->ro._l_addr);
8438 /* update the timer now */
8439 hnet->last_sent_time = *now;
8447 sctp_send_hb(struct sctp_tcb *stcb, int user_req, struct sctp_nets *u_net)
8449 struct sctp_tmit_chunk *chk;
8450 struct sctp_nets *net;
8451 struct sctp_heartbeat_chunk *hb;
8453 struct sockaddr_in *sin;
8454 struct sockaddr_in6 *sin6;
8456 if (user_req == 0) {
8457 net = sctp_select_hb_destination(stcb, &now);
8459 /* All our busy none to send to, just
8460 * start the timer again.
8462 if (stcb->asoc.state == 0) {
8465 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT,
8471 #ifndef SCTP_USE_ALLMAN_BURST
8473 /* found one idle.. decay cwnd on this one
8474 * by 1/2 if none outstanding.
8477 if (net->flight_size == 0) {
8479 if (net->addr_is_local) {
8480 if (net->cwnd < (net->mtu *4)) {
8481 net->cwnd = net->mtu * 4;
8484 if (net->cwnd < (net->mtu * 2)) {
8485 net->cwnd = net->mtu * 2;
8498 SCTP_GETTIME_TIMEVAL(&now);
8500 sin = (struct sockaddr_in *)&net->ro._l_addr;
8501 if (sin->sin_family != AF_INET) {
8502 if (sin->sin_family != AF_INET6) {
8507 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
8510 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
8511 kprintf("Gak, can't get a chunk for hb\n");
8516 sctppcbinfo.ipi_gencnt_chunk++;
8517 sctppcbinfo.ipi_count_chunk++;
8518 chk->rec.chunk_id = SCTP_HEARTBEAT_REQUEST;
8519 chk->asoc = &stcb->asoc;
8520 chk->send_size = sizeof(struct sctp_heartbeat_chunk);
8521 MGETHDR(chk->data, MB_DONTWAIT, MT_DATA);
8522 if (chk->data == NULL) {
8523 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
8524 sctppcbinfo.ipi_count_chunk--;
8525 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
8526 panic("Chunk count is negative");
8528 sctppcbinfo.ipi_gencnt_chunk++;
8531 chk->data->m_data += SCTP_MIN_OVERHEAD;
8532 chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
8533 chk->sent = SCTP_DATAGRAM_UNSENT;
8536 chk->whoTo->ref_count++;
8537 /* Now we have a mbuf that we can fill in with the details */
8538 hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
8540 /* fill out chunk header */
8541 hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
8542 hb->ch.chunk_flags = 0;
8543 hb->ch.chunk_length = htons(chk->send_size);
8544 /* Fill out hb parameter */
8545 hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
8546 hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
8547 hb->heartbeat.hb_info.time_value_1 = now.tv_sec;
8548 hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
8549 /* Did our user request this one, put it in */
8550 hb->heartbeat.hb_info.user_req = user_req;
8551 hb->heartbeat.hb_info.addr_family = sin->sin_family;
8552 hb->heartbeat.hb_info.addr_len = sin->sin_len;
8553 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
8554 /* we only take from the entropy pool if the address is
8557 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
8558 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
8560 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
8561 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
8563 if (sin->sin_family == AF_INET) {
8564 memcpy(hb->heartbeat.hb_info.address, &sin->sin_addr, sizeof(sin->sin_addr));
8565 } else if (sin->sin_family == AF_INET6) {
8566 /* We leave the scope the way it is in our lookup table. */
8567 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
8568 memcpy(hb->heartbeat.hb_info.address, &sin6->sin6_addr, sizeof(sin6->sin6_addr));
8570 /* huh compiler bug */
8572 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
8573 kprintf("Compiler bug bleeds a mbuf and a chunk\n");
8578 /* ok we have a destination that needs a beat */
8579 /* lets do the theshold management Qiaobing style */
8580 if (user_req == 0) {
8581 if (sctp_threshold_management(stcb->sctp_ep, stcb, net,
8582 stcb->asoc.max_send_times)) {
8583 /* we have lost the association, in a way this
8584 * is quite bad since we really are one less time
8585 * since we really did not send yet. This is the
8586 * down side to the Q's style as defined in the RFC
8587 * and not my alternate style defined in the RFC.
8589 if (chk->data != NULL) {
8590 sctp_m_freem(chk->data);
8593 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
8594 sctppcbinfo.ipi_count_chunk--;
8595 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
8596 panic("Chunk count is negative");
8598 sctppcbinfo.ipi_gencnt_chunk++;
8602 net->hb_responded = 0;
8604 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
8605 kprintf("Inserting chunk for HB\n");
8608 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
8609 stcb->asoc.ctrl_queue_cnt++;
8610 sctp_pegs[SCTP_HB_SENT]++;
8612 * Call directly med level routine to put out the chunk. It will
8613 * always tumble out control chunks aka HB but it may even tumble
8616 if (user_req == 0) {
8617 /* Ok now lets start the HB timer if it is NOT a user req */
8618 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
8625 sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
8628 struct sctp_association *asoc;
8629 struct sctp_ecne_chunk *ecne;
8630 struct sctp_tmit_chunk *chk;
8632 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
8633 if (chk->rec.chunk_id == SCTP_ECN_ECHO) {
8634 /* found a previous ECN_ECHO update it if needed */
8635 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
8636 ecne->tsn = htonl(high_tsn);
8640 /* nope could not find one to update so we must build one */
8641 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
8645 sctp_pegs[SCTP_ECNE_SENT]++;
8646 sctppcbinfo.ipi_count_chunk++;
8647 sctppcbinfo.ipi_gencnt_chunk++;
8648 chk->rec.chunk_id = SCTP_ECN_ECHO;
8649 chk->asoc = &stcb->asoc;
8650 chk->send_size = sizeof(struct sctp_ecne_chunk);
8651 MGETHDR(chk->data, MB_DONTWAIT, MT_DATA);
8652 if (chk->data == NULL) {
8653 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
8654 sctppcbinfo.ipi_count_chunk--;
8655 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
8656 panic("Chunk count is negative");
8658 sctppcbinfo.ipi_gencnt_chunk++;
8661 chk->data->m_data += SCTP_MIN_OVERHEAD;
8662 chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
8663 chk->sent = SCTP_DATAGRAM_UNSENT;
8666 chk->whoTo->ref_count++;
8667 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
8668 ecne->ch.chunk_type = SCTP_ECN_ECHO;
8669 ecne->ch.chunk_flags = 0;
8670 ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
8671 ecne->tsn = htonl(high_tsn);
8672 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
8673 asoc->ctrl_queue_cnt++;
8677 sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
8678 struct mbuf *m, int iphlen, int bad_crc)
8680 struct sctp_association *asoc;
8681 struct sctp_pktdrop_chunk *drp;
8682 struct sctp_tmit_chunk *chk;
8685 unsigned int small_one;
8690 if (asoc->peer_supports_pktdrop == 0) {
8691 /* peer must declare support before I
8696 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
8700 sctppcbinfo.ipi_count_chunk++;
8701 sctppcbinfo.ipi_gencnt_chunk++;
8703 iph = mtod(m, struct ip *);
8707 if (iph->ip_v == IPVERSION) {
8709 #if defined(__FreeBSD__)
8710 len = chk->send_size = iph->ip_len;
8712 len = chk->send_size = (iph->ip_len - iphlen);
8715 struct ip6_hdr *ip6h;
8717 ip6h = mtod(m, struct ip6_hdr *);
8718 len = chk->send_size = htons(ip6h->ip6_plen);
8720 if ((len+iphlen) > m->m_pkthdr.len) {
8722 chk->send_size = len = m->m_pkthdr.len - iphlen;
8724 chk->asoc = &stcb->asoc;
8725 MGETHDR(chk->data, MB_DONTWAIT, MT_DATA);
8726 if (chk->data == NULL) {
8728 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
8729 sctppcbinfo.ipi_count_chunk--;
8730 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
8731 panic("Chunk count is negative");
8733 sctppcbinfo.ipi_gencnt_chunk++;
8736 if ((chk->send_size+sizeof(struct sctp_pktdrop_chunk)+SCTP_MIN_OVERHEAD) > MHLEN) {
8737 MCLGET(chk->data, MB_DONTWAIT);
8738 if ((chk->data->m_flags & M_EXT) == 0) {
8740 sctp_m_freem(chk->data);
8745 chk->data->m_data += SCTP_MIN_OVERHEAD;
8746 drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
8748 sctp_m_freem(chk->data);
8752 small_one = asoc->smallest_mtu;
8753 if (small_one > MCLBYTES) {
8754 /* Only one cluster worth of data MAX */
8755 small_one = MCLBYTES;
8757 chk->book_size = (chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
8758 sizeof(struct sctphdr) + SCTP_MED_OVERHEAD);
8759 if (chk->book_size > small_one) {
8760 drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
8761 drp->trunc_len = htons(chk->send_size);
8762 chk->send_size = small_one - (SCTP_MED_OVERHEAD +
8763 sizeof(struct sctp_pktdrop_chunk) +
8764 sizeof(struct sctphdr));
8765 len = chk->send_size;
8767 /* no truncation needed */
8768 drp->ch.chunk_flags = 0;
8769 drp->trunc_len = htons(0);
8772 drp->ch.chunk_flags |= SCTP_BADCRC;
8774 chk->send_size += sizeof(struct sctp_pktdrop_chunk);
8775 chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
8776 chk->sent = SCTP_DATAGRAM_UNSENT;
8779 /* we should hit here */
8782 chk->whoTo = asoc->primary_destination;
8784 chk->whoTo->ref_count++;
8785 chk->rec.chunk_id = SCTP_PACKET_DROPPED;
8786 drp->ch.chunk_type = SCTP_PACKET_DROPPED;
8787 drp->ch.chunk_length = htons(chk->send_size);
8788 spc = stcb->sctp_socket->so_rcv.ssb_hiwat;
8792 drp->bottle_bw = htonl(spc);
8793 drp->current_onq = htonl(asoc->size_on_delivery_queue +
8794 asoc->size_on_reasm_queue +
8795 asoc->size_on_all_streams +
8796 asoc->my_rwnd_control_len +
8797 stcb->sctp_socket->so_rcv.ssb_cc);
8800 m_copydata(m, iphlen, len, datap);
8801 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
8802 asoc->ctrl_queue_cnt++;
8806 sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn)
8808 struct sctp_association *asoc;
8809 struct sctp_cwr_chunk *cwr;
8810 struct sctp_tmit_chunk *chk;
8813 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
8814 if (chk->rec.chunk_id == SCTP_ECN_CWR) {
8815 /* found a previous ECN_CWR update it if needed */
8816 cwr = mtod(chk->data, struct sctp_cwr_chunk *);
8817 if (compare_with_wrap(high_tsn, ntohl(cwr->tsn),
8819 cwr->tsn = htonl(high_tsn);
8824 /* nope could not find one to update so we must build one */
8825 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
8829 sctppcbinfo.ipi_count_chunk++;
8830 sctppcbinfo.ipi_gencnt_chunk++;
8831 chk->rec.chunk_id = SCTP_ECN_CWR;
8832 chk->asoc = &stcb->asoc;
8833 chk->send_size = sizeof(struct sctp_cwr_chunk);
8834 MGETHDR(chk->data, MB_DONTWAIT, MT_DATA);
8835 if (chk->data == NULL) {
8836 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
8837 sctppcbinfo.ipi_count_chunk--;
8838 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
8839 panic("Chunk count is negative");
8841 sctppcbinfo.ipi_gencnt_chunk++;
8844 chk->data->m_data += SCTP_MIN_OVERHEAD;
8845 chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
8846 chk->sent = SCTP_DATAGRAM_UNSENT;
8849 chk->whoTo->ref_count++;
8850 cwr = mtod(chk->data, struct sctp_cwr_chunk *);
8851 cwr->ch.chunk_type = SCTP_ECN_CWR;
8852 cwr->ch.chunk_flags = 0;
8853 cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
8854 cwr->tsn = htonl(high_tsn);
8855 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
8856 asoc->ctrl_queue_cnt++;
8859 sctp_reset_the_streams(struct sctp_tcb *stcb,
8860 struct sctp_stream_reset_request *req, int number_entries, uint16_t *list)
8864 if (req->reset_flags & SCTP_RESET_ALL) {
8865 for (i=0; i<stcb->asoc.streamoutcnt; i++) {
8866 stcb->asoc.strmout[i].next_sequence_sent = 0;
8868 } else if (number_entries) {
8869 for (i=0; i<number_entries; i++) {
8870 if (list[i] >= stcb->asoc.streamoutcnt) {
8871 /* no such stream */
8874 stcb->asoc.strmout[(list[i])].next_sequence_sent = 0;
8877 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list);
8881 sctp_send_str_reset_ack(struct sctp_tcb *stcb,
8882 struct sctp_stream_reset_request *req)
8884 struct sctp_association *asoc;
8885 struct sctp_stream_reset_resp *strack;
8886 struct sctp_tmit_chunk *chk;
8888 int number_entries, i;
8889 uint8_t two_way=0, not_peer=0;
8890 uint16_t *list=NULL;
8893 if (req->reset_flags & SCTP_RESET_ALL)
8896 number_entries = (ntohs(req->ph.param_length) - sizeof(struct sctp_stream_reset_request)) / sizeof(uint16_t);
8898 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
8902 sctppcbinfo.ipi_count_chunk++;
8903 sctppcbinfo.ipi_gencnt_chunk++;
8904 chk->rec.chunk_id = SCTP_STREAM_RESET;
8905 chk->asoc = &stcb->asoc;
8906 chk->send_size = sizeof(struct sctp_stream_reset_resp) + (number_entries * sizeof(uint16_t));
8907 MGETHDR(chk->data, MB_DONTWAIT, MT_DATA);
8908 if (chk->data == NULL) {
8910 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
8911 sctppcbinfo.ipi_count_chunk--;
8912 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
8913 panic("Chunk count is negative");
8915 sctppcbinfo.ipi_gencnt_chunk++;
8918 chk->data->m_data += SCTP_MIN_OVERHEAD;
8919 chk->data->m_pkthdr.len = chk->data->m_len = SCTP_SIZE32(chk->send_size);
8920 if (M_TRAILINGSPACE(chk->data) < (int)SCTP_SIZE32(chk->send_size)) {
8921 MCLGET(chk->data, MB_DONTWAIT);
8922 if ((chk->data->m_flags & M_EXT) == 0) {
8924 sctp_m_freem(chk->data);
8926 goto strresp_jump_out;
8928 chk->data->m_data += SCTP_MIN_OVERHEAD;
8930 if (M_TRAILINGSPACE(chk->data) < (int)SCTP_SIZE32(chk->send_size)) {
8931 /* can't do it, no room */
8933 sctp_m_freem(chk->data);
8935 goto strresp_jump_out;
8938 chk->sent = SCTP_DATAGRAM_UNSENT;
8940 chk->whoTo = asoc->primary_destination;
8941 chk->whoTo->ref_count++;
8942 strack = mtod(chk->data, struct sctp_stream_reset_resp *);
8944 strack->ch.chunk_type = SCTP_STREAM_RESET;
8945 strack->ch.chunk_flags = 0;
8946 strack->ch.chunk_length = htons(chk->send_size);
8948 memset(strack->sr_resp.reset_pad, 0, sizeof(strack->sr_resp.reset_pad));
8950 strack->sr_resp.ph.param_type = ntohs(SCTP_STR_RESET_RESPONSE);
8951 strack->sr_resp.ph.param_length = htons((chk->send_size - sizeof(struct sctp_chunkhdr)));
8955 if (chk->send_size % 4) {
8956 /* need a padding for the end */
8959 end = (uint8_t *)((caddr_t)strack + chk->send_size);
8960 pad = chk->send_size % 4;
8961 for (i = 0; i < pad; i++) {
8964 chk->send_size += pad;
8967 /* actual response */
8968 if (req->reset_flags & SCTP_RESET_YOUR) {
8969 strack->sr_resp.reset_flags = SCTP_RESET_PERFORMED;
8971 strack->sr_resp.reset_flags = 0;
8974 /* copied from reset request */
8975 strack->sr_resp.reset_req_seq_resp = req->reset_req_seq;
8976 seq = ntohl(req->reset_req_seq);
8978 list = req->list_of_streams;
8979 /* copy the un-converted network byte order streams */
8980 for (i=0; i<number_entries; i++) {
8981 strack->sr_resp.list_of_streams[i] = list[i];
8983 if (asoc->str_reset_seq_in == seq) {
8984 /* is it the next expected? */
8985 asoc->str_reset_seq_in++;
8986 strack->sr_resp.reset_at_tsn = htonl(asoc->sending_seq);
8987 asoc->str_reset_sending_seq = asoc->sending_seq;
8988 if (number_entries) {
8991 /* convert them to host byte order */
8992 for (i=0 ; i<number_entries; i++) {
8993 temp = ntohs(list[i]);
8997 if (req->reset_flags & SCTP_RESET_YOUR) {
8998 /* reset my outbound streams */
8999 sctp_reset_the_streams(stcb, req , number_entries, list);
9001 if (req->reset_flags & SCTP_RECIPRICAL) {
9002 /* reset peer too */
9003 sctp_send_str_reset_req(stcb, number_entries, list, two_way, not_peer);
9007 /* no its a retran so I must just ack and do nothing */
9008 strack->sr_resp.reset_at_tsn = htonl(asoc->str_reset_sending_seq);
9010 strack->sr_resp.cumulative_tsn = htonl(asoc->cumulative_tsn);
9011 TAILQ_INSERT_TAIL(&asoc->control_send_queue,
9014 asoc->ctrl_queue_cnt++;
9019 sctp_send_str_reset_req(struct sctp_tcb *stcb,
9020 int number_entrys, uint16_t *list, uint8_t two_way, uint8_t not_peer)
9022 /* Send a stream reset request. The number_entrys may be 0 and list NULL
9023 * if the request is to reset all streams. If two_way is true then we
9024 * not only request a RESET of the received streams but we also
9025 * request the peer to send a reset req to us too.
9026 * Flag combinations in table:
9028 * two_way | not_peer | = | Flags
9029 * ------------------------------
9030 * 0 | 0 | = | SCTP_RESET_YOUR (just the peer)
9031 * 1 | 0 | = | SCTP_RESET_YOUR | SCTP_RECIPRICAL (both sides)
9032 * 0 | 1 | = | Not a Valid Request (not anyone)
9033 * 1 | 1 | = | SCTP_RESET_RECIPRICAL (Just local host)
9035 struct sctp_association *asoc;
9036 struct sctp_stream_reset_req *strreq;
9037 struct sctp_tmit_chunk *chk;
9041 if (asoc->stream_reset_outstanding) {
9042 /* Already one pending, must get ACK back
9043 * to clear the flag.
9048 if ((two_way == 0) && (not_peer == 1)) {
9049 /* not a valid request */
9053 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
9057 sctppcbinfo.ipi_count_chunk++;
9058 sctppcbinfo.ipi_gencnt_chunk++;
9059 chk->rec.chunk_id = SCTP_STREAM_RESET;
9060 chk->asoc = &stcb->asoc;
9061 chk->send_size = sizeof(struct sctp_stream_reset_req) + (number_entrys * sizeof(uint16_t));
9062 MGETHDR(chk->data, MB_DONTWAIT, MT_DATA);
9063 if (chk->data == NULL) {
9065 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
9066 sctppcbinfo.ipi_count_chunk--;
9067 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
9068 panic("Chunk count is negative");
9070 sctppcbinfo.ipi_gencnt_chunk++;
9073 chk->data->m_data += SCTP_MIN_OVERHEAD;
9074 chk->data->m_pkthdr.len = chk->data->m_len = SCTP_SIZE32(chk->send_size);
9075 if (M_TRAILINGSPACE(chk->data) < (int)SCTP_SIZE32(chk->send_size)) {
9076 MCLGET(chk->data, MB_DONTWAIT);
9077 if ((chk->data->m_flags & M_EXT) == 0) {
9079 sctp_m_freem(chk->data);
9081 goto strreq_jump_out;
9083 chk->data->m_data += SCTP_MIN_OVERHEAD;
9085 if (M_TRAILINGSPACE(chk->data) < (int)SCTP_SIZE32(chk->send_size)) {
9086 /* can't do it, no room */
9088 sctp_m_freem(chk->data);
9090 goto strreq_jump_out;
9092 chk->sent = SCTP_DATAGRAM_UNSENT;
9094 chk->whoTo = asoc->primary_destination;
9095 chk->whoTo->ref_count++;
9097 strreq = mtod(chk->data, struct sctp_stream_reset_req *);
9098 strreq->ch.chunk_type = SCTP_STREAM_RESET;
9099 strreq->ch.chunk_flags = 0;
9100 strreq->ch.chunk_length = htons(chk->send_size);
9102 strreq->sr_req.ph.param_type = ntohs(SCTP_STR_RESET_REQUEST);
9103 strreq->sr_req.ph.param_length = htons((chk->send_size - sizeof(struct sctp_chunkhdr)));
9105 if (chk->send_size % 4) {
9106 /* need a padding for the end */
9109 end = (uint8_t *)((caddr_t)strreq + chk->send_size);
9110 pad = chk->send_size % 4;
9111 for (i=0; i<pad; i++) {
9114 chk->send_size += pad;
9117 strreq->sr_req.reset_flags = 0;
9118 if (number_entrys == 0) {
9119 strreq->sr_req.reset_flags |= SCTP_RESET_ALL;
9122 strreq->sr_req.reset_flags |= SCTP_RESET_YOUR;
9124 if (not_peer == 0) {
9125 strreq->sr_req.reset_flags |= SCTP_RECIPRICAL | SCTP_RESET_YOUR;
9127 strreq->sr_req.reset_flags |= SCTP_RECIPRICAL;
9130 memset(strreq->sr_req.reset_pad, 0, sizeof(strreq->sr_req.reset_pad));
9131 strreq->sr_req.reset_req_seq = htonl(asoc->str_reset_seq_out);
9132 if (number_entrys) {
9133 /* populate the specific entry's */
9135 for (i=0; i < number_entrys; i++) {
9136 strreq->sr_req.list_of_streams[i] = htons(list[i]);
9139 TAILQ_INSERT_TAIL(&asoc->control_send_queue,
9142 asoc->ctrl_queue_cnt++;
9143 sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
9144 asoc->stream_reset_outstanding = 1;
9148 sctp_send_abort(struct mbuf *m, int iphlen, struct sctphdr *sh, uint32_t vtag,
9149 struct mbuf *err_cause)
9152 * Formulate the abort message, and send it back down.
9155 struct sctp_abort_msg *abm;
9156 struct ip *iph, *iph_out;
9157 struct ip6_hdr *ip6, *ip6_out;
9160 /* don't respond to ABORT with ABORT */
9161 if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
9163 sctp_m_freem(err_cause);
9166 MGETHDR(mout, MB_DONTWAIT, MT_HEADER);
9169 sctp_m_freem(err_cause);
9172 iph = mtod(m, struct ip *);
9175 if (iph->ip_v == IPVERSION) {
9176 iph_out = mtod(mout, struct ip *);
9177 mout->m_len = sizeof(*iph_out) + sizeof(*abm);
9178 mout->m_next = err_cause;
9180 /* Fill in the IP header for the ABORT */
9181 iph_out->ip_v = IPVERSION;
9182 iph_out->ip_hl = (sizeof(struct ip) / 4);
9183 iph_out->ip_tos = (u_char)0;
9185 iph_out->ip_off = 0;
9186 iph_out->ip_ttl = MAXTTL;
9187 iph_out->ip_p = IPPROTO_SCTP;
9188 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
9189 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
9190 /* let IP layer calculate this */
9191 iph_out->ip_sum = 0;
9193 iphlen_out = sizeof(*iph_out);
9194 abm = (struct sctp_abort_msg *)((caddr_t)iph_out + iphlen_out);
9195 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
9196 ip6 = (struct ip6_hdr *)iph;
9197 ip6_out = mtod(mout, struct ip6_hdr *);
9198 mout->m_len = sizeof(*ip6_out) + sizeof(*abm);
9199 mout->m_next = err_cause;
9201 /* Fill in the IP6 header for the ABORT */
9202 ip6_out->ip6_flow = ip6->ip6_flow;
9203 ip6_out->ip6_hlim = ip6_defhlim;
9204 ip6_out->ip6_nxt = IPPROTO_SCTP;
9205 ip6_out->ip6_src = ip6->ip6_dst;
9206 ip6_out->ip6_dst = ip6->ip6_src;
9208 iphlen_out = sizeof(*ip6_out);
9209 abm = (struct sctp_abort_msg *)((caddr_t)ip6_out + iphlen_out);
9211 /* Currently not supported */
9215 abm->sh.src_port = sh->dest_port;
9216 abm->sh.dest_port = sh->src_port;
9217 abm->sh.checksum = 0;
9219 abm->sh.v_tag = sh->v_tag;
9220 abm->msg.ch.chunk_flags = SCTP_HAD_NO_TCB;
9222 abm->sh.v_tag = htonl(vtag);
9223 abm->msg.ch.chunk_flags = 0;
9225 abm->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
9228 struct mbuf *m_tmp = err_cause;
9230 /* get length of the err_cause chain */
9231 while (m_tmp != NULL) {
9232 err_len += m_tmp->m_len;
9233 m_tmp = m_tmp->m_next;
9235 mout->m_pkthdr.len = mout->m_len + err_len;
9237 /* need pad at end of chunk */
9240 padlen = 4 - (mout->m_pkthdr.len % 4);
9241 m_copyback(mout, mout->m_pkthdr.len, padlen, (caddr_t)&cpthis);
9243 abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch) + err_len);
9245 mout->m_pkthdr.len = mout->m_len;
9246 abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch));
9250 if ((sctp_no_csum_on_loopback) &&
9251 (m->m_pkthdr.rcvif) &&
9252 (m->m_pkthdr.rcvif->if_type == IFT_LOOP)) {
9253 abm->sh.checksum = 0;
9255 abm->sh.checksum = sctp_calculate_sum(mout, NULL, iphlen_out);
9258 /* zap the rcvif, it should be null */
9259 mout->m_pkthdr.rcvif = 0;
9260 if (iph_out != NULL) {
9263 /* zap the stack pointer to the route */
9264 bzero(&ro, sizeof ro);
9266 if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
9267 kprintf("sctp_send_abort calling ip_output:\n");
9268 sctp_print_address_pkt(iph_out, &abm->sh);
9271 /* set IPv4 length */
9272 #if defined(__FreeBSD__)
9273 iph_out->ip_len = mout->m_pkthdr.len;
9275 iph_out->ip_len = htons(mout->m_pkthdr.len);
9278 ip_output(mout, 0, &ro, IP_RAWOUTPUT, NULL
9279 #if defined(__OpenBSD__) || (defined(__FreeBSD__) && __FreeBSD_version >= 480000) \
9280 || defined(__NetBSD__) || defined(__DragonFly__)
9284 /* Free the route if we got one back */
9287 } else if (ip6_out != NULL) {
9288 #ifdef NEW_STRUCT_ROUTE
9291 struct route_in6 ro;
9294 /* zap the stack pointer to the route */
9295 bzero(&ro, sizeof(ro));
9297 if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
9298 kprintf("sctp_send_abort calling ip6_output:\n");
9299 sctp_print_address_pkt((struct ip *)ip6_out, &abm->sh);
9302 ip6_output(mout, NULL, &ro, 0, NULL, NULL
9303 #if defined(__NetBSD__)
9306 #if (defined(__FreeBSD__) && __FreeBSD_version >= 480000) || defined(__DragonFly__)
9310 /* Free the route if we got one back */
9314 sctp_pegs[SCTP_DATAGRAMS_SENT]++;
9318 sctp_send_operr_to(struct mbuf *m, int iphlen,
9322 struct sctphdr *ihdr;
9324 struct sctphdr *ohdr;
9325 struct sctp_chunkhdr *ophdr;
9329 struct sockaddr_in6 lsa6, fsa6;
9332 iph = mtod(m, struct ip *);
9333 ihdr = (struct sctphdr *)((caddr_t)iph + iphlen);
9334 if (!(scm->m_flags & M_PKTHDR)) {
9335 /* must be a pkthdr */
9336 kprintf("Huh, not a packet header in send_operr\n");
9340 M_PREPEND(scm, (sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr)), MB_DONTWAIT);
9342 /* can't send because we can't add a mbuf */
9345 ohdr = mtod(scm, struct sctphdr *);
9346 ohdr->src_port = ihdr->dest_port;
9347 ohdr->dest_port = ihdr->src_port;
9350 ophdr = (struct sctp_chunkhdr *)(ohdr + 1);
9351 ophdr->chunk_type = SCTP_OPERATION_ERROR;
9352 ophdr->chunk_flags = 0;
9353 ophdr->chunk_length = htons(scm->m_pkthdr.len - sizeof(struct sctphdr));
9354 if (scm->m_pkthdr.len % 4) {
9358 padlen = 4 - (scm->m_pkthdr.len % 4);
9359 m_copyback(scm, scm->m_pkthdr.len, padlen, (caddr_t)&cpthis);
9361 if ((sctp_no_csum_on_loopback) &&
9362 (m->m_pkthdr.rcvif) &&
9363 (m->m_pkthdr.rcvif->if_type == IFT_LOOP)) {
9366 val = sctp_calculate_sum(scm, NULL, 0);
9368 ohdr->checksum = val;
9369 if (iph->ip_v == IPVERSION) {
9373 M_PREPEND(scm, sizeof(struct ip), MB_DONTWAIT);
9376 bzero(&ro, sizeof ro);
9377 out = mtod(scm, struct ip *);
9378 out->ip_v = iph->ip_v;
9379 out->ip_hl = (sizeof(struct ip)/4);
9380 out->ip_tos = iph->ip_tos;
9381 out->ip_id = iph->ip_id;
9383 out->ip_ttl = MAXTTL;
9384 out->ip_p = IPPROTO_SCTP;
9386 out->ip_src = iph->ip_dst;
9387 out->ip_dst = iph->ip_src;
9388 #if defined(__FreeBSD__)
9389 out->ip_len = scm->m_pkthdr.len;
9391 out->ip_len = htons(scm->m_pkthdr.len);
9393 retcode = ip_output(scm, 0, &ro, IP_RAWOUTPUT, NULL
9394 #if defined(__OpenBSD__) || (defined(__FreeBSD__) && __FreeBSD_version >= 480000) \
9395 || defined(__NetBSD__) || defined(__DragonFly__)
9399 sctp_pegs[SCTP_DATAGRAMS_SENT]++;
9400 /* Free the route if we got one back */
9405 #ifdef NEW_STRUCT_ROUTE
9408 struct route_in6 ro;
9410 struct ip6_hdr *out6, *in6;
9412 M_PREPEND(scm, sizeof(struct ip6_hdr), MB_DONTWAIT);
9415 bzero(&ro, sizeof ro);
9416 in6 = mtod(m, struct ip6_hdr *);
9417 out6 = mtod(scm, struct ip6_hdr *);
9418 out6->ip6_flow = in6->ip6_flow;
9419 out6->ip6_hlim = ip6_defhlim;
9420 out6->ip6_nxt = IPPROTO_SCTP;
9421 out6->ip6_src = in6->ip6_dst;
9422 out6->ip6_dst = in6->ip6_src;
9425 bzero(&lsa6, sizeof(lsa6));
9426 lsa6.sin6_len = sizeof(lsa6);
9427 lsa6.sin6_family = AF_INET6;
9428 lsa6.sin6_addr = out6->ip6_src;
9429 bzero(&fsa6, sizeof(fsa6));
9430 fsa6.sin6_len = sizeof(fsa6);
9431 fsa6.sin6_family = AF_INET6;
9432 fsa6.sin6_addr = out6->ip6_dst;
9433 if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
9434 kprintf("sctp_operr_to calling ipv6 output:\n");
9436 sctp_print_address((struct sockaddr *)&lsa6);
9438 sctp_print_address((struct sockaddr *)&fsa6);
9440 #endif /* SCTP_DEBUG */
9441 ip6_output(scm, NULL, &ro, 0, NULL, NULL
9442 #if defined(__NetBSD__)
9445 #if (defined(__FreeBSD__) && __FreeBSD_version >= 480000) || defined(__DragonFly__)
9449 sctp_pegs[SCTP_DATAGRAMS_SENT]++;
9450 /* Free the route if we got one back */
9457 sctp_copy_one(struct mbuf *m, struct uio *uio, int cpsz, int resv_upfront, int *mbcnt)
9459 int left, cancpy, willcpy, error;
9468 if ((left+resv_upfront) > (int)MHLEN) {
9474 if ((m->m_flags & M_EXT) == 0) {
9478 *mbcnt += m->m_ext.ext_size;
9481 cancpy = M_TRAILINGSPACE(m);
9482 willcpy = min(cancpy, left);
9483 if ((willcpy + resv_upfront) > cancpy) {
9484 willcpy -= resv_upfront;
9487 /* Align data to the end */
9488 if ((m->m_flags & M_EXT) == 0) {
9489 if (m->m_flags & M_PKTHDR) {
9490 MH_ALIGN(m, willcpy);
9492 M_ALIGN(m, willcpy);
9495 MC_ALIGN(m, willcpy);
9497 error = uiomove(mtod(m, caddr_t), willcpy, uio);
9505 MGET(m->m_next, MB_WAIT, MT_DATA);
9506 if (m->m_next == NULL) {
9513 if (left > (int)MHLEN) {
9519 if ((m->m_flags & M_EXT) == 0) {
9523 *mbcnt += m->m_ext.ext_size;
9525 cancpy = M_TRAILINGSPACE(m);
9526 willcpy = min(cancpy, left);
9533 sctp_copy_it_in(struct sctp_inpcb *inp,
9534 struct sctp_tcb *stcb,
9535 struct sctp_association *asoc,
9536 struct sctp_nets *net,
9537 struct sctp_sndrcvinfo *srcv,
9541 /* This routine must be very careful in
9542 * its work. Protocol processing is
9543 * up and running so care must be taken to
9544 * spl...() when you need to do something
9545 * that may effect the stcb/asoc. The sb is
9546 * locked however. When data is copied the
9547 * protocol processing should be enabled since
9548 * this is a slower operation...
9552 int frag_size, mbcnt = 0, mbcnt_e = 0;
9553 unsigned int sndlen;
9554 unsigned int tot_demand;
9555 int tot_out, dataout;
9556 struct sctp_tmit_chunk *chk;
9558 struct sctp_stream_out *strq;
9563 so = stcb->sctp_socket;
9567 sndlen = uio->uio_resid;
9568 /* lock the socket buf */
9569 SOCKBUF_LOCK(&so->so_snd);
9570 error = ssb_lock(&so->so_snd, SBLOCKWAIT(flags));
9574 /* will it ever fit ? */
9575 if (sndlen > so->so_snd.ssb_hiwat) {
9576 /* It will NEVER fit */
9581 /* Do I need to block? */
9582 if ((so->so_snd.ssb_hiwat <
9583 (sndlen + asoc->total_output_queue_size)) ||
9584 (asoc->chunks_on_out_queue > sctp_max_chunks_on_queue) ||
9585 (asoc->total_output_mbuf_queue_size >
9586 so->so_snd.ssb_mbmax)
9588 /* prune any prsctp bufs out */
9589 if (asoc->peer_supports_prsctp) {
9590 sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
9593 * We store off a pointer to the endpoint.
9594 * Since on return from this we must check to
9595 * see if an so_error is set. If so we may have
9596 * been reset and our stcb destroyed. Returning
9597 * an error will flow back to the user...
9599 while ((so->so_snd.ssb_hiwat <
9600 (sndlen + asoc->total_output_queue_size)) ||
9601 (asoc->chunks_on_out_queue >
9602 sctp_max_chunks_on_queue) ||
9603 (asoc->total_output_mbuf_queue_size >
9604 so->so_snd.ssb_mbmax)
9606 if (flags & (MSG_FNONBLOCKING|MSG_DONTWAIT)) {
9607 /* Non-blocking io in place */
9608 error = EWOULDBLOCK;
9611 inp->sctp_tcb_at_block = (void *)stcb;
9612 inp->error_on_block = 0;
9613 #ifdef SCTP_BLK_LOGGING
9614 sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
9617 ssb_unlock(&so->so_snd);
9618 SCTP_TCB_UNLOCK(stcb);
9619 error = ssb_wait(&so->so_snd);
9620 SCTP_INP_RLOCK(inp);
9621 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
9622 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
9623 /* Should I really unlock ? */
9624 SCTP_INP_RUNLOCK(inp);
9628 SCTP_TCB_LOCK(stcb);
9629 SCTP_INP_RUNLOCK(inp);
9631 inp->sctp_tcb_at_block = 0;
9632 #ifdef SCTP_BLK_LOGGING
9633 sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
9636 if (inp->error_on_block) {
9638 * if our asoc was killed, the free code
9639 * (in sctp_pcb.c) will save a error in
9642 error = inp->error_on_block;
9650 /* did we encounter a socket error? */
9652 error = so->so_error;
9656 error = ssb_lock(&so->so_snd, M_WAITOK);
9658 /* Can't acquire the lock */
9662 #if defined(__FreeBSD__) && __FreeBSD_version >= 502115
9663 if (so->so_rcv.sb_state & SBS_CANTSENDMORE) {
9665 if (so->so_state & SS_CANTSENDMORE) {
9667 /* The socket is now set not to sendmore.. its gone */
9673 error = so->so_error;
9677 if (asoc->peer_supports_prsctp) {
9678 sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
9682 dataout = tot_out = uio->uio_resid;
9683 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
9684 resv_in_first = SCTP_MED_OVERHEAD;
9686 resv_in_first = SCTP_MED_V4_OVERHEAD;
9689 /* Are we aborting? */
9690 if (srcv->sinfo_flags & MSG_ABORT) {
9691 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
9692 (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_ECHOED)) {
9693 /* It has to be up before we abort */
9694 /* how big is the user initiated abort? */
9696 /* I wonder about doing a MGET without a splnet set.
9697 * it is done that way in the sosend code so I guess
9700 MGETHDR(mm, MB_WAIT, MT_DATA);
9702 struct sctp_paramhdr *ph;
9704 tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
9705 if (tot_demand > MHLEN) {
9706 if (tot_demand > MCLBYTES) {
9707 /* truncate user data */
9708 tot_demand = MCLBYTES;
9709 tot_out = tot_demand - sizeof(struct sctp_paramhdr);
9711 MCLGET(mm, MB_WAIT);
9712 if ((mm->m_flags & M_EXT) == 0) {
9713 /* truncate further */
9715 tot_out = tot_demand - sizeof(struct sctp_paramhdr);
9718 /* now move forward the data pointer */
9719 ph = mtod(mm, struct sctp_paramhdr *);
9720 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
9721 ph->param_length = htons((sizeof(struct sctp_paramhdr) + tot_out));
9723 mm->m_pkthdr.len = tot_out + sizeof(struct sctp_paramhdr);
9724 mm->m_len = mm->m_pkthdr.len;
9725 error = uiomove((caddr_t)ph, tot_out, uio);
9728 * Here if we can't get his data we
9729 * still abort we just don't get to
9730 * send the users note :-0
9736 ssb_unlock(&so->so_snd);
9737 SOCKBUF_UNLOCK(&so->so_snd);
9738 sctp_abort_an_association(stcb->sctp_ep, stcb,
9739 SCTP_RESPONSE_TO_USER_REQ,
9749 /* Now can we send this? */
9750 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
9751 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
9752 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
9753 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
9754 /* got data while shutting down */
9759 /* Is the stream no. valid? */
9760 if (srcv->sinfo_stream >= asoc->streamoutcnt) {
9761 /* Invalid stream number */
9766 if (asoc->strmout == NULL) {
9767 /* huh? software error */
9769 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
9770 kprintf("software error in sctp_copy_it_in\n");
9777 if ((srcv->sinfo_flags & MSG_EOF) &&
9778 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
9789 /* save off the tag */
9790 my_vtag = asoc->my_vtag;
9791 strq = &asoc->strmout[srcv->sinfo_stream];
9792 /* First lets figure out the "chunking" point */
9793 frag_size = sctp_get_frag_point(stcb, asoc);
9795 /* two choices here, it all fits in one chunk or
9796 * we need multiple chunks.
9799 SOCKBUF_UNLOCK(&so->so_snd);
9800 if (tot_out <= frag_size) {
9801 /* no need to setup a template */
9802 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
9805 SOCKBUF_LOCK(&so->so_snd);
9808 sctppcbinfo.ipi_count_chunk++;
9809 sctppcbinfo.ipi_gencnt_chunk++;
9810 asoc->chunks_on_out_queue++;
9811 MGETHDR(mm, MB_WAIT, MT_DATA);
9816 error = sctp_copy_one(mm, uio, tot_out, resv_in_first, &mbcnt_e);
9819 sctp_prepare_chunk(chk, stcb, srcv, strq, net);
9820 chk->mbcnt = mbcnt_e;
9823 mm->m_pkthdr.len = tot_out;
9827 /* the actual chunk flags */
9828 chk->rec.data.rcv_flags |= SCTP_DATA_NOT_FRAG;
9829 chk->whoTo->ref_count++;
9831 /* fix up the send_size if it is not present */
9832 chk->send_size = tot_out;
9833 chk->book_size = chk->send_size;
9834 /* ok, we are commited */
9835 if ((srcv->sinfo_flags & MSG_UNORDERED) == 0) {
9836 /* bump the ssn if we are unordered. */
9837 strq->next_sequence_sent++;
9839 if (chk->flags & SCTP_PR_SCTP_BUFFER) {
9840 asoc->sent_queue_cnt_removeable++;
9843 if ((asoc->state == 0) ||
9844 (my_vtag != asoc->my_vtag) ||
9845 (so != inp->sctp_socket) ||
9846 (inp->sctp_socket == 0)) {
9847 /* connection was aborted */
9852 asoc->stream_queue_cnt++;
9853 TAILQ_INSERT_TAIL(&strq->outqueue, chk, sctp_next);
9854 /* now check if this stream is on the wheel */
9855 if ((strq->next_spoke.tqe_next == NULL) &&
9856 (strq->next_spoke.tqe_prev == NULL)) {
9857 /* Insert it on the wheel since it is not
9860 sctp_insert_on_wheel(asoc, strq);
9865 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
9866 sctppcbinfo.ipi_count_chunk--;
9867 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
9868 panic("Chunk count is negative");
9870 SOCKBUF_LOCK(&so->so_snd);
9874 /* we need to setup a template */
9875 struct sctp_tmit_chunk template;
9876 struct sctpchunk_listhead tmp;
9878 /* setup the template */
9879 sctp_prepare_chunk(&template, stcb, srcv, strq, net);
9881 /* Prepare the temp list */
9884 /* Template is complete, now time for the work */
9885 while (tot_out > 0) {
9887 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
9890 * ok we must spin through and dump anything
9891 * we have allocated and then jump to the
9896 sctppcbinfo.ipi_count_chunk++;
9897 asoc->chunks_on_out_queue++;
9899 sctppcbinfo.ipi_gencnt_chunk++;
9901 chk->whoTo->ref_count++;
9902 MGETHDR(chk->data, MB_WAIT, MT_DATA);
9903 if (chk->data == NULL) {
9907 tot_demand = min(tot_out, frag_size);
9908 error = sctp_copy_one(chk->data, uio, tot_demand , resv_in_first, &mbcnt_e);
9911 /* now fix the chk->send_size */
9912 chk->mbcnt = mbcnt_e;
9915 chk->send_size = tot_demand;
9916 chk->data->m_pkthdr.len = tot_demand;
9917 chk->book_size = chk->send_size;
9918 if (chk->flags & SCTP_PR_SCTP_BUFFER) {
9919 asoc->sent_queue_cnt_removeable++;
9921 TAILQ_INSERT_TAIL(&tmp, chk, sctp_next);
9922 tot_out -= tot_demand;
9924 /* Now the tmp list holds all chunks and data */
9925 if ((srcv->sinfo_flags & MSG_UNORDERED) == 0) {
9926 /* bump the ssn if we are unordered. */
9927 strq->next_sequence_sent++;
9929 /* Mark the first/last flags. This will
9930 * result int a 3 for a single item on the list
9932 chk = TAILQ_FIRST(&tmp);
9933 chk->rec.data.rcv_flags |= SCTP_DATA_FIRST_FRAG;
9934 chk = TAILQ_LAST(&tmp, sctpchunk_listhead);
9935 chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
9937 /* now move it to the streams actual queue */
9938 /* first stop protocol processing */
9940 if ((asoc->state == 0) ||
9941 (my_vtag != asoc->my_vtag) ||
9942 (so != inp->sctp_socket) ||
9943 (inp->sctp_socket == 0)) {
9944 /* connection was aborted */
9949 chk = TAILQ_FIRST(&tmp);
9951 chk->data->m_nextpkt = 0;
9952 TAILQ_REMOVE(&tmp, chk, sctp_next);
9953 asoc->stream_queue_cnt++;
9954 TAILQ_INSERT_TAIL(&strq->outqueue, chk, sctp_next);
9955 chk = TAILQ_FIRST(&tmp);
9957 /* now check if this stream is on the wheel */
9958 if ((strq->next_spoke.tqe_next == NULL) &&
9959 (strq->next_spoke.tqe_prev == NULL)) {
9960 /* Insert it on the wheel since it is not
9963 sctp_insert_on_wheel(asoc, strq);
9965 /* Ok now we can allow pping */
9969 SOCKBUF_LOCK(&so->so_snd);
9970 chk = TAILQ_FIRST(&tmp);
9973 sctp_m_freem(chk->data);
9976 TAILQ_REMOVE(&tmp, chk, sctp_next);
9977 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
9978 sctppcbinfo.ipi_count_chunk--;
9979 asoc->chunks_on_out_queue--;
9980 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
9981 panic("Chunk count is negative");
9983 sctppcbinfo.ipi_gencnt_chunk++;
9984 chk = TAILQ_FIRST(&tmp);
9990 #ifdef SCTP_MBCNT_LOGGING
9991 sctp_log_mbcnt(SCTP_LOG_MBCNT_INCREASE,
9992 asoc->total_output_queue_size,
9994 asoc->total_output_mbuf_queue_size,
9998 SOCKBUF_LOCK(&so->so_snd);
9999 asoc->total_output_queue_size += dataout;
10000 asoc->total_output_mbuf_queue_size += mbcnt;
10001 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
10002 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
10003 so->so_snd.ssb_cc += dataout;
10004 so->so_snd.ssb_mbcnt += mbcnt;
10006 if ((srcv->sinfo_flags & MSG_EOF) &&
10007 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)
10009 int some_on_streamwheel = 0;
10011 if (!TAILQ_EMPTY(&asoc->out_wheel)) {
10012 /* Check to see if some data queued */
10013 struct sctp_stream_out *outs;
10014 TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) {
10015 if (!TAILQ_EMPTY(&outs->outqueue)) {
10016 some_on_streamwheel = 1;
10021 if (TAILQ_EMPTY(&asoc->send_queue) &&
10022 TAILQ_EMPTY(&asoc->sent_queue) &&
10023 (some_on_streamwheel == 0)) {
10024 /* there is nothing queued to send, so I'm done... */
10025 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
10026 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
10027 /* only send SHUTDOWN the first time through */
10029 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
10030 kprintf("%s:%d sends a shutdown\n",
10036 sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
10037 asoc->state = SCTP_STATE_SHUTDOWN_SENT;
10038 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
10039 asoc->primary_destination);
10040 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
10041 asoc->primary_destination);
10045 * we still got (or just got) data to send, so set
10049 * XXX sockets draft says that MSG_EOF should be sent
10050 * with no data. currently, we will allow user data
10051 * to be sent first and move to SHUTDOWN-PENDING
10053 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
10058 if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
10059 kprintf("++total out:%d total_mbuf_out:%d\n",
10060 (int)asoc->total_output_queue_size,
10061 (int)asoc->total_output_mbuf_queue_size);
10066 ssb_unlock(&so->so_snd);
10068 SOCKBUF_UNLOCK(&so->so_snd);
10077 sctp_sosend(struct socket *so,
10079 struct mbuf *addr_mbuf,
10081 struct sockaddr *addr,
10085 struct mbuf *control,
10086 #if defined(__NetBSD__) || defined(__APPLE__)
10090 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
10098 unsigned int sndlen;
10099 int error, use_rcvinfo;
10100 int queue_only = 0, queue_only_for_init=0;
10103 struct sctp_inpcb *inp;
10104 struct sctp_tcb *stcb=NULL;
10105 struct sctp_sndrcvinfo srcv;
10106 struct timeval now;
10107 struct sctp_nets *net;
10108 struct sctp_association *asoc;
10109 struct sctp_inpcb *t_inp;
10110 int create_lock_applied = 0;
10111 #if defined(__APPLE__)
10112 struct proc *p = current_proc();
10113 #elif defined(__NetBSD__)
10114 struct proc *p = curproc; /* XXX */
10115 struct sockaddr *addr = NULL;
10117 addr = mtod(addr_mbuf, struct sockaddr *);
10120 error = use_rcvinfo = 0;
10124 t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
10126 sndlen = uio->uio_resid;
10128 sndlen = top->m_pkthdr.len;
10133 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
10134 (inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING)) {
10135 /* The listner can NOT send */
10141 SCTP_ASOC_CREATE_LOCK(inp);
10142 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
10143 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
10144 /* Should I really unlock ? */
10150 create_lock_applied = 1;
10151 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
10152 (addr->sa_family == AF_INET6)) {
10158 /* now we must find the assoc */
10159 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
10160 SCTP_INP_RLOCK(inp);
10161 stcb = LIST_FIRST(&inp->sctp_asoc_list);
10162 if (stcb == NULL) {
10163 SCTP_INP_RUNLOCK(inp);
10168 SCTP_TCB_LOCK(stcb);
10169 SCTP_INP_RUNLOCK(inp);
10170 net = stcb->asoc.primary_destination;
10174 /* process cmsg snd/rcv info (maybe a assoc-id) */
10175 if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&srcv, control,
10178 if (srcv.sinfo_flags & MSG_SENDALL) {
10179 /* its a sendall */
10180 sctppcbinfo.mbuf_track--;
10181 sctp_m_freem(control);
10183 if (create_lock_applied) {
10184 SCTP_ASOC_CREATE_UNLOCK(inp);
10185 create_lock_applied = 0;
10187 return (sctp_sendall(inp, uio, top, &srcv));
10192 if (stcb == NULL) {
10193 /* Need to do a lookup */
10194 if (use_rcvinfo && srcv.sinfo_assoc_id) {
10195 stcb = sctp_findassociation_ep_asocid(inp, srcv.sinfo_assoc_id);
10197 * Question: Should I error here if the assoc_id is
10198 * no longer valid? i.e. I can't find it?
10202 /* Must locate the net structure */
10203 net = sctp_findnet(stcb, addr);
10206 if (stcb == NULL) {
10207 if (addr != NULL) {
10208 /* Since we did not use findep we must
10209 * increment it, and if we don't find a
10210 * tcb decrement it.
10212 SCTP_INP_WLOCK(inp);
10213 SCTP_INP_INCR_REF(inp);
10214 SCTP_INP_WUNLOCK(inp);
10215 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
10216 if (stcb == NULL) {
10217 SCTP_INP_WLOCK(inp);
10218 SCTP_INP_DECR_REF(inp);
10219 SCTP_INP_WUNLOCK(inp);
10224 if ((stcb == NULL) &&
10225 (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) {
10229 } else if ((stcb == NULL) && (addr == NULL)) {
10233 } else if (stcb == NULL) {
10234 /* UDP style, we must go ahead and start the INIT process */
10235 if ((use_rcvinfo) &&
10236 (srcv.sinfo_flags & MSG_ABORT)) {
10237 /* User asks to abort a non-existant asoc */
10242 /* get an asoc/stcb struct */
10243 stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0);
10244 if (stcb == NULL) {
10245 /* Error is setup for us in the call */
10249 if (create_lock_applied) {
10250 SCTP_ASOC_CREATE_UNLOCK(inp);
10251 create_lock_applied = 0;
10253 kprintf("Huh-3? create lock should have been on??\n");
10255 /* Turn on queue only flag to prevent data from being sent */
10257 asoc = &stcb->asoc;
10258 asoc->state = SCTP_STATE_COOKIE_WAIT;
10259 SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
10261 /* see if a init structure exists in cmsg headers */
10262 struct sctp_initmsg initm;
10264 if (sctp_find_cmsg(SCTP_INIT, (void *)&initm, control, sizeof(initm))) {
10265 /* we have an INIT override of the default */
10266 if (initm.sinit_max_attempts)
10267 asoc->max_init_times = initm.sinit_max_attempts;
10268 if (initm.sinit_num_ostreams)
10269 asoc->pre_open_streams = initm.sinit_num_ostreams;
10270 if (initm.sinit_max_instreams)
10271 asoc->max_inbound_streams = initm.sinit_max_instreams;
10272 if (initm.sinit_max_init_timeo)
10273 asoc->initial_init_rto_max = initm.sinit_max_init_timeo;
10274 if (asoc->streamoutcnt < asoc->pre_open_streams) {
10275 /* Default is NOT correct */
10277 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
10278 kprintf("Ok, defout:%d pre_open:%d\n",
10279 asoc->streamoutcnt, asoc->pre_open_streams);
10282 kfree(asoc->strmout, M_PCB);
10283 asoc->strmout = NULL;
10284 asoc->streamoutcnt = asoc->pre_open_streams;
10286 /* What happesn if this fails? .. we panic ...*/
10288 kmalloc(asoc->streamoutcnt *
10289 sizeof(struct sctp_stream_out),
10291 for (i = 0; i < asoc->streamoutcnt; i++) {
10293 * inbound side must be set to 0xffff,
10294 * also NOTE when we get the INIT-ACK
10295 * back (for INIT sender) we MUST
10296 * reduce the count (streamoutcnt) but
10297 * first check if we sent to any of the
10298 * upper streams that were dropped (if
10299 * some were). Those that were dropped
10300 * must be notified to the upper layer
10301 * as failed to send.
10303 asoc->strmout[i].next_sequence_sent = 0x0;
10304 TAILQ_INIT(&asoc->strmout[i].outqueue);
10305 asoc->strmout[i].stream_no = i;
10306 asoc->strmout[i].next_spoke.tqe_next = 0;
10307 asoc->strmout[i].next_spoke.tqe_prev = 0;
10313 /* out with the INIT */
10314 queue_only_for_init = 1;
10315 sctp_send_initiate(inp, stcb);
10317 * we may want to dig in after this call and adjust the MTU
10318 * value. It defaulted to 1500 (constant) but the ro structure
10319 * may now have an update and thus we may need to change it
10320 * BEFORE we append the message.
10322 net = stcb->asoc.primary_destination;
10323 asoc = &stcb->asoc;
10325 asoc = &stcb->asoc;
10327 if (create_lock_applied) {
10328 SCTP_ASOC_CREATE_UNLOCK(inp);
10329 create_lock_applied = 0;
10331 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
10332 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
10335 if (use_rcvinfo == 0) {
10336 /* Grab the default stuff from the asoc */
10337 srcv = stcb->asoc.def_send;
10339 /* we are now done with all control */
10341 sctp_m_freem(control);
10345 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
10346 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
10347 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
10348 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
10349 if ((use_rcvinfo) &&
10350 (srcv.sinfo_flags & MSG_ABORT)) {
10353 error = ECONNRESET;
10358 /* Ok, we will attempt a msgsnd :> */
10360 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
10361 p->td_lwp->lwp_ru.ru_msgsnd++;
10363 p->p_stats->p_ru.ru_msgsnd++;
10367 if (net && ((srcv.sinfo_flags & MSG_ADDR_OVER))) {
10368 /* we take the override or the unconfirmed */
10371 net = stcb->asoc.primary_destination;
10376 /* Must copy it all in from user land. The
10377 * socket buf is locked but we don't suspend
10378 * protocol processing until we are ready to
10382 error = sctp_copy_it_in(inp, stcb, asoc, net, &srcv, uio, flags);
10386 /* Here we must either pull in the user data to chunk
10387 * buffers, or use top to do a msg_append.
10389 error = sctp_msg_append(stcb, net, top, &srcv, flags);
10393 /* zap the top since it is now being used */
10397 if (net->flight_size > net->cwnd) {
10398 sctp_pegs[SCTP_SENDTO_FULL_CWND]++;
10401 } else if (asoc->ifp_had_enobuf) {
10402 sctp_pegs[SCTP_QUEONLY_BURSTLMT]++;
10405 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
10406 ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) * sizeof(struct sctp_data_chunk)) +
10407 SCTP_MED_OVERHEAD);
10409 if (((inp->sctp_flags & SCTP_PCB_FLAGS_NODELAY) == 0) &&
10410 (stcb->asoc.total_flight > 0) &&
10411 (un_sent < (int)stcb->asoc.smallest_mtu)) {
10413 /* Ok, Nagle is set on and we have data outstanding. Don't
10414 * send anything and let SACKs drive out the data unless we
10415 * have a "full" segment to send.
10417 sctp_pegs[SCTP_NAGLE_NOQ]++;
10420 sctp_pegs[SCTP_NAGLE_OFF]++;
10423 if (queue_only_for_init) {
10424 /* It is possible to have a turn around of the
10425 * INIT/INIT-ACK/COOKIE before I have a chance to
10426 * copy in the data. In such a case I DO want to
10427 * send it out by reversing the queue only flag.
10429 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) ||
10430 (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_ECHOED)) {
10431 /* yep, reverse it */
10436 if ((queue_only == 0) && (stcb->asoc.peers_rwnd && un_sent)) {
10437 /* we can attempt to send too.*/
10439 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
10440 kprintf("USR Send calls sctp_chunk_output\n");
10444 sctp_pegs[SCTP_OUTPUT_FRM_SND]++;
10445 sctp_chunk_output(inp, stcb, 0);
10447 } else if ((queue_only == 0) &&
10448 (stcb->asoc.peers_rwnd == 0) &&
10449 (stcb->asoc.total_flight == 0)) {
10450 /* We get to have a probe outstanding */
10452 sctp_from_user_send = 1;
10453 sctp_chunk_output(inp, stcb, 0);
10454 sctp_from_user_send = 0;
10457 } else if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
10458 int num_out, reason, cwnd_full;
10459 /* Here we do control only */
10461 sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
10462 &reason, 1, &cwnd_full, 1, &now, &now_filled);
10466 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
10467 kprintf("USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d \n",
10468 queue_only, stcb->asoc.peers_rwnd, un_sent,
10469 stcb->asoc.total_flight, stcb->asoc.chunks_on_out_queue,
10470 stcb->asoc.total_output_queue_size);
10474 if (create_lock_applied) {
10475 SCTP_ASOC_CREATE_UNLOCK(inp);
10476 create_lock_applied = 0;
10479 SCTP_TCB_UNLOCK(stcb);
10483 sctp_m_freem(control);