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;
3245 struct sctp_nets *net;
3247 memset(&sin4, 0, sizeof(sin4));
3248 memset(&sin6, 0, sizeof(sin6));
3249 sin4.sin_family = AF_INET;
3250 sin4.sin_len = sizeof(sin4);
3251 sin6.sin6_family = AF_INET6;
3252 sin6.sin6_len = sizeof(sin6);
3255 /* First what about the src address of the pkt ? */
3256 iph = mtod(in_initpkt, struct ip *);
3257 if (iph->ip_v == IPVERSION) {
3258 /* source addr is IPv4 */
3259 sin4.sin_addr = iph->ip_src;
3260 sa_touse = (struct sockaddr *)&sin4;
3261 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
3262 /* source addr is IPv6 */
3263 struct ip6_hdr *ip6h;
3264 ip6h = mtod(in_initpkt, struct ip6_hdr *);
3265 sin6.sin6_addr = ip6h->ip6_src;
3266 sa_touse = (struct sockaddr *)&sin6;
3272 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3273 sa = (struct sockaddr *)&net->ro._l_addr;
3274 if (sa->sa_family == sa_touse->sa_family) {
3275 if (sa->sa_family == AF_INET) {
3276 sa4 = (struct sockaddr_in *)sa;
3277 if (sa4->sin_addr.s_addr ==
3278 sin4.sin_addr.s_addr) {
3282 } else if (sa->sa_family == AF_INET6) {
3283 sa6 = (struct sockaddr_in6 *)sa;
3284 if (SCTP6_ARE_ADDR_EQUAL(&sa6->sin6_addr,
3293 /* New address added! no need to look futher. */
3296 /* Ok so far lets munge through the rest of the packet */
3299 offset += sizeof(struct sctp_init_chunk);
3300 phdr = sctp_get_next_param(mat, offset, ¶ms, sizeof(params));
3302 ptype = ntohs(phdr->param_type);
3303 plen = ntohs(phdr->param_length);
3304 if (ptype == SCTP_IPV4_ADDRESS) {
3305 struct sctp_ipv4addr_param *p4, p4_buf;
3307 phdr = sctp_get_next_param(mat, offset,
3308 (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
3309 if (plen != sizeof(struct sctp_ipv4addr_param) ||
3313 p4 = (struct sctp_ipv4addr_param *)phdr;
3314 sin4.sin_addr.s_addr = p4->addr;
3315 sa_touse = (struct sockaddr *)&sin4;
3316 } else if (ptype == SCTP_IPV6_ADDRESS) {
3317 struct sctp_ipv6addr_param *p6, p6_buf;
3319 phdr = sctp_get_next_param(mat, offset,
3320 (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
3321 if (plen != sizeof(struct sctp_ipv6addr_param) ||
3325 p6 = (struct sctp_ipv6addr_param *)phdr;
3326 memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
3328 sa_touse = (struct sockaddr *)&sin4;
3332 /* ok, sa_touse points to one to check */
3334 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3335 sa = (struct sockaddr *)&net->ro._l_addr;
3336 if (sa->sa_family != sa_touse->sa_family) {
3339 if (sa->sa_family == AF_INET) {
3340 sa4 = (struct sockaddr_in *)sa;
3341 if (sa4->sin_addr.s_addr ==
3342 sin4.sin_addr.s_addr) {
3346 } else if (sa->sa_family == AF_INET6) {
3347 sa6 = (struct sockaddr_in6 *)sa;
3348 if (SCTP6_ARE_ADDR_EQUAL(
3349 &sa6->sin6_addr, &sin6.sin6_addr)) {
3356 /* New addr added! no need to look further */
3360 offset += SCTP_SIZE32(plen);
3361 phdr = sctp_get_next_param(mat, offset, ¶ms, sizeof(params));
3367 * Given a MBUF chain that was sent into us containing an
3368 * INIT. Build a INIT-ACK with COOKIE and send back.
3369 * We assume that the in_initpkt has done a pullup to
3370 * include IPv6/4header, SCTP header and initial part of
3371 * INIT message (i.e. the struct sctp_init_msg).
3374 sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
3375 struct mbuf *init_pkt, int iphlen, int offset, struct sctphdr *sh,
3376 struct sctp_init_chunk *init_chk)
3378 struct sctp_association *asoc;
3379 struct mbuf *m, *m_at, *m_tmp, *m_cookie, *op_err, *m_last;
3380 struct sctp_init_msg *initackm_out;
3381 struct sctp_ecn_supported_param *ecn;
3382 struct sctp_prsctp_supported_param *prsctp;
3383 struct sctp_ecn_nonce_supported_param *ecn_nonce;
3384 struct sctp_supported_chunk_types_param *pr_supported;
3385 struct sockaddr_storage store;
3386 struct sockaddr_in *sin;
3387 struct sockaddr_in6 *sin6;
3390 struct ip6_hdr *ip6;
3391 struct sockaddr *to;
3392 struct sctp_state_cookie stc;
3393 struct sctp_nets *net=NULL;
3395 uint16_t his_limit, i_want;
3396 int abort_flag, padval, sz_of;
3404 if ((asoc != NULL) &&
3405 (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
3406 (sctp_are_there_new_addresses(asoc, init_pkt, iphlen, offset))) {
3407 /* new addresses, out of here in non-cookie-wait states */
3409 * Send a ABORT, we don't add the new address error clause though
3410 * we even set the T bit and copy in the 0 tag.. this looks no
3411 * different than if no listner was present.
3413 sctp_send_abort(init_pkt, iphlen, sh, 0, NULL);
3417 op_err = sctp_arethere_unrecognized_parameters(init_pkt,
3418 (offset+sizeof(struct sctp_init_chunk)),
3419 &abort_flag, (struct sctp_chunkhdr *)init_chk);
3421 sctp_send_abort(init_pkt, iphlen, sh, init_chk->init.initiate_tag, op_err);
3424 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
3426 /* No memory, INIT timer will re-attempt. */
3428 sctp_m_freem(op_err);
3431 MCLGET(m, MB_DONTWAIT);
3432 if ((m->m_flags & M_EXT) != M_EXT) {
3433 /* Failed to get cluster buffer */
3435 sctp_m_freem(op_err);
3439 m->m_data += SCTP_MIN_OVERHEAD;
3440 m->m_pkthdr.rcvif = 0;
3441 m->m_len = sizeof(struct sctp_init_msg);
3443 /* the time I built cookie */
3444 SCTP_GETTIME_TIMEVAL(&stc.time_entered);
3446 /* populate any tie tags */
3448 /* unlock before tag selections */
3449 SCTP_TCB_UNLOCK(stcb);
3450 if (asoc->my_vtag_nonce == 0)
3451 asoc->my_vtag_nonce = sctp_select_a_tag(inp);
3452 stc.tie_tag_my_vtag = asoc->my_vtag_nonce;
3454 if (asoc->peer_vtag_nonce == 0)
3455 asoc->peer_vtag_nonce = sctp_select_a_tag(inp);
3456 stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce;
3458 stc.cookie_life = asoc->cookie_life;
3459 net = asoc->primary_destination;
3460 /* now we must relock */
3461 SCTP_INP_RLOCK(inp);
3462 /* we may be in trouble here if the inp got freed
3463 * most likely this set of tests will protect
3464 * us but there is a chance not.
3466 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
3468 sctp_m_freem(op_err);
3470 sctp_send_abort(init_pkt, iphlen, sh, 0, NULL);
3473 SCTP_TCB_LOCK(stcb);
3474 SCTP_INP_RUNLOCK(stcb->sctp_ep);
3476 stc.tie_tag_my_vtag = 0;
3477 stc.tie_tag_peer_vtag = 0;
3478 /* life I will award this cookie */
3479 stc.cookie_life = inp->sctp_ep.def_cookie_life;
3482 /* copy in the ports for later check */
3483 stc.myport = sh->dest_port;
3484 stc.peerport = sh->src_port;
3487 * If we wanted to honor cookie life extentions, we would add
3488 * to stc.cookie_life. For now we should NOT honor any extension
3490 stc.site_scope = stc.local_scope = stc.loopback_scope = 0;
3491 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3492 struct inpcb *in_inp;
3493 /* Its a V6 socket */
3494 in_inp = (struct inpcb *)inp;
3495 stc.ipv6_addr_legal = 1;
3496 /* V4 addresses are NOT legal on the association */
3497 stc.ipv4_addr_legal = 0;
3499 /* Its a V4 socket, no - V6 */
3500 stc.ipv4_addr_legal = 1;
3501 stc.ipv6_addr_legal = 0;
3504 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
3509 /* now for scope setup */
3510 memset((caddr_t)&store, 0, sizeof(store));
3511 sin = (struct sockaddr_in *)&store;
3512 sin6 = (struct sockaddr_in6 *)&store;
3514 to = (struct sockaddr *)&store;
3515 iph = mtod(init_pkt, struct ip *);
3516 if (iph->ip_v == IPVERSION) {
3517 struct in_addr addr;
3518 struct route iproute;
3520 sin->sin_family = AF_INET;
3521 sin->sin_len = sizeof(struct sockaddr_in);
3522 sin->sin_port = sh->src_port;
3523 sin->sin_addr = iph->ip_src;
3524 /* lookup address */
3525 stc.address[0] = sin->sin_addr.s_addr;
3529 stc.addr_type = SCTP_IPV4_ADDRESS;
3530 /* local from address */
3531 memset(&iproute, 0, sizeof(iproute));
3533 memcpy(&ro->ro_dst, sin, sizeof(*sin));
3534 addr = sctp_ipv4_source_address_selection(inp, NULL,
3539 stc.laddress[0] = addr.s_addr;
3540 stc.laddress[1] = 0;
3541 stc.laddress[2] = 0;
3542 stc.laddress[3] = 0;
3543 stc.laddr_type = SCTP_IPV4_ADDRESS;
3544 /* scope_id is only for v6 */
3546 #ifndef SCTP_DONT_DO_PRIVADDR_SCOPE
3547 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
3552 #endif /* SCTP_DONT_DO_PRIVADDR_SCOPE */
3553 /* Must use the address in this case */
3554 if (sctp_is_address_on_local_host((struct sockaddr *)sin)) {
3555 stc.loopback_scope = 1;
3558 stc.local_scope = 1;
3560 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
3561 struct in6_addr addr;
3562 #ifdef NEW_STRUCT_ROUTE
3563 struct route iproute6;
3565 struct route_in6 iproute6;
3567 ip6 = mtod(init_pkt, struct ip6_hdr *);
3568 sin6->sin6_family = AF_INET6;
3569 sin6->sin6_len = sizeof(struct sockaddr_in6);
3570 sin6->sin6_port = sh->src_port;
3571 sin6->sin6_addr = ip6->ip6_src;
3572 /* lookup address */
3573 memcpy(&stc.address, &sin6->sin6_addr,
3574 sizeof(struct in6_addr));
3575 sin6->sin6_scope_id = 0;
3576 stc.addr_type = SCTP_IPV6_ADDRESS;
3578 if (sctp_is_address_on_local_host((struct sockaddr *)sin6)) {
3579 stc.loopback_scope = 1;
3580 stc.local_scope = 1;
3583 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
3585 * If the new destination is a LINK_LOCAL
3586 * we must have common both site and local
3587 * scope. Don't set local scope though since
3588 * we must depend on the source to be added
3589 * implicitly. We cannot assure just because
3590 * we share one link that all links are common.
3592 stc.local_scope = 0;
3595 /* we start counting for the private
3596 * address stuff at 1. since the link
3597 * local we source from won't show
3598 * up in our scoped cou8nt.
3601 /* pull out the scope_id from incoming pkt */
3602 in6_recoverscope(sin6, &ip6->ip6_src,
3603 init_pkt->m_pkthdr.rcvif);
3604 #if defined(SCTP_BASE_FREEBSD) || defined(__APPLE__) || defined(__DragonFly__)
3605 in6_embedscope(&sin6->sin6_addr, sin6, NULL,
3608 in6_embedscope(&sin6->sin6_addr, sin6);
3610 stc.scope_id = sin6->sin6_scope_id;
3611 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
3613 * If the new destination is SITE_LOCAL
3614 * then we must have site scope in common.
3618 /* local from address */
3619 memset(&iproute6, 0, sizeof(iproute6));
3620 ro = (struct route *)&iproute6;
3621 memcpy(&ro->ro_dst, sin6, sizeof(*sin6));
3622 addr = sctp_ipv6_source_address_selection(inp, NULL,
3627 memcpy(&stc.laddress, &addr, sizeof(struct in6_addr));
3628 stc.laddr_type = SCTP_IPV6_ADDRESS;
3631 /* set the scope per the existing tcb */
3632 struct sctp_nets *lnet;
3634 stc.loopback_scope = asoc->loopback_scope;
3635 stc.ipv4_scope = asoc->ipv4_local_scope;
3636 stc.site_scope = asoc->site_scope;
3637 stc.local_scope = asoc->local_scope;
3638 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
3639 if (lnet->ro._l_addr.sin6.sin6_family == AF_INET6) {
3640 if (IN6_IS_ADDR_LINKLOCAL(&lnet->ro._l_addr.sin6.sin6_addr)) {
3641 /* if we have a LL address, start counting
3649 /* use the net pointer */
3650 to = (struct sockaddr *)&net->ro._l_addr;
3651 if (to->sa_family == AF_INET) {
3652 sin = (struct sockaddr_in *)to;
3653 stc.address[0] = sin->sin_addr.s_addr;
3657 stc.addr_type = SCTP_IPV4_ADDRESS;
3658 if (net->src_addr_selected == 0) {
3659 /* strange case here, the INIT
3660 * should have did the selection.
3662 net->ro._s_addr.sin.sin_addr =
3663 sctp_ipv4_source_address_selection(inp,
3664 stcb, (struct route *)&net->ro, net, 0);
3665 net->src_addr_selected = 1;
3669 stc.laddress[0] = net->ro._s_addr.sin.sin_addr.s_addr;
3670 stc.laddress[1] = 0;
3671 stc.laddress[2] = 0;
3672 stc.laddress[3] = 0;
3673 stc.laddr_type = SCTP_IPV4_ADDRESS;
3674 } else if (to->sa_family == AF_INET6) {
3675 sin6 = (struct sockaddr_in6 *)to;
3676 memcpy(&stc.address, &sin6->sin6_addr,
3677 sizeof(struct in6_addr));
3678 stc.addr_type = SCTP_IPV6_ADDRESS;
3679 if (net->src_addr_selected == 0) {
3680 /* strange case here, the INIT
3681 * should have did the selection.
3683 net->ro._s_addr.sin6.sin6_addr =
3684 sctp_ipv6_source_address_selection(inp,
3685 stcb, (struct route *)&net->ro, net, 0);
3686 net->src_addr_selected = 1;
3688 memcpy(&stc.laddress, &net->ro._l_addr.sin6.sin6_addr,
3689 sizeof(struct in6_addr));
3690 stc.laddr_type = SCTP_IPV6_ADDRESS;
3693 /* Now lets put the SCTP header in place */
3694 initackm_out = mtod(m, struct sctp_init_msg *);
3695 initackm_out->sh.src_port = inp->sctp_lport;
3696 initackm_out->sh.dest_port = sh->src_port;
3697 initackm_out->sh.v_tag = init_chk->init.initiate_tag;
3698 /* Save it off for quick ref */
3699 stc.peers_vtag = init_chk->init.initiate_tag;
3700 initackm_out->sh.checksum = 0; /* calculate later */
3702 strncpy(stc.identification, SCTP_VERSION_STRING,
3703 min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification)));
3704 /* now the chunk header */
3705 initackm_out->msg.ch.chunk_type = SCTP_INITIATION_ACK;
3706 initackm_out->msg.ch.chunk_flags = 0;
3707 /* fill in later from mbuf we build */
3708 initackm_out->msg.ch.chunk_length = 0;
3709 /* place in my tag */
3710 if ((asoc != NULL) &&
3711 ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
3712 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED))) {
3713 /* re-use the v-tags and init-seq here */
3714 initackm_out->msg.init.initiate_tag = htonl(asoc->my_vtag);
3715 initackm_out->msg.init.initial_tsn = htonl(asoc->init_seq_number);
3717 initackm_out->msg.init.initiate_tag = htonl(sctp_select_a_tag(inp));
3718 /* get a TSN to use too */
3719 initackm_out->msg.init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
3721 /* save away my tag to */
3722 stc.my_vtag = initackm_out->msg.init.initiate_tag;
3724 /* set up some of the credits. */
3725 initackm_out->msg.init.a_rwnd = htonl(max(inp->sctp_socket->so_rcv.ssb_hiwat, SCTP_MINIMAL_RWND));
3726 /* set what I want */
3727 his_limit = ntohs(init_chk->init.num_inbound_streams);
3728 /* choose what I want */
3730 if (asoc->streamoutcnt > inp->sctp_ep.pre_open_stream_count) {
3731 i_want = asoc->streamoutcnt;
3733 i_want = inp->sctp_ep.pre_open_stream_count;
3736 i_want = inp->sctp_ep.pre_open_stream_count;
3738 if (his_limit < i_want) {
3739 /* I Want more :< */
3740 initackm_out->msg.init.num_outbound_streams = init_chk->init.num_inbound_streams;
3742 /* I can have what I want :> */
3743 initackm_out->msg.init.num_outbound_streams = htons(i_want);
3745 /* tell him his limt. */
3746 initackm_out->msg.init.num_inbound_streams =
3747 htons(inp->sctp_ep.max_open_streams_intome);
3748 /* setup the ECN pointer */
3750 /* if (inp->sctp_flags & SCTP_PCB_FLAGS_ADAPTIONEVNT) {*/
3751 if (inp->sctp_ep.adaption_layer_indicator) {
3752 struct sctp_adaption_layer_indication *ali;
3753 ali = (struct sctp_adaption_layer_indication *)(
3754 (caddr_t)initackm_out + sizeof(*initackm_out));
3755 ali->ph.param_type = htons(SCTP_ULP_ADAPTION);
3756 ali->ph.param_length = htons(sizeof(*ali));
3757 ali->indication = ntohl(inp->sctp_ep.adaption_layer_indicator);
3758 m->m_len += sizeof(*ali);
3759 ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali +
3762 ecn = (struct sctp_ecn_supported_param*)(
3763 (caddr_t)initackm_out + sizeof(*initackm_out));
3767 if (sctp_ecn == 1) {
3768 ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
3769 ecn->ph.param_length = htons(sizeof(*ecn));
3770 m->m_len += sizeof(*ecn);
3772 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
3775 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
3777 /* And now tell the peer we do pr-sctp */
3778 prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
3779 prsctp->ph.param_length = htons(sizeof(*prsctp));
3780 m->m_len += sizeof(*prsctp);
3783 /* And now tell the peer we do all the extensions */
3784 pr_supported = (struct sctp_supported_chunk_types_param *)((caddr_t)prsctp +
3787 pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
3788 pr_supported->ph.param_length = htons(sizeof(*pr_supported) + SCTP_EXT_COUNT);
3789 pr_supported->chunk_types[0] = SCTP_ASCONF;
3790 pr_supported->chunk_types[1] = SCTP_ASCONF_ACK;
3791 pr_supported->chunk_types[2] = SCTP_FORWARD_CUM_TSN;
3792 pr_supported->chunk_types[3] = SCTP_PACKET_DROPPED;
3793 pr_supported->chunk_types[4] = SCTP_STREAM_RESET;
3794 pr_supported->chunk_types[5] = 0; /* pad */
3795 pr_supported->chunk_types[6] = 0; /* pad */
3796 pr_supported->chunk_types[7] = 0; /* pad */
3798 m->m_len += (sizeof(*pr_supported) + SCTP_EXT_COUNT + SCTP_PAD_EXT_COUNT);
3799 if (sctp_ecn_nonce) {
3800 /* ECN nonce: And now tell the peer we support ECN nonce */
3801 ecn_nonce = (struct sctp_ecn_nonce_supported_param *)((caddr_t)pr_supported +
3802 sizeof(*pr_supported) + SCTP_EXT_COUNT + SCTP_PAD_EXT_COUNT);
3803 ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
3804 ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
3805 m->m_len += sizeof(*ecn_nonce);
3809 /* now the addresses */
3810 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3812 int cnt = cnt_inits_to;
3814 TAILQ_FOREACH(ifn, &ifnet, if_list) {
3815 struct ifaddr_container *ifac;
3817 if ((stc.loopback_scope == 0) &&
3818 (ifn->if_type == IFT_LOOP)) {
3820 * Skip loopback devices if loopback_scope
3825 TAILQ_FOREACH(ifac, &ifn->if_addrheads[mycpuid], ifa_link) {
3826 struct ifaddr *ifa = ifac->ifa;
3828 if (sctp_is_address_in_scope(ifa,
3829 stc.ipv4_addr_legal, stc.ipv6_addr_legal,
3830 stc.loopback_scope, stc.ipv4_scope,
3831 stc.local_scope, stc.site_scope) == 0) {
3838 TAILQ_FOREACH(ifn, &ifnet, if_list) {
3839 struct ifaddr_container *ifac;
3841 if ((stc.loopback_scope == 0) &&
3842 (ifn->if_type == IFT_LOOP)) {
3844 * Skip loopback devices if
3845 * loopback_scope not set
3849 TAILQ_FOREACH(ifac, &ifn->if_addrheads[mycpuid], ifa_link) {
3850 struct ifaddr *ifa = ifac->ifa;
3852 if (sctp_is_address_in_scope(ifa,
3853 stc.ipv4_addr_legal,
3854 stc.ipv6_addr_legal,
3855 stc.loopback_scope, stc.ipv4_scope,
3856 stc.local_scope, stc.site_scope) == 0) {
3859 m_at = sctp_add_addr_to_mbuf(m_at, ifa);
3864 struct sctp_laddr *laddr;
3867 /* First, how many ? */
3868 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3869 if (laddr->ifa == NULL) {
3872 if (laddr->ifa->ifa_addr == NULL)
3874 if (sctp_is_address_in_scope(laddr->ifa,
3875 stc.ipv4_addr_legal, stc.ipv6_addr_legal,
3876 stc.loopback_scope, stc.ipv4_scope,
3877 stc.local_scope, stc.site_scope) == 0) {
3882 /* If we bind a single address only we won't list
3883 * any. This way you can get through a NAT
3886 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3887 if (laddr->ifa == NULL) {
3889 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
3890 kprintf("Help I have fallen and I can't get up!\n");
3895 if (laddr->ifa->ifa_addr == NULL)
3897 if (sctp_is_address_in_scope(laddr->ifa,
3898 stc.ipv4_addr_legal, stc.ipv6_addr_legal,
3899 stc.loopback_scope, stc.ipv4_scope,
3900 stc.local_scope, stc.site_scope) == 0) {
3903 m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa);
3908 /* tack on the operational error if present */
3910 if (op_err->m_pkthdr.len % 4) {
3911 /* must add a pad to the param */
3914 padlen = 4 - (op_err->m_pkthdr.len % 4);
3915 m_copyback(op_err, op_err->m_pkthdr.len, padlen, (caddr_t)&cpthis);
3917 while (m_at->m_next != NULL) {
3918 m_at = m_at->m_next;
3920 m_at->m_next = op_err;
3921 while (m_at->m_next != NULL) {
3922 m_at = m_at->m_next;
3925 /* Get total size of init packet */
3926 sz_of = SCTP_SIZE32(ntohs(init_chk->ch.chunk_length));
3927 /* pre-calulate the size and update pkt header and chunk header */
3928 m->m_pkthdr.len = 0;
3929 for (m_tmp = m; m_tmp; m_tmp = m_tmp->m_next) {
3930 m->m_pkthdr.len += m_tmp->m_len;
3931 if (m_tmp->m_next == NULL) {
3932 /* m_tmp should now point to last one */
3937 * Figure now the size of the cookie. We know the size of the
3938 * INIT-ACK. The Cookie is going to be the size of INIT, INIT-ACK,
3939 * COOKIE-STRUCTURE and SIGNATURE.
3943 * take our earlier INIT calc and add in the sz we just calculated
3944 * minus the size of the sctphdr (its not included in chunk size
3947 /* add once for the INIT-ACK */
3948 sz_of += (m->m_pkthdr.len - sizeof(struct sctphdr));
3950 /* add a second time for the INIT-ACK in the cookie */
3951 sz_of += (m->m_pkthdr.len - sizeof(struct sctphdr));
3953 /* Now add the cookie header and cookie message struct */
3954 sz_of += sizeof(struct sctp_state_cookie_param);
3955 /* ...and add the size of our signature */
3956 sz_of += SCTP_SIGNATURE_SIZE;
3957 initackm_out->msg.ch.chunk_length = htons(sz_of);
3959 /* Now we must build a cookie */
3960 m_cookie = sctp_add_cookie(inp, init_pkt, offset, m,
3961 sizeof(struct sctphdr), &stc);
3962 if (m_cookie == NULL) {
3963 /* memory problem */
3967 /* Now append the cookie to the end and update the space/size */
3968 m_tmp->m_next = m_cookie;
3971 * We pass 0 here to NOT set IP_DF if its IPv4, we ignore the
3972 * return here since the timer will drive a retranmission.
3974 padval = m->m_pkthdr.len % 4;
3975 if ((padval) && (m_last)) {
3976 /* see my previous comments on m_last */
3978 ret = sctp_add_pad_tombuf(m_last, (4-padval));
3980 /* Houston we have a problem, no space */
3984 m->m_pkthdr.len += padval;
3986 sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, 0, NULL, 0);
3991 sctp_insert_on_wheel(struct sctp_association *asoc,
3992 struct sctp_stream_out *strq)
3994 struct sctp_stream_out *stre, *strn;
3995 stre = TAILQ_FIRST(&asoc->out_wheel);
3997 /* only one on wheel */
3998 TAILQ_INSERT_HEAD(&asoc->out_wheel, strq, next_spoke);
4001 for (; stre; stre = strn) {
4002 strn = TAILQ_NEXT(stre, next_spoke);
4003 if (stre->stream_no > strq->stream_no) {
4004 TAILQ_INSERT_BEFORE(stre, strq, next_spoke);
4006 } else if (stre->stream_no == strq->stream_no) {
4007 /* huh, should not happen */
4009 } else if (strn == NULL) {
4010 /* next one is null */
4011 TAILQ_INSERT_AFTER(&asoc->out_wheel, stre, strq,
4018 sctp_remove_from_wheel(struct sctp_association *asoc,
4019 struct sctp_stream_out *strq)
4021 /* take off and then setup so we know it is not on the wheel */
4022 TAILQ_REMOVE(&asoc->out_wheel, strq, next_spoke);
4023 strq->next_spoke.tqe_next = NULL;
4024 strq->next_spoke.tqe_prev = NULL;
4029 sctp_prune_prsctp(struct sctp_tcb *stcb,
4030 struct sctp_association *asoc,
4031 struct sctp_sndrcvinfo *srcv,
4036 struct sctp_tmit_chunk *chk, *nchk;
4037 if ((asoc->peer_supports_prsctp) && (asoc->sent_queue_cnt_removeable > 0)) {
4038 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
4040 * Look for chunks marked with the PR_SCTP
4041 * flag AND the buffer space flag. If the one
4042 * being sent is equal or greater priority then
4043 * purge the old one and free some space.
4045 if ((chk->flags & (SCTP_PR_SCTP_ENABLED |
4046 SCTP_PR_SCTP_BUFFER)) ==
4047 (SCTP_PR_SCTP_ENABLED|SCTP_PR_SCTP_BUFFER)) {
4049 * This one is PR-SCTP AND buffer space
4052 if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
4053 /* Lower numbers equates to
4054 * higher priority so if the
4055 * one we are looking at has a
4056 * larger or equal priority we
4057 * want to drop the data and
4058 * NOT retransmit it.
4067 if (chk->sent > SCTP_DATAGRAM_UNSENT)
4068 cause = SCTP_RESPONSE_TO_USER_REQ|SCTP_NOTIFY_DATAGRAM_SENT;
4070 cause = SCTP_RESPONSE_TO_USER_REQ|SCTP_NOTIFY_DATAGRAM_UNSENT;
4071 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
4074 freed_spc += ret_spc;
4075 if (freed_spc >= dataout) {
4078 } /* if chunk was present */
4079 } /* if of sufficent priority */
4080 } /* if chunk has enabled */
4081 } /* tailqforeach */
4083 chk = TAILQ_FIRST(&asoc->send_queue);
4085 nchk = TAILQ_NEXT(chk, sctp_next);
4086 /* Here we must move to the sent queue and mark */
4087 if ((chk->flags & (SCTP_PR_SCTP_ENABLED |
4088 SCTP_PR_SCTP_BUFFER)) ==
4089 (SCTP_PR_SCTP_ENABLED|SCTP_PR_SCTP_BUFFER)) {
4090 if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
4097 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
4098 SCTP_RESPONSE_TO_USER_REQ|SCTP_NOTIFY_DATAGRAM_UNSENT,
4101 freed_spc += ret_spc;
4102 if (freed_spc >= dataout) {
4105 } /* end if chk->data */
4106 } /* end if right class */
4107 } /* end if chk pr-sctp */
4109 } /* end while (chk) */
4110 } /* if enabled in asoc */
4114 sctp_prepare_chunk(struct sctp_tmit_chunk *template,
4115 struct sctp_tcb *stcb,
4116 struct sctp_sndrcvinfo *srcv,
4117 struct sctp_stream_out *strq,
4118 struct sctp_nets *net)
4120 bzero(template, sizeof(struct sctp_tmit_chunk));
4121 template->sent = SCTP_DATAGRAM_UNSENT;
4122 if ((stcb->asoc.peer_supports_prsctp) &&
4123 (srcv->sinfo_flags & (MSG_PR_SCTP_TTL|MSG_PR_SCTP_BUF)) &&
4124 (srcv->sinfo_timetolive > 0)
4127 * Peer supports PR-SCTP
4128 * The flags is set against this send for PR-SCTP
4129 * And timetolive is a postive value, zero is reserved
4130 * to mean a reliable send for both buffer/time
4133 if (srcv->sinfo_flags & MSG_PR_SCTP_BUF) {
4135 * Time to live is a priority stored in tv_sec
4136 * when doing the buffer drop thing.
4138 template->rec.data.timetodrop.tv_sec = srcv->sinfo_timetolive;
4142 SCTP_GETTIME_TIMEVAL(&template->rec.data.timetodrop);
4143 tv.tv_sec = srcv->sinfo_timetolive / 1000;
4144 tv.tv_usec = (srcv->sinfo_timetolive * 1000) % 1000000;
4146 timeradd(&template->rec.data.timetodrop, &tv,
4147 &template->rec.data.timetodrop);
4149 timevaladd(&template->rec.data.timetodrop, &tv);
4153 if ((srcv->sinfo_flags & MSG_UNORDERED) == 0) {
4154 template->rec.data.stream_seq = strq->next_sequence_sent;
4156 template->rec.data.stream_seq = 0;
4158 template->rec.data.TSN_seq = 0; /* not yet assigned */
4160 template->rec.data.stream_number = srcv->sinfo_stream;
4161 template->rec.data.payloadtype = srcv->sinfo_ppid;
4162 template->rec.data.context = srcv->sinfo_context;
4163 template->rec.data.doing_fast_retransmit = 0;
4164 template->rec.data.ect_nonce = 0; /* ECN Nonce */
4166 if (srcv->sinfo_flags & MSG_ADDR_OVER) {
4167 template->whoTo = net;
4169 if (stcb->asoc.primary_destination)
4170 template->whoTo = stcb->asoc.primary_destination;
4173 template->whoTo = net;
4176 /* the actual chunk flags */
4177 if (srcv->sinfo_flags & MSG_UNORDERED) {
4178 template->rec.data.rcv_flags = SCTP_DATA_UNORDERED;
4180 template->rec.data.rcv_flags = 0;
4182 /* no flags yet, FRAGMENT_OK goes here */
4183 template->flags = 0;
4185 if (stcb->asoc.peer_supports_prsctp) {
4186 if (srcv->sinfo_timetolive > 0) {
4188 * We only set the flag if timetolive (or
4189 * priority) was set to a positive number.
4190 * Zero is reserved specifically to be
4191 * EXCLUDED and sent reliable.
4193 if (srcv->sinfo_flags & MSG_PR_SCTP_TTL) {
4194 template->flags |= SCTP_PR_SCTP_ENABLED;
4196 if (srcv->sinfo_flags & MSG_PR_SCTP_BUF) {
4197 template->flags |= SCTP_PR_SCTP_BUFFER;
4201 template->asoc = &stcb->asoc;
4206 sctp_get_frag_point(struct sctp_tcb *stcb,
4207 struct sctp_association *asoc)
4211 /* For endpoints that have both 6 and 4 addresses
4212 * we must reserver room for the 6 ip header, for
4213 * those that are only dealing with V4 we use
4214 * a larger frag point.
4216 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
4217 ovh = SCTP_MED_OVERHEAD;
4219 ovh = SCTP_MED_V4_OVERHEAD;
4222 if (stcb->sctp_ep->sctp_frag_point > asoc->smallest_mtu)
4223 siz = asoc->smallest_mtu - ovh;
4225 siz = (stcb->sctp_ep->sctp_frag_point - ovh);
4227 if (siz > (MCLBYTES-sizeof(struct sctp_data_chunk))) { */
4228 /* A data chunk MUST fit in a cluster */
4229 /* siz = (MCLBYTES - sizeof(struct sctp_data_chunk));*/
4233 /* make it an even word boundary please */
4238 extern unsigned int sctp_max_chunks_on_queue;
4240 #define SBLOCKWAIT(f) (((f)&MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
4243 sctp_msg_append(struct sctp_tcb *stcb,
4244 struct sctp_nets *net,
4246 struct sctp_sndrcvinfo *srcv,
4250 struct sctp_association *asoc;
4251 struct sctp_stream_out *strq;
4252 struct sctp_tmit_chunk *chk;
4253 struct sctpchunk_listhead tmp;
4254 struct sctp_tmit_chunk template;
4255 struct mbuf *n, *mnext;
4257 unsigned int dataout, siz;
4262 if ((stcb == NULL) || (net == NULL) || (m == NULL) || (srcv == NULL)) {
4263 /* Software fault, you blew it on the call */
4265 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
4266 kprintf("software error in sctp_msg_append:1\n");
4267 kprintf("stcb:%p net:%p m:%p srcv:%p\n",
4268 stcb, net, m, srcv);
4275 so = stcb->sctp_socket;
4277 if (srcv->sinfo_flags & MSG_ABORT) {
4278 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
4279 (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_ECHOED)) {
4280 /* It has to be up before we abort */
4281 /* how big is the user initiated abort? */
4282 if ((m->m_flags & M_PKTHDR) && (m->m_pkthdr.len)) {
4283 dataout = m->m_pkthdr.len;
4287 for (n = m; n; n = n->m_next) {
4288 dataout += n->m_len;
4291 M_PREPEND(m, sizeof(struct sctp_paramhdr), MB_DONTWAIT);
4293 struct sctp_paramhdr *ph;
4294 m->m_len = sizeof(struct sctp_paramhdr) + dataout;
4295 ph = mtod(m, struct sctp_paramhdr *);
4296 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
4297 ph->param_length = htons(m->m_len);
4299 sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, m);
4302 /* Only free if we don't send an abort */
4307 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
4308 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
4309 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
4310 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
4311 /* got data while shutting down */
4316 if (srcv->sinfo_stream >= asoc->streamoutcnt) {
4317 /* Invalid stream number */
4321 if (asoc->strmout == NULL) {
4322 /* huh? software error */
4324 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
4325 kprintf("software error in sctp_msg_append:2\n");
4331 strq = &asoc->strmout[srcv->sinfo_stream];
4332 /* how big is it ? */
4333 if ((m->m_flags & M_PKTHDR) && (m->m_pkthdr.len)) {
4334 dataout = m->m_pkthdr.len;
4338 for (n = m; n; n = n->m_next) {
4339 dataout += n->m_len;
4343 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
4344 kprintf("Attempt to send out %d bytes\n",
4349 /* lock the socket buf */
4350 SOCKBUF_LOCK(&so->so_snd);
4351 error = ssb_lock(&so->so_snd, SBLOCKWAIT(flags));
4355 if (dataout > so->so_snd.ssb_hiwat) {
4356 /* It will NEVER fit */
4360 if ((srcv->sinfo_flags & MSG_EOF) &&
4361 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
4366 if ((so->so_snd.ssb_hiwat <
4367 (dataout + asoc->total_output_queue_size)) ||
4368 (asoc->chunks_on_out_queue > sctp_max_chunks_on_queue) ||
4369 (asoc->total_output_mbuf_queue_size >
4370 so->so_snd.ssb_mbmax)
4372 /* XXX Buffer space hunt for data to skip */
4373 if (asoc->peer_supports_prsctp) {
4374 sctp_prune_prsctp(stcb, asoc, srcv, dataout);
4376 while ((so->so_snd.ssb_hiwat <
4377 (dataout + asoc->total_output_queue_size)) ||
4378 (asoc->chunks_on_out_queue > sctp_max_chunks_on_queue) ||
4379 (asoc->total_output_mbuf_queue_size >
4380 so->so_snd.ssb_mbmax)) {
4381 struct sctp_inpcb *inp;
4382 /* Now did we free up enough room? */
4383 if (flags & (MSG_FNONBLOCKING|MSG_DONTWAIT)) {
4384 /* Non-blocking io in place */
4385 error = EWOULDBLOCK;
4389 * We store off a pointer to the endpoint.
4390 * Since on return from this we must check to
4391 * see if an so_error is set. If so we may have
4392 * been reset and our stcb destroyed. Returning
4393 * an error will cause the correct error return
4394 * through and fix this all.
4396 inp = stcb->sctp_ep;
4398 * Not sure how else to do this since
4399 * the level we suspended at is not
4400 * known deep down where we are. I will
4401 * drop to spl0() so that others can
4405 inp->sctp_tcb_at_block = (void *)stcb;
4406 inp->error_on_block = 0;
4407 ssb_unlock(&so->so_snd);
4408 error = ssb_wait(&so->so_snd);
4410 * XXX: This is ugly but I have
4411 * recreated most of what goes on to
4412 * block in the sb. UGHH
4413 * May want to add the bit about being
4414 * no longer connected.. but this then
4415 * further dooms the UDP model NOT to
4418 inp->sctp_tcb_at_block = 0;
4419 if (inp->error_on_block)
4420 error = inp->error_on_block;
4422 error = so->so_error;
4426 error = ssb_lock(&so->so_snd, M_WAITOK);
4429 /* Otherwise we cycle back and recheck
4432 #if defined(__FreeBSD__) && __FreeBSD_version >= 502115
4433 if (so->so_rcv.sb_state & SBS_CANTSENDMORE) {
4435 if (so->so_state & SS_CANTSENDMORE) {
4441 error = so->so_error;
4446 /* If we have a packet header fix it if it was broke */
4447 if (m->m_flags & M_PKTHDR) {
4448 m->m_pkthdr.len = dataout;
4450 /* use the smallest one, user set value or
4451 * smallest mtu of the asoc
4453 siz = sctp_get_frag_point(stcb, asoc);
4454 SOCKBUF_UNLOCK(&so->so_snd);
4455 if ((dataout) && (dataout <= siz)) {
4457 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
4460 SOCKBUF_LOCK(&so->so_snd);
4463 sctp_prepare_chunk(chk, stcb, srcv, strq, net);
4464 chk->whoTo->ref_count++;
4465 chk->rec.data.rcv_flags |= SCTP_DATA_NOT_FRAG;
4467 /* no flags yet, FRAGMENT_OK goes here */
4468 sctppcbinfo.ipi_count_chunk++;
4469 sctppcbinfo.ipi_gencnt_chunk++;
4470 asoc->chunks_on_out_queue++;
4473 /* Total in the MSIZE */
4474 for (mm = chk->data; mm; mm = mm->m_next) {
4476 if (mm->m_flags & M_EXT) {
4477 mbcnt += chk->data->m_ext.ext_size;
4480 /* fix up the send_size if it is not present */
4481 chk->send_size = dataout;
4482 chk->book_size = chk->send_size;
4484 /* ok, we are commited */
4485 if ((srcv->sinfo_flags & MSG_UNORDERED) == 0) {
4486 /* bump the ssn if we are unordered. */
4487 strq->next_sequence_sent++;
4489 chk->data->m_nextpkt = 0;
4490 asoc->stream_queue_cnt++;
4491 TAILQ_INSERT_TAIL(&strq->outqueue, chk, sctp_next);
4492 /* now check if this stream is on the wheel */
4493 if ((strq->next_spoke.tqe_next == NULL) &&
4494 (strq->next_spoke.tqe_prev == NULL)) {
4495 /* Insert it on the wheel since it is not
4498 sctp_insert_on_wheel(asoc, strq);
4500 } else if ((dataout) && (dataout > siz)) {
4502 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NO_FRAGMENT) &&
4505 SOCKBUF_LOCK(&so->so_snd);
4508 /* setup the template */
4509 sctp_prepare_chunk(&template, stcb, srcv, strq, net);
4512 while (dataout > siz) {
4514 * We can wait since this is called from the user
4517 n->m_nextpkt = m_split(n, siz, MB_WAIT);
4518 if (n->m_nextpkt == NULL) {
4520 SOCKBUF_LOCK(&so->so_snd);
4527 * ok, now we have a chain on m where m->m_nextpkt points to
4528 * the next chunk and m/m->m_next chain is the piece to send.
4529 * We must go through the chains and thread them on to
4530 * sctp_tmit_chunk chains and place them all on the stream
4531 * queue, breaking the m->m_nextpkt pointers as we go.
4537 * first go through and allocate a sctp_tmit chunk
4538 * for each chunk piece
4540 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
4543 * ok we must spin through and dump anything
4544 * we have allocated and then jump to the
4547 chk = TAILQ_FIRST(&tmp);
4549 TAILQ_REMOVE(&tmp, chk, sctp_next);
4550 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
4551 sctppcbinfo.ipi_count_chunk--;
4552 asoc->chunks_on_out_queue--;
4553 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
4554 panic("Chunk count is negative");
4556 sctppcbinfo.ipi_gencnt_chunk++;
4557 chk = TAILQ_FIRST(&tmp);
4560 SOCKBUF_LOCK(&so->so_snd);
4563 sctppcbinfo.ipi_count_chunk++;
4564 asoc->chunks_on_out_queue++;
4566 sctppcbinfo.ipi_gencnt_chunk++;
4568 chk->whoTo->ref_count++;
4570 /* Total in the MSIZE */
4572 for (mm = chk->data; mm; mm = mm->m_next) {
4574 if (mm->m_flags & M_EXT) {
4575 mbcnt_e += chk->data->m_ext.ext_size;
4578 /* now fix the chk->send_size */
4579 if (chk->data->m_flags & M_PKTHDR) {
4580 chk->send_size = chk->data->m_pkthdr.len;
4584 for (nn = chk->data; nn; nn = nn->m_next) {
4585 chk->send_size += nn->m_len;
4588 chk->book_size = chk->send_size;
4589 chk->mbcnt = mbcnt_e;
4591 if (chk->flags & SCTP_PR_SCTP_BUFFER) {
4592 asoc->sent_queue_cnt_removeable++;
4595 TAILQ_INSERT_TAIL(&tmp, chk, sctp_next);
4598 /* now that we have enough space for all de-couple the
4599 * chain of mbufs by going through our temp array
4600 * and breaking the pointers.
4602 /* ok, we are commited */
4603 if ((srcv->sinfo_flags & MSG_UNORDERED) == 0) {
4604 /* bump the ssn if we are unordered. */
4605 strq->next_sequence_sent++;
4607 /* Mark the first/last flags. This will
4608 * result int a 3 for a single item on the list
4610 chk = TAILQ_FIRST(&tmp);
4611 chk->rec.data.rcv_flags |= SCTP_DATA_FIRST_FRAG;
4612 chk = TAILQ_LAST(&tmp, sctpchunk_listhead);
4613 chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
4614 /* now break any chains on the queue and
4615 * move it to the streams actual queue.
4617 chk = TAILQ_FIRST(&tmp);
4619 chk->data->m_nextpkt = 0;
4620 TAILQ_REMOVE(&tmp, chk, sctp_next);
4621 asoc->stream_queue_cnt++;
4622 TAILQ_INSERT_TAIL(&strq->outqueue, chk, sctp_next);
4623 chk = TAILQ_FIRST(&tmp);
4625 /* now check if this stream is on the wheel */
4626 if ((strq->next_spoke.tqe_next == NULL) &&
4627 (strq->next_spoke.tqe_prev == NULL)) {
4628 /* Insert it on the wheel since it is not
4631 sctp_insert_on_wheel(asoc, strq);
4634 SOCKBUF_LOCK(&so->so_snd);
4635 /* has a SHUTDOWN been (also) requested by the user on this asoc? */
4638 if ((srcv->sinfo_flags & MSG_EOF) &&
4639 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
4641 int some_on_streamwheel = 0;
4643 if (!TAILQ_EMPTY(&asoc->out_wheel)) {
4644 /* Check to see if some data queued */
4645 struct sctp_stream_out *outs;
4646 TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) {
4647 if (!TAILQ_EMPTY(&outs->outqueue)) {
4648 some_on_streamwheel = 1;
4654 if (TAILQ_EMPTY(&asoc->send_queue) &&
4655 TAILQ_EMPTY(&asoc->sent_queue) &&
4656 (some_on_streamwheel == 0)) {
4657 /* there is nothing queued to send, so I'm done... */
4658 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
4659 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
4660 /* only send SHUTDOWN the first time through */
4662 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
4663 kprintf("%s:%d sends a shutdown\n",
4669 sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
4670 asoc->state = SCTP_STATE_SHUTDOWN_SENT;
4671 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
4672 asoc->primary_destination);
4673 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
4674 asoc->primary_destination);
4678 * we still got (or just got) data to send, so set
4682 * XXX sockets draft says that MSG_EOF should be sent
4683 * with no data. currently, we will allow user data
4684 * to be sent first and move to SHUTDOWN-PENDING
4686 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
4689 #ifdef SCTP_MBCNT_LOGGING
4690 sctp_log_mbcnt(SCTP_LOG_MBCNT_INCREASE,
4691 asoc->total_output_queue_size,
4693 asoc->total_output_mbuf_queue_size,
4696 asoc->total_output_queue_size += dataout;
4697 asoc->total_output_mbuf_queue_size += mbcnt;
4698 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4699 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
4700 so->so_snd.ssb_cc += dataout;
4701 so->so_snd.ssb_mbcnt += mbcnt;
4705 if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
4706 kprintf("++total out:%d total_mbuf_out:%d\n",
4707 (int)asoc->total_output_queue_size,
4708 (int)asoc->total_output_mbuf_queue_size);
4713 ssb_unlock(&so->so_snd);
4715 SOCKBUF_UNLOCK(&so->so_snd);
4717 if (m && m->m_nextpkt) {
4720 mnext = n->m_nextpkt;
4721 n->m_nextpkt = NULL;
4731 static struct mbuf *
4732 sctp_copy_mbufchain(struct mbuf *clonechain,
4733 struct mbuf *outchain)
4735 struct mbuf *appendchain;
4736 #if defined(__FreeBSD__) || defined(__NetBSD__)
4737 /* Supposedly m_copypacket is an optimization, use it if we can */
4738 if (clonechain->m_flags & M_PKTHDR) {
4739 appendchain = m_copypacket(clonechain, MB_DONTWAIT);
4740 sctp_pegs[SCTP_CACHED_SRC]++;
4742 appendchain = m_copy(clonechain, 0, M_COPYALL);
4743 #elif defined(__APPLE__)
4744 appendchain = sctp_m_copym(clonechain, 0, M_COPYALL, MB_DONTWAIT);
4746 appendchain = m_copy(clonechain, 0, M_COPYALL);
4749 if (appendchain == NULL) {
4752 sctp_m_freem(outchain);
4756 /* tack on to the end */
4760 if (m->m_next == NULL) {
4761 m->m_next = appendchain;
4766 if (outchain->m_flags & M_PKTHDR) {
4772 append_tot += t->m_len;
4775 outchain->m_pkthdr.len += append_tot;
4779 return (appendchain);
4784 sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr, u_int32_t val)
4786 struct sctp_copy_all *ca;
4790 ca = (struct sctp_copy_all *)ptr;
4791 if (ca->m == NULL) {
4794 if (ca->inp != inp) {
4798 m = sctp_copy_mbufchain(ca->m, NULL);
4800 /* can't copy so we are done */
4804 ret = sctp_msg_append(stcb, stcb->asoc.primary_destination, m,
4805 &ca->sndrcv, MSG_FNONBLOCKING);
4814 sctp_sendall_completes(void *ptr, u_int32_t val)
4816 struct sctp_copy_all *ca;
4817 ca = (struct sctp_copy_all *)ptr;
4818 /* Do a notify here?
4819 * Kacheong suggests that the notify
4820 * be done at the send time.. so you would
4821 * push up a notification if any send failed.
4822 * Don't know if this is feasable since the
4823 * only failures we have is "memory" related and
4824 * if you cannot get an mbuf to send the data
4825 * you surely can't get an mbuf to send up
4826 * to notify the user you can't send the data :->
4829 /* now free everything */
4835 #define MC_ALIGN(m, len) do { \
4836 (m)->m_data += (MCLBYTES - (len)) & ~(sizeof(long) - 1); \
4841 static struct mbuf *
4842 sctp_copy_out_all(struct uio *uio, int len)
4844 struct mbuf *ret, *at;
4845 int left, willcpy, cancpy, error;
4847 MGETHDR(ret, MB_WAIT, MT_HEADER);
4854 ret->m_pkthdr.len = len;
4855 MCLGET(ret, MB_WAIT);
4859 if ((ret->m_flags & M_EXT) == 0) {
4863 cancpy = M_TRAILINGSPACE(ret);
4864 willcpy = min(cancpy, left);
4867 /* Align data to the end */
4868 MC_ALIGN(at, willcpy);
4869 error = uiomove(mtod(at, caddr_t), willcpy, uio);
4875 at->m_len = willcpy;
4876 at->m_nextpkt = at->m_next = 0;
4879 MGET(at->m_next, MB_WAIT, MT_DATA);
4880 if (at->m_next == NULL) {
4885 MCLGET(at, MB_WAIT);
4889 if ((at->m_flags & M_EXT) == 0) {
4892 cancpy = M_TRAILINGSPACE(at);
4893 willcpy = min(cancpy, left);
4900 sctp_sendall (struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m, struct sctp_sndrcvinfo *srcv)
4903 struct sctp_copy_all *ca;
4904 ca = kmalloc(sizeof(struct sctp_copy_all), M_PCB, M_NOWAIT);
4909 memset (ca, 0, sizeof(struct sctp_copy_all));
4913 /* take off the sendall flag, it would
4914 * be bad if we failed to do this :-0
4916 ca->sndrcv.sinfo_flags &= ~MSG_SENDALL;
4918 /* get length and mbuf chain */
4920 ca->sndlen = uio->uio_resid;
4921 ca->m = sctp_copy_out_all(uio, ca->sndlen);
4922 if (ca->m == NULL) {
4927 if ((m->m_flags & M_PKTHDR) == 0) {
4930 ca->sndlen += m->m_len;
4934 ca->sndlen = m->m_pkthdr.len;
4939 ret = sctp_initiate_iterator(sctp_sendall_iterator, SCTP_PCB_ANY_FLAGS, SCTP_ASOC_ANY_STATE,
4940 (void *)ca, 0, sctp_sendall_completes, inp);
4943 kprintf("Failed to initate iterator to takeover associations\n");
4954 sctp_toss_old_cookies(struct sctp_association *asoc)
4956 struct sctp_tmit_chunk *chk, *nchk;
4957 chk = TAILQ_FIRST(&asoc->control_send_queue);
4959 nchk = TAILQ_NEXT(chk, sctp_next);
4960 if (chk->rec.chunk_id == SCTP_COOKIE_ECHO) {
4961 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
4963 sctp_m_freem(chk->data);
4966 asoc->ctrl_queue_cnt--;
4968 sctp_free_remote_addr(chk->whoTo);
4969 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
4970 sctppcbinfo.ipi_count_chunk--;
4971 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
4972 panic("Chunk count is negative");
4974 sctppcbinfo.ipi_gencnt_chunk++;
4981 sctp_toss_old_asconf(struct sctp_tcb *stcb)
4983 struct sctp_association *asoc;
4984 struct sctp_tmit_chunk *chk, *chk_tmp;
4987 for (chk = TAILQ_FIRST(&asoc->control_send_queue); chk != NULL;
4990 chk_tmp = TAILQ_NEXT(chk, sctp_next);
4991 /* find SCTP_ASCONF chunk in queue (only one ever in queue) */
4992 if (chk->rec.chunk_id == SCTP_ASCONF) {
4993 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
4995 sctp_m_freem(chk->data);
4998 asoc->ctrl_queue_cnt--;
5000 sctp_free_remote_addr(chk->whoTo);
5001 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
5002 sctppcbinfo.ipi_count_chunk--;
5003 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
5004 panic("Chunk count is negative");
5006 sctppcbinfo.ipi_gencnt_chunk++;
5013 sctp_clean_up_datalist(struct sctp_tcb *stcb,
5014 struct sctp_association *asoc,
5015 struct sctp_tmit_chunk **data_list,
5017 struct sctp_nets *net)
5020 for (i = 0; i < bundle_at; i++) {
5021 /* off of the send queue */
5023 /* Any chunk NOT 0 you zap the time
5024 * chunk 0 gets zapped or set based on
5025 * if a RTO measurment is needed.
5027 data_list[i]->do_rtt = 0;
5030 data_list[i]->sent_rcv_time = net->last_sent_time;
5031 TAILQ_REMOVE(&asoc->send_queue,
5034 /* on to the sent queue */
5035 TAILQ_INSERT_TAIL(&asoc->sent_queue,
5038 /* This does not lower until the cum-ack passes it */
5039 asoc->sent_queue_cnt++;
5040 asoc->send_queue_cnt--;
5041 if ((asoc->peers_rwnd <= 0) &&
5042 (asoc->total_flight == 0) &&
5044 /* Mark the chunk as being a window probe */
5046 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
5047 kprintf("WINDOW PROBE SET\n");
5050 sctp_pegs[SCTP_WINDOW_PROBES]++;
5051 data_list[i]->rec.data.state_flags |= SCTP_WINDOW_PROBE;
5053 data_list[i]->rec.data.state_flags &= ~SCTP_WINDOW_PROBE;
5055 #ifdef SCTP_AUDITING_ENABLED
5056 sctp_audit_log(0xC2, 3);
5058 data_list[i]->sent = SCTP_DATAGRAM_SENT;
5059 data_list[i]->snd_count = 1;
5060 net->flight_size += data_list[i]->book_size;
5061 asoc->total_flight += data_list[i]->book_size;
5062 asoc->total_flight_count++;
5063 #ifdef SCTP_LOG_RWND
5064 sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
5065 asoc->peers_rwnd , data_list[i]->send_size, sctp_peer_chunk_oh);
5067 asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
5068 (u_int32_t)(data_list[i]->send_size + sctp_peer_chunk_oh));
5069 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
5070 /* SWS sender side engages */
5071 asoc->peers_rwnd = 0;
5077 sctp_clean_up_ctl(struct sctp_association *asoc)
5079 struct sctp_tmit_chunk *chk, *nchk;
5080 for (chk = TAILQ_FIRST(&asoc->control_send_queue);
5082 nchk = TAILQ_NEXT(chk, sctp_next);
5083 if ((chk->rec.chunk_id == SCTP_SELECTIVE_ACK) ||
5084 (chk->rec.chunk_id == SCTP_HEARTBEAT_REQUEST) ||
5085 (chk->rec.chunk_id == SCTP_HEARTBEAT_ACK) ||
5086 (chk->rec.chunk_id == SCTP_SHUTDOWN) ||
5087 (chk->rec.chunk_id == SCTP_SHUTDOWN_ACK) ||
5088 (chk->rec.chunk_id == SCTP_OPERATION_ERROR) ||
5089 (chk->rec.chunk_id == SCTP_PACKET_DROPPED) ||
5090 (chk->rec.chunk_id == SCTP_COOKIE_ACK) ||
5091 (chk->rec.chunk_id == SCTP_ECN_CWR) ||
5092 (chk->rec.chunk_id == SCTP_ASCONF_ACK)) {
5093 /* Stray chunks must be cleaned up */
5095 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
5097 sctp_m_freem(chk->data);
5100 asoc->ctrl_queue_cnt--;
5101 sctp_free_remote_addr(chk->whoTo);
5102 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
5103 sctppcbinfo.ipi_count_chunk--;
5104 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
5105 panic("Chunk count is negative");
5107 sctppcbinfo.ipi_gencnt_chunk++;
5108 } else if (chk->rec.chunk_id == SCTP_STREAM_RESET) {
5109 struct sctp_stream_reset_req *strreq;
5110 /* special handling, we must look into the param */
5111 strreq = mtod(chk->data, struct sctp_stream_reset_req *);
5112 if (strreq->sr_req.ph.param_type == ntohs(SCTP_STR_RESET_RESPONSE)) {
5113 goto clean_up_anyway;
5120 sctp_move_to_outqueue(struct sctp_tcb *stcb,
5121 struct sctp_stream_out *strq)
5123 /* Move from the stream to the send_queue keeping track of the total */
5124 struct sctp_association *asoc;
5128 struct sctp_tmit_chunk *chk, *nchk;
5129 struct sctp_data_chunk *dchkh;
5130 struct sctpchunk_listhead tmp;
5135 chk = TAILQ_FIRST(&strq->outqueue);
5137 nchk = TAILQ_NEXT(chk, sctp_next);
5138 /* now put in the chunk header */
5140 M_PREPEND(chk->data, sizeof(struct sctp_data_chunk), MB_DONTWAIT);
5141 if (chk->data == NULL) {
5146 if (orig != chk->data) {
5147 /* A new mbuf was added, account for it */
5148 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5149 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
5150 stcb->sctp_socket->so_snd.ssb_mbcnt += MSIZE;
5152 #ifdef SCTP_MBCNT_LOGGING
5153 sctp_log_mbcnt(SCTP_LOG_MBCNT_INCREASE,
5154 asoc->total_output_queue_size,
5156 asoc->total_output_mbuf_queue_size,
5159 stcb->asoc.total_output_mbuf_queue_size += MSIZE;
5160 chk->mbcnt += MSIZE;
5162 chk->send_size += sizeof(struct sctp_data_chunk);
5163 /* This should NOT have to do anything, but
5164 * I would rather be cautious
5166 if (!failed && ((size_t)chk->data->m_len < sizeof(struct sctp_data_chunk))) {
5167 m_pullup(chk->data, sizeof(struct sctp_data_chunk));
5168 if (chk->data == NULL) {
5173 dchkh = mtod(chk->data, struct sctp_data_chunk *);
5174 dchkh->ch.chunk_length = htons(chk->send_size);
5175 /* Chunks must be padded to even word boundary */
5176 padval = chk->send_size % 4;
5178 /* For fragmented messages this should not
5179 * run except possibly on the last chunk
5181 if (sctp_pad_lastmbuf(chk->data, (4 - padval))) {
5182 /* we are in big big trouble no mbufs :< */
5186 chk->send_size += (4 - padval);
5188 /* pull from stream queue */
5189 TAILQ_REMOVE(&strq->outqueue, chk, sctp_next);
5190 asoc->stream_queue_cnt--;
5191 TAILQ_INSERT_TAIL(&tmp, chk, sctp_next);
5192 /* add it in to the size of moved chunks */
5193 if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
5194 /* we pull only one message */
5200 /* Gak, we just lost the user message */
5201 chk = TAILQ_FIRST(&tmp);
5203 nchk = TAILQ_NEXT(chk, sctp_next);
5204 TAILQ_REMOVE(&tmp, chk, sctp_next);
5206 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb,
5207 (SCTP_NOTIFY_DATAGRAM_UNSENT|SCTP_INTERNAL_ERROR),
5211 sctp_m_freem(chk->data);
5215 sctp_free_remote_addr(chk->whoTo);
5218 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
5219 sctppcbinfo.ipi_count_chunk--;
5220 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
5221 panic("Chunk count is negative");
5223 sctppcbinfo.ipi_gencnt_chunk++;
5228 /* now pull them off of temp wheel */
5229 chk = TAILQ_FIRST(&tmp);
5231 nchk = TAILQ_NEXT(chk, sctp_next);
5232 /* insert on send_queue */
5233 TAILQ_REMOVE(&tmp, chk, sctp_next);
5234 TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
5235 asoc->send_queue_cnt++;
5237 chk->rec.data.TSN_seq = asoc->sending_seq++;
5239 dchkh = mtod(chk->data, struct sctp_data_chunk *);
5240 /* Put the rest of the things in place now. Size
5241 * was done earlier in previous loop prior to
5244 dchkh->ch.chunk_type = SCTP_DATA;
5245 dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
5246 dchkh->dp.tsn = htonl(chk->rec.data.TSN_seq);
5247 dchkh->dp.stream_id = htons(strq->stream_no);
5248 dchkh->dp.stream_sequence = htons(chk->rec.data.stream_seq);
5249 dchkh->dp.protocol_id = chk->rec.data.payloadtype;
5250 /* total count moved */
5251 tot_moved += chk->send_size;
5258 sctp_fill_outqueue(struct sctp_tcb *stcb,
5259 struct sctp_nets *net)
5261 struct sctp_association *asoc;
5262 struct sctp_tmit_chunk *chk;
5263 struct sctp_stream_out *strq, *strqn;
5264 int mtu_fromwheel, goal_mtu;
5265 unsigned int moved, seenend, cnt_mvd=0;
5268 /* Attempt to move at least 1 MTU's worth
5269 * onto the wheel for each destination address
5271 goal_mtu = net->cwnd - net->flight_size;
5272 if ((unsigned int)goal_mtu < net->mtu) {
5273 goal_mtu = net->mtu;
5275 if (sctp_pegs[SCTP_MOVED_MTU] < (unsigned int)goal_mtu) {
5276 sctp_pegs[SCTP_MOVED_MTU] = goal_mtu;
5278 seenend = moved = mtu_fromwheel = 0;
5279 if (asoc->last_out_stream == NULL) {
5280 strq = asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
5281 if (asoc->last_out_stream == NULL) {
5282 /* huh nothing on the wheel, TSNH */
5287 strq = TAILQ_NEXT(asoc->last_out_stream, next_spoke);
5290 asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
5292 while (mtu_fromwheel < goal_mtu) {
5296 strq = TAILQ_FIRST(&asoc->out_wheel);
5297 } else if ((moved == 0) && (seenend)) {
5298 /* none left on the wheel */
5299 sctp_pegs[SCTP_MOVED_NLEF]++;
5303 * clear the flags and rotate back through
5308 strq = TAILQ_FIRST(&asoc->out_wheel);
5314 strqn = TAILQ_NEXT(strq, next_spoke);
5315 if ((chk = TAILQ_FIRST(&strq->outqueue)) == NULL) {
5316 /* none left on this queue, prune a spoke? */
5317 sctp_remove_from_wheel(asoc, strq);
5318 if (strq == asoc->last_out_stream) {
5319 /* the last one we used went off the wheel */
5320 asoc->last_out_stream = NULL;
5325 if (chk->whoTo != net) {
5326 /* Skip this stream, first one on stream
5327 * does not head to our current destination.
5332 mtu_fromwheel += sctp_move_to_outqueue(stcb, strq);
5335 asoc->last_out_stream = strq;
5338 sctp_pegs[SCTP_MOVED_MAX]++;
5340 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5341 kprintf("Ok we moved %d chunks to send queue\n",
5345 if (sctp_pegs[SCTP_MOVED_QMAX] < cnt_mvd) {
5346 sctp_pegs[SCTP_MOVED_QMAX] = cnt_mvd;
5351 sctp_fix_ecn_echo(struct sctp_association *asoc)
5353 struct sctp_tmit_chunk *chk;
5354 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
5355 if (chk->rec.chunk_id == SCTP_ECN_ECHO) {
5356 chk->sent = SCTP_DATAGRAM_UNSENT;
5362 sctp_move_to_an_alt(struct sctp_tcb *stcb,
5363 struct sctp_association *asoc,
5364 struct sctp_nets *net)
5366 struct sctp_tmit_chunk *chk;
5367 struct sctp_nets *a_net;
5368 a_net = sctp_find_alternate_net(stcb, net);
5369 if ((a_net != net) &&
5370 ((a_net->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE)) {
5372 * We only proceed if a valid alternate is found that is
5373 * not this one and is reachable. Here we must move all
5374 * chunks queued in the send queue off of the destination
5375 * address to our alternate.
5377 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
5378 if (chk->whoTo == net) {
5379 /* Move the chunk to our alternate */
5380 sctp_free_remote_addr(chk->whoTo);
5388 static int sctp_from_user_send=0;
5391 sctp_med_chunk_output(struct sctp_inpcb *inp,
5392 struct sctp_tcb *stcb,
5393 struct sctp_association *asoc,
5396 int control_only, int *cwnd_full, int from_where,
5397 struct timeval *now, int *now_filled)
5400 * Ok this is the generic chunk service queue.
5401 * we must do the following:
5402 * - Service the stream queue that is next, moving any message
5403 * (note I must get a complete message i.e. FIRST/MIDDLE and
5404 * LAST to the out queue in one pass) and assigning TSN's
5405 * - Check to see if the cwnd/rwnd allows any output, if so we
5406 * go ahead and fomulate and send the low level chunks. Making
5407 * sure to combine any control in the control chunk queue also.
5409 struct sctp_nets *net;
5410 struct mbuf *outchain;
5411 struct sctp_tmit_chunk *chk, *nchk;
5412 struct sctphdr *shdr;
5413 /* temp arrays for unlinking */
5414 struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
5415 int no_fragmentflg, error;
5416 int one_chunk, hbflag;
5417 int asconf, cookie, no_out_cnt;
5418 int bundle_at, ctl_cnt, no_data_chunks, cwnd_full_ind;
5419 unsigned int mtu, r_mtu, omtu;
5422 ctl_cnt = no_out_cnt = asconf = cookie = 0;
5424 * First lets prime the pump. For each destination, if there
5425 * is room in the flight size, attempt to pull an MTU's worth
5426 * out of the stream queues into the general send_queue
5428 #ifdef SCTP_AUDITING_ENABLED
5429 sctp_audit_log(0xC2, 2);
5432 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5433 kprintf("***********************\n");
5442 /* Nothing to possible to send? */
5443 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
5444 TAILQ_EMPTY(&asoc->send_queue) &&
5445 TAILQ_EMPTY(&asoc->out_wheel)) {
5447 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5448 kprintf("All wheels empty\n");
5453 if (asoc->peers_rwnd <= 0) {
5454 /* No room in peers rwnd */
5457 if (asoc->total_flight > 0) {
5458 /* we are allowed one chunk in flight */
5460 sctp_pegs[SCTP_RWND_BLOCKED]++;
5464 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5465 kprintf("Ok we have done the fillup no_data_chunk=%d tf=%d prw:%d\n",
5467 (int)asoc->total_flight, (int)asoc->peers_rwnd);
5470 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5472 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5473 kprintf("net:%p fs:%d cwnd:%d\n",
5474 net, net->flight_size, net->cwnd);
5477 if (net->flight_size >= net->cwnd) {
5478 /* skip this network, no room */
5481 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5482 kprintf("Ok skip fillup->fs:%d > cwnd:%d\n",
5487 sctp_pegs[SCTP_CWND_NOFILL]++;
5491 * spin through the stream queues moving one message and
5492 * assign TSN's as appropriate.
5494 sctp_fill_outqueue(stcb, net);
5496 *cwnd_full = cwnd_full_ind;
5497 /* now service each destination and send out what we can for it */
5499 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5501 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
5504 kprintf("We have %d chunks on the send_queue\n", chk_cnt);
5506 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
5509 kprintf("We have %d chunks on the sent_queue\n", chk_cnt);
5510 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
5513 kprintf("We have %d chunks on the control_queue\n", chk_cnt);
5516 /* If we have data to send, and DSACK is running, stop it
5517 * and build a SACK to dump on to bundle with output. This
5518 * actually MAY make it so the bundling does not occur if
5519 * the SACK is big but I think this is ok because basic SACK
5520 * space is pre-reserved in our fragmentation size choice.
5522 if ((TAILQ_FIRST(&asoc->send_queue) != NULL) &&
5523 (no_data_chunks == 0)) {
5524 /* We will be sending something */
5525 if (callout_pending(&stcb->asoc.dack_timer.timer)) {
5526 /* Yep a callout is pending */
5527 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
5530 sctp_send_sack(stcb);
5533 /* Nothing to send? */
5534 if ((TAILQ_FIRST(&asoc->control_send_queue) == NULL) &&
5535 (TAILQ_FIRST(&asoc->send_queue) == NULL)) {
5538 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5539 /* how much can we send? */
5540 if (net->ref_count < 2) {
5541 /* Ref-count of 1 so we cannot have data or control
5542 * queued to this address. Skip it.
5546 ctl_cnt = bundle_at = 0;
5551 if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
5552 mtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
5554 mtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
5556 if (mtu > asoc->peers_rwnd) {
5557 if (asoc->total_flight > 0) {
5558 /* We have a packet in flight somewhere */
5559 r_mtu = asoc->peers_rwnd;
5561 /* We are always allowed to send one MTU out */
5569 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5570 kprintf("Ok r_mtu is %d mtu is %d for this net:%p one_chunk:%d\n",
5571 r_mtu, mtu, net, one_chunk);
5574 /************************/
5575 /* Control transmission */
5576 /************************/
5577 /* Now first lets go through the control queue */
5578 for (chk = TAILQ_FIRST(&asoc->control_send_queue);
5580 nchk = TAILQ_NEXT(chk, sctp_next);
5581 if (chk->whoTo != net) {
5583 * No, not sent to the network we are
5588 if (chk->data == NULL) {
5591 if ((chk->data->m_flags & M_PKTHDR) == 0) {
5593 * NOTE: the chk queue MUST have the PKTHDR
5594 * flag set on it with a total in the
5595 * m_pkthdr.len field!! else the chunk will
5600 if (chk->sent != SCTP_DATAGRAM_UNSENT) {
5602 * It must be unsent. Cookies and ASCONF's
5603 * hang around but there timers will force
5604 * when marked for resend.
5608 /* Here we do NOT factor the r_mtu */
5609 if ((chk->data->m_pkthdr.len < (int)mtu) ||
5610 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
5612 * We probably should glom the mbuf chain from
5613 * the chk->data for control but the problem
5614 * is it becomes yet one more level of
5615 * tracking to do if for some reason output
5616 * fails. Then I have got to reconstruct the
5617 * merged control chain.. el yucko.. for now
5618 * we take the easy way and do the copy
5620 outchain = sctp_copy_mbufchain(chk->data,
5622 if (outchain == NULL) {
5625 /* update our MTU size */
5626 mtu -= chk->data->m_pkthdr.len;
5630 /* Do clear IP_DF ? */
5631 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
5634 /* Mark things to be removed, if needed */
5635 if ((chk->rec.chunk_id == SCTP_SELECTIVE_ACK) ||
5636 (chk->rec.chunk_id == SCTP_HEARTBEAT_REQUEST) ||
5637 (chk->rec.chunk_id == SCTP_HEARTBEAT_ACK) ||
5638 (chk->rec.chunk_id == SCTP_SHUTDOWN) ||
5639 (chk->rec.chunk_id == SCTP_SHUTDOWN_ACK) ||
5640 (chk->rec.chunk_id == SCTP_OPERATION_ERROR) ||
5641 (chk->rec.chunk_id == SCTP_COOKIE_ACK) ||
5642 (chk->rec.chunk_id == SCTP_ECN_CWR) ||
5643 (chk->rec.chunk_id == SCTP_PACKET_DROPPED) ||
5644 (chk->rec.chunk_id == SCTP_ASCONF_ACK)) {
5646 if (chk->rec.chunk_id == SCTP_HEARTBEAT_REQUEST)
5648 /* remove these chunks at the end */
5649 if (chk->rec.chunk_id == SCTP_SELECTIVE_ACK) {
5650 /* turn off the timer */
5651 if (callout_pending(&stcb->asoc.dack_timer.timer)) {
5652 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
5659 * Other chunks, since they have
5660 * timers running (i.e. COOKIE or
5661 * ASCONF) we just "trust" that it
5662 * gets sent or retransmitted.
5665 if (chk->rec.chunk_id == SCTP_COOKIE_ECHO) {
5668 } else if (chk->rec.chunk_id == SCTP_ASCONF) {
5670 * set hb flag since we can use
5676 chk->sent = SCTP_DATAGRAM_SENT;
5681 * Ok we are out of room but we can
5682 * output without effecting the flight
5683 * size since this little guy is a
5684 * control only packet.
5687 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
5691 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
5694 if (outchain->m_len == 0) {
5696 * Special case for when you
5697 * get a 0 len mbuf at the
5698 * head due to the lack of a
5699 * MHDR at the beginning.
5701 outchain->m_len = sizeof(struct sctphdr);
5703 M_PREPEND(outchain, sizeof(struct sctphdr), MB_DONTWAIT);
5704 if (outchain == NULL) {
5707 goto error_out_again;
5710 shdr = mtod(outchain, struct sctphdr *);
5711 shdr->src_port = inp->sctp_lport;
5712 shdr->dest_port = stcb->rport;
5713 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
5716 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
5717 (struct sockaddr *)&net->ro._l_addr,
5719 no_fragmentflg, 0, NULL, asconf))) {
5720 if (error == ENOBUFS) {
5721 asoc->ifp_had_enobuf = 1;
5723 sctp_pegs[SCTP_DATA_OUT_ERR]++;
5724 if (from_where == 0) {
5725 sctp_pegs[SCTP_ERROUT_FRM_USR]++;
5729 if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
5730 kprintf("Gak got ctrl error %d\n", error);
5733 /* error, could not output */
5736 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5737 kprintf("Update HB anyway\n");
5740 if (*now_filled == 0) {
5741 SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
5743 *now = net->last_sent_time;
5745 net->last_sent_time = *now;
5749 if (error == EHOSTUNREACH) {
5752 * unreachable during
5756 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5757 kprintf("Moving data to an alternate\n");
5760 sctp_move_to_an_alt(stcb, asoc, net);
5762 sctp_clean_up_ctl (asoc);
5765 asoc->ifp_had_enobuf = 0;
5766 /* Only HB or ASCONF advances time */
5768 if (*now_filled == 0) {
5769 SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
5771 *now = net->last_sent_time;
5773 net->last_sent_time = *now;
5778 * increase the number we sent, if a
5779 * cookie is sent we don't tell them
5783 *num_out += ctl_cnt;
5784 /* recalc a clean slate and setup */
5785 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5786 mtu = (net->mtu - SCTP_MIN_OVERHEAD);
5788 mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
5794 /*********************/
5795 /* Data transmission */
5796 /*********************/
5797 /* now lets add any data within the MTU constraints */
5798 if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
5799 omtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
5801 omtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
5805 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5806 kprintf("Now to data transmission\n");
5810 if (((asoc->state & SCTP_STATE_OPEN) == SCTP_STATE_OPEN) ||
5812 for (chk = TAILQ_FIRST(&asoc->send_queue); chk; chk = nchk) {
5813 if (no_data_chunks) {
5814 /* let only control go out */
5816 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5817 kprintf("Either nothing to send or we are full\n");
5822 if (net->flight_size >= net->cwnd) {
5823 /* skip this net, no room for data */
5825 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5826 kprintf("fs:%d > cwnd:%d\n",
5827 net->flight_size, net->cwnd);
5830 sctp_pegs[SCTP_CWND_BLOCKED]++;
5834 nchk = TAILQ_NEXT(chk, sctp_next);
5835 if (chk->whoTo != net) {
5836 /* No, not sent to this net */
5838 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5839 kprintf("chk->whoTo:%p not %p\n",
5847 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5848 kprintf("Can we pick up a chunk?\n");
5851 if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
5852 /* strange, we have a chunk that is to bit
5853 * for its destination and yet no fragment ok flag.
5854 * Something went wrong when the PMTU changed...we did
5855 * not mark this chunk for some reason?? I will
5856 * fix it here by letting IP fragment it for now and
5857 * printing a warning. This really should not happen ...
5859 /*#ifdef SCTP_DEBUG*/
5860 kprintf("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
5861 chk->send_size, mtu);
5863 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
5866 if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
5867 ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
5868 /* ok we will add this one */
5870 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5871 kprintf("Picking up the chunk\n");
5874 outchain = sctp_copy_mbufchain(chk->data, outchain);
5875 if (outchain == NULL) {
5877 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5878 kprintf("Gakk no memory\n");
5881 if (!callout_pending(&net->rxt_timer.timer)) {
5882 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
5886 /* upate our MTU size */
5887 /* Do clear IP_DF ? */
5888 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
5891 mtu -= chk->send_size;
5892 r_mtu -= chk->send_size;
5893 data_list[bundle_at++] = chk;
5894 if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
5902 if ((r_mtu <= 0) || one_chunk) {
5908 * Must be sent in order of the TSN's
5912 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5913 kprintf("ok no more chk:%d > mtu:%d || < r_mtu:%d\n",
5914 chk->send_size, mtu, r_mtu);
5921 } /* if asoc.state OPEN */
5922 /* Is there something to send for this destination? */
5924 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5925 kprintf("ok now is chain assembled? %p\n",
5931 /* We may need to start a control timer or two */
5933 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
5937 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
5940 /* must start a send timer if data is being sent */
5941 if (bundle_at && (!callout_pending(&net->rxt_timer.timer))) {
5942 /* no timer running on this destination
5946 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5947 kprintf("ok lets start a send timer .. we will transmit %p\n",
5951 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
5953 /* Now send it, if there is anything to send :> */
5954 if ((outchain->m_flags & M_PKTHDR) == 0) {
5957 MGETHDR(t, MB_DONTWAIT, MT_HEADER);
5959 sctp_m_freem(outchain);
5962 t->m_next = outchain;
5963 t->m_pkthdr.len = 0;
5964 t->m_pkthdr.rcvif = 0;
5969 outchain->m_pkthdr.len += t->m_len;
5973 if (outchain->m_len == 0) {
5974 /* Special case for when you get a 0 len
5975 * mbuf at the head due to the lack
5976 * of a MHDR at the beginning.
5978 MH_ALIGN(outchain, sizeof(struct sctphdr));
5979 outchain->m_len = sizeof(struct sctphdr);
5981 M_PREPEND(outchain, sizeof(struct sctphdr), MB_DONTWAIT);
5982 if (outchain == NULL) {
5988 shdr = mtod(outchain, struct sctphdr *);
5989 shdr->src_port = inp->sctp_lport;
5990 shdr->dest_port = stcb->rport;
5991 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
5993 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
5994 (struct sockaddr *)&net->ro._l_addr,
5996 no_fragmentflg, bundle_at, data_list[0], asconf))) {
5997 /* error, we could not output */
5998 if (error == ENOBUFS) {
5999 asoc->ifp_had_enobuf = 1;
6001 sctp_pegs[SCTP_DATA_OUT_ERR]++;
6002 if (from_where == 0) {
6003 sctp_pegs[SCTP_ERROUT_FRM_USR]++;
6008 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
6009 kprintf("Gak send error %d\n", error);
6014 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
6015 kprintf("Update HB time anyway\n");
6018 if (*now_filled == 0) {
6019 SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
6021 *now = net->last_sent_time;
6023 net->last_sent_time = *now;
6027 if (error == EHOSTUNREACH) {
6029 * Destination went unreachable during
6033 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
6034 kprintf("Calling the movement routine\n");
6037 sctp_move_to_an_alt(stcb, asoc, net);
6039 sctp_clean_up_ctl (asoc);
6042 asoc->ifp_had_enobuf = 0;
6044 if (bundle_at || hbflag) {
6045 /* For data/asconf and hb set time */
6046 if (*now_filled == 0) {
6047 SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
6049 *now = net->last_sent_time;
6051 net->last_sent_time = *now;
6056 *num_out += (ctl_cnt + bundle_at);
6059 if (!net->rto_pending) {
6060 /* setup for a RTO measurement */
6061 net->rto_pending = 1;
6062 data_list[0]->do_rtt = 1;
6064 data_list[0]->do_rtt = 0;
6066 sctp_pegs[SCTP_PEG_TSNS_SENT] += bundle_at;
6067 sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
6074 /* At the end there should be no NON timed
6075 * chunks hanging on this queue.
6077 if ((*num_out == 0) && (*reason_code == 0)) {
6080 sctp_clean_up_ctl (asoc);
6085 sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
6087 /* Prepend a OPERATIONAL_ERROR chunk header
6088 * and put on the end of the control chunk queue.
6090 /* Sender had better have gotten a MGETHDR or else
6091 * the control chunk will be forever skipped
6093 struct sctp_chunkhdr *hdr;
6094 struct sctp_tmit_chunk *chk;
6097 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
6100 sctp_m_freem(op_err);
6103 sctppcbinfo.ipi_count_chunk++;
6104 sctppcbinfo.ipi_gencnt_chunk++;
6105 M_PREPEND(op_err, sizeof(struct sctp_chunkhdr), MB_DONTWAIT);
6106 if (op_err == NULL) {
6107 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
6108 sctppcbinfo.ipi_count_chunk--;
6109 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
6110 panic("Chunk count is negative");
6112 sctppcbinfo.ipi_gencnt_chunk++;
6117 while (mat != NULL) {
6118 chk->send_size += mat->m_len;
6121 chk->rec.chunk_id = SCTP_OPERATION_ERROR;
6122 chk->sent = SCTP_DATAGRAM_UNSENT;
6125 chk->asoc = &stcb->asoc;
6127 chk->whoTo = chk->asoc->primary_destination;
6128 chk->whoTo->ref_count++;
6129 hdr = mtod(op_err, struct sctp_chunkhdr *);
6130 hdr->chunk_type = SCTP_OPERATION_ERROR;
6131 hdr->chunk_flags = 0;
6132 hdr->chunk_length = htons(chk->send_size);
6133 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue,
6136 chk->asoc->ctrl_queue_cnt++;
6140 sctp_send_cookie_echo(struct mbuf *m,
6142 struct sctp_tcb *stcb,
6143 struct sctp_nets *net)
6146 * pull out the cookie and put it at the front of the control
6150 struct mbuf *cookie, *mat;
6151 struct sctp_paramhdr parm, *phdr;
6152 struct sctp_chunkhdr *hdr;
6153 struct sctp_tmit_chunk *chk;
6154 uint16_t ptype, plen;
6155 /* First find the cookie in the param area */
6157 at = offset + sizeof(struct sctp_init_chunk);
6160 phdr = sctp_get_next_param(m, at, &parm, sizeof(parm));
6164 ptype = ntohs(phdr->param_type);
6165 plen = ntohs(phdr->param_length);
6166 if (ptype == SCTP_STATE_COOKIE) {
6168 /* found the cookie */
6169 if ((pad = (plen % 4))) {
6172 cookie = sctp_m_copym(m, at, plen, MB_DONTWAIT);
6173 if (cookie == NULL) {
6179 at += SCTP_SIZE32(plen);
6181 if (cookie == NULL) {
6182 /* Did not find the cookie */
6185 /* ok, we got the cookie lets change it into a cookie echo chunk */
6187 /* first the change from param to cookie */
6188 hdr = mtod(cookie, struct sctp_chunkhdr *);
6189 hdr->chunk_type = SCTP_COOKIE_ECHO;
6190 hdr->chunk_flags = 0;
6191 /* now we MUST have a PKTHDR on it */
6192 if ((cookie->m_flags & M_PKTHDR) != M_PKTHDR) {
6193 /* we hope this happens rarely */
6194 MGETHDR(mat, MB_DONTWAIT, MT_HEADER);
6196 sctp_m_freem(cookie);
6200 mat->m_pkthdr.rcvif = 0;
6201 mat->m_next = cookie;
6204 cookie->m_pkthdr.len = plen;
6205 /* get the chunk stuff now and place it in the FRONT of the queue */
6206 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
6209 sctp_m_freem(cookie);
6212 sctppcbinfo.ipi_count_chunk++;
6213 sctppcbinfo.ipi_gencnt_chunk++;
6214 chk->send_size = cookie->m_pkthdr.len;
6215 chk->rec.chunk_id = SCTP_COOKIE_ECHO;
6216 chk->sent = SCTP_DATAGRAM_UNSENT;
6219 chk->asoc = &stcb->asoc;
6221 chk->whoTo = chk->asoc->primary_destination;
6222 chk->whoTo->ref_count++;
6223 TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
6224 chk->asoc->ctrl_queue_cnt++;
6229 sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
6233 struct sctp_nets *net)
6235 /* take a HB request and make it into a
6236 * HB ack and send it.
6238 struct mbuf *outchain;
6239 struct sctp_chunkhdr *chdr;
6240 struct sctp_tmit_chunk *chk;
6244 /* must have a net pointer */
6247 outchain = sctp_m_copym(m, offset, chk_length, MB_DONTWAIT);
6248 if (outchain == NULL) {
6249 /* gak out of memory */
6252 chdr = mtod(outchain, struct sctp_chunkhdr *);
6253 chdr->chunk_type = SCTP_HEARTBEAT_ACK;
6254 chdr->chunk_flags = 0;
6255 if ((outchain->m_flags & M_PKTHDR) != M_PKTHDR) {
6256 /* should not happen but we are cautious. */
6258 MGETHDR(tmp, MB_DONTWAIT, MT_HEADER);
6263 tmp->m_pkthdr.rcvif = 0;
6264 tmp->m_next = outchain;
6267 outchain->m_pkthdr.len = chk_length;
6268 if (chk_length % 4) {
6272 padlen = 4 - (outchain->m_pkthdr.len % 4);
6273 m_copyback(outchain, outchain->m_pkthdr.len, padlen, (caddr_t)&cpthis);
6275 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
6278 sctp_m_freem(outchain);
6281 sctppcbinfo.ipi_count_chunk++;
6282 sctppcbinfo.ipi_gencnt_chunk++;
6284 chk->send_size = chk_length;
6285 chk->rec.chunk_id = SCTP_HEARTBEAT_ACK;
6286 chk->sent = SCTP_DATAGRAM_UNSENT;
6289 chk->asoc = &stcb->asoc;
6290 chk->data = outchain;
6292 chk->whoTo->ref_count++;
6293 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
6294 chk->asoc->ctrl_queue_cnt++;
6298 sctp_send_cookie_ack(struct sctp_tcb *stcb) {
6299 /* formulate and queue a cookie-ack back to sender */
6300 struct mbuf *cookie_ack;
6301 struct sctp_chunkhdr *hdr;
6302 struct sctp_tmit_chunk *chk;
6305 MGETHDR(cookie_ack, MB_DONTWAIT, MT_HEADER);
6306 if (cookie_ack == NULL) {
6310 cookie_ack->m_data += SCTP_MIN_OVERHEAD;
6311 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
6314 sctp_m_freem(cookie_ack);
6317 sctppcbinfo.ipi_count_chunk++;
6318 sctppcbinfo.ipi_gencnt_chunk++;
6320 chk->send_size = sizeof(struct sctp_chunkhdr);
6321 chk->rec.chunk_id = SCTP_COOKIE_ACK;
6322 chk->sent = SCTP_DATAGRAM_UNSENT;
6325 chk->asoc = &stcb->asoc;
6326 chk->data = cookie_ack;
6327 if (chk->asoc->last_control_chunk_from != NULL) {
6328 chk->whoTo = chk->asoc->last_control_chunk_from;
6330 chk->whoTo = chk->asoc->primary_destination;
6332 chk->whoTo->ref_count++;
6333 hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
6334 hdr->chunk_type = SCTP_COOKIE_ACK;
6335 hdr->chunk_flags = 0;
6336 hdr->chunk_length = htons(chk->send_size);
6337 cookie_ack->m_pkthdr.len = cookie_ack->m_len = chk->send_size;
6338 cookie_ack->m_pkthdr.rcvif = 0;
6339 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
6340 chk->asoc->ctrl_queue_cnt++;
6346 sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
6348 /* formulate and queue a SHUTDOWN-ACK back to the sender */
6349 struct mbuf *m_shutdown_ack;
6350 struct sctp_shutdown_ack_chunk *ack_cp;
6351 struct sctp_tmit_chunk *chk;
6353 m_shutdown_ack = NULL;
6354 MGETHDR(m_shutdown_ack, MB_DONTWAIT, MT_HEADER);
6355 if (m_shutdown_ack == NULL) {
6359 m_shutdown_ack->m_data += SCTP_MIN_OVERHEAD;
6360 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
6363 sctp_m_freem(m_shutdown_ack);
6366 sctppcbinfo.ipi_count_chunk++;
6367 sctppcbinfo.ipi_gencnt_chunk++;
6369 chk->send_size = sizeof(struct sctp_chunkhdr);
6370 chk->rec.chunk_id = SCTP_SHUTDOWN_ACK;
6371 chk->sent = SCTP_DATAGRAM_UNSENT;
6374 chk->asoc = &stcb->asoc;
6375 chk->data = m_shutdown_ack;
6379 ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
6380 ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
6381 ack_cp->ch.chunk_flags = 0;
6382 ack_cp->ch.chunk_length = htons(chk->send_size);
6383 m_shutdown_ack->m_pkthdr.len = m_shutdown_ack->m_len = chk->send_size;
6384 m_shutdown_ack->m_pkthdr.rcvif = 0;
6385 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
6386 chk->asoc->ctrl_queue_cnt++;
6391 sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
6393 /* formulate and queue a SHUTDOWN to the sender */
6394 struct mbuf *m_shutdown;
6395 struct sctp_shutdown_chunk *shutdown_cp;
6396 struct sctp_tmit_chunk *chk;
6399 MGETHDR(m_shutdown, MB_DONTWAIT, MT_HEADER);
6400 if (m_shutdown == NULL) {
6404 m_shutdown->m_data += SCTP_MIN_OVERHEAD;
6405 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
6408 sctp_m_freem(m_shutdown);
6411 sctppcbinfo.ipi_count_chunk++;
6412 sctppcbinfo.ipi_gencnt_chunk++;
6414 chk->send_size = sizeof(struct sctp_shutdown_chunk);
6415 chk->rec.chunk_id = SCTP_SHUTDOWN;
6416 chk->sent = SCTP_DATAGRAM_UNSENT;
6419 chk->asoc = &stcb->asoc;
6420 chk->data = m_shutdown;
6424 shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
6425 shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
6426 shutdown_cp->ch.chunk_flags = 0;
6427 shutdown_cp->ch.chunk_length = htons(chk->send_size);
6428 shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
6429 m_shutdown->m_pkthdr.len = m_shutdown->m_len = chk->send_size;
6430 m_shutdown->m_pkthdr.rcvif = 0;
6431 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
6432 chk->asoc->ctrl_queue_cnt++;
6434 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6435 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
6436 stcb->sctp_ep->sctp_socket->so_snd.ssb_cc = 0;
6437 soisdisconnecting(stcb->sctp_ep->sctp_socket);
6443 sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net)
6446 * formulate and queue an ASCONF to the peer
6447 * ASCONF parameters should be queued on the assoc queue
6449 struct sctp_tmit_chunk *chk;
6450 struct mbuf *m_asconf;
6452 /* compose an ASCONF chunk, maximum length is PMTU */
6453 m_asconf = sctp_compose_asconf(stcb);
6454 if (m_asconf == NULL) {
6457 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
6460 sctp_m_freem(m_asconf);
6463 sctppcbinfo.ipi_count_chunk++;
6464 sctppcbinfo.ipi_gencnt_chunk++;
6466 chk->data = m_asconf;
6467 chk->send_size = m_asconf->m_pkthdr.len;
6468 chk->rec.chunk_id = SCTP_ASCONF;
6469 chk->sent = SCTP_DATAGRAM_UNSENT;
6472 chk->asoc = &stcb->asoc;
6473 chk->whoTo = chk->asoc->primary_destination;
6474 chk->whoTo->ref_count++;
6475 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
6476 chk->asoc->ctrl_queue_cnt++;
6481 sctp_send_asconf_ack(struct sctp_tcb *stcb, uint32_t retrans)
6484 * formulate and queue a asconf-ack back to sender
6485 * the asconf-ack must be stored in the tcb
6487 struct sctp_tmit_chunk *chk;
6490 /* is there a asconf-ack mbuf chain to send? */
6491 if (stcb->asoc.last_asconf_ack_sent == NULL) {
6495 /* copy the asconf_ack */
6496 #if defined(__FreeBSD__) || defined(__NetBSD__)
6497 /* Supposedly the m_copypacket is a optimzation,
6500 if (stcb->asoc.last_asconf_ack_sent->m_flags & M_PKTHDR) {
6501 m_ack = m_copypacket(stcb->asoc.last_asconf_ack_sent, MB_DONTWAIT);
6502 sctp_pegs[SCTP_CACHED_SRC]++;
6504 m_ack = m_copy(stcb->asoc.last_asconf_ack_sent, 0, M_COPYALL);
6506 m_ack = m_copy(stcb->asoc.last_asconf_ack_sent, 0, M_COPYALL);
6508 if (m_ack == NULL) {
6509 /* couldn't copy it */
6513 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
6517 sctp_m_freem(m_ack);
6520 sctppcbinfo.ipi_count_chunk++;
6521 sctppcbinfo.ipi_gencnt_chunk++;
6523 /* figure out where it goes to */
6525 /* we're doing a retransmission */
6526 if (stcb->asoc.used_alt_asconfack > 2) {
6527 /* tried alternate nets already, go back */
6530 /* need to try and alternate net */
6531 chk->whoTo = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from);
6532 stcb->asoc.used_alt_asconfack++;
6534 if (chk->whoTo == NULL) {
6536 if (stcb->asoc.last_control_chunk_from == NULL)
6537 chk->whoTo = stcb->asoc.primary_destination;
6539 chk->whoTo = stcb->asoc.last_control_chunk_from;
6540 stcb->asoc.used_alt_asconfack = 0;
6544 if (stcb->asoc.last_control_chunk_from == NULL)
6545 chk->whoTo = stcb->asoc.primary_destination;
6547 chk->whoTo = stcb->asoc.last_control_chunk_from;
6548 stcb->asoc.used_alt_asconfack = 0;
6551 chk->send_size = m_ack->m_pkthdr.len;
6552 chk->rec.chunk_id = SCTP_ASCONF_ACK;
6553 chk->sent = SCTP_DATAGRAM_UNSENT;
6556 chk->asoc = &stcb->asoc;
6557 chk->whoTo->ref_count++;
6558 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
6559 chk->asoc->ctrl_queue_cnt++;
6565 sctp_chunk_retransmission(struct sctp_inpcb *inp,
6566 struct sctp_tcb *stcb,
6567 struct sctp_association *asoc,
6568 int *cnt_out, struct timeval *now, int *now_filled)
6571 * send out one MTU of retransmission.
6572 * If fast_retransmit is happening we ignore the cwnd.
6573 * Otherwise we obey the cwnd and rwnd.
6574 * For a Cookie or Asconf in the control chunk queue we retransmit
6575 * them by themselves.
6577 * For data chunks we will pick out the lowest TSN's in the
6578 * sent_queue marked for resend and bundle them all together
6579 * (up to a MTU of destination). The address to send to should
6580 * have been selected/changed where the retransmission was
6581 * marked (i.e. in FR or t3-timeout routines).
6583 struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
6584 struct sctp_tmit_chunk *chk, *fwd;
6586 struct sctphdr *shdr;
6588 struct sctp_nets *net;
6589 int no_fragmentflg, bundle_at, cnt_thru;
6591 int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
6593 tmr_started = ctl_cnt = bundle_at = error = 0;
6600 #ifdef SCTP_AUDITING_ENABLED
6601 sctp_audit_log(0xC3, 1);
6603 if (TAILQ_EMPTY(&asoc->sent_queue)) {
6605 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
6606 kprintf("SCTP hits empty queue with cnt set to %d?\n",
6607 asoc->sent_queue_retran_cnt);
6610 asoc->sent_queue_cnt = 0;
6611 asoc->sent_queue_cnt_removeable = 0;
6613 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
6614 if (chk->sent != SCTP_DATAGRAM_RESEND) {
6615 /* we only worry about things marked for resend */
6618 if ((chk->rec.chunk_id == SCTP_COOKIE_ECHO) ||
6619 (chk->rec.chunk_id == SCTP_ASCONF) ||
6620 (chk->rec.chunk_id == SCTP_STREAM_RESET) ||
6621 (chk->rec.chunk_id == SCTP_FORWARD_CUM_TSN)) {
6622 if (chk->rec.chunk_id == SCTP_STREAM_RESET) {
6623 /* For stream reset we only retran the request
6626 struct sctp_stream_reset_req *strreq;
6627 strreq = mtod(chk->data, struct sctp_stream_reset_req *);
6628 if (strreq->sr_req.ph.param_type != ntohs(SCTP_STR_RESET_REQUEST)) {
6633 if (chk->rec.chunk_id == SCTP_ASCONF) {
6637 if (chk->rec.chunk_id == SCTP_FORWARD_CUM_TSN) {
6641 m = sctp_copy_mbufchain(chk->data, m);
6647 /* do we have control chunks to retransmit? */
6649 /* Start a timer no matter if we suceed or fail */
6650 if (chk->rec.chunk_id == SCTP_COOKIE_ECHO) {
6651 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
6652 } else if (chk->rec.chunk_id == SCTP_ASCONF)
6653 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
6655 if (m->m_len == 0) {
6656 /* Special case for when you get a 0 len
6657 * mbuf at the head due to the lack
6658 * of a MHDR at the beginning.
6660 m->m_len = sizeof(struct sctphdr);
6662 M_PREPEND(m, sizeof(struct sctphdr), MB_DONTWAIT);
6667 shdr = mtod(m, struct sctphdr *);
6668 shdr->src_port = inp->sctp_lport;
6669 shdr->dest_port = stcb->rport;
6670 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
6672 chk->snd_count++; /* update our count */
6674 if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
6675 (struct sockaddr *)&chk->whoTo->ro._l_addr, m,
6676 no_fragmentflg, 0, NULL, asconf))) {
6677 sctp_pegs[SCTP_DATA_OUT_ERR]++;
6681 *We don't want to mark the net->sent time here since this
6682 * we use this for HB and retrans cannot measure RTT
6684 /* SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time);*/
6686 chk->sent = SCTP_DATAGRAM_SENT;
6687 asoc->sent_queue_retran_cnt--;
6688 if (asoc->sent_queue_retran_cnt < 0) {
6689 asoc->sent_queue_retran_cnt = 0;
6694 /* Clean up the fwd-tsn list */
6695 sctp_clean_up_ctl (asoc);
6699 /* Ok, it is just data retransmission we need to do or
6700 * that and a fwd-tsn with it all.
6702 if (TAILQ_EMPTY(&asoc->sent_queue)) {
6706 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
6707 kprintf("Normal chunk retransmission cnt:%d\n",
6708 asoc->sent_queue_retran_cnt);
6711 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) ||
6712 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT)) {
6713 /* not yet open, resend the cookie and that is it */
6718 #ifdef SCTP_AUDITING_ENABLED
6719 sctp_auditing(20, inp, stcb, NULL);
6721 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
6722 if (chk->sent != SCTP_DATAGRAM_RESEND) {
6723 /* No, not sent to this net or not ready for rtx */
6727 /* pick up the net */
6729 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
6730 mtu = (net->mtu - SCTP_MIN_OVERHEAD);
6732 mtu = net->mtu- SCTP_MIN_V4_OVERHEAD;
6735 if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
6736 /* No room in peers rwnd */
6738 tsn = asoc->last_acked_seq + 1;
6739 if (tsn == chk->rec.data.TSN_seq) {
6740 /* we make a special exception for this case.
6741 * The peer has no rwnd but is missing the
6742 * lowest chunk.. which is probably what is
6743 * holding up the rwnd.
6745 goto one_chunk_around;
6748 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
6749 kprintf("blocked-peers_rwnd:%d tf:%d\n",
6750 (int)asoc->peers_rwnd,
6751 (int)asoc->total_flight);
6754 sctp_pegs[SCTP_RWND_BLOCKED]++;
6758 if (asoc->peers_rwnd < mtu) {
6761 #ifdef SCTP_AUDITING_ENABLED
6762 sctp_audit_log(0xC3, 2);
6766 net->fast_retran_ip = 0;
6767 if (chk->rec.data.doing_fast_retransmit == 0) {
6768 /* if no FR in progress skip destination that
6769 * have flight_size > cwnd.
6771 if (net->flight_size >= net->cwnd) {
6772 sctp_pegs[SCTP_CWND_BLOCKED]++;
6776 /* Mark the destination net to have FR recovery
6779 net->fast_retran_ip = 1;
6782 if ((chk->send_size <= mtu) || (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
6783 /* ok we will add this one */
6784 m = sctp_copy_mbufchain(chk->data, m);
6788 /* upate our MTU size */
6789 /* Do clear IP_DF ? */
6790 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
6793 mtu -= chk->send_size;
6794 data_list[bundle_at++] = chk;
6795 if (one_chunk && (asoc->total_flight <= 0)) {
6796 sctp_pegs[SCTP_WINDOW_PROBES]++;
6797 chk->rec.data.state_flags |= SCTP_WINDOW_PROBE;
6800 if (one_chunk == 0) {
6801 /* now are there anymore forward from chk to pick up?*/
6802 fwd = TAILQ_NEXT(chk, sctp_next);
6804 if (fwd->sent != SCTP_DATAGRAM_RESEND) {
6805 /* Nope, not for retran */
6806 fwd = TAILQ_NEXT(fwd, sctp_next);
6809 if (fwd->whoTo != net) {
6810 /* Nope, not the net in question */
6811 fwd = TAILQ_NEXT(fwd, sctp_next);
6814 if (fwd->send_size <= mtu) {
6815 m = sctp_copy_mbufchain(fwd->data, m);
6819 /* upate our MTU size */
6820 /* Do clear IP_DF ? */
6821 if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
6824 mtu -= fwd->send_size;
6825 data_list[bundle_at++] = fwd;
6826 if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
6829 fwd = TAILQ_NEXT(fwd, sctp_next);
6831 /* can't fit so we are done */
6836 /* Is there something to send for this destination? */
6838 /* No matter if we fail/or suceed we should
6839 * start a timer. A failure is like a lost
6842 if (!callout_pending(&net->rxt_timer.timer)) {
6843 /* no timer running on this destination
6846 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
6849 if (m->m_len == 0) {
6850 /* Special case for when you get a 0 len
6851 * mbuf at the head due to the lack
6852 * of a MHDR at the beginning.
6854 m->m_len = sizeof(struct sctphdr);
6856 M_PREPEND(m, sizeof(struct sctphdr), MB_DONTWAIT);
6861 shdr = mtod(m, struct sctphdr *);
6862 shdr->src_port = inp->sctp_lport;
6863 shdr->dest_port = stcb->rport;
6864 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
6867 /* Now lets send it, if there is anything to send :> */
6868 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
6869 (struct sockaddr *)&net->ro._l_addr,
6871 no_fragmentflg, 0, NULL, asconf))) {
6872 /* error, we could not output */
6873 sctp_pegs[SCTP_DATA_OUT_ERR]++;
6878 * We don't want to mark the net->sent time here since
6879 * this we use this for HB and retrans cannot measure
6882 /* SCTP_GETTIME_TIMEVAL(&net->last_sent_time);*/
6884 /* For auto-close */
6886 if (*now_filled == 0) {
6887 SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
6888 *now = asoc->time_last_sent;
6891 asoc->time_last_sent = *now;
6893 *cnt_out += bundle_at;
6894 #ifdef SCTP_AUDITING_ENABLED
6895 sctp_audit_log(0xC4, bundle_at);
6897 for (i = 0; i < bundle_at; i++) {
6898 sctp_pegs[SCTP_RETRANTSN_SENT]++;
6899 data_list[i]->sent = SCTP_DATAGRAM_SENT;
6900 data_list[i]->snd_count++;
6901 asoc->sent_queue_retran_cnt--;
6902 /* record the time */
6903 data_list[i]->sent_rcv_time = asoc->time_last_sent;
6904 if (asoc->sent_queue_retran_cnt < 0) {
6905 asoc->sent_queue_retran_cnt = 0;
6907 net->flight_size += data_list[i]->book_size;
6908 asoc->total_flight += data_list[i]->book_size;
6909 asoc->total_flight_count++;
6911 #ifdef SCTP_LOG_RWND
6912 sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
6913 asoc->peers_rwnd , data_list[i]->send_size, sctp_peer_chunk_oh);
6915 asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
6916 (u_int32_t)(data_list[i]->send_size + sctp_peer_chunk_oh));
6917 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
6918 /* SWS sender side engages */
6919 asoc->peers_rwnd = 0;
6923 (data_list[i]->rec.data.doing_fast_retransmit)) {
6924 sctp_pegs[SCTP_FAST_RETRAN]++;
6925 if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
6926 (tmr_started == 0)) {
6928 * ok we just fast-retrans'd
6929 * the lowest TSN, i.e the
6930 * first on the list. In this
6931 * case we want to give some
6932 * more time to get a SACK
6933 * back without a t3-expiring.
6935 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
6936 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
6940 #ifdef SCTP_AUDITING_ENABLED
6941 sctp_auditing(21, inp, stcb, NULL);
6947 if (asoc->sent_queue_retran_cnt <= 0) {
6948 /* all done we have no more to retran */
6949 asoc->sent_queue_retran_cnt = 0;
6953 /* No more room in rwnd */
6956 /* stop the for loop here. we sent out a packet */
6964 sctp_timer_validation(struct sctp_inpcb *inp,
6965 struct sctp_tcb *stcb,
6966 struct sctp_association *asoc,
6969 struct sctp_nets *net;
6970 /* Validate that a timer is running somewhere */
6971 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
6972 if (callout_pending(&net->rxt_timer.timer)) {
6973 /* Here is a timer */
6977 /* Gak, we did not have a timer somewhere */
6979 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
6980 kprintf("Deadlock avoided starting timer on a dest at retran\n");
6983 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
6988 sctp_chunk_output(struct sctp_inpcb *inp,
6989 struct sctp_tcb *stcb,
6992 /* Ok this is the generic chunk service queue.
6993 * we must do the following:
6994 * - See if there are retransmits pending, if so we
6995 * must do these first and return.
6996 * - Service the stream queue that is next,
6997 * moving any message (note I must get a complete
6998 * message i.e. FIRST/MIDDLE and LAST to the out
6999 * queue in one pass) and assigning TSN's
7000 * - Check to see if the cwnd/rwnd allows any output, if
7001 * so we go ahead and fomulate and send the low level
7002 * chunks. Making sure to combine any control in the
7003 * control chunk queue also.
7005 struct sctp_association *asoc;
7006 struct sctp_nets *net;
7007 int error, num_out, tot_out, ret, reason_code, burst_cnt, burst_limit;
7015 sctp_pegs[SCTP_CALLS_TO_CO]++;
7017 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
7018 kprintf("in co - retran count:%d\n", asoc->sent_queue_retran_cnt);
7021 while (asoc->sent_queue_retran_cnt) {
7022 /* Ok, it is retransmission time only, we send out only ONE
7023 * packet with a single call off to the retran code.
7025 ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled);
7027 /* Can't send anymore */
7029 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7030 kprintf("retransmission ret:%d -- full\n", ret);
7034 * now lets push out control by calling med-level
7035 * output once. this assures that we WILL send HB's
7038 sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
7039 &cwnd_full, from_where,
7042 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7043 kprintf("Control send outputs:%d@full\n", num_out);
7046 #ifdef SCTP_AUDITING_ENABLED
7047 sctp_auditing(8, inp, stcb, NULL);
7049 return (sctp_timer_validation(inp, stcb, asoc, ret));
7053 * The count was off.. retran is not happening so do
7054 * the normal retransmission.
7057 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7058 kprintf("Done with retrans, none left fill up window\n");
7061 #ifdef SCTP_AUDITING_ENABLED
7062 sctp_auditing(9, inp, stcb, NULL);
7066 if (from_where == 1) {
7067 /* Only one transmission allowed out of a timeout */
7069 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7070 kprintf("Only one packet allowed out\n");
7073 #ifdef SCTP_AUDITING_ENABLED
7074 sctp_auditing(10, inp, stcb, NULL);
7076 /* Push out any control */
7077 sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, &cwnd_full, from_where,
7081 if ((num_out == 0) && (ret == 0)) {
7082 /* No more retrans to send */
7086 #ifdef SCTP_AUDITING_ENABLED
7087 sctp_auditing(12, inp, stcb, NULL);
7089 /* Check for bad destinations, if they exist move chunks around. */
7090 burst_limit = asoc->max_burst;
7091 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7092 if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) ==
7093 SCTP_ADDR_NOT_REACHABLE) {
7095 * if possible move things off of this address
7096 * we still may send below due to the dormant state
7097 * but we try to find an alternate address to send
7098 * to and if we have one we move all queued data on
7099 * the out wheel to this alternate address.
7101 sctp_move_to_an_alt(stcb, asoc, net);
7104 if ((asoc->sat_network) || (net->addr_is_local)) {
7105 burst_limit = asoc->max_burst * SCTP_SAT_NETWORK_BURST_INCR;
7109 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
7110 kprintf("examined net:%p burst limit:%d\n", net, asoc->max_burst);
7114 #ifdef SCTP_USE_ALLMAN_BURST
7115 if ((net->flight_size+(burst_limit*net->mtu)) < net->cwnd) {
7116 if (net->ssthresh < net->cwnd)
7117 net->ssthresh = net->cwnd;
7118 net->cwnd = (net->flight_size+(burst_limit*net->mtu));
7119 #ifdef SCTP_LOG_MAXBURST
7120 sctp_log_maxburst(net, 0, burst_limit, SCTP_MAX_BURST_APPLIED);
7122 sctp_pegs[SCTP_MAX_BURST_APL]++;
7124 net->fast_retran_ip = 0;
7129 /* Fill up what we can to the destination */
7134 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
7135 kprintf("Burst count:%d - call m-c-o\n", burst_cnt);
7138 error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
7139 &reason_code, 0, &cwnd_full, from_where,
7143 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7144 kprintf("Error %d was returned from med-c-op\n", error);
7147 #ifdef SCTP_LOG_MAXBURST
7148 sctp_log_maxburst(asoc->primary_destination, error , burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
7153 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
7154 kprintf("m-c-o put out %d\n", num_out);
7160 #ifndef SCTP_USE_ALLMAN_BURST
7161 && (burst_cnt < burst_limit)
7164 #ifndef SCTP_USE_ALLMAN_BURST
7165 if (burst_cnt >= burst_limit) {
7166 sctp_pegs[SCTP_MAX_BURST_APL]++;
7167 asoc->burst_limit_applied = 1;
7168 #ifdef SCTP_LOG_MAXBURST
7169 sctp_log_maxburst(asoc->primary_destination, 0 , burst_cnt, SCTP_MAX_BURST_APPLIED);
7172 asoc->burst_limit_applied = 0;
7177 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7178 kprintf("Ok, we have put out %d chunks\n", tot_out);
7182 sctp_pegs[SCTP_CO_NODATASNT]++;
7183 if (asoc->stream_queue_cnt > 0) {
7184 sctp_pegs[SCTP_SOS_NOSNT]++;
7186 sctp_pegs[SCTP_NOS_NOSNT]++;
7188 if (asoc->send_queue_cnt > 0) {
7189 sctp_pegs[SCTP_SOSE_NOSNT]++;
7191 sctp_pegs[SCTP_NOSE_NOSNT]++;
7194 /* Now we need to clean up the control chunk chain if
7195 * a ECNE is on it. It must be marked as UNSENT again
7196 * so next call will continue to send it until
7197 * such time that we get a CWR, to remove it.
7199 sctp_fix_ecn_echo(asoc);
7205 sctp_output(struct sctp_inpcb *inp, struct mbuf *m, struct sockaddr *addr,
7206 struct mbuf *control, struct thread *p, int flags)
7208 struct sctp_inpcb *t_inp;
7209 struct sctp_tcb *stcb;
7210 struct sctp_nets *net;
7211 struct sctp_association *asoc;
7212 int create_lock_applied = 0;
7213 int queue_only, error = 0;
7214 struct sctp_sndrcvinfo srcv;
7216 int use_rcvinfo = 0;
7218 /* struct route ro;*/
7227 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7228 kprintf("USR Send BEGINS\n");
7232 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
7233 (inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING)) {
7234 /* The listner can NOT send */
7236 sctppcbinfo.mbuf_track--;
7237 sctp_m_freem(control);
7244 /* Can't allow a V6 address on a non-v6 socket */
7246 SCTP_ASOC_CREATE_LOCK(inp);
7247 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
7248 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
7249 /* Should I really unlock ? */
7250 SCTP_ASOC_CREATE_UNLOCK(inp);
7252 sctppcbinfo.mbuf_track--;
7253 sctp_m_freem(control);
7260 create_lock_applied = 1;
7261 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
7262 (addr->sa_family == AF_INET6)) {
7263 SCTP_ASOC_CREATE_UNLOCK(inp);
7265 sctppcbinfo.mbuf_track--;
7266 sctp_m_freem(control);
7275 sctppcbinfo.mbuf_track++;
7276 if (sctp_find_cmsg(SCTP_SNDRCV, &srcv, control,
7278 if (srcv.sinfo_flags & MSG_SENDALL) {
7280 sctppcbinfo.mbuf_track--;
7281 sctp_m_freem(control);
7283 if (create_lock_applied) {
7284 SCTP_ASOC_CREATE_UNLOCK(inp);
7285 create_lock_applied = 0;
7287 return (sctp_sendall(inp, NULL, m, &srcv));
7289 if (srcv.sinfo_assoc_id) {
7290 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
7291 SCTP_INP_RLOCK(inp);
7292 stcb = LIST_FIRST(&inp->sctp_asoc_list);
7294 SCTP_TCB_LOCK(stcb);
7295 SCTP_INP_RUNLOCK(inp);
7298 if (create_lock_applied) {
7299 SCTP_ASOC_CREATE_UNLOCK(inp);
7300 create_lock_applied = 0;
7302 sctppcbinfo.mbuf_track--;
7303 sctp_m_freem(control);
7308 net = stcb->asoc.primary_destination;
7310 stcb = sctp_findassociation_ep_asocid(inp, srcv.sinfo_assoc_id);
7313 * Question: Should I error here if the
7315 * assoc_id is no longer valid?
7316 * i.e. I can't find it?
7320 /* Must locate the net structure */
7322 net = sctp_findnet(stcb, addr);
7325 net = stcb->asoc.primary_destination;
7331 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
7332 SCTP_INP_RLOCK(inp);
7333 stcb = LIST_FIRST(&inp->sctp_asoc_list);
7335 SCTP_TCB_LOCK(stcb);
7336 SCTP_INP_RUNLOCK(inp);
7339 if (create_lock_applied) {
7340 SCTP_ASOC_CREATE_UNLOCK(inp);
7341 create_lock_applied = 0;
7344 sctppcbinfo.mbuf_track--;
7345 sctp_m_freem(control);
7352 net = stcb->asoc.primary_destination;
7354 net = sctp_findnet(stcb, addr);
7356 net = stcb->asoc.primary_destination;
7361 SCTP_INP_WLOCK(inp);
7362 SCTP_INP_INCR_REF(inp);
7363 SCTP_INP_WUNLOCK(inp);
7364 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
7366 SCTP_INP_WLOCK(inp);
7367 SCTP_INP_DECR_REF(inp);
7368 SCTP_INP_WUNLOCK(inp);
7373 if ((stcb == NULL) &&
7374 (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) {
7376 sctppcbinfo.mbuf_track--;
7377 sctp_m_freem(control);
7380 if (create_lock_applied) {
7381 SCTP_ASOC_CREATE_UNLOCK(inp);
7382 create_lock_applied = 0;
7387 } else if ((stcb == NULL) &&
7390 sctppcbinfo.mbuf_track--;
7391 sctp_m_freem(control);
7394 if (create_lock_applied) {
7395 SCTP_ASOC_CREATE_UNLOCK(inp);
7396 create_lock_applied = 0;
7401 } else if (stcb == NULL) {
7402 /* UDP mode, we must go ahead and start the INIT process */
7403 if ((use_rcvinfo) && (srcv.sinfo_flags & MSG_ABORT)) {
7404 /* Strange user to do this */
7406 sctppcbinfo.mbuf_track--;
7407 sctp_m_freem(control);
7410 if (create_lock_applied) {
7411 SCTP_ASOC_CREATE_UNLOCK(inp);
7412 create_lock_applied = 0;
7418 stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0);
7421 sctppcbinfo.mbuf_track--;
7422 sctp_m_freem(control);
7425 if (create_lock_applied) {
7426 SCTP_ASOC_CREATE_UNLOCK(inp);
7427 create_lock_applied = 0;
7433 if (create_lock_applied) {
7434 SCTP_ASOC_CREATE_UNLOCK(inp);
7435 create_lock_applied = 0;
7437 kprintf("Huh-1, create lock should have been applied!\n");
7441 asoc->state = SCTP_STATE_COOKIE_WAIT;
7442 SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
7444 /* see if a init structure exists in cmsg headers */
7445 struct sctp_initmsg initm;
7447 if (sctp_find_cmsg(SCTP_INIT, &initm, control,
7449 /* we have an INIT override of the default */
7450 if (initm.sinit_max_attempts)
7451 asoc->max_init_times = initm.sinit_max_attempts;
7452 if (initm.sinit_num_ostreams)
7453 asoc->pre_open_streams = initm.sinit_num_ostreams;
7454 if (initm.sinit_max_instreams)
7455 asoc->max_inbound_streams = initm.sinit_max_instreams;
7456 if (initm.sinit_max_init_timeo)
7457 asoc->initial_init_rto_max = initm.sinit_max_init_timeo;
7459 if (asoc->streamoutcnt < asoc->pre_open_streams) {
7460 /* Default is NOT correct */
7462 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7463 kprintf("Ok, defout:%d pre_open:%d\n",
7464 asoc->streamoutcnt, asoc->pre_open_streams);
7467 kfree(asoc->strmout, M_PCB);
7468 asoc->strmout = NULL;
7469 asoc->streamoutcnt = asoc->pre_open_streams;
7470 asoc->strmout = kmalloc(asoc->streamoutcnt * sizeof(struct sctp_stream_out),
7472 for (i = 0; i < asoc->streamoutcnt; i++) {
7474 * inbound side must be set to 0xffff,
7475 * also NOTE when we get the INIT-ACK
7476 * back (for INIT sender) we MUST
7477 * reduce the count (streamoutcnt) but
7478 * first check if we sent to any of the
7479 * upper streams that were dropped (if
7480 * some were). Those that were dropped
7481 * must be notified to the upper layer
7482 * as failed to send.
7484 asoc->strmout[i].next_sequence_sent = 0x0;
7485 TAILQ_INIT(&asoc->strmout[i].outqueue);
7486 asoc->strmout[i].stream_no = i;
7487 asoc->strmout[i].next_spoke.tqe_next = 0;
7488 asoc->strmout[i].next_spoke.tqe_prev = 0;
7492 sctp_send_initiate(inp, stcb);
7494 * we may want to dig in after this call and adjust the MTU
7495 * value. It defaulted to 1500 (constant) but the ro structure
7496 * may now have an update and thus we may need to change it
7497 * BEFORE we append the message.
7499 net = stcb->asoc.primary_destination;
7501 if (create_lock_applied) {
7502 SCTP_ASOC_CREATE_UNLOCK(inp);
7503 create_lock_applied = 0;
7506 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
7507 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
7510 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
7511 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
7512 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
7513 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
7515 sctppcbinfo.mbuf_track--;
7516 sctp_m_freem(control);
7519 if ((use_rcvinfo) &&
7520 (srcv.sinfo_flags & MSG_ABORT)) {
7521 sctp_msg_append(stcb, net, m, &srcv, flags);
7529 SCTP_TCB_UNLOCK(stcb);
7533 if (create_lock_applied) {
7534 /* we should never hit here with the create lock applied
7537 SCTP_ASOC_CREATE_UNLOCK(inp);
7538 create_lock_applied = 0;
7542 if (use_rcvinfo == 0) {
7543 srcv = stcb->asoc.def_send;
7547 if (sctp_debug_on & SCTP_DEBUG_OUTPUT5) {
7548 kprintf("stream:%d\n", srcv.sinfo_stream);
7549 kprintf("flags:%x\n", (u_int)srcv.sinfo_flags);
7550 kprintf("ppid:%d\n", srcv.sinfo_ppid);
7551 kprintf("context:%d\n", srcv.sinfo_context);
7556 sctppcbinfo.mbuf_track--;
7557 sctp_m_freem(control);
7560 if (net && ((srcv.sinfo_flags & MSG_ADDR_OVER))) {
7561 /* we take the override or the unconfirmed */
7564 net = stcb->asoc.primary_destination;
7566 if ((error = sctp_msg_append(stcb, net, m, &srcv, flags))) {
7567 SCTP_TCB_UNLOCK(stcb);
7571 if (net->flight_size > net->cwnd) {
7572 sctp_pegs[SCTP_SENDTO_FULL_CWND]++;
7574 } else if (asoc->ifp_had_enobuf) {
7575 sctp_pegs[SCTP_QUEONLY_BURSTLMT]++;
7578 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
7579 ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) * sizeof(struct sctp_data_chunk)) +
7582 if (((inp->sctp_flags & SCTP_PCB_FLAGS_NODELAY) == 0) &&
7583 (stcb->asoc.total_flight > 0) &&
7584 (un_sent < (int)stcb->asoc.smallest_mtu)
7587 /* Ok, Nagle is set on and we have
7588 * data outstanding. Don't send anything
7589 * and let the SACK drive out the data.
7591 sctp_pegs[SCTP_NAGLE_NOQ]++;
7594 sctp_pegs[SCTP_NAGLE_OFF]++;
7597 if ((queue_only == 0) && stcb->asoc.peers_rwnd) {
7598 /* we can attempt to send too.*/
7600 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7601 kprintf("USR Send calls sctp_chunk_output\n");
7604 #ifdef SCTP_AUDITING_ENABLED
7605 sctp_audit_log(0xC0, 1);
7606 sctp_auditing(6, inp, stcb, net);
7608 sctp_pegs[SCTP_OUTPUT_FRM_SND]++;
7609 sctp_chunk_output(inp, stcb, 0);
7610 #ifdef SCTP_AUDITING_ENABLED
7611 sctp_audit_log(0xC0, 2);
7612 sctp_auditing(7, inp, stcb, net);
7617 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7618 kprintf("USR Send complete qo:%d prw:%d\n", queue_only, stcb->asoc.peers_rwnd);
7621 SCTP_TCB_UNLOCK(stcb);
7627 send_forward_tsn(struct sctp_tcb *stcb,
7628 struct sctp_association *asoc)
7630 struct sctp_tmit_chunk *chk;
7631 struct sctp_forward_tsn_chunk *fwdtsn;
7633 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7634 if (chk->rec.chunk_id == SCTP_FORWARD_CUM_TSN) {
7635 /* mark it to unsent */
7636 chk->sent = SCTP_DATAGRAM_UNSENT;
7638 /* Do we correct its output location? */
7639 if (chk->whoTo != asoc->primary_destination) {
7640 sctp_free_remote_addr(chk->whoTo);
7641 chk->whoTo = asoc->primary_destination;
7642 chk->whoTo->ref_count++;
7644 goto sctp_fill_in_rest;
7647 /* Ok if we reach here we must build one */
7648 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
7652 sctppcbinfo.ipi_count_chunk++;
7653 sctppcbinfo.ipi_gencnt_chunk++;
7654 chk->rec.chunk_id = SCTP_FORWARD_CUM_TSN;
7656 MGETHDR(chk->data, MB_DONTWAIT, MT_DATA);
7657 if (chk->data == NULL) {
7658 chk->whoTo->ref_count--;
7659 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
7660 sctppcbinfo.ipi_count_chunk--;
7661 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
7662 panic("Chunk count is negative");
7664 sctppcbinfo.ipi_gencnt_chunk++;
7667 chk->data->m_data += SCTP_MIN_OVERHEAD;
7668 chk->sent = SCTP_DATAGRAM_UNSENT;
7670 chk->whoTo = asoc->primary_destination;
7671 chk->whoTo->ref_count++;
7672 TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
7673 asoc->ctrl_queue_cnt++;
7675 /* Here we go through and fill out the part that
7676 * deals with stream/seq of the ones we skip.
7678 chk->data->m_pkthdr.len = chk->data->m_len = 0;
7680 struct sctp_tmit_chunk *at, *tp1, *last;
7681 struct sctp_strseq *strseq;
7682 unsigned int cnt_of_space, i, ovh;
7683 unsigned int space_needed;
7684 unsigned int cnt_of_skipped = 0;
7685 TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
7686 if (at->sent != SCTP_FORWARD_TSN_SKIP) {
7687 /* no more to look at */
7690 if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
7691 /* We don't report these */
7696 space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
7697 (cnt_of_skipped * sizeof(struct sctp_strseq)));
7698 if ((M_TRAILINGSPACE(chk->data) < (int)space_needed) &&
7699 ((chk->data->m_flags & M_EXT) == 0)) {
7700 /* Need a M_EXT, get one and move
7701 * fwdtsn to data area.
7703 MCLGET(chk->data, MB_DONTWAIT);
7705 cnt_of_space = M_TRAILINGSPACE(chk->data);
7707 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7708 ovh = SCTP_MIN_OVERHEAD;
7710 ovh = SCTP_MIN_V4_OVERHEAD;
7712 if (cnt_of_space > (asoc->smallest_mtu-ovh)) {
7713 /* trim to a mtu size */
7714 cnt_of_space = asoc->smallest_mtu - ovh;
7716 if (cnt_of_space < space_needed) {
7717 /* ok we must trim down the chunk by lowering
7718 * the advance peer ack point.
7720 cnt_of_skipped = (cnt_of_space-
7721 ((sizeof(struct sctp_forward_tsn_chunk))/
7722 sizeof(struct sctp_strseq)));
7723 /* Go through and find the TSN that
7724 * will be the one we report.
7726 at = TAILQ_FIRST(&asoc->sent_queue);
7727 for (i = 0; i < cnt_of_skipped; i++) {
7728 tp1 = TAILQ_NEXT(at, sctp_next);
7732 /* last now points to last one I can report, update peer ack point */
7733 asoc->advanced_peer_ack_point = last->rec.data.TSN_seq;
7734 space_needed -= (cnt_of_skipped * sizeof(struct sctp_strseq));
7736 chk->send_size = space_needed;
7737 /* Setup the chunk */
7738 fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
7739 fwdtsn->ch.chunk_length = htons(chk->send_size);
7740 fwdtsn->ch.chunk_flags = 0;
7741 fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
7742 fwdtsn->new_cumulative_tsn = htonl(asoc->advanced_peer_ack_point);
7743 chk->send_size = (sizeof(struct sctp_forward_tsn_chunk) +
7744 (cnt_of_skipped * sizeof(struct sctp_strseq)));
7745 chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
7747 /* Move pointer to after the fwdtsn and transfer to
7748 * the strseq pointer.
7750 strseq = (struct sctp_strseq *)fwdtsn;
7752 * Now populate the strseq list. This is done blindly
7753 * without pulling out duplicate stream info. This is
7754 * inefficent but won't harm the process since the peer
7755 * will look at these in sequence and will thus release
7756 * anything. It could mean we exceed the PMTU and chop
7757 * off some that we could have included.. but this is
7758 * unlikely (aka 1432/4 would mean 300+ stream seq's would
7759 * have to be reported in one FWD-TSN. With a bit of work
7760 * we can later FIX this to optimize and pull out duplcates..
7761 * but it does add more overhead. So for now... not!
7763 at = TAILQ_FIRST(&asoc->sent_queue);
7764 for (i = 0; i < cnt_of_skipped; i++) {
7765 tp1 = TAILQ_NEXT(at, sctp_next);
7766 if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
7767 /* We don't report these */
7772 strseq->stream = ntohs(at->rec.data.stream_number);
7773 strseq->sequence = ntohs(at->rec.data.stream_seq);
7783 sctp_send_sack(struct sctp_tcb *stcb)
7786 * Queue up a SACK in the control queue. We must first check to
7787 * see if a SACK is somehow on the control queue. If so, we will
7788 * take and and remove the old one.
7790 struct sctp_association *asoc;
7791 struct sctp_tmit_chunk *chk, *a_chk;
7792 struct sctp_sack_chunk *sack;
7793 struct sctp_gap_ack_block *gap_descriptor;
7796 unsigned int i, maxi, seeing_ones, m_size;
7797 unsigned int num_gap_blocks, space;
7803 if (asoc->last_data_chunk_from == NULL) {
7804 /* Hmm we never received anything */
7807 sctp_set_rwnd(stcb, asoc);
7808 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7809 if (chk->rec.chunk_id == SCTP_SELECTIVE_ACK) {
7810 /* Hmm, found a sack already on queue, remove it */
7811 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
7812 asoc->ctrl_queue_cnt++;
7815 sctp_m_freem(a_chk->data);
7817 sctp_free_remote_addr(a_chk->whoTo);
7818 a_chk->whoTo = NULL;
7822 if (a_chk == NULL) {
7823 a_chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
7824 if (a_chk == NULL) {
7825 /* No memory so we drop the idea, and set a timer */
7826 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
7827 stcb->sctp_ep, stcb, NULL);
7828 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
7829 stcb->sctp_ep, stcb, NULL);
7832 sctppcbinfo.ipi_count_chunk++;
7833 sctppcbinfo.ipi_gencnt_chunk++;
7834 a_chk->rec.chunk_id = SCTP_SELECTIVE_ACK;
7837 a_chk->snd_count = 0;
7838 a_chk->send_size = 0; /* fill in later */
7839 a_chk->sent = SCTP_DATAGRAM_UNSENT;
7840 m_size = (asoc->mapping_array_size << 3);
7842 if ((asoc->numduptsns) ||
7843 (asoc->last_data_chunk_from->dest_state & SCTP_ADDR_NOT_REACHABLE)
7845 /* Ok, we have some duplicates or the destination for the
7846 * sack is unreachable, lets see if we can select an alternate
7847 * than asoc->last_data_chunk_from
7849 if ((!(asoc->last_data_chunk_from->dest_state &
7850 SCTP_ADDR_NOT_REACHABLE)) &&
7851 (asoc->used_alt_onsack > 2)) {
7852 /* We used an alt last time, don't this time */
7853 a_chk->whoTo = NULL;
7855 asoc->used_alt_onsack++;
7856 a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from);
7858 if (a_chk->whoTo == NULL) {
7859 /* Nope, no alternate */
7860 a_chk->whoTo = asoc->last_data_chunk_from;
7861 asoc->used_alt_onsack = 0;
7864 /* No duplicates so we use the last
7865 * place we received data from.
7868 if (asoc->last_data_chunk_from == NULL) {
7869 kprintf("Huh, last_data_chunk_from is null when we want to sack??\n");
7872 asoc->used_alt_onsack = 0;
7873 a_chk->whoTo = asoc->last_data_chunk_from;
7876 a_chk->whoTo->ref_count++;
7878 /* Ok now lets formulate a MBUF with our sack */
7879 MGETHDR(a_chk->data, MB_DONTWAIT, MT_DATA);
7880 if ((a_chk->data == NULL) ||
7881 (a_chk->whoTo == NULL)) {
7882 /* rats, no mbuf memory */
7884 /* was a problem with the destination */
7885 sctp_m_freem(a_chk->data);
7888 a_chk->whoTo->ref_count--;
7889 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, a_chk);
7890 sctppcbinfo.ipi_count_chunk--;
7891 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
7892 panic("Chunk count is negative");
7894 sctppcbinfo.ipi_gencnt_chunk++;
7895 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
7896 stcb->sctp_ep, stcb, NULL);
7897 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
7898 stcb->sctp_ep, stcb, NULL);
7901 /* First count the number of gap ack blocks we need */
7902 if (asoc->highest_tsn_inside_map == asoc->cumulative_tsn) {
7903 /* We know if there are none above the cum-ack we
7904 * have everything with NO gaps
7908 /* Ok we must count how many gaps we
7912 if (asoc->highest_tsn_inside_map >= asoc->mapping_array_base_tsn) {
7913 maxi = (asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn);
7915 maxi = (asoc->highest_tsn_inside_map + (MAX_TSN - asoc->mapping_array_base_tsn) + 1);
7917 if (maxi > m_size) {
7918 /* impossible but who knows, someone is playing with us :> */
7920 kprintf("GAK maxi:%d > m_size:%d came out higher than allowed htsn:%u base:%u cumack:%u\n",
7923 asoc->highest_tsn_inside_map,
7924 asoc->mapping_array_base_tsn,
7925 asoc->cumulative_tsn
7931 if (asoc->cumulative_tsn >= asoc->mapping_array_base_tsn) {
7932 start = (asoc->cumulative_tsn - asoc->mapping_array_base_tsn);
7934 /* Set it so we start at 0 */
7937 /* Ok move start up one to look at the NEXT past the cum-ack */
7939 for (i = start; i <= maxi; i++) {
7941 /* while seeing ones I must
7942 * transition back to 0 before
7943 * finding the next gap and
7944 * counting the segment.
7946 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, i) == 0) {
7950 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, i)) {
7957 if (num_gap_blocks == 0) {
7959 * Traveled all of the bits and NO one,
7962 if (compare_with_wrap(asoc->cumulative_tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
7963 asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
7964 #ifdef SCTP_MAP_LOGGING
7965 sctp_log_map(0, 4, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
7971 /* Now calculate the space needed */
7972 space = (sizeof(struct sctp_sack_chunk) +
7973 (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
7974 (asoc->numduptsns * sizeof(int32_t))
7976 if (space > (asoc->smallest_mtu-SCTP_MAX_OVERHEAD)) {
7977 /* Reduce the size of the sack to fit */
7979 calc = (asoc->smallest_mtu - SCTP_MAX_OVERHEAD);
7980 calc -= sizeof(struct sctp_gap_ack_block);
7981 fit = calc/sizeof(struct sctp_gap_ack_block);
7982 if (fit > (int)num_gap_blocks) {
7983 /* discard some dups */
7984 asoc->numduptsns = (fit - num_gap_blocks);
7986 /* discard all dups and some gaps */
7987 num_gap_blocks = fit;
7988 asoc->numduptsns = 0;
7991 space = (sizeof(struct sctp_sack_chunk) +
7992 (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
7993 (asoc->numduptsns * sizeof(int32_t))
7998 if ((space+SCTP_MIN_OVERHEAD) > MHLEN) {
7999 /* We need a cluster */
8000 MCLGET(a_chk->data, MB_DONTWAIT);
8001 if ((a_chk->data->m_flags & M_EXT) != M_EXT) {
8002 /* can't get a cluster
8003 * give up and try later.
8006 sctp_m_freem(a_chk->data);
8008 a_chk->whoTo->ref_count--;
8009 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, a_chk);
8010 sctppcbinfo.ipi_count_chunk--;
8011 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
8012 panic("Chunk count is negative");
8014 sctppcbinfo.ipi_gencnt_chunk++;
8015 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
8016 stcb->sctp_ep, stcb, NULL);
8017 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
8018 stcb->sctp_ep, stcb, NULL);
8023 /* ok, lets go through and fill it in */
8024 a_chk->data->m_data += SCTP_MIN_OVERHEAD;
8025 sack = mtod(a_chk->data, struct sctp_sack_chunk *);
8026 sack->ch.chunk_type = SCTP_SELECTIVE_ACK;
8027 sack->ch.chunk_flags = asoc->receiver_nonce_sum & SCTP_SACK_NONCE_SUM;
8028 sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
8029 sack->sack.a_rwnd = htonl(asoc->my_rwnd);
8030 asoc->my_last_reported_rwnd = asoc->my_rwnd;
8031 sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
8032 sack->sack.num_dup_tsns = htons(asoc->numduptsns);
8034 a_chk->send_size = (sizeof(struct sctp_sack_chunk) +
8035 (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
8036 (asoc->numduptsns * sizeof(int32_t)));
8037 a_chk->data->m_pkthdr.len = a_chk->data->m_len = a_chk->send_size;
8038 sack->ch.chunk_length = htons(a_chk->send_size);
8040 gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)sack + sizeof(struct sctp_sack_chunk));
8042 for (i = start; i <= maxi; i++) {
8043 if (num_gap_blocks == 0) {
8047 /* while seeing Ones I must
8048 * transition back to 0 before
8049 * finding the next gap
8051 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, i) == 0) {
8052 gap_descriptor->end = htons(((uint16_t)(i-start)));
8058 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, i)) {
8059 gap_descriptor->start = htons(((uint16_t)(i+1-start)));
8060 /* advance struct to next pointer */
8065 if (num_gap_blocks) {
8066 /* special case where the array is all 1's
8067 * to the end of the array.
8069 gap_descriptor->end = htons(((uint16_t)((i-start))));
8072 /* now we must add any dups we are going to report. */
8073 if (asoc->numduptsns) {
8074 dup = (uint32_t *)gap_descriptor;
8075 for (i = 0; i < asoc->numduptsns; i++) {
8076 *dup = htonl(asoc->dup_tsns[i]);
8079 asoc->numduptsns = 0;
8081 /* now that the chunk is prepared queue it to the control
8084 TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
8085 asoc->ctrl_queue_cnt++;
8086 sctp_pegs[SCTP_PEG_SACKS_SENT]++;
8091 sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr)
8093 struct mbuf *m_abort;
8094 struct sctp_abort_msg *abort_m;
8097 MGETHDR(m_abort, MB_DONTWAIT, MT_HEADER);
8098 if (m_abort == NULL) {
8102 m_abort->m_data += SCTP_MIN_OVERHEAD;
8103 abort_m = mtod(m_abort, struct sctp_abort_msg *);
8104 m_abort->m_len = sizeof(struct sctp_abort_msg);
8105 m_abort->m_next = operr;
8115 abort_m->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
8116 abort_m->msg.ch.chunk_flags = 0;
8117 abort_m->msg.ch.chunk_length = htons(sizeof(struct sctp_abort_chunk) +
8119 abort_m->sh.src_port = stcb->sctp_ep->sctp_lport;
8120 abort_m->sh.dest_port = stcb->rport;
8121 abort_m->sh.v_tag = htonl(stcb->asoc.peer_vtag);
8122 abort_m->sh.checksum = 0;
8123 m_abort->m_pkthdr.len = m_abort->m_len + sz;
8124 m_abort->m_pkthdr.rcvif = 0;
8125 sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb,
8126 stcb->asoc.primary_destination,
8127 (struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr,
8128 m_abort, 1, 0, NULL, 0);
8132 sctp_send_shutdown_complete(struct sctp_tcb *stcb,
8133 struct sctp_nets *net)
8136 /* formulate and SEND a SHUTDOWN-COMPLETE */
8137 struct mbuf *m_shutdown_comp;
8138 struct sctp_shutdown_complete_msg *comp_cp;
8140 m_shutdown_comp = NULL;
8141 MGETHDR(m_shutdown_comp, MB_DONTWAIT, MT_HEADER);
8142 if (m_shutdown_comp == NULL) {
8146 m_shutdown_comp->m_data += sizeof(struct ip6_hdr);
8147 comp_cp = mtod(m_shutdown_comp, struct sctp_shutdown_complete_msg *);
8148 comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
8149 comp_cp->shut_cmp.ch.chunk_flags = 0;
8150 comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
8151 comp_cp->sh.src_port = stcb->sctp_ep->sctp_lport;
8152 comp_cp->sh.dest_port = stcb->rport;
8153 comp_cp->sh.v_tag = htonl(stcb->asoc.peer_vtag);
8154 comp_cp->sh.checksum = 0;
8156 m_shutdown_comp->m_pkthdr.len = m_shutdown_comp->m_len = sizeof(struct sctp_shutdown_complete_msg);
8157 m_shutdown_comp->m_pkthdr.rcvif = 0;
8158 sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
8159 (struct sockaddr *)&net->ro._l_addr, m_shutdown_comp,
8161 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
8162 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
8163 stcb->sctp_ep->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
8164 stcb->sctp_ep->sctp_socket->so_snd.ssb_cc = 0;
8165 soisdisconnected(stcb->sctp_ep->sctp_socket);
8171 sctp_send_shutdown_complete2(struct mbuf *m, int iphlen, struct sctphdr *sh)
8173 /* formulate and SEND a SHUTDOWN-COMPLETE */
8175 struct ip *iph, *iph_out;
8176 struct ip6_hdr *ip6, *ip6_out;
8178 struct sctp_shutdown_complete_msg *comp_cp;
8180 MGETHDR(mout, MB_DONTWAIT, MT_HEADER);
8185 iph = mtod(m, struct ip *);
8189 if (iph->ip_v == IPVERSION) {
8190 mout->m_len = sizeof(struct ip) +
8191 sizeof(struct sctp_shutdown_complete_msg);
8192 mout->m_next = NULL;
8193 iph_out = mtod(mout, struct ip *);
8195 /* Fill in the IP header for the ABORT */
8196 iph_out->ip_v = IPVERSION;
8197 iph_out->ip_hl = (sizeof(struct ip)/4);
8198 iph_out->ip_tos = (u_char)0;
8200 iph_out->ip_off = 0;
8201 iph_out->ip_ttl = MAXTTL;
8202 iph_out->ip_p = IPPROTO_SCTP;
8203 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
8204 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
8206 /* let IP layer calculate this */
8207 iph_out->ip_sum = 0;
8208 offset_out += sizeof(*iph_out);
8209 comp_cp = (struct sctp_shutdown_complete_msg *)(
8210 (caddr_t)iph_out + offset_out);
8211 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
8212 ip6 = (struct ip6_hdr *)iph;
8213 mout->m_len = sizeof(struct ip6_hdr) +
8214 sizeof(struct sctp_shutdown_complete_msg);
8215 mout->m_next = NULL;
8216 ip6_out = mtod(mout, struct ip6_hdr *);
8218 /* Fill in the IPv6 header for the ABORT */
8219 ip6_out->ip6_flow = ip6->ip6_flow;
8220 ip6_out->ip6_hlim = ip6_defhlim;
8221 ip6_out->ip6_nxt = IPPROTO_SCTP;
8222 ip6_out->ip6_src = ip6->ip6_dst;
8223 ip6_out->ip6_dst = ip6->ip6_src;
8224 ip6_out->ip6_plen = mout->m_len;
8225 offset_out += sizeof(*ip6_out);
8226 comp_cp = (struct sctp_shutdown_complete_msg *)(
8227 (caddr_t)ip6_out + offset_out);
8229 /* Currently not supported. */
8233 /* Now copy in and fill in the ABORT tags etc. */
8234 comp_cp->sh.src_port = sh->dest_port;
8235 comp_cp->sh.dest_port = sh->src_port;
8236 comp_cp->sh.checksum = 0;
8237 comp_cp->sh.v_tag = sh->v_tag;
8238 comp_cp->shut_cmp.ch.chunk_flags = SCTP_HAD_NO_TCB;
8239 comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
8240 comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
8242 mout->m_pkthdr.len = mout->m_len;
8244 if ((sctp_no_csum_on_loopback) &&
8245 (m->m_pkthdr.rcvif) &&
8246 (m->m_pkthdr.rcvif->if_type == IFT_LOOP)) {
8247 comp_cp->sh.checksum = 0;
8249 comp_cp->sh.checksum = sctp_calculate_sum(mout, NULL, offset_out);
8252 /* zap the rcvif, it should be null */
8253 mout->m_pkthdr.rcvif = 0;
8254 /* zap the stack pointer to the route */
8255 if (iph_out != NULL) {
8258 bzero(&ro, sizeof ro);
8260 if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
8261 kprintf("sctp_shutdown_complete2 calling ip_output:\n");
8262 sctp_print_address_pkt(iph_out, &comp_cp->sh);
8265 /* set IPv4 length */
8266 #if defined(__FreeBSD__)
8267 iph_out->ip_len = mout->m_pkthdr.len;
8269 iph_out->ip_len = htons(mout->m_pkthdr.len);
8272 ip_output(mout, 0, &ro, IP_RAWOUTPUT, NULL
8273 #if defined(__OpenBSD__) || (defined(__FreeBSD__) && __FreeBSD_version >= 480000) \
8274 || defined(__NetBSD__) || defined(__DragonFly__)
8278 /* Free the route if we got one back */
8281 } else if (ip6_out != NULL) {
8282 #ifdef NEW_STRUCT_ROUTE
8285 struct route_in6 ro;
8288 bzero(&ro, sizeof(ro));
8290 if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
8291 kprintf("sctp_shutdown_complete2 calling ip6_output:\n");
8292 sctp_print_address_pkt((struct ip *)ip6_out,
8296 ip6_output(mout, NULL, &ro, 0, NULL, NULL
8297 #if defined(__NetBSD__)
8300 #if (defined(__FreeBSD__) && __FreeBSD_version >= 480000) || defined(__DragonFly__)
8304 /* Free the route if we got one back */
8308 sctp_pegs[SCTP_DATAGRAMS_SENT]++;
8312 static struct sctp_nets *
8313 sctp_select_hb_destination(struct sctp_tcb *stcb, struct timeval *now)
8315 struct sctp_nets *net, *hnet;
8316 int ms_goneby, highest_ms, state_overide=0;
8318 SCTP_GETTIME_TIMEVAL(now);
8321 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
8323 ((net->dest_state & SCTP_ADDR_NOHB) && ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) ||
8324 (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)
8326 /* Skip this guy from consideration if HB is off AND its confirmed*/
8328 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
8329 kprintf("Skipping net:%p state:%d nohb/out-of-scope\n",
8330 net, net->dest_state);
8335 if (sctp_destination_is_reachable(stcb, (struct sockaddr *)&net->ro._l_addr) == 0) {
8336 /* skip this dest net from consideration */
8338 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
8339 kprintf("Skipping net:%p reachable NOT\n",
8345 if (net->last_sent_time.tv_sec) {
8346 /* Sent to so we subtract */
8347 ms_goneby = (now->tv_sec - net->last_sent_time.tv_sec) * 1000;
8349 /* Never been sent to */
8350 ms_goneby = 0x7fffffff;
8352 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
8353 kprintf("net:%p ms_goneby:%d\n",
8357 /* When the address state is unconfirmed but still considered reachable, we
8358 * HB at a higher rate. Once it goes confirmed OR reaches the "unreachable"
8359 * state, thenw we cut it back to HB at a more normal pace.
8361 if ((net->dest_state & (SCTP_ADDR_UNCONFIRMED|SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED) {
8367 if ((((unsigned int)ms_goneby >= net->RTO) || (state_overide)) &&
8368 (ms_goneby > highest_ms)) {
8369 highest_ms = ms_goneby;
8372 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
8373 kprintf("net:%p is the new high\n",
8380 ((hnet->dest_state & (SCTP_ADDR_UNCONFIRMED|SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED)) {
8386 if (highest_ms && (((unsigned int)highest_ms >= hnet->RTO) || state_overide)) {
8387 /* Found the one with longest delay bounds
8388 * OR it is unconfirmed and still not marked
8392 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
8393 kprintf("net:%p is the hb winner -",
8396 sctp_print_address((struct sockaddr *)&hnet->ro._l_addr);
8401 /* update the timer now */
8402 hnet->last_sent_time = *now;
8410 sctp_send_hb(struct sctp_tcb *stcb, int user_req, struct sctp_nets *u_net)
8412 struct sctp_tmit_chunk *chk;
8413 struct sctp_nets *net;
8414 struct sctp_heartbeat_chunk *hb;
8416 struct sockaddr_in *sin;
8417 struct sockaddr_in6 *sin6;
8419 if (user_req == 0) {
8420 net = sctp_select_hb_destination(stcb, &now);
8422 /* All our busy none to send to, just
8423 * start the timer again.
8425 if (stcb->asoc.state == 0) {
8428 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT,
8434 #ifndef SCTP_USE_ALLMAN_BURST
8436 /* found one idle.. decay cwnd on this one
8437 * by 1/2 if none outstanding.
8440 if (net->flight_size == 0) {
8442 if (net->addr_is_local) {
8443 if (net->cwnd < (net->mtu *4)) {
8444 net->cwnd = net->mtu * 4;
8447 if (net->cwnd < (net->mtu * 2)) {
8448 net->cwnd = net->mtu * 2;
8461 SCTP_GETTIME_TIMEVAL(&now);
8463 sin = (struct sockaddr_in *)&net->ro._l_addr;
8464 if (sin->sin_family != AF_INET) {
8465 if (sin->sin_family != AF_INET6) {
8470 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
8473 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
8474 kprintf("Gak, can't get a chunk for hb\n");
8479 sctppcbinfo.ipi_gencnt_chunk++;
8480 sctppcbinfo.ipi_count_chunk++;
8481 chk->rec.chunk_id = SCTP_HEARTBEAT_REQUEST;
8482 chk->asoc = &stcb->asoc;
8483 chk->send_size = sizeof(struct sctp_heartbeat_chunk);
8484 MGETHDR(chk->data, MB_DONTWAIT, MT_DATA);
8485 if (chk->data == NULL) {
8486 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
8487 sctppcbinfo.ipi_count_chunk--;
8488 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
8489 panic("Chunk count is negative");
8491 sctppcbinfo.ipi_gencnt_chunk++;
8494 chk->data->m_data += SCTP_MIN_OVERHEAD;
8495 chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
8496 chk->sent = SCTP_DATAGRAM_UNSENT;
8499 chk->whoTo->ref_count++;
8500 /* Now we have a mbuf that we can fill in with the details */
8501 hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
8503 /* fill out chunk header */
8504 hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
8505 hb->ch.chunk_flags = 0;
8506 hb->ch.chunk_length = htons(chk->send_size);
8507 /* Fill out hb parameter */
8508 hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
8509 hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
8510 hb->heartbeat.hb_info.time_value_1 = now.tv_sec;
8511 hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
8512 /* Did our user request this one, put it in */
8513 hb->heartbeat.hb_info.user_req = user_req;
8514 hb->heartbeat.hb_info.addr_family = sin->sin_family;
8515 hb->heartbeat.hb_info.addr_len = sin->sin_len;
8516 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
8517 /* we only take from the entropy pool if the address is
8520 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
8521 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
8523 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
8524 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
8526 if (sin->sin_family == AF_INET) {
8527 memcpy(hb->heartbeat.hb_info.address, &sin->sin_addr, sizeof(sin->sin_addr));
8528 } else if (sin->sin_family == AF_INET6) {
8529 /* We leave the scope the way it is in our lookup table. */
8530 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
8531 memcpy(hb->heartbeat.hb_info.address, &sin6->sin6_addr, sizeof(sin6->sin6_addr));
8533 /* huh compiler bug */
8535 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
8536 kprintf("Compiler bug bleeds a mbuf and a chunk\n");
8541 /* ok we have a destination that needs a beat */
8542 /* lets do the theshold management Qiaobing style */
8543 if (user_req == 0) {
8544 if (sctp_threshold_management(stcb->sctp_ep, stcb, net,
8545 stcb->asoc.max_send_times)) {
8546 /* we have lost the association, in a way this
8547 * is quite bad since we really are one less time
8548 * since we really did not send yet. This is the
8549 * down side to the Q's style as defined in the RFC
8550 * and not my alternate style defined in the RFC.
8552 if (chk->data != NULL) {
8553 sctp_m_freem(chk->data);
8556 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
8557 sctppcbinfo.ipi_count_chunk--;
8558 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
8559 panic("Chunk count is negative");
8561 sctppcbinfo.ipi_gencnt_chunk++;
8565 net->hb_responded = 0;
8567 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
8568 kprintf("Inserting chunk for HB\n");
8571 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
8572 stcb->asoc.ctrl_queue_cnt++;
8573 sctp_pegs[SCTP_HB_SENT]++;
8575 * Call directly med level routine to put out the chunk. It will
8576 * always tumble out control chunks aka HB but it may even tumble
8579 if (user_req == 0) {
8580 /* Ok now lets start the HB timer if it is NOT a user req */
8581 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
8588 sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
8591 struct sctp_association *asoc;
8592 struct sctp_ecne_chunk *ecne;
8593 struct sctp_tmit_chunk *chk;
8595 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
8596 if (chk->rec.chunk_id == SCTP_ECN_ECHO) {
8597 /* found a previous ECN_ECHO update it if needed */
8598 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
8599 ecne->tsn = htonl(high_tsn);
8603 /* nope could not find one to update so we must build one */
8604 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
8608 sctp_pegs[SCTP_ECNE_SENT]++;
8609 sctppcbinfo.ipi_count_chunk++;
8610 sctppcbinfo.ipi_gencnt_chunk++;
8611 chk->rec.chunk_id = SCTP_ECN_ECHO;
8612 chk->asoc = &stcb->asoc;
8613 chk->send_size = sizeof(struct sctp_ecne_chunk);
8614 MGETHDR(chk->data, MB_DONTWAIT, MT_DATA);
8615 if (chk->data == NULL) {
8616 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
8617 sctppcbinfo.ipi_count_chunk--;
8618 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
8619 panic("Chunk count is negative");
8621 sctppcbinfo.ipi_gencnt_chunk++;
8624 chk->data->m_data += SCTP_MIN_OVERHEAD;
8625 chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
8626 chk->sent = SCTP_DATAGRAM_UNSENT;
8629 chk->whoTo->ref_count++;
8630 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
8631 ecne->ch.chunk_type = SCTP_ECN_ECHO;
8632 ecne->ch.chunk_flags = 0;
8633 ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
8634 ecne->tsn = htonl(high_tsn);
8635 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
8636 asoc->ctrl_queue_cnt++;
8640 sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
8641 struct mbuf *m, int iphlen, int bad_crc)
8643 struct sctp_association *asoc;
8644 struct sctp_pktdrop_chunk *drp;
8645 struct sctp_tmit_chunk *chk;
8648 unsigned int small_one;
8653 if (asoc->peer_supports_pktdrop == 0) {
8654 /* peer must declare support before I
8659 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
8663 sctppcbinfo.ipi_count_chunk++;
8664 sctppcbinfo.ipi_gencnt_chunk++;
8666 iph = mtod(m, struct ip *);
8670 if (iph->ip_v == IPVERSION) {
8672 #if defined(__FreeBSD__)
8673 len = chk->send_size = iph->ip_len;
8675 len = chk->send_size = (iph->ip_len - iphlen);
8678 struct ip6_hdr *ip6h;
8680 ip6h = mtod(m, struct ip6_hdr *);
8681 len = chk->send_size = htons(ip6h->ip6_plen);
8683 if ((len+iphlen) > m->m_pkthdr.len) {
8685 chk->send_size = len = m->m_pkthdr.len - iphlen;
8687 chk->asoc = &stcb->asoc;
8688 MGETHDR(chk->data, MB_DONTWAIT, MT_DATA);
8689 if (chk->data == NULL) {
8691 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
8692 sctppcbinfo.ipi_count_chunk--;
8693 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
8694 panic("Chunk count is negative");
8696 sctppcbinfo.ipi_gencnt_chunk++;
8699 if ((chk->send_size+sizeof(struct sctp_pktdrop_chunk)+SCTP_MIN_OVERHEAD) > MHLEN) {
8700 MCLGET(chk->data, MB_DONTWAIT);
8701 if ((chk->data->m_flags & M_EXT) == 0) {
8703 sctp_m_freem(chk->data);
8708 chk->data->m_data += SCTP_MIN_OVERHEAD;
8709 drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
8711 sctp_m_freem(chk->data);
8715 small_one = asoc->smallest_mtu;
8716 if (small_one > MCLBYTES) {
8717 /* Only one cluster worth of data MAX */
8718 small_one = MCLBYTES;
8720 chk->book_size = (chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
8721 sizeof(struct sctphdr) + SCTP_MED_OVERHEAD);
8722 if (chk->book_size > small_one) {
8723 drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
8724 drp->trunc_len = htons(chk->send_size);
8725 chk->send_size = small_one - (SCTP_MED_OVERHEAD +
8726 sizeof(struct sctp_pktdrop_chunk) +
8727 sizeof(struct sctphdr));
8728 len = chk->send_size;
8730 /* no truncation needed */
8731 drp->ch.chunk_flags = 0;
8732 drp->trunc_len = htons(0);
8735 drp->ch.chunk_flags |= SCTP_BADCRC;
8737 chk->send_size += sizeof(struct sctp_pktdrop_chunk);
8738 chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
8739 chk->sent = SCTP_DATAGRAM_UNSENT;
8742 /* we should hit here */
8745 chk->whoTo = asoc->primary_destination;
8747 chk->whoTo->ref_count++;
8748 chk->rec.chunk_id = SCTP_PACKET_DROPPED;
8749 drp->ch.chunk_type = SCTP_PACKET_DROPPED;
8750 drp->ch.chunk_length = htons(chk->send_size);
8751 spc = stcb->sctp_socket->so_rcv.ssb_hiwat;
8755 drp->bottle_bw = htonl(spc);
8756 drp->current_onq = htonl(asoc->size_on_delivery_queue +
8757 asoc->size_on_reasm_queue +
8758 asoc->size_on_all_streams +
8759 asoc->my_rwnd_control_len +
8760 stcb->sctp_socket->so_rcv.ssb_cc);
8763 m_copydata(m, iphlen, len, datap);
8764 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
8765 asoc->ctrl_queue_cnt++;
8769 sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn)
8771 struct sctp_association *asoc;
8772 struct sctp_cwr_chunk *cwr;
8773 struct sctp_tmit_chunk *chk;
8776 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
8777 if (chk->rec.chunk_id == SCTP_ECN_CWR) {
8778 /* found a previous ECN_CWR update it if needed */
8779 cwr = mtod(chk->data, struct sctp_cwr_chunk *);
8780 if (compare_with_wrap(high_tsn, ntohl(cwr->tsn),
8782 cwr->tsn = htonl(high_tsn);
8787 /* nope could not find one to update so we must build one */
8788 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
8792 sctppcbinfo.ipi_count_chunk++;
8793 sctppcbinfo.ipi_gencnt_chunk++;
8794 chk->rec.chunk_id = SCTP_ECN_CWR;
8795 chk->asoc = &stcb->asoc;
8796 chk->send_size = sizeof(struct sctp_cwr_chunk);
8797 MGETHDR(chk->data, MB_DONTWAIT, MT_DATA);
8798 if (chk->data == NULL) {
8799 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
8800 sctppcbinfo.ipi_count_chunk--;
8801 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
8802 panic("Chunk count is negative");
8804 sctppcbinfo.ipi_gencnt_chunk++;
8807 chk->data->m_data += SCTP_MIN_OVERHEAD;
8808 chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
8809 chk->sent = SCTP_DATAGRAM_UNSENT;
8812 chk->whoTo->ref_count++;
8813 cwr = mtod(chk->data, struct sctp_cwr_chunk *);
8814 cwr->ch.chunk_type = SCTP_ECN_CWR;
8815 cwr->ch.chunk_flags = 0;
8816 cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
8817 cwr->tsn = htonl(high_tsn);
8818 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
8819 asoc->ctrl_queue_cnt++;
8822 sctp_reset_the_streams(struct sctp_tcb *stcb,
8823 struct sctp_stream_reset_request *req, int number_entries, uint16_t *list)
8827 if (req->reset_flags & SCTP_RESET_ALL) {
8828 for (i=0; i<stcb->asoc.streamoutcnt; i++) {
8829 stcb->asoc.strmout[i].next_sequence_sent = 0;
8831 } else if (number_entries) {
8832 for (i=0; i<number_entries; i++) {
8833 if (list[i] >= stcb->asoc.streamoutcnt) {
8834 /* no such stream */
8837 stcb->asoc.strmout[(list[i])].next_sequence_sent = 0;
8840 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list);
8844 sctp_send_str_reset_ack(struct sctp_tcb *stcb,
8845 struct sctp_stream_reset_request *req)
8847 struct sctp_association *asoc;
8848 struct sctp_stream_reset_resp *strack;
8849 struct sctp_tmit_chunk *chk;
8851 int number_entries, i;
8852 uint8_t two_way=0, not_peer=0;
8853 uint16_t *list=NULL;
8856 if (req->reset_flags & SCTP_RESET_ALL)
8859 number_entries = (ntohs(req->ph.param_length) - sizeof(struct sctp_stream_reset_request)) / sizeof(uint16_t);
8861 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
8865 sctppcbinfo.ipi_count_chunk++;
8866 sctppcbinfo.ipi_gencnt_chunk++;
8867 chk->rec.chunk_id = SCTP_STREAM_RESET;
8868 chk->asoc = &stcb->asoc;
8869 chk->send_size = sizeof(struct sctp_stream_reset_resp) + (number_entries * sizeof(uint16_t));
8870 MGETHDR(chk->data, MB_DONTWAIT, MT_DATA);
8871 if (chk->data == NULL) {
8873 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
8874 sctppcbinfo.ipi_count_chunk--;
8875 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
8876 panic("Chunk count is negative");
8878 sctppcbinfo.ipi_gencnt_chunk++;
8881 chk->data->m_data += SCTP_MIN_OVERHEAD;
8882 chk->data->m_pkthdr.len = chk->data->m_len = SCTP_SIZE32(chk->send_size);
8883 if (M_TRAILINGSPACE(chk->data) < (int)SCTP_SIZE32(chk->send_size)) {
8884 MCLGET(chk->data, MB_DONTWAIT);
8885 if ((chk->data->m_flags & M_EXT) == 0) {
8887 sctp_m_freem(chk->data);
8889 goto strresp_jump_out;
8891 chk->data->m_data += SCTP_MIN_OVERHEAD;
8893 if (M_TRAILINGSPACE(chk->data) < (int)SCTP_SIZE32(chk->send_size)) {
8894 /* can't do it, no room */
8896 sctp_m_freem(chk->data);
8898 goto strresp_jump_out;
8901 chk->sent = SCTP_DATAGRAM_UNSENT;
8903 chk->whoTo = asoc->primary_destination;
8904 chk->whoTo->ref_count++;
8905 strack = mtod(chk->data, struct sctp_stream_reset_resp *);
8907 strack->ch.chunk_type = SCTP_STREAM_RESET;
8908 strack->ch.chunk_flags = 0;
8909 strack->ch.chunk_length = htons(chk->send_size);
8911 memset(strack->sr_resp.reset_pad, 0, sizeof(strack->sr_resp.reset_pad));
8913 strack->sr_resp.ph.param_type = ntohs(SCTP_STR_RESET_RESPONSE);
8914 strack->sr_resp.ph.param_length = htons((chk->send_size - sizeof(struct sctp_chunkhdr)));
8918 if (chk->send_size % 4) {
8919 /* need a padding for the end */
8922 end = (uint8_t *)((caddr_t)strack + chk->send_size);
8923 pad = chk->send_size % 4;
8924 for (i = 0; i < pad; i++) {
8927 chk->send_size += pad;
8930 /* actual response */
8931 if (req->reset_flags & SCTP_RESET_YOUR) {
8932 strack->sr_resp.reset_flags = SCTP_RESET_PERFORMED;
8934 strack->sr_resp.reset_flags = 0;
8937 /* copied from reset request */
8938 strack->sr_resp.reset_req_seq_resp = req->reset_req_seq;
8939 seq = ntohl(req->reset_req_seq);
8941 list = req->list_of_streams;
8942 /* copy the un-converted network byte order streams */
8943 for (i=0; i<number_entries; i++) {
8944 strack->sr_resp.list_of_streams[i] = list[i];
8946 if (asoc->str_reset_seq_in == seq) {
8947 /* is it the next expected? */
8948 asoc->str_reset_seq_in++;
8949 strack->sr_resp.reset_at_tsn = htonl(asoc->sending_seq);
8950 asoc->str_reset_sending_seq = asoc->sending_seq;
8951 if (number_entries) {
8954 /* convert them to host byte order */
8955 for (i=0 ; i<number_entries; i++) {
8956 temp = ntohs(list[i]);
8960 if (req->reset_flags & SCTP_RESET_YOUR) {
8961 /* reset my outbound streams */
8962 sctp_reset_the_streams(stcb, req , number_entries, list);
8964 if (req->reset_flags & SCTP_RECIPRICAL) {
8965 /* reset peer too */
8966 sctp_send_str_reset_req(stcb, number_entries, list, two_way, not_peer);
8970 /* no its a retran so I must just ack and do nothing */
8971 strack->sr_resp.reset_at_tsn = htonl(asoc->str_reset_sending_seq);
8973 strack->sr_resp.cumulative_tsn = htonl(asoc->cumulative_tsn);
8974 TAILQ_INSERT_TAIL(&asoc->control_send_queue,
8977 asoc->ctrl_queue_cnt++;
8982 sctp_send_str_reset_req(struct sctp_tcb *stcb,
8983 int number_entrys, uint16_t *list, uint8_t two_way, uint8_t not_peer)
8985 /* Send a stream reset request. The number_entrys may be 0 and list NULL
8986 * if the request is to reset all streams. If two_way is true then we
8987 * not only request a RESET of the received streams but we also
8988 * request the peer to send a reset req to us too.
8989 * Flag combinations in table:
8991 * two_way | not_peer | = | Flags
8992 * ------------------------------
8993 * 0 | 0 | = | SCTP_RESET_YOUR (just the peer)
8994 * 1 | 0 | = | SCTP_RESET_YOUR | SCTP_RECIPRICAL (both sides)
8995 * 0 | 1 | = | Not a Valid Request (not anyone)
8996 * 1 | 1 | = | SCTP_RESET_RECIPRICAL (Just local host)
8998 struct sctp_association *asoc;
8999 struct sctp_stream_reset_req *strreq;
9000 struct sctp_tmit_chunk *chk;
9004 if (asoc->stream_reset_outstanding) {
9005 /* Already one pending, must get ACK back
9006 * to clear the flag.
9011 if ((two_way == 0) && (not_peer == 1)) {
9012 /* not a valid request */
9016 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
9020 sctppcbinfo.ipi_count_chunk++;
9021 sctppcbinfo.ipi_gencnt_chunk++;
9022 chk->rec.chunk_id = SCTP_STREAM_RESET;
9023 chk->asoc = &stcb->asoc;
9024 chk->send_size = sizeof(struct sctp_stream_reset_req) + (number_entrys * sizeof(uint16_t));
9025 MGETHDR(chk->data, MB_DONTWAIT, MT_DATA);
9026 if (chk->data == NULL) {
9028 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
9029 sctppcbinfo.ipi_count_chunk--;
9030 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
9031 panic("Chunk count is negative");
9033 sctppcbinfo.ipi_gencnt_chunk++;
9036 chk->data->m_data += SCTP_MIN_OVERHEAD;
9037 chk->data->m_pkthdr.len = chk->data->m_len = SCTP_SIZE32(chk->send_size);
9038 if (M_TRAILINGSPACE(chk->data) < (int)SCTP_SIZE32(chk->send_size)) {
9039 MCLGET(chk->data, MB_DONTWAIT);
9040 if ((chk->data->m_flags & M_EXT) == 0) {
9042 sctp_m_freem(chk->data);
9044 goto strreq_jump_out;
9046 chk->data->m_data += SCTP_MIN_OVERHEAD;
9048 if (M_TRAILINGSPACE(chk->data) < (int)SCTP_SIZE32(chk->send_size)) {
9049 /* can't do it, no room */
9051 sctp_m_freem(chk->data);
9053 goto strreq_jump_out;
9055 chk->sent = SCTP_DATAGRAM_UNSENT;
9057 chk->whoTo = asoc->primary_destination;
9058 chk->whoTo->ref_count++;
9060 strreq = mtod(chk->data, struct sctp_stream_reset_req *);
9061 strreq->ch.chunk_type = SCTP_STREAM_RESET;
9062 strreq->ch.chunk_flags = 0;
9063 strreq->ch.chunk_length = htons(chk->send_size);
9065 strreq->sr_req.ph.param_type = ntohs(SCTP_STR_RESET_REQUEST);
9066 strreq->sr_req.ph.param_length = htons((chk->send_size - sizeof(struct sctp_chunkhdr)));
9068 if (chk->send_size % 4) {
9069 /* need a padding for the end */
9072 end = (uint8_t *)((caddr_t)strreq + chk->send_size);
9073 pad = chk->send_size % 4;
9074 for (i=0; i<pad; i++) {
9077 chk->send_size += pad;
9080 strreq->sr_req.reset_flags = 0;
9081 if (number_entrys == 0) {
9082 strreq->sr_req.reset_flags |= SCTP_RESET_ALL;
9085 strreq->sr_req.reset_flags |= SCTP_RESET_YOUR;
9087 if (not_peer == 0) {
9088 strreq->sr_req.reset_flags |= SCTP_RECIPRICAL | SCTP_RESET_YOUR;
9090 strreq->sr_req.reset_flags |= SCTP_RECIPRICAL;
9093 memset(strreq->sr_req.reset_pad, 0, sizeof(strreq->sr_req.reset_pad));
9094 strreq->sr_req.reset_req_seq = htonl(asoc->str_reset_seq_out);
9095 if (number_entrys) {
9096 /* populate the specific entry's */
9098 for (i=0; i < number_entrys; i++) {
9099 strreq->sr_req.list_of_streams[i] = htons(list[i]);
9102 TAILQ_INSERT_TAIL(&asoc->control_send_queue,
9105 asoc->ctrl_queue_cnt++;
9106 sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
9107 asoc->stream_reset_outstanding = 1;
9111 sctp_send_abort(struct mbuf *m, int iphlen, struct sctphdr *sh, uint32_t vtag,
9112 struct mbuf *err_cause)
9115 * Formulate the abort message, and send it back down.
9118 struct sctp_abort_msg *abm;
9119 struct ip *iph, *iph_out;
9120 struct ip6_hdr *ip6, *ip6_out;
9123 /* don't respond to ABORT with ABORT */
9124 if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
9126 sctp_m_freem(err_cause);
9129 MGETHDR(mout, MB_DONTWAIT, MT_HEADER);
9132 sctp_m_freem(err_cause);
9135 iph = mtod(m, struct ip *);
9138 if (iph->ip_v == IPVERSION) {
9139 iph_out = mtod(mout, struct ip *);
9140 mout->m_len = sizeof(*iph_out) + sizeof(*abm);
9141 mout->m_next = err_cause;
9143 /* Fill in the IP header for the ABORT */
9144 iph_out->ip_v = IPVERSION;
9145 iph_out->ip_hl = (sizeof(struct ip) / 4);
9146 iph_out->ip_tos = (u_char)0;
9148 iph_out->ip_off = 0;
9149 iph_out->ip_ttl = MAXTTL;
9150 iph_out->ip_p = IPPROTO_SCTP;
9151 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
9152 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
9153 /* let IP layer calculate this */
9154 iph_out->ip_sum = 0;
9156 iphlen_out = sizeof(*iph_out);
9157 abm = (struct sctp_abort_msg *)((caddr_t)iph_out + iphlen_out);
9158 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
9159 ip6 = (struct ip6_hdr *)iph;
9160 ip6_out = mtod(mout, struct ip6_hdr *);
9161 mout->m_len = sizeof(*ip6_out) + sizeof(*abm);
9162 mout->m_next = err_cause;
9164 /* Fill in the IP6 header for the ABORT */
9165 ip6_out->ip6_flow = ip6->ip6_flow;
9166 ip6_out->ip6_hlim = ip6_defhlim;
9167 ip6_out->ip6_nxt = IPPROTO_SCTP;
9168 ip6_out->ip6_src = ip6->ip6_dst;
9169 ip6_out->ip6_dst = ip6->ip6_src;
9171 iphlen_out = sizeof(*ip6_out);
9172 abm = (struct sctp_abort_msg *)((caddr_t)ip6_out + iphlen_out);
9174 /* Currently not supported */
9178 abm->sh.src_port = sh->dest_port;
9179 abm->sh.dest_port = sh->src_port;
9180 abm->sh.checksum = 0;
9182 abm->sh.v_tag = sh->v_tag;
9183 abm->msg.ch.chunk_flags = SCTP_HAD_NO_TCB;
9185 abm->sh.v_tag = htonl(vtag);
9186 abm->msg.ch.chunk_flags = 0;
9188 abm->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
9191 struct mbuf *m_tmp = err_cause;
9193 /* get length of the err_cause chain */
9194 while (m_tmp != NULL) {
9195 err_len += m_tmp->m_len;
9196 m_tmp = m_tmp->m_next;
9198 mout->m_pkthdr.len = mout->m_len + err_len;
9200 /* need pad at end of chunk */
9203 padlen = 4 - (mout->m_pkthdr.len % 4);
9204 m_copyback(mout, mout->m_pkthdr.len, padlen, (caddr_t)&cpthis);
9206 abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch) + err_len);
9208 mout->m_pkthdr.len = mout->m_len;
9209 abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch));
9213 if ((sctp_no_csum_on_loopback) &&
9214 (m->m_pkthdr.rcvif) &&
9215 (m->m_pkthdr.rcvif->if_type == IFT_LOOP)) {
9216 abm->sh.checksum = 0;
9218 abm->sh.checksum = sctp_calculate_sum(mout, NULL, iphlen_out);
9221 /* zap the rcvif, it should be null */
9222 mout->m_pkthdr.rcvif = 0;
9223 if (iph_out != NULL) {
9226 /* zap the stack pointer to the route */
9227 bzero(&ro, sizeof ro);
9229 if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
9230 kprintf("sctp_send_abort calling ip_output:\n");
9231 sctp_print_address_pkt(iph_out, &abm->sh);
9234 /* set IPv4 length */
9235 #if defined(__FreeBSD__)
9236 iph_out->ip_len = mout->m_pkthdr.len;
9238 iph_out->ip_len = htons(mout->m_pkthdr.len);
9241 ip_output(mout, 0, &ro, IP_RAWOUTPUT, NULL
9242 #if defined(__OpenBSD__) || (defined(__FreeBSD__) && __FreeBSD_version >= 480000) \
9243 || defined(__NetBSD__) || defined(__DragonFly__)
9247 /* Free the route if we got one back */
9250 } else if (ip6_out != NULL) {
9251 #ifdef NEW_STRUCT_ROUTE
9254 struct route_in6 ro;
9257 /* zap the stack pointer to the route */
9258 bzero(&ro, sizeof(ro));
9260 if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
9261 kprintf("sctp_send_abort calling ip6_output:\n");
9262 sctp_print_address_pkt((struct ip *)ip6_out, &abm->sh);
9265 ip6_output(mout, NULL, &ro, 0, NULL, NULL
9266 #if defined(__NetBSD__)
9269 #if (defined(__FreeBSD__) && __FreeBSD_version >= 480000) || defined(__DragonFly__)
9273 /* Free the route if we got one back */
9277 sctp_pegs[SCTP_DATAGRAMS_SENT]++;
9281 sctp_send_operr_to(struct mbuf *m, int iphlen,
9285 struct sctphdr *ihdr;
9287 struct sctphdr *ohdr;
9288 struct sctp_chunkhdr *ophdr;
9292 struct sockaddr_in6 lsa6, fsa6;
9295 iph = mtod(m, struct ip *);
9296 ihdr = (struct sctphdr *)((caddr_t)iph + iphlen);
9297 if (!(scm->m_flags & M_PKTHDR)) {
9298 /* must be a pkthdr */
9299 kprintf("Huh, not a packet header in send_operr\n");
9303 M_PREPEND(scm, (sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr)), MB_DONTWAIT);
9305 /* can't send because we can't add a mbuf */
9308 ohdr = mtod(scm, struct sctphdr *);
9309 ohdr->src_port = ihdr->dest_port;
9310 ohdr->dest_port = ihdr->src_port;
9313 ophdr = (struct sctp_chunkhdr *)(ohdr + 1);
9314 ophdr->chunk_type = SCTP_OPERATION_ERROR;
9315 ophdr->chunk_flags = 0;
9316 ophdr->chunk_length = htons(scm->m_pkthdr.len - sizeof(struct sctphdr));
9317 if (scm->m_pkthdr.len % 4) {
9321 padlen = 4 - (scm->m_pkthdr.len % 4);
9322 m_copyback(scm, scm->m_pkthdr.len, padlen, (caddr_t)&cpthis);
9324 if ((sctp_no_csum_on_loopback) &&
9325 (m->m_pkthdr.rcvif) &&
9326 (m->m_pkthdr.rcvif->if_type == IFT_LOOP)) {
9329 val = sctp_calculate_sum(scm, NULL, 0);
9331 ohdr->checksum = val;
9332 if (iph->ip_v == IPVERSION) {
9336 M_PREPEND(scm, sizeof(struct ip), MB_DONTWAIT);
9339 bzero(&ro, sizeof ro);
9340 out = mtod(scm, struct ip *);
9341 out->ip_v = iph->ip_v;
9342 out->ip_hl = (sizeof(struct ip)/4);
9343 out->ip_tos = iph->ip_tos;
9344 out->ip_id = iph->ip_id;
9346 out->ip_ttl = MAXTTL;
9347 out->ip_p = IPPROTO_SCTP;
9349 out->ip_src = iph->ip_dst;
9350 out->ip_dst = iph->ip_src;
9351 #if defined(__FreeBSD__)
9352 out->ip_len = scm->m_pkthdr.len;
9354 out->ip_len = htons(scm->m_pkthdr.len);
9356 retcode = ip_output(scm, 0, &ro, IP_RAWOUTPUT, NULL
9357 #if defined(__OpenBSD__) || (defined(__FreeBSD__) && __FreeBSD_version >= 480000) \
9358 || defined(__NetBSD__) || defined(__DragonFly__)
9362 sctp_pegs[SCTP_DATAGRAMS_SENT]++;
9363 /* Free the route if we got one back */
9368 #ifdef NEW_STRUCT_ROUTE
9371 struct route_in6 ro;
9373 struct ip6_hdr *out6, *in6;
9375 M_PREPEND(scm, sizeof(struct ip6_hdr), MB_DONTWAIT);
9378 bzero(&ro, sizeof ro);
9379 in6 = mtod(m, struct ip6_hdr *);
9380 out6 = mtod(scm, struct ip6_hdr *);
9381 out6->ip6_flow = in6->ip6_flow;
9382 out6->ip6_hlim = ip6_defhlim;
9383 out6->ip6_nxt = IPPROTO_SCTP;
9384 out6->ip6_src = in6->ip6_dst;
9385 out6->ip6_dst = in6->ip6_src;
9388 bzero(&lsa6, sizeof(lsa6));
9389 lsa6.sin6_len = sizeof(lsa6);
9390 lsa6.sin6_family = AF_INET6;
9391 lsa6.sin6_addr = out6->ip6_src;
9392 bzero(&fsa6, sizeof(fsa6));
9393 fsa6.sin6_len = sizeof(fsa6);
9394 fsa6.sin6_family = AF_INET6;
9395 fsa6.sin6_addr = out6->ip6_dst;
9396 if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
9397 kprintf("sctp_operr_to calling ipv6 output:\n");
9399 sctp_print_address((struct sockaddr *)&lsa6);
9401 sctp_print_address((struct sockaddr *)&fsa6);
9403 #endif /* SCTP_DEBUG */
9404 ip6_output(scm, NULL, &ro, 0, NULL, NULL
9405 #if defined(__NetBSD__)
9408 #if (defined(__FreeBSD__) && __FreeBSD_version >= 480000) || defined(__DragonFly__)
9412 sctp_pegs[SCTP_DATAGRAMS_SENT]++;
9413 /* Free the route if we got one back */
9420 sctp_copy_one(struct mbuf *m, struct uio *uio, int cpsz, int resv_upfront, int *mbcnt)
9422 int left, cancpy, willcpy, error;
9431 if ((left+resv_upfront) > (int)MHLEN) {
9437 if ((m->m_flags & M_EXT) == 0) {
9441 *mbcnt += m->m_ext.ext_size;
9444 cancpy = M_TRAILINGSPACE(m);
9445 willcpy = min(cancpy, left);
9446 if ((willcpy + resv_upfront) > cancpy) {
9447 willcpy -= resv_upfront;
9450 /* Align data to the end */
9451 if ((m->m_flags & M_EXT) == 0) {
9452 if (m->m_flags & M_PKTHDR) {
9453 MH_ALIGN(m, willcpy);
9455 M_ALIGN(m, willcpy);
9458 MC_ALIGN(m, willcpy);
9460 error = uiomove(mtod(m, caddr_t), willcpy, uio);
9468 MGET(m->m_next, MB_WAIT, MT_DATA);
9469 if (m->m_next == NULL) {
9476 if (left > (int)MHLEN) {
9482 if ((m->m_flags & M_EXT) == 0) {
9486 *mbcnt += m->m_ext.ext_size;
9488 cancpy = M_TRAILINGSPACE(m);
9489 willcpy = min(cancpy, left);
9496 sctp_copy_it_in(struct sctp_inpcb *inp,
9497 struct sctp_tcb *stcb,
9498 struct sctp_association *asoc,
9499 struct sctp_nets *net,
9500 struct sctp_sndrcvinfo *srcv,
9504 /* This routine must be very careful in
9505 * its work. Protocol processing is
9506 * up and running so care must be taken to
9507 * spl...() when you need to do something
9508 * that may effect the stcb/asoc. The sb is
9509 * locked however. When data is copied the
9510 * protocol processing should be enabled since
9511 * this is a slower operation...
9515 int frag_size, mbcnt = 0, mbcnt_e = 0;
9516 unsigned int sndlen;
9517 unsigned int tot_demand;
9518 int tot_out, dataout;
9519 struct sctp_tmit_chunk *chk;
9521 struct sctp_stream_out *strq;
9526 so = stcb->sctp_socket;
9530 sndlen = uio->uio_resid;
9531 /* lock the socket buf */
9532 SOCKBUF_LOCK(&so->so_snd);
9533 error = ssb_lock(&so->so_snd, SBLOCKWAIT(flags));
9539 /* will it ever fit ? */
9540 if (sndlen > so->so_snd.ssb_hiwat) {
9541 /* It will NEVER fit */
9546 /* Do I need to block? */
9547 if ((so->so_snd.ssb_hiwat <
9548 (sndlen + asoc->total_output_queue_size)) ||
9549 (asoc->chunks_on_out_queue > sctp_max_chunks_on_queue) ||
9550 (asoc->total_output_mbuf_queue_size >
9551 so->so_snd.ssb_mbmax)
9553 /* prune any prsctp bufs out */
9554 if (asoc->peer_supports_prsctp) {
9555 sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
9558 * We store off a pointer to the endpoint.
9559 * Since on return from this we must check to
9560 * see if an so_error is set. If so we may have
9561 * been reset and our stcb destroyed. Returning
9562 * an error will flow back to the user...
9564 while ((so->so_snd.ssb_hiwat <
9565 (sndlen + asoc->total_output_queue_size)) ||
9566 (asoc->chunks_on_out_queue >
9567 sctp_max_chunks_on_queue) ||
9568 (asoc->total_output_mbuf_queue_size >
9569 so->so_snd.ssb_mbmax)
9571 if (flags & (MSG_FNONBLOCKING|MSG_DONTWAIT)) {
9572 /* Non-blocking io in place */
9573 error = EWOULDBLOCK;
9577 inp->sctp_tcb_at_block = (void *)stcb;
9578 inp->error_on_block = 0;
9579 #ifdef SCTP_BLK_LOGGING
9580 sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
9583 ssb_unlock(&so->so_snd);
9584 SCTP_TCB_UNLOCK(stcb);
9585 error = ssb_wait(&so->so_snd);
9586 SCTP_INP_RLOCK(inp);
9587 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
9588 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
9589 /* Should I really unlock ? */
9590 SCTP_INP_RUNLOCK(inp);
9595 SCTP_TCB_LOCK(stcb);
9596 SCTP_INP_RUNLOCK(inp);
9598 inp->sctp_tcb_at_block = 0;
9599 #ifdef SCTP_BLK_LOGGING
9600 sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
9603 if (inp->error_on_block) {
9605 * if our asoc was killed, the free code
9606 * (in sctp_pcb.c) will save a error in
9609 error = inp->error_on_block;
9617 /* did we encounter a socket error? */
9619 error = so->so_error;
9623 error = ssb_lock(&so->so_snd, M_WAITOK);
9625 /* Can't acquire the lock */
9629 #if defined(__FreeBSD__) && __FreeBSD_version >= 502115
9630 if (so->so_rcv.sb_state & SBS_CANTSENDMORE) {
9632 if (so->so_state & SS_CANTSENDMORE) {
9634 /* The socket is now set not to sendmore.. its gone */
9640 error = so->so_error;
9644 if (asoc->peer_supports_prsctp) {
9645 sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
9649 dataout = tot_out = uio->uio_resid;
9650 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
9651 resv_in_first = SCTP_MED_OVERHEAD;
9653 resv_in_first = SCTP_MED_V4_OVERHEAD;
9656 /* Are we aborting? */
9657 if (srcv->sinfo_flags & MSG_ABORT) {
9658 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
9659 (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_ECHOED)) {
9660 /* It has to be up before we abort */
9661 /* how big is the user initiated abort? */
9663 /* I wonder about doing a MGET without a splnet set.
9664 * it is done that way in the sosend code so I guess
9667 MGETHDR(mm, MB_WAIT, MT_DATA);
9669 struct sctp_paramhdr *ph;
9671 tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
9672 if (tot_demand > MHLEN) {
9673 if (tot_demand > MCLBYTES) {
9674 /* truncate user data */
9675 tot_demand = MCLBYTES;
9676 tot_out = tot_demand - sizeof(struct sctp_paramhdr);
9678 MCLGET(mm, MB_WAIT);
9679 if ((mm->m_flags & M_EXT) == 0) {
9680 /* truncate further */
9682 tot_out = tot_demand - sizeof(struct sctp_paramhdr);
9685 /* now move forward the data pointer */
9686 ph = mtod(mm, struct sctp_paramhdr *);
9687 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
9688 ph->param_length = htons((sizeof(struct sctp_paramhdr) + tot_out));
9690 mm->m_pkthdr.len = tot_out + sizeof(struct sctp_paramhdr);
9691 mm->m_len = mm->m_pkthdr.len;
9692 error = uiomove((caddr_t)ph, tot_out, uio);
9695 * Here if we can't get his data we
9696 * still abort we just don't get to
9697 * send the users note :-0
9703 ssb_unlock(&so->so_snd);
9704 SOCKBUF_UNLOCK(&so->so_snd);
9705 sctp_abort_an_association(stcb->sctp_ep, stcb,
9706 SCTP_RESPONSE_TO_USER_REQ,
9716 /* Now can we send this? */
9717 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
9718 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
9719 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
9720 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
9721 /* got data while shutting down */
9726 /* Is the stream no. valid? */
9727 if (srcv->sinfo_stream >= asoc->streamoutcnt) {
9728 /* Invalid stream number */
9733 if (asoc->strmout == NULL) {
9734 /* huh? software error */
9736 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
9737 kprintf("software error in sctp_copy_it_in\n");
9744 if ((srcv->sinfo_flags & MSG_EOF) &&
9745 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
9756 /* save off the tag */
9757 my_vtag = asoc->my_vtag;
9758 strq = &asoc->strmout[srcv->sinfo_stream];
9759 /* First lets figure out the "chunking" point */
9760 frag_size = sctp_get_frag_point(stcb, asoc);
9762 /* two choices here, it all fits in one chunk or
9763 * we need multiple chunks.
9766 SOCKBUF_UNLOCK(&so->so_snd);
9767 if (tot_out <= frag_size) {
9768 /* no need to setup a template */
9769 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
9772 SOCKBUF_LOCK(&so->so_snd);
9775 sctppcbinfo.ipi_count_chunk++;
9776 sctppcbinfo.ipi_gencnt_chunk++;
9777 asoc->chunks_on_out_queue++;
9778 MGETHDR(mm, MB_WAIT, MT_DATA);
9783 error = sctp_copy_one(mm, uio, tot_out, resv_in_first, &mbcnt_e);
9786 sctp_prepare_chunk(chk, stcb, srcv, strq, net);
9787 chk->mbcnt = mbcnt_e;
9790 mm->m_pkthdr.len = tot_out;
9794 /* the actual chunk flags */
9795 chk->rec.data.rcv_flags |= SCTP_DATA_NOT_FRAG;
9796 chk->whoTo->ref_count++;
9798 /* fix up the send_size if it is not present */
9799 chk->send_size = tot_out;
9800 chk->book_size = chk->send_size;
9801 /* ok, we are commited */
9802 if ((srcv->sinfo_flags & MSG_UNORDERED) == 0) {
9803 /* bump the ssn if we are unordered. */
9804 strq->next_sequence_sent++;
9806 if (chk->flags & SCTP_PR_SCTP_BUFFER) {
9807 asoc->sent_queue_cnt_removeable++;
9810 if ((asoc->state == 0) ||
9811 (my_vtag != asoc->my_vtag) ||
9812 (so != inp->sctp_socket) ||
9813 (inp->sctp_socket == 0)) {
9814 /* connection was aborted */
9819 asoc->stream_queue_cnt++;
9820 TAILQ_INSERT_TAIL(&strq->outqueue, chk, sctp_next);
9821 /* now check if this stream is on the wheel */
9822 if ((strq->next_spoke.tqe_next == NULL) &&
9823 (strq->next_spoke.tqe_prev == NULL)) {
9824 /* Insert it on the wheel since it is not
9827 sctp_insert_on_wheel(asoc, strq);
9832 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
9833 sctppcbinfo.ipi_count_chunk--;
9834 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
9835 panic("Chunk count is negative");
9837 SOCKBUF_LOCK(&so->so_snd);
9841 /* we need to setup a template */
9842 struct sctp_tmit_chunk template;
9843 struct sctpchunk_listhead tmp;
9845 /* setup the template */
9846 sctp_prepare_chunk(&template, stcb, srcv, strq, net);
9848 /* Prepare the temp list */
9851 /* Template is complete, now time for the work */
9852 while (tot_out > 0) {
9854 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
9857 * ok we must spin through and dump anything
9858 * we have allocated and then jump to the
9863 sctppcbinfo.ipi_count_chunk++;
9864 asoc->chunks_on_out_queue++;
9866 sctppcbinfo.ipi_gencnt_chunk++;
9868 chk->whoTo->ref_count++;
9869 MGETHDR(chk->data, MB_WAIT, MT_DATA);
9870 if (chk->data == NULL) {
9874 tot_demand = min(tot_out, frag_size);
9875 error = sctp_copy_one(chk->data, uio, tot_demand , resv_in_first, &mbcnt_e);
9878 /* now fix the chk->send_size */
9879 chk->mbcnt = mbcnt_e;
9882 chk->send_size = tot_demand;
9883 chk->data->m_pkthdr.len = tot_demand;
9884 chk->book_size = chk->send_size;
9885 if (chk->flags & SCTP_PR_SCTP_BUFFER) {
9886 asoc->sent_queue_cnt_removeable++;
9888 TAILQ_INSERT_TAIL(&tmp, chk, sctp_next);
9889 tot_out -= tot_demand;
9891 /* Now the tmp list holds all chunks and data */
9892 if ((srcv->sinfo_flags & MSG_UNORDERED) == 0) {
9893 /* bump the ssn if we are unordered. */
9894 strq->next_sequence_sent++;
9896 /* Mark the first/last flags. This will
9897 * result int a 3 for a single item on the list
9899 chk = TAILQ_FIRST(&tmp);
9900 chk->rec.data.rcv_flags |= SCTP_DATA_FIRST_FRAG;
9901 chk = TAILQ_LAST(&tmp, sctpchunk_listhead);
9902 chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
9904 /* now move it to the streams actual queue */
9905 /* first stop protocol processing */
9907 if ((asoc->state == 0) ||
9908 (my_vtag != asoc->my_vtag) ||
9909 (so != inp->sctp_socket) ||
9910 (inp->sctp_socket == 0)) {
9911 /* connection was aborted */
9916 chk = TAILQ_FIRST(&tmp);
9918 chk->data->m_nextpkt = 0;
9919 TAILQ_REMOVE(&tmp, chk, sctp_next);
9920 asoc->stream_queue_cnt++;
9921 TAILQ_INSERT_TAIL(&strq->outqueue, chk, sctp_next);
9922 chk = TAILQ_FIRST(&tmp);
9924 /* now check if this stream is on the wheel */
9925 if ((strq->next_spoke.tqe_next == NULL) &&
9926 (strq->next_spoke.tqe_prev == NULL)) {
9927 /* Insert it on the wheel since it is not
9930 sctp_insert_on_wheel(asoc, strq);
9932 /* Ok now we can allow pping */
9936 SOCKBUF_LOCK(&so->so_snd);
9937 chk = TAILQ_FIRST(&tmp);
9940 sctp_m_freem(chk->data);
9943 TAILQ_REMOVE(&tmp, chk, sctp_next);
9944 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
9945 sctppcbinfo.ipi_count_chunk--;
9946 asoc->chunks_on_out_queue--;
9947 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
9948 panic("Chunk count is negative");
9950 sctppcbinfo.ipi_gencnt_chunk++;
9951 chk = TAILQ_FIRST(&tmp);
9957 #ifdef SCTP_MBCNT_LOGGING
9958 sctp_log_mbcnt(SCTP_LOG_MBCNT_INCREASE,
9959 asoc->total_output_queue_size,
9961 asoc->total_output_mbuf_queue_size,
9965 SOCKBUF_LOCK(&so->so_snd);
9966 asoc->total_output_queue_size += dataout;
9967 asoc->total_output_mbuf_queue_size += mbcnt;
9968 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
9969 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
9970 so->so_snd.ssb_cc += dataout;
9971 so->so_snd.ssb_mbcnt += mbcnt;
9973 if ((srcv->sinfo_flags & MSG_EOF) &&
9974 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)
9976 int some_on_streamwheel = 0;
9978 if (!TAILQ_EMPTY(&asoc->out_wheel)) {
9979 /* Check to see if some data queued */
9980 struct sctp_stream_out *outs;
9981 TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) {
9982 if (!TAILQ_EMPTY(&outs->outqueue)) {
9983 some_on_streamwheel = 1;
9988 if (TAILQ_EMPTY(&asoc->send_queue) &&
9989 TAILQ_EMPTY(&asoc->sent_queue) &&
9990 (some_on_streamwheel == 0)) {
9991 /* there is nothing queued to send, so I'm done... */
9992 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
9993 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
9994 /* only send SHUTDOWN the first time through */
9996 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
9997 kprintf("%s:%d sends a shutdown\n",
10003 sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
10004 asoc->state = SCTP_STATE_SHUTDOWN_SENT;
10005 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
10006 asoc->primary_destination);
10007 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
10008 asoc->primary_destination);
10012 * we still got (or just got) data to send, so set
10016 * XXX sockets draft says that MSG_EOF should be sent
10017 * with no data. currently, we will allow user data
10018 * to be sent first and move to SHUTDOWN-PENDING
10020 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
10025 if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
10026 kprintf("++total out:%d total_mbuf_out:%d\n",
10027 (int)asoc->total_output_queue_size,
10028 (int)asoc->total_output_mbuf_queue_size);
10033 ssb_unlock(&so->so_snd);
10035 SOCKBUF_UNLOCK(&so->so_snd);
10044 sctp_sosend(struct socket *so,
10046 struct mbuf *addr_mbuf,
10048 struct sockaddr *addr,
10052 struct mbuf *control,
10053 #if defined(__NetBSD__) || defined(__APPLE__)
10057 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
10065 int error, use_rcvinfo;
10066 int queue_only = 0, queue_only_for_init=0;
10069 struct sctp_inpcb *inp;
10070 struct sctp_tcb *stcb=NULL;
10071 struct sctp_sndrcvinfo srcv;
10072 struct timeval now;
10073 struct sctp_nets *net;
10074 struct sctp_association *asoc;
10075 struct sctp_inpcb *t_inp;
10076 int create_lock_applied = 0;
10077 #if defined(__APPLE__)
10078 struct proc *p = current_proc();
10079 #elif defined(__NetBSD__)
10080 struct proc *p = curproc; /* XXX */
10081 struct sockaddr *addr = NULL;
10083 addr = mtod(addr_mbuf, struct sockaddr *);
10086 error = use_rcvinfo = 0;
10090 t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
10094 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
10095 (inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING)) {
10096 /* The listner can NOT send */
10102 SCTP_ASOC_CREATE_LOCK(inp);
10103 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
10104 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
10105 /* Should I really unlock ? */
10111 create_lock_applied = 1;
10112 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
10113 (addr->sa_family == AF_INET6)) {
10119 /* now we must find the assoc */
10120 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
10121 SCTP_INP_RLOCK(inp);
10122 stcb = LIST_FIRST(&inp->sctp_asoc_list);
10123 if (stcb == NULL) {
10124 SCTP_INP_RUNLOCK(inp);
10129 SCTP_TCB_LOCK(stcb);
10130 SCTP_INP_RUNLOCK(inp);
10131 net = stcb->asoc.primary_destination;
10135 /* process cmsg snd/rcv info (maybe a assoc-id) */
10136 if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&srcv, control,
10139 if (srcv.sinfo_flags & MSG_SENDALL) {
10140 /* its a sendall */
10141 sctppcbinfo.mbuf_track--;
10142 sctp_m_freem(control);
10144 if (create_lock_applied) {
10145 SCTP_ASOC_CREATE_UNLOCK(inp);
10146 create_lock_applied = 0;
10148 return (sctp_sendall(inp, uio, top, &srcv));
10153 if (stcb == NULL) {
10154 /* Need to do a lookup */
10155 if (use_rcvinfo && srcv.sinfo_assoc_id) {
10156 stcb = sctp_findassociation_ep_asocid(inp, srcv.sinfo_assoc_id);
10158 * Question: Should I error here if the assoc_id is
10159 * no longer valid? i.e. I can't find it?
10163 /* Must locate the net structure */
10164 net = sctp_findnet(stcb, addr);
10167 if (stcb == NULL) {
10168 if (addr != NULL) {
10169 /* Since we did not use findep we must
10170 * increment it, and if we don't find a
10171 * tcb decrement it.
10173 SCTP_INP_WLOCK(inp);
10174 SCTP_INP_INCR_REF(inp);
10175 SCTP_INP_WUNLOCK(inp);
10176 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
10177 if (stcb == NULL) {
10178 SCTP_INP_WLOCK(inp);
10179 SCTP_INP_DECR_REF(inp);
10180 SCTP_INP_WUNLOCK(inp);
10185 if ((stcb == NULL) &&
10186 (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) {
10190 } else if ((stcb == NULL) && (addr == NULL)) {
10194 } else if (stcb == NULL) {
10195 /* UDP style, we must go ahead and start the INIT process */
10196 if ((use_rcvinfo) &&
10197 (srcv.sinfo_flags & MSG_ABORT)) {
10198 /* User asks to abort a non-existant asoc */
10203 /* get an asoc/stcb struct */
10204 stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0);
10205 if (stcb == NULL) {
10206 /* Error is setup for us in the call */
10210 if (create_lock_applied) {
10211 SCTP_ASOC_CREATE_UNLOCK(inp);
10212 create_lock_applied = 0;
10214 kprintf("Huh-3? create lock should have been on??\n");
10216 /* Turn on queue only flag to prevent data from being sent */
10218 asoc = &stcb->asoc;
10219 asoc->state = SCTP_STATE_COOKIE_WAIT;
10220 SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
10222 /* see if a init structure exists in cmsg headers */
10223 struct sctp_initmsg initm;
10225 if (sctp_find_cmsg(SCTP_INIT, (void *)&initm, control, sizeof(initm))) {
10226 /* we have an INIT override of the default */
10227 if (initm.sinit_max_attempts)
10228 asoc->max_init_times = initm.sinit_max_attempts;
10229 if (initm.sinit_num_ostreams)
10230 asoc->pre_open_streams = initm.sinit_num_ostreams;
10231 if (initm.sinit_max_instreams)
10232 asoc->max_inbound_streams = initm.sinit_max_instreams;
10233 if (initm.sinit_max_init_timeo)
10234 asoc->initial_init_rto_max = initm.sinit_max_init_timeo;
10235 if (asoc->streamoutcnt < asoc->pre_open_streams) {
10236 /* Default is NOT correct */
10238 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
10239 kprintf("Ok, defout:%d pre_open:%d\n",
10240 asoc->streamoutcnt, asoc->pre_open_streams);
10243 kfree(asoc->strmout, M_PCB);
10244 asoc->strmout = NULL;
10245 asoc->streamoutcnt = asoc->pre_open_streams;
10247 /* What happesn if this fails? .. we panic ...*/
10249 kmalloc(asoc->streamoutcnt *
10250 sizeof(struct sctp_stream_out),
10252 for (i = 0; i < asoc->streamoutcnt; i++) {
10254 * inbound side must be set to 0xffff,
10255 * also NOTE when we get the INIT-ACK
10256 * back (for INIT sender) we MUST
10257 * reduce the count (streamoutcnt) but
10258 * first check if we sent to any of the
10259 * upper streams that were dropped (if
10260 * some were). Those that were dropped
10261 * must be notified to the upper layer
10262 * as failed to send.
10264 asoc->strmout[i].next_sequence_sent = 0x0;
10265 TAILQ_INIT(&asoc->strmout[i].outqueue);
10266 asoc->strmout[i].stream_no = i;
10267 asoc->strmout[i].next_spoke.tqe_next = 0;
10268 asoc->strmout[i].next_spoke.tqe_prev = 0;
10274 /* out with the INIT */
10275 queue_only_for_init = 1;
10276 sctp_send_initiate(inp, stcb);
10278 * we may want to dig in after this call and adjust the MTU
10279 * value. It defaulted to 1500 (constant) but the ro structure
10280 * may now have an update and thus we may need to change it
10281 * BEFORE we append the message.
10283 net = stcb->asoc.primary_destination;
10284 asoc = &stcb->asoc;
10286 asoc = &stcb->asoc;
10288 if (create_lock_applied) {
10289 SCTP_ASOC_CREATE_UNLOCK(inp);
10290 create_lock_applied = 0;
10292 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
10293 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
10296 if (use_rcvinfo == 0) {
10297 /* Grab the default stuff from the asoc */
10298 srcv = stcb->asoc.def_send;
10300 /* we are now done with all control */
10302 sctp_m_freem(control);
10306 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
10307 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
10308 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
10309 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
10310 if ((use_rcvinfo) &&
10311 (srcv.sinfo_flags & MSG_ABORT)) {
10314 error = ECONNRESET;
10319 /* Ok, we will attempt a msgsnd :> */
10321 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
10322 p->td_lwp->lwp_ru.ru_msgsnd++;
10324 p->p_stats->p_ru.ru_msgsnd++;
10328 if (net && ((srcv.sinfo_flags & MSG_ADDR_OVER))) {
10329 /* we take the override or the unconfirmed */
10332 net = stcb->asoc.primary_destination;
10337 /* Must copy it all in from user land. The
10338 * socket buf is locked but we don't suspend
10339 * protocol processing until we are ready to
10343 error = sctp_copy_it_in(inp, stcb, asoc, net, &srcv, uio, flags);
10347 /* Here we must either pull in the user data to chunk
10348 * buffers, or use top to do a msg_append.
10350 error = sctp_msg_append(stcb, net, top, &srcv, flags);
10354 /* zap the top since it is now being used */
10358 if (net->flight_size > net->cwnd) {
10359 sctp_pegs[SCTP_SENDTO_FULL_CWND]++;
10362 } else if (asoc->ifp_had_enobuf) {
10363 sctp_pegs[SCTP_QUEONLY_BURSTLMT]++;
10366 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
10367 ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) * sizeof(struct sctp_data_chunk)) +
10368 SCTP_MED_OVERHEAD);
10370 if (((inp->sctp_flags & SCTP_PCB_FLAGS_NODELAY) == 0) &&
10371 (stcb->asoc.total_flight > 0) &&
10372 (un_sent < (int)stcb->asoc.smallest_mtu)) {
10374 /* Ok, Nagle is set on and we have data outstanding. Don't
10375 * send anything and let SACKs drive out the data unless we
10376 * have a "full" segment to send.
10378 sctp_pegs[SCTP_NAGLE_NOQ]++;
10381 sctp_pegs[SCTP_NAGLE_OFF]++;
10384 if (queue_only_for_init) {
10385 /* It is possible to have a turn around of the
10386 * INIT/INIT-ACK/COOKIE before I have a chance to
10387 * copy in the data. In such a case I DO want to
10388 * send it out by reversing the queue only flag.
10390 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) ||
10391 (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_ECHOED)) {
10392 /* yep, reverse it */
10397 if ((queue_only == 0) && (stcb->asoc.peers_rwnd && un_sent)) {
10398 /* we can attempt to send too.*/
10400 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
10401 kprintf("USR Send calls sctp_chunk_output\n");
10405 sctp_pegs[SCTP_OUTPUT_FRM_SND]++;
10406 sctp_chunk_output(inp, stcb, 0);
10408 } else if ((queue_only == 0) &&
10409 (stcb->asoc.peers_rwnd == 0) &&
10410 (stcb->asoc.total_flight == 0)) {
10411 /* We get to have a probe outstanding */
10413 sctp_from_user_send = 1;
10414 sctp_chunk_output(inp, stcb, 0);
10415 sctp_from_user_send = 0;
10418 } else if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
10419 int num_out, reason, cwnd_full;
10420 /* Here we do control only */
10422 sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
10423 &reason, 1, &cwnd_full, 1, &now, &now_filled);
10427 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
10428 kprintf("USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d \n",
10429 queue_only, stcb->asoc.peers_rwnd, un_sent,
10430 stcb->asoc.total_flight, stcb->asoc.chunks_on_out_queue,
10431 stcb->asoc.total_output_queue_size);
10435 if (create_lock_applied) {
10436 SCTP_ASOC_CREATE_UNLOCK(inp);
10437 create_lock_applied = 0;
10440 SCTP_TCB_UNLOCK(stcb);
10444 sctp_m_freem(control);