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 /* Now look at the binding flag to see if V4 will be legal */
3498 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
3499 (in_inp->inp_flags & IN6P_IPV6_V6ONLY)
3500 #elif defined(__OpenBSD__)
3501 (0) /* For openbsd we do dual bind only */
3503 (((struct in6pcb *)in_inp)->in6p_flags & IN6P_IPV6_V6ONLY)
3506 stc.ipv4_addr_legal = 1;
3508 /* V4 addresses are NOT legal on the association */
3509 stc.ipv4_addr_legal = 0;
3512 /* Its a V4 socket, no - V6 */
3513 stc.ipv4_addr_legal = 1;
3514 stc.ipv6_addr_legal = 0;
3517 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
3522 /* now for scope setup */
3523 memset((caddr_t)&store, 0, sizeof(store));
3524 sin = (struct sockaddr_in *)&store;
3525 sin6 = (struct sockaddr_in6 *)&store;
3527 to = (struct sockaddr *)&store;
3528 iph = mtod(init_pkt, struct ip *);
3529 if (iph->ip_v == IPVERSION) {
3530 struct in_addr addr;
3531 struct route iproute;
3533 sin->sin_family = AF_INET;
3534 sin->sin_len = sizeof(struct sockaddr_in);
3535 sin->sin_port = sh->src_port;
3536 sin->sin_addr = iph->ip_src;
3537 /* lookup address */
3538 stc.address[0] = sin->sin_addr.s_addr;
3542 stc.addr_type = SCTP_IPV4_ADDRESS;
3543 /* local from address */
3544 memset(&iproute, 0, sizeof(iproute));
3546 memcpy(&ro->ro_dst, sin, sizeof(*sin));
3547 addr = sctp_ipv4_source_address_selection(inp, NULL,
3552 stc.laddress[0] = addr.s_addr;
3553 stc.laddress[1] = 0;
3554 stc.laddress[2] = 0;
3555 stc.laddress[3] = 0;
3556 stc.laddr_type = SCTP_IPV4_ADDRESS;
3557 /* scope_id is only for v6 */
3559 #ifndef SCTP_DONT_DO_PRIVADDR_SCOPE
3560 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
3565 #endif /* SCTP_DONT_DO_PRIVADDR_SCOPE */
3566 /* Must use the address in this case */
3567 if (sctp_is_address_on_local_host((struct sockaddr *)sin)) {
3568 stc.loopback_scope = 1;
3571 stc.local_scope = 1;
3573 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
3574 struct in6_addr addr;
3575 #ifdef NEW_STRUCT_ROUTE
3576 struct route iproute6;
3578 struct route_in6 iproute6;
3580 ip6 = mtod(init_pkt, struct ip6_hdr *);
3581 sin6->sin6_family = AF_INET6;
3582 sin6->sin6_len = sizeof(struct sockaddr_in6);
3583 sin6->sin6_port = sh->src_port;
3584 sin6->sin6_addr = ip6->ip6_src;
3585 /* lookup address */
3586 memcpy(&stc.address, &sin6->sin6_addr,
3587 sizeof(struct in6_addr));
3588 sin6->sin6_scope_id = 0;
3589 stc.addr_type = SCTP_IPV6_ADDRESS;
3591 if (sctp_is_address_on_local_host((struct sockaddr *)sin6)) {
3592 stc.loopback_scope = 1;
3593 stc.local_scope = 1;
3596 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
3598 * If the new destination is a LINK_LOCAL
3599 * we must have common both site and local
3600 * scope. Don't set local scope though since
3601 * we must depend on the source to be added
3602 * implicitly. We cannot assure just because
3603 * we share one link that all links are common.
3605 stc.local_scope = 0;
3608 /* we start counting for the private
3609 * address stuff at 1. since the link
3610 * local we source from won't show
3611 * up in our scoped cou8nt.
3614 /* pull out the scope_id from incoming pkt */
3615 in6_recoverscope(sin6, &ip6->ip6_src,
3616 init_pkt->m_pkthdr.rcvif);
3617 #if defined(SCTP_BASE_FREEBSD) || defined(__APPLE__) || defined(__DragonFly__)
3618 in6_embedscope(&sin6->sin6_addr, sin6, NULL,
3621 in6_embedscope(&sin6->sin6_addr, sin6);
3623 stc.scope_id = sin6->sin6_scope_id;
3624 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
3626 * If the new destination is SITE_LOCAL
3627 * then we must have site scope in common.
3631 /* local from address */
3632 memset(&iproute6, 0, sizeof(iproute6));
3633 ro = (struct route *)&iproute6;
3634 memcpy(&ro->ro_dst, sin6, sizeof(*sin6));
3635 addr = sctp_ipv6_source_address_selection(inp, NULL,
3640 memcpy(&stc.laddress, &addr, sizeof(struct in6_addr));
3641 stc.laddr_type = SCTP_IPV6_ADDRESS;
3644 /* set the scope per the existing tcb */
3645 struct sctp_nets *lnet;
3647 stc.loopback_scope = asoc->loopback_scope;
3648 stc.ipv4_scope = asoc->ipv4_local_scope;
3649 stc.site_scope = asoc->site_scope;
3650 stc.local_scope = asoc->local_scope;
3651 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
3652 if (lnet->ro._l_addr.sin6.sin6_family == AF_INET6) {
3653 if (IN6_IS_ADDR_LINKLOCAL(&lnet->ro._l_addr.sin6.sin6_addr)) {
3654 /* if we have a LL address, start counting
3662 /* use the net pointer */
3663 to = (struct sockaddr *)&net->ro._l_addr;
3664 if (to->sa_family == AF_INET) {
3665 sin = (struct sockaddr_in *)to;
3666 stc.address[0] = sin->sin_addr.s_addr;
3670 stc.addr_type = SCTP_IPV4_ADDRESS;
3671 if (net->src_addr_selected == 0) {
3672 /* strange case here, the INIT
3673 * should have did the selection.
3675 net->ro._s_addr.sin.sin_addr =
3676 sctp_ipv4_source_address_selection(inp,
3677 stcb, (struct route *)&net->ro, net, 0);
3678 net->src_addr_selected = 1;
3682 stc.laddress[0] = net->ro._s_addr.sin.sin_addr.s_addr;
3683 stc.laddress[1] = 0;
3684 stc.laddress[2] = 0;
3685 stc.laddress[3] = 0;
3686 stc.laddr_type = SCTP_IPV4_ADDRESS;
3687 } else if (to->sa_family == AF_INET6) {
3688 sin6 = (struct sockaddr_in6 *)to;
3689 memcpy(&stc.address, &sin6->sin6_addr,
3690 sizeof(struct in6_addr));
3691 stc.addr_type = SCTP_IPV6_ADDRESS;
3692 if (net->src_addr_selected == 0) {
3693 /* strange case here, the INIT
3694 * should have did the selection.
3696 net->ro._s_addr.sin6.sin6_addr =
3697 sctp_ipv6_source_address_selection(inp,
3698 stcb, (struct route *)&net->ro, net, 0);
3699 net->src_addr_selected = 1;
3701 memcpy(&stc.laddress, &net->ro._l_addr.sin6.sin6_addr,
3702 sizeof(struct in6_addr));
3703 stc.laddr_type = SCTP_IPV6_ADDRESS;
3706 /* Now lets put the SCTP header in place */
3707 initackm_out = mtod(m, struct sctp_init_msg *);
3708 initackm_out->sh.src_port = inp->sctp_lport;
3709 initackm_out->sh.dest_port = sh->src_port;
3710 initackm_out->sh.v_tag = init_chk->init.initiate_tag;
3711 /* Save it off for quick ref */
3712 stc.peers_vtag = init_chk->init.initiate_tag;
3713 initackm_out->sh.checksum = 0; /* calculate later */
3715 strncpy(stc.identification, SCTP_VERSION_STRING,
3716 min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification)));
3717 /* now the chunk header */
3718 initackm_out->msg.ch.chunk_type = SCTP_INITIATION_ACK;
3719 initackm_out->msg.ch.chunk_flags = 0;
3720 /* fill in later from mbuf we build */
3721 initackm_out->msg.ch.chunk_length = 0;
3722 /* place in my tag */
3723 if ((asoc != NULL) &&
3724 ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
3725 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED))) {
3726 /* re-use the v-tags and init-seq here */
3727 initackm_out->msg.init.initiate_tag = htonl(asoc->my_vtag);
3728 initackm_out->msg.init.initial_tsn = htonl(asoc->init_seq_number);
3730 initackm_out->msg.init.initiate_tag = htonl(sctp_select_a_tag(inp));
3731 /* get a TSN to use too */
3732 initackm_out->msg.init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
3734 /* save away my tag to */
3735 stc.my_vtag = initackm_out->msg.init.initiate_tag;
3737 /* set up some of the credits. */
3738 initackm_out->msg.init.a_rwnd = htonl(max(inp->sctp_socket->so_rcv.ssb_hiwat, SCTP_MINIMAL_RWND));
3739 /* set what I want */
3740 his_limit = ntohs(init_chk->init.num_inbound_streams);
3741 /* choose what I want */
3743 if (asoc->streamoutcnt > inp->sctp_ep.pre_open_stream_count) {
3744 i_want = asoc->streamoutcnt;
3746 i_want = inp->sctp_ep.pre_open_stream_count;
3749 i_want = inp->sctp_ep.pre_open_stream_count;
3751 if (his_limit < i_want) {
3752 /* I Want more :< */
3753 initackm_out->msg.init.num_outbound_streams = init_chk->init.num_inbound_streams;
3755 /* I can have what I want :> */
3756 initackm_out->msg.init.num_outbound_streams = htons(i_want);
3758 /* tell him his limt. */
3759 initackm_out->msg.init.num_inbound_streams =
3760 htons(inp->sctp_ep.max_open_streams_intome);
3761 /* setup the ECN pointer */
3763 /* if (inp->sctp_flags & SCTP_PCB_FLAGS_ADAPTIONEVNT) {*/
3764 if (inp->sctp_ep.adaption_layer_indicator) {
3765 struct sctp_adaption_layer_indication *ali;
3766 ali = (struct sctp_adaption_layer_indication *)(
3767 (caddr_t)initackm_out + sizeof(*initackm_out));
3768 ali->ph.param_type = htons(SCTP_ULP_ADAPTION);
3769 ali->ph.param_length = htons(sizeof(*ali));
3770 ali->indication = ntohl(inp->sctp_ep.adaption_layer_indicator);
3771 m->m_len += sizeof(*ali);
3772 ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali +
3775 ecn = (struct sctp_ecn_supported_param*)(
3776 (caddr_t)initackm_out + sizeof(*initackm_out));
3780 if (sctp_ecn == 1) {
3781 ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
3782 ecn->ph.param_length = htons(sizeof(*ecn));
3783 m->m_len += sizeof(*ecn);
3785 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
3788 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
3790 /* And now tell the peer we do pr-sctp */
3791 prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
3792 prsctp->ph.param_length = htons(sizeof(*prsctp));
3793 m->m_len += sizeof(*prsctp);
3796 /* And now tell the peer we do all the extensions */
3797 pr_supported = (struct sctp_supported_chunk_types_param *)((caddr_t)prsctp +
3800 pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
3801 pr_supported->ph.param_length = htons(sizeof(*pr_supported) + SCTP_EXT_COUNT);
3802 pr_supported->chunk_types[0] = SCTP_ASCONF;
3803 pr_supported->chunk_types[1] = SCTP_ASCONF_ACK;
3804 pr_supported->chunk_types[2] = SCTP_FORWARD_CUM_TSN;
3805 pr_supported->chunk_types[3] = SCTP_PACKET_DROPPED;
3806 pr_supported->chunk_types[4] = SCTP_STREAM_RESET;
3807 pr_supported->chunk_types[5] = 0; /* pad */
3808 pr_supported->chunk_types[6] = 0; /* pad */
3809 pr_supported->chunk_types[7] = 0; /* pad */
3811 m->m_len += (sizeof(*pr_supported) + SCTP_EXT_COUNT + SCTP_PAD_EXT_COUNT);
3812 if (sctp_ecn_nonce) {
3813 /* ECN nonce: And now tell the peer we support ECN nonce */
3814 ecn_nonce = (struct sctp_ecn_nonce_supported_param *)((caddr_t)pr_supported +
3815 sizeof(*pr_supported) + SCTP_EXT_COUNT + SCTP_PAD_EXT_COUNT);
3816 ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
3817 ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
3818 m->m_len += sizeof(*ecn_nonce);
3822 /* now the addresses */
3823 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3825 int cnt = cnt_inits_to;
3827 TAILQ_FOREACH(ifn, &ifnet, if_list) {
3828 struct ifaddr_container *ifac;
3830 if ((stc.loopback_scope == 0) &&
3831 (ifn->if_type == IFT_LOOP)) {
3833 * Skip loopback devices if loopback_scope
3838 TAILQ_FOREACH(ifac, &ifn->if_addrheads[mycpuid], ifa_link) {
3839 struct ifaddr *ifa = ifac->ifa;
3841 if (sctp_is_address_in_scope(ifa,
3842 stc.ipv4_addr_legal, stc.ipv6_addr_legal,
3843 stc.loopback_scope, stc.ipv4_scope,
3844 stc.local_scope, stc.site_scope) == 0) {
3851 TAILQ_FOREACH(ifn, &ifnet, if_list) {
3852 struct ifaddr_container *ifac;
3854 if ((stc.loopback_scope == 0) &&
3855 (ifn->if_type == IFT_LOOP)) {
3857 * Skip loopback devices if
3858 * loopback_scope not set
3862 TAILQ_FOREACH(ifac, &ifn->if_addrheads[mycpuid], ifa_link) {
3863 struct ifaddr *ifa = ifac->ifa;
3865 if (sctp_is_address_in_scope(ifa,
3866 stc.ipv4_addr_legal,
3867 stc.ipv6_addr_legal,
3868 stc.loopback_scope, stc.ipv4_scope,
3869 stc.local_scope, stc.site_scope) == 0) {
3872 m_at = sctp_add_addr_to_mbuf(m_at, ifa);
3877 struct sctp_laddr *laddr;
3880 /* First, how many ? */
3881 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3882 if (laddr->ifa == NULL) {
3885 if (laddr->ifa->ifa_addr == NULL)
3887 if (sctp_is_address_in_scope(laddr->ifa,
3888 stc.ipv4_addr_legal, stc.ipv6_addr_legal,
3889 stc.loopback_scope, stc.ipv4_scope,
3890 stc.local_scope, stc.site_scope) == 0) {
3895 /* If we bind a single address only we won't list
3896 * any. This way you can get through a NAT
3899 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3900 if (laddr->ifa == NULL) {
3902 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
3903 kprintf("Help I have fallen and I can't get up!\n");
3908 if (laddr->ifa->ifa_addr == NULL)
3910 if (sctp_is_address_in_scope(laddr->ifa,
3911 stc.ipv4_addr_legal, stc.ipv6_addr_legal,
3912 stc.loopback_scope, stc.ipv4_scope,
3913 stc.local_scope, stc.site_scope) == 0) {
3916 m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa);
3921 /* tack on the operational error if present */
3923 if (op_err->m_pkthdr.len % 4) {
3924 /* must add a pad to the param */
3927 padlen = 4 - (op_err->m_pkthdr.len % 4);
3928 m_copyback(op_err, op_err->m_pkthdr.len, padlen, (caddr_t)&cpthis);
3930 while (m_at->m_next != NULL) {
3931 m_at = m_at->m_next;
3933 m_at->m_next = op_err;
3934 while (m_at->m_next != NULL) {
3935 m_at = m_at->m_next;
3938 /* Get total size of init packet */
3939 sz_of = SCTP_SIZE32(ntohs(init_chk->ch.chunk_length));
3940 /* pre-calulate the size and update pkt header and chunk header */
3941 m->m_pkthdr.len = 0;
3942 for (m_tmp = m; m_tmp; m_tmp = m_tmp->m_next) {
3943 m->m_pkthdr.len += m_tmp->m_len;
3944 if (m_tmp->m_next == NULL) {
3945 /* m_tmp should now point to last one */
3950 * Figure now the size of the cookie. We know the size of the
3951 * INIT-ACK. The Cookie is going to be the size of INIT, INIT-ACK,
3952 * COOKIE-STRUCTURE and SIGNATURE.
3956 * take our earlier INIT calc and add in the sz we just calculated
3957 * minus the size of the sctphdr (its not included in chunk size
3960 /* add once for the INIT-ACK */
3961 sz_of += (m->m_pkthdr.len - sizeof(struct sctphdr));
3963 /* add a second time for the INIT-ACK in the cookie */
3964 sz_of += (m->m_pkthdr.len - sizeof(struct sctphdr));
3966 /* Now add the cookie header and cookie message struct */
3967 sz_of += sizeof(struct sctp_state_cookie_param);
3968 /* ...and add the size of our signature */
3969 sz_of += SCTP_SIGNATURE_SIZE;
3970 initackm_out->msg.ch.chunk_length = htons(sz_of);
3972 /* Now we must build a cookie */
3973 m_cookie = sctp_add_cookie(inp, init_pkt, offset, m,
3974 sizeof(struct sctphdr), &stc);
3975 if (m_cookie == NULL) {
3976 /* memory problem */
3980 /* Now append the cookie to the end and update the space/size */
3981 m_tmp->m_next = m_cookie;
3984 * We pass 0 here to NOT set IP_DF if its IPv4, we ignore the
3985 * return here since the timer will drive a retranmission.
3987 padval = m->m_pkthdr.len % 4;
3988 if ((padval) && (m_last)) {
3989 /* see my previous comments on m_last */
3991 ret = sctp_add_pad_tombuf(m_last, (4-padval));
3993 /* Houston we have a problem, no space */
3997 m->m_pkthdr.len += padval;
3999 sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, 0, NULL, 0);
4004 sctp_insert_on_wheel(struct sctp_association *asoc,
4005 struct sctp_stream_out *strq)
4007 struct sctp_stream_out *stre, *strn;
4008 stre = TAILQ_FIRST(&asoc->out_wheel);
4010 /* only one on wheel */
4011 TAILQ_INSERT_HEAD(&asoc->out_wheel, strq, next_spoke);
4014 for (; stre; stre = strn) {
4015 strn = TAILQ_NEXT(stre, next_spoke);
4016 if (stre->stream_no > strq->stream_no) {
4017 TAILQ_INSERT_BEFORE(stre, strq, next_spoke);
4019 } else if (stre->stream_no == strq->stream_no) {
4020 /* huh, should not happen */
4022 } else if (strn == NULL) {
4023 /* next one is null */
4024 TAILQ_INSERT_AFTER(&asoc->out_wheel, stre, strq,
4031 sctp_remove_from_wheel(struct sctp_association *asoc,
4032 struct sctp_stream_out *strq)
4034 /* take off and then setup so we know it is not on the wheel */
4035 TAILQ_REMOVE(&asoc->out_wheel, strq, next_spoke);
4036 strq->next_spoke.tqe_next = NULL;
4037 strq->next_spoke.tqe_prev = NULL;
4042 sctp_prune_prsctp(struct sctp_tcb *stcb,
4043 struct sctp_association *asoc,
4044 struct sctp_sndrcvinfo *srcv,
4049 struct sctp_tmit_chunk *chk, *nchk;
4050 if ((asoc->peer_supports_prsctp) && (asoc->sent_queue_cnt_removeable > 0)) {
4051 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
4053 * Look for chunks marked with the PR_SCTP
4054 * flag AND the buffer space flag. If the one
4055 * being sent is equal or greater priority then
4056 * purge the old one and free some space.
4058 if ((chk->flags & (SCTP_PR_SCTP_ENABLED |
4059 SCTP_PR_SCTP_BUFFER)) ==
4060 (SCTP_PR_SCTP_ENABLED|SCTP_PR_SCTP_BUFFER)) {
4062 * This one is PR-SCTP AND buffer space
4065 if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
4066 /* Lower numbers equates to
4067 * higher priority so if the
4068 * one we are looking at has a
4069 * larger or equal priority we
4070 * want to drop the data and
4071 * NOT retransmit it.
4080 if (chk->sent > SCTP_DATAGRAM_UNSENT)
4081 cause = SCTP_RESPONSE_TO_USER_REQ|SCTP_NOTIFY_DATAGRAM_SENT;
4083 cause = SCTP_RESPONSE_TO_USER_REQ|SCTP_NOTIFY_DATAGRAM_UNSENT;
4084 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
4087 freed_spc += ret_spc;
4088 if (freed_spc >= dataout) {
4091 } /* if chunk was present */
4092 } /* if of sufficent priority */
4093 } /* if chunk has enabled */
4094 } /* tailqforeach */
4096 chk = TAILQ_FIRST(&asoc->send_queue);
4098 nchk = TAILQ_NEXT(chk, sctp_next);
4099 /* Here we must move to the sent queue and mark */
4100 if ((chk->flags & (SCTP_PR_SCTP_ENABLED |
4101 SCTP_PR_SCTP_BUFFER)) ==
4102 (SCTP_PR_SCTP_ENABLED|SCTP_PR_SCTP_BUFFER)) {
4103 if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
4110 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
4111 SCTP_RESPONSE_TO_USER_REQ|SCTP_NOTIFY_DATAGRAM_UNSENT,
4114 freed_spc += ret_spc;
4115 if (freed_spc >= dataout) {
4118 } /* end if chk->data */
4119 } /* end if right class */
4120 } /* end if chk pr-sctp */
4122 } /* end while (chk) */
4123 } /* if enabled in asoc */
4127 sctp_prepare_chunk(struct sctp_tmit_chunk *template,
4128 struct sctp_tcb *stcb,
4129 struct sctp_sndrcvinfo *srcv,
4130 struct sctp_stream_out *strq,
4131 struct sctp_nets *net)
4133 bzero(template, sizeof(struct sctp_tmit_chunk));
4134 template->sent = SCTP_DATAGRAM_UNSENT;
4135 if ((stcb->asoc.peer_supports_prsctp) &&
4136 (srcv->sinfo_flags & (MSG_PR_SCTP_TTL|MSG_PR_SCTP_BUF)) &&
4137 (srcv->sinfo_timetolive > 0)
4140 * Peer supports PR-SCTP
4141 * The flags is set against this send for PR-SCTP
4142 * And timetolive is a postive value, zero is reserved
4143 * to mean a reliable send for both buffer/time
4146 if (srcv->sinfo_flags & MSG_PR_SCTP_BUF) {
4148 * Time to live is a priority stored in tv_sec
4149 * when doing the buffer drop thing.
4151 template->rec.data.timetodrop.tv_sec = srcv->sinfo_timetolive;
4155 SCTP_GETTIME_TIMEVAL(&template->rec.data.timetodrop);
4156 tv.tv_sec = srcv->sinfo_timetolive / 1000;
4157 tv.tv_usec = (srcv->sinfo_timetolive * 1000) % 1000000;
4159 timeradd(&template->rec.data.timetodrop, &tv,
4160 &template->rec.data.timetodrop);
4162 timevaladd(&template->rec.data.timetodrop, &tv);
4166 if ((srcv->sinfo_flags & MSG_UNORDERED) == 0) {
4167 template->rec.data.stream_seq = strq->next_sequence_sent;
4169 template->rec.data.stream_seq = 0;
4171 template->rec.data.TSN_seq = 0; /* not yet assigned */
4173 template->rec.data.stream_number = srcv->sinfo_stream;
4174 template->rec.data.payloadtype = srcv->sinfo_ppid;
4175 template->rec.data.context = srcv->sinfo_context;
4176 template->rec.data.doing_fast_retransmit = 0;
4177 template->rec.data.ect_nonce = 0; /* ECN Nonce */
4179 if (srcv->sinfo_flags & MSG_ADDR_OVER) {
4180 template->whoTo = net;
4182 if (stcb->asoc.primary_destination)
4183 template->whoTo = stcb->asoc.primary_destination;
4186 template->whoTo = net;
4189 /* the actual chunk flags */
4190 if (srcv->sinfo_flags & MSG_UNORDERED) {
4191 template->rec.data.rcv_flags = SCTP_DATA_UNORDERED;
4193 template->rec.data.rcv_flags = 0;
4195 /* no flags yet, FRAGMENT_OK goes here */
4196 template->flags = 0;
4198 if (stcb->asoc.peer_supports_prsctp) {
4199 if (srcv->sinfo_timetolive > 0) {
4201 * We only set the flag if timetolive (or
4202 * priority) was set to a positive number.
4203 * Zero is reserved specifically to be
4204 * EXCLUDED and sent reliable.
4206 if (srcv->sinfo_flags & MSG_PR_SCTP_TTL) {
4207 template->flags |= SCTP_PR_SCTP_ENABLED;
4209 if (srcv->sinfo_flags & MSG_PR_SCTP_BUF) {
4210 template->flags |= SCTP_PR_SCTP_BUFFER;
4214 template->asoc = &stcb->asoc;
4219 sctp_get_frag_point(struct sctp_tcb *stcb,
4220 struct sctp_association *asoc)
4224 /* For endpoints that have both 6 and 4 addresses
4225 * we must reserver room for the 6 ip header, for
4226 * those that are only dealing with V4 we use
4227 * a larger frag point.
4229 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
4230 ovh = SCTP_MED_OVERHEAD;
4232 ovh = SCTP_MED_V4_OVERHEAD;
4235 if (stcb->sctp_ep->sctp_frag_point > asoc->smallest_mtu)
4236 siz = asoc->smallest_mtu - ovh;
4238 siz = (stcb->sctp_ep->sctp_frag_point - ovh);
4240 if (siz > (MCLBYTES-sizeof(struct sctp_data_chunk))) { */
4241 /* A data chunk MUST fit in a cluster */
4242 /* siz = (MCLBYTES - sizeof(struct sctp_data_chunk));*/
4246 /* make it an even word boundary please */
4251 extern unsigned int sctp_max_chunks_on_queue;
4253 #define SBLOCKWAIT(f) (((f)&MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
4256 sctp_msg_append(struct sctp_tcb *stcb,
4257 struct sctp_nets *net,
4259 struct sctp_sndrcvinfo *srcv,
4263 struct sctp_association *asoc;
4264 struct sctp_stream_out *strq;
4265 struct sctp_tmit_chunk *chk;
4266 struct sctpchunk_listhead tmp;
4267 struct sctp_tmit_chunk template;
4268 struct mbuf *n, *mnext;
4270 unsigned int dataout, siz;
4275 if ((stcb == NULL) || (net == NULL) || (m == NULL) || (srcv == NULL)) {
4276 /* Software fault, you blew it on the call */
4278 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
4279 kprintf("software error in sctp_msg_append:1\n");
4280 kprintf("stcb:%p net:%p m:%p srcv:%p\n",
4281 stcb, net, m, srcv);
4288 so = stcb->sctp_socket;
4290 if (srcv->sinfo_flags & MSG_ABORT) {
4291 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
4292 (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_ECHOED)) {
4293 /* It has to be up before we abort */
4294 /* how big is the user initiated abort? */
4295 if ((m->m_flags & M_PKTHDR) && (m->m_pkthdr.len)) {
4296 dataout = m->m_pkthdr.len;
4300 for (n = m; n; n = n->m_next) {
4301 dataout += n->m_len;
4304 M_PREPEND(m, sizeof(struct sctp_paramhdr), MB_DONTWAIT);
4306 struct sctp_paramhdr *ph;
4307 m->m_len = sizeof(struct sctp_paramhdr) + dataout;
4308 ph = mtod(m, struct sctp_paramhdr *);
4309 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
4310 ph->param_length = htons(m->m_len);
4312 sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, m);
4315 /* Only free if we don't send an abort */
4320 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
4321 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
4322 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
4323 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
4324 /* got data while shutting down */
4329 if (srcv->sinfo_stream >= asoc->streamoutcnt) {
4330 /* Invalid stream number */
4334 if (asoc->strmout == NULL) {
4335 /* huh? software error */
4337 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
4338 kprintf("software error in sctp_msg_append:2\n");
4344 strq = &asoc->strmout[srcv->sinfo_stream];
4345 /* how big is it ? */
4346 if ((m->m_flags & M_PKTHDR) && (m->m_pkthdr.len)) {
4347 dataout = m->m_pkthdr.len;
4351 for (n = m; n; n = n->m_next) {
4352 dataout += n->m_len;
4356 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
4357 kprintf("Attempt to send out %d bytes\n",
4362 /* lock the socket buf */
4363 SOCKBUF_LOCK(&so->so_snd);
4364 error = ssb_lock(&so->so_snd, SBLOCKWAIT(flags));
4368 if (dataout > so->so_snd.ssb_hiwat) {
4369 /* It will NEVER fit */
4373 if ((srcv->sinfo_flags & MSG_EOF) &&
4374 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
4379 if ((so->so_snd.ssb_hiwat <
4380 (dataout + asoc->total_output_queue_size)) ||
4381 (asoc->chunks_on_out_queue > sctp_max_chunks_on_queue) ||
4382 (asoc->total_output_mbuf_queue_size >
4383 so->so_snd.ssb_mbmax)
4385 /* XXX Buffer space hunt for data to skip */
4386 if (asoc->peer_supports_prsctp) {
4387 sctp_prune_prsctp(stcb, asoc, srcv, dataout);
4389 while ((so->so_snd.ssb_hiwat <
4390 (dataout + asoc->total_output_queue_size)) ||
4391 (asoc->chunks_on_out_queue > sctp_max_chunks_on_queue) ||
4392 (asoc->total_output_mbuf_queue_size >
4393 so->so_snd.ssb_mbmax)) {
4394 struct sctp_inpcb *inp;
4395 /* Now did we free up enough room? */
4396 if (flags & (MSG_FNONBLOCKING|MSG_DONTWAIT)) {
4397 /* Non-blocking io in place */
4398 error = EWOULDBLOCK;
4402 * We store off a pointer to the endpoint.
4403 * Since on return from this we must check to
4404 * see if an so_error is set. If so we may have
4405 * been reset and our stcb destroyed. Returning
4406 * an error will cause the correct error return
4407 * through and fix this all.
4409 inp = stcb->sctp_ep;
4411 * Not sure how else to do this since
4412 * the level we suspended at is not
4413 * known deep down where we are. I will
4414 * drop to spl0() so that others can
4418 inp->sctp_tcb_at_block = (void *)stcb;
4419 inp->error_on_block = 0;
4420 ssb_unlock(&so->so_snd);
4421 error = ssb_wait(&so->so_snd);
4423 * XXX: This is ugly but I have
4424 * recreated most of what goes on to
4425 * block in the sb. UGHH
4426 * May want to add the bit about being
4427 * no longer connected.. but this then
4428 * further dooms the UDP model NOT to
4431 inp->sctp_tcb_at_block = 0;
4432 if (inp->error_on_block)
4433 error = inp->error_on_block;
4435 error = so->so_error;
4439 error = ssb_lock(&so->so_snd, M_WAITOK);
4442 /* Otherwise we cycle back and recheck
4445 #if defined(__FreeBSD__) && __FreeBSD_version >= 502115
4446 if (so->so_rcv.sb_state & SBS_CANTSENDMORE) {
4448 if (so->so_state & SS_CANTSENDMORE) {
4454 error = so->so_error;
4459 /* If we have a packet header fix it if it was broke */
4460 if (m->m_flags & M_PKTHDR) {
4461 m->m_pkthdr.len = dataout;
4463 /* use the smallest one, user set value or
4464 * smallest mtu of the asoc
4466 siz = sctp_get_frag_point(stcb, asoc);
4467 SOCKBUF_UNLOCK(&so->so_snd);
4468 if ((dataout) && (dataout <= siz)) {
4470 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
4473 SOCKBUF_LOCK(&so->so_snd);
4476 sctp_prepare_chunk(chk, stcb, srcv, strq, net);
4477 chk->whoTo->ref_count++;
4478 chk->rec.data.rcv_flags |= SCTP_DATA_NOT_FRAG;
4480 /* no flags yet, FRAGMENT_OK goes here */
4481 sctppcbinfo.ipi_count_chunk++;
4482 sctppcbinfo.ipi_gencnt_chunk++;
4483 asoc->chunks_on_out_queue++;
4486 /* Total in the MSIZE */
4487 for (mm = chk->data; mm; mm = mm->m_next) {
4489 if (mm->m_flags & M_EXT) {
4490 mbcnt += chk->data->m_ext.ext_size;
4493 /* fix up the send_size if it is not present */
4494 chk->send_size = dataout;
4495 chk->book_size = chk->send_size;
4497 /* ok, we are commited */
4498 if ((srcv->sinfo_flags & MSG_UNORDERED) == 0) {
4499 /* bump the ssn if we are unordered. */
4500 strq->next_sequence_sent++;
4502 chk->data->m_nextpkt = 0;
4503 asoc->stream_queue_cnt++;
4504 TAILQ_INSERT_TAIL(&strq->outqueue, chk, sctp_next);
4505 /* now check if this stream is on the wheel */
4506 if ((strq->next_spoke.tqe_next == NULL) &&
4507 (strq->next_spoke.tqe_prev == NULL)) {
4508 /* Insert it on the wheel since it is not
4511 sctp_insert_on_wheel(asoc, strq);
4513 } else if ((dataout) && (dataout > siz)) {
4515 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NO_FRAGMENT) &&
4518 SOCKBUF_LOCK(&so->so_snd);
4521 /* setup the template */
4522 sctp_prepare_chunk(&template, stcb, srcv, strq, net);
4525 while (dataout > siz) {
4527 * We can wait since this is called from the user
4530 n->m_nextpkt = m_split(n, siz, MB_WAIT);
4531 if (n->m_nextpkt == NULL) {
4533 SOCKBUF_LOCK(&so->so_snd);
4540 * ok, now we have a chain on m where m->m_nextpkt points to
4541 * the next chunk and m/m->m_next chain is the piece to send.
4542 * We must go through the chains and thread them on to
4543 * sctp_tmit_chunk chains and place them all on the stream
4544 * queue, breaking the m->m_nextpkt pointers as we go.
4550 * first go through and allocate a sctp_tmit chunk
4551 * for each chunk piece
4553 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
4556 * ok we must spin through and dump anything
4557 * we have allocated and then jump to the
4560 chk = TAILQ_FIRST(&tmp);
4562 TAILQ_REMOVE(&tmp, chk, sctp_next);
4563 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
4564 sctppcbinfo.ipi_count_chunk--;
4565 asoc->chunks_on_out_queue--;
4566 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
4567 panic("Chunk count is negative");
4569 sctppcbinfo.ipi_gencnt_chunk++;
4570 chk = TAILQ_FIRST(&tmp);
4573 SOCKBUF_LOCK(&so->so_snd);
4576 sctppcbinfo.ipi_count_chunk++;
4577 asoc->chunks_on_out_queue++;
4579 sctppcbinfo.ipi_gencnt_chunk++;
4581 chk->whoTo->ref_count++;
4583 /* Total in the MSIZE */
4585 for (mm = chk->data; mm; mm = mm->m_next) {
4587 if (mm->m_flags & M_EXT) {
4588 mbcnt_e += chk->data->m_ext.ext_size;
4591 /* now fix the chk->send_size */
4592 if (chk->data->m_flags & M_PKTHDR) {
4593 chk->send_size = chk->data->m_pkthdr.len;
4597 for (nn = chk->data; nn; nn = nn->m_next) {
4598 chk->send_size += nn->m_len;
4601 chk->book_size = chk->send_size;
4602 chk->mbcnt = mbcnt_e;
4604 if (chk->flags & SCTP_PR_SCTP_BUFFER) {
4605 asoc->sent_queue_cnt_removeable++;
4608 TAILQ_INSERT_TAIL(&tmp, chk, sctp_next);
4611 /* now that we have enough space for all de-couple the
4612 * chain of mbufs by going through our temp array
4613 * and breaking the pointers.
4615 /* ok, we are commited */
4616 if ((srcv->sinfo_flags & MSG_UNORDERED) == 0) {
4617 /* bump the ssn if we are unordered. */
4618 strq->next_sequence_sent++;
4620 /* Mark the first/last flags. This will
4621 * result int a 3 for a single item on the list
4623 chk = TAILQ_FIRST(&tmp);
4624 chk->rec.data.rcv_flags |= SCTP_DATA_FIRST_FRAG;
4625 chk = TAILQ_LAST(&tmp, sctpchunk_listhead);
4626 chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
4627 /* now break any chains on the queue and
4628 * move it to the streams actual queue.
4630 chk = TAILQ_FIRST(&tmp);
4632 chk->data->m_nextpkt = 0;
4633 TAILQ_REMOVE(&tmp, chk, sctp_next);
4634 asoc->stream_queue_cnt++;
4635 TAILQ_INSERT_TAIL(&strq->outqueue, chk, sctp_next);
4636 chk = TAILQ_FIRST(&tmp);
4638 /* now check if this stream is on the wheel */
4639 if ((strq->next_spoke.tqe_next == NULL) &&
4640 (strq->next_spoke.tqe_prev == NULL)) {
4641 /* Insert it on the wheel since it is not
4644 sctp_insert_on_wheel(asoc, strq);
4647 SOCKBUF_LOCK(&so->so_snd);
4648 /* has a SHUTDOWN been (also) requested by the user on this asoc? */
4651 if ((srcv->sinfo_flags & MSG_EOF) &&
4652 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
4654 int some_on_streamwheel = 0;
4656 if (!TAILQ_EMPTY(&asoc->out_wheel)) {
4657 /* Check to see if some data queued */
4658 struct sctp_stream_out *outs;
4659 TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) {
4660 if (!TAILQ_EMPTY(&outs->outqueue)) {
4661 some_on_streamwheel = 1;
4667 if (TAILQ_EMPTY(&asoc->send_queue) &&
4668 TAILQ_EMPTY(&asoc->sent_queue) &&
4669 (some_on_streamwheel == 0)) {
4670 /* there is nothing queued to send, so I'm done... */
4671 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
4672 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
4673 /* only send SHUTDOWN the first time through */
4675 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
4676 kprintf("%s:%d sends a shutdown\n",
4682 sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
4683 asoc->state = SCTP_STATE_SHUTDOWN_SENT;
4684 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
4685 asoc->primary_destination);
4686 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
4687 asoc->primary_destination);
4691 * we still got (or just got) data to send, so set
4695 * XXX sockets draft says that MSG_EOF should be sent
4696 * with no data. currently, we will allow user data
4697 * to be sent first and move to SHUTDOWN-PENDING
4699 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
4702 #ifdef SCTP_MBCNT_LOGGING
4703 sctp_log_mbcnt(SCTP_LOG_MBCNT_INCREASE,
4704 asoc->total_output_queue_size,
4706 asoc->total_output_mbuf_queue_size,
4709 asoc->total_output_queue_size += dataout;
4710 asoc->total_output_mbuf_queue_size += mbcnt;
4711 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4712 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
4713 so->so_snd.ssb_cc += dataout;
4714 so->so_snd.ssb_mbcnt += mbcnt;
4718 if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
4719 kprintf("++total out:%d total_mbuf_out:%d\n",
4720 (int)asoc->total_output_queue_size,
4721 (int)asoc->total_output_mbuf_queue_size);
4726 ssb_unlock(&so->so_snd);
4728 SOCKBUF_UNLOCK(&so->so_snd);
4730 if (m && m->m_nextpkt) {
4733 mnext = n->m_nextpkt;
4734 n->m_nextpkt = NULL;
4744 static struct mbuf *
4745 sctp_copy_mbufchain(struct mbuf *clonechain,
4746 struct mbuf *outchain)
4748 struct mbuf *appendchain;
4749 #if defined(__FreeBSD__) || defined(__NetBSD__)
4750 /* Supposedly m_copypacket is an optimization, use it if we can */
4751 if (clonechain->m_flags & M_PKTHDR) {
4752 appendchain = m_copypacket(clonechain, MB_DONTWAIT);
4753 sctp_pegs[SCTP_CACHED_SRC]++;
4755 appendchain = m_copy(clonechain, 0, M_COPYALL);
4756 #elif defined(__APPLE__)
4757 appendchain = sctp_m_copym(clonechain, 0, M_COPYALL, MB_DONTWAIT);
4759 appendchain = m_copy(clonechain, 0, M_COPYALL);
4762 if (appendchain == NULL) {
4765 sctp_m_freem(outchain);
4769 /* tack on to the end */
4773 if (m->m_next == NULL) {
4774 m->m_next = appendchain;
4779 if (outchain->m_flags & M_PKTHDR) {
4785 append_tot += t->m_len;
4788 outchain->m_pkthdr.len += append_tot;
4792 return (appendchain);
4797 sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr, u_int32_t val)
4799 struct sctp_copy_all *ca;
4803 ca = (struct sctp_copy_all *)ptr;
4804 if (ca->m == NULL) {
4807 if (ca->inp != inp) {
4811 m = sctp_copy_mbufchain(ca->m, NULL);
4813 /* can't copy so we are done */
4817 ret = sctp_msg_append(stcb, stcb->asoc.primary_destination, m,
4818 &ca->sndrcv, MSG_FNONBLOCKING);
4827 sctp_sendall_completes(void *ptr, u_int32_t val)
4829 struct sctp_copy_all *ca;
4830 ca = (struct sctp_copy_all *)ptr;
4831 /* Do a notify here?
4832 * Kacheong suggests that the notify
4833 * be done at the send time.. so you would
4834 * push up a notification if any send failed.
4835 * Don't know if this is feasable since the
4836 * only failures we have is "memory" related and
4837 * if you cannot get an mbuf to send the data
4838 * you surely can't get an mbuf to send up
4839 * to notify the user you can't send the data :->
4842 /* now free everything */
4848 #define MC_ALIGN(m, len) do { \
4849 (m)->m_data += (MCLBYTES - (len)) & ~(sizeof(long) - 1); \
4854 static struct mbuf *
4855 sctp_copy_out_all(struct uio *uio, int len)
4857 struct mbuf *ret, *at;
4858 int left, willcpy, cancpy, error;
4860 MGETHDR(ret, MB_WAIT, MT_HEADER);
4867 ret->m_pkthdr.len = len;
4868 MCLGET(ret, MB_WAIT);
4872 if ((ret->m_flags & M_EXT) == 0) {
4876 cancpy = M_TRAILINGSPACE(ret);
4877 willcpy = min(cancpy, left);
4880 /* Align data to the end */
4881 MC_ALIGN(at, willcpy);
4882 error = uiomove(mtod(at, caddr_t), willcpy, uio);
4888 at->m_len = willcpy;
4889 at->m_nextpkt = at->m_next = 0;
4892 MGET(at->m_next, MB_WAIT, MT_DATA);
4893 if (at->m_next == NULL) {
4898 MCLGET(at, MB_WAIT);
4902 if ((at->m_flags & M_EXT) == 0) {
4905 cancpy = M_TRAILINGSPACE(at);
4906 willcpy = min(cancpy, left);
4913 sctp_sendall (struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m, struct sctp_sndrcvinfo *srcv)
4916 struct sctp_copy_all *ca;
4917 ca = kmalloc(sizeof(struct sctp_copy_all), M_PCB, M_NOWAIT);
4922 memset (ca, 0, sizeof(struct sctp_copy_all));
4926 /* take off the sendall flag, it would
4927 * be bad if we failed to do this :-0
4929 ca->sndrcv.sinfo_flags &= ~MSG_SENDALL;
4931 /* get length and mbuf chain */
4933 ca->sndlen = uio->uio_resid;
4934 ca->m = sctp_copy_out_all(uio, ca->sndlen);
4935 if (ca->m == NULL) {
4940 if ((m->m_flags & M_PKTHDR) == 0) {
4943 ca->sndlen += m->m_len;
4947 ca->sndlen = m->m_pkthdr.len;
4952 ret = sctp_initiate_iterator(sctp_sendall_iterator, SCTP_PCB_ANY_FLAGS, SCTP_ASOC_ANY_STATE,
4953 (void *)ca, 0, sctp_sendall_completes, inp);
4956 kprintf("Failed to initate iterator to takeover associations\n");
4967 sctp_toss_old_cookies(struct sctp_association *asoc)
4969 struct sctp_tmit_chunk *chk, *nchk;
4970 chk = TAILQ_FIRST(&asoc->control_send_queue);
4972 nchk = TAILQ_NEXT(chk, sctp_next);
4973 if (chk->rec.chunk_id == SCTP_COOKIE_ECHO) {
4974 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
4976 sctp_m_freem(chk->data);
4979 asoc->ctrl_queue_cnt--;
4981 sctp_free_remote_addr(chk->whoTo);
4982 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
4983 sctppcbinfo.ipi_count_chunk--;
4984 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
4985 panic("Chunk count is negative");
4987 sctppcbinfo.ipi_gencnt_chunk++;
4994 sctp_toss_old_asconf(struct sctp_tcb *stcb)
4996 struct sctp_association *asoc;
4997 struct sctp_tmit_chunk *chk, *chk_tmp;
5000 for (chk = TAILQ_FIRST(&asoc->control_send_queue); chk != NULL;
5003 chk_tmp = TAILQ_NEXT(chk, sctp_next);
5004 /* find SCTP_ASCONF chunk in queue (only one ever in queue) */
5005 if (chk->rec.chunk_id == SCTP_ASCONF) {
5006 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
5008 sctp_m_freem(chk->data);
5011 asoc->ctrl_queue_cnt--;
5013 sctp_free_remote_addr(chk->whoTo);
5014 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
5015 sctppcbinfo.ipi_count_chunk--;
5016 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
5017 panic("Chunk count is negative");
5019 sctppcbinfo.ipi_gencnt_chunk++;
5026 sctp_clean_up_datalist(struct sctp_tcb *stcb,
5027 struct sctp_association *asoc,
5028 struct sctp_tmit_chunk **data_list,
5030 struct sctp_nets *net)
5033 for (i = 0; i < bundle_at; i++) {
5034 /* off of the send queue */
5036 /* Any chunk NOT 0 you zap the time
5037 * chunk 0 gets zapped or set based on
5038 * if a RTO measurment is needed.
5040 data_list[i]->do_rtt = 0;
5043 data_list[i]->sent_rcv_time = net->last_sent_time;
5044 TAILQ_REMOVE(&asoc->send_queue,
5047 /* on to the sent queue */
5048 TAILQ_INSERT_TAIL(&asoc->sent_queue,
5051 /* This does not lower until the cum-ack passes it */
5052 asoc->sent_queue_cnt++;
5053 asoc->send_queue_cnt--;
5054 if ((asoc->peers_rwnd <= 0) &&
5055 (asoc->total_flight == 0) &&
5057 /* Mark the chunk as being a window probe */
5059 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
5060 kprintf("WINDOW PROBE SET\n");
5063 sctp_pegs[SCTP_WINDOW_PROBES]++;
5064 data_list[i]->rec.data.state_flags |= SCTP_WINDOW_PROBE;
5066 data_list[i]->rec.data.state_flags &= ~SCTP_WINDOW_PROBE;
5068 #ifdef SCTP_AUDITING_ENABLED
5069 sctp_audit_log(0xC2, 3);
5071 data_list[i]->sent = SCTP_DATAGRAM_SENT;
5072 data_list[i]->snd_count = 1;
5073 net->flight_size += data_list[i]->book_size;
5074 asoc->total_flight += data_list[i]->book_size;
5075 asoc->total_flight_count++;
5076 #ifdef SCTP_LOG_RWND
5077 sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
5078 asoc->peers_rwnd , data_list[i]->send_size, sctp_peer_chunk_oh);
5080 asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
5081 (u_int32_t)(data_list[i]->send_size + sctp_peer_chunk_oh));
5082 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
5083 /* SWS sender side engages */
5084 asoc->peers_rwnd = 0;
5090 sctp_clean_up_ctl(struct sctp_association *asoc)
5092 struct sctp_tmit_chunk *chk, *nchk;
5093 for (chk = TAILQ_FIRST(&asoc->control_send_queue);
5095 nchk = TAILQ_NEXT(chk, sctp_next);
5096 if ((chk->rec.chunk_id == SCTP_SELECTIVE_ACK) ||
5097 (chk->rec.chunk_id == SCTP_HEARTBEAT_REQUEST) ||
5098 (chk->rec.chunk_id == SCTP_HEARTBEAT_ACK) ||
5099 (chk->rec.chunk_id == SCTP_SHUTDOWN) ||
5100 (chk->rec.chunk_id == SCTP_SHUTDOWN_ACK) ||
5101 (chk->rec.chunk_id == SCTP_OPERATION_ERROR) ||
5102 (chk->rec.chunk_id == SCTP_PACKET_DROPPED) ||
5103 (chk->rec.chunk_id == SCTP_COOKIE_ACK) ||
5104 (chk->rec.chunk_id == SCTP_ECN_CWR) ||
5105 (chk->rec.chunk_id == SCTP_ASCONF_ACK)) {
5106 /* Stray chunks must be cleaned up */
5108 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
5110 sctp_m_freem(chk->data);
5113 asoc->ctrl_queue_cnt--;
5114 sctp_free_remote_addr(chk->whoTo);
5115 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
5116 sctppcbinfo.ipi_count_chunk--;
5117 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
5118 panic("Chunk count is negative");
5120 sctppcbinfo.ipi_gencnt_chunk++;
5121 } else if (chk->rec.chunk_id == SCTP_STREAM_RESET) {
5122 struct sctp_stream_reset_req *strreq;
5123 /* special handling, we must look into the param */
5124 strreq = mtod(chk->data, struct sctp_stream_reset_req *);
5125 if (strreq->sr_req.ph.param_type == ntohs(SCTP_STR_RESET_RESPONSE)) {
5126 goto clean_up_anyway;
5133 sctp_move_to_outqueue(struct sctp_tcb *stcb,
5134 struct sctp_stream_out *strq)
5136 /* Move from the stream to the send_queue keeping track of the total */
5137 struct sctp_association *asoc;
5141 struct sctp_tmit_chunk *chk, *nchk;
5142 struct sctp_data_chunk *dchkh;
5143 struct sctpchunk_listhead tmp;
5148 chk = TAILQ_FIRST(&strq->outqueue);
5150 nchk = TAILQ_NEXT(chk, sctp_next);
5151 /* now put in the chunk header */
5153 M_PREPEND(chk->data, sizeof(struct sctp_data_chunk), MB_DONTWAIT);
5154 if (chk->data == NULL) {
5159 if (orig != chk->data) {
5160 /* A new mbuf was added, account for it */
5161 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5162 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
5163 stcb->sctp_socket->so_snd.ssb_mbcnt += MSIZE;
5165 #ifdef SCTP_MBCNT_LOGGING
5166 sctp_log_mbcnt(SCTP_LOG_MBCNT_INCREASE,
5167 asoc->total_output_queue_size,
5169 asoc->total_output_mbuf_queue_size,
5172 stcb->asoc.total_output_mbuf_queue_size += MSIZE;
5173 chk->mbcnt += MSIZE;
5175 chk->send_size += sizeof(struct sctp_data_chunk);
5176 /* This should NOT have to do anything, but
5177 * I would rather be cautious
5179 if (!failed && ((size_t)chk->data->m_len < sizeof(struct sctp_data_chunk))) {
5180 m_pullup(chk->data, sizeof(struct sctp_data_chunk));
5181 if (chk->data == NULL) {
5186 dchkh = mtod(chk->data, struct sctp_data_chunk *);
5187 dchkh->ch.chunk_length = htons(chk->send_size);
5188 /* Chunks must be padded to even word boundary */
5189 padval = chk->send_size % 4;
5191 /* For fragmented messages this should not
5192 * run except possibly on the last chunk
5194 if (sctp_pad_lastmbuf(chk->data, (4 - padval))) {
5195 /* we are in big big trouble no mbufs :< */
5199 chk->send_size += (4 - padval);
5201 /* pull from stream queue */
5202 TAILQ_REMOVE(&strq->outqueue, chk, sctp_next);
5203 asoc->stream_queue_cnt--;
5204 TAILQ_INSERT_TAIL(&tmp, chk, sctp_next);
5205 /* add it in to the size of moved chunks */
5206 if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
5207 /* we pull only one message */
5213 /* Gak, we just lost the user message */
5214 chk = TAILQ_FIRST(&tmp);
5216 nchk = TAILQ_NEXT(chk, sctp_next);
5217 TAILQ_REMOVE(&tmp, chk, sctp_next);
5219 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb,
5220 (SCTP_NOTIFY_DATAGRAM_UNSENT|SCTP_INTERNAL_ERROR),
5224 sctp_m_freem(chk->data);
5228 sctp_free_remote_addr(chk->whoTo);
5231 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
5232 sctppcbinfo.ipi_count_chunk--;
5233 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
5234 panic("Chunk count is negative");
5236 sctppcbinfo.ipi_gencnt_chunk++;
5241 /* now pull them off of temp wheel */
5242 chk = TAILQ_FIRST(&tmp);
5244 nchk = TAILQ_NEXT(chk, sctp_next);
5245 /* insert on send_queue */
5246 TAILQ_REMOVE(&tmp, chk, sctp_next);
5247 TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
5248 asoc->send_queue_cnt++;
5250 chk->rec.data.TSN_seq = asoc->sending_seq++;
5252 dchkh = mtod(chk->data, struct sctp_data_chunk *);
5253 /* Put the rest of the things in place now. Size
5254 * was done earlier in previous loop prior to
5257 dchkh->ch.chunk_type = SCTP_DATA;
5258 dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
5259 dchkh->dp.tsn = htonl(chk->rec.data.TSN_seq);
5260 dchkh->dp.stream_id = htons(strq->stream_no);
5261 dchkh->dp.stream_sequence = htons(chk->rec.data.stream_seq);
5262 dchkh->dp.protocol_id = chk->rec.data.payloadtype;
5263 /* total count moved */
5264 tot_moved += chk->send_size;
5271 sctp_fill_outqueue(struct sctp_tcb *stcb,
5272 struct sctp_nets *net)
5274 struct sctp_association *asoc;
5275 struct sctp_tmit_chunk *chk;
5276 struct sctp_stream_out *strq, *strqn;
5277 int mtu_fromwheel, goal_mtu;
5278 unsigned int moved, seenend, cnt_mvd=0;
5281 /* Attempt to move at least 1 MTU's worth
5282 * onto the wheel for each destination address
5284 goal_mtu = net->cwnd - net->flight_size;
5285 if ((unsigned int)goal_mtu < net->mtu) {
5286 goal_mtu = net->mtu;
5288 if (sctp_pegs[SCTP_MOVED_MTU] < (unsigned int)goal_mtu) {
5289 sctp_pegs[SCTP_MOVED_MTU] = goal_mtu;
5291 seenend = moved = mtu_fromwheel = 0;
5292 if (asoc->last_out_stream == NULL) {
5293 strq = asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
5294 if (asoc->last_out_stream == NULL) {
5295 /* huh nothing on the wheel, TSNH */
5300 strq = TAILQ_NEXT(asoc->last_out_stream, next_spoke);
5303 asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
5305 while (mtu_fromwheel < goal_mtu) {
5309 strq = TAILQ_FIRST(&asoc->out_wheel);
5310 } else if ((moved == 0) && (seenend)) {
5311 /* none left on the wheel */
5312 sctp_pegs[SCTP_MOVED_NLEF]++;
5316 * clear the flags and rotate back through
5321 strq = TAILQ_FIRST(&asoc->out_wheel);
5327 strqn = TAILQ_NEXT(strq, next_spoke);
5328 if ((chk = TAILQ_FIRST(&strq->outqueue)) == NULL) {
5329 /* none left on this queue, prune a spoke? */
5330 sctp_remove_from_wheel(asoc, strq);
5331 if (strq == asoc->last_out_stream) {
5332 /* the last one we used went off the wheel */
5333 asoc->last_out_stream = NULL;
5338 if (chk->whoTo != net) {
5339 /* Skip this stream, first one on stream
5340 * does not head to our current destination.
5345 mtu_fromwheel += sctp_move_to_outqueue(stcb, strq);
5348 asoc->last_out_stream = strq;
5351 sctp_pegs[SCTP_MOVED_MAX]++;
5353 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5354 kprintf("Ok we moved %d chunks to send queue\n",
5358 if (sctp_pegs[SCTP_MOVED_QMAX] < cnt_mvd) {
5359 sctp_pegs[SCTP_MOVED_QMAX] = cnt_mvd;
5364 sctp_fix_ecn_echo(struct sctp_association *asoc)
5366 struct sctp_tmit_chunk *chk;
5367 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
5368 if (chk->rec.chunk_id == SCTP_ECN_ECHO) {
5369 chk->sent = SCTP_DATAGRAM_UNSENT;
5375 sctp_move_to_an_alt(struct sctp_tcb *stcb,
5376 struct sctp_association *asoc,
5377 struct sctp_nets *net)
5379 struct sctp_tmit_chunk *chk;
5380 struct sctp_nets *a_net;
5381 a_net = sctp_find_alternate_net(stcb, net);
5382 if ((a_net != net) &&
5383 ((a_net->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE)) {
5385 * We only proceed if a valid alternate is found that is
5386 * not this one and is reachable. Here we must move all
5387 * chunks queued in the send queue off of the destination
5388 * address to our alternate.
5390 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
5391 if (chk->whoTo == net) {
5392 /* Move the chunk to our alternate */
5393 sctp_free_remote_addr(chk->whoTo);
5401 static int sctp_from_user_send=0;
5404 sctp_med_chunk_output(struct sctp_inpcb *inp,
5405 struct sctp_tcb *stcb,
5406 struct sctp_association *asoc,
5409 int control_only, int *cwnd_full, int from_where,
5410 struct timeval *now, int *now_filled)
5413 * Ok this is the generic chunk service queue.
5414 * we must do the following:
5415 * - Service the stream queue that is next, moving any message
5416 * (note I must get a complete message i.e. FIRST/MIDDLE and
5417 * LAST to the out queue in one pass) and assigning TSN's
5418 * - Check to see if the cwnd/rwnd allows any output, if so we
5419 * go ahead and fomulate and send the low level chunks. Making
5420 * sure to combine any control in the control chunk queue also.
5422 struct sctp_nets *net;
5423 struct mbuf *outchain;
5424 struct sctp_tmit_chunk *chk, *nchk;
5425 struct sctphdr *shdr;
5426 /* temp arrays for unlinking */
5427 struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
5428 int no_fragmentflg, error;
5429 int one_chunk, hbflag;
5430 int asconf, cookie, no_out_cnt;
5431 int bundle_at, ctl_cnt, no_data_chunks, cwnd_full_ind;
5432 unsigned int mtu, r_mtu, omtu;
5435 ctl_cnt = no_out_cnt = asconf = cookie = 0;
5437 * First lets prime the pump. For each destination, if there
5438 * is room in the flight size, attempt to pull an MTU's worth
5439 * out of the stream queues into the general send_queue
5441 #ifdef SCTP_AUDITING_ENABLED
5442 sctp_audit_log(0xC2, 2);
5445 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5446 kprintf("***********************\n");
5455 /* Nothing to possible to send? */
5456 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
5457 TAILQ_EMPTY(&asoc->send_queue) &&
5458 TAILQ_EMPTY(&asoc->out_wheel)) {
5460 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5461 kprintf("All wheels empty\n");
5466 if (asoc->peers_rwnd <= 0) {
5467 /* No room in peers rwnd */
5470 if (asoc->total_flight > 0) {
5471 /* we are allowed one chunk in flight */
5473 sctp_pegs[SCTP_RWND_BLOCKED]++;
5477 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5478 kprintf("Ok we have done the fillup no_data_chunk=%d tf=%d prw:%d\n",
5480 (int)asoc->total_flight, (int)asoc->peers_rwnd);
5483 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5485 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5486 kprintf("net:%p fs:%d cwnd:%d\n",
5487 net, net->flight_size, net->cwnd);
5490 if (net->flight_size >= net->cwnd) {
5491 /* skip this network, no room */
5494 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5495 kprintf("Ok skip fillup->fs:%d > cwnd:%d\n",
5500 sctp_pegs[SCTP_CWND_NOFILL]++;
5504 * spin through the stream queues moving one message and
5505 * assign TSN's as appropriate.
5507 sctp_fill_outqueue(stcb, net);
5509 *cwnd_full = cwnd_full_ind;
5510 /* now service each destination and send out what we can for it */
5512 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5514 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
5517 kprintf("We have %d chunks on the send_queue\n", chk_cnt);
5519 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
5522 kprintf("We have %d chunks on the sent_queue\n", chk_cnt);
5523 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
5526 kprintf("We have %d chunks on the control_queue\n", chk_cnt);
5529 /* If we have data to send, and DSACK is running, stop it
5530 * and build a SACK to dump on to bundle with output. This
5531 * actually MAY make it so the bundling does not occur if
5532 * the SACK is big but I think this is ok because basic SACK
5533 * space is pre-reserved in our fragmentation size choice.
5535 if ((TAILQ_FIRST(&asoc->send_queue) != NULL) &&
5536 (no_data_chunks == 0)) {
5537 /* We will be sending something */
5538 if (callout_pending(&stcb->asoc.dack_timer.timer)) {
5539 /* Yep a callout is pending */
5540 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
5543 sctp_send_sack(stcb);
5546 /* Nothing to send? */
5547 if ((TAILQ_FIRST(&asoc->control_send_queue) == NULL) &&
5548 (TAILQ_FIRST(&asoc->send_queue) == NULL)) {
5551 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5552 /* how much can we send? */
5553 if (net->ref_count < 2) {
5554 /* Ref-count of 1 so we cannot have data or control
5555 * queued to this address. Skip it.
5559 ctl_cnt = bundle_at = 0;
5564 if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
5565 mtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
5567 mtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
5569 if (mtu > asoc->peers_rwnd) {
5570 if (asoc->total_flight > 0) {
5571 /* We have a packet in flight somewhere */
5572 r_mtu = asoc->peers_rwnd;
5574 /* We are always allowed to send one MTU out */
5582 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5583 kprintf("Ok r_mtu is %d mtu is %d for this net:%p one_chunk:%d\n",
5584 r_mtu, mtu, net, one_chunk);
5587 /************************/
5588 /* Control transmission */
5589 /************************/
5590 /* Now first lets go through the control queue */
5591 for (chk = TAILQ_FIRST(&asoc->control_send_queue);
5593 nchk = TAILQ_NEXT(chk, sctp_next);
5594 if (chk->whoTo != net) {
5596 * No, not sent to the network we are
5601 if (chk->data == NULL) {
5604 if ((chk->data->m_flags & M_PKTHDR) == 0) {
5606 * NOTE: the chk queue MUST have the PKTHDR
5607 * flag set on it with a total in the
5608 * m_pkthdr.len field!! else the chunk will
5613 if (chk->sent != SCTP_DATAGRAM_UNSENT) {
5615 * It must be unsent. Cookies and ASCONF's
5616 * hang around but there timers will force
5617 * when marked for resend.
5621 /* Here we do NOT factor the r_mtu */
5622 if ((chk->data->m_pkthdr.len < (int)mtu) ||
5623 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
5625 * We probably should glom the mbuf chain from
5626 * the chk->data for control but the problem
5627 * is it becomes yet one more level of
5628 * tracking to do if for some reason output
5629 * fails. Then I have got to reconstruct the
5630 * merged control chain.. el yucko.. for now
5631 * we take the easy way and do the copy
5633 outchain = sctp_copy_mbufchain(chk->data,
5635 if (outchain == NULL) {
5638 /* update our MTU size */
5639 mtu -= chk->data->m_pkthdr.len;
5643 /* Do clear IP_DF ? */
5644 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
5647 /* Mark things to be removed, if needed */
5648 if ((chk->rec.chunk_id == SCTP_SELECTIVE_ACK) ||
5649 (chk->rec.chunk_id == SCTP_HEARTBEAT_REQUEST) ||
5650 (chk->rec.chunk_id == SCTP_HEARTBEAT_ACK) ||
5651 (chk->rec.chunk_id == SCTP_SHUTDOWN) ||
5652 (chk->rec.chunk_id == SCTP_SHUTDOWN_ACK) ||
5653 (chk->rec.chunk_id == SCTP_OPERATION_ERROR) ||
5654 (chk->rec.chunk_id == SCTP_COOKIE_ACK) ||
5655 (chk->rec.chunk_id == SCTP_ECN_CWR) ||
5656 (chk->rec.chunk_id == SCTP_PACKET_DROPPED) ||
5657 (chk->rec.chunk_id == SCTP_ASCONF_ACK)) {
5659 if (chk->rec.chunk_id == SCTP_HEARTBEAT_REQUEST)
5661 /* remove these chunks at the end */
5662 if (chk->rec.chunk_id == SCTP_SELECTIVE_ACK) {
5663 /* turn off the timer */
5664 if (callout_pending(&stcb->asoc.dack_timer.timer)) {
5665 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
5672 * Other chunks, since they have
5673 * timers running (i.e. COOKIE or
5674 * ASCONF) we just "trust" that it
5675 * gets sent or retransmitted.
5678 if (chk->rec.chunk_id == SCTP_COOKIE_ECHO) {
5681 } else if (chk->rec.chunk_id == SCTP_ASCONF) {
5683 * set hb flag since we can use
5689 chk->sent = SCTP_DATAGRAM_SENT;
5694 * Ok we are out of room but we can
5695 * output without effecting the flight
5696 * size since this little guy is a
5697 * control only packet.
5700 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
5704 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
5707 if (outchain->m_len == 0) {
5709 * Special case for when you
5710 * get a 0 len mbuf at the
5711 * head due to the lack of a
5712 * MHDR at the beginning.
5714 outchain->m_len = sizeof(struct sctphdr);
5716 M_PREPEND(outchain, sizeof(struct sctphdr), MB_DONTWAIT);
5717 if (outchain == NULL) {
5720 goto error_out_again;
5723 shdr = mtod(outchain, struct sctphdr *);
5724 shdr->src_port = inp->sctp_lport;
5725 shdr->dest_port = stcb->rport;
5726 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
5729 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
5730 (struct sockaddr *)&net->ro._l_addr,
5732 no_fragmentflg, 0, NULL, asconf))) {
5733 if (error == ENOBUFS) {
5734 asoc->ifp_had_enobuf = 1;
5736 sctp_pegs[SCTP_DATA_OUT_ERR]++;
5737 if (from_where == 0) {
5738 sctp_pegs[SCTP_ERROUT_FRM_USR]++;
5742 if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
5743 kprintf("Gak got ctrl error %d\n", error);
5746 /* error, could not output */
5749 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5750 kprintf("Update HB anyway\n");
5753 if (*now_filled == 0) {
5754 SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
5756 *now = net->last_sent_time;
5758 net->last_sent_time = *now;
5762 if (error == EHOSTUNREACH) {
5765 * unreachable during
5769 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5770 kprintf("Moving data to an alternate\n");
5773 sctp_move_to_an_alt(stcb, asoc, net);
5775 sctp_clean_up_ctl (asoc);
5778 asoc->ifp_had_enobuf = 0;
5779 /* Only HB or ASCONF advances time */
5781 if (*now_filled == 0) {
5782 SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
5784 *now = net->last_sent_time;
5786 net->last_sent_time = *now;
5791 * increase the number we sent, if a
5792 * cookie is sent we don't tell them
5796 *num_out += ctl_cnt;
5797 /* recalc a clean slate and setup */
5798 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5799 mtu = (net->mtu - SCTP_MIN_OVERHEAD);
5801 mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
5807 /*********************/
5808 /* Data transmission */
5809 /*********************/
5810 /* now lets add any data within the MTU constraints */
5811 if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
5812 omtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
5814 omtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
5818 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5819 kprintf("Now to data transmission\n");
5823 if (((asoc->state & SCTP_STATE_OPEN) == SCTP_STATE_OPEN) ||
5825 for (chk = TAILQ_FIRST(&asoc->send_queue); chk; chk = nchk) {
5826 if (no_data_chunks) {
5827 /* let only control go out */
5829 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5830 kprintf("Either nothing to send or we are full\n");
5835 if (net->flight_size >= net->cwnd) {
5836 /* skip this net, no room for data */
5838 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5839 kprintf("fs:%d > cwnd:%d\n",
5840 net->flight_size, net->cwnd);
5843 sctp_pegs[SCTP_CWND_BLOCKED]++;
5847 nchk = TAILQ_NEXT(chk, sctp_next);
5848 if (chk->whoTo != net) {
5849 /* No, not sent to this net */
5851 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5852 kprintf("chk->whoTo:%p not %p\n",
5860 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5861 kprintf("Can we pick up a chunk?\n");
5864 if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
5865 /* strange, we have a chunk that is to bit
5866 * for its destination and yet no fragment ok flag.
5867 * Something went wrong when the PMTU changed...we did
5868 * not mark this chunk for some reason?? I will
5869 * fix it here by letting IP fragment it for now and
5870 * printing a warning. This really should not happen ...
5872 /*#ifdef SCTP_DEBUG*/
5873 kprintf("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
5874 chk->send_size, mtu);
5876 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
5879 if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
5880 ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
5881 /* ok we will add this one */
5883 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5884 kprintf("Picking up the chunk\n");
5887 outchain = sctp_copy_mbufchain(chk->data, outchain);
5888 if (outchain == NULL) {
5890 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5891 kprintf("Gakk no memory\n");
5894 if (!callout_pending(&net->rxt_timer.timer)) {
5895 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
5899 /* upate our MTU size */
5900 /* Do clear IP_DF ? */
5901 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
5904 mtu -= chk->send_size;
5905 r_mtu -= chk->send_size;
5906 data_list[bundle_at++] = chk;
5907 if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
5915 if ((r_mtu <= 0) || one_chunk) {
5921 * Must be sent in order of the TSN's
5925 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5926 kprintf("ok no more chk:%d > mtu:%d || < r_mtu:%d\n",
5927 chk->send_size, mtu, r_mtu);
5934 } /* if asoc.state OPEN */
5935 /* Is there something to send for this destination? */
5937 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5938 kprintf("ok now is chain assembled? %p\n",
5944 /* We may need to start a control timer or two */
5946 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
5950 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
5953 /* must start a send timer if data is being sent */
5954 if (bundle_at && (!callout_pending(&net->rxt_timer.timer))) {
5955 /* no timer running on this destination
5959 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5960 kprintf("ok lets start a send timer .. we will transmit %p\n",
5964 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
5966 /* Now send it, if there is anything to send :> */
5967 if ((outchain->m_flags & M_PKTHDR) == 0) {
5970 MGETHDR(t, MB_DONTWAIT, MT_HEADER);
5972 sctp_m_freem(outchain);
5975 t->m_next = outchain;
5976 t->m_pkthdr.len = 0;
5977 t->m_pkthdr.rcvif = 0;
5982 outchain->m_pkthdr.len += t->m_len;
5986 if (outchain->m_len == 0) {
5987 /* Special case for when you get a 0 len
5988 * mbuf at the head due to the lack
5989 * of a MHDR at the beginning.
5991 MH_ALIGN(outchain, sizeof(struct sctphdr));
5992 outchain->m_len = sizeof(struct sctphdr);
5994 M_PREPEND(outchain, sizeof(struct sctphdr), MB_DONTWAIT);
5995 if (outchain == NULL) {
6001 shdr = mtod(outchain, struct sctphdr *);
6002 shdr->src_port = inp->sctp_lport;
6003 shdr->dest_port = stcb->rport;
6004 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
6006 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
6007 (struct sockaddr *)&net->ro._l_addr,
6009 no_fragmentflg, bundle_at, data_list[0], asconf))) {
6010 /* error, we could not output */
6011 if (error == ENOBUFS) {
6012 asoc->ifp_had_enobuf = 1;
6014 sctp_pegs[SCTP_DATA_OUT_ERR]++;
6015 if (from_where == 0) {
6016 sctp_pegs[SCTP_ERROUT_FRM_USR]++;
6021 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
6022 kprintf("Gak send error %d\n", error);
6027 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
6028 kprintf("Update HB time anyway\n");
6031 if (*now_filled == 0) {
6032 SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
6034 *now = net->last_sent_time;
6036 net->last_sent_time = *now;
6040 if (error == EHOSTUNREACH) {
6042 * Destination went unreachable during
6046 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
6047 kprintf("Calling the movement routine\n");
6050 sctp_move_to_an_alt(stcb, asoc, net);
6052 sctp_clean_up_ctl (asoc);
6055 asoc->ifp_had_enobuf = 0;
6057 if (bundle_at || hbflag) {
6058 /* For data/asconf and hb set time */
6059 if (*now_filled == 0) {
6060 SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
6062 *now = net->last_sent_time;
6064 net->last_sent_time = *now;
6069 *num_out += (ctl_cnt + bundle_at);
6072 if (!net->rto_pending) {
6073 /* setup for a RTO measurement */
6074 net->rto_pending = 1;
6075 data_list[0]->do_rtt = 1;
6077 data_list[0]->do_rtt = 0;
6079 sctp_pegs[SCTP_PEG_TSNS_SENT] += bundle_at;
6080 sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
6087 /* At the end there should be no NON timed
6088 * chunks hanging on this queue.
6090 if ((*num_out == 0) && (*reason_code == 0)) {
6093 sctp_clean_up_ctl (asoc);
6098 sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
6100 /* Prepend a OPERATIONAL_ERROR chunk header
6101 * and put on the end of the control chunk queue.
6103 /* Sender had better have gotten a MGETHDR or else
6104 * the control chunk will be forever skipped
6106 struct sctp_chunkhdr *hdr;
6107 struct sctp_tmit_chunk *chk;
6110 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
6113 sctp_m_freem(op_err);
6116 sctppcbinfo.ipi_count_chunk++;
6117 sctppcbinfo.ipi_gencnt_chunk++;
6118 M_PREPEND(op_err, sizeof(struct sctp_chunkhdr), MB_DONTWAIT);
6119 if (op_err == NULL) {
6120 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
6121 sctppcbinfo.ipi_count_chunk--;
6122 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
6123 panic("Chunk count is negative");
6125 sctppcbinfo.ipi_gencnt_chunk++;
6130 while (mat != NULL) {
6131 chk->send_size += mat->m_len;
6134 chk->rec.chunk_id = SCTP_OPERATION_ERROR;
6135 chk->sent = SCTP_DATAGRAM_UNSENT;
6138 chk->asoc = &stcb->asoc;
6140 chk->whoTo = chk->asoc->primary_destination;
6141 chk->whoTo->ref_count++;
6142 hdr = mtod(op_err, struct sctp_chunkhdr *);
6143 hdr->chunk_type = SCTP_OPERATION_ERROR;
6144 hdr->chunk_flags = 0;
6145 hdr->chunk_length = htons(chk->send_size);
6146 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue,
6149 chk->asoc->ctrl_queue_cnt++;
6153 sctp_send_cookie_echo(struct mbuf *m,
6155 struct sctp_tcb *stcb,
6156 struct sctp_nets *net)
6159 * pull out the cookie and put it at the front of the control
6163 struct mbuf *cookie, *mat;
6164 struct sctp_paramhdr parm, *phdr;
6165 struct sctp_chunkhdr *hdr;
6166 struct sctp_tmit_chunk *chk;
6167 uint16_t ptype, plen;
6168 /* First find the cookie in the param area */
6170 at = offset + sizeof(struct sctp_init_chunk);
6173 phdr = sctp_get_next_param(m, at, &parm, sizeof(parm));
6177 ptype = ntohs(phdr->param_type);
6178 plen = ntohs(phdr->param_length);
6179 if (ptype == SCTP_STATE_COOKIE) {
6181 /* found the cookie */
6182 if ((pad = (plen % 4))) {
6185 cookie = sctp_m_copym(m, at, plen, MB_DONTWAIT);
6186 if (cookie == NULL) {
6192 at += SCTP_SIZE32(plen);
6194 if (cookie == NULL) {
6195 /* Did not find the cookie */
6198 /* ok, we got the cookie lets change it into a cookie echo chunk */
6200 /* first the change from param to cookie */
6201 hdr = mtod(cookie, struct sctp_chunkhdr *);
6202 hdr->chunk_type = SCTP_COOKIE_ECHO;
6203 hdr->chunk_flags = 0;
6204 /* now we MUST have a PKTHDR on it */
6205 if ((cookie->m_flags & M_PKTHDR) != M_PKTHDR) {
6206 /* we hope this happens rarely */
6207 MGETHDR(mat, MB_DONTWAIT, MT_HEADER);
6209 sctp_m_freem(cookie);
6213 mat->m_pkthdr.rcvif = 0;
6214 mat->m_next = cookie;
6217 cookie->m_pkthdr.len = plen;
6218 /* get the chunk stuff now and place it in the FRONT of the queue */
6219 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
6222 sctp_m_freem(cookie);
6225 sctppcbinfo.ipi_count_chunk++;
6226 sctppcbinfo.ipi_gencnt_chunk++;
6227 chk->send_size = cookie->m_pkthdr.len;
6228 chk->rec.chunk_id = SCTP_COOKIE_ECHO;
6229 chk->sent = SCTP_DATAGRAM_UNSENT;
6232 chk->asoc = &stcb->asoc;
6234 chk->whoTo = chk->asoc->primary_destination;
6235 chk->whoTo->ref_count++;
6236 TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
6237 chk->asoc->ctrl_queue_cnt++;
6242 sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
6246 struct sctp_nets *net)
6248 /* take a HB request and make it into a
6249 * HB ack and send it.
6251 struct mbuf *outchain;
6252 struct sctp_chunkhdr *chdr;
6253 struct sctp_tmit_chunk *chk;
6257 /* must have a net pointer */
6260 outchain = sctp_m_copym(m, offset, chk_length, MB_DONTWAIT);
6261 if (outchain == NULL) {
6262 /* gak out of memory */
6265 chdr = mtod(outchain, struct sctp_chunkhdr *);
6266 chdr->chunk_type = SCTP_HEARTBEAT_ACK;
6267 chdr->chunk_flags = 0;
6268 if ((outchain->m_flags & M_PKTHDR) != M_PKTHDR) {
6269 /* should not happen but we are cautious. */
6271 MGETHDR(tmp, MB_DONTWAIT, MT_HEADER);
6276 tmp->m_pkthdr.rcvif = 0;
6277 tmp->m_next = outchain;
6280 outchain->m_pkthdr.len = chk_length;
6281 if (chk_length % 4) {
6285 padlen = 4 - (outchain->m_pkthdr.len % 4);
6286 m_copyback(outchain, outchain->m_pkthdr.len, padlen, (caddr_t)&cpthis);
6288 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
6291 sctp_m_freem(outchain);
6294 sctppcbinfo.ipi_count_chunk++;
6295 sctppcbinfo.ipi_gencnt_chunk++;
6297 chk->send_size = chk_length;
6298 chk->rec.chunk_id = SCTP_HEARTBEAT_ACK;
6299 chk->sent = SCTP_DATAGRAM_UNSENT;
6302 chk->asoc = &stcb->asoc;
6303 chk->data = outchain;
6305 chk->whoTo->ref_count++;
6306 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
6307 chk->asoc->ctrl_queue_cnt++;
6311 sctp_send_cookie_ack(struct sctp_tcb *stcb) {
6312 /* formulate and queue a cookie-ack back to sender */
6313 struct mbuf *cookie_ack;
6314 struct sctp_chunkhdr *hdr;
6315 struct sctp_tmit_chunk *chk;
6318 MGETHDR(cookie_ack, MB_DONTWAIT, MT_HEADER);
6319 if (cookie_ack == NULL) {
6323 cookie_ack->m_data += SCTP_MIN_OVERHEAD;
6324 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
6327 sctp_m_freem(cookie_ack);
6330 sctppcbinfo.ipi_count_chunk++;
6331 sctppcbinfo.ipi_gencnt_chunk++;
6333 chk->send_size = sizeof(struct sctp_chunkhdr);
6334 chk->rec.chunk_id = SCTP_COOKIE_ACK;
6335 chk->sent = SCTP_DATAGRAM_UNSENT;
6338 chk->asoc = &stcb->asoc;
6339 chk->data = cookie_ack;
6340 if (chk->asoc->last_control_chunk_from != NULL) {
6341 chk->whoTo = chk->asoc->last_control_chunk_from;
6343 chk->whoTo = chk->asoc->primary_destination;
6345 chk->whoTo->ref_count++;
6346 hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
6347 hdr->chunk_type = SCTP_COOKIE_ACK;
6348 hdr->chunk_flags = 0;
6349 hdr->chunk_length = htons(chk->send_size);
6350 cookie_ack->m_pkthdr.len = cookie_ack->m_len = chk->send_size;
6351 cookie_ack->m_pkthdr.rcvif = 0;
6352 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
6353 chk->asoc->ctrl_queue_cnt++;
6359 sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
6361 /* formulate and queue a SHUTDOWN-ACK back to the sender */
6362 struct mbuf *m_shutdown_ack;
6363 struct sctp_shutdown_ack_chunk *ack_cp;
6364 struct sctp_tmit_chunk *chk;
6366 m_shutdown_ack = NULL;
6367 MGETHDR(m_shutdown_ack, MB_DONTWAIT, MT_HEADER);
6368 if (m_shutdown_ack == NULL) {
6372 m_shutdown_ack->m_data += SCTP_MIN_OVERHEAD;
6373 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
6376 sctp_m_freem(m_shutdown_ack);
6379 sctppcbinfo.ipi_count_chunk++;
6380 sctppcbinfo.ipi_gencnt_chunk++;
6382 chk->send_size = sizeof(struct sctp_chunkhdr);
6383 chk->rec.chunk_id = SCTP_SHUTDOWN_ACK;
6384 chk->sent = SCTP_DATAGRAM_UNSENT;
6387 chk->asoc = &stcb->asoc;
6388 chk->data = m_shutdown_ack;
6392 ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
6393 ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
6394 ack_cp->ch.chunk_flags = 0;
6395 ack_cp->ch.chunk_length = htons(chk->send_size);
6396 m_shutdown_ack->m_pkthdr.len = m_shutdown_ack->m_len = chk->send_size;
6397 m_shutdown_ack->m_pkthdr.rcvif = 0;
6398 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
6399 chk->asoc->ctrl_queue_cnt++;
6404 sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
6406 /* formulate and queue a SHUTDOWN to the sender */
6407 struct mbuf *m_shutdown;
6408 struct sctp_shutdown_chunk *shutdown_cp;
6409 struct sctp_tmit_chunk *chk;
6412 MGETHDR(m_shutdown, MB_DONTWAIT, MT_HEADER);
6413 if (m_shutdown == NULL) {
6417 m_shutdown->m_data += SCTP_MIN_OVERHEAD;
6418 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
6421 sctp_m_freem(m_shutdown);
6424 sctppcbinfo.ipi_count_chunk++;
6425 sctppcbinfo.ipi_gencnt_chunk++;
6427 chk->send_size = sizeof(struct sctp_shutdown_chunk);
6428 chk->rec.chunk_id = SCTP_SHUTDOWN;
6429 chk->sent = SCTP_DATAGRAM_UNSENT;
6432 chk->asoc = &stcb->asoc;
6433 chk->data = m_shutdown;
6437 shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
6438 shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
6439 shutdown_cp->ch.chunk_flags = 0;
6440 shutdown_cp->ch.chunk_length = htons(chk->send_size);
6441 shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
6442 m_shutdown->m_pkthdr.len = m_shutdown->m_len = chk->send_size;
6443 m_shutdown->m_pkthdr.rcvif = 0;
6444 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
6445 chk->asoc->ctrl_queue_cnt++;
6447 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6448 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
6449 stcb->sctp_ep->sctp_socket->so_snd.ssb_cc = 0;
6450 soisdisconnecting(stcb->sctp_ep->sctp_socket);
6456 sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net)
6459 * formulate and queue an ASCONF to the peer
6460 * ASCONF parameters should be queued on the assoc queue
6462 struct sctp_tmit_chunk *chk;
6463 struct mbuf *m_asconf;
6465 /* compose an ASCONF chunk, maximum length is PMTU */
6466 m_asconf = sctp_compose_asconf(stcb);
6467 if (m_asconf == NULL) {
6470 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
6473 sctp_m_freem(m_asconf);
6476 sctppcbinfo.ipi_count_chunk++;
6477 sctppcbinfo.ipi_gencnt_chunk++;
6479 chk->data = m_asconf;
6480 chk->send_size = m_asconf->m_pkthdr.len;
6481 chk->rec.chunk_id = SCTP_ASCONF;
6482 chk->sent = SCTP_DATAGRAM_UNSENT;
6485 chk->asoc = &stcb->asoc;
6486 chk->whoTo = chk->asoc->primary_destination;
6487 chk->whoTo->ref_count++;
6488 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
6489 chk->asoc->ctrl_queue_cnt++;
6494 sctp_send_asconf_ack(struct sctp_tcb *stcb, uint32_t retrans)
6497 * formulate and queue a asconf-ack back to sender
6498 * the asconf-ack must be stored in the tcb
6500 struct sctp_tmit_chunk *chk;
6503 /* is there a asconf-ack mbuf chain to send? */
6504 if (stcb->asoc.last_asconf_ack_sent == NULL) {
6508 /* copy the asconf_ack */
6509 #if defined(__FreeBSD__) || defined(__NetBSD__)
6510 /* Supposedly the m_copypacket is a optimzation,
6513 if (stcb->asoc.last_asconf_ack_sent->m_flags & M_PKTHDR) {
6514 m_ack = m_copypacket(stcb->asoc.last_asconf_ack_sent, MB_DONTWAIT);
6515 sctp_pegs[SCTP_CACHED_SRC]++;
6517 m_ack = m_copy(stcb->asoc.last_asconf_ack_sent, 0, M_COPYALL);
6519 m_ack = m_copy(stcb->asoc.last_asconf_ack_sent, 0, M_COPYALL);
6521 if (m_ack == NULL) {
6522 /* couldn't copy it */
6526 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
6530 sctp_m_freem(m_ack);
6533 sctppcbinfo.ipi_count_chunk++;
6534 sctppcbinfo.ipi_gencnt_chunk++;
6536 /* figure out where it goes to */
6538 /* we're doing a retransmission */
6539 if (stcb->asoc.used_alt_asconfack > 2) {
6540 /* tried alternate nets already, go back */
6543 /* need to try and alternate net */
6544 chk->whoTo = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from);
6545 stcb->asoc.used_alt_asconfack++;
6547 if (chk->whoTo == NULL) {
6549 if (stcb->asoc.last_control_chunk_from == NULL)
6550 chk->whoTo = stcb->asoc.primary_destination;
6552 chk->whoTo = stcb->asoc.last_control_chunk_from;
6553 stcb->asoc.used_alt_asconfack = 0;
6557 if (stcb->asoc.last_control_chunk_from == NULL)
6558 chk->whoTo = stcb->asoc.primary_destination;
6560 chk->whoTo = stcb->asoc.last_control_chunk_from;
6561 stcb->asoc.used_alt_asconfack = 0;
6564 chk->send_size = m_ack->m_pkthdr.len;
6565 chk->rec.chunk_id = SCTP_ASCONF_ACK;
6566 chk->sent = SCTP_DATAGRAM_UNSENT;
6569 chk->asoc = &stcb->asoc;
6570 chk->whoTo->ref_count++;
6571 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
6572 chk->asoc->ctrl_queue_cnt++;
6578 sctp_chunk_retransmission(struct sctp_inpcb *inp,
6579 struct sctp_tcb *stcb,
6580 struct sctp_association *asoc,
6581 int *cnt_out, struct timeval *now, int *now_filled)
6584 * send out one MTU of retransmission.
6585 * If fast_retransmit is happening we ignore the cwnd.
6586 * Otherwise we obey the cwnd and rwnd.
6587 * For a Cookie or Asconf in the control chunk queue we retransmit
6588 * them by themselves.
6590 * For data chunks we will pick out the lowest TSN's in the
6591 * sent_queue marked for resend and bundle them all together
6592 * (up to a MTU of destination). The address to send to should
6593 * have been selected/changed where the retransmission was
6594 * marked (i.e. in FR or t3-timeout routines).
6596 struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
6597 struct sctp_tmit_chunk *chk, *fwd;
6599 struct sctphdr *shdr;
6601 struct sctp_nets *net;
6602 int no_fragmentflg, bundle_at, cnt_thru;
6604 int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
6606 tmr_started = ctl_cnt = bundle_at = error = 0;
6613 #ifdef SCTP_AUDITING_ENABLED
6614 sctp_audit_log(0xC3, 1);
6616 if (TAILQ_EMPTY(&asoc->sent_queue)) {
6618 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
6619 kprintf("SCTP hits empty queue with cnt set to %d?\n",
6620 asoc->sent_queue_retran_cnt);
6623 asoc->sent_queue_cnt = 0;
6624 asoc->sent_queue_cnt_removeable = 0;
6626 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
6627 if (chk->sent != SCTP_DATAGRAM_RESEND) {
6628 /* we only worry about things marked for resend */
6631 if ((chk->rec.chunk_id == SCTP_COOKIE_ECHO) ||
6632 (chk->rec.chunk_id == SCTP_ASCONF) ||
6633 (chk->rec.chunk_id == SCTP_STREAM_RESET) ||
6634 (chk->rec.chunk_id == SCTP_FORWARD_CUM_TSN)) {
6635 if (chk->rec.chunk_id == SCTP_STREAM_RESET) {
6636 /* For stream reset we only retran the request
6639 struct sctp_stream_reset_req *strreq;
6640 strreq = mtod(chk->data, struct sctp_stream_reset_req *);
6641 if (strreq->sr_req.ph.param_type != ntohs(SCTP_STR_RESET_REQUEST)) {
6646 if (chk->rec.chunk_id == SCTP_ASCONF) {
6650 if (chk->rec.chunk_id == SCTP_FORWARD_CUM_TSN) {
6654 m = sctp_copy_mbufchain(chk->data, m);
6660 /* do we have control chunks to retransmit? */
6662 /* Start a timer no matter if we suceed or fail */
6663 if (chk->rec.chunk_id == SCTP_COOKIE_ECHO) {
6664 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
6665 } else if (chk->rec.chunk_id == SCTP_ASCONF)
6666 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
6668 if (m->m_len == 0) {
6669 /* Special case for when you get a 0 len
6670 * mbuf at the head due to the lack
6671 * of a MHDR at the beginning.
6673 m->m_len = sizeof(struct sctphdr);
6675 M_PREPEND(m, sizeof(struct sctphdr), MB_DONTWAIT);
6680 shdr = mtod(m, struct sctphdr *);
6681 shdr->src_port = inp->sctp_lport;
6682 shdr->dest_port = stcb->rport;
6683 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
6685 chk->snd_count++; /* update our count */
6687 if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
6688 (struct sockaddr *)&chk->whoTo->ro._l_addr, m,
6689 no_fragmentflg, 0, NULL, asconf))) {
6690 sctp_pegs[SCTP_DATA_OUT_ERR]++;
6694 *We don't want to mark the net->sent time here since this
6695 * we use this for HB and retrans cannot measure RTT
6697 /* SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time);*/
6699 chk->sent = SCTP_DATAGRAM_SENT;
6700 asoc->sent_queue_retran_cnt--;
6701 if (asoc->sent_queue_retran_cnt < 0) {
6702 asoc->sent_queue_retran_cnt = 0;
6707 /* Clean up the fwd-tsn list */
6708 sctp_clean_up_ctl (asoc);
6712 /* Ok, it is just data retransmission we need to do or
6713 * that and a fwd-tsn with it all.
6715 if (TAILQ_EMPTY(&asoc->sent_queue)) {
6719 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
6720 kprintf("Normal chunk retransmission cnt:%d\n",
6721 asoc->sent_queue_retran_cnt);
6724 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) ||
6725 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT)) {
6726 /* not yet open, resend the cookie and that is it */
6731 #ifdef SCTP_AUDITING_ENABLED
6732 sctp_auditing(20, inp, stcb, NULL);
6734 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
6735 if (chk->sent != SCTP_DATAGRAM_RESEND) {
6736 /* No, not sent to this net or not ready for rtx */
6740 /* pick up the net */
6742 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
6743 mtu = (net->mtu - SCTP_MIN_OVERHEAD);
6745 mtu = net->mtu- SCTP_MIN_V4_OVERHEAD;
6748 if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
6749 /* No room in peers rwnd */
6751 tsn = asoc->last_acked_seq + 1;
6752 if (tsn == chk->rec.data.TSN_seq) {
6753 /* we make a special exception for this case.
6754 * The peer has no rwnd but is missing the
6755 * lowest chunk.. which is probably what is
6756 * holding up the rwnd.
6758 goto one_chunk_around;
6761 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
6762 kprintf("blocked-peers_rwnd:%d tf:%d\n",
6763 (int)asoc->peers_rwnd,
6764 (int)asoc->total_flight);
6767 sctp_pegs[SCTP_RWND_BLOCKED]++;
6771 if (asoc->peers_rwnd < mtu) {
6774 #ifdef SCTP_AUDITING_ENABLED
6775 sctp_audit_log(0xC3, 2);
6779 net->fast_retran_ip = 0;
6780 if (chk->rec.data.doing_fast_retransmit == 0) {
6781 /* if no FR in progress skip destination that
6782 * have flight_size > cwnd.
6784 if (net->flight_size >= net->cwnd) {
6785 sctp_pegs[SCTP_CWND_BLOCKED]++;
6789 /* Mark the destination net to have FR recovery
6792 net->fast_retran_ip = 1;
6795 if ((chk->send_size <= mtu) || (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
6796 /* ok we will add this one */
6797 m = sctp_copy_mbufchain(chk->data, m);
6801 /* upate our MTU size */
6802 /* Do clear IP_DF ? */
6803 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
6806 mtu -= chk->send_size;
6807 data_list[bundle_at++] = chk;
6808 if (one_chunk && (asoc->total_flight <= 0)) {
6809 sctp_pegs[SCTP_WINDOW_PROBES]++;
6810 chk->rec.data.state_flags |= SCTP_WINDOW_PROBE;
6813 if (one_chunk == 0) {
6814 /* now are there anymore forward from chk to pick up?*/
6815 fwd = TAILQ_NEXT(chk, sctp_next);
6817 if (fwd->sent != SCTP_DATAGRAM_RESEND) {
6818 /* Nope, not for retran */
6819 fwd = TAILQ_NEXT(fwd, sctp_next);
6822 if (fwd->whoTo != net) {
6823 /* Nope, not the net in question */
6824 fwd = TAILQ_NEXT(fwd, sctp_next);
6827 if (fwd->send_size <= mtu) {
6828 m = sctp_copy_mbufchain(fwd->data, m);
6832 /* upate our MTU size */
6833 /* Do clear IP_DF ? */
6834 if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
6837 mtu -= fwd->send_size;
6838 data_list[bundle_at++] = fwd;
6839 if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
6842 fwd = TAILQ_NEXT(fwd, sctp_next);
6844 /* can't fit so we are done */
6849 /* Is there something to send for this destination? */
6851 /* No matter if we fail/or suceed we should
6852 * start a timer. A failure is like a lost
6855 if (!callout_pending(&net->rxt_timer.timer)) {
6856 /* no timer running on this destination
6859 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
6862 if (m->m_len == 0) {
6863 /* Special case for when you get a 0 len
6864 * mbuf at the head due to the lack
6865 * of a MHDR at the beginning.
6867 m->m_len = sizeof(struct sctphdr);
6869 M_PREPEND(m, sizeof(struct sctphdr), MB_DONTWAIT);
6874 shdr = mtod(m, struct sctphdr *);
6875 shdr->src_port = inp->sctp_lport;
6876 shdr->dest_port = stcb->rport;
6877 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
6880 /* Now lets send it, if there is anything to send :> */
6881 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
6882 (struct sockaddr *)&net->ro._l_addr,
6884 no_fragmentflg, 0, NULL, asconf))) {
6885 /* error, we could not output */
6886 sctp_pegs[SCTP_DATA_OUT_ERR]++;
6891 * We don't want to mark the net->sent time here since
6892 * this we use this for HB and retrans cannot measure
6895 /* SCTP_GETTIME_TIMEVAL(&net->last_sent_time);*/
6897 /* For auto-close */
6899 if (*now_filled == 0) {
6900 SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
6901 *now = asoc->time_last_sent;
6904 asoc->time_last_sent = *now;
6906 *cnt_out += bundle_at;
6907 #ifdef SCTP_AUDITING_ENABLED
6908 sctp_audit_log(0xC4, bundle_at);
6910 for (i = 0; i < bundle_at; i++) {
6911 sctp_pegs[SCTP_RETRANTSN_SENT]++;
6912 data_list[i]->sent = SCTP_DATAGRAM_SENT;
6913 data_list[i]->snd_count++;
6914 asoc->sent_queue_retran_cnt--;
6915 /* record the time */
6916 data_list[i]->sent_rcv_time = asoc->time_last_sent;
6917 if (asoc->sent_queue_retran_cnt < 0) {
6918 asoc->sent_queue_retran_cnt = 0;
6920 net->flight_size += data_list[i]->book_size;
6921 asoc->total_flight += data_list[i]->book_size;
6922 asoc->total_flight_count++;
6924 #ifdef SCTP_LOG_RWND
6925 sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
6926 asoc->peers_rwnd , data_list[i]->send_size, sctp_peer_chunk_oh);
6928 asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
6929 (u_int32_t)(data_list[i]->send_size + sctp_peer_chunk_oh));
6930 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
6931 /* SWS sender side engages */
6932 asoc->peers_rwnd = 0;
6936 (data_list[i]->rec.data.doing_fast_retransmit)) {
6937 sctp_pegs[SCTP_FAST_RETRAN]++;
6938 if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
6939 (tmr_started == 0)) {
6941 * ok we just fast-retrans'd
6942 * the lowest TSN, i.e the
6943 * first on the list. In this
6944 * case we want to give some
6945 * more time to get a SACK
6946 * back without a t3-expiring.
6948 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
6949 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
6953 #ifdef SCTP_AUDITING_ENABLED
6954 sctp_auditing(21, inp, stcb, NULL);
6960 if (asoc->sent_queue_retran_cnt <= 0) {
6961 /* all done we have no more to retran */
6962 asoc->sent_queue_retran_cnt = 0;
6966 /* No more room in rwnd */
6969 /* stop the for loop here. we sent out a packet */
6977 sctp_timer_validation(struct sctp_inpcb *inp,
6978 struct sctp_tcb *stcb,
6979 struct sctp_association *asoc,
6982 struct sctp_nets *net;
6983 /* Validate that a timer is running somewhere */
6984 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
6985 if (callout_pending(&net->rxt_timer.timer)) {
6986 /* Here is a timer */
6990 /* Gak, we did not have a timer somewhere */
6992 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
6993 kprintf("Deadlock avoided starting timer on a dest at retran\n");
6996 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
7001 sctp_chunk_output(struct sctp_inpcb *inp,
7002 struct sctp_tcb *stcb,
7005 /* Ok this is the generic chunk service queue.
7006 * we must do the following:
7007 * - See if there are retransmits pending, if so we
7008 * must do these first and return.
7009 * - Service the stream queue that is next,
7010 * moving any message (note I must get a complete
7011 * message i.e. FIRST/MIDDLE and LAST to the out
7012 * queue in one pass) and assigning TSN's
7013 * - Check to see if the cwnd/rwnd allows any output, if
7014 * so we go ahead and fomulate and send the low level
7015 * chunks. Making sure to combine any control in the
7016 * control chunk queue also.
7018 struct sctp_association *asoc;
7019 struct sctp_nets *net;
7020 int error, num_out, tot_out, ret, reason_code, burst_cnt, burst_limit;
7028 sctp_pegs[SCTP_CALLS_TO_CO]++;
7030 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
7031 kprintf("in co - retran count:%d\n", asoc->sent_queue_retran_cnt);
7034 while (asoc->sent_queue_retran_cnt) {
7035 /* Ok, it is retransmission time only, we send out only ONE
7036 * packet with a single call off to the retran code.
7038 ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled);
7040 /* Can't send anymore */
7042 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7043 kprintf("retransmission ret:%d -- full\n", ret);
7047 * now lets push out control by calling med-level
7048 * output once. this assures that we WILL send HB's
7051 sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
7052 &cwnd_full, from_where,
7055 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7056 kprintf("Control send outputs:%d@full\n", num_out);
7059 #ifdef SCTP_AUDITING_ENABLED
7060 sctp_auditing(8, inp, stcb, NULL);
7062 return (sctp_timer_validation(inp, stcb, asoc, ret));
7066 * The count was off.. retran is not happening so do
7067 * the normal retransmission.
7070 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7071 kprintf("Done with retrans, none left fill up window\n");
7074 #ifdef SCTP_AUDITING_ENABLED
7075 sctp_auditing(9, inp, stcb, NULL);
7079 if (from_where == 1) {
7080 /* Only one transmission allowed out of a timeout */
7082 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7083 kprintf("Only one packet allowed out\n");
7086 #ifdef SCTP_AUDITING_ENABLED
7087 sctp_auditing(10, inp, stcb, NULL);
7089 /* Push out any control */
7090 sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, &cwnd_full, from_where,
7094 if ((num_out == 0) && (ret == 0)) {
7095 /* No more retrans to send */
7099 #ifdef SCTP_AUDITING_ENABLED
7100 sctp_auditing(12, inp, stcb, NULL);
7102 /* Check for bad destinations, if they exist move chunks around. */
7103 burst_limit = asoc->max_burst;
7104 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7105 if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) ==
7106 SCTP_ADDR_NOT_REACHABLE) {
7108 * if possible move things off of this address
7109 * we still may send below due to the dormant state
7110 * but we try to find an alternate address to send
7111 * to and if we have one we move all queued data on
7112 * the out wheel to this alternate address.
7114 sctp_move_to_an_alt(stcb, asoc, net);
7117 if ((asoc->sat_network) || (net->addr_is_local)) {
7118 burst_limit = asoc->max_burst * SCTP_SAT_NETWORK_BURST_INCR;
7122 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
7123 kprintf("examined net:%p burst limit:%d\n", net, asoc->max_burst);
7127 #ifdef SCTP_USE_ALLMAN_BURST
7128 if ((net->flight_size+(burst_limit*net->mtu)) < net->cwnd) {
7129 if (net->ssthresh < net->cwnd)
7130 net->ssthresh = net->cwnd;
7131 net->cwnd = (net->flight_size+(burst_limit*net->mtu));
7132 #ifdef SCTP_LOG_MAXBURST
7133 sctp_log_maxburst(net, 0, burst_limit, SCTP_MAX_BURST_APPLIED);
7135 sctp_pegs[SCTP_MAX_BURST_APL]++;
7137 net->fast_retran_ip = 0;
7142 /* Fill up what we can to the destination */
7147 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
7148 kprintf("Burst count:%d - call m-c-o\n", burst_cnt);
7151 error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
7152 &reason_code, 0, &cwnd_full, from_where,
7156 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7157 kprintf("Error %d was returned from med-c-op\n", error);
7160 #ifdef SCTP_LOG_MAXBURST
7161 sctp_log_maxburst(asoc->primary_destination, error , burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
7166 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
7167 kprintf("m-c-o put out %d\n", num_out);
7173 #ifndef SCTP_USE_ALLMAN_BURST
7174 && (burst_cnt < burst_limit)
7177 #ifndef SCTP_USE_ALLMAN_BURST
7178 if (burst_cnt >= burst_limit) {
7179 sctp_pegs[SCTP_MAX_BURST_APL]++;
7180 asoc->burst_limit_applied = 1;
7181 #ifdef SCTP_LOG_MAXBURST
7182 sctp_log_maxburst(asoc->primary_destination, 0 , burst_cnt, SCTP_MAX_BURST_APPLIED);
7185 asoc->burst_limit_applied = 0;
7190 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7191 kprintf("Ok, we have put out %d chunks\n", tot_out);
7195 sctp_pegs[SCTP_CO_NODATASNT]++;
7196 if (asoc->stream_queue_cnt > 0) {
7197 sctp_pegs[SCTP_SOS_NOSNT]++;
7199 sctp_pegs[SCTP_NOS_NOSNT]++;
7201 if (asoc->send_queue_cnt > 0) {
7202 sctp_pegs[SCTP_SOSE_NOSNT]++;
7204 sctp_pegs[SCTP_NOSE_NOSNT]++;
7207 /* Now we need to clean up the control chunk chain if
7208 * a ECNE is on it. It must be marked as UNSENT again
7209 * so next call will continue to send it until
7210 * such time that we get a CWR, to remove it.
7212 sctp_fix_ecn_echo(asoc);
7218 sctp_output(struct sctp_inpcb *inp, struct mbuf *m, struct sockaddr *addr,
7219 struct mbuf *control, struct thread *p, int flags)
7221 struct sctp_inpcb *t_inp;
7222 struct sctp_tcb *stcb;
7223 struct sctp_nets *net;
7224 struct sctp_association *asoc;
7225 int create_lock_applied = 0;
7226 int queue_only, error = 0;
7227 struct sctp_sndrcvinfo srcv;
7229 int use_rcvinfo = 0;
7231 /* struct route ro;*/
7240 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7241 kprintf("USR Send BEGINS\n");
7245 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
7246 (inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING)) {
7247 /* The listner can NOT send */
7249 sctppcbinfo.mbuf_track--;
7250 sctp_m_freem(control);
7257 /* Can't allow a V6 address on a non-v6 socket */
7259 SCTP_ASOC_CREATE_LOCK(inp);
7260 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
7261 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
7262 /* Should I really unlock ? */
7263 SCTP_ASOC_CREATE_UNLOCK(inp);
7265 sctppcbinfo.mbuf_track--;
7266 sctp_m_freem(control);
7273 create_lock_applied = 1;
7274 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
7275 (addr->sa_family == AF_INET6)) {
7276 SCTP_ASOC_CREATE_UNLOCK(inp);
7278 sctppcbinfo.mbuf_track--;
7279 sctp_m_freem(control);
7288 sctppcbinfo.mbuf_track++;
7289 if (sctp_find_cmsg(SCTP_SNDRCV, &srcv, control,
7291 if (srcv.sinfo_flags & MSG_SENDALL) {
7293 sctppcbinfo.mbuf_track--;
7294 sctp_m_freem(control);
7296 if (create_lock_applied) {
7297 SCTP_ASOC_CREATE_UNLOCK(inp);
7298 create_lock_applied = 0;
7300 return (sctp_sendall(inp, NULL, m, &srcv));
7302 if (srcv.sinfo_assoc_id) {
7303 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
7304 SCTP_INP_RLOCK(inp);
7305 stcb = LIST_FIRST(&inp->sctp_asoc_list);
7307 SCTP_TCB_LOCK(stcb);
7308 SCTP_INP_RUNLOCK(inp);
7311 if (create_lock_applied) {
7312 SCTP_ASOC_CREATE_UNLOCK(inp);
7313 create_lock_applied = 0;
7315 sctppcbinfo.mbuf_track--;
7316 sctp_m_freem(control);
7321 net = stcb->asoc.primary_destination;
7323 stcb = sctp_findassociation_ep_asocid(inp, srcv.sinfo_assoc_id);
7326 * Question: Should I error here if the
7328 * assoc_id is no longer valid?
7329 * i.e. I can't find it?
7333 /* Must locate the net structure */
7335 net = sctp_findnet(stcb, addr);
7338 net = stcb->asoc.primary_destination;
7344 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
7345 SCTP_INP_RLOCK(inp);
7346 stcb = LIST_FIRST(&inp->sctp_asoc_list);
7348 SCTP_TCB_LOCK(stcb);
7349 SCTP_INP_RUNLOCK(inp);
7352 if (create_lock_applied) {
7353 SCTP_ASOC_CREATE_UNLOCK(inp);
7354 create_lock_applied = 0;
7357 sctppcbinfo.mbuf_track--;
7358 sctp_m_freem(control);
7365 net = stcb->asoc.primary_destination;
7367 net = sctp_findnet(stcb, addr);
7369 net = stcb->asoc.primary_destination;
7374 SCTP_INP_WLOCK(inp);
7375 SCTP_INP_INCR_REF(inp);
7376 SCTP_INP_WUNLOCK(inp);
7377 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
7379 SCTP_INP_WLOCK(inp);
7380 SCTP_INP_DECR_REF(inp);
7381 SCTP_INP_WUNLOCK(inp);
7386 if ((stcb == NULL) &&
7387 (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) {
7389 sctppcbinfo.mbuf_track--;
7390 sctp_m_freem(control);
7393 if (create_lock_applied) {
7394 SCTP_ASOC_CREATE_UNLOCK(inp);
7395 create_lock_applied = 0;
7400 } else if ((stcb == NULL) &&
7403 sctppcbinfo.mbuf_track--;
7404 sctp_m_freem(control);
7407 if (create_lock_applied) {
7408 SCTP_ASOC_CREATE_UNLOCK(inp);
7409 create_lock_applied = 0;
7414 } else if (stcb == NULL) {
7415 /* UDP mode, we must go ahead and start the INIT process */
7416 if ((use_rcvinfo) && (srcv.sinfo_flags & MSG_ABORT)) {
7417 /* Strange user to do this */
7419 sctppcbinfo.mbuf_track--;
7420 sctp_m_freem(control);
7423 if (create_lock_applied) {
7424 SCTP_ASOC_CREATE_UNLOCK(inp);
7425 create_lock_applied = 0;
7431 stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0);
7434 sctppcbinfo.mbuf_track--;
7435 sctp_m_freem(control);
7438 if (create_lock_applied) {
7439 SCTP_ASOC_CREATE_UNLOCK(inp);
7440 create_lock_applied = 0;
7446 if (create_lock_applied) {
7447 SCTP_ASOC_CREATE_UNLOCK(inp);
7448 create_lock_applied = 0;
7450 kprintf("Huh-1, create lock should have been applied!\n");
7454 asoc->state = SCTP_STATE_COOKIE_WAIT;
7455 SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
7457 /* see if a init structure exists in cmsg headers */
7458 struct sctp_initmsg initm;
7460 if (sctp_find_cmsg(SCTP_INIT, &initm, control,
7462 /* we have an INIT override of the default */
7463 if (initm.sinit_max_attempts)
7464 asoc->max_init_times = initm.sinit_max_attempts;
7465 if (initm.sinit_num_ostreams)
7466 asoc->pre_open_streams = initm.sinit_num_ostreams;
7467 if (initm.sinit_max_instreams)
7468 asoc->max_inbound_streams = initm.sinit_max_instreams;
7469 if (initm.sinit_max_init_timeo)
7470 asoc->initial_init_rto_max = initm.sinit_max_init_timeo;
7472 if (asoc->streamoutcnt < asoc->pre_open_streams) {
7473 /* Default is NOT correct */
7475 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7476 kprintf("Ok, defout:%d pre_open:%d\n",
7477 asoc->streamoutcnt, asoc->pre_open_streams);
7480 kfree(asoc->strmout, M_PCB);
7481 asoc->strmout = NULL;
7482 asoc->streamoutcnt = asoc->pre_open_streams;
7483 asoc->strmout = kmalloc(asoc->streamoutcnt * sizeof(struct sctp_stream_out),
7485 for (i = 0; i < asoc->streamoutcnt; i++) {
7487 * inbound side must be set to 0xffff,
7488 * also NOTE when we get the INIT-ACK
7489 * back (for INIT sender) we MUST
7490 * reduce the count (streamoutcnt) but
7491 * first check if we sent to any of the
7492 * upper streams that were dropped (if
7493 * some were). Those that were dropped
7494 * must be notified to the upper layer
7495 * as failed to send.
7497 asoc->strmout[i].next_sequence_sent = 0x0;
7498 TAILQ_INIT(&asoc->strmout[i].outqueue);
7499 asoc->strmout[i].stream_no = i;
7500 asoc->strmout[i].next_spoke.tqe_next = 0;
7501 asoc->strmout[i].next_spoke.tqe_prev = 0;
7505 sctp_send_initiate(inp, stcb);
7507 * we may want to dig in after this call and adjust the MTU
7508 * value. It defaulted to 1500 (constant) but the ro structure
7509 * may now have an update and thus we may need to change it
7510 * BEFORE we append the message.
7512 net = stcb->asoc.primary_destination;
7514 if (create_lock_applied) {
7515 SCTP_ASOC_CREATE_UNLOCK(inp);
7516 create_lock_applied = 0;
7519 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
7520 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
7523 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
7524 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
7525 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
7526 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
7528 sctppcbinfo.mbuf_track--;
7529 sctp_m_freem(control);
7532 if ((use_rcvinfo) &&
7533 (srcv.sinfo_flags & MSG_ABORT)) {
7534 sctp_msg_append(stcb, net, m, &srcv, flags);
7542 SCTP_TCB_UNLOCK(stcb);
7546 if (create_lock_applied) {
7547 /* we should never hit here with the create lock applied
7550 SCTP_ASOC_CREATE_UNLOCK(inp);
7551 create_lock_applied = 0;
7555 if (use_rcvinfo == 0) {
7556 srcv = stcb->asoc.def_send;
7560 if (sctp_debug_on & SCTP_DEBUG_OUTPUT5) {
7561 kprintf("stream:%d\n", srcv.sinfo_stream);
7562 kprintf("flags:%x\n", (u_int)srcv.sinfo_flags);
7563 kprintf("ppid:%d\n", srcv.sinfo_ppid);
7564 kprintf("context:%d\n", srcv.sinfo_context);
7569 sctppcbinfo.mbuf_track--;
7570 sctp_m_freem(control);
7573 if (net && ((srcv.sinfo_flags & MSG_ADDR_OVER))) {
7574 /* we take the override or the unconfirmed */
7577 net = stcb->asoc.primary_destination;
7579 if ((error = sctp_msg_append(stcb, net, m, &srcv, flags))) {
7580 SCTP_TCB_UNLOCK(stcb);
7584 if (net->flight_size > net->cwnd) {
7585 sctp_pegs[SCTP_SENDTO_FULL_CWND]++;
7587 } else if (asoc->ifp_had_enobuf) {
7588 sctp_pegs[SCTP_QUEONLY_BURSTLMT]++;
7591 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
7592 ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) * sizeof(struct sctp_data_chunk)) +
7595 if (((inp->sctp_flags & SCTP_PCB_FLAGS_NODELAY) == 0) &&
7596 (stcb->asoc.total_flight > 0) &&
7597 (un_sent < (int)stcb->asoc.smallest_mtu)
7600 /* Ok, Nagle is set on and we have
7601 * data outstanding. Don't send anything
7602 * and let the SACK drive out the data.
7604 sctp_pegs[SCTP_NAGLE_NOQ]++;
7607 sctp_pegs[SCTP_NAGLE_OFF]++;
7610 if ((queue_only == 0) && stcb->asoc.peers_rwnd) {
7611 /* we can attempt to send too.*/
7613 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7614 kprintf("USR Send calls sctp_chunk_output\n");
7617 #ifdef SCTP_AUDITING_ENABLED
7618 sctp_audit_log(0xC0, 1);
7619 sctp_auditing(6, inp, stcb, net);
7621 sctp_pegs[SCTP_OUTPUT_FRM_SND]++;
7622 sctp_chunk_output(inp, stcb, 0);
7623 #ifdef SCTP_AUDITING_ENABLED
7624 sctp_audit_log(0xC0, 2);
7625 sctp_auditing(7, inp, stcb, net);
7630 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7631 kprintf("USR Send complete qo:%d prw:%d\n", queue_only, stcb->asoc.peers_rwnd);
7634 SCTP_TCB_UNLOCK(stcb);
7640 send_forward_tsn(struct sctp_tcb *stcb,
7641 struct sctp_association *asoc)
7643 struct sctp_tmit_chunk *chk;
7644 struct sctp_forward_tsn_chunk *fwdtsn;
7646 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7647 if (chk->rec.chunk_id == SCTP_FORWARD_CUM_TSN) {
7648 /* mark it to unsent */
7649 chk->sent = SCTP_DATAGRAM_UNSENT;
7651 /* Do we correct its output location? */
7652 if (chk->whoTo != asoc->primary_destination) {
7653 sctp_free_remote_addr(chk->whoTo);
7654 chk->whoTo = asoc->primary_destination;
7655 chk->whoTo->ref_count++;
7657 goto sctp_fill_in_rest;
7660 /* Ok if we reach here we must build one */
7661 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
7665 sctppcbinfo.ipi_count_chunk++;
7666 sctppcbinfo.ipi_gencnt_chunk++;
7667 chk->rec.chunk_id = SCTP_FORWARD_CUM_TSN;
7669 MGETHDR(chk->data, MB_DONTWAIT, MT_DATA);
7670 if (chk->data == NULL) {
7671 chk->whoTo->ref_count--;
7672 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
7673 sctppcbinfo.ipi_count_chunk--;
7674 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
7675 panic("Chunk count is negative");
7677 sctppcbinfo.ipi_gencnt_chunk++;
7680 chk->data->m_data += SCTP_MIN_OVERHEAD;
7681 chk->sent = SCTP_DATAGRAM_UNSENT;
7683 chk->whoTo = asoc->primary_destination;
7684 chk->whoTo->ref_count++;
7685 TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
7686 asoc->ctrl_queue_cnt++;
7688 /* Here we go through and fill out the part that
7689 * deals with stream/seq of the ones we skip.
7691 chk->data->m_pkthdr.len = chk->data->m_len = 0;
7693 struct sctp_tmit_chunk *at, *tp1, *last;
7694 struct sctp_strseq *strseq;
7695 unsigned int cnt_of_space, i, ovh;
7696 unsigned int space_needed;
7697 unsigned int cnt_of_skipped = 0;
7698 TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
7699 if (at->sent != SCTP_FORWARD_TSN_SKIP) {
7700 /* no more to look at */
7703 if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
7704 /* We don't report these */
7709 space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
7710 (cnt_of_skipped * sizeof(struct sctp_strseq)));
7711 if ((M_TRAILINGSPACE(chk->data) < (int)space_needed) &&
7712 ((chk->data->m_flags & M_EXT) == 0)) {
7713 /* Need a M_EXT, get one and move
7714 * fwdtsn to data area.
7716 MCLGET(chk->data, MB_DONTWAIT);
7718 cnt_of_space = M_TRAILINGSPACE(chk->data);
7720 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7721 ovh = SCTP_MIN_OVERHEAD;
7723 ovh = SCTP_MIN_V4_OVERHEAD;
7725 if (cnt_of_space > (asoc->smallest_mtu-ovh)) {
7726 /* trim to a mtu size */
7727 cnt_of_space = asoc->smallest_mtu - ovh;
7729 if (cnt_of_space < space_needed) {
7730 /* ok we must trim down the chunk by lowering
7731 * the advance peer ack point.
7733 cnt_of_skipped = (cnt_of_space-
7734 ((sizeof(struct sctp_forward_tsn_chunk))/
7735 sizeof(struct sctp_strseq)));
7736 /* Go through and find the TSN that
7737 * will be the one we report.
7739 at = TAILQ_FIRST(&asoc->sent_queue);
7740 for (i = 0; i < cnt_of_skipped; i++) {
7741 tp1 = TAILQ_NEXT(at, sctp_next);
7745 /* last now points to last one I can report, update peer ack point */
7746 asoc->advanced_peer_ack_point = last->rec.data.TSN_seq;
7747 space_needed -= (cnt_of_skipped * sizeof(struct sctp_strseq));
7749 chk->send_size = space_needed;
7750 /* Setup the chunk */
7751 fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
7752 fwdtsn->ch.chunk_length = htons(chk->send_size);
7753 fwdtsn->ch.chunk_flags = 0;
7754 fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
7755 fwdtsn->new_cumulative_tsn = htonl(asoc->advanced_peer_ack_point);
7756 chk->send_size = (sizeof(struct sctp_forward_tsn_chunk) +
7757 (cnt_of_skipped * sizeof(struct sctp_strseq)));
7758 chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
7760 /* Move pointer to after the fwdtsn and transfer to
7761 * the strseq pointer.
7763 strseq = (struct sctp_strseq *)fwdtsn;
7765 * Now populate the strseq list. This is done blindly
7766 * without pulling out duplicate stream info. This is
7767 * inefficent but won't harm the process since the peer
7768 * will look at these in sequence and will thus release
7769 * anything. It could mean we exceed the PMTU and chop
7770 * off some that we could have included.. but this is
7771 * unlikely (aka 1432/4 would mean 300+ stream seq's would
7772 * have to be reported in one FWD-TSN. With a bit of work
7773 * we can later FIX this to optimize and pull out duplcates..
7774 * but it does add more overhead. So for now... not!
7776 at = TAILQ_FIRST(&asoc->sent_queue);
7777 for (i = 0; i < cnt_of_skipped; i++) {
7778 tp1 = TAILQ_NEXT(at, sctp_next);
7779 if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
7780 /* We don't report these */
7785 strseq->stream = ntohs(at->rec.data.stream_number);
7786 strseq->sequence = ntohs(at->rec.data.stream_seq);
7796 sctp_send_sack(struct sctp_tcb *stcb)
7799 * Queue up a SACK in the control queue. We must first check to
7800 * see if a SACK is somehow on the control queue. If so, we will
7801 * take and and remove the old one.
7803 struct sctp_association *asoc;
7804 struct sctp_tmit_chunk *chk, *a_chk;
7805 struct sctp_sack_chunk *sack;
7806 struct sctp_gap_ack_block *gap_descriptor;
7809 unsigned int i, maxi, seeing_ones, m_size;
7810 unsigned int num_gap_blocks, space;
7816 if (asoc->last_data_chunk_from == NULL) {
7817 /* Hmm we never received anything */
7820 sctp_set_rwnd(stcb, asoc);
7821 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7822 if (chk->rec.chunk_id == SCTP_SELECTIVE_ACK) {
7823 /* Hmm, found a sack already on queue, remove it */
7824 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
7825 asoc->ctrl_queue_cnt++;
7828 sctp_m_freem(a_chk->data);
7830 sctp_free_remote_addr(a_chk->whoTo);
7831 a_chk->whoTo = NULL;
7835 if (a_chk == NULL) {
7836 a_chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
7837 if (a_chk == NULL) {
7838 /* No memory so we drop the idea, and set a timer */
7839 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
7840 stcb->sctp_ep, stcb, NULL);
7841 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
7842 stcb->sctp_ep, stcb, NULL);
7845 sctppcbinfo.ipi_count_chunk++;
7846 sctppcbinfo.ipi_gencnt_chunk++;
7847 a_chk->rec.chunk_id = SCTP_SELECTIVE_ACK;
7850 a_chk->snd_count = 0;
7851 a_chk->send_size = 0; /* fill in later */
7852 a_chk->sent = SCTP_DATAGRAM_UNSENT;
7853 m_size = (asoc->mapping_array_size << 3);
7855 if ((asoc->numduptsns) ||
7856 (asoc->last_data_chunk_from->dest_state & SCTP_ADDR_NOT_REACHABLE)
7858 /* Ok, we have some duplicates or the destination for the
7859 * sack is unreachable, lets see if we can select an alternate
7860 * than asoc->last_data_chunk_from
7862 if ((!(asoc->last_data_chunk_from->dest_state &
7863 SCTP_ADDR_NOT_REACHABLE)) &&
7864 (asoc->used_alt_onsack > 2)) {
7865 /* We used an alt last time, don't this time */
7866 a_chk->whoTo = NULL;
7868 asoc->used_alt_onsack++;
7869 a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from);
7871 if (a_chk->whoTo == NULL) {
7872 /* Nope, no alternate */
7873 a_chk->whoTo = asoc->last_data_chunk_from;
7874 asoc->used_alt_onsack = 0;
7877 /* No duplicates so we use the last
7878 * place we received data from.
7881 if (asoc->last_data_chunk_from == NULL) {
7882 kprintf("Huh, last_data_chunk_from is null when we want to sack??\n");
7885 asoc->used_alt_onsack = 0;
7886 a_chk->whoTo = asoc->last_data_chunk_from;
7889 a_chk->whoTo->ref_count++;
7891 /* Ok now lets formulate a MBUF with our sack */
7892 MGETHDR(a_chk->data, MB_DONTWAIT, MT_DATA);
7893 if ((a_chk->data == NULL) ||
7894 (a_chk->whoTo == NULL)) {
7895 /* rats, no mbuf memory */
7897 /* was a problem with the destination */
7898 sctp_m_freem(a_chk->data);
7901 a_chk->whoTo->ref_count--;
7902 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, a_chk);
7903 sctppcbinfo.ipi_count_chunk--;
7904 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
7905 panic("Chunk count is negative");
7907 sctppcbinfo.ipi_gencnt_chunk++;
7908 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
7909 stcb->sctp_ep, stcb, NULL);
7910 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
7911 stcb->sctp_ep, stcb, NULL);
7914 /* First count the number of gap ack blocks we need */
7915 if (asoc->highest_tsn_inside_map == asoc->cumulative_tsn) {
7916 /* We know if there are none above the cum-ack we
7917 * have everything with NO gaps
7921 /* Ok we must count how many gaps we
7925 if (asoc->highest_tsn_inside_map >= asoc->mapping_array_base_tsn) {
7926 maxi = (asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn);
7928 maxi = (asoc->highest_tsn_inside_map + (MAX_TSN - asoc->mapping_array_base_tsn) + 1);
7930 if (maxi > m_size) {
7931 /* impossible but who knows, someone is playing with us :> */
7933 kprintf("GAK maxi:%d > m_size:%d came out higher than allowed htsn:%u base:%u cumack:%u\n",
7936 asoc->highest_tsn_inside_map,
7937 asoc->mapping_array_base_tsn,
7938 asoc->cumulative_tsn
7944 if (asoc->cumulative_tsn >= asoc->mapping_array_base_tsn) {
7945 start = (asoc->cumulative_tsn - asoc->mapping_array_base_tsn);
7947 /* Set it so we start at 0 */
7950 /* Ok move start up one to look at the NEXT past the cum-ack */
7952 for (i = start; i <= maxi; i++) {
7954 /* while seeing ones I must
7955 * transition back to 0 before
7956 * finding the next gap and
7957 * counting the segment.
7959 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, i) == 0) {
7963 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, i)) {
7970 if (num_gap_blocks == 0) {
7972 * Traveled all of the bits and NO one,
7975 if (compare_with_wrap(asoc->cumulative_tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
7976 asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
7977 #ifdef SCTP_MAP_LOGGING
7978 sctp_log_map(0, 4, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
7984 /* Now calculate the space needed */
7985 space = (sizeof(struct sctp_sack_chunk) +
7986 (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
7987 (asoc->numduptsns * sizeof(int32_t))
7989 if (space > (asoc->smallest_mtu-SCTP_MAX_OVERHEAD)) {
7990 /* Reduce the size of the sack to fit */
7992 calc = (asoc->smallest_mtu - SCTP_MAX_OVERHEAD);
7993 calc -= sizeof(struct sctp_gap_ack_block);
7994 fit = calc/sizeof(struct sctp_gap_ack_block);
7995 if (fit > (int)num_gap_blocks) {
7996 /* discard some dups */
7997 asoc->numduptsns = (fit - num_gap_blocks);
7999 /* discard all dups and some gaps */
8000 num_gap_blocks = fit;
8001 asoc->numduptsns = 0;
8004 space = (sizeof(struct sctp_sack_chunk) +
8005 (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
8006 (asoc->numduptsns * sizeof(int32_t))
8011 if ((space+SCTP_MIN_OVERHEAD) > MHLEN) {
8012 /* We need a cluster */
8013 MCLGET(a_chk->data, MB_DONTWAIT);
8014 if ((a_chk->data->m_flags & M_EXT) != M_EXT) {
8015 /* can't get a cluster
8016 * give up and try later.
8019 sctp_m_freem(a_chk->data);
8021 a_chk->whoTo->ref_count--;
8022 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, a_chk);
8023 sctppcbinfo.ipi_count_chunk--;
8024 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
8025 panic("Chunk count is negative");
8027 sctppcbinfo.ipi_gencnt_chunk++;
8028 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
8029 stcb->sctp_ep, stcb, NULL);
8030 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
8031 stcb->sctp_ep, stcb, NULL);
8036 /* ok, lets go through and fill it in */
8037 a_chk->data->m_data += SCTP_MIN_OVERHEAD;
8038 sack = mtod(a_chk->data, struct sctp_sack_chunk *);
8039 sack->ch.chunk_type = SCTP_SELECTIVE_ACK;
8040 sack->ch.chunk_flags = asoc->receiver_nonce_sum & SCTP_SACK_NONCE_SUM;
8041 sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
8042 sack->sack.a_rwnd = htonl(asoc->my_rwnd);
8043 asoc->my_last_reported_rwnd = asoc->my_rwnd;
8044 sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
8045 sack->sack.num_dup_tsns = htons(asoc->numduptsns);
8047 a_chk->send_size = (sizeof(struct sctp_sack_chunk) +
8048 (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
8049 (asoc->numduptsns * sizeof(int32_t)));
8050 a_chk->data->m_pkthdr.len = a_chk->data->m_len = a_chk->send_size;
8051 sack->ch.chunk_length = htons(a_chk->send_size);
8053 gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)sack + sizeof(struct sctp_sack_chunk));
8055 for (i = start; i <= maxi; i++) {
8056 if (num_gap_blocks == 0) {
8060 /* while seeing Ones I must
8061 * transition back to 0 before
8062 * finding the next gap
8064 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, i) == 0) {
8065 gap_descriptor->end = htons(((uint16_t)(i-start)));
8071 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, i)) {
8072 gap_descriptor->start = htons(((uint16_t)(i+1-start)));
8073 /* advance struct to next pointer */
8078 if (num_gap_blocks) {
8079 /* special case where the array is all 1's
8080 * to the end of the array.
8082 gap_descriptor->end = htons(((uint16_t)((i-start))));
8085 /* now we must add any dups we are going to report. */
8086 if (asoc->numduptsns) {
8087 dup = (uint32_t *)gap_descriptor;
8088 for (i = 0; i < asoc->numduptsns; i++) {
8089 *dup = htonl(asoc->dup_tsns[i]);
8092 asoc->numduptsns = 0;
8094 /* now that the chunk is prepared queue it to the control
8097 TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
8098 asoc->ctrl_queue_cnt++;
8099 sctp_pegs[SCTP_PEG_SACKS_SENT]++;
8104 sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr)
8106 struct mbuf *m_abort;
8107 struct sctp_abort_msg *abort_m;
8110 MGETHDR(m_abort, MB_DONTWAIT, MT_HEADER);
8111 if (m_abort == NULL) {
8115 m_abort->m_data += SCTP_MIN_OVERHEAD;
8116 abort_m = mtod(m_abort, struct sctp_abort_msg *);
8117 m_abort->m_len = sizeof(struct sctp_abort_msg);
8118 m_abort->m_next = operr;
8128 abort_m->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
8129 abort_m->msg.ch.chunk_flags = 0;
8130 abort_m->msg.ch.chunk_length = htons(sizeof(struct sctp_abort_chunk) +
8132 abort_m->sh.src_port = stcb->sctp_ep->sctp_lport;
8133 abort_m->sh.dest_port = stcb->rport;
8134 abort_m->sh.v_tag = htonl(stcb->asoc.peer_vtag);
8135 abort_m->sh.checksum = 0;
8136 m_abort->m_pkthdr.len = m_abort->m_len + sz;
8137 m_abort->m_pkthdr.rcvif = 0;
8138 sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb,
8139 stcb->asoc.primary_destination,
8140 (struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr,
8141 m_abort, 1, 0, NULL, 0);
8145 sctp_send_shutdown_complete(struct sctp_tcb *stcb,
8146 struct sctp_nets *net)
8149 /* formulate and SEND a SHUTDOWN-COMPLETE */
8150 struct mbuf *m_shutdown_comp;
8151 struct sctp_shutdown_complete_msg *comp_cp;
8153 m_shutdown_comp = NULL;
8154 MGETHDR(m_shutdown_comp, MB_DONTWAIT, MT_HEADER);
8155 if (m_shutdown_comp == NULL) {
8159 m_shutdown_comp->m_data += sizeof(struct ip6_hdr);
8160 comp_cp = mtod(m_shutdown_comp, struct sctp_shutdown_complete_msg *);
8161 comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
8162 comp_cp->shut_cmp.ch.chunk_flags = 0;
8163 comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
8164 comp_cp->sh.src_port = stcb->sctp_ep->sctp_lport;
8165 comp_cp->sh.dest_port = stcb->rport;
8166 comp_cp->sh.v_tag = htonl(stcb->asoc.peer_vtag);
8167 comp_cp->sh.checksum = 0;
8169 m_shutdown_comp->m_pkthdr.len = m_shutdown_comp->m_len = sizeof(struct sctp_shutdown_complete_msg);
8170 m_shutdown_comp->m_pkthdr.rcvif = 0;
8171 sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
8172 (struct sockaddr *)&net->ro._l_addr, m_shutdown_comp,
8174 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
8175 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
8176 stcb->sctp_ep->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
8177 stcb->sctp_ep->sctp_socket->so_snd.ssb_cc = 0;
8178 soisdisconnected(stcb->sctp_ep->sctp_socket);
8184 sctp_send_shutdown_complete2(struct mbuf *m, int iphlen, struct sctphdr *sh)
8186 /* formulate and SEND a SHUTDOWN-COMPLETE */
8188 struct ip *iph, *iph_out;
8189 struct ip6_hdr *ip6, *ip6_out;
8191 struct sctp_shutdown_complete_msg *comp_cp;
8193 MGETHDR(mout, MB_DONTWAIT, MT_HEADER);
8198 iph = mtod(m, struct ip *);
8202 if (iph->ip_v == IPVERSION) {
8203 mout->m_len = sizeof(struct ip) +
8204 sizeof(struct sctp_shutdown_complete_msg);
8205 mout->m_next = NULL;
8206 iph_out = mtod(mout, struct ip *);
8208 /* Fill in the IP header for the ABORT */
8209 iph_out->ip_v = IPVERSION;
8210 iph_out->ip_hl = (sizeof(struct ip)/4);
8211 iph_out->ip_tos = (u_char)0;
8213 iph_out->ip_off = 0;
8214 iph_out->ip_ttl = MAXTTL;
8215 iph_out->ip_p = IPPROTO_SCTP;
8216 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
8217 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
8219 /* let IP layer calculate this */
8220 iph_out->ip_sum = 0;
8221 offset_out += sizeof(*iph_out);
8222 comp_cp = (struct sctp_shutdown_complete_msg *)(
8223 (caddr_t)iph_out + offset_out);
8224 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
8225 ip6 = (struct ip6_hdr *)iph;
8226 mout->m_len = sizeof(struct ip6_hdr) +
8227 sizeof(struct sctp_shutdown_complete_msg);
8228 mout->m_next = NULL;
8229 ip6_out = mtod(mout, struct ip6_hdr *);
8231 /* Fill in the IPv6 header for the ABORT */
8232 ip6_out->ip6_flow = ip6->ip6_flow;
8233 ip6_out->ip6_hlim = ip6_defhlim;
8234 ip6_out->ip6_nxt = IPPROTO_SCTP;
8235 ip6_out->ip6_src = ip6->ip6_dst;
8236 ip6_out->ip6_dst = ip6->ip6_src;
8237 ip6_out->ip6_plen = mout->m_len;
8238 offset_out += sizeof(*ip6_out);
8239 comp_cp = (struct sctp_shutdown_complete_msg *)(
8240 (caddr_t)ip6_out + offset_out);
8242 /* Currently not supported. */
8246 /* Now copy in and fill in the ABORT tags etc. */
8247 comp_cp->sh.src_port = sh->dest_port;
8248 comp_cp->sh.dest_port = sh->src_port;
8249 comp_cp->sh.checksum = 0;
8250 comp_cp->sh.v_tag = sh->v_tag;
8251 comp_cp->shut_cmp.ch.chunk_flags = SCTP_HAD_NO_TCB;
8252 comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
8253 comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
8255 mout->m_pkthdr.len = mout->m_len;
8257 if ((sctp_no_csum_on_loopback) &&
8258 (m->m_pkthdr.rcvif) &&
8259 (m->m_pkthdr.rcvif->if_type == IFT_LOOP)) {
8260 comp_cp->sh.checksum = 0;
8262 comp_cp->sh.checksum = sctp_calculate_sum(mout, NULL, offset_out);
8265 /* zap the rcvif, it should be null */
8266 mout->m_pkthdr.rcvif = 0;
8267 /* zap the stack pointer to the route */
8268 if (iph_out != NULL) {
8271 bzero(&ro, sizeof ro);
8273 if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
8274 kprintf("sctp_shutdown_complete2 calling ip_output:\n");
8275 sctp_print_address_pkt(iph_out, &comp_cp->sh);
8278 /* set IPv4 length */
8279 #if defined(__FreeBSD__)
8280 iph_out->ip_len = mout->m_pkthdr.len;
8282 iph_out->ip_len = htons(mout->m_pkthdr.len);
8285 ip_output(mout, 0, &ro, IP_RAWOUTPUT, NULL
8286 #if defined(__OpenBSD__) || (defined(__FreeBSD__) && __FreeBSD_version >= 480000) \
8287 || defined(__NetBSD__) || defined(__DragonFly__)
8291 /* Free the route if we got one back */
8294 } else if (ip6_out != NULL) {
8295 #ifdef NEW_STRUCT_ROUTE
8298 struct route_in6 ro;
8301 bzero(&ro, sizeof(ro));
8303 if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
8304 kprintf("sctp_shutdown_complete2 calling ip6_output:\n");
8305 sctp_print_address_pkt((struct ip *)ip6_out,
8309 ip6_output(mout, NULL, &ro, 0, NULL, NULL
8310 #if defined(__NetBSD__)
8313 #if (defined(__FreeBSD__) && __FreeBSD_version >= 480000) || defined(__DragonFly__)
8317 /* Free the route if we got one back */
8321 sctp_pegs[SCTP_DATAGRAMS_SENT]++;
8325 static struct sctp_nets *
8326 sctp_select_hb_destination(struct sctp_tcb *stcb, struct timeval *now)
8328 struct sctp_nets *net, *hnet;
8329 int ms_goneby, highest_ms, state_overide=0;
8331 SCTP_GETTIME_TIMEVAL(now);
8334 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
8336 ((net->dest_state & SCTP_ADDR_NOHB) && ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) ||
8337 (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)
8339 /* Skip this guy from consideration if HB is off AND its confirmed*/
8341 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
8342 kprintf("Skipping net:%p state:%d nohb/out-of-scope\n",
8343 net, net->dest_state);
8348 if (sctp_destination_is_reachable(stcb, (struct sockaddr *)&net->ro._l_addr) == 0) {
8349 /* skip this dest net from consideration */
8351 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
8352 kprintf("Skipping net:%p reachable NOT\n",
8358 if (net->last_sent_time.tv_sec) {
8359 /* Sent to so we subtract */
8360 ms_goneby = (now->tv_sec - net->last_sent_time.tv_sec) * 1000;
8362 /* Never been sent to */
8363 ms_goneby = 0x7fffffff;
8365 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
8366 kprintf("net:%p ms_goneby:%d\n",
8370 /* When the address state is unconfirmed but still considered reachable, we
8371 * HB at a higher rate. Once it goes confirmed OR reaches the "unreachable"
8372 * state, thenw we cut it back to HB at a more normal pace.
8374 if ((net->dest_state & (SCTP_ADDR_UNCONFIRMED|SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED) {
8380 if ((((unsigned int)ms_goneby >= net->RTO) || (state_overide)) &&
8381 (ms_goneby > highest_ms)) {
8382 highest_ms = ms_goneby;
8385 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
8386 kprintf("net:%p is the new high\n",
8393 ((hnet->dest_state & (SCTP_ADDR_UNCONFIRMED|SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED)) {
8399 if (highest_ms && (((unsigned int)highest_ms >= hnet->RTO) || state_overide)) {
8400 /* Found the one with longest delay bounds
8401 * OR it is unconfirmed and still not marked
8405 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
8406 kprintf("net:%p is the hb winner -",
8409 sctp_print_address((struct sockaddr *)&hnet->ro._l_addr);
8414 /* update the timer now */
8415 hnet->last_sent_time = *now;
8423 sctp_send_hb(struct sctp_tcb *stcb, int user_req, struct sctp_nets *u_net)
8425 struct sctp_tmit_chunk *chk;
8426 struct sctp_nets *net;
8427 struct sctp_heartbeat_chunk *hb;
8429 struct sockaddr_in *sin;
8430 struct sockaddr_in6 *sin6;
8432 if (user_req == 0) {
8433 net = sctp_select_hb_destination(stcb, &now);
8435 /* All our busy none to send to, just
8436 * start the timer again.
8438 if (stcb->asoc.state == 0) {
8441 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT,
8447 #ifndef SCTP_USE_ALLMAN_BURST
8449 /* found one idle.. decay cwnd on this one
8450 * by 1/2 if none outstanding.
8453 if (net->flight_size == 0) {
8455 if (net->addr_is_local) {
8456 if (net->cwnd < (net->mtu *4)) {
8457 net->cwnd = net->mtu * 4;
8460 if (net->cwnd < (net->mtu * 2)) {
8461 net->cwnd = net->mtu * 2;
8474 SCTP_GETTIME_TIMEVAL(&now);
8476 sin = (struct sockaddr_in *)&net->ro._l_addr;
8477 if (sin->sin_family != AF_INET) {
8478 if (sin->sin_family != AF_INET6) {
8483 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
8486 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
8487 kprintf("Gak, can't get a chunk for hb\n");
8492 sctppcbinfo.ipi_gencnt_chunk++;
8493 sctppcbinfo.ipi_count_chunk++;
8494 chk->rec.chunk_id = SCTP_HEARTBEAT_REQUEST;
8495 chk->asoc = &stcb->asoc;
8496 chk->send_size = sizeof(struct sctp_heartbeat_chunk);
8497 MGETHDR(chk->data, MB_DONTWAIT, MT_DATA);
8498 if (chk->data == NULL) {
8499 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
8500 sctppcbinfo.ipi_count_chunk--;
8501 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
8502 panic("Chunk count is negative");
8504 sctppcbinfo.ipi_gencnt_chunk++;
8507 chk->data->m_data += SCTP_MIN_OVERHEAD;
8508 chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
8509 chk->sent = SCTP_DATAGRAM_UNSENT;
8512 chk->whoTo->ref_count++;
8513 /* Now we have a mbuf that we can fill in with the details */
8514 hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
8516 /* fill out chunk header */
8517 hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
8518 hb->ch.chunk_flags = 0;
8519 hb->ch.chunk_length = htons(chk->send_size);
8520 /* Fill out hb parameter */
8521 hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
8522 hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
8523 hb->heartbeat.hb_info.time_value_1 = now.tv_sec;
8524 hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
8525 /* Did our user request this one, put it in */
8526 hb->heartbeat.hb_info.user_req = user_req;
8527 hb->heartbeat.hb_info.addr_family = sin->sin_family;
8528 hb->heartbeat.hb_info.addr_len = sin->sin_len;
8529 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
8530 /* we only take from the entropy pool if the address is
8533 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
8534 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
8536 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
8537 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
8539 if (sin->sin_family == AF_INET) {
8540 memcpy(hb->heartbeat.hb_info.address, &sin->sin_addr, sizeof(sin->sin_addr));
8541 } else if (sin->sin_family == AF_INET6) {
8542 /* We leave the scope the way it is in our lookup table. */
8543 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
8544 memcpy(hb->heartbeat.hb_info.address, &sin6->sin6_addr, sizeof(sin6->sin6_addr));
8546 /* huh compiler bug */
8548 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
8549 kprintf("Compiler bug bleeds a mbuf and a chunk\n");
8554 /* ok we have a destination that needs a beat */
8555 /* lets do the theshold management Qiaobing style */
8556 if (user_req == 0) {
8557 if (sctp_threshold_management(stcb->sctp_ep, stcb, net,
8558 stcb->asoc.max_send_times)) {
8559 /* we have lost the association, in a way this
8560 * is quite bad since we really are one less time
8561 * since we really did not send yet. This is the
8562 * down side to the Q's style as defined in the RFC
8563 * and not my alternate style defined in the RFC.
8565 if (chk->data != NULL) {
8566 sctp_m_freem(chk->data);
8569 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
8570 sctppcbinfo.ipi_count_chunk--;
8571 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
8572 panic("Chunk count is negative");
8574 sctppcbinfo.ipi_gencnt_chunk++;
8578 net->hb_responded = 0;
8580 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
8581 kprintf("Inserting chunk for HB\n");
8584 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
8585 stcb->asoc.ctrl_queue_cnt++;
8586 sctp_pegs[SCTP_HB_SENT]++;
8588 * Call directly med level routine to put out the chunk. It will
8589 * always tumble out control chunks aka HB but it may even tumble
8592 if (user_req == 0) {
8593 /* Ok now lets start the HB timer if it is NOT a user req */
8594 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
8601 sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
8604 struct sctp_association *asoc;
8605 struct sctp_ecne_chunk *ecne;
8606 struct sctp_tmit_chunk *chk;
8608 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
8609 if (chk->rec.chunk_id == SCTP_ECN_ECHO) {
8610 /* found a previous ECN_ECHO update it if needed */
8611 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
8612 ecne->tsn = htonl(high_tsn);
8616 /* nope could not find one to update so we must build one */
8617 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
8621 sctp_pegs[SCTP_ECNE_SENT]++;
8622 sctppcbinfo.ipi_count_chunk++;
8623 sctppcbinfo.ipi_gencnt_chunk++;
8624 chk->rec.chunk_id = SCTP_ECN_ECHO;
8625 chk->asoc = &stcb->asoc;
8626 chk->send_size = sizeof(struct sctp_ecne_chunk);
8627 MGETHDR(chk->data, MB_DONTWAIT, MT_DATA);
8628 if (chk->data == NULL) {
8629 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
8630 sctppcbinfo.ipi_count_chunk--;
8631 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
8632 panic("Chunk count is negative");
8634 sctppcbinfo.ipi_gencnt_chunk++;
8637 chk->data->m_data += SCTP_MIN_OVERHEAD;
8638 chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
8639 chk->sent = SCTP_DATAGRAM_UNSENT;
8642 chk->whoTo->ref_count++;
8643 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
8644 ecne->ch.chunk_type = SCTP_ECN_ECHO;
8645 ecne->ch.chunk_flags = 0;
8646 ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
8647 ecne->tsn = htonl(high_tsn);
8648 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
8649 asoc->ctrl_queue_cnt++;
8653 sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
8654 struct mbuf *m, int iphlen, int bad_crc)
8656 struct sctp_association *asoc;
8657 struct sctp_pktdrop_chunk *drp;
8658 struct sctp_tmit_chunk *chk;
8661 unsigned int small_one;
8666 if (asoc->peer_supports_pktdrop == 0) {
8667 /* peer must declare support before I
8672 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
8676 sctppcbinfo.ipi_count_chunk++;
8677 sctppcbinfo.ipi_gencnt_chunk++;
8679 iph = mtod(m, struct ip *);
8683 if (iph->ip_v == IPVERSION) {
8685 #if defined(__FreeBSD__)
8686 len = chk->send_size = iph->ip_len;
8688 len = chk->send_size = (iph->ip_len - iphlen);
8691 struct ip6_hdr *ip6h;
8693 ip6h = mtod(m, struct ip6_hdr *);
8694 len = chk->send_size = htons(ip6h->ip6_plen);
8696 if ((len+iphlen) > m->m_pkthdr.len) {
8698 chk->send_size = len = m->m_pkthdr.len - iphlen;
8700 chk->asoc = &stcb->asoc;
8701 MGETHDR(chk->data, MB_DONTWAIT, MT_DATA);
8702 if (chk->data == NULL) {
8704 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
8705 sctppcbinfo.ipi_count_chunk--;
8706 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
8707 panic("Chunk count is negative");
8709 sctppcbinfo.ipi_gencnt_chunk++;
8712 if ((chk->send_size+sizeof(struct sctp_pktdrop_chunk)+SCTP_MIN_OVERHEAD) > MHLEN) {
8713 MCLGET(chk->data, MB_DONTWAIT);
8714 if ((chk->data->m_flags & M_EXT) == 0) {
8716 sctp_m_freem(chk->data);
8721 chk->data->m_data += SCTP_MIN_OVERHEAD;
8722 drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
8724 sctp_m_freem(chk->data);
8728 small_one = asoc->smallest_mtu;
8729 if (small_one > MCLBYTES) {
8730 /* Only one cluster worth of data MAX */
8731 small_one = MCLBYTES;
8733 chk->book_size = (chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
8734 sizeof(struct sctphdr) + SCTP_MED_OVERHEAD);
8735 if (chk->book_size > small_one) {
8736 drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
8737 drp->trunc_len = htons(chk->send_size);
8738 chk->send_size = small_one - (SCTP_MED_OVERHEAD +
8739 sizeof(struct sctp_pktdrop_chunk) +
8740 sizeof(struct sctphdr));
8741 len = chk->send_size;
8743 /* no truncation needed */
8744 drp->ch.chunk_flags = 0;
8745 drp->trunc_len = htons(0);
8748 drp->ch.chunk_flags |= SCTP_BADCRC;
8750 chk->send_size += sizeof(struct sctp_pktdrop_chunk);
8751 chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
8752 chk->sent = SCTP_DATAGRAM_UNSENT;
8755 /* we should hit here */
8758 chk->whoTo = asoc->primary_destination;
8760 chk->whoTo->ref_count++;
8761 chk->rec.chunk_id = SCTP_PACKET_DROPPED;
8762 drp->ch.chunk_type = SCTP_PACKET_DROPPED;
8763 drp->ch.chunk_length = htons(chk->send_size);
8764 spc = stcb->sctp_socket->so_rcv.ssb_hiwat;
8768 drp->bottle_bw = htonl(spc);
8769 drp->current_onq = htonl(asoc->size_on_delivery_queue +
8770 asoc->size_on_reasm_queue +
8771 asoc->size_on_all_streams +
8772 asoc->my_rwnd_control_len +
8773 stcb->sctp_socket->so_rcv.ssb_cc);
8776 m_copydata(m, iphlen, len, datap);
8777 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
8778 asoc->ctrl_queue_cnt++;
8782 sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn)
8784 struct sctp_association *asoc;
8785 struct sctp_cwr_chunk *cwr;
8786 struct sctp_tmit_chunk *chk;
8789 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
8790 if (chk->rec.chunk_id == SCTP_ECN_CWR) {
8791 /* found a previous ECN_CWR update it if needed */
8792 cwr = mtod(chk->data, struct sctp_cwr_chunk *);
8793 if (compare_with_wrap(high_tsn, ntohl(cwr->tsn),
8795 cwr->tsn = htonl(high_tsn);
8800 /* nope could not find one to update so we must build one */
8801 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
8805 sctppcbinfo.ipi_count_chunk++;
8806 sctppcbinfo.ipi_gencnt_chunk++;
8807 chk->rec.chunk_id = SCTP_ECN_CWR;
8808 chk->asoc = &stcb->asoc;
8809 chk->send_size = sizeof(struct sctp_cwr_chunk);
8810 MGETHDR(chk->data, MB_DONTWAIT, MT_DATA);
8811 if (chk->data == NULL) {
8812 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
8813 sctppcbinfo.ipi_count_chunk--;
8814 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
8815 panic("Chunk count is negative");
8817 sctppcbinfo.ipi_gencnt_chunk++;
8820 chk->data->m_data += SCTP_MIN_OVERHEAD;
8821 chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
8822 chk->sent = SCTP_DATAGRAM_UNSENT;
8825 chk->whoTo->ref_count++;
8826 cwr = mtod(chk->data, struct sctp_cwr_chunk *);
8827 cwr->ch.chunk_type = SCTP_ECN_CWR;
8828 cwr->ch.chunk_flags = 0;
8829 cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
8830 cwr->tsn = htonl(high_tsn);
8831 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
8832 asoc->ctrl_queue_cnt++;
8835 sctp_reset_the_streams(struct sctp_tcb *stcb,
8836 struct sctp_stream_reset_request *req, int number_entries, uint16_t *list)
8840 if (req->reset_flags & SCTP_RESET_ALL) {
8841 for (i=0; i<stcb->asoc.streamoutcnt; i++) {
8842 stcb->asoc.strmout[i].next_sequence_sent = 0;
8844 } else if (number_entries) {
8845 for (i=0; i<number_entries; i++) {
8846 if (list[i] >= stcb->asoc.streamoutcnt) {
8847 /* no such stream */
8850 stcb->asoc.strmout[(list[i])].next_sequence_sent = 0;
8853 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list);
8857 sctp_send_str_reset_ack(struct sctp_tcb *stcb,
8858 struct sctp_stream_reset_request *req)
8860 struct sctp_association *asoc;
8861 struct sctp_stream_reset_resp *strack;
8862 struct sctp_tmit_chunk *chk;
8864 int number_entries, i;
8865 uint8_t two_way=0, not_peer=0;
8866 uint16_t *list=NULL;
8869 if (req->reset_flags & SCTP_RESET_ALL)
8872 number_entries = (ntohs(req->ph.param_length) - sizeof(struct sctp_stream_reset_request)) / sizeof(uint16_t);
8874 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
8878 sctppcbinfo.ipi_count_chunk++;
8879 sctppcbinfo.ipi_gencnt_chunk++;
8880 chk->rec.chunk_id = SCTP_STREAM_RESET;
8881 chk->asoc = &stcb->asoc;
8882 chk->send_size = sizeof(struct sctp_stream_reset_resp) + (number_entries * sizeof(uint16_t));
8883 MGETHDR(chk->data, MB_DONTWAIT, MT_DATA);
8884 if (chk->data == NULL) {
8886 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
8887 sctppcbinfo.ipi_count_chunk--;
8888 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
8889 panic("Chunk count is negative");
8891 sctppcbinfo.ipi_gencnt_chunk++;
8894 chk->data->m_data += SCTP_MIN_OVERHEAD;
8895 chk->data->m_pkthdr.len = chk->data->m_len = SCTP_SIZE32(chk->send_size);
8896 if (M_TRAILINGSPACE(chk->data) < (int)SCTP_SIZE32(chk->send_size)) {
8897 MCLGET(chk->data, MB_DONTWAIT);
8898 if ((chk->data->m_flags & M_EXT) == 0) {
8900 sctp_m_freem(chk->data);
8902 goto strresp_jump_out;
8904 chk->data->m_data += SCTP_MIN_OVERHEAD;
8906 if (M_TRAILINGSPACE(chk->data) < (int)SCTP_SIZE32(chk->send_size)) {
8907 /* can't do it, no room */
8909 sctp_m_freem(chk->data);
8911 goto strresp_jump_out;
8914 chk->sent = SCTP_DATAGRAM_UNSENT;
8916 chk->whoTo = asoc->primary_destination;
8917 chk->whoTo->ref_count++;
8918 strack = mtod(chk->data, struct sctp_stream_reset_resp *);
8920 strack->ch.chunk_type = SCTP_STREAM_RESET;
8921 strack->ch.chunk_flags = 0;
8922 strack->ch.chunk_length = htons(chk->send_size);
8924 memset(strack->sr_resp.reset_pad, 0, sizeof(strack->sr_resp.reset_pad));
8926 strack->sr_resp.ph.param_type = ntohs(SCTP_STR_RESET_RESPONSE);
8927 strack->sr_resp.ph.param_length = htons((chk->send_size - sizeof(struct sctp_chunkhdr)));
8931 if (chk->send_size % 4) {
8932 /* need a padding for the end */
8935 end = (uint8_t *)((caddr_t)strack + chk->send_size);
8936 pad = chk->send_size % 4;
8937 for (i = 0; i < pad; i++) {
8940 chk->send_size += pad;
8943 /* actual response */
8944 if (req->reset_flags & SCTP_RESET_YOUR) {
8945 strack->sr_resp.reset_flags = SCTP_RESET_PERFORMED;
8947 strack->sr_resp.reset_flags = 0;
8950 /* copied from reset request */
8951 strack->sr_resp.reset_req_seq_resp = req->reset_req_seq;
8952 seq = ntohl(req->reset_req_seq);
8954 list = req->list_of_streams;
8955 /* copy the un-converted network byte order streams */
8956 for (i=0; i<number_entries; i++) {
8957 strack->sr_resp.list_of_streams[i] = list[i];
8959 if (asoc->str_reset_seq_in == seq) {
8960 /* is it the next expected? */
8961 asoc->str_reset_seq_in++;
8962 strack->sr_resp.reset_at_tsn = htonl(asoc->sending_seq);
8963 asoc->str_reset_sending_seq = asoc->sending_seq;
8964 if (number_entries) {
8967 /* convert them to host byte order */
8968 for (i=0 ; i<number_entries; i++) {
8969 temp = ntohs(list[i]);
8973 if (req->reset_flags & SCTP_RESET_YOUR) {
8974 /* reset my outbound streams */
8975 sctp_reset_the_streams(stcb, req , number_entries, list);
8977 if (req->reset_flags & SCTP_RECIPRICAL) {
8978 /* reset peer too */
8979 sctp_send_str_reset_req(stcb, number_entries, list, two_way, not_peer);
8983 /* no its a retran so I must just ack and do nothing */
8984 strack->sr_resp.reset_at_tsn = htonl(asoc->str_reset_sending_seq);
8986 strack->sr_resp.cumulative_tsn = htonl(asoc->cumulative_tsn);
8987 TAILQ_INSERT_TAIL(&asoc->control_send_queue,
8990 asoc->ctrl_queue_cnt++;
8995 sctp_send_str_reset_req(struct sctp_tcb *stcb,
8996 int number_entrys, uint16_t *list, uint8_t two_way, uint8_t not_peer)
8998 /* Send a stream reset request. The number_entrys may be 0 and list NULL
8999 * if the request is to reset all streams. If two_way is true then we
9000 * not only request a RESET of the received streams but we also
9001 * request the peer to send a reset req to us too.
9002 * Flag combinations in table:
9004 * two_way | not_peer | = | Flags
9005 * ------------------------------
9006 * 0 | 0 | = | SCTP_RESET_YOUR (just the peer)
9007 * 1 | 0 | = | SCTP_RESET_YOUR | SCTP_RECIPRICAL (both sides)
9008 * 0 | 1 | = | Not a Valid Request (not anyone)
9009 * 1 | 1 | = | SCTP_RESET_RECIPRICAL (Just local host)
9011 struct sctp_association *asoc;
9012 struct sctp_stream_reset_req *strreq;
9013 struct sctp_tmit_chunk *chk;
9017 if (asoc->stream_reset_outstanding) {
9018 /* Already one pending, must get ACK back
9019 * to clear the flag.
9024 if ((two_way == 0) && (not_peer == 1)) {
9025 /* not a valid request */
9029 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
9033 sctppcbinfo.ipi_count_chunk++;
9034 sctppcbinfo.ipi_gencnt_chunk++;
9035 chk->rec.chunk_id = SCTP_STREAM_RESET;
9036 chk->asoc = &stcb->asoc;
9037 chk->send_size = sizeof(struct sctp_stream_reset_req) + (number_entrys * sizeof(uint16_t));
9038 MGETHDR(chk->data, MB_DONTWAIT, MT_DATA);
9039 if (chk->data == NULL) {
9041 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
9042 sctppcbinfo.ipi_count_chunk--;
9043 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
9044 panic("Chunk count is negative");
9046 sctppcbinfo.ipi_gencnt_chunk++;
9049 chk->data->m_data += SCTP_MIN_OVERHEAD;
9050 chk->data->m_pkthdr.len = chk->data->m_len = SCTP_SIZE32(chk->send_size);
9051 if (M_TRAILINGSPACE(chk->data) < (int)SCTP_SIZE32(chk->send_size)) {
9052 MCLGET(chk->data, MB_DONTWAIT);
9053 if ((chk->data->m_flags & M_EXT) == 0) {
9055 sctp_m_freem(chk->data);
9057 goto strreq_jump_out;
9059 chk->data->m_data += SCTP_MIN_OVERHEAD;
9061 if (M_TRAILINGSPACE(chk->data) < (int)SCTP_SIZE32(chk->send_size)) {
9062 /* can't do it, no room */
9064 sctp_m_freem(chk->data);
9066 goto strreq_jump_out;
9068 chk->sent = SCTP_DATAGRAM_UNSENT;
9070 chk->whoTo = asoc->primary_destination;
9071 chk->whoTo->ref_count++;
9073 strreq = mtod(chk->data, struct sctp_stream_reset_req *);
9074 strreq->ch.chunk_type = SCTP_STREAM_RESET;
9075 strreq->ch.chunk_flags = 0;
9076 strreq->ch.chunk_length = htons(chk->send_size);
9078 strreq->sr_req.ph.param_type = ntohs(SCTP_STR_RESET_REQUEST);
9079 strreq->sr_req.ph.param_length = htons((chk->send_size - sizeof(struct sctp_chunkhdr)));
9081 if (chk->send_size % 4) {
9082 /* need a padding for the end */
9085 end = (uint8_t *)((caddr_t)strreq + chk->send_size);
9086 pad = chk->send_size % 4;
9087 for (i=0; i<pad; i++) {
9090 chk->send_size += pad;
9093 strreq->sr_req.reset_flags = 0;
9094 if (number_entrys == 0) {
9095 strreq->sr_req.reset_flags |= SCTP_RESET_ALL;
9098 strreq->sr_req.reset_flags |= SCTP_RESET_YOUR;
9100 if (not_peer == 0) {
9101 strreq->sr_req.reset_flags |= SCTP_RECIPRICAL | SCTP_RESET_YOUR;
9103 strreq->sr_req.reset_flags |= SCTP_RECIPRICAL;
9106 memset(strreq->sr_req.reset_pad, 0, sizeof(strreq->sr_req.reset_pad));
9107 strreq->sr_req.reset_req_seq = htonl(asoc->str_reset_seq_out);
9108 if (number_entrys) {
9109 /* populate the specific entry's */
9111 for (i=0; i < number_entrys; i++) {
9112 strreq->sr_req.list_of_streams[i] = htons(list[i]);
9115 TAILQ_INSERT_TAIL(&asoc->control_send_queue,
9118 asoc->ctrl_queue_cnt++;
9119 sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
9120 asoc->stream_reset_outstanding = 1;
9124 sctp_send_abort(struct mbuf *m, int iphlen, struct sctphdr *sh, uint32_t vtag,
9125 struct mbuf *err_cause)
9128 * Formulate the abort message, and send it back down.
9131 struct sctp_abort_msg *abm;
9132 struct ip *iph, *iph_out;
9133 struct ip6_hdr *ip6, *ip6_out;
9136 /* don't respond to ABORT with ABORT */
9137 if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
9139 sctp_m_freem(err_cause);
9142 MGETHDR(mout, MB_DONTWAIT, MT_HEADER);
9145 sctp_m_freem(err_cause);
9148 iph = mtod(m, struct ip *);
9151 if (iph->ip_v == IPVERSION) {
9152 iph_out = mtod(mout, struct ip *);
9153 mout->m_len = sizeof(*iph_out) + sizeof(*abm);
9154 mout->m_next = err_cause;
9156 /* Fill in the IP header for the ABORT */
9157 iph_out->ip_v = IPVERSION;
9158 iph_out->ip_hl = (sizeof(struct ip) / 4);
9159 iph_out->ip_tos = (u_char)0;
9161 iph_out->ip_off = 0;
9162 iph_out->ip_ttl = MAXTTL;
9163 iph_out->ip_p = IPPROTO_SCTP;
9164 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
9165 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
9166 /* let IP layer calculate this */
9167 iph_out->ip_sum = 0;
9169 iphlen_out = sizeof(*iph_out);
9170 abm = (struct sctp_abort_msg *)((caddr_t)iph_out + iphlen_out);
9171 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
9172 ip6 = (struct ip6_hdr *)iph;
9173 ip6_out = mtod(mout, struct ip6_hdr *);
9174 mout->m_len = sizeof(*ip6_out) + sizeof(*abm);
9175 mout->m_next = err_cause;
9177 /* Fill in the IP6 header for the ABORT */
9178 ip6_out->ip6_flow = ip6->ip6_flow;
9179 ip6_out->ip6_hlim = ip6_defhlim;
9180 ip6_out->ip6_nxt = IPPROTO_SCTP;
9181 ip6_out->ip6_src = ip6->ip6_dst;
9182 ip6_out->ip6_dst = ip6->ip6_src;
9184 iphlen_out = sizeof(*ip6_out);
9185 abm = (struct sctp_abort_msg *)((caddr_t)ip6_out + iphlen_out);
9187 /* Currently not supported */
9191 abm->sh.src_port = sh->dest_port;
9192 abm->sh.dest_port = sh->src_port;
9193 abm->sh.checksum = 0;
9195 abm->sh.v_tag = sh->v_tag;
9196 abm->msg.ch.chunk_flags = SCTP_HAD_NO_TCB;
9198 abm->sh.v_tag = htonl(vtag);
9199 abm->msg.ch.chunk_flags = 0;
9201 abm->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
9204 struct mbuf *m_tmp = err_cause;
9206 /* get length of the err_cause chain */
9207 while (m_tmp != NULL) {
9208 err_len += m_tmp->m_len;
9209 m_tmp = m_tmp->m_next;
9211 mout->m_pkthdr.len = mout->m_len + err_len;
9213 /* need pad at end of chunk */
9216 padlen = 4 - (mout->m_pkthdr.len % 4);
9217 m_copyback(mout, mout->m_pkthdr.len, padlen, (caddr_t)&cpthis);
9219 abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch) + err_len);
9221 mout->m_pkthdr.len = mout->m_len;
9222 abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch));
9226 if ((sctp_no_csum_on_loopback) &&
9227 (m->m_pkthdr.rcvif) &&
9228 (m->m_pkthdr.rcvif->if_type == IFT_LOOP)) {
9229 abm->sh.checksum = 0;
9231 abm->sh.checksum = sctp_calculate_sum(mout, NULL, iphlen_out);
9234 /* zap the rcvif, it should be null */
9235 mout->m_pkthdr.rcvif = 0;
9236 if (iph_out != NULL) {
9239 /* zap the stack pointer to the route */
9240 bzero(&ro, sizeof ro);
9242 if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
9243 kprintf("sctp_send_abort calling ip_output:\n");
9244 sctp_print_address_pkt(iph_out, &abm->sh);
9247 /* set IPv4 length */
9248 #if defined(__FreeBSD__)
9249 iph_out->ip_len = mout->m_pkthdr.len;
9251 iph_out->ip_len = htons(mout->m_pkthdr.len);
9254 ip_output(mout, 0, &ro, IP_RAWOUTPUT, NULL
9255 #if defined(__OpenBSD__) || (defined(__FreeBSD__) && __FreeBSD_version >= 480000) \
9256 || defined(__NetBSD__) || defined(__DragonFly__)
9260 /* Free the route if we got one back */
9263 } else if (ip6_out != NULL) {
9264 #ifdef NEW_STRUCT_ROUTE
9267 struct route_in6 ro;
9270 /* zap the stack pointer to the route */
9271 bzero(&ro, sizeof(ro));
9273 if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
9274 kprintf("sctp_send_abort calling ip6_output:\n");
9275 sctp_print_address_pkt((struct ip *)ip6_out, &abm->sh);
9278 ip6_output(mout, NULL, &ro, 0, NULL, NULL
9279 #if defined(__NetBSD__)
9282 #if (defined(__FreeBSD__) && __FreeBSD_version >= 480000) || defined(__DragonFly__)
9286 /* Free the route if we got one back */
9290 sctp_pegs[SCTP_DATAGRAMS_SENT]++;
9294 sctp_send_operr_to(struct mbuf *m, int iphlen,
9298 struct sctphdr *ihdr;
9300 struct sctphdr *ohdr;
9301 struct sctp_chunkhdr *ophdr;
9305 struct sockaddr_in6 lsa6, fsa6;
9308 iph = mtod(m, struct ip *);
9309 ihdr = (struct sctphdr *)((caddr_t)iph + iphlen);
9310 if (!(scm->m_flags & M_PKTHDR)) {
9311 /* must be a pkthdr */
9312 kprintf("Huh, not a packet header in send_operr\n");
9316 M_PREPEND(scm, (sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr)), MB_DONTWAIT);
9318 /* can't send because we can't add a mbuf */
9321 ohdr = mtod(scm, struct sctphdr *);
9322 ohdr->src_port = ihdr->dest_port;
9323 ohdr->dest_port = ihdr->src_port;
9326 ophdr = (struct sctp_chunkhdr *)(ohdr + 1);
9327 ophdr->chunk_type = SCTP_OPERATION_ERROR;
9328 ophdr->chunk_flags = 0;
9329 ophdr->chunk_length = htons(scm->m_pkthdr.len - sizeof(struct sctphdr));
9330 if (scm->m_pkthdr.len % 4) {
9334 padlen = 4 - (scm->m_pkthdr.len % 4);
9335 m_copyback(scm, scm->m_pkthdr.len, padlen, (caddr_t)&cpthis);
9337 if ((sctp_no_csum_on_loopback) &&
9338 (m->m_pkthdr.rcvif) &&
9339 (m->m_pkthdr.rcvif->if_type == IFT_LOOP)) {
9342 val = sctp_calculate_sum(scm, NULL, 0);
9344 ohdr->checksum = val;
9345 if (iph->ip_v == IPVERSION) {
9349 M_PREPEND(scm, sizeof(struct ip), MB_DONTWAIT);
9352 bzero(&ro, sizeof ro);
9353 out = mtod(scm, struct ip *);
9354 out->ip_v = iph->ip_v;
9355 out->ip_hl = (sizeof(struct ip)/4);
9356 out->ip_tos = iph->ip_tos;
9357 out->ip_id = iph->ip_id;
9359 out->ip_ttl = MAXTTL;
9360 out->ip_p = IPPROTO_SCTP;
9362 out->ip_src = iph->ip_dst;
9363 out->ip_dst = iph->ip_src;
9364 #if defined(__FreeBSD__)
9365 out->ip_len = scm->m_pkthdr.len;
9367 out->ip_len = htons(scm->m_pkthdr.len);
9369 retcode = ip_output(scm, 0, &ro, IP_RAWOUTPUT, NULL
9370 #if defined(__OpenBSD__) || (defined(__FreeBSD__) && __FreeBSD_version >= 480000) \
9371 || defined(__NetBSD__) || defined(__DragonFly__)
9375 sctp_pegs[SCTP_DATAGRAMS_SENT]++;
9376 /* Free the route if we got one back */
9381 #ifdef NEW_STRUCT_ROUTE
9384 struct route_in6 ro;
9386 struct ip6_hdr *out6, *in6;
9388 M_PREPEND(scm, sizeof(struct ip6_hdr), MB_DONTWAIT);
9391 bzero(&ro, sizeof ro);
9392 in6 = mtod(m, struct ip6_hdr *);
9393 out6 = mtod(scm, struct ip6_hdr *);
9394 out6->ip6_flow = in6->ip6_flow;
9395 out6->ip6_hlim = ip6_defhlim;
9396 out6->ip6_nxt = IPPROTO_SCTP;
9397 out6->ip6_src = in6->ip6_dst;
9398 out6->ip6_dst = in6->ip6_src;
9401 bzero(&lsa6, sizeof(lsa6));
9402 lsa6.sin6_len = sizeof(lsa6);
9403 lsa6.sin6_family = AF_INET6;
9404 lsa6.sin6_addr = out6->ip6_src;
9405 bzero(&fsa6, sizeof(fsa6));
9406 fsa6.sin6_len = sizeof(fsa6);
9407 fsa6.sin6_family = AF_INET6;
9408 fsa6.sin6_addr = out6->ip6_dst;
9409 if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
9410 kprintf("sctp_operr_to calling ipv6 output:\n");
9412 sctp_print_address((struct sockaddr *)&lsa6);
9414 sctp_print_address((struct sockaddr *)&fsa6);
9416 #endif /* SCTP_DEBUG */
9417 ip6_output(scm, NULL, &ro, 0, NULL, NULL
9418 #if defined(__NetBSD__)
9421 #if (defined(__FreeBSD__) && __FreeBSD_version >= 480000) || defined(__DragonFly__)
9425 sctp_pegs[SCTP_DATAGRAMS_SENT]++;
9426 /* Free the route if we got one back */
9433 sctp_copy_one(struct mbuf *m, struct uio *uio, int cpsz, int resv_upfront, int *mbcnt)
9435 int left, cancpy, willcpy, error;
9444 if ((left+resv_upfront) > (int)MHLEN) {
9450 if ((m->m_flags & M_EXT) == 0) {
9454 *mbcnt += m->m_ext.ext_size;
9457 cancpy = M_TRAILINGSPACE(m);
9458 willcpy = min(cancpy, left);
9459 if ((willcpy + resv_upfront) > cancpy) {
9460 willcpy -= resv_upfront;
9463 /* Align data to the end */
9464 if ((m->m_flags & M_EXT) == 0) {
9465 if (m->m_flags & M_PKTHDR) {
9466 MH_ALIGN(m, willcpy);
9468 M_ALIGN(m, willcpy);
9471 MC_ALIGN(m, willcpy);
9473 error = uiomove(mtod(m, caddr_t), willcpy, uio);
9481 MGET(m->m_next, MB_WAIT, MT_DATA);
9482 if (m->m_next == NULL) {
9489 if (left > (int)MHLEN) {
9495 if ((m->m_flags & M_EXT) == 0) {
9499 *mbcnt += m->m_ext.ext_size;
9501 cancpy = M_TRAILINGSPACE(m);
9502 willcpy = min(cancpy, left);
9509 sctp_copy_it_in(struct sctp_inpcb *inp,
9510 struct sctp_tcb *stcb,
9511 struct sctp_association *asoc,
9512 struct sctp_nets *net,
9513 struct sctp_sndrcvinfo *srcv,
9517 /* This routine must be very careful in
9518 * its work. Protocol processing is
9519 * up and running so care must be taken to
9520 * spl...() when you need to do something
9521 * that may effect the stcb/asoc. The sb is
9522 * locked however. When data is copied the
9523 * protocol processing should be enabled since
9524 * this is a slower operation...
9528 int frag_size, mbcnt = 0, mbcnt_e = 0;
9529 unsigned int sndlen;
9530 unsigned int tot_demand;
9531 int tot_out, dataout;
9532 struct sctp_tmit_chunk *chk;
9534 struct sctp_stream_out *strq;
9539 so = stcb->sctp_socket;
9543 sndlen = uio->uio_resid;
9544 /* lock the socket buf */
9545 SOCKBUF_LOCK(&so->so_snd);
9546 error = ssb_lock(&so->so_snd, SBLOCKWAIT(flags));
9550 /* will it ever fit ? */
9551 if (sndlen > so->so_snd.ssb_hiwat) {
9552 /* It will NEVER fit */
9557 /* Do I need to block? */
9558 if ((so->so_snd.ssb_hiwat <
9559 (sndlen + asoc->total_output_queue_size)) ||
9560 (asoc->chunks_on_out_queue > sctp_max_chunks_on_queue) ||
9561 (asoc->total_output_mbuf_queue_size >
9562 so->so_snd.ssb_mbmax)
9564 /* prune any prsctp bufs out */
9565 if (asoc->peer_supports_prsctp) {
9566 sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
9569 * We store off a pointer to the endpoint.
9570 * Since on return from this we must check to
9571 * see if an so_error is set. If so we may have
9572 * been reset and our stcb destroyed. Returning
9573 * an error will flow back to the user...
9575 while ((so->so_snd.ssb_hiwat <
9576 (sndlen + asoc->total_output_queue_size)) ||
9577 (asoc->chunks_on_out_queue >
9578 sctp_max_chunks_on_queue) ||
9579 (asoc->total_output_mbuf_queue_size >
9580 so->so_snd.ssb_mbmax)
9582 if (flags & (MSG_FNONBLOCKING|MSG_DONTWAIT)) {
9583 /* Non-blocking io in place */
9584 error = EWOULDBLOCK;
9587 inp->sctp_tcb_at_block = (void *)stcb;
9588 inp->error_on_block = 0;
9589 #ifdef SCTP_BLK_LOGGING
9590 sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
9593 ssb_unlock(&so->so_snd);
9594 SCTP_TCB_UNLOCK(stcb);
9595 error = ssb_wait(&so->so_snd);
9596 SCTP_INP_RLOCK(inp);
9597 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
9598 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
9599 /* Should I really unlock ? */
9600 SCTP_INP_RUNLOCK(inp);
9604 SCTP_TCB_LOCK(stcb);
9605 SCTP_INP_RUNLOCK(inp);
9607 inp->sctp_tcb_at_block = 0;
9608 #ifdef SCTP_BLK_LOGGING
9609 sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
9612 if (inp->error_on_block) {
9614 * if our asoc was killed, the free code
9615 * (in sctp_pcb.c) will save a error in
9618 error = inp->error_on_block;
9626 /* did we encounter a socket error? */
9628 error = so->so_error;
9632 error = ssb_lock(&so->so_snd, M_WAITOK);
9634 /* Can't acquire the lock */
9638 #if defined(__FreeBSD__) && __FreeBSD_version >= 502115
9639 if (so->so_rcv.sb_state & SBS_CANTSENDMORE) {
9641 if (so->so_state & SS_CANTSENDMORE) {
9643 /* The socket is now set not to sendmore.. its gone */
9649 error = so->so_error;
9653 if (asoc->peer_supports_prsctp) {
9654 sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
9658 dataout = tot_out = uio->uio_resid;
9659 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
9660 resv_in_first = SCTP_MED_OVERHEAD;
9662 resv_in_first = SCTP_MED_V4_OVERHEAD;
9665 /* Are we aborting? */
9666 if (srcv->sinfo_flags & MSG_ABORT) {
9667 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
9668 (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_ECHOED)) {
9669 /* It has to be up before we abort */
9670 /* how big is the user initiated abort? */
9672 /* I wonder about doing a MGET without a splnet set.
9673 * it is done that way in the sosend code so I guess
9676 MGETHDR(mm, MB_WAIT, MT_DATA);
9678 struct sctp_paramhdr *ph;
9680 tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
9681 if (tot_demand > MHLEN) {
9682 if (tot_demand > MCLBYTES) {
9683 /* truncate user data */
9684 tot_demand = MCLBYTES;
9685 tot_out = tot_demand - sizeof(struct sctp_paramhdr);
9687 MCLGET(mm, MB_WAIT);
9688 if ((mm->m_flags & M_EXT) == 0) {
9689 /* truncate further */
9691 tot_out = tot_demand - sizeof(struct sctp_paramhdr);
9694 /* now move forward the data pointer */
9695 ph = mtod(mm, struct sctp_paramhdr *);
9696 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
9697 ph->param_length = htons((sizeof(struct sctp_paramhdr) + tot_out));
9699 mm->m_pkthdr.len = tot_out + sizeof(struct sctp_paramhdr);
9700 mm->m_len = mm->m_pkthdr.len;
9701 error = uiomove((caddr_t)ph, tot_out, uio);
9704 * Here if we can't get his data we
9705 * still abort we just don't get to
9706 * send the users note :-0
9712 ssb_unlock(&so->so_snd);
9713 SOCKBUF_UNLOCK(&so->so_snd);
9714 sctp_abort_an_association(stcb->sctp_ep, stcb,
9715 SCTP_RESPONSE_TO_USER_REQ,
9725 /* Now can we send this? */
9726 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
9727 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
9728 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
9729 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
9730 /* got data while shutting down */
9735 /* Is the stream no. valid? */
9736 if (srcv->sinfo_stream >= asoc->streamoutcnt) {
9737 /* Invalid stream number */
9742 if (asoc->strmout == NULL) {
9743 /* huh? software error */
9745 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
9746 kprintf("software error in sctp_copy_it_in\n");
9753 if ((srcv->sinfo_flags & MSG_EOF) &&
9754 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
9765 /* save off the tag */
9766 my_vtag = asoc->my_vtag;
9767 strq = &asoc->strmout[srcv->sinfo_stream];
9768 /* First lets figure out the "chunking" point */
9769 frag_size = sctp_get_frag_point(stcb, asoc);
9771 /* two choices here, it all fits in one chunk or
9772 * we need multiple chunks.
9775 SOCKBUF_UNLOCK(&so->so_snd);
9776 if (tot_out <= frag_size) {
9777 /* no need to setup a template */
9778 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
9781 SOCKBUF_LOCK(&so->so_snd);
9784 sctppcbinfo.ipi_count_chunk++;
9785 sctppcbinfo.ipi_gencnt_chunk++;
9786 asoc->chunks_on_out_queue++;
9787 MGETHDR(mm, MB_WAIT, MT_DATA);
9792 error = sctp_copy_one(mm, uio, tot_out, resv_in_first, &mbcnt_e);
9795 sctp_prepare_chunk(chk, stcb, srcv, strq, net);
9796 chk->mbcnt = mbcnt_e;
9799 mm->m_pkthdr.len = tot_out;
9803 /* the actual chunk flags */
9804 chk->rec.data.rcv_flags |= SCTP_DATA_NOT_FRAG;
9805 chk->whoTo->ref_count++;
9807 /* fix up the send_size if it is not present */
9808 chk->send_size = tot_out;
9809 chk->book_size = chk->send_size;
9810 /* ok, we are commited */
9811 if ((srcv->sinfo_flags & MSG_UNORDERED) == 0) {
9812 /* bump the ssn if we are unordered. */
9813 strq->next_sequence_sent++;
9815 if (chk->flags & SCTP_PR_SCTP_BUFFER) {
9816 asoc->sent_queue_cnt_removeable++;
9819 if ((asoc->state == 0) ||
9820 (my_vtag != asoc->my_vtag) ||
9821 (so != inp->sctp_socket) ||
9822 (inp->sctp_socket == 0)) {
9823 /* connection was aborted */
9828 asoc->stream_queue_cnt++;
9829 TAILQ_INSERT_TAIL(&strq->outqueue, chk, sctp_next);
9830 /* now check if this stream is on the wheel */
9831 if ((strq->next_spoke.tqe_next == NULL) &&
9832 (strq->next_spoke.tqe_prev == NULL)) {
9833 /* Insert it on the wheel since it is not
9836 sctp_insert_on_wheel(asoc, strq);
9841 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
9842 sctppcbinfo.ipi_count_chunk--;
9843 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
9844 panic("Chunk count is negative");
9846 SOCKBUF_LOCK(&so->so_snd);
9850 /* we need to setup a template */
9851 struct sctp_tmit_chunk template;
9852 struct sctpchunk_listhead tmp;
9854 /* setup the template */
9855 sctp_prepare_chunk(&template, stcb, srcv, strq, net);
9857 /* Prepare the temp list */
9860 /* Template is complete, now time for the work */
9861 while (tot_out > 0) {
9863 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
9866 * ok we must spin through and dump anything
9867 * we have allocated and then jump to the
9872 sctppcbinfo.ipi_count_chunk++;
9873 asoc->chunks_on_out_queue++;
9875 sctppcbinfo.ipi_gencnt_chunk++;
9877 chk->whoTo->ref_count++;
9878 MGETHDR(chk->data, MB_WAIT, MT_DATA);
9879 if (chk->data == NULL) {
9883 tot_demand = min(tot_out, frag_size);
9884 error = sctp_copy_one(chk->data, uio, tot_demand , resv_in_first, &mbcnt_e);
9887 /* now fix the chk->send_size */
9888 chk->mbcnt = mbcnt_e;
9891 chk->send_size = tot_demand;
9892 chk->data->m_pkthdr.len = tot_demand;
9893 chk->book_size = chk->send_size;
9894 if (chk->flags & SCTP_PR_SCTP_BUFFER) {
9895 asoc->sent_queue_cnt_removeable++;
9897 TAILQ_INSERT_TAIL(&tmp, chk, sctp_next);
9898 tot_out -= tot_demand;
9900 /* Now the tmp list holds all chunks and data */
9901 if ((srcv->sinfo_flags & MSG_UNORDERED) == 0) {
9902 /* bump the ssn if we are unordered. */
9903 strq->next_sequence_sent++;
9905 /* Mark the first/last flags. This will
9906 * result int a 3 for a single item on the list
9908 chk = TAILQ_FIRST(&tmp);
9909 chk->rec.data.rcv_flags |= SCTP_DATA_FIRST_FRAG;
9910 chk = TAILQ_LAST(&tmp, sctpchunk_listhead);
9911 chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
9913 /* now move it to the streams actual queue */
9914 /* first stop protocol processing */
9916 if ((asoc->state == 0) ||
9917 (my_vtag != asoc->my_vtag) ||
9918 (so != inp->sctp_socket) ||
9919 (inp->sctp_socket == 0)) {
9920 /* connection was aborted */
9925 chk = TAILQ_FIRST(&tmp);
9927 chk->data->m_nextpkt = 0;
9928 TAILQ_REMOVE(&tmp, chk, sctp_next);
9929 asoc->stream_queue_cnt++;
9930 TAILQ_INSERT_TAIL(&strq->outqueue, chk, sctp_next);
9931 chk = TAILQ_FIRST(&tmp);
9933 /* now check if this stream is on the wheel */
9934 if ((strq->next_spoke.tqe_next == NULL) &&
9935 (strq->next_spoke.tqe_prev == NULL)) {
9936 /* Insert it on the wheel since it is not
9939 sctp_insert_on_wheel(asoc, strq);
9941 /* Ok now we can allow pping */
9945 SOCKBUF_LOCK(&so->so_snd);
9946 chk = TAILQ_FIRST(&tmp);
9949 sctp_m_freem(chk->data);
9952 TAILQ_REMOVE(&tmp, chk, sctp_next);
9953 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
9954 sctppcbinfo.ipi_count_chunk--;
9955 asoc->chunks_on_out_queue--;
9956 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
9957 panic("Chunk count is negative");
9959 sctppcbinfo.ipi_gencnt_chunk++;
9960 chk = TAILQ_FIRST(&tmp);
9966 #ifdef SCTP_MBCNT_LOGGING
9967 sctp_log_mbcnt(SCTP_LOG_MBCNT_INCREASE,
9968 asoc->total_output_queue_size,
9970 asoc->total_output_mbuf_queue_size,
9974 SOCKBUF_LOCK(&so->so_snd);
9975 asoc->total_output_queue_size += dataout;
9976 asoc->total_output_mbuf_queue_size += mbcnt;
9977 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
9978 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
9979 so->so_snd.ssb_cc += dataout;
9980 so->so_snd.ssb_mbcnt += mbcnt;
9982 if ((srcv->sinfo_flags & MSG_EOF) &&
9983 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)
9985 int some_on_streamwheel = 0;
9987 if (!TAILQ_EMPTY(&asoc->out_wheel)) {
9988 /* Check to see if some data queued */
9989 struct sctp_stream_out *outs;
9990 TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) {
9991 if (!TAILQ_EMPTY(&outs->outqueue)) {
9992 some_on_streamwheel = 1;
9997 if (TAILQ_EMPTY(&asoc->send_queue) &&
9998 TAILQ_EMPTY(&asoc->sent_queue) &&
9999 (some_on_streamwheel == 0)) {
10000 /* there is nothing queued to send, so I'm done... */
10001 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
10002 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
10003 /* only send SHUTDOWN the first time through */
10005 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
10006 kprintf("%s:%d sends a shutdown\n",
10012 sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
10013 asoc->state = SCTP_STATE_SHUTDOWN_SENT;
10014 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
10015 asoc->primary_destination);
10016 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
10017 asoc->primary_destination);
10021 * we still got (or just got) data to send, so set
10025 * XXX sockets draft says that MSG_EOF should be sent
10026 * with no data. currently, we will allow user data
10027 * to be sent first and move to SHUTDOWN-PENDING
10029 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
10034 if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
10035 kprintf("++total out:%d total_mbuf_out:%d\n",
10036 (int)asoc->total_output_queue_size,
10037 (int)asoc->total_output_mbuf_queue_size);
10042 ssb_unlock(&so->so_snd);
10044 SOCKBUF_UNLOCK(&so->so_snd);
10053 sctp_sosend(struct socket *so,
10055 struct mbuf *addr_mbuf,
10057 struct sockaddr *addr,
10061 struct mbuf *control,
10062 #if defined(__NetBSD__) || defined(__APPLE__)
10066 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
10074 int error, use_rcvinfo;
10075 int queue_only = 0, queue_only_for_init=0;
10078 struct sctp_inpcb *inp;
10079 struct sctp_tcb *stcb=NULL;
10080 struct sctp_sndrcvinfo srcv;
10081 struct timeval now;
10082 struct sctp_nets *net;
10083 struct sctp_association *asoc;
10084 struct sctp_inpcb *t_inp;
10085 int create_lock_applied = 0;
10086 #if defined(__APPLE__)
10087 struct proc *p = current_proc();
10088 #elif defined(__NetBSD__)
10089 struct proc *p = curproc; /* XXX */
10090 struct sockaddr *addr = NULL;
10092 addr = mtod(addr_mbuf, struct sockaddr *);
10095 error = use_rcvinfo = 0;
10099 t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
10103 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
10104 (inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING)) {
10105 /* The listner can NOT send */
10111 SCTP_ASOC_CREATE_LOCK(inp);
10112 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
10113 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
10114 /* Should I really unlock ? */
10120 create_lock_applied = 1;
10121 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
10122 (addr->sa_family == AF_INET6)) {
10128 /* now we must find the assoc */
10129 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
10130 SCTP_INP_RLOCK(inp);
10131 stcb = LIST_FIRST(&inp->sctp_asoc_list);
10132 if (stcb == NULL) {
10133 SCTP_INP_RUNLOCK(inp);
10138 SCTP_TCB_LOCK(stcb);
10139 SCTP_INP_RUNLOCK(inp);
10140 net = stcb->asoc.primary_destination;
10144 /* process cmsg snd/rcv info (maybe a assoc-id) */
10145 if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&srcv, control,
10148 if (srcv.sinfo_flags & MSG_SENDALL) {
10149 /* its a sendall */
10150 sctppcbinfo.mbuf_track--;
10151 sctp_m_freem(control);
10153 if (create_lock_applied) {
10154 SCTP_ASOC_CREATE_UNLOCK(inp);
10155 create_lock_applied = 0;
10157 return (sctp_sendall(inp, uio, top, &srcv));
10162 if (stcb == NULL) {
10163 /* Need to do a lookup */
10164 if (use_rcvinfo && srcv.sinfo_assoc_id) {
10165 stcb = sctp_findassociation_ep_asocid(inp, srcv.sinfo_assoc_id);
10167 * Question: Should I error here if the assoc_id is
10168 * no longer valid? i.e. I can't find it?
10172 /* Must locate the net structure */
10173 net = sctp_findnet(stcb, addr);
10176 if (stcb == NULL) {
10177 if (addr != NULL) {
10178 /* Since we did not use findep we must
10179 * increment it, and if we don't find a
10180 * tcb decrement it.
10182 SCTP_INP_WLOCK(inp);
10183 SCTP_INP_INCR_REF(inp);
10184 SCTP_INP_WUNLOCK(inp);
10185 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
10186 if (stcb == NULL) {
10187 SCTP_INP_WLOCK(inp);
10188 SCTP_INP_DECR_REF(inp);
10189 SCTP_INP_WUNLOCK(inp);
10194 if ((stcb == NULL) &&
10195 (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) {
10199 } else if ((stcb == NULL) && (addr == NULL)) {
10203 } else if (stcb == NULL) {
10204 /* UDP style, we must go ahead and start the INIT process */
10205 if ((use_rcvinfo) &&
10206 (srcv.sinfo_flags & MSG_ABORT)) {
10207 /* User asks to abort a non-existant asoc */
10212 /* get an asoc/stcb struct */
10213 stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0);
10214 if (stcb == NULL) {
10215 /* Error is setup for us in the call */
10219 if (create_lock_applied) {
10220 SCTP_ASOC_CREATE_UNLOCK(inp);
10221 create_lock_applied = 0;
10223 kprintf("Huh-3? create lock should have been on??\n");
10225 /* Turn on queue only flag to prevent data from being sent */
10227 asoc = &stcb->asoc;
10228 asoc->state = SCTP_STATE_COOKIE_WAIT;
10229 SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
10231 /* see if a init structure exists in cmsg headers */
10232 struct sctp_initmsg initm;
10234 if (sctp_find_cmsg(SCTP_INIT, (void *)&initm, control, sizeof(initm))) {
10235 /* we have an INIT override of the default */
10236 if (initm.sinit_max_attempts)
10237 asoc->max_init_times = initm.sinit_max_attempts;
10238 if (initm.sinit_num_ostreams)
10239 asoc->pre_open_streams = initm.sinit_num_ostreams;
10240 if (initm.sinit_max_instreams)
10241 asoc->max_inbound_streams = initm.sinit_max_instreams;
10242 if (initm.sinit_max_init_timeo)
10243 asoc->initial_init_rto_max = initm.sinit_max_init_timeo;
10244 if (asoc->streamoutcnt < asoc->pre_open_streams) {
10245 /* Default is NOT correct */
10247 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
10248 kprintf("Ok, defout:%d pre_open:%d\n",
10249 asoc->streamoutcnt, asoc->pre_open_streams);
10252 kfree(asoc->strmout, M_PCB);
10253 asoc->strmout = NULL;
10254 asoc->streamoutcnt = asoc->pre_open_streams;
10256 /* What happesn if this fails? .. we panic ...*/
10258 kmalloc(asoc->streamoutcnt *
10259 sizeof(struct sctp_stream_out),
10261 for (i = 0; i < asoc->streamoutcnt; i++) {
10263 * inbound side must be set to 0xffff,
10264 * also NOTE when we get the INIT-ACK
10265 * back (for INIT sender) we MUST
10266 * reduce the count (streamoutcnt) but
10267 * first check if we sent to any of the
10268 * upper streams that were dropped (if
10269 * some were). Those that were dropped
10270 * must be notified to the upper layer
10271 * as failed to send.
10273 asoc->strmout[i].next_sequence_sent = 0x0;
10274 TAILQ_INIT(&asoc->strmout[i].outqueue);
10275 asoc->strmout[i].stream_no = i;
10276 asoc->strmout[i].next_spoke.tqe_next = 0;
10277 asoc->strmout[i].next_spoke.tqe_prev = 0;
10283 /* out with the INIT */
10284 queue_only_for_init = 1;
10285 sctp_send_initiate(inp, stcb);
10287 * we may want to dig in after this call and adjust the MTU
10288 * value. It defaulted to 1500 (constant) but the ro structure
10289 * may now have an update and thus we may need to change it
10290 * BEFORE we append the message.
10292 net = stcb->asoc.primary_destination;
10293 asoc = &stcb->asoc;
10295 asoc = &stcb->asoc;
10297 if (create_lock_applied) {
10298 SCTP_ASOC_CREATE_UNLOCK(inp);
10299 create_lock_applied = 0;
10301 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
10302 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
10305 if (use_rcvinfo == 0) {
10306 /* Grab the default stuff from the asoc */
10307 srcv = stcb->asoc.def_send;
10309 /* we are now done with all control */
10311 sctp_m_freem(control);
10315 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
10316 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
10317 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
10318 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
10319 if ((use_rcvinfo) &&
10320 (srcv.sinfo_flags & MSG_ABORT)) {
10323 error = ECONNRESET;
10328 /* Ok, we will attempt a msgsnd :> */
10330 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
10331 p->td_lwp->lwp_ru.ru_msgsnd++;
10333 p->p_stats->p_ru.ru_msgsnd++;
10337 if (net && ((srcv.sinfo_flags & MSG_ADDR_OVER))) {
10338 /* we take the override or the unconfirmed */
10341 net = stcb->asoc.primary_destination;
10346 /* Must copy it all in from user land. The
10347 * socket buf is locked but we don't suspend
10348 * protocol processing until we are ready to
10352 error = sctp_copy_it_in(inp, stcb, asoc, net, &srcv, uio, flags);
10356 /* Here we must either pull in the user data to chunk
10357 * buffers, or use top to do a msg_append.
10359 error = sctp_msg_append(stcb, net, top, &srcv, flags);
10363 /* zap the top since it is now being used */
10367 if (net->flight_size > net->cwnd) {
10368 sctp_pegs[SCTP_SENDTO_FULL_CWND]++;
10371 } else if (asoc->ifp_had_enobuf) {
10372 sctp_pegs[SCTP_QUEONLY_BURSTLMT]++;
10375 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
10376 ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) * sizeof(struct sctp_data_chunk)) +
10377 SCTP_MED_OVERHEAD);
10379 if (((inp->sctp_flags & SCTP_PCB_FLAGS_NODELAY) == 0) &&
10380 (stcb->asoc.total_flight > 0) &&
10381 (un_sent < (int)stcb->asoc.smallest_mtu)) {
10383 /* Ok, Nagle is set on and we have data outstanding. Don't
10384 * send anything and let SACKs drive out the data unless we
10385 * have a "full" segment to send.
10387 sctp_pegs[SCTP_NAGLE_NOQ]++;
10390 sctp_pegs[SCTP_NAGLE_OFF]++;
10393 if (queue_only_for_init) {
10394 /* It is possible to have a turn around of the
10395 * INIT/INIT-ACK/COOKIE before I have a chance to
10396 * copy in the data. In such a case I DO want to
10397 * send it out by reversing the queue only flag.
10399 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) ||
10400 (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_ECHOED)) {
10401 /* yep, reverse it */
10406 if ((queue_only == 0) && (stcb->asoc.peers_rwnd && un_sent)) {
10407 /* we can attempt to send too.*/
10409 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
10410 kprintf("USR Send calls sctp_chunk_output\n");
10414 sctp_pegs[SCTP_OUTPUT_FRM_SND]++;
10415 sctp_chunk_output(inp, stcb, 0);
10417 } else if ((queue_only == 0) &&
10418 (stcb->asoc.peers_rwnd == 0) &&
10419 (stcb->asoc.total_flight == 0)) {
10420 /* We get to have a probe outstanding */
10422 sctp_from_user_send = 1;
10423 sctp_chunk_output(inp, stcb, 0);
10424 sctp_from_user_send = 0;
10427 } else if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
10428 int num_out, reason, cwnd_full;
10429 /* Here we do control only */
10431 sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
10432 &reason, 1, &cwnd_full, 1, &now, &now_filled);
10436 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
10437 kprintf("USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d \n",
10438 queue_only, stcb->asoc.peers_rwnd, un_sent,
10439 stcb->asoc.total_flight, stcb->asoc.chunks_on_out_queue,
10440 stcb->asoc.total_output_queue_size);
10444 if (create_lock_applied) {
10445 SCTP_ASOC_CREATE_UNLOCK(inp);
10446 create_lock_applied = 0;
10449 SCTP_TCB_UNLOCK(stcb);
10453 sctp_m_freem(control);