Home | History | Annotate | Line # | Download | only in netinet
sctp_output.c revision 1.12
      1 /*	$NetBSD: sctp_output.c,v 1.12 2017/12/10 11:52:14 rjs Exp $ */
      2 /*	$KAME: sctp_output.c,v 1.48 2005/06/16 18:29:24 jinmei Exp $	*/
      3 
      4 /*
      5  * Copyright (C) 2002, 2003, 2004 Cisco Systems Inc,
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. Neither the name of the project nor the names of its contributors
     17  *    may be used to endorse or promote products derived from this software
     18  *    without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  * SUCH DAMAGE.
     31  */
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: sctp_output.c,v 1.12 2017/12/10 11:52:14 rjs Exp $");
     34 
     35 #ifdef _KERNEL_OPT
     36 #include "opt_ipsec.h"
     37 #include "opt_inet.h"
     38 #include "opt_sctp.h"
     39 #endif /* _KERNEL_OPT */
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/malloc.h>
     44 #include <sys/mbuf.h>
     45 #include <sys/domain.h>
     46 #include <sys/protosw.h>
     47 #include <sys/socket.h>
     48 #include <sys/socketvar.h>
     49 #include <sys/proc.h>
     50 #include <sys/kernel.h>
     51 #include <sys/sysctl.h>
     52 #include <sys/resourcevar.h>
     53 #include <sys/uio.h>
     54 #ifdef INET6
     55 #include <sys/domain.h>
     56 #endif
     57 
     58 #include <machine/limits.h>
     59 #include <machine/cpu.h>
     60 
     61 #include <net/if.h>
     62 #include <net/if_types.h>
     63 
     64 #include <net/route.h>
     65 
     66 #include <netinet/in.h>
     67 #include <netinet/in_systm.h>
     68 #include <netinet/ip.h>
     69 #include <netinet/in_pcb.h>
     70 #include <netinet/in_var.h>
     71 #include <netinet/ip_var.h>
     72 
     73 #ifdef INET6
     74 #include <netinet/ip6.h>
     75 #include <netinet6/ip6_var.h>
     76 #include <netinet6/scope6_var.h>
     77 #include <netinet6/nd6.h>
     78 
     79 #include <netinet6/in6_pcb.h>
     80 
     81 #include <netinet/icmp6.h>
     82 
     83 #endif /* INET6 */
     84 
     85 #include <net/net_osdep.h>
     86 
     87 #if defined(HAVE_NRL_INPCB) || defined(__FreeBSD__)
     88 #ifndef in6pcb
     89 #define in6pcb		inpcb
     90 #endif
     91 #endif
     92 
     93 #include <netinet/sctp_pcb.h>
     94 
     95 #ifdef IPSEC
     96 #include <netipsec/ipsec.h>
     97 #include <netipsec/key.h>
     98 #endif /* IPSEC */
     99 
    100 #include <netinet/sctp_var.h>
    101 #include <netinet/sctp_header.h>
    102 #include <netinet/sctputil.h>
    103 #include <netinet/sctp_pcb.h>
    104 #include <netinet/sctp_output.h>
    105 #include <netinet/sctp_uio.h>
    106 #include <netinet/sctputil.h>
    107 #include <netinet/sctp_hashdriver.h>
    108 #include <netinet/sctp_timer.h>
    109 #include <netinet/sctp_asconf.h>
    110 #include <netinet/sctp_indata.h>
    111 
    112 #ifdef SCTP_DEBUG
    113 extern uint32_t sctp_debug_on;
    114 #endif
    115 
    116 extern int sctp_peer_chunk_oh;
    117 
    118 static int
    119 sctp_find_cmsg(int c_type, void *data, struct mbuf *control, int cpsize)
    120 {
    121 	struct cmsghdr cmh;
    122 	int tlen, at;
    123 
    124 	tlen = control->m_len;
    125 	at = 0;
    126 	/*
    127 	 * Independent of how many mbufs, find the c_type inside the control
    128 	 * structure and copy out the data.
    129 	 */
    130 	while (at < tlen) {
    131 		if ((tlen-at) < (int)CMSG_ALIGN(sizeof(cmh))) {
    132 			/* not enough room for one more we are done. */
    133 			return (0);
    134 		}
    135 		m_copydata(control, at, sizeof(cmh), (void *)&cmh);
    136 		if ((cmh.cmsg_len + at) > tlen) {
    137 			/*
    138 			 * this is real messed up since there is not enough
    139 			 * data here to cover the cmsg header. We are done.
    140 			 */
    141 			return (0);
    142 		}
    143 		if ((cmh.cmsg_level == IPPROTO_SCTP) &&
    144 		    (c_type == cmh.cmsg_type)) {
    145 			/* found the one we want, copy it out */
    146 			at += CMSG_ALIGN(sizeof(struct cmsghdr));
    147 			if ((int)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < cpsize) {
    148 				/*
    149 				 * space of cmsg_len after header not
    150 				 * big enough
    151 				 */
    152 				return (0);
    153 			}
    154 			m_copydata(control, at, cpsize, data);
    155 			return (1);
    156 		 } else {
    157 			at += CMSG_ALIGN(cmh.cmsg_len);
    158 			if (cmh.cmsg_len == 0) {
    159 				break;
    160 			}
    161 		}
    162 	}
    163 	/* not found */
    164 	return (0);
    165 }
    166 
    167 static struct mbuf *
    168 sctp_add_addr_to_mbuf(struct mbuf *m, struct ifaddr *ifa)
    169 {
    170 	struct sctp_paramhdr *parmh;
    171 	struct mbuf *mret;
    172 	int len;
    173 	if (ifa->ifa_addr->sa_family == AF_INET) {
    174 		len = sizeof(struct sctp_ipv4addr_param);
    175 	} else if (ifa->ifa_addr->sa_family == AF_INET6) {
    176 		len = sizeof(struct sctp_ipv6addr_param);
    177 	} else {
    178 		/* unknown type */
    179 		return (m);
    180 	}
    181 
    182 	if (M_TRAILINGSPACE(m) >= len) {
    183 		/* easy side we just drop it on the end */
    184 		parmh = (struct sctp_paramhdr *)(m->m_data + m->m_len);
    185 		mret = m;
    186 	} else {
    187 		/* Need more space */
    188 		mret = m;
    189 		while (mret->m_next != NULL) {
    190 			mret = mret->m_next;
    191 		}
    192 		MGET(mret->m_next, M_DONTWAIT, MT_DATA);
    193 		if (mret->m_next == NULL) {
    194 			/* We are hosed, can't add more addresses */
    195 			return (m);
    196 		}
    197 		mret = mret->m_next;
    198 		parmh = mtod(mret, struct sctp_paramhdr *);
    199 	}
    200 	/* now add the parameter */
    201 	if (ifa->ifa_addr->sa_family == AF_INET) {
    202 		struct sctp_ipv4addr_param *ipv4p;
    203 		struct sockaddr_in *sin;
    204 		sin = (struct sockaddr_in *)ifa->ifa_addr;
    205 		ipv4p = (struct sctp_ipv4addr_param *)parmh;
    206 		parmh->param_type = htons(SCTP_IPV4_ADDRESS);
    207 		parmh->param_length = htons(len);
    208 		ipv4p->addr = sin->sin_addr.s_addr;
    209 		mret->m_len += len;
    210 	} else if (ifa->ifa_addr->sa_family == AF_INET6) {
    211 		struct sctp_ipv6addr_param *ipv6p;
    212 		struct sockaddr_in6 *sin6;
    213 		sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
    214 		ipv6p = (struct sctp_ipv6addr_param *)parmh;
    215 		parmh->param_type = htons(SCTP_IPV6_ADDRESS);
    216 		parmh->param_length = htons(len);
    217 		memcpy(ipv6p->addr, &sin6->sin6_addr,
    218 		    sizeof(ipv6p->addr));
    219 		/* clear embedded scope in the address */
    220 		in6_clearscope((struct in6_addr *)ipv6p->addr);
    221 		mret->m_len += len;
    222 	} else {
    223 		return (m);
    224 	}
    225 	return (mret);
    226 }
    227 
    228 
    229 
    230 static struct mbuf *
    231 sctp_add_cookie(struct sctp_inpcb *inp, struct mbuf *init, int init_offset,
    232     struct mbuf *initack, int initack_offset, struct sctp_state_cookie *stc_in)
    233 {
    234 	struct mbuf *copy_init, *copy_initack, *m_at, *sig, *mret;
    235 	struct sctp_state_cookie *stc;
    236 	struct sctp_paramhdr *ph;
    237 	uint8_t *signature;
    238 	int sig_offset;
    239 	uint16_t cookie_sz;
    240 
    241 	mret = NULL;
    242 
    243 	MGET(mret, M_DONTWAIT, MT_DATA);
    244 	if (mret == NULL) {
    245 		return (NULL);
    246 	}
    247 	copy_init = sctp_m_copym(init, init_offset, M_COPYALL, M_DONTWAIT);
    248 	if (copy_init == NULL) {
    249 		sctp_m_freem(mret);
    250 		return (NULL);
    251 	}
    252 	copy_initack = sctp_m_copym(initack, initack_offset, M_COPYALL,
    253 	    M_DONTWAIT);
    254 	if (copy_initack == NULL) {
    255 		sctp_m_freem(mret);
    256 		sctp_m_freem(copy_init);
    257 		return (NULL);
    258 	}
    259 	/* easy side we just drop it on the end */
    260 	ph = mtod(mret, struct sctp_paramhdr *);
    261 	mret->m_len = sizeof(struct sctp_state_cookie) +
    262 	    sizeof(struct sctp_paramhdr);
    263 	stc = (struct sctp_state_cookie *)((vaddr_t)ph +
    264 	    sizeof(struct sctp_paramhdr));
    265 	ph->param_type = htons(SCTP_STATE_COOKIE);
    266 	ph->param_length = 0;	/* fill in at the end */
    267 	/* Fill in the stc cookie data */
    268 	*stc = *stc_in;
    269 
    270 	/* tack the INIT and then the INIT-ACK onto the chain */
    271 	cookie_sz = 0;
    272 	m_at = mret;
    273 	for (m_at = mret; m_at; m_at = m_at->m_next) {
    274 		cookie_sz += m_at->m_len;
    275 		if (m_at->m_next == NULL) {
    276 			m_at->m_next = copy_init;
    277 			break;
    278 		}
    279 	}
    280 
    281 	for (m_at = copy_init; m_at; m_at = m_at->m_next) {
    282 		cookie_sz += m_at->m_len;
    283 		if (m_at->m_next == NULL) {
    284 			m_at->m_next = copy_initack;
    285 			break;
    286 		}
    287 	}
    288 
    289 	for (m_at = copy_initack; m_at; m_at = m_at->m_next) {
    290 		cookie_sz += m_at->m_len;
    291 		if (m_at->m_next == NULL) {
    292 			break;
    293 		}
    294 	}
    295 	MGET(sig, M_DONTWAIT, MT_DATA);
    296 	if (sig == NULL) {
    297 		/* no space */
    298 		sctp_m_freem(mret);
    299 		sctp_m_freem(copy_init);
    300 		sctp_m_freem(copy_initack);
    301 		return (NULL);
    302 	}
    303 	sig->m_len = 0;
    304 	m_at->m_next = sig;
    305 	sig_offset = 0;
    306 	signature = (uint8_t *)(mtod(sig, vaddr_t) + sig_offset);
    307 	/* Time to sign the cookie */
    308 	sctp_hash_digest_m((char *)inp->sctp_ep.secret_key[
    309 	    (int)(inp->sctp_ep.current_secret_number)],
    310 	    SCTP_SECRET_SIZE, mret, sizeof(struct sctp_paramhdr),
    311 	    (uint8_t *)signature);
    312 	sig->m_len += SCTP_SIGNATURE_SIZE;
    313 	cookie_sz += SCTP_SIGNATURE_SIZE;
    314 
    315 	ph->param_length = htons(cookie_sz);
    316 	return (mret);
    317 }
    318 
    319 
    320 static struct sockaddr_in *
    321 sctp_is_v4_ifa_addr_prefered (struct ifaddr *ifa, uint8_t loopscope, uint8_t ipv4_scope, uint8_t *sin_loop, uint8_t *sin_local)
    322 {
    323 	struct sockaddr_in *sin;
    324 	/*
    325 	 * Here we determine if its a prefered address. A
    326 	 * prefered address means it is the same scope or
    327 	 * higher scope then the destination.
    328 	 *  L = loopback, P = private, G = global
    329 	 * -----------------------------------------
    330 	 *  src    |      dest     |    result
    331 	 *-----------------------------------------
    332 	 *   L     |       L       |    yes
    333 	 *-----------------------------------------
    334 	 *   P     |       L       |    yes
    335 	 *-----------------------------------------
    336 	 *   G     |       L       |    yes
    337 	 *-----------------------------------------
    338 	 *   L     |       P       |    no
    339 	 *-----------------------------------------
    340 	 *   P     |       P       |    yes
    341 	 *-----------------------------------------
    342 	 *   G     |       P       |    no
    343 	 *-----------------------------------------
    344 	 *   L     |       G       |    no
    345 	 *-----------------------------------------
    346 	 *   P     |       G       |    no
    347 	 *-----------------------------------------
    348 	 *   G     |       G       |    yes
    349 	 *-----------------------------------------
    350 	 */
    351 
    352 	if (ifa->ifa_addr->sa_family != AF_INET) {
    353 		/* forget non-v4 */
    354 		return (NULL);
    355 	}
    356 	/* Ok the address may be ok */
    357 	sin = (struct sockaddr_in *)ifa->ifa_addr;
    358 	if (sin->sin_addr.s_addr == 0) {
    359 		return (NULL);
    360 	}
    361 	*sin_local = *sin_loop = 0;
    362 	if ((ifa->ifa_ifp->if_type == IFT_LOOP) ||
    363 	    (IN4_ISLOOPBACK_ADDRESS(&sin->sin_addr))) {
    364 		*sin_loop = 1;
    365 		*sin_local = 1;
    366 	}
    367 	if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
    368 		*sin_local = 1;
    369 	}
    370 	if (!loopscope && *sin_loop) {
    371 		/* Its a loopback address and we don't have loop scope */
    372 		return (NULL);
    373 	}
    374 	if (!ipv4_scope && *sin_local) {
    375 		/* Its a private address, and we don't have private address scope */
    376 		return (NULL);
    377 	}
    378 	if (((ipv4_scope == 0) && (loopscope == 0)) && (*sin_local)) {
    379 		/* its a global src and a private dest */
    380 		return (NULL);
    381 	}
    382 	/* its a prefered address */
    383 	return (sin);
    384 }
    385 
    386 static struct sockaddr_in *
    387 sctp_is_v4_ifa_addr_acceptable (struct ifaddr *ifa, uint8_t loopscope, uint8_t ipv4_scope, uint8_t *sin_loop, uint8_t *sin_local)
    388 {
    389 	struct sockaddr_in *sin;
    390 	/*
    391 	 * Here we determine if its a acceptable address. A
    392 	 * acceptable address means it is the same scope or
    393 	 * higher scope but we can allow for NAT which means
    394 	 * its ok to have a global dest and a private src.
    395 	 *
    396 	 *  L = loopback, P = private, G = global
    397 	 * -----------------------------------------
    398 	 *  src    |      dest     |    result
    399 	 *-----------------------------------------
    400 	 *   L     |       L       |    yes
    401 	 *-----------------------------------------
    402 	 *   P     |       L       |    yes
    403 	 *-----------------------------------------
    404 	 *   G     |       L       |    yes
    405 	 *-----------------------------------------
    406 	 *   L     |       P       |    no
    407 	 *-----------------------------------------
    408 	 *   P     |       P       |    yes
    409 	 *-----------------------------------------
    410 	 *   G     |       P       |    yes - probably this won't work.
    411 	 *-----------------------------------------
    412 	 *   L     |       G       |    no
    413 	 *-----------------------------------------
    414 	 *   P     |       G       |    yes
    415 	 *-----------------------------------------
    416 	 *   G     |       G       |    yes
    417 	 *-----------------------------------------
    418 	 */
    419 
    420 	if (ifa->ifa_addr->sa_family != AF_INET) {
    421 		/* forget non-v4 */
    422 		return (NULL);
    423 	}
    424 	/* Ok the address may be ok */
    425 	sin = (struct sockaddr_in *)ifa->ifa_addr;
    426 	if (sin->sin_addr.s_addr == 0) {
    427 		return (NULL);
    428 	}
    429 	*sin_local = *sin_loop = 0;
    430 	if ((ifa->ifa_ifp->if_type == IFT_LOOP) ||
    431 	    (IN4_ISLOOPBACK_ADDRESS(&sin->sin_addr))) {
    432 		*sin_loop = 1;
    433 		*sin_local = 1;
    434 	}
    435 	if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
    436 		*sin_local = 1;
    437 	}
    438 	if (!loopscope && *sin_loop) {
    439 		/* Its a loopback address and we don't have loop scope */
    440 		return (NULL);
    441 	}
    442 	/* its an acceptable address */
    443 	return (sin);
    444 }
    445 
    446 /*
    447  * This treats the address list on the ep as a restricted list
    448  * (negative list). If a the passed address is listed, then
    449  * the address is NOT allowed on the association.
    450  */
    451 int
    452 sctp_is_addr_restricted(struct sctp_tcb *stcb, struct sockaddr *addr)
    453 {
    454 	struct sctp_laddr *laddr;
    455 #ifdef SCTP_DEBUG
    456 	int cnt=0;
    457 #endif
    458 	if (stcb == NULL) {
    459 		/* There are no restrictions, no TCB :-) */
    460 		return (0);
    461 	}
    462 #ifdef SCTP_DEBUG
    463 	LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list, sctp_nxt_addr) {
    464 		cnt++;
    465 	}
    466 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
    467 		printf("There are %d addresses on the restricted list\n", cnt);
    468 	}
    469 	cnt = 0;
    470 #endif
    471 	LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list, sctp_nxt_addr) {
    472 		if (laddr->ifa == NULL) {
    473 #ifdef SCTP_DEBUG
    474 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
    475 				printf("Help I have fallen and I can't get up!\n");
    476 			}
    477 #endif
    478 			continue;
    479 		}
    480 #ifdef SCTP_DEBUG
    481 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
    482 			cnt++;
    483 			printf("Restricted address[%d]:", cnt);
    484 			sctp_print_address(laddr->ifa->ifa_addr);
    485 		}
    486 #endif
    487 		if (sctp_cmpaddr(addr, laddr->ifa->ifa_addr) == 1) {
    488 			/* Yes it is on the list */
    489 			return (1);
    490 		}
    491 	}
    492 	return (0);
    493 }
    494 
    495 static int
    496 sctp_is_addr_in_ep(struct sctp_inpcb *inp, struct ifaddr *ifa)
    497 {
    498 	struct sctp_laddr *laddr;
    499 
    500 	if (ifa == NULL)
    501 		return (0);
    502 	LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
    503 		if (laddr->ifa == NULL) {
    504 #ifdef SCTP_DEBUG
    505 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
    506 				printf("Help I have fallen and I can't get up!\n");
    507 			}
    508 #endif
    509 			continue;
    510 		}
    511 		if (laddr->ifa->ifa_addr == NULL)
    512 			continue;
    513 		if (laddr->ifa == ifa)
    514 			/* same pointer */
    515 			return (1);
    516 		if (laddr->ifa->ifa_addr->sa_family != ifa->ifa_addr->sa_family) {
    517 			/* skip non compatible address comparison */
    518 			continue;
    519 		}
    520 		if (sctp_cmpaddr(ifa->ifa_addr, laddr->ifa->ifa_addr) == 1) {
    521 			/* Yes it is restricted */
    522 			return (1);
    523 		}
    524 	}
    525 	return (0);
    526 }
    527 
    528 
    529 
    530 static struct in_addr
    531 sctp_choose_v4_boundspecific_inp(struct sctp_inpcb *inp,
    532 				 struct rtentry *rt,
    533 				 uint8_t ipv4_scope,
    534 				 uint8_t loopscope)
    535 {
    536 	struct in_addr ans;
    537 	struct sctp_laddr *laddr;
    538 	struct sockaddr_in *sin;
    539 	struct ifnet *ifn;
    540 	struct ifaddr *ifa;
    541 	uint8_t sin_loop, sin_local;
    542 
    543 	/* first question, is the ifn we will emit on
    544 	 * in our list, if so, we want that one.
    545 	 */
    546 	ifn = rt->rt_ifp;
    547 	if (ifn) {
    548 		/* is a prefered one on the interface we route out? */
    549 		IFADDR_READER_FOREACH(ifa, ifn) {
    550 			sin = sctp_is_v4_ifa_addr_prefered (ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
    551 			if (sin == NULL)
    552 				continue;
    553 			if (sctp_is_addr_in_ep(inp, ifa)) {
    554 				return (sin->sin_addr);
    555 			}
    556 		}
    557 		/* is an acceptable one on the interface we route out? */
    558 		IFADDR_READER_FOREACH(ifa, ifn) {
    559 			sin = sctp_is_v4_ifa_addr_acceptable (ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
    560 			if (sin == NULL)
    561 				continue;
    562 			if (sctp_is_addr_in_ep(inp, ifa)) {
    563 				return (sin->sin_addr);
    564 			}
    565 		}
    566 	}
    567 	/* ok, what about a prefered address in the inp */
    568 	for (laddr = LIST_FIRST(&inp->sctp_addr_list);
    569 	     laddr && (laddr != inp->next_addr_touse);
    570 	     laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
    571 		if (laddr->ifa == NULL) {
    572 			/* address has been removed */
    573 			continue;
    574 		}
    575 		sin = sctp_is_v4_ifa_addr_prefered (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
    576 		if (sin == NULL)
    577 			continue;
    578 		return (sin->sin_addr);
    579 
    580 	}
    581 	/* ok, what about an acceptable address in the inp */
    582 	for (laddr = LIST_FIRST(&inp->sctp_addr_list);
    583 	     laddr && (laddr != inp->next_addr_touse);
    584 	     laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
    585 		if (laddr->ifa == NULL) {
    586 			/* address has been removed */
    587 			continue;
    588 		}
    589 		sin = sctp_is_v4_ifa_addr_acceptable (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
    590 		if (sin == NULL)
    591 			continue;
    592 		return (sin->sin_addr);
    593 
    594 	}
    595 
    596 	/* no address bound can be a source for the destination we are in trouble */
    597 #ifdef SCTP_DEBUG
    598 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
    599 		printf("Src address selection for EP, no acceptable src address found for address\n");
    600 	}
    601 #endif
    602 	memset(&ans, 0, sizeof(ans));
    603 	return (ans);
    604 }
    605 
    606 
    607 
    608 static struct in_addr
    609 sctp_choose_v4_boundspecific_stcb(struct sctp_inpcb *inp,
    610 				  struct sctp_tcb *stcb,
    611 				  struct sctp_nets *net,
    612 				  struct rtentry *rt,
    613  			          uint8_t ipv4_scope,
    614 				  uint8_t loopscope,
    615 				  int non_asoc_addr_ok)
    616 {
    617 	/*
    618 	 * Here we have two cases, bound all asconf
    619 	 * allowed. bound all asconf not allowed.
    620 	 *
    621 	 */
    622 	struct sctp_laddr *laddr, *starting_point;
    623 	struct in_addr ans;
    624 	struct ifnet *ifn;
    625 	struct ifaddr *ifa;
    626 	uint8_t sin_loop, sin_local, start_at_beginning=0;
    627 	struct sockaddr_in *sin;
    628 
    629 	/* first question, is the ifn we will emit on
    630 	 * in our list, if so, we want that one.
    631 	 */
    632 	ifn = rt->rt_ifp;
    633 
    634  	if (inp->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) {
    635 		/*
    636 		 * Here we use the list of addresses on the endpoint. Then
    637 		 * the addresses listed on the "restricted" list is just that,
    638 		 * address that have not been added and can't be used (unless
    639 		 * the non_asoc_addr_ok is set).
    640 		 */
    641 #ifdef SCTP_DEBUG
    642 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
    643 			printf("Have a STCB - asconf allowed, not bound all have a netgative list\n");
    644 		}
    645 #endif
    646 		/* first question, is the ifn we will emit on
    647 		 * in our list, if so, we want that one.
    648 		 */
    649 		if (ifn) {
    650 			/* first try for an prefered address on the ep */
    651 			IFADDR_READER_FOREACH(ifa, ifn) {
    652 				if (sctp_is_addr_in_ep(inp, ifa)) {
    653 					sin = sctp_is_v4_ifa_addr_prefered (ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
    654 					if (sin == NULL)
    655 						continue;
    656 					if ((non_asoc_addr_ok == 0) &&
    657 					    (sctp_is_addr_restricted(stcb, (struct sockaddr *)sin))) {
    658 						/* on the no-no list */
    659 						continue;
    660 					}
    661 					return (sin->sin_addr);
    662 				}
    663 			}
    664 			/* next try for an acceptable address on the ep */
    665 			IFADDR_READER_FOREACH(ifa, ifn) {
    666 				if (sctp_is_addr_in_ep(inp, ifa)) {
    667 					sin = sctp_is_v4_ifa_addr_acceptable (ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
    668 					if (sin == NULL)
    669 						continue;
    670 					if ((non_asoc_addr_ok == 0) &&
    671 					    (sctp_is_addr_restricted(stcb, (struct sockaddr *)sin))) {
    672 						/* on the no-no list */
    673 						continue;
    674 					}
    675 					return (sin->sin_addr);
    676 				}
    677 			}
    678 
    679 		}
    680 		/* if we can't find one like that then we must
    681 		 * look at all addresses bound to pick one at
    682 		 * first prefereable then secondly acceptable.
    683 		 */
    684 		starting_point = stcb->asoc.last_used_address;
    685 	sctpv4_from_the_top:
    686 		if (stcb->asoc.last_used_address == NULL) {
    687 			start_at_beginning=1;
    688 			stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
    689 		}
    690 		/* search beginning with the last used address */
    691 		for (laddr = stcb->asoc.last_used_address; laddr;
    692 		     laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
    693 			if (laddr->ifa == NULL) {
    694 				/* address has been removed */
    695 				continue;
    696 			}
    697 			sin = sctp_is_v4_ifa_addr_prefered (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
    698 			if (sin == NULL)
    699 				continue;
    700 			if ((non_asoc_addr_ok == 0) &&
    701 			    (sctp_is_addr_restricted(stcb, (struct sockaddr *)sin))) {
    702 				/* on the no-no list */
    703 				continue;
    704 			}
    705 			return (sin->sin_addr);
    706 
    707 		}
    708 		if (start_at_beginning == 0) {
    709 			stcb->asoc.last_used_address = NULL;
    710 			goto sctpv4_from_the_top;
    711 		}
    712 		/* now try for any higher scope than the destination */
    713 		stcb->asoc.last_used_address = starting_point;
    714 		start_at_beginning = 0;
    715 	sctpv4_from_the_top2:
    716 		if (stcb->asoc.last_used_address == NULL) {
    717 			start_at_beginning=1;
    718 			stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
    719 		}
    720 		/* search beginning with the last used address */
    721 		for (laddr = stcb->asoc.last_used_address; laddr;
    722 		     laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
    723 			if (laddr->ifa == NULL) {
    724 				/* address has been removed */
    725 				continue;
    726 			}
    727 			sin = sctp_is_v4_ifa_addr_acceptable (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
    728 			if (sin == NULL)
    729 				continue;
    730 			if ((non_asoc_addr_ok == 0) &&
    731 			    (sctp_is_addr_restricted(stcb, (struct sockaddr *)sin))) {
    732 				/* on the no-no list */
    733 				continue;
    734 			}
    735 			return (sin->sin_addr);
    736 		}
    737 		if (start_at_beginning == 0) {
    738 			stcb->asoc.last_used_address = NULL;
    739 			goto sctpv4_from_the_top2;
    740 		}
    741 	} else {
    742 		/*
    743 		 * Here we have an address list on the association, thats the
    744 		 * only valid source addresses that we can use.
    745 		 */
    746 #ifdef SCTP_DEBUG
    747 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
    748 			printf("Have a STCB - no asconf allowed, not bound all have a postive list\n");
    749 		}
    750 #endif
    751 		/* First look at all addresses for one that is on
    752 		 * the interface we route out
    753 		 */
    754 		LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
    755 			     sctp_nxt_addr) {
    756 			if (laddr->ifa == NULL) {
    757 				/* address has been removed */
    758 				continue;
    759 			}
    760 			sin = sctp_is_v4_ifa_addr_prefered (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
    761 			if (sin == NULL)
    762 				continue;
    763 			/* first question, is laddr->ifa an address associated with the emit interface */
    764 			if (ifn) {
    765 				IFADDR_READER_FOREACH(ifa, ifn) {
    766 					if (laddr->ifa == ifa) {
    767 						sin = (struct sockaddr_in *)laddr->ifa->ifa_addr;
    768 						return (sin->sin_addr);
    769 					}
    770 					if (sctp_cmpaddr(ifa->ifa_addr, laddr->ifa->ifa_addr) == 1) {
    771 						sin = (struct sockaddr_in *)laddr->ifa->ifa_addr;
    772 						return (sin->sin_addr);
    773 					}
    774 				}
    775 			}
    776 		}
    777 		/* what about an acceptable one on the interface? */
    778 		LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
    779 			     sctp_nxt_addr) {
    780 			if (laddr->ifa == NULL) {
    781 				/* address has been removed */
    782 				continue;
    783 			}
    784 			sin = sctp_is_v4_ifa_addr_acceptable (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
    785 			if (sin == NULL)
    786 				continue;
    787 			/* first question, is laddr->ifa an address associated with the emit interface */
    788 			if (ifn) {
    789 				IFADDR_READER_FOREACH(ifa, ifn) {
    790 					if (laddr->ifa == ifa) {
    791 						sin = (struct sockaddr_in *)laddr->ifa->ifa_addr;
    792 						return (sin->sin_addr);
    793 					}
    794 					if (sctp_cmpaddr(ifa->ifa_addr, laddr->ifa->ifa_addr) == 1) {
    795 						sin = (struct sockaddr_in *)laddr->ifa->ifa_addr;
    796 						return (sin->sin_addr);
    797 					}
    798 				}
    799 			}
    800 		}
    801 		/* ok, next one that is preferable in general */
    802 		LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
    803 			     sctp_nxt_addr) {
    804 			if (laddr->ifa == NULL) {
    805 				/* address has been removed */
    806 				continue;
    807 			}
    808 			sin = sctp_is_v4_ifa_addr_prefered (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
    809 			if (sin == NULL)
    810 				continue;
    811 			return (sin->sin_addr);
    812 		}
    813 
    814 		/* last, what about one that is acceptable */
    815 		LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
    816 			     sctp_nxt_addr) {
    817 			if (laddr->ifa == NULL) {
    818 				/* address has been removed */
    819 				continue;
    820 			}
    821 			sin = sctp_is_v4_ifa_addr_acceptable (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
    822 			if (sin == NULL)
    823 				continue;
    824 			return (sin->sin_addr);
    825 		}
    826 	}
    827 	memset(&ans, 0, sizeof(ans));
    828 	return (ans);
    829 }
    830 
    831 static struct sockaddr_in *
    832 sctp_select_v4_nth_prefered_addr_from_ifn_boundall (struct ifnet *ifn, struct sctp_tcb *stcb, int non_asoc_addr_ok,
    833 						    uint8_t loopscope, uint8_t ipv4_scope, int cur_addr_num)
    834 {
    835 	struct ifaddr *ifa;
    836 	struct sockaddr_in *sin;
    837 	uint8_t sin_loop, sin_local;
    838 	int num_eligible_addr = 0;
    839 	IFADDR_READER_FOREACH(ifa, ifn) {
    840 		sin = sctp_is_v4_ifa_addr_prefered (ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
    841 		if (sin == NULL)
    842 			continue;
    843 		if (stcb) {
    844 			if ((non_asoc_addr_ok == 0) && sctp_is_addr_restricted(stcb, (struct sockaddr *)sin)) {
    845 				/* It is restricted for some reason.. probably
    846 				 * not yet added.
    847 				 */
    848 				continue;
    849 			}
    850 		}
    851 		if (cur_addr_num == num_eligible_addr) {
    852 			return (sin);
    853 		}
    854 	}
    855 	return (NULL);
    856 }
    857 
    858 
    859 static int
    860 sctp_count_v4_num_prefered_boundall (struct ifnet *ifn, struct sctp_tcb *stcb, int non_asoc_addr_ok,
    861 				     uint8_t loopscope, uint8_t ipv4_scope, uint8_t *sin_loop, uint8_t *sin_local)
    862 {
    863 	struct ifaddr *ifa;
    864 	struct sockaddr_in *sin;
    865 	int num_eligible_addr = 0;
    866 
    867 	IFADDR_READER_FOREACH(ifa, ifn) {
    868 		sin = sctp_is_v4_ifa_addr_prefered (ifa, loopscope, ipv4_scope, sin_loop, sin_local);
    869 		if (sin == NULL)
    870 			continue;
    871 		if (stcb) {
    872 			if ((non_asoc_addr_ok == 0) && sctp_is_addr_restricted(stcb, (struct sockaddr *)sin)) {
    873 				/* It is restricted for some reason.. probably
    874 				 * not yet added.
    875 				 */
    876 				continue;
    877 			}
    878 		}
    879 		num_eligible_addr++;
    880 	}
    881 	return (num_eligible_addr);
    882 
    883 }
    884 
    885 static struct in_addr
    886 sctp_choose_v4_boundall(struct sctp_inpcb *inp,
    887 			struct sctp_tcb *stcb,
    888 			struct sctp_nets *net,
    889 			struct rtentry *rt,
    890 			uint8_t ipv4_scope,
    891 			uint8_t loopscope,
    892 			int non_asoc_addr_ok)
    893 {
    894 	int cur_addr_num=0, num_prefered=0;
    895 	uint8_t sin_loop, sin_local;
    896 	struct ifnet *ifn;
    897 	struct sockaddr_in *sin;
    898 	struct in_addr ans;
    899 	struct ifaddr *ifa;
    900 	int s;
    901 	/*
    902 	 * For v4 we can use (in boundall) any address in the association. If
    903 	 * non_asoc_addr_ok is set we can use any address (at least in theory).
    904 	 * So we look for prefered addresses first. If we find one, we use it.
    905 	 * Otherwise we next try to get an address on the interface, which we
    906 	 * should be able to do (unless non_asoc_addr_ok is false and we are
    907 	 * routed out that way). In these cases where we can't use the address
    908 	 * of the interface we go through all the ifn's looking for an address
    909 	 * we can use and fill that in. Punting means we send back address
    910 	 * 0, which will probably cause problems actually since then IP will
    911 	 * fill in the address of the route ifn, which means we probably already
    912 	 * rejected it.. i.e. here comes an abort :-<.
    913 	 */
    914 	ifn = rt->rt_ifp;
    915 	if (net) {
    916 		cur_addr_num = net->indx_of_eligible_next_to_use;
    917 	}
    918 	if (ifn == NULL) {
    919  		goto bound_all_v4_plan_c;
    920 	}
    921 	num_prefered = sctp_count_v4_num_prefered_boundall (ifn, stcb, non_asoc_addr_ok, loopscope, ipv4_scope, &sin_loop, &sin_local);
    922 #ifdef SCTP_DEBUG
    923 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
    924 		printf("Found %d prefered source addresses\n", num_prefered);
    925 	}
    926 #endif
    927 	if (num_prefered == 0) {
    928 		/* no eligible addresses, we must use some other
    929 		 * interface address if we can find one.
    930 		 */
    931  		goto bound_all_v4_plan_b;
    932 	}
    933 	/* Ok we have num_eligible_addr set with how many we can use,
    934 	 * this may vary from call to call due to addresses being deprecated etc..
    935 	 */
    936 	if (cur_addr_num >= num_prefered) {
    937 		cur_addr_num = 0;
    938 	}
    939 	/* select the nth address from the list (where cur_addr_num is the nth) and
    940 	 * 0 is the first one, 1 is the second one etc...
    941 	 */
    942 #ifdef SCTP_DEBUG
    943 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
    944 		printf("cur_addr_num:%d\n", cur_addr_num);
    945 	}
    946 #endif
    947 	sin = sctp_select_v4_nth_prefered_addr_from_ifn_boundall (ifn, stcb, non_asoc_addr_ok, loopscope,
    948 								   ipv4_scope, cur_addr_num);
    949 
    950 	/* if sin is NULL something changed??, plan_a now */
    951 	if (sin) {
    952 		return (sin->sin_addr);
    953 	}
    954 
    955 	/*
    956 	 * plan_b: Look at the interface that we emit on
    957 	 *         and see if we can find an acceptable address.
    958 	 */
    959  bound_all_v4_plan_b:
    960 	IFADDR_READER_FOREACH(ifa, ifn) {
    961 		sin = sctp_is_v4_ifa_addr_acceptable (ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
    962 		if (sin == NULL)
    963 			continue;
    964 		if (stcb) {
    965 			if ((non_asoc_addr_ok == 0) && sctp_is_addr_restricted(stcb, (struct sockaddr *)sin)) {
    966 				/* It is restricted for some reason.. probably
    967 				 * not yet added.
    968 				 */
    969 				continue;
    970 			}
    971 		}
    972 		return (sin->sin_addr);
    973 	}
    974 	/*
    975 	 * plan_c: Look at all interfaces and find a prefered
    976 	 *         address. If we reache here we are in trouble I think.
    977 	 */
    978  bound_all_v4_plan_c:
    979 	s = pserialize_read_enter();
    980 	IFNET_READER_FOREACH(ifn) {
    981 		if (ifn == inp->next_ifn_touse)
    982 			break;
    983 		if (loopscope == 0 && ifn->if_type == IFT_LOOP) {
    984 			/* wrong base scope */
    985 			continue;
    986 		}
    987 		if (ifn == rt->rt_ifp)
    988 			/* already looked at this guy */
    989 			continue;
    990 		num_prefered = sctp_count_v4_num_prefered_boundall (ifn, stcb, non_asoc_addr_ok,
    991 								    loopscope, ipv4_scope, &sin_loop, &sin_local);
    992 #ifdef SCTP_DEBUG
    993 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
    994 			printf("Found ifn:%p %d prefered source addresses\n", ifn, num_prefered);
    995 		}
    996 #endif
    997 		if (num_prefered == 0) {
    998 			/*
    999 			 * None on this interface.
   1000 			 */
   1001 			continue;
   1002 		}
   1003 		/* Ok we have num_eligible_addr set with how many we can use,
   1004 		 * this may vary from call to call due to addresses being deprecated etc..
   1005 		 */
   1006 		if (cur_addr_num >= num_prefered) {
   1007 			cur_addr_num = 0;
   1008 		}
   1009 		sin = sctp_select_v4_nth_prefered_addr_from_ifn_boundall (ifn, stcb, non_asoc_addr_ok, loopscope,
   1010 									  ipv4_scope, cur_addr_num);
   1011 		if (sin == NULL)
   1012 			continue;
   1013 		pserialize_read_exit(s);
   1014 		return (sin->sin_addr);
   1015 
   1016 	}
   1017 	pserialize_read_exit(s);
   1018 
   1019 	/*
   1020 	 * plan_d: We are in deep trouble. No prefered address on
   1021 	 *         any interface. And the emit interface does not
   1022 	 *         even have an acceptable address. Take anything
   1023 	 *         we can get! If this does not work we are
   1024 	 *         probably going to emit a packet that will
   1025 	 *         illicit an ABORT, falling through.
   1026 	 */
   1027 
   1028 	s = pserialize_read_enter();
   1029 	IFNET_READER_FOREACH(ifn) {
   1030 		if (ifn == inp->next_ifn_touse)
   1031 			break;
   1032 		if (loopscope == 0 && ifn->if_type == IFT_LOOP) {
   1033 			/* wrong base scope */
   1034 			continue;
   1035 		}
   1036 		if (ifn == rt->rt_ifp)
   1037 			/* already looked at this guy */
   1038 			continue;
   1039 
   1040 		IFADDR_READER_FOREACH(ifa, ifn) {
   1041 			sin = sctp_is_v4_ifa_addr_acceptable (ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
   1042 			if (sin == NULL)
   1043 				continue;
   1044 			if (stcb) {
   1045 				if ((non_asoc_addr_ok == 0) && sctp_is_addr_restricted(stcb, (struct sockaddr *)sin)) {
   1046 					/* It is restricted for some reason.. probably
   1047 					 * not yet added.
   1048 					 */
   1049 					continue;
   1050 				}
   1051 			}
   1052 			pserialize_read_exit(s);
   1053 			return (sin->sin_addr);
   1054 		}
   1055 	}
   1056 	pserialize_read_exit(s);
   1057 	/*
   1058 	 * Ok we can find NO address to source from that is
   1059 	 * not on our negative list. It is either the special
   1060 	 * ASCONF case where we are sourceing from a intf that
   1061 	 * has been ifconfig'd to a different address (i.e.
   1062 	 * it holds a ADD/DEL/SET-PRIM and the proper lookup
   1063 	 * address. OR we are hosed, and this baby is going
   1064 	 * to abort the association.
   1065 	 */
   1066 	if (non_asoc_addr_ok) {
   1067 		return (((struct sockaddr_in *)(rt->rt_ifa->ifa_addr))->sin_addr);
   1068 	} else {
   1069 		memset(&ans, 0, sizeof(ans));
   1070 		return (ans);
   1071 	}
   1072 }
   1073 
   1074 
   1075 
   1076 /* tcb may be NULL */
   1077 struct in_addr
   1078 sctp_ipv4_source_address_selection(struct sctp_inpcb *inp,
   1079     struct sctp_tcb *stcb, struct route *ro, struct sctp_nets *net,
   1080     int non_asoc_addr_ok)
   1081 {
   1082 	struct in_addr ans;
   1083 	const struct sockaddr_in *to;
   1084 	struct rtentry *rt;
   1085 	uint8_t ipv4_scope, loopscope;
   1086 
   1087 	/*
   1088 	 * Rules:
   1089 	 * - Find the route if needed, cache if I can.
   1090 	 * - Look at interface address in route, Is it
   1091 	 *   in the bound list. If so we have the best source.
   1092 	 * - If not we must rotate amongst the addresses.
   1093 	 *
   1094 	 * Cavets and issues
   1095 	 *
   1096 	 * Do we need to pay attention to scope. We can have
   1097 	 * a private address or a global address we are sourcing
   1098 	 * or sending to. So if we draw it out
   1099 	 *      source     *      dest   *  result
   1100 	 *  ------------------------------------------
   1101 	 *  a   Private    *     Global  *  NAT?
   1102 	 *  ------------------------------------------
   1103 	 *  b   Private    *     Private *  No problem
   1104 	 *  ------------------------------------------
   1105 	 *  c   Global     *     Private *  Huh, How will this work?
   1106 	 *  ------------------------------------------
   1107 	 *  d   Global     *     Global  *  No Problem
   1108 	 *  ------------------------------------------
   1109 	 *
   1110 	 * And then we add to that what happens if there are multiple
   1111 	 * addresses assigned to an interface. Remember the ifa on a
   1112 	 * ifn is a linked list of addresses. So one interface can
   1113 	 * have more than one IPv4 address. What happens if we
   1114 	 * have both a private and a global address? Do we then
   1115 	 * use context of destination to sort out which one is
   1116 	 * best? And what about NAT's sending P->G may get you
   1117 	 * a NAT translation, or should you select the G thats
   1118 	 * on the interface in preference.
   1119 	 *
   1120 	 * Decisions:
   1121 	 *
   1122 	 *  - count the number of addresses on the interface.
   1123 	 *  - if its one, no problem except case <c>. For <a>
   1124 	 *    we will assume a NAT out there.
   1125 	 *  - if there are more than one, then we need to worry
   1126 	 *    about scope P or G. We should prefer G -> G and
   1127 	 *    P -> P if possible. Then as a secondary fall back
   1128 	 *    to mixed types G->P being a last ditch one.
   1129 	 *  - The above all works for bound all, but bound
   1130 	 *    specific we need to use the same concept but instead
   1131 	 *    only consider the bound addresses. If the bound set
   1132 	 *    is NOT assigned to the interface then we must use
   1133 	 *    rotation amongst them.
   1134 	 *
   1135 	 * Notes: For v4, we can always punt and let ip_output
   1136 	 * decide by sending back a source of 0.0.0.0
   1137 	 */
   1138 
   1139 	/*
   1140 	 * Need a route to cache.
   1141 	 *
   1142 	 */
   1143 	rt = rtcache_validate(ro);
   1144 	if (rt == NULL) {
   1145 		/* No route to host .. punt */
   1146 		memset(&ans, 0, sizeof(ans));
   1147 		return (ans);
   1148 	} else {
   1149 		to = satocsin(rtcache_getdst(ro));
   1150 	}
   1151 	/* Setup our scopes */
   1152 	if (stcb) {
   1153 		ipv4_scope = stcb->asoc.ipv4_local_scope;
   1154 		loopscope = stcb->asoc.loopback_scope;
   1155 	} else {
   1156 		/* Scope based on outbound address */
   1157 		if ((IN4_ISPRIVATE_ADDRESS(&to->sin_addr))) {
   1158 			ipv4_scope = 1;
   1159 			loopscope = 0;
   1160 		} else if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
   1161 			ipv4_scope = 1;
   1162 			loopscope = 1;
   1163 		} else {
   1164 			ipv4_scope = 0;
   1165 			loopscope = 0;
   1166 		}
   1167 	}
   1168 #ifdef SCTP_DEBUG
   1169 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1170 		printf("Scope setup loop:%d ipv4_scope:%d\n",
   1171 		       loopscope, ipv4_scope);
   1172 	}
   1173 #endif
   1174 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
   1175 		/*
   1176 		 * When bound to all if the address list is set
   1177 		 * it is a negative list. Addresses being added
   1178 		 * by asconf.
   1179 		 */
   1180 		ans = sctp_choose_v4_boundall(inp, stcb, net, rt,
   1181 		    ipv4_scope, loopscope, non_asoc_addr_ok);
   1182 		goto out;
   1183         }
   1184 	/*
   1185  	 * Three possiblities here:
   1186 	 *
   1187 	 * a) stcb is NULL, which means we operate only from
   1188 	 *    the list of addresses (ifa's) bound to the assoc and
   1189 	 *    we care not about the list.
   1190 	 * b) stcb is NOT-NULL, which means we have an assoc structure and
   1191 	 *    auto-asconf is on. This means that the list of addresses is
   1192          *    a NOT list. We use the list from the inp, but any listed address
   1193 	 *    in our list is NOT yet added. However if the non_asoc_addr_ok is
   1194 	 *    set we CAN use an address NOT available (i.e. being added). Its
   1195 	 *    a negative list.
   1196 	 * c) stcb is NOT-NULL, which means we have an assoc structure and
   1197 	 *    auto-asconf is off. This means that the list of addresses is
   1198          *    the ONLY addresses I can use.. its positive.
   1199 	 *
   1200 	 *    Note we collapse b & c into the same function just like in
   1201 	 *    the v6 address selection.
   1202 	 */
   1203 	if (stcb) {
   1204 		ans = sctp_choose_v4_boundspecific_stcb(inp, stcb, net,
   1205 		    rt, ipv4_scope, loopscope, non_asoc_addr_ok);
   1206 		goto out;
   1207 	} else {
   1208 		ans = sctp_choose_v4_boundspecific_inp(inp, rt,
   1209 		    ipv4_scope, loopscope);
   1210 		goto out;
   1211 	}
   1212 	/* this should not be reached */
   1213 	memset(&ans, 0, sizeof(ans));
   1214 out:
   1215 	rtcache_unref(rt, ro);
   1216 	return ans;
   1217 }
   1218 
   1219 
   1220 
   1221 static struct sockaddr_in6 *
   1222 sctp_is_v6_ifa_addr_acceptable (struct ifaddr *ifa, int loopscope, int loc_scope, int *sin_loop, int *sin_local)
   1223 {
   1224 	struct in6_ifaddr *ifa6;
   1225 	struct sockaddr_in6 *sin6;
   1226 
   1227 	if (ifa->ifa_addr->sa_family != AF_INET6) {
   1228 		/* forget non-v6 */
   1229 		return (NULL);
   1230 	}
   1231 	ifa6 = (struct in6_ifaddr *)ifa;
   1232 	/* ok to use deprecated addresses? */
   1233 	if (!ip6_use_deprecated) {
   1234 		if (IFA6_IS_DEPRECATED(ifa6)) {
   1235 			/* can't use this type */
   1236 			return (NULL);
   1237 		}
   1238 	}
   1239 	/* are we ok, with the current state of this address? */
   1240 	if (ifa6->ia6_flags &
   1241 	    (IN6_IFF_DETACHED | IN6_IFF_NOTREADY | IN6_IFF_ANYCAST)) {
   1242 		/* Can't use these types */
   1243 		return (NULL);
   1244 	}
   1245 	/* Ok the address may be ok */
   1246 	sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
   1247 	*sin_local = *sin_loop = 0;
   1248 	if ((ifa->ifa_ifp->if_type == IFT_LOOP) ||
   1249 	    (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))) {
   1250 		*sin_loop = 1;
   1251 	}
   1252 	if (!loopscope && *sin_loop) {
   1253 		/* Its a loopback address and we don't have loop scope */
   1254 		return (NULL);
   1255 	}
   1256 	if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
   1257 		/* we skip unspecifed addresses */
   1258 		return (NULL);
   1259 	}
   1260 
   1261 	if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
   1262 		*sin_local = 1;
   1263 	}
   1264 	if (!loc_scope && *sin_local) {
   1265 		/* Its a link local address, and we don't have link local scope */
   1266 		return (NULL);
   1267 	}
   1268 	return (sin6);
   1269 }
   1270 
   1271 
   1272 static struct sockaddr_in6 *
   1273 sctp_choose_v6_boundspecific_stcb(struct sctp_inpcb *inp,
   1274 				  struct sctp_tcb *stcb,
   1275 				  struct sctp_nets *net,
   1276 				  struct rtentry *rt,
   1277  			          uint8_t loc_scope,
   1278 				  uint8_t loopscope,
   1279 				  int non_asoc_addr_ok)
   1280 {
   1281 	/*
   1282 	 *   Each endpoint has a list of local addresses associated
   1283 	 *   with it. The address list is either a "negative list" i.e.
   1284 	 *   those addresses that are NOT allowed to be used as a source OR
   1285 	 *   a "postive list" i.e. those addresses that CAN be used.
   1286 	 *
   1287 	 *   Its a negative list if asconf is allowed. What we do
   1288 	 *   in this case is use the ep address list BUT we have
   1289 	 *   to cross check it against the negative list.
   1290 	 *
   1291 	 *   In the case where NO asconf is allowed, we have just
   1292 	 *   a straight association level list that we must use to
   1293 	 *   find a source address.
   1294 	 */
   1295 	struct sctp_laddr *laddr, *starting_point;
   1296 	struct sockaddr_in6 *sin6;
   1297 	int sin_loop, sin_local;
   1298 	int start_at_beginning=0;
   1299 	struct ifnet *ifn;
   1300 	struct ifaddr *ifa;
   1301 
   1302 	ifn = rt->rt_ifp;
   1303 	if (inp->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) {
   1304 #ifdef SCTP_DEBUG
   1305 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1306 			printf("Have a STCB - asconf allowed, not bound all have a netgative list\n");
   1307 		}
   1308 #endif
   1309 		/* first question, is the ifn we will emit on
   1310 		 * in our list, if so, we want that one.
   1311 		 */
   1312 		if (ifn) {
   1313 			IFADDR_READER_FOREACH(ifa, ifn) {
   1314 				if (sctp_is_addr_in_ep(inp, ifa)) {
   1315 					sin6 = sctp_is_v6_ifa_addr_acceptable (ifa, loopscope, loc_scope, &sin_loop, &sin_local);
   1316 					if (sin6 == NULL)
   1317 						continue;
   1318 					if ((non_asoc_addr_ok == 0) &&
   1319 					    (sctp_is_addr_restricted(stcb, (struct sockaddr *)sin6))) {
   1320 						/* on the no-no list */
   1321 						continue;
   1322 					}
   1323 					return (sin6);
   1324 				}
   1325 			}
   1326 		}
   1327 		starting_point = stcb->asoc.last_used_address;
   1328 		/* First try for matching scope */
   1329 	sctp_from_the_top:
   1330 		if (stcb->asoc.last_used_address == NULL) {
   1331 			start_at_beginning=1;
   1332 			stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
   1333 		}
   1334 		/* search beginning with the last used address */
   1335 		for (laddr = stcb->asoc.last_used_address; laddr;
   1336 		     laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
   1337 			if (laddr->ifa == NULL) {
   1338 				/* address has been removed */
   1339 				continue;
   1340 			}
   1341 			sin6 = sctp_is_v6_ifa_addr_acceptable (laddr->ifa, loopscope, loc_scope, &sin_loop, &sin_local);
   1342 			if (sin6 == NULL)
   1343 				continue;
   1344 			if ((non_asoc_addr_ok == 0) && (sctp_is_addr_restricted(stcb, (struct sockaddr *)sin6))) {
   1345 				/* on the no-no list */
   1346 				continue;
   1347 			}
   1348 			/* is it of matching scope ? */
   1349 			if ((loopscope == 0) &&
   1350 			    (loc_scope == 0) &&
   1351 			    (sin_loop == 0) &&
   1352 			    (sin_local == 0)) {
   1353 				/* all of global scope we are ok with it */
   1354 				return (sin6);
   1355 			}
   1356 			if (loopscope && sin_loop)
   1357 				/* both on the loopback, thats ok */
   1358 				return (sin6);
   1359 			if (loc_scope && sin_local)
   1360 				/* both local scope */
   1361 				return (sin6);
   1362 
   1363 		}
   1364 		if (start_at_beginning == 0) {
   1365 			stcb->asoc.last_used_address = NULL;
   1366 			goto sctp_from_the_top;
   1367 		}
   1368 		/* now try for any higher scope than the destination */
   1369 		stcb->asoc.last_used_address = starting_point;
   1370 		start_at_beginning = 0;
   1371 	sctp_from_the_top2:
   1372 		if (stcb->asoc.last_used_address == NULL) {
   1373 			start_at_beginning=1;
   1374 			stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
   1375 		}
   1376 		/* search beginning with the last used address */
   1377 		for (laddr = stcb->asoc.last_used_address; laddr;
   1378 		     laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
   1379 			if (laddr->ifa == NULL) {
   1380 				/* address has been removed */
   1381 				continue;
   1382 			}
   1383 			sin6 = sctp_is_v6_ifa_addr_acceptable (laddr->ifa, loopscope, loc_scope, &sin_loop, &sin_local);
   1384 			if (sin6 == NULL)
   1385 				continue;
   1386 			if ((non_asoc_addr_ok == 0) && (sctp_is_addr_restricted(stcb, (struct sockaddr *)sin6))) {
   1387 				/* on the no-no list */
   1388 				continue;
   1389 			}
   1390 			return (sin6);
   1391 		}
   1392 		if (start_at_beginning == 0) {
   1393 			stcb->asoc.last_used_address = NULL;
   1394 			goto sctp_from_the_top2;
   1395 		}
   1396 	} else {
   1397 #ifdef SCTP_DEBUG
   1398 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1399 			printf("Have a STCB - no asconf allowed, not bound all have a postive list\n");
   1400 		}
   1401 #endif
   1402 		/* First try for interface output match */
   1403 		LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
   1404 			     sctp_nxt_addr) {
   1405 			if (laddr->ifa == NULL) {
   1406 				/* address has been removed */
   1407 				continue;
   1408 			}
   1409 			sin6 = sctp_is_v6_ifa_addr_acceptable (laddr->ifa, loopscope, loc_scope, &sin_loop, &sin_local);
   1410 			if (sin6 == NULL)
   1411 				continue;
   1412 			/* first question, is laddr->ifa an address associated with the emit interface */
   1413 			if (ifn) {
   1414 				IFADDR_READER_FOREACH(ifa, ifn) {
   1415 					if (laddr->ifa == ifa) {
   1416 						sin6 = (struct sockaddr_in6 *)laddr->ifa->ifa_addr;
   1417 						return (sin6);
   1418 					}
   1419 					if (sctp_cmpaddr(ifa->ifa_addr, laddr->ifa->ifa_addr) == 1) {
   1420 						sin6 = (struct sockaddr_in6 *)laddr->ifa->ifa_addr;
   1421 						return (sin6);
   1422 					}
   1423 				}
   1424 			}
   1425 		}
   1426 		/* Next try for matching scope */
   1427 		LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
   1428 			     sctp_nxt_addr) {
   1429 			if (laddr->ifa == NULL) {
   1430 				/* address has been removed */
   1431 				continue;
   1432 			}
   1433 			sin6 = sctp_is_v6_ifa_addr_acceptable (laddr->ifa, loopscope, loc_scope, &sin_loop, &sin_local);
   1434 			if (sin6 == NULL)
   1435 				continue;
   1436 
   1437 			if ((loopscope == 0) &&
   1438 			    (loc_scope == 0) &&
   1439 			    (sin_loop == 0) &&
   1440 			    (sin_local == 0)) {
   1441 				/* all of global scope we are ok with it */
   1442 				return (sin6);
   1443 			}
   1444 			if (loopscope && sin_loop)
   1445 				/* both on the loopback, thats ok */
   1446 				return (sin6);
   1447 			if (loc_scope && sin_local)
   1448 				/* both local scope */
   1449 				return (sin6);
   1450 		}
   1451 		/* ok, now try for a higher scope in the source address */
   1452 		/* First try for matching scope */
   1453 		LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
   1454 			     sctp_nxt_addr) {
   1455 			if (laddr->ifa == NULL) {
   1456 				/* address has been removed */
   1457 				continue;
   1458 			}
   1459 			sin6 = sctp_is_v6_ifa_addr_acceptable (laddr->ifa, loopscope, loc_scope, &sin_loop, &sin_local);
   1460 			if (sin6 == NULL)
   1461 				continue;
   1462 			return (sin6);
   1463 		}
   1464 	}
   1465 	return (NULL);
   1466 }
   1467 
   1468 static struct sockaddr_in6 *
   1469 sctp_choose_v6_boundspecific_inp(struct sctp_inpcb *inp,
   1470 				 struct rtentry *rt,
   1471 				 uint8_t loc_scope,
   1472 				 uint8_t loopscope)
   1473 {
   1474 	/*
   1475 	 * Here we are bound specific and have only
   1476 	 * an inp. We must find an address that is bound
   1477 	 * that we can give out as a src address. We
   1478 	 * prefer two addresses of same scope if we can
   1479 	 * find them that way.
   1480 	 */
   1481 	struct sctp_laddr *laddr;
   1482 	struct sockaddr_in6 *sin6;
   1483 	struct ifnet *ifn;
   1484 	struct ifaddr *ifa;
   1485 	int sin_loop, sin_local;
   1486 
   1487 	/* first question, is the ifn we will emit on
   1488 	 * in our list, if so, we want that one.
   1489 	 */
   1490 
   1491 	ifn = rt->rt_ifp;
   1492 	if (ifn) {
   1493 		IFADDR_READER_FOREACH(ifa, ifn) {
   1494 			sin6 = sctp_is_v6_ifa_addr_acceptable (ifa, loopscope, loc_scope, &sin_loop, &sin_local);
   1495 			if (sin6 == NULL)
   1496 				continue;
   1497 			if (sctp_is_addr_in_ep(inp, ifa)) {
   1498 				return (sin6);
   1499 			}
   1500 		}
   1501 	}
   1502 	for (laddr = LIST_FIRST(&inp->sctp_addr_list);
   1503 	     laddr && (laddr != inp->next_addr_touse);
   1504 	     laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
   1505 		if (laddr->ifa == NULL) {
   1506 			/* address has been removed */
   1507 			continue;
   1508 		}
   1509 		sin6 = sctp_is_v6_ifa_addr_acceptable (laddr->ifa, loopscope, loc_scope, &sin_loop, &sin_local);
   1510 		if (sin6 == NULL)
   1511 			continue;
   1512 
   1513 		if ((loopscope == 0) &&
   1514 		    (loc_scope == 0) &&
   1515 		    (sin_loop == 0) &&
   1516 		    (sin_local == 0)) {
   1517 			/* all of global scope we are ok with it */
   1518 			return (sin6);
   1519 		}
   1520 		if (loopscope && sin_loop)
   1521 			/* both on the loopback, thats ok */
   1522 			return (sin6);
   1523 		if (loc_scope && sin_local)
   1524 			/* both local scope */
   1525 			return (sin6);
   1526 
   1527 	}
   1528 	/* if we reach here, we could not find two addresses
   1529 	 * of the same scope to give out. Lets look for any higher level
   1530 	 * scope for a source address.
   1531 	 */
   1532 	for (laddr = LIST_FIRST(&inp->sctp_addr_list);
   1533 	     laddr && (laddr != inp->next_addr_touse);
   1534 	     laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
   1535 		if (laddr->ifa == NULL) {
   1536 			/* address has been removed */
   1537 			continue;
   1538 		}
   1539 		sin6 = sctp_is_v6_ifa_addr_acceptable (laddr->ifa, loopscope, loc_scope, &sin_loop, &sin_local);
   1540 		if (sin6 == NULL)
   1541 			continue;
   1542 		return (sin6);
   1543 	}
   1544 	/* no address bound can be a source for the destination */
   1545 #ifdef SCTP_DEBUG
   1546 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1547 		printf("Src address selection for EP, no acceptable src address found for address\n");
   1548 	}
   1549 #endif
   1550 	return (NULL);
   1551 }
   1552 
   1553 
   1554 static struct sockaddr_in6 *
   1555 sctp_select_v6_nth_addr_from_ifn_boundall (struct ifnet *ifn, struct sctp_tcb *stcb, int non_asoc_addr_ok, uint8_t loopscope,
   1556 					   uint8_t loc_scope, int cur_addr_num, int match_scope)
   1557 {
   1558 	struct ifaddr *ifa;
   1559 	struct sockaddr_in6 *sin6;
   1560 	int sin_loop, sin_local;
   1561 	int num_eligible_addr = 0;
   1562 
   1563 	IFADDR_READER_FOREACH(ifa, ifn) {
   1564 		sin6 = sctp_is_v6_ifa_addr_acceptable (ifa, loopscope, loc_scope, &sin_loop, &sin_local);
   1565 		if (sin6 == NULL)
   1566 			continue;
   1567 		if (stcb) {
   1568 			if ((non_asoc_addr_ok == 0) && sctp_is_addr_restricted(stcb, (struct sockaddr *)sin6)) {
   1569 				/* It is restricted for some reason.. probably
   1570 				 * not yet added.
   1571 				 */
   1572 				continue;
   1573 			}
   1574 		}
   1575 		if (match_scope) {
   1576 			/* Here we are asked to match scope if possible */
   1577 			if (loopscope && sin_loop)
   1578 				/* src and destination are loopback scope */
   1579 				return (sin6);
   1580 			if (loc_scope && sin_local)
   1581 				/* src and destination are local scope */
   1582 				return (sin6);
   1583 			if ((loopscope == 0) &&
   1584 			    (loc_scope == 0)  &&
   1585 			    (sin_loop == 0) &&
   1586 			    (sin_local == 0)) {
   1587 				/* src and destination are global scope */
   1588 				return (sin6);
   1589 			}
   1590 			continue;
   1591 		}
   1592 		if (num_eligible_addr == cur_addr_num) {
   1593 			/* this is it */
   1594 			return (sin6);
   1595 		}
   1596 		num_eligible_addr++;
   1597 	}
   1598 	return (NULL);
   1599 }
   1600 
   1601 
   1602 static int
   1603 sctp_count_v6_num_eligible_boundall (struct ifnet *ifn, struct sctp_tcb *stcb,
   1604 				     int non_asoc_addr_ok, uint8_t loopscope, uint8_t loc_scope)
   1605 {
   1606 	struct ifaddr *ifa;
   1607 	struct sockaddr_in6 *sin6;
   1608 	int num_eligible_addr = 0;
   1609 	int sin_loop, sin_local;
   1610 
   1611 	IFADDR_READER_FOREACH(ifa, ifn) {
   1612 		sin6 = sctp_is_v6_ifa_addr_acceptable (ifa, loopscope, loc_scope, &sin_loop, &sin_local);
   1613 		if (sin6 == NULL)
   1614 			continue;
   1615 		if (stcb) {
   1616 			if ((non_asoc_addr_ok == 0) && sctp_is_addr_restricted(stcb, (struct sockaddr *)sin6)) {
   1617 				/* It is restricted for some reason.. probably
   1618 				 * not yet added.
   1619 				 */
   1620 				continue;
   1621 			}
   1622 		}
   1623 		num_eligible_addr++;
   1624 	}
   1625 	return (num_eligible_addr);
   1626 }
   1627 
   1628 
   1629 static struct sockaddr_in6 *
   1630 sctp_choose_v6_boundall(struct sctp_inpcb *inp,
   1631 			struct sctp_tcb *stcb,
   1632 			struct sctp_nets *net,
   1633 			struct rtentry *rt,
   1634 			uint8_t loc_scope,
   1635 			uint8_t loopscope,
   1636 			int non_asoc_addr_ok)
   1637 {
   1638 	/* Ok, we are bound all SO any address
   1639 	 * is ok to use as long as it is NOT in the negative
   1640 	 * list.
   1641 	 */
   1642 	int num_eligible_addr;
   1643 	int cur_addr_num=0;
   1644 	int started_at_beginning=0;
   1645 	int match_scope_prefered;
   1646 	/* first question is, how many eligible addresses are
   1647 	 * there for the destination ifn that we are using that
   1648 	 * are within the proper scope?
   1649 	 */
   1650 	struct ifnet *ifn;
   1651 	struct sockaddr_in6 *sin6;
   1652 	int s;
   1653 
   1654 	ifn = rt->rt_ifp;
   1655 	if (net) {
   1656 		cur_addr_num = net->indx_of_eligible_next_to_use;
   1657 	}
   1658 	if (cur_addr_num == 0) {
   1659 		match_scope_prefered = 1;
   1660 	} else {
   1661 		match_scope_prefered = 0;
   1662 	}
   1663 	num_eligible_addr = sctp_count_v6_num_eligible_boundall (ifn, stcb, non_asoc_addr_ok, loopscope, loc_scope);
   1664 #ifdef SCTP_DEBUG
   1665 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1666 		printf("Found %d eligible source addresses\n", num_eligible_addr);
   1667 	}
   1668 #endif
   1669 	if (num_eligible_addr == 0) {
   1670 		/* no eligible addresses, we must use some other
   1671 		 * interface address if we can find one.
   1672 		 */
   1673  		goto bound_all_v6_plan_b;
   1674 	}
   1675 	/* Ok we have num_eligible_addr set with how many we can use,
   1676 	 * this may vary from call to call due to addresses being deprecated etc..
   1677 	 */
   1678 	if (cur_addr_num >= num_eligible_addr) {
   1679 		cur_addr_num = 0;
   1680 	}
   1681 	/* select the nth address from the list (where cur_addr_num is the nth) and
   1682 	 * 0 is the first one, 1 is the second one etc...
   1683 	 */
   1684 #ifdef SCTP_DEBUG
   1685 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1686 		printf("cur_addr_num:%d match_scope_prefered:%d select it\n",
   1687 		       cur_addr_num, match_scope_prefered);
   1688 	}
   1689 #endif
   1690 	sin6 = sctp_select_v6_nth_addr_from_ifn_boundall (ifn, stcb, non_asoc_addr_ok, loopscope,
   1691 							  loc_scope, cur_addr_num, match_scope_prefered);
   1692 	if (match_scope_prefered && (sin6 == NULL)) {
   1693 		/* retry without the preference for matching scope */
   1694 #ifdef SCTP_DEBUG
   1695 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1696 		printf("retry with no match_scope_prefered\n");
   1697 	}
   1698 #endif
   1699 		sin6 = sctp_select_v6_nth_addr_from_ifn_boundall (ifn, stcb, non_asoc_addr_ok, loopscope,
   1700 								  loc_scope, cur_addr_num, 0);
   1701 	}
   1702 	if (sin6) {
   1703 #ifdef SCTP_DEBUG
   1704 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1705 			printf("Selected address %d ifn:%p for the route\n", cur_addr_num, ifn);
   1706 		}
   1707 #endif
   1708 		if (net) {
   1709 			/* store so we get the next one */
   1710 			if (cur_addr_num < 255)
   1711 				net->indx_of_eligible_next_to_use = cur_addr_num + 1;
   1712 			else
   1713 				net->indx_of_eligible_next_to_use = 0;
   1714 		}
   1715 		return (sin6);
   1716 	}
   1717 	num_eligible_addr = 0;
   1718  bound_all_v6_plan_b:
   1719 	/* ok, if we reach here we either fell through
   1720 	 * due to something changing during an interupt (unlikely)
   1721 	 * or we have NO eligible source addresses for the ifn
   1722 	 * of the route (most likely). We must look at all the other
   1723 	 * interfaces EXCEPT rt->rt_ifp and do the same game.
   1724 	 */
   1725 #ifdef SCTP_DEBUG
   1726 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1727 		printf("bound-all Plan B\n");
   1728 	}
   1729 #endif
   1730 	if (inp->next_ifn_touse == NULL) {
   1731 		started_at_beginning=1;
   1732 		inp->next_ifn_touse = IFNET_READER_FIRST();
   1733 #ifdef SCTP_DEBUG
   1734 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1735 			printf("Start at first IFN:%p\n", inp->next_ifn_touse);
   1736 		}
   1737 #endif
   1738 	} else {
   1739 		inp->next_ifn_touse = IFNET_READER_NEXT(inp->next_ifn_touse);
   1740 #ifdef SCTP_DEBUG
   1741 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1742 			printf("Resume at IFN:%p\n", inp->next_ifn_touse);
   1743 		}
   1744 #endif
   1745 		if (inp->next_ifn_touse == NULL) {
   1746 #ifdef SCTP_DEBUG
   1747 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1748 				printf("IFN Resets\n");
   1749 			}
   1750 #endif
   1751 			started_at_beginning=1;
   1752 			inp->next_ifn_touse = IFNET_READER_FIRST();
   1753 		}
   1754 	}
   1755 
   1756 	s = pserialize_read_enter();
   1757 	IFNET_READER_FOREACH(ifn) {
   1758 		if (loopscope == 0 && ifn->if_type == IFT_LOOP) {
   1759 			/* wrong base scope */
   1760 			continue;
   1761 		}
   1762 		if (loc_scope && (ifn->if_index != loc_scope)) {
   1763 			/* by definition the scope (from to->sin6_scopeid)
   1764 			 * must match that of the interface. If not then
   1765 			 * we could pick a wrong scope for the address.
   1766 			 * Ususally we don't hit plan-b since the route
   1767 			 * handles this. However we can hit plan-b when
   1768 			 * we send to local-host so the route is the
   1769 			 * loopback interface, but the destination is a
   1770 			 * link local.
   1771 			 */
   1772 			continue;
   1773 		}
   1774 		if (ifn == rt->rt_ifp) {
   1775 			/* already looked at this guy */
   1776 			continue;
   1777 		}
   1778 		/* Address rotation will only work when we are not
   1779 		 * rotating sourced interfaces and are using the interface
   1780 		 * of the route. We would need to have a per interface index
   1781 		 * in order to do proper rotation.
   1782 		 */
   1783 		num_eligible_addr = sctp_count_v6_num_eligible_boundall (ifn, stcb, non_asoc_addr_ok, loopscope, loc_scope);
   1784 #ifdef SCTP_DEBUG
   1785 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1786 			printf("IFN:%p has %d eligible\n", ifn, num_eligible_addr);
   1787 		}
   1788 #endif
   1789 		if (num_eligible_addr == 0) {
   1790 			/* none we can use */
   1791 			continue;
   1792 		}
   1793 		/* Ok we have num_eligible_addr set with how many we can use,
   1794 		 * this may vary from call to call due to addresses being deprecated etc..
   1795 		 */
   1796 		inp->next_ifn_touse = ifn;
   1797 
   1798 		/* select the first one we can find with perference for matching scope.
   1799 		 */
   1800 		sin6 = sctp_select_v6_nth_addr_from_ifn_boundall (ifn, stcb, non_asoc_addr_ok, loopscope, loc_scope, 0, 1);
   1801 		if (sin6 == NULL) {
   1802 			/* can't find one with matching scope how about a source with higher
   1803 			 * scope
   1804 			 */
   1805  			sin6 = sctp_select_v6_nth_addr_from_ifn_boundall (ifn, stcb, non_asoc_addr_ok, loopscope, loc_scope, 0, 0);
   1806 			if (sin6 == NULL)
   1807 				/* Hmm, can't find one in the interface now */
   1808 				continue;
   1809 		}
   1810 #ifdef SCTP_DEBUG
   1811 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1812 			printf("Selected the %d'th address of ifn:%p\n",
   1813 			       cur_addr_num, ifn);
   1814 		}
   1815 #endif
   1816 		pserialize_read_exit(s);
   1817 		return (sin6);
   1818 	}
   1819 	pserialize_read_exit(s);
   1820 
   1821 	if (started_at_beginning == 0) {
   1822 		/* we have not been through all of them yet, force
   1823 		 * us to go through them all.
   1824 		 */
   1825 #ifdef SCTP_DEBUG
   1826 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1827 			printf("Force a recycle\n");
   1828 		}
   1829 #endif
   1830 		inp->next_ifn_touse = NULL;
   1831 		goto bound_all_v6_plan_b;
   1832 	}
   1833 	return (NULL);
   1834 
   1835 }
   1836 
   1837 /* stcb and net may be NULL */
   1838 struct in6_addr
   1839 sctp_ipv6_source_address_selection(struct sctp_inpcb *inp,
   1840     struct sctp_tcb *stcb, struct route *ro, struct sctp_nets *net,
   1841     int non_asoc_addr_ok)
   1842 {
   1843 	struct in6_addr ans;
   1844 	struct sockaddr_in6 *rt_addr;
   1845 	uint8_t loc_scope, loopscope;
   1846 	struct sockaddr_in6 to;
   1847 	struct rtentry *rt;
   1848 
   1849 	/*
   1850 	 * This routine is tricky standard v6 src address
   1851 	 * selection cannot take into account what we have
   1852 	 * bound etc, so we can't use it.
   1853 	 *
   1854 	 * Instead here is what we must do:
   1855 	 * 1) Make sure we have a route, if we
   1856 	 *    don't have a route we can never reach the peer.
   1857 	 * 2) Once we have a route, determine the scope of the
   1858 	 *     route. Link local, loopback or global.
   1859 	 * 3) Next we divide into three types. Either we
   1860 	 *    are bound all.. which means we want to use
   1861 	 *    one of the addresses of the interface we are
   1862 	 *    going out. <or>
   1863 	 * 4a) We have not stcb, which means we are using the
   1864 	 *    specific addresses bound on an inp, in this
   1865 	 *    case we are similar to the stcb case (4b below)
   1866 	 *    accept the list is always a positive list.<or>
   1867 	 * 4b) We are bound specific with a stcb, which means we have a
   1868 	 *    list of bound addresses and we must see if the
   1869 	 *    ifn of the route is actually one of the bound addresses.
   1870 	 *    If not, then we must rotate addresses amongst properly
   1871 	 *    scoped bound addresses, if so we use the address
   1872 	 *    of the interface.
   1873 	 * 5) Always, no matter which path we take through the above
   1874 	 *    we must be sure the source address we use is allowed to
   1875 	 *    be used. I.e.  IN6_IFF_DETACHED, IN6_IFF_NOTREADY, and IN6_IFF_ANYCAST
   1876 	 *    addresses cannot be used.
   1877 	 * 6) Addresses that are deprecated MAY be used
   1878 	 * 		if (!ip6_use_deprecated) {
   1879 	 *                    if (IFA6_IS_DEPRECATED(ifa6)) {
   1880 	 *	                  skip the address
   1881   	 *	              }
   1882 	 *	        }
   1883  	 */
   1884 
   1885 	/*** 1> determine route, if not already done */
   1886 	rt = rtcache_validate(ro);
   1887 	if (rt == NULL) {
   1888 		/*
   1889 		 * Need a route to cache.
   1890 		 */
   1891 		int scope_save;
   1892 
   1893 		memcpy(&to, rtcache_getdst(ro), sizeof(struct sockaddr));
   1894 		scope_save = to.sin6_scope_id;
   1895 		to.sin6_scope_id = 0;
   1896 
   1897 		rt = rtcache_lookup(ro, (struct sockaddr *)&to);
   1898 		to.sin6_scope_id = scope_save;
   1899 	}
   1900 	if (rt == NULL) {
   1901 		/*
   1902 		 * no route to host. this packet is going no-where.
   1903 		 * We probably should make sure we arrange to send back
   1904 		 * an error.
   1905 		 */
   1906 #ifdef SCTP_DEBUG
   1907 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1908 			printf("No route to host, this packet cannot be sent!\n");
   1909 		}
   1910 #endif
   1911 		memset(&ans, 0, sizeof(ans));
   1912 		return (ans);
   1913 	}
   1914 
   1915 	/*** 2a> determine scope for outbound address/route */
   1916 	loc_scope = loopscope = 0;
   1917 	/*
   1918 	 * We base our scope on the outbound packet scope and route,
   1919 	 * NOT the TCB (if there is one). This way in local scope we will only
   1920 	 * use a local scope src address when we send to a local address.
   1921 	 */
   1922 
   1923 	if (IN6_IS_ADDR_LOOPBACK(&to.sin6_addr)) {
   1924 		/* If the route goes to the loopback address OR
   1925 		 * the address is a loopback address, we are loopback
   1926 		 * scope.
   1927 		 */
   1928 #ifdef SCTP_DEBUG
   1929 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1930 			printf("Loopback scope is set\n");
   1931 		}
   1932 #endif
   1933 		loc_scope = 0;
   1934 		loopscope = 1;
   1935 		if (net != NULL) {
   1936 			/* mark it as local */
   1937 			net->addr_is_local = 1;
   1938 		}
   1939 
   1940 	} else if (IN6_IS_ADDR_LINKLOCAL(&to.sin6_addr)) {
   1941 #ifdef SCTP_DEBUG
   1942 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1943 			printf("Link local scope is set, id:%d\n", to.sin6_scope_id);
   1944 		}
   1945 #endif
   1946 		if (to.sin6_scope_id)
   1947 			loc_scope = to.sin6_scope_id;
   1948 		else {
   1949 			loc_scope = 1;
   1950 		}
   1951 		loopscope = 0;
   1952 	} else {
   1953 #ifdef SCTP_DEBUG
   1954 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1955 			printf("Global scope is set\n");
   1956 		}
   1957 #endif
   1958 	}
   1959 
   1960 	/* now, depending on which way we are bound we call the appropriate
   1961 	 * routine to do steps 3-6
   1962 	 */
   1963 #ifdef SCTP_DEBUG
   1964 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1965 		printf("Destination address:");
   1966 		sctp_print_address((struct sockaddr *)&to);
   1967 	}
   1968 #endif
   1969 
   1970 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
   1971 #ifdef SCTP_DEBUG
   1972 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1973 			printf("Calling bound-all src addr selection for v6\n");
   1974 		}
   1975 #endif
   1976 		rt_addr = sctp_choose_v6_boundall(inp, stcb, net, rt, loc_scope, loopscope, non_asoc_addr_ok);
   1977 	} else {
   1978 #ifdef SCTP_DEBUG
   1979 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1980 			printf("Calling bound-specific src addr selection for v6\n");
   1981 		}
   1982 #endif
   1983 		if (stcb)
   1984 			rt_addr = sctp_choose_v6_boundspecific_stcb(inp, stcb, net, rt, loc_scope, loopscope,  non_asoc_addr_ok);
   1985 		else
   1986 			/* we can't have a non-asoc address since we have no association */
   1987 			rt_addr = sctp_choose_v6_boundspecific_inp(inp,  rt, loc_scope, loopscope);
   1988 	}
   1989 	rtcache_unref(rt, ro);
   1990 	if (rt_addr == NULL) {
   1991 		/* no suitable address? */
   1992 		struct in6_addr in6;
   1993 #ifdef SCTP_DEBUG
   1994 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   1995 			printf("V6 packet will reach dead-end no suitable src address\n");
   1996 		}
   1997 #endif
   1998 		memset(&in6, 0, sizeof(in6));
   1999 		return (in6);
   2000 	}
   2001 #ifdef SCTP_DEBUG
   2002 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   2003 		printf("Source address selected is:");
   2004 		sctp_print_address((struct sockaddr *)rt_addr);
   2005 	}
   2006 #endif
   2007 	return (rt_addr->sin6_addr);
   2008 }
   2009 
   2010 static uint8_t
   2011 sctp_get_ect(struct sctp_tcb *stcb,
   2012 	     struct sctp_tmit_chunk *chk)
   2013 {
   2014 	uint8_t this_random;
   2015 
   2016 	/* Huh? */
   2017 	if (sctp_ecn == 0)
   2018 		return (0);
   2019 
   2020 	if (sctp_ecn_nonce == 0)
   2021 		/* no nonce, always return ECT0 */
   2022 		return (SCTP_ECT0_BIT);
   2023 
   2024 	if (stcb->asoc.peer_supports_ecn_nonce == 0) {
   2025 		/* Peer does NOT support it, so we send a ECT0 only */
   2026  		return (SCTP_ECT0_BIT);
   2027 	}
   2028 
   2029 	if (chk == NULL)
   2030 	   return (SCTP_ECT0_BIT);
   2031 
   2032 	if (((stcb->asoc.hb_random_idx == 3) &&
   2033 	     (stcb->asoc.hb_ect_randombit > 7)) ||
   2034 	     (stcb->asoc.hb_random_idx > 3)) {
   2035 		uint32_t rndval;
   2036 		rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
   2037 		memcpy(stcb->asoc.hb_random_values, &rndval,
   2038 		       sizeof(stcb->asoc.hb_random_values));
   2039 		this_random = stcb->asoc.hb_random_values[0];
   2040 		stcb->asoc.hb_random_idx = 0;
   2041 		stcb->asoc.hb_ect_randombit = 0;
   2042 	} else {
   2043 		if (stcb->asoc.hb_ect_randombit > 7) {
   2044 		  stcb->asoc.hb_ect_randombit = 0;
   2045 		  stcb->asoc.hb_random_idx++;
   2046 		}
   2047 		this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
   2048 	}
   2049 	if ((this_random >> stcb->asoc.hb_ect_randombit) & 0x01) {
   2050 		if (chk != NULL)
   2051 			/* ECN Nonce stuff */
   2052 			chk->rec.data.ect_nonce = SCTP_ECT1_BIT;
   2053 		stcb->asoc.hb_ect_randombit++;
   2054 		return (SCTP_ECT1_BIT);
   2055 	} else {
   2056 		stcb->asoc.hb_ect_randombit++;
   2057 		return (SCTP_ECT0_BIT);
   2058 	}
   2059 }
   2060 
   2061 extern int sctp_no_csum_on_loopback;
   2062 
   2063 static int
   2064 sctp_lowlevel_chunk_output(struct sctp_inpcb *inp,
   2065 			   struct sctp_tcb *stcb,    /* may be NULL */
   2066 			   struct sctp_nets *net,
   2067 			   const struct sockaddr *to,
   2068 			   struct mbuf *m,
   2069 			   int nofragment_flag,
   2070 			   int ecn_ok,
   2071 			   struct sctp_tmit_chunk *chk,
   2072 			   int out_of_asoc_ok)
   2073 	/* nofragment_flag to tell if IP_DF should be set (IPv4 only) */
   2074 {
   2075 	/*
   2076 	 * Given a mbuf chain (via m_next) that holds a packet header
   2077 	 * WITH a SCTPHDR but no IP header, endpoint inp and sa structure.
   2078 	 * - calculate SCTP checksum and fill in
   2079 	 * - prepend a IP address header
   2080 	 * - if boundall use INADDR_ANY
   2081 	 * - if boundspecific do source address selection
   2082 	 * - set fragmentation option for ipV4
   2083 	 * - On return from IP output, check/adjust mtu size
   2084 	 * - of output interface and smallest_mtu size as well.
   2085 	 */
   2086 	struct sctphdr *sctphdr;
   2087 	int o_flgs;
   2088 	uint32_t csum;
   2089 	int ret;
   2090 	unsigned int have_mtu;
   2091 	struct route *ro;
   2092 	struct rtentry *rt;
   2093 
   2094 	if ((net) && (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)) {
   2095 		sctp_m_freem(m);
   2096 		return (EFAULT);
   2097 	}
   2098 	if ((m->m_flags & M_PKTHDR) == 0) {
   2099 #ifdef SCTP_DEBUG
   2100 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   2101 			printf("Software error: sctp_lowlevel_chunk_output() called with non pkthdr!\n");
   2102 		}
   2103 #endif
   2104 		sctp_m_freem(m);
   2105 		return (EFAULT);
   2106 	}
   2107 	/* Calculate the csum and fill in the length of the packet */
   2108 	sctphdr = mtod(m, struct sctphdr *);
   2109 	have_mtu = 0;
   2110 	if (sctp_no_csum_on_loopback &&
   2111 	     (stcb) &&
   2112 	     (stcb->asoc.loopback_scope)) {
   2113 		sctphdr->checksum = 0;
   2114 		m->m_pkthdr.len = sctp_calculate_len(m);
   2115 	} else {
   2116 		sctphdr->checksum = 0;
   2117 		csum = sctp_calculate_sum(m, &m->m_pkthdr.len, 0);
   2118 		sctphdr->checksum = csum;
   2119 	}
   2120 	if (to->sa_family == AF_INET) {
   2121 		struct ip *ip;
   2122 		static struct route iproute;
   2123 		M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
   2124 		if (m == NULL) {
   2125 			/* failed to prepend data, give up */
   2126 			return (ENOMEM);
   2127 		}
   2128 		ip = mtod(m, struct ip *);
   2129 		ip->ip_v = IPVERSION;
   2130 		ip->ip_hl = (sizeof(struct ip) >> 2);
   2131 		if (nofragment_flag) {
   2132 			ip->ip_off = htons(IP_DF);
   2133 		} else
   2134 			ip->ip_off = 0;
   2135 
   2136 		ip->ip_id = htons(ip_newid(NULL));
   2137 		ip->ip_ttl = inp->inp_ip_ttl;
   2138 		ip->ip_len = htons(m->m_pkthdr.len);
   2139 		if (stcb) {
   2140 			if ((stcb->asoc.ecn_allowed) && ecn_ok) {
   2141 				/* Enable ECN */
   2142 				ip->ip_tos = (u_char)((inp->ip_inp.inp.inp_ip.ip_tos & 0x000000fc) |
   2143 						      sctp_get_ect(stcb, chk));
   2144 			} else {
   2145 				/* No ECN */
   2146 				ip->ip_tos = inp->ip_inp.inp.inp_ip.ip_tos;
   2147 			}
   2148 		} else {
   2149 			/* no association at all */
   2150 			ip->ip_tos = inp->inp_ip_tos;
   2151 		}
   2152 		ip->ip_p = IPPROTO_SCTP;
   2153 		ip->ip_sum = 0;
   2154 #ifdef SCTP_DEBUG
   2155 		printf("chunk_output: net %p\n", net);
   2156 #endif
   2157 		if (net == NULL) {
   2158 			ro = &iproute;
   2159 			memset(&iproute, 0, sizeof(iproute));
   2160 			/* XXX */
   2161 			rt = rtcache_lookup(ro, to);
   2162 			rtcache_unref(rt, ro);
   2163 		} else {
   2164 			ro = (struct route *)&net->ro;
   2165 		}
   2166 		/* Now the address selection part */
   2167 		ip->ip_dst.s_addr = satocsin(to)->sin_addr.s_addr;
   2168 
   2169 		/* call the routine to select the src address */
   2170 		if (net) {
   2171 			if (net->src_addr_selected == 0) {
   2172 				/* Cache the source address */
   2173 				((struct sockaddr_in *)&net->_s_addr)->sin_addr = sctp_ipv4_source_address_selection(inp,
   2174 				    stcb,
   2175 				    ro, net, out_of_asoc_ok);
   2176 				rt = rtcache_validate(ro);
   2177 				if (rt != NULL) {
   2178 					net->src_addr_selected = 1;
   2179 				}
   2180 				rtcache_unref(rt, ro);
   2181 			}
   2182 			ip->ip_src = ((struct sockaddr_in *)&net->_s_addr)->sin_addr;
   2183 		} else {
   2184 			ip->ip_src = sctp_ipv4_source_address_selection(inp,
   2185 			    stcb, ro, net, out_of_asoc_ok);
   2186 		}
   2187 #ifdef SCTP_DEBUG
   2188 		printf("src addr %x\n", ip->ip_src.s_addr);
   2189 #endif
   2190 		/*
   2191 		 * If source address selection fails and we find no route then
   2192 		 * the ip_ouput should fail as well with a NO_ROUTE_TO_HOST
   2193 		 * type error. We probably should catch that somewhere and
   2194 		 * abort the association right away (assuming this is an INIT
   2195 		 * being sent).
   2196 		 */
   2197 		rt = rtcache_validate(ro);
   2198 		if (rt == NULL) {
   2199 			/*
   2200 			 * src addr selection failed to find a route (or valid
   2201 			 * source addr), so we can't get there from here!
   2202 			 */
   2203 #ifdef SCTP_DEBUG
   2204 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   2205 				printf("low_level_output: dropped v4 packet- no valid source addr\n");
   2206 				printf("Destination was %x\n", (u_int)(ntohl(ip->ip_dst.s_addr)));
   2207 			}
   2208 #endif /* SCTP_DEBUG */
   2209 			if (net) {
   2210 				if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb)
   2211 					sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
   2212 							stcb,
   2213 							SCTP_FAILED_THRESHOLD,
   2214 							(void *)net);
   2215 				net->dest_state &= ~SCTP_ADDR_REACHABLE;
   2216 				net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
   2217 				if (stcb) {
   2218 					if (net == stcb->asoc.primary_destination) {
   2219 						/* need a new primary */
   2220 						struct sctp_nets *alt;
   2221 						alt = sctp_find_alternate_net(stcb, net);
   2222 						if (alt != net) {
   2223 							if (sctp_set_primary_addr(stcb,
   2224 									      (struct sockaddr *)NULL,
   2225 										 alt) == 0) {
   2226 								net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
   2227 								net->src_addr_selected = 0;
   2228 							}
   2229 						}
   2230 					}
   2231 				}
   2232 			}
   2233 			sctp_m_freem(m);
   2234 			return (EHOSTUNREACH);
   2235 		} else {
   2236 			have_mtu = rt->rt_ifp->if_mtu;
   2237 		}
   2238 
   2239 		o_flgs = (IP_RAWOUTPUT | (inp->sctp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)));
   2240 #ifdef SCTP_DEBUG
   2241 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   2242 			printf("Calling ipv4 output routine from low level src addr:%x\n",
   2243 			       (u_int)(ntohl(ip->ip_src.s_addr)));
   2244 			printf("Destination is %x\n", (u_int)(ntohl(ip->ip_dst.s_addr)));
   2245 			printf("RTP route is %p through\n", rt);
   2246 			printf("length %d\n", ntohs(ip->ip_len));
   2247 		}
   2248 #endif
   2249 		if ((have_mtu) && (net) && (have_mtu > net->mtu)) {
   2250 			rt->rt_ifp->if_mtu = net->mtu;
   2251 		}
   2252 		ret = ip_output(m, inp->ip_inp.inp.inp_options,
   2253 				ro, o_flgs, inp->ip_inp.inp.inp_moptions,
   2254                                 &inp->ip_inp.inp);
   2255 		if ((rt) && (have_mtu) && (net) && (have_mtu > net->mtu)) {
   2256 			rt->rt_ifp->if_mtu = have_mtu;
   2257 		}
   2258 		sctp_pegs[SCTP_DATAGRAMS_SENT]++;
   2259 #ifdef SCTP_DEBUG
   2260 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   2261 			printf("Ip output returns %d\n", ret);
   2262 		}
   2263 #endif
   2264 		if (net == NULL) {
   2265 		} else {
   2266 			/* PMTU check versus smallest asoc MTU goes here */
   2267 			if (rt != NULL) {
   2268 				if (rt->rt_rmx.rmx_mtu &&
   2269 				    (stcb->asoc.smallest_mtu > rt->rt_rmx.rmx_mtu)) {
   2270 					sctp_mtu_size_reset(inp, &stcb->asoc,
   2271 					    rt->rt_rmx.rmx_mtu);
   2272 				}
   2273 			} else {
   2274 				/* route was freed */
   2275 				net->src_addr_selected = 0;
   2276 			}
   2277 		}
   2278 		rtcache_unref(rt, ro);
   2279 		return (ret);
   2280 	}
   2281 #ifdef INET6
   2282 	else if (to->sa_family == AF_INET6) {
   2283 		struct ip6_hdr *ip6h;
   2284 		static struct route ip6route;
   2285 		struct ifnet *ifp;
   2286 		u_char flowTop;
   2287 		uint16_t flowBottom;
   2288 		u_char tosBottom, tosTop;
   2289 		struct sockaddr_in6 *sin6, tmp, *lsa6, lsa6_tmp;
   2290 		int prev_scope=0;
   2291 		u_short prev_port=0;
   2292 
   2293 		M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
   2294 		if (m == NULL) {
   2295 			/* failed to prepend data, give up */
   2296 			return (ENOMEM);
   2297 		}
   2298 		ip6h = mtod(m, struct ip6_hdr *);
   2299 
   2300 		/*
   2301 		 * We assume here that inp_flow is in host byte order within
   2302 		 * the TCB!
   2303 		 */
   2304 		flowBottom = ((struct in6pcb *)inp)->in6p_flowinfo & 0x0000ffff;
   2305 		flowTop = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x000f0000) >> 16);
   2306 
   2307 		tosTop = (((((struct in6pcb *)inp)->in6p_flowinfo & 0xf0) >> 4) | IPV6_VERSION);
   2308 
   2309 		/* protect *sin6 from overwrite */
   2310 		memcpy(&tmp, to, sizeof(struct sockaddr_in6));
   2311 		sin6 = &tmp;
   2312 
   2313 		/* KAME hack: embed scopeid */
   2314 #if defined(SCTP_BASE_FREEBSD) || defined(__APPLE__)
   2315 		if (in6_embedscope(&sin6->sin6_addr, sin6, NULL, NULL) != 0)
   2316 #else
   2317 		/*
   2318 		 * XXX: appropriate scope zone must be provided or otherwise
   2319 		 * ip6_use_defzone must be 1.
   2320 		 */
   2321 		if (sa6_embedscope(sin6, ip6_use_defzone) != 0)
   2322 #endif
   2323 			return (EINVAL);
   2324 		if (net == NULL) {
   2325 			memset(&ip6route, 0, sizeof(ip6route));
   2326 			ro = (struct route *)&ip6route;
   2327 			/* XXX */
   2328 			rt = rtcache_lookup(ro, (struct sockaddr *) sin6);
   2329 			rtcache_unref(rt, ro);
   2330 		} else {
   2331 			ro = (struct route *)&net->ro;
   2332 		}
   2333 		if (stcb != NULL) {
   2334 			if ((stcb->asoc.ecn_allowed) && ecn_ok) {
   2335 				/* Enable ECN */
   2336 				tosBottom = (((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) | sctp_get_ect(stcb, chk)) << 4);
   2337 			} else {
   2338 				/* No ECN */
   2339 				tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
   2340 			}
   2341 		} else {
   2342 			/* we could get no asoc if it is a O-O-T-B packet */
   2343 			tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
   2344 		}
   2345 		ip6h->ip6_flow = htonl(((tosTop << 24) | ((tosBottom|flowTop) << 16) | flowBottom));
   2346 		ip6h->ip6_nxt = IPPROTO_SCTP;
   2347 		ip6h->ip6_plen = m->m_pkthdr.len;
   2348 		ip6h->ip6_dst = sin6->sin6_addr;
   2349 
   2350 		/*
   2351 		 * Add SRC address selection here:
   2352 		 * we can only reuse to a limited degree the kame src-addr-sel,
   2353 		 * since we can try their selection but it may not be bound.
   2354 		 */
   2355 		memset(&lsa6_tmp, 0, sizeof(lsa6_tmp));
   2356 		lsa6_tmp.sin6_family = AF_INET6;
   2357 		lsa6_tmp.sin6_len = sizeof(lsa6_tmp);
   2358 		lsa6 = &lsa6_tmp;
   2359 		rt = rtcache_validate(ro);
   2360 		if (net) {
   2361 			if (net->src_addr_selected == 0) {
   2362 				/* Cache the source address */
   2363 				((struct sockaddr_in6 *)&net->_s_addr)->sin6_addr = sctp_ipv6_source_address_selection(inp,
   2364 				    stcb, ro, net, out_of_asoc_ok);
   2365 
   2366 				if (rt != NULL) {
   2367 					net->src_addr_selected = 1;
   2368 				}
   2369 			}
   2370 			lsa6->sin6_addr = ((struct sockaddr_in6 *)&net->_s_addr)->sin6_addr;
   2371 		} else {
   2372 			lsa6->sin6_addr = sctp_ipv6_source_address_selection(
   2373 			    inp, stcb, ro, net, out_of_asoc_ok);
   2374 		}
   2375 		lsa6->sin6_port = inp->sctp_lport;
   2376 
   2377 		if (rt ==  NULL) {
   2378 			/*
   2379 			 * src addr selection failed to find a route (or valid
   2380 			 * source addr), so we can't get there from here!
   2381 			 */
   2382 #ifdef SCTP_DEBUG
   2383 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   2384 				printf("low_level_output: dropped v6 pkt- no valid source addr\n");
   2385 			}
   2386 #endif
   2387 			sctp_m_freem(m);
   2388 			if (net) {
   2389 				if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb)
   2390 					sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
   2391 							stcb,
   2392 							SCTP_FAILED_THRESHOLD,
   2393 							(void *)net);
   2394 				net->dest_state &= ~SCTP_ADDR_REACHABLE;
   2395 				net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
   2396 				if (stcb) {
   2397 					if (net == stcb->asoc.primary_destination) {
   2398 						/* need a new primary */
   2399 						struct sctp_nets *alt;
   2400 						alt = sctp_find_alternate_net(stcb, net);
   2401 						if (alt != net) {
   2402 							if (sctp_set_primary_addr(stcb,
   2403 									      (struct sockaddr *)NULL,
   2404 										 alt) == 0) {
   2405 								net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
   2406 								net->src_addr_selected = 0;
   2407 							}
   2408 						}
   2409 					}
   2410 				}
   2411 			}
   2412 			return (EHOSTUNREACH);
   2413 		}
   2414 
   2415 		ip6h->ip6_src = lsa6->sin6_addr;
   2416 
   2417 		/*
   2418 		 * We set the hop limit now since there is a good chance that
   2419 		 * our ro pointer is now filled
   2420 		 */
   2421 		ip6h->ip6_hlim = in6_selecthlim((struct in6pcb *)&inp->ip_inp.inp,
   2422 						(ro ?
   2423 						 (rt ? (rt->rt_ifp) : (NULL)) :
   2424 						 (NULL)));
   2425 		o_flgs = 0;
   2426 		ifp = rt->rt_ifp;
   2427 #ifdef SCTP_DEBUG
   2428 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   2429 			/* Copy to be sure something bad is not happening */
   2430 			sin6->sin6_addr = ip6h->ip6_dst;
   2431 			lsa6->sin6_addr = ip6h->ip6_src;
   2432 
   2433 			printf("Calling ipv6 output routine from low level\n");
   2434 			printf("src: ");
   2435 			sctp_print_address((struct sockaddr *)lsa6);
   2436 			printf("dst: ");
   2437 			sctp_print_address((struct sockaddr *)sin6);
   2438 		}
   2439 #endif /* SCTP_DEBUG */
   2440 		if (net) {
   2441 			sin6 = (struct sockaddr_in6 *)&net->ro.ro_sa;
   2442 			/* preserve the port and scope for link local send */
   2443 			prev_scope = sin6->sin6_scope_id;
   2444 			prev_port = sin6->sin6_port;
   2445 		}
   2446 		/* XXX NOMPSAFE need to hold ifp here */
   2447 		rtcache_unref(rt, ro);
   2448 		ret = ip6_output(m, ((struct in6pcb *)inp)->in6p_outputopts,
   2449 				 ro,
   2450 				 o_flgs,
   2451 				 ((struct in6pcb *)inp)->in6p_moptions,
   2452 				 (struct in6pcb *)inp,
   2453 				 &ifp);
   2454 		if (net) {
   2455 			/* for link local this must be done */
   2456 			sin6->sin6_scope_id = prev_scope;
   2457 			sin6->sin6_port = prev_port;
   2458 		}
   2459 #ifdef SCTP_DEBUG
   2460 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   2461 			printf("return from send is %d\n", ret);
   2462 		}
   2463 #endif /* SCTP_DEBUG_OUTPUT */
   2464 		sctp_pegs[SCTP_DATAGRAMS_SENT]++;
   2465 		if (net) {
   2466 			/* PMTU check versus smallest asoc MTU goes here */
   2467 			rt = rtcache_validate(ro);
   2468 			if (rt == NULL) {
   2469 				/* Route was freed */
   2470 				net->src_addr_selected = 0;
   2471 			}
   2472 			if (rt != NULL) {
   2473 				if (rt->rt_rmx.rmx_mtu &&
   2474 				    (stcb->asoc.smallest_mtu > rt->rt_rmx.rmx_mtu)) {
   2475 					sctp_mtu_size_reset(inp,
   2476 							    &stcb->asoc,
   2477 							    rt->rt_rmx.rmx_mtu);
   2478 				}
   2479 				rtcache_unref(rt, ro);
   2480 			} else if (ifp) {
   2481 				if (ND_IFINFO(ifp)->linkmtu &&
   2482 				    (stcb->asoc.smallest_mtu > ND_IFINFO(ifp)->linkmtu)) {
   2483 					sctp_mtu_size_reset(inp,
   2484 							    &stcb->asoc,
   2485 							    ND_IFINFO(ifp)->linkmtu);
   2486 				}
   2487 			}
   2488 		}
   2489 		return (ret);
   2490 	}
   2491 #endif
   2492 	else {
   2493 #ifdef SCTP_DEBUG
   2494 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   2495 			printf("Unknown protocol (TSNH) type %d\n", ((const struct sockaddr *)to)->sa_family);
   2496 		}
   2497 #endif
   2498 		sctp_m_freem(m);
   2499 		return (EFAULT);
   2500 	}
   2501 }
   2502 
   2503 static
   2504 int sctp_is_address_in_scope(struct ifaddr *ifa,
   2505  			     int ipv4_addr_legal,
   2506 			     int ipv6_addr_legal,
   2507 			     int loopback_scope,
   2508 			     int ipv4_local_scope,
   2509 			     int local_scope,
   2510 			     int site_scope)
   2511 {
   2512 	if ((loopback_scope == 0) &&
   2513 	    (ifa->ifa_ifp) &&
   2514 	    (ifa->ifa_ifp->if_type == IFT_LOOP)) {
   2515 		/* skip loopback if not in scope *
   2516 		 */
   2517 		return (0);
   2518 	}
   2519 	if ((ifa->ifa_addr->sa_family == AF_INET) && ipv4_addr_legal) {
   2520 		struct sockaddr_in *sin;
   2521 		sin = (struct sockaddr_in *)ifa->ifa_addr;
   2522 		if (sin->sin_addr.s_addr == 0) {
   2523 			/* not in scope , unspecified */
   2524 			return (0);
   2525 		}
   2526 		if ((ipv4_local_scope == 0) &&
   2527 		    (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
   2528 			/* private address not in scope */
   2529 			return (0);
   2530 		}
   2531 	} else if ((ifa->ifa_addr->sa_family == AF_INET6) && ipv6_addr_legal) {
   2532 		struct sockaddr_in6 *sin6;
   2533 		struct in6_ifaddr *ifa6;
   2534 
   2535 		ifa6 = (struct in6_ifaddr *)ifa;
   2536 		/* ok to use deprecated addresses? */
   2537 		if (!ip6_use_deprecated) {
   2538 			if (ifa6->ia6_flags &
   2539 			    IN6_IFF_DEPRECATED) {
   2540 				return (0);
   2541 			}
   2542 		}
   2543 		if (ifa6->ia6_flags &
   2544 		    (IN6_IFF_DETACHED |
   2545 		     IN6_IFF_ANYCAST |
   2546 		     IN6_IFF_NOTREADY)) {
   2547 			return (0);
   2548 		}
   2549 		sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
   2550 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
   2551 			/* skip unspecifed addresses */
   2552 			return (0);
   2553 		}
   2554 		if (/*(local_scope == 0) && */
   2555 		    (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) {
   2556 			return (0);
   2557 		}
   2558 		if ((site_scope == 0) &&
   2559 		    (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
   2560 			return (0);
   2561 		}
   2562 	} else {
   2563 		return (0);
   2564 	}
   2565 	return (1);
   2566 }
   2567 
   2568 
   2569 void
   2570 sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
   2571 {
   2572 	struct mbuf *m, *m_at, *m_last;
   2573 	struct sctp_nets *net;
   2574 	struct sctp_init_msg *initm;
   2575 	struct sctp_supported_addr_param *sup_addr;
   2576 	struct sctp_ecn_supported_param *ecn;
   2577 	struct sctp_prsctp_supported_param *prsctp;
   2578 	struct sctp_ecn_nonce_supported_param *ecn_nonce;
   2579 	struct sctp_supported_chunk_types_param *pr_supported;
   2580 	int cnt_inits_to=0;
   2581 	int padval, ret;
   2582 
   2583 	/* INIT's always go to the primary (and usually ONLY address) */
   2584 	m_last = NULL;
   2585 	net = stcb->asoc.primary_destination;
   2586 	if (net == NULL) {
   2587 		net = TAILQ_FIRST(&stcb->asoc.nets);
   2588 		if (net == NULL) {
   2589 			/* TSNH */
   2590 			return;
   2591 		}
   2592 		/* we confirm any address we send an INIT to */
   2593 		net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
   2594 		sctp_set_primary_addr(stcb, NULL, net);
   2595 	} else {
   2596 		/* we confirm any address we send an INIT to */
   2597 		net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
   2598 	}
   2599 #ifdef SCTP_DEBUG
   2600 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
   2601 		printf("Sending INIT to ");
   2602 		sctp_print_address (rtcache_getdst(&net->ro));
   2603 	}
   2604 #endif
   2605 	if (rtcache_getdst(&net->ro)->sa_family == AF_INET6) {
   2606 		/* special hook, if we are sending to link local
   2607 		 * it will not show up in our private address count.
   2608 		 */
   2609 		if (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr *) rtcache_getdst(&net->ro)->sa_data))
   2610 			cnt_inits_to = 1;
   2611 	}
   2612 	if (callout_pending(&net->rxt_timer.timer)) {
   2613 		/* This case should not happen */
   2614 		return;
   2615 	}
   2616 	/* start the INIT timer */
   2617 	if (sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net)) {
   2618 		/* we are hosed since I can't start the INIT timer? */
   2619 		return;
   2620 	}
   2621 	MGETHDR(m, M_DONTWAIT, MT_HEADER);
   2622 	if (m == NULL) {
   2623 		/* No memory, INIT timer will re-attempt. */
   2624 		return;
   2625 	}
   2626 	/* make it into a M_EXT */
   2627 	MCLGET(m, M_DONTWAIT);
   2628 	if ((m->m_flags & M_EXT) != M_EXT) {
   2629 		/* Failed to get cluster buffer */
   2630 		sctp_m_freem(m);
   2631 		return;
   2632 	}
   2633 	m->m_data += SCTP_MIN_OVERHEAD;
   2634 	m->m_len = sizeof(struct sctp_init_msg);
   2635 	/* Now lets put the SCTP header in place */
   2636 	initm = mtod(m, struct sctp_init_msg *);
   2637 	initm->sh.src_port = inp->sctp_lport;
   2638 	initm->sh.dest_port = stcb->rport;
   2639 	initm->sh.v_tag = 0;
   2640 	initm->sh.checksum = 0;	/* calculate later */
   2641 	/* now the chunk header */
   2642 	initm->msg.ch.chunk_type = SCTP_INITIATION;
   2643 	initm->msg.ch.chunk_flags = 0;
   2644 	/* fill in later from mbuf we build */
   2645 	initm->msg.ch.chunk_length = 0;
   2646 	/* place in my tag */
   2647 	initm->msg.init.initiate_tag = htonl(stcb->asoc.my_vtag);
   2648 	/* set up some of the credits. */
   2649 	initm->msg.init.a_rwnd = htonl(max(inp->sctp_socket->so_rcv.sb_hiwat,
   2650 	    SCTP_MINIMAL_RWND));
   2651 
   2652 	initm->msg.init.num_outbound_streams = htons(stcb->asoc.pre_open_streams);
   2653 	initm->msg.init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams);
   2654 	initm->msg.init.initial_tsn = htonl(stcb->asoc.init_seq_number);
   2655 	/* now the address restriction */
   2656 	sup_addr = (struct sctp_supported_addr_param *)((vaddr_t)initm +
   2657 	    sizeof(*initm));
   2658 	sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE);
   2659 	/* we support 2 types IPv6/IPv4 */
   2660 	sup_addr->ph.param_length = htons(sizeof(*sup_addr) +
   2661 					  sizeof(uint16_t));
   2662 	sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
   2663 	sup_addr->addr_type[1] = htons(SCTP_IPV6_ADDRESS);
   2664 	m->m_len += sizeof(*sup_addr) + sizeof(uint16_t);
   2665 
   2666 /*	if (inp->sctp_flags & SCTP_PCB_FLAGS_ADAPTIONEVNT) {*/
   2667 	if (inp->sctp_ep.adaption_layer_indicator) {
   2668 		struct sctp_adaption_layer_indication *ali;
   2669 		ali = (struct sctp_adaption_layer_indication *)(
   2670 		    (vaddr_t)sup_addr + sizeof(*sup_addr) + sizeof(uint16_t));
   2671 		ali->ph.param_type = htons(SCTP_ULP_ADAPTION);
   2672 		ali->ph.param_length = htons(sizeof(*ali));
   2673 		ali->indication = ntohl(inp->sctp_ep.adaption_layer_indicator);
   2674 		m->m_len += sizeof(*ali);
   2675 		ecn = (struct sctp_ecn_supported_param *)((vaddr_t)ali +
   2676 		    sizeof(*ali));
   2677 	} else {
   2678 		ecn = (struct sctp_ecn_supported_param *)((vaddr_t)sup_addr +
   2679 		    sizeof(*sup_addr) + sizeof(uint16_t));
   2680 	}
   2681 
   2682 	/* now any cookie time extensions */
   2683 	if (stcb->asoc.cookie_preserve_req) {
   2684 		struct sctp_cookie_perserve_param *cookie_preserve;
   2685 		cookie_preserve = (struct sctp_cookie_perserve_param *)(ecn);
   2686 		cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE);
   2687 		cookie_preserve->ph.param_length = htons(
   2688 		    sizeof(*cookie_preserve));
   2689 		cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req);
   2690 		m->m_len += sizeof(*cookie_preserve);
   2691 		ecn = (struct sctp_ecn_supported_param *)(
   2692 		    (vaddr_t)cookie_preserve + sizeof(*cookie_preserve));
   2693 		stcb->asoc.cookie_preserve_req = 0;
   2694 	}
   2695 
   2696 	/* ECN parameter */
   2697 	if (sctp_ecn == 1) {
   2698 		ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
   2699 		ecn->ph.param_length = htons(sizeof(*ecn));
   2700 		m->m_len += sizeof(*ecn);
   2701 		prsctp = (struct sctp_prsctp_supported_param *)((vaddr_t)ecn +
   2702 		    sizeof(*ecn));
   2703 	} else {
   2704 		prsctp = (struct sctp_prsctp_supported_param *)((vaddr_t)ecn);
   2705 	}
   2706 	/* And now tell the peer we do pr-sctp */
   2707 	prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
   2708 	prsctp->ph.param_length = htons(sizeof(*prsctp));
   2709 	m->m_len += sizeof(*prsctp);
   2710 
   2711 
   2712 	/* And now tell the peer we do all the extensions */
   2713 	pr_supported = (struct sctp_supported_chunk_types_param *)((vaddr_t)prsctp +
   2714 	   sizeof(*prsctp));
   2715 
   2716 	pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
   2717 	pr_supported->ph.param_length = htons(sizeof(*pr_supported) + SCTP_EXT_COUNT);
   2718 	pr_supported->chunk_types[0] = SCTP_ASCONF;
   2719 	pr_supported->chunk_types[1] = SCTP_ASCONF_ACK;
   2720 	pr_supported->chunk_types[2] = SCTP_FORWARD_CUM_TSN;
   2721 	pr_supported->chunk_types[3] = SCTP_PACKET_DROPPED;
   2722 	pr_supported->chunk_types[4] = SCTP_STREAM_RESET;
   2723 	pr_supported->chunk_types[5] = 0; /* pad */
   2724 	pr_supported->chunk_types[6] = 0; /* pad */
   2725 	pr_supported->chunk_types[7] = 0; /* pad */
   2726 
   2727 	m->m_len += (sizeof(*pr_supported) + SCTP_EXT_COUNT + SCTP_PAD_EXT_COUNT);
   2728 	/* ECN nonce: And now tell the peer we support ECN nonce */
   2729 
   2730 	if (sctp_ecn_nonce) {
   2731 		ecn_nonce = (struct sctp_ecn_nonce_supported_param *)((vaddr_t)pr_supported +
   2732 		    sizeof(*pr_supported) + SCTP_EXT_COUNT + SCTP_PAD_EXT_COUNT);
   2733 		ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
   2734 		ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
   2735 		m->m_len += sizeof(*ecn_nonce);
   2736 	}
   2737 
   2738 	m_at = m;
   2739 	/* now the addresses */
   2740 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
   2741 		struct ifnet *ifn;
   2742 		struct ifaddr *ifa;
   2743 		int cnt;
   2744 		int s;
   2745 
   2746 		cnt = cnt_inits_to;
   2747 		s = pserialize_read_enter();
   2748 		IFNET_READER_FOREACH(ifn) {
   2749 			if ((stcb->asoc.loopback_scope == 0) &&
   2750 			    (ifn->if_type == IFT_LOOP)) {
   2751 				/*
   2752 				 * Skip loopback devices if loopback_scope
   2753 				 * not set
   2754 				 */
   2755 				continue;
   2756 			}
   2757 			IFADDR_READER_FOREACH(ifa, ifn) {
   2758 				if (sctp_is_address_in_scope(ifa,
   2759 				    stcb->asoc.ipv4_addr_legal,
   2760 				    stcb->asoc.ipv6_addr_legal,
   2761 				    stcb->asoc.loopback_scope,
   2762 				    stcb->asoc.ipv4_local_scope,
   2763 				    stcb->asoc.local_scope,
   2764 				    stcb->asoc.site_scope) == 0) {
   2765 					continue;
   2766 				}
   2767 				cnt++;
   2768 			}
   2769 		}
   2770 		pserialize_read_exit(s);
   2771 
   2772 		if (cnt > 1) {
   2773 			s = pserialize_read_enter();
   2774 			IFNET_READER_FOREACH(ifn) {
   2775 				if ((stcb->asoc.loopback_scope == 0) &&
   2776 				    (ifn->if_type == IFT_LOOP)) {
   2777 					/*
   2778 					 * Skip loopback devices if loopback_scope
   2779 					 * not set
   2780 					 */
   2781 					continue;
   2782 				}
   2783 				IFADDR_READER_FOREACH(ifa, ifn) {
   2784 					if (sctp_is_address_in_scope(ifa,
   2785 					    stcb->asoc.ipv4_addr_legal,
   2786 					    stcb->asoc.ipv6_addr_legal,
   2787 					    stcb->asoc.loopback_scope,
   2788 					    stcb->asoc.ipv4_local_scope,
   2789 					    stcb->asoc.local_scope,
   2790 					    stcb->asoc.site_scope) == 0) {
   2791 						continue;
   2792 					}
   2793 					m_at = sctp_add_addr_to_mbuf(m_at, ifa);
   2794 				}
   2795 			}
   2796 			pserialize_read_exit(s);
   2797 		}
   2798 	} else {
   2799 		struct sctp_laddr *laddr;
   2800 		int cnt;
   2801 		cnt = cnt_inits_to;
   2802 		/* First, how many ? */
   2803 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
   2804 			if (laddr->ifa == NULL) {
   2805 				continue;
   2806 			}
   2807 			if (laddr->ifa->ifa_addr == NULL)
   2808 				continue;
   2809 			if (sctp_is_address_in_scope(laddr->ifa,
   2810 			    stcb->asoc.ipv4_addr_legal,
   2811 			    stcb->asoc.ipv6_addr_legal,
   2812 			    stcb->asoc.loopback_scope,
   2813 			    stcb->asoc.ipv4_local_scope,
   2814 			    stcb->asoc.local_scope,
   2815 			    stcb->asoc.site_scope) == 0) {
   2816 				continue;
   2817 			}
   2818 			cnt++;
   2819 		}
   2820 		/* To get through a NAT we only list addresses if
   2821 		 * we have more than one. That way if you just
   2822 		 * bind a single address we let the source of the init
   2823 		 * dictate our address.
   2824 		 */
   2825 		if (cnt > 1) {
   2826 			LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
   2827 				if (laddr->ifa == NULL) {
   2828 					continue;
   2829 				}
   2830 				if (laddr->ifa->ifa_addr == NULL) {
   2831 					continue;
   2832 				}
   2833 
   2834 				if (sctp_is_address_in_scope(laddr->ifa,
   2835 				    stcb->asoc.ipv4_addr_legal,
   2836 				    stcb->asoc.ipv6_addr_legal,
   2837 				    stcb->asoc.loopback_scope,
   2838 				    stcb->asoc.ipv4_local_scope,
   2839 				    stcb->asoc.local_scope,
   2840 				    stcb->asoc.site_scope) == 0) {
   2841 					continue;
   2842 				}
   2843 				m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa);
   2844 			}
   2845 		}
   2846 	}
   2847 	/* calulate the size and update pkt header and chunk header */
   2848 	m->m_pkthdr.len = 0;
   2849 	for (m_at = m; m_at; m_at = m_at->m_next) {
   2850 		if (m_at->m_next == NULL)
   2851 			m_last = m_at;
   2852 		m->m_pkthdr.len += m_at->m_len;
   2853 	}
   2854 	initm->msg.ch.chunk_length = htons((m->m_pkthdr.len -
   2855 	    sizeof(struct sctphdr)));
   2856 #ifdef SCTP_DEBUG
   2857 	printf("chunk_length %d\n", ntohs(initm->msg.ch.chunk_length));
   2858 #endif
   2859 	/* We pass 0 here to NOT set IP_DF if its IPv4, we
   2860 	 * ignore the return here since the timer will drive
   2861 	 * a retranmission.
   2862 	 */
   2863 
   2864 	/* I don't expect this to execute but we will be safe here */
   2865 	padval = m->m_pkthdr.len % 4;
   2866 	if ((padval) && (m_last)) {
   2867 		/* The compiler worries that m_last may not be
   2868 		 * set even though I think it is impossible :->
   2869 		 * however we add m_last here just in case.
   2870 		 */
   2871 		ret = sctp_add_pad_tombuf(m_last, (4-padval));
   2872 		if (ret) {
   2873 			/* Houston we have a problem, no space */
   2874 			sctp_m_freem(m);
   2875 			return;
   2876 		}
   2877 		m->m_pkthdr.len += padval;
   2878 	}
   2879 #ifdef SCTP_DEBUG
   2880 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
   2881 		printf("Calling lowlevel output stcb:%p net:%p\n",
   2882 		       stcb, net);
   2883 	}
   2884 #endif
   2885 	ret = sctp_lowlevel_chunk_output(inp, stcb, net,
   2886 		  rtcache_getdst(&net->ro), m, 0, 0, NULL, 0);
   2887 #ifdef SCTP_DEBUG
   2888 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
   2889 		printf("Low level output returns %d\n", ret);
   2890 	}
   2891 #endif
   2892 	sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
   2893 	SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
   2894 }
   2895 
   2896 struct mbuf *
   2897 sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt,
   2898     int param_offset, int *abort_processing, struct sctp_chunkhdr *cp)
   2899 {
   2900 	/* Given a mbuf containing an INIT or INIT-ACK
   2901 	 * with the param_offset being equal to the
   2902 	 * beginning of the params i.e. (iphlen + sizeof(struct sctp_init_msg)
   2903 	 * parse through the parameters to the end of the mbuf verifying
   2904 	 * that all parameters are known.
   2905 	 *
   2906 	 * For unknown parameters build and return a mbuf with
   2907 	 * UNRECOGNIZED_PARAMETER errors. If the flags indicate
   2908 	 * to stop processing this chunk stop, and set *abort_processing
   2909 	 * to 1.
   2910 	 *
   2911 	 * By having param_offset be pre-set to where parameters begin
   2912 	 * it is hoped that this routine may be reused in the future
   2913 	 * by new features.
   2914 	 */
   2915 	struct sctp_paramhdr *phdr, params;
   2916 
   2917 	struct mbuf *mat, *op_err;
   2918 	char tempbuf[2048];
   2919 	int at, limit, pad_needed;
   2920 	uint16_t ptype, plen;
   2921 	int err_at;
   2922 
   2923 	*abort_processing = 0;
   2924 	mat = in_initpkt;
   2925 	err_at = 0;
   2926 	limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk);
   2927 #ifdef SCTP_DEBUG
   2928 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
   2929 		printf("Limit is %d bytes\n", limit);
   2930 	}
   2931 #endif
   2932 	at = param_offset;
   2933 	op_err = NULL;
   2934 
   2935 	phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
   2936 	while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) {
   2937 		ptype = ntohs(phdr->param_type);
   2938 		plen = ntohs(phdr->param_length);
   2939 		limit -= SCTP_SIZE32(plen);
   2940 		if (plen < sizeof(struct sctp_paramhdr)) {
   2941 #ifdef SCTP_DEBUG
   2942 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
   2943 			printf("sctp_output.c:Impossible length in parameter < %d\n", plen);
   2944 	}
   2945 #endif
   2946 			*abort_processing = 1;
   2947 			break;
   2948 		}
   2949 		/* All parameters for all chunks that we
   2950 		 * know/understand are listed here. We process
   2951 		 * them other places and make appropriate
   2952 		 * stop actions per the upper bits. However
   2953 		 * this is the generic routine processor's can
   2954 		 * call to get back an operr.. to either incorporate (init-ack)
   2955 		 * or send.
   2956 		 */
   2957 		if ((ptype == SCTP_HEARTBEAT_INFO) ||
   2958 		    (ptype == SCTP_IPV4_ADDRESS) ||
   2959 		    (ptype == SCTP_IPV6_ADDRESS) ||
   2960 		    (ptype == SCTP_STATE_COOKIE) ||
   2961 		    (ptype == SCTP_UNRECOG_PARAM) ||
   2962 		    (ptype == SCTP_COOKIE_PRESERVE) ||
   2963 		    (ptype == SCTP_SUPPORTED_ADDRTYPE) ||
   2964 		    (ptype == SCTP_PRSCTP_SUPPORTED) ||
   2965 		    (ptype == SCTP_ADD_IP_ADDRESS) ||
   2966 		    (ptype == SCTP_DEL_IP_ADDRESS) ||
   2967 		    (ptype == SCTP_ECN_CAPABLE) ||
   2968 		    (ptype == SCTP_ULP_ADAPTION) ||
   2969 		    (ptype == SCTP_ERROR_CAUSE_IND) ||
   2970 		    (ptype == SCTP_SET_PRIM_ADDR) ||
   2971 		    (ptype == SCTP_SUCCESS_REPORT) ||
   2972 		    (ptype == SCTP_ULP_ADAPTION) ||
   2973 		    (ptype == SCTP_SUPPORTED_CHUNK_EXT) ||
   2974 		    (ptype == SCTP_ECN_NONCE_SUPPORTED)
   2975 			) {
   2976 			/* no skip it */
   2977 			at += SCTP_SIZE32(plen);
   2978 		} else if (ptype == SCTP_HOSTNAME_ADDRESS) {
   2979 			/* We can NOT handle HOST NAME addresses!! */
   2980 #ifdef SCTP_DEBUG
   2981 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
   2982 		printf("Can't handle hostname addresses.. abort processing\n");
   2983 	}
   2984 #endif
   2985 			*abort_processing = 1;
   2986 			if (op_err == NULL) {
   2987 				/* Ok need to try to get a mbuf */
   2988 				MGETHDR(op_err, M_DONTWAIT, MT_DATA);
   2989 				if (op_err) {
   2990 					op_err->m_len = 0;
   2991 					op_err->m_pkthdr.len = 0;
   2992 					/* pre-reserve space for ip and sctp header  and chunk hdr*/
   2993 					op_err->m_data += sizeof(struct ip6_hdr);
   2994 					op_err->m_data += sizeof(struct sctphdr);
   2995 					op_err->m_data += sizeof(struct sctp_chunkhdr);
   2996 				}
   2997 			}
   2998 			if (op_err) {
   2999 				/* If we have space */
   3000 				struct sctp_paramhdr s;
   3001 				if (err_at % 4) {
   3002 					u_int32_t cpthis=0;
   3003 					pad_needed = 4 - (err_at % 4);
   3004 					m_copyback(op_err, err_at, pad_needed, (void *)&cpthis);
   3005 					err_at += pad_needed;
   3006 				}
   3007 				s.param_type = htons(SCTP_CAUSE_UNRESOLV_ADDR);
   3008 				s.param_length = htons(sizeof(s) + plen);
   3009 				m_copyback(op_err, err_at, sizeof(s), (void *)&s);
   3010 				err_at += sizeof(s);
   3011 				phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, plen);
   3012 				if (phdr == NULL) {
   3013 					sctp_m_freem(op_err);
   3014 					/* we are out of memory but we
   3015 					 * still need to have a look at what to
   3016 					 * do (the system is in trouble though).
   3017 					 */
   3018 					return (NULL);
   3019 				}
   3020 				m_copyback(op_err, err_at, plen, (void *)phdr);
   3021 				err_at += plen;
   3022 			}
   3023 			return (op_err);
   3024 		} else {
   3025 			/* we do not recognize the parameter
   3026 			 * figure out what we do.
   3027 			 */
   3028 #ifdef SCTP_DEBUG
   3029 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
   3030 				printf("Got parameter type %x - unknown\n",
   3031 				       (u_int)ptype);
   3032 			}
   3033 #endif
   3034 			if ((ptype & 0x4000) == 0x4000) {
   3035 				/* Report bit is set?? */
   3036 #ifdef SCTP_DEBUG
   3037 				if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
   3038 					printf("Report bit is set\n");
   3039 				}
   3040 #endif
   3041 				if (op_err == NULL) {
   3042 					/* Ok need to try to get an mbuf */
   3043 					MGETHDR(op_err, M_DONTWAIT, MT_DATA);
   3044 					if (op_err) {
   3045 						op_err->m_len = 0;
   3046 						op_err->m_pkthdr.len = 0;
   3047 						op_err->m_data += sizeof(struct ip6_hdr);
   3048 						op_err->m_data += sizeof(struct sctphdr);
   3049 						op_err->m_data += sizeof(struct sctp_chunkhdr);
   3050 					}
   3051 				}
   3052 				if (op_err) {
   3053 					/* If we have space */
   3054 					struct sctp_paramhdr s;
   3055 					if (err_at % 4) {
   3056 						u_int32_t cpthis=0;
   3057 						pad_needed = 4 - (err_at % 4);
   3058 						m_copyback(op_err, err_at, pad_needed, (void *)&cpthis);
   3059 						err_at += pad_needed;
   3060  					}
   3061 					s.param_type = htons(SCTP_UNRECOG_PARAM);
   3062 					s.param_length = htons(sizeof(s) + plen);
   3063 					m_copyback(op_err, err_at, sizeof(s), (void *)&s);
   3064 					err_at += sizeof(s);
   3065 					if (plen > sizeof(tempbuf)) {
   3066 						plen = sizeof(tempbuf);
   3067 					}
   3068 					phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, plen);
   3069 					if (phdr == NULL) {
   3070 						sctp_m_freem(op_err);
   3071 						/* we are out of memory but we
   3072 						 * still need to have a look at what to
   3073 						 * do (the system is in trouble though).
   3074 						 */
   3075 						goto more_processing;
   3076 					}
   3077 					m_copyback(op_err, err_at, plen, (void *)phdr);
   3078 					err_at += plen;
   3079 				}
   3080 			}
   3081 		more_processing:
   3082 			if ((ptype & 0x8000) == 0x0000) {
   3083 #ifdef SCTP_DEBUG
   3084 				if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
   3085 					printf("Abort bit is now setting1\n");
   3086 				}
   3087 #endif
   3088 				return (op_err);
   3089 			} else {
   3090 				/* skip this chunk and continue processing */
   3091 				at += SCTP_SIZE32(plen);
   3092 			}
   3093 
   3094 		}
   3095 		phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
   3096 	}
   3097 	return (op_err);
   3098 }
   3099 
   3100 static int
   3101 sctp_are_there_new_addresses(struct sctp_association *asoc,
   3102     struct mbuf *in_initpkt, int iphlen, int offset)
   3103 {
   3104 	/*
   3105 	 * Given a INIT packet, look through the packet to verify that
   3106 	 * there are NO new addresses. As we go through the parameters
   3107 	 * add reports of any un-understood parameters that require an
   3108 	 * error.  Also we must return (1) to drop the packet if we see
   3109 	 * a un-understood parameter that tells us to drop the chunk.
   3110 	 */
   3111 	struct sockaddr_in sin4, *sa4;
   3112 	struct sockaddr_in6 sin6, *sa6;
   3113 	struct sockaddr *sa_touse;
   3114 	struct sockaddr *sa;
   3115 	struct sctp_paramhdr *phdr, params;
   3116 	struct ip *iph;
   3117 	struct mbuf *mat;
   3118 	uint16_t ptype, plen;
   3119 	uint8_t fnd;
   3120 	struct sctp_nets *net;
   3121 
   3122 	memset(&sin4, 0, sizeof(sin4));
   3123 	memset(&sin6, 0, sizeof(sin6));
   3124 	sin4.sin_family = AF_INET;
   3125 	sin4.sin_len = sizeof(sin4);
   3126 	sin6.sin6_family = AF_INET6;
   3127 	sin6.sin6_len = sizeof(sin6);
   3128 
   3129 	sa_touse = NULL;
   3130 	/* First what about the src address of the pkt ? */
   3131 	iph = mtod(in_initpkt, struct ip *);
   3132 	if (iph->ip_v == IPVERSION) {
   3133 		/* source addr is IPv4 */
   3134 		sin4.sin_addr = iph->ip_src;
   3135 		sa_touse = (struct sockaddr *)&sin4;
   3136 	} else if (iph->ip_v == (IPV6_VERSION >> 4)) {
   3137 		/* source addr is IPv6 */
   3138 		struct ip6_hdr *ip6h;
   3139 		ip6h = mtod(in_initpkt, struct ip6_hdr *);
   3140 		sin6.sin6_addr = ip6h->ip6_src;
   3141 		sa_touse = (struct sockaddr *)&sin6;
   3142 	} else {
   3143 		return (1);
   3144 	}
   3145 
   3146 	fnd = 0;
   3147 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
   3148 		sa = (struct sockaddr *)&net->ro.ro_sa;
   3149 		if (sa->sa_family == sa_touse->sa_family) {
   3150 			if (sa->sa_family == AF_INET) {
   3151 			    sa4 = (struct sockaddr_in *)sa;
   3152 				if (sa4->sin_addr.s_addr ==
   3153 				    sin4.sin_addr.s_addr) {
   3154 					fnd = 1;
   3155 					break;
   3156 				}
   3157 			} else if (sa->sa_family == AF_INET6) {
   3158 				sa6 = (struct sockaddr_in6 *)sa;
   3159 				if (SCTP6_ARE_ADDR_EQUAL(&sa6->sin6_addr,
   3160 				    &sin6.sin6_addr)) {
   3161 					fnd = 1;
   3162 					break;
   3163 				}
   3164 			}
   3165 		}
   3166 	}
   3167 	if (fnd == 0) {
   3168 		/* New address added! no need to look futher. */
   3169 		return (1);
   3170 	}
   3171 	/* Ok so far lets munge through the rest of the packet */
   3172 	mat = in_initpkt;
   3173 	sa_touse = NULL;
   3174 	offset += sizeof(struct sctp_init_chunk);
   3175 	phdr = sctp_get_next_param(mat, offset, &params, sizeof(params));
   3176 	while (phdr) {
   3177 		ptype = ntohs(phdr->param_type);
   3178 		plen = ntohs(phdr->param_length);
   3179 		if (ptype == SCTP_IPV4_ADDRESS) {
   3180 			struct sctp_ipv4addr_param *p4, p4_buf;
   3181 
   3182 			phdr = sctp_get_next_param(mat, offset,
   3183 			    (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
   3184 			if (plen != sizeof(struct sctp_ipv4addr_param) ||
   3185 			    phdr == NULL) {
   3186                                 return (1);
   3187                         }
   3188 			p4 = (struct sctp_ipv4addr_param *)phdr;
   3189 			sin4.sin_addr.s_addr = p4->addr;
   3190 			sa_touse = (struct sockaddr *)&sin4;
   3191 		} else if (ptype == SCTP_IPV6_ADDRESS) {
   3192 			struct sctp_ipv6addr_param *p6, p6_buf;
   3193 
   3194 			phdr = sctp_get_next_param(mat, offset,
   3195 			    (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
   3196 			if (plen != sizeof(struct sctp_ipv6addr_param) ||
   3197 			    phdr == NULL) {
   3198                                 return (1);
   3199                         }
   3200 			p6 = (struct sctp_ipv6addr_param *)phdr;
   3201 			memcpy((void *)&sin6.sin6_addr, p6->addr,
   3202 			    sizeof(p6->addr));
   3203 			sa_touse = (struct sockaddr *)&sin4;
   3204 		}
   3205 
   3206 		if (sa_touse) {
   3207 			/* ok, sa_touse points to one to check */
   3208 			fnd = 0;
   3209 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
   3210 				sa = (struct sockaddr *)&net->ro.ro_sa;
   3211 				if (sa->sa_family != sa_touse->sa_family) {
   3212 					continue;
   3213 				}
   3214 				if (sa->sa_family == AF_INET) {
   3215 					sa4 = (struct sockaddr_in *)sa;
   3216 					if (sa4->sin_addr.s_addr ==
   3217 					    sin4.sin_addr.s_addr) {
   3218 						fnd = 1;
   3219 						break;
   3220 					}
   3221 				} else if (sa->sa_family == AF_INET6) {
   3222 					sa6 = (struct sockaddr_in6 *)sa;
   3223 					if (SCTP6_ARE_ADDR_EQUAL(
   3224 					    &sa6->sin6_addr, &sin6.sin6_addr)) {
   3225 						fnd = 1;
   3226 						break;
   3227 					}
   3228 				}
   3229 			}
   3230 			if (!fnd) {
   3231 				/* New addr added! no need to look further */
   3232 				return (1);
   3233 			}
   3234 		}
   3235 		offset += SCTP_SIZE32(plen);
   3236 		phdr = sctp_get_next_param(mat, offset, &params, sizeof(params));
   3237 	}
   3238 	return (0);
   3239 }
   3240 
   3241 /*
   3242  * Given a MBUF chain that was sent into us containing an
   3243  * INIT. Build a INIT-ACK with COOKIE and send back.
   3244  * We assume that the in_initpkt has done a pullup to
   3245  * include IPv6/4header, SCTP header and initial part of
   3246  * INIT message (i.e. the struct sctp_init_msg).
   3247  */
   3248 void
   3249 sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
   3250     struct mbuf *init_pkt, int iphlen, int offset, struct sctphdr *sh,
   3251     struct sctp_init_chunk *init_chk)
   3252 {
   3253 	struct sctp_association *asoc;
   3254 	struct mbuf *m, *m_at, *m_tmp, *m_cookie, *op_err, *m_last;
   3255 	struct sctp_init_msg *initackm_out;
   3256 	struct sctp_ecn_supported_param *ecn;
   3257 	struct sctp_prsctp_supported_param *prsctp;
   3258 	struct sctp_ecn_nonce_supported_param *ecn_nonce;
   3259 	struct sctp_supported_chunk_types_param *pr_supported;
   3260 	struct sockaddr_storage store;
   3261 	struct sockaddr_in *sin;
   3262 	struct sockaddr_in6 *sin6;
   3263 	struct route *ro;
   3264 	struct ip *iph;
   3265 	struct ip6_hdr *ip6;
   3266 	const struct sockaddr *to;
   3267 	struct sctp_state_cookie stc;
   3268 	struct sctp_nets *net=NULL;
   3269 	int cnt_inits_to=0;
   3270 	uint16_t his_limit, i_want;
   3271 	int abort_flag, padval, sz_of;
   3272 	struct rtentry *rt;
   3273 
   3274 	if (stcb) {
   3275 		asoc = &stcb->asoc;
   3276 	} else {
   3277 		asoc = NULL;
   3278 	}
   3279 	m_last = NULL;
   3280 	if ((asoc != NULL) &&
   3281 	    (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
   3282 	    (sctp_are_there_new_addresses(asoc, init_pkt, iphlen, offset))) {
   3283 		/* new addresses, out of here in non-cookie-wait states */
   3284 		/*
   3285 		 * Send a ABORT, we don't add the new address error clause though
   3286 		 * we even set the T bit and copy in the 0 tag.. this looks no
   3287 		 * different than if no listner was present.
   3288 		 */
   3289 		sctp_send_abort(init_pkt, iphlen, sh, 0, NULL);
   3290 		return;
   3291 	}
   3292 	abort_flag = 0;
   3293 	op_err = sctp_arethere_unrecognized_parameters(init_pkt,
   3294 	    (offset+sizeof(struct sctp_init_chunk)),
   3295 	    &abort_flag, (struct sctp_chunkhdr *)init_chk);
   3296 	if (abort_flag) {
   3297 		sctp_send_abort(init_pkt, iphlen, sh, init_chk->init.initiate_tag, op_err);
   3298 		return;
   3299 	}
   3300 	MGETHDR(m, M_DONTWAIT, MT_HEADER);
   3301 	if (m == NULL) {
   3302 		/* No memory, INIT timer will re-attempt. */
   3303 		if (op_err)
   3304 			sctp_m_freem(op_err);
   3305 		return;
   3306 	}
   3307 	MCLGET(m, M_DONTWAIT);
   3308 	if ((m->m_flags & M_EXT) != M_EXT) {
   3309 		/* Failed to get cluster buffer */
   3310 		if (op_err)
   3311 			sctp_m_freem(op_err);
   3312 		sctp_m_freem(m);
   3313 		return;
   3314 	}
   3315 	m->m_data += SCTP_MIN_OVERHEAD;
   3316 	m_reset_rcvif(m);
   3317 	m->m_len = sizeof(struct sctp_init_msg);
   3318 
   3319 	/* the time I built cookie */
   3320 	SCTP_GETTIME_TIMEVAL(&stc.time_entered);
   3321 
   3322 	/* populate any tie tags */
   3323 	if (asoc != NULL) {
   3324 		/* unlock before tag selections */
   3325 		SCTP_TCB_UNLOCK(stcb);
   3326 		if (asoc->my_vtag_nonce == 0)
   3327 			asoc->my_vtag_nonce = sctp_select_a_tag(inp);
   3328 		stc.tie_tag_my_vtag = asoc->my_vtag_nonce;
   3329 
   3330 		if (asoc->peer_vtag_nonce == 0)
   3331 			asoc->peer_vtag_nonce = sctp_select_a_tag(inp);
   3332 		stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce;
   3333 
   3334 		stc.cookie_life = asoc->cookie_life;
   3335 		net = asoc->primary_destination;
   3336 		/* now we must relock */
   3337 		SCTP_INP_RLOCK(inp);
   3338 		/* we may be in trouble here if the inp got freed
   3339 		 * most likely this set of tests will protect
   3340 		 * us but there is a chance not.
   3341 		 */
   3342 		if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
   3343 			if (op_err)
   3344 				sctp_m_freem(op_err);
   3345 			sctp_m_freem(m);
   3346 			sctp_send_abort(init_pkt, iphlen, sh, 0, NULL);
   3347 			return;
   3348 		}
   3349 		SCTP_TCB_LOCK(stcb);
   3350 		SCTP_INP_RUNLOCK(stcb->sctp_ep);
   3351 	} else {
   3352 		stc.tie_tag_my_vtag = 0;
   3353 		stc.tie_tag_peer_vtag = 0;
   3354 		/* life I will award this cookie */
   3355 		stc.cookie_life = inp->sctp_ep.def_cookie_life;
   3356 	}
   3357 
   3358 	/* copy in the ports for later check */
   3359 	stc.myport = sh->dest_port;
   3360 	stc.peerport = sh->src_port;
   3361 
   3362 	/*
   3363 	 * If we wanted to honor cookie life extentions, we would add
   3364 	 * to stc.cookie_life. For now we should NOT honor any extension
   3365 	 */
   3366 	stc.site_scope = stc.local_scope = stc.loopback_scope = 0;
   3367 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
   3368 		struct inpcb *in_inp;
   3369 		/* Its a V6 socket */
   3370 		in_inp = (struct inpcb *)inp;
   3371 		stc.ipv6_addr_legal = 1;
   3372 		/* Now look at the binding flag to see if V4 will be legal */
   3373 		if (
   3374 #if defined(__FreeBSD__) || defined(__APPLE__)
   3375 		    (in_inp->inp_flags & IN6P_IPV6_V6ONLY)
   3376 #elif defined(__OpenBSD__)
   3377 		    (0)	/* For openbsd we do dual bind only */
   3378 #else
   3379 		    (((struct in6pcb *)in_inp)->in6p_flags & IN6P_IPV6_V6ONLY)
   3380 #endif
   3381 		    == 0) {
   3382 			stc.ipv4_addr_legal = 1;
   3383 		} else {
   3384 			/* V4 addresses are NOT legal on the association */
   3385 			stc.ipv4_addr_legal = 0;
   3386 		}
   3387 	} else {
   3388 		/* Its a V4 socket, no - V6 */
   3389 		stc.ipv4_addr_legal = 1;
   3390 		stc.ipv6_addr_legal = 0;
   3391 	}
   3392 
   3393 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
   3394 	stc.ipv4_scope = 1;
   3395 #else
   3396 	stc.ipv4_scope = 0;
   3397 #endif
   3398 	/* now for scope setup */
   3399 	memset((void *)&store, 0, sizeof(store));
   3400 	sin = (struct sockaddr_in *)&store;
   3401 	sin6 = (struct sockaddr_in6 *)&store;
   3402 	if (net == NULL) {
   3403 		to = (struct sockaddr *)&store;
   3404 		iph = mtod(init_pkt, struct ip *);
   3405 		if (iph->ip_v == IPVERSION) {
   3406 			struct in_addr addr;
   3407 			static struct route iproute;
   3408 
   3409 			sin->sin_family = AF_INET;
   3410 			sin->sin_len = sizeof(struct sockaddr_in);
   3411 			sin->sin_port = sh->src_port;
   3412 			sin->sin_addr = iph->ip_src;
   3413 			/* lookup address */
   3414 			stc.address[0] = sin->sin_addr.s_addr;
   3415 			stc.address[1] = 0;
   3416 			stc.address[2] = 0;
   3417 			stc.address[3] = 0;
   3418 			stc.addr_type = SCTP_IPV4_ADDRESS;
   3419 			/* local from address */
   3420 			memset(&iproute, 0, sizeof(iproute));
   3421 			ro = &iproute;
   3422 
   3423 			/* XXX */
   3424 			rt = rtcache_lookup(ro, (struct sockaddr *) sin);
   3425 			rtcache_unref(rt, ro);
   3426 			addr = sctp_ipv4_source_address_selection(inp, NULL,
   3427 			    ro, NULL, 0);
   3428 			stc.laddress[0] = addr.s_addr;
   3429 			stc.laddress[1] = 0;
   3430 			stc.laddress[2] = 0;
   3431 			stc.laddress[3] = 0;
   3432 			stc.laddr_type = SCTP_IPV4_ADDRESS;
   3433 			/* scope_id is only for v6 */
   3434 			stc.scope_id = 0;
   3435 #ifndef SCTP_DONT_DO_PRIVADDR_SCOPE
   3436 			if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
   3437 				stc.ipv4_scope = 1;
   3438 			}
   3439 #else
   3440 			stc.ipv4_scope = 1;
   3441 #endif /* SCTP_DONT_DO_PRIVADDR_SCOPE */
   3442 			/* Must use the address in this case */
   3443 			if (sctp_is_address_on_local_host((struct sockaddr *)sin)) {
   3444 				stc.loopback_scope = 1;
   3445 				stc.ipv4_scope = 1;
   3446 				stc.site_scope = 1;
   3447 				stc.local_scope = 1;
   3448 			}
   3449 		} else if (iph->ip_v == (IPV6_VERSION >> 4)) {
   3450 			struct in6_addr addr;
   3451                         static struct route iproute6;
   3452 			ip6 = mtod(init_pkt, struct ip6_hdr *);
   3453 			sin6->sin6_family = AF_INET6;
   3454 			sin6->sin6_len = sizeof(struct sockaddr_in6);
   3455 			sin6->sin6_port = sh->src_port;
   3456 			sin6->sin6_addr = ip6->ip6_src;
   3457 			/* lookup address */
   3458 			memcpy(&stc.address, &sin6->sin6_addr,
   3459 			    sizeof(struct in6_addr));
   3460 			sin6->sin6_scope_id = 0;
   3461 			stc.addr_type = SCTP_IPV6_ADDRESS;
   3462 			stc.scope_id = 0;
   3463 			if (sctp_is_address_on_local_host((struct sockaddr *)sin6)) {
   3464 				stc.loopback_scope = 1;
   3465 				stc.local_scope = 1;
   3466 				stc.site_scope = 1;
   3467 				stc.ipv4_scope = 1;
   3468 			} else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
   3469 				/*
   3470 				 * If the new destination is a LINK_LOCAL
   3471 				 * we must have common both site and local
   3472 				 * scope. Don't set local scope though since
   3473 				 * we must depend on the source to be added
   3474 				 * implicitly. We cannot assure just because
   3475 				 * we share one link that all links are common.
   3476 				 *
   3477 				 * XXX: never treat link-local case explicitly.
   3478 				 * Use general routines defined in scope6.c.
   3479 				 * (jinmei@kame)
   3480 				 */
   3481 				stc.local_scope = 0;
   3482 				stc.site_scope = 1;
   3483  				stc.ipv4_scope = 1;
   3484 				/* we start counting for the private
   3485 				 * address stuff at 1. since the link
   3486 				 * local we source from won't show
   3487 				 * up in our scoped count.
   3488 				 */
   3489 				cnt_inits_to=1;
   3490 				/* pull out the scope_id from incoming pkt */
   3491 #if defined(SCTP_BASE_FREEBSD) || defined(__APPLE__)
   3492 				(void)in6_recoverscope(sin6, &in6_src,
   3493 				    m_get_rcvif_NOMPSAFE(init_pkt));
   3494 				in6_embedscope(&sin6->sin6_addr, sin6, NULL,
   3495 				    NULL);
   3496 #else
   3497 				(void)sa6_recoverscope(sin6);
   3498 #endif
   3499 				stc.scope_id = sin6->sin6_scope_id;
   3500 
   3501 			} else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
   3502 				/*
   3503 				 * If the new destination is SITE_LOCAL
   3504 				 * then we must have site scope in common.
   3505 				 */
   3506 				stc.site_scope = 1;
   3507 			}
   3508 			/* local from address */
   3509 			memset(&iproute6, 0, sizeof(iproute6));
   3510 			ro = (struct route *)&iproute6;
   3511 			/* XXX */
   3512 			rt = rtcache_lookup(ro, (struct sockaddr *) sin6);
   3513 			rtcache_unref(rt, ro);
   3514 			addr = sctp_ipv6_source_address_selection(inp, NULL,
   3515 			    ro, NULL, 0);
   3516 			memcpy(&stc.laddress, &addr, sizeof(struct in6_addr));
   3517 			stc.laddr_type = SCTP_IPV6_ADDRESS;
   3518 		}
   3519 	} else {
   3520 		/* set the scope per the existing tcb */
   3521 		struct sctp_nets *lnet;
   3522 
   3523 		stc.loopback_scope = asoc->loopback_scope;
   3524 		stc.ipv4_scope = asoc->ipv4_local_scope;
   3525 		stc.site_scope = asoc->site_scope;
   3526 		stc.local_scope = asoc->local_scope;
   3527 		TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
   3528 			if (rtcache_getdst(&lnet->ro)->sa_family == AF_INET6) {
   3529 				if (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr *) rtcache_getdst(&lnet->ro)->sa_data)) {
   3530 					/* if we have a LL address, start counting
   3531 					 * at 1.
   3532 					 */
   3533  					cnt_inits_to = 1;
   3534 				}
   3535 			}
   3536 		}
   3537 
   3538 		/* use the net pointer */
   3539 		to = rtcache_getdst(&net->ro);
   3540 		if (to->sa_family == AF_INET) {
   3541 			memcpy(&stc.address[0], to, sizeof(struct in_addr));
   3542 			stc.address[1] = 0;
   3543 			stc.address[2] = 0;
   3544 			stc.address[3] = 0;
   3545 			stc.addr_type = SCTP_IPV4_ADDRESS;
   3546 			if (net->src_addr_selected == 0) {
   3547 				/* strange case here, the INIT
   3548 				 * should have did the selection.
   3549 				 */
   3550 				net->_s_addr.sin.sin_addr =
   3551 				    sctp_ipv4_source_address_selection(inp,
   3552 				    stcb, &net->ro, net, 0);
   3553 				net->src_addr_selected = 1;
   3554 
   3555 			}
   3556 
   3557 			stc.laddress[0] = net->_s_addr.sin.sin_addr.s_addr;
   3558 			stc.laddress[1] = 0;
   3559 			stc.laddress[2] = 0;
   3560 			stc.laddress[3] = 0;
   3561 			stc.laddr_type = SCTP_IPV4_ADDRESS;
   3562 		} else if (to->sa_family == AF_INET6) {
   3563 			memcpy(&stc.address, &to->sa_data,
   3564 			    sizeof(struct in6_addr));
   3565 			stc.addr_type = SCTP_IPV6_ADDRESS;
   3566 			if (net->src_addr_selected == 0) {
   3567 				/* strange case here, the INIT
   3568 				 * should have did the selection.
   3569 				 */
   3570 				net->_s_addr.sin6.sin6_addr =
   3571 				    sctp_ipv6_source_address_selection(inp,
   3572 				    stcb, &net->ro, net, 0);
   3573 				net->src_addr_selected = 1;
   3574 			}
   3575 			memcpy(&stc.laddress, &net->_s_addr.sin6.sin6_addr,
   3576 			    sizeof(struct in6_addr));
   3577 			stc.laddr_type = SCTP_IPV6_ADDRESS;
   3578 		}
   3579 	}
   3580 	/* Now lets put the SCTP header in place */
   3581 	initackm_out = mtod(m, struct sctp_init_msg *);
   3582 	initackm_out->sh.src_port = inp->sctp_lport;
   3583 	initackm_out->sh.dest_port = sh->src_port;
   3584 	initackm_out->sh.v_tag = init_chk->init.initiate_tag;
   3585 	/* Save it off for quick ref */
   3586 	stc.peers_vtag = init_chk->init.initiate_tag;
   3587 	initackm_out->sh.checksum = 0;	/* calculate later */
   3588 	/* who are we */
   3589 	strncpy(stc.identification, SCTP_VERSION_STRING,
   3590 	   min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification)));
   3591 	/* now the chunk header */
   3592 	initackm_out->msg.ch.chunk_type = SCTP_INITIATION_ACK;
   3593 	initackm_out->msg.ch.chunk_flags = 0;
   3594 	/* fill in later from mbuf we build */
   3595 	initackm_out->msg.ch.chunk_length = 0;
   3596 	/* place in my tag */
   3597 	if ((asoc != NULL) &&
   3598 	    ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
   3599 	     (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED))) {
   3600 		/* re-use the v-tags and init-seq here */
   3601 		initackm_out->msg.init.initiate_tag = htonl(asoc->my_vtag);
   3602 		initackm_out->msg.init.initial_tsn = htonl(asoc->init_seq_number);
   3603 	} else {
   3604 		initackm_out->msg.init.initiate_tag = htonl(sctp_select_a_tag(inp));
   3605 		/* get a TSN to use too */
   3606 		initackm_out->msg.init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
   3607 	}
   3608 	/* save away my tag to */
   3609 	stc.my_vtag = initackm_out->msg.init.initiate_tag;
   3610 
   3611 	/* set up some of the credits. */
   3612 	initackm_out->msg.init.a_rwnd = htonl(max(inp->sctp_socket->so_rcv.sb_hiwat, SCTP_MINIMAL_RWND));
   3613 	/* set what I want */
   3614 	his_limit = ntohs(init_chk->init.num_inbound_streams);
   3615 	/* choose what I want */
   3616 	if (asoc != NULL) {
   3617 		if (asoc->streamoutcnt > inp->sctp_ep.pre_open_stream_count) {
   3618 			i_want = asoc->streamoutcnt;
   3619 		} else {
   3620 			i_want = inp->sctp_ep.pre_open_stream_count;
   3621 		}
   3622 	} else {
   3623 		i_want = inp->sctp_ep.pre_open_stream_count;
   3624 	}
   3625 	if (his_limit < i_want) {
   3626 		/* I Want more :< */
   3627 		initackm_out->msg.init.num_outbound_streams = init_chk->init.num_inbound_streams;
   3628 	} else {
   3629 		/* I can have what I want :> */
   3630 		initackm_out->msg.init.num_outbound_streams = htons(i_want);
   3631 	}
   3632 	/* tell him his limt. */
   3633 	initackm_out->msg.init.num_inbound_streams =
   3634 	    htons(inp->sctp_ep.max_open_streams_intome);
   3635 	/* setup the ECN pointer */
   3636 
   3637 /*	if (inp->sctp_flags & SCTP_PCB_FLAGS_ADAPTIONEVNT) {*/
   3638 	if (inp->sctp_ep.adaption_layer_indicator) {
   3639 		struct sctp_adaption_layer_indication *ali;
   3640 		ali = (struct sctp_adaption_layer_indication *)(
   3641 		    (vaddr_t)initackm_out + sizeof(*initackm_out));
   3642 		ali->ph.param_type = htons(SCTP_ULP_ADAPTION);
   3643 		ali->ph.param_length = htons(sizeof(*ali));
   3644 		ali->indication = ntohl(inp->sctp_ep.adaption_layer_indicator);
   3645 		m->m_len += sizeof(*ali);
   3646 		ecn = (struct sctp_ecn_supported_param *)((vaddr_t)ali +
   3647 		    sizeof(*ali));
   3648 	} else {
   3649 		ecn = (struct sctp_ecn_supported_param*)(
   3650 		    (vaddr_t)initackm_out + sizeof(*initackm_out));
   3651 	}
   3652 
   3653 	/* ECN parameter */
   3654 	if (sctp_ecn == 1) {
   3655 		ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
   3656 		ecn->ph.param_length = htons(sizeof(*ecn));
   3657 		m->m_len += sizeof(*ecn);
   3658 
   3659 		prsctp = (struct sctp_prsctp_supported_param *)((vaddr_t)ecn +
   3660 		    sizeof(*ecn));
   3661 	} else {
   3662 		prsctp = (struct sctp_prsctp_supported_param *)((vaddr_t)ecn);
   3663 	}
   3664 	/* And now tell the peer we do  pr-sctp */
   3665 	prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
   3666 	prsctp->ph.param_length = htons(sizeof(*prsctp));
   3667 	m->m_len += sizeof(*prsctp);
   3668 
   3669 
   3670 	/* And now tell the peer we do all the extensions */
   3671 	pr_supported = (struct sctp_supported_chunk_types_param *)((vaddr_t)prsctp +
   3672 	   sizeof(*prsctp));
   3673 
   3674 	pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
   3675 	pr_supported->ph.param_length = htons(sizeof(*pr_supported) + SCTP_EXT_COUNT);
   3676 	pr_supported->chunk_types[0] = SCTP_ASCONF;
   3677 	pr_supported->chunk_types[1] = SCTP_ASCONF_ACK;
   3678 	pr_supported->chunk_types[2] = SCTP_FORWARD_CUM_TSN;
   3679 	pr_supported->chunk_types[3] = SCTP_PACKET_DROPPED;
   3680 	pr_supported->chunk_types[4] = SCTP_STREAM_RESET;
   3681 	pr_supported->chunk_types[5] = 0; /* pad */
   3682 	pr_supported->chunk_types[6] = 0; /* pad */
   3683 	pr_supported->chunk_types[7] = 0; /* pad */
   3684 
   3685 	m->m_len += (sizeof(*pr_supported) + SCTP_EXT_COUNT + SCTP_PAD_EXT_COUNT);
   3686 	if (sctp_ecn_nonce) {
   3687 		/* ECN nonce: And now tell the peer we support ECN nonce */
   3688 		ecn_nonce = (struct sctp_ecn_nonce_supported_param *)((vaddr_t)pr_supported +
   3689 		     sizeof(*pr_supported) + SCTP_EXT_COUNT + SCTP_PAD_EXT_COUNT);
   3690 		ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
   3691 		ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
   3692 		m->m_len += sizeof(*ecn_nonce);
   3693 	}
   3694 
   3695 	m_at = m;
   3696 	/* now the addresses */
   3697 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
   3698 		struct ifnet *ifn;
   3699 		struct ifaddr *ifa;
   3700 		int cnt = cnt_inits_to;
   3701 		int s;
   3702 
   3703 		s = pserialize_read_enter();
   3704 		IFNET_READER_FOREACH(ifn) {
   3705 			if ((stc.loopback_scope == 0) &&
   3706 			    (ifn->if_type == IFT_LOOP)) {
   3707 				/*
   3708 				 * Skip loopback devices if loopback_scope
   3709 				 * not set
   3710 				 */
   3711 				continue;
   3712 			}
   3713 			IFADDR_READER_FOREACH(ifa, ifn) {
   3714 				if (sctp_is_address_in_scope(ifa,
   3715 				    stc.ipv4_addr_legal, stc.ipv6_addr_legal,
   3716 				    stc.loopback_scope, stc.ipv4_scope,
   3717 				    stc.local_scope, stc.site_scope) == 0) {
   3718 					continue;
   3719 				}
   3720 				cnt++;
   3721 			}
   3722 		}
   3723 		pserialize_read_exit(s);
   3724 
   3725 		if (cnt > 1) {
   3726 			s = pserialize_read_enter();
   3727 			IFNET_READER_FOREACH(ifn) {
   3728 				if ((stc.loopback_scope == 0) &&
   3729 				    (ifn->if_type == IFT_LOOP)) {
   3730 					/*
   3731 					 * Skip loopback devices if
   3732 					 * loopback_scope not set
   3733 					 */
   3734 					continue;
   3735 				}
   3736 				IFADDR_READER_FOREACH(ifa, ifn) {
   3737 					if (sctp_is_address_in_scope(ifa,
   3738 					    stc.ipv4_addr_legal,
   3739 					    stc.ipv6_addr_legal,
   3740 					    stc.loopback_scope, stc.ipv4_scope,
   3741 					    stc.local_scope, stc.site_scope) == 0) {
   3742 						continue;
   3743 					}
   3744 					m_at = sctp_add_addr_to_mbuf(m_at, ifa);
   3745 				}
   3746 			}
   3747 			pserialize_read_exit(s);
   3748 		}
   3749 	} else {
   3750 		struct sctp_laddr *laddr;
   3751 		int cnt;
   3752 		cnt = cnt_inits_to;
   3753 		/* First, how many ? */
   3754 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
   3755 			if (laddr->ifa == NULL) {
   3756 				continue;
   3757 			}
   3758 			if (laddr->ifa->ifa_addr == NULL)
   3759 				continue;
   3760 			if (sctp_is_address_in_scope(laddr->ifa,
   3761 			    stc.ipv4_addr_legal, stc.ipv6_addr_legal,
   3762 			    stc.loopback_scope, stc.ipv4_scope,
   3763 			    stc.local_scope, stc.site_scope) == 0) {
   3764 				continue;
   3765 			}
   3766 			cnt++;
   3767 		}
   3768 		/* If we bind a single address only we won't list
   3769 		 * any. This way you can get through a NAT
   3770 		 */
   3771 		if (cnt > 1) {
   3772 			LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
   3773 				if (laddr->ifa == NULL) {
   3774 #ifdef SCTP_DEBUG
   3775 					if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   3776 						printf("Help I have fallen and I can't get up!\n");
   3777 					}
   3778 #endif
   3779 					continue;
   3780 				}
   3781 				if (laddr->ifa->ifa_addr == NULL)
   3782 					continue;
   3783 				if (sctp_is_address_in_scope(laddr->ifa,
   3784 				    stc.ipv4_addr_legal, stc.ipv6_addr_legal,
   3785 				    stc.loopback_scope, stc.ipv4_scope,
   3786 				    stc.local_scope, stc.site_scope) == 0) {
   3787 					continue;
   3788 				}
   3789 				m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa);
   3790 			}
   3791 		}
   3792 	}
   3793 
   3794 	/* tack on the operational error if present */
   3795 	if (op_err) {
   3796 		if (op_err->m_pkthdr.len % 4) {
   3797 			/* must add a pad to the param */
   3798 			u_int32_t cpthis=0;
   3799 			int padlen;
   3800 			padlen = 4 - (op_err->m_pkthdr.len % 4);
   3801 			m_copyback(op_err, op_err->m_pkthdr.len, padlen, (void *)&cpthis);
   3802 		}
   3803 		while (m_at->m_next != NULL) {
   3804 			m_at = m_at->m_next;
   3805 		}
   3806 		m_at->m_next = op_err;
   3807 		while (m_at->m_next != NULL) {
   3808 			m_at = m_at->m_next;
   3809 		}
   3810 	}
   3811 	/* Get total size of init packet */
   3812 	sz_of = SCTP_SIZE32(ntohs(init_chk->ch.chunk_length));
   3813 	/* pre-calulate the size and update pkt header and chunk header */
   3814 	m->m_pkthdr.len = 0;
   3815 	for (m_tmp = m; m_tmp; m_tmp = m_tmp->m_next) {
   3816 		m->m_pkthdr.len += m_tmp->m_len;
   3817 		if (m_tmp->m_next == NULL) {
   3818 			/* m_tmp should now point to last one */
   3819 			break;
   3820 		}
   3821 	}
   3822 	/*
   3823 	 * Figure now the size of the cookie. We know the size of the
   3824 	 * INIT-ACK. The Cookie is going to be the size of INIT, INIT-ACK,
   3825 	 * COOKIE-STRUCTURE and SIGNATURE.
   3826 	 */
   3827 
   3828 	/*
   3829 	 * take our earlier INIT calc and add in the sz we just calculated
   3830 	 * minus the size of the sctphdr (its not included in chunk size
   3831 	 */
   3832 
   3833 	/* add once for the INIT-ACK */
   3834 	sz_of += (m->m_pkthdr.len - sizeof(struct sctphdr));
   3835 
   3836 	/* add a second time for the INIT-ACK in the cookie */
   3837 	sz_of += (m->m_pkthdr.len - sizeof(struct sctphdr));
   3838 
   3839 	/* Now add the cookie header and cookie message struct */
   3840 	sz_of += sizeof(struct sctp_state_cookie_param);
   3841 	/* ...and add the size of our signature */
   3842 	sz_of += SCTP_SIGNATURE_SIZE;
   3843 	initackm_out->msg.ch.chunk_length = htons(sz_of);
   3844 
   3845 	/* Now we must build a cookie */
   3846 	m_cookie = sctp_add_cookie(inp, init_pkt, offset, m,
   3847 	    sizeof(struct sctphdr), &stc);
   3848 	if (m_cookie == NULL) {
   3849 		/* memory problem */
   3850 		sctp_m_freem(m);
   3851 		return;
   3852 	}
   3853 	/* Now append the cookie to the end and update the space/size */
   3854 	m_tmp->m_next = m_cookie;
   3855 
   3856 	/*
   3857 	 * We pass 0 here to NOT set IP_DF if its IPv4, we ignore the
   3858 	 * return here since the timer will drive a retranmission.
   3859 	 */
   3860 	padval = m->m_pkthdr.len % 4;
   3861 	if ((padval) && (m_last)) {
   3862 		/* see my previous comments on m_last */
   3863 		int ret;
   3864 		ret = sctp_add_pad_tombuf(m_last, (4-padval));
   3865 		if (ret) {
   3866 			/* Houston we have a problem, no space */
   3867 			sctp_m_freem(m);
   3868 			return;
   3869 		}
   3870 		m->m_pkthdr.len += padval;
   3871 	}
   3872 	sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, 0, NULL, 0);
   3873 }
   3874 
   3875 
   3876 static void
   3877 sctp_insert_on_wheel(struct sctp_association *asoc,
   3878 		     struct sctp_stream_out *strq)
   3879 {
   3880 	struct sctp_stream_out *stre, *strn;
   3881 	stre = TAILQ_FIRST(&asoc->out_wheel);
   3882 	if (stre == NULL) {
   3883 		/* only one on wheel */
   3884 		TAILQ_INSERT_HEAD(&asoc->out_wheel, strq, next_spoke);
   3885 		return;
   3886 	}
   3887 	for (; stre; stre = strn) {
   3888 		strn = TAILQ_NEXT(stre, next_spoke);
   3889 		if (stre->stream_no > strq->stream_no) {
   3890 			TAILQ_INSERT_BEFORE(stre, strq, next_spoke);
   3891 			return;
   3892 		} else if (stre->stream_no == strq->stream_no) {
   3893 			/* huh, should not happen */
   3894 			return;
   3895 		} else if (strn == NULL) {
   3896 			/* next one is null */
   3897 			TAILQ_INSERT_AFTER(&asoc->out_wheel, stre, strq,
   3898 					   next_spoke);
   3899 		}
   3900 	}
   3901 }
   3902 
   3903 static void
   3904 sctp_remove_from_wheel(struct sctp_association *asoc,
   3905 		       struct sctp_stream_out *strq)
   3906 {
   3907 	/* take off and then setup so we know it is not on the wheel */
   3908 	TAILQ_REMOVE(&asoc->out_wheel, strq, next_spoke);
   3909 	strq->next_spoke.tqe_next = NULL;
   3910 	strq->next_spoke.tqe_prev = NULL;
   3911 }
   3912 
   3913 
   3914 static void
   3915 sctp_prune_prsctp(struct sctp_tcb *stcb,
   3916 		  struct sctp_association *asoc,
   3917 		  struct sctp_sndrcvinfo *srcv,
   3918 		  int dataout
   3919 	)
   3920 {
   3921 	int freed_spc=0;
   3922 	struct sctp_tmit_chunk *chk, *nchk;
   3923 	if ((asoc->peer_supports_prsctp) && (asoc->sent_queue_cnt_removeable > 0)) {
   3924 		TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
   3925 			/*
   3926 			 * Look for chunks marked with the PR_SCTP
   3927 			 * flag AND the buffer space flag. If the one
   3928 			 * being sent is equal or greater priority then
   3929 			 * purge the old one and free some space.
   3930 			 */
   3931 			if ((chk->flags & (SCTP_PR_SCTP_ENABLED |
   3932 					   SCTP_PR_SCTP_BUFFER)) ==
   3933 			    (SCTP_PR_SCTP_ENABLED|SCTP_PR_SCTP_BUFFER)) {
   3934 				/*
   3935 				 * This one is PR-SCTP AND buffer space
   3936 				 * limited type
   3937 				 */
   3938 				if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
   3939 					/* Lower numbers equates to
   3940 					 * higher priority so if the
   3941 					 * one we are looking at has a
   3942 					 * larger or equal priority we
   3943 					 * want to drop the data and
   3944 					 * NOT retransmit it.
   3945 					 */
   3946 					if (chk->data) {
   3947 						/* We release the
   3948 						 * book_size if the
   3949 						 * mbuf is here
   3950 						 */
   3951 						int ret_spc;
   3952 						int cause;
   3953 						if (chk->sent > SCTP_DATAGRAM_UNSENT)
   3954 							cause = SCTP_RESPONSE_TO_USER_REQ|SCTP_NOTIFY_DATAGRAM_SENT;
   3955 						else
   3956 							cause = SCTP_RESPONSE_TO_USER_REQ|SCTP_NOTIFY_DATAGRAM_UNSENT;
   3957 						ret_spc  = sctp_release_pr_sctp_chunk(stcb, chk,
   3958 										      cause,
   3959 										      &asoc->sent_queue);
   3960 						freed_spc += ret_spc;
   3961 						if (freed_spc >= dataout) {
   3962 							return;
   3963 						}
   3964 					} /* if chunk was present */
   3965 				} /* if of sufficent priority */
   3966 			} /* if chunk has enabled */
   3967 		} /* tailqforeach */
   3968 
   3969 		chk = TAILQ_FIRST(&asoc->send_queue);
   3970 		while (chk) {
   3971 			nchk = TAILQ_NEXT(chk, sctp_next);
   3972 			/* Here we must move to the sent queue and mark */
   3973 			if ((chk->flags & (SCTP_PR_SCTP_ENABLED |
   3974 					   SCTP_PR_SCTP_BUFFER)) ==
   3975 			    (SCTP_PR_SCTP_ENABLED|SCTP_PR_SCTP_BUFFER)) {
   3976 				if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
   3977 					if (chk->data) {
   3978 						/* We release the
   3979 						 * book_size if the
   3980 						 * mbuf is here
   3981 						 */
   3982 						int ret_spc;
   3983 						ret_spc  = sctp_release_pr_sctp_chunk(stcb, chk,
   3984 						    SCTP_RESPONSE_TO_USER_REQ|SCTP_NOTIFY_DATAGRAM_UNSENT,
   3985 						    &asoc->send_queue);
   3986 
   3987 						freed_spc += ret_spc;
   3988 						if (freed_spc >= dataout) {
   3989 							return;
   3990 						}
   3991 					} /* end if chk->data */
   3992 				} /* end if right class */
   3993 			} /* end if chk pr-sctp */
   3994 			chk = nchk;
   3995 		} /* end while (chk) */
   3996 	} /* if enabled in asoc */
   3997 }
   3998 
   3999 static void
   4000 sctp_prepare_chunk(struct sctp_tmit_chunk *template,
   4001 		   struct sctp_tcb *stcb,
   4002 		   struct sctp_sndrcvinfo *srcv,
   4003 		   struct sctp_stream_out *strq,
   4004 		   struct sctp_nets *net)
   4005 {
   4006 	memset(template, 0, sizeof(struct sctp_tmit_chunk));
   4007 	template->sent = SCTP_DATAGRAM_UNSENT;
   4008 	if ((stcb->asoc.peer_supports_prsctp) &&
   4009 	    (srcv->sinfo_flags & (SCTP_PR_SCTP_TTL|SCTP_PR_SCTP_BUF)) &&
   4010 	    (srcv->sinfo_timetolive > 0)
   4011 		) {
   4012 		/* If:
   4013 		 *  Peer supports PR-SCTP
   4014 		 *  The flags is set against this send for PR-SCTP
   4015 		 *  And timetolive is a postive value, zero is reserved
   4016 		 *     to mean a reliable send for both buffer/time
   4017 		 *     related one.
   4018 		 */
   4019 		if (srcv->sinfo_flags & SCTP_PR_SCTP_BUF) {
   4020 			/*
   4021 			 * Time to live is a priority stored in tv_sec
   4022 			 * when doing the buffer drop thing.
   4023 			 */
   4024 			template->rec.data.timetodrop.tv_sec = srcv->sinfo_timetolive;
   4025 		} else {
   4026 			struct timeval tv;
   4027 
   4028 			SCTP_GETTIME_TIMEVAL(&template->rec.data.timetodrop);
   4029 			tv.tv_sec = srcv->sinfo_timetolive / 1000;
   4030 			tv.tv_usec = (srcv->sinfo_timetolive * 1000) % 1000000;
   4031 #ifndef __FreeBSD__
   4032 			timeradd(&template->rec.data.timetodrop, &tv,
   4033 			    &template->rec.data.timetodrop);
   4034 #else
   4035 			timevaladd(&template->rec.data.timetodrop, &tv);
   4036 #endif
   4037 		}
   4038 	}
   4039 	if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
   4040 		template->rec.data.stream_seq = strq->next_sequence_sent;
   4041 	} else {
   4042 		template->rec.data.stream_seq = 0;
   4043 	}
   4044 	template->rec.data.TSN_seq = 0;	/* not yet assigned */
   4045 
   4046 	template->rec.data.stream_number = srcv->sinfo_stream;
   4047 	template->rec.data.payloadtype = srcv->sinfo_ppid;
   4048 	template->rec.data.context = srcv->sinfo_context;
   4049 	template->rec.data.doing_fast_retransmit = 0;
   4050 	template->rec.data.ect_nonce = 0;   /* ECN Nonce */
   4051 
   4052 	if (srcv->sinfo_flags & SCTP_ADDR_OVER) {
   4053 		template->whoTo = net;
   4054 	} else {
   4055 		if (stcb->asoc.primary_destination)
   4056 			template->whoTo = stcb->asoc.primary_destination;
   4057 		else {
   4058 			/* TSNH */
   4059 			template->whoTo = net;
   4060 		}
   4061 	}
   4062 	/* the actual chunk flags */
   4063 	if (srcv->sinfo_flags & SCTP_UNORDERED) {
   4064 		template->rec.data.rcv_flags = SCTP_DATA_UNORDERED;
   4065 	} else {
   4066 		template->rec.data.rcv_flags = 0;
   4067 	}
   4068 	/* no flags yet, FRAGMENT_OK goes here */
   4069 	template->flags = 0;
   4070 	/* PR sctp flags */
   4071 	if (stcb->asoc.peer_supports_prsctp) {
   4072 		if (srcv->sinfo_timetolive > 0) {
   4073 			/*
   4074 			 * We only set the flag if timetolive (or
   4075 			 * priority) was set to a positive number.
   4076 			 * Zero is reserved specifically to be
   4077 			 * EXCLUDED and sent reliable.
   4078 			 */
   4079 			if (srcv->sinfo_flags & SCTP_PR_SCTP_TTL) {
   4080 				template->flags |= SCTP_PR_SCTP_ENABLED;
   4081 			}
   4082 			if (srcv->sinfo_flags & SCTP_PR_SCTP_BUF) {
   4083 				template->flags |= SCTP_PR_SCTP_BUFFER;
   4084 			}
   4085 		}
   4086 	}
   4087 	template->asoc = &stcb->asoc;
   4088 }
   4089 
   4090 
   4091 int
   4092 sctp_get_frag_point(struct sctp_tcb *stcb,
   4093 		    struct sctp_association *asoc)
   4094 {
   4095 	int siz, ovh;
   4096 
   4097 	/* For endpoints that have both 6 and 4 addresses
   4098 	 * we must reserver room for the 6 ip header, for
   4099 	 * those that are only dealing with V4 we use
   4100 	 * a larger frag point.
   4101 	 */
   4102  	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
   4103 		ovh = SCTP_MED_OVERHEAD;
   4104 	} else {
   4105 		ovh = SCTP_MED_V4_OVERHEAD;
   4106 	}
   4107 
   4108 	if (stcb->sctp_ep->sctp_frag_point > asoc->smallest_mtu)
   4109 		siz = asoc->smallest_mtu - ovh;
   4110 	else
   4111 		siz = (stcb->sctp_ep->sctp_frag_point - ovh);
   4112 /*
   4113   if (siz > (MCLBYTES-sizeof(struct sctp_data_chunk))) { */
   4114 		/* A data chunk MUST fit in a cluster */
   4115 /*		siz = (MCLBYTES - sizeof(struct sctp_data_chunk));*/
   4116 /*	}*/
   4117 
   4118 	if (siz % 4) {
   4119 		/* make it an even word boundary please */
   4120 		siz -= (siz % 4);
   4121 	}
   4122 	return (siz);
   4123 }
   4124 extern unsigned int sctp_max_chunks_on_queue;
   4125 
   4126 #define   SBLOCKWAIT(f)   (((f)&MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
   4127 
   4128 static int
   4129 sctp_msg_append(struct sctp_tcb *stcb,
   4130 		struct sctp_nets *net,
   4131 		struct mbuf *m,
   4132 		struct sctp_sndrcvinfo *srcv,
   4133 		int flags)
   4134 {
   4135 	struct socket *so;
   4136 	struct sctp_association *asoc;
   4137 	struct sctp_stream_out *strq;
   4138 	struct sctp_tmit_chunk *chk;
   4139 	struct sctpchunk_listhead tmp;
   4140 	struct sctp_tmit_chunk template;
   4141 	struct mbuf *n, *mnext;
   4142 	struct mbuf *mm;
   4143 	unsigned int dataout, siz;
   4144 	int mbcnt = 0;
   4145 	int mbcnt_e = 0;
   4146 	int error = 0;
   4147 
   4148 	if ((stcb == NULL) || (net == NULL) || (m == NULL) || (srcv == NULL)) {
   4149 		/* Software fault, you blew it on the call */
   4150 #ifdef SCTP_DEBUG
   4151 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   4152 			printf("software error in sctp_msg_append:1\n");
   4153 			printf("stcb:%p net:%p m:%p srcv:%p\n",
   4154 			       stcb, net, m, srcv);
   4155 		}
   4156 #endif
   4157 		if (m)
   4158 			sctp_m_freem(m);
   4159 		return (EFAULT);
   4160 	}
   4161 	so = stcb->sctp_socket;
   4162 	asoc = &stcb->asoc;
   4163 	if (srcv->sinfo_flags & SCTP_ABORT) {
   4164 		if ((SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
   4165 		    (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_ECHOED)) {
   4166 			/* It has to be up before we abort */
   4167 			/* how big is the user initiated abort? */
   4168 			if ((m->m_flags & M_PKTHDR) && (m->m_pkthdr.len)) {
   4169 				dataout = m->m_pkthdr.len;
   4170 			} else {
   4171 				/* we must count */
   4172 				dataout = 0;
   4173 				for (n = m; n; n = n->m_next) {
   4174 					dataout += n->m_len;
   4175 				}
   4176 			}
   4177 			M_PREPEND(m, sizeof(struct sctp_paramhdr), M_DONTWAIT);
   4178 			if (m) {
   4179 				struct sctp_paramhdr *ph;
   4180 				m->m_len = sizeof(struct sctp_paramhdr) + dataout;
   4181 				ph = mtod(m, struct sctp_paramhdr *);
   4182 				ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
   4183 				ph->param_length = htons(m->m_len);
   4184 			}
   4185 			sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, m);
   4186 			m = NULL;
   4187 		} else {
   4188 			/* Only free if we don't send an abort */
   4189 			;
   4190 		}
   4191 		goto out;
   4192 	}
   4193 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
   4194 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
   4195 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
   4196 	    (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
   4197 		/* got data while shutting down */
   4198 		error = ECONNRESET;
   4199 		goto out;
   4200 	}
   4201 
   4202 	if (srcv->sinfo_stream >= asoc->streamoutcnt) {
   4203 		/* Invalid stream number */
   4204 		error = EINVAL;
   4205 		goto out;
   4206 	}
   4207 	if (asoc->strmout == NULL) {
   4208 		/* huh? software error */
   4209 #ifdef SCTP_DEBUG
   4210 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   4211 			printf("software error in sctp_msg_append:2\n");
   4212 		}
   4213 #endif
   4214 		error = EFAULT;
   4215 		goto out;
   4216 	}
   4217 	strq = &asoc->strmout[srcv->sinfo_stream];
   4218 	/* how big is it ? */
   4219 	if ((m->m_flags & M_PKTHDR) && (m->m_pkthdr.len)) {
   4220 		dataout = m->m_pkthdr.len;
   4221 	} else {
   4222 		/* we must count */
   4223 		dataout = 0;
   4224 		for (n = m; n; n = n->m_next) {
   4225 			dataout += n->m_len;
   4226 		}
   4227 	}
   4228 #ifdef SCTP_DEBUG
   4229 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   4230 		printf("Attempt to send out %d bytes\n",
   4231 		       dataout);
   4232 	}
   4233 #endif
   4234 
   4235 	/* lock the socket buf */
   4236 	error = sblock(&so->so_snd, SBLOCKWAIT(flags));
   4237 	if (error)
   4238 		goto out_locked;
   4239 
   4240 	if (dataout > so->so_snd.sb_hiwat) {
   4241 		/* It will NEVER fit */
   4242 		error = EMSGSIZE;
   4243 		goto release;
   4244 	}
   4245 	if ((srcv->sinfo_flags & SCTP_EOF) &&
   4246 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
   4247 	    (dataout == 0)
   4248 		) {
   4249 		goto zap_by_it_all;
   4250 	}
   4251 	if ((so->so_snd.sb_hiwat <
   4252 	     (dataout + asoc->total_output_queue_size)) ||
   4253 	    (asoc->chunks_on_out_queue > sctp_max_chunks_on_queue) ||
   4254 	    (asoc->total_output_mbuf_queue_size >
   4255 	     so->so_snd.sb_mbmax)
   4256 		) {
   4257 		/* XXX Buffer space hunt for data to skip */
   4258 		if (asoc->peer_supports_prsctp) {
   4259 			sctp_prune_prsctp(stcb, asoc, srcv, dataout);
   4260 		}
   4261 		while ((so->so_snd.sb_hiwat <
   4262 		    (dataout + asoc->total_output_queue_size)) ||
   4263 		    (asoc->chunks_on_out_queue > sctp_max_chunks_on_queue) ||
   4264 		    (asoc->total_output_mbuf_queue_size >
   4265 		    so->so_snd.sb_mbmax)) {
   4266 			struct sctp_inpcb *inp;
   4267 			/* Now did we free up enough room? */
   4268 			if (so->so_state & SS_NBIO) {
   4269 				/* Non-blocking io in place */
   4270 				error = EWOULDBLOCK;
   4271 				goto release;
   4272 			}
   4273 			/*
   4274 			 * We store off a pointer to the endpoint.
   4275 			 * Since on return from this we must check to
   4276 			 * see if an so_error is set. If so we may have
   4277 			 * been reset and our stcb destroyed. Returning
   4278 			 * an error will cause the correct error return
   4279 			 * through and fix this all.
   4280 			 */
   4281 			inp = stcb->sctp_ep;
   4282 			/*
   4283 			 * Not sure how else to do this since
   4284 			 * the level we suspended at is not
   4285 			 * known deep down where we are. I will
   4286 			 * drop to spl0() so that others can
   4287 			 * get in.
   4288 			 */
   4289 
   4290 			inp->sctp_tcb_at_block = (void *)stcb;
   4291 			inp->error_on_block = 0;
   4292 			sbunlock(&so->so_snd);
   4293 			error = sbwait(&so->so_snd);
   4294 			/*
   4295 			 * XXX: This is ugly but I have
   4296 			 * recreated most of what goes on to
   4297 			 * block in the sb. UGHH
   4298 			 * May want to add the bit about being
   4299 			 * no longer connected.. but this then
   4300 			 * further dooms the UDP model NOT to
   4301 			 * allow this.
   4302 			 */
   4303 			inp->sctp_tcb_at_block = 0;
   4304 			if (inp->error_on_block)
   4305 				error = inp->error_on_block;
   4306 			if (so->so_error)
   4307 				error = so->so_error;
   4308 			if (error) {
   4309 				goto out_locked;
   4310 			}
   4311 			error = sblock(&so->so_snd, M_WAITOK);
   4312 			if (error)
   4313 				goto out_locked;
   4314 			/* Otherwise we cycle back and recheck
   4315 			 * the space
   4316 			 */
   4317 #if defined(__FreeBSD__) && __FreeBSD_version >= 502115
   4318 			if (so->so_rcv.sb_state & SBS_CANTSENDMORE) {
   4319 #else
   4320 			if (so->so_state & SS_CANTSENDMORE) {
   4321 #endif
   4322 				error = EPIPE;
   4323 				goto release;
   4324 			}
   4325 			if (so->so_error) {
   4326 				error = so->so_error;
   4327 				goto release;
   4328 			}
   4329 		}
   4330 	}
   4331 	/* If we have a packet header fix it if it was broke */
   4332 	if (m->m_flags & M_PKTHDR) {
   4333 		m->m_pkthdr.len = dataout;
   4334 	}
   4335 	/* use the smallest one, user set value or
   4336 	 * smallest mtu of the asoc
   4337 	 */
   4338 	siz = sctp_get_frag_point(stcb, asoc);
   4339 	if ((dataout) && (dataout <= siz)) {
   4340 		/* Fast path */
   4341 		chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
   4342 		if (chk == NULL) {
   4343 			error = ENOMEM;
   4344 			goto release;
   4345 		}
   4346 		sctp_prepare_chunk(chk, stcb, srcv, strq, net);
   4347 		chk->whoTo->ref_count++;
   4348 		chk->rec.data.rcv_flags |= SCTP_DATA_NOT_FRAG;
   4349 
   4350 		/* no flags yet, FRAGMENT_OK goes here */
   4351 		sctppcbinfo.ipi_count_chunk++;
   4352 		sctppcbinfo.ipi_gencnt_chunk++;
   4353 		asoc->chunks_on_out_queue++;
   4354 		chk->data = m;
   4355 		m = NULL;
   4356 		/* Total in the MSIZE */
   4357 		for (mm = chk->data; mm; mm = mm->m_next) {
   4358 			mbcnt += MSIZE;
   4359 			if (mm->m_flags & M_EXT) {
   4360 				mbcnt += chk->data->m_ext.ext_size;
   4361 			}
   4362 		}
   4363 		/* fix up the send_size if it is not present */
   4364 		chk->send_size = dataout;
   4365 		chk->book_size = chk->send_size;
   4366 		chk->mbcnt = mbcnt;
   4367 		/* ok, we are commited */
   4368 		if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
   4369 			/* bump the ssn if we are unordered. */
   4370 			strq->next_sequence_sent++;
   4371 		}
   4372 		chk->data->m_nextpkt = 0;
   4373 		asoc->stream_queue_cnt++;
   4374 		TAILQ_INSERT_TAIL(&strq->outqueue, chk, sctp_next);
   4375 		/* now check if this stream is on the wheel */
   4376 		if ((strq->next_spoke.tqe_next == NULL) &&
   4377 		    (strq->next_spoke.tqe_prev == NULL)) {
   4378 			/* Insert it on the wheel since it is not
   4379 			 * on it currently
   4380 			 */
   4381 			sctp_insert_on_wheel(asoc, strq);
   4382 		}
   4383 	} else if ((dataout) && (dataout > siz)) {
   4384 		/* Slow path */
   4385 		if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NO_FRAGMENT) &&
   4386 		    (dataout > siz)) {
   4387 			error = EMSGSIZE;
   4388 			goto release;
   4389 		}
   4390 		/* setup the template */
   4391 		sctp_prepare_chunk(&template, stcb, srcv, strq, net);
   4392 
   4393 		n = m;
   4394 		while (dataout > siz) {
   4395 			/*
   4396 			 * We can wait since this is called from the user
   4397 			 * send side
   4398 			 */
   4399 			n->m_nextpkt = m_split(n, siz, M_WAIT);
   4400 			if (n->m_nextpkt == NULL) {
   4401 				error = EFAULT;
   4402 				goto release;
   4403 			}
   4404 			dataout -= siz;
   4405 			n = n->m_nextpkt;
   4406 		}
   4407 		/*
   4408 		 * ok, now we have a chain on m where m->m_nextpkt points to
   4409 		 * the next chunk and m/m->m_next chain is the piece to send.
   4410 		 * We must go through the chains and thread them on to
   4411 		 * sctp_tmit_chunk chains and place them all on the stream
   4412 		 * queue, breaking the m->m_nextpkt pointers as we go.
   4413 		 */
   4414 		n = m;
   4415 		TAILQ_INIT(&tmp);
   4416 		while (n) {
   4417 			/*
   4418 			 * first go through and allocate a sctp_tmit chunk
   4419 			 * for each chunk piece
   4420 			 */
   4421 			chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
   4422 			if (chk == NULL) {
   4423 				/*
   4424 				 * ok we must spin through and dump anything
   4425 				 * we have allocated and then jump to the
   4426 				 * no_membad
   4427 				 */
   4428 				chk = TAILQ_FIRST(&tmp);
   4429 				while (chk) {
   4430 					TAILQ_REMOVE(&tmp, chk, sctp_next);
   4431 					SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   4432 					sctppcbinfo.ipi_count_chunk--;
   4433 					asoc->chunks_on_out_queue--;
   4434 					if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   4435 						panic("Chunk count is negative");
   4436 					}
   4437 					sctppcbinfo.ipi_gencnt_chunk++;
   4438 					chk = TAILQ_FIRST(&tmp);
   4439 				}
   4440 				error = ENOMEM;
   4441 				goto release;
   4442 			}
   4443 			sctppcbinfo.ipi_count_chunk++;
   4444 			asoc->chunks_on_out_queue++;
   4445 
   4446 			sctppcbinfo.ipi_gencnt_chunk++;
   4447 			*chk = template;
   4448 			chk->whoTo->ref_count++;
   4449 			chk->data = n;
   4450 			/* Total in the MSIZE */
   4451 			mbcnt_e = 0;
   4452 			for (mm = chk->data; mm; mm = mm->m_next) {
   4453 				mbcnt_e += MSIZE;
   4454 				if (mm->m_flags & M_EXT) {
   4455 					mbcnt_e += chk->data->m_ext.ext_size;
   4456 				}
   4457 			}
   4458 			/* now fix the chk->send_size */
   4459 			if (chk->data->m_flags & M_PKTHDR) {
   4460 				chk->send_size = chk->data->m_pkthdr.len;
   4461 			} else {
   4462 				struct mbuf *nn;
   4463 				chk->send_size = 0;
   4464 				for (nn = chk->data; nn; nn = nn->m_next) {
   4465 					chk->send_size += nn->m_len;
   4466 				}
   4467 			}
   4468 			chk->book_size = chk->send_size;
   4469 			chk->mbcnt = mbcnt_e;
   4470 			mbcnt += mbcnt_e;
   4471 			if (chk->flags & SCTP_PR_SCTP_BUFFER) {
   4472 				asoc->sent_queue_cnt_removeable++;
   4473 			}
   4474 			n = n->m_nextpkt;
   4475 			TAILQ_INSERT_TAIL(&tmp, chk, sctp_next);
   4476 		}
   4477 		m = NULL;
   4478 		/* now that we have enough space for all de-couple the
   4479 		 * chain of mbufs by going through our temp array
   4480 		 * and breaking the pointers.
   4481 		 */
   4482 		/* ok, we are commited */
   4483 		if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
   4484 			/* bump the ssn if we are unordered. */
   4485 			strq->next_sequence_sent++;
   4486 		}
   4487 		/* Mark the first/last flags. This will
   4488 		 * result int a 3 for a single item on the list
   4489 		 */
   4490 		chk = TAILQ_FIRST(&tmp);
   4491 		chk->rec.data.rcv_flags |= SCTP_DATA_FIRST_FRAG;
   4492 		chk = TAILQ_LAST(&tmp, sctpchunk_listhead);
   4493 		chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
   4494 		/* now break any chains on the queue and
   4495 		 * move it to the streams actual queue.
   4496 		 */
   4497 		chk = TAILQ_FIRST(&tmp);
   4498 		while (chk) {
   4499 			chk->data->m_nextpkt = 0;
   4500 			TAILQ_REMOVE(&tmp, chk, sctp_next);
   4501 			asoc->stream_queue_cnt++;
   4502 			TAILQ_INSERT_TAIL(&strq->outqueue, chk, sctp_next);
   4503 			chk = TAILQ_FIRST(&tmp);
   4504 		}
   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
   4509 			 * on it currently
   4510 			 */
   4511 			sctp_insert_on_wheel(asoc, strq);
   4512 		}
   4513 	}
   4514 	/* has a SHUTDOWN been (also) requested by the user on this asoc? */
   4515 zap_by_it_all:
   4516 
   4517 	if ((srcv->sinfo_flags & SCTP_EOF) &&
   4518 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
   4519 
   4520 		int some_on_streamwheel = 0;
   4521 
   4522 		if (!TAILQ_EMPTY(&asoc->out_wheel)) {
   4523 			/* Check to see if some data queued */
   4524 			struct sctp_stream_out *outs;
   4525 			TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) {
   4526 				if (!TAILQ_EMPTY(&outs->outqueue)) {
   4527 					some_on_streamwheel = 1;
   4528 					break;
   4529 				}
   4530 			}
   4531 		}
   4532 
   4533 		if (TAILQ_EMPTY(&asoc->send_queue) &&
   4534 		    TAILQ_EMPTY(&asoc->sent_queue) &&
   4535 		    (some_on_streamwheel == 0)) {
   4536 			/* there is nothing queued to send, so I'm done... */
   4537 			if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
   4538 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
   4539 				/* only send SHUTDOWN the first time through */
   4540 #ifdef SCTP_DEBUG
   4541 				if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
   4542 					printf("%s:%d sends a shutdown\n",
   4543 					       __FILE__,
   4544 					       __LINE__
   4545 						);
   4546 				}
   4547 #endif
   4548 				sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
   4549 				asoc->state = SCTP_STATE_SHUTDOWN_SENT;
   4550 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
   4551 						 asoc->primary_destination);
   4552 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
   4553 						 asoc->primary_destination);
   4554 			}
   4555 		} else {
   4556 			/*
   4557 			 * we still got (or just got) data to send, so set
   4558 			 * SHUTDOWN_PENDING
   4559 			 */
   4560 			asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
   4561 		}
   4562 	}
   4563 #ifdef SCTP_MBCNT_LOGGING
   4564 	sctp_log_mbcnt(SCTP_LOG_MBCNT_INCREASE,
   4565 		       asoc->total_output_queue_size,
   4566 		       dataout,
   4567 		       asoc->total_output_mbuf_queue_size,
   4568 		       mbcnt);
   4569 #endif
   4570 	asoc->total_output_queue_size += dataout;
   4571 	asoc->total_output_mbuf_queue_size += mbcnt;
   4572 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
   4573 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
   4574 		so->so_snd.sb_cc += dataout;
   4575 		so->so_snd.sb_mbcnt += mbcnt;
   4576 	}
   4577 
   4578 #ifdef SCTP_DEBUG
   4579 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
   4580 		printf("++total out:%d total_mbuf_out:%d\n",
   4581 		       (int)asoc->total_output_queue_size,
   4582 		       (int)asoc->total_output_mbuf_queue_size);
   4583 	}
   4584 #endif
   4585 
   4586 release:
   4587 	sbunlock(&so->so_snd);
   4588 out_locked:
   4589 
   4590 out:
   4591 	if (m && m->m_nextpkt) {
   4592 		n = m;
   4593 		while (n) {
   4594 			mnext = n->m_nextpkt;
   4595 			n->m_nextpkt = NULL;
   4596 			sctp_m_freem(n);
   4597 			n = mnext;
   4598 		}
   4599 	} else if (m)
   4600 		sctp_m_freem(m);
   4601 
   4602 	return (error);
   4603 }
   4604 
   4605 static struct mbuf *
   4606 sctp_copy_mbufchain(struct mbuf *clonechain,
   4607 		    struct mbuf *outchain)
   4608 {
   4609 	struct mbuf *appendchain;
   4610 #if defined(__FreeBSD__) || defined(__NetBSD__)
   4611 	/* Supposedly m_copypacket is an optimization, use it if we can */
   4612 	if (clonechain->m_flags & M_PKTHDR) {
   4613 		appendchain = m_copypacket(clonechain, M_DONTWAIT);
   4614 		sctp_pegs[SCTP_CACHED_SRC]++;
   4615 	} else
   4616 		appendchain = m_copy(clonechain, 0, M_COPYALL);
   4617 #elif defined(__APPLE__)
   4618 	appendchain = sctp_m_copym(clonechain, 0, M_COPYALL, M_DONTWAIT);
   4619 #else
   4620 	appendchain = m_copy(clonechain, 0, M_COPYALL);
   4621 #endif
   4622 
   4623 	if (appendchain == NULL) {
   4624 		/* error */
   4625 		if (outchain)
   4626 			sctp_m_freem(outchain);
   4627 		return (NULL);
   4628 	}
   4629 	if (outchain) {
   4630 		/* tack on to the end */
   4631 		struct mbuf *m;
   4632 		m = outchain;
   4633 		while (m) {
   4634 			if (m->m_next == NULL) {
   4635 				m->m_next = appendchain;
   4636 				break;
   4637 			}
   4638 			m = m->m_next;
   4639 		}
   4640 		if (outchain->m_flags & M_PKTHDR) {
   4641 			int append_tot;
   4642 			struct mbuf *t;
   4643 			t = appendchain;
   4644 			append_tot = 0;
   4645 			while (t) {
   4646 				append_tot += t->m_len;
   4647 				t = t->m_next;
   4648 			}
   4649 			outchain->m_pkthdr.len += append_tot;
   4650 		}
   4651 		return (outchain);
   4652 	} else {
   4653 		return (appendchain);
   4654 	}
   4655 }
   4656 
   4657 static void
   4658 sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr, u_int32_t val)
   4659 {
   4660 	struct sctp_copy_all *ca;
   4661 	struct mbuf *m;
   4662 	int turned_on_nonblock=0, ret;
   4663 
   4664 	ca = (struct sctp_copy_all *)ptr;
   4665 	if (ca->m == NULL) {
   4666 		return;
   4667 	}
   4668 	if (ca->inp != inp) {
   4669 		/* TSNH */
   4670 		return;
   4671 	}
   4672 	m = sctp_copy_mbufchain(ca->m, NULL);
   4673 	if (m == NULL) {
   4674 		/* can't copy so we are done */
   4675 		ca->cnt_failed++;
   4676 		return;
   4677 	}
   4678 	if ((stcb->sctp_socket->so_state & SS_NBIO) == 0) {
   4679 		/* we have to do this non-blocking */
   4680 		turned_on_nonblock = 1;
   4681 		stcb->sctp_socket->so_state |= SS_NBIO;
   4682 	}
   4683 	ret = sctp_msg_append(stcb, stcb->asoc.primary_destination, m, &ca->sndrcv, 0);
   4684 	if (turned_on_nonblock) {
   4685 		/* we turned on non-blocking so turn it off */
   4686 		stcb->sctp_socket->so_state &= ~SS_NBIO;
   4687 	}
   4688 	if (ret) {
   4689 		ca->cnt_failed++;
   4690 	} else {
   4691 		ca->cnt_sent++;
   4692 	}
   4693 }
   4694 
   4695 static void
   4696 sctp_sendall_completes(void *ptr, u_int32_t val)
   4697 {
   4698 	struct sctp_copy_all *ca;
   4699 	ca = (struct sctp_copy_all *)ptr;
   4700 	/* Do a notify here?
   4701 	 * Kacheong suggests that the notify
   4702 	 * be done at the send time.. so you would
   4703 	 * push up a notification if any send failed.
   4704 	 * Don't know if this is feasable since the
   4705 	 * only failures we have is "memory" related and
   4706 	 * if you cannot get an mbuf to send the data
   4707 	 * you surely can't get an mbuf to send up
   4708 	 * to notify the user you can't send the data :->
   4709 	 */
   4710 
   4711 	/* now free everything */
   4712 	m_freem(ca->m);
   4713 	free(ca, M_PCB);
   4714 }
   4715 
   4716 
   4717 #define	MC_ALIGN(m, len) do {						\
   4718 	(m)->m_data += (MCLBYTES - (len)) & ~(sizeof(long) - 1);		\
   4719 } while (0)
   4720 
   4721 
   4722 
   4723 static struct mbuf *
   4724 sctp_copy_out_all(struct uio *uio, int len)
   4725 {
   4726 	struct mbuf *ret, *at;
   4727 	int left, willcpy, cancpy, error;
   4728 
   4729 	MGETHDR(ret, M_WAIT, MT_HEADER);
   4730 	if (ret == NULL) {
   4731 		/* TSNH */
   4732 		return (NULL);
   4733 	}
   4734 	left = len;
   4735 	ret->m_len = 0;
   4736 	ret->m_pkthdr.len = len;
   4737 	MCLGET(ret, M_WAIT);
   4738 	if (ret == NULL) {
   4739 		return (NULL);
   4740 	}
   4741 	if ((ret->m_flags & M_EXT) == 0) {
   4742 		m_freem (ret);
   4743 		return (NULL);
   4744 	}
   4745 	cancpy = M_TRAILINGSPACE(ret);
   4746 	willcpy = min(cancpy, left);
   4747 	at = ret;
   4748 	while (left > 0) {
   4749 		/* Align data to the end */
   4750 		MC_ALIGN(at, willcpy);
   4751 		error = uiomove(mtod(at, void *), willcpy, uio);
   4752 		if (error) {
   4753 		err_out_now:
   4754 			m_freem(ret);
   4755 			return (NULL);
   4756 		}
   4757 		at->m_len = willcpy;
   4758 		at->m_nextpkt = at->m_next = 0;
   4759 		left -= willcpy;
   4760 		if (left > 0) {
   4761 			MGET(at->m_next, M_WAIT, MT_DATA);
   4762 			if (at->m_next == NULL) {
   4763 				goto err_out_now;
   4764 			}
   4765 			at = at->m_next;
   4766 			at->m_len = 0;
   4767 			MCLGET(at, M_WAIT);
   4768 			if (at == NULL) {
   4769 				goto err_out_now;
   4770 			}
   4771 			if ((at->m_flags & M_EXT) == 0) {
   4772 				goto err_out_now;
   4773 			}
   4774 			cancpy = M_TRAILINGSPACE(at);
   4775 			willcpy = min(cancpy, left);
   4776 		}
   4777 	}
   4778 	return (ret);
   4779 }
   4780 
   4781 static int
   4782 sctp_sendall (struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m, struct sctp_sndrcvinfo *srcv)
   4783 {
   4784 	int ret;
   4785 	struct sctp_copy_all *ca;
   4786 	ca = malloc(sizeof(struct sctp_copy_all), M_PCB, M_WAIT);
   4787 	if (ca == NULL) {
   4788 		m_freem(m);
   4789 		return (ENOMEM);
   4790 	}
   4791 	memset (ca, 0, sizeof(struct sctp_copy_all));
   4792 
   4793 	ca->inp = inp;
   4794 	ca->sndrcv = *srcv;
   4795 	/* take off the sendall flag, it would
   4796 	 * be bad if we failed to do this  :-0
   4797 	 */
   4798  	ca->sndrcv.sinfo_flags &= ~SCTP_SENDALL;
   4799 
   4800 	/* get length and mbuf chain */
   4801 	if (uio) {
   4802 		ca->sndlen = uio->uio_resid;
   4803 		ca->m = sctp_copy_out_all(uio, ca->sndlen);
   4804 		if (ca->m == NULL) {
   4805 			free(ca, M_PCB);
   4806 			return (ENOMEM);
   4807 		}
   4808 	} else {
   4809 		if ((m->m_flags & M_PKTHDR) == 0) {
   4810 			ca->sndlen = 0;
   4811 			while(m) {
   4812 				ca->sndlen += m->m_len;
   4813 				m = m->m_next;
   4814 			}
   4815 		} else {
   4816 			ca->sndlen = m->m_pkthdr.len;
   4817 		}
   4818 		ca->m = m;
   4819 	}
   4820 
   4821 	ret = sctp_initiate_iterator(sctp_sendall_iterator, SCTP_PCB_ANY_FLAGS, SCTP_ASOC_ANY_STATE,
   4822 				     (void *)ca, 0, sctp_sendall_completes, inp);
   4823 	if (ret) {
   4824 #ifdef SCTP_DEBUG
   4825 		printf("Failed to initate iterator to takeover associations\n");
   4826 #endif
   4827 		free(ca, M_PCB);
   4828 		return (EFAULT);
   4829 
   4830 	}
   4831 	return (0);
   4832 }
   4833 
   4834 
   4835 void
   4836 sctp_toss_old_cookies(struct sctp_association *asoc)
   4837 {
   4838 	struct sctp_tmit_chunk *chk, *nchk;
   4839 	chk = TAILQ_FIRST(&asoc->control_send_queue);
   4840 	while (chk) {
   4841 		nchk = TAILQ_NEXT(chk, sctp_next);
   4842 		if (chk->rec.chunk_id == SCTP_COOKIE_ECHO) {
   4843 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
   4844 			if (chk->data) {
   4845 				sctp_m_freem(chk->data);
   4846 				chk->data = NULL;
   4847 			}
   4848 			asoc->ctrl_queue_cnt--;
   4849 			if (chk->whoTo)
   4850 				sctp_free_remote_addr(chk->whoTo);
   4851 			SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   4852 			sctppcbinfo.ipi_count_chunk--;
   4853 			if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   4854 				panic("Chunk count is negative");
   4855 			}
   4856 			sctppcbinfo.ipi_gencnt_chunk++;
   4857 		}
   4858 		chk = nchk;
   4859 	}
   4860 }
   4861 
   4862 void
   4863 sctp_toss_old_asconf(struct sctp_tcb *stcb)
   4864 {
   4865 	struct sctp_association *asoc;
   4866 	struct sctp_tmit_chunk *chk, *chk_tmp;
   4867 
   4868 	asoc = &stcb->asoc;
   4869 	for (chk = TAILQ_FIRST(&asoc->control_send_queue); chk != NULL;
   4870 	     chk = chk_tmp) {
   4871 		/* get next chk */
   4872 		chk_tmp = TAILQ_NEXT(chk, sctp_next);
   4873 		/* find SCTP_ASCONF chunk in queue (only one ever in queue) */
   4874 		if (chk->rec.chunk_id == SCTP_ASCONF) {
   4875 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
   4876 			if (chk->data) {
   4877 				sctp_m_freem(chk->data);
   4878 				chk->data = NULL;
   4879 			}
   4880 			asoc->ctrl_queue_cnt--;
   4881 			if (chk->whoTo)
   4882 				sctp_free_remote_addr(chk->whoTo);
   4883 			SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   4884 			sctppcbinfo.ipi_count_chunk--;
   4885 			if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   4886 				panic("Chunk count is negative");
   4887 			}
   4888 			sctppcbinfo.ipi_gencnt_chunk++;
   4889 		}
   4890 	}
   4891 }
   4892 
   4893 
   4894 static void
   4895 sctp_clean_up_datalist(struct sctp_tcb *stcb,
   4896 		       struct sctp_association *asoc,
   4897 		       struct sctp_tmit_chunk **data_list,
   4898 		       int bundle_at,
   4899 		       struct sctp_nets *net)
   4900 {
   4901 	int i;
   4902 	for (i = 0; i < bundle_at; i++) {
   4903 		/* off of the send queue */
   4904 		if (i) {
   4905 			/* Any chunk NOT 0 you zap the time
   4906 			 * chunk 0 gets zapped or set based on
   4907 			 * if a RTO measurment is needed.
   4908 			 */
   4909 			data_list[i]->do_rtt = 0;
   4910 		}
   4911 		/* record time */
   4912 		data_list[i]->sent_rcv_time = net->last_sent_time;
   4913 		TAILQ_REMOVE(&asoc->send_queue,
   4914 			     data_list[i],
   4915 			     sctp_next);
   4916 		/* on to the sent queue */
   4917 		TAILQ_INSERT_TAIL(&asoc->sent_queue,
   4918 				  data_list[i],
   4919 				  sctp_next);
   4920 		/* This does not lower until the cum-ack passes it */
   4921 		asoc->sent_queue_cnt++;
   4922 		asoc->send_queue_cnt--;
   4923 		if ((asoc->peers_rwnd <= 0) &&
   4924 		    (asoc->total_flight == 0) &&
   4925 		    (bundle_at == 1)) {
   4926 			/* Mark the chunk as being a window probe */
   4927 #ifdef SCTP_DEBUG
   4928 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   4929 				printf("WINDOW PROBE SET\n");
   4930 			}
   4931 #endif
   4932 			sctp_pegs[SCTP_WINDOW_PROBES]++;
   4933 			data_list[i]->rec.data.state_flags |= SCTP_WINDOW_PROBE;
   4934 		} else {
   4935 			data_list[i]->rec.data.state_flags &= ~SCTP_WINDOW_PROBE;
   4936 		}
   4937 #ifdef SCTP_AUDITING_ENABLED
   4938 		sctp_audit_log(0xC2, 3);
   4939 #endif
   4940 		data_list[i]->sent = SCTP_DATAGRAM_SENT;
   4941 		data_list[i]->snd_count = 1;
   4942 		net->flight_size += data_list[i]->book_size;
   4943 		asoc->total_flight += data_list[i]->book_size;
   4944 		asoc->total_flight_count++;
   4945 #ifdef SCTP_LOG_RWND
   4946 		sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
   4947 			      asoc->peers_rwnd , data_list[i]->send_size, sctp_peer_chunk_oh);
   4948 #endif
   4949 		asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
   4950 						    (u_int32_t)(data_list[i]->send_size + sctp_peer_chunk_oh));
   4951 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
   4952 			/* SWS sender side engages */
   4953 			asoc->peers_rwnd = 0;
   4954 		}
   4955 	}
   4956 }
   4957 
   4958 static void
   4959 sctp_clean_up_ctl(struct sctp_association *asoc)
   4960 {
   4961 	struct sctp_tmit_chunk *chk, *nchk;
   4962 	for (chk = TAILQ_FIRST(&asoc->control_send_queue);
   4963 	    chk; chk = nchk) {
   4964 		nchk = TAILQ_NEXT(chk, sctp_next);
   4965 		if ((chk->rec.chunk_id == SCTP_SELECTIVE_ACK) ||
   4966 		    (chk->rec.chunk_id == SCTP_HEARTBEAT_REQUEST) ||
   4967 		    (chk->rec.chunk_id == SCTP_HEARTBEAT_ACK) ||
   4968 		    (chk->rec.chunk_id == SCTP_SHUTDOWN) ||
   4969 		    (chk->rec.chunk_id == SCTP_SHUTDOWN_ACK) ||
   4970 		    (chk->rec.chunk_id == SCTP_OPERATION_ERROR) ||
   4971 		    (chk->rec.chunk_id == SCTP_PACKET_DROPPED) ||
   4972 		    (chk->rec.chunk_id == SCTP_COOKIE_ACK) ||
   4973 		    (chk->rec.chunk_id == SCTP_ECN_CWR) ||
   4974 		    (chk->rec.chunk_id == SCTP_ASCONF_ACK)) {
   4975 			/* Stray chunks must be cleaned up */
   4976 		clean_up_anyway:
   4977 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
   4978 			if (chk->data) {
   4979 				sctp_m_freem(chk->data);
   4980 				chk->data = NULL;
   4981 			}
   4982 			asoc->ctrl_queue_cnt--;
   4983 			sctp_free_remote_addr(chk->whoTo);
   4984 			SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   4985 			sctppcbinfo.ipi_count_chunk--;
   4986 			if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   4987 				panic("Chunk count is negative");
   4988 			}
   4989 			sctppcbinfo.ipi_gencnt_chunk++;
   4990 		} else if (chk->rec.chunk_id == SCTP_STREAM_RESET) {
   4991 			struct sctp_stream_reset_req *strreq;
   4992 			/* special handling, we must look into the param */
   4993 			strreq = mtod(chk->data, struct sctp_stream_reset_req *);
   4994 			if (strreq->sr_req.ph.param_type == ntohs(SCTP_STR_RESET_RESPONSE)) {
   4995 				goto clean_up_anyway;
   4996 			}
   4997 		}
   4998 	}
   4999 }
   5000 
   5001 static int
   5002 sctp_move_to_outqueue(struct sctp_tcb *stcb,
   5003 		      struct sctp_stream_out *strq)
   5004 {
   5005 	/* Move from the stream to the send_queue keeping track of the total */
   5006 	struct sctp_association *asoc;
   5007 	int tot_moved = 0;
   5008 	int failed = 0;
   5009 	int padval;
   5010 	struct sctp_tmit_chunk *chk, *nchk;
   5011 	struct sctp_data_chunk *dchkh;
   5012 	struct sctpchunk_listhead tmp;
   5013 	struct mbuf *orig;
   5014 
   5015 	asoc = &stcb->asoc;
   5016 	TAILQ_INIT(&tmp);
   5017 	chk = TAILQ_FIRST(&strq->outqueue);
   5018 	while (chk) {
   5019 		nchk = TAILQ_NEXT(chk, sctp_next);
   5020 		/* now put in the chunk header */
   5021 		orig = chk->data;
   5022 		M_PREPEND(chk->data, sizeof(struct sctp_data_chunk), M_DONTWAIT);
   5023 		if (chk->data == NULL) {
   5024 			/* HELP */
   5025 			failed++;
   5026 			break;
   5027 		}
   5028 		if (orig != chk->data) {
   5029 			/* A new mbuf was added, account for it */
   5030 			if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
   5031 			    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
   5032 				stcb->sctp_socket->so_snd.sb_mbcnt += MSIZE;
   5033 			}
   5034 #ifdef SCTP_MBCNT_LOGGING
   5035 			sctp_log_mbcnt(SCTP_LOG_MBCNT_INCREASE,
   5036 				       asoc->total_output_queue_size,
   5037 				       0,
   5038 				       asoc->total_output_mbuf_queue_size,
   5039 				       MSIZE);
   5040 #endif
   5041 			stcb->asoc.total_output_mbuf_queue_size += MSIZE;
   5042 			chk->mbcnt += MSIZE;
   5043 		}
   5044 		chk->send_size += sizeof(struct sctp_data_chunk);
   5045 		/* This should NOT have to do anything, but
   5046 		 * I would rather be cautious
   5047 		 */
   5048 		if (!failed && ((size_t)chk->data->m_len < sizeof(struct sctp_data_chunk))) {
   5049 			m_pullup(chk->data, sizeof(struct sctp_data_chunk));
   5050 			if (chk->data == NULL) {
   5051 				failed++;
   5052 				break;
   5053 			}
   5054 		}
   5055 		dchkh = mtod(chk->data, struct sctp_data_chunk *);
   5056 		dchkh->ch.chunk_length = htons(chk->send_size);
   5057 		/* Chunks must be padded to even word boundary */
   5058 		padval = chk->send_size % 4;
   5059 		if (padval) {
   5060 			/* For fragmented messages this should not
   5061 			 * run except possibly on the last chunk
   5062 			 */
   5063 			if (sctp_pad_lastmbuf(chk->data, (4 - padval))) {
   5064 				/* we are in big big trouble no mbufs :< */
   5065 				failed++;
   5066 				break;
   5067 			}
   5068 			chk->send_size += (4 - padval);
   5069 		}
   5070 		/* pull from stream queue */
   5071 		TAILQ_REMOVE(&strq->outqueue, chk, sctp_next);
   5072 		asoc->stream_queue_cnt--;
   5073 		TAILQ_INSERT_TAIL(&tmp, chk, sctp_next);
   5074 		/* add it in to the size of moved chunks */
   5075 		if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
   5076 			/* we pull only one message */
   5077 			break;
   5078 		}
   5079 		chk = nchk;
   5080 	}
   5081 	if (failed) {
   5082 		/* Gak, we just lost the user message */
   5083 		chk = TAILQ_FIRST(&tmp);
   5084 		while (chk) {
   5085 			nchk = TAILQ_NEXT(chk, sctp_next);
   5086 			TAILQ_REMOVE(&tmp, chk, sctp_next);
   5087 
   5088 			sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb,
   5089 					(SCTP_NOTIFY_DATAGRAM_UNSENT|SCTP_INTERNAL_ERROR),
   5090 					chk);
   5091 
   5092 			if (chk->data) {
   5093 				sctp_m_freem(chk->data);
   5094 				chk->data = NULL;
   5095 			}
   5096 			if (chk->whoTo) {
   5097 				sctp_free_remote_addr(chk->whoTo);
   5098 				chk->whoTo = NULL;
   5099 			}
   5100 			SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   5101 			sctppcbinfo.ipi_count_chunk--;
   5102 			if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   5103 				panic("Chunk count is negative");
   5104 			}
   5105 			sctppcbinfo.ipi_gencnt_chunk++;
   5106 			chk = nchk;
   5107 		}
   5108 		return (0);
   5109 	}
   5110 	/* now pull them off of temp wheel */
   5111 	chk = TAILQ_FIRST(&tmp);
   5112 	while (chk) {
   5113 		nchk = TAILQ_NEXT(chk, sctp_next);
   5114 		/* insert on send_queue */
   5115 		TAILQ_REMOVE(&tmp, chk, sctp_next);
   5116 		TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
   5117 		asoc->send_queue_cnt++;
   5118 		/* assign TSN */
   5119 		chk->rec.data.TSN_seq = asoc->sending_seq++;
   5120 
   5121 		dchkh = mtod(chk->data, struct sctp_data_chunk *);
   5122 		/* Put the rest of the things in place now. Size
   5123 		 * was done earlier in previous loop prior to
   5124 		 * padding.
   5125 		 */
   5126 		dchkh->ch.chunk_type = SCTP_DATA;
   5127 		dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
   5128 		dchkh->dp.tsn = htonl(chk->rec.data.TSN_seq);
   5129 		dchkh->dp.stream_id = htons(strq->stream_no);
   5130 		dchkh->dp.stream_sequence = htons(chk->rec.data.stream_seq);
   5131 		dchkh->dp.protocol_id = chk->rec.data.payloadtype;
   5132 		/* total count moved */
   5133 		tot_moved += chk->send_size;
   5134 		chk = nchk;
   5135 	}
   5136 	return (tot_moved);
   5137 }
   5138 
   5139 static void
   5140 sctp_fill_outqueue(struct sctp_tcb *stcb,
   5141 		   struct sctp_nets *net)
   5142 {
   5143 	struct sctp_association *asoc;
   5144 	struct sctp_tmit_chunk *chk;
   5145 	struct sctp_stream_out *strq, *strqn;
   5146 	int mtu_fromwheel, goal_mtu;
   5147 	unsigned int moved, seenend, cnt_mvd=0;
   5148 
   5149 	asoc = &stcb->asoc;
   5150 	/* Attempt to move at least 1 MTU's worth
   5151 	 * onto the wheel for each destination address
   5152 	 */
   5153 	goal_mtu = net->cwnd - net->flight_size;
   5154 	if ((unsigned int)goal_mtu < net->mtu) {
   5155 		goal_mtu = net->mtu;
   5156 	}
   5157 	if (sctp_pegs[SCTP_MOVED_MTU] < (unsigned int)goal_mtu) {
   5158 		sctp_pegs[SCTP_MOVED_MTU] = goal_mtu;
   5159 	}
   5160 	seenend = moved = mtu_fromwheel = 0;
   5161 	if (asoc->last_out_stream == NULL) {
   5162 		strq = asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
   5163 		if (asoc->last_out_stream == NULL) {
   5164 			/* huh nothing on the wheel, TSNH */
   5165 			return;
   5166 		}
   5167 		goto done_it;
   5168 	}
   5169 	strq = TAILQ_NEXT(asoc->last_out_stream, next_spoke);
   5170  done_it:
   5171 	if (strq == NULL) {
   5172 		asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
   5173 	}
   5174 	while (mtu_fromwheel < goal_mtu) {
   5175 		if (strq == NULL) {
   5176 			if (seenend == 0) {
   5177 				seenend = 1;
   5178 				strq = TAILQ_FIRST(&asoc->out_wheel);
   5179 			} else if ((moved == 0) && (seenend)) {
   5180 				/* none left on the wheel */
   5181 				sctp_pegs[SCTP_MOVED_NLEF]++;
   5182 				return;
   5183 			} else if (moved) {
   5184 				/*
   5185 				 * clear the flags and rotate back through
   5186 				 * again
   5187 				 */
   5188 				moved = 0;
   5189 				seenend = 0;
   5190 				strq = TAILQ_FIRST(&asoc->out_wheel);
   5191 			}
   5192 			if (strq == NULL)
   5193 				break;
   5194 			continue;
   5195 		}
   5196 		strqn = TAILQ_NEXT(strq, next_spoke);
   5197 		if ((chk = TAILQ_FIRST(&strq->outqueue)) == NULL) {
   5198 			/* none left on this queue, prune a spoke?  */
   5199 			sctp_remove_from_wheel(asoc, strq);
   5200 			if (strq == asoc->last_out_stream) {
   5201 			    /* the last one we used went off the wheel */
   5202 			    asoc->last_out_stream = NULL;
   5203 			}
   5204 			strq = strqn;
   5205 			continue;
   5206 		}
   5207 		if (chk->whoTo != net) {
   5208 			/* Skip this stream, first one on stream
   5209 			 * does not head to our current destination.
   5210 			 */
   5211 			strq = strqn;
   5212 			continue;
   5213 		}
   5214 		mtu_fromwheel += sctp_move_to_outqueue(stcb, strq);
   5215 		cnt_mvd++;
   5216 		moved++;
   5217 		asoc->last_out_stream = strq;
   5218 		strq = strqn;
   5219 	}
   5220 	sctp_pegs[SCTP_MOVED_MAX]++;
   5221 #ifdef SCTP_DEBUG
   5222 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5223 		printf("Ok we moved %d chunks to send queue\n",
   5224 		       moved);
   5225 	}
   5226 #endif
   5227 	if (sctp_pegs[SCTP_MOVED_QMAX] < cnt_mvd) {
   5228 		sctp_pegs[SCTP_MOVED_QMAX] = cnt_mvd;
   5229 	}
   5230 }
   5231 
   5232 void
   5233 sctp_fix_ecn_echo(struct sctp_association *asoc)
   5234 {
   5235 	struct sctp_tmit_chunk *chk;
   5236 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
   5237 		if (chk->rec.chunk_id == SCTP_ECN_ECHO) {
   5238 			chk->sent = SCTP_DATAGRAM_UNSENT;
   5239 		}
   5240 	}
   5241 }
   5242 
   5243 static void
   5244 sctp_move_to_an_alt(struct sctp_tcb *stcb,
   5245 		    struct sctp_association *asoc,
   5246 		    struct sctp_nets *net)
   5247 {
   5248 	struct sctp_tmit_chunk *chk;
   5249 	struct sctp_nets *a_net;
   5250 	a_net = sctp_find_alternate_net(stcb, net);
   5251 	if ((a_net != net) &&
   5252 	    ((a_net->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE)) {
   5253 		/*
   5254 		 * We only proceed if a valid alternate is found that is
   5255 		 * not this one and is reachable. Here we must move all
   5256 		 * chunks queued in the send queue off of the destination
   5257 		 * address to our alternate.
   5258 		 */
   5259 		TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
   5260 			if (chk->whoTo == net) {
   5261 				/* Move the chunk to our alternate */
   5262 				sctp_free_remote_addr(chk->whoTo);
   5263 				chk->whoTo = a_net;
   5264 				a_net->ref_count++;
   5265 			}
   5266 		}
   5267 	}
   5268 }
   5269 
   5270 static int sctp_from_user_send=0;
   5271 
   5272 static int
   5273 sctp_med_chunk_output(struct sctp_inpcb *inp,
   5274 		      struct sctp_tcb *stcb,
   5275 		      struct sctp_association *asoc,
   5276 		      int *num_out,
   5277 		      int *reason_code,
   5278 		      int control_only, int *cwnd_full, int from_where,
   5279 		      struct timeval *now, int *now_filled)
   5280 {
   5281 	/*
   5282 	 * Ok this is the generic chunk service queue.
   5283 	 * we must do the following:
   5284 	 *  - Service the stream queue that is next, moving any message
   5285 	 *    (note I must get a complete message i.e. FIRST/MIDDLE and
   5286 	 *    LAST to the out queue in one pass) and assigning TSN's
   5287 	 *  - Check to see if the cwnd/rwnd allows any output, if so we
   5288 	 *    go ahead and fomulate and send the low level chunks. Making
   5289 	 *    sure to combine any control in the control chunk queue also.
   5290 	 */
   5291 	struct sctp_nets *net;
   5292 	struct mbuf *outchain;
   5293 	struct sctp_tmit_chunk *chk, *nchk;
   5294 	struct sctphdr *shdr;
   5295 	/* temp arrays for unlinking */
   5296 	struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
   5297 	int no_fragmentflg, error;
   5298 	int one_chunk, hbflag;
   5299 	int asconf, cookie, no_out_cnt;
   5300 	int bundle_at, ctl_cnt, no_data_chunks, cwnd_full_ind;
   5301         unsigned int mtu, r_mtu, omtu;
   5302 	*num_out = 0;
   5303 	cwnd_full_ind = 0;
   5304 	ctl_cnt = no_out_cnt = asconf = cookie = 0;
   5305 	/*
   5306 	 * First lets prime the pump. For each destination, if there
   5307 	 * is room in the flight size, attempt to pull an MTU's worth
   5308 	 * out of the stream queues into the general send_queue
   5309 	 */
   5310 #ifdef SCTP_AUDITING_ENABLED
   5311 	sctp_audit_log(0xC2, 2);
   5312 #endif
   5313 #ifdef SCTP_DEBUG
   5314 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5315 		printf("***********************\n");
   5316 	}
   5317 #endif
   5318 	hbflag = 0;
   5319 	if (control_only)
   5320 		no_data_chunks = 1;
   5321 	else
   5322 		no_data_chunks = 0;
   5323 
   5324 	/* Nothing to possible to send? */
   5325 	if (TAILQ_EMPTY(&asoc->control_send_queue) &&
   5326 	    TAILQ_EMPTY(&asoc->send_queue) &&
   5327 	    TAILQ_EMPTY(&asoc->out_wheel)) {
   5328 #ifdef SCTP_DEBUG
   5329 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5330 			printf("All wheels empty\n");
   5331 		}
   5332 #endif
   5333 		return (0);
   5334 	}
   5335 	if (asoc->peers_rwnd <= 0) {
   5336 		/* No room in peers rwnd */
   5337 		*cwnd_full = 1;
   5338 		*reason_code = 1;
   5339 		if (asoc->total_flight > 0) {
   5340 			/* we are allowed one chunk in flight */
   5341 			no_data_chunks = 1;
   5342 			sctp_pegs[SCTP_RWND_BLOCKED]++;
   5343 		}
   5344 	}
   5345 #ifdef SCTP_DEBUG
   5346 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5347 		printf("Ok we have done the fillup no_data_chunk=%d tf=%d prw:%d\n",
   5348 		       (int)no_data_chunks,
   5349 		       (int)asoc->total_flight, (int)asoc->peers_rwnd);
   5350 	}
   5351 #endif
   5352 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
   5353 #ifdef SCTP_DEBUG
   5354 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5355 			printf("net:%p fs:%d  cwnd:%d\n",
   5356 			       net, net->flight_size, net->cwnd);
   5357 		}
   5358 #endif
   5359 		if (net->flight_size >= net->cwnd) {
   5360 			/* skip this network, no room */
   5361 			cwnd_full_ind++;
   5362 #ifdef SCTP_DEBUG
   5363 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5364 				printf("Ok skip fillup->fs:%d > cwnd:%d\n",
   5365 				       net->flight_size,
   5366 				       net->cwnd);
   5367 			}
   5368 #endif
   5369 			sctp_pegs[SCTP_CWND_NOFILL]++;
   5370 			continue;
   5371 		}
   5372 		/*
   5373 		 * spin through the stream queues moving one message and
   5374 		 * assign TSN's as appropriate.
   5375 		 */
   5376 		sctp_fill_outqueue(stcb, net);
   5377 	}
   5378 	*cwnd_full = cwnd_full_ind;
   5379 	/* now service each destination and send out what we can for it */
   5380 #ifdef SCTP_DEBUG
   5381 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5382 		int chk_cnt = 0;
   5383 		TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
   5384 			chk_cnt++;
   5385 		}
   5386 		printf("We have %d chunks on the send_queue\n", chk_cnt);
   5387 		chk_cnt = 0;
   5388 		TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
   5389 			chk_cnt++;
   5390 		}
   5391 		printf("We have %d chunks on the sent_queue\n", chk_cnt);
   5392 		TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
   5393 			chk_cnt++;
   5394 		}
   5395 		printf("We have %d chunks on the control_queue\n", chk_cnt);
   5396 	}
   5397 #endif
   5398 	/* If we have data to send, and DSACK is running, stop it
   5399 	 * and build a SACK to dump on to bundle with output. This
   5400 	 * actually MAY make it so the bundling does not occur if
   5401 	 * the SACK is big but I think this is ok because basic SACK
   5402 	 * space is pre-reserved in our fragmentation size choice.
   5403 	 */
   5404 	if ((TAILQ_FIRST(&asoc->send_queue) != NULL) &&
   5405 	    (no_data_chunks == 0)) {
   5406 		/* We will be sending something */
   5407 		if (callout_pending(&stcb->asoc.dack_timer.timer)) {
   5408 			/* Yep a callout is pending */
   5409 			sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
   5410 					stcb->sctp_ep,
   5411 					stcb, NULL);
   5412 			sctp_send_sack(stcb);
   5413 		}
   5414 	}
   5415 	/* Nothing to send? */
   5416 	if ((TAILQ_FIRST(&asoc->control_send_queue) == NULL) &&
   5417 	    (TAILQ_FIRST(&asoc->send_queue) == NULL)) {
   5418 		return (0);
   5419 	}
   5420 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
   5421 		struct rtentry *rt;
   5422 		/* how much can we send? */
   5423 		if (net->ref_count < 2) {
   5424 			/* Ref-count of 1 so we cannot have data or control
   5425 			 * queued to this address. Skip it.
   5426 			 */
   5427  			continue;
   5428 		}
   5429 		ctl_cnt = bundle_at = 0;
   5430 		outchain = NULL;
   5431 		no_fragmentflg = 1;
   5432 		one_chunk = 0;
   5433 
   5434 		rt = rtcache_validate(&net->ro);
   5435 		if (rt != NULL) {
   5436 			/* if we have a route and an ifp
   5437 			 * check to see if we have room to
   5438 			 * send to this guy
   5439 			 */
   5440 			struct ifnet *ifp;
   5441 			ifp = net->ro._ro_rt->rt_ifp;
   5442 			if ((ifp->if_snd.ifq_len + 2) >= ifp->if_snd.ifq_maxlen) {
   5443 				sctp_pegs[SCTP_IFP_QUEUE_FULL]++;
   5444 #ifdef SCTP_LOG_MAXBURST
   5445 				sctp_log_maxburst(net, ifp->if_snd.ifq_len, ifp->if_snd.ifq_maxlen, SCTP_MAX_IFP_APPLIED);
   5446   #endif
   5447 				rtcache_unref(rt, &net->ro);
   5448 				continue;
   5449 			}
   5450 			rtcache_unref(rt, &net->ro);
   5451 		}
   5452 		if (((struct sockaddr *)&net->ro.ro_sa)->sa_family == AF_INET) {
   5453 			mtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
   5454 		} else {
   5455 			mtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
   5456 		}
   5457 		if (mtu > asoc->peers_rwnd) {
   5458 			if (asoc->total_flight > 0) {
   5459 				/* We have a packet in flight somewhere */
   5460 				r_mtu = asoc->peers_rwnd;
   5461 			} else {
   5462 				/* We are always allowed to send one MTU out */
   5463 				one_chunk = 1;
   5464 				r_mtu = mtu;
   5465 			}
   5466 		} else {
   5467 			r_mtu = mtu;
   5468 		}
   5469 #ifdef SCTP_DEBUG
   5470 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5471 			printf("Ok r_mtu is %d mtu is %d for this net:%p one_chunk:%d\n",
   5472 			       r_mtu, mtu, net, one_chunk);
   5473 		}
   5474 #endif
   5475 		/************************/
   5476 		/* Control transmission */
   5477 		/************************/
   5478 		/* Now first lets go through the control queue */
   5479 		for (chk = TAILQ_FIRST(&asoc->control_send_queue);
   5480 		     chk; chk = nchk) {
   5481 			nchk = TAILQ_NEXT(chk, sctp_next);
   5482 			if (chk->whoTo != net) {
   5483 				/*
   5484 				 * No, not sent to the network we are
   5485 				 * looking at
   5486 				 */
   5487 				continue;
   5488 			}
   5489 			if (chk->data == NULL) {
   5490 				continue;
   5491 			}
   5492 			if ((chk->data->m_flags & M_PKTHDR) == 0) {
   5493 				/*
   5494 				 * NOTE: the chk queue MUST have the PKTHDR
   5495 				 * flag set on it with a total in the
   5496 				 * m_pkthdr.len field!! else the chunk will
   5497 				 * ALWAYS be skipped
   5498 				 */
   5499 				continue;
   5500 			}
   5501 			if (chk->sent != SCTP_DATAGRAM_UNSENT) {
   5502 				/*
   5503 				 * It must be unsent. Cookies and ASCONF's
   5504 				 * hang around but there timers will force
   5505 				 * when marked for resend.
   5506 				 */
   5507 				continue;
   5508 			}
   5509 			/* Here we do NOT factor the r_mtu */
   5510 			if ((chk->data->m_pkthdr.len < (int)mtu) ||
   5511 			    (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
   5512 				/*
   5513 				 * We probably should glom the mbuf chain from
   5514 				 * the chk->data for control but the problem
   5515 				 * is it becomes yet one more level of
   5516 				 * tracking to do if for some reason output
   5517 				 * fails. Then I have got to reconstruct the
   5518 				 * merged control chain.. el yucko.. for now
   5519 				 * we take the easy way and do the copy
   5520 				 */
   5521 				outchain = sctp_copy_mbufchain(chk->data,
   5522 							       outchain);
   5523 				if (outchain == NULL) {
   5524 					return (ENOMEM);
   5525 				}
   5526 				/* update our MTU size */
   5527 				if (mtu > chk->data->m_pkthdr.len)
   5528 					mtu -= chk->data->m_pkthdr.len;
   5529 				else
   5530 					mtu = 0;
   5531 				/* Do clear IP_DF ? */
   5532 				if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
   5533 					no_fragmentflg = 0;
   5534 				}
   5535 				/* Mark things to be removed, if needed */
   5536 				if ((chk->rec.chunk_id == SCTP_SELECTIVE_ACK) ||
   5537 				    (chk->rec.chunk_id == SCTP_HEARTBEAT_REQUEST) ||
   5538 				    (chk->rec.chunk_id == SCTP_HEARTBEAT_ACK) ||
   5539 				    (chk->rec.chunk_id == SCTP_SHUTDOWN) ||
   5540 				    (chk->rec.chunk_id == SCTP_SHUTDOWN_ACK) ||
   5541 				    (chk->rec.chunk_id == SCTP_OPERATION_ERROR) ||
   5542 				    (chk->rec.chunk_id == SCTP_COOKIE_ACK) ||
   5543 				    (chk->rec.chunk_id == SCTP_ECN_CWR) ||
   5544 				    (chk->rec.chunk_id == SCTP_PACKET_DROPPED) ||
   5545 				    (chk->rec.chunk_id == SCTP_ASCONF_ACK)) {
   5546 
   5547 					if (chk->rec.chunk_id == SCTP_HEARTBEAT_REQUEST)
   5548 						hbflag = 1;
   5549 					/* remove these chunks at the end */
   5550 					if (chk->rec.chunk_id == SCTP_SELECTIVE_ACK) {
   5551 						/* turn off the timer */
   5552 						if (callout_pending(&stcb->asoc.dack_timer.timer)) {
   5553 							sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
   5554 									inp, stcb, net);
   5555 						}
   5556 					}
   5557 					ctl_cnt++;
   5558 				} else {
   5559 					/*
   5560 					 * Other chunks, since they have
   5561 					 * timers running (i.e. COOKIE or
   5562 					 * ASCONF) we just "trust" that it
   5563 					 * gets sent or retransmitted.
   5564 					 */
   5565 					ctl_cnt++;
   5566 					if (chk->rec.chunk_id == SCTP_COOKIE_ECHO) {
   5567 						cookie = 1;
   5568 						no_out_cnt = 1;
   5569 					} else if (chk->rec.chunk_id == SCTP_ASCONF) {
   5570 						/*
   5571 						 * set hb flag since we can use
   5572 						 * these for RTO
   5573 						 */
   5574 						hbflag = 1;
   5575 						asconf = 1;
   5576 					}
   5577 					chk->sent = SCTP_DATAGRAM_SENT;
   5578 					chk->snd_count++;
   5579 				}
   5580 				if (mtu == 0) {
   5581 					/*
   5582 					 * Ok we are out of room but we can
   5583 					 * output without effecting the flight
   5584 					 * size since this little guy is a
   5585 					 * control only packet.
   5586 					 */
   5587 					if (asconf) {
   5588 						sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
   5589 						asconf = 0;
   5590 					}
   5591 					if (cookie) {
   5592 						sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
   5593 						cookie = 0;
   5594 					}
   5595 					if (outchain->m_len == 0) {
   5596 						/*
   5597 						 * Special case for when you
   5598 						 * get a 0 len mbuf at the
   5599 						 * head due to the lack of a
   5600 						 * MHDR at the beginning.
   5601 						 */
   5602 						outchain->m_len = sizeof(struct sctphdr);
   5603 					} else {
   5604 						M_PREPEND(outchain, sizeof(struct sctphdr), M_DONTWAIT);
   5605 						if (outchain == NULL) {
   5606 							/* no memory */
   5607 							error = ENOBUFS;
   5608 							goto error_out_again;
   5609 						}
   5610 					}
   5611 					shdr = mtod(outchain, struct sctphdr *);
   5612 					shdr->src_port = inp->sctp_lport;
   5613 					shdr->dest_port = stcb->rport;
   5614 					shdr->v_tag = htonl(stcb->asoc.peer_vtag);
   5615 					shdr->checksum = 0;
   5616 
   5617 					if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
   5618 										rtcache_getdst(&net->ro),
   5619 										outchain,
   5620 										no_fragmentflg, 0, NULL, asconf))) {
   5621 						if (error == ENOBUFS) {
   5622 							asoc->ifp_had_enobuf = 1;
   5623 						}
   5624 						sctp_pegs[SCTP_DATA_OUT_ERR]++;
   5625 						if (from_where == 0) {
   5626 							sctp_pegs[SCTP_ERROUT_FRM_USR]++;
   5627 						}
   5628 					error_out_again:
   5629 #ifdef SCTP_DEBUG
   5630 						if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
   5631 							printf("Gak got ctrl error %d\n", error);
   5632 						}
   5633 #endif
   5634 						/* error, could not output */
   5635 						if (hbflag) {
   5636 #ifdef SCTP_DEBUG
   5637 							if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5638 								printf("Update HB anyway\n");
   5639 							}
   5640 #endif
   5641 							if (*now_filled == 0) {
   5642 								SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
   5643 								*now_filled = 1;
   5644 								*now = net->last_sent_time;
   5645 							} else {
   5646 								net->last_sent_time = *now;
   5647 							}
   5648 							hbflag = 0;
   5649 						}
   5650 						if (error == EHOSTUNREACH) {
   5651 							/*
   5652 							 * Destination went
   5653 							 * unreachable during
   5654 							 * this send
   5655 							 */
   5656 #ifdef SCTP_DEBUG
   5657 							if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5658 								printf("Moving data to an alterante\n");
   5659 							}
   5660 #endif
   5661 							sctp_move_to_an_alt(stcb, asoc, net);
   5662 						}
   5663 						sctp_clean_up_ctl (asoc);
   5664 						return (error);
   5665 					} else
   5666 						asoc->ifp_had_enobuf = 0;
   5667 					/* Only HB or ASCONF advances time */
   5668 					if (hbflag) {
   5669 						if (*now_filled == 0) {
   5670 							SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
   5671 							*now_filled = 1;
   5672 							*now = net->last_sent_time;
   5673 						} else {
   5674 							net->last_sent_time = *now;
   5675 						}
   5676 						hbflag = 0;
   5677 					}
   5678 					/*
   5679 					 * increase the number we sent, if a
   5680 					 * cookie is sent we don't tell them
   5681 					 * any was sent out.
   5682 					 */
   5683 					if (!no_out_cnt)
   5684 						*num_out +=  ctl_cnt;
   5685 					/* recalc a clean slate and setup */
   5686 					if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
   5687 						mtu = (net->mtu - SCTP_MIN_OVERHEAD);
   5688 					} else {
   5689 						mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
   5690 					}
   5691 					no_fragmentflg = 1;
   5692 				}
   5693 			}
   5694 		}
   5695 		/*********************/
   5696 		/* Data transmission */
   5697 		/*********************/
   5698 		/* now lets add any data within the MTU constraints */
   5699 		if (((struct sockaddr *)&net->ro.ro_sa)->sa_family == AF_INET) {
   5700 			omtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
   5701 		} else {
   5702 			omtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
   5703 		}
   5704 
   5705 #ifdef SCTP_DEBUG
   5706 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5707 			printf("Now to data transmission\n");
   5708 		}
   5709 #endif
   5710 
   5711 		if (((asoc->state & SCTP_STATE_OPEN) == SCTP_STATE_OPEN) ||
   5712 		    (cookie)) {
   5713 			for (chk = TAILQ_FIRST(&asoc->send_queue); chk; chk = nchk) {
   5714 				if (no_data_chunks) {
   5715 					/* let only control go out */
   5716 #ifdef SCTP_DEBUG
   5717 					if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5718 						printf("Either nothing to send or we are full\n");
   5719 					}
   5720 #endif
   5721 					break;
   5722 				}
   5723 				if (net->flight_size >= net->cwnd) {
   5724 					/* skip this net, no room for data */
   5725 #ifdef SCTP_DEBUG
   5726 					if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5727 						printf("fs:%d > cwnd:%d\n",
   5728 						       net->flight_size, net->cwnd);
   5729 					}
   5730 #endif
   5731 					sctp_pegs[SCTP_CWND_BLOCKED]++;
   5732 					*reason_code = 2;
   5733 					break;
   5734 				}
   5735 				nchk = TAILQ_NEXT(chk, sctp_next);
   5736 				if (chk->whoTo != net) {
   5737 					/* No, not sent to this net */
   5738 #ifdef SCTP_DEBUG
   5739 					if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5740 						printf("chk->whoTo:%p not %p\n",
   5741 						       chk->whoTo, net);
   5742 
   5743 					}
   5744 #endif
   5745 					continue;
   5746 				}
   5747 #ifdef SCTP_DEBUG
   5748 				if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5749 					printf("Can we pick up a chunk?\n");
   5750 				}
   5751 #endif
   5752 				if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
   5753 					/* strange, we have a chunk that is to bit
   5754 					 * for its destination and yet no fragment ok flag.
   5755 					 * Something went wrong when the PMTU changed...we did
   5756 					 * not mark this chunk for some reason?? I will
   5757 					 * fix it here by letting IP fragment it for now and
   5758 					 * printing a warning. This really should not happen ...
   5759 					 */
   5760 /*#ifdef SCTP_DEBUG*/
   5761 					printf("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
   5762 					       chk->send_size, mtu);
   5763 /*#endif*/
   5764 					chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
   5765 				}
   5766 
   5767 				if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
   5768 				    ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
   5769 					/* ok we will add this one */
   5770 #ifdef SCTP_DEBUG
   5771 					if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5772 						printf("Picking up the chunk\n");
   5773 					}
   5774 #endif
   5775 					outchain = sctp_copy_mbufchain(chk->data, outchain);
   5776 					if (outchain == NULL) {
   5777 #ifdef SCTP_DEBUG
   5778 						if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5779 							printf("Gakk no memory\n");
   5780 						}
   5781 #endif
   5782 						if (!callout_pending(&net->rxt_timer.timer)) {
   5783 							sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
   5784 						}
   5785 						return (ENOMEM);
   5786 					}
   5787 					/* upate our MTU size */
   5788 					/* Do clear IP_DF ? */
   5789 					if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
   5790 						no_fragmentflg = 0;
   5791 					}
   5792 					mtu -= chk->send_size;
   5793 					r_mtu -= chk->send_size;
   5794 					data_list[bundle_at++] = chk;
   5795 					if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
   5796 						mtu = 0;
   5797 						break;
   5798 					}
   5799 					if (mtu <= 0) {
   5800 						mtu = 0;
   5801 						break;
   5802 					}
   5803 					if ((r_mtu <= 0) || one_chunk) {
   5804 						r_mtu = 0;
   5805 						break;
   5806 					}
   5807 				} else {
   5808 					/*
   5809 					 * Must be sent in order of the TSN's
   5810 					 * (on a network)
   5811 					 */
   5812 #ifdef SCTP_DEBUG
   5813 					if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5814 						printf("ok no more chk:%d > mtu:%d || < r_mtu:%d\n",
   5815 						       chk->send_size, mtu, r_mtu);
   5816 					}
   5817 #endif
   5818 
   5819 					break;
   5820 				}
   5821 			}/* for () */
   5822 		} /* if asoc.state OPEN */
   5823 		/* Is there something to send for this destination? */
   5824 #ifdef SCTP_DEBUG
   5825 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5826 			printf("ok now is chain assembled? %p\n",
   5827 			       outchain);
   5828 		}
   5829 #endif
   5830 
   5831 		if (outchain) {
   5832 			/* We may need to start a control timer or two */
   5833 			if (asconf) {
   5834 				sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
   5835 				asconf = 0;
   5836 			}
   5837 			if (cookie) {
   5838 				sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
   5839 				cookie = 0;
   5840 			}
   5841 			/* must start a send timer if data is being sent */
   5842 			if (bundle_at && (!callout_pending(&net->rxt_timer.timer))) {
   5843 				/* no timer running on this destination
   5844 				 * restart it.
   5845 				 */
   5846 #ifdef SCTP_DEBUG
   5847 				if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5848 					printf("ok lets start a send timer .. we will transmit %p\n",
   5849 					       outchain);
   5850 				}
   5851 #endif
   5852 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
   5853 			}
   5854 			/* Now send it, if there is anything to send :> */
   5855 			if ((outchain->m_flags & M_PKTHDR) == 0) {
   5856 				struct mbuf *t;
   5857 
   5858 				MGETHDR(t, M_DONTWAIT, MT_HEADER);
   5859 				if (t == NULL) {
   5860 					sctp_m_freem(outchain);
   5861 					return (ENOMEM);
   5862 				}
   5863 				t->m_next = outchain;
   5864 				t->m_pkthdr.len = 0;
   5865 				m_reset_rcvif(t);
   5866 				t->m_len = 0;
   5867 
   5868 				outchain = t;
   5869 				while (t) {
   5870 					outchain->m_pkthdr.len += t->m_len;
   5871 					t = t->m_next;
   5872 				}
   5873 			}
   5874 			if (outchain->m_len == 0) {
   5875 				/* Special case for when you get a 0 len
   5876 				 * mbuf at the head due to the lack
   5877 				 * of a MHDR at the beginning.
   5878 				 */
   5879 				MH_ALIGN(outchain, sizeof(struct sctphdr));
   5880 				outchain->m_len = sizeof(struct sctphdr);
   5881 			} else {
   5882 				M_PREPEND(outchain, sizeof(struct sctphdr), M_DONTWAIT);
   5883 				if (outchain == NULL) {
   5884 					/* out of mbufs */
   5885 					error = ENOBUFS;
   5886 					goto errored_send;
   5887 				}
   5888 			}
   5889 			shdr = mtod(outchain, struct sctphdr *);
   5890 			shdr->src_port = inp->sctp_lport;
   5891 			shdr->dest_port = stcb->rport;
   5892 			shdr->v_tag = htonl(stcb->asoc.peer_vtag);
   5893 			shdr->checksum = 0;
   5894 			if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
   5895 								rtcache_getdst(&net->ro),
   5896 								outchain,
   5897 								no_fragmentflg, bundle_at, data_list[0], asconf))) {
   5898 				/* error, we could not output */
   5899 				if (error == ENOBUFS) {
   5900 					asoc->ifp_had_enobuf = 1;
   5901 				}
   5902 				sctp_pegs[SCTP_DATA_OUT_ERR]++;
   5903 				if (from_where == 0) {
   5904 					sctp_pegs[SCTP_ERROUT_FRM_USR]++;
   5905 				}
   5906 
   5907 			errored_send:
   5908 #ifdef SCTP_DEBUG
   5909 				if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5910 					printf("Gak send error %d\n", error);
   5911 				}
   5912 #endif
   5913 				if (hbflag) {
   5914 #ifdef SCTP_DEBUG
   5915 					if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5916 						printf("Update HB time anyway\n");
   5917 					}
   5918 #endif
   5919 					if (*now_filled == 0) {
   5920 						SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
   5921 						*now_filled = 1;
   5922 						*now = net->last_sent_time;
   5923 					} else {
   5924 						net->last_sent_time = *now;
   5925 					}
   5926 					hbflag = 0;
   5927 				}
   5928 				if (error == EHOSTUNREACH) {
   5929 					/*
   5930 					 * Destination went unreachable during
   5931 					 * this send
   5932 					 */
   5933 #ifdef SCTP_DEBUG
   5934 					if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   5935 						printf("Calling the movement routine\n");
   5936 					}
   5937 #endif
   5938 					sctp_move_to_an_alt(stcb, asoc, net);
   5939 				}
   5940 				sctp_clean_up_ctl (asoc);
   5941 				return (error);
   5942 			} else {
   5943 				asoc->ifp_had_enobuf = 0;
   5944 			}
   5945 			if (bundle_at || hbflag) {
   5946 				/* For data/asconf and hb set time */
   5947 				if (*now_filled == 0) {
   5948 					SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
   5949 					*now_filled = 1;
   5950 					*now = net->last_sent_time;
   5951 				} else {
   5952 					net->last_sent_time = *now;
   5953 				}
   5954 			}
   5955 
   5956 			if (!no_out_cnt) {
   5957 				*num_out += (ctl_cnt + bundle_at);
   5958 			}
   5959 			if (bundle_at) {
   5960 				if (!net->rto_pending) {
   5961 					/* setup for a RTO measurement */
   5962 					net->rto_pending = 1;
   5963 					data_list[0]->do_rtt = 1;
   5964 				} else {
   5965 					data_list[0]->do_rtt = 0;
   5966 				}
   5967 				sctp_pegs[SCTP_PEG_TSNS_SENT] += bundle_at;
   5968 				sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
   5969 			}
   5970 			if (one_chunk) {
   5971 				break;
   5972 			}
   5973 		}
   5974 	}
   5975 	/* At the end there should be no NON timed
   5976 	 * chunks hanging on this queue.
   5977 	 */
   5978 	if ((*num_out == 0) && (*reason_code == 0)) {
   5979 		*reason_code = 3;
   5980 	}
   5981 	sctp_clean_up_ctl (asoc);
   5982 	return (0);
   5983 }
   5984 
   5985 void
   5986 sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
   5987 {
   5988 	/* Prepend a OPERATIONAL_ERROR chunk header
   5989 	 * and put on the end of the control chunk queue.
   5990 	 */
   5991 	/* Sender had better have gotten a MGETHDR or else
   5992 	 * the control chunk will be forever skipped
   5993 	 */
   5994 	struct sctp_chunkhdr *hdr;
   5995 	struct sctp_tmit_chunk *chk;
   5996 	struct mbuf *mat;
   5997 
   5998 	chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
   5999 	if (chk == NULL) {
   6000 		/* no memory */
   6001 		sctp_m_freem(op_err);
   6002 		return;
   6003 	}
   6004 	sctppcbinfo.ipi_count_chunk++;
   6005 	sctppcbinfo.ipi_gencnt_chunk++;
   6006 	M_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_DONTWAIT);
   6007 	if (op_err == NULL) {
   6008 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   6009 		sctppcbinfo.ipi_count_chunk--;
   6010 		if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   6011 			panic("Chunk count is negative");
   6012 		}
   6013 		sctppcbinfo.ipi_gencnt_chunk++;
   6014 		return;
   6015 	}
   6016 	chk->send_size = 0;
   6017 	mat = op_err;
   6018 	while (mat != NULL) {
   6019 		chk->send_size += mat->m_len;
   6020 		mat = mat->m_next;
   6021 	}
   6022 	chk->rec.chunk_id = SCTP_OPERATION_ERROR;
   6023 	chk->sent = SCTP_DATAGRAM_UNSENT;
   6024 	chk->snd_count = 0;
   6025 	chk->flags = 0;
   6026 	chk->asoc = &stcb->asoc;
   6027 	chk->data = op_err;
   6028 	chk->whoTo = chk->asoc->primary_destination;
   6029 	chk->whoTo->ref_count++;
   6030 	hdr = mtod(op_err, struct sctp_chunkhdr *);
   6031 	hdr->chunk_type = SCTP_OPERATION_ERROR;
   6032 	hdr->chunk_flags = 0;
   6033 	hdr->chunk_length = htons(chk->send_size);
   6034 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue,
   6035 			  chk,
   6036 			  sctp_next);
   6037 	chk->asoc->ctrl_queue_cnt++;
   6038 }
   6039 
   6040 int
   6041 sctp_send_cookie_echo(struct mbuf *m,
   6042 		      int offset,
   6043 		      struct sctp_tcb *stcb,
   6044 		      struct sctp_nets *net)
   6045 {
   6046 	/*
   6047 	 * pull out the cookie and put it at the front of the control
   6048 	 * chunk queue.
   6049 	 */
   6050 	int at;
   6051 	struct mbuf *cookie, *mat;
   6052 	struct sctp_paramhdr parm, *phdr;
   6053 	struct sctp_chunkhdr *hdr;
   6054 	struct sctp_tmit_chunk *chk;
   6055 	uint16_t ptype, plen;
   6056 	/* First find the cookie in the param area */
   6057 	cookie = NULL;
   6058 	at = offset + sizeof(struct sctp_init_chunk);
   6059 
   6060 	do {
   6061 		phdr = sctp_get_next_param(m, at, &parm, sizeof(parm));
   6062 		if (phdr == NULL) {
   6063 			return (-3);
   6064 		}
   6065 		ptype = ntohs(phdr->param_type);
   6066 		plen = ntohs(phdr->param_length);
   6067 		if (ptype == SCTP_STATE_COOKIE) {
   6068 			int pad;
   6069 			/* found the cookie */
   6070 			if ((pad = (plen % 4))) {
   6071 				plen += 4 - pad;
   6072 			}
   6073 			cookie = sctp_m_copym(m, at, plen, M_DONTWAIT);
   6074 			if (cookie == NULL) {
   6075 				/* No memory */
   6076 				return (-2);
   6077 			}
   6078 			break;
   6079 		}
   6080 		at += SCTP_SIZE32(plen);
   6081 	} while (phdr);
   6082 	if (cookie == NULL) {
   6083 		/* Did not find the cookie */
   6084 		return (-3);
   6085 	}
   6086 	/* ok, we got the cookie lets change it into a cookie echo chunk */
   6087 
   6088 	/* first the change from param to cookie */
   6089 	hdr = mtod(cookie, struct sctp_chunkhdr *);
   6090 	hdr->chunk_type = SCTP_COOKIE_ECHO;
   6091 	hdr->chunk_flags = 0;
   6092 	/* now we MUST have a PKTHDR on it */
   6093 	if ((cookie->m_flags & M_PKTHDR) != M_PKTHDR) {
   6094 		/* we hope this happens rarely */
   6095 		MGETHDR(mat, M_DONTWAIT, MT_HEADER);
   6096 		if (mat == NULL) {
   6097 			sctp_m_freem(cookie);
   6098 			return (-4);
   6099 		}
   6100 		mat->m_len = 0;
   6101 		m_reset_rcvif(mat);
   6102 		mat->m_next = cookie;
   6103 		cookie = mat;
   6104 	}
   6105 	cookie->m_pkthdr.len = plen;
   6106 	/* get the chunk stuff now and place it in the FRONT of the queue */
   6107 	chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
   6108 	if (chk == NULL) {
   6109 		/* no memory */
   6110 		sctp_m_freem(cookie);
   6111 		return (-5);
   6112 	}
   6113 	sctppcbinfo.ipi_count_chunk++;
   6114 	sctppcbinfo.ipi_gencnt_chunk++;
   6115 	chk->send_size = cookie->m_pkthdr.len;
   6116 	chk->rec.chunk_id = SCTP_COOKIE_ECHO;
   6117 	chk->sent = SCTP_DATAGRAM_UNSENT;
   6118 	chk->snd_count = 0;
   6119 	chk->flags = 0;
   6120 	chk->asoc = &stcb->asoc;
   6121 	chk->data = cookie;
   6122 	chk->whoTo = chk->asoc->primary_destination;
   6123 	chk->whoTo->ref_count++;
   6124 	TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
   6125 	chk->asoc->ctrl_queue_cnt++;
   6126 	return (0);
   6127 }
   6128 
   6129 void
   6130 sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
   6131 			struct mbuf *m,
   6132 			int offset,
   6133 			int chk_length,
   6134 			struct sctp_nets *net)
   6135 {
   6136 	/* take a HB request and make it into a
   6137 	 * HB ack and send it.
   6138 	 */
   6139 	struct mbuf *outchain;
   6140 	struct sctp_chunkhdr *chdr;
   6141 	struct sctp_tmit_chunk *chk;
   6142 
   6143 
   6144 	if (net == NULL)
   6145 		/* must have a net pointer */
   6146 		return;
   6147 
   6148 	outchain = sctp_m_copym(m, offset, chk_length, M_DONTWAIT);
   6149 	if (outchain == NULL) {
   6150 		/* gak out of memory */
   6151 		return;
   6152 	}
   6153 	chdr = mtod(outchain, struct sctp_chunkhdr *);
   6154 	chdr->chunk_type = SCTP_HEARTBEAT_ACK;
   6155 	chdr->chunk_flags = 0;
   6156 	if ((outchain->m_flags & M_PKTHDR) != M_PKTHDR) {
   6157 		/* should not happen but we are cautious. */
   6158 		struct mbuf *tmp;
   6159 		MGETHDR(tmp, M_DONTWAIT, MT_HEADER);
   6160 		if (tmp == NULL) {
   6161 			return;
   6162 		}
   6163 		tmp->m_len = 0;
   6164 		m_reset_rcvif(tmp);
   6165 		tmp->m_next = outchain;
   6166 		outchain = tmp;
   6167 	}
   6168 	outchain->m_pkthdr.len = chk_length;
   6169 	if (chk_length % 4) {
   6170 		/* need pad */
   6171 		u_int32_t cpthis=0;
   6172 		int padlen;
   6173 		padlen = 4 - (outchain->m_pkthdr.len % 4);
   6174 		m_copyback(outchain, outchain->m_pkthdr.len, padlen, (void *)&cpthis);
   6175 	}
   6176 	chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
   6177 	if (chk == NULL) {
   6178 		/* no memory */
   6179 		sctp_m_freem(outchain);
   6180 		return ;
   6181 	}
   6182 	sctppcbinfo.ipi_count_chunk++;
   6183 	sctppcbinfo.ipi_gencnt_chunk++;
   6184 
   6185 	chk->send_size = chk_length;
   6186 	chk->rec.chunk_id = SCTP_HEARTBEAT_ACK;
   6187 	chk->sent = SCTP_DATAGRAM_UNSENT;
   6188 	chk->snd_count = 0;
   6189 	chk->flags = 0;
   6190 	chk->asoc = &stcb->asoc;
   6191 	chk->data = outchain;
   6192 	chk->whoTo = net;
   6193 	chk->whoTo->ref_count++;
   6194 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
   6195 	chk->asoc->ctrl_queue_cnt++;
   6196 }
   6197 
   6198 int
   6199 sctp_send_cookie_ack(struct sctp_tcb *stcb) {
   6200 	/* formulate and queue a cookie-ack back to sender */
   6201 	struct mbuf *cookie_ack;
   6202 	struct sctp_chunkhdr *hdr;
   6203 	struct sctp_tmit_chunk *chk;
   6204 
   6205 	cookie_ack = NULL;
   6206 	MGETHDR(cookie_ack, M_DONTWAIT, MT_HEADER);
   6207 	if (cookie_ack == NULL) {
   6208 		/* no mbuf's */
   6209 		return (-1);
   6210 	}
   6211  	cookie_ack->m_data += SCTP_MIN_OVERHEAD;
   6212 	chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
   6213 	if (chk == NULL) {
   6214 		/* no memory */
   6215 		sctp_m_freem(cookie_ack);
   6216 		return (-1);
   6217 	}
   6218 	sctppcbinfo.ipi_count_chunk++;
   6219 	sctppcbinfo.ipi_gencnt_chunk++;
   6220 
   6221 	chk->send_size = sizeof(struct sctp_chunkhdr);
   6222 	chk->rec.chunk_id = SCTP_COOKIE_ACK;
   6223 	chk->sent = SCTP_DATAGRAM_UNSENT;
   6224 	chk->snd_count = 0;
   6225 	chk->flags = 0;
   6226 	chk->asoc = &stcb->asoc;
   6227 	chk->data = cookie_ack;
   6228 	if (chk->asoc->last_control_chunk_from != NULL) {
   6229 		chk->whoTo = chk->asoc->last_control_chunk_from;
   6230 	} else {
   6231 		chk->whoTo = chk->asoc->primary_destination;
   6232 	}
   6233 	chk->whoTo->ref_count++;
   6234 	hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
   6235 	hdr->chunk_type = SCTP_COOKIE_ACK;
   6236 	hdr->chunk_flags = 0;
   6237 	hdr->chunk_length = htons(chk->send_size);
   6238 	cookie_ack->m_pkthdr.len = cookie_ack->m_len = chk->send_size;
   6239 	m_reset_rcvif(cookie_ack);
   6240 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
   6241 	chk->asoc->ctrl_queue_cnt++;
   6242 	return (0);
   6243 }
   6244 
   6245 
   6246 int
   6247 sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
   6248 {
   6249 	/* formulate and queue a SHUTDOWN-ACK back to the sender */
   6250 	struct mbuf *m_shutdown_ack;
   6251 	struct sctp_shutdown_ack_chunk *ack_cp;
   6252 	struct sctp_tmit_chunk *chk;
   6253 
   6254 	m_shutdown_ack = NULL;
   6255 	MGETHDR(m_shutdown_ack, M_DONTWAIT, MT_HEADER);
   6256 	if (m_shutdown_ack == NULL) {
   6257 		/* no mbuf's */
   6258 		return (-1);
   6259 	}
   6260 	m_shutdown_ack->m_data += SCTP_MIN_OVERHEAD;
   6261 	chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
   6262 	if (chk == NULL) {
   6263 		/* no memory */
   6264 		sctp_m_freem(m_shutdown_ack);
   6265 		return (-1);
   6266 	}
   6267 	sctppcbinfo.ipi_count_chunk++;
   6268 	sctppcbinfo.ipi_gencnt_chunk++;
   6269 
   6270 	chk->send_size = sizeof(struct sctp_chunkhdr);
   6271 	chk->rec.chunk_id = SCTP_SHUTDOWN_ACK;
   6272 	chk->sent = SCTP_DATAGRAM_UNSENT;
   6273 	chk->snd_count = 0;
   6274 	chk->flags = 0;
   6275 	chk->asoc = &stcb->asoc;
   6276 	chk->data = m_shutdown_ack;
   6277 	chk->whoTo = net;
   6278 	net->ref_count++;
   6279 
   6280 	ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
   6281 	ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
   6282 	ack_cp->ch.chunk_flags = 0;
   6283 	ack_cp->ch.chunk_length = htons(chk->send_size);
   6284 	m_shutdown_ack->m_pkthdr.len = m_shutdown_ack->m_len = chk->send_size;
   6285 	m_reset_rcvif(m_shutdown_ack);
   6286 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
   6287 	chk->asoc->ctrl_queue_cnt++;
   6288 	return (0);
   6289 }
   6290 
   6291 int
   6292 sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
   6293 {
   6294 	/* formulate and queue a SHUTDOWN to the sender */
   6295 	struct mbuf *m_shutdown;
   6296 	struct sctp_shutdown_chunk *shutdown_cp;
   6297 	struct sctp_tmit_chunk *chk;
   6298 
   6299 	m_shutdown = NULL;
   6300 	MGETHDR(m_shutdown, M_DONTWAIT, MT_HEADER);
   6301 	if (m_shutdown == NULL) {
   6302 		/* no mbuf's */
   6303 		return (-1);
   6304 	}
   6305 	m_shutdown->m_data += SCTP_MIN_OVERHEAD;
   6306 	chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
   6307 	if (chk == NULL) {
   6308 		/* no memory */
   6309 		sctp_m_freem(m_shutdown);
   6310 		return (-1);
   6311 	}
   6312 	sctppcbinfo.ipi_count_chunk++;
   6313 	sctppcbinfo.ipi_gencnt_chunk++;
   6314 
   6315 	chk->send_size = sizeof(struct sctp_shutdown_chunk);
   6316 	chk->rec.chunk_id = SCTP_SHUTDOWN;
   6317 	chk->sent = SCTP_DATAGRAM_UNSENT;
   6318 	chk->snd_count = 0;
   6319 	chk->flags = 0;
   6320 	chk->asoc = &stcb->asoc;
   6321 	chk->data = m_shutdown;
   6322 	chk->whoTo = net;
   6323 	net->ref_count++;
   6324 
   6325 	shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
   6326 	shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
   6327 	shutdown_cp->ch.chunk_flags = 0;
   6328 	shutdown_cp->ch.chunk_length = htons(chk->send_size);
   6329 	shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
   6330 	m_shutdown->m_pkthdr.len = m_shutdown->m_len = chk->send_size;
   6331 	m_reset_rcvif(m_shutdown);
   6332 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
   6333 	chk->asoc->ctrl_queue_cnt++;
   6334 
   6335 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
   6336 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
   6337 		stcb->sctp_ep->sctp_socket->so_snd.sb_cc = 0;
   6338 		soisdisconnecting(stcb->sctp_ep->sctp_socket);
   6339 	}
   6340 	return (0);
   6341 }
   6342 
   6343 int
   6344 sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net)
   6345 {
   6346 	/*
   6347 	 * formulate and queue an ASCONF to the peer
   6348 	 * ASCONF parameters should be queued on the assoc queue
   6349 	 */
   6350 	struct sctp_tmit_chunk *chk;
   6351 	struct mbuf *m_asconf;
   6352 
   6353 	/* compose an ASCONF chunk, maximum length is PMTU */
   6354 	m_asconf = sctp_compose_asconf(stcb);
   6355 	if (m_asconf == NULL) {
   6356 		return (-1);
   6357 	}
   6358 	chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
   6359 	if (chk == NULL) {
   6360 		/* no memory */
   6361 		sctp_m_freem(m_asconf);
   6362 		return (-1);
   6363 	}
   6364 	sctppcbinfo.ipi_count_chunk++;
   6365 	sctppcbinfo.ipi_gencnt_chunk++;
   6366 
   6367 	chk->data = m_asconf;
   6368 	chk->send_size = m_asconf->m_pkthdr.len;
   6369 	chk->rec.chunk_id = SCTP_ASCONF;
   6370 	chk->sent = SCTP_DATAGRAM_UNSENT;
   6371 	chk->snd_count = 0;
   6372 	chk->flags = 0;
   6373 	chk->asoc = &stcb->asoc;
   6374 	chk->whoTo = chk->asoc->primary_destination;
   6375 	chk->whoTo->ref_count++;
   6376 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
   6377 	chk->asoc->ctrl_queue_cnt++;
   6378 	return (0);
   6379 }
   6380 
   6381 int
   6382 sctp_send_asconf_ack(struct sctp_tcb *stcb, uint32_t retrans)
   6383 {
   6384 	/*
   6385 	 * formulate and queue a asconf-ack back to sender
   6386 	 * the asconf-ack must be stored in the tcb
   6387 	 */
   6388 	struct sctp_tmit_chunk *chk;
   6389 	struct mbuf *m_ack;
   6390 
   6391 	/* is there a asconf-ack mbuf chain to send? */
   6392 	if (stcb->asoc.last_asconf_ack_sent == NULL) {
   6393 		return (-1);
   6394 	}
   6395 
   6396 	/* copy the asconf_ack */
   6397 #if defined(__FreeBSD__) || defined(__NetBSD__)
   6398 	/* Supposedly the m_copypacket is a optimzation,
   6399 	 * use it if we can.
   6400 	 */
   6401 	if (stcb->asoc.last_asconf_ack_sent->m_flags & M_PKTHDR) {
   6402 		m_ack = m_copypacket(stcb->asoc.last_asconf_ack_sent, M_DONTWAIT);
   6403 		sctp_pegs[SCTP_CACHED_SRC]++;
   6404 	} else
   6405 		m_ack = m_copy(stcb->asoc.last_asconf_ack_sent, 0, M_COPYALL);
   6406 #else
   6407 		m_ack = m_copy(stcb->asoc.last_asconf_ack_sent, 0, M_COPYALL);
   6408 #endif
   6409 	if (m_ack == NULL) {
   6410 		/* couldn't copy it */
   6411 
   6412 		return (-1);
   6413 	}
   6414 	chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
   6415 	if (chk == NULL) {
   6416 		/* no memory */
   6417 		if (m_ack)
   6418 			sctp_m_freem(m_ack);
   6419 		return (-1);
   6420 	}
   6421 	sctppcbinfo.ipi_count_chunk++;
   6422 	sctppcbinfo.ipi_gencnt_chunk++;
   6423 
   6424 	/* figure out where it goes to */
   6425 	if (retrans) {
   6426 		/* we're doing a retransmission */
   6427 		if (stcb->asoc.used_alt_asconfack > 2) {
   6428 			/* tried alternate nets already, go back */
   6429 			chk->whoTo = NULL;
   6430 		} else {
   6431 			/* need to try and alternate net */
   6432 			chk->whoTo = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from);
   6433 			stcb->asoc.used_alt_asconfack++;
   6434 		}
   6435 		if (chk->whoTo == NULL) {
   6436 			/* no alternate */
   6437 			if (stcb->asoc.last_control_chunk_from == NULL)
   6438 				chk->whoTo = stcb->asoc.primary_destination;
   6439 			else
   6440 				chk->whoTo = stcb->asoc.last_control_chunk_from;
   6441 			stcb->asoc.used_alt_asconfack = 0;
   6442 		}
   6443 	} else {
   6444 		/* normal case */
   6445 		if (stcb->asoc.last_control_chunk_from == NULL)
   6446 			chk->whoTo = stcb->asoc.primary_destination;
   6447 		else
   6448 			chk->whoTo = stcb->asoc.last_control_chunk_from;
   6449 		stcb->asoc.used_alt_asconfack = 0;
   6450 	}
   6451 	chk->data = m_ack;
   6452 	chk->send_size = m_ack->m_pkthdr.len;
   6453 	chk->rec.chunk_id = SCTP_ASCONF_ACK;
   6454 	chk->sent = SCTP_DATAGRAM_UNSENT;
   6455 	chk->snd_count = 0;
   6456 	chk->flags = 0;
   6457 	chk->asoc = &stcb->asoc;
   6458 	chk->whoTo->ref_count++;
   6459 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
   6460 	chk->asoc->ctrl_queue_cnt++;
   6461 	return (0);
   6462 }
   6463 
   6464 
   6465 static int
   6466 sctp_chunk_retransmission(struct sctp_inpcb *inp,
   6467 			  struct sctp_tcb *stcb,
   6468 			  struct sctp_association *asoc,
   6469 			  int *cnt_out, struct timeval *now, int *now_filled)
   6470 {
   6471 	/*
   6472 	 * send out one MTU of retransmission.
   6473 	 * If fast_retransmit is happening we ignore the cwnd.
   6474 	 * Otherwise we obey the cwnd and rwnd.
   6475 	 * For a Cookie or Asconf in the control chunk queue we retransmit
   6476 	 * them by themselves.
   6477 	 *
   6478 	 * For data chunks we will pick out the lowest TSN's in the
   6479 	 * sent_queue marked for resend and bundle them all together
   6480 	 * (up to a MTU of destination). The address to send to should
   6481 	 * have been selected/changed where the retransmission was
   6482 	 * marked (i.e. in FR or t3-timeout routines).
   6483 	 */
   6484 	struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
   6485 	struct sctp_tmit_chunk *chk, *fwd;
   6486 	struct mbuf *m;
   6487 	struct sctphdr *shdr;
   6488 	int asconf;
   6489 	struct sctp_nets *net;
   6490 	int no_fragmentflg, bundle_at, cnt_thru;
   6491 	unsigned int mtu;
   6492 	int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
   6493 
   6494 	tmr_started = ctl_cnt = bundle_at =  error = 0;
   6495 	no_fragmentflg = 1;
   6496 	asconf = 0;
   6497 	fwd_tsn = 0;
   6498 	*cnt_out = 0;
   6499 	fwd = NULL;
   6500 	m = NULL;
   6501 #ifdef SCTP_AUDITING_ENABLED
   6502 	sctp_audit_log(0xC3, 1);
   6503 #endif
   6504 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
   6505 #ifdef SCTP_DEBUG
   6506 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   6507 			printf("SCTP hits empty queue with cnt set to %d?\n",
   6508 			       asoc->sent_queue_retran_cnt);
   6509 		}
   6510 #endif
   6511 		asoc->sent_queue_cnt = 0;
   6512 		asoc->sent_queue_cnt_removeable = 0;
   6513 	}
   6514 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
   6515 		if (chk->sent != SCTP_DATAGRAM_RESEND) {
   6516 			/* we only worry about things marked for resend */
   6517 			continue;
   6518 		}
   6519 		if ((chk->rec.chunk_id == SCTP_COOKIE_ECHO) ||
   6520 		    (chk->rec.chunk_id == SCTP_ASCONF) ||
   6521 		    (chk->rec.chunk_id == SCTP_STREAM_RESET) ||
   6522 		    (chk->rec.chunk_id == SCTP_FORWARD_CUM_TSN)) {
   6523 			if (chk->rec.chunk_id == SCTP_STREAM_RESET) {
   6524 				/* For stream reset we only retran the request
   6525 				 * not the response.
   6526 				 */
   6527 				struct sctp_stream_reset_req *strreq;
   6528 				strreq = mtod(chk->data, struct sctp_stream_reset_req *);
   6529 				if (strreq->sr_req.ph.param_type != ntohs(SCTP_STR_RESET_REQUEST)) {
   6530 					continue;
   6531 				}
   6532 			}
   6533 			ctl_cnt++;
   6534 			if (chk->rec.chunk_id == SCTP_ASCONF) {
   6535 				no_fragmentflg = 1;
   6536 				asconf = 1;
   6537 			}
   6538 			if (chk->rec.chunk_id == SCTP_FORWARD_CUM_TSN) {
   6539 				fwd_tsn = 1;
   6540 				fwd = chk;
   6541 			}
   6542 			m = sctp_copy_mbufchain(chk->data, m);
   6543 			break;
   6544 		}
   6545 	}
   6546 	one_chunk = 0;
   6547 	cnt_thru = 0;
   6548 	/* do we have control chunks to retransmit? */
   6549 	if (m != NULL) {
   6550 		/* Start a timer no matter if we suceed or fail */
   6551 		if (chk->rec.chunk_id == SCTP_COOKIE_ECHO) {
   6552 			sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
   6553 		} else if (chk->rec.chunk_id == SCTP_ASCONF)
   6554 			sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
   6555 
   6556 		if (m->m_len == 0) {
   6557 			/* Special case for when you get a 0 len
   6558 			 * mbuf at the head due to the lack
   6559 			 * of a MHDR at the beginning.
   6560 			 */
   6561 			m->m_len = sizeof(struct sctphdr);
   6562 		} else {
   6563 			M_PREPEND(m, sizeof(struct sctphdr), M_DONTWAIT);
   6564 			if (m == NULL) {
   6565 				return (ENOBUFS);
   6566 			}
   6567 		}
   6568 		shdr = mtod(m, struct sctphdr *);
   6569 		shdr->src_port = inp->sctp_lport;
   6570 		shdr->dest_port = stcb->rport;
   6571 		shdr->v_tag = htonl(stcb->asoc.peer_vtag);
   6572 		shdr->checksum = 0;
   6573 		chk->snd_count++;		/* update our count */
   6574 
   6575 		if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
   6576 		    rtcache_getdst(&chk->whoTo->ro), m,
   6577 		    no_fragmentflg, 0, NULL, asconf))) {
   6578 			sctp_pegs[SCTP_DATA_OUT_ERR]++;
   6579 			return (error);
   6580 		}
   6581 		/*
   6582 		 *We don't want to mark the net->sent time here since this
   6583 		 * we use this for HB and retrans cannot measure RTT
   6584 		 */
   6585 		/*    SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time);*/
   6586 		*cnt_out += 1;
   6587 		chk->sent = SCTP_DATAGRAM_SENT;
   6588 		sctp_ucount_decr(asoc->sent_queue_retran_cnt);
   6589 		if (fwd_tsn == 0) {
   6590 			return (0);
   6591 		} else {
   6592 			/* Clean up the fwd-tsn list */
   6593 			sctp_clean_up_ctl (asoc);
   6594 			return (0);
   6595 		}
   6596 	}
   6597 	/* Ok, it is just data retransmission we need to do or
   6598 	 * that and a fwd-tsn with it all.
   6599 	 */
   6600 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
   6601 		return (-1);
   6602 	}
   6603 #ifdef SCTP_DEBUG
   6604 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   6605 		printf("Normal chunk retransmission cnt:%d\n",
   6606 		       asoc->sent_queue_retran_cnt);
   6607 	}
   6608 #endif
   6609 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) ||
   6610 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT)) {
   6611 		/* not yet open, resend the cookie and that is it */
   6612 		return (1);
   6613 	}
   6614 
   6615 
   6616 #ifdef SCTP_AUDITING_ENABLED
   6617 	sctp_auditing(20, inp, stcb, NULL);
   6618 #endif
   6619 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
   6620 		if (chk->sent != SCTP_DATAGRAM_RESEND) {
   6621 			/* No, not sent to this net or not ready for rtx */
   6622 			continue;
   6623 
   6624 		}
   6625 		/* pick up the net */
   6626 		net = chk->whoTo;
   6627 		if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
   6628 			mtu = (net->mtu - SCTP_MIN_OVERHEAD);
   6629 		} else {
   6630 			mtu = net->mtu- SCTP_MIN_V4_OVERHEAD;
   6631 		}
   6632 
   6633 		if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
   6634 			/* No room in peers rwnd */
   6635 			uint32_t tsn;
   6636 			tsn = asoc->last_acked_seq + 1;
   6637 			if (tsn == chk->rec.data.TSN_seq) {
   6638 				/* we make a special exception for this case.
   6639 				 * The peer has no rwnd but is missing the
   6640 				 * lowest chunk.. which is probably what is
   6641 				 * holding up the rwnd.
   6642 				 */
   6643 				goto one_chunk_around;
   6644 			}
   6645 #ifdef SCTP_DEBUG
   6646 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   6647 				printf("blocked-peers_rwnd:%d tf:%d\n",
   6648 				       (int)asoc->peers_rwnd,
   6649 				       (int)asoc->total_flight);
   6650 			}
   6651 #endif
   6652 			sctp_pegs[SCTP_RWND_BLOCKED]++;
   6653 			return (1);
   6654 		}
   6655 	one_chunk_around:
   6656 		if (asoc->peers_rwnd < mtu) {
   6657 			one_chunk = 1;
   6658 		}
   6659 #ifdef SCTP_AUDITING_ENABLED
   6660 		sctp_audit_log(0xC3, 2);
   6661 #endif
   6662 		bundle_at = 0;
   6663 		m = NULL;
   6664 		net->fast_retran_ip = 0;
   6665 		if (chk->rec.data.doing_fast_retransmit == 0) {
   6666 			/* if no FR in progress skip destination that
   6667 			 * have flight_size > cwnd.
   6668 			 */
   6669 			if (net->flight_size >= net->cwnd) {
   6670 				sctp_pegs[SCTP_CWND_BLOCKED]++;
   6671 				continue;
   6672 			}
   6673 		} else {
   6674 			/* Mark the destination net to have FR recovery
   6675 			 * limits put on it.
   6676 			 */
   6677 			net->fast_retran_ip = 1;
   6678 		}
   6679 
   6680 		if ((chk->send_size <= mtu) || (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
   6681 			/* ok we will add this one */
   6682 			m = sctp_copy_mbufchain(chk->data, m);
   6683 			if (m == NULL) {
   6684 				return (ENOMEM);
   6685 			}
   6686 			/* upate our MTU size */
   6687 			/* Do clear IP_DF ? */
   6688 			if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
   6689 				no_fragmentflg = 0;
   6690 			}
   6691 			mtu -= chk->send_size;
   6692 			data_list[bundle_at++] = chk;
   6693 			if (one_chunk && (asoc->total_flight <= 0)) {
   6694 				sctp_pegs[SCTP_WINDOW_PROBES]++;
   6695 				chk->rec.data.state_flags |= SCTP_WINDOW_PROBE;
   6696 			}
   6697 		}
   6698 		if (one_chunk == 0) {
   6699 			/* now are there anymore forward from chk to pick up?*/
   6700 			fwd = TAILQ_NEXT(chk, sctp_next);
   6701 			while (fwd) {
   6702 				if (fwd->sent != SCTP_DATAGRAM_RESEND) {
   6703 					/* Nope, not for retran */
   6704 					fwd = TAILQ_NEXT(fwd, sctp_next);
   6705 					continue;
   6706 				}
   6707 				if (fwd->whoTo != net) {
   6708 					/* Nope, not the net in question */
   6709 					fwd = TAILQ_NEXT(fwd, sctp_next);
   6710 					continue;
   6711 				}
   6712 				if (fwd->send_size <= mtu) {
   6713 					m = sctp_copy_mbufchain(fwd->data, m);
   6714 					if (m == NULL) {
   6715 						return (ENOMEM);
   6716 					}
   6717 					/* upate our MTU size */
   6718 					/* Do clear IP_DF ? */
   6719 					if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
   6720 						no_fragmentflg = 0;
   6721 					}
   6722 					mtu -= fwd->send_size;
   6723 					data_list[bundle_at++] = fwd;
   6724 					if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
   6725 						break;
   6726 					}
   6727 					fwd = TAILQ_NEXT(fwd, sctp_next);
   6728 				} else {
   6729 					/* can't fit so we are done */
   6730 					break;
   6731 				}
   6732 			}
   6733 		}
   6734 		/* Is there something to send for this destination? */
   6735 		if (m) {
   6736 			/* No matter if we fail/or suceed we should
   6737 			 * start a timer. A failure is like a lost
   6738 			 * IP packet :-)
   6739 			 */
   6740 			if (!callout_pending(&net->rxt_timer.timer)) {
   6741 				/* no timer running on this destination
   6742 				 * restart it.
   6743 				 */
   6744 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
   6745 				tmr_started = 1;
   6746 			}
   6747 			if (m->m_len == 0) {
   6748 				/* Special case for when you get a 0 len
   6749 				 * mbuf at the head due to the lack
   6750 				 * of a MHDR at the beginning.
   6751 				 */
   6752 				m->m_len = sizeof(struct sctphdr);
   6753 			} else {
   6754 				M_PREPEND(m, sizeof(struct sctphdr), M_DONTWAIT);
   6755 				if (m == NULL) {
   6756 					return (ENOBUFS);
   6757 				}
   6758 			}
   6759 			shdr = mtod(m, struct sctphdr *);
   6760 			shdr->src_port = inp->sctp_lport;
   6761 			shdr->dest_port = stcb->rport;
   6762 			shdr->v_tag = htonl(stcb->asoc.peer_vtag);
   6763 			shdr->checksum = 0;
   6764 
   6765 			/* Now lets send it, if there is anything to send :> */
   6766 			if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
   6767 							       rtcache_getdst(&net->ro),
   6768 							       m,
   6769 							       no_fragmentflg, 0, NULL, asconf))) {
   6770 				/* error, we could not output */
   6771 				sctp_pegs[SCTP_DATA_OUT_ERR]++;
   6772 				return (error);
   6773 			}
   6774 			/* For HB's */
   6775 			/*
   6776 			 * We don't want to mark the net->sent time here since
   6777 			 * this we use this for HB and retrans cannot measure
   6778 			 * RTT
   6779 			 */
   6780 			/*      SCTP_GETTIME_TIMEVAL(&net->last_sent_time);*/
   6781 
   6782 			/* For auto-close */
   6783 			cnt_thru++;
   6784 			if (*now_filled == 0) {
   6785 				SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
   6786 				*now = asoc->time_last_sent;
   6787 				*now_filled = 1;
   6788 			} else {
   6789 				asoc->time_last_sent = *now;
   6790 			}
   6791 			*cnt_out += bundle_at;
   6792 #ifdef SCTP_AUDITING_ENABLED
   6793 			sctp_audit_log(0xC4, bundle_at);
   6794 #endif
   6795 			for (i = 0; i < bundle_at; i++) {
   6796 				sctp_pegs[SCTP_RETRANTSN_SENT]++;
   6797 				data_list[i]->sent = SCTP_DATAGRAM_SENT;
   6798 				data_list[i]->snd_count++;
   6799 				sctp_ucount_decr(asoc->sent_queue_retran_cnt);
   6800 				/* record the time */
   6801 				data_list[i]->sent_rcv_time = asoc->time_last_sent;
   6802 				net->flight_size += data_list[i]->book_size;
   6803 				asoc->total_flight += data_list[i]->book_size;
   6804 				asoc->total_flight_count++;
   6805 
   6806 #ifdef SCTP_LOG_RWND
   6807 				sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
   6808 					      asoc->peers_rwnd , data_list[i]->send_size, sctp_peer_chunk_oh);
   6809 #endif
   6810 				asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
   6811 								    (u_int32_t)(data_list[i]->send_size + sctp_peer_chunk_oh));
   6812 				if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
   6813 					/* SWS sender side engages */
   6814 					asoc->peers_rwnd = 0;
   6815 				}
   6816 
   6817 				if ((i == 0) &&
   6818 				    (data_list[i]->rec.data.doing_fast_retransmit)) {
   6819 					sctp_pegs[SCTP_FAST_RETRAN]++;
   6820 					if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
   6821 					    (tmr_started == 0)) {
   6822 						/*
   6823 						 * ok we just fast-retrans'd
   6824 						 * the lowest TSN, i.e the
   6825 						 * first on the list. In this
   6826 						 * case we want to give some
   6827 						 * more time to get a SACK
   6828 						 * back without a t3-expiring.
   6829 						 */
   6830 						sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
   6831 						sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
   6832 					}
   6833 				}
   6834 			}
   6835 #ifdef SCTP_AUDITING_ENABLED
   6836 			sctp_auditing(21, inp, stcb, NULL);
   6837 #endif
   6838 		} else {
   6839 			/* None will fit */
   6840 			return (1);
   6841 		}
   6842 		if (asoc->sent_queue_retran_cnt <= 0) {
   6843 			/* all done we have no more to retran */
   6844 			asoc->sent_queue_retran_cnt = 0;
   6845 			break;
   6846 		}
   6847 		if (one_chunk) {
   6848 			/* No more room in rwnd */
   6849 			return (1);
   6850 		}
   6851 		/* stop the for loop here. we sent out a packet */
   6852 		break;
   6853 	}
   6854 	return (0);
   6855 }
   6856 
   6857 
   6858 static int
   6859 sctp_timer_validation(struct sctp_inpcb *inp,
   6860 		      struct sctp_tcb *stcb,
   6861 		      struct sctp_association *asoc,
   6862 		      int ret)
   6863 {
   6864 	struct sctp_nets *net;
   6865 	/* Validate that a timer is running somewhere */
   6866 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
   6867 		if (callout_pending(&net->rxt_timer.timer)) {
   6868 			/* Here is a timer */
   6869 			return (ret);
   6870 		}
   6871 	}
   6872 	/* Gak, we did not have a timer somewhere */
   6873 #ifdef SCTP_DEBUG
   6874 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   6875 		printf("Deadlock avoided starting timer on a dest at retran\n");
   6876 	}
   6877 #endif
   6878 	sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
   6879 	return (ret);
   6880 }
   6881 
   6882 int
   6883 sctp_chunk_output(struct sctp_inpcb *inp,
   6884 		  struct sctp_tcb *stcb,
   6885 		  int from_where)
   6886 {
   6887 	/* Ok this is the generic chunk service queue.
   6888 	 * we must do the following:
   6889 	 *  - See if there are retransmits pending, if so we
   6890 	 *   	must do these first and return.
   6891 	 *  - Service the stream queue that is next,
   6892 	 *    moving any message (note I must get a complete
   6893 	 *    message i.e. FIRST/MIDDLE and LAST to the out
   6894 	 *    queue in one pass) and assigning TSN's
   6895 	 *  - Check to see if the cwnd/rwnd allows any output, if
   6896 	 *	so we go ahead and fomulate and send the low level
   6897 	 *    chunks. Making sure to combine any control in the
   6898 	 *    control chunk queue also.
   6899 	 */
   6900 	struct sctp_association *asoc;
   6901 	struct sctp_nets *net;
   6902 	int error, num_out, tot_out, ret, reason_code, burst_cnt, burst_limit;
   6903 	struct timeval now;
   6904 	int now_filled=0;
   6905 	int cwnd_full=0;
   6906 	asoc = &stcb->asoc;
   6907 	tot_out = 0;
   6908 	num_out = 0;
   6909 	reason_code = 0;
   6910 	sctp_pegs[SCTP_CALLS_TO_CO]++;
   6911 #ifdef SCTP_DEBUG
   6912 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   6913 		printf("in co - retran count:%d\n", asoc->sent_queue_retran_cnt);
   6914 	}
   6915 #endif
   6916 	while (asoc->sent_queue_retran_cnt) {
   6917 		/* Ok, it is retransmission time only, we send out only ONE
   6918 		 * packet with a single call off to the retran code.
   6919 		 */
   6920 		ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled);
   6921 		if (ret > 0) {
   6922 			/* Can't send anymore */
   6923 #ifdef SCTP_DEBUG
   6924 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   6925 				printf("retransmission ret:%d -- full\n", ret);
   6926 			}
   6927 #endif
   6928 			/*
   6929 			 * now lets push out control by calling med-level
   6930 			 * output once. this assures that we WILL send HB's
   6931 			 * if queued too.
   6932 			 */
   6933 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
   6934 						    &cwnd_full, from_where,
   6935 						    &now, &now_filled);
   6936 #ifdef SCTP_DEBUG
   6937 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   6938 				printf("Control send outputs:%d@full\n", num_out);
   6939 			}
   6940 #endif
   6941 #ifdef SCTP_AUDITING_ENABLED
   6942 			sctp_auditing(8, inp, stcb, NULL);
   6943 #endif
   6944 			return (sctp_timer_validation(inp, stcb, asoc, ret));
   6945 		}
   6946 		if (ret < 0) {
   6947 			/*
   6948 			 * The count was off.. retran is not happening so do
   6949 			 * the normal retransmission.
   6950 			 */
   6951 #ifdef SCTP_DEBUG
   6952 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   6953 				printf("Done with retrans, none left fill up window\n");
   6954 			}
   6955 #endif
   6956 #ifdef SCTP_AUDITING_ENABLED
   6957 			sctp_auditing(9, inp, stcb, NULL);
   6958 #endif
   6959 			break;
   6960 		}
   6961 		if (from_where == 1) {
   6962 			/* Only one transmission allowed out of a timeout */
   6963 #ifdef SCTP_DEBUG
   6964 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   6965 				printf("Only one packet allowed out\n");
   6966 			}
   6967 #endif
   6968 #ifdef SCTP_AUDITING_ENABLED
   6969 			sctp_auditing(10, inp, stcb, NULL);
   6970 #endif
   6971 			/* Push out any control */
   6972 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, &cwnd_full, from_where,
   6973 						    &now, &now_filled);
   6974 			return (ret);
   6975 		}
   6976 		if ((num_out == 0) && (ret == 0)) {
   6977 			/* No more retrans to send */
   6978 			break;
   6979 		}
   6980 	}
   6981 #ifdef SCTP_AUDITING_ENABLED
   6982 	sctp_auditing(12, inp, stcb, NULL);
   6983 #endif
   6984 	/* Check for bad destinations, if they exist move chunks around. */
   6985 	burst_limit = asoc->max_burst;
   6986 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
   6987 		if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) ==
   6988 		    SCTP_ADDR_NOT_REACHABLE) {
   6989 			/*
   6990 			 * if possible move things off of this address
   6991 			 * we still may send below due to the dormant state
   6992 			 * but we try to find an alternate address to send
   6993 			 * to and if we have one we move all queued data on
   6994 			 * the out wheel to this alternate address.
   6995 			 */
   6996 			sctp_move_to_an_alt(stcb, asoc, net);
   6997 		} else {
   6998 			/*
   6999 			if ((asoc->sat_network) || (net->addr_is_local)) {
   7000 				burst_limit = asoc->max_burst * SCTP_SAT_NETWORK_BURST_INCR;
   7001 			}
   7002 			*/
   7003 #ifdef SCTP_DEBUG
   7004 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   7005 				printf("examined net:%p burst limit:%d\n", net, asoc->max_burst);
   7006 			}
   7007 #endif
   7008 
   7009 #ifdef SCTP_USE_ALLMAN_BURST
   7010 			if ((net->flight_size+(burst_limit*net->mtu)) < net->cwnd) {
   7011 				if (net->ssthresh < net->cwnd)
   7012 					net->ssthresh = net->cwnd;
   7013 				net->cwnd = (net->flight_size+(burst_limit*net->mtu));
   7014 #ifdef SCTP_LOG_MAXBURST
   7015 				sctp_log_maxburst(net, 0, burst_limit, SCTP_MAX_BURST_APPLIED);
   7016 #endif
   7017 				sctp_pegs[SCTP_MAX_BURST_APL]++;
   7018 			}
   7019 			net->fast_retran_ip = 0;
   7020 #endif
   7021 		}
   7022 
   7023 	}
   7024 	/* Fill up what we can to the destination */
   7025 	burst_cnt = 0;
   7026 	cwnd_full = 0;
   7027 	do {
   7028 #ifdef SCTP_DEBUG
   7029 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   7030 			printf("Burst count:%d - call m-c-o\n", burst_cnt);
   7031 		}
   7032 #endif
   7033 		error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
   7034 					      &reason_code, 0,  &cwnd_full, from_where,
   7035 					      &now, &now_filled);
   7036 		if (error) {
   7037 #ifdef SCTP_DEBUG
   7038 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   7039 				printf("Error %d was returned from med-c-op\n", error);
   7040 			}
   7041 #endif
   7042 #ifdef SCTP_LOG_MAXBURST
   7043 			sctp_log_maxburst(asoc->primary_destination, error , burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
   7044 #endif
   7045 			break;
   7046 		}
   7047 #ifdef SCTP_DEBUG
   7048 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
   7049 			printf("m-c-o put out %d\n", num_out);
   7050 		}
   7051 #endif
   7052 		tot_out += num_out;
   7053 		burst_cnt++;
   7054 	} while (num_out
   7055 #ifndef SCTP_USE_ALLMAN_BURST
   7056 		 &&  (burst_cnt < burst_limit)
   7057 #endif
   7058 		);
   7059 #ifndef SCTP_USE_ALLMAN_BURST
   7060 	if (burst_cnt >= burst_limit) {
   7061 		sctp_pegs[SCTP_MAX_BURST_APL]++;
   7062  		asoc->burst_limit_applied = 1;
   7063 #ifdef SCTP_LOG_MAXBURST
   7064 		sctp_log_maxburst(asoc->primary_destination, 0 , burst_cnt, SCTP_MAX_BURST_APPLIED);
   7065 #endif
   7066  	} else {
   7067 		asoc->burst_limit_applied = 0;
   7068  	}
   7069 #endif
   7070 
   7071 #ifdef SCTP_DEBUG
   7072 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   7073 		printf("Ok, we have put out %d chunks\n", tot_out);
   7074 	}
   7075 #endif
   7076 	if (tot_out == 0) {
   7077 		sctp_pegs[SCTP_CO_NODATASNT]++;
   7078 		if (asoc->stream_queue_cnt > 0) {
   7079 			sctp_pegs[SCTP_SOS_NOSNT]++;
   7080 		} else {
   7081 			sctp_pegs[SCTP_NOS_NOSNT]++;
   7082 		}
   7083 		if (asoc->send_queue_cnt > 0) {
   7084 			sctp_pegs[SCTP_SOSE_NOSNT]++;
   7085 		} else {
   7086 			sctp_pegs[SCTP_NOSE_NOSNT]++;
   7087 		}
   7088 	}
   7089 	/* Now we need to clean up the control chunk chain if
   7090 	 * a ECNE is on it. It must be marked as UNSENT again
   7091 	 * so next call will continue to send it until
   7092 	 * such time that we get a CWR, to remove it.
   7093 	 */
   7094 	sctp_fix_ecn_echo(asoc);
   7095 	return (error);
   7096 }
   7097 
   7098 
   7099 int
   7100 sctp_output(struct sctp_inpcb *inp, struct mbuf *m,
   7101      struct sockaddr *addr, struct mbuf *control, struct lwp *l, int flags)
   7102 {
   7103 	struct sctp_inpcb *t_inp;
   7104  	struct sctp_tcb *stcb;
   7105 	struct sctp_nets *net;
   7106 	struct sctp_association *asoc;
   7107 	int create_lock_applied = 0;
   7108 	int queue_only, error = 0;
   7109 	struct sctp_sndrcvinfo srcv;
   7110 	int un_sent = 0;
   7111 	int use_rcvinfo = 0;
   7112 	t_inp = inp;
   7113 	/*  struct route ro;*/
   7114 
   7115 	queue_only = 0;
   7116 	stcb = NULL;
   7117 	asoc = NULL;
   7118 	net = NULL;
   7119 
   7120 #ifdef SCTP_DEBUG
   7121 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   7122 		printf("USR Send BEGINS\n");
   7123 	}
   7124 #endif
   7125 
   7126 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
   7127 	    (inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING)) {
   7128 		/* The listner can NOT send */
   7129 		if (control) {
   7130 			sctppcbinfo.mbuf_track--;
   7131 			sctp_m_freem(control);
   7132 			control = NULL;
   7133 		}
   7134 		sctp_m_freem(m);
   7135 		return (EFAULT);
   7136 	}
   7137 	/* Can't allow a V6 address on a non-v6 socket */
   7138 	if (addr) {
   7139 		SCTP_ASOC_CREATE_LOCK(inp);
   7140 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
   7141 		    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
   7142 			/* Should I really unlock ? */
   7143 			SCTP_ASOC_CREATE_UNLOCK(inp);
   7144 			if (control) {
   7145 				sctppcbinfo.mbuf_track--;
   7146 				sctp_m_freem(control);
   7147 				control = NULL;
   7148 			}
   7149 			sctp_m_freem(m);
   7150 			return (EFAULT);
   7151 		}
   7152 		create_lock_applied = 1;
   7153 		if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
   7154 		    (addr->sa_family == AF_INET6)) {
   7155 			SCTP_ASOC_CREATE_UNLOCK(inp);
   7156 			if (control) {
   7157 				sctppcbinfo.mbuf_track--;
   7158 				sctp_m_freem(control);
   7159 				control = NULL;
   7160 			}
   7161 			sctp_m_freem(m);
   7162 			return (EINVAL);
   7163 		}
   7164 	}
   7165 	if (control) {
   7166 		sctppcbinfo.mbuf_track++;
   7167 		if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&srcv, control,
   7168 				   sizeof(srcv))) {
   7169 			if (srcv.sinfo_flags & SCTP_SENDALL) {
   7170 				/* its a sendall */
   7171 				sctppcbinfo.mbuf_track--;
   7172 				sctp_m_freem(control);
   7173 				if (create_lock_applied) {
   7174 					SCTP_ASOC_CREATE_UNLOCK(inp);
   7175 					create_lock_applied = 0;
   7176 				}
   7177 				return (sctp_sendall(inp, NULL, m, &srcv));
   7178 			}
   7179 			if (srcv.sinfo_assoc_id) {
   7180 				if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
   7181 					SCTP_INP_RLOCK(inp);
   7182 					stcb = LIST_FIRST(&inp->sctp_asoc_list);
   7183 					if (stcb) {
   7184 						SCTP_TCB_LOCK(stcb);
   7185 					}
   7186 					SCTP_INP_RUNLOCK(inp);
   7187 
   7188 					if (stcb == NULL) {
   7189 						if (create_lock_applied) {
   7190 							SCTP_ASOC_CREATE_UNLOCK(inp);
   7191 							create_lock_applied = 0;
   7192 						}
   7193 						sctppcbinfo.mbuf_track--;
   7194 						sctp_m_freem(control);
   7195 						sctp_m_freem(m);
   7196 						return (ENOTCONN);
   7197 					}
   7198 					net = stcb->asoc.primary_destination;
   7199 				} else {
   7200 					stcb = sctp_findassociation_ep_asocid(inp, srcv.sinfo_assoc_id);
   7201 				}
   7202 				/*
   7203 				 * Question: Should I error here if the
   7204 
   7205 				 * assoc_id is no longer valid?
   7206 				 * i.e. I can't find it?
   7207 				 */
   7208 				if ((stcb) &&
   7209 				    (addr != NULL)) {
   7210 					/* Must locate the net structure */
   7211 					if (addr)
   7212 						net = sctp_findnet(stcb, addr);
   7213 				}
   7214 				if (net == NULL)
   7215 					net = stcb->asoc.primary_destination;
   7216 			}
   7217 			use_rcvinfo = 1;
   7218 		}
   7219 	}
   7220 	if (stcb == NULL) {
   7221 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
   7222 			SCTP_INP_RLOCK(inp);
   7223 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
   7224 			if (stcb) {
   7225 				SCTP_TCB_LOCK(stcb);
   7226 			}
   7227 			SCTP_INP_RUNLOCK(inp);
   7228 			if (stcb == NULL) {
   7229 				if (create_lock_applied) {
   7230 					SCTP_ASOC_CREATE_UNLOCK(inp);
   7231 					create_lock_applied = 0;
   7232 				}
   7233 				if (control) {
   7234 					sctppcbinfo.mbuf_track--;
   7235 					sctp_m_freem(control);
   7236 					control = NULL;
   7237 				}
   7238 				sctp_m_freem(m);
   7239 				return (ENOTCONN);
   7240 			}
   7241 			if (addr == NULL) {
   7242 				net = stcb->asoc.primary_destination;
   7243 			} else {
   7244 				net = sctp_findnet(stcb, addr);
   7245 				if (net == NULL) {
   7246 					net = stcb->asoc.primary_destination;
   7247 				}
   7248 			}
   7249 		} else {
   7250 			if (addr != NULL) {
   7251 				SCTP_INP_WLOCK(inp);
   7252 				SCTP_INP_INCR_REF(inp);
   7253 				SCTP_INP_WUNLOCK(inp);
   7254 				stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
   7255 				if (stcb == NULL) {
   7256 					SCTP_INP_WLOCK(inp);
   7257 					SCTP_INP_DECR_REF(inp);
   7258 					SCTP_INP_WUNLOCK(inp);
   7259 				}
   7260 			}
   7261 		}
   7262 	}
   7263 	if ((stcb == NULL) &&
   7264 	    (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) {
   7265 		if (control) {
   7266 			sctppcbinfo.mbuf_track--;
   7267 			sctp_m_freem(control);
   7268 			control = NULL;
   7269 		}
   7270 		if (create_lock_applied) {
   7271 			SCTP_ASOC_CREATE_UNLOCK(inp);
   7272 			create_lock_applied = 0;
   7273 		}
   7274 		sctp_m_freem(m);
   7275 		return (ENOTCONN);
   7276 	} else if ((stcb == NULL) &&
   7277 		   (addr == NULL)) {
   7278 		if (control) {
   7279 			sctppcbinfo.mbuf_track--;
   7280 			sctp_m_freem(control);
   7281 			control = NULL;
   7282 		}
   7283 		if (create_lock_applied) {
   7284 			SCTP_ASOC_CREATE_UNLOCK(inp);
   7285 			create_lock_applied = 0;
   7286 		}
   7287 		sctp_m_freem(m);
   7288 		return (ENOENT);
   7289 	} else if (stcb == NULL) {
   7290 		/* UDP mode, we must go ahead and start the INIT process */
   7291 		if ((use_rcvinfo) && (srcv.sinfo_flags & SCTP_ABORT)) {
   7292 			/* Strange user to do this */
   7293 			if (control) {
   7294 				sctppcbinfo.mbuf_track--;
   7295 				sctp_m_freem(control);
   7296 				control = NULL;
   7297 			}
   7298 			if (create_lock_applied) {
   7299 				SCTP_ASOC_CREATE_UNLOCK(inp);
   7300 				create_lock_applied = 0;
   7301 			}
   7302 			sctp_m_freem(m);
   7303 			return (ENOENT);
   7304 		}
   7305 		stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0);
   7306 		if (stcb == NULL) {
   7307 			if (control) {
   7308 				sctppcbinfo.mbuf_track--;
   7309 				sctp_m_freem(control);
   7310 				control = NULL;
   7311 			}
   7312 			if (create_lock_applied) {
   7313 				SCTP_ASOC_CREATE_UNLOCK(inp);
   7314 				create_lock_applied = 0;
   7315 			}
   7316 			sctp_m_freem(m);
   7317 			return (error);
   7318 		}
   7319 		if (create_lock_applied) {
   7320 			SCTP_ASOC_CREATE_UNLOCK(inp);
   7321 			create_lock_applied = 0;
   7322 		} else {
   7323 			printf("Huh-1, create lock should have been applied!\n");
   7324 		}
   7325 		queue_only = 1;
   7326 		asoc = &stcb->asoc;
   7327 		asoc->state = SCTP_STATE_COOKIE_WAIT;
   7328 		SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
   7329 		if (control) {
   7330 			/* see if a init structure exists in cmsg headers */
   7331 			struct sctp_initmsg initm;
   7332 			int i;
   7333 			if (sctp_find_cmsg(SCTP_INIT, (void *)&initm, control,
   7334 					   sizeof(initm))) {
   7335 				/* we have an INIT override of the default */
   7336 				if (initm.sinit_max_attempts)
   7337 					asoc->max_init_times = initm.sinit_max_attempts;
   7338 				if (initm.sinit_num_ostreams)
   7339 					asoc->pre_open_streams = initm.sinit_num_ostreams;
   7340 				if (initm.sinit_max_instreams)
   7341 					asoc->max_inbound_streams = initm.sinit_max_instreams;
   7342 				if (initm.sinit_max_init_timeo)
   7343 					asoc->initial_init_rto_max = initm.sinit_max_init_timeo;
   7344 			}
   7345 			if (asoc->streamoutcnt < asoc->pre_open_streams) {
   7346 				/* Default is NOT correct */
   7347 #ifdef SCTP_DEBUG
   7348 				if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   7349 					printf("Ok, defout:%d pre_open:%d\n",
   7350 					       asoc->streamoutcnt, asoc->pre_open_streams);
   7351 				}
   7352 #endif
   7353 				free(asoc->strmout, M_PCB);
   7354 				asoc->strmout = NULL;
   7355 				asoc->streamoutcnt = asoc->pre_open_streams;
   7356 				asoc->strmout = malloc(asoc->streamoutcnt *
   7357 				       sizeof(struct sctp_stream_out), M_PCB,
   7358 				       M_WAIT);
   7359 				for (i = 0; i < asoc->streamoutcnt; i++) {
   7360 					/*
   7361 					 * inbound side must be set to 0xffff,
   7362 					 * also NOTE when we get the INIT-ACK
   7363 					 * back (for INIT sender) we MUST
   7364 					 * reduce the count (streamoutcnt) but
   7365 					 * first check if we sent to any of the
   7366 					 * upper streams that were dropped (if
   7367 					 * some were). Those that were dropped
   7368 					 * must be notified to the upper layer
   7369 					 * as failed to send.
   7370 					 */
   7371 					asoc->strmout[i].next_sequence_sent = 0x0;
   7372 					TAILQ_INIT(&asoc->strmout[i].outqueue);
   7373 					asoc->strmout[i].stream_no = i;
   7374 					asoc->strmout[i].next_spoke.tqe_next = 0;
   7375 					asoc->strmout[i].next_spoke.tqe_prev = 0;
   7376 				}
   7377 			}
   7378 		}
   7379 		sctp_send_initiate(inp, stcb);
   7380 		/*
   7381 		 * we may want to dig in after this call and adjust the MTU
   7382 		 * value. It defaulted to 1500 (constant) but the ro structure
   7383 		 * may now have an update and thus we may need to change it
   7384 		 * BEFORE we append the message.
   7385 		 */
   7386 		net = stcb->asoc.primary_destination;
   7387 	} else {
   7388 		if (create_lock_applied) {
   7389 			SCTP_ASOC_CREATE_UNLOCK(inp);
   7390 			create_lock_applied = 0;
   7391 		}
   7392 		asoc = &stcb->asoc;
   7393 		if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
   7394 		    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
   7395 			queue_only = 1;
   7396 		}
   7397 		if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
   7398 		    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
   7399 		    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
   7400 		    (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
   7401 			if (control) {
   7402 				sctppcbinfo.mbuf_track--;
   7403 				sctp_m_freem(control);
   7404 				control = NULL;
   7405 			}
   7406 			if ((use_rcvinfo) &&
   7407 			    (srcv.sinfo_flags & SCTP_ABORT)) {
   7408 				sctp_msg_append(stcb, net, m, &srcv, flags);
   7409 				error = 0;
   7410 			} else {
   7411 				if (m)
   7412 					sctp_m_freem(m);
   7413 				error = ECONNRESET;
   7414 			}
   7415 			SCTP_TCB_UNLOCK(stcb);
   7416 			return (error);
   7417 		}
   7418 	}
   7419 	if (create_lock_applied) {
   7420 		/* we should never hit here with the create lock applied
   7421 		 *
   7422 		 */
   7423 		SCTP_ASOC_CREATE_UNLOCK(inp);
   7424 		create_lock_applied = 0;
   7425 	}
   7426 
   7427 
   7428 	if (use_rcvinfo == 0) {
   7429 		srcv = stcb->asoc.def_send;
   7430 	}
   7431 #ifdef SCTP_DEBUG
   7432 	else {
   7433 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT5) {
   7434 			printf("stream:%d\n", srcv.sinfo_stream);
   7435 			printf("flags:%x\n", (u_int)srcv.sinfo_flags);
   7436 			printf("ppid:%d\n", srcv.sinfo_ppid);
   7437 			printf("context:%d\n", srcv.sinfo_context);
   7438 		}
   7439 	}
   7440 #endif
   7441 	if (control) {
   7442 		sctppcbinfo.mbuf_track--;
   7443 		sctp_m_freem(control);
   7444 		control = NULL;
   7445 	}
   7446 	if (net && ((srcv.sinfo_flags & SCTP_ADDR_OVER))) {
   7447 		/* we take the override or the unconfirmed */
   7448 		;
   7449 	} else {
   7450 		net = stcb->asoc.primary_destination;
   7451 	}
   7452 	if ((error = sctp_msg_append(stcb, net, m, &srcv, flags))) {
   7453 		SCTP_TCB_UNLOCK(stcb);
   7454 		return (error);
   7455 	}
   7456 	if (net->flight_size > net->cwnd) {
   7457 		sctp_pegs[SCTP_SENDTO_FULL_CWND]++;
   7458 		queue_only = 1;
   7459  	} else if (asoc->ifp_had_enobuf) {
   7460 		sctp_pegs[SCTP_QUEONLY_BURSTLMT]++;
   7461 	 	queue_only = 1;
   7462  	} else {
   7463 		un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
   7464 			   ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) * sizeof(struct sctp_data_chunk)) +
   7465 			   SCTP_MED_OVERHEAD);
   7466 
   7467 		if (((inp->sctp_flags & SCTP_PCB_FLAGS_NODELAY) == 0) &&
   7468 		    (stcb->asoc.total_flight > 0) &&
   7469 		    (un_sent < (int)stcb->asoc.smallest_mtu)
   7470 			) {
   7471 
   7472 			/* Ok, Nagle is set on and we have
   7473 			 * data outstanding. Don't send anything
   7474 			 * and let the SACK drive out the data.
   7475 			 */
   7476 			sctp_pegs[SCTP_NAGLE_NOQ]++;
   7477 			queue_only = 1;
   7478 		} else {
   7479 			sctp_pegs[SCTP_NAGLE_OFF]++;
   7480 		}
   7481 	}
   7482 	if ((queue_only == 0) && stcb->asoc.peers_rwnd) {
   7483 		/* we can attempt to send too.*/
   7484 #ifdef SCTP_DEBUG
   7485 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   7486 			printf("USR Send calls sctp_chunk_output\n");
   7487 		}
   7488 #endif
   7489 #ifdef SCTP_AUDITING_ENABLED
   7490 		sctp_audit_log(0xC0, 1);
   7491 		sctp_auditing(6, inp, stcb, net);
   7492 #endif
   7493 		sctp_pegs[SCTP_OUTPUT_FRM_SND]++;
   7494 		sctp_chunk_output(inp, stcb, 0);
   7495 #ifdef SCTP_AUDITING_ENABLED
   7496 		sctp_audit_log(0xC0, 2);
   7497 		sctp_auditing(7, inp, stcb, net);
   7498 #endif
   7499 
   7500 	}
   7501 #ifdef SCTP_DEBUG
   7502 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   7503 		printf("USR Send complete qo:%d prw:%d\n", queue_only, stcb->asoc.peers_rwnd);
   7504 	}
   7505 #endif
   7506 	SCTP_TCB_UNLOCK(stcb);
   7507 	return (0);
   7508 }
   7509 
   7510 void
   7511 send_forward_tsn(struct sctp_tcb *stcb,
   7512 		 struct sctp_association *asoc)
   7513 {
   7514 	struct sctp_tmit_chunk *chk;
   7515 	struct sctp_forward_tsn_chunk *fwdtsn;
   7516 
   7517 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
   7518 		if (chk->rec.chunk_id == SCTP_FORWARD_CUM_TSN) {
   7519 			/* mark it to unsent */
   7520 			chk->sent = SCTP_DATAGRAM_UNSENT;
   7521 			chk->snd_count = 0;
   7522 			/* Do we correct its output location? */
   7523 			if (chk->whoTo != asoc->primary_destination) {
   7524 				sctp_free_remote_addr(chk->whoTo);
   7525 				chk->whoTo = asoc->primary_destination;
   7526 				chk->whoTo->ref_count++;
   7527 			}
   7528 			goto sctp_fill_in_rest;
   7529 		}
   7530 	}
   7531 	/* Ok if we reach here we must build one */
   7532 	chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
   7533 	if (chk == NULL) {
   7534 		return;
   7535 	}
   7536 	sctppcbinfo.ipi_count_chunk++;
   7537 	sctppcbinfo.ipi_gencnt_chunk++;
   7538 	chk->rec.chunk_id = SCTP_FORWARD_CUM_TSN;
   7539 	chk->asoc = asoc;
   7540 	MGETHDR(chk->data, M_DONTWAIT, MT_DATA);
   7541 	if (chk->data == NULL) {
   7542 		chk->whoTo->ref_count--;
   7543 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   7544 		sctppcbinfo.ipi_count_chunk--;
   7545 		if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   7546 			panic("Chunk count is negative");
   7547 		}
   7548 		sctppcbinfo.ipi_gencnt_chunk++;
   7549 		return;
   7550 	}
   7551 	chk->data->m_data += SCTP_MIN_OVERHEAD;
   7552 	chk->sent = SCTP_DATAGRAM_UNSENT;
   7553 	chk->snd_count = 0;
   7554 	chk->whoTo = asoc->primary_destination;
   7555 	chk->whoTo->ref_count++;
   7556 	TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
   7557 	asoc->ctrl_queue_cnt++;
   7558  sctp_fill_in_rest:
   7559 	/* Here we go through and fill out the part that
   7560 	 * deals with stream/seq of the ones we skip.
   7561 	 */
   7562 	chk->data->m_pkthdr.len = chk->data->m_len = 0;
   7563 	{
   7564 		struct sctp_tmit_chunk *at, *tp1, *last;
   7565 		struct sctp_strseq *strseq;
   7566 		unsigned int cnt_of_space, i, ovh;
   7567 		unsigned int space_needed;
   7568 		unsigned int cnt_of_skipped = 0;
   7569 		TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
   7570 			if (at->sent != SCTP_FORWARD_TSN_SKIP) {
   7571 				/* no more to look at */
   7572 				break;
   7573 			}
   7574 			if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
   7575 				/* We don't report these */
   7576 				continue;
   7577 			}
   7578 			cnt_of_skipped++;
   7579 		}
   7580 		space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
   7581 				(cnt_of_skipped * sizeof(struct sctp_strseq)));
   7582 		if ((M_TRAILINGSPACE(chk->data) < (int)space_needed) &&
   7583 		    ((chk->data->m_flags & M_EXT) == 0)) {
   7584 			/* Need a M_EXT, get one and move
   7585 			 * fwdtsn to data area.
   7586 			 */
   7587 			MCLGET(chk->data, M_DONTWAIT);
   7588 		}
   7589 		cnt_of_space = M_TRAILINGSPACE(chk->data);
   7590 
   7591 		if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
   7592 			ovh = SCTP_MIN_OVERHEAD;
   7593 		} else {
   7594 			ovh = SCTP_MIN_V4_OVERHEAD;
   7595 		}
   7596 		if (cnt_of_space > (asoc->smallest_mtu-ovh)) {
   7597 			/* trim to a mtu size */
   7598 			cnt_of_space = asoc->smallest_mtu - ovh;
   7599 		}
   7600 		if (cnt_of_space < space_needed) {
   7601 			/* ok we must trim down the chunk by lowering
   7602 			 * the advance peer ack point.
   7603 			 */
   7604 			cnt_of_skipped = (cnt_of_space-
   7605 					  ((sizeof(struct sctp_forward_tsn_chunk))/
   7606  					    sizeof(struct sctp_strseq)));
   7607 			/* Go through and find the TSN that
   7608 			 * will be the one we report.
   7609 			 */
   7610 			at = TAILQ_FIRST(&asoc->sent_queue);
   7611 			for (i = 0; i < cnt_of_skipped; i++) {
   7612 				tp1 = TAILQ_NEXT(at, sctp_next);
   7613 				at = tp1;
   7614 			}
   7615 			last = at;
   7616 			/* last now points to last one I can report, update peer ack point */
   7617 			asoc->advanced_peer_ack_point = last->rec.data.TSN_seq;
   7618 			space_needed -= (cnt_of_skipped * sizeof(struct sctp_strseq));
   7619 		}
   7620 		chk->send_size = space_needed;
   7621 		/* Setup the chunk */
   7622 		fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
   7623 		fwdtsn->ch.chunk_length = htons(chk->send_size);
   7624 		fwdtsn->ch.chunk_flags = 0;
   7625 		fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
   7626 		fwdtsn->new_cumulative_tsn = htonl(asoc->advanced_peer_ack_point);
   7627 		chk->send_size = (sizeof(struct sctp_forward_tsn_chunk) +
   7628 				  (cnt_of_skipped * sizeof(struct sctp_strseq)));
   7629 		chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
   7630 		fwdtsn++;
   7631 		/* Move pointer to after the fwdtsn and transfer to
   7632 		 * the strseq pointer.
   7633 		 */
   7634 		strseq = (struct sctp_strseq *)fwdtsn;
   7635 		/*
   7636 		 * Now populate the strseq list. This is done blindly
   7637 		 * without pulling out duplicate stream info. This is
   7638 		 * inefficent but won't harm the process since the peer
   7639 		 * will look at these in sequence and will thus release
   7640 		 * anything. It could mean we exceed the PMTU and chop
   7641 		 * off some that we could have included.. but this is
   7642 		 * unlikely (aka 1432/4 would mean 300+ stream seq's would
   7643 		 * have to be reported in one FWD-TSN. With a bit of work
   7644 		 * we can later FIX this to optimize and pull out duplcates..
   7645 		 * but it does add more overhead. So for now... not!
   7646 		 */
   7647 		at = TAILQ_FIRST(&asoc->sent_queue);
   7648 		for (i = 0; i < cnt_of_skipped; i++) {
   7649 			tp1 = TAILQ_NEXT(at, sctp_next);
   7650 			if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
   7651 				/* We don't report these */
   7652 				i--;
   7653 				at = tp1;
   7654 				continue;
   7655 			}
   7656 			strseq->stream = ntohs(at->rec.data.stream_number);
   7657 			strseq->sequence = ntohs(at->rec.data.stream_seq);
   7658 			strseq++;
   7659 			at = tp1;
   7660 		}
   7661 	}
   7662 	return;
   7663 
   7664 }
   7665 
   7666 void
   7667 sctp_send_sack(struct sctp_tcb *stcb)
   7668 {
   7669 	/*
   7670 	 * Queue up a SACK in the control queue. We must first check to
   7671 	 * see if a SACK is somehow on the control queue. If so, we will
   7672 	 * take and and remove the old one.
   7673 	 */
   7674 	struct sctp_association *asoc;
   7675 	struct sctp_tmit_chunk *chk, *a_chk;
   7676 	struct sctp_sack_chunk *sack;
   7677 	struct sctp_gap_ack_block *gap_descriptor;
   7678 	uint32_t *dup;
   7679 	int start;
   7680 	unsigned int i, maxi, seeing_ones, m_size;
   7681 	unsigned int num_gap_blocks, space;
   7682 
   7683 	start = maxi = 0;
   7684 	seeing_ones = 1;
   7685 	a_chk = NULL;
   7686 	asoc = &stcb->asoc;
   7687 	if (asoc->last_data_chunk_from == NULL) {
   7688 		/* Hmm we never received anything */
   7689 		return;
   7690 	}
   7691 	sctp_set_rwnd(stcb, asoc);
   7692 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
   7693 		if (chk->rec.chunk_id == SCTP_SELECTIVE_ACK) {
   7694 			/* Hmm, found a sack already on queue, remove it */
   7695 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
   7696 			asoc->ctrl_queue_cnt++;
   7697 			a_chk = chk;
   7698 			if (a_chk->data)
   7699 				sctp_m_freem(a_chk->data);
   7700 			a_chk->data = NULL;
   7701 			sctp_free_remote_addr(a_chk->whoTo);
   7702 			a_chk->whoTo = NULL;
   7703 			break;
   7704 		}
   7705 	}
   7706 	if (a_chk == NULL) {
   7707 		a_chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
   7708 		if (a_chk == NULL) {
   7709 			/* No memory so we drop the idea, and set a timer */
   7710 			sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
   7711 					stcb->sctp_ep, stcb, NULL);
   7712 			sctp_timer_start(SCTP_TIMER_TYPE_RECV,
   7713 					 stcb->sctp_ep, stcb, NULL);
   7714 			return;
   7715 		}
   7716 		sctppcbinfo.ipi_count_chunk++;
   7717 		sctppcbinfo.ipi_gencnt_chunk++;
   7718 		a_chk->rec.chunk_id = SCTP_SELECTIVE_ACK;
   7719 	}
   7720 	a_chk->asoc = asoc;
   7721 	a_chk->snd_count = 0;
   7722 	a_chk->send_size = 0;	/* fill in later */
   7723 	a_chk->sent = SCTP_DATAGRAM_UNSENT;
   7724 	m_size = (asoc->mapping_array_size << 3);
   7725 
   7726 	if ((asoc->numduptsns) ||
   7727 	    (asoc->last_data_chunk_from->dest_state & SCTP_ADDR_NOT_REACHABLE)
   7728 		) {
   7729 		/* Ok, we have some duplicates or the destination for the
   7730 		 * sack is unreachable, lets see if we can select an alternate
   7731 		 * than asoc->last_data_chunk_from
   7732 		 */
   7733 		if ((!(asoc->last_data_chunk_from->dest_state &
   7734 		      SCTP_ADDR_NOT_REACHABLE)) &&
   7735 		    (asoc->used_alt_onsack > 2)) {
   7736 			/* We used an alt last time, don't this time */
   7737 			a_chk->whoTo = NULL;
   7738 		} else {
   7739 			asoc->used_alt_onsack++;
   7740 			a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from);
   7741 		}
   7742 		if (a_chk->whoTo == NULL) {
   7743 			/* Nope, no alternate */
   7744 			a_chk->whoTo = asoc->last_data_chunk_from;
   7745 			asoc->used_alt_onsack = 0;
   7746 		}
   7747 	} else {
   7748 		/* No duplicates so we use the last
   7749 		 * place we received data from.
   7750 		 */
   7751 #ifdef SCTP_DEBUG
   7752 		if (asoc->last_data_chunk_from == NULL) {
   7753 			printf("Huh, last_data_chunk_from is null when we want to sack??\n");
   7754 		}
   7755 #endif
   7756 		asoc->used_alt_onsack = 0;
   7757 		a_chk->whoTo = asoc->last_data_chunk_from;
   7758 	}
   7759 	if (a_chk->whoTo)
   7760 		a_chk->whoTo->ref_count++;
   7761 
   7762 	/* Ok now lets formulate a MBUF with our sack */
   7763 	MGETHDR(a_chk->data, M_DONTWAIT, MT_DATA);
   7764 	if ((a_chk->data == NULL) ||
   7765 	    (a_chk->whoTo == NULL)) {
   7766 		/* rats, no mbuf memory */
   7767 		if (a_chk->data) {
   7768 			/* was a problem with the destination */
   7769 			sctp_m_freem(a_chk->data);
   7770 			a_chk->data = NULL;
   7771 		}
   7772 		a_chk->whoTo->ref_count--;
   7773 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, a_chk);
   7774 		sctppcbinfo.ipi_count_chunk--;
   7775 		if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   7776 			panic("Chunk count is negative");
   7777 		}
   7778 		sctppcbinfo.ipi_gencnt_chunk++;
   7779 		sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
   7780 				stcb->sctp_ep, stcb, NULL);
   7781 		sctp_timer_start(SCTP_TIMER_TYPE_RECV,
   7782 				 stcb->sctp_ep, stcb, NULL);
   7783 		return;
   7784 	}
   7785 	/* First count the number of gap ack blocks we need */
   7786 	if (asoc->highest_tsn_inside_map == asoc->cumulative_tsn) {
   7787 		/* We know if there are none above the cum-ack we
   7788 		 * have everything with NO gaps
   7789 		 */
   7790 		num_gap_blocks = 0;
   7791 	} else {
   7792 		/* Ok we must count how many gaps we
   7793 		 * have.
   7794 		 */
   7795 		num_gap_blocks = 0;
   7796 		if (asoc->highest_tsn_inside_map >= asoc->mapping_array_base_tsn) {
   7797 			maxi = (asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn);
   7798 		} else {
   7799 			maxi = (asoc->highest_tsn_inside_map  + (MAX_TSN - asoc->mapping_array_base_tsn) + 1);
   7800 		}
   7801 		if (maxi > m_size) {
   7802 			/* impossible but who knows, someone is playing with us  :> */
   7803 #ifdef SCTP_DEBUG
   7804 			printf("GAK maxi:%d  > m_size:%d came out higher than allowed htsn:%u base:%u cumack:%u\n",
   7805 			       maxi,
   7806 			       m_size,
   7807 			       asoc->highest_tsn_inside_map,
   7808 			       asoc->mapping_array_base_tsn,
   7809 			       asoc->cumulative_tsn
   7810 			       );
   7811 #endif
   7812 			num_gap_blocks = 0;
   7813 			goto no_gaps_now;
   7814 		}
   7815 		if (asoc->cumulative_tsn >= asoc->mapping_array_base_tsn) {
   7816 			start = (asoc->cumulative_tsn - asoc->mapping_array_base_tsn);
   7817 		} else {
   7818 			/* Set it so we start at 0 */
   7819 			start = -1;
   7820 		}
   7821 		/* Ok move start up one to look at the NEXT past the cum-ack */
   7822 		start++;
   7823 		for (i = start; i <= maxi; i++) {
   7824 			if (seeing_ones) {
   7825 				/* while seeing ones I must
   7826 				 * transition back to 0 before
   7827 				 * finding the next gap and
   7828 				 * counting the segment.
   7829 				 */
   7830 				if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, i) == 0) {
   7831 					seeing_ones = 0;
   7832 				}
   7833 			} else {
   7834 				if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, i)) {
   7835 					seeing_ones = 1;
   7836 					num_gap_blocks++;
   7837 				}
   7838 			}
   7839 		}
   7840 	no_gaps_now:
   7841 		if (num_gap_blocks == 0) {
   7842 			/*
   7843 			 * Traveled all of the bits and NO one,
   7844 			 * must have reneged
   7845 			 */
   7846 			if (compare_with_wrap(asoc->cumulative_tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
   7847 			   asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
   7848 #ifdef SCTP_MAP_LOGGING
   7849 			   sctp_log_map(0, 4, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
   7850 #endif
   7851 			}
   7852 		}
   7853 	}
   7854 
   7855 	/* Now calculate the space needed */
   7856 	space = (sizeof(struct sctp_sack_chunk) +
   7857 		 (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
   7858 		 (asoc->numduptsns * sizeof(int32_t))
   7859 		);
   7860 	if (space > (asoc->smallest_mtu-SCTP_MAX_OVERHEAD)) {
   7861 		/* Reduce the size of the sack to fit */
   7862 		int calc, fit;
   7863 		calc = (asoc->smallest_mtu - SCTP_MAX_OVERHEAD);
   7864 		calc -= sizeof(struct sctp_gap_ack_block);
   7865 		fit = calc/sizeof(struct sctp_gap_ack_block);
   7866 		if (fit > (int)num_gap_blocks) {
   7867 			/* discard some dups */
   7868 			asoc->numduptsns = (fit - num_gap_blocks);
   7869 		} else {
   7870 			/* discard all dups and some gaps */
   7871 			num_gap_blocks = fit;
   7872 			asoc->numduptsns = 0;
   7873 		}
   7874 		/* recalc space */
   7875 		space = (sizeof(struct sctp_sack_chunk) +
   7876 			 (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
   7877 			 (asoc->numduptsns * sizeof(int32_t))
   7878 			);
   7879 
   7880 	}
   7881 
   7882 	if ((space+SCTP_MIN_OVERHEAD) > MHLEN) {
   7883 		/* We need a cluster */
   7884 		MCLGET(a_chk->data, M_DONTWAIT);
   7885 		if ((a_chk->data->m_flags & M_EXT) != M_EXT) {
   7886 			/* can't get a cluster
   7887 			 * give up and try later.
   7888 			 */
   7889 			if (a_chk->data)
   7890 				sctp_m_freem(a_chk->data);
   7891 			a_chk->data = NULL;
   7892 			a_chk->whoTo->ref_count--;
   7893 			SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, a_chk);
   7894 			sctppcbinfo.ipi_count_chunk--;
   7895 			if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   7896 				panic("Chunk count is negative");
   7897 			}
   7898 			sctppcbinfo.ipi_gencnt_chunk++;
   7899 			sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
   7900 					stcb->sctp_ep, stcb, NULL);
   7901 			sctp_timer_start(SCTP_TIMER_TYPE_RECV,
   7902 					 stcb->sctp_ep, stcb, NULL);
   7903 			return;
   7904 		}
   7905 	}
   7906 
   7907 	/* ok, lets go through and fill it in */
   7908 	a_chk->data->m_data += SCTP_MIN_OVERHEAD;
   7909 	sack = mtod(a_chk->data, struct sctp_sack_chunk *);
   7910 	sack->ch.chunk_type = SCTP_SELECTIVE_ACK;
   7911 	sack->ch.chunk_flags = asoc->receiver_nonce_sum & SCTP_SACK_NONCE_SUM;
   7912 	sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
   7913 	sack->sack.a_rwnd = htonl(asoc->my_rwnd);
   7914 	asoc->my_last_reported_rwnd = asoc->my_rwnd;
   7915 	sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
   7916 	sack->sack.num_dup_tsns = htons(asoc->numduptsns);
   7917 
   7918 	a_chk->send_size = (sizeof(struct sctp_sack_chunk) +
   7919 			    (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
   7920 			    (asoc->numduptsns * sizeof(int32_t)));
   7921 	a_chk->data->m_pkthdr.len = a_chk->data->m_len = a_chk->send_size;
   7922 	sack->ch.chunk_length = htons(a_chk->send_size);
   7923 
   7924 	gap_descriptor = (struct sctp_gap_ack_block *)((vaddr_t)sack + sizeof(struct sctp_sack_chunk));
   7925 	seeing_ones = 0;
   7926 	for (i = start; i <= maxi; i++) {
   7927 		if (num_gap_blocks == 0) {
   7928 			break;
   7929 		}
   7930 		if (seeing_ones) {
   7931 			/* while seeing Ones I must
   7932 			 * transition back to 0 before
   7933 			 * finding the next gap
   7934 			 */
   7935 			if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, i) == 0) {
   7936 				gap_descriptor->end = htons(((uint16_t)(i-start)));
   7937 				gap_descriptor++;
   7938 				seeing_ones = 0;
   7939 				num_gap_blocks--;
   7940 			}
   7941 		} else {
   7942 			if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, i)) {
   7943 				gap_descriptor->start = htons(((uint16_t)(i+1-start)));
   7944 				/* advance struct to next pointer */
   7945 				seeing_ones = 1;
   7946 			}
   7947 		}
   7948 	}
   7949 	if (num_gap_blocks) {
   7950 		/* special case where the array is all 1's
   7951 		 * to the end of the array.
   7952 		 */
   7953 		gap_descriptor->end = htons(((uint16_t)((i-start))));
   7954 		gap_descriptor++;
   7955 	}
   7956 	/* now we must add any dups we are going to report. */
   7957 	if (asoc->numduptsns) {
   7958 		dup = (uint32_t *)gap_descriptor;
   7959 		for (i = 0; i < asoc->numduptsns; i++) {
   7960 			*dup = htonl(asoc->dup_tsns[i]);
   7961 			dup++;
   7962 		}
   7963 		asoc->numduptsns = 0;
   7964 	}
   7965 	/* now that the chunk is prepared queue it to the control
   7966 	 * chunk queue.
   7967 	 */
   7968 	TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
   7969 	asoc->ctrl_queue_cnt++;
   7970 	sctp_pegs[SCTP_PEG_SACKS_SENT]++;
   7971 	return;
   7972 }
   7973 
   7974 void
   7975 sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr)
   7976 {
   7977 	struct mbuf *m_abort;
   7978 	struct sctp_abort_msg *abort_m;
   7979 	int sz;
   7980 	abort_m = NULL;
   7981 	MGETHDR(m_abort, M_DONTWAIT, MT_HEADER);
   7982 	if (m_abort == NULL) {
   7983 		/* no mbuf's */
   7984 		return;
   7985 	}
   7986 	m_abort->m_data += SCTP_MIN_OVERHEAD;
   7987 	abort_m = mtod(m_abort, struct sctp_abort_msg *);
   7988 	m_abort->m_len = sizeof(struct sctp_abort_msg);
   7989 	m_abort->m_next = operr;
   7990 	sz = 0;
   7991 	if (operr) {
   7992 		struct mbuf *n;
   7993 		n = operr;
   7994 		while (n) {
   7995 			sz += n->m_len;
   7996 			n = n->m_next;
   7997 		}
   7998 	}
   7999 	abort_m->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
   8000 	abort_m->msg.ch.chunk_flags = 0;
   8001 	abort_m->msg.ch.chunk_length = htons(sizeof(struct sctp_abort_chunk) +
   8002 					     sz);
   8003 	abort_m->sh.src_port = stcb->sctp_ep->sctp_lport;
   8004 	abort_m->sh.dest_port = stcb->rport;
   8005 	abort_m->sh.v_tag = htonl(stcb->asoc.peer_vtag);
   8006 	abort_m->sh.checksum = 0;
   8007 	m_abort->m_pkthdr.len = m_abort->m_len + sz;
   8008 	m_reset_rcvif(m_abort);
   8009 	sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb,
   8010 	    stcb->asoc.primary_destination,
   8011 	    rtcache_getdst(&stcb->asoc.primary_destination->ro),
   8012 	    m_abort, 1, 0, NULL, 0);
   8013 }
   8014 
   8015 int
   8016 sctp_send_shutdown_complete(struct sctp_tcb *stcb,
   8017 			    struct sctp_nets *net)
   8018 
   8019 {
   8020 	/* formulate and SEND a SHUTDOWN-COMPLETE */
   8021 	struct mbuf *m_shutdown_comp;
   8022 	struct sctp_shutdown_complete_msg *comp_cp;
   8023 
   8024 	m_shutdown_comp = NULL;
   8025 	MGETHDR(m_shutdown_comp, M_DONTWAIT, MT_HEADER);
   8026 	if (m_shutdown_comp == NULL) {
   8027 		/* no mbuf's */
   8028 		return (-1);
   8029 	}
   8030 	m_shutdown_comp->m_data += sizeof(struct ip6_hdr);
   8031 	comp_cp = mtod(m_shutdown_comp, struct sctp_shutdown_complete_msg *);
   8032 	comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
   8033 	comp_cp->shut_cmp.ch.chunk_flags = 0;
   8034 	comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
   8035 	comp_cp->sh.src_port = stcb->sctp_ep->sctp_lport;
   8036 	comp_cp->sh.dest_port = stcb->rport;
   8037 	comp_cp->sh.v_tag = htonl(stcb->asoc.peer_vtag);
   8038 	comp_cp->sh.checksum = 0;
   8039 
   8040 	m_shutdown_comp->m_pkthdr.len = m_shutdown_comp->m_len = sizeof(struct sctp_shutdown_complete_msg);
   8041 	m_reset_rcvif(m_shutdown_comp);
   8042 	sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
   8043 	    rtcache_getdst(&net->ro), m_shutdown_comp,
   8044 	    1, 0, NULL, 0);
   8045 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
   8046 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
   8047 		stcb->sctp_ep->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
   8048 		stcb->sctp_ep->sctp_socket->so_snd.sb_cc = 0;
   8049 		soisdisconnected(stcb->sctp_ep->sctp_socket);
   8050 	}
   8051 	return (0);
   8052 }
   8053 
   8054 int
   8055 sctp_send_shutdown_complete2(struct mbuf *m, int iphlen, struct sctphdr *sh)
   8056 {
   8057 	/* formulate and SEND a SHUTDOWN-COMPLETE */
   8058 	struct mbuf *mout;
   8059 	struct ip *iph, *iph_out;
   8060 	struct ip6_hdr *ip6, *ip6_out;
   8061 	int offset_out;
   8062 	struct sctp_shutdown_complete_msg *comp_cp;
   8063 
   8064 	MGETHDR(mout, M_DONTWAIT, MT_HEADER);
   8065 	if (mout == NULL) {
   8066 		/* no mbuf's */
   8067 		return (-1);
   8068 	}
   8069 	iph = mtod(m, struct ip *);
   8070 	iph_out = NULL;
   8071 	ip6_out = NULL;
   8072 	offset_out = 0;
   8073 	if (iph->ip_v == IPVERSION) {
   8074 		mout->m_len = sizeof(struct ip) +
   8075 		    sizeof(struct sctp_shutdown_complete_msg);
   8076 		mout->m_next = NULL;
   8077 		iph_out = mtod(mout, struct ip *);
   8078 
   8079 		/* Fill in the IP header for the ABORT */
   8080 		iph_out->ip_v = IPVERSION;
   8081 		iph_out->ip_hl = (sizeof(struct ip)/4);
   8082 		iph_out->ip_tos = (u_char)0;
   8083 		iph_out->ip_id = 0;
   8084 		iph_out->ip_off = 0;
   8085 		iph_out->ip_ttl = MAXTTL;
   8086 		iph_out->ip_p = IPPROTO_SCTP;
   8087 		iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
   8088 		iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
   8089 
   8090 		/* let IP layer calculate this */
   8091 		iph_out->ip_sum = 0;
   8092 		offset_out += sizeof(*iph_out);
   8093 		comp_cp = (struct sctp_shutdown_complete_msg *)(
   8094 		    (vaddr_t)iph_out + offset_out);
   8095 	} else if (iph->ip_v == (IPV6_VERSION >> 4)) {
   8096 		ip6 = (struct ip6_hdr *)iph;
   8097 		mout->m_len = sizeof(struct ip6_hdr) +
   8098 		    sizeof(struct sctp_shutdown_complete_msg);
   8099 		mout->m_next = NULL;
   8100 		ip6_out = mtod(mout, struct ip6_hdr *);
   8101 
   8102 		/* Fill in the IPv6 header for the ABORT */
   8103 		ip6_out->ip6_flow = ip6->ip6_flow;
   8104 		ip6_out->ip6_hlim = ip6_defhlim;
   8105 		ip6_out->ip6_nxt = IPPROTO_SCTP;
   8106 		ip6_out->ip6_src = ip6->ip6_dst;
   8107 		ip6_out->ip6_dst = ip6->ip6_src;
   8108  		ip6_out->ip6_plen = mout->m_len;
   8109 		offset_out += sizeof(*ip6_out);
   8110 		comp_cp = (struct sctp_shutdown_complete_msg *)(
   8111 		    (vaddr_t)ip6_out + offset_out);
   8112 	} else {
   8113 		/* Currently not supported. */
   8114 		return (-1);
   8115 	}
   8116 
   8117 	/* Now copy in and fill in the ABORT tags etc. */
   8118 	comp_cp->sh.src_port = sh->dest_port;
   8119 	comp_cp->sh.dest_port = sh->src_port;
   8120 	comp_cp->sh.checksum = 0;
   8121 	comp_cp->sh.v_tag = sh->v_tag;
   8122 	comp_cp->shut_cmp.ch.chunk_flags = SCTP_HAD_NO_TCB;
   8123 	comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
   8124 	comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
   8125 
   8126 	mout->m_pkthdr.len = mout->m_len;
   8127 	/* add checksum */
   8128 	if ((sctp_no_csum_on_loopback) && m_get_rcvif_NOMPSAFE(m) != NULL &&
   8129 	    m_get_rcvif_NOMPSAFE(m)->if_type == IFT_LOOP) {
   8130 		comp_cp->sh.checksum =  0;
   8131 	} else {
   8132 		comp_cp->sh.checksum = sctp_calculate_sum(mout, NULL, offset_out);
   8133 	}
   8134 
   8135 	/* zap the rcvif, it should be null */
   8136 	m_reset_rcvif(mout);
   8137 	/* zap the stack pointer to the route */
   8138 	if (iph_out != NULL) {
   8139 		struct route ro;
   8140 
   8141 		memset(&ro, 0, sizeof ro);
   8142 #ifdef SCTP_DEBUG
   8143 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
   8144 			printf("sctp_shutdown_complete2 calling ip_output:\n");
   8145 			sctp_print_address_pkt(iph_out, &comp_cp->sh);
   8146 		}
   8147 #endif
   8148 		/* set IPv4 length */
   8149 		iph_out->ip_len = htons(mout->m_pkthdr.len);
   8150 		/* out it goes */
   8151 		ip_output(mout, 0, &ro, IP_RAWOUTPUT, NULL, NULL);
   8152 	} else if (ip6_out != NULL) {
   8153 		struct route ro;
   8154 
   8155 		memset(&ro, 0, sizeof(ro));
   8156 #ifdef SCTP_DEBUG
   8157 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
   8158 			printf("sctp_shutdown_complete2 calling ip6_output:\n");
   8159 			sctp_print_address_pkt((struct ip *)ip6_out,
   8160 			    &comp_cp->sh);
   8161 		}
   8162 #endif
   8163 		ip6_output(mout, NULL, &ro, 0, NULL, NULL, NULL);
   8164 	}
   8165 	sctp_pegs[SCTP_DATAGRAMS_SENT]++;
   8166 	return (0);
   8167 }
   8168 
   8169 static struct sctp_nets *
   8170 sctp_select_hb_destination(struct sctp_tcb *stcb, struct timeval *now)
   8171 {
   8172 	struct sctp_nets *net, *hnet;
   8173 	int ms_goneby, highest_ms, state_overide=0;
   8174 
   8175 	SCTP_GETTIME_TIMEVAL(now);
   8176 	highest_ms = 0;
   8177 	hnet = NULL;
   8178 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
   8179 		if (
   8180 			((net->dest_state & SCTP_ADDR_NOHB) && ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) ||
   8181 			(net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)
   8182 			) {
   8183 			/* Skip this guy from consideration if HB is off AND its confirmed*/
   8184 #ifdef SCTP_DEBUG
   8185 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
   8186 				printf("Skipping net:%p state:%d nohb/out-of-scope\n",
   8187 				       net, net->dest_state);
   8188 			}
   8189 #endif
   8190 			continue;
   8191 		}
   8192 		if (sctp_destination_is_reachable(stcb, (struct sockaddr *)&net->ro.ro_sa) == 0) {
   8193 			/* skip this dest net from consideration */
   8194 #ifdef SCTP_DEBUG
   8195 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
   8196 				printf("Skipping net:%p reachable NOT\n",
   8197 				       net);
   8198 			}
   8199 #endif
   8200 			continue;
   8201 		}
   8202 		if (net->last_sent_time.tv_sec) {
   8203 			/* Sent to so we subtract */
   8204 			ms_goneby = (now->tv_sec - net->last_sent_time.tv_sec) * 1000;
   8205 		} else
   8206 			/* Never been sent to */
   8207 			ms_goneby = 0x7fffffff;
   8208 #ifdef SCTP_DEBUG
   8209 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
   8210 			printf("net:%p ms_goneby:%d\n",
   8211 			       net, ms_goneby);
   8212 		}
   8213 #endif
   8214 		/* When the address state is unconfirmed but still considered reachable, we
   8215 		 * HB at a higher rate. Once it goes confirmed OR reaches the "unreachable"
   8216 		 * state, thenw we cut it back to HB at a more normal pace.
   8217 		 */
   8218 		if ((net->dest_state & (SCTP_ADDR_UNCONFIRMED|SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED) {
   8219 			state_overide = 1;
   8220 		} else {
   8221 			state_overide = 0;
   8222 		}
   8223 
   8224 		if ((((unsigned int)ms_goneby >= net->RTO) || (state_overide)) &&
   8225 		    (ms_goneby > highest_ms)) {
   8226 			highest_ms = ms_goneby;
   8227 			hnet = net;
   8228 #ifdef SCTP_DEBUG
   8229 			if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
   8230 				printf("net:%p is the new high\n",
   8231 				       net);
   8232 			}
   8233 #endif
   8234 		}
   8235 	}
   8236 	if (hnet &&
   8237 	   ((hnet->dest_state & (SCTP_ADDR_UNCONFIRMED|SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED)) {
   8238 		state_overide = 1;
   8239 	} else {
   8240 		state_overide = 0;
   8241 	}
   8242 
   8243 	if (highest_ms && (((unsigned int)highest_ms >= hnet->RTO) || state_overide)) {
   8244 		/* Found the one with longest delay bounds
   8245 		 * OR it is unconfirmed and still not marked
   8246 		 * unreachable.
   8247 		 */
   8248 #ifdef SCTP_DEBUG
   8249 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
   8250 			printf("net:%p is the hb winner -",
   8251 				hnet);
   8252 			if (hnet)
   8253 				sctp_print_address((struct sockaddr *)&hnet->ro.ro_sa);
   8254 			else
   8255 				printf(" none\n");
   8256 		}
   8257 #endif
   8258 		/* update the timer now */
   8259 		hnet->last_sent_time = *now;
   8260 		return (hnet);
   8261 	}
   8262 	/* Nothing to HB */
   8263 	return (NULL);
   8264 }
   8265 
   8266 int
   8267 sctp_send_hb(struct sctp_tcb *stcb, int user_req, struct sctp_nets *u_net)
   8268 {
   8269 	struct sctp_tmit_chunk *chk;
   8270 	struct sctp_nets *net;
   8271 	struct sctp_heartbeat_chunk *hb;
   8272 	struct timeval now;
   8273 	struct sockaddr_in *sin;
   8274 	struct sockaddr_in6 *sin6;
   8275 
   8276 	if (user_req == 0) {
   8277 		net = sctp_select_hb_destination(stcb, &now);
   8278 		if (net == NULL) {
   8279 			/* All our busy none to send to, just
   8280 			 * start the timer again.
   8281 			 */
   8282 			if (stcb->asoc.state == 0) {
   8283 				return (0);
   8284 			}
   8285 			sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT,
   8286 					 stcb->sctp_ep,
   8287 					 stcb,
   8288 					 net);
   8289 			return (0);
   8290 		}
   8291 #ifndef SCTP_USE_ALLMAN_BURST
   8292 		else {
   8293 			/* found one idle.. decay cwnd on this one
   8294 			 * by 1/2 if none outstanding.
   8295 			 */
   8296 
   8297 			if (net->flight_size == 0) {
   8298 				net->cwnd /= 2;
   8299 				if (net->addr_is_local) {
   8300 					if (net->cwnd < (net->mtu *4)) {
   8301 						net->cwnd = net->mtu * 4;
   8302 					}
   8303 				} else {
   8304 					if (net->cwnd < (net->mtu * 2)) {
   8305 						net->cwnd = net->mtu * 2;
   8306 					}
   8307 				}
   8308 
   8309 			}
   8310 
   8311 		}
   8312 #endif
   8313 	} else {
   8314 		net = u_net;
   8315 		if (net == NULL) {
   8316 			return (0);
   8317 		}
   8318 		SCTP_GETTIME_TIMEVAL(&now);
   8319 	}
   8320 	sin = (struct sockaddr_in *)&net->ro.ro_sa;
   8321 	if (sin->sin_family != AF_INET) {
   8322 		if (sin->sin_family != AF_INET6) {
   8323 			/* huh */
   8324 			return (0);
   8325 		}
   8326 	}
   8327 	chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
   8328 	if (chk == NULL) {
   8329 #ifdef SCTP_DEBUG
   8330 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
   8331 			printf("Gak, can't get a chunk for hb\n");
   8332 		}
   8333 #endif
   8334 		return (0);
   8335 	}
   8336 	sctppcbinfo.ipi_gencnt_chunk++;
   8337 	sctppcbinfo.ipi_count_chunk++;
   8338 	chk->rec.chunk_id = SCTP_HEARTBEAT_REQUEST;
   8339 	chk->asoc = &stcb->asoc;
   8340 	chk->send_size = sizeof(struct sctp_heartbeat_chunk);
   8341 	MGETHDR(chk->data, M_DONTWAIT, MT_DATA);
   8342 	if (chk->data == NULL) {
   8343 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   8344 		sctppcbinfo.ipi_count_chunk--;
   8345 		if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   8346 			panic("Chunk count is negative");
   8347 		}
   8348 		sctppcbinfo.ipi_gencnt_chunk++;
   8349 		return (0);
   8350 	}
   8351 	chk->data->m_data += SCTP_MIN_OVERHEAD;
   8352 	chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
   8353 	chk->sent = SCTP_DATAGRAM_UNSENT;
   8354 	chk->snd_count = 0;
   8355 	chk->whoTo = net;
   8356 	chk->whoTo->ref_count++;
   8357 	/* Now we have a mbuf that we can fill in with the details */
   8358 	hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
   8359 
   8360 	/* fill out chunk header */
   8361 	hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
   8362 	hb->ch.chunk_flags = 0;
   8363 	hb->ch.chunk_length = htons(chk->send_size);
   8364 	/* Fill out hb parameter */
   8365 	hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
   8366 	hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
   8367 	hb->heartbeat.hb_info.time_value_1 = now.tv_sec;
   8368 	hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
   8369 	/* Did our user request this one, put it in */
   8370 	hb->heartbeat.hb_info.user_req = user_req;
   8371 	hb->heartbeat.hb_info.addr_family = sin->sin_family;
   8372 	hb->heartbeat.hb_info.addr_len = sin->sin_len;
   8373 	if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
   8374 		/* we only take from the entropy pool if the address is
   8375 		 * not confirmed.
   8376 		 */
   8377  		net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
   8378  		net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
   8379 	} else {
   8380 		net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
   8381 		net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
   8382 	}
   8383 	if (sin->sin_family == AF_INET) {
   8384 		memcpy(hb->heartbeat.hb_info.address, &sin->sin_addr, sizeof(sin->sin_addr));
   8385 	} else if (sin->sin_family == AF_INET6) {
   8386 		/* We leave the scope the way it is in our lookup table. */
   8387 		sin6 = (struct sockaddr_in6 *)&net->ro.ro_sa;
   8388 		memcpy(hb->heartbeat.hb_info.address, &sin6->sin6_addr, sizeof(sin6->sin6_addr));
   8389 	} else {
   8390 		/* huh compiler bug */
   8391 #ifdef SCTP_DEBUG
   8392 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   8393 			printf("Compiler bug bleeds a mbuf and a chunk\n");
   8394 		}
   8395 #endif
   8396 		return (0);
   8397 	}
   8398 	/* ok we have a destination that needs a beat */
   8399 	/* lets do the theshold management Qiaobing style */
   8400 	if (user_req == 0) {
   8401 		if (sctp_threshold_management(stcb->sctp_ep, stcb, net,
   8402 					      stcb->asoc.max_send_times)) {
   8403 			/* we have lost the association, in a way this
   8404 			 * is quite bad since we really are one less time
   8405 			 * since we really did not send yet. This is the
   8406 			 * down side to the Q's style as defined in the RFC
   8407 			 * and not my alternate style defined in the RFC.
   8408 			 */
   8409 			if (chk->data != NULL) {
   8410 				sctp_m_freem(chk->data);
   8411 				chk->data = NULL;
   8412 			}
   8413 			SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   8414 			sctppcbinfo.ipi_count_chunk--;
   8415 			if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   8416 				panic("Chunk count is negative");
   8417 			}
   8418 			sctppcbinfo.ipi_gencnt_chunk++;
   8419 			return (-1);
   8420 		}
   8421 	}
   8422 	net->hb_responded = 0;
   8423 #ifdef SCTP_DEBUG
   8424 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
   8425 		printf("Inserting chunk for HB\n");
   8426 	}
   8427 #endif
   8428 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
   8429 	stcb->asoc.ctrl_queue_cnt++;
   8430 	sctp_pegs[SCTP_HB_SENT]++;
   8431 	/*
   8432 	 * Call directly med level routine to put out the chunk. It will
   8433 	 * always tumble out control chunks aka HB but it may even tumble
   8434 	 * out data too.
   8435 	 */
   8436 	if (user_req == 0) {
   8437 		/* Ok now lets start the HB timer if it is NOT a user req */
   8438 		sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
   8439 				 stcb, net);
   8440 	}
   8441 	return (1);
   8442 }
   8443 
   8444 void
   8445 sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
   8446 		   uint32_t high_tsn)
   8447 {
   8448 	struct sctp_association *asoc;
   8449 	struct sctp_ecne_chunk *ecne;
   8450 	struct sctp_tmit_chunk *chk;
   8451 	asoc = &stcb->asoc;
   8452 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
   8453 		if (chk->rec.chunk_id == SCTP_ECN_ECHO) {
   8454 			/* found a previous ECN_ECHO update it if needed */
   8455 			ecne = mtod(chk->data, struct sctp_ecne_chunk *);
   8456 			ecne->tsn = htonl(high_tsn);
   8457 			return;
   8458 		}
   8459 	}
   8460 	/* nope could not find one to update so we must build one */
   8461 	chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
   8462 	if (chk == NULL) {
   8463 		return;
   8464 	}
   8465 	sctp_pegs[SCTP_ECNE_SENT]++;
   8466 	sctppcbinfo.ipi_count_chunk++;
   8467 	sctppcbinfo.ipi_gencnt_chunk++;
   8468 	chk->rec.chunk_id = SCTP_ECN_ECHO;
   8469 	chk->asoc = &stcb->asoc;
   8470 	chk->send_size = sizeof(struct sctp_ecne_chunk);
   8471 	MGETHDR(chk->data, M_DONTWAIT, MT_DATA);
   8472 	if (chk->data == NULL) {
   8473 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   8474 		sctppcbinfo.ipi_count_chunk--;
   8475 		if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   8476 			panic("Chunk count is negative");
   8477 		}
   8478 		sctppcbinfo.ipi_gencnt_chunk++;
   8479 		return;
   8480 	}
   8481 	chk->data->m_data += SCTP_MIN_OVERHEAD;
   8482 	chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
   8483 	chk->sent = SCTP_DATAGRAM_UNSENT;
   8484 	chk->snd_count = 0;
   8485 	chk->whoTo = net;
   8486 	chk->whoTo->ref_count++;
   8487 	ecne = mtod(chk->data, struct sctp_ecne_chunk *);
   8488 	ecne->ch.chunk_type = SCTP_ECN_ECHO;
   8489 	ecne->ch.chunk_flags = 0;
   8490 	ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
   8491 	ecne->tsn = htonl(high_tsn);
   8492 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
   8493 	asoc->ctrl_queue_cnt++;
   8494 }
   8495 
   8496 void
   8497 sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
   8498 			 struct mbuf *m, int iphlen, int bad_crc)
   8499 {
   8500 	struct sctp_association *asoc;
   8501 	struct sctp_pktdrop_chunk *drp;
   8502 	struct sctp_tmit_chunk *chk;
   8503 	uint8_t *datap;
   8504 	int len;
   8505 	unsigned int small_one;
   8506 	struct ip *iph;
   8507 
   8508 	long spc;
   8509 	asoc = &stcb->asoc;
   8510 	if (asoc->peer_supports_pktdrop == 0) {
   8511 		/* peer must declare support before I
   8512 		 * send one.
   8513 		 */
   8514 		return;
   8515 	}
   8516 	chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
   8517 	if (chk == NULL) {
   8518 		return;
   8519 	}
   8520 	sctppcbinfo.ipi_count_chunk++;
   8521 	sctppcbinfo.ipi_gencnt_chunk++;
   8522 
   8523 	iph = mtod(m, struct ip *);
   8524 	if (iph == NULL) {
   8525 		return;
   8526 	}
   8527 	if (iph->ip_v == IPVERSION) {
   8528 		/* IPv4 */
   8529 #if defined(__FreeBSD__)
   8530 		len = chk->send_size = iph->ip_len;
   8531 #else
   8532 		len = chk->send_size = (iph->ip_len - iphlen);
   8533 #endif
   8534 	} else {
   8535 		struct ip6_hdr *ip6h;
   8536 		/* IPv6 */
   8537 		ip6h = mtod(m, struct ip6_hdr *);
   8538 		len = chk->send_size = htons(ip6h->ip6_plen);
   8539 	}
   8540 	if ((len+iphlen) > m->m_pkthdr.len) {
   8541 		/* huh */
   8542 		chk->send_size = len = m->m_pkthdr.len - iphlen;
   8543 	}
   8544 	chk->asoc = &stcb->asoc;
   8545 	MGETHDR(chk->data, M_DONTWAIT, MT_DATA);
   8546 	if (chk->data == NULL) {
   8547 	jump_out:
   8548 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   8549 		sctppcbinfo.ipi_count_chunk--;
   8550 		if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   8551 			panic("Chunk count is negative");
   8552 		}
   8553 		sctppcbinfo.ipi_gencnt_chunk++;
   8554 		return;
   8555 	}
   8556 	if ((chk->send_size+sizeof(struct sctp_pktdrop_chunk)+SCTP_MIN_OVERHEAD) > MHLEN) {
   8557 		MCLGET(chk->data, M_DONTWAIT);
   8558 		if ((chk->data->m_flags & M_EXT) == 0) {
   8559 			/* Give up */
   8560 			sctp_m_freem(chk->data);
   8561 			chk->data = NULL;
   8562 			goto jump_out;
   8563 		}
   8564 	}
   8565 	chk->data->m_data += SCTP_MIN_OVERHEAD;
   8566 	drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
   8567 	if (drp == NULL) {
   8568 		sctp_m_freem(chk->data);
   8569 		chk->data = NULL;
   8570 		goto jump_out;
   8571 	}
   8572 	small_one = asoc->smallest_mtu;
   8573 	if (small_one > MCLBYTES) {
   8574 		/* Only one cluster worth of data MAX */
   8575 		small_one = MCLBYTES;
   8576 	}
   8577 	chk->book_size = (chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
   8578 			  sizeof(struct sctphdr) + SCTP_MED_OVERHEAD);
   8579 	if (chk->book_size > small_one) {
   8580 		drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
   8581 		drp->trunc_len = htons(chk->send_size);
   8582 		chk->send_size = small_one - (SCTP_MED_OVERHEAD +
   8583 					     sizeof(struct sctp_pktdrop_chunk) +
   8584 					     sizeof(struct sctphdr));
   8585 		len = chk->send_size;
   8586 	} else {
   8587 		/* no truncation needed */
   8588 		drp->ch.chunk_flags = 0;
   8589 		drp->trunc_len = htons(0);
   8590 	}
   8591 	if (bad_crc) {
   8592 		drp->ch.chunk_flags |= SCTP_BADCRC;
   8593 	}
   8594 	chk->send_size += sizeof(struct sctp_pktdrop_chunk);
   8595 	chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
   8596 	chk->sent = SCTP_DATAGRAM_UNSENT;
   8597 	chk->snd_count = 0;
   8598 	if (net) {
   8599 		/* we should hit here */
   8600 		chk->whoTo = net;
   8601 	} else {
   8602 		chk->whoTo = asoc->primary_destination;
   8603 	}
   8604 	chk->whoTo->ref_count++;
   8605 	chk->rec.chunk_id = SCTP_PACKET_DROPPED;
   8606 	drp->ch.chunk_type = SCTP_PACKET_DROPPED;
   8607 	drp->ch.chunk_length = htons(chk->send_size);
   8608 	spc = stcb->sctp_socket->so_rcv.sb_hiwat;
   8609 	if (spc < 0) {
   8610 		spc = 0;
   8611 	}
   8612 	drp->bottle_bw = htonl(spc);
   8613 	drp->current_onq = htonl(asoc->size_on_delivery_queue +
   8614 				 asoc->size_on_reasm_queue +
   8615 				 asoc->size_on_all_streams +
   8616 				 asoc->my_rwnd_control_len +
   8617 		                 stcb->sctp_socket->so_rcv.sb_cc);
   8618 	drp->reserved = 0;
   8619 	datap = drp->data;
   8620         m_copydata(m, iphlen, len, datap);
   8621 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
   8622 	asoc->ctrl_queue_cnt++;
   8623 }
   8624 
   8625 void
   8626 sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn)
   8627 {
   8628 	struct sctp_association *asoc;
   8629 	struct sctp_cwr_chunk *cwr;
   8630 	struct sctp_tmit_chunk *chk;
   8631 
   8632 	asoc = &stcb->asoc;
   8633 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
   8634 		if (chk->rec.chunk_id == SCTP_ECN_CWR) {
   8635 			/* found a previous ECN_CWR update it if needed */
   8636 			cwr = mtod(chk->data, struct sctp_cwr_chunk *);
   8637 			if (compare_with_wrap(high_tsn, ntohl(cwr->tsn),
   8638 					      MAX_TSN)) {
   8639 				cwr->tsn = htonl(high_tsn);
   8640 			}
   8641 			return;
   8642 		}
   8643 	}
   8644 	/* nope could not find one to update so we must build one */
   8645 	chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
   8646 	if (chk == NULL) {
   8647 		return;
   8648 	}
   8649 	sctppcbinfo.ipi_count_chunk++;
   8650 	sctppcbinfo.ipi_gencnt_chunk++;
   8651 	chk->rec.chunk_id = SCTP_ECN_CWR;
   8652 	chk->asoc = &stcb->asoc;
   8653 	chk->send_size = sizeof(struct sctp_cwr_chunk);
   8654 	MGETHDR(chk->data, M_DONTWAIT, MT_DATA);
   8655 	if (chk->data == NULL) {
   8656 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   8657 		sctppcbinfo.ipi_count_chunk--;
   8658 		if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   8659 			panic("Chunk count is negative");
   8660 		}
   8661 		sctppcbinfo.ipi_gencnt_chunk++;
   8662 		return;
   8663 	}
   8664 	chk->data->m_data += SCTP_MIN_OVERHEAD;
   8665 	chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
   8666 	chk->sent = SCTP_DATAGRAM_UNSENT;
   8667 	chk->snd_count = 0;
   8668 	chk->whoTo = net;
   8669 	chk->whoTo->ref_count++;
   8670 	cwr = mtod(chk->data, struct sctp_cwr_chunk *);
   8671 	cwr->ch.chunk_type = SCTP_ECN_CWR;
   8672 	cwr->ch.chunk_flags = 0;
   8673 	cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
   8674 	cwr->tsn = htonl(high_tsn);
   8675 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
   8676 	asoc->ctrl_queue_cnt++;
   8677 }
   8678 static void
   8679 sctp_reset_the_streams(struct sctp_tcb *stcb,
   8680      struct sctp_stream_reset_request *req, int number_entries, uint16_t *list)
   8681 {
   8682 	int i;
   8683 
   8684 	if (req->reset_flags & SCTP_RESET_ALL) {
   8685 		for (i=0; i<stcb->asoc.streamoutcnt; i++) {
   8686 			stcb->asoc.strmout[i].next_sequence_sent = 0;
   8687 		}
   8688 	} else if (number_entries) {
   8689 		for (i=0; i<number_entries; i++) {
   8690 			if (list[i] >= stcb->asoc.streamoutcnt) {
   8691 				/* no such stream */
   8692 				continue;
   8693 			}
   8694 			stcb->asoc.strmout[(list[i])].next_sequence_sent = 0;
   8695 		}
   8696 	}
   8697 	sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list);
   8698 }
   8699 
   8700 void
   8701 sctp_send_str_reset_ack(struct sctp_tcb *stcb,
   8702      struct sctp_stream_reset_request *req)
   8703 {
   8704 	struct sctp_association *asoc;
   8705 	struct sctp_stream_reset_resp *strack;
   8706 	struct sctp_tmit_chunk *chk;
   8707 	uint32_t seq;
   8708 	int number_entries, i;
   8709 	uint8_t two_way=0, not_peer=0;
   8710 	uint16_t *list=NULL;
   8711 
   8712 	asoc = &stcb->asoc;
   8713 	if (req->reset_flags & SCTP_RESET_ALL)
   8714 		number_entries = 0;
   8715 	else
   8716 		number_entries = (ntohs(req->ph.param_length) - sizeof(struct sctp_stream_reset_request)) / sizeof(uint16_t);
   8717 
   8718 	chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
   8719 	if (chk == NULL) {
   8720 		return;
   8721 	}
   8722 	sctppcbinfo.ipi_count_chunk++;
   8723 	sctppcbinfo.ipi_gencnt_chunk++;
   8724 	chk->rec.chunk_id = SCTP_STREAM_RESET;
   8725 	chk->asoc = &stcb->asoc;
   8726 	chk->send_size = sizeof(struct sctp_stream_reset_resp) + (number_entries * sizeof(uint16_t));
   8727 	MGETHDR(chk->data, M_DONTWAIT, MT_DATA);
   8728 	if (chk->data == NULL) {
   8729 	strresp_jump_out:
   8730 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   8731 		sctppcbinfo.ipi_count_chunk--;
   8732 		if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   8733 			panic("Chunk count is negative");
   8734 		}
   8735 		sctppcbinfo.ipi_gencnt_chunk++;
   8736 		return;
   8737 	}
   8738 	chk->data->m_data += SCTP_MIN_OVERHEAD;
   8739 	chk->data->m_pkthdr.len = chk->data->m_len = SCTP_SIZE32(chk->send_size);
   8740 	if (M_TRAILINGSPACE(chk->data) < (int)SCTP_SIZE32(chk->send_size)) {
   8741 		MCLGET(chk->data, M_DONTWAIT);
   8742 		if ((chk->data->m_flags & M_EXT) == 0) {
   8743 			/* Give up */
   8744 			sctp_m_freem(chk->data);
   8745 			chk->data = NULL;
   8746 			goto strresp_jump_out;
   8747 		}
   8748 		chk->data->m_data += SCTP_MIN_OVERHEAD;
   8749 	}
   8750 	if (M_TRAILINGSPACE(chk->data) < (int)SCTP_SIZE32(chk->send_size)) {
   8751 		/* can't do it, no room */
   8752 		/* Give up */
   8753 		sctp_m_freem(chk->data);
   8754 		chk->data = NULL;
   8755 		goto strresp_jump_out;
   8756 
   8757 	}
   8758 	chk->sent = SCTP_DATAGRAM_UNSENT;
   8759 	chk->snd_count = 0;
   8760 	chk->whoTo = asoc->primary_destination;
   8761 	chk->whoTo->ref_count++;
   8762 	strack = mtod(chk->data, struct sctp_stream_reset_resp *);
   8763 
   8764 	strack->ch.chunk_type = SCTP_STREAM_RESET;
   8765 	strack->ch.chunk_flags = 0;
   8766 	strack->ch.chunk_length = htons(chk->send_size);
   8767 
   8768 	memset(strack->sr_resp.reset_pad, 0, sizeof(strack->sr_resp.reset_pad));
   8769 
   8770 	strack->sr_resp.ph.param_type = ntohs(SCTP_STR_RESET_RESPONSE);
   8771 	strack->sr_resp.ph.param_length = htons((chk->send_size - sizeof(struct sctp_chunkhdr)));
   8772 
   8773 
   8774 
   8775 	if (chk->send_size % 4) {
   8776 		/* need a padding for the end */
   8777 		int pad;
   8778 		uint8_t *end;
   8779 		end = (uint8_t *)((vaddr_t)strack + chk->send_size);
   8780 		pad = chk->send_size % 4;
   8781 		for (i = 0; i < pad; i++) {
   8782 			end[i] = 0;
   8783 		}
   8784 		chk->send_size += pad;
   8785 	}
   8786 
   8787         /* actual response */
   8788 	if (req->reset_flags & SCTP_RESET_YOUR) {
   8789 		strack->sr_resp.reset_flags = SCTP_RESET_PERFORMED;
   8790 	} else {
   8791 		strack->sr_resp.reset_flags = 0;
   8792 	}
   8793 
   8794 	/* copied from reset request */
   8795 	strack->sr_resp.reset_req_seq_resp = req->reset_req_seq;
   8796 	seq = ntohl(req->reset_req_seq);
   8797 
   8798 	list = req->list_of_streams;
   8799 	/* copy the un-converted network byte order streams */
   8800 	for (i=0; i<number_entries; i++) {
   8801 		strack->sr_resp.list_of_streams[i] = list[i];
   8802 	}
   8803 	if (asoc->str_reset_seq_in == seq) {
   8804 		/* is it the next expected? */
   8805 		asoc->str_reset_seq_in++;
   8806 		strack->sr_resp.reset_at_tsn = htonl(asoc->sending_seq);
   8807 		asoc->str_reset_sending_seq = asoc->sending_seq;
   8808 		if (number_entries) {
   8809 			uint16_t temp;
   8810 			/* convert them to host byte order */
   8811 			for (i=0 ; i<number_entries; i++) {
   8812 				temp = ntohs(list[i]);
   8813 				list[i] = temp;
   8814 			}
   8815 		}
   8816 		if (req->reset_flags & SCTP_RESET_YOUR) {
   8817 			/* reset my outbound streams */
   8818 			sctp_reset_the_streams(stcb, req , number_entries, list);
   8819 		}
   8820 		if (req->reset_flags & SCTP_RECIPRICAL) {
   8821 			/* reset peer too */
   8822 			sctp_send_str_reset_req(stcb, number_entries, list, two_way, not_peer);
   8823 		}
   8824 
   8825 	} else {
   8826 		/* no its a retran so I must just ack and do nothing */
   8827 		strack->sr_resp.reset_at_tsn = htonl(asoc->str_reset_sending_seq);
   8828 	}
   8829 	strack->sr_resp.cumulative_tsn = htonl(asoc->cumulative_tsn);
   8830 	TAILQ_INSERT_TAIL(&asoc->control_send_queue,
   8831 			  chk,
   8832 			  sctp_next);
   8833 	asoc->ctrl_queue_cnt++;
   8834 }
   8835 
   8836 
   8837 void
   8838 sctp_send_str_reset_req(struct sctp_tcb *stcb,
   8839      int number_entrys, uint16_t *list, uint8_t two_way, uint8_t not_peer)
   8840 {
   8841 	/* Send a stream reset request. The number_entrys may be 0 and list NULL
   8842 	 * if the request is to reset all streams. If two_way is true then we
   8843 	 * not only request a RESET of the received streams but we also
   8844 	 * request the peer to send a reset req to us too.
   8845 	 * Flag combinations in table:
   8846 	 *
   8847 	 *       two_way | not_peer  | = | Flags
   8848 	 *       ------------------------------
   8849 	 *         0     |    0      | = | SCTP_RESET_YOUR (just the peer)
   8850 	 *         1     |    0      | = | SCTP_RESET_YOUR | SCTP_RECIPRICAL (both sides)
   8851 	 *         0     |    1      | = | Not a Valid Request (not anyone)
   8852 	 *         1     |    1      | = | SCTP_RESET_RECIPRICAL (Just local host)
   8853 	 */
   8854 	struct sctp_association *asoc;
   8855 	struct sctp_stream_reset_req *strreq;
   8856 	struct sctp_tmit_chunk *chk;
   8857 
   8858 
   8859 	asoc = &stcb->asoc;
   8860 	if (asoc->stream_reset_outstanding) {
   8861 		/* Already one pending, must get ACK back
   8862 		 * to clear the flag.
   8863 		 */
   8864 		return;
   8865 	}
   8866 
   8867 	if ((two_way == 0) && (not_peer == 1)) {
   8868 		/* not a valid request */
   8869 		return;
   8870 	}
   8871 
   8872 	chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
   8873 	if (chk == NULL) {
   8874 		return;
   8875 	}
   8876 	sctppcbinfo.ipi_count_chunk++;
   8877 	sctppcbinfo.ipi_gencnt_chunk++;
   8878 	chk->rec.chunk_id = SCTP_STREAM_RESET;
   8879 	chk->asoc = &stcb->asoc;
   8880 	chk->send_size = sizeof(struct sctp_stream_reset_req) + (number_entrys * sizeof(uint16_t));
   8881 	MGETHDR(chk->data, M_DONTWAIT, MT_DATA);
   8882 	if (chk->data == NULL) {
   8883 	strreq_jump_out:
   8884 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   8885 		sctppcbinfo.ipi_count_chunk--;
   8886 		if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   8887 			panic("Chunk count is negative");
   8888 		}
   8889 		sctppcbinfo.ipi_gencnt_chunk++;
   8890 		return;
   8891 	}
   8892 	chk->data->m_data += SCTP_MIN_OVERHEAD;
   8893 	chk->data->m_pkthdr.len = chk->data->m_len = SCTP_SIZE32(chk->send_size);
   8894 	if (M_TRAILINGSPACE(chk->data) < (int)SCTP_SIZE32(chk->send_size)) {
   8895 		MCLGET(chk->data, M_DONTWAIT);
   8896 		if ((chk->data->m_flags & M_EXT) == 0) {
   8897 			/* Give up */
   8898 			sctp_m_freem(chk->data);
   8899 			chk->data = NULL;
   8900 			goto strreq_jump_out;
   8901 		}
   8902 		chk->data->m_data += SCTP_MIN_OVERHEAD;
   8903 	}
   8904 	if (M_TRAILINGSPACE(chk->data) < (int)SCTP_SIZE32(chk->send_size)) {
   8905 		/* can't do it, no room */
   8906 		/* Give up */
   8907 		sctp_m_freem(chk->data);
   8908 		chk->data = NULL;
   8909 		goto strreq_jump_out;
   8910 	}
   8911 	chk->sent = SCTP_DATAGRAM_UNSENT;
   8912 	chk->snd_count = 0;
   8913 	chk->whoTo = asoc->primary_destination;
   8914 	chk->whoTo->ref_count++;
   8915 
   8916 	strreq = mtod(chk->data, struct sctp_stream_reset_req *);
   8917 	strreq->ch.chunk_type = SCTP_STREAM_RESET;
   8918 	strreq->ch.chunk_flags = 0;
   8919 	strreq->ch.chunk_length = htons(chk->send_size);
   8920 
   8921 	strreq->sr_req.ph.param_type = ntohs(SCTP_STR_RESET_REQUEST);
   8922 	strreq->sr_req.ph.param_length = htons((chk->send_size - sizeof(struct sctp_chunkhdr)));
   8923 
   8924 	if (chk->send_size % 4) {
   8925 		/* need a padding for the end */
   8926 		int pad, i;
   8927 		uint8_t *end;
   8928 		end = (uint8_t *)((vaddr_t)strreq + chk->send_size);
   8929 		pad = chk->send_size % 4;
   8930 		for (i=0; i<pad; i++) {
   8931 			end[i] = 0;
   8932 		}
   8933 		chk->send_size += pad;
   8934 	}
   8935 
   8936 	strreq->sr_req.reset_flags = 0;
   8937 	if (number_entrys == 0) {
   8938 		strreq->sr_req.reset_flags |= SCTP_RESET_ALL;
   8939 	}
   8940 	if (two_way == 0) {
   8941 		strreq->sr_req.reset_flags |= SCTP_RESET_YOUR;
   8942 	} else {
   8943 		if (not_peer == 0) {
   8944 			strreq->sr_req.reset_flags |= SCTP_RECIPRICAL | SCTP_RESET_YOUR;
   8945 		} else {
   8946 			strreq->sr_req.reset_flags |= SCTP_RECIPRICAL;
   8947 		}
   8948 	}
   8949 	memset(strreq->sr_req.reset_pad, 0, sizeof(strreq->sr_req.reset_pad));
   8950 	strreq->sr_req.reset_req_seq = htonl(asoc->str_reset_seq_out);
   8951 	if (number_entrys) {
   8952 		/* populate the specific entry's */
   8953 		int i;
   8954 		for (i=0; i < number_entrys; i++) {
   8955 			strreq->sr_req.list_of_streams[i] = htons(list[i]);
   8956 		}
   8957 	}
   8958 	TAILQ_INSERT_TAIL(&asoc->control_send_queue,
   8959 			  chk,
   8960 			  sctp_next);
   8961 	asoc->ctrl_queue_cnt++;
   8962 	sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
   8963 	asoc->stream_reset_outstanding = 1;
   8964 }
   8965 
   8966 void
   8967 sctp_send_abort(struct mbuf *m, int iphlen, struct sctphdr *sh, uint32_t vtag,
   8968     struct mbuf *err_cause)
   8969 {
   8970 	/*
   8971 	 * Formulate the abort message, and send it back down.
   8972 	 */
   8973 	struct mbuf *mout;
   8974 	struct sctp_abort_msg *abm;
   8975 	struct ip *iph, *iph_out;
   8976 	struct ip6_hdr *ip6, *ip6_out;
   8977 	int iphlen_out;
   8978 
   8979 	/* don't respond to ABORT with ABORT */
   8980 	if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
   8981 		if (err_cause)
   8982 			sctp_m_freem(err_cause);
   8983 		return;
   8984 	}
   8985 	MGETHDR(mout, M_DONTWAIT, MT_HEADER);
   8986 	if (mout == NULL) {
   8987 		if (err_cause)
   8988 			sctp_m_freem(err_cause);
   8989 		return;
   8990 	}
   8991 	iph = mtod(m, struct ip *);
   8992 	iph_out = NULL;
   8993 	ip6_out = NULL;
   8994 	if (iph->ip_v == IPVERSION) {
   8995 		iph_out = mtod(mout, struct ip *);
   8996 		mout->m_len = sizeof(*iph_out) + sizeof(*abm);
   8997 		mout->m_next = err_cause;
   8998 
   8999 		/* Fill in the IP header for the ABORT */
   9000 		iph_out->ip_v = IPVERSION;
   9001 		iph_out->ip_hl = (sizeof(struct ip) / 4);
   9002 		iph_out->ip_tos = (u_char)0;
   9003 		iph_out->ip_id = 0;
   9004 		iph_out->ip_off = 0;
   9005 		iph_out->ip_ttl = MAXTTL;
   9006 		iph_out->ip_p = IPPROTO_SCTP;
   9007 		iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
   9008 		iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
   9009 		/* let IP layer calculate this */
   9010 		iph_out->ip_sum = 0;
   9011 
   9012 		iphlen_out = sizeof(*iph_out);
   9013 		abm = (struct sctp_abort_msg *)((vaddr_t)iph_out + iphlen_out);
   9014 	} else if (iph->ip_v == (IPV6_VERSION >> 4)) {
   9015 		ip6 = (struct ip6_hdr *)iph;
   9016 		ip6_out = mtod(mout, struct ip6_hdr *);
   9017 		mout->m_len = sizeof(*ip6_out) + sizeof(*abm);
   9018 		mout->m_next = err_cause;
   9019 
   9020 		/* Fill in the IP6 header for the ABORT */
   9021 		ip6_out->ip6_flow = ip6->ip6_flow;
   9022 		ip6_out->ip6_hlim = ip6_defhlim;
   9023 		ip6_out->ip6_nxt = IPPROTO_SCTP;
   9024 		ip6_out->ip6_src = ip6->ip6_dst;
   9025 		ip6_out->ip6_dst = ip6->ip6_src;
   9026 
   9027 		iphlen_out = sizeof(*ip6_out);
   9028 		abm = (struct sctp_abort_msg *)((vaddr_t)ip6_out + iphlen_out);
   9029 	} else {
   9030 		/* Currently not supported */
   9031 		return;
   9032 	}
   9033 
   9034 	abm->sh.src_port = sh->dest_port;
   9035 	abm->sh.dest_port = sh->src_port;
   9036 	abm->sh.checksum = 0;
   9037 	if (vtag == 0) {
   9038 		abm->sh.v_tag = sh->v_tag;
   9039 		abm->msg.ch.chunk_flags = SCTP_HAD_NO_TCB;
   9040 	} else {
   9041 		abm->sh.v_tag = htonl(vtag);
   9042 		abm->msg.ch.chunk_flags = 0;
   9043 	}
   9044 	abm->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
   9045 
   9046 	if (err_cause) {
   9047 		struct mbuf *m_tmp = err_cause;
   9048 		int err_len = 0;
   9049 		/* get length of the err_cause chain */
   9050 		while (m_tmp != NULL) {
   9051 			err_len += m_tmp->m_len;
   9052 			m_tmp = m_tmp->m_next;
   9053 		}
   9054 		mout->m_pkthdr.len = mout->m_len + err_len;
   9055 		if (err_len % 4) {
   9056 			/* need pad at end of chunk */
   9057 			u_int32_t cpthis=0;
   9058 			int padlen;
   9059 			padlen = 4 - (mout->m_pkthdr.len % 4);
   9060 			m_copyback(mout, mout->m_pkthdr.len, padlen, (void *)&cpthis);
   9061 		}
   9062 		abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch) + err_len);
   9063 	} else {
   9064 		mout->m_pkthdr.len = mout->m_len;
   9065 		abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch));
   9066 	}
   9067 
   9068 	/* add checksum */
   9069 	if ((sctp_no_csum_on_loopback) && m_get_rcvif_NOMPSAFE(m) != NULL &&
   9070 	    m_get_rcvif_NOMPSAFE(m)->if_type == IFT_LOOP) {
   9071 		abm->sh.checksum =  0;
   9072 	} else {
   9073 		abm->sh.checksum = sctp_calculate_sum(mout, NULL, iphlen_out);
   9074 	}
   9075 
   9076 	/* zap the rcvif, it should be null */
   9077 	m_reset_rcvif(mout);
   9078 	if (iph_out != NULL) {
   9079 		struct route ro;
   9080 
   9081 		/* zap the stack pointer to the route */
   9082 		memset(&ro, 0, sizeof ro);
   9083 #ifdef SCTP_DEBUG
   9084 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
   9085                         printf("sctp_send_abort calling ip_output:\n");
   9086 			sctp_print_address_pkt(iph_out, &abm->sh);
   9087                 }
   9088 #endif
   9089 		/* set IPv4 length */
   9090 		iph_out->ip_len = htons(mout->m_pkthdr.len);
   9091 		/* out it goes */
   9092 		(void)ip_output(mout, 0, &ro, IP_RAWOUTPUT, NULL, NULL);
   9093 	} else if (ip6_out != NULL) {
   9094 		struct route ro;
   9095 
   9096 		/* zap the stack pointer to the route */
   9097 		memset(&ro, 0, sizeof(ro));
   9098 #ifdef SCTP_DEBUG
   9099 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
   9100                         printf("sctp_send_abort calling ip6_output:\n");
   9101 			sctp_print_address_pkt((struct ip *)ip6_out, &abm->sh);
   9102                 }
   9103 #endif
   9104 		ip6_output(mout, NULL, &ro, 0, NULL, NULL, NULL);
   9105 	}
   9106         sctp_pegs[SCTP_DATAGRAMS_SENT]++;
   9107 }
   9108 
   9109 void
   9110 sctp_send_operr_to(struct mbuf *m, int iphlen,
   9111 		   struct mbuf *scm,
   9112 		   uint32_t vtag)
   9113 {
   9114 	struct sctphdr *ihdr;
   9115 	struct sctphdr *ohdr;
   9116 	struct sctp_chunkhdr *ophdr;
   9117 
   9118 	struct ip *iph;
   9119 #ifdef SCTP_DEBUG
   9120 	struct sockaddr_in6 lsa6, fsa6;
   9121 #endif
   9122 	uint32_t val;
   9123 	iph = mtod(m, struct ip *);
   9124 	ihdr = (struct sctphdr *)((vaddr_t)iph + iphlen);
   9125 	if (!(scm->m_flags & M_PKTHDR)) {
   9126 		/* must be a pkthdr */
   9127 		printf("Huh, not a packet header in send_operr\n");
   9128 		m_freem(scm);
   9129 		return;
   9130 	}
   9131 	M_PREPEND(scm, (sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr)), M_DONTWAIT);
   9132 	if (scm == NULL) {
   9133 		/* can't send because we can't add a mbuf */
   9134 		return;
   9135 	}
   9136 	ohdr = mtod(scm, struct sctphdr *);
   9137 	ohdr->src_port = ihdr->dest_port;
   9138 	ohdr->dest_port = ihdr->src_port;
   9139 	ohdr->v_tag = vtag;
   9140 	ohdr->checksum = 0;
   9141 	ophdr = (struct sctp_chunkhdr *)(ohdr + 1);
   9142 	ophdr->chunk_type = SCTP_OPERATION_ERROR;
   9143 	ophdr->chunk_flags = 0;
   9144 	ophdr->chunk_length = htons(scm->m_pkthdr.len - sizeof(struct sctphdr));
   9145 	if (scm->m_pkthdr.len % 4) {
   9146 		/* need padding */
   9147 		u_int32_t cpthis=0;
   9148 		int padlen;
   9149 		padlen = 4 - (scm->m_pkthdr.len % 4);
   9150 		m_copyback(scm, scm->m_pkthdr.len, padlen, (void *)&cpthis);
   9151 	}
   9152 	if ((sctp_no_csum_on_loopback) && m_get_rcvif_NOMPSAFE(m) != NULL &&
   9153 	    m_get_rcvif_NOMPSAFE(m)->if_type == IFT_LOOP) {
   9154 		val = 0;
   9155 	} else {
   9156 		val = sctp_calculate_sum(scm, NULL, 0);
   9157 	}
   9158 	ohdr->checksum = val;
   9159 	if (iph->ip_v == IPVERSION) {
   9160 		/* V4 */
   9161 		struct ip *out;
   9162 		struct route ro;
   9163 		M_PREPEND(scm, sizeof(struct ip), M_DONTWAIT);
   9164 		if (scm == NULL)
   9165 			return;
   9166 		memset(&ro, 0, sizeof ro);
   9167 		out = mtod(scm, struct ip *);
   9168 		out->ip_v = iph->ip_v;
   9169 		out->ip_hl = (sizeof(struct ip)/4);
   9170 		out->ip_tos = iph->ip_tos;
   9171 		out->ip_id = iph->ip_id;
   9172 		out->ip_off = 0;
   9173 		out->ip_ttl = MAXTTL;
   9174 		out->ip_p = IPPROTO_SCTP;
   9175 		out->ip_sum = 0;
   9176 		out->ip_src = iph->ip_dst;
   9177 		out->ip_dst = iph->ip_src;
   9178 		out->ip_len = htons(scm->m_pkthdr.len);
   9179 		ip_output(scm, 0, &ro, IP_RAWOUTPUT, NULL, NULL);
   9180 		sctp_pegs[SCTP_DATAGRAMS_SENT]++;
   9181 	} else {
   9182 		/* V6 */
   9183 		struct route ro;
   9184 		struct ip6_hdr *out6, *in6;
   9185 
   9186 		M_PREPEND(scm, sizeof(struct ip6_hdr), M_DONTWAIT);
   9187 		if (scm == NULL)
   9188 			return;
   9189 		memset(&ro, 0, sizeof ro);
   9190 		in6 = mtod(m, struct ip6_hdr *);
   9191 		out6 = mtod(scm, struct ip6_hdr *);
   9192 		out6->ip6_flow = in6->ip6_flow;
   9193 		out6->ip6_hlim = ip6_defhlim;
   9194 		out6->ip6_nxt = IPPROTO_SCTP;
   9195 		out6->ip6_src = in6->ip6_dst;
   9196 		out6->ip6_dst = in6->ip6_src;
   9197 
   9198 #ifdef SCTP_DEBUG
   9199 		memset(&lsa6, 0, sizeof(lsa6));
   9200 		lsa6.sin6_len = sizeof(lsa6);
   9201 		lsa6.sin6_family = AF_INET6;
   9202 		lsa6.sin6_addr = out6->ip6_src;
   9203 		memset(&fsa6, 0, sizeof(fsa6));
   9204 		fsa6.sin6_len = sizeof(fsa6);
   9205 		fsa6.sin6_family = AF_INET6;
   9206 		fsa6.sin6_addr = out6->ip6_dst;
   9207 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
   9208 			printf("sctp_operr_to calling ipv6 output:\n");
   9209 			printf("src: ");
   9210 			sctp_print_address((struct sockaddr *)&lsa6);
   9211 			printf("dst ");
   9212 			sctp_print_address((struct sockaddr *)&fsa6);
   9213 		}
   9214 #endif /* SCTP_DEBUG */
   9215 		ip6_output(scm, NULL, &ro, 0, NULL, NULL, NULL);
   9216 		sctp_pegs[SCTP_DATAGRAMS_SENT]++;
   9217 	}
   9218 }
   9219 
   9220 static int
   9221 sctp_copy_one(struct mbuf *m, struct uio *uio, int cpsz, int resv_upfront, int *mbcnt)
   9222 {
   9223 	int left, cancpy, willcpy, error;
   9224 	left = cpsz;
   9225 
   9226 	if (m == NULL) {
   9227 		/* TSNH */
   9228 		*mbcnt = 0;
   9229 		return (ENOMEM);
   9230 	}
   9231 	m->m_len = 0;
   9232 	if ((left+resv_upfront) > (int)MHLEN) {
   9233 		MCLGET(m, M_WAIT);
   9234 		if (m == NULL) {
   9235 			*mbcnt = 0;
   9236 			return (ENOMEM);
   9237 		}
   9238 		if ((m->m_flags & M_EXT) == 0) {
   9239 			*mbcnt = 0;
   9240 			return (ENOMEM);
   9241 		}
   9242 		*mbcnt += m->m_ext.ext_size;
   9243 	}
   9244 	*mbcnt += MSIZE;
   9245 	cancpy = M_TRAILINGSPACE(m);
   9246 	willcpy = min(cancpy, left);
   9247 	if ((willcpy + resv_upfront) > cancpy) {
   9248 		willcpy -= resv_upfront;
   9249 	}
   9250 	while (left > 0) {
   9251 		/* Align data to the end */
   9252 		if ((m->m_flags & M_EXT) == 0) {
   9253 			if (m->m_flags & M_PKTHDR) {
   9254 				MH_ALIGN(m, willcpy);
   9255 			} else {
   9256 				M_ALIGN(m, willcpy);
   9257 			}
   9258 		} else {
   9259 			MC_ALIGN(m, willcpy);
   9260 		}
   9261 		error = uiomove(mtod(m, void *), willcpy, uio);
   9262 		if (error) {
   9263 			return (error);
   9264 		}
   9265 		m->m_len = willcpy;
   9266 		m->m_nextpkt = 0;
   9267 		left -= willcpy;
   9268 		if (left > 0) {
   9269 			MGET(m->m_next, M_WAIT, MT_DATA);
   9270 			if (m->m_next == NULL) {
   9271 				*mbcnt = 0;
   9272 				return (ENOMEM);
   9273 			}
   9274 			m = m->m_next;
   9275 			m->m_len = 0;
   9276 			*mbcnt += MSIZE;
   9277 			if (left > (int)MHLEN) {
   9278 				MCLGET(m, M_WAIT);
   9279 				if (m == NULL) {
   9280 					*mbcnt = 0;
   9281 					return (ENOMEM);
   9282 				}
   9283 				if ((m->m_flags & M_EXT) == 0) {
   9284 					*mbcnt = 0;
   9285 					return (ENOMEM);
   9286 				}
   9287 				*mbcnt += m->m_ext.ext_size;
   9288 			}
   9289 			cancpy = M_TRAILINGSPACE(m);
   9290 			willcpy = min(cancpy, left);
   9291 		}
   9292 	}
   9293 	return (0);
   9294 }
   9295 
   9296 static int
   9297 sctp_copy_it_in(struct sctp_inpcb *inp,
   9298 		struct sctp_tcb *stcb,
   9299 		struct sctp_association *asoc,
   9300 		struct sctp_nets *net,
   9301 		struct sctp_sndrcvinfo *srcv,
   9302 		struct uio *uio,
   9303 		int flags)
   9304 {
   9305 	/* This routine must be very careful in
   9306 	 * its work. Protocol processing is
   9307 	 * up and running so care must be taken to
   9308 	 * spl...() when you need to do something
   9309 	 * that may effect the stcb/asoc. The sb is
   9310 	 * locked however. When data is copied the
   9311 	 * protocol processing should be enabled since
   9312 	 * this is a slower operation...
   9313 	 */
   9314 	struct socket *so;
   9315 	int error = 0;
   9316 	int frag_size, mbcnt = 0, mbcnt_e = 0;
   9317 	unsigned int sndlen;
   9318 	unsigned int tot_demand;
   9319 	int tot_out, dataout;
   9320 	struct sctp_tmit_chunk *chk;
   9321 	struct mbuf *mm;
   9322 	struct sctp_stream_out *strq;
   9323 	uint32_t my_vtag;
   9324 	int resv_in_first;
   9325 
   9326 	so = stcb->sctp_socket;
   9327 	solock(so);
   9328 	chk = NULL;
   9329 	mm = NULL;
   9330 
   9331 	sndlen = uio->uio_resid;
   9332 	/* lock the socket buf */
   9333 	error = sblock(&so->so_snd, SBLOCKWAIT(flags));
   9334 	if (error)
   9335 		goto out_locked;
   9336 
   9337 #ifdef SCTP_DEBUG
   9338 	printf("sctp_copy_it_in: %d\n", sndlen);
   9339 #endif
   9340 	/* will it ever fit ? */
   9341 	if (sndlen > so->so_snd.sb_hiwat) {
   9342 		/* It will NEVER fit */
   9343 		error = EMSGSIZE;
   9344 		goto release;
   9345 	}
   9346 	/* Do I need to block? */
   9347 	if ((so->so_snd.sb_hiwat <
   9348 	    (sndlen + asoc->total_output_queue_size)) ||
   9349 	    (asoc->chunks_on_out_queue > sctp_max_chunks_on_queue) ||
   9350 	    (asoc->total_output_mbuf_queue_size >
   9351 	    so->so_snd.sb_mbmax)
   9352 	) {
   9353 		/* prune any prsctp bufs out */
   9354 		if (asoc->peer_supports_prsctp) {
   9355 			sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
   9356 		}
   9357 		/*
   9358 		 * We store off a pointer to the endpoint.
   9359 		 * Since on return from this we must check to
   9360 		 * see if an so_error is set. If so we may have
   9361 		 * been reset and our stcb destroyed. Returning
   9362 		 * an error will flow back to the user...
   9363 		 */
   9364 		while ((so->so_snd.sb_hiwat <
   9365 		    (sndlen + asoc->total_output_queue_size)) ||
   9366 		    (asoc->chunks_on_out_queue >
   9367 		    sctp_max_chunks_on_queue) ||
   9368 		    (asoc->total_output_mbuf_queue_size >
   9369 		    so->so_snd.sb_mbmax)
   9370 		) {
   9371 			if ((so->so_state & SS_NBIO)
   9372 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
   9373 			    || (flags & MSG_NBIO)
   9374 #endif
   9375 				) {
   9376 				/* Non-blocking io in place */
   9377 				error = EWOULDBLOCK;
   9378 				goto release;
   9379 			}
   9380 			inp->sctp_tcb_at_block = (void *)stcb;
   9381 			inp->error_on_block = 0;
   9382 #ifdef SCTP_BLK_LOGGING
   9383 			sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
   9384 			    so, asoc);
   9385 #endif
   9386 			sbunlock(&so->so_snd);
   9387 			SCTP_TCB_UNLOCK(stcb);
   9388 			error = sbwait(&so->so_snd);
   9389 			SCTP_INP_RLOCK(inp);
   9390 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
   9391 			    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
   9392 				/* Should I really unlock ? */
   9393 				SCTP_INP_RUNLOCK(inp);
   9394 				error = EFAULT;
   9395 				goto out_locked;
   9396 			}
   9397 			SCTP_TCB_LOCK(stcb);
   9398 			SCTP_INP_RUNLOCK(inp);
   9399 
   9400 			inp->sctp_tcb_at_block = 0;
   9401 #ifdef SCTP_BLK_LOGGING
   9402 			sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
   9403 			    so, asoc);
   9404 #endif
   9405 			if (inp->error_on_block) {
   9406 				/*
   9407 				 * if our asoc was killed, the free code
   9408 				 * (in sctp_pcb.c) will save a error in
   9409 				 * here for us
   9410 				 */
   9411  				error = inp->error_on_block;
   9412 				goto out_locked;
   9413 			}
   9414 			if (error) {
   9415 				goto out_locked;
   9416 			}
   9417 			/* did we encounter a socket error? */
   9418 			if (so->so_error) {
   9419 				error = so->so_error;
   9420 				goto out_locked;
   9421 			}
   9422 			error = sblock(&so->so_snd, M_WAITOK);
   9423 			if (error) {
   9424 				/* Can't aquire the lock */
   9425 				goto out_locked;
   9426 			}
   9427 #if defined(__FreeBSD__) && __FreeBSD_version >= 502115
   9428 			if (so->so_rcv.sb_state & SBS_CANTSENDMORE) {
   9429 #else
   9430 			if (so->so_state & SS_CANTSENDMORE) {
   9431 #endif
   9432 				/* The socket is now set not to sendmore.. its gone */
   9433 				error = EPIPE;
   9434 				goto release;
   9435 			}
   9436 			if (so->so_error) {
   9437 				error = so->so_error;
   9438 				goto release;
   9439 			}
   9440 			if (asoc->peer_supports_prsctp) {
   9441 				sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
   9442 			}
   9443 		}
   9444 	}
   9445 	dataout = tot_out = uio->uio_resid;
   9446  	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
   9447 		resv_in_first = SCTP_MED_OVERHEAD;
   9448 	} else {
   9449 		resv_in_first = SCTP_MED_V4_OVERHEAD;
   9450 	}
   9451 
   9452 	/* Are we aborting? */
   9453 	if (srcv->sinfo_flags & SCTP_ABORT) {
   9454 		if ((SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
   9455 		    (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_ECHOED)) {
   9456 			/* It has to be up before we abort */
   9457 			/* how big is the user initiated abort? */
   9458 
   9459 			/* I wonder about doing a MGET without a splnet set.
   9460 			 * it is done that way in the sosend code so I guess
   9461 			 * it is ok :-0
   9462 			 */
   9463  			MGETHDR(mm, M_WAIT, MT_DATA);
   9464 			if (mm) {
   9465 				struct sctp_paramhdr *ph;
   9466 
   9467 				tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
   9468 				if (tot_demand > MHLEN) {
   9469 					if (tot_demand > MCLBYTES) {
   9470 						/* truncate user data */
   9471 						tot_demand = MCLBYTES;
   9472 						tot_out = tot_demand - sizeof(struct sctp_paramhdr);
   9473 					}
   9474 					MCLGET(mm, M_WAIT);
   9475 					if ((mm->m_flags & M_EXT) == 0) {
   9476 						/* truncate further */
   9477 						tot_demand = MHLEN;
   9478 						tot_out = tot_demand - sizeof(struct sctp_paramhdr);
   9479 					}
   9480 				}
   9481 				/* now move forward the data pointer */
   9482 				ph = mtod(mm, struct sctp_paramhdr *);
   9483 				ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
   9484 				ph->param_length = htons((sizeof(struct sctp_paramhdr) + tot_out));
   9485 				ph++;
   9486 				mm->m_pkthdr.len = tot_out + sizeof(struct sctp_paramhdr);
   9487 				mm->m_len = mm->m_pkthdr.len;
   9488 				error = uiomove((void *)ph, (int)tot_out, uio);
   9489 				if (error) {
   9490 					/*
   9491 					 * Here if we can't get his data we
   9492 					 * still abort we just don't get to
   9493 					 * send the users note :-0
   9494 					 */
   9495 					sctp_m_freem(mm);
   9496 					mm = NULL;
   9497 				}
   9498 			}
   9499 			sbunlock(&so->so_snd);
   9500 			sctp_abort_an_association(stcb->sctp_ep, stcb,
   9501 						  SCTP_RESPONSE_TO_USER_REQ,
   9502 						  mm);
   9503 			mm = NULL;
   9504 			goto out_locked;
   9505 		}
   9506 		goto release;
   9507 	}
   9508 
   9509 	/* Now can we send this? */
   9510 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
   9511 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
   9512 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
   9513 	    (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
   9514 		/* got data while shutting down */
   9515 		error = ECONNRESET;
   9516 		goto release;
   9517  	}
   9518  	/* Is the stream no. valid? */
   9519 	if (srcv->sinfo_stream >= asoc->streamoutcnt) {
   9520  		/* Invalid stream number */
   9521 		error = EINVAL;
   9522 		goto release;
   9523  	}
   9524 	if (asoc->strmout == NULL) {
   9525 		/* huh? software error */
   9526 #ifdef SCTP_DEBUG
   9527  		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   9528  			printf("software error in sctp_copy_it_in\n");
   9529  		}
   9530 #endif
   9531 		error = EFAULT;
   9532 		goto release;
   9533 	}
   9534 	if ((srcv->sinfo_flags & SCTP_EOF) &&
   9535 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
   9536 	    (tot_out == 0)) {
   9537 		sounlock(so);
   9538 		goto zap_by_it_now;
   9539 	}
   9540  	if (tot_out == 0) {
   9541  		/* not allowed */
   9542  		error = EMSGSIZE;
   9543 		goto release;
   9544  	}
   9545 	/* save off the tag */
   9546 	my_vtag = asoc->my_vtag;
   9547 	strq = &asoc->strmout[srcv->sinfo_stream];
   9548 	/* First lets figure out the "chunking" point */
   9549 	frag_size = sctp_get_frag_point(stcb, asoc);
   9550 
   9551 	/* two choices here, it all fits in one chunk or
   9552 	 * we need multiple chunks.
   9553 	 */
   9554 	sounlock(so);
   9555 	if (tot_out <= frag_size) {
   9556 		/* no need to setup a template */
   9557 		chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
   9558 		if (chk == NULL) {
   9559 			error = ENOMEM;
   9560 			goto release;
   9561 		}
   9562 		sctppcbinfo.ipi_count_chunk++;
   9563 		sctppcbinfo.ipi_gencnt_chunk++;
   9564 		asoc->chunks_on_out_queue++;
   9565 		MGETHDR(mm, M_WAIT, MT_DATA);
   9566 		if (mm == NULL) {
   9567 			error = ENOMEM;
   9568 			goto clean_up;
   9569 		}
   9570 		error = sctp_copy_one(mm, uio, tot_out, resv_in_first, &mbcnt_e);
   9571 		if (error)
   9572 			goto clean_up;
   9573 		sctp_prepare_chunk(chk, stcb, srcv, strq, net);
   9574 		chk->mbcnt = mbcnt_e;
   9575 		mbcnt += mbcnt_e;
   9576 		mbcnt_e = 0;
   9577 		mm->m_pkthdr.len = tot_out;
   9578 		chk->data = mm;
   9579 		mm = NULL;
   9580 
   9581 		/* the actual chunk flags */
   9582 		chk->rec.data.rcv_flags |= SCTP_DATA_NOT_FRAG;
   9583 		chk->whoTo->ref_count++;
   9584 
   9585 		/* fix up the send_size if it is not present */
   9586 		chk->send_size = tot_out;
   9587 		chk->book_size = chk->send_size;
   9588 		/* ok, we are commited */
   9589 		if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
   9590 			/* bump the ssn if we are unordered. */
   9591 			strq->next_sequence_sent++;
   9592 		}
   9593 		if (chk->flags & SCTP_PR_SCTP_BUFFER) {
   9594 			asoc->sent_queue_cnt_removeable++;
   9595 		}
   9596 		solock(so);
   9597 		if ((asoc->state == 0) ||
   9598 		    (my_vtag != asoc->my_vtag) ||
   9599 		    (so != inp->sctp_socket) ||
   9600 		    (inp->sctp_socket == 0)) {
   9601 			/* connection was aborted */
   9602 			sounlock(so);
   9603 			error = ECONNRESET;
   9604 			goto clean_up;
   9605 		}
   9606 		asoc->stream_queue_cnt++;
   9607 		TAILQ_INSERT_TAIL(&strq->outqueue, chk, sctp_next);
   9608 		/* now check if this stream is on the wheel */
   9609 		if ((strq->next_spoke.tqe_next == NULL) &&
   9610 		    (strq->next_spoke.tqe_prev == NULL)) {
   9611 			/* Insert it on the wheel since it is not
   9612 			 * on it currently
   9613 			 */
   9614 			sctp_insert_on_wheel(asoc, strq);
   9615 		}
   9616 		sounlock(so);
   9617 clean_up:
   9618 		if (error) {
   9619 			SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   9620 			sctppcbinfo.ipi_count_chunk--;
   9621 			if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   9622 				panic("Chunk count is negative");
   9623 			}
   9624 			goto release;
   9625 		}
   9626 	} else {
   9627 		/* we need to setup a template */
   9628 		struct sctp_tmit_chunk template;
   9629 		struct sctpchunk_listhead tmp;
   9630 
   9631 		/* setup the template */
   9632 		sctp_prepare_chunk(&template, stcb, srcv, strq, net);
   9633 
   9634 		/* Prepare the temp list */
   9635 		TAILQ_INIT(&tmp);
   9636 
   9637 		/* Template is complete, now time for the work */
   9638 		while (tot_out > 0) {
   9639 			/* Get a chunk */
   9640  			chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
   9641 			if (chk == NULL) {
   9642 				/*
   9643 				 * ok we must spin through and dump anything
   9644 				 * we have allocated and then jump to the
   9645 				 * no_membad
   9646 				 */
   9647 				error = ENOMEM;
   9648 			}
   9649 			sctppcbinfo.ipi_count_chunk++;
   9650 			asoc->chunks_on_out_queue++;
   9651 
   9652 			sctppcbinfo.ipi_gencnt_chunk++;
   9653 			*chk = template;
   9654 			chk->whoTo->ref_count++;
   9655 			MGETHDR(chk->data, M_WAIT, MT_DATA);
   9656 			if (chk->data == NULL) {
   9657 				error = ENOMEM;
   9658 				goto temp_clean_up;
   9659 			}
   9660 			tot_demand = min(tot_out, frag_size);
   9661 			error = sctp_copy_one(chk->data, uio, tot_demand , resv_in_first, &mbcnt_e);
   9662 			if (error)
   9663 				goto temp_clean_up;
   9664 			/* now fix the chk->send_size */
   9665 			chk->mbcnt = mbcnt_e;
   9666 			mbcnt += mbcnt_e;
   9667 			mbcnt_e = 0;
   9668 			chk->send_size = tot_demand;
   9669 			chk->data->m_pkthdr.len = tot_demand;
   9670 			chk->book_size = chk->send_size;
   9671 			if (chk->flags & SCTP_PR_SCTP_BUFFER) {
   9672 				asoc->sent_queue_cnt_removeable++;
   9673 			}
   9674 			TAILQ_INSERT_TAIL(&tmp, chk, sctp_next);
   9675 			tot_out -= tot_demand;
   9676 		}
   9677 		/* Now the tmp list holds all chunks and data */
   9678 		if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
   9679 			/* bump the ssn if we are unordered. */
   9680 			strq->next_sequence_sent++;
   9681 		}
   9682 		/* Mark the first/last flags. This will
   9683 		 * result int a 3 for a single item on the list
   9684 		 */
   9685 		chk = TAILQ_FIRST(&tmp);
   9686 		chk->rec.data.rcv_flags |= SCTP_DATA_FIRST_FRAG;
   9687 		chk = TAILQ_LAST(&tmp, sctpchunk_listhead);
   9688 		chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
   9689 
   9690 		/* now move it to the streams actual queue */
   9691 		/* first stop protocol processing */
   9692 		mutex_enter(softnet_lock);
   9693 		if ((asoc->state == 0) ||
   9694 		    (my_vtag != asoc->my_vtag) ||
   9695 		    (so != inp->sctp_socket) ||
   9696 		    (inp->sctp_socket == 0)) {
   9697 			/* connection was aborted */
   9698 			mutex_exit(softnet_lock);
   9699 			error = ECONNRESET;
   9700 			goto temp_clean_up;
   9701 		}
   9702 		chk = TAILQ_FIRST(&tmp);
   9703 		while (chk) {
   9704 			chk->data->m_nextpkt = 0;
   9705 			TAILQ_REMOVE(&tmp, chk, sctp_next);
   9706 			asoc->stream_queue_cnt++;
   9707 			TAILQ_INSERT_TAIL(&strq->outqueue, chk, sctp_next);
   9708 			chk = TAILQ_FIRST(&tmp);
   9709 		}
   9710 		/* now check if this stream is on the wheel */
   9711 		if ((strq->next_spoke.tqe_next == NULL) &&
   9712 		    (strq->next_spoke.tqe_prev == NULL)) {
   9713 			/* Insert it on the wheel since it is not
   9714 			 * on it currently
   9715 			 */
   9716 			sctp_insert_on_wheel(asoc, strq);
   9717 		}
   9718 		/* Ok now we can allow pping */
   9719 		mutex_exit(softnet_lock);
   9720 temp_clean_up:
   9721 		if (error) {
   9722 			chk = TAILQ_FIRST(&tmp);
   9723 			while (chk) {
   9724 				if (chk->data) {
   9725 					sctp_m_freem(chk->data);
   9726 					chk->data = NULL;
   9727 				}
   9728 				TAILQ_REMOVE(&tmp, chk, sctp_next);
   9729 				SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
   9730 				sctppcbinfo.ipi_count_chunk--;
   9731 				asoc->chunks_on_out_queue--;
   9732 				if ((int)sctppcbinfo.ipi_count_chunk < 0) {
   9733 					panic("Chunk count is negative");
   9734 				}
   9735 				sctppcbinfo.ipi_gencnt_chunk++;
   9736 				chk = TAILQ_FIRST(&tmp);
   9737 			}
   9738 			goto release;
   9739 		}
   9740 	}
   9741 zap_by_it_now:
   9742 #ifdef SCTP_MBCNT_LOGGING
   9743 	sctp_log_mbcnt(SCTP_LOG_MBCNT_INCREASE,
   9744 		       asoc->total_output_queue_size,
   9745 		       dataout,
   9746 		       asoc->total_output_mbuf_queue_size,
   9747 		       mbcnt);
   9748 #endif
   9749 	solock(so);
   9750 	asoc->total_output_queue_size += dataout;
   9751 	asoc->total_output_mbuf_queue_size += mbcnt;
   9752 	if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
   9753 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
   9754 		so->so_snd.sb_cc += dataout;
   9755 		so->so_snd.sb_mbcnt += mbcnt;
   9756 	}
   9757 	if ((srcv->sinfo_flags & SCTP_EOF) &&
   9758 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)
   9759 		) {
   9760 		int some_on_streamwheel = 0;
   9761 		error = 0;
   9762 		if (!TAILQ_EMPTY(&asoc->out_wheel)) {
   9763 			/* Check to see if some data queued */
   9764 			struct sctp_stream_out *outs;
   9765 			TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) {
   9766 				if (!TAILQ_EMPTY(&outs->outqueue)) {
   9767 					some_on_streamwheel = 1;
   9768 					break;
   9769 				}
   9770 			}
   9771 		}
   9772 		if (TAILQ_EMPTY(&asoc->send_queue) &&
   9773 		    TAILQ_EMPTY(&asoc->sent_queue) &&
   9774 		    (some_on_streamwheel == 0)) {
   9775 			/* there is nothing queued to send, so I'm done... */
   9776 			if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
   9777 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
   9778 				/* only send SHUTDOWN the first time through */
   9779 #ifdef SCTP_DEBUG
   9780 				if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
   9781 					printf("%s:%d sends a shutdown\n",
   9782 					       __FILE__,
   9783 					       __LINE__
   9784 						);
   9785 				}
   9786 #endif
   9787 				sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
   9788 				asoc->state = SCTP_STATE_SHUTDOWN_SENT;
   9789 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
   9790 						 asoc->primary_destination);
   9791 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
   9792 						 asoc->primary_destination);
   9793 			}
   9794 		} else {
   9795 			/*
   9796 			 * we still got (or just got) data to send, so set
   9797 			 * SHUTDOWN_PENDING
   9798 			 */
   9799 			/*
   9800 			 * XXX sockets draft says that SCTP_EOF should be sent
   9801 			 * with no data.  currently, we will allow user data
   9802 			 * to be sent first and move to SHUTDOWN-PENDING
   9803 			 */
   9804 			asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
   9805 		}
   9806 	}
   9807 #ifdef SCTP_DEBUG
   9808 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
   9809 		printf("++total out:%d total_mbuf_out:%d\n",
   9810 		       (int)asoc->total_output_queue_size,
   9811 		       (int)asoc->total_output_mbuf_queue_size);
   9812 	}
   9813 #endif
   9814 
   9815 release:
   9816 	sbunlock(&so->so_snd);
   9817 out_locked:
   9818 	sounlock(so);
   9819 
   9820 	if (mm)
   9821 		sctp_m_freem(mm);
   9822 	return (error);
   9823 }
   9824 
   9825 
   9826 int
   9827 sctp_sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
   9828 	    struct mbuf *top, struct mbuf *control, int flags, struct lwp *p)
   9829 {
   9830 	int error, use_rcvinfo;
   9831 	int queue_only = 0, queue_only_for_init=0;
   9832 	int un_sent = 0;
   9833 	int now_filled=0;
   9834 	struct sctp_inpcb *inp;
   9835  	struct sctp_tcb *stcb=NULL;
   9836 	struct sctp_sndrcvinfo srcv;
   9837 	struct timeval now;
   9838 	struct sctp_nets *net;
   9839 	struct sctp_association *asoc;
   9840 	struct sctp_inpcb *t_inp;
   9841 	int create_lock_applied = 0;
   9842 
   9843 	error = use_rcvinfo = 0;
   9844 	net = NULL;
   9845 	stcb = NULL;
   9846 	asoc = NULL;
   9847 	t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
   9848 
   9849 	solock(so);
   9850 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
   9851 	    (inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING)) {
   9852 		/* The listner can NOT send */
   9853 		error = EFAULT;
   9854 		sounlock(so);
   9855 		goto out;
   9856 	}
   9857 	if (addr) {
   9858 		SCTP_ASOC_CREATE_LOCK(inp);
   9859 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
   9860 		    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
   9861 			/* Should I really unlock ? */
   9862 			error = EFAULT;
   9863 			sounlock(so);
   9864 			goto out;
   9865 
   9866 		}
   9867 		create_lock_applied = 1;
   9868 		if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
   9869 		    (addr->sa_family == AF_INET6)) {
   9870 			error = EINVAL;
   9871 			sounlock(so);
   9872 			goto out;
   9873 		}
   9874 	}
   9875 	/* now we must find the assoc */
   9876 	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
   9877 		SCTP_INP_RLOCK(inp);
   9878 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
   9879 		if (stcb == NULL) {
   9880 			SCTP_INP_RUNLOCK(inp);
   9881 			error = ENOTCONN;
   9882 			sounlock(so);
   9883 			goto out;
   9884 		}
   9885 		SCTP_TCB_LOCK(stcb);
   9886 		SCTP_INP_RUNLOCK(inp);
   9887 		net = stcb->asoc.primary_destination;
   9888 	}
   9889 #ifdef SCTP_DEBUG
   9890 	printf("sctp_sosend: get control\n");
   9891 #endif
   9892 	/* get control */
   9893 	if (control) {
   9894 		/* process cmsg snd/rcv info (maybe a assoc-id) */
   9895 		if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&srcv, control,
   9896 				   sizeof(srcv))) {
   9897 			/* got one */
   9898 			if (srcv.sinfo_flags & SCTP_SENDALL) {
   9899 				/* its a sendall */
   9900 				sctppcbinfo.mbuf_track--;
   9901 				sctp_m_freem(control);
   9902 
   9903 				if (create_lock_applied) {
   9904 					SCTP_ASOC_CREATE_UNLOCK(inp);
   9905 					create_lock_applied = 0;
   9906 				}
   9907 				return (sctp_sendall(inp, uio, top, &srcv));
   9908 			}
   9909 			use_rcvinfo = 1;
   9910 		}
   9911 	}
   9912 #ifdef SCTP_DEBUG
   9913 	printf("sctp_sosend: doing lookup\n");
   9914 #endif
   9915 	if (stcb == NULL) {
   9916 		/* Need to do a lookup */
   9917 		if (use_rcvinfo && srcv.sinfo_assoc_id) {
   9918 			stcb = sctp_findassociation_ep_asocid(inp, srcv.sinfo_assoc_id);
   9919 			/*
   9920 			 * Question: Should I error here if the assoc_id is
   9921 			 * no longer valid? i.e. I can't find it?
   9922 			 */
   9923 			if ((stcb) &&
   9924 			    (addr != NULL)) {
   9925 				/* Must locate the net structure */
   9926 				net = sctp_findnet(stcb, addr);
   9927 			}
   9928 		}
   9929 		if (stcb == NULL) {
   9930 			if (addr != NULL) {
   9931 				/* Since we did not use findep we must
   9932 				 * increment it, and if we don't find a
   9933 				 * tcb decrement it.
   9934 				 */
   9935 				SCTP_INP_WLOCK(inp);
   9936 				SCTP_INP_INCR_REF(inp);
   9937 				SCTP_INP_WUNLOCK(inp);
   9938 				stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
   9939 				if (stcb == NULL) {
   9940 					SCTP_INP_WLOCK(inp);
   9941 					SCTP_INP_DECR_REF(inp);
   9942 					SCTP_INP_WUNLOCK(inp);
   9943 				}
   9944 			}
   9945 		}
   9946 	}
   9947 	if ((stcb == NULL) &&
   9948 	    (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) {
   9949 		error = ENOTCONN;
   9950 		sounlock(so);
   9951 		goto out;
   9952 	} else if ((stcb == NULL) && (addr == NULL)) {
   9953 		error = ENOENT;
   9954 		sounlock(so);
   9955 		goto out;
   9956 	} else if (stcb == NULL) {
   9957 		/* UDP style, we must go ahead and start the INIT process */
   9958 		if ((use_rcvinfo) &&
   9959 		    (srcv.sinfo_flags & SCTP_ABORT)) {
   9960 			/* User asks to abort a non-existant asoc */
   9961 			error = ENOENT;
   9962 			sounlock(so);
   9963 			goto out;
   9964 		}
   9965 		/* get an asoc/stcb struct */
   9966 		stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0);
   9967 		if (stcb == NULL) {
   9968 			/* Error is setup for us in the call */
   9969 			sounlock(so);
   9970 			goto out;
   9971 		}
   9972 		if (create_lock_applied) {
   9973 			SCTP_ASOC_CREATE_UNLOCK(inp);
   9974 			create_lock_applied = 0;
   9975 		} else {
   9976 			printf("Huh-3? create lock should have been on??\n");
   9977 		}
   9978 		/* Turn on queue only flag to prevent data from being sent */
   9979  		queue_only = 1;
   9980 		asoc = &stcb->asoc;
   9981 		asoc->state = SCTP_STATE_COOKIE_WAIT;
   9982 		SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
   9983 		if (control) {
   9984 			/* see if a init structure exists in cmsg headers */
   9985 			struct sctp_initmsg initm;
   9986 			int i;
   9987 			if (sctp_find_cmsg(SCTP_INIT, (void *)&initm, control, sizeof(initm))) {
   9988 				/* we have an INIT override of the default */
   9989 				if (initm.sinit_max_attempts)
   9990 					asoc->max_init_times = initm.sinit_max_attempts;
   9991 				if (initm.sinit_num_ostreams)
   9992 					asoc->pre_open_streams = initm.sinit_num_ostreams;
   9993 				if (initm.sinit_max_instreams)
   9994 					asoc->max_inbound_streams = initm.sinit_max_instreams;
   9995 				if (initm.sinit_max_init_timeo)
   9996 					asoc->initial_init_rto_max = initm.sinit_max_init_timeo;
   9997 				if (asoc->streamoutcnt < asoc->pre_open_streams) {
   9998 					/* Default is NOT correct */
   9999 #ifdef SCTP_DEBUG
   10000 					if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   10001 						printf("Ok, defout:%d pre_open:%d\n",
   10002 						       asoc->streamoutcnt, asoc->pre_open_streams);
   10003 					}
   10004 #endif
   10005 					free(asoc->strmout, M_PCB);
   10006 					asoc->strmout = NULL;
   10007 					asoc->streamoutcnt = asoc->pre_open_streams;
   10008 
   10009 					/* What happesn if this fails? .. we panic ...*/
   10010 					asoc->strmout = malloc(
   10011 					       asoc->streamoutcnt *
   10012 					       sizeof(struct sctp_stream_out),
   10013 					       M_PCB, M_WAIT);
   10014 					for (i = 0; i < asoc->streamoutcnt; i++) {
   10015 						/*
   10016 						 * inbound side must be set to 0xffff,
   10017 						 * also NOTE when we get the INIT-ACK
   10018 						 * back (for INIT sender) we MUST
   10019 						 * reduce the count (streamoutcnt) but
   10020 						 * first check if we sent to any of the
   10021 						 * upper streams that were dropped (if
   10022 						 * some were). Those that were dropped
   10023 						 * must be notified to the upper layer
   10024 						 * as failed to send.
   10025 						 */
   10026 						asoc->strmout[i].next_sequence_sent = 0x0;
   10027 						TAILQ_INIT(&asoc->strmout[i].outqueue);
   10028 						asoc->strmout[i].stream_no = i;
   10029 						asoc->strmout[i].next_spoke.tqe_next = 0;
   10030 						asoc->strmout[i].next_spoke.tqe_prev = 0;
   10031 					}
   10032 				}
   10033 			}
   10034 
   10035 		}
   10036 		/* out with the INIT */
   10037 		queue_only_for_init = 1;
   10038 		sctp_send_initiate(inp, stcb);
   10039 		/*
   10040 		 * we may want to dig in after this call and adjust the MTU
   10041 		 * value. It defaulted to 1500 (constant) but the ro structure
   10042 		 * may now have an update and thus we may need to change it
   10043 		 * BEFORE we append the message.
   10044 		 */
   10045 		net = stcb->asoc.primary_destination;
   10046 		asoc = &stcb->asoc;
   10047 	} else {
   10048 		asoc = &stcb->asoc;
   10049 	}
   10050 	if (create_lock_applied) {
   10051 		SCTP_ASOC_CREATE_UNLOCK(inp);
   10052 		create_lock_applied = 0;
   10053 	}
   10054 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
   10055 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
   10056 		queue_only = 1;
   10057 	}
   10058 	if (use_rcvinfo == 0) {
   10059 		/* Grab the default stuff from the asoc */
   10060 		srcv = stcb->asoc.def_send;
   10061 	}
   10062 	/* we are now done with all control */
   10063 	if (control) {
   10064 		sctp_m_freem(control);
   10065 		control = NULL;
   10066 	}
   10067 
   10068 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
   10069 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
   10070 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
   10071 	    (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
   10072 		if ((use_rcvinfo) &&
   10073 		    (srcv.sinfo_flags & SCTP_ABORT)) {
   10074 			;
   10075 		} else {
   10076 			error = ECONNRESET;
   10077 			sounlock(so);
   10078 			goto out;
   10079 		}
   10080 	}
   10081 	/* Ok, we will attempt a msgsnd :> */
   10082 #if 0	/* XXX */
   10083 	if (p)
   10084 		p->p_stats->p_ru.ru_msgsnd++;
   10085 #endif
   10086 
   10087 	if (stcb) {
   10088 		if (net && ((srcv.sinfo_flags & SCTP_ADDR_OVER))) {
   10089 			/* we take the override or the unconfirmed */
   10090 			;
   10091 		} else {
   10092 			net = stcb->asoc.primary_destination;
   10093 		}
   10094 	}
   10095 
   10096 #ifdef SCTP_DEBUG
   10097 	printf("sctp_sosend: before copying in %p\n", top);
   10098 #endif
   10099 	if (top == NULL) {
   10100 		/* Must copy it all in from user land. The
   10101 		 * socket buf is locked but we don't suspend
   10102 		 * protocol processing until we are ready to
   10103 		 * send/queue it.
   10104 		 */
   10105 		sounlock(so);
   10106 #ifdef SCTP_DEBUG
   10107 		printf("sctp_sosend: before cii\n");
   10108 #endif
   10109 		error = sctp_copy_it_in(inp, stcb, asoc, net, &srcv, uio, flags);
   10110 #ifdef SCTP_DEBUG
   10111 		printf("sctp_sosend: after cii\n");
   10112 #endif
   10113 		if (error)
   10114 			goto out;
   10115 	} else {
   10116 		/* Here we must either pull in the user data to chunk
   10117 		 * buffers, or use top to do a msg_append.
   10118 		 */
   10119  		error = sctp_msg_append(stcb, net, top, &srcv, flags);
   10120 		sounlock(so);
   10121 		if (error)
   10122 			goto out;
   10123 		/* zap the top since it is now being used */
   10124 		top = 0;
   10125 	}
   10126 #ifdef SCTP_DEBUG
   10127 	printf("sctp_sosend: after copying in\n");
   10128 #endif
   10129 	if (net->flight_size > net->cwnd) {
   10130 		sctp_pegs[SCTP_SENDTO_FULL_CWND]++;
   10131 		queue_only = 1;
   10132 
   10133 	} else if (asoc->ifp_had_enobuf) {
   10134 		sctp_pegs[SCTP_QUEONLY_BURSTLMT]++;
   10135 		queue_only = 1;
   10136  	} else {
   10137 		un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
   10138 			   ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) * sizeof(struct sctp_data_chunk)) +
   10139 			   SCTP_MED_OVERHEAD);
   10140 
   10141 		if (((inp->sctp_flags & SCTP_PCB_FLAGS_NODELAY) == 0) &&
   10142 		    (stcb->asoc.total_flight > 0) &&
   10143 		    (un_sent < (int)stcb->asoc.smallest_mtu)) {
   10144 
   10145 			/* Ok, Nagle is set on and we have data outstanding. Don't
   10146 			 * send anything and let SACKs drive out the data unless we
   10147 			 * have a "full" segment to send.
   10148 			 */
   10149 			sctp_pegs[SCTP_NAGLE_NOQ]++;
   10150 			queue_only = 1;
   10151 		} else {
   10152 			sctp_pegs[SCTP_NAGLE_OFF]++;
   10153 		}
   10154 	}
   10155 	if (queue_only_for_init) {
   10156 		/* It is possible to have a turn around of the
   10157 		 * INIT/INIT-ACK/COOKIE before I have a chance to
   10158 		 * copy in the data. In such a case I DO want to
   10159 		 * send it out by reversing the queue only flag.
   10160 		 */
   10161 		if ((SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) ||
   10162 		    (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_ECHOED)) {
   10163 			/* yep, reverse it */
   10164 			queue_only = 0;
   10165 		}
   10166  	}
   10167 
   10168 #ifdef SCTP_DEBUG
   10169 	printf("sctp_sosend: before sending chunk\n");
   10170 #endif
   10171 	if ((queue_only == 0) && (stcb->asoc.peers_rwnd  && un_sent)) {
   10172 		/* we can attempt to send too.*/
   10173 #ifdef SCTP_DEBUG
   10174 		if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   10175 			printf("USR Send calls sctp_chunk_output\n");
   10176 		}
   10177 #endif
   10178 		solock(so);
   10179 		sctp_pegs[SCTP_OUTPUT_FRM_SND]++;
   10180 		sctp_chunk_output(inp, stcb, 0);
   10181 		sounlock(so);
   10182 	} else if ((queue_only == 0) &&
   10183 		   (stcb->asoc.peers_rwnd == 0) &&
   10184 		   (stcb->asoc.total_flight == 0)) {
   10185 		/* We get to have a probe outstanding */
   10186 		solock(so);
   10187 		sctp_from_user_send = 1;
   10188 		sctp_chunk_output(inp, stcb, 0);
   10189 		sctp_from_user_send = 0;
   10190 		sounlock(so);
   10191 
   10192 	} else if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
   10193 		int num_out, reason, cwnd_full;
   10194 		/* Here we do control only */
   10195 		solock(so);
   10196 		sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
   10197 				      &reason, 1, &cwnd_full, 1, &now, &now_filled);
   10198 		sounlock(so);
   10199 	}
   10200 #ifdef SCTP_DEBUG
   10201 	if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
   10202 		printf("USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d \n",
   10203 		       queue_only, stcb->asoc.peers_rwnd, un_sent,
   10204 		       stcb->asoc.total_flight, stcb->asoc.chunks_on_out_queue,
   10205 		       stcb->asoc.total_output_queue_size);
   10206 	}
   10207 #endif
   10208  out:
   10209 	if (create_lock_applied) {
   10210 		SCTP_ASOC_CREATE_UNLOCK(inp);
   10211 		create_lock_applied = 0;
   10212 	}
   10213 	if (stcb) {
   10214 		SCTP_TCB_UNLOCK(stcb);
   10215 	}
   10216 	if (top)
   10217 		sctp_m_freem(top);
   10218 	if (control)
   10219 		sctp_m_freem(control);
   10220 	return (error);
   10221 }
   10222